]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/subvolume.c
Update bcachefs sources to bca25b802d fixup! bcachefs: Fix bch2_check_discard_freespa...
[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 static int bch2_subvolume_delete(struct btree_trans *, u32);
12
13 /* Snapshot tree: */
14
15 void bch2_snapshot_tree_to_text(struct printbuf *out, struct bch_fs *c,
16                                 struct bkey_s_c k)
17 {
18         struct bkey_s_c_snapshot_tree t = bkey_s_c_to_snapshot_tree(k);
19
20         prt_printf(out, "subvol %u root snapshot %u",
21                    le32_to_cpu(t.v->master_subvol),
22                    le32_to_cpu(t.v->root_snapshot));
23 }
24
25 int bch2_snapshot_tree_invalid(const struct bch_fs *c, struct bkey_s_c k,
26                                unsigned flags, struct printbuf *err)
27 {
28         if (bkey_gt(k.k->p, POS(0, U32_MAX)) ||
29             bkey_lt(k.k->p, POS(0, 1))) {
30                 prt_printf(err, "bad pos");
31                 return -BCH_ERR_invalid_bkey;
32         }
33
34         return 0;
35 }
36
37 int bch2_snapshot_tree_lookup(struct btree_trans *trans, u32 id,
38                               struct bch_snapshot_tree *s)
39 {
40         int ret = bch2_bkey_get_val_typed(trans, BTREE_ID_snapshot_trees, POS(0, id),
41                                           BTREE_ITER_WITH_UPDATES, snapshot_tree, s);
42
43         if (bch2_err_matches(ret, ENOENT))
44                 ret = -BCH_ERR_ENOENT_snapshot_tree;
45         return ret;
46 }
47
48 static struct bkey_i_snapshot_tree *
49 __snapshot_tree_create(struct btree_trans *trans)
50 {
51         struct btree_iter iter;
52         int ret = bch2_bkey_get_empty_slot(trans, &iter,
53                         BTREE_ID_snapshot_trees, POS(0, U32_MAX));
54         struct bkey_i_snapshot_tree *s_t;
55
56         if (ret == -BCH_ERR_ENOSPC_btree_slot)
57                 ret = -BCH_ERR_ENOSPC_snapshot_tree;
58         if (ret)
59                 return ERR_PTR(ret);
60
61         s_t = bch2_bkey_alloc(trans, &iter, 0, snapshot_tree);
62         ret = PTR_ERR_OR_ZERO(s_t);
63         bch2_trans_iter_exit(trans, &iter);
64         return ret ? ERR_PTR(ret) : s_t;
65 }
66
67 static int snapshot_tree_create(struct btree_trans *trans,
68                                 u32 root_id, u32 subvol_id, u32 *tree_id)
69 {
70         struct bkey_i_snapshot_tree *n_tree =
71                 __snapshot_tree_create(trans);
72
73         if (IS_ERR(n_tree))
74                 return PTR_ERR(n_tree);
75
76         n_tree->v.master_subvol = cpu_to_le32(subvol_id);
77         n_tree->v.root_snapshot = cpu_to_le32(root_id);
78         *tree_id = n_tree->k.p.offset;
79         return 0;
80 }
81
82 /* Snapshot nodes: */
83
84 void bch2_snapshot_to_text(struct printbuf *out, struct bch_fs *c,
85                            struct bkey_s_c k)
86 {
87         struct bkey_s_c_snapshot s = bkey_s_c_to_snapshot(k);
88
89         prt_printf(out, "is_subvol %llu deleted %llu parent %10u children %10u %10u subvol %u tree %u",
90                BCH_SNAPSHOT_SUBVOL(s.v),
91                BCH_SNAPSHOT_DELETED(s.v),
92                le32_to_cpu(s.v->parent),
93                le32_to_cpu(s.v->children[0]),
94                le32_to_cpu(s.v->children[1]),
95                le32_to_cpu(s.v->subvol),
96                le32_to_cpu(s.v->tree));
97 }
98
99 int bch2_snapshot_invalid(const struct bch_fs *c, struct bkey_s_c k,
100                           unsigned flags, struct printbuf *err)
101 {
102         struct bkey_s_c_snapshot s;
103         u32 i, id;
104
105         if (bkey_gt(k.k->p, POS(0, U32_MAX)) ||
106             bkey_lt(k.k->p, POS(0, 1))) {
107                 prt_printf(err, "bad pos");
108                 return -BCH_ERR_invalid_bkey;
109         }
110
111         s = bkey_s_c_to_snapshot(k);
112
113         id = le32_to_cpu(s.v->parent);
114         if (id && id <= k.k->p.offset) {
115                 prt_printf(err, "bad parent node (%u <= %llu)",
116                        id, k.k->p.offset);
117                 return -BCH_ERR_invalid_bkey;
118         }
119
120         if (le32_to_cpu(s.v->children[0]) < le32_to_cpu(s.v->children[1])) {
121                 prt_printf(err, "children not normalized");
122                 return -BCH_ERR_invalid_bkey;
123         }
124
125         if (s.v->children[0] &&
126             s.v->children[0] == s.v->children[1]) {
127                 prt_printf(err, "duplicate child nodes");
128                 return -BCH_ERR_invalid_bkey;
129         }
130
131         for (i = 0; i < 2; i++) {
132                 id = le32_to_cpu(s.v->children[i]);
133
134                 if (id >= k.k->p.offset) {
135                         prt_printf(err, "bad child node (%u >= %llu)",
136                                id, k.k->p.offset);
137                         return -BCH_ERR_invalid_bkey;
138                 }
139         }
140
141         return 0;
142 }
143
144 int bch2_mark_snapshot(struct btree_trans *trans,
145                        enum btree_id btree, unsigned level,
146                        struct bkey_s_c old, struct bkey_s_c new,
147                        unsigned flags)
148 {
149         struct bch_fs *c = trans->c;
150         struct snapshot_t *t;
151
152         t = genradix_ptr_alloc(&c->snapshots,
153                                U32_MAX - new.k->p.offset,
154                                GFP_KERNEL);
155         if (!t)
156                 return -BCH_ERR_ENOMEM_mark_snapshot;
157
158         if (new.k->type == KEY_TYPE_snapshot) {
159                 struct bkey_s_c_snapshot s = bkey_s_c_to_snapshot(new);
160
161                 t->parent       = le32_to_cpu(s.v->parent);
162                 t->children[0]  = le32_to_cpu(s.v->children[0]);
163                 t->children[1]  = le32_to_cpu(s.v->children[1]);
164                 t->subvol       = BCH_SNAPSHOT_SUBVOL(s.v) ? le32_to_cpu(s.v->subvol) : 0;
165                 t->tree         = le32_to_cpu(s.v->tree);
166         } else {
167                 t->parent       = 0;
168                 t->children[0]  = 0;
169                 t->children[1]  = 0;
170                 t->subvol       = 0;
171                 t->tree         = 0;
172         }
173
174         return 0;
175 }
176
177 static int snapshot_lookup(struct btree_trans *trans, u32 id,
178                            struct bch_snapshot *s)
179 {
180         return bch2_bkey_get_val_typed(trans, BTREE_ID_snapshots, POS(0, id),
181                                        BTREE_ITER_WITH_UPDATES, snapshot, s);
182 }
183
184 static int snapshot_live(struct btree_trans *trans, u32 id)
185 {
186         struct bch_snapshot v;
187         int ret;
188
189         if (!id)
190                 return 0;
191
192         ret = snapshot_lookup(trans, id, &v);
193         if (bch2_err_matches(ret, ENOENT))
194                 bch_err(trans->c, "snapshot node %u not found", id);
195         if (ret)
196                 return ret;
197
198         return !BCH_SNAPSHOT_DELETED(&v);
199 }
200
201 static int bch2_snapshot_set_equiv(struct btree_trans *trans, struct bkey_s_c k)
202 {
203         struct bch_fs *c = trans->c;
204         unsigned i, nr_live = 0, live_idx = 0;
205         struct bkey_s_c_snapshot snap;
206         u32 id = k.k->p.offset, child[2];
207
208         if (k.k->type != KEY_TYPE_snapshot)
209                 return 0;
210
211         snap = bkey_s_c_to_snapshot(k);
212
213         child[0] = le32_to_cpu(snap.v->children[0]);
214         child[1] = le32_to_cpu(snap.v->children[1]);
215
216         for (i = 0; i < 2; i++) {
217                 int ret = snapshot_live(trans, child[i]);
218
219                 if (ret < 0)
220                         return ret;
221
222                 if (ret)
223                         live_idx = i;
224                 nr_live += ret;
225         }
226
227         snapshot_t(c, id)->equiv = nr_live == 1
228                 ? snapshot_t(c, child[live_idx])->equiv
229                 : id;
230         return 0;
231 }
232
233 /* fsck: */
234
235 static u32 bch2_snapshot_child(struct bch_fs *c, u32 id, unsigned child)
236 {
237         return snapshot_t(c, id)->children[child];
238 }
239
240 static u32 bch2_snapshot_left_child(struct bch_fs *c, u32 id)
241 {
242         return bch2_snapshot_child(c, id, 0);
243 }
244
245 static u32 bch2_snapshot_right_child(struct bch_fs *c, u32 id)
246 {
247         return bch2_snapshot_child(c, id, 1);
248 }
249
250 static u32 bch2_snapshot_tree_next(struct bch_fs *c, u32 id)
251 {
252         u32 n, parent;
253
254         n = bch2_snapshot_left_child(c, id);
255         if (n)
256                 return n;
257
258         while ((parent = bch2_snapshot_parent(c, id))) {
259                 n = bch2_snapshot_right_child(c, parent);
260                 if (n && n != id)
261                         return n;
262                 id = parent;
263         }
264
265         return 0;
266 }
267
268 static u32 bch2_snapshot_tree_oldest_subvol(struct bch_fs *c, u32 snapshot_root)
269 {
270         u32 id = snapshot_root;
271         u32 subvol = 0, s;
272
273         while (id) {
274                 s = snapshot_t(c, id)->subvol;
275
276                 if (s && (!subvol || s < subvol))
277                         subvol = s;
278
279                 id = bch2_snapshot_tree_next(c, id);
280         }
281
282         return subvol;
283 }
284
285 static int bch2_snapshot_tree_master_subvol(struct btree_trans *trans,
286                                             u32 snapshot_root, u32 *subvol_id)
287 {
288         struct bch_fs *c = trans->c;
289         struct btree_iter iter;
290         struct bkey_s_c k;
291         struct bkey_s_c_subvolume s;
292         bool found = false;
293         int ret;
294
295         for_each_btree_key_norestart(trans, iter, BTREE_ID_subvolumes, POS_MIN,
296                                      0, k, ret) {
297                 if (k.k->type != KEY_TYPE_subvolume)
298                         continue;
299
300                 s = bkey_s_c_to_subvolume(k);
301                 if (!bch2_snapshot_is_ancestor(c, le32_to_cpu(s.v->snapshot), snapshot_root))
302                         continue;
303                 if (!BCH_SUBVOLUME_SNAP(s.v)) {
304                         *subvol_id = s.k->p.offset;
305                         found = true;
306                         break;
307                 }
308         }
309
310         bch2_trans_iter_exit(trans, &iter);
311
312         if (!ret && !found) {
313                 struct bkey_i_subvolume *s;
314
315                 *subvol_id = bch2_snapshot_tree_oldest_subvol(c, snapshot_root);
316
317                 s = bch2_bkey_get_mut_typed(trans, &iter,
318                                             BTREE_ID_subvolumes, POS(0, *subvol_id),
319                                             0, subvolume);
320                 ret = PTR_ERR_OR_ZERO(s);
321                 if (ret)
322                         return ret;
323
324                 SET_BCH_SUBVOLUME_SNAP(&s->v, false);
325         }
326
327         return ret;
328 }
329
330 static int check_snapshot_tree(struct btree_trans *trans,
331                                struct btree_iter *iter,
332                                struct bkey_s_c k)
333 {
334         struct bch_fs *c = trans->c;
335         struct bkey_s_c_snapshot_tree st;
336         struct bch_snapshot s;
337         struct bch_subvolume subvol;
338         struct printbuf buf = PRINTBUF;
339         u32 root_id;
340         int ret;
341
342         if (k.k->type != KEY_TYPE_snapshot_tree)
343                 return 0;
344
345         st = bkey_s_c_to_snapshot_tree(k);
346         root_id = le32_to_cpu(st.v->root_snapshot);
347
348         ret = snapshot_lookup(trans, root_id, &s);
349         if (ret && !bch2_err_matches(ret, ENOENT))
350                 goto err;
351
352         if (fsck_err_on(ret ||
353                         root_id != bch2_snapshot_root(c, root_id) ||
354                         st.k->p.offset != le32_to_cpu(s.tree),
355                         c,
356                         "snapshot tree points to missing/incorrect snapshot:\n  %s",
357                         (bch2_bkey_val_to_text(&buf, c, st.s_c), buf.buf))) {
358                 ret = bch2_btree_delete_at(trans, iter, 0);
359                 goto err;
360         }
361
362         ret = bch2_subvolume_get(trans, le32_to_cpu(st.v->master_subvol),
363                                  false, 0, &subvol);
364         if (ret && !bch2_err_matches(ret, ENOENT))
365                 goto err;
366
367         if (fsck_err_on(ret, c,
368                         "snapshot tree points to missing subvolume:\n  %s",
369                         (printbuf_reset(&buf),
370                          bch2_bkey_val_to_text(&buf, c, st.s_c), buf.buf)) ||
371             fsck_err_on(!bch2_snapshot_is_ancestor(c,
372                                                    le32_to_cpu(subvol.snapshot),
373                                                    root_id), c,
374                         "snapshot tree points to subvolume that does not point to snapshot in this tree:\n  %s",
375                         (printbuf_reset(&buf),
376                          bch2_bkey_val_to_text(&buf, c, st.s_c), buf.buf)) ||
377             fsck_err_on(BCH_SUBVOLUME_SNAP(&subvol), c,
378                         "snapshot tree points to snapshot subvolume:\n  %s",
379                         (printbuf_reset(&buf),
380                          bch2_bkey_val_to_text(&buf, c, st.s_c), buf.buf))) {
381                 struct bkey_i_snapshot_tree *u;
382                 u32 subvol_id;
383
384                 ret = bch2_snapshot_tree_master_subvol(trans, root_id, &subvol_id);
385                 if (ret)
386                         goto err;
387
388                 u = bch2_bkey_make_mut_typed(trans, iter, k, 0, snapshot_tree);
389                 ret = PTR_ERR_OR_ZERO(u);
390                 if (ret)
391                         goto err;
392
393                 u->v.master_subvol = cpu_to_le32(subvol_id);
394                 st = snapshot_tree_i_to_s_c(u);
395         }
396 err:
397 fsck_err:
398         printbuf_exit(&buf);
399         return ret;
400 }
401
402 /*
403  * For each snapshot_tree, make sure it points to the root of a snapshot tree
404  * and that snapshot entry points back to it, or delete it.
405  *
406  * And, make sure it points to a subvolume within that snapshot tree, or correct
407  * it to point to the oldest subvolume within that snapshot tree.
408  */
409 int bch2_fs_check_snapshot_trees(struct bch_fs *c)
410 {
411         struct btree_iter iter;
412         struct bkey_s_c k;
413         int ret;
414
415         ret = bch2_trans_run(c,
416                 for_each_btree_key_commit(&trans, iter,
417                         BTREE_ID_snapshot_trees, POS_MIN,
418                         BTREE_ITER_PREFETCH, k,
419                         NULL, NULL, BTREE_INSERT_LAZY_RW|BTREE_INSERT_NOFAIL,
420                 check_snapshot_tree(&trans, &iter, k)));
421
422         if (ret)
423                 bch_err(c, "error %i checking snapshot trees", ret);
424         return ret;
425 }
426
427 /*
428  * Look up snapshot tree for @tree_id and find root,
429  * make sure @snap_id is a descendent:
430  */
431 static int snapshot_tree_ptr_good(struct btree_trans *trans,
432                                   u32 snap_id, u32 tree_id)
433 {
434         struct bch_snapshot_tree s_t;
435         int ret = bch2_snapshot_tree_lookup(trans, tree_id, &s_t);
436
437         if (bch2_err_matches(ret, ENOENT))
438                 return 0;
439         if (ret)
440                 return ret;
441
442         return bch2_snapshot_is_ancestor(trans->c, snap_id, le32_to_cpu(s_t.root_snapshot));
443 }
444
445 /*
446  * snapshot_tree pointer was incorrect: look up root snapshot node, make sure
447  * its snapshot_tree pointer is correct (allocate new one if necessary), then
448  * update this node's pointer to root node's pointer:
449  */
450 static int snapshot_tree_ptr_repair(struct btree_trans *trans,
451                                     struct btree_iter *iter,
452                                     struct bkey_s_c_snapshot *s)
453 {
454         struct bch_fs *c = trans->c;
455         struct btree_iter root_iter;
456         struct bch_snapshot_tree s_t;
457         struct bkey_s_c_snapshot root;
458         struct bkey_i_snapshot *u;
459         u32 root_id = bch2_snapshot_root(c, s->k->p.offset), tree_id;
460         int ret;
461
462         root = bch2_bkey_get_iter_typed(trans, &root_iter,
463                                BTREE_ID_snapshots, POS(0, root_id),
464                                BTREE_ITER_WITH_UPDATES, snapshot);
465         ret = bkey_err(root);
466         if (ret)
467                 goto err;
468
469         tree_id = le32_to_cpu(root.v->tree);
470
471         ret = bch2_snapshot_tree_lookup(trans, tree_id, &s_t);
472         if (ret && !bch2_err_matches(ret, ENOENT))
473                 return ret;
474
475         if (ret || le32_to_cpu(s_t.root_snapshot) != root_id) {
476                 u = bch2_bkey_make_mut_typed(trans, &root_iter, root.s_c, 0, snapshot);
477                 ret =   PTR_ERR_OR_ZERO(u) ?:
478                         snapshot_tree_create(trans, root_id,
479                                 bch2_snapshot_tree_oldest_subvol(c, root_id),
480                                 &tree_id);
481                 if (ret)
482                         goto err;
483
484                 u->v.tree = cpu_to_le32(tree_id);
485                 if (s->k->p.snapshot == root_id)
486                         *s = snapshot_i_to_s_c(u);
487         }
488
489         if (s->k->p.snapshot != root_id) {
490                 u = bch2_bkey_make_mut_typed(trans, iter, s->s_c, 0, snapshot);
491                 ret = PTR_ERR_OR_ZERO(u);
492                 if (ret)
493                         goto err;
494
495                 u->v.tree = cpu_to_le32(tree_id);
496                 *s = snapshot_i_to_s_c(u);
497         }
498 err:
499         bch2_trans_iter_exit(trans, &root_iter);
500         return ret;
501 }
502
503 static int check_snapshot(struct btree_trans *trans,
504                           struct btree_iter *iter,
505                           struct bkey_s_c k)
506 {
507         struct bch_fs *c = trans->c;
508         struct bkey_s_c_snapshot s;
509         struct bch_subvolume subvol;
510         struct bch_snapshot v;
511         struct printbuf buf = PRINTBUF;
512         bool should_have_subvol;
513         u32 i, id;
514         int ret = 0;
515
516         if (k.k->type != KEY_TYPE_snapshot)
517                 return 0;
518
519         s = bkey_s_c_to_snapshot(k);
520         id = le32_to_cpu(s.v->parent);
521         if (id) {
522                 ret = snapshot_lookup(trans, id, &v);
523                 if (bch2_err_matches(ret, ENOENT))
524                         bch_err(c, "snapshot with nonexistent parent:\n  %s",
525                                 (bch2_bkey_val_to_text(&buf, c, s.s_c), buf.buf));
526                 if (ret)
527                         goto err;
528
529                 if (le32_to_cpu(v.children[0]) != s.k->p.offset &&
530                     le32_to_cpu(v.children[1]) != s.k->p.offset) {
531                         bch_err(c, "snapshot parent %u missing pointer to child %llu",
532                                 id, s.k->p.offset);
533                         ret = -EINVAL;
534                         goto err;
535                 }
536         }
537
538         for (i = 0; i < 2 && s.v->children[i]; i++) {
539                 id = le32_to_cpu(s.v->children[i]);
540
541                 ret = snapshot_lookup(trans, id, &v);
542                 if (bch2_err_matches(ret, ENOENT))
543                         bch_err(c, "snapshot node %llu has nonexistent child %u",
544                                 s.k->p.offset, id);
545                 if (ret)
546                         goto err;
547
548                 if (le32_to_cpu(v.parent) != s.k->p.offset) {
549                         bch_err(c, "snapshot child %u has wrong parent (got %u should be %llu)",
550                                 id, le32_to_cpu(v.parent), s.k->p.offset);
551                         ret = -EINVAL;
552                         goto err;
553                 }
554         }
555
556         should_have_subvol = BCH_SNAPSHOT_SUBVOL(s.v) &&
557                 !BCH_SNAPSHOT_DELETED(s.v);
558
559         if (should_have_subvol) {
560                 id = le32_to_cpu(s.v->subvol);
561                 ret = bch2_subvolume_get(trans, id, 0, false, &subvol);
562                 if (bch2_err_matches(ret, ENOENT))
563                         bch_err(c, "snapshot points to nonexistent subvolume:\n  %s",
564                                 (bch2_bkey_val_to_text(&buf, c, s.s_c), buf.buf));
565                 if (ret)
566                         goto err;
567
568                 if (BCH_SNAPSHOT_SUBVOL(s.v) != (le32_to_cpu(subvol.snapshot) == s.k->p.offset)) {
569                         bch_err(c, "snapshot node %llu has wrong BCH_SNAPSHOT_SUBVOL",
570                                 s.k->p.offset);
571                         ret = -EINVAL;
572                         goto err;
573                 }
574         } else {
575                 if (fsck_err_on(s.v->subvol, c, "snapshot should not point to subvol:\n  %s",
576                                 (bch2_bkey_val_to_text(&buf, c, s.s_c), buf.buf))) {
577                         struct bkey_i_snapshot *u = bch2_trans_kmalloc(trans, sizeof(*u));
578
579                         ret = PTR_ERR_OR_ZERO(u);
580                         if (ret)
581                                 goto err;
582
583                         bkey_reassemble(&u->k_i, s.s_c);
584                         u->v.subvol = 0;
585                         ret = bch2_trans_update(trans, iter, &u->k_i, 0);
586                         if (ret)
587                                 goto err;
588
589                         s = snapshot_i_to_s_c(u);
590                 }
591         }
592
593         ret = snapshot_tree_ptr_good(trans, s.k->p.offset, le32_to_cpu(s.v->tree));
594         if (ret < 0)
595                 goto err;
596
597         if (fsck_err_on(!ret, c, "snapshot points to missing/incorrect tree:\n  %s",
598                         (bch2_bkey_val_to_text(&buf, c, s.s_c), buf.buf))) {
599                 ret = snapshot_tree_ptr_repair(trans, iter, &s);
600                 if (ret)
601                         goto err;
602         }
603         ret = 0;
604
605         if (BCH_SNAPSHOT_DELETED(s.v))
606                 set_bit(BCH_FS_HAVE_DELETED_SNAPSHOTS, &c->flags);
607 err:
608 fsck_err:
609         printbuf_exit(&buf);
610         return ret;
611 }
612
613 int bch2_fs_check_snapshots(struct bch_fs *c)
614 {
615         struct btree_iter iter;
616         struct bkey_s_c k;
617         int ret;
618
619         ret = bch2_trans_run(c,
620                 for_each_btree_key_commit(&trans, iter,
621                         BTREE_ID_snapshots, POS_MIN,
622                         BTREE_ITER_PREFETCH, k,
623                         NULL, NULL, BTREE_INSERT_LAZY_RW|BTREE_INSERT_NOFAIL,
624                 check_snapshot(&trans, &iter, k)));
625         if (ret)
626                 bch_err(c, "%s: error %s", __func__, bch2_err_str(ret));
627         return ret;
628 }
629
630 static int check_subvol(struct btree_trans *trans,
631                         struct btree_iter *iter,
632                         struct bkey_s_c k)
633 {
634         struct bch_fs *c = trans->c;
635         struct bkey_s_c_subvolume subvol;
636         struct bch_snapshot snapshot;
637         unsigned snapid;
638         int ret = 0;
639
640         if (k.k->type != KEY_TYPE_subvolume)
641                 return 0;
642
643         subvol = bkey_s_c_to_subvolume(k);
644         snapid = le32_to_cpu(subvol.v->snapshot);
645         ret = snapshot_lookup(trans, snapid, &snapshot);
646
647         if (bch2_err_matches(ret, ENOENT))
648                 bch_err(c, "subvolume %llu points to nonexistent snapshot %u",
649                         k.k->p.offset, snapid);
650         if (ret)
651                 return ret;
652
653         if (BCH_SUBVOLUME_UNLINKED(subvol.v)) {
654                 bch2_fs_lazy_rw(c);
655
656                 ret = bch2_subvolume_delete(trans, iter->pos.offset);
657                 if (ret)
658                         bch_err(c, "error deleting subvolume %llu: %s",
659                                 iter->pos.offset, bch2_err_str(ret));
660                 return ret ?: -BCH_ERR_transaction_restart_nested;
661         }
662
663         if (!BCH_SUBVOLUME_SNAP(subvol.v)) {
664                 u32 snapshot_root = bch2_snapshot_root(c, le32_to_cpu(subvol.v->snapshot));
665                 u32 snapshot_tree = snapshot_t(c, snapshot_root)->tree;
666                 struct bch_snapshot_tree st;
667
668                 ret = bch2_snapshot_tree_lookup(trans, snapshot_tree, &st);
669
670                 bch2_fs_inconsistent_on(bch2_err_matches(ret, ENOENT), c,
671                                 "%s: snapshot tree %u not found", __func__, snapshot_tree);
672
673                 if (ret)
674                         return ret;
675
676                 if (fsck_err_on(le32_to_cpu(st.master_subvol) != subvol.k->p.offset, c,
677                                 "subvolume %llu is not set as snapshot but is not master subvolume",
678                                 k.k->p.offset)) {
679                         struct bkey_i_subvolume *s =
680                                 bch2_bkey_make_mut_typed(trans, iter, subvol.s_c, 0, subvolume);
681                         ret = PTR_ERR_OR_ZERO(s);
682                         if (ret)
683                                 return ret;
684
685                         SET_BCH_SUBVOLUME_SNAP(&s->v, true);
686                 }
687         }
688
689 fsck_err:
690         return ret;
691 }
692
693 int bch2_fs_check_subvols(struct bch_fs *c)
694 {
695         struct btree_iter iter;
696         struct bkey_s_c k;
697         int ret;
698
699         ret = bch2_trans_run(c,
700                 for_each_btree_key_commit(&trans, iter,
701                         BTREE_ID_subvolumes, POS_MIN, BTREE_ITER_PREFETCH, k,
702                         NULL, NULL, BTREE_INSERT_LAZY_RW|BTREE_INSERT_NOFAIL,
703                 check_subvol(&trans, &iter, k)));
704         if (ret)
705                 bch_err(c, "%s: error %s", __func__, bch2_err_str(ret));
706
707         return ret;
708 }
709
710 void bch2_fs_snapshots_exit(struct bch_fs *c)
711 {
712         genradix_free(&c->snapshots);
713 }
714
715 int bch2_fs_snapshots_start(struct bch_fs *c)
716 {
717         struct btree_iter iter;
718         struct bkey_s_c k;
719         int ret = 0;
720
721         ret = bch2_trans_run(c,
722                 for_each_btree_key2(&trans, iter, BTREE_ID_snapshots,
723                            POS_MIN, 0, k,
724                         bch2_mark_snapshot(&trans, BTREE_ID_snapshots, 0, bkey_s_c_null, k, 0) ?:
725                         bch2_snapshot_set_equiv(&trans, k)));
726         if (ret)
727                 bch_err(c, "error starting snapshots: %s", bch2_err_str(ret));
728         return ret;
729 }
730
731 /*
732  * Mark a snapshot as deleted, for future cleanup:
733  */
734 static int bch2_snapshot_node_set_deleted(struct btree_trans *trans, u32 id)
735 {
736         struct btree_iter iter;
737         struct bkey_i_snapshot *s;
738         int ret = 0;
739
740         s = bch2_bkey_get_mut_typed(trans, &iter,
741                                     BTREE_ID_snapshots, POS(0, id),
742                                     0, snapshot);
743         ret = PTR_ERR_OR_ZERO(s);
744         if (unlikely(ret)) {
745                 bch2_fs_inconsistent_on(bch2_err_matches(ret, ENOENT),
746                                         trans->c, "missing snapshot %u", id);
747                 return ret;
748         }
749
750         /* already deleted? */
751         if (BCH_SNAPSHOT_DELETED(&s->v))
752                 goto err;
753
754         SET_BCH_SNAPSHOT_DELETED(&s->v, true);
755         SET_BCH_SNAPSHOT_SUBVOL(&s->v, false);
756         s->v.subvol = 0;
757 err:
758         bch2_trans_iter_exit(trans, &iter);
759         return ret;
760 }
761
762 static int bch2_snapshot_node_delete(struct btree_trans *trans, u32 id)
763 {
764         struct bch_fs *c = trans->c;
765         struct btree_iter iter, p_iter = (struct btree_iter) { NULL };
766         struct btree_iter tree_iter = (struct btree_iter) { NULL };
767         struct bkey_s_c_snapshot s;
768         u32 parent_id;
769         unsigned i;
770         int ret = 0;
771
772         s = bch2_bkey_get_iter_typed(trans, &iter, BTREE_ID_snapshots, POS(0, id),
773                                      BTREE_ITER_INTENT, snapshot);
774         ret = bkey_err(s);
775         bch2_fs_inconsistent_on(bch2_err_matches(ret, ENOENT), c,
776                                 "missing snapshot %u", id);
777
778         if (ret)
779                 goto err;
780
781         BUG_ON(!BCH_SNAPSHOT_DELETED(s.v));
782         parent_id = le32_to_cpu(s.v->parent);
783
784         if (parent_id) {
785                 struct bkey_i_snapshot *parent;
786
787                 parent = bch2_bkey_get_mut_typed(trans, &p_iter,
788                                      BTREE_ID_snapshots, POS(0, parent_id),
789                                      0, snapshot);
790                 ret = PTR_ERR_OR_ZERO(parent);
791                 if (unlikely(ret)) {
792                         bch2_fs_inconsistent_on(bch2_err_matches(ret, ENOENT), c,
793                                                 "missing snapshot %u", parent_id);
794                         goto err;
795                 }
796
797                 for (i = 0; i < 2; i++)
798                         if (le32_to_cpu(parent->v.children[i]) == id)
799                                 break;
800
801                 if (i == 2)
802                         bch_err(c, "snapshot %u missing child pointer to %u",
803                                 parent_id, id);
804                 else
805                         parent->v.children[i] = 0;
806
807                 if (le32_to_cpu(parent->v.children[0]) <
808                     le32_to_cpu(parent->v.children[1]))
809                         swap(parent->v.children[0],
810                              parent->v.children[1]);
811         } else {
812                 /*
813                  * We're deleting the root of a snapshot tree: update the
814                  * snapshot_tree entry to point to the new root, or delete it if
815                  * this is the last snapshot ID in this tree:
816                  */
817                 struct bkey_i_snapshot_tree *s_t;
818
819                 BUG_ON(s.v->children[1]);
820
821                 s_t = bch2_bkey_get_mut_typed(trans, &tree_iter,
822                                 BTREE_ID_snapshot_trees, POS(0, le32_to_cpu(s.v->tree)),
823                                 0, snapshot_tree);
824                 ret = PTR_ERR_OR_ZERO(s_t);
825                 if (ret)
826                         goto err;
827
828                 if (s.v->children[0]) {
829                         s_t->v.root_snapshot = cpu_to_le32(s.v->children[0]);
830                 } else {
831                         s_t->k.type = KEY_TYPE_deleted;
832                         set_bkey_val_u64s(&s_t->k, 0);
833                 }
834         }
835
836         ret = bch2_btree_delete_at(trans, &iter, 0);
837 err:
838         bch2_trans_iter_exit(trans, &tree_iter);
839         bch2_trans_iter_exit(trans, &p_iter);
840         bch2_trans_iter_exit(trans, &iter);
841         return ret;
842 }
843
844 static int create_snapids(struct btree_trans *trans, u32 parent, u32 tree,
845                           u32 *new_snapids,
846                           u32 *snapshot_subvols,
847                           unsigned nr_snapids)
848 {
849         struct btree_iter iter;
850         struct bkey_i_snapshot *n;
851         struct bkey_s_c k;
852         unsigned i;
853         int ret;
854
855         bch2_trans_iter_init(trans, &iter, BTREE_ID_snapshots,
856                              POS_MIN, BTREE_ITER_INTENT);
857         k = bch2_btree_iter_peek(&iter);
858         ret = bkey_err(k);
859         if (ret)
860                 goto err;
861
862         for (i = 0; i < nr_snapids; i++) {
863                 k = bch2_btree_iter_prev_slot(&iter);
864                 ret = bkey_err(k);
865                 if (ret)
866                         goto err;
867
868                 if (!k.k || !k.k->p.offset) {
869                         ret = -BCH_ERR_ENOSPC_snapshot_create;
870                         goto err;
871                 }
872
873                 n = bch2_bkey_alloc(trans, &iter, 0, snapshot);
874                 ret = PTR_ERR_OR_ZERO(n);
875                 if (ret)
876                         goto err;
877
878                 n->v.flags      = 0;
879                 n->v.parent     = cpu_to_le32(parent);
880                 n->v.subvol     = cpu_to_le32(snapshot_subvols[i]);
881                 n->v.tree       = cpu_to_le32(tree);
882                 SET_BCH_SNAPSHOT_SUBVOL(&n->v, true);
883
884                 ret = bch2_mark_snapshot(trans, BTREE_ID_snapshots, 0,
885                                          bkey_s_c_null, bkey_i_to_s_c(&n->k_i), 0);
886                 if (ret)
887                         goto err;
888
889                 new_snapids[i]  = iter.pos.offset;
890         }
891 err:
892         bch2_trans_iter_exit(trans, &iter);
893         return ret;
894 }
895
896 /*
897  * Create new snapshot IDs as children of an existing snapshot ID:
898  */
899 static int bch2_snapshot_node_create_children(struct btree_trans *trans, u32 parent,
900                               u32 *new_snapids,
901                               u32 *snapshot_subvols,
902                               unsigned nr_snapids)
903 {
904         struct btree_iter iter;
905         struct bkey_i_snapshot *n_parent;
906         int ret = 0;
907
908         n_parent = bch2_bkey_get_mut_typed(trans, &iter,
909                         BTREE_ID_snapshots, POS(0, parent),
910                         0, snapshot);
911         ret = PTR_ERR_OR_ZERO(n_parent);
912         if (unlikely(ret)) {
913                 if (bch2_err_matches(ret, ENOENT))
914                         bch_err(trans->c, "snapshot %u not found", parent);
915                 return ret;
916         }
917
918         if (n_parent->v.children[0] || n_parent->v.children[1]) {
919                 bch_err(trans->c, "Trying to add child snapshot nodes to parent that already has children");
920                 ret = -EINVAL;
921                 goto err;
922         }
923
924         ret = create_snapids(trans, parent, le32_to_cpu(n_parent->v.tree),
925                              new_snapids, snapshot_subvols, nr_snapids);
926         if (ret)
927                 goto err;
928
929         n_parent->v.children[0] = cpu_to_le32(new_snapids[0]);
930         n_parent->v.children[1] = cpu_to_le32(new_snapids[1]);
931         n_parent->v.subvol = 0;
932         SET_BCH_SNAPSHOT_SUBVOL(&n_parent->v, false);
933 err:
934         bch2_trans_iter_exit(trans, &iter);
935         return ret;
936 }
937
938 /*
939  * Create a snapshot node that is the root of a new tree:
940  */
941 static int bch2_snapshot_node_create_tree(struct btree_trans *trans,
942                               u32 *new_snapids,
943                               u32 *snapshot_subvols,
944                               unsigned nr_snapids)
945 {
946         struct bkey_i_snapshot_tree *n_tree;
947         int ret;
948
949         n_tree = __snapshot_tree_create(trans);
950         ret =   PTR_ERR_OR_ZERO(n_tree) ?:
951                 create_snapids(trans, 0, n_tree->k.p.offset,
952                              new_snapids, snapshot_subvols, nr_snapids);
953         if (ret)
954                 return ret;
955
956         n_tree->v.master_subvol = cpu_to_le32(snapshot_subvols[0]);
957         n_tree->v.root_snapshot = cpu_to_le32(new_snapids[0]);
958         return 0;
959 }
960
961 int bch2_snapshot_node_create(struct btree_trans *trans, u32 parent,
962                               u32 *new_snapids,
963                               u32 *snapshot_subvols,
964                               unsigned nr_snapids)
965 {
966         BUG_ON((parent == 0) != (nr_snapids == 1));
967         BUG_ON((parent != 0) != (nr_snapids == 2));
968
969         return parent
970                 ? bch2_snapshot_node_create_children(trans, parent,
971                                 new_snapids, snapshot_subvols, nr_snapids)
972                 : bch2_snapshot_node_create_tree(trans,
973                                 new_snapids, snapshot_subvols, nr_snapids);
974
975 }
976
977 static int snapshot_delete_key(struct btree_trans *trans,
978                                struct btree_iter *iter,
979                                struct bkey_s_c k,
980                                snapshot_id_list *deleted,
981                                snapshot_id_list *equiv_seen,
982                                struct bpos *last_pos)
983 {
984         struct bch_fs *c = trans->c;
985         u32 equiv = snapshot_t(c, k.k->p.snapshot)->equiv;
986
987         if (!bkey_eq(k.k->p, *last_pos))
988                 equiv_seen->nr = 0;
989         *last_pos = k.k->p;
990
991         if (snapshot_list_has_id(deleted, k.k->p.snapshot) ||
992             snapshot_list_has_id(equiv_seen, equiv)) {
993                 return bch2_btree_delete_at(trans, iter,
994                                             BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
995         } else {
996                 return snapshot_list_add(c, equiv_seen, equiv);
997         }
998 }
999
1000 static int bch2_delete_redundant_snapshot(struct btree_trans *trans, struct btree_iter *iter,
1001                                           struct bkey_s_c k)
1002 {
1003         struct bkey_s_c_snapshot snap;
1004         u32 children[2];
1005         int ret;
1006
1007         if (k.k->type != KEY_TYPE_snapshot)
1008                 return 0;
1009
1010         snap = bkey_s_c_to_snapshot(k);
1011         if (BCH_SNAPSHOT_DELETED(snap.v) ||
1012             BCH_SNAPSHOT_SUBVOL(snap.v))
1013                 return 0;
1014
1015         children[0] = le32_to_cpu(snap.v->children[0]);
1016         children[1] = le32_to_cpu(snap.v->children[1]);
1017
1018         ret   = snapshot_live(trans, children[0]) ?:
1019                 snapshot_live(trans, children[1]);
1020         if (ret < 0)
1021                 return ret;
1022
1023         if (!ret)
1024                 return bch2_snapshot_node_set_deleted(trans, k.k->p.offset);
1025         return 0;
1026 }
1027
1028 int bch2_delete_dead_snapshots(struct bch_fs *c)
1029 {
1030         struct btree_trans trans;
1031         struct btree_iter iter;
1032         struct bkey_s_c k;
1033         struct bkey_s_c_snapshot snap;
1034         snapshot_id_list deleted = { 0 };
1035         u32 i, id;
1036         int ret = 0;
1037
1038         if (!test_bit(BCH_FS_HAVE_DELETED_SNAPSHOTS, &c->flags))
1039                 return 0;
1040
1041         if (!test_bit(BCH_FS_STARTED, &c->flags)) {
1042                 ret = bch2_fs_read_write_early(c);
1043                 if (ret) {
1044                         bch_err(c, "error deleleting dead snapshots: error going rw: %s", bch2_err_str(ret));
1045                         return ret;
1046                 }
1047         }
1048
1049         bch2_trans_init(&trans, c, 0, 0);
1050
1051         /*
1052          * For every snapshot node: If we have no live children and it's not
1053          * pointed to by a subvolume, delete it:
1054          */
1055         ret = for_each_btree_key_commit(&trans, iter, BTREE_ID_snapshots,
1056                         POS_MIN, 0, k,
1057                         NULL, NULL, 0,
1058                 bch2_delete_redundant_snapshot(&trans, &iter, k));
1059         if (ret) {
1060                 bch_err(c, "error deleting redundant snapshots: %s", bch2_err_str(ret));
1061                 goto err;
1062         }
1063
1064         for_each_btree_key2(&trans, iter, BTREE_ID_snapshots,
1065                            POS_MIN, 0, k,
1066                 bch2_snapshot_set_equiv(&trans, k));
1067         if (ret) {
1068                 bch_err(c, "error in bch2_snapshots_set_equiv: %s", bch2_err_str(ret));
1069                 goto err;
1070         }
1071
1072         for_each_btree_key(&trans, iter, BTREE_ID_snapshots,
1073                            POS_MIN, 0, k, ret) {
1074                 if (k.k->type != KEY_TYPE_snapshot)
1075                         continue;
1076
1077                 snap = bkey_s_c_to_snapshot(k);
1078                 if (BCH_SNAPSHOT_DELETED(snap.v)) {
1079                         ret = snapshot_list_add(c, &deleted, k.k->p.offset);
1080                         if (ret)
1081                                 break;
1082                 }
1083         }
1084         bch2_trans_iter_exit(&trans, &iter);
1085
1086         if (ret) {
1087                 bch_err(c, "error walking snapshots: %s", bch2_err_str(ret));
1088                 goto err;
1089         }
1090
1091         for (id = 0; id < BTREE_ID_NR; id++) {
1092                 struct bpos last_pos = POS_MIN;
1093                 snapshot_id_list equiv_seen = { 0 };
1094
1095                 if (!btree_type_has_snapshots(id))
1096                         continue;
1097
1098                 ret = for_each_btree_key_commit(&trans, iter,
1099                                 id, POS_MIN,
1100                                 BTREE_ITER_PREFETCH|BTREE_ITER_ALL_SNAPSHOTS, k,
1101                                 NULL, NULL, BTREE_INSERT_NOFAIL,
1102                         snapshot_delete_key(&trans, &iter, k, &deleted, &equiv_seen, &last_pos));
1103
1104                 darray_exit(&equiv_seen);
1105
1106                 if (ret) {
1107                         bch_err(c, "error deleting snapshot keys: %s", bch2_err_str(ret));
1108                         goto err;
1109                 }
1110         }
1111
1112         for (i = 0; i < deleted.nr; i++) {
1113                 ret = commit_do(&trans, NULL, NULL, 0,
1114                         bch2_snapshot_node_delete(&trans, deleted.data[i]));
1115                 if (ret) {
1116                         bch_err(c, "error deleting snapshot %u: %s",
1117                                 deleted.data[i], bch2_err_str(ret));
1118                         goto err;
1119                 }
1120         }
1121
1122         clear_bit(BCH_FS_HAVE_DELETED_SNAPSHOTS, &c->flags);
1123 err:
1124         darray_exit(&deleted);
1125         bch2_trans_exit(&trans);
1126         return ret;
1127 }
1128
1129 static void bch2_delete_dead_snapshots_work(struct work_struct *work)
1130 {
1131         struct bch_fs *c = container_of(work, struct bch_fs, snapshot_delete_work);
1132
1133         bch2_delete_dead_snapshots(c);
1134         bch2_write_ref_put(c, BCH_WRITE_REF_delete_dead_snapshots);
1135 }
1136
1137 void bch2_delete_dead_snapshots_async(struct bch_fs *c)
1138 {
1139         if (bch2_write_ref_tryget(c, BCH_WRITE_REF_delete_dead_snapshots) &&
1140             !queue_work(c->write_ref_wq, &c->snapshot_delete_work))
1141                 bch2_write_ref_put(c, BCH_WRITE_REF_delete_dead_snapshots);
1142 }
1143
1144 static int bch2_delete_dead_snapshots_hook(struct btree_trans *trans,
1145                                            struct btree_trans_commit_hook *h)
1146 {
1147         struct bch_fs *c = trans->c;
1148
1149         set_bit(BCH_FS_HAVE_DELETED_SNAPSHOTS, &c->flags);
1150
1151         if (!test_bit(BCH_FS_FSCK_DONE, &c->flags))
1152                 return 0;
1153
1154         bch2_delete_dead_snapshots_async(c);
1155         return 0;
1156 }
1157
1158 /* Subvolumes: */
1159
1160 int bch2_subvolume_invalid(const struct bch_fs *c, struct bkey_s_c k,
1161                            unsigned flags, struct printbuf *err)
1162 {
1163         if (bkey_lt(k.k->p, SUBVOL_POS_MIN) ||
1164             bkey_gt(k.k->p, SUBVOL_POS_MAX)) {
1165                 prt_printf(err, "invalid pos");
1166                 return -BCH_ERR_invalid_bkey;
1167         }
1168
1169         return 0;
1170 }
1171
1172 void bch2_subvolume_to_text(struct printbuf *out, struct bch_fs *c,
1173                             struct bkey_s_c k)
1174 {
1175         struct bkey_s_c_subvolume s = bkey_s_c_to_subvolume(k);
1176
1177         prt_printf(out, "root %llu snapshot id %u",
1178                    le64_to_cpu(s.v->inode),
1179                    le32_to_cpu(s.v->snapshot));
1180
1181         if (bkey_val_bytes(s.k) > offsetof(struct bch_subvolume, parent))
1182                 prt_printf(out, " parent %u", le32_to_cpu(s.v->parent));
1183 }
1184
1185 static __always_inline int
1186 bch2_subvolume_get_inlined(struct btree_trans *trans, unsigned subvol,
1187                            bool inconsistent_if_not_found,
1188                            int iter_flags,
1189                            struct bch_subvolume *s)
1190 {
1191         int ret = bch2_bkey_get_val_typed(trans, BTREE_ID_subvolumes, POS(0, subvol),
1192                                           iter_flags, subvolume, s);
1193         bch2_fs_inconsistent_on(bch2_err_matches(ret, ENOENT) &&
1194                                 inconsistent_if_not_found,
1195                                 trans->c, "missing subvolume %u", subvol);
1196         return ret;
1197 }
1198
1199 int bch2_subvolume_get(struct btree_trans *trans, unsigned subvol,
1200                        bool inconsistent_if_not_found,
1201                        int iter_flags,
1202                        struct bch_subvolume *s)
1203 {
1204         return bch2_subvolume_get_inlined(trans, subvol, inconsistent_if_not_found, iter_flags, s);
1205 }
1206
1207 int bch2_snapshot_get_subvol(struct btree_trans *trans, u32 snapshot,
1208                              struct bch_subvolume *subvol)
1209 {
1210         struct bch_snapshot snap;
1211
1212         return  snapshot_lookup(trans, snapshot, &snap) ?:
1213                 bch2_subvolume_get(trans, le32_to_cpu(snap.subvol), true, 0, subvol);
1214 }
1215
1216 int bch2_subvolume_get_snapshot(struct btree_trans *trans, u32 subvol,
1217                                 u32 *snapid)
1218 {
1219         struct btree_iter iter;
1220         struct bkey_s_c k;
1221         int ret;
1222
1223         k = bch2_bkey_get_iter(trans, &iter, BTREE_ID_subvolumes, POS(0, subvol),
1224                                BTREE_ITER_CACHED|
1225                                BTREE_ITER_WITH_UPDATES);
1226         ret = bkey_err(k) ?: k.k->type == KEY_TYPE_subvolume ? 0 : -BCH_ERR_ENOENT_subvolume;
1227
1228         if (likely(!ret))
1229                 *snapid = le32_to_cpu(bkey_s_c_to_subvolume(k).v->snapshot);
1230         else if (bch2_err_matches(ret, ENOENT))
1231                 bch2_fs_inconsistent(trans->c, "missing subvolume %u", subvol);
1232         bch2_trans_iter_exit(trans, &iter);
1233         return ret;
1234 }
1235
1236 static int bch2_subvolume_reparent(struct btree_trans *trans,
1237                                    struct btree_iter *iter,
1238                                    struct bkey_s_c k,
1239                                    u32 old_parent, u32 new_parent)
1240 {
1241         struct bkey_i_subvolume *s;
1242         int ret;
1243
1244         if (k.k->type != KEY_TYPE_subvolume)
1245                 return 0;
1246
1247         if (bkey_val_bytes(k.k) > offsetof(struct bch_subvolume, parent) &&
1248             le32_to_cpu(bkey_s_c_to_subvolume(k).v->parent) != old_parent)
1249                 return 0;
1250
1251         s = bch2_bkey_make_mut_typed(trans, iter, k, 0, subvolume);
1252         ret = PTR_ERR_OR_ZERO(s);
1253         if (ret)
1254                 return ret;
1255
1256         s->v.parent = cpu_to_le32(new_parent);
1257         return 0;
1258 }
1259
1260 /*
1261  * Scan for subvolumes with parent @subvolid_to_delete, reparent:
1262  */
1263 static int bch2_subvolumes_reparent(struct btree_trans *trans, u32 subvolid_to_delete)
1264 {
1265         struct btree_iter iter;
1266         struct bkey_s_c k;
1267         struct bch_subvolume s;
1268
1269         return lockrestart_do(trans,
1270                         bch2_subvolume_get(trans, subvolid_to_delete, true,
1271                                    BTREE_ITER_CACHED, &s)) ?:
1272                 for_each_btree_key_commit(trans, iter,
1273                                 BTREE_ID_subvolumes, POS_MIN, BTREE_ITER_PREFETCH, k,
1274                                 NULL, NULL, BTREE_INSERT_NOFAIL,
1275                         bch2_subvolume_reparent(trans, &iter, k,
1276                                         subvolid_to_delete, le32_to_cpu(s.parent)));
1277 }
1278
1279 /*
1280  * Delete subvolume, mark snapshot ID as deleted, queue up snapshot
1281  * deletion/cleanup:
1282  */
1283 static int __bch2_subvolume_delete(struct btree_trans *trans, u32 subvolid)
1284 {
1285         struct btree_iter iter;
1286         struct bkey_s_c_subvolume subvol;
1287         struct btree_trans_commit_hook *h;
1288         u32 snapid;
1289         int ret = 0;
1290
1291         subvol = bch2_bkey_get_iter_typed(trans, &iter,
1292                                 BTREE_ID_subvolumes, POS(0, subvolid),
1293                                 BTREE_ITER_CACHED|BTREE_ITER_INTENT,
1294                                 subvolume);
1295         ret = bkey_err(subvol);
1296         bch2_fs_inconsistent_on(bch2_err_matches(ret, ENOENT), trans->c,
1297                                 "missing subvolume %u", subvolid);
1298         if (ret)
1299                 return ret;
1300
1301         snapid = le32_to_cpu(subvol.v->snapshot);
1302
1303         ret = bch2_btree_delete_at(trans, &iter, 0);
1304         if (ret)
1305                 goto err;
1306
1307         ret = bch2_snapshot_node_set_deleted(trans, snapid);
1308         if (ret)
1309                 goto err;
1310
1311         h = bch2_trans_kmalloc(trans, sizeof(*h));
1312         ret = PTR_ERR_OR_ZERO(h);
1313         if (ret)
1314                 goto err;
1315
1316         h->fn = bch2_delete_dead_snapshots_hook;
1317         bch2_trans_commit_hook(trans, h);
1318 err:
1319         bch2_trans_iter_exit(trans, &iter);
1320         return ret;
1321 }
1322
1323 static int bch2_subvolume_delete(struct btree_trans *trans, u32 subvolid)
1324 {
1325         return bch2_subvolumes_reparent(trans, subvolid) ?:
1326                 commit_do(trans, NULL, NULL, BTREE_INSERT_NOFAIL,
1327                           __bch2_subvolume_delete(trans, subvolid));
1328 }
1329
1330 void bch2_subvolume_wait_for_pagecache_and_delete(struct work_struct *work)
1331 {
1332         struct bch_fs *c = container_of(work, struct bch_fs,
1333                                 snapshot_wait_for_pagecache_and_delete_work);
1334         snapshot_id_list s;
1335         u32 *id;
1336         int ret = 0;
1337
1338         while (!ret) {
1339                 mutex_lock(&c->snapshots_unlinked_lock);
1340                 s = c->snapshots_unlinked;
1341                 darray_init(&c->snapshots_unlinked);
1342                 mutex_unlock(&c->snapshots_unlinked_lock);
1343
1344                 if (!s.nr)
1345                         break;
1346
1347                 bch2_evict_subvolume_inodes(c, &s);
1348
1349                 for (id = s.data; id < s.data + s.nr; id++) {
1350                         ret = bch2_trans_run(c, bch2_subvolume_delete(&trans, *id));
1351                         if (ret) {
1352                                 bch_err(c, "error deleting subvolume %u: %s", *id, bch2_err_str(ret));
1353                                 break;
1354                         }
1355                 }
1356
1357                 darray_exit(&s);
1358         }
1359
1360         bch2_write_ref_put(c, BCH_WRITE_REF_snapshot_delete_pagecache);
1361 }
1362
1363 struct subvolume_unlink_hook {
1364         struct btree_trans_commit_hook  h;
1365         u32                             subvol;
1366 };
1367
1368 int bch2_subvolume_wait_for_pagecache_and_delete_hook(struct btree_trans *trans,
1369                                                       struct btree_trans_commit_hook *_h)
1370 {
1371         struct subvolume_unlink_hook *h = container_of(_h, struct subvolume_unlink_hook, h);
1372         struct bch_fs *c = trans->c;
1373         int ret = 0;
1374
1375         mutex_lock(&c->snapshots_unlinked_lock);
1376         if (!snapshot_list_has_id(&c->snapshots_unlinked, h->subvol))
1377                 ret = snapshot_list_add(c, &c->snapshots_unlinked, h->subvol);
1378         mutex_unlock(&c->snapshots_unlinked_lock);
1379
1380         if (ret)
1381                 return ret;
1382
1383         if (!bch2_write_ref_tryget(c, BCH_WRITE_REF_snapshot_delete_pagecache))
1384                 return -EROFS;
1385
1386         if (!queue_work(c->write_ref_wq, &c->snapshot_wait_for_pagecache_and_delete_work))
1387                 bch2_write_ref_put(c, BCH_WRITE_REF_snapshot_delete_pagecache);
1388         return 0;
1389 }
1390
1391 int bch2_subvolume_unlink(struct btree_trans *trans, u32 subvolid)
1392 {
1393         struct btree_iter iter;
1394         struct bkey_i_subvolume *n;
1395         struct subvolume_unlink_hook *h;
1396         int ret = 0;
1397
1398         h = bch2_trans_kmalloc(trans, sizeof(*h));
1399         ret = PTR_ERR_OR_ZERO(h);
1400         if (ret)
1401                 return ret;
1402
1403         h->h.fn         = bch2_subvolume_wait_for_pagecache_and_delete_hook;
1404         h->subvol       = subvolid;
1405         bch2_trans_commit_hook(trans, &h->h);
1406
1407         n = bch2_bkey_get_mut_typed(trans, &iter,
1408                         BTREE_ID_subvolumes, POS(0, subvolid),
1409                         BTREE_ITER_CACHED, subvolume);
1410         ret = PTR_ERR_OR_ZERO(n);
1411         if (unlikely(ret)) {
1412                 bch2_fs_inconsistent_on(bch2_err_matches(ret, ENOENT), trans->c,
1413                                         "missing subvolume %u", subvolid);
1414                 return ret;
1415         }
1416
1417         SET_BCH_SUBVOLUME_UNLINKED(&n->v, true);
1418         bch2_trans_iter_exit(trans, &iter);
1419         return ret;
1420 }
1421
1422 int bch2_subvolume_create(struct btree_trans *trans, u64 inode,
1423                           u32 src_subvolid,
1424                           u32 *new_subvolid,
1425                           u32 *new_snapshotid,
1426                           bool ro)
1427 {
1428         struct bch_fs *c = trans->c;
1429         struct btree_iter dst_iter, src_iter = (struct btree_iter) { NULL };
1430         struct bkey_i_subvolume *new_subvol = NULL;
1431         struct bkey_i_subvolume *src_subvol = NULL;
1432         u32 parent = 0, new_nodes[2], snapshot_subvols[2];
1433         int ret = 0;
1434
1435         ret = bch2_bkey_get_empty_slot(trans, &dst_iter,
1436                                 BTREE_ID_subvolumes, POS(0, U32_MAX));
1437         if (ret == -BCH_ERR_ENOSPC_btree_slot)
1438                 ret = -BCH_ERR_ENOSPC_subvolume_create;
1439         if (ret)
1440                 return ret;
1441
1442         snapshot_subvols[0] = dst_iter.pos.offset;
1443         snapshot_subvols[1] = src_subvolid;
1444
1445         if (src_subvolid) {
1446                 /* Creating a snapshot: */
1447
1448                 src_subvol = bch2_bkey_get_mut_typed(trans, &src_iter,
1449                                 BTREE_ID_subvolumes, POS(0, src_subvolid),
1450                                 BTREE_ITER_CACHED, subvolume);
1451                 ret = PTR_ERR_OR_ZERO(src_subvol);
1452                 if (unlikely(ret)) {
1453                         bch2_fs_inconsistent_on(bch2_err_matches(ret, ENOENT), c,
1454                                                 "subvolume %u not found", src_subvolid);
1455                         goto err;
1456                 }
1457
1458                 parent = le32_to_cpu(src_subvol->v.snapshot);
1459         }
1460
1461         ret = bch2_snapshot_node_create(trans, parent, new_nodes,
1462                                         snapshot_subvols,
1463                                         src_subvolid ? 2 : 1);
1464         if (ret)
1465                 goto err;
1466
1467         if (src_subvolid) {
1468                 src_subvol->v.snapshot = cpu_to_le32(new_nodes[1]);
1469                 ret = bch2_trans_update(trans, &src_iter, &src_subvol->k_i, 0);
1470                 if (ret)
1471                         goto err;
1472         }
1473
1474         new_subvol = bch2_bkey_alloc(trans, &dst_iter, 0, subvolume);
1475         ret = PTR_ERR_OR_ZERO(new_subvol);
1476         if (ret)
1477                 goto err;
1478
1479         new_subvol->v.flags     = 0;
1480         new_subvol->v.snapshot  = cpu_to_le32(new_nodes[0]);
1481         new_subvol->v.inode     = cpu_to_le64(inode);
1482         new_subvol->v.parent    = cpu_to_le32(src_subvolid);
1483         new_subvol->v.otime.lo  = cpu_to_le64(bch2_current_time(c));
1484         new_subvol->v.otime.hi  = 0;
1485
1486         SET_BCH_SUBVOLUME_RO(&new_subvol->v, ro);
1487         SET_BCH_SUBVOLUME_SNAP(&new_subvol->v, src_subvolid != 0);
1488
1489         *new_subvolid   = new_subvol->k.p.offset;
1490         *new_snapshotid = new_nodes[0];
1491 err:
1492         bch2_trans_iter_exit(trans, &src_iter);
1493         bch2_trans_iter_exit(trans, &dst_iter);
1494         return ret;
1495 }
1496
1497 int bch2_fs_subvolumes_init(struct bch_fs *c)
1498 {
1499         INIT_WORK(&c->snapshot_delete_work, bch2_delete_dead_snapshots_work);
1500         INIT_WORK(&c->snapshot_wait_for_pagecache_and_delete_work,
1501                   bch2_subvolume_wait_for_pagecache_and_delete);
1502         mutex_init(&c->snapshots_unlinked_lock);
1503         return 0;
1504 }