Remove kernel-space header in btrfs-progs
[btrfs-progs-unstable.git] / root-tree.c
blobb6392149506eefe5f4e0252e6ffe8f4ab08e486e
1 /*
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.
19 #include "ctree.h"
20 #include "transaction.h"
21 #include "disk-io.h"
22 #include "print-tree.h"
24 int btrfs_find_last_root(struct btrfs_root *root, u64 objectid,
25 struct btrfs_root_item *item, struct btrfs_key *key)
27 struct btrfs_path *path;
28 struct btrfs_key search_key;
29 struct btrfs_key found_key;
30 struct extent_buffer *l;
31 int ret;
32 int slot;
34 search_key.objectid = objectid;
35 search_key.type = (u8)-1;
36 search_key.offset = (u64)-1;
38 path = btrfs_alloc_path();
39 BUG_ON(!path);
40 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
41 if (ret < 0)
42 goto out;
44 BUG_ON(ret == 0);
45 l = path->nodes[0];
46 BUG_ON(path->slots[0] == 0);
47 slot = path->slots[0] - 1;
48 btrfs_item_key_to_cpu(l, &found_key, slot);
49 if (found_key.objectid != objectid) {
50 ret = 1;
51 goto out;
53 read_extent_buffer(l, item, btrfs_item_ptr_offset(l, slot),
54 sizeof(*item));
55 memcpy(key, &found_key, sizeof(found_key));
56 ret = 0;
57 out:
58 btrfs_release_path(root, path);
59 btrfs_free_path(path);
60 return ret;
63 int btrfs_update_root(struct btrfs_trans_handle *trans, struct btrfs_root
64 *root, struct btrfs_key *key, struct btrfs_root_item
65 *item)
67 struct btrfs_path *path;
68 struct extent_buffer *l;
69 int ret;
70 int slot;
71 unsigned long ptr;
73 path = btrfs_alloc_path();
74 BUG_ON(!path);
75 ret = btrfs_search_slot(trans, root, key, path, 0, 1);
76 if (ret < 0)
77 goto out;
78 BUG_ON(ret != 0);
79 l = path->nodes[0];
80 slot = path->slots[0];
81 ptr = btrfs_item_ptr_offset(l, slot);
82 write_extent_buffer(l, item, ptr, sizeof(*item));
83 btrfs_mark_buffer_dirty(path->nodes[0]);
84 out:
85 btrfs_release_path(root, path);
86 btrfs_free_path(path);
87 return ret;
90 int btrfs_insert_root(struct btrfs_trans_handle *trans, struct btrfs_root
91 *root, struct btrfs_key *key, struct btrfs_root_item
92 *item)
94 int ret;
95 ret = btrfs_insert_item(trans, root, key, item, sizeof(*item));
96 return ret;
99 #if 0
100 int btrfs_find_dead_roots(struct btrfs_root *root, u64 objectid,
101 struct btrfs_root *latest)
103 struct btrfs_root *dead_root;
104 struct btrfs_item *item;
105 struct btrfs_root_item *ri;
106 struct btrfs_key key;
107 struct btrfs_path *path;
108 int ret;
109 u32 nritems;
110 struct extent_buffer *leaf;
111 int slot;
113 key.objectid = objectid;
114 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
115 key.offset = 0;
116 path = btrfs_alloc_path();
117 if (!path)
118 return -ENOMEM;
119 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
120 if (ret < 0)
121 goto err;
122 while(1) {
123 leaf = path->nodes[0];
124 nritems = btrfs_header_nritems(leaf);
125 slot = path->slots[0];
126 if (slot >= nritems) {
127 ret = btrfs_next_leaf(root, path);
128 if (ret)
129 break;
130 leaf = path->nodes[0];
131 nritems = btrfs_header_nritems(leaf);
132 slot = path->slots[0];
134 item = btrfs_item_nr(leaf, slot);
135 btrfs_item_key_to_cpu(leaf, &key, slot);
136 if (btrfs_key_type(&key) != BTRFS_ROOT_ITEM_KEY)
137 goto next;
139 if (key.objectid < objectid)
140 goto next;
142 if (key.objectid > objectid)
143 break;
145 ri = btrfs_item_ptr(leaf, slot, struct btrfs_root_item);
146 if (btrfs_disk_root_refs(leaf, ri) != 0)
147 goto next;
149 dead_root = btrfs_read_fs_root_no_radix(root->fs_info, &key);
150 if (IS_ERR(dead_root)) {
151 ret = PTR_ERR(dead_root);
152 goto err;
155 ret = btrfs_add_dead_root(dead_root, latest,
156 &root->fs_info->dead_roots);
157 if (ret)
158 goto err;
159 next:
160 slot++;
161 path->slots[0]++;
163 ret = 0;
164 err:
165 btrfs_free_path(path);
166 return ret;
168 #endif
170 int btrfs_del_root(struct btrfs_trans_handle *trans, struct btrfs_root *root,
171 struct btrfs_key *key)
173 struct btrfs_path *path;
174 int ret;
175 u32 refs;
176 struct btrfs_root_item *ri;
177 struct extent_buffer *leaf;
179 path = btrfs_alloc_path();
180 BUG_ON(!path);
181 ret = btrfs_search_slot(trans, root, key, path, -1, 1);
182 if (ret < 0)
183 goto out;
184 if (ret) {
185 btrfs_print_leaf(root, path->nodes[0]);
186 printk("failed to del %Lu %u %Lu\n", key->objectid, key->type, key->offset);
189 BUG_ON(ret != 0);
190 leaf = path->nodes[0];
191 ri = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_item);
193 refs = btrfs_disk_root_refs(leaf, ri);
194 BUG_ON(refs != 0);
195 ret = btrfs_del_item(trans, root, path);
196 out:
197 btrfs_release_path(root, path);
198 btrfs_free_path(path);
199 return ret;