1 // SPDX-License-Identifier: GPL-2.0 1 2 3 #include "bcachefs.h" 4 #include "btree_key_cache.h" 5 #include "btree_write_buffer.h" 6 #include "bkey_methods.h" 7 #include "btree_update.h" 8 #include "buckets.h" 9 #include "compress.h" 10 #include "dirent.h" 11 #include "disk_accounting.h" 12 #include "error.h" 13 #include "extents.h" 14 #include "extent_update.h" 15 #include "inode.h" 16 #include "str_hash.h" 17 #include "snapshot.h" 18 #include "subvolume.h" 19 #include "varint.h" 20 21 #include <linux/random.h> 22 23 #include <asm/unaligned.h> 24 25 #define x(name, ...) #name, 26 const char * const bch2_inode_opts[] = { 27 BCH_INODE_OPTS() 28 NULL, 29 }; 30 31 static const char * const bch2_inode_flag_strs 32 BCH_INODE_FLAGS() 33 NULL 34 }; 35 #undef x 36 37 static const u8 byte_table[8] = { 1, 2, 3, 4, 38 39 static int inode_decode_field(const u8 *in, co 40 u64 out[2], unsi 41 { 42 __be64 be[2] = { 0, 0 }; 43 unsigned bytes, shift; 44 u8 *p; 45 46 if (in >= end) 47 return -1; 48 49 if (!*in) 50 return -1; 51 52 /* 53 * position of highest set bit indicat 54 * shift = number of bits to remove in 55 */ 56 shift = 8 - __fls(*in); /* 1 <= shif 57 bytes = byte_table[shift - 1]; 58 59 if (in + bytes > end) 60 return -1; 61 62 p = (u8 *) be + 16 - bytes; 63 memcpy(p, in, bytes); 64 *p ^= (1 << 8) >> shift; 65 66 out[0] = be64_to_cpu(be[0]); 67 out[1] = be64_to_cpu(be[1]); 68 *out_bits = out[0] ? 64 + fls64(out[0] 69 70 return bytes; 71 } 72 73 static inline void bch2_inode_pack_inlined(str 74 con 75 { 76 struct bkey_i_inode_v3 *k = &packed->i 77 u8 *out = k->v.fields; 78 u8 *end = (void *) &packed[1]; 79 u8 *last_nonzero_field = out; 80 unsigned nr_fields = 0, last_nonzero_f 81 unsigned bytes; 82 int ret; 83 84 bkey_inode_v3_init(&packed->inode.k_i) 85 packed->inode.k.p.offset = inod 86 packed->inode.v.bi_journal_seq = cpu_ 87 packed->inode.v.bi_hash_seed = inod 88 packed->inode.v.bi_flags = cpu_ 89 packed->inode.v.bi_sectors = cpu_ 90 packed->inode.v.bi_size = cpu_ 91 packed->inode.v.bi_version = cpu_ 92 SET_INODEv3_MODE(&packed->inode.v, ino 93 SET_INODEv3_FIELDS_START(&packed->inod 94 95 96 #define x(_name, _bits) 97 nr_fields++; 98 99 if (inode->_name) { 100 ret = bch2_varint_encode_fast( 101 out += ret; 102 103 if (_bits > 64) 104 *out++ = 0; 105 106 last_nonzero_field = out; 107 last_nonzero_fieldnr = nr_fiel 108 } else { 109 *out++ = 0; 110 111 if (_bits > 64) 112 *out++ = 0; 113 } 114 115 BCH_INODE_FIELDS_v3() 116 #undef x 117 BUG_ON(out > end); 118 119 out = last_nonzero_field; 120 nr_fields = last_nonzero_fieldnr; 121 122 bytes = out - (u8 *) &packed->inode.v; 123 set_bkey_val_bytes(&packed->inode.k, b 124 memset_u64s_tail(&packed->inode.v, 0, 125 126 SET_INODEv3_NR_FIELDS(&k->v, nr_fields 127 128 if (IS_ENABLED(CONFIG_BCACHEFS_DEBUG)) 129 struct bch_inode_unpacked unpa 130 131 ret = bch2_inode_unpack(bkey_i 132 BUG_ON(ret); 133 BUG_ON(unpacked.bi_inum 134 BUG_ON(unpacked.bi_hash_seed 135 BUG_ON(unpacked.bi_sectors 136 BUG_ON(unpacked.bi_size 137 BUG_ON(unpacked.bi_version 138 BUG_ON(unpacked.bi_mode 139 140 #define x(_name, _bits) if (unpacked._name != 141 panic("unpacked %llu s 142 (u64) unpacked._ 143 BCH_INODE_FIELDS_v3() 144 #undef x 145 } 146 } 147 148 void bch2_inode_pack(struct bkey_inode_buf *pa 149 const struct bch_inode_un 150 { 151 bch2_inode_pack_inlined(packed, inode) 152 } 153 154 static noinline int bch2_inode_unpack_v1(struc 155 struct bch_ino 156 { 157 const u8 *in = inode.v->fields; 158 const u8 *end = bkey_val_end(inode); 159 u64 field[2]; 160 unsigned fieldnr = 0, field_bits; 161 int ret; 162 163 #define x(_name, _bits) 164 if (fieldnr++ == INODE_NR_FIELDS(inode 165 unsigned offset = offsetof(str 166 memset((void *) unpacked + off 167 sizeof(*unpacked) - off 168 return 0; 169 } 170 171 ret = inode_decode_field(in, end, fiel 172 if (ret < 0) 173 return ret; 174 175 if (field_bits > sizeof(unpacked->_nam 176 return -1; 177 178 unpacked->_name = field[1]; 179 in += ret; 180 181 BCH_INODE_FIELDS_v2() 182 #undef x 183 184 /* XXX: signal if there were more fiel 185 return 0; 186 } 187 188 static int bch2_inode_unpack_v2(struct bch_ino 189 const u8 *in, 190 unsigned nr_fi 191 { 192 unsigned fieldnr = 0; 193 int ret; 194 u64 v[2]; 195 196 #define x(_name, _bits) 197 if (fieldnr < nr_fields) { 198 ret = bch2_varint_decode_fast( 199 if (ret < 0) 200 return ret; 201 in += ret; 202 203 if (_bits > 64) { 204 ret = bch2_varint_deco 205 if (ret < 0) 206 return ret; 207 in += ret; 208 } else { 209 v[1] = 0; 210 } 211 } else { 212 v[0] = v[1] = 0; 213 } 214 215 unpacked->_name = v[0]; 216 if (v[1] || v[0] != unpacked->_name) 217 return -1; 218 fieldnr++; 219 220 BCH_INODE_FIELDS_v2() 221 #undef x 222 223 /* XXX: signal if there were more fiel 224 return 0; 225 } 226 227 static int bch2_inode_unpack_v3(struct bkey_s_ 228 struct bch_ino 229 { 230 struct bkey_s_c_inode_v3 inode = bkey_ 231 const u8 *in = inode.v->fields; 232 const u8 *end = bkey_val_end(inode); 233 unsigned nr_fields = INODEv3_NR_FIELDS 234 unsigned fieldnr = 0; 235 int ret; 236 u64 v[2]; 237 238 unpacked->bi_inum = inode.k->p.o 239 unpacked->bi_journal_seq= le64_to_cpu( 240 unpacked->bi_hash_seed = inode.v->bi_ 241 unpacked->bi_flags = le64_to_cpu( 242 unpacked->bi_sectors = le64_to_cpu( 243 unpacked->bi_size = le64_to_cpu( 244 unpacked->bi_version = le64_to_cpu( 245 unpacked->bi_mode = INODEv3_MODE 246 247 #define x(_name, _bits) 248 if (fieldnr < nr_fields) { 249 ret = bch2_varint_decode_fast( 250 if (ret < 0) 251 return ret; 252 in += ret; 253 254 if (_bits > 64) { 255 ret = bch2_varint_deco 256 if (ret < 0) 257 return ret; 258 in += ret; 259 } else { 260 v[1] = 0; 261 } 262 } else { 263 v[0] = v[1] = 0; 264 } 265 266 unpacked->_name = v[0]; 267 if (v[1] || v[0] != unpacked->_name) 268 return -1; 269 fieldnr++; 270 271 BCH_INODE_FIELDS_v3() 272 #undef x 273 274 /* XXX: signal if there were more fiel 275 return 0; 276 } 277 278 static noinline int bch2_inode_unpack_slowpath 279 280 { 281 memset(unpacked, 0, sizeof(*unpacked)) 282 283 switch (k.k->type) { 284 case KEY_TYPE_inode: { 285 struct bkey_s_c_inode inode = 286 287 unpacked->bi_inum = inod 288 unpacked->bi_journal_seq= 0; 289 unpacked->bi_hash_seed = inod 290 unpacked->bi_flags = le32 291 unpacked->bi_mode = le16 292 293 if (INODE_NEW_VARINT(inode.v)) 294 return bch2_inode_unpa 295 296 297 } else { 298 return bch2_inode_unpa 299 } 300 break; 301 } 302 case KEY_TYPE_inode_v2: { 303 struct bkey_s_c_inode_v2 inode 304 305 unpacked->bi_inum = inod 306 unpacked->bi_journal_seq= le64 307 unpacked->bi_hash_seed = inod 308 unpacked->bi_flags = le64 309 unpacked->bi_mode = le16 310 311 return bch2_inode_unpack_v2(un 312 bk 313 IN 314 } 315 default: 316 BUG(); 317 } 318 } 319 320 int bch2_inode_unpack(struct bkey_s_c k, 321 struct bch_inode_unpacke 322 { 323 if (likely(k.k->type == KEY_TYPE_inode 324 return bch2_inode_unpack_v3(k, 325 return bch2_inode_unpack_slowpath(k, u 326 } 327 328 int bch2_inode_peek_nowarn(struct btree_trans 329 struct btree_iter *iter, 330 struct bch_inode_unpacked 331 subvol_inum inum, unsigned 332 { 333 struct bkey_s_c k; 334 u32 snapshot; 335 int ret; 336 337 ret = bch2_subvolume_get_snapshot(tran 338 if (ret) 339 return ret; 340 341 k = bch2_bkey_get_iter(trans, iter, BT 342 SPOS(0, inum.in 343 flags|BTREE_ITE 344 ret = bkey_err(k); 345 if (ret) 346 return ret; 347 348 ret = bkey_is_inode(k.k) ? 0 : -BCH_ER 349 if (ret) 350 goto err; 351 352 ret = bch2_inode_unpack(k, inode); 353 if (ret) 354 goto err; 355 356 return 0; 357 err: 358 bch2_trans_iter_exit(trans, iter); 359 return ret; 360 } 361 362 int bch2_inode_peek(struct btree_trans *trans, 363 struct btree_iter *iter, 364 struct bch_inode_unpacked 365 subvol_inum inum, unsigned 366 { 367 int ret = bch2_inode_peek_nowarn(trans 368 bch_err_msg(trans->c, ret, "looking up 369 return ret; 370 } 371 372 int bch2_inode_write_flags(struct btree_trans 373 struct btree_iter *iter, 374 struct bch_inode_unpacked 375 enum btree_iter_update_tr 376 { 377 struct bkey_inode_buf *inode_p; 378 379 inode_p = bch2_trans_kmalloc(trans, si 380 if (IS_ERR(inode_p)) 381 return PTR_ERR(inode_p); 382 383 bch2_inode_pack_inlined(inode_p, inode 384 inode_p->inode.k.p.snapshot = iter->sn 385 return bch2_trans_update(trans, iter, 386 } 387 388 int __bch2_fsck_write_inode(struct btree_trans 389 struct bch_inode_unpa 390 u32 snapshot) 391 { 392 struct bkey_inode_buf *inode_p = 393 bch2_trans_kmalloc(trans, size 394 395 if (IS_ERR(inode_p)) 396 return PTR_ERR(inode_p); 397 398 bch2_inode_pack(inode_p, inode); 399 inode_p->inode.k.p.snapshot = snapshot 400 401 return bch2_btree_insert_nonextent(tra 402 &inode_p->inod 403 BTREE_UPDATE_i 404 } 405 406 int bch2_fsck_write_inode(struct btree_trans * 407 struct bch_inode_u 408 u32 snapshot) 409 { 410 int ret = commit_do(trans, NULL, NULL, 411 __bch2_fsck_write_ 412 bch_err_fn(trans->c, ret); 413 return ret; 414 } 415 416 struct bkey_i *bch2_inode_to_v3(struct btree_t 417 { 418 struct bch_inode_unpacked u; 419 struct bkey_inode_buf *inode_p; 420 int ret; 421 422 if (!bkey_is_inode(&k->k)) 423 return ERR_PTR(-ENOENT); 424 425 inode_p = bch2_trans_kmalloc(trans, si 426 if (IS_ERR(inode_p)) 427 return ERR_CAST(inode_p); 428 429 ret = bch2_inode_unpack(bkey_i_to_s_c( 430 if (ret) 431 return ERR_PTR(ret); 432 433 bch2_inode_pack(inode_p, &u); 434 return &inode_p->inode.k_i; 435 } 436 437 static int __bch2_inode_validate(struct bch_fs 438 enum bch_vali 439 { 440 struct bch_inode_unpacked unpacked; 441 int ret = 0; 442 443 bkey_fsck_err_on(k.k->p.inode, 444 c, inode_pos_inode_no 445 "nonzero k.p.inode"); 446 447 bkey_fsck_err_on(k.k->p.offset < BLOCK 448 c, inode_pos_blockdev 449 "fs inode in blockdev 450 451 bkey_fsck_err_on(bch2_inode_unpack(k, 452 c, inode_unpack_error 453 "invalid variable len 454 455 bkey_fsck_err_on(unpacked.bi_data_chec 456 c, inode_checksum_typ 457 "invalid data checksu 458 unpacked.bi_data_chec 459 460 bkey_fsck_err_on(unpacked.bi_compressi 461 !bch2_compression_opt 462 c, inode_compression_ 463 "invalid compression 464 465 bkey_fsck_err_on((unpacked.bi_flags & 466 unpacked.bi_nlink != 467 c, inode_unlinked_but 468 "flagged as unlinked 469 470 bkey_fsck_err_on(unpacked.bi_subvol && 471 c, inode_subvol_root_ 472 "subvolume root but n 473 fsck_err: 474 return ret; 475 } 476 477 int bch2_inode_validate(struct bch_fs *c, stru 478 enum bch_validate_flag 479 { 480 struct bkey_s_c_inode inode = bkey_s_c 481 int ret = 0; 482 483 bkey_fsck_err_on(INODE_STR_HASH(inode. 484 c, inode_str_hash_inv 485 "invalid str hash typ 486 INODE_STR_HASH(inode. 487 488 ret = __bch2_inode_validate(c, k, flag 489 fsck_err: 490 return ret; 491 } 492 493 int bch2_inode_v2_validate(struct bch_fs *c, s 494 enum bch_validate_f 495 { 496 struct bkey_s_c_inode_v2 inode = bkey_ 497 int ret = 0; 498 499 bkey_fsck_err_on(INODEv2_STR_HASH(inod 500 c, inode_str_hash_inv 501 "invalid str hash typ 502 INODEv2_STR_HASH(inod 503 504 ret = __bch2_inode_validate(c, k, flag 505 fsck_err: 506 return ret; 507 } 508 509 int bch2_inode_v3_validate(struct bch_fs *c, s 510 enum bch_validate_f 511 { 512 struct bkey_s_c_inode_v3 inode = bkey_ 513 int ret = 0; 514 515 bkey_fsck_err_on(INODEv3_FIELDS_START( 516 INODEv3_FIELDS_START( 517 c, inode_v3_fields_st 518 "invalid fields_start 519 INODEv3_FIELDS_START( 520 INODEv3_FIELDS_START_ 521 bkey_val_u64s(inode.k 522 523 bkey_fsck_err_on(INODEv3_STR_HASH(inod 524 c, inode_str_hash_inv 525 "invalid str hash typ 526 INODEv3_STR_HASH(inod 527 528 ret = __bch2_inode_validate(c, k, flag 529 fsck_err: 530 return ret; 531 } 532 533 static void __bch2_inode_unpacked_to_text(stru 534 stru 535 { 536 prt_printf(out, "\n"); 537 printbuf_indent_add(out, 2); 538 prt_printf(out, "mode=%o\n", inode->bi 539 540 prt_str(out, "flags="); 541 prt_bitflags(out, bch2_inode_flag_strs 542 prt_printf(out, "(%x)\n", inode->bi_fl 543 544 prt_printf(out, "journal_seq=%llu\n", 545 prt_printf(out, "bi_size=%llu\n", 546 prt_printf(out, "bi_sectors=%llu\n", 547 prt_printf(out, "bi_version=%llu\n", 548 549 #define x(_name, _bits) 550 prt_printf(out, #_name "=%llu\n", (u64 551 BCH_INODE_FIELDS_v3() 552 #undef x 553 554 bch2_printbuf_strip_trailing_newline(o 555 printbuf_indent_sub(out, 2); 556 } 557 558 void bch2_inode_unpacked_to_text(struct printb 559 { 560 prt_printf(out, "inum: %llu ", inode-> 561 __bch2_inode_unpacked_to_text(out, ino 562 } 563 564 void bch2_inode_to_text(struct printbuf *out, 565 { 566 struct bch_inode_unpacked inode; 567 568 if (bch2_inode_unpack(k, &inode)) { 569 prt_printf(out, "(unpack error 570 return; 571 } 572 573 __bch2_inode_unpacked_to_text(out, &in 574 } 575 576 static inline u64 bkey_inode_flags(struct bkey 577 { 578 switch (k.k->type) { 579 case KEY_TYPE_inode: 580 return le32_to_cpu(bkey_s_c_to 581 case KEY_TYPE_inode_v2: 582 return le64_to_cpu(bkey_s_c_to 583 case KEY_TYPE_inode_v3: 584 return le64_to_cpu(bkey_s_c_to 585 default: 586 return 0; 587 } 588 } 589 590 static inline bool bkey_is_deleted_inode(struc 591 { 592 return bkey_inode_flags(k) & BCH_INODE 593 } 594 595 int bch2_trigger_inode(struct btree_trans *tra 596 enum btree_id btree_id, 597 struct bkey_s_c old, 598 struct bkey_s new, 599 enum btree_iter_update_ 600 { 601 if ((flags & BTREE_TRIGGER_atomic) && 602 BUG_ON(!trans->journal_res.seq 603 bkey_s_to_inode_v3(new).v->bi_ 604 } 605 606 s64 nr = bkey_is_inode(new.k) - bkey_i 607 if ((flags & (BTREE_TRIGGER_transactio 608 struct disk_accounting_pos acc 609 int ret = bch2_disk_accounting 610 if (ret) 611 return ret; 612 } 613 614 int deleted_delta = (int) bkey_is_ 615 (int) bkey_is_ 616 if ((flags & BTREE_TRIGGER_transaction 617 int ret = bch2_btree_bit_mod_b 618 619 if (ret) 620 return ret; 621 } 622 623 return 0; 624 } 625 626 int bch2_inode_generation_validate(struct bch_ 627 enum bch_va 628 { 629 int ret = 0; 630 631 bkey_fsck_err_on(k.k->p.inode, 632 c, inode_pos_inode_no 633 "nonzero k.p.inode"); 634 fsck_err: 635 return ret; 636 } 637 638 void bch2_inode_generation_to_text(struct prin 639 struct bkey 640 { 641 struct bkey_s_c_inode_generation gen = 642 643 prt_printf(out, "generation: %u", le32 644 } 645 646 void bch2_inode_init_early(struct bch_fs *c, 647 struct bch_inode_un 648 { 649 enum bch_str_hash_type str_hash = 650 bch2_str_hash_opt_to_type(c, c 651 652 memset(inode_u, 0, sizeof(*inode_u)); 653 654 /* ick */ 655 inode_u->bi_flags |= str_hash << INODE 656 get_random_bytes(&inode_u->bi_hash_see 657 sizeof(inode_u->bi_ha 658 } 659 660 void bch2_inode_init_late(struct bch_inode_unp 661 uid_t uid, gid_t gid 662 struct bch_inode_unp 663 { 664 inode_u->bi_mode = mode; 665 inode_u->bi_uid = uid; 666 inode_u->bi_gid = gid; 667 inode_u->bi_dev = rdev; 668 inode_u->bi_atime = now; 669 inode_u->bi_mtime = now; 670 inode_u->bi_ctime = now; 671 inode_u->bi_otime = now; 672 673 if (parent && parent->bi_mode & S_ISGI 674 inode_u->bi_gid = parent->bi_g 675 if (S_ISDIR(mode)) 676 inode_u->bi_mode |= S_ 677 } 678 679 if (parent) { 680 #define x(_name, ...) inode_u->bi_##_name = 681 BCH_INODE_OPTS() 682 #undef x 683 } 684 } 685 686 void bch2_inode_init(struct bch_fs *c, struct 687 uid_t uid, gid_t gid, umo 688 struct bch_inode_unpacked 689 { 690 bch2_inode_init_early(c, inode_u); 691 bch2_inode_init_late(inode_u, bch2_cur 692 uid, gid, mode, r 693 } 694 695 static inline u32 bkey_generation(struct bkey_ 696 { 697 switch (k.k->type) { 698 case KEY_TYPE_inode: 699 case KEY_TYPE_inode_v2: 700 BUG(); 701 case KEY_TYPE_inode_generation: 702 return le32_to_cpu(bkey_s_c_to 703 default: 704 return 0; 705 } 706 } 707 708 /* 709 * This just finds an empty slot: 710 */ 711 int bch2_inode_create(struct btree_trans *tran 712 struct btree_iter *iter, 713 struct bch_inode_unpacke 714 u32 snapshot, u64 cpu) 715 { 716 struct bch_fs *c = trans->c; 717 struct bkey_s_c k; 718 u64 min, max, start, pos, *hint; 719 int ret = 0; 720 unsigned bits = (c->opts.inodes_32bit 721 722 if (c->opts.shard_inode_numbers) { 723 bits -= c->inode_shard_bits; 724 725 min = (cpu << bits); 726 max = (cpu << bits) | ~(ULLONG 727 728 min = max_t(u64, min, BLOCKDEV 729 hint = c->unused_inode_hints + 730 } else { 731 min = BLOCKDEV_INODE_MAX; 732 max = ~(ULLONG_MAX << bits); 733 hint = c->unused_inode_hints; 734 } 735 736 start = READ_ONCE(*hint); 737 738 if (start >= max || start < min) 739 start = min; 740 741 pos = start; 742 bch2_trans_iter_init(trans, iter, BTRE 743 BTREE_ITER_all_sn 744 BTREE_ITER_intent 745 again: 746 while ((k = bch2_btree_iter_peek(iter) 747 !(ret = bkey_err(k)) && 748 bkey_lt(k.k->p, POS(0, max))) { 749 if (pos < iter->pos.offset) 750 goto found_slot; 751 752 /* 753 * We don't need to iterate ov 754 * we've found just one: 755 */ 756 pos = iter->pos.offset + 1; 757 bch2_btree_iter_set_pos(iter, 758 } 759 760 if (!ret && pos < max) 761 goto found_slot; 762 763 if (!ret && start == min) 764 ret = -BCH_ERR_ENOSPC_inode_cr 765 766 if (ret) { 767 bch2_trans_iter_exit(trans, it 768 return ret; 769 } 770 771 /* Retry from start */ 772 pos = start = min; 773 bch2_btree_iter_set_pos(iter, POS(0, p 774 goto again; 775 found_slot: 776 bch2_btree_iter_set_pos(iter, SPOS(0, 777 k = bch2_btree_iter_peek_slot(iter); 778 ret = bkey_err(k); 779 if (ret) { 780 bch2_trans_iter_exit(trans, it 781 return ret; 782 } 783 784 *hint = k.k->p.offse 785 inode_u->bi_inum = k.k->p.offse 786 inode_u->bi_generation = bkey_generat 787 return 0; 788 } 789 790 static int bch2_inode_delete_keys(struct btree 791 subvol_inum 792 { 793 struct btree_iter iter; 794 struct bkey_s_c k; 795 struct bkey_i delete; 796 struct bpos end = POS(inum.inum, U64_M 797 u32 snapshot; 798 int ret = 0; 799 800 /* 801 * We're never going to be deleting pa 802 * extent iterator: 803 */ 804 bch2_trans_iter_init(trans, &iter, id, 805 BTREE_ITER_intent 806 807 while (1) { 808 bch2_trans_begin(trans); 809 810 ret = bch2_subvolume_get_snaps 811 if (ret) 812 goto err; 813 814 bch2_btree_iter_set_snapshot(& 815 816 k = bch2_btree_iter_peek_upto( 817 ret = bkey_err(k); 818 if (ret) 819 goto err; 820 821 if (!k.k) 822 break; 823 824 bkey_init(&delete.k); 825 delete.k.p = iter.pos; 826 827 if (iter.flags & BTREE_ITER_is 828 bch2_key_resize(&delet 829 bpos_m 830 iter.p 831 832 ret = bch2_trans_update(trans, 833 bch2_trans_commit(trans, 834 BCH_TR 835 err: 836 if (ret && !bch2_err_matches(r 837 break; 838 } 839 840 bch2_trans_iter_exit(trans, &iter); 841 return ret; 842 } 843 844 int bch2_inode_rm(struct bch_fs *c, subvol_inu 845 { 846 struct btree_trans *trans = bch2_trans 847 struct btree_iter iter = { NULL }; 848 struct bkey_i_inode_generation delete; 849 struct bch_inode_unpacked inode_u; 850 struct bkey_s_c k; 851 u32 snapshot; 852 int ret; 853 854 /* 855 * If this was a directory, there shou 856 * but there could be whiteouts (from 857 * delete: 858 * 859 * XXX: the dirent could ideally would 860 * longer needed 861 */ 862 ret = bch2_inode_delete_keys(trans, 863 bch2_inode_delete_keys(trans, 864 bch2_inode_delete_keys(trans, 865 if (ret) 866 goto err; 867 retry: 868 bch2_trans_begin(trans); 869 870 ret = bch2_subvolume_get_snapshot(tran 871 if (ret) 872 goto err; 873 874 k = bch2_bkey_get_iter(trans, &iter, B 875 SPOS(0, inum.in 876 BTREE_ITER_inte 877 ret = bkey_err(k); 878 if (ret) 879 goto err; 880 881 if (!bkey_is_inode(k.k)) { 882 bch2_fs_inconsistent(c, 883 "inode %l 884 inum.inum 885 ret = -EIO; 886 goto err; 887 } 888 889 bch2_inode_unpack(k, &inode_u); 890 891 bkey_inode_generation_init(&delete.k_i 892 delete.k.p = iter.pos; 893 delete.v.bi_generation = cpu_to_le32(i 894 895 ret = bch2_trans_update(trans, &iter 896 bch2_trans_commit(trans, NULL, 897 BCH_TRANS_COMM 898 err: 899 bch2_trans_iter_exit(trans, &iter); 900 if (bch2_err_matches(ret, BCH_ERR_tran 901 goto retry; 902 903 bch2_trans_put(trans); 904 return ret; 905 } 906 907 int bch2_inode_find_by_inum_nowarn_trans(struc 908 subvol_inum 909 struct bch_i 910 { 911 struct btree_iter iter; 912 int ret; 913 914 ret = bch2_inode_peek_nowarn(trans, &i 915 if (!ret) 916 bch2_trans_iter_exit(trans, &i 917 return ret; 918 } 919 920 int bch2_inode_find_by_inum_trans(struct btree 921 subvol_inum 922 struct bch_i 923 { 924 struct btree_iter iter; 925 int ret; 926 927 ret = bch2_inode_peek(trans, &iter, in 928 if (!ret) 929 bch2_trans_iter_exit(trans, &i 930 return ret; 931 } 932 933 int bch2_inode_find_by_inum(struct bch_fs *c, 934 struct bch_inode_u 935 { 936 return bch2_trans_do(c, NULL, NULL, 0, 937 bch2_inode_find_by_inum_trans( 938 } 939 940 int bch2_inode_nlink_inc(struct bch_inode_unpa 941 { 942 if (bi->bi_flags & BCH_INODE_unlinked) 943 bi->bi_flags &= ~BCH_INODE_unl 944 else { 945 if (bi->bi_nlink == U32_MAX) 946 return -EINVAL; 947 948 bi->bi_nlink++; 949 } 950 951 return 0; 952 } 953 954 void bch2_inode_nlink_dec(struct btree_trans * 955 { 956 if (bi->bi_nlink && (bi->bi_flags & BC 957 bch2_trans_inconsistent(trans, 958 bi->bi 959 return; 960 } 961 962 if (bi->bi_flags & BCH_INODE_unlinked) 963 bch2_trans_inconsistent(trans, 964 return; 965 } 966 967 if (bi->bi_nlink) 968 bi->bi_nlink--; 969 else 970 bi->bi_flags |= BCH_INODE_unli 971 } 972 973 struct bch_opts bch2_inode_opts_to_opts(struct 974 { 975 struct bch_opts ret = { 0 }; 976 #define x(_name, _bits) 977 if (inode->bi_##_name) 978 opt_set(ret, _name, inode->bi_ 979 BCH_INODE_OPTS() 980 #undef x 981 return ret; 982 } 983 984 void bch2_inode_opts_get(struct bch_io_opts *o 985 struct bch_inode_unpa 986 { 987 #define x(_name, _bits) opts->_name = 988 BCH_INODE_OPTS() 989 #undef x 990 991 if (opts->nocow) 992 opts->compression = opts->back 993 } 994 995 int bch2_inum_opts_get(struct btree_trans *tra 996 { 997 struct bch_inode_unpacked inode; 998 int ret = lockrestart_do(trans, bch2_i 999 1000 if (ret) 1001 return ret; 1002 1003 bch2_inode_opts_get(opts, trans->c, & 1004 return 0; 1005 } 1006 1007 int bch2_inode_rm_snapshot(struct btree_trans 1008 { 1009 struct bch_fs *c = trans->c; 1010 struct btree_iter iter = { NULL }; 1011 struct bkey_i_inode_generation delete 1012 struct bch_inode_unpacked inode_u; 1013 struct bkey_s_c k; 1014 int ret; 1015 1016 do { 1017 ret = bch2_btree_delete_ran 1018 1019 1020 1021 bch2_btree_delete_ran 1022 1023 1024 1025 bch2_btree_delete_ran 1026 1027 1028 1029 } while (ret == -BCH_ERR_transaction_ 1030 if (ret) 1031 goto err; 1032 retry: 1033 bch2_trans_begin(trans); 1034 1035 k = bch2_bkey_get_iter(trans, &iter, 1036 SPOS(0, inum, 1037 ret = bkey_err(k); 1038 if (ret) 1039 goto err; 1040 1041 if (!bkey_is_inode(k.k)) { 1042 bch2_fs_inconsistent(c, 1043 "inode % 1044 inum, sn 1045 ret = -EIO; 1046 goto err; 1047 } 1048 1049 bch2_inode_unpack(k, &inode_u); 1050 1051 /* Subvolume root? */ 1052 if (inode_u.bi_subvol) 1053 bch_warn(c, "deleting inode % 1054 1055 bkey_inode_generation_init(&delete.k_ 1056 delete.k.p = iter.pos; 1057 delete.v.bi_generation = cpu_to_le32( 1058 1059 ret = bch2_trans_update(trans, &ite 1060 bch2_trans_commit(trans, NULL 1061 BCH_TRANS_COM 1062 err: 1063 bch2_trans_iter_exit(trans, &iter); 1064 if (bch2_err_matches(ret, BCH_ERR_tra 1065 goto retry; 1066 1067 return ret ?: -BCH_ERR_transaction_re 1068 } 1069 1070 static int may_delete_deleted_inode(struct bt 1071 struct bt 1072 struct bp 1073 bool *nee 1074 { 1075 struct bch_fs *c = trans->c; 1076 struct btree_iter inode_iter; 1077 struct bkey_s_c k; 1078 struct bch_inode_unpacked inode; 1079 int ret; 1080 1081 k = bch2_bkey_get_iter(trans, &inode_ 1082 ret = bkey_err(k); 1083 if (ret) 1084 return ret; 1085 1086 ret = bkey_is_inode(k.k) ? 0 : -BCH_E 1087 if (fsck_err_on(!bkey_is_inode(k.k), 1088 trans, deleted_inode_ 1089 "nonexistent inode %l 1090 pos.offset, pos.snaps 1091 goto delete; 1092 1093 ret = bch2_inode_unpack(k, &inode); 1094 if (ret) 1095 goto out; 1096 1097 if (S_ISDIR(inode.bi_mode)) { 1098 ret = bch2_empty_dir_snapshot 1099 if (fsck_err_on(bch2_err_matc 1100 trans, delete 1101 "non empty di 1102 pos.offset, p 1103 goto delete; 1104 if (ret) 1105 goto out; 1106 } 1107 1108 if (fsck_err_on(!(inode.bi_flags & BC 1109 trans, deleted_inode_ 1110 "non-deleted inode %l 1111 pos.offset, pos.snaps 1112 goto delete; 1113 1114 if (c->sb.clean && 1115 !fsck_err(trans, deleted_inode_bu 1116 "filesystem marked as c 1117 pos.offset, pos.snapsho 1118 ret = 0; 1119 goto out; 1120 } 1121 1122 if (bch2_snapshot_is_internal_node(c, 1123 struct bpos new_min_pos; 1124 1125 ret = bch2_propagate_key_to_s 1126 if (ret) 1127 goto out; 1128 1129 inode.bi_flags &= ~BCH_INODE_ 1130 1131 ret = bch2_inode_write_flags( 1132 1133 bch_err_msg(c, ret, "clearing 1134 if (ret) 1135 goto out; 1136 1137 /* 1138 * We'll need another write b 1139 * unlinked inodes in the sna 1140 */ 1141 *need_another_pass = true; 1142 goto out; 1143 } 1144 1145 ret = 1; 1146 out: 1147 fsck_err: 1148 bch2_trans_iter_exit(trans, &inode_it 1149 return ret; 1150 delete: 1151 ret = bch2_btree_bit_mod_buffered(tra 1152 goto out; 1153 } 1154 1155 int bch2_delete_dead_inodes(struct bch_fs *c) 1156 { 1157 struct btree_trans *trans = bch2_tran 1158 bool need_another_pass; 1159 int ret; 1160 again: 1161 /* 1162 * if we ran check_inodes() unlinked 1163 * cleaned up but the write buffer wi 1164 * alway need a write buffer flush 1165 */ 1166 ret = bch2_btree_write_buffer_flush_s 1167 if (ret) 1168 goto err; 1169 1170 need_another_pass = false; 1171 1172 /* 1173 * Weird transaction restart handling 1174 * bch2_inode_rm_snapshot() will retu 1175 * but we can't retry because the btr 1176 * flushed and we'd spin: 1177 */ 1178 ret = for_each_btree_key_commit(trans 1179 BTREE 1180 NULL, 1181 ret = may_delete_deleted_inod 1182 if (ret > 0) { 1183 bch_verbose(c, "delet 1184 1185 ret = bch2_inode_rm_s 1186 /* 1187 * We don't want to l 1188 * error here means w 1189 * we're actually don 1190 * same key because t 1191 * yet 1192 */ 1193 if (bch2_err_matches( 1194 ret = 0; 1195 continue; 1196 } 1197 } 1198 1199 ret; 1200 })); 1201 1202 if (!ret && need_another_pass) 1203 goto again; 1204 err: 1205 bch2_trans_put(trans); 1206 return ret; 1207 } 1208
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.