extent-tree.c 16.3 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
	struct buffer_head *bh = sb_getblk(root->fs_info->sb, blocknr);
C
Chris Mason 已提交
168
	struct btrfs_header *header;
169
	BUG_ON(!bh);
C
Chris Mason 已提交
170 171 172 173 174 175 176

	header = btrfs_buffer_header(bh);
	if (btrfs_header_generation(header) ==
	    root->fs_info->running_transaction->transid) {
		return 0;
	}

C
Chris Mason 已提交
177
	err = radix_tree_insert(&root->fs_info->pinned_radix,
178
				blocknr, bh);
C
Chris Mason 已提交
179 180
	if (err && err != -EEXIST) {
		BUG();
C
Chris Mason 已提交
181
		return err;
C
Chris Mason 已提交
182
	}
C
Chris Mason 已提交
183 184
	if (err == -EEXIST)
		brelse(bh);
C
Chris Mason 已提交
185 186 187 188 189
	radix_tree_tag_set(&root->fs_info->pinned_radix, blocknr,
			   tag);
	return 0;
}

190
/*
191
 * remove an extent from the root, returns 0 on success
192
 */
193
static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
C
Chris Mason 已提交
194
			 *root, u64 blocknr, u64 num_blocks, int pin)
195
{
C
Chris Mason 已提交
196
	struct btrfs_path path;
C
Chris Mason 已提交
197
	struct btrfs_key key;
198 199
	struct btrfs_fs_info *info = root->fs_info;
	struct btrfs_root *extent_root = info->extent_root;
200
	int ret;
C
Chris Mason 已提交
201
	struct btrfs_extent_item *ei;
C
Chris Mason 已提交
202
	struct btrfs_key ins;
C
Chris Mason 已提交
203
	u32 refs;
C
Chris Mason 已提交
204

205 206
	key.objectid = blocknr;
	key.flags = 0;
207
	btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
208 209
	key.offset = num_blocks;

210
	find_free_extent(trans, root, 0, 0, (u64)-1, &ins);
C
Chris Mason 已提交
211
	btrfs_init_path(&path);
212
	ret = btrfs_search_slot(trans, extent_root, &key, &path, -1, 1);
213
	if (ret) {
214
		printk("failed to find %Lu\n", key.objectid);
C
Chris Mason 已提交
215
		btrfs_print_tree(extent_root, extent_root->node);
216
		printk("failed to find %Lu\n", key.objectid);
217 218
		BUG();
	}
C
Chris Mason 已提交
219
	ei = btrfs_item_ptr(btrfs_buffer_leaf(path.nodes[0]), path.slots[0],
C
Chris Mason 已提交
220
			    struct btrfs_extent_item);
221
	BUG_ON(ei->refs == 0);
C
Chris Mason 已提交
222 223 224
	refs = btrfs_extent_refs(ei) - 1;
	btrfs_set_extent_refs(ei, refs);
	if (refs == 0) {
225
		u64 super_blocks_used;
C
Chris Mason 已提交
226 227 228 229 230 231 232

		if (pin) {
			ret = pin_down_block(root, blocknr,
					     CTREE_EXTENT_PINNED);
			BUG_ON(ret);
		}

233 234 235
		super_blocks_used = btrfs_super_blocks_used(info->disk_super);
		btrfs_set_super_blocks_used(info->disk_super,
					    super_blocks_used - num_blocks);
236
		ret = btrfs_del_item(trans, extent_root, &path);
C
Chris Mason 已提交
237
		if (extent_root->fs_info->last_insert.objectid > blocknr)
238
			extent_root->fs_info->last_insert.objectid = blocknr;
239 240 241
		if (ret)
			BUG();
	}
242
	mark_buffer_dirty(path.nodes[0]);
C
Chris Mason 已提交
243
	btrfs_release_path(extent_root, &path);
244
	finish_current_insert(trans, extent_root);
245 246 247 248 249 250 251
	return ret;
}

/*
 * find all the blocks marked as pending in the radix tree and remove
 * them from the extent map
 */
252 253
static int del_pending_extents(struct btrfs_trans_handle *trans, struct
			       btrfs_root *extent_root)
