extent-tree.c 38.7 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 191 192 193
	}
out:
	return max(cache->last_alloc, search_start);

new_group:
194 195
	cache = btrfs_lookup_block_group(root->fs_info,
					 last + cache->key.offset - 1);
196 197 198 199
	if (!cache) {
		return max((*cache_ret)->last_alloc, search_start);
	}
	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 250
			used = btrfs_block_group_used(&shint->item);
			if (used + shint->pinned <
C
Chris Mason 已提交
251
			    div_factor(shint->key.offset, factor)) {
C
Chris Mason 已提交
252 253 254 255 256
				return shint;
			}
		}
	}
	if (hint && hint->data == data) {
257
		used = btrfs_block_group_used(&hint->item);
C
Chris Mason 已提交
258 259
		if (used + hint->pinned <
		    div_factor(hint->key.offset, factor)) {
260 261
			return hint;
		}
262
		last = hint->key.offset * 3;
C
Chris Mason 已提交
263
		if (hint->key.objectid >= last)
264 265
			last = max(search_start + hint->key.offset - 1,
				   hint->key.objectid - last);
C
Chris Mason 已提交
266 267
		else
			last = hint->key.objectid + hint->key.offset;
268 269
		hint_last = last;
	} else {
270 271 272 273 274 275
		if (hint)
			hint_last = max(hint->key.objectid, search_start);
		else
			hint_last = search_start;

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

		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);

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

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

329
	path = btrfs_alloc_path();
330 331
	if (!path)
		return -ENOMEM;
332

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

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

357 358 359 360 361 362 363 364
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 已提交
365 366 367
static int lookup_extent_ref(struct btrfs_trans_handle *trans,
			     struct btrfs_root *root, u64 blocknr,
			     u64 num_blocks, u32 *refs)
368
{
369
	struct btrfs_path *path;
370
	int ret;
C
Chris Mason 已提交
371
	struct btrfs_key key;
372
	struct extent_buffer *l;
C
Chris Mason 已提交
373
	struct btrfs_extent_item *item;
374 375

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

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

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

416
	if (!root->ref_cows)
417
		return 0;
418 419 420 421

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

C
Chris Mason 已提交
480 481 482 483 484 485 486 487
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;
488 489
	unsigned long bi;
	struct extent_buffer *leaf;
C
Chris Mason 已提交
490 491

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

	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 已提交
500
	btrfs_release_path(extent_root, path);
501
fail:
C
Chris Mason 已提交
502 503 504 505 506 507
	finish_current_insert(trans, extent_root);
	pending_ret = del_pending_extents(trans, extent_root);
	if (ret)
		return ret;
	if (pending_ret)
		return pending_ret;
C
Chris Mason 已提交
508 509
	if (cache->data)
		cache->last_alloc = cache->first_free;
C
Chris Mason 已提交
510 511 512 513
	return 0;

}

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

528
	block_group_cache = &root->fs_info->block_group_cache;
C
Chris Mason 已提交
529 530 531 532 533
	path = btrfs_alloc_path();
	if (!path)
		return -ENOMEM;

	while(1) {
534 535 536
		ret = find_first_extent_bit(block_group_cache, last,
					    &start, &end, BLOCK_GROUP_DIRTY);
		if (ret)
C
Chris Mason 已提交
537
			break;
538

539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554
		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 已提交
555
		}
556 557
		clear_extent_bits(block_group_cache, start, end,
				  BLOCK_GROUP_DIRTY, GFP_NOFS);
C
Chris Mason 已提交
558 559 560 561 562 563 564
	}
	btrfs_free_path(path);
	return werr;
}

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

C
Chris Mason 已提交
576
	while(total) {
577
		cache = btrfs_lookup_block_group(info, blocknr);
C
Chris Mason 已提交
578
		if (!cache) {
C
Chris Mason 已提交
579
			return -1;
C
Chris Mason 已提交
580
		}
C
Chris Mason 已提交
581 582
		block_in_group = blocknr - cache->key.objectid;
		WARN_ON(block_in_group > cache->key.offset);
583 584 585 586
		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 已提交
587 588 589

		old_val = btrfs_block_group_used(&cache->item);
		num = min(total, cache->key.offset - block_in_group);
C
Chris Mason 已提交
590 591 592
		if (alloc) {
			if (blocknr > cache->last_alloc)
				cache->last_alloc = blocknr;
C
Chris Mason 已提交
593
			if (cache->data != data &&
C
Chris Mason 已提交
594
			    old_val < (cache->key.offset >> 1)) {
595 596
				int bit_to_clear;
				int bit_to_set;
C
Chris Mason 已提交
597

598
				cache->data = data;
C
Chris Mason 已提交
599
				if (data) {
600 601
					bit_to_clear = BLOCK_GROUP_DATA;
					bit_to_set = BLOCK_GROUP_METADATA;
C
Chris Mason 已提交
602 603 604
					cache->item.flags |=
						BTRFS_BLOCK_GROUP_DATA;
				} else {
605 606
					bit_to_clear = BLOCK_GROUP_METADATA;
					bit_to_set = BLOCK_GROUP_DATA;
C
Chris Mason 已提交
607 608 609
					cache->item.flags &=
						~BTRFS_BLOCK_GROUP_DATA;
				}
610 611 612 613 614 615
				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 已提交
616 617
			}
			old_val += num;
C
Chris Mason 已提交
618
		} else {
C
Chris Mason 已提交
619
			old_val -= num;
C
Chris Mason 已提交
620 621
			if (blocknr < cache->first_free)
				cache->first_free = blocknr;
622 623 624 625
			if (mark_free) {
				set_extent_dirty(&info->free_space_cache,
						 blocknr, blocknr + num - 1,
						 GFP_NOFS);
626
			}
C
Chris Mason 已提交
627
		}
C
Chris Mason 已提交
628
		btrfs_set_block_group_used(&cache->item, old_val);
629 630
		total -= num;
		blocknr += num;
C
Chris Mason 已提交
631 632 633 634
	}
	return 0;
}

