]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/subvolume.c
Update bcachefs sources to e7f6215768 bcachefs: Fix snapshot_skiplist_good()
[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 "snapshot.h"
10 #include "subvolume.h"
11
12 #include <linux/random.h>
13
14 static int bch2_subvolume_delete(struct btree_trans *, u32);
15
16 static int check_subvol(struct btree_trans *trans,
17                         struct btree_iter *iter,
18                         struct bkey_s_c k)
19 {
20         struct bch_fs *c = trans->c;
21         struct bkey_s_c_subvolume subvol;
22         struct bch_snapshot snapshot;
23         unsigned snapid;
24         int ret = 0;
25
26         if (k.k->type != KEY_TYPE_subvolume)
27                 return 0;
28
29         subvol = bkey_s_c_to_subvolume(k);
30         snapid = le32_to_cpu(subvol.v->snapshot);
31         ret = bch2_snapshot_lookup(trans, snapid, &snapshot);
32
33         if (bch2_err_matches(ret, ENOENT))
34                 bch_err(c, "subvolume %llu points to nonexistent snapshot %u",
35                         k.k->p.offset, snapid);
36         if (ret)
37                 return ret;
38
39         if (BCH_SUBVOLUME_UNLINKED(subvol.v)) {
40                 bch2_fs_lazy_rw(c);
41
42                 ret = bch2_subvolume_delete(trans, iter->pos.offset);
43                 if (ret)
44                         bch_err(c, "error deleting subvolume %llu: %s",
45                                 iter->pos.offset, bch2_err_str(ret));
46                 return ret ?: -BCH_ERR_transaction_restart_nested;
47         }
48
49         if (!BCH_SUBVOLUME_SNAP(subvol.v)) {
50                 u32 snapshot_root = bch2_snapshot_root(c, le32_to_cpu(subvol.v->snapshot));
51                 u32 snapshot_tree;
52                 struct bch_snapshot_tree st;
53
54                 rcu_read_lock();
55                 snapshot_tree = snapshot_t(c, snapshot_root)->tree;
56                 rcu_read_unlock();
57
58                 ret = bch2_snapshot_tree_lookup(trans, snapshot_tree, &st);
59
60                 bch2_fs_inconsistent_on(bch2_err_matches(ret, ENOENT), c,
61                                 "%s: snapshot tree %u not found", __func__, snapshot_tree);
62
63                 if (ret)
64                         return ret;
65
66                 if (fsck_err_on(le32_to_cpu(st.master_subvol) != subvol.k->p.offset, c,
67                                 "subvolume %llu is not set as snapshot but is not master subvolume",
68                                 k.k->p.offset)) {
69                         struct bkey_i_subvolume *s =
70                                 bch2_bkey_make_mut_typed(trans, iter, &subvol.s_c, 0, subvolume);
71                         ret = PTR_ERR_OR_ZERO(s);
72                         if (ret)
73                                 return ret;
74
75                         SET_BCH_SUBVOLUME_SNAP(&s->v, true);
76                 }
77         }
78
79 fsck_err:
80         return ret;
81 }
82
83 int bch2_check_subvols(struct bch_fs *c)
84 {
85         struct btree_iter iter;
86         struct bkey_s_c k;
87         int ret;
88
89         ret = bch2_trans_run(c,
90                 for_each_btree_key_commit(&trans, iter,
91                         BTREE_ID_subvolumes, POS_MIN, BTREE_ITER_PREFETCH, k,
92                         NULL, NULL, BTREE_INSERT_LAZY_RW|BTREE_INSERT_NOFAIL,
93                 check_subvol(&trans, &iter, k)));
94         if (ret)
95                 bch_err_fn(c, ret);
96         return ret;
97 }
98
99 /* Subvolumes: */
100
101 int bch2_subvolume_invalid(const struct bch_fs *c, struct bkey_s_c k,
102                            unsigned flags, struct printbuf *err)
103 {
104         if (bkey_lt(k.k->p, SUBVOL_POS_MIN) ||
105             bkey_gt(k.k->p, SUBVOL_POS_MAX)) {
106                 prt_printf(err, "invalid pos");
107                 return -BCH_ERR_invalid_bkey;
108         }
109
110         return 0;
111 }
112
113 void bch2_subvolume_to_text(struct printbuf *out, struct bch_fs *c,
114                             struct bkey_s_c k)
115 {
116         struct bkey_s_c_subvolume s = bkey_s_c_to_subvolume(k);
117
118         prt_printf(out, "root %llu snapshot id %u",
119                    le64_to_cpu(s.v->inode),
120                    le32_to_cpu(s.v->snapshot));
121
122         if (bkey_val_bytes(s.k) > offsetof(struct bch_subvolume, parent))
123                 prt_printf(out, " parent %u", le32_to_cpu(s.v->parent));
124 }
125
126 static __always_inline int
127 bch2_subvolume_get_inlined(struct btree_trans *trans, unsigned subvol,
128                            bool inconsistent_if_not_found,
129                            int iter_flags,
130                            struct bch_subvolume *s)
131 {
132         int ret = bch2_bkey_get_val_typed(trans, BTREE_ID_subvolumes, POS(0, subvol),
133                                           iter_flags, subvolume, s);
134         bch2_fs_inconsistent_on(bch2_err_matches(ret, ENOENT) &&
135                                 inconsistent_if_not_found,
136                                 trans->c, "missing subvolume %u", subvol);
137         return ret;
138 }
139
140 int bch2_subvolume_get(struct btree_trans *trans, unsigned subvol,
141                        bool inconsistent_if_not_found,
142                        int iter_flags,
143                        struct bch_subvolume *s)
144 {
145         return bch2_subvolume_get_inlined(trans, subvol, inconsistent_if_not_found, iter_flags, s);
146 }
147
148 int bch2_snapshot_get_subvol(struct btree_trans *trans, u32 snapshot,
149                              struct bch_subvolume *subvol)
150 {
151         struct bch_snapshot snap;
152
153         return  bch2_snapshot_lookup(trans, snapshot, &snap) ?:
154                 bch2_subvolume_get(trans, le32_to_cpu(snap.subvol), true, 0, subvol);
155 }
156
157 int bch2_subvolume_get_snapshot(struct btree_trans *trans, u32 subvolid,
158                                 u32 *snapid)
159 {
160         struct btree_iter iter;
161         struct bkey_s_c_subvolume subvol;
162         int ret;
163
164         subvol = bch2_bkey_get_iter_typed(trans, &iter,
165                                           BTREE_ID_subvolumes, POS(0, subvolid),
166                                           BTREE_ITER_CACHED|BTREE_ITER_WITH_UPDATES,
167                                           subvolume);
168         ret = bkey_err(subvol);
169         bch2_fs_inconsistent_on(bch2_err_matches(ret, ENOENT), trans->c,
170                                 "missing subvolume %u", subvolid);
171
172         if (likely(!ret))
173                 *snapid = le32_to_cpu(subvol.v->snapshot);
174         bch2_trans_iter_exit(trans, &iter);
175         return ret;
176 }
177
178 static int bch2_subvolume_reparent(struct btree_trans *trans,
179                                    struct btree_iter *iter,
180                                    struct bkey_s_c k,
181                                    u32 old_parent, u32 new_parent)
182 {
183         struct bkey_i_subvolume *s;
184         int ret;
185
186         if (k.k->type != KEY_TYPE_subvolume)
187                 return 0;
188
189         if (bkey_val_bytes(k.k) > offsetof(struct bch_subvolume, parent) &&
190             le32_to_cpu(bkey_s_c_to_subvolume(k).v->parent) != old_parent)
191                 return 0;
192
193         s = bch2_bkey_make_mut_typed(trans, iter, &k, 0, subvolume);
194         ret = PTR_ERR_OR_ZERO(s);
195         if (ret)
196                 return ret;
197
198         s->v.parent = cpu_to_le32(new_parent);
199         return 0;
200 }
201
202 /*
203  * Separate from the snapshot tree in the snapshots btree, we record the tree
204  * structure of how snapshot subvolumes were created - the parent subvolume of
205  * each snapshot subvolume.
206  *
207  * When a subvolume is deleted, we scan for child subvolumes and reparant them,
208  * to avoid dangling references:
209  */
210 static int bch2_subvolumes_reparent(struct btree_trans *trans, u32 subvolid_to_delete)
211 {
212         struct btree_iter iter;
213         struct bkey_s_c k;
214         struct bch_subvolume s;
215
216         return lockrestart_do(trans,
217                         bch2_subvolume_get(trans, subvolid_to_delete, true,
218                                    BTREE_ITER_CACHED, &s)) ?:
219                 for_each_btree_key_commit(trans, iter,
220                                 BTREE_ID_subvolumes, POS_MIN, BTREE_ITER_PREFETCH, k,
221                                 NULL, NULL, BTREE_INSERT_NOFAIL,
222                         bch2_subvolume_reparent(trans, &iter, k,
223                                         subvolid_to_delete, le32_to_cpu(s.parent)));
224 }
225
226 /*
227  * Delete subvolume, mark snapshot ID as deleted, queue up snapshot
228  * deletion/cleanup:
229  */
230 static int __bch2_subvolume_delete(struct btree_trans *trans, u32 subvolid)
231 {
232         struct btree_iter iter;
233         struct bkey_s_c_subvolume subvol;
234         struct btree_trans_commit_hook *h;
235         u32 snapid;
236         int ret = 0;
237
238         subvol = bch2_bkey_get_iter_typed(trans, &iter,
239                                 BTREE_ID_subvolumes, POS(0, subvolid),
240                                 BTREE_ITER_CACHED|BTREE_ITER_INTENT,
241                                 subvolume);
242         ret = bkey_err(subvol);
243         bch2_fs_inconsistent_on(bch2_err_matches(ret, ENOENT), trans->c,
244                                 "missing subvolume %u", subvolid);
245         if (ret)
246                 return ret;
247
248         snapid = le32_to_cpu(subvol.v->snapshot);
249
250         ret = bch2_btree_delete_at(trans, &iter, 0);
251         if (ret)
252                 goto err;
253
254         ret = bch2_snapshot_node_set_deleted(trans, snapid);
255         if (ret)
256                 goto err;
257
258         h = bch2_trans_kmalloc(trans, sizeof(*h));
259         ret = PTR_ERR_OR_ZERO(h);
260         if (ret)
261                 goto err;
262
263         h->fn = bch2_delete_dead_snapshots_hook;
264         bch2_trans_commit_hook(trans, h);
265 err:
266         bch2_trans_iter_exit(trans, &iter);
267         return ret;
268 }
269
270 static int bch2_subvolume_delete(struct btree_trans *trans, u32 subvolid)
271 {
272         return bch2_subvolumes_reparent(trans, subvolid) ?:
273                 commit_do(trans, NULL, NULL, BTREE_INSERT_NOFAIL,
274                           __bch2_subvolume_delete(trans, subvolid));
275 }
276
277 static void bch2_subvolume_wait_for_pagecache_and_delete(struct work_struct *work)
278 {
279         struct bch_fs *c = container_of(work, struct bch_fs,
280                                 snapshot_wait_for_pagecache_and_delete_work);
281         snapshot_id_list s;
282         u32 *id;
283         int ret = 0;
284
285         while (!ret) {
286                 mutex_lock(&c->snapshots_unlinked_lock);
287                 s = c->snapshots_unlinked;
288                 darray_init(&c->snapshots_unlinked);
289                 mutex_unlock(&c->snapshots_unlinked_lock);
290
291                 if (!s.nr)
292                         break;
293
294                 bch2_evict_subvolume_inodes(c, &s);
295
296                 for (id = s.data; id < s.data + s.nr; id++) {
297                         ret = bch2_trans_run(c, bch2_subvolume_delete(&trans, *id));
298                         if (ret) {
299                                 bch_err(c, "error deleting subvolume %u: %s", *id, bch2_err_str(ret));
300                                 break;
301                         }
302                 }
303
304                 darray_exit(&s);
305         }
306
307         bch2_write_ref_put(c, BCH_WRITE_REF_snapshot_delete_pagecache);
308 }
309
310 struct subvolume_unlink_hook {
311         struct btree_trans_commit_hook  h;
312         u32                             subvol;
313 };
314
315 static int bch2_subvolume_wait_for_pagecache_and_delete_hook(struct btree_trans *trans,
316                                                       struct btree_trans_commit_hook *_h)
317 {
318         struct subvolume_unlink_hook *h = container_of(_h, struct subvolume_unlink_hook, h);
319         struct bch_fs *c = trans->c;
320         int ret = 0;
321
322         mutex_lock(&c->snapshots_unlinked_lock);
323         if (!snapshot_list_has_id(&c->snapshots_unlinked, h->subvol))
324                 ret = snapshot_list_add(c, &c->snapshots_unlinked, h->subvol);
325         mutex_unlock(&c->snapshots_unlinked_lock);
326
327         if (ret)
328                 return ret;
329
330         if (!bch2_write_ref_tryget(c, BCH_WRITE_REF_snapshot_delete_pagecache))
331                 return -EROFS;
332
333         if (!queue_work(c->write_ref_wq, &c->snapshot_wait_for_pagecache_and_delete_work))
334                 bch2_write_ref_put(c, BCH_WRITE_REF_snapshot_delete_pagecache);
335         return 0;
336 }
337
338 int bch2_subvolume_unlink(struct btree_trans *trans, u32 subvolid)
339 {
340         struct btree_iter iter;
341         struct bkey_i_subvolume *n;
342         struct subvolume_unlink_hook *h;
343         int ret = 0;
344
345         h = bch2_trans_kmalloc(trans, sizeof(*h));
346         ret = PTR_ERR_OR_ZERO(h);
347         if (ret)
348                 return ret;
349
350         h->h.fn         = bch2_subvolume_wait_for_pagecache_and_delete_hook;
351         h->subvol       = subvolid;
352         bch2_trans_commit_hook(trans, &h->h);
353
354         n = bch2_bkey_get_mut_typed(trans, &iter,
355                         BTREE_ID_subvolumes, POS(0, subvolid),
356                         BTREE_ITER_CACHED, subvolume);
357         ret = PTR_ERR_OR_ZERO(n);
358         if (unlikely(ret)) {
359                 bch2_fs_inconsistent_on(bch2_err_matches(ret, ENOENT), trans->c,
360                                         "missing subvolume %u", subvolid);
361                 return ret;
362         }
363
364         SET_BCH_SUBVOLUME_UNLINKED(&n->v, true);
365         bch2_trans_iter_exit(trans, &iter);
366         return ret;
367 }
368
369 int bch2_subvolume_create(struct btree_trans *trans, u64 inode,
370                           u32 src_subvolid,
371                           u32 *new_subvolid,
372                           u32 *new_snapshotid,
373                           bool ro)
374 {
375         struct bch_fs *c = trans->c;
376         struct btree_iter dst_iter, src_iter = (struct btree_iter) { NULL };
377         struct bkey_i_subvolume *new_subvol = NULL;
378         struct bkey_i_subvolume *src_subvol = NULL;
379         u32 parent = 0, new_nodes[2], snapshot_subvols[2];
380         int ret = 0;
381
382         ret = bch2_bkey_get_empty_slot(trans, &dst_iter,
383                                 BTREE_ID_subvolumes, POS(0, U32_MAX));
384         if (ret == -BCH_ERR_ENOSPC_btree_slot)
385                 ret = -BCH_ERR_ENOSPC_subvolume_create;
386         if (ret)
387                 return ret;
388
389         snapshot_subvols[0] = dst_iter.pos.offset;
390         snapshot_subvols[1] = src_subvolid;
391
392         if (src_subvolid) {
393                 /* Creating a snapshot: */
394
395                 src_subvol = bch2_bkey_get_mut_typed(trans, &src_iter,
396                                 BTREE_ID_subvolumes, POS(0, src_subvolid),
397                                 BTREE_ITER_CACHED, subvolume);
398                 ret = PTR_ERR_OR_ZERO(src_subvol);
399                 if (unlikely(ret)) {
400                         bch2_fs_inconsistent_on(bch2_err_matches(ret, ENOENT), c,
401                                                 "subvolume %u not found", src_subvolid);
402                         goto err;
403                 }
404
405                 parent = le32_to_cpu(src_subvol->v.snapshot);
406         }
407
408         ret = bch2_snapshot_node_create(trans, parent, new_nodes,
409                                         snapshot_subvols,
410                                         src_subvolid ? 2 : 1);
411         if (ret)
412                 goto err;
413
414         if (src_subvolid) {
415                 src_subvol->v.snapshot = cpu_to_le32(new_nodes[1]);
416                 ret = bch2_trans_update(trans, &src_iter, &src_subvol->k_i, 0);
417                 if (ret)
418                         goto err;
419         }
420
421         new_subvol = bch2_bkey_alloc(trans, &dst_iter, 0, subvolume);
422         ret = PTR_ERR_OR_ZERO(new_subvol);
423         if (ret)
424                 goto err;
425
426         new_subvol->v.flags     = 0;
427         new_subvol->v.snapshot  = cpu_to_le32(new_nodes[0]);
428         new_subvol->v.inode     = cpu_to_le64(inode);
429         new_subvol->v.parent    = cpu_to_le32(src_subvolid);
430         new_subvol->v.otime.lo  = cpu_to_le64(bch2_current_time(c));
431         new_subvol->v.otime.hi  = 0;
432
433         SET_BCH_SUBVOLUME_RO(&new_subvol->v, ro);
434         SET_BCH_SUBVOLUME_SNAP(&new_subvol->v, src_subvolid != 0);
435
436         *new_subvolid   = new_subvol->k.p.offset;
437         *new_snapshotid = new_nodes[0];
438 err:
439         bch2_trans_iter_exit(trans, &src_iter);
440         bch2_trans_iter_exit(trans, &dst_iter);
441         return ret;
442 }
443
444 int bch2_fs_subvolumes_init(struct bch_fs *c)
445 {
446         INIT_WORK(&c->snapshot_delete_work, bch2_delete_dead_snapshots_work);
447         INIT_WORK(&c->snapshot_wait_for_pagecache_and_delete_work,
448                   bch2_subvolume_wait_for_pagecache_and_delete);
449         mutex_init(&c->snapshots_unlinked_lock);
450         return 0;
451 }