254 255
{
	int ret;
C
Chris Mason 已提交
256 257
	int wret;
	int err = 0;
258
	struct buffer_head *gang[4];
259
	int i;
C
Chris Mason 已提交
260
	struct radix_tree_root *radix = &extent_root->fs_info->pinned_radix;
261 262

	while(1) {
263
		ret = radix_tree_gang_lookup_tag(
C
Chris Mason 已提交
264
					&extent_root->fs_info->pinned_radix,
265 266 267
					(void **)gang, 0,
					ARRAY_SIZE(gang),
					CTREE_EXTENT_PENDING_DEL);
268 269 270
		if (!ret)
			break;
		for (i = 0; i < ret; i++) {
271 272 273
			radix_tree_tag_set(radix, gang[i]->b_blocknr,
					   CTREE_EXTENT_PINNED);
			radix_tree_tag_clear(radix, gang[i]->b_blocknr,
274
					     CTREE_EXTENT_PENDING_DEL);
275
			wret = __free_extent(trans, extent_root,
C
Chris Mason 已提交
276
					     gang[i]->b_blocknr, 1, 0);
C
Chris Mason 已提交
277 278
			if (wret)
				err = wret;
279 280
		}
	}
C
Chris Mason 已提交
281
	return err;
282 283 284 285 286
}

/*
 * remove an extent from the root, returns 0 on success
 */
287 288
int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
		      *root, u64 blocknr, u64 num_blocks, int pin)
289
{
290
	struct btrfs_root *extent_root = root->fs_info->extent_root;
C
Chris Mason 已提交
291
	struct buffer_head *t;
292 293
	int pending_ret;
	int ret;
294

295
	if (root == extent_root) {
296
		t = find_tree_block(root, blocknr);
C
Chris Mason 已提交
297
		pin_down_block(root, blocknr, CTREE_EXTENT_PENDING_DEL);
298 299
		return 0;
	}
C
Chris Mason 已提交
300
	ret = __free_extent(trans, root, blocknr, num_blocks, pin);
C
Chris Mason 已提交
301
	pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
302 303 304 305 306 307 308
	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
309
 * ins->flags = BTRFS_EXTENT_ITEM_KEY
310 311 312
 * ins->offset == number of blocks
 * Any available blocks before search_start are skipped.
 */
313 314 315
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)
316
{
C
Chris Mason 已提交
317
	struct btrfs_path path;
C
Chris Mason 已提交
318
	struct btrfs_key key;
319 320 321
	int ret;
	u64 hole_size = 0;
	int slot = 0;
C
Chris Mason 已提交
322
	u64 last_block = 0;
C
Chris Mason 已提交
323
	u64 test_block;
324
	int start_found;
C
Chris Mason 已提交
325
	struct btrfs_leaf *l;
326
	struct btrfs_root * root = orig_root->fs_info->extent_root;
327
	int total_needed = num_blocks;
C
Chris Mason 已提交
328
	int level;
329

C
Chris Mason 已提交
330 331
	level = btrfs_header_level(btrfs_buffer_header(root->node));
	total_needed += (level + 1) * 3;
332 333
	if (root->fs_info->last_insert.objectid > search_start)
		search_start = root->fs_info->last_insert.objectid;
334 335 336 337

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

338
check_failed:
C
Chris Mason 已提交
339
	btrfs_init_path(&path);
340 341 342
	ins->objectid = search_start;
	ins->offset = 0;
	start_found = 0;
343
	ret = btrfs_search_slot(trans, root, ins, &path, 0, 0);
C
Chris Mason 已提交
344 345
	if (ret < 0)
		goto error;
C
Chris Mason 已提交
346

347 348 349
	if (path.slots[0] > 0)
		path.slots[0]--;

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

/*
 * 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.
 */
421 422 423
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)
424 425 426
{
	int ret;
	int pending_ret;
427 428 429
	u64 super_blocks_used;
	struct btrfs_fs_info *info = root->fs_info;
	struct btrfs_root *extent_root = info->extent_root;
C
Chris Mason 已提交
430
	struct btrfs_extent_item extent_item;
C
Chris Mason 已提交
431

C
Chris Mason 已提交
432 433
	btrfs_set_extent_refs(&extent_item, 1);
	btrfs_set_extent_owner(&extent_item, owner);
434

C
Chris Mason 已提交
435
	if (root == extent_root) {
436
		BUG_ON(extent_root->fs_info->current_insert.offset == 0);
C
Chris Mason 已提交
437
		BUG_ON(num_blocks != 1);
438 439
		BUG_ON(extent_root->fs_info->current_insert.flags ==
		       extent_root->fs_info->current_insert.offset);
C
Chris Mason 已提交
440
		ins->offset = 1;
441 442
		ins->objectid = extent_root->fs_info->current_insert.objectid +
				extent_root->fs_info->current_insert.flags++;
443 444
		return 0;
	}
445
	ret = find_free_extent(trans, root, num_blocks, search_start,
C
Chris Mason 已提交
446 447 448
			       search_end, ins);
	if (ret)
		return ret;
449

450 451 452
	super_blocks_used = btrfs_super_blocks_used(info->disk_super);
	btrfs_set_super_blocks_used(info->disk_super, super_blocks_used +
				    num_blocks);
453 454
	ret = btrfs_insert_item(trans, extent_root, ins, &extent_item,
				sizeof(extent_item));
C
Chris Mason 已提交
455

456
	finish_current_insert(trans, extent_root);
C
Chris Mason 已提交
457
	pending_ret = del_pending_extents(trans, extent_root);
C
Chris Mason 已提交
458 459 460 461 462
	if (ret)
		return ret;
	if (pending_ret)
		return pending_ret;
	return 0;
463 464 465 466 467 468
}

