]>
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" | |
e089f05c | 8 | #include "transaction.h" |
fec577fb | 9 | |
e089f05c CM |
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 | |
16 | *extent_root); | |
037e6390 | 17 | |
fec577fb CM |
18 | /* |
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 | |
23 | * manner for deletes. | |
24 | */ | |
037e6390 | 25 | #define CTREE_EXTENT_PENDING_DEL 0 |
fec577fb | 26 | |
e089f05c CM |
27 | static int inc_block_ref(struct btrfs_trans_handle *trans, struct btrfs_root |
28 | *root, u64 blocknr) | |
02217ed2 | 29 | { |
234b63a0 | 30 | struct btrfs_path path; |
02217ed2 | 31 | int ret; |
e2fa7227 | 32 | struct btrfs_key key; |
234b63a0 CM |
33 | struct btrfs_leaf *l; |
34 | struct btrfs_extent_item *item; | |
e2fa7227 | 35 | struct btrfs_key ins; |
cf27e1ee | 36 | u32 refs; |
037e6390 | 37 | |
9f5fae2f CM |
38 | find_free_extent(trans, root->fs_info->extent_root, 0, 0, (u64)-1, |
39 | &ins); | |
234b63a0 | 40 | btrfs_init_path(&path); |
02217ed2 CM |
41 | key.objectid = blocknr; |
42 | key.flags = 0; | |
62e2749e | 43 | btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY); |
02217ed2 | 44 | key.offset = 1; |
9f5fae2f CM |
45 | ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, &path, |
46 | 0, 1); | |
a28ec197 CM |
47 | if (ret != 0) |
48 | BUG(); | |
02217ed2 CM |
49 | BUG_ON(ret != 0); |
50 | l = &path.nodes[0]->leaf; | |
4beb1b8b | 51 | item = btrfs_item_ptr(l, path.slots[0], struct btrfs_extent_item); |
cf27e1ee CM |
52 | refs = btrfs_extent_refs(item); |
53 | btrfs_set_extent_refs(item, refs + 1); | |
a28ec197 | 54 | |
02217ed2 | 55 | BUG_ON(list_empty(&path.nodes[0]->dirty)); |
9f5fae2f CM |
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); | |
02217ed2 CM |
59 | return 0; |
60 | } | |
61 | ||
e089f05c CM |
62 | static int lookup_block_ref(struct btrfs_trans_handle *trans, struct btrfs_root |
63 | *root, u64 blocknr, u32 *refs) | |
a28ec197 | 64 | { |
234b63a0 | 65 | struct btrfs_path path; |
a28ec197 | 66 | int ret; |
e2fa7227 | 67 | struct btrfs_key key; |
234b63a0 CM |
68 | struct btrfs_leaf *l; |
69 | struct btrfs_extent_item *item; | |
70 | btrfs_init_path(&path); | |
a28ec197 | 71 | key.objectid = blocknr; |
a28ec197 | 72 | key.offset = 1; |
62e2749e CM |
73 | key.flags = 0; |
74 | btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY); | |
9f5fae2f CM |
75 | ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, &path, |
76 | 0, 0); | |
a28ec197 CM |
77 | if (ret != 0) |
78 | BUG(); | |
79 | l = &path.nodes[0]->leaf; | |
4beb1b8b | 80 | item = btrfs_item_ptr(l, path.slots[0], struct btrfs_extent_item); |
cf27e1ee | 81 | *refs = btrfs_extent_refs(item); |
9f5fae2f | 82 | btrfs_release_path(root->fs_info->extent_root, &path); |
a28ec197 CM |
83 | return 0; |
84 | } | |
85 | ||
e089f05c CM |
86 | int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
87 | struct btrfs_buffer *buf) | |
02217ed2 CM |
88 | { |
89 | u64 blocknr; | |
90 | int i; | |
a28ec197 | 91 | |
3768f368 | 92 | if (!root->ref_cows) |
a28ec197 | 93 | return 0; |
7518a238 | 94 | if (btrfs_is_leaf(&buf->node)) |
a28ec197 CM |
95 | return 0; |
96 | ||
7518a238 | 97 | for (i = 0; i < btrfs_header_nritems(&buf->node.header); i++) { |
1d4f8a0c | 98 | blocknr = btrfs_node_blockptr(&buf->node, i); |
e089f05c | 99 | inc_block_ref(trans, root, blocknr); |
02217ed2 CM |
100 | } |
101 | return 0; | |
102 | } | |
103 | ||
e089f05c CM |
104 | int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, struct |
105 | btrfs_root *root) | |
a28ec197 | 106 | { |
a28ec197 | 107 | unsigned long gang[8]; |
88fd146c | 108 | u64 first = 0; |
a28ec197 CM |
109 | int ret; |
110 | int i; | |
111 | ||
112 | while(1) { | |
9f5fae2f CM |
113 | ret = radix_tree_gang_lookup(&root->fs_info->pinned_radix, |
114 | (void **)gang, 0, | |
115 | ARRAY_SIZE(gang)); | |
a28ec197 CM |
116 | if (!ret) |
117 | break; | |
88fd146c CM |
118 | if (!first) |
119 | first = gang[0]; | |
0579da42 | 120 | for (i = 0; i < ret; i++) { |
9f5fae2f CM |
121 | radix_tree_delete(&root->fs_info->pinned_radix, |
122 | gang[i]); | |
0579da42 | 123 | } |
a28ec197 | 124 | } |
9f5fae2f CM |
125 | root->fs_info->last_insert.objectid = first; |
126 | root->fs_info->last_insert.offset = 0; | |
a28ec197 CM |
127 | return 0; |
128 | } | |
129 | ||
e089f05c CM |
130 | static int finish_current_insert(struct btrfs_trans_handle *trans, struct |
131 | btrfs_root *extent_root) | |
037e6390 | 132 | { |
e2fa7227 | 133 | struct btrfs_key ins; |
234b63a0 | 134 | struct btrfs_extent_item extent_item; |
037e6390 CM |
135 | int i; |
136 | int ret; | |
1261ec42 CM |
137 | u64 super_blocks_used; |
138 | struct btrfs_fs_info *info = extent_root->fs_info; | |
037e6390 | 139 | |
cf27e1ee CM |
140 | btrfs_set_extent_refs(&extent_item, 1); |
141 | btrfs_set_extent_owner(&extent_item, | |
142 | btrfs_header_parentid(&extent_root->node->node.header)); | |
037e6390 CM |
143 | ins.offset = 1; |
144 | ins.flags = 0; | |
62e2749e | 145 | btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY); |
037e6390 | 146 | |
9f5fae2f CM |
147 | for (i = 0; i < extent_root->fs_info->current_insert.flags; i++) { |
148 | ins.objectid = extent_root->fs_info->current_insert.objectid + | |
149 | i; | |
1261ec42 CM |
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); | |
e089f05c CM |
153 | ret = btrfs_insert_item(trans, extent_root, &ins, &extent_item, |
154 | sizeof(extent_item)); | |
037e6390 CM |
155 | BUG_ON(ret); |
156 | } | |
9f5fae2f | 157 | extent_root->fs_info->current_insert.offset = 0; |
037e6390 CM |
158 | return 0; |
159 | } | |
160 | ||
fec577fb | 161 | /* |
a28ec197 | 162 | * remove an extent from the root, returns 0 on success |
fec577fb | 163 | */ |
e089f05c CM |
164 | static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root |
165 | *root, u64 blocknr, u64 num_blocks, int pin) | |
a28ec197 | 166 | { |
234b63a0 | 167 | struct btrfs_path path; |
e2fa7227 | 168 | struct btrfs_key key; |
1261ec42 CM |
169 | struct btrfs_fs_info *info = root->fs_info; |
170 | struct btrfs_root *extent_root = info->extent_root; | |
a28ec197 | 171 | int ret; |
234b63a0 | 172 | struct btrfs_extent_item *ei; |
e2fa7227 | 173 | struct btrfs_key ins; |
cf27e1ee | 174 | u32 refs; |
037e6390 | 175 | |
88fd146c | 176 | BUG_ON(pin && num_blocks != 1); |
a28ec197 CM |
177 | key.objectid = blocknr; |
178 | key.flags = 0; | |
62e2749e | 179 | btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY); |
a28ec197 CM |
180 | key.offset = num_blocks; |
181 | ||
e089f05c | 182 | find_free_extent(trans, root, 0, 0, (u64)-1, &ins); |
234b63a0 | 183 | btrfs_init_path(&path); |
e089f05c | 184 | ret = btrfs_search_slot(trans, extent_root, &key, &path, -1, 1); |
a28ec197 CM |
185 | if (ret) { |
186 | printf("failed to find %Lu\n", key.objectid); | |
234b63a0 | 187 | btrfs_print_tree(extent_root, extent_root->node); |
a28ec197 CM |
188 | printf("failed to find %Lu\n", key.objectid); |
189 | BUG(); | |
190 | } | |
123abc88 CM |
191 | ei = btrfs_item_ptr(&path.nodes[0]->leaf, path.slots[0], |
192 | struct btrfs_extent_item); | |
a28ec197 | 193 | BUG_ON(ei->refs == 0); |
cf27e1ee CM |
194 | refs = btrfs_extent_refs(ei) - 1; |
195 | btrfs_set_extent_refs(ei, refs); | |
196 | if (refs == 0) { | |
1261ec42 | 197 | u64 super_blocks_used; |
88fd146c | 198 | if (pin) { |
a28ec197 CM |
199 | int err; |
200 | radix_tree_preload(GFP_KERNEL); | |
1261ec42 CM |
201 | err = radix_tree_insert(&info->pinned_radix, |
202 | blocknr, (void *)blocknr); | |
a28ec197 CM |
203 | BUG_ON(err); |
204 | radix_tree_preload_end(); | |
205 | } | |
1261ec42 CM |
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); | |
e089f05c | 209 | ret = btrfs_del_item(trans, extent_root, &path); |
9f5fae2f CM |
210 | if (!pin && extent_root->fs_info->last_insert.objectid > |
211 | blocknr) | |
212 | extent_root->fs_info->last_insert.objectid = blocknr; | |
a28ec197 CM |
213 | if (ret) |
214 | BUG(); | |
215 | } | |
234b63a0 | 216 | btrfs_release_path(extent_root, &path); |
e089f05c | 217 | finish_current_insert(trans, extent_root); |
a28ec197 CM |
218 | return ret; |
219 | } | |
220 | ||
a28ec197 CM |
221 | /* |
222 | * find all the blocks marked as pending in the radix tree and remove | |
223 | * them from the extent map | |
224 | */ | |
e089f05c CM |
225 | static int del_pending_extents(struct btrfs_trans_handle *trans, struct |
226 | btrfs_root *extent_root) | |
a28ec197 CM |
227 | { |
228 | int ret; | |
234b63a0 | 229 | struct btrfs_buffer *gang[4]; |
a28ec197 CM |
230 | int i; |
231 | ||
232 | while(1) { | |
9f5fae2f CM |
233 | ret = radix_tree_gang_lookup_tag( |
234 | &extent_root->fs_info->cache_radix, | |
235 | (void **)gang, 0, | |
236 | ARRAY_SIZE(gang), | |
237 | CTREE_EXTENT_PENDING_DEL); | |
a28ec197 CM |
238 | if (!ret) |
239 | break; | |
240 | for (i = 0; i < ret; i++) { | |
e089f05c | 241 | ret = __free_extent(trans, extent_root, |
88fd146c | 242 | gang[i]->blocknr, 1, 1); |
9f5fae2f CM |
243 | radix_tree_tag_clear(&extent_root->fs_info->cache_radix, |
244 | gang[i]->blocknr, | |
245 | CTREE_EXTENT_PENDING_DEL); | |
234b63a0 | 246 | btrfs_block_release(extent_root, gang[i]); |
fec577fb CM |
247 | } |
248 | } | |
249 | return 0; | |
250 | } | |
251 | ||
e089f05c CM |
252 | static int run_pending(struct btrfs_trans_handle *trans, struct btrfs_root |
253 | *extent_root) | |
a28ec197 | 254 | { |
9f5fae2f CM |
255 | while(radix_tree_tagged(&extent_root->fs_info->cache_radix, |
256 | CTREE_EXTENT_PENDING_DEL)) | |
e089f05c | 257 | del_pending_extents(trans, extent_root); |
a28ec197 CM |
258 | return 0; |
259 | } | |
260 | ||
261 | ||
fec577fb CM |
262 | /* |
263 | * remove an extent from the root, returns 0 on success | |
264 | */ | |
e089f05c CM |
265 | int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root |
266 | *root, u64 blocknr, u64 num_blocks, int pin) | |
fec577fb | 267 | { |
9f5fae2f | 268 | struct btrfs_root *extent_root = root->fs_info->extent_root; |
234b63a0 | 269 | struct btrfs_buffer *t; |
fec577fb CM |
270 | int pending_ret; |
271 | int ret; | |
a28ec197 | 272 | |
fec577fb | 273 | if (root == extent_root) { |
a28ec197 | 274 | t = find_tree_block(root, blocknr); |
9f5fae2f | 275 | radix_tree_tag_set(&root->fs_info->cache_radix, blocknr, |
a28ec197 | 276 | CTREE_EXTENT_PENDING_DEL); |
fec577fb CM |
277 | return 0; |
278 | } | |
e089f05c | 279 | ret = __free_extent(trans, root, blocknr, num_blocks, pin); |
9f5fae2f | 280 | pending_ret = run_pending(trans, root->fs_info->extent_root); |
fec577fb CM |
281 | return ret ? ret : pending_ret; |
282 | } | |
283 | ||
284 | /* | |
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 | |
62e2749e | 288 | * ins->flags = BTRFS_EXTENT_ITEM_KEY |
fec577fb CM |
289 | * ins->offset == number of blocks |
290 | * Any available blocks before search_start are skipped. | |
291 | */ | |
e089f05c CM |
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) | |
fec577fb | 295 | { |
234b63a0 | 296 | struct btrfs_path path; |
e2fa7227 | 297 | struct btrfs_key key; |
fec577fb CM |
298 | int ret; |
299 | u64 hole_size = 0; | |
300 | int slot = 0; | |
301 | u64 last_block; | |
037e6390 | 302 | u64 test_block; |
fec577fb | 303 | int start_found; |
234b63a0 | 304 | struct btrfs_leaf *l; |
9f5fae2f | 305 | struct btrfs_root * root = orig_root->fs_info->extent_root; |
0579da42 | 306 | int total_needed = num_blocks; |
fec577fb | 307 | |
7518a238 | 308 | total_needed += (btrfs_header_level(&root->node->node.header) + 1) * 3; |
9f5fae2f CM |
309 | if (root->fs_info->last_insert.objectid > search_start) |
310 | search_start = root->fs_info->last_insert.objectid; | |
62e2749e CM |
311 | |
312 | ins->flags = 0; | |
313 | btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY); | |
314 | ||
fec577fb | 315 | check_failed: |
234b63a0 | 316 | btrfs_init_path(&path); |
fec577fb CM |
317 | ins->objectid = search_start; |
318 | ins->offset = 0; | |
fec577fb | 319 | start_found = 0; |
e089f05c | 320 | ret = btrfs_search_slot(trans, root, ins, &path, 0, 0); |
0f70abe2 CM |
321 | if (ret < 0) |
322 | goto error; | |
aa5d6bed | 323 | |
0579da42 CM |
324 | if (path.slots[0] > 0) |
325 | path.slots[0]--; | |
326 | ||
fec577fb CM |
327 | while (1) { |
328 | l = &path.nodes[0]->leaf; | |
329 | slot = path.slots[0]; | |
7518a238 | 330 | if (slot >= btrfs_header_nritems(&l->header)) { |
234b63a0 | 331 | ret = btrfs_next_leaf(root, &path); |
fec577fb CM |
332 | if (ret == 0) |
333 | continue; | |
0f70abe2 CM |
334 | if (ret < 0) |
335 | goto error; | |
fec577fb CM |
336 | if (!start_found) { |
337 | ins->objectid = search_start; | |
037e6390 | 338 | ins->offset = (u64)-1; |
fec577fb CM |
339 | start_found = 1; |
340 | goto check_pending; | |
341 | } | |
342 | ins->objectid = last_block > search_start ? | |
343 | last_block : search_start; | |
037e6390 | 344 | ins->offset = (u64)-1; |
fec577fb CM |
345 | goto check_pending; |
346 | } | |
e2fa7227 CM |
347 | btrfs_disk_key_to_cpu(&key, &l->items[slot].key); |
348 | if (key.objectid >= search_start) { | |
fec577fb | 349 | if (start_found) { |
0579da42 CM |
350 | if (last_block < search_start) |
351 | last_block = search_start; | |
e2fa7227 | 352 | hole_size = key.objectid - last_block; |
037e6390 | 353 | if (hole_size > total_needed) { |
fec577fb | 354 | ins->objectid = last_block; |
037e6390 | 355 | ins->offset = hole_size; |
fec577fb CM |
356 | goto check_pending; |
357 | } | |
0579da42 | 358 | } |
fec577fb | 359 | } |
0579da42 | 360 | start_found = 1; |
e2fa7227 | 361 | last_block = key.objectid + key.offset; |
fec577fb CM |
362 | path.slots[0]++; |
363 | } | |
364 | // FIXME -ENOSPC | |
365 | check_pending: | |
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 | |
368 | */ | |
234b63a0 | 369 | btrfs_release_path(root, &path); |
fec577fb | 370 | BUG_ON(ins->objectid < search_start); |
037e6390 CM |
371 | for (test_block = ins->objectid; |
372 | test_block < ins->objectid + total_needed; test_block++) { | |
9f5fae2f CM |
373 | if (radix_tree_lookup(&root->fs_info->pinned_radix, |
374 | test_block)) { | |
037e6390 | 375 | search_start = test_block + 1; |
fec577fb CM |
376 | goto check_failed; |
377 | } | |
378 | } | |
9f5fae2f CM |
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; | |
037e6390 | 384 | ins->offset = num_blocks; |
fec577fb | 385 | return 0; |
0f70abe2 | 386 | error: |
234b63a0 | 387 | btrfs_release_path(root, &path); |
0f70abe2 | 388 | return ret; |
fec577fb CM |
389 | } |
390 | ||
fec577fb CM |
391 | /* |
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. | |
395 | * | |
396 | * returns 0 if everything worked, non-zero otherwise. | |
397 | */ | |
e089f05c CM |
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) | |
fec577fb CM |
401 | { |
402 | int ret; | |
403 | int pending_ret; | |
1261ec42 CM |
404 | u64 super_blocks_used; |
405 | struct btrfs_fs_info *info = root->fs_info; | |
406 | struct btrfs_root *extent_root = info->extent_root; | |
234b63a0 | 407 | struct btrfs_extent_item extent_item; |
037e6390 | 408 | |
cf27e1ee CM |
409 | btrfs_set_extent_refs(&extent_item, 1); |
410 | btrfs_set_extent_owner(&extent_item, owner); | |
fec577fb | 411 | |
037e6390 | 412 | if (root == extent_root) { |
9f5fae2f | 413 | BUG_ON(extent_root->fs_info->current_insert.offset == 0); |
037e6390 | 414 | BUG_ON(num_blocks != 1); |
9f5fae2f CM |
415 | BUG_ON(extent_root->fs_info->current_insert.flags == |
416 | extent_root->fs_info->current_insert.offset); | |
037e6390 | 417 | ins->offset = 1; |
9f5fae2f CM |
418 | ins->objectid = extent_root->fs_info->current_insert.objectid + |
419 | extent_root->fs_info->current_insert.flags++; | |
fec577fb CM |
420 | return 0; |
421 | } | |
e089f05c | 422 | ret = find_free_extent(trans, root, num_blocks, search_start, |
037e6390 CM |
423 | search_end, ins); |
424 | if (ret) | |
425 | return ret; | |
fec577fb | 426 | |
1261ec42 CM |
427 | super_blocks_used = btrfs_super_blocks_used(info->disk_super); |
428 | btrfs_set_super_blocks_used(info->disk_super, super_blocks_used + | |
429 | num_blocks); | |
e089f05c CM |
430 | ret = btrfs_insert_item(trans, extent_root, ins, &extent_item, |
431 | sizeof(extent_item)); | |
037e6390 | 432 | |
e089f05c CM |
433 | finish_current_insert(trans, extent_root); |
434 | pending_ret = run_pending(trans, extent_root); | |
037e6390 CM |
435 | if (ret) |
436 | return ret; | |
437 | if (pending_ret) | |
438 | return pending_ret; | |
439 | return 0; | |
fec577fb CM |
440 | } |
441 | ||
442 | /* | |
443 | * helper function to allocate a block for a given tree | |
444 | * returns the tree buffer or NULL. | |
445 | */ | |
e089f05c CM |
446 | struct btrfs_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans, |
447 | struct btrfs_root *root) | |
fec577fb | 448 | { |
e2fa7227 | 449 | struct btrfs_key ins; |
fec577fb | 450 | int ret; |
234b63a0 | 451 | struct btrfs_buffer *buf; |
fec577fb | 452 | |
e089f05c | 453 | ret = alloc_extent(trans, root, 1, 0, (unsigned long)-1, |
7518a238 | 454 | btrfs_header_parentid(&root->node->node.header), |
037e6390 | 455 | &ins); |
fec577fb CM |
456 | if (ret) { |
457 | BUG(); | |
458 | return NULL; | |
459 | } | |
037e6390 | 460 | buf = find_tree_block(root, ins.objectid); |
e089f05c | 461 | dirty_tree_block(trans, root, buf); |
fec577fb CM |
462 | return buf; |
463 | } | |
a28ec197 | 464 | |
9aca1d51 CM |
465 | /* |
466 | * helper function for drop_snapshot, this walks down the tree dropping ref | |
467 | * counts as it goes. | |
468 | */ | |
e089f05c CM |
469 | static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root |
470 | *root, struct btrfs_path *path, int *level) | |
20524f02 | 471 | { |
234b63a0 CM |
472 | struct btrfs_buffer *next; |
473 | struct btrfs_buffer *cur; | |
20524f02 CM |
474 | u64 blocknr; |
475 | int ret; | |
476 | u32 refs; | |
477 | ||
e089f05c CM |
478 | ret = lookup_block_ref(trans, root, path->nodes[*level]->blocknr, |
479 | &refs); | |
20524f02 CM |
480 | BUG_ON(ret); |
481 | if (refs > 1) | |
482 | goto out; | |
9aca1d51 CM |
483 | /* |
484 | * walk down to the last node level and free all the leaves | |
485 | */ | |
20524f02 CM |
486 | while(*level > 0) { |
487 | cur = path->nodes[*level]; | |
7518a238 CM |
488 | if (path->slots[*level] >= |
489 | btrfs_header_nritems(&cur->node.header)) | |
20524f02 | 490 | break; |
1d4f8a0c | 491 | blocknr = btrfs_node_blockptr(&cur->node, path->slots[*level]); |
e089f05c | 492 | ret = lookup_block_ref(trans, root, blocknr, &refs); |
20524f02 CM |
493 | if (refs != 1 || *level == 1) { |
494 | path->slots[*level]++; | |
e089f05c | 495 | ret = btrfs_free_extent(trans, root, blocknr, 1, 1); |
20524f02 CM |
496 | BUG_ON(ret); |
497 | continue; | |
498 | } | |
499 | BUG_ON(ret); | |
500 | next = read_tree_block(root, blocknr); | |
83e15a28 | 501 | if (path->nodes[*level-1]) |
234b63a0 | 502 | btrfs_block_release(root, path->nodes[*level-1]); |
20524f02 | 503 | path->nodes[*level-1] = next; |
7518a238 | 504 | *level = btrfs_header_level(&next->node.header); |
20524f02 CM |
505 | path->slots[*level] = 0; |
506 | } | |
507 | out: | |
e089f05c CM |
508 | ret = btrfs_free_extent(trans, root, path->nodes[*level]->blocknr, 1, |
509 | 1); | |
234b63a0 | 510 | btrfs_block_release(root, path->nodes[*level]); |
20524f02 CM |
511 | path->nodes[*level] = NULL; |
512 | *level += 1; | |
513 | BUG_ON(ret); | |
514 | return 0; | |
515 | } | |
516 | ||
9aca1d51 CM |
517 | /* |
518 | * helper for dropping snapshots. This walks back up the tree in the path | |
519 | * to find the first node higher up where we haven't yet gone through | |
520 | * all the slots | |
521 | */ | |
e089f05c CM |
522 | static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root |
523 | *root, struct btrfs_path *path, int *level) | |
20524f02 CM |
524 | { |
525 | int i; | |
526 | int slot; | |
527 | int ret; | |
234b63a0 | 528 | for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) { |
20524f02 | 529 | slot = path->slots[i]; |
7518a238 CM |
530 | if (slot < |
531 | btrfs_header_nritems(&path->nodes[i]->node.header)- 1) { | |
20524f02 CM |
532 | path->slots[i]++; |
533 | *level = i; | |
534 | return 0; | |
535 | } else { | |
e089f05c CM |
536 | ret = btrfs_free_extent(trans, root, |
537 | path->nodes[*level]->blocknr, | |
538 | 1, 1); | |
234b63a0 | 539 | btrfs_block_release(root, path->nodes[*level]); |
83e15a28 | 540 | path->nodes[*level] = NULL; |
20524f02 CM |
541 | *level = i + 1; |
542 | BUG_ON(ret); | |
543 | } | |
544 | } | |
545 | return 1; | |
546 | } | |
547 | ||
9aca1d51 CM |
548 | /* |
549 | * drop the reference count on the tree rooted at 'snap'. This traverses | |
550 | * the tree freeing any blocks that have a ref count of zero after being | |
551 | * decremented. | |
552 | */ | |
e089f05c CM |
553 | int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root |
554 | *root, struct btrfs_buffer *snap) | |
20524f02 | 555 | { |
3768f368 | 556 | int ret = 0; |
9aca1d51 | 557 | int wret; |
20524f02 | 558 | int level; |
234b63a0 | 559 | struct btrfs_path path; |
20524f02 CM |
560 | int i; |
561 | int orig_level; | |
562 | ||
234b63a0 | 563 | btrfs_init_path(&path); |
20524f02 | 564 | |
7518a238 | 565 | level = btrfs_header_level(&snap->node.header); |
20524f02 CM |
566 | orig_level = level; |
567 | path.nodes[level] = snap; | |
568 | path.slots[level] = 0; | |
569 | while(1) { | |
e089f05c | 570 | wret = walk_down_tree(trans, root, &path, &level); |
9aca1d51 | 571 | if (wret > 0) |
20524f02 | 572 | break; |
9aca1d51 CM |
573 | if (wret < 0) |
574 | ret = wret; | |
575 | ||
e089f05c | 576 | wret = walk_up_tree(trans, root, &path, &level); |
9aca1d51 | 577 | if (wret > 0) |
20524f02 | 578 | break; |
9aca1d51 CM |
579 | if (wret < 0) |
580 | ret = wret; | |
20524f02 | 581 | } |
83e15a28 CM |
582 | for (i = 0; i <= orig_level; i++) { |
583 | if (path.nodes[i]) { | |
234b63a0 | 584 | btrfs_block_release(root, path.nodes[i]); |
83e15a28 | 585 | } |
20524f02 | 586 | } |
9aca1d51 | 587 | return ret; |
20524f02 | 588 | } |