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