3 #include "kerncompat.h"
4 #include "radix-tree.h"
7 #include "print-tree.h"
9 static int find_free_extent(struct ctree_root *orig_root, u64 num_blocks,
10 u64 search_start, u64 search_end, struct key *ins);
11 static int finish_current_insert(struct ctree_root *extent_root);
12 static int run_pending(struct ctree_root *extent_root);
15 * pending extents are blocks that we're trying to allocate in the extent
16 * map while trying to grow the map because of other allocations. To avoid
17 * recursing, they are tagged in the radix tree and cleaned up after
18 * other allocations are done. The pending tag is also used in the same
21 #define CTREE_EXTENT_PENDING_DEL 0
23 static int inc_block_ref(struct ctree_root *root, u64 blocknr)
25 struct ctree_path path;
29 struct extent_item *item;
32 find_free_extent(root->extent_root, 0, 0, (u64)-1, &ins);
34 key.objectid = blocknr;
37 ret = search_slot(root->extent_root, &key, &path, 0, 1);
41 l = &path.nodes[0]->leaf;
42 item = (struct extent_item *)(l->data +
43 l->items[path.slots[0]].offset);
46 BUG_ON(list_empty(&path.nodes[0]->dirty));
47 release_path(root->extent_root, &path);
48 finish_current_insert(root->extent_root);
49 run_pending(root->extent_root);
53 static int lookup_block_ref(struct ctree_root *root, u64 blocknr, u32 *refs)
55 struct ctree_path path;
59 struct extent_item *item;
61 key.objectid = blocknr;
64 ret = search_slot(root->extent_root, &key, &path, 0, 0);
67 l = &path.nodes[0]->leaf;
68 item = (struct extent_item *)(l->data +
69 l->items[path.slots[0]].offset);
71 release_path(root->extent_root, &path);
75 int btrfs_inc_ref(struct ctree_root *root, struct tree_buffer *buf)
80 if (root == root->extent_root)
82 if (is_leaf(buf->node.header.flags))
85 for (i = 0; i < buf->node.header.nritems; i++) {
86 blocknr = buf->node.blockptrs[i];
87 inc_block_ref(root, blocknr);
92 int btrfs_finish_extent_commit(struct ctree_root *root)
94 struct ctree_root *extent_root = root->extent_root;
95 unsigned long gang[8];
100 ret = radix_tree_gang_lookup(&extent_root->pinned_radix,
105 for (i = 0; i < ret; i++) {
106 radix_tree_delete(&extent_root->pinned_radix, gang[i]);
109 extent_root->last_insert.objectid = 0;
110 extent_root->last_insert.offset = 0;
114 static int finish_current_insert(struct ctree_root *extent_root)
117 struct extent_item extent_item;
121 extent_item.refs = 1;
122 extent_item.owner = extent_root->node->node.header.parentid;
126 for (i = 0; i < extent_root->current_insert.flags; i++) {
127 ins.objectid = extent_root->current_insert.objectid + i;
128 ret = insert_item(extent_root, &ins, &extent_item,
129 sizeof(extent_item));
132 extent_root->current_insert.offset = 0;
137 * remove an extent from the root, returns 0 on success
139 int __free_extent(struct ctree_root *root, u64 blocknr, u64 num_blocks)
141 struct ctree_path path;
143 struct ctree_root *extent_root = root->extent_root;
146 struct extent_item *ei;
149 key.objectid = blocknr;
151 key.offset = num_blocks;
153 find_free_extent(root, 0, 0, (u64)-1, &ins);
155 ret = search_slot(extent_root, &key, &path, -1, 1);
157 printf("failed to find %Lu\n", key.objectid);
158 print_tree(extent_root, extent_root->node);
159 printf("failed to find %Lu\n", key.objectid);
162 item = path.nodes[0]->leaf.items + path.slots[0];
163 ei = (struct extent_item *)(path.nodes[0]->leaf.data + item->offset);
164 BUG_ON(ei->refs == 0);
167 if (root == extent_root) {
169 radix_tree_preload(GFP_KERNEL);
170 err = radix_tree_insert(&extent_root->pinned_radix,
171 blocknr, (void *)blocknr);
173 radix_tree_preload_end();
175 ret = del_item(extent_root, &path);
176 if (root != extent_root &&
177 extent_root->last_insert.objectid < blocknr)
178 extent_root->last_insert.objectid = blocknr;
182 release_path(extent_root, &path);
183 finish_current_insert(extent_root);
188 * find all the blocks marked as pending in the radix tree and remove
189 * them from the extent map
191 static int del_pending_extents(struct ctree_root *extent_root)
194 struct tree_buffer *gang[4];
198 ret = radix_tree_gang_lookup_tag(&extent_root->cache_radix,
201 CTREE_EXTENT_PENDING_DEL);
204 for (i = 0; i < ret; i++) {
205 ret = __free_extent(extent_root, gang[i]->blocknr, 1);
206 radix_tree_tag_clear(&extent_root->cache_radix,
208 CTREE_EXTENT_PENDING_DEL);
209 tree_block_release(extent_root, gang[i]);
215 static int run_pending(struct ctree_root *extent_root)
217 while(radix_tree_tagged(&extent_root->cache_radix,
218 CTREE_EXTENT_PENDING_DEL))
219 del_pending_extents(extent_root);
225 * remove an extent from the root, returns 0 on success
227 int free_extent(struct ctree_root *root, u64 blocknr, u64 num_blocks)
230 struct ctree_root *extent_root = root->extent_root;
231 struct tree_buffer *t;
235 if (root == extent_root) {
236 t = find_tree_block(root, blocknr);
237 radix_tree_tag_set(&root->cache_radix, blocknr,
238 CTREE_EXTENT_PENDING_DEL);
241 key.objectid = blocknr;
243 key.offset = num_blocks;
244 ret = __free_extent(root, blocknr, num_blocks);
245 pending_ret = run_pending(root->extent_root);
246 return ret ? ret : pending_ret;
250 * walks the btree of allocated extents and find a hole of a given size.
251 * The key ins is changed to record the hole:
252 * ins->objectid == block start
254 * ins->offset == number of blocks
255 * Any available blocks before search_start are skipped.
257 static int find_free_extent(struct ctree_root *orig_root, u64 num_blocks,
258 u64 search_start, u64 search_end, struct key *ins)
260 struct ctree_path path;
269 struct ctree_root * root = orig_root->extent_root;
270 int total_needed = num_blocks;
272 total_needed += (node_level(root->node->node.header.flags) + 1) * 3;
273 if (root->last_insert.objectid > search_start)
274 search_start = root->last_insert.objectid;
277 ins->objectid = search_start;
281 ret = search_slot(root, ins, &path, 0, 0);
285 if (path.slots[0] > 0)
289 l = &path.nodes[0]->leaf;
290 slot = path.slots[0];
291 if (slot >= l->header.nritems) {
292 ret = next_leaf(root, &path);
298 ins->objectid = search_start;
299 ins->offset = (u64)-1;
303 ins->objectid = last_block > search_start ?
304 last_block : search_start;
305 ins->offset = (u64)-1;
308 key = &l->items[slot].key;
309 if (key->objectid >= search_start) {
311 if (last_block < search_start)
312 last_block = search_start;
313 hole_size = key->objectid - last_block;
314 if (hole_size > total_needed) {
315 ins->objectid = last_block;
316 ins->offset = hole_size;
322 last_block = key->objectid + key->offset;
327 /* we have to make sure we didn't find an extent that has already
328 * been allocated by the map tree or the original allocation
330 release_path(root, &path);
331 BUG_ON(ins->objectid < search_start);
332 for (test_block = ins->objectid;
333 test_block < ins->objectid + total_needed; test_block++) {
334 if (radix_tree_lookup(&root->pinned_radix, test_block)) {
335 search_start = test_block + 1;
339 BUG_ON(root->current_insert.offset);
340 root->current_insert.offset = total_needed - num_blocks;
341 root->current_insert.objectid = ins->objectid + num_blocks;
342 root->current_insert.flags = 0;
343 root->last_insert.objectid = ins->objectid;
344 ins->offset = num_blocks;
347 release_path(root, &path);
352 * finds a free extent and does all the dirty work required for allocation
353 * returns the key for the extent through ins, and a tree buffer for
354 * the first block of the extent through buf.
356 * returns 0 if everything worked, non-zero otherwise.
358 int alloc_extent(struct ctree_root *root, u64 num_blocks, u64 search_start,
359 u64 search_end, u64 owner, struct key *ins)
363 struct ctree_root *extent_root = root->extent_root;
364 struct extent_item extent_item;
366 extent_item.refs = 1;
367 extent_item.owner = owner;
369 if (root == extent_root) {
370 BUG_ON(extent_root->current_insert.offset == 0);
371 BUG_ON(num_blocks != 1);
372 BUG_ON(extent_root->current_insert.flags ==
373 extent_root->current_insert.offset);
375 ins->objectid = extent_root->current_insert.objectid +
376 extent_root->current_insert.flags++;
379 ret = find_free_extent(root, num_blocks, search_start,
384 ret = insert_item(extent_root, ins, &extent_item,
385 sizeof(extent_item));
387 finish_current_insert(extent_root);
388 pending_ret = run_pending(extent_root);
397 * helper function to allocate a block for a given tree
398 * returns the tree buffer or NULL.
400 struct tree_buffer *alloc_free_block(struct ctree_root *root)
404 struct tree_buffer *buf;
406 ret = alloc_extent(root, 1, 0, (unsigned long)-1,
407 root->node->node.header.parentid,
413 buf = find_tree_block(root, ins.objectid);
414 dirty_tree_block(root, buf);
418 int walk_down_tree(struct ctree_root *root, struct ctree_path *path, int *level)
420 struct tree_buffer *next;
421 struct tree_buffer *cur;
426 ret = lookup_block_ref(root, path->nodes[*level]->blocknr, &refs);
431 cur = path->nodes[*level];
432 if (path->slots[*level] >= cur->node.header.nritems)
434 blocknr = cur->node.blockptrs[path->slots[*level]];
435 ret = lookup_block_ref(root, blocknr, &refs);
436 if (refs != 1 || *level == 1) {
437 path->slots[*level]++;
438 ret = free_extent(root, blocknr, 1);
443 next = read_tree_block(root, blocknr);
444 if (path->nodes[*level-1]) {
445 tree_block_release(root, path->nodes[*level-1]);
447 path->nodes[*level-1] = next;
448 *level = node_level(next->node.header.flags);
449 path->slots[*level] = 0;
452 ret = free_extent(root, path->nodes[*level]->blocknr, 1);
453 path->nodes[*level] = NULL;
459 int walk_up_tree(struct ctree_root *root, struct ctree_path *path, int *level)
464 for(i = *level; i < MAX_LEVEL - 1 && path->nodes[i]; i++) {
465 slot = path->slots[i];
466 if (slot < path->nodes[i]->node.header.nritems - 1) {
471 ret = free_extent(root,
472 path->nodes[*level]->blocknr, 1);
480 int btrfs_drop_snapshot(struct ctree_root *root, struct tree_buffer *snap)
484 struct ctree_path path;
490 level = node_level(snap->node.header.flags);
492 path.nodes[level] = snap;
493 path.slots[level] = 0;
495 ret = walk_down_tree(root, &path, &level);
498 ret = walk_up_tree(root, &path, &level);
502 for (i = 0; i < orig_level; i++) {
504 tree_block_release(root, path.nodes[i]);
512 int btrfs_drop_snapshot(struct ctree_root *root, struct tree_buffer *snap)
517 u64 blocknr = snap->blocknr;
519 level = node_level(snap->node.header.flags);
520 ret = lookup_block_ref(root, snap->blocknr, &refs);
522 if (refs == 1 && level != 0) {
523 struct node *n = &snap->node;
524 struct tree_buffer *b;
526 for (i = 0; i < n->header.nritems; i++) {
527 b = read_tree_block(root, n->blockptrs[i]);
528 /* FIXME, don't recurse here */
529 ret = btrfs_drop_snapshot(root, b);
531 tree_block_release(root, b);
534 ret = free_extent(root, blocknr, 1);