blob: 1486cf9de1daae2be18cd3780f67600af09706d0 [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 * lookup the root with the highest offset for a given objectid. The key we do
26 * find is copied into 'key'. If we find something return 0, otherwise 1, < 0
27 * on error.
28 */
Chris Mason3768f362007-03-13 16:47:54 -040029int btrfs_find_last_root(struct btrfs_root *root, u64 objectid,
30 struct btrfs_root_item *item, struct btrfs_key *key)
31{
Chris Mason5caf2a02007-04-02 11:20:42 -040032 struct btrfs_path *path;
Chris Mason3768f362007-03-13 16:47:54 -040033 struct btrfs_key search_key;
Chris Mason5f39d392007-10-15 16:14:19 -040034 struct btrfs_key found_key;
35 struct extent_buffer *l;
Chris Mason3768f362007-03-13 16:47:54 -040036 int ret;
37 int slot;
38
39 search_key.objectid = objectid;
Chris Mason0660b5a2008-11-17 20:37:39 -050040 search_key.type = BTRFS_ROOT_ITEM_KEY;
Chris Mason5eda7b52007-06-22 14:16:25 -040041 search_key.offset = (u64)-1;
Chris Mason3768f362007-03-13 16:47:54 -040042
Chris Mason5caf2a02007-04-02 11:20:42 -040043 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +000044 if (!path)
45 return -ENOMEM;
Chris Mason5caf2a02007-04-02 11:20:42 -040046 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
Chris Mason3768f362007-03-13 16:47:54 -040047 if (ret < 0)
48 goto out;
Chris Mason5f39d392007-10-15 16:14:19 -040049
Chris Mason3768f362007-03-13 16:47:54 -040050 BUG_ON(ret == 0);
Yan, Zheng76dda932009-09-21 16:00:26 -040051 if (path->slots[0] == 0) {
Chris Mason3768f362007-03-13 16:47:54 -040052 ret = 1;
53 goto out;
54 }
Yan, Zheng76dda932009-09-21 16:00:26 -040055 l = path->nodes[0];
56 slot = path->slots[0] - 1;
57 btrfs_item_key_to_cpu(l, &found_key, slot);
58 if (found_key.objectid != objectid ||
59 found_key.type != BTRFS_ROOT_ITEM_KEY) {
60 ret = 1;
61 goto out;
62 }
63 if (item)
64 read_extent_buffer(l, item, btrfs_item_ptr_offset(l, slot),
65 sizeof(*item));
66 if (key)
67 memcpy(key, &found_key, sizeof(found_key));
Chris Mason3768f362007-03-13 16:47:54 -040068 ret = 0;
69out:
Chris Mason5caf2a02007-04-02 11:20:42 -040070 btrfs_free_path(path);
Chris Mason3768f362007-03-13 16:47:54 -040071 return ret;
72}
73
Mark Fashehbf5f32e2011-07-14 21:23:06 +000074void btrfs_set_root_node(struct btrfs_root_item *item,
75 struct extent_buffer *node)
Yan Zheng5d4f98a2009-06-10 10:45:14 -040076{
77 btrfs_set_root_bytenr(item, node->start);
78 btrfs_set_root_level(item, btrfs_header_level(node));
79 btrfs_set_root_generation(item, btrfs_header_generation(node));
Yan Zheng5d4f98a2009-06-10 10:45:14 -040080}
81
Chris Masond352ac62008-09-29 15:18:18 -040082/*
83 * copy the data in 'item' into the btree
84 */
Chris Masone089f052007-03-16 16:20:31 -040085int btrfs_update_root(struct btrfs_trans_handle *trans, struct btrfs_root
86 *root, struct btrfs_key *key, struct btrfs_root_item
87 *item)
Chris Mason3768f362007-03-13 16:47:54 -040088{
Chris Mason5caf2a02007-04-02 11:20:42 -040089 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -040090 struct extent_buffer *l;
Chris Mason3768f362007-03-13 16:47:54 -040091 int ret;
92 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -040093 unsigned long ptr;
Chris Mason3768f362007-03-13 16:47:54 -040094
Chris Mason5caf2a02007-04-02 11:20:42 -040095 path = btrfs_alloc_path();
Jeff Mahoneyb45a9d82011-10-03 23:22:44 -040096 if (!path)
97 return -ENOMEM;
98
Chris Mason5caf2a02007-04-02 11:20:42 -040099 ret = btrfs_search_slot(trans, root, key, path, 0, 1);
Chris Mason3768f362007-03-13 16:47:54 -0400100 if (ret < 0)
101 goto out;
Chris Masond6667462008-01-03 14:51:00 -0500102
103 if (ret != 0) {
104 btrfs_print_leaf(root, path->nodes[0]);
Chris Masond3977122009-01-05 21:25:51 -0500105 printk(KERN_CRIT "unable to update root key %llu %u %llu\n",
106 (unsigned long long)key->objectid, key->type,
107 (unsigned long long)key->offset);
Chris Masond6667462008-01-03 14:51:00 -0500108 BUG_ON(1);
109 }
110
Chris Mason5f39d392007-10-15 16:14:19 -0400111 l = path->nodes[0];
Chris Mason5caf2a02007-04-02 11:20:42 -0400112 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -0400113 ptr = btrfs_item_ptr_offset(l, slot);
114 write_extent_buffer(l, item, ptr, sizeof(*item));
Chris Mason5caf2a02007-04-02 11:20:42 -0400115 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason3768f362007-03-13 16:47:54 -0400116out:
Chris Mason5caf2a02007-04-02 11:20:42 -0400117 btrfs_free_path(path);
Chris Mason3768f362007-03-13 16:47:54 -0400118 return ret;
119}
120
Jeff Mahoneyd16cb052011-10-03 23:22:34 -0400121int btrfs_insert_root(struct btrfs_trans_handle *trans, struct btrfs_root *root,
122 struct btrfs_key *key, struct btrfs_root_item *item)
Chris Mason3768f362007-03-13 16:47:54 -0400123{
Jeff Mahoneyd16cb052011-10-03 23:22:34 -0400124 return btrfs_insert_item(trans, root, key, item, sizeof(*item));
Chris Mason3768f362007-03-13 16:47:54 -0400125}
126
Chris Masond352ac62008-09-29 15:18:18 -0400127/*
128 * at mount time we want to find all the old transaction snapshots that were in
Chris Masond3977122009-01-05 21:25:51 -0500129 * the process of being deleted if we crashed. This is any root item with an
130 * offset lower than the latest root. They need to be queued for deletion to
131 * finish what was happening when we crashed.
Chris Masond352ac62008-09-29 15:18:18 -0400132 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400133int btrfs_find_dead_roots(struct btrfs_root *root, u64 objectid)
Chris Mason5eda7b52007-06-22 14:16:25 -0400134{
135 struct btrfs_root *dead_root;
Chris Mason5eda7b52007-06-22 14:16:25 -0400136 struct btrfs_root_item *ri;
137 struct btrfs_key key;
Chris Masona7a16fd2008-06-26 10:34:20 -0400138 struct btrfs_key found_key;
Chris Mason5eda7b52007-06-22 14:16:25 -0400139 struct btrfs_path *path;
140 int ret;
141 u32 nritems;
Chris Mason5f39d392007-10-15 16:14:19 -0400142 struct extent_buffer *leaf;
Chris Mason5eda7b52007-06-22 14:16:25 -0400143 int slot;
144
Chris Mason5ce14bb2007-09-11 11:15:39 -0400145 key.objectid = objectid;
Chris Mason5eda7b52007-06-22 14:16:25 -0400146 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
147 key.offset = 0;
148 path = btrfs_alloc_path();
149 if (!path)
150 return -ENOMEM;
Chris Masona7a16fd2008-06-26 10:34:20 -0400151
152again:
Chris Mason5eda7b52007-06-22 14:16:25 -0400153 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
154 if (ret < 0)
155 goto err;
Chris Masond3977122009-01-05 21:25:51 -0500156 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400157 leaf = path->nodes[0];
158 nritems = btrfs_header_nritems(leaf);
Chris Mason5eda7b52007-06-22 14:16:25 -0400159 slot = path->slots[0];
160 if (slot >= nritems) {
161 ret = btrfs_next_leaf(root, path);
162 if (ret)
163 break;
Chris Mason5f39d392007-10-15 16:14:19 -0400164 leaf = path->nodes[0];
165 nritems = btrfs_header_nritems(leaf);
Chris Mason5eda7b52007-06-22 14:16:25 -0400166 slot = path->slots[0];
167 }
Chris Mason5f39d392007-10-15 16:14:19 -0400168 btrfs_item_key_to_cpu(leaf, &key, slot);
Chris Mason5eda7b52007-06-22 14:16:25 -0400169 if (btrfs_key_type(&key) != BTRFS_ROOT_ITEM_KEY)
170 goto next;
Chris Mason5ce14bb2007-09-11 11:15:39 -0400171
172 if (key.objectid < objectid)
173 goto next;
174
175 if (key.objectid > objectid)
176 break;
177
Chris Mason5eda7b52007-06-22 14:16:25 -0400178 ri = btrfs_item_ptr(leaf, slot, struct btrfs_root_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400179 if (btrfs_disk_root_refs(leaf, ri) != 0)
Chris Mason5eda7b52007-06-22 14:16:25 -0400180 goto next;
Chris Mason5ce14bb2007-09-11 11:15:39 -0400181
Chris Masona7a16fd2008-06-26 10:34:20 -0400182 memcpy(&found_key, &key, sizeof(key));
183 key.offset++;
David Sterbab3b4aa72011-04-21 01:20:15 +0200184 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400185 dead_root =
186 btrfs_read_fs_root_no_radix(root->fs_info->tree_root,
187 &found_key);
Aneesha1f39632007-07-11 10:03:27 -0400188 if (IS_ERR(dead_root)) {
189 ret = PTR_ERR(dead_root);
Chris Mason5eda7b52007-06-22 14:16:25 -0400190 goto err;
191 }
Chris Mason5ce14bb2007-09-11 11:15:39 -0400192
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400193 ret = btrfs_add_dead_root(dead_root);
Chris Mason5eda7b52007-06-22 14:16:25 -0400194 if (ret)
195 goto err;
Chris Masona7a16fd2008-06-26 10:34:20 -0400196 goto again;
Chris Mason5eda7b52007-06-22 14:16:25 -0400197next:
198 slot++;
199 path->slots[0]++;
200 }
201 ret = 0;
202err:
203 btrfs_free_path(path);
204 return ret;
205}
206
Yan, Zheng76dda932009-09-21 16:00:26 -0400207int btrfs_find_orphan_roots(struct btrfs_root *tree_root)
208{
209 struct extent_buffer *leaf;
210 struct btrfs_path *path;
211 struct btrfs_key key;
Yan, Zhengd68fc572010-05-16 10:49:58 -0400212 struct btrfs_key root_key;
213 struct btrfs_root *root;
Yan, Zheng76dda932009-09-21 16:00:26 -0400214 int err = 0;
215 int ret;
216
217 path = btrfs_alloc_path();
218 if (!path)
219 return -ENOMEM;
220
221 key.objectid = BTRFS_ORPHAN_OBJECTID;
222 key.type = BTRFS_ORPHAN_ITEM_KEY;
223 key.offset = 0;
224
Yan, Zhengd68fc572010-05-16 10:49:58 -0400225 root_key.type = BTRFS_ROOT_ITEM_KEY;
226 root_key.offset = (u64)-1;
227
Yan, Zheng76dda932009-09-21 16:00:26 -0400228 while (1) {
229 ret = btrfs_search_slot(NULL, tree_root, &key, path, 0, 0);
230 if (ret < 0) {
231 err = ret;
232 break;
233 }
234
235 leaf = path->nodes[0];
236 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
237 ret = btrfs_next_leaf(tree_root, path);
238 if (ret < 0)
239 err = ret;
240 if (ret != 0)
241 break;
242 leaf = path->nodes[0];
243 }
244
245 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +0200246 btrfs_release_path(path);
Yan, Zheng76dda932009-09-21 16:00:26 -0400247
248 if (key.objectid != BTRFS_ORPHAN_OBJECTID ||
249 key.type != BTRFS_ORPHAN_ITEM_KEY)
250 break;
251
Yan, Zhengd68fc572010-05-16 10:49:58 -0400252 root_key.objectid = key.offset;
253 key.offset++;
254
255 root = btrfs_read_fs_root_no_name(tree_root->fs_info,
256 &root_key);
257 if (!IS_ERR(root))
258 continue;
259
260 ret = PTR_ERR(root);
261 if (ret != -ENOENT) {
Yan, Zheng76dda932009-09-21 16:00:26 -0400262 err = ret;
263 break;
264 }
265
Yan, Zhengd68fc572010-05-16 10:49:58 -0400266 ret = btrfs_find_dead_roots(tree_root, root_key.objectid);
267 if (ret) {
268 err = ret;
269 break;
270 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400271 }
272
273 btrfs_free_path(path);
274 return err;
275}
276
Chris Masond352ac62008-09-29 15:18:18 -0400277/* drop the root item for 'key' from 'root' */
Chris Masone089f052007-03-16 16:20:31 -0400278int btrfs_del_root(struct btrfs_trans_handle *trans, struct btrfs_root *root,
279 struct btrfs_key *key)
Chris Mason3768f362007-03-13 16:47:54 -0400280{
Chris Mason5caf2a02007-04-02 11:20:42 -0400281 struct btrfs_path *path;
Chris Mason3768f362007-03-13 16:47:54 -0400282 int ret;
Chris Masonc5739bb2007-04-10 09:27:04 -0400283 struct btrfs_root_item *ri;
Chris Mason5f39d392007-10-15 16:14:19 -0400284 struct extent_buffer *leaf;
Chris Mason3768f362007-03-13 16:47:54 -0400285
Chris Mason5caf2a02007-04-02 11:20:42 -0400286 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +0000287 if (!path)
288 return -ENOMEM;
Chris Mason5caf2a02007-04-02 11:20:42 -0400289 ret = btrfs_search_slot(trans, root, key, path, -1, 1);
Chris Mason3768f362007-03-13 16:47:54 -0400290 if (ret < 0)
291 goto out;
Chris Masonedbd8d42007-12-21 16:27:24 -0500292
Chris Mason3768f362007-03-13 16:47:54 -0400293 BUG_ON(ret != 0);
Chris Mason5f39d392007-10-15 16:14:19 -0400294 leaf = path->nodes[0];
295 ri = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_item);
Chris Masonc5739bb2007-04-10 09:27:04 -0400296
Chris Mason5eda7b52007-06-22 14:16:25 -0400297 ret = btrfs_del_item(trans, root, path);
Chris Mason3768f362007-03-13 16:47:54 -0400298out:
Chris Mason5caf2a02007-04-02 11:20:42 -0400299 btrfs_free_path(path);
Chris Mason3768f362007-03-13 16:47:54 -0400300 return ret;
301}
Chris Mason0660b5a2008-11-17 20:37:39 -0500302
303int btrfs_del_root_ref(struct btrfs_trans_handle *trans,
304 struct btrfs_root *tree_root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400305 u64 root_id, u64 ref_id, u64 dirid, u64 *sequence,
306 const char *name, int name_len)
307
Chris Mason0660b5a2008-11-17 20:37:39 -0500308{
Chris Mason0660b5a2008-11-17 20:37:39 -0500309 struct btrfs_path *path;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400310 struct btrfs_root_ref *ref;
311 struct extent_buffer *leaf;
312 struct btrfs_key key;
313 unsigned long ptr;
314 int err = 0;
315 int ret;
Chris Mason0660b5a2008-11-17 20:37:39 -0500316
317 path = btrfs_alloc_path();
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400318 if (!path)
319 return -ENOMEM;
Chris Mason0660b5a2008-11-17 20:37:39 -0500320
321 key.objectid = root_id;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400322 key.type = BTRFS_ROOT_BACKREF_KEY;
Chris Mason0660b5a2008-11-17 20:37:39 -0500323 key.offset = ref_id;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400324again:
Chris Mason0660b5a2008-11-17 20:37:39 -0500325 ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400326 BUG_ON(ret < 0);
327 if (ret == 0) {
328 leaf = path->nodes[0];
329 ref = btrfs_item_ptr(leaf, path->slots[0],
330 struct btrfs_root_ref);
Chris Mason0660b5a2008-11-17 20:37:39 -0500331
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400332 WARN_ON(btrfs_root_ref_dirid(leaf, ref) != dirid);
333 WARN_ON(btrfs_root_ref_name_len(leaf, ref) != name_len);
334 ptr = (unsigned long)(ref + 1);
335 WARN_ON(memcmp_extent_buffer(leaf, name, ptr, name_len));
336 *sequence = btrfs_root_ref_sequence(leaf, ref);
337
338 ret = btrfs_del_item(trans, tree_root, path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +0000339 if (ret) {
340 err = ret;
341 goto out;
342 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400343 } else
344 err = -ENOENT;
345
346 if (key.type == BTRFS_ROOT_BACKREF_KEY) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200347 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400348 key.objectid = ref_id;
349 key.type = BTRFS_ROOT_REF_KEY;
350 key.offset = root_id;
351 goto again;
352 }
Chris Mason0660b5a2008-11-17 20:37:39 -0500353
Tsutomu Itoh65a246c2011-05-19 04:37:44 +0000354out:
Chris Mason0660b5a2008-11-17 20:37:39 -0500355 btrfs_free_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400356 return err;
Chris Mason0660b5a2008-11-17 20:37:39 -0500357}
358
Chris Masonea9e8b12008-11-17 21:14:24 -0500359int btrfs_find_root_ref(struct btrfs_root *tree_root,
360 struct btrfs_path *path,
361 u64 root_id, u64 ref_id)
362{
363 struct btrfs_key key;
364 int ret;
365
366 key.objectid = root_id;
367 key.type = BTRFS_ROOT_REF_KEY;
368 key.offset = ref_id;
369
370 ret = btrfs_search_slot(NULL, tree_root, &key, path, 0, 0);
371 return ret;
372}
373
Chris Mason0660b5a2008-11-17 20:37:39 -0500374/*
375 * add a btrfs_root_ref item. type is either BTRFS_ROOT_REF_KEY
376 * or BTRFS_ROOT_BACKREF_KEY.
377 *
378 * The dirid, sequence, name and name_len refer to the directory entry
379 * that is referencing the root.
380 *
381 * For a forward ref, the root_id is the id of the tree referencing
382 * the root and ref_id is the id of the subvol or snapshot.
383 *
384 * For a back ref the root_id is the id of the subvol or snapshot and
385 * ref_id is the id of the tree referencing it.
386 */
387int btrfs_add_root_ref(struct btrfs_trans_handle *trans,
388 struct btrfs_root *tree_root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400389 u64 root_id, u64 ref_id, u64 dirid, u64 sequence,
Chris Mason0660b5a2008-11-17 20:37:39 -0500390 const char *name, int name_len)
391{
392 struct btrfs_key key;
393 int ret;
394 struct btrfs_path *path;
395 struct btrfs_root_ref *ref;
396 struct extent_buffer *leaf;
397 unsigned long ptr;
398
Chris Mason0660b5a2008-11-17 20:37:39 -0500399 path = btrfs_alloc_path();
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400400 if (!path)
401 return -ENOMEM;
Chris Mason0660b5a2008-11-17 20:37:39 -0500402
403 key.objectid = root_id;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400404 key.type = BTRFS_ROOT_BACKREF_KEY;
Chris Mason0660b5a2008-11-17 20:37:39 -0500405 key.offset = ref_id;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400406again:
Chris Mason0660b5a2008-11-17 20:37:39 -0500407 ret = btrfs_insert_empty_item(trans, tree_root, path, &key,
408 sizeof(*ref) + name_len);
409 BUG_ON(ret);
410
411 leaf = path->nodes[0];
412 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
413 btrfs_set_root_ref_dirid(leaf, ref, dirid);
414 btrfs_set_root_ref_sequence(leaf, ref, sequence);
415 btrfs_set_root_ref_name_len(leaf, ref, name_len);
416 ptr = (unsigned long)(ref + 1);
417 write_extent_buffer(leaf, name, ptr, name_len);
418 btrfs_mark_buffer_dirty(leaf);
419
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400420 if (key.type == BTRFS_ROOT_BACKREF_KEY) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200421 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400422 key.objectid = ref_id;
423 key.type = BTRFS_ROOT_REF_KEY;
424 key.offset = root_id;
425 goto again;
426 }
427
Chris Mason0660b5a2008-11-17 20:37:39 -0500428 btrfs_free_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400429 return 0;
Chris Mason0660b5a2008-11-17 20:37:39 -0500430}
Li Zefan08fe4db2011-03-28 02:01:25 +0000431
432/*
433 * Old btrfs forgets to init root_item->flags and root_item->byte_limit
434 * for subvolumes. To work around this problem, we steal a bit from
435 * root_item->inode_item->flags, and use it to indicate if those fields
436 * have been properly initialized.
437 */
438void btrfs_check_and_init_root_item(struct btrfs_root_item *root_item)
439{
440 u64 inode_flags = le64_to_cpu(root_item->inode.flags);
441
442 if (!(inode_flags & BTRFS_INODE_ROOT_ITEM_INIT)) {
443 inode_flags |= BTRFS_INODE_ROOT_ITEM_INIT;
444 root_item->inode.flags = cpu_to_le64(inode_flags);
445 root_item->flags = 0;
446 root_item->byte_limit = 0;
447 }
448}