extent-tree.c 39.6 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
struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
							 btrfs_fs_info *info,
132
							 u64 bytenr)
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
	block_group_cache = &info->block_group_cache;
	ret = find_first_extent_bit(block_group_cache,
143
				    bytenr, &start, &end,
144
				    BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA);
C
Chris Mason 已提交
145
	if (ret) {
146
		return NULL;
C
Chris Mason 已提交
147
	}
148 149 150 151
	ret = get_state_private(block_group_cache, start, &ptr);
	if (ret)
		return NULL;

J
Jens Axboe 已提交
152
	block_group = (struct btrfs_block_group_cache *)(unsigned long)ptr;
153 154


155
	if (block_group->key.objectid <= bytenr && bytenr <=
156 157 158
	    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
{
	int ret;
	struct btrfs_block_group_cache *cache = *cache_ret;
168
	u64 last;
169 170
	u64 start = 0;
	u64 end = 0;
171
	u64 cache_miss = 0;
172
	int wrapped = 0;
173 174

again:
175 176 177
	ret = cache_block_group(root, cache);
	if (ret)
		goto out;
178

179 180
	last = max(search_start, cache->key.objectid);

181
	while(1) {
182 183
		ret = find_first_extent_bit(&root->fs_info->free_space_cache,
					    last, &start, &end, EXTENT_DIRTY);
184
		if (ret) {
185 186
			if (!cache_miss)
				cache_miss = last;
187 188
			goto new_group;
		}
189 190 191

		start = max(last, start);
		last = end + 1;
192 193 194
		if (last - start < num) {
			if (last == cache->key.objectid + cache->key.offset)
				cache_miss = start;
195
			continue;
196 197 198
		}
		if (data != BTRFS_BLOCK_GROUP_MIXED &&
		    start + num >= cache->key.objectid + cache->key.offset)
199
			goto new_group;
200
		return start;
201 202
	}
out:
203
	return search_start;
204 205

new_group:
206
	last = cache->key.objectid + cache->key.offset;
207
wrapped:
208
	cache = btrfs_lookup_block_group(root->fs_info, last);
209
	if (!cache) {
210 211 212 213 214 215
		if (!wrapped) {
			wrapped = 1;
			last = search_start;
			data = BTRFS_BLOCK_GROUP_MIXED;
			goto wrapped;
		}
216
		return search_start;
217
	}
218 219 220 221 222 223
	if (cache_miss && !cache->cached) {
		cache_block_group(root, cache);
		last = cache_miss;

		cache = btrfs_lookup_block_group(root->fs_info, last);
	}
224
	cache = btrfs_find_block_group(root, cache, last, data, 0);
225
	*cache_ret = cache;
226
	cache_miss = 0;
227 228 229
	goto again;
}

C
Chris Mason 已提交
230 231
static u64 div_factor(u64 num, int factor)
{
232 233
	if (factor == 10)
		return num;
C
Chris Mason 已提交
234 235 236 237 238
	num *= factor;
	do_div(num, 10);
	return num;
}

239 240
struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
						 struct btrfs_block_group_cache
C
Chris Mason 已提交
241
						 *hint, u64 search_start,
242
						 int data, int owner)
C
Chris Mason 已提交
243
{
244 245
	struct btrfs_block_group_cache *cache;
	struct extent_map_tree *block_group_cache;
246
	struct btrfs_block_group_cache *found_group = NULL;
C
Chris Mason 已提交
247 248
	struct btrfs_fs_info *info = root->fs_info;
	u64 used;
249 250
	u64 last = 0;
	u64 hint_last;
251 252 253 254 255
	u64 start;
	u64 end;
	u64 free_check;
	u64 ptr;
	int bit;
C
Chris Mason 已提交
256
	int ret;
257
	int full_search = 0;
258
	int factor = 8;
C
Chris Mason 已提交
259
	int data_swap = 0;
260

261 262
	block_group_cache = &info->block_group_cache;

263
	if (!owner)
264
		factor = 8;
C
Chris Mason 已提交
265

266
	if (data == BTRFS_BLOCK_GROUP_MIXED) {
267
		bit = BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA;
268 269
		factor = 10;
	} else if (data)
270 271 272
		bit = BLOCK_GROUP_DATA;
	else
		bit = BLOCK_GROUP_METADATA;
C
Chris Mason 已提交
273 274 275

	if (search_start) {
		struct btrfs_block_group_cache *shint;
276
		shint = btrfs_lookup_block_group(info, search_start);
277 278
		if (shint && (shint->data == data ||
			      shint->data == BTRFS_BLOCK_GROUP_MIXED)) {
C
Chris Mason 已提交
279
			used = btrfs_block_group_used(&shint->item);
280
			if (used < div_factor(shint->key.offset, factor)) {
C
Chris Mason 已提交
281 282 283 284
				return shint;
			}
		}
	}
285 286
	if (hint && (hint->data == data ||
		     hint->data == BTRFS_BLOCK_GROUP_MIXED)) {
287
		used = btrfs_block_group_used(&hint->item);
288
		if (used < div_factor(hint->key.offset, factor)) {
289 290
			return hint;
		}
291
		last = hint->key.objectid + hint->key.offset;
292 293
		hint_last = last;
	} else {
294 295 296 297 298 299
		if (hint)
			hint_last = max(hint->key.objectid, search_start);
		else
			hint_last = search_start;

		last = hint_last;
300 301
	}
again:
C
Chris Mason 已提交
302
	while(1) {
303 304 305
		ret = find_first_extent_bit(block_group_cache, last,
					    &start, &end, bit);
		if (ret)
C
Chris Mason 已提交
306
			break;
307 308 309 310 311

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

J
Jens Axboe 已提交
312
		cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
313 314 315 316 317 318 319 320
		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);

321
		if (used < free_check) {
322 323
			found_group = cache;
			goto found;
C
Chris Mason 已提交
324
		}
325
		cond_resched();
C
Chris Mason 已提交
326
	}
327
	if (!full_search) {
C
Chris Mason 已提交
328
		last = search_start;
329 330 331
		full_search = 1;
		goto again;
	}
C
Chris Mason 已提交
332 333
	if (!data_swap) {
		data_swap = 1;
334
		bit = BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA;
C
Chris Mason 已提交
335 336 337
		last = search_start;
		goto again;
	}