C
Chris Mason 已提交
635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652
int btrfs_copy_pinned(struct btrfs_root *root, struct radix_tree_root *copy)
{
	unsigned long gang[8];
	u64 last = 0;
	struct radix_tree_root *pinned_radix = &root->fs_info->pinned_radix;
	int ret;
	int i;

	while(1) {
		ret = find_first_radix_bit(pinned_radix, gang, last,
					   ARRAY_SIZE(gang));
		if (!ret)
			break;
		for (i = 0 ; i < ret; i++) {
			set_radix_bit(copy, gang[i]);
			last = gang[i] + 1;
		}
	}
653 654 655
	ret = find_first_radix_bit(&root->fs_info->extent_ins_radix, gang, 0,
				   ARRAY_SIZE(gang));
	WARN_ON(ret);
C
Chris Mason 已提交
656 657 658 659 660 661
	return 0;
}

int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
			       struct btrfs_root *root,
			       struct radix_tree_root *unpin_radix)
662
{
C
Chris Mason 已提交
663
	unsigned long gang[8];
C
Chris Mason 已提交
664
	struct btrfs_block_group_cache *block_group;
665
	u64 first = 0;
666 667
	int ret;
	int i;
C
Chris Mason 已提交
668
	struct radix_tree_root *pinned_radix = &root->fs_info->pinned_radix;
669 670 671
	struct extent_map_tree *free_space_cache;

	free_space_cache = &root->fs_info->free_space_cache;
672 673

	while(1) {
C
Chris Mason 已提交
674
		ret = find_first_radix_bit(unpin_radix, gang, 0,
C
Chris Mason 已提交
675
					   ARRAY_SIZE(gang));
676 677
		if (!ret)
			break;
678
		if (!first)
C
Chris Mason 已提交
679
			first = gang[0];
680
		for (i = 0; i < ret; i++) {
C
Chris Mason 已提交
681
			clear_radix_bit(pinned_radix, gang[i]);
C
Chris Mason 已提交
682
			clear_radix_bit(unpin_radix, gang[i]);
683 684
			block_group = btrfs_lookup_block_group(root->fs_info,
							       gang[i]);
C
Chris Mason 已提交
685 686 687 688 689
			if (block_group) {
				WARN_ON(block_group->pinned == 0);
				block_group->pinned--;
				if (gang[i] < block_group->last_alloc)
					block_group->last_alloc = gang[i];
690 691
				set_extent_dirty(free_space_cache,
						 gang[i], gang[i], GFP_NOFS);
C
Chris Mason 已提交
692
			}
693
		}
694 695 696 697
	}
	return 0;
}

698 699
static int finish_current_insert(struct btrfs_trans_handle *trans, struct
				 btrfs_root *extent_root)
C
Chris Mason 已提交
700
{
C
Chris Mason 已提交
701
	struct btrfs_key ins;
C
Chris Mason 已提交
702
	struct btrfs_extent_item extent_item;
C
Chris Mason 已提交
703 704
	int i;
	int ret;
705 706
	int err;
	unsigned long gang[8];
707
	struct btrfs_fs_info *info = extent_root->fs_info;
C
Chris Mason 已提交
708

709
	btrfs_set_stack_extent_refs(&extent_item, 1);
C
Chris Mason 已提交
710
	ins.offset = 1;
711
	btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
712 713
	btrfs_set_stack_extent_owner(&extent_item,
				     extent_root->root_key.objectid);
C
Chris Mason 已提交
714

715 716 717 718 719 720 721 722 723 724 725 726 727 728
	while(1) {
		ret = find_first_radix_bit(&info->extent_ins_radix, gang, 0,
					   ARRAY_SIZE(gang));
		if (!ret)
			break;

		for (i = 0; i < ret; i++) {
			ins.objectid = gang[i];
			err = btrfs_insert_item(trans, extent_root, &ins,
						&extent_item,
						sizeof(extent_item));
			clear_radix_bit(&info->extent_ins_radix, gang[i]);
			WARN_ON(err);
		}
C
Chris Mason 已提交
729 730 731 732
	}
	return 0;
}

