Commit | Line | Data |
---|---|---|
fec577fb CM |
1 | #include <stdio.h> |
2 | #include <stdlib.h> | |
3 | #include "kerncompat.h" | |
4 | #include "radix-tree.h" | |
5 | #include "ctree.h" | |
6 | #include "disk-io.h" | |
7 | #include "print-tree.h" | |
8 | ||
037e6390 | 9 | static int find_free_extent(struct ctree_root *orig_root, u64 num_blocks, |
e2fa7227 CM |
10 | u64 search_start, u64 search_end, |
11 | struct btrfs_key *ins); | |
037e6390 CM |
12 | static int finish_current_insert(struct ctree_root *extent_root); |
13 | static int run_pending(struct ctree_root *extent_root); | |
14 | ||
fec577fb CM |
15 | /* |
16 | * pending extents are blocks that we're trying to allocate in the extent | |
17 | * map while trying to grow the map because of other allocations. To avoid | |
18 | * recursing, they are tagged in the radix tree and cleaned up after | |
19 | * other allocations are done. The pending tag is also used in the same | |
20 | * manner for deletes. | |
21 | */ | |
037e6390 | 22 | #define CTREE_EXTENT_PENDING_DEL 0 |
fec577fb | 23 | |
02217ed2 CM |
24 | static int inc_block_ref(struct ctree_root *root, u64 blocknr) |
25 | { | |
26 | struct ctree_path path; | |
27 | int ret; | |
e2fa7227 | 28 | struct btrfs_key key; |
02217ed2 CM |
29 | struct leaf *l; |
30 | struct extent_item *item; | |
e2fa7227 | 31 | struct btrfs_key ins; |
037e6390 CM |
32 | |
33 | find_free_extent(root->extent_root, 0, 0, (u64)-1, &ins); | |
02217ed2 CM |
34 | init_path(&path); |
35 | key.objectid = blocknr; | |
36 | key.flags = 0; | |
37 | key.offset = 1; | |
38 | ret = search_slot(root->extent_root, &key, &path, 0, 1); | |
a28ec197 CM |
39 | if (ret != 0) |
40 | BUG(); | |
02217ed2 CM |
41 | BUG_ON(ret != 0); |
42 | l = &path.nodes[0]->leaf; | |
43 | item = (struct extent_item *)(l->data + | |
44 | l->items[path.slots[0]].offset); | |
45 | item->refs++; | |
a28ec197 | 46 | |
02217ed2 CM |
47 | BUG_ON(list_empty(&path.nodes[0]->dirty)); |
48 | release_path(root->extent_root, &path); | |
037e6390 CM |
49 | finish_current_insert(root->extent_root); |
50 | run_pending(root->extent_root); | |
02217ed2 CM |
51 | return 0; |
52 | } | |
53 | ||
20524f02 | 54 | static int lookup_block_ref(struct ctree_root *root, u64 blocknr, u32 *refs) |
a28ec197 CM |
55 | { |
56 | struct ctree_path path; | |
57 | int ret; | |
e2fa7227 | 58 | struct btrfs_key key; |
a28ec197 CM |
59 | struct leaf *l; |
60 | struct extent_item *item; | |
61 | init_path(&path); | |
62 | key.objectid = blocknr; | |
63 | key.flags = 0; | |
64 | key.offset = 1; | |
65 | ret = search_slot(root->extent_root, &key, &path, 0, 0); | |
66 | if (ret != 0) | |
67 | BUG(); | |
68 | l = &path.nodes[0]->leaf; | |
69 | item = (struct extent_item *)(l->data + | |
70 | l->items[path.slots[0]].offset); | |
71 | *refs = item->refs; | |
72 | release_path(root->extent_root, &path); | |
73 | return 0; | |
74 | } | |
75 | ||
02217ed2 CM |
76 | int btrfs_inc_ref(struct ctree_root *root, struct tree_buffer *buf) |
77 | { | |
78 | u64 blocknr; | |
79 | int i; | |
a28ec197 CM |
80 | |
81 | if (root == root->extent_root) | |
82 | return 0; | |
7518a238 | 83 | if (btrfs_is_leaf(&buf->node)) |
a28ec197 CM |
84 | return 0; |
85 | ||
7518a238 | 86 | for (i = 0; i < btrfs_header_nritems(&buf->node.header); i++) { |
02217ed2 CM |
87 | blocknr = buf->node.blockptrs[i]; |
88 | inc_block_ref(root, blocknr); | |
89 | } | |
90 | return 0; | |
91 | } | |
92 | ||
a28ec197 CM |
93 | int btrfs_finish_extent_commit(struct ctree_root *root) |
94 | { | |
95 | struct ctree_root *extent_root = root->extent_root; | |
96 | unsigned long gang[8]; | |
97 | int ret; | |
98 | int i; | |
99 | ||
100 | while(1) { | |
101 | ret = radix_tree_gang_lookup(&extent_root->pinned_radix, | |
102 | (void **)gang, 0, | |
103 | ARRAY_SIZE(gang)); | |
104 | if (!ret) | |
105 | break; | |
0579da42 | 106 | for (i = 0; i < ret; i++) { |
a28ec197 | 107 | radix_tree_delete(&extent_root->pinned_radix, gang[i]); |
0579da42 | 108 | } |
a28ec197 | 109 | } |
0579da42 CM |
110 | extent_root->last_insert.objectid = 0; |
111 | extent_root->last_insert.offset = 0; | |
a28ec197 CM |
112 | return 0; |
113 | } | |
114 | ||
037e6390 CM |
115 | static int finish_current_insert(struct ctree_root *extent_root) |
116 | { | |
e2fa7227 | 117 | struct btrfs_key ins; |
037e6390 CM |
118 | struct extent_item extent_item; |
119 | int i; | |
120 | int ret; | |
121 | ||
122 | extent_item.refs = 1; | |
7518a238 CM |
123 | extent_item.owner = |
124 | btrfs_header_parentid(&extent_root->node->node.header); | |
037e6390 CM |
125 | ins.offset = 1; |
126 | ins.flags = 0; | |
127 | ||
128 | for (i = 0; i < extent_root->current_insert.flags; i++) { | |
129 | ins.objectid = extent_root->current_insert.objectid + i; | |
130 | ret = insert_item(extent_root, &ins, &extent_item, | |
131 | sizeof(extent_item)); | |
132 | BUG_ON(ret); | |
133 | } | |
134 | extent_root->current_insert.offset = 0; | |
135 | return 0; | |
136 | } | |
137 | ||
fec577fb | 138 | /* |
a28ec197 | 139 | * remove an extent from the root, returns 0 on success |
fec577fb | 140 | */ |
a28ec197 CM |
141 | int __free_extent(struct ctree_root *root, u64 blocknr, u64 num_blocks) |
142 | { | |
143 | struct ctree_path path; | |
e2fa7227 | 144 | struct btrfs_key key; |
a28ec197 CM |
145 | struct ctree_root *extent_root = root->extent_root; |
146 | int ret; | |
147 | struct item *item; | |
148 | struct extent_item *ei; | |
e2fa7227 | 149 | struct btrfs_key ins; |
037e6390 | 150 | |
a28ec197 CM |
151 | key.objectid = blocknr; |
152 | key.flags = 0; | |
153 | key.offset = num_blocks; | |
154 | ||
037e6390 | 155 | find_free_extent(root, 0, 0, (u64)-1, &ins); |
a28ec197 CM |
156 | init_path(&path); |
157 | ret = search_slot(extent_root, &key, &path, -1, 1); | |
158 | if (ret) { | |
159 | printf("failed to find %Lu\n", key.objectid); | |
160 | print_tree(extent_root, extent_root->node); | |
161 | printf("failed to find %Lu\n", key.objectid); | |
162 | BUG(); | |
163 | } | |
164 | item = path.nodes[0]->leaf.items + path.slots[0]; | |
165 | ei = (struct extent_item *)(path.nodes[0]->leaf.data + item->offset); | |
166 | BUG_ON(ei->refs == 0); | |
167 | ei->refs--; | |
168 | if (ei->refs == 0) { | |
169 | if (root == extent_root) { | |
170 | int err; | |
171 | radix_tree_preload(GFP_KERNEL); | |
172 | err = radix_tree_insert(&extent_root->pinned_radix, | |
173 | blocknr, (void *)blocknr); | |
174 | BUG_ON(err); | |
175 | radix_tree_preload_end(); | |
176 | } | |
177 | ret = del_item(extent_root, &path); | |
0579da42 CM |
178 | if (root != extent_root && |
179 | extent_root->last_insert.objectid < blocknr) | |
180 | extent_root->last_insert.objectid = blocknr; | |
a28ec197 CM |
181 | if (ret) |
182 | BUG(); | |
183 | } | |
184 | release_path(extent_root, &path); | |
037e6390 | 185 | finish_current_insert(extent_root); |
a28ec197 CM |
186 | return ret; |
187 | } | |
188 | ||
a28ec197 CM |
189 | /* |
190 | * find all the blocks marked as pending in the radix tree and remove | |
191 | * them from the extent map | |
192 | */ | |
193 | static int del_pending_extents(struct ctree_root *extent_root) | |
194 | { | |
195 | int ret; | |
196 | struct tree_buffer *gang[4]; | |
197 | int i; | |
198 | ||
199 | while(1) { | |
200 | ret = radix_tree_gang_lookup_tag(&extent_root->cache_radix, | |
201 | (void **)gang, 0, | |
202 | ARRAY_SIZE(gang), | |
203 | CTREE_EXTENT_PENDING_DEL); | |
204 | if (!ret) | |
205 | break; | |
206 | for (i = 0; i < ret; i++) { | |
207 | ret = __free_extent(extent_root, gang[i]->blocknr, 1); | |
fec577fb CM |
208 | radix_tree_tag_clear(&extent_root->cache_radix, |
209 | gang[i]->blocknr, | |
a28ec197 | 210 | CTREE_EXTENT_PENDING_DEL); |
fec577fb CM |
211 | tree_block_release(extent_root, gang[i]); |
212 | } | |
213 | } | |
214 | return 0; | |
215 | } | |
216 | ||
a28ec197 CM |
217 | static int run_pending(struct ctree_root *extent_root) |
218 | { | |
219 | while(radix_tree_tagged(&extent_root->cache_radix, | |
037e6390 | 220 | CTREE_EXTENT_PENDING_DEL)) |
a28ec197 | 221 | del_pending_extents(extent_root); |
a28ec197 CM |
222 | return 0; |
223 | } | |
224 | ||
225 | ||
fec577fb CM |
226 | /* |
227 | * remove an extent from the root, returns 0 on success | |
228 | */ | |
229 | int free_extent(struct ctree_root *root, u64 blocknr, u64 num_blocks) | |
230 | { | |
e2fa7227 | 231 | struct btrfs_key key; |
fec577fb CM |
232 | struct ctree_root *extent_root = root->extent_root; |
233 | struct tree_buffer *t; | |
234 | int pending_ret; | |
235 | int ret; | |
a28ec197 | 236 | |
fec577fb | 237 | if (root == extent_root) { |
a28ec197 | 238 | t = find_tree_block(root, blocknr); |
037e6390 | 239 | radix_tree_tag_set(&root->cache_radix, blocknr, |
a28ec197 | 240 | CTREE_EXTENT_PENDING_DEL); |
fec577fb CM |
241 | return 0; |
242 | } | |
a28ec197 CM |
243 | key.objectid = blocknr; |
244 | key.flags = 0; | |
245 | key.offset = num_blocks; | |
246 | ret = __free_extent(root, blocknr, num_blocks); | |
247 | pending_ret = run_pending(root->extent_root); | |
fec577fb CM |
248 | return ret ? ret : pending_ret; |
249 | } | |
250 | ||
251 | /* | |
252 | * walks the btree of allocated extents and find a hole of a given size. | |
253 | * The key ins is changed to record the hole: | |
254 | * ins->objectid == block start | |
255 | * ins->flags = 0 | |
256 | * ins->offset == number of blocks | |
257 | * Any available blocks before search_start are skipped. | |
258 | */ | |
0f70abe2 | 259 | static int find_free_extent(struct ctree_root *orig_root, u64 num_blocks, |
e2fa7227 CM |
260 | u64 search_start, u64 search_end, |
261 | struct btrfs_key *ins) | |
fec577fb CM |
262 | { |
263 | struct ctree_path path; | |
e2fa7227 | 264 | struct btrfs_key key; |
fec577fb CM |
265 | int ret; |
266 | u64 hole_size = 0; | |
267 | int slot = 0; | |
268 | u64 last_block; | |
037e6390 | 269 | u64 test_block; |
fec577fb CM |
270 | int start_found; |
271 | struct leaf *l; | |
272 | struct ctree_root * root = orig_root->extent_root; | |
0579da42 | 273 | int total_needed = num_blocks; |
fec577fb | 274 | |
7518a238 | 275 | total_needed += (btrfs_header_level(&root->node->node.header) + 1) * 3; |
0579da42 CM |
276 | if (root->last_insert.objectid > search_start) |
277 | search_start = root->last_insert.objectid; | |
fec577fb CM |
278 | check_failed: |
279 | init_path(&path); | |
280 | ins->objectid = search_start; | |
281 | ins->offset = 0; | |
282 | ins->flags = 0; | |
283 | start_found = 0; | |
02217ed2 | 284 | ret = search_slot(root, ins, &path, 0, 0); |
0f70abe2 CM |
285 | if (ret < 0) |
286 | goto error; | |
aa5d6bed | 287 | |
0579da42 CM |
288 | if (path.slots[0] > 0) |
289 | path.slots[0]--; | |
290 | ||
fec577fb CM |
291 | while (1) { |
292 | l = &path.nodes[0]->leaf; | |
293 | slot = path.slots[0]; | |
7518a238 | 294 | if (slot >= btrfs_header_nritems(&l->header)) { |
fec577fb CM |
295 | ret = next_leaf(root, &path); |
296 | if (ret == 0) | |
297 | continue; | |
0f70abe2 CM |
298 | if (ret < 0) |
299 | goto error; | |
fec577fb CM |
300 | if (!start_found) { |
301 | ins->objectid = search_start; | |
037e6390 | 302 | ins->offset = (u64)-1; |
fec577fb CM |
303 | start_found = 1; |
304 | goto check_pending; | |
305 | } | |
306 | ins->objectid = last_block > search_start ? | |
307 | last_block : search_start; | |
037e6390 | 308 | ins->offset = (u64)-1; |
fec577fb CM |
309 | goto check_pending; |
310 | } | |
e2fa7227 CM |
311 | btrfs_disk_key_to_cpu(&key, &l->items[slot].key); |
312 | if (key.objectid >= search_start) { | |
fec577fb | 313 | if (start_found) { |
0579da42 CM |
314 | if (last_block < search_start) |
315 | last_block = search_start; | |
e2fa7227 | 316 | hole_size = key.objectid - last_block; |
037e6390 | 317 | if (hole_size > total_needed) { |
fec577fb | 318 | ins->objectid = last_block; |
037e6390 | 319 | ins->offset = hole_size; |
fec577fb CM |
320 | goto check_pending; |
321 | } | |
0579da42 | 322 | } |
fec577fb | 323 | } |
0579da42 | 324 | start_found = 1; |
e2fa7227 | 325 | last_block = key.objectid + key.offset; |
fec577fb CM |
326 | path.slots[0]++; |
327 | } | |
328 | // FIXME -ENOSPC | |
329 | check_pending: | |
330 | /* we have to make sure we didn't find an extent that has already | |
331 | * been allocated by the map tree or the original allocation | |
332 | */ | |
333 | release_path(root, &path); | |
334 | BUG_ON(ins->objectid < search_start); | |
037e6390 CM |
335 | for (test_block = ins->objectid; |
336 | test_block < ins->objectid + total_needed; test_block++) { | |
337 | if (radix_tree_lookup(&root->pinned_radix, test_block)) { | |
338 | search_start = test_block + 1; | |
fec577fb CM |
339 | goto check_failed; |
340 | } | |
341 | } | |
037e6390 | 342 | BUG_ON(root->current_insert.offset); |
0579da42 | 343 | root->current_insert.offset = total_needed - num_blocks; |
037e6390 CM |
344 | root->current_insert.objectid = ins->objectid + num_blocks; |
345 | root->current_insert.flags = 0; | |
0579da42 | 346 | root->last_insert.objectid = ins->objectid; |
037e6390 | 347 | ins->offset = num_blocks; |
fec577fb | 348 | return 0; |
0f70abe2 CM |
349 | error: |
350 | release_path(root, &path); | |
351 | return ret; | |
fec577fb CM |
352 | } |
353 | ||
fec577fb CM |
354 | /* |
355 | * finds a free extent and does all the dirty work required for allocation | |
356 | * returns the key for the extent through ins, and a tree buffer for | |
357 | * the first block of the extent through buf. | |
358 | * | |
359 | * returns 0 if everything worked, non-zero otherwise. | |
360 | */ | |
361 | int alloc_extent(struct ctree_root *root, u64 num_blocks, u64 search_start, | |
e2fa7227 | 362 | u64 search_end, u64 owner, struct btrfs_key *ins) |
fec577fb CM |
363 | { |
364 | int ret; | |
365 | int pending_ret; | |
037e6390 | 366 | struct ctree_root *extent_root = root->extent_root; |
fec577fb | 367 | struct extent_item extent_item; |
037e6390 | 368 | |
fec577fb CM |
369 | extent_item.refs = 1; |
370 | extent_item.owner = owner; | |
371 | ||
037e6390 CM |
372 | if (root == extent_root) { |
373 | BUG_ON(extent_root->current_insert.offset == 0); | |
374 | BUG_ON(num_blocks != 1); | |
375 | BUG_ON(extent_root->current_insert.flags == | |
376 | extent_root->current_insert.offset); | |
377 | ins->offset = 1; | |
378 | ins->objectid = extent_root->current_insert.objectid + | |
379 | extent_root->current_insert.flags++; | |
fec577fb CM |
380 | return 0; |
381 | } | |
037e6390 CM |
382 | ret = find_free_extent(root, num_blocks, search_start, |
383 | search_end, ins); | |
384 | if (ret) | |
385 | return ret; | |
fec577fb | 386 | |
037e6390 CM |
387 | ret = insert_item(extent_root, ins, &extent_item, |
388 | sizeof(extent_item)); | |
389 | ||
390 | finish_current_insert(extent_root); | |
391 | pending_ret = run_pending(extent_root); | |
392 | if (ret) | |
393 | return ret; | |
394 | if (pending_ret) | |
395 | return pending_ret; | |
396 | return 0; | |
fec577fb CM |
397 | } |
398 | ||
399 | /* | |
400 | * helper function to allocate a block for a given tree | |
401 | * returns the tree buffer or NULL. | |
402 | */ | |
403 | struct tree_buffer *alloc_free_block(struct ctree_root *root) | |
404 | { | |
e2fa7227 | 405 | struct btrfs_key ins; |
fec577fb | 406 | int ret; |
037e6390 | 407 | struct tree_buffer *buf; |
fec577fb CM |
408 | |
409 | ret = alloc_extent(root, 1, 0, (unsigned long)-1, | |
7518a238 | 410 | btrfs_header_parentid(&root->node->node.header), |
037e6390 | 411 | &ins); |
fec577fb CM |
412 | if (ret) { |
413 | BUG(); | |
414 | return NULL; | |
415 | } | |
037e6390 CM |
416 | buf = find_tree_block(root, ins.objectid); |
417 | dirty_tree_block(root, buf); | |
fec577fb CM |
418 | return buf; |
419 | } | |
a28ec197 | 420 | |
20524f02 CM |
421 | int walk_down_tree(struct ctree_root *root, struct ctree_path *path, int *level) |
422 | { | |
423 | struct tree_buffer *next; | |
424 | struct tree_buffer *cur; | |
425 | u64 blocknr; | |
426 | int ret; | |
427 | u32 refs; | |
428 | ||
429 | ret = lookup_block_ref(root, path->nodes[*level]->blocknr, &refs); | |
430 | BUG_ON(ret); | |
431 | if (refs > 1) | |
432 | goto out; | |
433 | while(*level > 0) { | |
434 | cur = path->nodes[*level]; | |
7518a238 CM |
435 | if (path->slots[*level] >= |
436 | btrfs_header_nritems(&cur->node.header)) | |
20524f02 CM |
437 | break; |
438 | blocknr = cur->node.blockptrs[path->slots[*level]]; | |
439 | ret = lookup_block_ref(root, blocknr, &refs); | |
440 | if (refs != 1 || *level == 1) { | |
441 | path->slots[*level]++; | |
442 | ret = free_extent(root, blocknr, 1); | |
443 | BUG_ON(ret); | |
444 | continue; | |
445 | } | |
446 | BUG_ON(ret); | |
447 | next = read_tree_block(root, blocknr); | |
83e15a28 | 448 | if (path->nodes[*level-1]) |
20524f02 | 449 | tree_block_release(root, path->nodes[*level-1]); |
20524f02 | 450 | path->nodes[*level-1] = next; |
7518a238 | 451 | *level = btrfs_header_level(&next->node.header); |
20524f02 CM |
452 | path->slots[*level] = 0; |
453 | } | |
454 | out: | |
455 | ret = free_extent(root, path->nodes[*level]->blocknr, 1); | |
83e15a28 | 456 | tree_block_release(root, path->nodes[*level]); |
20524f02 CM |
457 | path->nodes[*level] = NULL; |
458 | *level += 1; | |
459 | BUG_ON(ret); | |
460 | return 0; | |
461 | } | |
462 | ||
463 | int walk_up_tree(struct ctree_root *root, struct ctree_path *path, int *level) | |
464 | { | |
465 | int i; | |
466 | int slot; | |
467 | int ret; | |
468 | for(i = *level; i < MAX_LEVEL - 1 && path->nodes[i]; i++) { | |
469 | slot = path->slots[i]; | |
7518a238 CM |
470 | if (slot < |
471 | btrfs_header_nritems(&path->nodes[i]->node.header)- 1) { | |
20524f02 CM |
472 | path->slots[i]++; |
473 | *level = i; | |
474 | return 0; | |
475 | } else { | |
476 | ret = free_extent(root, | |
477 | path->nodes[*level]->blocknr, 1); | |
83e15a28 CM |
478 | tree_block_release(root, path->nodes[*level]); |
479 | path->nodes[*level] = NULL; | |
20524f02 CM |
480 | *level = i + 1; |
481 | BUG_ON(ret); | |
482 | } | |
483 | } | |
484 | return 1; | |
485 | } | |
486 | ||
487 | int btrfs_drop_snapshot(struct ctree_root *root, struct tree_buffer *snap) | |
488 | { | |
489 | int ret; | |
490 | int level; | |
491 | struct ctree_path path; | |
492 | int i; | |
493 | int orig_level; | |
494 | ||
495 | init_path(&path); | |
496 | ||
7518a238 | 497 | level = btrfs_header_level(&snap->node.header); |
20524f02 CM |
498 | orig_level = level; |
499 | path.nodes[level] = snap; | |
500 | path.slots[level] = 0; | |
501 | while(1) { | |
502 | ret = walk_down_tree(root, &path, &level); | |
503 | if (ret > 0) | |
504 | break; | |
505 | ret = walk_up_tree(root, &path, &level); | |
506 | if (ret > 0) | |
507 | break; | |
508 | } | |
83e15a28 CM |
509 | for (i = 0; i <= orig_level; i++) { |
510 | if (path.nodes[i]) { | |
20524f02 | 511 | tree_block_release(root, path.nodes[i]); |
83e15a28 | 512 | } |
20524f02 CM |
513 | } |
514 | ||
515 | return 0; | |
516 | } |