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