extent-tree.c 40.4 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
	return NULL;
}
161 162
static u64 find_search_start(struct btrfs_root *root,
			     struct btrfs_block_group_cache **cache_ret,
163 164
			     u64 search_start, int num,
			     int data, int full_scan)
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
	if (cache_miss && !cache->cached) {
		cache_block_group(root, cache);
		last = cache_miss;
		cache = btrfs_lookup_block_group(root->fs_info, last);
	}
223 224
	if (!full_scan)
		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 281
			if (used + shint->pinned <
			    div_factor(shint->key.offset, factor)) {
C
Chris Mason 已提交
282 283 284 285
				return shint;
			}
		}
	}
286 287
	if (hint && (hint->data == data ||
		     hint->data == BTRFS_BLOCK_GROUP_MIXED)) {
288
		used = btrfs_block_group_used(&hint->item);
289 290
		if (used + hint->pinned <
		    div_factor(hint->key.offset, factor)) {
291 292
			return hint;
		}
293
		last = hint->key.objectid + hint->key.offset;
294 295
		hint_last = last;
	} else {
296 297 298 299 300 301
		if (hint)
			hint_last = max(hint->key.objectid, search_start);
		else
			hint_last = search_start;

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

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

J
Jens Axboe 已提交
314
		cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
315 316 317 318 319 320 321
		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);
322
		if (used + cache->pinned < free_check) {
323 324
			found_group = cache;
			goto found;
C
Chris Mason 已提交
325
		}
326
		cond_resched();
C
Chris Mason 已提交
327
	}
328
	if (!full_search) {
C
Chris Mason 已提交
329
		last = search_start;
330 331 332
		full_search = 1;
		goto again;
	}
C
Chris Mason 已提交
333 334
	if (!data_swap) {
		data_swap = 1;
335
		bit = BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA;
C
Chris Mason 已提交
336 337 338
		last = search_start;
		goto again;
	}
C
Chris Mason 已提交
339
found:
340
	return found_group;
C
Chris Mason 已提交
341 342
}

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

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

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

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

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

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

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

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

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

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

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

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

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

}

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

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

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

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

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

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

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

		old_val = btrfs_block_group_used(&cache->item);
616
		num_bytes = min(total, cache->key.offset - byte_in_group);
C
Chris Mason 已提交
617
		if (alloc) {
C
Chris Mason 已提交
618
			if (cache->data != data &&
C
Chris Mason 已提交
619
			    old_val < (cache->key.offset >> 1)) {
620 621 622
				int bit_to_clear;
				int bit_to_set;
				cache->data = data;
C
Chris Mason 已提交
623
				if (data) {
624 625
					bit_to_clear = BLOCK_GROUP_METADATA;
					bit_to_set = BLOCK_GROUP_DATA;
626 627
					cache->item.flags &=
						~BTRFS_BLOCK_GROUP_MIXED;
C
Chris Mason 已提交
628 629 630
					cache->item.flags |=
						BTRFS_BLOCK_GROUP_DATA;
				} else {
631 632
					bit_to_clear = BLOCK_GROUP_DATA;
					bit_to_set = BLOCK_GROUP_METADATA;
633 634
					cache->item.flags &=
						~BTRFS_BLOCK_GROUP_MIXED;
C
Chris Mason 已提交
635 636 637
					cache->item.flags &=
						~BTRFS_BLOCK_GROUP_DATA;
				}
638 639 640 641 642 643
				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);
644 645 646 647 648 649 650 651
			} 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 已提交
652
			}
653
			old_val += num_bytes;
C
Chris Mason 已提交
654
		} else {
655
			old_val -= num_bytes;
656 657
			if (mark_free) {
				set_extent_dirty(&info->free_space_cache,
658
						 bytenr, bytenr + num_bytes - 1,
659
						 GFP_NOFS);
660
			}
C
Chris Mason 已提交
661
		}
C
Chris Mason 已提交
662
		btrfs_set_block_group_used(&cache->item, old_val);
663 664
		total -= num_bytes;
		bytenr += num_bytes;
C
Chris Mason 已提交
665 666 667
	}
	return 0;
}
668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698
static int update_pinned_extents(struct btrfs_root *root,
				u64 bytenr, u64 num, int pin)
{
	u64 len;
	struct btrfs_block_group_cache *cache;
	struct btrfs_fs_info *fs_info = root->fs_info;

	if (pin) {
		set_extent_dirty(&fs_info->pinned_extents,
				bytenr, bytenr + num - 1, GFP_NOFS);
	} else {
		clear_extent_dirty(&fs_info->pinned_extents,
				bytenr, bytenr + num - 1, GFP_NOFS);
	}
	while (num > 0) {
		cache = btrfs_lookup_block_group(fs_info, bytenr);
		WARN_ON(!cache);
		len = min(num, cache->key.offset -
			  (bytenr - cache->key.objectid));
		if (pin) {
			cache->pinned += len;
			fs_info->total_pinned += len;
		} else {
			cache->pinned -= len;
			fs_info->total_pinned -= len;
		}
		bytenr += len;
		num -= len;
	}
	return 0;
}
C
Chris Mason 已提交
699

