1 // SPDX-License-Identifier: GPL-2.0
4 #include "bkey_methods.h"
5 #include "btree_update.h"
12 #include <linux/random.h>
14 #include <asm/unaligned.h>
16 const char * const bch2_inode_opts[] = {
17 #define x(name, ...) #name,
23 static const u8 byte_table[8] = { 1, 2, 3, 4, 6, 8, 10, 13 };
24 static const u8 bits_table[8] = {
35 static int inode_encode_field(u8 *out, u8 *end, u64 hi, u64 lo)
37 __be64 in[2] = { cpu_to_be64(hi), cpu_to_be64(lo), };
38 unsigned shift, bytes, bits = likely(!hi)
42 for (shift = 1; shift <= 8; shift++)
43 if (bits < bits_table[shift - 1])
48 bytes = byte_table[shift - 1];
50 BUG_ON(out + bytes > end);
52 memcpy(out, (u8 *) in + 16 - bytes, bytes);
53 *out |= (1 << 8) >> shift;
58 static int inode_decode_field(const u8 *in, const u8 *end,
59 u64 out[2], unsigned *out_bits)
61 __be64 be[2] = { 0, 0 };
62 unsigned bytes, shift;
72 * position of highest set bit indicates number of bytes:
73 * shift = number of bits to remove in high byte:
75 shift = 8 - __fls(*in); /* 1 <= shift <= 8 */
76 bytes = byte_table[shift - 1];
81 p = (u8 *) be + 16 - bytes;
83 *p ^= (1 << 8) >> shift;
85 out[0] = be64_to_cpu(be[0]);
86 out[1] = be64_to_cpu(be[1]);
87 *out_bits = out[0] ? 64 + fls64(out[0]) : fls64(out[1]);
92 void bch2_inode_pack(struct bkey_inode_buf *packed,
93 const struct bch_inode_unpacked *inode)
95 u8 *out = packed->inode.v.fields;
96 u8 *end = (void *) &packed[1];
97 u8 *last_nonzero_field = out;
98 unsigned nr_fields = 0, last_nonzero_fieldnr = 0;
100 bkey_inode_init(&packed->inode.k_i);
101 packed->inode.k.p.inode = inode->bi_inum;
102 packed->inode.v.bi_hash_seed = inode->bi_hash_seed;
103 packed->inode.v.bi_flags = cpu_to_le32(inode->bi_flags);
104 packed->inode.v.bi_mode = cpu_to_le16(inode->bi_mode);
106 #define x(_name, _bits) \
107 out += inode_encode_field(out, end, 0, inode->_name); \
110 if (inode->_name) { \
111 last_nonzero_field = out; \
112 last_nonzero_fieldnr = nr_fields; \
118 out = last_nonzero_field;
119 nr_fields = last_nonzero_fieldnr;
121 set_bkey_val_bytes(&packed->inode.k, out - (u8 *) &packed->inode.v);
123 (u8 *) &packed->inode.v +
124 bkey_val_bytes(&packed->inode.k) - out);
126 SET_INODE_NR_FIELDS(&packed->inode.v, nr_fields);
128 if (IS_ENABLED(CONFIG_BCACHEFS_DEBUG)) {
129 struct bch_inode_unpacked unpacked;
131 int ret = bch2_inode_unpack(inode_i_to_s_c(&packed->inode),
134 BUG_ON(unpacked.bi_inum != inode->bi_inum);
135 BUG_ON(unpacked.bi_hash_seed != inode->bi_hash_seed);
136 BUG_ON(unpacked.bi_mode != inode->bi_mode);
138 #define x(_name, _bits) BUG_ON(unpacked._name != inode->_name);
144 int bch2_inode_unpack(struct bkey_s_c_inode inode,
145 struct bch_inode_unpacked *unpacked)
147 const u8 *in = inode.v->fields;
148 const u8 *end = (void *) inode.v + bkey_val_bytes(inode.k);
150 unsigned fieldnr = 0, field_bits;
153 unpacked->bi_inum = inode.k->p.inode;
154 unpacked->bi_hash_seed = inode.v->bi_hash_seed;
155 unpacked->bi_flags = le32_to_cpu(inode.v->bi_flags);
156 unpacked->bi_mode = le16_to_cpu(inode.v->bi_mode);
158 #define x(_name, _bits) \
159 if (fieldnr++ == INODE_NR_FIELDS(inode.v)) { \
160 memset(&unpacked->_name, 0, \
161 sizeof(*unpacked) - \
162 offsetof(struct bch_inode_unpacked, _name)); \
166 ret = inode_decode_field(in, end, field, &field_bits); \
170 if (field_bits > sizeof(unpacked->_name) * 8) \
173 unpacked->_name = field[1]; \
179 /* XXX: signal if there were more fields than expected? */
184 const char *bch2_inode_invalid(const struct bch_fs *c, struct bkey_s_c k)
186 struct bkey_s_c_inode inode = bkey_s_c_to_inode(k);
187 struct bch_inode_unpacked unpacked;
190 return "nonzero offset";
192 if (bkey_val_bytes(k.k) < sizeof(struct bch_inode))
193 return "incorrect value size";
195 if (k.k->p.inode < BLOCKDEV_INODE_MAX)
196 return "fs inode in blockdev range";
198 if (INODE_STR_HASH(inode.v) >= BCH_STR_HASH_NR)
199 return "invalid str hash type";
201 if (bch2_inode_unpack(inode, &unpacked))
202 return "invalid variable length fields";
204 if (unpacked.bi_data_checksum >= BCH_CSUM_OPT_NR + 1)
205 return "invalid data checksum type";
207 if (unpacked.bi_compression >= BCH_COMPRESSION_OPT_NR + 1)
208 return "invalid data checksum type";
210 if ((unpacked.bi_flags & BCH_INODE_UNLINKED) &&
211 unpacked.bi_nlink != 0)
212 return "flagged as unlinked but bi_nlink != 0";
217 void bch2_inode_to_text(struct printbuf *out, struct bch_fs *c,
220 struct bkey_s_c_inode inode = bkey_s_c_to_inode(k);
221 struct bch_inode_unpacked unpacked;
223 if (bch2_inode_unpack(inode, &unpacked)) {
224 pr_buf(out, "(unpack error)");
228 #define x(_name, _bits) \
229 pr_buf(out, #_name ": %llu ", (u64) unpacked._name);
234 const char *bch2_inode_generation_invalid(const struct bch_fs *c,
238 return "nonzero offset";
240 if (bkey_val_bytes(k.k) != sizeof(struct bch_inode_generation))
241 return "incorrect value size";
246 void bch2_inode_generation_to_text(struct printbuf *out, struct bch_fs *c,
249 struct bkey_s_c_inode_generation gen = bkey_s_c_to_inode_generation(k);
251 pr_buf(out, "generation: %u", le32_to_cpu(gen.v->bi_generation));
254 void bch2_inode_init(struct bch_fs *c, struct bch_inode_unpacked *inode_u,
255 uid_t uid, gid_t gid, umode_t mode, dev_t rdev,
256 struct bch_inode_unpacked *parent)
258 s64 now = bch2_current_time(c);
260 memset(inode_u, 0, sizeof(*inode_u));
263 inode_u->bi_flags |= c->opts.str_hash << INODE_STR_HASH_OFFSET;
264 get_random_bytes(&inode_u->bi_hash_seed,
265 sizeof(inode_u->bi_hash_seed));
267 inode_u->bi_mode = mode;
268 inode_u->bi_uid = uid;
269 inode_u->bi_gid = gid;
270 inode_u->bi_dev = rdev;
271 inode_u->bi_atime = now;
272 inode_u->bi_mtime = now;
273 inode_u->bi_ctime = now;
274 inode_u->bi_otime = now;
277 #define x(_name, ...) inode_u->bi_##_name = parent->bi_##_name;
283 static inline u32 bkey_generation(struct bkey_s_c k)
288 case KEY_TYPE_inode_generation:
289 return le32_to_cpu(bkey_s_c_to_inode_generation(k).v->bi_generation);
295 int __bch2_inode_create(struct btree_trans *trans,
296 struct bch_inode_unpacked *inode_u,
297 u64 min, u64 max, u64 *hint)
299 struct bch_fs *c = trans->c;
300 struct bkey_inode_buf *inode_p;
301 struct btree_iter *iter;
308 if (c->opts.inodes_32bit)
309 max = min_t(u64, max, U32_MAX);
311 start = READ_ONCE(*hint);
313 if (start >= max || start < min)
316 inode_p = bch2_trans_kmalloc(trans, sizeof(*inode_p));
318 return PTR_ERR(inode_p);
320 iter = bch2_trans_get_iter(trans,
321 BTREE_ID_INODES, POS(start, 0),
322 BTREE_ITER_SLOTS|BTREE_ITER_INTENT);
324 return PTR_ERR(iter);
327 struct bkey_s_c k = bch2_btree_iter_peek_slot(iter);
336 if (iter->pos.inode >= max)
339 bch2_btree_iter_next_slot(iter);
343 *hint = k.k->p.inode;
344 inode_u->bi_inum = k.k->p.inode;
345 inode_u->bi_generation = bkey_generation(k);
347 bch2_inode_pack(inode_p, inode_u);
348 bch2_trans_update(trans, iter, &inode_p->inode.k_i);
354 /* Retry from start */
356 bch2_btree_iter_set_pos(iter, POS(start, 0));
363 int bch2_inode_create(struct bch_fs *c, struct bch_inode_unpacked *inode_u,
364 u64 min, u64 max, u64 *hint)
366 return bch2_trans_do(c, NULL, BTREE_INSERT_ATOMIC,
367 __bch2_inode_create(&trans, inode_u, min, max, hint));
370 int bch2_inode_rm(struct bch_fs *c, u64 inode_nr)
372 struct btree_trans trans;
373 struct btree_iter *iter;
374 struct bkey_i_inode_generation delete;
375 struct bpos start = POS(inode_nr, 0);
376 struct bpos end = POS(inode_nr + 1, 0);
380 * If this was a directory, there shouldn't be any real dirents left -
381 * but there could be whiteouts (from hash collisions) that we should
384 * XXX: the dirent could ideally would delete whiteouts when they're no
387 ret = bch2_btree_delete_range(c, BTREE_ID_EXTENTS,
389 bch2_btree_delete_range(c, BTREE_ID_XATTRS,
391 bch2_btree_delete_range(c, BTREE_ID_DIRENTS,
396 bch2_trans_init(&trans, c, 0, 0);
398 iter = bch2_trans_get_iter(&trans, BTREE_ID_INODES, POS(inode_nr, 0),
399 BTREE_ITER_SLOTS|BTREE_ITER_INTENT);
401 struct bkey_s_c k = bch2_btree_iter_peek_slot(iter);
402 u32 bi_generation = 0;
408 bch2_fs_inconsistent_on(k.k->type != KEY_TYPE_inode, c,
409 "inode %llu not found when deleting",
413 case KEY_TYPE_inode: {
414 struct bch_inode_unpacked inode_u;
416 if (!bch2_inode_unpack(bkey_s_c_to_inode(k), &inode_u))
417 bi_generation = inode_u.bi_generation + 1;
420 case KEY_TYPE_inode_generation: {
421 struct bkey_s_c_inode_generation g =
422 bkey_s_c_to_inode_generation(k);
423 bi_generation = le32_to_cpu(g.v->bi_generation);
428 if (!bi_generation) {
429 bkey_init(&delete.k);
430 delete.k.p.inode = inode_nr;
432 bkey_inode_generation_init(&delete.k_i);
433 delete.k.p.inode = inode_nr;
434 delete.v.bi_generation = cpu_to_le32(bi_generation);
437 bch2_trans_update(&trans, iter, &delete.k_i);
439 ret = bch2_trans_commit(&trans, NULL, NULL,
441 BTREE_INSERT_NOFAIL);
442 } while (ret == -EINTR);
444 bch2_trans_exit(&trans);
448 int bch2_inode_find_by_inum_trans(struct btree_trans *trans, u64 inode_nr,
449 struct bch_inode_unpacked *inode)
451 struct btree_iter *iter;
455 iter = bch2_trans_get_iter(trans, BTREE_ID_INODES,
456 POS(inode_nr, 0), BTREE_ITER_SLOTS);
458 return PTR_ERR(iter);
460 k = bch2_btree_iter_peek_slot(iter);
461 if (k.k->type == KEY_TYPE_inode)
462 ret = bch2_inode_unpack(bkey_s_c_to_inode(k), inode);
464 bch2_trans_iter_put(trans, iter);
469 int bch2_inode_find_by_inum(struct bch_fs *c, u64 inode_nr,
470 struct bch_inode_unpacked *inode)
472 return bch2_trans_do(c, NULL, 0,
473 bch2_inode_find_by_inum_trans(&trans, inode_nr, inode));
476 #ifdef CONFIG_BCACHEFS_DEBUG
477 void bch2_inode_pack_test(void)
479 struct bch_inode_unpacked *u, test_inodes[] = {
486 .bi_sectors = U64_MAX,
490 .bi_generation = U32_MAX,
495 for (u = test_inodes;
496 u < test_inodes + ARRAY_SIZE(test_inodes);
498 struct bkey_inode_buf p;
500 bch2_inode_pack(&p, u);