blob: 5ef72599a581d0b2894a16373ab93b2ca57cd490 [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();
91 BUG_ON(!path);
Chris Mason5caf2a02007-04-02 11:20:42 -040092 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
Chris Mason3768f362007-03-13 16:47:54 -040093 if (ret < 0)
94 goto out;
Chris Mason5f39d392007-10-15 16:14:19 -040095
Chris Mason3768f362007-03-13 16:47:54 -040096 BUG_ON(ret == 0);
Chris Mason5f39d392007-10-15 16:14:19 -040097 l = path->nodes[0];
Chris Mason5caf2a02007-04-02 11:20:42 -040098 BUG_ON(path->slots[0] == 0);
99 slot = path->slots[0] - 1;
Chris Mason5f39d392007-10-15 16:14:19 -0400100 btrfs_item_key_to_cpu(l, &found_key, slot);
101 if (found_key.objectid != objectid) {
Chris Mason3768f362007-03-13 16:47:54 -0400102 ret = 1;
103 goto out;
104 }
Chris Mason5f39d392007-10-15 16:14:19 -0400105 read_extent_buffer(l, item, btrfs_item_ptr_offset(l, slot),
106 sizeof(*item));
107 memcpy(key, &found_key, sizeof(found_key));
Chris Mason3768f362007-03-13 16:47:54 -0400108 ret = 0;
109out:
Chris Mason5caf2a02007-04-02 11:20:42 -0400110 btrfs_free_path(path);
Chris Mason3768f362007-03-13 16:47:54 -0400111 return ret;
112}
113
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400114int btrfs_set_root_node(struct btrfs_root_item *item,
115 struct extent_buffer *node)
116{
117 btrfs_set_root_bytenr(item, node->start);
118 btrfs_set_root_level(item, btrfs_header_level(node));
119 btrfs_set_root_generation(item, btrfs_header_generation(node));
120 return 0;
121}
122
Chris Masond352ac62008-09-29 15:18:18 -0400123/*
124 * copy the data in 'item' into the btree
125 */
Chris Masone089f052007-03-16 16:20:31 -0400126int btrfs_update_root(struct btrfs_trans_handle *trans, struct btrfs_root
127 *root, struct btrfs_key *key, struct btrfs_root_item
128 *item)
Chris Mason3768f362007-03-13 16:47:54 -0400129{
Chris Mason5caf2a02007-04-02 11:20:42 -0400130 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400131 struct extent_buffer *l;
Chris Mason3768f362007-03-13 16:47:54 -0400132 int ret;
133 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -0400134 unsigned long ptr;
Chris Mason3768f362007-03-13 16:47:54 -0400135
Chris Mason5caf2a02007-04-02 11:20:42 -0400136 path = btrfs_alloc_path();
137 BUG_ON(!path);
Chris Mason5caf2a02007-04-02 11:20:42 -0400138 ret = btrfs_search_slot(trans, root, key, path, 0, 1);
Chris Mason3768f362007-03-13 16:47:54 -0400139 if (ret < 0)
140 goto out;
Chris Masond6667462008-01-03 14:51:00 -0500141
142 if (ret != 0) {
143 btrfs_print_leaf(root, path->nodes[0]);
Chris Masond3977122009-01-05 21:25:51 -0500144 printk(KERN_CRIT "unable to update root key %llu %u %llu\n",
145 (unsigned long long)key->objectid, key->type,
146 (unsigned long long)key->offset);
Chris Masond6667462008-01-03 14:51:00 -0500147 BUG_ON(1);
148 }
149
Chris Mason5f39d392007-10-15 16:14:19 -0400150 l = path->nodes[0];
Chris Mason5caf2a02007-04-02 11:20:42 -0400151 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -0400152 ptr = btrfs_item_ptr_offset(l, slot);
153 write_extent_buffer(l, item, ptr, sizeof(*item));
Chris Mason5caf2a02007-04-02 11:20:42 -0400154 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason3768f362007-03-13 16:47:54 -0400155out:
Chris Mason5caf2a02007-04-02 11:20:42 -0400156 btrfs_release_path(root, path);
157 btrfs_free_path(path);
Chris Mason3768f362007-03-13 16:47:54 -0400158 return ret;
159}
160
Chris Masone089f052007-03-16 16:20:31 -0400161int btrfs_insert_root(struct btrfs_trans_handle *trans, struct btrfs_root
162 *root, struct btrfs_key *key, struct btrfs_root_item
163 *item)
Chris Mason3768f362007-03-13 16:47:54 -0400164{
165 int ret;
Chris Masone089f052007-03-16 16:20:31 -0400166 ret = btrfs_insert_item(trans, root, key, item, sizeof(*item));
Chris Mason3768f362007-03-13 16:47:54 -0400167 return ret;
168}
169
Chris Masond352ac62008-09-29 15:18:18 -0400170/*
171 * at mount time we want to find all the old transaction snapshots that were in
Chris Masond3977122009-01-05 21:25:51 -0500172 * the process of being deleted if we crashed. This is any root item with an
173 * offset lower than the latest root. They need to be queued for deletion to
174 * finish what was happening when we crashed.
Chris Masond352ac62008-09-29 15:18:18 -0400175 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400176int btrfs_find_dead_roots(struct btrfs_root *root, u64 objectid)
Chris Mason5eda7b52007-06-22 14:16:25 -0400177{
178 struct btrfs_root *dead_root;
179 struct btrfs_item *item;
180 struct btrfs_root_item *ri;
181 struct btrfs_key key;
Chris Masona7a16fd2008-06-26 10:34:20 -0400182 struct btrfs_key found_key;
Chris Mason5eda7b52007-06-22 14:16:25 -0400183 struct btrfs_path *path;
184 int ret;
185 u32 nritems;
Chris Mason5f39d392007-10-15 16:14:19 -0400186 struct extent_buffer *leaf;
Chris Mason5eda7b52007-06-22 14:16:25 -0400187 int slot;
188
Chris Mason5ce14bb2007-09-11 11:15:39 -0400189 key.objectid = objectid;
Chris Mason5eda7b52007-06-22 14:16:25 -0400190 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
191 key.offset = 0;
192 path = btrfs_alloc_path();
193 if (!path)
194 return -ENOMEM;
Chris Masona7a16fd2008-06-26 10:34:20 -0400195
196again:
Chris Mason5eda7b52007-06-22 14:16:25 -0400197 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
198 if (ret < 0)
199 goto err;
Chris Masond3977122009-01-05 21:25:51 -0500200 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400201 leaf = path->nodes[0];
202 nritems = btrfs_header_nritems(leaf);
Chris Mason5eda7b52007-06-22 14:16:25 -0400203 slot = path->slots[0];
204 if (slot >= nritems) {
205 ret = btrfs_next_leaf(root, path);
206 if (ret)
207 break;
Chris Mason5f39d392007-10-15 16:14:19 -0400208 leaf = path->nodes[0];
209 nritems = btrfs_header_nritems(leaf);
Chris Mason5eda7b52007-06-22 14:16:25 -0400210 slot = path->slots[0];
211 }
Chris Mason5f39d392007-10-15 16:14:19 -0400212 item = btrfs_item_nr(leaf, slot);
213 btrfs_item_key_to_cpu(leaf, &key, slot);
Chris Mason5eda7b52007-06-22 14:16:25 -0400214 if (btrfs_key_type(&key) != BTRFS_ROOT_ITEM_KEY)
215 goto next;
Chris Mason5ce14bb2007-09-11 11:15:39 -0400216
217 if (key.objectid < objectid)
218 goto next;
219
220 if (key.objectid > objectid)
221 break;
222
Chris Mason5eda7b52007-06-22 14:16:25 -0400223 ri = btrfs_item_ptr(leaf, slot, struct btrfs_root_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400224 if (btrfs_disk_root_refs(leaf, ri) != 0)
Chris Mason5eda7b52007-06-22 14:16:25 -0400225 goto next;
Chris Mason5ce14bb2007-09-11 11:15:39 -0400226
Chris Masona7a16fd2008-06-26 10:34:20 -0400227 memcpy(&found_key, &key, sizeof(key));
228 key.offset++;
229 btrfs_release_path(root, path);
Chris Masone02119d2008-09-05 16:13:11 -0400230 dead_root =
231 btrfs_read_fs_root_no_radix(root->fs_info->tree_root,
232 &found_key);
Aneesha1f39632007-07-11 10:03:27 -0400233 if (IS_ERR(dead_root)) {
234 ret = PTR_ERR(dead_root);
Chris Mason5eda7b52007-06-22 14:16:25 -0400235 goto err;
236 }
Chris Mason5ce14bb2007-09-11 11:15:39 -0400237
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400238 ret = btrfs_add_dead_root(dead_root);
Chris Mason5eda7b52007-06-22 14:16:25 -0400239 if (ret)
240 goto err;
Chris Masona7a16fd2008-06-26 10:34:20 -0400241 goto again;
Chris Mason5eda7b52007-06-22 14:16:25 -0400242next:
243 slot++;
244 path->slots[0]++;
245 }
246 ret = 0;
247err:
248 btrfs_free_path(path);
249 return ret;
250}
251
Chris Masond352ac62008-09-29 15:18:18 -0400252/* drop the root item for 'key' from 'root' */
Chris Masone089f052007-03-16 16:20:31 -0400253int btrfs_del_root(struct btrfs_trans_handle *trans, struct btrfs_root *root,
254 struct btrfs_key *key)
Chris Mason3768f362007-03-13 16:47:54 -0400255{
Chris Mason5caf2a02007-04-02 11:20:42 -0400256 struct btrfs_path *path;
Chris Mason3768f362007-03-13 16:47:54 -0400257 int ret;
Chris Masonc5739bb2007-04-10 09:27:04 -0400258 u32 refs;
259 struct btrfs_root_item *ri;
Chris Mason5f39d392007-10-15 16:14:19 -0400260 struct extent_buffer *leaf;
Chris Mason3768f362007-03-13 16:47:54 -0400261
Chris Mason5caf2a02007-04-02 11:20:42 -0400262 path = btrfs_alloc_path();
263 BUG_ON(!path);
Chris Mason5caf2a02007-04-02 11:20:42 -0400264 ret = btrfs_search_slot(trans, root, key, path, -1, 1);
Chris Mason3768f362007-03-13 16:47:54 -0400265 if (ret < 0)
266 goto out;
Chris Masonedbd8d42007-12-21 16:27:24 -0500267
Chris Mason3768f362007-03-13 16:47:54 -0400268 BUG_ON(ret != 0);
Chris Mason5f39d392007-10-15 16:14:19 -0400269 leaf = path->nodes[0];
270 ri = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_item);
Chris Masonc5739bb2007-04-10 09:27:04 -0400271
Chris Mason5f39d392007-10-15 16:14:19 -0400272 refs = btrfs_disk_root_refs(leaf, ri);
Chris Mason5eda7b52007-06-22 14:16:25 -0400273 BUG_ON(refs != 0);
274 ret = btrfs_del_item(trans, root, path);
Chris Mason3768f362007-03-13 16:47:54 -0400275out:
Chris Mason5caf2a02007-04-02 11:20:42 -0400276 btrfs_release_path(root, path);
277 btrfs_free_path(path);
Chris Mason3768f362007-03-13 16:47:54 -0400278 return ret;
279}
Chris Mason0660b5a2008-11-17 20:37:39 -0500280
281int btrfs_del_root_ref(struct btrfs_trans_handle *trans,
282 struct btrfs_root *tree_root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400283 u64 root_id, u64 ref_id, u64 dirid, u64 *sequence,
284 const char *name, int name_len)
285
Chris Mason0660b5a2008-11-17 20:37:39 -0500286{
Chris Mason0660b5a2008-11-17 20:37:39 -0500287 struct btrfs_path *path;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400288 struct btrfs_root_ref *ref;
289 struct extent_buffer *leaf;
290 struct btrfs_key key;
291 unsigned long ptr;
292 int err = 0;
293 int ret;
Chris Mason0660b5a2008-11-17 20:37:39 -0500294
295 path = btrfs_alloc_path();
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400296 if (!path)
297 return -ENOMEM;
Chris Mason0660b5a2008-11-17 20:37:39 -0500298
299 key.objectid = root_id;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400300 key.type = BTRFS_ROOT_BACKREF_KEY;
Chris Mason0660b5a2008-11-17 20:37:39 -0500301 key.offset = ref_id;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400302again:
Chris Mason0660b5a2008-11-17 20:37:39 -0500303 ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400304 BUG_ON(ret < 0);
305 if (ret == 0) {
306 leaf = path->nodes[0];
307 ref = btrfs_item_ptr(leaf, path->slots[0],
308 struct btrfs_root_ref);
Chris Mason0660b5a2008-11-17 20:37:39 -0500309
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400310 WARN_ON(btrfs_root_ref_dirid(leaf, ref) != dirid);
311 WARN_ON(btrfs_root_ref_name_len(leaf, ref) != name_len);
312 ptr = (unsigned long)(ref + 1);
313 WARN_ON(memcmp_extent_buffer(leaf, name, ptr, name_len));
314 *sequence = btrfs_root_ref_sequence(leaf, ref);
315
316 ret = btrfs_del_item(trans, tree_root, path);
317 BUG_ON(ret);
318 } else
319 err = -ENOENT;
320
321 if (key.type == BTRFS_ROOT_BACKREF_KEY) {
322 btrfs_release_path(tree_root, path);
323 key.objectid = ref_id;
324 key.type = BTRFS_ROOT_REF_KEY;
325 key.offset = root_id;
326 goto again;
327 }
Chris Mason0660b5a2008-11-17 20:37:39 -0500328
329 btrfs_free_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400330 return err;
Chris Mason0660b5a2008-11-17 20:37:39 -0500331}
332
Chris Masonea9e8b12008-11-17 21:14:24 -0500333int btrfs_find_root_ref(struct btrfs_root *tree_root,
334 struct btrfs_path *path,
335 u64 root_id, u64 ref_id)
336{
337 struct btrfs_key key;
338 int ret;
339
340 key.objectid = root_id;
341 key.type = BTRFS_ROOT_REF_KEY;
342 key.offset = ref_id;
343
344 ret = btrfs_search_slot(NULL, tree_root, &key, path, 0, 0);
345 return ret;
346}
347
Chris Mason0660b5a2008-11-17 20:37:39 -0500348/*
349 * add a btrfs_root_ref item. type is either BTRFS_ROOT_REF_KEY
350 * or BTRFS_ROOT_BACKREF_KEY.
351 *
352 * The dirid, sequence, name and name_len refer to the directory entry
353 * that is referencing the root.
354 *
355 * For a forward ref, the root_id is the id of the tree referencing
356 * the root and ref_id is the id of the subvol or snapshot.
357 *
358 * For a back ref the root_id is the id of the subvol or snapshot and
359 * ref_id is the id of the tree referencing it.
360 */
361int btrfs_add_root_ref(struct btrfs_trans_handle *trans,
362 struct btrfs_root *tree_root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400363 u64 root_id, u64 ref_id, u64 dirid, u64 sequence,
Chris Mason0660b5a2008-11-17 20:37:39 -0500364 const char *name, int name_len)
365{
366 struct btrfs_key key;
367 int ret;
368 struct btrfs_path *path;
369 struct btrfs_root_ref *ref;
370 struct extent_buffer *leaf;
371 unsigned long ptr;
372
Chris Mason0660b5a2008-11-17 20:37:39 -0500373 path = btrfs_alloc_path();
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400374 if (!path)
375 return -ENOMEM;
Chris Mason0660b5a2008-11-17 20:37:39 -0500376
377 key.objectid = root_id;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400378 key.type = BTRFS_ROOT_BACKREF_KEY;
Chris Mason0660b5a2008-11-17 20:37:39 -0500379 key.offset = ref_id;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400380again:
Chris Mason0660b5a2008-11-17 20:37:39 -0500381 ret = btrfs_insert_empty_item(trans, tree_root, path, &key,
382 sizeof(*ref) + name_len);
383 BUG_ON(ret);
384
385 leaf = path->nodes[0];
386 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
387 btrfs_set_root_ref_dirid(leaf, ref, dirid);
388 btrfs_set_root_ref_sequence(leaf, ref, sequence);
389 btrfs_set_root_ref_name_len(leaf, ref, name_len);
390 ptr = (unsigned long)(ref + 1);
391 write_extent_buffer(leaf, name, ptr, name_len);
392 btrfs_mark_buffer_dirty(leaf);
393
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400394 if (key.type == BTRFS_ROOT_BACKREF_KEY) {
395 btrfs_release_path(tree_root, path);
396 key.objectid = ref_id;
397 key.type = BTRFS_ROOT_REF_KEY;
398 key.offset = root_id;
399 goto again;
400 }
401
Chris Mason0660b5a2008-11-17 20:37:39 -0500402 btrfs_free_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400403 return 0;
Chris Mason0660b5a2008-11-17 20:37:39 -0500404}