1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * fs/mpage.c
4 *
5 * Copyright (C) 2002, Linus Torvalds.
6 *
7 * Contains functions related to preparing and submitting BIOs which contain
8 * multiple pagecache pages.
9 *
10 * 15May2002 Andrew Morton
11 * Initial version
12 * 27Jun2002 axboe@suse.de
13 * use bio_add_page() to build bio's just the right size
14 */
15
16 #include <linux/kernel.h>
17 #include <linux/export.h>
18 #include <linux/mm.h>
19 #include <linux/kdev_t.h>
20 #include <linux/gfp.h>
21 #include <linux/bio.h>
22 #include <linux/fs.h>
23 #include <linux/buffer_head.h>
24 #include <linux/blkdev.h>
25 #include <linux/highmem.h>
26 #include <linux/prefetch.h>
27 #include <linux/mpage.h>
28 #include <linux/mm_inline.h>
29 #include <linux/writeback.h>
30 #include <linux/backing-dev.h>
31 #include <linux/pagevec.h>
32 #include <linux/cleancache.h>
33 #include "internal.h"
34
35 /*
36 * I/O completion handler for multipage BIOs.
37 *
38 * The mpage code never puts partial pages into a BIO (except for end-of-file).
39 * If a page does not map to a contiguous run of blocks then it simply falls
40 * back to block_read_full_folio().
41 *
42 * Why is this? If a page's completion depends on a number of different BIOs
43 * which can complete in any order (or at the same time) then determining the
44 * status of that page is hard. See end_buffer_async_read() for the details.
45 * There is no point in duplicating all that complexity.
46 */
mpage_end_io(struct bio * bio)47 static void mpage_end_io(struct bio *bio)
48 {
49 struct bio_vec *bv;
50 struct bvec_iter_all iter_all;
51
52 bio_for_each_segment_all(bv, bio, iter_all) {
53 struct page *page = bv->bv_page;
54 page_endio(page, bio_op(bio),
55 blk_status_to_errno(bio->bi_status));
56 }
57
58 bio_put(bio);
59 }
60
mpage_bio_submit(struct bio * bio)61 static struct bio *mpage_bio_submit(struct bio *bio)
62 {
63 bio->bi_end_io = mpage_end_io;
64 guard_bio_eod(bio);
65 submit_bio(bio);
66 return NULL;
67 }
68
69 /*
70 * support function for mpage_readahead. The fs supplied get_block might
71 * return an up to date buffer. This is used to map that buffer into
72 * the page, which allows read_folio to avoid triggering a duplicate call
73 * to get_block.
74 *
75 * The idea is to avoid adding buffers to pages that don't already have
76 * them. So when the buffer is up to date and the page size == block size,
77 * this marks the page up to date instead of adding new buffers.
78 */
map_buffer_to_folio(struct folio * folio,struct buffer_head * bh,int page_block)79 static void map_buffer_to_folio(struct folio *folio, struct buffer_head *bh,
80 int page_block)
81 {
82 struct inode *inode = folio->mapping->host;
83 struct buffer_head *page_bh, *head;
84 int block = 0;
85
86 head = folio_buffers(folio);
87 if (!head) {
88 /*
89 * don't make any buffers if there is only one buffer on
90 * the folio and the folio just needs to be set up to date
91 */
92 if (inode->i_blkbits == PAGE_SHIFT &&
93 buffer_uptodate(bh)) {
94 folio_mark_uptodate(folio);
95 return;
96 }
97 create_empty_buffers(&folio->page, i_blocksize(inode), 0);
98 head = folio_buffers(folio);
99 }
100
101 page_bh = head;
102 do {
103 if (block == page_block) {
104 page_bh->b_state = bh->b_state;
105 page_bh->b_bdev = bh->b_bdev;
106 page_bh->b_blocknr = bh->b_blocknr;
107 break;
108 }
109 page_bh = page_bh->b_this_page;
110 block++;
111 } while (page_bh != head);
112 }
113
114 struct mpage_readpage_args {
115 struct bio *bio;
116 struct folio *folio;
117 unsigned int nr_pages;
118 bool is_readahead;
119 sector_t last_block_in_bio;
120 struct buffer_head map_bh;
121 unsigned long first_logical_block;
122 get_block_t *get_block;
123 };
124
125 /*
126 * This is the worker routine which does all the work of mapping the disk
127 * blocks and constructs largest possible bios, submits them for IO if the
128 * blocks are not contiguous on the disk.
129 *
130 * We pass a buffer_head back and forth and use its buffer_mapped() flag to
131 * represent the validity of its disk mapping and to decide when to do the next
132 * get_block() call.
133 */
do_mpage_readpage(struct mpage_readpage_args * args)134 static struct bio *do_mpage_readpage(struct mpage_readpage_args *args)
135 {
136 struct folio *folio = args->folio;
137 struct inode *inode = folio->mapping->host;
138 const unsigned blkbits = inode->i_blkbits;
139 const unsigned blocks_per_page = PAGE_SIZE >> blkbits;
140 const unsigned blocksize = 1 << blkbits;
141 struct buffer_head *map_bh = &args->map_bh;
142 sector_t block_in_file;
143 sector_t last_block;
144 sector_t last_block_in_file;
145 sector_t blocks[MAX_BUF_PER_PAGE];
146 unsigned page_block;
147 unsigned first_hole = blocks_per_page;
148 struct block_device *bdev = NULL;
149 int length;
150 int fully_mapped = 1;
151 blk_opf_t opf = REQ_OP_READ;
152 unsigned nblocks;
153 unsigned relative_block;
154 gfp_t gfp = mapping_gfp_constraint(folio->mapping, GFP_KERNEL);
155
156 /* MAX_BUF_PER_PAGE, for example */
157 VM_BUG_ON_FOLIO(folio_test_large(folio), folio);
158
159 if (args->is_readahead) {
160 opf |= REQ_RAHEAD;
161 gfp |= __GFP_NORETRY | __GFP_NOWARN;
162 }
163
164 if (folio_buffers(folio))
165 goto confused;
166
167 block_in_file = (sector_t)folio->index << (PAGE_SHIFT - blkbits);
168 last_block = block_in_file + args->nr_pages * blocks_per_page;
169 last_block_in_file = (i_size_read(inode) + blocksize - 1) >> blkbits;
170 if (last_block > last_block_in_file)
171 last_block = last_block_in_file;
172 page_block = 0;
173
174 /*
175 * Map blocks using the result from the previous get_blocks call first.
176 */
177 nblocks = map_bh->b_size >> blkbits;
178 if (buffer_mapped(map_bh) &&
179 block_in_file > args->first_logical_block &&
180 block_in_file < (args->first_logical_block + nblocks)) {
181 unsigned map_offset = block_in_file - args->first_logical_block;
182 unsigned last = nblocks - map_offset;
183
184 for (relative_block = 0; ; relative_block++) {
185 if (relative_block == last) {
186 clear_buffer_mapped(map_bh);
187 break;
188 }
189 if (page_block == blocks_per_page)
190 break;
191 blocks[page_block] = map_bh->b_blocknr + map_offset +
192 relative_block;
193 page_block++;
194 block_in_file++;
195 }
196 bdev = map_bh->b_bdev;
197 }
198
199 /*
200 * Then do more get_blocks calls until we are done with this folio.
201 */
202 map_bh->b_page = &folio->page;
203 while (page_block < blocks_per_page) {
204 map_bh->b_state = 0;
205 map_bh->b_size = 0;
206
207 if (block_in_file < last_block) {
208 map_bh->b_size = (last_block-block_in_file) << blkbits;
209 if (args->get_block(inode, block_in_file, map_bh, 0))
210 goto confused;
211 args->first_logical_block = block_in_file;
212 }
213
214 if (!buffer_mapped(map_bh)) {
215 fully_mapped = 0;
216 if (first_hole == blocks_per_page)
217 first_hole = page_block;
218 page_block++;
219 block_in_file++;
220 continue;
221 }
222
223 /* some filesystems will copy data into the page during
224 * the get_block call, in which case we don't want to
225 * read it again. map_buffer_to_folio copies the data
226 * we just collected from get_block into the folio's buffers
227 * so read_folio doesn't have to repeat the get_block call
228 */
229 if (buffer_uptodate(map_bh)) {
230 map_buffer_to_folio(folio, map_bh, page_block);
231 goto confused;
232 }
233
234 if (first_hole != blocks_per_page)
235 goto confused; /* hole -> non-hole */
236
237 /* Contiguous blocks? */
238 if (page_block && blocks[page_block-1] != map_bh->b_blocknr-1)
239 goto confused;
240 nblocks = map_bh->b_size >> blkbits;
241 for (relative_block = 0; ; relative_block++) {
242 if (relative_block == nblocks) {
243 clear_buffer_mapped(map_bh);
244 break;
245 } else if (page_block == blocks_per_page)
246 break;
247 blocks[page_block] = map_bh->b_blocknr+relative_block;
248 page_block++;
249 block_in_file++;
250 }
251 bdev = map_bh->b_bdev;
252 }
253
254 if (first_hole != blocks_per_page) {
255 folio_zero_segment(folio, first_hole << blkbits, PAGE_SIZE);
256 if (first_hole == 0) {
257 folio_mark_uptodate(folio);
258 folio_unlock(folio);
259 goto out;
260 }
261 } else if (fully_mapped) {
262 folio_set_mappedtodisk(folio);
263 }
264
265 if (fully_mapped && blocks_per_page == 1 && !folio_test_uptodate(folio) &&
266 cleancache_get_page(&folio->page) == 0) {
267 folio_mark_uptodate(folio);
268 goto confused;
269 }
270
271 /*
272 * This folio will go to BIO. Do we need to send this BIO off first?
273 */
274 if (args->bio && (args->last_block_in_bio != blocks[0] - 1))
275 args->bio = mpage_bio_submit(args->bio);
276
277 alloc_new:
278 if (args->bio == NULL) {
279 if (first_hole == blocks_per_page) {
280 if (!bdev_read_page(bdev, blocks[0] << (blkbits - 9),
281 &folio->page))
282 goto out;
283 }
284 args->bio = bio_alloc(bdev, bio_max_segs(args->nr_pages), opf,
285 gfp);
286 if (args->bio == NULL)
287 goto confused;
288 args->bio->bi_iter.bi_sector = blocks[0] << (blkbits - 9);
289 }
290
291 length = first_hole << blkbits;
292 if (!bio_add_folio(args->bio, folio, length, 0)) {
293 args->bio = mpage_bio_submit(args->bio);
294 goto alloc_new;
295 }
296
297 relative_block = block_in_file - args->first_logical_block;
298 nblocks = map_bh->b_size >> blkbits;
299 if ((buffer_boundary(map_bh) && relative_block == nblocks) ||
300 (first_hole != blocks_per_page))
301 args->bio = mpage_bio_submit(args->bio);
302 else
303 args->last_block_in_bio = blocks[blocks_per_page - 1];
304 out:
305 return args->bio;
306
307 confused:
308 if (args->bio)
309 args->bio = mpage_bio_submit(args->bio);
310 if (!folio_test_uptodate(folio))
311 block_read_full_folio(folio, args->get_block);
312 else
313 folio_unlock(folio);
314 goto out;
315 }
316
317 /**
318 * mpage_readahead - start reads against pages
319 * @rac: Describes which pages to read.
320 * @get_block: The filesystem's block mapper function.
321 *
322 * This function walks the pages and the blocks within each page, building and
323 * emitting large BIOs.
324 *
325 * If anything unusual happens, such as:
326 *
327 * - encountering a page which has buffers
328 * - encountering a page which has a non-hole after a hole
329 * - encountering a page with non-contiguous blocks
330 *
331 * then this code just gives up and calls the buffer_head-based read function.
332 * It does handle a page which has holes at the end - that is a common case:
333 * the end-of-file on blocksize < PAGE_SIZE setups.
334 *
335 * BH_Boundary explanation:
336 *
337 * There is a problem. The mpage read code assembles several pages, gets all
338 * their disk mappings, and then submits them all. That's fine, but obtaining
339 * the disk mappings may require I/O. Reads of indirect blocks, for example.
340 *
341 * So an mpage read of the first 16 blocks of an ext2 file will cause I/O to be
342 * submitted in the following order:
343 *
344 * 12 0 1 2 3 4 5 6 7 8 9 10 11 13 14 15 16
345 *
346 * because the indirect block has to be read to get the mappings of blocks
347 * 13,14,15,16. Obviously, this impacts performance.
348 *
349 * So what we do it to allow the filesystem's get_block() function to set
350 * BH_Boundary when it maps block 11. BH_Boundary says: mapping of the block
351 * after this one will require I/O against a block which is probably close to
352 * this one. So you should push what I/O you have currently accumulated.
353 *
354 * This all causes the disk requests to be issued in the correct order.
355 */
mpage_readahead(struct readahead_control * rac,get_block_t get_block)356 void mpage_readahead(struct readahead_control *rac, get_block_t get_block)
357 {
358 struct folio *folio;
359 struct mpage_readpage_args args = {
360 .get_block = get_block,
361 .is_readahead = true,
362 };
363
364 while ((folio = readahead_folio(rac))) {
365 prefetchw(&folio->flags);
366 args.folio = folio;
367 args.nr_pages = readahead_count(rac);
368 args.bio = do_mpage_readpage(&args);
369 }
370 if (args.bio)
371 mpage_bio_submit(args.bio);
372 }
373 EXPORT_SYMBOL(mpage_readahead);
374
375 /*
376 * This isn't called much at all
377 */
mpage_read_folio(struct folio * folio,get_block_t get_block)378 int mpage_read_folio(struct folio *folio, get_block_t get_block)
379 {
380 struct mpage_readpage_args args = {
381 .folio = folio,
382 .nr_pages = 1,
383 .get_block = get_block,
384 };
385
386 args.bio = do_mpage_readpage(&args);
387 if (args.bio)
388 mpage_bio_submit(args.bio);
389 return 0;
390 }
391 EXPORT_SYMBOL(mpage_read_folio);
392
393 /*
394 * Writing is not so simple.
395 *
396 * If the page has buffers then they will be used for obtaining the disk
397 * mapping. We only support pages which are fully mapped-and-dirty, with a
398 * special case for pages which are unmapped at the end: end-of-file.
399 *
400 * If the page has no buffers (preferred) then the page is mapped here.
401 *
402 * If all blocks are found to be contiguous then the page can go into the
403 * BIO. Otherwise fall back to the mapping's writepage().
404 *
405 * FIXME: This code wants an estimate of how many pages are still to be
406 * written, so it can intelligently allocate a suitably-sized BIO. For now,
407 * just allocate full-size (16-page) BIOs.
408 */
409
410 struct mpage_data {
411 struct bio *bio;
412 sector_t last_block_in_bio;
413 get_block_t *get_block;
414 };
415
416 /*
417 * We have our BIO, so we can now mark the buffers clean. Make
418 * sure to only clean buffers which we know we'll be writing.
419 */
clean_buffers(struct page * page,unsigned first_unmapped)420 static void clean_buffers(struct page *page, unsigned first_unmapped)
421 {
422 unsigned buffer_counter = 0;
423 struct buffer_head *bh, *head;
424 if (!page_has_buffers(page))
425 return;
426 head = page_buffers(page);
427 bh = head;
428
429 do {
430 if (buffer_counter++ == first_unmapped)
431 break;
432 clear_buffer_dirty(bh);
433 bh = bh->b_this_page;
434 } while (bh != head);
435
436 /*
437 * we cannot drop the bh if the page is not uptodate or a concurrent
438 * read_folio would fail to serialize with the bh and it would read from
439 * disk before we reach the platter.
440 */
441 if (buffer_heads_over_limit && PageUptodate(page))
442 try_to_free_buffers(page_folio(page));
443 }
444
445 /*
446 * For situations where we want to clean all buffers attached to a page.
447 * We don't need to calculate how many buffers are attached to the page,
448 * we just need to specify a number larger than the maximum number of buffers.
449 */
clean_page_buffers(struct page * page)450 void clean_page_buffers(struct page *page)
451 {
452 clean_buffers(page, ~0U);
453 }
454
__mpage_writepage(struct page * page,struct writeback_control * wbc,void * data)455 static int __mpage_writepage(struct page *page, struct writeback_control *wbc,
456 void *data)
457 {
458 struct mpage_data *mpd = data;
459 struct bio *bio = mpd->bio;
460 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_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);
477 int ret = 0;
478
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 * block_dirty_folio -> 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_folio(). If this address_space is also
524 * using mpage_readahead 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));
533 block_in_file = (sector_t)page->index << (PAGE_SHIFT - blkbits);
534 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;
539 map_bh.b_size = 1 << blkbits;
540 if (mpd->get_block(inode, block_in_file, &map_bh, 1))
541 goto confused;
542 if (buffer_new(&map_bh))
543 clean_bdev_bh_alias(&map_bh);
544 if (buffer_boundary(&map_bh)) {
545 boundary_block = map_bh.b_blocknr;
546 boundary_bdev = map_bh.b_bdev;
547 }
548 if (page_block) {
549 if (map_bh.b_blocknr != blocks[page_block-1] + 1)
550 goto confused;
551 }
552 blocks[page_block++] = map_bh.b_blocknr;
553 boundary = buffer_boundary(&map_bh);
554 bdev = map_bh.b_bdev;
555 if (block_in_file == last_block)
556 break;
557 block_in_file++;
558 }
559 BUG_ON(page_block == 0);
560
561 first_unmapped = page_block;
562
563 page_is_mapped:
564 end_index = i_size >> PAGE_SHIFT;
565 if (page->index >= end_index) {
566 /*
567 * The page straddles i_size. It must be zeroed out on each
568 * and every writepage invocation because it may be mmapped.
569 * "A file is mapped in multiples of the page size. For a file
570 * that is not a multiple of the page size, the remaining memory
571 * is zeroed when mapped, and writes to that region are not
572 * written out to the file."
573 */
574 unsigned offset = i_size & (PAGE_SIZE - 1);
575
576 if (page->index > end_index || !offset)
577 goto confused;
578 zero_user_segment(page, offset, PAGE_SIZE);
579 }
580
581 /*
582 * This page will go to BIO. Do we need to send this BIO off first?
583 */
584 if (bio && mpd->last_block_in_bio != blocks[0] - 1)
585 bio = mpage_bio_submit(bio);
586
587 alloc_new:
588 if (bio == NULL) {
589 if (first_unmapped == blocks_per_page) {
590 if (!bdev_write_page(bdev, blocks[0] << (blkbits - 9),
591 page, wbc))
592 goto out;
593 }
594 bio = bio_alloc(bdev, BIO_MAX_VECS,
595 REQ_OP_WRITE | wbc_to_write_flags(wbc),
596 GFP_NOFS);
597 bio->bi_iter.bi_sector = blocks[0] << (blkbits - 9);
598 wbc_init_bio(wbc, bio);
599 }
600
601 /*
602 * Must try to add the page before marking the buffer clean or
603 * the confused fail path above (OOM) will be very confused when
604 * it finds all bh marked clean (i.e. it will not write anything)
605 */
606 wbc_account_cgroup_owner(wbc, page, PAGE_SIZE);
607 length = first_unmapped << blkbits;
608 if (bio_add_page(bio, page, length, 0) < length) {
609 bio = mpage_bio_submit(bio);
610 goto alloc_new;
611 }
612
613 clean_buffers(page, first_unmapped);
614
615 BUG_ON(PageWriteback(page));
616 set_page_writeback(page);
617 unlock_page(page);
618 if (boundary || (first_unmapped != blocks_per_page)) {
619 bio = mpage_bio_submit(bio);
620 if (boundary_block) {
621 write_boundary_block(boundary_bdev,
622 boundary_block, 1 << blkbits);
623 }
624 } else {
625 mpd->last_block_in_bio = blocks[blocks_per_page - 1];
626 }
627 goto out;
628
629 confused:
630 if (bio)
631 bio = mpage_bio_submit(bio);
632
633 /*
634 * The caller has a ref on the inode, so *mapping is stable
635 */
636 ret = block_write_full_page(page, mpd->get_block, wbc);
637 mapping_set_error(mapping, ret);
638 out:
639 mpd->bio = bio;
640 return ret;
641 }
642
643 /**
644 * mpage_writepages - walk the list of dirty pages of the given address space & writepage() all of them
645 * @mapping: address space structure to write
646 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
647 * @get_block: the filesystem's block mapper function.
648 *
649 * This is a library function, which implements the writepages()
650 * address_space_operation.
651 *
652 * If a page is already under I/O, generic_writepages() skips it, even
653 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
654 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
655 * and msync() need to guarantee that all the data which was dirty at the time
656 * the call was made get new I/O started against them. If wbc->sync_mode is
657 * WB_SYNC_ALL then we were called for data integrity and we must wait for
658 * existing IO to complete.
659 */
660 int
mpage_writepages(struct address_space * mapping,struct writeback_control * wbc,get_block_t get_block)661 mpage_writepages(struct address_space *mapping,
662 struct writeback_control *wbc, get_block_t get_block)
663 {
664 struct mpage_data mpd = {
665 .get_block = get_block,
666 };
667 struct blk_plug plug;
668 int ret;
669
670 blk_start_plug(&plug);
671 ret = write_cache_pages(mapping, wbc, __mpage_writepage, &mpd);
672 if (mpd.bio)
673 mpage_bio_submit(mpd.bio);
674 blk_finish_plug(&plug);
675 return ret;
676 }
677 EXPORT_SYMBOL(mpage_writepages);
678