extent-tree.c 37.5 KB
Newer Older
C
Chris Mason 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * Copyright (C) 2007 Oracle.  All rights reserved.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public
 * License v2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 021110-1307, USA.
 */

Z
Zach Brown 已提交
19
#include <linux/sched.h>
20 21 22
#include "ctree.h"
#include "disk-io.h"
#include "print-tree.h"
23
#include "transaction.h"
24

25 26 27 28
#define BLOCK_GROUP_DATA EXTENT_WRITEBACK
#define BLOCK_GROUP_METADATA EXTENT_UPTODATE
#define BLOCK_GROUP_DIRTY EXTENT_DIRTY

29 30
static int finish_current_insert(struct btrfs_trans_handle *trans, struct
				 btrfs_root *extent_root);
C
Chris Mason 已提交
31 32
static int del_pending_extents(struct btrfs_trans_handle *trans, struct
			       btrfs_root *extent_root);
33

34 35 36 37 38 39
static int cache_block_group(struct btrfs_root *root,
			     struct btrfs_block_group_cache *block_group)
{
	struct btrfs_path *path;
	int ret;
	struct btrfs_key key;
40
	struct extent_buffer *leaf;
41
	struct extent_map_tree *free_space_cache;
42 43 44
	int slot;
	u64 last = 0;
	u64 hole_size;
45
	u64 first_free;
46 47 48
	int found = 0;

	root = root->fs_info->extent_root;
49
	free_space_cache = &root->fs_info->free_space_cache;
50 51 52

	if (block_group->cached)
		return 0;
53

54 55 56
	path = btrfs_alloc_path();
	if (!path)
		return -ENOMEM;
57

58
	path->reada = 2;
59
	first_free = block_group->key.objectid;
60 61
	key.objectid = block_group->key.objectid;
	key.offset = 0;
62

63 64
	btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
	ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
65

66 67
	if (ret < 0)
		return ret;
68

69 70
	if (ret && path->slots[0] > 0)
		path->slots[0]--;
71

72
	while(1) {
73
		leaf = path->nodes[0];
74
		slot = path->slots[0];
75
		if (slot >= btrfs_header_nritems(leaf)) {
76
			ret = btrfs_next_leaf(root, path);
77 78
			if (ret < 0)
				goto err;
79
			if (ret == 0) {
80
				continue;
81
			} else {
82 83 84
				break;
			}
		}
85

86
		btrfs_item_key_to_cpu(leaf, &key, slot);
87 88 89 90 91 92
		if (key.objectid < block_group->key.objectid) {
			if (key.objectid + key.offset > first_free)
				first_free = key.objectid + key.offset;
			goto next;
		}

93 94 95 96
		if (key.objectid >= block_group->key.objectid +
		    block_group->key.offset) {
			break;
		}
97

98 99
		if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) {
			if (!found) {
100
				last = first_free;
101 102
				found = 1;
			}
103 104 105 106 107
			if (key.objectid > last) {
				hole_size = key.objectid - last;
				set_extent_dirty(free_space_cache, last,
						 last + hole_size - 1,
						 GFP_NOFS);
108 109
			}
			last = key.objectid + key.offset;
110
		}
111
next:
112 113 114
		path->slots[0]++;
	}

115 116 117 118 119 120
	if (!found)
		last = first_free;
	if (block_group->key.objectid +
	    block_group->key.offset > last) {
		hole_size = block_group->key.objectid +
			block_group->key.offset - last;
121 122
		set_extent_dirty(free_space_cache, last,
				 last + hole_size - 1, GFP_NOFS);
123
	}
124
	block_group->cached = 1;
125
err:
126 127 128 129
	btrfs_free_path(path);
	return 0;
}

130 131 132
struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
							 btrfs_fs_info *info,
							 u64 blocknr)
C
Chris Mason 已提交
133
{
134 135 136 137 138
	struct extent_map_tree *block_group_cache;
	struct btrfs_block_group_cache *block_group = NULL;
	u64 ptr;
	u64 start;
	u64 end;
C
Chris Mason 已提交
139 140
	int ret;

141 142 143 144
	block_group_cache = &info->block_group_cache;
	ret = find_first_extent_bit(block_group_cache,
				    blocknr, &start, &end,
				    BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA);
C
Chris Mason 已提交
145
	if (ret) {
146
		return NULL;
C
Chris Mason 已提交
147
	}
148 149 150 151 152 153 154 155 156 157 158
	ret = get_state_private(block_group_cache, start, &ptr);
	if (ret)
		return NULL;

	block_group = (struct btrfs_block_group_cache *)ptr;


	if (block_group->key.objectid <= blocknr && blocknr <=
	    block_group->key.objectid + block_group->key.offset)
		return block_group;

C
Chris Mason 已提交
159 160 161
	return NULL;
}

162 163
static u64 find_search_start(struct btrfs_root *root,
			     struct btrfs_block_group_cache **cache_ret,
164
			     u64 search_start, int num, int data)
165 166 167 168
{
	int ret;
	struct btrfs_block_group_cache *cache = *cache_ret;
	u64 last = max(search_start, cache->key.objectid);
169 170
	u64 start = 0;
	u64 end = 0;
171 172

again:
173 174 175
	ret = cache_block_group(root, cache);
	if (ret)
		goto out;
176
	while(1) {
177 178 179
		ret = find_first_extent_bit(&root->fs_info->free_space_cache,
					    last, &start, &end, EXTENT_DIRTY);
		if (ret)
180
			goto out;
181 182 183 184 185

		start = max(last, start);
		last = end + 1;
		if (end + 1 - start < num)
			continue;
186
		if (start + num >= cache->key.objectid + cache->key.offset)
187
			goto new_group;
188
		return start;
189 190
	}
out:
191
	return search_start;
192 193

new_group:
194 195
	cache = btrfs_lookup_block_group(root->fs_info,
					 last + cache->key.offset - 1);
196
	if (!cache) {
197
		return search_start;
198 199
	}
	cache = btrfs_find_block_group(root, cache,
200
				       last + cache->key.offset - 1, data, 0);
201
	*cache_ret = cache;
202
	last = min(cache->key.objectid, last);
203 204 205
	goto again;
}

C
Chris Mason 已提交
206 207 208 209 210 211 212
static u64 div_factor(u64 num, int factor)
{
	num *= factor;
	do_div(num, 10);
	return num;
}

213 214
struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
						 struct btrfs_block_group_cache
C
Chris Mason 已提交
215
						 *hint, u64 search_start,
216
						 int data, int owner)
