]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/subvolume.c
rust: Fix ptr casting in Fs::open()
[bcachefs-tools-debian] / libbcachefs / subvolume.c
1 // SPDX-License-Identifier: GPL-2.0
2
3 #include "bcachefs.h"
4 #include "btree_key_cache.h"
5 #include "btree_update.h"
6 #include "errcode.h"
7 #include "error.h"
8 #include "fs.h"
9 #include "subvolume.h"
10
11 /* Snapshot tree: */
12
13 void bch2_snapshot_to_text(struct printbuf *out, struct bch_fs *c,
14                            struct bkey_s_c k)
15 {
16         struct bkey_s_c_snapshot s = bkey_s_c_to_snapshot(k);
17
18         prt_printf(out, "is_subvol %llu deleted %llu parent %10u children %10u %10u subvol %u",
19                BCH_SNAPSHOT_SUBVOL(s.v),
20                BCH_SNAPSHOT_DELETED(s.v),
21                le32_to_cpu(s.v->parent),
22                le32_to_cpu(s.v->children[0]),
23                le32_to_cpu(s.v->children[1]),
24                le32_to_cpu(s.v->subvol));
25 }
26
27 int bch2_snapshot_invalid(const struct bch_fs *c, struct bkey_s_c k,
28                           unsigned flags, struct printbuf *err)
29 {
30         struct bkey_s_c_snapshot s;
31         u32 i, id;
32
33         if (bkey_gt(k.k->p, POS(0, U32_MAX)) ||
34             bkey_lt(k.k->p, POS(0, 1))) {
35                 prt_printf(err, "bad pos");
36                 return -BCH_ERR_invalid_bkey;
37         }
38
39         if (bkey_val_bytes(k.k) != sizeof(struct bch_snapshot)) {
40                 prt_printf(err, "bad val size (%zu != %zu)",
41                        bkey_val_bytes(k.k), sizeof(struct bch_snapshot));
42                 return -BCH_ERR_invalid_bkey;
43         }
44
45         s = bkey_s_c_to_snapshot(k);
46
47         id = le32_to_cpu(s.v->parent);
48         if (id && id <= k.k->p.offset) {
49                 prt_printf(err, "bad parent node (%u <= %llu)",
50                        id, k.k->p.offset);
51                 return -BCH_ERR_invalid_bkey;
52         }
53
54         if (le32_to_cpu(s.v->children[0]) < le32_to_cpu(s.v->children[1])) {
55                 prt_printf(err, "children not normalized");
56                 return -BCH_ERR_invalid_bkey;
57         }
58
59         if (s.v->children[0] &&
60             s.v->children[0] == s.v->children[1]) {
61                 prt_printf(err, "duplicate child nodes");
62                 return -BCH_ERR_invalid_bkey;
63         }
64
65         for (i = 0; i < 2; i++) {
66                 id = le32_to_cpu(s.v->children[i]);
67
68                 if (id >= k.k->p.offset) {
69                         prt_printf(err, "bad child node (%u >= %llu)",
70                                id, k.k->p.offset);
71                         return -BCH_ERR_invalid_bkey;
72                 }
73         }
74
75         return 0;
76 }
77
78 int bch2_mark_snapshot(struct btree_trans *trans,
79                        enum btree_id btree, unsigned level,
80                        struct bkey_s_c old, struct bkey_s_c new,
81                        unsigned flags)
82 {
83         struct bch_fs *c = trans->c;
84         struct snapshot_t *t;
85
86         t = genradix_ptr_alloc(&c->snapshots,
87                                U32_MAX - new.k->p.offset,
88                                GFP_KERNEL);
89         if (!t)
90                 return -ENOMEM;
91
92         if (new.k->type == KEY_TYPE_snapshot) {
93                 struct bkey_s_c_snapshot s = bkey_s_c_to_snapshot(new);
94
95                 t->parent       = le32_to_cpu(s.v->parent);
96                 t->children[0]  = le32_to_cpu(s.v->children[0]);
97                 t->children[1]  = le32_to_cpu(s.v->children[1]);
98                 t->subvol       = BCH_SNAPSHOT_SUBVOL(s.v) ? le32_to_cpu(s.v->subvol) : 0;
99         } else {
100                 t->parent       = 0;
101                 t->children[0]  = 0;
102                 t->children[1]  = 0;
103                 t->subvol       = 0;
104         }
105
106         return 0;
107 }
108
109 static int snapshot_lookup(struct btree_trans *trans, u32 id,
110                            struct bch_snapshot *s)
111 {
112         struct btree_iter iter;
113         struct bkey_s_c k;
114         int ret;
115
116         bch2_trans_iter_init(trans, &iter, BTREE_ID_snapshots, POS(0, id),
117                              BTREE_ITER_WITH_UPDATES);
118         k = bch2_btree_iter_peek_slot(&iter);
119         ret = bkey_err(k) ?: k.k->type == KEY_TYPE_snapshot ? 0 : -ENOENT;
120
121         if (!ret)
122                 *s = *bkey_s_c_to_snapshot(k).v;
123
124         bch2_trans_iter_exit(trans, &iter);
125         return ret;
126 }
127
128 static int snapshot_live(struct btree_trans *trans, u32 id)
129 {
130         struct bch_snapshot v;
131         int ret;
132
133         if (!id)
134                 return 0;
135
136         ret = snapshot_lookup(trans, id, &v);
137         if (ret == -ENOENT)
138                 bch_err(trans->c, "snapshot node %u not found", id);
139         if (ret)
140                 return ret;
141
142         return !BCH_SNAPSHOT_DELETED(&v);
143 }
144
145 static int bch2_snapshot_set_equiv(struct btree_trans *trans, struct bkey_s_c k)
146 {
147         struct bch_fs *c = trans->c;
148         unsigned i, nr_live = 0, live_idx = 0;
149         struct bkey_s_c_snapshot snap;
150         u32 id = k.k->p.offset, child[2];
151
152         if (k.k->type != KEY_TYPE_snapshot)
153                 return 0;
154
155         snap = bkey_s_c_to_snapshot(k);
156
157         child[0] = le32_to_cpu(snap.v->children[0]);
158         child[1] = le32_to_cpu(snap.v->children[1]);
159
160         for (i = 0; i < 2; i++) {
161                 int ret = snapshot_live(trans, child[i]);
162
163                 if (ret < 0)
164                         return ret;
165
166                 if (ret)
167                         live_idx = i;
168                 nr_live += ret;
169         }
170
171         snapshot_t(c, id)->equiv = nr_live == 1
172                 ? snapshot_t(c, child[live_idx])->equiv
173                 : id;
174         return 0;
175 }
176
177 /* fsck: */
178 static int check_snapshot(struct btree_trans *trans,
179                           struct btree_iter *iter,
180                           struct bkey_s_c k)
181 {
182         struct bch_fs *c = trans->c;
183         struct bkey_s_c_snapshot s;
184         struct bch_subvolume subvol;
185         struct bch_snapshot v;
186         struct printbuf buf = PRINTBUF;
187         bool should_have_subvol;
188         u32 i, id;
189         int ret = 0;
190
191         if (k.k->type != KEY_TYPE_snapshot)
192                 return 0;
193
194         s = bkey_s_c_to_snapshot(k);
195         id = le32_to_cpu(s.v->parent);
196         if (id) {
197                 ret = snapshot_lookup(trans, id, &v);
198                 if (ret == -ENOENT)
199                         bch_err(c, "snapshot with nonexistent parent:\n  %s",
200                                 (bch2_bkey_val_to_text(&buf, c, s.s_c), buf.buf));
201                 if (ret)
202                         goto err;
203
204                 if (le32_to_cpu(v.children[0]) != s.k->p.offset &&
205                     le32_to_cpu(v.children[1]) != s.k->p.offset) {
206                         bch_err(c, "snapshot parent %u missing pointer to child %llu",
207                                 id, s.k->p.offset);
208                         ret = -EINVAL;
209                         goto err;
210                 }
211         }
212
213         for (i = 0; i < 2 && s.v->children[i]; i++) {
214                 id = le32_to_cpu(s.v->children[i]);
215
216                 ret = snapshot_lookup(trans, id, &v);
217                 if (ret == -ENOENT)
218                         bch_err(c, "snapshot node %llu has nonexistent child %u",
219                                 s.k->p.offset, id);
220                 if (ret)
221                         goto err;
222
223                 if (le32_to_cpu(v.parent) != s.k->p.offset) {
224                         bch_err(c, "snapshot child %u has wrong parent (got %u should be %llu)",
225                                 id, le32_to_cpu(v.parent), s.k->p.offset);
226                         ret = -EINVAL;
227                         goto err;
228                 }
229         }
230
231         should_have_subvol = BCH_SNAPSHOT_SUBVOL(s.v) &&
232                 !BCH_SNAPSHOT_DELETED(s.v);
233
234         if (should_have_subvol) {
235                 id = le32_to_cpu(s.v->subvol);
236                 ret = bch2_subvolume_get(trans, id, 0, false, &subvol);
237                 if (ret == -ENOENT)
238                         bch_err(c, "snapshot points to nonexistent subvolume:\n  %s",
239                                 (bch2_bkey_val_to_text(&buf, c, s.s_c), buf.buf));
240                 if (ret)
241                         goto err;
242
243                 if (BCH_SNAPSHOT_SUBVOL(s.v) != (le32_to_cpu(subvol.snapshot) == s.k->p.offset)) {
244                         bch_err(c, "snapshot node %llu has wrong BCH_SNAPSHOT_SUBVOL",
245                                 s.k->p.offset);
246                         ret = -EINVAL;
247                         goto err;
248                 }
249         } else {
250                 if (fsck_err_on(s.v->subvol, c, "snapshot should not point to subvol:\n  %s",
251                                 (bch2_bkey_val_to_text(&buf, c, s.s_c), buf.buf))) {
252                         struct bkey_i_snapshot *u = bch2_trans_kmalloc(trans, sizeof(*u));
253
254                         ret = PTR_ERR_OR_ZERO(u);
255                         if (ret)
256                                 goto err;
257
258                         bkey_reassemble(&u->k_i, s.s_c);
259                         u->v.subvol = 0;
260                         ret = bch2_trans_update(trans, iter, &u->k_i, 0);
261                         if (ret)
262                                 goto err;
263                 }
264         }
265
266         if (BCH_SNAPSHOT_DELETED(s.v))
267                 set_bit(BCH_FS_HAVE_DELETED_SNAPSHOTS, &c->flags);
268 err:
269 fsck_err:
270         printbuf_exit(&buf);
271         return ret;
272 }
273
274 int bch2_fs_check_snapshots(struct bch_fs *c)
275 {
276         struct btree_trans trans;
277         struct btree_iter iter;
278         struct bkey_s_c k;
279         int ret;
280
281         bch2_trans_init(&trans, c, 0, 0);
282
283         ret = for_each_btree_key_commit(&trans, iter,
284                         BTREE_ID_snapshots, POS_MIN,
285                         BTREE_ITER_PREFETCH, k,
286                         NULL, NULL, BTREE_INSERT_LAZY_RW|BTREE_INSERT_NOFAIL,
287                 check_snapshot(&trans, &iter, k));
288
289         if (ret)
290                 bch_err(c, "error %i checking snapshots", ret);
291
292         bch2_trans_exit(&trans);
293         return ret;
294 }
295
296 static int check_subvol(struct btree_trans *trans,
297                         struct btree_iter *iter,
298                         struct bkey_s_c k)
299 {
300         struct bkey_s_c_subvolume subvol;
301         struct bch_snapshot snapshot;
302         unsigned snapid;
303         int ret;
304
305         if (k.k->type != KEY_TYPE_subvolume)
306                 return 0;
307
308         subvol = bkey_s_c_to_subvolume(k);
309         snapid = le32_to_cpu(subvol.v->snapshot);
310         ret = snapshot_lookup(trans, snapid, &snapshot);
311
312         if (ret == -ENOENT)
313                 bch_err(trans->c, "subvolume %llu points to nonexistent snapshot %u",
314                         k.k->p.offset, snapid);
315         if (ret)
316                 return ret;
317
318         if (BCH_SUBVOLUME_UNLINKED(subvol.v)) {
319                 ret = bch2_subvolume_delete(trans, iter->pos.offset);
320                 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
321                         bch_err(trans->c, "error deleting subvolume %llu: %s",
322                                 iter->pos.offset, bch2_err_str(ret));
323                 if (ret)
324                         return ret;
325         }
326
327         return 0;
328 }
329
330 int bch2_fs_check_subvols(struct bch_fs *c)
331 {
332         struct btree_trans trans;
333         struct btree_iter iter;
334         struct bkey_s_c k;
335         int ret;
336
337         bch2_trans_init(&trans, c, 0, 0);
338
339         ret = for_each_btree_key_commit(&trans, iter,
340                         BTREE_ID_subvolumes, POS_MIN, BTREE_ITER_PREFETCH, k,
341                         NULL, NULL, BTREE_INSERT_LAZY_RW|BTREE_INSERT_NOFAIL,
342                 check_subvol(&trans, &iter, k));
343
344         bch2_trans_exit(&trans);
345
346         return ret;
347 }
348
349 void bch2_fs_snapshots_exit(struct bch_fs *c)
350 {
351         genradix_free(&c->snapshots);
352 }
353
354 int bch2_fs_snapshots_start(struct bch_fs *c)
355 {
356         struct btree_trans trans;
357         struct btree_iter iter;
358         struct bkey_s_c k;
359         int ret = 0;
360
361         bch2_trans_init(&trans, c, 0, 0);
362
363         for_each_btree_key2(&trans, iter, BTREE_ID_snapshots,
364                            POS_MIN, 0, k,
365                 bch2_mark_snapshot(&trans, BTREE_ID_snapshots, 0, bkey_s_c_null, k, 0) ?:
366                 bch2_snapshot_set_equiv(&trans, k));
367
368         bch2_trans_exit(&trans);
369
370         if (ret)
371                 bch_err(c, "error starting snapshots: %s", bch2_err_str(ret));
372         return ret;
373 }
374
375 /*
376  * Mark a snapshot as deleted, for future cleanup:
377  */
378 static int bch2_snapshot_node_set_deleted(struct btree_trans *trans, u32 id)
379 {
380         struct btree_iter iter;
381         struct bkey_i_snapshot *s;
382         int ret = 0;
383
384         bch2_trans_iter_init(trans, &iter, BTREE_ID_snapshots, POS(0, id),
385                              BTREE_ITER_INTENT);
386         s = bch2_bkey_get_mut_typed(trans, &iter, snapshot);
387         ret = PTR_ERR_OR_ZERO(s);
388         if (unlikely(ret)) {
389                 bch2_fs_inconsistent_on(ret == -ENOENT, trans->c, "missing snapshot %u", id);
390                 goto err;
391         }
392
393         /* already deleted? */
394         if (BCH_SNAPSHOT_DELETED(&s->v))
395                 goto err;
396
397         SET_BCH_SNAPSHOT_DELETED(&s->v, true);
398         SET_BCH_SNAPSHOT_SUBVOL(&s->v, false);
399         s->v.subvol = 0;
400
401         ret = bch2_trans_update(trans, &iter, &s->k_i, 0);
402         if (ret)
403                 goto err;
404 err:
405         bch2_trans_iter_exit(trans, &iter);
406         return ret;
407 }
408
409 static int bch2_snapshot_node_delete(struct btree_trans *trans, u32 id)
410 {
411         struct btree_iter iter, p_iter = (struct btree_iter) { NULL };
412         struct bkey_s_c k;
413         struct bkey_s_c_snapshot s;
414         u32 parent_id;
415         unsigned i;
416         int ret = 0;
417
418         bch2_trans_iter_init(trans, &iter, BTREE_ID_snapshots, POS(0, id),
419                              BTREE_ITER_INTENT);
420         k = bch2_btree_iter_peek_slot(&iter);
421         ret = bkey_err(k);
422         if (ret)
423                 goto err;
424
425         if (k.k->type != KEY_TYPE_snapshot) {
426                 bch2_fs_inconsistent(trans->c, "missing snapshot %u", id);
427                 ret = -ENOENT;
428                 goto err;
429         }
430
431         s = bkey_s_c_to_snapshot(k);
432
433         BUG_ON(!BCH_SNAPSHOT_DELETED(s.v));
434         parent_id = le32_to_cpu(s.v->parent);
435
436         if (parent_id) {
437                 struct bkey_i_snapshot *parent;
438
439                 bch2_trans_iter_init(trans, &p_iter, BTREE_ID_snapshots,
440                                      POS(0, parent_id),
441                                      BTREE_ITER_INTENT);
442                 parent = bch2_bkey_get_mut_typed(trans, &p_iter, snapshot);
443                 ret = PTR_ERR_OR_ZERO(parent);
444                 if (unlikely(ret)) {
445                         bch2_fs_inconsistent_on(ret == -ENOENT, trans->c, "missing snapshot %u", parent_id);
446                         goto err;
447                 }
448
449                 for (i = 0; i < 2; i++)
450                         if (le32_to_cpu(parent->v.children[i]) == id)
451                                 break;
452
453                 if (i == 2)
454                         bch_err(trans->c, "snapshot %u missing child pointer to %u",
455                                 parent_id, id);
456                 else
457                         parent->v.children[i] = 0;
458
459                 if (le32_to_cpu(parent->v.children[0]) <
460                     le32_to_cpu(parent->v.children[1]))
461                         swap(parent->v.children[0],
462                              parent->v.children[1]);
463
464                 ret = bch2_trans_update(trans, &p_iter, &parent->k_i, 0);
465                 if (ret)
466                         goto err;
467         }
468
469         ret = bch2_btree_delete_at(trans, &iter, 0);
470 err:
471         bch2_trans_iter_exit(trans, &p_iter);
472         bch2_trans_iter_exit(trans, &iter);
473         return ret;
474 }
475
476 int bch2_snapshot_node_create(struct btree_trans *trans, u32 parent,
477                               u32 *new_snapids,
478                               u32 *snapshot_subvols,
479                               unsigned nr_snapids)
480 {
481         struct btree_iter iter;
482         struct bkey_i_snapshot *n;
483         struct bkey_s_c k;
484         unsigned i;
485         int ret = 0;
486
487         bch2_trans_iter_init(trans, &iter, BTREE_ID_snapshots,
488                              POS_MIN, BTREE_ITER_INTENT);
489         k = bch2_btree_iter_peek(&iter);
490         ret = bkey_err(k);
491         if (ret)
492                 goto err;
493
494         for (i = 0; i < nr_snapids; i++) {
495                 k = bch2_btree_iter_prev_slot(&iter);
496                 ret = bkey_err(k);
497                 if (ret)
498                         goto err;
499
500                 if (!k.k || !k.k->p.offset) {
501                         ret = -BCH_ERR_ENOSPC_snapshot_create;
502                         goto err;
503                 }
504
505                 n = bch2_bkey_alloc(trans, &iter, snapshot);
506                 ret = PTR_ERR_OR_ZERO(n);
507                 if (ret)
508                         goto err;
509
510                 n->v.flags      = 0;
511                 n->v.parent     = cpu_to_le32(parent);
512                 n->v.subvol     = cpu_to_le32(snapshot_subvols[i]);
513                 n->v.pad        = 0;
514                 SET_BCH_SNAPSHOT_SUBVOL(&n->v, true);
515
516                 ret   = bch2_trans_update(trans, &iter, &n->k_i, 0);
517                 if (ret)
518                         goto err;
519
520                 new_snapids[i]  = iter.pos.offset;
521         }
522
523         if (parent) {
524                 bch2_btree_iter_set_pos(&iter, POS(0, parent));
525                 n = bch2_bkey_get_mut_typed(trans, &iter, snapshot);
526                 ret = PTR_ERR_OR_ZERO(n);
527                 if (unlikely(ret)) {
528                         if (ret == -ENOENT)
529                                 bch_err(trans->c, "snapshot %u not found", parent);
530                         goto err;
531                 }
532
533                 if (n->v.children[0] || n->v.children[1]) {
534                         bch_err(trans->c, "Trying to add child snapshot nodes to parent that already has children");
535                         ret = -EINVAL;
536                         goto err;
537                 }
538
539                 n->v.children[0] = cpu_to_le32(new_snapids[0]);
540                 n->v.children[1] = cpu_to_le32(new_snapids[1]);
541                 n->v.subvol = 0;
542                 SET_BCH_SNAPSHOT_SUBVOL(&n->v, false);
543                 ret = bch2_trans_update(trans, &iter, &n->k_i, 0);
544                 if (ret)
545                         goto err;
546         }
547 err:
548         bch2_trans_iter_exit(trans, &iter);
549         return ret;
550 }
551
552 static int snapshot_delete_key(struct btree_trans *trans,
553                                struct btree_iter *iter,
554                                struct bkey_s_c k,
555                                snapshot_id_list *deleted,
556                                snapshot_id_list *equiv_seen,
557                                struct bpos *last_pos)
558 {
559         struct bch_fs *c = trans->c;
560         u32 equiv = snapshot_t(c, k.k->p.snapshot)->equiv;
561
562         if (!bkey_eq(k.k->p, *last_pos))
563                 equiv_seen->nr = 0;
564         *last_pos = k.k->p;
565
566         if (snapshot_list_has_id(deleted, k.k->p.snapshot) ||
567             snapshot_list_has_id(equiv_seen, equiv)) {
568                 return bch2_btree_delete_at(trans, iter,
569                                             BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
570         } else {
571                 return snapshot_list_add(c, equiv_seen, equiv);
572         }
573 }
574
575 static int bch2_delete_redundant_snapshot(struct btree_trans *trans, struct btree_iter *iter,
576                                           struct bkey_s_c k)
577 {
578         struct bkey_s_c_snapshot snap;
579         u32 children[2];
580         int ret;
581
582         if (k.k->type != KEY_TYPE_snapshot)
583                 return 0;
584
585         snap = bkey_s_c_to_snapshot(k);
586         if (BCH_SNAPSHOT_DELETED(snap.v) ||
587             BCH_SNAPSHOT_SUBVOL(snap.v))
588                 return 0;
589
590         children[0] = le32_to_cpu(snap.v->children[0]);
591         children[1] = le32_to_cpu(snap.v->children[1]);
592
593         ret   = snapshot_live(trans, children[0]) ?:
594                 snapshot_live(trans, children[1]);
595         if (ret < 0)
596                 return ret;
597
598         if (!ret)
599                 return bch2_snapshot_node_set_deleted(trans, k.k->p.offset);
600         return 0;
601 }
602
603 int bch2_delete_dead_snapshots(struct bch_fs *c)
604 {
605         struct btree_trans trans;
606         struct btree_iter iter;
607         struct bkey_s_c k;
608         struct bkey_s_c_snapshot snap;
609         snapshot_id_list deleted = { 0 };
610         u32 i, id;
611         int ret = 0;
612
613         if (!test_bit(BCH_FS_HAVE_DELETED_SNAPSHOTS, &c->flags))
614                 return 0;
615
616         if (!test_bit(BCH_FS_STARTED, &c->flags)) {
617                 ret = bch2_fs_read_write_early(c);
618                 if (ret) {
619                         bch_err(c, "error deleleting dead snapshots: error going rw: %s", bch2_err_str(ret));
620                         return ret;
621                 }
622         }
623
624         bch2_trans_init(&trans, c, 0, 0);
625
626         /*
627          * For every snapshot node: If we have no live children and it's not
628          * pointed to by a subvolume, delete it:
629          */
630         ret = for_each_btree_key_commit(&trans, iter, BTREE_ID_snapshots,
631                         POS_MIN, 0, k,
632                         NULL, NULL, 0,
633                 bch2_delete_redundant_snapshot(&trans, &iter, k));
634         if (ret) {
635                 bch_err(c, "error deleting redundant snapshots: %s", bch2_err_str(ret));
636                 goto err;
637         }
638
639         for_each_btree_key2(&trans, iter, BTREE_ID_snapshots,
640                            POS_MIN, 0, k,
641                 bch2_snapshot_set_equiv(&trans, k));
642         if (ret) {
643                 bch_err(c, "error in bch2_snapshots_set_equiv: %s", bch2_err_str(ret));
644                 goto err;
645         }
646
647         for_each_btree_key(&trans, iter, BTREE_ID_snapshots,
648                            POS_MIN, 0, k, ret) {
649                 if (k.k->type != KEY_TYPE_snapshot)
650                         continue;
651
652                 snap = bkey_s_c_to_snapshot(k);
653                 if (BCH_SNAPSHOT_DELETED(snap.v)) {
654                         ret = snapshot_list_add(c, &deleted, k.k->p.offset);
655                         if (ret)
656                                 break;
657                 }
658         }
659         bch2_trans_iter_exit(&trans, &iter);
660
661         if (ret) {
662                 bch_err(c, "error walking snapshots: %s", bch2_err_str(ret));
663                 goto err;
664         }
665
666         for (id = 0; id < BTREE_ID_NR; id++) {
667                 struct bpos last_pos = POS_MIN;
668                 snapshot_id_list equiv_seen = { 0 };
669
670                 if (!btree_type_has_snapshots(id))
671                         continue;
672
673                 ret = for_each_btree_key_commit(&trans, iter,
674                                 id, POS_MIN,
675                                 BTREE_ITER_PREFETCH|BTREE_ITER_ALL_SNAPSHOTS, k,
676                                 NULL, NULL, BTREE_INSERT_NOFAIL,
677                         snapshot_delete_key(&trans, &iter, k, &deleted, &equiv_seen, &last_pos));
678
679                 darray_exit(&equiv_seen);
680
681                 if (ret) {
682                         bch_err(c, "error deleting snapshot keys: %s", bch2_err_str(ret));
683                         goto err;
684                 }
685         }
686
687         for (i = 0; i < deleted.nr; i++) {
688                 ret = commit_do(&trans, NULL, NULL, 0,
689                         bch2_snapshot_node_delete(&trans, deleted.data[i]));
690                 if (ret) {
691                         bch_err(c, "error deleting snapshot %u: %s",
692                                 deleted.data[i], bch2_err_str(ret));
693                         goto err;
694                 }
695         }
696
697         clear_bit(BCH_FS_HAVE_DELETED_SNAPSHOTS, &c->flags);
698 err:
699         darray_exit(&deleted);
700         bch2_trans_exit(&trans);
701         return ret;
702 }
703
704 static void bch2_delete_dead_snapshots_work(struct work_struct *work)
705 {
706         struct bch_fs *c = container_of(work, struct bch_fs, snapshot_delete_work);
707
708         bch2_delete_dead_snapshots(c);
709         bch2_write_ref_put(c, BCH_WRITE_REF_delete_dead_snapshots);
710 }
711
712 void bch2_delete_dead_snapshots_async(struct bch_fs *c)
713 {
714         if (bch2_write_ref_tryget(c, BCH_WRITE_REF_delete_dead_snapshots) &&
715             !queue_work(system_long_wq, &c->snapshot_delete_work))
716                 bch2_write_ref_put(c, BCH_WRITE_REF_delete_dead_snapshots);
717 }
718
719 static int bch2_delete_dead_snapshots_hook(struct btree_trans *trans,
720                                            struct btree_trans_commit_hook *h)
721 {
722         struct bch_fs *c = trans->c;
723
724         set_bit(BCH_FS_HAVE_DELETED_SNAPSHOTS, &c->flags);
725
726         if (!test_bit(BCH_FS_FSCK_DONE, &c->flags))
727                 return 0;
728
729         bch2_delete_dead_snapshots_async(c);
730         return 0;
731 }
732
733 /* Subvolumes: */
734
735 int bch2_subvolume_invalid(const struct bch_fs *c, struct bkey_s_c k,
736                            unsigned flags, struct printbuf *err)
737 {
738         if (bkey_lt(k.k->p, SUBVOL_POS_MIN) ||
739             bkey_gt(k.k->p, SUBVOL_POS_MAX)) {
740                 prt_printf(err, "invalid pos");
741                 return -BCH_ERR_invalid_bkey;
742         }
743
744         if (bkey_val_bytes(k.k) != sizeof(struct bch_subvolume)) {
745                 prt_printf(err, "incorrect value size (%zu != %zu)",
746                        bkey_val_bytes(k.k), sizeof(struct bch_subvolume));
747                 return -BCH_ERR_invalid_bkey;
748         }
749
750         return 0;
751 }
752
753 void bch2_subvolume_to_text(struct printbuf *out, struct bch_fs *c,
754                             struct bkey_s_c k)
755 {
756         struct bkey_s_c_subvolume s = bkey_s_c_to_subvolume(k);
757
758         prt_printf(out, "root %llu snapshot id %u",
759                le64_to_cpu(s.v->inode),
760                le32_to_cpu(s.v->snapshot));
761 }
762
763 static __always_inline int
764 bch2_subvolume_get_inlined(struct btree_trans *trans, unsigned subvol,
765                            bool inconsistent_if_not_found,
766                            int iter_flags,
767                            struct bch_subvolume *s)
768 {
769         struct btree_iter iter;
770         struct bkey_s_c k;
771         int ret;
772
773         bch2_trans_iter_init(trans, &iter, BTREE_ID_subvolumes, POS(0, subvol),
774                              iter_flags);
775         k = bch2_btree_iter_peek_slot(&iter);
776         ret = bkey_err(k) ?: k.k->type == KEY_TYPE_subvolume ? 0 : -ENOENT;
777
778         if (ret == -ENOENT && inconsistent_if_not_found)
779                 bch2_fs_inconsistent(trans->c, "missing subvolume %u", subvol);
780         if (!ret)
781                 *s = *bkey_s_c_to_subvolume(k).v;
782
783         bch2_trans_iter_exit(trans, &iter);
784         return ret;
785 }
786
787 int bch2_subvolume_get(struct btree_trans *trans, unsigned subvol,
788                        bool inconsistent_if_not_found,
789                        int iter_flags,
790                        struct bch_subvolume *s)
791 {
792         return bch2_subvolume_get_inlined(trans, subvol, inconsistent_if_not_found, iter_flags, s);
793 }
794
795 int bch2_snapshot_get_subvol(struct btree_trans *trans, u32 snapshot,
796                              struct bch_subvolume *subvol)
797 {
798         struct bch_snapshot snap;
799
800         return  snapshot_lookup(trans, snapshot, &snap) ?:
801                 bch2_subvolume_get(trans, le32_to_cpu(snap.subvol), true, 0, subvol);
802 }
803
804 int bch2_subvolume_get_snapshot(struct btree_trans *trans, u32 subvol,
805                                 u32 *snapid)
806 {
807         struct bch_subvolume s;
808         int ret;
809
810         ret = bch2_subvolume_get_inlined(trans, subvol, true,
811                                          BTREE_ITER_CACHED|
812                                          BTREE_ITER_WITH_UPDATES,
813                                          &s);
814         if (!ret)
815                 *snapid = le32_to_cpu(s.snapshot);
816         return ret;
817 }
818
819 /*
820  * Delete subvolume, mark snapshot ID as deleted, queue up snapshot
821  * deletion/cleanup:
822  */
823 int bch2_subvolume_delete(struct btree_trans *trans, u32 subvolid)
824 {
825         struct btree_iter iter;
826         struct bkey_s_c k;
827         struct bkey_s_c_subvolume subvol;
828         struct btree_trans_commit_hook *h;
829         u32 snapid;
830         int ret = 0;
831
832         bch2_trans_iter_init(trans, &iter, BTREE_ID_subvolumes,
833                              POS(0, subvolid),
834                              BTREE_ITER_CACHED|
835                              BTREE_ITER_INTENT);
836         k = bch2_btree_iter_peek_slot(&iter);
837         ret = bkey_err(k);
838         if (ret)
839                 goto err;
840
841         if (k.k->type != KEY_TYPE_subvolume) {
842                 bch2_fs_inconsistent(trans->c, "missing subvolume %u", subvolid);
843                 ret = -EIO;
844                 goto err;
845         }
846
847         subvol = bkey_s_c_to_subvolume(k);
848         snapid = le32_to_cpu(subvol.v->snapshot);
849
850         ret = bch2_btree_delete_at(trans, &iter, 0);
851         if (ret)
852                 goto err;
853
854         ret = bch2_snapshot_node_set_deleted(trans, snapid);
855         if (ret)
856                 goto err;
857
858         h = bch2_trans_kmalloc(trans, sizeof(*h));
859         ret = PTR_ERR_OR_ZERO(h);
860         if (ret)
861                 goto err;
862
863         h->fn = bch2_delete_dead_snapshots_hook;
864         bch2_trans_commit_hook(trans, h);
865 err:
866         bch2_trans_iter_exit(trans, &iter);
867         return ret;
868 }
869
870 void bch2_subvolume_wait_for_pagecache_and_delete(struct work_struct *work)
871 {
872         struct bch_fs *c = container_of(work, struct bch_fs,
873                                 snapshot_wait_for_pagecache_and_delete_work);
874         snapshot_id_list s;
875         u32 *id;
876         int ret = 0;
877
878         while (!ret) {
879                 mutex_lock(&c->snapshots_unlinked_lock);
880                 s = c->snapshots_unlinked;
881                 darray_init(&c->snapshots_unlinked);
882                 mutex_unlock(&c->snapshots_unlinked_lock);
883
884                 if (!s.nr)
885                         break;
886
887                 bch2_evict_subvolume_inodes(c, &s);
888
889                 for (id = s.data; id < s.data + s.nr; id++) {
890                         ret = bch2_trans_do(c, NULL, NULL, BTREE_INSERT_NOFAIL,
891                                       bch2_subvolume_delete(&trans, *id));
892                         if (ret) {
893                                 bch_err(c, "error deleting subvolume %u: %s", *id, bch2_err_str(ret));
894                                 break;
895                         }
896                 }
897
898                 darray_exit(&s);
899         }
900
901         bch2_write_ref_put(c, BCH_WRITE_REF_snapshot_delete_pagecache);
902 }
903
904 struct subvolume_unlink_hook {
905         struct btree_trans_commit_hook  h;
906         u32                             subvol;
907 };
908
909 int bch2_subvolume_wait_for_pagecache_and_delete_hook(struct btree_trans *trans,
910                                                       struct btree_trans_commit_hook *_h)
911 {
912         struct subvolume_unlink_hook *h = container_of(_h, struct subvolume_unlink_hook, h);
913         struct bch_fs *c = trans->c;
914         int ret = 0;
915
916         mutex_lock(&c->snapshots_unlinked_lock);
917         if (!snapshot_list_has_id(&c->snapshots_unlinked, h->subvol))
918                 ret = snapshot_list_add(c, &c->snapshots_unlinked, h->subvol);
919         mutex_unlock(&c->snapshots_unlinked_lock);
920
921         if (ret)
922                 return ret;
923
924         if (!bch2_write_ref_tryget(c, BCH_WRITE_REF_snapshot_delete_pagecache))
925                 return -EROFS;
926
927         if (!queue_work(system_long_wq, &c->snapshot_wait_for_pagecache_and_delete_work))
928                 bch2_write_ref_put(c, BCH_WRITE_REF_snapshot_delete_pagecache);
929         return 0;
930 }
931
932 int bch2_subvolume_unlink(struct btree_trans *trans, u32 subvolid)
933 {
934         struct btree_iter iter;
935         struct bkey_i_subvolume *n;
936         struct subvolume_unlink_hook *h;
937         int ret = 0;
938
939         bch2_trans_iter_init(trans, &iter, BTREE_ID_subvolumes,
940                              POS(0, subvolid),
941                              BTREE_ITER_CACHED|
942                              BTREE_ITER_INTENT);
943         n = bch2_bkey_get_mut_typed(trans, &iter, subvolume);
944         ret = PTR_ERR_OR_ZERO(n);
945         if (unlikely(ret)) {
946                 bch2_fs_inconsistent_on(ret == -ENOENT, trans->c, "missing subvolume %u", subvolid);
947                 goto err;
948         }
949
950         SET_BCH_SUBVOLUME_UNLINKED(&n->v, true);
951
952         ret = bch2_trans_update(trans, &iter, &n->k_i, 0);
953         if (ret)
954                 goto err;
955
956         h = bch2_trans_kmalloc(trans, sizeof(*h));
957         ret = PTR_ERR_OR_ZERO(h);
958         if (ret)
959                 goto err;
960
961         h->h.fn         = bch2_subvolume_wait_for_pagecache_and_delete_hook;
962         h->subvol       = subvolid;
963         bch2_trans_commit_hook(trans, &h->h);
964 err:
965         bch2_trans_iter_exit(trans, &iter);
966         return ret;
967 }
968
969 int bch2_subvolume_create(struct btree_trans *trans, u64 inode,
970                           u32 src_subvolid,
971                           u32 *new_subvolid,
972                           u32 *new_snapshotid,
973                           bool ro)
974 {
975         struct bch_fs *c = trans->c;
976         struct btree_iter dst_iter, src_iter = (struct btree_iter) { NULL };
977         struct bkey_i_subvolume *new_subvol = NULL;
978         struct bkey_i_subvolume *src_subvol = NULL;
979         struct bkey_s_c k;
980         u32 parent = 0, new_nodes[2], snapshot_subvols[2];
981         int ret = 0;
982
983         for_each_btree_key(trans, dst_iter, BTREE_ID_subvolumes, SUBVOL_POS_MIN,
984                            BTREE_ITER_SLOTS|BTREE_ITER_INTENT, k, ret) {
985                 if (bkey_gt(k.k->p, SUBVOL_POS_MAX))
986                         break;
987
988                 /*
989                  * bch2_subvolume_delete() doesn't flush the btree key cache -
990                  * ideally it would but that's tricky
991                  */
992                 if (bkey_deleted(k.k) &&
993                     !bch2_btree_key_cache_find(c, BTREE_ID_subvolumes, dst_iter.pos))
994                         goto found_slot;
995         }
996
997         if (!ret)
998                 ret = -BCH_ERR_ENOSPC_subvolume_create;
999         goto err;
1000 found_slot:
1001         snapshot_subvols[0] = dst_iter.pos.offset;
1002         snapshot_subvols[1] = src_subvolid;
1003
1004         if (src_subvolid) {
1005                 /* Creating a snapshot: */
1006
1007                 bch2_trans_iter_init(trans, &src_iter, BTREE_ID_subvolumes,
1008                                      POS(0, src_subvolid),
1009                                      BTREE_ITER_CACHED|
1010                                      BTREE_ITER_INTENT);
1011                 src_subvol = bch2_bkey_get_mut_typed(trans, &src_iter, subvolume);
1012                 ret = PTR_ERR_OR_ZERO(src_subvol);
1013                 if (unlikely(ret)) {
1014                         bch2_fs_inconsistent_on(ret == -ENOENT, trans->c,
1015                                                 "subvolume %u not found", src_subvolid);
1016                         goto err;
1017                 }
1018
1019                 parent = le32_to_cpu(src_subvol->v.snapshot);
1020         }
1021
1022         ret = bch2_snapshot_node_create(trans, parent, new_nodes,
1023                                         snapshot_subvols,
1024                                         src_subvolid ? 2 : 1);
1025         if (ret)
1026                 goto err;
1027
1028         if (src_subvolid) {
1029                 src_subvol->v.snapshot = cpu_to_le32(new_nodes[1]);
1030                 ret = bch2_trans_update(trans, &src_iter, &src_subvol->k_i, 0);
1031                 if (ret)
1032                         goto err;
1033         }
1034
1035         new_subvol = bch2_bkey_alloc(trans, &dst_iter, subvolume);
1036         ret = PTR_ERR_OR_ZERO(new_subvol);
1037         if (ret)
1038                 goto err;
1039
1040         new_subvol->v.flags     = 0;
1041         new_subvol->v.snapshot  = cpu_to_le32(new_nodes[0]);
1042         new_subvol->v.inode     = cpu_to_le64(inode);
1043         SET_BCH_SUBVOLUME_RO(&new_subvol->v, ro);
1044         SET_BCH_SUBVOLUME_SNAP(&new_subvol->v, src_subvolid != 0);
1045         ret = bch2_trans_update(trans, &dst_iter, &new_subvol->k_i, 0);
1046         if (ret)
1047                 goto err;
1048
1049         *new_subvolid   = new_subvol->k.p.offset;
1050         *new_snapshotid = new_nodes[0];
1051 err:
1052         bch2_trans_iter_exit(trans, &src_iter);
1053         bch2_trans_iter_exit(trans, &dst_iter);
1054         return ret;
1055 }
1056
1057 int bch2_fs_subvolumes_init(struct bch_fs *c)
1058 {
1059         INIT_WORK(&c->snapshot_delete_work, bch2_delete_dead_snapshots_work);
1060         INIT_WORK(&c->snapshot_wait_for_pagecache_and_delete_work,
1061                   bch2_subvolume_wait_for_pagecache_and_delete);
1062         mutex_init(&c->snapshots_unlinked_lock);
1063         return 0;
1064 }