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

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

25 26
static int finish_current_insert(struct btrfs_trans_handle *trans, struct
				 btrfs_root *extent_root);
C
Chris Mason 已提交
27 28
static int del_pending_extents(struct btrfs_trans_handle *trans, struct
			       btrfs_root *extent_root);
29

30 31 32 33 34 35
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;
36
	struct extent_buffer *leaf;
37
	struct extent_map_tree *free_space_cache;
38 39 40
	int slot;
	u64 last = 0;
	u64 hole_size;
41
	u64 first_free;
42 43 44
	int found = 0;

	root = root->fs_info->extent_root;
45
	free_space_cache = &root->fs_info->free_space_cache;
46 47 48

	if (block_group->cached)
		return 0;
49

50 51 52
	path = btrfs_alloc_path();
	if (!path)
		return -ENOMEM;
53

54
	path->reada = 2;
55
	first_free = block_group->key.objectid;
56 57
	key.objectid = block_group->key.objectid;
	key.offset = 0;
58

59 60
	btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
	ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
61

62 63
	if (ret < 0)
		return ret;
64

65 66
	if (ret && path->slots[0] > 0)
		path->slots[0]--;
67

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

82
		btrfs_item_key_to_cpu(leaf, &key, slot);
83 84 85 86 87 88
		if (key.objectid < block_group->key.objectid) {
			if (key.objectid + key.offset > first_free)
				first_free = key.objectid + key.offset;
			goto next;
		}

89 90 91 92
		if (key.objectid >= block_group->key.objectid +
		    block_group->key.offset) {
			break;
		}
93

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

111 112 113 114 115 116
	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;
117 118
		set_extent_dirty(free_space_cache, last,
				 last + hole_size - 1, GFP_NOFS);
119
	}
120
	block_group->cached = 1;
121
err:
122 123 124 125
	btrfs_free_path(path);
	return 0;
}

126 127 128
struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
							 btrfs_fs_info *info,
							 u64 blocknr)
C
Chris Mason 已提交
129 130 131 132 133 134 135 136
{
	struct btrfs_block_group_cache *block_group;
	int ret;

	ret = radix_tree_gang_lookup(&info->block_group_radix,
				     (void **)&block_group,
				     blocknr, 1);
	if (ret) {
C
Chris Mason 已提交
137
		if (block_group->key.objectid <= blocknr && blocknr <=
C
Chris Mason 已提交
138 139 140 141 142 143 144
		    block_group->key.objectid + block_group->key.offset)
			return block_group;
	}
	ret = radix_tree_gang_lookup(&info->block_group_data_radix,
				     (void **)&block_group,
				     blocknr, 1);
	if (ret) {
C
Chris Mason 已提交
145
		if (block_group->key.objectid <= blocknr && blocknr <=
C
Chris Mason 已提交
146 147 148 149 150 151
		    block_group->key.objectid + block_group->key.offset)
			return block_group;
	}
	return NULL;
}

152 153
static u64 find_search_start(struct btrfs_root *root,
			     struct btrfs_block_group_cache **cache_ret,
154
			     u64 search_start, int num, int data)
155 156 157 158
{
	int ret;
	struct btrfs_block_group_cache *cache = *cache_ret;
	u64 last = max(search_start, cache->key.objectid);
159 160
	u64 start = 0;
	u64 end = 0;
161 162

again:
163 164 165
	ret = cache_block_group(root, cache);
	if (ret)
		goto out;
166
	while(1) {
167 168 169
		ret = find_first_extent_bit(&root->fs_info->free_space_cache,
					    last, &start, &end, EXTENT_DIRTY);
		if (ret)
170
			goto out;
171 172 173 174 175 176

		start = max(last, start);
		last = end + 1;
		if (end + 1 - start < num)
			continue;
		if (start + num > cache->key.objectid + cache->key.offset)
177
			goto new_group;
178
		return start;
179 180 181 182 183
	}
out:
	return max(cache->last_alloc, search_start);

new_group:
184 185
	cache = btrfs_lookup_block_group(root->fs_info,
					 last + cache->key.offset - 1);
186 187 188 189
	if (!cache) {
		return max((*cache_ret)->last_alloc, search_start);
	}
	cache = btrfs_find_block_group(root, cache,
190
				       last + cache->key.offset - 1, data, 0);
191 192 193 194
	*cache_ret = cache;
	goto again;
}

C
Chris Mason 已提交
195 196 197 198 199 200 201
static u64 div_factor(u64 num, int factor)
{
	num *= factor;
	do_div(num, 10);
	return num;
}

202 203
struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
						 struct btrfs_block_group_cache
C
Chris Mason 已提交
204
						 *hint, u64 search_start,
205
						 int data, int owner)
