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 * 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
30 int btrfs_search_root(struct btrfs_root
*root
, u64 search_start
,
33 struct btrfs_path
*path
;
34 struct btrfs_key search_key
;
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;
42 path
= btrfs_alloc_path();
45 ret
= btrfs_search_slot(NULL
, root
, &search_key
, path
, 0, 0);
52 if (path
->slots
[0] >= btrfs_header_nritems(path
->nodes
[0])) {
53 ret
= btrfs_next_leaf(root
, path
);
57 btrfs_item_key_to_cpu(path
->nodes
[0], &search_key
, path
->slots
[0]);
58 if (search_key
.type
!= BTRFS_ROOT_ITEM_KEY
) {
60 btrfs_release_path(root
, path
);
64 *found_objectid
= search_key
.objectid
;
67 btrfs_free_path(path
);
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
76 int btrfs_find_last_root(struct btrfs_root
*root
, u64 objectid
,
77 struct btrfs_root_item
*item
, struct btrfs_key
*key
)
79 struct btrfs_path
*path
;
80 struct btrfs_key search_key
;
81 struct btrfs_key found_key
;
82 struct extent_buffer
*l
;
86 search_key
.objectid
= objectid
;
87 search_key
.type
= BTRFS_ROOT_ITEM_KEY
;
88 search_key
.offset
= (u64
)-1;
90 path
= btrfs_alloc_path();
92 ret
= btrfs_search_slot(NULL
, root
, &search_key
, path
, 0, 0);
97 if (path
->slots
[0] == 0) {
102 slot
= path
->slots
[0] - 1;
103 btrfs_item_key_to_cpu(l
, &found_key
, slot
);
104 if (found_key
.objectid
!= objectid
||
105 found_key
.type
!= BTRFS_ROOT_ITEM_KEY
) {
110 read_extent_buffer(l
, item
, btrfs_item_ptr_offset(l
, slot
),
113 memcpy(key
, &found_key
, sizeof(found_key
));
116 btrfs_free_path(path
);
120 int btrfs_set_root_node(struct btrfs_root_item
*item
,
121 struct extent_buffer
*node
)
123 btrfs_set_root_bytenr(item
, node
->start
);
124 btrfs_set_root_level(item
, btrfs_header_level(node
));
125 btrfs_set_root_generation(item
, btrfs_header_generation(node
));
130 * copy the data in 'item' into the btree
132 int btrfs_update_root(struct btrfs_trans_handle
*trans
, struct btrfs_root
133 *root
, struct btrfs_key
*key
, struct btrfs_root_item
136 struct btrfs_path
*path
;
137 struct extent_buffer
*l
;
142 path
= btrfs_alloc_path();
144 ret
= btrfs_search_slot(trans
, root
, key
, path
, 0, 1);
149 btrfs_print_leaf(root
, path
->nodes
[0]);
150 printk(KERN_CRIT
"unable to update root key %llu %u %llu\n",
151 (unsigned long long)key
->objectid
, key
->type
,
152 (unsigned long long)key
->offset
);
157 slot
= path
->slots
[0];
158 ptr
= btrfs_item_ptr_offset(l
, slot
);
159 write_extent_buffer(l
, item
, ptr
, sizeof(*item
));
160 btrfs_mark_buffer_dirty(path
->nodes
[0]);
162 btrfs_release_path(root
, path
);
163 btrfs_free_path(path
);
167 int btrfs_insert_root(struct btrfs_trans_handle
*trans
, struct btrfs_root
168 *root
, struct btrfs_key
*key
, struct btrfs_root_item
172 ret
= btrfs_insert_item(trans
, root
, key
, item
, sizeof(*item
));
177 * at mount time we want to find all the old transaction snapshots that were in
178 * the process of being deleted if we crashed. This is any root item with an
179 * offset lower than the latest root. They need to be queued for deletion to
180 * finish what was happening when we crashed.
182 int btrfs_find_dead_roots(struct btrfs_root
*root
, u64 objectid
)
184 struct btrfs_root
*dead_root
;
185 struct btrfs_item
*item
;
186 struct btrfs_root_item
*ri
;
187 struct btrfs_key key
;
188 struct btrfs_key found_key
;
189 struct btrfs_path
*path
;
192 struct extent_buffer
*leaf
;
195 key
.objectid
= objectid
;
196 btrfs_set_key_type(&key
, BTRFS_ROOT_ITEM_KEY
);
198 path
= btrfs_alloc_path();
203 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
207 leaf
= path
->nodes
[0];
208 nritems
= btrfs_header_nritems(leaf
);
209 slot
= path
->slots
[0];
210 if (slot
>= nritems
) {
211 ret
= btrfs_next_leaf(root
, path
);
214 leaf
= path
->nodes
[0];
215 nritems
= btrfs_header_nritems(leaf
);
216 slot
= path
->slots
[0];
218 item
= btrfs_item_nr(leaf
, slot
);
219 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
220 if (btrfs_key_type(&key
) != BTRFS_ROOT_ITEM_KEY
)
223 if (key
.objectid
< objectid
)
226 if (key
.objectid
> objectid
)
229 ri
= btrfs_item_ptr(leaf
, slot
, struct btrfs_root_item
);
230 if (btrfs_disk_root_refs(leaf
, ri
) != 0)
233 memcpy(&found_key
, &key
, sizeof(key
));
235 btrfs_release_path(root
, path
);
237 btrfs_read_fs_root_no_radix(root
->fs_info
->tree_root
,
239 if (IS_ERR(dead_root
)) {
240 ret
= PTR_ERR(dead_root
);
244 ret
= btrfs_add_dead_root(dead_root
);
254 btrfs_free_path(path
);
258 int btrfs_find_orphan_roots(struct btrfs_root
*tree_root
)
260 struct extent_buffer
*leaf
;
261 struct btrfs_path
*path
;
262 struct btrfs_key key
;
266 path
= btrfs_alloc_path();
270 key
.objectid
= BTRFS_ORPHAN_OBJECTID
;
271 key
.type
= BTRFS_ORPHAN_ITEM_KEY
;
275 ret
= btrfs_search_slot(NULL
, tree_root
, &key
, path
, 0, 0);
281 leaf
= path
->nodes
[0];
282 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
283 ret
= btrfs_next_leaf(tree_root
, path
);
288 leaf
= path
->nodes
[0];
291 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
292 btrfs_release_path(tree_root
, path
);
294 if (key
.objectid
!= BTRFS_ORPHAN_OBJECTID
||
295 key
.type
!= BTRFS_ORPHAN_ITEM_KEY
)
298 ret
= btrfs_find_dead_roots(tree_root
, key
.offset
);
307 btrfs_free_path(path
);
311 /* drop the root item for 'key' from 'root' */
312 int btrfs_del_root(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
313 struct btrfs_key
*key
)
315 struct btrfs_path
*path
;
318 struct btrfs_root_item
*ri
;
319 struct extent_buffer
*leaf
;
321 path
= btrfs_alloc_path();
323 ret
= btrfs_search_slot(trans
, root
, key
, path
, -1, 1);
328 leaf
= path
->nodes
[0];
329 ri
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_root_item
);
331 refs
= btrfs_disk_root_refs(leaf
, ri
);
333 ret
= btrfs_del_item(trans
, root
, path
);
335 btrfs_release_path(root
, path
);
336 btrfs_free_path(path
);
340 int btrfs_del_root_ref(struct btrfs_trans_handle
*trans
,
341 struct btrfs_root
*tree_root
,
342 u64 root_id
, u64 ref_id
, u64 dirid
, u64
*sequence
,
343 const char *name
, int name_len
)
346 struct btrfs_path
*path
;
347 struct btrfs_root_ref
*ref
;
348 struct extent_buffer
*leaf
;
349 struct btrfs_key key
;
354 path
= btrfs_alloc_path();
358 key
.objectid
= root_id
;
359 key
.type
= BTRFS_ROOT_BACKREF_KEY
;
362 ret
= btrfs_search_slot(trans
, tree_root
, &key
, path
, -1, 1);
365 leaf
= path
->nodes
[0];
366 ref
= btrfs_item_ptr(leaf
, path
->slots
[0],
367 struct btrfs_root_ref
);
369 WARN_ON(btrfs_root_ref_dirid(leaf
, ref
) != dirid
);
370 WARN_ON(btrfs_root_ref_name_len(leaf
, ref
) != name_len
);
371 ptr
= (unsigned long)(ref
+ 1);
372 WARN_ON(memcmp_extent_buffer(leaf
, name
, ptr
, name_len
));
373 *sequence
= btrfs_root_ref_sequence(leaf
, ref
);
375 ret
= btrfs_del_item(trans
, tree_root
, path
);
380 if (key
.type
== BTRFS_ROOT_BACKREF_KEY
) {
381 btrfs_release_path(tree_root
, path
);
382 key
.objectid
= ref_id
;
383 key
.type
= BTRFS_ROOT_REF_KEY
;
384 key
.offset
= root_id
;
388 btrfs_free_path(path
);
392 int btrfs_find_root_ref(struct btrfs_root
*tree_root
,
393 struct btrfs_path
*path
,
394 u64 root_id
, u64 ref_id
)
396 struct btrfs_key key
;
399 key
.objectid
= root_id
;
400 key
.type
= BTRFS_ROOT_REF_KEY
;
403 ret
= btrfs_search_slot(NULL
, tree_root
, &key
, path
, 0, 0);
408 * add a btrfs_root_ref item. type is either BTRFS_ROOT_REF_KEY
409 * or BTRFS_ROOT_BACKREF_KEY.
411 * The dirid, sequence, name and name_len refer to the directory entry
412 * that is referencing the root.
414 * For a forward ref, the root_id is the id of the tree referencing
415 * the root and ref_id is the id of the subvol or snapshot.
417 * For a back ref the root_id is the id of the subvol or snapshot and
418 * ref_id is the id of the tree referencing it.
420 int btrfs_add_root_ref(struct btrfs_trans_handle
*trans
,
421 struct btrfs_root
*tree_root
,
422 u64 root_id
, u64 ref_id
, u64 dirid
, u64 sequence
,
423 const char *name
, int name_len
)
425 struct btrfs_key key
;
427 struct btrfs_path
*path
;
428 struct btrfs_root_ref
*ref
;
429 struct extent_buffer
*leaf
;
432 path
= btrfs_alloc_path();
436 key
.objectid
= root_id
;
437 key
.type
= BTRFS_ROOT_BACKREF_KEY
;
440 ret
= btrfs_insert_empty_item(trans
, tree_root
, path
, &key
,
441 sizeof(*ref
) + name_len
);
444 leaf
= path
->nodes
[0];
445 ref
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_root_ref
);
446 btrfs_set_root_ref_dirid(leaf
, ref
, dirid
);
447 btrfs_set_root_ref_sequence(leaf
, ref
, sequence
);
448 btrfs_set_root_ref_name_len(leaf
, ref
, name_len
);
449 ptr
= (unsigned long)(ref
+ 1);
450 write_extent_buffer(leaf
, name
, ptr
, name_len
);
451 btrfs_mark_buffer_dirty(leaf
);
453 if (key
.type
== BTRFS_ROOT_BACKREF_KEY
) {
454 btrfs_release_path(tree_root
, path
);
455 key
.objectid
= ref_id
;
456 key
.type
= BTRFS_ROOT_REF_KEY
;
457 key
.offset
= root_id
;
461 btrfs_free_path(path
);