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