C
Chris Mason 已提交
206 207
{
	struct btrfs_block_group_cache *cache[8];
208
	struct btrfs_block_group_cache *found_group = NULL;
C
Chris Mason 已提交
209
	struct btrfs_fs_info *info = root->fs_info;
C
Chris Mason 已提交
210
	struct radix_tree_root *radix;
C
Chris Mason 已提交
211
	struct radix_tree_root *swap_radix;
C
Chris Mason 已提交
212
	u64 used;
213 214
	u64 last = 0;
	u64 hint_last;
C
Chris Mason 已提交
215 216
	int i;
	int ret;
217
	int full_search = 0;
218
	int factor = 8;
C
Chris Mason 已提交
219
	int data_swap = 0;
220 221 222

	if (!owner)
		factor = 5;
C
Chris Mason 已提交
223

C
Chris Mason 已提交
224
	if (data) {
C
Chris Mason 已提交
225
		radix = &info->block_group_data_radix;
C
Chris Mason 已提交
226 227
		swap_radix = &info->block_group_radix;
	} else {
C
Chris Mason 已提交
228
		radix = &info->block_group_radix;
C
Chris Mason 已提交
229 230
		swap_radix = &info->block_group_data_radix;
	}
C
Chris Mason 已提交
231 232 233

	if (search_start) {
		struct btrfs_block_group_cache *shint;
234
		shint = btrfs_lookup_block_group(info, search_start);
235
		if (shint && shint->data == data) {
C
Chris Mason 已提交
236 237
			used = btrfs_block_group_used(&shint->item);
			if (used + shint->pinned <
C
Chris Mason 已提交
238
			    div_factor(shint->key.offset, factor)) {
C
Chris Mason 已提交
239 240 241 242 243
				return shint;
			}
		}
	}
	if (hint && hint->data == data) {
244
		used = btrfs_block_group_used(&hint->item);
C
Chris Mason 已提交
245 246
		if (used + hint->pinned <
		    div_factor(hint->key.offset, factor)) {
247 248
			return hint;
		}
C
Chris Mason 已提交
249
		if (used >= div_factor(hint->key.offset, 8)) {
C
Chris Mason 已提交
250 251 252 253 254
			radix_tree_tag_clear(radix,
					     hint->key.objectid +
					     hint->key.offset - 1,
					     BTRFS_BLOCK_GROUP_AVAIL);
		}
255
		last = hint->key.offset * 3;
C
Chris Mason 已提交
256
		if (hint->key.objectid >= last)
257 258
			last = max(search_start + hint->key.offset - 1,
				   hint->key.objectid - last);
C
Chris Mason 已提交
259 260
		else
			last = hint->key.objectid + hint->key.offset;
261 262
		hint_last = last;
	} else {
263 264 265 266 267 268
		if (hint)
			hint_last = max(hint->key.objectid, search_start);
		else
			hint_last = search_start;

		last = hint_last;
269
	}
C
Chris Mason 已提交
270
	while(1) {
C
Chris Mason 已提交
271
		ret = radix_tree_gang_lookup_tag(radix, (void **)cache,
C
Chris Mason 已提交
272
						 last, ARRAY_SIZE(cache),
273
						 BTRFS_BLOCK_GROUP_AVAIL);
C
Chris Mason 已提交
274 275 276
		if (!ret)
			break;
		for (i = 0; i < ret; i++) {
C
Chris Mason 已提交
277 278
			last = cache[i]->key.objectid +
				cache[i]->key.offset;
C
Chris Mason 已提交
279
			used = btrfs_block_group_used(&cache[i]->item);
C
Chris Mason 已提交
280
			if (used + cache[i]->pinned <
C
Chris Mason 已提交
281
			    div_factor(cache[i]->key.offset, factor)) {
282 283
				found_group = cache[i];
				goto found;
C
Chris Mason 已提交
284
			}
C
Chris Mason 已提交
285
			if (used >= div_factor(cache[i]->key.offset, 8)) {
C
Chris Mason 已提交
286 287 288 289 290
				radix_tree_tag_clear(radix,
						     cache[i]->key.objectid +
						     cache[i]->key.offset - 1,
						     BTRFS_BLOCK_GROUP_AVAIL);
			}
C
Chris Mason 已提交
291
		}
292
		cond_resched();
C
Chris Mason 已提交
293
	}
294 295
	last = hint_last;
again:
C
Chris Mason 已提交
296
	while(1) {
C
Chris Mason 已提交
297 298
		ret = radix_tree_gang_lookup(radix, (void **)cache,
					     last, ARRAY_SIZE(cache));
C
Chris Mason 已提交
299 300 301
		if (!ret)
			break;
		for (i = 0; i < ret; i++) {
C
Chris Mason 已提交
302 303
			last = cache[i]->key.objectid +
				cache[i]->key.offset;
C
Chris Mason 已提交
304
			used = btrfs_block_group_used(&cache[i]->item);
C
Chris Mason 已提交
305
			if (used + cache[i]->pinned < cache[i]->key.offset) {
306 307
				found_group = cache[i];
				goto found;
C
Chris Mason 已提交
308
			}
C
Chris Mason 已提交
309 310 311 312 313 314
			if (used >= cache[i]->key.offset) {
				radix_tree_tag_clear(radix,
						     cache[i]->key.objectid +
						     cache[i]->key.offset - 1,
						     BTRFS_BLOCK_GROUP_AVAIL);
			}
C
Chris Mason 已提交
315
		}
316
		cond_resched();
C
Chris Mason 已提交
317
	}
318
	if (!full_search) {
C
Chris Mason 已提交
319
		last = search_start;
320 321 322
		full_search = 1;
		goto again;
	}
C
Chris Mason 已提交
323 324 325 326 327 328 329 330
	if (!data_swap) {
		struct radix_tree_root *tmp = radix;
		data_swap = 1;
		radix = swap_radix;
		swap_radix = tmp;
		last = search_start;
		goto again;
	}
331
	if (!found_group) {
C
Chris Mason 已提交
332
		ret = radix_tree_gang_lookup(radix,
333
					     (void **)&found_group, 0, 1);
C
Chris Mason 已提交
334 335 336 337 338
		if (ret == 0) {
			ret = radix_tree_gang_lookup(swap_radix,
						     (void **)&found_group,
						     0, 1);
		}
339 340
		BUG_ON(ret != 1);
	}
C
Chris Mason 已提交
341
found:
342
	return found_group;
C
Chris Mason 已提交
343 344
}

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

356
	path = btrfs_alloc_path();
357 358
	if (!path)
		return -ENOMEM;
359

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

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

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

	path = btrfs_alloc_path();
403
	key.objectid = blocknr;
404
	key.offset = num_blocks;
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 412
	if (ret != 0) {
		btrfs_print_leaf(root, path->nodes[0]);
		printk("failed to find block number %Lu\n", blocknr);
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,
				    extent_buffer_blocknr(root->node), 1);
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 blocknr;
434 435
	u32 nritems;
	struct btrfs_key key;
436
	struct btrfs_file_extent_item *fi;
C
Chris Mason 已提交
437
	int i;
438 439
	int leaf;
	int ret;
440 441
	int faili;
	int err;
