extent-tree.c 39.1 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
	int wrapped = 0;
172 173

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

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

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

		start = max(last, start);
		last = end + 1;
		if (end + 1 - start < num)
			continue;
191
		if (start + num >= cache->key.objectid + cache->key.offset)
192
			goto new_group;
193
		return start;
194 195
	}
out:
196
	return search_start;
197 198

new_group:
199
	last = cache->key.objectid + cache->key.offset;
200
wrapped:
201
	cache = btrfs_lookup_block_group(root->fs_info, last);
202
	if (!cache) {
203 204 205 206 207 208
		if (!wrapped) {
			wrapped = 1;
			last = search_start;
			data = BTRFS_BLOCK_GROUP_MIXED;
			goto wrapped;
		}
209
		return search_start;
210
	}
211
	cache = btrfs_find_block_group(root, cache, last, data, 0);
212 213 214 215
	*cache_ret = cache;
	goto again;
}

C
Chris Mason 已提交
216 217 218 219 220 221 222
static u64 div_factor(u64 num, int factor)
{
	num *= factor;
	do_div(num, 10);
	return num;
}

223 224
struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
						 struct btrfs_block_group_cache
C
Chris Mason 已提交
225
						 *hint, u64 search_start,
226
						 int data, int owner)
C
Chris Mason 已提交
227
{
228 229
	struct btrfs_block_group_cache *cache;
	struct extent_map_tree *block_group_cache;
230
	struct btrfs_block_group_cache *found_group = NULL;
C
Chris Mason 已提交
231 232
	struct btrfs_fs_info *info = root->fs_info;
	u64 used;
233 234
	u64 last = 0;
	u64 hint_last;
235 236 237 238 239
	u64 start;
	u64 end;
	u64 free_check;
	u64 ptr;
	int bit;
C
Chris Mason 已提交
240
	int ret;
241
	int full_search = 0;
242
	int factor = 8;
C
Chris Mason 已提交
243
	int data_swap = 0;
244

245 246
	block_group_cache = &info->block_group_cache;

247
	if (!owner)
248
		factor = 8;
C
Chris Mason 已提交
249

250 251 252
	if (data == BTRFS_BLOCK_GROUP_MIXED)
		bit = BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA;
	else if (data)
253 254 255
		bit = BLOCK_GROUP_DATA;
	else
		bit = BLOCK_GROUP_METADATA;
C
Chris Mason 已提交
256 257 258

	if (search_start) {
		struct btrfs_block_group_cache *shint;
259
		shint = btrfs_lookup_block_group(info, search_start);
260 261
		if (shint && (shint->data == data ||
			      shint->data == BTRFS_BLOCK_GROUP_MIXED)) {
C
Chris Mason 已提交
262
			used = btrfs_block_group_used(&shint->item);
263
			if (used < div_factor(shint->key.offset, factor)) {
C
Chris Mason 已提交
264 265 266 267
				return shint;
			}
		}
	}
268 269
	if (hint && (hint->data == data ||
		     hint->data == BTRFS_BLOCK_GROUP_MIXED)) {
270
		used = btrfs_block_group_used(&hint->item);
271
		if (used < div_factor(hint->key.offset, factor)) {
272 273
			return hint;
		}
274
		last = hint->key.objectid + hint->key.offset;
275 276
		hint_last = last;
	} else {
277 278 279 280 281 282
		if (hint)
			hint_last = max(hint->key.objectid, search_start);
		else
			hint_last = search_start;

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

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

J
Jens Axboe 已提交
295
		cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
296 297 298 299 300 301 302 303
		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);

304
		if (used < free_check) {
305 306
			found_group = cache;
			goto found;
C
Chris Mason 已提交
307
		}
308
		cond_resched();
C
Chris Mason 已提交
309
	}
310
	if (!full_search) {
C
Chris Mason 已提交
311
		last = search_start;
312 313 314
		full_search = 1;
		goto again;
	}
C
Chris Mason 已提交
315 316
	if (!data_swap) {
		data_swap = 1;
317
		bit = BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA;
C
Chris Mason 已提交
318 319 320
		last = search_start;
		goto again;
	}
C
Chris Mason 已提交
321
found:
322
	return found_group;
C
Chris Mason 已提交
323 324
}

C
Chris Mason 已提交
325 326
int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
				struct btrfs_root *root,
327
				u64 bytenr, u64 num_bytes)
C
Chris Mason 已提交
328
{
329
	struct btrfs_path *path;
C
Chris Mason 已提交
330
	int ret;
C
Chris Mason 已提交
331
	struct btrfs_key key;
332
	struct extent_buffer *l;
C
Chris Mason 已提交
333
	struct btrfs_extent_item *item;
C
Chris Mason 已提交
334
	u32 refs;
C
Chris Mason 已提交
335

336
	WARN_ON(num_bytes < root->sectorsize);
337
	path = btrfs_alloc_path();
338 339
	if (!path)
		return -ENOMEM;
340

341
	key.objectid = bytenr;
342
	btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
343
	key.offset = num_bytes;
344
	ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
345
				0, 1);
346 347
	if (ret < 0)
		return ret;
348
	if (ret != 0) {
349
		BUG();
350
	}
C
Chris Mason 已提交
351
	BUG_ON(ret != 0);
352
	l = path->nodes[0];
353
	item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
354 355
	refs = btrfs_extent_refs(l, item);
	btrfs_set_extent_refs(l, item, refs + 1);
356
	btrfs_mark_buffer_dirty(path->nodes[0]);
357

358 359
	btrfs_release_path(root->fs_info->extent_root, path);
	btrfs_free_path(path);