700
int btrfs_copy_pinned(struct btrfs_root *root, struct extent_map_tree *copy)
C
Chris Mason 已提交
701 702
{
	u64 last = 0;
703 704 705
	u64 start;
	u64 end;
	struct extent_map_tree *pinned_extents = &root->fs_info->pinned_extents;
C
Chris Mason 已提交
706 707 708
	int ret;

	while(1) {
709 710 711
		ret = find_first_extent_bit(pinned_extents, last,
					    &start, &end, EXTENT_DIRTY);
		if (ret)
C
Chris Mason 已提交
712
			break;
713 714
		set_extent_dirty(copy, start, end, GFP_NOFS);
		last = end + 1;
C
Chris Mason 已提交
715 716 717 718 719 720
	}
	return 0;
}

int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
			       struct btrfs_root *root,
721
			       struct extent_map_tree *unpin)
722
{
723 724
	u64 start;
	u64 end;
725
	int ret;
726 727
	struct extent_map_tree *free_space_cache;
	free_space_cache = &root->fs_info->free_space_cache;
728 729

	while(1) {
730 731 732
		ret = find_first_extent_bit(unpin, 0, &start, &end,
					    EXTENT_DIRTY);
		if (ret)
733
			break;
734
		update_pinned_extents(root, start, end + 1 - start, 0);
735 736
		clear_extent_dirty(unpin, start, end, GFP_NOFS);
		set_extent_dirty(free_space_cache, start, end, GFP_NOFS);
737 738 739 740
	}
	return 0;
}

741 742
static int finish_current_insert(struct btrfs_trans_handle *trans, struct
				 btrfs_root *extent_root)
C
Chris Mason 已提交
743
{
C
Chris Mason 已提交
744
	struct btrfs_key ins;
C
Chris Mason 已提交
745
	struct btrfs_extent_item extent_item;
C
Chris Mason 已提交
746
	int ret;
747 748 749
	int err = 0;
	u64 start;
	u64 end;
750
	struct btrfs_fs_info *info = extent_root->fs_info;
C
Chris Mason 已提交
751

752
	btrfs_set_stack_extent_refs(&extent_item, 1);
753
	btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
754 755
	btrfs_set_stack_extent_owner(&extent_item,
				     extent_root->root_key.objectid);
C
Chris Mason 已提交
756

757
	while(1) {
758 759 760
		ret = find_first_extent_bit(&info->extent_ins, 0, &start,
					    &end, EXTENT_LOCKED);
		if (ret)
761 762
			break;

763 764 765 766 767 768
		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 已提交
769 770 771 772
	}
	return 0;
}

773 774
static int pin_down_bytes(struct btrfs_root *root, u64 bytenr, u32 num_bytes,
			  int pending)
C
Chris Mason 已提交
775
{
776
	int err = 0;
777
	struct extent_buffer *buf;
C
Chris Mason 已提交
778

C
Chris Mason 已提交
779
	if (!pending) {
780
		buf = btrfs_find_tree_block(root, bytenr, num_bytes);
781 782
		if (buf) {
			if (btrfs_buffer_uptodate(buf)) {
C
Chris Mason 已提交
783 784
				u64 transid =
				    root->fs_info->running_transaction->transid;
785 786
				if (btrfs_header_generation(buf) == transid) {
					free_extent_buffer(buf);
787
					return 1;
C
Chris Mason 已提交
788
				}
C
Chris Mason 已提交
789
			}
790
			free_extent_buffer(buf);
C
Chris Mason 已提交
791
		}
792
		update_pinned_extents(root, bytenr, num_bytes, 1);
C
Chris Mason 已提交
793
	} else {
794
		set_extent_bits(&root->fs_info->pending_del,
795 796
				bytenr, bytenr + num_bytes - 1,
				EXTENT_LOCKED, GFP_NOFS);
C
Chris Mason 已提交
797
	}
C
Chris Mason 已提交
798
	BUG_ON(err < 0);
C
Chris Mason 已提交
799 800 801
	return 0;
}

802
/*
803
 * remove an extent from the root, returns 0 on success
804
 */
805
static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
806
			 *root, u64 bytenr, u64 num_bytes, int pin,
807
			 int mark_free)
