1 // SPDX-License-Identifier: GPL-2.0
4 #include "bkey_methods.h"
5 #include "btree_update.h"
11 #include <linux/random.h>
13 #include <asm/unaligned.h>
15 const char * const bch2_inode_opts[] = {
16 #define x(name, ...) #name,
22 static const u8 byte_table[8] = { 1, 2, 3, 4, 6, 8, 10, 13 };
23 static const u8 bits_table[8] = {
34 static int inode_encode_field(u8 *out, u8 *end, u64 hi, u64 lo)
36 __be64 in[2] = { cpu_to_be64(hi), cpu_to_be64(lo), };
37 unsigned shift, bytes, bits = likely(!hi)
41 for (shift = 1; shift <= 8; shift++)
42 if (bits < bits_table[shift - 1])
47 bytes = byte_table[shift - 1];
49 BUG_ON(out + bytes > end);
51 memcpy(out, (u8 *) in + 16 - bytes, bytes);
52 *out |= (1 << 8) >> shift;
57 static int inode_decode_field(const u8 *in, const u8 *end,
58 u64 out[2], unsigned *out_bits)
60 __be64 be[2] = { 0, 0 };
61 unsigned bytes, shift;
71 * position of highest set bit indicates number of bytes:
72 * shift = number of bits to remove in high byte:
74 shift = 8 - __fls(*in); /* 1 <= shift <= 8 */
75 bytes = byte_table[shift - 1];
80 p = (u8 *) be + 16 - bytes;
82 *p ^= (1 << 8) >> shift;
84 out[0] = be64_to_cpu(be[0]);
85 out[1] = be64_to_cpu(be[1]);
86 *out_bits = out[0] ? 64 + fls64(out[0]) : fls64(out[1]);
91 void bch2_inode_pack(struct bkey_inode_buf *packed,
92 const struct bch_inode_unpacked *inode)
94 u8 *out = packed->inode.v.fields;
95 u8 *end = (void *) &packed[1];
96 u8 *last_nonzero_field = out;
97 unsigned nr_fields = 0, last_nonzero_fieldnr = 0;
99 bkey_inode_init(&packed->inode.k_i);
100 packed->inode.k.p.inode = inode->bi_inum;
101 packed->inode.v.bi_hash_seed = inode->bi_hash_seed;
102 packed->inode.v.bi_flags = cpu_to_le32(inode->bi_flags);
103 packed->inode.v.bi_mode = cpu_to_le16(inode->bi_mode);
105 #define x(_name, _bits) \
106 out += inode_encode_field(out, end, 0, inode->_name); \
109 if (inode->_name) { \
110 last_nonzero_field = out; \
111 last_nonzero_fieldnr = nr_fields; \
117 out = last_nonzero_field;
118 nr_fields = last_nonzero_fieldnr;
120 set_bkey_val_bytes(&packed->inode.k, out - (u8 *) &packed->inode.v);
122 (u8 *) &packed->inode.v +
123 bkey_val_bytes(&packed->inode.k) - out);
125 SET_INODE_NR_FIELDS(&packed->inode.v, nr_fields);
127 if (IS_ENABLED(CONFIG_BCACHEFS_DEBUG)) {
128 struct bch_inode_unpacked unpacked;
130 int ret = bch2_inode_unpack(inode_i_to_s_c(&packed->inode),
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_mode != inode->bi_mode);
137 #define x(_name, _bits) BUG_ON(unpacked._name != inode->_name);
143 int bch2_inode_unpack(struct bkey_s_c_inode inode,
144 struct bch_inode_unpacked *unpacked)
146 const u8 *in = inode.v->fields;
147 const u8 *end = (void *) inode.v + bkey_val_bytes(inode.k);
149 unsigned fieldnr = 0, field_bits;
152 unpacked->bi_inum = inode.k->p.inode;
153 unpacked->bi_hash_seed = inode.v->bi_hash_seed;
154 unpacked->bi_flags = le32_to_cpu(inode.v->bi_flags);
155 unpacked->bi_mode = le16_to_cpu(inode.v->bi_mode);
157 #define x(_name, _bits) \
158 if (fieldnr++ == INODE_NR_FIELDS(inode.v)) { \
159 memset(&unpacked->_name, 0, \
160 sizeof(*unpacked) - \
161 offsetof(struct bch_inode_unpacked, _name)); \
165 ret = inode_decode_field(in, end, field, &field_bits); \
169 if (field_bits > sizeof(unpacked->_name) * 8) \
172 unpacked->_name = field[1]; \
178 /* XXX: signal if there were more fields than expected? */
183 struct btree_iter *bch2_inode_peek(struct btree_trans *trans,
184 struct bch_inode_unpacked *inode,
185 u64 inum, unsigned flags)
187 struct btree_iter *iter;
191 iter = bch2_trans_get_iter(trans, BTREE_ID_INODES, POS(inum, 0),
192 BTREE_ITER_SLOTS|flags);
196 k = bch2_btree_iter_peek_slot(iter);
201 ret = k.k->type == KEY_TYPE_inode ? 0 : -EIO;
205 ret = bch2_inode_unpack(bkey_s_c_to_inode(k), inode);
211 bch2_trans_iter_put(trans, iter);
215 int bch2_inode_write(struct btree_trans *trans,
216 struct btree_iter *iter,
217 struct bch_inode_unpacked *inode)
219 struct bkey_inode_buf *inode_p;
221 inode_p = bch2_trans_kmalloc(trans, sizeof(*inode_p));
223 return PTR_ERR(inode_p);
225 bch2_inode_pack(inode_p, inode);
226 bch2_trans_update(trans, iter, &inode_p->inode.k_i);
230 const char *bch2_inode_invalid(const struct bch_fs *c, struct bkey_s_c k)
232 struct bkey_s_c_inode inode = bkey_s_c_to_inode(k);
233 struct bch_inode_unpacked unpacked;
236 return "nonzero offset";
238 if (bkey_val_bytes(k.k) < sizeof(struct bch_inode))
239 return "incorrect value size";
241 if (k.k->p.inode < BLOCKDEV_INODE_MAX)
242 return "fs inode in blockdev range";
244 if (INODE_STR_HASH(inode.v) >= BCH_STR_HASH_NR)
245 return "invalid str hash type";
247 if (bch2_inode_unpack(inode, &unpacked))
248 return "invalid variable length fields";
250 if (unpacked.bi_data_checksum >= BCH_CSUM_OPT_NR + 1)
251 return "invalid data checksum type";
253 if (unpacked.bi_compression >= BCH_COMPRESSION_OPT_NR + 1)
254 return "invalid data checksum type";
256 if ((unpacked.bi_flags & BCH_INODE_UNLINKED) &&
257 unpacked.bi_nlink != 0)
258 return "flagged as unlinked but bi_nlink != 0";
263 void bch2_inode_to_text(struct printbuf *out, struct bch_fs *c,
266 struct bkey_s_c_inode inode = bkey_s_c_to_inode(k);
267 struct bch_inode_unpacked unpacked;
269 if (bch2_inode_unpack(inode, &unpacked)) {
270 pr_buf(out, "(unpack error)");
274 #define x(_name, _bits) \
275 pr_buf(out, #_name ": %llu ", (u64) unpacked._name);
280 const char *bch2_inode_generation_invalid(const struct bch_fs *c,
284 return "nonzero offset";
286 if (bkey_val_bytes(k.k) != sizeof(struct bch_inode_generation))
287 return "incorrect value size";
292 void bch2_inode_generation_to_text(struct printbuf *out, struct bch_fs *c,
295 struct bkey_s_c_inode_generation gen = bkey_s_c_to_inode_generation(k);
297 pr_buf(out, "generation: %u", le32_to_cpu(gen.v->bi_generation));
300 void bch2_inode_init_early(struct bch_fs *c,
301 struct bch_inode_unpacked *inode_u)
303 enum bch_str_hash_type str_hash =
304 bch2_str_hash_opt_to_type(c, c->opts.str_hash);
306 memset(inode_u, 0, sizeof(*inode_u));
309 inode_u->bi_flags |= str_hash << INODE_STR_HASH_OFFSET;
310 get_random_bytes(&inode_u->bi_hash_seed,
311 sizeof(inode_u->bi_hash_seed));
314 void bch2_inode_init_late(struct bch_inode_unpacked *inode_u, u64 now,
315 uid_t uid, gid_t gid, umode_t mode, dev_t rdev,
316 struct bch_inode_unpacked *parent)
318 inode_u->bi_mode = mode;
319 inode_u->bi_uid = uid;
320 inode_u->bi_gid = gid;
321 inode_u->bi_dev = rdev;
322 inode_u->bi_atime = now;
323 inode_u->bi_mtime = now;
324 inode_u->bi_ctime = now;
325 inode_u->bi_otime = now;
327 if (parent && parent->bi_mode & S_ISGID) {
328 inode_u->bi_gid = parent->bi_gid;
330 inode_u->bi_mode |= S_ISGID;
334 #define x(_name, ...) inode_u->bi_##_name = parent->bi_##_name;
340 void bch2_inode_init(struct bch_fs *c, struct bch_inode_unpacked *inode_u,
341 uid_t uid, gid_t gid, umode_t mode, dev_t rdev,
342 struct bch_inode_unpacked *parent)
344 bch2_inode_init_early(c, inode_u);
345 bch2_inode_init_late(inode_u, bch2_current_time(c),
346 uid, gid, mode, rdev, parent);
349 static inline u32 bkey_generation(struct bkey_s_c k)
354 case KEY_TYPE_inode_generation:
355 return le32_to_cpu(bkey_s_c_to_inode_generation(k).v->bi_generation);
361 int bch2_inode_create(struct btree_trans *trans,
362 struct bch_inode_unpacked *inode_u,
363 u64 min, u64 max, u64 *hint)
365 struct bch_fs *c = trans->c;
366 struct bkey_inode_buf *inode_p;
367 struct btree_iter *iter;
374 if (c->opts.inodes_32bit)
375 max = min_t(u64, max, U32_MAX);
377 start = READ_ONCE(*hint);
379 if (start >= max || start < min)
382 inode_p = bch2_trans_kmalloc(trans, sizeof(*inode_p));
384 return PTR_ERR(inode_p);
386 iter = bch2_trans_get_iter(trans,
387 BTREE_ID_INODES, POS(start, 0),
388 BTREE_ITER_SLOTS|BTREE_ITER_INTENT);
390 return PTR_ERR(iter);
393 struct bkey_s_c k = bch2_btree_iter_peek_slot(iter);
402 if (iter->pos.inode >= max)
405 bch2_btree_iter_next_slot(iter);
409 *hint = k.k->p.inode;
410 inode_u->bi_inum = k.k->p.inode;
411 inode_u->bi_generation = bkey_generation(k);
413 bch2_inode_pack(inode_p, inode_u);
414 bch2_trans_update(trans, iter, &inode_p->inode.k_i);
420 /* Retry from start */
422 bch2_btree_iter_set_pos(iter, POS(start, 0));
429 int bch2_inode_rm(struct bch_fs *c, u64 inode_nr)
431 struct btree_trans trans;
432 struct btree_iter *iter;
433 struct bkey_i_inode_generation delete;
434 struct bpos start = POS(inode_nr, 0);
435 struct bpos end = POS(inode_nr + 1, 0);
439 * If this was a directory, there shouldn't be any real dirents left -
440 * but there could be whiteouts (from hash collisions) that we should
443 * XXX: the dirent could ideally would delete whiteouts when they're no
446 ret = bch2_btree_delete_range(c, BTREE_ID_EXTENTS,
448 bch2_btree_delete_range(c, BTREE_ID_XATTRS,
450 bch2_btree_delete_range(c, BTREE_ID_DIRENTS,
455 bch2_trans_init(&trans, c, 0, 0);
457 iter = bch2_trans_get_iter(&trans, BTREE_ID_INODES, POS(inode_nr, 0),
458 BTREE_ITER_SLOTS|BTREE_ITER_INTENT);
460 struct bkey_s_c k = bch2_btree_iter_peek_slot(iter);
461 u32 bi_generation = 0;
467 bch2_fs_inconsistent_on(k.k->type != KEY_TYPE_inode, c,
468 "inode %llu not found when deleting",
472 case KEY_TYPE_inode: {
473 struct bch_inode_unpacked inode_u;
475 if (!bch2_inode_unpack(bkey_s_c_to_inode(k), &inode_u))
476 bi_generation = inode_u.bi_generation + 1;
479 case KEY_TYPE_inode_generation: {
480 struct bkey_s_c_inode_generation g =
481 bkey_s_c_to_inode_generation(k);
482 bi_generation = le32_to_cpu(g.v->bi_generation);
487 if (!bi_generation) {
488 bkey_init(&delete.k);
489 delete.k.p.inode = inode_nr;
491 bkey_inode_generation_init(&delete.k_i);
492 delete.k.p.inode = inode_nr;
493 delete.v.bi_generation = cpu_to_le32(bi_generation);
496 bch2_trans_update(&trans, iter, &delete.k_i);
498 ret = bch2_trans_commit(&trans, NULL, NULL,
500 BTREE_INSERT_NOFAIL);
501 } while (ret == -EINTR);
503 bch2_trans_exit(&trans);
507 int bch2_inode_find_by_inum_trans(struct btree_trans *trans, u64 inode_nr,
508 struct bch_inode_unpacked *inode)
510 struct btree_iter *iter;
514 iter = bch2_trans_get_iter(trans, BTREE_ID_INODES,
515 POS(inode_nr, 0), BTREE_ITER_SLOTS);
517 return PTR_ERR(iter);
519 k = bch2_btree_iter_peek_slot(iter);
520 if (k.k->type == KEY_TYPE_inode)
521 ret = bch2_inode_unpack(bkey_s_c_to_inode(k), inode);
523 bch2_trans_iter_put(trans, iter);
528 int bch2_inode_find_by_inum(struct bch_fs *c, u64 inode_nr,
529 struct bch_inode_unpacked *inode)
531 return bch2_trans_do(c, NULL, 0,
532 bch2_inode_find_by_inum_trans(&trans, inode_nr, inode));
535 #ifdef CONFIG_BCACHEFS_DEBUG
536 void bch2_inode_pack_test(void)
538 struct bch_inode_unpacked *u, test_inodes[] = {
545 .bi_sectors = U64_MAX,
549 .bi_generation = U32_MAX,
554 for (u = test_inodes;
555 u < test_inodes + ARRAY_SIZE(test_inodes);
557 struct bkey_inode_buf p;
559 bch2_inode_pack(&p, u);