blob: 10da0da730177672c18ca20862fba9ff4fffb0b6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 *
Francois Camie1f8e872008-10-15 22:01:59 -07009 * 15May2002 Andrew Morton
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 * 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>
Paul Gortmaker630d9c42011-11-16 23:57:37 -050016#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/mm.h>
18#include <linux/kdev_t.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#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>
Dan Magenheimerc515e1f2011-05-26 10:01:43 -060030#include <linux/cleancache.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32/*
33 * I/O completion handler for multipage BIOs.
34 *
35 * The mpage code never puts partial pages into a BIO (except for end-of-file).
36 * If a page does not map to a contiguous run of blocks then it simply falls
37 * back to block_read_full_page().
38 *
39 * Why is this? If a page's completion depends on a number of different BIOs
40 * which can complete in any order (or at the same time) then determining the
41 * status of that page is hard. See end_buffer_async_read() for the details.
42 * There is no point in duplicating all that complexity.
43 */
Hai Shanc32b0d42011-01-13 15:45:51 -080044static void mpage_end_io(struct bio *bio, int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045{
Kent Overstreet2c30c712013-11-07 12:20:26 -080046 struct bio_vec *bv;
47 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Kent Overstreet2c30c712013-11-07 12:20:26 -080049 bio_for_each_segment_all(bv, bio, i) {
50 struct page *page = bv->bv_page;
Matthew Wilcox57d99842014-06-04 16:07:45 -070051 page_endio(page, bio_data_dir(bio), err);
Kent Overstreet2c30c712013-11-07 12:20:26 -080052 }
53
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055}
56
Dmitri Vorobievced117c2009-03-31 00:41:20 +030057static struct bio *mpage_bio_submit(int rw, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058{
Hai Shanc32b0d42011-01-13 15:45:51 -080059 bio->bi_end_io = mpage_end_io;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 submit_bio(rw, bio);
61 return NULL;
62}
63
64static struct bio *
65mpage_alloc(struct block_device *bdev,
66 sector_t first_sector, int nr_vecs,
Al Virodd0fc662005-10-07 07:46:04 +010067 gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068{
69 struct bio *bio;
70
71 bio = bio_alloc(gfp_flags, nr_vecs);
72
73 if (bio == NULL && (current->flags & PF_MEMALLOC)) {
74 while (!bio && (nr_vecs /= 2))
75 bio = bio_alloc(gfp_flags, nr_vecs);
76 }
77
78 if (bio) {
79 bio->bi_bdev = bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -070080 bio->bi_iter.bi_sector = first_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 }
82 return bio;
83}
84
85/*
86 * support function for mpage_readpages. The fs supplied get_block might
87 * return an up to date buffer. This is used to map that buffer into
88 * the page, which allows readpage to avoid triggering a duplicate call
89 * to get_block.
90 *
91 * The idea is to avoid adding buffers to pages that don't already have
92 * them. So when the buffer is up to date and the page size == block size,
93 * this marks the page up to date instead of adding new buffers.
94 */
95static void
96map_buffer_to_page(struct page *page, struct buffer_head *bh, int page_block)
97{
98 struct inode *inode = page->mapping->host;
99 struct buffer_head *page_bh, *head;
100 int block = 0;
101
102 if (!page_has_buffers(page)) {
103 /*
104 * don't make any buffers if there is only one buffer on
105 * the page and the page just needs to be set up to date
106 */
107 if (inode->i_blkbits == PAGE_CACHE_SHIFT &&
108 buffer_uptodate(bh)) {
109 SetPageUptodate(page);
110 return;
111 }
112 create_empty_buffers(page, 1 << inode->i_blkbits, 0);
113 }
114 head = page_buffers(page);
115 page_bh = head;
116 do {
117 if (block == page_block) {
118 page_bh->b_state = bh->b_state;
119 page_bh->b_bdev = bh->b_bdev;
120 page_bh->b_blocknr = bh->b_blocknr;
121 break;
122 }
123 page_bh = page_bh->b_this_page;
124 block++;
125 } while (page_bh != head);
126}
127
Badari Pulavartyfa30bd02006-03-26 01:38:01 -0800128/*
129 * This is the worker routine which does all the work of mapping the disk
130 * blocks and constructs largest possible bios, submits them for IO if the
131 * blocks are not contiguous on the disk.
132 *
133 * We pass a buffer_head back and forth and use its buffer_mapped() flag to
134 * represent the validity of its disk mapping and to decide when to do the next
135 * get_block() call.
136 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137static struct bio *
138do_mpage_readpage(struct bio *bio, struct page *page, unsigned nr_pages,
Badari Pulavartyfa30bd02006-03-26 01:38:01 -0800139 sector_t *last_block_in_bio, struct buffer_head *map_bh,
140 unsigned long *first_logical_block, get_block_t get_block)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
142 struct inode *inode = page->mapping->host;
143 const unsigned blkbits = inode->i_blkbits;
144 const unsigned blocks_per_page = PAGE_CACHE_SIZE >> blkbits;
145 const unsigned blocksize = 1 << blkbits;
146 sector_t block_in_file;
147 sector_t last_block;
Badari Pulavartyfa30bd02006-03-26 01:38:01 -0800148 sector_t last_block_in_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 sector_t blocks[MAX_BUF_PER_PAGE];
150 unsigned page_block;
151 unsigned first_hole = blocks_per_page;
152 struct block_device *bdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 int length;
154 int fully_mapped = 1;
Badari Pulavartyfa30bd02006-03-26 01:38:01 -0800155 unsigned nblocks;
156 unsigned relative_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
158 if (page_has_buffers(page))
159 goto confused;
160
Andrew Morton54b21a72006-01-08 01:03:05 -0800161 block_in_file = (sector_t)page->index << (PAGE_CACHE_SHIFT - blkbits);
Badari Pulavartyfa30bd02006-03-26 01:38:01 -0800162 last_block = block_in_file + nr_pages * blocks_per_page;
163 last_block_in_file = (i_size_read(inode) + blocksize - 1) >> blkbits;
164 if (last_block > last_block_in_file)
165 last_block = last_block_in_file;
166 page_block = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Badari Pulavartyfa30bd02006-03-26 01:38:01 -0800168 /*
169 * Map blocks using the result from the previous get_blocks call first.
170 */
171 nblocks = map_bh->b_size >> blkbits;
172 if (buffer_mapped(map_bh) && block_in_file > *first_logical_block &&
173 block_in_file < (*first_logical_block + nblocks)) {
174 unsigned map_offset = block_in_file - *first_logical_block;
175 unsigned last = nblocks - map_offset;
176
177 for (relative_block = 0; ; relative_block++) {
178 if (relative_block == last) {
179 clear_buffer_mapped(map_bh);
180 break;
181 }
182 if (page_block == blocks_per_page)
183 break;
184 blocks[page_block] = map_bh->b_blocknr + map_offset +
185 relative_block;
186 page_block++;
187 block_in_file++;
188 }
189 bdev = map_bh->b_bdev;
190 }
191
192 /*
193 * Then do more get_blocks calls until we are done with this page.
194 */
195 map_bh->b_page = page;
196 while (page_block < blocks_per_page) {
197 map_bh->b_state = 0;
198 map_bh->b_size = 0;
199
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 if (block_in_file < last_block) {
Badari Pulavartyfa30bd02006-03-26 01:38:01 -0800201 map_bh->b_size = (last_block-block_in_file) << blkbits;
202 if (get_block(inode, block_in_file, map_bh, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 goto confused;
Badari Pulavartyfa30bd02006-03-26 01:38:01 -0800204 *first_logical_block = block_in_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 }
206
Badari Pulavartyfa30bd02006-03-26 01:38:01 -0800207 if (!buffer_mapped(map_bh)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 fully_mapped = 0;
209 if (first_hole == blocks_per_page)
210 first_hole = page_block;
Badari Pulavartyfa30bd02006-03-26 01:38:01 -0800211 page_block++;
212 block_in_file++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 continue;
214 }
215
216 /* some filesystems will copy data into the page during
217 * the get_block call, in which case we don't want to
218 * read it again. map_buffer_to_page copies the data
219 * we just collected from get_block into the page's buffers
220 * so readpage doesn't have to repeat the get_block call
221 */
Badari Pulavartyfa30bd02006-03-26 01:38:01 -0800222 if (buffer_uptodate(map_bh)) {
223 map_buffer_to_page(page, map_bh, page_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 goto confused;
225 }
226
227 if (first_hole != blocks_per_page)
228 goto confused; /* hole -> non-hole */
229
230 /* Contiguous blocks? */
Badari Pulavartyfa30bd02006-03-26 01:38:01 -0800231 if (page_block && blocks[page_block-1] != map_bh->b_blocknr-1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 goto confused;
Badari Pulavartyfa30bd02006-03-26 01:38:01 -0800233 nblocks = map_bh->b_size >> blkbits;
234 for (relative_block = 0; ; relative_block++) {
235 if (relative_block == nblocks) {
236 clear_buffer_mapped(map_bh);
237 break;
238 } else if (page_block == blocks_per_page)
239 break;
240 blocks[page_block] = map_bh->b_blocknr+relative_block;
241 page_block++;
242 block_in_file++;
243 }
244 bdev = map_bh->b_bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 }
246
247 if (first_hole != blocks_per_page) {
Christoph Lametereebd2aa2008-02-04 22:28:29 -0800248 zero_user_segment(page, first_hole << blkbits, PAGE_CACHE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 if (first_hole == 0) {
250 SetPageUptodate(page);
251 unlock_page(page);
252 goto out;
253 }
254 } else if (fully_mapped) {
255 SetPageMappedToDisk(page);
256 }
257
Dan Magenheimerc515e1f2011-05-26 10:01:43 -0600258 if (fully_mapped && blocks_per_page == 1 && !PageUptodate(page) &&
259 cleancache_get_page(page) == 0) {
260 SetPageUptodate(page);
261 goto confused;
262 }
263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 /*
265 * This page will go to BIO. Do we need to send this BIO off first?
266 */
267 if (bio && (*last_block_in_bio != blocks[0] - 1))
268 bio = mpage_bio_submit(READ, bio);
269
270alloc_new:
271 if (bio == NULL) {
272 bio = mpage_alloc(bdev, blocks[0] << (blkbits - 9),
273 min_t(int, nr_pages, bio_get_nr_vecs(bdev)),
274 GFP_KERNEL);
275 if (bio == NULL)
276 goto confused;
277 }
278
279 length = first_hole << blkbits;
280 if (bio_add_page(bio, page, length, 0) < length) {
281 bio = mpage_bio_submit(READ, bio);
282 goto alloc_new;
283 }
284
Miquel van Smoorenburg38c8e612009-01-06 14:39:02 -0800285 relative_block = block_in_file - *first_logical_block;
286 nblocks = map_bh->b_size >> blkbits;
287 if ((buffer_boundary(map_bh) && relative_block == nblocks) ||
288 (first_hole != blocks_per_page))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 bio = mpage_bio_submit(READ, bio);
290 else
291 *last_block_in_bio = blocks[blocks_per_page - 1];
292out:
293 return bio;
294
295confused:
296 if (bio)
297 bio = mpage_bio_submit(READ, bio);
298 if (!PageUptodate(page))
299 block_read_full_page(page, get_block);
300 else
301 unlock_page(page);
302 goto out;
303}
304
Martin Waitz67be2dd2005-05-01 08:59:26 -0700305/**
Randy Dunlap78a4a502008-02-29 22:02:31 -0800306 * mpage_readpages - populate an address space with some pages & start reads against them
Martin Waitz67be2dd2005-05-01 08:59:26 -0700307 * @mapping: the address_space
308 * @pages: The address of a list_head which contains the target pages. These
309 * pages have their ->index populated and are otherwise uninitialised.
Martin Waitz67be2dd2005-05-01 08:59:26 -0700310 * The page at @pages->prev has the lowest file offset, and reads should be
311 * issued in @pages->prev to @pages->next order.
Martin Waitz67be2dd2005-05-01 08:59:26 -0700312 * @nr_pages: The number of pages at *@pages
313 * @get_block: The filesystem's block mapper function.
314 *
315 * This function walks the pages and the blocks within each page, building and
316 * emitting large BIOs.
317 *
318 * If anything unusual happens, such as:
319 *
320 * - encountering a page which has buffers
321 * - encountering a page which has a non-hole after a hole
322 * - encountering a page with non-contiguous blocks
323 *
324 * then this code just gives up and calls the buffer_head-based read function.
325 * It does handle a page which has holes at the end - that is a common case:
326 * the end-of-file on blocksize < PAGE_CACHE_SIZE setups.
327 *
328 * BH_Boundary explanation:
329 *
330 * There is a problem. The mpage read code assembles several pages, gets all
331 * their disk mappings, and then submits them all. That's fine, but obtaining
332 * the disk mappings may require I/O. Reads of indirect blocks, for example.
333 *
334 * So an mpage read of the first 16 blocks of an ext2 file will cause I/O to be
335 * submitted in the following order:
336 * 12 0 1 2 3 4 5 6 7 8 9 10 11 13 14 15 16
Randy Dunlap78a4a502008-02-29 22:02:31 -0800337 *
Martin Waitz67be2dd2005-05-01 08:59:26 -0700338 * because the indirect block has to be read to get the mappings of blocks
339 * 13,14,15,16. Obviously, this impacts performance.
340 *
341 * So what we do it to allow the filesystem's get_block() function to set
342 * BH_Boundary when it maps block 11. BH_Boundary says: mapping of the block
343 * after this one will require I/O against a block which is probably close to
344 * this one. So you should push what I/O you have currently accumulated.
345 *
346 * This all causes the disk requests to be issued in the correct order.
347 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348int
349mpage_readpages(struct address_space *mapping, struct list_head *pages,
350 unsigned nr_pages, get_block_t get_block)
351{
352 struct bio *bio = NULL;
353 unsigned page_idx;
354 sector_t last_block_in_bio = 0;
Badari Pulavartyfa30bd02006-03-26 01:38:01 -0800355 struct buffer_head map_bh;
356 unsigned long first_logical_block = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
Aneesh Kumar K.V79ffab32009-05-13 15:13:42 -0400358 map_bh.b_state = 0;
359 map_bh.b_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
361 struct page *page = list_entry(pages->prev, struct page, lru);
362
363 prefetchw(&page->flags);
364 list_del(&page->lru);
Nick Piggineb2be182007-10-16 01:24:57 -0700365 if (!add_to_page_cache_lru(page, mapping,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 page->index, GFP_KERNEL)) {
367 bio = do_mpage_readpage(bio, page,
368 nr_pages - page_idx,
Badari Pulavartyfa30bd02006-03-26 01:38:01 -0800369 &last_block_in_bio, &map_bh,
370 &first_logical_block,
371 get_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 }
Nick Piggineb2be182007-10-16 01:24:57 -0700373 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 BUG_ON(!list_empty(pages));
376 if (bio)
377 mpage_bio_submit(READ, bio);
378 return 0;
379}
380EXPORT_SYMBOL(mpage_readpages);
381
382/*
383 * This isn't called much at all
384 */
385int mpage_readpage(struct page *page, get_block_t get_block)
386{
387 struct bio *bio = NULL;
388 sector_t last_block_in_bio = 0;
Badari Pulavartyfa30bd02006-03-26 01:38:01 -0800389 struct buffer_head map_bh;
390 unsigned long first_logical_block = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
Aneesh Kumar K.V79ffab32009-05-13 15:13:42 -0400392 map_bh.b_state = 0;
393 map_bh.b_size = 0;
Badari Pulavartyfa30bd02006-03-26 01:38:01 -0800394 bio = do_mpage_readpage(bio, page, 1, &last_block_in_bio,
395 &map_bh, &first_logical_block, get_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 if (bio)
397 mpage_bio_submit(READ, bio);
398 return 0;
399}
400EXPORT_SYMBOL(mpage_readpage);
401
402/*
403 * Writing is not so simple.
404 *
405 * If the page has buffers then they will be used for obtaining the disk
406 * mapping. We only support pages which are fully mapped-and-dirty, with a
407 * special case for pages which are unmapped at the end: end-of-file.
408 *
409 * If the page has no buffers (preferred) then the page is mapped here.
410 *
411 * If all blocks are found to be contiguous then the page can go into the
412 * BIO. Otherwise fall back to the mapping's writepage().
413 *
414 * FIXME: This code wants an estimate of how many pages are still to be
415 * written, so it can intelligently allocate a suitably-sized BIO. For now,
416 * just allocate full-size (16-page) BIOs.
417 */
Miklos Szeredi0ea97182007-05-10 22:22:51 -0700418
Dmitri Vorobievced117c2009-03-31 00:41:20 +0300419struct mpage_data {
420 struct bio *bio;
421 sector_t last_block_in_bio;
422 get_block_t *get_block;
423 unsigned use_writepage;
424};
425
Matthew Wilcox90768ee2014-06-04 16:07:44 -0700426/*
427 * We have our BIO, so we can now mark the buffers clean. Make
428 * sure to only clean buffers which we know we'll be writing.
429 */
430static void clean_buffers(struct page *page, unsigned first_unmapped)
431{
432 unsigned buffer_counter = 0;
433 struct buffer_head *bh, *head;
434 if (!page_has_buffers(page))
435 return;
436 head = page_buffers(page);
437 bh = head;
438
439 do {
440 if (buffer_counter++ == first_unmapped)
441 break;
442 clear_buffer_dirty(bh);
443 bh = bh->b_this_page;
444 } while (bh != head);
445
446 /*
447 * we cannot drop the bh if the page is not uptodate or a concurrent
448 * readpage would fail to serialize with the bh and it would read from
449 * disk before we reach the platter.
450 */
451 if (buffer_heads_over_limit && PageUptodate(page))
452 try_to_free_buffers(page);
453}
454
Dmitri Vorobievced117c2009-03-31 00:41:20 +0300455static int __mpage_writepage(struct page *page, struct writeback_control *wbc,
Alex Tomas29a814d2008-07-11 19:27:31 -0400456 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457{
Miklos Szeredi0ea97182007-05-10 22:22:51 -0700458 struct mpage_data *mpd = data;
459 struct bio *bio = mpd->bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 struct address_space *mapping = page->mapping;
461 struct inode *inode = page->mapping->host;
462 const unsigned blkbits = inode->i_blkbits;
463 unsigned long end_index;
464 const unsigned blocks_per_page = PAGE_CACHE_SIZE >> blkbits;
465 sector_t last_block;
466 sector_t block_in_file;
467 sector_t blocks[MAX_BUF_PER_PAGE];
468 unsigned page_block;
469 unsigned first_unmapped = blocks_per_page;
470 struct block_device *bdev = NULL;
471 int boundary = 0;
472 sector_t boundary_block = 0;
473 struct block_device *boundary_bdev = NULL;
474 int length;
475 struct buffer_head map_bh;
476 loff_t i_size = i_size_read(inode);
Miklos Szeredi0ea97182007-05-10 22:22:51 -0700477 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
479 if (page_has_buffers(page)) {
480 struct buffer_head *head = page_buffers(page);
481 struct buffer_head *bh = head;
482
483 /* If they're all mapped and dirty, do it */
484 page_block = 0;
485 do {
486 BUG_ON(buffer_locked(bh));
487 if (!buffer_mapped(bh)) {
488 /*
489 * unmapped dirty buffers are created by
490 * __set_page_dirty_buffers -> mmapped data
491 */
492 if (buffer_dirty(bh))
493 goto confused;
494 if (first_unmapped == blocks_per_page)
495 first_unmapped = page_block;
496 continue;
497 }
498
499 if (first_unmapped != blocks_per_page)
500 goto confused; /* hole -> non-hole */
501
502 if (!buffer_dirty(bh) || !buffer_uptodate(bh))
503 goto confused;
504 if (page_block) {
505 if (bh->b_blocknr != blocks[page_block-1] + 1)
506 goto confused;
507 }
508 blocks[page_block++] = bh->b_blocknr;
509 boundary = buffer_boundary(bh);
510 if (boundary) {
511 boundary_block = bh->b_blocknr;
512 boundary_bdev = bh->b_bdev;
513 }
514 bdev = bh->b_bdev;
515 } while ((bh = bh->b_this_page) != head);
516
517 if (first_unmapped)
518 goto page_is_mapped;
519
520 /*
521 * Page has buffers, but they are all unmapped. The page was
522 * created by pagein or read over a hole which was handled by
523 * block_read_full_page(). If this address_space is also
524 * using mpage_readpages then this can rarely happen.
525 */
526 goto confused;
527 }
528
529 /*
530 * The page has no buffers: map it to disk
531 */
532 BUG_ON(!PageUptodate(page));
Andrew Morton54b21a72006-01-08 01:03:05 -0800533 block_in_file = (sector_t)page->index << (PAGE_CACHE_SHIFT - blkbits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 last_block = (i_size - 1) >> blkbits;
535 map_bh.b_page = page;
536 for (page_block = 0; page_block < blocks_per_page; ) {
537
538 map_bh.b_state = 0;
Badari Pulavartyb0cf2322006-03-26 01:38:00 -0800539 map_bh.b_size = 1 << blkbits;
Miklos Szeredi0ea97182007-05-10 22:22:51 -0700540 if (mpd->get_block(inode, block_in_file, &map_bh, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 goto confused;
542 if (buffer_new(&map_bh))
543 unmap_underlying_metadata(map_bh.b_bdev,
544 map_bh.b_blocknr);
545 if (buffer_boundary(&map_bh)) {
546 boundary_block = map_bh.b_blocknr;
547 boundary_bdev = map_bh.b_bdev;
548 }
549 if (page_block) {
550 if (map_bh.b_blocknr != blocks[page_block-1] + 1)
551 goto confused;
552 }
553 blocks[page_block++] = map_bh.b_blocknr;
554 boundary = buffer_boundary(&map_bh);
555 bdev = map_bh.b_bdev;
556 if (block_in_file == last_block)
557 break;
558 block_in_file++;
559 }
560 BUG_ON(page_block == 0);
561
562 first_unmapped = page_block;
563
564page_is_mapped:
565 end_index = i_size >> PAGE_CACHE_SHIFT;
566 if (page->index >= end_index) {
567 /*
568 * The page straddles i_size. It must be zeroed out on each
Adam Buchbinder2a61aa42009-12-11 16:35:40 -0500569 * and every writepage invocation because it may be mmapped.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 * "A file is mapped in multiples of the page size. For a file
571 * that is not a multiple of the page size, the remaining memory
572 * is zeroed when mapped, and writes to that region are not
573 * written out to the file."
574 */
575 unsigned offset = i_size & (PAGE_CACHE_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
577 if (page->index > end_index || !offset)
578 goto confused;
Christoph Lametereebd2aa2008-02-04 22:28:29 -0800579 zero_user_segment(page, offset, PAGE_CACHE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 }
581
582 /*
583 * This page will go to BIO. Do we need to send this BIO off first?
584 */
Miklos Szeredi0ea97182007-05-10 22:22:51 -0700585 if (bio && mpd->last_block_in_bio != blocks[0] - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 bio = mpage_bio_submit(WRITE, bio);
587
588alloc_new:
589 if (bio == NULL) {
590 bio = mpage_alloc(bdev, blocks[0] << (blkbits - 9),
591 bio_get_nr_vecs(bdev), GFP_NOFS|__GFP_HIGH);
592 if (bio == NULL)
593 goto confused;
594 }
595
596 /*
597 * Must try to add the page before marking the buffer clean or
598 * the confused fail path above (OOM) will be very confused when
599 * it finds all bh marked clean (i.e. it will not write anything)
600 */
601 length = first_unmapped << blkbits;
602 if (bio_add_page(bio, page, length, 0) < length) {
603 bio = mpage_bio_submit(WRITE, bio);
604 goto alloc_new;
605 }
606
Matthew Wilcox90768ee2014-06-04 16:07:44 -0700607 clean_buffers(page, first_unmapped);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
609 BUG_ON(PageWriteback(page));
610 set_page_writeback(page);
611 unlock_page(page);
612 if (boundary || (first_unmapped != blocks_per_page)) {
613 bio = mpage_bio_submit(WRITE, bio);
614 if (boundary_block) {
615 write_boundary_block(boundary_bdev,
616 boundary_block, 1 << blkbits);
617 }
618 } else {
Miklos Szeredi0ea97182007-05-10 22:22:51 -0700619 mpd->last_block_in_bio = blocks[blocks_per_page - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 }
621 goto out;
622
623confused:
624 if (bio)
625 bio = mpage_bio_submit(WRITE, bio);
626
Miklos Szeredi0ea97182007-05-10 22:22:51 -0700627 if (mpd->use_writepage) {
628 ret = mapping->a_ops->writepage(page, wbc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 } else {
Miklos Szeredi0ea97182007-05-10 22:22:51 -0700630 ret = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 goto out;
632 }
633 /*
634 * The caller has a ref on the inode, so *mapping is stable
635 */
Miklos Szeredi0ea97182007-05-10 22:22:51 -0700636 mapping_set_error(mapping, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637out:
Miklos Szeredi0ea97182007-05-10 22:22:51 -0700638 mpd->bio = bio;
639 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640}
641
642/**
Randy Dunlap78a4a502008-02-29 22:02:31 -0800643 * mpage_writepages - walk the list of dirty pages of the given address space & writepage() all of them
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 * @mapping: address space structure to write
645 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
646 * @get_block: the filesystem's block mapper function.
647 * If this is NULL then use a_ops->writepage. Otherwise, go
648 * direct-to-BIO.
649 *
650 * This is a library function, which implements the writepages()
651 * address_space_operation.
652 *
653 * If a page is already under I/O, generic_writepages() skips it, even
654 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
655 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
656 * and msync() need to guarantee that all the data which was dirty at the time
657 * the call was made get new I/O started against them. If wbc->sync_mode is
658 * WB_SYNC_ALL then we were called for data integrity and we must wait for
659 * existing IO to complete.
660 */
661int
662mpage_writepages(struct address_space *mapping,
663 struct writeback_control *wbc, get_block_t get_block)
664{
Jens Axboe2ed1a6b2010-06-22 12:52:14 +0200665 struct blk_plug plug;
Miklos Szeredi0ea97182007-05-10 22:22:51 -0700666 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
Jens Axboe2ed1a6b2010-06-22 12:52:14 +0200668 blk_start_plug(&plug);
669
Miklos Szeredi0ea97182007-05-10 22:22:51 -0700670 if (!get_block)
671 ret = generic_writepages(mapping, wbc);
672 else {
673 struct mpage_data mpd = {
674 .bio = NULL,
675 .last_block_in_bio = 0,
676 .get_block = get_block,
677 .use_writepage = 1,
678 };
679
680 ret = write_cache_pages(mapping, wbc, __mpage_writepage, &mpd);
681 if (mpd.bio)
682 mpage_bio_submit(WRITE, mpd.bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 }
Jens Axboe2ed1a6b2010-06-22 12:52:14 +0200684 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 return ret;
686}
687EXPORT_SYMBOL(mpage_writepages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
689int mpage_writepage(struct page *page, get_block_t get_block,
690 struct writeback_control *wbc)
691{
Miklos Szeredi0ea97182007-05-10 22:22:51 -0700692 struct mpage_data mpd = {
693 .bio = NULL,
694 .last_block_in_bio = 0,
695 .get_block = get_block,
696 .use_writepage = 0,
697 };
698 int ret = __mpage_writepage(page, wbc, &mpd);
699 if (mpd.bio)
700 mpage_bio_submit(WRITE, mpd.bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 return ret;
702}
703EXPORT_SYMBOL(mpage_writepage);