Merge branch 'drm-nouveau-fixes-3.9' of git://anongit.freedesktop.org/git/nouveau...
[linux-2.6/libata-dev.git] / fs / ext4 / page-io.c
blob047a6de04a0ac8195d5453de514a8e95526fcce4
1 /*
2 * linux/fs/ext4/page-io.c
4 * This contains the new page_io functions for ext4
6 * Written by Theodore Ts'o, 2010.
7 */
9 #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>
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>
26 #include <linux/mm.h>
28 #include "ext4_jbd2.h"
29 #include "xattr.h"
30 #include "acl.h"
32 static struct kmem_cache *io_page_cachep, *io_end_cachep;
34 int __init ext4_init_pageio(void)
36 io_page_cachep = KMEM_CACHE(ext4_io_page, SLAB_RECLAIM_ACCOUNT);
37 if (io_page_cachep == NULL)
38 return -ENOMEM;
39 io_end_cachep = KMEM_CACHE(ext4_io_end, SLAB_RECLAIM_ACCOUNT);
40 if (io_end_cachep == NULL) {
41 kmem_cache_destroy(io_page_cachep);
42 return -ENOMEM;
44 return 0;
47 void ext4_exit_pageio(void)
49 kmem_cache_destroy(io_end_cachep);
50 kmem_cache_destroy(io_page_cachep);
54 * This function is called by ext4_evict_inode() to make sure there is
55 * no more pending I/O completion work left to do.
57 void ext4_ioend_shutdown(struct inode *inode)
59 wait_queue_head_t *wq = ext4_ioend_wq(inode);
61 wait_event(*wq, (atomic_read(&EXT4_I(inode)->i_ioend_count) == 0));
63 * We need to make sure the work structure is finished being
64 * used before we let the inode get destroyed.
66 if (work_pending(&EXT4_I(inode)->i_unwritten_work))
67 cancel_work_sync(&EXT4_I(inode)->i_unwritten_work);
70 static void put_io_page(struct ext4_io_page *io_page)
72 if (atomic_dec_and_test(&io_page->p_count)) {
73 end_page_writeback(io_page->p_page);
74 put_page(io_page->p_page);
75 kmem_cache_free(io_page_cachep, io_page);
79 void ext4_free_io_end(ext4_io_end_t *io)
81 int i;
83 BUG_ON(!io);
84 BUG_ON(!list_empty(&io->list));
85 BUG_ON(io->flag & EXT4_IO_END_UNWRITTEN);
87 for (i = 0; i < io->num_io_pages; i++)
88 put_io_page(io->pages[i]);
89 io->num_io_pages = 0;
90 if (atomic_dec_and_test(&EXT4_I(io->inode)->i_ioend_count))
91 wake_up_all(ext4_ioend_wq(io->inode));
92 kmem_cache_free(io_end_cachep, io);
95 /* check a range of space and convert unwritten extents to written. */
96 static int ext4_end_io(ext4_io_end_t *io)
98 struct inode *inode = io->inode;
99 loff_t offset = io->offset;
100 ssize_t size = io->size;
101 int ret = 0;
103 ext4_debug("ext4_end_io_nolock: io 0x%p from inode %lu,list->next 0x%p,"
104 "list->prev 0x%p\n",
105 io, inode->i_ino, io->list.next, io->list.prev);
107 ret = ext4_convert_unwritten_extents(inode, offset, size);
108 if (ret < 0) {
109 ext4_msg(inode->i_sb, KERN_EMERG,
110 "failed to convert unwritten extents to written "
111 "extents -- potential data loss! "
112 "(inode %lu, offset %llu, size %zd, error %d)",
113 inode->i_ino, offset, size, ret);
115 /* Wake up anyone waiting on unwritten extent conversion */
116 if (atomic_dec_and_test(&EXT4_I(inode)->i_unwritten))
117 wake_up_all(ext4_ioend_wq(inode));
118 if (io->flag & EXT4_IO_END_DIRECT)
119 inode_dio_done(inode);
120 if (io->iocb)
121 aio_complete(io->iocb, io->result, 0);
122 return ret;
125 static void dump_completed_IO(struct inode *inode)
127 #ifdef EXT4FS_DEBUG
128 struct list_head *cur, *before, *after;
129 ext4_io_end_t *io, *io0, *io1;
131 if (list_empty(&EXT4_I(inode)->i_completed_io_list)) {
132 ext4_debug("inode %lu completed_io list is empty\n",
133 inode->i_ino);
134 return;
137 ext4_debug("Dump inode %lu completed_io list\n", inode->i_ino);
138 list_for_each_entry(io, &EXT4_I(inode)->i_completed_io_list, list) {
139 cur = &io->list;
140 before = cur->prev;
141 io0 = container_of(before, ext4_io_end_t, list);
142 after = cur->next;
143 io1 = container_of(after, ext4_io_end_t, list);
145 ext4_debug("io 0x%p from inode %lu,prev 0x%p,next 0x%p\n",
146 io, inode->i_ino, io0, io1);
148 #endif
151 /* Add the io_end to per-inode completed end_io list. */
152 void ext4_add_complete_io(ext4_io_end_t *io_end)
154 struct ext4_inode_info *ei = EXT4_I(io_end->inode);
155 struct workqueue_struct *wq;
156 unsigned long flags;
158 BUG_ON(!(io_end->flag & EXT4_IO_END_UNWRITTEN));
159 wq = EXT4_SB(io_end->inode->i_sb)->dio_unwritten_wq;
161 spin_lock_irqsave(&ei->i_completed_io_lock, flags);
162 if (list_empty(&ei->i_completed_io_list))
163 queue_work(wq, &ei->i_unwritten_work);
164 list_add_tail(&io_end->list, &ei->i_completed_io_list);
165 spin_unlock_irqrestore(&ei->i_completed_io_lock, flags);
168 static int ext4_do_flush_completed_IO(struct inode *inode)
170 ext4_io_end_t *io;
171 struct list_head unwritten;
172 unsigned long flags;
173 struct ext4_inode_info *ei = EXT4_I(inode);
174 int err, ret = 0;
176 spin_lock_irqsave(&ei->i_completed_io_lock, flags);
177 dump_completed_IO(inode);
178 list_replace_init(&ei->i_completed_io_list, &unwritten);
179 spin_unlock_irqrestore(&ei->i_completed_io_lock, flags);
181 while (!list_empty(&unwritten)) {
182 io = list_entry(unwritten.next, ext4_io_end_t, list);
183 BUG_ON(!(io->flag & EXT4_IO_END_UNWRITTEN));
184 list_del_init(&io->list);
186 err = ext4_end_io(io);
187 if (unlikely(!ret && err))
188 ret = err;
189 io->flag &= ~EXT4_IO_END_UNWRITTEN;
190 ext4_free_io_end(io);
192 return ret;
196 * work on completed aio dio IO, to convert unwritten extents to extents
198 void ext4_end_io_work(struct work_struct *work)
200 struct ext4_inode_info *ei = container_of(work, struct ext4_inode_info,
201 i_unwritten_work);
202 ext4_do_flush_completed_IO(&ei->vfs_inode);
205 int ext4_flush_unwritten_io(struct inode *inode)
207 int ret;
208 WARN_ON_ONCE(!mutex_is_locked(&inode->i_mutex) &&
209 !(inode->i_state & I_FREEING));
210 ret = ext4_do_flush_completed_IO(inode);
211 ext4_unwritten_wait(inode);
212 return ret;
215 ext4_io_end_t *ext4_init_io_end(struct inode *inode, gfp_t flags)
217 ext4_io_end_t *io = kmem_cache_zalloc(io_end_cachep, flags);
218 if (io) {
219 atomic_inc(&EXT4_I(inode)->i_ioend_count);
220 io->inode = inode;
221 INIT_LIST_HEAD(&io->list);
223 return io;
227 * Print an buffer I/O error compatible with the fs/buffer.c. This
228 * provides compatibility with dmesg scrapers that look for a specific
229 * buffer I/O error message. We really need a unified error reporting
230 * structure to userspace ala Digital Unix's uerf system, but it's
231 * probably not going to happen in my lifetime, due to LKML politics...
233 static void buffer_io_error(struct buffer_head *bh)
235 char b[BDEVNAME_SIZE];
236 printk(KERN_ERR "Buffer I/O error on device %s, logical block %llu\n",
237 bdevname(bh->b_bdev, b),
238 (unsigned long long)bh->b_blocknr);
241 static void ext4_end_bio(struct bio *bio, int error)
243 ext4_io_end_t *io_end = bio->bi_private;
244 struct inode *inode;
245 int i;
246 sector_t bi_sector = bio->bi_sector;
248 BUG_ON(!io_end);
249 bio->bi_private = NULL;
250 bio->bi_end_io = NULL;
251 if (test_bit(BIO_UPTODATE, &bio->bi_flags))
252 error = 0;
253 bio_put(bio);
255 for (i = 0; i < io_end->num_io_pages; i++) {
256 struct page *page = io_end->pages[i]->p_page;
257 struct buffer_head *bh, *head;
258 loff_t offset;
259 loff_t io_end_offset;
261 if (error) {
262 SetPageError(page);
263 set_bit(AS_EIO, &page->mapping->flags);
264 head = page_buffers(page);
265 BUG_ON(!head);
267 io_end_offset = io_end->offset + io_end->size;
269 offset = (sector_t) page->index << PAGE_CACHE_SHIFT;
270 bh = head;
271 do {
272 if ((offset >= io_end->offset) &&
273 (offset+bh->b_size <= io_end_offset))
274 buffer_io_error(bh);
276 offset += bh->b_size;
277 bh = bh->b_this_page;
278 } while (bh != head);
281 put_io_page(io_end->pages[i]);
283 io_end->num_io_pages = 0;
284 inode = io_end->inode;
286 if (error) {
287 io_end->flag |= EXT4_IO_END_ERROR;
288 ext4_warning(inode->i_sb, "I/O error writing to inode %lu "
289 "(offset %llu size %ld starting block %llu)",
290 inode->i_ino,
291 (unsigned long long) io_end->offset,
292 (long) io_end->size,
293 (unsigned long long)
294 bi_sector >> (inode->i_blkbits - 9));
297 if (!(io_end->flag & EXT4_IO_END_UNWRITTEN)) {
298 ext4_free_io_end(io_end);
299 return;
302 ext4_add_complete_io(io_end);
305 void ext4_io_submit(struct ext4_io_submit *io)
307 struct bio *bio = io->io_bio;
309 if (bio) {
310 bio_get(io->io_bio);
311 submit_bio(io->io_op, io->io_bio);
312 BUG_ON(bio_flagged(io->io_bio, BIO_EOPNOTSUPP));
313 bio_put(io->io_bio);
315 io->io_bio = NULL;
316 io->io_op = 0;
317 io->io_end = NULL;
320 static int io_submit_init(struct ext4_io_submit *io,
321 struct inode *inode,
322 struct writeback_control *wbc,
323 struct buffer_head *bh)
325 ext4_io_end_t *io_end;
326 struct page *page = bh->b_page;
327 int nvecs = bio_get_nr_vecs(bh->b_bdev);
328 struct bio *bio;
330 io_end = ext4_init_io_end(inode, GFP_NOFS);
331 if (!io_end)
332 return -ENOMEM;
333 bio = bio_alloc(GFP_NOIO, min(nvecs, BIO_MAX_PAGES));
334 bio->bi_sector = bh->b_blocknr * (bh->b_size >> 9);
335 bio->bi_bdev = bh->b_bdev;
336 bio->bi_private = io->io_end = io_end;
337 bio->bi_end_io = ext4_end_bio;
339 io_end->offset = (page->index << PAGE_CACHE_SHIFT) + bh_offset(bh);
341 io->io_bio = bio;
342 io->io_op = (wbc->sync_mode == WB_SYNC_ALL ? WRITE_SYNC : WRITE);
343 io->io_next_block = bh->b_blocknr;
344 return 0;
347 static int io_submit_add_bh(struct ext4_io_submit *io,
348 struct ext4_io_page *io_page,
349 struct inode *inode,
350 struct writeback_control *wbc,
351 struct buffer_head *bh)
353 ext4_io_end_t *io_end;
354 int ret;
356 if (buffer_new(bh)) {
357 clear_buffer_new(bh);
358 unmap_underlying_metadata(bh->b_bdev, bh->b_blocknr);
361 if (io->io_bio && bh->b_blocknr != io->io_next_block) {
362 submit_and_retry:
363 ext4_io_submit(io);
365 if (io->io_bio == NULL) {
366 ret = io_submit_init(io, inode, wbc, bh);
367 if (ret)
368 return ret;
370 io_end = io->io_end;
371 if ((io_end->num_io_pages >= MAX_IO_PAGES) &&
372 (io_end->pages[io_end->num_io_pages-1] != io_page))
373 goto submit_and_retry;
374 if (buffer_uninit(bh))
375 ext4_set_io_unwritten_flag(inode, io_end);
376 io->io_end->size += bh->b_size;
377 io->io_next_block++;
378 ret = bio_add_page(io->io_bio, bh->b_page, bh->b_size, bh_offset(bh));
379 if (ret != bh->b_size)
380 goto submit_and_retry;
381 if ((io_end->num_io_pages == 0) ||
382 (io_end->pages[io_end->num_io_pages-1] != io_page)) {
383 io_end->pages[io_end->num_io_pages++] = io_page;
384 atomic_inc(&io_page->p_count);
386 return 0;
389 int ext4_bio_write_page(struct ext4_io_submit *io,
390 struct page *page,
391 int len,
392 struct writeback_control *wbc)
394 struct inode *inode = page->mapping->host;
395 unsigned block_start, block_end, blocksize;
396 struct ext4_io_page *io_page;
397 struct buffer_head *bh, *head;
398 int ret = 0;
400 blocksize = 1 << inode->i_blkbits;
402 BUG_ON(!PageLocked(page));
403 BUG_ON(PageWriteback(page));
405 io_page = kmem_cache_alloc(io_page_cachep, GFP_NOFS);
406 if (!io_page) {
407 redirty_page_for_writepage(wbc, page);
408 unlock_page(page);
409 return -ENOMEM;
411 io_page->p_page = page;
412 atomic_set(&io_page->p_count, 1);
413 get_page(page);
414 set_page_writeback(page);
415 ClearPageError(page);
417 for (bh = head = page_buffers(page), block_start = 0;
418 bh != head || !block_start;
419 block_start = block_end, bh = bh->b_this_page) {
421 block_end = block_start + blocksize;
422 if (block_start >= len) {
424 * Comments copied from block_write_full_page_endio:
426 * The page straddles i_size. It must be zeroed out on
427 * each and every writepage invocation because it may
428 * be mmapped. "A file is mapped in multiples of the
429 * page size. For a file that is not a multiple of
430 * the page size, the remaining memory is zeroed when
431 * mapped, and writes to that region are not written
432 * out to the file."
434 zero_user_segment(page, block_start, block_end);
435 clear_buffer_dirty(bh);
436 set_buffer_uptodate(bh);
437 continue;
439 if (!buffer_dirty(bh) || buffer_delay(bh) ||
440 !buffer_mapped(bh) || buffer_unwritten(bh)) {
441 /* A hole? We can safely clear the dirty bit */
442 if (!buffer_mapped(bh))
443 clear_buffer_dirty(bh);
444 if (io->io_bio)
445 ext4_io_submit(io);
446 continue;
448 ret = io_submit_add_bh(io, io_page, inode, wbc, bh);
449 if (ret) {
451 * We only get here on ENOMEM. Not much else
452 * we can do but mark the page as dirty, and
453 * better luck next time.
455 redirty_page_for_writepage(wbc, page);
456 break;
458 clear_buffer_dirty(bh);
460 unlock_page(page);
462 * If the page was truncated before we could do the writeback,
463 * or we had a memory allocation error while trying to write
464 * the first buffer head, we won't have submitted any pages for
465 * I/O. In that case we need to make sure we've cleared the
466 * PageWriteback bit from the page to prevent the system from
467 * wedging later on.
469 put_io_page(io_page);
470 return ret;