Gao Xiang | 29b24f6 | 2019-07-31 23:57:31 +0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 2 | /* |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 3 | * Copyright (C) 2017-2018 HUAWEI, Inc. |
| 4 | * http://www.huawei.com/ |
| 5 | * Created by Gao Xiang <gaoxiang25@huawei.com> |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 6 | */ |
| 7 | #include "internal.h" |
| 8 | #include <linux/prefetch.h> |
| 9 | |
Chao Yu | 13f06f4 | 2018-07-26 20:21:55 +0800 | [diff] [blame] | 10 | #include <trace/events/erofs.h> |
| 11 | |
Gao Xiang | 99634bf | 2019-09-04 10:09:05 +0800 | [diff] [blame] | 12 | static void erofs_readendio(struct bio *bio) |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 13 | { |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 14 | struct bio_vec *bvec; |
Gao Xiang | 14a56ec | 2019-03-25 11:40:09 +0800 | [diff] [blame] | 15 | blk_status_t err = bio->bi_status; |
Ming Lei | 6dc4f10 | 2019-02-15 19:13:19 +0800 | [diff] [blame] | 16 | struct bvec_iter_all iter_all; |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 17 | |
Christoph Hellwig | 2b070cf | 2019-04-25 09:03:00 +0200 | [diff] [blame] | 18 | bio_for_each_segment_all(bvec, bio, iter_all) { |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 19 | struct page *page = bvec->bv_page; |
| 20 | |
| 21 | /* page is already locked */ |
Chen Gong | 9141b60 | 2018-09-18 22:27:28 +0800 | [diff] [blame] | 22 | DBG_BUGON(PageUptodate(page)); |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 23 | |
Gao Xiang | 8d8a09b | 2019-08-30 00:38:27 +0800 | [diff] [blame] | 24 | if (err) |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 25 | SetPageError(page); |
| 26 | else |
| 27 | SetPageUptodate(page); |
| 28 | |
| 29 | unlock_page(page); |
| 30 | /* page could be reclaimed now */ |
| 31 | } |
| 32 | bio_put(bio); |
| 33 | } |
| 34 | |
Gao Xiang | e655b5b | 2019-09-04 10:09:03 +0800 | [diff] [blame] | 35 | struct page *erofs_get_meta_page(struct super_block *sb, erofs_blk_t blkaddr) |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 36 | { |
Gao Xiang | 55252ab | 2019-09-22 02:43:55 +0800 | [diff] [blame] | 37 | struct address_space *const mapping = sb->s_bdev->bd_inode->i_mapping; |
| 38 | struct page *page; |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 39 | |
Gao Xiang | 55252ab | 2019-09-22 02:43:55 +0800 | [diff] [blame] | 40 | page = read_cache_page_gfp(mapping, blkaddr, |
Gao Xiang | 618f40e | 2019-09-04 10:09:12 +0800 | [diff] [blame] | 41 | mapping_gfp_constraint(mapping, ~__GFP_FS)); |
Gao Xiang | 55252ab | 2019-09-22 02:43:55 +0800 | [diff] [blame] | 42 | /* should already be PageUptodate */ |
| 43 | if (!IS_ERR(page)) |
| 44 | lock_page(page); |
| 45 | return page; |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | static int erofs_map_blocks_flatmode(struct inode *inode, |
Bhagyashri P. Dighole | f0950b0 | 2018-11-05 19:48:38 +0000 | [diff] [blame] | 49 | struct erofs_map_blocks *map, |
| 50 | int flags) |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 51 | { |
Chen Gong | 9141b60 | 2018-09-18 22:27:28 +0800 | [diff] [blame] | 52 | int err = 0; |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 53 | erofs_blk_t nblocks, lastblk; |
| 54 | u64 offset = map->m_la; |
Gao Xiang | a5876e2 | 2019-09-04 10:08:56 +0800 | [diff] [blame] | 55 | struct erofs_inode *vi = EROFS_I(inode); |
Gao Xiang | 8a76568 | 2019-09-04 10:08:54 +0800 | [diff] [blame] | 56 | bool tailendpacking = (vi->datalayout == EROFS_INODE_FLAT_INLINE); |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 57 | |
Chao Yu | 13f06f4 | 2018-07-26 20:21:55 +0800 | [diff] [blame] | 58 | trace_erofs_map_blocks_flatmode_enter(inode, map, flags); |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 59 | |
| 60 | nblocks = DIV_ROUND_UP(inode->i_size, PAGE_SIZE); |
Gao Xiang | 8a76568 | 2019-09-04 10:08:54 +0800 | [diff] [blame] | 61 | lastblk = nblocks - tailendpacking; |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 62 | |
Gao Xiang | 8d8a09b | 2019-08-30 00:38:27 +0800 | [diff] [blame] | 63 | if (offset >= inode->i_size) { |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 64 | /* leave out-of-bound access unmapped */ |
| 65 | map->m_flags = 0; |
| 66 | map->m_plen = 0; |
| 67 | goto out; |
| 68 | } |
| 69 | |
| 70 | /* there is no hole in flatmode */ |
| 71 | map->m_flags = EROFS_MAP_MAPPED; |
| 72 | |
| 73 | if (offset < blknr_to_addr(lastblk)) { |
| 74 | map->m_pa = blknr_to_addr(vi->raw_blkaddr) + map->m_la; |
| 75 | map->m_plen = blknr_to_addr(lastblk) - offset; |
Gao Xiang | 8a76568 | 2019-09-04 10:08:54 +0800 | [diff] [blame] | 76 | } else if (tailendpacking) { |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 77 | /* 2 - inode inline B: inode, [xattrs], inline last blk... */ |
| 78 | struct erofs_sb_info *sbi = EROFS_SB(inode->i_sb); |
| 79 | |
| 80 | map->m_pa = iloc(sbi, vi->nid) + vi->inode_isize + |
| 81 | vi->xattr_isize + erofs_blkoff(map->m_la); |
| 82 | map->m_plen = inode->i_size - offset; |
| 83 | |
Gao Xiang | a6b9b1d | 2019-08-14 18:37:03 +0800 | [diff] [blame] | 84 | /* inline data should be located in one meta block */ |
Chen Gong | 9141b60 | 2018-09-18 22:27:28 +0800 | [diff] [blame] | 85 | if (erofs_blkoff(map->m_pa) + map->m_plen > PAGE_SIZE) { |
Gao Xiang | 4f761fa | 2019-09-04 10:09:09 +0800 | [diff] [blame] | 86 | erofs_err(inode->i_sb, |
| 87 | "inline data cross block boundary @ nid %llu", |
| 88 | vi->nid); |
Chen Gong | 9141b60 | 2018-09-18 22:27:28 +0800 | [diff] [blame] | 89 | DBG_BUGON(1); |
Gao Xiang | a6b9b1d | 2019-08-14 18:37:03 +0800 | [diff] [blame] | 90 | err = -EFSCORRUPTED; |
Chen Gong | 9141b60 | 2018-09-18 22:27:28 +0800 | [diff] [blame] | 91 | goto err_out; |
| 92 | } |
| 93 | |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 94 | map->m_flags |= EROFS_MAP_META; |
| 95 | } else { |
Gao Xiang | 4f761fa | 2019-09-04 10:09:09 +0800 | [diff] [blame] | 96 | erofs_err(inode->i_sb, |
| 97 | "internal error @ nid: %llu (size %llu), m_la 0x%llx", |
| 98 | vi->nid, inode->i_size, map->m_la); |
Chen Gong | 9141b60 | 2018-09-18 22:27:28 +0800 | [diff] [blame] | 99 | DBG_BUGON(1); |
| 100 | err = -EIO; |
| 101 | goto err_out; |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | out: |
| 105 | map->m_llen = map->m_plen; |
Chen Gong | 9141b60 | 2018-09-18 22:27:28 +0800 | [diff] [blame] | 106 | |
| 107 | err_out: |
Chao Yu | 13f06f4 | 2018-07-26 20:21:55 +0800 | [diff] [blame] | 108 | trace_erofs_map_blocks_flatmode_exit(inode, map, flags, 0); |
Chen Gong | 9141b60 | 2018-09-18 22:27:28 +0800 | [diff] [blame] | 109 | return err; |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | int erofs_map_blocks(struct inode *inode, |
Bhagyashri P. Dighole | f0950b0 | 2018-11-05 19:48:38 +0000 | [diff] [blame] | 113 | struct erofs_map_blocks *map, int flags) |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 114 | { |
Gao Xiang | a5876e2 | 2019-09-04 10:08:56 +0800 | [diff] [blame] | 115 | if (erofs_inode_is_data_compressed(EROFS_I(inode)->datalayout)) { |
Chao Yu | 3b42341 | 2019-01-15 09:42:21 +0800 | [diff] [blame] | 116 | int err = z_erofs_map_blocks_iter(inode, map, flags); |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 117 | |
Chao Yu | 3b42341 | 2019-01-15 09:42:21 +0800 | [diff] [blame] | 118 | if (map->mpage) { |
| 119 | put_page(map->mpage); |
| 120 | map->mpage = NULL; |
| 121 | } |
Gao Xiang | 02827e1 | 2018-07-26 20:21:58 +0800 | [diff] [blame] | 122 | return err; |
| 123 | } |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 124 | return erofs_map_blocks_flatmode(inode, map, flags); |
| 125 | } |
| 126 | |
Bhagyashri P. Dighole | f0950b0 | 2018-11-05 19:48:38 +0000 | [diff] [blame] | 127 | static inline struct bio *erofs_read_raw_page(struct bio *bio, |
| 128 | struct address_space *mapping, |
| 129 | struct page *page, |
| 130 | erofs_off_t *last_block, |
| 131 | unsigned int nblocks, |
| 132 | bool ra) |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 133 | { |
Gao Xiang | 14a56ec | 2019-03-25 11:40:09 +0800 | [diff] [blame] | 134 | struct inode *const inode = mapping->host; |
| 135 | struct super_block *const sb = inode->i_sb; |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 136 | erofs_off_t current_block = (erofs_off_t)page->index; |
| 137 | int err; |
| 138 | |
Chen Gong | 9141b60 | 2018-09-18 22:27:28 +0800 | [diff] [blame] | 139 | DBG_BUGON(!nblocks); |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 140 | |
| 141 | if (PageUptodate(page)) { |
| 142 | err = 0; |
| 143 | goto has_updated; |
| 144 | } |
| 145 | |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 146 | /* note that for readpage case, bio also equals to NULL */ |
Bhagyashri P. Dighole | d1ab8244 | 2018-11-05 19:49:05 +0000 | [diff] [blame] | 147 | if (bio && |
Bhagyashri P. Dighole | f0950b0 | 2018-11-05 19:48:38 +0000 | [diff] [blame] | 148 | /* not continuous */ |
| 149 | *last_block + 1 != current_block) { |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 150 | submit_bio_retry: |
Gao Xiang | 94e4e15 | 2019-09-04 10:09:04 +0800 | [diff] [blame] | 151 | submit_bio(bio); |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 152 | bio = NULL; |
| 153 | } |
| 154 | |
Bhagyashri P. Dighole | d1ab8244 | 2018-11-05 19:49:05 +0000 | [diff] [blame] | 155 | if (!bio) { |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 156 | struct erofs_map_blocks map = { |
| 157 | .m_la = blknr_to_addr(current_block), |
| 158 | }; |
Gao Xiang | 5544195 | 2018-07-26 20:22:00 +0800 | [diff] [blame] | 159 | erofs_blk_t blknr; |
Thomas Weißschuh | 7dd68b1 | 2018-09-10 21:41:14 +0200 | [diff] [blame] | 160 | unsigned int blkoff; |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 161 | |
| 162 | err = erofs_map_blocks(inode, &map, EROFS_GET_BLOCKS_RAW); |
Gao Xiang | 8d8a09b | 2019-08-30 00:38:27 +0800 | [diff] [blame] | 163 | if (err) |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 164 | goto err_out; |
| 165 | |
| 166 | /* zero out the holed page */ |
Gao Xiang | 8d8a09b | 2019-08-30 00:38:27 +0800 | [diff] [blame] | 167 | if (!(map.m_flags & EROFS_MAP_MAPPED)) { |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 168 | zero_user_segment(page, 0, PAGE_SIZE); |
| 169 | SetPageUptodate(page); |
| 170 | |
| 171 | /* imply err = 0, see erofs_map_blocks */ |
| 172 | goto has_updated; |
| 173 | } |
| 174 | |
| 175 | /* for RAW access mode, m_plen must be equal to m_llen */ |
Chen Gong | 9141b60 | 2018-09-18 22:27:28 +0800 | [diff] [blame] | 176 | DBG_BUGON(map.m_plen != map.m_llen); |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 177 | |
Gao Xiang | 5544195 | 2018-07-26 20:22:00 +0800 | [diff] [blame] | 178 | blknr = erofs_blknr(map.m_pa); |
| 179 | blkoff = erofs_blkoff(map.m_pa); |
| 180 | |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 181 | /* deal with inline page */ |
| 182 | if (map.m_flags & EROFS_MAP_META) { |
| 183 | void *vsrc, *vto; |
| 184 | struct page *ipage; |
| 185 | |
Chen Gong | 9141b60 | 2018-09-18 22:27:28 +0800 | [diff] [blame] | 186 | DBG_BUGON(map.m_plen > PAGE_SIZE); |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 187 | |
Gao Xiang | e655b5b | 2019-09-04 10:09:03 +0800 | [diff] [blame] | 188 | ipage = erofs_get_meta_page(inode->i_sb, blknr); |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 189 | |
| 190 | if (IS_ERR(ipage)) { |
| 191 | err = PTR_ERR(ipage); |
| 192 | goto err_out; |
| 193 | } |
| 194 | |
| 195 | vsrc = kmap_atomic(ipage); |
| 196 | vto = kmap_atomic(page); |
Gao Xiang | 5544195 | 2018-07-26 20:22:00 +0800 | [diff] [blame] | 197 | memcpy(vto, vsrc + blkoff, map.m_plen); |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 198 | memset(vto + map.m_plen, 0, PAGE_SIZE - map.m_plen); |
| 199 | kunmap_atomic(vto); |
| 200 | kunmap_atomic(vsrc); |
| 201 | flush_dcache_page(page); |
| 202 | |
| 203 | SetPageUptodate(page); |
| 204 | /* TODO: could we unlock the page earlier? */ |
| 205 | unlock_page(ipage); |
| 206 | put_page(ipage); |
| 207 | |
| 208 | /* imply err = 0, see erofs_map_blocks */ |
| 209 | goto has_updated; |
| 210 | } |
| 211 | |
| 212 | /* pa must be block-aligned for raw reading */ |
Chen Gong | 9141b60 | 2018-09-18 22:27:28 +0800 | [diff] [blame] | 213 | DBG_BUGON(erofs_blkoff(map.m_pa)); |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 214 | |
| 215 | /* max # of continuous pages */ |
| 216 | if (nblocks > DIV_ROUND_UP(map.m_plen, PAGE_SIZE)) |
| 217 | nblocks = DIV_ROUND_UP(map.m_plen, PAGE_SIZE); |
| 218 | if (nblocks > BIO_MAX_PAGES) |
| 219 | nblocks = BIO_MAX_PAGES; |
| 220 | |
Gao Xiang | 618f40e | 2019-09-04 10:09:12 +0800 | [diff] [blame] | 221 | bio = bio_alloc(GFP_NOIO, nblocks); |
| 222 | |
| 223 | bio->bi_end_io = erofs_readendio; |
| 224 | bio_set_dev(bio, sb->s_bdev); |
| 225 | bio->bi_iter.bi_sector = (sector_t)blknr << |
| 226 | LOG_SECTORS_PER_BLOCK; |
| 227 | bio->bi_opf = REQ_OP_READ; |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | err = bio_add_page(bio, page, PAGE_SIZE, 0); |
| 231 | /* out of the extent or bio is full */ |
| 232 | if (err < PAGE_SIZE) |
| 233 | goto submit_bio_retry; |
| 234 | |
| 235 | *last_block = current_block; |
| 236 | |
| 237 | /* shift in advance in case of it followed by too many gaps */ |
Gao Xiang | f4e97f5 | 2019-04-12 17:53:14 +0800 | [diff] [blame] | 238 | if (bio->bi_iter.bi_size >= bio->bi_max_vecs * PAGE_SIZE) { |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 239 | /* err should reassign to 0 after submitting */ |
| 240 | err = 0; |
| 241 | goto submit_bio_out; |
| 242 | } |
| 243 | |
| 244 | return bio; |
| 245 | |
| 246 | err_out: |
| 247 | /* for sync reading, set page error immediately */ |
| 248 | if (!ra) { |
| 249 | SetPageError(page); |
| 250 | ClearPageUptodate(page); |
| 251 | } |
| 252 | has_updated: |
| 253 | unlock_page(page); |
| 254 | |
| 255 | /* if updated manually, continuous pages has a gap */ |
Bhagyashri P. Dighole | d1ab8244 | 2018-11-05 19:49:05 +0000 | [diff] [blame] | 256 | if (bio) |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 257 | submit_bio_out: |
Gao Xiang | 94e4e15 | 2019-09-04 10:09:04 +0800 | [diff] [blame] | 258 | submit_bio(bio); |
Gao Xiang | 8d8a09b | 2019-08-30 00:38:27 +0800 | [diff] [blame] | 259 | return err ? ERR_PTR(err) : NULL; |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | /* |
| 263 | * since we dont have write or truncate flows, so no inode |
| 264 | * locking needs to be held at the moment. |
| 265 | */ |
| 266 | static int erofs_raw_access_readpage(struct file *file, struct page *page) |
| 267 | { |
Gao Xiang | 34f853b | 2020-05-28 04:48:44 -0400 | [diff] [blame] | 268 | erofs_off_t uninitialized_var(last_block); |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 269 | struct bio *bio; |
| 270 | |
Chao Yu | 13f06f4 | 2018-07-26 20:21:55 +0800 | [diff] [blame] | 271 | trace_erofs_readpage(page, true); |
| 272 | |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 273 | bio = erofs_read_raw_page(NULL, page->mapping, |
Bhagyashri P. Dighole | f0950b0 | 2018-11-05 19:48:38 +0000 | [diff] [blame] | 274 | page, &last_block, 1, false); |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 275 | |
| 276 | if (IS_ERR(bio)) |
| 277 | return PTR_ERR(bio); |
| 278 | |
Chen Gong | 9141b60 | 2018-09-18 22:27:28 +0800 | [diff] [blame] | 279 | DBG_BUGON(bio); /* since we have only one bio -- must be NULL */ |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 280 | return 0; |
| 281 | } |
| 282 | |
Matthew Wilcox (Oracle) | 0c07a9f | 2020-06-01 21:47:09 -0700 | [diff] [blame] | 283 | static void erofs_raw_access_readahead(struct readahead_control *rac) |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 284 | { |
Gao Xiang | 34f853b | 2020-05-28 04:48:44 -0400 | [diff] [blame] | 285 | erofs_off_t uninitialized_var(last_block); |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 286 | struct bio *bio = NULL; |
Matthew Wilcox (Oracle) | 0c07a9f | 2020-06-01 21:47:09 -0700 | [diff] [blame] | 287 | struct page *page; |
Chao Yu | 13f06f4 | 2018-07-26 20:21:55 +0800 | [diff] [blame] | 288 | |
Matthew Wilcox (Oracle) | 0c07a9f | 2020-06-01 21:47:09 -0700 | [diff] [blame] | 289 | trace_erofs_readpages(rac->mapping->host, readahead_index(rac), |
| 290 | readahead_count(rac), true); |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 291 | |
Matthew Wilcox (Oracle) | 0c07a9f | 2020-06-01 21:47:09 -0700 | [diff] [blame] | 292 | while ((page = readahead_page(rac))) { |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 293 | prefetchw(&page->flags); |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 294 | |
Matthew Wilcox (Oracle) | 0c07a9f | 2020-06-01 21:47:09 -0700 | [diff] [blame] | 295 | bio = erofs_read_raw_page(bio, rac->mapping, page, &last_block, |
| 296 | readahead_count(rac), true); |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 297 | |
Matthew Wilcox (Oracle) | 0c07a9f | 2020-06-01 21:47:09 -0700 | [diff] [blame] | 298 | /* all the page errors are ignored when readahead */ |
| 299 | if (IS_ERR(bio)) { |
| 300 | pr_err("%s, readahead error at page %lu of nid %llu\n", |
| 301 | __func__, page->index, |
| 302 | EROFS_I(rac->mapping->host)->nid); |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 303 | |
Matthew Wilcox (Oracle) | 0c07a9f | 2020-06-01 21:47:09 -0700 | [diff] [blame] | 304 | bio = NULL; |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 305 | } |
| 306 | |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 307 | put_page(page); |
| 308 | } |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 309 | |
| 310 | /* the rare case (end in gaps) */ |
Gao Xiang | 8d8a09b | 2019-08-30 00:38:27 +0800 | [diff] [blame] | 311 | if (bio) |
Gao Xiang | 94e4e15 | 2019-09-04 10:09:04 +0800 | [diff] [blame] | 312 | submit_bio(bio); |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 313 | } |
| 314 | |
Chao Yu | 9da681e | 2019-07-16 17:32:56 +0800 | [diff] [blame] | 315 | static int erofs_get_block(struct inode *inode, sector_t iblock, |
| 316 | struct buffer_head *bh, int create) |
| 317 | { |
| 318 | struct erofs_map_blocks map = { |
| 319 | .m_la = iblock << 9, |
| 320 | }; |
| 321 | int err; |
| 322 | |
| 323 | err = erofs_map_blocks(inode, &map, EROFS_GET_BLOCKS_RAW); |
| 324 | if (err) |
| 325 | return err; |
| 326 | |
| 327 | if (map.m_flags & EROFS_MAP_MAPPED) |
| 328 | bh->b_blocknr = erofs_blknr(map.m_pa); |
| 329 | |
| 330 | return err; |
| 331 | } |
| 332 | |
| 333 | static sector_t erofs_bmap(struct address_space *mapping, sector_t block) |
| 334 | { |
| 335 | struct inode *inode = mapping->host; |
| 336 | |
Gao Xiang | a5876e2 | 2019-09-04 10:08:56 +0800 | [diff] [blame] | 337 | if (EROFS_I(inode)->datalayout == EROFS_INODE_FLAT_INLINE) { |
Chao Yu | 9da681e | 2019-07-16 17:32:56 +0800 | [diff] [blame] | 338 | erofs_blk_t blks = i_size_read(inode) >> LOG_BLOCK_SIZE; |
| 339 | |
| 340 | if (block >> LOG_SECTORS_PER_BLOCK >= blks) |
| 341 | return 0; |
| 342 | } |
| 343 | |
| 344 | return generic_block_bmap(mapping, block, erofs_get_block); |
| 345 | } |
| 346 | |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 347 | /* for uncompressed (aligned) files and raw access for other files */ |
| 348 | const struct address_space_operations erofs_raw_access_aops = { |
| 349 | .readpage = erofs_raw_access_readpage, |
Matthew Wilcox (Oracle) | 0c07a9f | 2020-06-01 21:47:09 -0700 | [diff] [blame] | 350 | .readahead = erofs_raw_access_readahead, |
Chao Yu | 9da681e | 2019-07-16 17:32:56 +0800 | [diff] [blame] | 351 | .bmap = erofs_bmap, |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 352 | }; |
| 353 | |