blob: 5bf1ed57f178ea60b7ca8e98aceb6710fc9f55c4 [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
Alexander Block8ea05e32012-07-25 17:35:53 +020019#include <linux/uuid.h>
Chris Mason3768f362007-03-13 16:47:54 -040020#include "ctree.h"
Chris Mason5eda7b52007-06-22 14:16:25 -040021#include "transaction.h"
Chris Mason3768f362007-03-13 16:47:54 -040022#include "disk-io.h"
23#include "print-tree.h"
24
Chris Masonbf4ef672008-05-08 13:26:18 -040025/*
Alexander Block8ea05e32012-07-25 17:35:53 +020026 * Read a root item from the tree. In case we detect a root item smaller then
27 * sizeof(root_item), we know it's an old version of the root structure and
28 * initialize all new fields to zero. The same happens if we detect mismatching
29 * generation numbers as then we know the root was once mounted with an older
30 * kernel that was not aware of the root item structure change.
31 */
Stefan Behrens5fbf83c2013-04-19 15:08:04 +000032void btrfs_read_root_item(struct extent_buffer *eb, int slot,
33 struct btrfs_root_item *item)
Alexander Block8ea05e32012-07-25 17:35:53 +020034{
35 uuid_le uuid;
36 int len;
37 int need_reset = 0;
38
39 len = btrfs_item_size_nr(eb, slot);
40 read_extent_buffer(eb, item, btrfs_item_ptr_offset(eb, slot),
41 min_t(int, len, (int)sizeof(*item)));
42 if (len < sizeof(*item))
43 need_reset = 1;
44 if (!need_reset && btrfs_root_generation(item)
45 != btrfs_root_generation_v2(item)) {
46 if (btrfs_root_generation_v2(item) != 0) {
47 printk(KERN_WARNING "btrfs: mismatching "
48 "generation and generation_v2 "
49 "found in root item. This root "
50 "was probably mounted with an "
51 "older kernel. Resetting all "
52 "new fields.\n");
53 }
54 need_reset = 1;
55 }
56 if (need_reset) {
57 memset(&item->generation_v2, 0,
58 sizeof(*item) - offsetof(struct btrfs_root_item,
59 generation_v2));
60
61 uuid_le_gen(&uuid);
62 memcpy(item->uuid, uuid.b, BTRFS_UUID_SIZE);
63 }
64}
65
66/*
Chris Masond352ac62008-09-29 15:18:18 -040067 * lookup the root with the highest offset for a given objectid. The key we do
68 * find is copied into 'key'. If we find something return 0, otherwise 1, < 0
69 * on error.
70 */
Chris Mason3768f362007-03-13 16:47:54 -040071int btrfs_find_last_root(struct btrfs_root *root, u64 objectid,
72 struct btrfs_root_item *item, struct btrfs_key *key)
73{
Chris Mason5caf2a02007-04-02 11:20:42 -040074 struct btrfs_path *path;
Chris Mason3768f362007-03-13 16:47:54 -040075 struct btrfs_key search_key;
Chris Mason5f39d392007-10-15 16:14:19 -040076 struct btrfs_key found_key;
77 struct extent_buffer *l;
Chris Mason3768f362007-03-13 16:47:54 -040078 int ret;
79 int slot;
80
81 search_key.objectid = objectid;
Chris Mason0660b5a2008-11-17 20:37:39 -050082 search_key.type = BTRFS_ROOT_ITEM_KEY;
Chris Mason5eda7b52007-06-22 14:16:25 -040083 search_key.offset = (u64)-1;
Chris Mason3768f362007-03-13 16:47:54 -040084
Chris Mason5caf2a02007-04-02 11:20:42 -040085 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +000086 if (!path)
87 return -ENOMEM;
Chris Mason5caf2a02007-04-02 11:20:42 -040088 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
Chris Mason3768f362007-03-13 16:47:54 -040089 if (ret < 0)
90 goto out;
Chris Mason5f39d392007-10-15 16:14:19 -040091
Chris Mason3768f362007-03-13 16:47:54 -040092 BUG_ON(ret == 0);
Yan, Zheng76dda932009-09-21 16:00:26 -040093 if (path->slots[0] == 0) {
Chris Mason3768f362007-03-13 16:47:54 -040094 ret = 1;
95 goto out;
96 }
Yan, Zheng76dda932009-09-21 16:00:26 -040097 l = path->nodes[0];
98 slot = path->slots[0] - 1;
99 btrfs_item_key_to_cpu(l, &found_key, slot);
100 if (found_key.objectid != objectid ||
101 found_key.type != BTRFS_ROOT_ITEM_KEY) {
102 ret = 1;
103 goto out;
104 }
105 if (item)
Stefan Behrens5fbf83c2013-04-19 15:08:04 +0000106 btrfs_read_root_item(l, slot, item);
Yan, Zheng76dda932009-09-21 16:00:26 -0400107 if (key)
108 memcpy(key, &found_key, sizeof(found_key));
Alexander Block8ea05e32012-07-25 17:35:53 +0200109
Chris Mason3768f362007-03-13 16:47:54 -0400110 ret = 0;
111out:
Chris Mason5caf2a02007-04-02 11:20:42 -0400112 btrfs_free_path(path);
Chris Mason3768f362007-03-13 16:47:54 -0400113 return ret;
114}
115
Mark Fashehbf5f32e2011-07-14 21:23:06 +0000116void btrfs_set_root_node(struct btrfs_root_item *item,
117 struct extent_buffer *node)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400118{
119 btrfs_set_root_bytenr(item, node->start);
120 btrfs_set_root_level(item, btrfs_header_level(node));
121 btrfs_set_root_generation(item, btrfs_header_generation(node));
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400122}
123
Chris Masond352ac62008-09-29 15:18:18 -0400124/*
125 * copy the data in 'item' into the btree
126 */
Chris Masone089f052007-03-16 16:20:31 -0400127int btrfs_update_root(struct btrfs_trans_handle *trans, struct btrfs_root
128 *root, struct btrfs_key *key, struct btrfs_root_item
129 *item)
Chris Mason3768f362007-03-13 16:47:54 -0400130{
Chris Mason5caf2a02007-04-02 11:20:42 -0400131 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400132 struct extent_buffer *l;
Chris Mason3768f362007-03-13 16:47:54 -0400133 int ret;
134 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -0400135 unsigned long ptr;
Alexander Block8ea05e32012-07-25 17:35:53 +0200136 int old_len;
Chris Mason3768f362007-03-13 16:47:54 -0400137
Chris Mason5caf2a02007-04-02 11:20:42 -0400138 path = btrfs_alloc_path();
Jeff Mahoneyb45a9d82011-10-03 23:22:44 -0400139 if (!path)
140 return -ENOMEM;
141
Chris Mason5caf2a02007-04-02 11:20:42 -0400142 ret = btrfs_search_slot(trans, root, key, path, 0, 1);
David Sterba005d6422012-09-18 07:52:32 -0600143 if (ret < 0) {
144 btrfs_abort_transaction(trans, root, ret);
145 goto out;
146 }
Chris Masond6667462008-01-03 14:51:00 -0500147
148 if (ret != 0) {
149 btrfs_print_leaf(root, path->nodes[0]);
Chris Masond3977122009-01-05 21:25:51 -0500150 printk(KERN_CRIT "unable to update root key %llu %u %llu\n",
151 (unsigned long long)key->objectid, key->type,
152 (unsigned long long)key->offset);
Chris Masond6667462008-01-03 14:51:00 -0500153 BUG_ON(1);
154 }
155
Chris Mason5f39d392007-10-15 16:14:19 -0400156 l = path->nodes[0];
Chris Mason5caf2a02007-04-02 11:20:42 -0400157 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -0400158 ptr = btrfs_item_ptr_offset(l, slot);
Alexander Block8ea05e32012-07-25 17:35:53 +0200159 old_len = btrfs_item_size_nr(l, slot);
160
161 /*
162 * If this is the first time we update the root item which originated
163 * from an older kernel, we need to enlarge the item size to make room
164 * for the added fields.
165 */
166 if (old_len < sizeof(*item)) {
167 btrfs_release_path(path);
168 ret = btrfs_search_slot(trans, root, key, path,
169 -1, 1);
David Sterba005d6422012-09-18 07:52:32 -0600170 if (ret < 0) {
171 btrfs_abort_transaction(trans, root, ret);
172 goto out;
173 }
174
Alexander Block8ea05e32012-07-25 17:35:53 +0200175 ret = btrfs_del_item(trans, root, path);
David Sterba005d6422012-09-18 07:52:32 -0600176 if (ret < 0) {
177 btrfs_abort_transaction(trans, root, ret);
178 goto out;
179 }
Alexander Block8ea05e32012-07-25 17:35:53 +0200180 btrfs_release_path(path);
181 ret = btrfs_insert_empty_item(trans, root, path,
182 key, sizeof(*item));
David Sterba005d6422012-09-18 07:52:32 -0600183 if (ret < 0) {
184 btrfs_abort_transaction(trans, root, ret);
185 goto out;
186 }
Alexander Block8ea05e32012-07-25 17:35:53 +0200187 l = path->nodes[0];
188 slot = path->slots[0];
189 ptr = btrfs_item_ptr_offset(l, slot);
190 }
191
192 /*
193 * Update generation_v2 so at the next mount we know the new root
194 * fields are valid.
195 */
196 btrfs_set_root_generation_v2(item, btrfs_root_generation(item));
197
Chris Mason5f39d392007-10-15 16:14:19 -0400198 write_extent_buffer(l, item, ptr, sizeof(*item));
Chris Mason5caf2a02007-04-02 11:20:42 -0400199 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason3768f362007-03-13 16:47:54 -0400200out:
Chris Mason5caf2a02007-04-02 11:20:42 -0400201 btrfs_free_path(path);
Chris Mason3768f362007-03-13 16:47:54 -0400202 return ret;
203}
204
Jeff Mahoneyd16cb052011-10-03 23:22:34 -0400205int btrfs_insert_root(struct btrfs_trans_handle *trans, struct btrfs_root *root,
206 struct btrfs_key *key, struct btrfs_root_item *item)
Chris Mason3768f362007-03-13 16:47:54 -0400207{
Alexander Block8ea05e32012-07-25 17:35:53 +0200208 /*
209 * Make sure generation v1 and v2 match. See update_root for details.
210 */
211 btrfs_set_root_generation_v2(item, btrfs_root_generation(item));
Jeff Mahoneyd16cb052011-10-03 23:22:34 -0400212 return btrfs_insert_item(trans, root, key, item, sizeof(*item));
Chris Mason3768f362007-03-13 16:47:54 -0400213}
214
Chris Masond352ac62008-09-29 15:18:18 -0400215/*
216 * at mount time we want to find all the old transaction snapshots that were in
Chris Masond3977122009-01-05 21:25:51 -0500217 * the process of being deleted if we crashed. This is any root item with an
218 * offset lower than the latest root. They need to be queued for deletion to
219 * finish what was happening when we crashed.
Chris Masond352ac62008-09-29 15:18:18 -0400220 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400221int btrfs_find_dead_roots(struct btrfs_root *root, u64 objectid)
Chris Mason5eda7b52007-06-22 14:16:25 -0400222{
223 struct btrfs_root *dead_root;
Chris Mason5eda7b52007-06-22 14:16:25 -0400224 struct btrfs_root_item *ri;
225 struct btrfs_key key;
Chris Masona7a16fd2008-06-26 10:34:20 -0400226 struct btrfs_key found_key;
Chris Mason5eda7b52007-06-22 14:16:25 -0400227 struct btrfs_path *path;
228 int ret;
229 u32 nritems;
Chris Mason5f39d392007-10-15 16:14:19 -0400230 struct extent_buffer *leaf;
Chris Mason5eda7b52007-06-22 14:16:25 -0400231 int slot;
232
Chris Mason5ce14bb2007-09-11 11:15:39 -0400233 key.objectid = objectid;
Chris Mason5eda7b52007-06-22 14:16:25 -0400234 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
235 key.offset = 0;
236 path = btrfs_alloc_path();
237 if (!path)
238 return -ENOMEM;
Chris Masona7a16fd2008-06-26 10:34:20 -0400239
240again:
Chris Mason5eda7b52007-06-22 14:16:25 -0400241 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
242 if (ret < 0)
243 goto err;
Chris Masond3977122009-01-05 21:25:51 -0500244 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400245 leaf = path->nodes[0];
246 nritems = btrfs_header_nritems(leaf);
Chris Mason5eda7b52007-06-22 14:16:25 -0400247 slot = path->slots[0];
248 if (slot >= nritems) {
249 ret = btrfs_next_leaf(root, path);
250 if (ret)
251 break;
Chris Mason5f39d392007-10-15 16:14:19 -0400252 leaf = path->nodes[0];
253 nritems = btrfs_header_nritems(leaf);
Chris Mason5eda7b52007-06-22 14:16:25 -0400254 slot = path->slots[0];
255 }
Chris Mason5f39d392007-10-15 16:14:19 -0400256 btrfs_item_key_to_cpu(leaf, &key, slot);
Chris Mason5eda7b52007-06-22 14:16:25 -0400257 if (btrfs_key_type(&key) != BTRFS_ROOT_ITEM_KEY)
258 goto next;
Chris Mason5ce14bb2007-09-11 11:15:39 -0400259
260 if (key.objectid < objectid)
261 goto next;
262
263 if (key.objectid > objectid)
264 break;
265
Chris Mason5eda7b52007-06-22 14:16:25 -0400266 ri = btrfs_item_ptr(leaf, slot, struct btrfs_root_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400267 if (btrfs_disk_root_refs(leaf, ri) != 0)
Chris Mason5eda7b52007-06-22 14:16:25 -0400268 goto next;
Chris Mason5ce14bb2007-09-11 11:15:39 -0400269
Chris Masona7a16fd2008-06-26 10:34:20 -0400270 memcpy(&found_key, &key, sizeof(key));
271 key.offset++;
David Sterbab3b4aa72011-04-21 01:20:15 +0200272 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400273 dead_root =
274 btrfs_read_fs_root_no_radix(root->fs_info->tree_root,
275 &found_key);
Aneesha1f39632007-07-11 10:03:27 -0400276 if (IS_ERR(dead_root)) {
277 ret = PTR_ERR(dead_root);
Chris Mason5eda7b52007-06-22 14:16:25 -0400278 goto err;
279 }
Chris Mason5ce14bb2007-09-11 11:15:39 -0400280
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400281 ret = btrfs_add_dead_root(dead_root);
Chris Mason5eda7b52007-06-22 14:16:25 -0400282 if (ret)
283 goto err;
Chris Masona7a16fd2008-06-26 10:34:20 -0400284 goto again;
Chris Mason5eda7b52007-06-22 14:16:25 -0400285next:
286 slot++;
287 path->slots[0]++;
288 }
289 ret = 0;
290err:
291 btrfs_free_path(path);
292 return ret;
293}
294
Yan, Zheng76dda932009-09-21 16:00:26 -0400295int btrfs_find_orphan_roots(struct btrfs_root *tree_root)
296{
297 struct extent_buffer *leaf;
298 struct btrfs_path *path;
299 struct btrfs_key key;
Yan, Zhengd68fc572010-05-16 10:49:58 -0400300 struct btrfs_key root_key;
301 struct btrfs_root *root;
Yan, Zheng76dda932009-09-21 16:00:26 -0400302 int err = 0;
303 int ret;
304
305 path = btrfs_alloc_path();
306 if (!path)
307 return -ENOMEM;
308
309 key.objectid = BTRFS_ORPHAN_OBJECTID;
310 key.type = BTRFS_ORPHAN_ITEM_KEY;
311 key.offset = 0;
312
Yan, Zhengd68fc572010-05-16 10:49:58 -0400313 root_key.type = BTRFS_ROOT_ITEM_KEY;
314 root_key.offset = (u64)-1;
315
Yan, Zheng76dda932009-09-21 16:00:26 -0400316 while (1) {
317 ret = btrfs_search_slot(NULL, tree_root, &key, path, 0, 0);
318 if (ret < 0) {
319 err = ret;
320 break;
321 }
322
323 leaf = path->nodes[0];
324 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
325 ret = btrfs_next_leaf(tree_root, path);
326 if (ret < 0)
327 err = ret;
328 if (ret != 0)
329 break;
330 leaf = path->nodes[0];
331 }
332
333 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +0200334 btrfs_release_path(path);
Yan, Zheng76dda932009-09-21 16:00:26 -0400335
336 if (key.objectid != BTRFS_ORPHAN_OBJECTID ||
337 key.type != BTRFS_ORPHAN_ITEM_KEY)
338 break;
339
Yan, Zhengd68fc572010-05-16 10:49:58 -0400340 root_key.objectid = key.offset;
341 key.offset++;
342
343 root = btrfs_read_fs_root_no_name(tree_root->fs_info,
344 &root_key);
345 if (!IS_ERR(root))
346 continue;
347
348 ret = PTR_ERR(root);
349 if (ret != -ENOENT) {
Yan, Zheng76dda932009-09-21 16:00:26 -0400350 err = ret;
351 break;
352 }
353
Yan, Zhengd68fc572010-05-16 10:49:58 -0400354 ret = btrfs_find_dead_roots(tree_root, root_key.objectid);
355 if (ret) {
356 err = ret;
357 break;
358 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400359 }
360
361 btrfs_free_path(path);
362 return err;
363}
364
Chris Masond352ac62008-09-29 15:18:18 -0400365/* drop the root item for 'key' from 'root' */
Chris Masone089f052007-03-16 16:20:31 -0400366int btrfs_del_root(struct btrfs_trans_handle *trans, struct btrfs_root *root,
367 struct btrfs_key *key)
Chris Mason3768f362007-03-13 16:47:54 -0400368{
Chris Mason5caf2a02007-04-02 11:20:42 -0400369 struct btrfs_path *path;
Chris Mason3768f362007-03-13 16:47:54 -0400370 int ret;
Chris Masonc5739bb2007-04-10 09:27:04 -0400371 struct btrfs_root_item *ri;
Chris Mason5f39d392007-10-15 16:14:19 -0400372 struct extent_buffer *leaf;
Chris Mason3768f362007-03-13 16:47:54 -0400373
Chris Mason5caf2a02007-04-02 11:20:42 -0400374 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +0000375 if (!path)
376 return -ENOMEM;
Chris Mason5caf2a02007-04-02 11:20:42 -0400377 ret = btrfs_search_slot(trans, root, key, path, -1, 1);
Chris Mason3768f362007-03-13 16:47:54 -0400378 if (ret < 0)
379 goto out;
Chris Masonedbd8d42007-12-21 16:27:24 -0500380
Chris Mason3768f362007-03-13 16:47:54 -0400381 BUG_ON(ret != 0);
Chris Mason5f39d392007-10-15 16:14:19 -0400382 leaf = path->nodes[0];
383 ri = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_item);
Chris Masonc5739bb2007-04-10 09:27:04 -0400384
Chris Mason5eda7b52007-06-22 14:16:25 -0400385 ret = btrfs_del_item(trans, root, path);
Chris Mason3768f362007-03-13 16:47:54 -0400386out:
Chris Mason5caf2a02007-04-02 11:20:42 -0400387 btrfs_free_path(path);
Chris Mason3768f362007-03-13 16:47:54 -0400388 return ret;
389}
Chris Mason0660b5a2008-11-17 20:37:39 -0500390
391int btrfs_del_root_ref(struct btrfs_trans_handle *trans,
392 struct btrfs_root *tree_root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400393 u64 root_id, u64 ref_id, u64 dirid, u64 *sequence,
394 const char *name, int name_len)
395
Chris Mason0660b5a2008-11-17 20:37:39 -0500396{
Chris Mason0660b5a2008-11-17 20:37:39 -0500397 struct btrfs_path *path;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400398 struct btrfs_root_ref *ref;
399 struct extent_buffer *leaf;
400 struct btrfs_key key;
401 unsigned long ptr;
402 int err = 0;
403 int ret;
Chris Mason0660b5a2008-11-17 20:37:39 -0500404
405 path = btrfs_alloc_path();
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400406 if (!path)
407 return -ENOMEM;
Chris Mason0660b5a2008-11-17 20:37:39 -0500408
409 key.objectid = root_id;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400410 key.type = BTRFS_ROOT_BACKREF_KEY;
Chris Mason0660b5a2008-11-17 20:37:39 -0500411 key.offset = ref_id;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400412again:
Chris Mason0660b5a2008-11-17 20:37:39 -0500413 ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400414 BUG_ON(ret < 0);
415 if (ret == 0) {
416 leaf = path->nodes[0];
417 ref = btrfs_item_ptr(leaf, path->slots[0],
418 struct btrfs_root_ref);
Chris Mason0660b5a2008-11-17 20:37:39 -0500419
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400420 WARN_ON(btrfs_root_ref_dirid(leaf, ref) != dirid);
421 WARN_ON(btrfs_root_ref_name_len(leaf, ref) != name_len);
422 ptr = (unsigned long)(ref + 1);
423 WARN_ON(memcmp_extent_buffer(leaf, name, ptr, name_len));
424 *sequence = btrfs_root_ref_sequence(leaf, ref);
425
426 ret = btrfs_del_item(trans, tree_root, path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +0000427 if (ret) {
428 err = ret;
429 goto out;
430 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400431 } else
432 err = -ENOENT;
433
434 if (key.type == BTRFS_ROOT_BACKREF_KEY) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200435 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400436 key.objectid = ref_id;
437 key.type = BTRFS_ROOT_REF_KEY;
438 key.offset = root_id;
439 goto again;
440 }
Chris Mason0660b5a2008-11-17 20:37:39 -0500441
Tsutomu Itoh65a246c2011-05-19 04:37:44 +0000442out:
Chris Mason0660b5a2008-11-17 20:37:39 -0500443 btrfs_free_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400444 return err;
Chris Mason0660b5a2008-11-17 20:37:39 -0500445}
446
Chris Masonea9e8b12008-11-17 21:14:24 -0500447int btrfs_find_root_ref(struct btrfs_root *tree_root,
448 struct btrfs_path *path,
449 u64 root_id, u64 ref_id)
450{
451 struct btrfs_key key;
452 int ret;
453
454 key.objectid = root_id;
455 key.type = BTRFS_ROOT_REF_KEY;
456 key.offset = ref_id;
457
458 ret = btrfs_search_slot(NULL, tree_root, &key, path, 0, 0);
459 return ret;
460}
461
Chris Mason0660b5a2008-11-17 20:37:39 -0500462/*
463 * add a btrfs_root_ref item. type is either BTRFS_ROOT_REF_KEY
464 * or BTRFS_ROOT_BACKREF_KEY.
465 *
466 * The dirid, sequence, name and name_len refer to the directory entry
467 * that is referencing the root.
468 *
469 * For a forward ref, the root_id is the id of the tree referencing
470 * the root and ref_id is the id of the subvol or snapshot.
471 *
472 * For a back ref the root_id is the id of the subvol or snapshot and
473 * ref_id is the id of the tree referencing it.
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100474 *
475 * Will return 0, -ENOMEM, or anything from the CoW path
Chris Mason0660b5a2008-11-17 20:37:39 -0500476 */
477int btrfs_add_root_ref(struct btrfs_trans_handle *trans,
478 struct btrfs_root *tree_root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400479 u64 root_id, u64 ref_id, u64 dirid, u64 sequence,
Chris Mason0660b5a2008-11-17 20:37:39 -0500480 const char *name, int name_len)
481{
482 struct btrfs_key key;
483 int ret;
484 struct btrfs_path *path;
485 struct btrfs_root_ref *ref;
486 struct extent_buffer *leaf;
487 unsigned long ptr;
488
Chris Mason0660b5a2008-11-17 20:37:39 -0500489 path = btrfs_alloc_path();
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400490 if (!path)
491 return -ENOMEM;
Chris Mason0660b5a2008-11-17 20:37:39 -0500492
493 key.objectid = root_id;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400494 key.type = BTRFS_ROOT_BACKREF_KEY;
Chris Mason0660b5a2008-11-17 20:37:39 -0500495 key.offset = ref_id;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400496again:
Chris Mason0660b5a2008-11-17 20:37:39 -0500497 ret = btrfs_insert_empty_item(trans, tree_root, path, &key,
498 sizeof(*ref) + name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100499 if (ret) {
500 btrfs_abort_transaction(trans, tree_root, ret);
501 btrfs_free_path(path);
502 return ret;
503 }
Chris Mason0660b5a2008-11-17 20:37:39 -0500504
505 leaf = path->nodes[0];
506 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
507 btrfs_set_root_ref_dirid(leaf, ref, dirid);
508 btrfs_set_root_ref_sequence(leaf, ref, sequence);
509 btrfs_set_root_ref_name_len(leaf, ref, name_len);
510 ptr = (unsigned long)(ref + 1);
511 write_extent_buffer(leaf, name, ptr, name_len);
512 btrfs_mark_buffer_dirty(leaf);
513
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400514 if (key.type == BTRFS_ROOT_BACKREF_KEY) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200515 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400516 key.objectid = ref_id;
517 key.type = BTRFS_ROOT_REF_KEY;
518 key.offset = root_id;
519 goto again;
520 }
521
Chris Mason0660b5a2008-11-17 20:37:39 -0500522 btrfs_free_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400523 return 0;
Chris Mason0660b5a2008-11-17 20:37:39 -0500524}
Li Zefan08fe4db2011-03-28 02:01:25 +0000525
526/*
527 * Old btrfs forgets to init root_item->flags and root_item->byte_limit
528 * for subvolumes. To work around this problem, we steal a bit from
529 * root_item->inode_item->flags, and use it to indicate if those fields
530 * have been properly initialized.
531 */
532void btrfs_check_and_init_root_item(struct btrfs_root_item *root_item)
533{
534 u64 inode_flags = le64_to_cpu(root_item->inode.flags);
535
536 if (!(inode_flags & BTRFS_INODE_ROOT_ITEM_INIT)) {
537 inode_flags |= BTRFS_INODE_ROOT_ITEM_INIT;
538 root_item->inode.flags = cpu_to_le64(inode_flags);
539 root_item->flags = 0;
540 root_item->byte_limit = 0;
541 }
542}
Alexander Block8ea05e32012-07-25 17:35:53 +0200543
544void btrfs_update_root_times(struct btrfs_trans_handle *trans,
545 struct btrfs_root *root)
546{
547 struct btrfs_root_item *item = &root->root_item;
548 struct timespec ct = CURRENT_TIME;
549
Anand Jain5f3ab902012-12-07 09:28:54 +0000550 spin_lock(&root->root_item_lock);
Dan Carpenterdadd1102012-07-30 02:10:44 -0600551 item->ctransid = cpu_to_le64(trans->transid);
Alexander Block8ea05e32012-07-25 17:35:53 +0200552 item->ctime.sec = cpu_to_le64(ct.tv_sec);
Dan Carpenterdadd1102012-07-30 02:10:44 -0600553 item->ctime.nsec = cpu_to_le32(ct.tv_nsec);
Anand Jain5f3ab902012-12-07 09:28:54 +0000554 spin_unlock(&root->root_item_lock);
Alexander Block8ea05e32012-07-25 17:35:53 +0200555}