xref: /openbmc/linux/fs/erofs/data.c (revision 3acea5fc)
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 
30*3acea5fcSJingbo Xu /*
31*3acea5fcSJingbo Xu  * Derive the block size from inode->i_blkbits to make compatible with
32*3acea5fcSJingbo Xu  * anonymous inode in fscache mode.
33*3acea5fcSJingbo Xu  */
34fe5de585SGao Xiang void *erofs_bread(struct erofs_buf *buf, struct inode *inode,
35fdf80a47SGao Xiang 		  erofs_blk_t blkaddr, enum erofs_kmap_type type)
36fdf80a47SGao Xiang {
37*3acea5fcSJingbo Xu 	erofs_off_t offset = (erofs_off_t)blkaddr << inode->i_blkbits;
38fe5de585SGao Xiang 	struct address_space *const mapping = inode->i_mapping;
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();
485375e7c8SJeffle Xu 		folio = read_cache_folio(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 
70fe5de585SGao Xiang void *erofs_read_metabuf(struct erofs_buf *buf, struct super_block *sb,
71fe5de585SGao Xiang 			 erofs_blk_t blkaddr, enum erofs_kmap_type type)
72fe5de585SGao Xiang {
735375e7c8SJeffle Xu 	if (erofs_is_fscache_mode(sb))
745375e7c8SJeffle Xu 		return erofs_bread(buf, EROFS_SB(sb)->s_fscache->inode,
755375e7c8SJeffle Xu 				   blkaddr, type);
765375e7c8SJeffle Xu 
77fe5de585SGao Xiang 	return erofs_bread(buf, sb->s_bdev->bd_inode, blkaddr, type);
78fe5de585SGao Xiang }
79fe5de585SGao Xiang 
8047e4937aSGao Xiang static int erofs_map_blocks_flatmode(struct inode *inode,
818b58f9f0SJingbo Xu 				     struct erofs_map_blocks *map)
8247e4937aSGao Xiang {
8347e4937aSGao Xiang 	erofs_blk_t nblocks, lastblk;
8447e4937aSGao Xiang 	u64 offset = map->m_la;
85a5876e24SGao Xiang 	struct erofs_inode *vi = EROFS_I(inode);
86*3acea5fcSJingbo Xu 	struct super_block *sb = inode->i_sb;
878a765682SGao Xiang 	bool tailendpacking = (vi->datalayout == EROFS_INODE_FLAT_INLINE);
8847e4937aSGao Xiang 
89*3acea5fcSJingbo Xu 	nblocks = erofs_iblks(inode);
908a765682SGao Xiang 	lastblk = nblocks - tailendpacking;
9147e4937aSGao Xiang 
9247e4937aSGao Xiang 	/* there is no hole in flatmode */
9347e4937aSGao Xiang 	map->m_flags = EROFS_MAP_MAPPED;
94*3acea5fcSJingbo Xu 	if (offset < erofs_pos(sb, lastblk)) {
95*3acea5fcSJingbo Xu 		map->m_pa = erofs_pos(sb, vi->raw_blkaddr) + map->m_la;
96*3acea5fcSJingbo Xu 		map->m_plen = erofs_pos(sb, lastblk) - offset;
978a765682SGao Xiang 	} else if (tailendpacking) {
98b780d3fcSGao Xiang 		map->m_pa = erofs_iloc(inode) + vi->inode_isize +
99*3acea5fcSJingbo Xu 			vi->xattr_isize + erofs_blkoff(sb, offset);
10047e4937aSGao Xiang 		map->m_plen = inode->i_size - offset;
10147e4937aSGao Xiang 
102469407a3SGao Xiang 		/* inline data should be located in the same meta block */
103*3acea5fcSJingbo Xu 		if (erofs_blkoff(sb, map->m_pa) + map->m_plen > sb->s_blocksize) {
104*3acea5fcSJingbo Xu 			erofs_err(sb, "inline data cross block boundary @ nid %llu",
10547e4937aSGao Xiang 				  vi->nid);
10647e4937aSGao Xiang 			DBG_BUGON(1);
107469407a3SGao Xiang 			return -EFSCORRUPTED;
10847e4937aSGao Xiang 		}
10947e4937aSGao Xiang 		map->m_flags |= EROFS_MAP_META;
11047e4937aSGao Xiang 	} else {
111*3acea5fcSJingbo Xu 		erofs_err(sb, "internal error @ nid: %llu (size %llu), m_la 0x%llx",
11247e4937aSGao Xiang 			  vi->nid, inode->i_size, map->m_la);
11347e4937aSGao Xiang 		DBG_BUGON(1);
114469407a3SGao Xiang 		return -EIO;
11547e4937aSGao Xiang 	}
116469407a3SGao Xiang 	return 0;
11747e4937aSGao Xiang }
11847e4937aSGao Xiang 
1198b58f9f0SJingbo Xu int erofs_map_blocks(struct inode *inode, struct erofs_map_blocks *map)
120c5aa903aSGao Xiang {
121c5aa903aSGao Xiang 	struct super_block *sb = inode->i_sb;
122c5aa903aSGao Xiang 	struct erofs_inode *vi = EROFS_I(inode);
123c5aa903aSGao Xiang 	struct erofs_inode_chunk_index *idx;
124fdf80a47SGao Xiang 	struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
125c5aa903aSGao Xiang 	u64 chunknr;
126c5aa903aSGao Xiang 	unsigned int unit;
127c5aa903aSGao Xiang 	erofs_off_t pos;
128fdf80a47SGao Xiang 	void *kaddr;
129c5aa903aSGao Xiang 	int err = 0;
130c5aa903aSGao Xiang 
1318b58f9f0SJingbo Xu 	trace_erofs_map_blocks_enter(inode, map, 0);
132dfeab2e9SGao Xiang 	map->m_deviceid = 0;
133c5aa903aSGao Xiang 	if (map->m_la >= inode->i_size) {
134c5aa903aSGao Xiang 		/* leave out-of-bound access unmapped */
135c5aa903aSGao Xiang 		map->m_flags = 0;
136c5aa903aSGao Xiang 		map->m_plen = 0;
137c5aa903aSGao Xiang 		goto out;
138c5aa903aSGao Xiang 	}
139c5aa903aSGao Xiang 
140469407a3SGao Xiang 	if (vi->datalayout != EROFS_INODE_CHUNK_BASED) {
1418b58f9f0SJingbo Xu 		err = erofs_map_blocks_flatmode(inode, map);
142469407a3SGao Xiang 		goto out;
143469407a3SGao Xiang 	}
144c5aa903aSGao Xiang 
145c5aa903aSGao Xiang 	if (vi->chunkformat & EROFS_CHUNK_FORMAT_INDEXES)
146c5aa903aSGao Xiang 		unit = sizeof(*idx);			/* chunk index */
147c5aa903aSGao Xiang 	else
148c5aa903aSGao Xiang 		unit = EROFS_BLOCK_MAP_ENTRY_SIZE;	/* block map */
149c5aa903aSGao Xiang 
150c5aa903aSGao Xiang 	chunknr = map->m_la >> vi->chunkbits;
151b780d3fcSGao Xiang 	pos = ALIGN(erofs_iloc(inode) + vi->inode_isize +
152c5aa903aSGao Xiang 		    vi->xattr_isize, unit) + unit * chunknr;
153c5aa903aSGao Xiang 
154*3acea5fcSJingbo Xu 	kaddr = erofs_read_metabuf(&buf, sb, erofs_blknr(sb, pos), EROFS_KMAP);
155fdf80a47SGao Xiang 	if (IS_ERR(kaddr)) {
156fdf80a47SGao Xiang 		err = PTR_ERR(kaddr);
157469407a3SGao Xiang 		goto out;
158469407a3SGao Xiang 	}
159c5aa903aSGao Xiang 	map->m_la = chunknr << vi->chunkbits;
160c5aa903aSGao Xiang 	map->m_plen = min_t(erofs_off_t, 1UL << vi->chunkbits,
161*3acea5fcSJingbo Xu 			round_up(inode->i_size - map->m_la, sb->s_blocksize));
162c5aa903aSGao Xiang 
163c5aa903aSGao Xiang 	/* handle block map */
164c5aa903aSGao Xiang 	if (!(vi->chunkformat & EROFS_CHUNK_FORMAT_INDEXES)) {
165*3acea5fcSJingbo Xu 		__le32 *blkaddr = kaddr + erofs_blkoff(sb, pos);
166c5aa903aSGao Xiang 
167c5aa903aSGao Xiang 		if (le32_to_cpu(*blkaddr) == EROFS_NULL_ADDR) {
168c5aa903aSGao Xiang 			map->m_flags = 0;
169c5aa903aSGao Xiang 		} else {
170*3acea5fcSJingbo Xu 			map->m_pa = erofs_pos(sb, le32_to_cpu(*blkaddr));
171c5aa903aSGao Xiang 			map->m_flags = EROFS_MAP_MAPPED;
172c5aa903aSGao Xiang 		}
173c5aa903aSGao Xiang 		goto out_unlock;
174c5aa903aSGao Xiang 	}
175c5aa903aSGao Xiang 	/* parse chunk indexes */
176*3acea5fcSJingbo Xu 	idx = kaddr + erofs_blkoff(sb, pos);
177c5aa903aSGao Xiang 	switch (le32_to_cpu(idx->blkaddr)) {
178c5aa903aSGao Xiang 	case EROFS_NULL_ADDR:
179c5aa903aSGao Xiang 		map->m_flags = 0;
180c5aa903aSGao Xiang 		break;
181c5aa903aSGao Xiang 	default:
182dfeab2e9SGao Xiang 		map->m_deviceid = le16_to_cpu(idx->device_id) &
183dfeab2e9SGao Xiang 			EROFS_SB(sb)->device_id_mask;
184*3acea5fcSJingbo Xu 		map->m_pa = erofs_pos(sb, le32_to_cpu(idx->blkaddr));
185c5aa903aSGao Xiang 		map->m_flags = EROFS_MAP_MAPPED;
186c5aa903aSGao Xiang 		break;
187c5aa903aSGao Xiang 	}
188c5aa903aSGao Xiang out_unlock:
189fdf80a47SGao Xiang 	erofs_put_metabuf(&buf);
190c5aa903aSGao Xiang out:
191469407a3SGao Xiang 	if (!err)
192c5aa903aSGao Xiang 		map->m_llen = map->m_plen;
1938b58f9f0SJingbo Xu 	trace_erofs_map_blocks_exit(inode, map, 0, err);
194c5aa903aSGao Xiang 	return err;
195c5aa903aSGao Xiang }
196c5aa903aSGao Xiang 
197dfeab2e9SGao Xiang int erofs_map_dev(struct super_block *sb, struct erofs_map_dev *map)
198dfeab2e9SGao Xiang {
199dfeab2e9SGao Xiang 	struct erofs_dev_context *devs = EROFS_SB(sb)->devs;
200dfeab2e9SGao Xiang 	struct erofs_device_info *dif;
201dfeab2e9SGao Xiang 	int id;
202dfeab2e9SGao Xiang 
203dfeab2e9SGao Xiang 	/* primary device by default */
204dfeab2e9SGao Xiang 	map->m_bdev = sb->s_bdev;
205dfeab2e9SGao Xiang 	map->m_daxdev = EROFS_SB(sb)->dax_dev;
206de205114SChristoph Hellwig 	map->m_dax_part_off = EROFS_SB(sb)->dax_part_off;
207955b478eSJeffle Xu 	map->m_fscache = EROFS_SB(sb)->s_fscache;
208dfeab2e9SGao Xiang 
209dfeab2e9SGao Xiang 	if (map->m_deviceid) {
210dfeab2e9SGao Xiang 		down_read(&devs->rwsem);
211dfeab2e9SGao Xiang 		dif = idr_find(&devs->tree, map->m_deviceid - 1);
212dfeab2e9SGao Xiang 		if (!dif) {
213dfeab2e9SGao Xiang 			up_read(&devs->rwsem);
214dfeab2e9SGao Xiang 			return -ENODEV;
215dfeab2e9SGao Xiang 		}
216dfeab2e9SGao Xiang 		map->m_bdev = dif->bdev;
217dfeab2e9SGao Xiang 		map->m_daxdev = dif->dax_dev;
218de205114SChristoph Hellwig 		map->m_dax_part_off = dif->dax_part_off;
219955b478eSJeffle Xu 		map->m_fscache = dif->fscache;
220dfeab2e9SGao Xiang 		up_read(&devs->rwsem);
221dfeab2e9SGao Xiang 	} else if (devs->extra_devices) {
222dfeab2e9SGao Xiang 		down_read(&devs->rwsem);
223dfeab2e9SGao Xiang 		idr_for_each_entry(&devs->tree, dif, id) {
224dfeab2e9SGao Xiang 			erofs_off_t startoff, length;
225dfeab2e9SGao Xiang 
226dfeab2e9SGao Xiang 			if (!dif->mapped_blkaddr)
227dfeab2e9SGao Xiang 				continue;
228*3acea5fcSJingbo Xu 			startoff = erofs_pos(sb, dif->mapped_blkaddr);
229*3acea5fcSJingbo Xu 			length = erofs_pos(sb, dif->blocks);
230dfeab2e9SGao Xiang 
231dfeab2e9SGao Xiang 			if (map->m_pa >= startoff &&
232dfeab2e9SGao Xiang 			    map->m_pa < startoff + length) {
233dfeab2e9SGao Xiang 				map->m_pa -= startoff;
234dfeab2e9SGao Xiang 				map->m_bdev = dif->bdev;
235dfeab2e9SGao Xiang 				map->m_daxdev = dif->dax_dev;
236de205114SChristoph Hellwig 				map->m_dax_part_off = dif->dax_part_off;
237955b478eSJeffle Xu 				map->m_fscache = dif->fscache;
238dfeab2e9SGao Xiang 				break;
239dfeab2e9SGao Xiang 			}
240dfeab2e9SGao Xiang 		}
241dfeab2e9SGao Xiang 		up_read(&devs->rwsem);
242dfeab2e9SGao Xiang 	}
243dfeab2e9SGao Xiang 	return 0;
244dfeab2e9SGao Xiang }
245dfeab2e9SGao Xiang 
246a08e67a0SHuang Jianan static int erofs_iomap_begin(struct inode *inode, loff_t offset, loff_t length,
247a08e67a0SHuang Jianan 		unsigned int flags, struct iomap *iomap, struct iomap *srcmap)
248a08e67a0SHuang Jianan {
249a08e67a0SHuang Jianan 	int ret;
250*3acea5fcSJingbo Xu 	struct super_block *sb = inode->i_sb;
251a08e67a0SHuang Jianan 	struct erofs_map_blocks map;
252dfeab2e9SGao Xiang 	struct erofs_map_dev mdev;
253a08e67a0SHuang Jianan 
254a08e67a0SHuang Jianan 	map.m_la = offset;
255a08e67a0SHuang Jianan 	map.m_llen = length;
256a08e67a0SHuang Jianan 
2578b58f9f0SJingbo Xu 	ret = erofs_map_blocks(inode, &map);
258a08e67a0SHuang Jianan 	if (ret < 0)
259a08e67a0SHuang Jianan 		return ret;
260a08e67a0SHuang Jianan 
261dfeab2e9SGao Xiang 	mdev = (struct erofs_map_dev) {
262dfeab2e9SGao Xiang 		.m_deviceid = map.m_deviceid,
263dfeab2e9SGao Xiang 		.m_pa = map.m_pa,
264dfeab2e9SGao Xiang 	};
265*3acea5fcSJingbo Xu 	ret = erofs_map_dev(sb, &mdev);
266dfeab2e9SGao Xiang 	if (ret)
267dfeab2e9SGao Xiang 		return ret;
268dfeab2e9SGao Xiang 
269a08e67a0SHuang Jianan 	iomap->offset = map.m_la;
270e33f42b2SGao Xiang 	if (flags & IOMAP_DAX)
271de205114SChristoph Hellwig 		iomap->dax_dev = mdev.m_daxdev;
272e33f42b2SGao Xiang 	else
273de205114SChristoph Hellwig 		iomap->bdev = mdev.m_bdev;
274a08e67a0SHuang Jianan 	iomap->length = map.m_llen;
275a08e67a0SHuang Jianan 	iomap->flags = 0;
276771c994eSGao Xiang 	iomap->private = NULL;
277a08e67a0SHuang Jianan 
278a08e67a0SHuang Jianan 	if (!(map.m_flags & EROFS_MAP_MAPPED)) {
279a08e67a0SHuang Jianan 		iomap->type = IOMAP_HOLE;
280a08e67a0SHuang Jianan 		iomap->addr = IOMAP_NULL_ADDR;
281a08e67a0SHuang Jianan 		if (!iomap->length)
282a08e67a0SHuang Jianan 			iomap->length = length;
283a08e67a0SHuang Jianan 		return 0;
284a08e67a0SHuang Jianan 	}
285a08e67a0SHuang Jianan 
286a08e67a0SHuang Jianan 	if (map.m_flags & EROFS_MAP_META) {
287fdf80a47SGao Xiang 		void *ptr;
288fdf80a47SGao Xiang 		struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
289771c994eSGao Xiang 
290771c994eSGao Xiang 		iomap->type = IOMAP_INLINE;
291*3acea5fcSJingbo Xu 		ptr = erofs_read_metabuf(&buf, sb,
292*3acea5fcSJingbo Xu 				erofs_blknr(sb, mdev.m_pa), EROFS_KMAP);
293fdf80a47SGao Xiang 		if (IS_ERR(ptr))
294fdf80a47SGao Xiang 			return PTR_ERR(ptr);
295*3acea5fcSJingbo Xu 		iomap->inline_data = ptr + erofs_blkoff(sb, mdev.m_pa);
296fdf80a47SGao Xiang 		iomap->private = buf.base;
297771c994eSGao Xiang 	} else {
298a08e67a0SHuang Jianan 		iomap->type = IOMAP_MAPPED;
299dfeab2e9SGao Xiang 		iomap->addr = mdev.m_pa;
300e33f42b2SGao Xiang 		if (flags & IOMAP_DAX)
301e33f42b2SGao Xiang 			iomap->addr += mdev.m_dax_part_off;
302771c994eSGao Xiang 	}
303a08e67a0SHuang Jianan 	return 0;
304a08e67a0SHuang Jianan }
305a08e67a0SHuang Jianan 
306771c994eSGao Xiang static int erofs_iomap_end(struct inode *inode, loff_t pos, loff_t length,
307771c994eSGao Xiang 		ssize_t written, unsigned int flags, struct iomap *iomap)
308771c994eSGao Xiang {
309fdf80a47SGao Xiang 	void *ptr = iomap->private;
310771c994eSGao Xiang 
311fdf80a47SGao Xiang 	if (ptr) {
312fdf80a47SGao Xiang 		struct erofs_buf buf = {
313fdf80a47SGao Xiang 			.page = kmap_to_page(ptr),
314fdf80a47SGao Xiang 			.base = ptr,
315fdf80a47SGao Xiang 			.kmap_type = EROFS_KMAP,
316fdf80a47SGao Xiang 		};
317fdf80a47SGao Xiang 
318771c994eSGao Xiang 		DBG_BUGON(iomap->type != IOMAP_INLINE);
319fdf80a47SGao Xiang 		erofs_put_metabuf(&buf);
320771c994eSGao Xiang 	} else {
321771c994eSGao Xiang 		DBG_BUGON(iomap->type == IOMAP_INLINE);
322771c994eSGao Xiang 	}
323771c994eSGao Xiang 	return written;
324771c994eSGao Xiang }
325771c994eSGao Xiang 
326a08e67a0SHuang Jianan static const struct iomap_ops erofs_iomap_ops = {
327a08e67a0SHuang Jianan 	.iomap_begin = erofs_iomap_begin,
328771c994eSGao Xiang 	.iomap_end = erofs_iomap_end,
329a08e67a0SHuang Jianan };
330a08e67a0SHuang Jianan 
331eadcd6b5SGao Xiang int erofs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
332eadcd6b5SGao Xiang 		 u64 start, u64 len)
333eadcd6b5SGao Xiang {
334eadcd6b5SGao Xiang 	if (erofs_inode_is_data_compressed(EROFS_I(inode)->datalayout)) {
335eadcd6b5SGao Xiang #ifdef CONFIG_EROFS_FS_ZIP
336eadcd6b5SGao Xiang 		return iomap_fiemap(inode, fieinfo, start, len,
337eadcd6b5SGao Xiang 				    &z_erofs_iomap_report_ops);
338eadcd6b5SGao Xiang #else
339eadcd6b5SGao Xiang 		return -EOPNOTSUPP;
340eadcd6b5SGao Xiang #endif
341eadcd6b5SGao Xiang 	}
342eadcd6b5SGao Xiang 	return iomap_fiemap(inode, fieinfo, start, len, &erofs_iomap_ops);
343eadcd6b5SGao Xiang }
344eadcd6b5SGao Xiang 
345771c994eSGao Xiang /*
346771c994eSGao Xiang  * since we dont have write or truncate flows, so no inode
347771c994eSGao Xiang  * locking needs to be held at the moment.
348771c994eSGao Xiang  */
3497479c505SMatthew Wilcox (Oracle) static int erofs_read_folio(struct file *file, struct folio *folio)
350771c994eSGao Xiang {
3517479c505SMatthew Wilcox (Oracle) 	return iomap_read_folio(folio, &erofs_iomap_ops);
352771c994eSGao Xiang }
353771c994eSGao Xiang 
354771c994eSGao Xiang static void erofs_readahead(struct readahead_control *rac)
355771c994eSGao Xiang {
356771c994eSGao Xiang 	return iomap_readahead(rac, &erofs_iomap_ops);
357771c994eSGao Xiang }
358771c994eSGao Xiang 
359771c994eSGao Xiang static sector_t erofs_bmap(struct address_space *mapping, sector_t block)
360771c994eSGao Xiang {
361771c994eSGao Xiang 	return iomap_bmap(mapping, block, &erofs_iomap_ops);
362771c994eSGao Xiang }
363771c994eSGao Xiang 
364a08e67a0SHuang Jianan static ssize_t erofs_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
365a08e67a0SHuang Jianan {
366de8a801aSGao Xiang 	struct inode *inode = file_inode(iocb->ki_filp);
367de8a801aSGao Xiang 
368a08e67a0SHuang Jianan 	/* no need taking (shared) inode lock since it's a ro filesystem */
369a08e67a0SHuang Jianan 	if (!iov_iter_count(to))
370a08e67a0SHuang Jianan 		return 0;
371a08e67a0SHuang Jianan 
37206252e9cSGao Xiang #ifdef CONFIG_FS_DAX
373de8a801aSGao Xiang 	if (IS_DAX(inode))
37406252e9cSGao Xiang 		return dax_iomap_rw(iocb, to, &erofs_iomap_ops);
37506252e9cSGao Xiang #endif
376a08e67a0SHuang Jianan 	if (iocb->ki_flags & IOCB_DIRECT) {
377de8a801aSGao Xiang 		struct block_device *bdev = inode->i_sb->s_bdev;
378de8a801aSGao Xiang 		unsigned int blksize_mask;
379a08e67a0SHuang Jianan 
380de8a801aSGao Xiang 		if (bdev)
381de8a801aSGao Xiang 			blksize_mask = bdev_logical_block_size(bdev) - 1;
382de8a801aSGao Xiang 		else
3833993f4f4SYue Hu 			blksize_mask = i_blocksize(inode) - 1;
384de8a801aSGao Xiang 
385de8a801aSGao Xiang 		if ((iocb->ki_pos | iov_iter_count(to) |
386de8a801aSGao Xiang 		     iov_iter_alignment(to)) & blksize_mask)
387de8a801aSGao Xiang 			return -EINVAL;
388de8a801aSGao Xiang 
389a08e67a0SHuang Jianan 		return iomap_dio_rw(iocb, to, &erofs_iomap_ops,
390786f847fSChristoph Hellwig 				    NULL, 0, NULL, 0);
391a08e67a0SHuang Jianan 	}
392a08e67a0SHuang Jianan 	return filemap_read(iocb, to, 0);
393a08e67a0SHuang Jianan }
394a08e67a0SHuang Jianan 
39547e4937aSGao Xiang /* for uncompressed (aligned) files and raw access for other files */
39647e4937aSGao Xiang const struct address_space_operations erofs_raw_access_aops = {
3977479c505SMatthew Wilcox (Oracle) 	.read_folio = erofs_read_folio,
398771c994eSGao Xiang 	.readahead = erofs_readahead,
39947e4937aSGao Xiang 	.bmap = erofs_bmap,
400a08e67a0SHuang Jianan 	.direct_IO = noop_direct_IO,
401ce529cc2SJingbo Xu 	.release_folio = iomap_release_folio,
402ce529cc2SJingbo Xu 	.invalidate_folio = iomap_invalidate_folio,
403a08e67a0SHuang Jianan };
404a08e67a0SHuang Jianan 
40506252e9cSGao Xiang #ifdef CONFIG_FS_DAX
40606252e9cSGao Xiang static vm_fault_t erofs_dax_huge_fault(struct vm_fault *vmf,
40706252e9cSGao Xiang 		enum page_entry_size pe_size)
40806252e9cSGao Xiang {
40906252e9cSGao Xiang 	return dax_iomap_fault(vmf, pe_size, NULL, NULL, &erofs_iomap_ops);
41006252e9cSGao Xiang }
41106252e9cSGao Xiang 
41206252e9cSGao Xiang static vm_fault_t erofs_dax_fault(struct vm_fault *vmf)
41306252e9cSGao Xiang {
41406252e9cSGao Xiang 	return erofs_dax_huge_fault(vmf, PE_SIZE_PTE);
41506252e9cSGao Xiang }
41606252e9cSGao Xiang 
41706252e9cSGao Xiang static const struct vm_operations_struct erofs_dax_vm_ops = {
41806252e9cSGao Xiang 	.fault		= erofs_dax_fault,
41906252e9cSGao Xiang 	.huge_fault	= erofs_dax_huge_fault,
42006252e9cSGao Xiang };
42106252e9cSGao Xiang 
42206252e9cSGao Xiang static int erofs_file_mmap(struct file *file, struct vm_area_struct *vma)
42306252e9cSGao Xiang {
42406252e9cSGao Xiang 	if (!IS_DAX(file_inode(file)))
42506252e9cSGao Xiang 		return generic_file_readonly_mmap(file, vma);
42606252e9cSGao Xiang 
42706252e9cSGao Xiang 	if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
42806252e9cSGao Xiang 		return -EINVAL;
42906252e9cSGao Xiang 
43006252e9cSGao Xiang 	vma->vm_ops = &erofs_dax_vm_ops;
4311c71222eSSuren Baghdasaryan 	vm_flags_set(vma, VM_HUGEPAGE);
43206252e9cSGao Xiang 	return 0;
43306252e9cSGao Xiang }
43406252e9cSGao Xiang #else
43506252e9cSGao Xiang #define erofs_file_mmap	generic_file_readonly_mmap
43606252e9cSGao Xiang #endif
43706252e9cSGao Xiang 
438a08e67a0SHuang Jianan const struct file_operations erofs_file_fops = {
439a08e67a0SHuang Jianan 	.llseek		= generic_file_llseek,
440a08e67a0SHuang Jianan 	.read_iter	= erofs_file_read_iter,
44106252e9cSGao Xiang 	.mmap		= erofs_file_mmap,
442a08e67a0SHuang Jianan 	.splice_read	= generic_file_splice_read,
44347e4937aSGao Xiang };
444