blob: 10d8e4d88071747651afd3a875eae0fb407c22e4 [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 */
32void btrfs_read_root_item(struct btrfs_root *root,
33 struct extent_buffer *eb, int slot,
34 struct btrfs_root_item *item)
35{
36 uuid_le uuid;
37 int len;
38 int need_reset = 0;
39
40 len = btrfs_item_size_nr(eb, slot);
41 read_extent_buffer(eb, item, btrfs_item_ptr_offset(eb, slot),
42 min_t(int, len, (int)sizeof(*item)));
43 if (len < sizeof(*item))
44 need_reset = 1;
45 if (!need_reset && btrfs_root_generation(item)
46 != btrfs_root_generation_v2(item)) {
47 if (btrfs_root_generation_v2(item) != 0) {
48 printk(KERN_WARNING "btrfs: mismatching "
49 "generation and generation_v2 "
50 "found in root item. This root "
51 "was probably mounted with an "
52 "older kernel. Resetting all "
53 "new fields.\n");
54 }
55 need_reset = 1;
56 }
57 if (need_reset) {
58 memset(&item->generation_v2, 0,
59 sizeof(*item) - offsetof(struct btrfs_root_item,
60 generation_v2));
61
62 uuid_le_gen(&uuid);
63 memcpy(item->uuid, uuid.b, BTRFS_UUID_SIZE);
64 }
65}
66
67/*
Chris Masond352ac62008-09-29 15:18:18 -040068 * lookup the root with the highest offset for a given objectid. The key we do
69 * find is copied into 'key'. If we find something return 0, otherwise 1, < 0
70 * on error.
71 */
Chris Mason3768f362007-03-13 16:47:54 -040072int btrfs_find_last_root(struct btrfs_root *root, u64 objectid,
73 struct btrfs_root_item *item, struct btrfs_key *key)
74{
Chris Mason5caf2a02007-04-02 11:20:42 -040075 struct btrfs_path *path;
Chris Mason3768f362007-03-13 16:47:54 -040076 struct btrfs_key search_key;
Chris Mason5f39d392007-10-15 16:14:19 -040077 struct btrfs_key found_key;
78 struct extent_buffer *l;
Chris Mason3768f362007-03-13 16:47:54 -040079 int ret;
80 int slot;
81
82 search_key.objectid = objectid;
Chris Mason0660b5a2008-11-17 20:37:39 -050083 search_key.type = BTRFS_ROOT_ITEM_KEY;
Chris Mason5eda7b52007-06-22 14:16:25 -040084 search_key.offset = (u64)-1;
Chris Mason3768f362007-03-13 16:47:54 -040085
Chris Mason5caf2a02007-04-02 11:20:42 -040086 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +000087 if (!path)
88 return -ENOMEM;
Chris Mason5caf2a02007-04-02 11:20:42 -040089 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
Chris Mason3768f362007-03-13 16:47:54 -040090 if (ret < 0)
91 goto out;
Chris Mason5f39d392007-10-15 16:14:19 -040092
Chris Mason3768f362007-03-13 16:47:54 -040093 BUG_ON(ret == 0);
Yan, Zheng76dda932009-09-21 16:00:26 -040094 if (path->slots[0] == 0) {
Chris Mason3768f362007-03-13 16:47:54 -040095 ret = 1;
96 goto out;
97 }
Yan, Zheng76dda932009-09-21 16:00:26 -040098 l = path->nodes[0];
99 slot = path->slots[0] - 1;
100 btrfs_item_key_to_cpu(l, &found_key, slot);
101 if (found_key.objectid != objectid ||
102 found_key.type != BTRFS_ROOT_ITEM_KEY) {
103 ret = 1;
104 goto out;
105 }
106 if (item)
Alexander Block8ea05e32012-07-25 17:35:53 +0200107 btrfs_read_root_item(root, l, slot, item);
Yan, Zheng76dda932009-09-21 16:00:26 -0400108 if (key)
109 memcpy(key, &found_key, sizeof(found_key));
Alexander Block8ea05e32012-07-25 17:35:53 +0200110
Chris Mason3768f362007-03-13 16:47:54 -0400111 ret = 0;
112out:
Chris Mason5caf2a02007-04-02 11:20:42 -0400113 btrfs_free_path(path);
Chris Mason3768f362007-03-13 16:47:54 -0400114 return ret;
115}
116
Mark Fashehbf5f32e2011-07-14 21:23:06 +0000117void btrfs_set_root_node(struct btrfs_root_item *item,
118 struct extent_buffer *node)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400119{
120 btrfs_set_root_bytenr(item, node->start);
121 btrfs_set_root_level(item, btrfs_header_level(node));
122 btrfs_set_root_generation(item, btrfs_header_generation(node));
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400123}
124
Chris Masond352ac62008-09-29 15:18:18 -0400125/*
126 * copy the data in 'item' into the btree
127 */
Chris Masone089f052007-03-16 16:20:31 -0400128int btrfs_update_root(struct btrfs_trans_handle *trans, struct btrfs_root
129 *root, struct btrfs_key *key, struct btrfs_root_item
130 *item)
Chris Mason3768f362007-03-13 16:47:54 -0400131{
Chris Mason5caf2a02007-04-02 11:20:42 -0400132 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400133 struct extent_buffer *l;
Chris Mason3768f362007-03-13 16:47:54 -0400134 int ret;
135 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -0400136 unsigned long ptr;
Alexander Block8ea05e32012-07-25 17:35:53 +0200137 int old_len;
Chris Mason3768f362007-03-13 16:47:54 -0400138
Chris Mason5caf2a02007-04-02 11:20:42 -0400139 path = btrfs_alloc_path();
Jeff Mahoneyb45a9d82011-10-03 23:22:44 -0400140 if (!path)
141 return -ENOMEM;
142
Chris Mason5caf2a02007-04-02 11:20:42 -0400143 ret = btrfs_search_slot(trans, root, key, path, 0, 1);
Alexander Block8ea05e32012-07-25 17:35:53 +0200144 if (ret < 0)
145 goto out_abort;
Chris Masond6667462008-01-03 14:51:00 -0500146
147 if (ret != 0) {
148 btrfs_print_leaf(root, path->nodes[0]);
Chris Masond3977122009-01-05 21:25:51 -0500149 printk(KERN_CRIT "unable to update root key %llu %u %llu\n",
150 (unsigned long long)key->objectid, key->type,
151 (unsigned long long)key->offset);
Chris Masond6667462008-01-03 14:51:00 -0500152 BUG_ON(1);
153 }
154
Chris Mason5f39d392007-10-15 16:14:19 -0400155 l = path->nodes[0];
Chris Mason5caf2a02007-04-02 11:20:42 -0400156 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -0400157 ptr = btrfs_item_ptr_offset(l, slot);
Alexander Block8ea05e32012-07-25 17:35:53 +0200158 old_len = btrfs_item_size_nr(l, slot);
159
160 /*
161 * If this is the first time we update the root item which originated
162 * from an older kernel, we need to enlarge the item size to make room
163 * for the added fields.
164 */
165 if (old_len < sizeof(*item)) {
166 btrfs_release_path(path);
167 ret = btrfs_search_slot(trans, root, key, path,
168 -1, 1);
169 if (ret < 0)
170 goto out_abort;
171 ret = btrfs_del_item(trans, root, path);
172 if (ret < 0)
173 goto out_abort;
174 btrfs_release_path(path);
175 ret = btrfs_insert_empty_item(trans, root, path,
176 key, sizeof(*item));
177 if (ret < 0)
178 goto out_abort;
179 l = path->nodes[0];
180 slot = path->slots[0];
181 ptr = btrfs_item_ptr_offset(l, slot);
182 }
183
184 /*
185 * Update generation_v2 so at the next mount we know the new root
186 * fields are valid.
187 */
188 btrfs_set_root_generation_v2(item, btrfs_root_generation(item));
189
Chris Mason5f39d392007-10-15 16:14:19 -0400190 write_extent_buffer(l, item, ptr, sizeof(*item));
Chris Mason5caf2a02007-04-02 11:20:42 -0400191 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason3768f362007-03-13 16:47:54 -0400192out:
Chris Mason5caf2a02007-04-02 11:20:42 -0400193 btrfs_free_path(path);
Chris Mason3768f362007-03-13 16:47:54 -0400194 return ret;
Alexander Block8ea05e32012-07-25 17:35:53 +0200195
196out_abort:
197 btrfs_abort_transaction(trans, root, ret);
198 goto out;
Chris Mason3768f362007-03-13 16:47:54 -0400199}
200
Jeff Mahoneyd16cb052011-10-03 23:22:34 -0400201int btrfs_insert_root(struct btrfs_trans_handle *trans, struct btrfs_root *root,
202 struct btrfs_key *key, struct btrfs_root_item *item)
Chris Mason3768f362007-03-13 16:47:54 -0400203{
Alexander Block8ea05e32012-07-25 17:35:53 +0200204 /*
205 * Make sure generation v1 and v2 match. See update_root for details.
206 */
207 btrfs_set_root_generation_v2(item, btrfs_root_generation(item));
Jeff Mahoneyd16cb052011-10-03 23:22:34 -0400208 return btrfs_insert_item(trans, root, key, item, sizeof(*item));
Chris Mason3768f362007-03-13 16:47:54 -0400209}
210
Chris Masond352ac62008-09-29 15:18:18 -0400211/*
212 * at mount time we want to find all the old transaction snapshots that were in
Chris Masond3977122009-01-05 21:25:51 -0500213 * the process of being deleted if we crashed. This is any root item with an
214 * offset lower than the latest root. They need to be queued for deletion to
215 * finish what was happening when we crashed.
Chris Masond352ac62008-09-29 15:18:18 -0400216 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400217int btrfs_find_dead_roots(struct btrfs_root *root, u64 objectid)
Chris Mason5eda7b52007-06-22 14:16:25 -0400218{
219 struct btrfs_root *dead_root;
Chris Mason5eda7b52007-06-22 14:16:25 -0400220 struct btrfs_root_item *ri;
221 struct btrfs_key key;
Chris Masona7a16fd2008-06-26 10:34:20 -0400222 struct btrfs_key found_key;
Chris Mason5eda7b52007-06-22 14:16:25 -0400223 struct btrfs_path *path;
224 int ret;
225 u32 nritems;
Chris Mason5f39d392007-10-15 16:14:19 -0400226 struct extent_buffer *leaf;
Chris Mason5eda7b52007-06-22 14:16:25 -0400227 int slot;
228
Chris Mason5ce14bb2007-09-11 11:15:39 -0400229 key.objectid = objectid;
Chris Mason5eda7b52007-06-22 14:16:25 -0400230 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
231 key.offset = 0;
232 path = btrfs_alloc_path();
233 if (!path)
234 return -ENOMEM;
Chris Masona7a16fd2008-06-26 10:34:20 -0400235
236again:
Chris Mason5eda7b52007-06-22 14:16:25 -0400237 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
238 if (ret < 0)
239 goto err;
Chris Masond3977122009-01-05 21:25:51 -0500240 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400241 leaf = path->nodes[0];
242 nritems = btrfs_header_nritems(leaf);
Chris Mason5eda7b52007-06-22 14:16:25 -0400243 slot = path->slots[0];
244 if (slot >= nritems) {
245 ret = btrfs_next_leaf(root, path);
246 if (ret)
247 break;
Chris Mason5f39d392007-10-15 16:14:19 -0400248 leaf = path->nodes[0];
249 nritems = btrfs_header_nritems(leaf);
Chris Mason5eda7b52007-06-22 14:16:25 -0400250 slot = path->slots[0];
251 }
Chris Mason5f39d392007-10-15 16:14:19 -0400252 btrfs_item_key_to_cpu(leaf, &key, slot);
Chris Mason5eda7b52007-06-22 14:16:25 -0400253 if (btrfs_key_type(&key) != BTRFS_ROOT_ITEM_KEY)
254 goto next;
Chris Mason5ce14bb2007-09-11 11:15:39 -0400255
256 if (key.objectid < objectid)
257 goto next;
258
259 if (key.objectid > objectid)
260 break;
261
Chris Mason5eda7b52007-06-22 14:16:25 -0400262 ri = btrfs_item_ptr(leaf, slot, struct btrfs_root_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400263 if (btrfs_disk_root_refs(leaf, ri) != 0)
Chris Mason5eda7b52007-06-22 14:16:25 -0400264 goto next;
Chris Mason5ce14bb2007-09-11 11:15:39 -0400265
Chris Masona7a16fd2008-06-26 10:34:20 -0400266 memcpy(&found_key, &key, sizeof(key));
267 key.offset++;
David Sterbab3b4aa72011-04-21 01:20:15 +0200268 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400269 dead_root =
270 btrfs_read_fs_root_no_radix(root->fs_info->tree_root,
271 &found_key);
Aneesha1f39632007-07-11 10:03:27 -0400272 if (IS_ERR(dead_root)) {
273 ret = PTR_ERR(dead_root);
Chris Mason5eda7b52007-06-22 14:16:25 -0400274 goto err;
275 }
Chris Mason5ce14bb2007-09-11 11:15:39 -0400276
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400277 ret = btrfs_add_dead_root(dead_root);
Chris Mason5eda7b52007-06-22 14:16:25 -0400278 if (ret)
279 goto err;
Chris Masona7a16fd2008-06-26 10:34:20 -0400280 goto again;
Chris Mason5eda7b52007-06-22 14:16:25 -0400281next:
282 slot++;
283 path->slots[0]++;
284 }
285 ret = 0;
286err:
287 btrfs_free_path(path);
288 return ret;
289}
290
Yan, Zheng76dda932009-09-21 16:00:26 -0400291int btrfs_find_orphan_roots(struct btrfs_root *tree_root)
292{
293 struct extent_buffer *leaf;
294 struct btrfs_path *path;
295 struct btrfs_key key;
Yan, Zhengd68fc572010-05-16 10:49:58 -0400296 struct btrfs_key root_key;
297 struct btrfs_root *root;
Yan, Zheng76dda932009-09-21 16:00:26 -0400298 int err = 0;
299 int ret;
300
301 path = btrfs_alloc_path();
302 if (!path)
303 return -ENOMEM;
304
305 key.objectid = BTRFS_ORPHAN_OBJECTID;
306 key.type = BTRFS_ORPHAN_ITEM_KEY;
307 key.offset = 0;
308
Yan, Zhengd68fc572010-05-16 10:49:58 -0400309 root_key.type = BTRFS_ROOT_ITEM_KEY;
310 root_key.offset = (u64)-1;
311
Yan, Zheng76dda932009-09-21 16:00:26 -0400312 while (1) {
313 ret = btrfs_search_slot(NULL, tree_root, &key, path, 0, 0);
314 if (ret < 0) {
315 err = ret;
316 break;
317 }
318
319 leaf = path->nodes[0];
320 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
321 ret = btrfs_next_leaf(tree_root, path);
322 if (ret < 0)
323 err = ret;
324 if (ret != 0)
325 break;
326 leaf = path->nodes[0];
327 }
328
329 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +0200330 btrfs_release_path(path);
Yan, Zheng76dda932009-09-21 16:00:26 -0400331
332 if (key.objectid != BTRFS_ORPHAN_OBJECTID ||
333 key.type != BTRFS_ORPHAN_ITEM_KEY)
334 break;
335
Yan, Zhengd68fc572010-05-16 10:49:58 -0400336 root_key.objectid = key.offset;
337 key.offset++;
338
339 root = btrfs_read_fs_root_no_name(tree_root->fs_info,
340 &root_key);
341 if (!IS_ERR(root))
342 continue;
343
344 ret = PTR_ERR(root);
345 if (ret != -ENOENT) {
Yan, Zheng76dda932009-09-21 16:00:26 -0400346 err = ret;
347 break;
348 }
349
Yan, Zhengd68fc572010-05-16 10:49:58 -0400350 ret = btrfs_find_dead_roots(tree_root, root_key.objectid);
351 if (ret) {
352 err = ret;
353 break;
354 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400355 }
356
357 btrfs_free_path(path);
358 return err;
359}
360
Chris Masond352ac62008-09-29 15:18:18 -0400361/* drop the root item for 'key' from 'root' */
Chris Masone089f052007-03-16 16:20:31 -0400362int btrfs_del_root(struct btrfs_trans_handle *trans, struct btrfs_root *root,
363 struct btrfs_key *key)
Chris Mason3768f362007-03-13 16:47:54 -0400364{
Chris Mason5caf2a02007-04-02 11:20:42 -0400365 struct btrfs_path *path;
Chris Mason3768f362007-03-13 16:47:54 -0400366 int ret;
Chris Masonc5739bb2007-04-10 09:27:04 -0400367 struct btrfs_root_item *ri;
Chris Mason5f39d392007-10-15 16:14:19 -0400368 struct extent_buffer *leaf;
Chris Mason3768f362007-03-13 16:47:54 -0400369
Chris Mason5caf2a02007-04-02 11:20:42 -0400370 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +0000371 if (!path)
372 return -ENOMEM;
Chris Mason5caf2a02007-04-02 11:20:42 -0400373 ret = btrfs_search_slot(trans, root, key, path, -1, 1);
Chris Mason3768f362007-03-13 16:47:54 -0400374 if (ret < 0)
375 goto out;
Chris Masonedbd8d42007-12-21 16:27:24 -0500376
Chris Mason3768f362007-03-13 16:47:54 -0400377 BUG_ON(ret != 0);
Chris Mason5f39d392007-10-15 16:14:19 -0400378 leaf = path->nodes[0];
379 ri = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_item);
Chris Masonc5739bb2007-04-10 09:27:04 -0400380
Chris Mason5eda7b52007-06-22 14:16:25 -0400381 ret = btrfs_del_item(trans, root, path);
Chris Mason3768f362007-03-13 16:47:54 -0400382out:
Chris Mason5caf2a02007-04-02 11:20:42 -0400383 btrfs_free_path(path);
Chris Mason3768f362007-03-13 16:47:54 -0400384 return ret;
385}
Chris Mason0660b5a2008-11-17 20:37:39 -0500386
387int btrfs_del_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,
390 const char *name, int name_len)
391
Chris Mason0660b5a2008-11-17 20:37:39 -0500392{
Chris Mason0660b5a2008-11-17 20:37:39 -0500393 struct btrfs_path *path;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400394 struct btrfs_root_ref *ref;
395 struct extent_buffer *leaf;
396 struct btrfs_key key;
397 unsigned long ptr;
398 int err = 0;
399 int ret;
Chris Mason0660b5a2008-11-17 20:37:39 -0500400
401 path = btrfs_alloc_path();
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400402 if (!path)
403 return -ENOMEM;
Chris Mason0660b5a2008-11-17 20:37:39 -0500404
405 key.objectid = root_id;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400406 key.type = BTRFS_ROOT_BACKREF_KEY;
Chris Mason0660b5a2008-11-17 20:37:39 -0500407 key.offset = ref_id;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400408again:
Chris Mason0660b5a2008-11-17 20:37:39 -0500409 ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400410 BUG_ON(ret < 0);
411 if (ret == 0) {
412 leaf = path->nodes[0];
413 ref = btrfs_item_ptr(leaf, path->slots[0],
414 struct btrfs_root_ref);
Chris Mason0660b5a2008-11-17 20:37:39 -0500415
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400416 WARN_ON(btrfs_root_ref_dirid(leaf, ref) != dirid);
417 WARN_ON(btrfs_root_ref_name_len(leaf, ref) != name_len);
418 ptr = (unsigned long)(ref + 1);
419 WARN_ON(memcmp_extent_buffer(leaf, name, ptr, name_len));
420 *sequence = btrfs_root_ref_sequence(leaf, ref);
421
422 ret = btrfs_del_item(trans, tree_root, path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +0000423 if (ret) {
424 err = ret;
425 goto out;
426 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400427 } else
428 err = -ENOENT;
429
430 if (key.type == BTRFS_ROOT_BACKREF_KEY) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200431 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400432 key.objectid = ref_id;
433 key.type = BTRFS_ROOT_REF_KEY;
434 key.offset = root_id;
435 goto again;
436 }
Chris Mason0660b5a2008-11-17 20:37:39 -0500437
Tsutomu Itoh65a246c2011-05-19 04:37:44 +0000438out:
Chris Mason0660b5a2008-11-17 20:37:39 -0500439 btrfs_free_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400440 return err;
Chris Mason0660b5a2008-11-17 20:37:39 -0500441}
442
Chris Masonea9e8b12008-11-17 21:14:24 -0500443int btrfs_find_root_ref(struct btrfs_root *tree_root,
444 struct btrfs_path *path,
445 u64 root_id, u64 ref_id)
446{
447 struct btrfs_key key;
448 int ret;
449
450 key.objectid = root_id;
451 key.type = BTRFS_ROOT_REF_KEY;
452 key.offset = ref_id;
453
454 ret = btrfs_search_slot(NULL, tree_root, &key, path, 0, 0);
455 return ret;
456}
457
Chris Mason0660b5a2008-11-17 20:37:39 -0500458/*
459 * add a btrfs_root_ref item. type is either BTRFS_ROOT_REF_KEY
460 * or BTRFS_ROOT_BACKREF_KEY.
461 *
462 * The dirid, sequence, name and name_len refer to the directory entry
463 * that is referencing the root.
464 *
465 * For a forward ref, the root_id is the id of the tree referencing
466 * the root and ref_id is the id of the subvol or snapshot.
467 *
468 * For a back ref the root_id is the id of the subvol or snapshot and
469 * ref_id is the id of the tree referencing it.
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100470 *
471 * Will return 0, -ENOMEM, or anything from the CoW path
Chris Mason0660b5a2008-11-17 20:37:39 -0500472 */
473int btrfs_add_root_ref(struct btrfs_trans_handle *trans,
474 struct btrfs_root *tree_root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400475 u64 root_id, u64 ref_id, u64 dirid, u64 sequence,
Chris Mason0660b5a2008-11-17 20:37:39 -0500476 const char *name, int name_len)
477{
478 struct btrfs_key key;
479 int ret;
480 struct btrfs_path *path;
481 struct btrfs_root_ref *ref;
482 struct extent_buffer *leaf;
483 unsigned long ptr;
484
Chris Mason0660b5a2008-11-17 20:37:39 -0500485 path = btrfs_alloc_path();
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400486 if (!path)
487 return -ENOMEM;
Chris Mason0660b5a2008-11-17 20:37:39 -0500488
489 key.objectid = root_id;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400490 key.type = BTRFS_ROOT_BACKREF_KEY;
Chris Mason0660b5a2008-11-17 20:37:39 -0500491 key.offset = ref_id;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400492again:
Chris Mason0660b5a2008-11-17 20:37:39 -0500493 ret = btrfs_insert_empty_item(trans, tree_root, path, &key,
494 sizeof(*ref) + name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100495 if (ret) {
496 btrfs_abort_transaction(trans, tree_root, ret);
497 btrfs_free_path(path);
498 return ret;
499 }
Chris Mason0660b5a2008-11-17 20:37:39 -0500500
501 leaf = path->nodes[0];
502 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
503 btrfs_set_root_ref_dirid(leaf, ref, dirid);
504 btrfs_set_root_ref_sequence(leaf, ref, sequence);
505 btrfs_set_root_ref_name_len(leaf, ref, name_len);
506 ptr = (unsigned long)(ref + 1);
507 write_extent_buffer(leaf, name, ptr, name_len);
508 btrfs_mark_buffer_dirty(leaf);
509
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400510 if (key.type == BTRFS_ROOT_BACKREF_KEY) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200511 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400512 key.objectid = ref_id;
513 key.type = BTRFS_ROOT_REF_KEY;
514 key.offset = root_id;
515 goto again;
516 }
517
Chris Mason0660b5a2008-11-17 20:37:39 -0500518 btrfs_free_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400519 return 0;
Chris Mason0660b5a2008-11-17 20:37:39 -0500520}
Li Zefan08fe4db2011-03-28 02:01:25 +0000521
522/*
523 * Old btrfs forgets to init root_item->flags and root_item->byte_limit
524 * for subvolumes. To work around this problem, we steal a bit from
525 * root_item->inode_item->flags, and use it to indicate if those fields
526 * have been properly initialized.
527 */
528void btrfs_check_and_init_root_item(struct btrfs_root_item *root_item)
529{
530 u64 inode_flags = le64_to_cpu(root_item->inode.flags);
531
532 if (!(inode_flags & BTRFS_INODE_ROOT_ITEM_INIT)) {
533 inode_flags |= BTRFS_INODE_ROOT_ITEM_INIT;
534 root_item->inode.flags = cpu_to_le64(inode_flags);
535 root_item->flags = 0;
536 root_item->byte_limit = 0;
537 }
538}
Alexander Block8ea05e32012-07-25 17:35:53 +0200539
540void btrfs_update_root_times(struct btrfs_trans_handle *trans,
541 struct btrfs_root *root)
542{
543 struct btrfs_root_item *item = &root->root_item;
544 struct timespec ct = CURRENT_TIME;
545
546 spin_lock(&root->root_times_lock);
Dan Carpenterdadd1102012-07-30 02:10:44 -0600547 item->ctransid = cpu_to_le64(trans->transid);
Alexander Block8ea05e32012-07-25 17:35:53 +0200548 item->ctime.sec = cpu_to_le64(ct.tv_sec);
Dan Carpenterdadd1102012-07-30 02:10:44 -0600549 item->ctime.nsec = cpu_to_le32(ct.tv_nsec);
Alexander Block8ea05e32012-07-25 17:35:53 +0200550 spin_unlock(&root->root_times_lock);
551}