C
Chris Mason 已提交
217
{
218 219
	struct btrfs_block_group_cache *cache;
	struct extent_map_tree *block_group_cache;
220
	struct btrfs_block_group_cache *found_group = NULL;
C
Chris Mason 已提交
221 222
	struct btrfs_fs_info *info = root->fs_info;
	u64 used;
223 224
	u64 last = 0;
	u64 hint_last;
225 226 227 228 229
	u64 start;
	u64 end;
	u64 free_check;
	u64 ptr;
	int bit;
C
Chris Mason 已提交
230
	int ret;
231
	int full_search = 0;
232
	int factor = 8;
C
Chris Mason 已提交
233
	int data_swap = 0;
234

235 236
	block_group_cache = &info->block_group_cache;

237 238
	if (!owner)
		factor = 5;
C
Chris Mason 已提交
239

240 241 242 243
	if (data)
		bit = BLOCK_GROUP_DATA;
	else
		bit = BLOCK_GROUP_METADATA;
C
Chris Mason 已提交
244 245 246

	if (search_start) {
		struct btrfs_block_group_cache *shint;
247
		shint = btrfs_lookup_block_group(info, search_start);
248
		if (shint && shint->data == data) {
C
Chris Mason 已提交
249
			used = btrfs_block_group_used(&shint->item);
250
			if (used < div_factor(shint->key.offset, factor)) {
C
Chris Mason 已提交
251 252 253 254 255
				return shint;
			}
		}
	}
	if (hint && hint->data == data) {
256
		used = btrfs_block_group_used(&hint->item);
257
		if (used < div_factor(hint->key.offset, factor)) {
258 259
			return hint;
		}
260
		last = hint->key.offset * 3;
C
Chris Mason 已提交
261
		if (hint->key.objectid >= last)
262 263
			last = max(search_start + hint->key.offset - 1,
				   hint->key.objectid - last);
C
Chris Mason 已提交
264 265
		else
			last = hint->key.objectid + hint->key.offset;
266 267
		hint_last = last;
	} else {
268 269 270 271 272 273
		if (hint)
			hint_last = max(hint->key.objectid, search_start);
		else
			hint_last = search_start;

		last = hint_last;
274 275
	}
again:
C
Chris Mason 已提交
276
	while(1) {
277 278 279
		ret = find_first_extent_bit(block_group_cache, last,
					    &start, &end, bit);
		if (ret)
C
Chris Mason 已提交
280
			break;
281 282 283 284 285 286 287 288 289 290 291 292 293 294

		ret = get_state_private(block_group_cache, start, &ptr);
		if (ret)
			break;

		cache = (struct btrfs_block_group_cache *)ptr;
		last = cache->key.objectid + cache->key.offset;
		used = btrfs_block_group_used(&cache->item);

		if (full_search)
			free_check = cache->key.offset;
		else
			free_check = div_factor(cache->key.offset, factor);

295
		if (used < free_check) {
296 297
			found_group = cache;
			goto found;
C
Chris Mason 已提交
298
		}
299
		cond_resched();
C
Chris Mason 已提交
300
	}
301
	if (!full_search) {
C
Chris Mason 已提交
302
		last = search_start;
303 304 305
		full_search = 1;
		goto again;
	}
C
Chris Mason 已提交
306 307
	if (!data_swap) {
		data_swap = 1;
308
		bit = BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA;
C
Chris Mason 已提交
309 310 311
		last = search_start;
		goto again;
	}
C
Chris Mason 已提交
312
found:
313
	return found_group;
C
Chris Mason 已提交
314 315
}

C
Chris Mason 已提交
316 317 318
int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
				struct btrfs_root *root,
				u64 blocknr, u64 num_blocks)
C
Chris Mason 已提交
319
{
320
	struct btrfs_path *path;
C
Chris Mason 已提交
321
	int ret;
C
Chris Mason 已提交
322
	struct btrfs_key key;
323
	struct extent_buffer *l;
C
Chris Mason 已提交
324
	struct btrfs_extent_item *item;
C
Chris Mason 已提交
325
	u32 refs;
C
Chris Mason 已提交
326

327
	path = btrfs_alloc_path();
328 329
	if (!path)
		return -ENOMEM;
330

C
Chris Mason 已提交
331
	key.objectid = blocknr;
332
	btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
333
	key.offset = num_blocks;
334
	ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
335
				0, 1);
336 337
	if (ret < 0)
		return ret;
338
	if (ret != 0) {
339
		BUG();
340
	}
C
Chris Mason 已提交
341
	BUG_ON(ret != 0);
342
	l = path->nodes[0];
343
	item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
344 345
	refs = btrfs_extent_refs(l, item);
	btrfs_set_extent_refs(l, item, refs + 1);
346
	btrfs_mark_buffer_dirty(path->nodes[0]);
347

348 349
	btrfs_release_path(root->fs_info->extent_root, path);
	btrfs_free_path(path);
350
	finish_current_insert(trans, root->fs_info->extent_root);
C
Chris Mason 已提交
351
	del_pending_extents(trans, root->fs_info->extent_root);
C
Chris Mason 已提交
352 353 354
	return 0;
}

355 356 357 358 359 360 361 362
int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
			 struct btrfs_root *root)
{
	finish_current_insert(trans, root->fs_info->extent_root);
	del_pending_extents(trans, root->fs_info->extent_root);
	return 0;
}

C
Chris Mason 已提交
363 364 365
static int lookup_extent_ref(struct btrfs_trans_handle *trans,
			     struct btrfs_root *root, u64 blocknr,
			     u64 num_blocks, u32 *refs)
366
{
367
	struct btrfs_path *path;
368
	int ret;
C
Chris Mason 已提交
369
	struct btrfs_key key;
370
	struct extent_buffer *l;
C
Chris Mason 已提交
371
	struct btrfs_extent_item *item;
372 373

	path = btrfs_alloc_path();
374
	key.objectid = blocknr;
375
	key.offset = num_blocks;
376
	btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
377
	ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
378
				0, 0);
379 380
	if (ret < 0)
		goto out;
381 382 383
	if (ret != 0) {
		btrfs_print_leaf(root, path->nodes[0]);
		printk("failed to find block number %Lu\n", blocknr);
384
		BUG();
385 386
	}
	l = path->nodes[0];
387
	item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
388
	*refs = btrfs_extent_refs(l, item);
389
out:
390
	btrfs_free_path(path);
391 392 393
	return 0;
}

C
Chris Mason 已提交
394 395 396
int btrfs_inc_root_ref(struct btrfs_trans_handle *trans,
		       struct btrfs_root *root)
{
397 398
	return btrfs_inc_extent_ref(trans, root,
				    extent_buffer_blocknr(root->node), 1);
C
Chris Mason 已提交
399 400
}

401
int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
402
		  struct extent_buffer *buf)