C
Chris Mason 已提交
338
found:
339
	return found_group;
C
Chris Mason 已提交
340 341
}

C
Chris Mason 已提交
342 343
int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
				struct btrfs_root *root,
344
				u64 bytenr, u64 num_bytes)
C
Chris Mason 已提交
345
{
346
	struct btrfs_path *path;
C
Chris Mason 已提交
347
	int ret;
C
Chris Mason 已提交
348
	struct btrfs_key key;
349
	struct extent_buffer *l;
C
Chris Mason 已提交
350
	struct btrfs_extent_item *item;
C
Chris Mason 已提交
351
	u32 refs;
C
Chris Mason 已提交
352

353
	WARN_ON(num_bytes < root->sectorsize);
354
	path = btrfs_alloc_path();
355 356
	if (!path)
		return -ENOMEM;
357

358
	key.objectid = bytenr;
359
	btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
360
	key.offset = num_bytes;
361
	ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
362
				0, 1);
363 364
	if (ret < 0)
		return ret;
365
	if (ret != 0) {
366
		BUG();
367
	}
C
Chris Mason 已提交
368
	BUG_ON(ret != 0);
369
	l = path->nodes[0];
370
	item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
371 372
	refs = btrfs_extent_refs(l, item);
	btrfs_set_extent_refs(l, item, refs + 1);
373
	btrfs_mark_buffer_dirty(path->nodes[0]);
374

375 376
	btrfs_release_path(root->fs_info->extent_root, path);
	btrfs_free_path(path);
377
	finish_current_insert(trans, root->fs_info->extent_root);
C
Chris Mason 已提交
378
	del_pending_extents(trans, root->fs_info->extent_root);
C
Chris Mason 已提交
379 380 381
	return 0;
}

382 383 384 385 386 387 388 389
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 已提交
390
static int lookup_extent_ref(struct btrfs_trans_handle *trans,
391 392
			     struct btrfs_root *root, u64 bytenr,
			     u64 num_bytes, u32 *refs)
393
{
394
	struct btrfs_path *path;
395
	int ret;
C
Chris Mason 已提交
396
	struct btrfs_key key;
397
	struct extent_buffer *l;
C
Chris Mason 已提交
398
	struct btrfs_extent_item *item;
399

400
	WARN_ON(num_bytes < root->sectorsize);
401
	path = btrfs_alloc_path();
402 403
	key.objectid = bytenr;
	key.offset = num_bytes;
404
	btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
405
	ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
406
				0, 0);
407 408
	if (ret < 0)
		goto out;
409 410
	if (ret != 0) {
		btrfs_print_leaf(root, path->nodes[0]);
411
		printk("failed to find block number %Lu\n", bytenr);
412
		BUG();
413 414
	}
	l = path->nodes[0];
415
	item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
416
	*refs = btrfs_extent_refs(l, item);
417
out:
418
	btrfs_free_path(path);
419 420 421
	return 0;
}

C
Chris Mason 已提交
422 423 424
int btrfs_inc_root_ref(struct btrfs_trans_handle *trans,
		       struct btrfs_root *root)
{
425 426
	return btrfs_inc_extent_ref(trans, root, root->node->start,
				    root->node->len);
C
Chris Mason 已提交
427 428
}

429
int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
430
		  struct extent_buffer *buf)
C
Chris Mason 已提交
431
{
432
	u64 bytenr;
433 434
	u32 nritems;
	struct btrfs_key key;
435
	struct btrfs_file_extent_item *fi;
C
Chris Mason 已提交
436
	int i;
437
	int level;
438
	int ret;
439 440
	int faili;
	int err;
441

442
	if (!root->ref_cows)
443
		return 0;
444

445
	level = btrfs_header_level(buf);
446 447
	nritems = btrfs_header_nritems(buf);
	for (i = 0; i < nritems; i++) {
448 449
		if (level == 0) {
			u64 disk_bytenr;
450 451
			btrfs_item_key_to_cpu(buf, &key, i);
			if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
452
				continue;
453
			fi = btrfs_item_ptr(buf, i,
454
					    struct btrfs_file_extent_item);
455
			if (btrfs_file_extent_type(buf, fi) ==
456 457
			    BTRFS_FILE_EXTENT_INLINE)
				continue;
458 459
			disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
			if (disk_bytenr == 0)
C
Chris Mason 已提交
460
				continue;
461 462
			ret = btrfs_inc_extent_ref(trans, root, disk_bytenr,
				    btrfs_file_extent_disk_num_bytes(buf, fi));
463 464 465 466
			if (ret) {
				faili = i;
				goto fail;
			}
467
		} else {
468 469 470
			bytenr = btrfs_node_blockptr(buf, i);
			ret = btrfs_inc_extent_ref(trans, root, bytenr,
					   btrfs_level_size(root, level - 1));
471 472 473 474
			if (ret) {
				faili = i;
				goto fail;
			}
475
		}
C
Chris Mason 已提交
476 477
	}
	return 0;
478
fail:
C
Chris Mason 已提交
479
	WARN_ON(1);
480
	for (i =0; i < faili; i++) {
481 482
		if (level == 0) {
			u64 disk_bytenr;
483 484
			btrfs_item_key_to_cpu(buf, &key, i);
			if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
485
				continue;
486
			fi = btrfs_item_ptr(buf, i,
487
					    struct btrfs_file_extent_item);
488
			if (btrfs_file_extent_type(buf, fi) ==
489 490
			    BTRFS_FILE_EXTENT_INLINE)
				continue;
491 492
			disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
			if (disk_bytenr == 0)
493
				continue;
494 495
			err = btrfs_free_extent(trans, root, disk_bytenr,
				    btrfs_file_extent_disk_num_bytes(buf,
496
								      fi), 0);
497 498
			BUG_ON(err);
		} else {
499 500 501
			bytenr = btrfs_node_blockptr(buf, i);
			err = btrfs_free_extent(trans, root, bytenr,
					btrfs_level_size(root, level - 1), 0);
502 503 504 505
			BUG_ON(err);
		}
	}
	return ret;
C
Chris Mason 已提交
506 507
}

C
Chris Mason 已提交
508 509 510 511 512 513 514 515
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;
516 517
	unsigned long bi;
	struct extent_buffer *leaf;
