]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/inode.c
Update bcachefs sources to ed2a5f4260 bcachefs: Add a missing bch2_btree_path_travers...
[bcachefs-tools-debian] / libbcachefs / inode.c
1 // SPDX-License-Identifier: GPL-2.0
2
3 #include "bcachefs.h"
4 #include "btree_key_cache.h"
5 #include "bkey_methods.h"
6 #include "btree_update.h"
7 #include "buckets.h"
8 #include "error.h"
9 #include "extents.h"
10 #include "extent_update.h"
11 #include "inode.h"
12 #include "str_hash.h"
13 #include "subvolume.h"
14 #include "varint.h"
15
16 #include <linux/random.h>
17
18 #include <asm/unaligned.h>
19
20 const char * const bch2_inode_opts[] = {
21 #define x(name, ...)    #name,
22         BCH_INODE_OPTS()
23 #undef  x
24         NULL,
25 };
26
27 static const u8 byte_table[8] = { 1, 2, 3, 4, 6, 8, 10, 13 };
28
29 static int inode_decode_field(const u8 *in, const u8 *end,
30                               u64 out[2], unsigned *out_bits)
31 {
32         __be64 be[2] = { 0, 0 };
33         unsigned bytes, shift;
34         u8 *p;
35
36         if (in >= end)
37                 return -1;
38
39         if (!*in)
40                 return -1;
41
42         /*
43          * position of highest set bit indicates number of bytes:
44          * shift = number of bits to remove in high byte:
45          */
46         shift   = 8 - __fls(*in); /* 1 <= shift <= 8 */
47         bytes   = byte_table[shift - 1];
48
49         if (in + bytes > end)
50                 return -1;
51
52         p = (u8 *) be + 16 - bytes;
53         memcpy(p, in, bytes);
54         *p ^= (1 << 8) >> shift;
55
56         out[0] = be64_to_cpu(be[0]);
57         out[1] = be64_to_cpu(be[1]);
58         *out_bits = out[0] ? 64 + fls64(out[0]) : fls64(out[1]);
59
60         return bytes;
61 }
62
63 static inline void bch2_inode_pack_inlined(struct bkey_inode_buf *packed,
64                                            const struct bch_inode_unpacked *inode)
65 {
66         struct bkey_i_inode_v3 *k = &packed->inode;
67         u8 *out = k->v.fields;
68         u8 *end = (void *) &packed[1];
69         u8 *last_nonzero_field = out;
70         unsigned nr_fields = 0, last_nonzero_fieldnr = 0;
71         unsigned bytes;
72         int ret;
73
74         bkey_inode_v3_init(&packed->inode.k_i);
75         packed->inode.k.p.offset        = inode->bi_inum;
76         packed->inode.v.bi_journal_seq  = cpu_to_le64(inode->bi_journal_seq);
77         packed->inode.v.bi_hash_seed    = inode->bi_hash_seed;
78         packed->inode.v.bi_flags        = cpu_to_le64(inode->bi_flags);
79         packed->inode.v.bi_sectors      = cpu_to_le64(inode->bi_sectors);
80         packed->inode.v.bi_size         = cpu_to_le64(inode->bi_size);
81         packed->inode.v.bi_version      = cpu_to_le64(inode->bi_version);
82         SET_INODEv3_MODE(&packed->inode.v, inode->bi_mode);
83         SET_INODEv3_FIELDS_START(&packed->inode.v, INODEv3_FIELDS_START_CUR);
84
85
86 #define x(_name, _bits)                                                 \
87         nr_fields++;                                                    \
88                                                                         \
89         if (inode->_name) {                                             \
90                 ret = bch2_varint_encode_fast(out, inode->_name);       \
91                 out += ret;                                             \
92                                                                         \
93                 if (_bits > 64)                                         \
94                         *out++ = 0;                                     \
95                                                                         \
96                 last_nonzero_field = out;                               \
97                 last_nonzero_fieldnr = nr_fields;                       \
98         } else {                                                        \
99                 *out++ = 0;                                             \
100                                                                         \
101                 if (_bits > 64)                                         \
102                         *out++ = 0;                                     \
103         }
104
105         BCH_INODE_FIELDS_v3()
106 #undef  x
107         BUG_ON(out > end);
108
109         out = last_nonzero_field;
110         nr_fields = last_nonzero_fieldnr;
111
112         bytes = out - (u8 *) &packed->inode.v;
113         set_bkey_val_bytes(&packed->inode.k, bytes);
114         memset_u64s_tail(&packed->inode.v, 0, bytes);
115
116         SET_INODEv3_NR_FIELDS(&k->v, nr_fields);
117
118         if (IS_ENABLED(CONFIG_BCACHEFS_DEBUG)) {
119                 struct bch_inode_unpacked unpacked;
120
121                 int ret = bch2_inode_unpack(bkey_i_to_s_c(&packed->inode.k_i),
122                                            &unpacked);
123                 BUG_ON(ret);
124                 BUG_ON(unpacked.bi_inum         != inode->bi_inum);
125                 BUG_ON(unpacked.bi_hash_seed    != inode->bi_hash_seed);
126                 BUG_ON(unpacked.bi_sectors      != inode->bi_sectors);
127                 BUG_ON(unpacked.bi_size         != inode->bi_size);
128                 BUG_ON(unpacked.bi_version      != inode->bi_version);
129                 BUG_ON(unpacked.bi_mode         != inode->bi_mode);
130
131 #define x(_name, _bits) if (unpacked._name != inode->_name)             \
132                         panic("unpacked %llu should be %llu",           \
133                               (u64) unpacked._name, (u64) inode->_name);
134                 BCH_INODE_FIELDS_v3()
135 #undef  x
136         }
137 }
138
139 void bch2_inode_pack(struct bkey_inode_buf *packed,
140                      const struct bch_inode_unpacked *inode)
141 {
142         bch2_inode_pack_inlined(packed, inode);
143 }
144
145 static noinline int bch2_inode_unpack_v1(struct bkey_s_c_inode inode,
146                                 struct bch_inode_unpacked *unpacked)
147 {
148         const u8 *in = inode.v->fields;
149         const u8 *end = bkey_val_end(inode);
150         u64 field[2];
151         unsigned fieldnr = 0, field_bits;
152         int ret;
153
154 #define x(_name, _bits)                                 \
155         if (fieldnr++ == INODE_NR_FIELDS(inode.v)) {                    \
156                 unsigned offset = offsetof(struct bch_inode_unpacked, _name);\
157                 memset((void *) unpacked + offset, 0,                   \
158                        sizeof(*unpacked) - offset);                     \
159                 return 0;                                               \
160         }                                                               \
161                                                                         \
162         ret = inode_decode_field(in, end, field, &field_bits);          \
163         if (ret < 0)                                                    \
164                 return ret;                                             \
165                                                                         \
166         if (field_bits > sizeof(unpacked->_name) * 8)                   \
167                 return -1;                                              \
168                                                                         \
169         unpacked->_name = field[1];                                     \
170         in += ret;
171
172         BCH_INODE_FIELDS_v2()
173 #undef  x
174
175         /* XXX: signal if there were more fields than expected? */
176         return 0;
177 }
178
179 static int bch2_inode_unpack_v2(struct bch_inode_unpacked *unpacked,
180                                 const u8 *in, const u8 *end,
181                                 unsigned nr_fields)
182 {
183         unsigned fieldnr = 0;
184         int ret;
185         u64 v[2];
186
187 #define x(_name, _bits)                                                 \
188         if (fieldnr < nr_fields) {                                      \
189                 ret = bch2_varint_decode_fast(in, end, &v[0]);          \
190                 if (ret < 0)                                            \
191                         return ret;                                     \
192                 in += ret;                                              \
193                                                                         \
194                 if (_bits > 64) {                                       \
195                         ret = bch2_varint_decode_fast(in, end, &v[1]);  \
196                         if (ret < 0)                                    \
197                                 return ret;                             \
198                         in += ret;                                      \
199                 } else {                                                \
200                         v[1] = 0;                                       \
201                 }                                                       \
202         } else {                                                        \
203                 v[0] = v[1] = 0;                                        \
204         }                                                               \
205                                                                         \
206         unpacked->_name = v[0];                                         \
207         if (v[1] || v[0] != unpacked->_name)                            \
208                 return -1;                                              \
209         fieldnr++;
210
211         BCH_INODE_FIELDS_v2()
212 #undef  x
213
214         /* XXX: signal if there were more fields than expected? */
215         return 0;
216 }
217
218 static int bch2_inode_unpack_v3(struct bkey_s_c k,
219                                 struct bch_inode_unpacked *unpacked)
220 {
221         struct bkey_s_c_inode_v3 inode = bkey_s_c_to_inode_v3(k);
222         const u8 *in = inode.v->fields;
223         const u8 *end = bkey_val_end(inode);
224         unsigned nr_fields = INODEv3_NR_FIELDS(inode.v);
225         unsigned fieldnr = 0;
226         int ret;
227         u64 v[2];
228
229         unpacked->bi_inum       = inode.k->p.offset;
230         unpacked->bi_journal_seq= le64_to_cpu(inode.v->bi_journal_seq);
231         unpacked->bi_hash_seed  = inode.v->bi_hash_seed;
232         unpacked->bi_flags      = le64_to_cpu(inode.v->bi_flags);
233         unpacked->bi_sectors    = le64_to_cpu(inode.v->bi_sectors);
234         unpacked->bi_size       = le64_to_cpu(inode.v->bi_size);
235         unpacked->bi_version    = le64_to_cpu(inode.v->bi_version);
236         unpacked->bi_mode       = INODEv3_MODE(inode.v);
237
238 #define x(_name, _bits)                                                 \
239         if (fieldnr < nr_fields) {                                      \
240                 ret = bch2_varint_decode_fast(in, end, &v[0]);          \
241                 if (ret < 0)                                            \
242                         return ret;                                     \
243                 in += ret;                                              \
244                                                                         \
245                 if (_bits > 64) {                                       \
246                         ret = bch2_varint_decode_fast(in, end, &v[1]);  \
247                         if (ret < 0)                                    \
248                                 return ret;                             \
249                         in += ret;                                      \
250                 } else {                                                \
251                         v[1] = 0;                                       \
252                 }                                                       \
253         } else {                                                        \
254                 v[0] = v[1] = 0;                                        \
255         }                                                               \
256                                                                         \
257         unpacked->_name = v[0];                                         \
258         if (v[1] || v[0] != unpacked->_name)                            \
259                 return -1;                                              \
260         fieldnr++;
261
262         BCH_INODE_FIELDS_v3()
263 #undef  x
264
265         /* XXX: signal if there were more fields than expected? */
266         return 0;
267 }
268
269 static noinline int bch2_inode_unpack_slowpath(struct bkey_s_c k,
270                                                struct bch_inode_unpacked *unpacked)
271 {
272         memset(unpacked, 0, sizeof(*unpacked));
273
274         switch (k.k->type) {
275         case KEY_TYPE_inode: {
276                 struct bkey_s_c_inode inode = bkey_s_c_to_inode(k);
277
278                 unpacked->bi_inum       = inode.k->p.offset;
279                 unpacked->bi_journal_seq= 0;
280                 unpacked->bi_hash_seed  = inode.v->bi_hash_seed;
281                 unpacked->bi_flags      = le32_to_cpu(inode.v->bi_flags);
282                 unpacked->bi_mode       = le16_to_cpu(inode.v->bi_mode);
283
284                 if (INODE_NEW_VARINT(inode.v)) {
285                         return bch2_inode_unpack_v2(unpacked, inode.v->fields,
286                                                     bkey_val_end(inode),
287                                                     INODE_NR_FIELDS(inode.v));
288                 } else {
289                         return bch2_inode_unpack_v1(inode, unpacked);
290                 }
291                 break;
292         }
293         case KEY_TYPE_inode_v2: {
294                 struct bkey_s_c_inode_v2 inode = bkey_s_c_to_inode_v2(k);
295
296                 unpacked->bi_inum       = inode.k->p.offset;
297                 unpacked->bi_journal_seq= le64_to_cpu(inode.v->bi_journal_seq);
298                 unpacked->bi_hash_seed  = inode.v->bi_hash_seed;
299                 unpacked->bi_flags      = le64_to_cpu(inode.v->bi_flags);
300                 unpacked->bi_mode       = le16_to_cpu(inode.v->bi_mode);
301
302                 return bch2_inode_unpack_v2(unpacked, inode.v->fields,
303                                             bkey_val_end(inode),
304                                             INODEv2_NR_FIELDS(inode.v));
305         }
306         default:
307                 BUG();
308         }
309 }
310
311 int bch2_inode_unpack(struct bkey_s_c k,
312                       struct bch_inode_unpacked *unpacked)
313 {
314         if (likely(k.k->type == KEY_TYPE_inode_v3))
315                 return bch2_inode_unpack_v3(k, unpacked);
316         return bch2_inode_unpack_slowpath(k, unpacked);
317 }
318
319 int bch2_inode_peek(struct btree_trans *trans,
320                     struct btree_iter *iter,
321                     struct bch_inode_unpacked *inode,
322                     subvol_inum inum, unsigned flags)
323 {
324         struct bkey_s_c k;
325         u32 snapshot;
326         int ret;
327
328         ret = bch2_subvolume_get_snapshot(trans, inum.subvol, &snapshot);
329         if (ret)
330                 return ret;
331
332         bch2_trans_iter_init(trans, iter, BTREE_ID_inodes,
333                              SPOS(0, inum.inum, snapshot),
334                              flags|BTREE_ITER_CACHED);
335         k = bch2_btree_iter_peek_slot(iter);
336         ret = bkey_err(k);
337         if (ret)
338                 goto err;
339
340         ret = bkey_is_inode(k.k) ? 0 : -ENOENT;
341         if (ret)
342                 goto err;
343
344         ret = bch2_inode_unpack(k, inode);
345         if (ret)
346                 goto err;
347
348         return 0;
349 err:
350         bch2_trans_iter_exit(trans, iter);
351         return ret;
352 }
353
354 int bch2_inode_write(struct btree_trans *trans,
355                      struct btree_iter *iter,
356                      struct bch_inode_unpacked *inode)
357 {
358         struct bkey_inode_buf *inode_p;
359
360         inode_p = bch2_trans_kmalloc(trans, sizeof(*inode_p));
361         if (IS_ERR(inode_p))
362                 return PTR_ERR(inode_p);
363
364         bch2_inode_pack_inlined(inode_p, inode);
365         inode_p->inode.k.p.snapshot = iter->snapshot;
366         return bch2_trans_update(trans, iter, &inode_p->inode.k_i, 0);
367 }
368
369 struct bkey_i *bch2_inode_to_v3(struct btree_trans *trans, struct bkey_i *k)
370 {
371         struct bch_inode_unpacked u;
372         struct bkey_inode_buf *inode_p;
373         int ret;
374
375         if (!bkey_is_inode(&k->k))
376                 return ERR_PTR(-ENOENT);
377
378         inode_p = bch2_trans_kmalloc(trans, sizeof(*inode_p));
379         if (IS_ERR(inode_p))
380                 return ERR_CAST(inode_p);
381
382         ret = bch2_inode_unpack(bkey_i_to_s_c(k), &u);
383         if (ret)
384                 return ERR_PTR(ret);
385
386         bch2_inode_pack(inode_p, &u);
387         return &inode_p->inode.k_i;
388 }
389
390 static int __bch2_inode_invalid(struct bkey_s_c k, struct printbuf *err)
391 {
392         struct bch_inode_unpacked unpacked;
393
394         if (k.k->p.inode) {
395                 prt_printf(err, "nonzero k.p.inode");
396                 return -BCH_ERR_invalid_bkey;
397         }
398
399         if (k.k->p.offset < BLOCKDEV_INODE_MAX) {
400                 prt_printf(err, "fs inode in blockdev range");
401                 return -BCH_ERR_invalid_bkey;
402         }
403
404         if (bch2_inode_unpack(k, &unpacked)) {
405                 prt_printf(err, "invalid variable length fields");
406                 return -BCH_ERR_invalid_bkey;
407         }
408
409         if (unpacked.bi_data_checksum >= BCH_CSUM_OPT_NR + 1) {
410                 prt_printf(err, "invalid data checksum type (%u >= %u",
411                         unpacked.bi_data_checksum, BCH_CSUM_OPT_NR + 1);
412                 return -BCH_ERR_invalid_bkey;
413         }
414
415         if (unpacked.bi_compression >= BCH_COMPRESSION_OPT_NR + 1) {
416                 prt_printf(err, "invalid data checksum type (%u >= %u)",
417                        unpacked.bi_compression, BCH_COMPRESSION_OPT_NR + 1);
418                 return -BCH_ERR_invalid_bkey;
419         }
420
421         if ((unpacked.bi_flags & BCH_INODE_UNLINKED) &&
422             unpacked.bi_nlink != 0) {
423                 prt_printf(err, "flagged as unlinked but bi_nlink != 0");
424                 return -BCH_ERR_invalid_bkey;
425         }
426
427         if (unpacked.bi_subvol && !S_ISDIR(unpacked.bi_mode)) {
428                 prt_printf(err, "subvolume root but not a directory");
429                 return -BCH_ERR_invalid_bkey;
430         }
431
432         return 0;
433 }
434
435 int bch2_inode_invalid(const struct bch_fs *c, struct bkey_s_c k,
436                        int rw, struct printbuf *err)
437 {
438         struct bkey_s_c_inode inode = bkey_s_c_to_inode(k);
439
440         if (bkey_val_bytes(k.k) < sizeof(*inode.v)) {
441                 prt_printf(err, "incorrect value size (%zu < %zu)",
442                        bkey_val_bytes(k.k), sizeof(*inode.v));
443                 return -BCH_ERR_invalid_bkey;
444         }
445
446         if (INODE_STR_HASH(inode.v) >= BCH_STR_HASH_NR) {
447                 prt_printf(err, "invalid str hash type (%llu >= %u)",
448                        INODE_STR_HASH(inode.v), BCH_STR_HASH_NR);
449                 return -BCH_ERR_invalid_bkey;
450         }
451
452         return __bch2_inode_invalid(k, err);
453 }
454
455 int bch2_inode_v2_invalid(const struct bch_fs *c, struct bkey_s_c k,
456                           int rw, struct printbuf *err)
457 {
458         struct bkey_s_c_inode_v2 inode = bkey_s_c_to_inode_v2(k);
459
460         if (bkey_val_bytes(k.k) < sizeof(*inode.v)) {
461                 prt_printf(err, "incorrect value size (%zu < %zu)",
462                        bkey_val_bytes(k.k), sizeof(*inode.v));
463                 return -BCH_ERR_invalid_bkey;
464         }
465
466         if (INODEv2_STR_HASH(inode.v) >= BCH_STR_HASH_NR) {
467                 prt_printf(err, "invalid str hash type (%llu >= %u)",
468                        INODEv2_STR_HASH(inode.v), BCH_STR_HASH_NR);
469                 return -BCH_ERR_invalid_bkey;
470         }
471
472         return __bch2_inode_invalid(k, err);
473 }
474
475 int bch2_inode_v3_invalid(const struct bch_fs *c, struct bkey_s_c k,
476                           int rw, struct printbuf *err)
477 {
478         struct bkey_s_c_inode_v3 inode = bkey_s_c_to_inode_v3(k);
479
480         if (bkey_val_bytes(k.k) < sizeof(*inode.v)) {
481                 prt_printf(err, "incorrect value size (%zu < %zu)",
482                        bkey_val_bytes(k.k), sizeof(*inode.v));
483                 return -BCH_ERR_invalid_bkey;
484         }
485
486         if (INODEv3_FIELDS_START(inode.v) < INODEv3_FIELDS_START_INITIAL ||
487             INODEv3_FIELDS_START(inode.v) > bkey_val_u64s(inode.k)) {
488                 prt_printf(err, "invalid fields_start (got %llu, min %u max %zu)",
489                        INODEv3_FIELDS_START(inode.v),
490                        INODEv3_FIELDS_START_INITIAL,
491                        bkey_val_u64s(inode.k));
492                 return -BCH_ERR_invalid_bkey;
493         }
494
495         if (INODEv3_STR_HASH(inode.v) >= BCH_STR_HASH_NR) {
496                 prt_printf(err, "invalid str hash type (%llu >= %u)",
497                        INODEv3_STR_HASH(inode.v), BCH_STR_HASH_NR);
498                 return -BCH_ERR_invalid_bkey;
499         }
500
501         return __bch2_inode_invalid(k, err);
502 }
503
504 static void __bch2_inode_unpacked_to_text(struct printbuf *out,
505                                           struct bch_inode_unpacked *inode)
506 {
507         prt_printf(out, "mode %o flags %x journal_seq %llu bi_size %llu bi_sectors %llu bi_version %llu",
508                inode->bi_mode, inode->bi_flags,
509                inode->bi_journal_seq,
510                inode->bi_size,
511                inode->bi_sectors,
512                inode->bi_version);
513
514 #define x(_name, _bits)                                         \
515         prt_printf(out, " "#_name " %llu", (u64) inode->_name);
516         BCH_INODE_FIELDS_v3()
517 #undef  x
518 }
519
520 void bch2_inode_unpacked_to_text(struct printbuf *out, struct bch_inode_unpacked *inode)
521 {
522         prt_printf(out, "inum: %llu ", inode->bi_inum);
523         __bch2_inode_unpacked_to_text(out, inode);
524 }
525
526 void bch2_inode_to_text(struct printbuf *out, struct bch_fs *c, struct bkey_s_c k)
527 {
528         struct bch_inode_unpacked inode;
529
530         if (bch2_inode_unpack(k, &inode)) {
531                 prt_printf(out, "(unpack error)");
532                 return;
533         }
534
535         __bch2_inode_unpacked_to_text(out, &inode);
536 }
537
538 int bch2_inode_generation_invalid(const struct bch_fs *c, struct bkey_s_c k,
539                                   int rw, struct printbuf *err)
540 {
541         if (k.k->p.inode) {
542                 prt_printf(err, "nonzero k.p.inode");
543                 return -BCH_ERR_invalid_bkey;
544         }
545
546         if (bkey_val_bytes(k.k) != sizeof(struct bch_inode_generation)) {
547                 prt_printf(err, "incorrect value size (%zu != %zu)",
548                        bkey_val_bytes(k.k), sizeof(struct bch_inode_generation));
549                 return -BCH_ERR_invalid_bkey;
550         }
551
552         return 0;
553 }
554
555 void bch2_inode_generation_to_text(struct printbuf *out, struct bch_fs *c,
556                                    struct bkey_s_c k)
557 {
558         struct bkey_s_c_inode_generation gen = bkey_s_c_to_inode_generation(k);
559
560         prt_printf(out, "generation: %u", le32_to_cpu(gen.v->bi_generation));
561 }
562
563 void bch2_inode_init_early(struct bch_fs *c,
564                            struct bch_inode_unpacked *inode_u)
565 {
566         enum bch_str_hash_type str_hash =
567                 bch2_str_hash_opt_to_type(c, c->opts.str_hash);
568
569         memset(inode_u, 0, sizeof(*inode_u));
570
571         /* ick */
572         inode_u->bi_flags |= str_hash << INODE_STR_HASH_OFFSET;
573         get_random_bytes(&inode_u->bi_hash_seed,
574                          sizeof(inode_u->bi_hash_seed));
575 }
576
577 void bch2_inode_init_late(struct bch_inode_unpacked *inode_u, u64 now,
578                           uid_t uid, gid_t gid, umode_t mode, dev_t rdev,
579                           struct bch_inode_unpacked *parent)
580 {
581         inode_u->bi_mode        = mode;
582         inode_u->bi_uid         = uid;
583         inode_u->bi_gid         = gid;
584         inode_u->bi_dev         = rdev;
585         inode_u->bi_atime       = now;
586         inode_u->bi_mtime       = now;
587         inode_u->bi_ctime       = now;
588         inode_u->bi_otime       = now;
589
590         if (parent && parent->bi_mode & S_ISGID) {
591                 inode_u->bi_gid = parent->bi_gid;
592                 if (S_ISDIR(mode))
593                         inode_u->bi_mode |= S_ISGID;
594         }
595
596         if (parent) {
597 #define x(_name, ...)   inode_u->bi_##_name = parent->bi_##_name;
598                 BCH_INODE_OPTS()
599 #undef x
600         }
601 }
602
603 void bch2_inode_init(struct bch_fs *c, struct bch_inode_unpacked *inode_u,
604                      uid_t uid, gid_t gid, umode_t mode, dev_t rdev,
605                      struct bch_inode_unpacked *parent)
606 {
607         bch2_inode_init_early(c, inode_u);
608         bch2_inode_init_late(inode_u, bch2_current_time(c),
609                              uid, gid, mode, rdev, parent);
610 }
611
612 static inline u32 bkey_generation(struct bkey_s_c k)
613 {
614         switch (k.k->type) {
615         case KEY_TYPE_inode:
616         case KEY_TYPE_inode_v2:
617                 BUG();
618         case KEY_TYPE_inode_generation:
619                 return le32_to_cpu(bkey_s_c_to_inode_generation(k).v->bi_generation);
620         default:
621                 return 0;
622         }
623 }
624
625 /*
626  * This just finds an empty slot:
627  */
628 int bch2_inode_create(struct btree_trans *trans,
629                       struct btree_iter *iter,
630                       struct bch_inode_unpacked *inode_u,
631                       u32 snapshot, u64 cpu)
632 {
633         struct bch_fs *c = trans->c;
634         struct bkey_s_c k;
635         u64 min, max, start, pos, *hint;
636         int ret = 0;
637         unsigned bits = (c->opts.inodes_32bit ? 31 : 63);
638
639         if (c->opts.shard_inode_numbers) {
640                 bits -= c->inode_shard_bits;
641
642                 min = (cpu << bits);
643                 max = (cpu << bits) | ~(ULLONG_MAX << bits);
644
645                 min = max_t(u64, min, BLOCKDEV_INODE_MAX);
646                 hint = c->unused_inode_hints + cpu;
647         } else {
648                 min = BLOCKDEV_INODE_MAX;
649                 max = ~(ULLONG_MAX << bits);
650                 hint = c->unused_inode_hints;
651         }
652
653         start = READ_ONCE(*hint);
654
655         if (start >= max || start < min)
656                 start = min;
657
658         pos = start;
659         bch2_trans_iter_init(trans, iter, BTREE_ID_inodes, POS(0, pos),
660                              BTREE_ITER_ALL_SNAPSHOTS|
661                              BTREE_ITER_INTENT);
662 again:
663         while ((k = bch2_btree_iter_peek(iter)).k &&
664                !(ret = bkey_err(k)) &&
665                bkey_lt(k.k->p, POS(0, max))) {
666                 while (pos < iter->pos.offset) {
667                         if (!bch2_btree_key_cache_find(c, BTREE_ID_inodes, POS(0, pos)))
668                                 goto found_slot;
669
670                         pos++;
671                 }
672
673                 if (k.k->p.snapshot == snapshot &&
674                     !bkey_is_inode(k.k) &&
675                     !bch2_btree_key_cache_find(c, BTREE_ID_inodes, SPOS(0, pos, snapshot))) {
676                         bch2_btree_iter_advance(iter);
677                         continue;
678                 }
679
680                 /*
681                  * We don't need to iterate over keys in every snapshot once
682                  * we've found just one:
683                  */
684                 pos = iter->pos.offset + 1;
685                 bch2_btree_iter_set_pos(iter, POS(0, pos));
686         }
687
688         while (!ret && pos < max) {
689                 if (!bch2_btree_key_cache_find(c, BTREE_ID_inodes, POS(0, pos)))
690                         goto found_slot;
691
692                 pos++;
693         }
694
695         if (!ret && start == min)
696                 ret = -BCH_ERR_ENOSPC_inode_create;
697
698         if (ret) {
699                 bch2_trans_iter_exit(trans, iter);
700                 return ret;
701         }
702
703         /* Retry from start */
704         pos = start = min;
705         bch2_btree_iter_set_pos(iter, POS(0, pos));
706         goto again;
707 found_slot:
708         bch2_btree_iter_set_pos(iter, SPOS(0, pos, snapshot));
709         k = bch2_btree_iter_peek_slot(iter);
710         ret = bkey_err(k);
711         if (ret) {
712                 bch2_trans_iter_exit(trans, iter);
713                 return ret;
714         }
715
716         /* We may have raced while the iterator wasn't pointing at pos: */
717         if (bkey_is_inode(k.k) ||
718             bch2_btree_key_cache_find(c, BTREE_ID_inodes, k.k->p))
719                 goto again;
720
721         *hint                   = k.k->p.offset;
722         inode_u->bi_inum        = k.k->p.offset;
723         inode_u->bi_generation  = bkey_generation(k);
724         return 0;
725 }
726
727 static int bch2_inode_delete_keys(struct btree_trans *trans,
728                                   subvol_inum inum, enum btree_id id)
729 {
730         struct btree_iter iter;
731         struct bkey_s_c k;
732         struct bkey_i delete;
733         u32 snapshot;
734         int ret = 0;
735
736         /*
737          * We're never going to be deleting extents, no need to use an extent
738          * iterator:
739          */
740         bch2_trans_iter_init(trans, &iter, id, POS(inum.inum, 0),
741                              BTREE_ITER_INTENT);
742
743         while (1) {
744                 bch2_trans_begin(trans);
745
746                 ret = bch2_subvolume_get_snapshot(trans, inum.subvol, &snapshot);
747                 if (ret)
748                         goto err;
749
750                 bch2_btree_iter_set_snapshot(&iter, snapshot);
751
752                 k = bch2_btree_iter_peek_upto(&iter, POS(inum.inum, U64_MAX));
753                 ret = bkey_err(k);
754                 if (ret)
755                         goto err;
756
757                 if (!k.k)
758                         break;
759
760                 bkey_init(&delete.k);
761                 delete.k.p = iter.pos;
762
763                 if (iter.flags & BTREE_ITER_IS_EXTENTS) {
764                         bch2_key_resize(&delete.k, k.k->p.offset - iter.pos.offset);
765
766                         ret = bch2_extent_trim_atomic(trans, &iter, &delete);
767                         if (ret)
768                                 goto err;
769                 }
770
771                 ret = bch2_trans_update(trans, &iter, &delete, 0) ?:
772                       bch2_trans_commit(trans, NULL, NULL,
773                                         BTREE_INSERT_NOFAIL);
774 err:
775                 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
776                         break;
777         }
778
779         bch2_trans_iter_exit(trans, &iter);
780         return ret;
781 }
782
783 int bch2_inode_rm(struct bch_fs *c, subvol_inum inum)
784 {
785         struct btree_trans trans;
786         struct btree_iter iter = { NULL };
787         struct bkey_i_inode_generation delete;
788         struct bch_inode_unpacked inode_u;
789         struct bkey_s_c k;
790         u32 snapshot;
791         int ret;
792
793         bch2_trans_init(&trans, c, 0, 1024);
794
795         /*
796          * If this was a directory, there shouldn't be any real dirents left -
797          * but there could be whiteouts (from hash collisions) that we should
798          * delete:
799          *
800          * XXX: the dirent could ideally would delete whiteouts when they're no
801          * longer needed
802          */
803         ret   = bch2_inode_delete_keys(&trans, inum, BTREE_ID_extents) ?:
804                 bch2_inode_delete_keys(&trans, inum, BTREE_ID_xattrs) ?:
805                 bch2_inode_delete_keys(&trans, inum, BTREE_ID_dirents);
806         if (ret)
807                 goto err;
808 retry:
809         bch2_trans_begin(&trans);
810
811         ret = bch2_subvolume_get_snapshot(&trans, inum.subvol, &snapshot);
812         if (ret)
813                 goto err;
814
815         bch2_trans_iter_init(&trans, &iter, BTREE_ID_inodes,
816                              SPOS(0, inum.inum, snapshot),
817                              BTREE_ITER_INTENT|BTREE_ITER_CACHED);
818         k = bch2_btree_iter_peek_slot(&iter);
819
820         ret = bkey_err(k);
821         if (ret)
822                 goto err;
823
824         if (!bkey_is_inode(k.k)) {
825                 bch2_fs_inconsistent(trans.c,
826                                      "inode %llu not found when deleting",
827                                      inum.inum);
828                 ret = -EIO;
829                 goto err;
830         }
831
832         bch2_inode_unpack(k, &inode_u);
833
834         /* Subvolume root? */
835         BUG_ON(inode_u.bi_subvol);
836
837         bkey_inode_generation_init(&delete.k_i);
838         delete.k.p = iter.pos;
839         delete.v.bi_generation = cpu_to_le32(inode_u.bi_generation + 1);
840
841         ret   = bch2_trans_update(&trans, &iter, &delete.k_i, 0) ?:
842                 bch2_trans_commit(&trans, NULL, NULL,
843                                 BTREE_INSERT_NOFAIL);
844 err:
845         bch2_trans_iter_exit(&trans, &iter);
846         if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
847                 goto retry;
848
849         bch2_trans_exit(&trans);
850         return ret;
851 }
852
853 int bch2_inode_find_by_inum_trans(struct btree_trans *trans,
854                                   subvol_inum inum,
855                                   struct bch_inode_unpacked *inode)
856 {
857         struct btree_iter iter;
858         int ret;
859
860         ret = bch2_inode_peek(trans, &iter, inode, inum, 0);
861         if (!ret)
862                 bch2_trans_iter_exit(trans, &iter);
863         return ret;
864 }
865
866 int bch2_inode_find_by_inum(struct bch_fs *c, subvol_inum inum,
867                             struct bch_inode_unpacked *inode)
868 {
869         return bch2_trans_do(c, NULL, NULL, 0,
870                 bch2_inode_find_by_inum_trans(&trans, inum, inode));
871 }
872
873 int bch2_inode_nlink_inc(struct bch_inode_unpacked *bi)
874 {
875         if (bi->bi_flags & BCH_INODE_UNLINKED)
876                 bi->bi_flags &= ~BCH_INODE_UNLINKED;
877         else {
878                 if (bi->bi_nlink == U32_MAX)
879                         return -EINVAL;
880
881                 bi->bi_nlink++;
882         }
883
884         return 0;
885 }
886
887 void bch2_inode_nlink_dec(struct btree_trans *trans, struct bch_inode_unpacked *bi)
888 {
889         if (bi->bi_nlink && (bi->bi_flags & BCH_INODE_UNLINKED)) {
890                 bch2_trans_inconsistent(trans, "inode %llu unlinked but link count nonzero",
891                                         bi->bi_inum);
892                 return;
893         }
894
895         if (bi->bi_flags & BCH_INODE_UNLINKED) {
896                 bch2_trans_inconsistent(trans, "inode %llu link count underflow", bi->bi_inum);
897                 return;
898         }
899
900         if (bi->bi_nlink)
901                 bi->bi_nlink--;
902         else
903                 bi->bi_flags |= BCH_INODE_UNLINKED;
904 }
905
906 struct bch_opts bch2_inode_opts_to_opts(struct bch_inode_unpacked *inode)
907 {
908         struct bch_opts ret = { 0 };
909 #define x(_name, _bits)                                                 \
910         if (inode->bi_##_name)                                          \
911                 opt_set(ret, _name, inode->bi_##_name - 1);
912         BCH_INODE_OPTS()
913 #undef x
914         return ret;
915 }
916
917 void bch2_inode_opts_get(struct bch_io_opts *opts, struct bch_fs *c,
918                          struct bch_inode_unpacked *inode)
919 {
920 #define x(_name, _bits)         opts->_name = inode_opt_get(c, inode, _name);
921         BCH_INODE_OPTS()
922 #undef x
923
924         if (opts->nocow)
925                 opts->compression = opts->background_compression = opts->data_checksum = opts->erasure_code = 0;
926 }