]> bbs.cooldavid.org Git - net-next-2.6.git/blame - fs/btrfs/root-tree.c
Btrfs: Fix free block discard calls down to the block layer
[net-next-2.6.git] / fs / btrfs / root-tree.c
CommitLineData
6cbd5570
CM
1/*
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
3768f368 19#include "ctree.h"
5eda7b5e 20#include "transaction.h"
3768f368
CM
21#include "disk-io.h"
22#include "print-tree.h"
23
bf4ef679 24/*
d352ac68
CM
25 * 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.
bf4ef679
CM
29 */
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
d352ac68
CM
71/*
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 */
3768f368
CM
76int btrfs_find_last_root(struct btrfs_root *root, u64 objectid,
77 struct btrfs_root_item *item, struct btrfs_key *key)
78{
5caf2a00 79 struct btrfs_path *path;
3768f368 80 struct btrfs_key search_key;
5f39d397
CM
81 struct btrfs_key found_key;
82 struct extent_buffer *l;
3768f368
CM
83 int ret;
84 int slot;
85
86 search_key.objectid = objectid;
0660b5af 87 search_key.type = BTRFS_ROOT_ITEM_KEY;
5eda7b5e 88 search_key.offset = (u64)-1;
3768f368 89
5caf2a00
CM
90 path = btrfs_alloc_path();
91 BUG_ON(!path);
5caf2a00 92 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
3768f368
CM
93 if (ret < 0)
94 goto out;
5f39d397 95
3768f368 96 BUG_ON(ret == 0);
5f39d397 97 l = path->nodes[0];
5caf2a00
CM
98 BUG_ON(path->slots[0] == 0);
99 slot = path->slots[0] - 1;
5f39d397
CM
100 btrfs_item_key_to_cpu(l, &found_key, slot);
101 if (found_key.objectid != objectid) {
3768f368
CM
102 ret = 1;
103 goto out;
104 }
5f39d397
CM
105 read_extent_buffer(l, item, btrfs_item_ptr_offset(l, slot),
106 sizeof(*item));
107 memcpy(key, &found_key, sizeof(found_key));
3768f368
CM
108 ret = 0;
109out:
5caf2a00 110 btrfs_free_path(path);
3768f368
CM
111 return ret;
112}
113
d352ac68
CM
114/*
115 * copy the data in 'item' into the btree
116 */
e089f05c
CM
117int btrfs_update_root(struct btrfs_trans_handle *trans, struct btrfs_root
118 *root, struct btrfs_key *key, struct btrfs_root_item
119 *item)
3768f368 120{
5caf2a00 121 struct btrfs_path *path;
5f39d397 122 struct extent_buffer *l;
3768f368
CM
123 int ret;
124 int slot;
5f39d397 125 unsigned long ptr;
3768f368 126
5caf2a00
CM
127 path = btrfs_alloc_path();
128 BUG_ON(!path);
5caf2a00 129 ret = btrfs_search_slot(trans, root, key, path, 0, 1);
3768f368
CM
130 if (ret < 0)
131 goto out;
d6667462
CM
132
133 if (ret != 0) {
134 btrfs_print_leaf(root, path->nodes[0]);
135 printk("unable to update root key %Lu %u %Lu\n",
136 key->objectid, key->type, key->offset);
137 BUG_ON(1);
138 }
139
5f39d397 140 l = path->nodes[0];
5caf2a00 141 slot = path->slots[0];
5f39d397
CM
142 ptr = btrfs_item_ptr_offset(l, slot);
143 write_extent_buffer(l, item, ptr, sizeof(*item));
5caf2a00 144 btrfs_mark_buffer_dirty(path->nodes[0]);
3768f368 145out:
5caf2a00
CM
146 btrfs_release_path(root, path);
147 btrfs_free_path(path);
3768f368
CM
148 return ret;
149}
150
e089f05c
CM
151int btrfs_insert_root(struct btrfs_trans_handle *trans, struct btrfs_root
152 *root, struct btrfs_key *key, struct btrfs_root_item
153 *item)
3768f368
CM
154{
155 int ret;
e089f05c 156 ret = btrfs_insert_item(trans, root, key, item, sizeof(*item));
3768f368
CM
157 return ret;
158}
159
d352ac68
CM
160/*
161 * at mount time we want to find all the old transaction snapshots that were in
162 * the process of being deleted if we crashed. This is any root item with an offset
163 * lower than the latest root. They need to be queued for deletion to finish
164 * what was happening when we crashed.
165 */
5ce14bbc
CM
166int btrfs_find_dead_roots(struct btrfs_root *root, u64 objectid,
167 struct btrfs_root *latest)
5eda7b5e
CM
168{
169 struct btrfs_root *dead_root;
170 struct btrfs_item *item;
171 struct btrfs_root_item *ri;
172 struct btrfs_key key;
a7a16fd7 173 struct btrfs_key found_key;
5eda7b5e
CM
174 struct btrfs_path *path;
175 int ret;
176 u32 nritems;
5f39d397 177 struct extent_buffer *leaf;
5eda7b5e
CM
178 int slot;
179
5ce14bbc 180 key.objectid = objectid;
5eda7b5e
CM
181 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
182 key.offset = 0;
183 path = btrfs_alloc_path();
184 if (!path)
185 return -ENOMEM;
a7a16fd7
CM
186
187again:
5eda7b5e
CM
188 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
189 if (ret < 0)
190 goto err;
191 while(1) {
5f39d397
CM
192 leaf = path->nodes[0];
193 nritems = btrfs_header_nritems(leaf);
5eda7b5e
CM
194 slot = path->slots[0];
195 if (slot >= nritems) {
196 ret = btrfs_next_leaf(root, path);
197 if (ret)
198 break;
5f39d397
CM
199 leaf = path->nodes[0];
200 nritems = btrfs_header_nritems(leaf);
5eda7b5e
CM
201 slot = path->slots[0];
202 }
5f39d397
CM
203 item = btrfs_item_nr(leaf, slot);
204 btrfs_item_key_to_cpu(leaf, &key, slot);
5eda7b5e
CM
205 if (btrfs_key_type(&key) != BTRFS_ROOT_ITEM_KEY)
206 goto next;
5ce14bbc
CM
207
208 if (key.objectid < objectid)
209 goto next;
210
211 if (key.objectid > objectid)
212 break;
213
5eda7b5e 214 ri = btrfs_item_ptr(leaf, slot, struct btrfs_root_item);
5f39d397 215 if (btrfs_disk_root_refs(leaf, ri) != 0)
5eda7b5e 216 goto next;
5ce14bbc 217
a7a16fd7
CM
218 memcpy(&found_key, &key, sizeof(key));
219 key.offset++;
220 btrfs_release_path(root, path);
e02119d5
CM
221 dead_root =
222 btrfs_read_fs_root_no_radix(root->fs_info->tree_root,
223 &found_key);
a1f39630
A
224 if (IS_ERR(dead_root)) {
225 ret = PTR_ERR(dead_root);
5eda7b5e
CM
226 goto err;
227 }
5ce14bbc 228
1a40e23b
ZY
229 if (objectid == BTRFS_TREE_RELOC_OBJECTID)
230 ret = btrfs_add_dead_reloc_root(dead_root);
231 else
232 ret = btrfs_add_dead_root(dead_root, latest);
5eda7b5e
CM
233 if (ret)
234 goto err;
a7a16fd7 235 goto again;
5eda7b5e
CM
236next:
237 slot++;
238 path->slots[0]++;
239 }
240 ret = 0;
241err:
242 btrfs_free_path(path);
243 return ret;
244}
245
d352ac68 246/* drop the root item for 'key' from 'root' */
e089f05c
CM
247int btrfs_del_root(struct btrfs_trans_handle *trans, struct btrfs_root *root,
248 struct btrfs_key *key)
3768f368 249{
5caf2a00 250 struct btrfs_path *path;
3768f368 251 int ret;
c5739bba
CM
252 u32 refs;
253 struct btrfs_root_item *ri;
5f39d397 254 struct extent_buffer *leaf;
3768f368 255
5caf2a00
CM
256 path = btrfs_alloc_path();
257 BUG_ON(!path);
5caf2a00 258 ret = btrfs_search_slot(trans, root, key, path, -1, 1);
3768f368
CM
259 if (ret < 0)
260 goto out;
edbd8d4e
CM
261 if (ret) {
262btrfs_print_leaf(root, path->nodes[0]);
263printk("failed to del %Lu %u %Lu\n", key->objectid, key->type, key->offset);
264
265 }
3768f368 266 BUG_ON(ret != 0);
5f39d397
CM
267 leaf = path->nodes[0];
268 ri = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_item);
c5739bba 269
5f39d397 270 refs = btrfs_disk_root_refs(leaf, ri);
5eda7b5e
CM
271 BUG_ON(refs != 0);
272 ret = btrfs_del_item(trans, root, path);
3768f368 273out:
5caf2a00
CM
274 btrfs_release_path(root, path);
275 btrfs_free_path(path);
3768f368
CM
276 return ret;
277}
0660b5af 278
b2950863 279#if 0 /* this will get used when snapshot deletion is implemented */
0660b5af
CM
280int btrfs_del_root_ref(struct btrfs_trans_handle *trans,
281 struct btrfs_root *tree_root,
282 u64 root_id, u8 type, u64 ref_id)
283{
284 struct btrfs_key key;
285 int ret;
286 struct btrfs_path *path;
287
288 path = btrfs_alloc_path();
289
290 key.objectid = root_id;
291 key.type = type;
292 key.offset = ref_id;
293
294 ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1);
295 BUG_ON(ret);
296
297 ret = btrfs_del_item(trans, tree_root, path);
298 BUG_ON(ret);
299
300 btrfs_free_path(path);
301 return ret;
302}
b2950863 303#endif
0660b5af 304
ea9e8b11
CM
305int btrfs_find_root_ref(struct btrfs_root *tree_root,
306 struct btrfs_path *path,
307 u64 root_id, u64 ref_id)
308{
309 struct btrfs_key key;
310 int ret;
311
312 key.objectid = root_id;
313 key.type = BTRFS_ROOT_REF_KEY;
314 key.offset = ref_id;
315
316 ret = btrfs_search_slot(NULL, tree_root, &key, path, 0, 0);
317 return ret;
318}
319
320
0660b5af
CM
321/*
322 * add a btrfs_root_ref item. type is either BTRFS_ROOT_REF_KEY
323 * or BTRFS_ROOT_BACKREF_KEY.
324 *
325 * The dirid, sequence, name and name_len refer to the directory entry
326 * that is referencing the root.
327 *
328 * For a forward ref, the root_id is the id of the tree referencing
329 * the root and ref_id is the id of the subvol or snapshot.
330 *
331 * For a back ref the root_id is the id of the subvol or snapshot and
332 * ref_id is the id of the tree referencing it.
333 */
334int btrfs_add_root_ref(struct btrfs_trans_handle *trans,
335 struct btrfs_root *tree_root,
336 u64 root_id, u8 type, u64 ref_id,
337 u64 dirid, u64 sequence,
338 const char *name, int name_len)
339{
340 struct btrfs_key key;
341 int ret;
342 struct btrfs_path *path;
343 struct btrfs_root_ref *ref;
344 struct extent_buffer *leaf;
345 unsigned long ptr;
346
347
348 path = btrfs_alloc_path();
349
350 key.objectid = root_id;
351 key.type = type;
352 key.offset = ref_id;
353
354 ret = btrfs_insert_empty_item(trans, tree_root, path, &key,
355 sizeof(*ref) + name_len);
356 BUG_ON(ret);
357
358 leaf = path->nodes[0];
359 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
360 btrfs_set_root_ref_dirid(leaf, ref, dirid);
361 btrfs_set_root_ref_sequence(leaf, ref, sequence);
362 btrfs_set_root_ref_name_len(leaf, ref, name_len);
363 ptr = (unsigned long)(ref + 1);
364 write_extent_buffer(leaf, name, ptr, name_len);
365 btrfs_mark_buffer_dirty(leaf);
366
367 btrfs_free_path(path);
368 return ret;
369}