extent-tree.c 16.1 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
	mark_buffer_dirty(path.nodes[0]);
53

54 55
	btrfs_release_path(root->fs_info->extent_root, &path);
	finish_current_insert(trans, root->fs_info->extent_root);
C
Chris Mason 已提交
56
	del_pending_extents(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
	if (ret != 0)
		BUG();
C
Chris Mason 已提交
77
	l = btrfs_buffer_leaf(path.nodes[0]);
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
int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
C
Chris Mason 已提交
85
		  struct buffer_head *buf)
C
Chris Mason 已提交
86 87
{
	u64 blocknr;
C
Chris Mason 已提交
88
	struct btrfs_node *buf_node;
C
Chris Mason 已提交
89
	int i;
90

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

C
Chris Mason 已提交
97 98
	for (i = 0; i < btrfs_header_nritems(&buf_node->header); i++) {
		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
	struct buffer_head *gang[8];
108
	u64 first = 0;
109 110 111 112
	int ret;
	int i;

	while(1) {
C
Chris Mason 已提交
113
		ret = radix_tree_gang_lookup_tag(&root->fs_info->pinned_radix,
114
					     (void **)gang, 0,
C
Chris Mason 已提交
115 116
					     ARRAY_SIZE(gang),
					     CTREE_EXTENT_PINNED);
117 118
		if (!ret)
			break;
119
		if (!first)
120
			first = gang[0]->b_blocknr;
121
		for (i = 0; i < ret; i++) {
122
			radix_tree_delete(&root->fs_info->pinned_radix,
123 124
					  gang[i]->b_blocknr);
			brelse(gang[i]);
125
		}
126
	}
127 128
	if (root->fs_info->last_insert.objectid > first)
		root->fs_info->last_insert.objectid = first;
129
	root->fs_info->last_insert.offset = 0;
130 131 132
	return 0;
}

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

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

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

C
Chris Mason 已提交
164 165 166
static int pin_down_block(struct btrfs_root *root, u64 blocknr, int tag)
{
	int err;
167 168
	struct buffer_head *bh = sb_getblk(root->fs_info->sb, blocknr);
	BUG_ON(!bh);
C
Chris Mason 已提交
169
	err = radix_tree_insert(&root->fs_info->pinned_radix,
170
				blocknr, bh);
C
Chris Mason 已提交
171 172
	if (err && err != -EEXIST) {
		BUG();
C
Chris Mason 已提交
173
		return err;
C
Chris Mason 已提交
174
	}
C
Chris Mason 已提交
175 176 177 178 179
	radix_tree_tag_set(&root->fs_info->pinned_radix, blocknr,
			   tag);
	return 0;
}

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

195 196
	key.objectid = blocknr;
	key.flags = 0;
197
	btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
198 199
	key.offset = num_blocks;

200
	find_free_extent(trans, root, 0, 0, (u64)-1, &ins);
C
Chris Mason 已提交
201
	btrfs_init_path(&path);
202
	ret = btrfs_search_slot(trans, extent_root, &key, &path, -1, 1);
203
	if (ret) {
204
		printk("failed to find %Lu\n", key.objectid);
C
Chris Mason 已提交
205
		btrfs_print_tree(extent_root, extent_root->node);
206
		printk("failed to find %Lu\n", key.objectid);
207 208
		BUG();
	}
C
Chris Mason 已提交
209
	ei = btrfs_item_ptr(btrfs_buffer_leaf(path.nodes[0]), path.slots[0],
C
Chris Mason 已提交
210
			    struct btrfs_extent_item);
211
	BUG_ON(ei->refs == 0);
C
Chris Mason 已提交
212 213 214
	refs = btrfs_extent_refs(ei) - 1;
	btrfs_set_extent_refs(ei, refs);
	if (refs == 0) {
215 216 217 218
		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);
219
		ret = btrfs_del_item(trans, extent_root, &path);
C
Chris Mason 已提交
220
		if (extent_root->fs_info->last_insert.objectid >
221 222
		    blocknr)
			extent_root->fs_info->last_insert.objectid = blocknr;
223 224 225
		if (ret)
			BUG();
	}
226
	mark_buffer_dirty(path.nodes[0]);
C
Chris Mason 已提交
227
	btrfs_release_path(extent_root, &path);
228
	finish_current_insert(trans, extent_root);
229 230 231 232 233 234 235
	return ret;
}

/*
 * find all the blocks marked as pending in the radix tree and remove
 * them from the extent map
 */
