blob: 3e0ceef641b1d640a5baad0f6b5ac2aba6fd6609 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
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 *
Francois Camie1f8e872008-10-15 22:01:59 -070010 * 15May2002 Andrew Morton
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 * 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>
Paul Gortmaker630d9c42011-11-16 23:57:37 -050017#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/mm.h>
19#include <linux/kdev_t.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#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>
Andrew Morton02c43632016-03-15 14:55:15 -070028#include <linux/mm_inline.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/writeback.h>
30#include <linux/backing-dev.h>
31#include <linux/pagevec.h>
Suren Baghdasaryan982031a2023-04-12 16:03:03 -070032#include <linux/cleancache.h>
Akinobu Mita4db96b72014-10-09 15:26:55 -070033#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
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
Matthew Wilcox (Oracle)2c69e202022-04-29 10:40:40 -040040 * back to block_read_full_folio().
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 *
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 */
Pankaj Raghavf0d6ca42023-04-11 14:29:19 +020047static void mpage_read_end_io(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048{
Pankaj Raghav09a607c2023-04-11 14:29:20 +020049 struct folio_iter fi;
50 int err = blk_status_to_errno(bio->bi_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Pankaj Raghav09a607c2023-04-11 14:29:20 +020052 bio_for_each_folio_all(fi, bio) {
53 if (err)
54 folio_set_error(fi.folio);
55 else
56 folio_mark_uptodate(fi.folio);
57 folio_unlock(fi.folio);
58 }
Kent Overstreet2c30c712013-11-07 12:20:26 -080059
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061}
62
Pankaj Raghavf0d6ca42023-04-11 14:29:19 +020063static void mpage_write_end_io(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064{
Pankaj Raghav09a607c2023-04-11 14:29:20 +020065 struct folio_iter fi;
66 int err = blk_status_to_errno(bio->bi_status);
Pankaj Raghavf0d6ca42023-04-11 14:29:19 +020067
Pankaj Raghav09a607c2023-04-11 14:29:20 +020068 bio_for_each_folio_all(fi, bio) {
69 if (err) {
70 folio_set_error(fi.folio);
71 mapping_set_error(fi.folio->mapping, err);
72 }
73 folio_end_writeback(fi.folio);
74 }
Pankaj Raghavf0d6ca42023-04-11 14:29:19 +020075
76 bio_put(bio);
77}
78
79static struct bio *mpage_bio_submit_read(struct bio *bio)
80{
81 bio->bi_end_io = mpage_read_end_io;
82 guard_bio_eod(bio);
83 submit_bio(bio);
84 return NULL;
85}
86
87static struct bio *mpage_bio_submit_write(struct bio *bio)
88{
89 bio->bi_end_io = mpage_write_end_io;
Ming Lei83c9c542020-01-05 09:41:14 +080090 guard_bio_eod(bio);
Mike Christie4e49ea42016-06-05 14:31:41 -050091 submit_bio(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 return NULL;
93}
94
Linus Torvalds1da177e2005-04-16 15:20:36 -070095/*
Matthew Wilcox (Oracle)d4388342020-06-01 21:47:02 -070096 * support function for mpage_readahead. The fs supplied get_block might
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 * return an up to date buffer. This is used to map that buffer into
Matthew Wilcox (Oracle)2c69e202022-04-29 10:40:40 -040098 * the page, which allows read_folio to avoid triggering a duplicate call
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 * to get_block.
100 *
101 * The idea is to avoid adding buffers to pages that don't already have
102 * them. So when the buffer is up to date and the page size == block size,
103 * this marks the page up to date instead of adding new buffers.
104 */
Matthew Wilcox (Oracle)211d0442022-06-10 22:58:24 -0400105static void map_buffer_to_folio(struct folio *folio, struct buffer_head *bh,
106 int page_block)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107{
Matthew Wilcox (Oracle)211d0442022-06-10 22:58:24 -0400108 struct inode *inode = folio->mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 struct buffer_head *page_bh, *head;
110 int block = 0;
111
Matthew Wilcox (Oracle)211d0442022-06-10 22:58:24 -0400112 head = folio_buffers(folio);
113 if (!head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 /*
115 * don't make any buffers if there is only one buffer on
Matthew Wilcox (Oracle)211d0442022-06-10 22:58:24 -0400116 * the folio and the folio just needs to be set up to date
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300118 if (inode->i_blkbits == PAGE_SHIFT &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 buffer_uptodate(bh)) {
Matthew Wilcox (Oracle)211d0442022-06-10 22:58:24 -0400120 folio_mark_uptodate(folio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 return;
122 }
Matthew Wilcox (Oracle)211d0442022-06-10 22:58:24 -0400123 create_empty_buffers(&folio->page, i_blocksize(inode), 0);
124 head = folio_buffers(folio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 }
Matthew Wilcox (Oracle)211d0442022-06-10 22:58:24 -0400126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 page_bh = head;
128 do {
129 if (block == page_block) {
130 page_bh->b_state = bh->b_state;
131 page_bh->b_bdev = bh->b_bdev;
132 page_bh->b_blocknr = bh->b_blocknr;
133 break;
134 }
135 page_bh = page_bh->b_this_page;
136 block++;
137 } while (page_bh != head);
138}
139
Jens Axboe357c1202018-08-17 15:45:32 -0700140struct mpage_readpage_args {
141 struct bio *bio;
Matthew Wilcox (Oracle)211d0442022-06-10 22:58:24 -0400142 struct folio *folio;
Jens Axboe357c1202018-08-17 15:45:32 -0700143 unsigned int nr_pages;
Jens Axboe74c81642018-08-17 15:45:36 -0700144 bool is_readahead;
Jens Axboe357c1202018-08-17 15:45:32 -0700145 sector_t last_block_in_bio;
146 struct buffer_head map_bh;
147 unsigned long first_logical_block;
148 get_block_t *get_block;
Jens Axboe357c1202018-08-17 15:45:32 -0700149};
150
Badari Pulavartyfa30bd02006-03-26 01:38:01 -0800151/*
152 * This is the worker routine which does all the work of mapping the disk
153 * blocks and constructs largest possible bios, submits them for IO if the
154 * blocks are not contiguous on the disk.
155 *
156 * We pass a buffer_head back and forth and use its buffer_mapped() flag to
157 * represent the validity of its disk mapping and to decide when to do the next
158 * get_block() call.
159 */
Jens Axboe357c1202018-08-17 15:45:32 -0700160static struct bio *do_mpage_readpage(struct mpage_readpage_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{
Matthew Wilcox (Oracle)211d0442022-06-10 22:58:24 -0400162 struct folio *folio = args->folio;
163 struct inode *inode = folio->mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 const unsigned blkbits = inode->i_blkbits;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300165 const unsigned blocks_per_page = PAGE_SIZE >> blkbits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 const unsigned blocksize = 1 << blkbits;
Jens Axboe357c1202018-08-17 15:45:32 -0700167 struct buffer_head *map_bh = &args->map_bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 sector_t block_in_file;
169 sector_t last_block;
Badari Pulavartyfa30bd02006-03-26 01:38:01 -0800170 sector_t last_block_in_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 sector_t blocks[MAX_BUF_PER_PAGE];
172 unsigned page_block;
173 unsigned first_hole = blocks_per_page;
174 struct block_device *bdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 int length;
176 int fully_mapped = 1;
Bart Van Asschef84c94a2022-07-14 11:07:15 -0700177 blk_opf_t opf = REQ_OP_READ;
Badari Pulavartyfa30bd02006-03-26 01:38:01 -0800178 unsigned nblocks;
179 unsigned relative_block;
Matthew Wilcox (Oracle)211d0442022-06-10 22:58:24 -0400180 gfp_t gfp = mapping_gfp_constraint(folio->mapping, GFP_KERNEL);
181
182 /* MAX_BUF_PER_PAGE, for example */
183 VM_BUG_ON_FOLIO(folio_test_large(folio), folio);
Jens Axboe74c81642018-08-17 15:45:36 -0700184
185 if (args->is_readahead) {
Bart Van Asschef84c94a2022-07-14 11:07:15 -0700186 opf |= REQ_RAHEAD;
Christoph Hellwig61285ff2022-03-23 16:39:52 +0100187 gfp |= __GFP_NORETRY | __GFP_NOWARN;
Jens Axboe74c81642018-08-17 15:45:36 -0700188 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
Matthew Wilcox (Oracle)211d0442022-06-10 22:58:24 -0400190 if (folio_buffers(folio))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 goto confused;
192
Matthew Wilcox (Oracle)211d0442022-06-10 22:58:24 -0400193 block_in_file = (sector_t)folio->index << (PAGE_SHIFT - blkbits);
Jens Axboe357c1202018-08-17 15:45:32 -0700194 last_block = block_in_file + args->nr_pages * blocks_per_page;
Badari Pulavartyfa30bd02006-03-26 01:38:01 -0800195 last_block_in_file = (i_size_read(inode) + blocksize - 1) >> blkbits;
196 if (last_block > last_block_in_file)
197 last_block = last_block_in_file;
198 page_block = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Badari Pulavartyfa30bd02006-03-26 01:38:01 -0800200 /*
201 * Map blocks using the result from the previous get_blocks call first.
202 */
203 nblocks = map_bh->b_size >> blkbits;
Jens Axboe357c1202018-08-17 15:45:32 -0700204 if (buffer_mapped(map_bh) &&
205 block_in_file > args->first_logical_block &&
206 block_in_file < (args->first_logical_block + nblocks)) {
207 unsigned map_offset = block_in_file - args->first_logical_block;
Badari Pulavartyfa30bd02006-03-26 01:38:01 -0800208 unsigned last = nblocks - map_offset;
209
210 for (relative_block = 0; ; relative_block++) {
211 if (relative_block == last) {
212 clear_buffer_mapped(map_bh);
213 break;
214 }
215 if (page_block == blocks_per_page)
216 break;
217 blocks[page_block] = map_bh->b_blocknr + map_offset +
218 relative_block;
219 page_block++;
220 block_in_file++;
221 }
222 bdev = map_bh->b_bdev;
223 }
224
225 /*
Matthew Wilcox (Oracle)211d0442022-06-10 22:58:24 -0400226 * Then do more get_blocks calls until we are done with this folio.
Badari Pulavartyfa30bd02006-03-26 01:38:01 -0800227 */
Matthew Wilcox (Oracle)a5fd8392022-12-15 21:44:02 +0000228 map_bh->b_folio = folio;
Badari Pulavartyfa30bd02006-03-26 01:38:01 -0800229 while (page_block < blocks_per_page) {
230 map_bh->b_state = 0;
231 map_bh->b_size = 0;
232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 if (block_in_file < last_block) {
Badari Pulavartyfa30bd02006-03-26 01:38:01 -0800234 map_bh->b_size = (last_block-block_in_file) << blkbits;
Jens Axboe357c1202018-08-17 15:45:32 -0700235 if (args->get_block(inode, block_in_file, map_bh, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 goto confused;
Jens Axboe357c1202018-08-17 15:45:32 -0700237 args->first_logical_block = block_in_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 }
239
Badari Pulavartyfa30bd02006-03-26 01:38:01 -0800240 if (!buffer_mapped(map_bh)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 fully_mapped = 0;
242 if (first_hole == blocks_per_page)
243 first_hole = page_block;
Badari Pulavartyfa30bd02006-03-26 01:38:01 -0800244 page_block++;
245 block_in_file++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 continue;
247 }
248
249 /* some filesystems will copy data into the page during
250 * the get_block call, in which case we don't want to
Matthew Wilcox (Oracle)211d0442022-06-10 22:58:24 -0400251 * read it again. map_buffer_to_folio copies the data
252 * we just collected from get_block into the folio's buffers
253 * so read_folio doesn't have to repeat the get_block call
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 */
Badari Pulavartyfa30bd02006-03-26 01:38:01 -0800255 if (buffer_uptodate(map_bh)) {
Matthew Wilcox (Oracle)211d0442022-06-10 22:58:24 -0400256 map_buffer_to_folio(folio, map_bh, page_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 goto confused;
258 }
259
260 if (first_hole != blocks_per_page)
261 goto confused; /* hole -> non-hole */
262
263 /* Contiguous blocks? */
Badari Pulavartyfa30bd02006-03-26 01:38:01 -0800264 if (page_block && blocks[page_block-1] != map_bh->b_blocknr-1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 goto confused;
Badari Pulavartyfa30bd02006-03-26 01:38:01 -0800266 nblocks = map_bh->b_size >> blkbits;
267 for (relative_block = 0; ; relative_block++) {
268 if (relative_block == nblocks) {
269 clear_buffer_mapped(map_bh);
270 break;
271 } else if (page_block == blocks_per_page)
272 break;
273 blocks[page_block] = map_bh->b_blocknr+relative_block;
274 page_block++;
275 block_in_file++;
276 }
277 bdev = map_bh->b_bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 }
279
280 if (first_hole != blocks_per_page) {
Matthew Wilcox (Oracle)211d0442022-06-10 22:58:24 -0400281 folio_zero_segment(folio, first_hole << blkbits, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 if (first_hole == 0) {
Matthew Wilcox (Oracle)211d0442022-06-10 22:58:24 -0400283 folio_mark_uptodate(folio);
284 folio_unlock(folio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 goto out;
286 }
287 } else if (fully_mapped) {
Matthew Wilcox (Oracle)211d0442022-06-10 22:58:24 -0400288 folio_set_mappedtodisk(folio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 }
290
Suren Baghdasaryan982031a2023-04-12 16:03:03 -0700291 if (fully_mapped && blocks_per_page == 1 && !folio_test_uptodate(folio) &&
292 cleancache_get_page(&folio->page) == 0) {
293 folio_mark_uptodate(folio);
294 goto confused;
295 }
296
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 /*
Matthew Wilcox (Oracle)211d0442022-06-10 22:58:24 -0400298 * This folio will go to BIO. Do we need to send this BIO off first?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 */
Jens Axboe357c1202018-08-17 15:45:32 -0700300 if (args->bio && (args->last_block_in_bio != blocks[0] - 1))
Pankaj Raghavf0d6ca42023-04-11 14:29:19 +0200301 args->bio = mpage_bio_submit_read(args->bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
303alloc_new:
Jens Axboe357c1202018-08-17 15:45:32 -0700304 if (args->bio == NULL) {
Bart Van Asschef84c94a2022-07-14 11:07:15 -0700305 args->bio = bio_alloc(bdev, bio_max_segs(args->nr_pages), opf,
Christoph Hellwig07888c662022-01-24 10:11:05 +0100306 gfp);
Jens Axboe357c1202018-08-17 15:45:32 -0700307 if (args->bio == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 goto confused;
Christoph Hellwigd5f68a42022-01-24 10:10:49 +0100309 args->bio->bi_iter.bi_sector = blocks[0] << (blkbits - 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 }
311
312 length = first_hole << blkbits;
Matthew Wilcox (Oracle)211d0442022-06-10 22:58:24 -0400313 if (!bio_add_folio(args->bio, folio, length, 0)) {
Pankaj Raghavf0d6ca42023-04-11 14:29:19 +0200314 args->bio = mpage_bio_submit_read(args->bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 goto alloc_new;
316 }
317
Jens Axboe357c1202018-08-17 15:45:32 -0700318 relative_block = block_in_file - args->first_logical_block;
Miquel van Smoorenburg38c8e612009-01-06 14:39:02 -0800319 nblocks = map_bh->b_size >> blkbits;
320 if ((buffer_boundary(map_bh) && relative_block == nblocks) ||
321 (first_hole != blocks_per_page))
Pankaj Raghavf0d6ca42023-04-11 14:29:19 +0200322 args->bio = mpage_bio_submit_read(args->bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 else
Jens Axboe357c1202018-08-17 15:45:32 -0700324 args->last_block_in_bio = blocks[blocks_per_page - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325out:
Jens Axboe357c1202018-08-17 15:45:32 -0700326 return args->bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
328confused:
Jens Axboe357c1202018-08-17 15:45:32 -0700329 if (args->bio)
Pankaj Raghavf0d6ca42023-04-11 14:29:19 +0200330 args->bio = mpage_bio_submit_read(args->bio);
Matthew Wilcox (Oracle)211d0442022-06-10 22:58:24 -0400331 if (!folio_test_uptodate(folio))
332 block_read_full_folio(folio, args->get_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 else
Matthew Wilcox (Oracle)211d0442022-06-10 22:58:24 -0400334 folio_unlock(folio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 goto out;
336}
337
Martin Waitz67be2dd2005-05-01 08:59:26 -0700338/**
Matthew Wilcox (Oracle)d4388342020-06-01 21:47:02 -0700339 * mpage_readahead - start reads against pages
340 * @rac: Describes which pages to read.
Martin Waitz67be2dd2005-05-01 08:59:26 -0700341 * @get_block: The filesystem's block mapper function.
342 *
343 * This function walks the pages and the blocks within each page, building and
344 * emitting large BIOs.
345 *
346 * If anything unusual happens, such as:
347 *
348 * - encountering a page which has buffers
349 * - encountering a page which has a non-hole after a hole
350 * - encountering a page with non-contiguous blocks
351 *
352 * then this code just gives up and calls the buffer_head-based read function.
353 * It does handle a page which has holes at the end - that is a common case:
Kirill A. Shutemovea1754a2016-04-01 15:29:48 +0300354 * the end-of-file on blocksize < PAGE_SIZE setups.
Martin Waitz67be2dd2005-05-01 08:59:26 -0700355 *
356 * BH_Boundary explanation:
357 *
358 * There is a problem. The mpage read code assembles several pages, gets all
359 * their disk mappings, and then submits them all. That's fine, but obtaining
360 * the disk mappings may require I/O. Reads of indirect blocks, for example.
361 *
362 * So an mpage read of the first 16 blocks of an ext2 file will cause I/O to be
363 * submitted in the following order:
Mauro Carvalho Chehab0117d422017-05-12 07:45:42 -0300364 *
Martin Waitz67be2dd2005-05-01 08:59:26 -0700365 * 12 0 1 2 3 4 5 6 7 8 9 10 11 13 14 15 16
Randy Dunlap78a4a502008-02-29 22:02:31 -0800366 *
Martin Waitz67be2dd2005-05-01 08:59:26 -0700367 * because the indirect block has to be read to get the mappings of blocks
368 * 13,14,15,16. Obviously, this impacts performance.
369 *
370 * So what we do it to allow the filesystem's get_block() function to set
371 * BH_Boundary when it maps block 11. BH_Boundary says: mapping of the block
372 * after this one will require I/O against a block which is probably close to
373 * this one. So you should push what I/O you have currently accumulated.
374 *
375 * This all causes the disk requests to be issued in the correct order.
376 */
Matthew Wilcox (Oracle)d4388342020-06-01 21:47:02 -0700377void mpage_readahead(struct readahead_control *rac, get_block_t get_block)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378{
Matthew Wilcox (Oracle)211d0442022-06-10 22:58:24 -0400379 struct folio *folio;
Jens Axboe357c1202018-08-17 15:45:32 -0700380 struct mpage_readpage_args args = {
381 .get_block = get_block,
Jens Axboe74c81642018-08-17 15:45:36 -0700382 .is_readahead = true,
Jens Axboe357c1202018-08-17 15:45:32 -0700383 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
Matthew Wilcox (Oracle)211d0442022-06-10 22:58:24 -0400385 while ((folio = readahead_folio(rac))) {
386 prefetchw(&folio->flags);
387 args.folio = folio;
Matthew Wilcox (Oracle)d4388342020-06-01 21:47:02 -0700388 args.nr_pages = readahead_count(rac);
389 args.bio = do_mpage_readpage(&args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 }
Jens Axboe357c1202018-08-17 15:45:32 -0700391 if (args.bio)
Pankaj Raghavf0d6ca42023-04-11 14:29:19 +0200392 mpage_bio_submit_read(args.bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393}
Greg Kroah-Hartmana38b2072020-07-02 12:51:03 +0200394EXPORT_SYMBOL_NS(mpage_readahead, ANDROID_GKI_VFS_EXPORT_ONLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
396/*
397 * This isn't called much at all
398 */
Matthew Wilcox (Oracle)f132ab72022-04-29 11:47:39 -0400399int mpage_read_folio(struct folio *folio, get_block_t get_block)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400{
Jens Axboe357c1202018-08-17 15:45:32 -0700401 struct mpage_readpage_args args = {
Matthew Wilcox (Oracle)211d0442022-06-10 22:58:24 -0400402 .folio = folio,
Jens Axboe357c1202018-08-17 15:45:32 -0700403 .nr_pages = 1,
404 .get_block = get_block,
Jens Axboe357c1202018-08-17 15:45:32 -0700405 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
Jens Axboe357c1202018-08-17 15:45:32 -0700407 args.bio = do_mpage_readpage(&args);
408 if (args.bio)
Pankaj Raghavf0d6ca42023-04-11 14:29:19 +0200409 mpage_bio_submit_read(args.bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 return 0;
411}
Matthew Wilcox (Oracle)f132ab72022-04-29 11:47:39 -0400412EXPORT_SYMBOL(mpage_read_folio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
414/*
415 * Writing is not so simple.
416 *
417 * If the page has buffers then they will be used for obtaining the disk
418 * mapping. We only support pages which are fully mapped-and-dirty, with a
419 * special case for pages which are unmapped at the end: end-of-file.
420 *
421 * If the page has no buffers (preferred) then the page is mapped here.
422 *
423 * If all blocks are found to be contiguous then the page can go into the
424 * BIO. Otherwise fall back to the mapping's writepage().
425 *
426 * FIXME: This code wants an estimate of how many pages are still to be
427 * written, so it can intelligently allocate a suitably-sized BIO. For now,
428 * just allocate full-size (16-page) BIOs.
429 */
Miklos Szeredi0ea97182007-05-10 22:22:51 -0700430
Dmitri Vorobievced117c2009-03-31 00:41:20 +0300431struct mpage_data {
432 struct bio *bio;
433 sector_t last_block_in_bio;
434 get_block_t *get_block;
Dmitri Vorobievced117c2009-03-31 00:41:20 +0300435};
436
Matthew Wilcox90768ee2014-06-04 16:07:44 -0700437/*
438 * We have our BIO, so we can now mark the buffers clean. Make
439 * sure to only clean buffers which we know we'll be writing.
440 */
441static void clean_buffers(struct page *page, unsigned first_unmapped)
442{
443 unsigned buffer_counter = 0;
444 struct buffer_head *bh, *head;
445 if (!page_has_buffers(page))
446 return;
447 head = page_buffers(page);
448 bh = head;
449
450 do {
451 if (buffer_counter++ == first_unmapped)
452 break;
453 clear_buffer_dirty(bh);
454 bh = bh->b_this_page;
455 } while (bh != head);
456
457 /*
458 * we cannot drop the bh if the page is not uptodate or a concurrent
Matthew Wilcox (Oracle)2c69e202022-04-29 10:40:40 -0400459 * read_folio would fail to serialize with the bh and it would read from
Matthew Wilcox90768ee2014-06-04 16:07:44 -0700460 * disk before we reach the platter.
461 */
462 if (buffer_heads_over_limit && PageUptodate(page))
Matthew Wilcox (Oracle)68189fe2022-05-01 01:08:08 -0400463 try_to_free_buffers(page_folio(page));
Matthew Wilcox90768ee2014-06-04 16:07:44 -0700464}
465
Matthew Wilcoxf8927602017-10-13 15:58:15 -0700466/*
467 * For situations where we want to clean all buffers attached to a page.
468 * We don't need to calculate how many buffers are attached to the page,
469 * we just need to specify a number larger than the maximum number of buffers.
470 */
471void clean_page_buffers(struct page *page)
472{
473 clean_buffers(page, ~0U);
474}
475
Matthew Wilcox (Oracle)d585bdb2023-01-26 20:12:54 +0000476static int __mpage_writepage(struct folio *folio, struct writeback_control *wbc,
Alex Tomas29a814d2008-07-11 19:27:31 -0400477 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478{
Miklos Szeredi0ea97182007-05-10 22:22:51 -0700479 struct mpage_data *mpd = data;
480 struct bio *bio = mpd->bio;
Matthew Wilcox (Oracle)9160cff2023-01-26 20:12:55 +0000481 struct address_space *mapping = folio->mapping;
482 struct inode *inode = mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 const unsigned blkbits = inode->i_blkbits;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300484 const unsigned blocks_per_page = PAGE_SIZE >> blkbits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 sector_t last_block;
486 sector_t block_in_file;
487 sector_t blocks[MAX_BUF_PER_PAGE];
488 unsigned page_block;
489 unsigned first_unmapped = blocks_per_page;
490 struct block_device *bdev = NULL;
491 int boundary = 0;
492 sector_t boundary_block = 0;
493 struct block_device *boundary_bdev = NULL;
Matthew Wilcox (Oracle)9160cff2023-01-26 20:12:55 +0000494 size_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 struct buffer_head map_bh;
496 loff_t i_size = i_size_read(inode);
Miklos Szeredi0ea97182007-05-10 22:22:51 -0700497 int ret = 0;
Matthew Wilcox (Oracle)9160cff2023-01-26 20:12:55 +0000498 struct buffer_head *head = folio_buffers(folio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
Matthew Wilcox (Oracle)9160cff2023-01-26 20:12:55 +0000500 if (head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 struct buffer_head *bh = head;
502
503 /* If they're all mapped and dirty, do it */
504 page_block = 0;
505 do {
506 BUG_ON(buffer_locked(bh));
507 if (!buffer_mapped(bh)) {
508 /*
509 * unmapped dirty buffers are created by
Matthew Wilcox (Oracle)e6219002022-02-09 20:22:12 +0000510 * block_dirty_folio -> mmapped data
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 */
512 if (buffer_dirty(bh))
513 goto confused;
514 if (first_unmapped == blocks_per_page)
515 first_unmapped = page_block;
516 continue;
517 }
518
519 if (first_unmapped != blocks_per_page)
520 goto confused; /* hole -> non-hole */
521
522 if (!buffer_dirty(bh) || !buffer_uptodate(bh))
523 goto confused;
524 if (page_block) {
525 if (bh->b_blocknr != blocks[page_block-1] + 1)
526 goto confused;
527 }
528 blocks[page_block++] = bh->b_blocknr;
529 boundary = buffer_boundary(bh);
530 if (boundary) {
531 boundary_block = bh->b_blocknr;
532 boundary_bdev = bh->b_bdev;
533 }
534 bdev = bh->b_bdev;
535 } while ((bh = bh->b_this_page) != head);
536
537 if (first_unmapped)
538 goto page_is_mapped;
539
540 /*
541 * Page has buffers, but they are all unmapped. The page was
542 * created by pagein or read over a hole which was handled by
Matthew Wilcox (Oracle)2c69e202022-04-29 10:40:40 -0400543 * block_read_full_folio(). If this address_space is also
Matthew Wilcox (Oracle)d4388342020-06-01 21:47:02 -0700544 * using mpage_readahead then this can rarely happen.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 */
546 goto confused;
547 }
548
549 /*
550 * The page has no buffers: map it to disk
551 */
Matthew Wilcox (Oracle)9160cff2023-01-26 20:12:55 +0000552 BUG_ON(!folio_test_uptodate(folio));
553 block_in_file = (sector_t)folio->index << (PAGE_SHIFT - blkbits);
Jan Kara4b89a372023-01-03 11:44:30 +0100554 /*
555 * Whole page beyond EOF? Skip allocating blocks to avoid leaking
556 * space.
557 */
558 if (block_in_file >= (i_size + (1 << blkbits) - 1) >> blkbits)
559 goto page_is_mapped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 last_block = (i_size - 1) >> blkbits;
Matthew Wilcox (Oracle)9160cff2023-01-26 20:12:55 +0000561 map_bh.b_folio = folio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 for (page_block = 0; page_block < blocks_per_page; ) {
563
564 map_bh.b_state = 0;
Badari Pulavartyb0cf23212006-03-26 01:38:00 -0800565 map_bh.b_size = 1 << blkbits;
Miklos Szeredi0ea97182007-05-10 22:22:51 -0700566 if (mpd->get_block(inode, block_in_file, &map_bh, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 goto confused;
Jan Kara70108392023-01-25 14:59:14 +0100568 if (!buffer_mapped(&map_bh))
569 goto confused;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 if (buffer_new(&map_bh))
Jan Karae64855c2016-11-04 18:08:15 +0100571 clean_bdev_bh_alias(&map_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 if (buffer_boundary(&map_bh)) {
573 boundary_block = map_bh.b_blocknr;
574 boundary_bdev = map_bh.b_bdev;
575 }
576 if (page_block) {
577 if (map_bh.b_blocknr != blocks[page_block-1] + 1)
578 goto confused;
579 }
580 blocks[page_block++] = map_bh.b_blocknr;
581 boundary = buffer_boundary(&map_bh);
582 bdev = map_bh.b_bdev;
583 if (block_in_file == last_block)
584 break;
585 block_in_file++;
586 }
587 BUG_ON(page_block == 0);
588
589 first_unmapped = page_block;
590
591page_is_mapped:
Matthew Wilcox (Oracle)9160cff2023-01-26 20:12:55 +0000592 /* Don't bother writing beyond EOF, truncate will discard the folio */
593 if (folio_pos(folio) >= i_size)
594 goto confused;
595 length = folio_size(folio);
596 if (folio_pos(folio) + length > i_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 /*
598 * The page straddles i_size. It must be zeroed out on each
Adam Buchbinder2a61aa42009-12-11 16:35:40 -0500599 * and every writepage invocation because it may be mmapped.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 * "A file is mapped in multiples of the page size. For a file
601 * that is not a multiple of the page size, the remaining memory
602 * is zeroed when mapped, and writes to that region are not
603 * written out to the file."
604 */
Matthew Wilcox (Oracle)9160cff2023-01-26 20:12:55 +0000605 length = i_size - folio_pos(folio);
606 folio_zero_segment(folio, length, folio_size(folio));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 }
608
609 /*
610 * This page will go to BIO. Do we need to send this BIO off first?
611 */
Miklos Szeredi0ea97182007-05-10 22:22:51 -0700612 if (bio && mpd->last_block_in_bio != blocks[0] - 1)
Pankaj Raghavf0d6ca42023-04-11 14:29:19 +0200613 bio = mpage_bio_submit_write(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
615alloc_new:
616 if (bio == NULL) {
Christoph Hellwig77c436d2022-02-22 16:46:32 +0100617 bio = bio_alloc(bdev, BIO_MAX_VECS,
618 REQ_OP_WRITE | wbc_to_write_flags(wbc),
619 GFP_NOFS);
Christoph Hellwigd5f68a42022-01-24 10:10:49 +0100620 bio->bi_iter.bi_sector = blocks[0] << (blkbits - 9);
Tejun Heob16b1de2015-06-02 08:39:48 -0600621 wbc_init_bio(wbc, bio);
Bart Van Asschea90e30c2024-02-02 12:39:25 -0800622 bio->bi_write_hint = inode->i_write_hint;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 }
624
625 /*
626 * Must try to add the page before marking the buffer clean or
627 * the confused fail path above (OOM) will be very confused when
628 * it finds all bh marked clean (i.e. it will not write anything)
629 */
Matthew Wilcox (Oracle)9160cff2023-01-26 20:12:55 +0000630 wbc_account_cgroup_owner(wbc, &folio->page, folio_size(folio));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 length = first_unmapped << blkbits;
Matthew Wilcox (Oracle)9160cff2023-01-26 20:12:55 +0000632 if (!bio_add_folio(bio, folio, length, 0)) {
Pankaj Raghavf0d6ca42023-04-11 14:29:19 +0200633 bio = mpage_bio_submit_write(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 goto alloc_new;
635 }
636
Matthew Wilcox (Oracle)9160cff2023-01-26 20:12:55 +0000637 clean_buffers(&folio->page, first_unmapped);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
Matthew Wilcox (Oracle)9160cff2023-01-26 20:12:55 +0000639 BUG_ON(folio_test_writeback(folio));
640 folio_start_writeback(folio);
641 folio_unlock(folio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 if (boundary || (first_unmapped != blocks_per_page)) {
Pankaj Raghavf0d6ca42023-04-11 14:29:19 +0200643 bio = mpage_bio_submit_write(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 if (boundary_block) {
645 write_boundary_block(boundary_bdev,
646 boundary_block, 1 << blkbits);
647 }
648 } else {
Miklos Szeredi0ea97182007-05-10 22:22:51 -0700649 mpd->last_block_in_bio = blocks[blocks_per_page - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 }
651 goto out;
652
653confused:
654 if (bio)
Pankaj Raghavf0d6ca42023-04-11 14:29:19 +0200655 bio = mpage_bio_submit_write(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 /*
658 * The caller has a ref on the inode, so *mapping is stable
659 */
Matthew Wilcox (Oracle)9160cff2023-01-26 20:12:55 +0000660 ret = block_write_full_page(&folio->page, mpd->get_block, wbc);
Miklos Szeredi0ea97182007-05-10 22:22:51 -0700661 mapping_set_error(mapping, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662out:
Miklos Szeredi0ea97182007-05-10 22:22:51 -0700663 mpd->bio = bio;
664 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665}
666
667/**
Randy Dunlap78a4a502008-02-29 22:02:31 -0800668 * mpage_writepages - walk the list of dirty pages of the given address space & writepage() all of them
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 * @mapping: address space structure to write
670 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
671 * @get_block: the filesystem's block mapper function.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 *
673 * This is a library function, which implements the writepages()
674 * address_space_operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 */
676int
677mpage_writepages(struct address_space *mapping,
678 struct writeback_control *wbc, get_block_t get_block)
679{
Christoph Hellwigcf5e7a62022-06-13 07:37:15 +0200680 struct mpage_data mpd = {
681 .get_block = get_block,
682 };
Jens Axboe2ed1a6b2010-06-22 12:52:14 +0200683 struct blk_plug plug;
Miklos Szeredi0ea97182007-05-10 22:22:51 -0700684 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
Jens Axboe2ed1a6b2010-06-22 12:52:14 +0200686 blk_start_plug(&plug);
Christoph Hellwigcf5e7a62022-06-13 07:37:15 +0200687 ret = write_cache_pages(mapping, wbc, __mpage_writepage, &mpd);
688 if (mpd.bio)
Pankaj Raghavf0d6ca42023-04-11 14:29:19 +0200689 mpage_bio_submit_write(mpd.bio);
Jens Axboe2ed1a6b2010-06-22 12:52:14 +0200690 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 return ret;
692}
693EXPORT_SYMBOL(mpage_writepages);