3 #include "bkey_methods.h"
4 #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 const char *bch2_inode_invalid(const struct bch_fs *c, struct bkey_s_c k)
185 struct bkey_s_c_inode inode = bkey_s_c_to_inode(k);
186 struct bch_inode_unpacked unpacked;
189 return "nonzero offset";
191 if (bkey_val_bytes(k.k) < sizeof(struct bch_inode))
192 return "incorrect value size";
194 if (k.k->p.inode < BLOCKDEV_INODE_MAX)
195 return "fs inode in blockdev range";
197 if (INODE_STR_HASH(inode.v) >= BCH_STR_HASH_NR)
198 return "invalid str hash type";
200 if (bch2_inode_unpack(inode, &unpacked))
201 return "invalid variable length fields";
203 if (unpacked.bi_data_checksum >= BCH_CSUM_OPT_NR + 1)
204 return "invalid data checksum type";
206 if (unpacked.bi_compression >= BCH_COMPRESSION_OPT_NR + 1)
207 return "invalid data checksum type";
209 if ((unpacked.bi_flags & BCH_INODE_UNLINKED) &&
210 unpacked.bi_nlink != 0)
211 return "flagged as unlinked but bi_nlink != 0";
216 void bch2_inode_to_text(struct printbuf *out, struct bch_fs *c,
219 struct bkey_s_c_inode inode = bkey_s_c_to_inode(k);
220 struct bch_inode_unpacked unpacked;
222 if (bch2_inode_unpack(inode, &unpacked)) {
223 pr_buf(out, "(unpack error)");
227 #define x(_name, _bits) \
228 pr_buf(out, #_name ": %llu ", (u64) unpacked._name);
233 const char *bch2_inode_generation_invalid(const struct bch_fs *c,
237 return "nonzero offset";
239 if (bkey_val_bytes(k.k) != sizeof(struct bch_inode_generation))
240 return "incorrect value size";
245 void bch2_inode_generation_to_text(struct printbuf *out, struct bch_fs *c,
250 void bch2_inode_init(struct bch_fs *c, struct bch_inode_unpacked *inode_u,
251 uid_t uid, gid_t gid, umode_t mode, dev_t rdev,
252 struct bch_inode_unpacked *parent)
254 s64 now = timespec_to_bch2_time(c,
255 timespec64_trunc(current_kernel_time64(),
256 c->sb.time_precision));
258 memset(inode_u, 0, sizeof(*inode_u));
261 inode_u->bi_flags |= c->opts.str_hash << INODE_STR_HASH_OFFSET;
262 get_random_bytes(&inode_u->bi_hash_seed,
263 sizeof(inode_u->bi_hash_seed));
265 inode_u->bi_mode = mode;
266 inode_u->bi_uid = uid;
267 inode_u->bi_gid = gid;
268 inode_u->bi_dev = rdev;
269 inode_u->bi_atime = now;
270 inode_u->bi_mtime = now;
271 inode_u->bi_ctime = now;
272 inode_u->bi_otime = now;
275 #define x(_name, ...) inode_u->bi_##_name = parent->bi_##_name;
281 static inline u32 bkey_generation(struct bkey_s_c k)
286 case KEY_TYPE_inode_generation:
287 return le32_to_cpu(bkey_s_c_to_inode_generation(k).v->bi_generation);
293 int __bch2_inode_create(struct btree_trans *trans,
294 struct bch_inode_unpacked *inode_u,
295 u64 min, u64 max, u64 *hint)
297 struct bch_fs *c = trans->c;
298 struct bkey_inode_buf *inode_p;
299 struct btree_iter *iter;
306 if (c->opts.inodes_32bit)
307 max = min_t(u64, max, U32_MAX);
309 start = READ_ONCE(*hint);
311 if (start >= max || start < min)
314 inode_p = bch2_trans_kmalloc(trans, sizeof(*inode_p));
316 return PTR_ERR(inode_p);
318 iter = bch2_trans_get_iter(trans,
319 BTREE_ID_INODES, POS(start, 0),
320 BTREE_ITER_SLOTS|BTREE_ITER_INTENT);
322 return PTR_ERR(iter);
325 struct bkey_s_c k = bch2_btree_iter_peek_slot(iter);
327 ret = btree_iter_err(k);
334 if (iter->pos.inode >= max)
337 bch2_btree_iter_next_slot(iter);
341 *hint = k.k->p.inode;
342 inode_u->bi_inum = k.k->p.inode;
343 inode_u->bi_generation = bkey_generation(k);
345 bch2_inode_pack(inode_p, inode_u);
346 bch2_trans_update(trans,
347 BTREE_INSERT_ENTRY(iter, &inode_p->inode.k_i));
353 /* Retry from start */
355 bch2_btree_iter_set_pos(iter, POS(start, 0));
362 int bch2_inode_create(struct bch_fs *c, struct bch_inode_unpacked *inode_u,
363 u64 min, u64 max, u64 *hint)
365 return bch2_trans_do(c, NULL, BTREE_INSERT_ATOMIC,
366 __bch2_inode_create(&trans, inode_u, min, max, hint));
369 int bch2_inode_rm(struct bch_fs *c, u64 inode_nr)
371 struct btree_trans trans;
372 struct btree_iter *iter;
373 struct bkey_i_inode_generation delete;
374 struct bpos start = POS(inode_nr, 0);
375 struct bpos end = POS(inode_nr + 1, 0);
379 * If this was a directory, there shouldn't be any real dirents left -
380 * but there could be whiteouts (from hash collisions) that we should
383 * XXX: the dirent could ideally would delete whiteouts when they're no
386 ret = bch2_btree_delete_range(c, BTREE_ID_EXTENTS,
388 bch2_btree_delete_range(c, BTREE_ID_XATTRS,
390 bch2_btree_delete_range(c, BTREE_ID_DIRENTS,
395 bch2_trans_init(&trans, c);
397 iter = bch2_trans_get_iter(&trans, BTREE_ID_INODES, POS(inode_nr, 0),
398 BTREE_ITER_SLOTS|BTREE_ITER_INTENT);
400 struct bkey_s_c k = bch2_btree_iter_peek_slot(iter);
401 u32 bi_generation = 0;
403 ret = btree_iter_err(k);
407 bch2_fs_inconsistent_on(k.k->type != KEY_TYPE_inode, c,
408 "inode %llu not found when deleting",
412 case KEY_TYPE_inode: {
413 struct bch_inode_unpacked inode_u;
415 if (!bch2_inode_unpack(bkey_s_c_to_inode(k), &inode_u))
416 bi_generation = inode_u.bi_generation + 1;
419 case KEY_TYPE_inode_generation: {
420 struct bkey_s_c_inode_generation g =
421 bkey_s_c_to_inode_generation(k);
422 bi_generation = le32_to_cpu(g.v->bi_generation);
427 if (!bi_generation) {
428 bkey_init(&delete.k);
429 delete.k.p.inode = inode_nr;
431 bkey_inode_generation_init(&delete.k_i);
432 delete.k.p.inode = inode_nr;
433 delete.v.bi_generation = cpu_to_le32(bi_generation);
436 bch2_trans_update(&trans,
437 BTREE_INSERT_ENTRY(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(struct bch_fs *c, u64 inode_nr,
449 struct bch_inode_unpacked *inode)
451 struct btree_iter iter;
455 for_each_btree_key(&iter, c, BTREE_ID_INODES,
457 BTREE_ITER_SLOTS, k) {
460 ret = bch2_inode_unpack(bkey_s_c_to_inode(k), inode);
463 /* hole, not found */
470 return bch2_btree_iter_unlock(&iter) ?: ret;
473 #ifdef CONFIG_BCACHEFS_DEBUG
474 void bch2_inode_pack_test(void)
476 struct bch_inode_unpacked *u, test_inodes[] = {
483 .bi_sectors = U64_MAX,
487 .bi_generation = U32_MAX,
492 for (u = test_inodes;
493 u < test_inodes + ARRAY_SIZE(test_inodes);
495 struct bkey_inode_buf p;
497 bch2_inode_pack(&p, u);