]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/inode.c
Update bcachefs sources to ffe09df106 bcachefs: Verify fs hasn't been modified before...
[bcachefs-tools-debian] / libbcachefs / inode.c
1
2 #include "bcachefs.h"
3 #include "bkey_methods.h"
4 #include "btree_update.h"
5 #include "error.h"
6 #include "extents.h"
7 #include "inode.h"
8 #include "io.h"
9 #include "keylist.h"
10
11 #include <linux/random.h>
12
13 #include <asm/unaligned.h>
14
15 const char * const bch2_inode_opts[] = {
16 #define x(name, ...)    #name,
17         BCH_INODE_OPTS()
18 #undef  x
19         NULL,
20 };
21
22 static const u8 byte_table[8] = { 1, 2, 3, 4, 6, 8, 10, 13 };
23 static const u8 bits_table[8] = {
24         1  * 8 - 1,
25         2  * 8 - 2,
26         3  * 8 - 3,
27         4  * 8 - 4,
28         6  * 8 - 5,
29         8  * 8 - 6,
30         10 * 8 - 7,
31         13 * 8 - 8,
32 };
33
34 static int inode_encode_field(u8 *out, u8 *end, u64 hi, u64 lo)
35 {
36         __be64 in[2] = { cpu_to_be64(hi), cpu_to_be64(lo), };
37         unsigned shift, bytes, bits = likely(!hi)
38                 ? fls64(lo)
39                 : fls64(hi) + 64;
40
41         for (shift = 1; shift <= 8; shift++)
42                 if (bits < bits_table[shift - 1])
43                         goto got_shift;
44
45         BUG();
46 got_shift:
47         bytes = byte_table[shift - 1];
48
49         BUG_ON(out + bytes > end);
50
51         memcpy(out, (u8 *) in + 16 - bytes, bytes);
52         *out |= (1 << 8) >> shift;
53
54         return bytes;
55 }
56
57 static int inode_decode_field(const u8 *in, const u8 *end,
58                               u64 out[2], unsigned *out_bits)
59 {
60         __be64 be[2] = { 0, 0 };
61         unsigned bytes, shift;
62         u8 *p;
63
64         if (in >= end)
65                 return -1;
66
67         if (!*in)
68                 return -1;
69
70         /*
71          * position of highest set bit indicates number of bytes:
72          * shift = number of bits to remove in high byte:
73          */
74         shift   = 8 - __fls(*in); /* 1 <= shift <= 8 */
75         bytes   = byte_table[shift - 1];
76
77         if (in + bytes > end)
78                 return -1;
79
80         p = (u8 *) be + 16 - bytes;
81         memcpy(p, in, bytes);
82         *p ^= (1 << 8) >> shift;
83
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]);
87
88         return bytes;
89 }
90
91 void bch2_inode_pack(struct bkey_inode_buf *packed,
92                      const struct bch_inode_unpacked *inode)
93 {
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;
98
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);
104
105 #define x(_name, _bits)                                 \
106         out += inode_encode_field(out, end, 0, inode->_name);           \
107         nr_fields++;                                                    \
108                                                                         \
109         if (inode->_name) {                                             \
110                 last_nonzero_field = out;                               \
111                 last_nonzero_fieldnr = nr_fields;                       \
112         }
113
114         BCH_INODE_FIELDS()
115 #undef  x
116
117         out = last_nonzero_field;
118         nr_fields = last_nonzero_fieldnr;
119
120         set_bkey_val_bytes(&packed->inode.k, out - (u8 *) &packed->inode.v);
121         memset(out, 0,
122                (u8 *) &packed->inode.v +
123                bkey_val_bytes(&packed->inode.k) - out);
124
125         SET_INODE_NR_FIELDS(&packed->inode.v, nr_fields);
126
127         if (IS_ENABLED(CONFIG_BCACHEFS_DEBUG)) {
128                 struct bch_inode_unpacked unpacked;
129
130                 int ret = bch2_inode_unpack(inode_i_to_s_c(&packed->inode),
131                                            &unpacked);
132                 BUG_ON(ret);
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);
136
137 #define x(_name, _bits) BUG_ON(unpacked._name != inode->_name);
138                 BCH_INODE_FIELDS()
139 #undef  x
140         }
141 }
142
143 int bch2_inode_unpack(struct bkey_s_c_inode inode,
144                       struct bch_inode_unpacked *unpacked)
145 {
146         const u8 *in = inode.v->fields;
147         const u8 *end = (void *) inode.v + bkey_val_bytes(inode.k);
148         u64 field[2];
149         unsigned fieldnr = 0, field_bits;
150         int ret;
151
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);
156
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));     \
162                 return 0;                                               \
163         }                                                               \
164                                                                         \
165         ret = inode_decode_field(in, end, field, &field_bits);          \
166         if (ret < 0)                                                    \
167                 return ret;                                             \
168                                                                         \
169         if (field_bits > sizeof(unpacked->_name) * 8)                   \
170                 return -1;                                              \
171                                                                         \
172         unpacked->_name = field[1];                                     \
173         in += ret;
174
175         BCH_INODE_FIELDS()
176 #undef  x
177
178         /* XXX: signal if there were more fields than expected? */
179
180         return 0;
181 }
182
183 const char *bch2_inode_invalid(const struct bch_fs *c, struct bkey_s_c k)
184 {
185                 struct bkey_s_c_inode inode = bkey_s_c_to_inode(k);
186                 struct bch_inode_unpacked unpacked;
187
188         if (k.k->p.offset)
189                 return "nonzero offset";
190
191         if (bkey_val_bytes(k.k) < sizeof(struct bch_inode))
192                 return "incorrect value size";
193
194         if (k.k->p.inode < BLOCKDEV_INODE_MAX)
195                 return "fs inode in blockdev range";
196
197         if (INODE_STR_HASH(inode.v) >= BCH_STR_HASH_NR)
198                 return "invalid str hash type";
199
200         if (bch2_inode_unpack(inode, &unpacked))
201                 return "invalid variable length fields";
202
203         if (unpacked.bi_data_checksum >= BCH_CSUM_OPT_NR + 1)
204                 return "invalid data checksum type";
205
206         if (unpacked.bi_compression >= BCH_COMPRESSION_OPT_NR + 1)
207                 return "invalid data checksum type";
208
209         if ((unpacked.bi_flags & BCH_INODE_UNLINKED) &&
210             unpacked.bi_nlink != 0)
211                 return "flagged as unlinked but bi_nlink != 0";
212
213         return NULL;
214 }
215
216 void bch2_inode_to_text(struct printbuf *out, struct bch_fs *c,
217                        struct bkey_s_c k)
218 {
219         struct bkey_s_c_inode inode = bkey_s_c_to_inode(k);
220         struct bch_inode_unpacked unpacked;
221
222         if (bch2_inode_unpack(inode, &unpacked)) {
223                 pr_buf(out, "(unpack error)");
224                 return;
225         }
226
227 #define x(_name, _bits)                                         \
228         pr_buf(out, #_name ": %llu ", (u64) unpacked._name);
229         BCH_INODE_FIELDS()
230 #undef  x
231 }
232
233 const char *bch2_inode_generation_invalid(const struct bch_fs *c,
234                                           struct bkey_s_c k)
235 {
236         if (k.k->p.offset)
237                 return "nonzero offset";
238
239         if (bkey_val_bytes(k.k) != sizeof(struct bch_inode_generation))
240                 return "incorrect value size";
241
242         return NULL;
243 }
244
245 void bch2_inode_generation_to_text(struct printbuf *out, struct bch_fs *c,
246                                    struct bkey_s_c k)
247 {
248 }
249
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)
253 {
254         s64 now = timespec_to_bch2_time(c,
255                 timespec64_trunc(current_kernel_time64(),
256                                  c->sb.time_precision));
257
258         memset(inode_u, 0, sizeof(*inode_u));
259
260         /* ick */
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));
264
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;
273
274         if (parent) {
275 #define x(_name, ...)   inode_u->bi_##_name = parent->bi_##_name;
276                 BCH_INODE_OPTS()
277 #undef x
278         }
279 }
280
281 static inline u32 bkey_generation(struct bkey_s_c k)
282 {
283         switch (k.k->type) {
284         case KEY_TYPE_inode:
285                 BUG();
286         case KEY_TYPE_inode_generation:
287                 return le32_to_cpu(bkey_s_c_to_inode_generation(k).v->bi_generation);
288         default:
289                 return 0;
290         }
291 }
292
293 int __bch2_inode_create(struct btree_trans *trans,
294                         struct bch_inode_unpacked *inode_u,
295                         u64 min, u64 max, u64 *hint)
296 {
297         struct bch_fs *c = trans->c;
298         struct bkey_inode_buf *inode_p;
299         struct btree_iter *iter;
300         u64 start;
301         int ret;
302
303         if (!max)
304                 max = ULLONG_MAX;
305
306         if (c->opts.inodes_32bit)
307                 max = min_t(u64, max, U32_MAX);
308
309         start = READ_ONCE(*hint);
310
311         if (start >= max || start < min)
312                 start = min;
313
314         inode_p = bch2_trans_kmalloc(trans, sizeof(*inode_p));
315         if (IS_ERR(inode_p))
316                 return PTR_ERR(inode_p);
317
318         iter = bch2_trans_get_iter(trans,
319                         BTREE_ID_INODES, POS(start, 0),
320                         BTREE_ITER_SLOTS|BTREE_ITER_INTENT);
321         if (IS_ERR(iter))
322                 return PTR_ERR(iter);
323 again:
324         while (1) {
325                 struct bkey_s_c k = bch2_btree_iter_peek_slot(iter);
326
327                 ret = btree_iter_err(k);
328                 if (ret)
329                         return ret;
330
331                 switch (k.k->type) {
332                 case KEY_TYPE_inode:
333                         /* slot used */
334                         if (iter->pos.inode >= max)
335                                 goto out;
336
337                         bch2_btree_iter_next_slot(iter);
338                         break;
339
340                 default:
341                         *hint                   = k.k->p.inode;
342                         inode_u->bi_inum        = k.k->p.inode;
343                         inode_u->bi_generation  = bkey_generation(k);
344
345                         bch2_inode_pack(inode_p, inode_u);
346                         bch2_trans_update(trans,
347                                 BTREE_INSERT_ENTRY(iter, &inode_p->inode.k_i));
348                         return 0;
349                 }
350         }
351 out:
352         if (start != min) {
353                 /* Retry from start */
354                 start = min;
355                 bch2_btree_iter_set_pos(iter, POS(start, 0));
356                 goto again;
357         }
358
359         return -ENOSPC;
360 }
361
362 int bch2_inode_create(struct bch_fs *c, struct bch_inode_unpacked *inode_u,
363                       u64 min, u64 max, u64 *hint)
364 {
365         return bch2_trans_do(c, NULL, BTREE_INSERT_ATOMIC,
366                         __bch2_inode_create(&trans, inode_u, min, max, hint));
367 }
368
369 int bch2_inode_rm(struct bch_fs *c, u64 inode_nr)
370 {
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);
376         int ret;
377
378         /*
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
381          * delete:
382          *
383          * XXX: the dirent could ideally would delete whiteouts when they're no
384          * longer needed
385          */
386         ret   = bch2_btree_delete_range(c, BTREE_ID_EXTENTS,
387                                         start, end, NULL) ?:
388                 bch2_btree_delete_range(c, BTREE_ID_XATTRS,
389                                         start, end, NULL) ?:
390                 bch2_btree_delete_range(c, BTREE_ID_DIRENTS,
391                                         start, end, NULL);
392         if (ret)
393                 return ret;
394
395         bch2_trans_init(&trans, c);
396
397         iter = bch2_trans_get_iter(&trans, BTREE_ID_INODES, POS(inode_nr, 0),
398                                    BTREE_ITER_SLOTS|BTREE_ITER_INTENT);
399         do {
400                 struct bkey_s_c k = bch2_btree_iter_peek_slot(iter);
401                 u32 bi_generation = 0;
402
403                 ret = btree_iter_err(k);
404                 if (ret)
405                         break;
406
407                 bch2_fs_inconsistent_on(k.k->type != KEY_TYPE_inode, c,
408                                         "inode %llu not found when deleting",
409                                         inode_nr);
410
411                 switch (k.k->type) {
412                 case KEY_TYPE_inode: {
413                         struct bch_inode_unpacked inode_u;
414
415                         if (!bch2_inode_unpack(bkey_s_c_to_inode(k), &inode_u))
416                                 bi_generation = inode_u.bi_generation + 1;
417                         break;
418                 }
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);
423                         break;
424                 }
425                 }
426
427                 if (!bi_generation) {
428                         bkey_init(&delete.k);
429                         delete.k.p.inode = inode_nr;
430                 } else {
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);
434                 }
435
436                 bch2_trans_update(&trans,
437                                   BTREE_INSERT_ENTRY(iter, &delete.k_i));
438
439                 ret = bch2_trans_commit(&trans, NULL, NULL,
440                                         BTREE_INSERT_ATOMIC|
441                                         BTREE_INSERT_NOFAIL);
442         } while (ret == -EINTR);
443
444         bch2_trans_exit(&trans);
445         return ret;
446 }
447
448 int bch2_inode_find_by_inum(struct bch_fs *c, u64 inode_nr,
449                             struct bch_inode_unpacked *inode)
450 {
451         struct btree_iter iter;
452         struct bkey_s_c k;
453         int ret = -ENOENT;
454
455         for_each_btree_key(&iter, c, BTREE_ID_INODES,
456                            POS(inode_nr, 0),
457                            BTREE_ITER_SLOTS, k) {
458                 switch (k.k->type) {
459                 case KEY_TYPE_inode:
460                         ret = bch2_inode_unpack(bkey_s_c_to_inode(k), inode);
461                         break;
462                 default:
463                         /* hole, not found */
464                         break;
465                 }
466
467                 break;
468         }
469
470         return bch2_btree_iter_unlock(&iter) ?: ret;
471 }
472
473 #ifdef CONFIG_BCACHEFS_DEBUG
474 void bch2_inode_pack_test(void)
475 {
476         struct bch_inode_unpacked *u, test_inodes[] = {
477                 {
478                         .bi_atime       = U64_MAX,
479                         .bi_ctime       = U64_MAX,
480                         .bi_mtime       = U64_MAX,
481                         .bi_otime       = U64_MAX,
482                         .bi_size        = U64_MAX,
483                         .bi_sectors     = U64_MAX,
484                         .bi_uid         = U32_MAX,
485                         .bi_gid         = U32_MAX,
486                         .bi_nlink       = U32_MAX,
487                         .bi_generation  = U32_MAX,
488                         .bi_dev         = U32_MAX,
489                 },
490         };
491
492         for (u = test_inodes;
493              u < test_inodes + ARRAY_SIZE(test_inodes);
494              u++) {
495                 struct bkey_inode_buf p;
496
497                 bch2_inode_pack(&p, u);
498         }
499 }
500 #endif