236 237
static int del_pending_extents(struct btrfs_trans_handle *trans, struct
			       btrfs_root *extent_root)
238 239
{
	int ret;
C
Chris Mason 已提交
240 241
	int wret;
	int err = 0;
242
	struct buffer_head *gang[4];
243
	int i;
C
Chris Mason 已提交
244
	struct radix_tree_root *radix = &extent_root->fs_info->pinned_radix;
245 246

	while(1) {
247
		ret = radix_tree_gang_lookup_tag(
C
Chris Mason 已提交
248
					&extent_root->fs_info->pinned_radix,
249 250 251
					(void **)gang, 0,
					ARRAY_SIZE(gang),
					CTREE_EXTENT_PENDING_DEL);
252 253 254
		if (!ret)
			break;
		for (i = 0; i < ret; i++) {
255 256 257
			radix_tree_tag_set(radix, gang[i]->b_blocknr,
					   CTREE_EXTENT_PINNED);
			radix_tree_tag_clear(radix, gang[i]->b_blocknr,
258
					     CTREE_EXTENT_PENDING_DEL);
259 260
			wret = __free_extent(trans, extent_root,
					     gang[i]->b_blocknr, 1);
C
Chris Mason 已提交
261 262
			if (wret)
				err = wret;
263 264
		}
	}
C
Chris Mason 已提交
265
	return err;
266 267 268 269 270
}

/*
 * remove an extent from the root, returns 0 on success
 */
271 272
int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
		      *root, u64 blocknr, u64 num_blocks, int pin)
273
{
274
	struct btrfs_root *extent_root = root->fs_info->extent_root;
C
Chris Mason 已提交
275
	struct buffer_head *t;
276 277
	int pending_ret;
	int ret;
278

279
	if (root == extent_root) {
280
		t = find_tree_block(root, blocknr);
C
Chris Mason 已提交
281
		pin_down_block(root, blocknr, CTREE_EXTENT_PENDING_DEL);
282 283
		return 0;
	}
C
Chris Mason 已提交
284 285 286 287 288 289
	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);
290 291 292 293 294 295 296
	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
297
 * ins->flags = BTRFS_EXTENT_ITEM_KEY
298 299 300
 * ins->offset == number of blocks
 * Any available blocks before search_start are skipped.
 */
301 302 303
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)
304
{
C
Chris Mason 已提交
305
	struct btrfs_path path;
C
Chris Mason 已提交
306
	struct btrfs_key key;
307 308 309
	int ret;
	u64 hole_size = 0;
	int slot = 0;
C
Chris Mason 已提交
310
	u64 last_block = 0;
C
Chris Mason 已提交
311
	u64 test_block;
312
	int start_found;
C
Chris Mason 已提交
313
	struct btrfs_leaf *l;
314
	struct btrfs_root * root = orig_root->fs_info->extent_root;
315
	int total_needed = num_blocks;
C
Chris Mason 已提交
316
	int level;
317

C
Chris Mason 已提交
318 319
	level = btrfs_header_level(btrfs_buffer_header(root->node));
	total_needed += (level + 1) * 3;
320 321
	if (root->fs_info->last_insert.objectid > search_start)
		search_start = root->fs_info->last_insert.objectid;
322 323 324 325

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

326
check_failed:
C
Chris Mason 已提交
327
	btrfs_init_path(&path);
328 329 330
	ins->objectid = search_start;
	ins->offset = 0;
	start_found = 0;
331
	ret = btrfs_search_slot(trans, root, ins, &path, 0, 0);
C
Chris Mason 已提交
332 333
	if (ret < 0)
		goto error;
C
Chris Mason 已提交
334

335 336 337
	if (path.slots[0] > 0)
		path.slots[0]--;

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

/*
 * 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.
 */
409 410 411
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)
412 413 414
{
	int ret;
	int pending_ret;
415 416 417
	u64 super_blocks_used;
	struct btrfs_fs_info *info = root->fs_info;
	struct btrfs_root *extent_root = info->extent_root;
C
Chris Mason 已提交
418
	struct btrfs_extent_item extent_item;
C
Chris Mason 已提交
419

C
Chris Mason 已提交
420 421
	btrfs_set_extent_refs(&extent_item, 1);
	btrfs_set_extent_owner(&extent_item, owner);
422

C
Chris Mason 已提交
423
	if (root == extent_root) {
424
		BUG_ON(extent_root->fs_info->current_insert.offset == 0);
C
Chris Mason 已提交
425
		BUG_ON(num_blocks != 1);
426 427
		BUG_ON(extent_root->fs_info->current_insert.flags ==
		       extent_root->fs_info->current_insert.offset);
C
Chris Mason 已提交
428
		ins->offset = 1;
429 430
		ins->objectid = extent_root->fs_info->current_insert.objectid +
				extent_root->fs_info->current_insert.flags++;
431 432
		return 0;
	}
433
	ret = find_free_extent(trans, root, num_blocks, search_start,
C
Chris Mason 已提交
434 435 436
			       search_end, ins);
	if (ret)
		return ret;
437

438 439 440
	super_blocks_used = btrfs_super_blocks_used(info->disk_super);
	btrfs_set_super_blocks_used(info->disk_super, super_blocks_used +
				    num_blocks);
441 442
	ret = btrfs_insert_item(trans, extent_root, ins, &extent_item,
				sizeof(extent_item));
C
Chris Mason 已提交
443

444
	finish_current_insert(trans, extent_root);
C
Chris Mason 已提交
445
	pending_ret = del_pending_extents(trans, extent_root);
C
Chris Mason 已提交
446 447 448 449 450
	if (ret)
		return ret;
	if (pending_ret)
		return pending_ret;
	return 0;
451 452 453 454 455 456
}

