blob: 2bb117dc508a77b25d46d623f91638ddbaa4c173 [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>
Andrew Morton02c43632016-03-15 14:55:15 -070027#include <linux/mm_inline.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/writeback.h>
29#include <linux/backing-dev.h>
30#include <linux/pagevec.h>
Dan Magenheimerc515e1f2011-05-26 10:01:43 -060031#include <linux/cleancache.h>
Akinobu Mita4db96b72014-10-09 15:26:55 -070032#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Mohan Srinivasandd85f782016-09-19 17:33:50 -070034#define CREATE_TRACE_POINTS
35#include <trace/events/android_fs.h>
36
37EXPORT_TRACEPOINT_SYMBOL(android_fs_datawrite_start);
38EXPORT_TRACEPOINT_SYMBOL(android_fs_datawrite_end);
39EXPORT_TRACEPOINT_SYMBOL(android_fs_dataread_start);
40EXPORT_TRACEPOINT_SYMBOL(android_fs_dataread_end);
41
Linus Torvalds1da177e2005-04-16 15:20:36 -070042/*
43 * I/O completion handler for multipage BIOs.
44 *
45 * The mpage code never puts partial pages into a BIO (except for end-of-file).
46 * If a page does not map to a contiguous run of blocks then it simply falls
47 * back to block_read_full_page().
48 *
49 * Why is this? If a page's completion depends on a number of different BIOs
50 * which can complete in any order (or at the same time) then determining the
51 * status of that page is hard. See end_buffer_async_read() for the details.
52 * There is no point in duplicating all that complexity.
53 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +020054static void mpage_end_io(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -070055{
Kent Overstreet2c30c712013-11-07 12:20:26 -080056 struct bio_vec *bv;
57 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Mohan Srinivasandd85f782016-09-19 17:33:50 -070059 if (trace_android_fs_dataread_end_enabled() &&
60 (bio_data_dir(bio) == READ)) {
61 struct page *first_page = bio->bi_io_vec[0].bv_page;
62
63 if (first_page != NULL)
64 trace_android_fs_dataread_end(first_page->mapping->host,
65 page_offset(first_page),
66 bio->bi_iter.bi_size);
67 }
68
Kent Overstreet2c30c712013-11-07 12:20:26 -080069 bio_for_each_segment_all(bv, bio, i) {
70 struct page *page = bv->bv_page;
Jens Axboec11f0c02016-08-05 08:11:04 -060071 page_endio(page, op_is_write(bio_op(bio)), bio->bi_error);
Kent Overstreet2c30c712013-11-07 12:20:26 -080072 }
73
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075}
76
Mike Christieeed25cd2016-06-05 14:31:59 -050077static struct bio *mpage_bio_submit(int op, int op_flags, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -070078{
Amit Pundir66aa71c2016-10-04 03:12:20 +053079 if (trace_android_fs_dataread_start_enabled() && (op == REQ_OP_READ)) {
Mohan Srinivasandd85f782016-09-19 17:33:50 -070080 struct page *first_page = bio->bi_io_vec[0].bv_page;
81
82 if (first_page != NULL) {
83 trace_android_fs_dataread_start(
84 first_page->mapping->host,
85 page_offset(first_page),
86 bio->bi_iter.bi_size,
87 current->pid,
88 current->comm);
89 }
90 }
Hai Shanc32b0d42011-01-13 15:45:51 -080091 bio->bi_end_io = mpage_end_io;
Mike Christieeed25cd2016-06-05 14:31:59 -050092 bio_set_op_attrs(bio, op, op_flags);
93 guard_bio_eod(op, bio);
Mike Christie4e49ea42016-06-05 14:31:41 -050094 submit_bio(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 return NULL;
96}
97
98static struct bio *
99mpage_alloc(struct block_device *bdev,
100 sector_t first_sector, int nr_vecs,
Al Virodd0fc662005-10-07 07:46:04 +0100101 gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102{
103 struct bio *bio;
104
Michal Hocko8a5c7432016-07-26 15:24:53 -0700105 /* Restrict the given (page cache) mask for slab allocations */
106 gfp_flags &= GFP_KERNEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 bio = bio_alloc(gfp_flags, nr_vecs);
108
109 if (bio == NULL && (current->flags & PF_MEMALLOC)) {
110 while (!bio && (nr_vecs /= 2))
111 bio = bio_alloc(gfp_flags, nr_vecs);
112 }
113
114 if (bio) {
115 bio->bi_bdev = bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700116 bio->bi_iter.bi_sector = first_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 }
118 return bio;
119}
120
121/*
122 * support function for mpage_readpages. The fs supplied get_block might
123 * return an up to date buffer. This is used to map that buffer into
124 * the page, which allows readpage to avoid triggering a duplicate call
125 * to get_block.
126 *
127 * The idea is to avoid adding buffers to pages that don't already have
128 * them. So when the buffer is up to date and the page size == block size,
129 * this marks the page up to date instead of adding new buffers.
130 */
131static void
132map_buffer_to_page(struct page *page, struct buffer_head *bh, int page_block)
133{
134 struct inode *inode = page->mapping->host;
135 struct buffer_head *page_bh, *head;
136 int block = 0;
137
138 if (!page_has_buffers(page)) {
139 /*
140 * don't make any buffers if there is only one buffer on
141 * the page and the page just needs to be set up to date
142 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300143 if (inode->i_blkbits == PAGE_SHIFT &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 buffer_uptodate(bh)) {
145 SetPageUptodate(page);
146 return;
147 }
148 create_empty_buffers(page, 1 << inode->i_blkbits, 0);
149 }
150 head = page_buffers(page);
151 page_bh = head;
152 do {
153 if (block == page_block) {
154 page_bh->b_state = bh->b_state;
155 page_bh->b_bdev = bh->b_bdev;
156 page_bh->b_blocknr = bh->b_blocknr;
157 break;
158 }
159 page_bh = page_bh->b_this_page;
160 block++;
161 } while (page_bh != head);
162}
163
Badari Pulavartyfa30bd02006-03-26 01:38:01 -0800164/*
165 * This is the worker routine which does all the work of mapping the disk
166 * blocks and constructs largest possible bios, submits them for IO if the
167 * blocks are not contiguous on the disk.
168 *
169 * We pass a buffer_head back and forth and use its buffer_mapped() flag to
170 * represent the validity of its disk mapping and to decide when to do the next
171 * get_block() call.
172 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173static struct bio *
174do_mpage_readpage(struct bio *bio, struct page *page, unsigned nr_pages,
Badari Pulavartyfa30bd02006-03-26 01:38:01 -0800175 sector_t *last_block_in_bio, struct buffer_head *map_bh,
Michal Hocko063d99b2015-10-15 15:28:24 -0700176 unsigned long *first_logical_block, get_block_t get_block,
177 gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{
179 struct inode *inode = page->mapping->host;
180 const unsigned blkbits = inode->i_blkbits;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300181 const unsigned blocks_per_page = PAGE_SIZE >> blkbits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 const unsigned blocksize = 1 << blkbits;
183 sector_t block_in_file;
184 sector_t last_block;
Badari Pulavartyfa30bd02006-03-26 01:38:01 -0800185 sector_t last_block_in_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 sector_t blocks[MAX_BUF_PER_PAGE];
187 unsigned page_block;
188 unsigned first_hole = blocks_per_page;
189 struct block_device *bdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 int length;
191 int fully_mapped = 1;
Badari Pulavartyfa30bd02006-03-26 01:38:01 -0800192 unsigned nblocks;
193 unsigned relative_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
195 if (page_has_buffers(page))
196 goto confused;
197
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300198 block_in_file = (sector_t)page->index << (PAGE_SHIFT - blkbits);
Badari Pulavartyfa30bd02006-03-26 01:38:01 -0800199 last_block = block_in_file + nr_pages * blocks_per_page;
200 last_block_in_file = (i_size_read(inode) + blocksize - 1) >> blkbits;
201 if (last_block > last_block_in_file)
202 last_block = last_block_in_file;
203 page_block = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Badari Pulavartyfa30bd02006-03-26 01:38:01 -0800205 /*
206 * Map blocks using the result from the previous get_blocks call first.
207 */
208 nblocks = map_bh->b_size >> blkbits;
209 if (buffer_mapped(map_bh) && block_in_file > *first_logical_block &&
210 block_in_file < (*first_logical_block + nblocks)) {
211 unsigned map_offset = block_in_file - *first_logical_block;
212 unsigned last = nblocks - map_offset;
213
214 for (relative_block = 0; ; relative_block++) {
215 if (relative_block == last) {
216 clear_buffer_mapped(map_bh);
217 break;
218 }
219 if (page_block == blocks_per_page)
220 break;
221 blocks[page_block] = map_bh->b_blocknr + map_offset +
222 relative_block;
223 page_block++;
224 block_in_file++;
225 }
226 bdev = map_bh->b_bdev;
227 }
228
229 /*
230 * Then do more get_blocks calls until we are done with this page.
231 */
232 map_bh->b_page = page;
233 while (page_block < blocks_per_page) {
234 map_bh->b_state = 0;
235 map_bh->b_size = 0;
236
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 if (block_in_file < last_block) {
Badari Pulavartyfa30bd02006-03-26 01:38:01 -0800238 map_bh->b_size = (last_block-block_in_file) << blkbits;
239 if (get_block(inode, block_in_file, map_bh, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 goto confused;
Badari Pulavartyfa30bd02006-03-26 01:38:01 -0800241 *first_logical_block = block_in_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 }
243
Badari Pulavartyfa30bd02006-03-26 01:38:01 -0800244 if (!buffer_mapped(map_bh)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 fully_mapped = 0;
246 if (first_hole == blocks_per_page)
247 first_hole = page_block;
Badari Pulavartyfa30bd02006-03-26 01:38:01 -0800248 page_block++;
249 block_in_file++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 continue;
251 }
252
253 /* some filesystems will copy data into the page during
254 * the get_block call, in which case we don't want to
255 * read it again. map_buffer_to_page copies the data
256 * we just collected from get_block into the page's buffers
257 * so readpage doesn't have to repeat the get_block call
258 */
Badari Pulavartyfa30bd02006-03-26 01:38:01 -0800259 if (buffer_uptodate(map_bh)) {
260 map_buffer_to_page(page, map_bh, page_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 goto confused;
262 }
263
264 if (first_hole != blocks_per_page)
265 goto confused; /* hole -> non-hole */
266
267 /* Contiguous blocks? */
Badari Pulavartyfa30bd02006-03-26 01:38:01 -0800268 if (page_block && blocks[page_block-1] != map_bh->b_blocknr-1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 goto confused;
Badari Pulavartyfa30bd02006-03-26 01:38:01 -0800270 nblocks = map_bh->b_size >> blkbits;
271 for (relative_block = 0; ; relative_block++) {
272 if (relative_block == nblocks) {
273 clear_buffer_mapped(map_bh);
274 break;
275 } else if (page_block == blocks_per_page)
276 break;
277 blocks[page_block] = map_bh->b_blocknr+relative_block;
278 page_block++;
279 block_in_file++;
280 }
281 bdev = map_bh->b_bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 }
283
284 if (first_hole != blocks_per_page) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300285 zero_user_segment(page, first_hole << blkbits, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 if (first_hole == 0) {
287 SetPageUptodate(page);
288 unlock_page(page);
289 goto out;
290 }
291 } else if (fully_mapped) {
292 SetPageMappedToDisk(page);
293 }
294
Dan Magenheimerc515e1f2011-05-26 10:01:43 -0600295 if (fully_mapped && blocks_per_page == 1 && !PageUptodate(page) &&
296 cleancache_get_page(page) == 0) {
297 SetPageUptodate(page);
298 goto confused;
299 }
300
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 /*
302 * This page will go to BIO. Do we need to send this BIO off first?
303 */
304 if (bio && (*last_block_in_bio != blocks[0] - 1))
Mike Christieeed25cd2016-06-05 14:31:59 -0500305 bio = mpage_bio_submit(REQ_OP_READ, 0, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
307alloc_new:
308 if (bio == NULL) {
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700309 if (first_hole == blocks_per_page) {
310 if (!bdev_read_page(bdev, blocks[0] << (blkbits - 9),
311 page))
312 goto out;
313 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 bio = mpage_alloc(bdev, blocks[0] << (blkbits - 9),
Michal Hocko063d99b2015-10-15 15:28:24 -0700315 min_t(int, nr_pages, BIO_MAX_PAGES), gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 if (bio == NULL)
317 goto confused;
318 }
319
320 length = first_hole << blkbits;
321 if (bio_add_page(bio, page, length, 0) < length) {
Mike Christieeed25cd2016-06-05 14:31:59 -0500322 bio = mpage_bio_submit(REQ_OP_READ, 0, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 goto alloc_new;
324 }
325
Miquel van Smoorenburg38c8e612009-01-06 14:39:02 -0800326 relative_block = block_in_file - *first_logical_block;
327 nblocks = map_bh->b_size >> blkbits;
328 if ((buffer_boundary(map_bh) && relative_block == nblocks) ||
329 (first_hole != blocks_per_page))
Mike Christieeed25cd2016-06-05 14:31:59 -0500330 bio = mpage_bio_submit(REQ_OP_READ, 0, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 else
332 *last_block_in_bio = blocks[blocks_per_page - 1];
333out:
334 return bio;
335
336confused:
337 if (bio)
Mike Christieeed25cd2016-06-05 14:31:59 -0500338 bio = mpage_bio_submit(REQ_OP_READ, 0, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 if (!PageUptodate(page))
340 block_read_full_page(page, get_block);
341 else
342 unlock_page(page);
343 goto out;
344}
345
Martin Waitz67be2dd2005-05-01 08:59:26 -0700346/**
Randy Dunlap78a4a502008-02-29 22:02:31 -0800347 * mpage_readpages - populate an address space with some pages & start reads against them
Martin Waitz67be2dd2005-05-01 08:59:26 -0700348 * @mapping: the address_space
349 * @pages: The address of a list_head which contains the target pages. These
350 * pages have their ->index populated and are otherwise uninitialised.
Martin Waitz67be2dd2005-05-01 08:59:26 -0700351 * The page at @pages->prev has the lowest file offset, and reads should be
352 * issued in @pages->prev to @pages->next order.
Martin Waitz67be2dd2005-05-01 08:59:26 -0700353 * @nr_pages: The number of pages at *@pages
354 * @get_block: The filesystem's block mapper function.
355 *
356 * This function walks the pages and the blocks within each page, building and
357 * emitting large BIOs.
358 *
359 * If anything unusual happens, such as:
360 *
361 * - encountering a page which has buffers
362 * - encountering a page which has a non-hole after a hole
363 * - encountering a page with non-contiguous blocks
364 *
365 * then this code just gives up and calls the buffer_head-based read function.
366 * It does handle a page which has holes at the end - that is a common case:
Kirill A. Shutemovea1754a2016-04-01 15:29:48 +0300367 * the end-of-file on blocksize < PAGE_SIZE setups.
Martin Waitz67be2dd2005-05-01 08:59:26 -0700368 *
369 * BH_Boundary explanation:
370 *
371 * There is a problem. The mpage read code assembles several pages, gets all
372 * their disk mappings, and then submits them all. That's fine, but obtaining
373 * the disk mappings may require I/O. Reads of indirect blocks, for example.
374 *
375 * So an mpage read of the first 16 blocks of an ext2 file will cause I/O to be
376 * submitted in the following order:
377 * 12 0 1 2 3 4 5 6 7 8 9 10 11 13 14 15 16
Randy Dunlap78a4a502008-02-29 22:02:31 -0800378 *
Martin Waitz67be2dd2005-05-01 08:59:26 -0700379 * because the indirect block has to be read to get the mappings of blocks
380 * 13,14,15,16. Obviously, this impacts performance.
381 *
382 * So what we do it to allow the filesystem's get_block() function to set
383 * BH_Boundary when it maps block 11. BH_Boundary says: mapping of the block
384 * after this one will require I/O against a block which is probably close to
385 * this one. So you should push what I/O you have currently accumulated.
386 *
387 * This all causes the disk requests to be issued in the correct order.
388 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389int
390mpage_readpages(struct address_space *mapping, struct list_head *pages,
391 unsigned nr_pages, get_block_t get_block)
392{
393 struct bio *bio = NULL;
394 unsigned page_idx;
395 sector_t last_block_in_bio = 0;
Badari Pulavartyfa30bd02006-03-26 01:38:01 -0800396 struct buffer_head map_bh;
397 unsigned long first_logical_block = 0;
Michal Hocko8a5c7432016-07-26 15:24:53 -0700398 gfp_t gfp = readahead_gfp_mask(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Aneesh Kumar K.V79ffab32009-05-13 15:13:42 -0400400 map_bh.b_state = 0;
401 map_bh.b_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
Andrew Morton02c43632016-03-15 14:55:15 -0700403 struct page *page = lru_to_page(pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
405 prefetchw(&page->flags);
406 list_del(&page->lru);
Nick Piggineb2be182007-10-16 01:24:57 -0700407 if (!add_to_page_cache_lru(page, mapping,
Michal Hocko063d99b2015-10-15 15:28:24 -0700408 page->index,
409 gfp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 bio = do_mpage_readpage(bio, page,
411 nr_pages - page_idx,
Badari Pulavartyfa30bd02006-03-26 01:38:01 -0800412 &last_block_in_bio, &map_bh,
413 &first_logical_block,
Michal Hocko063d99b2015-10-15 15:28:24 -0700414 get_block, gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300416 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 BUG_ON(!list_empty(pages));
419 if (bio)
Mike Christieeed25cd2016-06-05 14:31:59 -0500420 mpage_bio_submit(REQ_OP_READ, 0, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 return 0;
422}
423EXPORT_SYMBOL(mpage_readpages);
424
425/*
426 * This isn't called much at all
427 */
428int mpage_readpage(struct page *page, get_block_t get_block)
429{
430 struct bio *bio = NULL;
431 sector_t last_block_in_bio = 0;
Badari Pulavartyfa30bd02006-03-26 01:38:01 -0800432 struct buffer_head map_bh;
433 unsigned long first_logical_block = 0;
Michal Hockoc62d2552015-11-06 16:28:49 -0800434 gfp_t gfp = mapping_gfp_constraint(page->mapping, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
Aneesh Kumar K.V79ffab32009-05-13 15:13:42 -0400436 map_bh.b_state = 0;
437 map_bh.b_size = 0;
Badari Pulavartyfa30bd02006-03-26 01:38:01 -0800438 bio = do_mpage_readpage(bio, page, 1, &last_block_in_bio,
Michal Hocko063d99b2015-10-15 15:28:24 -0700439 &map_bh, &first_logical_block, get_block, gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 if (bio)
Mike Christieeed25cd2016-06-05 14:31:59 -0500441 mpage_bio_submit(REQ_OP_READ, 0, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 return 0;
443}
444EXPORT_SYMBOL(mpage_readpage);
445
446/*
447 * Writing is not so simple.
448 *
449 * If the page has buffers then they will be used for obtaining the disk
450 * mapping. We only support pages which are fully mapped-and-dirty, with a
451 * special case for pages which are unmapped at the end: end-of-file.
452 *
453 * If the page has no buffers (preferred) then the page is mapped here.
454 *
455 * If all blocks are found to be contiguous then the page can go into the
456 * BIO. Otherwise fall back to the mapping's writepage().
457 *
458 * FIXME: This code wants an estimate of how many pages are still to be
459 * written, so it can intelligently allocate a suitably-sized BIO. For now,
460 * just allocate full-size (16-page) BIOs.
461 */
Miklos Szeredi0ea97182007-05-10 22:22:51 -0700462
Dmitri Vorobievced117c2009-03-31 00:41:20 +0300463struct mpage_data {
464 struct bio *bio;
465 sector_t last_block_in_bio;
466 get_block_t *get_block;
467 unsigned use_writepage;
468};
469
Matthew Wilcox90768ee2014-06-04 16:07:44 -0700470/*
471 * We have our BIO, so we can now mark the buffers clean. Make
472 * sure to only clean buffers which we know we'll be writing.
473 */
474static void clean_buffers(struct page *page, unsigned first_unmapped)
475{
476 unsigned buffer_counter = 0;
477 struct buffer_head *bh, *head;
478 if (!page_has_buffers(page))
479 return;
480 head = page_buffers(page);
481 bh = head;
482
483 do {
484 if (buffer_counter++ == first_unmapped)
485 break;
486 clear_buffer_dirty(bh);
487 bh = bh->b_this_page;
488 } while (bh != head);
489
490 /*
491 * we cannot drop the bh if the page is not uptodate or a concurrent
492 * readpage would fail to serialize with the bh and it would read from
493 * disk before we reach the platter.
494 */
495 if (buffer_heads_over_limit && PageUptodate(page))
496 try_to_free_buffers(page);
497}
498
Dmitri Vorobievced117c2009-03-31 00:41:20 +0300499static int __mpage_writepage(struct page *page, struct writeback_control *wbc,
Alex Tomas29a814d2008-07-11 19:27:31 -0400500 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501{
Miklos Szeredi0ea97182007-05-10 22:22:51 -0700502 struct mpage_data *mpd = data;
503 struct bio *bio = mpd->bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 struct address_space *mapping = page->mapping;
505 struct inode *inode = page->mapping->host;
506 const unsigned blkbits = inode->i_blkbits;
507 unsigned long end_index;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300508 const unsigned blocks_per_page = PAGE_SIZE >> blkbits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 sector_t last_block;
510 sector_t block_in_file;
511 sector_t blocks[MAX_BUF_PER_PAGE];
512 unsigned page_block;
513 unsigned first_unmapped = blocks_per_page;
514 struct block_device *bdev = NULL;
515 int boundary = 0;
516 sector_t boundary_block = 0;
517 struct block_device *boundary_bdev = NULL;
518 int length;
519 struct buffer_head map_bh;
520 loff_t i_size = i_size_read(inode);
Miklos Szeredi0ea97182007-05-10 22:22:51 -0700521 int ret = 0;
Mike Christieeed25cd2016-06-05 14:31:59 -0500522 int op_flags = (wbc->sync_mode == WB_SYNC_ALL ? WRITE_SYNC : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
524 if (page_has_buffers(page)) {
525 struct buffer_head *head = page_buffers(page);
526 struct buffer_head *bh = head;
527
528 /* If they're all mapped and dirty, do it */
529 page_block = 0;
530 do {
531 BUG_ON(buffer_locked(bh));
532 if (!buffer_mapped(bh)) {
533 /*
534 * unmapped dirty buffers are created by
535 * __set_page_dirty_buffers -> mmapped data
536 */
537 if (buffer_dirty(bh))
538 goto confused;
539 if (first_unmapped == blocks_per_page)
540 first_unmapped = page_block;
541 continue;
542 }
543
544 if (first_unmapped != blocks_per_page)
545 goto confused; /* hole -> non-hole */
546
547 if (!buffer_dirty(bh) || !buffer_uptodate(bh))
548 goto confused;
549 if (page_block) {
550 if (bh->b_blocknr != blocks[page_block-1] + 1)
551 goto confused;
552 }
553 blocks[page_block++] = bh->b_blocknr;
554 boundary = buffer_boundary(bh);
555 if (boundary) {
556 boundary_block = bh->b_blocknr;
557 boundary_bdev = bh->b_bdev;
558 }
559 bdev = bh->b_bdev;
560 } while ((bh = bh->b_this_page) != head);
561
562 if (first_unmapped)
563 goto page_is_mapped;
564
565 /*
566 * Page has buffers, but they are all unmapped. The page was
567 * created by pagein or read over a hole which was handled by
568 * block_read_full_page(). If this address_space is also
569 * using mpage_readpages then this can rarely happen.
570 */
571 goto confused;
572 }
573
574 /*
575 * The page has no buffers: map it to disk
576 */
577 BUG_ON(!PageUptodate(page));
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300578 block_in_file = (sector_t)page->index << (PAGE_SHIFT - blkbits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 last_block = (i_size - 1) >> blkbits;
580 map_bh.b_page = page;
581 for (page_block = 0; page_block < blocks_per_page; ) {
582
583 map_bh.b_state = 0;
Badari Pulavartyb0cf2322006-03-26 01:38:00 -0800584 map_bh.b_size = 1 << blkbits;
Miklos Szeredi0ea97182007-05-10 22:22:51 -0700585 if (mpd->get_block(inode, block_in_file, &map_bh, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 goto confused;
587 if (buffer_new(&map_bh))
588 unmap_underlying_metadata(map_bh.b_bdev,
589 map_bh.b_blocknr);
590 if (buffer_boundary(&map_bh)) {
591 boundary_block = map_bh.b_blocknr;
592 boundary_bdev = map_bh.b_bdev;
593 }
594 if (page_block) {
595 if (map_bh.b_blocknr != blocks[page_block-1] + 1)
596 goto confused;
597 }
598 blocks[page_block++] = map_bh.b_blocknr;
599 boundary = buffer_boundary(&map_bh);
600 bdev = map_bh.b_bdev;
601 if (block_in_file == last_block)
602 break;
603 block_in_file++;
604 }
605 BUG_ON(page_block == 0);
606
607 first_unmapped = page_block;
608
609page_is_mapped:
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300610 end_index = i_size >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 if (page->index >= end_index) {
612 /*
613 * The page straddles i_size. It must be zeroed out on each
Adam Buchbinder2a61aa42009-12-11 16:35:40 -0500614 * and every writepage invocation because it may be mmapped.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 * "A file is mapped in multiples of the page size. For a file
616 * that is not a multiple of the page size, the remaining memory
617 * is zeroed when mapped, and writes to that region are not
618 * written out to the file."
619 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300620 unsigned offset = i_size & (PAGE_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
622 if (page->index > end_index || !offset)
623 goto confused;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300624 zero_user_segment(page, offset, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 }
626
627 /*
628 * This page will go to BIO. Do we need to send this BIO off first?
629 */
Miklos Szeredi0ea97182007-05-10 22:22:51 -0700630 if (bio && mpd->last_block_in_bio != blocks[0] - 1)
Mike Christieeed25cd2016-06-05 14:31:59 -0500631 bio = mpage_bio_submit(REQ_OP_WRITE, op_flags, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
633alloc_new:
634 if (bio == NULL) {
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700635 if (first_unmapped == blocks_per_page) {
636 if (!bdev_write_page(bdev, blocks[0] << (blkbits - 9),
637 page, wbc)) {
638 clean_buffers(page, first_unmapped);
639 goto out;
640 }
641 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 bio = mpage_alloc(bdev, blocks[0] << (blkbits - 9),
Kent Overstreetb54ffb72015-05-19 14:31:01 +0200643 BIO_MAX_PAGES, GFP_NOFS|__GFP_HIGH);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 if (bio == NULL)
645 goto confused;
Tejun Heo429b3fb2015-05-22 17:14:04 -0400646
Tejun Heob16b1de2015-06-02 08:39:48 -0600647 wbc_init_bio(wbc, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 }
649
650 /*
651 * Must try to add the page before marking the buffer clean or
652 * the confused fail path above (OOM) will be very confused when
653 * it finds all bh marked clean (i.e. it will not write anything)
654 */
Tejun Heo2a814902015-05-28 14:50:51 -0400655 wbc_account_io(wbc, page, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 length = first_unmapped << blkbits;
657 if (bio_add_page(bio, page, length, 0) < length) {
Mike Christieeed25cd2016-06-05 14:31:59 -0500658 bio = mpage_bio_submit(REQ_OP_WRITE, op_flags, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 goto alloc_new;
660 }
661
Matthew Wilcox90768ee2014-06-04 16:07:44 -0700662 clean_buffers(page, first_unmapped);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
664 BUG_ON(PageWriteback(page));
665 set_page_writeback(page);
666 unlock_page(page);
667 if (boundary || (first_unmapped != blocks_per_page)) {
Mike Christieeed25cd2016-06-05 14:31:59 -0500668 bio = mpage_bio_submit(REQ_OP_WRITE, op_flags, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 if (boundary_block) {
670 write_boundary_block(boundary_bdev,
671 boundary_block, 1 << blkbits);
672 }
673 } else {
Miklos Szeredi0ea97182007-05-10 22:22:51 -0700674 mpd->last_block_in_bio = blocks[blocks_per_page - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 }
676 goto out;
677
678confused:
679 if (bio)
Mike Christieeed25cd2016-06-05 14:31:59 -0500680 bio = mpage_bio_submit(REQ_OP_WRITE, op_flags, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
Miklos Szeredi0ea97182007-05-10 22:22:51 -0700682 if (mpd->use_writepage) {
683 ret = mapping->a_ops->writepage(page, wbc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 } else {
Miklos Szeredi0ea97182007-05-10 22:22:51 -0700685 ret = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 goto out;
687 }
688 /*
689 * The caller has a ref on the inode, so *mapping is stable
690 */
Miklos Szeredi0ea97182007-05-10 22:22:51 -0700691 mapping_set_error(mapping, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692out:
Miklos Szeredi0ea97182007-05-10 22:22:51 -0700693 mpd->bio = bio;
694 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695}
696
697/**
Randy Dunlap78a4a502008-02-29 22:02:31 -0800698 * mpage_writepages - walk the list of dirty pages of the given address space & writepage() all of them
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 * @mapping: address space structure to write
700 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
701 * @get_block: the filesystem's block mapper function.
702 * If this is NULL then use a_ops->writepage. Otherwise, go
703 * direct-to-BIO.
704 *
705 * This is a library function, which implements the writepages()
706 * address_space_operation.
707 *
708 * If a page is already under I/O, generic_writepages() skips it, even
709 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
710 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
711 * and msync() need to guarantee that all the data which was dirty at the time
712 * the call was made get new I/O started against them. If wbc->sync_mode is
713 * WB_SYNC_ALL then we were called for data integrity and we must wait for
714 * existing IO to complete.
715 */
716int
717mpage_writepages(struct address_space *mapping,
718 struct writeback_control *wbc, get_block_t get_block)
719{
Jens Axboe2ed1a6b2010-06-22 12:52:14 +0200720 struct blk_plug plug;
Miklos Szeredi0ea97182007-05-10 22:22:51 -0700721 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
Jens Axboe2ed1a6b2010-06-22 12:52:14 +0200723 blk_start_plug(&plug);
724
Miklos Szeredi0ea97182007-05-10 22:22:51 -0700725 if (!get_block)
726 ret = generic_writepages(mapping, wbc);
727 else {
728 struct mpage_data mpd = {
729 .bio = NULL,
730 .last_block_in_bio = 0,
731 .get_block = get_block,
732 .use_writepage = 1,
733 };
734
735 ret = write_cache_pages(mapping, wbc, __mpage_writepage, &mpd);
Roman Pen5948edb2015-09-15 08:27:25 -0600736 if (mpd.bio) {
Mike Christieeed25cd2016-06-05 14:31:59 -0500737 int op_flags = (wbc->sync_mode == WB_SYNC_ALL ?
738 WRITE_SYNC : 0);
739 mpage_bio_submit(REQ_OP_WRITE, op_flags, mpd.bio);
Roman Pen5948edb2015-09-15 08:27:25 -0600740 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 }
Jens Axboe2ed1a6b2010-06-22 12:52:14 +0200742 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 return ret;
744}
745EXPORT_SYMBOL(mpage_writepages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
747int mpage_writepage(struct page *page, get_block_t get_block,
748 struct writeback_control *wbc)
749{
Miklos Szeredi0ea97182007-05-10 22:22:51 -0700750 struct mpage_data mpd = {
751 .bio = NULL,
752 .last_block_in_bio = 0,
753 .get_block = get_block,
754 .use_writepage = 0,
755 };
756 int ret = __mpage_writepage(page, wbc, &mpd);
Roman Pen5948edb2015-09-15 08:27:25 -0600757 if (mpd.bio) {
Mike Christieeed25cd2016-06-05 14:31:59 -0500758 int op_flags = (wbc->sync_mode == WB_SYNC_ALL ?
759 WRITE_SYNC : 0);
760 mpage_bio_submit(REQ_OP_WRITE, op_flags, mpd.bio);
Roman Pen5948edb2015-09-15 08:27:25 -0600761 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 return ret;
763}
764EXPORT_SYMBOL(mpage_writepage);