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) 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 do { 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 DBG_BUGON(!m->delta[1] && 425 m->clusterofs != 1 << lclusterbits); 426 } else if (m->type == Z_EROFS_LCLUSTER_TYPE_PLAIN || 427 m->type == Z_EROFS_LCLUSTER_TYPE_HEAD1 || 428 m->type == Z_EROFS_LCLUSTER_TYPE_HEAD2) { 429 /* go on until the next HEAD lcluster */ 430 if (lcn != headlcn) 431 break; 432 m->delta[1] = 1; 433 } else { 434 erofs_err(inode->i_sb, "unknown type %u @ lcn %llu of nid %llu", 435 m->type, lcn, vi->nid); 436 DBG_BUGON(1); 437 return -EOPNOTSUPP; 438 } 439 lcn += m->delta[1]; 440 } while (m->delta[1]); 441 442 map->m_llen = (lcn << lclusterbits) + m->clusterofs - map->m_la; 443 return 0; 444 } 445 446 static int z_erofs_do_map_blocks(struct inode *inode, 447 struct erofs_map_blocks *map, int flags) 448 { 449 struct erofs_inode *const vi = EROFS_I(inode); 450 bool ztailpacking = vi->z_advise & Z_EROFS_ADVISE_INLINE_PCLUSTER; 451 bool fragment = vi->z_advise & Z_EROFS_ADVISE_FRAGMENT_PCLUSTER; 452 struct z_erofs_maprecorder m = { 453 .inode = inode, 454 .map = map, 455 }; 456 int err = 0; 457 unsigned int lclusterbits, endoff; 458 unsigned long initial_lcn; 459 unsigned long long ofs, end; 460 461 lclusterbits = vi->z_logical_clusterbits; 462 ofs = flags & EROFS_GET_BLOCKS_FINDTAIL ? inode->i_size - 1 : map->m_la; 463 initial_lcn = ofs >> lclusterbits; 464 endoff = ofs & ((1 << lclusterbits) - 1); 465 466 err = z_erofs_load_lcluster_from_disk(&m, initial_lcn, false); 467 if (err) 468 goto unmap_out; 469 470 if (ztailpacking && (flags & EROFS_GET_BLOCKS_FINDTAIL)) 471 vi->z_idataoff = m.nextpackoff; 472 473 map->m_flags = EROFS_MAP_MAPPED | EROFS_MAP_ENCODED; 474 end = (m.lcn + 1ULL) << lclusterbits; 475 476 switch (m.type) { 477 case Z_EROFS_LCLUSTER_TYPE_PLAIN: 478 case Z_EROFS_LCLUSTER_TYPE_HEAD1: 479 case Z_EROFS_LCLUSTER_TYPE_HEAD2: 480 if (endoff >= m.clusterofs) { 481 m.headtype = m.type; 482 map->m_la = (m.lcn << lclusterbits) | m.clusterofs; 483 /* 484 * For ztailpacking files, in order to inline data more 485 * effectively, special EOF lclusters are now supported 486 * which can have three parts at most. 487 */ 488 if (ztailpacking && end > inode->i_size) 489 end = inode->i_size; 490 break; 491 } 492 /* m.lcn should be >= 1 if endoff < m.clusterofs */ 493 if (!m.lcn) { 494 erofs_err(inode->i_sb, 495 "invalid logical cluster 0 at nid %llu", 496 vi->nid); 497 err = -EFSCORRUPTED; 498 goto unmap_out; 499 } 500 end = (m.lcn << lclusterbits) | m.clusterofs; 501 map->m_flags |= EROFS_MAP_FULL_MAPPED; 502 m.delta[0] = 1; 503 fallthrough; 504 case Z_EROFS_LCLUSTER_TYPE_NONHEAD: 505 /* get the corresponding first chunk */ 506 err = z_erofs_extent_lookback(&m, m.delta[0]); 507 if (err) 508 goto unmap_out; 509 break; 510 default: 511 erofs_err(inode->i_sb, 512 "unknown type %u @ offset %llu of nid %llu", 513 m.type, ofs, vi->nid); 514 err = -EOPNOTSUPP; 515 goto unmap_out; 516 } 517 if (m.partialref) 518 map->m_flags |= EROFS_MAP_PARTIAL_REF; 519 map->m_llen = end - map->m_la; 520 521 if (flags & EROFS_GET_BLOCKS_FINDTAIL) { 522 vi->z_tailextent_headlcn = m.lcn; 523 /* for non-compact indexes, fragmentoff is 64 bits */ 524 if (fragment && vi->datalayout == EROFS_INODE_COMPRESSED_FULL) 525 vi->z_fragmentoff |= (u64)m.pblk << 32; 526 } 527 if (ztailpacking && m.lcn == vi->z_tailextent_headlcn) { 528 map->m_flags |= EROFS_MAP_META; 529 map->m_pa = vi->z_idataoff; 530 map->m_plen = vi->z_idata_size; 531 } else if (fragment && m.lcn == vi->z_tailextent_headlcn) { 532 map->m_flags |= EROFS_MAP_FRAGMENT; 533 } else { 534 map->m_pa = erofs_pos(inode->i_sb, m.pblk); 535 err = z_erofs_get_extent_compressedlen(&m, initial_lcn); 536 if (err) 537 goto unmap_out; 538 } 539 540 if (m.headtype == Z_EROFS_LCLUSTER_TYPE_PLAIN) { 541 if (map->m_llen > map->m_plen) { 542 DBG_BUGON(1); 543 err = -EFSCORRUPTED; 544 goto unmap_out; 545 } 546 if (vi->z_advise & Z_EROFS_ADVISE_INTERLACED_PCLUSTER) 547 map->m_algorithmformat = 548 Z_EROFS_COMPRESSION_INTERLACED; 549 else 550 map->m_algorithmformat = 551 Z_EROFS_COMPRESSION_SHIFTED; 552 } else if (m.headtype == Z_EROFS_LCLUSTER_TYPE_HEAD2) { 553 map->m_algorithmformat = vi->z_algorithmtype[1]; 554 } else { 555 map->m_algorithmformat = vi->z_algorithmtype[0]; 556 } 557 558 if ((flags & EROFS_GET_BLOCKS_FIEMAP) || 559 ((flags & EROFS_GET_BLOCKS_READMORE) && 560 (map->m_algorithmformat == Z_EROFS_COMPRESSION_LZMA || 561 map->m_algorithmformat == Z_EROFS_COMPRESSION_DEFLATE) && 562 map->m_llen >= i_blocksize(inode))) { 563 err = z_erofs_get_extent_decompressedlen(&m); 564 if (!err) 565 map->m_flags |= EROFS_MAP_FULL_MAPPED; 566 } 567 568 unmap_out: 569 erofs_unmap_metabuf(&m.map->buf); 570 return err; 571 } 572 573 static int z_erofs_fill_inode_lazy(struct inode *inode) 574 { 575 struct erofs_inode *const vi = EROFS_I(inode); 576 struct super_block *const sb = inode->i_sb; 577 int err, headnr; 578 erofs_off_t pos; 579 struct erofs_buf buf = __EROFS_BUF_INITIALIZER; 580 void *kaddr; 581 struct z_erofs_map_header *h; 582 583 if (test_bit(EROFS_I_Z_INITED_BIT, &vi->flags)) { 584 /* 585 * paired with smp_mb() at the end of the function to ensure 586 * fields will only be observed after the bit is set. 587 */ 588 smp_mb(); 589 return 0; 590 } 591 592 if (wait_on_bit_lock(&vi->flags, EROFS_I_BL_Z_BIT, TASK_KILLABLE)) 593 return -ERESTARTSYS; 594 595 err = 0; 596 if (test_bit(EROFS_I_Z_INITED_BIT, &vi->flags)) 597 goto out_unlock; 598 599 pos = ALIGN(erofs_iloc(inode) + vi->inode_isize + vi->xattr_isize, 8); 600 kaddr = erofs_read_metabuf(&buf, sb, erofs_blknr(sb, pos), EROFS_KMAP); 601 if (IS_ERR(kaddr)) { 602 err = PTR_ERR(kaddr); 603 goto out_unlock; 604 } 605 606 h = kaddr + erofs_blkoff(sb, pos); 607 /* 608 * if the highest bit of the 8-byte map header is set, the whole file 609 * is stored in the packed inode. The rest bits keeps z_fragmentoff. 610 */ 611 if (h->h_clusterbits >> Z_EROFS_FRAGMENT_INODE_BIT) { 612 vi->z_advise = Z_EROFS_ADVISE_FRAGMENT_PCLUSTER; 613 vi->z_fragmentoff = le64_to_cpu(*(__le64 *)h) ^ (1ULL << 63); 614 vi->z_tailextent_headlcn = 0; 615 goto done; 616 } 617 vi->z_advise = le16_to_cpu(h->h_advise); 618 vi->z_algorithmtype[0] = h->h_algorithmtype & 15; 619 vi->z_algorithmtype[1] = h->h_algorithmtype >> 4; 620 621 headnr = 0; 622 if (vi->z_algorithmtype[0] >= Z_EROFS_COMPRESSION_MAX || 623 vi->z_algorithmtype[++headnr] >= Z_EROFS_COMPRESSION_MAX) { 624 erofs_err(sb, "unknown HEAD%u format %u for nid %llu, please upgrade kernel", 625 headnr + 1, vi->z_algorithmtype[headnr], vi->nid); 626 err = -EOPNOTSUPP; 627 goto out_put_metabuf; 628 } 629 630 vi->z_logical_clusterbits = sb->s_blocksize_bits + (h->h_clusterbits & 7); 631 if (!erofs_sb_has_big_pcluster(EROFS_SB(sb)) && 632 vi->z_advise & (Z_EROFS_ADVISE_BIG_PCLUSTER_1 | 633 Z_EROFS_ADVISE_BIG_PCLUSTER_2)) { 634 erofs_err(sb, "per-inode big pcluster without sb feature for nid %llu", 635 vi->nid); 636 err = -EFSCORRUPTED; 637 goto out_put_metabuf; 638 } 639 if (vi->datalayout == EROFS_INODE_COMPRESSED_COMPACT && 640 !(vi->z_advise & Z_EROFS_ADVISE_BIG_PCLUSTER_1) ^ 641 !(vi->z_advise & Z_EROFS_ADVISE_BIG_PCLUSTER_2)) { 642 erofs_err(sb, "big pcluster head1/2 of compact indexes should be consistent for nid %llu", 643 vi->nid); 644 err = -EFSCORRUPTED; 645 goto out_put_metabuf; 646 } 647 648 if (vi->z_advise & Z_EROFS_ADVISE_INLINE_PCLUSTER) { 649 struct erofs_map_blocks map = { 650 .buf = __EROFS_BUF_INITIALIZER 651 }; 652 653 vi->z_idata_size = le16_to_cpu(h->h_idata_size); 654 err = z_erofs_do_map_blocks(inode, &map, 655 EROFS_GET_BLOCKS_FINDTAIL); 656 erofs_put_metabuf(&map.buf); 657 658 if (!map.m_plen || 659 erofs_blkoff(sb, map.m_pa) + map.m_plen > sb->s_blocksize) { 660 erofs_err(sb, "invalid tail-packing pclustersize %llu", 661 map.m_plen); 662 err = -EFSCORRUPTED; 663 } 664 if (err < 0) 665 goto out_put_metabuf; 666 } 667 668 if (vi->z_advise & Z_EROFS_ADVISE_FRAGMENT_PCLUSTER && 669 !(h->h_clusterbits >> Z_EROFS_FRAGMENT_INODE_BIT)) { 670 struct erofs_map_blocks map = { 671 .buf = __EROFS_BUF_INITIALIZER 672 }; 673 674 vi->z_fragmentoff = le32_to_cpu(h->h_fragmentoff); 675 err = z_erofs_do_map_blocks(inode, &map, 676 EROFS_GET_BLOCKS_FINDTAIL); 677 erofs_put_metabuf(&map.buf); 678 if (err < 0) 679 goto out_put_metabuf; 680 } 681 done: 682 /* paired with smp_mb() at the beginning of the function */ 683 smp_mb(); 684 set_bit(EROFS_I_Z_INITED_BIT, &vi->flags); 685 out_put_metabuf: 686 erofs_put_metabuf(&buf); 687 out_unlock: 688 clear_and_wake_up_bit(EROFS_I_BL_Z_BIT, &vi->flags); 689 return err; 690 } 691 692 int z_erofs_map_blocks_iter(struct inode *inode, struct erofs_map_blocks *map, 693 int flags) 694 { 695 struct erofs_inode *const vi = EROFS_I(inode); 696 int err = 0; 697 698 trace_z_erofs_map_blocks_iter_enter(inode, map, flags); 699 700 /* when trying to read beyond EOF, leave it unmapped */ 701 if (map->m_la >= inode->i_size) { 702 map->m_llen = map->m_la + 1 - inode->i_size; 703 map->m_la = inode->i_size; 704 map->m_flags = 0; 705 goto out; 706 } 707 708 err = z_erofs_fill_inode_lazy(inode); 709 if (err) 710 goto out; 711 712 if ((vi->z_advise & Z_EROFS_ADVISE_FRAGMENT_PCLUSTER) && 713 !vi->z_tailextent_headlcn) { 714 map->m_la = 0; 715 map->m_llen = inode->i_size; 716 map->m_flags = EROFS_MAP_MAPPED | EROFS_MAP_FULL_MAPPED | 717 EROFS_MAP_FRAGMENT; 718 goto out; 719 } 720 721 err = z_erofs_do_map_blocks(inode, map, flags); 722 out: 723 trace_z_erofs_map_blocks_iter_exit(inode, map, flags, err); 724 return err; 725 } 726 727 static int z_erofs_iomap_begin_report(struct inode *inode, loff_t offset, 728 loff_t length, unsigned int flags, 729 struct iomap *iomap, struct iomap *srcmap) 730 { 731 int ret; 732 struct erofs_map_blocks map = { .m_la = offset }; 733 734 ret = z_erofs_map_blocks_iter(inode, &map, EROFS_GET_BLOCKS_FIEMAP); 735 erofs_put_metabuf(&map.buf); 736 if (ret < 0) 737 return ret; 738 739 iomap->bdev = inode->i_sb->s_bdev; 740 iomap->offset = map.m_la; 741 iomap->length = map.m_llen; 742 if (map.m_flags & EROFS_MAP_MAPPED) { 743 iomap->type = IOMAP_MAPPED; 744 iomap->addr = map.m_flags & EROFS_MAP_FRAGMENT ? 745 IOMAP_NULL_ADDR : map.m_pa; 746 } else { 747 iomap->type = IOMAP_HOLE; 748 iomap->addr = IOMAP_NULL_ADDR; 749 /* 750 * No strict rule on how to describe extents for post EOF, yet 751 * we need to do like below. Otherwise, iomap itself will get 752 * into an endless loop on post EOF. 753 * 754 * Calculate the effective offset by subtracting extent start 755 * (map.m_la) from the requested offset, and add it to length. 756 * (NB: offset >= map.m_la always) 757 */ 758 if (iomap->offset >= inode->i_size) 759 iomap->length = length + offset - map.m_la; 760 } 761 iomap->flags = 0; 762 return 0; 763 } 764 765 const struct iomap_ops z_erofs_iomap_report_ops = { 766 .iomap_begin = z_erofs_iomap_begin_report, 767 }; 768