Commit | Line | Data |
---|---|---|
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> | |
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> | |
1ae48a63 | 26 | #include <linux/mm.h> |
bd2d0210 TT |
27 | |
28 | #include "ext4_jbd2.h" | |
29 | #include "xattr.h" | |
30 | #include "acl.h" | |
bd2d0210 TT |
31 | |
32 | static struct kmem_cache *io_page_cachep, *io_end_cachep; | |
33 | ||
5dabfc78 | 34 | int __init ext4_init_pageio(void) |
bd2d0210 TT |
35 | { |
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); | |
13195184 | 40 | if (io_end_cachep == NULL) { |
bd2d0210 TT |
41 | kmem_cache_destroy(io_page_cachep); |
42 | return -ENOMEM; | |
43 | } | |
bd2d0210 TT |
44 | return 0; |
45 | } | |
46 | ||
5dabfc78 | 47 | void ext4_exit_pageio(void) |
bd2d0210 TT |
48 | { |
49 | kmem_cache_destroy(io_end_cachep); | |
50 | kmem_cache_destroy(io_page_cachep); | |
51 | } | |
52 | ||
1ada47d9 TT |
53 | /* |
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. | |
56 | */ | |
57 | void ext4_ioend_shutdown(struct inode *inode) | |
f7ad6d2e | 58 | { |
e9e3bcec | 59 | wait_queue_head_t *wq = ext4_ioend_wq(inode); |
f7ad6d2e TT |
60 | |
61 | wait_event(*wq, (atomic_read(&EXT4_I(inode)->i_ioend_count) == 0)); | |
1ada47d9 TT |
62 | /* |
63 | * We need to make sure the work structure is finished being | |
64 | * used before we let the inode get destroyed. | |
65 | */ | |
66 | if (work_pending(&EXT4_I(inode)->i_unwritten_work)) | |
67 | cancel_work_sync(&EXT4_I(inode)->i_unwritten_work); | |
f7ad6d2e TT |
68 | } |
69 | ||
83668e71 TT |
70 | static void put_io_page(struct ext4_io_page *io_page) |
71 | { | |
72 | if (atomic_dec_and_test(&io_page->p_count)) { | |
6268b325 | 73 | end_page_writeback(io_page->p_page); |
83668e71 TT |
74 | put_page(io_page->p_page); |
75 | kmem_cache_free(io_page_cachep, io_page); | |
76 | } | |
77 | } | |
78 | ||
bd2d0210 TT |
79 | void ext4_free_io_end(ext4_io_end_t *io) |
80 | { | |
81 | int i; | |
82 | ||
83 | BUG_ON(!io); | |
28a535f9 | 84 | BUG_ON(!list_empty(&io->list)); |
82e54229 DM |
85 | BUG_ON(io->flag & EXT4_IO_END_UNWRITTEN); |
86 | ||
83668e71 TT |
87 | for (i = 0; i < io->num_io_pages; i++) |
88 | put_io_page(io->pages[i]); | |
bd2d0210 | 89 | io->num_io_pages = 0; |
4e298021 TT |
90 | if (atomic_dec_and_test(&EXT4_I(io->inode)->i_ioend_count)) |
91 | wake_up_all(ext4_ioend_wq(io->inode)); | |
bd2d0210 TT |
92 | kmem_cache_free(io_end_cachep, io); |
93 | } | |
94 | ||
28a535f9 DM |
95 | /* check a range of space and convert unwritten extents to written. */ |
96 | static int ext4_end_io(ext4_io_end_t *io) | |
bd2d0210 TT |
97 | { |
98 | struct inode *inode = io->inode; | |
99 | loff_t offset = io->offset; | |
100 | ssize_t size = io->size; | |
101 | int ret = 0; | |
102 | ||
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); | |
106 | ||
bd2d0210 TT |
107 | ret = ext4_convert_unwritten_extents(inode, offset, size); |
108 | if (ret < 0) { | |
b82e384c TT |
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); | |
bd2d0210 | 114 | } |
b82e384c | 115 | /* Wake up anyone waiting on unwritten extent conversion */ |
e27f41e1 | 116 | if (atomic_dec_and_test(&EXT4_I(inode)->i_unwritten)) |
8d8c1825 | 117 | wake_up_all(ext4_ioend_wq(inode)); |
091e26df JK |
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); | |
bd2d0210 TT |
122 | return ret; |
123 | } | |
124 | ||
28a535f9 DM |
125 | static void dump_completed_IO(struct inode *inode) |
126 | { | |
127 | #ifdef EXT4FS_DEBUG | |
128 | struct list_head *cur, *before, *after; | |
129 | ext4_io_end_t *io, *io0, *io1; | |
28a535f9 DM |
130 | |
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; | |
135 | } | |
136 | ||
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); | |
144 | ||
145 | ext4_debug("io 0x%p from inode %lu,prev 0x%p,next 0x%p\n", | |
146 | io, inode->i_ino, io0, io1); | |
147 | } | |
148 | #endif | |
149 | } | |
150 | ||
151 | /* Add the io_end to per-inode completed end_io list. */ | |
152 | void ext4_add_complete_io(ext4_io_end_t *io_end) | |
bd2d0210 | 153 | { |
28a535f9 DM |
154 | struct ext4_inode_info *ei = EXT4_I(io_end->inode); |
155 | struct workqueue_struct *wq; | |
156 | unsigned long flags; | |
157 | ||
158 | BUG_ON(!(io_end->flag & EXT4_IO_END_UNWRITTEN)); | |
159 | wq = EXT4_SB(io_end->inode->i_sb)->dio_unwritten_wq; | |
bd2d0210 | 160 | |
d73d5046 | 161 | spin_lock_irqsave(&ei->i_completed_io_lock, flags); |
84c17543 JK |
162 | if (list_empty(&ei->i_completed_io_list)) |
163 | queue_work(wq, &ei->i_unwritten_work); | |
28a535f9 DM |
164 | list_add_tail(&io_end->list, &ei->i_completed_io_list); |
165 | spin_unlock_irqrestore(&ei->i_completed_io_lock, flags); | |
166 | } | |
d73d5046 | 167 | |
84c17543 | 168 | static int ext4_do_flush_completed_IO(struct inode *inode) |
28a535f9 DM |
169 | { |
170 | ext4_io_end_t *io; | |
002bd7fa | 171 | struct list_head unwritten; |
28a535f9 DM |
172 | unsigned long flags; |
173 | struct ext4_inode_info *ei = EXT4_I(inode); | |
174 | int err, ret = 0; | |
175 | ||
28a535f9 DM |
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); | |
180 | ||
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); | |
185 | ||
186 | err = ext4_end_io(io); | |
187 | if (unlikely(!ret && err)) | |
188 | ret = err; | |
28a535f9 | 189 | io->flag &= ~EXT4_IO_END_UNWRITTEN; |
28a535f9 DM |
190 | ext4_free_io_end(io); |
191 | } | |
192 | return ret; | |
193 | } | |
194 | ||
195 | /* | |
196 | * work on completed aio dio IO, to convert unwritten extents to extents | |
197 | */ | |
84c17543 | 198 | void ext4_end_io_work(struct work_struct *work) |
28a535f9 | 199 | { |
84c17543 JK |
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); | |
28a535f9 DM |
203 | } |
204 | ||
c278531d | 205 | int ext4_flush_unwritten_io(struct inode *inode) |
28a535f9 | 206 | { |
c278531d DM |
207 | int ret; |
208 | WARN_ON_ONCE(!mutex_is_locked(&inode->i_mutex) && | |
209 | !(inode->i_state & I_FREEING)); | |
84c17543 | 210 | ret = ext4_do_flush_completed_IO(inode); |
c278531d DM |
211 | ext4_unwritten_wait(inode); |
212 | return ret; | |
bd2d0210 TT |
213 | } |
214 | ||
215 | ext4_io_end_t *ext4_init_io_end(struct inode *inode, gfp_t flags) | |
216 | { | |
b17b35ec | 217 | ext4_io_end_t *io = kmem_cache_zalloc(io_end_cachep, flags); |
bd2d0210 | 218 | if (io) { |
f7ad6d2e TT |
219 | atomic_inc(&EXT4_I(inode)->i_ioend_count); |
220 | io->inode = inode; | |
bd2d0210 TT |
221 | INIT_LIST_HEAD(&io->list); |
222 | } | |
223 | return io; | |
224 | } | |
225 | ||
226 | /* | |
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... | |
232 | */ | |
233 | static void buffer_io_error(struct buffer_head *bh) | |
234 | { | |
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); | |
239 | } | |
240 | ||
241 | static void ext4_end_bio(struct bio *bio, int error) | |
242 | { | |
243 | ext4_io_end_t *io_end = bio->bi_private; | |
bd2d0210 | 244 | struct inode *inode; |
bd2d0210 | 245 | int i; |
d50bdd5a | 246 | sector_t bi_sector = bio->bi_sector; |
bd2d0210 TT |
247 | |
248 | BUG_ON(!io_end); | |
bd2d0210 TT |
249 | bio->bi_private = NULL; |
250 | bio->bi_end_io = NULL; | |
251 | if (test_bit(BIO_UPTODATE, &bio->bi_flags)) | |
252 | error = 0; | |
bd2d0210 TT |
253 | bio_put(bio); |
254 | ||
bd2d0210 TT |
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; | |
39db00f1 CW |
258 | loff_t offset; |
259 | loff_t io_end_offset; | |
bd2d0210 | 260 | |
39db00f1 | 261 | if (error) { |
bd2d0210 | 262 | SetPageError(page); |
39db00f1 CW |
263 | set_bit(AS_EIO, &page->mapping->flags); |
264 | head = page_buffers(page); | |
265 | BUG_ON(!head); | |
266 | ||
267 | io_end_offset = io_end->offset + io_end->size; | |
bd2d0210 TT |
268 | |
269 | offset = (sector_t) page->index << PAGE_CACHE_SHIFT; | |
270 | bh = head; | |
271 | do { | |
272 | if ((offset >= io_end->offset) && | |
39db00f1 CW |
273 | (offset+bh->b_size <= io_end_offset)) |
274 | buffer_io_error(bh); | |
275 | ||
bd2d0210 TT |
276 | offset += bh->b_size; |
277 | bh = bh->b_this_page; | |
278 | } while (bh != head); | |
279 | } | |
280 | ||
6268b325 | 281 | put_io_page(io_end->pages[i]); |
bd2d0210 | 282 | } |
6268b325 | 283 | io_end->num_io_pages = 0; |
f7ad6d2e TT |
284 | inode = io_end->inode; |
285 | ||
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) | |
d50bdd5a | 294 | bi_sector >> (inode->i_blkbits - 9)); |
f7ad6d2e | 295 | } |
bd2d0210 | 296 | |
b6168443 TT |
297 | if (!(io_end->flag & EXT4_IO_END_UNWRITTEN)) { |
298 | ext4_free_io_end(io_end); | |
299 | return; | |
300 | } | |
301 | ||
28a535f9 | 302 | ext4_add_complete_io(io_end); |
bd2d0210 TT |
303 | } |
304 | ||
305 | void ext4_io_submit(struct ext4_io_submit *io) | |
306 | { | |
307 | struct bio *bio = io->io_bio; | |
308 | ||
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); | |
314 | } | |
7dc57615 | 315 | io->io_bio = NULL; |
bd2d0210 | 316 | io->io_op = 0; |
7dc57615 | 317 | io->io_end = NULL; |
bd2d0210 TT |
318 | } |
319 | ||
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) | |
324 | { | |
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; | |
329 | ||
330 | io_end = ext4_init_io_end(inode, GFP_NOFS); | |
331 | if (!io_end) | |
332 | return -ENOMEM; | |
275d3ba6 | 333 | bio = bio_alloc(GFP_NOIO, min(nvecs, BIO_MAX_PAGES)); |
bd2d0210 TT |
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; | |
338 | ||
bd2d0210 TT |
339 | io_end->offset = (page->index << PAGE_CACHE_SHIFT) + bh_offset(bh); |
340 | ||
341 | io->io_bio = bio; | |
721a9602 | 342 | io->io_op = (wbc->sync_mode == WB_SYNC_ALL ? WRITE_SYNC : WRITE); |
bd2d0210 TT |
343 | io->io_next_block = bh->b_blocknr; |
344 | return 0; | |
345 | } | |
346 | ||
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) | |
352 | { | |
353 | ext4_io_end_t *io_end; | |
354 | int ret; | |
355 | ||
356 | if (buffer_new(bh)) { | |
357 | clear_buffer_new(bh); | |
358 | unmap_underlying_metadata(bh->b_bdev, bh->b_blocknr); | |
359 | } | |
360 | ||
bd2d0210 TT |
361 | if (io->io_bio && bh->b_blocknr != io->io_next_block) { |
362 | submit_and_retry: | |
363 | ext4_io_submit(io); | |
364 | } | |
365 | if (io->io_bio == NULL) { | |
366 | ret = io_submit_init(io, inode, wbc, bh); | |
367 | if (ret) | |
368 | return ret; | |
369 | } | |
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; | |
0edeb71d TM |
374 | if (buffer_uninit(bh)) |
375 | ext4_set_io_unwritten_flag(inode, io_end); | |
bd2d0210 TT |
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; | |
83668e71 | 384 | atomic_inc(&io_page->p_count); |
bd2d0210 TT |
385 | } |
386 | return 0; | |
387 | } | |
388 | ||
389 | int ext4_bio_write_page(struct ext4_io_submit *io, | |
390 | struct page *page, | |
391 | int len, | |
392 | struct writeback_control *wbc) | |
393 | { | |
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; | |
399 | ||
400 | blocksize = 1 << inode->i_blkbits; | |
401 | ||
d50bdd5a | 402 | BUG_ON(!PageLocked(page)); |
bd2d0210 | 403 | BUG_ON(PageWriteback(page)); |
bd2d0210 TT |
404 | |
405 | io_page = kmem_cache_alloc(io_page_cachep, GFP_NOFS); | |
406 | if (!io_page) { | |
1ae48a63 | 407 | redirty_page_for_writepage(wbc, page); |
bd2d0210 TT |
408 | unlock_page(page); |
409 | return -ENOMEM; | |
410 | } | |
411 | io_page->p_page = page; | |
83668e71 | 412 | atomic_set(&io_page->p_count, 1); |
bd2d0210 | 413 | get_page(page); |
a54aa761 TT |
414 | set_page_writeback(page); |
415 | ClearPageError(page); | |
bd2d0210 TT |
416 | |
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) { | |
d50bdd5a | 420 | |
bd2d0210 TT |
421 | block_end = block_start + blocksize; |
422 | if (block_start >= len) { | |
5a0dc736 YY |
423 | /* |
424 | * Comments copied from block_write_full_page_endio: | |
425 | * | |
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." | |
433 | */ | |
434 | zero_user_segment(page, block_start, block_end); | |
bd2d0210 TT |
435 | clear_buffer_dirty(bh); |
436 | set_buffer_uptodate(bh); | |
437 | continue; | |
438 | } | |
8a850c3f JK |
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; | |
447 | } | |
bd2d0210 TT |
448 | ret = io_submit_add_bh(io, io_page, inode, wbc, bh); |
449 | if (ret) { | |
450 | /* | |
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. | |
454 | */ | |
1ae48a63 | 455 | redirty_page_for_writepage(wbc, page); |
bd2d0210 TT |
456 | break; |
457 | } | |
1ae48a63 | 458 | clear_buffer_dirty(bh); |
bd2d0210 TT |
459 | } |
460 | unlock_page(page); | |
461 | /* | |
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. | |
468 | */ | |
83668e71 | 469 | put_io_page(io_page); |
bd2d0210 TT |
470 | return ret; |
471 | } |