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); |
76dda93c YZ |
97 | if (path->slots[0] == 0) { |
98 | ret = 1; | |
99 | goto out; | |
100 | } | |
5f39d397 | 101 | l = path->nodes[0]; |
5caf2a00 | 102 | slot = path->slots[0] - 1; |
5f39d397 | 103 | btrfs_item_key_to_cpu(l, &found_key, slot); |
76dda93c YZ |
104 | if (found_key.objectid != objectid || |
105 | found_key.type != BTRFS_ROOT_ITEM_KEY) { | |
3768f368 CM |
106 | ret = 1; |
107 | goto out; | |
108 | } | |
76dda93c YZ |
109 | if (item) |
110 | read_extent_buffer(l, item, btrfs_item_ptr_offset(l, slot), | |
111 | sizeof(*item)); | |
112 | if (key) | |
113 | memcpy(key, &found_key, sizeof(found_key)); | |
3768f368 CM |
114 | ret = 0; |
115 | out: | |
5caf2a00 | 116 | btrfs_free_path(path); |
3768f368 CM |
117 | return ret; |
118 | } | |
119 | ||
5d4f98a2 YZ |
120 | int btrfs_set_root_node(struct btrfs_root_item *item, |
121 | struct extent_buffer *node) | |
122 | { | |
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)); | |
126 | return 0; | |
127 | } | |
128 | ||
d352ac68 CM |
129 | /* |
130 | * copy the data in 'item' into the btree | |
131 | */ | |
e089f05c CM |
132 | int btrfs_update_root(struct btrfs_trans_handle *trans, struct btrfs_root |
133 | *root, struct btrfs_key *key, struct btrfs_root_item | |
134 | *item) | |
3768f368 | 135 | { |
5caf2a00 | 136 | struct btrfs_path *path; |
5f39d397 | 137 | struct extent_buffer *l; |
3768f368 CM |
138 | int ret; |
139 | int slot; | |
5f39d397 | 140 | unsigned long ptr; |
3768f368 | 141 | |
5caf2a00 CM |
142 | path = btrfs_alloc_path(); |
143 | BUG_ON(!path); | |
5caf2a00 | 144 | ret = btrfs_search_slot(trans, root, key, path, 0, 1); |
3768f368 CM |
145 | if (ret < 0) |
146 | goto out; | |
d6667462 CM |
147 | |
148 | if (ret != 0) { | |
149 | btrfs_print_leaf(root, path->nodes[0]); | |
d397712b CM |
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); | |
d6667462 CM |
153 | BUG_ON(1); |
154 | } | |
155 | ||
5f39d397 | 156 | l = path->nodes[0]; |
5caf2a00 | 157 | slot = path->slots[0]; |
5f39d397 CM |
158 | ptr = btrfs_item_ptr_offset(l, slot); |
159 | write_extent_buffer(l, item, ptr, sizeof(*item)); | |
5caf2a00 | 160 | btrfs_mark_buffer_dirty(path->nodes[0]); |
3768f368 | 161 | out: |
5caf2a00 | 162 | btrfs_free_path(path); |
3768f368 CM |
163 | return ret; |
164 | } | |
165 | ||
e089f05c CM |
166 | int btrfs_insert_root(struct btrfs_trans_handle *trans, struct btrfs_root |
167 | *root, struct btrfs_key *key, struct btrfs_root_item | |
168 | *item) | |
3768f368 CM |
169 | { |
170 | int ret; | |
e089f05c | 171 | ret = btrfs_insert_item(trans, root, key, item, sizeof(*item)); |
3768f368 CM |
172 | return ret; |
173 | } | |
174 | ||
d352ac68 CM |
175 | /* |
176 | * at mount time we want to find all the old transaction snapshots that were in | |
d397712b CM |
177 | * the process of being deleted if we crashed. This is any root item with an |
178 | * offset lower than the latest root. They need to be queued for deletion to | |
179 | * finish what was happening when we crashed. | |
d352ac68 | 180 | */ |
5d4f98a2 | 181 | int btrfs_find_dead_roots(struct btrfs_root *root, u64 objectid) |
5eda7b5e CM |
182 | { |
183 | struct btrfs_root *dead_root; | |
184 | struct btrfs_item *item; | |
185 | struct btrfs_root_item *ri; | |
186 | struct btrfs_key key; | |
a7a16fd7 | 187 | struct btrfs_key found_key; |
5eda7b5e CM |
188 | struct btrfs_path *path; |
189 | int ret; | |
190 | u32 nritems; | |
5f39d397 | 191 | struct extent_buffer *leaf; |
5eda7b5e CM |
192 | int slot; |
193 | ||
5ce14bbc | 194 | key.objectid = objectid; |
5eda7b5e CM |
195 | btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY); |
196 | key.offset = 0; | |
197 | path = btrfs_alloc_path(); | |
198 | if (!path) | |
199 | return -ENOMEM; | |
a7a16fd7 CM |
200 | |
201 | again: | |
5eda7b5e CM |
202 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
203 | if (ret < 0) | |
204 | goto err; | |
d397712b | 205 | while (1) { |
5f39d397 CM |
206 | leaf = path->nodes[0]; |
207 | nritems = btrfs_header_nritems(leaf); | |
5eda7b5e CM |
208 | slot = path->slots[0]; |
209 | if (slot >= nritems) { | |
210 | ret = btrfs_next_leaf(root, path); | |
211 | if (ret) | |
212 | break; | |
5f39d397 CM |
213 | leaf = path->nodes[0]; |
214 | nritems = btrfs_header_nritems(leaf); | |
5eda7b5e CM |
215 | slot = path->slots[0]; |
216 | } | |
5f39d397 CM |
217 | item = btrfs_item_nr(leaf, slot); |
218 | btrfs_item_key_to_cpu(leaf, &key, slot); | |
5eda7b5e CM |
219 | if (btrfs_key_type(&key) != BTRFS_ROOT_ITEM_KEY) |
220 | goto next; | |
5ce14bbc CM |
221 | |
222 | if (key.objectid < objectid) | |
223 | goto next; | |
224 | ||
225 | if (key.objectid > objectid) | |
226 | break; | |
227 | ||
5eda7b5e | 228 | ri = btrfs_item_ptr(leaf, slot, struct btrfs_root_item); |
5f39d397 | 229 | if (btrfs_disk_root_refs(leaf, ri) != 0) |
5eda7b5e | 230 | goto next; |
5ce14bbc | 231 | |
a7a16fd7 CM |
232 | memcpy(&found_key, &key, sizeof(key)); |
233 | key.offset++; | |
234 | btrfs_release_path(root, path); | |
e02119d5 CM |
235 | dead_root = |
236 | btrfs_read_fs_root_no_radix(root->fs_info->tree_root, | |
237 | &found_key); | |
a1f39630 A |
238 | if (IS_ERR(dead_root)) { |
239 | ret = PTR_ERR(dead_root); | |
5eda7b5e CM |
240 | goto err; |
241 | } | |
5ce14bbc | 242 | |
5d4f98a2 | 243 | ret = btrfs_add_dead_root(dead_root); |
5eda7b5e CM |
244 | if (ret) |
245 | goto err; | |
a7a16fd7 | 246 | goto again; |
5eda7b5e CM |
247 | next: |
248 | slot++; | |
249 | path->slots[0]++; | |
250 | } | |
251 | ret = 0; | |
252 | err: | |
253 | btrfs_free_path(path); | |
254 | return ret; | |
255 | } | |
256 | ||
76dda93c YZ |
257 | int btrfs_find_orphan_roots(struct btrfs_root *tree_root) |
258 | { | |
259 | struct extent_buffer *leaf; | |
260 | struct btrfs_path *path; | |
261 | struct btrfs_key key; | |
d68fc57b YZ |
262 | struct btrfs_key root_key; |
263 | struct btrfs_root *root; | |
76dda93c YZ |
264 | int err = 0; |
265 | int ret; | |
266 | ||
267 | path = btrfs_alloc_path(); | |
268 | if (!path) | |
269 | return -ENOMEM; | |
270 | ||
271 | key.objectid = BTRFS_ORPHAN_OBJECTID; | |
272 | key.type = BTRFS_ORPHAN_ITEM_KEY; | |
273 | key.offset = 0; | |
274 | ||
d68fc57b YZ |
275 | root_key.type = BTRFS_ROOT_ITEM_KEY; |
276 | root_key.offset = (u64)-1; | |
277 | ||
76dda93c YZ |
278 | while (1) { |
279 | ret = btrfs_search_slot(NULL, tree_root, &key, path, 0, 0); | |
280 | if (ret < 0) { | |
281 | err = ret; | |
282 | break; | |
283 | } | |
284 | ||
285 | leaf = path->nodes[0]; | |
286 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { | |
287 | ret = btrfs_next_leaf(tree_root, path); | |
288 | if (ret < 0) | |
289 | err = ret; | |
290 | if (ret != 0) | |
291 | break; | |
292 | leaf = path->nodes[0]; | |
293 | } | |
294 | ||
295 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); | |
296 | btrfs_release_path(tree_root, path); | |
297 | ||
298 | if (key.objectid != BTRFS_ORPHAN_OBJECTID || | |
299 | key.type != BTRFS_ORPHAN_ITEM_KEY) | |
300 | break; | |
301 | ||
d68fc57b YZ |
302 | root_key.objectid = key.offset; |
303 | key.offset++; | |
304 | ||
305 | root = btrfs_read_fs_root_no_name(tree_root->fs_info, | |
306 | &root_key); | |
307 | if (!IS_ERR(root)) | |
308 | continue; | |
309 | ||
310 | ret = PTR_ERR(root); | |
311 | if (ret != -ENOENT) { | |
76dda93c YZ |
312 | err = ret; |
313 | break; | |
314 | } | |
315 | ||
d68fc57b YZ |
316 | ret = btrfs_find_dead_roots(tree_root, root_key.objectid); |
317 | if (ret) { | |
318 | err = ret; | |
319 | break; | |
320 | } | |
76dda93c YZ |
321 | } |
322 | ||
323 | btrfs_free_path(path); | |
324 | return err; | |
325 | } | |
326 | ||
d352ac68 | 327 | /* drop the root item for 'key' from 'root' */ |
e089f05c CM |
328 | int btrfs_del_root(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
329 | struct btrfs_key *key) | |
3768f368 | 330 | { |
5caf2a00 | 331 | struct btrfs_path *path; |
3768f368 | 332 | int ret; |
c5739bba | 333 | struct btrfs_root_item *ri; |
5f39d397 | 334 | struct extent_buffer *leaf; |
3768f368 | 335 | |
5caf2a00 CM |
336 | path = btrfs_alloc_path(); |
337 | BUG_ON(!path); | |
5caf2a00 | 338 | ret = btrfs_search_slot(trans, root, key, path, -1, 1); |
3768f368 CM |
339 | if (ret < 0) |
340 | goto out; | |
edbd8d4e | 341 | |
3768f368 | 342 | BUG_ON(ret != 0); |
5f39d397 CM |
343 | leaf = path->nodes[0]; |
344 | ri = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_item); | |
c5739bba | 345 | |
5eda7b5e | 346 | ret = btrfs_del_item(trans, root, path); |
3768f368 | 347 | out: |
5caf2a00 | 348 | btrfs_free_path(path); |
3768f368 CM |
349 | return ret; |
350 | } | |
0660b5af CM |
351 | |
352 | int btrfs_del_root_ref(struct btrfs_trans_handle *trans, | |
353 | struct btrfs_root *tree_root, | |
4df27c4d YZ |
354 | u64 root_id, u64 ref_id, u64 dirid, u64 *sequence, |
355 | const char *name, int name_len) | |
356 | ||
0660b5af | 357 | { |
4df27c4d YZ |
358 | struct btrfs_path *path; |
359 | struct btrfs_root_ref *ref; | |
360 | struct extent_buffer *leaf; | |
0660b5af | 361 | struct btrfs_key key; |
4df27c4d YZ |
362 | unsigned long ptr; |
363 | int err = 0; | |
0660b5af | 364 | int ret; |
0660b5af CM |
365 | |
366 | path = btrfs_alloc_path(); | |
4df27c4d YZ |
367 | if (!path) |
368 | return -ENOMEM; | |
0660b5af CM |
369 | |
370 | key.objectid = root_id; | |
4df27c4d | 371 | key.type = BTRFS_ROOT_BACKREF_KEY; |
0660b5af | 372 | key.offset = ref_id; |
4df27c4d | 373 | again: |
0660b5af | 374 | ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1); |
4df27c4d YZ |
375 | BUG_ON(ret < 0); |
376 | if (ret == 0) { | |
377 | leaf = path->nodes[0]; | |
378 | ref = btrfs_item_ptr(leaf, path->slots[0], | |
379 | struct btrfs_root_ref); | |
380 | ||
381 | WARN_ON(btrfs_root_ref_dirid(leaf, ref) != dirid); | |
382 | WARN_ON(btrfs_root_ref_name_len(leaf, ref) != name_len); | |
383 | ptr = (unsigned long)(ref + 1); | |
384 | WARN_ON(memcmp_extent_buffer(leaf, name, ptr, name_len)); | |
385 | *sequence = btrfs_root_ref_sequence(leaf, ref); | |
386 | ||
387 | ret = btrfs_del_item(trans, tree_root, path); | |
388 | BUG_ON(ret); | |
389 | } else | |
390 | err = -ENOENT; | |
391 | ||
392 | if (key.type == BTRFS_ROOT_BACKREF_KEY) { | |
393 | btrfs_release_path(tree_root, path); | |
394 | key.objectid = ref_id; | |
395 | key.type = BTRFS_ROOT_REF_KEY; | |
396 | key.offset = root_id; | |
397 | goto again; | |
398 | } | |
0660b5af CM |
399 | |
400 | btrfs_free_path(path); | |
4df27c4d | 401 | return err; |
0660b5af CM |
402 | } |
403 | ||
ea9e8b11 CM |
404 | int btrfs_find_root_ref(struct btrfs_root *tree_root, |
405 | struct btrfs_path *path, | |
406 | u64 root_id, u64 ref_id) | |
407 | { | |
408 | struct btrfs_key key; | |
409 | int ret; | |
410 | ||
411 | key.objectid = root_id; | |
412 | key.type = BTRFS_ROOT_REF_KEY; | |
413 | key.offset = ref_id; | |
414 | ||
415 | ret = btrfs_search_slot(NULL, tree_root, &key, path, 0, 0); | |
416 | return ret; | |
417 | } | |
418 | ||
0660b5af CM |
419 | /* |
420 | * add a btrfs_root_ref item. type is either BTRFS_ROOT_REF_KEY | |
421 | * or BTRFS_ROOT_BACKREF_KEY. | |
422 | * | |
423 | * The dirid, sequence, name and name_len refer to the directory entry | |
424 | * that is referencing the root. | |
425 | * | |
426 | * For a forward ref, the root_id is the id of the tree referencing | |
427 | * the root and ref_id is the id of the subvol or snapshot. | |
428 | * | |
429 | * For a back ref the root_id is the id of the subvol or snapshot and | |
430 | * ref_id is the id of the tree referencing it. | |
431 | */ | |
432 | int btrfs_add_root_ref(struct btrfs_trans_handle *trans, | |
433 | struct btrfs_root *tree_root, | |
4df27c4d | 434 | u64 root_id, u64 ref_id, u64 dirid, u64 sequence, |
0660b5af CM |
435 | const char *name, int name_len) |
436 | { | |
437 | struct btrfs_key key; | |
438 | int ret; | |
439 | struct btrfs_path *path; | |
440 | struct btrfs_root_ref *ref; | |
441 | struct extent_buffer *leaf; | |
442 | unsigned long ptr; | |
443 | ||
0660b5af | 444 | path = btrfs_alloc_path(); |
4df27c4d YZ |
445 | if (!path) |
446 | return -ENOMEM; | |
0660b5af CM |
447 | |
448 | key.objectid = root_id; | |
4df27c4d | 449 | key.type = BTRFS_ROOT_BACKREF_KEY; |
0660b5af | 450 | key.offset = ref_id; |
4df27c4d | 451 | again: |
0660b5af CM |
452 | ret = btrfs_insert_empty_item(trans, tree_root, path, &key, |
453 | sizeof(*ref) + name_len); | |
454 | BUG_ON(ret); | |
455 | ||
456 | leaf = path->nodes[0]; | |
457 | ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref); | |
458 | btrfs_set_root_ref_dirid(leaf, ref, dirid); | |
459 | btrfs_set_root_ref_sequence(leaf, ref, sequence); | |
460 | btrfs_set_root_ref_name_len(leaf, ref, name_len); | |
461 | ptr = (unsigned long)(ref + 1); | |
462 | write_extent_buffer(leaf, name, ptr, name_len); | |
463 | btrfs_mark_buffer_dirty(leaf); | |
464 | ||
4df27c4d YZ |
465 | if (key.type == BTRFS_ROOT_BACKREF_KEY) { |
466 | btrfs_release_path(tree_root, path); | |
467 | key.objectid = ref_id; | |
468 | key.type = BTRFS_ROOT_REF_KEY; | |
469 | key.offset = root_id; | |
470 | goto again; | |
471 | } | |
472 | ||
0660b5af | 473 | btrfs_free_path(path); |
4df27c4d | 474 | return 0; |
0660b5af | 475 | } |