root-tree.c revision efe120a067c8674a8ae21b194f0e68f098b61ee2
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
198ea05e3a4262b9e6871c349fa3486bcfc72ffd1aAlexander Block#include <linux/uuid.h>
203768f3689fc76ecea17414936dff7a02746a4355Chris Mason#include "ctree.h"
215eda7b5e9b0bed864dd18284c7df9b3c8207dad7Chris Mason#include "transaction.h"
223768f3689fc76ecea17414936dff7a02746a4355Chris Mason#include "disk-io.h"
233768f3689fc76ecea17414936dff7a02746a4355Chris Mason#include "print-tree.h"
243768f3689fc76ecea17414936dff7a02746a4355Chris Mason
25bf4ef67924d87b0addb32f084e83a9283496350eChris Mason/*
268ea05e3a4262b9e6871c349fa3486bcfc72ffd1aAlexander Block * Read a root item from the tree. In case we detect a root item smaller then
278ea05e3a4262b9e6871c349fa3486bcfc72ffd1aAlexander Block * sizeof(root_item), we know it's an old version of the root structure and
288ea05e3a4262b9e6871c349fa3486bcfc72ffd1aAlexander Block * initialize all new fields to zero. The same happens if we detect mismatching
298ea05e3a4262b9e6871c349fa3486bcfc72ffd1aAlexander Block * generation numbers as then we know the root was once mounted with an older
308ea05e3a4262b9e6871c349fa3486bcfc72ffd1aAlexander Block * kernel that was not aware of the root item structure change.
318ea05e3a4262b9e6871c349fa3486bcfc72ffd1aAlexander Block */
32171170c1c5625cab9687ecf6714e09e0c8a6ed3cSergei Trofimovichstatic void btrfs_read_root_item(struct extent_buffer *eb, int slot,
33171170c1c5625cab9687ecf6714e09e0c8a6ed3cSergei Trofimovich				struct btrfs_root_item *item)
348ea05e3a4262b9e6871c349fa3486bcfc72ffd1aAlexander Block{
358ea05e3a4262b9e6871c349fa3486bcfc72ffd1aAlexander Block	uuid_le uuid;
368ea05e3a4262b9e6871c349fa3486bcfc72ffd1aAlexander Block	int len;
378ea05e3a4262b9e6871c349fa3486bcfc72ffd1aAlexander Block	int need_reset = 0;
388ea05e3a4262b9e6871c349fa3486bcfc72ffd1aAlexander Block
398ea05e3a4262b9e6871c349fa3486bcfc72ffd1aAlexander Block	len = btrfs_item_size_nr(eb, slot);
408ea05e3a4262b9e6871c349fa3486bcfc72ffd1aAlexander Block	read_extent_buffer(eb, item, btrfs_item_ptr_offset(eb, slot),
418ea05e3a4262b9e6871c349fa3486bcfc72ffd1aAlexander Block			min_t(int, len, (int)sizeof(*item)));
428ea05e3a4262b9e6871c349fa3486bcfc72ffd1aAlexander Block	if (len < sizeof(*item))
438ea05e3a4262b9e6871c349fa3486bcfc72ffd1aAlexander Block		need_reset = 1;
448ea05e3a4262b9e6871c349fa3486bcfc72ffd1aAlexander Block	if (!need_reset && btrfs_root_generation(item)
458ea05e3a4262b9e6871c349fa3486bcfc72ffd1aAlexander Block		!= btrfs_root_generation_v2(item)) {
468ea05e3a4262b9e6871c349fa3486bcfc72ffd1aAlexander Block		if (btrfs_root_generation_v2(item) != 0) {
47efe120a067c8674a8ae21b194f0e68f098b61ee2Frank Holton			printk(KERN_WARNING "BTRFS: mismatching "
488ea05e3a4262b9e6871c349fa3486bcfc72ffd1aAlexander Block					"generation and generation_v2 "
498ea05e3a4262b9e6871c349fa3486bcfc72ffd1aAlexander Block					"found in root item. This root "
508ea05e3a4262b9e6871c349fa3486bcfc72ffd1aAlexander Block					"was probably mounted with an "
518ea05e3a4262b9e6871c349fa3486bcfc72ffd1aAlexander Block					"older kernel. Resetting all "
528ea05e3a4262b9e6871c349fa3486bcfc72ffd1aAlexander Block					"new fields.\n");
538ea05e3a4262b9e6871c349fa3486bcfc72ffd1aAlexander Block		}
548ea05e3a4262b9e6871c349fa3486bcfc72ffd1aAlexander Block		need_reset = 1;
558ea05e3a4262b9e6871c349fa3486bcfc72ffd1aAlexander Block	}
568ea05e3a4262b9e6871c349fa3486bcfc72ffd1aAlexander Block	if (need_reset) {
578ea05e3a4262b9e6871c349fa3486bcfc72ffd1aAlexander Block		memset(&item->generation_v2, 0,
588ea05e3a4262b9e6871c349fa3486bcfc72ffd1aAlexander Block			sizeof(*item) - offsetof(struct btrfs_root_item,
598ea05e3a4262b9e6871c349fa3486bcfc72ffd1aAlexander Block					generation_v2));
608ea05e3a4262b9e6871c349fa3486bcfc72ffd1aAlexander Block
618ea05e3a4262b9e6871c349fa3486bcfc72ffd1aAlexander Block		uuid_le_gen(&uuid);
628ea05e3a4262b9e6871c349fa3486bcfc72ffd1aAlexander Block		memcpy(item->uuid, uuid.b, BTRFS_UUID_SIZE);
638ea05e3a4262b9e6871c349fa3486bcfc72ffd1aAlexander Block	}
648ea05e3a4262b9e6871c349fa3486bcfc72ffd1aAlexander Block}
658ea05e3a4262b9e6871c349fa3486bcfc72ffd1aAlexander Block
668ea05e3a4262b9e6871c349fa3486bcfc72ffd1aAlexander Block/*
67cb517eabba4f109810dba2e5f37b0dcf22103065Miao Xie * btrfs_find_root - lookup the root by the key.
68cb517eabba4f109810dba2e5f37b0dcf22103065Miao Xie * root: the root of the root tree
69cb517eabba4f109810dba2e5f37b0dcf22103065Miao Xie * search_key: the key to search
70cb517eabba4f109810dba2e5f37b0dcf22103065Miao Xie * path: the path we search
71cb517eabba4f109810dba2e5f37b0dcf22103065Miao Xie * root_item: the root item of the tree we look for
72cb517eabba4f109810dba2e5f37b0dcf22103065Miao Xie * root_key: the reak key of the tree we look for
73cb517eabba4f109810dba2e5f37b0dcf22103065Miao Xie *
74cb517eabba4f109810dba2e5f37b0dcf22103065Miao Xie * If ->offset of 'seach_key' is -1ULL, it means we are not sure the offset
75cb517eabba4f109810dba2e5f37b0dcf22103065Miao Xie * of the search key, just lookup the root with the highest offset for a
76cb517eabba4f109810dba2e5f37b0dcf22103065Miao Xie * given objectid.
77cb517eabba4f109810dba2e5f37b0dcf22103065Miao Xie *
78cb517eabba4f109810dba2e5f37b0dcf22103065Miao Xie * If we find something return 0, otherwise > 0, < 0 on error.
79d352ac68148b69937d39ca5d48bcc4478e118dbfChris Mason */
80cb517eabba4f109810dba2e5f37b0dcf22103065Miao Xieint btrfs_find_root(struct btrfs_root *root, struct btrfs_key *search_key,
81cb517eabba4f109810dba2e5f37b0dcf22103065Miao Xie		    struct btrfs_path *path, struct btrfs_root_item *root_item,
82cb517eabba4f109810dba2e5f37b0dcf22103065Miao Xie		    struct btrfs_key *root_key)
833768f3689fc76ecea17414936dff7a02746a4355Chris Mason{
845f39d397dfbe140a14edecd4e73c34ce23c4f9eeChris Mason	struct btrfs_key found_key;
855f39d397dfbe140a14edecd4e73c34ce23c4f9eeChris Mason	struct extent_buffer *l;
863768f3689fc76ecea17414936dff7a02746a4355Chris Mason	int ret;
873768f3689fc76ecea17414936dff7a02746a4355Chris Mason	int slot;
883768f3689fc76ecea17414936dff7a02746a4355Chris Mason
89cb517eabba4f109810dba2e5f37b0dcf22103065Miao Xie	ret = btrfs_search_slot(NULL, root, search_key, path, 0, 0);
903768f3689fc76ecea17414936dff7a02746a4355Chris Mason	if (ret < 0)
91cb517eabba4f109810dba2e5f37b0dcf22103065Miao Xie		return ret;
925f39d397dfbe140a14edecd4e73c34ce23c4f9eeChris Mason
93cb517eabba4f109810dba2e5f37b0dcf22103065Miao Xie	if (search_key->offset != -1ULL) {	/* the search key is exact */
94cb517eabba4f109810dba2e5f37b0dcf22103065Miao Xie		if (ret > 0)
95cb517eabba4f109810dba2e5f37b0dcf22103065Miao Xie			goto out;
96cb517eabba4f109810dba2e5f37b0dcf22103065Miao Xie	} else {
97cb517eabba4f109810dba2e5f37b0dcf22103065Miao Xie		BUG_ON(ret == 0);		/* Logical error */
98cb517eabba4f109810dba2e5f37b0dcf22103065Miao Xie		if (path->slots[0] == 0)
99cb517eabba4f109810dba2e5f37b0dcf22103065Miao Xie			goto out;
100cb517eabba4f109810dba2e5f37b0dcf22103065Miao Xie		path->slots[0]--;
101cb517eabba4f109810dba2e5f37b0dcf22103065Miao Xie		ret = 0;
10276dda93c6ae2c1dc3e6cde34569d6aca26b0c918Yan, Zheng	}
103cb517eabba4f109810dba2e5f37b0dcf22103065Miao Xie
1045f39d397dfbe140a14edecd4e73c34ce23c4f9eeChris Mason	l = path->nodes[0];
105cb517eabba4f109810dba2e5f37b0dcf22103065Miao Xie	slot = path->slots[0];
106cb517eabba4f109810dba2e5f37b0dcf22103065Miao Xie
1075f39d397dfbe140a14edecd4e73c34ce23c4f9eeChris Mason	btrfs_item_key_to_cpu(l, &found_key, slot);
108cb517eabba4f109810dba2e5f37b0dcf22103065Miao Xie	if (found_key.objectid != search_key->objectid ||
10976dda93c6ae2c1dc3e6cde34569d6aca26b0c918Yan, Zheng	    found_key.type != BTRFS_ROOT_ITEM_KEY) {
1103768f3689fc76ecea17414936dff7a02746a4355Chris Mason		ret = 1;
1113768f3689fc76ecea17414936dff7a02746a4355Chris Mason		goto out;
1123768f3689fc76ecea17414936dff7a02746a4355Chris Mason	}
1138ea05e3a4262b9e6871c349fa3486bcfc72ffd1aAlexander Block
114cb517eabba4f109810dba2e5f37b0dcf22103065Miao Xie	if (root_item)
115cb517eabba4f109810dba2e5f37b0dcf22103065Miao Xie		btrfs_read_root_item(l, slot, root_item);
116cb517eabba4f109810dba2e5f37b0dcf22103065Miao Xie	if (root_key)
117cb517eabba4f109810dba2e5f37b0dcf22103065Miao Xie		memcpy(root_key, &found_key, sizeof(found_key));
1183768f3689fc76ecea17414936dff7a02746a4355Chris Masonout:
119cb517eabba4f109810dba2e5f37b0dcf22103065Miao Xie	btrfs_release_path(path);
1203768f3689fc76ecea17414936dff7a02746a4355Chris Mason	return ret;
1213768f3689fc76ecea17414936dff7a02746a4355Chris Mason}
1223768f3689fc76ecea17414936dff7a02746a4355Chris Mason
123bf5f32ecb6caac52b4d1c083251b3dd4f40a0b7aMark Fashehvoid btrfs_set_root_node(struct btrfs_root_item *item,
124bf5f32ecb6caac52b4d1c083251b3dd4f40a0b7aMark Fasheh			 struct extent_buffer *node)
1255d4f98a28c7d334091c1b7744f48a1acdd2a4ae0Yan Zheng{
1265d4f98a28c7d334091c1b7744f48a1acdd2a4ae0Yan Zheng	btrfs_set_root_bytenr(item, node->start);
1275d4f98a28c7d334091c1b7744f48a1acdd2a4ae0Yan Zheng	btrfs_set_root_level(item, btrfs_header_level(node));
1285d4f98a28c7d334091c1b7744f48a1acdd2a4ae0Yan Zheng	btrfs_set_root_generation(item, btrfs_header_generation(node));
1295d4f98a28c7d334091c1b7744f48a1acdd2a4ae0Yan Zheng}
1305d4f98a28c7d334091c1b7744f48a1acdd2a4ae0Yan Zheng
131d352ac68148b69937d39ca5d48bcc4478e118dbfChris Mason/*
132d352ac68148b69937d39ca5d48bcc4478e118dbfChris Mason * copy the data in 'item' into the btree
133d352ac68148b69937d39ca5d48bcc4478e118dbfChris Mason */
134e089f05c18ab36ed5fa7e2319052e03ab800d518Chris Masonint btrfs_update_root(struct btrfs_trans_handle *trans, struct btrfs_root
135e089f05c18ab36ed5fa7e2319052e03ab800d518Chris Mason		      *root, struct btrfs_key *key, struct btrfs_root_item
136e089f05c18ab36ed5fa7e2319052e03ab800d518Chris Mason		      *item)
1373768f3689fc76ecea17414936dff7a02746a4355Chris Mason{
1385caf2a002901f0fde475371c4bf1c553b51884afChris Mason	struct btrfs_path *path;
1395f39d397dfbe140a14edecd4e73c34ce23c4f9eeChris Mason	struct extent_buffer *l;
1403768f3689fc76ecea17414936dff7a02746a4355Chris Mason	int ret;
1413768f3689fc76ecea17414936dff7a02746a4355Chris Mason	int slot;
1425f39d397dfbe140a14edecd4e73c34ce23c4f9eeChris Mason	unsigned long ptr;
1438ea05e3a4262b9e6871c349fa3486bcfc72ffd1aAlexander Block	int old_len;
1443768f3689fc76ecea17414936dff7a02746a4355Chris Mason
1455caf2a002901f0fde475371c4bf1c553b51884afChris Mason	path = btrfs_alloc_path();
146b45a9d8b48e5ce534bd222007c43cbf374544f0bJeff Mahoney	if (!path)
147b45a9d8b48e5ce534bd222007c43cbf374544f0bJeff Mahoney		return -ENOMEM;
148b45a9d8b48e5ce534bd222007c43cbf374544f0bJeff Mahoney
1495caf2a002901f0fde475371c4bf1c553b51884afChris Mason	ret = btrfs_search_slot(trans, root, key, path, 0, 1);
150005d6427ac4f276d937a36ca6a1d62b181ed70bfDavid Sterba	if (ret < 0) {
151005d6427ac4f276d937a36ca6a1d62b181ed70bfDavid Sterba		btrfs_abort_transaction(trans, root, ret);
152005d6427ac4f276d937a36ca6a1d62b181ed70bfDavid Sterba		goto out;
153005d6427ac4f276d937a36ca6a1d62b181ed70bfDavid Sterba	}
154d666746207a01546e55bdaa4b721d1890faaf6e8Chris Mason
155d666746207a01546e55bdaa4b721d1890faaf6e8Chris Mason	if (ret != 0) {
156d666746207a01546e55bdaa4b721d1890faaf6e8Chris Mason		btrfs_print_leaf(root, path->nodes[0]);
157efe120a067c8674a8ae21b194f0e68f098b61ee2Frank Holton		btrfs_crit(root->fs_info, "unable to update root key %llu %u %llu",
158c1c9ff7c94e83fae89a742df74db51156869bad5Geert Uytterhoeven		       key->objectid, key->type, key->offset);
159d666746207a01546e55bdaa4b721d1890faaf6e8Chris Mason		BUG_ON(1);
160d666746207a01546e55bdaa4b721d1890faaf6e8Chris Mason	}
161d666746207a01546e55bdaa4b721d1890faaf6e8Chris Mason
1625f39d397dfbe140a14edecd4e73c34ce23c4f9eeChris Mason	l = path->nodes[0];
1635caf2a002901f0fde475371c4bf1c553b51884afChris Mason	slot = path->slots[0];
1645f39d397dfbe140a14edecd4e73c34ce23c4f9eeChris Mason	ptr = btrfs_item_ptr_offset(l, slot);
1658ea05e3a4262b9e6871c349fa3486bcfc72ffd1aAlexander Block	old_len = btrfs_item_size_nr(l, slot);
1668ea05e3a4262b9e6871c349fa3486bcfc72ffd1aAlexander Block
1678ea05e3a4262b9e6871c349fa3486bcfc72ffd1aAlexander Block	/*
1688ea05e3a4262b9e6871c349fa3486bcfc72ffd1aAlexander Block	 * If this is the first time we update the root item which originated
1698ea05e3a4262b9e6871c349fa3486bcfc72ffd1aAlexander Block	 * from an older kernel, we need to enlarge the item size to make room
1708ea05e3a4262b9e6871c349fa3486bcfc72ffd1aAlexander Block	 * for the added fields.
1718ea05e3a4262b9e6871c349fa3486bcfc72ffd1aAlexander Block	 */
1728ea05e3a4262b9e6871c349fa3486bcfc72ffd1aAlexander Block	if (old_len < sizeof(*item)) {
1738ea05e3a4262b9e6871c349fa3486bcfc72ffd1aAlexander Block		btrfs_release_path(path);
1748ea05e3a4262b9e6871c349fa3486bcfc72ffd1aAlexander Block		ret = btrfs_search_slot(trans, root, key, path,
1758ea05e3a4262b9e6871c349fa3486bcfc72ffd1aAlexander Block				-1, 1);
176005d6427ac4f276d937a36ca6a1d62b181ed70bfDavid Sterba		if (ret < 0) {
177005d6427ac4f276d937a36ca6a1d62b181ed70bfDavid Sterba			btrfs_abort_transaction(trans, root, ret);
178005d6427ac4f276d937a36ca6a1d62b181ed70bfDavid Sterba			goto out;
179005d6427ac4f276d937a36ca6a1d62b181ed70bfDavid Sterba		}
180005d6427ac4f276d937a36ca6a1d62b181ed70bfDavid Sterba
1818ea05e3a4262b9e6871c349fa3486bcfc72ffd1aAlexander Block		ret = btrfs_del_item(trans, root, path);
182005d6427ac4f276d937a36ca6a1d62b181ed70bfDavid Sterba		if (ret < 0) {
183005d6427ac4f276d937a36ca6a1d62b181ed70bfDavid Sterba			btrfs_abort_transaction(trans, root, ret);
184005d6427ac4f276d937a36ca6a1d62b181ed70bfDavid Sterba			goto out;
185005d6427ac4f276d937a36ca6a1d62b181ed70bfDavid Sterba		}
1868ea05e3a4262b9e6871c349fa3486bcfc72ffd1aAlexander Block		btrfs_release_path(path);
1878ea05e3a4262b9e6871c349fa3486bcfc72ffd1aAlexander Block		ret = btrfs_insert_empty_item(trans, root, path,
1888ea05e3a4262b9e6871c349fa3486bcfc72ffd1aAlexander Block				key, sizeof(*item));
189005d6427ac4f276d937a36ca6a1d62b181ed70bfDavid Sterba		if (ret < 0) {
190005d6427ac4f276d937a36ca6a1d62b181ed70bfDavid Sterba			btrfs_abort_transaction(trans, root, ret);
191005d6427ac4f276d937a36ca6a1d62b181ed70bfDavid Sterba			goto out;
192005d6427ac4f276d937a36ca6a1d62b181ed70bfDavid Sterba		}
1938ea05e3a4262b9e6871c349fa3486bcfc72ffd1aAlexander Block		l = path->nodes[0];
1948ea05e3a4262b9e6871c349fa3486bcfc72ffd1aAlexander Block		slot = path->slots[0];
1958ea05e3a4262b9e6871c349fa3486bcfc72ffd1aAlexander Block		ptr = btrfs_item_ptr_offset(l, slot);
1968ea05e3a4262b9e6871c349fa3486bcfc72ffd1aAlexander Block	}
1978ea05e3a4262b9e6871c349fa3486bcfc72ffd1aAlexander Block
1988ea05e3a4262b9e6871c349fa3486bcfc72ffd1aAlexander Block	/*
1998ea05e3a4262b9e6871c349fa3486bcfc72ffd1aAlexander Block	 * Update generation_v2 so at the next mount we know the new root
2008ea05e3a4262b9e6871c349fa3486bcfc72ffd1aAlexander Block	 * fields are valid.
2018ea05e3a4262b9e6871c349fa3486bcfc72ffd1aAlexander Block	 */
2028ea05e3a4262b9e6871c349fa3486bcfc72ffd1aAlexander Block	btrfs_set_root_generation_v2(item, btrfs_root_generation(item));
2038ea05e3a4262b9e6871c349fa3486bcfc72ffd1aAlexander Block
2045f39d397dfbe140a14edecd4e73c34ce23c4f9eeChris Mason	write_extent_buffer(l, item, ptr, sizeof(*item));
2055caf2a002901f0fde475371c4bf1c553b51884afChris Mason	btrfs_mark_buffer_dirty(path->nodes[0]);
2063768f3689fc76ecea17414936dff7a02746a4355Chris Masonout:
2075caf2a002901f0fde475371c4bf1c553b51884afChris Mason	btrfs_free_path(path);
2083768f3689fc76ecea17414936dff7a02746a4355Chris Mason	return ret;
2093768f3689fc76ecea17414936dff7a02746a4355Chris Mason}
2103768f3689fc76ecea17414936dff7a02746a4355Chris Mason
211d16cb050e5b1c3a9d754fed7098eefb8237877d1Jeff Mahoneyint btrfs_insert_root(struct btrfs_trans_handle *trans, struct btrfs_root *root,
212d16cb050e5b1c3a9d754fed7098eefb8237877d1Jeff Mahoney		      struct btrfs_key *key, struct btrfs_root_item *item)
2133768f3689fc76ecea17414936dff7a02746a4355Chris Mason{
2148ea05e3a4262b9e6871c349fa3486bcfc72ffd1aAlexander Block	/*
2158ea05e3a4262b9e6871c349fa3486bcfc72ffd1aAlexander Block	 * Make sure generation v1 and v2 match. See update_root for details.
2168ea05e3a4262b9e6871c349fa3486bcfc72ffd1aAlexander Block	 */
2178ea05e3a4262b9e6871c349fa3486bcfc72ffd1aAlexander Block	btrfs_set_root_generation_v2(item, btrfs_root_generation(item));
218d16cb050e5b1c3a9d754fed7098eefb8237877d1Jeff Mahoney	return btrfs_insert_item(trans, root, key, item, sizeof(*item));
2193768f3689fc76ecea17414936dff7a02746a4355Chris Mason}
2203768f3689fc76ecea17414936dff7a02746a4355Chris Mason
22176dda93c6ae2c1dc3e6cde34569d6aca26b0c918Yan, Zhengint btrfs_find_orphan_roots(struct btrfs_root *tree_root)
22276dda93c6ae2c1dc3e6cde34569d6aca26b0c918Yan, Zheng{
22376dda93c6ae2c1dc3e6cde34569d6aca26b0c918Yan, Zheng	struct extent_buffer *leaf;
22476dda93c6ae2c1dc3e6cde34569d6aca26b0c918Yan, Zheng	struct btrfs_path *path;
22576dda93c6ae2c1dc3e6cde34569d6aca26b0c918Yan, Zheng	struct btrfs_key key;
226d68fc57b7e3245cfacf2e3b47acfed1946a11786Yan, Zheng	struct btrfs_key root_key;
227d68fc57b7e3245cfacf2e3b47acfed1946a11786Yan, Zheng	struct btrfs_root *root;
22876dda93c6ae2c1dc3e6cde34569d6aca26b0c918Yan, Zheng	int err = 0;
22976dda93c6ae2c1dc3e6cde34569d6aca26b0c918Yan, Zheng	int ret;
23068a7342c51c950428d90cd15da898c63d6c33267Josef Bacik	bool can_recover = true;
23168a7342c51c950428d90cd15da898c63d6c33267Josef Bacik
23268a7342c51c950428d90cd15da898c63d6c33267Josef Bacik	if (tree_root->fs_info->sb->s_flags & MS_RDONLY)
23368a7342c51c950428d90cd15da898c63d6c33267Josef Bacik		can_recover = false;
23476dda93c6ae2c1dc3e6cde34569d6aca26b0c918Yan, Zheng
23576dda93c6ae2c1dc3e6cde34569d6aca26b0c918Yan, Zheng	path = btrfs_alloc_path();
23676dda93c6ae2c1dc3e6cde34569d6aca26b0c918Yan, Zheng	if (!path)
23776dda93c6ae2c1dc3e6cde34569d6aca26b0c918Yan, Zheng		return -ENOMEM;
23876dda93c6ae2c1dc3e6cde34569d6aca26b0c918Yan, Zheng
23976dda93c6ae2c1dc3e6cde34569d6aca26b0c918Yan, Zheng	key.objectid = BTRFS_ORPHAN_OBJECTID;
24076dda93c6ae2c1dc3e6cde34569d6aca26b0c918Yan, Zheng	key.type = BTRFS_ORPHAN_ITEM_KEY;
24176dda93c6ae2c1dc3e6cde34569d6aca26b0c918Yan, Zheng	key.offset = 0;
24276dda93c6ae2c1dc3e6cde34569d6aca26b0c918Yan, Zheng
243d68fc57b7e3245cfacf2e3b47acfed1946a11786Yan, Zheng	root_key.type = BTRFS_ROOT_ITEM_KEY;
244d68fc57b7e3245cfacf2e3b47acfed1946a11786Yan, Zheng	root_key.offset = (u64)-1;
245d68fc57b7e3245cfacf2e3b47acfed1946a11786Yan, Zheng
24676dda93c6ae2c1dc3e6cde34569d6aca26b0c918Yan, Zheng	while (1) {
24776dda93c6ae2c1dc3e6cde34569d6aca26b0c918Yan, Zheng		ret = btrfs_search_slot(NULL, tree_root, &key, path, 0, 0);
24876dda93c6ae2c1dc3e6cde34569d6aca26b0c918Yan, Zheng		if (ret < 0) {
24976dda93c6ae2c1dc3e6cde34569d6aca26b0c918Yan, Zheng			err = ret;
25076dda93c6ae2c1dc3e6cde34569d6aca26b0c918Yan, Zheng			break;
25176dda93c6ae2c1dc3e6cde34569d6aca26b0c918Yan, Zheng		}
25276dda93c6ae2c1dc3e6cde34569d6aca26b0c918Yan, Zheng
25376dda93c6ae2c1dc3e6cde34569d6aca26b0c918Yan, Zheng		leaf = path->nodes[0];
25476dda93c6ae2c1dc3e6cde34569d6aca26b0c918Yan, Zheng		if (path->slots[0] >= btrfs_header_nritems(leaf)) {
25576dda93c6ae2c1dc3e6cde34569d6aca26b0c918Yan, Zheng			ret = btrfs_next_leaf(tree_root, path);
25676dda93c6ae2c1dc3e6cde34569d6aca26b0c918Yan, Zheng			if (ret < 0)
25776dda93c6ae2c1dc3e6cde34569d6aca26b0c918Yan, Zheng				err = ret;
25876dda93c6ae2c1dc3e6cde34569d6aca26b0c918Yan, Zheng			if (ret != 0)
25976dda93c6ae2c1dc3e6cde34569d6aca26b0c918Yan, Zheng				break;
26076dda93c6ae2c1dc3e6cde34569d6aca26b0c918Yan, Zheng			leaf = path->nodes[0];
26176dda93c6ae2c1dc3e6cde34569d6aca26b0c918Yan, Zheng		}
26276dda93c6ae2c1dc3e6cde34569d6aca26b0c918Yan, Zheng
26376dda93c6ae2c1dc3e6cde34569d6aca26b0c918Yan, Zheng		btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
264b3b4aa74b58bded927f579fff787fb6fa1c0393cDavid Sterba		btrfs_release_path(path);
26576dda93c6ae2c1dc3e6cde34569d6aca26b0c918Yan, Zheng
26676dda93c6ae2c1dc3e6cde34569d6aca26b0c918Yan, Zheng		if (key.objectid != BTRFS_ORPHAN_OBJECTID ||
26776dda93c6ae2c1dc3e6cde34569d6aca26b0c918Yan, Zheng		    key.type != BTRFS_ORPHAN_ITEM_KEY)
26876dda93c6ae2c1dc3e6cde34569d6aca26b0c918Yan, Zheng			break;
26976dda93c6ae2c1dc3e6cde34569d6aca26b0c918Yan, Zheng
270d68fc57b7e3245cfacf2e3b47acfed1946a11786Yan, Zheng		root_key.objectid = key.offset;
271d68fc57b7e3245cfacf2e3b47acfed1946a11786Yan, Zheng		key.offset++;
272d68fc57b7e3245cfacf2e3b47acfed1946a11786Yan, Zheng
273cb517eabba4f109810dba2e5f37b0dcf22103065Miao Xie		root = btrfs_read_fs_root(tree_root, &root_key);
27468a7342c51c950428d90cd15da898c63d6c33267Josef Bacik		err = PTR_RET(root);
27568a7342c51c950428d90cd15da898c63d6c33267Josef Bacik		if (err && err != -ENOENT) {
276cb517eabba4f109810dba2e5f37b0dcf22103065Miao Xie			break;
27768a7342c51c950428d90cd15da898c63d6c33267Josef Bacik		} else if (err == -ENOENT) {
27868a7342c51c950428d90cd15da898c63d6c33267Josef Bacik			struct btrfs_trans_handle *trans;
27968a7342c51c950428d90cd15da898c63d6c33267Josef Bacik
28068a7342c51c950428d90cd15da898c63d6c33267Josef Bacik			btrfs_release_path(path);
28168a7342c51c950428d90cd15da898c63d6c33267Josef Bacik
28268a7342c51c950428d90cd15da898c63d6c33267Josef Bacik			trans = btrfs_join_transaction(tree_root);
28368a7342c51c950428d90cd15da898c63d6c33267Josef Bacik			if (IS_ERR(trans)) {
28468a7342c51c950428d90cd15da898c63d6c33267Josef Bacik				err = PTR_ERR(trans);
28568a7342c51c950428d90cd15da898c63d6c33267Josef Bacik				btrfs_error(tree_root->fs_info, err,
28668a7342c51c950428d90cd15da898c63d6c33267Josef Bacik					    "Failed to start trans to delete "
28768a7342c51c950428d90cd15da898c63d6c33267Josef Bacik					    "orphan item");
28868a7342c51c950428d90cd15da898c63d6c33267Josef Bacik				break;
28968a7342c51c950428d90cd15da898c63d6c33267Josef Bacik			}
29068a7342c51c950428d90cd15da898c63d6c33267Josef Bacik			err = btrfs_del_orphan_item(trans, tree_root,
29168a7342c51c950428d90cd15da898c63d6c33267Josef Bacik						    root_key.objectid);
29268a7342c51c950428d90cd15da898c63d6c33267Josef Bacik			btrfs_end_transaction(trans, tree_root);
29368a7342c51c950428d90cd15da898c63d6c33267Josef Bacik			if (err) {
29468a7342c51c950428d90cd15da898c63d6c33267Josef Bacik				btrfs_error(tree_root->fs_info, err,
29568a7342c51c950428d90cd15da898c63d6c33267Josef Bacik					    "Failed to delete root orphan "
29668a7342c51c950428d90cd15da898c63d6c33267Josef Bacik					    "item");
29768a7342c51c950428d90cd15da898c63d6c33267Josef Bacik				break;
29868a7342c51c950428d90cd15da898c63d6c33267Josef Bacik			}
29968a7342c51c950428d90cd15da898c63d6c33267Josef Bacik			continue;
300cb517eabba4f109810dba2e5f37b0dcf22103065Miao Xie		}
301cb517eabba4f109810dba2e5f37b0dcf22103065Miao Xie
302cb517eabba4f109810dba2e5f37b0dcf22103065Miao Xie		err = btrfs_init_fs_root(root);
303cb517eabba4f109810dba2e5f37b0dcf22103065Miao Xie		if (err) {
304cb517eabba4f109810dba2e5f37b0dcf22103065Miao Xie			btrfs_free_fs_root(root);
30576dda93c6ae2c1dc3e6cde34569d6aca26b0c918Yan, Zheng			break;
30676dda93c6ae2c1dc3e6cde34569d6aca26b0c918Yan, Zheng		}
30776dda93c6ae2c1dc3e6cde34569d6aca26b0c918Yan, Zheng
308cb517eabba4f109810dba2e5f37b0dcf22103065Miao Xie		root->orphan_item_inserted = 1;
309cb517eabba4f109810dba2e5f37b0dcf22103065Miao Xie
310cb517eabba4f109810dba2e5f37b0dcf22103065Miao Xie		err = btrfs_insert_fs_root(root->fs_info, root);
311cb517eabba4f109810dba2e5f37b0dcf22103065Miao Xie		if (err) {
312cb517eabba4f109810dba2e5f37b0dcf22103065Miao Xie			BUG_ON(err == -EEXIST);
313cb517eabba4f109810dba2e5f37b0dcf22103065Miao Xie			btrfs_free_fs_root(root);
314d68fc57b7e3245cfacf2e3b47acfed1946a11786Yan, Zheng			break;
315d68fc57b7e3245cfacf2e3b47acfed1946a11786Yan, Zheng		}
31614927d95464956ffe8af4278331a6bfea94ab780Miao Xie
31714927d95464956ffe8af4278331a6bfea94ab780Miao Xie		if (btrfs_root_refs(&root->root_item) == 0)
31814927d95464956ffe8af4278331a6bfea94ab780Miao Xie			btrfs_add_dead_root(root);
31976dda93c6ae2c1dc3e6cde34569d6aca26b0c918Yan, Zheng	}
32076dda93c6ae2c1dc3e6cde34569d6aca26b0c918Yan, Zheng
32176dda93c6ae2c1dc3e6cde34569d6aca26b0c918Yan, Zheng	btrfs_free_path(path);
32276dda93c6ae2c1dc3e6cde34569d6aca26b0c918Yan, Zheng	return err;
32376dda93c6ae2c1dc3e6cde34569d6aca26b0c918Yan, Zheng}
32476dda93c6ae2c1dc3e6cde34569d6aca26b0c918Yan, Zheng
325d352ac68148b69937d39ca5d48bcc4478e118dbfChris Mason/* drop the root item for 'key' from 'root' */
326e089f05c18ab36ed5fa7e2319052e03ab800d518Chris Masonint btrfs_del_root(struct btrfs_trans_handle *trans, struct btrfs_root *root,
327e089f05c18ab36ed5fa7e2319052e03ab800d518Chris Mason		   struct btrfs_key *key)
3283768f3689fc76ecea17414936dff7a02746a4355Chris Mason{
3295caf2a002901f0fde475371c4bf1c553b51884afChris Mason	struct btrfs_path *path;
3303768f3689fc76ecea17414936dff7a02746a4355Chris Mason	int ret;
3313768f3689fc76ecea17414936dff7a02746a4355Chris Mason
3325caf2a002901f0fde475371c4bf1c553b51884afChris Mason	path = btrfs_alloc_path();
333db5b493ac78e46c7b6bad22cd25d8041564cd8eaTsutomu Itoh	if (!path)
334db5b493ac78e46c7b6bad22cd25d8041564cd8eaTsutomu Itoh		return -ENOMEM;
3355caf2a002901f0fde475371c4bf1c553b51884afChris Mason	ret = btrfs_search_slot(trans, root, key, path, -1, 1);
3363768f3689fc76ecea17414936dff7a02746a4355Chris Mason	if (ret < 0)
3373768f3689fc76ecea17414936dff7a02746a4355Chris Mason		goto out;
338edbd8d4efe4ddaf29a175ae504e2c9a05a96ebeeChris Mason
3393768f3689fc76ecea17414936dff7a02746a4355Chris Mason	BUG_ON(ret != 0);
340c5739bba5260a59cebd20a51a55080592c8d3b07Chris Mason
3415eda7b5e9b0bed864dd18284c7df9b3c8207dad7Chris Mason	ret = btrfs_del_item(trans, root, path);
3423768f3689fc76ecea17414936dff7a02746a4355Chris Masonout:
3435caf2a002901f0fde475371c4bf1c553b51884afChris Mason	btrfs_free_path(path);
3443768f3689fc76ecea17414936dff7a02746a4355Chris Mason	return ret;
3453768f3689fc76ecea17414936dff7a02746a4355Chris Mason}
3460660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason
3470660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Masonint btrfs_del_root_ref(struct btrfs_trans_handle *trans,
3480660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason		       struct btrfs_root *tree_root,
3494df27c4d5cc1dda54ed7d0a8389347f2df359cf9Yan, Zheng		       u64 root_id, u64 ref_id, u64 dirid, u64 *sequence,
3504df27c4d5cc1dda54ed7d0a8389347f2df359cf9Yan, Zheng		       const char *name, int name_len)
3514df27c4d5cc1dda54ed7d0a8389347f2df359cf9Yan, Zheng
3520660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason{
3534df27c4d5cc1dda54ed7d0a8389347f2df359cf9Yan, Zheng	struct btrfs_path *path;
3544df27c4d5cc1dda54ed7d0a8389347f2df359cf9Yan, Zheng	struct btrfs_root_ref *ref;
3554df27c4d5cc1dda54ed7d0a8389347f2df359cf9Yan, Zheng	struct extent_buffer *leaf;
3560660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason	struct btrfs_key key;
3574df27c4d5cc1dda54ed7d0a8389347f2df359cf9Yan, Zheng	unsigned long ptr;
3584df27c4d5cc1dda54ed7d0a8389347f2df359cf9Yan, Zheng	int err = 0;
3590660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason	int ret;
3600660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason
3610660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason	path = btrfs_alloc_path();
3624df27c4d5cc1dda54ed7d0a8389347f2df359cf9Yan, Zheng	if (!path)
3634df27c4d5cc1dda54ed7d0a8389347f2df359cf9Yan, Zheng		return -ENOMEM;
3640660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason
3650660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason	key.objectid = root_id;
3664df27c4d5cc1dda54ed7d0a8389347f2df359cf9Yan, Zheng	key.type = BTRFS_ROOT_BACKREF_KEY;
3670660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason	key.offset = ref_id;
3684df27c4d5cc1dda54ed7d0a8389347f2df359cf9Yan, Zhengagain:
3690660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason	ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1);
3704df27c4d5cc1dda54ed7d0a8389347f2df359cf9Yan, Zheng	BUG_ON(ret < 0);
3714df27c4d5cc1dda54ed7d0a8389347f2df359cf9Yan, Zheng	if (ret == 0) {
3724df27c4d5cc1dda54ed7d0a8389347f2df359cf9Yan, Zheng		leaf = path->nodes[0];
3734df27c4d5cc1dda54ed7d0a8389347f2df359cf9Yan, Zheng		ref = btrfs_item_ptr(leaf, path->slots[0],
3744df27c4d5cc1dda54ed7d0a8389347f2df359cf9Yan, Zheng				     struct btrfs_root_ref);
3754df27c4d5cc1dda54ed7d0a8389347f2df359cf9Yan, Zheng
3764df27c4d5cc1dda54ed7d0a8389347f2df359cf9Yan, Zheng		WARN_ON(btrfs_root_ref_dirid(leaf, ref) != dirid);
3774df27c4d5cc1dda54ed7d0a8389347f2df359cf9Yan, Zheng		WARN_ON(btrfs_root_ref_name_len(leaf, ref) != name_len);
3784df27c4d5cc1dda54ed7d0a8389347f2df359cf9Yan, Zheng		ptr = (unsigned long)(ref + 1);
3794df27c4d5cc1dda54ed7d0a8389347f2df359cf9Yan, Zheng		WARN_ON(memcmp_extent_buffer(leaf, name, ptr, name_len));
3804df27c4d5cc1dda54ed7d0a8389347f2df359cf9Yan, Zheng		*sequence = btrfs_root_ref_sequence(leaf, ref);
3814df27c4d5cc1dda54ed7d0a8389347f2df359cf9Yan, Zheng
3824df27c4d5cc1dda54ed7d0a8389347f2df359cf9Yan, Zheng		ret = btrfs_del_item(trans, tree_root, path);
38365a246c5ffe3b487a001de025816326939e63362Tsutomu Itoh		if (ret) {
38465a246c5ffe3b487a001de025816326939e63362Tsutomu Itoh			err = ret;
38565a246c5ffe3b487a001de025816326939e63362Tsutomu Itoh			goto out;
38665a246c5ffe3b487a001de025816326939e63362Tsutomu Itoh		}
3874df27c4d5cc1dda54ed7d0a8389347f2df359cf9Yan, Zheng	} else
3884df27c4d5cc1dda54ed7d0a8389347f2df359cf9Yan, Zheng		err = -ENOENT;
3894df27c4d5cc1dda54ed7d0a8389347f2df359cf9Yan, Zheng
3904df27c4d5cc1dda54ed7d0a8389347f2df359cf9Yan, Zheng	if (key.type == BTRFS_ROOT_BACKREF_KEY) {
391b3b4aa74b58bded927f579fff787fb6fa1c0393cDavid Sterba		btrfs_release_path(path);
3924df27c4d5cc1dda54ed7d0a8389347f2df359cf9Yan, Zheng		key.objectid = ref_id;
3934df27c4d5cc1dda54ed7d0a8389347f2df359cf9Yan, Zheng		key.type = BTRFS_ROOT_REF_KEY;
3944df27c4d5cc1dda54ed7d0a8389347f2df359cf9Yan, Zheng		key.offset = root_id;
3954df27c4d5cc1dda54ed7d0a8389347f2df359cf9Yan, Zheng		goto again;
3964df27c4d5cc1dda54ed7d0a8389347f2df359cf9Yan, Zheng	}
3970660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason
39865a246c5ffe3b487a001de025816326939e63362Tsutomu Itohout:
3990660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason	btrfs_free_path(path);
4004df27c4d5cc1dda54ed7d0a8389347f2df359cf9Yan, Zheng	return err;
4010660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason}
4020660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason
4030660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason/*
4040660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason * add a btrfs_root_ref item.  type is either BTRFS_ROOT_REF_KEY
4050660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason * or BTRFS_ROOT_BACKREF_KEY.
4060660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason *
4070660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason * The dirid, sequence, name and name_len refer to the directory entry
4080660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason * that is referencing the root.
4090660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason *
4100660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason * For a forward ref, the root_id is the id of the tree referencing
4110660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason * the root and ref_id is the id of the subvol  or snapshot.
4120660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason *
4130660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason * For a back ref the root_id is the id of the subvol or snapshot and
4140660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason * ref_id is the id of the tree referencing it.
41579787eaab46121d4713ed03c8fc63b9ec3eaec76Jeff Mahoney *
41679787eaab46121d4713ed03c8fc63b9ec3eaec76Jeff Mahoney * Will return 0, -ENOMEM, or anything from the CoW path
4170660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason */
4180660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Masonint btrfs_add_root_ref(struct btrfs_trans_handle *trans,
4190660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason		       struct btrfs_root *tree_root,
4204df27c4d5cc1dda54ed7d0a8389347f2df359cf9Yan, Zheng		       u64 root_id, u64 ref_id, u64 dirid, u64 sequence,
4210660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason		       const char *name, int name_len)
4220660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason{
4230660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason	struct btrfs_key key;
4240660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason	int ret;
4250660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason	struct btrfs_path *path;
4260660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason	struct btrfs_root_ref *ref;
4270660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason	struct extent_buffer *leaf;
4280660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason	unsigned long ptr;
4290660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason
4300660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason	path = btrfs_alloc_path();
4314df27c4d5cc1dda54ed7d0a8389347f2df359cf9Yan, Zheng	if (!path)
4324df27c4d5cc1dda54ed7d0a8389347f2df359cf9Yan, Zheng		return -ENOMEM;
4330660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason
4340660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason	key.objectid = root_id;
4354df27c4d5cc1dda54ed7d0a8389347f2df359cf9Yan, Zheng	key.type = BTRFS_ROOT_BACKREF_KEY;
4360660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason	key.offset = ref_id;
4374df27c4d5cc1dda54ed7d0a8389347f2df359cf9Yan, Zhengagain:
4380660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason	ret = btrfs_insert_empty_item(trans, tree_root, path, &key,
4390660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason				      sizeof(*ref) + name_len);
44079787eaab46121d4713ed03c8fc63b9ec3eaec76Jeff Mahoney	if (ret) {
44179787eaab46121d4713ed03c8fc63b9ec3eaec76Jeff Mahoney		btrfs_abort_transaction(trans, tree_root, ret);
44279787eaab46121d4713ed03c8fc63b9ec3eaec76Jeff Mahoney		btrfs_free_path(path);
44379787eaab46121d4713ed03c8fc63b9ec3eaec76Jeff Mahoney		return ret;
44479787eaab46121d4713ed03c8fc63b9ec3eaec76Jeff Mahoney	}
4450660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason
4460660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason	leaf = path->nodes[0];
4470660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason	ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
4480660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason	btrfs_set_root_ref_dirid(leaf, ref, dirid);
4490660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason	btrfs_set_root_ref_sequence(leaf, ref, sequence);
4500660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason	btrfs_set_root_ref_name_len(leaf, ref, name_len);
4510660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason	ptr = (unsigned long)(ref + 1);
4520660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason	write_extent_buffer(leaf, name, ptr, name_len);
4530660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason	btrfs_mark_buffer_dirty(leaf);
4540660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason
4554df27c4d5cc1dda54ed7d0a8389347f2df359cf9Yan, Zheng	if (key.type == BTRFS_ROOT_BACKREF_KEY) {
456b3b4aa74b58bded927f579fff787fb6fa1c0393cDavid Sterba		btrfs_release_path(path);
4574df27c4d5cc1dda54ed7d0a8389347f2df359cf9Yan, Zheng		key.objectid = ref_id;
4584df27c4d5cc1dda54ed7d0a8389347f2df359cf9Yan, Zheng		key.type = BTRFS_ROOT_REF_KEY;
4594df27c4d5cc1dda54ed7d0a8389347f2df359cf9Yan, Zheng		key.offset = root_id;
4604df27c4d5cc1dda54ed7d0a8389347f2df359cf9Yan, Zheng		goto again;
4614df27c4d5cc1dda54ed7d0a8389347f2df359cf9Yan, Zheng	}
4624df27c4d5cc1dda54ed7d0a8389347f2df359cf9Yan, Zheng
4630660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason	btrfs_free_path(path);
4644df27c4d5cc1dda54ed7d0a8389347f2df359cf9Yan, Zheng	return 0;
4650660b5af3f7ac0fac69de975914e1f4a3a586fb3Chris Mason}
46608fe4db170b4193603d9d31f40ebaf652d07ac9cLi Zefan
46708fe4db170b4193603d9d31f40ebaf652d07ac9cLi Zefan/*
46808fe4db170b4193603d9d31f40ebaf652d07ac9cLi Zefan * Old btrfs forgets to init root_item->flags and root_item->byte_limit
46908fe4db170b4193603d9d31f40ebaf652d07ac9cLi Zefan * for subvolumes. To work around this problem, we steal a bit from
47008fe4db170b4193603d9d31f40ebaf652d07ac9cLi Zefan * root_item->inode_item->flags, and use it to indicate if those fields
47108fe4db170b4193603d9d31f40ebaf652d07ac9cLi Zefan * have been properly initialized.
47208fe4db170b4193603d9d31f40ebaf652d07ac9cLi Zefan */
47308fe4db170b4193603d9d31f40ebaf652d07ac9cLi Zefanvoid btrfs_check_and_init_root_item(struct btrfs_root_item *root_item)
47408fe4db170b4193603d9d31f40ebaf652d07ac9cLi Zefan{
4753cae210fa529d69cb25c2a3c491f29dab687b245Qu Wenruo	u64 inode_flags = btrfs_stack_inode_flags(&root_item->inode);
47608fe4db170b4193603d9d31f40ebaf652d07ac9cLi Zefan
47708fe4db170b4193603d9d31f40ebaf652d07ac9cLi Zefan	if (!(inode_flags & BTRFS_INODE_ROOT_ITEM_INIT)) {
47808fe4db170b4193603d9d31f40ebaf652d07ac9cLi Zefan		inode_flags |= BTRFS_INODE_ROOT_ITEM_INIT;
4793cae210fa529d69cb25c2a3c491f29dab687b245Qu Wenruo		btrfs_set_stack_inode_flags(&root_item->inode, inode_flags);
4803cae210fa529d69cb25c2a3c491f29dab687b245Qu Wenruo		btrfs_set_root_flags(root_item, 0);
4813cae210fa529d69cb25c2a3c491f29dab687b245Qu Wenruo		btrfs_set_root_limit(root_item, 0);
48208fe4db170b4193603d9d31f40ebaf652d07ac9cLi Zefan	}
48308fe4db170b4193603d9d31f40ebaf652d07ac9cLi Zefan}
4848ea05e3a4262b9e6871c349fa3486bcfc72ffd1aAlexander Block
4858ea05e3a4262b9e6871c349fa3486bcfc72ffd1aAlexander Blockvoid btrfs_update_root_times(struct btrfs_trans_handle *trans,
4868ea05e3a4262b9e6871c349fa3486bcfc72ffd1aAlexander Block			     struct btrfs_root *root)
4878ea05e3a4262b9e6871c349fa3486bcfc72ffd1aAlexander Block{
4888ea05e3a4262b9e6871c349fa3486bcfc72ffd1aAlexander Block	struct btrfs_root_item *item = &root->root_item;
4898ea05e3a4262b9e6871c349fa3486bcfc72ffd1aAlexander Block	struct timespec ct = CURRENT_TIME;
4908ea05e3a4262b9e6871c349fa3486bcfc72ffd1aAlexander Block
4915f3ab90a72f98adbf00c50ac2d4d2b47cf4a9685Anand Jain	spin_lock(&root->root_item_lock);
4923cae210fa529d69cb25c2a3c491f29dab687b245Qu Wenruo	btrfs_set_root_ctransid(item, trans->transid);
4933cae210fa529d69cb25c2a3c491f29dab687b245Qu Wenruo	btrfs_set_stack_timespec_sec(&item->ctime, ct.tv_sec);
4943cae210fa529d69cb25c2a3c491f29dab687b245Qu Wenruo	btrfs_set_stack_timespec_nsec(&item->ctime, ct.tv_nsec);
4955f3ab90a72f98adbf00c50ac2d4d2b47cf4a9685Anand Jain	spin_unlock(&root->root_item_lock);
4968ea05e3a4262b9e6871c349fa3486bcfc72ffd1aAlexander Block}
497