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"
11 #include "disk_accounting.h"
14 #include "extent_update.h"
18 #include "subvolume.h"
21 #include <linux/random.h>
23 #include <asm/unaligned.h>
25 #define x(name, ...) #name,
26 const char * const bch2_inode_opts[] = {
31 static const char * const bch2_inode_flag_strs[] = {
37 static const u8 byte_table[8] = { 1, 2, 3, 4, 6, 8, 10, 13 };
39 static int inode_decode_field(const u8 *in, const u8 *end,
40 u64 out[2], unsigned *out_bits)
42 __be64 be[2] = { 0, 0 };
43 unsigned bytes, shift;
53 * position of highest set bit indicates number of bytes:
54 * shift = number of bits to remove in high byte:
56 shift = 8 - __fls(*in); /* 1 <= shift <= 8 */
57 bytes = byte_table[shift - 1];
62 p = (u8 *) be + 16 - bytes;
64 *p ^= (1 << 8) >> shift;
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]) : fls64(out[1]);
73 static inline void bch2_inode_pack_inlined(struct bkey_inode_buf *packed,
74 const struct bch_inode_unpacked *inode)
76 struct bkey_i_inode_v3 *k = &packed->inode;
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_fieldnr = 0;
84 bkey_inode_v3_init(&packed->inode.k_i);
85 packed->inode.k.p.offset = inode->bi_inum;
86 packed->inode.v.bi_journal_seq = cpu_to_le64(inode->bi_journal_seq);
87 packed->inode.v.bi_hash_seed = inode->bi_hash_seed;
88 packed->inode.v.bi_flags = cpu_to_le64(inode->bi_flags);
89 packed->inode.v.bi_sectors = cpu_to_le64(inode->bi_sectors);
90 packed->inode.v.bi_size = cpu_to_le64(inode->bi_size);
91 packed->inode.v.bi_version = cpu_to_le64(inode->bi_version);
92 SET_INODEv3_MODE(&packed->inode.v, inode->bi_mode);
93 SET_INODEv3_FIELDS_START(&packed->inode.v, INODEv3_FIELDS_START_CUR);
96 #define x(_name, _bits) \
100 ret = bch2_varint_encode_fast(out, inode->_name); \
106 last_nonzero_field = out; \
107 last_nonzero_fieldnr = nr_fields; \
115 BCH_INODE_FIELDS_v3()
119 out = last_nonzero_field;
120 nr_fields = last_nonzero_fieldnr;
122 bytes = out - (u8 *) &packed->inode.v;
123 set_bkey_val_bytes(&packed->inode.k, bytes);
124 memset_u64s_tail(&packed->inode.v, 0, bytes);
126 SET_INODEv3_NR_FIELDS(&k->v, nr_fields);
128 if (IS_ENABLED(CONFIG_BCACHEFS_DEBUG)) {
129 struct bch_inode_unpacked unpacked;
131 ret = bch2_inode_unpack(bkey_i_to_s_c(&packed->inode.k_i), &unpacked);
133 BUG_ON(unpacked.bi_inum != inode->bi_inum);
134 BUG_ON(unpacked.bi_hash_seed != inode->bi_hash_seed);
135 BUG_ON(unpacked.bi_sectors != inode->bi_sectors);
136 BUG_ON(unpacked.bi_size != inode->bi_size);
137 BUG_ON(unpacked.bi_version != inode->bi_version);
138 BUG_ON(unpacked.bi_mode != inode->bi_mode);
140 #define x(_name, _bits) if (unpacked._name != inode->_name) \
141 panic("unpacked %llu should be %llu", \
142 (u64) unpacked._name, (u64) inode->_name);
143 BCH_INODE_FIELDS_v3()
148 void bch2_inode_pack(struct bkey_inode_buf *packed,
149 const struct bch_inode_unpacked *inode)
151 bch2_inode_pack_inlined(packed, inode);
154 static noinline int bch2_inode_unpack_v1(struct bkey_s_c_inode inode,
155 struct bch_inode_unpacked *unpacked)
157 const u8 *in = inode.v->fields;
158 const u8 *end = bkey_val_end(inode);
160 unsigned fieldnr = 0, field_bits;
163 #define x(_name, _bits) \
164 if (fieldnr++ == INODE_NR_FIELDS(inode.v)) { \
165 unsigned offset = offsetof(struct bch_inode_unpacked, _name);\
166 memset((void *) unpacked + offset, 0, \
167 sizeof(*unpacked) - offset); \
171 ret = inode_decode_field(in, end, field, &field_bits); \
175 if (field_bits > sizeof(unpacked->_name) * 8) \
178 unpacked->_name = field[1]; \
181 BCH_INODE_FIELDS_v2()
184 /* XXX: signal if there were more fields than expected? */
188 static int bch2_inode_unpack_v2(struct bch_inode_unpacked *unpacked,
189 const u8 *in, const u8 *end,
192 unsigned fieldnr = 0;
196 #define x(_name, _bits) \
197 if (fieldnr < nr_fields) { \
198 ret = bch2_varint_decode_fast(in, end, &v[0]); \
204 ret = bch2_varint_decode_fast(in, end, &v[1]); \
215 unpacked->_name = v[0]; \
216 if (v[1] || v[0] != unpacked->_name) \
220 BCH_INODE_FIELDS_v2()
223 /* XXX: signal if there were more fields than expected? */
227 static int bch2_inode_unpack_v3(struct bkey_s_c k,
228 struct bch_inode_unpacked *unpacked)
230 struct bkey_s_c_inode_v3 inode = bkey_s_c_to_inode_v3(k);
231 const u8 *in = inode.v->fields;
232 const u8 *end = bkey_val_end(inode);
233 unsigned nr_fields = INODEv3_NR_FIELDS(inode.v);
234 unsigned fieldnr = 0;
238 unpacked->bi_inum = inode.k->p.offset;
239 unpacked->bi_journal_seq= le64_to_cpu(inode.v->bi_journal_seq);
240 unpacked->bi_hash_seed = inode.v->bi_hash_seed;
241 unpacked->bi_flags = le64_to_cpu(inode.v->bi_flags);
242 unpacked->bi_sectors = le64_to_cpu(inode.v->bi_sectors);
243 unpacked->bi_size = le64_to_cpu(inode.v->bi_size);
244 unpacked->bi_version = le64_to_cpu(inode.v->bi_version);
245 unpacked->bi_mode = INODEv3_MODE(inode.v);
247 #define x(_name, _bits) \
248 if (fieldnr < nr_fields) { \
249 ret = bch2_varint_decode_fast(in, end, &v[0]); \
255 ret = bch2_varint_decode_fast(in, end, &v[1]); \
266 unpacked->_name = v[0]; \
267 if (v[1] || v[0] != unpacked->_name) \
271 BCH_INODE_FIELDS_v3()
274 /* XXX: signal if there were more fields than expected? */
278 static noinline int bch2_inode_unpack_slowpath(struct bkey_s_c k,
279 struct bch_inode_unpacked *unpacked)
281 memset(unpacked, 0, sizeof(*unpacked));
284 case KEY_TYPE_inode: {
285 struct bkey_s_c_inode inode = bkey_s_c_to_inode(k);
287 unpacked->bi_inum = inode.k->p.offset;
288 unpacked->bi_journal_seq= 0;
289 unpacked->bi_hash_seed = inode.v->bi_hash_seed;
290 unpacked->bi_flags = le32_to_cpu(inode.v->bi_flags);
291 unpacked->bi_mode = le16_to_cpu(inode.v->bi_mode);
293 if (INODE_NEW_VARINT(inode.v)) {
294 return bch2_inode_unpack_v2(unpacked, inode.v->fields,
296 INODE_NR_FIELDS(inode.v));
298 return bch2_inode_unpack_v1(inode, unpacked);
302 case KEY_TYPE_inode_v2: {
303 struct bkey_s_c_inode_v2 inode = bkey_s_c_to_inode_v2(k);
305 unpacked->bi_inum = inode.k->p.offset;
306 unpacked->bi_journal_seq= le64_to_cpu(inode.v->bi_journal_seq);
307 unpacked->bi_hash_seed = inode.v->bi_hash_seed;
308 unpacked->bi_flags = le64_to_cpu(inode.v->bi_flags);
309 unpacked->bi_mode = le16_to_cpu(inode.v->bi_mode);
311 return bch2_inode_unpack_v2(unpacked, inode.v->fields,
313 INODEv2_NR_FIELDS(inode.v));
320 int bch2_inode_unpack(struct bkey_s_c k,
321 struct bch_inode_unpacked *unpacked)
323 if (likely(k.k->type == KEY_TYPE_inode_v3))
324 return bch2_inode_unpack_v3(k, unpacked);
325 return bch2_inode_unpack_slowpath(k, unpacked);
328 int bch2_inode_peek_nowarn(struct btree_trans *trans,
329 struct btree_iter *iter,
330 struct bch_inode_unpacked *inode,
331 subvol_inum inum, unsigned flags)
337 ret = bch2_subvolume_get_snapshot(trans, inum.subvol, &snapshot);
341 k = bch2_bkey_get_iter(trans, iter, BTREE_ID_inodes,
342 SPOS(0, inum.inum, snapshot),
343 flags|BTREE_ITER_cached);
348 ret = bkey_is_inode(k.k) ? 0 : -BCH_ERR_ENOENT_inode;
352 ret = bch2_inode_unpack(k, inode);
358 bch2_trans_iter_exit(trans, iter);
362 int bch2_inode_peek(struct btree_trans *trans,
363 struct btree_iter *iter,
364 struct bch_inode_unpacked *inode,
365 subvol_inum inum, unsigned flags)
367 int ret = bch2_inode_peek_nowarn(trans, iter, inode, inum, flags);
368 bch_err_msg(trans->c, ret, "looking up inum %u:%llu:", inum.subvol, inum.inum);
372 int bch2_inode_write_flags(struct btree_trans *trans,
373 struct btree_iter *iter,
374 struct bch_inode_unpacked *inode,
375 enum btree_iter_update_trigger_flags flags)
377 struct bkey_inode_buf *inode_p;
379 inode_p = bch2_trans_kmalloc(trans, sizeof(*inode_p));
381 return PTR_ERR(inode_p);
383 bch2_inode_pack_inlined(inode_p, inode);
384 inode_p->inode.k.p.snapshot = iter->snapshot;
385 return bch2_trans_update(trans, iter, &inode_p->inode.k_i, flags);
388 int __bch2_fsck_write_inode(struct btree_trans *trans,
389 struct bch_inode_unpacked *inode,
392 struct bkey_inode_buf *inode_p =
393 bch2_trans_kmalloc(trans, sizeof(*inode_p));
396 return PTR_ERR(inode_p);
398 bch2_inode_pack(inode_p, inode);
399 inode_p->inode.k.p.snapshot = snapshot;
401 return bch2_btree_insert_nonextent(trans, BTREE_ID_inodes,
403 BTREE_UPDATE_internal_snapshot_node);
406 int bch2_fsck_write_inode(struct btree_trans *trans,
407 struct bch_inode_unpacked *inode,
410 int ret = commit_do(trans, NULL, NULL, BCH_TRANS_COMMIT_no_enospc,
411 __bch2_fsck_write_inode(trans, inode, snapshot));
412 bch_err_fn(trans->c, ret);
416 struct bkey_i *bch2_inode_to_v3(struct btree_trans *trans, struct bkey_i *k)
418 struct bch_inode_unpacked u;
419 struct bkey_inode_buf *inode_p;
422 if (!bkey_is_inode(&k->k))
423 return ERR_PTR(-ENOENT);
425 inode_p = bch2_trans_kmalloc(trans, sizeof(*inode_p));
427 return ERR_CAST(inode_p);
429 ret = bch2_inode_unpack(bkey_i_to_s_c(k), &u);
433 bch2_inode_pack(inode_p, &u);
434 return &inode_p->inode.k_i;
437 static int __bch2_inode_validate(struct bch_fs *c, struct bkey_s_c k,
438 enum bch_validate_flags flags)
440 struct bch_inode_unpacked unpacked;
443 bkey_fsck_err_on(k.k->p.inode,
444 c, inode_pos_inode_nonzero,
445 "nonzero k.p.inode");
447 bkey_fsck_err_on(k.k->p.offset < BLOCKDEV_INODE_MAX,
448 c, inode_pos_blockdev_range,
449 "fs inode in blockdev range");
451 bkey_fsck_err_on(bch2_inode_unpack(k, &unpacked),
452 c, inode_unpack_error,
453 "invalid variable length fields");
455 bkey_fsck_err_on(unpacked.bi_data_checksum >= BCH_CSUM_OPT_NR + 1,
456 c, inode_checksum_type_invalid,
457 "invalid data checksum type (%u >= %u",
458 unpacked.bi_data_checksum, BCH_CSUM_OPT_NR + 1);
460 bkey_fsck_err_on(unpacked.bi_compression &&
461 !bch2_compression_opt_valid(unpacked.bi_compression - 1),
462 c, inode_compression_type_invalid,
463 "invalid compression opt %u", unpacked.bi_compression - 1);
465 bkey_fsck_err_on((unpacked.bi_flags & BCH_INODE_unlinked) &&
466 unpacked.bi_nlink != 0,
467 c, inode_unlinked_but_nlink_nonzero,
468 "flagged as unlinked but bi_nlink != 0");
470 bkey_fsck_err_on(unpacked.bi_subvol && !S_ISDIR(unpacked.bi_mode),
471 c, inode_subvol_root_but_not_dir,
472 "subvolume root but not a directory");
477 int bch2_inode_validate(struct bch_fs *c, struct bkey_s_c k,
478 enum bch_validate_flags flags)
480 struct bkey_s_c_inode inode = bkey_s_c_to_inode(k);
483 bkey_fsck_err_on(INODE_STR_HASH(inode.v) >= BCH_STR_HASH_NR,
484 c, inode_str_hash_invalid,
485 "invalid str hash type (%llu >= %u)",
486 INODE_STR_HASH(inode.v), BCH_STR_HASH_NR);
488 ret = __bch2_inode_validate(c, k, flags);
493 int bch2_inode_v2_validate(struct bch_fs *c, struct bkey_s_c k,
494 enum bch_validate_flags flags)
496 struct bkey_s_c_inode_v2 inode = bkey_s_c_to_inode_v2(k);
499 bkey_fsck_err_on(INODEv2_STR_HASH(inode.v) >= BCH_STR_HASH_NR,
500 c, inode_str_hash_invalid,
501 "invalid str hash type (%llu >= %u)",
502 INODEv2_STR_HASH(inode.v), BCH_STR_HASH_NR);
504 ret = __bch2_inode_validate(c, k, flags);
509 int bch2_inode_v3_validate(struct bch_fs *c, struct bkey_s_c k,
510 enum bch_validate_flags flags)
512 struct bkey_s_c_inode_v3 inode = bkey_s_c_to_inode_v3(k);
515 bkey_fsck_err_on(INODEv3_FIELDS_START(inode.v) < INODEv3_FIELDS_START_INITIAL ||
516 INODEv3_FIELDS_START(inode.v) > bkey_val_u64s(inode.k),
517 c, inode_v3_fields_start_bad,
518 "invalid fields_start (got %llu, min %u max %zu)",
519 INODEv3_FIELDS_START(inode.v),
520 INODEv3_FIELDS_START_INITIAL,
521 bkey_val_u64s(inode.k));
523 bkey_fsck_err_on(INODEv3_STR_HASH(inode.v) >= BCH_STR_HASH_NR,
524 c, inode_str_hash_invalid,
525 "invalid str hash type (%llu >= %u)",
526 INODEv3_STR_HASH(inode.v), BCH_STR_HASH_NR);
528 ret = __bch2_inode_validate(c, k, flags);
533 static void __bch2_inode_unpacked_to_text(struct printbuf *out,
534 struct bch_inode_unpacked *inode)
536 prt_printf(out, "\n");
537 printbuf_indent_add(out, 2);
538 prt_printf(out, "mode=%o\n", inode->bi_mode);
540 prt_str(out, "flags=");
541 prt_bitflags(out, bch2_inode_flag_strs, inode->bi_flags & ((1U << 20) - 1));
542 prt_printf(out, "(%x)\n", inode->bi_flags);
544 prt_printf(out, "journal_seq=%llu\n", inode->bi_journal_seq);
545 prt_printf(out, "bi_size=%llu\n", inode->bi_size);
546 prt_printf(out, "bi_sectors=%llu\n", inode->bi_sectors);
547 prt_printf(out, "bi_version=%llu\n", inode->bi_version);
549 #define x(_name, _bits) \
550 prt_printf(out, #_name "=%llu\n", (u64) inode->_name);
551 BCH_INODE_FIELDS_v3()
554 bch2_printbuf_strip_trailing_newline(out);
555 printbuf_indent_sub(out, 2);
558 void bch2_inode_unpacked_to_text(struct printbuf *out, struct bch_inode_unpacked *inode)
560 prt_printf(out, "inum: %llu ", inode->bi_inum);
561 __bch2_inode_unpacked_to_text(out, inode);
564 void bch2_inode_to_text(struct printbuf *out, struct bch_fs *c, struct bkey_s_c k)
566 struct bch_inode_unpacked inode;
568 if (bch2_inode_unpack(k, &inode)) {
569 prt_printf(out, "(unpack error)");
573 __bch2_inode_unpacked_to_text(out, &inode);
576 static inline u64 bkey_inode_flags(struct bkey_s_c k)
580 return le32_to_cpu(bkey_s_c_to_inode(k).v->bi_flags);
581 case KEY_TYPE_inode_v2:
582 return le64_to_cpu(bkey_s_c_to_inode_v2(k).v->bi_flags);
583 case KEY_TYPE_inode_v3:
584 return le64_to_cpu(bkey_s_c_to_inode_v3(k).v->bi_flags);
590 static inline bool bkey_is_deleted_inode(struct bkey_s_c k)
592 return bkey_inode_flags(k) & BCH_INODE_unlinked;
595 int bch2_trigger_inode(struct btree_trans *trans,
596 enum btree_id btree_id, unsigned level,
599 enum btree_iter_update_trigger_flags flags)
601 if ((flags & BTREE_TRIGGER_atomic) && (flags & BTREE_TRIGGER_insert)) {
602 BUG_ON(!trans->journal_res.seq);
603 bkey_s_to_inode_v3(new).v->bi_journal_seq = cpu_to_le64(trans->journal_res.seq);
606 s64 nr = bkey_is_inode(new.k) - bkey_is_inode(old.k);
607 if ((flags & (BTREE_TRIGGER_transactional|BTREE_TRIGGER_gc)) && nr) {
608 struct disk_accounting_pos acc = { .type = BCH_DISK_ACCOUNTING_nr_inodes };
609 int ret = bch2_disk_accounting_mod(trans, &acc, &nr, 1, flags & BTREE_TRIGGER_gc);
614 int deleted_delta = (int) bkey_is_deleted_inode(new.s_c) -
615 (int) bkey_is_deleted_inode(old);
616 if ((flags & BTREE_TRIGGER_transactional) && deleted_delta) {
617 int ret = bch2_btree_bit_mod_buffered(trans, BTREE_ID_deleted_inodes,
618 new.k->p, deleted_delta > 0);
626 int bch2_inode_generation_validate(struct bch_fs *c, struct bkey_s_c k,
627 enum bch_validate_flags flags)
631 bkey_fsck_err_on(k.k->p.inode,
632 c, inode_pos_inode_nonzero,
633 "nonzero k.p.inode");
638 void bch2_inode_generation_to_text(struct printbuf *out, struct bch_fs *c,
641 struct bkey_s_c_inode_generation gen = bkey_s_c_to_inode_generation(k);
643 prt_printf(out, "generation: %u", le32_to_cpu(gen.v->bi_generation));
646 void bch2_inode_init_early(struct bch_fs *c,
647 struct bch_inode_unpacked *inode_u)
649 enum bch_str_hash_type str_hash =
650 bch2_str_hash_opt_to_type(c, c->opts.str_hash);
652 memset(inode_u, 0, sizeof(*inode_u));
655 inode_u->bi_flags |= str_hash << INODE_STR_HASH_OFFSET;
656 get_random_bytes(&inode_u->bi_hash_seed,
657 sizeof(inode_u->bi_hash_seed));
660 void bch2_inode_init_late(struct bch_inode_unpacked *inode_u, u64 now,
661 uid_t uid, gid_t gid, umode_t mode, dev_t rdev,
662 struct bch_inode_unpacked *parent)
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;
673 if (parent && parent->bi_mode & S_ISGID) {
674 inode_u->bi_gid = parent->bi_gid;
676 inode_u->bi_mode |= S_ISGID;
680 #define x(_name, ...) inode_u->bi_##_name = parent->bi_##_name;
686 void bch2_inode_init(struct bch_fs *c, struct bch_inode_unpacked *inode_u,
687 uid_t uid, gid_t gid, umode_t mode, dev_t rdev,
688 struct bch_inode_unpacked *parent)
690 bch2_inode_init_early(c, inode_u);
691 bch2_inode_init_late(inode_u, bch2_current_time(c),
692 uid, gid, mode, rdev, parent);
695 static inline u32 bkey_generation(struct bkey_s_c k)
699 case KEY_TYPE_inode_v2:
701 case KEY_TYPE_inode_generation:
702 return le32_to_cpu(bkey_s_c_to_inode_generation(k).v->bi_generation);
709 * This just finds an empty slot:
711 int bch2_inode_create(struct btree_trans *trans,
712 struct btree_iter *iter,
713 struct bch_inode_unpacked *inode_u,
714 u32 snapshot, u64 cpu)
716 struct bch_fs *c = trans->c;
718 u64 min, max, start, pos, *hint;
720 unsigned bits = (c->opts.inodes_32bit ? 31 : 63);
722 if (c->opts.shard_inode_numbers) {
723 bits -= c->inode_shard_bits;
726 max = (cpu << bits) | ~(ULLONG_MAX << bits);
728 min = max_t(u64, min, BLOCKDEV_INODE_MAX);
729 hint = c->unused_inode_hints + cpu;
731 min = BLOCKDEV_INODE_MAX;
732 max = ~(ULLONG_MAX << bits);
733 hint = c->unused_inode_hints;
736 start = READ_ONCE(*hint);
738 if (start >= max || start < min)
742 bch2_trans_iter_init(trans, iter, BTREE_ID_inodes, POS(0, pos),
743 BTREE_ITER_all_snapshots|
746 while ((k = bch2_btree_iter_peek(iter)).k &&
747 !(ret = bkey_err(k)) &&
748 bkey_lt(k.k->p, POS(0, max))) {
749 if (pos < iter->pos.offset)
753 * We don't need to iterate over keys in every snapshot once
754 * we've found just one:
756 pos = iter->pos.offset + 1;
757 bch2_btree_iter_set_pos(iter, POS(0, pos));
760 if (!ret && pos < max)
763 if (!ret && start == min)
764 ret = -BCH_ERR_ENOSPC_inode_create;
767 bch2_trans_iter_exit(trans, iter);
771 /* Retry from start */
773 bch2_btree_iter_set_pos(iter, POS(0, pos));
776 bch2_btree_iter_set_pos(iter, SPOS(0, pos, snapshot));
777 k = bch2_btree_iter_peek_slot(iter);
780 bch2_trans_iter_exit(trans, iter);
784 *hint = k.k->p.offset;
785 inode_u->bi_inum = k.k->p.offset;
786 inode_u->bi_generation = bkey_generation(k);
790 static int bch2_inode_delete_keys(struct btree_trans *trans,
791 subvol_inum inum, enum btree_id id)
793 struct btree_iter iter;
795 struct bkey_i delete;
796 struct bpos end = POS(inum.inum, U64_MAX);
801 * We're never going to be deleting partial extents, no need to use an
804 bch2_trans_iter_init(trans, &iter, id, POS(inum.inum, 0),
808 bch2_trans_begin(trans);
810 ret = bch2_subvolume_get_snapshot(trans, inum.subvol, &snapshot);
814 bch2_btree_iter_set_snapshot(&iter, snapshot);
816 k = bch2_btree_iter_peek_upto(&iter, end);
824 bkey_init(&delete.k);
825 delete.k.p = iter.pos;
827 if (iter.flags & BTREE_ITER_is_extents)
828 bch2_key_resize(&delete.k,
829 bpos_min(end, k.k->p).offset -
832 ret = bch2_trans_update(trans, &iter, &delete, 0) ?:
833 bch2_trans_commit(trans, NULL, NULL,
834 BCH_TRANS_COMMIT_no_enospc);
836 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
840 bch2_trans_iter_exit(trans, &iter);
844 int bch2_inode_rm(struct bch_fs *c, subvol_inum inum)
846 struct btree_trans *trans = bch2_trans_get(c);
847 struct btree_iter iter = { NULL };
848 struct bkey_i_inode_generation delete;
849 struct bch_inode_unpacked inode_u;
855 * If this was a directory, there shouldn't be any real dirents left -
856 * but there could be whiteouts (from hash collisions) that we should
859 * XXX: the dirent could ideally would delete whiteouts when they're no
862 ret = bch2_inode_delete_keys(trans, inum, BTREE_ID_extents) ?:
863 bch2_inode_delete_keys(trans, inum, BTREE_ID_xattrs) ?:
864 bch2_inode_delete_keys(trans, inum, BTREE_ID_dirents);
868 bch2_trans_begin(trans);
870 ret = bch2_subvolume_get_snapshot(trans, inum.subvol, &snapshot);
874 k = bch2_bkey_get_iter(trans, &iter, BTREE_ID_inodes,
875 SPOS(0, inum.inum, snapshot),
876 BTREE_ITER_intent|BTREE_ITER_cached);
881 if (!bkey_is_inode(k.k)) {
882 bch2_fs_inconsistent(c,
883 "inode %llu:%u not found when deleting",
884 inum.inum, snapshot);
889 bch2_inode_unpack(k, &inode_u);
891 bkey_inode_generation_init(&delete.k_i);
892 delete.k.p = iter.pos;
893 delete.v.bi_generation = cpu_to_le32(inode_u.bi_generation + 1);
895 ret = bch2_trans_update(trans, &iter, &delete.k_i, 0) ?:
896 bch2_trans_commit(trans, NULL, NULL,
897 BCH_TRANS_COMMIT_no_enospc);
899 bch2_trans_iter_exit(trans, &iter);
900 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
903 bch2_trans_put(trans);
907 int bch2_inode_find_by_inum_nowarn_trans(struct btree_trans *trans,
909 struct bch_inode_unpacked *inode)
911 struct btree_iter iter;
914 ret = bch2_inode_peek_nowarn(trans, &iter, inode, inum, 0);
916 bch2_trans_iter_exit(trans, &iter);
920 int bch2_inode_find_by_inum_trans(struct btree_trans *trans,
922 struct bch_inode_unpacked *inode)
924 struct btree_iter iter;
927 ret = bch2_inode_peek(trans, &iter, inode, inum, 0);
929 bch2_trans_iter_exit(trans, &iter);
933 int bch2_inode_find_by_inum(struct bch_fs *c, subvol_inum inum,
934 struct bch_inode_unpacked *inode)
936 return bch2_trans_do(c, NULL, NULL, 0,
937 bch2_inode_find_by_inum_trans(trans, inum, inode));
940 int bch2_inode_nlink_inc(struct bch_inode_unpacked *bi)
942 if (bi->bi_flags & BCH_INODE_unlinked)
943 bi->bi_flags &= ~BCH_INODE_unlinked;
945 if (bi->bi_nlink == U32_MAX)
954 void bch2_inode_nlink_dec(struct btree_trans *trans, struct bch_inode_unpacked *bi)
956 if (bi->bi_nlink && (bi->bi_flags & BCH_INODE_unlinked)) {
957 bch2_trans_inconsistent(trans, "inode %llu unlinked but link count nonzero",
962 if (bi->bi_flags & BCH_INODE_unlinked) {
963 bch2_trans_inconsistent(trans, "inode %llu link count underflow", bi->bi_inum);
970 bi->bi_flags |= BCH_INODE_unlinked;
973 struct bch_opts bch2_inode_opts_to_opts(struct bch_inode_unpacked *inode)
975 struct bch_opts ret = { 0 };
976 #define x(_name, _bits) \
977 if (inode->bi_##_name) \
978 opt_set(ret, _name, inode->bi_##_name - 1);
984 void bch2_inode_opts_get(struct bch_io_opts *opts, struct bch_fs *c,
985 struct bch_inode_unpacked *inode)
987 #define x(_name, _bits) opts->_name = inode_opt_get(c, inode, _name);
992 opts->compression = opts->background_compression = opts->data_checksum = opts->erasure_code = 0;
995 int bch2_inum_opts_get(struct btree_trans *trans, subvol_inum inum, struct bch_io_opts *opts)
997 struct bch_inode_unpacked inode;
998 int ret = lockrestart_do(trans, bch2_inode_find_by_inum_trans(trans, inum, &inode));
1003 bch2_inode_opts_get(opts, trans->c, &inode);
1007 int bch2_inode_rm_snapshot(struct btree_trans *trans, u64 inum, u32 snapshot)
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;
1017 ret = bch2_btree_delete_range_trans(trans, BTREE_ID_extents,
1018 SPOS(inum, 0, snapshot),
1019 SPOS(inum, U64_MAX, snapshot),
1021 bch2_btree_delete_range_trans(trans, BTREE_ID_dirents,
1022 SPOS(inum, 0, snapshot),
1023 SPOS(inum, U64_MAX, snapshot),
1025 bch2_btree_delete_range_trans(trans, BTREE_ID_xattrs,
1026 SPOS(inum, 0, snapshot),
1027 SPOS(inum, U64_MAX, snapshot),
1029 } while (ret == -BCH_ERR_transaction_restart_nested);
1033 bch2_trans_begin(trans);
1035 k = bch2_bkey_get_iter(trans, &iter, BTREE_ID_inodes,
1036 SPOS(0, inum, snapshot), BTREE_ITER_intent);
1041 if (!bkey_is_inode(k.k)) {
1042 bch2_fs_inconsistent(c,
1043 "inode %llu:%u not found when deleting",
1049 bch2_inode_unpack(k, &inode_u);
1051 /* Subvolume root? */
1052 if (inode_u.bi_subvol)
1053 bch_warn(c, "deleting inode %llu marked as unlinked, but also a subvolume root!?", inode_u.bi_inum);
1055 bkey_inode_generation_init(&delete.k_i);
1056 delete.k.p = iter.pos;
1057 delete.v.bi_generation = cpu_to_le32(inode_u.bi_generation + 1);
1059 ret = bch2_trans_update(trans, &iter, &delete.k_i, 0) ?:
1060 bch2_trans_commit(trans, NULL, NULL,
1061 BCH_TRANS_COMMIT_no_enospc);
1063 bch2_trans_iter_exit(trans, &iter);
1064 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
1067 return ret ?: -BCH_ERR_transaction_restart_nested;
1070 static int may_delete_deleted_inode(struct btree_trans *trans,
1071 struct btree_iter *iter,
1073 bool *need_another_pass)
1075 struct bch_fs *c = trans->c;
1076 struct btree_iter inode_iter;
1078 struct bch_inode_unpacked inode;
1081 k = bch2_bkey_get_iter(trans, &inode_iter, BTREE_ID_inodes, pos, BTREE_ITER_cached);
1086 ret = bkey_is_inode(k.k) ? 0 : -BCH_ERR_ENOENT_inode;
1087 if (fsck_err_on(!bkey_is_inode(k.k),
1088 trans, deleted_inode_missing,
1089 "nonexistent inode %llu:%u in deleted_inodes btree",
1090 pos.offset, pos.snapshot))
1093 ret = bch2_inode_unpack(k, &inode);
1097 if (S_ISDIR(inode.bi_mode)) {
1098 ret = bch2_empty_dir_snapshot(trans, pos.offset, 0, pos.snapshot);
1099 if (fsck_err_on(bch2_err_matches(ret, ENOTEMPTY),
1100 trans, deleted_inode_is_dir,
1101 "non empty directory %llu:%u in deleted_inodes btree",
1102 pos.offset, pos.snapshot))
1108 if (fsck_err_on(!(inode.bi_flags & BCH_INODE_unlinked),
1109 trans, deleted_inode_not_unlinked,
1110 "non-deleted inode %llu:%u in deleted_inodes btree",
1111 pos.offset, pos.snapshot))
1115 !fsck_err(trans, deleted_inode_but_clean,
1116 "filesystem marked as clean but have deleted inode %llu:%u",
1117 pos.offset, pos.snapshot)) {
1122 if (bch2_snapshot_is_internal_node(c, pos.snapshot)) {
1123 struct bpos new_min_pos;
1125 ret = bch2_propagate_key_to_snapshot_leaves(trans, inode_iter.btree_id, k, &new_min_pos);
1129 inode.bi_flags &= ~BCH_INODE_unlinked;
1131 ret = bch2_inode_write_flags(trans, &inode_iter, &inode,
1132 BTREE_UPDATE_internal_snapshot_node);
1133 bch_err_msg(c, ret, "clearing inode unlinked flag");
1138 * We'll need another write buffer flush to pick up the new
1139 * unlinked inodes in the snapshot leaves:
1141 *need_another_pass = true;
1148 bch2_trans_iter_exit(trans, &inode_iter);
1151 ret = bch2_btree_bit_mod_buffered(trans, BTREE_ID_deleted_inodes, pos, false);
1155 int bch2_delete_dead_inodes(struct bch_fs *c)
1157 struct btree_trans *trans = bch2_trans_get(c);
1158 bool need_another_pass;
1162 * if we ran check_inodes() unlinked inodes will have already been
1163 * cleaned up but the write buffer will be out of sync; therefore we
1164 * alway need a write buffer flush
1166 ret = bch2_btree_write_buffer_flush_sync(trans);
1170 need_another_pass = false;
1173 * Weird transaction restart handling here because on successful delete,
1174 * bch2_inode_rm_snapshot() will return a nested transaction restart,
1175 * but we can't retry because the btree write buffer won't have been
1176 * flushed and we'd spin:
1178 ret = for_each_btree_key_commit(trans, iter, BTREE_ID_deleted_inodes, POS_MIN,
1179 BTREE_ITER_prefetch|BTREE_ITER_all_snapshots, k,
1180 NULL, NULL, BCH_TRANS_COMMIT_no_enospc, ({
1181 ret = may_delete_deleted_inode(trans, &iter, k.k->p, &need_another_pass);
1183 bch_verbose(c, "deleting unlinked inode %llu:%u", k.k->p.offset, k.k->p.snapshot);
1185 ret = bch2_inode_rm_snapshot(trans, k.k->p.offset, k.k->p.snapshot);
1187 * We don't want to loop here: a transaction restart
1188 * error here means we handled a transaction restart and
1189 * we're actually done, but if we loop we'll retry the
1190 * same key because the write buffer hasn't been flushed
1193 if (bch2_err_matches(ret, BCH_ERR_transaction_restart)) {
1202 if (!ret && need_another_pass)
1205 bch2_trans_put(trans);