3 #include "kerncompat.h"
4 #include "radix-tree.h"
7 #include "print-tree.h"
9 static int find_free_extent(struct btrfs_root
*orig_root
, u64 num_blocks
,
10 u64 search_start
, u64 search_end
,
11 struct btrfs_key
*ins
);
12 static int finish_current_insert(struct btrfs_root
*extent_root
);
13 static int run_pending(struct btrfs_root
*extent_root
);
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
22 #define CTREE_EXTENT_PENDING_DEL 0
24 static int inc_block_ref(struct btrfs_root
*root
, u64 blocknr
)
26 struct btrfs_path path
;
30 struct btrfs_extent_item
*item
;
34 find_free_extent(root
->extent_root
, 0, 0, (u64
)-1, &ins
);
35 btrfs_init_path(&path
);
36 key
.objectid
= blocknr
;
39 ret
= btrfs_search_slot(root
->extent_root
, &key
, &path
, 0, 1);
43 l
= &path
.nodes
[0]->leaf
;
44 item
= btrfs_item_ptr(l
, path
.slots
[0], struct btrfs_extent_item
);
45 refs
= btrfs_extent_refs(item
);
46 btrfs_set_extent_refs(item
, refs
+ 1);
48 BUG_ON(list_empty(&path
.nodes
[0]->dirty
));
49 btrfs_release_path(root
->extent_root
, &path
);
50 finish_current_insert(root
->extent_root
);
51 run_pending(root
->extent_root
);
55 static int lookup_block_ref(struct btrfs_root
*root
, u64 blocknr
, u32
*refs
)
57 struct btrfs_path path
;
61 struct btrfs_extent_item
*item
;
62 btrfs_init_path(&path
);
63 key
.objectid
= blocknr
;
66 ret
= btrfs_search_slot(root
->extent_root
, &key
, &path
, 0, 0);
69 l
= &path
.nodes
[0]->leaf
;
70 item
= btrfs_item_ptr(l
, path
.slots
[0], struct btrfs_extent_item
);
71 *refs
= btrfs_extent_refs(item
);
72 btrfs_release_path(root
->extent_root
, &path
);
76 int btrfs_inc_ref(struct btrfs_root
*root
, struct btrfs_buffer
*buf
)
83 if (btrfs_is_leaf(&buf
->node
))
86 for (i
= 0; i
< btrfs_header_nritems(&buf
->node
.header
); i
++) {
87 blocknr
= btrfs_node_blockptr(&buf
->node
, i
);
88 inc_block_ref(root
, blocknr
);
93 int btrfs_finish_extent_commit(struct btrfs_root
*root
)
95 unsigned long gang
[8];
100 ret
= radix_tree_gang_lookup(&root
->pinned_radix
,
105 for (i
= 0; i
< ret
; i
++) {
106 radix_tree_delete(&root
->pinned_radix
, gang
[i
]);
109 root
->last_insert
.objectid
= 0;
110 root
->last_insert
.offset
= 0;
114 static int finish_current_insert(struct btrfs_root
*extent_root
)
116 struct btrfs_key ins
;
117 struct btrfs_extent_item extent_item
;
121 btrfs_set_extent_refs(&extent_item
, 1);
122 btrfs_set_extent_owner(&extent_item
,
123 btrfs_header_parentid(&extent_root
->node
->node
.header
));
127 for (i
= 0; i
< extent_root
->current_insert
.flags
; i
++) {
128 ins
.objectid
= extent_root
->current_insert
.objectid
+ i
;
129 ret
= btrfs_insert_item(extent_root
, &ins
, &extent_item
,
130 sizeof(extent_item
));
133 extent_root
->current_insert
.offset
= 0;
138 * remove an extent from the root, returns 0 on success
140 static int __free_extent(struct btrfs_root
*root
, u64 blocknr
, u64 num_blocks
)
142 struct btrfs_path path
;
143 struct btrfs_key key
;
144 struct btrfs_root
*extent_root
= root
->extent_root
;
146 struct btrfs_extent_item
*ei
;
147 struct btrfs_key ins
;
150 key
.objectid
= blocknr
;
152 key
.offset
= num_blocks
;
154 find_free_extent(root
, 0, 0, (u64
)-1, &ins
);
155 btrfs_init_path(&path
);
156 ret
= btrfs_search_slot(extent_root
, &key
, &path
, -1, 1);
158 printf("failed to find %Lu\n", key
.objectid
);
159 btrfs_print_tree(extent_root
, extent_root
->node
);
160 printf("failed to find %Lu\n", key
.objectid
);
163 ei
= btrfs_item_ptr(&path
.nodes
[0]->leaf
, path
.slots
[0],
164 struct btrfs_extent_item
);
165 BUG_ON(ei
->refs
== 0);
166 refs
= btrfs_extent_refs(ei
) - 1;
167 btrfs_set_extent_refs(ei
, refs
);
169 if (!root
->ref_cows
) {
171 radix_tree_preload(GFP_KERNEL
);
172 err
= radix_tree_insert(&extent_root
->pinned_radix
,
173 blocknr
, (void *)blocknr
);
175 radix_tree_preload_end();
177 ret
= btrfs_del_item(extent_root
, &path
);
178 if (root
!= extent_root
&&
179 extent_root
->last_insert
.objectid
> blocknr
)
180 extent_root
->last_insert
.objectid
= blocknr
;
184 btrfs_release_path(extent_root
, &path
);
185 finish_current_insert(extent_root
);
190 * find all the blocks marked as pending in the radix tree and remove
191 * them from the extent map
193 static int del_pending_extents(struct btrfs_root
*extent_root
)
196 struct btrfs_buffer
*gang
[4];
200 ret
= radix_tree_gang_lookup_tag(&extent_root
->cache_radix
,
203 CTREE_EXTENT_PENDING_DEL
);
206 for (i
= 0; i
< ret
; i
++) {
207 ret
= __free_extent(extent_root
, gang
[i
]->blocknr
, 1);
208 radix_tree_tag_clear(&extent_root
->cache_radix
,
210 CTREE_EXTENT_PENDING_DEL
);
211 btrfs_block_release(extent_root
, gang
[i
]);
217 static int run_pending(struct btrfs_root
*extent_root
)
219 while(radix_tree_tagged(&extent_root
->cache_radix
,
220 CTREE_EXTENT_PENDING_DEL
))
221 del_pending_extents(extent_root
);
227 * remove an extent from the root, returns 0 on success
229 int btrfs_free_extent(struct btrfs_root
*root
, u64 blocknr
, u64 num_blocks
)
231 struct btrfs_key key
;
232 struct btrfs_root
*extent_root
= root
->extent_root
;
233 struct btrfs_buffer
*t
;
237 if (root
== extent_root
) {
238 t
= find_tree_block(root
, blocknr
);
239 radix_tree_tag_set(&root
->cache_radix
, blocknr
,
240 CTREE_EXTENT_PENDING_DEL
);
243 key
.objectid
= blocknr
;
245 key
.offset
= num_blocks
;
246 ret
= __free_extent(root
, blocknr
, num_blocks
);
247 pending_ret
= run_pending(root
->extent_root
);
248 return ret
? ret
: pending_ret
;
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
256 * ins->offset == number of blocks
257 * Any available blocks before search_start are skipped.
259 static int find_free_extent(struct btrfs_root
*orig_root
, u64 num_blocks
,
260 u64 search_start
, u64 search_end
,
261 struct btrfs_key
*ins
)
263 struct btrfs_path path
;
264 struct btrfs_key key
;
271 struct btrfs_leaf
*l
;
272 struct btrfs_root
* root
= orig_root
->extent_root
;
273 int total_needed
= num_blocks
;
275 total_needed
+= (btrfs_header_level(&root
->node
->node
.header
) + 1) * 3;
276 if (root
->last_insert
.objectid
> search_start
)
277 search_start
= root
->last_insert
.objectid
;
279 btrfs_init_path(&path
);
280 ins
->objectid
= search_start
;
284 ret
= btrfs_search_slot(root
, ins
, &path
, 0, 0);
288 if (path
.slots
[0] > 0)
292 l
= &path
.nodes
[0]->leaf
;
293 slot
= path
.slots
[0];
294 if (slot
>= btrfs_header_nritems(&l
->header
)) {
295 ret
= btrfs_next_leaf(root
, &path
);
301 ins
->objectid
= search_start
;
302 ins
->offset
= (u64
)-1;
306 ins
->objectid
= last_block
> search_start
?
307 last_block
: search_start
;
308 ins
->offset
= (u64
)-1;
311 btrfs_disk_key_to_cpu(&key
, &l
->items
[slot
].key
);
312 if (key
.objectid
>= search_start
) {
314 if (last_block
< search_start
)
315 last_block
= search_start
;
316 hole_size
= key
.objectid
- last_block
;
317 if (hole_size
> total_needed
) {
318 ins
->objectid
= last_block
;
319 ins
->offset
= hole_size
;
325 last_block
= key
.objectid
+ key
.offset
;
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
333 btrfs_release_path(root
, &path
);
334 BUG_ON(ins
->objectid
< search_start
);
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;
342 BUG_ON(root
->current_insert
.offset
);
343 root
->current_insert
.offset
= total_needed
- num_blocks
;
344 root
->current_insert
.objectid
= ins
->objectid
+ num_blocks
;
345 root
->current_insert
.flags
= 0;
346 root
->last_insert
.objectid
= ins
->objectid
;
347 ins
->offset
= num_blocks
;
350 btrfs_release_path(root
, &path
);
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.
359 * returns 0 if everything worked, non-zero otherwise.
361 static int alloc_extent(struct btrfs_root
*root
, u64 num_blocks
,
362 u64 search_start
, u64 search_end
, u64 owner
,
363 struct btrfs_key
*ins
)
367 struct btrfs_root
*extent_root
= root
->extent_root
;
368 struct btrfs_extent_item extent_item
;
370 btrfs_set_extent_refs(&extent_item
, 1);
371 btrfs_set_extent_owner(&extent_item
, owner
);
373 if (root
== extent_root
) {
374 BUG_ON(extent_root
->current_insert
.offset
== 0);
375 BUG_ON(num_blocks
!= 1);
376 BUG_ON(extent_root
->current_insert
.flags
==
377 extent_root
->current_insert
.offset
);
379 ins
->objectid
= extent_root
->current_insert
.objectid
+
380 extent_root
->current_insert
.flags
++;
383 ret
= find_free_extent(root
, num_blocks
, search_start
,
388 ret
= btrfs_insert_item(extent_root
, ins
, &extent_item
,
389 sizeof(extent_item
));
391 finish_current_insert(extent_root
);
392 pending_ret
= run_pending(extent_root
);
401 * helper function to allocate a block for a given tree
402 * returns the tree buffer or NULL.
404 struct btrfs_buffer
*btrfs_alloc_free_block(struct btrfs_root
*root
)
406 struct btrfs_key ins
;
408 struct btrfs_buffer
*buf
;
410 ret
= alloc_extent(root
, 1, 0, (unsigned long)-1,
411 btrfs_header_parentid(&root
->node
->node
.header
),
417 buf
= find_tree_block(root
, ins
.objectid
);
418 dirty_tree_block(root
, buf
);
423 * helper function for drop_snapshot, this walks down the tree dropping ref
426 static int walk_down_tree(struct btrfs_root
*root
,
427 struct btrfs_path
*path
, int *level
)
429 struct btrfs_buffer
*next
;
430 struct btrfs_buffer
*cur
;
435 ret
= lookup_block_ref(root
, path
->nodes
[*level
]->blocknr
, &refs
);
440 * walk down to the last node level and free all the leaves
443 cur
= path
->nodes
[*level
];
444 if (path
->slots
[*level
] >=
445 btrfs_header_nritems(&cur
->node
.header
))
447 blocknr
= btrfs_node_blockptr(&cur
->node
, path
->slots
[*level
]);
448 ret
= lookup_block_ref(root
, blocknr
, &refs
);
449 if (refs
!= 1 || *level
== 1) {
450 path
->slots
[*level
]++;
451 ret
= btrfs_free_extent(root
, blocknr
, 1);
456 next
= read_tree_block(root
, blocknr
);
457 if (path
->nodes
[*level
-1])
458 btrfs_block_release(root
, path
->nodes
[*level
-1]);
459 path
->nodes
[*level
-1] = next
;
460 *level
= btrfs_header_level(&next
->node
.header
);
461 path
->slots
[*level
] = 0;
464 ret
= btrfs_free_extent(root
, path
->nodes
[*level
]->blocknr
, 1);
465 btrfs_block_release(root
, path
->nodes
[*level
]);
466 path
->nodes
[*level
] = NULL
;
473 * helper for dropping snapshots. This walks back up the tree in the path
474 * to find the first node higher up where we haven't yet gone through
477 static int walk_up_tree(struct btrfs_root
*root
, struct btrfs_path
*path
,
483 for(i
= *level
; i
< BTRFS_MAX_LEVEL
- 1 && path
->nodes
[i
]; i
++) {
484 slot
= path
->slots
[i
];
486 btrfs_header_nritems(&path
->nodes
[i
]->node
.header
)- 1) {
491 ret
= btrfs_free_extent(root
,
492 path
->nodes
[*level
]->blocknr
, 1);
493 btrfs_block_release(root
, path
->nodes
[*level
]);
494 path
->nodes
[*level
] = NULL
;
503 * drop the reference count on the tree rooted at 'snap'. This traverses
504 * the tree freeing any blocks that have a ref count of zero after being
507 int btrfs_drop_snapshot(struct btrfs_root
*root
, struct btrfs_buffer
*snap
)
512 struct btrfs_path path
;
516 btrfs_init_path(&path
);
518 level
= btrfs_header_level(&snap
->node
.header
);
520 path
.nodes
[level
] = snap
;
521 path
.slots
[level
] = 0;
523 wret
= walk_down_tree(root
, &path
, &level
);
529 wret
= walk_up_tree(root
, &path
, &level
);
535 for (i
= 0; i
<= orig_level
; i
++) {
537 btrfs_block_release(root
, path
.nodes
[i
]);