3 #include "bkey_methods.h"
4 #include "btree_update.h"
11 #include <linux/random.h>
13 #include <asm/unaligned.h>
15 #define FIELD_BYTES() \
17 static const u8 byte_table[8] = { 1, 2, 3, 4, 6, 8, 10, 13 };
18 static const u8 bits_table[8] = {
29 static int inode_encode_field(u8 *out, u8 *end, u64 hi, u64 lo)
31 __be64 in[2] = { cpu_to_be64(hi), cpu_to_be64(lo), };
32 unsigned shift, bytes, bits = likely(!hi)
36 for (shift = 1; shift <= 8; shift++)
37 if (bits < bits_table[shift - 1])
42 bytes = byte_table[shift - 1];
44 BUG_ON(out + bytes > end);
46 memcpy(out, (u8 *) in + 16 - bytes, bytes);
47 *out |= (1 << 8) >> shift;
52 static int inode_decode_field(const u8 *in, const u8 *end,
53 u64 out[2], unsigned *out_bits)
55 __be64 be[2] = { 0, 0 };
56 unsigned bytes, shift;
66 * position of highest set bit indicates number of bytes:
67 * shift = number of bits to remove in high byte:
69 shift = 8 - __fls(*in); /* 1 <= shift <= 8 */
70 bytes = byte_table[shift - 1];
75 p = (u8 *) be + 16 - bytes;
77 *p ^= (1 << 8) >> shift;
79 out[0] = be64_to_cpu(be[0]);
80 out[1] = be64_to_cpu(be[1]);
81 *out_bits = out[0] ? 64 + fls64(out[0]) : fls64(out[1]);
86 void bch2_inode_pack(struct bkey_inode_buf *packed,
87 const struct bch_inode_unpacked *inode)
89 u8 *out = packed->inode.v.fields;
90 u8 *end = (void *) &packed[1];
91 u8 *last_nonzero_field = out;
92 unsigned nr_fields = 0, last_nonzero_fieldnr = 0;
94 bkey_inode_init(&packed->inode.k_i);
95 packed->inode.k.p.inode = inode->bi_inum;
96 packed->inode.v.bi_hash_seed = inode->bi_hash_seed;
97 packed->inode.v.bi_flags = cpu_to_le32(inode->bi_flags);
98 packed->inode.v.bi_mode = cpu_to_le16(inode->bi_mode);
100 #define BCH_INODE_FIELD(_name, _bits) \
101 out += inode_encode_field(out, end, 0, inode->_name); \
104 if (inode->_name) { \
105 last_nonzero_field = out; \
106 last_nonzero_fieldnr = nr_fields; \
110 #undef BCH_INODE_FIELD
112 out = last_nonzero_field;
113 nr_fields = last_nonzero_fieldnr;
115 set_bkey_val_bytes(&packed->inode.k, out - (u8 *) &packed->inode.v);
117 (u8 *) &packed->inode.v +
118 bkey_val_bytes(&packed->inode.k) - out);
120 SET_INODE_NR_FIELDS(&packed->inode.v, nr_fields);
122 if (IS_ENABLED(CONFIG_BCACHEFS_DEBUG)) {
123 struct bch_inode_unpacked unpacked;
125 int ret = bch2_inode_unpack(inode_i_to_s_c(&packed->inode),
128 BUG_ON(unpacked.bi_inum != inode->bi_inum);
129 BUG_ON(unpacked.bi_hash_seed != inode->bi_hash_seed);
130 BUG_ON(unpacked.bi_mode != inode->bi_mode);
132 #define BCH_INODE_FIELD(_name, _bits) BUG_ON(unpacked._name != inode->_name);
134 #undef BCH_INODE_FIELD
138 int bch2_inode_unpack(struct bkey_s_c_inode inode,
139 struct bch_inode_unpacked *unpacked)
141 const u8 *in = inode.v->fields;
142 const u8 *end = (void *) inode.v + bkey_val_bytes(inode.k);
144 unsigned fieldnr = 0, field_bits;
147 unpacked->bi_inum = inode.k->p.inode;
148 unpacked->bi_hash_seed = inode.v->bi_hash_seed;
149 unpacked->bi_flags = le32_to_cpu(inode.v->bi_flags);
150 unpacked->bi_mode = le16_to_cpu(inode.v->bi_mode);
152 #define BCH_INODE_FIELD(_name, _bits) \
153 if (fieldnr++ == INODE_NR_FIELDS(inode.v)) { \
154 memset(&unpacked->_name, 0, \
155 sizeof(*unpacked) - \
156 offsetof(struct bch_inode_unpacked, _name)); \
160 ret = inode_decode_field(in, end, field, &field_bits); \
164 if (field_bits > sizeof(unpacked->_name) * 8) \
167 unpacked->_name = field[1]; \
171 #undef BCH_INODE_FIELD
173 /* XXX: signal if there were more fields than expected? */
178 const char *bch2_inode_invalid(const struct bch_fs *c, struct bkey_s_c k)
180 struct bkey_s_c_inode inode = bkey_s_c_to_inode(k);
181 struct bch_inode_unpacked unpacked;
184 return "nonzero offset";
186 if (bkey_val_bytes(k.k) < sizeof(struct bch_inode))
187 return "incorrect value size";
189 if (k.k->p.inode < BLOCKDEV_INODE_MAX)
190 return "fs inode in blockdev range";
192 if (INODE_STR_HASH(inode.v) >= BCH_STR_HASH_NR)
193 return "invalid str hash type";
195 if (bch2_inode_unpack(inode, &unpacked))
196 return "invalid variable length fields";
198 if (unpacked.bi_data_checksum >= BCH_CSUM_OPT_NR + 1)
199 return "invalid data checksum type";
201 if (unpacked.bi_compression >= BCH_COMPRESSION_OPT_NR + 1)
202 return "invalid data checksum type";
204 if ((unpacked.bi_flags & BCH_INODE_UNLINKED) &&
205 unpacked.bi_nlink != 0)
206 return "flagged as unlinked but bi_nlink != 0";
211 void bch2_inode_to_text(struct printbuf *out, struct bch_fs *c,
214 struct bkey_s_c_inode inode = bkey_s_c_to_inode(k);
215 struct bch_inode_unpacked unpacked;
217 if (bch2_inode_unpack(inode, &unpacked)) {
218 pr_buf(out, "(unpack error)");
222 #define BCH_INODE_FIELD(_name, _bits) \
223 pr_buf(out, #_name ": %llu ", (u64) unpacked._name);
225 #undef BCH_INODE_FIELD
228 const char *bch2_inode_generation_invalid(const struct bch_fs *c,
232 return "nonzero offset";
234 if (bkey_val_bytes(k.k) != sizeof(struct bch_inode_generation))
235 return "incorrect value size";
240 void bch2_inode_generation_to_text(struct printbuf *out, struct bch_fs *c,
245 void bch2_inode_init(struct bch_fs *c, struct bch_inode_unpacked *inode_u,
246 uid_t uid, gid_t gid, umode_t mode, dev_t rdev,
247 struct bch_inode_unpacked *parent)
249 s64 now = timespec_to_bch2_time(c,
250 timespec64_trunc(current_kernel_time64(),
251 c->sb.time_precision));
253 memset(inode_u, 0, sizeof(*inode_u));
256 inode_u->bi_flags |= c->opts.str_hash << INODE_STR_HASH_OFFSET;
257 get_random_bytes(&inode_u->bi_hash_seed, sizeof(inode_u->bi_hash_seed));
259 inode_u->bi_mode = mode;
260 inode_u->bi_uid = uid;
261 inode_u->bi_gid = gid;
262 inode_u->bi_dev = rdev;
263 inode_u->bi_atime = now;
264 inode_u->bi_mtime = now;
265 inode_u->bi_ctime = now;
266 inode_u->bi_otime = now;
269 #define BCH_INODE_FIELD(_name) inode_u->_name = parent->_name;
270 BCH_INODE_FIELDS_INHERIT()
271 #undef BCH_INODE_FIELD
275 static inline u32 bkey_generation(struct bkey_s_c k)
280 case KEY_TYPE_inode_generation:
281 return le32_to_cpu(bkey_s_c_to_inode_generation(k).v->bi_generation);
287 int __bch2_inode_create(struct btree_trans *trans,
288 struct bch_inode_unpacked *inode_u,
289 u64 min, u64 max, u64 *hint)
291 struct bch_fs *c = trans->c;
292 struct bkey_inode_buf *inode_p;
293 struct btree_iter *iter;
300 if (c->opts.inodes_32bit)
301 max = min_t(u64, max, U32_MAX);
303 start = READ_ONCE(*hint);
305 if (start >= max || start < min)
308 inode_p = bch2_trans_kmalloc(trans, sizeof(*inode_p));
310 return PTR_ERR(inode_p);
312 iter = bch2_trans_get_iter(trans,
313 BTREE_ID_INODES, POS(start, 0),
314 BTREE_ITER_SLOTS|BTREE_ITER_INTENT);
316 return PTR_ERR(iter);
319 struct bkey_s_c k = bch2_btree_iter_peek_slot(iter);
321 ret = btree_iter_err(k);
328 if (iter->pos.inode >= max)
331 bch2_btree_iter_next_slot(iter);
335 *hint = k.k->p.inode;
336 inode_u->bi_inum = k.k->p.inode;
337 inode_u->bi_generation = bkey_generation(k);
339 bch2_inode_pack(inode_p, inode_u);
340 bch2_trans_update(trans,
341 BTREE_INSERT_ENTRY(iter, &inode_p->inode.k_i));
347 /* Retry from start */
349 bch2_btree_iter_set_pos(iter, POS(start, 0));
356 int bch2_inode_create(struct bch_fs *c, struct bch_inode_unpacked *inode_u,
357 u64 min, u64 max, u64 *hint)
359 return bch2_trans_do(c, NULL, BTREE_INSERT_ATOMIC,
360 __bch2_inode_create(&trans, inode_u, min, max, hint));
363 int bch2_inode_rm(struct bch_fs *c, u64 inode_nr)
365 struct btree_iter iter;
366 struct bkey_i_inode_generation delete;
367 struct bpos start = POS(inode_nr, 0);
368 struct bpos end = POS(inode_nr + 1, 0);
372 * If this was a directory, there shouldn't be any real dirents left -
373 * but there could be whiteouts (from hash collisions) that we should
376 * XXX: the dirent could ideally would delete whiteouts when they're no
379 ret = bch2_btree_delete_range(c, BTREE_ID_EXTENTS,
381 bch2_btree_delete_range(c, BTREE_ID_XATTRS,
383 bch2_btree_delete_range(c, BTREE_ID_DIRENTS,
388 bch2_btree_iter_init(&iter, c, BTREE_ID_INODES, POS(inode_nr, 0),
389 BTREE_ITER_SLOTS|BTREE_ITER_INTENT);
391 struct bkey_s_c k = bch2_btree_iter_peek_slot(&iter);
392 u32 bi_generation = 0;
394 ret = btree_iter_err(k);
396 bch2_btree_iter_unlock(&iter);
400 bch2_fs_inconsistent_on(k.k->type != KEY_TYPE_inode, c,
401 "inode %llu not found when deleting",
405 case KEY_TYPE_inode: {
406 struct bch_inode_unpacked inode_u;
408 if (!bch2_inode_unpack(bkey_s_c_to_inode(k), &inode_u))
409 bi_generation = inode_u.bi_generation + 1;
412 case KEY_TYPE_inode_generation: {
413 struct bkey_s_c_inode_generation g =
414 bkey_s_c_to_inode_generation(k);
415 bi_generation = le32_to_cpu(g.v->bi_generation);
420 if (!bi_generation) {
421 bkey_init(&delete.k);
422 delete.k.p.inode = inode_nr;
424 bkey_inode_generation_init(&delete.k_i);
425 delete.k.p.inode = inode_nr;
426 delete.v.bi_generation = cpu_to_le32(bi_generation);
429 ret = bch2_btree_insert_at(c, NULL, NULL,
432 BTREE_INSERT_ENTRY(&iter, &delete.k_i));
433 } while (ret == -EINTR);
435 bch2_btree_iter_unlock(&iter);
439 int bch2_inode_find_by_inum(struct bch_fs *c, u64 inode_nr,
440 struct bch_inode_unpacked *inode)
442 struct btree_iter iter;
446 for_each_btree_key(&iter, c, BTREE_ID_INODES,
448 BTREE_ITER_SLOTS, k) {
451 ret = bch2_inode_unpack(bkey_s_c_to_inode(k), inode);
454 /* hole, not found */
461 return bch2_btree_iter_unlock(&iter) ?: ret;
464 #ifdef CONFIG_BCACHEFS_DEBUG
465 void bch2_inode_pack_test(void)
467 struct bch_inode_unpacked *u, test_inodes[] = {
474 .bi_sectors = U64_MAX,
478 .bi_generation = U32_MAX,
483 for (u = test_inodes;
484 u < test_inodes + ARRAY_SIZE(test_inodes);
486 struct bkey_inode_buf p;
488 bch2_inode_pack(&p, u);