360
	finish_current_insert(trans, root->fs_info->extent_root);
C
Chris Mason 已提交
361
	del_pending_extents(trans, root->fs_info->extent_root);
C
Chris Mason 已提交
362 363 364
	return 0;
}

365 366 367 368 369 370 371 372
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 已提交
373
static int lookup_extent_ref(struct btrfs_trans_handle *trans,
374 375
			     struct btrfs_root *root, u64 bytenr,
			     u64 num_bytes, u32 *refs)
376
{
377
	struct btrfs_path *path;
378
	int ret;
C
Chris Mason 已提交
379
	struct btrfs_key key;
380
	struct extent_buffer *l;
C
Chris Mason 已提交
381
	struct btrfs_extent_item *item;
382

383
	WARN_ON(num_bytes < root->sectorsize);
384
	path = btrfs_alloc_path();
385 386
	key.objectid = bytenr;
	key.offset = num_bytes;
387
	btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
388
	ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
389
				0, 0);
390 391
	if (ret < 0)
		goto out;
392 393
	if (ret != 0) {
		btrfs_print_leaf(root, path->nodes[0]);
394
		printk("failed to find block number %Lu\n", bytenr);
395
		BUG();
396 397
	}
	l = path->nodes[0];
398
	item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
399
	*refs = btrfs_extent_refs(l, item);
400
out:
401
	btrfs_free_path(path);
402 403 404
	return 0;
}

C
Chris Mason 已提交
405 406 407
int btrfs_inc_root_ref(struct btrfs_trans_handle *trans,
		       struct btrfs_root *root)
{
408 409
	return btrfs_inc_extent_ref(trans, root, root->node->start,
				    root->node->len);
C
Chris Mason 已提交
410 411
}

412
int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
413
		  struct extent_buffer *buf)
C
Chris Mason 已提交
414
{
415
	u64 bytenr;
416 417
	u32 nritems;
	struct btrfs_key key;
418
	struct btrfs_file_extent_item *fi;
C
Chris Mason 已提交
419
	int i;
420
	int level;
421
	int ret;
422 423
	int faili;
	int err;
424

425
	if (!root->ref_cows)
426
		return 0;
427

428
	level = btrfs_header_level(buf);
429 430
	nritems = btrfs_header_nritems(buf);
	for (i = 0; i < nritems; i++) {
431 432
		if (level == 0) {
			u64 disk_bytenr;
433 434
			btrfs_item_key_to_cpu(buf, &key, i);
			if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
435
				continue;
436
			fi = btrfs_item_ptr(buf, i,
437
					    struct btrfs_file_extent_item);
438
			if (btrfs_file_extent_type(buf, fi) ==
439 440
			    BTRFS_FILE_EXTENT_INLINE)
				continue;
441 442
			disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
			if (disk_bytenr == 0)
C
Chris Mason 已提交
443
				continue;
444 445
			ret = btrfs_inc_extent_ref(trans, root, disk_bytenr,
				    btrfs_file_extent_disk_num_bytes(buf, fi));
446 447 448 449
			if (ret) {
				faili = i;
				goto fail;
			}
450
		} else {
451 452 453
			bytenr = btrfs_node_blockptr(buf, i);
			ret = btrfs_inc_extent_ref(trans, root, bytenr,
					   btrfs_level_size(root, level - 1));
454 455 456 457
			if (ret) {
				faili = i;
				goto fail;
			}
458
		}
C
Chris Mason 已提交
459 460
	}
	return 0;
461
fail:
C
Chris Mason 已提交
462
	WARN_ON(1);
463
	for (i =0; i < faili; i++) {
464 465
		if (level == 0) {
			u64 disk_bytenr;
466 467
			btrfs_item_key_to_cpu(buf, &key, i);
			if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
468
				continue;
469
			fi = btrfs_item_ptr(buf, i,
470
					    struct btrfs_file_extent_item);
471
			if (btrfs_file_extent_type(buf, fi) ==
472 473
			    BTRFS_FILE_EXTENT_INLINE)
				continue;
474 475
			disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
			if (disk_bytenr == 0)
476
				continue;
477 478
			err = btrfs_free_extent(trans, root, disk_bytenr,
				    btrfs_file_extent_disk_num_bytes(buf,
479
								      fi), 0);
480 481
			BUG_ON(err);
		} else {
482 483 484
			bytenr = btrfs_node_blockptr(buf, i);
			err = btrfs_free_extent(trans, root, bytenr,
					btrfs_level_size(root, level - 1), 0);
485 486 487 488
			BUG_ON(err);
		}
	}
	return ret;
C
Chris Mason 已提交
489 490
}

C
Chris Mason 已提交
491 492 493 494 495 496 497 498
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;
499 500
	unsigned long bi;
	struct extent_buffer *leaf;
C
Chris Mason 已提交
501 502

	ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
503 504
	if (ret < 0)
		goto fail;
C
Chris Mason 已提交
505
	BUG_ON(ret);
506 507 508 509 510

	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 已提交
511
	btrfs_release_path(extent_root, path);
512
fail:
C
Chris Mason 已提交
513 514 515 516 517 518 519 520 521 522
	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;

}

523 524
int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
				   struct btrfs_root *root)