C
Chris Mason 已提交
733
static int pin_down_block(struct btrfs_root *root, u64 blocknr, int pending)
C
Chris Mason 已提交
734 735
{
	int err;
736
	struct extent_buffer *buf;
C
Chris Mason 已提交
737

C
Chris Mason 已提交
738
	if (!pending) {
739 740 741
		buf = btrfs_find_tree_block(root, blocknr);
		if (buf) {
			if (btrfs_buffer_uptodate(buf)) {
C
Chris Mason 已提交
742 743
				u64 transid =
				    root->fs_info->running_transaction->transid;
744 745
				if (btrfs_header_generation(buf) == transid) {
					free_extent_buffer(buf);
C
Chris Mason 已提交
746 747
					return 0;
				}
C
Chris Mason 已提交
748
			}
749
			free_extent_buffer(buf);
C
Chris Mason 已提交
750 751
		}
		err = set_radix_bit(&root->fs_info->pinned_radix, blocknr);
C
Chris Mason 已提交
752 753
		if (!err) {
			struct btrfs_block_group_cache *cache;
754 755
			cache = btrfs_lookup_block_group(root->fs_info,
							 blocknr);
C
Chris Mason 已提交
756 757 758
			if (cache)
				cache->pinned++;
		}
C
Chris Mason 已提交
759 760 761
	} else {
		err = set_radix_bit(&root->fs_info->pending_del_radix, blocknr);
	}
C
Chris Mason 已提交
762
	BUG_ON(err < 0);
C
Chris Mason 已提交
763 764 765
	return 0;
}

766
/*
767
 * remove an extent from the root, returns 0 on success
768
 */
769
static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
770 771
			 *root, u64 blocknr, u64 num_blocks, int pin,
			 int mark_free)
772
{
773
	struct btrfs_path *path;
C
Chris Mason 已提交
774
	struct btrfs_key key;
775 776
	struct btrfs_fs_info *info = root->fs_info;
	struct btrfs_root *extent_root = info->extent_root;
777
	struct extent_buffer *leaf;
778
	int ret;
C
Chris Mason 已提交
779
	struct btrfs_extent_item *ei;
C
Chris Mason 已提交
780
	u32 refs;
C
Chris Mason 已提交
781

782
	key.objectid = blocknr;
783
	btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
784 785
	key.offset = num_blocks;

786
	path = btrfs_alloc_path();
787 788
	if (!path)
		return -ENOMEM;
789

790 791 792 793
	ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
	if (ret < 0)
		return ret;
	BUG_ON(ret);
794 795 796

	leaf = path->nodes[0];
	ei = btrfs_item_ptr(leaf, path->slots[0],
C
Chris Mason 已提交
797
			    struct btrfs_extent_item);
798 799 800 801 802 803
	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 已提交
804
	if (refs == 0) {
805
		u64 super_blocks_used, root_blocks_used;
C
Chris Mason 已提交
806 807

		if (pin) {
C
Chris Mason 已提交
808
			ret = pin_down_block(root, blocknr, 0);
C
Chris Mason 已提交
809 810 811
			BUG_ON(ret);
		}

812
		/* block accounting for super block */
813 814
		super_blocks_used = btrfs_super_blocks_used(&info->super_copy);
		btrfs_set_super_blocks_used(&info->super_copy,
815
					    super_blocks_used - num_blocks);
816 817

		/* block accounting for root item */
818 819
		root_blocks_used = btrfs_root_used(&root->root_item);
		btrfs_set_root_used(&root->root_item,
820 821
					   root_blocks_used - num_blocks);

822
		ret = btrfs_del_item(trans, extent_root, path);
823 824 825
		if (ret) {
			return ret;
		}
826
		ret = update_block_group(trans, root, blocknr, num_blocks, 0,
C
Chris Mason 已提交
827
					 mark_free, 0);
C
Chris Mason 已提交
828
		BUG_ON(ret);
829
	}
830
	btrfs_free_path(path);
831
	finish_current_insert(trans, extent_root);
832 833 834 835 836 837 838
	return ret;
}

/*
 * find all the blocks marked as pending in the radix tree and remove
 * them from the extent map
 */
839 840
static int del_pending_extents(struct btrfs_trans_handle *trans, struct
			       btrfs_root *extent_root)
841 842
{
	int ret;
C
Chris Mason 已提交
843 844
	int wret;
	int err = 0;
C
Chris Mason 已提交
845
	unsigned long gang[4];
846
	int i;
C
Chris Mason 已提交
847 848
	struct radix_tree_root *pending_radix;
	struct radix_tree_root *pinned_radix;
C
Chris Mason 已提交
849
	struct btrfs_block_group_cache *cache;
C
Chris Mason 已提交
850 851 852

	pending_radix = &extent_root->fs_info->pending_del_radix;
	pinned_radix = &extent_root->fs_info->pinned_radix;
853 854

	while(1) {
855
		ret = find_first_radix_bit(pending_radix, gang, 0,
C
Chris Mason 已提交
856
					   ARRAY_SIZE(gang));
857 858 859
		if (!ret)
			break;
		for (i = 0; i < ret; i++) {
C
Chris Mason 已提交
860
			wret = set_radix_bit(pinned_radix, gang[i]);
C
Chris Mason 已提交
861
			if (wret == 0) {
862 863
				cache =
				  btrfs_lookup_block_group(extent_root->fs_info,
C
Chris Mason 已提交
864 865 866 867 868 869 870 871 872
							   gang[i]);
				if (cache)
					cache->pinned++;
			}
			if (wret < 0) {
				printk(KERN_CRIT "set_radix_bit, err %d\n",
				       wret);
				BUG_ON(wret < 0);
			}
C
Chris Mason 已提交
873 874
			wret = clear_radix_bit(pending_radix, gang[i]);
			BUG_ON(wret);
875
			wret = __free_extent(trans, extent_root,
876
					     gang[i], 1, 0, 0);
C
Chris Mason 已提交
877 878
			if (wret)
				err = wret;
879 880
		}
	}
C
Chris Mason 已提交
881
	return err;
882 883 884 885 886
}