C
Chris Mason 已提交
518 519

	ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
520 521
	if (ret < 0)
		goto fail;
C
Chris Mason 已提交
522
	BUG_ON(ret);
523 524 525 526 527

	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 已提交
528
	btrfs_release_path(extent_root, path);
529
fail:
C
Chris Mason 已提交
530 531 532 533 534 535 536 537 538 539
	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;

}

540 541
int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
				   struct btrfs_root *root)
C
Chris Mason 已提交
542
{
543 544
	struct extent_map_tree *block_group_cache;
	struct btrfs_block_group_cache *cache;
C
Chris Mason 已提交
545 546 547 548
	int ret;
	int err = 0;
	int werr = 0;
	struct btrfs_path *path;
549 550 551 552
	u64 last = 0;
	u64 start;
	u64 end;
	u64 ptr;
C
Chris Mason 已提交
553

554
	block_group_cache = &root->fs_info->block_group_cache;
C
Chris Mason 已提交
555 556 557 558 559
	path = btrfs_alloc_path();
	if (!path)
		return -ENOMEM;

	while(1) {
560 561 562
		ret = find_first_extent_bit(block_group_cache, last,
					    &start, &end, BLOCK_GROUP_DIRTY);
		if (ret)
C
Chris Mason 已提交
563
			break;
564

565 566 567 568 569
		last = end + 1;
		ret = get_state_private(block_group_cache, start, &ptr);
		if (ret)
			break;

J
Jens Axboe 已提交
570
		cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
571 572 573 574 575 576 577 578 579 580
		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 已提交
581
		}
582 583
		clear_extent_bits(block_group_cache, start, end,
				  BLOCK_GROUP_DIRTY, GFP_NOFS);
C
Chris Mason 已提交
584 585 586 587 588 589 590
	}
	btrfs_free_path(path);
	return werr;
}

static int update_block_group(struct btrfs_trans_handle *trans,
			      struct btrfs_root *root,
591 592
			      u64 bytenr, u64 num_bytes, int alloc,
			      int mark_free, int data)
C
Chris Mason 已提交
593 594 595
{
	struct btrfs_block_group_cache *cache;
	struct btrfs_fs_info *info = root->fs_info;
596
	u64 total = num_bytes;
C
Chris Mason 已提交
597
	u64 old_val;
598
	u64 byte_in_group;
599 600
	u64 start;
	u64 end;
C
Chris Mason 已提交
601

C
Chris Mason 已提交
602
	while(total) {
603
		cache = btrfs_lookup_block_group(info, bytenr);
C
Chris Mason 已提交
604
		if (!cache) {
C
Chris Mason 已提交
605
			return -1;
C
Chris Mason 已提交
606
		}
607 608
		byte_in_group = bytenr - cache->key.objectid;
		WARN_ON(byte_in_group > cache->key.offset);
609 610 611 612
		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 已提交
613 614

		old_val = btrfs_block_group_used(&cache->item);
615
		num_bytes = min(total, cache->key.offset - byte_in_group);
C
Chris Mason 已提交
616
		if (alloc) {
C
Chris Mason 已提交
617
			if (cache->data != data &&
C
Chris Mason 已提交
618
			    old_val < (cache->key.offset >> 1)) {
619 620 621
				int bit_to_clear;
				int bit_to_set;
				cache->data = data;
C
Chris Mason 已提交
622
				if (data) {
623 624
					bit_to_clear = BLOCK_GROUP_METADATA;
					bit_to_set = BLOCK_GROUP_DATA;
625 626
					cache->item.flags &=
						~BTRFS_BLOCK_GROUP_MIXED;
C
Chris Mason 已提交
627 628 629
					cache->item.flags |=
						BTRFS_BLOCK_GROUP_DATA;
				} else {
630 631
					bit_to_clear = BLOCK_GROUP_DATA;
					bit_to_set = BLOCK_GROUP_METADATA;
632 633
					cache->item.flags &=
						~BTRFS_BLOCK_GROUP_MIXED;
C
Chris Mason 已提交
634 635 636
					cache->item.flags &=
						~BTRFS_BLOCK_GROUP_DATA;
				}
637 638 639 640 641 642
				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);
643 644 645 646 647 648 649 650
			} else if (cache->data != data &&
				   cache->data != BTRFS_BLOCK_GROUP_MIXED) {
				cache->data = BTRFS_BLOCK_GROUP_MIXED;
				set_extent_bits(&info->block_group_cache,
						start, end,
						BLOCK_GROUP_DATA |
						BLOCK_GROUP_METADATA,
						GFP_NOFS);
C
Chris Mason 已提交
651
			}
652
			old_val += num_bytes;
C
Chris Mason 已提交
653
		} else {
654
			old_val -= num_bytes;
655 656
			if (mark_free) {
				set_extent_dirty(&info->free_space_cache,
657
						 bytenr, bytenr + num_bytes - 1,
658
						 GFP_NOFS);
659
			}
C
Chris Mason 已提交
660
		}
C
Chris Mason 已提交
661
		btrfs_set_block_group_used(&cache->item, old_val);
662 663
		total -= num_bytes;
		bytenr += num_bytes;
C
Chris Mason 已提交
664 665 666 667
	}
	return 0;
}

668
int btrfs_copy_pinned(struct btrfs_root *root, struct extent_map_tree *copy)
C
Chris Mason 已提交
669 670
{
	u64 last = 0;
671 672 673
	u64 start;
	u64 end;
	struct extent_map_tree *pinned_extents = &root->fs_info->pinned_extents;
C
Chris Mason 已提交
674 675 676
	int ret;

	while(1) {
677 678 679
		ret = find_first_extent_bit(pinned_extents, last,
					    &start, &end, EXTENT_DIRTY);
		if (ret)
C
Chris Mason 已提交
680
			break;
681 682
		set_extent_dirty(copy, start, end, GFP_NOFS);
		last = end + 1;
C
Chris Mason 已提交
683 684 685 686 687 688
	}
	return 0;
}

int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
			       struct btrfs_root *root,
689
			       struct extent_map_tree *unpin)