442

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

	leaf = btrfs_is_leaf(buf);
	nritems = btrfs_header_nritems(buf);
	for (i = 0; i < nritems; i++) {
449
		if (leaf) {
C
Chris Mason 已提交
450
			u64 disk_blocknr;
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
			disk_blocknr = btrfs_file_extent_disk_blocknr(buf, fi);
C
Chris Mason 已提交
460 461 462
			if (disk_blocknr == 0)
				continue;
			ret = btrfs_inc_extent_ref(trans, root, disk_blocknr,
463
				    btrfs_file_extent_disk_num_blocks(buf, fi));
464 465 466 467
			if (ret) {
				faili = i;
				goto fail;
			}
468
		} else {
469
			blocknr = btrfs_node_blockptr(buf, i);
C
Chris Mason 已提交
470
			ret = btrfs_inc_extent_ref(trans, root, blocknr, 1);
471 472 473 474
			if (ret) {
				faili = i;
				goto fail;
			}
475
		}
C
Chris Mason 已提交
476 477
	}
	return 0;
478
fail:
C
Chris Mason 已提交
479
	WARN_ON(1);
480 481 482
	for (i =0; i < faili; i++) {
		if (leaf) {
			u64 disk_blocknr;
483 484
			btrfs_item_key_to_cpu(buf, &key, i);
			if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
485
				continue;
486
			fi = btrfs_item_ptr(buf, i,
487
					    struct btrfs_file_extent_item);
488
			if (btrfs_file_extent_type(buf, fi) ==
489 490
			    BTRFS_FILE_EXTENT_INLINE)
				continue;
491
			disk_blocknr = btrfs_file_extent_disk_blocknr(buf, fi);
492 493 494
			if (disk_blocknr == 0)
				continue;
			err = btrfs_free_extent(trans, root, disk_blocknr,
495 496
				    btrfs_file_extent_disk_num_blocks(buf,
								      fi), 0);
497 498
			BUG_ON(err);
		} else {
499
			blocknr = btrfs_node_blockptr(buf, i);
500 501 502 503 504
			err = btrfs_free_extent(trans, root, blocknr, 1, 0);
			BUG_ON(err);
		}
	}
	return ret;
C
Chris Mason 已提交
505 506
}

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

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

	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 已提交
527
	btrfs_release_path(extent_root, path);
528
fail:
C
Chris Mason 已提交
529 530 531 532 533 534
	finish_current_insert(trans, extent_root);
	pending_ret = del_pending_extents(trans, extent_root);
	if (ret)
		return ret;
	if (pending_ret)
		return pending_ret;
C
Chris Mason 已提交
535 536
	if (cache->data)
		cache->last_alloc = cache->first_free;
C
Chris Mason 已提交
537 538 539 540
	return 0;

}

C
Chris Mason 已提交
541 542 543
static int write_dirty_block_radix(struct btrfs_trans_handle *trans,
				   struct btrfs_root *root,
				   struct radix_tree_root *radix)