/*
 * remove an extent from the root, returns 0 on success
 */
887 888
int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
		      *root, u64 blocknr, u64 num_blocks, int pin)
889
{
890
	struct btrfs_root *extent_root = root->fs_info->extent_root;
891 892
	int pending_ret;
	int ret;
893

894
	if (root == extent_root) {
C
Chris Mason 已提交
895
		pin_down_block(root, blocknr, 1);
896 897
		return 0;
	}
898
	ret = __free_extent(trans, root, blocknr, num_blocks, pin, pin == 0);
C
Chris Mason 已提交
899
	pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
900 901 902 903 904 905 906
	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
907
 * ins->flags = BTRFS_EXTENT_ITEM_KEY
908 909 910
 * ins->offset == number of blocks
 * Any available blocks before search_start are skipped.
 */
911
static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
912 913
			    *orig_root, u64 num_blocks, u64 empty_size,
			    u64 search_start, u64 search_end, u64 hint_block,
914 915
			    struct btrfs_key *ins, u64 exclude_start,
			    u64 exclude_nr, int data)
916
{
917
	struct btrfs_path *path;
C
Chris Mason 已提交
918
	struct btrfs_key key;
919 920 921
	int ret;
	u64 hole_size = 0;
	int slot = 0;
C
Chris Mason 已提交
922
	u64 last_block = 0;
C
Chris Mason 已提交
923
	u64 test_block;
C
Chris Mason 已提交
924
	u64 orig_search_start = search_start;
925
	int start_found;
926
	struct extent_buffer *l;
927
	struct btrfs_root * root = orig_root->fs_info->extent_root;
928
	struct btrfs_fs_info *info = root->fs_info;
929
	int total_needed = num_blocks;
C
Chris Mason 已提交
930
	int level;
C
Chris Mason 已提交
931
	struct btrfs_block_group_cache *block_group;
C
Chris Mason 已提交
932
	int full_scan = 0;
933
	int wrapped = 0;
934
	u64 cached_search_start = 0;
935

936
	WARN_ON(num_blocks < 1);
937 938
	btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);

939 940
	level = btrfs_header_level(root->node);

C
Chris Mason 已提交
941
	if (search_end == (u64)-1)
942
		search_end = btrfs_super_total_blocks(&info->super_copy);
943
	if (hint_block) {
944
		block_group = btrfs_lookup_block_group(info, hint_block);
C
Chris Mason 已提交
945
		block_group = btrfs_find_block_group(root, block_group,
946
						     hint_block, data, 1);
C
Chris Mason 已提交
947 948 949
	} else {
		block_group = btrfs_find_block_group(root,
						     trans->block_group, 0,
950
						     data, 1);
C
Chris Mason 已提交
951 952
	}

953
	total_needed += empty_size;
954 955
	path = btrfs_alloc_path();

C
Chris Mason 已提交
956
check_failed:
957 958 959
	search_start = find_search_start(root, &block_group,
					 search_start, total_needed, data);
	cached_search_start = search_start;
960

961
	btrfs_init_path(path);
962 963 964
	ins->objectid = search_start;
	ins->offset = 0;
	start_found = 0;
965
	path->reada = 2;
966

967
	ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
C
Chris Mason 已提交
968 969
	if (ret < 0)
		goto error;
C
Chris Mason 已提交
970

971
	if (path->slots[0] > 0) {
972
		path->slots[0]--;
973 974
	}

975 976 977
	l = path->nodes[0];
	btrfs_item_key_to_cpu(l, &key, path->slots[0]);

978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994
	/*
	 * 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]--;
		}
	}
995

996
	while (1) {
997
		l = path->nodes[0];
998
		slot = path->slots[0];
999
		if (slot >= btrfs_header_nritems(l)) {
1000
			ret = btrfs_next_leaf(root, path);
1001 1002
			if (ret == 0)
				continue;
C
Chris Mason 已提交
1003 1004
			if (ret < 0)
				goto error;
1005 1006
			if (!start_found) {
				ins->objectid = search_start;
C
Chris Mason 已提交
1007
				ins->offset = search_end - search_start;
1008 1009 1010 1011 1012
				start_found = 1;
				goto check_pending;
			}
			ins->objectid = last_block > search_start ?
					last_block : search_start;
C
Chris Mason 已提交
1013
			ins->offset = search_end - ins->objectid;
1014 1015
			goto check_pending;
		}
1016
		btrfs_item_key_to_cpu(l, &key, slot);
1017

1018 1019 1020 1021 1022 1023 1024 1025 1026
		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;
1027
			}
1028
		}
1029 1030 1031 1032 1033
		if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY) {
			if (!start_found) {
				last_block = key.objectid;
				start_found = 1;
			}
1034
			goto next;
1035 1036
		}

1037

1038
		start_found = 1;
C
Chris Mason 已提交
1039
		last_block = key.objectid + key.offset;
1040

1041
		if (!full_scan && last_block >= block_group->key.objectid +
C
Chris Mason 已提交
1042 1043 1044 1045 1046 1047
		    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 已提交
1048
next:
1049
		path->slots[0]++;
1050
		cond_resched();
1051 1052 1053 1054 1055
	}
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
	 */