690
{
691 692
	u64 start;
	u64 end;
693
	int ret;
694
	struct extent_map_tree *pinned_extents = &root->fs_info->pinned_extents;
695 696 697
	struct extent_map_tree *free_space_cache;

	free_space_cache = &root->fs_info->free_space_cache;
698 699

	while(1) {
700 701 702
		ret = find_first_extent_bit(unpin, 0, &start, &end,
					    EXTENT_DIRTY);
		if (ret)
703
			break;
704 705 706 707 708

		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);
709 710 711 712
	}
	return 0;
}

713 714
static int finish_current_insert(struct btrfs_trans_handle *trans, struct
				 btrfs_root *extent_root)
C
Chris Mason 已提交
715
{
C
Chris Mason 已提交
716
	struct btrfs_key ins;
C
Chris Mason 已提交
717
	struct btrfs_extent_item extent_item;
C
Chris Mason 已提交
718
	int ret;
719 720 721
	int err = 0;
	u64 start;
	u64 end;
722
	struct btrfs_fs_info *info = extent_root->fs_info;
C
Chris Mason 已提交
723

724
	btrfs_set_stack_extent_refs(&extent_item, 1);
725
	btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
726 727
	btrfs_set_stack_extent_owner(&extent_item,
				     extent_root->root_key.objectid);
C
Chris Mason 已提交
728

729
	while(1) {
730 731 732
		ret = find_first_extent_bit(&info->extent_ins, 0, &start,
					    &end, EXTENT_LOCKED);
		if (ret)
733 734
			break;

735 736 737 738 739 740
		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 已提交
741 742 743 744
	}
	return 0;
}

745 746
static int pin_down_bytes(struct btrfs_root *root, u64 bytenr, u32 num_bytes,
			  int pending)
C
Chris Mason 已提交
747
{
748
	int err = 0;
749
	struct extent_buffer *buf;
C
Chris Mason 已提交
750

C
Chris Mason 已提交
751
	if (!pending) {
752
		buf = btrfs_find_tree_block(root, bytenr, num_bytes);
753 754
		if (buf) {
			if (btrfs_buffer_uptodate(buf)) {
C
Chris Mason 已提交
755 756
				u64 transid =
				    root->fs_info->running_transaction->transid;
757 758
				if (btrfs_header_generation(buf) == transid) {
					free_extent_buffer(buf);
759
					return 1;
C
Chris Mason 已提交
760
				}
C
Chris Mason 已提交
761
			}
762
			free_extent_buffer(buf);
C
Chris Mason 已提交
763
		}
764
		set_extent_dirty(&root->fs_info->pinned_extents,
765
				 bytenr, bytenr + num_bytes - 1, GFP_NOFS);
C
Chris Mason 已提交
766
	} else {
767
		set_extent_bits(&root->fs_info->pending_del,
768 769
				bytenr, bytenr + num_bytes - 1,
				EXTENT_LOCKED, GFP_NOFS);
C
Chris Mason 已提交
770
	}
C
Chris Mason 已提交
771
	BUG_ON(err < 0);
C
Chris Mason 已提交
772 773 774
	return 0;
}

775
/*
776
 * remove an extent from the root, returns 0 on success
777
 */
778
static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
779
			 *root, u64 bytenr, u64 num_bytes, int pin,
780
			 int mark_free)
781
{
782
	struct btrfs_path *path;
C
Chris Mason 已提交
783
	struct btrfs_key key;
784 785
	struct btrfs_fs_info *info = root->fs_info;
	struct btrfs_root *extent_root = info->extent_root;
786
	struct extent_buffer *leaf;
787
	int ret;
C
Chris Mason 已提交
788
	struct btrfs_extent_item *ei;
C
Chris Mason 已提交
789
	u32 refs;
C
Chris Mason 已提交
790

791
	key.objectid = bytenr;
792
	btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
793
	key.offset = num_bytes;
794

795
	path = btrfs_alloc_path();
796 797
	if (!path)
		return -ENOMEM;
798

799 800 801 802
	ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
	if (ret < 0)
		return ret;
	BUG_ON(ret);
803 804 805

	leaf = path->nodes[0];
	ei = btrfs_item_ptr(leaf, path->slots[0],
C
Chris Mason 已提交
806
			    struct btrfs_extent_item);
807 808 809 810 811 812
	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 已提交
813
	if (refs == 0) {
814 815
		u64 super_used;
		u64 root_used;
C
Chris Mason 已提交
816 817

		if (pin) {
818
			ret = pin_down_bytes(root, bytenr, num_bytes, 0);
819 820 821
			if (ret > 0)
				mark_free = 1;
			BUG_ON(ret < 0);
C
Chris Mason 已提交
822 823
		}

824
		/* block accounting for super block */
825 826 827
		super_used = btrfs_super_bytes_used(&info->super_copy);
		btrfs_set_super_bytes_used(&info->super_copy,
					   super_used - num_bytes);
828 829

		/* block accounting for root item */
830
		root_used = btrfs_root_used(&root->root_item);
831
		btrfs_set_root_used(&root->root_item,
832
					   root_used - num_bytes);
833

834
		ret = btrfs_del_item(trans, extent_root, path);
835 836 837
		if (ret) {
			return ret;
		}
838
		ret = update_block_group(trans, root, bytenr, num_bytes, 0,
C
Chris Mason 已提交
839
					 mark_free, 0);
C
Chris Mason 已提交
840
		BUG_ON(ret);
841
	}
842
	btrfs_free_path(path);
843
	finish_current_insert(trans, extent_root);
844 845 846 847 848 849 850
	return ret;
}

/*
 * find all the blocks marked as pending in the radix tree and remove
 * them from the extent map
 */
851 852
static int del_pending_extents(struct btrfs_trans_handle *trans, struct
			       btrfs_root *extent_root)
853 854
{
	int ret;
C
Chris Mason 已提交
855
	int err = 0;
856 857 858 859
	u64 start;
	u64 end;
	struct extent_map_tree *pending_del;
	struct extent_map_tree *pinned_extents;
C
Chris Mason 已提交
860

861 862
	pending_del = &extent_root->fs_info->pending_del;
	pinned_extents = &extent_root->fs_info->pinned_extents;
863 864

	while(1) {
865 866 867
		ret = find_first_extent_bit(pending_del, 0, &start, &end,
					    EXTENT_LOCKED);
		if (ret)
868
			break;
869 870 871 872 873 874 875 876

		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;
877
	}
C
Chris Mason 已提交
878
	return err;
879 880 881 882 883
}

