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;
36 #define to_ioend_wq(v) (&ioend_wq[((unsigned long)v) % WQ_HASH_SZ])
37 static wait_queue_head_t ioend_wq[WQ_HASH_SZ];
39 int __init ext4_init_pageio(void)
43 io_page_cachep = KMEM_CACHE(ext4_io_page, SLAB_RECLAIM_ACCOUNT);
44 if (io_page_cachep == NULL)
46 io_end_cachep = KMEM_CACHE(ext4_io_end, SLAB_RECLAIM_ACCOUNT);
47 if (io_page_cachep == NULL) {
48 kmem_cache_destroy(io_page_cachep);
51 for (i = 0; i < WQ_HASH_SZ; i++)
52 init_waitqueue_head(&ioend_wq[i]);
57 void ext4_exit_pageio(void)
59 kmem_cache_destroy(io_end_cachep);
60 kmem_cache_destroy(io_page_cachep);
63 void ext4_ioend_wait(struct inode *inode)
65 wait_queue_head_t *wq = to_ioend_wq(inode);
67 wait_event(*wq, (atomic_read(&EXT4_I(inode)->i_ioend_count) == 0));
70 void ext4_free_io_end(ext4_io_end_t *io)
73 wait_queue_head_t *wq;
78 for (i = 0; i < io->num_io_pages; i++) {
79 if (--io->pages[i]->p_count == 0) {
80 struct page *page = io->pages[i]->p_page;
82 end_page_writeback(page);
84 kmem_cache_free(io_page_cachep, io->pages[i]);
88 wq = to_ioend_wq(io->inode);
89 if (atomic_dec_and_test(&EXT4_I(io->inode)->i_ioend_count) &&
92 kmem_cache_free(io_end_cachep, io);
96 * check a range of space and convert unwritten extents to written.
98 int ext4_end_io_nolock(ext4_io_end_t *io)
100 struct inode *inode = io->inode;
101 loff_t offset = io->offset;
102 ssize_t size = io->size;
105 ext4_debug("ext4_end_io_nolock: io 0x%p from inode %lu,list->next 0x%p,"
107 io, inode->i_ino, io->list.next, io->list.prev);
109 if (list_empty(&io->list))
112 if (!(io->flag & EXT4_IO_END_UNWRITTEN))
115 ret = ext4_convert_unwritten_extents(inode, offset, size);
117 printk(KERN_EMERG "%s: failed to convert unwritten "
118 "extents to written extents, error is %d "
119 "io is still on inode %lu aio dio list\n",
120 __func__, ret, inode->i_ino);
125 aio_complete(io->iocb, io->result, 0);
126 /* clear the DIO AIO unwritten flag */
127 io->flag &= ~EXT4_IO_END_UNWRITTEN;
132 * work on completed aio dio IO, to convert unwritten extents to extents
134 static void ext4_end_io_work(struct work_struct *work)
136 ext4_io_end_t *io = container_of(work, ext4_io_end_t, work);
137 struct inode *inode = io->inode;
138 struct ext4_inode_info *ei = EXT4_I(inode);
142 mutex_lock(&inode->i_mutex);
143 ret = ext4_end_io_nolock(io);
145 mutex_unlock(&inode->i_mutex);
149 spin_lock_irqsave(&ei->i_completed_io_lock, flags);
150 if (!list_empty(&io->list))
151 list_del_init(&io->list);
152 spin_unlock_irqrestore(&ei->i_completed_io_lock, flags);
153 mutex_unlock(&inode->i_mutex);
154 ext4_free_io_end(io);
157 ext4_io_end_t *ext4_init_io_end(struct inode *inode, gfp_t flags)
159 ext4_io_end_t *io = NULL;
161 io = kmem_cache_alloc(io_end_cachep, flags);
163 memset(io, 0, sizeof(*io));
164 atomic_inc(&EXT4_I(inode)->i_ioend_count);
166 INIT_WORK(&io->work, ext4_end_io_work);
167 INIT_LIST_HEAD(&io->list);
173 * Print an buffer I/O error compatible with the fs/buffer.c. This
174 * provides compatibility with dmesg scrapers that look for a specific
175 * buffer I/O error message. We really need a unified error reporting
176 * structure to userspace ala Digital Unix's uerf system, but it's
177 * probably not going to happen in my lifetime, due to LKML politics...
179 static void buffer_io_error(struct buffer_head *bh)
181 char b[BDEVNAME_SIZE];
182 printk(KERN_ERR "Buffer I/O error on device %s, logical block %llu\n",
183 bdevname(bh->b_bdev, b),
184 (unsigned long long)bh->b_blocknr);
187 static void ext4_end_bio(struct bio *bio, int error)
189 ext4_io_end_t *io_end = bio->bi_private;
190 struct workqueue_struct *wq;
196 bio->bi_private = NULL;
197 bio->bi_end_io = NULL;
198 if (test_bit(BIO_UPTODATE, &bio->bi_flags))
202 for (i = 0; i < io_end->num_io_pages; i++) {
203 struct page *page = io_end->pages[i]->p_page;
204 struct buffer_head *bh, *head;
205 int partial_write = 0;
207 head = page_buffers(page);
211 if (head->b_size == PAGE_CACHE_SIZE)
212 clear_buffer_dirty(head);
215 loff_t io_end_offset = io_end->offset + io_end->size;
217 offset = (sector_t) page->index << PAGE_CACHE_SHIFT;
220 if ((offset >= io_end->offset) &&
221 (offset+bh->b_size <= io_end_offset)) {
225 clear_buffer_dirty(bh);
227 if (buffer_delay(bh))
229 else if (!buffer_mapped(bh))
230 clear_buffer_dirty(bh);
231 else if (buffer_dirty(bh))
233 offset += bh->b_size;
234 bh = bh->b_this_page;
235 } while (bh != head);
238 if (--io_end->pages[i]->p_count == 0) {
239 struct page *page = io_end->pages[i]->p_page;
241 end_page_writeback(page);
243 kmem_cache_free(io_page_cachep, io_end->pages[i]);
247 * If this is a partial write which happened to make
248 * all buffers uptodate then we can optimize away a
249 * bogus readpage() for the next read(). Here we
250 * 'discover' whether the page went uptodate as a
251 * result of this (potentially partial) write.
254 SetPageUptodate(page);
256 io_end->num_io_pages = 0;
257 inode = io_end->inode;
260 io_end->flag |= EXT4_IO_END_ERROR;
261 ext4_warning(inode->i_sb, "I/O error writing to inode %lu "
262 "(offset %llu size %ld starting block %llu)",
264 (unsigned long long) io_end->offset,
267 bio->bi_sector >> (inode->i_blkbits - 9));
270 /* Add the io_end to per-inode completed io list*/
271 spin_lock_irqsave(&EXT4_I(inode)->i_completed_io_lock, flags);
272 list_add_tail(&io_end->list, &EXT4_I(inode)->i_completed_io_list);
273 spin_unlock_irqrestore(&EXT4_I(inode)->i_completed_io_lock, flags);
275 wq = EXT4_SB(inode->i_sb)->dio_unwritten_wq;
276 /* queue the work to convert unwritten extents to written */
277 queue_work(wq, &io_end->work);
280 void ext4_io_submit(struct ext4_io_submit *io)
282 struct bio *bio = io->io_bio;
286 submit_bio(io->io_op, io->io_bio);
287 BUG_ON(bio_flagged(io->io_bio, BIO_EOPNOTSUPP));
295 static int io_submit_init(struct ext4_io_submit *io,
297 struct writeback_control *wbc,
298 struct buffer_head *bh)
300 ext4_io_end_t *io_end;
301 struct page *page = bh->b_page;
302 int nvecs = bio_get_nr_vecs(bh->b_bdev);
305 io_end = ext4_init_io_end(inode, GFP_NOFS);
309 bio = bio_alloc(GFP_NOIO, nvecs);
311 } while (bio == NULL);
313 bio->bi_sector = bh->b_blocknr * (bh->b_size >> 9);
314 bio->bi_bdev = bh->b_bdev;
315 bio->bi_private = io->io_end = io_end;
316 bio->bi_end_io = ext4_end_bio;
318 io_end->offset = (page->index << PAGE_CACHE_SHIFT) + bh_offset(bh);
321 io->io_op = (wbc->sync_mode == WB_SYNC_ALL ?
322 WRITE_SYNC_PLUG : WRITE);
323 io->io_next_block = bh->b_blocknr;
327 static int io_submit_add_bh(struct ext4_io_submit *io,
328 struct ext4_io_page *io_page,
330 struct writeback_control *wbc,
331 struct buffer_head *bh)
333 ext4_io_end_t *io_end;
336 if (buffer_new(bh)) {
337 clear_buffer_new(bh);
338 unmap_underlying_metadata(bh->b_bdev, bh->b_blocknr);
341 if (!buffer_mapped(bh) || buffer_delay(bh)) {
342 if (!buffer_mapped(bh))
343 clear_buffer_dirty(bh);
349 if (io->io_bio && bh->b_blocknr != io->io_next_block) {
353 if (io->io_bio == NULL) {
354 ret = io_submit_init(io, inode, wbc, bh);
359 if ((io_end->num_io_pages >= MAX_IO_PAGES) &&
360 (io_end->pages[io_end->num_io_pages-1] != io_page))
361 goto submit_and_retry;
362 if (buffer_uninit(bh))
363 io->io_end->flag |= EXT4_IO_END_UNWRITTEN;
364 io->io_end->size += bh->b_size;
366 ret = bio_add_page(io->io_bio, bh->b_page, bh->b_size, bh_offset(bh));
367 if (ret != bh->b_size)
368 goto submit_and_retry;
369 if ((io_end->num_io_pages == 0) ||
370 (io_end->pages[io_end->num_io_pages-1] != io_page)) {
371 io_end->pages[io_end->num_io_pages++] = io_page;
377 int ext4_bio_write_page(struct ext4_io_submit *io,
380 struct writeback_control *wbc)
382 struct inode *inode = page->mapping->host;
383 unsigned block_start, block_end, blocksize;
384 struct ext4_io_page *io_page;
385 struct buffer_head *bh, *head;
388 blocksize = 1 << inode->i_blkbits;
390 BUG_ON(PageWriteback(page));
391 set_page_writeback(page);
392 ClearPageError(page);
394 io_page = kmem_cache_alloc(io_page_cachep, GFP_NOFS);
396 set_page_dirty(page);
400 io_page->p_page = page;
401 io_page->p_count = 0;
404 for (bh = head = page_buffers(page), block_start = 0;
405 bh != head || !block_start;
406 block_start = block_end, bh = bh->b_this_page) {
407 block_end = block_start + blocksize;
408 if (block_start >= len) {
409 clear_buffer_dirty(bh);
410 set_buffer_uptodate(bh);
413 ret = io_submit_add_bh(io, io_page, inode, wbc, bh);
416 * We only get here on ENOMEM. Not much else
417 * we can do but mark the page as dirty, and
418 * better luck next time.
420 set_page_dirty(page);
426 * If the page was truncated before we could do the writeback,
427 * or we had a memory allocation error while trying to write
428 * the first buffer head, we won't have submitted any pages for
429 * I/O. In that case we need to make sure we've cleared the
430 * PageWriteback bit from the page to prevent the system from
433 if (io_page->p_count == 0) {
435 end_page_writeback(page);
436 kmem_cache_free(io_page_cachep, io_page);