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