/*
 * helper function to allocate a block for a given tree
 * returns the tree buffer or NULL.
 */
C
Chris Mason 已提交
469
struct buffer_head *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
470
					    struct btrfs_root *root)
471
{
C
Chris Mason 已提交
472
	struct btrfs_key ins;
473
	int ret;
C
Chris Mason 已提交
474
	struct buffer_head *buf;
475

476
	ret = alloc_extent(trans, root, 1, 0, (unsigned long)-1,
C
Chris Mason 已提交
477
		btrfs_header_parentid(btrfs_buffer_header(root->node)), &ins);
478 479 480 481
	if (ret) {
		BUG();
		return NULL;
	}
C
Chris Mason 已提交
482
	buf = find_tree_block(root, ins.objectid);
483
	set_buffer_uptodate(buf);
484 485
	return buf;
}
486

C
Chris Mason 已提交
487 488 489 490
/*
 * helper function for drop_snapshot, this walks down the tree dropping ref
 * counts as it goes.
 */
491 492
static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
			  *root, struct btrfs_path *path, int *level)
C
Chris Mason 已提交
493
{
C
Chris Mason 已提交
494 495
	struct buffer_head *next;
	struct buffer_head *cur;
C
Chris Mason 已提交
496 497 498 499
	u64 blocknr;
	int ret;
	u32 refs;

C
Chris Mason 已提交
500
	ret = lookup_block_ref(trans, root, path->nodes[*level]->b_blocknr,
501
			       &refs);
C
Chris Mason 已提交
502 503 504
	BUG_ON(ret);
	if (refs > 1)
		goto out;
C
Chris Mason 已提交
505 506 507
	/*
	 * walk down to the last node level and free all the leaves
	 */
C
Chris Mason 已提交
508 509
	while(*level > 0) {
		cur = path->nodes[*level];
510
		if (path->slots[*level] >=
C
Chris Mason 已提交
511
		    btrfs_header_nritems(btrfs_buffer_header(cur)))
C
Chris Mason 已提交
512
			break;
C
Chris Mason 已提交
513 514
		blocknr = btrfs_node_blockptr(btrfs_buffer_node(cur),
					      path->slots[*level]);
515
		ret = lookup_block_ref(trans, root, blocknr, &refs);
C
Chris Mason 已提交
516 517
		if (refs != 1 || *level == 1) {
			path->slots[*level]++;
518
			ret = btrfs_free_extent(trans, root, blocknr, 1, 1);
C
Chris Mason 已提交
519 520 521 522 523
			BUG_ON(ret);
			continue;
		}
		BUG_ON(ret);
		next = read_tree_block(root, blocknr);
C
Chris Mason 已提交
524
		if (path->nodes[*level-1])
C
Chris Mason 已提交
525
			btrfs_block_release(root, path->nodes[*level-1]);
C
Chris Mason 已提交
526
		path->nodes[*level-1] = next;
C
Chris Mason 已提交
527
		*level = btrfs_header_level(btrfs_buffer_header(next));
C
Chris Mason 已提交
528 529 530
		path->slots[*level] = 0;
	}
out:
C
Chris Mason 已提交
531 532
	ret = btrfs_free_extent(trans, root, path->nodes[*level]->b_blocknr,
				1, 1);
C
Chris Mason 已提交
533
	btrfs_block_release(root, path->nodes[*level]);
C
Chris Mason 已提交
534 535 536 537 538 539
	path->nodes[*level] = NULL;
	*level += 1;
	BUG_ON(ret);
	return 0;
}