1056
	btrfs_release_path(root, path);
1057
	BUG_ON(ins->objectid < search_start);
1058

1059 1060 1061
	if (ins->objectid + num_blocks >= search_end)
		goto enospc;

C
Chris Mason 已提交
1062
	for (test_block = ins->objectid;
1063
	     test_block < ins->objectid + num_blocks; test_block++) {
1064 1065
		if (test_radix_bit(&info->pinned_radix, test_block) ||
		    test_radix_bit(&info->extent_ins_radix, test_block)) {
C
Chris Mason 已提交
1066
			search_start = test_block + 1;
C
Chris Mason 已提交
1067
			goto new_group;
1068 1069
		}
	}
1070 1071 1072 1073 1074
	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;
	}
1075
	if (!data) {
1076
		block_group = btrfs_lookup_block_group(info, ins->objectid);
1077 1078
		if (block_group)
			trans->block_group = block_group;
1079
	}
C
Chris Mason 已提交
1080
	ins->offset = num_blocks;
1081
	btrfs_free_path(path);
1082
	return 0;
C
Chris Mason 已提交
1083 1084

new_group:
C
Chris Mason 已提交
1085
	if (search_start + num_blocks >= search_end) {
1086
enospc:
C
Chris Mason 已提交
1087
		search_start = orig_search_start;
1088 1089 1090 1091
		if (full_scan) {
			ret = -ENOSPC;
			goto error;
		}
1092 1093 1094
		if (wrapped) {
			if (!full_scan)
				total_needed -= empty_size;
1095
			full_scan = 1;
1096
		} else
1097
			wrapped = 1;
C
Chris Mason 已提交
1098
	}
1099
	block_group = btrfs_lookup_block_group(info, search_start);
1100
	cond_resched();
C
Chris Mason 已提交
1101 1102
	if (!full_scan)
		block_group = btrfs_find_block_group(root, block_group,
1103
						     search_start, data, 0);
C
Chris Mason 已提交
1104 1105
	goto check_failed;

C
Chris Mason 已提交
1106
error:
1107 1108
	btrfs_release_path(root, path);
	btrfs_free_path(path);
C
Chris Mason 已提交
1109
	return ret;
1110 1111 1112 1113 1114 1115 1116 1117
}
/*
 * 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.
 */
1118 1119
int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
		       struct btrfs_root *root, u64 owner,
1120
		       u64 num_blocks, u64 empty_size, u64 hint_block,
C
Chris Mason 已提交
1121
		       u64 search_end, struct btrfs_key *ins, int data)
1122 1123 1124
{
	int ret;
	int pending_ret;
1125
	u64 super_blocks_used, root_blocks_used;
1126
	u64 search_start = 0;
1127 1128
	struct btrfs_fs_info *info = root->fs_info;
	struct btrfs_root *extent_root = info->extent_root;
C
Chris Mason 已提交
1129
	struct btrfs_extent_item extent_item;
C
Chris Mason 已提交
1130

1131 1132
	btrfs_set_stack_extent_refs(&extent_item, 1);
	btrfs_set_stack_extent_owner(&extent_item, owner);
1133

1134
	WARN_ON(num_blocks < 1);
1135 1136
	ret = find_free_extent(trans, root, num_blocks, empty_size,
			       search_start, search_end, hint_block, ins,
1137 1138
			       trans->alloc_exclude_start,
			       trans->alloc_exclude_nr, data);
C
Chris Mason 已提交
1139
	BUG_ON(ret);
1140 1141
	if (ret)
		return ret;
1142

1143
	/* block accounting for super block */
1144 1145
	super_blocks_used = btrfs_super_blocks_used(&info->super_copy);
	btrfs_set_super_blocks_used(&info->super_copy, super_blocks_used +
1146
				    num_blocks);
1147

1148
	/* block accounting for root item */
1149 1150
	root_blocks_used = btrfs_root_used(&root->root_item);
	btrfs_set_root_used(&root->root_item, root_blocks_used +
1151 1152
				   num_blocks);

1153 1154 1155 1156
	clear_extent_dirty(&root->fs_info->free_space_cache,
			   ins->objectid, ins->objectid + ins->offset - 1,
			   GFP_NOFS);

1157 1158 1159 1160 1161 1162 1163 1164 1165
	if (root == extent_root) {
		BUG_ON(num_blocks != 1);
		set_radix_bit(&root->fs_info->extent_ins_radix, ins->objectid);
		goto update_block;
	}

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

1169 1170 1171
	trans->alloc_exclude_start = 0;
	trans->alloc_exclude_nr = 0;

C
Chris Mason 已提交
1172
	BUG_ON(ret);
1173
	finish_current_insert(trans, extent_root);
C
Chris Mason 已提交
1174
	pending_ret = del_pending_extents(trans, extent_root);
1175

1176
	if (ret) {
C
Chris Mason 已提交
1177
		return ret;
1178 1179
	}
	if (pending_ret) {
C
Chris Mason 已提交
1180
		return pending_ret;
1181
	}
1182 1183

update_block:
C
Chris Mason 已提交
1184 1185
	ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0,
				 data);
