1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2018-2019 HUAWEI, Inc.
4 * https://www.huawei.com/
7 #include <asm/unaligned.h>
8 #include <trace/events/erofs.h>
10 int z_erofs_fill_inode(struct inode *inode)
12 struct erofs_inode *const vi = EROFS_I(inode);
13 struct erofs_sb_info *sbi = EROFS_SB(inode->i_sb);
15 if (!erofs_sb_has_big_pcluster(sbi) &&
16 vi->datalayout == EROFS_INODE_FLAT_COMPRESSION_LEGACY) {
18 vi->z_algorithmtype[0] = 0;
19 vi->z_algorithmtype[1] = 0;
20 vi->z_logical_clusterbits = LOG_BLOCK_SIZE;
21 set_bit(EROFS_I_Z_INITED_BIT, &vi->flags);
23 inode->i_mapping->a_ops = &z_erofs_aops;
27 static int z_erofs_fill_inode_lazy(struct inode *inode)
29 struct erofs_inode *const vi = EROFS_I(inode);
30 struct super_block *const sb = inode->i_sb;
35 struct z_erofs_map_header *h;
37 if (test_bit(EROFS_I_Z_INITED_BIT, &vi->flags)) {
39 * paired with smp_mb() at the end of the function to ensure
40 * fields will only be observed after the bit is set.
46 if (wait_on_bit_lock(&vi->flags, EROFS_I_BL_Z_BIT, TASK_KILLABLE))
50 if (test_bit(EROFS_I_Z_INITED_BIT, &vi->flags))
53 DBG_BUGON(!erofs_sb_has_big_pcluster(EROFS_SB(sb)) &&
54 vi->datalayout == EROFS_INODE_FLAT_COMPRESSION_LEGACY);
56 pos = ALIGN(iloc(EROFS_SB(sb), vi->nid) + vi->inode_isize +
58 page = erofs_get_meta_page(sb, erofs_blknr(pos));
64 kaddr = kmap_atomic(page);
66 h = kaddr + erofs_blkoff(pos);
67 vi->z_advise = le16_to_cpu(h->h_advise);
68 vi->z_algorithmtype[0] = h->h_algorithmtype & 15;
69 vi->z_algorithmtype[1] = h->h_algorithmtype >> 4;
72 if (vi->z_algorithmtype[0] >= Z_EROFS_COMPRESSION_MAX ||
73 vi->z_algorithmtype[++headnr] >= Z_EROFS_COMPRESSION_MAX) {
74 erofs_err(sb, "unknown HEAD%u format %u for nid %llu, please upgrade kernel",
75 headnr + 1, vi->z_algorithmtype[headnr], vi->nid);
80 vi->z_logical_clusterbits = LOG_BLOCK_SIZE + (h->h_clusterbits & 7);
81 if (!erofs_sb_has_big_pcluster(EROFS_SB(sb)) &&
82 vi->z_advise & (Z_EROFS_ADVISE_BIG_PCLUSTER_1 |
83 Z_EROFS_ADVISE_BIG_PCLUSTER_2)) {
84 erofs_err(sb, "per-inode big pcluster without sb feature for nid %llu",
89 if (vi->datalayout == EROFS_INODE_FLAT_COMPRESSION &&
90 !(vi->z_advise & Z_EROFS_ADVISE_BIG_PCLUSTER_1) ^
91 !(vi->z_advise & Z_EROFS_ADVISE_BIG_PCLUSTER_2)) {
92 erofs_err(sb, "big pcluster head1/2 of compact indexes should be consistent for nid %llu",
97 /* paired with smp_mb() at the beginning of the function */
99 set_bit(EROFS_I_Z_INITED_BIT, &vi->flags);
101 kunmap_atomic(kaddr);
105 clear_and_wake_up_bit(EROFS_I_BL_Z_BIT, &vi->flags);
109 struct z_erofs_maprecorder {
111 struct erofs_map_blocks *map;
115 /* compression extent information gathered */
119 erofs_blk_t pblk, compressedlcs;
122 static int z_erofs_reload_indexes(struct z_erofs_maprecorder *m,
125 struct super_block *const sb = m->inode->i_sb;
126 struct erofs_map_blocks *const map = m->map;
127 struct page *mpage = map->mpage;
130 if (mpage->index == eblk) {
132 m->kaddr = kmap_atomic(mpage);
137 kunmap_atomic(m->kaddr);
143 mpage = erofs_get_meta_page(sb, eblk);
146 return PTR_ERR(mpage);
148 m->kaddr = kmap_atomic(mpage);
154 static int legacy_load_cluster_from_disk(struct z_erofs_maprecorder *m,
157 struct inode *const inode = m->inode;
158 struct erofs_inode *const vi = EROFS_I(inode);
159 const erofs_off_t ibase = iloc(EROFS_I_SB(inode), vi->nid);
160 const erofs_off_t pos =
161 Z_EROFS_VLE_LEGACY_INDEX_ALIGN(ibase + vi->inode_isize +
163 lcn * sizeof(struct z_erofs_vle_decompressed_index);
164 struct z_erofs_vle_decompressed_index *di;
165 unsigned int advise, type;
168 err = z_erofs_reload_indexes(m, erofs_blknr(pos));
173 di = m->kaddr + erofs_blkoff(pos);
175 advise = le16_to_cpu(di->di_advise);
176 type = (advise >> Z_EROFS_VLE_DI_CLUSTER_TYPE_BIT) &
177 ((1 << Z_EROFS_VLE_DI_CLUSTER_TYPE_BITS) - 1);
179 case Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD:
180 m->clusterofs = 1 << vi->z_logical_clusterbits;
181 m->delta[0] = le16_to_cpu(di->di_u.delta[0]);
182 if (m->delta[0] & Z_EROFS_VLE_DI_D0_CBLKCNT) {
183 if (!(vi->z_advise & (Z_EROFS_ADVISE_BIG_PCLUSTER_1 |
184 Z_EROFS_ADVISE_BIG_PCLUSTER_2))) {
186 return -EFSCORRUPTED;
188 m->compressedlcs = m->delta[0] &
189 ~Z_EROFS_VLE_DI_D0_CBLKCNT;
192 m->delta[1] = le16_to_cpu(di->di_u.delta[1]);
194 case Z_EROFS_VLE_CLUSTER_TYPE_PLAIN:
195 case Z_EROFS_VLE_CLUSTER_TYPE_HEAD1:
196 case Z_EROFS_VLE_CLUSTER_TYPE_HEAD2:
197 m->clusterofs = le16_to_cpu(di->di_clusterofs);
198 m->pblk = le32_to_cpu(di->di_u.blkaddr);
208 static unsigned int decode_compactedbits(unsigned int lobits,
210 u8 *in, unsigned int pos, u8 *type)
212 const unsigned int v = get_unaligned_le32(in + pos / 8) >> (pos & 7);
213 const unsigned int lo = v & lomask;
215 *type = (v >> lobits) & 3;
219 static int get_compacted_la_distance(unsigned int lclusterbits,
220 unsigned int encodebits,
221 unsigned int vcnt, u8 *in, int i)
223 const unsigned int lomask = (1 << lclusterbits) - 1;
224 unsigned int lo, d1 = 0;
227 DBG_BUGON(i >= vcnt);
230 lo = decode_compactedbits(lclusterbits, lomask,
231 in, encodebits * i, &type);
233 if (type != Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD)
236 } while (++i < vcnt);
238 /* vcnt - 1 (Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD) item */
239 if (!(lo & Z_EROFS_VLE_DI_D0_CBLKCNT))
244 static int unpack_compacted_index(struct z_erofs_maprecorder *m,
245 unsigned int amortizedshift,
246 unsigned int eofs, bool lookahead)
248 struct erofs_inode *const vi = EROFS_I(m->inode);
249 const unsigned int lclusterbits = vi->z_logical_clusterbits;
250 const unsigned int lomask = (1 << lclusterbits) - 1;
251 unsigned int vcnt, base, lo, encodebits, nblk;
256 if (1 << amortizedshift == 4)
258 else if (1 << amortizedshift == 2 && lclusterbits == 12)
263 big_pcluster = vi->z_advise & Z_EROFS_ADVISE_BIG_PCLUSTER_1;
264 encodebits = ((vcnt << amortizedshift) - sizeof(__le32)) * 8 / vcnt;
265 base = round_down(eofs, vcnt << amortizedshift);
266 in = m->kaddr + base;
268 i = (eofs - base) >> amortizedshift;
270 lo = decode_compactedbits(lclusterbits, lomask,
271 in, encodebits * i, &type);
273 if (type == Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD) {
274 m->clusterofs = 1 << lclusterbits;
276 /* figure out lookahead_distance: delta[1] if needed */
278 m->delta[1] = get_compacted_la_distance(lclusterbits,
279 encodebits, vcnt, in, i);
280 if (lo & Z_EROFS_VLE_DI_D0_CBLKCNT) {
283 return -EFSCORRUPTED;
285 m->compressedlcs = lo & ~Z_EROFS_VLE_DI_D0_CBLKCNT;
288 } else if (i + 1 != (int)vcnt) {
293 * since the last lcluster in the pack is special,
294 * of which lo saves delta[1] rather than delta[0].
295 * Hence, get delta[0] by the previous lcluster indirectly.
297 lo = decode_compactedbits(lclusterbits, lomask,
298 in, encodebits * (i - 1), &type);
299 if (type != Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD)
301 else if (lo & Z_EROFS_VLE_DI_D0_CBLKCNT)
303 m->delta[0] = lo + 1;
308 /* figout out blkaddr (pblk) for HEAD lclusters */
313 lo = decode_compactedbits(lclusterbits, lomask,
314 in, encodebits * i, &type);
315 if (type == Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD)
325 lo = decode_compactedbits(lclusterbits, lomask,
326 in, encodebits * i, &type);
327 if (type == Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD) {
328 if (lo & Z_EROFS_VLE_DI_D0_CBLKCNT) {
330 nblk += lo & ~Z_EROFS_VLE_DI_D0_CBLKCNT;
333 /* bigpcluster shouldn't have plain d0 == 1 */
336 return -EFSCORRUPTED;
344 in += (vcnt << amortizedshift) - sizeof(__le32);
345 m->pblk = le32_to_cpu(*(__le32 *)in) + nblk;
349 static int compacted_load_cluster_from_disk(struct z_erofs_maprecorder *m,
350 unsigned long lcn, bool lookahead)
352 struct inode *const inode = m->inode;
353 struct erofs_inode *const vi = EROFS_I(inode);
354 const unsigned int lclusterbits = vi->z_logical_clusterbits;
355 const erofs_off_t ebase = ALIGN(iloc(EROFS_I_SB(inode), vi->nid) +
356 vi->inode_isize + vi->xattr_isize, 8) +
357 sizeof(struct z_erofs_map_header);
358 const unsigned int totalidx = DIV_ROUND_UP(inode->i_size, EROFS_BLKSIZ);
359 unsigned int compacted_4b_initial, compacted_2b;
360 unsigned int amortizedshift;
364 if (lclusterbits != 12)
371 /* used to align to 32-byte (compacted_2b) alignment */
372 compacted_4b_initial = (32 - ebase % 32) / 4;
373 if (compacted_4b_initial == 32 / 4)
374 compacted_4b_initial = 0;
376 if ((vi->z_advise & Z_EROFS_ADVISE_COMPACTED_2B) &&
377 compacted_4b_initial < totalidx)
378 compacted_2b = rounddown(totalidx - compacted_4b_initial, 16);
383 if (lcn < compacted_4b_initial) {
387 pos += compacted_4b_initial * 4;
388 lcn -= compacted_4b_initial;
390 if (lcn < compacted_2b) {
394 pos += compacted_2b * 2;
398 pos += lcn * (1 << amortizedshift);
399 err = z_erofs_reload_indexes(m, erofs_blknr(pos));
402 return unpack_compacted_index(m, amortizedshift, erofs_blkoff(pos),
406 static int z_erofs_load_cluster_from_disk(struct z_erofs_maprecorder *m,
407 unsigned int lcn, bool lookahead)
409 const unsigned int datamode = EROFS_I(m->inode)->datalayout;
411 if (datamode == EROFS_INODE_FLAT_COMPRESSION_LEGACY)
412 return legacy_load_cluster_from_disk(m, lcn);
414 if (datamode == EROFS_INODE_FLAT_COMPRESSION)
415 return compacted_load_cluster_from_disk(m, lcn, lookahead);
420 static int z_erofs_extent_lookback(struct z_erofs_maprecorder *m,
421 unsigned int lookback_distance)
423 struct erofs_inode *const vi = EROFS_I(m->inode);
424 struct erofs_map_blocks *const map = m->map;
425 const unsigned int lclusterbits = vi->z_logical_clusterbits;
426 unsigned long lcn = m->lcn;
429 if (lcn < lookback_distance) {
430 erofs_err(m->inode->i_sb,
431 "bogus lookback distance @ nid %llu", vi->nid);
433 return -EFSCORRUPTED;
436 /* load extent head logical cluster if needed */
437 lcn -= lookback_distance;
438 err = z_erofs_load_cluster_from_disk(m, lcn, false);
443 case Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD:
445 erofs_err(m->inode->i_sb,
446 "invalid lookback distance 0 @ nid %llu",
449 return -EFSCORRUPTED;
451 return z_erofs_extent_lookback(m, m->delta[0]);
452 case Z_EROFS_VLE_CLUSTER_TYPE_PLAIN:
453 case Z_EROFS_VLE_CLUSTER_TYPE_HEAD1:
454 case Z_EROFS_VLE_CLUSTER_TYPE_HEAD2:
455 m->headtype = m->type;
456 map->m_la = (lcn << lclusterbits) | m->clusterofs;
459 erofs_err(m->inode->i_sb,
460 "unknown type %u @ lcn %lu of nid %llu",
461 m->type, lcn, vi->nid);
468 static int z_erofs_get_extent_compressedlen(struct z_erofs_maprecorder *m,
469 unsigned int initial_lcn)
471 struct erofs_inode *const vi = EROFS_I(m->inode);
472 struct erofs_map_blocks *const map = m->map;
473 const unsigned int lclusterbits = vi->z_logical_clusterbits;
477 DBG_BUGON(m->type != Z_EROFS_VLE_CLUSTER_TYPE_PLAIN &&
478 m->type != Z_EROFS_VLE_CLUSTER_TYPE_HEAD1 &&
479 m->type != Z_EROFS_VLE_CLUSTER_TYPE_HEAD2);
480 DBG_BUGON(m->type != m->headtype);
482 if (m->headtype == Z_EROFS_VLE_CLUSTER_TYPE_PLAIN ||
483 ((m->headtype == Z_EROFS_VLE_CLUSTER_TYPE_HEAD1) &&
484 !(vi->z_advise & Z_EROFS_ADVISE_BIG_PCLUSTER_1)) ||
485 ((m->headtype == Z_EROFS_VLE_CLUSTER_TYPE_HEAD2) &&
486 !(vi->z_advise & Z_EROFS_ADVISE_BIG_PCLUSTER_2))) {
487 map->m_plen = 1 << lclusterbits;
491 if (m->compressedlcs)
494 err = z_erofs_load_cluster_from_disk(m, lcn, false);
499 * If the 1st NONHEAD lcluster has already been handled initially w/o
500 * valid compressedlcs, which means at least it mustn't be CBLKCNT, or
501 * an internal implemenatation error is detected.
503 * The following code can also handle it properly anyway, but let's
504 * BUG_ON in the debugging mode only for developers to notice that.
506 DBG_BUGON(lcn == initial_lcn &&
507 m->type == Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD);
510 case Z_EROFS_VLE_CLUSTER_TYPE_PLAIN:
511 case Z_EROFS_VLE_CLUSTER_TYPE_HEAD1:
512 case Z_EROFS_VLE_CLUSTER_TYPE_HEAD2:
514 * if the 1st NONHEAD lcluster is actually PLAIN or HEAD type
515 * rather than CBLKCNT, it's a 1 lcluster-sized pcluster.
517 m->compressedlcs = 1;
519 case Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD:
520 if (m->delta[0] != 1)
521 goto err_bonus_cblkcnt;
522 if (m->compressedlcs)
526 erofs_err(m->inode->i_sb,
527 "cannot found CBLKCNT @ lcn %lu of nid %llu",
530 return -EFSCORRUPTED;
533 map->m_plen = m->compressedlcs << lclusterbits;
536 erofs_err(m->inode->i_sb,
537 "bogus CBLKCNT @ lcn %lu of nid %llu",
540 return -EFSCORRUPTED;
543 static int z_erofs_get_extent_decompressedlen(struct z_erofs_maprecorder *m)
545 struct inode *inode = m->inode;
546 struct erofs_inode *vi = EROFS_I(inode);
547 struct erofs_map_blocks *map = m->map;
548 unsigned int lclusterbits = vi->z_logical_clusterbits;
549 u64 lcn = m->lcn, headlcn = map->m_la >> lclusterbits;
553 /* handle the last EOF pcluster (no next HEAD lcluster) */
554 if ((lcn << lclusterbits) >= inode->i_size) {
555 map->m_llen = inode->i_size - map->m_la;
559 err = z_erofs_load_cluster_from_disk(m, lcn, true);
563 if (m->type == Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD) {
564 DBG_BUGON(!m->delta[1] &&
565 m->clusterofs != 1 << lclusterbits);
566 } else if (m->type == Z_EROFS_VLE_CLUSTER_TYPE_PLAIN ||
567 m->type == Z_EROFS_VLE_CLUSTER_TYPE_HEAD1 ||
568 m->type == Z_EROFS_VLE_CLUSTER_TYPE_HEAD2) {
569 /* go on until the next HEAD lcluster */
574 erofs_err(inode->i_sb, "unknown type %u @ lcn %llu of nid %llu",
575 m->type, lcn, vi->nid);
580 } while (m->delta[1]);
582 map->m_llen = (lcn << lclusterbits) + m->clusterofs - map->m_la;
586 int z_erofs_map_blocks_iter(struct inode *inode,
587 struct erofs_map_blocks *map,
590 struct erofs_inode *const vi = EROFS_I(inode);
591 struct z_erofs_maprecorder m = {
596 unsigned int lclusterbits, endoff;
597 unsigned long initial_lcn;
598 unsigned long long ofs, end;
600 trace_z_erofs_map_blocks_iter_enter(inode, map, flags);
602 /* when trying to read beyond EOF, leave it unmapped */
603 if (map->m_la >= inode->i_size) {
604 map->m_llen = map->m_la + 1 - inode->i_size;
605 map->m_la = inode->i_size;
610 err = z_erofs_fill_inode_lazy(inode);
614 lclusterbits = vi->z_logical_clusterbits;
616 initial_lcn = ofs >> lclusterbits;
617 endoff = ofs & ((1 << lclusterbits) - 1);
619 err = z_erofs_load_cluster_from_disk(&m, initial_lcn, false);
623 map->m_flags = EROFS_MAP_MAPPED | EROFS_MAP_ENCODED;
624 end = (m.lcn + 1ULL) << lclusterbits;
627 case Z_EROFS_VLE_CLUSTER_TYPE_PLAIN:
628 case Z_EROFS_VLE_CLUSTER_TYPE_HEAD1:
629 case Z_EROFS_VLE_CLUSTER_TYPE_HEAD2:
630 if (endoff >= m.clusterofs) {
632 map->m_la = (m.lcn << lclusterbits) | m.clusterofs;
635 /* m.lcn should be >= 1 if endoff < m.clusterofs */
637 erofs_err(inode->i_sb,
638 "invalid logical cluster 0 at nid %llu",
643 end = (m.lcn << lclusterbits) | m.clusterofs;
644 map->m_flags |= EROFS_MAP_FULL_MAPPED;
647 case Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD:
648 /* get the corresponding first chunk */
649 err = z_erofs_extent_lookback(&m, m.delta[0]);
654 erofs_err(inode->i_sb,
655 "unknown type %u @ offset %llu of nid %llu",
656 m.type, ofs, vi->nid);
661 map->m_llen = end - map->m_la;
662 map->m_pa = blknr_to_addr(m.pblk);
664 err = z_erofs_get_extent_compressedlen(&m, initial_lcn);
668 if (m.headtype == Z_EROFS_VLE_CLUSTER_TYPE_PLAIN)
669 map->m_algorithmformat = Z_EROFS_COMPRESSION_SHIFTED;
670 else if (m.headtype == Z_EROFS_VLE_CLUSTER_TYPE_HEAD2)
671 map->m_algorithmformat = vi->z_algorithmtype[1];
673 map->m_algorithmformat = vi->z_algorithmtype[0];
675 if ((flags & EROFS_GET_BLOCKS_FIEMAP) ||
676 ((flags & EROFS_GET_BLOCKS_READMORE) &&
677 map->m_algorithmformat == Z_EROFS_COMPRESSION_LZMA &&
678 map->m_llen >= EROFS_BLKSIZ)) {
679 err = z_erofs_get_extent_decompressedlen(&m);
681 map->m_flags |= EROFS_MAP_FULL_MAPPED;
685 kunmap_atomic(m.kaddr);
688 erofs_dbg("%s, m_la %llu m_pa %llu m_llen %llu m_plen %llu m_flags 0%o",
689 __func__, map->m_la, map->m_pa,
690 map->m_llen, map->m_plen, map->m_flags);
692 trace_z_erofs_map_blocks_iter_exit(inode, map, flags, err);
694 /* aggressively BUG_ON iff CONFIG_EROFS_FS_DEBUG is on */
695 DBG_BUGON(err < 0 && err != -ENOMEM);
699 static int z_erofs_iomap_begin_report(struct inode *inode, loff_t offset,
700 loff_t length, unsigned int flags,
701 struct iomap *iomap, struct iomap *srcmap)
704 struct erofs_map_blocks map = { .m_la = offset };
706 ret = z_erofs_map_blocks_iter(inode, &map, EROFS_GET_BLOCKS_FIEMAP);
712 iomap->bdev = inode->i_sb->s_bdev;
713 iomap->offset = map.m_la;
714 iomap->length = map.m_llen;
715 if (map.m_flags & EROFS_MAP_MAPPED) {
716 iomap->type = IOMAP_MAPPED;
717 iomap->addr = map.m_pa;
719 iomap->type = IOMAP_HOLE;
720 iomap->addr = IOMAP_NULL_ADDR;
722 * No strict rule how to describe extents for post EOF, yet
723 * we need do like below. Otherwise, iomap itself will get
724 * into an endless loop on post EOF.
726 if (iomap->offset >= inode->i_size)
727 iomap->length = length + map.m_la - offset;
733 const struct iomap_ops z_erofs_iomap_report_ops = {
734 .iomap_begin = z_erofs_iomap_begin_report,