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 173 174 175 176 177 178
	BUG_ON(err);
	if (err)
		return err;
	radix_tree_tag_set(&root->fs_info->pinned_radix, blocknr,
			   tag);
	return 0;
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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