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 void bch2_inode_pack(struct bch_fs *c,
64 struct bkey_inode_buf *packed,
65 const struct bch_inode_unpacked *inode)
67 struct bkey_i_inode_v2 *k = &packed->inode;
68 u8 *out = k->v.fields;
69 u8 *end = (void *) &packed[1];
70 u8 *last_nonzero_field = out;
71 unsigned nr_fields = 0, last_nonzero_fieldnr = 0;
75 bkey_inode_v2_init(&packed->inode.k_i);
76 packed->inode.k.p.offset = inode->bi_inum;
77 packed->inode.v.bi_journal_seq = cpu_to_le64(inode->bi_journal_seq);
78 packed->inode.v.bi_hash_seed = inode->bi_hash_seed;
79 packed->inode.v.bi_flags = cpu_to_le64(inode->bi_flags);
80 packed->inode.v.bi_flags = cpu_to_le64(inode->bi_flags);
81 packed->inode.v.bi_mode = cpu_to_le16(inode->bi_mode);
83 #define x(_name, _bits) \
87 ret = bch2_varint_encode_fast(out, inode->_name); \
93 last_nonzero_field = out; \
94 last_nonzero_fieldnr = nr_fields; \
106 out = last_nonzero_field;
107 nr_fields = last_nonzero_fieldnr;
109 bytes = out - (u8 *) &packed->inode.v;
110 set_bkey_val_bytes(&packed->inode.k, bytes);
111 memset_u64s_tail(&packed->inode.v, 0, bytes);
113 SET_INODEv2_NR_FIELDS(&k->v, nr_fields);
115 if (IS_ENABLED(CONFIG_BCACHEFS_DEBUG)) {
116 struct bch_inode_unpacked unpacked;
118 int ret = bch2_inode_unpack(bkey_i_to_s_c(&packed->inode.k_i),
121 BUG_ON(unpacked.bi_inum != inode->bi_inum);
122 BUG_ON(unpacked.bi_hash_seed != inode->bi_hash_seed);
123 BUG_ON(unpacked.bi_mode != inode->bi_mode);
125 #define x(_name, _bits) if (unpacked._name != inode->_name) \
126 panic("unpacked %llu should be %llu", \
127 (u64) unpacked._name, (u64) inode->_name);
133 static noinline int bch2_inode_unpack_v1(struct bkey_s_c_inode inode,
134 struct bch_inode_unpacked *unpacked)
136 const u8 *in = inode.v->fields;
137 const u8 *end = bkey_val_end(inode);
139 unsigned fieldnr = 0, field_bits;
142 #define x(_name, _bits) \
143 if (fieldnr++ == INODE_NR_FIELDS(inode.v)) { \
144 unsigned offset = offsetof(struct bch_inode_unpacked, _name);\
145 memset((void *) unpacked + offset, 0, \
146 sizeof(*unpacked) - offset); \
150 ret = inode_decode_field(in, end, field, &field_bits); \
154 if (field_bits > sizeof(unpacked->_name) * 8) \
157 unpacked->_name = field[1]; \
163 /* XXX: signal if there were more fields than expected? */
167 static int bch2_inode_unpack_v2(struct bch_inode_unpacked *unpacked,
168 const u8 *in, const u8 *end,
171 unsigned fieldnr = 0;
175 #define x(_name, _bits) \
176 if (fieldnr < nr_fields) { \
177 ret = bch2_varint_decode_fast(in, end, &v[0]); \
183 ret = bch2_varint_decode_fast(in, end, &v[1]); \
194 unpacked->_name = v[0]; \
195 if (v[1] || v[0] != unpacked->_name) \
202 /* XXX: signal if there were more fields than expected? */
206 int bch2_inode_unpack(struct bkey_s_c k,
207 struct bch_inode_unpacked *unpacked)
210 case KEY_TYPE_inode: {
211 struct bkey_s_c_inode inode = bkey_s_c_to_inode(k);
213 unpacked->bi_inum = inode.k->p.offset;
214 unpacked->bi_journal_seq= 0;
215 unpacked->bi_hash_seed = inode.v->bi_hash_seed;
216 unpacked->bi_flags = le32_to_cpu(inode.v->bi_flags);
217 unpacked->bi_mode = le16_to_cpu(inode.v->bi_mode);
219 if (INODE_NEW_VARINT(inode.v)) {
220 return bch2_inode_unpack_v2(unpacked, inode.v->fields,
222 INODE_NR_FIELDS(inode.v));
224 return bch2_inode_unpack_v1(inode, unpacked);
228 case KEY_TYPE_inode_v2: {
229 struct bkey_s_c_inode_v2 inode = bkey_s_c_to_inode_v2(k);
231 unpacked->bi_inum = inode.k->p.offset;
232 unpacked->bi_journal_seq= le64_to_cpu(inode.v->bi_journal_seq);
233 unpacked->bi_hash_seed = inode.v->bi_hash_seed;
234 unpacked->bi_flags = le64_to_cpu(inode.v->bi_flags);
235 unpacked->bi_mode = le16_to_cpu(inode.v->bi_mode);
237 return bch2_inode_unpack_v2(unpacked, inode.v->fields,
239 INODEv2_NR_FIELDS(inode.v));
246 int bch2_inode_peek(struct btree_trans *trans,
247 struct btree_iter *iter,
248 struct bch_inode_unpacked *inode,
249 subvol_inum inum, unsigned flags)
255 ret = bch2_subvolume_get_snapshot(trans, inum.subvol, &snapshot);
259 bch2_trans_iter_init(trans, iter, BTREE_ID_inodes,
260 SPOS(0, inum.inum, snapshot),
261 flags|BTREE_ITER_CACHED);
262 k = bch2_btree_iter_peek_slot(iter);
267 ret = bkey_is_inode(k.k) ? 0 : -ENOENT;
271 ret = bch2_inode_unpack(k, inode);
277 bch2_trans_iter_exit(trans, iter);
281 int bch2_inode_write(struct btree_trans *trans,
282 struct btree_iter *iter,
283 struct bch_inode_unpacked *inode)
285 struct bkey_inode_buf *inode_p;
287 inode_p = bch2_trans_kmalloc(trans, sizeof(*inode_p));
289 return PTR_ERR(inode_p);
291 bch2_inode_pack(trans->c, inode_p, inode);
292 inode_p->inode.k.p.snapshot = iter->snapshot;
293 return bch2_trans_update(trans, iter, &inode_p->inode.k_i, 0);
296 static int __bch2_inode_invalid(struct bkey_s_c k, struct printbuf *err)
298 struct bch_inode_unpacked unpacked;
301 prt_printf(err, "nonzero k.p.inode");
305 if (k.k->p.offset < BLOCKDEV_INODE_MAX) {
306 prt_printf(err, "fs inode in blockdev range");
310 if (bch2_inode_unpack(k, &unpacked)){
311 prt_printf(err, "invalid variable length fields");
315 if (unpacked.bi_data_checksum >= BCH_CSUM_OPT_NR + 1) {
316 prt_printf(err, "invalid data checksum type (%u >= %u",
317 unpacked.bi_data_checksum, BCH_CSUM_OPT_NR + 1);
321 if (unpacked.bi_compression >= BCH_COMPRESSION_OPT_NR + 1) {
322 prt_printf(err, "invalid data checksum type (%u >= %u)",
323 unpacked.bi_compression, BCH_COMPRESSION_OPT_NR + 1);
327 if ((unpacked.bi_flags & BCH_INODE_UNLINKED) &&
328 unpacked.bi_nlink != 0) {
329 prt_printf(err, "flagged as unlinked but bi_nlink != 0");
333 if (unpacked.bi_subvol && !S_ISDIR(unpacked.bi_mode)) {
334 prt_printf(err, "subvolume root but not a directory");
341 int bch2_inode_invalid(const struct bch_fs *c, struct bkey_s_c k,
342 int rw, struct printbuf *err)
344 struct bkey_s_c_inode inode = bkey_s_c_to_inode(k);
346 if (bkey_val_bytes(k.k) < sizeof(*inode.v)) {
347 prt_printf(err, "incorrect value size (%zu < %zu)",
348 bkey_val_bytes(k.k), sizeof(*inode.v));
352 if (INODE_STR_HASH(inode.v) >= BCH_STR_HASH_NR) {
353 prt_printf(err, "invalid str hash type (%llu >= %u)",
354 INODE_STR_HASH(inode.v), BCH_STR_HASH_NR);
358 return __bch2_inode_invalid(k, err);
361 int bch2_inode_v2_invalid(const struct bch_fs *c, struct bkey_s_c k,
362 int rw, struct printbuf *err)
364 struct bkey_s_c_inode_v2 inode = bkey_s_c_to_inode_v2(k);
366 if (bkey_val_bytes(k.k) < sizeof(*inode.v)) {
367 prt_printf(err, "incorrect value size (%zu < %zu)",
368 bkey_val_bytes(k.k), sizeof(*inode.v));
372 if (INODEv2_STR_HASH(inode.v) >= BCH_STR_HASH_NR) {
373 prt_printf(err, "invalid str hash type (%llu >= %u)",
374 INODEv2_STR_HASH(inode.v), BCH_STR_HASH_NR);
378 return __bch2_inode_invalid(k, err);
381 static void __bch2_inode_unpacked_to_text(struct printbuf *out, struct bch_inode_unpacked *inode)
383 prt_printf(out, "mode %o flags %x journal_seq %llu",
384 inode->bi_mode, inode->bi_flags,
385 inode->bi_journal_seq);
387 #define x(_name, _bits) \
388 prt_printf(out, " "#_name " %llu", (u64) inode->_name);
393 void bch2_inode_unpacked_to_text(struct printbuf *out, struct bch_inode_unpacked *inode)
395 prt_printf(out, "inum: %llu ", inode->bi_inum);
396 __bch2_inode_unpacked_to_text(out, inode);
399 void bch2_inode_to_text(struct printbuf *out, struct bch_fs *c,
402 struct bch_inode_unpacked inode;
404 if (bch2_inode_unpack(k, &inode)) {
405 prt_printf(out, "(unpack error)");
409 __bch2_inode_unpacked_to_text(out, &inode);
412 int bch2_inode_generation_invalid(const struct bch_fs *c, struct bkey_s_c k,
413 int rw, struct printbuf *err)
416 prt_printf(err, "nonzero k.p.inode");
420 if (bkey_val_bytes(k.k) != sizeof(struct bch_inode_generation)) {
421 prt_printf(err, "incorrect value size (%zu != %zu)",
422 bkey_val_bytes(k.k), sizeof(struct bch_inode_generation));
429 void bch2_inode_generation_to_text(struct printbuf *out, struct bch_fs *c,
432 struct bkey_s_c_inode_generation gen = bkey_s_c_to_inode_generation(k);
434 prt_printf(out, "generation: %u", le32_to_cpu(gen.v->bi_generation));
437 void bch2_inode_init_early(struct bch_fs *c,
438 struct bch_inode_unpacked *inode_u)
440 enum bch_str_hash_type str_hash =
441 bch2_str_hash_opt_to_type(c, c->opts.str_hash);
443 memset(inode_u, 0, sizeof(*inode_u));
446 inode_u->bi_flags |= str_hash << INODE_STR_HASH_OFFSET;
447 get_random_bytes(&inode_u->bi_hash_seed,
448 sizeof(inode_u->bi_hash_seed));
451 void bch2_inode_init_late(struct bch_inode_unpacked *inode_u, u64 now,
452 uid_t uid, gid_t gid, umode_t mode, dev_t rdev,
453 struct bch_inode_unpacked *parent)
455 inode_u->bi_mode = mode;
456 inode_u->bi_uid = uid;
457 inode_u->bi_gid = gid;
458 inode_u->bi_dev = rdev;
459 inode_u->bi_atime = now;
460 inode_u->bi_mtime = now;
461 inode_u->bi_ctime = now;
462 inode_u->bi_otime = now;
464 if (parent && parent->bi_mode & S_ISGID) {
465 inode_u->bi_gid = parent->bi_gid;
467 inode_u->bi_mode |= S_ISGID;
471 #define x(_name, ...) inode_u->bi_##_name = parent->bi_##_name;
477 void bch2_inode_init(struct bch_fs *c, struct bch_inode_unpacked *inode_u,
478 uid_t uid, gid_t gid, umode_t mode, dev_t rdev,
479 struct bch_inode_unpacked *parent)
481 bch2_inode_init_early(c, inode_u);
482 bch2_inode_init_late(inode_u, bch2_current_time(c),
483 uid, gid, mode, rdev, parent);
486 static inline u32 bkey_generation(struct bkey_s_c k)
490 case KEY_TYPE_inode_v2:
492 case KEY_TYPE_inode_generation:
493 return le32_to_cpu(bkey_s_c_to_inode_generation(k).v->bi_generation);
500 * This just finds an empty slot:
502 int bch2_inode_create(struct btree_trans *trans,
503 struct btree_iter *iter,
504 struct bch_inode_unpacked *inode_u,
505 u32 snapshot, u64 cpu)
507 struct bch_fs *c = trans->c;
509 u64 min, max, start, pos, *hint;
511 unsigned bits = (c->opts.inodes_32bit ? 31 : 63);
513 if (c->opts.shard_inode_numbers) {
514 bits -= c->inode_shard_bits;
517 max = (cpu << bits) | ~(ULLONG_MAX << bits);
519 min = max_t(u64, min, BLOCKDEV_INODE_MAX);
520 hint = c->unused_inode_hints + cpu;
522 min = BLOCKDEV_INODE_MAX;
523 max = ~(ULLONG_MAX << bits);
524 hint = c->unused_inode_hints;
527 start = READ_ONCE(*hint);
529 if (start >= max || start < min)
533 bch2_trans_iter_init(trans, iter, BTREE_ID_inodes, POS(0, pos),
534 BTREE_ITER_ALL_SNAPSHOTS|
537 while ((k = bch2_btree_iter_peek(iter)).k &&
538 !(ret = bkey_err(k)) &&
539 bkey_cmp(k.k->p, POS(0, max)) < 0) {
540 while (pos < iter->pos.offset) {
541 if (!bch2_btree_key_cache_find(c, BTREE_ID_inodes, POS(0, pos)))
547 if (k.k->p.snapshot == snapshot &&
548 !bkey_is_inode(k.k) &&
549 !bch2_btree_key_cache_find(c, BTREE_ID_inodes, SPOS(0, pos, snapshot))) {
550 bch2_btree_iter_advance(iter);
555 * We don't need to iterate over keys in every snapshot once
556 * we've found just one:
558 pos = iter->pos.offset + 1;
559 bch2_btree_iter_set_pos(iter, POS(0, pos));
562 while (!ret && pos < max) {
563 if (!bch2_btree_key_cache_find(c, BTREE_ID_inodes, POS(0, pos)))
569 if (!ret && start == min)
573 bch2_trans_iter_exit(trans, iter);
577 /* Retry from start */
579 bch2_btree_iter_set_pos(iter, POS(0, pos));
582 bch2_btree_iter_set_pos(iter, SPOS(0, pos, snapshot));
583 k = bch2_btree_iter_peek_slot(iter);
586 bch2_trans_iter_exit(trans, iter);
590 /* We may have raced while the iterator wasn't pointing at pos: */
591 if (bkey_is_inode(k.k) ||
592 bch2_btree_key_cache_find(c, BTREE_ID_inodes, k.k->p))
595 *hint = k.k->p.offset;
596 inode_u->bi_inum = k.k->p.offset;
597 inode_u->bi_generation = bkey_generation(k);
601 static int bch2_inode_delete_keys(struct btree_trans *trans,
602 subvol_inum inum, enum btree_id id)
604 struct btree_iter iter;
606 struct bkey_i delete;
611 * We're never going to be deleting extents, no need to use an extent
614 bch2_trans_iter_init(trans, &iter, id, POS(inum.inum, 0),
615 BTREE_ITER_NOT_EXTENTS|
619 bch2_trans_begin(trans);
621 ret = bch2_subvolume_get_snapshot(trans, inum.subvol, &snapshot);
625 bch2_btree_iter_set_snapshot(&iter, snapshot);
627 k = bch2_btree_iter_peek_upto(&iter, POS(inum.inum, U64_MAX));
635 bkey_init(&delete.k);
636 delete.k.p = iter.pos;
638 ret = bch2_trans_update(trans, &iter, &delete, 0) ?:
639 bch2_trans_commit(trans, NULL, NULL,
640 BTREE_INSERT_NOFAIL);
642 if (ret && ret != -EINTR)
646 bch2_trans_iter_exit(trans, &iter);
650 int bch2_inode_rm(struct bch_fs *c, subvol_inum inum)
652 struct btree_trans trans;
653 struct btree_iter iter = { NULL };
654 struct bkey_i_inode_generation delete;
655 struct bch_inode_unpacked inode_u;
660 bch2_trans_init(&trans, c, 0, 1024);
663 * If this was a directory, there shouldn't be any real dirents left -
664 * but there could be whiteouts (from hash collisions) that we should
667 * XXX: the dirent could ideally would delete whiteouts when they're no
670 ret = bch2_inode_delete_keys(&trans, inum, BTREE_ID_extents) ?:
671 bch2_inode_delete_keys(&trans, inum, BTREE_ID_xattrs) ?:
672 bch2_inode_delete_keys(&trans, inum, BTREE_ID_dirents);
676 bch2_trans_begin(&trans);
678 ret = bch2_subvolume_get_snapshot(&trans, inum.subvol, &snapshot);
682 bch2_trans_iter_init(&trans, &iter, BTREE_ID_inodes,
683 SPOS(0, inum.inum, snapshot),
684 BTREE_ITER_INTENT|BTREE_ITER_CACHED);
685 k = bch2_btree_iter_peek_slot(&iter);
691 if (!bkey_is_inode(k.k)) {
692 bch2_fs_inconsistent(trans.c,
693 "inode %llu not found when deleting",
699 bch2_inode_unpack(k, &inode_u);
701 /* Subvolume root? */
702 BUG_ON(inode_u.bi_subvol);
704 bkey_inode_generation_init(&delete.k_i);
705 delete.k.p = iter.pos;
706 delete.v.bi_generation = cpu_to_le32(inode_u.bi_generation + 1);
708 ret = bch2_trans_update(&trans, &iter, &delete.k_i, 0) ?:
709 bch2_trans_commit(&trans, NULL, NULL,
710 BTREE_INSERT_NOFAIL);
712 bch2_trans_iter_exit(&trans, &iter);
716 bch2_trans_exit(&trans);
720 int bch2_inode_find_by_inum_trans(struct btree_trans *trans,
722 struct bch_inode_unpacked *inode)
724 struct btree_iter iter;
727 ret = bch2_inode_peek(trans, &iter, inode, inum, 0);
729 bch2_trans_iter_exit(trans, &iter);
733 int bch2_inode_find_by_inum(struct bch_fs *c, subvol_inum inum,
734 struct bch_inode_unpacked *inode)
736 return bch2_trans_do(c, NULL, NULL, 0,
737 bch2_inode_find_by_inum_trans(&trans, inum, inode));
740 int bch2_inode_nlink_inc(struct bch_inode_unpacked *bi)
742 if (bi->bi_flags & BCH_INODE_UNLINKED)
743 bi->bi_flags &= ~BCH_INODE_UNLINKED;
745 if (bi->bi_nlink == U32_MAX)
754 void bch2_inode_nlink_dec(struct btree_trans *trans, struct bch_inode_unpacked *bi)
756 if (bi->bi_nlink && (bi->bi_flags & BCH_INODE_UNLINKED)) {
757 bch2_trans_inconsistent(trans, "inode %llu unlinked but link count nonzero",
762 if (bi->bi_flags & BCH_INODE_UNLINKED) {
763 bch2_trans_inconsistent(trans, "inode %llu link count underflow", bi->bi_inum);
770 bi->bi_flags |= BCH_INODE_UNLINKED;