162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Copyright (C) 2007 Oracle.  All rights reserved.
462306a36Sopenharmony_ci */
562306a36Sopenharmony_ci
662306a36Sopenharmony_ci#include <linux/err.h>
762306a36Sopenharmony_ci#include <linux/uuid.h>
862306a36Sopenharmony_ci#include "ctree.h"
962306a36Sopenharmony_ci#include "fs.h"
1062306a36Sopenharmony_ci#include "messages.h"
1162306a36Sopenharmony_ci#include "transaction.h"
1262306a36Sopenharmony_ci#include "disk-io.h"
1362306a36Sopenharmony_ci#include "print-tree.h"
1462306a36Sopenharmony_ci#include "qgroup.h"
1562306a36Sopenharmony_ci#include "space-info.h"
1662306a36Sopenharmony_ci#include "accessors.h"
1762306a36Sopenharmony_ci#include "root-tree.h"
1862306a36Sopenharmony_ci#include "orphan.h"
1962306a36Sopenharmony_ci
2062306a36Sopenharmony_ci/*
2162306a36Sopenharmony_ci * Read a root item from the tree. In case we detect a root item smaller then
2262306a36Sopenharmony_ci * sizeof(root_item), we know it's an old version of the root structure and
2362306a36Sopenharmony_ci * initialize all new fields to zero. The same happens if we detect mismatching
2462306a36Sopenharmony_ci * generation numbers as then we know the root was once mounted with an older
2562306a36Sopenharmony_ci * kernel that was not aware of the root item structure change.
2662306a36Sopenharmony_ci */
2762306a36Sopenharmony_cistatic void btrfs_read_root_item(struct extent_buffer *eb, int slot,
2862306a36Sopenharmony_ci				struct btrfs_root_item *item)
2962306a36Sopenharmony_ci{
3062306a36Sopenharmony_ci	u32 len;
3162306a36Sopenharmony_ci	int need_reset = 0;
3262306a36Sopenharmony_ci
3362306a36Sopenharmony_ci	len = btrfs_item_size(eb, slot);
3462306a36Sopenharmony_ci	read_extent_buffer(eb, item, btrfs_item_ptr_offset(eb, slot),
3562306a36Sopenharmony_ci			   min_t(u32, len, sizeof(*item)));
3662306a36Sopenharmony_ci	if (len < sizeof(*item))
3762306a36Sopenharmony_ci		need_reset = 1;
3862306a36Sopenharmony_ci	if (!need_reset && btrfs_root_generation(item)
3962306a36Sopenharmony_ci		!= btrfs_root_generation_v2(item)) {
4062306a36Sopenharmony_ci		if (btrfs_root_generation_v2(item) != 0) {
4162306a36Sopenharmony_ci			btrfs_warn(eb->fs_info,
4262306a36Sopenharmony_ci					"mismatching generation and generation_v2 found in root item. This root was probably mounted with an older kernel. Resetting all new fields.");
4362306a36Sopenharmony_ci		}
4462306a36Sopenharmony_ci		need_reset = 1;
4562306a36Sopenharmony_ci	}
4662306a36Sopenharmony_ci	if (need_reset) {
4762306a36Sopenharmony_ci		/* Clear all members from generation_v2 onwards. */
4862306a36Sopenharmony_ci		memset_startat(item, 0, generation_v2);
4962306a36Sopenharmony_ci		generate_random_guid(item->uuid);
5062306a36Sopenharmony_ci	}
5162306a36Sopenharmony_ci}
5262306a36Sopenharmony_ci
5362306a36Sopenharmony_ci/*
5462306a36Sopenharmony_ci * btrfs_find_root - lookup the root by the key.
5562306a36Sopenharmony_ci * root: the root of the root tree
5662306a36Sopenharmony_ci * search_key: the key to search
5762306a36Sopenharmony_ci * path: the path we search
5862306a36Sopenharmony_ci * root_item: the root item of the tree we look for
5962306a36Sopenharmony_ci * root_key: the root key of the tree we look for
6062306a36Sopenharmony_ci *
6162306a36Sopenharmony_ci * If ->offset of 'search_key' is -1ULL, it means we are not sure the offset
6262306a36Sopenharmony_ci * of the search key, just lookup the root with the highest offset for a
6362306a36Sopenharmony_ci * given objectid.
6462306a36Sopenharmony_ci *
6562306a36Sopenharmony_ci * If we find something return 0, otherwise > 0, < 0 on error.
6662306a36Sopenharmony_ci */
6762306a36Sopenharmony_ciint btrfs_find_root(struct btrfs_root *root, const struct btrfs_key *search_key,
6862306a36Sopenharmony_ci		    struct btrfs_path *path, struct btrfs_root_item *root_item,
6962306a36Sopenharmony_ci		    struct btrfs_key *root_key)
7062306a36Sopenharmony_ci{
7162306a36Sopenharmony_ci	struct btrfs_key found_key;
7262306a36Sopenharmony_ci	struct extent_buffer *l;
7362306a36Sopenharmony_ci	int ret;
7462306a36Sopenharmony_ci	int slot;
7562306a36Sopenharmony_ci
7662306a36Sopenharmony_ci	ret = btrfs_search_slot(NULL, root, search_key, path, 0, 0);
7762306a36Sopenharmony_ci	if (ret < 0)
7862306a36Sopenharmony_ci		return ret;
7962306a36Sopenharmony_ci
8062306a36Sopenharmony_ci	if (search_key->offset != -1ULL) {	/* the search key is exact */
8162306a36Sopenharmony_ci		if (ret > 0)
8262306a36Sopenharmony_ci			goto out;
8362306a36Sopenharmony_ci	} else {
8462306a36Sopenharmony_ci		BUG_ON(ret == 0);		/* Logical error */
8562306a36Sopenharmony_ci		if (path->slots[0] == 0)
8662306a36Sopenharmony_ci			goto out;
8762306a36Sopenharmony_ci		path->slots[0]--;
8862306a36Sopenharmony_ci		ret = 0;
8962306a36Sopenharmony_ci	}
9062306a36Sopenharmony_ci
9162306a36Sopenharmony_ci	l = path->nodes[0];
9262306a36Sopenharmony_ci	slot = path->slots[0];
9362306a36Sopenharmony_ci
9462306a36Sopenharmony_ci	btrfs_item_key_to_cpu(l, &found_key, slot);
9562306a36Sopenharmony_ci	if (found_key.objectid != search_key->objectid ||
9662306a36Sopenharmony_ci	    found_key.type != BTRFS_ROOT_ITEM_KEY) {
9762306a36Sopenharmony_ci		ret = 1;
9862306a36Sopenharmony_ci		goto out;
9962306a36Sopenharmony_ci	}
10062306a36Sopenharmony_ci
10162306a36Sopenharmony_ci	if (root_item)
10262306a36Sopenharmony_ci		btrfs_read_root_item(l, slot, root_item);
10362306a36Sopenharmony_ci	if (root_key)
10462306a36Sopenharmony_ci		memcpy(root_key, &found_key, sizeof(found_key));
10562306a36Sopenharmony_ciout:
10662306a36Sopenharmony_ci	btrfs_release_path(path);
10762306a36Sopenharmony_ci	return ret;
10862306a36Sopenharmony_ci}
10962306a36Sopenharmony_ci
11062306a36Sopenharmony_civoid btrfs_set_root_node(struct btrfs_root_item *item,
11162306a36Sopenharmony_ci			 struct extent_buffer *node)
11262306a36Sopenharmony_ci{
11362306a36Sopenharmony_ci	btrfs_set_root_bytenr(item, node->start);
11462306a36Sopenharmony_ci	btrfs_set_root_level(item, btrfs_header_level(node));
11562306a36Sopenharmony_ci	btrfs_set_root_generation(item, btrfs_header_generation(node));
11662306a36Sopenharmony_ci}
11762306a36Sopenharmony_ci
11862306a36Sopenharmony_ci/*
11962306a36Sopenharmony_ci * copy the data in 'item' into the btree
12062306a36Sopenharmony_ci */
12162306a36Sopenharmony_ciint btrfs_update_root(struct btrfs_trans_handle *trans, struct btrfs_root
12262306a36Sopenharmony_ci		      *root, struct btrfs_key *key, struct btrfs_root_item
12362306a36Sopenharmony_ci		      *item)
12462306a36Sopenharmony_ci{
12562306a36Sopenharmony_ci	struct btrfs_fs_info *fs_info = root->fs_info;
12662306a36Sopenharmony_ci	struct btrfs_path *path;
12762306a36Sopenharmony_ci	struct extent_buffer *l;
12862306a36Sopenharmony_ci	int ret;
12962306a36Sopenharmony_ci	int slot;
13062306a36Sopenharmony_ci	unsigned long ptr;
13162306a36Sopenharmony_ci	u32 old_len;
13262306a36Sopenharmony_ci
13362306a36Sopenharmony_ci	path = btrfs_alloc_path();
13462306a36Sopenharmony_ci	if (!path)
13562306a36Sopenharmony_ci		return -ENOMEM;
13662306a36Sopenharmony_ci
13762306a36Sopenharmony_ci	ret = btrfs_search_slot(trans, root, key, path, 0, 1);
13862306a36Sopenharmony_ci	if (ret < 0)
13962306a36Sopenharmony_ci		goto out;
14062306a36Sopenharmony_ci
14162306a36Sopenharmony_ci	if (ret > 0) {
14262306a36Sopenharmony_ci		btrfs_crit(fs_info,
14362306a36Sopenharmony_ci			"unable to find root key (%llu %u %llu) in tree %llu",
14462306a36Sopenharmony_ci			key->objectid, key->type, key->offset,
14562306a36Sopenharmony_ci			root->root_key.objectid);
14662306a36Sopenharmony_ci		ret = -EUCLEAN;
14762306a36Sopenharmony_ci		btrfs_abort_transaction(trans, ret);
14862306a36Sopenharmony_ci		goto out;
14962306a36Sopenharmony_ci	}
15062306a36Sopenharmony_ci
15162306a36Sopenharmony_ci	l = path->nodes[0];
15262306a36Sopenharmony_ci	slot = path->slots[0];
15362306a36Sopenharmony_ci	ptr = btrfs_item_ptr_offset(l, slot);
15462306a36Sopenharmony_ci	old_len = btrfs_item_size(l, slot);
15562306a36Sopenharmony_ci
15662306a36Sopenharmony_ci	/*
15762306a36Sopenharmony_ci	 * If this is the first time we update the root item which originated
15862306a36Sopenharmony_ci	 * from an older kernel, we need to enlarge the item size to make room
15962306a36Sopenharmony_ci	 * for the added fields.
16062306a36Sopenharmony_ci	 */
16162306a36Sopenharmony_ci	if (old_len < sizeof(*item)) {
16262306a36Sopenharmony_ci		btrfs_release_path(path);
16362306a36Sopenharmony_ci		ret = btrfs_search_slot(trans, root, key, path,
16462306a36Sopenharmony_ci				-1, 1);
16562306a36Sopenharmony_ci		if (ret < 0) {
16662306a36Sopenharmony_ci			btrfs_abort_transaction(trans, ret);
16762306a36Sopenharmony_ci			goto out;
16862306a36Sopenharmony_ci		}
16962306a36Sopenharmony_ci
17062306a36Sopenharmony_ci		ret = btrfs_del_item(trans, root, path);
17162306a36Sopenharmony_ci		if (ret < 0) {
17262306a36Sopenharmony_ci			btrfs_abort_transaction(trans, ret);
17362306a36Sopenharmony_ci			goto out;
17462306a36Sopenharmony_ci		}
17562306a36Sopenharmony_ci		btrfs_release_path(path);
17662306a36Sopenharmony_ci		ret = btrfs_insert_empty_item(trans, root, path,
17762306a36Sopenharmony_ci				key, sizeof(*item));
17862306a36Sopenharmony_ci		if (ret < 0) {
17962306a36Sopenharmony_ci			btrfs_abort_transaction(trans, ret);
18062306a36Sopenharmony_ci			goto out;
18162306a36Sopenharmony_ci		}
18262306a36Sopenharmony_ci		l = path->nodes[0];
18362306a36Sopenharmony_ci		slot = path->slots[0];
18462306a36Sopenharmony_ci		ptr = btrfs_item_ptr_offset(l, slot);
18562306a36Sopenharmony_ci	}
18662306a36Sopenharmony_ci
18762306a36Sopenharmony_ci	/*
18862306a36Sopenharmony_ci	 * Update generation_v2 so at the next mount we know the new root
18962306a36Sopenharmony_ci	 * fields are valid.
19062306a36Sopenharmony_ci	 */
19162306a36Sopenharmony_ci	btrfs_set_root_generation_v2(item, btrfs_root_generation(item));
19262306a36Sopenharmony_ci
19362306a36Sopenharmony_ci	write_extent_buffer(l, item, ptr, sizeof(*item));
19462306a36Sopenharmony_ci	btrfs_mark_buffer_dirty(trans, path->nodes[0]);
19562306a36Sopenharmony_ciout:
19662306a36Sopenharmony_ci	btrfs_free_path(path);
19762306a36Sopenharmony_ci	return ret;
19862306a36Sopenharmony_ci}
19962306a36Sopenharmony_ci
20062306a36Sopenharmony_ciint btrfs_insert_root(struct btrfs_trans_handle *trans, struct btrfs_root *root,
20162306a36Sopenharmony_ci		      const struct btrfs_key *key, struct btrfs_root_item *item)
20262306a36Sopenharmony_ci{
20362306a36Sopenharmony_ci	/*
20462306a36Sopenharmony_ci	 * Make sure generation v1 and v2 match. See update_root for details.
20562306a36Sopenharmony_ci	 */
20662306a36Sopenharmony_ci	btrfs_set_root_generation_v2(item, btrfs_root_generation(item));
20762306a36Sopenharmony_ci	return btrfs_insert_item(trans, root, key, item, sizeof(*item));
20862306a36Sopenharmony_ci}
20962306a36Sopenharmony_ci
21062306a36Sopenharmony_ciint btrfs_find_orphan_roots(struct btrfs_fs_info *fs_info)
21162306a36Sopenharmony_ci{
21262306a36Sopenharmony_ci	struct btrfs_root *tree_root = fs_info->tree_root;
21362306a36Sopenharmony_ci	struct extent_buffer *leaf;
21462306a36Sopenharmony_ci	struct btrfs_path *path;
21562306a36Sopenharmony_ci	struct btrfs_key key;
21662306a36Sopenharmony_ci	struct btrfs_root *root;
21762306a36Sopenharmony_ci	int err = 0;
21862306a36Sopenharmony_ci	int ret;
21962306a36Sopenharmony_ci
22062306a36Sopenharmony_ci	path = btrfs_alloc_path();
22162306a36Sopenharmony_ci	if (!path)
22262306a36Sopenharmony_ci		return -ENOMEM;
22362306a36Sopenharmony_ci
22462306a36Sopenharmony_ci	key.objectid = BTRFS_ORPHAN_OBJECTID;
22562306a36Sopenharmony_ci	key.type = BTRFS_ORPHAN_ITEM_KEY;
22662306a36Sopenharmony_ci	key.offset = 0;
22762306a36Sopenharmony_ci
22862306a36Sopenharmony_ci	while (1) {
22962306a36Sopenharmony_ci		u64 root_objectid;
23062306a36Sopenharmony_ci
23162306a36Sopenharmony_ci		ret = btrfs_search_slot(NULL, tree_root, &key, path, 0, 0);
23262306a36Sopenharmony_ci		if (ret < 0) {
23362306a36Sopenharmony_ci			err = ret;
23462306a36Sopenharmony_ci			break;
23562306a36Sopenharmony_ci		}
23662306a36Sopenharmony_ci
23762306a36Sopenharmony_ci		leaf = path->nodes[0];
23862306a36Sopenharmony_ci		if (path->slots[0] >= btrfs_header_nritems(leaf)) {
23962306a36Sopenharmony_ci			ret = btrfs_next_leaf(tree_root, path);
24062306a36Sopenharmony_ci			if (ret < 0)
24162306a36Sopenharmony_ci				err = ret;
24262306a36Sopenharmony_ci			if (ret != 0)
24362306a36Sopenharmony_ci				break;
24462306a36Sopenharmony_ci			leaf = path->nodes[0];
24562306a36Sopenharmony_ci		}
24662306a36Sopenharmony_ci
24762306a36Sopenharmony_ci		btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
24862306a36Sopenharmony_ci		btrfs_release_path(path);
24962306a36Sopenharmony_ci
25062306a36Sopenharmony_ci		if (key.objectid != BTRFS_ORPHAN_OBJECTID ||
25162306a36Sopenharmony_ci		    key.type != BTRFS_ORPHAN_ITEM_KEY)
25262306a36Sopenharmony_ci			break;
25362306a36Sopenharmony_ci
25462306a36Sopenharmony_ci		root_objectid = key.offset;
25562306a36Sopenharmony_ci		key.offset++;
25662306a36Sopenharmony_ci
25762306a36Sopenharmony_ci		root = btrfs_get_fs_root(fs_info, root_objectid, false);
25862306a36Sopenharmony_ci		err = PTR_ERR_OR_ZERO(root);
25962306a36Sopenharmony_ci		if (err && err != -ENOENT) {
26062306a36Sopenharmony_ci			break;
26162306a36Sopenharmony_ci		} else if (err == -ENOENT) {
26262306a36Sopenharmony_ci			struct btrfs_trans_handle *trans;
26362306a36Sopenharmony_ci
26462306a36Sopenharmony_ci			btrfs_release_path(path);
26562306a36Sopenharmony_ci
26662306a36Sopenharmony_ci			trans = btrfs_join_transaction(tree_root);
26762306a36Sopenharmony_ci			if (IS_ERR(trans)) {
26862306a36Sopenharmony_ci				err = PTR_ERR(trans);
26962306a36Sopenharmony_ci				btrfs_handle_fs_error(fs_info, err,
27062306a36Sopenharmony_ci					    "Failed to start trans to delete orphan item");
27162306a36Sopenharmony_ci				break;
27262306a36Sopenharmony_ci			}
27362306a36Sopenharmony_ci			err = btrfs_del_orphan_item(trans, tree_root,
27462306a36Sopenharmony_ci						    root_objectid);
27562306a36Sopenharmony_ci			btrfs_end_transaction(trans);
27662306a36Sopenharmony_ci			if (err) {
27762306a36Sopenharmony_ci				btrfs_handle_fs_error(fs_info, err,
27862306a36Sopenharmony_ci					    "Failed to delete root orphan item");
27962306a36Sopenharmony_ci				break;
28062306a36Sopenharmony_ci			}
28162306a36Sopenharmony_ci			continue;
28262306a36Sopenharmony_ci		}
28362306a36Sopenharmony_ci
28462306a36Sopenharmony_ci		WARN_ON(!test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state));
28562306a36Sopenharmony_ci		if (btrfs_root_refs(&root->root_item) == 0) {
28662306a36Sopenharmony_ci			struct btrfs_key drop_key;
28762306a36Sopenharmony_ci
28862306a36Sopenharmony_ci			btrfs_disk_key_to_cpu(&drop_key, &root->root_item.drop_progress);
28962306a36Sopenharmony_ci			/*
29062306a36Sopenharmony_ci			 * If we have a non-zero drop_progress then we know we
29162306a36Sopenharmony_ci			 * made it partly through deleting this snapshot, and
29262306a36Sopenharmony_ci			 * thus we need to make sure we block any balance from
29362306a36Sopenharmony_ci			 * happening until this snapshot is completely dropped.
29462306a36Sopenharmony_ci			 */
29562306a36Sopenharmony_ci			if (drop_key.objectid != 0 || drop_key.type != 0 ||
29662306a36Sopenharmony_ci			    drop_key.offset != 0) {
29762306a36Sopenharmony_ci				set_bit(BTRFS_FS_UNFINISHED_DROPS, &fs_info->flags);
29862306a36Sopenharmony_ci				set_bit(BTRFS_ROOT_UNFINISHED_DROP, &root->state);
29962306a36Sopenharmony_ci			}
30062306a36Sopenharmony_ci
30162306a36Sopenharmony_ci			set_bit(BTRFS_ROOT_DEAD_TREE, &root->state);
30262306a36Sopenharmony_ci			btrfs_add_dead_root(root);
30362306a36Sopenharmony_ci		}
30462306a36Sopenharmony_ci		btrfs_put_root(root);
30562306a36Sopenharmony_ci	}
30662306a36Sopenharmony_ci
30762306a36Sopenharmony_ci	btrfs_free_path(path);
30862306a36Sopenharmony_ci	return err;
30962306a36Sopenharmony_ci}
31062306a36Sopenharmony_ci
31162306a36Sopenharmony_ci/* drop the root item for 'key' from the tree root */
31262306a36Sopenharmony_ciint btrfs_del_root(struct btrfs_trans_handle *trans,
31362306a36Sopenharmony_ci		   const struct btrfs_key *key)
31462306a36Sopenharmony_ci{
31562306a36Sopenharmony_ci	struct btrfs_root *root = trans->fs_info->tree_root;
31662306a36Sopenharmony_ci	struct btrfs_path *path;
31762306a36Sopenharmony_ci	int ret;
31862306a36Sopenharmony_ci
31962306a36Sopenharmony_ci	path = btrfs_alloc_path();
32062306a36Sopenharmony_ci	if (!path)
32162306a36Sopenharmony_ci		return -ENOMEM;
32262306a36Sopenharmony_ci	ret = btrfs_search_slot(trans, root, key, path, -1, 1);
32362306a36Sopenharmony_ci	if (ret < 0)
32462306a36Sopenharmony_ci		goto out;
32562306a36Sopenharmony_ci
32662306a36Sopenharmony_ci	BUG_ON(ret != 0);
32762306a36Sopenharmony_ci
32862306a36Sopenharmony_ci	ret = btrfs_del_item(trans, root, path);
32962306a36Sopenharmony_ciout:
33062306a36Sopenharmony_ci	btrfs_free_path(path);
33162306a36Sopenharmony_ci	return ret;
33262306a36Sopenharmony_ci}
33362306a36Sopenharmony_ci
33462306a36Sopenharmony_ciint btrfs_del_root_ref(struct btrfs_trans_handle *trans, u64 root_id,
33562306a36Sopenharmony_ci		       u64 ref_id, u64 dirid, u64 *sequence,
33662306a36Sopenharmony_ci		       const struct fscrypt_str *name)
33762306a36Sopenharmony_ci{
33862306a36Sopenharmony_ci	struct btrfs_root *tree_root = trans->fs_info->tree_root;
33962306a36Sopenharmony_ci	struct btrfs_path *path;
34062306a36Sopenharmony_ci	struct btrfs_root_ref *ref;
34162306a36Sopenharmony_ci	struct extent_buffer *leaf;
34262306a36Sopenharmony_ci	struct btrfs_key key;
34362306a36Sopenharmony_ci	unsigned long ptr;
34462306a36Sopenharmony_ci	int ret;
34562306a36Sopenharmony_ci
34662306a36Sopenharmony_ci	path = btrfs_alloc_path();
34762306a36Sopenharmony_ci	if (!path)
34862306a36Sopenharmony_ci		return -ENOMEM;
34962306a36Sopenharmony_ci
35062306a36Sopenharmony_ci	key.objectid = root_id;
35162306a36Sopenharmony_ci	key.type = BTRFS_ROOT_BACKREF_KEY;
35262306a36Sopenharmony_ci	key.offset = ref_id;
35362306a36Sopenharmony_ciagain:
35462306a36Sopenharmony_ci	ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1);
35562306a36Sopenharmony_ci	if (ret < 0) {
35662306a36Sopenharmony_ci		goto out;
35762306a36Sopenharmony_ci	} else if (ret == 0) {
35862306a36Sopenharmony_ci		leaf = path->nodes[0];
35962306a36Sopenharmony_ci		ref = btrfs_item_ptr(leaf, path->slots[0],
36062306a36Sopenharmony_ci				     struct btrfs_root_ref);
36162306a36Sopenharmony_ci		ptr = (unsigned long)(ref + 1);
36262306a36Sopenharmony_ci		if ((btrfs_root_ref_dirid(leaf, ref) != dirid) ||
36362306a36Sopenharmony_ci		    (btrfs_root_ref_name_len(leaf, ref) != name->len) ||
36462306a36Sopenharmony_ci		    memcmp_extent_buffer(leaf, name->name, ptr, name->len)) {
36562306a36Sopenharmony_ci			ret = -ENOENT;
36662306a36Sopenharmony_ci			goto out;
36762306a36Sopenharmony_ci		}
36862306a36Sopenharmony_ci		*sequence = btrfs_root_ref_sequence(leaf, ref);
36962306a36Sopenharmony_ci
37062306a36Sopenharmony_ci		ret = btrfs_del_item(trans, tree_root, path);
37162306a36Sopenharmony_ci		if (ret)
37262306a36Sopenharmony_ci			goto out;
37362306a36Sopenharmony_ci	} else {
37462306a36Sopenharmony_ci		ret = -ENOENT;
37562306a36Sopenharmony_ci		goto out;
37662306a36Sopenharmony_ci	}
37762306a36Sopenharmony_ci
37862306a36Sopenharmony_ci	if (key.type == BTRFS_ROOT_BACKREF_KEY) {
37962306a36Sopenharmony_ci		btrfs_release_path(path);
38062306a36Sopenharmony_ci		key.objectid = ref_id;
38162306a36Sopenharmony_ci		key.type = BTRFS_ROOT_REF_KEY;
38262306a36Sopenharmony_ci		key.offset = root_id;
38362306a36Sopenharmony_ci		goto again;
38462306a36Sopenharmony_ci	}
38562306a36Sopenharmony_ci
38662306a36Sopenharmony_ciout:
38762306a36Sopenharmony_ci	btrfs_free_path(path);
38862306a36Sopenharmony_ci	return ret;
38962306a36Sopenharmony_ci}
39062306a36Sopenharmony_ci
39162306a36Sopenharmony_ci/*
39262306a36Sopenharmony_ci * add a btrfs_root_ref item.  type is either BTRFS_ROOT_REF_KEY
39362306a36Sopenharmony_ci * or BTRFS_ROOT_BACKREF_KEY.
39462306a36Sopenharmony_ci *
39562306a36Sopenharmony_ci * The dirid, sequence, name and name_len refer to the directory entry
39662306a36Sopenharmony_ci * that is referencing the root.
39762306a36Sopenharmony_ci *
39862306a36Sopenharmony_ci * For a forward ref, the root_id is the id of the tree referencing
39962306a36Sopenharmony_ci * the root and ref_id is the id of the subvol  or snapshot.
40062306a36Sopenharmony_ci *
40162306a36Sopenharmony_ci * For a back ref the root_id is the id of the subvol or snapshot and
40262306a36Sopenharmony_ci * ref_id is the id of the tree referencing it.
40362306a36Sopenharmony_ci *
40462306a36Sopenharmony_ci * Will return 0, -ENOMEM, or anything from the CoW path
40562306a36Sopenharmony_ci */
40662306a36Sopenharmony_ciint btrfs_add_root_ref(struct btrfs_trans_handle *trans, u64 root_id,
40762306a36Sopenharmony_ci		       u64 ref_id, u64 dirid, u64 sequence,
40862306a36Sopenharmony_ci		       const struct fscrypt_str *name)
40962306a36Sopenharmony_ci{
41062306a36Sopenharmony_ci	struct btrfs_root *tree_root = trans->fs_info->tree_root;
41162306a36Sopenharmony_ci	struct btrfs_key key;
41262306a36Sopenharmony_ci	int ret;
41362306a36Sopenharmony_ci	struct btrfs_path *path;
41462306a36Sopenharmony_ci	struct btrfs_root_ref *ref;
41562306a36Sopenharmony_ci	struct extent_buffer *leaf;
41662306a36Sopenharmony_ci	unsigned long ptr;
41762306a36Sopenharmony_ci
41862306a36Sopenharmony_ci	path = btrfs_alloc_path();
41962306a36Sopenharmony_ci	if (!path)
42062306a36Sopenharmony_ci		return -ENOMEM;
42162306a36Sopenharmony_ci
42262306a36Sopenharmony_ci	key.objectid = root_id;
42362306a36Sopenharmony_ci	key.type = BTRFS_ROOT_BACKREF_KEY;
42462306a36Sopenharmony_ci	key.offset = ref_id;
42562306a36Sopenharmony_ciagain:
42662306a36Sopenharmony_ci	ret = btrfs_insert_empty_item(trans, tree_root, path, &key,
42762306a36Sopenharmony_ci				      sizeof(*ref) + name->len);
42862306a36Sopenharmony_ci	if (ret) {
42962306a36Sopenharmony_ci		btrfs_abort_transaction(trans, ret);
43062306a36Sopenharmony_ci		btrfs_free_path(path);
43162306a36Sopenharmony_ci		return ret;
43262306a36Sopenharmony_ci	}
43362306a36Sopenharmony_ci
43462306a36Sopenharmony_ci	leaf = path->nodes[0];
43562306a36Sopenharmony_ci	ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
43662306a36Sopenharmony_ci	btrfs_set_root_ref_dirid(leaf, ref, dirid);
43762306a36Sopenharmony_ci	btrfs_set_root_ref_sequence(leaf, ref, sequence);
43862306a36Sopenharmony_ci	btrfs_set_root_ref_name_len(leaf, ref, name->len);
43962306a36Sopenharmony_ci	ptr = (unsigned long)(ref + 1);
44062306a36Sopenharmony_ci	write_extent_buffer(leaf, name->name, ptr, name->len);
44162306a36Sopenharmony_ci	btrfs_mark_buffer_dirty(trans, leaf);
44262306a36Sopenharmony_ci
44362306a36Sopenharmony_ci	if (key.type == BTRFS_ROOT_BACKREF_KEY) {
44462306a36Sopenharmony_ci		btrfs_release_path(path);
44562306a36Sopenharmony_ci		key.objectid = ref_id;
44662306a36Sopenharmony_ci		key.type = BTRFS_ROOT_REF_KEY;
44762306a36Sopenharmony_ci		key.offset = root_id;
44862306a36Sopenharmony_ci		goto again;
44962306a36Sopenharmony_ci	}
45062306a36Sopenharmony_ci
45162306a36Sopenharmony_ci	btrfs_free_path(path);
45262306a36Sopenharmony_ci	return 0;
45362306a36Sopenharmony_ci}
45462306a36Sopenharmony_ci
45562306a36Sopenharmony_ci/*
45662306a36Sopenharmony_ci * Old btrfs forgets to init root_item->flags and root_item->byte_limit
45762306a36Sopenharmony_ci * for subvolumes. To work around this problem, we steal a bit from
45862306a36Sopenharmony_ci * root_item->inode_item->flags, and use it to indicate if those fields
45962306a36Sopenharmony_ci * have been properly initialized.
46062306a36Sopenharmony_ci */
46162306a36Sopenharmony_civoid btrfs_check_and_init_root_item(struct btrfs_root_item *root_item)
46262306a36Sopenharmony_ci{
46362306a36Sopenharmony_ci	u64 inode_flags = btrfs_stack_inode_flags(&root_item->inode);
46462306a36Sopenharmony_ci
46562306a36Sopenharmony_ci	if (!(inode_flags & BTRFS_INODE_ROOT_ITEM_INIT)) {
46662306a36Sopenharmony_ci		inode_flags |= BTRFS_INODE_ROOT_ITEM_INIT;
46762306a36Sopenharmony_ci		btrfs_set_stack_inode_flags(&root_item->inode, inode_flags);
46862306a36Sopenharmony_ci		btrfs_set_root_flags(root_item, 0);
46962306a36Sopenharmony_ci		btrfs_set_root_limit(root_item, 0);
47062306a36Sopenharmony_ci	}
47162306a36Sopenharmony_ci}
47262306a36Sopenharmony_ci
47362306a36Sopenharmony_civoid btrfs_update_root_times(struct btrfs_trans_handle *trans,
47462306a36Sopenharmony_ci			     struct btrfs_root *root)
47562306a36Sopenharmony_ci{
47662306a36Sopenharmony_ci	struct btrfs_root_item *item = &root->root_item;
47762306a36Sopenharmony_ci	struct timespec64 ct;
47862306a36Sopenharmony_ci
47962306a36Sopenharmony_ci	ktime_get_real_ts64(&ct);
48062306a36Sopenharmony_ci	spin_lock(&root->root_item_lock);
48162306a36Sopenharmony_ci	btrfs_set_root_ctransid(item, trans->transid);
48262306a36Sopenharmony_ci	btrfs_set_stack_timespec_sec(&item->ctime, ct.tv_sec);
48362306a36Sopenharmony_ci	btrfs_set_stack_timespec_nsec(&item->ctime, ct.tv_nsec);
48462306a36Sopenharmony_ci	spin_unlock(&root->root_item_lock);
48562306a36Sopenharmony_ci}
48662306a36Sopenharmony_ci
48762306a36Sopenharmony_ci/*
48862306a36Sopenharmony_ci * btrfs_subvolume_reserve_metadata() - reserve space for subvolume operation
48962306a36Sopenharmony_ci * root: the root of the parent directory
49062306a36Sopenharmony_ci * rsv: block reservation
49162306a36Sopenharmony_ci * items: the number of items that we need do reservation
49262306a36Sopenharmony_ci * use_global_rsv: allow fallback to the global block reservation
49362306a36Sopenharmony_ci *
49462306a36Sopenharmony_ci * This function is used to reserve the space for snapshot/subvolume
49562306a36Sopenharmony_ci * creation and deletion. Those operations are different with the
49662306a36Sopenharmony_ci * common file/directory operations, they change two fs/file trees
49762306a36Sopenharmony_ci * and root tree, the number of items that the qgroup reserves is
49862306a36Sopenharmony_ci * different with the free space reservation. So we can not use
49962306a36Sopenharmony_ci * the space reservation mechanism in start_transaction().
50062306a36Sopenharmony_ci */
50162306a36Sopenharmony_ciint btrfs_subvolume_reserve_metadata(struct btrfs_root *root,
50262306a36Sopenharmony_ci				     struct btrfs_block_rsv *rsv, int items,
50362306a36Sopenharmony_ci				     bool use_global_rsv)
50462306a36Sopenharmony_ci{
50562306a36Sopenharmony_ci	u64 qgroup_num_bytes = 0;
50662306a36Sopenharmony_ci	u64 num_bytes;
50762306a36Sopenharmony_ci	int ret;
50862306a36Sopenharmony_ci	struct btrfs_fs_info *fs_info = root->fs_info;
50962306a36Sopenharmony_ci	struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
51062306a36Sopenharmony_ci
51162306a36Sopenharmony_ci	if (test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags)) {
51262306a36Sopenharmony_ci		/* One for parent inode, two for dir entries */
51362306a36Sopenharmony_ci		qgroup_num_bytes = 3 * fs_info->nodesize;
51462306a36Sopenharmony_ci		ret = btrfs_qgroup_reserve_meta_prealloc(root,
51562306a36Sopenharmony_ci							 qgroup_num_bytes, true,
51662306a36Sopenharmony_ci							 false);
51762306a36Sopenharmony_ci		if (ret)
51862306a36Sopenharmony_ci			return ret;
51962306a36Sopenharmony_ci	}
52062306a36Sopenharmony_ci
52162306a36Sopenharmony_ci	num_bytes = btrfs_calc_insert_metadata_size(fs_info, items);
52262306a36Sopenharmony_ci	rsv->space_info = btrfs_find_space_info(fs_info,
52362306a36Sopenharmony_ci					    BTRFS_BLOCK_GROUP_METADATA);
52462306a36Sopenharmony_ci	ret = btrfs_block_rsv_add(fs_info, rsv, num_bytes,
52562306a36Sopenharmony_ci				  BTRFS_RESERVE_FLUSH_ALL);
52662306a36Sopenharmony_ci
52762306a36Sopenharmony_ci	if (ret == -ENOSPC && use_global_rsv)
52862306a36Sopenharmony_ci		ret = btrfs_block_rsv_migrate(global_rsv, rsv, num_bytes, true);
52962306a36Sopenharmony_ci
53062306a36Sopenharmony_ci	if (ret && qgroup_num_bytes)
53162306a36Sopenharmony_ci		btrfs_qgroup_free_meta_prealloc(root, qgroup_num_bytes);
53262306a36Sopenharmony_ci
53362306a36Sopenharmony_ci	if (!ret) {
53462306a36Sopenharmony_ci		spin_lock(&rsv->lock);
53562306a36Sopenharmony_ci		rsv->qgroup_rsv_reserved += qgroup_num_bytes;
53662306a36Sopenharmony_ci		spin_unlock(&rsv->lock);
53762306a36Sopenharmony_ci	}
53862306a36Sopenharmony_ci	return ret;
53962306a36Sopenharmony_ci}
54062306a36Sopenharmony_ci
54162306a36Sopenharmony_civoid btrfs_subvolume_release_metadata(struct btrfs_root *root,
54262306a36Sopenharmony_ci				      struct btrfs_block_rsv *rsv)
54362306a36Sopenharmony_ci{
54462306a36Sopenharmony_ci	struct btrfs_fs_info *fs_info = root->fs_info;
54562306a36Sopenharmony_ci	u64 qgroup_to_release;
54662306a36Sopenharmony_ci
54762306a36Sopenharmony_ci	btrfs_block_rsv_release(fs_info, rsv, (u64)-1, &qgroup_to_release);
54862306a36Sopenharmony_ci	btrfs_qgroup_convert_reserved_meta(root, qgroup_to_release);
54962306a36Sopenharmony_ci}
550