extent-tree.c 15.8 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
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);
C
Chris Mason 已提交
13 14
static int del_pending_extents(struct btrfs_trans_handle *trans, struct
			       btrfs_root *extent_root);
15 16 17 18 19 20 21
/*
 * 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 已提交
22
#define CTREE_EXTENT_PENDING_DEL 0
C
Chris Mason 已提交
23
#define CTREE_EXTENT_PINNED 1
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
	BUG_ON(ret != 0);
C
Chris Mason 已提交
48
	l = btrfs_buffer_leaf(path.nodes[0]);
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

53 54
	btrfs_release_path(root->fs_info->extent_root, &path);
	finish_current_insert(trans, root->fs_info->extent_root);
C
Chris Mason 已提交
55
	del_pending_extents(trans, root->fs_info->extent_root);
C
Chris Mason 已提交
56 57 58
	return 0;
}

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

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

90
	if (!root->ref_cows)
91
		return 0;
C
Chris Mason 已提交
92 93
	buf_node = btrfs_buffer_node(buf);
	if (btrfs_is_leaf(buf_node))
94 95
		return 0;

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

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

	while(1) {
C
Chris Mason 已提交
112
		ret = radix_tree_gang_lookup_tag(&root->fs_info->pinned_radix,
113
					     (void **)gang, 0,
C
Chris Mason 已提交
114 115
					     ARRAY_SIZE(gang),
					     CTREE_EXTENT_PINNED);
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
	btrfs_set_extent_refs(&extent_item, 1);
	btrfs_set_extent_owner(&extent_item,
C
Chris Mason 已提交
142
		btrfs_header_parentid(btrfs_buffer_header(extent_root->node)));
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;
}

C
Chris Mason 已提交
161 162 163 164 165 166 167 168 169 170 171 172 173
static int pin_down_block(struct btrfs_root *root, u64 blocknr, int tag)
{
	int err;
	err = radix_tree_insert(&root->fs_info->pinned_radix,
				blocknr, (void *)blocknr);
	BUG_ON(err);
	if (err)
		return err;
	radix_tree_tag_set(&root->fs_info->pinned_radix, blocknr,
			   tag);
	return 0;
}

174
/*
175
 * remove an extent from the root, returns 0 on success
176
 */
177
static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
C
Chris Mason 已提交
178
			 *root, u64 blocknr, u64 num_blocks)
179
{
C
Chris Mason 已提交
180
	struct btrfs_path path;
C
Chris Mason 已提交
181
	struct btrfs_key key;
182 183
	struct btrfs_fs_info *info = root->fs_info;
	struct btrfs_root *extent_root = info->extent_root;
184
	int ret;
C
Chris Mason 已提交
185
	struct btrfs_extent_item *ei;
C
Chris Mason 已提交
186
	struct btrfs_key ins;
C
Chris Mason 已提交
187
	u32 refs;
C
Chris Mason 已提交
188

189 190
	key.objectid = blocknr;
	key.flags = 0;
191
	btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
192 193
	key.offset = num_blocks;

194
	find_free_extent(trans, root, 0, 0, (u64)-1, &ins);
C
Chris Mason 已提交
195
	btrfs_init_path(&path);
196
	ret = btrfs_search_slot(trans, extent_root, &key, &path, -1, 1);
197
	if (ret) {
198
		printk("failed to find %Lu\n", key.objectid);
C
Chris Mason 已提交
199
		btrfs_print_tree(extent_root, extent_root->node);
200
		printk("failed to find %Lu\n", key.objectid);
201 202
		BUG();
	}
C
Chris Mason 已提交
203
	ei = btrfs_item_ptr(btrfs_buffer_leaf(path.nodes[0]), path.slots[0],
C
Chris Mason 已提交
204
			    struct btrfs_extent_item);
205
	BUG_ON(ei->refs == 0);
C
Chris Mason 已提交
206 207 208
	refs = btrfs_extent_refs(ei) - 1;
	btrfs_set_extent_refs(ei, refs);
	if (refs == 0) {
209 210 211 212
		u64 super_blocks_used;
		super_blocks_used = btrfs_super_blocks_used(info->disk_super);
		btrfs_set_super_blocks_used(info->disk_super,
					    super_blocks_used - num_blocks);
213
		ret = btrfs_del_item(trans, extent_root, &path);
C
Chris Mason 已提交
214
		if (extent_root->fs_info->last_insert.objectid >
215 216
		    blocknr)
			extent_root->fs_info->last_insert.objectid = blocknr;
217 218 219
		if (ret)
			BUG();
	}
C
Chris Mason 已提交
220
	btrfs_release_path(extent_root, &path);
221
	finish_current_insert(trans, extent_root);
222 223 224 225 226 227 228
	return ret;
}

/*
 * find all the blocks marked as pending in the radix tree and remove
 * them from the extent map
 */
229 230
static int del_pending_extents(struct btrfs_trans_handle *trans, struct
			       btrfs_root *extent_root)
231 232
{
	int ret;
C
Chris Mason 已提交
233 234 235
	int wret;
	int err = 0;
	unsigned long gang[4];
236
	int i;
C
Chris Mason 已提交
237
	struct radix_tree_root *radix = &extent_root->fs_info->pinned_radix;
238 239

	while(1) {
240
		ret = radix_tree_gang_lookup_tag(
C
Chris Mason 已提交
241
					&extent_root->fs_info->pinned_radix,
242 243 244
					(void **)gang, 0,
					ARRAY_SIZE(gang),
					CTREE_EXTENT_PENDING_DEL);
245 246 247
		if (!ret)
			break;
		for (i = 0; i < ret; i++) {
C
Chris Mason 已提交
248 249
			radix_tree_tag_set(radix, gang[i], CTREE_EXTENT_PINNED);
			radix_tree_tag_clear(radix, gang[i],
250
					     CTREE_EXTENT_PENDING_DEL);
C
Chris Mason 已提交
251 252 253
			wret = __free_extent(trans, extent_root, gang[i], 1);
			if (wret)
				err = wret;
254 255
		}
	}
C
Chris Mason 已提交
256
	return err;
257 258 259 260 261
}

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

270
	if (root == extent_root) {
271
		t = find_tree_block(root, blocknr);
C
Chris Mason 已提交
272
		pin_down_block(root, blocknr, CTREE_EXTENT_PENDING_DEL);
273 274
		return 0;
	}
C
Chris Mason 已提交
275 276 277 278 279 280
	if (pin) {
		ret = pin_down_block(root, blocknr, CTREE_EXTENT_PINNED);
		BUG_ON(ret);
	}
	ret = __free_extent(trans, root, blocknr, num_blocks);
	pending_ret = del_pending_extents(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
	int ret;
	u64 hole_size = 0;
	int slot = 0;
C
Chris Mason 已提交
301
	u64 last_block = 0;
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;
C
Chris Mason 已提交
307
	int level;
308

C
Chris Mason 已提交
309 310
	level = btrfs_header_level(btrfs_buffer_header(root->node));
	total_needed += (level + 1) * 3;
311 312
	if (root->fs_info->last_insert.objectid > search_start)
		search_start = root->fs_info->last_insert.objectid;
313 314 315 316

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

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

326 327 328
	if (path.slots[0] > 0)
		path.slots[0]--;

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

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

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

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

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

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

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

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

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

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

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

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

C
Chris Mason 已提交
565
	btrfs_init_path(&path);
C
Chris Mason 已提交
566

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

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