C
Chris Mason 已提交
525
{
526 527
	struct extent_map_tree *block_group_cache;
	struct btrfs_block_group_cache *cache;
C
Chris Mason 已提交
528 529 530 531
	int ret;
	int err = 0;
	int werr = 0;
	struct btrfs_path *path;
532 533 534 535
	u64 last = 0;
	u64 start;
	u64 end;
	u64 ptr;
C
Chris Mason 已提交
536

537
	block_group_cache = &root->fs_info->block_group_cache;
C
Chris Mason 已提交
538 539 540 541 542
	path = btrfs_alloc_path();
	if (!path)
		return -ENOMEM;

	while(1) {
543 544 545
		ret = find_first_extent_bit(block_group_cache, last,
					    &start, &end, BLOCK_GROUP_DIRTY);
		if (ret)
C
Chris Mason 已提交
546
			break;
547

548 549 550 551 552
		last = end + 1;
		ret = get_state_private(block_group_cache, start, &ptr);
		if (ret)
			break;

J
Jens Axboe 已提交
553
		cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
554 555 556 557 558 559 560 561 562 563
		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 已提交
564
		}
565 566
		clear_extent_bits(block_group_cache, start, end,
				  BLOCK_GROUP_DIRTY, GFP_NOFS);
C
Chris Mason 已提交
567 568 569 570 571 572 573
	}
	btrfs_free_path(path);
	return werr;
}

static int update_block_group(struct btrfs_trans_handle *trans,
			      struct btrfs_root *root,
574 575
			      u64 bytenr, u64 num_bytes, int alloc,
			      int mark_free, int data)
C
Chris Mason 已提交
576 577 578
{
	struct btrfs_block_group_cache *cache;
	struct btrfs_fs_info *info = root->fs_info;
579
	u64 total = num_bytes;
C
Chris Mason 已提交
580
	u64 old_val;
581
	u64 byte_in_group;
582 583
	u64 start;
	u64 end;
C
Chris Mason 已提交
584

C
Chris Mason 已提交
585
	while(total) {
586
		cache = btrfs_lookup_block_group(info, bytenr);
C
Chris Mason 已提交
587
		if (!cache) {
C
Chris Mason 已提交
588
			return -1;
C
Chris Mason 已提交
589
		}
590 591
		byte_in_group = bytenr - cache->key.objectid;
		WARN_ON(byte_in_group > cache->key.offset);
592 593 594 595
		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 已提交
596 597

		old_val = btrfs_block_group_used(&cache->item);
598
		num_bytes = min(total, cache->key.offset - byte_in_group);
C
Chris Mason 已提交
599
		if (alloc) {
C
Chris Mason 已提交
600
			if (cache->data != data &&
C
Chris Mason 已提交
601
			    old_val < (cache->key.offset >> 1)) {
602 603 604
				int bit_to_clear;
				int bit_to_set;
				cache->data = data;
C
Chris Mason 已提交
605
				if (data) {
606 607
					bit_to_clear = BLOCK_GROUP_METADATA;
					bit_to_set = BLOCK_GROUP_DATA;
608 609
					cache->item.flags &=
						~BTRFS_BLOCK_GROUP_MIXED;
C
Chris Mason 已提交
610 611 612
					cache->item.flags |=
						BTRFS_BLOCK_GROUP_DATA;
				} else {
613 614
					bit_to_clear = BLOCK_GROUP_DATA;
					bit_to_set = BLOCK_GROUP_METADATA;
615 616
					cache->item.flags &=
						~BTRFS_BLOCK_GROUP_MIXED;
C
Chris Mason 已提交
617 618 619
					cache->item.flags &=
						~BTRFS_BLOCK_GROUP_DATA;
				}
620 621 622 623 624 625
				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);
626 627 628 629 630 631 632 633
			} 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 已提交
634
			}
635
			old_val += num_bytes;
C
Chris Mason 已提交
636
		} else {
637
			old_val -= num_bytes;
638 639
			if (mark_free) {
				set_extent_dirty(&info->free_space_cache,
640
						 bytenr, bytenr + num_bytes - 1,
641
						 GFP_NOFS);
642
			}
C
Chris Mason 已提交
643
		}
C
Chris Mason 已提交
644
		btrfs_set_block_group_used(&cache->item, old_val);
645 646
		total -= num_bytes;
		bytenr += num_bytes;
C
Chris Mason 已提交
647 648 649 650
	}
	return 0;
}

651
int btrfs_copy_pinned(struct btrfs_root *root, struct extent_map_tree *copy)
C
Chris Mason 已提交
652 653
{
	u64 last = 0;
654 655 656
	u64 start;
	u64 end;
	struct extent_map_tree *pinned_extents = &root->fs_info->pinned_extents;
C
Chris Mason 已提交
657 658 659
	int ret;

	while(1) {
660 661 662
		ret = find_first_extent_bit(pinned_extents, last,
					    &start, &end, EXTENT_DIRTY);
		if (ret)
C
Chris Mason 已提交
663
			break;
664 665
		set_extent_dirty(copy, start, end, GFP_NOFS);
		last = end + 1;
C
Chris Mason 已提交
666 667 668 669 670 671
	}
	return 0;
}

int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
			       struct btrfs_root *root,
672
			       struct extent_map_tree *unpin)
673
{
674 675
	u64 start;
	u64 end;
676
	int ret;
677
	struct extent_map_tree *pinned_extents = &root->fs_info->pinned_extents;
678 679 680
	struct extent_map_tree *free_space_cache;

	free_space_cache = &root->fs_info->free_space_cache;
681 682

	while(1) {
683 684 685
		ret = find_first_extent_bit(unpin, 0, &start, &end,
					    EXTENT_DIRTY);
		if (ret)
686
			break;
687 688 689 690 691

		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);
692 693 694 695
	}
	return 0;
}

696 697
static int finish_current_insert(struct btrfs_trans_handle *trans, struct
				 btrfs_root *extent_root)
