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