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->fs_info->extent_root, 0, 0, (u64)-1,
40 btrfs_init_path(&path);
41 key.objectid = blocknr;
43 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
45 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, &path,
50 l = &path.nodes[0]->leaf;
51 item = btrfs_item_ptr(l, path.slots[0], struct btrfs_extent_item);
52 refs = btrfs_extent_refs(item);
53 btrfs_set_extent_refs(item, refs + 1);
55 BUG_ON(list_empty(&path.nodes[0]->dirty));
56 btrfs_release_path(root->fs_info->extent_root, &path);
57 finish_current_insert(trans, root->fs_info->extent_root);
58 run_pending(trans, root->fs_info->extent_root);
62 static int lookup_block_ref(struct btrfs_trans_handle *trans, struct btrfs_root
63 *root, u64 blocknr, u32 *refs)
65 struct btrfs_path path;
69 struct btrfs_extent_item *item;
70 btrfs_init_path(&path);
71 key.objectid = blocknr;
74 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
75 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, &path,
79 l = &path.nodes[0]->leaf;
80 item = btrfs_item_ptr(l, path.slots[0], struct btrfs_extent_item);
81 *refs = btrfs_extent_refs(item);
82 btrfs_release_path(root->fs_info->extent_root, &path);
86 int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
87 struct btrfs_buffer *buf)
94 if (btrfs_is_leaf(&buf->node))
97 for (i = 0; i < btrfs_header_nritems(&buf->node.header); i++) {
98 blocknr = btrfs_node_blockptr(&buf->node, i);
99 inc_block_ref(trans, root, blocknr);
104 int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, struct
107 unsigned long gang[8];
113 ret = radix_tree_gang_lookup(&root->fs_info->pinned_radix,
120 for (i = 0; i < ret; i++) {
121 radix_tree_delete(&root->fs_info->pinned_radix,
125 root->fs_info->last_insert.objectid = first;
126 root->fs_info->last_insert.offset = 0;
130 static int finish_current_insert(struct btrfs_trans_handle *trans, struct
131 btrfs_root *extent_root)
133 struct btrfs_key ins;
134 struct btrfs_extent_item extent_item;
137 u64 super_blocks_used;
138 struct btrfs_fs_info *info = extent_root->fs_info;
140 btrfs_set_extent_refs(&extent_item, 1);
141 btrfs_set_extent_owner(&extent_item,
142 btrfs_header_parentid(&extent_root->node->node.header));
145 btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
147 for (i = 0; i < extent_root->fs_info->current_insert.flags; i++) {
148 ins.objectid = extent_root->fs_info->current_insert.objectid +
150 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
151 btrfs_set_super_blocks_used(info->disk_super,
152 super_blocks_used + 1);
153 ret = btrfs_insert_item(trans, extent_root, &ins, &extent_item,
154 sizeof(extent_item));
157 extent_root->fs_info->current_insert.offset = 0;
162 * remove an extent from the root, returns 0 on success
164 static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
165 *root, u64 blocknr, u64 num_blocks, int pin)
167 struct btrfs_path path;
168 struct btrfs_key key;
169 struct btrfs_fs_info *info = root->fs_info;
170 struct btrfs_root *extent_root = info->extent_root;
172 struct btrfs_extent_item *ei;
173 struct btrfs_key ins;
176 BUG_ON(pin && num_blocks != 1);
177 key.objectid = blocknr;
179 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
180 key.offset = num_blocks;
182 find_free_extent(trans, root, 0, 0, (u64)-1, &ins);
183 btrfs_init_path(&path);
184 ret = btrfs_search_slot(trans, extent_root, &key, &path, -1, 1);
186 printf("failed to find %Lu\n", key.objectid);
187 btrfs_print_tree(extent_root, extent_root->node);
188 printf("failed to find %Lu\n", key.objectid);
191 ei = btrfs_item_ptr(&path.nodes[0]->leaf, path.slots[0],
192 struct btrfs_extent_item);
193 BUG_ON(ei->refs == 0);
194 refs = btrfs_extent_refs(ei) - 1;
195 btrfs_set_extent_refs(ei, refs);
197 u64 super_blocks_used;
200 radix_tree_preload(GFP_KERNEL);
201 err = radix_tree_insert(&info->pinned_radix,
202 blocknr, (void *)blocknr);
204 radix_tree_preload_end();
206 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
207 btrfs_set_super_blocks_used(info->disk_super,
208 super_blocks_used - num_blocks);
209 ret = btrfs_del_item(trans, extent_root, &path);
210 if (!pin && extent_root->fs_info->last_insert.objectid >
212 extent_root->fs_info->last_insert.objectid = blocknr;
216 btrfs_release_path(extent_root, &path);
217 finish_current_insert(trans, extent_root);
222 * find all the blocks marked as pending in the radix tree and remove
223 * them from the extent map
225 static int del_pending_extents(struct btrfs_trans_handle *trans, struct
226 btrfs_root *extent_root)
229 struct btrfs_buffer *gang[4];
233 ret = radix_tree_gang_lookup_tag(
234 &extent_root->fs_info->cache_radix,
237 CTREE_EXTENT_PENDING_DEL);
240 for (i = 0; i < ret; i++) {
241 ret = __free_extent(trans, extent_root,
242 gang[i]->blocknr, 1, 1);
243 radix_tree_tag_clear(&extent_root->fs_info->cache_radix,
245 CTREE_EXTENT_PENDING_DEL);
246 btrfs_block_release(extent_root, gang[i]);
252 static int run_pending(struct btrfs_trans_handle *trans, struct btrfs_root
255 while(radix_tree_tagged(&extent_root->fs_info->cache_radix,
256 CTREE_EXTENT_PENDING_DEL))
257 del_pending_extents(trans, extent_root);
263 * remove an extent from the root, returns 0 on success
265 int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
266 *root, u64 blocknr, u64 num_blocks, int pin)
268 struct btrfs_root *extent_root = root->fs_info->extent_root;
269 struct btrfs_buffer *t;
273 if (root == extent_root) {
274 t = find_tree_block(root, blocknr);
275 radix_tree_tag_set(&root->fs_info->cache_radix, blocknr,
276 CTREE_EXTENT_PENDING_DEL);
279 ret = __free_extent(trans, root, blocknr, num_blocks, pin);
280 pending_ret = run_pending(trans, root->fs_info->extent_root);
281 return ret ? ret : pending_ret;
285 * walks the btree of allocated extents and find a hole of a given size.
286 * The key ins is changed to record the hole:
287 * ins->objectid == block start
288 * ins->flags = BTRFS_EXTENT_ITEM_KEY
289 * ins->offset == number of blocks
290 * Any available blocks before search_start are skipped.
292 static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
293 *orig_root, u64 num_blocks, u64 search_start, u64
294 search_end, struct btrfs_key *ins)
296 struct btrfs_path path;
297 struct btrfs_key key;
304 struct btrfs_leaf *l;
305 struct btrfs_root * root = orig_root->fs_info->extent_root;
306 int total_needed = num_blocks;
308 total_needed += (btrfs_header_level(&root->node->node.header) + 1) * 3;
309 if (root->fs_info->last_insert.objectid > search_start)
310 search_start = root->fs_info->last_insert.objectid;
313 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
316 btrfs_init_path(&path);
317 ins->objectid = search_start;
320 ret = btrfs_search_slot(trans, root, ins, &path, 0, 0);
324 if (path.slots[0] > 0)
328 l = &path.nodes[0]->leaf;
329 slot = path.slots[0];
330 if (slot >= btrfs_header_nritems(&l->header)) {
331 ret = btrfs_next_leaf(root, &path);
337 ins->objectid = search_start;
338 ins->offset = (u64)-1;
342 ins->objectid = last_block > search_start ?
343 last_block : search_start;
344 ins->offset = (u64)-1;
347 btrfs_disk_key_to_cpu(&key, &l->items[slot].key);
348 if (key.objectid >= search_start) {
350 if (last_block < search_start)
351 last_block = search_start;
352 hole_size = key.objectid - last_block;
353 if (hole_size > total_needed) {
354 ins->objectid = last_block;
355 ins->offset = hole_size;
361 last_block = key.objectid + key.offset;
366 /* we have to make sure we didn't find an extent that has already
367 * been allocated by the map tree or the original allocation
369 btrfs_release_path(root, &path);
370 BUG_ON(ins->objectid < search_start);
371 for (test_block = ins->objectid;
372 test_block < ins->objectid + total_needed; test_block++) {
373 if (radix_tree_lookup(&root->fs_info->pinned_radix,
375 search_start = test_block + 1;
379 BUG_ON(root->fs_info->current_insert.offset);
380 root->fs_info->current_insert.offset = total_needed - num_blocks;
381 root->fs_info->current_insert.objectid = ins->objectid + num_blocks;
382 root->fs_info->current_insert.flags = 0;
383 root->fs_info->last_insert.objectid = ins->objectid;
384 ins->offset = num_blocks;
387 btrfs_release_path(root, &path);
392 * finds a free extent and does all the dirty work required for allocation
393 * returns the key for the extent through ins, and a tree buffer for
394 * the first block of the extent through buf.
396 * returns 0 if everything worked, non-zero otherwise.
398 static int alloc_extent(struct btrfs_trans_handle *trans, struct btrfs_root
399 *root, u64 num_blocks, u64 search_start, u64
400 search_end, u64 owner, struct btrfs_key *ins)
404 u64 super_blocks_used;
405 struct btrfs_fs_info *info = root->fs_info;
406 struct btrfs_root *extent_root = info->extent_root;
407 struct btrfs_extent_item extent_item;
409 btrfs_set_extent_refs(&extent_item, 1);
410 btrfs_set_extent_owner(&extent_item, owner);
412 if (root == extent_root) {
413 BUG_ON(extent_root->fs_info->current_insert.offset == 0);
414 BUG_ON(num_blocks != 1);
415 BUG_ON(extent_root->fs_info->current_insert.flags ==
416 extent_root->fs_info->current_insert.offset);
418 ins->objectid = extent_root->fs_info->current_insert.objectid +
419 extent_root->fs_info->current_insert.flags++;
422 ret = find_free_extent(trans, root, num_blocks, search_start,
427 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
428 btrfs_set_super_blocks_used(info->disk_super, super_blocks_used +
430 ret = btrfs_insert_item(trans, extent_root, ins, &extent_item,
431 sizeof(extent_item));
433 finish_current_insert(trans, extent_root);
434 pending_ret = run_pending(trans, extent_root);
443 * helper function to allocate a block for a given tree
444 * returns the tree buffer or NULL.
446 struct btrfs_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
447 struct btrfs_root *root)
449 struct btrfs_key ins;
451 struct btrfs_buffer *buf;
453 ret = alloc_extent(trans, root, 1, 0, (unsigned long)-1,
454 btrfs_header_parentid(&root->node->node.header),
460 buf = find_tree_block(root, ins.objectid);
461 dirty_tree_block(trans, root, buf);
462 btrfs_set_header_generation(&buf->node.header,
463 root->root_key.offset + 1);
464 memcpy(buf->node.header.fsid, root->fs_info->disk_super->fsid,
465 sizeof(buf->node.header.fsid));
470 * helper function for drop_snapshot, this walks down the tree dropping ref
473 static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
474 *root, struct btrfs_path *path, int *level)
476 struct btrfs_buffer *next;
477 struct btrfs_buffer *cur;
482 ret = lookup_block_ref(trans, root, path->nodes[*level]->blocknr,
488 * walk down to the last node level and free all the leaves
491 cur = path->nodes[*level];
492 if (path->slots[*level] >=
493 btrfs_header_nritems(&cur->node.header))
495 blocknr = btrfs_node_blockptr(&cur->node, path->slots[*level]);
496 ret = lookup_block_ref(trans, root, blocknr, &refs);
497 if (refs != 1 || *level == 1) {
498 path->slots[*level]++;
499 ret = btrfs_free_extent(trans, root, blocknr, 1, 1);
504 next = read_tree_block(root, blocknr);
505 if (path->nodes[*level-1])
506 btrfs_block_release(root, path->nodes[*level-1]);
507 path->nodes[*level-1] = next;
508 *level = btrfs_header_level(&next->node.header);
509 path->slots[*level] = 0;
512 ret = btrfs_free_extent(trans, root, path->nodes[*level]->blocknr, 1,
514 btrfs_block_release(root, path->nodes[*level]);
515 path->nodes[*level] = NULL;
522 * helper for dropping snapshots. This walks back up the tree in the path
523 * to find the first node higher up where we haven't yet gone through
526 static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root
527 *root, struct btrfs_path *path, int *level)
532 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
533 slot = path->slots[i];
535 btrfs_header_nritems(&path->nodes[i]->node.header)- 1) {
540 ret = btrfs_free_extent(trans, root,
541 path->nodes[*level]->blocknr,
543 btrfs_block_release(root, path->nodes[*level]);
544 path->nodes[*level] = NULL;
553 * drop the reference count on the tree rooted at 'snap'. This traverses
554 * the tree freeing any blocks that have a ref count of zero after being
557 int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
558 *root, struct btrfs_buffer *snap)
563 struct btrfs_path path;
567 btrfs_init_path(&path);
569 level = btrfs_header_level(&snap->node.header);
571 path.nodes[level] = snap;
572 path.slots[level] = 0;
574 wret = walk_down_tree(trans, root, &path, &level);
580 wret = walk_up_tree(trans, root, &path, &level);
586 for (i = 0; i <= orig_level; i++) {
588 btrfs_block_release(root, path.nodes[i]);