808
{
809
	struct btrfs_path *path;
C
Chris Mason 已提交
810
	struct btrfs_key key;
811 812
	struct btrfs_fs_info *info = root->fs_info;
	struct btrfs_root *extent_root = info->extent_root;
813
	struct extent_buffer *leaf;
814
	int ret;
C
Chris Mason 已提交
815
	struct btrfs_extent_item *ei;
C
Chris Mason 已提交
816
	u32 refs;
C
Chris Mason 已提交
817

818
	key.objectid = bytenr;
819
	btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
820
	key.offset = num_bytes;
821

822
	path = btrfs_alloc_path();
823 824
	if (!path)
		return -ENOMEM;
825

826 827 828 829
	ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
	if (ret < 0)
		return ret;
	BUG_ON(ret);
830 831 832

	leaf = path->nodes[0];
	ei = btrfs_item_ptr(leaf, path->slots[0],
C
Chris Mason 已提交
833
			    struct btrfs_extent_item);
834 835 836 837 838 839
	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 已提交
840
	if (refs == 0) {
841 842
		u64 super_used;
		u64 root_used;
C
Chris Mason 已提交
843 844

		if (pin) {
845
			ret = pin_down_bytes(root, bytenr, num_bytes, 0);
846 847 848
			if (ret > 0)
				mark_free = 1;
			BUG_ON(ret < 0);
C
Chris Mason 已提交
849 850
		}

851
		/* block accounting for super block */
852 853 854
		super_used = btrfs_super_bytes_used(&info->super_copy);
		btrfs_set_super_bytes_used(&info->super_copy,
					   super_used - num_bytes);
855 856

		/* block accounting for root item */
857
		root_used = btrfs_root_used(&root->root_item);
858
		btrfs_set_root_used(&root->root_item,
859
					   root_used - num_bytes);
860

861
		ret = btrfs_del_item(trans, extent_root, path);
862 863 864
		if (ret) {
			return ret;
		}
865
		ret = update_block_group(trans, root, bytenr, num_bytes, 0,
C
Chris Mason 已提交
866
					 mark_free, 0);
C
Chris Mason 已提交
867
		BUG_ON(ret);
868
	}
869
	btrfs_free_path(path);
870
	finish_current_insert(trans, extent_root);
871 872 873 874 875 876 877
	return ret;
}

/*
 * find all the blocks marked as pending in the radix tree and remove
 * them from the extent map
 */
878 879
static int del_pending_extents(struct btrfs_trans_handle *trans, struct
			       btrfs_root *extent_root)
880 881
{
	int ret;
C
Chris Mason 已提交
882
	int err = 0;
883 884 885 886
	u64 start;
	u64 end;
	struct extent_map_tree *pending_del;
	struct extent_map_tree *pinned_extents;
C
Chris Mason 已提交
887

888 889
	pending_del = &extent_root->fs_info->pending_del;
	pinned_extents = &extent_root->fs_info->pinned_extents;
890 891

	while(1) {
892 893 894
		ret = find_first_extent_bit(pending_del, 0, &start, &end,
					    EXTENT_LOCKED);
		if (ret)
895
			break;
896
		update_pinned_extents(extent_root, start, end + 1 - start, 1);
897 898 899 900 901 902
		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;
903
	}
C
Chris Mason 已提交
904
	return err;
905 906 907 908 909
}

/*
 * remove an extent from the root, returns 0 on success
 */
910
int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
911
		      *root, u64 bytenr, u64 num_bytes, int pin)
912
{
913
	struct btrfs_root *extent_root = root->fs_info->extent_root;
914 915
	int pending_ret;
	int ret;
916

917
	WARN_ON(num_bytes < root->sectorsize);
918
	if (root == extent_root) {
919
		pin_down_bytes(root, bytenr, num_bytes, 1);
920 921
		return 0;
	}
922
	ret = __free_extent(trans, root, bytenr, num_bytes, pin, pin == 0);
C
Chris Mason 已提交
923
	pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
924 925 926 927 928 929 930
	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
931
 * ins->flags = BTRFS_EXTENT_ITEM_KEY
932 933 934
 * ins->offset == number of blocks
 * Any available blocks before search_start are skipped.
 */
935
static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
936 937
			    *orig_root, u64 num_bytes, u64 empty_size,
			    u64 search_start, u64 search_end, u64 hint_byte,
938 939
			    struct btrfs_key *ins, u64 exclude_start,
			    u64 exclude_nr, int data)
940
{
941
	struct btrfs_path *path;
C
Chris Mason 已提交
942
	struct btrfs_key key;
943 944 945
	int ret;
	u64 hole_size = 0;
	int slot = 0;
946
	u64 last_byte = 0;
C
Chris Mason 已提交
947
	u64 orig_search_start = search_start;
948
	int start_found;
949
	struct extent_buffer *l;
950
	struct btrfs_root * root = orig_root->fs_info->extent_root;
951
	struct btrfs_fs_info *info = root->fs_info;
952
	u64 total_needed = num_bytes;
C
Chris Mason 已提交
953
	int level;
C
Chris Mason 已提交
954
	struct btrfs_block_group_cache *block_group;
C
Chris Mason 已提交
955
	int full_scan = 0;
956
	int wrapped = 0;
957
	u64 cached_start;
958

959
	WARN_ON(num_bytes < root->sectorsize);
960 961
	btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);

962 963
	level = btrfs_header_level(root->node);

964 965 966 967
	if (num_bytes >= 96 * 1024 * 1024 && hint_byte) {
		data = BTRFS_BLOCK_GROUP_MIXED;
	}

C
Chris Mason 已提交
968
	if (search_end == (u64)-1)
