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