xref: /linux/fs/bcachefs/subvolume.c (revision 9f9a53472452b83d44d5e1d77b6dea6eaa043204)
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 
subvolume_children_pos(struct bkey_s_c k)16 static struct bpos subvolume_children_pos(struct bkey_s_c k)
17 {
18 	if (k.k->type != KEY_TYPE_subvolume)
19 		return POS_MIN;
20 
21 	struct bkey_s_c_subvolume s = bkey_s_c_to_subvolume(k);
22 	if (!s.v->fs_path_parent)
23 		return POS_MIN;
24 	return POS(le32_to_cpu(s.v->fs_path_parent), s.k->p.offset);
25 }
26 
check_subvol(struct btree_trans * trans,struct btree_iter * iter,struct bkey_s_c k)27 static int check_subvol(struct btree_trans *trans,
28 			struct btree_iter *iter,
29 			struct bkey_s_c k)
30 {
31 	struct bch_fs *c = trans->c;
32 	struct bkey_s_c_subvolume subvol;
33 	struct btree_iter subvol_children_iter = {};
34 	struct bch_snapshot snapshot;
35 	struct printbuf buf = PRINTBUF;
36 	unsigned snapid;
37 	int ret = 0;
38 
39 	if (k.k->type != KEY_TYPE_subvolume)
40 		return 0;
41 
42 	subvol = bkey_s_c_to_subvolume(k);
43 	snapid = le32_to_cpu(subvol.v->snapshot);
44 	ret = bch2_snapshot_lookup(trans, snapid, &snapshot);
45 
46 	if (bch2_err_matches(ret, ENOENT))
47 		bch_err(c, "subvolume %llu points to nonexistent snapshot %u",
48 			k.k->p.offset, snapid);
49 	if (ret)
50 		return ret;
51 
52 	if (BCH_SUBVOLUME_UNLINKED(subvol.v)) {
53 		ret = bch2_subvolume_delete(trans, iter->pos.offset);
54 		bch_err_msg(c, ret, "deleting subvolume %llu", iter->pos.offset);
55 		return ret ?: -BCH_ERR_transaction_restart_nested;
56 	}
57 
58 	if (fsck_err_on(subvol.k->p.offset == BCACHEFS_ROOT_SUBVOL &&
59 			subvol.v->fs_path_parent,
60 			trans, subvol_root_fs_path_parent_nonzero,
61 			"root subvolume has nonzero fs_path_parent\n%s",
62 			(bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
63 		struct bkey_i_subvolume *n =
64 			bch2_bkey_make_mut_typed(trans, iter, &subvol.s_c, 0, subvolume);
65 		ret = PTR_ERR_OR_ZERO(n);
66 		if (ret)
67 			goto err;
68 
69 		n->v.fs_path_parent = 0;
70 	}
71 
72 	if (subvol.v->fs_path_parent) {
73 		struct bpos pos = subvolume_children_pos(k);
74 
75 		struct bkey_s_c subvol_children_k =
76 			bch2_bkey_get_iter(trans, &subvol_children_iter,
77 					   BTREE_ID_subvolume_children, pos, 0);
78 		ret = bkey_err(subvol_children_k);
79 		if (ret)
80 			goto err;
81 
82 		if (fsck_err_on(subvol_children_k.k->type != KEY_TYPE_set,
83 				trans, subvol_children_not_set,
84 				"subvolume not set in subvolume_children btree at %llu:%llu\n%s",
85 				pos.inode, pos.offset,
86 				(printbuf_reset(&buf),
87 				 bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
88 			ret = bch2_btree_bit_mod(trans, BTREE_ID_subvolume_children, pos, true);
89 			if (ret)
90 				goto err;
91 		}
92 	}
93 
94 	struct bch_inode_unpacked inode;
95 	ret = bch2_inode_find_by_inum_nowarn_trans(trans,
96 				    (subvol_inum) { k.k->p.offset, le64_to_cpu(subvol.v->inode) },
97 				    &inode);
98 	if (!ret) {
99 		if (fsck_err_on(inode.bi_subvol != subvol.k->p.offset,
100 				trans, subvol_root_wrong_bi_subvol,
101 				"subvol root %llu:%u has wrong bi_subvol field: got %u, should be %llu",
102 				inode.bi_inum, inode.bi_snapshot,
103 				inode.bi_subvol, subvol.k->p.offset)) {
104 			inode.bi_subvol = subvol.k->p.offset;
105 			ret = __bch2_fsck_write_inode(trans, &inode, le32_to_cpu(subvol.v->snapshot));
106 			if (ret)
107 				goto err;
108 		}
109 	} else if (bch2_err_matches(ret, ENOENT)) {
110 		if (fsck_err(trans, subvol_to_missing_root,
111 			     "subvolume %llu points to missing subvolume root %llu:%u",
112 			     k.k->p.offset, le64_to_cpu(subvol.v->inode),
113 			     le32_to_cpu(subvol.v->snapshot))) {
114 			ret = bch2_subvolume_delete(trans, iter->pos.offset);
115 			bch_err_msg(c, ret, "deleting subvolume %llu", iter->pos.offset);
116 			ret = ret ?: -BCH_ERR_transaction_restart_nested;
117 			goto err;
118 		}
119 	} else {
120 		goto err;
121 	}
122 
123 	if (!BCH_SUBVOLUME_SNAP(subvol.v)) {
124 		u32 snapshot_root = bch2_snapshot_root(c, le32_to_cpu(subvol.v->snapshot));
125 		u32 snapshot_tree;
126 		struct bch_snapshot_tree st;
127 
128 		rcu_read_lock();
129 		snapshot_tree = snapshot_t(c, snapshot_root)->tree;
130 		rcu_read_unlock();
131 
132 		ret = bch2_snapshot_tree_lookup(trans, snapshot_tree, &st);
133 
134 		bch2_fs_inconsistent_on(bch2_err_matches(ret, ENOENT), c,
135 				"%s: snapshot tree %u not found", __func__, snapshot_tree);
136 
137 		if (ret)
138 			goto err;
139 
140 		if (fsck_err_on(le32_to_cpu(st.master_subvol) != subvol.k->p.offset,
141 				trans, subvol_not_master_and_not_snapshot,
142 				"subvolume %llu is not set as snapshot but is not master subvolume",
143 				k.k->p.offset)) {
144 			struct bkey_i_subvolume *s =
145 				bch2_bkey_make_mut_typed(trans, iter, &subvol.s_c, 0, subvolume);
146 			ret = PTR_ERR_OR_ZERO(s);
147 			if (ret)
148 				goto err;
149 
150 			SET_BCH_SUBVOLUME_SNAP(&s->v, true);
151 		}
152 	}
153 err:
154 fsck_err:
155 	bch2_trans_iter_exit(trans, &subvol_children_iter);
156 	printbuf_exit(&buf);
157 	return ret;
158 }
159 
bch2_check_subvols(struct bch_fs * c)160 int bch2_check_subvols(struct bch_fs *c)
161 {
162 	int ret = bch2_trans_run(c,
163 		for_each_btree_key_commit(trans, iter,
164 				BTREE_ID_subvolumes, POS_MIN, BTREE_ITER_prefetch, k,
165 				NULL, NULL, BCH_TRANS_COMMIT_no_enospc,
166 			check_subvol(trans, &iter, k)));
167 	bch_err_fn(c, ret);
168 	return ret;
169 }
170 
check_subvol_child(struct btree_trans * trans,struct btree_iter * child_iter,struct bkey_s_c child_k)171 static int check_subvol_child(struct btree_trans *trans,
172 			      struct btree_iter *child_iter,
173 			      struct bkey_s_c child_k)
174 {
175 	struct bch_subvolume s;
176 	int ret = bch2_bkey_get_val_typed(trans, BTREE_ID_subvolumes, POS(0, child_k.k->p.offset),
177 					  0, subvolume, &s);
178 	if (ret && !bch2_err_matches(ret, ENOENT))
179 		return ret;
180 
181 	if (fsck_err_on(ret ||
182 			le32_to_cpu(s.fs_path_parent) != child_k.k->p.inode,
183 			trans, subvol_children_bad,
184 			"incorrect entry in subvolume_children btree %llu:%llu",
185 			child_k.k->p.inode, child_k.k->p.offset)) {
186 		ret = bch2_btree_delete_at(trans, child_iter, 0);
187 		if (ret)
188 			goto err;
189 	}
190 err:
191 fsck_err:
192 	return ret;
193 }
194 
bch2_check_subvol_children(struct bch_fs * c)195 int bch2_check_subvol_children(struct bch_fs *c)
196 {
197 	int ret = bch2_trans_run(c,
198 		for_each_btree_key_commit(trans, iter,
199 				BTREE_ID_subvolume_children, POS_MIN, BTREE_ITER_prefetch, k,
200 				NULL, NULL, BCH_TRANS_COMMIT_no_enospc,
201 			check_subvol_child(trans, &iter, k)));
202 	bch_err_fn(c, ret);
203 	return 0;
204 }
205 
206 /* Subvolumes: */
207 
bch2_subvolume_validate(struct bch_fs * c,struct bkey_s_c k,enum bch_validate_flags flags)208 int bch2_subvolume_validate(struct bch_fs *c, struct bkey_s_c k,
209 			   enum bch_validate_flags flags)
210 {
211 	struct bkey_s_c_subvolume subvol = bkey_s_c_to_subvolume(k);
212 	int ret = 0;
213 
214 	bkey_fsck_err_on(bkey_lt(k.k->p, SUBVOL_POS_MIN) ||
215 			 bkey_gt(k.k->p, SUBVOL_POS_MAX),
216 			 c, subvol_pos_bad,
217 			 "invalid pos");
218 
219 	bkey_fsck_err_on(!subvol.v->snapshot,
220 			 c, subvol_snapshot_bad,
221 			 "invalid snapshot");
222 
223 	bkey_fsck_err_on(!subvol.v->inode,
224 			 c, subvol_inode_bad,
225 			 "invalid inode");
226 fsck_err:
227 	return ret;
228 }
229 
bch2_subvolume_to_text(struct printbuf * out,struct bch_fs * c,struct bkey_s_c k)230 void bch2_subvolume_to_text(struct printbuf *out, struct bch_fs *c,
231 			    struct bkey_s_c k)
232 {
233 	struct bkey_s_c_subvolume s = bkey_s_c_to_subvolume(k);
234 
235 	prt_printf(out, "root %llu snapshot id %u",
236 		   le64_to_cpu(s.v->inode),
237 		   le32_to_cpu(s.v->snapshot));
238 
239 	if (bkey_val_bytes(s.k) > offsetof(struct bch_subvolume, creation_parent)) {
240 		prt_printf(out, " creation_parent %u", le32_to_cpu(s.v->creation_parent));
241 		prt_printf(out, " fs_parent %u", le32_to_cpu(s.v->fs_path_parent));
242 	}
243 }
244 
subvolume_children_mod(struct btree_trans * trans,struct bpos pos,bool set)245 static int subvolume_children_mod(struct btree_trans *trans, struct bpos pos, bool set)
246 {
247 	return !bpos_eq(pos, POS_MIN)
248 		? bch2_btree_bit_mod(trans, BTREE_ID_subvolume_children, pos, set)
249 		: 0;
250 }
251 
bch2_subvolume_trigger(struct btree_trans * trans,enum btree_id btree_id,unsigned level,struct bkey_s_c old,struct bkey_s new,enum btree_iter_update_trigger_flags flags)252 int bch2_subvolume_trigger(struct btree_trans *trans,
253 			   enum btree_id btree_id, unsigned level,
254 			   struct bkey_s_c old, struct bkey_s new,
255 			   enum btree_iter_update_trigger_flags flags)
256 {
257 	if (flags & BTREE_TRIGGER_transactional) {
258 		struct bpos children_pos_old = subvolume_children_pos(old);
259 		struct bpos children_pos_new = subvolume_children_pos(new.s_c);
260 
261 		if (!bpos_eq(children_pos_old, children_pos_new)) {
262 			int ret = subvolume_children_mod(trans, children_pos_old, false) ?:
263 				  subvolume_children_mod(trans, children_pos_new, true);
264 			if (ret)
265 				return ret;
266 		}
267 	}
268 
269 	return 0;
270 }
271 
bch2_subvol_has_children(struct btree_trans * trans,u32 subvol)272 int bch2_subvol_has_children(struct btree_trans *trans, u32 subvol)
273 {
274 	struct btree_iter iter;
275 
276 	bch2_trans_iter_init(trans, &iter, BTREE_ID_subvolume_children, POS(subvol, 0), 0);
277 	struct bkey_s_c k = bch2_btree_iter_peek(&iter);
278 	bch2_trans_iter_exit(trans, &iter);
279 
280 	return bkey_err(k) ?: k.k && k.k->p.inode == subvol
281 		? -BCH_ERR_ENOTEMPTY_subvol_not_empty
282 		: 0;
283 }
284 
285 static __always_inline int
bch2_subvolume_get_inlined(struct btree_trans * trans,unsigned subvol,bool inconsistent_if_not_found,int iter_flags,struct bch_subvolume * s)286 bch2_subvolume_get_inlined(struct btree_trans *trans, unsigned subvol,
287 			   bool inconsistent_if_not_found,
288 			   int iter_flags,
289 			   struct bch_subvolume *s)
290 {
291 	int ret = bch2_bkey_get_val_typed(trans, BTREE_ID_subvolumes, POS(0, subvol),
292 					  iter_flags, subvolume, s);
293 	bch2_fs_inconsistent_on(bch2_err_matches(ret, ENOENT) &&
294 				inconsistent_if_not_found,
295 				trans->c, "missing subvolume %u", subvol);
296 	return ret;
297 }
298 
bch2_subvolume_get(struct btree_trans * trans,unsigned subvol,bool inconsistent_if_not_found,int iter_flags,struct bch_subvolume * s)299 int bch2_subvolume_get(struct btree_trans *trans, unsigned subvol,
300 		       bool inconsistent_if_not_found,
301 		       int iter_flags,
302 		       struct bch_subvolume *s)
303 {
304 	return bch2_subvolume_get_inlined(trans, subvol, inconsistent_if_not_found, iter_flags, s);
305 }
306 
bch2_subvol_is_ro_trans(struct btree_trans * trans,u32 subvol)307 int bch2_subvol_is_ro_trans(struct btree_trans *trans, u32 subvol)
308 {
309 	struct bch_subvolume s;
310 	int ret = bch2_subvolume_get_inlined(trans, subvol, true, 0, &s);
311 	if (ret)
312 		return ret;
313 
314 	if (BCH_SUBVOLUME_RO(&s))
315 		return -EROFS;
316 	return 0;
317 }
318 
bch2_subvol_is_ro(struct bch_fs * c,u32 subvol)319 int bch2_subvol_is_ro(struct bch_fs *c, u32 subvol)
320 {
321 	return bch2_trans_do(c, NULL, NULL, 0,
322 		bch2_subvol_is_ro_trans(trans, subvol));
323 }
324 
bch2_snapshot_get_subvol(struct btree_trans * trans,u32 snapshot,struct bch_subvolume * subvol)325 int bch2_snapshot_get_subvol(struct btree_trans *trans, u32 snapshot,
326 			     struct bch_subvolume *subvol)
327 {
328 	struct bch_snapshot snap;
329 
330 	return  bch2_snapshot_lookup(trans, snapshot, &snap) ?:
331 		bch2_subvolume_get(trans, le32_to_cpu(snap.subvol), true, 0, subvol);
332 }
333 
bch2_subvolume_get_snapshot(struct btree_trans * trans,u32 subvolid,u32 * snapid)334 int bch2_subvolume_get_snapshot(struct btree_trans *trans, u32 subvolid,
335 				u32 *snapid)
336 {
337 	struct btree_iter iter;
338 	struct bkey_s_c_subvolume subvol;
339 	int ret;
340 
341 	subvol = bch2_bkey_get_iter_typed(trans, &iter,
342 					  BTREE_ID_subvolumes, POS(0, subvolid),
343 					  BTREE_ITER_cached|BTREE_ITER_with_updates,
344 					  subvolume);
345 	ret = bkey_err(subvol);
346 	bch2_fs_inconsistent_on(bch2_err_matches(ret, ENOENT), trans->c,
347 				"missing subvolume %u", subvolid);
348 
349 	if (likely(!ret))
350 		*snapid = le32_to_cpu(subvol.v->snapshot);
351 	bch2_trans_iter_exit(trans, &iter);
352 	return ret;
353 }
354 
bch2_subvolume_reparent(struct btree_trans * trans,struct btree_iter * iter,struct bkey_s_c k,u32 old_parent,u32 new_parent)355 static int bch2_subvolume_reparent(struct btree_trans *trans,
356 				   struct btree_iter *iter,
357 				   struct bkey_s_c k,
358 				   u32 old_parent, u32 new_parent)
359 {
360 	struct bkey_i_subvolume *s;
361 	int ret;
362 
363 	if (k.k->type != KEY_TYPE_subvolume)
364 		return 0;
365 
366 	if (bkey_val_bytes(k.k) > offsetof(struct bch_subvolume, creation_parent) &&
367 	    le32_to_cpu(bkey_s_c_to_subvolume(k).v->creation_parent) != old_parent)
368 		return 0;
369 
370 	s = bch2_bkey_make_mut_typed(trans, iter, &k, 0, subvolume);
371 	ret = PTR_ERR_OR_ZERO(s);
372 	if (ret)
373 		return ret;
374 
375 	s->v.creation_parent = cpu_to_le32(new_parent);
376 	return 0;
377 }
378 
379 /*
380  * Separate from the snapshot tree in the snapshots btree, we record the tree
381  * structure of how snapshot subvolumes were created - the parent subvolume of
382  * each snapshot subvolume.
383  *
384  * When a subvolume is deleted, we scan for child subvolumes and reparant them,
385  * to avoid dangling references:
386  */
bch2_subvolumes_reparent(struct btree_trans * trans,u32 subvolid_to_delete)387 static int bch2_subvolumes_reparent(struct btree_trans *trans, u32 subvolid_to_delete)
388 {
389 	struct bch_subvolume s;
390 
391 	return lockrestart_do(trans,
392 			bch2_subvolume_get(trans, subvolid_to_delete, true,
393 				   BTREE_ITER_cached, &s)) ?:
394 		for_each_btree_key_commit(trans, iter,
395 				BTREE_ID_subvolumes, POS_MIN, BTREE_ITER_prefetch, k,
396 				NULL, NULL, BCH_TRANS_COMMIT_no_enospc,
397 			bch2_subvolume_reparent(trans, &iter, k,
398 					subvolid_to_delete, le32_to_cpu(s.creation_parent)));
399 }
400 
401 /*
402  * Delete subvolume, mark snapshot ID as deleted, queue up snapshot
403  * deletion/cleanup:
404  */
__bch2_subvolume_delete(struct btree_trans * trans,u32 subvolid)405 static int __bch2_subvolume_delete(struct btree_trans *trans, u32 subvolid)
406 {
407 	struct btree_iter iter;
408 	struct bkey_s_c_subvolume subvol;
409 	u32 snapid;
410 	int ret = 0;
411 
412 	subvol = bch2_bkey_get_iter_typed(trans, &iter,
413 				BTREE_ID_subvolumes, POS(0, subvolid),
414 				BTREE_ITER_cached|BTREE_ITER_intent,
415 				subvolume);
416 	ret = bkey_err(subvol);
417 	bch2_fs_inconsistent_on(bch2_err_matches(ret, ENOENT), trans->c,
418 				"missing subvolume %u", subvolid);
419 	if (ret)
420 		return ret;
421 
422 	snapid = le32_to_cpu(subvol.v->snapshot);
423 
424 	ret =   bch2_btree_delete_at(trans, &iter, 0) ?:
425 		bch2_snapshot_node_set_deleted(trans, snapid);
426 	bch2_trans_iter_exit(trans, &iter);
427 	return ret;
428 }
429 
bch2_subvolume_delete(struct btree_trans * trans,u32 subvolid)430 static int bch2_subvolume_delete(struct btree_trans *trans, u32 subvolid)
431 {
432 	return bch2_subvolumes_reparent(trans, subvolid) ?:
433 		commit_do(trans, NULL, NULL, BCH_TRANS_COMMIT_no_enospc,
434 			  __bch2_subvolume_delete(trans, subvolid));
435 }
436 
bch2_subvolume_wait_for_pagecache_and_delete(struct work_struct * work)437 static void bch2_subvolume_wait_for_pagecache_and_delete(struct work_struct *work)
438 {
439 	struct bch_fs *c = container_of(work, struct bch_fs,
440 				snapshot_wait_for_pagecache_and_delete_work);
441 	snapshot_id_list s;
442 	u32 *id;
443 	int ret = 0;
444 
445 	while (!ret) {
446 		mutex_lock(&c->snapshots_unlinked_lock);
447 		s = c->snapshots_unlinked;
448 		darray_init(&c->snapshots_unlinked);
449 		mutex_unlock(&c->snapshots_unlinked_lock);
450 
451 		if (!s.nr)
452 			break;
453 
454 		bch2_evict_subvolume_inodes(c, &s);
455 
456 		for (id = s.data; id < s.data + s.nr; id++) {
457 			ret = bch2_trans_run(c, bch2_subvolume_delete(trans, *id));
458 			bch_err_msg(c, ret, "deleting subvolume %u", *id);
459 			if (ret)
460 				break;
461 		}
462 
463 		darray_exit(&s);
464 	}
465 
466 	bch2_write_ref_put(c, BCH_WRITE_REF_snapshot_delete_pagecache);
467 }
468 
469 struct subvolume_unlink_hook {
470 	struct btree_trans_commit_hook	h;
471 	u32				subvol;
472 };
473 
bch2_subvolume_wait_for_pagecache_and_delete_hook(struct btree_trans * trans,struct btree_trans_commit_hook * _h)474 static int bch2_subvolume_wait_for_pagecache_and_delete_hook(struct btree_trans *trans,
475 						      struct btree_trans_commit_hook *_h)
476 {
477 	struct subvolume_unlink_hook *h = container_of(_h, struct subvolume_unlink_hook, h);
478 	struct bch_fs *c = trans->c;
479 	int ret = 0;
480 
481 	mutex_lock(&c->snapshots_unlinked_lock);
482 	if (!snapshot_list_has_id(&c->snapshots_unlinked, h->subvol))
483 		ret = snapshot_list_add(c, &c->snapshots_unlinked, h->subvol);
484 	mutex_unlock(&c->snapshots_unlinked_lock);
485 
486 	if (ret)
487 		return ret;
488 
489 	if (!bch2_write_ref_tryget(c, BCH_WRITE_REF_snapshot_delete_pagecache))
490 		return -EROFS;
491 
492 	if (!queue_work(c->write_ref_wq, &c->snapshot_wait_for_pagecache_and_delete_work))
493 		bch2_write_ref_put(c, BCH_WRITE_REF_snapshot_delete_pagecache);
494 	return 0;
495 }
496 
bch2_subvolume_unlink(struct btree_trans * trans,u32 subvolid)497 int bch2_subvolume_unlink(struct btree_trans *trans, u32 subvolid)
498 {
499 	struct btree_iter iter;
500 	struct bkey_i_subvolume *n;
501 	struct subvolume_unlink_hook *h;
502 	int ret = 0;
503 
504 	h = bch2_trans_kmalloc(trans, sizeof(*h));
505 	ret = PTR_ERR_OR_ZERO(h);
506 	if (ret)
507 		return ret;
508 
509 	h->h.fn		= bch2_subvolume_wait_for_pagecache_and_delete_hook;
510 	h->subvol	= subvolid;
511 	bch2_trans_commit_hook(trans, &h->h);
512 
513 	n = bch2_bkey_get_mut_typed(trans, &iter,
514 			BTREE_ID_subvolumes, POS(0, subvolid),
515 			BTREE_ITER_cached, subvolume);
516 	ret = PTR_ERR_OR_ZERO(n);
517 	if (unlikely(ret)) {
518 		bch2_fs_inconsistent_on(bch2_err_matches(ret, ENOENT), trans->c,
519 					"missing subvolume %u", subvolid);
520 		return ret;
521 	}
522 
523 	SET_BCH_SUBVOLUME_UNLINKED(&n->v, true);
524 	bch2_trans_iter_exit(trans, &iter);
525 	return ret;
526 }
527 
bch2_subvolume_create(struct btree_trans * trans,u64 inode,u32 parent_subvolid,u32 src_subvolid,u32 * new_subvolid,u32 * new_snapshotid,bool ro)528 int bch2_subvolume_create(struct btree_trans *trans, u64 inode,
529 			  u32 parent_subvolid,
530 			  u32 src_subvolid,
531 			  u32 *new_subvolid,
532 			  u32 *new_snapshotid,
533 			  bool ro)
534 {
535 	struct bch_fs *c = trans->c;
536 	struct btree_iter dst_iter, src_iter = (struct btree_iter) { NULL };
537 	struct bkey_i_subvolume *new_subvol = NULL;
538 	struct bkey_i_subvolume *src_subvol = NULL;
539 	u32 parent = 0, new_nodes[2], snapshot_subvols[2];
540 	int ret = 0;
541 
542 	ret = bch2_bkey_get_empty_slot(trans, &dst_iter,
543 				BTREE_ID_subvolumes, POS(0, U32_MAX));
544 	if (ret == -BCH_ERR_ENOSPC_btree_slot)
545 		ret = -BCH_ERR_ENOSPC_subvolume_create;
546 	if (ret)
547 		return ret;
548 
549 	snapshot_subvols[0] = dst_iter.pos.offset;
550 	snapshot_subvols[1] = src_subvolid;
551 
552 	if (src_subvolid) {
553 		/* Creating a snapshot: */
554 
555 		src_subvol = bch2_bkey_get_mut_typed(trans, &src_iter,
556 				BTREE_ID_subvolumes, POS(0, src_subvolid),
557 				BTREE_ITER_cached, subvolume);
558 		ret = PTR_ERR_OR_ZERO(src_subvol);
559 		if (unlikely(ret)) {
560 			bch2_fs_inconsistent_on(bch2_err_matches(ret, ENOENT), c,
561 						"subvolume %u not found", src_subvolid);
562 			goto err;
563 		}
564 
565 		parent = le32_to_cpu(src_subvol->v.snapshot);
566 	}
567 
568 	ret = bch2_snapshot_node_create(trans, parent, new_nodes,
569 					snapshot_subvols,
570 					src_subvolid ? 2 : 1);
571 	if (ret)
572 		goto err;
573 
574 	if (src_subvolid) {
575 		src_subvol->v.snapshot = cpu_to_le32(new_nodes[1]);
576 		ret = bch2_trans_update(trans, &src_iter, &src_subvol->k_i, 0);
577 		if (ret)
578 			goto err;
579 	}
580 
581 	new_subvol = bch2_bkey_alloc(trans, &dst_iter, 0, subvolume);
582 	ret = PTR_ERR_OR_ZERO(new_subvol);
583 	if (ret)
584 		goto err;
585 
586 	new_subvol->v.flags		= 0;
587 	new_subvol->v.snapshot		= cpu_to_le32(new_nodes[0]);
588 	new_subvol->v.inode		= cpu_to_le64(inode);
589 	new_subvol->v.creation_parent	= cpu_to_le32(src_subvolid);
590 	new_subvol->v.fs_path_parent	= cpu_to_le32(parent_subvolid);
591 	new_subvol->v.otime.lo		= cpu_to_le64(bch2_current_time(c));
592 	new_subvol->v.otime.hi		= 0;
593 
594 	SET_BCH_SUBVOLUME_RO(&new_subvol->v, ro);
595 	SET_BCH_SUBVOLUME_SNAP(&new_subvol->v, src_subvolid != 0);
596 
597 	*new_subvolid	= new_subvol->k.p.offset;
598 	*new_snapshotid	= new_nodes[0];
599 err:
600 	bch2_trans_iter_exit(trans, &src_iter);
601 	bch2_trans_iter_exit(trans, &dst_iter);
602 	return ret;
603 }
604 
bch2_initialize_subvolumes(struct bch_fs * c)605 int bch2_initialize_subvolumes(struct bch_fs *c)
606 {
607 	struct bkey_i_snapshot_tree	root_tree;
608 	struct bkey_i_snapshot		root_snapshot;
609 	struct bkey_i_subvolume		root_volume;
610 	int ret;
611 
612 	bkey_snapshot_tree_init(&root_tree.k_i);
613 	root_tree.k.p.offset		= 1;
614 	root_tree.v.master_subvol	= cpu_to_le32(1);
615 	root_tree.v.root_snapshot	= cpu_to_le32(U32_MAX);
616 
617 	bkey_snapshot_init(&root_snapshot.k_i);
618 	root_snapshot.k.p.offset = U32_MAX;
619 	root_snapshot.v.flags	= 0;
620 	root_snapshot.v.parent	= 0;
621 	root_snapshot.v.subvol	= cpu_to_le32(BCACHEFS_ROOT_SUBVOL);
622 	root_snapshot.v.tree	= cpu_to_le32(1);
623 	SET_BCH_SNAPSHOT_SUBVOL(&root_snapshot.v, true);
624 
625 	bkey_subvolume_init(&root_volume.k_i);
626 	root_volume.k.p.offset = BCACHEFS_ROOT_SUBVOL;
627 	root_volume.v.flags	= 0;
628 	root_volume.v.snapshot	= cpu_to_le32(U32_MAX);
629 	root_volume.v.inode	= cpu_to_le64(BCACHEFS_ROOT_INO);
630 
631 	ret =   bch2_btree_insert(c, BTREE_ID_snapshot_trees,	&root_tree.k_i, NULL, 0, 0) ?:
632 		bch2_btree_insert(c, BTREE_ID_snapshots,	&root_snapshot.k_i, NULL, 0, 0) ?:
633 		bch2_btree_insert(c, BTREE_ID_subvolumes,	&root_volume.k_i, NULL, 0, 0);
634 	bch_err_fn(c, ret);
635 	return ret;
636 }
637 
__bch2_fs_upgrade_for_subvolumes(struct btree_trans * trans)638 static int __bch2_fs_upgrade_for_subvolumes(struct btree_trans *trans)
639 {
640 	struct btree_iter iter;
641 	struct bkey_s_c k;
642 	struct bch_inode_unpacked inode;
643 	int ret;
644 
645 	k = bch2_bkey_get_iter(trans, &iter, BTREE_ID_inodes,
646 			       SPOS(0, BCACHEFS_ROOT_INO, U32_MAX), 0);
647 	ret = bkey_err(k);
648 	if (ret)
649 		return ret;
650 
651 	if (!bkey_is_inode(k.k)) {
652 		bch_err(trans->c, "root inode not found");
653 		ret = -BCH_ERR_ENOENT_inode;
654 		goto err;
655 	}
656 
657 	ret = bch2_inode_unpack(k, &inode);
658 	BUG_ON(ret);
659 
660 	inode.bi_subvol = BCACHEFS_ROOT_SUBVOL;
661 
662 	ret = bch2_inode_write(trans, &iter, &inode);
663 err:
664 	bch2_trans_iter_exit(trans, &iter);
665 	return ret;
666 }
667 
668 /* set bi_subvol on root inode */
bch2_fs_upgrade_for_subvolumes(struct bch_fs * c)669 int bch2_fs_upgrade_for_subvolumes(struct bch_fs *c)
670 {
671 	int ret = bch2_trans_do(c, NULL, NULL, BCH_TRANS_COMMIT_lazy_rw,
672 				__bch2_fs_upgrade_for_subvolumes(trans));
673 	bch_err_fn(c, ret);
674 	return ret;
675 }
676 
bch2_fs_subvolumes_init(struct bch_fs * c)677 int bch2_fs_subvolumes_init(struct bch_fs *c)
678 {
679 	INIT_WORK(&c->snapshot_delete_work, bch2_delete_dead_snapshots_work);
680 	INIT_WORK(&c->snapshot_wait_for_pagecache_and_delete_work,
681 		  bch2_subvolume_wait_for_pagecache_and_delete);
682 	mutex_init(&c->snapshots_unlinked_lock);
683 	return 0;
684 }
685