969 970 971
		search_end = btrfs_super_total_bytes(&info->super_copy);
	if (hint_byte) {
		block_group = btrfs_lookup_block_group(info, hint_byte);
C
Chris Mason 已提交
972
		block_group = btrfs_find_block_group(root, block_group,
973
						     hint_byte, data, 1);
C
Chris Mason 已提交
974 975 976
	} else {
		block_group = btrfs_find_block_group(root,
						     trans->block_group, 0,
977
						     data, 1);
C
Chris Mason 已提交
978 979
	}

980
	total_needed += empty_size;
981
	path = btrfs_alloc_path();
C
Chris Mason 已提交
982
check_failed:
983 984
	search_start = find_search_start(root, &block_group, search_start,
					 total_needed, data, full_scan);
985
	cached_start = search_start;
986
	btrfs_init_path(path);
987 988 989
	ins->objectid = search_start;
	ins->offset = 0;
	start_found = 0;
990
	path->reada = 2;
991

992
	ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
C
Chris Mason 已提交
993 994
	if (ret < 0)
		goto error;
C
Chris Mason 已提交
995

996
	if (path->slots[0] > 0) {
997
		path->slots[0]--;
998 999
	}

1000 1001 1002
	l = path->nodes[0];
	btrfs_item_key_to_cpu(l, &key, path->slots[0]);

1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019
	/*
	 * 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]--;
		}
	}
1020

1021
	while (1) {
1022
		l = path->nodes[0];
1023
		slot = path->slots[0];
1024
		if (slot >= btrfs_header_nritems(l)) {
1025
			ret = btrfs_next_leaf(root, path);
1026 1027
			if (ret == 0)
				continue;
C
Chris Mason 已提交
1028 1029
			if (ret < 0)
				goto error;
1030 1031 1032

			search_start = max(search_start,
					   block_group->key.objectid);
1033 1034
			if (!start_found) {
				ins->objectid = search_start;
C
Chris Mason 已提交
1035
				ins->offset = search_end - search_start;
1036 1037 1038
				start_found = 1;
				goto check_pending;
			}
1039 1040
			ins->objectid = last_byte > search_start ?
					last_byte : search_start;
C
Chris Mason 已提交
1041
			ins->offset = search_end - ins->objectid;
1042
			BUG_ON(ins->objectid >= search_end);
1043 1044
			goto check_pending;
		}
1045
		btrfs_item_key_to_cpu(l, &key, slot);
1046

1047
		if (key.objectid >= search_start && key.objectid > last_byte &&
1048
		    start_found) {
1049 1050 1051 1052 1053
			if (last_byte < search_start)
				last_byte = search_start;
			hole_size = key.objectid - last_byte;
			if (hole_size >= num_bytes) {
				ins->objectid = last_byte;
1054 1055
				ins->offset = hole_size;
				goto check_pending;
1056
			}
1057
		}
1058 1059
		if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY) {
			if (!start_found) {
1060
				last_byte = key.objectid;
1061 1062
				start_found = 1;
			}
1063
			goto next;
1064 1065
		}

1066

1067
		start_found = 1;
1068
		last_byte = key.objectid + key.offset;
1069

1070 1071
		if (!full_scan && data != BTRFS_BLOCK_GROUP_MIXED &&
		    last_byte >= block_group->key.objectid +
C
Chris Mason 已提交
1072 1073 1074
		    block_group->key.offset) {
			btrfs_release_path(root, path);
			search_start = block_group->key.objectid +
1075
				block_group->key.offset;
C
Chris Mason 已提交
1076 1077
			goto new_group;
		}
C
Chris Mason 已提交
1078
next:
1079
		path->slots[0]++;
1080
		cond_resched();
1081 1082 1083 1084 1085
	}
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
	 */
1086
	btrfs_release_path(root, path);
1087
	BUG_ON(ins->objectid < search_start);
1088

1089
	if (ins->objectid + num_bytes >= search_end)
1090 1091
		goto enospc;

1092 1093
	if (!full_scan && data != BTRFS_BLOCK_GROUP_MIXED &&
	    ins->objectid + num_bytes >= block_group->
1094 1095 1096 1097 1098
	    key.objectid + block_group->key.offset) {
		search_start = block_group->key.objectid +
			block_group->key.offset;
		goto new_group;
	}
1099
	if (test_range_bit(&info->extent_ins, ins->objectid,
1100 1101
			   ins->objectid + num_bytes -1, EXTENT_LOCKED, 0)) {
		search_start = ins->objectid + num_bytes;
1102 1103 1104
		goto new_group;
	}
	if (test_range_bit(&info->pinned_extents, ins->objectid,
1105 1106
			   ins->objectid + num_bytes -1, EXTENT_DIRTY, 0)) {
		search_start = ins->objectid + num_bytes;
1107
		goto new_group;
1108
	}
1109
	if (exclude_nr > 0 && (ins->objectid + num_bytes > exclude_start &&
1110 1111 1112 1113
	    ins->objectid < exclude_start + exclude_nr)) {
		search_start = exclude_start + exclude_nr;
		goto new_group;
	}
