extent-tree.c 15.7 KB
Newer Older
1 2 3 4 5 6 7
#include <stdio.h>
#include <stdlib.h>
#include "kerncompat.h"
#include "radix-tree.h"
#include "ctree.h"
#include "disk-io.h"
#include "print-tree.h"
8
#include "transaction.h"
9

10 11 12 13 14 15 16
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 已提交
17

18 19 20 21 22 23 24
/*
 * 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 已提交
25
#define CTREE_EXTENT_PENDING_DEL 0
26

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

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

C
Chris Mason 已提交
55
	BUG_ON(list_empty(&path.nodes[0]->dirty));
56 57 58
	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 已提交
59 60 61
	return 0;
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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


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

273
	if (root == extent_root) {
274
		t = find_tree_block(root, blocknr);
275
		radix_tree_tag_set(&root->fs_info->cache_radix, blocknr,
276
				   CTREE_EXTENT_PENDING_DEL);
277 278
		return 0;
	}
279
	ret = __free_extent(trans, root, blocknr, num_blocks, pin);
280
	pending_ret = run_pending(trans, root->fs_info->extent_root);
281 282 283 284 285 286 287
	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
288
 * ins->flags = BTRFS_EXTENT_ITEM_KEY
289 290 291
 * ins->offset == number of blocks
 * Any available blocks before search_start are skipped.
 */
292 293 294
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)
295
{
C
Chris Mason 已提交
296
	struct btrfs_path path;
C
Chris Mason 已提交
297
	struct btrfs_key key;
298 299 300 301
	int ret;
	u64 hole_size = 0;
	int slot = 0;
	u64 last_block;
C
Chris Mason 已提交
302
	u64 test_block;
303
	int start_found;
C
Chris Mason 已提交
304
	struct btrfs_leaf *l;
305
	struct btrfs_root * root = orig_root->fs_info->extent_root;
306
	int total_needed = num_blocks;
307

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

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

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

324 325 326
	if (path.slots[0] > 0)
		path.slots[0]--;

327 328 329
	while (1) {
		l = &path.nodes[0]->leaf;
		slot = path.slots[0];
330
		if (slot >= btrfs_header_nritems(&l->header)) {
C
Chris Mason 已提交
331
			ret = btrfs_next_leaf(root, &path);
332 333
			if (ret == 0)
				continue;
C
Chris Mason 已提交
334 335
			if (ret < 0)
				goto error;
336 337
			if (!start_found) {
				ins->objectid = search_start;
C
Chris Mason 已提交
338
				ins->offset = (u64)-1;
339 340 341 342 343
				start_found = 1;
				goto check_pending;
			}
			ins->objectid = last_block > search_start ?
					last_block : search_start;
C
Chris Mason 已提交
344
			ins->offset = (u64)-1;
345 346
			goto check_pending;
		}
C
Chris Mason 已提交
347 348
		btrfs_disk_key_to_cpu(&key, &l->items[slot].key);
		if (key.objectid >= search_start) {
349
			if (start_found) {
350 351
				if (last_block < search_start)
					last_block = search_start;
C
Chris Mason 已提交
352
				hole_size = key.objectid - last_block;
C
Chris Mason 已提交
353
				if (hole_size > total_needed) {
354
					ins->objectid = last_block;
C
Chris Mason 已提交
355
					ins->offset = hole_size;
356 357
					goto check_pending;
				}
358
			}
359
		}
360
		start_found = 1;
C
Chris Mason 已提交
361
		last_block = key.objectid + key.offset;
362 363 364 365 366 367 368
		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 已提交
369
	btrfs_release_path(root, &path);
370
	BUG_ON(ins->objectid < search_start);
C
Chris Mason 已提交
371 372
	for (test_block = ins->objectid;
	     test_block < ins->objectid + total_needed; test_block++) {
373 374
		if (radix_tree_lookup(&root->fs_info->pinned_radix,
				      test_block)) {
C
Chris Mason 已提交
375
			search_start = test_block + 1;
376 377 378
			goto check_failed;
		}
	}
379 380 381 382 383
	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 已提交
384
	ins->offset = num_blocks;
385
	return 0;
C
Chris Mason 已提交
386
error:
C
Chris Mason 已提交
387
	btrfs_release_path(root, &path);
C
Chris Mason 已提交
388
	return ret;
389 390 391 392 393 394 395 396 397
}

/*
 * 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.
 */
398 399 400
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)
401 402 403
{
	int ret;
	int pending_ret;
404 405 406
	u64 super_blocks_used;
	struct btrfs_fs_info *info = root->fs_info;
	struct btrfs_root *extent_root = info->extent_root;
C
Chris Mason 已提交
407
	struct btrfs_extent_item extent_item;
C
Chris Mason 已提交
408

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

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

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

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

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

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

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

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

C
Chris Mason 已提交
517 518 519 520 521
/*
 * 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
 */
522 523
static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root
			*root, struct btrfs_path *path, int *level)
C
Chris Mason 已提交
524 525 526 527
{
	int i;
	int slot;
	int ret;
C
Chris Mason 已提交
528
	for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
C
Chris Mason 已提交
529
		slot = path->slots[i];
530 531
		if (slot <
		    btrfs_header_nritems(&path->nodes[i]->node.header)- 1) {
C
Chris Mason 已提交
532 533 534 535
			path->slots[i]++;
			*level = i;
			return 0;
		} else {
536 537 538
			ret = btrfs_free_extent(trans, root,
						path->nodes[*level]->blocknr,
						1, 1);
C
Chris Mason 已提交
539
			btrfs_block_release(root, path->nodes[*level]);
C
Chris Mason 已提交
540
			path->nodes[*level] = NULL;
C
Chris Mason 已提交
541 542 543 544 545 546 547
			*level = i + 1;
			BUG_ON(ret);
		}
	}
	return 1;
}

C
Chris Mason 已提交
548 549 550 551 552
/*
 * 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.
 */
553 554
int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
			*root, struct btrfs_buffer *snap)
C
Chris Mason 已提交
555
{
556
	int ret = 0;
C
Chris Mason 已提交
557
	int wret;
C
Chris Mason 已提交
558
	int level;
C
Chris Mason 已提交
559
	struct btrfs_path path;
C
Chris Mason 已提交
560 561 562
	int i;
	int orig_level;

C
Chris Mason 已提交
563
	btrfs_init_path(&path);
C
Chris Mason 已提交
564

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

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