extent-tree.c 15.6 KB
Newer Older
1 2
#include <linux/module.h>
#include <linux/radix-tree.h>
3 4 5
#include "ctree.h"
#include "disk-io.h"
#include "print-tree.h"
6
#include "transaction.h"
7

8 9 10 11 12 13 14
static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
			    *orig_root, u64 num_blocks, u64 search_start, u64
			    search_end, struct btrfs_key *ins);
static int finish_current_insert(struct btrfs_trans_handle *trans, struct
				 btrfs_root *extent_root);
static int run_pending(struct btrfs_trans_handle *trans, struct btrfs_root
		       *extent_root);
C
Chris Mason 已提交
15

16 17 18 19 20 21 22
/*
 * pending extents are blocks that we're trying to allocate in the extent
 * map while trying to grow the map because of other allocations.  To avoid
 * recursing, they are tagged in the radix tree and cleaned up after
 * other allocations are done.  The pending tag is also used in the same
 * manner for deletes.
 */
C
Chris Mason 已提交
23
#define CTREE_EXTENT_PENDING_DEL 0
24

25 26
static int inc_block_ref(struct btrfs_trans_handle *trans, struct btrfs_root
			 *root, u64 blocknr)
C
Chris Mason 已提交
27
{
C
Chris Mason 已提交
28
	struct btrfs_path path;
C
Chris Mason 已提交
29
	int ret;
C
Chris Mason 已提交
30
	struct btrfs_key key;
C
Chris Mason 已提交
31 32
	struct btrfs_leaf *l;
	struct btrfs_extent_item *item;
C
Chris Mason 已提交
33
	struct btrfs_key ins;
C
Chris Mason 已提交
34
	u32 refs;
C
Chris Mason 已提交
35

36 37
	find_free_extent(trans, root->fs_info->extent_root, 0, 0, (u64)-1,
			 &ins);
C
Chris Mason 已提交
38
	btrfs_init_path(&path);
C
Chris Mason 已提交
39 40
	key.objectid = blocknr;
	key.flags = 0;
41
	btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
C
Chris Mason 已提交
42
	key.offset = 1;
43 44
	ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, &path,
				0, 1);
45 46
	if (ret != 0)
		BUG();
C
Chris Mason 已提交
47 48
	BUG_ON(ret != 0);
	l = &path.nodes[0]->leaf;
49
	item = btrfs_item_ptr(l, path.slots[0], struct btrfs_extent_item);
C
Chris Mason 已提交
50 51
	refs = btrfs_extent_refs(item);
	btrfs_set_extent_refs(item, refs + 1);
52

C
Chris Mason 已提交
53
	BUG_ON(list_empty(&path.nodes[0]->dirty));
54 55 56
	btrfs_release_path(root->fs_info->extent_root, &path);
	finish_current_insert(trans, root->fs_info->extent_root);
	run_pending(trans, root->fs_info->extent_root);
C
Chris Mason 已提交
57 58 59
	return 0;
}

60 61
static int lookup_block_ref(struct btrfs_trans_handle *trans, struct btrfs_root
			    *root, u64 blocknr, u32 *refs)
62
{
C
Chris Mason 已提交
63
	struct btrfs_path path;
64
	int ret;
C
Chris Mason 已提交
65
	struct btrfs_key key;
C
Chris Mason 已提交
66 67 68
	struct btrfs_leaf *l;
	struct btrfs_extent_item *item;
	btrfs_init_path(&path);
69 70
	key.objectid = blocknr;
	key.offset = 1;
71 72
	key.flags = 0;
	btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
73 74
	ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, &path,
				0, 0);
75 76 77
	if (ret != 0)
		BUG();
	l = &path.nodes[0]->leaf;
78
	item = btrfs_item_ptr(l, path.slots[0], struct btrfs_extent_item);
C
Chris Mason 已提交
79
	*refs = btrfs_extent_refs(item);
80
	btrfs_release_path(root->fs_info->extent_root, &path);
81 82 83
	return 0;
}

84 85
int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
		  struct btrfs_buffer *buf)
