xref: /openbmc/linux/fs/erofs/zmap.c (revision 72bb5262)
147e4937aSGao Xiang // SPDX-License-Identifier: GPL-2.0-only
247e4937aSGao Xiang /*
347e4937aSGao Xiang  * Copyright (C) 2018-2019 HUAWEI, Inc.
4592e7cd0SAlexander A. Klimov  *             https://www.huawei.com/
547e4937aSGao Xiang  */
647e4937aSGao Xiang #include "internal.h"
747e4937aSGao Xiang #include <asm/unaligned.h>
847e4937aSGao Xiang #include <trace/events/erofs.h>
947e4937aSGao Xiang 
1047e4937aSGao Xiang int z_erofs_fill_inode(struct inode *inode)
1147e4937aSGao Xiang {
12a5876e24SGao Xiang 	struct erofs_inode *const vi = EROFS_I(inode);
13cec6e93bSGao Xiang 	struct erofs_sb_info *sbi = EROFS_SB(inode->i_sb);
1447e4937aSGao Xiang 
15cec6e93bSGao Xiang 	if (!erofs_sb_has_big_pcluster(sbi) &&
16cec6e93bSGao Xiang 	    vi->datalayout == EROFS_INODE_FLAT_COMPRESSION_LEGACY) {
1747e4937aSGao Xiang 		vi->z_advise = 0;
1847e4937aSGao Xiang 		vi->z_algorithmtype[0] = 0;
1947e4937aSGao Xiang 		vi->z_algorithmtype[1] = 0;
2047e4937aSGao Xiang 		vi->z_logical_clusterbits = LOG_BLOCK_SIZE;
21a5876e24SGao Xiang 		set_bit(EROFS_I_Z_INITED_BIT, &vi->flags);
2247e4937aSGao Xiang 	}
230c638f70SGao Xiang 	inode->i_mapping->a_ops = &z_erofs_aops;
2447e4937aSGao Xiang 	return 0;
2547e4937aSGao Xiang }
2647e4937aSGao Xiang 
270c638f70SGao Xiang static int z_erofs_fill_inode_lazy(struct inode *inode)
2847e4937aSGao Xiang {
29a5876e24SGao Xiang 	struct erofs_inode *const vi = EROFS_I(inode);
3047e4937aSGao Xiang 	struct super_block *const sb = inode->i_sb;
31*72bb5262SGao Xiang 	int err, headnr;
3247e4937aSGao Xiang 	erofs_off_t pos;
3347e4937aSGao Xiang 	struct page *page;
3447e4937aSGao Xiang 	void *kaddr;
3547e4937aSGao Xiang 	struct z_erofs_map_header *h;
3647e4937aSGao Xiang 
37ce063129SGao Xiang 	if (test_bit(EROFS_I_Z_INITED_BIT, &vi->flags)) {
38ce063129SGao Xiang 		/*
39ce063129SGao Xiang 		 * paired with smp_mb() at the end of the function to ensure
40ce063129SGao Xiang 		 * fields will only be observed after the bit is set.
41ce063129SGao Xiang 		 */
42ce063129SGao Xiang 		smp_mb();
4347e4937aSGao Xiang 		return 0;
44ce063129SGao Xiang 	}
4547e4937aSGao Xiang 
46a5876e24SGao Xiang 	if (wait_on_bit_lock(&vi->flags, EROFS_I_BL_Z_BIT, TASK_KILLABLE))
4747e4937aSGao Xiang 		return -ERESTARTSYS;
4847e4937aSGao Xiang 
4947e4937aSGao Xiang 	err = 0;
50a5876e24SGao Xiang 	if (test_bit(EROFS_I_Z_INITED_BIT, &vi->flags))
5147e4937aSGao Xiang 		goto out_unlock;
5247e4937aSGao Xiang 
53cec6e93bSGao Xiang 	DBG_BUGON(!erofs_sb_has_big_pcluster(EROFS_SB(sb)) &&
54cec6e93bSGao Xiang 		  vi->datalayout == EROFS_INODE_FLAT_COMPRESSION_LEGACY);
5547e4937aSGao Xiang 
5647e4937aSGao Xiang 	pos = ALIGN(iloc(EROFS_SB(sb), vi->nid) + vi->inode_isize +
5747e4937aSGao Xiang 		    vi->xattr_isize, 8);
58e655b5b3SGao Xiang 	page = erofs_get_meta_page(sb, erofs_blknr(pos));
5947e4937aSGao Xiang 	if (IS_ERR(page)) {
6047e4937aSGao Xiang 		err = PTR_ERR(page);
6147e4937aSGao Xiang 		goto out_unlock;
6247e4937aSGao Xiang 	}
6347e4937aSGao Xiang 
6447e4937aSGao Xiang 	kaddr = kmap_atomic(page);
6547e4937aSGao Xiang 
6647e4937aSGao Xiang 	h = kaddr + erofs_blkoff(pos);
6747e4937aSGao Xiang 	vi->z_advise = le16_to_cpu(h->h_advise);
6847e4937aSGao Xiang 	vi->z_algorithmtype[0] = h->h_algorithmtype & 15;
6947e4937aSGao Xiang 	vi->z_algorithmtype[1] = h->h_algorithmtype >> 4;
7047e4937aSGao Xiang 
71*72bb5262SGao Xiang 	headnr = 0;
72*72bb5262SGao Xiang 	if (vi->z_algorithmtype[0] >= Z_EROFS_COMPRESSION_MAX ||
73*72bb5262SGao Xiang 	    vi->z_algorithmtype[++headnr] >= Z_EROFS_COMPRESSION_MAX) {
74*72bb5262SGao Xiang 		erofs_err(sb, "unknown HEAD%u format %u for nid %llu, please upgrade kernel",
75*72bb5262SGao Xiang 			  headnr + 1, vi->z_algorithmtype[headnr], vi->nid);
7647e4937aSGao Xiang 		err = -EOPNOTSUPP;
7747e4937aSGao Xiang 		goto unmap_done;
7847e4937aSGao Xiang 	}
7947e4937aSGao Xiang 
8047e4937aSGao Xiang 	vi->z_logical_clusterbits = LOG_BLOCK_SIZE + (h->h_clusterbits & 7);
81b86269f4SGao Xiang 	if (!erofs_sb_has_big_pcluster(EROFS_SB(sb)) &&
82b86269f4SGao Xiang 	    vi->z_advise & (Z_EROFS_ADVISE_BIG_PCLUSTER_1 |
83b86269f4SGao Xiang 			    Z_EROFS_ADVISE_BIG_PCLUSTER_2)) {
84b86269f4SGao Xiang 		erofs_err(sb, "per-inode big pcluster without sb feature for nid %llu",
85b86269f4SGao Xiang 			  vi->nid);
86b86269f4SGao Xiang 		err = -EFSCORRUPTED;
87b86269f4SGao Xiang 		goto unmap_done;
88b86269f4SGao Xiang 	}
89b86269f4SGao Xiang 	if (vi->datalayout == EROFS_INODE_FLAT_COMPRESSION &&
90b86269f4SGao Xiang 	    !(vi->z_advise & Z_EROFS_ADVISE_BIG_PCLUSTER_1) ^
91b86269f4SGao Xiang 	    !(vi->z_advise & Z_EROFS_ADVISE_BIG_PCLUSTER_2)) {
92b86269f4SGao Xiang 		erofs_err(sb, "big pcluster head1/2 of compact indexes should be consistent for nid %llu",
93b86269f4SGao Xiang 			  vi->nid);
94b86269f4SGao Xiang 		err = -EFSCORRUPTED;
95b86269f4SGao Xiang 		goto unmap_done;
96b86269f4SGao Xiang 	}
97ce063129SGao Xiang 	/* paired with smp_mb() at the beginning of the function */
98ce063129SGao Xiang 	smp_mb();
99a5876e24SGao Xiang 	set_bit(EROFS_I_Z_INITED_BIT, &vi->flags);
10047e4937aSGao Xiang unmap_done:
10147e4937aSGao Xiang 	kunmap_atomic(kaddr);
10247e4937aSGao Xiang 	unlock_page(page);
10347e4937aSGao Xiang 	put_page(page);
10447e4937aSGao Xiang out_unlock:
105a5876e24SGao Xiang 	clear_and_wake_up_bit(EROFS_I_BL_Z_BIT, &vi->flags);
10647e4937aSGao Xiang 	return err;
10747e4937aSGao Xiang }
10847e4937aSGao Xiang 
10947e4937aSGao Xiang struct z_erofs_maprecorder {
11047e4937aSGao Xiang 	struct inode *inode;
11147e4937aSGao Xiang 	struct erofs_map_blocks *map;
11247e4937aSGao Xiang 	void *kaddr;
11347e4937aSGao Xiang 
11447e4937aSGao Xiang 	unsigned long lcn;
11547e4937aSGao Xiang 	/* compression extent information gathered */
1168f899262SGao Xiang 	u8  type, headtype;
11747e4937aSGao Xiang 	u16 clusterofs;
11847e4937aSGao Xiang 	u16 delta[2];
119cec6e93bSGao Xiang 	erofs_blk_t pblk, compressedlcs;
12047e4937aSGao Xiang };
12147e4937aSGao Xiang 
12247e4937aSGao Xiang static int z_erofs_reload_indexes(struct z_erofs_maprecorder *m,
12347e4937aSGao Xiang 				  erofs_blk_t eblk)
12447e4937aSGao Xiang {
12547e4937aSGao Xiang 	struct super_block *const sb = m->inode->i_sb;
12647e4937aSGao Xiang 	struct erofs_map_blocks *const map = m->map;
12747e4937aSGao Xiang 	struct page *mpage = map->mpage;
12847e4937aSGao Xiang 
12947e4937aSGao Xiang 	if (mpage) {
13047e4937aSGao Xiang 		if (mpage->index == eblk) {
13147e4937aSGao Xiang 			if (!m->kaddr)
13247e4937aSGao Xiang 				m->kaddr = kmap_atomic(mpage);
13347e4937aSGao Xiang 			return 0;
13447e4937aSGao Xiang 		}
13547e4937aSGao Xiang 
13647e4937aSGao Xiang 		if (m->kaddr) {
13747e4937aSGao Xiang 			kunmap_atomic(m->kaddr);
13847e4937aSGao Xiang 			m->kaddr = NULL;
13947e4937aSGao Xiang 		}
14047e4937aSGao Xiang 		put_page(mpage);
14147e4937aSGao Xiang 	}
14247e4937aSGao Xiang 
143e655b5b3SGao Xiang 	mpage = erofs_get_meta_page(sb, eblk);
14447e4937aSGao Xiang 	if (IS_ERR(mpage)) {
14547e4937aSGao Xiang 		map->mpage = NULL;
14647e4937aSGao Xiang 		return PTR_ERR(mpage);
14747e4937aSGao Xiang 	}
14847e4937aSGao Xiang 	m->kaddr = kmap_atomic(mpage);
14947e4937aSGao Xiang 	unlock_page(mpage);
15047e4937aSGao Xiang 	map->mpage = mpage;
15147e4937aSGao Xiang 	return 0;
15247e4937aSGao Xiang }
15347e4937aSGao Xiang 
1540c638f70SGao Xiang static int legacy_load_cluster_from_disk(struct z_erofs_maprecorder *m,
15547e4937aSGao Xiang 					 unsigned long lcn)
15647e4937aSGao Xiang {
15747e4937aSGao Xiang 	struct inode *const inode = m->inode;
158a5876e24SGao Xiang 	struct erofs_inode *const vi = EROFS_I(inode);
15947e4937aSGao Xiang 	const erofs_off_t ibase = iloc(EROFS_I_SB(inode), vi->nid);
16047e4937aSGao Xiang 	const erofs_off_t pos =
16147e4937aSGao Xiang 		Z_EROFS_VLE_LEGACY_INDEX_ALIGN(ibase + vi->inode_isize +
16247e4937aSGao Xiang 					       vi->xattr_isize) +
16347e4937aSGao Xiang 		lcn * sizeof(struct z_erofs_vle_decompressed_index);
16447e4937aSGao Xiang 	struct z_erofs_vle_decompressed_index *di;
16547e4937aSGao Xiang 	unsigned int advise, type;
16647e4937aSGao Xiang 	int err;
16747e4937aSGao Xiang 
16847e4937aSGao Xiang 	err = z_erofs_reload_indexes(m, erofs_blknr(pos));
16947e4937aSGao Xiang 	if (err)
17047e4937aSGao Xiang 		return err;
17147e4937aSGao Xiang 
17247e4937aSGao Xiang 	m->lcn = lcn;
17347e4937aSGao Xiang 	di = m->kaddr + erofs_blkoff(pos);
17447e4937aSGao Xiang 
17547e4937aSGao Xiang 	advise = le16_to_cpu(di->di_advise);
17647e4937aSGao Xiang 	type = (advise >> Z_EROFS_VLE_DI_CLUSTER_TYPE_BIT) &
17747e4937aSGao Xiang 		((1 << Z_EROFS_VLE_DI_CLUSTER_TYPE_BITS) - 1);
17847e4937aSGao Xiang 	switch (type) {
17947e4937aSGao Xiang 	case Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD:
18047e4937aSGao Xiang 		m->clusterofs = 1 << vi->z_logical_clusterbits;
18147e4937aSGao Xiang 		m->delta[0] = le16_to_cpu(di->di_u.delta[0]);
182cec6e93bSGao Xiang 		if (m->delta[0] & Z_EROFS_VLE_DI_D0_CBLKCNT) {
183*72bb5262SGao Xiang 			if (!(vi->z_advise & (Z_EROFS_ADVISE_BIG_PCLUSTER_1 |
184*72bb5262SGao Xiang 					Z_EROFS_ADVISE_BIG_PCLUSTER_2))) {
185cec6e93bSGao Xiang 				DBG_BUGON(1);
186cec6e93bSGao Xiang 				return -EFSCORRUPTED;
187cec6e93bSGao Xiang 			}
188cec6e93bSGao Xiang 			m->compressedlcs = m->delta[0] &
189cec6e93bSGao Xiang 				~Z_EROFS_VLE_DI_D0_CBLKCNT;
190cec6e93bSGao Xiang 			m->delta[0] = 1;
191cec6e93bSGao Xiang 		}
19247e4937aSGao Xiang 		m->delta[1] = le16_to_cpu(di->di_u.delta[1]);
19347e4937aSGao Xiang 		break;
19447e4937aSGao Xiang 	case Z_EROFS_VLE_CLUSTER_TYPE_PLAIN:
195*72bb5262SGao Xiang 	case Z_EROFS_VLE_CLUSTER_TYPE_HEAD1:
196*72bb5262SGao Xiang 	case Z_EROFS_VLE_CLUSTER_TYPE_HEAD2:
19747e4937aSGao Xiang 		m->clusterofs = le16_to_cpu(di->di_clusterofs);
19847e4937aSGao Xiang 		m->pblk = le32_to_cpu(di->di_u.blkaddr);
19947e4937aSGao Xiang 		break;
20047e4937aSGao Xiang 	default:
20147e4937aSGao Xiang 		DBG_BUGON(1);
20247e4937aSGao Xiang 		return -EOPNOTSUPP;
20347e4937aSGao Xiang 	}
20447e4937aSGao Xiang 	m->type = type;
20547e4937aSGao Xiang 	return 0;
20647e4937aSGao Xiang }
20747e4937aSGao Xiang 
20847e4937aSGao Xiang static unsigned int decode_compactedbits(unsigned int lobits,
20947e4937aSGao Xiang 					 unsigned int lomask,
21047e4937aSGao Xiang 					 u8 *in, unsigned int pos, u8 *type)
21147e4937aSGao Xiang {
21247e4937aSGao Xiang 	const unsigned int v = get_unaligned_le32(in + pos / 8) >> (pos & 7);
21347e4937aSGao Xiang 	const unsigned int lo = v & lomask;
21447e4937aSGao Xiang 
21547e4937aSGao Xiang 	*type = (v >> lobits) & 3;
21647e4937aSGao Xiang 	return lo;
21747e4937aSGao Xiang }
21847e4937aSGao Xiang 
219d95ae5e2SGao Xiang static int get_compacted_la_distance(unsigned int lclusterbits,
220d95ae5e2SGao Xiang 				     unsigned int encodebits,
221d95ae5e2SGao Xiang 				     unsigned int vcnt, u8 *in, int i)
222d95ae5e2SGao Xiang {
223d95ae5e2SGao Xiang 	const unsigned int lomask = (1 << lclusterbits) - 1;
224d95ae5e2SGao Xiang 	unsigned int lo, d1 = 0;
225d95ae5e2SGao Xiang 	u8 type;
226d95ae5e2SGao Xiang 
227d95ae5e2SGao Xiang 	DBG_BUGON(i >= vcnt);
228d95ae5e2SGao Xiang 
229d95ae5e2SGao Xiang 	do {
230d95ae5e2SGao Xiang 		lo = decode_compactedbits(lclusterbits, lomask,
231d95ae5e2SGao Xiang 					  in, encodebits * i, &type);
232d95ae5e2SGao Xiang 
233d95ae5e2SGao Xiang 		if (type != Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD)
234d95ae5e2SGao Xiang 			return d1;
235d95ae5e2SGao Xiang 		++d1;
236d95ae5e2SGao Xiang 	} while (++i < vcnt);
237d95ae5e2SGao Xiang 
238d95ae5e2SGao Xiang 	/* vcnt - 1 (Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD) item */
239d95ae5e2SGao Xiang 	if (!(lo & Z_EROFS_VLE_DI_D0_CBLKCNT))
240d95ae5e2SGao Xiang 		d1 += lo - 1;
241d95ae5e2SGao Xiang 	return d1;
242d95ae5e2SGao Xiang }
243d95ae5e2SGao Xiang 
24447e4937aSGao Xiang static int unpack_compacted_index(struct z_erofs_maprecorder *m,
24547e4937aSGao Xiang 				  unsigned int amortizedshift,
246d95ae5e2SGao Xiang 				  unsigned int eofs, bool lookahead)
24747e4937aSGao Xiang {
248a5876e24SGao Xiang 	struct erofs_inode *const vi = EROFS_I(m->inode);
24947e4937aSGao Xiang 	const unsigned int lclusterbits = vi->z_logical_clusterbits;
25047e4937aSGao Xiang 	const unsigned int lomask = (1 << lclusterbits) - 1;
25147e4937aSGao Xiang 	unsigned int vcnt, base, lo, encodebits, nblk;
25247e4937aSGao Xiang 	int i;
25347e4937aSGao Xiang 	u8 *in, type;
254b86269f4SGao Xiang 	bool big_pcluster;
25547e4937aSGao Xiang 
25647e4937aSGao Xiang 	if (1 << amortizedshift == 4)
25747e4937aSGao Xiang 		vcnt = 2;
25847e4937aSGao Xiang 	else if (1 << amortizedshift == 2 && lclusterbits == 12)
25947e4937aSGao Xiang 		vcnt = 16;
26047e4937aSGao Xiang 	else
26147e4937aSGao Xiang 		return -EOPNOTSUPP;
26247e4937aSGao Xiang 
263b86269f4SGao Xiang 	big_pcluster = vi->z_advise & Z_EROFS_ADVISE_BIG_PCLUSTER_1;
26447e4937aSGao Xiang 	encodebits = ((vcnt << amortizedshift) - sizeof(__le32)) * 8 / vcnt;
26547e4937aSGao Xiang 	base = round_down(eofs, vcnt << amortizedshift);
26647e4937aSGao Xiang 	in = m->kaddr + base;
26747e4937aSGao Xiang 
26847e4937aSGao Xiang 	i = (eofs - base) >> amortizedshift;
26947e4937aSGao Xiang 
27047e4937aSGao Xiang 	lo = decode_compactedbits(lclusterbits, lomask,
27147e4937aSGao Xiang 				  in, encodebits * i, &type);
27247e4937aSGao Xiang 	m->type = type;
27347e4937aSGao Xiang 	if (type == Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD) {
27447e4937aSGao Xiang 		m->clusterofs = 1 << lclusterbits;
275d95ae5e2SGao Xiang 
276d95ae5e2SGao Xiang 		/* figure out lookahead_distance: delta[1] if needed */
277d95ae5e2SGao Xiang 		if (lookahead)
278d95ae5e2SGao Xiang 			m->delta[1] = get_compacted_la_distance(lclusterbits,
279d95ae5e2SGao Xiang 						encodebits, vcnt, in, i);
280b86269f4SGao Xiang 		if (lo & Z_EROFS_VLE_DI_D0_CBLKCNT) {
281b86269f4SGao Xiang 			if (!big_pcluster) {
282b86269f4SGao Xiang 				DBG_BUGON(1);
283b86269f4SGao Xiang 				return -EFSCORRUPTED;
284b86269f4SGao Xiang 			}
285b86269f4SGao Xiang 			m->compressedlcs = lo & ~Z_EROFS_VLE_DI_D0_CBLKCNT;
286b86269f4SGao Xiang 			m->delta[0] = 1;
287b86269f4SGao Xiang 			return 0;
288b86269f4SGao Xiang 		} else if (i + 1 != (int)vcnt) {
28947e4937aSGao Xiang 			m->delta[0] = lo;
29047e4937aSGao Xiang 			return 0;
29147e4937aSGao Xiang 		}
29247e4937aSGao Xiang 		/*
29347e4937aSGao Xiang 		 * since the last lcluster in the pack is special,
29447e4937aSGao Xiang 		 * of which lo saves delta[1] rather than delta[0].
29547e4937aSGao Xiang 		 * Hence, get delta[0] by the previous lcluster indirectly.
29647e4937aSGao Xiang 		 */
29747e4937aSGao Xiang 		lo = decode_compactedbits(lclusterbits, lomask,
29847e4937aSGao Xiang 					  in, encodebits * (i - 1), &type);
29947e4937aSGao Xiang 		if (type != Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD)
30047e4937aSGao Xiang 			lo = 0;
301b86269f4SGao Xiang 		else if (lo & Z_EROFS_VLE_DI_D0_CBLKCNT)
302b86269f4SGao Xiang 			lo = 1;
30347e4937aSGao Xiang 		m->delta[0] = lo + 1;
30447e4937aSGao Xiang 		return 0;
30547e4937aSGao Xiang 	}
30647e4937aSGao Xiang 	m->clusterofs = lo;
30747e4937aSGao Xiang 	m->delta[0] = 0;
30847e4937aSGao Xiang 	/* figout out blkaddr (pblk) for HEAD lclusters */
309b86269f4SGao Xiang 	if (!big_pcluster) {
31047e4937aSGao Xiang 		nblk = 1;
31147e4937aSGao Xiang 		while (i > 0) {
31247e4937aSGao Xiang 			--i;
31347e4937aSGao Xiang 			lo = decode_compactedbits(lclusterbits, lomask,
31447e4937aSGao Xiang 						  in, encodebits * i, &type);
31547e4937aSGao Xiang 			if (type == Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD)
31647e4937aSGao Xiang 				i -= lo;
31747e4937aSGao Xiang 
31847e4937aSGao Xiang 			if (i >= 0)
31947e4937aSGao Xiang 				++nblk;
32047e4937aSGao Xiang 		}
321b86269f4SGao Xiang 	} else {
322b86269f4SGao Xiang 		nblk = 0;
323b86269f4SGao Xiang 		while (i > 0) {
324b86269f4SGao Xiang 			--i;
325b86269f4SGao Xiang 			lo = decode_compactedbits(lclusterbits, lomask,
326b86269f4SGao Xiang 						  in, encodebits * i, &type);
327b86269f4SGao Xiang 			if (type == Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD) {
328b86269f4SGao Xiang 				if (lo & Z_EROFS_VLE_DI_D0_CBLKCNT) {
329b86269f4SGao Xiang 					--i;
330b86269f4SGao Xiang 					nblk += lo & ~Z_EROFS_VLE_DI_D0_CBLKCNT;
331b86269f4SGao Xiang 					continue;
332b86269f4SGao Xiang 				}
333b86269f4SGao Xiang 				/* bigpcluster shouldn't have plain d0 == 1 */
334b86269f4SGao Xiang 				if (lo <= 1) {
335b86269f4SGao Xiang 					DBG_BUGON(1);
336b86269f4SGao Xiang 					return -EFSCORRUPTED;
337b86269f4SGao Xiang 				}
338b86269f4SGao Xiang 				i -= lo - 2;
339b86269f4SGao Xiang 				continue;
340b86269f4SGao Xiang 			}
341b86269f4SGao Xiang 			++nblk;
342b86269f4SGao Xiang 		}
343b86269f4SGao Xiang 	}
34447e4937aSGao Xiang 	in += (vcnt << amortizedshift) - sizeof(__le32);
34547e4937aSGao Xiang 	m->pblk = le32_to_cpu(*(__le32 *)in) + nblk;
34647e4937aSGao Xiang 	return 0;
34747e4937aSGao Xiang }
34847e4937aSGao Xiang 
34947e4937aSGao Xiang static int compacted_load_cluster_from_disk(struct z_erofs_maprecorder *m,
350d95ae5e2SGao Xiang 					    unsigned long lcn, bool lookahead)
35147e4937aSGao Xiang {
35247e4937aSGao Xiang 	struct inode *const inode = m->inode;
353a5876e24SGao Xiang 	struct erofs_inode *const vi = EROFS_I(inode);
35447e4937aSGao Xiang 	const unsigned int lclusterbits = vi->z_logical_clusterbits;
35547e4937aSGao Xiang 	const erofs_off_t ebase = ALIGN(iloc(EROFS_I_SB(inode), vi->nid) +
35647e4937aSGao Xiang 					vi->inode_isize + vi->xattr_isize, 8) +
35747e4937aSGao Xiang 		sizeof(struct z_erofs_map_header);
35847e4937aSGao Xiang 	const unsigned int totalidx = DIV_ROUND_UP(inode->i_size, EROFS_BLKSIZ);
35947e4937aSGao Xiang 	unsigned int compacted_4b_initial, compacted_2b;
36047e4937aSGao Xiang 	unsigned int amortizedshift;
36147e4937aSGao Xiang 	erofs_off_t pos;
36247e4937aSGao Xiang 	int err;
36347e4937aSGao Xiang 
36447e4937aSGao Xiang 	if (lclusterbits != 12)
36547e4937aSGao Xiang 		return -EOPNOTSUPP;
36647e4937aSGao Xiang 
36747e4937aSGao Xiang 	if (lcn >= totalidx)
36847e4937aSGao Xiang 		return -EINVAL;
36947e4937aSGao Xiang 
37047e4937aSGao Xiang 	m->lcn = lcn;
37147e4937aSGao Xiang 	/* used to align to 32-byte (compacted_2b) alignment */
37247e4937aSGao Xiang 	compacted_4b_initial = (32 - ebase % 32) / 4;
37347e4937aSGao Xiang 	if (compacted_4b_initial == 32 / 4)
37447e4937aSGao Xiang 		compacted_4b_initial = 0;
37547e4937aSGao Xiang 
376c40dd3caSYue Hu 	if ((vi->z_advise & Z_EROFS_ADVISE_COMPACTED_2B) &&
377c40dd3caSYue Hu 	    compacted_4b_initial < totalidx)
37847e4937aSGao Xiang 		compacted_2b = rounddown(totalidx - compacted_4b_initial, 16);
37947e4937aSGao Xiang 	else
38047e4937aSGao Xiang 		compacted_2b = 0;
38147e4937aSGao Xiang 
38247e4937aSGao Xiang 	pos = ebase;
38347e4937aSGao Xiang 	if (lcn < compacted_4b_initial) {
38447e4937aSGao Xiang 		amortizedshift = 2;
38547e4937aSGao Xiang 		goto out;
38647e4937aSGao Xiang 	}
38747e4937aSGao Xiang 	pos += compacted_4b_initial * 4;
38847e4937aSGao Xiang 	lcn -= compacted_4b_initial;
38947e4937aSGao Xiang 
39047e4937aSGao Xiang 	if (lcn < compacted_2b) {
39147e4937aSGao Xiang 		amortizedshift = 1;
39247e4937aSGao Xiang 		goto out;
39347e4937aSGao Xiang 	}
39447e4937aSGao Xiang 	pos += compacted_2b * 2;
39547e4937aSGao Xiang 	lcn -= compacted_2b;
39647e4937aSGao Xiang 	amortizedshift = 2;
39747e4937aSGao Xiang out:
39847e4937aSGao Xiang 	pos += lcn * (1 << amortizedshift);
39947e4937aSGao Xiang 	err = z_erofs_reload_indexes(m, erofs_blknr(pos));
40047e4937aSGao Xiang 	if (err)
40147e4937aSGao Xiang 		return err;
402d95ae5e2SGao Xiang 	return unpack_compacted_index(m, amortizedshift, erofs_blkoff(pos),
403d95ae5e2SGao Xiang 				      lookahead);
40447e4937aSGao Xiang }
40547e4937aSGao Xiang 
4060c638f70SGao Xiang static int z_erofs_load_cluster_from_disk(struct z_erofs_maprecorder *m,
407d95ae5e2SGao Xiang 					  unsigned int lcn, bool lookahead)
40847e4937aSGao Xiang {
409a5876e24SGao Xiang 	const unsigned int datamode = EROFS_I(m->inode)->datalayout;
41047e4937aSGao Xiang 
41147e4937aSGao Xiang 	if (datamode == EROFS_INODE_FLAT_COMPRESSION_LEGACY)
4120c638f70SGao Xiang 		return legacy_load_cluster_from_disk(m, lcn);
41347e4937aSGao Xiang 
41447e4937aSGao Xiang 	if (datamode == EROFS_INODE_FLAT_COMPRESSION)
415d95ae5e2SGao Xiang 		return compacted_load_cluster_from_disk(m, lcn, lookahead);
41647e4937aSGao Xiang 
41747e4937aSGao Xiang 	return -EINVAL;
41847e4937aSGao Xiang }
41947e4937aSGao Xiang 
4200c638f70SGao Xiang static int z_erofs_extent_lookback(struct z_erofs_maprecorder *m,
42147e4937aSGao Xiang 				   unsigned int lookback_distance)
42247e4937aSGao Xiang {
423a5876e24SGao Xiang 	struct erofs_inode *const vi = EROFS_I(m->inode);
42447e4937aSGao Xiang 	struct erofs_map_blocks *const map = m->map;
42547e4937aSGao Xiang 	const unsigned int lclusterbits = vi->z_logical_clusterbits;
42647e4937aSGao Xiang 	unsigned long lcn = m->lcn;
42747e4937aSGao Xiang 	int err;
42847e4937aSGao Xiang 
42947e4937aSGao Xiang 	if (lcn < lookback_distance) {
4304f761fa2SGao Xiang 		erofs_err(m->inode->i_sb,
4314f761fa2SGao Xiang 			  "bogus lookback distance @ nid %llu", vi->nid);
43247e4937aSGao Xiang 		DBG_BUGON(1);
43347e4937aSGao Xiang 		return -EFSCORRUPTED;
43447e4937aSGao Xiang 	}
43547e4937aSGao Xiang 
43647e4937aSGao Xiang 	/* load extent head logical cluster if needed */
43747e4937aSGao Xiang 	lcn -= lookback_distance;
438d95ae5e2SGao Xiang 	err = z_erofs_load_cluster_from_disk(m, lcn, false);
43947e4937aSGao Xiang 	if (err)
44047e4937aSGao Xiang 		return err;
44147e4937aSGao Xiang 
44247e4937aSGao Xiang 	switch (m->type) {
44347e4937aSGao Xiang 	case Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD:
4448d8a09b0SGao Xiang 		if (!m->delta[0]) {
4454f761fa2SGao Xiang 			erofs_err(m->inode->i_sb,
4464f761fa2SGao Xiang 				  "invalid lookback distance 0 @ nid %llu",
44747e4937aSGao Xiang 				  vi->nid);
44847e4937aSGao Xiang 			DBG_BUGON(1);
44947e4937aSGao Xiang 			return -EFSCORRUPTED;
45047e4937aSGao Xiang 		}
4510c638f70SGao Xiang 		return z_erofs_extent_lookback(m, m->delta[0]);
45247e4937aSGao Xiang 	case Z_EROFS_VLE_CLUSTER_TYPE_PLAIN:
453*72bb5262SGao Xiang 	case Z_EROFS_VLE_CLUSTER_TYPE_HEAD1:
454*72bb5262SGao Xiang 	case Z_EROFS_VLE_CLUSTER_TYPE_HEAD2:
4558f899262SGao Xiang 		m->headtype = m->type;
45647e4937aSGao Xiang 		map->m_la = (lcn << lclusterbits) | m->clusterofs;
45747e4937aSGao Xiang 		break;
45847e4937aSGao Xiang 	default:
4594f761fa2SGao Xiang 		erofs_err(m->inode->i_sb,
4604f761fa2SGao Xiang 			  "unknown type %u @ lcn %lu of nid %llu",
46147e4937aSGao Xiang 			  m->type, lcn, vi->nid);
46247e4937aSGao Xiang 		DBG_BUGON(1);
46347e4937aSGao Xiang 		return -EOPNOTSUPP;
46447e4937aSGao Xiang 	}
46547e4937aSGao Xiang 	return 0;
46647e4937aSGao Xiang }
46747e4937aSGao Xiang 
468cec6e93bSGao Xiang static int z_erofs_get_extent_compressedlen(struct z_erofs_maprecorder *m,
469cec6e93bSGao Xiang 					    unsigned int initial_lcn)
470cec6e93bSGao Xiang {
471cec6e93bSGao Xiang 	struct erofs_inode *const vi = EROFS_I(m->inode);
472cec6e93bSGao Xiang 	struct erofs_map_blocks *const map = m->map;
473cec6e93bSGao Xiang 	const unsigned int lclusterbits = vi->z_logical_clusterbits;
474cec6e93bSGao Xiang 	unsigned long lcn;
475cec6e93bSGao Xiang 	int err;
476cec6e93bSGao Xiang 
477cec6e93bSGao Xiang 	DBG_BUGON(m->type != Z_EROFS_VLE_CLUSTER_TYPE_PLAIN &&
478*72bb5262SGao Xiang 		  m->type != Z_EROFS_VLE_CLUSTER_TYPE_HEAD1 &&
479*72bb5262SGao Xiang 		  m->type != Z_EROFS_VLE_CLUSTER_TYPE_HEAD2);
480*72bb5262SGao Xiang 	DBG_BUGON(m->type != m->headtype);
481*72bb5262SGao Xiang 
4828f899262SGao Xiang 	if (m->headtype == Z_EROFS_VLE_CLUSTER_TYPE_PLAIN ||
483*72bb5262SGao Xiang 	    ((m->headtype == Z_EROFS_VLE_CLUSTER_TYPE_HEAD1) &&
484*72bb5262SGao Xiang 	     !(vi->z_advise & Z_EROFS_ADVISE_BIG_PCLUSTER_1)) ||
485*72bb5262SGao Xiang 	    ((m->headtype == Z_EROFS_VLE_CLUSTER_TYPE_HEAD2) &&
486*72bb5262SGao Xiang 	     !(vi->z_advise & Z_EROFS_ADVISE_BIG_PCLUSTER_2))) {
487cec6e93bSGao Xiang 		map->m_plen = 1 << lclusterbits;
488cec6e93bSGao Xiang 		return 0;
489cec6e93bSGao Xiang 	}
490cec6e93bSGao Xiang 	lcn = m->lcn + 1;
491cec6e93bSGao Xiang 	if (m->compressedlcs)
492cec6e93bSGao Xiang 		goto out;
493cec6e93bSGao Xiang 
494d95ae5e2SGao Xiang 	err = z_erofs_load_cluster_from_disk(m, lcn, false);
495cec6e93bSGao Xiang 	if (err)
496cec6e93bSGao Xiang 		return err;
497cec6e93bSGao Xiang 
4980852b6caSGao Xiang 	/*
4990852b6caSGao Xiang 	 * If the 1st NONHEAD lcluster has already been handled initially w/o
5000852b6caSGao Xiang 	 * valid compressedlcs, which means at least it mustn't be CBLKCNT, or
5010852b6caSGao Xiang 	 * an internal implemenatation error is detected.
5020852b6caSGao Xiang 	 *
5030852b6caSGao Xiang 	 * The following code can also handle it properly anyway, but let's
5040852b6caSGao Xiang 	 * BUG_ON in the debugging mode only for developers to notice that.
5050852b6caSGao Xiang 	 */
5060852b6caSGao Xiang 	DBG_BUGON(lcn == initial_lcn &&
5070852b6caSGao Xiang 		  m->type == Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD);
5080852b6caSGao Xiang 
509cec6e93bSGao Xiang 	switch (m->type) {
5100852b6caSGao Xiang 	case Z_EROFS_VLE_CLUSTER_TYPE_PLAIN:
511*72bb5262SGao Xiang 	case Z_EROFS_VLE_CLUSTER_TYPE_HEAD1:
512*72bb5262SGao Xiang 	case Z_EROFS_VLE_CLUSTER_TYPE_HEAD2:
5130852b6caSGao Xiang 		/*
5140852b6caSGao Xiang 		 * if the 1st NONHEAD lcluster is actually PLAIN or HEAD type
5150852b6caSGao Xiang 		 * rather than CBLKCNT, it's a 1 lcluster-sized pcluster.
5160852b6caSGao Xiang 		 */
5170852b6caSGao Xiang 		m->compressedlcs = 1;
5180852b6caSGao Xiang 		break;
519cec6e93bSGao Xiang 	case Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD:
520cec6e93bSGao Xiang 		if (m->delta[0] != 1)
521cec6e93bSGao Xiang 			goto err_bonus_cblkcnt;
522cec6e93bSGao Xiang 		if (m->compressedlcs)
523cec6e93bSGao Xiang 			break;
524cec6e93bSGao Xiang 		fallthrough;
525cec6e93bSGao Xiang 	default:
526cec6e93bSGao Xiang 		erofs_err(m->inode->i_sb,
527cec6e93bSGao Xiang 			  "cannot found CBLKCNT @ lcn %lu of nid %llu",
528cec6e93bSGao Xiang 			  lcn, vi->nid);
529cec6e93bSGao Xiang 		DBG_BUGON(1);
530cec6e93bSGao Xiang 		return -EFSCORRUPTED;
531cec6e93bSGao Xiang 	}
532cec6e93bSGao Xiang out:
533cec6e93bSGao Xiang 	map->m_plen = m->compressedlcs << lclusterbits;
534cec6e93bSGao Xiang 	return 0;
535cec6e93bSGao Xiang err_bonus_cblkcnt:
536cec6e93bSGao Xiang 	erofs_err(m->inode->i_sb,
537cec6e93bSGao Xiang 		  "bogus CBLKCNT @ lcn %lu of nid %llu",
538cec6e93bSGao Xiang 		  lcn, vi->nid);
539cec6e93bSGao Xiang 	DBG_BUGON(1);
540cec6e93bSGao Xiang 	return -EFSCORRUPTED;
541cec6e93bSGao Xiang }
542cec6e93bSGao Xiang 
543d95ae5e2SGao Xiang static int z_erofs_get_extent_decompressedlen(struct z_erofs_maprecorder *m)
544d95ae5e2SGao Xiang {
545d95ae5e2SGao Xiang 	struct inode *inode = m->inode;
546d95ae5e2SGao Xiang 	struct erofs_inode *vi = EROFS_I(inode);
547d95ae5e2SGao Xiang 	struct erofs_map_blocks *map = m->map;
548d95ae5e2SGao Xiang 	unsigned int lclusterbits = vi->z_logical_clusterbits;
549d95ae5e2SGao Xiang 	u64 lcn = m->lcn, headlcn = map->m_la >> lclusterbits;
550d95ae5e2SGao Xiang 	int err;
551d95ae5e2SGao Xiang 
552d95ae5e2SGao Xiang 	do {
553d95ae5e2SGao Xiang 		/* handle the last EOF pcluster (no next HEAD lcluster) */
554d95ae5e2SGao Xiang 		if ((lcn << lclusterbits) >= inode->i_size) {
555d95ae5e2SGao Xiang 			map->m_llen = inode->i_size - map->m_la;
556d95ae5e2SGao Xiang 			return 0;
557d95ae5e2SGao Xiang 		}
558d95ae5e2SGao Xiang 
559d95ae5e2SGao Xiang 		err = z_erofs_load_cluster_from_disk(m, lcn, true);
560d95ae5e2SGao Xiang 		if (err)
561d95ae5e2SGao Xiang 			return err;
562d95ae5e2SGao Xiang 
563d95ae5e2SGao Xiang 		if (m->type == Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD) {
564d95ae5e2SGao Xiang 			DBG_BUGON(!m->delta[1] &&
565d95ae5e2SGao Xiang 				  m->clusterofs != 1 << lclusterbits);
566d95ae5e2SGao Xiang 		} else if (m->type == Z_EROFS_VLE_CLUSTER_TYPE_PLAIN ||
567*72bb5262SGao Xiang 			   m->type == Z_EROFS_VLE_CLUSTER_TYPE_HEAD1 ||
568*72bb5262SGao Xiang 			   m->type == Z_EROFS_VLE_CLUSTER_TYPE_HEAD2) {
569d95ae5e2SGao Xiang 			/* go on until the next HEAD lcluster */
570d95ae5e2SGao Xiang 			if (lcn != headlcn)
571d95ae5e2SGao Xiang 				break;
572d95ae5e2SGao Xiang 			m->delta[1] = 1;
573d95ae5e2SGao Xiang 		} else {
574d95ae5e2SGao Xiang 			erofs_err(inode->i_sb, "unknown type %u @ lcn %llu of nid %llu",
575d95ae5e2SGao Xiang 				  m->type, lcn, vi->nid);
576d95ae5e2SGao Xiang 			DBG_BUGON(1);
577d95ae5e2SGao Xiang 			return -EOPNOTSUPP;
578d95ae5e2SGao Xiang 		}
579d95ae5e2SGao Xiang 		lcn += m->delta[1];
580d95ae5e2SGao Xiang 	} while (m->delta[1]);
581d95ae5e2SGao Xiang 
582d95ae5e2SGao Xiang 	map->m_llen = (lcn << lclusterbits) + m->clusterofs - map->m_la;
583d95ae5e2SGao Xiang 	return 0;
584d95ae5e2SGao Xiang }
585d95ae5e2SGao Xiang 
58647e4937aSGao Xiang int z_erofs_map_blocks_iter(struct inode *inode,
58747e4937aSGao Xiang 			    struct erofs_map_blocks *map,
58847e4937aSGao Xiang 			    int flags)
58947e4937aSGao Xiang {
590a5876e24SGao Xiang 	struct erofs_inode *const vi = EROFS_I(inode);
59147e4937aSGao Xiang 	struct z_erofs_maprecorder m = {
59247e4937aSGao Xiang 		.inode = inode,
59347e4937aSGao Xiang 		.map = map,
59447e4937aSGao Xiang 	};
59547e4937aSGao Xiang 	int err = 0;
59647e4937aSGao Xiang 	unsigned int lclusterbits, endoff;
597cec6e93bSGao Xiang 	unsigned long initial_lcn;
59847e4937aSGao Xiang 	unsigned long long ofs, end;
59947e4937aSGao Xiang 
60047e4937aSGao Xiang 	trace_z_erofs_map_blocks_iter_enter(inode, map, flags);
60147e4937aSGao Xiang 
60247e4937aSGao Xiang 	/* when trying to read beyond EOF, leave it unmapped */
6038d8a09b0SGao Xiang 	if (map->m_la >= inode->i_size) {
60447e4937aSGao Xiang 		map->m_llen = map->m_la + 1 - inode->i_size;
60547e4937aSGao Xiang 		map->m_la = inode->i_size;
60647e4937aSGao Xiang 		map->m_flags = 0;
60747e4937aSGao Xiang 		goto out;
60847e4937aSGao Xiang 	}
60947e4937aSGao Xiang 
6100c638f70SGao Xiang 	err = z_erofs_fill_inode_lazy(inode);
61147e4937aSGao Xiang 	if (err)
61247e4937aSGao Xiang 		goto out;
61347e4937aSGao Xiang 
61447e4937aSGao Xiang 	lclusterbits = vi->z_logical_clusterbits;
61547e4937aSGao Xiang 	ofs = map->m_la;
616cec6e93bSGao Xiang 	initial_lcn = ofs >> lclusterbits;
61747e4937aSGao Xiang 	endoff = ofs & ((1 << lclusterbits) - 1);
61847e4937aSGao Xiang 
619d95ae5e2SGao Xiang 	err = z_erofs_load_cluster_from_disk(&m, initial_lcn, false);
62047e4937aSGao Xiang 	if (err)
62147e4937aSGao Xiang 		goto unmap_out;
62247e4937aSGao Xiang 
6238f899262SGao Xiang 	map->m_flags = EROFS_MAP_MAPPED | EROFS_MAP_ENCODED;
62447e4937aSGao Xiang 	end = (m.lcn + 1ULL) << lclusterbits;
62547e4937aSGao Xiang 
62647e4937aSGao Xiang 	switch (m.type) {
62747e4937aSGao Xiang 	case Z_EROFS_VLE_CLUSTER_TYPE_PLAIN:
628*72bb5262SGao Xiang 	case Z_EROFS_VLE_CLUSTER_TYPE_HEAD1:
629*72bb5262SGao Xiang 	case Z_EROFS_VLE_CLUSTER_TYPE_HEAD2:
63047e4937aSGao Xiang 		if (endoff >= m.clusterofs) {
6318f899262SGao Xiang 			m.headtype = m.type;
63247e4937aSGao Xiang 			map->m_la = (m.lcn << lclusterbits) | m.clusterofs;
63347e4937aSGao Xiang 			break;
63447e4937aSGao Xiang 		}
63547e4937aSGao Xiang 		/* m.lcn should be >= 1 if endoff < m.clusterofs */
6368d8a09b0SGao Xiang 		if (!m.lcn) {
6374f761fa2SGao Xiang 			erofs_err(inode->i_sb,
6384f761fa2SGao Xiang 				  "invalid logical cluster 0 at nid %llu",
63947e4937aSGao Xiang 				  vi->nid);
64047e4937aSGao Xiang 			err = -EFSCORRUPTED;
64147e4937aSGao Xiang 			goto unmap_out;
64247e4937aSGao Xiang 		}
64347e4937aSGao Xiang 		end = (m.lcn << lclusterbits) | m.clusterofs;
64447e4937aSGao Xiang 		map->m_flags |= EROFS_MAP_FULL_MAPPED;
64547e4937aSGao Xiang 		m.delta[0] = 1;
646df561f66SGustavo A. R. Silva 		fallthrough;
64747e4937aSGao Xiang 	case Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD:
648fe6adcceSRuiqi Gong 		/* get the corresponding first chunk */
6490c638f70SGao Xiang 		err = z_erofs_extent_lookback(&m, m.delta[0]);
6508d8a09b0SGao Xiang 		if (err)
65147e4937aSGao Xiang 			goto unmap_out;
65247e4937aSGao Xiang 		break;
65347e4937aSGao Xiang 	default:
6544f761fa2SGao Xiang 		erofs_err(inode->i_sb,
6554f761fa2SGao Xiang 			  "unknown type %u @ offset %llu of nid %llu",
65647e4937aSGao Xiang 			  m.type, ofs, vi->nid);
65747e4937aSGao Xiang 		err = -EOPNOTSUPP;
65847e4937aSGao Xiang 		goto unmap_out;
65947e4937aSGao Xiang 	}
66047e4937aSGao Xiang 
66147e4937aSGao Xiang 	map->m_llen = end - map->m_la;
66247e4937aSGao Xiang 	map->m_pa = blknr_to_addr(m.pblk);
66347e4937aSGao Xiang 
664cec6e93bSGao Xiang 	err = z_erofs_get_extent_compressedlen(&m, initial_lcn);
665cec6e93bSGao Xiang 	if (err)
666cec6e93bSGao Xiang 		goto out;
667d95ae5e2SGao Xiang 
6688f899262SGao Xiang 	if (m.headtype == Z_EROFS_VLE_CLUSTER_TYPE_PLAIN)
6698f899262SGao Xiang 		map->m_algorithmformat = Z_EROFS_COMPRESSION_SHIFTED;
670*72bb5262SGao Xiang 	else if (m.headtype == Z_EROFS_VLE_CLUSTER_TYPE_HEAD2)
671*72bb5262SGao Xiang 		map->m_algorithmformat = vi->z_algorithmtype[1];
6728f899262SGao Xiang 	else
6738f899262SGao Xiang 		map->m_algorithmformat = vi->z_algorithmtype[0];
6748f899262SGao Xiang 
675d95ae5e2SGao Xiang 	if (flags & EROFS_GET_BLOCKS_FIEMAP) {
676d95ae5e2SGao Xiang 		err = z_erofs_get_extent_decompressedlen(&m);
677d95ae5e2SGao Xiang 		if (!err)
678d95ae5e2SGao Xiang 			map->m_flags |= EROFS_MAP_FULL_MAPPED;
679d95ae5e2SGao Xiang 	}
68047e4937aSGao Xiang unmap_out:
68147e4937aSGao Xiang 	if (m.kaddr)
68247e4937aSGao Xiang 		kunmap_atomic(m.kaddr);
68347e4937aSGao Xiang 
68447e4937aSGao Xiang out:
6854f761fa2SGao Xiang 	erofs_dbg("%s, m_la %llu m_pa %llu m_llen %llu m_plen %llu m_flags 0%o",
68647e4937aSGao Xiang 		  __func__, map->m_la, map->m_pa,
68747e4937aSGao Xiang 		  map->m_llen, map->m_plen, map->m_flags);
68847e4937aSGao Xiang 
68947e4937aSGao Xiang 	trace_z_erofs_map_blocks_iter_exit(inode, map, flags, err);
69047e4937aSGao Xiang 
69147e4937aSGao Xiang 	/* aggressively BUG_ON iff CONFIG_EROFS_FS_DEBUG is on */
69247e4937aSGao Xiang 	DBG_BUGON(err < 0 && err != -ENOMEM);
69347e4937aSGao Xiang 	return err;
69447e4937aSGao Xiang }
695eadcd6b5SGao Xiang 
696eadcd6b5SGao Xiang static int z_erofs_iomap_begin_report(struct inode *inode, loff_t offset,
697eadcd6b5SGao Xiang 				loff_t length, unsigned int flags,
698eadcd6b5SGao Xiang 				struct iomap *iomap, struct iomap *srcmap)
699eadcd6b5SGao Xiang {
700eadcd6b5SGao Xiang 	int ret;
701eadcd6b5SGao Xiang 	struct erofs_map_blocks map = { .m_la = offset };
702eadcd6b5SGao Xiang 
703eadcd6b5SGao Xiang 	ret = z_erofs_map_blocks_iter(inode, &map, EROFS_GET_BLOCKS_FIEMAP);
704eadcd6b5SGao Xiang 	if (map.mpage)
705eadcd6b5SGao Xiang 		put_page(map.mpage);
706eadcd6b5SGao Xiang 	if (ret < 0)
707eadcd6b5SGao Xiang 		return ret;
708eadcd6b5SGao Xiang 
709eadcd6b5SGao Xiang 	iomap->bdev = inode->i_sb->s_bdev;
710eadcd6b5SGao Xiang 	iomap->offset = map.m_la;
711eadcd6b5SGao Xiang 	iomap->length = map.m_llen;
712eadcd6b5SGao Xiang 	if (map.m_flags & EROFS_MAP_MAPPED) {
713eadcd6b5SGao Xiang 		iomap->type = IOMAP_MAPPED;
714eadcd6b5SGao Xiang 		iomap->addr = map.m_pa;
715eadcd6b5SGao Xiang 	} else {
716eadcd6b5SGao Xiang 		iomap->type = IOMAP_HOLE;
717eadcd6b5SGao Xiang 		iomap->addr = IOMAP_NULL_ADDR;
718eadcd6b5SGao Xiang 		/*
719eadcd6b5SGao Xiang 		 * No strict rule how to describe extents for post EOF, yet
720eadcd6b5SGao Xiang 		 * we need do like below. Otherwise, iomap itself will get
721eadcd6b5SGao Xiang 		 * into an endless loop on post EOF.
722eadcd6b5SGao Xiang 		 */
723eadcd6b5SGao Xiang 		if (iomap->offset >= inode->i_size)
724eadcd6b5SGao Xiang 			iomap->length = length + map.m_la - offset;
725eadcd6b5SGao Xiang 	}
726eadcd6b5SGao Xiang 	iomap->flags = 0;
727eadcd6b5SGao Xiang 	return 0;
728eadcd6b5SGao Xiang }
729eadcd6b5SGao Xiang 
730eadcd6b5SGao Xiang const struct iomap_ops z_erofs_iomap_report_ops = {
731eadcd6b5SGao Xiang 	.iomap_begin = z_erofs_iomap_begin_report,
732eadcd6b5SGao Xiang };
733