1114
	if (!data) {
1115
		block_group = btrfs_lookup_block_group(info, ins->objectid);
1116 1117
		if (block_group)
			trans->block_group = block_group;
1118
	}
1119
	ins->offset = num_bytes;
1120
	btrfs_free_path(path);
1121
	return 0;
C
Chris Mason 已提交
1122 1123

new_group:
1124
	if (search_start + num_bytes >= search_end) {
1125
enospc:
C
Chris Mason 已提交
1126
		search_start = orig_search_start;
1127 1128 1129 1130
		if (full_scan) {
			ret = -ENOSPC;
			goto error;
		}
1131 1132 1133
		if (wrapped) {
			if (!full_scan)
				total_needed -= empty_size;
1134
			full_scan = 1;
1135
		} else
1136
			wrapped = 1;
C
Chris Mason 已提交
1137
	}
1138
	block_group = btrfs_lookup_block_group(info, search_start);
1139
	cond_resched();
C
Chris Mason 已提交
1140 1141
	if (!full_scan)
		block_group = btrfs_find_block_group(root, block_group,
1142
						     search_start, data, 0);
C
Chris Mason 已提交
1143 1144
	goto check_failed;

C
Chris Mason 已提交
1145
error:
1146 1147
	btrfs_release_path(root, path);
	btrfs_free_path(path);
C
Chris Mason 已提交
1148
	return ret;
1149 1150 1151 1152 1153 1154 1155 1156
}
/*
 * 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.
 */
1157 1158
int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
		       struct btrfs_root *root, u64 owner,
1159
		       u64 num_bytes, u64 empty_size, u64 hint_byte,
C
Chris Mason 已提交
1160
		       u64 search_end, struct btrfs_key *ins, int data)
1161 1162 1163
{
	int ret;
	int pending_ret;
1164
	u64 super_used, root_used;
1165
	u64 search_start = 0;
1166 1167
	struct btrfs_fs_info *info = root->fs_info;
	struct btrfs_root *extent_root = info->extent_root;
C
Chris Mason 已提交
1168
	struct btrfs_extent_item extent_item;
C
Chris Mason 已提交
1169

1170 1171
	btrfs_set_stack_extent_refs(&extent_item, 1);
	btrfs_set_stack_extent_owner(&extent_item, owner);
1172

1173 1174 1175
	WARN_ON(num_bytes < root->sectorsize);
	ret = find_free_extent(trans, root, num_bytes, empty_size,
			       search_start, search_end, hint_byte, ins,
1176 1177
			       trans->alloc_exclude_start,
			       trans->alloc_exclude_nr, data);
C
Chris Mason 已提交
1178
	BUG_ON(ret);
1179 1180
	if (ret)
		return ret;
1181

1182
	/* block accounting for super block */
1183 1184
	super_used = btrfs_super_bytes_used(&info->super_copy);
	btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes);
1185

1186
	/* block accounting for root item */
1187 1188
	root_used = btrfs_root_used(&root->root_item);
	btrfs_set_root_used(&root->root_item, root_used + num_bytes);
1189

1190 1191 1192 1193
	clear_extent_dirty(&root->fs_info->free_space_cache,
			   ins->objectid, ins->objectid + ins->offset - 1,
			   GFP_NOFS);

1194
	if (root == extent_root) {
1195 1196 1197
		set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
				ins->objectid + ins->offset - 1,
				EXTENT_LOCKED, GFP_NOFS);
1198
		WARN_ON(data == 1);
1199 1200 1201 1202 1203 1204
		goto update_block;
	}

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

1208 1209 1210
	trans->alloc_exclude_start = 0;
	trans->alloc_exclude_nr = 0;

C
Chris Mason 已提交
1211
	BUG_ON(ret);
1212
	finish_current_insert(trans, extent_root);
C
Chris Mason 已提交
1213
	pending_ret = del_pending_extents(trans, extent_root);
1214

1215
	if (ret) {
C
Chris Mason 已提交
1216
		return ret;
1217 1218
	}
	if (pending_ret) {
C
Chris Mason 已提交
1219
		return pending_ret;
1220
	}
1221 1222

update_block:
C
Chris Mason 已提交
1223 1224
	ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0,
				 data);
C
Chris Mason 已提交
1225
	BUG_ON(ret);
C
Chris Mason 已提交
1226
	return 0;
1227 1228 1229 1230 1231 1232
}

/*
 * helper function to allocate a block for a given tree
 * returns the tree buffer or NULL.
 */
1233
struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
1234 1235
					     struct btrfs_root *root,
					     u32 blocksize, u64 hint,
1236
					     u64 empty_size)
1237
{
C
Chris Mason 已提交
1238
	struct btrfs_key ins;
1239
	int ret;
1240
	struct extent_buffer *buf;
1241

1242
	ret = btrfs_alloc_extent(trans, root, root->root_key.objectid,
1243 1244
				 blocksize, empty_size, hint,
				 (u64)-1, &ins, 0);
1245
	if (ret) {
1246 1247
		BUG_ON(ret > 0);
		return ERR_PTR(ret);
1248
	}
1249
	buf = btrfs_find_create_tree_block(root, ins.objectid, blocksize);
1250
	if (!buf) {
1251
		btrfs_free_extent(trans, root, ins.objectid, blocksize, 0);
1252 1253
		return ERR_PTR(-ENOMEM);
	}
1254 1255 1256
	btrfs_set_buffer_uptodate(buf);
	set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
			 buf->start + buf->len - 1, GFP_NOFS);