C
Chris Mason 已提交
86 87 88
{
	u64 blocknr;
	int i;
89

90
	if (!root->ref_cows)
91
		return 0;
92
	if (btrfs_is_leaf(&buf->node))
93 94
		return 0;

95
	for (i = 0; i < btrfs_header_nritems(&buf->node.header); i++) {
96
		blocknr = btrfs_node_blockptr(&buf->node, i);
97
		inc_block_ref(trans, root, blocknr);
C
Chris Mason 已提交
98 99 100 101
	}
	return 0;
}

102 103
int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, struct
			       btrfs_root *root)
104 105
{
	unsigned long gang[8];
106
	u64 first = 0;
107 108 109 110
	int ret;
	int i;

	while(1) {
111 112 113
		ret = radix_tree_gang_lookup(&root->fs_info->pinned_radix,
					     (void **)gang, 0,
					     ARRAY_SIZE(gang));
114 115
		if (!ret)
			break;
116 117
		if (!first)
			first = gang[0];
118
		for (i = 0; i < ret; i++) {
119 120
			radix_tree_delete(&root->fs_info->pinned_radix,
					  gang[i]);
121
		}
122
	}
123 124
	root->fs_info->last_insert.objectid = first;
	root->fs_info->last_insert.offset = 0;
125 126 127
	return 0;
}

128 129
static int finish_current_insert(struct btrfs_trans_handle *trans, struct
				 btrfs_root *extent_root)
C
Chris Mason 已提交
130
{
C
Chris Mason 已提交
131
	struct btrfs_key ins;
C
Chris Mason 已提交
132
	struct btrfs_extent_item extent_item;
C
Chris Mason 已提交
133 134
	int i;
	int ret;
135 136
	u64 super_blocks_used;
	struct btrfs_fs_info *info = extent_root->fs_info;
C
Chris Mason 已提交
137

C
Chris Mason 已提交
138 139 140
	btrfs_set_extent_refs(&extent_item, 1);
	btrfs_set_extent_owner(&extent_item,
		btrfs_header_parentid(&extent_root->node->node.header));
C
Chris Mason 已提交
141 142
	ins.offset = 1;
	ins.flags = 0;
143
	btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
C
Chris Mason 已提交
144

145 146 147
	for (i = 0; i < extent_root->fs_info->current_insert.flags; i++) {
		ins.objectid = extent_root->fs_info->current_insert.objectid +
				i;
148 149 150
		super_blocks_used = btrfs_super_blocks_used(info->disk_super);
		btrfs_set_super_blocks_used(info->disk_super,
					    super_blocks_used + 1);
151 152
		ret = btrfs_insert_item(trans, extent_root, &ins, &extent_item,
					sizeof(extent_item));
C
Chris Mason 已提交
153 154
		BUG_ON(ret);
	}
155
	extent_root->fs_info->current_insert.offset = 0;
C
Chris Mason 已提交
156 157 158
	return 0;
}

159
/*
160
 * remove an extent from the root, returns 0 on success
161
 */
162 163
static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
			 *root, u64 blocknr, u64 num_blocks, int pin)
164
{
C
Chris Mason 已提交
165
	struct btrfs_path path;
C
Chris Mason 已提交
166
	struct btrfs_key key;
167 168
	struct btrfs_fs_info *info = root->fs_info;
	struct btrfs_root *extent_root = info->extent_root;
169
	int ret;
C
Chris Mason 已提交
170
	struct btrfs_extent_item *ei;
C
Chris Mason 已提交
171
	struct btrfs_key ins;
C
Chris Mason 已提交
172
	u32 refs;
C
Chris Mason 已提交
173

174
	BUG_ON(pin && num_blocks != 1);
175 176
	key.objectid = blocknr;
	key.flags = 0;
177
	btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
178 179
	key.offset = num_blocks;

180
	find_free_extent(trans, root, 0, 0, (u64)-1, &ins);
C
Chris Mason 已提交
181
	btrfs_init_path(&path);
182
	ret = btrfs_search_slot(trans, extent_root, &key, &path, -1, 1);
183
	if (ret) {
184
		printk("failed to find %Lu\n", key.objectid);
C
Chris Mason 已提交
185
		btrfs_print_tree(extent_root, extent_root->node);
186
		printk("failed to find %Lu\n", key.objectid);
187 188
		BUG();
	}
C
Chris Mason 已提交
189 190
	ei = btrfs_item_ptr(&path.nodes[0]->leaf, path.slots[0],
			    struct btrfs_extent_item);
191
	BUG_ON(ei->refs == 0);
C
Chris Mason 已提交
192 193 194
	refs = btrfs_extent_refs(ei) - 1;
	btrfs_set_extent_refs(ei, refs);
	if (refs == 0) {
195
		u64 super_blocks_used;
196
		if (pin) {
197 198
			int err;
			radix_tree_preload(GFP_KERNEL);
199 200
			err = radix_tree_insert(&info->pinned_radix,
						blocknr, (void *)blocknr);
201 202 203
			BUG_ON(err);
			radix_tree_preload_end();
		}
204 205 206
		super_blocks_used = btrfs_super_blocks_used(info->disk_super);
		btrfs_set_super_blocks_used(info->disk_super,
					    super_blocks_used - num_blocks);
207
		ret = btrfs_del_item(trans, extent_root, &path);
208 209 210
		if (!pin && extent_root->fs_info->last_insert.objectid >
		    blocknr)
			extent_root->fs_info->last_insert.objectid = blocknr;
211 212 213
		if (ret)
			BUG();
	}
C
Chris Mason 已提交
214
	btrfs_release_path(extent_root, &path);
215
	finish_current_insert(trans, extent_root);
216 217 218 219 220 221 222
	return ret;
}

