147e4937aSGao Xiang // SPDX-License-Identifier: GPL-2.0-only 247e4937aSGao Xiang /* 347e4937aSGao Xiang * Copyright (C) 2017-2018 HUAWEI, Inc. 4592e7cd0SAlexander A. Klimov * https://www.huawei.com/ 5c5aa903aSGao Xiang * Copyright (C) 2021, Alibaba Cloud 647e4937aSGao Xiang */ 747e4937aSGao Xiang #include "internal.h" 847e4937aSGao Xiang #include <linux/prefetch.h> 95375e7c8SJeffle Xu #include <linux/sched/mm.h> 1006252e9cSGao Xiang #include <linux/dax.h> 1147e4937aSGao Xiang #include <trace/events/erofs.h> 1247e4937aSGao Xiang 13fdf80a47SGao Xiang void erofs_unmap_metabuf(struct erofs_buf *buf) 14fdf80a47SGao Xiang { 15fdf80a47SGao Xiang if (buf->kmap_type == EROFS_KMAP) 16927e5010SGao Xiang kunmap_local(buf->base); 17fdf80a47SGao Xiang buf->base = NULL; 18fdf80a47SGao Xiang buf->kmap_type = EROFS_NO_KMAP; 19fdf80a47SGao Xiang } 20fdf80a47SGao Xiang 21fdf80a47SGao Xiang void erofs_put_metabuf(struct erofs_buf *buf) 22fdf80a47SGao Xiang { 23fdf80a47SGao Xiang if (!buf->page) 24fdf80a47SGao Xiang return; 25fdf80a47SGao Xiang erofs_unmap_metabuf(buf); 26fdf80a47SGao Xiang put_page(buf->page); 27fdf80a47SGao Xiang buf->page = NULL; 28fdf80a47SGao Xiang } 29fdf80a47SGao Xiang 303acea5fcSJingbo Xu /* 313acea5fcSJingbo Xu * Derive the block size from inode->i_blkbits to make compatible with 323acea5fcSJingbo Xu * anonymous inode in fscache mode. 333acea5fcSJingbo Xu */ 34*eb2c5e41SGao Xiang void *erofs_bread(struct erofs_buf *buf, erofs_blk_t blkaddr, 35*eb2c5e41SGao Xiang enum erofs_kmap_type type) 36fdf80a47SGao Xiang { 37*eb2c5e41SGao Xiang struct inode *inode = buf->inode; 383acea5fcSJingbo Xu erofs_off_t offset = (erofs_off_t)blkaddr << inode->i_blkbits; 39fdf80a47SGao Xiang pgoff_t index = offset >> PAGE_SHIFT; 40fdf80a47SGao Xiang struct page *page = buf->page; 415375e7c8SJeffle Xu struct folio *folio; 425375e7c8SJeffle Xu unsigned int nofs_flag; 43fdf80a47SGao Xiang 44fdf80a47SGao Xiang if (!page || page->index != index) { 45fdf80a47SGao Xiang erofs_put_metabuf(buf); 465375e7c8SJeffle Xu 475375e7c8SJeffle Xu nofs_flag = memalloc_nofs_save(); 48*eb2c5e41SGao Xiang folio = read_cache_folio(inode->i_mapping, index, NULL, NULL); 495375e7c8SJeffle Xu memalloc_nofs_restore(nofs_flag); 505375e7c8SJeffle Xu if (IS_ERR(folio)) 515375e7c8SJeffle Xu return folio; 525375e7c8SJeffle Xu 53fdf80a47SGao Xiang /* should already be PageUptodate, no need to lock page */ 545375e7c8SJeffle Xu page = folio_file_page(folio, index); 55fdf80a47SGao Xiang buf->page = page; 56fdf80a47SGao Xiang } 57fdf80a47SGao Xiang if (buf->kmap_type == EROFS_NO_KMAP) { 58fdf80a47SGao Xiang if (type == EROFS_KMAP) 59927e5010SGao Xiang buf->base = kmap_local_page(page); 60fdf80a47SGao Xiang buf->kmap_type = type; 61fdf80a47SGao Xiang } else if (buf->kmap_type != type) { 62fdf80a47SGao Xiang DBG_BUGON(1); 63fdf80a47SGao Xiang return ERR_PTR(-EFAULT); 64fdf80a47SGao Xiang } 65fdf80a47SGao Xiang if (type == EROFS_NO_KMAP) 66fdf80a47SGao Xiang return NULL; 67fdf80a47SGao Xiang return buf->base + (offset & ~PAGE_MASK); 68fdf80a47SGao Xiang } 69fdf80a47SGao Xiang 70*eb2c5e41SGao Xiang void erofs_init_metabuf(struct erofs_buf *buf, struct super_block *sb) 71*eb2c5e41SGao Xiang { 72*eb2c5e41SGao Xiang if (erofs_is_fscache_mode(sb)) 73*eb2c5e41SGao Xiang buf->inode = EROFS_SB(sb)->s_fscache->inode; 74*eb2c5e41SGao Xiang else 75*eb2c5e41SGao Xiang buf->inode = sb->s_bdev->bd_inode; 76*eb2c5e41SGao Xiang } 77*eb2c5e41SGao Xiang 78fe5de585SGao Xiang void *erofs_read_metabuf(struct erofs_buf *buf, struct super_block *sb, 79fe5de585SGao Xiang erofs_blk_t blkaddr, enum erofs_kmap_type type) 80fe5de585SGao Xiang { 81*eb2c5e41SGao Xiang erofs_init_metabuf(buf, sb); 82*eb2c5e41SGao Xiang return erofs_bread(buf, blkaddr, type); 83fe5de585SGao Xiang } 84fe5de585SGao Xiang 8547e4937aSGao Xiang static int erofs_map_blocks_flatmode(struct inode *inode, 868b58f9f0SJingbo Xu struct erofs_map_blocks *map) 8747e4937aSGao Xiang { 8847e4937aSGao Xiang erofs_blk_t nblocks, lastblk; 8947e4937aSGao Xiang u64 offset = map->m_la; 90a5876e24SGao Xiang struct erofs_inode *vi = EROFS_I(inode); 913acea5fcSJingbo Xu struct super_block *sb = inode->i_sb; 928a765682SGao Xiang bool tailendpacking = (vi->datalayout == EROFS_INODE_FLAT_INLINE); 9347e4937aSGao Xiang 943acea5fcSJingbo Xu nblocks = erofs_iblks(inode); 958a765682SGao Xiang lastblk = nblocks - tailendpacking; 9647e4937aSGao Xiang 9747e4937aSGao Xiang /* there is no hole in flatmode */ 9847e4937aSGao Xiang map->m_flags = EROFS_MAP_MAPPED; 993acea5fcSJingbo Xu if (offset < erofs_pos(sb, lastblk)) { 1003acea5fcSJingbo Xu map->m_pa = erofs_pos(sb, vi->raw_blkaddr) + map->m_la; 1013acea5fcSJingbo Xu map->m_plen = erofs_pos(sb, lastblk) - offset; 1028a765682SGao Xiang } else if (tailendpacking) { 103b780d3fcSGao Xiang map->m_pa = erofs_iloc(inode) + vi->inode_isize + 1043acea5fcSJingbo Xu vi->xattr_isize + erofs_blkoff(sb, offset); 10547e4937aSGao Xiang map->m_plen = inode->i_size - offset; 10647e4937aSGao Xiang 107469407a3SGao Xiang /* inline data should be located in the same meta block */ 1083acea5fcSJingbo Xu if (erofs_blkoff(sb, map->m_pa) + map->m_plen > sb->s_blocksize) { 1093acea5fcSJingbo Xu erofs_err(sb, "inline data cross block boundary @ nid %llu", 11047e4937aSGao Xiang vi->nid); 11147e4937aSGao Xiang DBG_BUGON(1); 112469407a3SGao Xiang return -EFSCORRUPTED; 11347e4937aSGao Xiang } 11447e4937aSGao Xiang map->m_flags |= EROFS_MAP_META; 11547e4937aSGao Xiang } else { 1163acea5fcSJingbo Xu erofs_err(sb, "internal error @ nid: %llu (size %llu), m_la 0x%llx", 11747e4937aSGao Xiang vi->nid, inode->i_size, map->m_la); 11847e4937aSGao Xiang DBG_BUGON(1); 119469407a3SGao Xiang return -EIO; 12047e4937aSGao Xiang } 121469407a3SGao Xiang return 0; 12247e4937aSGao Xiang } 12347e4937aSGao Xiang 1248b58f9f0SJingbo Xu int erofs_map_blocks(struct inode *inode, struct erofs_map_blocks *map) 125c5aa903aSGao Xiang { 126c5aa903aSGao Xiang struct super_block *sb = inode->i_sb; 127c5aa903aSGao Xiang struct erofs_inode *vi = EROFS_I(inode); 128c5aa903aSGao Xiang struct erofs_inode_chunk_index *idx; 129fdf80a47SGao Xiang struct erofs_buf buf = __EROFS_BUF_INITIALIZER; 130c5aa903aSGao Xiang u64 chunknr; 131c5aa903aSGao Xiang unsigned int unit; 132c5aa903aSGao Xiang erofs_off_t pos; 133fdf80a47SGao Xiang void *kaddr; 134c5aa903aSGao Xiang int err = 0; 135c5aa903aSGao Xiang 1368b58f9f0SJingbo Xu trace_erofs_map_blocks_enter(inode, map, 0); 137dfeab2e9SGao Xiang map->m_deviceid = 0; 138c5aa903aSGao Xiang if (map->m_la >= inode->i_size) { 139c5aa903aSGao Xiang /* leave out-of-bound access unmapped */ 140c5aa903aSGao Xiang map->m_flags = 0; 141c5aa903aSGao Xiang map->m_plen = 0; 142c5aa903aSGao Xiang goto out; 143c5aa903aSGao Xiang } 144c5aa903aSGao Xiang 145469407a3SGao Xiang if (vi->datalayout != EROFS_INODE_CHUNK_BASED) { 1468b58f9f0SJingbo Xu err = erofs_map_blocks_flatmode(inode, map); 147469407a3SGao Xiang goto out; 148469407a3SGao Xiang } 149c5aa903aSGao Xiang 150c5aa903aSGao Xiang if (vi->chunkformat & EROFS_CHUNK_FORMAT_INDEXES) 151c5aa903aSGao Xiang unit = sizeof(*idx); /* chunk index */ 152c5aa903aSGao Xiang else 153c5aa903aSGao Xiang unit = EROFS_BLOCK_MAP_ENTRY_SIZE; /* block map */ 154c5aa903aSGao Xiang 155c5aa903aSGao Xiang chunknr = map->m_la >> vi->chunkbits; 156b780d3fcSGao Xiang pos = ALIGN(erofs_iloc(inode) + vi->inode_isize + 157c5aa903aSGao Xiang vi->xattr_isize, unit) + unit * chunknr; 158c5aa903aSGao Xiang 1593acea5fcSJingbo Xu kaddr = erofs_read_metabuf(&buf, sb, erofs_blknr(sb, pos), EROFS_KMAP); 160fdf80a47SGao Xiang if (IS_ERR(kaddr)) { 161fdf80a47SGao Xiang err = PTR_ERR(kaddr); 162469407a3SGao Xiang goto out; 163469407a3SGao Xiang } 164c5aa903aSGao Xiang map->m_la = chunknr << vi->chunkbits; 165c5aa903aSGao Xiang map->m_plen = min_t(erofs_off_t, 1UL << vi->chunkbits, 1663acea5fcSJingbo Xu round_up(inode->i_size - map->m_la, sb->s_blocksize)); 167c5aa903aSGao Xiang 168c5aa903aSGao Xiang /* handle block map */ 169c5aa903aSGao Xiang if (!(vi->chunkformat & EROFS_CHUNK_FORMAT_INDEXES)) { 1703acea5fcSJingbo Xu __le32 *blkaddr = kaddr + erofs_blkoff(sb, pos); 171c5aa903aSGao Xiang 172c5aa903aSGao Xiang if (le32_to_cpu(*blkaddr) == EROFS_NULL_ADDR) { 173c5aa903aSGao Xiang map->m_flags = 0; 174c5aa903aSGao Xiang } else { 1753acea5fcSJingbo Xu map->m_pa = erofs_pos(sb, le32_to_cpu(*blkaddr)); 176c5aa903aSGao Xiang map->m_flags = EROFS_MAP_MAPPED; 177c5aa903aSGao Xiang } 178c5aa903aSGao Xiang goto out_unlock; 179c5aa903aSGao Xiang } 180c5aa903aSGao Xiang /* parse chunk indexes */ 1813acea5fcSJingbo Xu idx = kaddr + erofs_blkoff(sb, pos); 182c5aa903aSGao Xiang switch (le32_to_cpu(idx->blkaddr)) { 183c5aa903aSGao Xiang case EROFS_NULL_ADDR: 184c5aa903aSGao Xiang map->m_flags = 0; 185c5aa903aSGao Xiang break; 186c5aa903aSGao Xiang default: 187dfeab2e9SGao Xiang map->m_deviceid = le16_to_cpu(idx->device_id) & 188dfeab2e9SGao Xiang EROFS_SB(sb)->device_id_mask; 1893acea5fcSJingbo Xu map->m_pa = erofs_pos(sb, le32_to_cpu(idx->blkaddr)); 190c5aa903aSGao Xiang map->m_flags = EROFS_MAP_MAPPED; 191c5aa903aSGao Xiang break; 192c5aa903aSGao Xiang } 193c5aa903aSGao Xiang out_unlock: 194fdf80a47SGao Xiang erofs_put_metabuf(&buf); 195c5aa903aSGao Xiang out: 196469407a3SGao Xiang if (!err) 197c5aa903aSGao Xiang map->m_llen = map->m_plen; 1988b58f9f0SJingbo Xu trace_erofs_map_blocks_exit(inode, map, 0, err); 199c5aa903aSGao Xiang return err; 200c5aa903aSGao Xiang } 201c5aa903aSGao Xiang 202dfeab2e9SGao Xiang int erofs_map_dev(struct super_block *sb, struct erofs_map_dev *map) 203dfeab2e9SGao Xiang { 204dfeab2e9SGao Xiang struct erofs_dev_context *devs = EROFS_SB(sb)->devs; 205dfeab2e9SGao Xiang struct erofs_device_info *dif; 206dfeab2e9SGao Xiang int id; 207dfeab2e9SGao Xiang 208dfeab2e9SGao Xiang map->m_bdev = sb->s_bdev; 209dfeab2e9SGao Xiang map->m_daxdev = EROFS_SB(sb)->dax_dev; 210de205114SChristoph Hellwig map->m_dax_part_off = EROFS_SB(sb)->dax_part_off; 211955b478eSJeffle Xu map->m_fscache = EROFS_SB(sb)->s_fscache; 212dfeab2e9SGao Xiang 213dfeab2e9SGao Xiang if (map->m_deviceid) { 214dfeab2e9SGao Xiang down_read(&devs->rwsem); 215dfeab2e9SGao Xiang dif = idr_find(&devs->tree, map->m_deviceid - 1); 216dfeab2e9SGao Xiang if (!dif) { 217dfeab2e9SGao Xiang up_read(&devs->rwsem); 218dfeab2e9SGao Xiang return -ENODEV; 219dfeab2e9SGao Xiang } 2208b465fecSJia Zhu if (devs->flatdev) { 2218b465fecSJia Zhu map->m_pa += erofs_pos(sb, dif->mapped_blkaddr); 2228b465fecSJia Zhu up_read(&devs->rwsem); 2238b465fecSJia Zhu return 0; 2248b465fecSJia Zhu } 225dfeab2e9SGao Xiang map->m_bdev = dif->bdev; 226dfeab2e9SGao Xiang map->m_daxdev = dif->dax_dev; 227de205114SChristoph Hellwig map->m_dax_part_off = dif->dax_part_off; 228955b478eSJeffle Xu map->m_fscache = dif->fscache; 229dfeab2e9SGao Xiang up_read(&devs->rwsem); 2308b465fecSJia Zhu } else if (devs->extra_devices && !devs->flatdev) { 231dfeab2e9SGao Xiang down_read(&devs->rwsem); 232dfeab2e9SGao Xiang idr_for_each_entry(&devs->tree, dif, id) { 233dfeab2e9SGao Xiang erofs_off_t startoff, length; 234dfeab2e9SGao Xiang 235dfeab2e9SGao Xiang if (!dif->mapped_blkaddr) 236dfeab2e9SGao Xiang continue; 2373acea5fcSJingbo Xu startoff = erofs_pos(sb, dif->mapped_blkaddr); 2383acea5fcSJingbo Xu length = erofs_pos(sb, dif->blocks); 239dfeab2e9SGao Xiang 240dfeab2e9SGao Xiang if (map->m_pa >= startoff && 241dfeab2e9SGao Xiang map->m_pa < startoff + length) { 242dfeab2e9SGao Xiang map->m_pa -= startoff; 243dfeab2e9SGao Xiang map->m_bdev = dif->bdev; 244dfeab2e9SGao Xiang map->m_daxdev = dif->dax_dev; 245de205114SChristoph Hellwig map->m_dax_part_off = dif->dax_part_off; 246955b478eSJeffle Xu map->m_fscache = dif->fscache; 247dfeab2e9SGao Xiang break; 248dfeab2e9SGao Xiang } 249dfeab2e9SGao Xiang } 250dfeab2e9SGao Xiang up_read(&devs->rwsem); 251dfeab2e9SGao Xiang } 252dfeab2e9SGao Xiang return 0; 253dfeab2e9SGao Xiang } 254dfeab2e9SGao Xiang 255a08e67a0SHuang Jianan static int erofs_iomap_begin(struct inode *inode, loff_t offset, loff_t length, 256a08e67a0SHuang Jianan unsigned int flags, struct iomap *iomap, struct iomap *srcmap) 257a08e67a0SHuang Jianan { 258a08e67a0SHuang Jianan int ret; 2593acea5fcSJingbo Xu struct super_block *sb = inode->i_sb; 260a08e67a0SHuang Jianan struct erofs_map_blocks map; 261dfeab2e9SGao Xiang struct erofs_map_dev mdev; 262a08e67a0SHuang Jianan 263a08e67a0SHuang Jianan map.m_la = offset; 264a08e67a0SHuang Jianan map.m_llen = length; 265a08e67a0SHuang Jianan 2668b58f9f0SJingbo Xu ret = erofs_map_blocks(inode, &map); 267a08e67a0SHuang Jianan if (ret < 0) 268a08e67a0SHuang Jianan return ret; 269a08e67a0SHuang Jianan 270dfeab2e9SGao Xiang mdev = (struct erofs_map_dev) { 271dfeab2e9SGao Xiang .m_deviceid = map.m_deviceid, 272dfeab2e9SGao Xiang .m_pa = map.m_pa, 273dfeab2e9SGao Xiang }; 2743acea5fcSJingbo Xu ret = erofs_map_dev(sb, &mdev); 275dfeab2e9SGao Xiang if (ret) 276dfeab2e9SGao Xiang return ret; 277dfeab2e9SGao Xiang 278a08e67a0SHuang Jianan iomap->offset = map.m_la; 279e33f42b2SGao Xiang if (flags & IOMAP_DAX) 280de205114SChristoph Hellwig iomap->dax_dev = mdev.m_daxdev; 281e33f42b2SGao Xiang else 282de205114SChristoph Hellwig iomap->bdev = mdev.m_bdev; 283a08e67a0SHuang Jianan iomap->length = map.m_llen; 284a08e67a0SHuang Jianan iomap->flags = 0; 285771c994eSGao Xiang iomap->private = NULL; 286a08e67a0SHuang Jianan 287a08e67a0SHuang Jianan if (!(map.m_flags & EROFS_MAP_MAPPED)) { 288a08e67a0SHuang Jianan iomap->type = IOMAP_HOLE; 289a08e67a0SHuang Jianan iomap->addr = IOMAP_NULL_ADDR; 290a08e67a0SHuang Jianan if (!iomap->length) 291a08e67a0SHuang Jianan iomap->length = length; 292a08e67a0SHuang Jianan return 0; 293a08e67a0SHuang Jianan } 294a08e67a0SHuang Jianan 295a08e67a0SHuang Jianan if (map.m_flags & EROFS_MAP_META) { 296fdf80a47SGao Xiang void *ptr; 297fdf80a47SGao Xiang struct erofs_buf buf = __EROFS_BUF_INITIALIZER; 298771c994eSGao Xiang 299771c994eSGao Xiang iomap->type = IOMAP_INLINE; 3003acea5fcSJingbo Xu ptr = erofs_read_metabuf(&buf, sb, 3013acea5fcSJingbo Xu erofs_blknr(sb, mdev.m_pa), EROFS_KMAP); 302fdf80a47SGao Xiang if (IS_ERR(ptr)) 303fdf80a47SGao Xiang return PTR_ERR(ptr); 3043acea5fcSJingbo Xu iomap->inline_data = ptr + erofs_blkoff(sb, mdev.m_pa); 305fdf80a47SGao Xiang iomap->private = buf.base; 306771c994eSGao Xiang } else { 307a08e67a0SHuang Jianan iomap->type = IOMAP_MAPPED; 308dfeab2e9SGao Xiang iomap->addr = mdev.m_pa; 309e33f42b2SGao Xiang if (flags & IOMAP_DAX) 310e33f42b2SGao Xiang iomap->addr += mdev.m_dax_part_off; 311771c994eSGao Xiang } 312a08e67a0SHuang Jianan return 0; 313a08e67a0SHuang Jianan } 314a08e67a0SHuang Jianan 315771c994eSGao Xiang static int erofs_iomap_end(struct inode *inode, loff_t pos, loff_t length, 316771c994eSGao Xiang ssize_t written, unsigned int flags, struct iomap *iomap) 317771c994eSGao Xiang { 318fdf80a47SGao Xiang void *ptr = iomap->private; 319771c994eSGao Xiang 320fdf80a47SGao Xiang if (ptr) { 321fdf80a47SGao Xiang struct erofs_buf buf = { 322fdf80a47SGao Xiang .page = kmap_to_page(ptr), 323fdf80a47SGao Xiang .base = ptr, 324fdf80a47SGao Xiang .kmap_type = EROFS_KMAP, 325fdf80a47SGao Xiang }; 326fdf80a47SGao Xiang 327771c994eSGao Xiang DBG_BUGON(iomap->type != IOMAP_INLINE); 328fdf80a47SGao Xiang erofs_put_metabuf(&buf); 329771c994eSGao Xiang } else { 330771c994eSGao Xiang DBG_BUGON(iomap->type == IOMAP_INLINE); 331771c994eSGao Xiang } 332771c994eSGao Xiang return written; 333771c994eSGao Xiang } 334771c994eSGao Xiang 335a08e67a0SHuang Jianan static const struct iomap_ops erofs_iomap_ops = { 336a08e67a0SHuang Jianan .iomap_begin = erofs_iomap_begin, 337771c994eSGao Xiang .iomap_end = erofs_iomap_end, 338a08e67a0SHuang Jianan }; 339a08e67a0SHuang Jianan 340eadcd6b5SGao Xiang int erofs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, 341eadcd6b5SGao Xiang u64 start, u64 len) 342eadcd6b5SGao Xiang { 343eadcd6b5SGao Xiang if (erofs_inode_is_data_compressed(EROFS_I(inode)->datalayout)) { 344eadcd6b5SGao Xiang #ifdef CONFIG_EROFS_FS_ZIP 345eadcd6b5SGao Xiang return iomap_fiemap(inode, fieinfo, start, len, 346eadcd6b5SGao Xiang &z_erofs_iomap_report_ops); 347eadcd6b5SGao Xiang #else 348eadcd6b5SGao Xiang return -EOPNOTSUPP; 349eadcd6b5SGao Xiang #endif 350eadcd6b5SGao Xiang } 351eadcd6b5SGao Xiang return iomap_fiemap(inode, fieinfo, start, len, &erofs_iomap_ops); 352eadcd6b5SGao Xiang } 353eadcd6b5SGao Xiang 354771c994eSGao Xiang /* 355771c994eSGao Xiang * since we dont have write or truncate flows, so no inode 356771c994eSGao Xiang * locking needs to be held at the moment. 357771c994eSGao Xiang */ 3587479c505SMatthew Wilcox (Oracle) static int erofs_read_folio(struct file *file, struct folio *folio) 359771c994eSGao Xiang { 3607479c505SMatthew Wilcox (Oracle) return iomap_read_folio(folio, &erofs_iomap_ops); 361771c994eSGao Xiang } 362771c994eSGao Xiang 363771c994eSGao Xiang static void erofs_readahead(struct readahead_control *rac) 364771c994eSGao Xiang { 365771c994eSGao Xiang return iomap_readahead(rac, &erofs_iomap_ops); 366771c994eSGao Xiang } 367771c994eSGao Xiang 368771c994eSGao Xiang static sector_t erofs_bmap(struct address_space *mapping, sector_t block) 369771c994eSGao Xiang { 370771c994eSGao Xiang return iomap_bmap(mapping, block, &erofs_iomap_ops); 371771c994eSGao Xiang } 372771c994eSGao Xiang 373a08e67a0SHuang Jianan static ssize_t erofs_file_read_iter(struct kiocb *iocb, struct iov_iter *to) 374a08e67a0SHuang Jianan { 375de8a801aSGao Xiang struct inode *inode = file_inode(iocb->ki_filp); 376de8a801aSGao Xiang 377a08e67a0SHuang Jianan /* no need taking (shared) inode lock since it's a ro filesystem */ 378a08e67a0SHuang Jianan if (!iov_iter_count(to)) 379a08e67a0SHuang Jianan return 0; 380a08e67a0SHuang Jianan 38106252e9cSGao Xiang #ifdef CONFIG_FS_DAX 382de8a801aSGao Xiang if (IS_DAX(inode)) 38306252e9cSGao Xiang return dax_iomap_rw(iocb, to, &erofs_iomap_ops); 38406252e9cSGao Xiang #endif 385a08e67a0SHuang Jianan if (iocb->ki_flags & IOCB_DIRECT) { 386de8a801aSGao Xiang struct block_device *bdev = inode->i_sb->s_bdev; 387de8a801aSGao Xiang unsigned int blksize_mask; 388a08e67a0SHuang Jianan 389de8a801aSGao Xiang if (bdev) 390de8a801aSGao Xiang blksize_mask = bdev_logical_block_size(bdev) - 1; 391de8a801aSGao Xiang else 3923993f4f4SYue Hu blksize_mask = i_blocksize(inode) - 1; 393de8a801aSGao Xiang 394de8a801aSGao Xiang if ((iocb->ki_pos | iov_iter_count(to) | 395de8a801aSGao Xiang iov_iter_alignment(to)) & blksize_mask) 396de8a801aSGao Xiang return -EINVAL; 397de8a801aSGao Xiang 398a08e67a0SHuang Jianan return iomap_dio_rw(iocb, to, &erofs_iomap_ops, 399786f847fSChristoph Hellwig NULL, 0, NULL, 0); 400a08e67a0SHuang Jianan } 401a08e67a0SHuang Jianan return filemap_read(iocb, to, 0); 402a08e67a0SHuang Jianan } 403a08e67a0SHuang Jianan 40447e4937aSGao Xiang /* for uncompressed (aligned) files and raw access for other files */ 40547e4937aSGao Xiang const struct address_space_operations erofs_raw_access_aops = { 4067479c505SMatthew Wilcox (Oracle) .read_folio = erofs_read_folio, 407771c994eSGao Xiang .readahead = erofs_readahead, 40847e4937aSGao Xiang .bmap = erofs_bmap, 409a08e67a0SHuang Jianan .direct_IO = noop_direct_IO, 410ce529cc2SJingbo Xu .release_folio = iomap_release_folio, 411ce529cc2SJingbo Xu .invalidate_folio = iomap_invalidate_folio, 412a08e67a0SHuang Jianan }; 413a08e67a0SHuang Jianan 41406252e9cSGao Xiang #ifdef CONFIG_FS_DAX 41506252e9cSGao Xiang static vm_fault_t erofs_dax_huge_fault(struct vm_fault *vmf, 41606252e9cSGao Xiang enum page_entry_size pe_size) 41706252e9cSGao Xiang { 41806252e9cSGao Xiang return dax_iomap_fault(vmf, pe_size, NULL, NULL, &erofs_iomap_ops); 41906252e9cSGao Xiang } 42006252e9cSGao Xiang 42106252e9cSGao Xiang static vm_fault_t erofs_dax_fault(struct vm_fault *vmf) 42206252e9cSGao Xiang { 42306252e9cSGao Xiang return erofs_dax_huge_fault(vmf, PE_SIZE_PTE); 42406252e9cSGao Xiang } 42506252e9cSGao Xiang 42606252e9cSGao Xiang static const struct vm_operations_struct erofs_dax_vm_ops = { 42706252e9cSGao Xiang .fault = erofs_dax_fault, 42806252e9cSGao Xiang .huge_fault = erofs_dax_huge_fault, 42906252e9cSGao Xiang }; 43006252e9cSGao Xiang 43106252e9cSGao Xiang static int erofs_file_mmap(struct file *file, struct vm_area_struct *vma) 43206252e9cSGao Xiang { 43306252e9cSGao Xiang if (!IS_DAX(file_inode(file))) 43406252e9cSGao Xiang return generic_file_readonly_mmap(file, vma); 43506252e9cSGao Xiang 43606252e9cSGao Xiang if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE)) 43706252e9cSGao Xiang return -EINVAL; 43806252e9cSGao Xiang 43906252e9cSGao Xiang vma->vm_ops = &erofs_dax_vm_ops; 4401c71222eSSuren Baghdasaryan vm_flags_set(vma, VM_HUGEPAGE); 44106252e9cSGao Xiang return 0; 44206252e9cSGao Xiang } 44306252e9cSGao Xiang #else 44406252e9cSGao Xiang #define erofs_file_mmap generic_file_readonly_mmap 44506252e9cSGao Xiang #endif 44606252e9cSGao Xiang 447a08e67a0SHuang Jianan const struct file_operations erofs_file_fops = { 448a08e67a0SHuang Jianan .llseek = generic_file_llseek, 449a08e67a0SHuang Jianan .read_iter = erofs_file_read_iter, 45006252e9cSGao Xiang .mmap = erofs_file_mmap, 451a08e67a0SHuang Jianan .splice_read = generic_file_splice_read, 45247e4937aSGao Xiang }; 453