2 * Copyright (C) 2007 Oracle. All rights reserved.
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.
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.
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.
20 #include "transaction.h"
22 #include "print-tree.h"
25 * returns 0 on finding something, 1 if no more roots are there
28 int btrfs_search_root(struct btrfs_root *root, u64 search_start,
31 struct btrfs_path *path;
32 struct btrfs_key search_key;
35 root = root->fs_info->tree_root;
36 search_key.objectid = search_start;
37 search_key.type = (u8)-1;
38 search_key.offset = (u64)-1;
40 path = btrfs_alloc_path();
43 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
50 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
51 ret = btrfs_next_leaf(root, path);
55 btrfs_item_key_to_cpu(path->nodes[0], &search_key, path->slots[0]);
56 if (search_key.type != BTRFS_ROOT_ITEM_KEY) {
58 btrfs_release_path(root, path);
62 *found_objectid = search_key.objectid;
65 btrfs_free_path(path);
69 int btrfs_find_last_root(struct btrfs_root *root, u64 objectid,
70 struct btrfs_root_item *item, struct btrfs_key *key)
72 struct btrfs_path *path;
73 struct btrfs_key search_key;
74 struct btrfs_key found_key;
75 struct extent_buffer *l;
79 search_key.objectid = objectid;
80 search_key.type = (u8)-1;
81 search_key.offset = (u64)-1;
83 path = btrfs_alloc_path();
85 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
91 BUG_ON(path->slots[0] == 0);
92 slot = path->slots[0] - 1;
93 btrfs_item_key_to_cpu(l, &found_key, slot);
94 if (found_key.objectid != objectid) {
98 read_extent_buffer(l, item, btrfs_item_ptr_offset(l, slot),
100 memcpy(key, &found_key, sizeof(found_key));
103 btrfs_free_path(path);
107 int btrfs_update_root(struct btrfs_trans_handle *trans, struct btrfs_root
108 *root, struct btrfs_key *key, struct btrfs_root_item
111 struct btrfs_path *path;
112 struct extent_buffer *l;
117 path = btrfs_alloc_path();
119 ret = btrfs_search_slot(trans, root, key, path, 0, 1);
124 btrfs_print_leaf(root, path->nodes[0]);
125 printk("unable to update root key %Lu %u %Lu\n",
126 key->objectid, key->type, key->offset);
131 slot = path->slots[0];
132 ptr = btrfs_item_ptr_offset(l, slot);
133 write_extent_buffer(l, item, ptr, sizeof(*item));
134 btrfs_mark_buffer_dirty(path->nodes[0]);
136 btrfs_release_path(root, path);
137 btrfs_free_path(path);
141 int btrfs_insert_root(struct btrfs_trans_handle *trans, struct btrfs_root
142 *root, struct btrfs_key *key, struct btrfs_root_item
146 ret = btrfs_insert_item(trans, root, key, item, sizeof(*item));
150 int btrfs_find_dead_roots(struct btrfs_root *root, u64 objectid,
151 struct btrfs_root *latest)
153 struct btrfs_root *dead_root;
154 struct btrfs_item *item;
155 struct btrfs_root_item *ri;
156 struct btrfs_key key;
157 struct btrfs_key found_key;
158 struct btrfs_path *path;
161 struct extent_buffer *leaf;
164 key.objectid = objectid;
165 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
167 path = btrfs_alloc_path();
172 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
176 leaf = path->nodes[0];
177 nritems = btrfs_header_nritems(leaf);
178 slot = path->slots[0];
179 if (slot >= nritems) {
180 ret = btrfs_next_leaf(root, path);
183 leaf = path->nodes[0];
184 nritems = btrfs_header_nritems(leaf);
185 slot = path->slots[0];
187 item = btrfs_item_nr(leaf, slot);
188 btrfs_item_key_to_cpu(leaf, &key, slot);
189 if (btrfs_key_type(&key) != BTRFS_ROOT_ITEM_KEY)
192 if (key.objectid < objectid)
195 if (key.objectid > objectid)
198 ri = btrfs_item_ptr(leaf, slot, struct btrfs_root_item);
199 if (btrfs_disk_root_refs(leaf, ri) != 0)
202 memcpy(&found_key, &key, sizeof(key));
204 btrfs_release_path(root, path);
206 btrfs_read_fs_root_no_radix(root->fs_info->tree_root,
208 if (IS_ERR(dead_root)) {
209 ret = PTR_ERR(dead_root);
213 if (objectid == BTRFS_TREE_RELOC_OBJECTID)
214 ret = btrfs_add_dead_reloc_root(dead_root);
216 ret = btrfs_add_dead_root(dead_root, latest);
226 btrfs_free_path(path);
230 int btrfs_del_root(struct btrfs_trans_handle *trans, struct btrfs_root *root,
231 struct btrfs_key *key)
233 struct btrfs_path *path;
236 struct btrfs_root_item *ri;
237 struct extent_buffer *leaf;
239 path = btrfs_alloc_path();
241 ret = btrfs_search_slot(trans, root, key, path, -1, 1);
245 btrfs_print_leaf(root, path->nodes[0]);
246 printk("failed to del %Lu %u %Lu\n", key->objectid, key->type, key->offset);
250 leaf = path->nodes[0];
251 ri = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_item);
253 refs = btrfs_disk_root_refs(leaf, ri);
255 ret = btrfs_del_item(trans, root, path);
257 btrfs_release_path(root, path);
258 btrfs_free_path(path);