/*
 * remove an extent from the root, returns 0 on success
 */
884
int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
885
		      *root, u64 bytenr, u64 num_bytes, int pin)
886
{
887
	struct btrfs_root *extent_root = root->fs_info->extent_root;
888 889
	int pending_ret;
	int ret;
890

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

933
	WARN_ON(num_bytes < root->sectorsize);
934 935
	btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);

936 937
	level = btrfs_header_level(root->node);

938 939 940 941
	if (num_bytes >= 96 * 1024 * 1024 && hint_byte) {
		data = BTRFS_BLOCK_GROUP_MIXED;
	}

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

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

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

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

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

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

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

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

997
	while (1) {
998
		l = path->nodes[0];
999
		slot = path->slots[0];
1000
		if (slot >= btrfs_header_nritems(l)) {
1001
			ret = btrfs_next_leaf(root, path);
1002 1003
			if (ret == 0)
				continue;
C
Chris Mason 已提交
1004 1005
			if (ret < 0)
				goto error;
1006 1007 1008

			search_start = max(search_start,
					   block_group->key.objectid);
1009 1010
			if (!start_found) {
				ins->objectid = search_start;
C
Chris Mason 已提交
1011
				ins->offset = search_end - search_start;
1012 1013 1014
				start_found = 1;
				goto check_pending;
			}
1015 1016
			ins->objectid = last_byte > search_start ?
					last_byte : search_start;
C
Chris Mason 已提交
1017
			ins->offset = search_end - ins->objectid;
1018
			BUG_ON(ins->objectid >= search_end);
1019 1020
			goto check_pending;
		}
1021
		btrfs_item_key_to_cpu(l, &key, slot);
1022

1023
		if (key.objectid >= search_start && key.objectid > last_byte &&
1024
		    start_found) {
1025 1026 1027 1028 1029
			if (last_byte < search_start)
				last_byte = search_start;
			hole_size = key.objectid - last_byte;
			if (hole_size >= num_bytes) {
				ins->objectid = last_byte;
1030 1031
				ins->offset = hole_size;
				goto check_pending;
1032
			}
1033
		}
1034 1035
		if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY) {
			if (!start_found) {
1036
				last_byte = key.objectid;
1037 1038
				start_found = 1;
			}
1039
			goto next;
1040 1041
		}

1042

1043
		start_found = 1;
1044
		last_byte = key.objectid + key.offset;
1045

1046 1047
		if (!full_scan && data != BTRFS_BLOCK_GROUP_MIXED &&
		    last_byte >= block_group->key.objectid +
C
Chris Mason 已提交
1048 1049 1050
		    block_group->key.offset) {
			btrfs_release_path(root, path);
			search_start = block_group->key.objectid +
1051
				block_group->key.offset;
C
Chris Mason 已提交
1052 1053
			goto new_group;
		}
C
Chris Mason 已提交
1054
next:
1055
		path->slots[0]++;
1056
		cond_resched();
1057 1058 1059 1060 1061
	}
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
	 */
1062
	btrfs_release_path(root, path);
1063
	BUG_ON(ins->objectid < search_start);
1064

1065
	if (ins->objectid + num_bytes >= search_end)
1066 1067
		goto enospc;

1068 1069
	if (!full_scan && data != BTRFS_BLOCK_GROUP_MIXED &&
	    ins->objectid + num_bytes >= block_group->
1070 1071 1072 1073 1074
	    key.objectid + block_group->key.offset) {
		search_start = block_group->key.objectid +
			block_group->key.offset;
		goto new_group;
	}
1075
	if (test_range_bit(&info->extent_ins, ins->objectid,
1076 1077
			   ins->objectid + num_bytes -1, EXTENT_LOCKED, 0)) {
		search_start = ins->objectid + num_bytes;
1078 1079 1080
		goto new_group;
	}
	if (test_range_bit(&info->pinned_extents, ins->objectid,
1081 1082
			   ins->objectid + num_bytes -1, EXTENT_DIRTY, 0)) {
		search_start = ins->objectid + num_bytes;
1083
		goto new_group;
1084
	}
1085
	if (exclude_nr > 0 && (ins->objectid + num_bytes > exclude_start &&
1086 1087 1088 1089
	    ins->objectid < exclude_start + exclude_nr)) {
		search_start = exclude_start + exclude_nr;
		goto new_group;
	}
1090
	if (!data) {
1091
		block_group = btrfs_lookup_block_group(info, ins->objectid);
1092 1093
		if (block_group)
			trans->block_group = block_group;
1094
	}
1095
	ins->offset = num_bytes;
1096
	btrfs_free_path(path);
1097
	return 0;
C
Chris Mason 已提交
1098 1099

new_group:
1100
	if (search_start + num_bytes >= search_end) {
1101
enospc:
C
Chris Mason 已提交
1102
		search_start = orig_search_start;
1103 1104 1105 1106
		if (full_scan) {
			ret = -ENOSPC;
			goto error;
		}
1107 1108 1109
		if (wrapped) {
			if (!full_scan)
				total_needed -= empty_size;
1110
			full_scan = 1;
1111
		} else
1112
			wrapped = 1;
C
Chris Mason 已提交
1113
	}
1114
	block_group = btrfs_lookup_block_group(info, search_start);
1115
	cond_resched();
C
Chris Mason 已提交
1116 1117
	if (!full_scan)
		block_group = btrfs_find_block_group(root, block_group,
1118
						     search_start, data, 0);
C
Chris Mason 已提交
1119 1120
	goto check_failed;

C
Chris Mason 已提交
1121
error:
1122 1123
	btrfs_release_path(root, path);
	btrfs_free_path(path);
C
Chris Mason 已提交
1124
	return ret;
1125 1126 1127 1128 1129 1130 1131 1132
}
/*
 * 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.
 */
1133 1134
int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
		       struct btrfs_root *root, u64 owner,
1135
		       u64 num_bytes, u64 empty_size, u64 hint_byte,
C
Chris Mason 已提交
1136
		       u64 search_end, struct btrfs_key *ins, int data)
