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 | ||
9 | #include <linux/module.h> | |
10 | #include <linux/fs.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> | |
27 | ||
28 | #include "ext4_jbd2.h" | |
29 | #include "xattr.h" | |
30 | #include "acl.h" | |
31 | #include "ext4_extents.h" | |
32 | ||
33 | static struct kmem_cache *io_page_cachep, *io_end_cachep; | |
34 | ||
5dabfc78 | 35 | int __init ext4_init_pageio(void) |
bd2d0210 TT |
36 | { |
37 | io_page_cachep = KMEM_CACHE(ext4_io_page, SLAB_RECLAIM_ACCOUNT); | |
38 | if (io_page_cachep == NULL) | |
39 | return -ENOMEM; | |
40 | io_end_cachep = KMEM_CACHE(ext4_io_end, SLAB_RECLAIM_ACCOUNT); | |
13195184 | 41 | if (io_end_cachep == NULL) { |
bd2d0210 TT |
42 | kmem_cache_destroy(io_page_cachep); |
43 | return -ENOMEM; | |
44 | } | |
bd2d0210 TT |
45 | return 0; |
46 | } | |
47 | ||
5dabfc78 | 48 | void ext4_exit_pageio(void) |
bd2d0210 TT |
49 | { |
50 | kmem_cache_destroy(io_end_cachep); | |
51 | kmem_cache_destroy(io_page_cachep); | |
52 | } | |
53 | ||
f7ad6d2e TT |
54 | void ext4_ioend_wait(struct inode *inode) |
55 | { | |
e9e3bcec | 56 | wait_queue_head_t *wq = ext4_ioend_wq(inode); |
f7ad6d2e TT |
57 | |
58 | wait_event(*wq, (atomic_read(&EXT4_I(inode)->i_ioend_count) == 0)); | |
59 | } | |
60 | ||
83668e71 TT |
61 | static void put_io_page(struct ext4_io_page *io_page) |
62 | { | |
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); | |
67 | } | |
68 | } | |
69 | ||
bd2d0210 TT |
70 | void ext4_free_io_end(ext4_io_end_t *io) |
71 | { | |
72 | int i; | |
73 | ||
74 | BUG_ON(!io); | |
75 | if (io->page) | |
76 | put_page(io->page); | |
83668e71 TT |
77 | for (i = 0; i < io->num_io_pages; i++) |
78 | put_io_page(io->pages[i]); | |
bd2d0210 | 79 | io->num_io_pages = 0; |
4e298021 TT |
80 | if (atomic_dec_and_test(&EXT4_I(io->inode)->i_ioend_count)) |
81 | wake_up_all(ext4_ioend_wq(io->inode)); | |
bd2d0210 TT |
82 | kmem_cache_free(io_end_cachep, io); |
83 | } | |
84 | ||
85 | /* | |
86 | * check a range of space and convert unwritten extents to written. | |
d73d5046 TM |
87 | * |
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() | |
bd2d0210 TT |
90 | */ |
91 | int ext4_end_io_nolock(ext4_io_end_t *io) | |
92 | { | |
93 | struct inode *inode = io->inode; | |
94 | loff_t offset = io->offset; | |
95 | ssize_t size = io->size; | |
96 | int ret = 0; | |
97 | ||
98 | ext4_debug("ext4_end_io_nolock: io 0x%p from inode %lu,list->next 0x%p," | |
99 | "list->prev 0x%p\n", | |
100 | io, inode->i_ino, io->list.next, io->list.prev); | |
101 | ||
bd2d0210 TT |
102 | if (!(io->flag & EXT4_IO_END_UNWRITTEN)) |
103 | return ret; | |
104 | ||
105 | ret = ext4_convert_unwritten_extents(inode, offset, size); | |
106 | if (ret < 0) { | |
107 | printk(KERN_EMERG "%s: failed to convert unwritten " | |
108 | "extents to written extents, error is %d " | |
109 | "io is still on inode %lu aio dio list\n", | |
110 | __func__, ret, inode->i_ino); | |
111 | return ret; | |
112 | } | |
113 | ||
114 | if (io->iocb) | |
115 | aio_complete(io->iocb, io->result, 0); | |
116 | /* clear the DIO AIO unwritten flag */ | |
e9e3bcec ES |
117 | if (io->flag & EXT4_IO_END_UNWRITTEN) { |
118 | io->flag &= ~EXT4_IO_END_UNWRITTEN; | |
119 | /* Wake up anyone waiting on unwritten extent conversion */ | |
4e298021 TT |
120 | if (atomic_dec_and_test(&EXT4_I(inode)->i_aiodio_unwritten)) |
121 | wake_up_all(ext4_ioend_wq(io->inode)); | |
e9e3bcec ES |
122 | } |
123 | ||
bd2d0210 TT |
124 | return ret; |
125 | } | |
126 | ||
127 | /* | |
128 | * work on completed aio dio IO, to convert unwritten extents to extents | |
129 | */ | |
130 | static void ext4_end_io_work(struct work_struct *work) | |
131 | { | |
132 | ext4_io_end_t *io = container_of(work, ext4_io_end_t, work); | |
133 | struct inode *inode = io->inode; | |
134 | struct ext4_inode_info *ei = EXT4_I(inode); | |
135 | unsigned long flags; | |
136 | int ret; | |
137 | ||
d73d5046 TM |
138 | spin_lock_irqsave(&ei->i_completed_io_lock, flags); |
139 | if (list_empty(&io->list)) { | |
140 | spin_unlock_irqrestore(&ei->i_completed_io_lock, flags); | |
141 | goto free; | |
142 | } | |
143 | spin_unlock_irqrestore(&ei->i_completed_io_lock, flags); | |
144 | ||
8c0bec21 JZ |
145 | if (!mutex_trylock(&inode->i_mutex)) { |
146 | /* | |
147 | * Requeue the work instead of waiting so that the work | |
148 | * items queued after this can be processed. | |
149 | */ | |
150 | queue_work(EXT4_SB(inode->i_sb)->dio_unwritten_wq, &io->work); | |
151 | /* | |
152 | * To prevent the ext4-dio-unwritten thread from keeping | |
153 | * requeueing end_io requests and occupying cpu for too long, | |
154 | * yield the cpu if it sees an end_io request that has already | |
155 | * been requeued. | |
156 | */ | |
157 | if (io->flag & EXT4_IO_END_QUEUED) | |
158 | yield(); | |
159 | io->flag |= EXT4_IO_END_QUEUED; | |
160 | return; | |
161 | } | |
bd2d0210 TT |
162 | ret = ext4_end_io_nolock(io); |
163 | if (ret < 0) { | |
164 | mutex_unlock(&inode->i_mutex); | |
165 | return; | |
166 | } | |
167 | ||
168 | spin_lock_irqsave(&ei->i_completed_io_lock, flags); | |
169 | if (!list_empty(&io->list)) | |
170 | list_del_init(&io->list); | |
171 | spin_unlock_irqrestore(&ei->i_completed_io_lock, flags); | |
172 | mutex_unlock(&inode->i_mutex); | |
d73d5046 | 173 | free: |
bd2d0210 TT |
174 | ext4_free_io_end(io); |
175 | } | |
176 | ||
177 | ext4_io_end_t *ext4_init_io_end(struct inode *inode, gfp_t flags) | |
178 | { | |
b17b35ec | 179 | ext4_io_end_t *io = kmem_cache_zalloc(io_end_cachep, flags); |
bd2d0210 | 180 | if (io) { |
f7ad6d2e TT |
181 | atomic_inc(&EXT4_I(inode)->i_ioend_count); |
182 | io->inode = inode; | |
bd2d0210 TT |
183 | INIT_WORK(&io->work, ext4_end_io_work); |
184 | INIT_LIST_HEAD(&io->list); | |
185 | } | |
186 | return io; | |
187 | } | |
188 | ||
189 | /* | |
190 | * Print an buffer I/O error compatible with the fs/buffer.c. This | |
191 | * provides compatibility with dmesg scrapers that look for a specific | |
192 | * buffer I/O error message. We really need a unified error reporting | |
193 | * structure to userspace ala Digital Unix's uerf system, but it's | |
194 | * probably not going to happen in my lifetime, due to LKML politics... | |
195 | */ | |
196 | static void buffer_io_error(struct buffer_head *bh) | |
197 | { | |
198 | char b[BDEVNAME_SIZE]; | |
199 | printk(KERN_ERR "Buffer I/O error on device %s, logical block %llu\n", | |
200 | bdevname(bh->b_bdev, b), | |
201 | (unsigned long long)bh->b_blocknr); | |
202 | } | |
203 | ||
204 | static void ext4_end_bio(struct bio *bio, int error) | |
205 | { | |
206 | ext4_io_end_t *io_end = bio->bi_private; | |
207 | struct workqueue_struct *wq; | |
208 | struct inode *inode; | |
209 | unsigned long flags; | |
bd2d0210 | 210 | int i; |
d50bdd5a | 211 | sector_t bi_sector = bio->bi_sector; |
bd2d0210 TT |
212 | |
213 | BUG_ON(!io_end); | |
bd2d0210 TT |
214 | bio->bi_private = NULL; |
215 | bio->bi_end_io = NULL; | |
216 | if (test_bit(BIO_UPTODATE, &bio->bi_flags)) | |
217 | error = 0; | |
bd2d0210 TT |
218 | bio_put(bio); |
219 | ||
bd2d0210 TT |
220 | for (i = 0; i < io_end->num_io_pages; i++) { |
221 | struct page *page = io_end->pages[i]->p_page; | |
222 | struct buffer_head *bh, *head; | |
39db00f1 CW |
223 | loff_t offset; |
224 | loff_t io_end_offset; | |
bd2d0210 | 225 | |
39db00f1 | 226 | if (error) { |
bd2d0210 | 227 | SetPageError(page); |
39db00f1 CW |
228 | set_bit(AS_EIO, &page->mapping->flags); |
229 | head = page_buffers(page); | |
230 | BUG_ON(!head); | |
231 | ||
232 | io_end_offset = io_end->offset + io_end->size; | |
bd2d0210 TT |
233 | |
234 | offset = (sector_t) page->index << PAGE_CACHE_SHIFT; | |
235 | bh = head; | |
236 | do { | |
237 | if ((offset >= io_end->offset) && | |
39db00f1 CW |
238 | (offset+bh->b_size <= io_end_offset)) |
239 | buffer_io_error(bh); | |
240 | ||
bd2d0210 TT |
241 | offset += bh->b_size; |
242 | bh = bh->b_this_page; | |
243 | } while (bh != head); | |
244 | } | |
245 | ||
08da1193 | 246 | put_io_page(io_end->pages[i]); |
bd2d0210 | 247 | } |
bd2d0210 | 248 | io_end->num_io_pages = 0; |
f7ad6d2e TT |
249 | inode = io_end->inode; |
250 | ||
251 | if (error) { | |
252 | io_end->flag |= EXT4_IO_END_ERROR; | |
253 | ext4_warning(inode->i_sb, "I/O error writing to inode %lu " | |
254 | "(offset %llu size %ld starting block %llu)", | |
255 | inode->i_ino, | |
256 | (unsigned long long) io_end->offset, | |
257 | (long) io_end->size, | |
258 | (unsigned long long) | |
d50bdd5a | 259 | bi_sector >> (inode->i_blkbits - 9)); |
f7ad6d2e | 260 | } |
bd2d0210 | 261 | |
b6168443 TT |
262 | if (!(io_end->flag & EXT4_IO_END_UNWRITTEN)) { |
263 | ext4_free_io_end(io_end); | |
264 | return; | |
265 | } | |
266 | ||
bd2d0210 TT |
267 | /* Add the io_end to per-inode completed io list*/ |
268 | spin_lock_irqsave(&EXT4_I(inode)->i_completed_io_lock, flags); | |
269 | list_add_tail(&io_end->list, &EXT4_I(inode)->i_completed_io_list); | |
270 | spin_unlock_irqrestore(&EXT4_I(inode)->i_completed_io_lock, flags); | |
271 | ||
272 | wq = EXT4_SB(inode->i_sb)->dio_unwritten_wq; | |
273 | /* queue the work to convert unwritten extents to written */ | |
274 | queue_work(wq, &io_end->work); | |
275 | } | |
276 | ||
277 | void ext4_io_submit(struct ext4_io_submit *io) | |
278 | { | |
279 | struct bio *bio = io->io_bio; | |
280 | ||
281 | if (bio) { | |
282 | bio_get(io->io_bio); | |
283 | submit_bio(io->io_op, io->io_bio); | |
284 | BUG_ON(bio_flagged(io->io_bio, BIO_EOPNOTSUPP)); | |
285 | bio_put(io->io_bio); | |
286 | } | |
7dc57615 | 287 | io->io_bio = NULL; |
bd2d0210 | 288 | io->io_op = 0; |
7dc57615 | 289 | io->io_end = NULL; |
bd2d0210 TT |
290 | } |
291 | ||
292 | static int io_submit_init(struct ext4_io_submit *io, | |
293 | struct inode *inode, | |
294 | struct writeback_control *wbc, | |
295 | struct buffer_head *bh) | |
296 | { | |
297 | ext4_io_end_t *io_end; | |
298 | struct page *page = bh->b_page; | |
299 | int nvecs = bio_get_nr_vecs(bh->b_bdev); | |
300 | struct bio *bio; | |
301 | ||
302 | io_end = ext4_init_io_end(inode, GFP_NOFS); | |
303 | if (!io_end) | |
304 | return -ENOMEM; | |
275d3ba6 | 305 | bio = bio_alloc(GFP_NOIO, min(nvecs, BIO_MAX_PAGES)); |
bd2d0210 TT |
306 | bio->bi_sector = bh->b_blocknr * (bh->b_size >> 9); |
307 | bio->bi_bdev = bh->b_bdev; | |
308 | bio->bi_private = io->io_end = io_end; | |
309 | bio->bi_end_io = ext4_end_bio; | |
310 | ||
bd2d0210 TT |
311 | io_end->offset = (page->index << PAGE_CACHE_SHIFT) + bh_offset(bh); |
312 | ||
313 | io->io_bio = bio; | |
721a9602 | 314 | io->io_op = (wbc->sync_mode == WB_SYNC_ALL ? WRITE_SYNC : WRITE); |
bd2d0210 TT |
315 | io->io_next_block = bh->b_blocknr; |
316 | return 0; | |
317 | } | |
318 | ||
319 | static int io_submit_add_bh(struct ext4_io_submit *io, | |
320 | struct ext4_io_page *io_page, | |
321 | struct inode *inode, | |
322 | struct writeback_control *wbc, | |
323 | struct buffer_head *bh) | |
324 | { | |
325 | ext4_io_end_t *io_end; | |
326 | int ret; | |
327 | ||
328 | if (buffer_new(bh)) { | |
329 | clear_buffer_new(bh); | |
330 | unmap_underlying_metadata(bh->b_bdev, bh->b_blocknr); | |
331 | } | |
332 | ||
333 | if (!buffer_mapped(bh) || buffer_delay(bh)) { | |
334 | if (!buffer_mapped(bh)) | |
335 | clear_buffer_dirty(bh); | |
336 | if (io->io_bio) | |
337 | ext4_io_submit(io); | |
338 | return 0; | |
339 | } | |
340 | ||
341 | if (io->io_bio && bh->b_blocknr != io->io_next_block) { | |
342 | submit_and_retry: | |
343 | ext4_io_submit(io); | |
344 | } | |
345 | if (io->io_bio == NULL) { | |
346 | ret = io_submit_init(io, inode, wbc, bh); | |
347 | if (ret) | |
348 | return ret; | |
349 | } | |
350 | io_end = io->io_end; | |
351 | if ((io_end->num_io_pages >= MAX_IO_PAGES) && | |
352 | (io_end->pages[io_end->num_io_pages-1] != io_page)) | |
353 | goto submit_and_retry; | |
32c80b32 TM |
354 | if (buffer_uninit(bh) && !(io_end->flag & EXT4_IO_END_UNWRITTEN)) { |
355 | io_end->flag |= EXT4_IO_END_UNWRITTEN; | |
356 | atomic_inc(&EXT4_I(inode)->i_aiodio_unwritten); | |
357 | } | |
bd2d0210 TT |
358 | io->io_end->size += bh->b_size; |
359 | io->io_next_block++; | |
360 | ret = bio_add_page(io->io_bio, bh->b_page, bh->b_size, bh_offset(bh)); | |
361 | if (ret != bh->b_size) | |
362 | goto submit_and_retry; | |
363 | if ((io_end->num_io_pages == 0) || | |
364 | (io_end->pages[io_end->num_io_pages-1] != io_page)) { | |
365 | io_end->pages[io_end->num_io_pages++] = io_page; | |
83668e71 | 366 | atomic_inc(&io_page->p_count); |
bd2d0210 TT |
367 | } |
368 | return 0; | |
369 | } | |
370 | ||
371 | int ext4_bio_write_page(struct ext4_io_submit *io, | |
372 | struct page *page, | |
373 | int len, | |
374 | struct writeback_control *wbc) | |
375 | { | |
376 | struct inode *inode = page->mapping->host; | |
377 | unsigned block_start, block_end, blocksize; | |
378 | struct ext4_io_page *io_page; | |
379 | struct buffer_head *bh, *head; | |
380 | int ret = 0; | |
381 | ||
382 | blocksize = 1 << inode->i_blkbits; | |
383 | ||
d50bdd5a | 384 | BUG_ON(!PageLocked(page)); |
bd2d0210 | 385 | BUG_ON(PageWriteback(page)); |
bd2d0210 TT |
386 | |
387 | io_page = kmem_cache_alloc(io_page_cachep, GFP_NOFS); | |
388 | if (!io_page) { | |
389 | set_page_dirty(page); | |
390 | unlock_page(page); | |
391 | return -ENOMEM; | |
392 | } | |
393 | io_page->p_page = page; | |
83668e71 | 394 | atomic_set(&io_page->p_count, 1); |
bd2d0210 | 395 | get_page(page); |
a54aa761 TT |
396 | set_page_writeback(page); |
397 | ClearPageError(page); | |
bd2d0210 TT |
398 | |
399 | for (bh = head = page_buffers(page), block_start = 0; | |
400 | bh != head || !block_start; | |
401 | block_start = block_end, bh = bh->b_this_page) { | |
d50bdd5a | 402 | |
bd2d0210 TT |
403 | block_end = block_start + blocksize; |
404 | if (block_start >= len) { | |
405 | clear_buffer_dirty(bh); | |
406 | set_buffer_uptodate(bh); | |
407 | continue; | |
408 | } | |
d50bdd5a | 409 | clear_buffer_dirty(bh); |
bd2d0210 TT |
410 | ret = io_submit_add_bh(io, io_page, inode, wbc, bh); |
411 | if (ret) { | |
412 | /* | |
413 | * We only get here on ENOMEM. Not much else | |
414 | * we can do but mark the page as dirty, and | |
415 | * better luck next time. | |
416 | */ | |
417 | set_page_dirty(page); | |
418 | break; | |
419 | } | |
420 | } | |
421 | unlock_page(page); | |
422 | /* | |
423 | * If the page was truncated before we could do the writeback, | |
424 | * or we had a memory allocation error while trying to write | |
425 | * the first buffer head, we won't have submitted any pages for | |
426 | * I/O. In that case we need to make sure we've cleared the | |
427 | * PageWriteback bit from the page to prevent the system from | |
428 | * wedging later on. | |
429 | */ | |
83668e71 | 430 | put_io_page(io_page); |
bd2d0210 TT |
431 | return ret; |
432 | } |