1257 1258 1259 1260
	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;
1261
	btrfs_set_buffer_defrag(buf);
1262
	trans->blocks_used++;
1263 1264
	return buf;
}
1265

1266
static int drop_leaf_ref(struct btrfs_trans_handle *trans,
1267
			 struct btrfs_root *root, struct extent_buffer *leaf)
1268
{
1269
	struct btrfs_key key;
1270 1271 1272 1273 1274
	struct btrfs_file_extent_item *fi;
	int i;
	int nritems;
	int ret;

1275 1276
	BUG_ON(!btrfs_is_leaf(leaf));
	nritems = btrfs_header_nritems(leaf);
1277
	for (i = 0; i < nritems; i++) {
1278
		u64 disk_bytenr;
1279 1280 1281

		btrfs_item_key_to_cpu(leaf, &key, i);
		if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1282 1283
			continue;
		fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
1284 1285
		if (btrfs_file_extent_type(leaf, fi) ==
		    BTRFS_FILE_EXTENT_INLINE)
1286
			continue;
1287 1288 1289 1290
		/*
		 * FIXME make sure to insert a trans record that
		 * repeats the snapshot del on crash
		 */
1291 1292
		disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
		if (disk_bytenr == 0)
C
Chris Mason 已提交
1293
			continue;
1294 1295
		ret = btrfs_free_extent(trans, root, disk_bytenr,
				btrfs_file_extent_disk_num_bytes(leaf, fi), 0);
1296 1297 1298 1299 1300
		BUG_ON(ret);
	}
	return 0;
}

1301
static void reada_walk_down(struct btrfs_root *root,
1302
			    struct extent_buffer *node)
1303 1304 1305
{
	int i;
	u32 nritems;
1306
	u64 bytenr;
1307 1308
	int ret;
	u32 refs;
1309 1310
	int level;
	u32 blocksize;
1311

1312
	nritems = btrfs_header_nritems(node);
1313
	level = btrfs_header_level(node);
1314
	for (i = 0; i < nritems; i++) {
1315 1316 1317
		bytenr = btrfs_node_blockptr(node, i);
		blocksize = btrfs_level_size(root, level - 1);
		ret = lookup_extent_ref(NULL, root, bytenr, blocksize, &refs);
1318 1319 1320
		BUG_ON(ret);
		if (refs != 1)
			continue;
1321
		mutex_unlock(&root->fs_info->fs_mutex);
1322
		ret = readahead_tree_block(root, bytenr, blocksize);
1323 1324
		cond_resched();
		mutex_lock(&root->fs_info->fs_mutex);
1325 1326 1327 1328 1329
		if (ret)
			break;
	}
}

C
Chris Mason 已提交
1330 1331 1332 1333
/*
 * helper function for drop_snapshot, this walks down the tree dropping ref
 * counts as it goes.
 */
1334 1335
static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
			  *root, struct btrfs_path *path, int *level)
