ext4: fix overflow when counting used blocks on 32-bit architectures
[linux-2.6-block.git] / fs / ext4 / page-io.c
CommitLineData
bd2d0210
TT
1/*
2 * linux/fs/ext4/page-io.c
3 *
4 * This contains the new page_io functions for ext4
5 *
6 * Written by Theodore Ts'o, 2010.
7 */
8
bd2d0210
TT
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>
a27bb332 21#include <linux/aio.h>
bd2d0210
TT
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>
1ae48a63 27#include <linux/mm.h>
bd2d0210
TT
28
29#include "ext4_jbd2.h"
30#include "xattr.h"
31#include "acl.h"
bd2d0210 32
0058f965 33static struct kmem_cache *io_end_cachep;
bd2d0210 34
5dabfc78 35int __init ext4_init_pageio(void)
bd2d0210 36{
bd2d0210 37 io_end_cachep = KMEM_CACHE(ext4_io_end, SLAB_RECLAIM_ACCOUNT);
0058f965 38 if (io_end_cachep == NULL)
bd2d0210 39 return -ENOMEM;
bd2d0210
TT
40 return 0;
41}
42
5dabfc78 43void ext4_exit_pageio(void)
bd2d0210
TT
44{
45 kmem_cache_destroy(io_end_cachep);
bd2d0210
TT
46}
47
1ada47d9
TT
48/*
49 * This function is called by ext4_evict_inode() to make sure there is
50 * no more pending I/O completion work left to do.
51 */
52void ext4_ioend_shutdown(struct inode *inode)
f7ad6d2e 53{
e9e3bcec 54 wait_queue_head_t *wq = ext4_ioend_wq(inode);
f7ad6d2e
TT
55
56 wait_event(*wq, (atomic_read(&EXT4_I(inode)->i_ioend_count) == 0));
1ada47d9
TT
57 /*
58 * We need to make sure the work structure is finished being
59 * used before we let the inode get destroyed.
60 */
61 if (work_pending(&EXT4_I(inode)->i_unwritten_work))
62 cancel_work_sync(&EXT4_I(inode)->i_unwritten_work);
f7ad6d2e
TT
63}
64
a549984b 65void ext4_free_io_end(ext4_io_end_t *io)
bd2d0210 66{
a549984b
TT
67 BUG_ON(!io);
68 BUG_ON(!list_empty(&io->list));
69 BUG_ON(io->flag & EXT4_IO_END_UNWRITTEN);
82e54229 70
a549984b
TT
71 if (atomic_dec_and_test(&EXT4_I(io->inode)->i_ioend_count))
72 wake_up_all(ext4_ioend_wq(io->inode));
73 kmem_cache_free(io_end_cachep, io);
bd2d0210
TT
74}
75
28a535f9
DM
76/* check a range of space and convert unwritten extents to written. */
77static int ext4_end_io(ext4_io_end_t *io)
bd2d0210
TT
78{
79 struct inode *inode = io->inode;
80 loff_t offset = io->offset;
81 ssize_t size = io->size;
82 int ret = 0;
83
84 ext4_debug("ext4_end_io_nolock: io 0x%p from inode %lu,list->next 0x%p,"
85 "list->prev 0x%p\n",
86 io, inode->i_ino, io->list.next, io->list.prev);
87
bd2d0210
TT
88 ret = ext4_convert_unwritten_extents(inode, offset, size);
89 if (ret < 0) {
b82e384c
TT
90 ext4_msg(inode->i_sb, KERN_EMERG,
91 "failed to convert unwritten extents to written "
92 "extents -- potential data loss! "
93 "(inode %lu, offset %llu, size %zd, error %d)",
94 inode->i_ino, offset, size, ret);
bd2d0210 95 }
a549984b
TT
96 /* Wake up anyone waiting on unwritten extent conversion */
97 if (atomic_dec_and_test(&EXT4_I(inode)->i_unwritten))
98 wake_up_all(ext4_ioend_wq(inode));
99 if (io->flag & EXT4_IO_END_DIRECT)
100 inode_dio_done(inode);
101 if (io->iocb)
102 aio_complete(io->iocb, io->result, 0);
bd2d0210
TT
103 return ret;
104}
105
28a535f9
DM
106static void dump_completed_IO(struct inode *inode)
107{
108#ifdef EXT4FS_DEBUG
109 struct list_head *cur, *before, *after;
110 ext4_io_end_t *io, *io0, *io1;
28a535f9
DM
111
112 if (list_empty(&EXT4_I(inode)->i_completed_io_list)) {
113 ext4_debug("inode %lu completed_io list is empty\n",
114 inode->i_ino);
115 return;
116 }
117
118 ext4_debug("Dump inode %lu completed_io list\n", inode->i_ino);
119 list_for_each_entry(io, &EXT4_I(inode)->i_completed_io_list, list) {
120 cur = &io->list;
121 before = cur->prev;
122 io0 = container_of(before, ext4_io_end_t, list);
123 after = cur->next;
124 io1 = container_of(after, ext4_io_end_t, list);
125
126 ext4_debug("io 0x%p from inode %lu,prev 0x%p,next 0x%p\n",
127 io, inode->i_ino, io0, io1);
128 }
129#endif
130}
131
132/* Add the io_end to per-inode completed end_io list. */
a549984b 133void ext4_add_complete_io(ext4_io_end_t *io_end)
bd2d0210 134{
28a535f9
DM
135 struct ext4_inode_info *ei = EXT4_I(io_end->inode);
136 struct workqueue_struct *wq;
137 unsigned long flags;
138
139 BUG_ON(!(io_end->flag & EXT4_IO_END_UNWRITTEN));
140 wq = EXT4_SB(io_end->inode->i_sb)->dio_unwritten_wq;
bd2d0210 141
d73d5046 142 spin_lock_irqsave(&ei->i_completed_io_lock, flags);
84c17543
JK
143 if (list_empty(&ei->i_completed_io_list))
144 queue_work(wq, &ei->i_unwritten_work);
28a535f9
DM
145 list_add_tail(&io_end->list, &ei->i_completed_io_list);
146 spin_unlock_irqrestore(&ei->i_completed_io_lock, flags);
147}
d73d5046 148
84c17543 149static int ext4_do_flush_completed_IO(struct inode *inode)
28a535f9
DM
150{
151 ext4_io_end_t *io;
002bd7fa 152 struct list_head unwritten;
28a535f9
DM
153 unsigned long flags;
154 struct ext4_inode_info *ei = EXT4_I(inode);
155 int err, ret = 0;
156
28a535f9
DM
157 spin_lock_irqsave(&ei->i_completed_io_lock, flags);
158 dump_completed_IO(inode);
159 list_replace_init(&ei->i_completed_io_list, &unwritten);
160 spin_unlock_irqrestore(&ei->i_completed_io_lock, flags);
161
162 while (!list_empty(&unwritten)) {
163 io = list_entry(unwritten.next, ext4_io_end_t, list);
164 BUG_ON(!(io->flag & EXT4_IO_END_UNWRITTEN));
165 list_del_init(&io->list);
166
167 err = ext4_end_io(io);
168 if (unlikely(!ret && err))
169 ret = err;
a549984b
TT
170 io->flag &= ~EXT4_IO_END_UNWRITTEN;
171 ext4_free_io_end(io);
28a535f9
DM
172 }
173 return ret;
174}
175
176/*
177 * work on completed aio dio IO, to convert unwritten extents to extents
178 */
84c17543 179void ext4_end_io_work(struct work_struct *work)
28a535f9 180{
84c17543
JK
181 struct ext4_inode_info *ei = container_of(work, struct ext4_inode_info,
182 i_unwritten_work);
183 ext4_do_flush_completed_IO(&ei->vfs_inode);
28a535f9
DM
184}
185
c278531d 186int ext4_flush_unwritten_io(struct inode *inode)
28a535f9 187{
c278531d
DM
188 int ret;
189 WARN_ON_ONCE(!mutex_is_locked(&inode->i_mutex) &&
190 !(inode->i_state & I_FREEING));
84c17543 191 ret = ext4_do_flush_completed_IO(inode);
c278531d
DM
192 ext4_unwritten_wait(inode);
193 return ret;
bd2d0210
TT
194}
195
196ext4_io_end_t *ext4_init_io_end(struct inode *inode, gfp_t flags)
197{
b17b35ec 198 ext4_io_end_t *io = kmem_cache_zalloc(io_end_cachep, flags);
bd2d0210 199 if (io) {
f7ad6d2e
TT
200 atomic_inc(&EXT4_I(inode)->i_ioend_count);
201 io->inode = inode;
bd2d0210
TT
202 INIT_LIST_HEAD(&io->list);
203 }
204 return io;
205}
206
207/*
208 * Print an buffer I/O error compatible with the fs/buffer.c. This
209 * provides compatibility with dmesg scrapers that look for a specific
210 * buffer I/O error message. We really need a unified error reporting
211 * structure to userspace ala Digital Unix's uerf system, but it's
212 * probably not going to happen in my lifetime, due to LKML politics...
213 */
214static void buffer_io_error(struct buffer_head *bh)
215{
216 char b[BDEVNAME_SIZE];
217 printk(KERN_ERR "Buffer I/O error on device %s, logical block %llu\n",
218 bdevname(bh->b_bdev, b),
219 (unsigned long long)bh->b_blocknr);
220}
221
222static void ext4_end_bio(struct bio *bio, int error)
223{
224 ext4_io_end_t *io_end = bio->bi_private;
bd2d0210 225 struct inode *inode;
bd2d0210 226 int i;
0058f965 227 int blocksize;
d50bdd5a 228 sector_t bi_sector = bio->bi_sector;
bd2d0210
TT
229
230 BUG_ON(!io_end);
0058f965
JK
231 inode = io_end->inode;
232 blocksize = 1 << inode->i_blkbits;
bd2d0210
TT
233 bio->bi_private = NULL;
234 bio->bi_end_io = NULL;
235 if (test_bit(BIO_UPTODATE, &bio->bi_flags))
236 error = 0;
0058f965
JK
237 for (i = 0; i < bio->bi_vcnt; i++) {
238 struct bio_vec *bvec = &bio->bi_io_vec[i];
239 struct page *page = bvec->bv_page;
bd2d0210 240 struct buffer_head *bh, *head;
0058f965
JK
241 unsigned bio_start = bvec->bv_offset;
242 unsigned bio_end = bio_start + bvec->bv_len;
243 unsigned under_io = 0;
244 unsigned long flags;
245
246 if (!page)
247 continue;
bd2d0210 248
39db00f1 249 if (error) {
bd2d0210 250 SetPageError(page);
39db00f1 251 set_bit(AS_EIO, &page->mapping->flags);
bd2d0210 252 }
0058f965
JK
253 bh = head = page_buffers(page);
254 /*
255 * We check all buffers in the page under BH_Uptodate_Lock
256 * to avoid races with other end io clearing async_write flags
257 */
258 local_irq_save(flags);
259 bit_spin_lock(BH_Uptodate_Lock, &head->b_state);
260 do {
261 if (bh_offset(bh) < bio_start ||
262 bh_offset(bh) + blocksize > bio_end) {
263 if (buffer_async_write(bh))
264 under_io++;
265 continue;
266 }
267 clear_buffer_async_write(bh);
268 if (error)
269 buffer_io_error(bh);
270 } while ((bh = bh->b_this_page) != head);
271 bit_spin_unlock(BH_Uptodate_Lock, &head->b_state);
272 local_irq_restore(flags);
273 if (!under_io)
274 end_page_writeback(page);
bd2d0210 275 }
0058f965 276 bio_put(bio);
f7ad6d2e
TT
277
278 if (error) {
279 io_end->flag |= EXT4_IO_END_ERROR;
280 ext4_warning(inode->i_sb, "I/O error writing to inode %lu "
281 "(offset %llu size %ld starting block %llu)",
282 inode->i_ino,
283 (unsigned long long) io_end->offset,
284 (long) io_end->size,
285 (unsigned long long)
d50bdd5a 286 bi_sector >> (inode->i_blkbits - 9));
f7ad6d2e 287 }
bd2d0210 288
a549984b
TT
289 if (!(io_end->flag & EXT4_IO_END_UNWRITTEN)) {
290 ext4_free_io_end(io_end);
291 return;
292 }
293
294 ext4_add_complete_io(io_end);
bd2d0210
TT
295}
296
297void ext4_io_submit(struct ext4_io_submit *io)
298{
299 struct bio *bio = io->io_bio;
300
301 if (bio) {
302 bio_get(io->io_bio);
303 submit_bio(io->io_op, io->io_bio);
304 BUG_ON(bio_flagged(io->io_bio, BIO_EOPNOTSUPP));
305 bio_put(io->io_bio);
306 }
7dc57615 307 io->io_bio = NULL;
a549984b 308 io->io_op = 0;
7dc57615 309 io->io_end = NULL;
bd2d0210
TT
310}
311
a549984b
TT
312static int io_submit_init(struct ext4_io_submit *io,
313 struct inode *inode,
314 struct writeback_control *wbc,
315 struct buffer_head *bh)
bd2d0210 316{
a549984b
TT
317 ext4_io_end_t *io_end;
318 struct page *page = bh->b_page;
bd2d0210
TT
319 int nvecs = bio_get_nr_vecs(bh->b_bdev);
320 struct bio *bio;
321
a549984b
TT
322 io_end = ext4_init_io_end(inode, GFP_NOFS);
323 if (!io_end)
324 return -ENOMEM;
275d3ba6 325 bio = bio_alloc(GFP_NOIO, min(nvecs, BIO_MAX_PAGES));
bd2d0210
TT
326 bio->bi_sector = bh->b_blocknr * (bh->b_size >> 9);
327 bio->bi_bdev = bh->b_bdev;
a549984b 328 bio->bi_private = io->io_end = io_end;
bd2d0210 329 bio->bi_end_io = ext4_end_bio;
a549984b
TT
330
331 io_end->offset = (page->index << PAGE_CACHE_SHIFT) + bh_offset(bh);
332
bd2d0210 333 io->io_bio = bio;
a549984b 334 io->io_op = (wbc->sync_mode == WB_SYNC_ALL ? WRITE_SYNC : WRITE);
bd2d0210
TT
335 io->io_next_block = bh->b_blocknr;
336 return 0;
337}
338
339static int io_submit_add_bh(struct ext4_io_submit *io,
bd2d0210 340 struct inode *inode,
a549984b 341 struct writeback_control *wbc,
bd2d0210
TT
342 struct buffer_head *bh)
343{
344 ext4_io_end_t *io_end;
345 int ret;
346
bd2d0210
TT
347 if (io->io_bio && bh->b_blocknr != io->io_next_block) {
348submit_and_retry:
349 ext4_io_submit(io);
350 }
351 if (io->io_bio == NULL) {
a549984b 352 ret = io_submit_init(io, inode, wbc, bh);
bd2d0210
TT
353 if (ret)
354 return ret;
355 }
356 io_end = io->io_end;
7b001d6a 357 if (test_clear_buffer_uninit(bh))
0edeb71d 358 ext4_set_io_unwritten_flag(inode, io_end);
a549984b 359 io->io_end->size += bh->b_size;
bd2d0210 360 io->io_next_block++;
a549984b
TT
361 ret = bio_add_page(io->io_bio, bh->b_page, bh->b_size, bh_offset(bh));
362 if (ret != bh->b_size)
363 goto submit_and_retry;
bd2d0210
TT
364 return 0;
365}
366
367int ext4_bio_write_page(struct ext4_io_submit *io,
368 struct page *page,
369 int len,
370 struct writeback_control *wbc)
371{
372 struct inode *inode = page->mapping->host;
0058f965 373 unsigned block_start, blocksize;
bd2d0210
TT
374 struct buffer_head *bh, *head;
375 int ret = 0;
0058f965 376 int nr_submitted = 0;
bd2d0210
TT
377
378 blocksize = 1 << inode->i_blkbits;
379
d50bdd5a 380 BUG_ON(!PageLocked(page));
bd2d0210 381 BUG_ON(PageWriteback(page));
bd2d0210 382
a54aa761
TT
383 set_page_writeback(page);
384 ClearPageError(page);
bd2d0210 385
0058f965
JK
386 /*
387 * In the first loop we prepare and mark buffers to submit. We have to
388 * mark all buffers in the page before submitting so that
389 * end_page_writeback() cannot be called from ext4_bio_end_io() when IO
390 * on the first buffer finishes and we are still working on submitting
391 * the second buffer.
392 */
393 bh = head = page_buffers(page);
394 do {
395 block_start = bh_offset(bh);
bd2d0210 396 if (block_start >= len) {
5a0dc736
YY
397 /*
398 * Comments copied from block_write_full_page_endio:
399 *
400 * The page straddles i_size. It must be zeroed out on
401 * each and every writepage invocation because it may
402 * be mmapped. "A file is mapped in multiples of the
403 * page size. For a file that is not a multiple of
404 * the page size, the remaining memory is zeroed when
405 * mapped, and writes to that region are not written
406 * out to the file."
407 */
0058f965
JK
408 zero_user_segment(page, block_start,
409 block_start + blocksize);
bd2d0210
TT
410 clear_buffer_dirty(bh);
411 set_buffer_uptodate(bh);
412 continue;
413 }
8a850c3f
JK
414 if (!buffer_dirty(bh) || buffer_delay(bh) ||
415 !buffer_mapped(bh) || buffer_unwritten(bh)) {
416 /* A hole? We can safely clear the dirty bit */
417 if (!buffer_mapped(bh))
418 clear_buffer_dirty(bh);
419 if (io->io_bio)
420 ext4_io_submit(io);
421 continue;
422 }
0058f965
JK
423 if (buffer_new(bh)) {
424 clear_buffer_new(bh);
425 unmap_underlying_metadata(bh->b_bdev, bh->b_blocknr);
426 }
427 set_buffer_async_write(bh);
428 } while ((bh = bh->b_this_page) != head);
429
430 /* Now submit buffers to write */
431 bh = head = page_buffers(page);
432 do {
433 if (!buffer_async_write(bh))
434 continue;
a549984b 435 ret = io_submit_add_bh(io, inode, wbc, bh);
bd2d0210
TT
436 if (ret) {
437 /*
438 * We only get here on ENOMEM. Not much else
439 * we can do but mark the page as dirty, and
440 * better luck next time.
441 */
1ae48a63 442 redirty_page_for_writepage(wbc, page);
bd2d0210
TT
443 break;
444 }
0058f965 445 nr_submitted++;
1ae48a63 446 clear_buffer_dirty(bh);
0058f965
JK
447 } while ((bh = bh->b_this_page) != head);
448
449 /* Error stopped previous loop? Clean up buffers... */
450 if (ret) {
451 do {
452 clear_buffer_async_write(bh);
453 bh = bh->b_this_page;
454 } while (bh != head);
bd2d0210
TT
455 }
456 unlock_page(page);
0058f965
JK
457 /* Nothing submitted - we have to end page writeback */
458 if (!nr_submitted)
459 end_page_writeback(page);
bd2d0210
TT
460 return ret;
461}