1137 1138 1139
{
	int ret;
	int pending_ret;
1140
	u64 super_used, root_used;
1141
	u64 search_start = 0;
1142 1143
	struct btrfs_fs_info *info = root->fs_info;
	struct btrfs_root *extent_root = info->extent_root;
C
Chris Mason 已提交
1144
	struct btrfs_extent_item extent_item;
C
Chris Mason 已提交
1145

1146 1147
	btrfs_set_stack_extent_refs(&extent_item, 1);
	btrfs_set_stack_extent_owner(&extent_item, owner);
1148

1149 1150 1151
	WARN_ON(num_bytes < root->sectorsize);
	ret = find_free_extent(trans, root, num_bytes, empty_size,
			       search_start, search_end, hint_byte, ins,
1152 1153
			       trans->alloc_exclude_start,
			       trans->alloc_exclude_nr, data);
C
Chris Mason 已提交
1154
	BUG_ON(ret);
1155 1156
	if (ret)
		return ret;
1157

1158
	/* block accounting for super block */
1159 1160
	super_used = btrfs_super_bytes_used(&info->super_copy);
	btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes);
1161

1162
	/* block accounting for root item */
1163 1164
	root_used = btrfs_root_used(&root->root_item);
	btrfs_set_root_used(&root->root_item, root_used + num_bytes);
1165

1166 1167 1168 1169
	clear_extent_dirty(&root->fs_info->free_space_cache,
			   ins->objectid, ins->objectid + ins->offset - 1,
			   GFP_NOFS);

1170
	if (root == extent_root) {
1171 1172 1173
		set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
				ins->objectid + ins->offset - 1,
				EXTENT_LOCKED, GFP_NOFS);
1174
		WARN_ON(data == 1);
1175 1176 1177 1178 1179 1180
		goto update_block;
	}

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

1184 1185 1186
	trans->alloc_exclude_start = 0;
	trans->alloc_exclude_nr = 0;

C
Chris Mason 已提交
1187
	BUG_ON(ret);
1188
	finish_current_insert(trans, extent_root);
C
Chris Mason 已提交
1189
	pending_ret = del_pending_extents(trans, extent_root);
1190

1191
	if (ret) {
C
Chris Mason 已提交
1192
		return ret;
1193 1194
	}
	if (pending_ret) {
C
Chris Mason 已提交
1195
		return pending_ret;
1196
	}
1197 1198

update_block:
C
Chris Mason 已提交
1199 1200
	ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0,
				 data);
C
Chris Mason 已提交
1201
	BUG_ON(ret);
C
Chris Mason 已提交
1202
	return 0;
1203 1204 1205 1206 1207 1208
}

/*
 * helper function to allocate a block for a given tree
 * returns the tree buffer or NULL.
 */
1209
struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
1210 1211
					     struct btrfs_root *root,
					     u32 blocksize, u64 hint,
1212
					     u64 empty_size)
1213
{
C
Chris Mason 已提交
1214
	struct btrfs_key ins;
1215
	int ret;
1216
	struct extent_buffer *buf;
1217

1218
	ret = btrfs_alloc_extent(trans, root, root->root_key.objectid,
1219 1220
				 blocksize, empty_size, hint,
				 (u64)-1, &ins, 0);
1221
	if (ret) {
1222 1223
		BUG_ON(ret > 0);
		return ERR_PTR(ret);
1224
	}
1225
	buf = btrfs_find_create_tree_block(root, ins.objectid, blocksize);
1226
	if (!buf) {
1227
		btrfs_free_extent(trans, root, ins.objectid, blocksize, 0);
1228 1229
		return ERR_PTR(-ENOMEM);
	}
1230 1231 1232
	btrfs_set_buffer_uptodate(buf);
	set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
			 buf->start + buf->len - 1, GFP_NOFS);
1233 1234 1235 1236
	set_extent_bits(&BTRFS_I(root->fs_info->btree_inode)->extent_tree,
			buf->start, buf->start + buf->len - 1,
			EXTENT_CSUM, GFP_NOFS);
	buf->flags |= EXTENT_CSUM;
1237
	btrfs_set_buffer_defrag(buf);
1238
	trans->blocks_used++;
1239 1240
	return buf;
}
1241

1242
static int drop_leaf_ref(struct btrfs_trans_handle *trans,
1243
			 struct btrfs_root *root, struct extent_buffer *leaf)
1244
{
1245
	struct btrfs_key key;
1246 1247 1248 1249 1250
	struct btrfs_file_extent_item *fi;
	int i;
	int nritems;
	int ret;

1251 1252
	BUG_ON(!btrfs_is_leaf(leaf));
	nritems = btrfs_header_nritems(leaf);
1253
	for (i = 0; i < nritems; i++) {
1254
		u64 disk_bytenr;
1255 1256 1257

		btrfs_item_key_to_cpu(leaf, &key, i);
		if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1258 1259
			continue;
		fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
1260 1261
		if (btrfs_file_extent_type(leaf, fi) ==
		    BTRFS_FILE_EXTENT_INLINE)
1262
			continue;
1263 1264 1265 1266
		/*
		 * FIXME make sure to insert a trans record that
		 * repeats the snapshot del on crash
		 */
1267 1268
		disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
		if (disk_bytenr == 0)
C
Chris Mason 已提交
1269
			continue;
1270 1271
		ret = btrfs_free_extent(trans, root, disk_bytenr,
				btrfs_file_extent_disk_num_bytes(leaf, fi), 0);
1272 1273 1274 1275 1276
		BUG_ON(ret);
	}
	return 0;
}

1277
static void reada_walk_down(struct btrfs_root *root,
1278
			    struct extent_buffer *node)
1279 1280 1281
{
	int i;
	u32 nritems;
1282
	u64 bytenr;
1283 1284
	int ret;
	u32 refs;
1285 1286
	int level;
	u32 blocksize;
1287

1288
	nritems = btrfs_header_nritems(node);
1289
	level = btrfs_header_level(node);
1290
	for (i = 0; i < nritems; i++) {
1291 1292 1293
		bytenr = btrfs_node_blockptr(node, i);
		blocksize = btrfs_level_size(root, level - 1);
		ret = lookup_extent_ref(NULL, root, bytenr, blocksize, &refs);
1294 1295 1296
		BUG_ON(ret);
		if (refs != 1)
			continue;
1297
		mutex_unlock(&root->fs_info->fs_mutex);
1298
		ret = readahead_tree_block(root, bytenr, blocksize);
1299 1300
		cond_resched();
		mutex_lock(&root->fs_info->fs_mutex);
1301 1302 1303 1304 1305
		if (ret)
			break;
	}
}