C
Chris Mason 已提交
1186
	BUG_ON(ret);
C
Chris Mason 已提交
1187
	return 0;
1188 1189 1190 1191 1192 1193
}

/*
 * helper function to allocate a block for a given tree
 * returns the tree buffer or NULL.
 */
1194 1195 1196
struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
					     struct btrfs_root *root, u64 hint,
					     u64 empty_size)
1197
{
C
Chris Mason 已提交
1198
	struct btrfs_key ins;
1199
	int ret;
1200
	struct extent_buffer *buf;
1201

1202
	ret = btrfs_alloc_extent(trans, root, root->root_key.objectid,
1203
				 1, empty_size, hint, (u64)-1, &ins, 0);
1204
	if (ret) {
1205 1206
		BUG_ON(ret > 0);
		return ERR_PTR(ret);
1207
	}
1208
	buf = btrfs_find_create_tree_block(root, ins.objectid);
1209 1210 1211 1212
	if (!buf) {
		btrfs_free_extent(trans, root, ins.objectid, 1, 0);
		return ERR_PTR(-ENOMEM);
	}
1213
	btrfs_set_buffer_uptodate(buf);
1214
	buf->alloc_addr = (unsigned long)__builtin_return_address(0);
1215 1216 1217
	set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
			 buf->start + buf->len - 1, GFP_NOFS);
	/*
C
Chris Mason 已提交
1218
	set_buffer_checked(buf);
1219
	set_buffer_defrag(buf);
1220 1221 1222 1223
	*/
	/* FIXME!!!!!!!!!!!!!!!!
	set_radix_bit(&trans->transaction->dirty_pages, buf->pages[0]->index);
	*/
1224
	trans->blocks_used++;
1225 1226
	return buf;
}
1227

1228
static int drop_leaf_ref(struct btrfs_trans_handle *trans,
1229
			 struct btrfs_root *root, struct extent_buffer *leaf)
1230
{
1231
	struct btrfs_key key;
1232 1233 1234 1235 1236
	struct btrfs_file_extent_item *fi;
	int i;
	int nritems;
	int ret;

1237 1238
	BUG_ON(!btrfs_is_leaf(leaf));
	nritems = btrfs_header_nritems(leaf);
1239
	for (i = 0; i < nritems; i++) {
C
Chris Mason 已提交
1240
		u64 disk_blocknr;
1241 1242 1243

		btrfs_item_key_to_cpu(leaf, &key, i);
		if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1244 1245
			continue;
		fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
1246 1247
		if (btrfs_file_extent_type(leaf, fi) ==
		    BTRFS_FILE_EXTENT_INLINE)
1248
			continue;
1249 1250 1251 1252
		/*
		 * FIXME make sure to insert a trans record that
		 * repeats the snapshot del on crash
		 */
1253
		disk_blocknr = btrfs_file_extent_disk_blocknr(leaf, fi);
C
Chris Mason 已提交
1254 1255 1256
		if (disk_blocknr == 0)
			continue;
		ret = btrfs_free_extent(trans, root, disk_blocknr,
1257
				btrfs_file_extent_disk_num_blocks(leaf, fi), 0);
1258 1259 1260 1261 1262
		BUG_ON(ret);
	}
	return 0;
}

1263
static void reada_walk_down(struct btrfs_root *root,
1264
			    struct extent_buffer *node)
1265 1266 1267 1268 1269 1270 1271
{
	int i;
	u32 nritems;
	u64 blocknr;
	int ret;
	u32 refs;

1272
	nritems = btrfs_header_nritems(node);
1273 1274 1275 1276 1277 1278
	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;
1279
		mutex_unlock(&root->fs_info->fs_mutex);
1280
		ret = readahead_tree_block(root, blocknr);
1281 1282
		cond_resched();
		mutex_lock(&root->fs_info->fs_mutex);
1283 1284 1285 1286 1287
		if (ret)
			break;
	}
}

C
Chris Mason 已提交
1288 1289 1290 1291
/*
 * helper function for drop_snapshot, this walks down the tree dropping ref
 * counts as it goes.
 */
1292 1293
static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
			  *root, struct btrfs_path *path, int *level)