/*
 * find all the blocks marked as pending in the radix tree and remove
 * them from the extent map
 */
223 224
static int del_pending_extents(struct btrfs_trans_handle *trans, struct
			       btrfs_root *extent_root)
225 226
{
	int ret;
C
Chris Mason 已提交
227
	struct btrfs_buffer *gang[4];
228 229 230
	int i;

	while(1) {
231 232 233 234 235
		ret = radix_tree_gang_lookup_tag(
					&extent_root->fs_info->cache_radix,
					(void **)gang, 0,
					ARRAY_SIZE(gang),
					CTREE_EXTENT_PENDING_DEL);
236 237 238
		if (!ret)
			break;
		for (i = 0; i < ret; i++) {
239
			ret = __free_extent(trans, extent_root,
240
					    gang[i]->blocknr, 1, 1);
241 242 243
			radix_tree_tag_clear(&extent_root->fs_info->cache_radix,
					     gang[i]->blocknr,
					     CTREE_EXTENT_PENDING_DEL);
C
Chris Mason 已提交
244
			btrfs_block_release(extent_root, gang[i]);
245 246 247 248 249
		}
	}
	return 0;
}

250 251
static int run_pending(struct btrfs_trans_handle *trans, struct btrfs_root
		       *extent_root)
252
{
253 254
	while(radix_tree_tagged(&extent_root->fs_info->cache_radix,
				CTREE_EXTENT_PENDING_DEL))
255
		del_pending_extents(trans, extent_root);
256 257 258 259
	return 0;
}


260 261 262
/*
 * remove an extent from the root, returns 0 on success
 */
263 264
int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
		      *root, u64 blocknr, u64 num_blocks, int pin)
265
{
266
	struct btrfs_root *extent_root = root->fs_info->extent_root;
C
Chris Mason 已提交
267
	struct btrfs_buffer *t;
268 269
	int pending_ret;
	int ret;
270

271
	if (root == extent_root) {
272
		t = find_tree_block(root, blocknr);
273
		radix_tree_tag_set(&root->fs_info->cache_radix, blocknr,
274
				   CTREE_EXTENT_PENDING_DEL);
275 276
		return 0;
	}
277
	ret = __free_extent(trans, root, blocknr, num_blocks, pin);
278
	pending_ret = run_pending(trans, root->fs_info->extent_root);
279 280 281 282 283 284 285
	return ret ? ret : pending_ret;
}

/*
 * walks the btree of allocated extents and find a hole of a given size.
 * The key ins is changed to record the hole:
 * ins->objectid == block start
286
 * ins->flags = BTRFS_EXTENT_ITEM_KEY
287 288 289
 * ins->offset == number of blocks
 * Any available blocks before search_start are skipped.
 */
290 291 292
static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
			    *orig_root, u64 num_blocks, u64 search_start, u64
			    search_end, struct btrfs_key *ins)