C
Chris Mason 已提交
1306 1307 1308 1309
/*
 * helper function for drop_snapshot, this walks down the tree dropping ref
 * counts as it goes.
 */
1310 1311
static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
			  *root, struct btrfs_path *path, int *level)
C
Chris Mason 已提交
1312
{
1313 1314
	struct extent_buffer *next;
	struct extent_buffer *cur;
1315 1316
	u64 bytenr;
	u32 blocksize;
C
Chris Mason 已提交
1317 1318 1319
	int ret;
	u32 refs;

1320 1321
	WARN_ON(*level < 0);
	WARN_ON(*level >= BTRFS_MAX_LEVEL);
1322
	ret = lookup_extent_ref(trans, root,
1323 1324
				path->nodes[*level]->start,
				path->nodes[*level]->len, &refs);
C
Chris Mason 已提交
1325 1326 1327
	BUG_ON(ret);
	if (refs > 1)
		goto out;
1328

C
Chris Mason 已提交
1329 1330 1331
	/*
	 * walk down to the last node level and free all the leaves
	 */
1332
	while(*level >= 0) {
1333 1334
		WARN_ON(*level < 0);
		WARN_ON(*level >= BTRFS_MAX_LEVEL);
C
Chris Mason 已提交
1335
		cur = path->nodes[*level];
1336 1337

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

1340
		if (btrfs_header_level(cur) != *level)
C
Chris Mason 已提交
1341
			WARN_ON(1);
1342

1343
		if (path->slots[*level] >=
1344
		    btrfs_header_nritems(cur))
C
Chris Mason 已提交
1345
			break;
1346 1347 1348 1349 1350
		if (*level == 0) {
			ret = drop_leaf_ref(trans, root, cur);
			BUG_ON(ret);
			break;
		}
1351 1352 1353
		bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
		blocksize = btrfs_level_size(root, *level - 1);
		ret = lookup_extent_ref(trans, root, bytenr, blocksize, &refs);
1354 1355
		BUG_ON(ret);
		if (refs != 1) {
C
Chris Mason 已提交
1356
			path->slots[*level]++;
1357 1358
			ret = btrfs_free_extent(trans, root, bytenr,
						blocksize, 1);
C
Chris Mason 已提交
1359 1360 1361
			BUG_ON(ret);
			continue;
		}
1362
		next = btrfs_find_tree_block(root, bytenr, blocksize);
1363 1364
		if (!next || !btrfs_buffer_uptodate(next)) {
			free_extent_buffer(next);
1365
			mutex_unlock(&root->fs_info->fs_mutex);
1366
			next = read_tree_block(root, bytenr, blocksize);
1367 1368 1369
			mutex_lock(&root->fs_info->fs_mutex);

			/* we dropped the lock, check one more time */
1370 1371
			ret = lookup_extent_ref(trans, root, bytenr,
						blocksize, &refs);
1372 1373 1374
			BUG_ON(ret);
			if (refs != 1) {
				path->slots[*level]++;
1375
				free_extent_buffer(next);
1376
				ret = btrfs_free_extent(trans, root,
1377
							bytenr, blocksize, 1);
1378 1379 1380 1381
				BUG_ON(ret);
				continue;
			}
		}
1382
		WARN_ON(*level <= 0);
C
Chris Mason 已提交
1383
		if (path->nodes[*level-1])
1384
			free_extent_buffer(path->nodes[*level-1]);
C
Chris Mason 已提交
1385
		path->nodes[*level-1] = next;
1386
		*level = btrfs_header_level(next);
C
Chris Mason 已提交
1387 1388 1389
		path->slots[*level] = 0;
	}
out:
1390 1391
	WARN_ON(*level < 0);
	WARN_ON(*level >= BTRFS_MAX_LEVEL);
1392 1393
	ret = btrfs_free_extent(trans, root, path->nodes[*level]->start,
				path->nodes[*level]->len, 1);
1394
	free_extent_buffer(path->nodes[*level]);
C
Chris Mason 已提交
1395 1396 1397 1398 1399 1400
	path->nodes[*level] = NULL;
	*level += 1;
	BUG_ON(ret);
	return 0;
}

C
Chris Mason 已提交
1401 1402 1403 1404 1405
/*
 * 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
 */
1406 1407
static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root
			*root, struct btrfs_path *path, int *level)
C
Chris Mason 已提交
1408 1409 1410 1411
{
	int i;
	int slot;
	int ret;
1412 1413
	struct btrfs_root_item *root_item = &root->root_item;

C
Chris Mason 已提交
1414
	for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
C
Chris Mason 已提交
1415
		slot = path->slots[i];
1416 1417 1418 1419
		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 已提交
1420 1421
			path->slots[i]++;
			*level = i;
1422
			WARN_ON(*level == 0);
1423
			btrfs_node_key(node, &disk_key, path->slots[i]);
1424
			memcpy(&root_item->drop_progress,
1425
			       &disk_key, sizeof(disk_key));
1426
			root_item->drop_level = i;
C
Chris Mason 已提交
1427 1428
			return 0;
		} else {
1429
			ret = btrfs_free_extent(trans, root,
1430 1431
						path->nodes[*level]->start,
						path->nodes[*level]->len, 1);
1432
			BUG_ON(ret);
1433
			free_extent_buffer(path->nodes[*level]);
C
Chris Mason 已提交
1434
			path->nodes[*level] = NULL;
C
Chris Mason 已提交
1435 1436 1437 1438 1439 1440
			*level = i + 1;
		}
	}
	return 1;
}

C
Chris Mason 已提交
1441 1442 1443 1444 1445
/*
 * 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.
 */
1446
int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
1447
			*root)
