2 * linux/fs/ext4/page-io.c
4 * This contains the new page_io functions for ext4
6 * Written by Theodore Ts'o, 2010.
9 #include <linux/module.h>
11 #include <linux/time.h>
12 #include <linux/jbd2.h>
13 #include <linux/highuid.h>
14 #include <linux/pagemap.h>
15 #include <linux/quotaops.h>
16 #include <linux/string.h>
17 #include <linux/buffer_head.h>
18 #include <linux/writeback.h>
19 #include <linux/pagevec.h>
20 #include <linux/mpage.h>
21 #include <linux/namei.h>
22 #include <linux/uio.h>
23 #include <linux/bio.h>
24 #include <linux/workqueue.h>
25 #include <linux/kernel.h>
26 #include <linux/slab.h>
28 #include "ext4_jbd2.h"
31 #include "ext4_extents.h"
33 static struct kmem_cache *io_page_cachep, *io_end_cachep;
35 int __init ext4_init_pageio(void)
37 io_page_cachep = KMEM_CACHE(ext4_io_page, SLAB_RECLAIM_ACCOUNT);
38 if (io_page_cachep == NULL)
40 io_end_cachep = KMEM_CACHE(ext4_io_end, SLAB_RECLAIM_ACCOUNT);
41 if (io_end_cachep == NULL) {
42 kmem_cache_destroy(io_page_cachep);
48 void ext4_exit_pageio(void)
50 kmem_cache_destroy(io_end_cachep);
51 kmem_cache_destroy(io_page_cachep);
54 void ext4_ioend_wait(struct inode *inode)
56 wait_queue_head_t *wq = ext4_ioend_wq(inode);
58 wait_event(*wq, (atomic_read(&EXT4_I(inode)->i_ioend_count) == 0));
61 static void put_io_page(struct ext4_io_page *io_page)
63 if (atomic_dec_and_test(&io_page->p_count)) {
64 end_page_writeback(io_page->p_page);
65 put_page(io_page->p_page);
66 kmem_cache_free(io_page_cachep, io_page);
70 void ext4_free_io_end(ext4_io_end_t *io)
77 for (i = 0; i < io->num_io_pages; i++)
78 put_io_page(io->pages[i]);
80 if (atomic_dec_and_test(&EXT4_I(io->inode)->i_ioend_count))
81 wake_up_all(ext4_ioend_wq(io->inode));
82 kmem_cache_free(io_end_cachep, io);
86 * check a range of space and convert unwritten extents to written.
88 * Called with inode->i_mutex; we depend on this when we manipulate
89 * io->flag, since we could otherwise race with ext4_flush_completed_IO()
91 int ext4_end_io_nolock(ext4_io_end_t *io)
93 struct inode *inode = io->inode;
94 loff_t offset = io->offset;
95 ssize_t size = io->size;
98 ext4_debug("ext4_end_io_nolock: io 0x%p from inode %lu,list->next 0x%p,"
100 io, inode->i_ino, io->list.next, io->list.prev);
102 ret = ext4_convert_unwritten_extents(inode, offset, size);
104 ext4_msg(inode->i_sb, KERN_EMERG,
105 "failed to convert unwritten extents to written "
106 "extents -- potential data loss! "
107 "(inode %lu, offset %llu, size %zd, error %d)",
108 inode->i_ino, offset, size, ret);
111 /* Wake up anyone waiting on unwritten extent conversion */
112 if (atomic_dec_and_test(&EXT4_I(inode)->i_aiodio_unwritten))
113 wake_up_all(ext4_ioend_wq(io->inode));
114 if (io->flag & EXT4_IO_END_DIRECT)
115 inode_dio_done(inode);
117 aio_complete(io->iocb, io->result, 0);
122 * work on completed aio dio IO, to convert unwritten extents to extents
124 static void ext4_end_io_work(struct work_struct *work)
126 ext4_io_end_t *io = container_of(work, ext4_io_end_t, work);
127 struct inode *inode = io->inode;
128 struct ext4_inode_info *ei = EXT4_I(inode);
131 spin_lock_irqsave(&ei->i_completed_io_lock, flags);
132 if (io->flag & EXT4_IO_END_IN_FSYNC)
134 if (list_empty(&io->list)) {
135 spin_unlock_irqrestore(&ei->i_completed_io_lock, flags);
139 if (!mutex_trylock(&inode->i_mutex)) {
142 was_queued = !!(io->flag & EXT4_IO_END_QUEUED);
143 io->flag |= EXT4_IO_END_QUEUED;
144 spin_unlock_irqrestore(&ei->i_completed_io_lock, flags);
146 * Requeue the work instead of waiting so that the work
147 * items queued after this can be processed.
149 queue_work(EXT4_SB(inode->i_sb)->dio_unwritten_wq, &io->work);
151 * To prevent the ext4-dio-unwritten thread from keeping
152 * requeueing end_io requests and occupying cpu for too long,
153 * yield the cpu if it sees an end_io request that has already
160 list_del_init(&io->list);
161 spin_unlock_irqrestore(&ei->i_completed_io_lock, flags);
162 (void) ext4_end_io_nolock(io);
163 mutex_unlock(&inode->i_mutex);
165 ext4_free_io_end(io);
168 ext4_io_end_t *ext4_init_io_end(struct inode *inode, gfp_t flags)
170 ext4_io_end_t *io = kmem_cache_zalloc(io_end_cachep, flags);
172 atomic_inc(&EXT4_I(inode)->i_ioend_count);
174 INIT_WORK(&io->work, ext4_end_io_work);
175 INIT_LIST_HEAD(&io->list);
181 * Print an buffer I/O error compatible with the fs/buffer.c. This
182 * provides compatibility with dmesg scrapers that look for a specific
183 * buffer I/O error message. We really need a unified error reporting
184 * structure to userspace ala Digital Unix's uerf system, but it's
185 * probably not going to happen in my lifetime, due to LKML politics...
187 static void buffer_io_error(struct buffer_head *bh)
189 char b[BDEVNAME_SIZE];
190 printk(KERN_ERR "Buffer I/O error on device %s, logical block %llu\n",
191 bdevname(bh->b_bdev, b),
192 (unsigned long long)bh->b_blocknr);
195 static void ext4_end_bio(struct bio *bio, int error)
197 ext4_io_end_t *io_end = bio->bi_private;
198 struct workqueue_struct *wq;
202 sector_t bi_sector = bio->bi_sector;
205 bio->bi_private = NULL;
206 bio->bi_end_io = NULL;
207 if (test_bit(BIO_UPTODATE, &bio->bi_flags))
211 for (i = 0; i < io_end->num_io_pages; i++) {
212 struct page *page = io_end->pages[i]->p_page;
213 struct buffer_head *bh, *head;
215 loff_t io_end_offset;
219 set_bit(AS_EIO, &page->mapping->flags);
220 head = page_buffers(page);
223 io_end_offset = io_end->offset + io_end->size;
225 offset = (sector_t) page->index << PAGE_CACHE_SHIFT;
228 if ((offset >= io_end->offset) &&
229 (offset+bh->b_size <= io_end_offset))
232 offset += bh->b_size;
233 bh = bh->b_this_page;
234 } while (bh != head);
237 put_io_page(io_end->pages[i]);
239 io_end->num_io_pages = 0;
240 inode = io_end->inode;
243 io_end->flag |= EXT4_IO_END_ERROR;
244 ext4_warning(inode->i_sb, "I/O error %d writing to inode %lu "
245 "(offset %llu size %ld starting block %llu)",
247 (unsigned long long) io_end->offset,
250 bi_sector >> (inode->i_blkbits - 9));
251 mapping_set_error(inode->i_mapping, error);
254 if (!(io_end->flag & EXT4_IO_END_UNWRITTEN)) {
255 ext4_free_io_end(io_end);
259 /* Add the io_end to per-inode completed io list*/
260 spin_lock_irqsave(&EXT4_I(inode)->i_completed_io_lock, flags);
261 list_add_tail(&io_end->list, &EXT4_I(inode)->i_completed_io_list);
262 spin_unlock_irqrestore(&EXT4_I(inode)->i_completed_io_lock, flags);
264 wq = EXT4_SB(inode->i_sb)->dio_unwritten_wq;
265 /* queue the work to convert unwritten extents to written */
266 queue_work(wq, &io_end->work);
269 void ext4_io_submit(struct ext4_io_submit *io)
271 struct bio *bio = io->io_bio;
275 submit_bio(io->io_op, io->io_bio);
276 BUG_ON(bio_flagged(io->io_bio, BIO_EOPNOTSUPP));
284 static int io_submit_init(struct ext4_io_submit *io,
286 struct writeback_control *wbc,
287 struct buffer_head *bh)
289 ext4_io_end_t *io_end;
290 struct page *page = bh->b_page;
291 int nvecs = bio_get_nr_vecs(bh->b_bdev);
294 io_end = ext4_init_io_end(inode, GFP_NOFS);
297 bio = bio_alloc(GFP_NOIO, min(nvecs, BIO_MAX_PAGES));
298 bio->bi_sector = bh->b_blocknr * (bh->b_size >> 9);
299 bio->bi_bdev = bh->b_bdev;
300 bio->bi_private = io->io_end = io_end;
301 bio->bi_end_io = ext4_end_bio;
303 io_end->offset = (page->index << PAGE_CACHE_SHIFT) + bh_offset(bh);
306 io->io_op = (wbc->sync_mode == WB_SYNC_ALL ? WRITE_SYNC : WRITE);
307 io->io_next_block = bh->b_blocknr;
311 static int io_submit_add_bh(struct ext4_io_submit *io,
312 struct ext4_io_page *io_page,
314 struct writeback_control *wbc,
315 struct buffer_head *bh)
317 ext4_io_end_t *io_end;
320 if (buffer_new(bh)) {
321 clear_buffer_new(bh);
322 unmap_underlying_metadata(bh->b_bdev, bh->b_blocknr);
325 if (!buffer_mapped(bh) || buffer_delay(bh)) {
326 if (!buffer_mapped(bh))
327 clear_buffer_dirty(bh);
333 if (io->io_bio && bh->b_blocknr != io->io_next_block) {
337 if (io->io_bio == NULL) {
338 ret = io_submit_init(io, inode, wbc, bh);
343 if ((io_end->num_io_pages >= MAX_IO_PAGES) &&
344 (io_end->pages[io_end->num_io_pages-1] != io_page))
345 goto submit_and_retry;
346 if (buffer_uninit(bh))
347 ext4_set_io_unwritten_flag(inode, io_end);
348 io->io_end->size += bh->b_size;
350 ret = bio_add_page(io->io_bio, bh->b_page, bh->b_size, bh_offset(bh));
351 if (ret != bh->b_size)
352 goto submit_and_retry;
353 if ((io_end->num_io_pages == 0) ||
354 (io_end->pages[io_end->num_io_pages-1] != io_page)) {
355 io_end->pages[io_end->num_io_pages++] = io_page;
356 atomic_inc(&io_page->p_count);
361 int ext4_bio_write_page(struct ext4_io_submit *io,
364 struct writeback_control *wbc)
366 struct inode *inode = page->mapping->host;
367 unsigned block_start, block_end, blocksize;
368 struct ext4_io_page *io_page;
369 struct buffer_head *bh, *head;
372 blocksize = 1 << inode->i_blkbits;
374 BUG_ON(!PageLocked(page));
375 BUG_ON(PageWriteback(page));
377 io_page = kmem_cache_alloc(io_page_cachep, GFP_NOFS);
379 set_page_dirty(page);
383 io_page->p_page = page;
384 atomic_set(&io_page->p_count, 1);
386 set_page_writeback(page);
387 ClearPageError(page);
390 * Comments copied from block_write_full_page_endio:
392 * The page straddles i_size. It must be zeroed out on each and every
393 * writepage invocation because it may be mmapped. "A file is mapped
394 * in multiples of the page size. For a file that is not a multiple of
395 * the page size, the remaining memory is zeroed when mapped, and
396 * writes to that region are not written out to the file."
398 if (len < PAGE_CACHE_SIZE)
399 zero_user_segment(page, len, PAGE_CACHE_SIZE);
401 for (bh = head = page_buffers(page), block_start = 0;
402 bh != head || !block_start;
403 block_start = block_end, bh = bh->b_this_page) {
405 block_end = block_start + blocksize;
406 if (block_start >= len) {
407 clear_buffer_dirty(bh);
408 set_buffer_uptodate(bh);
411 clear_buffer_dirty(bh);
412 ret = io_submit_add_bh(io, io_page, inode, wbc, bh);
415 * We only get here on ENOMEM. Not much else
416 * we can do but mark the page as dirty, and
417 * better luck next time.
419 set_page_dirty(page);
425 * If the page was truncated before we could do the writeback,
426 * or we had a memory allocation error while trying to write
427 * the first buffer head, we won't have submitted any pages for
428 * I/O. In that case we need to make sure we've cleared the
429 * PageWriteback bit from the page to prevent the system from
432 put_io_page(io_page);