C
Chris Mason 已提交
403 404
{
	u64 blocknr;
405 406
	u32 nritems;
	struct btrfs_key key;
407
	struct btrfs_file_extent_item *fi;
C
Chris Mason 已提交
408
	int i;
409 410
	int leaf;
	int ret;
411 412
	int faili;
	int err;
413

414
	if (!root->ref_cows)
415
		return 0;
416 417 418 419

	leaf = btrfs_is_leaf(buf);
	nritems = btrfs_header_nritems(buf);
	for (i = 0; i < nritems; i++) {
420
		if (leaf) {
C
Chris Mason 已提交
421
			u64 disk_blocknr;
422 423
			btrfs_item_key_to_cpu(buf, &key, i);
			if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
424
				continue;
425
			fi = btrfs_item_ptr(buf, i,
426
					    struct btrfs_file_extent_item);
427
			if (btrfs_file_extent_type(buf, fi) ==
428 429
			    BTRFS_FILE_EXTENT_INLINE)
				continue;
430
			disk_blocknr = btrfs_file_extent_disk_blocknr(buf, fi);
C
Chris Mason 已提交
431 432 433
			if (disk_blocknr == 0)
				continue;
			ret = btrfs_inc_extent_ref(trans, root, disk_blocknr,
434
				    btrfs_file_extent_disk_num_blocks(buf, fi));
435 436 437 438
			if (ret) {
				faili = i;
				goto fail;
			}
439
		} else {
440
			blocknr = btrfs_node_blockptr(buf, i);
C
Chris Mason 已提交
441
			ret = btrfs_inc_extent_ref(trans, root, blocknr, 1);
442 443 444 445
			if (ret) {
				faili = i;
				goto fail;
			}
446
		}
C
Chris Mason 已提交
447 448
	}
	return 0;
449
fail:
C
Chris Mason 已提交
450
	WARN_ON(1);
451 452 453
	for (i =0; i < faili; i++) {
		if (leaf) {
			u64 disk_blocknr;
454 455
			btrfs_item_key_to_cpu(buf, &key, i);
			if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
456
				continue;
457
			fi = btrfs_item_ptr(buf, i,
458
					    struct btrfs_file_extent_item);
459
			if (btrfs_file_extent_type(buf, fi) ==
460 461
			    BTRFS_FILE_EXTENT_INLINE)
				continue;
462
			disk_blocknr = btrfs_file_extent_disk_blocknr(buf, fi);
463 464 465
			if (disk_blocknr == 0)
				continue;
			err = btrfs_free_extent(trans, root, disk_blocknr,
466 467
				    btrfs_file_extent_disk_num_blocks(buf,
								      fi), 0);
468 469
			BUG_ON(err);
		} else {
470
			blocknr = btrfs_node_blockptr(buf, i);
471 472 473 474 475
			err = btrfs_free_extent(trans, root, blocknr, 1, 0);
			BUG_ON(err);
		}
	}
	return ret;
C
Chris Mason 已提交
476 477
}

C
Chris Mason 已提交
478 479 480 481 482 483 484 485
static int write_one_cache_group(struct btrfs_trans_handle *trans,
				 struct btrfs_root *root,
				 struct btrfs_path *path,
				 struct btrfs_block_group_cache *cache)
{
	int ret;
	int pending_ret;
	struct btrfs_root *extent_root = root->fs_info->extent_root;
486 487
	unsigned long bi;
	struct extent_buffer *leaf;
C
Chris Mason 已提交
488 489

	ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
490 491
	if (ret < 0)
		goto fail;
C
Chris Mason 已提交
492
	BUG_ON(ret);
493 494 495 496 497

	leaf = path->nodes[0];
	bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
	write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
	btrfs_mark_buffer_dirty(leaf);
C
Chris Mason 已提交
498
	btrfs_release_path(extent_root, path);
499
fail:
C
Chris Mason 已提交
500 501 502 503 504 505 506 507 508 509
	finish_current_insert(trans, extent_root);
	pending_ret = del_pending_extents(trans, extent_root);
	if (ret)
		return ret;
	if (pending_ret)
		return pending_ret;
	return 0;

}

510 511
int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
				   struct btrfs_root *root)
C
Chris Mason 已提交
512
{
513 514
	struct extent_map_tree *block_group_cache;
	struct btrfs_block_group_cache *cache;
C
Chris Mason 已提交
515 516 517 518
	int ret;
	int err = 0;
	int werr = 0;
	struct btrfs_path *path;
519 520 521 522
	u64 last = 0;
	u64 start;
	u64 end;
	u64 ptr;
C
Chris Mason 已提交
523

524
	block_group_cache = &root->fs_info->block_group_cache;
C
Chris Mason 已提交
525 526 527 528 529
	path = btrfs_alloc_path();
	if (!path)
		return -ENOMEM;

	while(1) {
530 531 532
		ret = find_first_extent_bit(block_group_cache, last,
					    &start, &end, BLOCK_GROUP_DIRTY);
		if (ret)
C
Chris Mason 已提交
533
			break;
534

535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550
		last = end + 1;
		ret = get_state_private(block_group_cache, start, &ptr);
		if (ret)
			break;

		cache = (struct btrfs_block_group_cache *)ptr;
		err = write_one_cache_group(trans, root,
					    path, cache);
		/*
		 * if we fail to write the cache group, we want
		 * to keep it marked dirty in hopes that a later
		 * write will work
		 */
		if (err) {
			werr = err;
			continue;
C
Chris Mason 已提交
551
		}
552 553
		clear_extent_bits(block_group_cache, start, end,
				  BLOCK_GROUP_DIRTY, GFP_NOFS);
C
Chris Mason 已提交
554 555 556 557 558 559 560
	}
	btrfs_free_path(path);
	return werr;
}

static int update_block_group(struct btrfs_trans_handle *trans,
			      struct btrfs_root *root,
C
Chris Mason 已提交
561 562
			      u64 blocknr, u64 num, int alloc, int mark_free,
			      int data)
C
Chris Mason 已提交
563 564 565 566 567 568
{
	struct btrfs_block_group_cache *cache;
	struct btrfs_fs_info *info = root->fs_info;
	u64 total = num;
	u64 old_val;
	u64 block_in_group;
569 570
	u64 start;
	u64 end;
C
Chris Mason 已提交
571

C
Chris Mason 已提交
572
	while(total) {
573
		cache = btrfs_lookup_block_group(info, blocknr);
C
Chris Mason 已提交
574
		if (!cache) {
C
Chris Mason 已提交
575
			return -1;
C
Chris Mason 已提交
576
		}
C
Chris Mason 已提交
577 578
		block_in_group = blocknr - cache->key.objectid;
		WARN_ON(block_in_group > cache->key.offset);
579 580 581 582
		start = cache->key.objectid;
		end = start + cache->key.offset - 1;
		set_extent_bits(&info->block_group_cache, start, end,
				BLOCK_GROUP_DIRTY, GFP_NOFS);
C
Chris Mason 已提交
583 584 585

		old_val = btrfs_block_group_used(&cache->item);
		num = min(total, cache->key.offset - block_in_group);
C
Chris Mason 已提交
586
		if (alloc) {
C
Chris Mason 已提交
587
			if (cache->data != data &&
C
Chris Mason 已提交
588
			    old_val < (cache->key.offset >> 1)) {
589 590
				int bit_to_clear;
				int bit_to_set;
C
Chris Mason 已提交
591

592
				cache->data = data;
C
Chris Mason 已提交
593
				if (data) {
594 595
					bit_to_clear = BLOCK_GROUP_DATA;
					bit_to_set = BLOCK_GROUP_METADATA;
C
Chris Mason 已提交
596 597 598
					cache->item.flags |=
						BTRFS_BLOCK_GROUP_DATA;
				} else {
599 600
					bit_to_clear = BLOCK_GROUP_METADATA;
					bit_to_set = BLOCK_GROUP_DATA;
C
Chris Mason 已提交
601 602 603
					cache->item.flags &=
						~BTRFS_BLOCK_GROUP_DATA;
				}
604 605 606 607 608 609
				clear_extent_bits(&info->block_group_cache,
						  start, end, bit_to_clear,
						  GFP_NOFS);
				set_extent_bits(&info->block_group_cache,
						start, end, bit_to_set,
						GFP_NOFS);
C
Chris Mason 已提交
610 611
			}
			old_val += num;
C
Chris Mason 已提交
612
		} else {
C
Chris Mason 已提交
613
			old_val -= num;
614 615 616 617
			if (mark_free) {
				set_extent_dirty(&info->free_space_cache,
						 blocknr, blocknr + num - 1,
						 GFP_NOFS);
618
			}
C
Chris Mason 已提交
619
		}
C
Chris Mason 已提交
620
		btrfs_set_block_group_used(&cache->item, old_val);
621 622
		total -= num;
		blocknr += num;
C
Chris Mason 已提交
623 624 625 626
	}
	return 0;
}

