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 unpack_compacted_index(struct z_erofs_maprecorder *m, 216 unsigned int amortizedshift, 217 unsigned int eofs) 218 { 219 struct erofs_inode *const vi = EROFS_I(m->inode); 220 const unsigned int lclusterbits = vi->z_logical_clusterbits; 221 const unsigned int lomask = (1 << lclusterbits) - 1; 222 unsigned int vcnt, base, lo, encodebits, nblk; 223 int i; 224 u8 *in, type; 225 bool big_pcluster; 226 227 if (1 << amortizedshift == 4) 228 vcnt = 2; 229 else if (1 << amortizedshift == 2 && lclusterbits == 12) 230 vcnt = 16; 231 else 232 return -EOPNOTSUPP; 233 234 big_pcluster = vi->z_advise & Z_EROFS_ADVISE_BIG_PCLUSTER_1; 235 encodebits = ((vcnt << amortizedshift) - sizeof(__le32)) * 8 / vcnt; 236 base = round_down(eofs, vcnt << amortizedshift); 237 in = m->kaddr + base; 238 239 i = (eofs - base) >> amortizedshift; 240 241 lo = decode_compactedbits(lclusterbits, lomask, 242 in, encodebits * i, &type); 243 m->type = type; 244 if (type == Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD) { 245 m->clusterofs = 1 << lclusterbits; 246 if (lo & Z_EROFS_VLE_DI_D0_CBLKCNT) { 247 if (!big_pcluster) { 248 DBG_BUGON(1); 249 return -EFSCORRUPTED; 250 } 251 m->compressedlcs = lo & ~Z_EROFS_VLE_DI_D0_CBLKCNT; 252 m->delta[0] = 1; 253 return 0; 254 } else if (i + 1 != (int)vcnt) { 255 m->delta[0] = lo; 256 return 0; 257 } 258 /* 259 * since the last lcluster in the pack is special, 260 * of which lo saves delta[1] rather than delta[0]. 261 * Hence, get delta[0] by the previous lcluster indirectly. 262 */ 263 lo = decode_compactedbits(lclusterbits, lomask, 264 in, encodebits * (i - 1), &type); 265 if (type != Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD) 266 lo = 0; 267 else if (lo & Z_EROFS_VLE_DI_D0_CBLKCNT) 268 lo = 1; 269 m->delta[0] = lo + 1; 270 return 0; 271 } 272 m->clusterofs = lo; 273 m->delta[0] = 0; 274 /* figout out blkaddr (pblk) for HEAD lclusters */ 275 if (!big_pcluster) { 276 nblk = 1; 277 while (i > 0) { 278 --i; 279 lo = decode_compactedbits(lclusterbits, lomask, 280 in, encodebits * i, &type); 281 if (type == Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD) 282 i -= lo; 283 284 if (i >= 0) 285 ++nblk; 286 } 287 } else { 288 nblk = 0; 289 while (i > 0) { 290 --i; 291 lo = decode_compactedbits(lclusterbits, lomask, 292 in, encodebits * i, &type); 293 if (type == Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD) { 294 if (lo & Z_EROFS_VLE_DI_D0_CBLKCNT) { 295 --i; 296 nblk += lo & ~Z_EROFS_VLE_DI_D0_CBLKCNT; 297 continue; 298 } 299 /* bigpcluster shouldn't have plain d0 == 1 */ 300 if (lo <= 1) { 301 DBG_BUGON(1); 302 return -EFSCORRUPTED; 303 } 304 i -= lo - 2; 305 continue; 306 } 307 ++nblk; 308 } 309 } 310 in += (vcnt << amortizedshift) - sizeof(__le32); 311 m->pblk = le32_to_cpu(*(__le32 *)in) + nblk; 312 return 0; 313 } 314 315 static int compacted_load_cluster_from_disk(struct z_erofs_maprecorder *m, 316 unsigned long lcn) 317 { 318 struct inode *const inode = m->inode; 319 struct erofs_inode *const vi = EROFS_I(inode); 320 const unsigned int lclusterbits = vi->z_logical_clusterbits; 321 const erofs_off_t ebase = ALIGN(iloc(EROFS_I_SB(inode), vi->nid) + 322 vi->inode_isize + vi->xattr_isize, 8) + 323 sizeof(struct z_erofs_map_header); 324 const unsigned int totalidx = DIV_ROUND_UP(inode->i_size, EROFS_BLKSIZ); 325 unsigned int compacted_4b_initial, compacted_2b; 326 unsigned int amortizedshift; 327 erofs_off_t pos; 328 int err; 329 330 if (lclusterbits != 12) 331 return -EOPNOTSUPP; 332 333 if (lcn >= totalidx) 334 return -EINVAL; 335 336 m->lcn = lcn; 337 /* used to align to 32-byte (compacted_2b) alignment */ 338 compacted_4b_initial = (32 - ebase % 32) / 4; 339 if (compacted_4b_initial == 32 / 4) 340 compacted_4b_initial = 0; 341 342 if (vi->z_advise & Z_EROFS_ADVISE_COMPACTED_2B) 343 compacted_2b = rounddown(totalidx - compacted_4b_initial, 16); 344 else 345 compacted_2b = 0; 346 347 pos = ebase; 348 if (lcn < compacted_4b_initial) { 349 amortizedshift = 2; 350 goto out; 351 } 352 pos += compacted_4b_initial * 4; 353 lcn -= compacted_4b_initial; 354 355 if (lcn < compacted_2b) { 356 amortizedshift = 1; 357 goto out; 358 } 359 pos += compacted_2b * 2; 360 lcn -= compacted_2b; 361 amortizedshift = 2; 362 out: 363 pos += lcn * (1 << amortizedshift); 364 err = z_erofs_reload_indexes(m, erofs_blknr(pos)); 365 if (err) 366 return err; 367 return unpack_compacted_index(m, amortizedshift, erofs_blkoff(pos)); 368 } 369 370 static int z_erofs_load_cluster_from_disk(struct z_erofs_maprecorder *m, 371 unsigned int lcn) 372 { 373 const unsigned int datamode = EROFS_I(m->inode)->datalayout; 374 375 if (datamode == EROFS_INODE_FLAT_COMPRESSION_LEGACY) 376 return legacy_load_cluster_from_disk(m, lcn); 377 378 if (datamode == EROFS_INODE_FLAT_COMPRESSION) 379 return compacted_load_cluster_from_disk(m, lcn); 380 381 return -EINVAL; 382 } 383 384 static int z_erofs_extent_lookback(struct z_erofs_maprecorder *m, 385 unsigned int lookback_distance) 386 { 387 struct erofs_inode *const vi = EROFS_I(m->inode); 388 struct erofs_map_blocks *const map = m->map; 389 const unsigned int lclusterbits = vi->z_logical_clusterbits; 390 unsigned long lcn = m->lcn; 391 int err; 392 393 if (lcn < lookback_distance) { 394 erofs_err(m->inode->i_sb, 395 "bogus lookback distance @ nid %llu", vi->nid); 396 DBG_BUGON(1); 397 return -EFSCORRUPTED; 398 } 399 400 /* load extent head logical cluster if needed */ 401 lcn -= lookback_distance; 402 err = z_erofs_load_cluster_from_disk(m, lcn); 403 if (err) 404 return err; 405 406 switch (m->type) { 407 case Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD: 408 if (!m->delta[0]) { 409 erofs_err(m->inode->i_sb, 410 "invalid lookback distance 0 @ nid %llu", 411 vi->nid); 412 DBG_BUGON(1); 413 return -EFSCORRUPTED; 414 } 415 return z_erofs_extent_lookback(m, m->delta[0]); 416 case Z_EROFS_VLE_CLUSTER_TYPE_PLAIN: 417 map->m_flags &= ~EROFS_MAP_ZIPPED; 418 fallthrough; 419 case Z_EROFS_VLE_CLUSTER_TYPE_HEAD: 420 map->m_la = (lcn << lclusterbits) | m->clusterofs; 421 break; 422 default: 423 erofs_err(m->inode->i_sb, 424 "unknown type %u @ lcn %lu of nid %llu", 425 m->type, lcn, vi->nid); 426 DBG_BUGON(1); 427 return -EOPNOTSUPP; 428 } 429 return 0; 430 } 431 432 static int z_erofs_get_extent_compressedlen(struct z_erofs_maprecorder *m, 433 unsigned int initial_lcn) 434 { 435 struct erofs_inode *const vi = EROFS_I(m->inode); 436 struct erofs_map_blocks *const map = m->map; 437 const unsigned int lclusterbits = vi->z_logical_clusterbits; 438 unsigned long lcn; 439 int err; 440 441 DBG_BUGON(m->type != Z_EROFS_VLE_CLUSTER_TYPE_PLAIN && 442 m->type != Z_EROFS_VLE_CLUSTER_TYPE_HEAD); 443 if (!(map->m_flags & EROFS_MAP_ZIPPED) || 444 !(vi->z_advise & Z_EROFS_ADVISE_BIG_PCLUSTER_1)) { 445 map->m_plen = 1 << lclusterbits; 446 return 0; 447 } 448 449 lcn = m->lcn + 1; 450 if (m->compressedlcs) 451 goto out; 452 453 err = z_erofs_load_cluster_from_disk(m, lcn); 454 if (err) 455 return err; 456 457 /* 458 * If the 1st NONHEAD lcluster has already been handled initially w/o 459 * valid compressedlcs, which means at least it mustn't be CBLKCNT, or 460 * an internal implemenatation error is detected. 461 * 462 * The following code can also handle it properly anyway, but let's 463 * BUG_ON in the debugging mode only for developers to notice that. 464 */ 465 DBG_BUGON(lcn == initial_lcn && 466 m->type == Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD); 467 468 switch (m->type) { 469 case Z_EROFS_VLE_CLUSTER_TYPE_PLAIN: 470 case Z_EROFS_VLE_CLUSTER_TYPE_HEAD: 471 /* 472 * if the 1st NONHEAD lcluster is actually PLAIN or HEAD type 473 * rather than CBLKCNT, it's a 1 lcluster-sized pcluster. 474 */ 475 m->compressedlcs = 1; 476 break; 477 case Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD: 478 if (m->delta[0] != 1) 479 goto err_bonus_cblkcnt; 480 if (m->compressedlcs) 481 break; 482 fallthrough; 483 default: 484 erofs_err(m->inode->i_sb, 485 "cannot found CBLKCNT @ lcn %lu of nid %llu", 486 lcn, vi->nid); 487 DBG_BUGON(1); 488 return -EFSCORRUPTED; 489 } 490 out: 491 map->m_plen = m->compressedlcs << lclusterbits; 492 return 0; 493 err_bonus_cblkcnt: 494 erofs_err(m->inode->i_sb, 495 "bogus CBLKCNT @ lcn %lu of nid %llu", 496 lcn, vi->nid); 497 DBG_BUGON(1); 498 return -EFSCORRUPTED; 499 } 500 501 int z_erofs_map_blocks_iter(struct inode *inode, 502 struct erofs_map_blocks *map, 503 int flags) 504 { 505 struct erofs_inode *const vi = EROFS_I(inode); 506 struct z_erofs_maprecorder m = { 507 .inode = inode, 508 .map = map, 509 }; 510 int err = 0; 511 unsigned int lclusterbits, endoff; 512 unsigned long initial_lcn; 513 unsigned long long ofs, end; 514 515 trace_z_erofs_map_blocks_iter_enter(inode, map, flags); 516 517 /* when trying to read beyond EOF, leave it unmapped */ 518 if (map->m_la >= inode->i_size) { 519 map->m_llen = map->m_la + 1 - inode->i_size; 520 map->m_la = inode->i_size; 521 map->m_flags = 0; 522 goto out; 523 } 524 525 err = z_erofs_fill_inode_lazy(inode); 526 if (err) 527 goto out; 528 529 lclusterbits = vi->z_logical_clusterbits; 530 ofs = map->m_la; 531 initial_lcn = ofs >> lclusterbits; 532 endoff = ofs & ((1 << lclusterbits) - 1); 533 534 err = z_erofs_load_cluster_from_disk(&m, initial_lcn); 535 if (err) 536 goto unmap_out; 537 538 map->m_flags = EROFS_MAP_ZIPPED; /* by default, compressed */ 539 end = (m.lcn + 1ULL) << lclusterbits; 540 541 switch (m.type) { 542 case Z_EROFS_VLE_CLUSTER_TYPE_PLAIN: 543 if (endoff >= m.clusterofs) 544 map->m_flags &= ~EROFS_MAP_ZIPPED; 545 fallthrough; 546 case Z_EROFS_VLE_CLUSTER_TYPE_HEAD: 547 if (endoff >= m.clusterofs) { 548 map->m_la = (m.lcn << lclusterbits) | m.clusterofs; 549 break; 550 } 551 /* m.lcn should be >= 1 if endoff < m.clusterofs */ 552 if (!m.lcn) { 553 erofs_err(inode->i_sb, 554 "invalid logical cluster 0 at nid %llu", 555 vi->nid); 556 err = -EFSCORRUPTED; 557 goto unmap_out; 558 } 559 end = (m.lcn << lclusterbits) | m.clusterofs; 560 map->m_flags |= EROFS_MAP_FULL_MAPPED; 561 m.delta[0] = 1; 562 fallthrough; 563 case Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD: 564 /* get the corresponding first chunk */ 565 err = z_erofs_extent_lookback(&m, m.delta[0]); 566 if (err) 567 goto unmap_out; 568 break; 569 default: 570 erofs_err(inode->i_sb, 571 "unknown type %u @ offset %llu of nid %llu", 572 m.type, ofs, vi->nid); 573 err = -EOPNOTSUPP; 574 goto unmap_out; 575 } 576 577 map->m_llen = end - map->m_la; 578 map->m_pa = blknr_to_addr(m.pblk); 579 map->m_flags |= EROFS_MAP_MAPPED; 580 581 err = z_erofs_get_extent_compressedlen(&m, initial_lcn); 582 if (err) 583 goto out; 584 unmap_out: 585 if (m.kaddr) 586 kunmap_atomic(m.kaddr); 587 588 out: 589 erofs_dbg("%s, m_la %llu m_pa %llu m_llen %llu m_plen %llu m_flags 0%o", 590 __func__, map->m_la, map->m_pa, 591 map->m_llen, map->m_plen, map->m_flags); 592 593 trace_z_erofs_map_blocks_iter_exit(inode, map, flags, err); 594 595 /* aggressively BUG_ON iff CONFIG_EROFS_FS_DEBUG is on */ 596 DBG_BUGON(err < 0 && err != -ENOMEM); 597 return err; 598 } 599