xref: /openbmc/linux/fs/btrfs/root-tree.c (revision 14431815)
1c1d7c514SDavid Sterba // SPDX-License-Identifier: GPL-2.0
26cbd5570SChris Mason /*
36cbd5570SChris Mason  * Copyright (C) 2007 Oracle.  All rights reserved.
46cbd5570SChris Mason  */
56cbd5570SChris Mason 
6886322e8SSachin Kamat #include <linux/err.h>
78ea05e3aSAlexander Block #include <linux/uuid.h>
83768f368SChris Mason #include "ctree.h"
9ec8eb376SJosef Bacik #include "fs.h"
10ec8eb376SJosef Bacik #include "messages.h"
115eda7b5eSChris Mason #include "transaction.h"
123768f368SChris Mason #include "disk-io.h"
133768f368SChris Mason #include "print-tree.h"
1428a32d2bSJosef Bacik #include "qgroup.h"
1528a32d2bSJosef Bacik #include "space-info.h"
1607e81dc9SJosef Bacik #include "accessors.h"
1745c40c8fSJosef Bacik #include "root-tree.h"
18aa5d3003SJosef Bacik #include "orphan.h"
193768f368SChris Mason 
20bf4ef679SChris Mason /*
218ea05e3aSAlexander Block  * Read a root item from the tree. In case we detect a root item smaller then
228ea05e3aSAlexander Block  * sizeof(root_item), we know it's an old version of the root structure and
238ea05e3aSAlexander Block  * initialize all new fields to zero. The same happens if we detect mismatching
248ea05e3aSAlexander Block  * generation numbers as then we know the root was once mounted with an older
258ea05e3aSAlexander Block  * kernel that was not aware of the root item structure change.
268ea05e3aSAlexander Block  */
btrfs_read_root_item(struct extent_buffer * eb,int slot,struct btrfs_root_item * item)27171170c1SSergei Trofimovich static void btrfs_read_root_item(struct extent_buffer *eb, int slot,
288ea05e3aSAlexander Block 				struct btrfs_root_item *item)
298ea05e3aSAlexander Block {
30f65e25e3SYueHaibing 	u32 len;
318ea05e3aSAlexander Block 	int need_reset = 0;
328ea05e3aSAlexander Block 
333212fa14SJosef Bacik 	len = btrfs_item_size(eb, slot);
348ea05e3aSAlexander Block 	read_extent_buffer(eb, item, btrfs_item_ptr_offset(eb, slot),
35f65e25e3SYueHaibing 			   min_t(u32, len, sizeof(*item)));
368ea05e3aSAlexander Block 	if (len < sizeof(*item))
378ea05e3aSAlexander Block 		need_reset = 1;
388ea05e3aSAlexander Block 	if (!need_reset && btrfs_root_generation(item)
398ea05e3aSAlexander Block 		!= btrfs_root_generation_v2(item)) {
408ea05e3aSAlexander Block 		if (btrfs_root_generation_v2(item) != 0) {
41f14d104dSDavid Sterba 			btrfs_warn(eb->fs_info,
425d163e0eSJeff Mahoney 					"mismatching generation and generation_v2 found in root item. This root was probably mounted with an older kernel. Resetting all new fields.");
438ea05e3aSAlexander Block 		}
448ea05e3aSAlexander Block 		need_reset = 1;
458ea05e3aSAlexander Block 	}
468ea05e3aSAlexander Block 	if (need_reset) {
47a2c5062fSKees Cook 		/* Clear all members from generation_v2 onwards. */
48a2c5062fSKees Cook 		memset_startat(item, 0, generation_v2);
49807fc790SAndy Shevchenko 		generate_random_guid(item->uuid);
508ea05e3aSAlexander Block 	}
518ea05e3aSAlexander Block }
528ea05e3aSAlexander Block 
538ea05e3aSAlexander Block /*
54cb517eabSMiao Xie  * btrfs_find_root - lookup the root by the key.
55cb517eabSMiao Xie  * root: the root of the root tree
56cb517eabSMiao Xie  * search_key: the key to search
57cb517eabSMiao Xie  * path: the path we search
58cb517eabSMiao Xie  * root_item: the root item of the tree we look for
5901327610SNicholas D Steeves  * root_key: the root key of the tree we look for
60cb517eabSMiao Xie  *
6101327610SNicholas D Steeves  * If ->offset of 'search_key' is -1ULL, it means we are not sure the offset
62cb517eabSMiao Xie  * of the search key, just lookup the root with the highest offset for a
63cb517eabSMiao Xie  * given objectid.
64cb517eabSMiao Xie  *
65cb517eabSMiao Xie  * If we find something return 0, otherwise > 0, < 0 on error.
66d352ac68SChris Mason  */
btrfs_find_root(struct btrfs_root * root,const struct btrfs_key * search_key,struct btrfs_path * path,struct btrfs_root_item * root_item,struct btrfs_key * root_key)67310712b2SOmar Sandoval int btrfs_find_root(struct btrfs_root *root, const struct btrfs_key *search_key,
68cb517eabSMiao Xie 		    struct btrfs_path *path, struct btrfs_root_item *root_item,
69cb517eabSMiao Xie 		    struct btrfs_key *root_key)
703768f368SChris Mason {
715f39d397SChris Mason 	struct btrfs_key found_key;
725f39d397SChris Mason 	struct extent_buffer *l;
733768f368SChris Mason 	int ret;
743768f368SChris Mason 	int slot;
753768f368SChris Mason 
76cb517eabSMiao Xie 	ret = btrfs_search_slot(NULL, root, search_key, path, 0, 0);
773768f368SChris Mason 	if (ret < 0)
78cb517eabSMiao Xie 		return ret;
795f39d397SChris Mason 
80cb517eabSMiao Xie 	if (search_key->offset != -1ULL) {	/* the search key is exact */
81cb517eabSMiao Xie 		if (ret > 0)
823768f368SChris Mason 			goto out;
83cb517eabSMiao Xie 	} else {
84cb517eabSMiao Xie 		BUG_ON(ret == 0);		/* Logical error */
85cb517eabSMiao Xie 		if (path->slots[0] == 0)
86cb517eabSMiao Xie 			goto out;
87cb517eabSMiao Xie 		path->slots[0]--;
88cb517eabSMiao Xie 		ret = 0;
893768f368SChris Mason 	}
90cb517eabSMiao Xie 
9176dda93cSYan, Zheng 	l = path->nodes[0];
92cb517eabSMiao Xie 	slot = path->slots[0];
93cb517eabSMiao Xie 
9476dda93cSYan, Zheng 	btrfs_item_key_to_cpu(l, &found_key, slot);
95cb517eabSMiao Xie 	if (found_key.objectid != search_key->objectid ||
9676dda93cSYan, Zheng 	    found_key.type != BTRFS_ROOT_ITEM_KEY) {
9776dda93cSYan, Zheng 		ret = 1;
9876dda93cSYan, Zheng 		goto out;
9976dda93cSYan, Zheng 	}
1008ea05e3aSAlexander Block 
101cb517eabSMiao Xie 	if (root_item)
102cb517eabSMiao Xie 		btrfs_read_root_item(l, slot, root_item);
103cb517eabSMiao Xie 	if (root_key)
104cb517eabSMiao Xie 		memcpy(root_key, &found_key, sizeof(found_key));
1053768f368SChris Mason out:
106cb517eabSMiao Xie 	btrfs_release_path(path);
1073768f368SChris Mason 	return ret;
1083768f368SChris Mason }
1093768f368SChris Mason 
btrfs_set_root_node(struct btrfs_root_item * item,struct extent_buffer * node)110bf5f32ecSMark Fasheh void btrfs_set_root_node(struct btrfs_root_item *item,
1115d4f98a2SYan Zheng 			 struct extent_buffer *node)
1125d4f98a2SYan Zheng {
1135d4f98a2SYan Zheng 	btrfs_set_root_bytenr(item, node->start);
1145d4f98a2SYan Zheng 	btrfs_set_root_level(item, btrfs_header_level(node));
1155d4f98a2SYan Zheng 	btrfs_set_root_generation(item, btrfs_header_generation(node));
1165d4f98a2SYan Zheng }
1175d4f98a2SYan Zheng 
118d352ac68SChris Mason /*
119d352ac68SChris Mason  * copy the data in 'item' into the btree
120d352ac68SChris Mason  */
btrfs_update_root(struct btrfs_trans_handle * trans,struct btrfs_root * root,struct btrfs_key * key,struct btrfs_root_item * item)121e089f05cSChris Mason int btrfs_update_root(struct btrfs_trans_handle *trans, struct btrfs_root
122e089f05cSChris Mason 		      *root, struct btrfs_key *key, struct btrfs_root_item
123e089f05cSChris Mason 		      *item)
1243768f368SChris Mason {
1250b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = root->fs_info;
1265caf2a00SChris Mason 	struct btrfs_path *path;
1275f39d397SChris Mason 	struct extent_buffer *l;
1283768f368SChris Mason 	int ret;
1293768f368SChris Mason 	int slot;
1305f39d397SChris Mason 	unsigned long ptr;
1310412e58cSAlexandru Moise 	u32 old_len;
1323768f368SChris Mason 
1335caf2a00SChris Mason 	path = btrfs_alloc_path();
134b45a9d8bSJeff Mahoney 	if (!path)
135b45a9d8bSJeff Mahoney 		return -ENOMEM;
136b45a9d8bSJeff Mahoney 
1375caf2a00SChris Mason 	ret = btrfs_search_slot(trans, root, key, path, 0, 1);
13872bd2323SFilipe Manana 	if (ret < 0)
139005d6427SDavid Sterba 		goto out;
140d6667462SChris Mason 
1417ac1e464SQu Wenruo 	if (ret > 0) {
1427ac1e464SQu Wenruo 		btrfs_crit(fs_info,
1437ac1e464SQu Wenruo 			"unable to find root key (%llu %u %llu) in tree %llu",
1447ac1e464SQu Wenruo 			key->objectid, key->type, key->offset,
1457ac1e464SQu Wenruo 			root->root_key.objectid);
1467ac1e464SQu Wenruo 		ret = -EUCLEAN;
1477ac1e464SQu Wenruo 		btrfs_abort_transaction(trans, ret);
1487ac1e464SQu Wenruo 		goto out;
149d6667462SChris Mason 	}
150d6667462SChris Mason 
1515f39d397SChris Mason 	l = path->nodes[0];
1525caf2a00SChris Mason 	slot = path->slots[0];
1535f39d397SChris Mason 	ptr = btrfs_item_ptr_offset(l, slot);
1543212fa14SJosef Bacik 	old_len = btrfs_item_size(l, slot);
1558ea05e3aSAlexander Block 
1568ea05e3aSAlexander Block 	/*
1578ea05e3aSAlexander Block 	 * If this is the first time we update the root item which originated
1588ea05e3aSAlexander Block 	 * from an older kernel, we need to enlarge the item size to make room
1598ea05e3aSAlexander Block 	 * for the added fields.
1608ea05e3aSAlexander Block 	 */
1618ea05e3aSAlexander Block 	if (old_len < sizeof(*item)) {
1628ea05e3aSAlexander Block 		btrfs_release_path(path);
1638ea05e3aSAlexander Block 		ret = btrfs_search_slot(trans, root, key, path,
1648ea05e3aSAlexander Block 				-1, 1);
165005d6427SDavid Sterba 		if (ret < 0) {
16666642832SJeff Mahoney 			btrfs_abort_transaction(trans, ret);
167005d6427SDavid Sterba 			goto out;
168005d6427SDavid Sterba 		}
169005d6427SDavid Sterba 
1708ea05e3aSAlexander Block 		ret = btrfs_del_item(trans, root, path);
171005d6427SDavid Sterba 		if (ret < 0) {
17266642832SJeff Mahoney 			btrfs_abort_transaction(trans, ret);
173005d6427SDavid Sterba 			goto out;
174005d6427SDavid Sterba 		}
1758ea05e3aSAlexander Block 		btrfs_release_path(path);
1768ea05e3aSAlexander Block 		ret = btrfs_insert_empty_item(trans, root, path,
1778ea05e3aSAlexander Block 				key, sizeof(*item));
178005d6427SDavid Sterba 		if (ret < 0) {
17966642832SJeff Mahoney 			btrfs_abort_transaction(trans, ret);
180005d6427SDavid Sterba 			goto out;
181005d6427SDavid Sterba 		}
1828ea05e3aSAlexander Block 		l = path->nodes[0];
1838ea05e3aSAlexander Block 		slot = path->slots[0];
1848ea05e3aSAlexander Block 		ptr = btrfs_item_ptr_offset(l, slot);
1858ea05e3aSAlexander Block 	}
1868ea05e3aSAlexander Block 
1878ea05e3aSAlexander Block 	/*
1888ea05e3aSAlexander Block 	 * Update generation_v2 so at the next mount we know the new root
1898ea05e3aSAlexander Block 	 * fields are valid.
1908ea05e3aSAlexander Block 	 */
1918ea05e3aSAlexander Block 	btrfs_set_root_generation_v2(item, btrfs_root_generation(item));
1928ea05e3aSAlexander Block 
1935f39d397SChris Mason 	write_extent_buffer(l, item, ptr, sizeof(*item));
194*d5e09e38SFilipe Manana 	btrfs_mark_buffer_dirty(trans, path->nodes[0]);
1953768f368SChris Mason out:
1965caf2a00SChris Mason 	btrfs_free_path(path);
1973768f368SChris Mason 	return ret;
1983768f368SChris Mason }
1993768f368SChris Mason 
btrfs_insert_root(struct btrfs_trans_handle * trans,struct btrfs_root * root,const struct btrfs_key * key,struct btrfs_root_item * item)200d16cb050SJeff Mahoney int btrfs_insert_root(struct btrfs_trans_handle *trans, struct btrfs_root *root,
201310712b2SOmar Sandoval 		      const struct btrfs_key *key, struct btrfs_root_item *item)
2023768f368SChris Mason {
2038ea05e3aSAlexander Block 	/*
2048ea05e3aSAlexander Block 	 * Make sure generation v1 and v2 match. See update_root for details.
2058ea05e3aSAlexander Block 	 */
2068ea05e3aSAlexander Block 	btrfs_set_root_generation_v2(item, btrfs_root_generation(item));
207d16cb050SJeff Mahoney 	return btrfs_insert_item(trans, root, key, item, sizeof(*item));
2083768f368SChris Mason }
2093768f368SChris Mason 
btrfs_find_orphan_roots(struct btrfs_fs_info * fs_info)2106bccf3abSJeff Mahoney int btrfs_find_orphan_roots(struct btrfs_fs_info *fs_info)
21176dda93cSYan, Zheng {
2126bccf3abSJeff Mahoney 	struct btrfs_root *tree_root = fs_info->tree_root;
21376dda93cSYan, Zheng 	struct extent_buffer *leaf;
21476dda93cSYan, Zheng 	struct btrfs_path *path;
21576dda93cSYan, Zheng 	struct btrfs_key key;
216d68fc57bSYan, Zheng 	struct btrfs_root *root;
21776dda93cSYan, Zheng 	int err = 0;
21876dda93cSYan, Zheng 	int ret;
21976dda93cSYan, Zheng 
22076dda93cSYan, Zheng 	path = btrfs_alloc_path();
22176dda93cSYan, Zheng 	if (!path)
22276dda93cSYan, Zheng 		return -ENOMEM;
22376dda93cSYan, Zheng 
22476dda93cSYan, Zheng 	key.objectid = BTRFS_ORPHAN_OBJECTID;
22576dda93cSYan, Zheng 	key.type = BTRFS_ORPHAN_ITEM_KEY;
22676dda93cSYan, Zheng 	key.offset = 0;
22776dda93cSYan, Zheng 
22876dda93cSYan, Zheng 	while (1) {
22956e9357aSDavid Sterba 		u64 root_objectid;
23056e9357aSDavid Sterba 
23176dda93cSYan, Zheng 		ret = btrfs_search_slot(NULL, tree_root, &key, path, 0, 0);
23276dda93cSYan, Zheng 		if (ret < 0) {
23376dda93cSYan, Zheng 			err = ret;
23476dda93cSYan, Zheng 			break;
23576dda93cSYan, Zheng 		}
23676dda93cSYan, Zheng 
23776dda93cSYan, Zheng 		leaf = path->nodes[0];
23876dda93cSYan, Zheng 		if (path->slots[0] >= btrfs_header_nritems(leaf)) {
23976dda93cSYan, Zheng 			ret = btrfs_next_leaf(tree_root, path);
24076dda93cSYan, Zheng 			if (ret < 0)
24176dda93cSYan, Zheng 				err = ret;
24276dda93cSYan, Zheng 			if (ret != 0)
24376dda93cSYan, Zheng 				break;
24476dda93cSYan, Zheng 			leaf = path->nodes[0];
24576dda93cSYan, Zheng 		}
24676dda93cSYan, Zheng 
24776dda93cSYan, Zheng 		btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
248b3b4aa74SDavid Sterba 		btrfs_release_path(path);
24976dda93cSYan, Zheng 
25076dda93cSYan, Zheng 		if (key.objectid != BTRFS_ORPHAN_OBJECTID ||
25176dda93cSYan, Zheng 		    key.type != BTRFS_ORPHAN_ITEM_KEY)
25276dda93cSYan, Zheng 			break;
25376dda93cSYan, Zheng 
25456e9357aSDavid Sterba 		root_objectid = key.offset;
255d68fc57bSYan, Zheng 		key.offset++;
256d68fc57bSYan, Zheng 
25756e9357aSDavid Sterba 		root = btrfs_get_fs_root(fs_info, root_objectid, false);
258886322e8SSachin Kamat 		err = PTR_ERR_OR_ZERO(root);
25968a7342cSJosef Bacik 		if (err && err != -ENOENT) {
26076dda93cSYan, Zheng 			break;
26168a7342cSJosef Bacik 		} else if (err == -ENOENT) {
26268a7342cSJosef Bacik 			struct btrfs_trans_handle *trans;
26368a7342cSJosef Bacik 
26468a7342cSJosef Bacik 			btrfs_release_path(path);
26568a7342cSJosef Bacik 
26668a7342cSJosef Bacik 			trans = btrfs_join_transaction(tree_root);
26768a7342cSJosef Bacik 			if (IS_ERR(trans)) {
26868a7342cSJosef Bacik 				err = PTR_ERR(trans);
2690b246afaSJeff Mahoney 				btrfs_handle_fs_error(fs_info, err,
2705d163e0eSJeff Mahoney 					    "Failed to start trans to delete orphan item");
27168a7342cSJosef Bacik 				break;
27268a7342cSJosef Bacik 			}
27368a7342cSJosef Bacik 			err = btrfs_del_orphan_item(trans, tree_root,
27456e9357aSDavid Sterba 						    root_objectid);
2753a45bb20SJeff Mahoney 			btrfs_end_transaction(trans);
27668a7342cSJosef Bacik 			if (err) {
2770b246afaSJeff Mahoney 				btrfs_handle_fs_error(fs_info, err,
2785d163e0eSJeff Mahoney 					    "Failed to delete root orphan item");
27968a7342cSJosef Bacik 				break;
28068a7342cSJosef Bacik 			}
28168a7342cSJosef Bacik 			continue;
28276dda93cSYan, Zheng 		}
28376dda93cSYan, Zheng 
284e59d18b4SJosef Bacik 		WARN_ON(!test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state));
28578c52d9eSJosef Bacik 		if (btrfs_root_refs(&root->root_item) == 0) {
286b4be6aefSJosef Bacik 			struct btrfs_key drop_key;
287b4be6aefSJosef Bacik 
288b4be6aefSJosef Bacik 			btrfs_disk_key_to_cpu(&drop_key, &root->root_item.drop_progress);
289b4be6aefSJosef Bacik 			/*
290b4be6aefSJosef Bacik 			 * If we have a non-zero drop_progress then we know we
291b4be6aefSJosef Bacik 			 * made it partly through deleting this snapshot, and
292b4be6aefSJosef Bacik 			 * thus we need to make sure we block any balance from
293b4be6aefSJosef Bacik 			 * happening until this snapshot is completely dropped.
294b4be6aefSJosef Bacik 			 */
295b4be6aefSJosef Bacik 			if (drop_key.objectid != 0 || drop_key.type != 0 ||
296b4be6aefSJosef Bacik 			    drop_key.offset != 0) {
297b4be6aefSJosef Bacik 				set_bit(BTRFS_FS_UNFINISHED_DROPS, &fs_info->flags);
298b4be6aefSJosef Bacik 				set_bit(BTRFS_ROOT_UNFINISHED_DROP, &root->state);
299b4be6aefSJosef Bacik 			}
300b4be6aefSJosef Bacik 
30178c52d9eSJosef Bacik 			set_bit(BTRFS_ROOT_DEAD_TREE, &root->state);
30214927d95SMiao Xie 			btrfs_add_dead_root(root);
30376dda93cSYan, Zheng 		}
30400246528SJosef Bacik 		btrfs_put_root(root);
30578c52d9eSJosef Bacik 	}
30676dda93cSYan, Zheng 
30776dda93cSYan, Zheng 	btrfs_free_path(path);
30876dda93cSYan, Zheng 	return err;
30976dda93cSYan, Zheng }
31076dda93cSYan, Zheng 
3111cd5447eSJeff Mahoney /* drop the root item for 'key' from the tree root */
btrfs_del_root(struct btrfs_trans_handle * trans,const struct btrfs_key * key)3121cd5447eSJeff Mahoney int btrfs_del_root(struct btrfs_trans_handle *trans,
313ab9ce7d4SLu Fengqi 		   const struct btrfs_key *key)
3143768f368SChris Mason {
315ab9ce7d4SLu Fengqi 	struct btrfs_root *root = trans->fs_info->tree_root;
3165caf2a00SChris Mason 	struct btrfs_path *path;
3173768f368SChris Mason 	int ret;
3183768f368SChris Mason 
3195caf2a00SChris Mason 	path = btrfs_alloc_path();
320db5b493aSTsutomu Itoh 	if (!path)
321db5b493aSTsutomu Itoh 		return -ENOMEM;
3225caf2a00SChris Mason 	ret = btrfs_search_slot(trans, root, key, path, -1, 1);
3233768f368SChris Mason 	if (ret < 0)
3243768f368SChris Mason 		goto out;
325edbd8d4eSChris Mason 
3263768f368SChris Mason 	BUG_ON(ret != 0);
327c5739bbaSChris Mason 
3285caf2a00SChris Mason 	ret = btrfs_del_item(trans, root, path);
3293768f368SChris Mason out:
3305caf2a00SChris Mason 	btrfs_free_path(path);
3313768f368SChris Mason 	return ret;
3323768f368SChris Mason }
3330660b5afSChris Mason 
btrfs_del_root_ref(struct btrfs_trans_handle * trans,u64 root_id,u64 ref_id,u64 dirid,u64 * sequence,const struct fscrypt_str * name)3343ee1c553SLu Fengqi int btrfs_del_root_ref(struct btrfs_trans_handle *trans, u64 root_id,
335e43eec81SSweet Tea Dorminy 		       u64 ref_id, u64 dirid, u64 *sequence,
3366db75318SSweet Tea Dorminy 		       const struct fscrypt_str *name)
3370660b5afSChris Mason {
3383ee1c553SLu Fengqi 	struct btrfs_root *tree_root = trans->fs_info->tree_root;
3390660b5afSChris Mason 	struct btrfs_path *path;
3404df27c4dSYan, Zheng 	struct btrfs_root_ref *ref;
3414df27c4dSYan, Zheng 	struct extent_buffer *leaf;
3424df27c4dSYan, Zheng 	struct btrfs_key key;
3434df27c4dSYan, Zheng 	unsigned long ptr;
3444df27c4dSYan, Zheng 	int ret;
3450660b5afSChris Mason 
3460660b5afSChris Mason 	path = btrfs_alloc_path();
3474df27c4dSYan, Zheng 	if (!path)
3484df27c4dSYan, Zheng 		return -ENOMEM;
3490660b5afSChris Mason 
3500660b5afSChris Mason 	key.objectid = root_id;
3514df27c4dSYan, Zheng 	key.type = BTRFS_ROOT_BACKREF_KEY;
3520660b5afSChris Mason 	key.offset = ref_id;
3534df27c4dSYan, Zheng again:
3540660b5afSChris Mason 	ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1);
35547bf225aSFilipe Manana 	if (ret < 0) {
3568289ed9fSQu Wenruo 		goto out;
35747bf225aSFilipe Manana 	} else if (ret == 0) {
3584df27c4dSYan, Zheng 		leaf = path->nodes[0];
3594df27c4dSYan, Zheng 		ref = btrfs_item_ptr(leaf, path->slots[0],
3604df27c4dSYan, Zheng 				     struct btrfs_root_ref);
3614df27c4dSYan, Zheng 		ptr = (unsigned long)(ref + 1);
362423a716cSJosef Bacik 		if ((btrfs_root_ref_dirid(leaf, ref) != dirid) ||
363e43eec81SSweet Tea Dorminy 		    (btrfs_root_ref_name_len(leaf, ref) != name->len) ||
364e43eec81SSweet Tea Dorminy 		    memcmp_extent_buffer(leaf, name->name, ptr, name->len)) {
3651fdbd03dSFilipe Manana 			ret = -ENOENT;
366423a716cSJosef Bacik 			goto out;
367423a716cSJosef Bacik 		}
3684df27c4dSYan, Zheng 		*sequence = btrfs_root_ref_sequence(leaf, ref);
3690660b5afSChris Mason 
3700660b5afSChris Mason 		ret = btrfs_del_item(trans, tree_root, path);
3711fdbd03dSFilipe Manana 		if (ret)
3721fdbd03dSFilipe Manana 			goto out;
3731fdbd03dSFilipe Manana 	} else {
3741fdbd03dSFilipe Manana 		ret = -ENOENT;
37565a246c5STsutomu Itoh 		goto out;
37665a246c5STsutomu Itoh 	}
3774df27c4dSYan, Zheng 
3784df27c4dSYan, Zheng 	if (key.type == BTRFS_ROOT_BACKREF_KEY) {
379b3b4aa74SDavid Sterba 		btrfs_release_path(path);
3804df27c4dSYan, Zheng 		key.objectid = ref_id;
3814df27c4dSYan, Zheng 		key.type = BTRFS_ROOT_REF_KEY;
3824df27c4dSYan, Zheng 		key.offset = root_id;
3834df27c4dSYan, Zheng 		goto again;
3844df27c4dSYan, Zheng 	}
3850660b5afSChris Mason 
38665a246c5STsutomu Itoh out:
3870660b5afSChris Mason 	btrfs_free_path(path);
3881fdbd03dSFilipe Manana 	return ret;
3890660b5afSChris Mason }
3900660b5afSChris Mason 
3910660b5afSChris Mason /*
3920660b5afSChris Mason  * add a btrfs_root_ref item.  type is either BTRFS_ROOT_REF_KEY
3930660b5afSChris Mason  * or BTRFS_ROOT_BACKREF_KEY.
3940660b5afSChris Mason  *
3950660b5afSChris Mason  * The dirid, sequence, name and name_len refer to the directory entry
3960660b5afSChris Mason  * that is referencing the root.
3970660b5afSChris Mason  *
3980660b5afSChris Mason  * For a forward ref, the root_id is the id of the tree referencing
3990660b5afSChris Mason  * the root and ref_id is the id of the subvol  or snapshot.
4000660b5afSChris Mason  *
4010660b5afSChris Mason  * For a back ref the root_id is the id of the subvol or snapshot and
4020660b5afSChris Mason  * ref_id is the id of the tree referencing it.
40379787eaaSJeff Mahoney  *
40479787eaaSJeff Mahoney  * Will return 0, -ENOMEM, or anything from the CoW path
4050660b5afSChris Mason  */
btrfs_add_root_ref(struct btrfs_trans_handle * trans,u64 root_id,u64 ref_id,u64 dirid,u64 sequence,const struct fscrypt_str * name)4066025c19fSLu Fengqi int btrfs_add_root_ref(struct btrfs_trans_handle *trans, u64 root_id,
407e43eec81SSweet Tea Dorminy 		       u64 ref_id, u64 dirid, u64 sequence,
4086db75318SSweet Tea Dorminy 		       const struct fscrypt_str *name)
4090660b5afSChris Mason {
4106025c19fSLu Fengqi 	struct btrfs_root *tree_root = trans->fs_info->tree_root;
4110660b5afSChris Mason 	struct btrfs_key key;
4120660b5afSChris Mason 	int ret;
4130660b5afSChris Mason 	struct btrfs_path *path;
4140660b5afSChris Mason 	struct btrfs_root_ref *ref;
4150660b5afSChris Mason 	struct extent_buffer *leaf;
4160660b5afSChris Mason 	unsigned long ptr;
4170660b5afSChris Mason 
4180660b5afSChris Mason 	path = btrfs_alloc_path();
4194df27c4dSYan, Zheng 	if (!path)
4204df27c4dSYan, Zheng 		return -ENOMEM;
4210660b5afSChris Mason 
4220660b5afSChris Mason 	key.objectid = root_id;
4234df27c4dSYan, Zheng 	key.type = BTRFS_ROOT_BACKREF_KEY;
4240660b5afSChris Mason 	key.offset = ref_id;
4254df27c4dSYan, Zheng again:
4260660b5afSChris Mason 	ret = btrfs_insert_empty_item(trans, tree_root, path, &key,
427e43eec81SSweet Tea Dorminy 				      sizeof(*ref) + name->len);
42879787eaaSJeff Mahoney 	if (ret) {
42966642832SJeff Mahoney 		btrfs_abort_transaction(trans, ret);
43079787eaaSJeff Mahoney 		btrfs_free_path(path);
43179787eaaSJeff Mahoney 		return ret;
43279787eaaSJeff Mahoney 	}
4330660b5afSChris Mason 
4340660b5afSChris Mason 	leaf = path->nodes[0];
4350660b5afSChris Mason 	ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
4360660b5afSChris Mason 	btrfs_set_root_ref_dirid(leaf, ref, dirid);
4370660b5afSChris Mason 	btrfs_set_root_ref_sequence(leaf, ref, sequence);
438e43eec81SSweet Tea Dorminy 	btrfs_set_root_ref_name_len(leaf, ref, name->len);
4390660b5afSChris Mason 	ptr = (unsigned long)(ref + 1);
440e43eec81SSweet Tea Dorminy 	write_extent_buffer(leaf, name->name, ptr, name->len);
441*d5e09e38SFilipe Manana 	btrfs_mark_buffer_dirty(trans, leaf);
4420660b5afSChris Mason 
4434df27c4dSYan, Zheng 	if (key.type == BTRFS_ROOT_BACKREF_KEY) {
444b3b4aa74SDavid Sterba 		btrfs_release_path(path);
4454df27c4dSYan, Zheng 		key.objectid = ref_id;
4464df27c4dSYan, Zheng 		key.type = BTRFS_ROOT_REF_KEY;
4474df27c4dSYan, Zheng 		key.offset = root_id;
4484df27c4dSYan, Zheng 		goto again;
4494df27c4dSYan, Zheng 	}
4504df27c4dSYan, Zheng 
4510660b5afSChris Mason 	btrfs_free_path(path);
4524df27c4dSYan, Zheng 	return 0;
4530660b5afSChris Mason }
45408fe4db1SLi Zefan 
45508fe4db1SLi Zefan /*
45608fe4db1SLi Zefan  * Old btrfs forgets to init root_item->flags and root_item->byte_limit
45708fe4db1SLi Zefan  * for subvolumes. To work around this problem, we steal a bit from
45808fe4db1SLi Zefan  * root_item->inode_item->flags, and use it to indicate if those fields
45908fe4db1SLi Zefan  * have been properly initialized.
46008fe4db1SLi Zefan  */
btrfs_check_and_init_root_item(struct btrfs_root_item * root_item)46108fe4db1SLi Zefan void btrfs_check_and_init_root_item(struct btrfs_root_item *root_item)
46208fe4db1SLi Zefan {
4633cae210fSQu Wenruo 	u64 inode_flags = btrfs_stack_inode_flags(&root_item->inode);
46408fe4db1SLi Zefan 
46508fe4db1SLi Zefan 	if (!(inode_flags & BTRFS_INODE_ROOT_ITEM_INIT)) {
46608fe4db1SLi Zefan 		inode_flags |= BTRFS_INODE_ROOT_ITEM_INIT;
4673cae210fSQu Wenruo 		btrfs_set_stack_inode_flags(&root_item->inode, inode_flags);
4683cae210fSQu Wenruo 		btrfs_set_root_flags(root_item, 0);
4693cae210fSQu Wenruo 		btrfs_set_root_limit(root_item, 0);
47008fe4db1SLi Zefan 	}
47108fe4db1SLi Zefan }
4728ea05e3aSAlexander Block 
btrfs_update_root_times(struct btrfs_trans_handle * trans,struct btrfs_root * root)4738ea05e3aSAlexander Block void btrfs_update_root_times(struct btrfs_trans_handle *trans,
4748ea05e3aSAlexander Block 			     struct btrfs_root *root)
4758ea05e3aSAlexander Block {
4768ea05e3aSAlexander Block 	struct btrfs_root_item *item = &root->root_item;
47795582b00SDeepa Dinamani 	struct timespec64 ct;
4788ea05e3aSAlexander Block 
47995582b00SDeepa Dinamani 	ktime_get_real_ts64(&ct);
4805f3ab90aSAnand Jain 	spin_lock(&root->root_item_lock);
4813cae210fSQu Wenruo 	btrfs_set_root_ctransid(item, trans->transid);
4823cae210fSQu Wenruo 	btrfs_set_stack_timespec_sec(&item->ctime, ct.tv_sec);
4833cae210fSQu Wenruo 	btrfs_set_stack_timespec_nsec(&item->ctime, ct.tv_nsec);
4845f3ab90aSAnand Jain 	spin_unlock(&root->root_item_lock);
4858ea05e3aSAlexander Block }
48628a32d2bSJosef Bacik 
48728a32d2bSJosef Bacik /*
48828a32d2bSJosef Bacik  * btrfs_subvolume_reserve_metadata() - reserve space for subvolume operation
48928a32d2bSJosef Bacik  * root: the root of the parent directory
49028a32d2bSJosef Bacik  * rsv: block reservation
49128a32d2bSJosef Bacik  * items: the number of items that we need do reservation
49228a32d2bSJosef Bacik  * use_global_rsv: allow fallback to the global block reservation
49328a32d2bSJosef Bacik  *
49428a32d2bSJosef Bacik  * This function is used to reserve the space for snapshot/subvolume
49528a32d2bSJosef Bacik  * creation and deletion. Those operations are different with the
49628a32d2bSJosef Bacik  * common file/directory operations, they change two fs/file trees
49728a32d2bSJosef Bacik  * and root tree, the number of items that the qgroup reserves is
49828a32d2bSJosef Bacik  * different with the free space reservation. So we can not use
49928a32d2bSJosef Bacik  * the space reservation mechanism in start_transaction().
50028a32d2bSJosef Bacik  */
btrfs_subvolume_reserve_metadata(struct btrfs_root * root,struct btrfs_block_rsv * rsv,int items,bool use_global_rsv)50128a32d2bSJosef Bacik int btrfs_subvolume_reserve_metadata(struct btrfs_root *root,
50228a32d2bSJosef Bacik 				     struct btrfs_block_rsv *rsv, int items,
50328a32d2bSJosef Bacik 				     bool use_global_rsv)
50428a32d2bSJosef Bacik {
50528a32d2bSJosef Bacik 	u64 qgroup_num_bytes = 0;
50628a32d2bSJosef Bacik 	u64 num_bytes;
50728a32d2bSJosef Bacik 	int ret;
50828a32d2bSJosef Bacik 	struct btrfs_fs_info *fs_info = root->fs_info;
50928a32d2bSJosef Bacik 	struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
51028a32d2bSJosef Bacik 
51128a32d2bSJosef Bacik 	if (test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags)) {
51228a32d2bSJosef Bacik 		/* One for parent inode, two for dir entries */
51328a32d2bSJosef Bacik 		qgroup_num_bytes = 3 * fs_info->nodesize;
51428a32d2bSJosef Bacik 		ret = btrfs_qgroup_reserve_meta_prealloc(root,
515d4135134SFilipe Manana 							 qgroup_num_bytes, true,
516d4135134SFilipe Manana 							 false);
51728a32d2bSJosef Bacik 		if (ret)
51828a32d2bSJosef Bacik 			return ret;
51928a32d2bSJosef Bacik 	}
52028a32d2bSJosef Bacik 
5212bd36e7bSJosef Bacik 	num_bytes = btrfs_calc_insert_metadata_size(fs_info, items);
52228a32d2bSJosef Bacik 	rsv->space_info = btrfs_find_space_info(fs_info,
52328a32d2bSJosef Bacik 					    BTRFS_BLOCK_GROUP_METADATA);
5249270501cSJosef Bacik 	ret = btrfs_block_rsv_add(fs_info, rsv, num_bytes,
52528a32d2bSJosef Bacik 				  BTRFS_RESERVE_FLUSH_ALL);
52628a32d2bSJosef Bacik 
52728a32d2bSJosef Bacik 	if (ret == -ENOSPC && use_global_rsv)
52828a32d2bSJosef Bacik 		ret = btrfs_block_rsv_migrate(global_rsv, rsv, num_bytes, true);
52928a32d2bSJosef Bacik 
53028a32d2bSJosef Bacik 	if (ret && qgroup_num_bytes)
53128a32d2bSJosef Bacik 		btrfs_qgroup_free_meta_prealloc(root, qgroup_num_bytes);
53228a32d2bSJosef Bacik 
533e85fde51SQu Wenruo 	if (!ret) {
534e85fde51SQu Wenruo 		spin_lock(&rsv->lock);
535e85fde51SQu Wenruo 		rsv->qgroup_rsv_reserved += qgroup_num_bytes;
536e85fde51SQu Wenruo 		spin_unlock(&rsv->lock);
537e85fde51SQu Wenruo 	}
53828a32d2bSJosef Bacik 	return ret;
53928a32d2bSJosef Bacik }
540