C
Chris Mason 已提交
698
{
C
Chris Mason 已提交
699
	struct btrfs_key ins;
C
Chris Mason 已提交
700
	struct btrfs_extent_item extent_item;
C
Chris Mason 已提交
701
	int ret;
702 703 704
	int err = 0;
	u64 start;
	u64 end;
705
	struct btrfs_fs_info *info = extent_root->fs_info;
C
Chris Mason 已提交
706

707
	btrfs_set_stack_extent_refs(&extent_item, 1);
708
	btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
709 710
	btrfs_set_stack_extent_owner(&extent_item,
				     extent_root->root_key.objectid);
C
Chris Mason 已提交
711

712
	while(1) {
713 714 715
		ret = find_first_extent_bit(&info->extent_ins, 0, &start,
					    &end, EXTENT_LOCKED);
		if (ret)
716 717
			break;

718 719 720 721 722 723
		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 已提交
724 725 726 727
	}
	return 0;
}

728 729
static int pin_down_bytes(struct btrfs_root *root, u64 bytenr, u32 num_bytes,
			  int pending)
C
Chris Mason 已提交
730
{
731
	int err = 0;
732
	struct extent_buffer *buf;
C
Chris Mason 已提交
733

C
Chris Mason 已提交
734
	if (!pending) {
735
		buf = btrfs_find_tree_block(root, bytenr, num_bytes);
736 737
		if (buf) {
			if (btrfs_buffer_uptodate(buf)) {
C
Chris Mason 已提交
738 739
				u64 transid =
				    root->fs_info->running_transaction->transid;
740 741
				if (btrfs_header_generation(buf) == transid) {
					free_extent_buffer(buf);
742
					return 1;
C
Chris Mason 已提交
743
				}
C
Chris Mason 已提交
744
			}
745
			free_extent_buffer(buf);
C
Chris Mason 已提交
746
		}
747
		set_extent_dirty(&root->fs_info->pinned_extents,
748
				 bytenr, bytenr + num_bytes - 1, GFP_NOFS);
C
Chris Mason 已提交
749
	} else {
750
		set_extent_bits(&root->fs_info->pending_del,
751 752
				bytenr, bytenr + num_bytes - 1,
				EXTENT_LOCKED, GFP_NOFS);
C
Chris Mason 已提交
753
	}
C
Chris Mason 已提交
754
	BUG_ON(err < 0);
C
Chris Mason 已提交
755 756 757
	return 0;
}

758
/*
759
 * remove an extent from the root, returns 0 on success
760
 */
761
static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
762
			 *root, u64 bytenr, u64 num_bytes, int pin,
763
			 int mark_free)
764
{
765
	struct btrfs_path *path;
C
Chris Mason 已提交
766
	struct btrfs_key key;
767 768
	struct btrfs_fs_info *info = root->fs_info;
	struct btrfs_root *extent_root = info->extent_root;
769
	struct extent_buffer *leaf;
770
	int ret;
C
Chris Mason 已提交
771
	struct btrfs_extent_item *ei;
C
Chris Mason 已提交
772
	u32 refs;
C
Chris Mason 已提交
773

774
	key.objectid = bytenr;
775
	btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
776
	key.offset = num_bytes;
777

778
	path = btrfs_alloc_path();
779 780
	if (!path)
		return -ENOMEM;
781

782 783 784 785
	ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
	if (ret < 0)
		return ret;
	BUG_ON(ret);
786 787 788

	leaf = path->nodes[0];
	ei = btrfs_item_ptr(leaf, path->slots[0],
C
Chris Mason 已提交
789
			    struct btrfs_extent_item);
790 791 792 793 794 795
	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 已提交
796
	if (refs == 0) {
797 798
		u64 super_used;
		u64 root_used;
C
Chris Mason 已提交
799 800

		if (pin) {
801
			ret = pin_down_bytes(root, bytenr, num_bytes, 0);
802 803 804
			if (ret > 0)
				mark_free = 1;
			BUG_ON(ret < 0);
C
Chris Mason 已提交
805 806
		}

807
		/* block accounting for super block */
808 809 810
		super_used = btrfs_super_bytes_used(&info->super_copy);
		btrfs_set_super_bytes_used(&info->super_copy,
					   super_used - num_bytes);
811 812

		/* block accounting for root item */
813
		root_used = btrfs_root_used(&root->root_item);
814
		btrfs_set_root_used(&root->root_item,
815
					   root_used - num_bytes);
816

817
		ret = btrfs_del_item(trans, extent_root, path);
818 819 820
		if (ret) {
			return ret;
		}
821
		ret = update_block_group(trans, root, bytenr, num_bytes, 0,
C
Chris Mason 已提交
822
					 mark_free, 0);
C
Chris Mason 已提交
823
		BUG_ON(ret);
824
	}
825
	btrfs_free_path(path);
826
	finish_current_insert(trans, extent_root);
827 828 829 830 831 832 833
	return ret;
}

/*
 * find all the blocks marked as pending in the radix tree and remove
 * them from the extent map
 */
834 835
static int del_pending_extents(struct btrfs_trans_handle *trans, struct
			       btrfs_root *extent_root)
836 837
{
	int ret;
C
Chris Mason 已提交
838
	int err = 0;
839 840 841 842
	u64 start;
	u64 end;
	struct extent_map_tree *pending_del;
	struct extent_map_tree *pinned_extents;
C
Chris Mason 已提交
843

844 845
	pending_del = &extent_root->fs_info->pending_del;
	pinned_extents = &extent_root->fs_info->pinned_extents;
846 847

	while(1) {
848 849 850
		ret = find_first_extent_bit(pending_del, 0, &start, &end,
					    EXTENT_LOCKED);
		if (ret)
851
			break;
852 853 854 855 856 857 858 859

		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;
860
	}
C
Chris Mason 已提交
861
	return err;
862 863 864 865 866
}

