Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * fs/mpage.c | |
3 | * | |
4 | * Copyright (C) 2002, Linus Torvalds. | |
5 | * | |
6 | * Contains functions related to preparing and submitting BIOs which contain | |
7 | * multiple pagecache pages. | |
8 | * | |
e1f8e874 | 9 | * 15May2002 Andrew Morton |
1da177e4 LT |
10 | * Initial version |
11 | * 27Jun2002 axboe@suse.de | |
12 | * use bio_add_page() to build bio's just the right size | |
13 | */ | |
14 | ||
15 | #include <linux/kernel.h> | |
630d9c47 | 16 | #include <linux/export.h> |
1da177e4 LT |
17 | #include <linux/mm.h> |
18 | #include <linux/kdev_t.h> | |
5a0e3ad6 | 19 | #include <linux/gfp.h> |
1da177e4 LT |
20 | #include <linux/bio.h> |
21 | #include <linux/fs.h> | |
22 | #include <linux/buffer_head.h> | |
23 | #include <linux/blkdev.h> | |
24 | #include <linux/highmem.h> | |
25 | #include <linux/prefetch.h> | |
26 | #include <linux/mpage.h> | |
27 | #include <linux/writeback.h> | |
28 | #include <linux/backing-dev.h> | |
29 | #include <linux/pagevec.h> | |
c515e1fd | 30 | #include <linux/cleancache.h> |
4db96b71 | 31 | #include "internal.h" |
1da177e4 | 32 | |
32cbbe59 MS |
33 | #define CREATE_TRACE_POINTS |
34 | #include <trace/events/android_fs.h> | |
35 | ||
36 | EXPORT_TRACEPOINT_SYMBOL(android_fs_datawrite_start); | |
37 | EXPORT_TRACEPOINT_SYMBOL(android_fs_datawrite_end); | |
38 | EXPORT_TRACEPOINT_SYMBOL(android_fs_dataread_start); | |
39 | EXPORT_TRACEPOINT_SYMBOL(android_fs_dataread_end); | |
40 | ||
1da177e4 LT |
41 | /* |
42 | * I/O completion handler for multipage BIOs. | |
43 | * | |
44 | * The mpage code never puts partial pages into a BIO (except for end-of-file). | |
45 | * If a page does not map to a contiguous run of blocks then it simply falls | |
46 | * back to block_read_full_page(). | |
47 | * | |
48 | * Why is this? If a page's completion depends on a number of different BIOs | |
49 | * which can complete in any order (or at the same time) then determining the | |
50 | * status of that page is hard. See end_buffer_async_read() for the details. | |
51 | * There is no point in duplicating all that complexity. | |
52 | */ | |
4246a0b6 | 53 | static void mpage_end_io(struct bio *bio) |
1da177e4 | 54 | { |
2c30c71b KO |
55 | struct bio_vec *bv; |
56 | int i; | |
1da177e4 | 57 | |
32cbbe59 MS |
58 | if (trace_android_fs_dataread_end_enabled() && |
59 | (bio_data_dir(bio) == READ)) { | |
60 | struct page *first_page = bio->bi_io_vec[0].bv_page; | |
61 | ||
62 | if (first_page != NULL) | |
63 | trace_android_fs_dataread_end(first_page->mapping->host, | |
64 | page_offset(first_page), | |
65 | bio->bi_iter.bi_size); | |
66 | } | |
67 | ||
2c30c71b KO |
68 | bio_for_each_segment_all(bv, bio, i) { |
69 | struct page *page = bv->bv_page; | |
4246a0b6 | 70 | page_endio(page, bio_data_dir(bio), bio->bi_error); |
2c30c71b KO |
71 | } |
72 | ||
1da177e4 | 73 | bio_put(bio); |
1da177e4 LT |
74 | } |
75 | ||
ced117c7 | 76 | static struct bio *mpage_bio_submit(int rw, struct bio *bio) |
1da177e4 | 77 | { |
32cbbe59 MS |
78 | if (trace_android_fs_dataread_start_enabled() && (rw == READ)) { |
79 | struct page *first_page = bio->bi_io_vec[0].bv_page; | |
80 | ||
81 | if (first_page != NULL) { | |
82 | trace_android_fs_dataread_start( | |
83 | first_page->mapping->host, | |
84 | page_offset(first_page), | |
85 | bio->bi_iter.bi_size, | |
86 | current->pid, | |
87 | current->comm); | |
88 | } | |
89 | } | |
c32b0d4b | 90 | bio->bi_end_io = mpage_end_io; |
4db96b71 | 91 | guard_bio_eod(rw, bio); |
1da177e4 LT |
92 | submit_bio(rw, bio); |
93 | return NULL; | |
94 | } | |
95 | ||
96 | static struct bio * | |
97 | mpage_alloc(struct block_device *bdev, | |
98 | sector_t first_sector, int nr_vecs, | |
dd0fc66f | 99 | gfp_t gfp_flags) |
1da177e4 LT |
100 | { |
101 | struct bio *bio; | |
102 | ||
103 | bio = bio_alloc(gfp_flags, nr_vecs); | |
104 | ||
105 | if (bio == NULL && (current->flags & PF_MEMALLOC)) { | |
106 | while (!bio && (nr_vecs /= 2)) | |
107 | bio = bio_alloc(gfp_flags, nr_vecs); | |
108 | } | |
109 | ||
110 | if (bio) { | |
111 | bio->bi_bdev = bdev; | |
4f024f37 | 112 | bio->bi_iter.bi_sector = first_sector; |
1da177e4 LT |
113 | } |
114 | return bio; | |
115 | } | |
116 | ||
117 | /* | |
118 | * support function for mpage_readpages. The fs supplied get_block might | |
119 | * return an up to date buffer. This is used to map that buffer into | |
120 | * the page, which allows readpage to avoid triggering a duplicate call | |
121 | * to get_block. | |
122 | * | |
123 | * The idea is to avoid adding buffers to pages that don't already have | |
124 | * them. So when the buffer is up to date and the page size == block size, | |
125 | * this marks the page up to date instead of adding new buffers. | |
126 | */ | |
127 | static void | |
128 | map_buffer_to_page(struct page *page, struct buffer_head *bh, int page_block) | |
129 | { | |
130 | struct inode *inode = page->mapping->host; | |
131 | struct buffer_head *page_bh, *head; | |
132 | int block = 0; | |
133 | ||
134 | if (!page_has_buffers(page)) { | |
135 | /* | |
136 | * don't make any buffers if there is only one buffer on | |
137 | * the page and the page just needs to be set up to date | |
138 | */ | |
139 | if (inode->i_blkbits == PAGE_CACHE_SHIFT && | |
140 | buffer_uptodate(bh)) { | |
141 | SetPageUptodate(page); | |
142 | return; | |
143 | } | |
144 | create_empty_buffers(page, 1 << inode->i_blkbits, 0); | |
145 | } | |
146 | head = page_buffers(page); | |
147 | page_bh = head; | |
148 | do { | |
149 | if (block == page_block) { | |
150 | page_bh->b_state = bh->b_state; | |
151 | page_bh->b_bdev = bh->b_bdev; | |
152 | page_bh->b_blocknr = bh->b_blocknr; | |
153 | break; | |
154 | } | |
155 | page_bh = page_bh->b_this_page; | |
156 | block++; | |
157 | } while (page_bh != head); | |
158 | } | |
159 | ||
fa30bd05 BP |
160 | /* |
161 | * This is the worker routine which does all the work of mapping the disk | |
162 | * blocks and constructs largest possible bios, submits them for IO if the | |
163 | * blocks are not contiguous on the disk. | |
164 | * | |
165 | * We pass a buffer_head back and forth and use its buffer_mapped() flag to | |
166 | * represent the validity of its disk mapping and to decide when to do the next | |
167 | * get_block() call. | |
168 | */ | |
1da177e4 LT |
169 | static struct bio * |
170 | do_mpage_readpage(struct bio *bio, struct page *page, unsigned nr_pages, | |
fa30bd05 | 171 | sector_t *last_block_in_bio, struct buffer_head *map_bh, |
063d99b4 MH |
172 | unsigned long *first_logical_block, get_block_t get_block, |
173 | gfp_t gfp) | |
1da177e4 LT |
174 | { |
175 | struct inode *inode = page->mapping->host; | |
176 | const unsigned blkbits = inode->i_blkbits; | |
177 | const unsigned blocks_per_page = PAGE_CACHE_SIZE >> blkbits; | |
178 | const unsigned blocksize = 1 << blkbits; | |
179 | sector_t block_in_file; | |
180 | sector_t last_block; | |
fa30bd05 | 181 | sector_t last_block_in_file; |
1da177e4 LT |
182 | sector_t blocks[MAX_BUF_PER_PAGE]; |
183 | unsigned page_block; | |
184 | unsigned first_hole = blocks_per_page; | |
185 | struct block_device *bdev = NULL; | |
1da177e4 LT |
186 | int length; |
187 | int fully_mapped = 1; | |
fa30bd05 BP |
188 | unsigned nblocks; |
189 | unsigned relative_block; | |
1da177e4 LT |
190 | |
191 | if (page_has_buffers(page)) | |
192 | goto confused; | |
193 | ||
54b21a79 | 194 | block_in_file = (sector_t)page->index << (PAGE_CACHE_SHIFT - blkbits); |
fa30bd05 BP |
195 | last_block = block_in_file + nr_pages * blocks_per_page; |
196 | last_block_in_file = (i_size_read(inode) + blocksize - 1) >> blkbits; | |
197 | if (last_block > last_block_in_file) | |
198 | last_block = last_block_in_file; | |
199 | page_block = 0; | |
200 | ||
201 | /* | |
202 | * Map blocks using the result from the previous get_blocks call first. | |
203 | */ | |
204 | nblocks = map_bh->b_size >> blkbits; | |
205 | if (buffer_mapped(map_bh) && block_in_file > *first_logical_block && | |
206 | block_in_file < (*first_logical_block + nblocks)) { | |
207 | unsigned map_offset = block_in_file - *first_logical_block; | |
208 | unsigned last = nblocks - map_offset; | |
209 | ||
210 | for (relative_block = 0; ; relative_block++) { | |
211 | if (relative_block == last) { | |
212 | clear_buffer_mapped(map_bh); | |
213 | break; | |
214 | } | |
215 | if (page_block == blocks_per_page) | |
216 | break; | |
217 | blocks[page_block] = map_bh->b_blocknr + map_offset + | |
218 | relative_block; | |
219 | page_block++; | |
220 | block_in_file++; | |
221 | } | |
222 | bdev = map_bh->b_bdev; | |
223 | } | |
224 | ||
225 | /* | |
226 | * Then do more get_blocks calls until we are done with this page. | |
227 | */ | |
228 | map_bh->b_page = page; | |
229 | while (page_block < blocks_per_page) { | |
230 | map_bh->b_state = 0; | |
231 | map_bh->b_size = 0; | |
1da177e4 | 232 | |
1da177e4 | 233 | if (block_in_file < last_block) { |
fa30bd05 BP |
234 | map_bh->b_size = (last_block-block_in_file) << blkbits; |
235 | if (get_block(inode, block_in_file, map_bh, 0)) | |
1da177e4 | 236 | goto confused; |
fa30bd05 | 237 | *first_logical_block = block_in_file; |
1da177e4 LT |
238 | } |
239 | ||
fa30bd05 | 240 | if (!buffer_mapped(map_bh)) { |
1da177e4 LT |
241 | fully_mapped = 0; |
242 | if (first_hole == blocks_per_page) | |
243 | first_hole = page_block; | |
fa30bd05 BP |
244 | page_block++; |
245 | block_in_file++; | |
1da177e4 LT |
246 | continue; |
247 | } | |
248 | ||
249 | /* some filesystems will copy data into the page during | |
250 | * the get_block call, in which case we don't want to | |
251 | * read it again. map_buffer_to_page copies the data | |
252 | * we just collected from get_block into the page's buffers | |
253 | * so readpage doesn't have to repeat the get_block call | |
254 | */ | |
fa30bd05 BP |
255 | if (buffer_uptodate(map_bh)) { |
256 | map_buffer_to_page(page, map_bh, page_block); | |
1da177e4 LT |
257 | goto confused; |
258 | } | |
259 | ||
260 | if (first_hole != blocks_per_page) | |
261 | goto confused; /* hole -> non-hole */ | |
262 | ||
263 | /* Contiguous blocks? */ | |
fa30bd05 | 264 | if (page_block && blocks[page_block-1] != map_bh->b_blocknr-1) |
1da177e4 | 265 | goto confused; |
fa30bd05 BP |
266 | nblocks = map_bh->b_size >> blkbits; |
267 | for (relative_block = 0; ; relative_block++) { | |
268 | if (relative_block == nblocks) { | |
269 | clear_buffer_mapped(map_bh); | |
270 | break; | |
271 | } else if (page_block == blocks_per_page) | |
272 | break; | |
273 | blocks[page_block] = map_bh->b_blocknr+relative_block; | |
274 | page_block++; | |
275 | block_in_file++; | |
276 | } | |
277 | bdev = map_bh->b_bdev; | |
1da177e4 LT |
278 | } |
279 | ||
280 | if (first_hole != blocks_per_page) { | |
eebd2aa3 | 281 | zero_user_segment(page, first_hole << blkbits, PAGE_CACHE_SIZE); |
1da177e4 LT |
282 | if (first_hole == 0) { |
283 | SetPageUptodate(page); | |
284 | unlock_page(page); | |
285 | goto out; | |
286 | } | |
287 | } else if (fully_mapped) { | |
288 | SetPageMappedToDisk(page); | |
289 | } | |
290 | ||
c515e1fd DM |
291 | if (fully_mapped && blocks_per_page == 1 && !PageUptodate(page) && |
292 | cleancache_get_page(page) == 0) { | |
293 | SetPageUptodate(page); | |
294 | goto confused; | |
295 | } | |
296 | ||
1da177e4 LT |
297 | /* |
298 | * This page will go to BIO. Do we need to send this BIO off first? | |
299 | */ | |
300 | if (bio && (*last_block_in_bio != blocks[0] - 1)) | |
301 | bio = mpage_bio_submit(READ, bio); | |
302 | ||
303 | alloc_new: | |
304 | if (bio == NULL) { | |
47a191fd MW |
305 | if (first_hole == blocks_per_page) { |
306 | if (!bdev_read_page(bdev, blocks[0] << (blkbits - 9), | |
307 | page)) | |
308 | goto out; | |
309 | } | |
1da177e4 | 310 | bio = mpage_alloc(bdev, blocks[0] << (blkbits - 9), |
063d99b4 | 311 | min_t(int, nr_pages, BIO_MAX_PAGES), gfp); |
1da177e4 LT |
312 | if (bio == NULL) |
313 | goto confused; | |
314 | } | |
315 | ||
316 | length = first_hole << blkbits; | |
317 | if (bio_add_page(bio, page, length, 0) < length) { | |
318 | bio = mpage_bio_submit(READ, bio); | |
319 | goto alloc_new; | |
320 | } | |
321 | ||
38c8e618 MS |
322 | relative_block = block_in_file - *first_logical_block; |
323 | nblocks = map_bh->b_size >> blkbits; | |
324 | if ((buffer_boundary(map_bh) && relative_block == nblocks) || | |
325 | (first_hole != blocks_per_page)) | |
1da177e4 LT |
326 | bio = mpage_bio_submit(READ, bio); |
327 | else | |
328 | *last_block_in_bio = blocks[blocks_per_page - 1]; | |
329 | out: | |
330 | return bio; | |
331 | ||
332 | confused: | |
333 | if (bio) | |
334 | bio = mpage_bio_submit(READ, bio); | |
335 | if (!PageUptodate(page)) | |
336 | block_read_full_page(page, get_block); | |
337 | else | |
338 | unlock_page(page); | |
339 | goto out; | |
340 | } | |
341 | ||
67be2dd1 | 342 | /** |
78a4a50a | 343 | * mpage_readpages - populate an address space with some pages & start reads against them |
67be2dd1 MW |
344 | * @mapping: the address_space |
345 | * @pages: The address of a list_head which contains the target pages. These | |
346 | * pages have their ->index populated and are otherwise uninitialised. | |
67be2dd1 MW |
347 | * The page at @pages->prev has the lowest file offset, and reads should be |
348 | * issued in @pages->prev to @pages->next order. | |
67be2dd1 MW |
349 | * @nr_pages: The number of pages at *@pages |
350 | * @get_block: The filesystem's block mapper function. | |
351 | * | |
352 | * This function walks the pages and the blocks within each page, building and | |
353 | * emitting large BIOs. | |
354 | * | |
355 | * If anything unusual happens, such as: | |
356 | * | |
357 | * - encountering a page which has buffers | |
358 | * - encountering a page which has a non-hole after a hole | |
359 | * - encountering a page with non-contiguous blocks | |
360 | * | |
361 | * then this code just gives up and calls the buffer_head-based read function. | |
362 | * It does handle a page which has holes at the end - that is a common case: | |
363 | * the end-of-file on blocksize < PAGE_CACHE_SIZE setups. | |
364 | * | |
365 | * BH_Boundary explanation: | |
366 | * | |
367 | * There is a problem. The mpage read code assembles several pages, gets all | |
368 | * their disk mappings, and then submits them all. That's fine, but obtaining | |
369 | * the disk mappings may require I/O. Reads of indirect blocks, for example. | |
370 | * | |
371 | * So an mpage read of the first 16 blocks of an ext2 file will cause I/O to be | |
372 | * submitted in the following order: | |
373 | * 12 0 1 2 3 4 5 6 7 8 9 10 11 13 14 15 16 | |
78a4a50a | 374 | * |
67be2dd1 MW |
375 | * because the indirect block has to be read to get the mappings of blocks |
376 | * 13,14,15,16. Obviously, this impacts performance. | |
377 | * | |
378 | * So what we do it to allow the filesystem's get_block() function to set | |
379 | * BH_Boundary when it maps block 11. BH_Boundary says: mapping of the block | |
380 | * after this one will require I/O against a block which is probably close to | |
381 | * this one. So you should push what I/O you have currently accumulated. | |
382 | * | |
383 | * This all causes the disk requests to be issued in the correct order. | |
384 | */ | |
1da177e4 LT |
385 | int |
386 | mpage_readpages(struct address_space *mapping, struct list_head *pages, | |
387 | unsigned nr_pages, get_block_t get_block) | |
388 | { | |
389 | struct bio *bio = NULL; | |
390 | unsigned page_idx; | |
391 | sector_t last_block_in_bio = 0; | |
fa30bd05 BP |
392 | struct buffer_head map_bh; |
393 | unsigned long first_logical_block = 0; | |
c62d2555 | 394 | gfp_t gfp = mapping_gfp_constraint(mapping, GFP_KERNEL); |
1da177e4 | 395 | |
79ffab34 AK |
396 | map_bh.b_state = 0; |
397 | map_bh.b_size = 0; | |
1da177e4 LT |
398 | for (page_idx = 0; page_idx < nr_pages; page_idx++) { |
399 | struct page *page = list_entry(pages->prev, struct page, lru); | |
400 | ||
401 | prefetchw(&page->flags); | |
402 | list_del(&page->lru); | |
eb2be189 | 403 | if (!add_to_page_cache_lru(page, mapping, |
063d99b4 MH |
404 | page->index, |
405 | gfp)) { | |
1da177e4 LT |
406 | bio = do_mpage_readpage(bio, page, |
407 | nr_pages - page_idx, | |
fa30bd05 BP |
408 | &last_block_in_bio, &map_bh, |
409 | &first_logical_block, | |
063d99b4 | 410 | get_block, gfp); |
1da177e4 | 411 | } |
eb2be189 | 412 | page_cache_release(page); |
1da177e4 | 413 | } |
1da177e4 LT |
414 | BUG_ON(!list_empty(pages)); |
415 | if (bio) | |
416 | mpage_bio_submit(READ, bio); | |
417 | return 0; | |
418 | } | |
419 | EXPORT_SYMBOL(mpage_readpages); | |
420 | ||
421 | /* | |
422 | * This isn't called much at all | |
423 | */ | |
424 | int mpage_readpage(struct page *page, get_block_t get_block) | |
425 | { | |
426 | struct bio *bio = NULL; | |
427 | sector_t last_block_in_bio = 0; | |
fa30bd05 BP |
428 | struct buffer_head map_bh; |
429 | unsigned long first_logical_block = 0; | |
c62d2555 | 430 | gfp_t gfp = mapping_gfp_constraint(page->mapping, GFP_KERNEL); |
1da177e4 | 431 | |
79ffab34 AK |
432 | map_bh.b_state = 0; |
433 | map_bh.b_size = 0; | |
fa30bd05 | 434 | bio = do_mpage_readpage(bio, page, 1, &last_block_in_bio, |
063d99b4 | 435 | &map_bh, &first_logical_block, get_block, gfp); |
1da177e4 LT |
436 | if (bio) |
437 | mpage_bio_submit(READ, bio); | |
438 | return 0; | |
439 | } | |
440 | EXPORT_SYMBOL(mpage_readpage); | |
441 | ||
442 | /* | |
443 | * Writing is not so simple. | |
444 | * | |
445 | * If the page has buffers then they will be used for obtaining the disk | |
446 | * mapping. We only support pages which are fully mapped-and-dirty, with a | |
447 | * special case for pages which are unmapped at the end: end-of-file. | |
448 | * | |
449 | * If the page has no buffers (preferred) then the page is mapped here. | |
450 | * | |
451 | * If all blocks are found to be contiguous then the page can go into the | |
452 | * BIO. Otherwise fall back to the mapping's writepage(). | |
453 | * | |
454 | * FIXME: This code wants an estimate of how many pages are still to be | |
455 | * written, so it can intelligently allocate a suitably-sized BIO. For now, | |
456 | * just allocate full-size (16-page) BIOs. | |
457 | */ | |
0ea97180 | 458 | |
ced117c7 DV |
459 | struct mpage_data { |
460 | struct bio *bio; | |
461 | sector_t last_block_in_bio; | |
462 | get_block_t *get_block; | |
463 | unsigned use_writepage; | |
464 | }; | |
465 | ||
90768eee MW |
466 | /* |
467 | * We have our BIO, so we can now mark the buffers clean. Make | |
468 | * sure to only clean buffers which we know we'll be writing. | |
469 | */ | |
470 | static void clean_buffers(struct page *page, unsigned first_unmapped) | |
471 | { | |
472 | unsigned buffer_counter = 0; | |
473 | struct buffer_head *bh, *head; | |
474 | if (!page_has_buffers(page)) | |
475 | return; | |
476 | head = page_buffers(page); | |
477 | bh = head; | |
478 | ||
479 | do { | |
480 | if (buffer_counter++ == first_unmapped) | |
481 | break; | |
482 | clear_buffer_dirty(bh); | |
483 | bh = bh->b_this_page; | |
484 | } while (bh != head); | |
485 | ||
486 | /* | |
487 | * we cannot drop the bh if the page is not uptodate or a concurrent | |
488 | * readpage would fail to serialize with the bh and it would read from | |
489 | * disk before we reach the platter. | |
490 | */ | |
491 | if (buffer_heads_over_limit && PageUptodate(page)) | |
492 | try_to_free_buffers(page); | |
493 | } | |
494 | ||
ced117c7 | 495 | static int __mpage_writepage(struct page *page, struct writeback_control *wbc, |
29a814d2 | 496 | void *data) |
1da177e4 | 497 | { |
0ea97180 MS |
498 | struct mpage_data *mpd = data; |
499 | struct bio *bio = mpd->bio; | |
1da177e4 LT |
500 | struct address_space *mapping = page->mapping; |
501 | struct inode *inode = page->mapping->host; | |
502 | const unsigned blkbits = inode->i_blkbits; | |
503 | unsigned long end_index; | |
504 | const unsigned blocks_per_page = PAGE_CACHE_SIZE >> blkbits; | |
505 | sector_t last_block; | |
506 | sector_t block_in_file; | |
507 | sector_t blocks[MAX_BUF_PER_PAGE]; | |
508 | unsigned page_block; | |
509 | unsigned first_unmapped = blocks_per_page; | |
510 | struct block_device *bdev = NULL; | |
511 | int boundary = 0; | |
512 | sector_t boundary_block = 0; | |
513 | struct block_device *boundary_bdev = NULL; | |
514 | int length; | |
515 | struct buffer_head map_bh; | |
516 | loff_t i_size = i_size_read(inode); | |
0ea97180 | 517 | int ret = 0; |
5948edbc | 518 | int wr = (wbc->sync_mode == WB_SYNC_ALL ? WRITE_SYNC : WRITE); |
1da177e4 LT |
519 | |
520 | if (page_has_buffers(page)) { | |
521 | struct buffer_head *head = page_buffers(page); | |
522 | struct buffer_head *bh = head; | |
523 | ||
524 | /* If they're all mapped and dirty, do it */ | |
525 | page_block = 0; | |
526 | do { | |
527 | BUG_ON(buffer_locked(bh)); | |
528 | if (!buffer_mapped(bh)) { | |
529 | /* | |
530 | * unmapped dirty buffers are created by | |
531 | * __set_page_dirty_buffers -> mmapped data | |
532 | */ | |
533 | if (buffer_dirty(bh)) | |
534 | goto confused; | |
535 | if (first_unmapped == blocks_per_page) | |
536 | first_unmapped = page_block; | |
537 | continue; | |
538 | } | |
539 | ||
540 | if (first_unmapped != blocks_per_page) | |
541 | goto confused; /* hole -> non-hole */ | |
542 | ||
543 | if (!buffer_dirty(bh) || !buffer_uptodate(bh)) | |
544 | goto confused; | |
545 | if (page_block) { | |
546 | if (bh->b_blocknr != blocks[page_block-1] + 1) | |
547 | goto confused; | |
548 | } | |
549 | blocks[page_block++] = bh->b_blocknr; | |
550 | boundary = buffer_boundary(bh); | |
551 | if (boundary) { | |
552 | boundary_block = bh->b_blocknr; | |
553 | boundary_bdev = bh->b_bdev; | |
554 | } | |
555 | bdev = bh->b_bdev; | |
556 | } while ((bh = bh->b_this_page) != head); | |
557 | ||
558 | if (first_unmapped) | |
559 | goto page_is_mapped; | |
560 | ||
561 | /* | |
562 | * Page has buffers, but they are all unmapped. The page was | |
563 | * created by pagein or read over a hole which was handled by | |
564 | * block_read_full_page(). If this address_space is also | |
565 | * using mpage_readpages then this can rarely happen. | |
566 | */ | |
567 | goto confused; | |
568 | } | |
569 | ||
570 | /* | |
571 | * The page has no buffers: map it to disk | |
572 | */ | |
573 | BUG_ON(!PageUptodate(page)); | |
54b21a79 | 574 | block_in_file = (sector_t)page->index << (PAGE_CACHE_SHIFT - blkbits); |
1da177e4 LT |
575 | last_block = (i_size - 1) >> blkbits; |
576 | map_bh.b_page = page; | |
577 | for (page_block = 0; page_block < blocks_per_page; ) { | |
578 | ||
579 | map_bh.b_state = 0; | |
b0cf2321 | 580 | map_bh.b_size = 1 << blkbits; |
0ea97180 | 581 | if (mpd->get_block(inode, block_in_file, &map_bh, 1)) |
1da177e4 LT |
582 | goto confused; |
583 | if (buffer_new(&map_bh)) | |
584 | unmap_underlying_metadata(map_bh.b_bdev, | |
585 | map_bh.b_blocknr); | |
586 | if (buffer_boundary(&map_bh)) { | |
587 | boundary_block = map_bh.b_blocknr; | |
588 | boundary_bdev = map_bh.b_bdev; | |
589 | } | |
590 | if (page_block) { | |
591 | if (map_bh.b_blocknr != blocks[page_block-1] + 1) | |
592 | goto confused; | |
593 | } | |
594 | blocks[page_block++] = map_bh.b_blocknr; | |
595 | boundary = buffer_boundary(&map_bh); | |
596 | bdev = map_bh.b_bdev; | |
597 | if (block_in_file == last_block) | |
598 | break; | |
599 | block_in_file++; | |
600 | } | |
601 | BUG_ON(page_block == 0); | |
602 | ||
603 | first_unmapped = page_block; | |
604 | ||
605 | page_is_mapped: | |
606 | end_index = i_size >> PAGE_CACHE_SHIFT; | |
607 | if (page->index >= end_index) { | |
608 | /* | |
609 | * The page straddles i_size. It must be zeroed out on each | |
2a61aa40 | 610 | * and every writepage invocation because it may be mmapped. |
1da177e4 LT |
611 | * "A file is mapped in multiples of the page size. For a file |
612 | * that is not a multiple of the page size, the remaining memory | |
613 | * is zeroed when mapped, and writes to that region are not | |
614 | * written out to the file." | |
615 | */ | |
616 | unsigned offset = i_size & (PAGE_CACHE_SIZE - 1); | |
1da177e4 LT |
617 | |
618 | if (page->index > end_index || !offset) | |
619 | goto confused; | |
eebd2aa3 | 620 | zero_user_segment(page, offset, PAGE_CACHE_SIZE); |
1da177e4 LT |
621 | } |
622 | ||
623 | /* | |
624 | * This page will go to BIO. Do we need to send this BIO off first? | |
625 | */ | |
0ea97180 | 626 | if (bio && mpd->last_block_in_bio != blocks[0] - 1) |
5948edbc | 627 | bio = mpage_bio_submit(wr, bio); |
1da177e4 LT |
628 | |
629 | alloc_new: | |
630 | if (bio == NULL) { | |
47a191fd MW |
631 | if (first_unmapped == blocks_per_page) { |
632 | if (!bdev_write_page(bdev, blocks[0] << (blkbits - 9), | |
633 | page, wbc)) { | |
634 | clean_buffers(page, first_unmapped); | |
635 | goto out; | |
636 | } | |
637 | } | |
1da177e4 | 638 | bio = mpage_alloc(bdev, blocks[0] << (blkbits - 9), |
b54ffb73 | 639 | BIO_MAX_PAGES, GFP_NOFS|__GFP_HIGH); |
1da177e4 LT |
640 | if (bio == NULL) |
641 | goto confused; | |
429b3fb0 | 642 | |
b16b1deb | 643 | wbc_init_bio(wbc, bio); |
1da177e4 LT |
644 | } |
645 | ||
646 | /* | |
647 | * Must try to add the page before marking the buffer clean or | |
648 | * the confused fail path above (OOM) will be very confused when | |
649 | * it finds all bh marked clean (i.e. it will not write anything) | |
650 | */ | |
2a814908 | 651 | wbc_account_io(wbc, page, PAGE_SIZE); |
1da177e4 LT |
652 | length = first_unmapped << blkbits; |
653 | if (bio_add_page(bio, page, length, 0) < length) { | |
5948edbc | 654 | bio = mpage_bio_submit(wr, bio); |
1da177e4 LT |
655 | goto alloc_new; |
656 | } | |
657 | ||
90768eee | 658 | clean_buffers(page, first_unmapped); |
1da177e4 LT |
659 | |
660 | BUG_ON(PageWriteback(page)); | |
661 | set_page_writeback(page); | |
662 | unlock_page(page); | |
663 | if (boundary || (first_unmapped != blocks_per_page)) { | |
5948edbc | 664 | bio = mpage_bio_submit(wr, bio); |
1da177e4 LT |
665 | if (boundary_block) { |
666 | write_boundary_block(boundary_bdev, | |
667 | boundary_block, 1 << blkbits); | |
668 | } | |
669 | } else { | |
0ea97180 | 670 | mpd->last_block_in_bio = blocks[blocks_per_page - 1]; |
1da177e4 LT |
671 | } |
672 | goto out; | |
673 | ||
674 | confused: | |
675 | if (bio) | |
5948edbc | 676 | bio = mpage_bio_submit(wr, bio); |
1da177e4 | 677 | |
0ea97180 MS |
678 | if (mpd->use_writepage) { |
679 | ret = mapping->a_ops->writepage(page, wbc); | |
1da177e4 | 680 | } else { |
0ea97180 | 681 | ret = -EAGAIN; |
1da177e4 LT |
682 | goto out; |
683 | } | |
684 | /* | |
685 | * The caller has a ref on the inode, so *mapping is stable | |
686 | */ | |
0ea97180 | 687 | mapping_set_error(mapping, ret); |
1da177e4 | 688 | out: |
0ea97180 MS |
689 | mpd->bio = bio; |
690 | return ret; | |
1da177e4 LT |
691 | } |
692 | ||
693 | /** | |
78a4a50a | 694 | * mpage_writepages - walk the list of dirty pages of the given address space & writepage() all of them |
1da177e4 LT |
695 | * @mapping: address space structure to write |
696 | * @wbc: subtract the number of written pages from *@wbc->nr_to_write | |
697 | * @get_block: the filesystem's block mapper function. | |
698 | * If this is NULL then use a_ops->writepage. Otherwise, go | |
699 | * direct-to-BIO. | |
700 | * | |
701 | * This is a library function, which implements the writepages() | |
702 | * address_space_operation. | |
703 | * | |
704 | * If a page is already under I/O, generic_writepages() skips it, even | |
705 | * if it's dirty. This is desirable behaviour for memory-cleaning writeback, | |
706 | * but it is INCORRECT for data-integrity system calls such as fsync(). fsync() | |
707 | * and msync() need to guarantee that all the data which was dirty at the time | |
708 | * the call was made get new I/O started against them. If wbc->sync_mode is | |
709 | * WB_SYNC_ALL then we were called for data integrity and we must wait for | |
710 | * existing IO to complete. | |
711 | */ | |
712 | int | |
713 | mpage_writepages(struct address_space *mapping, | |
714 | struct writeback_control *wbc, get_block_t get_block) | |
1da177e4 | 715 | { |
2ed1a6bc | 716 | struct blk_plug plug; |
0ea97180 MS |
717 | int ret; |
718 | ||
2ed1a6bc JA |
719 | blk_start_plug(&plug); |
720 | ||
0ea97180 MS |
721 | if (!get_block) |
722 | ret = generic_writepages(mapping, wbc); | |
723 | else { | |
724 | struct mpage_data mpd = { | |
725 | .bio = NULL, | |
726 | .last_block_in_bio = 0, | |
727 | .get_block = get_block, | |
728 | .use_writepage = 1, | |
729 | }; | |
730 | ||
731 | ret = write_cache_pages(mapping, wbc, __mpage_writepage, &mpd); | |
5948edbc RP |
732 | if (mpd.bio) { |
733 | int wr = (wbc->sync_mode == WB_SYNC_ALL ? | |
734 | WRITE_SYNC : WRITE); | |
735 | mpage_bio_submit(wr, mpd.bio); | |
736 | } | |
1da177e4 | 737 | } |
2ed1a6bc | 738 | blk_finish_plug(&plug); |
1da177e4 LT |
739 | return ret; |
740 | } | |
741 | EXPORT_SYMBOL(mpage_writepages); | |
1da177e4 LT |
742 | |
743 | int mpage_writepage(struct page *page, get_block_t get_block, | |
744 | struct writeback_control *wbc) | |
745 | { | |
0ea97180 MS |
746 | struct mpage_data mpd = { |
747 | .bio = NULL, | |
748 | .last_block_in_bio = 0, | |
749 | .get_block = get_block, | |
750 | .use_writepage = 0, | |
751 | }; | |
752 | int ret = __mpage_writepage(page, wbc, &mpd); | |
5948edbc RP |
753 | if (mpd.bio) { |
754 | int wr = (wbc->sync_mode == WB_SYNC_ALL ? | |
755 | WRITE_SYNC : WRITE); | |
756 | mpage_bio_submit(wr, mpd.bio); | |
757 | } | |
1da177e4 LT |
758 | return ret; |
759 | } | |
760 | EXPORT_SYMBOL(mpage_writepage); |