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. |
Alexander A. Klimov | 592e7cd | 2020-07-13 15:09:44 +0200 | [diff] [blame] | 4 | * https://www.huawei.com/ |
Gao Xiang | c5aa903 | 2021-08-20 18:00:19 +0800 | [diff] [blame] | 5 | * Copyright (C) 2021, Alibaba Cloud |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 6 | */ |
| 7 | #include "internal.h" |
| 8 | #include <linux/prefetch.h> |
Gao Xiang | 06252e9 | 2021-08-05 08:36:00 +0800 | [diff] [blame] | 9 | #include <linux/dax.h> |
Chao Yu | 13f06f4 | 2018-07-26 20:21:55 +0800 | [diff] [blame] | 10 | #include <trace/events/erofs.h> |
| 11 | |
Gao Xiang | e655b5b | 2019-09-04 10:09:03 +0800 | [diff] [blame] | 12 | 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] | 13 | { |
Gao Xiang | 55252ab | 2019-09-22 02:43:55 +0800 | [diff] [blame] | 14 | struct address_space *const mapping = sb->s_bdev->bd_inode->i_mapping; |
| 15 | struct page *page; |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 16 | |
Gao Xiang | 55252ab | 2019-09-22 02:43:55 +0800 | [diff] [blame] | 17 | page = read_cache_page_gfp(mapping, blkaddr, |
Gao Xiang | 618f40e | 2019-09-04 10:09:12 +0800 | [diff] [blame] | 18 | mapping_gfp_constraint(mapping, ~__GFP_FS)); |
Gao Xiang | 55252ab | 2019-09-22 02:43:55 +0800 | [diff] [blame] | 19 | /* should already be PageUptodate */ |
| 20 | if (!IS_ERR(page)) |
| 21 | lock_page(page); |
| 22 | return page; |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 23 | } |
| 24 | |
| 25 | static int erofs_map_blocks_flatmode(struct inode *inode, |
Bhagyashri P. Dighole | f0950b0 | 2018-11-05 19:48:38 +0000 | [diff] [blame] | 26 | struct erofs_map_blocks *map, |
| 27 | int flags) |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 28 | { |
| 29 | erofs_blk_t nblocks, lastblk; |
| 30 | u64 offset = map->m_la; |
Gao Xiang | a5876e2 | 2019-09-04 10:08:56 +0800 | [diff] [blame] | 31 | struct erofs_inode *vi = EROFS_I(inode); |
Gao Xiang | 8a76568 | 2019-09-04 10:08:54 +0800 | [diff] [blame] | 32 | bool tailendpacking = (vi->datalayout == EROFS_INODE_FLAT_INLINE); |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 33 | |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 34 | nblocks = DIV_ROUND_UP(inode->i_size, PAGE_SIZE); |
Gao Xiang | 8a76568 | 2019-09-04 10:08:54 +0800 | [diff] [blame] | 35 | lastblk = nblocks - tailendpacking; |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 36 | |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 37 | /* there is no hole in flatmode */ |
| 38 | map->m_flags = EROFS_MAP_MAPPED; |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 39 | if (offset < blknr_to_addr(lastblk)) { |
| 40 | map->m_pa = blknr_to_addr(vi->raw_blkaddr) + map->m_la; |
| 41 | map->m_plen = blknr_to_addr(lastblk) - offset; |
Gao Xiang | 8a76568 | 2019-09-04 10:08:54 +0800 | [diff] [blame] | 42 | } else if (tailendpacking) { |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 43 | /* 2 - inode inline B: inode, [xattrs], inline last blk... */ |
| 44 | struct erofs_sb_info *sbi = EROFS_SB(inode->i_sb); |
| 45 | |
| 46 | map->m_pa = iloc(sbi, vi->nid) + vi->inode_isize + |
| 47 | vi->xattr_isize + erofs_blkoff(map->m_la); |
| 48 | map->m_plen = inode->i_size - offset; |
| 49 | |
Gao Xiang | 469407a | 2021-12-09 09:29:18 +0800 | [diff] [blame^] | 50 | /* inline data should be located in the same meta block */ |
| 51 | if (erofs_blkoff(map->m_pa) + map->m_plen > EROFS_BLKSIZ) { |
Gao Xiang | 4f761fa | 2019-09-04 10:09:09 +0800 | [diff] [blame] | 52 | erofs_err(inode->i_sb, |
| 53 | "inline data cross block boundary @ nid %llu", |
| 54 | vi->nid); |
Chen Gong | 9141b60 | 2018-09-18 22:27:28 +0800 | [diff] [blame] | 55 | DBG_BUGON(1); |
Gao Xiang | 469407a | 2021-12-09 09:29:18 +0800 | [diff] [blame^] | 56 | return -EFSCORRUPTED; |
Chen Gong | 9141b60 | 2018-09-18 22:27:28 +0800 | [diff] [blame] | 57 | } |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 58 | map->m_flags |= EROFS_MAP_META; |
| 59 | } else { |
Gao Xiang | 4f761fa | 2019-09-04 10:09:09 +0800 | [diff] [blame] | 60 | erofs_err(inode->i_sb, |
| 61 | "internal error @ nid: %llu (size %llu), m_la 0x%llx", |
| 62 | vi->nid, inode->i_size, map->m_la); |
Chen Gong | 9141b60 | 2018-09-18 22:27:28 +0800 | [diff] [blame] | 63 | DBG_BUGON(1); |
Gao Xiang | 469407a | 2021-12-09 09:29:18 +0800 | [diff] [blame^] | 64 | return -EIO; |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 65 | } |
Gao Xiang | 469407a | 2021-12-09 09:29:18 +0800 | [diff] [blame^] | 66 | return 0; |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 67 | } |
| 68 | |
Gao Xiang | c5aa903 | 2021-08-20 18:00:19 +0800 | [diff] [blame] | 69 | static int erofs_map_blocks(struct inode *inode, |
| 70 | struct erofs_map_blocks *map, int flags) |
| 71 | { |
| 72 | struct super_block *sb = inode->i_sb; |
| 73 | struct erofs_inode *vi = EROFS_I(inode); |
| 74 | struct erofs_inode_chunk_index *idx; |
| 75 | struct page *page; |
| 76 | u64 chunknr; |
| 77 | unsigned int unit; |
| 78 | erofs_off_t pos; |
| 79 | int err = 0; |
| 80 | |
Gao Xiang | 469407a | 2021-12-09 09:29:18 +0800 | [diff] [blame^] | 81 | trace_erofs_map_blocks_enter(inode, map, flags); |
Gao Xiang | dfeab2e | 2021-10-14 16:10:10 +0800 | [diff] [blame] | 82 | map->m_deviceid = 0; |
Gao Xiang | c5aa903 | 2021-08-20 18:00:19 +0800 | [diff] [blame] | 83 | if (map->m_la >= inode->i_size) { |
| 84 | /* leave out-of-bound access unmapped */ |
| 85 | map->m_flags = 0; |
| 86 | map->m_plen = 0; |
| 87 | goto out; |
| 88 | } |
| 89 | |
Gao Xiang | 469407a | 2021-12-09 09:29:18 +0800 | [diff] [blame^] | 90 | if (vi->datalayout != EROFS_INODE_CHUNK_BASED) { |
| 91 | err = erofs_map_blocks_flatmode(inode, map, flags); |
| 92 | goto out; |
| 93 | } |
Gao Xiang | c5aa903 | 2021-08-20 18:00:19 +0800 | [diff] [blame] | 94 | |
| 95 | if (vi->chunkformat & EROFS_CHUNK_FORMAT_INDEXES) |
| 96 | unit = sizeof(*idx); /* chunk index */ |
| 97 | else |
| 98 | unit = EROFS_BLOCK_MAP_ENTRY_SIZE; /* block map */ |
| 99 | |
| 100 | chunknr = map->m_la >> vi->chunkbits; |
| 101 | pos = ALIGN(iloc(EROFS_SB(sb), vi->nid) + vi->inode_isize + |
| 102 | vi->xattr_isize, unit) + unit * chunknr; |
| 103 | |
| 104 | page = erofs_get_meta_page(inode->i_sb, erofs_blknr(pos)); |
Gao Xiang | 469407a | 2021-12-09 09:29:18 +0800 | [diff] [blame^] | 105 | if (IS_ERR(page)) { |
| 106 | err = PTR_ERR(page); |
| 107 | goto out; |
| 108 | } |
Gao Xiang | c5aa903 | 2021-08-20 18:00:19 +0800 | [diff] [blame] | 109 | map->m_la = chunknr << vi->chunkbits; |
| 110 | map->m_plen = min_t(erofs_off_t, 1UL << vi->chunkbits, |
| 111 | roundup(inode->i_size - map->m_la, EROFS_BLKSIZ)); |
| 112 | |
| 113 | /* handle block map */ |
| 114 | if (!(vi->chunkformat & EROFS_CHUNK_FORMAT_INDEXES)) { |
| 115 | __le32 *blkaddr = page_address(page) + erofs_blkoff(pos); |
| 116 | |
| 117 | if (le32_to_cpu(*blkaddr) == EROFS_NULL_ADDR) { |
| 118 | map->m_flags = 0; |
| 119 | } else { |
| 120 | map->m_pa = blknr_to_addr(le32_to_cpu(*blkaddr)); |
| 121 | map->m_flags = EROFS_MAP_MAPPED; |
| 122 | } |
| 123 | goto out_unlock; |
| 124 | } |
| 125 | /* parse chunk indexes */ |
| 126 | idx = page_address(page) + erofs_blkoff(pos); |
| 127 | switch (le32_to_cpu(idx->blkaddr)) { |
| 128 | case EROFS_NULL_ADDR: |
| 129 | map->m_flags = 0; |
| 130 | break; |
| 131 | default: |
Gao Xiang | dfeab2e | 2021-10-14 16:10:10 +0800 | [diff] [blame] | 132 | map->m_deviceid = le16_to_cpu(idx->device_id) & |
| 133 | EROFS_SB(sb)->device_id_mask; |
Gao Xiang | c5aa903 | 2021-08-20 18:00:19 +0800 | [diff] [blame] | 134 | map->m_pa = blknr_to_addr(le32_to_cpu(idx->blkaddr)); |
| 135 | map->m_flags = EROFS_MAP_MAPPED; |
| 136 | break; |
| 137 | } |
| 138 | out_unlock: |
| 139 | unlock_page(page); |
| 140 | put_page(page); |
| 141 | out: |
Gao Xiang | 469407a | 2021-12-09 09:29:18 +0800 | [diff] [blame^] | 142 | if (!err) |
| 143 | map->m_llen = map->m_plen; |
| 144 | trace_erofs_map_blocks_exit(inode, map, flags, 0); |
Gao Xiang | c5aa903 | 2021-08-20 18:00:19 +0800 | [diff] [blame] | 145 | return err; |
| 146 | } |
| 147 | |
Gao Xiang | dfeab2e | 2021-10-14 16:10:10 +0800 | [diff] [blame] | 148 | int erofs_map_dev(struct super_block *sb, struct erofs_map_dev *map) |
| 149 | { |
| 150 | struct erofs_dev_context *devs = EROFS_SB(sb)->devs; |
| 151 | struct erofs_device_info *dif; |
| 152 | int id; |
| 153 | |
| 154 | /* primary device by default */ |
| 155 | map->m_bdev = sb->s_bdev; |
| 156 | map->m_daxdev = EROFS_SB(sb)->dax_dev; |
| 157 | |
| 158 | if (map->m_deviceid) { |
| 159 | down_read(&devs->rwsem); |
| 160 | dif = idr_find(&devs->tree, map->m_deviceid - 1); |
| 161 | if (!dif) { |
| 162 | up_read(&devs->rwsem); |
| 163 | return -ENODEV; |
| 164 | } |
| 165 | map->m_bdev = dif->bdev; |
| 166 | map->m_daxdev = dif->dax_dev; |
| 167 | up_read(&devs->rwsem); |
| 168 | } else if (devs->extra_devices) { |
| 169 | down_read(&devs->rwsem); |
| 170 | idr_for_each_entry(&devs->tree, dif, id) { |
| 171 | erofs_off_t startoff, length; |
| 172 | |
| 173 | if (!dif->mapped_blkaddr) |
| 174 | continue; |
| 175 | startoff = blknr_to_addr(dif->mapped_blkaddr); |
| 176 | length = blknr_to_addr(dif->blocks); |
| 177 | |
| 178 | if (map->m_pa >= startoff && |
| 179 | map->m_pa < startoff + length) { |
| 180 | map->m_pa -= startoff; |
| 181 | map->m_bdev = dif->bdev; |
| 182 | map->m_daxdev = dif->dax_dev; |
| 183 | break; |
| 184 | } |
| 185 | } |
| 186 | up_read(&devs->rwsem); |
| 187 | } |
| 188 | return 0; |
| 189 | } |
| 190 | |
Huang Jianan | a08e67a | 2021-08-05 08:35:59 +0800 | [diff] [blame] | 191 | static int erofs_iomap_begin(struct inode *inode, loff_t offset, loff_t length, |
| 192 | unsigned int flags, struct iomap *iomap, struct iomap *srcmap) |
| 193 | { |
| 194 | int ret; |
| 195 | struct erofs_map_blocks map; |
Gao Xiang | dfeab2e | 2021-10-14 16:10:10 +0800 | [diff] [blame] | 196 | struct erofs_map_dev mdev; |
Huang Jianan | a08e67a | 2021-08-05 08:35:59 +0800 | [diff] [blame] | 197 | |
| 198 | map.m_la = offset; |
| 199 | map.m_llen = length; |
| 200 | |
Gao Xiang | c5aa903 | 2021-08-20 18:00:19 +0800 | [diff] [blame] | 201 | ret = erofs_map_blocks(inode, &map, EROFS_GET_BLOCKS_RAW); |
Huang Jianan | a08e67a | 2021-08-05 08:35:59 +0800 | [diff] [blame] | 202 | if (ret < 0) |
| 203 | return ret; |
| 204 | |
Gao Xiang | dfeab2e | 2021-10-14 16:10:10 +0800 | [diff] [blame] | 205 | mdev = (struct erofs_map_dev) { |
| 206 | .m_deviceid = map.m_deviceid, |
| 207 | .m_pa = map.m_pa, |
| 208 | }; |
| 209 | ret = erofs_map_dev(inode->i_sb, &mdev); |
| 210 | if (ret) |
| 211 | return ret; |
| 212 | |
| 213 | iomap->bdev = mdev.m_bdev; |
| 214 | iomap->dax_dev = mdev.m_daxdev; |
Huang Jianan | a08e67a | 2021-08-05 08:35:59 +0800 | [diff] [blame] | 215 | iomap->offset = map.m_la; |
| 216 | iomap->length = map.m_llen; |
| 217 | iomap->flags = 0; |
Gao Xiang | 771c994 | 2021-08-05 08:36:01 +0800 | [diff] [blame] | 218 | iomap->private = NULL; |
Huang Jianan | a08e67a | 2021-08-05 08:35:59 +0800 | [diff] [blame] | 219 | |
| 220 | if (!(map.m_flags & EROFS_MAP_MAPPED)) { |
| 221 | iomap->type = IOMAP_HOLE; |
| 222 | iomap->addr = IOMAP_NULL_ADDR; |
| 223 | if (!iomap->length) |
| 224 | iomap->length = length; |
| 225 | return 0; |
| 226 | } |
| 227 | |
Huang Jianan | a08e67a | 2021-08-05 08:35:59 +0800 | [diff] [blame] | 228 | if (map.m_flags & EROFS_MAP_META) { |
Gao Xiang | 771c994 | 2021-08-05 08:36:01 +0800 | [diff] [blame] | 229 | struct page *ipage; |
| 230 | |
| 231 | iomap->type = IOMAP_INLINE; |
| 232 | ipage = erofs_get_meta_page(inode->i_sb, |
Gao Xiang | dfeab2e | 2021-10-14 16:10:10 +0800 | [diff] [blame] | 233 | erofs_blknr(mdev.m_pa)); |
Gao Xiang | 771c994 | 2021-08-05 08:36:01 +0800 | [diff] [blame] | 234 | if (IS_ERR(ipage)) |
| 235 | return PTR_ERR(ipage); |
| 236 | iomap->inline_data = page_address(ipage) + |
Gao Xiang | dfeab2e | 2021-10-14 16:10:10 +0800 | [diff] [blame] | 237 | erofs_blkoff(mdev.m_pa); |
Gao Xiang | 771c994 | 2021-08-05 08:36:01 +0800 | [diff] [blame] | 238 | iomap->private = ipage; |
| 239 | } else { |
| 240 | iomap->type = IOMAP_MAPPED; |
Gao Xiang | dfeab2e | 2021-10-14 16:10:10 +0800 | [diff] [blame] | 241 | iomap->addr = mdev.m_pa; |
Huang Jianan | a08e67a | 2021-08-05 08:35:59 +0800 | [diff] [blame] | 242 | } |
Huang Jianan | a08e67a | 2021-08-05 08:35:59 +0800 | [diff] [blame] | 243 | return 0; |
| 244 | } |
| 245 | |
Gao Xiang | 771c994 | 2021-08-05 08:36:01 +0800 | [diff] [blame] | 246 | static int erofs_iomap_end(struct inode *inode, loff_t pos, loff_t length, |
| 247 | ssize_t written, unsigned int flags, struct iomap *iomap) |
| 248 | { |
| 249 | struct page *ipage = iomap->private; |
| 250 | |
| 251 | if (ipage) { |
| 252 | DBG_BUGON(iomap->type != IOMAP_INLINE); |
| 253 | unlock_page(ipage); |
| 254 | put_page(ipage); |
| 255 | } else { |
| 256 | DBG_BUGON(iomap->type == IOMAP_INLINE); |
| 257 | } |
| 258 | return written; |
| 259 | } |
| 260 | |
Huang Jianan | a08e67a | 2021-08-05 08:35:59 +0800 | [diff] [blame] | 261 | static const struct iomap_ops erofs_iomap_ops = { |
| 262 | .iomap_begin = erofs_iomap_begin, |
Gao Xiang | 771c994 | 2021-08-05 08:36:01 +0800 | [diff] [blame] | 263 | .iomap_end = erofs_iomap_end, |
Huang Jianan | a08e67a | 2021-08-05 08:35:59 +0800 | [diff] [blame] | 264 | }; |
| 265 | |
Gao Xiang | eadcd6b | 2021-08-13 13:29:31 +0800 | [diff] [blame] | 266 | int erofs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
| 267 | u64 start, u64 len) |
| 268 | { |
| 269 | if (erofs_inode_is_data_compressed(EROFS_I(inode)->datalayout)) { |
| 270 | #ifdef CONFIG_EROFS_FS_ZIP |
| 271 | return iomap_fiemap(inode, fieinfo, start, len, |
| 272 | &z_erofs_iomap_report_ops); |
| 273 | #else |
| 274 | return -EOPNOTSUPP; |
| 275 | #endif |
| 276 | } |
| 277 | return iomap_fiemap(inode, fieinfo, start, len, &erofs_iomap_ops); |
| 278 | } |
| 279 | |
Gao Xiang | 771c994 | 2021-08-05 08:36:01 +0800 | [diff] [blame] | 280 | /* |
| 281 | * since we dont have write or truncate flows, so no inode |
| 282 | * locking needs to be held at the moment. |
| 283 | */ |
| 284 | static int erofs_readpage(struct file *file, struct page *page) |
| 285 | { |
| 286 | return iomap_readpage(page, &erofs_iomap_ops); |
| 287 | } |
| 288 | |
| 289 | static void erofs_readahead(struct readahead_control *rac) |
| 290 | { |
| 291 | return iomap_readahead(rac, &erofs_iomap_ops); |
| 292 | } |
| 293 | |
| 294 | static sector_t erofs_bmap(struct address_space *mapping, sector_t block) |
| 295 | { |
| 296 | return iomap_bmap(mapping, block, &erofs_iomap_ops); |
| 297 | } |
| 298 | |
Huang Jianan | a08e67a | 2021-08-05 08:35:59 +0800 | [diff] [blame] | 299 | static int erofs_prepare_dio(struct kiocb *iocb, struct iov_iter *to) |
| 300 | { |
| 301 | struct inode *inode = file_inode(iocb->ki_filp); |
| 302 | loff_t align = iocb->ki_pos | iov_iter_count(to) | |
| 303 | iov_iter_alignment(to); |
| 304 | struct block_device *bdev = inode->i_sb->s_bdev; |
| 305 | unsigned int blksize_mask; |
| 306 | |
| 307 | if (bdev) |
| 308 | blksize_mask = (1 << ilog2(bdev_logical_block_size(bdev))) - 1; |
| 309 | else |
| 310 | blksize_mask = (1 << inode->i_blkbits) - 1; |
| 311 | |
| 312 | if (align & blksize_mask) |
| 313 | return -EINVAL; |
Huang Jianan | a08e67a | 2021-08-05 08:35:59 +0800 | [diff] [blame] | 314 | return 0; |
| 315 | } |
| 316 | |
| 317 | static ssize_t erofs_file_read_iter(struct kiocb *iocb, struct iov_iter *to) |
| 318 | { |
| 319 | /* no need taking (shared) inode lock since it's a ro filesystem */ |
| 320 | if (!iov_iter_count(to)) |
| 321 | return 0; |
| 322 | |
Gao Xiang | 06252e9 | 2021-08-05 08:36:00 +0800 | [diff] [blame] | 323 | #ifdef CONFIG_FS_DAX |
| 324 | if (IS_DAX(iocb->ki_filp->f_mapping->host)) |
| 325 | return dax_iomap_rw(iocb, to, &erofs_iomap_ops); |
| 326 | #endif |
Huang Jianan | a08e67a | 2021-08-05 08:35:59 +0800 | [diff] [blame] | 327 | if (iocb->ki_flags & IOCB_DIRECT) { |
| 328 | int err = erofs_prepare_dio(iocb, to); |
| 329 | |
| 330 | if (!err) |
| 331 | return iomap_dio_rw(iocb, to, &erofs_iomap_ops, |
Andreas Gruenbacher | 4fdccaa | 2021-07-24 12:26:41 +0200 | [diff] [blame] | 332 | NULL, 0, 0); |
Huang Jianan | a08e67a | 2021-08-05 08:35:59 +0800 | [diff] [blame] | 333 | if (err < 0) |
| 334 | return err; |
| 335 | } |
| 336 | return filemap_read(iocb, to, 0); |
| 337 | } |
| 338 | |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 339 | /* for uncompressed (aligned) files and raw access for other files */ |
| 340 | const struct address_space_operations erofs_raw_access_aops = { |
Gao Xiang | 771c994 | 2021-08-05 08:36:01 +0800 | [diff] [blame] | 341 | .readpage = erofs_readpage, |
| 342 | .readahead = erofs_readahead, |
Chao Yu | 9da681e | 2019-07-16 17:32:56 +0800 | [diff] [blame] | 343 | .bmap = erofs_bmap, |
Huang Jianan | a08e67a | 2021-08-05 08:35:59 +0800 | [diff] [blame] | 344 | .direct_IO = noop_direct_IO, |
| 345 | }; |
| 346 | |
Gao Xiang | 06252e9 | 2021-08-05 08:36:00 +0800 | [diff] [blame] | 347 | #ifdef CONFIG_FS_DAX |
| 348 | static vm_fault_t erofs_dax_huge_fault(struct vm_fault *vmf, |
| 349 | enum page_entry_size pe_size) |
| 350 | { |
| 351 | return dax_iomap_fault(vmf, pe_size, NULL, NULL, &erofs_iomap_ops); |
| 352 | } |
| 353 | |
| 354 | static vm_fault_t erofs_dax_fault(struct vm_fault *vmf) |
| 355 | { |
| 356 | return erofs_dax_huge_fault(vmf, PE_SIZE_PTE); |
| 357 | } |
| 358 | |
| 359 | static const struct vm_operations_struct erofs_dax_vm_ops = { |
| 360 | .fault = erofs_dax_fault, |
| 361 | .huge_fault = erofs_dax_huge_fault, |
| 362 | }; |
| 363 | |
| 364 | static int erofs_file_mmap(struct file *file, struct vm_area_struct *vma) |
| 365 | { |
| 366 | if (!IS_DAX(file_inode(file))) |
| 367 | return generic_file_readonly_mmap(file, vma); |
| 368 | |
| 369 | if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE)) |
| 370 | return -EINVAL; |
| 371 | |
| 372 | vma->vm_ops = &erofs_dax_vm_ops; |
| 373 | vma->vm_flags |= VM_HUGEPAGE; |
| 374 | return 0; |
| 375 | } |
| 376 | #else |
| 377 | #define erofs_file_mmap generic_file_readonly_mmap |
| 378 | #endif |
| 379 | |
Huang Jianan | a08e67a | 2021-08-05 08:35:59 +0800 | [diff] [blame] | 380 | const struct file_operations erofs_file_fops = { |
| 381 | .llseek = generic_file_llseek, |
| 382 | .read_iter = erofs_file_read_iter, |
Gao Xiang | 06252e9 | 2021-08-05 08:36:00 +0800 | [diff] [blame] | 383 | .mmap = erofs_file_mmap, |
Huang Jianan | a08e67a | 2021-08-05 08:35:59 +0800 | [diff] [blame] | 384 | .splice_read = generic_file_splice_read, |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 385 | }; |