1 // SPDX-License-Identifier: GPL-2.0
4 #include "btree_key_cache.h"
5 #include "btree_write_buffer.h"
6 #include "bkey_methods.h"
7 #include "btree_update.h"
12 #include "extent_update.h"
16 #include "subvolume.h"
19 #include <linux/random.h>
21 #include <asm/unaligned.h>
23 #define x(name, ...) #name,
24 const char * const bch2_inode_opts[] = {
29 static const char * const bch2_inode_flag_strs[] = {
35 static const u8 byte_table[8] = { 1, 2, 3, 4, 6, 8, 10, 13 };
37 static int inode_decode_field(const u8 *in, const u8 *end,
38 u64 out[2], unsigned *out_bits)
40 __be64 be[2] = { 0, 0 };
41 unsigned bytes, shift;
51 * position of highest set bit indicates number of bytes:
52 * shift = number of bits to remove in high byte:
54 shift = 8 - __fls(*in); /* 1 <= shift <= 8 */
55 bytes = byte_table[shift - 1];
60 p = (u8 *) be + 16 - bytes;
62 *p ^= (1 << 8) >> shift;
64 out[0] = be64_to_cpu(be[0]);
65 out[1] = be64_to_cpu(be[1]);
66 *out_bits = out[0] ? 64 + fls64(out[0]) : fls64(out[1]);
71 static inline void bch2_inode_pack_inlined(struct bkey_inode_buf *packed,
72 const struct bch_inode_unpacked *inode)
74 struct bkey_i_inode_v3 *k = &packed->inode;
75 u8 *out = k->v.fields;
76 u8 *end = (void *) &packed[1];
77 u8 *last_nonzero_field = out;
78 unsigned nr_fields = 0, last_nonzero_fieldnr = 0;
82 bkey_inode_v3_init(&packed->inode.k_i);
83 packed->inode.k.p.offset = inode->bi_inum;
84 packed->inode.v.bi_journal_seq = cpu_to_le64(inode->bi_journal_seq);
85 packed->inode.v.bi_hash_seed = inode->bi_hash_seed;
86 packed->inode.v.bi_flags = cpu_to_le64(inode->bi_flags);
87 packed->inode.v.bi_sectors = cpu_to_le64(inode->bi_sectors);
88 packed->inode.v.bi_size = cpu_to_le64(inode->bi_size);
89 packed->inode.v.bi_version = cpu_to_le64(inode->bi_version);
90 SET_INODEv3_MODE(&packed->inode.v, inode->bi_mode);
91 SET_INODEv3_FIELDS_START(&packed->inode.v, INODEv3_FIELDS_START_CUR);
94 #define x(_name, _bits) \
98 ret = bch2_varint_encode_fast(out, inode->_name); \
104 last_nonzero_field = out; \
105 last_nonzero_fieldnr = nr_fields; \
113 BCH_INODE_FIELDS_v3()
117 out = last_nonzero_field;
118 nr_fields = last_nonzero_fieldnr;
120 bytes = out - (u8 *) &packed->inode.v;
121 set_bkey_val_bytes(&packed->inode.k, bytes);
122 memset_u64s_tail(&packed->inode.v, 0, bytes);
124 SET_INODEv3_NR_FIELDS(&k->v, nr_fields);
126 if (IS_ENABLED(CONFIG_BCACHEFS_DEBUG)) {
127 struct bch_inode_unpacked unpacked;
129 ret = bch2_inode_unpack(bkey_i_to_s_c(&packed->inode.k_i), &unpacked);
131 BUG_ON(unpacked.bi_inum != inode->bi_inum);
132 BUG_ON(unpacked.bi_hash_seed != inode->bi_hash_seed);
133 BUG_ON(unpacked.bi_sectors != inode->bi_sectors);
134 BUG_ON(unpacked.bi_size != inode->bi_size);
135 BUG_ON(unpacked.bi_version != inode->bi_version);
136 BUG_ON(unpacked.bi_mode != inode->bi_mode);
138 #define x(_name, _bits) if (unpacked._name != inode->_name) \
139 panic("unpacked %llu should be %llu", \
140 (u64) unpacked._name, (u64) inode->_name);
141 BCH_INODE_FIELDS_v3()
146 void bch2_inode_pack(struct bkey_inode_buf *packed,
147 const struct bch_inode_unpacked *inode)
149 bch2_inode_pack_inlined(packed, inode);
152 static noinline int bch2_inode_unpack_v1(struct bkey_s_c_inode inode,
153 struct bch_inode_unpacked *unpacked)
155 const u8 *in = inode.v->fields;
156 const u8 *end = bkey_val_end(inode);
158 unsigned fieldnr = 0, field_bits;
161 #define x(_name, _bits) \
162 if (fieldnr++ == INODE_NR_FIELDS(inode.v)) { \
163 unsigned offset = offsetof(struct bch_inode_unpacked, _name);\
164 memset((void *) unpacked + offset, 0, \
165 sizeof(*unpacked) - offset); \
169 ret = inode_decode_field(in, end, field, &field_bits); \
173 if (field_bits > sizeof(unpacked->_name) * 8) \
176 unpacked->_name = field[1]; \
179 BCH_INODE_FIELDS_v2()
182 /* XXX: signal if there were more fields than expected? */
186 static int bch2_inode_unpack_v2(struct bch_inode_unpacked *unpacked,
187 const u8 *in, const u8 *end,
190 unsigned fieldnr = 0;
194 #define x(_name, _bits) \
195 if (fieldnr < nr_fields) { \
196 ret = bch2_varint_decode_fast(in, end, &v[0]); \
202 ret = bch2_varint_decode_fast(in, end, &v[1]); \
213 unpacked->_name = v[0]; \
214 if (v[1] || v[0] != unpacked->_name) \
218 BCH_INODE_FIELDS_v2()
221 /* XXX: signal if there were more fields than expected? */
225 static int bch2_inode_unpack_v3(struct bkey_s_c k,
226 struct bch_inode_unpacked *unpacked)
228 struct bkey_s_c_inode_v3 inode = bkey_s_c_to_inode_v3(k);
229 const u8 *in = inode.v->fields;
230 const u8 *end = bkey_val_end(inode);
231 unsigned nr_fields = INODEv3_NR_FIELDS(inode.v);
232 unsigned fieldnr = 0;
236 unpacked->bi_inum = inode.k->p.offset;
237 unpacked->bi_journal_seq= le64_to_cpu(inode.v->bi_journal_seq);
238 unpacked->bi_hash_seed = inode.v->bi_hash_seed;
239 unpacked->bi_flags = le64_to_cpu(inode.v->bi_flags);
240 unpacked->bi_sectors = le64_to_cpu(inode.v->bi_sectors);
241 unpacked->bi_size = le64_to_cpu(inode.v->bi_size);
242 unpacked->bi_version = le64_to_cpu(inode.v->bi_version);
243 unpacked->bi_mode = INODEv3_MODE(inode.v);
245 #define x(_name, _bits) \
246 if (fieldnr < nr_fields) { \
247 ret = bch2_varint_decode_fast(in, end, &v[0]); \
253 ret = bch2_varint_decode_fast(in, end, &v[1]); \
264 unpacked->_name = v[0]; \
265 if (v[1] || v[0] != unpacked->_name) \
269 BCH_INODE_FIELDS_v3()
272 /* XXX: signal if there were more fields than expected? */
276 static noinline int bch2_inode_unpack_slowpath(struct bkey_s_c k,
277 struct bch_inode_unpacked *unpacked)
279 memset(unpacked, 0, sizeof(*unpacked));
282 case KEY_TYPE_inode: {
283 struct bkey_s_c_inode inode = bkey_s_c_to_inode(k);
285 unpacked->bi_inum = inode.k->p.offset;
286 unpacked->bi_journal_seq= 0;
287 unpacked->bi_hash_seed = inode.v->bi_hash_seed;
288 unpacked->bi_flags = le32_to_cpu(inode.v->bi_flags);
289 unpacked->bi_mode = le16_to_cpu(inode.v->bi_mode);
291 if (INODE_NEW_VARINT(inode.v)) {
292 return bch2_inode_unpack_v2(unpacked, inode.v->fields,
294 INODE_NR_FIELDS(inode.v));
296 return bch2_inode_unpack_v1(inode, unpacked);
300 case KEY_TYPE_inode_v2: {
301 struct bkey_s_c_inode_v2 inode = bkey_s_c_to_inode_v2(k);
303 unpacked->bi_inum = inode.k->p.offset;
304 unpacked->bi_journal_seq= le64_to_cpu(inode.v->bi_journal_seq);
305 unpacked->bi_hash_seed = inode.v->bi_hash_seed;
306 unpacked->bi_flags = le64_to_cpu(inode.v->bi_flags);
307 unpacked->bi_mode = le16_to_cpu(inode.v->bi_mode);
309 return bch2_inode_unpack_v2(unpacked, inode.v->fields,
311 INODEv2_NR_FIELDS(inode.v));
318 int bch2_inode_unpack(struct bkey_s_c k,
319 struct bch_inode_unpacked *unpacked)
321 if (likely(k.k->type == KEY_TYPE_inode_v3))
322 return bch2_inode_unpack_v3(k, unpacked);
323 return bch2_inode_unpack_slowpath(k, unpacked);
326 static int bch2_inode_peek_nowarn(struct btree_trans *trans,
327 struct btree_iter *iter,
328 struct bch_inode_unpacked *inode,
329 subvol_inum inum, unsigned flags)
335 ret = bch2_subvolume_get_snapshot(trans, inum.subvol, &snapshot);
339 k = bch2_bkey_get_iter(trans, iter, BTREE_ID_inodes,
340 SPOS(0, inum.inum, snapshot),
341 flags|BTREE_ITER_CACHED);
346 ret = bkey_is_inode(k.k) ? 0 : -BCH_ERR_ENOENT_inode;
350 ret = bch2_inode_unpack(k, inode);
356 bch2_trans_iter_exit(trans, iter);
360 int bch2_inode_peek(struct btree_trans *trans,
361 struct btree_iter *iter,
362 struct bch_inode_unpacked *inode,
363 subvol_inum inum, unsigned flags)
365 int ret = bch2_inode_peek_nowarn(trans, iter, inode, inum, flags);
366 bch_err_msg(trans->c, ret, "looking up inum %u:%llu:", inum.subvol, inum.inum);
370 int bch2_inode_write_flags(struct btree_trans *trans,
371 struct btree_iter *iter,
372 struct bch_inode_unpacked *inode,
373 enum btree_update_flags flags)
375 struct bkey_inode_buf *inode_p;
377 inode_p = bch2_trans_kmalloc(trans, sizeof(*inode_p));
379 return PTR_ERR(inode_p);
381 bch2_inode_pack_inlined(inode_p, inode);
382 inode_p->inode.k.p.snapshot = iter->snapshot;
383 return bch2_trans_update(trans, iter, &inode_p->inode.k_i, flags);
386 struct bkey_i *bch2_inode_to_v3(struct btree_trans *trans, struct bkey_i *k)
388 struct bch_inode_unpacked u;
389 struct bkey_inode_buf *inode_p;
392 if (!bkey_is_inode(&k->k))
393 return ERR_PTR(-ENOENT);
395 inode_p = bch2_trans_kmalloc(trans, sizeof(*inode_p));
397 return ERR_CAST(inode_p);
399 ret = bch2_inode_unpack(bkey_i_to_s_c(k), &u);
403 bch2_inode_pack(inode_p, &u);
404 return &inode_p->inode.k_i;
407 static int __bch2_inode_invalid(struct bch_fs *c, struct bkey_s_c k, struct printbuf *err)
409 struct bch_inode_unpacked unpacked;
412 bkey_fsck_err_on(k.k->p.inode, c, err,
413 inode_pos_inode_nonzero,
414 "nonzero k.p.inode");
416 bkey_fsck_err_on(k.k->p.offset < BLOCKDEV_INODE_MAX, c, err,
417 inode_pos_blockdev_range,
418 "fs inode in blockdev range");
420 bkey_fsck_err_on(bch2_inode_unpack(k, &unpacked), c, err,
422 "invalid variable length fields");
424 bkey_fsck_err_on(unpacked.bi_data_checksum >= BCH_CSUM_OPT_NR + 1, c, err,
425 inode_checksum_type_invalid,
426 "invalid data checksum type (%u >= %u",
427 unpacked.bi_data_checksum, BCH_CSUM_OPT_NR + 1);
429 bkey_fsck_err_on(unpacked.bi_compression &&
430 !bch2_compression_opt_valid(unpacked.bi_compression - 1), c, err,
431 inode_compression_type_invalid,
432 "invalid compression opt %u", unpacked.bi_compression - 1);
434 bkey_fsck_err_on((unpacked.bi_flags & BCH_INODE_unlinked) &&
435 unpacked.bi_nlink != 0, c, err,
436 inode_unlinked_but_nlink_nonzero,
437 "flagged as unlinked but bi_nlink != 0");
439 bkey_fsck_err_on(unpacked.bi_subvol && !S_ISDIR(unpacked.bi_mode), c, err,
440 inode_subvol_root_but_not_dir,
441 "subvolume root but not a directory");
446 int bch2_inode_invalid(struct bch_fs *c, struct bkey_s_c k,
447 enum bkey_invalid_flags flags,
448 struct printbuf *err)
450 struct bkey_s_c_inode inode = bkey_s_c_to_inode(k);
453 bkey_fsck_err_on(INODE_STR_HASH(inode.v) >= BCH_STR_HASH_NR, c, err,
454 inode_str_hash_invalid,
455 "invalid str hash type (%llu >= %u)",
456 INODE_STR_HASH(inode.v), BCH_STR_HASH_NR);
458 ret = __bch2_inode_invalid(c, k, err);
463 int bch2_inode_v2_invalid(struct bch_fs *c, struct bkey_s_c k,
464 enum bkey_invalid_flags flags,
465 struct printbuf *err)
467 struct bkey_s_c_inode_v2 inode = bkey_s_c_to_inode_v2(k);
470 bkey_fsck_err_on(INODEv2_STR_HASH(inode.v) >= BCH_STR_HASH_NR, c, err,
471 inode_str_hash_invalid,
472 "invalid str hash type (%llu >= %u)",
473 INODEv2_STR_HASH(inode.v), BCH_STR_HASH_NR);
475 ret = __bch2_inode_invalid(c, k, err);
480 int bch2_inode_v3_invalid(struct bch_fs *c, struct bkey_s_c k,
481 enum bkey_invalid_flags flags,
482 struct printbuf *err)
484 struct bkey_s_c_inode_v3 inode = bkey_s_c_to_inode_v3(k);
487 bkey_fsck_err_on(INODEv3_FIELDS_START(inode.v) < INODEv3_FIELDS_START_INITIAL ||
488 INODEv3_FIELDS_START(inode.v) > bkey_val_u64s(inode.k), c, err,
489 inode_v3_fields_start_bad,
490 "invalid fields_start (got %llu, min %u max %zu)",
491 INODEv3_FIELDS_START(inode.v),
492 INODEv3_FIELDS_START_INITIAL,
493 bkey_val_u64s(inode.k));
495 bkey_fsck_err_on(INODEv3_STR_HASH(inode.v) >= BCH_STR_HASH_NR, c, err,
496 inode_str_hash_invalid,
497 "invalid str hash type (%llu >= %u)",
498 INODEv3_STR_HASH(inode.v), BCH_STR_HASH_NR);
500 ret = __bch2_inode_invalid(c, k, err);
505 static void __bch2_inode_unpacked_to_text(struct printbuf *out,
506 struct bch_inode_unpacked *inode)
508 prt_printf(out, "mode=%o ", inode->bi_mode);
510 prt_str(out, "flags=");
511 prt_bitflags(out, bch2_inode_flag_strs, inode->bi_flags & ((1U << 20) - 1));
512 prt_printf(out, " (%x)", inode->bi_flags);
514 prt_printf(out, " journal_seq=%llu bi_size=%llu bi_sectors=%llu bi_version=%llu",
515 inode->bi_journal_seq,
520 #define x(_name, _bits) \
521 prt_printf(out, " "#_name "=%llu", (u64) inode->_name);
522 BCH_INODE_FIELDS_v3()
526 void bch2_inode_unpacked_to_text(struct printbuf *out, struct bch_inode_unpacked *inode)
528 prt_printf(out, "inum: %llu ", inode->bi_inum);
529 __bch2_inode_unpacked_to_text(out, inode);
532 void bch2_inode_to_text(struct printbuf *out, struct bch_fs *c, struct bkey_s_c k)
534 struct bch_inode_unpacked inode;
536 if (bch2_inode_unpack(k, &inode)) {
537 prt_printf(out, "(unpack error)");
541 __bch2_inode_unpacked_to_text(out, &inode);
544 static inline u64 bkey_inode_flags(struct bkey_s_c k)
548 return le32_to_cpu(bkey_s_c_to_inode(k).v->bi_flags);
549 case KEY_TYPE_inode_v2:
550 return le64_to_cpu(bkey_s_c_to_inode_v2(k).v->bi_flags);
551 case KEY_TYPE_inode_v3:
552 return le64_to_cpu(bkey_s_c_to_inode_v3(k).v->bi_flags);
558 static inline bool bkey_is_deleted_inode(struct bkey_s_c k)
560 return bkey_inode_flags(k) & BCH_INODE_unlinked;
563 int bch2_trans_mark_inode(struct btree_trans *trans,
564 enum btree_id btree_id, unsigned level,
569 int nr = bkey_is_inode(&new->k) - bkey_is_inode(old.k);
570 bool old_deleted = bkey_is_deleted_inode(old);
571 bool new_deleted = bkey_is_deleted_inode(bkey_i_to_s_c(new));
574 int ret = bch2_replicas_deltas_realloc(trans, 0);
575 struct replicas_delta_list *d = trans->fs_usage_deltas;
583 if (old_deleted != new_deleted) {
584 int ret = bch2_btree_bit_mod(trans, BTREE_ID_deleted_inodes, new->k.p, new_deleted);
592 int bch2_mark_inode(struct btree_trans *trans,
593 enum btree_id btree_id, unsigned level,
594 struct bkey_s_c old, struct bkey_s_c new,
597 struct bch_fs *c = trans->c;
598 struct bch_fs_usage *fs_usage;
599 u64 journal_seq = trans->journal_res.seq;
601 if (flags & BTREE_TRIGGER_INSERT) {
602 struct bch_inode_v3 *v = (struct bch_inode_v3 *) new.v;
604 BUG_ON(!journal_seq);
605 BUG_ON(new.k->type != KEY_TYPE_inode_v3);
607 v->bi_journal_seq = cpu_to_le64(journal_seq);
610 if (flags & BTREE_TRIGGER_GC) {
611 percpu_down_read(&c->mark_lock);
614 fs_usage = fs_usage_ptr(c, journal_seq, flags & BTREE_TRIGGER_GC);
615 fs_usage->nr_inodes += bkey_is_inode(new.k);
616 fs_usage->nr_inodes -= bkey_is_inode(old.k);
619 percpu_up_read(&c->mark_lock);
624 int bch2_inode_generation_invalid(struct bch_fs *c, struct bkey_s_c k,
625 enum bkey_invalid_flags flags,
626 struct printbuf *err)
630 bkey_fsck_err_on(k.k->p.inode, c, err,
631 inode_pos_inode_nonzero,
632 "nonzero k.p.inode");
637 void bch2_inode_generation_to_text(struct printbuf *out, struct bch_fs *c,
640 struct bkey_s_c_inode_generation gen = bkey_s_c_to_inode_generation(k);
642 prt_printf(out, "generation: %u", le32_to_cpu(gen.v->bi_generation));
645 void bch2_inode_init_early(struct bch_fs *c,
646 struct bch_inode_unpacked *inode_u)
648 enum bch_str_hash_type str_hash =
649 bch2_str_hash_opt_to_type(c, c->opts.str_hash);
651 memset(inode_u, 0, sizeof(*inode_u));
654 inode_u->bi_flags |= str_hash << INODE_STR_HASH_OFFSET;
655 get_random_bytes(&inode_u->bi_hash_seed,
656 sizeof(inode_u->bi_hash_seed));
659 void bch2_inode_init_late(struct bch_inode_unpacked *inode_u, u64 now,
660 uid_t uid, gid_t gid, umode_t mode, dev_t rdev,
661 struct bch_inode_unpacked *parent)
663 inode_u->bi_mode = mode;
664 inode_u->bi_uid = uid;
665 inode_u->bi_gid = gid;
666 inode_u->bi_dev = rdev;
667 inode_u->bi_atime = now;
668 inode_u->bi_mtime = now;
669 inode_u->bi_ctime = now;
670 inode_u->bi_otime = now;
672 if (parent && parent->bi_mode & S_ISGID) {
673 inode_u->bi_gid = parent->bi_gid;
675 inode_u->bi_mode |= S_ISGID;
679 #define x(_name, ...) inode_u->bi_##_name = parent->bi_##_name;
685 void bch2_inode_init(struct bch_fs *c, struct bch_inode_unpacked *inode_u,
686 uid_t uid, gid_t gid, umode_t mode, dev_t rdev,
687 struct bch_inode_unpacked *parent)
689 bch2_inode_init_early(c, inode_u);
690 bch2_inode_init_late(inode_u, bch2_current_time(c),
691 uid, gid, mode, rdev, parent);
694 static inline u32 bkey_generation(struct bkey_s_c k)
698 case KEY_TYPE_inode_v2:
700 case KEY_TYPE_inode_generation:
701 return le32_to_cpu(bkey_s_c_to_inode_generation(k).v->bi_generation);
708 * This just finds an empty slot:
710 int bch2_inode_create(struct btree_trans *trans,
711 struct btree_iter *iter,
712 struct bch_inode_unpacked *inode_u,
713 u32 snapshot, u64 cpu)
715 struct bch_fs *c = trans->c;
717 u64 min, max, start, pos, *hint;
719 unsigned bits = (c->opts.inodes_32bit ? 31 : 63);
721 if (c->opts.shard_inode_numbers) {
722 bits -= c->inode_shard_bits;
725 max = (cpu << bits) | ~(ULLONG_MAX << bits);
727 min = max_t(u64, min, BLOCKDEV_INODE_MAX);
728 hint = c->unused_inode_hints + cpu;
730 min = BLOCKDEV_INODE_MAX;
731 max = ~(ULLONG_MAX << bits);
732 hint = c->unused_inode_hints;
735 start = READ_ONCE(*hint);
737 if (start >= max || start < min)
741 bch2_trans_iter_init(trans, iter, BTREE_ID_inodes, POS(0, pos),
742 BTREE_ITER_ALL_SNAPSHOTS|
745 while ((k = bch2_btree_iter_peek(iter)).k &&
746 !(ret = bkey_err(k)) &&
747 bkey_lt(k.k->p, POS(0, max))) {
748 if (pos < iter->pos.offset)
752 * We don't need to iterate over keys in every snapshot once
753 * we've found just one:
755 pos = iter->pos.offset + 1;
756 bch2_btree_iter_set_pos(iter, POS(0, pos));
759 if (!ret && pos < max)
762 if (!ret && start == min)
763 ret = -BCH_ERR_ENOSPC_inode_create;
766 bch2_trans_iter_exit(trans, iter);
770 /* Retry from start */
772 bch2_btree_iter_set_pos(iter, POS(0, pos));
775 bch2_btree_iter_set_pos(iter, SPOS(0, pos, snapshot));
776 k = bch2_btree_iter_peek_slot(iter);
779 bch2_trans_iter_exit(trans, iter);
783 *hint = k.k->p.offset;
784 inode_u->bi_inum = k.k->p.offset;
785 inode_u->bi_generation = bkey_generation(k);
789 static int bch2_inode_delete_keys(struct btree_trans *trans,
790 subvol_inum inum, enum btree_id id)
792 struct btree_iter iter;
794 struct bkey_i delete;
795 struct bpos end = POS(inum.inum, U64_MAX);
800 * We're never going to be deleting partial extents, no need to use an
803 bch2_trans_iter_init(trans, &iter, id, POS(inum.inum, 0),
807 bch2_trans_begin(trans);
809 ret = bch2_subvolume_get_snapshot(trans, inum.subvol, &snapshot);
813 bch2_btree_iter_set_snapshot(&iter, snapshot);
815 k = bch2_btree_iter_peek_upto(&iter, end);
823 bkey_init(&delete.k);
824 delete.k.p = iter.pos;
826 if (iter.flags & BTREE_ITER_IS_EXTENTS)
827 bch2_key_resize(&delete.k,
828 bpos_min(end, k.k->p).offset -
831 ret = bch2_trans_update(trans, &iter, &delete, 0) ?:
832 bch2_trans_commit(trans, NULL, NULL,
833 BCH_TRANS_COMMIT_no_enospc);
835 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
839 bch2_trans_iter_exit(trans, &iter);
843 int bch2_inode_rm(struct bch_fs *c, subvol_inum inum)
845 struct btree_trans *trans = bch2_trans_get(c);
846 struct btree_iter iter = { NULL };
847 struct bkey_i_inode_generation delete;
848 struct bch_inode_unpacked inode_u;
854 * If this was a directory, there shouldn't be any real dirents left -
855 * but there could be whiteouts (from hash collisions) that we should
858 * XXX: the dirent could ideally would delete whiteouts when they're no
861 ret = bch2_inode_delete_keys(trans, inum, BTREE_ID_extents) ?:
862 bch2_inode_delete_keys(trans, inum, BTREE_ID_xattrs) ?:
863 bch2_inode_delete_keys(trans, inum, BTREE_ID_dirents);
867 bch2_trans_begin(trans);
869 ret = bch2_subvolume_get_snapshot(trans, inum.subvol, &snapshot);
873 k = bch2_bkey_get_iter(trans, &iter, BTREE_ID_inodes,
874 SPOS(0, inum.inum, snapshot),
875 BTREE_ITER_INTENT|BTREE_ITER_CACHED);
880 if (!bkey_is_inode(k.k)) {
881 bch2_fs_inconsistent(c,
882 "inode %llu:%u not found when deleting",
883 inum.inum, snapshot);
888 bch2_inode_unpack(k, &inode_u);
890 bkey_inode_generation_init(&delete.k_i);
891 delete.k.p = iter.pos;
892 delete.v.bi_generation = cpu_to_le32(inode_u.bi_generation + 1);
894 ret = bch2_trans_update(trans, &iter, &delete.k_i, 0) ?:
895 bch2_trans_commit(trans, NULL, NULL,
896 BCH_TRANS_COMMIT_no_enospc);
898 bch2_trans_iter_exit(trans, &iter);
899 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
902 bch2_trans_put(trans);
906 int bch2_inode_find_by_inum_nowarn_trans(struct btree_trans *trans,
908 struct bch_inode_unpacked *inode)
910 struct btree_iter iter;
913 ret = bch2_inode_peek_nowarn(trans, &iter, inode, inum, 0);
915 bch2_trans_iter_exit(trans, &iter);
919 int bch2_inode_find_by_inum_trans(struct btree_trans *trans,
921 struct bch_inode_unpacked *inode)
923 struct btree_iter iter;
926 ret = bch2_inode_peek(trans, &iter, inode, inum, 0);
928 bch2_trans_iter_exit(trans, &iter);
932 int bch2_inode_find_by_inum(struct bch_fs *c, subvol_inum inum,
933 struct bch_inode_unpacked *inode)
935 return bch2_trans_do(c, NULL, NULL, 0,
936 bch2_inode_find_by_inum_trans(trans, inum, inode));
939 int bch2_inode_nlink_inc(struct bch_inode_unpacked *bi)
941 if (bi->bi_flags & BCH_INODE_unlinked)
942 bi->bi_flags &= ~BCH_INODE_unlinked;
944 if (bi->bi_nlink == U32_MAX)
953 void bch2_inode_nlink_dec(struct btree_trans *trans, struct bch_inode_unpacked *bi)
955 if (bi->bi_nlink && (bi->bi_flags & BCH_INODE_unlinked)) {
956 bch2_trans_inconsistent(trans, "inode %llu unlinked but link count nonzero",
961 if (bi->bi_flags & BCH_INODE_unlinked) {
962 bch2_trans_inconsistent(trans, "inode %llu link count underflow", bi->bi_inum);
969 bi->bi_flags |= BCH_INODE_unlinked;
972 struct bch_opts bch2_inode_opts_to_opts(struct bch_inode_unpacked *inode)
974 struct bch_opts ret = { 0 };
975 #define x(_name, _bits) \
976 if (inode->bi_##_name) \
977 opt_set(ret, _name, inode->bi_##_name - 1);
983 void bch2_inode_opts_get(struct bch_io_opts *opts, struct bch_fs *c,
984 struct bch_inode_unpacked *inode)
986 #define x(_name, _bits) opts->_name = inode_opt_get(c, inode, _name);
991 opts->compression = opts->background_compression = opts->data_checksum = opts->erasure_code = 0;
994 int bch2_inum_opts_get(struct btree_trans *trans, subvol_inum inum, struct bch_io_opts *opts)
996 struct bch_inode_unpacked inode;
997 int ret = lockrestart_do(trans, bch2_inode_find_by_inum_trans(trans, inum, &inode));
1002 bch2_inode_opts_get(opts, trans->c, &inode);
1006 int bch2_inode_rm_snapshot(struct btree_trans *trans, u64 inum, u32 snapshot)
1008 struct bch_fs *c = trans->c;
1009 struct btree_iter iter = { NULL };
1010 struct bkey_i_inode_generation delete;
1011 struct bch_inode_unpacked inode_u;
1016 ret = bch2_btree_delete_range_trans(trans, BTREE_ID_extents,
1017 SPOS(inum, 0, snapshot),
1018 SPOS(inum, U64_MAX, snapshot),
1020 bch2_btree_delete_range_trans(trans, BTREE_ID_dirents,
1021 SPOS(inum, 0, snapshot),
1022 SPOS(inum, U64_MAX, snapshot),
1024 bch2_btree_delete_range_trans(trans, BTREE_ID_xattrs,
1025 SPOS(inum, 0, snapshot),
1026 SPOS(inum, U64_MAX, snapshot),
1028 } while (ret == -BCH_ERR_transaction_restart_nested);
1032 bch2_trans_begin(trans);
1034 k = bch2_bkey_get_iter(trans, &iter, BTREE_ID_inodes,
1035 SPOS(0, inum, snapshot), BTREE_ITER_INTENT);
1040 if (!bkey_is_inode(k.k)) {
1041 bch2_fs_inconsistent(c,
1042 "inode %llu:%u not found when deleting",
1048 bch2_inode_unpack(k, &inode_u);
1050 /* Subvolume root? */
1051 if (inode_u.bi_subvol)
1052 bch_warn(c, "deleting inode %llu marked as unlinked, but also a subvolume root!?", inode_u.bi_inum);
1054 bkey_inode_generation_init(&delete.k_i);
1055 delete.k.p = iter.pos;
1056 delete.v.bi_generation = cpu_to_le32(inode_u.bi_generation + 1);
1058 ret = bch2_trans_update(trans, &iter, &delete.k_i, 0) ?:
1059 bch2_trans_commit(trans, NULL, NULL,
1060 BCH_TRANS_COMMIT_no_enospc);
1062 bch2_trans_iter_exit(trans, &iter);
1063 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
1066 return ret ?: -BCH_ERR_transaction_restart_nested;
1069 static int may_delete_deleted_inode(struct btree_trans *trans,
1070 struct btree_iter *iter,
1072 bool *need_another_pass)
1074 struct bch_fs *c = trans->c;
1075 struct btree_iter inode_iter;
1077 struct bch_inode_unpacked inode;
1080 k = bch2_bkey_get_iter(trans, &inode_iter, BTREE_ID_inodes, pos, BTREE_ITER_CACHED);
1085 ret = bkey_is_inode(k.k) ? 0 : -BCH_ERR_ENOENT_inode;
1086 if (fsck_err_on(!bkey_is_inode(k.k), c,
1087 deleted_inode_missing,
1088 "nonexistent inode %llu:%u in deleted_inodes btree",
1089 pos.offset, pos.snapshot))
1092 ret = bch2_inode_unpack(k, &inode);
1096 if (fsck_err_on(S_ISDIR(inode.bi_mode), c,
1097 deleted_inode_is_dir,
1098 "directory %llu:%u in deleted_inodes btree",
1099 pos.offset, pos.snapshot))
1102 if (fsck_err_on(!(inode.bi_flags & BCH_INODE_unlinked), c,
1103 deleted_inode_not_unlinked,
1104 "non-deleted inode %llu:%u in deleted_inodes btree",
1105 pos.offset, pos.snapshot))
1110 deleted_inode_but_clean,
1111 "filesystem marked as clean but have deleted inode %llu:%u",
1112 pos.offset, pos.snapshot)) {
1117 if (bch2_snapshot_is_internal_node(c, pos.snapshot)) {
1118 struct bpos new_min_pos;
1120 ret = bch2_propagate_key_to_snapshot_leaves(trans, inode_iter.btree_id, k, &new_min_pos);
1124 inode.bi_flags &= ~BCH_INODE_unlinked;
1126 ret = bch2_inode_write_flags(trans, &inode_iter, &inode,
1127 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
1128 bch_err_msg(c, ret, "clearing inode unlinked flag");
1133 * We'll need another write buffer flush to pick up the new
1134 * unlinked inodes in the snapshot leaves:
1136 *need_another_pass = true;
1143 bch2_trans_iter_exit(trans, &inode_iter);
1146 ret = bch2_btree_bit_mod(trans, BTREE_ID_deleted_inodes, pos, false);
1150 int bch2_delete_dead_inodes(struct bch_fs *c)
1152 struct btree_trans *trans = bch2_trans_get(c);
1153 struct btree_iter iter;
1155 bool need_another_pass;
1158 need_another_pass = false;
1161 * Weird transaction restart handling here because on successful delete,
1162 * bch2_inode_rm_snapshot() will return a nested transaction restart,
1163 * but we can't retry because the btree write buffer won't have been
1164 * flushed and we'd spin:
1166 for_each_btree_key(trans, iter, BTREE_ID_deleted_inodes, POS_MIN,
1167 BTREE_ITER_PREFETCH|BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
1168 ret = commit_do(trans, NULL, NULL,
1169 BCH_TRANS_COMMIT_no_enospc|
1170 BCH_TRANS_COMMIT_lazy_rw,
1171 may_delete_deleted_inode(trans, &iter, k.k->p, &need_another_pass));
1176 if (!test_bit(BCH_FS_rw, &c->flags)) {
1177 bch2_trans_unlock(trans);
1181 bch_verbose(c, "deleting unlinked inode %llu:%u", k.k->p.offset, k.k->p.snapshot);
1183 ret = bch2_inode_rm_snapshot(trans, k.k->p.offset, k.k->p.snapshot);
1184 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
1188 bch2_trans_iter_exit(trans, &iter);
1190 if (!ret && need_another_pass) {
1191 ret = bch2_btree_write_buffer_flush_sync(trans);
1197 bch2_trans_put(trans);