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