C
Chris Mason 已提交
1336
{
1337 1338
	struct extent_buffer *next;
	struct extent_buffer *cur;
1339 1340
	u64 bytenr;
	u32 blocksize;
C
Chris Mason 已提交
1341 1342 1343
	int ret;
	u32 refs;

1344 1345
	WARN_ON(*level < 0);
	WARN_ON(*level >= BTRFS_MAX_LEVEL);
1346
	ret = lookup_extent_ref(trans, root,
1347 1348
				path->nodes[*level]->start,
				path->nodes[*level]->len, &refs);
C
Chris Mason 已提交
1349 1350 1351
	BUG_ON(ret);
	if (refs > 1)
		goto out;
1352

C
Chris Mason 已提交
1353 1354 1355
	/*
	 * walk down to the last node level and free all the leaves
	 */
1356
	while(*level >= 0) {
1357 1358
		WARN_ON(*level < 0);
		WARN_ON(*level >= BTRFS_MAX_LEVEL);
C
Chris Mason 已提交
1359
		cur = path->nodes[*level];
1360 1361

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

1364
		if (btrfs_header_level(cur) != *level)
C
Chris Mason 已提交
1365
			WARN_ON(1);
1366

1367
		if (path->slots[*level] >=
1368
		    btrfs_header_nritems(cur))
C
Chris Mason 已提交
1369
			break;
1370 1371 1372 1373 1374
		if (*level == 0) {
			ret = drop_leaf_ref(trans, root, cur);
			BUG_ON(ret);
			break;
		}
1375 1376 1377
		bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
		blocksize = btrfs_level_size(root, *level - 1);
		ret = lookup_extent_ref(trans, root, bytenr, blocksize, &refs);
1378 1379
		BUG_ON(ret);
		if (refs != 1) {
C
Chris Mason 已提交
1380
			path->slots[*level]++;
1381 1382
			ret = btrfs_free_extent(trans, root, bytenr,
						blocksize, 1);
C
Chris Mason 已提交
1383 1384 1385
			BUG_ON(ret);
			continue;
		}
1386
		next = btrfs_find_tree_block(root, bytenr, blocksize);
1387 1388
		if (!next || !btrfs_buffer_uptodate(next)) {
			free_extent_buffer(next);
1389
			mutex_unlock(&root->fs_info->fs_mutex);
1390
			next = read_tree_block(root, bytenr, blocksize);
1391 1392 1393
			mutex_lock(&root->fs_info->fs_mutex);

			/* we dropped the lock, check one more time */
1394 1395
			ret = lookup_extent_ref(trans, root, bytenr,
						blocksize, &refs);
1396 1397 1398
			BUG_ON(ret);
			if (refs != 1) {
				path->slots[*level]++;
1399
				free_extent_buffer(next);
1400
				ret = btrfs_free_extent(trans, root,
1401
							bytenr, blocksize, 1);
1402 1403 1404 1405
				BUG_ON(ret);
				continue;
			}
		}
1406
		WARN_ON(*level <= 0);
C
Chris Mason 已提交
1407
		if (path->nodes[*level-1])
1408
			free_extent_buffer(path->nodes[*level-1]);
C
Chris Mason 已提交
1409
		path->nodes[*level-1] = next;
1410
		*level = btrfs_header_level(next);
C
Chris Mason 已提交
1411 1412 1413
		path->slots[*level] = 0;
	}
out:
1414 1415
	WARN_ON(*level < 0);
	WARN_ON(*level >= BTRFS_MAX_LEVEL);
1416 1417
	ret = btrfs_free_extent(trans, root, path->nodes[*level]->start,
				path->nodes[*level]->len, 1);
1418
	free_extent_buffer(path->nodes[*level]);
C
Chris Mason 已提交
1419 1420 1421 1422 1423 1424
	path->nodes[*level] = NULL;
	*level += 1;
	BUG_ON(ret);
	return 0;
}

C
Chris Mason 已提交
1425 1426 1427 1428 1429
/*
 * 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
 */
1430 1431
static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root
			*root, struct btrfs_path *path, int *level)
C
Chris Mason 已提交
1432 1433 1434 1435
{
	int i;
	int slot;
	int ret;
1436 1437
	struct btrfs_root_item *root_item = &root->root_item;

C
Chris Mason 已提交
1438
	for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
C
Chris Mason 已提交
1439
		slot = path->slots[i];
1440 1441 1442 1443
		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 已提交
1444 1445
			path->slots[i]++;
			*level = i;
1446
			WARN_ON(*level == 0);
1447
			btrfs_node_key(node, &disk_key, path->slots[i]);
1448
			memcpy(&root_item->drop_progress,
1449
			       &disk_key, sizeof(disk_key));
1450
			root_item->drop_level = i;
C
Chris Mason 已提交
1451 1452
			return 0;
		} else {
1453
			ret = btrfs_free_extent(trans, root,
1454 1455
						path->nodes[*level]->start,
						path->nodes[*level]->len, 1);
1456
			BUG_ON(ret);
1457
			free_extent_buffer(path->nodes[*level]);
C
Chris Mason 已提交
1458
			path->nodes[*level] = NULL;
C
Chris Mason 已提交
1459 1460 1461 1462 1463 1464
			*level = i + 1;
		}
	}
	return 1;
}

C
Chris Mason 已提交
1465 1466 1467 1468 1469
/*
 * 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.
 */
1470
int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
1471
			*root)
C
Chris Mason 已提交
1472
{
1473
	int ret = 0;
C
Chris Mason 已提交
1474
	int wret;
C
Chris Mason 已提交
1475
	int level;
1476
	struct btrfs_path *path;
C
Chris Mason 已提交
1477 1478
	int i;
	int orig_level;
1479
	struct btrfs_root_item *root_item = &root->root_item;
C
Chris Mason 已提交
1480

1481 1482
	path = btrfs_alloc_path();
	BUG_ON(!path);
C
Chris Mason 已提交
1483

1484
	level = btrfs_header_level(root->node);
C
Chris Mason 已提交
1485
	orig_level = level;
1486 1487
	if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
		path->nodes[level] = root->node;
1488
		extent_buffer_get(root->node);
1489 1490 1491
		path->slots[level] = 0;
	} else {
		struct btrfs_key key;
1492 1493
		struct btrfs_disk_key found_key;
		struct extent_buffer *node;
1494

1495
		btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
1496 1497
		level = root_item->drop_level;
		path->lowest_level = level;
1498
		wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1499
		if (wret < 0) {
1500 1501 1502
			ret = wret;
			goto out;
		}
1503 1504 1505 1506
		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)));
1507
	}
