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