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