blob: 5f20bc48104185e686727081a9debe855601182a [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
Jan Kara97a851e2013-06-04 11:58:58 -040065static void ext4_release_io_end(ext4_io_end_t *io_end)
Theodore Ts'obd2d0212010-10-27 21:30:10 -040066{
Jan Kara97a851e2013-06-04 11:58:58 -040067 BUG_ON(!list_empty(&io_end->list));
68 BUG_ON(io_end->flag & EXT4_IO_END_UNWRITTEN);
Jan Kara6b523df2013-06-04 13:21:11 -040069 WARN_ON(io_end->handle);
Theodore Ts'obd2d0212010-10-27 21:30:10 -040070
Jan Kara97a851e2013-06-04 11:58:58 -040071 if (atomic_dec_and_test(&EXT4_I(io_end->inode)->i_ioend_count))
72 wake_up_all(ext4_ioend_wq(io_end->inode));
73 if (io_end->flag & EXT4_IO_END_DIRECT)
74 inode_dio_done(io_end->inode);
75 if (io_end->iocb)
76 aio_complete(io_end->iocb, io_end->result, 0);
77 kmem_cache_free(io_end_cachep, io_end);
78}
79
80static void ext4_clear_io_unwritten_flag(ext4_io_end_t *io_end)
81{
82 struct inode *inode = io_end->inode;
83
84 io_end->flag &= ~EXT4_IO_END_UNWRITTEN;
85 /* Wake up anyone waiting on unwritten extent conversion */
86 if (atomic_dec_and_test(&EXT4_I(inode)->i_unwritten))
87 wake_up_all(ext4_ioend_wq(inode));
Theodore Ts'obd2d0212010-10-27 21:30:10 -040088}
89
Dmitry Monakhov28a535f2012-09-29 00:14:55 -040090/* check a range of space and convert unwritten extents to written. */
91static int ext4_end_io(ext4_io_end_t *io)
Theodore Ts'obd2d0212010-10-27 21:30:10 -040092{
93 struct inode *inode = io->inode;
94 loff_t offset = io->offset;
95 ssize_t size = io->size;
Jan Kara6b523df2013-06-04 13:21:11 -040096 handle_t *handle = io->handle;
Theodore Ts'obd2d0212010-10-27 21:30:10 -040097 int ret = 0;
98
99 ext4_debug("ext4_end_io_nolock: io 0x%p from inode %lu,list->next 0x%p,"
100 "list->prev 0x%p\n",
101 io, inode->i_ino, io->list.next, io->list.prev);
102
Jan Kara6b523df2013-06-04 13:21:11 -0400103 io->handle = NULL; /* Following call will use up the handle */
104 ret = ext4_convert_unwritten_extents(handle, inode, offset, size);
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400105 if (ret < 0) {
Theodore Ts'ob82e3842011-10-31 10:56:32 -0400106 ext4_msg(inode->i_sb, KERN_EMERG,
107 "failed to convert unwritten extents to written "
108 "extents -- potential data loss! "
109 "(inode %lu, offset %llu, size %zd, error %d)",
110 inode->i_ino, offset, size, ret);
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400111 }
Jan Kara97a851e2013-06-04 11:58:58 -0400112 ext4_clear_io_unwritten_flag(io);
113 ext4_release_io_end(io);
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400114 return ret;
115}
116
Dmitry Monakhov28a535f2012-09-29 00:14:55 -0400117static void dump_completed_IO(struct inode *inode)
118{
119#ifdef EXT4FS_DEBUG
120 struct list_head *cur, *before, *after;
121 ext4_io_end_t *io, *io0, *io1;
Dmitry Monakhov28a535f2012-09-29 00:14:55 -0400122
123 if (list_empty(&EXT4_I(inode)->i_completed_io_list)) {
124 ext4_debug("inode %lu completed_io list is empty\n",
125 inode->i_ino);
126 return;
127 }
128
129 ext4_debug("Dump inode %lu completed_io list\n", inode->i_ino);
130 list_for_each_entry(io, &EXT4_I(inode)->i_completed_io_list, list) {
131 cur = &io->list;
132 before = cur->prev;
133 io0 = container_of(before, ext4_io_end_t, list);
134 after = cur->next;
135 io1 = container_of(after, ext4_io_end_t, list);
136
137 ext4_debug("io 0x%p from inode %lu,prev 0x%p,next 0x%p\n",
138 io, inode->i_ino, io0, io1);
139 }
140#endif
141}
142
143/* Add the io_end to per-inode completed end_io list. */
Jan Kara97a851e2013-06-04 11:58:58 -0400144static void ext4_add_complete_io(ext4_io_end_t *io_end)
Dmitry Monakhov28a535f2012-09-29 00:14:55 -0400145{
146 struct ext4_inode_info *ei = EXT4_I(io_end->inode);
147 struct workqueue_struct *wq;
148 unsigned long flags;
149
150 BUG_ON(!(io_end->flag & EXT4_IO_END_UNWRITTEN));
151 wq = EXT4_SB(io_end->inode->i_sb)->dio_unwritten_wq;
152
153 spin_lock_irqsave(&ei->i_completed_io_lock, flags);
Jan Kara84c17542013-01-28 09:43:46 -0500154 if (list_empty(&ei->i_completed_io_list))
155 queue_work(wq, &ei->i_unwritten_work);
Dmitry Monakhov28a535f2012-09-29 00:14:55 -0400156 list_add_tail(&io_end->list, &ei->i_completed_io_list);
157 spin_unlock_irqrestore(&ei->i_completed_io_lock, flags);
158}
159
Jan Kara84c17542013-01-28 09:43:46 -0500160static int ext4_do_flush_completed_IO(struct inode *inode)
Dmitry Monakhov28a535f2012-09-29 00:14:55 -0400161{
162 ext4_io_end_t *io;
Jan Kara002bd7f2013-01-28 09:49:15 -0500163 struct list_head unwritten;
Dmitry Monakhov28a535f2012-09-29 00:14:55 -0400164 unsigned long flags;
165 struct ext4_inode_info *ei = EXT4_I(inode);
166 int err, ret = 0;
167
Dmitry Monakhov28a535f2012-09-29 00:14:55 -0400168 spin_lock_irqsave(&ei->i_completed_io_lock, flags);
169 dump_completed_IO(inode);
170 list_replace_init(&ei->i_completed_io_list, &unwritten);
171 spin_unlock_irqrestore(&ei->i_completed_io_lock, flags);
172
173 while (!list_empty(&unwritten)) {
174 io = list_entry(unwritten.next, ext4_io_end_t, list);
175 BUG_ON(!(io->flag & EXT4_IO_END_UNWRITTEN));
176 list_del_init(&io->list);
177
178 err = ext4_end_io(io);
179 if (unlikely(!ret && err))
180 ret = err;
Dmitry Monakhov28a535f2012-09-29 00:14:55 -0400181 }
182 return ret;
183}
184
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400185/*
186 * work on completed aio dio IO, to convert unwritten extents to extents
187 */
Jan Kara84c17542013-01-28 09:43:46 -0500188void ext4_end_io_work(struct work_struct *work)
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400189{
Jan Kara84c17542013-01-28 09:43:46 -0500190 struct ext4_inode_info *ei = container_of(work, struct ext4_inode_info,
191 i_unwritten_work);
192 ext4_do_flush_completed_IO(&ei->vfs_inode);
Dmitry Monakhov28a535f2012-09-29 00:14:55 -0400193}
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400194
Dmitry Monakhovc2785312012-10-05 11:31:55 -0400195int ext4_flush_unwritten_io(struct inode *inode)
Dmitry Monakhov28a535f2012-09-29 00:14:55 -0400196{
Dmitry Monakhovc2785312012-10-05 11:31:55 -0400197 int ret;
198 WARN_ON_ONCE(!mutex_is_locked(&inode->i_mutex) &&
199 !(inode->i_state & I_FREEING));
Jan Kara84c17542013-01-28 09:43:46 -0500200 ret = ext4_do_flush_completed_IO(inode);
Dmitry Monakhovc2785312012-10-05 11:31:55 -0400201 ext4_unwritten_wait(inode);
202 return ret;
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400203}
204
205ext4_io_end_t *ext4_init_io_end(struct inode *inode, gfp_t flags)
206{
Jesper Juhlb17b35e2010-12-19 21:41:55 -0500207 ext4_io_end_t *io = kmem_cache_zalloc(io_end_cachep, flags);
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400208 if (io) {
Theodore Ts'of7ad6d22010-11-08 13:43:33 -0500209 atomic_inc(&EXT4_I(inode)->i_ioend_count);
210 io->inode = inode;
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400211 INIT_LIST_HEAD(&io->list);
Jan Kara97a851e2013-06-04 11:58:58 -0400212 atomic_set(&io->count, 1);
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400213 }
214 return io;
215}
216
Jan Kara97a851e2013-06-04 11:58:58 -0400217void ext4_put_io_end_defer(ext4_io_end_t *io_end)
218{
219 if (atomic_dec_and_test(&io_end->count)) {
220 if (!(io_end->flag & EXT4_IO_END_UNWRITTEN) || !io_end->size) {
221 ext4_release_io_end(io_end);
222 return;
223 }
224 ext4_add_complete_io(io_end);
225 }
226}
227
228int ext4_put_io_end(ext4_io_end_t *io_end)
229{
230 int err = 0;
231
232 if (atomic_dec_and_test(&io_end->count)) {
233 if (io_end->flag & EXT4_IO_END_UNWRITTEN) {
Jan Kara6b523df2013-06-04 13:21:11 -0400234 err = ext4_convert_unwritten_extents(io_end->handle,
235 io_end->inode, io_end->offset,
236 io_end->size);
237 io_end->handle = NULL;
Jan Kara97a851e2013-06-04 11:58:58 -0400238 ext4_clear_io_unwritten_flag(io_end);
239 }
240 ext4_release_io_end(io_end);
241 }
242 return err;
243}
244
245ext4_io_end_t *ext4_get_io_end(ext4_io_end_t *io_end)
246{
247 atomic_inc(&io_end->count);
248 return io_end;
249}
250
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400251/*
252 * Print an buffer I/O error compatible with the fs/buffer.c. This
253 * provides compatibility with dmesg scrapers that look for a specific
254 * buffer I/O error message. We really need a unified error reporting
255 * structure to userspace ala Digital Unix's uerf system, but it's
256 * probably not going to happen in my lifetime, due to LKML politics...
257 */
258static void buffer_io_error(struct buffer_head *bh)
259{
260 char b[BDEVNAME_SIZE];
261 printk(KERN_ERR "Buffer I/O error on device %s, logical block %llu\n",
262 bdevname(bh->b_bdev, b),
263 (unsigned long long)bh->b_blocknr);
264}
265
266static void ext4_end_bio(struct bio *bio, int error)
267{
268 ext4_io_end_t *io_end = bio->bi_private;
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400269 struct inode *inode;
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400270 int i;
Jan Kara0058f962013-04-11 23:48:32 -0400271 int blocksize;
Curt Wohlgemuthd50bdd52011-02-07 12:46:14 -0500272 sector_t bi_sector = bio->bi_sector;
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400273
274 BUG_ON(!io_end);
Jan Kara0058f962013-04-11 23:48:32 -0400275 inode = io_end->inode;
276 blocksize = 1 << inode->i_blkbits;
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400277 bio->bi_private = NULL;
278 bio->bi_end_io = NULL;
279 if (test_bit(BIO_UPTODATE, &bio->bi_flags))
280 error = 0;
Jan Kara0058f962013-04-11 23:48:32 -0400281 for (i = 0; i < bio->bi_vcnt; i++) {
282 struct bio_vec *bvec = &bio->bi_io_vec[i];
283 struct page *page = bvec->bv_page;
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400284 struct buffer_head *bh, *head;
Jan Kara0058f962013-04-11 23:48:32 -0400285 unsigned bio_start = bvec->bv_offset;
286 unsigned bio_end = bio_start + bvec->bv_len;
287 unsigned under_io = 0;
288 unsigned long flags;
289
290 if (!page)
291 continue;
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400292
Curt Wohlgemuth39db00f2011-04-30 13:26:26 -0400293 if (error) {
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400294 SetPageError(page);
Curt Wohlgemuth39db00f2011-04-30 13:26:26 -0400295 set_bit(AS_EIO, &page->mapping->flags);
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400296 }
Jan Kara0058f962013-04-11 23:48:32 -0400297 bh = head = page_buffers(page);
298 /*
299 * We check all buffers in the page under BH_Uptodate_Lock
300 * to avoid races with other end io clearing async_write flags
301 */
302 local_irq_save(flags);
303 bit_spin_lock(BH_Uptodate_Lock, &head->b_state);
304 do {
305 if (bh_offset(bh) < bio_start ||
306 bh_offset(bh) + blocksize > bio_end) {
307 if (buffer_async_write(bh))
308 under_io++;
309 continue;
310 }
311 clear_buffer_async_write(bh);
312 if (error)
313 buffer_io_error(bh);
314 } while ((bh = bh->b_this_page) != head);
315 bit_spin_unlock(BH_Uptodate_Lock, &head->b_state);
316 local_irq_restore(flags);
317 if (!under_io)
318 end_page_writeback(page);
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400319 }
Jan Kara0058f962013-04-11 23:48:32 -0400320 bio_put(bio);
Theodore Ts'of7ad6d22010-11-08 13:43:33 -0500321
322 if (error) {
323 io_end->flag |= EXT4_IO_END_ERROR;
324 ext4_warning(inode->i_sb, "I/O error writing to inode %lu "
325 "(offset %llu size %ld starting block %llu)",
326 inode->i_ino,
327 (unsigned long long) io_end->offset,
328 (long) io_end->size,
329 (unsigned long long)
Curt Wohlgemuthd50bdd52011-02-07 12:46:14 -0500330 bi_sector >> (inode->i_blkbits - 9));
Theodore Ts'of7ad6d22010-11-08 13:43:33 -0500331 }
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400332
Jan Kara97a851e2013-06-04 11:58:58 -0400333 ext4_put_io_end_defer(io_end);
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400334}
335
336void ext4_io_submit(struct ext4_io_submit *io)
337{
338 struct bio *bio = io->io_bio;
339
340 if (bio) {
341 bio_get(io->io_bio);
342 submit_bio(io->io_op, io->io_bio);
343 BUG_ON(bio_flagged(io->io_bio, BIO_EOPNOTSUPP));
344 bio_put(io->io_bio);
345 }
Peter Huewe7dc57612011-02-21 21:01:42 -0500346 io->io_bio = NULL;
Jan Kara97a851e2013-06-04 11:58:58 -0400347}
348
349void ext4_io_submit_init(struct ext4_io_submit *io,
350 struct writeback_control *wbc)
351{
352 io->io_op = (wbc->sync_mode == WB_SYNC_ALL ? WRITE_SYNC : WRITE);
353 io->io_bio = NULL;
Peter Huewe7dc57612011-02-21 21:01:42 -0500354 io->io_end = NULL;
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400355}
356
Jan Kara97a851e2013-06-04 11:58:58 -0400357static int io_submit_init_bio(struct ext4_io_submit *io,
358 struct buffer_head *bh)
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400359{
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400360 int nvecs = bio_get_nr_vecs(bh->b_bdev);
361 struct bio *bio;
362
Theodore Ts'o275d3ba2011-06-29 21:44:45 -0400363 bio = bio_alloc(GFP_NOIO, min(nvecs, BIO_MAX_PAGES));
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400364 bio->bi_sector = bh->b_blocknr * (bh->b_size >> 9);
365 bio->bi_bdev = bh->b_bdev;
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400366 bio->bi_end_io = ext4_end_bio;
Jan Kara97a851e2013-06-04 11:58:58 -0400367 bio->bi_private = ext4_get_io_end(io->io_end);
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400368 io->io_bio = bio;
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400369 io->io_next_block = bh->b_blocknr;
370 return 0;
371}
372
373static int io_submit_add_bh(struct ext4_io_submit *io,
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400374 struct inode *inode,
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400375 struct buffer_head *bh)
376{
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400377 int ret;
378
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400379 if (io->io_bio && bh->b_blocknr != io->io_next_block) {
380submit_and_retry:
381 ext4_io_submit(io);
382 }
383 if (io->io_bio == NULL) {
Jan Kara97a851e2013-06-04 11:58:58 -0400384 ret = io_submit_init_bio(io, bh);
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400385 if (ret)
386 return ret;
387 }
Theodore Ts'oa5499842013-05-11 19:07:42 -0400388 ret = bio_add_page(io->io_bio, bh->b_page, bh->b_size, bh_offset(bh));
389 if (ret != bh->b_size)
390 goto submit_and_retry;
Jan Kara97a851e2013-06-04 11:58:58 -0400391 io->io_next_block++;
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400392 return 0;
393}
394
395int ext4_bio_write_page(struct ext4_io_submit *io,
396 struct page *page,
397 int len,
398 struct writeback_control *wbc)
399{
400 struct inode *inode = page->mapping->host;
Jan Kara0058f962013-04-11 23:48:32 -0400401 unsigned block_start, blocksize;
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400402 struct buffer_head *bh, *head;
403 int ret = 0;
Jan Kara0058f962013-04-11 23:48:32 -0400404 int nr_submitted = 0;
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400405
406 blocksize = 1 << inode->i_blkbits;
407
Curt Wohlgemuthd50bdd52011-02-07 12:46:14 -0500408 BUG_ON(!PageLocked(page));
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400409 BUG_ON(PageWriteback(page));
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400410
Theodore Ts'oa54aa762011-02-27 16:43:24 -0500411 set_page_writeback(page);
412 ClearPageError(page);
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400413
Jan Kara0058f962013-04-11 23:48:32 -0400414 /*
415 * In the first loop we prepare and mark buffers to submit. We have to
416 * mark all buffers in the page before submitting so that
417 * end_page_writeback() cannot be called from ext4_bio_end_io() when IO
418 * on the first buffer finishes and we are still working on submitting
419 * the second buffer.
420 */
421 bh = head = page_buffers(page);
422 do {
423 block_start = bh_offset(bh);
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400424 if (block_start >= len) {
Yongqiang Yang5a0dc732011-12-13 22:29:12 -0500425 /*
426 * Comments copied from block_write_full_page_endio:
427 *
428 * The page straddles i_size. It must be zeroed out on
429 * each and every writepage invocation because it may
430 * be mmapped. "A file is mapped in multiples of the
431 * page size. For a file that is not a multiple of
432 * the page size, the remaining memory is zeroed when
433 * mapped, and writes to that region are not written
434 * out to the file."
435 */
Jan Kara0058f962013-04-11 23:48:32 -0400436 zero_user_segment(page, block_start,
437 block_start + blocksize);
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400438 clear_buffer_dirty(bh);
439 set_buffer_uptodate(bh);
440 continue;
441 }
Jan Kara8a850c32013-01-28 20:53:28 -0500442 if (!buffer_dirty(bh) || buffer_delay(bh) ||
443 !buffer_mapped(bh) || buffer_unwritten(bh)) {
444 /* A hole? We can safely clear the dirty bit */
445 if (!buffer_mapped(bh))
446 clear_buffer_dirty(bh);
447 if (io->io_bio)
448 ext4_io_submit(io);
449 continue;
450 }
Jan Kara0058f962013-04-11 23:48:32 -0400451 if (buffer_new(bh)) {
452 clear_buffer_new(bh);
453 unmap_underlying_metadata(bh->b_bdev, bh->b_blocknr);
454 }
455 set_buffer_async_write(bh);
456 } while ((bh = bh->b_this_page) != head);
457
458 /* Now submit buffers to write */
459 bh = head = page_buffers(page);
460 do {
461 if (!buffer_async_write(bh))
462 continue;
Jan Kara97a851e2013-06-04 11:58:58 -0400463 ret = io_submit_add_bh(io, inode, bh);
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400464 if (ret) {
465 /*
466 * We only get here on ENOMEM. Not much else
467 * we can do but mark the page as dirty, and
468 * better luck next time.
469 */
Jan Kara1ae48a62013-01-28 09:32:54 -0500470 redirty_page_for_writepage(wbc, page);
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400471 break;
472 }
Jan Kara0058f962013-04-11 23:48:32 -0400473 nr_submitted++;
Jan Kara1ae48a62013-01-28 09:32:54 -0500474 clear_buffer_dirty(bh);
Jan Kara0058f962013-04-11 23:48:32 -0400475 } while ((bh = bh->b_this_page) != head);
476
477 /* Error stopped previous loop? Clean up buffers... */
478 if (ret) {
479 do {
480 clear_buffer_async_write(bh);
481 bh = bh->b_this_page;
482 } while (bh != head);
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400483 }
484 unlock_page(page);
Jan Kara0058f962013-04-11 23:48:32 -0400485 /* Nothing submitted - we have to end page writeback */
486 if (!nr_submitted)
487 end_page_writeback(page);
Theodore Ts'obd2d0212010-10-27 21:30:10 -0400488 return ret;
489}