C
Chris Mason 已提交
1294
{
1295 1296
	struct extent_buffer *next;
	struct extent_buffer *cur;
C
Chris Mason 已提交
1297 1298 1299 1300
	u64 blocknr;
	int ret;
	u32 refs;

1301 1302
	WARN_ON(*level < 0);
	WARN_ON(*level >= BTRFS_MAX_LEVEL);
1303 1304 1305
	ret = lookup_extent_ref(trans, root,
				extent_buffer_blocknr(path->nodes[*level]),
				1, &refs);
C
Chris Mason 已提交
1306 1307 1308
	BUG_ON(ret);
	if (refs > 1)
		goto out;
1309

C
Chris Mason 已提交
1310 1311 1312
	/*
	 * walk down to the last node level and free all the leaves
	 */
1313
	while(*level >= 0) {
1314 1315
		WARN_ON(*level < 0);
		WARN_ON(*level >= BTRFS_MAX_LEVEL);
C
Chris Mason 已提交
1316
		cur = path->nodes[*level];
1317 1318

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

1321
		if (btrfs_header_level(cur) != *level)
C
Chris Mason 已提交
1322
			WARN_ON(1);
1323

1324
		if (path->slots[*level] >=
1325
		    btrfs_header_nritems(cur))
C
Chris Mason 已提交
1326
			break;
1327 1328 1329 1330 1331
		if (*level == 0) {
			ret = drop_leaf_ref(trans, root, cur);
			BUG_ON(ret);
			break;
		}
1332
		blocknr = btrfs_node_blockptr(cur, path->slots[*level]);
C
Chris Mason 已提交
1333
		ret = lookup_extent_ref(trans, root, blocknr, 1, &refs);
1334 1335
		BUG_ON(ret);
		if (refs != 1) {
C
Chris Mason 已提交
1336
			path->slots[*level]++;
1337
			ret = btrfs_free_extent(trans, root, blocknr, 1, 1);
C
Chris Mason 已提交
1338 1339 1340
			BUG_ON(ret);
			continue;
		}
1341
		next = btrfs_find_tree_block(root, blocknr);
1342 1343
		if (!next || !btrfs_buffer_uptodate(next)) {
			free_extent_buffer(next);
1344 1345 1346 1347 1348 1349 1350 1351 1352
			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]++;
1353
				free_extent_buffer(next);
1354 1355 1356 1357 1358 1359
				ret = btrfs_free_extent(trans, root,
							blocknr, 1, 1);
				BUG_ON(ret);
				continue;
			}
		}
1360
		WARN_ON(*level <= 0);
C
Chris Mason 已提交
1361
		if (path->nodes[*level-1])
1362
			free_extent_buffer(path->nodes[*level-1]);
C
Chris Mason 已提交
1363
		path->nodes[*level-1] = next;
1364
		*level = btrfs_header_level(next);
C
Chris Mason 已提交
1365 1366 1367
		path->slots[*level] = 0;
	}
out:
1368 1369
	WARN_ON(*level < 0);
	WARN_ON(*level >= BTRFS_MAX_LEVEL);
1370
	ret = btrfs_free_extent(trans, root,
1371 1372
			extent_buffer_blocknr(path->nodes[*level]), 1, 1);
	free_extent_buffer(path->nodes[*level]);
C
Chris Mason 已提交
1373 1374 1375 1376 1377 1378
	path->nodes[*level] = NULL;
	*level += 1;
	BUG_ON(ret);
	return 0;
}

C
Chris Mason 已提交
1379 1380 1381 1382 1383
/*
 * 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
 */
1384 1385
static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root
			*root, struct btrfs_path *path, int *level)
C
Chris Mason 已提交
1386 1387 1388 1389
{
	int i;
	int slot;
	int ret;
1390 1391
	struct btrfs_root_item *root_item = &root->root_item;

C
Chris Mason 已提交
1392
	for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
C
Chris Mason 已提交
1393
		slot = path->slots[i];
1394 1395 1396 1397
		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 已提交
1398 1399
			path->slots[i]++;
			*level = i;
1400
			WARN_ON(*level == 0);
1401
			btrfs_node_key(node, &disk_key, path->slots[i]);
1402
			memcpy(&root_item->drop_progress,
1403
			       &disk_key, sizeof(disk_key));
1404
			root_item->drop_level = i;
C
Chris Mason 已提交
1405 1406
			return 0;
		} else {
1407
			ret = btrfs_free_extent(trans, root,
1408 1409
				    extent_buffer_blocknr(path->nodes[*level]),
				    1, 1);
1410
			BUG_ON(ret);
1411
			free_extent_buffer(path->nodes[*level]);
C
Chris Mason 已提交
1412
			path->nodes[*level] = NULL;
C
Chris Mason 已提交
1413 1414 1415 1416 1417 1418
			*level = i + 1;
		}
	}
	return 1;
}

C
Chris Mason 已提交
1419 1420 1421 1422 1423
/*
 * 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.
 */
1424
int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
1425
			*root)
C
Chris Mason 已提交
1426
{
1427
	int ret = 0;
C
Chris Mason 已提交
1428
	int wret;
C
Chris Mason 已提交
1429
	int level;
1430
	struct btrfs_path *path;
C
Chris Mason 已提交
1431 1432
	int i;
	int orig_level;
1433
	struct btrfs_root_item *root_item = &root->root_item;
C
Chris Mason 已提交
1434

1435 1436
	path = btrfs_alloc_path();
	BUG_ON(!path);
C
Chris Mason 已提交
1437

1438
	level = btrfs_header_level(root->node);
C
Chris Mason 已提交
1439
	orig_level = level;
1440 1441
	if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
		path->nodes[level] = root->node;
1442
		extent_buffer_get(root->node);
1443 1444 1445
		path->slots[level] = 0;
	} else {
		struct btrfs_key key;
1446 1447
		struct btrfs_disk_key found_key;
		struct extent_buffer *node;
1448

1449
		btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
1450 1451
		level = root_item->drop_level;
		path->lowest_level = level;
1452
		wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1453
		if (wret < 0) {
1454 1455 1456
			ret = wret;
			goto out;
		}
1457 1458 1459 1460
		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)));