C
Chris Mason 已提交
544 545 546 547 548 549 550
{
	struct btrfs_block_group_cache *cache[8];
	int ret;
	int err = 0;
	int werr = 0;
	int i;
	struct btrfs_path *path;
551
	unsigned long off = 0;
C
Chris Mason 已提交
552 553 554 555 556 557 558

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

	while(1) {
		ret = radix_tree_gang_lookup_tag(radix, (void **)cache,
559
						 off, ARRAY_SIZE(cache),
C
Chris Mason 已提交
560 561 562 563 564 565
						 BTRFS_BLOCK_GROUP_DIRTY);
		if (!ret)
			break;
		for (i = 0; i < ret; i++) {
			err = write_one_cache_group(trans, root,
						    path, cache[i]);
566 567 568 569 570 571
			/*
			 * 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) {
C
Chris Mason 已提交
572
				werr = err;
573 574 575 576 577 578 579 580
				off = cache[i]->key.objectid +
					cache[i]->key.offset;
				continue;
			}

			radix_tree_tag_clear(radix, cache[i]->key.objectid +
					     cache[i]->key.offset - 1,
					     BTRFS_BLOCK_GROUP_DIRTY);
C
Chris Mason 已提交
581 582 583 584 585 586
		}
	}
	btrfs_free_path(path);
	return werr;
}

C
Chris Mason 已提交
587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602
int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
				   struct btrfs_root *root)
{
	int ret;
	int ret2;
	ret = write_dirty_block_radix(trans, root,
				      &root->fs_info->block_group_radix);
	ret2 = write_dirty_block_radix(trans, root,
				      &root->fs_info->block_group_data_radix);
	if (ret)
		return ret;
	if (ret2)
		return ret2;
	return 0;
}

C
Chris Mason 已提交
603 604
static int update_block_group(struct btrfs_trans_handle *trans,
			      struct btrfs_root *root,
C
Chris Mason 已提交
605 606
			      u64 blocknr, u64 num, int alloc, int mark_free,
			      int data)
C
Chris Mason 已提交
607 608 609 610 611 612
{
	struct btrfs_block_group_cache *cache;
	struct btrfs_fs_info *info = root->fs_info;
	u64 total = num;
	u64 old_val;
	u64 block_in_group;
C
Chris Mason 已提交
613
	int ret;
C
Chris Mason 已提交
614

C
Chris Mason 已提交
615
	while(total) {
616
		cache = btrfs_lookup_block_group(info, blocknr);
C
Chris Mason 已提交
617
		if (!cache) {
C
Chris Mason 已提交
618
			return -1;
C
Chris Mason 已提交
619
		}
C
Chris Mason 已提交
620 621
		block_in_group = blocknr - cache->key.objectid;
		WARN_ON(block_in_group > cache->key.offset);
C
Chris Mason 已提交
622
		radix_tree_tag_set(cache->radix, cache->key.objectid +
C
Chris Mason 已提交
623
				   cache->key.offset - 1,
C
Chris Mason 已提交
624 625 626 627
				   BTRFS_BLOCK_GROUP_DIRTY);

		old_val = btrfs_block_group_used(&cache->item);
		num = min(total, cache->key.offset - block_in_group);
C
Chris Mason 已提交
628 629 630
		if (alloc) {
			if (blocknr > cache->last_alloc)
				cache->last_alloc = blocknr;
C
Chris Mason 已提交
631
			if (cache->data != data &&
C
Chris Mason 已提交
632
			    old_val < (cache->key.offset >> 1)) {
C
Chris Mason 已提交
633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653
				cache->data = data;
				radix_tree_delete(cache->radix,
						  cache->key.objectid +
						  cache->key.offset - 1);

				if (data) {
					cache->radix =
						&info->block_group_data_radix;
					cache->item.flags |=
						BTRFS_BLOCK_GROUP_DATA;
				} else {
					cache->radix = &info->block_group_radix;
					cache->item.flags &=
						~BTRFS_BLOCK_GROUP_DATA;
				}
				ret = radix_tree_insert(cache->radix,
							cache->key.objectid +
							cache->key.offset - 1,
							(void *)cache);
			}
			old_val += num;
C
Chris Mason 已提交
654
		} else {
C
Chris Mason 已提交
655
			old_val -= num;
C
Chris Mason 已提交
656 657
			if (blocknr < cache->first_free)
				cache->first_free = blocknr;
658 659 660 661
			if (mark_free) {
				set_extent_dirty(&info->free_space_cache,
						 blocknr, blocknr + num - 1,
						 GFP_NOFS);
662
			}
C
Chris Mason 已提交
663 664
			if (old_val < (cache->key.offset >> 1) &&
			    old_val + num >= (cache->key.offset >> 1)) {
665 666 667 668 669
				radix_tree_tag_set(cache->radix,
						   cache->key.objectid +
						   cache->key.offset - 1,
						   BTRFS_BLOCK_GROUP_AVAIL);
			}
C
Chris Mason 已提交
670
		}
C
Chris Mason 已提交
671
		btrfs_set_block_group_used(&cache->item, old_val);
672 673
		total -= num;
		blocknr += num;
C
Chris Mason 已提交
674 675 676 677
	}
	return 0;
}

C
Chris Mason 已提交
678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695
int btrfs_copy_pinned(struct btrfs_root *root, struct radix_tree_root *copy)
{
	unsigned long gang[8];
	u64 last = 0;
	struct radix_tree_root *pinned_radix = &root->fs_info->pinned_radix;
	int ret;
	int i;

	while(1) {
		ret = find_first_radix_bit(pinned_radix, gang, last,
					   ARRAY_SIZE(gang));
		if (!ret)
			break;
		for (i = 0 ; i < ret; i++) {
			set_radix_bit(copy, gang[i]);
			last = gang[i] + 1;
		}
	}
696 697 698
	ret = find_first_radix_bit(&root->fs_info->extent_ins_radix, gang, 0,
				   ARRAY_SIZE(gang));
	WARN_ON(ret);
C
Chris Mason 已提交
699 700 701 702 703 704
	return 0;
}

int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
			       struct btrfs_root *root,
			       struct radix_tree_root *unpin_radix)
705
{
C
Chris Mason 已提交
706
	unsigned long gang[8];
C
Chris Mason 已提交
707
	struct btrfs_block_group_cache *block_group;
708
	u64 first = 0;
709 710
	int ret;
	int i;
C
Chris Mason 已提交
711
	struct radix_tree_root *pinned_radix = &root->fs_info->pinned_radix;
712 713 714
	struct extent_map_tree *free_space_cache;

	free_space_cache = &root->fs_info->free_space_cache;
715 716

	while(1) {
C
Chris Mason 已提交
717
		ret = find_first_radix_bit(unpin_radix, gang, 0,
C
Chris Mason 已提交
718
					   ARRAY_SIZE(gang));
719 720
		if (!ret)
			break;
721
		if (!first)
C
Chris Mason 已提交
722
			first = gang[0];
723
		for (i = 0; i < ret; i++) {
C
Chris Mason 已提交
724
			clear_radix_bit(pinned_radix, gang[i]);
C
Chris Mason 已提交
725
			clear_radix_bit(unpin_radix, gang[i]);
726 727
			block_group = btrfs_lookup_block_group(root->fs_info,
							       gang[i]);
C
Chris Mason 已提交
728 729 730 731 732
			if (block_group) {
				WARN_ON(block_group->pinned == 0);
				block_group->pinned--;
				if (gang[i] < block_group->last_alloc)
					block_group->last_alloc = gang[i];
733 734 735 736 737
				if (!block_group->data) {
					set_extent_dirty(free_space_cache,
							 gang[i], gang[i],
							 GFP_NOFS);
				}
C
Chris Mason 已提交
738
			}
739
		}
740 741 742 743
	}
	return 0;
}

744 745
static int finish_current_insert(struct btrfs_trans_handle *trans, struct
				 btrfs_root *extent_root)
C
Chris Mason 已提交
746
{
C
Chris Mason 已提交
747
	struct btrfs_key ins;
C
Chris Mason 已提交
748
	struct btrfs_extent_item extent_item;
C
Chris Mason 已提交
749 750
	int i;
	int ret;
751 752
	int err;
	unsigned long gang[8];
753
	struct btrfs_fs_info *info = extent_root->fs_info;
C
Chris Mason 已提交
754

755
	btrfs_set_stack_extent_refs(&extent_item, 1);
C
Chris Mason 已提交
756
	ins.offset = 1;
757
	btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
758 759
	btrfs_set_stack_extent_owner(&extent_item,
				     extent_root->root_key.objectid);
C
Chris Mason 已提交
760

761 762 763 764 765 766 767 768 769 770 771 772 773 774
	while(1) {
		ret = find_first_radix_bit(&info->extent_ins_radix, gang, 0,
					   ARRAY_SIZE(gang));
		if (!ret)
			break;

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

C
Chris Mason 已提交
779
static int pin_down_block(struct btrfs_root *root, u64 blocknr, int pending)
C
Chris Mason 已提交
780 781
{
	int err;
782
	struct extent_buffer *buf;
C
Chris Mason 已提交
783

C
Chris Mason 已提交
784
	if (!pending) {
785 786 787
		buf = btrfs_find_tree_block(root, blocknr);
		if (buf) {
			if (btrfs_buffer_uptodate(buf)) {
C
Chris Mason 已提交
788 789
				u64 transid =
				    root->fs_info->running_transaction->transid;
790 791
				if (btrfs_header_generation(buf) == transid) {
					free_extent_buffer(buf);
C
Chris Mason 已提交
792 793
					return 0;
				}
C
Chris Mason 已提交
794
			}
795
			free_extent_buffer(buf);
C
Chris Mason 已提交
796 797
		}
		err = set_radix_bit(&root->fs_info->pinned_radix, blocknr);
C
Chris Mason 已提交
798 799
		if (!err) {
			struct btrfs_block_group_cache *cache;
800 801
			cache = btrfs_lookup_block_group(root->fs_info,
							 blocknr);
C
Chris Mason 已提交
802 803 804
			if (cache)
				cache->pinned++;
		}
C
Chris Mason 已提交
805 806 807
	} else {
		err = set_radix_bit(&root->fs_info->pending_del_radix, blocknr);
	}
C
Chris Mason 已提交
808
	BUG_ON(err < 0);
C
Chris Mason 已提交
809 810 811
	return 0;
}

812
/*
813
 * remove an extent from the root, returns 0 on success
814
 */
815
static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
816 817
			 *root, u64 blocknr, u64 num_blocks, int pin,
			 int mark_free)
818
{
819
	struct btrfs_path *path;
C
Chris Mason 已提交
820
	struct btrfs_key key;
821 822
	struct btrfs_fs_info *info = root->fs_info;
	struct btrfs_root *extent_root = info->extent_root;
823
	struct extent_buffer *leaf;
824
	int ret;
C
Chris Mason 已提交
825
	struct btrfs_extent_item *ei;
C
Chris Mason 已提交
826
	u32 refs;
C
Chris Mason 已提交
827

828
	key.objectid = blocknr;
829
	btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
830 831
	key.offset = num_blocks;

832
	path = btrfs_alloc_path();
833 834
	if (!path)
		return -ENOMEM;
835

836 837 838 839
	ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
	if (ret < 0)
		return ret;
	BUG_ON(ret);
840 841 842

	leaf = path->nodes[0];
	ei = btrfs_item_ptr(leaf, path->slots[0],
C
Chris Mason 已提交
843
			    struct btrfs_extent_item);
844 845 846 847 848 849
	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 已提交
850
	if (refs == 0) {
851
		u64 super_blocks_used, root_blocks_used;
C
Chris Mason 已提交
852 853

		if (pin) {
C
Chris Mason 已提交
854
			ret = pin_down_block(root, blocknr, 0);
C
Chris Mason 已提交
855 856 857
			BUG_ON(ret);
		}

858
		/* block accounting for super block */
859 860
		super_blocks_used = btrfs_super_blocks_used(&info->super_copy);
		btrfs_set_super_blocks_used(&info->super_copy,
861
					    super_blocks_used - num_blocks);
862 863

		/* block accounting for root item */
864 865
		root_blocks_used = btrfs_root_used(&root->root_item);
		btrfs_set_root_used(&root->root_item,
866 867
					   root_blocks_used - num_blocks);

868
		ret = btrfs_del_item(trans, extent_root, path);
869 870 871
		if (ret) {
			return ret;
		}
872
		ret = update_block_group(trans, root, blocknr, num_blocks, 0,
C
Chris Mason 已提交
873
					 mark_free, 0);
C
Chris Mason 已提交
874
		BUG_ON(ret);
875
	}
876
	btrfs_free_path(path);
877
	finish_current_insert(trans, extent_root);
878 879 880 881 882 883 884
	return ret;
}

/*
 * find all the blocks marked as pending in the radix tree and remove
 * them from the extent map
 */
885 886
static int del_pending_extents(struct btrfs_trans_handle *trans, struct
			       btrfs_root *extent_root)
887 888
{
	int ret;
C
Chris Mason 已提交
889 890
	int wret;
	int err = 0;
C
Chris Mason 已提交
891
	unsigned long gang[4];
892
	int i;
C
Chris Mason 已提交
893 894
	struct radix_tree_root *pending_radix;
	struct radix_tree_root *pinned_radix;
C
Chris Mason 已提交
895
	struct btrfs_block_group_cache *cache;
C
Chris Mason 已提交
896 897 898

	pending_radix = &extent_root->fs_info->pending_del_radix;
	pinned_radix = &extent_root->fs_info->pinned_radix;
899 900

	while(1) {
901
		ret = find_first_radix_bit(pending_radix, gang, 0,
C
Chris Mason 已提交
902
					   ARRAY_SIZE(gang));
903 904 905
		if (!ret)
			break;
		for (i = 0; i < ret; i++) {
C
Chris Mason 已提交
906
			wret = set_radix_bit(pinned_radix, gang[i]);
C
Chris Mason 已提交
907
			if (wret == 0) {
908 909
				cache =
				  btrfs_lookup_block_group(extent_root->fs_info,
C
Chris Mason 已提交
910 911 912 913 914 915 916 917 918
							   gang[i]);
				if (cache)
					cache->pinned++;
			}
			if (wret < 0) {
				printk(KERN_CRIT "set_radix_bit, err %d\n",
				       wret);
				BUG_ON(wret < 0);
			}
C
Chris Mason 已提交
919 920
			wret = clear_radix_bit(pending_radix, gang[i]);
			BUG_ON(wret);
921
			wret = __free_extent(trans, extent_root,
922
					     gang[i], 1, 0, 0);
C
Chris Mason 已提交
923 924
			if (wret)
				err = wret;
925 926
		}
	}
C
Chris Mason 已提交
927
	return err;
928 929 930 931 932
}

/*
 * remove an extent from the root, returns 0 on success
 */
933 934
int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
		      *root, u64 blocknr, u64 num_blocks, int pin)
935
{
936
	struct btrfs_root *extent_root = root->fs_info->extent_root;
937 938
	int pending_ret;
	int ret;
939

940
	if (root == extent_root) {
C
Chris Mason 已提交
941
		pin_down_block(root, blocknr, 1);
942 943
		return 0;
	}
944
	ret = __free_extent(trans, root, blocknr, num_blocks, pin, pin == 0);
C
Chris Mason 已提交
945
	pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
946 947 948 949 950 951 952
	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
953
 * ins->flags = BTRFS_EXTENT_ITEM_KEY
954 955 956
 * ins->offset == number of blocks
 * Any available blocks before search_start are skipped.
 */
957
static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
958 959
			    *orig_root, u64 num_blocks, u64 empty_size,
			    u64 search_start, u64 search_end, u64 hint_block,
960 961
			    struct btrfs_key *ins, u64 exclude_start,
			    u64 exclude_nr, int data)
962
{
963
	struct btrfs_path *path;
C
Chris Mason 已提交
964
	struct btrfs_key key;
965 966 967
	int ret;
	u64 hole_size = 0;
	int slot = 0;
C
Chris Mason 已提交
968
	u64 last_block = 0;
C
Chris Mason 已提交
969
	u64 test_block;
C
Chris Mason 已提交
970
	u64 orig_search_start = search_start;
971
	int start_found;
972
	struct extent_buffer *l;
973
	struct btrfs_root * root = orig_root->fs_info->extent_root;
974
	struct btrfs_fs_info *info = root->fs_info;
975
	int total_needed = num_blocks;
C
Chris Mason 已提交
976
	int level;
C
Chris Mason 已提交
977
	struct btrfs_block_group_cache *block_group;
C
Chris Mason 已提交
978
	int full_scan = 0;
979
	int wrapped = 0;
980
	u64 cached_search_start = 0;
981

982
	WARN_ON(num_blocks < 1);
983 984
	btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);

985 986
	level = btrfs_header_level(root->node);

C
Chris Mason 已提交
987
	if (search_end == (u64)-1)
988
		search_end = btrfs_super_total_blocks(&info->super_copy);
989
	if (hint_block) {
990
		block_group = btrfs_lookup_block_group(info, hint_block);
C
Chris Mason 已提交
991
		block_group = btrfs_find_block_group(root, block_group,
992
						     hint_block, data, 1);
C
Chris Mason 已提交
993 994 995
	} else {
		block_group = btrfs_find_block_group(root,
						     trans->block_group, 0,
996
						     data, 1);
C
Chris Mason 已提交
997 998
	}

999
	total_needed += empty_size;
1000 1001
	path = btrfs_alloc_path();

C
Chris Mason 已提交
1002
check_failed:
1003 1004 1005
	search_start = find_search_start(root, &block_group,
					 search_start, total_needed, data);
	cached_search_start = search_start;
1006

1007
	btrfs_init_path(path);
1008 1009 1010
	ins->objectid = search_start;
	ins->offset = 0;
	start_found = 0;
1011
	path->reada = 2;
1012

1013
	ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
C
Chris Mason 已提交
1014 1015
	if (ret < 0)
		goto error;
C
Chris Mason 已提交
1016

1017
	if (path->slots[0] > 0) {
1018
		path->slots[0]--;
1019 1020
	}

1021 1022 1023
	l = path->nodes[0];
	btrfs_item_key_to_cpu(l, &key, path->slots[0]);

1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040
	/*
	 * 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]--;
		}
	}
1041

1042
	while (1) {
1043
		l = path->nodes[0];
1044
		slot = path->slots[0];
1045
		if (slot >= btrfs_header_nritems(l)) {
1046
			ret = btrfs_next_leaf(root, path);
1047 1048
			if (ret == 0)
				continue;
C
Chris Mason 已提交
1049 1050
			if (ret < 0)
				goto error;
1051 1052
			if (!start_found) {
				ins->objectid = search_start;
C
Chris Mason 已提交
1053
				ins->offset = search_end - search_start;
1054 1055 1056 1057 1058
				start_found = 1;
				goto check_pending;
			}
			ins->objectid = last_block > search_start ?
					last_block : search_start;
C
Chris Mason 已提交
1059
			ins->offset = search_end - ins->objectid;
1060 1061
			goto check_pending;
		}
1062

1063
		btrfs_item_key_to_cpu(l, &key, slot);
1064 1065 1066 1067 1068 1069 1070 1071 1072
		if (key.objectid >= search_start && key.objectid > last_block &&
		    start_found) {
			if (last_block < search_start)
				last_block = search_start;
			hole_size = key.objectid - last_block;
			if (hole_size >= num_blocks) {
				ins->objectid = last_block;
				ins->offset = hole_size;
				goto check_pending;
1073
			}
1074
		}
1075 1076 1077 1078

		if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY)
			goto next;

1079
		start_found = 1;
C
Chris Mason 已提交
1080
		last_block = key.objectid + key.offset;
1081

1082
		if (!full_scan && last_block >= block_group->key.objectid +
C
Chris Mason 已提交
1083 1084 1085 1086 1087 1088
		    block_group->key.offset) {
			btrfs_release_path(root, path);
			search_start = block_group->key.objectid +
				block_group->key.offset * 2;
			goto new_group;
		}
C
Chris Mason 已提交
1089
next:
1090
		path->slots[0]++;
1091
		cond_resched();
1092 1093 1094 1095 1096
	}
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
	 */
1097
	btrfs_release_path(root, path);
1098
	BUG_ON(ins->objectid < search_start);
1099

1100 1101 1102
	if (ins->objectid + num_blocks >= search_end)
		goto enospc;

C
Chris Mason 已提交
1103
	for (test_block = ins->objectid;
1104
	     test_block < ins->objectid + num_blocks; test_block++) {
1105 1106
		if (test_radix_bit(&info->pinned_radix, test_block) ||
		    test_radix_bit(&info->extent_ins_radix, test_block)) {
C
Chris Mason 已提交
1107
			search_start = test_block + 1;
C
Chris Mason 已提交
1108
			goto new_group;
1109 1110
		}
	}
1111 1112 1113 1114 1115
	if (exclude_nr > 0 && (ins->objectid + num_blocks > exclude_start &&
	    ins->objectid < exclude_start + exclude_nr)) {
		search_start = exclude_start + exclude_nr;
		goto new_group;
	}
1116
	if (!data) {
1117
		block_group = btrfs_lookup_block_group(info, ins->objectid);
1118 1119
		if (block_group)
			trans->block_group = block_group;
1120
	}
C
Chris Mason 已提交
1121
	ins->offset = num_blocks;
1122
	btrfs_free_path(path);
1123 1124 1125
	if (0 && ins->objectid != cached_search_start) {
printk("\tcached was %Lu found %Lu\n", cached_search_start, ins->objectid);
	}
1126
	return 0;
C
Chris Mason 已提交
1127 1128

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

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

1175 1176
	btrfs_set_stack_extent_refs(&extent_item, 1);
	btrfs_set_stack_extent_owner(&extent_item, owner);
1177

1178
	WARN_ON(num_blocks < 1);
1179 1180
	ret = find_free_extent(trans, root, num_blocks, empty_size,
			       search_start, search_end, hint_block, ins,
1181 1182
			       trans->alloc_exclude_start,
			       trans->alloc_exclude_nr, data);
C
Chris Mason 已提交
1183
	BUG_ON(ret);
1184 1185
	if (ret)
		return ret;
1186

1187
	/* block accounting for super block */
1188 1189
	super_blocks_used = btrfs_super_blocks_used(&info->super_copy);
	btrfs_set_super_blocks_used(&info->super_copy, super_blocks_used +
1190
				    num_blocks);
1191

1192
	/* block accounting for root item */
1193 1194
	root_blocks_used = btrfs_root_used(&root->root_item);
	btrfs_set_root_used(&root->root_item, root_blocks_used +
1195 1196
				   num_blocks);

1197 1198 1199 1200
	clear_extent_dirty(&root->fs_info->free_space_cache,
			   ins->objectid, ins->objectid + ins->offset - 1,
			   GFP_NOFS);

1201 1202 1203 1204 1205 1206 1207 1208 1209
	if (root == extent_root) {
		BUG_ON(num_blocks != 1);
		set_radix_bit(&root->fs_info->extent_ins_radix, ins->objectid);
		goto update_block;
	}

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

1213 1214 1215
	trans->alloc_exclude_start = 0;
	trans->alloc_exclude_nr = 0;

C
Chris Mason 已提交
1216
	BUG_ON(ret);
1217
	finish_current_insert(trans, extent_root);
C
Chris Mason 已提交
1218
	pending_ret = del_pending_extents(trans, extent_root);
1219

1220
	if (ret) {
C
Chris Mason 已提交
1221
		return ret;
1222 1223
	}
	if (pending_ret) {
C
Chris Mason 已提交
1224
		return pending_ret;
1225
	}
1226 1227

update_block:
C
Chris Mason 已提交
1228 1229
	ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0,
				 data);
