2 * linux/fs/ext4/page-io.c
4 * This contains the new page_io functions for ext4
6 * Written by Theodore Ts'o, 2010.
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>
28 #include "ext4_jbd2.h"
32 static struct kmem_cache *io_end_cachep;
34 int __init ext4_init_pageio(void)
36 io_end_cachep = KMEM_CACHE(ext4_io_end, SLAB_RECLAIM_ACCOUNT);
37 if (io_end_cachep == NULL)
42 void ext4_exit_pageio(void)
44 kmem_cache_destroy(io_end_cachep);
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.
51 void ext4_ioend_shutdown(struct inode *inode)
53 wait_queue_head_t *wq = ext4_ioend_wq(inode);
55 wait_event(*wq, (atomic_read(&EXT4_I(inode)->i_ioend_count) == 0));
57 * We need to make sure the work structure is finished being
58 * used before we let the inode get destroyed.
60 if (work_pending(&EXT4_I(inode)->i_unwritten_work))
61 cancel_work_sync(&EXT4_I(inode)->i_unwritten_work);
64 static void ext4_release_io_end(ext4_io_end_t *io_end)
66 BUG_ON(!list_empty(&io_end->list));
67 BUG_ON(io_end->flag & EXT4_IO_END_UNWRITTEN);
69 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);
74 aio_complete(io_end->iocb, io_end->result, 0);
75 kmem_cache_free(io_end_cachep, io_end);
78 static void ext4_clear_io_unwritten_flag(ext4_io_end_t *io_end)
80 struct inode *inode = io_end->inode;
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));
88 /* check a range of space and convert unwritten extents to written. */
89 static int ext4_end_io(ext4_io_end_t *io)
91 struct inode *inode = io->inode;
92 loff_t offset = io->offset;
93 ssize_t size = io->size;
96 ext4_debug("ext4_end_io_nolock: io 0x%p from inode %lu,list->next 0x%p,"
98 io, inode->i_ino, io->list.next, io->list.prev);
100 ret = ext4_convert_unwritten_extents(inode, offset, size);
102 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);
108 ext4_clear_io_unwritten_flag(io);
109 ext4_release_io_end(io);
113 static void dump_completed_IO(struct inode *inode)
116 struct list_head *cur, *before, *after;
117 ext4_io_end_t *io, *io0, *io1;
119 if (list_empty(&EXT4_I(inode)->i_completed_io_list)) {
120 ext4_debug("inode %lu completed_io list is empty\n",
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) {
129 io0 = container_of(before, ext4_io_end_t, list);
131 io1 = container_of(after, ext4_io_end_t, list);
133 ext4_debug("io 0x%p from inode %lu,prev 0x%p,next 0x%p\n",
134 io, inode->i_ino, io0, io1);
139 /* Add the io_end to per-inode completed end_io list. */
140 static void ext4_add_complete_io(ext4_io_end_t *io_end)
142 struct ext4_inode_info *ei = EXT4_I(io_end->inode);
143 struct workqueue_struct *wq;
146 BUG_ON(!(io_end->flag & EXT4_IO_END_UNWRITTEN));
147 wq = EXT4_SB(io_end->inode->i_sb)->dio_unwritten_wq;
149 spin_lock_irqsave(&ei->i_completed_io_lock, flags);
150 if (list_empty(&ei->i_completed_io_list))
151 queue_work(wq, &ei->i_unwritten_work);
152 list_add_tail(&io_end->list, &ei->i_completed_io_list);
153 spin_unlock_irqrestore(&ei->i_completed_io_lock, flags);
156 static int ext4_do_flush_completed_IO(struct inode *inode)
159 struct list_head unwritten;
161 struct ext4_inode_info *ei = EXT4_I(inode);
164 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);
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);
174 err = ext4_end_io(io);
175 if (unlikely(!ret && err))
182 * work on completed aio dio IO, to convert unwritten extents to extents
184 void ext4_end_io_work(struct work_struct *work)
186 struct ext4_inode_info *ei = container_of(work, struct ext4_inode_info,
188 ext4_do_flush_completed_IO(&ei->vfs_inode);
191 int ext4_flush_unwritten_io(struct inode *inode)
194 WARN_ON_ONCE(!mutex_is_locked(&inode->i_mutex) &&
195 !(inode->i_state & I_FREEING));
196 ret = ext4_do_flush_completed_IO(inode);
197 ext4_unwritten_wait(inode);
201 ext4_io_end_t *ext4_init_io_end(struct inode *inode, gfp_t flags)
203 ext4_io_end_t *io = kmem_cache_zalloc(io_end_cachep, flags);
205 atomic_inc(&EXT4_I(inode)->i_ioend_count);
207 INIT_LIST_HEAD(&io->list);
208 atomic_set(&io->count, 1);
213 void ext4_put_io_end_defer(ext4_io_end_t *io_end)
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);
220 ext4_add_complete_io(io_end);
224 int ext4_put_io_end(ext4_io_end_t *io_end)
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);
234 ext4_release_io_end(io_end);
239 ext4_io_end_t *ext4_get_io_end(ext4_io_end_t *io_end)
241 atomic_inc(&io_end->count);
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...
252 static void buffer_io_error(struct buffer_head *bh)
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);
260 static void ext4_end_bio(struct bio *bio, int error)
262 ext4_io_end_t *io_end = bio->bi_private;
266 sector_t bi_sector = bio->bi_sector;
269 inode = io_end->inode;
270 blocksize = 1 << inode->i_blkbits;
271 bio->bi_private = NULL;
272 bio->bi_end_io = NULL;
273 if (test_bit(BIO_UPTODATE, &bio->bi_flags))
275 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;
278 struct buffer_head *bh, *head;
279 unsigned bio_start = bvec->bv_offset;
280 unsigned bio_end = bio_start + bvec->bv_len;
281 unsigned under_io = 0;
289 set_bit(AS_EIO, &page->mapping->flags);
291 bh = head = page_buffers(page);
293 * We check all buffers in the page under BH_Uptodate_Lock
294 * to avoid races with other end io clearing async_write flags
296 local_irq_save(flags);
297 bit_spin_lock(BH_Uptodate_Lock, &head->b_state);
299 if (bh_offset(bh) < bio_start ||
300 bh_offset(bh) + blocksize > bio_end) {
301 if (buffer_async_write(bh))
305 clear_buffer_async_write(bh);
308 } while ((bh = bh->b_this_page) != head);
309 bit_spin_unlock(BH_Uptodate_Lock, &head->b_state);
310 local_irq_restore(flags);
312 end_page_writeback(page);
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)",
321 (unsigned long long) io_end->offset,
324 bi_sector >> (inode->i_blkbits - 9));
327 ext4_put_io_end_defer(io_end);
330 void ext4_io_submit(struct ext4_io_submit *io)
332 struct bio *bio = io->io_bio;
336 submit_bio(io->io_op, io->io_bio);
337 BUG_ON(bio_flagged(io->io_bio, BIO_EOPNOTSUPP));
343 void ext4_io_submit_init(struct ext4_io_submit *io,
344 struct writeback_control *wbc)
346 io->io_op = (wbc->sync_mode == WB_SYNC_ALL ? WRITE_SYNC : WRITE);
351 static int io_submit_init_bio(struct ext4_io_submit *io,
352 struct buffer_head *bh)
354 int nvecs = bio_get_nr_vecs(bh->b_bdev);
357 bio = bio_alloc(GFP_NOIO, min(nvecs, BIO_MAX_PAGES));
358 bio->bi_sector = bh->b_blocknr * (bh->b_size >> 9);
359 bio->bi_bdev = bh->b_bdev;
360 bio->bi_end_io = ext4_end_bio;
361 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)
366 io->io_next_block = bh->b_blocknr;
370 static int io_submit_add_bh(struct ext4_io_submit *io,
372 struct buffer_head *bh)
374 ext4_io_end_t *io_end;
377 if (io->io_bio && bh->b_blocknr != io->io_next_block) {
381 if (io->io_bio == NULL) {
382 ret = io_submit_init_bio(io, bh);
386 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;
390 if (test_clear_buffer_uninit(bh))
391 ext4_set_io_unwritten_flag(inode, io_end);
392 io_end->size += bh->b_size;
397 int ext4_bio_write_page(struct ext4_io_submit *io,
400 struct writeback_control *wbc)
402 struct inode *inode = page->mapping->host;
403 unsigned block_start, blocksize;
404 struct buffer_head *bh, *head;
406 int nr_submitted = 0;
408 blocksize = 1 << inode->i_blkbits;
410 BUG_ON(!PageLocked(page));
411 BUG_ON(PageWriteback(page));
413 set_page_writeback(page);
414 ClearPageError(page);
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
423 bh = head = page_buffers(page);
425 block_start = bh_offset(bh);
426 if (block_start >= len) {
428 * Comments copied from block_write_full_page_endio:
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
438 zero_user_segment(page, block_start,
439 block_start + blocksize);
440 clear_buffer_dirty(bh);
441 set_buffer_uptodate(bh);
444 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);
453 if (buffer_new(bh)) {
454 clear_buffer_new(bh);
455 unmap_underlying_metadata(bh->b_bdev, bh->b_blocknr);
457 set_buffer_async_write(bh);
458 } while ((bh = bh->b_this_page) != head);
460 /* Now submit buffers to write */
461 bh = head = page_buffers(page);
463 if (!buffer_async_write(bh))
465 ret = io_submit_add_bh(io, inode, bh);
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.
472 redirty_page_for_writepage(wbc, page);
476 clear_buffer_dirty(bh);
477 } while ((bh = bh->b_this_page) != head);
479 /* Error stopped previous loop? Clean up buffers... */
482 clear_buffer_async_write(bh);
483 bh = bh->b_this_page;
484 } while (bh != head);
487 /* Nothing submitted - we have to end page writeback */
489 end_page_writeback(page);