1 // SPDX-License-Identifier: GPL-2.0+
4 int z_erofs_fill_inode(struct erofs_inode *vi)
6 if (!erofs_sb_has_big_pcluster() &&
7 vi->datalayout == EROFS_INODE_FLAT_COMPRESSION_LEGACY) {
9 vi->z_algorithmtype[0] = 0;
10 vi->z_algorithmtype[1] = 0;
11 vi->z_logical_clusterbits = LOG_BLOCK_SIZE;
13 vi->flags |= EROFS_I_Z_INITED;
18 static int z_erofs_fill_inode_lazy(struct erofs_inode *vi)
22 struct z_erofs_map_header *h;
23 char buf[sizeof(struct z_erofs_map_header)];
25 if (vi->flags & EROFS_I_Z_INITED)
28 DBG_BUGON(!erofs_sb_has_big_pcluster() &&
29 vi->datalayout == EROFS_INODE_FLAT_COMPRESSION_LEGACY);
30 pos = round_up(iloc(vi->nid) + vi->inode_isize + vi->xattr_isize, 8);
32 ret = erofs_dev_read(0, buf, pos, sizeof(buf));
36 h = (struct z_erofs_map_header *)buf;
37 vi->z_advise = le16_to_cpu(h->h_advise);
38 vi->z_algorithmtype[0] = h->h_algorithmtype & 15;
39 vi->z_algorithmtype[1] = h->h_algorithmtype >> 4;
41 if (vi->z_algorithmtype[0] >= Z_EROFS_COMPRESSION_MAX) {
42 erofs_err("unknown compression format %u for nid %llu",
43 vi->z_algorithmtype[0], (unsigned long long)vi->nid);
47 vi->z_logical_clusterbits = LOG_BLOCK_SIZE + (h->h_clusterbits & 7);
48 if (vi->datalayout == EROFS_INODE_FLAT_COMPRESSION &&
49 !(vi->z_advise & Z_EROFS_ADVISE_BIG_PCLUSTER_1) ^
50 !(vi->z_advise & Z_EROFS_ADVISE_BIG_PCLUSTER_2)) {
51 erofs_err("big pcluster head1/2 of compact indexes should be consistent for nid %llu",
55 vi->flags |= EROFS_I_Z_INITED;
59 struct z_erofs_maprecorder {
60 struct erofs_inode *inode;
61 struct erofs_map_blocks *map;
65 /* compression extent information gathered */
69 erofs_blk_t pblk, compressedlcs;
72 static int z_erofs_reload_indexes(struct z_erofs_maprecorder *m,
76 struct erofs_map_blocks *const map = m->map;
77 char *mpage = map->mpage;
79 if (map->index == eblk)
82 ret = erofs_blk_read(mpage, eblk, 1);
91 static int legacy_load_cluster_from_disk(struct z_erofs_maprecorder *m,
94 struct erofs_inode *const vi = m->inode;
95 const erofs_off_t ibase = iloc(vi->nid);
96 const erofs_off_t pos =
97 Z_EROFS_VLE_LEGACY_INDEX_ALIGN(ibase + vi->inode_isize +
99 lcn * sizeof(struct z_erofs_vle_decompressed_index);
100 struct z_erofs_vle_decompressed_index *di;
101 unsigned int advise, type;
104 err = z_erofs_reload_indexes(m, erofs_blknr(pos));
109 di = m->kaddr + erofs_blkoff(pos);
111 advise = le16_to_cpu(di->di_advise);
112 type = (advise >> Z_EROFS_VLE_DI_CLUSTER_TYPE_BIT) &
113 ((1 << Z_EROFS_VLE_DI_CLUSTER_TYPE_BITS) - 1);
115 case Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD:
116 m->clusterofs = 1 << vi->z_logical_clusterbits;
117 m->delta[0] = le16_to_cpu(di->di_u.delta[0]);
118 if (m->delta[0] & Z_EROFS_VLE_DI_D0_CBLKCNT) {
119 if (!(vi->z_advise & Z_EROFS_ADVISE_BIG_PCLUSTER_1)) {
121 return -EFSCORRUPTED;
123 m->compressedlcs = m->delta[0] &
124 ~Z_EROFS_VLE_DI_D0_CBLKCNT;
127 m->delta[1] = le16_to_cpu(di->di_u.delta[1]);
129 case Z_EROFS_VLE_CLUSTER_TYPE_PLAIN:
130 case Z_EROFS_VLE_CLUSTER_TYPE_HEAD:
131 m->clusterofs = le16_to_cpu(di->di_clusterofs);
132 m->pblk = le32_to_cpu(di->di_u.blkaddr);
142 static unsigned int decode_compactedbits(unsigned int lobits,
144 u8 *in, unsigned int pos, u8 *type)
146 const unsigned int v = get_unaligned_le32(in + pos / 8) >> (pos & 7);
147 const unsigned int lo = v & lomask;
149 *type = (v >> lobits) & 3;
153 static int get_compacted_la_distance(unsigned int lclusterbits,
154 unsigned int encodebits,
155 unsigned int vcnt, u8 *in, int i)
157 const unsigned int lomask = (1 << lclusterbits) - 1;
158 unsigned int lo, d1 = 0;
161 DBG_BUGON(i >= vcnt);
164 lo = decode_compactedbits(lclusterbits, lomask,
165 in, encodebits * i, &type);
167 if (type != Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD)
170 } while (++i < vcnt);
172 /* vcnt - 1 (Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD) item */
173 if (!(lo & Z_EROFS_VLE_DI_D0_CBLKCNT))
178 static int unpack_compacted_index(struct z_erofs_maprecorder *m,
179 unsigned int amortizedshift,
180 unsigned int eofs, bool lookahead)
182 struct erofs_inode *const vi = m->inode;
183 const unsigned int lclusterbits = vi->z_logical_clusterbits;
184 const unsigned int lomask = (1 << lclusterbits) - 1;
185 unsigned int vcnt, base, lo, encodebits, nblk;
190 if (1 << amortizedshift == 4)
192 else if (1 << amortizedshift == 2 && lclusterbits == 12)
197 big_pcluster = vi->z_advise & Z_EROFS_ADVISE_BIG_PCLUSTER_1;
198 encodebits = ((vcnt << amortizedshift) - sizeof(__le32)) * 8 / vcnt;
199 base = round_down(eofs, vcnt << amortizedshift);
200 in = m->kaddr + base;
202 i = (eofs - base) >> amortizedshift;
204 lo = decode_compactedbits(lclusterbits, lomask,
205 in, encodebits * i, &type);
207 if (type == Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD) {
208 m->clusterofs = 1 << lclusterbits;
210 /* figure out lookahead_distance: delta[1] if needed */
212 m->delta[1] = get_compacted_la_distance(lclusterbits,
215 if (lo & Z_EROFS_VLE_DI_D0_CBLKCNT) {
218 return -EFSCORRUPTED;
220 m->compressedlcs = lo & ~Z_EROFS_VLE_DI_D0_CBLKCNT;
223 } else if (i + 1 != (int)vcnt) {
228 * since the last lcluster in the pack is special,
229 * of which lo saves delta[1] rather than delta[0].
230 * Hence, get delta[0] by the previous lcluster indirectly.
232 lo = decode_compactedbits(lclusterbits, lomask,
233 in, encodebits * (i - 1), &type);
234 if (type != Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD)
236 else if (lo & Z_EROFS_VLE_DI_D0_CBLKCNT)
238 m->delta[0] = lo + 1;
243 /* figout out blkaddr (pblk) for HEAD lclusters */
248 lo = decode_compactedbits(lclusterbits, lomask,
249 in, encodebits * i, &type);
250 if (type == Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD)
260 lo = decode_compactedbits(lclusterbits, lomask,
261 in, encodebits * i, &type);
262 if (type == Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD) {
263 if (lo & Z_EROFS_VLE_DI_D0_CBLKCNT) {
265 nblk += lo & ~Z_EROFS_VLE_DI_D0_CBLKCNT;
270 /* --i; ++nblk; continue; */
271 return -EFSCORRUPTED;
279 in += (vcnt << amortizedshift) - sizeof(__le32);
280 m->pblk = le32_to_cpu(*(__le32 *)in) + nblk;
284 static int compacted_load_cluster_from_disk(struct z_erofs_maprecorder *m,
285 unsigned long lcn, bool lookahead)
287 struct erofs_inode *const vi = m->inode;
288 const unsigned int lclusterbits = vi->z_logical_clusterbits;
289 const erofs_off_t ebase = round_up(iloc(vi->nid) + vi->inode_isize +
290 vi->xattr_isize, 8) +
291 sizeof(struct z_erofs_map_header);
292 const unsigned int totalidx = DIV_ROUND_UP(vi->i_size, EROFS_BLKSIZ);
293 unsigned int compacted_4b_initial, compacted_2b;
294 unsigned int amortizedshift;
298 if (lclusterbits != 12)
305 /* used to align to 32-byte (compacted_2b) alignment */
306 compacted_4b_initial = (32 - ebase % 32) / 4;
307 if (compacted_4b_initial == 32 / 4)
308 compacted_4b_initial = 0;
310 if (vi->z_advise & Z_EROFS_ADVISE_COMPACTED_2B)
311 compacted_2b = rounddown(totalidx - compacted_4b_initial, 16);
316 if (lcn < compacted_4b_initial) {
320 pos += compacted_4b_initial * 4;
321 lcn -= compacted_4b_initial;
323 if (lcn < compacted_2b) {
327 pos += compacted_2b * 2;
331 pos += lcn * (1 << amortizedshift);
332 err = z_erofs_reload_indexes(m, erofs_blknr(pos));
335 return unpack_compacted_index(m, amortizedshift, erofs_blkoff(pos),
339 static int z_erofs_load_cluster_from_disk(struct z_erofs_maprecorder *m,
340 unsigned int lcn, bool lookahead)
342 const unsigned int datamode = m->inode->datalayout;
344 if (datamode == EROFS_INODE_FLAT_COMPRESSION_LEGACY)
345 return legacy_load_cluster_from_disk(m, lcn);
347 if (datamode == EROFS_INODE_FLAT_COMPRESSION)
348 return compacted_load_cluster_from_disk(m, lcn, lookahead);
353 static int z_erofs_extent_lookback(struct z_erofs_maprecorder *m,
354 unsigned int lookback_distance)
356 struct erofs_inode *const vi = m->inode;
357 struct erofs_map_blocks *const map = m->map;
358 const unsigned int lclusterbits = vi->z_logical_clusterbits;
359 unsigned long lcn = m->lcn;
362 if (lcn < lookback_distance) {
363 erofs_err("bogus lookback distance @ nid %llu",
364 (unsigned long long)vi->nid);
366 return -EFSCORRUPTED;
369 /* load extent head logical cluster if needed */
370 lcn -= lookback_distance;
371 err = z_erofs_load_cluster_from_disk(m, lcn, false);
376 case Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD:
378 erofs_err("invalid lookback distance 0 @ nid %llu",
379 (unsigned long long)vi->nid);
381 return -EFSCORRUPTED;
383 return z_erofs_extent_lookback(m, m->delta[0]);
384 case Z_EROFS_VLE_CLUSTER_TYPE_PLAIN:
385 case Z_EROFS_VLE_CLUSTER_TYPE_HEAD:
386 m->headtype = m->type;
387 map->m_la = (lcn << lclusterbits) | m->clusterofs;
390 erofs_err("unknown type %u @ lcn %lu of nid %llu",
391 m->type, lcn, (unsigned long long)vi->nid);
398 static int z_erofs_get_extent_compressedlen(struct z_erofs_maprecorder *m,
399 unsigned int initial_lcn)
401 struct erofs_inode *const vi = m->inode;
402 struct erofs_map_blocks *const map = m->map;
403 const unsigned int lclusterbits = vi->z_logical_clusterbits;
407 DBG_BUGON(m->type != Z_EROFS_VLE_CLUSTER_TYPE_PLAIN &&
408 m->type != Z_EROFS_VLE_CLUSTER_TYPE_HEAD);
409 if (m->headtype == Z_EROFS_VLE_CLUSTER_TYPE_PLAIN ||
410 !(vi->z_advise & Z_EROFS_ADVISE_BIG_PCLUSTER_1)) {
411 map->m_plen = 1 << lclusterbits;
416 if (m->compressedlcs)
419 err = z_erofs_load_cluster_from_disk(m, lcn, false);
424 * If the 1st NONHEAD lcluster has already been handled initially w/o
425 * valid compressedlcs, which means at least it mustn't be CBLKCNT, or
426 * an internal implemenatation error is detected.
428 * The following code can also handle it properly anyway, but let's
429 * BUG_ON in the debugging mode only for developers to notice that.
431 DBG_BUGON(lcn == initial_lcn &&
432 m->type == Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD);
435 case Z_EROFS_VLE_CLUSTER_TYPE_PLAIN:
436 case Z_EROFS_VLE_CLUSTER_TYPE_HEAD:
438 * if the 1st NONHEAD lcluster is actually PLAIN or HEAD type
439 * rather than CBLKCNT, it's a 1 lcluster-sized pcluster.
441 m->compressedlcs = 1;
443 case Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD:
444 if (m->delta[0] != 1)
445 goto err_bonus_cblkcnt;
446 if (m->compressedlcs)
450 erofs_err("cannot found CBLKCNT @ lcn %lu of nid %llu",
451 lcn, vi->nid | 0ULL);
453 return -EFSCORRUPTED;
456 map->m_plen = m->compressedlcs << lclusterbits;
459 erofs_err("bogus CBLKCNT @ lcn %lu of nid %llu",
460 lcn, vi->nid | 0ULL);
462 return -EFSCORRUPTED;
465 static int z_erofs_get_extent_decompressedlen(struct z_erofs_maprecorder *m)
467 struct erofs_inode *const vi = m->inode;
468 struct erofs_map_blocks *map = m->map;
469 unsigned int lclusterbits = vi->z_logical_clusterbits;
470 u64 lcn = m->lcn, headlcn = map->m_la >> lclusterbits;
474 /* handle the last EOF pcluster (no next HEAD lcluster) */
475 if ((lcn << lclusterbits) >= vi->i_size) {
476 map->m_llen = vi->i_size - map->m_la;
480 err = z_erofs_load_cluster_from_disk(m, lcn, true);
484 if (m->type == Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD) {
485 DBG_BUGON(!m->delta[1] &&
486 m->clusterofs != 1 << lclusterbits);
487 } else if (m->type == Z_EROFS_VLE_CLUSTER_TYPE_PLAIN ||
488 m->type == Z_EROFS_VLE_CLUSTER_TYPE_HEAD) {
489 /* go on until the next HEAD lcluster */
494 erofs_err("unknown type %u @ lcn %llu of nid %llu",
496 (unsigned long long)vi->nid);
501 } while (m->delta[1]);
503 map->m_llen = (lcn << lclusterbits) + m->clusterofs - map->m_la;
507 int z_erofs_map_blocks_iter(struct erofs_inode *vi,
508 struct erofs_map_blocks *map,
511 struct z_erofs_maprecorder m = {
517 unsigned int lclusterbits, endoff;
518 unsigned long initial_lcn;
519 unsigned long long ofs, end;
521 /* when trying to read beyond EOF, leave it unmapped */
522 if (map->m_la >= vi->i_size) {
523 map->m_llen = map->m_la + 1 - vi->i_size;
524 map->m_la = vi->i_size;
529 err = z_erofs_fill_inode_lazy(vi);
533 lclusterbits = vi->z_logical_clusterbits;
535 initial_lcn = ofs >> lclusterbits;
536 endoff = ofs & ((1 << lclusterbits) - 1);
538 err = z_erofs_load_cluster_from_disk(&m, initial_lcn, false);
542 map->m_flags = EROFS_MAP_MAPPED | EROFS_MAP_ENCODED;
543 end = (m.lcn + 1ULL) << lclusterbits;
545 case Z_EROFS_VLE_CLUSTER_TYPE_PLAIN:
546 case Z_EROFS_VLE_CLUSTER_TYPE_HEAD:
547 if (endoff >= m.clusterofs) {
549 map->m_la = (m.lcn << lclusterbits) | m.clusterofs;
552 /* m.lcn should be >= 1 if endoff < m.clusterofs */
554 erofs_err("invalid logical cluster 0 at nid %llu",
555 (unsigned long long)vi->nid);
559 end = (m.lcn << lclusterbits) | m.clusterofs;
560 map->m_flags |= EROFS_MAP_FULL_MAPPED;
563 case Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD:
564 /* get the correspoinding first chunk */
565 err = z_erofs_extent_lookback(&m, m.delta[0]);
570 erofs_err("unknown type %u @ offset %llu of nid %llu",
571 m.type, ofs, (unsigned long long)vi->nid);
576 map->m_llen = end - map->m_la;
577 map->m_pa = blknr_to_addr(m.pblk);
579 err = z_erofs_get_extent_compressedlen(&m, initial_lcn);
583 if (m.headtype == Z_EROFS_VLE_CLUSTER_TYPE_PLAIN)
584 map->m_algorithmformat = Z_EROFS_COMPRESSION_SHIFTED;
586 map->m_algorithmformat = vi->z_algorithmtype[0];
588 if (flags & EROFS_GET_BLOCKS_FIEMAP) {
589 err = z_erofs_get_extent_decompressedlen(&m);
591 map->m_flags |= EROFS_MAP_FULL_MAPPED;
595 erofs_dbg("m_la %" PRIu64 " m_pa %" PRIu64 " m_llen %" PRIu64 " m_plen %" PRIu64 " m_flags 0%o",
596 map->m_la, map->m_pa,
597 map->m_llen, map->m_plen, map->m_flags);
599 DBG_BUGON(err < 0 && err != -ENOMEM);