C
Chris Mason 已提交
1230
	BUG_ON(ret);
C
Chris Mason 已提交
1231
	return 0;
1232 1233 1234 1235 1236 1237
}

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

1246
	ret = btrfs_alloc_extent(trans, root, root->root_key.objectid,
1247
				 1, empty_size, hint, (u64)-1, &ins, 0);
1248
	if (ret) {
1249 1250
		BUG_ON(ret > 0);
		return ERR_PTR(ret);
1251
	}
1252
	buf = btrfs_find_create_tree_block(root, ins.objectid);
1253 1254 1255 1256
	if (!buf) {
		btrfs_free_extent(trans, root, ins.objectid, 1, 0);
		return ERR_PTR(-ENOMEM);
	}
1257
	btrfs_set_buffer_uptodate(buf);
1258
	buf->alloc_addr = (unsigned long)__builtin_return_address(0);
1259 1260 1261
	set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
			 buf->start + buf->len - 1, GFP_NOFS);
	/*
C
Chris Mason 已提交
1262
	set_buffer_checked(buf);
1263
	set_buffer_defrag(buf);
1264 1265 1266 1267
	*/
	/* FIXME!!!!!!!!!!!!!!!!
	set_radix_bit(&trans->transaction->dirty_pages, buf->pages[0]->index);
	*/