C
Chris Mason 已提交
1448
{
1449
	int ret = 0;
C
Chris Mason 已提交
1450
	int wret;
C
Chris Mason 已提交
1451
	int level;
1452
	struct btrfs_path *path;
C
Chris Mason 已提交
1453 1454
	int i;
	int orig_level;
1455
	struct btrfs_root_item *root_item = &root->root_item;
C
Chris Mason 已提交
1456

1457 1458
	path = btrfs_alloc_path();
	BUG_ON(!path);
C
Chris Mason 已提交
1459

1460
	level = btrfs_header_level(root->node);
C
Chris Mason 已提交
1461
	orig_level = level;
1462 1463
	if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
		path->nodes[level] = root->node;
1464
		extent_buffer_get(root->node);
1465 1466 1467
		path->slots[level] = 0;
	} else {
		struct btrfs_key key;
1468 1469
		struct btrfs_disk_key found_key;
		struct extent_buffer *node;
1470

1471
		btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
1472 1473
		level = root_item->drop_level;
		path->lowest_level = level;
1474
		wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1475
		if (wret < 0) {
1476 1477 1478
			ret = wret;
			goto out;
		}
1479 1480 1481 1482
		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)));
1483
	}
C
Chris Mason 已提交
1484
	while(1) {
1485
		wret = walk_down_tree(trans, root, path, &level);
C
Chris Mason 已提交
1486
		if (wret > 0)
C
Chris Mason 已提交
1487
			break;
C
Chris Mason 已提交
1488 1489 1490
		if (wret < 0)
			ret = wret;

1491
		wret = walk_up_tree(trans, root, path, &level);
C
Chris Mason 已提交
1492
		if (wret > 0)
C
Chris Mason 已提交
1493
			break;
C
Chris Mason 已提交
1494 1495
		if (wret < 0)
			ret = wret;
1496 1497
		ret = -EAGAIN;
		break;
C
Chris Mason 已提交
1498
	}
C
Chris Mason 已提交
1499
	for (i = 0; i <= orig_level; i++) {
1500
		if (path->nodes[i]) {
1501
			free_extent_buffer(path->nodes[i]);
1502
			path->nodes[i] = NULL;
C
Chris Mason 已提交
1503
		}
C
Chris Mason 已提交
1504
	}
1505
out:
1506
	btrfs_free_path(path);
C
Chris Mason 已提交
1507
	return ret;
C
Chris Mason 已提交
1508
}
C
Chris Mason 已提交
1509

1510
int btrfs_free_block_groups(struct btrfs_fs_info *info)
C
Chris Mason 已提交
1511
{
1512 1513
	u64 start;
	u64 end;
1514
	u64 ptr;
C
Chris Mason 已提交
1515 1516
	int ret;
	while(1) {
1517 1518 1519
		ret = find_first_extent_bit(&info->block_group_cache, 0,
					    &start, &end, (unsigned int)-1);
		if (ret)
C
Chris Mason 已提交
1520
			break;
1521 1522 1523
		ret = get_state_private(&info->block_group_cache, start, &ptr);
		if (!ret)
			kfree((void *)(unsigned long)ptr);
1524 1525
		clear_extent_bits(&info->block_group_cache, start,
				  end, (unsigned int)-1, GFP_NOFS);
C
Chris Mason 已提交
1526
	}
1527
	while(1) {
1528 1529 1530
		ret = find_first_extent_bit(&info->free_space_cache, 0,
					    &start, &end, EXTENT_DIRTY);
		if (ret)
1531
			break;
1532 1533
		clear_extent_dirty(&info->free_space_cache, start,
				   end, GFP_NOFS);
1534
	}
C
Chris Mason 已提交
1535 1536 1537
	return 0;
}

C
Chris Mason 已提交
1538 1539 1540 1541 1542
int btrfs_read_block_groups(struct btrfs_root *root)
{
	struct btrfs_path *path;
	int ret;
	int err = 0;
1543
	int bit;
C
Chris Mason 已提交
1544
	struct btrfs_block_group_cache *cache;
C
Chris Mason 已提交
1545
	struct btrfs_fs_info *info = root->fs_info;
1546
	struct extent_map_tree *block_group_cache;
C
Chris Mason 已提交
1547 1548
	struct btrfs_key key;
	struct btrfs_key found_key;
1549
	struct extent_buffer *leaf;
1550 1551

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

C
Chris Mason 已提交
1553
	root = info->extent_root;
C
Chris Mason 已提交
1554
	key.objectid = 0;
1555
	key.offset = BTRFS_BLOCK_GROUP_SIZE;
C
Chris Mason 已提交
1556 1557 1558 1559 1560 1561 1562
	btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);

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

	while(1) {
C
Chris Mason 已提交
1563
		ret = btrfs_search_slot(NULL, info->extent_root,
C
Chris Mason 已提交
1564 1565 1566 1567 1568
					&key, path, 0, 0);
		if (ret != 0) {
			err = ret;
			break;
		}
1569 1570
		leaf = path->nodes[0];
		btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
C
Chris Mason 已提交
1571 1572 1573 1574 1575
		cache = kmalloc(sizeof(*cache), GFP_NOFS);
		if (!cache) {
			err = -1;
			break;
		}
C
Chris Mason 已提交
1576

1577 1578 1579
		read_extent_buffer(leaf, &cache->item,
				   btrfs_item_ptr_offset(leaf, path->slots[0]),
				   sizeof(cache->item));
C
Chris Mason 已提交
1580
		memcpy(&cache->key, &found_key, sizeof(found_key));
1581 1582
		cache->cached = 0;

C
Chris Mason 已提交
1583 1584
		key.objectid = found_key.objectid + found_key.offset;
		btrfs_release_path(root, path);
1585

1586 1587 1588 1589
		if (cache->item.flags & BTRFS_BLOCK_GROUP_MIXED) {
			bit = BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA;
			cache->data = BTRFS_BLOCK_GROUP_MIXED;
		} else if (cache->item.flags & BTRFS_BLOCK_GROUP_DATA) {
1590
			bit = BLOCK_GROUP_DATA;
1591
			cache->data = BTRFS_BLOCK_GROUP_DATA;
1592 1593 1594
		} else {
			bit = BLOCK_GROUP_METADATA;
			cache->data = 0;
1595
		}
1596 1597 1598 1599 1600 1601

		/* 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,
J
Jens Axboe 已提交
1602
				  (unsigned long)cache);
1603

C
Chris Mason 已提交
1604
		if (key.objectid >=
1605
		    btrfs_super_total_bytes(&info->super_copy))
C
Chris Mason 已提交
1606 1607 1608 1609 1610 1611
			break;
	}

	btrfs_free_path(path);
	return 0;
}