1461
	}
C
Chris Mason 已提交
1462
	while(1) {
1463
		wret = walk_down_tree(trans, root, path, &level);
C
Chris Mason 已提交
1464
		if (wret > 0)
C
Chris Mason 已提交
1465
			break;
C
Chris Mason 已提交
1466 1467 1468
		if (wret < 0)
			ret = wret;

1469
		wret = walk_up_tree(trans, root, path, &level);
C
Chris Mason 已提交
1470
		if (wret > 0)
C
Chris Mason 已提交
1471
			break;
C
Chris Mason 已提交
1472 1473
		if (wret < 0)
			ret = wret;
1474 1475
		ret = -EAGAIN;
		break;
C
Chris Mason 已提交
1476
	}
C
Chris Mason 已提交
1477
	for (i = 0; i <= orig_level; i++) {
1478
		if (path->nodes[i]) {
1479
			free_extent_buffer(path->nodes[i]);
1480
			path->nodes[i] = 0;
C
Chris Mason 已提交
1481
		}
C
Chris Mason 已提交
1482
	}
1483
out:
1484
	btrfs_free_path(path);
C
Chris Mason 已提交
1485
	return ret;
C
Chris Mason 已提交
1486
}
C
Chris Mason 已提交
1487

1488
int btrfs_free_block_groups(struct btrfs_fs_info *info)
C
Chris Mason 已提交
1489
{
1490 1491
	u64 start;
	u64 end;
C
Chris Mason 已提交
1492 1493 1494
	int ret;

	while(1) {
1495 1496 1497
		ret = find_first_extent_bit(&info->block_group_cache, 0,
					    &start, &end, (unsigned int)-1);
		if (ret)
C
Chris Mason 已提交
1498
			break;
1499 1500
		clear_extent_bits(&info->block_group_cache, start,
				  end, (unsigned int)-1, GFP_NOFS);
C
Chris Mason 已提交
1501
	}
1502
	while(1) {
1503 1504 1505
		ret = find_first_extent_bit(&info->free_space_cache, 0,
					    &start, &end, EXTENT_DIRTY);
		if (ret)
1506
			break;
1507 1508
		clear_extent_dirty(&info->free_space_cache, start,
				   end, GFP_NOFS);
1509
	}
C
Chris Mason 已提交
1510 1511 1512
	return 0;
}

C
Chris Mason 已提交
1513 1514 1515 1516 1517
int btrfs_read_block_groups(struct btrfs_root *root)
{
	struct btrfs_path *path;
	int ret;
	int err = 0;
1518
	int bit;
C
Chris Mason 已提交
1519
	struct btrfs_block_group_cache *cache;
C
Chris Mason 已提交
1520
	struct btrfs_fs_info *info = root->fs_info;
1521
	struct extent_map_tree *block_group_cache;
C
Chris Mason 已提交
1522 1523
	struct btrfs_key key;
	struct btrfs_key found_key;
1524
	struct extent_buffer *leaf;
C
Chris Mason 已提交
1525
	u64 group_size_blocks;
1526 1527

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

C
Chris Mason 已提交
1529
	group_size_blocks = BTRFS_BLOCK_GROUP_SIZE >>
1530 1531
		info->sb->s_blocksize_bits;

C
Chris Mason 已提交
1532
	root = info->extent_root;
C
Chris Mason 已提交
1533 1534 1535 1536 1537 1538 1539 1540 1541
	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 已提交
1542
		ret = btrfs_search_slot(NULL, info->extent_root,
C
Chris Mason 已提交
1543 1544 1545 1546 1547
					&key, path, 0, 0);
		if (ret != 0) {
			err = ret;
			break;
		}
1548 1549
		leaf = path->nodes[0];
		btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
C
Chris Mason 已提交
1550 1551 1552 1553 1554
		cache = kmalloc(sizeof(*cache), GFP_NOFS);
		if (!cache) {
			err = -1;
			break;
		}
C
Chris Mason 已提交
1555

1556 1557 1558
		read_extent_buffer(leaf, &cache->item,
				   btrfs_item_ptr_offset(leaf, path->slots[0]),
				   sizeof(cache->item));
C
Chris Mason 已提交
1559
		memcpy(&cache->key, &found_key, sizeof(found_key));
1560 1561
		cache->last_alloc = cache->key.objectid;
		cache->first_free = cache->key.objectid;
C
Chris Mason 已提交
1562
		cache->pinned = 0;
1563 1564
		cache->cached = 0;

C
Chris Mason 已提交
1565 1566
		key.objectid = found_key.objectid + found_key.offset;
		btrfs_release_path(root, path);
1567

1568 1569 1570 1571 1572 1573
		if (cache->item.flags & BTRFS_BLOCK_GROUP_DATA) {
			bit = BLOCK_GROUP_DATA;
			cache->data = 1;
		} else {
			bit = BLOCK_GROUP_METADATA;
			cache->data = 0;
1574
		}
1575 1576 1577 1578 1579 1580 1581 1582

		/* 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 已提交
1583
		if (key.objectid >=
1584
		    btrfs_super_total_blocks(&info->super_copy))
C
Chris Mason 已提交
1585 1586 1587 1588 1589 1590
			break;
	}

	btrfs_free_path(path);
	return 0;
}