blob: 5929cd0baa2077ebce9a3b0c51d528de9b89ac33 [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
Jan Kara4eec7082013-04-11 23:56:53 -040064static void ext4_release_io_end(ext4_io_end_t *io_end)
Theodore Ts'obd2d0212010-10-27 21:30:10 -040065{
Jan Kara4eec7082013-04-11 23:56:53 -040066 BUG_ON(!list_empty(&io_end->list));
67 BUG_ON(io_end->flag & EXT4_IO_END_UNWRITTEN);
Theodore Ts'obd2d0212010-10-27 21:30:10 -040068
Jan Kara4eec7082013-04-11 23:56:53 -040069 if (atomic_dec_and_test(&EXT4_I(io_end->inode)->i_ioend_count))
70 wake_up_all(ext4_ioend_wq(io_end->inode));
71 if (io_end->flag & EXT4_IO_END_DIRECT)
72 inode_dio_done(io_end->inode);
73 if (io_end->iocb)
74 aio_complete(io_end->iocb, io_end->result, 0);
75 kmem_cache_free(io_end_cachep, io_end);
76}
Dmitry Monakhov82e54222012-09-28 23:36:25 -040077
Jan Kara4eec7082013-04-11 23:56:53 -040078static void ext4_clear_io_unwritten_flag(ext4_io_end_t *io_end)
79{
80 struct inode *inode = io_end->inode;
81
82 io_end->flag &= ~EXT4_IO_END_UNWRITTEN;
83 /* Wake up anyone waiting on unwritten extent conversion */
84 if (atomic_dec_and_test(&EXT4_I(inode)->i_unwritten))
85 wake_up_all(ext4_ioend_wq(inode));
Theodore Ts'obd2d0212010-10-27 21:30:10 -040086}
87
Dmitry Monakhov28a535f2012-09-29 00:14:55 -040088/* check a range of space and convert unwritten extents to written. */
89static int ext4_end_io(ext4_io_end_t *io)
Theodore Ts'obd2d0212010-10-27 21:30:10 -040090{
91 struct inode *inode = io->inode;
92 loff_t offset = io->offset;
93 ssize_t size = io->size;
94 int ret = 0;
95
96 ext4_debug("ext4_end_io_nolock: io 0x%p from inode %lu,list->next 0x%p,"
97 "list->prev 0x%p\n",
98 io, inode->i_ino, io->list.next, io->list.prev);
99
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400100 ret = ext4_convert_unwritten_extents(inode, offset, size);
101 if (ret < 0) {
Theodore Ts'ob82e3842011-10-31 10:56:32 -0400102 ext4_msg(inode->i_sb, KERN_EMERG,
103 "failed to convert unwritten extents to written "
104 "extents -- potential data loss! "
105 "(inode %lu, offset %llu, size %zd, error %d)",
106 inode->i_ino, offset, size, ret);
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400107 }
Jan Kara4eec7082013-04-11 23:56:53 -0400108 ext4_clear_io_unwritten_flag(io);
109 ext4_release_io_end(io);
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400110 return ret;
111}
112
Dmitry Monakhov28a535f2012-09-29 00:14:55 -0400113static void dump_completed_IO(struct inode *inode)
114{
115#ifdef EXT4FS_DEBUG
116 struct list_head *cur, *before, *after;
117 ext4_io_end_t *io, *io0, *io1;
Dmitry Monakhov28a535f2012-09-29 00:14:55 -0400118
119 if (list_empty(&EXT4_I(inode)->i_completed_io_list)) {
120 ext4_debug("inode %lu completed_io list is empty\n",
121 inode->i_ino);
122 return;
123 }
124
125 ext4_debug("Dump inode %lu completed_io list\n", inode->i_ino);
126 list_for_each_entry(io, &EXT4_I(inode)->i_completed_io_list, list) {
127 cur = &io->list;
128 before = cur->prev;
129 io0 = container_of(before, ext4_io_end_t, list);
130 after = cur->next;
131 io1 = container_of(after, ext4_io_end_t, list);
132
133 ext4_debug("io 0x%p from inode %lu,prev 0x%p,next 0x%p\n",
134 io, inode->i_ino, io0, io1);
135 }
136#endif
137}
138
139/* Add the io_end to per-inode completed end_io list. */
Jan Kara4eec7082013-04-11 23:56:53 -0400140static void ext4_add_complete_io(ext4_io_end_t *io_end)
Dmitry Monakhov28a535f2012-09-29 00:14:55 -0400141{
142 struct ext4_inode_info *ei = EXT4_I(io_end->inode);
143 struct workqueue_struct *wq;
144 unsigned long flags;
145
146 BUG_ON(!(io_end->flag & EXT4_IO_END_UNWRITTEN));
147 wq = EXT4_SB(io_end->inode->i_sb)->dio_unwritten_wq;
148
149 spin_lock_irqsave(&ei->i_completed_io_lock, flags);
Jan Kara84c17542013-01-28 09:43:46 -0500150 if (list_empty(&ei->i_completed_io_list))
151 queue_work(wq, &ei->i_unwritten_work);
Dmitry Monakhov28a535f2012-09-29 00:14:55 -0400152 list_add_tail(&io_end->list, &ei->i_completed_io_list);
153 spin_unlock_irqrestore(&ei->i_completed_io_lock, flags);
154}
155
Jan Kara84c17542013-01-28 09:43:46 -0500156static int ext4_do_flush_completed_IO(struct inode *inode)
Dmitry Monakhov28a535f2012-09-29 00:14:55 -0400157{
158 ext4_io_end_t *io;
Jan Kara002bd7f2013-01-28 09:49:15 -0500159 struct list_head unwritten;
Dmitry Monakhov28a535f2012-09-29 00:14:55 -0400160 unsigned long flags;
161 struct ext4_inode_info *ei = EXT4_I(inode);
162 int err, ret = 0;
163
Dmitry Monakhov28a535f2012-09-29 00:14:55 -0400164 spin_lock_irqsave(&ei->i_completed_io_lock, flags);
165 dump_completed_IO(inode);
166 list_replace_init(&ei->i_completed_io_list, &unwritten);
167 spin_unlock_irqrestore(&ei->i_completed_io_lock, flags);
168
169 while (!list_empty(&unwritten)) {
170 io = list_entry(unwritten.next, ext4_io_end_t, list);
171 BUG_ON(!(io->flag & EXT4_IO_END_UNWRITTEN));
172 list_del_init(&io->list);
173
174 err = ext4_end_io(io);
175 if (unlikely(!ret && err))
176 ret = err;
Dmitry Monakhov28a535f2012-09-29 00:14:55 -0400177 }
178 return ret;
179}
180
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400181/*
182 * work on completed aio dio IO, to convert unwritten extents to extents
183 */
Jan Kara84c17542013-01-28 09:43:46 -0500184void ext4_end_io_work(struct work_struct *work)
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400185{
Jan Kara84c17542013-01-28 09:43:46 -0500186 struct ext4_inode_info *ei = container_of(work, struct ext4_inode_info,
187 i_unwritten_work);
188 ext4_do_flush_completed_IO(&ei->vfs_inode);
Dmitry Monakhov28a535f2012-09-29 00:14:55 -0400189}
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400190
Dmitry Monakhovc2785312012-10-05 11:31:55 -0400191int ext4_flush_unwritten_io(struct inode *inode)
Dmitry Monakhov28a535f2012-09-29 00:14:55 -0400192{
Dmitry Monakhovc2785312012-10-05 11:31:55 -0400193 int ret;
194 WARN_ON_ONCE(!mutex_is_locked(&inode->i_mutex) &&
195 !(inode->i_state & I_FREEING));
Jan Kara84c17542013-01-28 09:43:46 -0500196 ret = ext4_do_flush_completed_IO(inode);
Dmitry Monakhovc2785312012-10-05 11:31:55 -0400197 ext4_unwritten_wait(inode);
198 return ret;
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400199}
200
201ext4_io_end_t *ext4_init_io_end(struct inode *inode, gfp_t flags)
202{
Jesper Juhlb17b35e2010-12-19 21:41:55 -0500203 ext4_io_end_t *io = kmem_cache_zalloc(io_end_cachep, flags);
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400204 if (io) {
Theodore Ts'of7ad6d22010-11-08 13:43:33 -0500205 atomic_inc(&EXT4_I(inode)->i_ioend_count);
206 io->inode = inode;
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400207 INIT_LIST_HEAD(&io->list);
Jan Kara4eec7082013-04-11 23:56:53 -0400208 atomic_set(&io->count, 1);
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400209 }
210 return io;
211}
212
Jan Kara4eec7082013-04-11 23:56:53 -0400213void ext4_put_io_end_defer(ext4_io_end_t *io_end)
214{
215 if (atomic_dec_and_test(&io_end->count)) {
216 if (!(io_end->flag & EXT4_IO_END_UNWRITTEN) || !io_end->size) {
217 ext4_release_io_end(io_end);
218 return;
219 }
220 ext4_add_complete_io(io_end);
221 }
222}
223
224int ext4_put_io_end(ext4_io_end_t *io_end)
225{
226 int err = 0;
227
228 if (atomic_dec_and_test(&io_end->count)) {
229 if (io_end->flag & EXT4_IO_END_UNWRITTEN) {
230 err = ext4_convert_unwritten_extents(io_end->inode,
231 io_end->offset, io_end->size);
232 ext4_clear_io_unwritten_flag(io_end);
233 }
234 ext4_release_io_end(io_end);
235 }
236 return err;
237}
238
239ext4_io_end_t *ext4_get_io_end(ext4_io_end_t *io_end)
240{
241 atomic_inc(&io_end->count);
242 return io_end;
243}
244
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400245/*
246 * Print an buffer I/O error compatible with the fs/buffer.c. This
247 * provides compatibility with dmesg scrapers that look for a specific
248 * buffer I/O error message. We really need a unified error reporting
249 * structure to userspace ala Digital Unix's uerf system, but it's
250 * probably not going to happen in my lifetime, due to LKML politics...
251 */
252static void buffer_io_error(struct buffer_head *bh)
253{
254 char b[BDEVNAME_SIZE];
255 printk(KERN_ERR "Buffer I/O error on device %s, logical block %llu\n",
256 bdevname(bh->b_bdev, b),
257 (unsigned long long)bh->b_blocknr);
258}
259
260static void ext4_end_bio(struct bio *bio, int error)
261{
262 ext4_io_end_t *io_end = bio->bi_private;
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400263 struct inode *inode;
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400264 int i;
Jan Kara0058f962013-04-11 23:48:32 -0400265 int blocksize;
Curt Wohlgemuthd50bdd52011-02-07 12:46:14 -0500266 sector_t bi_sector = bio->bi_sector;
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400267
268 BUG_ON(!io_end);
Jan Kara0058f962013-04-11 23:48:32 -0400269 inode = io_end->inode;
270 blocksize = 1 << inode->i_blkbits;
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400271 bio->bi_private = NULL;
272 bio->bi_end_io = NULL;
273 if (test_bit(BIO_UPTODATE, &bio->bi_flags))
274 error = 0;
Jan Kara0058f962013-04-11 23:48:32 -0400275 for (i = 0; i < bio->bi_vcnt; i++) {
276 struct bio_vec *bvec = &bio->bi_io_vec[i];
277 struct page *page = bvec->bv_page;
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400278 struct buffer_head *bh, *head;
Jan Kara0058f962013-04-11 23:48:32 -0400279 unsigned bio_start = bvec->bv_offset;
280 unsigned bio_end = bio_start + bvec->bv_len;
281 unsigned under_io = 0;
282 unsigned long flags;
283
284 if (!page)
285 continue;
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400286
Curt Wohlgemuth39db00f2011-04-30 13:26:26 -0400287 if (error) {
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400288 SetPageError(page);
Curt Wohlgemuth39db00f2011-04-30 13:26:26 -0400289 set_bit(AS_EIO, &page->mapping->flags);
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400290 }
Jan Kara0058f962013-04-11 23:48:32 -0400291 bh = head = page_buffers(page);
292 /*
293 * We check all buffers in the page under BH_Uptodate_Lock
294 * to avoid races with other end io clearing async_write flags
295 */
296 local_irq_save(flags);
297 bit_spin_lock(BH_Uptodate_Lock, &head->b_state);
298 do {
299 if (bh_offset(bh) < bio_start ||
300 bh_offset(bh) + blocksize > bio_end) {
301 if (buffer_async_write(bh))
302 under_io++;
303 continue;
304 }
305 clear_buffer_async_write(bh);
306 if (error)
307 buffer_io_error(bh);
308 } while ((bh = bh->b_this_page) != head);
309 bit_spin_unlock(BH_Uptodate_Lock, &head->b_state);
310 local_irq_restore(flags);
311 if (!under_io)
312 end_page_writeback(page);
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400313 }
Jan Kara0058f962013-04-11 23:48:32 -0400314 bio_put(bio);
Theodore Ts'of7ad6d22010-11-08 13:43:33 -0500315
316 if (error) {
317 io_end->flag |= EXT4_IO_END_ERROR;
318 ext4_warning(inode->i_sb, "I/O error writing to inode %lu "
319 "(offset %llu size %ld starting block %llu)",
320 inode->i_ino,
321 (unsigned long long) io_end->offset,
322 (long) io_end->size,
323 (unsigned long long)
Curt Wohlgemuthd50bdd52011-02-07 12:46:14 -0500324 bi_sector >> (inode->i_blkbits - 9));
Theodore Ts'of7ad6d22010-11-08 13:43:33 -0500325 }
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400326
Jan Kara4eec7082013-04-11 23:56:53 -0400327 ext4_put_io_end_defer(io_end);
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400328}
329
330void ext4_io_submit(struct ext4_io_submit *io)
331{
332 struct bio *bio = io->io_bio;
333
334 if (bio) {
335 bio_get(io->io_bio);
336 submit_bio(io->io_op, io->io_bio);
337 BUG_ON(bio_flagged(io->io_bio, BIO_EOPNOTSUPP));
338 bio_put(io->io_bio);
339 }
Peter Huewe7dc57612011-02-21 21:01:42 -0500340 io->io_bio = NULL;
Jan Kara4eec7082013-04-11 23:56:53 -0400341}
342
343void ext4_io_submit_init(struct ext4_io_submit *io,
344 struct writeback_control *wbc)
345{
346 io->io_op = (wbc->sync_mode == WB_SYNC_ALL ? WRITE_SYNC : WRITE);
347 io->io_bio = NULL;
Peter Huewe7dc57612011-02-21 21:01:42 -0500348 io->io_end = NULL;
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400349}
350
Jan Kara4eec7082013-04-11 23:56:53 -0400351static int io_submit_init_bio(struct ext4_io_submit *io,
352 struct buffer_head *bh)
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400353{
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400354 int nvecs = bio_get_nr_vecs(bh->b_bdev);
355 struct bio *bio;
356
Theodore Ts'o275d3ba2011-06-29 21:44:45 -0400357 bio = bio_alloc(GFP_NOIO, min(nvecs, BIO_MAX_PAGES));
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400358 bio->bi_sector = bh->b_blocknr * (bh->b_size >> 9);
359 bio->bi_bdev = bh->b_bdev;
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400360 bio->bi_end_io = ext4_end_bio;
Jan Kara4eec7082013-04-11 23:56:53 -0400361 bio->bi_private = ext4_get_io_end(io->io_end);
362 if (!io->io_end->size)
363 io->io_end->offset = (bh->b_page->index << PAGE_CACHE_SHIFT)
364 + bh_offset(bh);
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400365 io->io_bio = bio;
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400366 io->io_next_block = bh->b_blocknr;
367 return 0;
368}
369
370static int io_submit_add_bh(struct ext4_io_submit *io,
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400371 struct inode *inode,
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400372 struct buffer_head *bh)
373{
374 ext4_io_end_t *io_end;
375 int ret;
376
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400377 if (io->io_bio && bh->b_blocknr != io->io_next_block) {
378submit_and_retry:
379 ext4_io_submit(io);
380 }
381 if (io->io_bio == NULL) {
Jan Kara4eec7082013-04-11 23:56:53 -0400382 ret = io_submit_init_bio(io, bh);
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400383 if (ret)
384 return ret;
385 }
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400386 ret = bio_add_page(io->io_bio, bh->b_page, bh->b_size, bh_offset(bh));
387 if (ret != bh->b_size)
388 goto submit_and_retry;
Jan Kara4eec7082013-04-11 23:56:53 -0400389 io_end = io->io_end;
Jan Kara7b001d62013-04-12 00:03:19 -0400390 if (test_clear_buffer_uninit(bh))
Jan Kara4eec7082013-04-11 23:56:53 -0400391 ext4_set_io_unwritten_flag(inode, io_end);
392 io_end->size += bh->b_size;
393 io->io_next_block++;
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400394 return 0;
395}
396
397int ext4_bio_write_page(struct ext4_io_submit *io,
398 struct page *page,
399 int len,
400 struct writeback_control *wbc)
401{
402 struct inode *inode = page->mapping->host;
Jan Kara0058f962013-04-11 23:48:32 -0400403 unsigned block_start, blocksize;
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400404 struct buffer_head *bh, *head;
405 int ret = 0;
Jan Kara0058f962013-04-11 23:48:32 -0400406 int nr_submitted = 0;
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400407
408 blocksize = 1 << inode->i_blkbits;
409
Curt Wohlgemuthd50bdd52011-02-07 12:46:14 -0500410 BUG_ON(!PageLocked(page));
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400411 BUG_ON(PageWriteback(page));
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400412
Theodore Ts'oa54aa762011-02-27 16:43:24 -0500413 set_page_writeback(page);
414 ClearPageError(page);
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400415
Jan Kara0058f962013-04-11 23:48:32 -0400416 /*
417 * In the first loop we prepare and mark buffers to submit. We have to
418 * mark all buffers in the page before submitting so that
419 * end_page_writeback() cannot be called from ext4_bio_end_io() when IO
420 * on the first buffer finishes and we are still working on submitting
421 * the second buffer.
422 */
423 bh = head = page_buffers(page);
424 do {
425 block_start = bh_offset(bh);
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400426 if (block_start >= len) {
Yongqiang Yang5a0dc732011-12-13 22:29:12 -0500427 /*
428 * Comments copied from block_write_full_page_endio:
429 *
430 * The page straddles i_size. It must be zeroed out on
431 * each and every writepage invocation because it may
432 * be mmapped. "A file is mapped in multiples of the
433 * page size. For a file that is not a multiple of
434 * the page size, the remaining memory is zeroed when
435 * mapped, and writes to that region are not written
436 * out to the file."
437 */
Jan Kara0058f962013-04-11 23:48:32 -0400438 zero_user_segment(page, block_start,
439 block_start + blocksize);
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400440 clear_buffer_dirty(bh);
441 set_buffer_uptodate(bh);
442 continue;
443 }
Jan Kara8a850c32013-01-28 20:53:28 -0500444 if (!buffer_dirty(bh) || buffer_delay(bh) ||
445 !buffer_mapped(bh) || buffer_unwritten(bh)) {
446 /* A hole? We can safely clear the dirty bit */
447 if (!buffer_mapped(bh))
448 clear_buffer_dirty(bh);
449 if (io->io_bio)
450 ext4_io_submit(io);
451 continue;
452 }
Jan Kara0058f962013-04-11 23:48:32 -0400453 if (buffer_new(bh)) {
454 clear_buffer_new(bh);
455 unmap_underlying_metadata(bh->b_bdev, bh->b_blocknr);
456 }
457 set_buffer_async_write(bh);
458 } while ((bh = bh->b_this_page) != head);
459
460 /* Now submit buffers to write */
461 bh = head = page_buffers(page);
462 do {
463 if (!buffer_async_write(bh))
464 continue;
Jan Kara4eec7082013-04-11 23:56:53 -0400465 ret = io_submit_add_bh(io, inode, bh);
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400466 if (ret) {
467 /*
468 * We only get here on ENOMEM. Not much else
469 * we can do but mark the page as dirty, and
470 * better luck next time.
471 */
Jan Kara1ae48a62013-01-28 09:32:54 -0500472 redirty_page_for_writepage(wbc, page);
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400473 break;
474 }
Jan Kara0058f962013-04-11 23:48:32 -0400475 nr_submitted++;
Jan Kara1ae48a62013-01-28 09:32:54 -0500476 clear_buffer_dirty(bh);
Jan Kara0058f962013-04-11 23:48:32 -0400477 } while ((bh = bh->b_this_page) != head);
478
479 /* Error stopped previous loop? Clean up buffers... */
480 if (ret) {
481 do {
482 clear_buffer_async_write(bh);
483 bh = bh->b_this_page;
484 } while (bh != head);
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400485 }
486 unlock_page(page);
Jan Kara0058f962013-04-11 23:48:32 -0400487 /* Nothing submitted - we have to end page writeback */
488 if (!nr_submitted)
489 end_page_writeback(page);
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400490 return ret;
491}