3 #include "kerncompat.h"
4 #include "radix-tree.h"
7 #include "print-tree.h"
8 #include "transaction.h"
10 static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
11 *orig_root, u64 num_blocks, u64 search_start, u64
12 search_end, struct btrfs_key *ins);
13 static int finish_current_insert(struct btrfs_trans_handle *trans, struct
14 btrfs_root *extent_root);
15 static int run_pending(struct btrfs_trans_handle *trans, struct btrfs_root
19 * pending extents are blocks that we're trying to allocate in the extent
20 * map while trying to grow the map because of other allocations. To avoid
21 * recursing, they are tagged in the radix tree and cleaned up after
22 * other allocations are done. The pending tag is also used in the same
25 #define CTREE_EXTENT_PENDING_DEL 0
27 static int inc_block_ref(struct btrfs_trans_handle *trans, struct btrfs_root
30 struct btrfs_path path;
34 struct btrfs_extent_item *item;
38 find_free_extent(trans, root->extent_root, 0, 0, (u64)-1, &ins);
39 btrfs_init_path(&path);
40 key.objectid = blocknr;
42 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
44 ret = btrfs_search_slot(trans, root->extent_root, &key, &path, 0, 1);
48 l = &path.nodes[0]->leaf;
49 item = btrfs_item_ptr(l, path.slots[0], struct btrfs_extent_item);
50 refs = btrfs_extent_refs(item);
51 btrfs_set_extent_refs(item, refs + 1);
53 BUG_ON(list_empty(&path.nodes[0]->dirty));
54 btrfs_release_path(root->extent_root, &path);
55 finish_current_insert(trans, root->extent_root);
56 run_pending(trans, root->extent_root);
60 static int lookup_block_ref(struct btrfs_trans_handle *trans, struct btrfs_root
61 *root, u64 blocknr, u32 *refs)
63 struct btrfs_path path;
67 struct btrfs_extent_item *item;
68 btrfs_init_path(&path);
69 key.objectid = blocknr;
72 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
73 ret = btrfs_search_slot(trans, root->extent_root, &key, &path, 0, 0);
76 l = &path.nodes[0]->leaf;
77 item = btrfs_item_ptr(l, path.slots[0], struct btrfs_extent_item);
78 *refs = btrfs_extent_refs(item);
79 btrfs_release_path(root->extent_root, &path);
83 int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
84 struct btrfs_buffer *buf)
91 if (btrfs_is_leaf(&buf->node))
94 for (i = 0; i < btrfs_header_nritems(&buf->node.header); i++) {
95 blocknr = btrfs_node_blockptr(&buf->node, i);
96 inc_block_ref(trans, root, blocknr);
101 int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, struct
104 unsigned long gang[8];
110 ret = radix_tree_gang_lookup(&root->pinned_radix,
117 for (i = 0; i < ret; i++) {
118 radix_tree_delete(&root->pinned_radix, gang[i]);
121 root->last_insert.objectid = first;
122 root->last_insert.offset = 0;
126 static int finish_current_insert(struct btrfs_trans_handle *trans, struct
127 btrfs_root *extent_root)
129 struct btrfs_key ins;
130 struct btrfs_extent_item extent_item;
134 btrfs_set_extent_refs(&extent_item, 1);
135 btrfs_set_extent_owner(&extent_item,
136 btrfs_header_parentid(&extent_root->node->node.header));
139 btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
141 for (i = 0; i < extent_root->current_insert.flags; i++) {
142 ins.objectid = extent_root->current_insert.objectid + i;
143 ret = btrfs_insert_item(trans, extent_root, &ins, &extent_item,
144 sizeof(extent_item));
147 extent_root->current_insert.offset = 0;
152 * remove an extent from the root, returns 0 on success
154 static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
155 *root, u64 blocknr, u64 num_blocks, int pin)
157 struct btrfs_path path;
158 struct btrfs_key key;
159 struct btrfs_root *extent_root = root->extent_root;
161 struct btrfs_extent_item *ei;
162 struct btrfs_key ins;
165 BUG_ON(pin && num_blocks != 1);
166 key.objectid = blocknr;
168 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
169 key.offset = num_blocks;
171 find_free_extent(trans, root, 0, 0, (u64)-1, &ins);
172 btrfs_init_path(&path);
173 ret = btrfs_search_slot(trans, extent_root, &key, &path, -1, 1);
175 printf("failed to find %Lu\n", key.objectid);
176 btrfs_print_tree(extent_root, extent_root->node);
177 printf("failed to find %Lu\n", key.objectid);
180 ei = btrfs_item_ptr(&path.nodes[0]->leaf, path.slots[0],
181 struct btrfs_extent_item);
182 BUG_ON(ei->refs == 0);
183 refs = btrfs_extent_refs(ei) - 1;
184 btrfs_set_extent_refs(ei, refs);
188 radix_tree_preload(GFP_KERNEL);
189 err = radix_tree_insert(&extent_root->pinned_radix,
190 blocknr, (void *)blocknr);
192 radix_tree_preload_end();
194 ret = btrfs_del_item(trans, extent_root, &path);
195 if (!pin && extent_root->last_insert.objectid > blocknr)
196 extent_root->last_insert.objectid = blocknr;
200 btrfs_release_path(extent_root, &path);
201 finish_current_insert(trans, extent_root);
206 * find all the blocks marked as pending in the radix tree and remove
207 * them from the extent map
209 static int del_pending_extents(struct btrfs_trans_handle *trans, struct
210 btrfs_root *extent_root)
213 struct btrfs_buffer *gang[4];
217 ret = radix_tree_gang_lookup_tag(&extent_root->cache_radix,
220 CTREE_EXTENT_PENDING_DEL);
223 for (i = 0; i < ret; i++) {
224 ret = __free_extent(trans, extent_root,
225 gang[i]->blocknr, 1, 1);
226 radix_tree_tag_clear(&extent_root->cache_radix,
228 CTREE_EXTENT_PENDING_DEL);
229 btrfs_block_release(extent_root, gang[i]);
235 static int run_pending(struct btrfs_trans_handle *trans, struct btrfs_root
238 while(radix_tree_tagged(&extent_root->cache_radix,
239 CTREE_EXTENT_PENDING_DEL))
240 del_pending_extents(trans, extent_root);
246 * remove an extent from the root, returns 0 on success
248 int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
249 *root, u64 blocknr, u64 num_blocks, int pin)
251 struct btrfs_root *extent_root = root->extent_root;
252 struct btrfs_buffer *t;
256 if (root == extent_root) {
257 t = find_tree_block(root, blocknr);
258 radix_tree_tag_set(&root->cache_radix, blocknr,
259 CTREE_EXTENT_PENDING_DEL);
262 ret = __free_extent(trans, root, blocknr, num_blocks, pin);
263 pending_ret = run_pending(trans, root->extent_root);
264 return ret ? ret : pending_ret;
268 * walks the btree of allocated extents and find a hole of a given size.
269 * The key ins is changed to record the hole:
270 * ins->objectid == block start
271 * ins->flags = BTRFS_EXTENT_ITEM_KEY
272 * ins->offset == number of blocks
273 * Any available blocks before search_start are skipped.
275 static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
276 *orig_root, u64 num_blocks, u64 search_start, u64
277 search_end, struct btrfs_key *ins)
279 struct btrfs_path path;
280 struct btrfs_key key;
287 struct btrfs_leaf *l;
288 struct btrfs_root * root = orig_root->extent_root;
289 int total_needed = num_blocks;
291 total_needed += (btrfs_header_level(&root->node->node.header) + 1) * 3;
292 if (root->last_insert.objectid > search_start)
293 search_start = root->last_insert.objectid;
296 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
299 btrfs_init_path(&path);
300 ins->objectid = search_start;
303 ret = btrfs_search_slot(trans, root, ins, &path, 0, 0);
307 if (path.slots[0] > 0)
311 l = &path.nodes[0]->leaf;
312 slot = path.slots[0];
313 if (slot >= btrfs_header_nritems(&l->header)) {
314 ret = btrfs_next_leaf(root, &path);
320 ins->objectid = search_start;
321 ins->offset = (u64)-1;
325 ins->objectid = last_block > search_start ?
326 last_block : search_start;
327 ins->offset = (u64)-1;
330 btrfs_disk_key_to_cpu(&key, &l->items[slot].key);
331 if (key.objectid >= search_start) {
333 if (last_block < search_start)
334 last_block = search_start;
335 hole_size = key.objectid - last_block;
336 if (hole_size > total_needed) {
337 ins->objectid = last_block;
338 ins->offset = hole_size;
344 last_block = key.objectid + key.offset;
349 /* we have to make sure we didn't find an extent that has already
350 * been allocated by the map tree or the original allocation
352 btrfs_release_path(root, &path);
353 BUG_ON(ins->objectid < search_start);
354 for (test_block = ins->objectid;
355 test_block < ins->objectid + total_needed; test_block++) {
356 if (radix_tree_lookup(&root->pinned_radix, test_block)) {
357 search_start = test_block + 1;
361 BUG_ON(root->current_insert.offset);
362 root->current_insert.offset = total_needed - num_blocks;
363 root->current_insert.objectid = ins->objectid + num_blocks;
364 root->current_insert.flags = 0;
365 root->last_insert.objectid = ins->objectid;
366 ins->offset = num_blocks;
369 btrfs_release_path(root, &path);
374 * finds a free extent and does all the dirty work required for allocation
375 * returns the key for the extent through ins, and a tree buffer for
376 * the first block of the extent through buf.
378 * returns 0 if everything worked, non-zero otherwise.
380 static int alloc_extent(struct btrfs_trans_handle *trans, struct btrfs_root
381 *root, u64 num_blocks, u64 search_start, u64
382 search_end, u64 owner, struct btrfs_key *ins)
386 struct btrfs_root *extent_root = root->extent_root;
387 struct btrfs_extent_item extent_item;
389 btrfs_set_extent_refs(&extent_item, 1);
390 btrfs_set_extent_owner(&extent_item, owner);
392 if (root == extent_root) {
393 BUG_ON(extent_root->current_insert.offset == 0);
394 BUG_ON(num_blocks != 1);
395 BUG_ON(extent_root->current_insert.flags ==
396 extent_root->current_insert.offset);
398 ins->objectid = extent_root->current_insert.objectid +
399 extent_root->current_insert.flags++;
402 ret = find_free_extent(trans, root, num_blocks, search_start,
407 ret = btrfs_insert_item(trans, extent_root, ins, &extent_item,
408 sizeof(extent_item));
410 finish_current_insert(trans, extent_root);
411 pending_ret = run_pending(trans, extent_root);
420 * helper function to allocate a block for a given tree
421 * returns the tree buffer or NULL.
423 struct btrfs_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
424 struct btrfs_root *root)
426 struct btrfs_key ins;
428 struct btrfs_buffer *buf;
430 ret = alloc_extent(trans, root, 1, 0, (unsigned long)-1,
431 btrfs_header_parentid(&root->node->node.header),
437 buf = find_tree_block(root, ins.objectid);
438 dirty_tree_block(trans, root, buf);
443 * helper function for drop_snapshot, this walks down the tree dropping ref
446 static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
447 *root, struct btrfs_path *path, int *level)
449 struct btrfs_buffer *next;
450 struct btrfs_buffer *cur;
455 ret = lookup_block_ref(trans, root, path->nodes[*level]->blocknr,
461 * walk down to the last node level and free all the leaves
464 cur = path->nodes[*level];
465 if (path->slots[*level] >=
466 btrfs_header_nritems(&cur->node.header))
468 blocknr = btrfs_node_blockptr(&cur->node, path->slots[*level]);
469 ret = lookup_block_ref(trans, root, blocknr, &refs);
470 if (refs != 1 || *level == 1) {
471 path->slots[*level]++;
472 ret = btrfs_free_extent(trans, root, blocknr, 1, 1);
477 next = read_tree_block(root, blocknr);
478 if (path->nodes[*level-1])
479 btrfs_block_release(root, path->nodes[*level-1]);
480 path->nodes[*level-1] = next;
481 *level = btrfs_header_level(&next->node.header);
482 path->slots[*level] = 0;
485 ret = btrfs_free_extent(trans, root, path->nodes[*level]->blocknr, 1,
487 btrfs_block_release(root, path->nodes[*level]);
488 path->nodes[*level] = NULL;
495 * helper for dropping snapshots. This walks back up the tree in the path
496 * to find the first node higher up where we haven't yet gone through
499 static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root
500 *root, struct btrfs_path *path, int *level)
505 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
506 slot = path->slots[i];
508 btrfs_header_nritems(&path->nodes[i]->node.header)- 1) {
513 ret = btrfs_free_extent(trans, root,
514 path->nodes[*level]->blocknr,
516 btrfs_block_release(root, path->nodes[*level]);
517 path->nodes[*level] = NULL;
526 * drop the reference count on the tree rooted at 'snap'. This traverses
527 * the tree freeing any blocks that have a ref count of zero after being
530 int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
531 *root, struct btrfs_buffer *snap)
536 struct btrfs_path path;
540 btrfs_init_path(&path);
542 level = btrfs_header_level(&snap->node.header);
544 path.nodes[level] = snap;
545 path.slots[level] = 0;
547 wret = walk_down_tree(trans, root, &path, &level);
553 wret = walk_up_tree(trans, root, &path, &level);
559 for (i = 0; i <= orig_level; i++) {
561 btrfs_block_release(root, path.nodes[i]);