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 struct z_erofs_maprecorder {
12 struct erofs_map_blocks *map;
16 /* compression extent information gathered */
20 erofs_blk_t pblk, compressedblks;
21 erofs_off_t nextpackoff;
25 static int legacy_load_cluster_from_disk(struct z_erofs_maprecorder *m,
28 struct inode *const inode = m->inode;
29 struct erofs_inode *const vi = EROFS_I(inode);
30 const erofs_off_t pos = Z_EROFS_FULL_INDEX_ALIGN(erofs_iloc(inode) +
31 vi->inode_isize + vi->xattr_isize) +
32 lcn * sizeof(struct z_erofs_lcluster_index);
33 struct z_erofs_lcluster_index *di;
34 unsigned int advise, type;
36 m->kaddr = erofs_read_metabuf(&m->map->buf, inode->i_sb,
37 erofs_blknr(inode->i_sb, pos), EROFS_KMAP);
39 return PTR_ERR(m->kaddr);
41 m->nextpackoff = pos + sizeof(struct z_erofs_lcluster_index);
43 di = m->kaddr + erofs_blkoff(inode->i_sb, pos);
45 advise = le16_to_cpu(di->di_advise);
46 type = (advise >> Z_EROFS_LI_LCLUSTER_TYPE_BIT) &
47 ((1 << Z_EROFS_LI_LCLUSTER_TYPE_BITS) - 1);
49 case Z_EROFS_LCLUSTER_TYPE_NONHEAD:
50 m->clusterofs = 1 << vi->z_logical_clusterbits;
51 m->delta[0] = le16_to_cpu(di->di_u.delta[0]);
52 if (m->delta[0] & Z_EROFS_LI_D0_CBLKCNT) {
53 if (!(vi->z_advise & (Z_EROFS_ADVISE_BIG_PCLUSTER_1 |
54 Z_EROFS_ADVISE_BIG_PCLUSTER_2))) {
58 m->compressedblks = m->delta[0] &
59 ~Z_EROFS_LI_D0_CBLKCNT;
62 m->delta[1] = le16_to_cpu(di->di_u.delta[1]);
64 case Z_EROFS_LCLUSTER_TYPE_PLAIN:
65 case Z_EROFS_LCLUSTER_TYPE_HEAD1:
66 case Z_EROFS_LCLUSTER_TYPE_HEAD2:
67 if (advise & Z_EROFS_LI_PARTIAL_REF)
69 m->clusterofs = le16_to_cpu(di->di_clusterofs);
70 if (m->clusterofs >= 1 << vi->z_logical_clusterbits) {
74 m->pblk = le32_to_cpu(di->di_u.blkaddr);
84 static unsigned int decode_compactedbits(unsigned int lobits,
86 u8 *in, unsigned int pos, u8 *type)
88 const unsigned int v = get_unaligned_le32(in + pos / 8) >> (pos & 7);
89 const unsigned int lo = v & lomask;
91 *type = (v >> lobits) & 3;
95 static int get_compacted_la_distance(unsigned int lclusterbits,
96 unsigned int encodebits,
97 unsigned int vcnt, u8 *in, int i)
99 const unsigned int lomask = (1 << lclusterbits) - 1;
100 unsigned int lo, d1 = 0;
103 DBG_BUGON(i >= vcnt);
106 lo = decode_compactedbits(lclusterbits, lomask,
107 in, encodebits * i, &type);
109 if (type != Z_EROFS_LCLUSTER_TYPE_NONHEAD)
112 } while (++i < vcnt);
114 /* vcnt - 1 (Z_EROFS_LCLUSTER_TYPE_NONHEAD) item */
115 if (!(lo & Z_EROFS_LI_D0_CBLKCNT))
120 static int unpack_compacted_index(struct z_erofs_maprecorder *m,
121 unsigned int amortizedshift,
122 erofs_off_t pos, bool lookahead)
124 struct erofs_inode *const vi = EROFS_I(m->inode);
125 const unsigned int lclusterbits = vi->z_logical_clusterbits;
126 const unsigned int lomask = (1 << lclusterbits) - 1;
127 unsigned int vcnt, base, lo, encodebits, nblk, eofs;
132 if (1 << amortizedshift == 4)
134 else if (1 << amortizedshift == 2 && lclusterbits == 12)
139 /* it doesn't equal to round_up(..) */
140 m->nextpackoff = round_down(pos, vcnt << amortizedshift) +
141 (vcnt << amortizedshift);
142 big_pcluster = vi->z_advise & Z_EROFS_ADVISE_BIG_PCLUSTER_1;
143 encodebits = ((vcnt << amortizedshift) - sizeof(__le32)) * 8 / vcnt;
144 eofs = erofs_blkoff(m->inode->i_sb, pos);
145 base = round_down(eofs, vcnt << amortizedshift);
146 in = m->kaddr + base;
148 i = (eofs - base) >> amortizedshift;
150 lo = decode_compactedbits(lclusterbits, lomask,
151 in, encodebits * i, &type);
153 if (type == Z_EROFS_LCLUSTER_TYPE_NONHEAD) {
154 m->clusterofs = 1 << lclusterbits;
156 /* figure out lookahead_distance: delta[1] if needed */
158 m->delta[1] = get_compacted_la_distance(lclusterbits,
159 encodebits, vcnt, in, i);
160 if (lo & Z_EROFS_LI_D0_CBLKCNT) {
163 return -EFSCORRUPTED;
165 m->compressedblks = lo & ~Z_EROFS_LI_D0_CBLKCNT;
168 } else if (i + 1 != (int)vcnt) {
173 * since the last lcluster in the pack is special,
174 * of which lo saves delta[1] rather than delta[0].
175 * Hence, get delta[0] by the previous lcluster indirectly.
177 lo = decode_compactedbits(lclusterbits, lomask,
178 in, encodebits * (i - 1), &type);
179 if (type != Z_EROFS_LCLUSTER_TYPE_NONHEAD)
181 else if (lo & Z_EROFS_LI_D0_CBLKCNT)
183 m->delta[0] = lo + 1;
188 /* figout out blkaddr (pblk) for HEAD lclusters */
193 lo = decode_compactedbits(lclusterbits, lomask,
194 in, encodebits * i, &type);
195 if (type == Z_EROFS_LCLUSTER_TYPE_NONHEAD)
205 lo = decode_compactedbits(lclusterbits, lomask,
206 in, encodebits * i, &type);
207 if (type == Z_EROFS_LCLUSTER_TYPE_NONHEAD) {
208 if (lo & Z_EROFS_LI_D0_CBLKCNT) {
210 nblk += lo & ~Z_EROFS_LI_D0_CBLKCNT;
213 /* bigpcluster shouldn't have plain d0 == 1 */
216 return -EFSCORRUPTED;
224 in += (vcnt << amortizedshift) - sizeof(__le32);
225 m->pblk = le32_to_cpu(*(__le32 *)in) + nblk;
229 static int compacted_load_cluster_from_disk(struct z_erofs_maprecorder *m,
230 unsigned long lcn, bool lookahead)
232 struct inode *const inode = m->inode;
233 struct erofs_inode *const vi = EROFS_I(inode);
234 const unsigned int lclusterbits = vi->z_logical_clusterbits;
235 const erofs_off_t ebase = sizeof(struct z_erofs_map_header) +
236 ALIGN(erofs_iloc(inode) + vi->inode_isize + vi->xattr_isize, 8);
237 unsigned int totalidx = erofs_iblks(inode);
238 unsigned int compacted_4b_initial, compacted_2b;
239 unsigned int amortizedshift;
242 if (lclusterbits != 12)
249 /* used to align to 32-byte (compacted_2b) alignment */
250 compacted_4b_initial = (32 - ebase % 32) / 4;
251 if (compacted_4b_initial == 32 / 4)
252 compacted_4b_initial = 0;
254 if ((vi->z_advise & Z_EROFS_ADVISE_COMPACTED_2B) &&
255 compacted_4b_initial < totalidx)
256 compacted_2b = rounddown(totalidx - compacted_4b_initial, 16);
261 if (lcn < compacted_4b_initial) {
265 pos += compacted_4b_initial * 4;
266 lcn -= compacted_4b_initial;
268 if (lcn < compacted_2b) {
272 pos += compacted_2b * 2;
276 pos += lcn * (1 << amortizedshift);
277 m->kaddr = erofs_read_metabuf(&m->map->buf, inode->i_sb,
278 erofs_blknr(inode->i_sb, pos), EROFS_KMAP);
279 if (IS_ERR(m->kaddr))
280 return PTR_ERR(m->kaddr);
281 return unpack_compacted_index(m, amortizedshift, pos, lookahead);
284 static int z_erofs_load_cluster_from_disk(struct z_erofs_maprecorder *m,
285 unsigned int lcn, bool lookahead)
287 const unsigned int datamode = EROFS_I(m->inode)->datalayout;
289 if (datamode == EROFS_INODE_COMPRESSED_FULL)
290 return legacy_load_cluster_from_disk(m, lcn);
292 if (datamode == EROFS_INODE_COMPRESSED_COMPACT)
293 return compacted_load_cluster_from_disk(m, lcn, lookahead);
298 static int z_erofs_extent_lookback(struct z_erofs_maprecorder *m,
299 unsigned int lookback_distance)
301 struct erofs_inode *const vi = EROFS_I(m->inode);
302 const unsigned int lclusterbits = vi->z_logical_clusterbits;
304 while (m->lcn >= lookback_distance) {
305 unsigned long lcn = m->lcn - lookback_distance;
308 /* load extent head logical cluster if needed */
309 err = z_erofs_load_cluster_from_disk(m, lcn, false);
314 case Z_EROFS_LCLUSTER_TYPE_NONHEAD:
316 erofs_err(m->inode->i_sb,
317 "invalid lookback distance 0 @ nid %llu",
320 return -EFSCORRUPTED;
322 lookback_distance = m->delta[0];
324 case Z_EROFS_LCLUSTER_TYPE_PLAIN:
325 case Z_EROFS_LCLUSTER_TYPE_HEAD1:
326 case Z_EROFS_LCLUSTER_TYPE_HEAD2:
327 m->headtype = m->type;
328 m->map->m_la = (lcn << lclusterbits) | m->clusterofs;
331 erofs_err(m->inode->i_sb,
332 "unknown type %u @ lcn %lu of nid %llu",
333 m->type, lcn, vi->nid);
339 erofs_err(m->inode->i_sb, "bogus lookback distance @ nid %llu",
342 return -EFSCORRUPTED;
345 static int z_erofs_get_extent_compressedlen(struct z_erofs_maprecorder *m,
346 unsigned int initial_lcn)
348 struct super_block *sb = m->inode->i_sb;
349 struct erofs_inode *const vi = EROFS_I(m->inode);
350 struct erofs_map_blocks *const map = m->map;
351 const unsigned int lclusterbits = vi->z_logical_clusterbits;
355 DBG_BUGON(m->type != Z_EROFS_LCLUSTER_TYPE_PLAIN &&
356 m->type != Z_EROFS_LCLUSTER_TYPE_HEAD1 &&
357 m->type != Z_EROFS_LCLUSTER_TYPE_HEAD2);
358 DBG_BUGON(m->type != m->headtype);
360 if (m->headtype == Z_EROFS_LCLUSTER_TYPE_PLAIN ||
361 ((m->headtype == Z_EROFS_LCLUSTER_TYPE_HEAD1) &&
362 !(vi->z_advise & Z_EROFS_ADVISE_BIG_PCLUSTER_1)) ||
363 ((m->headtype == Z_EROFS_LCLUSTER_TYPE_HEAD2) &&
364 !(vi->z_advise & Z_EROFS_ADVISE_BIG_PCLUSTER_2))) {
365 map->m_plen = 1ULL << lclusterbits;
369 if (m->compressedblks)
372 err = z_erofs_load_cluster_from_disk(m, lcn, false);
377 * If the 1st NONHEAD lcluster has already been handled initially w/o
378 * valid compressedblks, which means at least it mustn't be CBLKCNT, or
379 * an internal implemenatation error is detected.
381 * The following code can also handle it properly anyway, but let's
382 * BUG_ON in the debugging mode only for developers to notice that.
384 DBG_BUGON(lcn == initial_lcn &&
385 m->type == Z_EROFS_LCLUSTER_TYPE_NONHEAD);
388 case Z_EROFS_LCLUSTER_TYPE_PLAIN:
389 case Z_EROFS_LCLUSTER_TYPE_HEAD1:
390 case Z_EROFS_LCLUSTER_TYPE_HEAD2:
392 * if the 1st NONHEAD lcluster is actually PLAIN or HEAD type
393 * rather than CBLKCNT, it's a 1 lcluster-sized pcluster.
395 m->compressedblks = 1 << (lclusterbits - sb->s_blocksize_bits);
397 case Z_EROFS_LCLUSTER_TYPE_NONHEAD:
398 if (m->delta[0] != 1)
399 goto err_bonus_cblkcnt;
400 if (m->compressedblks)
404 erofs_err(m->inode->i_sb,
405 "cannot found CBLKCNT @ lcn %lu of nid %llu",
408 return -EFSCORRUPTED;
411 map->m_plen = erofs_pos(sb, m->compressedblks);
414 erofs_err(m->inode->i_sb,
415 "bogus CBLKCNT @ lcn %lu of nid %llu",
418 return -EFSCORRUPTED;
421 static int z_erofs_get_extent_decompressedlen(struct z_erofs_maprecorder *m)
423 struct inode *inode = m->inode;
424 struct erofs_inode *vi = EROFS_I(inode);
425 struct erofs_map_blocks *map = m->map;
426 unsigned int lclusterbits = vi->z_logical_clusterbits;
427 u64 lcn = m->lcn, headlcn = map->m_la >> lclusterbits;
431 /* handle the last EOF pcluster (no next HEAD lcluster) */
432 if ((lcn << lclusterbits) >= inode->i_size) {
433 map->m_llen = inode->i_size - map->m_la;
437 err = z_erofs_load_cluster_from_disk(m, lcn, true);
441 if (m->type == Z_EROFS_LCLUSTER_TYPE_NONHEAD) {
442 DBG_BUGON(!m->delta[1] &&
443 m->clusterofs != 1 << lclusterbits);
444 } else if (m->type == Z_EROFS_LCLUSTER_TYPE_PLAIN ||
445 m->type == Z_EROFS_LCLUSTER_TYPE_HEAD1 ||
446 m->type == Z_EROFS_LCLUSTER_TYPE_HEAD2) {
447 /* go on until the next HEAD lcluster */
452 erofs_err(inode->i_sb, "unknown type %u @ lcn %llu of nid %llu",
453 m->type, lcn, vi->nid);
458 } while (m->delta[1]);
460 map->m_llen = (lcn << lclusterbits) + m->clusterofs - map->m_la;
464 static int z_erofs_do_map_blocks(struct inode *inode,
465 struct erofs_map_blocks *map, int flags)
467 struct erofs_inode *const vi = EROFS_I(inode);
468 bool ztailpacking = vi->z_advise & Z_EROFS_ADVISE_INLINE_PCLUSTER;
469 bool fragment = vi->z_advise & Z_EROFS_ADVISE_FRAGMENT_PCLUSTER;
470 struct z_erofs_maprecorder m = {
475 unsigned int lclusterbits, endoff;
476 unsigned long initial_lcn;
477 unsigned long long ofs, end;
479 lclusterbits = vi->z_logical_clusterbits;
480 ofs = flags & EROFS_GET_BLOCKS_FINDTAIL ? inode->i_size - 1 : map->m_la;
481 initial_lcn = ofs >> lclusterbits;
482 endoff = ofs & ((1 << lclusterbits) - 1);
484 err = z_erofs_load_cluster_from_disk(&m, initial_lcn, false);
488 if (ztailpacking && (flags & EROFS_GET_BLOCKS_FINDTAIL))
489 vi->z_idataoff = m.nextpackoff;
491 map->m_flags = EROFS_MAP_MAPPED | EROFS_MAP_ENCODED;
492 end = (m.lcn + 1ULL) << lclusterbits;
495 case Z_EROFS_LCLUSTER_TYPE_PLAIN:
496 case Z_EROFS_LCLUSTER_TYPE_HEAD1:
497 case Z_EROFS_LCLUSTER_TYPE_HEAD2:
498 if (endoff >= m.clusterofs) {
500 map->m_la = (m.lcn << lclusterbits) | m.clusterofs;
502 * For ztailpacking files, in order to inline data more
503 * effectively, special EOF lclusters are now supported
504 * which can have three parts at most.
506 if (ztailpacking && end > inode->i_size)
510 /* m.lcn should be >= 1 if endoff < m.clusterofs */
512 erofs_err(inode->i_sb,
513 "invalid logical cluster 0 at nid %llu",
518 end = (m.lcn << lclusterbits) | m.clusterofs;
519 map->m_flags |= EROFS_MAP_FULL_MAPPED;
522 case Z_EROFS_LCLUSTER_TYPE_NONHEAD:
523 /* get the corresponding first chunk */
524 err = z_erofs_extent_lookback(&m, m.delta[0]);
529 erofs_err(inode->i_sb,
530 "unknown type %u @ offset %llu of nid %llu",
531 m.type, ofs, vi->nid);
536 map->m_flags |= EROFS_MAP_PARTIAL_REF;
537 map->m_llen = end - map->m_la;
539 if (flags & EROFS_GET_BLOCKS_FINDTAIL) {
540 vi->z_tailextent_headlcn = m.lcn;
541 /* for non-compact indexes, fragmentoff is 64 bits */
543 vi->datalayout == EROFS_INODE_COMPRESSED_FULL)
544 vi->z_fragmentoff |= (u64)m.pblk << 32;
546 if (ztailpacking && m.lcn == vi->z_tailextent_headlcn) {
547 map->m_flags |= EROFS_MAP_META;
548 map->m_pa = vi->z_idataoff;
549 map->m_plen = vi->z_idata_size;
550 } else if (fragment && m.lcn == vi->z_tailextent_headlcn) {
551 map->m_flags |= EROFS_MAP_FRAGMENT;
553 map->m_pa = erofs_pos(inode->i_sb, m.pblk);
554 err = z_erofs_get_extent_compressedlen(&m, initial_lcn);
559 if (m.headtype == Z_EROFS_LCLUSTER_TYPE_PLAIN) {
560 if (map->m_llen > map->m_plen) {
565 if (vi->z_advise & Z_EROFS_ADVISE_INTERLACED_PCLUSTER)
566 map->m_algorithmformat =
567 Z_EROFS_COMPRESSION_INTERLACED;
569 map->m_algorithmformat =
570 Z_EROFS_COMPRESSION_SHIFTED;
571 } else if (m.headtype == Z_EROFS_LCLUSTER_TYPE_HEAD2) {
572 map->m_algorithmformat = vi->z_algorithmtype[1];
574 map->m_algorithmformat = vi->z_algorithmtype[0];
577 if ((flags & EROFS_GET_BLOCKS_FIEMAP) ||
578 ((flags & EROFS_GET_BLOCKS_READMORE) &&
579 map->m_algorithmformat == Z_EROFS_COMPRESSION_LZMA &&
580 map->m_llen >= i_blocksize(inode))) {
581 err = z_erofs_get_extent_decompressedlen(&m);
583 map->m_flags |= EROFS_MAP_FULL_MAPPED;
587 erofs_unmap_metabuf(&m.map->buf);
591 static int z_erofs_fill_inode_lazy(struct inode *inode)
593 struct erofs_inode *const vi = EROFS_I(inode);
594 struct super_block *const sb = inode->i_sb;
597 struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
599 struct z_erofs_map_header *h;
601 if (test_bit(EROFS_I_Z_INITED_BIT, &vi->flags)) {
603 * paired with smp_mb() at the end of the function to ensure
604 * fields will only be observed after the bit is set.
610 if (wait_on_bit_lock(&vi->flags, EROFS_I_BL_Z_BIT, TASK_KILLABLE))
614 if (test_bit(EROFS_I_Z_INITED_BIT, &vi->flags))
617 pos = ALIGN(erofs_iloc(inode) + vi->inode_isize + vi->xattr_isize, 8);
618 kaddr = erofs_read_metabuf(&buf, sb, erofs_blknr(sb, pos), EROFS_KMAP);
620 err = PTR_ERR(kaddr);
624 h = kaddr + erofs_blkoff(sb, pos);
626 * if the highest bit of the 8-byte map header is set, the whole file
627 * is stored in the packed inode. The rest bits keeps z_fragmentoff.
629 if (h->h_clusterbits >> Z_EROFS_FRAGMENT_INODE_BIT) {
630 vi->z_advise = Z_EROFS_ADVISE_FRAGMENT_PCLUSTER;
631 vi->z_fragmentoff = le64_to_cpu(*(__le64 *)h) ^ (1ULL << 63);
632 vi->z_tailextent_headlcn = 0;
635 vi->z_advise = le16_to_cpu(h->h_advise);
636 vi->z_algorithmtype[0] = h->h_algorithmtype & 15;
637 vi->z_algorithmtype[1] = h->h_algorithmtype >> 4;
640 if (vi->z_algorithmtype[0] >= Z_EROFS_COMPRESSION_MAX ||
641 vi->z_algorithmtype[++headnr] >= Z_EROFS_COMPRESSION_MAX) {
642 erofs_err(sb, "unknown HEAD%u format %u for nid %llu, please upgrade kernel",
643 headnr + 1, vi->z_algorithmtype[headnr], vi->nid);
645 goto out_put_metabuf;
648 vi->z_logical_clusterbits = sb->s_blocksize_bits + (h->h_clusterbits & 7);
649 if (!erofs_sb_has_big_pcluster(EROFS_SB(sb)) &&
650 vi->z_advise & (Z_EROFS_ADVISE_BIG_PCLUSTER_1 |
651 Z_EROFS_ADVISE_BIG_PCLUSTER_2)) {
652 erofs_err(sb, "per-inode big pcluster without sb feature for nid %llu",
655 goto out_put_metabuf;
657 if (vi->datalayout == EROFS_INODE_COMPRESSED_COMPACT &&
658 !(vi->z_advise & Z_EROFS_ADVISE_BIG_PCLUSTER_1) ^
659 !(vi->z_advise & Z_EROFS_ADVISE_BIG_PCLUSTER_2)) {
660 erofs_err(sb, "big pcluster head1/2 of compact indexes should be consistent for nid %llu",
663 goto out_put_metabuf;
666 if (vi->z_advise & Z_EROFS_ADVISE_INLINE_PCLUSTER) {
667 struct erofs_map_blocks map = {
668 .buf = __EROFS_BUF_INITIALIZER
671 vi->z_idata_size = le16_to_cpu(h->h_idata_size);
672 err = z_erofs_do_map_blocks(inode, &map,
673 EROFS_GET_BLOCKS_FINDTAIL);
674 erofs_put_metabuf(&map.buf);
677 erofs_blkoff(sb, map.m_pa) + map.m_plen > sb->s_blocksize) {
678 erofs_err(sb, "invalid tail-packing pclustersize %llu",
683 goto out_put_metabuf;
686 if (vi->z_advise & Z_EROFS_ADVISE_FRAGMENT_PCLUSTER &&
687 !(h->h_clusterbits >> Z_EROFS_FRAGMENT_INODE_BIT)) {
688 struct erofs_map_blocks map = {
689 .buf = __EROFS_BUF_INITIALIZER
692 vi->z_fragmentoff = le32_to_cpu(h->h_fragmentoff);
693 err = z_erofs_do_map_blocks(inode, &map,
694 EROFS_GET_BLOCKS_FINDTAIL);
695 erofs_put_metabuf(&map.buf);
697 goto out_put_metabuf;
700 /* paired with smp_mb() at the beginning of the function */
702 set_bit(EROFS_I_Z_INITED_BIT, &vi->flags);
704 erofs_put_metabuf(&buf);
706 clear_and_wake_up_bit(EROFS_I_BL_Z_BIT, &vi->flags);
710 int z_erofs_map_blocks_iter(struct inode *inode, struct erofs_map_blocks *map,
713 struct erofs_inode *const vi = EROFS_I(inode);
716 trace_z_erofs_map_blocks_iter_enter(inode, map, flags);
718 /* when trying to read beyond EOF, leave it unmapped */
719 if (map->m_la >= inode->i_size) {
720 map->m_llen = map->m_la + 1 - inode->i_size;
721 map->m_la = inode->i_size;
726 err = z_erofs_fill_inode_lazy(inode);
730 if ((vi->z_advise & Z_EROFS_ADVISE_FRAGMENT_PCLUSTER) &&
731 !vi->z_tailextent_headlcn) {
733 map->m_llen = inode->i_size;
734 map->m_flags = EROFS_MAP_MAPPED | EROFS_MAP_FULL_MAPPED |
739 err = z_erofs_do_map_blocks(inode, map, flags);
741 trace_z_erofs_map_blocks_iter_exit(inode, map, flags, err);
745 static int z_erofs_iomap_begin_report(struct inode *inode, loff_t offset,
746 loff_t length, unsigned int flags,
747 struct iomap *iomap, struct iomap *srcmap)
750 struct erofs_map_blocks map = { .m_la = offset };
752 ret = z_erofs_map_blocks_iter(inode, &map, EROFS_GET_BLOCKS_FIEMAP);
753 erofs_put_metabuf(&map.buf);
757 iomap->bdev = inode->i_sb->s_bdev;
758 iomap->offset = map.m_la;
759 iomap->length = map.m_llen;
760 if (map.m_flags & EROFS_MAP_MAPPED) {
761 iomap->type = IOMAP_MAPPED;
762 iomap->addr = map.m_flags & EROFS_MAP_FRAGMENT ?
763 IOMAP_NULL_ADDR : map.m_pa;
765 iomap->type = IOMAP_HOLE;
766 iomap->addr = IOMAP_NULL_ADDR;
768 * No strict rule on how to describe extents for post EOF, yet
769 * we need to do like below. Otherwise, iomap itself will get
770 * into an endless loop on post EOF.
772 * Calculate the effective offset by subtracting extent start
773 * (map.m_la) from the requested offset, and add it to length.
774 * (NB: offset >= map.m_la always)
776 if (iomap->offset >= inode->i_size)
777 iomap->length = length + offset - map.m_la;
783 const struct iomap_ops z_erofs_iomap_report_ops = {
784 .iomap_begin = z_erofs_iomap_begin_report,