627
int btrfs_copy_pinned(struct btrfs_root *root, struct extent_map_tree *copy)
C
Chris Mason 已提交
628 629
{
	u64 last = 0;
630 631 632
	u64 start;
	u64 end;
	struct extent_map_tree *pinned_extents = &root->fs_info->pinned_extents;
C
Chris Mason 已提交
633 634 635
	int ret;

	while(1) {
636 637 638
		ret = find_first_extent_bit(pinned_extents, last,
					    &start, &end, EXTENT_DIRTY);
		if (ret)
C
Chris Mason 已提交
639
			break;
640 641
		set_extent_dirty(copy, start, end, GFP_NOFS);
		last = end + 1;
C
Chris Mason 已提交
642 643 644 645 646 647
	}
	return 0;
}

int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
			       struct btrfs_root *root,
648
			       struct extent_map_tree *unpin)
649
{
650 651
	u64 start;
	u64 end;
652
	int ret;
653
	struct extent_map_tree *pinned_extents = &root->fs_info->pinned_extents;
654 655 656
	struct extent_map_tree *free_space_cache;

	free_space_cache = &root->fs_info->free_space_cache;
657 658

	while(1) {
659 660 661
		ret = find_first_extent_bit(unpin, 0, &start, &end,
					    EXTENT_DIRTY);
		if (ret)
662
			break;
663 664 665 666 667

		clear_extent_dirty(pinned_extents, start, end,
				   GFP_NOFS);
		clear_extent_dirty(unpin, start, end, GFP_NOFS);
		set_extent_dirty(free_space_cache, start, end, GFP_NOFS);
668 669 670 671
	}
	return 0;
}

672 673
static int finish_current_insert(struct btrfs_trans_handle *trans, struct
				 btrfs_root *extent_root)
C
Chris Mason 已提交
674
{
C
Chris Mason 已提交
675
	struct btrfs_key ins;
C
Chris Mason 已提交
676
	struct btrfs_extent_item extent_item;
C
Chris Mason 已提交
677
	int ret;
678 679 680
	int err = 0;
	u64 start;
	u64 end;
681
	struct btrfs_fs_info *info = extent_root->fs_info;
C
Chris Mason 已提交
682

683
	btrfs_set_stack_extent_refs(&extent_item, 1);
684
	btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
685 686
	btrfs_set_stack_extent_owner(&extent_item,
				     extent_root->root_key.objectid);
C
Chris Mason 已提交
687

688
	while(1) {
689 690 691
		ret = find_first_extent_bit(&info->extent_ins, 0, &start,
					    &end, EXTENT_LOCKED);
		if (ret)
692 693
			break;

694 695 696 697 698 699
		ins.objectid = start;
		ins.offset = end + 1 - start;
		err = btrfs_insert_item(trans, extent_root, &ins,
					&extent_item, sizeof(extent_item));
		clear_extent_bits(&info->extent_ins, start, end, EXTENT_LOCKED,
				  GFP_NOFS);
C
Chris Mason 已提交
700 701 702 703
	}
	return 0;
}

C
Chris Mason 已提交
704
static int pin_down_block(struct btrfs_root *root, u64 blocknr, int pending)
C
Chris Mason 已提交
705
{
706
	int err = 0;
707
	struct extent_buffer *buf;
C
Chris Mason 已提交
708

C
Chris Mason 已提交
709
	if (!pending) {
710 711 712
		buf = btrfs_find_tree_block(root, blocknr);
		if (buf) {
			if (btrfs_buffer_uptodate(buf)) {
C
Chris Mason 已提交
713 714
				u64 transid =
				    root->fs_info->running_transaction->transid;
715 716
				if (btrfs_header_generation(buf) == transid) {
					free_extent_buffer(buf);
C
Chris Mason 已提交
717 718
					return 0;
				}
C
Chris Mason 已提交
719
			}
720
			free_extent_buffer(buf);
C
Chris Mason 已提交
721
		}
722 723
		set_extent_dirty(&root->fs_info->pinned_extents,
				 blocknr, blocknr, GFP_NOFS);
C
Chris Mason 已提交
724
	} else {
725 726
		set_extent_bits(&root->fs_info->pending_del,
				  blocknr, blocknr, EXTENT_LOCKED, GFP_NOFS);
C
Chris Mason 已提交
727
	}
C
Chris Mason 已提交
728
	BUG_ON(err < 0);
C
Chris Mason 已提交
729 730 731
	return 0;
}

732
/*
733
 * remove an extent from the root, returns 0 on success
734
 */
735
static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
736 737
			 *root, u64 blocknr, u64 num_blocks, int pin,
			 int mark_free)
738
{
739
	struct btrfs_path *path;
C
Chris Mason 已提交
740
	struct btrfs_key key;
741 742
	struct btrfs_fs_info *info = root->fs_info;
	struct btrfs_root *extent_root = info->extent_root;
743
	struct extent_buffer *leaf;
744
	int ret;
C
Chris Mason 已提交
745
	struct btrfs_extent_item *ei;
C
Chris Mason 已提交
746
	u32 refs;
C
Chris Mason 已提交
747

748
	key.objectid = blocknr;
749
	btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
750 751
	key.offset = num_blocks;

752
	path = btrfs_alloc_path();
753 754
	if (!path)
		return -ENOMEM;
755

756 757 758 759
	ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
	if (ret < 0)
		return ret;
	BUG_ON(ret);
760 761 762

	leaf = path->nodes[0];
	ei = btrfs_item_ptr(leaf, path->slots[0],
C
Chris Mason 已提交
763
			    struct btrfs_extent_item);
764 765 766 767 768 769
	refs = btrfs_extent_refs(leaf, ei);
	BUG_ON(refs == 0);
	refs -= 1;
	btrfs_set_extent_refs(leaf, ei, refs);
	btrfs_mark_buffer_dirty(leaf);

C
Chris Mason 已提交
770
	if (refs == 0) {
771
		u64 super_blocks_used, root_blocks_used;
C
Chris Mason 已提交
772 773

		if (pin) {
C
Chris Mason 已提交
774
			ret = pin_down_block(root, blocknr, 0);
C
Chris Mason 已提交
775 776 777
			BUG_ON(ret);
		}

778
		/* block accounting for super block */
779 780
		super_blocks_used = btrfs_super_blocks_used(&info->super_copy);
		btrfs_set_super_blocks_used(&info->super_copy,
781
					    super_blocks_used - num_blocks);
782 783

		/* block accounting for root item */
784 785
		root_blocks_used = btrfs_root_used(&root->root_item);
		btrfs_set_root_used(&root->root_item,
786 787
					   root_blocks_used - num_blocks);

788
		ret = btrfs_del_item(trans, extent_root, path);
789 790 791
		if (ret) {
			return ret;
		}
792
		ret = update_block_group(trans, root, blocknr, num_blocks, 0,
C
Chris Mason 已提交
793
					 mark_free, 0);
C
Chris Mason 已提交
794
		BUG_ON(ret);
795
	}
796
	btrfs_free_path(path);
797
	finish_current_insert(trans, extent_root);
798 799 800 801 802 803 804
	return ret;
}

/*
 * find all the blocks marked as pending in the radix tree and remove
 * them from the extent map
 */
805 806
static int del_pending_extents(struct btrfs_trans_handle *trans, struct
			       btrfs_root *extent_root)
