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
erofs_unmap_metabuf(struct erofs_buf * buf)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
erofs_put_metabuf(struct erofs_buf * buf)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 */
erofs_bread(struct erofs_buf * buf,erofs_blk_t blkaddr,enum erofs_kmap_type type)34eb2c5e41SGao Xiang void *erofs_bread(struct erofs_buf *buf, erofs_blk_t blkaddr,
35eb2c5e41SGao Xiang enum erofs_kmap_type type)
36fdf80a47SGao Xiang {
37eb2c5e41SGao 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();
48eb2c5e41SGao 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
erofs_init_metabuf(struct erofs_buf * buf,struct super_block * sb)70eb2c5e41SGao Xiang void erofs_init_metabuf(struct erofs_buf *buf, struct super_block *sb)
71eb2c5e41SGao Xiang {
72eb2c5e41SGao Xiang if (erofs_is_fscache_mode(sb))
73eb2c5e41SGao Xiang buf->inode = EROFS_SB(sb)->s_fscache->inode;
74eb2c5e41SGao Xiang else
75eb2c5e41SGao Xiang buf->inode = sb->s_bdev->bd_inode;
76eb2c5e41SGao Xiang }
77eb2c5e41SGao Xiang
erofs_read_metabuf(struct erofs_buf * buf,struct super_block * sb,erofs_blk_t blkaddr,enum erofs_kmap_type type)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 {
81eb2c5e41SGao Xiang erofs_init_metabuf(buf, sb);
82eb2c5e41SGao Xiang return erofs_bread(buf, blkaddr, type);
83fe5de585SGao Xiang }
84fe5de585SGao Xiang
erofs_map_blocks_flatmode(struct inode * inode,struct erofs_map_blocks * map)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
erofs_map_blocks(struct inode * inode,struct erofs_map_blocks * map)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
erofs_map_dev(struct super_block * sb,struct erofs_map_dev * map)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 }
225*00432384SJingbo Xu map->m_bdev = dif->bdev_handle ? dif->bdev_handle->bdev : NULL;
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;
243*00432384SJingbo Xu map->m_bdev = dif->bdev_handle ?
244*00432384SJingbo Xu dif->bdev_handle->bdev : NULL;
245dfeab2e9SGao Xiang map->m_daxdev = dif->dax_dev;
246de205114SChristoph Hellwig map->m_dax_part_off = dif->dax_part_off;
247955b478eSJeffle Xu map->m_fscache = dif->fscache;
248dfeab2e9SGao Xiang break;
249dfeab2e9SGao Xiang }
250dfeab2e9SGao Xiang }
251dfeab2e9SGao Xiang up_read(&devs->rwsem);
252dfeab2e9SGao Xiang }
253dfeab2e9SGao Xiang return 0;
254dfeab2e9SGao Xiang }
255dfeab2e9SGao Xiang
erofs_iomap_begin(struct inode * inode,loff_t offset,loff_t length,unsigned int flags,struct iomap * iomap,struct iomap * srcmap)256a08e67a0SHuang Jianan static int erofs_iomap_begin(struct inode *inode, loff_t offset, loff_t length,
257a08e67a0SHuang Jianan unsigned int flags, struct iomap *iomap, struct iomap *srcmap)
258a08e67a0SHuang Jianan {
259a08e67a0SHuang Jianan int ret;
2603acea5fcSJingbo Xu struct super_block *sb = inode->i_sb;
261a08e67a0SHuang Jianan struct erofs_map_blocks map;
262dfeab2e9SGao Xiang struct erofs_map_dev mdev;
263a08e67a0SHuang Jianan
264a08e67a0SHuang Jianan map.m_la = offset;
265a08e67a0SHuang Jianan map.m_llen = length;
266a08e67a0SHuang Jianan
2678b58f9f0SJingbo Xu ret = erofs_map_blocks(inode, &map);
268a08e67a0SHuang Jianan if (ret < 0)
269a08e67a0SHuang Jianan return ret;
270a08e67a0SHuang Jianan
271dfeab2e9SGao Xiang mdev = (struct erofs_map_dev) {
272dfeab2e9SGao Xiang .m_deviceid = map.m_deviceid,
273dfeab2e9SGao Xiang .m_pa = map.m_pa,
274dfeab2e9SGao Xiang };
2753acea5fcSJingbo Xu ret = erofs_map_dev(sb, &mdev);
276dfeab2e9SGao Xiang if (ret)
277dfeab2e9SGao Xiang return ret;
278dfeab2e9SGao Xiang
279a08e67a0SHuang Jianan iomap->offset = map.m_la;
280e33f42b2SGao Xiang if (flags & IOMAP_DAX)
281de205114SChristoph Hellwig iomap->dax_dev = mdev.m_daxdev;
282e33f42b2SGao Xiang else
283de205114SChristoph Hellwig iomap->bdev = mdev.m_bdev;
284a08e67a0SHuang Jianan iomap->length = map.m_llen;
285a08e67a0SHuang Jianan iomap->flags = 0;
286771c994eSGao Xiang iomap->private = NULL;
287a08e67a0SHuang Jianan
288a08e67a0SHuang Jianan if (!(map.m_flags & EROFS_MAP_MAPPED)) {
289a08e67a0SHuang Jianan iomap->type = IOMAP_HOLE;
290a08e67a0SHuang Jianan iomap->addr = IOMAP_NULL_ADDR;
291a08e67a0SHuang Jianan if (!iomap->length)
292a08e67a0SHuang Jianan iomap->length = length;
293a08e67a0SHuang Jianan return 0;
294a08e67a0SHuang Jianan }
295a08e67a0SHuang Jianan
296a08e67a0SHuang Jianan if (map.m_flags & EROFS_MAP_META) {
297fdf80a47SGao Xiang void *ptr;
298fdf80a47SGao Xiang struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
299771c994eSGao Xiang
300771c994eSGao Xiang iomap->type = IOMAP_INLINE;
3013acea5fcSJingbo Xu ptr = erofs_read_metabuf(&buf, sb,
3023acea5fcSJingbo Xu erofs_blknr(sb, mdev.m_pa), EROFS_KMAP);
303fdf80a47SGao Xiang if (IS_ERR(ptr))
304fdf80a47SGao Xiang return PTR_ERR(ptr);
3053acea5fcSJingbo Xu iomap->inline_data = ptr + erofs_blkoff(sb, mdev.m_pa);
306fdf80a47SGao Xiang iomap->private = buf.base;
307771c994eSGao Xiang } else {
308a08e67a0SHuang Jianan iomap->type = IOMAP_MAPPED;
309dfeab2e9SGao Xiang iomap->addr = mdev.m_pa;
310e33f42b2SGao Xiang if (flags & IOMAP_DAX)
311e33f42b2SGao Xiang iomap->addr += mdev.m_dax_part_off;
312771c994eSGao Xiang }
313a08e67a0SHuang Jianan return 0;
314a08e67a0SHuang Jianan }
315a08e67a0SHuang Jianan
erofs_iomap_end(struct inode * inode,loff_t pos,loff_t length,ssize_t written,unsigned int flags,struct iomap * iomap)316771c994eSGao Xiang static int erofs_iomap_end(struct inode *inode, loff_t pos, loff_t length,
317771c994eSGao Xiang ssize_t written, unsigned int flags, struct iomap *iomap)
318771c994eSGao Xiang {
319fdf80a47SGao Xiang void *ptr = iomap->private;
320771c994eSGao Xiang
321fdf80a47SGao Xiang if (ptr) {
322fdf80a47SGao Xiang struct erofs_buf buf = {
323fdf80a47SGao Xiang .page = kmap_to_page(ptr),
324fdf80a47SGao Xiang .base = ptr,
325fdf80a47SGao Xiang .kmap_type = EROFS_KMAP,
326fdf80a47SGao Xiang };
327fdf80a47SGao Xiang
328771c994eSGao Xiang DBG_BUGON(iomap->type != IOMAP_INLINE);
329fdf80a47SGao Xiang erofs_put_metabuf(&buf);
330771c994eSGao Xiang } else {
331771c994eSGao Xiang DBG_BUGON(iomap->type == IOMAP_INLINE);
332771c994eSGao Xiang }
333771c994eSGao Xiang return written;
334771c994eSGao Xiang }
335771c994eSGao Xiang
336a08e67a0SHuang Jianan static const struct iomap_ops erofs_iomap_ops = {
337a08e67a0SHuang Jianan .iomap_begin = erofs_iomap_begin,
338771c994eSGao Xiang .iomap_end = erofs_iomap_end,
339a08e67a0SHuang Jianan };
340a08e67a0SHuang Jianan
erofs_fiemap(struct inode * inode,struct fiemap_extent_info * fieinfo,u64 start,u64 len)341eadcd6b5SGao Xiang int erofs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
342eadcd6b5SGao Xiang u64 start, u64 len)
343eadcd6b5SGao Xiang {
344eadcd6b5SGao Xiang if (erofs_inode_is_data_compressed(EROFS_I(inode)->datalayout)) {
345eadcd6b5SGao Xiang #ifdef CONFIG_EROFS_FS_ZIP
346eadcd6b5SGao Xiang return iomap_fiemap(inode, fieinfo, start, len,
347eadcd6b5SGao Xiang &z_erofs_iomap_report_ops);
348eadcd6b5SGao Xiang #else
349eadcd6b5SGao Xiang return -EOPNOTSUPP;
350eadcd6b5SGao Xiang #endif
351eadcd6b5SGao Xiang }
352eadcd6b5SGao Xiang return iomap_fiemap(inode, fieinfo, start, len, &erofs_iomap_ops);
353eadcd6b5SGao Xiang }
354eadcd6b5SGao Xiang
355771c994eSGao Xiang /*
356771c994eSGao Xiang * since we dont have write or truncate flows, so no inode
357771c994eSGao Xiang * locking needs to be held at the moment.
358771c994eSGao Xiang */
erofs_read_folio(struct file * file,struct folio * folio)3597479c505SMatthew Wilcox (Oracle) static int erofs_read_folio(struct file *file, struct folio *folio)
360771c994eSGao Xiang {
3617479c505SMatthew Wilcox (Oracle) return iomap_read_folio(folio, &erofs_iomap_ops);
362771c994eSGao Xiang }
363771c994eSGao Xiang
erofs_readahead(struct readahead_control * rac)364771c994eSGao Xiang static void erofs_readahead(struct readahead_control *rac)
365771c994eSGao Xiang {
366771c994eSGao Xiang return iomap_readahead(rac, &erofs_iomap_ops);
367771c994eSGao Xiang }
368771c994eSGao Xiang
erofs_bmap(struct address_space * mapping,sector_t block)369771c994eSGao Xiang static sector_t erofs_bmap(struct address_space *mapping, sector_t block)
370771c994eSGao Xiang {
371771c994eSGao Xiang return iomap_bmap(mapping, block, &erofs_iomap_ops);
372771c994eSGao Xiang }
373771c994eSGao Xiang
erofs_file_read_iter(struct kiocb * iocb,struct iov_iter * to)374a08e67a0SHuang Jianan static ssize_t erofs_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
375a08e67a0SHuang Jianan {
376de8a801aSGao Xiang struct inode *inode = file_inode(iocb->ki_filp);
377de8a801aSGao Xiang
378a08e67a0SHuang Jianan /* no need taking (shared) inode lock since it's a ro filesystem */
379a08e67a0SHuang Jianan if (!iov_iter_count(to))
380a08e67a0SHuang Jianan return 0;
381a08e67a0SHuang Jianan
38206252e9cSGao Xiang #ifdef CONFIG_FS_DAX
383de8a801aSGao Xiang if (IS_DAX(inode))
38406252e9cSGao Xiang return dax_iomap_rw(iocb, to, &erofs_iomap_ops);
38506252e9cSGao Xiang #endif
386a08e67a0SHuang Jianan if (iocb->ki_flags & IOCB_DIRECT) {
387de8a801aSGao Xiang struct block_device *bdev = inode->i_sb->s_bdev;
388de8a801aSGao Xiang unsigned int blksize_mask;
389a08e67a0SHuang Jianan
390de8a801aSGao Xiang if (bdev)
391de8a801aSGao Xiang blksize_mask = bdev_logical_block_size(bdev) - 1;
392de8a801aSGao Xiang else
3933993f4f4SYue Hu blksize_mask = i_blocksize(inode) - 1;
394de8a801aSGao Xiang
395de8a801aSGao Xiang if ((iocb->ki_pos | iov_iter_count(to) |
396de8a801aSGao Xiang iov_iter_alignment(to)) & blksize_mask)
397de8a801aSGao Xiang return -EINVAL;
398de8a801aSGao Xiang
399a08e67a0SHuang Jianan return iomap_dio_rw(iocb, to, &erofs_iomap_ops,
400786f847fSChristoph Hellwig NULL, 0, NULL, 0);
401a08e67a0SHuang Jianan }
402a08e67a0SHuang Jianan return filemap_read(iocb, to, 0);
403a08e67a0SHuang Jianan }
404a08e67a0SHuang Jianan
40547e4937aSGao Xiang /* for uncompressed (aligned) files and raw access for other files */
40647e4937aSGao Xiang const struct address_space_operations erofs_raw_access_aops = {
4077479c505SMatthew Wilcox (Oracle) .read_folio = erofs_read_folio,
408771c994eSGao Xiang .readahead = erofs_readahead,
40947e4937aSGao Xiang .bmap = erofs_bmap,
410a08e67a0SHuang Jianan .direct_IO = noop_direct_IO,
411ce529cc2SJingbo Xu .release_folio = iomap_release_folio,
412ce529cc2SJingbo Xu .invalidate_folio = iomap_invalidate_folio,
413a08e67a0SHuang Jianan };
414a08e67a0SHuang Jianan
41506252e9cSGao Xiang #ifdef CONFIG_FS_DAX
erofs_dax_huge_fault(struct vm_fault * vmf,unsigned int order)41606252e9cSGao Xiang static vm_fault_t erofs_dax_huge_fault(struct vm_fault *vmf,
4171d024e7aSMatthew Wilcox (Oracle) unsigned int order)
41806252e9cSGao Xiang {
4191d024e7aSMatthew Wilcox (Oracle) return dax_iomap_fault(vmf, order, NULL, NULL, &erofs_iomap_ops);
42006252e9cSGao Xiang }
42106252e9cSGao Xiang
erofs_dax_fault(struct vm_fault * vmf)42206252e9cSGao Xiang static vm_fault_t erofs_dax_fault(struct vm_fault *vmf)
42306252e9cSGao Xiang {
4241d024e7aSMatthew Wilcox (Oracle) return erofs_dax_huge_fault(vmf, 0);
42506252e9cSGao Xiang }
42606252e9cSGao Xiang
42706252e9cSGao Xiang static const struct vm_operations_struct erofs_dax_vm_ops = {
42806252e9cSGao Xiang .fault = erofs_dax_fault,
42906252e9cSGao Xiang .huge_fault = erofs_dax_huge_fault,
43006252e9cSGao Xiang };
43106252e9cSGao Xiang
erofs_file_mmap(struct file * file,struct vm_area_struct * vma)43206252e9cSGao Xiang static int erofs_file_mmap(struct file *file, struct vm_area_struct *vma)
43306252e9cSGao Xiang {
43406252e9cSGao Xiang if (!IS_DAX(file_inode(file)))
43506252e9cSGao Xiang return generic_file_readonly_mmap(file, vma);
43606252e9cSGao Xiang
43706252e9cSGao Xiang if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
43806252e9cSGao Xiang return -EINVAL;
43906252e9cSGao Xiang
44006252e9cSGao Xiang vma->vm_ops = &erofs_dax_vm_ops;
4411c71222eSSuren Baghdasaryan vm_flags_set(vma, VM_HUGEPAGE);
44206252e9cSGao Xiang return 0;
44306252e9cSGao Xiang }
44406252e9cSGao Xiang #else
44506252e9cSGao Xiang #define erofs_file_mmap generic_file_readonly_mmap
44606252e9cSGao Xiang #endif
44706252e9cSGao Xiang
448a08e67a0SHuang Jianan const struct file_operations erofs_file_fops = {
449a08e67a0SHuang Jianan .llseek = generic_file_llseek,
450a08e67a0SHuang Jianan .read_iter = erofs_file_read_iter,
45106252e9cSGao Xiang .mmap = erofs_file_mmap,
452b3b87525SGao Xiang .get_unmapped_area = thp_get_unmapped_area,
4532cb1e089SDavid Howells .splice_read = filemap_splice_read,
45447e4937aSGao Xiang };
455