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