]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/inode.c
938c7b43d0b468dab3c1fc1a8eb7804d6f1d1599
[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         switch (k.k->type) {
273         case KEY_TYPE_inode: {
274                 struct bkey_s_c_inode inode = bkey_s_c_to_inode(k);
275
276                 unpacked->bi_inum       = inode.k->p.offset;
277                 unpacked->bi_journal_seq= 0;
278                 unpacked->bi_hash_seed  = inode.v->bi_hash_seed;
279                 unpacked->bi_flags      = le32_to_cpu(inode.v->bi_flags);
280                 unpacked->bi_mode       = le16_to_cpu(inode.v->bi_mode);
281
282                 if (INODE_NEW_VARINT(inode.v)) {
283                         return bch2_inode_unpack_v2(unpacked, inode.v->fields,
284                                                     bkey_val_end(inode),
285                                                     INODE_NR_FIELDS(inode.v));
286                 } else {
287                         return bch2_inode_unpack_v1(inode, unpacked);
288                 }
289                 break;
290         }
291         case KEY_TYPE_inode_v2: {
292                 struct bkey_s_c_inode_v2 inode = bkey_s_c_to_inode_v2(k);
293
294                 unpacked->bi_inum       = inode.k->p.offset;
295                 unpacked->bi_journal_seq= le64_to_cpu(inode.v->bi_journal_seq);
296                 unpacked->bi_hash_seed  = inode.v->bi_hash_seed;
297                 unpacked->bi_flags      = le64_to_cpu(inode.v->bi_flags);
298                 unpacked->bi_mode       = le16_to_cpu(inode.v->bi_mode);
299
300                 return bch2_inode_unpack_v2(unpacked, inode.v->fields,
301                                             bkey_val_end(inode),
302                                             INODEv2_NR_FIELDS(inode.v));
303         }
304         default:
305                 BUG();
306         }
307 }
308
309 int bch2_inode_unpack(struct bkey_s_c k,
310                       struct bch_inode_unpacked *unpacked)
311 {
312         if (likely(k.k->type == KEY_TYPE_inode_v3))
313                 return bch2_inode_unpack_v3(k, unpacked);
314         return bch2_inode_unpack_slowpath(k, unpacked);
315 }
316
317 int bch2_inode_peek(struct btree_trans *trans,
318                     struct btree_iter *iter,
319                     struct bch_inode_unpacked *inode,
320                     subvol_inum inum, unsigned flags)
321 {
322         struct bkey_s_c k;
323         u32 snapshot;
324         int ret;
325
326         ret = bch2_subvolume_get_snapshot(trans, inum.subvol, &snapshot);
327         if (ret)
328                 return ret;
329
330         bch2_trans_iter_init(trans, iter, BTREE_ID_inodes,
331                              SPOS(0, inum.inum, snapshot),
332                              flags|BTREE_ITER_CACHED);
333         k = bch2_btree_iter_peek_slot(iter);
334         ret = bkey_err(k);
335         if (ret)
336                 goto err;
337
338         ret = bkey_is_inode(k.k) ? 0 : -ENOENT;
339         if (ret)
340                 goto err;
341
342         ret = bch2_inode_unpack(k, inode);
343         if (ret)
344                 goto err;
345
346         return 0;
347 err:
348         bch2_trans_iter_exit(trans, iter);
349         return ret;
350 }
351
352 int bch2_inode_write(struct btree_trans *trans,
353                      struct btree_iter *iter,
354                      struct bch_inode_unpacked *inode)
355 {
356         struct bkey_inode_buf *inode_p;
357
358         inode_p = bch2_trans_kmalloc(trans, sizeof(*inode_p));
359         if (IS_ERR(inode_p))
360                 return PTR_ERR(inode_p);
361
362         bch2_inode_pack_inlined(inode_p, inode);
363         inode_p->inode.k.p.snapshot = iter->snapshot;
364         return bch2_trans_update(trans, iter, &inode_p->inode.k_i, 0);
365 }
366
367 struct bkey_i *bch2_inode_to_v3(struct btree_trans *trans, struct bkey_i *k)
368 {
369         struct bch_inode_unpacked u;
370         struct bkey_inode_buf *inode_p;
371         int ret;
372
373         if (!bkey_is_inode(&k->k))
374                 return ERR_PTR(-ENOENT);
375
376         inode_p = bch2_trans_kmalloc(trans, sizeof(*inode_p));
377         if (IS_ERR(inode_p))
378                 return ERR_CAST(inode_p);
379
380         ret = bch2_inode_unpack(bkey_i_to_s_c(k), &u);
381         if (ret)
382                 return ERR_PTR(ret);
383
384         bch2_inode_pack(inode_p, &u);
385         return &inode_p->inode.k_i;
386 }
387
388 static int __bch2_inode_invalid(struct bkey_s_c k, struct printbuf *err)
389 {
390         struct bch_inode_unpacked unpacked;
391
392         if (k.k->p.inode) {
393                 prt_printf(err, "nonzero k.p.inode");
394                 return -BCH_ERR_invalid_bkey;
395         }
396
397         if (k.k->p.offset < BLOCKDEV_INODE_MAX) {
398                 prt_printf(err, "fs inode in blockdev range");
399                 return -BCH_ERR_invalid_bkey;
400         }
401
402         if (bch2_inode_unpack(k, &unpacked)) {
403                 prt_printf(err, "invalid variable length fields");
404                 return -BCH_ERR_invalid_bkey;
405         }
406
407         if (unpacked.bi_data_checksum >= BCH_CSUM_OPT_NR + 1) {
408                 prt_printf(err, "invalid data checksum type (%u >= %u",
409                         unpacked.bi_data_checksum, BCH_CSUM_OPT_NR + 1);
410                 return -BCH_ERR_invalid_bkey;
411         }
412
413         if (unpacked.bi_compression >= BCH_COMPRESSION_OPT_NR + 1) {
414                 prt_printf(err, "invalid data checksum type (%u >= %u)",
415                        unpacked.bi_compression, BCH_COMPRESSION_OPT_NR + 1);
416                 return -BCH_ERR_invalid_bkey;
417         }
418
419         if ((unpacked.bi_flags & BCH_INODE_UNLINKED) &&
420             unpacked.bi_nlink != 0) {
421                 prt_printf(err, "flagged as unlinked but bi_nlink != 0");
422                 return -BCH_ERR_invalid_bkey;
423         }
424
425         if (unpacked.bi_subvol && !S_ISDIR(unpacked.bi_mode)) {
426                 prt_printf(err, "subvolume root but not a directory");
427                 return -BCH_ERR_invalid_bkey;
428         }
429
430         return 0;
431 }
432
433 int bch2_inode_invalid(const struct bch_fs *c, struct bkey_s_c k,
434                        int rw, struct printbuf *err)
435 {
436         struct bkey_s_c_inode inode = bkey_s_c_to_inode(k);
437
438         if (bkey_val_bytes(k.k) < sizeof(*inode.v)) {
439                 prt_printf(err, "incorrect value size (%zu < %zu)",
440                        bkey_val_bytes(k.k), sizeof(*inode.v));
441                 return -BCH_ERR_invalid_bkey;
442         }
443
444         if (INODE_STR_HASH(inode.v) >= BCH_STR_HASH_NR) {
445                 prt_printf(err, "invalid str hash type (%llu >= %u)",
446                        INODE_STR_HASH(inode.v), BCH_STR_HASH_NR);
447                 return -BCH_ERR_invalid_bkey;
448         }
449
450         return __bch2_inode_invalid(k, err);
451 }
452
453 int bch2_inode_v2_invalid(const struct bch_fs *c, struct bkey_s_c k,
454                           int rw, struct printbuf *err)
455 {
456         struct bkey_s_c_inode_v2 inode = bkey_s_c_to_inode_v2(k);
457
458         if (bkey_val_bytes(k.k) < sizeof(*inode.v)) {
459                 prt_printf(err, "incorrect value size (%zu < %zu)",
460                        bkey_val_bytes(k.k), sizeof(*inode.v));
461                 return -BCH_ERR_invalid_bkey;
462         }
463
464         if (INODEv2_STR_HASH(inode.v) >= BCH_STR_HASH_NR) {
465                 prt_printf(err, "invalid str hash type (%llu >= %u)",
466                        INODEv2_STR_HASH(inode.v), BCH_STR_HASH_NR);
467                 return -BCH_ERR_invalid_bkey;
468         }
469
470         return __bch2_inode_invalid(k, err);
471 }
472
473 int bch2_inode_v3_invalid(const struct bch_fs *c, struct bkey_s_c k,
474                           int rw, struct printbuf *err)
475 {
476         struct bkey_s_c_inode_v3 inode = bkey_s_c_to_inode_v3(k);
477
478         if (bkey_val_bytes(k.k) < sizeof(*inode.v)) {
479                 prt_printf(err, "incorrect value size (%zu < %zu)",
480                        bkey_val_bytes(k.k), sizeof(*inode.v));
481                 return -BCH_ERR_invalid_bkey;
482         }
483
484         if (INODEv3_FIELDS_START(inode.v) < INODEv3_FIELDS_START_INITIAL ||
485             INODEv3_FIELDS_START(inode.v) > bkey_val_u64s(inode.k)) {
486                 prt_printf(err, "invalid fields_start (got %llu, min %u max %zu)",
487                        INODEv3_FIELDS_START(inode.v),
488                        INODEv3_FIELDS_START_INITIAL,
489                        bkey_val_u64s(inode.k));
490                 return -BCH_ERR_invalid_bkey;
491         }
492
493         if (INODEv3_STR_HASH(inode.v) >= BCH_STR_HASH_NR) {
494                 prt_printf(err, "invalid str hash type (%llu >= %u)",
495                        INODEv3_STR_HASH(inode.v), BCH_STR_HASH_NR);
496                 return -BCH_ERR_invalid_bkey;
497         }
498
499         return __bch2_inode_invalid(k, err);
500 }
501
502 static void __bch2_inode_unpacked_to_text(struct printbuf *out,
503                                           struct bch_inode_unpacked *inode)
504 {
505         prt_printf(out, "mode %o flags %x journal_seq %llu bi_size %llu bi_sectors %llu bi_version %llu",
506                inode->bi_mode, inode->bi_flags,
507                inode->bi_journal_seq,
508                inode->bi_size,
509                inode->bi_sectors,
510                inode->bi_version);
511
512 #define x(_name, _bits)                                         \
513         prt_printf(out, " "#_name " %llu", (u64) inode->_name);
514         BCH_INODE_FIELDS_v3()
515 #undef  x
516 }
517
518 void bch2_inode_unpacked_to_text(struct printbuf *out, struct bch_inode_unpacked *inode)
519 {
520         prt_printf(out, "inum: %llu ", inode->bi_inum);
521         __bch2_inode_unpacked_to_text(out, inode);
522 }
523
524 void bch2_inode_to_text(struct printbuf *out, struct bch_fs *c, struct bkey_s_c k)
525 {
526         struct bch_inode_unpacked inode;
527
528         if (bch2_inode_unpack(k, &inode)) {
529                 prt_printf(out, "(unpack error)");
530                 return;
531         }
532
533         __bch2_inode_unpacked_to_text(out, &inode);
534 }
535
536 int bch2_inode_generation_invalid(const struct bch_fs *c, struct bkey_s_c k,
537                                   int rw, struct printbuf *err)
538 {
539         if (k.k->p.inode) {
540                 prt_printf(err, "nonzero k.p.inode");
541                 return -BCH_ERR_invalid_bkey;
542         }
543
544         if (bkey_val_bytes(k.k) != sizeof(struct bch_inode_generation)) {
545                 prt_printf(err, "incorrect value size (%zu != %zu)",
546                        bkey_val_bytes(k.k), sizeof(struct bch_inode_generation));
547                 return -BCH_ERR_invalid_bkey;
548         }
549
550         return 0;
551 }
552
553 void bch2_inode_generation_to_text(struct printbuf *out, struct bch_fs *c,
554                                    struct bkey_s_c k)
555 {
556         struct bkey_s_c_inode_generation gen = bkey_s_c_to_inode_generation(k);
557
558         prt_printf(out, "generation: %u", le32_to_cpu(gen.v->bi_generation));
559 }
560
561 void bch2_inode_init_early(struct bch_fs *c,
562                            struct bch_inode_unpacked *inode_u)
563 {
564         enum bch_str_hash_type str_hash =
565                 bch2_str_hash_opt_to_type(c, c->opts.str_hash);
566
567         memset(inode_u, 0, sizeof(*inode_u));
568
569         /* ick */
570         inode_u->bi_flags |= str_hash << INODE_STR_HASH_OFFSET;
571         get_random_bytes(&inode_u->bi_hash_seed,
572                          sizeof(inode_u->bi_hash_seed));
573 }
574
575 void bch2_inode_init_late(struct bch_inode_unpacked *inode_u, u64 now,
576                           uid_t uid, gid_t gid, umode_t mode, dev_t rdev,
577                           struct bch_inode_unpacked *parent)
578 {
579         inode_u->bi_mode        = mode;
580         inode_u->bi_uid         = uid;
581         inode_u->bi_gid         = gid;
582         inode_u->bi_dev         = rdev;
583         inode_u->bi_atime       = now;
584         inode_u->bi_mtime       = now;
585         inode_u->bi_ctime       = now;
586         inode_u->bi_otime       = now;
587
588         if (parent && parent->bi_mode & S_ISGID) {
589                 inode_u->bi_gid = parent->bi_gid;
590                 if (S_ISDIR(mode))
591                         inode_u->bi_mode |= S_ISGID;
592         }
593
594         if (parent) {
595 #define x(_name, ...)   inode_u->bi_##_name = parent->bi_##_name;
596                 BCH_INODE_OPTS()
597 #undef x
598         }
599 }
600
601 void bch2_inode_init(struct bch_fs *c, struct bch_inode_unpacked *inode_u,
602                      uid_t uid, gid_t gid, umode_t mode, dev_t rdev,
603                      struct bch_inode_unpacked *parent)
604 {
605         bch2_inode_init_early(c, inode_u);
606         bch2_inode_init_late(inode_u, bch2_current_time(c),
607                              uid, gid, mode, rdev, parent);
608 }
609
610 static inline u32 bkey_generation(struct bkey_s_c k)
611 {
612         switch (k.k->type) {
613         case KEY_TYPE_inode:
614         case KEY_TYPE_inode_v2:
615                 BUG();
616         case KEY_TYPE_inode_generation:
617                 return le32_to_cpu(bkey_s_c_to_inode_generation(k).v->bi_generation);
618         default:
619                 return 0;
620         }
621 }
622
623 /*
624  * This just finds an empty slot:
625  */
626 int bch2_inode_create(struct btree_trans *trans,
627                       struct btree_iter *iter,
628                       struct bch_inode_unpacked *inode_u,
629                       u32 snapshot, u64 cpu)
630 {
631         struct bch_fs *c = trans->c;
632         struct bkey_s_c k;
633         u64 min, max, start, pos, *hint;
634         int ret = 0;
635         unsigned bits = (c->opts.inodes_32bit ? 31 : 63);
636
637         if (c->opts.shard_inode_numbers) {
638                 bits -= c->inode_shard_bits;
639
640                 min = (cpu << bits);
641                 max = (cpu << bits) | ~(ULLONG_MAX << bits);
642
643                 min = max_t(u64, min, BLOCKDEV_INODE_MAX);
644                 hint = c->unused_inode_hints + cpu;
645         } else {
646                 min = BLOCKDEV_INODE_MAX;
647                 max = ~(ULLONG_MAX << bits);
648                 hint = c->unused_inode_hints;
649         }
650
651         start = READ_ONCE(*hint);
652
653         if (start >= max || start < min)
654                 start = min;
655
656         pos = start;
657         bch2_trans_iter_init(trans, iter, BTREE_ID_inodes, POS(0, pos),
658                              BTREE_ITER_ALL_SNAPSHOTS|
659                              BTREE_ITER_INTENT);
660 again:
661         while ((k = bch2_btree_iter_peek(iter)).k &&
662                !(ret = bkey_err(k)) &&
663                bkey_lt(k.k->p, POS(0, max))) {
664                 while (pos < iter->pos.offset) {
665                         if (!bch2_btree_key_cache_find(c, BTREE_ID_inodes, POS(0, pos)))
666                                 goto found_slot;
667
668                         pos++;
669                 }
670
671                 if (k.k->p.snapshot == snapshot &&
672                     !bkey_is_inode(k.k) &&
673                     !bch2_btree_key_cache_find(c, BTREE_ID_inodes, SPOS(0, pos, snapshot))) {
674                         bch2_btree_iter_advance(iter);
675                         continue;
676                 }
677
678                 /*
679                  * We don't need to iterate over keys in every snapshot once
680                  * we've found just one:
681                  */
682                 pos = iter->pos.offset + 1;
683                 bch2_btree_iter_set_pos(iter, POS(0, pos));
684         }
685
686         while (!ret && pos < max) {
687                 if (!bch2_btree_key_cache_find(c, BTREE_ID_inodes, POS(0, pos)))
688                         goto found_slot;
689
690                 pos++;
691         }
692
693         if (!ret && start == min)
694                 ret = -BCH_ERR_ENOSPC_inode_create;
695
696         if (ret) {
697                 bch2_trans_iter_exit(trans, iter);
698                 return ret;
699         }
700
701         /* Retry from start */
702         pos = start = min;
703         bch2_btree_iter_set_pos(iter, POS(0, pos));
704         goto again;
705 found_slot:
706         bch2_btree_iter_set_pos(iter, SPOS(0, pos, snapshot));
707         k = bch2_btree_iter_peek_slot(iter);
708         ret = bkey_err(k);
709         if (ret) {
710                 bch2_trans_iter_exit(trans, iter);
711                 return ret;
712         }
713
714         /* We may have raced while the iterator wasn't pointing at pos: */
715         if (bkey_is_inode(k.k) ||
716             bch2_btree_key_cache_find(c, BTREE_ID_inodes, k.k->p))
717                 goto again;
718
719         *hint                   = k.k->p.offset;
720         inode_u->bi_inum        = k.k->p.offset;
721         inode_u->bi_generation  = bkey_generation(k);
722         return 0;
723 }
724
725 static int bch2_inode_delete_keys(struct btree_trans *trans,
726                                   subvol_inum inum, enum btree_id id)
727 {
728         struct btree_iter iter;
729         struct bkey_s_c k;
730         struct bkey_i delete;
731         u32 snapshot;
732         int ret = 0;
733
734         /*
735          * We're never going to be deleting extents, no need to use an extent
736          * iterator:
737          */
738         bch2_trans_iter_init(trans, &iter, id, POS(inum.inum, 0),
739                              BTREE_ITER_INTENT);
740
741         while (1) {
742                 bch2_trans_begin(trans);
743
744                 ret = bch2_subvolume_get_snapshot(trans, inum.subvol, &snapshot);
745                 if (ret)
746                         goto err;
747
748                 bch2_btree_iter_set_snapshot(&iter, snapshot);
749
750                 k = bch2_btree_iter_peek_upto(&iter, POS(inum.inum, U64_MAX));
751                 ret = bkey_err(k);
752                 if (ret)
753                         goto err;
754
755                 if (!k.k)
756                         break;
757
758                 bkey_init(&delete.k);
759                 delete.k.p = iter.pos;
760
761                 if (iter.flags & BTREE_ITER_IS_EXTENTS) {
762                         bch2_key_resize(&delete.k, k.k->p.offset - iter.pos.offset);
763
764                         ret = bch2_extent_trim_atomic(trans, &iter, &delete);
765                         if (ret)
766                                 goto err;
767                 }
768
769                 ret = bch2_trans_update(trans, &iter, &delete, 0) ?:
770                       bch2_trans_commit(trans, NULL, NULL,
771                                         BTREE_INSERT_NOFAIL);
772 err:
773                 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
774                         break;
775         }
776
777         bch2_trans_iter_exit(trans, &iter);
778         return ret;
779 }
780
781 int bch2_inode_rm(struct bch_fs *c, subvol_inum inum)
782 {
783         struct btree_trans trans;
784         struct btree_iter iter = { NULL };
785         struct bkey_i_inode_generation delete;
786         struct bch_inode_unpacked inode_u;
787         struct bkey_s_c k;
788         u32 snapshot;
789         int ret;
790
791         bch2_trans_init(&trans, c, 0, 1024);
792
793         /*
794          * If this was a directory, there shouldn't be any real dirents left -
795          * but there could be whiteouts (from hash collisions) that we should
796          * delete:
797          *
798          * XXX: the dirent could ideally would delete whiteouts when they're no
799          * longer needed
800          */
801         ret   = bch2_inode_delete_keys(&trans, inum, BTREE_ID_extents) ?:
802                 bch2_inode_delete_keys(&trans, inum, BTREE_ID_xattrs) ?:
803                 bch2_inode_delete_keys(&trans, inum, BTREE_ID_dirents);
804         if (ret)
805                 goto err;
806 retry:
807         bch2_trans_begin(&trans);
808
809         ret = bch2_subvolume_get_snapshot(&trans, inum.subvol, &snapshot);
810         if (ret)
811                 goto err;
812
813         bch2_trans_iter_init(&trans, &iter, BTREE_ID_inodes,
814                              SPOS(0, inum.inum, snapshot),
815                              BTREE_ITER_INTENT|BTREE_ITER_CACHED);
816         k = bch2_btree_iter_peek_slot(&iter);
817
818         ret = bkey_err(k);
819         if (ret)
820                 goto err;
821
822         if (!bkey_is_inode(k.k)) {
823                 bch2_fs_inconsistent(trans.c,
824                                      "inode %llu not found when deleting",
825                                      inum.inum);
826                 ret = -EIO;
827                 goto err;
828         }
829
830         bch2_inode_unpack(k, &inode_u);
831
832         /* Subvolume root? */
833         BUG_ON(inode_u.bi_subvol);
834
835         bkey_inode_generation_init(&delete.k_i);
836         delete.k.p = iter.pos;
837         delete.v.bi_generation = cpu_to_le32(inode_u.bi_generation + 1);
838
839         ret   = bch2_trans_update(&trans, &iter, &delete.k_i, 0) ?:
840                 bch2_trans_commit(&trans, NULL, NULL,
841                                 BTREE_INSERT_NOFAIL);
842 err:
843         bch2_trans_iter_exit(&trans, &iter);
844         if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
845                 goto retry;
846
847         bch2_trans_exit(&trans);
848         return ret;
849 }
850
851 int bch2_inode_find_by_inum_trans(struct btree_trans *trans,
852                                   subvol_inum inum,
853                                   struct bch_inode_unpacked *inode)
854 {
855         struct btree_iter iter;
856         int ret;
857
858         ret = bch2_inode_peek(trans, &iter, inode, inum, 0);
859         if (!ret)
860                 bch2_trans_iter_exit(trans, &iter);
861         return ret;
862 }
863
864 int bch2_inode_find_by_inum(struct bch_fs *c, subvol_inum inum,
865                             struct bch_inode_unpacked *inode)
866 {
867         return bch2_trans_do(c, NULL, NULL, 0,
868                 bch2_inode_find_by_inum_trans(&trans, inum, inode));
869 }
870
871 int bch2_inode_nlink_inc(struct bch_inode_unpacked *bi)
872 {
873         if (bi->bi_flags & BCH_INODE_UNLINKED)
874                 bi->bi_flags &= ~BCH_INODE_UNLINKED;
875         else {
876                 if (bi->bi_nlink == U32_MAX)
877                         return -EINVAL;
878
879                 bi->bi_nlink++;
880         }
881
882         return 0;
883 }
884
885 void bch2_inode_nlink_dec(struct btree_trans *trans, struct bch_inode_unpacked *bi)
886 {
887         if (bi->bi_nlink && (bi->bi_flags & BCH_INODE_UNLINKED)) {
888                 bch2_trans_inconsistent(trans, "inode %llu unlinked but link count nonzero",
889                                         bi->bi_inum);
890                 return;
891         }
892
893         if (bi->bi_flags & BCH_INODE_UNLINKED) {
894                 bch2_trans_inconsistent(trans, "inode %llu link count underflow", bi->bi_inum);
895                 return;
896         }
897
898         if (bi->bi_nlink)
899                 bi->bi_nlink--;
900         else
901                 bi->bi_flags |= BCH_INODE_UNLINKED;
902 }
903
904 struct bch_opts bch2_inode_opts_to_opts(struct bch_inode_unpacked *inode)
905 {
906         struct bch_opts ret = { 0 };
907 #define x(_name, _bits)                                                 \
908         if (inode->bi_##_name)                                          \
909                 opt_set(ret, _name, inode->bi_##_name - 1);
910         BCH_INODE_OPTS()
911 #undef x
912         return ret;
913 }
914
915 void bch2_inode_opts_get(struct bch_io_opts *opts, struct bch_fs *c,
916                          struct bch_inode_unpacked *inode)
917 {
918 #define x(_name, _bits)         opts->_name = inode_opt_get(c, inode, _name);
919         BCH_INODE_OPTS()
920 #undef x
921
922         if (opts->nocow)
923                 opts->compression = opts->background_compression = opts->data_checksum = opts->erasure_code = 0;
924 }