1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2017-2018 HUAWEI, Inc.
4 * https://www.huawei.com/
7 #include <linux/prefetch.h>
9 #include <trace/events/erofs.h>
11 static void erofs_readendio(struct bio *bio)
14 blk_status_t err = bio->bi_status;
15 struct bvec_iter_all iter_all;
17 bio_for_each_segment_all(bvec, bio, iter_all) {
18 struct page *page = bvec->bv_page;
20 /* page is already locked */
21 DBG_BUGON(PageUptodate(page));
26 SetPageUptodate(page);
29 /* page could be reclaimed now */
34 struct page *erofs_get_meta_page(struct super_block *sb, erofs_blk_t blkaddr)
36 struct address_space *const mapping = sb->s_bdev->bd_inode->i_mapping;
39 page = read_cache_page_gfp(mapping, blkaddr,
40 mapping_gfp_constraint(mapping, ~__GFP_FS));
41 /* should already be PageUptodate */
47 static int erofs_map_blocks_flatmode(struct inode *inode,
48 struct erofs_map_blocks *map,
52 erofs_blk_t nblocks, lastblk;
53 u64 offset = map->m_la;
54 struct erofs_inode *vi = EROFS_I(inode);
55 bool tailendpacking = (vi->datalayout == EROFS_INODE_FLAT_INLINE);
57 trace_erofs_map_blocks_flatmode_enter(inode, map, flags);
59 nblocks = DIV_ROUND_UP(inode->i_size, PAGE_SIZE);
60 lastblk = nblocks - tailendpacking;
62 if (offset >= inode->i_size) {
63 /* leave out-of-bound access unmapped */
69 /* there is no hole in flatmode */
70 map->m_flags = EROFS_MAP_MAPPED;
72 if (offset < blknr_to_addr(lastblk)) {
73 map->m_pa = blknr_to_addr(vi->raw_blkaddr) + map->m_la;
74 map->m_plen = blknr_to_addr(lastblk) - offset;
75 } else if (tailendpacking) {
76 /* 2 - inode inline B: inode, [xattrs], inline last blk... */
77 struct erofs_sb_info *sbi = EROFS_SB(inode->i_sb);
79 map->m_pa = iloc(sbi, vi->nid) + vi->inode_isize +
80 vi->xattr_isize + erofs_blkoff(map->m_la);
81 map->m_plen = inode->i_size - offset;
83 /* inline data should be located in one meta block */
84 if (erofs_blkoff(map->m_pa) + map->m_plen > PAGE_SIZE) {
85 erofs_err(inode->i_sb,
86 "inline data cross block boundary @ nid %llu",
93 map->m_flags |= EROFS_MAP_META;
95 erofs_err(inode->i_sb,
96 "internal error @ nid: %llu (size %llu), m_la 0x%llx",
97 vi->nid, inode->i_size, map->m_la);
104 map->m_llen = map->m_plen;
107 trace_erofs_map_blocks_flatmode_exit(inode, map, flags, 0);
111 static inline struct bio *erofs_read_raw_page(struct bio *bio,
112 struct address_space *mapping,
114 erofs_off_t *last_block,
115 unsigned int nblocks,
119 struct inode *const inode = mapping->host;
120 struct super_block *const sb = inode->i_sb;
121 erofs_off_t current_block = (erofs_off_t)page->index;
126 if (PageUptodate(page)) {
131 /* note that for readpage case, bio also equals to NULL */
133 (*last_block + 1 != current_block || !*eblks)) {
140 struct erofs_map_blocks map = {
141 .m_la = blknr_to_addr(current_block),
146 err = erofs_map_blocks_flatmode(inode, &map, EROFS_GET_BLOCKS_RAW);
150 /* zero out the holed page */
151 if (!(map.m_flags & EROFS_MAP_MAPPED)) {
152 zero_user_segment(page, 0, PAGE_SIZE);
153 SetPageUptodate(page);
155 /* imply err = 0, see erofs_map_blocks */
159 /* for RAW access mode, m_plen must be equal to m_llen */
160 DBG_BUGON(map.m_plen != map.m_llen);
162 blknr = erofs_blknr(map.m_pa);
163 blkoff = erofs_blkoff(map.m_pa);
165 /* deal with inline page */
166 if (map.m_flags & EROFS_MAP_META) {
170 DBG_BUGON(map.m_plen > PAGE_SIZE);
172 ipage = erofs_get_meta_page(inode->i_sb, blknr);
175 err = PTR_ERR(ipage);
179 vsrc = kmap_atomic(ipage);
180 vto = kmap_atomic(page);
181 memcpy(vto, vsrc + blkoff, map.m_plen);
182 memset(vto + map.m_plen, 0, PAGE_SIZE - map.m_plen);
185 flush_dcache_page(page);
187 SetPageUptodate(page);
188 /* TODO: could we unlock the page earlier? */
192 /* imply err = 0, see erofs_map_blocks */
196 /* pa must be block-aligned for raw reading */
197 DBG_BUGON(erofs_blkoff(map.m_pa));
199 /* max # of continuous pages */
200 if (nblocks > DIV_ROUND_UP(map.m_plen, PAGE_SIZE))
201 nblocks = DIV_ROUND_UP(map.m_plen, PAGE_SIZE);
203 *eblks = bio_max_segs(nblocks);
204 bio = bio_alloc(GFP_NOIO, *eblks);
206 bio->bi_end_io = erofs_readendio;
207 bio_set_dev(bio, sb->s_bdev);
208 bio->bi_iter.bi_sector = (sector_t)blknr <<
209 LOG_SECTORS_PER_BLOCK;
210 bio->bi_opf = REQ_OP_READ | (ra ? REQ_RAHEAD : 0);
213 err = bio_add_page(bio, page, PAGE_SIZE, 0);
214 /* out of the extent or bio is full */
216 goto submit_bio_retry;
218 *last_block = current_block;
222 /* for sync reading, set page error immediately */
225 ClearPageUptodate(page);
230 /* if updated manually, continuous pages has a gap */
233 return err ? ERR_PTR(err) : NULL;
237 * since we dont have write or truncate flows, so no inode
238 * locking needs to be held at the moment.
240 static int erofs_raw_access_readpage(struct file *file, struct page *page)
242 erofs_off_t last_block;
246 trace_erofs_readpage(page, true);
248 bio = erofs_read_raw_page(NULL, page->mapping,
249 page, &last_block, 1, &eblks, false);
259 static void erofs_raw_access_readahead(struct readahead_control *rac)
261 erofs_off_t last_block;
263 struct bio *bio = NULL;
266 trace_erofs_readpages(rac->mapping->host, readahead_index(rac),
267 readahead_count(rac), true);
269 while ((page = readahead_page(rac))) {
270 prefetchw(&page->flags);
272 bio = erofs_read_raw_page(bio, rac->mapping, page, &last_block,
273 readahead_count(rac), &eblks, true);
275 /* all the page errors are ignored when readahead */
277 pr_err("%s, readahead error at page %lu of nid %llu\n",
278 __func__, page->index,
279 EROFS_I(rac->mapping->host)->nid);
291 static sector_t erofs_bmap(struct address_space *mapping, sector_t block)
293 struct inode *inode = mapping->host;
294 struct erofs_map_blocks map = {
295 .m_la = blknr_to_addr(block),
298 if (EROFS_I(inode)->datalayout == EROFS_INODE_FLAT_INLINE) {
299 erofs_blk_t blks = i_size_read(inode) >> LOG_BLOCK_SIZE;
301 if (block >> LOG_SECTORS_PER_BLOCK >= blks)
305 if (!erofs_map_blocks_flatmode(inode, &map, EROFS_GET_BLOCKS_RAW))
306 return erofs_blknr(map.m_pa);
311 /* for uncompressed (aligned) files and raw access for other files */
312 const struct address_space_operations erofs_raw_access_aops = {
313 .readpage = erofs_raw_access_readpage,
314 .readahead = erofs_raw_access_readahead,