blob: 6626aba57ebb7599a33a512d994b17693844de02 [file] [log] [blame]
Theodore Ts'obd2d0212010-10-27 21:30:10 -04001/*
2 * linux/fs/ext4/page-io.c
3 *
4 * This contains the new page_io functions for ext4
5 *
6 * Written by Theodore Ts'o, 2010.
7 */
8
Theodore Ts'obd2d0212010-10-27 21:30:10 -04009#include <linux/fs.h>
10#include <linux/time.h>
11#include <linux/jbd2.h>
12#include <linux/highuid.h>
13#include <linux/pagemap.h>
14#include <linux/quotaops.h>
15#include <linux/string.h>
16#include <linux/buffer_head.h>
17#include <linux/writeback.h>
18#include <linux/pagevec.h>
19#include <linux/mpage.h>
20#include <linux/namei.h>
21#include <linux/uio.h>
22#include <linux/bio.h>
23#include <linux/workqueue.h>
24#include <linux/kernel.h>
25#include <linux/slab.h>
Jan Kara1ae48a62013-01-28 09:32:54 -050026#include <linux/mm.h>
Theodore Ts'obd2d0212010-10-27 21:30:10 -040027
28#include "ext4_jbd2.h"
29#include "xattr.h"
30#include "acl.h"
Theodore Ts'obd2d0212010-10-27 21:30:10 -040031
Jan Kara0058f962013-04-11 23:48:32 -040032static struct kmem_cache *io_end_cachep;
Theodore Ts'obd2d0212010-10-27 21:30:10 -040033
Theodore Ts'o5dabfc72010-10-27 21:30:14 -040034int __init ext4_init_pageio(void)
Theodore Ts'obd2d0212010-10-27 21:30:10 -040035{
Theodore Ts'obd2d0212010-10-27 21:30:10 -040036 io_end_cachep = KMEM_CACHE(ext4_io_end, SLAB_RECLAIM_ACCOUNT);
Jan Kara0058f962013-04-11 23:48:32 -040037 if (io_end_cachep == NULL)
Theodore Ts'obd2d0212010-10-27 21:30:10 -040038 return -ENOMEM;
Theodore Ts'obd2d0212010-10-27 21:30:10 -040039 return 0;
40}
41
Theodore Ts'o5dabfc72010-10-27 21:30:14 -040042void ext4_exit_pageio(void)
Theodore Ts'obd2d0212010-10-27 21:30:10 -040043{
44 kmem_cache_destroy(io_end_cachep);
Theodore Ts'obd2d0212010-10-27 21:30:10 -040045}
46
Theodore Ts'o1ada47d2013-03-20 09:39:42 -040047/*
48 * This function is called by ext4_evict_inode() to make sure there is
49 * no more pending I/O completion work left to do.
50 */
51void ext4_ioend_shutdown(struct inode *inode)
Theodore Ts'of7ad6d22010-11-08 13:43:33 -050052{
Eric Sandeene9e3bce2011-02-12 08:17:34 -050053 wait_queue_head_t *wq = ext4_ioend_wq(inode);
Theodore Ts'of7ad6d22010-11-08 13:43:33 -050054
55 wait_event(*wq, (atomic_read(&EXT4_I(inode)->i_ioend_count) == 0));
Theodore Ts'o1ada47d2013-03-20 09:39:42 -040056 /*
57 * We need to make sure the work structure is finished being
58 * used before we let the inode get destroyed.
59 */
60 if (work_pending(&EXT4_I(inode)->i_unwritten_work))
61 cancel_work_sync(&EXT4_I(inode)->i_unwritten_work);
Theodore Ts'of7ad6d22010-11-08 13:43:33 -050062}
63
Theodore Ts'oa5499842013-05-11 19:07:42 -040064void ext4_free_io_end(ext4_io_end_t *io)
Theodore Ts'obd2d0212010-10-27 21:30:10 -040065{
Theodore Ts'oa5499842013-05-11 19:07:42 -040066 BUG_ON(!io);
67 BUG_ON(!list_empty(&io->list));
68 BUG_ON(io->flag & EXT4_IO_END_UNWRITTEN);
Theodore Ts'obd2d0212010-10-27 21:30:10 -040069
Theodore Ts'oa5499842013-05-11 19:07:42 -040070 if (atomic_dec_and_test(&EXT4_I(io->inode)->i_ioend_count))
71 wake_up_all(ext4_ioend_wq(io->inode));
72 kmem_cache_free(io_end_cachep, io);
Theodore Ts'obd2d0212010-10-27 21:30:10 -040073}
74
Dmitry Monakhov28a535f2012-09-29 00:14:55 -040075/* check a range of space and convert unwritten extents to written. */
76static int ext4_end_io(ext4_io_end_t *io)
Theodore Ts'obd2d0212010-10-27 21:30:10 -040077{
78 struct inode *inode = io->inode;
79 loff_t offset = io->offset;
80 ssize_t size = io->size;
81 int ret = 0;
82
83 ext4_debug("ext4_end_io_nolock: io 0x%p from inode %lu,list->next 0x%p,"
84 "list->prev 0x%p\n",
85 io, inode->i_ino, io->list.next, io->list.prev);
86
Theodore Ts'obd2d0212010-10-27 21:30:10 -040087 ret = ext4_convert_unwritten_extents(inode, offset, size);
88 if (ret < 0) {
Theodore Ts'ob82e3842011-10-31 10:56:32 -040089 ext4_msg(inode->i_sb, KERN_EMERG,
90 "failed to convert unwritten extents to written "
91 "extents -- potential data loss! "
92 "(inode %lu, offset %llu, size %zd, error %d)",
93 inode->i_ino, offset, size, ret);
Theodore Ts'obd2d0212010-10-27 21:30:10 -040094 }
Theodore Ts'oa5499842013-05-11 19:07:42 -040095 /* Wake up anyone waiting on unwritten extent conversion */
96 if (atomic_dec_and_test(&EXT4_I(inode)->i_unwritten))
97 wake_up_all(ext4_ioend_wq(inode));
98 if (io->flag & EXT4_IO_END_DIRECT)
99 inode_dio_done(inode);
100 if (io->iocb)
101 aio_complete(io->iocb, io->result, 0);
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400102 return ret;
103}
104
Dmitry Monakhov28a535f2012-09-29 00:14:55 -0400105static void dump_completed_IO(struct inode *inode)
106{
107#ifdef EXT4FS_DEBUG
108 struct list_head *cur, *before, *after;
109 ext4_io_end_t *io, *io0, *io1;
Dmitry Monakhov28a535f2012-09-29 00:14:55 -0400110
111 if (list_empty(&EXT4_I(inode)->i_completed_io_list)) {
112 ext4_debug("inode %lu completed_io list is empty\n",
113 inode->i_ino);
114 return;
115 }
116
117 ext4_debug("Dump inode %lu completed_io list\n", inode->i_ino);
118 list_for_each_entry(io, &EXT4_I(inode)->i_completed_io_list, list) {
119 cur = &io->list;
120 before = cur->prev;
121 io0 = container_of(before, ext4_io_end_t, list);
122 after = cur->next;
123 io1 = container_of(after, ext4_io_end_t, list);
124
125 ext4_debug("io 0x%p from inode %lu,prev 0x%p,next 0x%p\n",
126 io, inode->i_ino, io0, io1);
127 }
128#endif
129}
130
131/* Add the io_end to per-inode completed end_io list. */
Theodore Ts'oa5499842013-05-11 19:07:42 -0400132void ext4_add_complete_io(ext4_io_end_t *io_end)
Dmitry Monakhov28a535f2012-09-29 00:14:55 -0400133{
134 struct ext4_inode_info *ei = EXT4_I(io_end->inode);
135 struct workqueue_struct *wq;
136 unsigned long flags;
137
138 BUG_ON(!(io_end->flag & EXT4_IO_END_UNWRITTEN));
139 wq = EXT4_SB(io_end->inode->i_sb)->dio_unwritten_wq;
140
141 spin_lock_irqsave(&ei->i_completed_io_lock, flags);
Jan Kara84c17542013-01-28 09:43:46 -0500142 if (list_empty(&ei->i_completed_io_list))
143 queue_work(wq, &ei->i_unwritten_work);
Dmitry Monakhov28a535f2012-09-29 00:14:55 -0400144 list_add_tail(&io_end->list, &ei->i_completed_io_list);
145 spin_unlock_irqrestore(&ei->i_completed_io_lock, flags);
146}
147
Jan Kara84c17542013-01-28 09:43:46 -0500148static int ext4_do_flush_completed_IO(struct inode *inode)
Dmitry Monakhov28a535f2012-09-29 00:14:55 -0400149{
150 ext4_io_end_t *io;
Jan Kara002bd7f2013-01-28 09:49:15 -0500151 struct list_head unwritten;
Dmitry Monakhov28a535f2012-09-29 00:14:55 -0400152 unsigned long flags;
153 struct ext4_inode_info *ei = EXT4_I(inode);
154 int err, ret = 0;
155
Dmitry Monakhov28a535f2012-09-29 00:14:55 -0400156 spin_lock_irqsave(&ei->i_completed_io_lock, flags);
157 dump_completed_IO(inode);
158 list_replace_init(&ei->i_completed_io_list, &unwritten);
159 spin_unlock_irqrestore(&ei->i_completed_io_lock, flags);
160
161 while (!list_empty(&unwritten)) {
162 io = list_entry(unwritten.next, ext4_io_end_t, list);
163 BUG_ON(!(io->flag & EXT4_IO_END_UNWRITTEN));
164 list_del_init(&io->list);
165
166 err = ext4_end_io(io);
167 if (unlikely(!ret && err))
168 ret = err;
Theodore Ts'oa5499842013-05-11 19:07:42 -0400169 io->flag &= ~EXT4_IO_END_UNWRITTEN;
170 ext4_free_io_end(io);
Dmitry Monakhov28a535f2012-09-29 00:14:55 -0400171 }
172 return ret;
173}
174
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400175/*
176 * work on completed aio dio IO, to convert unwritten extents to extents
177 */
Jan Kara84c17542013-01-28 09:43:46 -0500178void ext4_end_io_work(struct work_struct *work)
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400179{
Jan Kara84c17542013-01-28 09:43:46 -0500180 struct ext4_inode_info *ei = container_of(work, struct ext4_inode_info,
181 i_unwritten_work);
182 ext4_do_flush_completed_IO(&ei->vfs_inode);
Dmitry Monakhov28a535f2012-09-29 00:14:55 -0400183}
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400184
Dmitry Monakhovc2785312012-10-05 11:31:55 -0400185int ext4_flush_unwritten_io(struct inode *inode)
Dmitry Monakhov28a535f2012-09-29 00:14:55 -0400186{
Dmitry Monakhovc2785312012-10-05 11:31:55 -0400187 int ret;
188 WARN_ON_ONCE(!mutex_is_locked(&inode->i_mutex) &&
189 !(inode->i_state & I_FREEING));
Jan Kara84c17542013-01-28 09:43:46 -0500190 ret = ext4_do_flush_completed_IO(inode);
Dmitry Monakhovc2785312012-10-05 11:31:55 -0400191 ext4_unwritten_wait(inode);
192 return ret;
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400193}
194
195ext4_io_end_t *ext4_init_io_end(struct inode *inode, gfp_t flags)
196{
Jesper Juhlb17b35e2010-12-19 21:41:55 -0500197 ext4_io_end_t *io = kmem_cache_zalloc(io_end_cachep, flags);
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400198 if (io) {
Theodore Ts'of7ad6d22010-11-08 13:43:33 -0500199 atomic_inc(&EXT4_I(inode)->i_ioend_count);
200 io->inode = inode;
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400201 INIT_LIST_HEAD(&io->list);
202 }
203 return io;
204}
205
206/*
207 * Print an buffer I/O error compatible with the fs/buffer.c. This
208 * provides compatibility with dmesg scrapers that look for a specific
209 * buffer I/O error message. We really need a unified error reporting
210 * structure to userspace ala Digital Unix's uerf system, but it's
211 * probably not going to happen in my lifetime, due to LKML politics...
212 */
213static void buffer_io_error(struct buffer_head *bh)
214{
215 char b[BDEVNAME_SIZE];
216 printk(KERN_ERR "Buffer I/O error on device %s, logical block %llu\n",
217 bdevname(bh->b_bdev, b),
218 (unsigned long long)bh->b_blocknr);
219}
220
221static void ext4_end_bio(struct bio *bio, int error)
222{
223 ext4_io_end_t *io_end = bio->bi_private;
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400224 struct inode *inode;
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400225 int i;
Jan Kara0058f962013-04-11 23:48:32 -0400226 int blocksize;
Curt Wohlgemuthd50bdd52011-02-07 12:46:14 -0500227 sector_t bi_sector = bio->bi_sector;
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400228
229 BUG_ON(!io_end);
Jan Kara0058f962013-04-11 23:48:32 -0400230 inode = io_end->inode;
231 blocksize = 1 << inode->i_blkbits;
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400232 bio->bi_private = NULL;
233 bio->bi_end_io = NULL;
234 if (test_bit(BIO_UPTODATE, &bio->bi_flags))
235 error = 0;
Jan Kara0058f962013-04-11 23:48:32 -0400236 for (i = 0; i < bio->bi_vcnt; i++) {
237 struct bio_vec *bvec = &bio->bi_io_vec[i];
238 struct page *page = bvec->bv_page;
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400239 struct buffer_head *bh, *head;
Jan Kara0058f962013-04-11 23:48:32 -0400240 unsigned bio_start = bvec->bv_offset;
241 unsigned bio_end = bio_start + bvec->bv_len;
242 unsigned under_io = 0;
243 unsigned long flags;
244
245 if (!page)
246 continue;
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400247
Curt Wohlgemuth39db00f2011-04-30 13:26:26 -0400248 if (error) {
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400249 SetPageError(page);
Curt Wohlgemuth39db00f2011-04-30 13:26:26 -0400250 set_bit(AS_EIO, &page->mapping->flags);
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400251 }
Jan Kara0058f962013-04-11 23:48:32 -0400252 bh = head = page_buffers(page);
253 /*
254 * We check all buffers in the page under BH_Uptodate_Lock
255 * to avoid races with other end io clearing async_write flags
256 */
257 local_irq_save(flags);
258 bit_spin_lock(BH_Uptodate_Lock, &head->b_state);
259 do {
260 if (bh_offset(bh) < bio_start ||
261 bh_offset(bh) + blocksize > bio_end) {
262 if (buffer_async_write(bh))
263 under_io++;
264 continue;
265 }
266 clear_buffer_async_write(bh);
267 if (error)
268 buffer_io_error(bh);
269 } while ((bh = bh->b_this_page) != head);
270 bit_spin_unlock(BH_Uptodate_Lock, &head->b_state);
271 local_irq_restore(flags);
272 if (!under_io)
273 end_page_writeback(page);
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400274 }
Jan Kara0058f962013-04-11 23:48:32 -0400275 bio_put(bio);
Theodore Ts'of7ad6d22010-11-08 13:43:33 -0500276
277 if (error) {
278 io_end->flag |= EXT4_IO_END_ERROR;
279 ext4_warning(inode->i_sb, "I/O error writing to inode %lu "
280 "(offset %llu size %ld starting block %llu)",
281 inode->i_ino,
282 (unsigned long long) io_end->offset,
283 (long) io_end->size,
284 (unsigned long long)
Curt Wohlgemuthd50bdd52011-02-07 12:46:14 -0500285 bi_sector >> (inode->i_blkbits - 9));
Theodore Ts'of7ad6d22010-11-08 13:43:33 -0500286 }
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400287
Theodore Ts'oa5499842013-05-11 19:07:42 -0400288 if (!(io_end->flag & EXT4_IO_END_UNWRITTEN)) {
289 ext4_free_io_end(io_end);
290 return;
291 }
292
293 ext4_add_complete_io(io_end);
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400294}
295
296void ext4_io_submit(struct ext4_io_submit *io)
297{
298 struct bio *bio = io->io_bio;
299
300 if (bio) {
301 bio_get(io->io_bio);
302 submit_bio(io->io_op, io->io_bio);
303 BUG_ON(bio_flagged(io->io_bio, BIO_EOPNOTSUPP));
304 bio_put(io->io_bio);
305 }
Peter Huewe7dc57612011-02-21 21:01:42 -0500306 io->io_bio = NULL;
Theodore Ts'oa5499842013-05-11 19:07:42 -0400307 io->io_op = 0;
Peter Huewe7dc57612011-02-21 21:01:42 -0500308 io->io_end = NULL;
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400309}
310
Theodore Ts'oa5499842013-05-11 19:07:42 -0400311static int io_submit_init(struct ext4_io_submit *io,
312 struct inode *inode,
313 struct writeback_control *wbc,
314 struct buffer_head *bh)
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400315{
Theodore Ts'oa5499842013-05-11 19:07:42 -0400316 ext4_io_end_t *io_end;
317 struct page *page = bh->b_page;
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400318 int nvecs = bio_get_nr_vecs(bh->b_bdev);
319 struct bio *bio;
320
Theodore Ts'oa5499842013-05-11 19:07:42 -0400321 io_end = ext4_init_io_end(inode, GFP_NOFS);
322 if (!io_end)
323 return -ENOMEM;
Theodore Ts'o275d3ba2011-06-29 21:44:45 -0400324 bio = bio_alloc(GFP_NOIO, min(nvecs, BIO_MAX_PAGES));
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400325 bio->bi_sector = bh->b_blocknr * (bh->b_size >> 9);
326 bio->bi_bdev = bh->b_bdev;
Theodore Ts'oa5499842013-05-11 19:07:42 -0400327 bio->bi_private = io->io_end = io_end;
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400328 bio->bi_end_io = ext4_end_bio;
Theodore Ts'oa5499842013-05-11 19:07:42 -0400329
330 io_end->offset = (page->index << PAGE_CACHE_SHIFT) + bh_offset(bh);
331
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400332 io->io_bio = bio;
Theodore Ts'oa5499842013-05-11 19:07:42 -0400333 io->io_op = (wbc->sync_mode == WB_SYNC_ALL ? WRITE_SYNC : WRITE);
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400334 io->io_next_block = bh->b_blocknr;
335 return 0;
336}
337
338static int io_submit_add_bh(struct ext4_io_submit *io,
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400339 struct inode *inode,
Theodore Ts'oa5499842013-05-11 19:07:42 -0400340 struct writeback_control *wbc,
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400341 struct buffer_head *bh)
342{
343 ext4_io_end_t *io_end;
344 int ret;
345
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400346 if (io->io_bio && bh->b_blocknr != io->io_next_block) {
347submit_and_retry:
348 ext4_io_submit(io);
349 }
350 if (io->io_bio == NULL) {
Theodore Ts'oa5499842013-05-11 19:07:42 -0400351 ret = io_submit_init(io, inode, wbc, bh);
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400352 if (ret)
353 return ret;
354 }
Jan Kara4eec7082013-04-11 23:56:53 -0400355 io_end = io->io_end;
Jan Kara7b001d62013-04-12 00:03:19 -0400356 if (test_clear_buffer_uninit(bh))
Jan Kara4eec7082013-04-11 23:56:53 -0400357 ext4_set_io_unwritten_flag(inode, io_end);
Theodore Ts'oa5499842013-05-11 19:07:42 -0400358 io->io_end->size += bh->b_size;
Jan Kara4eec7082013-04-11 23:56:53 -0400359 io->io_next_block++;
Theodore Ts'oa5499842013-05-11 19:07:42 -0400360 ret = bio_add_page(io->io_bio, bh->b_page, bh->b_size, bh_offset(bh));
361 if (ret != bh->b_size)
362 goto submit_and_retry;
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400363 return 0;
364}
365
366int ext4_bio_write_page(struct ext4_io_submit *io,
367 struct page *page,
368 int len,
369 struct writeback_control *wbc)
370{
371 struct inode *inode = page->mapping->host;
Jan Kara0058f962013-04-11 23:48:32 -0400372 unsigned block_start, blocksize;
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400373 struct buffer_head *bh, *head;
374 int ret = 0;
Jan Kara0058f962013-04-11 23:48:32 -0400375 int nr_submitted = 0;
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400376
377 blocksize = 1 << inode->i_blkbits;
378
Curt Wohlgemuthd50bdd52011-02-07 12:46:14 -0500379 BUG_ON(!PageLocked(page));
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400380 BUG_ON(PageWriteback(page));
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400381
Theodore Ts'oa54aa762011-02-27 16:43:24 -0500382 set_page_writeback(page);
383 ClearPageError(page);
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400384
Jan Kara0058f962013-04-11 23:48:32 -0400385 /*
386 * In the first loop we prepare and mark buffers to submit. We have to
387 * mark all buffers in the page before submitting so that
388 * end_page_writeback() cannot be called from ext4_bio_end_io() when IO
389 * on the first buffer finishes and we are still working on submitting
390 * the second buffer.
391 */
392 bh = head = page_buffers(page);
393 do {
394 block_start = bh_offset(bh);
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400395 if (block_start >= len) {
Yongqiang Yang5a0dc732011-12-13 22:29:12 -0500396 /*
397 * Comments copied from block_write_full_page_endio:
398 *
399 * The page straddles i_size. It must be zeroed out on
400 * each and every writepage invocation because it may
401 * be mmapped. "A file is mapped in multiples of the
402 * page size. For a file that is not a multiple of
403 * the page size, the remaining memory is zeroed when
404 * mapped, and writes to that region are not written
405 * out to the file."
406 */
Jan Kara0058f962013-04-11 23:48:32 -0400407 zero_user_segment(page, block_start,
408 block_start + blocksize);
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400409 clear_buffer_dirty(bh);
410 set_buffer_uptodate(bh);
411 continue;
412 }
Jan Kara8a850c32013-01-28 20:53:28 -0500413 if (!buffer_dirty(bh) || buffer_delay(bh) ||
414 !buffer_mapped(bh) || buffer_unwritten(bh)) {
415 /* A hole? We can safely clear the dirty bit */
416 if (!buffer_mapped(bh))
417 clear_buffer_dirty(bh);
418 if (io->io_bio)
419 ext4_io_submit(io);
420 continue;
421 }
Jan Kara0058f962013-04-11 23:48:32 -0400422 if (buffer_new(bh)) {
423 clear_buffer_new(bh);
424 unmap_underlying_metadata(bh->b_bdev, bh->b_blocknr);
425 }
426 set_buffer_async_write(bh);
427 } while ((bh = bh->b_this_page) != head);
428
429 /* Now submit buffers to write */
430 bh = head = page_buffers(page);
431 do {
432 if (!buffer_async_write(bh))
433 continue;
Theodore Ts'oa5499842013-05-11 19:07:42 -0400434 ret = io_submit_add_bh(io, inode, wbc, bh);
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400435 if (ret) {
436 /*
437 * We only get here on ENOMEM. Not much else
438 * we can do but mark the page as dirty, and
439 * better luck next time.
440 */
Jan Kara1ae48a62013-01-28 09:32:54 -0500441 redirty_page_for_writepage(wbc, page);
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400442 break;
443 }
Jan Kara0058f962013-04-11 23:48:32 -0400444 nr_submitted++;
Jan Kara1ae48a62013-01-28 09:32:54 -0500445 clear_buffer_dirty(bh);
Jan Kara0058f962013-04-11 23:48:32 -0400446 } while ((bh = bh->b_this_page) != head);
447
448 /* Error stopped previous loop? Clean up buffers... */
449 if (ret) {
450 do {
451 clear_buffer_async_write(bh);
452 bh = bh->b_this_page;
453 } while (bh != head);
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400454 }
455 unlock_page(page);
Jan Kara0058f962013-04-11 23:48:32 -0400456 /* Nothing submitted - we have to end page writeback */
457 if (!nr_submitted)
458 end_page_writeback(page);
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400459 return ret;
460}