]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/inode.c
Update bcachefs sources to 14ce2a2031 bcachefs: fixes for building in userspace
[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 #define FIELD_BYTES()                                           \
16
17 static const u8 byte_table[8] = { 1, 2, 3, 4, 6, 8, 10, 13 };
18 static const u8 bits_table[8] = {
19         1  * 8 - 1,
20         2  * 8 - 2,
21         3  * 8 - 3,
22         4  * 8 - 4,
23         6  * 8 - 5,
24         8  * 8 - 6,
25         10 * 8 - 7,
26         13 * 8 - 8,
27 };
28
29 static int inode_encode_field(u8 *out, u8 *end, u64 hi, u64 lo)
30 {
31         __be64 in[2] = { cpu_to_be64(hi), cpu_to_be64(lo), };
32         unsigned shift, bytes, bits = likely(!hi)
33                 ? fls64(lo)
34                 : fls64(hi) + 64;
35
36         for (shift = 1; shift <= 8; shift++)
37                 if (bits < bits_table[shift - 1])
38                         goto got_shift;
39
40         BUG();
41 got_shift:
42         bytes = byte_table[shift - 1];
43
44         BUG_ON(out + bytes > end);
45
46         memcpy(out, (u8 *) in + 16 - bytes, bytes);
47         *out |= (1 << 8) >> shift;
48
49         return bytes;
50 }
51
52 static int inode_decode_field(const u8 *in, const u8 *end,
53                               u64 out[2], unsigned *out_bits)
54 {
55         __be64 be[2] = { 0, 0 };
56         unsigned bytes, shift;
57         u8 *p;
58
59         if (in >= end)
60                 return -1;
61
62         if (!*in)
63                 return -1;
64
65         /*
66          * position of highest set bit indicates number of bytes:
67          * shift = number of bits to remove in high byte:
68          */
69         shift   = 8 - __fls(*in); /* 1 <= shift <= 8 */
70         bytes   = byte_table[shift - 1];
71
72         if (in + bytes > end)
73                 return -1;
74
75         p = (u8 *) be + 16 - bytes;
76         memcpy(p, in, bytes);
77         *p ^= (1 << 8) >> shift;
78
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]);
82
83         return bytes;
84 }
85
86 void bch2_inode_pack(struct bkey_inode_buf *packed,
87                      const struct bch_inode_unpacked *inode)
88 {
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;
93
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);
99
100 #define BCH_INODE_FIELD(_name, _bits)                                   \
101         out += inode_encode_field(out, end, 0, inode->_name);           \
102         nr_fields++;                                                    \
103                                                                         \
104         if (inode->_name) {                                             \
105                 last_nonzero_field = out;                               \
106                 last_nonzero_fieldnr = nr_fields;                       \
107         }
108
109         BCH_INODE_FIELDS()
110 #undef  BCH_INODE_FIELD
111
112         out = last_nonzero_field;
113         nr_fields = last_nonzero_fieldnr;
114
115         set_bkey_val_bytes(&packed->inode.k, out - (u8 *) &packed->inode.v);
116         memset(out, 0,
117                (u8 *) &packed->inode.v +
118                bkey_val_bytes(&packed->inode.k) - out);
119
120         SET_INODE_NR_FIELDS(&packed->inode.v, nr_fields);
121
122         if (IS_ENABLED(CONFIG_BCACHEFS_DEBUG)) {
123                 struct bch_inode_unpacked unpacked;
124
125                 int ret = bch2_inode_unpack(inode_i_to_s_c(&packed->inode),
126                                            &unpacked);
127                 BUG_ON(ret);
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);
131
132 #define BCH_INODE_FIELD(_name, _bits)   BUG_ON(unpacked._name != inode->_name);
133                 BCH_INODE_FIELDS()
134 #undef  BCH_INODE_FIELD
135         }
136 }
137
138 int bch2_inode_unpack(struct bkey_s_c_inode inode,
139                       struct bch_inode_unpacked *unpacked)
140 {
141         const u8 *in = inode.v->fields;
142         const u8 *end = (void *) inode.v + bkey_val_bytes(inode.k);
143         u64 field[2];
144         unsigned fieldnr = 0, field_bits;
145         int ret;
146
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);
151
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));     \
157                 return 0;                                               \
158         }                                                               \
159                                                                         \
160         ret = inode_decode_field(in, end, field, &field_bits);          \
161         if (ret < 0)                                                    \
162                 return ret;                                             \
163                                                                         \
164         if (field_bits > sizeof(unpacked->_name) * 8)                   \
165                 return -1;                                              \
166                                                                         \
167         unpacked->_name = field[1];                                     \
168         in += ret;
169
170         BCH_INODE_FIELDS()
171 #undef  BCH_INODE_FIELD
172
173         /* XXX: signal if there were more fields than expected? */
174
175         return 0;
176 }
177
178 static const char *bch2_inode_invalid(const struct bch_fs *c,
179                                       struct bkey_s_c k)
180 {
181         if (k.k->p.offset)
182                 return "nonzero offset";
183
184         switch (k.k->type) {
185         case BCH_INODE_FS: {
186                 struct bkey_s_c_inode inode = bkey_s_c_to_inode(k);
187                 struct bch_inode_unpacked unpacked;
188
189                 if (bkey_val_bytes(k.k) < sizeof(struct bch_inode))
190                         return "incorrect value size";
191
192                 if (k.k->p.inode < BLOCKDEV_INODE_MAX)
193                         return "fs inode in blockdev range";
194
195                 if (INODE_STR_HASH(inode.v) >= BCH_STR_HASH_NR)
196                         return "invalid str hash type";
197
198                 if (bch2_inode_unpack(inode, &unpacked))
199                         return "invalid variable length fields";
200
201                 if (unpacked.bi_data_checksum >= BCH_CSUM_OPT_NR + 1)
202                         return "invalid data checksum type";
203
204                 if (unpacked.bi_compression >= BCH_COMPRESSION_OPT_NR + 1)
205                         return "invalid data checksum type";
206
207                 return NULL;
208         }
209         case BCH_INODE_BLOCKDEV:
210                 if (bkey_val_bytes(k.k) != sizeof(struct bch_inode_blockdev))
211                         return "incorrect value size";
212
213                 if (k.k->p.inode >= BLOCKDEV_INODE_MAX)
214                         return "blockdev inode in fs range";
215
216                 return NULL;
217         case BCH_INODE_GENERATION:
218                 if (bkey_val_bytes(k.k) != sizeof(struct bch_inode_generation))
219                         return "incorrect value size";
220
221                 return NULL;
222         default:
223                 return "invalid type";
224         }
225 }
226
227 static void bch2_inode_to_text(struct bch_fs *c, char *buf,
228                                size_t size, struct bkey_s_c k)
229 {
230         char *out = buf, *end = out + size;
231         struct bkey_s_c_inode inode;
232         struct bch_inode_unpacked unpacked;
233
234         switch (k.k->type) {
235         case BCH_INODE_FS:
236                 inode = bkey_s_c_to_inode(k);
237                 if (bch2_inode_unpack(inode, &unpacked)) {
238                         out += scnprintf(out, end - out, "(unpack error)");
239                         break;
240                 }
241
242 #define BCH_INODE_FIELD(_name, _bits)                                           \
243                 out += scnprintf(out, end - out, #_name ": %llu ", (u64) unpacked._name);
244                 BCH_INODE_FIELDS()
245 #undef  BCH_INODE_FIELD
246                 break;
247         }
248 }
249
250 const struct bkey_ops bch2_bkey_inode_ops = {
251         .key_invalid    = bch2_inode_invalid,
252         .val_to_text    = bch2_inode_to_text,
253 };
254
255 void bch2_inode_init(struct bch_fs *c, struct bch_inode_unpacked *inode_u,
256                      uid_t uid, gid_t gid, umode_t mode, dev_t rdev,
257                      struct bch_inode_unpacked *parent)
258 {
259         s64 now = timespec_to_bch2_time(c,
260                 timespec_trunc(current_kernel_time(),
261                                c->sb.time_precision));
262
263         memset(inode_u, 0, sizeof(*inode_u));
264
265         /* ick */
266         inode_u->bi_flags |= c->opts.str_hash << INODE_STR_HASH_OFFSET;
267         get_random_bytes(&inode_u->bi_hash_seed, sizeof(inode_u->bi_hash_seed));
268
269         inode_u->bi_mode        = mode;
270         inode_u->bi_uid         = uid;
271         inode_u->bi_gid         = gid;
272         inode_u->bi_dev         = rdev;
273         inode_u->bi_atime       = now;
274         inode_u->bi_mtime       = now;
275         inode_u->bi_ctime       = now;
276         inode_u->bi_otime       = now;
277
278         if (parent) {
279 #define BCH_INODE_FIELD(_name)  inode_u->_name = parent->_name;
280                 BCH_INODE_FIELDS_INHERIT()
281 #undef BCH_INODE_FIELD
282         }
283 }
284
285 int bch2_inode_create(struct bch_fs *c, struct bch_inode_unpacked *inode_u,
286                       u64 min, u64 max, u64 *hint)
287 {
288         struct bkey_inode_buf inode_p;
289         struct btree_iter iter;
290         bool searched_from_start = false;
291         int ret;
292
293         if (!max)
294                 max = ULLONG_MAX;
295
296         if (c->opts.inodes_32bit)
297                 max = min_t(u64, max, U32_MAX);
298
299         if (*hint >= max || *hint < min)
300                 *hint = min;
301
302         if (*hint == min)
303                 searched_from_start = true;
304 again:
305         bch2_btree_iter_init(&iter, c, BTREE_ID_INODES, POS(*hint, 0),
306                              BTREE_ITER_INTENT);
307
308         while (1) {
309                 struct bkey_s_c k = bch2_btree_iter_peek_with_holes(&iter);
310                 u32 bi_generation = 0;
311
312                 ret = btree_iter_err(k);
313                 if (ret) {
314                         bch2_btree_iter_unlock(&iter);
315                         return ret;
316                 }
317
318                 switch (k.k->type) {
319                 case BCH_INODE_BLOCKDEV:
320                 case BCH_INODE_FS:
321                         /* slot used */
322                         if (iter.pos.inode == max)
323                                 goto out;
324
325                         bch2_btree_iter_advance_pos(&iter);
326                         break;
327
328                 case BCH_INODE_GENERATION: {
329                         struct bkey_s_c_inode_generation g =
330                                 bkey_s_c_to_inode_generation(k);
331                         bi_generation = le32_to_cpu(g.v->bi_generation);
332                         /* fallthrough: */
333                 }
334                 default:
335                         inode_u->bi_generation = bi_generation;
336
337                         bch2_inode_pack(&inode_p, inode_u);
338                         inode_p.inode.k.p = k.k->p;
339
340                         ret = bch2_btree_insert_at(c, NULL, NULL, NULL,
341                                         BTREE_INSERT_ATOMIC,
342                                         BTREE_INSERT_ENTRY(&iter,
343                                                            &inode_p.inode.k_i));
344
345                         if (ret != -EINTR) {
346                                 bch2_btree_iter_unlock(&iter);
347
348                                 if (!ret) {
349                                         inode_u->bi_inum =
350                                                 inode_p.inode.k.p.inode;
351                                         *hint = inode_p.inode.k.p.inode + 1;
352                                 }
353
354                                 return ret;
355                         }
356
357                         if (ret == -EINTR)
358                                 continue;
359
360                 }
361         }
362 out:
363         bch2_btree_iter_unlock(&iter);
364
365         if (!searched_from_start) {
366                 /* Retry from start */
367                 *hint = min;
368                 searched_from_start = true;
369                 goto again;
370         }
371
372         return -ENOSPC;
373 }
374
375 int bch2_inode_truncate(struct bch_fs *c, u64 inode_nr, u64 new_size,
376                         struct extent_insert_hook *hook, u64 *journal_seq)
377 {
378         return bch2_btree_delete_range(c, BTREE_ID_EXTENTS,
379                                        POS(inode_nr, new_size),
380                                        POS(inode_nr + 1, 0),
381                                        ZERO_VERSION, NULL, hook,
382                                        journal_seq);
383 }
384
385 int bch2_inode_rm(struct bch_fs *c, u64 inode_nr)
386 {
387         struct btree_iter iter;
388         struct bkey_i_inode_generation delete;
389         int ret;
390
391         ret = bch2_inode_truncate(c, inode_nr, 0, NULL, NULL);
392         if (ret < 0)
393                 return ret;
394
395         ret = bch2_btree_delete_range(c, BTREE_ID_XATTRS,
396                                      POS(inode_nr, 0),
397                                      POS(inode_nr + 1, 0),
398                                      ZERO_VERSION, NULL, NULL, NULL);
399         if (ret < 0)
400                 return ret;
401
402         /*
403          * If this was a directory, there shouldn't be any real dirents left -
404          * but there could be whiteouts (from hash collisions) that we should
405          * delete:
406          *
407          * XXX: the dirent could ideally would delete whiteouts when they're no
408          * longer needed
409          */
410         ret = bch2_btree_delete_range(c, BTREE_ID_DIRENTS,
411                                      POS(inode_nr, 0),
412                                      POS(inode_nr + 1, 0),
413                                      ZERO_VERSION, NULL, NULL, NULL);
414         if (ret < 0)
415                 return ret;
416
417         bch2_btree_iter_init(&iter, c, BTREE_ID_INODES, POS(inode_nr, 0),
418                              BTREE_ITER_INTENT);
419         do {
420                 struct bkey_s_c k = bch2_btree_iter_peek_with_holes(&iter);
421                 u32 bi_generation = 0;
422
423                 ret = btree_iter_err(k);
424                 if (ret) {
425                         bch2_btree_iter_unlock(&iter);
426                         return ret;
427                 }
428
429                 bch2_fs_inconsistent_on(k.k->type != BCH_INODE_FS, c,
430                                         "inode %llu not found when deleting",
431                                         inode_nr);
432
433                 switch (k.k->type) {
434                 case BCH_INODE_FS: {
435                         struct bch_inode_unpacked inode_u;
436
437                         if (!bch2_inode_unpack(bkey_s_c_to_inode(k), &inode_u))
438                                 bi_generation = inode_u.bi_generation + 1;
439                         break;
440                 }
441                 case BCH_INODE_GENERATION: {
442                         struct bkey_s_c_inode_generation g =
443                                 bkey_s_c_to_inode_generation(k);
444                         bi_generation = le32_to_cpu(g.v->bi_generation);
445                         break;
446                 }
447                 }
448
449                 if (!bi_generation) {
450                         bkey_init(&delete.k);
451                         delete.k.p.inode = inode_nr;
452                 } else {
453                         bkey_inode_generation_init(&delete.k_i);
454                         delete.k.p.inode = inode_nr;
455                         delete.v.bi_generation = cpu_to_le32(bi_generation);
456                 }
457
458                 ret = bch2_btree_insert_at(c, NULL, NULL, NULL,
459                                 BTREE_INSERT_ATOMIC|
460                                 BTREE_INSERT_NOFAIL,
461                                 BTREE_INSERT_ENTRY(&iter, &delete.k_i));
462         } while (ret == -EINTR);
463
464         bch2_btree_iter_unlock(&iter);
465         return ret;
466 }
467
468 int bch2_inode_find_by_inum(struct bch_fs *c, u64 inode_nr,
469                             struct bch_inode_unpacked *inode)
470 {
471         struct btree_iter iter;
472         struct bkey_s_c k;
473         int ret = -ENOENT;
474
475         for_each_btree_key(&iter, c, BTREE_ID_INODES,
476                            POS(inode_nr, 0),
477                            BTREE_ITER_WITH_HOLES, k) {
478                 switch (k.k->type) {
479                 case BCH_INODE_FS:
480                         ret = bch2_inode_unpack(bkey_s_c_to_inode(k), inode);
481                         break;
482                 default:
483                         /* hole, not found */
484                         break;
485                 }
486
487                 break;
488
489         }
490
491         return bch2_btree_iter_unlock(&iter) ?: ret;
492 }
493
494 int bch2_cached_dev_inode_find_by_uuid(struct bch_fs *c, uuid_le *uuid,
495                                        struct bkey_i_inode_blockdev *ret)
496 {
497         struct btree_iter iter;
498         struct bkey_s_c k;
499
500         for_each_btree_key(&iter, c, BTREE_ID_INODES, POS(0, 0), 0, k) {
501                 if (k.k->p.inode >= BLOCKDEV_INODE_MAX)
502                         break;
503
504                 if (k.k->type == BCH_INODE_BLOCKDEV) {
505                         struct bkey_s_c_inode_blockdev inode =
506                                 bkey_s_c_to_inode_blockdev(k);
507
508                         pr_debug("found inode %llu: %pU (u64s %u)",
509                                  inode.k->p.inode, inode.v->i_uuid.b,
510                                  inode.k->u64s);
511
512                         if (CACHED_DEV(inode.v) &&
513                             !memcmp(uuid, &inode.v->i_uuid, 16)) {
514                                 bkey_reassemble(&ret->k_i, k);
515                                 bch2_btree_iter_unlock(&iter);
516                                 return 0;
517                         }
518                 }
519
520                 bch2_btree_iter_cond_resched(&iter);
521         }
522         bch2_btree_iter_unlock(&iter);
523         return -ENOENT;
524 }
525
526 #ifdef CONFIG_BCACHEFS_DEBUG
527 void bch2_inode_pack_test(void)
528 {
529         struct bch_inode_unpacked *u, test_inodes[] = {
530                 {
531                         .bi_atime       = U64_MAX,
532                         .bi_ctime       = U64_MAX,
533                         .bi_mtime       = U64_MAX,
534                         .bi_otime       = U64_MAX,
535                         .bi_size        = U64_MAX,
536                         .bi_sectors     = U64_MAX,
537                         .bi_uid         = U32_MAX,
538                         .bi_gid         = U32_MAX,
539                         .bi_nlink       = U32_MAX,
540                         .bi_generation  = U32_MAX,
541                         .bi_dev         = U32_MAX,
542                 },
543         };
544
545         for (u = test_inodes;
546              u < test_inodes + ARRAY_SIZE(test_inodes);
547              u++) {
548                 struct bkey_inode_buf p;
549
550                 bch2_inode_pack(&p, u);
551         }
552 }
553 #endif