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"
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
;
34 search_key
.objectid
= objectid
;
35 search_key
.type
= (u8
)-1;
36 search_key
.offset
= (u64
)-1;
38 path
= btrfs_alloc_path();
40 ret
= btrfs_search_slot(NULL
, root
, &search_key
, path
, 0, 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
) {
53 read_extent_buffer(l
, item
, btrfs_item_ptr_offset(l
, slot
),
55 memcpy(key
, &found_key
, sizeof(found_key
));
58 btrfs_release_path(root
, path
);
59 btrfs_free_path(path
);
63 int btrfs_update_root(struct btrfs_trans_handle
*trans
, struct btrfs_root
64 *root
, struct btrfs_key
*key
, struct btrfs_root_item
67 struct btrfs_path
*path
;
68 struct extent_buffer
*l
;
73 path
= btrfs_alloc_path();
75 ret
= btrfs_search_slot(trans
, root
, key
, path
, 0, 1);
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]);
85 btrfs_release_path(root
, path
);
86 btrfs_free_path(path
);
90 int btrfs_insert_root(struct btrfs_trans_handle
*trans
, struct btrfs_root
91 *root
, struct btrfs_key
*key
, struct btrfs_root_item
95 ret
= btrfs_insert_item(trans
, root
, key
, item
, sizeof(*item
));
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
;
110 struct extent_buffer
*leaf
;
113 key
.objectid
= objectid
;
114 btrfs_set_key_type(&key
, BTRFS_ROOT_ITEM_KEY
);
116 path
= btrfs_alloc_path();
119 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
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
);
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
)
139 if (key
.objectid
< objectid
)
142 if (key
.objectid
> objectid
)
145 ri
= btrfs_item_ptr(leaf
, slot
, struct btrfs_root_item
);
146 if (btrfs_disk_root_refs(leaf
, ri
) != 0)
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
);
155 ret
= btrfs_add_dead_root(dead_root
, latest
,
156 &root
->fs_info
->dead_roots
);
165 btrfs_free_path(path
);
170 int btrfs_del_root(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
171 struct btrfs_key
*key
)
173 struct btrfs_path
*path
;
176 struct btrfs_root_item
*ri
;
177 struct extent_buffer
*leaf
;
179 path
= btrfs_alloc_path();
181 ret
= btrfs_search_slot(trans
, root
, key
, path
, -1, 1);
185 btrfs_print_leaf(root
, path
->nodes
[0]);
186 printk("failed to del %Lu %u %Lu\n", key
->objectid
, key
->type
, key
->offset
);
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
);
195 ret
= btrfs_del_item(trans
, root
, path
);
197 btrfs_release_path(root
, path
);
198 btrfs_free_path(path
);