807 808
{
	int ret;
C
Chris Mason 已提交
809
	int err = 0;
810 811 812 813
	u64 start;
	u64 end;
	struct extent_map_tree *pending_del;
	struct extent_map_tree *pinned_extents;
C
Chris Mason 已提交
814

815 816
	pending_del = &extent_root->fs_info->pending_del;
	pinned_extents = &extent_root->fs_info->pinned_extents;
817 818

	while(1) {
819 820 821
		ret = find_first_extent_bit(pending_del, 0, &start, &end,
					    EXTENT_LOCKED);
		if (ret)
822
			break;
823 824 825 826 827 828 829 830

		set_extent_dirty(pinned_extents, start, end, GFP_NOFS);
		clear_extent_bits(pending_del, start, end, EXTENT_LOCKED,
				  GFP_NOFS);
		ret = __free_extent(trans, extent_root,
				     start, end + 1 - start, 0, 0);
		if (ret)
			err = ret;
831
	}
C
Chris Mason 已提交
832
	return err;
833 834 835 836 837
}

/*
 * remove an extent from the root, returns 0 on success
 */
838 839
int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
		      *root, u64 blocknr, u64 num_blocks, int pin)
840
{
841
	struct btrfs_root *extent_root = root->fs_info->extent_root;
842 843
	int pending_ret;
	int ret;
844

845
	if (root == extent_root) {
C
Chris Mason 已提交
846
		pin_down_block(root, blocknr, 1);
847 848
		return 0;
	}
849
	ret = __free_extent(trans, root, blocknr, num_blocks, pin, pin == 0);
C
Chris Mason 已提交
850
	pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
851 852 853 854 855 856 857
	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
858
 * ins->flags = BTRFS_EXTENT_ITEM_KEY
859 860 861
 * ins->offset == number of blocks
 * Any available blocks before search_start are skipped.
 */
862
static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
863 864
			    *orig_root, u64 num_blocks, u64 empty_size,
			    u64 search_start, u64 search_end, u64 hint_block,
865 866
			    struct btrfs_key *ins, u64 exclude_start,
			    u64 exclude_nr, int data)
867
{
868
	struct btrfs_path *path;
C
Chris Mason 已提交
869
	struct btrfs_key key;
870 871 872
	int ret;
	u64 hole_size = 0;
	int slot = 0;
C
Chris Mason 已提交
873
	u64 last_block = 0;
C
Chris Mason 已提交
874
	u64 orig_search_start = search_start;
875
	int start_found;
876
	struct extent_buffer *l;
877
	struct btrfs_root * root = orig_root->fs_info->extent_root;
878
	struct btrfs_fs_info *info = root->fs_info;
879
	int total_needed = num_blocks;
C
Chris Mason 已提交
880
	int level;
C
Chris Mason 已提交
881
	struct btrfs_block_group_cache *block_group;
C
Chris Mason 已提交
882
	int full_scan = 0;
883
	int wrapped = 0;
884
	u64 cached_search_start = 0;
885

886
	WARN_ON(num_blocks < 1);
887 888
	btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);

889 890
	level = btrfs_header_level(root->node);

C
Chris Mason 已提交
891
	if (search_end == (u64)-1)
892
		search_end = btrfs_super_total_blocks(&info->super_copy);
893
	if (hint_block) {
894
		block_group = btrfs_lookup_block_group(info, hint_block);
C
Chris Mason 已提交
895
		block_group = btrfs_find_block_group(root, block_group,
896
						     hint_block, data, 1);
C
Chris Mason 已提交
897 898 899
	} else {
		block_group = btrfs_find_block_group(root,
						     trans->block_group, 0,
900
						     data, 1);
C
Chris Mason 已提交
901 902
	}

903
	total_needed += empty_size;
904 905
	path = btrfs_alloc_path();

C
Chris Mason 已提交
906
check_failed:
907 908 909
	search_start = find_search_start(root, &block_group,
					 search_start, total_needed, data);
	cached_search_start = search_start;
910

911
	btrfs_init_path(path);
912 913 914
	ins->objectid = search_start;
	ins->offset = 0;
	start_found = 0;
915
	path->reada = 2;
916

917
	ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
C
Chris Mason 已提交
918 919
	if (ret < 0)
		goto error;
C
Chris Mason 已提交
920

921
	if (path->slots[0] > 0) {
922
		path->slots[0]--;
923 924
	}

925 926 927
	l = path->nodes[0];
	btrfs_item_key_to_cpu(l, &key, path->slots[0]);

928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944
	/*
	 * a rare case, go back one key if we hit a block group item
	 * instead of an extent item
	 */
	if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY &&
	    key.objectid + key.offset >= search_start) {
		ins->objectid = key.objectid;
		ins->offset = key.offset - 1;
		btrfs_release_path(root, path);
		ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
		if (ret < 0)
			goto error;

		if (path->slots[0] > 0) {
			path->slots[0]--;
		}
	}
945

946
	while (1) {
947
		l = path->nodes[0];
948
		slot = path->slots[0];
949
		if (slot >= btrfs_header_nritems(l)) {
950
			ret = btrfs_next_leaf(root, path);
951 952
			if (ret == 0)
				continue;
C
Chris Mason 已提交
953 954
			if (ret < 0)
				goto error;
955 956
			if (!start_found) {
				ins->objectid = search_start;
C
Chris Mason 已提交
957
				ins->offset = search_end - search_start;
958 959 960 961 962
				start_found = 1;
				goto check_pending;
			}
			ins->objectid = last_block > search_start ?
					last_block : search_start;
C
Chris Mason 已提交
963
			ins->offset = search_end - ins->objectid;
964 965
			goto check_pending;
		}
966
		btrfs_item_key_to_cpu(l, &key, slot);
967

968 969 970 971 972 973 974 975 976
		if (key.objectid >= search_start && key.objectid > last_block &&
		    start_found) {
			if (last_block < search_start)
				last_block = search_start;
			hole_size = key.objectid - last_block;
			if (hole_size >= num_blocks) {
				ins->objectid = last_block;
				ins->offset = hole_size;
				goto check_pending;
977
			}
978
		}
979 980 981 982 983
		if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY) {
			if (!start_found) {
				last_block = key.objectid;
				start_found = 1;
			}
984
			goto next;
985 986
		}

987

988
		start_found = 1;
C
Chris Mason 已提交
989
		last_block = key.objectid + key.offset;
990

991
		if (!full_scan && last_block >= block_group->key.objectid +
C
Chris Mason 已提交
992 993 994 995 996 997
		    block_group->key.offset) {
			btrfs_release_path(root, path);
			search_start = block_group->key.objectid +
				block_group->key.offset * 2;
			goto new_group;
		}
C
Chris Mason 已提交
998
next:
999
		path->slots[0]++;
1000
		cond_resched();
1001 1002 1003 1004 1005
	}
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
	 */
1006
	btrfs_release_path(root, path);
1007
	BUG_ON(ins->objectid < search_start);
1008

1009 1010 1011
	if (ins->objectid + num_blocks >= search_end)
		goto enospc;

1012 1013 1014 1015 1016 1017 1018 1019 1020
	if (test_range_bit(&info->extent_ins, ins->objectid,
			   ins->objectid + num_blocks -1, EXTENT_LOCKED, 0)) {
		search_start = ins->objectid + num_blocks;
		goto new_group;
	}
	if (test_range_bit(&info->pinned_extents, ins->objectid,
			   ins->objectid + num_blocks -1, EXTENT_DIRTY, 0)) {
		search_start = ins->objectid + num_blocks;
		goto new_group;
1021
	}
