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