1 // SPDX-License-Identifier: GPL-2.0
4 #include "btree_key_cache.h"
5 #include "bkey_methods.h"
6 #include "btree_update.h"
10 #include "extent_update.h"
13 #include "subvolume.h"
16 #include <linux/random.h>
18 #include <asm/unaligned.h>
20 const char * const bch2_inode_opts[] = {
21 #define x(name, ...) #name,
27 static const u8 byte_table[8] = { 1, 2, 3, 4, 6, 8, 10, 13 };
29 static int inode_decode_field(const u8 *in, const u8 *end,
30 u64 out[2], unsigned *out_bits)
32 __be64 be[2] = { 0, 0 };
33 unsigned bytes, shift;
43 * position of highest set bit indicates number of bytes:
44 * shift = number of bits to remove in high byte:
46 shift = 8 - __fls(*in); /* 1 <= shift <= 8 */
47 bytes = byte_table[shift - 1];
52 p = (u8 *) be + 16 - bytes;
54 *p ^= (1 << 8) >> shift;
56 out[0] = be64_to_cpu(be[0]);
57 out[1] = be64_to_cpu(be[1]);
58 *out_bits = out[0] ? 64 + fls64(out[0]) : fls64(out[1]);
63 static inline void bch2_inode_pack_inlined(struct bkey_inode_buf *packed,
64 const struct bch_inode_unpacked *inode)
66 struct bkey_i_inode_v3 *k = &packed->inode;
67 u8 *out = k->v.fields;
68 u8 *end = (void *) &packed[1];
69 u8 *last_nonzero_field = out;
70 unsigned nr_fields = 0, last_nonzero_fieldnr = 0;
74 bkey_inode_v3_init(&packed->inode.k_i);
75 packed->inode.k.p.offset = inode->bi_inum;
76 packed->inode.v.bi_journal_seq = cpu_to_le64(inode->bi_journal_seq);
77 packed->inode.v.bi_hash_seed = inode->bi_hash_seed;
78 packed->inode.v.bi_flags = cpu_to_le64(inode->bi_flags);
79 packed->inode.v.bi_sectors = cpu_to_le64(inode->bi_sectors);
80 packed->inode.v.bi_size = cpu_to_le64(inode->bi_size);
81 packed->inode.v.bi_version = cpu_to_le64(inode->bi_version);
82 SET_INODEv3_MODE(&packed->inode.v, inode->bi_mode);
83 SET_INODEv3_FIELDS_START(&packed->inode.v, INODEv3_FIELDS_START_CUR);
86 #define x(_name, _bits) \
90 ret = bch2_varint_encode_fast(out, inode->_name); \
96 last_nonzero_field = out; \
97 last_nonzero_fieldnr = nr_fields; \
105 BCH_INODE_FIELDS_v3()
109 out = last_nonzero_field;
110 nr_fields = last_nonzero_fieldnr;
112 bytes = out - (u8 *) &packed->inode.v;
113 set_bkey_val_bytes(&packed->inode.k, bytes);
114 memset_u64s_tail(&packed->inode.v, 0, bytes);
116 SET_INODEv3_NR_FIELDS(&k->v, nr_fields);
118 if (IS_ENABLED(CONFIG_BCACHEFS_DEBUG)) {
119 struct bch_inode_unpacked unpacked;
121 int ret = bch2_inode_unpack(bkey_i_to_s_c(&packed->inode.k_i),
124 BUG_ON(unpacked.bi_inum != inode->bi_inum);
125 BUG_ON(unpacked.bi_hash_seed != inode->bi_hash_seed);
126 BUG_ON(unpacked.bi_sectors != inode->bi_sectors);
127 BUG_ON(unpacked.bi_size != inode->bi_size);
128 BUG_ON(unpacked.bi_version != inode->bi_version);
129 BUG_ON(unpacked.bi_mode != inode->bi_mode);
131 #define x(_name, _bits) if (unpacked._name != inode->_name) \
132 panic("unpacked %llu should be %llu", \
133 (u64) unpacked._name, (u64) inode->_name);
134 BCH_INODE_FIELDS_v3()
139 void bch2_inode_pack(struct bkey_inode_buf *packed,
140 const struct bch_inode_unpacked *inode)
142 bch2_inode_pack_inlined(packed, inode);
145 static noinline int bch2_inode_unpack_v1(struct bkey_s_c_inode inode,
146 struct bch_inode_unpacked *unpacked)
148 const u8 *in = inode.v->fields;
149 const u8 *end = bkey_val_end(inode);
151 unsigned fieldnr = 0, field_bits;
154 #define x(_name, _bits) \
155 if (fieldnr++ == INODE_NR_FIELDS(inode.v)) { \
156 unsigned offset = offsetof(struct bch_inode_unpacked, _name);\
157 memset((void *) unpacked + offset, 0, \
158 sizeof(*unpacked) - offset); \
162 ret = inode_decode_field(in, end, field, &field_bits); \
166 if (field_bits > sizeof(unpacked->_name) * 8) \
169 unpacked->_name = field[1]; \
172 BCH_INODE_FIELDS_v2()
175 /* XXX: signal if there were more fields than expected? */
179 static int bch2_inode_unpack_v2(struct bch_inode_unpacked *unpacked,
180 const u8 *in, const u8 *end,
183 unsigned fieldnr = 0;
187 #define x(_name, _bits) \
188 if (fieldnr < nr_fields) { \
189 ret = bch2_varint_decode_fast(in, end, &v[0]); \
195 ret = bch2_varint_decode_fast(in, end, &v[1]); \
206 unpacked->_name = v[0]; \
207 if (v[1] || v[0] != unpacked->_name) \
211 BCH_INODE_FIELDS_v2()
214 /* XXX: signal if there were more fields than expected? */
218 static int bch2_inode_unpack_v3(struct bkey_s_c k,
219 struct bch_inode_unpacked *unpacked)
221 struct bkey_s_c_inode_v3 inode = bkey_s_c_to_inode_v3(k);
222 const u8 *in = inode.v->fields;
223 const u8 *end = bkey_val_end(inode);
224 unsigned nr_fields = INODEv3_NR_FIELDS(inode.v);
225 unsigned fieldnr = 0;
229 unpacked->bi_inum = inode.k->p.offset;
230 unpacked->bi_journal_seq= le64_to_cpu(inode.v->bi_journal_seq);
231 unpacked->bi_hash_seed = inode.v->bi_hash_seed;
232 unpacked->bi_flags = le64_to_cpu(inode.v->bi_flags);
233 unpacked->bi_sectors = le64_to_cpu(inode.v->bi_sectors);
234 unpacked->bi_size = le64_to_cpu(inode.v->bi_size);
235 unpacked->bi_version = le64_to_cpu(inode.v->bi_version);
236 unpacked->bi_mode = INODEv3_MODE(inode.v);
238 #define x(_name, _bits) \
239 if (fieldnr < nr_fields) { \
240 ret = bch2_varint_decode_fast(in, end, &v[0]); \
246 ret = bch2_varint_decode_fast(in, end, &v[1]); \
257 unpacked->_name = v[0]; \
258 if (v[1] || v[0] != unpacked->_name) \
262 BCH_INODE_FIELDS_v3()
265 /* XXX: signal if there were more fields than expected? */
269 static noinline int bch2_inode_unpack_slowpath(struct bkey_s_c k,
270 struct bch_inode_unpacked *unpacked)
272 memset(unpacked, 0, sizeof(*unpacked));
275 case KEY_TYPE_inode: {
276 struct bkey_s_c_inode inode = bkey_s_c_to_inode(k);
278 unpacked->bi_inum = inode.k->p.offset;
279 unpacked->bi_journal_seq= 0;
280 unpacked->bi_hash_seed = inode.v->bi_hash_seed;
281 unpacked->bi_flags = le32_to_cpu(inode.v->bi_flags);
282 unpacked->bi_mode = le16_to_cpu(inode.v->bi_mode);
284 if (INODE_NEW_VARINT(inode.v)) {
285 return bch2_inode_unpack_v2(unpacked, inode.v->fields,
287 INODE_NR_FIELDS(inode.v));
289 return bch2_inode_unpack_v1(inode, unpacked);
293 case KEY_TYPE_inode_v2: {
294 struct bkey_s_c_inode_v2 inode = bkey_s_c_to_inode_v2(k);
296 unpacked->bi_inum = inode.k->p.offset;
297 unpacked->bi_journal_seq= le64_to_cpu(inode.v->bi_journal_seq);
298 unpacked->bi_hash_seed = inode.v->bi_hash_seed;
299 unpacked->bi_flags = le64_to_cpu(inode.v->bi_flags);
300 unpacked->bi_mode = le16_to_cpu(inode.v->bi_mode);
302 return bch2_inode_unpack_v2(unpacked, inode.v->fields,
304 INODEv2_NR_FIELDS(inode.v));
311 int bch2_inode_unpack(struct bkey_s_c k,
312 struct bch_inode_unpacked *unpacked)
314 if (likely(k.k->type == KEY_TYPE_inode_v3))
315 return bch2_inode_unpack_v3(k, unpacked);
316 return bch2_inode_unpack_slowpath(k, unpacked);
319 int bch2_inode_peek(struct btree_trans *trans,
320 struct btree_iter *iter,
321 struct bch_inode_unpacked *inode,
322 subvol_inum inum, unsigned flags)
328 ret = bch2_subvolume_get_snapshot(trans, inum.subvol, &snapshot);
332 bch2_trans_iter_init(trans, iter, BTREE_ID_inodes,
333 SPOS(0, inum.inum, snapshot),
334 flags|BTREE_ITER_CACHED);
335 k = bch2_btree_iter_peek_slot(iter);
340 ret = bkey_is_inode(k.k) ? 0 : -ENOENT;
344 ret = bch2_inode_unpack(k, inode);
350 bch2_trans_iter_exit(trans, iter);
354 int bch2_inode_write(struct btree_trans *trans,
355 struct btree_iter *iter,
356 struct bch_inode_unpacked *inode)
358 struct bkey_inode_buf *inode_p;
360 inode_p = bch2_trans_kmalloc(trans, sizeof(*inode_p));
362 return PTR_ERR(inode_p);
364 bch2_inode_pack_inlined(inode_p, inode);
365 inode_p->inode.k.p.snapshot = iter->snapshot;
366 return bch2_trans_update(trans, iter, &inode_p->inode.k_i, 0);
369 struct bkey_i *bch2_inode_to_v3(struct btree_trans *trans, struct bkey_i *k)
371 struct bch_inode_unpacked u;
372 struct bkey_inode_buf *inode_p;
375 if (!bkey_is_inode(&k->k))
376 return ERR_PTR(-ENOENT);
378 inode_p = bch2_trans_kmalloc(trans, sizeof(*inode_p));
380 return ERR_CAST(inode_p);
382 ret = bch2_inode_unpack(bkey_i_to_s_c(k), &u);
386 bch2_inode_pack(inode_p, &u);
387 return &inode_p->inode.k_i;
390 static int __bch2_inode_invalid(struct bkey_s_c k, struct printbuf *err)
392 struct bch_inode_unpacked unpacked;
395 prt_printf(err, "nonzero k.p.inode");
396 return -BCH_ERR_invalid_bkey;
399 if (k.k->p.offset < BLOCKDEV_INODE_MAX) {
400 prt_printf(err, "fs inode in blockdev range");
401 return -BCH_ERR_invalid_bkey;
404 if (bch2_inode_unpack(k, &unpacked)) {
405 prt_printf(err, "invalid variable length fields");
406 return -BCH_ERR_invalid_bkey;
409 if (unpacked.bi_data_checksum >= BCH_CSUM_OPT_NR + 1) {
410 prt_printf(err, "invalid data checksum type (%u >= %u",
411 unpacked.bi_data_checksum, BCH_CSUM_OPT_NR + 1);
412 return -BCH_ERR_invalid_bkey;
415 if (unpacked.bi_compression >= BCH_COMPRESSION_OPT_NR + 1) {
416 prt_printf(err, "invalid data checksum type (%u >= %u)",
417 unpacked.bi_compression, BCH_COMPRESSION_OPT_NR + 1);
418 return -BCH_ERR_invalid_bkey;
421 if ((unpacked.bi_flags & BCH_INODE_UNLINKED) &&
422 unpacked.bi_nlink != 0) {
423 prt_printf(err, "flagged as unlinked but bi_nlink != 0");
424 return -BCH_ERR_invalid_bkey;
427 if (unpacked.bi_subvol && !S_ISDIR(unpacked.bi_mode)) {
428 prt_printf(err, "subvolume root but not a directory");
429 return -BCH_ERR_invalid_bkey;
435 int bch2_inode_invalid(const struct bch_fs *c, struct bkey_s_c k,
436 unsigned flags, struct printbuf *err)
438 struct bkey_s_c_inode inode = bkey_s_c_to_inode(k);
440 if (bkey_val_bytes(k.k) < sizeof(*inode.v)) {
441 prt_printf(err, "incorrect value size (%zu < %zu)",
442 bkey_val_bytes(k.k), sizeof(*inode.v));
443 return -BCH_ERR_invalid_bkey;
446 if (INODE_STR_HASH(inode.v) >= BCH_STR_HASH_NR) {
447 prt_printf(err, "invalid str hash type (%llu >= %u)",
448 INODE_STR_HASH(inode.v), BCH_STR_HASH_NR);
449 return -BCH_ERR_invalid_bkey;
452 return __bch2_inode_invalid(k, err);
455 int bch2_inode_v2_invalid(const struct bch_fs *c, struct bkey_s_c k,
456 unsigned flags, struct printbuf *err)
458 struct bkey_s_c_inode_v2 inode = bkey_s_c_to_inode_v2(k);
460 if (bkey_val_bytes(k.k) < sizeof(*inode.v)) {
461 prt_printf(err, "incorrect value size (%zu < %zu)",
462 bkey_val_bytes(k.k), sizeof(*inode.v));
463 return -BCH_ERR_invalid_bkey;
466 if (INODEv2_STR_HASH(inode.v) >= BCH_STR_HASH_NR) {
467 prt_printf(err, "invalid str hash type (%llu >= %u)",
468 INODEv2_STR_HASH(inode.v), BCH_STR_HASH_NR);
469 return -BCH_ERR_invalid_bkey;
472 return __bch2_inode_invalid(k, err);
475 int bch2_inode_v3_invalid(const struct bch_fs *c, struct bkey_s_c k,
476 unsigned flags, struct printbuf *err)
478 struct bkey_s_c_inode_v3 inode = bkey_s_c_to_inode_v3(k);
480 if (bkey_val_bytes(k.k) < sizeof(*inode.v)) {
481 prt_printf(err, "incorrect value size (%zu < %zu)",
482 bkey_val_bytes(k.k), sizeof(*inode.v));
483 return -BCH_ERR_invalid_bkey;
486 if (INODEv3_FIELDS_START(inode.v) < INODEv3_FIELDS_START_INITIAL ||
487 INODEv3_FIELDS_START(inode.v) > bkey_val_u64s(inode.k)) {
488 prt_printf(err, "invalid fields_start (got %llu, min %u max %zu)",
489 INODEv3_FIELDS_START(inode.v),
490 INODEv3_FIELDS_START_INITIAL,
491 bkey_val_u64s(inode.k));
492 return -BCH_ERR_invalid_bkey;
495 if (INODEv3_STR_HASH(inode.v) >= BCH_STR_HASH_NR) {
496 prt_printf(err, "invalid str hash type (%llu >= %u)",
497 INODEv3_STR_HASH(inode.v), BCH_STR_HASH_NR);
498 return -BCH_ERR_invalid_bkey;
501 return __bch2_inode_invalid(k, err);
504 static void __bch2_inode_unpacked_to_text(struct printbuf *out,
505 struct bch_inode_unpacked *inode)
507 prt_printf(out, "mode %o flags %x journal_seq %llu bi_size %llu bi_sectors %llu bi_version %llu",
508 inode->bi_mode, inode->bi_flags,
509 inode->bi_journal_seq,
514 #define x(_name, _bits) \
515 prt_printf(out, " "#_name " %llu", (u64) inode->_name);
516 BCH_INODE_FIELDS_v3()
520 void bch2_inode_unpacked_to_text(struct printbuf *out, struct bch_inode_unpacked *inode)
522 prt_printf(out, "inum: %llu ", inode->bi_inum);
523 __bch2_inode_unpacked_to_text(out, inode);
526 void bch2_inode_to_text(struct printbuf *out, struct bch_fs *c, struct bkey_s_c k)
528 struct bch_inode_unpacked inode;
530 if (bch2_inode_unpack(k, &inode)) {
531 prt_printf(out, "(unpack error)");
535 __bch2_inode_unpacked_to_text(out, &inode);
538 int bch2_inode_generation_invalid(const struct bch_fs *c, struct bkey_s_c k,
539 unsigned flags, struct printbuf *err)
542 prt_printf(err, "nonzero k.p.inode");
543 return -BCH_ERR_invalid_bkey;
546 if (bkey_val_bytes(k.k) != sizeof(struct bch_inode_generation)) {
547 prt_printf(err, "incorrect value size (%zu != %zu)",
548 bkey_val_bytes(k.k), sizeof(struct bch_inode_generation));
549 return -BCH_ERR_invalid_bkey;
555 void bch2_inode_generation_to_text(struct printbuf *out, struct bch_fs *c,
558 struct bkey_s_c_inode_generation gen = bkey_s_c_to_inode_generation(k);
560 prt_printf(out, "generation: %u", le32_to_cpu(gen.v->bi_generation));
563 void bch2_inode_init_early(struct bch_fs *c,
564 struct bch_inode_unpacked *inode_u)
566 enum bch_str_hash_type str_hash =
567 bch2_str_hash_opt_to_type(c, c->opts.str_hash);
569 memset(inode_u, 0, sizeof(*inode_u));
572 inode_u->bi_flags |= str_hash << INODE_STR_HASH_OFFSET;
573 get_random_bytes(&inode_u->bi_hash_seed,
574 sizeof(inode_u->bi_hash_seed));
577 void bch2_inode_init_late(struct bch_inode_unpacked *inode_u, u64 now,
578 uid_t uid, gid_t gid, umode_t mode, dev_t rdev,
579 struct bch_inode_unpacked *parent)
581 inode_u->bi_mode = mode;
582 inode_u->bi_uid = uid;
583 inode_u->bi_gid = gid;
584 inode_u->bi_dev = rdev;
585 inode_u->bi_atime = now;
586 inode_u->bi_mtime = now;
587 inode_u->bi_ctime = now;
588 inode_u->bi_otime = now;
590 if (parent && parent->bi_mode & S_ISGID) {
591 inode_u->bi_gid = parent->bi_gid;
593 inode_u->bi_mode |= S_ISGID;
597 #define x(_name, ...) inode_u->bi_##_name = parent->bi_##_name;
603 void bch2_inode_init(struct bch_fs *c, struct bch_inode_unpacked *inode_u,
604 uid_t uid, gid_t gid, umode_t mode, dev_t rdev,
605 struct bch_inode_unpacked *parent)
607 bch2_inode_init_early(c, inode_u);
608 bch2_inode_init_late(inode_u, bch2_current_time(c),
609 uid, gid, mode, rdev, parent);
612 static inline u32 bkey_generation(struct bkey_s_c k)
616 case KEY_TYPE_inode_v2:
618 case KEY_TYPE_inode_generation:
619 return le32_to_cpu(bkey_s_c_to_inode_generation(k).v->bi_generation);
626 * This just finds an empty slot:
628 int bch2_inode_create(struct btree_trans *trans,
629 struct btree_iter *iter,
630 struct bch_inode_unpacked *inode_u,
631 u32 snapshot, u64 cpu)
633 struct bch_fs *c = trans->c;
635 u64 min, max, start, pos, *hint;
637 unsigned bits = (c->opts.inodes_32bit ? 31 : 63);
639 if (c->opts.shard_inode_numbers) {
640 bits -= c->inode_shard_bits;
643 max = (cpu << bits) | ~(ULLONG_MAX << bits);
645 min = max_t(u64, min, BLOCKDEV_INODE_MAX);
646 hint = c->unused_inode_hints + cpu;
648 min = BLOCKDEV_INODE_MAX;
649 max = ~(ULLONG_MAX << bits);
650 hint = c->unused_inode_hints;
653 start = READ_ONCE(*hint);
655 if (start >= max || start < min)
659 bch2_trans_iter_init(trans, iter, BTREE_ID_inodes, POS(0, pos),
660 BTREE_ITER_ALL_SNAPSHOTS|
663 while ((k = bch2_btree_iter_peek(iter)).k &&
664 !(ret = bkey_err(k)) &&
665 bkey_lt(k.k->p, POS(0, max))) {
666 if (pos < iter->pos.offset)
670 * We don't need to iterate over keys in every snapshot once
671 * we've found just one:
673 pos = iter->pos.offset + 1;
674 bch2_btree_iter_set_pos(iter, POS(0, pos));
677 if (!ret && pos < max)
680 if (!ret && start == min)
681 ret = -BCH_ERR_ENOSPC_inode_create;
684 bch2_trans_iter_exit(trans, iter);
688 /* Retry from start */
690 bch2_btree_iter_set_pos(iter, POS(0, pos));
693 bch2_btree_iter_set_pos(iter, SPOS(0, pos, snapshot));
694 k = bch2_btree_iter_peek_slot(iter);
697 bch2_trans_iter_exit(trans, iter);
701 *hint = k.k->p.offset;
702 inode_u->bi_inum = k.k->p.offset;
703 inode_u->bi_generation = bkey_generation(k);
707 static int bch2_inode_delete_keys(struct btree_trans *trans,
708 subvol_inum inum, enum btree_id id)
710 struct btree_iter iter;
712 struct bkey_i delete;
717 * We're never going to be deleting partial extents, no need to use an
720 bch2_trans_iter_init(trans, &iter, id, POS(inum.inum, 0),
721 BTREE_ITER_INTENT|BTREE_ITER_NOT_EXTENTS);
724 bch2_trans_begin(trans);
726 ret = bch2_subvolume_get_snapshot(trans, inum.subvol, &snapshot);
730 bch2_btree_iter_set_snapshot(&iter, snapshot);
732 k = bch2_btree_iter_peek_upto(&iter, POS(inum.inum, U64_MAX));
740 bkey_init(&delete.k);
741 delete.k.p = iter.pos;
743 ret = bch2_trans_update(trans, &iter, &delete, 0) ?:
744 bch2_trans_commit(trans, NULL, NULL,
745 BTREE_INSERT_NOFAIL);
747 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
751 bch2_trans_iter_exit(trans, &iter);
755 int bch2_inode_rm(struct bch_fs *c, subvol_inum inum)
757 struct btree_trans trans;
758 struct btree_iter iter = { NULL };
759 struct bkey_i_inode_generation delete;
760 struct bch_inode_unpacked inode_u;
765 bch2_trans_init(&trans, c, 0, 1024);
768 * If this was a directory, there shouldn't be any real dirents left -
769 * but there could be whiteouts (from hash collisions) that we should
772 * XXX: the dirent could ideally would delete whiteouts when they're no
775 ret = bch2_inode_delete_keys(&trans, inum, BTREE_ID_extents) ?:
776 bch2_inode_delete_keys(&trans, inum, BTREE_ID_xattrs) ?:
777 bch2_inode_delete_keys(&trans, inum, BTREE_ID_dirents);
781 bch2_trans_begin(&trans);
783 ret = bch2_subvolume_get_snapshot(&trans, inum.subvol, &snapshot);
787 bch2_trans_iter_init(&trans, &iter, BTREE_ID_inodes,
788 SPOS(0, inum.inum, snapshot),
789 BTREE_ITER_INTENT|BTREE_ITER_CACHED);
790 k = bch2_btree_iter_peek_slot(&iter);
796 if (!bkey_is_inode(k.k)) {
797 bch2_fs_inconsistent(trans.c,
798 "inode %llu:%u not found when deleting",
799 inum.inum, snapshot);
804 bch2_inode_unpack(k, &inode_u);
806 /* Subvolume root? */
807 BUG_ON(inode_u.bi_subvol);
809 bkey_inode_generation_init(&delete.k_i);
810 delete.k.p = iter.pos;
811 delete.v.bi_generation = cpu_to_le32(inode_u.bi_generation + 1);
813 ret = bch2_trans_update(&trans, &iter, &delete.k_i, 0) ?:
814 bch2_trans_commit(&trans, NULL, NULL,
815 BTREE_INSERT_NOFAIL);
817 bch2_trans_iter_exit(&trans, &iter);
818 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
821 bch2_trans_exit(&trans);
825 int bch2_inode_find_by_inum_trans(struct btree_trans *trans,
827 struct bch_inode_unpacked *inode)
829 struct btree_iter iter;
832 ret = bch2_inode_peek(trans, &iter, inode, inum, 0);
834 bch2_trans_iter_exit(trans, &iter);
838 int bch2_inode_find_by_inum(struct bch_fs *c, subvol_inum inum,
839 struct bch_inode_unpacked *inode)
841 return bch2_trans_do(c, NULL, NULL, 0,
842 bch2_inode_find_by_inum_trans(&trans, inum, inode));
845 int bch2_inode_nlink_inc(struct bch_inode_unpacked *bi)
847 if (bi->bi_flags & BCH_INODE_UNLINKED)
848 bi->bi_flags &= ~BCH_INODE_UNLINKED;
850 if (bi->bi_nlink == U32_MAX)
859 void bch2_inode_nlink_dec(struct btree_trans *trans, struct bch_inode_unpacked *bi)
861 if (bi->bi_nlink && (bi->bi_flags & BCH_INODE_UNLINKED)) {
862 bch2_trans_inconsistent(trans, "inode %llu unlinked but link count nonzero",
867 if (bi->bi_flags & BCH_INODE_UNLINKED) {
868 bch2_trans_inconsistent(trans, "inode %llu link count underflow", bi->bi_inum);
875 bi->bi_flags |= BCH_INODE_UNLINKED;
878 struct bch_opts bch2_inode_opts_to_opts(struct bch_inode_unpacked *inode)
880 struct bch_opts ret = { 0 };
881 #define x(_name, _bits) \
882 if (inode->bi_##_name) \
883 opt_set(ret, _name, inode->bi_##_name - 1);
889 void bch2_inode_opts_get(struct bch_io_opts *opts, struct bch_fs *c,
890 struct bch_inode_unpacked *inode)
892 #define x(_name, _bits) opts->_name = inode_opt_get(c, inode, _name);
897 opts->compression = opts->background_compression = opts->data_checksum = opts->erasure_code = 0;