1268
	trans->blocks_used++;
1269 1270
	return buf;
}
1271

1272
static int drop_leaf_ref(struct btrfs_trans_handle *trans,
1273
			 struct btrfs_root *root, struct extent_buffer *leaf)
1274
{
1275
	struct btrfs_key key;
1276 1277 1278 1279 1280
	struct btrfs_file_extent_item *fi;
	int i;
	int nritems;
	int ret;

1281 1282
	BUG_ON(!btrfs_is_leaf(leaf));
	nritems = btrfs_header_nritems(leaf);
1283
	for (i = 0; i < nritems; i++) {
C
Chris Mason 已提交
1284
		u64 disk_blocknr;
1285 1286 1287

		btrfs_item_key_to_cpu(leaf, &key, i);
		if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1288 1289
			continue;
		fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
1290 1291
		if (btrfs_file_extent_type(leaf, fi) ==
		    BTRFS_FILE_EXTENT_INLINE)
1292
			continue;
1293 1294 1295 1296
		/*
		 * FIXME make sure to insert a trans record that
		 * repeats the snapshot del on crash
		 */
1297
		disk_blocknr = btrfs_file_extent_disk_blocknr(leaf, fi);
C
Chris Mason 已提交
1298 1299 1300
		if (disk_blocknr == 0)
			continue;
		ret = btrfs_free_extent(trans, root, disk_blocknr,
1301
				btrfs_file_extent_disk_num_blocks(leaf, fi), 0);
1302 1303 1304 1305 1306
		BUG_ON(ret);
	}
	return 0;
}