/*
 * remove an extent from the root, returns 0 on success
 */
867
int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
868
		      *root, u64 bytenr, u64 num_bytes, int pin)
869
{
870
	struct btrfs_root *extent_root = root->fs_info->extent_root;
871 872
	int pending_ret;
	int ret;
873

874
	WARN_ON(num_bytes < root->sectorsize);
875
	if (root == extent_root) {
876
		pin_down_bytes(root, bytenr, num_bytes, 1);
877 878
		return 0;
	}
879
	ret = __free_extent(trans, root, bytenr, num_bytes, pin, pin == 0);
C
Chris Mason 已提交
880
	pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
881 882 883 884 885 886 887
	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
888
 * ins->flags = BTRFS_EXTENT_ITEM_KEY
889 890 891
 * ins->offset == number of blocks
 * Any available blocks before search_start are skipped.
 */
892
static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
893 894
			    *orig_root, u64 num_bytes, u64 empty_size,
			    u64 search_start, u64 search_end, u64 hint_byte,
895 896
			    struct btrfs_key *ins, u64 exclude_start,
			    u64 exclude_nr, int data)
897
{
898
	struct btrfs_path *path;
C
Chris Mason 已提交
899
	struct btrfs_key key;
900 901 902
	int ret;
	u64 hole_size = 0;
	int slot = 0;
903
	u64 last_byte = 0;
C
Chris Mason 已提交
904
	u64 orig_search_start = search_start;
905
	int start_found;
906
	struct extent_buffer *l;
907
	struct btrfs_root * root = orig_root->fs_info->extent_root;
908
	struct btrfs_fs_info *info = root->fs_info;
909
	u64 total_needed = num_bytes;
C
Chris Mason 已提交
910
	int level;
C
Chris Mason 已提交
911
	struct btrfs_block_group_cache *block_group;
C
Chris Mason 已提交
912
	int full_scan = 0;
913
	int wrapped = 0;
914
	u64 cached_start;
915

916
	WARN_ON(num_bytes < root->sectorsize);
917 918
	btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);

919 920
	level = btrfs_header_level(root->node);

C
Chris Mason 已提交
921
	if (search_end == (u64)-1)
922 923 924
		search_end = btrfs_super_total_bytes(&info->super_copy);
	if (hint_byte) {
		block_group = btrfs_lookup_block_group(info, hint_byte);
C
Chris Mason 已提交
925
		block_group = btrfs_find_block_group(root, block_group,
926
						     hint_byte, data, 1);
C
Chris Mason 已提交
927 928 929
	} else {
		block_group = btrfs_find_block_group(root,
						     trans->block_group, 0,
930
						     data, 1);
C
Chris Mason 已提交
931 932
	}

933
	total_needed += empty_size;
934 935
	path = btrfs_alloc_path();

C
Chris Mason 已提交
936
check_failed:
937 938
	search_start = find_search_start(root, &block_group,
					 search_start, total_needed, data);
939
	cached_start = search_start;
940
	btrfs_init_path(path);
941 942 943
	ins->objectid = search_start;
	ins->offset = 0;
	start_found = 0;
944
	path->reada = 2;
945

946
	ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
C
Chris Mason 已提交
947 948
	if (ret < 0)
		goto error;
C
Chris Mason 已提交
949

950
	if (path->slots[0] > 0) {
951
		path->slots[0]--;
952 953
	}

954 955 956
	l = path->nodes[0];
	btrfs_item_key_to_cpu(l, &key, path->slots[0]);

957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973
	/*
	 * 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]--;
		}
	}
974

975
	while (1) {
976
		l = path->nodes[0];
977
		slot = path->slots[0];
978
		if (slot >= btrfs_header_nritems(l)) {
979
			ret = btrfs_next_leaf(root, path);
980 981
			if (ret == 0)
				continue;
C
Chris Mason 已提交
982 983
			if (ret < 0)
				goto error;
984 985 986

			search_start = max(search_start,
					   block_group->key.objectid);
987 988
			if (!start_found) {
				ins->objectid = search_start;
C
Chris Mason 已提交
989
				ins->offset = search_end - search_start;
990 991 992
				start_found = 1;
				goto check_pending;
			}
993 994
			ins->objectid = last_byte > search_start ?
					last_byte : search_start;
C
Chris Mason 已提交
995
			ins->offset = search_end - ins->objectid;
996
			BUG_ON(ins->objectid >= search_end);
997 998
			goto check_pending;
		}
999
		btrfs_item_key_to_cpu(l, &key, slot);
1000

1001
		if (key.objectid >= search_start && key.objectid > last_byte &&
1002
		    start_found) {
1003 1004 1005 1006 1007
			if (last_byte < search_start)
				last_byte = search_start;
			hole_size = key.objectid - last_byte;
			if (hole_size >= num_bytes) {
				ins->objectid = last_byte;
1008 1009
				ins->offset = hole_size;
				goto check_pending;
1010
			}
1011
		}
1012 1013
		if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY) {
			if (!start_found) {
1014
				last_byte = key.objectid;
1015 1016
				start_found = 1;
			}
1017
			goto next;
1018 1019
		}

1020

1021
		start_found = 1;
1022
		last_byte = key.objectid + key.offset;
1023

1024
		if (!full_scan && last_byte >= block_group->key.objectid +
C
Chris Mason 已提交
1025 1026 1027
		    block_group->key.offset) {
			btrfs_release_path(root, path);
			search_start = block_group->key.objectid +
1028
				block_group->key.offset;
C
Chris Mason 已提交
1029 1030
			goto new_group;
		}
C
Chris Mason 已提交
1031
next:
1032
		path->slots[0]++;
1033
		cond_resched();
1034 1035 1036 1037 1038
	}
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
	 */