C
Chris Mason 已提交
540 541 542 543 544
/*
 * 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
 */
545 546
static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root
			*root, struct btrfs_path *path, int *level)
C
Chris Mason 已提交
547 548 549 550
{
	int i;
	int slot;
	int ret;
C
Chris Mason 已提交
551
	for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
C
Chris Mason 已提交
552
		slot = path->slots[i];
C
Chris Mason 已提交
553 554
		if (slot < btrfs_header_nritems(
		    btrfs_buffer_header(path->nodes[i])) - 1) {
C
Chris Mason 已提交
555 556 557 558
			path->slots[i]++;
			*level = i;
			return 0;
		} else {
559
			ret = btrfs_free_extent(trans, root,
C
Chris Mason 已提交
560
						path->nodes[*level]->b_blocknr,
561
						1, 1);
C
Chris Mason 已提交
562
			btrfs_block_release(root, path->nodes[*level]);
C
Chris Mason 已提交
563
			path->nodes[*level] = NULL;
C
Chris Mason 已提交
564 565 566 567 568 569 570
			*level = i + 1;
			BUG_ON(ret);
		}
	}
	return 1;
}

C
Chris Mason 已提交
571 572 573 574 575
/*
 * 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.
 */
576
int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
C
Chris Mason 已提交
577
			*root, struct buffer_head *snap)
C
Chris Mason 已提交
578
{
579
	int ret = 0;
C
Chris Mason 已提交
580
	int wret;
C
Chris Mason 已提交
581
	int level;
C
Chris Mason 已提交
582
	struct btrfs_path path;
C
Chris Mason 已提交
583 584 585
	int i;
	int orig_level;

C
Chris Mason 已提交
586
	btrfs_init_path(&path);
C
Chris Mason 已提交
587

C
Chris Mason 已提交
588
	level = btrfs_header_level(btrfs_buffer_header(snap));
C
Chris Mason 已提交
589 590 591 592
	orig_level = level;
	path.nodes[level] = snap;
	path.slots[level] = 0;
	while(1) {
593
		wret = walk_down_tree(trans, root, &path, &level);
C
Chris Mason 已提交
594
		if (wret > 0)
C
Chris Mason 已提交
595
			break;
C
Chris Mason 已提交
596 597 598
		if (wret < 0)
			ret = wret;

599
		wret = walk_up_tree(trans, root, &path, &level);
C
Chris Mason 已提交
600
		if (wret > 0)
C
Chris Mason 已提交
601
			break;
C
Chris Mason 已提交
602 603
		if (wret < 0)
			ret = wret;
C
Chris Mason 已提交
604
	}
C
Chris Mason 已提交
605 606
	for (i = 0; i <= orig_level; i++) {
		if (path.nodes[i]) {
C
Chris Mason 已提交
607
			btrfs_block_release(root, path.nodes[i]);
C
Chris Mason 已提交
608
		}
C
Chris Mason 已提交
609
	}
C
Chris Mason 已提交
610
	return ret;
C
Chris Mason 已提交
611
}