1307
static void reada_walk_down(struct btrfs_root *root,
1308
			    struct extent_buffer *node)
1309 1310 1311 1312 1313 1314 1315
{
	int i;
	u32 nritems;
	u64 blocknr;
	int ret;
	u32 refs;

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

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

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

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

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

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

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

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

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

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

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

1479 1480
	path = btrfs_alloc_path();
	BUG_ON(!path);
C
Chris Mason 已提交
1481

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

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

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

C
Chris Mason 已提交
1532
static int free_block_group_radix(struct radix_tree_root *radix)
C
Chris Mason 已提交
1533 1534 1535 1536 1537 1538
{
	int ret;
	struct btrfs_block_group_cache *cache[8];
	int i;

	while(1) {
C
Chris Mason 已提交
1539
		ret = radix_tree_gang_lookup(radix, (void **)cache, 0,
C
Chris Mason 已提交
1540 1541 1542 1543
					     ARRAY_SIZE(cache));
		if (!ret)
			break;
		for (i = 0; i < ret; i++) {
C
Chris Mason 已提交
1544
			radix_tree_delete(radix, cache[i]->key.objectid +
C
Chris Mason 已提交
1545 1546 1547 1548 1549 1550 1551
					  cache[i]->key.offset - 1);
			kfree(cache[i]);
		}
	}
	return 0;
}

C
Chris Mason 已提交
1552 1553 1554 1555
int btrfs_free_block_groups(struct btrfs_fs_info *info)
{
	int ret;
	int ret2;
1556 1557
	u64 start;
	u64 end;
C
Chris Mason 已提交
1558 1559 1560 1561 1562 1563 1564

	ret = free_block_group_radix(&info->block_group_radix);
	ret2 = free_block_group_radix(&info->block_group_data_radix);
	if (ret)
		return ret;
	if (ret2)
		return ret2;
1565 1566

	while(1) {
1567 1568 1569
		ret = find_first_extent_bit(&info->free_space_cache, 0,
					    &start, &end, EXTENT_DIRTY);
		if (ret)
1570
			break;
1571 1572
		clear_extent_dirty(&info->free_space_cache, start,
				   end, GFP_NOFS);
1573
	}
C
Chris Mason 已提交
1574 1575 1576
	return 0;
}

C
Chris Mason 已提交
1577 1578 1579 1580 1581 1582
int btrfs_read_block_groups(struct btrfs_root *root)
{
	struct btrfs_path *path;
	int ret;
	int err = 0;
	struct btrfs_block_group_cache *cache;
C
Chris Mason 已提交
1583 1584
	struct btrfs_fs_info *info = root->fs_info;
	struct radix_tree_root *radix;
C
Chris Mason 已提交
1585 1586
	struct btrfs_key key;
	struct btrfs_key found_key;
1587
	struct extent_buffer *leaf;
C
Chris Mason 已提交
1588
	u64 group_size_blocks;
1589
	u64 used;
C
Chris Mason 已提交
1590

C
Chris Mason 已提交
1591 1592
	group_size_blocks = BTRFS_BLOCK_GROUP_SIZE >>
		root->fs_info->sb->s_blocksize_bits;
C
Chris Mason 已提交
1593
	root = info->extent_root;
C
Chris Mason 已提交
1594 1595 1596 1597 1598 1599 1600 1601 1602
	key.objectid = 0;
	key.offset = group_size_blocks;
	btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);

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

	while(1) {
C
Chris Mason 已提交
1603
		ret = btrfs_search_slot(NULL, info->extent_root,
C
Chris Mason 已提交
1604 1605 1606 1607 1608
					&key, path, 0, 0);
		if (ret != 0) {
			err = ret;
			break;
		}
1609 1610
		leaf = path->nodes[0];
		btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
C
Chris Mason 已提交
1611 1612 1613 1614 1615
		cache = kmalloc(sizeof(*cache), GFP_NOFS);
		if (!cache) {
			err = -1;
			break;
		}
C
Chris Mason 已提交
1616

1617 1618 1619 1620
		read_extent_buffer(leaf, &cache->item,
				   btrfs_item_ptr_offset(leaf, path->slots[0]),
				   sizeof(cache->item));
		if (cache->item.flags & BTRFS_BLOCK_GROUP_DATA) {
C
Chris Mason 已提交
1621
			radix = &info->block_group_data_radix;
C
Chris Mason 已提交
1622 1623
			cache->data = 1;
		} else {
C
Chris Mason 已提交
1624
			radix = &info->block_group_radix;
C
Chris Mason 已提交
1625 1626
			cache->data = 0;
		}
C
Chris Mason 已提交
1627

C
Chris Mason 已提交
1628
		memcpy(&cache->key, &found_key, sizeof(found_key));
1629 1630
		cache->last_alloc = cache->key.objectid;
		cache->first_free = cache->key.objectid;
C
Chris Mason 已提交
1631
		cache->pinned = 0;
1632 1633
		cache->cached = 0;

C
Chris Mason 已提交
1634 1635
		cache->radix = radix;

C
Chris Mason 已提交
1636 1637
		key.objectid = found_key.objectid + found_key.offset;
		btrfs_release_path(root, path);
1638

C
Chris Mason 已提交
1639
		ret = radix_tree_insert(radix, found_key.objectid +
C
Chris Mason 已提交
1640 1641 1642
					found_key.offset - 1,
					(void *)cache);
		BUG_ON(ret);
1643
		used = btrfs_block_group_used(&cache->item);
C
Chris Mason 已提交
1644
		if (used < div_factor(key.offset, 8)) {
C
Chris Mason 已提交
1645
			radix_tree_tag_set(radix, found_key.objectid +
1646 1647 1648
					   found_key.offset - 1,
					   BTRFS_BLOCK_GROUP_AVAIL);
		}
C
Chris Mason 已提交
1649
		if (key.objectid >=
1650
		    btrfs_super_total_blocks(&info->super_copy))
C
Chris Mason 已提交
1651 1652 1653 1654 1655 1656
			break;
	}

	btrfs_free_path(path);
	return 0;
}