1039
	btrfs_release_path(root, path);
1040
	BUG_ON(ins->objectid < search_start);
1041

1042
	if (ins->objectid + num_bytes >= search_end)
1043 1044
		goto enospc;

1045 1046 1047 1048 1049 1050
	if (!full_scan && ins->objectid + num_bytes >= block_group->
	    key.objectid + block_group->key.offset) {
		search_start = block_group->key.objectid +
			block_group->key.offset;
		goto new_group;
	}
1051
	if (test_range_bit(&info->extent_ins, ins->objectid,
1052 1053
			   ins->objectid + num_bytes -1, EXTENT_LOCKED, 0)) {
		search_start = ins->objectid + num_bytes;
1054 1055 1056
		goto new_group;
	}
	if (test_range_bit(&info->pinned_extents, ins->objectid,
1057 1058
			   ins->objectid + num_bytes -1, EXTENT_DIRTY, 0)) {
		search_start = ins->objectid + num_bytes;
1059
		goto new_group;
1060
	}
1061
	if (exclude_nr > 0 && (ins->objectid + num_bytes > exclude_start &&
1062 1063 1064 1065
	    ins->objectid < exclude_start + exclude_nr)) {
		search_start = exclude_start + exclude_nr;
		goto new_group;
	}
1066
	if (!data) {
1067
		block_group = btrfs_lookup_block_group(info, ins->objectid);
1068 1069
		if (block_group)
			trans->block_group = block_group;
1070
	}
1071
	ins->offset = num_bytes;
1072
	btrfs_free_path(path);
1073
	return 0;
C
Chris Mason 已提交
1074 1075

new_group:
1076
	if (search_start + num_bytes >= search_end) {
1077
enospc:
C
Chris Mason 已提交
1078
		search_start = orig_search_start;
1079 1080 1081 1082
		if (full_scan) {
			ret = -ENOSPC;
			goto error;
		}
1083 1084 1085
		if (wrapped) {
			if (!full_scan)
				total_needed -= empty_size;
1086
			full_scan = 1;
1087
		} else
1088
			wrapped = 1;
C
Chris Mason 已提交
1089
	}
1090
	block_group = btrfs_lookup_block_group(info, search_start);
1091
	cond_resched();
C
Chris Mason 已提交
1092 1093
	if (!full_scan)
		block_group = btrfs_find_block_group(root, block_group,
1094
						     search_start, data, 0);
C
Chris Mason 已提交
1095 1096
	goto check_failed;

C
Chris Mason 已提交
1097
error:
1098 1099
	btrfs_release_path(root, path);
	btrfs_free_path(path);
C
Chris Mason 已提交
1100
	return ret;
1101 1102 1103 1104 1105 1106 1107 1108
}
/*
 * 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.
 */
1109 1110
int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
		       struct btrfs_root *root, u64 owner,
1111
		       u64 num_bytes, u64 empty_size, u64 hint_byte,
C
Chris Mason 已提交
1112
		       u64 search_end, struct btrfs_key *ins, int data)
1113 1114 1115
{
	int ret;
	int pending_ret;
1116
	u64 super_used, root_used;
1117
	u64 search_start = 0;
1118 1119
	struct btrfs_fs_info *info = root->fs_info;
	struct btrfs_root *extent_root = info->extent_root;
C
Chris Mason 已提交
1120
	struct btrfs_extent_item extent_item;
C
Chris Mason 已提交
1121

1122 1123
	btrfs_set_stack_extent_refs(&extent_item, 1);
	btrfs_set_stack_extent_owner(&extent_item, owner);
1124

1125 1126 1127
	WARN_ON(num_bytes < root->sectorsize);
	ret = find_free_extent(trans, root, num_bytes, empty_size,
			       search_start, search_end, hint_byte, ins,
1128 1129
			       trans->alloc_exclude_start,
			       trans->alloc_exclude_nr, data);
C
Chris Mason 已提交
1130
	BUG_ON(ret);
1131 1132
	if (ret)
		return ret;
1133

1134
	/* block accounting for super block */
1135 1136
	super_used = btrfs_super_bytes_used(&info->super_copy);
	btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes);
1137

1138
	/* block accounting for root item */
1139 1140
	root_used = btrfs_root_used(&root->root_item);
	btrfs_set_root_used(&root->root_item, root_used + num_bytes);
1141

1142 1143 1144 1145
	clear_extent_dirty(&root->fs_info->free_space_cache,
			   ins->objectid, ins->objectid + ins->offset - 1,
			   GFP_NOFS);

1146
	if (root == extent_root) {
1147 1148 1149
		set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
				ins->objectid + ins->offset - 1,
				EXTENT_LOCKED, GFP_NOFS);
1150
		WARN_ON(data == 1);
1151 1152 1153 1154 1155 1156
		goto update_block;
	}

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

1160 1161 1162
	trans->alloc_exclude_start = 0;
	trans->alloc_exclude_nr = 0;

C
Chris Mason 已提交
1163
	BUG_ON(ret);
1164
	finish_current_insert(trans, extent_root);
C
Chris Mason 已提交
1165
	pending_ret = del_pending_extents(trans, extent_root);
1166

1167
	if (ret) {
C
Chris Mason 已提交
1168
		return ret;
1169 1170
	}
	if (pending_ret) {
C
Chris Mason 已提交
1171
		return pending_ret;
1172
	}
1173 1174

