blob: 475e72215964e2067429035d6c91609da0d67e23 [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;
43 item = (struct extent_item *)(l->data +
44 l->items[path.slots[0]].offset);
45 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 +
70 l->items[path.slots[0]].offset);
71 *refs = item->refs;
72 release_path(root->extent_root, &path);
73 return 0;
74}
75
Chris Mason02217ed2007-03-02 16:08:05 -050076int btrfs_inc_ref(struct ctree_root *root, struct tree_buffer *buf)
77{
78 u64 blocknr;
79 int i;
Chris Masona28ec192007-03-06 20:08:01 -050080
81 if (root == root->extent_root)
82 return 0;
Chris Mason7518a232007-03-12 12:01:18 -040083 if (btrfs_is_leaf(&buf->node))
Chris Masona28ec192007-03-06 20:08:01 -050084 return 0;
85
Chris Mason7518a232007-03-12 12:01:18 -040086 for (i = 0; i < btrfs_header_nritems(&buf->node.header); i++) {
Chris Mason02217ed2007-03-02 16:08:05 -050087 blocknr = buf->node.blockptrs[i];
88 inc_block_ref(root, blocknr);
89 }
90 return 0;
91}
92
Chris Masona28ec192007-03-06 20:08:01 -050093int btrfs_finish_extent_commit(struct ctree_root *root)
94{
95 struct ctree_root *extent_root = root->extent_root;
96 unsigned long gang[8];
97 int ret;
98 int i;
99
100 while(1) {
101 ret = radix_tree_gang_lookup(&extent_root->pinned_radix,
102 (void **)gang, 0,
103 ARRAY_SIZE(gang));
104 if (!ret)
105 break;
Chris Mason0579da42007-03-07 16:15:30 -0500106 for (i = 0; i < ret; i++) {
Chris Masona28ec192007-03-06 20:08:01 -0500107 radix_tree_delete(&extent_root->pinned_radix, gang[i]);
Chris Mason0579da42007-03-07 16:15:30 -0500108 }
Chris Masona28ec192007-03-06 20:08:01 -0500109 }
Chris Mason0579da42007-03-07 16:15:30 -0500110 extent_root->last_insert.objectid = 0;
111 extent_root->last_insert.offset = 0;
Chris Masona28ec192007-03-06 20:08:01 -0500112 return 0;
113}
114
Chris Mason037e6392007-03-07 11:50:24 -0500115static int finish_current_insert(struct ctree_root *extent_root)
116{
Chris Masone2fa7222007-03-12 16:22:34 -0400117 struct btrfs_key ins;
Chris Mason037e6392007-03-07 11:50:24 -0500118 struct extent_item extent_item;
119 int i;
120 int ret;
121
122 extent_item.refs = 1;
Chris Mason7518a232007-03-12 12:01:18 -0400123 extent_item.owner =
124 btrfs_header_parentid(&extent_root->node->node.header);
Chris Mason037e6392007-03-07 11:50:24 -0500125 ins.offset = 1;
126 ins.flags = 0;
127
128 for (i = 0; i < extent_root->current_insert.flags; i++) {
129 ins.objectid = extent_root->current_insert.objectid + i;
130 ret = insert_item(extent_root, &ins, &extent_item,
131 sizeof(extent_item));
132 BUG_ON(ret);
133 }
134 extent_root->current_insert.offset = 0;
135 return 0;
136}
137
Chris Masona28ec192007-03-06 20:08:01 -0500138/*
139 * remove an extent from the root, returns 0 on success
140 */
141int __free_extent(struct ctree_root *root, u64 blocknr, u64 num_blocks)
142{
143 struct ctree_path path;
Chris Masone2fa7222007-03-12 16:22:34 -0400144 struct btrfs_key key;
Chris Masona28ec192007-03-06 20:08:01 -0500145 struct ctree_root *extent_root = root->extent_root;
146 int ret;
147 struct item *item;
148 struct extent_item *ei;
Chris Masone2fa7222007-03-12 16:22:34 -0400149 struct btrfs_key ins;
Chris Mason037e6392007-03-07 11:50:24 -0500150
Chris Masona28ec192007-03-06 20:08:01 -0500151 key.objectid = blocknr;
152 key.flags = 0;
153 key.offset = num_blocks;
154
Chris Mason037e6392007-03-07 11:50:24 -0500155 find_free_extent(root, 0, 0, (u64)-1, &ins);
Chris Masona28ec192007-03-06 20:08:01 -0500156 init_path(&path);
157 ret = search_slot(extent_root, &key, &path, -1, 1);
158 if (ret) {
159 printf("failed to find %Lu\n", key.objectid);
160 print_tree(extent_root, extent_root->node);
161 printf("failed to find %Lu\n", key.objectid);
162 BUG();
163 }
164 item = path.nodes[0]->leaf.items + path.slots[0];
165 ei = (struct extent_item *)(path.nodes[0]->leaf.data + item->offset);
166 BUG_ON(ei->refs == 0);
167 ei->refs--;
168 if (ei->refs == 0) {
169 if (root == extent_root) {
170 int err;
171 radix_tree_preload(GFP_KERNEL);
172 err = radix_tree_insert(&extent_root->pinned_radix,
173 blocknr, (void *)blocknr);
174 BUG_ON(err);
175 radix_tree_preload_end();
176 }
177 ret = del_item(extent_root, &path);
Chris Mason0579da42007-03-07 16:15:30 -0500178 if (root != extent_root &&
179 extent_root->last_insert.objectid < blocknr)
180 extent_root->last_insert.objectid = blocknr;
Chris Masona28ec192007-03-06 20:08:01 -0500181 if (ret)
182 BUG();
183 }
184 release_path(extent_root, &path);
Chris Mason037e6392007-03-07 11:50:24 -0500185 finish_current_insert(extent_root);
Chris Masona28ec192007-03-06 20:08:01 -0500186 return ret;
187}
188
189/*
Chris Masonfec577f2007-02-26 10:40:21 -0500190 * find all the blocks marked as pending in the radix tree and remove
191 * them from the extent map
192 */
193static int del_pending_extents(struct ctree_root *extent_root)
194{
195 int ret;
Chris Masonfec577f2007-02-26 10:40:21 -0500196 struct tree_buffer *gang[4];
197 int i;
Chris Masonfec577f2007-02-26 10:40:21 -0500198
199 while(1) {
200 ret = radix_tree_gang_lookup_tag(&extent_root->cache_radix,
201 (void **)gang, 0,
202 ARRAY_SIZE(gang),
Chris Masona28ec192007-03-06 20:08:01 -0500203 CTREE_EXTENT_PENDING_DEL);
Chris Masonfec577f2007-02-26 10:40:21 -0500204 if (!ret)
205 break;
206 for (i = 0; i < ret; i++) {
Chris Masona28ec192007-03-06 20:08:01 -0500207 ret = __free_extent(extent_root, gang[i]->blocknr, 1);
Chris Masonfec577f2007-02-26 10:40:21 -0500208 radix_tree_tag_clear(&extent_root->cache_radix,
209 gang[i]->blocknr,
Chris Masona28ec192007-03-06 20:08:01 -0500210 CTREE_EXTENT_PENDING_DEL);
Chris Masonfec577f2007-02-26 10:40:21 -0500211 tree_block_release(extent_root, gang[i]);
212 }
213 }
214 return 0;
215}
216
Chris Masona28ec192007-03-06 20:08:01 -0500217static int run_pending(struct ctree_root *extent_root)
218{
219 while(radix_tree_tagged(&extent_root->cache_radix,
Chris Mason037e6392007-03-07 11:50:24 -0500220 CTREE_EXTENT_PENDING_DEL))
Chris Masona28ec192007-03-06 20:08:01 -0500221 del_pending_extents(extent_root);
Chris Masona28ec192007-03-06 20:08:01 -0500222 return 0;
223}
224
225
Chris Masonfec577f2007-02-26 10:40:21 -0500226/*
227 * remove an extent from the root, returns 0 on success
228 */
229int free_extent(struct ctree_root *root, u64 blocknr, u64 num_blocks)
230{
Chris Masone2fa7222007-03-12 16:22:34 -0400231 struct btrfs_key key;
Chris Masonfec577f2007-02-26 10:40:21 -0500232 struct ctree_root *extent_root = root->extent_root;
233 struct tree_buffer *t;
234 int pending_ret;
235 int ret;
Chris Masona28ec192007-03-06 20:08:01 -0500236
237 if (root == extent_root) {
238 t = find_tree_block(root, blocknr);
Chris Mason037e6392007-03-07 11:50:24 -0500239 radix_tree_tag_set(&root->cache_radix, blocknr,
Chris Masona28ec192007-03-06 20:08:01 -0500240 CTREE_EXTENT_PENDING_DEL);
Chris Masona28ec192007-03-06 20:08:01 -0500241 return 0;
242 }
Chris Masonfec577f2007-02-26 10:40:21 -0500243 key.objectid = blocknr;
244 key.flags = 0;
245 key.offset = num_blocks;
Chris Masona28ec192007-03-06 20:08:01 -0500246 ret = __free_extent(root, blocknr, num_blocks);
247 pending_ret = run_pending(root->extent_root);
Chris Masonfec577f2007-02-26 10:40:21 -0500248 return ret ? ret : pending_ret;
249}
250
251/*
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
255 * ins->flags = 0
256 * ins->offset == number of blocks
257 * Any available blocks before search_start are skipped.
258 */
Chris Mason0f70abe2007-02-28 16:46:22 -0500259static int find_free_extent(struct ctree_root *orig_root, u64 num_blocks,
Chris Masone2fa7222007-03-12 16:22:34 -0400260 u64 search_start, u64 search_end,
261 struct btrfs_key *ins)
Chris Masonfec577f2007-02-26 10:40:21 -0500262{
263 struct ctree_path path;
Chris Masone2fa7222007-03-12 16:22:34 -0400264 struct btrfs_key key;
Chris Masonfec577f2007-02-26 10:40:21 -0500265 int ret;
266 u64 hole_size = 0;
267 int slot = 0;
268 u64 last_block;
Chris Mason037e6392007-03-07 11:50:24 -0500269 u64 test_block;
Chris Masonfec577f2007-02-26 10:40:21 -0500270 int start_found;
271 struct leaf *l;
272 struct ctree_root * root = orig_root->extent_root;
Chris Mason0579da42007-03-07 16:15:30 -0500273 int total_needed = num_blocks;
Chris Masonfec577f2007-02-26 10:40:21 -0500274
Chris Mason7518a232007-03-12 12:01:18 -0400275 total_needed += (btrfs_header_level(&root->node->node.header) + 1) * 3;
Chris Mason0579da42007-03-07 16:15:30 -0500276 if (root->last_insert.objectid > search_start)
277 search_start = root->last_insert.objectid;
Chris Masonfec577f2007-02-26 10:40:21 -0500278check_failed:
279 init_path(&path);
280 ins->objectid = search_start;
281 ins->offset = 0;
282 ins->flags = 0;
283 start_found = 0;
Chris Mason02217ed2007-03-02 16:08:05 -0500284 ret = search_slot(root, ins, &path, 0, 0);
Chris Mason0f70abe2007-02-28 16:46:22 -0500285 if (ret < 0)
286 goto error;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500287
Chris Mason0579da42007-03-07 16:15:30 -0500288 if (path.slots[0] > 0)
289 path.slots[0]--;
290
Chris Masonfec577f2007-02-26 10:40:21 -0500291 while (1) {
292 l = &path.nodes[0]->leaf;
293 slot = path.slots[0];
Chris Mason7518a232007-03-12 12:01:18 -0400294 if (slot >= btrfs_header_nritems(&l->header)) {
Chris Masonfec577f2007-02-26 10:40:21 -0500295 ret = next_leaf(root, &path);
296 if (ret == 0)
297 continue;
Chris Mason0f70abe2007-02-28 16:46:22 -0500298 if (ret < 0)
299 goto error;
Chris Masonfec577f2007-02-26 10:40:21 -0500300 if (!start_found) {
301 ins->objectid = search_start;
Chris Mason037e6392007-03-07 11:50:24 -0500302 ins->offset = (u64)-1;
Chris Masonfec577f2007-02-26 10:40:21 -0500303 start_found = 1;
304 goto check_pending;
305 }
306 ins->objectid = last_block > search_start ?
307 last_block : search_start;
Chris Mason037e6392007-03-07 11:50:24 -0500308 ins->offset = (u64)-1;
Chris Masonfec577f2007-02-26 10:40:21 -0500309 goto check_pending;
310 }
Chris Masone2fa7222007-03-12 16:22:34 -0400311 btrfs_disk_key_to_cpu(&key, &l->items[slot].key);
312 if (key.objectid >= search_start) {
Chris Masonfec577f2007-02-26 10:40:21 -0500313 if (start_found) {
Chris Mason0579da42007-03-07 16:15:30 -0500314 if (last_block < search_start)
315 last_block = search_start;
Chris Masone2fa7222007-03-12 16:22:34 -0400316 hole_size = key.objectid - last_block;
Chris Mason037e6392007-03-07 11:50:24 -0500317 if (hole_size > total_needed) {
Chris Masonfec577f2007-02-26 10:40:21 -0500318 ins->objectid = last_block;
Chris Mason037e6392007-03-07 11:50:24 -0500319 ins->offset = hole_size;
Chris Masonfec577f2007-02-26 10:40:21 -0500320 goto check_pending;
321 }
Chris Mason0579da42007-03-07 16:15:30 -0500322 }
Chris Masonfec577f2007-02-26 10:40:21 -0500323 }
Chris Mason0579da42007-03-07 16:15:30 -0500324 start_found = 1;
Chris Masone2fa7222007-03-12 16:22:34 -0400325 last_block = key.objectid + key.offset;
Chris Masonfec577f2007-02-26 10:40:21 -0500326 path.slots[0]++;
327 }
328 // FIXME -ENOSPC
329check_pending:
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
332 */
333 release_path(root, &path);
334 BUG_ON(ins->objectid < search_start);
Chris Mason037e6392007-03-07 11:50:24 -0500335 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;
Chris Masonfec577f2007-02-26 10:40:21 -0500339 goto check_failed;
340 }
341 }
Chris Mason037e6392007-03-07 11:50:24 -0500342 BUG_ON(root->current_insert.offset);
Chris Mason0579da42007-03-07 16:15:30 -0500343 root->current_insert.offset = total_needed - num_blocks;
Chris Mason037e6392007-03-07 11:50:24 -0500344 root->current_insert.objectid = ins->objectid + num_blocks;
345 root->current_insert.flags = 0;
Chris Mason0579da42007-03-07 16:15:30 -0500346 root->last_insert.objectid = ins->objectid;
Chris Mason037e6392007-03-07 11:50:24 -0500347 ins->offset = num_blocks;
Chris Masonfec577f2007-02-26 10:40:21 -0500348 return 0;
Chris Mason0f70abe2007-02-28 16:46:22 -0500349error:
350 release_path(root, &path);
351 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -0500352}
353
354/*
Chris Masonfec577f2007-02-26 10:40:21 -0500355 * 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.
358 *
359 * returns 0 if everything worked, non-zero otherwise.
360 */
361int alloc_extent(struct ctree_root *root, u64 num_blocks, u64 search_start,
Chris Masone2fa7222007-03-12 16:22:34 -0400362 u64 search_end, u64 owner, struct btrfs_key *ins)
Chris Masonfec577f2007-02-26 10:40:21 -0500363{
364 int ret;
365 int pending_ret;
Chris Mason037e6392007-03-07 11:50:24 -0500366 struct ctree_root *extent_root = root->extent_root;
Chris Masonfec577f2007-02-26 10:40:21 -0500367 struct extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -0500368
Chris Masonfec577f2007-02-26 10:40:21 -0500369 extent_item.refs = 1;
370 extent_item.owner = owner;
371
Chris Mason037e6392007-03-07 11:50:24 -0500372 if (root == extent_root) {
373 BUG_ON(extent_root->current_insert.offset == 0);
374 BUG_ON(num_blocks != 1);
375 BUG_ON(extent_root->current_insert.flags ==
376 extent_root->current_insert.offset);
377 ins->offset = 1;
378 ins->objectid = extent_root->current_insert.objectid +
379 extent_root->current_insert.flags++;
Chris Masonfec577f2007-02-26 10:40:21 -0500380 return 0;
381 }
Chris Mason037e6392007-03-07 11:50:24 -0500382 ret = find_free_extent(root, num_blocks, search_start,
383 search_end, ins);
384 if (ret)
385 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -0500386
Chris Mason037e6392007-03-07 11:50:24 -0500387 ret = insert_item(extent_root, ins, &extent_item,
388 sizeof(extent_item));
389
390 finish_current_insert(extent_root);
391 pending_ret = run_pending(extent_root);
392 if (ret)
393 return ret;
394 if (pending_ret)
395 return pending_ret;
396 return 0;
Chris Masonfec577f2007-02-26 10:40:21 -0500397}
398
399/*
400 * helper function to allocate a block for a given tree
401 * returns the tree buffer or NULL.
402 */
403struct tree_buffer *alloc_free_block(struct ctree_root *root)
404{
Chris Masone2fa7222007-03-12 16:22:34 -0400405 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -0500406 int ret;
Chris Mason037e6392007-03-07 11:50:24 -0500407 struct tree_buffer *buf;
Chris Masonfec577f2007-02-26 10:40:21 -0500408
409 ret = alloc_extent(root, 1, 0, (unsigned long)-1,
Chris Mason7518a232007-03-12 12:01:18 -0400410 btrfs_header_parentid(&root->node->node.header),
Chris Mason037e6392007-03-07 11:50:24 -0500411 &ins);
Chris Masonfec577f2007-02-26 10:40:21 -0500412 if (ret) {
413 BUG();
414 return NULL;
415 }
Chris Mason037e6392007-03-07 11:50:24 -0500416 buf = find_tree_block(root, ins.objectid);
417 dirty_tree_block(root, buf);
Chris Masonfec577f2007-02-26 10:40:21 -0500418 return buf;
419}
Chris Masona28ec192007-03-06 20:08:01 -0500420
Chris Mason20524f02007-03-10 06:35:47 -0500421int walk_down_tree(struct ctree_root *root, struct ctree_path *path, int *level)
422{
423 struct tree_buffer *next;
424 struct tree_buffer *cur;
425 u64 blocknr;
426 int ret;
427 u32 refs;
428
429 ret = lookup_block_ref(root, path->nodes[*level]->blocknr, &refs);
430 BUG_ON(ret);
431 if (refs > 1)
432 goto out;
433 while(*level > 0) {
434 cur = path->nodes[*level];
Chris Mason7518a232007-03-12 12:01:18 -0400435 if (path->slots[*level] >=
436 btrfs_header_nritems(&cur->node.header))
Chris Mason20524f02007-03-10 06:35:47 -0500437 break;
438 blocknr = cur->node.blockptrs[path->slots[*level]];
439 ret = lookup_block_ref(root, blocknr, &refs);
440 if (refs != 1 || *level == 1) {
441 path->slots[*level]++;
442 ret = free_extent(root, blocknr, 1);
443 BUG_ON(ret);
444 continue;
445 }
446 BUG_ON(ret);
447 next = read_tree_block(root, blocknr);
Chris Mason83e15a22007-03-12 09:03:27 -0400448 if (path->nodes[*level-1])
Chris Mason20524f02007-03-10 06:35:47 -0500449 tree_block_release(root, path->nodes[*level-1]);
Chris Mason20524f02007-03-10 06:35:47 -0500450 path->nodes[*level-1] = next;
Chris Mason7518a232007-03-12 12:01:18 -0400451 *level = btrfs_header_level(&next->node.header);
Chris Mason20524f02007-03-10 06:35:47 -0500452 path->slots[*level] = 0;
453 }
454out:
455 ret = free_extent(root, path->nodes[*level]->blocknr, 1);
Chris Mason83e15a22007-03-12 09:03:27 -0400456 tree_block_release(root, path->nodes[*level]);
Chris Mason20524f02007-03-10 06:35:47 -0500457 path->nodes[*level] = NULL;
458 *level += 1;
459 BUG_ON(ret);
460 return 0;
461}
462
463int walk_up_tree(struct ctree_root *root, struct ctree_path *path, int *level)
464{
465 int i;
466 int slot;
467 int ret;
468 for(i = *level; i < MAX_LEVEL - 1 && path->nodes[i]; i++) {
469 slot = path->slots[i];
Chris Mason7518a232007-03-12 12:01:18 -0400470 if (slot <
471 btrfs_header_nritems(&path->nodes[i]->node.header)- 1) {
Chris Mason20524f02007-03-10 06:35:47 -0500472 path->slots[i]++;
473 *level = i;
474 return 0;
475 } else {
476 ret = free_extent(root,
477 path->nodes[*level]->blocknr, 1);
Chris Mason83e15a22007-03-12 09:03:27 -0400478 tree_block_release(root, path->nodes[*level]);
479 path->nodes[*level] = NULL;
Chris Mason20524f02007-03-10 06:35:47 -0500480 *level = i + 1;
481 BUG_ON(ret);
482 }
483 }
484 return 1;
485}
486
487int btrfs_drop_snapshot(struct ctree_root *root, struct tree_buffer *snap)
488{
489 int ret;
490 int level;
491 struct ctree_path path;
492 int i;
493 int orig_level;
494
495 init_path(&path);
496
Chris Mason7518a232007-03-12 12:01:18 -0400497 level = btrfs_header_level(&snap->node.header);
Chris Mason20524f02007-03-10 06:35:47 -0500498 orig_level = level;
499 path.nodes[level] = snap;
500 path.slots[level] = 0;
501 while(1) {
502 ret = walk_down_tree(root, &path, &level);
503 if (ret > 0)
504 break;
505 ret = walk_up_tree(root, &path, &level);
506 if (ret > 0)
507 break;
508 }
Chris Mason83e15a22007-03-12 09:03:27 -0400509 for (i = 0; i <= orig_level; i++) {
510 if (path.nodes[i]) {
Chris Mason20524f02007-03-10 06:35:47 -0500511 tree_block_release(root, path.nodes[i]);
Chris Mason83e15a22007-03-12 09:03:27 -0400512 }
Chris Mason20524f02007-03-10 06:35:47 -0500513 }
514
515 return 0;
516}