blob: 4acf1f78881b6c4c61aa10377a62ea4368106593 [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>
Kent Overstreeta27bb332013-05-07 16:19:08 -070021#include <linux/aio.h>
Theodore Ts'obd2d0212010-10-27 21:30:10 -040022#include <linux/uio.h>
23#include <linux/bio.h>
24#include <linux/workqueue.h>
25#include <linux/kernel.h>
26#include <linux/slab.h>
Jan Kara1ae48a62013-01-28 09:32:54 -050027#include <linux/mm.h>
Theodore Ts'obd2d0212010-10-27 21:30:10 -040028
29#include "ext4_jbd2.h"
30#include "xattr.h"
31#include "acl.h"
Theodore Ts'obd2d0212010-10-27 21:30:10 -040032
Jan Kara0058f962013-04-11 23:48:32 -040033static struct kmem_cache *io_end_cachep;
Theodore Ts'obd2d0212010-10-27 21:30:10 -040034
Theodore Ts'o5dabfc72010-10-27 21:30:14 -040035int __init ext4_init_pageio(void)
Theodore Ts'obd2d0212010-10-27 21:30:10 -040036{
Theodore Ts'obd2d0212010-10-27 21:30:10 -040037 io_end_cachep = KMEM_CACHE(ext4_io_end, SLAB_RECLAIM_ACCOUNT);
Jan Kara0058f962013-04-11 23:48:32 -040038 if (io_end_cachep == NULL)
Theodore Ts'obd2d0212010-10-27 21:30:10 -040039 return -ENOMEM;
Theodore Ts'obd2d0212010-10-27 21:30:10 -040040 return 0;
41}
42
Theodore Ts'o5dabfc72010-10-27 21:30:14 -040043void ext4_exit_pageio(void)
Theodore Ts'obd2d0212010-10-27 21:30:10 -040044{
45 kmem_cache_destroy(io_end_cachep);
Theodore Ts'obd2d0212010-10-27 21:30:10 -040046}
47
Theodore Ts'o1ada47d2013-03-20 09:39:42 -040048/*
49 * This function is called by ext4_evict_inode() to make sure there is
50 * no more pending I/O completion work left to do.
51 */
52void ext4_ioend_shutdown(struct inode *inode)
Theodore Ts'of7ad6d22010-11-08 13:43:33 -050053{
Eric Sandeene9e3bce2011-02-12 08:17:34 -050054 wait_queue_head_t *wq = ext4_ioend_wq(inode);
Theodore Ts'of7ad6d22010-11-08 13:43:33 -050055
56 wait_event(*wq, (atomic_read(&EXT4_I(inode)->i_ioend_count) == 0));
Theodore Ts'o1ada47d2013-03-20 09:39:42 -040057 /*
58 * We need to make sure the work structure is finished being
59 * used before we let the inode get destroyed.
60 */
61 if (work_pending(&EXT4_I(inode)->i_unwritten_work))
62 cancel_work_sync(&EXT4_I(inode)->i_unwritten_work);
Theodore Ts'of7ad6d22010-11-08 13:43:33 -050063}
64
Theodore Ts'oa5499842013-05-11 19:07:42 -040065void ext4_free_io_end(ext4_io_end_t *io)
Theodore Ts'obd2d0212010-10-27 21:30:10 -040066{
Theodore Ts'oa5499842013-05-11 19:07:42 -040067 BUG_ON(!io);
68 BUG_ON(!list_empty(&io->list));
69 BUG_ON(io->flag & EXT4_IO_END_UNWRITTEN);
Theodore Ts'obd2d0212010-10-27 21:30:10 -040070
Theodore Ts'oa5499842013-05-11 19:07:42 -040071 if (atomic_dec_and_test(&EXT4_I(io->inode)->i_ioend_count))
72 wake_up_all(ext4_ioend_wq(io->inode));
73 kmem_cache_free(io_end_cachep, io);
Theodore Ts'obd2d0212010-10-27 21:30:10 -040074}
75
Dmitry Monakhov28a535f2012-09-29 00:14:55 -040076/* check a range of space and convert unwritten extents to written. */
77static int ext4_end_io(ext4_io_end_t *io)
Theodore Ts'obd2d0212010-10-27 21:30:10 -040078{
79 struct inode *inode = io->inode;
80 loff_t offset = io->offset;
81 ssize_t size = io->size;
82 int ret = 0;
83
84 ext4_debug("ext4_end_io_nolock: io 0x%p from inode %lu,list->next 0x%p,"
85 "list->prev 0x%p\n",
86 io, inode->i_ino, io->list.next, io->list.prev);
87
Theodore Ts'obd2d0212010-10-27 21:30:10 -040088 ret = ext4_convert_unwritten_extents(inode, offset, size);
89 if (ret < 0) {
Theodore Ts'ob82e3842011-10-31 10:56:32 -040090 ext4_msg(inode->i_sb, KERN_EMERG,
91 "failed to convert unwritten extents to written "
92 "extents -- potential data loss! "
93 "(inode %lu, offset %llu, size %zd, error %d)",
94 inode->i_ino, offset, size, ret);
Theodore Ts'obd2d0212010-10-27 21:30:10 -040095 }
Theodore Ts'oa5499842013-05-11 19:07:42 -040096 /* Wake up anyone waiting on unwritten extent conversion */
97 if (atomic_dec_and_test(&EXT4_I(inode)->i_unwritten))
98 wake_up_all(ext4_ioend_wq(inode));
99 if (io->flag & EXT4_IO_END_DIRECT)
100 inode_dio_done(inode);
101 if (io->iocb)
102 aio_complete(io->iocb, io->result, 0);
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400103 return ret;
104}
105
Dmitry Monakhov28a535f2012-09-29 00:14:55 -0400106static void dump_completed_IO(struct inode *inode)
107{
108#ifdef EXT4FS_DEBUG
109 struct list_head *cur, *before, *after;
110 ext4_io_end_t *io, *io0, *io1;
Dmitry Monakhov28a535f2012-09-29 00:14:55 -0400111
112 if (list_empty(&EXT4_I(inode)->i_completed_io_list)) {
113 ext4_debug("inode %lu completed_io list is empty\n",
114 inode->i_ino);
115 return;
116 }
117
118 ext4_debug("Dump inode %lu completed_io list\n", inode->i_ino);
119 list_for_each_entry(io, &EXT4_I(inode)->i_completed_io_list, list) {
120 cur = &io->list;
121 before = cur->prev;
122 io0 = container_of(before, ext4_io_end_t, list);
123 after = cur->next;
124 io1 = container_of(after, ext4_io_end_t, list);
125
126 ext4_debug("io 0x%p from inode %lu,prev 0x%p,next 0x%p\n",
127 io, inode->i_ino, io0, io1);
128 }
129#endif
130}
131
132/* Add the io_end to per-inode completed end_io list. */
Theodore Ts'oa5499842013-05-11 19:07:42 -0400133void ext4_add_complete_io(ext4_io_end_t *io_end)
Dmitry Monakhov28a535f2012-09-29 00:14:55 -0400134{
135 struct ext4_inode_info *ei = EXT4_I(io_end->inode);
136 struct workqueue_struct *wq;
137 unsigned long flags;
138
139 BUG_ON(!(io_end->flag & EXT4_IO_END_UNWRITTEN));
140 wq = EXT4_SB(io_end->inode->i_sb)->dio_unwritten_wq;
141
142 spin_lock_irqsave(&ei->i_completed_io_lock, flags);
Jan Kara84c17542013-01-28 09:43:46 -0500143 if (list_empty(&ei->i_completed_io_list))
144 queue_work(wq, &ei->i_unwritten_work);
Dmitry Monakhov28a535f2012-09-29 00:14:55 -0400145 list_add_tail(&io_end->list, &ei->i_completed_io_list);
146 spin_unlock_irqrestore(&ei->i_completed_io_lock, flags);
147}
148
Jan Kara84c17542013-01-28 09:43:46 -0500149static int ext4_do_flush_completed_IO(struct inode *inode)
Dmitry Monakhov28a535f2012-09-29 00:14:55 -0400150{
151 ext4_io_end_t *io;
Jan Kara002bd7f2013-01-28 09:49:15 -0500152 struct list_head unwritten;
Dmitry Monakhov28a535f2012-09-29 00:14:55 -0400153 unsigned long flags;
154 struct ext4_inode_info *ei = EXT4_I(inode);
155 int err, ret = 0;
156
Dmitry Monakhov28a535f2012-09-29 00:14:55 -0400157 spin_lock_irqsave(&ei->i_completed_io_lock, flags);
158 dump_completed_IO(inode);
159 list_replace_init(&ei->i_completed_io_list, &unwritten);
160 spin_unlock_irqrestore(&ei->i_completed_io_lock, flags);
161
162 while (!list_empty(&unwritten)) {
163 io = list_entry(unwritten.next, ext4_io_end_t, list);
164 BUG_ON(!(io->flag & EXT4_IO_END_UNWRITTEN));
165 list_del_init(&io->list);
166
167 err = ext4_end_io(io);
168 if (unlikely(!ret && err))
169 ret = err;
Theodore Ts'oa5499842013-05-11 19:07:42 -0400170 io->flag &= ~EXT4_IO_END_UNWRITTEN;
171 ext4_free_io_end(io);
Dmitry Monakhov28a535f2012-09-29 00:14:55 -0400172 }
173 return ret;
174}
175
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400176/*
177 * work on completed aio dio IO, to convert unwritten extents to extents
178 */
Jan Kara84c17542013-01-28 09:43:46 -0500179void ext4_end_io_work(struct work_struct *work)
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400180{
Jan Kara84c17542013-01-28 09:43:46 -0500181 struct ext4_inode_info *ei = container_of(work, struct ext4_inode_info,
182 i_unwritten_work);
183 ext4_do_flush_completed_IO(&ei->vfs_inode);
Dmitry Monakhov28a535f2012-09-29 00:14:55 -0400184}
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400185
Dmitry Monakhovc2785312012-10-05 11:31:55 -0400186int ext4_flush_unwritten_io(struct inode *inode)
Dmitry Monakhov28a535f2012-09-29 00:14:55 -0400187{
Dmitry Monakhovc2785312012-10-05 11:31:55 -0400188 int ret;
189 WARN_ON_ONCE(!mutex_is_locked(&inode->i_mutex) &&
190 !(inode->i_state & I_FREEING));
Jan Kara84c17542013-01-28 09:43:46 -0500191 ret = ext4_do_flush_completed_IO(inode);
Dmitry Monakhovc2785312012-10-05 11:31:55 -0400192 ext4_unwritten_wait(inode);
193 return ret;
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400194}
195
196ext4_io_end_t *ext4_init_io_end(struct inode *inode, gfp_t flags)
197{
Jesper Juhlb17b35e2010-12-19 21:41:55 -0500198 ext4_io_end_t *io = kmem_cache_zalloc(io_end_cachep, flags);
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400199 if (io) {
Theodore Ts'of7ad6d22010-11-08 13:43:33 -0500200 atomic_inc(&EXT4_I(inode)->i_ioend_count);
201 io->inode = inode;
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400202 INIT_LIST_HEAD(&io->list);
203 }
204 return io;
205}
206
207/*
208 * Print an buffer I/O error compatible with the fs/buffer.c. This
209 * provides compatibility with dmesg scrapers that look for a specific
210 * buffer I/O error message. We really need a unified error reporting
211 * structure to userspace ala Digital Unix's uerf system, but it's
212 * probably not going to happen in my lifetime, due to LKML politics...
213 */
214static void buffer_io_error(struct buffer_head *bh)
215{
216 char b[BDEVNAME_SIZE];
217 printk(KERN_ERR "Buffer I/O error on device %s, logical block %llu\n",
218 bdevname(bh->b_bdev, b),
219 (unsigned long long)bh->b_blocknr);
220}
221
222static void ext4_end_bio(struct bio *bio, int error)
223{
224 ext4_io_end_t *io_end = bio->bi_private;
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400225 struct inode *inode;
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400226 int i;
Jan Kara0058f962013-04-11 23:48:32 -0400227 int blocksize;
Curt Wohlgemuthd50bdd52011-02-07 12:46:14 -0500228 sector_t bi_sector = bio->bi_sector;
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400229
230 BUG_ON(!io_end);
Jan Kara0058f962013-04-11 23:48:32 -0400231 inode = io_end->inode;
232 blocksize = 1 << inode->i_blkbits;
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400233 bio->bi_private = NULL;
234 bio->bi_end_io = NULL;
235 if (test_bit(BIO_UPTODATE, &bio->bi_flags))
236 error = 0;
Jan Kara0058f962013-04-11 23:48:32 -0400237 for (i = 0; i < bio->bi_vcnt; i++) {
238 struct bio_vec *bvec = &bio->bi_io_vec[i];
239 struct page *page = bvec->bv_page;
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400240 struct buffer_head *bh, *head;
Jan Kara0058f962013-04-11 23:48:32 -0400241 unsigned bio_start = bvec->bv_offset;
242 unsigned bio_end = bio_start + bvec->bv_len;
243 unsigned under_io = 0;
244 unsigned long flags;
245
246 if (!page)
247 continue;
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400248
Curt Wohlgemuth39db00f2011-04-30 13:26:26 -0400249 if (error) {
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400250 SetPageError(page);
Curt Wohlgemuth39db00f2011-04-30 13:26:26 -0400251 set_bit(AS_EIO, &page->mapping->flags);
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400252 }
Jan Kara0058f962013-04-11 23:48:32 -0400253 bh = head = page_buffers(page);
254 /*
255 * We check all buffers in the page under BH_Uptodate_Lock
256 * to avoid races with other end io clearing async_write flags
257 */
258 local_irq_save(flags);
259 bit_spin_lock(BH_Uptodate_Lock, &head->b_state);
260 do {
261 if (bh_offset(bh) < bio_start ||
262 bh_offset(bh) + blocksize > bio_end) {
263 if (buffer_async_write(bh))
264 under_io++;
265 continue;
266 }
267 clear_buffer_async_write(bh);
268 if (error)
269 buffer_io_error(bh);
270 } while ((bh = bh->b_this_page) != head);
271 bit_spin_unlock(BH_Uptodate_Lock, &head->b_state);
272 local_irq_restore(flags);
273 if (!under_io)
274 end_page_writeback(page);
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400275 }
Jan Kara0058f962013-04-11 23:48:32 -0400276 bio_put(bio);
Theodore Ts'of7ad6d22010-11-08 13:43:33 -0500277
278 if (error) {
279 io_end->flag |= EXT4_IO_END_ERROR;
280 ext4_warning(inode->i_sb, "I/O error writing to inode %lu "
281 "(offset %llu size %ld starting block %llu)",
282 inode->i_ino,
283 (unsigned long long) io_end->offset,
284 (long) io_end->size,
285 (unsigned long long)
Curt Wohlgemuthd50bdd52011-02-07 12:46:14 -0500286 bi_sector >> (inode->i_blkbits - 9));
Theodore Ts'of7ad6d22010-11-08 13:43:33 -0500287 }
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400288
Theodore Ts'oa5499842013-05-11 19:07:42 -0400289 if (!(io_end->flag & EXT4_IO_END_UNWRITTEN)) {
290 ext4_free_io_end(io_end);
291 return;
292 }
293
294 ext4_add_complete_io(io_end);
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400295}
296
297void ext4_io_submit(struct ext4_io_submit *io)
298{
299 struct bio *bio = io->io_bio;
300
301 if (bio) {
302 bio_get(io->io_bio);
303 submit_bio(io->io_op, io->io_bio);
304 BUG_ON(bio_flagged(io->io_bio, BIO_EOPNOTSUPP));
305 bio_put(io->io_bio);
306 }
Peter Huewe7dc57612011-02-21 21:01:42 -0500307 io->io_bio = NULL;
Theodore Ts'oa5499842013-05-11 19:07:42 -0400308 io->io_op = 0;
Peter Huewe7dc57612011-02-21 21:01:42 -0500309 io->io_end = NULL;
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400310}
311
Theodore Ts'oa5499842013-05-11 19:07:42 -0400312static int io_submit_init(struct ext4_io_submit *io,
313 struct inode *inode,
314 struct writeback_control *wbc,
315 struct buffer_head *bh)
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400316{
Theodore Ts'oa5499842013-05-11 19:07:42 -0400317 ext4_io_end_t *io_end;
318 struct page *page = bh->b_page;
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400319 int nvecs = bio_get_nr_vecs(bh->b_bdev);
320 struct bio *bio;
321
Theodore Ts'oa5499842013-05-11 19:07:42 -0400322 io_end = ext4_init_io_end(inode, GFP_NOFS);
323 if (!io_end)
324 return -ENOMEM;
Theodore Ts'o275d3ba2011-06-29 21:44:45 -0400325 bio = bio_alloc(GFP_NOIO, min(nvecs, BIO_MAX_PAGES));
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400326 bio->bi_sector = bh->b_blocknr * (bh->b_size >> 9);
327 bio->bi_bdev = bh->b_bdev;
Theodore Ts'oa5499842013-05-11 19:07:42 -0400328 bio->bi_private = io->io_end = io_end;
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400329 bio->bi_end_io = ext4_end_bio;
Theodore Ts'oa5499842013-05-11 19:07:42 -0400330
331 io_end->offset = (page->index << PAGE_CACHE_SHIFT) + bh_offset(bh);
332
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400333 io->io_bio = bio;
Theodore Ts'oa5499842013-05-11 19:07:42 -0400334 io->io_op = (wbc->sync_mode == WB_SYNC_ALL ? WRITE_SYNC : WRITE);
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400335 io->io_next_block = bh->b_blocknr;
336 return 0;
337}
338
339static int io_submit_add_bh(struct ext4_io_submit *io,
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400340 struct inode *inode,
Theodore Ts'oa5499842013-05-11 19:07:42 -0400341 struct writeback_control *wbc,
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400342 struct buffer_head *bh)
343{
344 ext4_io_end_t *io_end;
345 int ret;
346
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400347 if (io->io_bio && bh->b_blocknr != io->io_next_block) {
348submit_and_retry:
349 ext4_io_submit(io);
350 }
351 if (io->io_bio == NULL) {
Theodore Ts'oa5499842013-05-11 19:07:42 -0400352 ret = io_submit_init(io, inode, wbc, bh);
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400353 if (ret)
354 return ret;
355 }
Jan Kara4eec7082013-04-11 23:56:53 -0400356 io_end = io->io_end;
Jan Kara7b001d62013-04-12 00:03:19 -0400357 if (test_clear_buffer_uninit(bh))
Jan Kara4eec7082013-04-11 23:56:53 -0400358 ext4_set_io_unwritten_flag(inode, io_end);
Theodore Ts'oa5499842013-05-11 19:07:42 -0400359 io->io_end->size += bh->b_size;
Jan Kara4eec7082013-04-11 23:56:53 -0400360 io->io_next_block++;
Theodore Ts'oa5499842013-05-11 19:07:42 -0400361 ret = bio_add_page(io->io_bio, bh->b_page, bh->b_size, bh_offset(bh));
362 if (ret != bh->b_size)
363 goto submit_and_retry;
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400364 return 0;
365}
366
367int ext4_bio_write_page(struct ext4_io_submit *io,
368 struct page *page,
369 int len,
370 struct writeback_control *wbc)
371{
372 struct inode *inode = page->mapping->host;
Jan Kara0058f962013-04-11 23:48:32 -0400373 unsigned block_start, blocksize;
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400374 struct buffer_head *bh, *head;
375 int ret = 0;
Jan Kara0058f962013-04-11 23:48:32 -0400376 int nr_submitted = 0;
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400377
378 blocksize = 1 << inode->i_blkbits;
379
Curt Wohlgemuthd50bdd52011-02-07 12:46:14 -0500380 BUG_ON(!PageLocked(page));
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400381 BUG_ON(PageWriteback(page));
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400382
Theodore Ts'oa54aa762011-02-27 16:43:24 -0500383 set_page_writeback(page);
384 ClearPageError(page);
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400385
Jan Kara0058f962013-04-11 23:48:32 -0400386 /*
387 * In the first loop we prepare and mark buffers to submit. We have to
388 * mark all buffers in the page before submitting so that
389 * end_page_writeback() cannot be called from ext4_bio_end_io() when IO
390 * on the first buffer finishes and we are still working on submitting
391 * the second buffer.
392 */
393 bh = head = page_buffers(page);
394 do {
395 block_start = bh_offset(bh);
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400396 if (block_start >= len) {
Yongqiang Yang5a0dc732011-12-13 22:29:12 -0500397 /*
398 * Comments copied from block_write_full_page_endio:
399 *
400 * The page straddles i_size. It must be zeroed out on
401 * each and every writepage invocation because it may
402 * be mmapped. "A file is mapped in multiples of the
403 * page size. For a file that is not a multiple of
404 * the page size, the remaining memory is zeroed when
405 * mapped, and writes to that region are not written
406 * out to the file."
407 */
Jan Kara0058f962013-04-11 23:48:32 -0400408 zero_user_segment(page, block_start,
409 block_start + blocksize);
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400410 clear_buffer_dirty(bh);
411 set_buffer_uptodate(bh);
412 continue;
413 }
Jan Kara8a850c32013-01-28 20:53:28 -0500414 if (!buffer_dirty(bh) || buffer_delay(bh) ||
415 !buffer_mapped(bh) || buffer_unwritten(bh)) {
416 /* A hole? We can safely clear the dirty bit */
417 if (!buffer_mapped(bh))
418 clear_buffer_dirty(bh);
419 if (io->io_bio)
420 ext4_io_submit(io);
421 continue;
422 }
Jan Kara0058f962013-04-11 23:48:32 -0400423 if (buffer_new(bh)) {
424 clear_buffer_new(bh);
425 unmap_underlying_metadata(bh->b_bdev, bh->b_blocknr);
426 }
427 set_buffer_async_write(bh);
428 } while ((bh = bh->b_this_page) != head);
429
430 /* Now submit buffers to write */
431 bh = head = page_buffers(page);
432 do {
433 if (!buffer_async_write(bh))
434 continue;
Theodore Ts'oa5499842013-05-11 19:07:42 -0400435 ret = io_submit_add_bh(io, inode, wbc, bh);
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400436 if (ret) {
437 /*
438 * We only get here on ENOMEM. Not much else
439 * we can do but mark the page as dirty, and
440 * better luck next time.
441 */
Jan Kara1ae48a62013-01-28 09:32:54 -0500442 redirty_page_for_writepage(wbc, page);
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400443 break;
444 }
Jan Kara0058f962013-04-11 23:48:32 -0400445 nr_submitted++;
Jan Kara1ae48a62013-01-28 09:32:54 -0500446 clear_buffer_dirty(bh);
Jan Kara0058f962013-04-11 23:48:32 -0400447 } while ((bh = bh->b_this_page) != head);
448
449 /* Error stopped previous loop? Clean up buffers... */
450 if (ret) {
451 do {
452 clear_buffer_async_write(bh);
453 bh = bh->b_this_page;
454 } while (bh != head);
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400455 }
456 unlock_page(page);
Jan Kara0058f962013-04-11 23:48:32 -0400457 /* Nothing submitted - we have to end page writeback */
458 if (!nr_submitted)
459 end_page_writeback(page);
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400460 return ret;
461}