blob: 6928bff62daa8370b4d9efce397fac35208c9611 [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
Chris Mason3768f362007-03-13 16:47:54 -040019#include "ctree.h"
Chris Mason5eda7b52007-06-22 14:16:25 -040020#include "transaction.h"
Chris Mason3768f362007-03-13 16:47:54 -040021#include "disk-io.h"
22#include "print-tree.h"
23
Chris Masonbf4ef672008-05-08 13:26:18 -040024/*
Chris Masond352ac62008-09-29 15:18:18 -040025 * search forward for a root, starting with objectid 'search_start'
26 * if a root key is found, the objectid we find is filled into 'found_objectid'
27 * and 0 is returned. < 0 is returned on error, 1 if there is nothing
28 * left in the tree.
Chris Masonbf4ef672008-05-08 13:26:18 -040029 */
30int btrfs_search_root(struct btrfs_root *root, u64 search_start,
31 u64 *found_objectid)
32{
33 struct btrfs_path *path;
34 struct btrfs_key search_key;
35 int ret;
36
37 root = root->fs_info->tree_root;
38 search_key.objectid = search_start;
39 search_key.type = (u8)-1;
40 search_key.offset = (u64)-1;
41
42 path = btrfs_alloc_path();
43 BUG_ON(!path);
44again:
45 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
46 if (ret < 0)
47 goto out;
48 if (ret == 0) {
49 ret = 1;
50 goto out;
51 }
52 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
53 ret = btrfs_next_leaf(root, path);
54 if (ret)
55 goto out;
56 }
57 btrfs_item_key_to_cpu(path->nodes[0], &search_key, path->slots[0]);
58 if (search_key.type != BTRFS_ROOT_ITEM_KEY) {
59 search_key.offset++;
60 btrfs_release_path(root, path);
61 goto again;
62 }
63 ret = 0;
64 *found_objectid = search_key.objectid;
65
66out:
67 btrfs_free_path(path);
68 return ret;
69}
70
Chris Masond352ac62008-09-29 15:18:18 -040071/*
72 * lookup the root with the highest offset for a given objectid. The key we do
73 * find is copied into 'key'. If we find something return 0, otherwise 1, < 0
74 * on error.
75 */
Chris Mason3768f362007-03-13 16:47:54 -040076int btrfs_find_last_root(struct btrfs_root *root, u64 objectid,
77 struct btrfs_root_item *item, struct btrfs_key *key)
78{
Chris Mason5caf2a02007-04-02 11:20:42 -040079 struct btrfs_path *path;
Chris Mason3768f362007-03-13 16:47:54 -040080 struct btrfs_key search_key;
Chris Mason5f39d392007-10-15 16:14:19 -040081 struct btrfs_key found_key;
82 struct extent_buffer *l;
Chris Mason3768f362007-03-13 16:47:54 -040083 int ret;
84 int slot;
85
86 search_key.objectid = objectid;
Chris Mason0660b5a2008-11-17 20:37:39 -050087 search_key.type = BTRFS_ROOT_ITEM_KEY;
Chris Mason5eda7b52007-06-22 14:16:25 -040088 search_key.offset = (u64)-1;
Chris Mason3768f362007-03-13 16:47:54 -040089
Chris Mason5caf2a02007-04-02 11:20:42 -040090 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +000091 if (!path)
92 return -ENOMEM;
Chris Mason5caf2a02007-04-02 11:20:42 -040093 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
Chris Mason3768f362007-03-13 16:47:54 -040094 if (ret < 0)
95 goto out;
Chris Mason5f39d392007-10-15 16:14:19 -040096
Chris Mason3768f362007-03-13 16:47:54 -040097 BUG_ON(ret == 0);
Yan, Zheng76dda932009-09-21 16:00:26 -040098 if (path->slots[0] == 0) {
Chris Mason3768f362007-03-13 16:47:54 -040099 ret = 1;
100 goto out;
101 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400102 l = path->nodes[0];
103 slot = path->slots[0] - 1;
104 btrfs_item_key_to_cpu(l, &found_key, slot);
105 if (found_key.objectid != objectid ||
106 found_key.type != BTRFS_ROOT_ITEM_KEY) {
107 ret = 1;
108 goto out;
109 }
110 if (item)
111 read_extent_buffer(l, item, btrfs_item_ptr_offset(l, slot),
112 sizeof(*item));
113 if (key)
114 memcpy(key, &found_key, sizeof(found_key));
Chris Mason3768f362007-03-13 16:47:54 -0400115 ret = 0;
116out:
Chris Mason5caf2a02007-04-02 11:20:42 -0400117 btrfs_free_path(path);
Chris Mason3768f362007-03-13 16:47:54 -0400118 return ret;
119}
120
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400121int btrfs_set_root_node(struct btrfs_root_item *item,
122 struct extent_buffer *node)
123{
124 btrfs_set_root_bytenr(item, node->start);
125 btrfs_set_root_level(item, btrfs_header_level(node));
126 btrfs_set_root_generation(item, btrfs_header_generation(node));
127 return 0;
128}
129
Chris Masond352ac62008-09-29 15:18:18 -0400130/*
131 * copy the data in 'item' into the btree
132 */
Chris Masone089f052007-03-16 16:20:31 -0400133int btrfs_update_root(struct btrfs_trans_handle *trans, struct btrfs_root
134 *root, struct btrfs_key *key, struct btrfs_root_item
135 *item)
Chris Mason3768f362007-03-13 16:47:54 -0400136{
Chris Mason5caf2a02007-04-02 11:20:42 -0400137 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400138 struct extent_buffer *l;
Chris Mason3768f362007-03-13 16:47:54 -0400139 int ret;
140 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -0400141 unsigned long ptr;
Chris Mason3768f362007-03-13 16:47:54 -0400142
Chris Mason5caf2a02007-04-02 11:20:42 -0400143 path = btrfs_alloc_path();
144 BUG_ON(!path);
Chris Mason5caf2a02007-04-02 11:20:42 -0400145 ret = btrfs_search_slot(trans, root, key, path, 0, 1);
Chris Mason3768f362007-03-13 16:47:54 -0400146 if (ret < 0)
147 goto out;
Chris Masond6667462008-01-03 14:51:00 -0500148
149 if (ret != 0) {
150 btrfs_print_leaf(root, path->nodes[0]);
Chris Masond3977122009-01-05 21:25:51 -0500151 printk(KERN_CRIT "unable to update root key %llu %u %llu\n",
152 (unsigned long long)key->objectid, key->type,
153 (unsigned long long)key->offset);
Chris Masond6667462008-01-03 14:51:00 -0500154 BUG_ON(1);
155 }
156
Chris Mason5f39d392007-10-15 16:14:19 -0400157 l = path->nodes[0];
Chris Mason5caf2a02007-04-02 11:20:42 -0400158 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -0400159 ptr = btrfs_item_ptr_offset(l, slot);
160 write_extent_buffer(l, item, ptr, sizeof(*item));
Chris Mason5caf2a02007-04-02 11:20:42 -0400161 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason3768f362007-03-13 16:47:54 -0400162out:
Chris Mason5caf2a02007-04-02 11:20:42 -0400163 btrfs_free_path(path);
Chris Mason3768f362007-03-13 16:47:54 -0400164 return ret;
165}
166
Chris Masone089f052007-03-16 16:20:31 -0400167int btrfs_insert_root(struct btrfs_trans_handle *trans, struct btrfs_root
168 *root, struct btrfs_key *key, struct btrfs_root_item
169 *item)
Chris Mason3768f362007-03-13 16:47:54 -0400170{
171 int ret;
Chris Masone089f052007-03-16 16:20:31 -0400172 ret = btrfs_insert_item(trans, root, key, item, sizeof(*item));
Chris Mason3768f362007-03-13 16:47:54 -0400173 return ret;
174}
175
Chris Masond352ac62008-09-29 15:18:18 -0400176/*
177 * at mount time we want to find all the old transaction snapshots that were in
Chris Masond3977122009-01-05 21:25:51 -0500178 * the process of being deleted if we crashed. This is any root item with an
179 * offset lower than the latest root. They need to be queued for deletion to
180 * finish what was happening when we crashed.
Chris Masond352ac62008-09-29 15:18:18 -0400181 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400182int btrfs_find_dead_roots(struct btrfs_root *root, u64 objectid)
Chris Mason5eda7b52007-06-22 14:16:25 -0400183{
184 struct btrfs_root *dead_root;
Chris Mason5eda7b52007-06-22 14:16:25 -0400185 struct btrfs_root_item *ri;
186 struct btrfs_key key;
Chris Masona7a16fd2008-06-26 10:34:20 -0400187 struct btrfs_key found_key;
Chris Mason5eda7b52007-06-22 14:16:25 -0400188 struct btrfs_path *path;
189 int ret;
190 u32 nritems;
Chris Mason5f39d392007-10-15 16:14:19 -0400191 struct extent_buffer *leaf;
Chris Mason5eda7b52007-06-22 14:16:25 -0400192 int slot;
193
Chris Mason5ce14bb2007-09-11 11:15:39 -0400194 key.objectid = objectid;
Chris Mason5eda7b52007-06-22 14:16:25 -0400195 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
196 key.offset = 0;
197 path = btrfs_alloc_path();
198 if (!path)
199 return -ENOMEM;
Chris Masona7a16fd2008-06-26 10:34:20 -0400200
201again:
Chris Mason5eda7b52007-06-22 14:16:25 -0400202 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
203 if (ret < 0)
204 goto err;
Chris Masond3977122009-01-05 21:25:51 -0500205 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400206 leaf = path->nodes[0];
207 nritems = btrfs_header_nritems(leaf);
Chris Mason5eda7b52007-06-22 14:16:25 -0400208 slot = path->slots[0];
209 if (slot >= nritems) {
210 ret = btrfs_next_leaf(root, path);
211 if (ret)
212 break;
Chris Mason5f39d392007-10-15 16:14:19 -0400213 leaf = path->nodes[0];
214 nritems = btrfs_header_nritems(leaf);
Chris Mason5eda7b52007-06-22 14:16:25 -0400215 slot = path->slots[0];
216 }
Chris Mason5f39d392007-10-15 16:14:19 -0400217 btrfs_item_key_to_cpu(leaf, &key, slot);
Chris Mason5eda7b52007-06-22 14:16:25 -0400218 if (btrfs_key_type(&key) != BTRFS_ROOT_ITEM_KEY)
219 goto next;
Chris Mason5ce14bb2007-09-11 11:15:39 -0400220
221 if (key.objectid < objectid)
222 goto next;
223
224 if (key.objectid > objectid)
225 break;
226
Chris Mason5eda7b52007-06-22 14:16:25 -0400227 ri = btrfs_item_ptr(leaf, slot, struct btrfs_root_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400228 if (btrfs_disk_root_refs(leaf, ri) != 0)
Chris Mason5eda7b52007-06-22 14:16:25 -0400229 goto next;
Chris Mason5ce14bb2007-09-11 11:15:39 -0400230
Chris Masona7a16fd2008-06-26 10:34:20 -0400231 memcpy(&found_key, &key, sizeof(key));
232 key.offset++;
233 btrfs_release_path(root, path);
Chris Masone02119d2008-09-05 16:13:11 -0400234 dead_root =
235 btrfs_read_fs_root_no_radix(root->fs_info->tree_root,
236 &found_key);
Aneesha1f39632007-07-11 10:03:27 -0400237 if (IS_ERR(dead_root)) {
238 ret = PTR_ERR(dead_root);
Chris Mason5eda7b52007-06-22 14:16:25 -0400239 goto err;
240 }
Chris Mason5ce14bb2007-09-11 11:15:39 -0400241
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400242 ret = btrfs_add_dead_root(dead_root);
Chris Mason5eda7b52007-06-22 14:16:25 -0400243 if (ret)
244 goto err;
Chris Masona7a16fd2008-06-26 10:34:20 -0400245 goto again;
Chris Mason5eda7b52007-06-22 14:16:25 -0400246next:
247 slot++;
248 path->slots[0]++;
249 }
250 ret = 0;
251err:
252 btrfs_free_path(path);
253 return ret;
254}
255
Yan, Zheng76dda932009-09-21 16:00:26 -0400256int btrfs_find_orphan_roots(struct btrfs_root *tree_root)
257{
258 struct extent_buffer *leaf;
259 struct btrfs_path *path;
260 struct btrfs_key key;
Yan, Zhengd68fc572010-05-16 10:49:58 -0400261 struct btrfs_key root_key;
262 struct btrfs_root *root;
Yan, Zheng76dda932009-09-21 16:00:26 -0400263 int err = 0;
264 int ret;
265
266 path = btrfs_alloc_path();
267 if (!path)
268 return -ENOMEM;
269
270 key.objectid = BTRFS_ORPHAN_OBJECTID;
271 key.type = BTRFS_ORPHAN_ITEM_KEY;
272 key.offset = 0;
273
Yan, Zhengd68fc572010-05-16 10:49:58 -0400274 root_key.type = BTRFS_ROOT_ITEM_KEY;
275 root_key.offset = (u64)-1;
276
Yan, Zheng76dda932009-09-21 16:00:26 -0400277 while (1) {
278 ret = btrfs_search_slot(NULL, tree_root, &key, path, 0, 0);
279 if (ret < 0) {
280 err = ret;
281 break;
282 }
283
284 leaf = path->nodes[0];
285 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
286 ret = btrfs_next_leaf(tree_root, path);
287 if (ret < 0)
288 err = ret;
289 if (ret != 0)
290 break;
291 leaf = path->nodes[0];
292 }
293
294 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
295 btrfs_release_path(tree_root, path);
296
297 if (key.objectid != BTRFS_ORPHAN_OBJECTID ||
298 key.type != BTRFS_ORPHAN_ITEM_KEY)
299 break;
300
Yan, Zhengd68fc572010-05-16 10:49:58 -0400301 root_key.objectid = key.offset;
302 key.offset++;
303
304 root = btrfs_read_fs_root_no_name(tree_root->fs_info,
305 &root_key);
306 if (!IS_ERR(root))
307 continue;
308
309 ret = PTR_ERR(root);
310 if (ret != -ENOENT) {
Yan, Zheng76dda932009-09-21 16:00:26 -0400311 err = ret;
312 break;
313 }
314
Yan, Zhengd68fc572010-05-16 10:49:58 -0400315 ret = btrfs_find_dead_roots(tree_root, root_key.objectid);
316 if (ret) {
317 err = ret;
318 break;
319 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400320 }
321
322 btrfs_free_path(path);
323 return err;
324}
325
Chris Masond352ac62008-09-29 15:18:18 -0400326/* drop the root item for 'key' from 'root' */
Chris Masone089f052007-03-16 16:20:31 -0400327int btrfs_del_root(struct btrfs_trans_handle *trans, struct btrfs_root *root,
328 struct btrfs_key *key)
Chris Mason3768f362007-03-13 16:47:54 -0400329{
Chris Mason5caf2a02007-04-02 11:20:42 -0400330 struct btrfs_path *path;
Chris Mason3768f362007-03-13 16:47:54 -0400331 int ret;
Chris Masonc5739bb2007-04-10 09:27:04 -0400332 struct btrfs_root_item *ri;
Chris Mason5f39d392007-10-15 16:14:19 -0400333 struct extent_buffer *leaf;
Chris Mason3768f362007-03-13 16:47:54 -0400334
Chris Mason5caf2a02007-04-02 11:20:42 -0400335 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +0000336 if (!path)
337 return -ENOMEM;
Chris Mason5caf2a02007-04-02 11:20:42 -0400338 ret = btrfs_search_slot(trans, root, key, path, -1, 1);
Chris Mason3768f362007-03-13 16:47:54 -0400339 if (ret < 0)
340 goto out;
Chris Masonedbd8d42007-12-21 16:27:24 -0500341
Chris Mason3768f362007-03-13 16:47:54 -0400342 BUG_ON(ret != 0);
Chris Mason5f39d392007-10-15 16:14:19 -0400343 leaf = path->nodes[0];
344 ri = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_item);
Chris Masonc5739bb2007-04-10 09:27:04 -0400345
Chris Mason5eda7b52007-06-22 14:16:25 -0400346 ret = btrfs_del_item(trans, root, path);
Chris Mason3768f362007-03-13 16:47:54 -0400347out:
Chris Mason5caf2a02007-04-02 11:20:42 -0400348 btrfs_free_path(path);
Chris Mason3768f362007-03-13 16:47:54 -0400349 return ret;
350}
Chris Mason0660b5a2008-11-17 20:37:39 -0500351
352int btrfs_del_root_ref(struct btrfs_trans_handle *trans,
353 struct btrfs_root *tree_root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400354 u64 root_id, u64 ref_id, u64 dirid, u64 *sequence,
355 const char *name, int name_len)
356
Chris Mason0660b5a2008-11-17 20:37:39 -0500357{
Chris Mason0660b5a2008-11-17 20:37:39 -0500358 struct btrfs_path *path;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400359 struct btrfs_root_ref *ref;
360 struct extent_buffer *leaf;
361 struct btrfs_key key;
362 unsigned long ptr;
363 int err = 0;
364 int ret;
Chris Mason0660b5a2008-11-17 20:37:39 -0500365
366 path = btrfs_alloc_path();
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400367 if (!path)
368 return -ENOMEM;
Chris Mason0660b5a2008-11-17 20:37:39 -0500369
370 key.objectid = root_id;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400371 key.type = BTRFS_ROOT_BACKREF_KEY;
Chris Mason0660b5a2008-11-17 20:37:39 -0500372 key.offset = ref_id;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400373again:
Chris Mason0660b5a2008-11-17 20:37:39 -0500374 ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400375 BUG_ON(ret < 0);
376 if (ret == 0) {
377 leaf = path->nodes[0];
378 ref = btrfs_item_ptr(leaf, path->slots[0],
379 struct btrfs_root_ref);
Chris Mason0660b5a2008-11-17 20:37:39 -0500380
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400381 WARN_ON(btrfs_root_ref_dirid(leaf, ref) != dirid);
382 WARN_ON(btrfs_root_ref_name_len(leaf, ref) != name_len);
383 ptr = (unsigned long)(ref + 1);
384 WARN_ON(memcmp_extent_buffer(leaf, name, ptr, name_len));
385 *sequence = btrfs_root_ref_sequence(leaf, ref);
386
387 ret = btrfs_del_item(trans, tree_root, path);
388 BUG_ON(ret);
389 } else
390 err = -ENOENT;
391
392 if (key.type == BTRFS_ROOT_BACKREF_KEY) {
393 btrfs_release_path(tree_root, path);
394 key.objectid = ref_id;
395 key.type = BTRFS_ROOT_REF_KEY;
396 key.offset = root_id;
397 goto again;
398 }
Chris Mason0660b5a2008-11-17 20:37:39 -0500399
400 btrfs_free_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400401 return err;
Chris Mason0660b5a2008-11-17 20:37:39 -0500402}
403
Chris Masonea9e8b12008-11-17 21:14:24 -0500404int btrfs_find_root_ref(struct btrfs_root *tree_root,
405 struct btrfs_path *path,
406 u64 root_id, u64 ref_id)
407{
408 struct btrfs_key key;
409 int ret;
410
411 key.objectid = root_id;
412 key.type = BTRFS_ROOT_REF_KEY;
413 key.offset = ref_id;
414
415 ret = btrfs_search_slot(NULL, tree_root, &key, path, 0, 0);
416 return ret;
417}
418
Chris Mason0660b5a2008-11-17 20:37:39 -0500419/*
420 * add a btrfs_root_ref item. type is either BTRFS_ROOT_REF_KEY
421 * or BTRFS_ROOT_BACKREF_KEY.
422 *
423 * The dirid, sequence, name and name_len refer to the directory entry
424 * that is referencing the root.
425 *
426 * For a forward ref, the root_id is the id of the tree referencing
427 * the root and ref_id is the id of the subvol or snapshot.
428 *
429 * For a back ref the root_id is the id of the subvol or snapshot and
430 * ref_id is the id of the tree referencing it.
431 */
432int btrfs_add_root_ref(struct btrfs_trans_handle *trans,
433 struct btrfs_root *tree_root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400434 u64 root_id, u64 ref_id, u64 dirid, u64 sequence,
Chris Mason0660b5a2008-11-17 20:37:39 -0500435 const char *name, int name_len)
436{
437 struct btrfs_key key;
438 int ret;
439 struct btrfs_path *path;
440 struct btrfs_root_ref *ref;
441 struct extent_buffer *leaf;
442 unsigned long ptr;
443
Chris Mason0660b5a2008-11-17 20:37:39 -0500444 path = btrfs_alloc_path();
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400445 if (!path)
446 return -ENOMEM;
Chris Mason0660b5a2008-11-17 20:37:39 -0500447
448 key.objectid = root_id;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400449 key.type = BTRFS_ROOT_BACKREF_KEY;
Chris Mason0660b5a2008-11-17 20:37:39 -0500450 key.offset = ref_id;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400451again:
Chris Mason0660b5a2008-11-17 20:37:39 -0500452 ret = btrfs_insert_empty_item(trans, tree_root, path, &key,
453 sizeof(*ref) + name_len);
454 BUG_ON(ret);
455
456 leaf = path->nodes[0];
457 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
458 btrfs_set_root_ref_dirid(leaf, ref, dirid);
459 btrfs_set_root_ref_sequence(leaf, ref, sequence);
460 btrfs_set_root_ref_name_len(leaf, ref, name_len);
461 ptr = (unsigned long)(ref + 1);
462 write_extent_buffer(leaf, name, ptr, name_len);
463 btrfs_mark_buffer_dirty(leaf);
464
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400465 if (key.type == BTRFS_ROOT_BACKREF_KEY) {
466 btrfs_release_path(tree_root, path);
467 key.objectid = ref_id;
468 key.type = BTRFS_ROOT_REF_KEY;
469 key.offset = root_id;
470 goto again;
471 }
472
Chris Mason0660b5a2008-11-17 20:37:39 -0500473 btrfs_free_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400474 return 0;
Chris Mason0660b5a2008-11-17 20:37:39 -0500475}
Li Zefan08fe4db2011-03-28 02:01:25 +0000476
477/*
478 * Old btrfs forgets to init root_item->flags and root_item->byte_limit
479 * for subvolumes. To work around this problem, we steal a bit from
480 * root_item->inode_item->flags, and use it to indicate if those fields
481 * have been properly initialized.
482 */
483void btrfs_check_and_init_root_item(struct btrfs_root_item *root_item)
484{
485 u64 inode_flags = le64_to_cpu(root_item->inode.flags);
486
487 if (!(inode_flags & BTRFS_INODE_ROOT_ITEM_INIT)) {
488 inode_flags |= BTRFS_INODE_ROOT_ITEM_INIT;
489 root_item->inode.flags = cpu_to_le64(inode_flags);
490 root_item->flags = 0;
491 root_item->byte_limit = 0;
492 }
493}