]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/inode.c
Update bcachefs sources to d4b7ef921a bcachefs: Refactoring
[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                 return NULL;
202         }
203         case BCH_INODE_BLOCKDEV:
204                 if (bkey_val_bytes(k.k) != sizeof(struct bch_inode_blockdev))
205                         return "incorrect value size";
206
207                 if (k.k->p.inode >= BLOCKDEV_INODE_MAX)
208                         return "blockdev inode in fs range";
209
210                 return NULL;
211         case BCH_INODE_GENERATION:
212                 if (bkey_val_bytes(k.k) != sizeof(struct bch_inode_generation))
213                         return "incorrect value size";
214
215                 return NULL;
216         default:
217                 return "invalid type";
218         }
219 }
220
221 static void bch2_inode_to_text(struct bch_fs *c, char *buf,
222                                size_t size, struct bkey_s_c k)
223 {
224         struct bkey_s_c_inode inode;
225         struct bch_inode_unpacked unpacked;
226
227         switch (k.k->type) {
228         case BCH_INODE_FS:
229                 inode = bkey_s_c_to_inode(k);
230                 if (bch2_inode_unpack(inode, &unpacked)) {
231                         scnprintf(buf, size, "(unpack error)");
232                         break;
233                 }
234
235                 scnprintf(buf, size, "i_size %llu", unpacked.bi_size);
236                 break;
237         }
238 }
239
240 const struct bkey_ops bch2_bkey_inode_ops = {
241         .key_invalid    = bch2_inode_invalid,
242         .val_to_text    = bch2_inode_to_text,
243 };
244
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 {
248         s64 now = timespec_to_bch2_time(c, CURRENT_TIME);
249
250         memset(inode_u, 0, sizeof(*inode_u));
251
252         /* ick */
253         inode_u->bi_flags |= c->opts.str_hash << INODE_STR_HASH_OFFSET;
254         get_random_bytes(&inode_u->bi_hash_seed, sizeof(inode_u->bi_hash_seed));
255
256         inode_u->bi_mode        = mode;
257         inode_u->bi_uid         = uid;
258         inode_u->bi_gid         = gid;
259         inode_u->bi_dev         = rdev;
260         inode_u->bi_atime       = now;
261         inode_u->bi_mtime       = now;
262         inode_u->bi_ctime       = now;
263         inode_u->bi_otime       = now;
264 }
265
266 int bch2_inode_create(struct bch_fs *c, struct bch_inode_unpacked *inode_u,
267                       u64 min, u64 max, u64 *hint)
268 {
269         struct bkey_inode_buf inode_p;
270         struct btree_iter iter;
271         bool searched_from_start = false;
272         int ret;
273
274         if (!max)
275                 max = ULLONG_MAX;
276
277         if (c->opts.inodes_32bit)
278                 max = min_t(u64, max, U32_MAX);
279
280         if (*hint >= max || *hint < min)
281                 *hint = min;
282
283         if (*hint == min)
284                 searched_from_start = true;
285 again:
286         bch2_btree_iter_init(&iter, c, BTREE_ID_INODES, POS(*hint, 0),
287                              BTREE_ITER_INTENT);
288
289         while (1) {
290                 struct bkey_s_c k = bch2_btree_iter_peek_with_holes(&iter);
291                 u32 bi_generation = 0;
292
293                 ret = btree_iter_err(k);
294                 if (ret) {
295                         bch2_btree_iter_unlock(&iter);
296                         return ret;
297                 }
298
299                 switch (k.k->type) {
300                 case BCH_INODE_BLOCKDEV:
301                 case BCH_INODE_FS:
302                         /* slot used */
303                         if (iter.pos.inode == max)
304                                 goto out;
305
306                         bch2_btree_iter_advance_pos(&iter);
307                         break;
308
309                 case BCH_INODE_GENERATION: {
310                         struct bkey_s_c_inode_generation g =
311                                 bkey_s_c_to_inode_generation(k);
312                         bi_generation = le32_to_cpu(g.v->bi_generation);
313                         /* fallthrough: */
314                 }
315                 default:
316                         inode_u->bi_generation = bi_generation;
317
318                         bch2_inode_pack(&inode_p, inode_u);
319                         inode_p.inode.k.p = k.k->p;
320
321                         ret = bch2_btree_insert_at(c, NULL, NULL, NULL,
322                                         BTREE_INSERT_ATOMIC,
323                                         BTREE_INSERT_ENTRY(&iter,
324                                                            &inode_p.inode.k_i));
325
326                         if (ret != -EINTR) {
327                                 bch2_btree_iter_unlock(&iter);
328
329                                 if (!ret) {
330                                         inode_u->bi_inum =
331                                                 inode_p.inode.k.p.inode;
332                                         *hint = inode_p.inode.k.p.inode + 1;
333                                 }
334
335                                 return ret;
336                         }
337
338                         if (ret == -EINTR)
339                                 continue;
340
341                 }
342         }
343 out:
344         bch2_btree_iter_unlock(&iter);
345
346         if (!searched_from_start) {
347                 /* Retry from start */
348                 *hint = min;
349                 searched_from_start = true;
350                 goto again;
351         }
352
353         return -ENOSPC;
354 }
355
356 int bch2_inode_truncate(struct bch_fs *c, u64 inode_nr, u64 new_size,
357                         struct extent_insert_hook *hook, u64 *journal_seq)
358 {
359         return bch2_btree_delete_range(c, BTREE_ID_EXTENTS,
360                                        POS(inode_nr, new_size),
361                                        POS(inode_nr + 1, 0),
362                                        ZERO_VERSION, NULL, hook,
363                                        journal_seq);
364 }
365
366 int bch2_inode_rm(struct bch_fs *c, u64 inode_nr)
367 {
368         struct btree_iter iter;
369         struct bkey_i_inode_generation delete;
370         int ret;
371
372         ret = bch2_inode_truncate(c, inode_nr, 0, NULL, NULL);
373         if (ret < 0)
374                 return ret;
375
376         ret = bch2_btree_delete_range(c, BTREE_ID_XATTRS,
377                                      POS(inode_nr, 0),
378                                      POS(inode_nr + 1, 0),
379                                      ZERO_VERSION, NULL, NULL, NULL);
380         if (ret < 0)
381                 return ret;
382
383         /*
384          * If this was a directory, there shouldn't be any real dirents left -
385          * but there could be whiteouts (from hash collisions) that we should
386          * delete:
387          *
388          * XXX: the dirent could ideally would delete whiteouts when they're no
389          * longer needed
390          */
391         ret = bch2_btree_delete_range(c, BTREE_ID_DIRENTS,
392                                      POS(inode_nr, 0),
393                                      POS(inode_nr + 1, 0),
394                                      ZERO_VERSION, NULL, NULL, NULL);
395         if (ret < 0)
396                 return ret;
397
398         bch2_btree_iter_init(&iter, c, BTREE_ID_INODES, POS(inode_nr, 0),
399                              BTREE_ITER_INTENT);
400         do {
401                 struct bkey_s_c k = bch2_btree_iter_peek_with_holes(&iter);
402                 u32 bi_generation = 0;
403
404                 ret = btree_iter_err(k);
405                 if (ret) {
406                         bch2_btree_iter_unlock(&iter);
407                         return ret;
408                 }
409
410                 bch2_fs_inconsistent_on(k.k->type != BCH_INODE_FS, c,
411                                         "inode %llu not found when deleting",
412                                         inode_nr);
413
414                 switch (k.k->type) {
415                 case BCH_INODE_FS: {
416                         struct bch_inode_unpacked inode_u;
417
418                         if (!bch2_inode_unpack(bkey_s_c_to_inode(k), &inode_u))
419                                 bi_generation = cpu_to_le32(inode_u.bi_generation) + 1;
420                         break;
421                 }
422                 case BCH_INODE_GENERATION: {
423                         struct bkey_s_c_inode_generation g =
424                                 bkey_s_c_to_inode_generation(k);
425                         bi_generation = le32_to_cpu(g.v->bi_generation);
426                         break;
427                 }
428                 }
429
430                 if (!bi_generation) {
431                         bkey_init(&delete.k);
432                         delete.k.p.inode = inode_nr;
433                 } else {
434                         bkey_inode_generation_init(&delete.k_i);
435                         delete.k.p.inode = inode_nr;
436                         delete.v.bi_generation = cpu_to_le32(bi_generation);
437                 }
438
439                 ret = bch2_btree_insert_at(c, NULL, NULL, NULL,
440                                 BTREE_INSERT_ATOMIC|
441                                 BTREE_INSERT_NOFAIL,
442                                 BTREE_INSERT_ENTRY(&iter, &delete.k_i));
443         } while (ret == -EINTR);
444
445         bch2_btree_iter_unlock(&iter);
446         return ret;
447 }
448
449 int bch2_inode_find_by_inum(struct bch_fs *c, u64 inode_nr,
450                             struct bch_inode_unpacked *inode)
451 {
452         struct btree_iter iter;
453         struct bkey_s_c k;
454         int ret = -ENOENT;
455
456         for_each_btree_key(&iter, c, BTREE_ID_INODES,
457                            POS(inode_nr, 0),
458                            BTREE_ITER_WITH_HOLES, k) {
459                 switch (k.k->type) {
460                 case BCH_INODE_FS:
461                         ret = bch2_inode_unpack(bkey_s_c_to_inode(k), inode);
462                         break;
463                 default:
464                         /* hole, not found */
465                         break;
466                 }
467
468                 break;
469
470         }
471
472         return bch2_btree_iter_unlock(&iter) ?: ret;
473 }
474
475 int bch2_cached_dev_inode_find_by_uuid(struct bch_fs *c, uuid_le *uuid,
476                                        struct bkey_i_inode_blockdev *ret)
477 {
478         struct btree_iter iter;
479         struct bkey_s_c k;
480
481         for_each_btree_key(&iter, c, BTREE_ID_INODES, POS(0, 0), 0, k) {
482                 if (k.k->p.inode >= BLOCKDEV_INODE_MAX)
483                         break;
484
485                 if (k.k->type == BCH_INODE_BLOCKDEV) {
486                         struct bkey_s_c_inode_blockdev inode =
487                                 bkey_s_c_to_inode_blockdev(k);
488
489                         pr_debug("found inode %llu: %pU (u64s %u)",
490                                  inode.k->p.inode, inode.v->i_uuid.b,
491                                  inode.k->u64s);
492
493                         if (CACHED_DEV(inode.v) &&
494                             !memcmp(uuid, &inode.v->i_uuid, 16)) {
495                                 bkey_reassemble(&ret->k_i, k);
496                                 bch2_btree_iter_unlock(&iter);
497                                 return 0;
498                         }
499                 }
500
501                 bch2_btree_iter_cond_resched(&iter);
502         }
503         bch2_btree_iter_unlock(&iter);
504         return -ENOENT;
505 }
506
507 #ifdef CONFIG_BCACHEFS_DEBUG
508 void bch2_inode_pack_test(void)
509 {
510         struct bch_inode_unpacked *u, test_inodes[] = {
511                 {
512                         .bi_atime       = U64_MAX,
513                         .bi_ctime       = U64_MAX,
514                         .bi_mtime       = U64_MAX,
515                         .bi_otime       = U64_MAX,
516                         .bi_size        = U64_MAX,
517                         .bi_sectors     = U64_MAX,
518                         .bi_uid         = U32_MAX,
519                         .bi_gid         = U32_MAX,
520                         .bi_nlink       = U32_MAX,
521                         .bi_generation  = U32_MAX,
522                         .bi_dev         = U32_MAX,
523                 },
524         };
525
526         for (u = test_inodes;
527              u < test_inodes + ARRAY_SIZE(test_inodes);
528              u++) {
529                 struct bkey_inode_buf p;
530
531                 bch2_inode_pack(&p, u);
532         }
533 }
534 #endif