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 <asm/unaligned.h> 8 #include <trace/events/erofs.h> 9 10 int z_erofs_fill_inode(struct inode *inode) 11 { 12 struct erofs_inode *const vi = EROFS_I(inode); 13 struct erofs_sb_info *sbi = EROFS_SB(inode->i_sb); 14 15 if (!erofs_sb_has_big_pcluster(sbi) && 16 vi->datalayout == EROFS_INODE_FLAT_COMPRESSION_LEGACY) { 17 vi->z_advise = 0; 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); 22 } 23 inode->i_mapping->a_ops = &z_erofs_aops; 24 return 0; 25 } 26 27 static int z_erofs_fill_inode_lazy(struct inode *inode) 28 { 29 struct erofs_inode *const vi = EROFS_I(inode); 30 struct super_block *const sb = inode->i_sb; 31 int err; 32 erofs_off_t pos; 33 struct page *page; 34 void *kaddr; 35 struct z_erofs_map_header *h; 36 37 if (test_bit(EROFS_I_Z_INITED_BIT, &vi->flags)) { 38 /* 39 * paired with smp_mb() at the end of the function to ensure 40 * fields will only be observed after the bit is set. 41 */ 42 smp_mb(); 43 return 0; 44 } 45 46 if (wait_on_bit_lock(&vi->flags, EROFS_I_BL_Z_BIT, TASK_KILLABLE)) 47 return -ERESTARTSYS; 48 49 err = 0; 50 if (test_bit(EROFS_I_Z_INITED_BIT, &vi->flags)) 51 goto out_unlock; 52 53 DBG_BUGON(!erofs_sb_has_big_pcluster(EROFS_SB(sb)) && 54 vi->datalayout == EROFS_INODE_FLAT_COMPRESSION_LEGACY); 55 56 pos = ALIGN(iloc(EROFS_SB(sb), vi->nid) + vi->inode_isize + 57 vi->xattr_isize, 8); 58 page = erofs_get_meta_page(sb, erofs_blknr(pos)); 59 if (IS_ERR(page)) { 60 err = PTR_ERR(page); 61 goto out_unlock; 62 } 63 64 kaddr = kmap_atomic(page); 65 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; 70 71 if (vi->z_algorithmtype[0] >= Z_EROFS_COMPRESSION_MAX) { 72 erofs_err(sb, "unknown compression format %u for nid %llu, please upgrade kernel", 73 vi->z_algorithmtype[0], vi->nid); 74 err = -EOPNOTSUPP; 75 goto unmap_done; 76 } 77 78 vi->z_logical_clusterbits = LOG_BLOCK_SIZE + (h->h_clusterbits & 7); 79 if (!erofs_sb_has_big_pcluster(EROFS_SB(sb)) && 80 vi->z_advise & (Z_EROFS_ADVISE_BIG_PCLUSTER_1 | 81 Z_EROFS_ADVISE_BIG_PCLUSTER_2)) { 82 erofs_err(sb, "per-inode big pcluster without sb feature for nid %llu", 83 vi->nid); 84 err = -EFSCORRUPTED; 85 goto unmap_done; 86 } 87 if (vi->datalayout == EROFS_INODE_FLAT_COMPRESSION && 88 !(vi->z_advise & Z_EROFS_ADVISE_BIG_PCLUSTER_1) ^ 89 !(vi->z_advise & Z_EROFS_ADVISE_BIG_PCLUSTER_2)) { 90 erofs_err(sb, "big pcluster head1/2 of compact indexes should be consistent for nid %llu", 91 vi->nid); 92 err = -EFSCORRUPTED; 93 goto unmap_done; 94 } 95 /* paired with smp_mb() at the beginning of the function */ 96 smp_mb(); 97 set_bit(EROFS_I_Z_INITED_BIT, &vi->flags); 98 unmap_done: 99 kunmap_atomic(kaddr); 100 unlock_page(page); 101 put_page(page); 102 out_unlock: 103 clear_and_wake_up_bit(EROFS_I_BL_Z_BIT, &vi->flags); 104 return err; 105 } 106 107 struct z_erofs_maprecorder { 108 struct inode *inode; 109 struct erofs_map_blocks *map; 110 void *kaddr; 111 112 unsigned long lcn; 113 /* compression extent information gathered */ 114 u8 type; 115 u16 clusterofs; 116 u16 delta[2]; 117 erofs_blk_t pblk, compressedlcs; 118 }; 119 120 static int z_erofs_reload_indexes(struct z_erofs_maprecorder *m, 121 erofs_blk_t eblk) 122 { 123 struct super_block *const sb = m->inode->i_sb; 124 struct erofs_map_blocks *const map = m->map; 125 struct page *mpage = map->mpage; 126 127 if (mpage) { 128 if (mpage->index == eblk) { 129 if (!m->kaddr) 130 m->kaddr = kmap_atomic(mpage); 131 return 0; 132 } 133 134 if (m->kaddr) { 135 kunmap_atomic(m->kaddr); 136 m->kaddr = NULL; 137 } 138 put_page(mpage); 139 } 140 141 mpage = erofs_get_meta_page(sb, eblk); 142 if (IS_ERR(mpage)) { 143 map->mpage = NULL; 144 return PTR_ERR(mpage); 145 } 146 m->kaddr = kmap_atomic(mpage); 147 unlock_page(mpage); 148 map->mpage = mpage; 149 return 0; 150 } 151 152 static int legacy_load_cluster_from_disk(struct z_erofs_maprecorder *m, 153 unsigned long lcn) 154 { 155 struct inode *const inode = m->inode; 156 struct erofs_inode *const vi = EROFS_I(inode); 157 const erofs_off_t ibase = iloc(EROFS_I_SB(inode), vi->nid); 158 const erofs_off_t pos = 159 Z_EROFS_VLE_LEGACY_INDEX_ALIGN(ibase + vi->inode_isize + 160 vi->xattr_isize) + 161 lcn * sizeof(struct z_erofs_vle_decompressed_index); 162 struct z_erofs_vle_decompressed_index *di; 163 unsigned int advise, type; 164 int err; 165 166 err = z_erofs_reload_indexes(m, erofs_blknr(pos)); 167 if (err) 168 return err; 169 170 m->lcn = lcn; 171 di = m->kaddr + erofs_blkoff(pos); 172 173 advise = le16_to_cpu(di->di_advise); 174 type = (advise >> Z_EROFS_VLE_DI_CLUSTER_TYPE_BIT) & 175 ((1 << Z_EROFS_VLE_DI_CLUSTER_TYPE_BITS) - 1); 176 switch (type) { 177 case Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD: 178 m->clusterofs = 1 << vi->z_logical_clusterbits; 179 m->delta[0] = le16_to_cpu(di->di_u.delta[0]); 180 if (m->delta[0] & Z_EROFS_VLE_DI_D0_CBLKCNT) { 181 if (!(vi->z_advise & Z_EROFS_ADVISE_BIG_PCLUSTER_1)) { 182 DBG_BUGON(1); 183 return -EFSCORRUPTED; 184 } 185 m->compressedlcs = m->delta[0] & 186 ~Z_EROFS_VLE_DI_D0_CBLKCNT; 187 m->delta[0] = 1; 188 } 189 m->delta[1] = le16_to_cpu(di->di_u.delta[1]); 190 break; 191 case Z_EROFS_VLE_CLUSTER_TYPE_PLAIN: 192 case Z_EROFS_VLE_CLUSTER_TYPE_HEAD: 193 m->clusterofs = le16_to_cpu(di->di_clusterofs); 194 m->pblk = le32_to_cpu(di->di_u.blkaddr); 195 break; 196 default: 197 DBG_BUGON(1); 198 return -EOPNOTSUPP; 199 } 200 m->type = type; 201 return 0; 202 } 203 204 static unsigned int decode_compactedbits(unsigned int lobits, 205 unsigned int lomask, 206 u8 *in, unsigned int pos, u8 *type) 207 { 208 const unsigned int v = get_unaligned_le32(in + pos / 8) >> (pos & 7); 209 const unsigned int lo = v & lomask; 210 211 *type = (v >> lobits) & 3; 212 return lo; 213 } 214 215 static int get_compacted_la_distance(unsigned int lclusterbits, 216 unsigned int encodebits, 217 unsigned int vcnt, u8 *in, int i) 218 { 219 const unsigned int lomask = (1 << lclusterbits) - 1; 220 unsigned int lo, d1 = 0; 221 u8 type; 222 223 DBG_BUGON(i >= vcnt); 224 225 do { 226 lo = decode_compactedbits(lclusterbits, lomask, 227 in, encodebits * i, &type); 228 229 if (type != Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD) 230 return d1; 231 ++d1; 232 } while (++i < vcnt); 233 234 /* vcnt - 1 (Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD) item */ 235 if (!(lo & Z_EROFS_VLE_DI_D0_CBLKCNT)) 236 d1 += lo - 1; 237 return d1; 238 } 239 240 static int unpack_compacted_index(struct z_erofs_maprecorder *m, 241 unsigned int amortizedshift, 242 unsigned int eofs, bool lookahead) 243 { 244 struct erofs_inode *const vi = EROFS_I(m->inode); 245 const unsigned int lclusterbits = vi->z_logical_clusterbits; 246 const unsigned int lomask = (1 << lclusterbits) - 1; 247 unsigned int vcnt, base, lo, encodebits, nblk; 248 int i; 249 u8 *in, type; 250 bool big_pcluster; 251 252 if (1 << amortizedshift == 4) 253 vcnt = 2; 254 else if (1 << amortizedshift == 2 && lclusterbits == 12) 255 vcnt = 16; 256 else 257 return -EOPNOTSUPP; 258 259 big_pcluster = vi->z_advise & Z_EROFS_ADVISE_BIG_PCLUSTER_1; 260 encodebits = ((vcnt << amortizedshift) - sizeof(__le32)) * 8 / vcnt; 261 base = round_down(eofs, vcnt << amortizedshift); 262 in = m->kaddr + base; 263 264 i = (eofs - base) >> amortizedshift; 265 266 lo = decode_compactedbits(lclusterbits, lomask, 267 in, encodebits * i, &type); 268 m->type = type; 269 if (type == Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD) { 270 m->clusterofs = 1 << lclusterbits; 271 272 /* figure out lookahead_distance: delta[1] if needed */ 273 if (lookahead) 274 m->delta[1] = get_compacted_la_distance(lclusterbits, 275 encodebits, vcnt, in, i); 276 if (lo & Z_EROFS_VLE_DI_D0_CBLKCNT) { 277 if (!big_pcluster) { 278 DBG_BUGON(1); 279 return -EFSCORRUPTED; 280 } 281 m->compressedlcs = lo & ~Z_EROFS_VLE_DI_D0_CBLKCNT; 282 m->delta[0] = 1; 283 return 0; 284 } else if (i + 1 != (int)vcnt) { 285 m->delta[0] = lo; 286 return 0; 287 } 288 /* 289 * since the last lcluster in the pack is special, 290 * of which lo saves delta[1] rather than delta[0]. 291 * Hence, get delta[0] by the previous lcluster indirectly. 292 */ 293 lo = decode_compactedbits(lclusterbits, lomask, 294 in, encodebits * (i - 1), &type); 295 if (type != Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD) 296 lo = 0; 297 else if (lo & Z_EROFS_VLE_DI_D0_CBLKCNT) 298 lo = 1; 299 m->delta[0] = lo + 1; 300 return 0; 301 } 302 m->clusterofs = lo; 303 m->delta[0] = 0; 304 /* figout out blkaddr (pblk) for HEAD lclusters */ 305 if (!big_pcluster) { 306 nblk = 1; 307 while (i > 0) { 308 --i; 309 lo = decode_compactedbits(lclusterbits, lomask, 310 in, encodebits * i, &type); 311 if (type == Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD) 312 i -= lo; 313 314 if (i >= 0) 315 ++nblk; 316 } 317 } else { 318 nblk = 0; 319 while (i > 0) { 320 --i; 321 lo = decode_compactedbits(lclusterbits, lomask, 322 in, encodebits * i, &type); 323 if (type == Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD) { 324 if (lo & Z_EROFS_VLE_DI_D0_CBLKCNT) { 325 --i; 326 nblk += lo & ~Z_EROFS_VLE_DI_D0_CBLKCNT; 327 continue; 328 } 329 /* bigpcluster shouldn't have plain d0 == 1 */ 330 if (lo <= 1) { 331 DBG_BUGON(1); 332 return -EFSCORRUPTED; 333 } 334 i -= lo - 2; 335 continue; 336 } 337 ++nblk; 338 } 339 } 340 in += (vcnt << amortizedshift) - sizeof(__le32); 341 m->pblk = le32_to_cpu(*(__le32 *)in) + nblk; 342 return 0; 343 } 344 345 static int compacted_load_cluster_from_disk(struct z_erofs_maprecorder *m, 346 unsigned long lcn, bool lookahead) 347 { 348 struct inode *const inode = m->inode; 349 struct erofs_inode *const vi = EROFS_I(inode); 350 const unsigned int lclusterbits = vi->z_logical_clusterbits; 351 const erofs_off_t ebase = ALIGN(iloc(EROFS_I_SB(inode), vi->nid) + 352 vi->inode_isize + vi->xattr_isize, 8) + 353 sizeof(struct z_erofs_map_header); 354 const unsigned int totalidx = DIV_ROUND_UP(inode->i_size, EROFS_BLKSIZ); 355 unsigned int compacted_4b_initial, compacted_2b; 356 unsigned int amortizedshift; 357 erofs_off_t pos; 358 int err; 359 360 if (lclusterbits != 12) 361 return -EOPNOTSUPP; 362 363 if (lcn >= totalidx) 364 return -EINVAL; 365 366 m->lcn = lcn; 367 /* used to align to 32-byte (compacted_2b) alignment */ 368 compacted_4b_initial = (32 - ebase % 32) / 4; 369 if (compacted_4b_initial == 32 / 4) 370 compacted_4b_initial = 0; 371 372 if (vi->z_advise & Z_EROFS_ADVISE_COMPACTED_2B) 373 compacted_2b = rounddown(totalidx - compacted_4b_initial, 16); 374 else 375 compacted_2b = 0; 376 377 pos = ebase; 378 if (lcn < compacted_4b_initial) { 379 amortizedshift = 2; 380 goto out; 381 } 382 pos += compacted_4b_initial * 4; 383 lcn -= compacted_4b_initial; 384 385 if (lcn < compacted_2b) { 386 amortizedshift = 1; 387 goto out; 388 } 389 pos += compacted_2b * 2; 390 lcn -= compacted_2b; 391 amortizedshift = 2; 392 out: 393 pos += lcn * (1 << amortizedshift); 394 err = z_erofs_reload_indexes(m, erofs_blknr(pos)); 395 if (err) 396 return err; 397 return unpack_compacted_index(m, amortizedshift, erofs_blkoff(pos), 398 lookahead); 399 } 400 401 static int z_erofs_load_cluster_from_disk(struct z_erofs_maprecorder *m, 402 unsigned int lcn, bool lookahead) 403 { 404 const unsigned int datamode = EROFS_I(m->inode)->datalayout; 405 406 if (datamode == EROFS_INODE_FLAT_COMPRESSION_LEGACY) 407 return legacy_load_cluster_from_disk(m, lcn); 408 409 if (datamode == EROFS_INODE_FLAT_COMPRESSION) 410 return compacted_load_cluster_from_disk(m, lcn, lookahead); 411 412 return -EINVAL; 413 } 414 415 static int z_erofs_extent_lookback(struct z_erofs_maprecorder *m, 416 unsigned int lookback_distance) 417 { 418 struct erofs_inode *const vi = EROFS_I(m->inode); 419 struct erofs_map_blocks *const map = m->map; 420 const unsigned int lclusterbits = vi->z_logical_clusterbits; 421 unsigned long lcn = m->lcn; 422 int err; 423 424 if (lcn < lookback_distance) { 425 erofs_err(m->inode->i_sb, 426 "bogus lookback distance @ nid %llu", vi->nid); 427 DBG_BUGON(1); 428 return -EFSCORRUPTED; 429 } 430 431 /* load extent head logical cluster if needed */ 432 lcn -= lookback_distance; 433 err = z_erofs_load_cluster_from_disk(m, lcn, false); 434 if (err) 435 return err; 436 437 switch (m->type) { 438 case Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD: 439 if (!m->delta[0]) { 440 erofs_err(m->inode->i_sb, 441 "invalid lookback distance 0 @ nid %llu", 442 vi->nid); 443 DBG_BUGON(1); 444 return -EFSCORRUPTED; 445 } 446 return z_erofs_extent_lookback(m, m->delta[0]); 447 case Z_EROFS_VLE_CLUSTER_TYPE_PLAIN: 448 map->m_flags &= ~EROFS_MAP_ZIPPED; 449 fallthrough; 450 case Z_EROFS_VLE_CLUSTER_TYPE_HEAD: 451 map->m_la = (lcn << lclusterbits) | m->clusterofs; 452 break; 453 default: 454 erofs_err(m->inode->i_sb, 455 "unknown type %u @ lcn %lu of nid %llu", 456 m->type, lcn, vi->nid); 457 DBG_BUGON(1); 458 return -EOPNOTSUPP; 459 } 460 return 0; 461 } 462 463 static int z_erofs_get_extent_compressedlen(struct z_erofs_maprecorder *m, 464 unsigned int initial_lcn) 465 { 466 struct erofs_inode *const vi = EROFS_I(m->inode); 467 struct erofs_map_blocks *const map = m->map; 468 const unsigned int lclusterbits = vi->z_logical_clusterbits; 469 unsigned long lcn; 470 int err; 471 472 DBG_BUGON(m->type != Z_EROFS_VLE_CLUSTER_TYPE_PLAIN && 473 m->type != Z_EROFS_VLE_CLUSTER_TYPE_HEAD); 474 if (!(map->m_flags & EROFS_MAP_ZIPPED) || 475 !(vi->z_advise & Z_EROFS_ADVISE_BIG_PCLUSTER_1)) { 476 map->m_plen = 1 << lclusterbits; 477 return 0; 478 } 479 480 lcn = m->lcn + 1; 481 if (m->compressedlcs) 482 goto out; 483 484 err = z_erofs_load_cluster_from_disk(m, lcn, false); 485 if (err) 486 return err; 487 488 /* 489 * If the 1st NONHEAD lcluster has already been handled initially w/o 490 * valid compressedlcs, which means at least it mustn't be CBLKCNT, or 491 * an internal implemenatation error is detected. 492 * 493 * The following code can also handle it properly anyway, but let's 494 * BUG_ON in the debugging mode only for developers to notice that. 495 */ 496 DBG_BUGON(lcn == initial_lcn && 497 m->type == Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD); 498 499 switch (m->type) { 500 case Z_EROFS_VLE_CLUSTER_TYPE_PLAIN: 501 case Z_EROFS_VLE_CLUSTER_TYPE_HEAD: 502 /* 503 * if the 1st NONHEAD lcluster is actually PLAIN or HEAD type 504 * rather than CBLKCNT, it's a 1 lcluster-sized pcluster. 505 */ 506 m->compressedlcs = 1; 507 break; 508 case Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD: 509 if (m->delta[0] != 1) 510 goto err_bonus_cblkcnt; 511 if (m->compressedlcs) 512 break; 513 fallthrough; 514 default: 515 erofs_err(m->inode->i_sb, 516 "cannot found CBLKCNT @ lcn %lu of nid %llu", 517 lcn, vi->nid); 518 DBG_BUGON(1); 519 return -EFSCORRUPTED; 520 } 521 out: 522 map->m_plen = m->compressedlcs << lclusterbits; 523 return 0; 524 err_bonus_cblkcnt: 525 erofs_err(m->inode->i_sb, 526 "bogus CBLKCNT @ lcn %lu of nid %llu", 527 lcn, vi->nid); 528 DBG_BUGON(1); 529 return -EFSCORRUPTED; 530 } 531 532 static int z_erofs_get_extent_decompressedlen(struct z_erofs_maprecorder *m) 533 { 534 struct inode *inode = m->inode; 535 struct erofs_inode *vi = EROFS_I(inode); 536 struct erofs_map_blocks *map = m->map; 537 unsigned int lclusterbits = vi->z_logical_clusterbits; 538 u64 lcn = m->lcn, headlcn = map->m_la >> lclusterbits; 539 int err; 540 541 do { 542 /* handle the last EOF pcluster (no next HEAD lcluster) */ 543 if ((lcn << lclusterbits) >= inode->i_size) { 544 map->m_llen = inode->i_size - map->m_la; 545 return 0; 546 } 547 548 err = z_erofs_load_cluster_from_disk(m, lcn, true); 549 if (err) 550 return err; 551 552 if (m->type == Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD) { 553 DBG_BUGON(!m->delta[1] && 554 m->clusterofs != 1 << lclusterbits); 555 } else if (m->type == Z_EROFS_VLE_CLUSTER_TYPE_PLAIN || 556 m->type == Z_EROFS_VLE_CLUSTER_TYPE_HEAD) { 557 /* go on until the next HEAD lcluster */ 558 if (lcn != headlcn) 559 break; 560 m->delta[1] = 1; 561 } else { 562 erofs_err(inode->i_sb, "unknown type %u @ lcn %llu of nid %llu", 563 m->type, lcn, vi->nid); 564 DBG_BUGON(1); 565 return -EOPNOTSUPP; 566 } 567 lcn += m->delta[1]; 568 } while (m->delta[1]); 569 570 map->m_llen = (lcn << lclusterbits) + m->clusterofs - map->m_la; 571 return 0; 572 } 573 574 int z_erofs_map_blocks_iter(struct inode *inode, 575 struct erofs_map_blocks *map, 576 int flags) 577 { 578 struct erofs_inode *const vi = EROFS_I(inode); 579 struct z_erofs_maprecorder m = { 580 .inode = inode, 581 .map = map, 582 }; 583 int err = 0; 584 unsigned int lclusterbits, endoff; 585 unsigned long initial_lcn; 586 unsigned long long ofs, end; 587 588 trace_z_erofs_map_blocks_iter_enter(inode, map, flags); 589 590 /* when trying to read beyond EOF, leave it unmapped */ 591 if (map->m_la >= inode->i_size) { 592 map->m_llen = map->m_la + 1 - inode->i_size; 593 map->m_la = inode->i_size; 594 map->m_flags = 0; 595 goto out; 596 } 597 598 err = z_erofs_fill_inode_lazy(inode); 599 if (err) 600 goto out; 601 602 lclusterbits = vi->z_logical_clusterbits; 603 ofs = map->m_la; 604 initial_lcn = ofs >> lclusterbits; 605 endoff = ofs & ((1 << lclusterbits) - 1); 606 607 err = z_erofs_load_cluster_from_disk(&m, initial_lcn, false); 608 if (err) 609 goto unmap_out; 610 611 map->m_flags = EROFS_MAP_ZIPPED; /* by default, compressed */ 612 end = (m.lcn + 1ULL) << lclusterbits; 613 614 switch (m.type) { 615 case Z_EROFS_VLE_CLUSTER_TYPE_PLAIN: 616 if (endoff >= m.clusterofs) 617 map->m_flags &= ~EROFS_MAP_ZIPPED; 618 fallthrough; 619 case Z_EROFS_VLE_CLUSTER_TYPE_HEAD: 620 if (endoff >= m.clusterofs) { 621 map->m_la = (m.lcn << lclusterbits) | m.clusterofs; 622 break; 623 } 624 /* m.lcn should be >= 1 if endoff < m.clusterofs */ 625 if (!m.lcn) { 626 erofs_err(inode->i_sb, 627 "invalid logical cluster 0 at nid %llu", 628 vi->nid); 629 err = -EFSCORRUPTED; 630 goto unmap_out; 631 } 632 end = (m.lcn << lclusterbits) | m.clusterofs; 633 map->m_flags |= EROFS_MAP_FULL_MAPPED; 634 m.delta[0] = 1; 635 fallthrough; 636 case Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD: 637 /* get the corresponding first chunk */ 638 err = z_erofs_extent_lookback(&m, m.delta[0]); 639 if (err) 640 goto unmap_out; 641 break; 642 default: 643 erofs_err(inode->i_sb, 644 "unknown type %u @ offset %llu of nid %llu", 645 m.type, ofs, vi->nid); 646 err = -EOPNOTSUPP; 647 goto unmap_out; 648 } 649 650 map->m_llen = end - map->m_la; 651 map->m_pa = blknr_to_addr(m.pblk); 652 map->m_flags |= EROFS_MAP_MAPPED; 653 654 err = z_erofs_get_extent_compressedlen(&m, initial_lcn); 655 if (err) 656 goto out; 657 658 if (flags & EROFS_GET_BLOCKS_FIEMAP) { 659 err = z_erofs_get_extent_decompressedlen(&m); 660 if (!err) 661 map->m_flags |= EROFS_MAP_FULL_MAPPED; 662 } 663 unmap_out: 664 if (m.kaddr) 665 kunmap_atomic(m.kaddr); 666 667 out: 668 erofs_dbg("%s, m_la %llu m_pa %llu m_llen %llu m_plen %llu m_flags 0%o", 669 __func__, map->m_la, map->m_pa, 670 map->m_llen, map->m_plen, map->m_flags); 671 672 trace_z_erofs_map_blocks_iter_exit(inode, map, flags, err); 673 674 /* aggressively BUG_ON iff CONFIG_EROFS_FS_DEBUG is on */ 675 DBG_BUGON(err < 0 && err != -ENOMEM); 676 return err; 677 } 678 679 static int z_erofs_iomap_begin_report(struct inode *inode, loff_t offset, 680 loff_t length, unsigned int flags, 681 struct iomap *iomap, struct iomap *srcmap) 682 { 683 int ret; 684 struct erofs_map_blocks map = { .m_la = offset }; 685 686 ret = z_erofs_map_blocks_iter(inode, &map, EROFS_GET_BLOCKS_FIEMAP); 687 if (map.mpage) 688 put_page(map.mpage); 689 if (ret < 0) 690 return ret; 691 692 iomap->bdev = inode->i_sb->s_bdev; 693 iomap->offset = map.m_la; 694 iomap->length = map.m_llen; 695 if (map.m_flags & EROFS_MAP_MAPPED) { 696 iomap->type = IOMAP_MAPPED; 697 iomap->addr = map.m_pa; 698 } else { 699 iomap->type = IOMAP_HOLE; 700 iomap->addr = IOMAP_NULL_ADDR; 701 /* 702 * No strict rule how to describe extents for post EOF, yet 703 * we need do like below. Otherwise, iomap itself will get 704 * into an endless loop on post EOF. 705 */ 706 if (iomap->offset >= inode->i_size) 707 iomap->length = length + map.m_la - offset; 708 } 709 iomap->flags = 0; 710 return 0; 711 } 712 713 const struct iomap_ops z_erofs_iomap_report_ops = { 714 .iomap_begin = z_erofs_iomap_begin_report, 715 }; 716