xref: /linux/fs/erofs/zmap.c (revision af2d6148d2a159e1a0862bce5a2c88c1618a2b27)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2018-2019 HUAWEI, Inc.
4  *             https://www.huawei.com/
5  */
6 #include "internal.h"
7 #include <linux/unaligned.h>
8 #include <trace/events/erofs.h>
9 
10 struct z_erofs_maprecorder {
11 	struct inode *inode;
12 	struct erofs_map_blocks *map;
13 	unsigned long lcn;
14 	/* compression extent information gathered */
15 	u8  type, headtype;
16 	u16 clusterofs;
17 	u16 delta[2];
18 	erofs_blk_t pblk, compressedblks;
19 	erofs_off_t nextpackoff;
20 	bool partialref;
21 };
22 
23 static int z_erofs_load_full_lcluster(struct z_erofs_maprecorder *m,
24 				      unsigned long lcn)
25 {
26 	struct inode *const inode = m->inode;
27 	struct erofs_inode *const vi = EROFS_I(inode);
28 	const erofs_off_t pos = Z_EROFS_FULL_INDEX_START(erofs_iloc(inode) +
29 			vi->inode_isize + vi->xattr_isize) +
30 			lcn * sizeof(struct z_erofs_lcluster_index);
31 	struct z_erofs_lcluster_index *di;
32 	unsigned int advise;
33 
34 	di = erofs_read_metabuf(&m->map->buf, inode->i_sb, pos, true);
35 	if (IS_ERR(di))
36 		return PTR_ERR(di);
37 	m->lcn = lcn;
38 	m->nextpackoff = pos + sizeof(struct z_erofs_lcluster_index);
39 
40 	advise = le16_to_cpu(di->di_advise);
41 	m->type = advise & Z_EROFS_LI_LCLUSTER_TYPE_MASK;
42 	if (m->type == Z_EROFS_LCLUSTER_TYPE_NONHEAD) {
43 		m->clusterofs = 1 << vi->z_lclusterbits;
44 		m->delta[0] = le16_to_cpu(di->di_u.delta[0]);
45 		if (m->delta[0] & Z_EROFS_LI_D0_CBLKCNT) {
46 			if (!(vi->z_advise & (Z_EROFS_ADVISE_BIG_PCLUSTER_1 |
47 					Z_EROFS_ADVISE_BIG_PCLUSTER_2))) {
48 				DBG_BUGON(1);
49 				return -EFSCORRUPTED;
50 			}
51 			m->compressedblks = m->delta[0] & ~Z_EROFS_LI_D0_CBLKCNT;
52 			m->delta[0] = 1;
53 		}
54 		m->delta[1] = le16_to_cpu(di->di_u.delta[1]);
55 	} else {
56 		m->partialref = !!(advise & Z_EROFS_LI_PARTIAL_REF);
57 		m->clusterofs = le16_to_cpu(di->di_clusterofs);
58 		if (m->clusterofs >= 1 << vi->z_lclusterbits) {
59 			DBG_BUGON(1);
60 			return -EFSCORRUPTED;
61 		}
62 		m->pblk = le32_to_cpu(di->di_u.blkaddr);
63 	}
64 	return 0;
65 }
66 
67 static unsigned int decode_compactedbits(unsigned int lobits,
68 					 u8 *in, unsigned int pos, u8 *type)
69 {
70 	const unsigned int v = get_unaligned_le32(in + pos / 8) >> (pos & 7);
71 	const unsigned int lo = v & ((1 << lobits) - 1);
72 
73 	*type = (v >> lobits) & 3;
74 	return lo;
75 }
76 
77 static int get_compacted_la_distance(unsigned int lobits,
78 				     unsigned int encodebits,
79 				     unsigned int vcnt, u8 *in, int i)
80 {
81 	unsigned int lo, d1 = 0;
82 	u8 type;
83 
84 	DBG_BUGON(i >= vcnt);
85 
86 	do {
87 		lo = decode_compactedbits(lobits, in, encodebits * i, &type);
88 
89 		if (type != Z_EROFS_LCLUSTER_TYPE_NONHEAD)
90 			return d1;
91 		++d1;
92 	} while (++i < vcnt);
93 
94 	/* vcnt - 1 (Z_EROFS_LCLUSTER_TYPE_NONHEAD) item */
95 	if (!(lo & Z_EROFS_LI_D0_CBLKCNT))
96 		d1 += lo - 1;
97 	return d1;
98 }
99 
100 static int z_erofs_load_compact_lcluster(struct z_erofs_maprecorder *m,
101 					 unsigned long lcn, bool lookahead)
102 {
103 	struct inode *const inode = m->inode;
104 	struct erofs_inode *const vi = EROFS_I(inode);
105 	const erofs_off_t ebase = Z_EROFS_MAP_HEADER_END(erofs_iloc(inode) +
106 			vi->inode_isize + vi->xattr_isize);
107 	const unsigned int lclusterbits = vi->z_lclusterbits;
108 	const unsigned int totalidx = erofs_iblks(inode);
109 	unsigned int compacted_4b_initial, compacted_2b, amortizedshift;
110 	unsigned int vcnt, lo, lobits, encodebits, nblk, bytes;
111 	bool big_pcluster = vi->z_advise & Z_EROFS_ADVISE_BIG_PCLUSTER_1;
112 	erofs_off_t pos;
113 	u8 *in, type;
114 	int i;
115 
116 	if (lcn >= totalidx || lclusterbits > 14)
117 		return -EINVAL;
118 
119 	m->lcn = lcn;
120 	/* used to align to 32-byte (compacted_2b) alignment */
121 	compacted_4b_initial = ((32 - ebase % 32) / 4) & 7;
122 	compacted_2b = 0;
123 	if ((vi->z_advise & Z_EROFS_ADVISE_COMPACTED_2B) &&
124 	    compacted_4b_initial < totalidx)
125 		compacted_2b = rounddown(totalidx - compacted_4b_initial, 16);
126 
127 	pos = ebase;
128 	amortizedshift = 2;	/* compact_4b */
129 	if (lcn >= compacted_4b_initial) {
130 		pos += compacted_4b_initial * 4;
131 		lcn -= compacted_4b_initial;
132 		if (lcn < compacted_2b) {
133 			amortizedshift = 1;
134 		} else {
135 			pos += compacted_2b * 2;
136 			lcn -= compacted_2b;
137 		}
138 	}
139 	pos += lcn * (1 << amortizedshift);
140 
141 	/* figure out the lcluster count in this pack */
142 	if (1 << amortizedshift == 4 && lclusterbits <= 14)
143 		vcnt = 2;
144 	else if (1 << amortizedshift == 2 && lclusterbits <= 12)
145 		vcnt = 16;
146 	else
147 		return -EOPNOTSUPP;
148 
149 	in = erofs_read_metabuf(&m->map->buf, m->inode->i_sb, pos, true);
150 	if (IS_ERR(in))
151 		return PTR_ERR(in);
152 
153 	/* it doesn't equal to round_up(..) */
154 	m->nextpackoff = round_down(pos, vcnt << amortizedshift) +
155 			 (vcnt << amortizedshift);
156 	lobits = max(lclusterbits, ilog2(Z_EROFS_LI_D0_CBLKCNT) + 1U);
157 	encodebits = ((vcnt << amortizedshift) - sizeof(__le32)) * 8 / vcnt;
158 	bytes = pos & ((vcnt << amortizedshift) - 1);
159 	in -= bytes;
160 	i = bytes >> amortizedshift;
161 
162 	lo = decode_compactedbits(lobits, in, encodebits * i, &type);
163 	m->type = type;
164 	if (type == Z_EROFS_LCLUSTER_TYPE_NONHEAD) {
165 		m->clusterofs = 1 << lclusterbits;
166 
167 		/* figure out lookahead_distance: delta[1] if needed */
168 		if (lookahead)
169 			m->delta[1] = get_compacted_la_distance(lobits,
170 						encodebits, vcnt, in, i);
171 		if (lo & Z_EROFS_LI_D0_CBLKCNT) {
172 			if (!big_pcluster) {
173 				DBG_BUGON(1);
174 				return -EFSCORRUPTED;
175 			}
176 			m->compressedblks = lo & ~Z_EROFS_LI_D0_CBLKCNT;
177 			m->delta[0] = 1;
178 			return 0;
179 		} else if (i + 1 != (int)vcnt) {
180 			m->delta[0] = lo;
181 			return 0;
182 		}
183 		/*
184 		 * since the last lcluster in the pack is special,
185 		 * of which lo saves delta[1] rather than delta[0].
186 		 * Hence, get delta[0] by the previous lcluster indirectly.
187 		 */
188 		lo = decode_compactedbits(lobits, in,
189 					  encodebits * (i - 1), &type);
190 		if (type != Z_EROFS_LCLUSTER_TYPE_NONHEAD)
191 			lo = 0;
192 		else if (lo & Z_EROFS_LI_D0_CBLKCNT)
193 			lo = 1;
194 		m->delta[0] = lo + 1;
195 		return 0;
196 	}
197 	m->clusterofs = lo;
198 	m->delta[0] = 0;
199 	/* figout out blkaddr (pblk) for HEAD lclusters */
200 	if (!big_pcluster) {
201 		nblk = 1;
202 		while (i > 0) {
203 			--i;
204 			lo = decode_compactedbits(lobits, in,
205 						  encodebits * i, &type);
206 			if (type == Z_EROFS_LCLUSTER_TYPE_NONHEAD)
207 				i -= lo;
208 
209 			if (i >= 0)
210 				++nblk;
211 		}
212 	} else {
213 		nblk = 0;
214 		while (i > 0) {
215 			--i;
216 			lo = decode_compactedbits(lobits, in,
217 						  encodebits * i, &type);
218 			if (type == Z_EROFS_LCLUSTER_TYPE_NONHEAD) {
219 				if (lo & Z_EROFS_LI_D0_CBLKCNT) {
220 					--i;
221 					nblk += lo & ~Z_EROFS_LI_D0_CBLKCNT;
222 					continue;
223 				}
224 				/* bigpcluster shouldn't have plain d0 == 1 */
225 				if (lo <= 1) {
226 					DBG_BUGON(1);
227 					return -EFSCORRUPTED;
228 				}
229 				i -= lo - 2;
230 				continue;
231 			}
232 			++nblk;
233 		}
234 	}
235 	in += (vcnt << amortizedshift) - sizeof(__le32);
236 	m->pblk = le32_to_cpu(*(__le32 *)in) + nblk;
237 	return 0;
238 }
239 
240 static int z_erofs_load_lcluster_from_disk(struct z_erofs_maprecorder *m,
241 					   unsigned int lcn, bool lookahead)
242 {
243 	switch (EROFS_I(m->inode)->datalayout) {
244 	case EROFS_INODE_COMPRESSED_FULL:
245 		return z_erofs_load_full_lcluster(m, lcn);
246 	case EROFS_INODE_COMPRESSED_COMPACT:
247 		return z_erofs_load_compact_lcluster(m, lcn, lookahead);
248 	default:
249 		return -EINVAL;
250 	}
251 }
252 
253 static int z_erofs_extent_lookback(struct z_erofs_maprecorder *m,
254 				   unsigned int lookback_distance)
255 {
256 	struct super_block *sb = m->inode->i_sb;
257 	struct erofs_inode *const vi = EROFS_I(m->inode);
258 	const unsigned int lclusterbits = vi->z_lclusterbits;
259 
260 	while (m->lcn >= lookback_distance) {
261 		unsigned long lcn = m->lcn - lookback_distance;
262 		int err;
263 
264 		err = z_erofs_load_lcluster_from_disk(m, lcn, false);
265 		if (err)
266 			return err;
267 
268 		if (m->type >= Z_EROFS_LCLUSTER_TYPE_MAX) {
269 			erofs_err(sb, "unknown type %u @ lcn %lu of nid %llu",
270 				  m->type, lcn, vi->nid);
271 			DBG_BUGON(1);
272 			return -EOPNOTSUPP;
273 		} else if (m->type == Z_EROFS_LCLUSTER_TYPE_NONHEAD) {
274 			lookback_distance = m->delta[0];
275 			if (!lookback_distance)
276 				break;
277 			continue;
278 		} else {
279 			m->headtype = m->type;
280 			m->map->m_la = (lcn << lclusterbits) | m->clusterofs;
281 			return 0;
282 		}
283 	}
284 	erofs_err(sb, "bogus lookback distance %u @ lcn %lu of nid %llu",
285 		  lookback_distance, m->lcn, vi->nid);
286 	DBG_BUGON(1);
287 	return -EFSCORRUPTED;
288 }
289 
290 static int z_erofs_get_extent_compressedlen(struct z_erofs_maprecorder *m,
291 					    unsigned int initial_lcn)
292 {
293 	struct inode *inode = m->inode;
294 	struct super_block *sb = inode->i_sb;
295 	struct erofs_inode *vi = EROFS_I(inode);
296 	bool bigpcl1 = vi->z_advise & Z_EROFS_ADVISE_BIG_PCLUSTER_1;
297 	bool bigpcl2 = vi->z_advise & Z_EROFS_ADVISE_BIG_PCLUSTER_2;
298 	unsigned long lcn = m->lcn + 1;
299 	int err;
300 
301 	DBG_BUGON(m->type == Z_EROFS_LCLUSTER_TYPE_NONHEAD);
302 	DBG_BUGON(m->type != m->headtype);
303 
304 	if ((m->headtype == Z_EROFS_LCLUSTER_TYPE_HEAD1 && !bigpcl1) ||
305 	    ((m->headtype == Z_EROFS_LCLUSTER_TYPE_PLAIN ||
306 	      m->headtype == Z_EROFS_LCLUSTER_TYPE_HEAD2) && !bigpcl2) ||
307 	    (lcn << vi->z_lclusterbits) >= inode->i_size)
308 		m->compressedblks = 1;
309 
310 	if (m->compressedblks)
311 		goto out;
312 
313 	err = z_erofs_load_lcluster_from_disk(m, lcn, false);
314 	if (err)
315 		return err;
316 
317 	/*
318 	 * If the 1st NONHEAD lcluster has already been handled initially w/o
319 	 * valid compressedblks, which means at least it mustn't be CBLKCNT, or
320 	 * an internal implemenatation error is detected.
321 	 *
322 	 * The following code can also handle it properly anyway, but let's
323 	 * BUG_ON in the debugging mode only for developers to notice that.
324 	 */
325 	DBG_BUGON(lcn == initial_lcn &&
326 		  m->type == Z_EROFS_LCLUSTER_TYPE_NONHEAD);
327 
328 	if (m->type == Z_EROFS_LCLUSTER_TYPE_NONHEAD) {
329 		if (m->delta[0] != 1) {
330 			erofs_err(sb, "bogus CBLKCNT @ lcn %lu of nid %llu", lcn, vi->nid);
331 			DBG_BUGON(1);
332 			return -EFSCORRUPTED;
333 		}
334 		if (m->compressedblks)
335 			goto out;
336 	} else if (m->type < Z_EROFS_LCLUSTER_TYPE_MAX) {
337 		/*
338 		 * if the 1st NONHEAD lcluster is actually PLAIN or HEAD type
339 		 * rather than CBLKCNT, it's a 1 block-sized pcluster.
340 		 */
341 		m->compressedblks = 1;
342 		goto out;
343 	}
344 	erofs_err(sb, "cannot found CBLKCNT @ lcn %lu of nid %llu", lcn, vi->nid);
345 	DBG_BUGON(1);
346 	return -EFSCORRUPTED;
347 out:
348 	m->map->m_plen = erofs_pos(sb, m->compressedblks);
349 	return 0;
350 }
351 
352 static int z_erofs_get_extent_decompressedlen(struct z_erofs_maprecorder *m)
353 {
354 	struct inode *inode = m->inode;
355 	struct erofs_inode *vi = EROFS_I(inode);
356 	struct erofs_map_blocks *map = m->map;
357 	unsigned int lclusterbits = vi->z_lclusterbits;
358 	u64 lcn = m->lcn, headlcn = map->m_la >> lclusterbits;
359 	int err;
360 
361 	while (1) {
362 		/* handle the last EOF pcluster (no next HEAD lcluster) */
363 		if ((lcn << lclusterbits) >= inode->i_size) {
364 			map->m_llen = inode->i_size - map->m_la;
365 			return 0;
366 		}
367 
368 		err = z_erofs_load_lcluster_from_disk(m, lcn, true);
369 		if (err)
370 			return err;
371 
372 		if (m->type == Z_EROFS_LCLUSTER_TYPE_NONHEAD) {
373 			/* work around invalid d1 generated by pre-1.0 mkfs */
374 			if (unlikely(!m->delta[1])) {
375 				m->delta[1] = 1;
376 				DBG_BUGON(1);
377 			}
378 		} else if (m->type < Z_EROFS_LCLUSTER_TYPE_MAX) {
379 			if (lcn != headlcn)
380 				break;	/* ends at the next HEAD lcluster */
381 			m->delta[1] = 1;
382 		} else {
383 			erofs_err(inode->i_sb, "unknown type %u @ lcn %llu of nid %llu",
384 				  m->type, lcn, vi->nid);
385 			DBG_BUGON(1);
386 			return -EOPNOTSUPP;
387 		}
388 		lcn += m->delta[1];
389 	}
390 	map->m_llen = (lcn << lclusterbits) + m->clusterofs - map->m_la;
391 	return 0;
392 }
393 
394 static int z_erofs_map_blocks_fo(struct inode *inode,
395 				 struct erofs_map_blocks *map, int flags)
396 {
397 	struct erofs_inode *vi = EROFS_I(inode);
398 	struct super_block *sb = inode->i_sb;
399 	bool fragment = vi->z_advise & Z_EROFS_ADVISE_FRAGMENT_PCLUSTER;
400 	bool ztailpacking = vi->z_idata_size;
401 	unsigned int lclusterbits = vi->z_lclusterbits;
402 	struct z_erofs_maprecorder m = {
403 		.inode = inode,
404 		.map = map,
405 	};
406 	int err = 0;
407 	unsigned int endoff, afmt;
408 	unsigned long initial_lcn;
409 	unsigned long long ofs, end;
410 
411 	ofs = flags & EROFS_GET_BLOCKS_FINDTAIL ? inode->i_size - 1 : map->m_la;
412 	if (fragment && !(flags & EROFS_GET_BLOCKS_FINDTAIL) &&
413 	    !vi->z_tailextent_headlcn) {
414 		map->m_la = 0;
415 		map->m_llen = inode->i_size;
416 		map->m_flags = EROFS_MAP_FRAGMENT;
417 		return 0;
418 	}
419 	initial_lcn = ofs >> lclusterbits;
420 	endoff = ofs & ((1 << lclusterbits) - 1);
421 
422 	err = z_erofs_load_lcluster_from_disk(&m, initial_lcn, false);
423 	if (err)
424 		goto unmap_out;
425 
426 	if ((flags & EROFS_GET_BLOCKS_FINDTAIL) && ztailpacking)
427 		vi->z_fragmentoff = m.nextpackoff;
428 	map->m_flags = EROFS_MAP_MAPPED | EROFS_MAP_ENCODED;
429 	end = (m.lcn + 1ULL) << lclusterbits;
430 
431 	switch (m.type) {
432 	case Z_EROFS_LCLUSTER_TYPE_PLAIN:
433 	case Z_EROFS_LCLUSTER_TYPE_HEAD1:
434 	case Z_EROFS_LCLUSTER_TYPE_HEAD2:
435 		if (endoff >= m.clusterofs) {
436 			m.headtype = m.type;
437 			map->m_la = (m.lcn << lclusterbits) | m.clusterofs;
438 			/*
439 			 * For ztailpacking files, in order to inline data more
440 			 * effectively, special EOF lclusters are now supported
441 			 * which can have three parts at most.
442 			 */
443 			if (ztailpacking && end > inode->i_size)
444 				end = inode->i_size;
445 			break;
446 		}
447 		/* m.lcn should be >= 1 if endoff < m.clusterofs */
448 		if (!m.lcn) {
449 			erofs_err(sb, "invalid logical cluster 0 at nid %llu",
450 				  vi->nid);
451 			err = -EFSCORRUPTED;
452 			goto unmap_out;
453 		}
454 		end = (m.lcn << lclusterbits) | m.clusterofs;
455 		map->m_flags |= EROFS_MAP_FULL_MAPPED;
456 		m.delta[0] = 1;
457 		fallthrough;
458 	case Z_EROFS_LCLUSTER_TYPE_NONHEAD:
459 		/* get the corresponding first chunk */
460 		err = z_erofs_extent_lookback(&m, m.delta[0]);
461 		if (err)
462 			goto unmap_out;
463 		break;
464 	default:
465 		erofs_err(sb, "unknown type %u @ offset %llu of nid %llu",
466 			  m.type, ofs, vi->nid);
467 		err = -EOPNOTSUPP;
468 		goto unmap_out;
469 	}
470 	if (m.partialref)
471 		map->m_flags |= EROFS_MAP_PARTIAL_REF;
472 	map->m_llen = end - map->m_la;
473 
474 	if (flags & EROFS_GET_BLOCKS_FINDTAIL) {
475 		vi->z_tailextent_headlcn = m.lcn;
476 		/* for non-compact indexes, fragmentoff is 64 bits */
477 		if (fragment && vi->datalayout == EROFS_INODE_COMPRESSED_FULL)
478 			vi->z_fragmentoff |= (u64)m.pblk << 32;
479 	}
480 	if (ztailpacking && m.lcn == vi->z_tailextent_headlcn) {
481 		map->m_flags |= EROFS_MAP_META;
482 		map->m_pa = vi->z_fragmentoff;
483 		map->m_plen = vi->z_idata_size;
484 		if (erofs_blkoff(sb, map->m_pa) + map->m_plen > sb->s_blocksize) {
485 			erofs_err(sb, "invalid tail-packing pclustersize %llu",
486 				  map->m_plen);
487 			err = -EFSCORRUPTED;
488 			goto unmap_out;
489 		}
490 	} else if (fragment && m.lcn == vi->z_tailextent_headlcn) {
491 		map->m_flags = EROFS_MAP_FRAGMENT;
492 	} else {
493 		map->m_pa = erofs_pos(sb, m.pblk);
494 		err = z_erofs_get_extent_compressedlen(&m, initial_lcn);
495 		if (err)
496 			goto unmap_out;
497 	}
498 
499 	if (m.headtype == Z_EROFS_LCLUSTER_TYPE_PLAIN) {
500 		if (map->m_llen > map->m_plen) {
501 			DBG_BUGON(1);
502 			err = -EFSCORRUPTED;
503 			goto unmap_out;
504 		}
505 		afmt = vi->z_advise & Z_EROFS_ADVISE_INTERLACED_PCLUSTER ?
506 			Z_EROFS_COMPRESSION_INTERLACED :
507 			Z_EROFS_COMPRESSION_SHIFTED;
508 	} else {
509 		afmt = m.headtype == Z_EROFS_LCLUSTER_TYPE_HEAD2 ?
510 			vi->z_algorithmtype[1] : vi->z_algorithmtype[0];
511 		if (!(EROFS_I_SB(inode)->available_compr_algs & (1 << afmt))) {
512 			erofs_err(sb, "inconsistent algorithmtype %u for nid %llu",
513 				  afmt, vi->nid);
514 			err = -EFSCORRUPTED;
515 			goto unmap_out;
516 		}
517 	}
518 	map->m_algorithmformat = afmt;
519 
520 	if ((flags & EROFS_GET_BLOCKS_FIEMAP) ||
521 	    ((flags & EROFS_GET_BLOCKS_READMORE) &&
522 	     (map->m_algorithmformat == Z_EROFS_COMPRESSION_LZMA ||
523 	      map->m_algorithmformat == Z_EROFS_COMPRESSION_DEFLATE ||
524 	      map->m_algorithmformat == Z_EROFS_COMPRESSION_ZSTD) &&
525 	      map->m_llen >= i_blocksize(inode))) {
526 		err = z_erofs_get_extent_decompressedlen(&m);
527 		if (!err)
528 			map->m_flags |= EROFS_MAP_FULL_MAPPED;
529 	}
530 
531 unmap_out:
532 	erofs_unmap_metabuf(&m.map->buf);
533 	return err;
534 }
535 
536 static int z_erofs_map_blocks_ext(struct inode *inode,
537 				  struct erofs_map_blocks *map, int flags)
538 {
539 	struct erofs_inode *vi = EROFS_I(inode);
540 	struct super_block *sb = inode->i_sb;
541 	bool interlaced = vi->z_advise & Z_EROFS_ADVISE_INTERLACED_PCLUSTER;
542 	unsigned int recsz = z_erofs_extent_recsize(vi->z_advise);
543 	erofs_off_t pos = round_up(Z_EROFS_MAP_HEADER_END(erofs_iloc(inode) +
544 				   vi->inode_isize + vi->xattr_isize), recsz);
545 	erofs_off_t lend = inode->i_size;
546 	erofs_off_t l, r, mid, pa, la, lstart;
547 	struct z_erofs_extent *ext;
548 	unsigned int fmt;
549 	bool last;
550 
551 	map->m_flags = 0;
552 	if (recsz <= offsetof(struct z_erofs_extent, pstart_hi)) {
553 		if (recsz <= offsetof(struct z_erofs_extent, pstart_lo)) {
554 			ext = erofs_read_metabuf(&map->buf, sb, pos, true);
555 			if (IS_ERR(ext))
556 				return PTR_ERR(ext);
557 			pa = le64_to_cpu(*(__le64 *)ext);
558 			pos += sizeof(__le64);
559 			lstart = 0;
560 		} else {
561 			lstart = round_down(map->m_la, 1 << vi->z_lclusterbits);
562 			pos += (lstart >> vi->z_lclusterbits) * recsz;
563 			pa = EROFS_NULL_ADDR;
564 		}
565 
566 		for (; lstart <= map->m_la; lstart += 1 << vi->z_lclusterbits) {
567 			ext = erofs_read_metabuf(&map->buf, sb, pos, true);
568 			if (IS_ERR(ext))
569 				return PTR_ERR(ext);
570 			map->m_plen = le32_to_cpu(ext->plen);
571 			if (pa != EROFS_NULL_ADDR) {
572 				map->m_pa = pa;
573 				pa += map->m_plen & Z_EROFS_EXTENT_PLEN_MASK;
574 			} else {
575 				map->m_pa = le32_to_cpu(ext->pstart_lo);
576 			}
577 			pos += recsz;
578 		}
579 		last = (lstart >= round_up(lend, 1 << vi->z_lclusterbits));
580 		lend = min(lstart, lend);
581 		lstart -= 1 << vi->z_lclusterbits;
582 	} else {
583 		lstart = lend;
584 		for (l = 0, r = vi->z_extents; l < r; ) {
585 			mid = l + (r - l) / 2;
586 			ext = erofs_read_metabuf(&map->buf, sb,
587 						 pos + mid * recsz, true);
588 			if (IS_ERR(ext))
589 				return PTR_ERR(ext);
590 
591 			la = le32_to_cpu(ext->lstart_lo);
592 			pa = le32_to_cpu(ext->pstart_lo) |
593 				(u64)le32_to_cpu(ext->pstart_hi) << 32;
594 			if (recsz > offsetof(struct z_erofs_extent, lstart_hi))
595 				la |= (u64)le32_to_cpu(ext->lstart_hi) << 32;
596 
597 			if (la > map->m_la) {
598 				r = mid;
599 				if (la > lend) {
600 					DBG_BUGON(1);
601 					return -EFSCORRUPTED;
602 				}
603 				lend = la;
604 			} else {
605 				l = mid + 1;
606 				if (map->m_la == la)
607 					r = min(l + 1, r);
608 				lstart = la;
609 				map->m_plen = le32_to_cpu(ext->plen);
610 				map->m_pa = pa;
611 			}
612 		}
613 		last = (l >= vi->z_extents);
614 	}
615 
616 	if (lstart < lend) {
617 		map->m_la = lstart;
618 		if (last && (vi->z_advise & Z_EROFS_ADVISE_FRAGMENT_PCLUSTER)) {
619 			map->m_flags = EROFS_MAP_FRAGMENT;
620 			vi->z_fragmentoff = map->m_plen;
621 			if (recsz > offsetof(struct z_erofs_extent, pstart_lo))
622 				vi->z_fragmentoff |= map->m_pa << 32;
623 		} else if (map->m_plen) {
624 			map->m_flags |= EROFS_MAP_MAPPED |
625 				EROFS_MAP_FULL_MAPPED | EROFS_MAP_ENCODED;
626 			fmt = map->m_plen >> Z_EROFS_EXTENT_PLEN_FMT_BIT;
627 			if (fmt)
628 				map->m_algorithmformat = fmt - 1;
629 			else if (interlaced && !erofs_blkoff(sb, map->m_pa))
630 				map->m_algorithmformat =
631 					Z_EROFS_COMPRESSION_INTERLACED;
632 			else
633 				map->m_algorithmformat =
634 					Z_EROFS_COMPRESSION_SHIFTED;
635 			if (map->m_plen & Z_EROFS_EXTENT_PLEN_PARTIAL)
636 				map->m_flags |= EROFS_MAP_PARTIAL_REF;
637 			map->m_plen &= Z_EROFS_EXTENT_PLEN_MASK;
638 		}
639 	}
640 	map->m_llen = lend - map->m_la;
641 	return 0;
642 }
643 
644 static int z_erofs_fill_inode_lazy(struct inode *inode)
645 {
646 	struct erofs_inode *const vi = EROFS_I(inode);
647 	struct super_block *const sb = inode->i_sb;
648 	int err, headnr;
649 	erofs_off_t pos;
650 	struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
651 	struct z_erofs_map_header *h;
652 
653 	if (test_bit(EROFS_I_Z_INITED_BIT, &vi->flags)) {
654 		/*
655 		 * paired with smp_mb() at the end of the function to ensure
656 		 * fields will only be observed after the bit is set.
657 		 */
658 		smp_mb();
659 		return 0;
660 	}
661 
662 	if (wait_on_bit_lock(&vi->flags, EROFS_I_BL_Z_BIT, TASK_KILLABLE))
663 		return -ERESTARTSYS;
664 
665 	err = 0;
666 	if (test_bit(EROFS_I_Z_INITED_BIT, &vi->flags))
667 		goto out_unlock;
668 
669 	pos = ALIGN(erofs_iloc(inode) + vi->inode_isize + vi->xattr_isize, 8);
670 	h = erofs_read_metabuf(&buf, sb, pos, true);
671 	if (IS_ERR(h)) {
672 		err = PTR_ERR(h);
673 		goto out_unlock;
674 	}
675 
676 	/*
677 	 * if the highest bit of the 8-byte map header is set, the whole file
678 	 * is stored in the packed inode. The rest bits keeps z_fragmentoff.
679 	 */
680 	if (h->h_clusterbits >> Z_EROFS_FRAGMENT_INODE_BIT) {
681 		vi->z_advise = Z_EROFS_ADVISE_FRAGMENT_PCLUSTER;
682 		vi->z_fragmentoff = le64_to_cpu(*(__le64 *)h) ^ (1ULL << 63);
683 		vi->z_tailextent_headlcn = 0;
684 		goto done;
685 	}
686 	vi->z_advise = le16_to_cpu(h->h_advise);
687 	vi->z_lclusterbits = sb->s_blocksize_bits + (h->h_clusterbits & 15);
688 	if (vi->datalayout == EROFS_INODE_COMPRESSED_FULL &&
689 	    (vi->z_advise & Z_EROFS_ADVISE_EXTENTS)) {
690 		vi->z_extents = le32_to_cpu(h->h_extents_lo) |
691 			((u64)le16_to_cpu(h->h_extents_hi) << 32);
692 		goto done;
693 	}
694 
695 	vi->z_algorithmtype[0] = h->h_algorithmtype & 15;
696 	vi->z_algorithmtype[1] = h->h_algorithmtype >> 4;
697 	if (vi->z_advise & Z_EROFS_ADVISE_FRAGMENT_PCLUSTER)
698 		vi->z_fragmentoff = le32_to_cpu(h->h_fragmentoff);
699 	else if (vi->z_advise & Z_EROFS_ADVISE_INLINE_PCLUSTER)
700 		vi->z_idata_size = le16_to_cpu(h->h_idata_size);
701 
702 	headnr = 0;
703 	if (vi->z_algorithmtype[0] >= Z_EROFS_COMPRESSION_MAX ||
704 	    vi->z_algorithmtype[++headnr] >= Z_EROFS_COMPRESSION_MAX) {
705 		erofs_err(sb, "unknown HEAD%u format %u for nid %llu, please upgrade kernel",
706 			  headnr + 1, vi->z_algorithmtype[headnr], vi->nid);
707 		err = -EOPNOTSUPP;
708 		goto out_put_metabuf;
709 	}
710 
711 	if (!erofs_sb_has_big_pcluster(EROFS_SB(sb)) &&
712 	    vi->z_advise & (Z_EROFS_ADVISE_BIG_PCLUSTER_1 |
713 			    Z_EROFS_ADVISE_BIG_PCLUSTER_2)) {
714 		erofs_err(sb, "per-inode big pcluster without sb feature for nid %llu",
715 			  vi->nid);
716 		err = -EFSCORRUPTED;
717 		goto out_put_metabuf;
718 	}
719 	if (vi->datalayout == EROFS_INODE_COMPRESSED_COMPACT &&
720 	    !(vi->z_advise & Z_EROFS_ADVISE_BIG_PCLUSTER_1) ^
721 	    !(vi->z_advise & Z_EROFS_ADVISE_BIG_PCLUSTER_2)) {
722 		erofs_err(sb, "big pcluster head1/2 of compact indexes should be consistent for nid %llu",
723 			  vi->nid);
724 		err = -EFSCORRUPTED;
725 		goto out_put_metabuf;
726 	}
727 
728 	if (vi->z_idata_size ||
729 	    (vi->z_advise & Z_EROFS_ADVISE_FRAGMENT_PCLUSTER)) {
730 		struct erofs_map_blocks map = {
731 			.buf = __EROFS_BUF_INITIALIZER
732 		};
733 
734 		err = z_erofs_map_blocks_fo(inode, &map,
735 					    EROFS_GET_BLOCKS_FINDTAIL);
736 		erofs_put_metabuf(&map.buf);
737 		if (err < 0)
738 			goto out_put_metabuf;
739 	}
740 done:
741 	/* paired with smp_mb() at the beginning of the function */
742 	smp_mb();
743 	set_bit(EROFS_I_Z_INITED_BIT, &vi->flags);
744 out_put_metabuf:
745 	erofs_put_metabuf(&buf);
746 out_unlock:
747 	clear_and_wake_up_bit(EROFS_I_BL_Z_BIT, &vi->flags);
748 	return err;
749 }
750 
751 int z_erofs_map_blocks_iter(struct inode *inode, struct erofs_map_blocks *map,
752 			    int flags)
753 {
754 	struct erofs_inode *const vi = EROFS_I(inode);
755 	int err = 0;
756 
757 	trace_erofs_map_blocks_enter(inode, map, flags);
758 	if (map->m_la >= inode->i_size) {	/* post-EOF unmapped extent */
759 		map->m_llen = map->m_la + 1 - inode->i_size;
760 		map->m_la = inode->i_size;
761 		map->m_flags = 0;
762 	} else {
763 		err = z_erofs_fill_inode_lazy(inode);
764 		if (!err) {
765 			if (vi->datalayout == EROFS_INODE_COMPRESSED_FULL &&
766 			    (vi->z_advise & Z_EROFS_ADVISE_EXTENTS))
767 				err = z_erofs_map_blocks_ext(inode, map, flags);
768 			else
769 				err = z_erofs_map_blocks_fo(inode, map, flags);
770 		}
771 		if (!err && (map->m_flags & EROFS_MAP_ENCODED) &&
772 		    unlikely(map->m_plen > Z_EROFS_PCLUSTER_MAX_SIZE ||
773 			     map->m_llen > Z_EROFS_PCLUSTER_MAX_DSIZE))
774 			err = -EOPNOTSUPP;
775 		if (err)
776 			map->m_llen = 0;
777 	}
778 	trace_erofs_map_blocks_exit(inode, map, flags, err);
779 	return err;
780 }
781 
782 static int z_erofs_iomap_begin_report(struct inode *inode, loff_t offset,
783 				loff_t length, unsigned int flags,
784 				struct iomap *iomap, struct iomap *srcmap)
785 {
786 	int ret;
787 	struct erofs_map_blocks map = { .m_la = offset };
788 
789 	ret = z_erofs_map_blocks_iter(inode, &map, EROFS_GET_BLOCKS_FIEMAP);
790 	erofs_put_metabuf(&map.buf);
791 	if (ret < 0)
792 		return ret;
793 
794 	iomap->bdev = inode->i_sb->s_bdev;
795 	iomap->offset = map.m_la;
796 	iomap->length = map.m_llen;
797 	if (map.m_flags & EROFS_MAP_MAPPED) {
798 		iomap->type = IOMAP_MAPPED;
799 		iomap->addr = map.m_flags & __EROFS_MAP_FRAGMENT ?
800 			      IOMAP_NULL_ADDR : map.m_pa;
801 	} else {
802 		iomap->type = IOMAP_HOLE;
803 		iomap->addr = IOMAP_NULL_ADDR;
804 		/*
805 		 * No strict rule on how to describe extents for post EOF, yet
806 		 * we need to do like below. Otherwise, iomap itself will get
807 		 * into an endless loop on post EOF.
808 		 *
809 		 * Calculate the effective offset by subtracting extent start
810 		 * (map.m_la) from the requested offset, and add it to length.
811 		 * (NB: offset >= map.m_la always)
812 		 */
813 		if (iomap->offset >= inode->i_size)
814 			iomap->length = length + offset - map.m_la;
815 	}
816 	iomap->flags = 0;
817 	return 0;
818 }
819 
820 const struct iomap_ops z_erofs_iomap_report_ops = {
821 	.iomap_begin = z_erofs_iomap_begin_report,
822 };
823