root-tree.c revision 4df27c4d5cc1dda54ed7d0a8389347f2df359cf9
16cbd55707802b98843f953d1ae6d8f5bcd9a76c0Chris Mason/*
26cbd55707802b98843f953d1ae6d8f5bcd9a76c0Chris Mason * Copyright (C) 2007 Oracle.  All rights reserved.
36cbd55707802b98843f953d1ae6d8f5bcd9a76c0Chris Mason *
46cbd55707802b98843f953d1ae6d8f5bcd9a76c0Chris Mason * This program is free software; you can redistribute it and/or
56cbd55707802b98843f953d1ae6d8f5bcd9a76c0Chris Mason * modify it under the terms of the GNU General Public
66cbd55707802b98843f953d1ae6d8f5bcd9a76c0Chris Mason * License v2 as published by the Free Software Foundation.
76cbd55707802b98843f953d1ae6d8f5bcd9a76c0Chris Mason *
86cbd55707802b98843f953d1ae6d8f5bcd9a76c0Chris Mason * This program is distributed in the hope that it will be useful,
96cbd55707802b98843f953d1ae6d8f5bcd9a76c0Chris Mason * but WITHOUT ANY WARRANTY; without even the implied warranty of
106cbd55707802b98843f953d1ae6d8f5bcd9a76c0Chris Mason * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
116cbd55707802b98843f953d1ae6d8f5bcd9a76c0Chris Mason * General Public License for more details.
126cbd55707802b98843f953d1ae6d8f5bcd9a76c0Chris Mason *
136cbd55707802b98843f953d1ae6d8f5bcd9a76c0Chris Mason * You should have received a copy of the GNU General Public
146cbd55707802b98843f953d1ae6d8f5bcd9a76c0Chris Mason * License along with this program; if not, write to the
156cbd55707802b98843f953d1ae6d8f5bcd9a76c0Chris Mason * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
166cbd55707802b98843f953d1ae6d8f5bcd9a76c0Chris Mason * Boston, MA 021110-1307, USA.
176cbd55707802b98843f953d1ae6d8f5bcd9a76c0Chris Mason */
186cbd55707802b98843f953d1ae6d8f5bcd9a76c0Chris Mason
193768f3689fc76ecea17414936dff7a02746a4355Chris Mason#include "ctree.h"
205eda7b5e9b0bed864dd18284c7df9b3c8207dad7Chris Mason#include "transaction.h"
213768f3689fc76ecea17414936dff7a02746a4355Chris Mason#include "disk-io.h"
223768f3689fc76ecea17414936dff7a02746a4355Chris Mason#include "print-tree.h"
233768f3689fc76ecea17414936dff7a02746a4355Chris Mason
24bf4ef67924d87b0addb32f084e83a9283496350eChris Mason/*
25d352ac68148b69937d39ca5d48bcc4478e118dbfChris Mason *  search forward for a root, starting with objectid 'search_start'
26d352ac68148b69937d39ca5d48bcc4478e118dbfChris Mason *  if a root key is found, the objectid we find is filled into 'found_objectid'
27d352ac68148b69937d39ca5d48bcc4478e118dbfChris Mason *  and 0 is returned.  < 0 is returned on error, 1 if there is nothing
28d352ac68148b69937d39ca5d48bcc4478e118dbfChris Mason *  left in the tree.
29bf4ef67924d87b0addb32f084e83a9283496350eChris Mason */
30bf4ef67924d87b0addb32f084e83a9283496350eChris Masonint btrfs_search_root(struct btrfs_root *root, u64 search_start,
31bf4ef67924d87b0addb32f084e83a9283496350eChris Mason		      u64 *found_objectid)
32bf4ef67924d87b0addb32f084e83a9283496350eChris Mason{
33bf4ef67924d87b0addb32f084e83a9283496350eChris Mason	struct btrfs_path *path;
34bf4ef67924d87b0addb32f084e83a9283496350eChris Mason	struct btrfs_key search_key;
35bf4ef67924d87b0addb32f084e83a9283496350eChris Mason	int ret;
36bf4ef67924d87b0addb32f084e83a9283496350eChris Mason
37bf4ef67924d87b0addb32f084e83a9283496350eChris Mason	root = root->fs_info->tree_root;
38bf4ef67924d87b0addb32f084e83a9283496350eChris Mason	search_key.objectid = search_start;
39bf4ef67924d87b0addb32f084e83a9283496350eChris Mason	search_key.type = (u8)-1;
40bf4ef67924d87b0addb32f084e83a9283496350eChris Mason	search_key.offset = (u64)-1;
41bf4ef67924d87b0addb32f084e83a9283496350eChris Mason
42bf4ef67924d87b0addb32f084e83a9283496350eChris Mason	path = btrfs_alloc_path();
43bf4ef67924d87b0addb32f084e83a9283496350eChris Mason	BUG_ON(!path);
44bf4ef67924d87b0addb32f084e83a9283496350eChris Masonagain:
45bf4ef67924d87b0addb32f084e83a9283496350eChris Mason	ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
46bf4ef67924d87b0addb32f084e83a9283496350eChris Mason	if (ret < 0)
47bf4ef67924d87b0addb32f084e83a9283496350eChris Mason		goto out;
48bf4ef67924d87b0addb32f084e83a9283496350eChris Mason	if (ret == 0) {
49bf4ef67924d87b0addb32f084e83a9283496350eChris Mason		ret = 1;
50bf4ef67924d87b0addb32f084e83a9283496350eChris Mason		goto out;
51bf4ef67924d87b0addb32f084e83a9283496350eChris Mason	}
52bf4ef67924d87b0addb32f084e83a9283496350eChris Mason	if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
53bf4ef67924d87b0addb32f084e83a9283496350eChris Mason		ret = btrfs_next_leaf(root, path);
54bf4ef67924d87b0addb32f084e83a9283496350eChris Mason		if (ret)
55bf4ef67924d87b0addb32f084e83a9283496350eChris Mason			goto out;
56bf4ef67924d87b0addb32f084e83a9283496350eChris Mason	}
57bf4ef67924d87b0addb32f084e83a9283496350eChris Mason	btrfs_item_key_to_cpu(path->nodes[0], &search_key, path->slots[0]);
58bf4ef67924d87b0addb32f084e83a9283496350eChris Mason	if (search_key.type != BTRFS_ROOT_ITEM_KEY) {
59bf4ef67924d87b0addb32f084e83a9283496350eChris Mason		search_key.offset++;
60bf4ef67924d87b0addb32f084e83a9283496350eChris Mason		btrfs_release_path(root, path);
61bf4ef67924d87b0addb32f084e83a9283496350eChris Mason		goto again;
62bf4ef67924d87b0addb32f084e83a9283496350eChris Mason	}
63bf4ef67924d87b0addb32f084e83a9283496350eChris Mason	ret = 0;
64bf4ef67924d87b0addb32f084e83a9283496350eChris Mason	*found_objectid = search_key.objectid;
65bf4ef67924d87b0addb32f084e83a9283496350eChris Mason
66bf4ef67924d87b0addb32f084e83a9283496350eChris Masonout:
67bf4ef67924d87b0addb32f084e83a9283496350eChris Mason	btrfs_free_path(path);
68bf4ef67924d87b0addb32f084e83a9283496350eChris Mason	return ret;
69bf4ef67924d87b0addb32f084e83a9283496350eChris Mason}
70bf4ef67924d87b0addb32f084e83a9283496350eChris Mason
71d352ac68148b69937d39ca5d48bcc4478e118dbfChris Mason/*
72d352ac68148b69937d39ca5d48bcc4478e118dbfChris Mason * lookup the root with the highest offset for a given objectid.  The key we do
73d352ac68148b69937d39ca5d48bcc4478e118dbfChris Mason * find is copied into 'key'.  If we find something return 0, otherwise 1, < 0
74d352ac68148b69937d39ca5d48bcc4478e118dbfChris Mason * on error.
75d352ac68148b69937d39ca5d48bcc4478e118dbfChris Mason */
763768f3689fc76ecea17414936dff7a02746a4355Chris Masonint btrfs_find_last_root(struct btrfs_root *root, u64 objectid,
773768f3689fc76ecea17414936dff7a02746a4355Chris Mason			struct btrfs_root_item *item, struct btrfs_key *key)
783768f3689fc76ecea17414936dff7a02746a4355Chris Mason{
795caf2a002901f0fde475371c4bf1c553b51884afChris Mason	struct btrfs_path *path;
803768f3689fc76ecea17414936dff7a02746a4355Chris Mason	struct btrfs_key search_key;
815f39d397dfbe140a14edecd4e73c34ce23c4f9eeChris Mason	struct btrfs_key found_key;
825f39d397dfbe140a14edecd4e73c34ce23c4f9eeChris Mason	struct extent_buffer *l;
833768f3689fc76ecea17414936dff7a02746a4355Chris Mason	int ret;
843768f3689fc76ecea17414936dff7a02746a4355Chris Mason	int slot;
853768f3689fc76ecea17414936dff7a02746a4355Chris Mason
863768f3689fc76ecea17414936dff7a02746a4355Chris Mason	search_key.objectid = objectid;
870660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason	search_key.type = BTRFS_ROOT_ITEM_KEY;
885eda7b5e9b0bed864dd18284c7df9b3c8207dad7Chris Mason	search_key.offset = (u64)-1;
893768f3689fc76ecea17414936dff7a02746a4355Chris Mason
905caf2a002901f0fde475371c4bf1c553b51884afChris Mason	path = btrfs_alloc_path();
915caf2a002901f0fde475371c4bf1c553b51884afChris Mason	BUG_ON(!path);
925caf2a002901f0fde475371c4bf1c553b51884afChris Mason	ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
933768f3689fc76ecea17414936dff7a02746a4355Chris Mason	if (ret < 0)
943768f3689fc76ecea17414936dff7a02746a4355Chris Mason		goto out;
955f39d397dfbe140a14edecd4e73c34ce23c4f9eeChris Mason
963768f3689fc76ecea17414936dff7a02746a4355Chris Mason	BUG_ON(ret == 0);
975f39d397dfbe140a14edecd4e73c34ce23c4f9eeChris Mason	l = path->nodes[0];
985caf2a002901f0fde475371c4bf1c553b51884afChris Mason	BUG_ON(path->slots[0] == 0);
995caf2a002901f0fde475371c4bf1c553b51884afChris Mason	slot = path->slots[0] - 1;
1005f39d397dfbe140a14edecd4e73c34ce23c4f9eeChris Mason	btrfs_item_key_to_cpu(l, &found_key, slot);
1015f39d397dfbe140a14edecd4e73c34ce23c4f9eeChris Mason	if (found_key.objectid != objectid) {
1023768f3689fc76ecea17414936dff7a02746a4355Chris Mason		ret = 1;
1033768f3689fc76ecea17414936dff7a02746a4355Chris Mason		goto out;
1043768f3689fc76ecea17414936dff7a02746a4355Chris Mason	}
1055f39d397dfbe140a14edecd4e73c34ce23c4f9eeChris Mason	read_extent_buffer(l, item, btrfs_item_ptr_offset(l, slot),
1065f39d397dfbe140a14edecd4e73c34ce23c4f9eeChris Mason			   sizeof(*item));
1075f39d397dfbe140a14edecd4e73c34ce23c4f9eeChris Mason	memcpy(key, &found_key, sizeof(found_key));
1083768f3689fc76ecea17414936dff7a02746a4355Chris Mason	ret = 0;
1093768f3689fc76ecea17414936dff7a02746a4355Chris Masonout:
1105caf2a002901f0fde475371c4bf1c553b51884afChris Mason	btrfs_free_path(path);
1113768f3689fc76ecea17414936dff7a02746a4355Chris Mason	return ret;
1123768f3689fc76ecea17414936dff7a02746a4355Chris Mason}
1133768f3689fc76ecea17414936dff7a02746a4355Chris Mason
1145d4f98a28c7d334091c1b7744f48a1acdd2a4ae0Yan Zhengint btrfs_set_root_node(struct btrfs_root_item *item,
1155d4f98a28c7d334091c1b7744f48a1acdd2a4ae0Yan Zheng			struct extent_buffer *node)
1165d4f98a28c7d334091c1b7744f48a1acdd2a4ae0Yan Zheng{
1175d4f98a28c7d334091c1b7744f48a1acdd2a4ae0Yan Zheng	btrfs_set_root_bytenr(item, node->start);
1185d4f98a28c7d334091c1b7744f48a1acdd2a4ae0Yan Zheng	btrfs_set_root_level(item, btrfs_header_level(node));
1195d4f98a28c7d334091c1b7744f48a1acdd2a4ae0Yan Zheng	btrfs_set_root_generation(item, btrfs_header_generation(node));
1205d4f98a28c7d334091c1b7744f48a1acdd2a4ae0Yan Zheng	return 0;
1215d4f98a28c7d334091c1b7744f48a1acdd2a4ae0Yan Zheng}
1225d4f98a28c7d334091c1b7744f48a1acdd2a4ae0Yan Zheng
123d352ac68148b69937d39ca5d48bcc4478e118dbfChris Mason/*
124d352ac68148b69937d39ca5d48bcc4478e118dbfChris Mason * copy the data in 'item' into the btree
125d352ac68148b69937d39ca5d48bcc4478e118dbfChris Mason */
126e089f05c18ab36ed5fa7e2319052e03ab800d518Chris Masonint btrfs_update_root(struct btrfs_trans_handle *trans, struct btrfs_root
127e089f05c18ab36ed5fa7e2319052e03ab800d518Chris Mason		      *root, struct btrfs_key *key, struct btrfs_root_item
128e089f05c18ab36ed5fa7e2319052e03ab800d518Chris Mason		      *item)
1293768f3689fc76ecea17414936dff7a02746a4355Chris Mason{
1305caf2a002901f0fde475371c4bf1c553b51884afChris Mason	struct btrfs_path *path;
1315f39d397dfbe140a14edecd4e73c34ce23c4f9eeChris Mason	struct extent_buffer *l;
1323768f3689fc76ecea17414936dff7a02746a4355Chris Mason	int ret;
1333768f3689fc76ecea17414936dff7a02746a4355Chris Mason	int slot;
1345f39d397dfbe140a14edecd4e73c34ce23c4f9eeChris Mason	unsigned long ptr;
1353768f3689fc76ecea17414936dff7a02746a4355Chris Mason
1365caf2a002901f0fde475371c4bf1c553b51884afChris Mason	path = btrfs_alloc_path();
1375caf2a002901f0fde475371c4bf1c553b51884afChris Mason	BUG_ON(!path);
1385caf2a002901f0fde475371c4bf1c553b51884afChris Mason	ret = btrfs_search_slot(trans, root, key, path, 0, 1);
1393768f3689fc76ecea17414936dff7a02746a4355Chris Mason	if (ret < 0)
1403768f3689fc76ecea17414936dff7a02746a4355Chris Mason		goto out;
141d666746207a01546e55bdaa4b721d1890faaf6e8Chris Mason
142d666746207a01546e55bdaa4b721d1890faaf6e8Chris Mason	if (ret != 0) {
143d666746207a01546e55bdaa4b721d1890faaf6e8Chris Mason		btrfs_print_leaf(root, path->nodes[0]);
144d397712bcc6a759a560fd247e6053ecae091f958Chris Mason		printk(KERN_CRIT "unable to update root key %llu %u %llu\n",
145d397712bcc6a759a560fd247e6053ecae091f958Chris Mason		       (unsigned long long)key->objectid, key->type,
146d397712bcc6a759a560fd247e6053ecae091f958Chris Mason		       (unsigned long long)key->offset);
147d666746207a01546e55bdaa4b721d1890faaf6e8Chris Mason		BUG_ON(1);
148d666746207a01546e55bdaa4b721d1890faaf6e8Chris Mason	}
149d666746207a01546e55bdaa4b721d1890faaf6e8Chris Mason
1505f39d397dfbe140a14edecd4e73c34ce23c4f9eeChris Mason	l = path->nodes[0];
1515caf2a002901f0fde475371c4bf1c553b51884afChris Mason	slot = path->slots[0];
1525f39d397dfbe140a14edecd4e73c34ce23c4f9eeChris Mason	ptr = btrfs_item_ptr_offset(l, slot);
1535f39d397dfbe140a14edecd4e73c34ce23c4f9eeChris Mason	write_extent_buffer(l, item, ptr, sizeof(*item));
1545caf2a002901f0fde475371c4bf1c553b51884afChris Mason	btrfs_mark_buffer_dirty(path->nodes[0]);
1553768f3689fc76ecea17414936dff7a02746a4355Chris Masonout:
1565caf2a002901f0fde475371c4bf1c553b51884afChris Mason	btrfs_release_path(root, path);
1575caf2a002901f0fde475371c4bf1c553b51884afChris Mason	btrfs_free_path(path);
1583768f3689fc76ecea17414936dff7a02746a4355Chris Mason	return ret;
1593768f3689fc76ecea17414936dff7a02746a4355Chris Mason}
1603768f3689fc76ecea17414936dff7a02746a4355Chris Mason
161e089f05c18ab36ed5fa7e2319052e03ab800d518Chris Masonint btrfs_insert_root(struct btrfs_trans_handle *trans, struct btrfs_root
162e089f05c18ab36ed5fa7e2319052e03ab800d518Chris Mason		      *root, struct btrfs_key *key, struct btrfs_root_item
163e089f05c18ab36ed5fa7e2319052e03ab800d518Chris Mason		      *item)
1643768f3689fc76ecea17414936dff7a02746a4355Chris Mason{
1653768f3689fc76ecea17414936dff7a02746a4355Chris Mason	int ret;
166e089f05c18ab36ed5fa7e2319052e03ab800d518Chris Mason	ret = btrfs_insert_item(trans, root, key, item, sizeof(*item));
1673768f3689fc76ecea17414936dff7a02746a4355Chris Mason	return ret;
1683768f3689fc76ecea17414936dff7a02746a4355Chris Mason}
1693768f3689fc76ecea17414936dff7a02746a4355Chris Mason
170d352ac68148b69937d39ca5d48bcc4478e118dbfChris Mason/*
171d352ac68148b69937d39ca5d48bcc4478e118dbfChris Mason * at mount time we want to find all the old transaction snapshots that were in
172d397712bcc6a759a560fd247e6053ecae091f958Chris Mason * the process of being deleted if we crashed.  This is any root item with an
173d397712bcc6a759a560fd247e6053ecae091f958Chris Mason * offset lower than the latest root.  They need to be queued for deletion to
174d397712bcc6a759a560fd247e6053ecae091f958Chris Mason * finish what was happening when we crashed.
175d352ac68148b69937d39ca5d48bcc4478e118dbfChris Mason */
1765d4f98a28c7d334091c1b7744f48a1acdd2a4ae0Yan Zhengint btrfs_find_dead_roots(struct btrfs_root *root, u64 objectid)
1775eda7b5e9b0bed864dd18284c7df9b3c8207dad7Chris Mason{
1785eda7b5e9b0bed864dd18284c7df9b3c8207dad7Chris Mason	struct btrfs_root *dead_root;
1795eda7b5e9b0bed864dd18284c7df9b3c8207dad7Chris Mason	struct btrfs_item *item;
1805eda7b5e9b0bed864dd18284c7df9b3c8207dad7Chris Mason	struct btrfs_root_item *ri;
1815eda7b5e9b0bed864dd18284c7df9b3c8207dad7Chris Mason	struct btrfs_key key;
182a7a16fd772620605c76e8ac8bdbc8ccc9e3df1a0Chris Mason	struct btrfs_key found_key;
1835eda7b5e9b0bed864dd18284c7df9b3c8207dad7Chris Mason	struct btrfs_path *path;
1845eda7b5e9b0bed864dd18284c7df9b3c8207dad7Chris Mason	int ret;
1855eda7b5e9b0bed864dd18284c7df9b3c8207dad7Chris Mason	u32 nritems;
1865f39d397dfbe140a14edecd4e73c34ce23c4f9eeChris Mason	struct extent_buffer *leaf;
1875eda7b5e9b0bed864dd18284c7df9b3c8207dad7Chris Mason	int slot;
1885eda7b5e9b0bed864dd18284c7df9b3c8207dad7Chris Mason
1895ce14bbcdd1b5d9233d26a1e89faf3a26c820c58Chris Mason	key.objectid = objectid;
1905eda7b5e9b0bed864dd18284c7df9b3c8207dad7Chris Mason	btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
1915eda7b5e9b0bed864dd18284c7df9b3c8207dad7Chris Mason	key.offset = 0;
1925eda7b5e9b0bed864dd18284c7df9b3c8207dad7Chris Mason	path = btrfs_alloc_path();
1935eda7b5e9b0bed864dd18284c7df9b3c8207dad7Chris Mason	if (!path)
1945eda7b5e9b0bed864dd18284c7df9b3c8207dad7Chris Mason		return -ENOMEM;
195a7a16fd772620605c76e8ac8bdbc8ccc9e3df1a0Chris Mason
196a7a16fd772620605c76e8ac8bdbc8ccc9e3df1a0Chris Masonagain:
1975eda7b5e9b0bed864dd18284c7df9b3c8207dad7Chris Mason	ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1985eda7b5e9b0bed864dd18284c7df9b3c8207dad7Chris Mason	if (ret < 0)
1995eda7b5e9b0bed864dd18284c7df9b3c8207dad7Chris Mason		goto err;
200d397712bcc6a759a560fd247e6053ecae091f958Chris Mason	while (1) {
2015f39d397dfbe140a14edecd4e73c34ce23c4f9eeChris Mason		leaf = path->nodes[0];
2025f39d397dfbe140a14edecd4e73c34ce23c4f9eeChris Mason		nritems = btrfs_header_nritems(leaf);
2035eda7b5e9b0bed864dd18284c7df9b3c8207dad7Chris Mason		slot = path->slots[0];
2045eda7b5e9b0bed864dd18284c7df9b3c8207dad7Chris Mason		if (slot >= nritems) {
2055eda7b5e9b0bed864dd18284c7df9b3c8207dad7Chris Mason			ret = btrfs_next_leaf(root, path);
2065eda7b5e9b0bed864dd18284c7df9b3c8207dad7Chris Mason			if (ret)
2075eda7b5e9b0bed864dd18284c7df9b3c8207dad7Chris Mason				break;
2085f39d397dfbe140a14edecd4e73c34ce23c4f9eeChris Mason			leaf = path->nodes[0];
2095f39d397dfbe140a14edecd4e73c34ce23c4f9eeChris Mason			nritems = btrfs_header_nritems(leaf);
2105eda7b5e9b0bed864dd18284c7df9b3c8207dad7Chris Mason			slot = path->slots[0];
2115eda7b5e9b0bed864dd18284c7df9b3c8207dad7Chris Mason		}
2125f39d397dfbe140a14edecd4e73c34ce23c4f9eeChris Mason		item = btrfs_item_nr(leaf, slot);
2135f39d397dfbe140a14edecd4e73c34ce23c4f9eeChris Mason		btrfs_item_key_to_cpu(leaf, &key, slot);
2145eda7b5e9b0bed864dd18284c7df9b3c8207dad7Chris Mason		if (btrfs_key_type(&key) != BTRFS_ROOT_ITEM_KEY)
2155eda7b5e9b0bed864dd18284c7df9b3c8207dad7Chris Mason			goto next;
2165ce14bbcdd1b5d9233d26a1e89faf3a26c820c58Chris Mason
2175ce14bbcdd1b5d9233d26a1e89faf3a26c820c58Chris Mason		if (key.objectid < objectid)
2185ce14bbcdd1b5d9233d26a1e89faf3a26c820c58Chris Mason			goto next;
2195ce14bbcdd1b5d9233d26a1e89faf3a26c820c58Chris Mason
2205ce14bbcdd1b5d9233d26a1e89faf3a26c820c58Chris Mason		if (key.objectid > objectid)
2215ce14bbcdd1b5d9233d26a1e89faf3a26c820c58Chris Mason			break;
2225ce14bbcdd1b5d9233d26a1e89faf3a26c820c58Chris Mason
2235eda7b5e9b0bed864dd18284c7df9b3c8207dad7Chris Mason		ri = btrfs_item_ptr(leaf, slot, struct btrfs_root_item);
2245f39d397dfbe140a14edecd4e73c34ce23c4f9eeChris Mason		if (btrfs_disk_root_refs(leaf, ri) != 0)
2255eda7b5e9b0bed864dd18284c7df9b3c8207dad7Chris Mason			goto next;
2265ce14bbcdd1b5d9233d26a1e89faf3a26c820c58Chris Mason
227a7a16fd772620605c76e8ac8bdbc8ccc9e3df1a0Chris Mason		memcpy(&found_key, &key, sizeof(key));
228a7a16fd772620605c76e8ac8bdbc8ccc9e3df1a0Chris Mason		key.offset++;
229a7a16fd772620605c76e8ac8bdbc8ccc9e3df1a0Chris Mason		btrfs_release_path(root, path);
230e02119d5a7b4396c5a872582fddc8bd6d305a70aChris Mason		dead_root =
231e02119d5a7b4396c5a872582fddc8bd6d305a70aChris Mason			btrfs_read_fs_root_no_radix(root->fs_info->tree_root,
232e02119d5a7b4396c5a872582fddc8bd6d305a70aChris Mason						    &found_key);
233a1f396304fb7e5f18e4ea81c294415375f1c814cAneesh		if (IS_ERR(dead_root)) {
234a1f396304fb7e5f18e4ea81c294415375f1c814cAneesh			ret = PTR_ERR(dead_root);
2355eda7b5e9b0bed864dd18284c7df9b3c8207dad7Chris Mason			goto err;
2365eda7b5e9b0bed864dd18284c7df9b3c8207dad7Chris Mason		}
2375ce14bbcdd1b5d9233d26a1e89faf3a26c820c58Chris Mason
2385d4f98a28c7d334091c1b7744f48a1acdd2a4ae0Yan Zheng		ret = btrfs_add_dead_root(dead_root);
2395eda7b5e9b0bed864dd18284c7df9b3c8207dad7Chris Mason		if (ret)
2405eda7b5e9b0bed864dd18284c7df9b3c8207dad7Chris Mason			goto err;
241a7a16fd772620605c76e8ac8bdbc8ccc9e3df1a0Chris Mason		goto again;
2425eda7b5e9b0bed864dd18284c7df9b3c8207dad7Chris Masonnext:
2435eda7b5e9b0bed864dd18284c7df9b3c8207dad7Chris Mason		slot++;
2445eda7b5e9b0bed864dd18284c7df9b3c8207dad7Chris Mason		path->slots[0]++;
2455eda7b5e9b0bed864dd18284c7df9b3c8207dad7Chris Mason	}
2465eda7b5e9b0bed864dd18284c7df9b3c8207dad7Chris Mason	ret = 0;
2475eda7b5e9b0bed864dd18284c7df9b3c8207dad7Chris Masonerr:
2485eda7b5e9b0bed864dd18284c7df9b3c8207dad7Chris Mason	btrfs_free_path(path);
2495eda7b5e9b0bed864dd18284c7df9b3c8207dad7Chris Mason	return ret;
2505eda7b5e9b0bed864dd18284c7df9b3c8207dad7Chris Mason}
2515eda7b5e9b0bed864dd18284c7df9b3c8207dad7Chris Mason
252d352ac68148b69937d39ca5d48bcc4478e118dbfChris Mason/* drop the root item for 'key' from 'root' */
253e089f05c18ab36ed5fa7e2319052e03ab800d518Chris Masonint btrfs_del_root(struct btrfs_trans_handle *trans, struct btrfs_root *root,
254e089f05c18ab36ed5fa7e2319052e03ab800d518Chris Mason		   struct btrfs_key *key)
2553768f3689fc76ecea17414936dff7a02746a4355Chris Mason{
2565caf2a002901f0fde475371c4bf1c553b51884afChris Mason	struct btrfs_path *path;
2573768f3689fc76ecea17414936dff7a02746a4355Chris Mason	int ret;
258c5739bba5260a59cebd20a51a55080592c8d3b07Chris Mason	u32 refs;
259c5739bba5260a59cebd20a51a55080592c8d3b07Chris Mason	struct btrfs_root_item *ri;
2605f39d397dfbe140a14edecd4e73c34ce23c4f9eeChris Mason	struct extent_buffer *leaf;
2613768f3689fc76ecea17414936dff7a02746a4355Chris Mason
2625caf2a002901f0fde475371c4bf1c553b51884afChris Mason	path = btrfs_alloc_path();
2635caf2a002901f0fde475371c4bf1c553b51884afChris Mason	BUG_ON(!path);
2645caf2a002901f0fde475371c4bf1c553b51884afChris Mason	ret = btrfs_search_slot(trans, root, key, path, -1, 1);
2653768f3689fc76ecea17414936dff7a02746a4355Chris Mason	if (ret < 0)
2663768f3689fc76ecea17414936dff7a02746a4355Chris Mason		goto out;
267edbd8d4efe4ddaf29a175ae504e2c9a05a96ebeeChris Mason
2683768f3689fc76ecea17414936dff7a02746a4355Chris Mason	BUG_ON(ret != 0);
2695f39d397dfbe140a14edecd4e73c34ce23c4f9eeChris Mason	leaf = path->nodes[0];
2705f39d397dfbe140a14edecd4e73c34ce23c4f9eeChris Mason	ri = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_item);
271c5739bba5260a59cebd20a51a55080592c8d3b07Chris Mason
2725f39d397dfbe140a14edecd4e73c34ce23c4f9eeChris Mason	refs = btrfs_disk_root_refs(leaf, ri);
2735eda7b5e9b0bed864dd18284c7df9b3c8207dad7Chris Mason	BUG_ON(refs != 0);
2745eda7b5e9b0bed864dd18284c7df9b3c8207dad7Chris Mason	ret = btrfs_del_item(trans, root, path);
2753768f3689fc76ecea17414936dff7a02746a4355Chris Masonout:
2765caf2a002901f0fde475371c4bf1c553b51884afChris Mason	btrfs_release_path(root, path);
2775caf2a002901f0fde475371c4bf1c553b51884afChris Mason	btrfs_free_path(path);
2783768f3689fc76ecea17414936dff7a02746a4355Chris Mason	return ret;
2793768f3689fc76ecea17414936dff7a02746a4355Chris Mason}
2800660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason
2810660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Masonint btrfs_del_root_ref(struct btrfs_trans_handle *trans,
2820660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason		       struct btrfs_root *tree_root,
2834df27c4d5cc1dda54ed7d0a8389347f2df359cf9Yan, Zheng		       u64 root_id, u64 ref_id, u64 dirid, u64 *sequence,
2844df27c4d5cc1dda54ed7d0a8389347f2df359cf9Yan, Zheng		       const char *name, int name_len)
2854df27c4d5cc1dda54ed7d0a8389347f2df359cf9Yan, Zheng
2860660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason{
2874df27c4d5cc1dda54ed7d0a8389347f2df359cf9Yan, Zheng	struct btrfs_path *path;
2884df27c4d5cc1dda54ed7d0a8389347f2df359cf9Yan, Zheng	struct btrfs_root_ref *ref;
2894df27c4d5cc1dda54ed7d0a8389347f2df359cf9Yan, Zheng	struct extent_buffer *leaf;
2900660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason	struct btrfs_key key;
2914df27c4d5cc1dda54ed7d0a8389347f2df359cf9Yan, Zheng	unsigned long ptr;
2924df27c4d5cc1dda54ed7d0a8389347f2df359cf9Yan, Zheng	int err = 0;
2930660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason	int ret;
2940660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason
2950660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason	path = btrfs_alloc_path();
2964df27c4d5cc1dda54ed7d0a8389347f2df359cf9Yan, Zheng	if (!path)
2974df27c4d5cc1dda54ed7d0a8389347f2df359cf9Yan, Zheng		return -ENOMEM;
2980660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason
2990660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason	key.objectid = root_id;
3004df27c4d5cc1dda54ed7d0a8389347f2df359cf9Yan, Zheng	key.type = BTRFS_ROOT_BACKREF_KEY;
3010660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason	key.offset = ref_id;
3024df27c4d5cc1dda54ed7d0a8389347f2df359cf9Yan, Zhengagain:
3030660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason	ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1);
3044df27c4d5cc1dda54ed7d0a8389347f2df359cf9Yan, Zheng	BUG_ON(ret < 0);
3054df27c4d5cc1dda54ed7d0a8389347f2df359cf9Yan, Zheng	if (ret == 0) {
3064df27c4d5cc1dda54ed7d0a8389347f2df359cf9Yan, Zheng		leaf = path->nodes[0];
3074df27c4d5cc1dda54ed7d0a8389347f2df359cf9Yan, Zheng		ref = btrfs_item_ptr(leaf, path->slots[0],
3084df27c4d5cc1dda54ed7d0a8389347f2df359cf9Yan, Zheng				     struct btrfs_root_ref);
3094df27c4d5cc1dda54ed7d0a8389347f2df359cf9Yan, Zheng
3104df27c4d5cc1dda54ed7d0a8389347f2df359cf9Yan, Zheng		WARN_ON(btrfs_root_ref_dirid(leaf, ref) != dirid);
3114df27c4d5cc1dda54ed7d0a8389347f2df359cf9Yan, Zheng		WARN_ON(btrfs_root_ref_name_len(leaf, ref) != name_len);
3124df27c4d5cc1dda54ed7d0a8389347f2df359cf9Yan, Zheng		ptr = (unsigned long)(ref + 1);
3134df27c4d5cc1dda54ed7d0a8389347f2df359cf9Yan, Zheng		WARN_ON(memcmp_extent_buffer(leaf, name, ptr, name_len));
3144df27c4d5cc1dda54ed7d0a8389347f2df359cf9Yan, Zheng		*sequence = btrfs_root_ref_sequence(leaf, ref);
3154df27c4d5cc1dda54ed7d0a8389347f2df359cf9Yan, Zheng
3164df27c4d5cc1dda54ed7d0a8389347f2df359cf9Yan, Zheng		ret = btrfs_del_item(trans, tree_root, path);
3174df27c4d5cc1dda54ed7d0a8389347f2df359cf9Yan, Zheng		BUG_ON(ret);
3184df27c4d5cc1dda54ed7d0a8389347f2df359cf9Yan, Zheng	} else
3194df27c4d5cc1dda54ed7d0a8389347f2df359cf9Yan, Zheng		err = -ENOENT;
3204df27c4d5cc1dda54ed7d0a8389347f2df359cf9Yan, Zheng
3214df27c4d5cc1dda54ed7d0a8389347f2df359cf9Yan, Zheng	if (key.type == BTRFS_ROOT_BACKREF_KEY) {
3224df27c4d5cc1dda54ed7d0a8389347f2df359cf9Yan, Zheng		btrfs_release_path(tree_root, path);
3234df27c4d5cc1dda54ed7d0a8389347f2df359cf9Yan, Zheng		key.objectid = ref_id;
3244df27c4d5cc1dda54ed7d0a8389347f2df359cf9Yan, Zheng		key.type = BTRFS_ROOT_REF_KEY;
3254df27c4d5cc1dda54ed7d0a8389347f2df359cf9Yan, Zheng		key.offset = root_id;
3264df27c4d5cc1dda54ed7d0a8389347f2df359cf9Yan, Zheng		goto again;
3274df27c4d5cc1dda54ed7d0a8389347f2df359cf9Yan, Zheng	}
3280660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason
3290660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason	btrfs_free_path(path);
3304df27c4d5cc1dda54ed7d0a8389347f2df359cf9Yan, Zheng	return err;
3310660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason}
3320660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason
333ea9e8b11bd1252dcbc23afefcf1a52ec6aa3c113Chris Masonint btrfs_find_root_ref(struct btrfs_root *tree_root,
334ea9e8b11bd1252dcbc23afefcf1a52ec6aa3c113Chris Mason		   struct btrfs_path *path,
335ea9e8b11bd1252dcbc23afefcf1a52ec6aa3c113Chris Mason		   u64 root_id, u64 ref_id)
336ea9e8b11bd1252dcbc23afefcf1a52ec6aa3c113Chris Mason{
337ea9e8b11bd1252dcbc23afefcf1a52ec6aa3c113Chris Mason	struct btrfs_key key;
338ea9e8b11bd1252dcbc23afefcf1a52ec6aa3c113Chris Mason	int ret;
339ea9e8b11bd1252dcbc23afefcf1a52ec6aa3c113Chris Mason
340ea9e8b11bd1252dcbc23afefcf1a52ec6aa3c113Chris Mason	key.objectid = root_id;
341ea9e8b11bd1252dcbc23afefcf1a52ec6aa3c113Chris Mason	key.type = BTRFS_ROOT_REF_KEY;
342ea9e8b11bd1252dcbc23afefcf1a52ec6aa3c113Chris Mason	key.offset = ref_id;
343ea9e8b11bd1252dcbc23afefcf1a52ec6aa3c113Chris Mason
344ea9e8b11bd1252dcbc23afefcf1a52ec6aa3c113Chris Mason	ret = btrfs_search_slot(NULL, tree_root, &key, path, 0, 0);
345ea9e8b11bd1252dcbc23afefcf1a52ec6aa3c113Chris Mason	return ret;
346ea9e8b11bd1252dcbc23afefcf1a52ec6aa3c113Chris Mason}
347ea9e8b11bd1252dcbc23afefcf1a52ec6aa3c113Chris Mason
3480660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason/*
3490660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason * add a btrfs_root_ref item.  type is either BTRFS_ROOT_REF_KEY
3500660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason * or BTRFS_ROOT_BACKREF_KEY.
3510660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason *
3520660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason * The dirid, sequence, name and name_len refer to the directory entry
3530660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason * that is referencing the root.
3540660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason *
3550660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason * For a forward ref, the root_id is the id of the tree referencing
3560660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason * the root and ref_id is the id of the subvol  or snapshot.
3570660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason *
3580660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason * For a back ref the root_id is the id of the subvol or snapshot and
3590660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason * ref_id is the id of the tree referencing it.
3600660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason */
3610660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Masonint btrfs_add_root_ref(struct btrfs_trans_handle *trans,
3620660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason		       struct btrfs_root *tree_root,
3634df27c4d5cc1dda54ed7d0a8389347f2df359cf9Yan, Zheng		       u64 root_id, u64 ref_id, u64 dirid, u64 sequence,
3640660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason		       const char *name, int name_len)
3650660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason{
3660660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason	struct btrfs_key key;
3670660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason	int ret;
3680660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason	struct btrfs_path *path;
3690660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason	struct btrfs_root_ref *ref;
3700660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason	struct extent_buffer *leaf;
3710660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason	unsigned long ptr;
3720660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason
3730660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason	path = btrfs_alloc_path();
3744df27c4d5cc1dda54ed7d0a8389347f2df359cf9Yan, Zheng	if (!path)
3754df27c4d5cc1dda54ed7d0a8389347f2df359cf9Yan, Zheng		return -ENOMEM;
3760660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason
3770660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason	key.objectid = root_id;
3784df27c4d5cc1dda54ed7d0a8389347f2df359cf9Yan, Zheng	key.type = BTRFS_ROOT_BACKREF_KEY;
3790660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason	key.offset = ref_id;
3804df27c4d5cc1dda54ed7d0a8389347f2df359cf9Yan, Zhengagain:
3810660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason	ret = btrfs_insert_empty_item(trans, tree_root, path, &key,
3820660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason				      sizeof(*ref) + name_len);
3830660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason	BUG_ON(ret);
3840660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason
3850660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason	leaf = path->nodes[0];
3860660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason	ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
3870660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason	btrfs_set_root_ref_dirid(leaf, ref, dirid);
3880660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason	btrfs_set_root_ref_sequence(leaf, ref, sequence);
3890660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason	btrfs_set_root_ref_name_len(leaf, ref, name_len);
3900660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason	ptr = (unsigned long)(ref + 1);
3910660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason	write_extent_buffer(leaf, name, ptr, name_len);
3920660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason	btrfs_mark_buffer_dirty(leaf);
3930660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason
3944df27c4d5cc1dda54ed7d0a8389347f2df359cf9Yan, Zheng	if (key.type == BTRFS_ROOT_BACKREF_KEY) {
3954df27c4d5cc1dda54ed7d0a8389347f2df359cf9Yan, Zheng		btrfs_release_path(tree_root, path);
3964df27c4d5cc1dda54ed7d0a8389347f2df359cf9Yan, Zheng		key.objectid = ref_id;
3974df27c4d5cc1dda54ed7d0a8389347f2df359cf9Yan, Zheng		key.type = BTRFS_ROOT_REF_KEY;
3984df27c4d5cc1dda54ed7d0a8389347f2df359cf9Yan, Zheng		key.offset = root_id;
3994df27c4d5cc1dda54ed7d0a8389347f2df359cf9Yan, Zheng		goto again;
4004df27c4d5cc1dda54ed7d0a8389347f2df359cf9Yan, Zheng	}
4014df27c4d5cc1dda54ed7d0a8389347f2df359cf9Yan, Zheng
4020660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason	btrfs_free_path(path);
4034df27c4d5cc1dda54ed7d0a8389347f2df359cf9Yan, Zheng	return 0;
4040660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason}
405