1022 1023 1024 1025 1026
	if (exclude_nr > 0 && (ins->objectid + num_blocks > exclude_start &&
	    ins->objectid < exclude_start + exclude_nr)) {
		search_start = exclude_start + exclude_nr;
		goto new_group;
	}
1027
	if (!data) {
1028
		block_group = btrfs_lookup_block_group(info, ins->objectid);
1029 1030
		if (block_group)
			trans->block_group = block_group;
1031
	}
C
Chris Mason 已提交
1032
	ins->offset = num_blocks;
1033
	btrfs_free_path(path);
1034
	return 0;
C
Chris Mason 已提交
1035 1036

new_group:
C
Chris Mason 已提交
1037
	if (search_start + num_blocks >= search_end) {
1038
enospc:
C
Chris Mason 已提交
1039
		search_start = orig_search_start;
1040 1041 1042 1043
		if (full_scan) {
			ret = -ENOSPC;
			goto error;
		}
1044 1045 1046
		if (wrapped) {
			if (!full_scan)
				total_needed -= empty_size;
1047
			full_scan = 1;
1048
		} else
1049
			wrapped = 1;
C
Chris Mason 已提交
1050
	}
1051
	block_group = btrfs_lookup_block_group(info, search_start);
1052
	cond_resched();
C
Chris Mason 已提交
1053 1054
	if (!full_scan)
		block_group = btrfs_find_block_group(root, block_group,
1055
						     search_start, data, 0);
C
Chris Mason 已提交
1056 1057
	goto check_failed;

C
Chris Mason 已提交
1058
error:
1059 1060
	btrfs_release_path(root, path);
	btrfs_free_path(path);
C
Chris Mason 已提交
1061
	return ret;
1062 1063 1064 1065 1066 1067 1068 1069
}
/*
 * 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.
 */
1070 1071
int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
		       struct btrfs_root *root, u64 owner,
1072
		       u64 num_blocks, u64 empty_size, u64 hint_block,
C
Chris Mason 已提交
1073
		       u64 search_end, struct btrfs_key *ins, int data)
1074 1075 1076
{
	int ret;
	int pending_ret;
1077
	u64 super_blocks_used, root_blocks_used;
1078
	u64 search_start = 0;
1079 1080
	struct btrfs_fs_info *info = root->fs_info;
	struct btrfs_root *extent_root = info->extent_root;
C
Chris Mason 已提交
1081
	struct btrfs_extent_item extent_item;
C
Chris Mason 已提交
1082

1083 1084
	btrfs_set_stack_extent_refs(&extent_item, 1);
	btrfs_set_stack_extent_owner(&extent_item, owner);
1085

1086
	WARN_ON(num_blocks < 1);
1087 1088
	ret = find_free_extent(trans, root, num_blocks, empty_size,
			       search_start, search_end, hint_block, ins,
1089 1090
			       trans->alloc_exclude_start,
			       trans->alloc_exclude_nr, data);
C
Chris Mason 已提交
1091
	BUG_ON(ret);
1092 1093
	if (ret)
		return ret;
1094

1095
	/* block accounting for super block */
1096 1097
	super_blocks_used = btrfs_super_blocks_used(&info->super_copy);
	btrfs_set_super_blocks_used(&info->super_copy, super_blocks_used +
1098
				    num_blocks);
1099

1100
	/* block accounting for root item */
1101 1102
	root_blocks_used = btrfs_root_used(&root->root_item);
	btrfs_set_root_used(&root->root_item, root_blocks_used +
1103 1104
				   num_blocks);

1105 1106 1107 1108
	clear_extent_dirty(&root->fs_info->free_space_cache,
			   ins->objectid, ins->objectid + ins->offset - 1,
			   GFP_NOFS);

1109 1110
	if (root == extent_root) {
		BUG_ON(num_blocks != 1);
1111 1112 1113
		set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
				ins->objectid + ins->offset - 1,
				EXTENT_LOCKED, GFP_NOFS);
1114 1115 1116 1117 1118 1119
		goto update_block;
	}

	WARN_ON(trans->alloc_exclude_nr);
	trans->alloc_exclude_start = ins->objectid;
	trans->alloc_exclude_nr = ins->offset;
1120 1121
	ret = btrfs_insert_item(trans, extent_root, ins, &extent_item,
				sizeof(extent_item));
C
Chris Mason 已提交
1122

1123 1124 1125
	trans->alloc_exclude_start = 0;
	trans->alloc_exclude_nr = 0;

C
Chris Mason 已提交
1126
	BUG_ON(ret);
1127
	finish_current_insert(trans, extent_root);
C
Chris Mason 已提交
1128
	pending_ret = del_pending_extents(trans, extent_root);
1129

1130
	if (ret) {
C
Chris Mason 已提交
1131
		return ret;
1132 1133
	}
	if (pending_ret) {
C
Chris Mason 已提交
1134
		return pending_ret;
1135
	}
1136 1137

update_block:
C
Chris Mason 已提交
1138 1139
	ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0,
				 data);
C
Chris Mason 已提交
1140
	BUG_ON(ret);
C
Chris Mason 已提交
1141
	return 0;
1142 1143 1144 1145 1146 1147
}

/*
 * helper function to allocate a block for a given tree
 * returns the tree buffer or NULL.
 */
1148 1149 1150
struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
					     struct btrfs_root *root, u64 hint,
					     u64 empty_size)
1151
{
C
Chris Mason 已提交
1152
	struct btrfs_key ins;
1153
	int ret;
1154
	struct extent_buffer *buf;
1155

1156
	ret = btrfs_alloc_extent(trans, root, root->root_key.objectid,
1157
				 1, empty_size, hint, (u64)-1, &ins, 0);
1158
	if (ret) {
1159 1160
		BUG_ON(ret > 0);
		return ERR_PTR(ret);
1161
	}
1162
	buf = btrfs_find_create_tree_block(root, ins.objectid);
1163 1164 1165 1166
	if (!buf) {
		btrfs_free_extent(trans, root, ins.objectid, 1, 0);
		return ERR_PTR(-ENOMEM);
	}
1167
	btrfs_set_buffer_uptodate(buf);
1168
	buf->alloc_addr = (unsigned long)__builtin_return_address(0);
1169 1170 1171
	set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
			 buf->start + buf->len - 1, GFP_NOFS);
	/*
C
Chris Mason 已提交
1172
	set_buffer_checked(buf);
1173
	set_buffer_defrag(buf);
1174 1175 1176 1177
	*/
	/* FIXME!!!!!!!!!!!!!!!!
	set_radix_bit(&trans->transaction->dirty_pages, buf->pages[0]->index);
	*/
1178
	trans->blocks_used++;
1179 1180
	return buf;
}
1181

1182
static int drop_leaf_ref(struct btrfs_trans_handle *trans,
1183
			 struct btrfs_root *root, struct extent_buffer *leaf)
