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