293
{
C
Chris Mason 已提交
294
	struct btrfs_path path;
C
Chris Mason 已提交
295
	struct btrfs_key key;
296 297 298 299
	int ret;
	u64 hole_size = 0;
	int slot = 0;
	u64 last_block;
C
Chris Mason 已提交
300
	u64 test_block;
301
	int start_found;
C
Chris Mason 已提交
302
	struct btrfs_leaf *l;
303
	struct btrfs_root * root = orig_root->fs_info->extent_root;
304
	int total_needed = num_blocks;
305

306
	total_needed += (btrfs_header_level(&root->node->node.header) + 1) * 3;
307 308
	if (root->fs_info->last_insert.objectid > search_start)
		search_start = root->fs_info->last_insert.objectid;
309 310 311 312

	ins->flags = 0;
	btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);

313
check_failed:
C
Chris Mason 已提交
314
	btrfs_init_path(&path);
315 316 317
	ins->objectid = search_start;
	ins->offset = 0;
	start_found = 0;
318
	ret = btrfs_search_slot(trans, root, ins, &path, 0, 0);
C
Chris Mason 已提交
319 320
	if (ret < 0)
		goto error;
C
Chris Mason 已提交
321

322 323 324
	if (path.slots[0] > 0)
		path.slots[0]--;

325 326 327
	while (1) {
		l = &path.nodes[0]->leaf;
		slot = path.slots[0];
328
		if (slot >= btrfs_header_nritems(&l->header)) {
C
Chris Mason 已提交
329
			ret = btrfs_next_leaf(root, &path);
330 331
			if (ret == 0)
				continue;
C
Chris Mason 已提交
332 333
			if (ret < 0)
				goto error;
334 335
			if (!start_found) {
				ins->objectid = search_start;
C
Chris Mason 已提交
336
				ins->offset = (u64)-1;
337 338 339 340 341
				start_found = 1;
				goto check_pending;
			}
			ins->objectid = last_block > search_start ?
					last_block : search_start;
C
Chris Mason 已提交
342
			ins->offset = (u64)-1;
343 344
			goto check_pending;
		}
C
Chris Mason 已提交
345 346
		btrfs_disk_key_to_cpu(&key, &l->items[slot].key);
		if (key.objectid >= search_start) {
347
			if (start_found) {
348 349
				if (last_block < search_start)
					last_block = search_start;
C
Chris Mason 已提交
350
				hole_size = key.objectid - last_block;
C
Chris Mason 已提交
351
				if (hole_size > total_needed) {
352
					ins->objectid = last_block;
C
Chris Mason 已提交
353
					ins->offset = hole_size;
354 355
					goto check_pending;
				}
356
			}
357
		}
358
		start_found = 1;
C
Chris Mason 已提交
359
		last_block = key.objectid + key.offset;
360 361 362 363 364 365 366
		path.slots[0]++;
	}
	// FIXME -ENOSPC
check_pending:
	/* we have to make sure we didn't find an extent that has already
	 * been allocated by the map tree or the original allocation
	 */
C
Chris Mason 已提交
367
	btrfs_release_path(root, &path);
368
	BUG_ON(ins->objectid < search_start);
C
Chris Mason 已提交
369 370
	for (test_block = ins->objectid;
	     test_block < ins->objectid + total_needed; test_block++) {
371 372
		if (radix_tree_lookup(&root->fs_info->pinned_radix,
				      test_block)) {
C
Chris Mason 已提交
373
			search_start = test_block + 1;
374 375 376
			goto check_failed;
		}
	}
377 378 379 380 381
	BUG_ON(root->fs_info->current_insert.offset);
	root->fs_info->current_insert.offset = total_needed - num_blocks;
	root->fs_info->current_insert.objectid = ins->objectid + num_blocks;
	root->fs_info->current_insert.flags = 0;
	root->fs_info->last_insert.objectid = ins->objectid;
C
Chris Mason 已提交
382
	ins->offset = num_blocks;
383
	return 0;
C
Chris Mason 已提交
384
error:
C
Chris Mason 已提交
385
	btrfs_release_path(root, &path);
C
Chris Mason 已提交
386
	return ret;
387 388 389 390 391 392 393 394 395
}

/*
 * finds a free extent and does all the dirty work required for allocation
 * returns the key for the extent through ins, and a tree buffer for
 * the first block of the extent through buf.
 *
 * returns 0 if everything worked, non-zero otherwise.
 */