1184
{
1185
	struct btrfs_key key;
1186 1187 1188 1189 1190
	struct btrfs_file_extent_item *fi;
	int i;
	int nritems;
	int ret;

1191 1192
	BUG_ON(!btrfs_is_leaf(leaf));
	nritems = btrfs_header_nritems(leaf);
1193
	for (i = 0; i < nritems; i++) {
C
Chris Mason 已提交
1194
		u64 disk_blocknr;
1195 1196 1197

		btrfs_item_key_to_cpu(leaf, &key, i);
		if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1198 1199
			continue;
		fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
1200 1201
		if (btrfs_file_extent_type(leaf, fi) ==
		    BTRFS_FILE_EXTENT_INLINE)
1202
			continue;
1203 1204 1205 1206
		/*
		 * FIXME make sure to insert a trans record that
		 * repeats the snapshot del on crash
		 */
1207
		disk_blocknr = btrfs_file_extent_disk_blocknr(leaf, fi);
C
Chris Mason 已提交
1208 1209 1210
		if (disk_blocknr == 0)
			continue;
		ret = btrfs_free_extent(trans, root, disk_blocknr,
1211
				btrfs_file_extent_disk_num_blocks(leaf, fi), 0);
1212 1213 1214 1215 1216
		BUG_ON(ret);
	}
	return 0;
}

1217
static void reada_walk_down(struct btrfs_root *root,
1218
			    struct extent_buffer *node)
1219 1220 1221 1222 1223 1224 1225
{
	int i;
	u32 nritems;
	u64 blocknr;
	int ret;
	u32 refs;

1226
	nritems = btrfs_header_nritems(node);
1227 1228 1229 1230 1231 1232
	for (i = 0; i < nritems; i++) {
		blocknr = btrfs_node_blockptr(node, i);
		ret = lookup_extent_ref(NULL, root, blocknr, 1, &refs);
		BUG_ON(ret);
		if (refs != 1)
			continue;
1233
		mutex_unlock(&root->fs_info->fs_mutex);
1234
		ret = readahead_tree_block(root, blocknr);
1235 1236
		cond_resched();
		mutex_lock(&root->fs_info->fs_mutex);
1237 1238 1239 1240 1241
		if (ret)
			break;
	}
}

C
Chris Mason 已提交
1242 1243 1244 1245
/*
 * helper function for drop_snapshot, this walks down the tree dropping ref
 * counts as it goes.
 */
1246 1247
static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
			  *root, struct btrfs_path *path, int *level)
C
Chris Mason 已提交
1248
{
1249 1250
	struct extent_buffer *next;
	struct extent_buffer *cur;
C
Chris Mason 已提交
1251 1252 1253 1254
	u64 blocknr;
	int ret;
	u32 refs;

1255 1256
	WARN_ON(*level < 0);
	WARN_ON(*level >= BTRFS_MAX_LEVEL);
1257 1258 1259
	ret = lookup_extent_ref(trans, root,
				extent_buffer_blocknr(path->nodes[*level]),
				1, &refs);
C
Chris Mason 已提交
1260 1261 1262
	BUG_ON(ret);
	if (refs > 1)
		goto out;
1263

C
Chris Mason 已提交
1264 1265 1266
	/*
	 * walk down to the last node level and free all the leaves
	 */
1267
	while(*level >= 0) {
1268 1269
		WARN_ON(*level < 0);
		WARN_ON(*level >= BTRFS_MAX_LEVEL);
C
Chris Mason 已提交
1270
		cur = path->nodes[*level];
1271 1272

		if (*level > 0 && path->slots[*level] == 0)
1273
			reada_walk_down(root, cur);
1274

1275
		if (btrfs_header_level(cur) != *level)
C
Chris Mason 已提交
1276
			WARN_ON(1);
1277

1278
		if (path->slots[*level] >=
1279
		    btrfs_header_nritems(cur))
C
Chris Mason 已提交
1280
			break;
1281 1282 1283 1284 1285
		if (*level == 0) {
			ret = drop_leaf_ref(trans, root, cur);
			BUG_ON(ret);
			break;
		}
1286
		blocknr = btrfs_node_blockptr(cur, path->slots[*level]);
C
Chris Mason 已提交
1287
		ret = lookup_extent_ref(trans, root, blocknr, 1, &refs);
1288 1289
		BUG_ON(ret);
		if (refs != 1) {
C
Chris Mason 已提交
1290
			path->slots[*level]++;
1291
			ret = btrfs_free_extent(trans, root, blocknr, 1, 1);
C
Chris Mason 已提交
1292 1293 1294
			BUG_ON(ret);
			continue;
		}
1295
		next = btrfs_find_tree_block(root, blocknr);
1296 1297
		if (!next || !btrfs_buffer_uptodate(next)) {
			free_extent_buffer(next);
1298 1299 1300 1301 1302 1303 1304 1305 1306
			mutex_unlock(&root->fs_info->fs_mutex);
			next = read_tree_block(root, blocknr);
			mutex_lock(&root->fs_info->fs_mutex);

			/* we dropped the lock, check one more time */
			ret = lookup_extent_ref(trans, root, blocknr, 1, &refs);
			BUG_ON(ret);
			if (refs != 1) {
				path->slots[*level]++;
1307
				free_extent_buffer(next);
1308 1309 1310 1311 1312 1313
				ret = btrfs_free_extent(trans, root,
							blocknr, 1, 1);
				BUG_ON(ret);
				continue;
			}
		}
1314
		WARN_ON(*level <= 0);
C
Chris Mason 已提交
1315
		if (path->nodes[*level-1])
1316
			free_extent_buffer(path->nodes[*level-1]);
C
Chris Mason 已提交
1317
		path->nodes[*level-1] = next;
1318
		*level = btrfs_header_level(next);
C
Chris Mason 已提交
1319 1320 1321
		path->slots[*level] = 0;
	}
out:
1322 1323
	WARN_ON(*level < 0);
	WARN_ON(*level >= BTRFS_MAX_LEVEL);
1324
	ret = btrfs_free_extent(trans, root,
1325 1326
			extent_buffer_blocknr(path->nodes[*level]), 1, 1);
	free_extent_buffer(path->nodes[*level]);
C
Chris Mason 已提交
1327 1328 1329 1330 1331 1332
	path->nodes[*level] = NULL;
	*level += 1;
	BUG_ON(ret);
	return 0;
}

C
Chris Mason 已提交
1333 1334 1335 1336 1337
/*
 * 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
 */
1338 1339
static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root
			*root, struct btrfs_path *path, int *level)
C
Chris Mason 已提交
1340 1341 1342 1343
{
	int i;
	int slot;
	int ret;
1344 1345
	struct btrfs_root_item *root_item = &root->root_item;

C
Chris Mason 已提交
1346
	for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
C
Chris Mason 已提交
1347
		slot = path->slots[i];
1348 1349 1350 1351
		if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
			struct extent_buffer *node;
			struct btrfs_disk_key disk_key;
			node = path->nodes[i];
C
Chris Mason 已提交
1352 1353
			path->slots[i]++;
			*level = i;
1354
			WARN_ON(*level == 0);
1355
			btrfs_node_key(node, &disk_key, path->slots[i]);
1356
			memcpy(&root_item->drop_progress,
1357
			       &disk_key, sizeof(disk_key));
1358
			root_item->drop_level = i;
C
Chris Mason 已提交
1359 1360
			return 0;
		} else {
1361
			ret = btrfs_free_extent(trans, root,
1362 1363
				    extent_buffer_blocknr(path->nodes[*level]),
				    1, 1);
1364
			BUG_ON(ret);
1365
			free_extent_buffer(path->nodes[*level]);
C
Chris Mason 已提交
1366
			path->nodes[*level] = NULL;
C
Chris Mason 已提交
1367 1368 1369 1370 1371 1372
			*level = i + 1;
		}
	}
	return 1;
}