update_block:
C
Chris Mason 已提交
1175 1176
	ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0,
				 data);
C
Chris Mason 已提交
1177
	BUG_ON(ret);
C
Chris Mason 已提交
1178
	return 0;
1179 1180 1181 1182 1183 1184
}

/*
 * helper function to allocate a block for a given tree
 * returns the tree buffer or NULL.
 */
1185
struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
1186 1187
					     struct btrfs_root *root,
					     u32 blocksize, u64 hint,
1188
					     u64 empty_size)
1189
{
C
Chris Mason 已提交
1190
	struct btrfs_key ins;
1191
	int ret;
1192
	struct extent_buffer *buf;
1193

1194
	ret = btrfs_alloc_extent(trans, root, root->root_key.objectid,
1195 1196
				 blocksize, empty_size, hint,
				 (u64)-1, &ins, 0);
1197
	if (ret) {
1198 1199
		BUG_ON(ret > 0);
		return ERR_PTR(ret);
1200
	}
1201
	buf = btrfs_find_create_tree_block(root, ins.objectid, blocksize);
1202
	if (!buf) {
1203
		btrfs_free_extent(trans, root, ins.objectid, blocksize, 0);
1204 1205
		return ERR_PTR(-ENOMEM);
	}
1206 1207 1208
	btrfs_set_buffer_uptodate(buf);
	set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
			 buf->start + buf->len - 1, GFP_NOFS);
1209 1210 1211 1212
	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;
1213
	btrfs_set_buffer_defrag(buf);
1214
	trans->blocks_used++;
1215 1216
	return buf;
}
1217

1218
static int drop_leaf_ref(struct btrfs_trans_handle *trans,
1219
			 struct btrfs_root *root, struct extent_buffer *leaf)
1220
{
1221
	struct btrfs_key key;
1222 1223 1224 1225 1226
	struct btrfs_file_extent_item *fi;
	int i;
	int nritems;
	int ret;

1227 1228
	BUG_ON(!btrfs_is_leaf(leaf));
	nritems = btrfs_header_nritems(leaf);
1229
	for (i = 0; i < nritems; i++) {
1230
		u64 disk_bytenr;
1231 1232 1233

		btrfs_item_key_to_cpu(leaf, &key, i);
		if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1234 1235
			continue;
		fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
1236 1237
		if (btrfs_file_extent_type(leaf, fi) ==
		    BTRFS_FILE_EXTENT_INLINE)
1238
			continue;
1239 1240 1241 1242
		/*
		 * FIXME make sure to insert a trans record that
		 * repeats the snapshot del on crash
		 */
1243 1244
		disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
		if (disk_bytenr == 0)
C
Chris Mason 已提交
1245
			continue;
1246 1247
		ret = btrfs_free_extent(trans, root, disk_bytenr,
				btrfs_file_extent_disk_num_bytes(leaf, fi), 0);
1248 1249 1250 1251 1252
		BUG_ON(ret);
	}
	return 0;
}

1253
static void reada_walk_down(struct btrfs_root *root,
1254
			    struct extent_buffer *node)
1255 1256 1257
{
	int i;
	u32 nritems;
1258
	u64 bytenr;
1259 1260
	int ret;
	u32 refs;
1261 1262
	int level;
	u32 blocksize;
1263

1264
	nritems = btrfs_header_nritems(node);
1265
	level = btrfs_header_level(node);
1266
	for (i = 0; i < nritems; i++) {
1267 1268 1269
		bytenr = btrfs_node_blockptr(node, i);
		blocksize = btrfs_level_size(root, level - 1);
		ret = lookup_extent_ref(NULL, root, bytenr, blocksize, &refs);
1270 1271 1272
		BUG_ON(ret);
		if (refs != 1)
			continue;
1273
		mutex_unlock(&root->fs_info->fs_mutex);
1274
		ret = readahead_tree_block(root, bytenr, blocksize);
1275 1276
		cond_resched();
		mutex_lock(&root->fs_info->fs_mutex);
1277 1278 1279 1280 1281
		if (ret)
			break;
	}
}

C
Chris Mason 已提交
1282 1283 1284 1285
/*
 * helper function for drop_snapshot, this walks down the tree dropping ref
 * counts as it goes.
 */
1286 1287
static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
			  *root, struct btrfs_path *path, int *level)