/*
 * helper function to allocate a block for a given tree
 * returns the tree buffer or NULL.
 */
C
Chris Mason 已提交
457
struct buffer_head *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
458
					    struct btrfs_root *root)
459
{
C
Chris Mason 已提交
460
	struct btrfs_key ins;
461
	int ret;
C
Chris Mason 已提交
462
	struct buffer_head *buf;
463

464
	ret = alloc_extent(trans, root, 1, 0, (unsigned long)-1,
C
Chris Mason 已提交
465
		btrfs_header_parentid(btrfs_buffer_header(root->node)), &ins);
466 467 468 469
	if (ret) {
		BUG();
		return NULL;
	}
C
Chris Mason 已提交
470
	buf = find_tree_block(root, ins.objectid);
471
	set_buffer_uptodate(buf);
472 473
	return buf;
}
474

C
Chris Mason 已提交
475 476 477 478
/*
 * helper function for drop_snapshot, this walks down the tree dropping ref
 * counts as it goes.
 */
479 480
static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
			  *root, struct btrfs_path *path, int *level)
C
Chris Mason 已提交
481
{
C
Chris Mason 已提交
482 483
	struct buffer_head *next;
	struct buffer_head *cur;
C
Chris Mason 已提交
484 485 486 487
	u64 blocknr;
	int ret;
	u32 refs;

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

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

C
Chris Mason 已提交
559 560 561 562 563
/*
 * 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.
 */
564
int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
C
Chris Mason 已提交
565
			*root, struct buffer_head *snap)
C
Chris Mason 已提交
566
{
567
	int ret = 0;
C
Chris Mason 已提交
568
	int wret;
C
Chris Mason 已提交
569
	int level;
C
Chris Mason 已提交
570
	struct btrfs_path path;
C
Chris Mason 已提交
571 572 573
	int i;
	int orig_level;

C
Chris Mason 已提交
574
	btrfs_init_path(&path);
C
Chris Mason 已提交
575

C
Chris Mason 已提交
576
	level = btrfs_header_level(btrfs_buffer_header(snap));
C
Chris Mason 已提交
577 578 579 580
	orig_level = level;
	path.nodes[level] = snap;
	path.slots[level] = 0;
	while(1) {
581
		wret = walk_down_tree(trans, root, &path, &level);
C
Chris Mason 已提交
582
		if (wret > 0)
C
Chris Mason 已提交
583
			break;
C
Chris Mason 已提交
584 585 586
		if (wret < 0)
			ret = wret;

587
		wret = walk_up_tree(trans, root, &path, &level);
C
Chris Mason 已提交
588
		if (wret > 0)
C
Chris Mason 已提交
589
			break;
C
Chris Mason 已提交
590 591
		if (wret < 0)
			ret = wret;
C
Chris Mason 已提交
592
	}
C
Chris Mason 已提交
593 594
	for (i = 0; i <= orig_level; i++) {
		if (path.nodes[i]) {
C
Chris Mason 已提交
595
			btrfs_block_release(root, path.nodes[i]);
C
Chris Mason 已提交
596
		}
C
Chris Mason 已提交
597
	}
C
Chris Mason 已提交
598
	return ret;
C
Chris Mason 已提交
599
}