C
Chris Mason 已提交
1373 1374 1375 1376 1377
/*
 * 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.
 */
1378
int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
1379
			*root)
C
Chris Mason 已提交
1380
{
1381
	int ret = 0;
C
Chris Mason 已提交
1382
	int wret;
C
Chris Mason 已提交
1383
	int level;
1384
	struct btrfs_path *path;
C
Chris Mason 已提交
1385 1386
	int i;
	int orig_level;
1387
	struct btrfs_root_item *root_item = &root->root_item;
C
Chris Mason 已提交
1388

1389 1390
	path = btrfs_alloc_path();
	BUG_ON(!path);
C
Chris Mason 已提交
1391

1392
	level = btrfs_header_level(root->node);
C
Chris Mason 已提交
1393
	orig_level = level;
1394 1395
	if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
		path->nodes[level] = root->node;
1396
		extent_buffer_get(root->node);
1397 1398 1399
		path->slots[level] = 0;
	} else {
		struct btrfs_key key;
1400 1401
		struct btrfs_disk_key found_key;
		struct extent_buffer *node;
1402

1403
		btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
1404 1405
		level = root_item->drop_level;
		path->lowest_level = level;
1406
		wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1407
		if (wret < 0) {
1408 1409 1410
			ret = wret;
			goto out;
		}
1411 1412 1413 1414
		node = path->nodes[level];
		btrfs_node_key(node, &found_key, path->slots[level]);
		WARN_ON(memcmp(&found_key, &root_item->drop_progress,
			       sizeof(found_key)));
1415
	}
C
Chris Mason 已提交
1416
	while(1) {
1417
		wret = walk_down_tree(trans, root, path, &level);
C
Chris Mason 已提交
1418
		if (wret > 0)
C
Chris Mason 已提交
1419
			break;
C
Chris Mason 已提交
1420 1421 1422
		if (wret < 0)
			ret = wret;

1423
		wret = walk_up_tree(trans, root, path, &level);
C
Chris Mason 已提交
1424
		if (wret > 0)
C
Chris Mason 已提交
1425
			break;
C
Chris Mason 已提交
1426 1427
		if (wret < 0)
			ret = wret;
1428 1429
		ret = -EAGAIN;
		break;
C
Chris Mason 已提交
1430
	}
C
Chris Mason 已提交
1431
	for (i = 0; i <= orig_level; i++) {
1432
		if (path->nodes[i]) {
1433
			free_extent_buffer(path->nodes[i]);
1434
			path->nodes[i] = 0;
C
Chris Mason 已提交
1435
		}
C
Chris Mason 已提交
1436
	}
1437
out:
1438
	btrfs_free_path(path);
C
Chris Mason 已提交
1439
	return ret;
C
Chris Mason 已提交
1440
}
C
Chris Mason 已提交
1441

1442
int btrfs_free_block_groups(struct btrfs_fs_info *info)
C
Chris Mason 已提交
1443
{
1444 1445
	u64 start;
	u64 end;
C
Chris Mason 已提交
1446 1447 1448
	int ret;

	while(1) {
1449 1450 1451
		ret = find_first_extent_bit(&info->block_group_cache, 0,
					    &start, &end, (unsigned int)-1);
		if (ret)
C
Chris Mason 已提交
1452
			break;
1453 1454
		clear_extent_bits(&info->block_group_cache, start,
				  end, (unsigned int)-1, GFP_NOFS);
C
Chris Mason 已提交
1455
	}
1456
	while(1) {
1457 1458 1459
		ret = find_first_extent_bit(&info->free_space_cache, 0,
					    &start, &end, EXTENT_DIRTY);
		if (ret)
1460
			break;
1461 1462
		clear_extent_dirty(&info->free_space_cache, start,
				   end, GFP_NOFS);
1463
	}
C
Chris Mason 已提交
1464 1465 1466
	return 0;
}

C
Chris Mason 已提交
1467 1468 1469 1470 1471
int btrfs_read_block_groups(struct btrfs_root *root)
{
	struct btrfs_path *path;
	int ret;
	int err = 0;
1472
	int bit;
C
Chris Mason 已提交
1473
	struct btrfs_block_group_cache *cache;
C
Chris Mason 已提交
1474
	struct btrfs_fs_info *info = root->fs_info;
1475
	struct extent_map_tree *block_group_cache;
C
Chris Mason 已提交
1476 1477
	struct btrfs_key key;
	struct btrfs_key found_key;
1478
	struct extent_buffer *leaf;
C
Chris Mason 已提交
1479
	u64 group_size_blocks;
1480 1481

	block_group_cache = &info->block_group_cache;
C
Chris Mason 已提交
1482

C
Chris Mason 已提交
1483
	group_size_blocks = BTRFS_BLOCK_GROUP_SIZE >>
1484 1485
		info->sb->s_blocksize_bits;

C
Chris Mason 已提交
1486
	root = info->extent_root;
C
Chris Mason 已提交
1487 1488 1489 1490 1491 1492 1493 1494 1495
	key.objectid = 0;
	key.offset = group_size_blocks;
	btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);

	path = btrfs_alloc_path();
	if (!path)
		return -ENOMEM;

	while(1) {
C
Chris Mason 已提交
1496
		ret = btrfs_search_slot(NULL, info->extent_root,
C
Chris Mason 已提交
1497 1498 1499 1500 1501
					&key, path, 0, 0);
		if (ret != 0) {
			err = ret;
			break;
		}
1502 1503
		leaf = path->nodes[0];
		btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
C
Chris Mason 已提交
1504 1505 1506 1507 1508
		cache = kmalloc(sizeof(*cache), GFP_NOFS);
		if (!cache) {
			err = -1;
			break;
		}
C
Chris Mason 已提交
1509

1510 1511 1512
		read_extent_buffer(leaf, &cache->item,
				   btrfs_item_ptr_offset(leaf, path->slots[0]),
				   sizeof(cache->item));
C
Chris Mason 已提交
1513
		memcpy(&cache->key, &found_key, sizeof(found_key));
1514 1515
		cache->cached = 0;

C
Chris Mason 已提交
1516 1517
		key.objectid = found_key.objectid + found_key.offset;
		btrfs_release_path(root, path);
1518

1519 1520 1521 1522 1523 1524
		if (cache->item.flags & BTRFS_BLOCK_GROUP_DATA) {
			bit = BLOCK_GROUP_DATA;
			cache->data = 1;
		} else {
			bit = BLOCK_GROUP_METADATA;
			cache->data = 0;
1525
		}
1526 1527 1528 1529 1530 1531 1532 1533

		/* use EXTENT_LOCKED to prevent merging */
		set_extent_bits(block_group_cache, found_key.objectid,
				found_key.objectid + found_key.offset - 1,
				bit | EXTENT_LOCKED, GFP_NOFS);
		set_state_private(block_group_cache, found_key.objectid,
				  (u64)cache);

C
Chris Mason 已提交
1534
		if (key.objectid >=
1535
		    btrfs_super_total_blocks(&info->super_copy))
C
Chris Mason 已提交
1536 1537 1538 1539 1540 1541
			break;
	}

	btrfs_free_path(path);
	return 0;
}