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