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