396 397 398
static int alloc_extent(struct btrfs_trans_handle *trans, struct btrfs_root
			*root, u64 num_blocks, u64 search_start, u64
			search_end, u64 owner, struct btrfs_key *ins)
399 400 401
{
	int ret;
	int pending_ret;
402 403 404
	u64 super_blocks_used;
	struct btrfs_fs_info *info = root->fs_info;
	struct btrfs_root *extent_root = info->extent_root;
C
Chris Mason 已提交
405
	struct btrfs_extent_item extent_item;
C
Chris Mason 已提交
406

C
Chris Mason 已提交
407 408
	btrfs_set_extent_refs(&extent_item, 1);
	btrfs_set_extent_owner(&extent_item, owner);
409

C
Chris Mason 已提交
410
	if (root == extent_root) {
411
		BUG_ON(extent_root->fs_info->current_insert.offset == 0);
C
Chris Mason 已提交
412
		BUG_ON(num_blocks != 1);
413 414
		BUG_ON(extent_root->fs_info->current_insert.flags ==
		       extent_root->fs_info->current_insert.offset);
C
Chris Mason 已提交
415
		ins->offset = 1;
416 417
		ins->objectid = extent_root->fs_info->current_insert.objectid +
				extent_root->fs_info->current_insert.flags++;
418 419
		return 0;
	}
420
	ret = find_free_extent(trans, root, num_blocks, search_start,
C
Chris Mason 已提交
421 422 423
			       search_end, ins);
	if (ret)
		return ret;
424

425 426 427
	super_blocks_used = btrfs_super_blocks_used(info->disk_super);
	btrfs_set_super_blocks_used(info->disk_super, super_blocks_used +
				    num_blocks);
428 429
	ret = btrfs_insert_item(trans, extent_root, ins, &extent_item,
				sizeof(extent_item));
C
Chris Mason 已提交
430

431 432
	finish_current_insert(trans, extent_root);
	pending_ret = run_pending(trans, extent_root);
C
Chris Mason 已提交
433 434 435 436 437
	if (ret)
		return ret;
	if (pending_ret)
		return pending_ret;
	return 0;
438 439 440 441 442 443
}

/*
 * helper function to allocate a block for a given tree
 * returns the tree buffer or NULL.
 */
444 445
struct btrfs_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
					    struct btrfs_root *root)
446
{
C
Chris Mason 已提交
447
	struct btrfs_key ins;
448
	int ret;
C
Chris Mason 已提交
449
	struct btrfs_buffer *buf;
450

451
	ret = alloc_extent(trans, root, 1, 0, (unsigned long)-1,
452
			   btrfs_header_parentid(&root->node->node.header),
C
Chris Mason 已提交
453
			   &ins);
454 455 456 457
	if (ret) {
		BUG();
		return NULL;
	}
C
Chris Mason 已提交
458
	buf = find_tree_block(root, ins.objectid);
459
	dirty_tree_block(trans, root, buf);
460 461
	return buf;
}
462

C
Chris Mason 已提交
463 464 465 466
/*
 * helper function for drop_snapshot, this walks down the tree dropping ref
 * counts as it goes.
 */
467 468
static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
			  *root, struct btrfs_path *path, int *level)