C
Chris Mason 已提交
1508
	while(1) {
1509
		wret = walk_down_tree(trans, root, path, &level);
C
Chris Mason 已提交
1510
		if (wret > 0)
C
Chris Mason 已提交
1511
			break;
C
Chris Mason 已提交
1512 1513 1514
		if (wret < 0)
			ret = wret;

1515
		wret = walk_up_tree(trans, root, path, &level);
C
Chris Mason 已提交
1516
		if (wret > 0)
C
Chris Mason 已提交
1517
			break;
C
Chris Mason 已提交
1518 1519
		if (wret < 0)
			ret = wret;
1520 1521
		ret = -EAGAIN;
		break;
C
Chris Mason 已提交
1522
	}
C
Chris Mason 已提交
1523
	for (i = 0; i <= orig_level; i++) {
1524
		if (path->nodes[i]) {
1525
			free_extent_buffer(path->nodes[i]);
1526
			path->nodes[i] = NULL;
C
Chris Mason 已提交
1527
		}
C
Chris Mason 已提交
1528
	}
1529
out:
1530
	btrfs_free_path(path);
C
Chris Mason 已提交
1531
	return ret;
C
Chris Mason 已提交
1532
}
C
Chris Mason 已提交
1533

1534
int btrfs_free_block_groups(struct btrfs_fs_info *info)
C
Chris Mason 已提交
1535
{
1536 1537
	u64 start;
	u64 end;
1538
	u64 ptr;
C
Chris Mason 已提交
1539 1540
	int ret;
	while(1) {
1541 1542 1543
		ret = find_first_extent_bit(&info->block_group_cache, 0,
					    &start, &end, (unsigned int)-1);
		if (ret)
C
Chris Mason 已提交
1544
			break;
1545 1546 1547
		ret = get_state_private(&info->block_group_cache, start, &ptr);
		if (!ret)
			kfree((void *)(unsigned long)ptr);
1548 1549
		clear_extent_bits(&info->block_group_cache, start,
				  end, (unsigned int)-1, GFP_NOFS);
C
Chris Mason 已提交
1550
	}
1551
	while(1) {
1552 1553 1554
		ret = find_first_extent_bit(&info->free_space_cache, 0,
					    &start, &end, EXTENT_DIRTY);
		if (ret)
1555
			break;
1556 1557
		clear_extent_dirty(&info->free_space_cache, start,
				   end, GFP_NOFS);
1558
	}
C
Chris Mason 已提交
1559 1560 1561
	return 0;
}

C
Chris Mason 已提交
1562 1563 1564 1565 1566
int btrfs_read_block_groups(struct btrfs_root *root)
{
	struct btrfs_path *path;
	int ret;
	int err = 0;
1567
	int bit;
C
Chris Mason 已提交
1568
	struct btrfs_block_group_cache *cache;
C
Chris Mason 已提交
1569
	struct btrfs_fs_info *info = root->fs_info;
1570
	struct extent_map_tree *block_group_cache;
C
Chris Mason 已提交
1571 1572
	struct btrfs_key key;
	struct btrfs_key found_key;
1573
	struct extent_buffer *leaf;
1574 1575

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

C
Chris Mason 已提交
1577
	root = info->extent_root;
C
Chris Mason 已提交
1578
	key.objectid = 0;
1579
	key.offset = BTRFS_BLOCK_GROUP_SIZE;
C
Chris Mason 已提交
1580 1581 1582 1583 1584 1585 1586
	btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);

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

	while(1) {
C
Chris Mason 已提交
1587
		ret = btrfs_search_slot(NULL, info->extent_root,
C
Chris Mason 已提交
1588 1589 1590 1591 1592
					&key, path, 0, 0);
		if (ret != 0) {
			err = ret;
			break;
		}
1593 1594
		leaf = path->nodes[0];
		btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
C
Chris Mason 已提交
1595 1596 1597 1598 1599
		cache = kmalloc(sizeof(*cache), GFP_NOFS);
		if (!cache) {
			err = -1;
			break;
		}
C
Chris Mason 已提交
1600

1601 1602 1603
		read_extent_buffer(leaf, &cache->item,
				   btrfs_item_ptr_offset(leaf, path->slots[0]),
				   sizeof(cache->item));
C
Chris Mason 已提交
1604
		memcpy(&cache->key, &found_key, sizeof(found_key));
1605
		cache->cached = 0;
1606
		cache->pinned = 0;
C
Chris Mason 已提交
1607 1608
		key.objectid = found_key.objectid + found_key.offset;
		btrfs_release_path(root, path);
1609

1610 1611 1612 1613
		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) {
1614
			bit = BLOCK_GROUP_DATA;
1615
			cache->data = BTRFS_BLOCK_GROUP_DATA;
1616 1617 1618
		} else {
			bit = BLOCK_GROUP_METADATA;
			cache->data = 0;
1619
		}
1620 1621 1622 1623 1624 1625

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

C
Chris Mason 已提交
1628
		if (key.objectid >=
1629
		    btrfs_super_total_bytes(&info->super_copy))
C
Chris Mason 已提交
1630 1631 1632 1633 1634 1635
			break;
	}

	btrfs_free_path(path);
	return 0;
}