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