C
Chris Mason 已提交
1288
{
1289 1290
	struct extent_buffer *next;
	struct extent_buffer *cur;
1291 1292
	u64 bytenr;
	u32 blocksize;
C
Chris Mason 已提交
1293 1294 1295
	int ret;
	u32 refs;

1296 1297
	WARN_ON(*level < 0);
	WARN_ON(*level >= BTRFS_MAX_LEVEL);
1298
	ret = lookup_extent_ref(trans, root,
1299 1300
				path->nodes[*level]->start,
				path->nodes[*level]->len, &refs);
C
Chris Mason 已提交
1301 1302 1303
	BUG_ON(ret);
	if (refs > 1)
		goto out;
1304

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

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

1316
		if (btrfs_header_level(cur) != *level)
C
Chris Mason 已提交
1317
			WARN_ON(1);
1318

1319
		if (path->slots[*level] >=
1320
		    btrfs_header_nritems(cur))
C
Chris Mason 已提交
1321
			break;
1322 1323 1324 1325 1326
		if (*level == 0) {
			ret = drop_leaf_ref(trans, root, cur);
			BUG_ON(ret);
			break;
		}
1327 1328 1329
		bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
		blocksize = btrfs_level_size(root, *level - 1);
		ret = lookup_extent_ref(trans, root, bytenr, blocksize, &refs);
1330 1331
		BUG_ON(ret);
		if (refs != 1) {
C
Chris Mason 已提交
1332
			path->slots[*level]++;
1333 1334
			ret = btrfs_free_extent(trans, root, bytenr,
						blocksize, 1);
C
Chris Mason 已提交
1335 1336 1337
			BUG_ON(ret);
			continue;
		}
1338
		next = btrfs_find_tree_block(root, bytenr, blocksize);
1339 1340
		if (!next || !btrfs_buffer_uptodate(next)) {
			free_extent_buffer(next);
1341
			mutex_unlock(&root->fs_info->fs_mutex);
1342
			next = read_tree_block(root, bytenr, blocksize);
1343 1344 1345
			mutex_lock(&root->fs_info->fs_mutex);

			/* we dropped the lock, check one more time */
1346 1347
			ret = lookup_extent_ref(trans, root, bytenr,
						blocksize, &refs);
1348 1349 1350
			BUG_ON(ret);
			if (refs != 1) {
				path->slots[*level]++;
1351
				free_extent_buffer(next);
1352
				ret = btrfs_free_extent(trans, root,
1353
							bytenr, blocksize, 1);
1354 1355 1356 1357
				BUG_ON(ret);
				continue;
			}
		}
1358
		WARN_ON(*level <= 0);
C
Chris Mason 已提交
1359
		if (path->nodes[*level-1])
1360
			free_extent_buffer(path->nodes[*level-1]);
C
Chris Mason 已提交
1361
		path->nodes[*level-1] = next;
1362
		*level = btrfs_header_level(next);
C
Chris Mason 已提交
1363 1364 1365
		path->slots[*level] = 0;
	}
out:
1366 1367
	WARN_ON(*level < 0);
	WARN_ON(*level >= BTRFS_MAX_LEVEL);
1368 1369
	ret = btrfs_free_extent(trans, root, path->nodes[*level]->start,
				path->nodes[*level]->len, 1);
1370
	free_extent_buffer(path->nodes[*level]);
C
Chris Mason 已提交
1371 1372 1373 1374 1375 1376
	path->nodes[*level] = NULL;
	*level += 1;
	BUG_ON(ret);
	return 0;
}

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

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

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

1433 1434
	path = btrfs_alloc_path();
	BUG_ON(!path);
C
Chris Mason 已提交
1435

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

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

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

1486
int btrfs_free_block_groups(struct btrfs_fs_info *info)
C
Chris Mason 已提交
1487
{
1488 1489
	u64 start;
	u64 end;
1490
	u64 ptr;
C
Chris Mason 已提交
1491 1492
	int ret;
	while(1) {
1493 1494 1495
		ret = find_first_extent_bit(&info->block_group_cache, 0,
					    &start, &end, (unsigned int)-1);
		if (ret)
C
Chris Mason 已提交
1496
			break;
1497 1498 1499
		ret = get_state_private(&info->block_group_cache, start, &ptr);
		if (!ret)
			kfree((void *)(unsigned long)ptr);
1500 1501
		clear_extent_bits(&info->block_group_cache, start,
				  end, (unsigned int)-1, GFP_NOFS);
C
Chris Mason 已提交
1502
	}
1503
	while(1) {
1504 1505 1506
		ret = find_first_extent_bit(&info->free_space_cache, 0,
					    &start, &end, EXTENT_DIRTY);
		if (ret)
1507
			break;
1508 1509
		clear_extent_dirty(&info->free_space_cache, start,
				   end, GFP_NOFS);
1510
	}
C
Chris Mason 已提交
1511 1512 1513
	return 0;
}

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

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

C
Chris Mason 已提交
1529
	root = info->extent_root;
C
Chris Mason 已提交
1530
	key.objectid = 0;
1531
	key.offset = BTRFS_BLOCK_GROUP_SIZE;
C
Chris Mason 已提交
1532 1533 1534 1535 1536 1537 1538
	btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);

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

	while(1) {
C
Chris Mason 已提交
1539
		ret = btrfs_search_slot(NULL, info->extent_root,
C
Chris Mason 已提交
1540 1541 1542 1543 1544
					&key, path, 0, 0);
		if (ret != 0) {
			err = ret;
			break;
		}
1545 1546
		leaf = path->nodes[0];
		btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
C
Chris Mason 已提交
1547 1548 1549 1550 1551
		cache = kmalloc(sizeof(*cache), GFP_NOFS);
		if (!cache) {
			err = -1;
			break;
		}
C
Chris Mason 已提交
1552

1553 1554 1555
		read_extent_buffer(leaf, &cache->item,
				   btrfs_item_ptr_offset(leaf, path->slots[0]),
				   sizeof(cache->item));
C
Chris Mason 已提交
1556
		memcpy(&cache->key, &found_key, sizeof(found_key));
1557 1558
		cache->cached = 0;

C
Chris Mason 已提交
1559 1560
		key.objectid = found_key.objectid + found_key.offset;
		btrfs_release_path(root, path);
1561

1562 1563 1564 1565
		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) {
1566
			bit = BLOCK_GROUP_DATA;
1567
			cache->data = BTRFS_BLOCK_GROUP_DATA;
1568 1569 1570
		} else {
			bit = BLOCK_GROUP_METADATA;
			cache->data = 0;
1571
		}
1572 1573 1574 1575 1576 1577

		/* 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 已提交
1578
				  (unsigned long)cache);
1579

C
Chris Mason 已提交
1580
		if (key.objectid >=
1581
		    btrfs_super_total_bytes(&info->super_copy))
C
Chris Mason 已提交
1582 1583 1584 1585 1586 1587
			break;
	}

	btrfs_free_path(path);
	return 0;
}