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 void ext4_free_io_end(ext4_io_end_t *io)
67 BUG_ON(!list_empty(&io->list));
68 BUG_ON(io->flag & EXT4_IO_END_UNWRITTEN);
70 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);
75 /* check a range of space and convert unwritten extents to written. */
76 static int ext4_end_io(ext4_io_end_t *io)
78 struct inode *inode = io->inode;
79 loff_t offset = io->offset;
80 ssize_t size = io->size;
83 ext4_debug("ext4_end_io_nolock: io 0x%p from inode %lu,list->next 0x%p,"
85 io, inode->i_ino, io->list.next, io->list.prev);
87 ret = ext4_convert_unwritten_extents(inode, offset, size);
89 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);
95 /* 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);
101 aio_complete(io->iocb, io->result, 0);
105 static void dump_completed_IO(struct inode *inode)
108 struct list_head *cur, *before, *after;
109 ext4_io_end_t *io, *io0, *io1;
111 if (list_empty(&EXT4_I(inode)->i_completed_io_list)) {
112 ext4_debug("inode %lu completed_io list is empty\n",
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) {
121 io0 = container_of(before, ext4_io_end_t, list);
123 io1 = container_of(after, ext4_io_end_t, list);
125 ext4_debug("io 0x%p from inode %lu,prev 0x%p,next 0x%p\n",
126 io, inode->i_ino, io0, io1);
131 /* Add the io_end to per-inode completed end_io list. */
132 void ext4_add_complete_io(ext4_io_end_t *io_end)
134 struct ext4_inode_info *ei = EXT4_I(io_end->inode);
135 struct workqueue_struct *wq;
138 BUG_ON(!(io_end->flag & EXT4_IO_END_UNWRITTEN));
139 wq = EXT4_SB(io_end->inode->i_sb)->dio_unwritten_wq;
141 spin_lock_irqsave(&ei->i_completed_io_lock, flags);
142 if (list_empty(&ei->i_completed_io_list))
143 queue_work(wq, &ei->i_unwritten_work);
144 list_add_tail(&io_end->list, &ei->i_completed_io_list);
145 spin_unlock_irqrestore(&ei->i_completed_io_lock, flags);
148 static int ext4_do_flush_completed_IO(struct inode *inode)
151 struct list_head unwritten;
153 struct ext4_inode_info *ei = EXT4_I(inode);
156 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);
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);
166 err = ext4_end_io(io);
167 if (unlikely(!ret && err))
169 io->flag &= ~EXT4_IO_END_UNWRITTEN;
170 ext4_free_io_end(io);
176 * work on completed aio dio IO, to convert unwritten extents to extents
178 void ext4_end_io_work(struct work_struct *work)
180 struct ext4_inode_info *ei = container_of(work, struct ext4_inode_info,
182 ext4_do_flush_completed_IO(&ei->vfs_inode);
185 int ext4_flush_unwritten_io(struct inode *inode)
188 WARN_ON_ONCE(!mutex_is_locked(&inode->i_mutex) &&
189 !(inode->i_state & I_FREEING));
190 ret = ext4_do_flush_completed_IO(inode);
191 ext4_unwritten_wait(inode);
195 ext4_io_end_t *ext4_init_io_end(struct inode *inode, gfp_t flags)
197 ext4_io_end_t *io = kmem_cache_zalloc(io_end_cachep, flags);
199 atomic_inc(&EXT4_I(inode)->i_ioend_count);
201 INIT_LIST_HEAD(&io->list);
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...
213 static void buffer_io_error(struct buffer_head *bh)
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);
221 static void ext4_end_bio(struct bio *bio, int error)
223 ext4_io_end_t *io_end = bio->bi_private;
227 sector_t bi_sector = bio->bi_sector;
230 inode = io_end->inode;
231 blocksize = 1 << inode->i_blkbits;
232 bio->bi_private = NULL;
233 bio->bi_end_io = NULL;
234 if (test_bit(BIO_UPTODATE, &bio->bi_flags))
236 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;
239 struct buffer_head *bh, *head;
240 unsigned bio_start = bvec->bv_offset;
241 unsigned bio_end = bio_start + bvec->bv_len;
242 unsigned under_io = 0;
250 set_bit(AS_EIO, &page->mapping->flags);
252 bh = head = page_buffers(page);
254 * We check all buffers in the page under BH_Uptodate_Lock
255 * to avoid races with other end io clearing async_write flags
257 local_irq_save(flags);
258 bit_spin_lock(BH_Uptodate_Lock, &head->b_state);
260 if (bh_offset(bh) < bio_start ||
261 bh_offset(bh) + blocksize > bio_end) {
262 if (buffer_async_write(bh))
266 clear_buffer_async_write(bh);
269 } while ((bh = bh->b_this_page) != head);
270 bit_spin_unlock(BH_Uptodate_Lock, &head->b_state);
271 local_irq_restore(flags);
273 end_page_writeback(page);
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)",
282 (unsigned long long) io_end->offset,
285 bi_sector >> (inode->i_blkbits - 9));
288 if (!(io_end->flag & EXT4_IO_END_UNWRITTEN)) {
289 ext4_free_io_end(io_end);
293 ext4_add_complete_io(io_end);
296 void ext4_io_submit(struct ext4_io_submit *io)
298 struct bio *bio = io->io_bio;
302 submit_bio(io->io_op, io->io_bio);
303 BUG_ON(bio_flagged(io->io_bio, BIO_EOPNOTSUPP));
311 static int io_submit_init(struct ext4_io_submit *io,
313 struct writeback_control *wbc,
314 struct buffer_head *bh)
316 ext4_io_end_t *io_end;
317 struct page *page = bh->b_page;
318 int nvecs = bio_get_nr_vecs(bh->b_bdev);
321 io_end = ext4_init_io_end(inode, GFP_NOFS);
324 bio = bio_alloc(GFP_NOIO, min(nvecs, BIO_MAX_PAGES));
325 bio->bi_sector = bh->b_blocknr * (bh->b_size >> 9);
326 bio->bi_bdev = bh->b_bdev;
327 bio->bi_private = io->io_end = io_end;
328 bio->bi_end_io = ext4_end_bio;
330 io_end->offset = (page->index << PAGE_CACHE_SHIFT) + bh_offset(bh);
333 io->io_op = (wbc->sync_mode == WB_SYNC_ALL ? WRITE_SYNC : WRITE);
334 io->io_next_block = bh->b_blocknr;
338 static int io_submit_add_bh(struct ext4_io_submit *io,
340 struct writeback_control *wbc,
341 struct buffer_head *bh)
343 ext4_io_end_t *io_end;
346 if (io->io_bio && bh->b_blocknr != io->io_next_block) {
350 if (io->io_bio == NULL) {
351 ret = io_submit_init(io, inode, wbc, bh);
356 if (test_clear_buffer_uninit(bh))
357 ext4_set_io_unwritten_flag(inode, io_end);
358 io->io_end->size += bh->b_size;
360 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;
366 int ext4_bio_write_page(struct ext4_io_submit *io,
369 struct writeback_control *wbc)
371 struct inode *inode = page->mapping->host;
372 unsigned block_start, blocksize;
373 struct buffer_head *bh, *head;
375 int nr_submitted = 0;
377 blocksize = 1 << inode->i_blkbits;
379 BUG_ON(!PageLocked(page));
380 BUG_ON(PageWriteback(page));
382 set_page_writeback(page);
383 ClearPageError(page);
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
392 bh = head = page_buffers(page);
394 block_start = bh_offset(bh);
395 if (block_start >= len) {
397 * Comments copied from block_write_full_page_endio:
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
407 zero_user_segment(page, block_start,
408 block_start + blocksize);
409 clear_buffer_dirty(bh);
410 set_buffer_uptodate(bh);
413 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);
422 if (buffer_new(bh)) {
423 clear_buffer_new(bh);
424 unmap_underlying_metadata(bh->b_bdev, bh->b_blocknr);
426 set_buffer_async_write(bh);
427 } while ((bh = bh->b_this_page) != head);
429 /* Now submit buffers to write */
430 bh = head = page_buffers(page);
432 if (!buffer_async_write(bh))
434 ret = io_submit_add_bh(io, inode, wbc, bh);
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.
441 redirty_page_for_writepage(wbc, page);
445 clear_buffer_dirty(bh);
446 } while ((bh = bh->b_this_page) != head);
448 /* Error stopped previous loop? Clean up buffers... */
451 clear_buffer_async_write(bh);
452 bh = bh->b_this_page;
453 } while (bh != head);
456 /* Nothing submitted - we have to end page writeback */
458 end_page_writeback(page);