C
Chris Mason 已提交
469
{
C
Chris Mason 已提交
470 471
	struct btrfs_buffer *next;
	struct btrfs_buffer *cur;
C
Chris Mason 已提交
472 473 474 475
	u64 blocknr;
	int ret;
	u32 refs;

476 477
	ret = lookup_block_ref(trans, root, path->nodes[*level]->blocknr,
			       &refs);
C
Chris Mason 已提交
478 479 480
	BUG_ON(ret);
	if (refs > 1)
		goto out;
C
Chris Mason 已提交
481 482 483
	/*
	 * walk down to the last node level and free all the leaves
	 */
C
Chris Mason 已提交
484 485
	while(*level > 0) {
		cur = path->nodes[*level];
486 487
		if (path->slots[*level] >=
		    btrfs_header_nritems(&cur->node.header))
C
Chris Mason 已提交
488
			break;
489
		blocknr = btrfs_node_blockptr(&cur->node, path->slots[*level]);
490
		ret = lookup_block_ref(trans, root, blocknr, &refs);
C
Chris Mason 已提交
491 492
		if (refs != 1 || *level == 1) {
			path->slots[*level]++;
493
			ret = btrfs_free_extent(trans, root, blocknr, 1, 1);
C
Chris Mason 已提交
494 495 496 497 498
			BUG_ON(ret);
			continue;
		}
		BUG_ON(ret);
		next = read_tree_block(root, blocknr);
C
Chris Mason 已提交
499
		if (path->nodes[*level-1])
C
Chris Mason 已提交
500
			btrfs_block_release(root, path->nodes[*level-1]);
C
Chris Mason 已提交
501
		path->nodes[*level-1] = next;
502
		*level = btrfs_header_level(&next->node.header);
C
Chris Mason 已提交
503 504 505
		path->slots[*level] = 0;
	}
out:
506 507
	ret = btrfs_free_extent(trans, root, path->nodes[*level]->blocknr, 1,
				1);
C
Chris Mason 已提交
508
	btrfs_block_release(root, path->nodes[*level]);
C
Chris Mason 已提交
509 510 511 512 513 514
	path->nodes[*level] = NULL;
	*level += 1;
	BUG_ON(ret);
	return 0;
}

C
Chris Mason 已提交
515 516 517 518 519
/*
 * helper for dropping snapshots.  This walks back up the tree in the path
 * to find the first node higher up where we haven't yet gone through
 * all the slots
 */
520 521
static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root
			*root, struct btrfs_path *path, int *level)
C
Chris Mason 已提交
522 523 524 525
{
	int i;
	int slot;
	int ret;
C
Chris Mason 已提交
526
	for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
C
Chris Mason 已提交
527
		slot = path->slots[i];
528 529
		if (slot <
		    btrfs_header_nritems(&path->nodes[i]->node.header)- 1) {
C
Chris Mason 已提交
530 531 532 533
			path->slots[i]++;
			*level = i;
			return 0;
		} else {
534 535 536
			ret = btrfs_free_extent(trans, root,
						path->nodes[*level]->blocknr,
						1, 1);
C
Chris Mason 已提交
537
			btrfs_block_release(root, path->nodes[*level]);
C
Chris Mason 已提交
538
			path->nodes[*level] = NULL;
C
Chris Mason 已提交
539 540 541 542 543 544 545
			*level = i + 1;
			BUG_ON(ret);
		}
	}
	return 1;
}

C
Chris Mason 已提交
546 547 548 549 550
/*
 * drop the reference count on the tree rooted at 'snap'.  This traverses
 * the tree freeing any blocks that have a ref count of zero after being
 * decremented.
 */
551 552
int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
			*root, struct btrfs_buffer *snap)
C
Chris Mason 已提交
553
{
554
	int ret = 0;
C
Chris Mason 已提交
555
	int wret;
C
Chris Mason 已提交
556
	int level;
C
Chris Mason 已提交
557
	struct btrfs_path path;
C
Chris Mason 已提交
558 559 560
	int i;
	int orig_level;

C
Chris Mason 已提交
561
	btrfs_init_path(&path);
C
Chris Mason 已提交
562

563
	level = btrfs_header_level(&snap->node.header);
C
Chris Mason 已提交
564 565 566 567
	orig_level = level;
	path.nodes[level] = snap;
	path.slots[level] = 0;
	while(1) {
568
		wret = walk_down_tree(trans, root, &path, &level);
C
Chris Mason 已提交
569
		if (wret > 0)
C
Chris Mason 已提交
570
			break;
C
Chris Mason 已提交
571 572 573
		if (wret < 0)
			ret = wret;

574
		wret = walk_up_tree(trans, root, &path, &level);
C
Chris Mason 已提交
575
		if (wret > 0)
C
Chris Mason 已提交
576
			break;
C
Chris Mason 已提交
577 578
		if (wret < 0)
			ret = wret;
C
Chris Mason 已提交
579
	}
C
Chris Mason 已提交
580 581
	for (i = 0; i <= orig_level; i++) {
		if (path.nodes[i]) {
C
Chris Mason 已提交
582
			btrfs_block_release(root, path.nodes[i]);
C
Chris Mason 已提交
583
		}
C
Chris Mason 已提交
584
	}
C
Chris Mason 已提交
585
	return ret;
C
Chris Mason 已提交
586
}