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 30fe5de585SGao Xiang void *erofs_bread(struct erofs_buf *buf, struct inode *inode, 31fdf80a47SGao Xiang erofs_blk_t blkaddr, enum erofs_kmap_type type) 32fdf80a47SGao Xiang { 33fe5de585SGao Xiang struct address_space *const mapping = inode->i_mapping; 34fdf80a47SGao Xiang erofs_off_t offset = blknr_to_addr(blkaddr); 35fdf80a47SGao Xiang pgoff_t index = offset >> PAGE_SHIFT; 36fdf80a47SGao Xiang struct page *page = buf->page; 375375e7c8SJeffle Xu struct folio *folio; 385375e7c8SJeffle Xu unsigned int nofs_flag; 39fdf80a47SGao Xiang 40fdf80a47SGao Xiang if (!page || page->index != index) { 41fdf80a47SGao Xiang erofs_put_metabuf(buf); 425375e7c8SJeffle Xu 435375e7c8SJeffle Xu nofs_flag = memalloc_nofs_save(); 445375e7c8SJeffle Xu folio = read_cache_folio(mapping, index, NULL, NULL); 455375e7c8SJeffle Xu memalloc_nofs_restore(nofs_flag); 465375e7c8SJeffle Xu if (IS_ERR(folio)) 475375e7c8SJeffle Xu return folio; 485375e7c8SJeffle Xu 49fdf80a47SGao Xiang /* should already be PageUptodate, no need to lock page */ 505375e7c8SJeffle Xu page = folio_file_page(folio, index); 51fdf80a47SGao Xiang buf->page = page; 52fdf80a47SGao Xiang } 53fdf80a47SGao Xiang if (buf->kmap_type == EROFS_NO_KMAP) { 54fdf80a47SGao Xiang if (type == EROFS_KMAP) 55927e5010SGao Xiang buf->base = kmap_local_page(page); 56fdf80a47SGao Xiang buf->kmap_type = type; 57fdf80a47SGao Xiang } else if (buf->kmap_type != type) { 58fdf80a47SGao Xiang DBG_BUGON(1); 59fdf80a47SGao Xiang return ERR_PTR(-EFAULT); 60fdf80a47SGao Xiang } 61fdf80a47SGao Xiang if (type == EROFS_NO_KMAP) 62fdf80a47SGao Xiang return NULL; 63fdf80a47SGao Xiang return buf->base + (offset & ~PAGE_MASK); 64fdf80a47SGao Xiang } 65fdf80a47SGao Xiang 66fe5de585SGao Xiang void *erofs_read_metabuf(struct erofs_buf *buf, struct super_block *sb, 67fe5de585SGao Xiang erofs_blk_t blkaddr, enum erofs_kmap_type type) 68fe5de585SGao Xiang { 695375e7c8SJeffle Xu if (erofs_is_fscache_mode(sb)) 705375e7c8SJeffle Xu return erofs_bread(buf, EROFS_SB(sb)->s_fscache->inode, 715375e7c8SJeffle Xu blkaddr, type); 725375e7c8SJeffle Xu 73fe5de585SGao Xiang return erofs_bread(buf, sb->s_bdev->bd_inode, blkaddr, type); 74fe5de585SGao Xiang } 75fe5de585SGao Xiang 7647e4937aSGao Xiang static int erofs_map_blocks_flatmode(struct inode *inode, 778b58f9f0SJingbo Xu struct erofs_map_blocks *map) 7847e4937aSGao Xiang { 7947e4937aSGao Xiang erofs_blk_t nblocks, lastblk; 8047e4937aSGao Xiang u64 offset = map->m_la; 81a5876e24SGao Xiang struct erofs_inode *vi = EROFS_I(inode); 828a765682SGao Xiang bool tailendpacking = (vi->datalayout == EROFS_INODE_FLAT_INLINE); 8347e4937aSGao Xiang 84fdf80a47SGao Xiang nblocks = DIV_ROUND_UP(inode->i_size, EROFS_BLKSIZ); 858a765682SGao Xiang lastblk = nblocks - tailendpacking; 8647e4937aSGao Xiang 8747e4937aSGao Xiang /* there is no hole in flatmode */ 8847e4937aSGao Xiang map->m_flags = EROFS_MAP_MAPPED; 8947e4937aSGao Xiang if (offset < blknr_to_addr(lastblk)) { 9047e4937aSGao Xiang map->m_pa = blknr_to_addr(vi->raw_blkaddr) + map->m_la; 9147e4937aSGao Xiang map->m_plen = blknr_to_addr(lastblk) - offset; 928a765682SGao Xiang } else if (tailendpacking) { 93b780d3fcSGao Xiang map->m_pa = erofs_iloc(inode) + vi->inode_isize + 94b780d3fcSGao Xiang vi->xattr_isize + erofs_blkoff(offset); 9547e4937aSGao Xiang map->m_plen = inode->i_size - offset; 9647e4937aSGao Xiang 97469407a3SGao Xiang /* inline data should be located in the same meta block */ 98469407a3SGao Xiang if (erofs_blkoff(map->m_pa) + map->m_plen > EROFS_BLKSIZ) { 994f761fa2SGao Xiang erofs_err(inode->i_sb, 1004f761fa2SGao Xiang "inline data cross block boundary @ nid %llu", 10147e4937aSGao Xiang vi->nid); 10247e4937aSGao Xiang DBG_BUGON(1); 103469407a3SGao Xiang return -EFSCORRUPTED; 10447e4937aSGao Xiang } 10547e4937aSGao Xiang map->m_flags |= EROFS_MAP_META; 10647e4937aSGao Xiang } else { 1074f761fa2SGao Xiang erofs_err(inode->i_sb, 1084f761fa2SGao Xiang "internal error @ nid: %llu (size %llu), m_la 0x%llx", 10947e4937aSGao Xiang vi->nid, inode->i_size, map->m_la); 11047e4937aSGao Xiang DBG_BUGON(1); 111469407a3SGao Xiang return -EIO; 11247e4937aSGao Xiang } 113469407a3SGao Xiang return 0; 11447e4937aSGao Xiang } 11547e4937aSGao Xiang 1168b58f9f0SJingbo Xu int erofs_map_blocks(struct inode *inode, struct erofs_map_blocks *map) 117c5aa903aSGao Xiang { 118c5aa903aSGao Xiang struct super_block *sb = inode->i_sb; 119c5aa903aSGao Xiang struct erofs_inode *vi = EROFS_I(inode); 120c5aa903aSGao Xiang struct erofs_inode_chunk_index *idx; 121fdf80a47SGao Xiang struct erofs_buf buf = __EROFS_BUF_INITIALIZER; 122c5aa903aSGao Xiang u64 chunknr; 123c5aa903aSGao Xiang unsigned int unit; 124c5aa903aSGao Xiang erofs_off_t pos; 125fdf80a47SGao Xiang void *kaddr; 126c5aa903aSGao Xiang int err = 0; 127c5aa903aSGao Xiang 1288b58f9f0SJingbo Xu trace_erofs_map_blocks_enter(inode, map, 0); 129dfeab2e9SGao Xiang map->m_deviceid = 0; 130c5aa903aSGao Xiang if (map->m_la >= inode->i_size) { 131c5aa903aSGao Xiang /* leave out-of-bound access unmapped */ 132c5aa903aSGao Xiang map->m_flags = 0; 133c5aa903aSGao Xiang map->m_plen = 0; 134c5aa903aSGao Xiang goto out; 135c5aa903aSGao Xiang } 136c5aa903aSGao Xiang 137469407a3SGao Xiang if (vi->datalayout != EROFS_INODE_CHUNK_BASED) { 1388b58f9f0SJingbo Xu err = erofs_map_blocks_flatmode(inode, map); 139469407a3SGao Xiang goto out; 140469407a3SGao Xiang } 141c5aa903aSGao Xiang 142c5aa903aSGao Xiang if (vi->chunkformat & EROFS_CHUNK_FORMAT_INDEXES) 143c5aa903aSGao Xiang unit = sizeof(*idx); /* chunk index */ 144c5aa903aSGao Xiang else 145c5aa903aSGao Xiang unit = EROFS_BLOCK_MAP_ENTRY_SIZE; /* block map */ 146c5aa903aSGao Xiang 147c5aa903aSGao Xiang chunknr = map->m_la >> vi->chunkbits; 148b780d3fcSGao Xiang pos = ALIGN(erofs_iloc(inode) + vi->inode_isize + 149c5aa903aSGao Xiang vi->xattr_isize, unit) + unit * chunknr; 150c5aa903aSGao Xiang 151fdf80a47SGao Xiang kaddr = erofs_read_metabuf(&buf, sb, erofs_blknr(pos), EROFS_KMAP); 152fdf80a47SGao Xiang if (IS_ERR(kaddr)) { 153fdf80a47SGao Xiang err = PTR_ERR(kaddr); 154469407a3SGao Xiang goto out; 155469407a3SGao Xiang } 156c5aa903aSGao Xiang map->m_la = chunknr << vi->chunkbits; 157c5aa903aSGao Xiang map->m_plen = min_t(erofs_off_t, 1UL << vi->chunkbits, 158c5aa903aSGao Xiang roundup(inode->i_size - map->m_la, EROFS_BLKSIZ)); 159c5aa903aSGao Xiang 160c5aa903aSGao Xiang /* handle block map */ 161c5aa903aSGao Xiang if (!(vi->chunkformat & EROFS_CHUNK_FORMAT_INDEXES)) { 162fdf80a47SGao Xiang __le32 *blkaddr = kaddr + erofs_blkoff(pos); 163c5aa903aSGao Xiang 164c5aa903aSGao Xiang if (le32_to_cpu(*blkaddr) == EROFS_NULL_ADDR) { 165c5aa903aSGao Xiang map->m_flags = 0; 166c5aa903aSGao Xiang } else { 167c5aa903aSGao Xiang map->m_pa = blknr_to_addr(le32_to_cpu(*blkaddr)); 168c5aa903aSGao Xiang map->m_flags = EROFS_MAP_MAPPED; 169c5aa903aSGao Xiang } 170c5aa903aSGao Xiang goto out_unlock; 171c5aa903aSGao Xiang } 172c5aa903aSGao Xiang /* parse chunk indexes */ 173fdf80a47SGao Xiang idx = kaddr + erofs_blkoff(pos); 174c5aa903aSGao Xiang switch (le32_to_cpu(idx->blkaddr)) { 175c5aa903aSGao Xiang case EROFS_NULL_ADDR: 176c5aa903aSGao Xiang map->m_flags = 0; 177c5aa903aSGao Xiang break; 178c5aa903aSGao Xiang default: 179dfeab2e9SGao Xiang map->m_deviceid = le16_to_cpu(idx->device_id) & 180dfeab2e9SGao Xiang EROFS_SB(sb)->device_id_mask; 181c5aa903aSGao Xiang map->m_pa = blknr_to_addr(le32_to_cpu(idx->blkaddr)); 182c5aa903aSGao Xiang map->m_flags = EROFS_MAP_MAPPED; 183c5aa903aSGao Xiang break; 184c5aa903aSGao Xiang } 185c5aa903aSGao Xiang out_unlock: 186fdf80a47SGao Xiang erofs_put_metabuf(&buf); 187c5aa903aSGao Xiang out: 188469407a3SGao Xiang if (!err) 189c5aa903aSGao Xiang map->m_llen = map->m_plen; 1908b58f9f0SJingbo Xu trace_erofs_map_blocks_exit(inode, map, 0, err); 191c5aa903aSGao Xiang return err; 192c5aa903aSGao Xiang } 193c5aa903aSGao Xiang 194dfeab2e9SGao Xiang int erofs_map_dev(struct super_block *sb, struct erofs_map_dev *map) 195dfeab2e9SGao Xiang { 196dfeab2e9SGao Xiang struct erofs_dev_context *devs = EROFS_SB(sb)->devs; 197dfeab2e9SGao Xiang struct erofs_device_info *dif; 198dfeab2e9SGao Xiang int id; 199dfeab2e9SGao Xiang 200dfeab2e9SGao Xiang /* primary device by default */ 201dfeab2e9SGao Xiang map->m_bdev = sb->s_bdev; 202dfeab2e9SGao Xiang map->m_daxdev = EROFS_SB(sb)->dax_dev; 203de205114SChristoph Hellwig map->m_dax_part_off = EROFS_SB(sb)->dax_part_off; 204955b478eSJeffle Xu map->m_fscache = EROFS_SB(sb)->s_fscache; 205dfeab2e9SGao Xiang 206dfeab2e9SGao Xiang if (map->m_deviceid) { 207dfeab2e9SGao Xiang down_read(&devs->rwsem); 208dfeab2e9SGao Xiang dif = idr_find(&devs->tree, map->m_deviceid - 1); 209dfeab2e9SGao Xiang if (!dif) { 210dfeab2e9SGao Xiang up_read(&devs->rwsem); 211dfeab2e9SGao Xiang return -ENODEV; 212dfeab2e9SGao Xiang } 213dfeab2e9SGao Xiang map->m_bdev = dif->bdev; 214dfeab2e9SGao Xiang map->m_daxdev = dif->dax_dev; 215de205114SChristoph Hellwig map->m_dax_part_off = dif->dax_part_off; 216955b478eSJeffle Xu map->m_fscache = dif->fscache; 217dfeab2e9SGao Xiang up_read(&devs->rwsem); 218dfeab2e9SGao Xiang } else if (devs->extra_devices) { 219dfeab2e9SGao Xiang down_read(&devs->rwsem); 220dfeab2e9SGao Xiang idr_for_each_entry(&devs->tree, dif, id) { 221dfeab2e9SGao Xiang erofs_off_t startoff, length; 222dfeab2e9SGao Xiang 223dfeab2e9SGao Xiang if (!dif->mapped_blkaddr) 224dfeab2e9SGao Xiang continue; 225dfeab2e9SGao Xiang startoff = blknr_to_addr(dif->mapped_blkaddr); 226dfeab2e9SGao Xiang length = blknr_to_addr(dif->blocks); 227dfeab2e9SGao Xiang 228dfeab2e9SGao Xiang if (map->m_pa >= startoff && 229dfeab2e9SGao Xiang map->m_pa < startoff + length) { 230dfeab2e9SGao Xiang map->m_pa -= startoff; 231dfeab2e9SGao Xiang map->m_bdev = dif->bdev; 232dfeab2e9SGao Xiang map->m_daxdev = dif->dax_dev; 233de205114SChristoph Hellwig map->m_dax_part_off = dif->dax_part_off; 234955b478eSJeffle Xu map->m_fscache = dif->fscache; 235dfeab2e9SGao Xiang break; 236dfeab2e9SGao Xiang } 237dfeab2e9SGao Xiang } 238dfeab2e9SGao Xiang up_read(&devs->rwsem); 239dfeab2e9SGao Xiang } 240dfeab2e9SGao Xiang return 0; 241dfeab2e9SGao Xiang } 242dfeab2e9SGao Xiang 243a08e67a0SHuang Jianan static int erofs_iomap_begin(struct inode *inode, loff_t offset, loff_t length, 244a08e67a0SHuang Jianan unsigned int flags, struct iomap *iomap, struct iomap *srcmap) 245a08e67a0SHuang Jianan { 246a08e67a0SHuang Jianan int ret; 247a08e67a0SHuang Jianan struct erofs_map_blocks map; 248dfeab2e9SGao Xiang struct erofs_map_dev mdev; 249a08e67a0SHuang Jianan 250a08e67a0SHuang Jianan map.m_la = offset; 251a08e67a0SHuang Jianan map.m_llen = length; 252a08e67a0SHuang Jianan 2538b58f9f0SJingbo Xu ret = erofs_map_blocks(inode, &map); 254a08e67a0SHuang Jianan if (ret < 0) 255a08e67a0SHuang Jianan return ret; 256a08e67a0SHuang Jianan 257dfeab2e9SGao Xiang mdev = (struct erofs_map_dev) { 258dfeab2e9SGao Xiang .m_deviceid = map.m_deviceid, 259dfeab2e9SGao Xiang .m_pa = map.m_pa, 260dfeab2e9SGao Xiang }; 261dfeab2e9SGao Xiang ret = erofs_map_dev(inode->i_sb, &mdev); 262dfeab2e9SGao Xiang if (ret) 263dfeab2e9SGao Xiang return ret; 264dfeab2e9SGao Xiang 265a08e67a0SHuang Jianan iomap->offset = map.m_la; 266e33f42b2SGao Xiang if (flags & IOMAP_DAX) 267de205114SChristoph Hellwig iomap->dax_dev = mdev.m_daxdev; 268e33f42b2SGao Xiang else 269de205114SChristoph Hellwig iomap->bdev = mdev.m_bdev; 270a08e67a0SHuang Jianan iomap->length = map.m_llen; 271a08e67a0SHuang Jianan iomap->flags = 0; 272771c994eSGao Xiang iomap->private = NULL; 273a08e67a0SHuang Jianan 274a08e67a0SHuang Jianan if (!(map.m_flags & EROFS_MAP_MAPPED)) { 275a08e67a0SHuang Jianan iomap->type = IOMAP_HOLE; 276a08e67a0SHuang Jianan iomap->addr = IOMAP_NULL_ADDR; 277a08e67a0SHuang Jianan if (!iomap->length) 278a08e67a0SHuang Jianan iomap->length = length; 279a08e67a0SHuang Jianan return 0; 280a08e67a0SHuang Jianan } 281a08e67a0SHuang Jianan 282a08e67a0SHuang Jianan if (map.m_flags & EROFS_MAP_META) { 283fdf80a47SGao Xiang void *ptr; 284fdf80a47SGao Xiang struct erofs_buf buf = __EROFS_BUF_INITIALIZER; 285771c994eSGao Xiang 286771c994eSGao Xiang iomap->type = IOMAP_INLINE; 287fdf80a47SGao Xiang ptr = erofs_read_metabuf(&buf, inode->i_sb, 288fdf80a47SGao Xiang erofs_blknr(mdev.m_pa), EROFS_KMAP); 289fdf80a47SGao Xiang if (IS_ERR(ptr)) 290fdf80a47SGao Xiang return PTR_ERR(ptr); 291fdf80a47SGao Xiang iomap->inline_data = ptr + erofs_blkoff(mdev.m_pa); 292fdf80a47SGao Xiang iomap->private = buf.base; 293771c994eSGao Xiang } else { 294a08e67a0SHuang Jianan iomap->type = IOMAP_MAPPED; 295dfeab2e9SGao Xiang iomap->addr = mdev.m_pa; 296e33f42b2SGao Xiang if (flags & IOMAP_DAX) 297e33f42b2SGao Xiang iomap->addr += mdev.m_dax_part_off; 298771c994eSGao Xiang } 299a08e67a0SHuang Jianan return 0; 300a08e67a0SHuang Jianan } 301a08e67a0SHuang Jianan 302771c994eSGao Xiang static int erofs_iomap_end(struct inode *inode, loff_t pos, loff_t length, 303771c994eSGao Xiang ssize_t written, unsigned int flags, struct iomap *iomap) 304771c994eSGao Xiang { 305fdf80a47SGao Xiang void *ptr = iomap->private; 306771c994eSGao Xiang 307fdf80a47SGao Xiang if (ptr) { 308fdf80a47SGao Xiang struct erofs_buf buf = { 309fdf80a47SGao Xiang .page = kmap_to_page(ptr), 310fdf80a47SGao Xiang .base = ptr, 311fdf80a47SGao Xiang .kmap_type = EROFS_KMAP, 312fdf80a47SGao Xiang }; 313fdf80a47SGao Xiang 314771c994eSGao Xiang DBG_BUGON(iomap->type != IOMAP_INLINE); 315fdf80a47SGao Xiang erofs_put_metabuf(&buf); 316771c994eSGao Xiang } else { 317771c994eSGao Xiang DBG_BUGON(iomap->type == IOMAP_INLINE); 318771c994eSGao Xiang } 319771c994eSGao Xiang return written; 320771c994eSGao Xiang } 321771c994eSGao Xiang 322a08e67a0SHuang Jianan static const struct iomap_ops erofs_iomap_ops = { 323a08e67a0SHuang Jianan .iomap_begin = erofs_iomap_begin, 324771c994eSGao Xiang .iomap_end = erofs_iomap_end, 325a08e67a0SHuang Jianan }; 326a08e67a0SHuang Jianan 327eadcd6b5SGao Xiang int erofs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, 328eadcd6b5SGao Xiang u64 start, u64 len) 329eadcd6b5SGao Xiang { 330eadcd6b5SGao Xiang if (erofs_inode_is_data_compressed(EROFS_I(inode)->datalayout)) { 331eadcd6b5SGao Xiang #ifdef CONFIG_EROFS_FS_ZIP 332eadcd6b5SGao Xiang return iomap_fiemap(inode, fieinfo, start, len, 333eadcd6b5SGao Xiang &z_erofs_iomap_report_ops); 334eadcd6b5SGao Xiang #else 335eadcd6b5SGao Xiang return -EOPNOTSUPP; 336eadcd6b5SGao Xiang #endif 337eadcd6b5SGao Xiang } 338eadcd6b5SGao Xiang return iomap_fiemap(inode, fieinfo, start, len, &erofs_iomap_ops); 339eadcd6b5SGao Xiang } 340eadcd6b5SGao Xiang 341771c994eSGao Xiang /* 342771c994eSGao Xiang * since we dont have write or truncate flows, so no inode 343771c994eSGao Xiang * locking needs to be held at the moment. 344771c994eSGao Xiang */ 3457479c505SMatthew Wilcox (Oracle) static int erofs_read_folio(struct file *file, struct folio *folio) 346771c994eSGao Xiang { 3477479c505SMatthew Wilcox (Oracle) return iomap_read_folio(folio, &erofs_iomap_ops); 348771c994eSGao Xiang } 349771c994eSGao Xiang 350771c994eSGao Xiang static void erofs_readahead(struct readahead_control *rac) 351771c994eSGao Xiang { 352771c994eSGao Xiang return iomap_readahead(rac, &erofs_iomap_ops); 353771c994eSGao Xiang } 354771c994eSGao Xiang 355771c994eSGao Xiang static sector_t erofs_bmap(struct address_space *mapping, sector_t block) 356771c994eSGao Xiang { 357771c994eSGao Xiang return iomap_bmap(mapping, block, &erofs_iomap_ops); 358771c994eSGao Xiang } 359771c994eSGao Xiang 360a08e67a0SHuang Jianan static ssize_t erofs_file_read_iter(struct kiocb *iocb, struct iov_iter *to) 361a08e67a0SHuang Jianan { 362de8a801aSGao Xiang struct inode *inode = file_inode(iocb->ki_filp); 363de8a801aSGao Xiang 364a08e67a0SHuang Jianan /* no need taking (shared) inode lock since it's a ro filesystem */ 365a08e67a0SHuang Jianan if (!iov_iter_count(to)) 366a08e67a0SHuang Jianan return 0; 367a08e67a0SHuang Jianan 36806252e9cSGao Xiang #ifdef CONFIG_FS_DAX 369de8a801aSGao Xiang if (IS_DAX(inode)) 37006252e9cSGao Xiang return dax_iomap_rw(iocb, to, &erofs_iomap_ops); 37106252e9cSGao Xiang #endif 372a08e67a0SHuang Jianan if (iocb->ki_flags & IOCB_DIRECT) { 373de8a801aSGao Xiang struct block_device *bdev = inode->i_sb->s_bdev; 374de8a801aSGao Xiang unsigned int blksize_mask; 375a08e67a0SHuang Jianan 376de8a801aSGao Xiang if (bdev) 377de8a801aSGao Xiang blksize_mask = bdev_logical_block_size(bdev) - 1; 378de8a801aSGao Xiang else 379*3993f4f4SYue Hu blksize_mask = i_blocksize(inode) - 1; 380de8a801aSGao Xiang 381de8a801aSGao Xiang if ((iocb->ki_pos | iov_iter_count(to) | 382de8a801aSGao Xiang iov_iter_alignment(to)) & blksize_mask) 383de8a801aSGao Xiang return -EINVAL; 384de8a801aSGao Xiang 385a08e67a0SHuang Jianan return iomap_dio_rw(iocb, to, &erofs_iomap_ops, 386786f847fSChristoph Hellwig NULL, 0, NULL, 0); 387a08e67a0SHuang Jianan } 388a08e67a0SHuang Jianan return filemap_read(iocb, to, 0); 389a08e67a0SHuang Jianan } 390a08e67a0SHuang Jianan 39147e4937aSGao Xiang /* for uncompressed (aligned) files and raw access for other files */ 39247e4937aSGao Xiang const struct address_space_operations erofs_raw_access_aops = { 3937479c505SMatthew Wilcox (Oracle) .read_folio = erofs_read_folio, 394771c994eSGao Xiang .readahead = erofs_readahead, 39547e4937aSGao Xiang .bmap = erofs_bmap, 396a08e67a0SHuang Jianan .direct_IO = noop_direct_IO, 397ce529cc2SJingbo Xu .release_folio = iomap_release_folio, 398ce529cc2SJingbo Xu .invalidate_folio = iomap_invalidate_folio, 399a08e67a0SHuang Jianan }; 400a08e67a0SHuang Jianan 40106252e9cSGao Xiang #ifdef CONFIG_FS_DAX 40206252e9cSGao Xiang static vm_fault_t erofs_dax_huge_fault(struct vm_fault *vmf, 40306252e9cSGao Xiang enum page_entry_size pe_size) 40406252e9cSGao Xiang { 40506252e9cSGao Xiang return dax_iomap_fault(vmf, pe_size, NULL, NULL, &erofs_iomap_ops); 40606252e9cSGao Xiang } 40706252e9cSGao Xiang 40806252e9cSGao Xiang static vm_fault_t erofs_dax_fault(struct vm_fault *vmf) 40906252e9cSGao Xiang { 41006252e9cSGao Xiang return erofs_dax_huge_fault(vmf, PE_SIZE_PTE); 41106252e9cSGao Xiang } 41206252e9cSGao Xiang 41306252e9cSGao Xiang static const struct vm_operations_struct erofs_dax_vm_ops = { 41406252e9cSGao Xiang .fault = erofs_dax_fault, 41506252e9cSGao Xiang .huge_fault = erofs_dax_huge_fault, 41606252e9cSGao Xiang }; 41706252e9cSGao Xiang 41806252e9cSGao Xiang static int erofs_file_mmap(struct file *file, struct vm_area_struct *vma) 41906252e9cSGao Xiang { 42006252e9cSGao Xiang if (!IS_DAX(file_inode(file))) 42106252e9cSGao Xiang return generic_file_readonly_mmap(file, vma); 42206252e9cSGao Xiang 42306252e9cSGao Xiang if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE)) 42406252e9cSGao Xiang return -EINVAL; 42506252e9cSGao Xiang 42606252e9cSGao Xiang vma->vm_ops = &erofs_dax_vm_ops; 4271c71222eSSuren Baghdasaryan vm_flags_set(vma, VM_HUGEPAGE); 42806252e9cSGao Xiang return 0; 42906252e9cSGao Xiang } 43006252e9cSGao Xiang #else 43106252e9cSGao Xiang #define erofs_file_mmap generic_file_readonly_mmap 43206252e9cSGao Xiang #endif 43306252e9cSGao Xiang 434a08e67a0SHuang Jianan const struct file_operations erofs_file_fops = { 435a08e67a0SHuang Jianan .llseek = generic_file_llseek, 436a08e67a0SHuang Jianan .read_iter = erofs_file_read_iter, 43706252e9cSGao Xiang .mmap = erofs_file_mmap, 438a08e67a0SHuang Jianan .splice_read = generic_file_splice_read, 43947e4937aSGao Xiang }; 440