blob: fdf95bd07f9033403d9ea7212d596f9ee8547f35 [file] [log] [blame]
Chris Masonfec577f2007-02-26 10:40:21 -05001#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"
8
Chris Mason037e6392007-03-07 11:50:24 -05009static int find_free_extent(struct ctree_root *orig_root, u64 num_blocks,
Chris Masone2fa7222007-03-12 16:22:34 -040010 u64 search_start, u64 search_end,
11 struct btrfs_key *ins);
Chris Mason037e6392007-03-07 11:50:24 -050012static int finish_current_insert(struct ctree_root *extent_root);
13static int run_pending(struct ctree_root *extent_root);
14
Chris Masonfec577f2007-02-26 10:40:21 -050015/*
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
20 * manner for deletes.
21 */
Chris Mason037e6392007-03-07 11:50:24 -050022#define CTREE_EXTENT_PENDING_DEL 0
Chris Masonfec577f2007-02-26 10:40:21 -050023
Chris Mason02217ed2007-03-02 16:08:05 -050024static int inc_block_ref(struct ctree_root *root, u64 blocknr)
25{
26 struct ctree_path path;
27 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -040028 struct btrfs_key key;
Chris Mason02217ed2007-03-02 16:08:05 -050029 struct leaf *l;
30 struct extent_item *item;
Chris Masone2fa7222007-03-12 16:22:34 -040031 struct btrfs_key ins;
Chris Mason037e6392007-03-07 11:50:24 -050032
33 find_free_extent(root->extent_root, 0, 0, (u64)-1, &ins);
Chris Mason02217ed2007-03-02 16:08:05 -050034 init_path(&path);
35 key.objectid = blocknr;
36 key.flags = 0;
37 key.offset = 1;
38 ret = search_slot(root->extent_root, &key, &path, 0, 1);
Chris Masona28ec192007-03-06 20:08:01 -050039 if (ret != 0)
40 BUG();
Chris Mason02217ed2007-03-02 16:08:05 -050041 BUG_ON(ret != 0);
42 l = &path.nodes[0]->leaf;
Chris Mason0783fcf2007-03-12 20:12:07 -040043 item = (struct extent_item *)(l->data + btrfs_item_offset(l->items +
44 path.slots[0]));
Chris Mason02217ed2007-03-02 16:08:05 -050045 item->refs++;
Chris Masona28ec192007-03-06 20:08:01 -050046
Chris Mason02217ed2007-03-02 16:08:05 -050047 BUG_ON(list_empty(&path.nodes[0]->dirty));
48 release_path(root->extent_root, &path);
Chris Mason037e6392007-03-07 11:50:24 -050049 finish_current_insert(root->extent_root);
50 run_pending(root->extent_root);
Chris Mason02217ed2007-03-02 16:08:05 -050051 return 0;
52}
53
Chris Mason20524f02007-03-10 06:35:47 -050054static int lookup_block_ref(struct ctree_root *root, u64 blocknr, u32 *refs)
Chris Masona28ec192007-03-06 20:08:01 -050055{
56 struct ctree_path path;
57 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -040058 struct btrfs_key key;
Chris Masona28ec192007-03-06 20:08:01 -050059 struct leaf *l;
60 struct extent_item *item;
61 init_path(&path);
62 key.objectid = blocknr;
63 key.flags = 0;
64 key.offset = 1;
65 ret = search_slot(root->extent_root, &key, &path, 0, 0);
66 if (ret != 0)
67 BUG();
68 l = &path.nodes[0]->leaf;
69 item = (struct extent_item *)(l->data +
Chris Mason0783fcf2007-03-12 20:12:07 -040070 btrfs_item_offset(l->items +
71 path.slots[0]));
Chris Masona28ec192007-03-06 20:08:01 -050072 *refs = item->refs;
73 release_path(root->extent_root, &path);
74 return 0;
75}
76
Chris Mason02217ed2007-03-02 16:08:05 -050077int btrfs_inc_ref(struct ctree_root *root, struct tree_buffer *buf)
78{
79 u64 blocknr;
80 int i;
Chris Masona28ec192007-03-06 20:08:01 -050081
82 if (root == root->extent_root)
83 return 0;
Chris Mason7518a232007-03-12 12:01:18 -040084 if (btrfs_is_leaf(&buf->node))
Chris Masona28ec192007-03-06 20:08:01 -050085 return 0;
86
Chris Mason7518a232007-03-12 12:01:18 -040087 for (i = 0; i < btrfs_header_nritems(&buf->node.header); i++) {
Chris Mason02217ed2007-03-02 16:08:05 -050088 blocknr = buf->node.blockptrs[i];
89 inc_block_ref(root, blocknr);
90 }
91 return 0;
92}
93
Chris Masona28ec192007-03-06 20:08:01 -050094int btrfs_finish_extent_commit(struct ctree_root *root)
95{
96 struct ctree_root *extent_root = root->extent_root;
97 unsigned long gang[8];
98 int ret;
99 int i;
100
101 while(1) {
102 ret = radix_tree_gang_lookup(&extent_root->pinned_radix,
103 (void **)gang, 0,
104 ARRAY_SIZE(gang));
105 if (!ret)
106 break;
Chris Mason0579da42007-03-07 16:15:30 -0500107 for (i = 0; i < ret; i++) {
Chris Masona28ec192007-03-06 20:08:01 -0500108 radix_tree_delete(&extent_root->pinned_radix, gang[i]);
Chris Mason0579da42007-03-07 16:15:30 -0500109 }
Chris Masona28ec192007-03-06 20:08:01 -0500110 }
Chris Mason0579da42007-03-07 16:15:30 -0500111 extent_root->last_insert.objectid = 0;
112 extent_root->last_insert.offset = 0;
Chris Masona28ec192007-03-06 20:08:01 -0500113 return 0;
114}
115
Chris Mason037e6392007-03-07 11:50:24 -0500116static int finish_current_insert(struct ctree_root *extent_root)
117{
Chris Masone2fa7222007-03-12 16:22:34 -0400118 struct btrfs_key ins;
Chris Mason037e6392007-03-07 11:50:24 -0500119 struct extent_item extent_item;
120 int i;
121 int ret;
122
123 extent_item.refs = 1;
Chris Mason7518a232007-03-12 12:01:18 -0400124 extent_item.owner =
125 btrfs_header_parentid(&extent_root->node->node.header);
Chris Mason037e6392007-03-07 11:50:24 -0500126 ins.offset = 1;
127 ins.flags = 0;
128
129 for (i = 0; i < extent_root->current_insert.flags; i++) {
130 ins.objectid = extent_root->current_insert.objectid + i;
131 ret = insert_item(extent_root, &ins, &extent_item,
132 sizeof(extent_item));
133 BUG_ON(ret);
134 }
135 extent_root->current_insert.offset = 0;
136 return 0;
137}
138
Chris Masona28ec192007-03-06 20:08:01 -0500139/*
140 * remove an extent from the root, returns 0 on success
141 */
142int __free_extent(struct ctree_root *root, u64 blocknr, u64 num_blocks)
143{
144 struct ctree_path path;
Chris Masone2fa7222007-03-12 16:22:34 -0400145 struct btrfs_key key;
Chris Masona28ec192007-03-06 20:08:01 -0500146 struct ctree_root *extent_root = root->extent_root;
147 int ret;
Chris Mason0783fcf2007-03-12 20:12:07 -0400148 struct btrfs_item *item;
Chris Masona28ec192007-03-06 20:08:01 -0500149 struct extent_item *ei;
Chris Masone2fa7222007-03-12 16:22:34 -0400150 struct btrfs_key ins;
Chris Mason037e6392007-03-07 11:50:24 -0500151
Chris Masona28ec192007-03-06 20:08:01 -0500152 key.objectid = blocknr;
153 key.flags = 0;
154 key.offset = num_blocks;
155
Chris Mason037e6392007-03-07 11:50:24 -0500156 find_free_extent(root, 0, 0, (u64)-1, &ins);
Chris Masona28ec192007-03-06 20:08:01 -0500157 init_path(&path);
158 ret = search_slot(extent_root, &key, &path, -1, 1);
159 if (ret) {
160 printf("failed to find %Lu\n", key.objectid);
161 print_tree(extent_root, extent_root->node);
162 printf("failed to find %Lu\n", key.objectid);
163 BUG();
164 }
165 item = path.nodes[0]->leaf.items + path.slots[0];
Chris Mason0783fcf2007-03-12 20:12:07 -0400166 ei = (struct extent_item *)(path.nodes[0]->leaf.data +
167 btrfs_item_offset(item));
Chris Masona28ec192007-03-06 20:08:01 -0500168 BUG_ON(ei->refs == 0);
169 ei->refs--;
170 if (ei->refs == 0) {
171 if (root == extent_root) {
172 int err;
173 radix_tree_preload(GFP_KERNEL);
174 err = radix_tree_insert(&extent_root->pinned_radix,
175 blocknr, (void *)blocknr);
176 BUG_ON(err);
177 radix_tree_preload_end();
178 }
179 ret = del_item(extent_root, &path);
Chris Mason0579da42007-03-07 16:15:30 -0500180 if (root != extent_root &&
181 extent_root->last_insert.objectid < blocknr)
182 extent_root->last_insert.objectid = blocknr;
Chris Masona28ec192007-03-06 20:08:01 -0500183 if (ret)
184 BUG();
185 }
186 release_path(extent_root, &path);
Chris Mason037e6392007-03-07 11:50:24 -0500187 finish_current_insert(extent_root);
Chris Masona28ec192007-03-06 20:08:01 -0500188 return ret;
189}
190
191/*
Chris Masonfec577f2007-02-26 10:40:21 -0500192 * find all the blocks marked as pending in the radix tree and remove
193 * them from the extent map
194 */
195static int del_pending_extents(struct ctree_root *extent_root)
196{
197 int ret;
Chris Masonfec577f2007-02-26 10:40:21 -0500198 struct tree_buffer *gang[4];
199 int i;
Chris Masonfec577f2007-02-26 10:40:21 -0500200
201 while(1) {
202 ret = radix_tree_gang_lookup_tag(&extent_root->cache_radix,
203 (void **)gang, 0,
204 ARRAY_SIZE(gang),
Chris Masona28ec192007-03-06 20:08:01 -0500205 CTREE_EXTENT_PENDING_DEL);
Chris Masonfec577f2007-02-26 10:40:21 -0500206 if (!ret)
207 break;
208 for (i = 0; i < ret; i++) {
Chris Masona28ec192007-03-06 20:08:01 -0500209 ret = __free_extent(extent_root, gang[i]->blocknr, 1);
Chris Masonfec577f2007-02-26 10:40:21 -0500210 radix_tree_tag_clear(&extent_root->cache_radix,
211 gang[i]->blocknr,
Chris Masona28ec192007-03-06 20:08:01 -0500212 CTREE_EXTENT_PENDING_DEL);
Chris Masonfec577f2007-02-26 10:40:21 -0500213 tree_block_release(extent_root, gang[i]);
214 }
215 }
216 return 0;
217}
218
Chris Masona28ec192007-03-06 20:08:01 -0500219static int run_pending(struct ctree_root *extent_root)
220{
221 while(radix_tree_tagged(&extent_root->cache_radix,
Chris Mason037e6392007-03-07 11:50:24 -0500222 CTREE_EXTENT_PENDING_DEL))
Chris Masona28ec192007-03-06 20:08:01 -0500223 del_pending_extents(extent_root);
Chris Masona28ec192007-03-06 20:08:01 -0500224 return 0;
225}
226
227
Chris Masonfec577f2007-02-26 10:40:21 -0500228/*
229 * remove an extent from the root, returns 0 on success
230 */
231int free_extent(struct ctree_root *root, u64 blocknr, u64 num_blocks)
232{
Chris Masone2fa7222007-03-12 16:22:34 -0400233 struct btrfs_key key;
Chris Masonfec577f2007-02-26 10:40:21 -0500234 struct ctree_root *extent_root = root->extent_root;
235 struct tree_buffer *t;
236 int pending_ret;
237 int ret;
Chris Masona28ec192007-03-06 20:08:01 -0500238
239 if (root == extent_root) {
240 t = find_tree_block(root, blocknr);
Chris Mason037e6392007-03-07 11:50:24 -0500241 radix_tree_tag_set(&root->cache_radix, blocknr,
Chris Masona28ec192007-03-06 20:08:01 -0500242 CTREE_EXTENT_PENDING_DEL);
Chris Masona28ec192007-03-06 20:08:01 -0500243 return 0;
244 }
Chris Masonfec577f2007-02-26 10:40:21 -0500245 key.objectid = blocknr;
246 key.flags = 0;
247 key.offset = num_blocks;
Chris Masona28ec192007-03-06 20:08:01 -0500248 ret = __free_extent(root, blocknr, num_blocks);
249 pending_ret = run_pending(root->extent_root);
Chris Masonfec577f2007-02-26 10:40:21 -0500250 return ret ? ret : pending_ret;
251}
252
253/*
254 * walks the btree of allocated extents and find a hole of a given size.
255 * The key ins is changed to record the hole:
256 * ins->objectid == block start
257 * ins->flags = 0
258 * ins->offset == number of blocks
259 * Any available blocks before search_start are skipped.
260 */
Chris Mason0f70abe2007-02-28 16:46:22 -0500261static int find_free_extent(struct ctree_root *orig_root, u64 num_blocks,
Chris Masone2fa7222007-03-12 16:22:34 -0400262 u64 search_start, u64 search_end,
263 struct btrfs_key *ins)
Chris Masonfec577f2007-02-26 10:40:21 -0500264{
265 struct ctree_path path;
Chris Masone2fa7222007-03-12 16:22:34 -0400266 struct btrfs_key key;
Chris Masonfec577f2007-02-26 10:40:21 -0500267 int ret;
268 u64 hole_size = 0;
269 int slot = 0;
270 u64 last_block;
Chris Mason037e6392007-03-07 11:50:24 -0500271 u64 test_block;
Chris Masonfec577f2007-02-26 10:40:21 -0500272 int start_found;
273 struct leaf *l;
274 struct ctree_root * root = orig_root->extent_root;
Chris Mason0579da42007-03-07 16:15:30 -0500275 int total_needed = num_blocks;
Chris Masonfec577f2007-02-26 10:40:21 -0500276
Chris Mason7518a232007-03-12 12:01:18 -0400277 total_needed += (btrfs_header_level(&root->node->node.header) + 1) * 3;
Chris Mason0579da42007-03-07 16:15:30 -0500278 if (root->last_insert.objectid > search_start)
279 search_start = root->last_insert.objectid;
Chris Masonfec577f2007-02-26 10:40:21 -0500280check_failed:
281 init_path(&path);
282 ins->objectid = search_start;
283 ins->offset = 0;
284 ins->flags = 0;
285 start_found = 0;
Chris Mason02217ed2007-03-02 16:08:05 -0500286 ret = search_slot(root, ins, &path, 0, 0);
Chris Mason0f70abe2007-02-28 16:46:22 -0500287 if (ret < 0)
288 goto error;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500289
Chris Mason0579da42007-03-07 16:15:30 -0500290 if (path.slots[0] > 0)
291 path.slots[0]--;
292
Chris Masonfec577f2007-02-26 10:40:21 -0500293 while (1) {
294 l = &path.nodes[0]->leaf;
295 slot = path.slots[0];
Chris Mason7518a232007-03-12 12:01:18 -0400296 if (slot >= btrfs_header_nritems(&l->header)) {
Chris Masonfec577f2007-02-26 10:40:21 -0500297 ret = next_leaf(root, &path);
298 if (ret == 0)
299 continue;
Chris Mason0f70abe2007-02-28 16:46:22 -0500300 if (ret < 0)
301 goto error;
Chris Masonfec577f2007-02-26 10:40:21 -0500302 if (!start_found) {
303 ins->objectid = search_start;
Chris Mason037e6392007-03-07 11:50:24 -0500304 ins->offset = (u64)-1;
Chris Masonfec577f2007-02-26 10:40:21 -0500305 start_found = 1;
306 goto check_pending;
307 }
308 ins->objectid = last_block > search_start ?
309 last_block : search_start;
Chris Mason037e6392007-03-07 11:50:24 -0500310 ins->offset = (u64)-1;
Chris Masonfec577f2007-02-26 10:40:21 -0500311 goto check_pending;
312 }
Chris Masone2fa7222007-03-12 16:22:34 -0400313 btrfs_disk_key_to_cpu(&key, &l->items[slot].key);
314 if (key.objectid >= search_start) {
Chris Masonfec577f2007-02-26 10:40:21 -0500315 if (start_found) {
Chris Mason0579da42007-03-07 16:15:30 -0500316 if (last_block < search_start)
317 last_block = search_start;
Chris Masone2fa7222007-03-12 16:22:34 -0400318 hole_size = key.objectid - last_block;
Chris Mason037e6392007-03-07 11:50:24 -0500319 if (hole_size > total_needed) {
Chris Masonfec577f2007-02-26 10:40:21 -0500320 ins->objectid = last_block;
Chris Mason037e6392007-03-07 11:50:24 -0500321 ins->offset = hole_size;
Chris Masonfec577f2007-02-26 10:40:21 -0500322 goto check_pending;
323 }
Chris Mason0579da42007-03-07 16:15:30 -0500324 }
Chris Masonfec577f2007-02-26 10:40:21 -0500325 }
Chris Mason0579da42007-03-07 16:15:30 -0500326 start_found = 1;
Chris Masone2fa7222007-03-12 16:22:34 -0400327 last_block = key.objectid + key.offset;
Chris Masonfec577f2007-02-26 10:40:21 -0500328 path.slots[0]++;
329 }
330 // FIXME -ENOSPC
331check_pending:
332 /* we have to make sure we didn't find an extent that has already
333 * been allocated by the map tree or the original allocation
334 */
335 release_path(root, &path);
336 BUG_ON(ins->objectid < search_start);
Chris Mason037e6392007-03-07 11:50:24 -0500337 for (test_block = ins->objectid;
338 test_block < ins->objectid + total_needed; test_block++) {
339 if (radix_tree_lookup(&root->pinned_radix, test_block)) {
340 search_start = test_block + 1;
Chris Masonfec577f2007-02-26 10:40:21 -0500341 goto check_failed;
342 }
343 }
Chris Mason037e6392007-03-07 11:50:24 -0500344 BUG_ON(root->current_insert.offset);
Chris Mason0579da42007-03-07 16:15:30 -0500345 root->current_insert.offset = total_needed - num_blocks;
Chris Mason037e6392007-03-07 11:50:24 -0500346 root->current_insert.objectid = ins->objectid + num_blocks;
347 root->current_insert.flags = 0;
Chris Mason0579da42007-03-07 16:15:30 -0500348 root->last_insert.objectid = ins->objectid;
Chris Mason037e6392007-03-07 11:50:24 -0500349 ins->offset = num_blocks;
Chris Masonfec577f2007-02-26 10:40:21 -0500350 return 0;
Chris Mason0f70abe2007-02-28 16:46:22 -0500351error:
352 release_path(root, &path);
353 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -0500354}
355
356/*
Chris Masonfec577f2007-02-26 10:40:21 -0500357 * finds a free extent and does all the dirty work required for allocation
358 * returns the key for the extent through ins, and a tree buffer for
359 * the first block of the extent through buf.
360 *
361 * returns 0 if everything worked, non-zero otherwise.
362 */
363int alloc_extent(struct ctree_root *root, u64 num_blocks, u64 search_start,
Chris Masone2fa7222007-03-12 16:22:34 -0400364 u64 search_end, u64 owner, struct btrfs_key *ins)
Chris Masonfec577f2007-02-26 10:40:21 -0500365{
366 int ret;
367 int pending_ret;
Chris Mason037e6392007-03-07 11:50:24 -0500368 struct ctree_root *extent_root = root->extent_root;
Chris Masonfec577f2007-02-26 10:40:21 -0500369 struct extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -0500370
Chris Masonfec577f2007-02-26 10:40:21 -0500371 extent_item.refs = 1;
372 extent_item.owner = owner;
373
Chris Mason037e6392007-03-07 11:50:24 -0500374 if (root == extent_root) {
375 BUG_ON(extent_root->current_insert.offset == 0);
376 BUG_ON(num_blocks != 1);
377 BUG_ON(extent_root->current_insert.flags ==
378 extent_root->current_insert.offset);
379 ins->offset = 1;
380 ins->objectid = extent_root->current_insert.objectid +
381 extent_root->current_insert.flags++;
Chris Masonfec577f2007-02-26 10:40:21 -0500382 return 0;
383 }
Chris Mason037e6392007-03-07 11:50:24 -0500384 ret = find_free_extent(root, num_blocks, search_start,
385 search_end, ins);
386 if (ret)
387 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -0500388
Chris Mason037e6392007-03-07 11:50:24 -0500389 ret = insert_item(extent_root, ins, &extent_item,
390 sizeof(extent_item));
391
392 finish_current_insert(extent_root);
393 pending_ret = run_pending(extent_root);
394 if (ret)
395 return ret;
396 if (pending_ret)
397 return pending_ret;
398 return 0;
Chris Masonfec577f2007-02-26 10:40:21 -0500399}
400
401/*
402 * helper function to allocate a block for a given tree
403 * returns the tree buffer or NULL.
404 */
405struct tree_buffer *alloc_free_block(struct ctree_root *root)
406{
Chris Masone2fa7222007-03-12 16:22:34 -0400407 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -0500408 int ret;
Chris Mason037e6392007-03-07 11:50:24 -0500409 struct tree_buffer *buf;
Chris Masonfec577f2007-02-26 10:40:21 -0500410
411 ret = alloc_extent(root, 1, 0, (unsigned long)-1,
Chris Mason7518a232007-03-12 12:01:18 -0400412 btrfs_header_parentid(&root->node->node.header),
Chris Mason037e6392007-03-07 11:50:24 -0500413 &ins);
Chris Masonfec577f2007-02-26 10:40:21 -0500414 if (ret) {
415 BUG();
416 return NULL;
417 }
Chris Mason037e6392007-03-07 11:50:24 -0500418 buf = find_tree_block(root, ins.objectid);
419 dirty_tree_block(root, buf);
Chris Masonfec577f2007-02-26 10:40:21 -0500420 return buf;
421}
Chris Masona28ec192007-03-06 20:08:01 -0500422
Chris Mason20524f02007-03-10 06:35:47 -0500423int walk_down_tree(struct ctree_root *root, struct ctree_path *path, int *level)
424{
425 struct tree_buffer *next;
426 struct tree_buffer *cur;
427 u64 blocknr;
428 int ret;
429 u32 refs;
430
431 ret = lookup_block_ref(root, path->nodes[*level]->blocknr, &refs);
432 BUG_ON(ret);
433 if (refs > 1)
434 goto out;
435 while(*level > 0) {
436 cur = path->nodes[*level];
Chris Mason7518a232007-03-12 12:01:18 -0400437 if (path->slots[*level] >=
438 btrfs_header_nritems(&cur->node.header))
Chris Mason20524f02007-03-10 06:35:47 -0500439 break;
440 blocknr = cur->node.blockptrs[path->slots[*level]];
441 ret = lookup_block_ref(root, blocknr, &refs);
442 if (refs != 1 || *level == 1) {
443 path->slots[*level]++;
444 ret = free_extent(root, blocknr, 1);
445 BUG_ON(ret);
446 continue;
447 }
448 BUG_ON(ret);
449 next = read_tree_block(root, blocknr);
Chris Mason83e15a22007-03-12 09:03:27 -0400450 if (path->nodes[*level-1])
Chris Mason20524f02007-03-10 06:35:47 -0500451 tree_block_release(root, path->nodes[*level-1]);
Chris Mason20524f02007-03-10 06:35:47 -0500452 path->nodes[*level-1] = next;
Chris Mason7518a232007-03-12 12:01:18 -0400453 *level = btrfs_header_level(&next->node.header);
Chris Mason20524f02007-03-10 06:35:47 -0500454 path->slots[*level] = 0;
455 }
456out:
457 ret = free_extent(root, path->nodes[*level]->blocknr, 1);
Chris Mason83e15a22007-03-12 09:03:27 -0400458 tree_block_release(root, path->nodes[*level]);
Chris Mason20524f02007-03-10 06:35:47 -0500459 path->nodes[*level] = NULL;
460 *level += 1;
461 BUG_ON(ret);
462 return 0;
463}
464
465int walk_up_tree(struct ctree_root *root, struct ctree_path *path, int *level)
466{
467 int i;
468 int slot;
469 int ret;
470 for(i = *level; i < MAX_LEVEL - 1 && path->nodes[i]; i++) {
471 slot = path->slots[i];
Chris Mason7518a232007-03-12 12:01:18 -0400472 if (slot <
473 btrfs_header_nritems(&path->nodes[i]->node.header)- 1) {
Chris Mason20524f02007-03-10 06:35:47 -0500474 path->slots[i]++;
475 *level = i;
476 return 0;
477 } else {
478 ret = free_extent(root,
479 path->nodes[*level]->blocknr, 1);
Chris Mason83e15a22007-03-12 09:03:27 -0400480 tree_block_release(root, path->nodes[*level]);
481 path->nodes[*level] = NULL;
Chris Mason20524f02007-03-10 06:35:47 -0500482 *level = i + 1;
483 BUG_ON(ret);
484 }
485 }
486 return 1;
487}
488
489int btrfs_drop_snapshot(struct ctree_root *root, struct tree_buffer *snap)
490{
491 int ret;
492 int level;
493 struct ctree_path path;
494 int i;
495 int orig_level;
496
497 init_path(&path);
498
Chris Mason7518a232007-03-12 12:01:18 -0400499 level = btrfs_header_level(&snap->node.header);
Chris Mason20524f02007-03-10 06:35:47 -0500500 orig_level = level;
501 path.nodes[level] = snap;
502 path.slots[level] = 0;
503 while(1) {
504 ret = walk_down_tree(root, &path, &level);
505 if (ret > 0)
506 break;
507 ret = walk_up_tree(root, &path, &level);
508 if (ret > 0)
509 break;
510 }
Chris Mason83e15a22007-03-12 09:03:27 -0400511 for (i = 0; i <= orig_level; i++) {
512 if (path.nodes[i]) {
Chris Mason20524f02007-03-10 06:35:47 -0500513 tree_block_release(root, path.nodes[i]);
Chris Mason83e15a22007-03-12 09:03:27 -0400514 }
Chris Mason20524f02007-03-10 06:35:47 -0500515 }
516
517 return 0;
518}