extent-tree.c 32.0 KB
Newer Older
1
#include <linux/module.h>
2 3 4
#include "ctree.h"
#include "disk-io.h"
#include "print-tree.h"
5
#include "transaction.h"
6

7 8
static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
			    *orig_root, u64 num_blocks, u64 search_start, u64
C
Chris Mason 已提交
9
			    search_end, struct btrfs_key *ins, int data);
10 11
static int finish_current_insert(struct btrfs_trans_handle *trans, struct
				 btrfs_root *extent_root);
C
Chris Mason 已提交
12 13
static int del_pending_extents(struct btrfs_trans_handle *trans, struct
			       btrfs_root *extent_root);
14

C
Chris Mason 已提交
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
static struct btrfs_block_group_cache *lookup_block_group(struct
							  btrfs_fs_info *info,
							  u64 blocknr)
{
	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) {
		if (block_group->key.objectid <= blocknr && blocknr <
		    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) {
		if (block_group->key.objectid <= blocknr && blocknr <
		    block_group->key.objectid + block_group->key.offset)
			return block_group;
	}
printk("lookup_block_group fails for blocknr %Lu\n", blocknr);
	return NULL;
}

42 43
struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
						 struct btrfs_block_group_cache
C
Chris Mason 已提交
44 45
						 *hint, u64 search_start,
						 int data)
C
Chris Mason 已提交
46 47
{
	struct btrfs_block_group_cache *cache[8];
48
	struct btrfs_block_group_cache *found_group = NULL;
C
Chris Mason 已提交
49
	struct btrfs_fs_info *info = root->fs_info;
C
Chris Mason 已提交
50
	struct radix_tree_root *radix;
C
Chris Mason 已提交
51
	u64 used;
52 53
	u64 last = 0;
	u64 hint_last;
C
Chris Mason 已提交
54 55
	int i;
	int ret;
56
	int full_search = 0;
C
Chris Mason 已提交
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74

	if (data)
		radix = &info->block_group_data_radix;
	else
		radix = &info->block_group_radix;

	if (search_start) {
		struct btrfs_block_group_cache *shint;
		shint = lookup_block_group(info, search_start);
		if (shint->data == data) {
			used = btrfs_block_group_used(&shint->item);
			if (used + shint->pinned <
			    (shint->key.offset * 8) / 10) {
				return shint;
			}
		}
	}
	if (hint && hint->data == data) {
75
		used = btrfs_block_group_used(&hint->item);
C
Chris Mason 已提交
76
		if (used + hint->pinned < (hint->key.offset * 8) / 10) {
77 78
			return hint;
		}
C
Chris Mason 已提交
79 80 81 82 83 84 85 86 87 88 89
		if (used >= (hint->key.offset * 8) / 10) {
			radix_tree_tag_clear(radix,
					     hint->key.objectid +
					     hint->key.offset - 1,
					     BTRFS_BLOCK_GROUP_AVAIL);
		}
		last = hint->key.offset * 2;
		if (hint->key.objectid >= last)
			last = max(search_start, hint->key.objectid - last);
		else
			last = hint->key.objectid + hint->key.offset;
90 91
		hint_last = last;
	} else {
C
Chris Mason 已提交
92 93
		hint_last = search_start;
		last = search_start;
94
	}
C
Chris Mason 已提交
95
	while(1) {
C
Chris Mason 已提交
96
		ret = radix_tree_gang_lookup_tag(radix, (void **)cache,
C
Chris Mason 已提交
97
						 last, ARRAY_SIZE(cache),
98
						 BTRFS_BLOCK_GROUP_AVAIL);
C
Chris Mason 已提交
99 100 101
		if (!ret)
			break;
		for (i = 0; i < ret; i++) {
C
Chris Mason 已提交
102 103
			last = cache[i]->key.objectid +
				cache[i]->key.offset;
C
Chris Mason 已提交
104
			used = btrfs_block_group_used(&cache[i]->item);
C
Chris Mason 已提交
105 106
			if (used + cache[i]->pinned <
			    (cache[i]->key.offset * 8) / 10) {
107 108
				found_group = cache[i];
				goto found;
C
Chris Mason 已提交
109
			}
C
Chris Mason 已提交
110 111 112 113 114 115
			if (used >= (cache[i]->key.offset * 8) / 10) {
				radix_tree_tag_clear(radix,
						     cache[i]->key.objectid +
						     cache[i]->key.offset - 1,
						     BTRFS_BLOCK_GROUP_AVAIL);
			}
C
Chris Mason 已提交
116 117
		}
	}
118 119
	last = hint_last;
again:
C
Chris Mason 已提交
120
	while(1) {
C
Chris Mason 已提交
121 122
		ret = radix_tree_gang_lookup(radix, (void **)cache,
					     last, ARRAY_SIZE(cache));
C
Chris Mason 已提交
123 124 125
		if (!ret)
			break;
		for (i = 0; i < ret; i++) {
C
Chris Mason 已提交
126 127
			last = cache[i]->key.objectid +
				cache[i]->key.offset;
C
Chris Mason 已提交
128
			used = btrfs_block_group_used(&cache[i]->item);
C
Chris Mason 已提交
129
			if (used + cache[i]->pinned < cache[i]->key.offset) {
130 131
				found_group = cache[i];
				goto found;
C
Chris Mason 已提交
132
			}
C
Chris Mason 已提交
133 134 135 136 137 138
			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 已提交
139 140
		}
	}
141
	if (!full_search) {
C
Chris Mason 已提交
142
		last = search_start;
143 144 145 146
		full_search = 1;
		goto again;
	}
	if (!found_group) {
C
Chris Mason 已提交
147
		ret = radix_tree_gang_lookup(radix,
148 149 150
					     (void **)&found_group, 0, 1);
		BUG_ON(ret != 1);
	}
C
Chris Mason 已提交
151
found:
152
	return found_group;
C
Chris Mason 已提交
153 154
}

C
Chris Mason 已提交
155 156 157
int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
				struct btrfs_root *root,
				u64 blocknr, u64 num_blocks)
C
Chris Mason 已提交
158
{
159
	struct btrfs_path *path;
C
Chris Mason 已提交
160
	int ret;
C
Chris Mason 已提交
161
	struct btrfs_key key;
C
Chris Mason 已提交
162 163
	struct btrfs_leaf *l;
	struct btrfs_extent_item *item;
C
Chris Mason 已提交
164
	struct btrfs_key ins;
C
Chris Mason 已提交
165
	u32 refs;
C
Chris Mason 已提交
166

167
	find_free_extent(trans, root->fs_info->extent_root, 0, 0, (u64)-1,
C
Chris Mason 已提交
168
			 &ins, 0);
169 170 171
	path = btrfs_alloc_path();
	BUG_ON(!path);
	btrfs_init_path(path);
C
Chris Mason 已提交
172 173
	key.objectid = blocknr;
	key.flags = 0;
174
	btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
175
	key.offset = num_blocks;
176
	ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
177
				0, 1);
178 179
	if (ret != 0) {
printk("can't find block %Lu %Lu\n", blocknr, num_blocks);
180
		BUG();
181
	}
C
Chris Mason 已提交
182
	BUG_ON(ret != 0);
183 184
	l = btrfs_buffer_leaf(path->nodes[0]);
	item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
C
Chris Mason 已提交
185 186
	refs = btrfs_extent_refs(item);
	btrfs_set_extent_refs(item, refs + 1);
187
	btrfs_mark_buffer_dirty(path->nodes[0]);
188

189 190
	btrfs_release_path(root->fs_info->extent_root, path);
	btrfs_free_path(path);
191
	finish_current_insert(trans, root->fs_info->extent_root);
C
Chris Mason 已提交
192
	del_pending_extents(trans, root->fs_info->extent_root);
C
Chris Mason 已提交
193 194 195
	return 0;
}

C
Chris Mason 已提交
196 197 198
static int lookup_extent_ref(struct btrfs_trans_handle *trans,
			     struct btrfs_root *root, u64 blocknr,
			     u64 num_blocks, u32 *refs)
199
{
200
	struct btrfs_path *path;
201
	int ret;
C
Chris Mason 已提交
202
	struct btrfs_key key;
C
Chris Mason 已提交
203 204
	struct btrfs_leaf *l;
	struct btrfs_extent_item *item;
205 206 207

	path = btrfs_alloc_path();
	btrfs_init_path(path);
208
	key.objectid = blocknr;
209
	key.offset = num_blocks;
210 211
	key.flags = 0;
	btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
212
	ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
213
				0, 0);
214 215
	if (ret != 0)
		BUG();
216 217
	l = btrfs_buffer_leaf(path->nodes[0]);
	item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
C
Chris Mason 已提交
218
	*refs = btrfs_extent_refs(item);
219 220
	btrfs_release_path(root->fs_info->extent_root, path);
	btrfs_free_path(path);
221 222 223
	return 0;
}

C
Chris Mason 已提交
224 225 226
int btrfs_inc_root_ref(struct btrfs_trans_handle *trans,
		       struct btrfs_root *root)
{
C
Chris Mason 已提交
227
	return btrfs_inc_extent_ref(trans, root, bh_blocknr(root->node), 1);
C
Chris Mason 已提交
228 229
}

230
int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
C
Chris Mason 已提交
231
		  struct buffer_head *buf)
C
Chris Mason 已提交
232 233
{
	u64 blocknr;
C
Chris Mason 已提交
234
	struct btrfs_node *buf_node;
235 236 237
	struct btrfs_leaf *buf_leaf;
	struct btrfs_disk_key *key;
	struct btrfs_file_extent_item *fi;
C
Chris Mason 已提交
238
	int i;
239 240
	int leaf;
	int ret;
241

242
	if (!root->ref_cows)
243
		return 0;
C
Chris Mason 已提交
244
	buf_node = btrfs_buffer_node(buf);
245 246
	leaf = btrfs_is_leaf(buf_node);
	buf_leaf = btrfs_buffer_leaf(buf);
C
Chris Mason 已提交
247
	for (i = 0; i < btrfs_header_nritems(&buf_node->header); i++) {
248 249 250 251 252 253
		if (leaf) {
			key = &buf_leaf->items[i].key;
			if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY)
				continue;
			fi = btrfs_item_ptr(buf_leaf, i,
					    struct btrfs_file_extent_item);
254 255 256
			if (btrfs_file_extent_type(fi) ==
			    BTRFS_FILE_EXTENT_INLINE)
				continue;
C
Chris Mason 已提交
257
			ret = btrfs_inc_extent_ref(trans, root,
258 259 260 261 262
				    btrfs_file_extent_disk_blocknr(fi),
				    btrfs_file_extent_disk_num_blocks(fi));
			BUG_ON(ret);
		} else {
			blocknr = btrfs_node_blockptr(buf_node, i);
C
Chris Mason 已提交
263
			ret = btrfs_inc_extent_ref(trans, root, blocknr, 1);
264 265
			BUG_ON(ret);
		}
C
Chris Mason 已提交
266 267 268 269
	}
	return 0;
}

C
Chris Mason 已提交
270 271 272 273 274 275 276 277 278 279 280
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;
	struct btrfs_block_group_item *bi;
	struct btrfs_key ins;

C
Chris Mason 已提交
281
	find_free_extent(trans, extent_root, 0, 0, (u64)-1, &ins, 0);
C
Chris Mason 已提交
282 283 284 285 286 287 288 289 290 291 292 293 294 295
	ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
	BUG_ON(ret);
	bi = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
			    struct btrfs_block_group_item);
	memcpy(bi, &cache->item, sizeof(*bi));
	mark_buffer_dirty(path->nodes[0]);
	btrfs_release_path(extent_root, path);

	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 已提交
296 297
	if (cache->data)
		cache->last_alloc = cache->first_free;
C
Chris Mason 已提交
298 299 300 301
	return 0;

}

C
Chris Mason 已提交
302 303 304
static int write_dirty_block_radix(struct btrfs_trans_handle *trans,
				   struct btrfs_root *root,
				   struct radix_tree_root *radix)
C
Chris Mason 已提交
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
{
	struct btrfs_block_group_cache *cache[8];
	int ret;
	int err = 0;
	int werr = 0;
	int i;
	struct btrfs_path *path;

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

	while(1) {
		ret = radix_tree_gang_lookup_tag(radix, (void **)cache,
						 0, ARRAY_SIZE(cache),
						 BTRFS_BLOCK_GROUP_DIRTY);
		if (!ret)
			break;
		for (i = 0; i < ret; i++) {
			radix_tree_tag_clear(radix, cache[i]->key.objectid +
					     cache[i]->key.offset - 1,
					     BTRFS_BLOCK_GROUP_DIRTY);
			err = write_one_cache_group(trans, root,
						    path, cache[i]);
			if (err)
				werr = err;
		}
	}
	btrfs_free_path(path);
	return werr;
}

C
Chris Mason 已提交
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
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 已提交
353 354 355 356 357 358
static int update_block_group(struct btrfs_trans_handle *trans,
			      struct btrfs_root *root,
			      u64 blocknr, u64 num, int alloc)
{
	struct btrfs_block_group_cache *cache;
	struct btrfs_fs_info *info = root->fs_info;
C
Chris Mason 已提交
359
	struct radix_tree_root *radix;
C
Chris Mason 已提交
360 361 362 363
	u64 total = num;
	u64 old_val;
	u64 block_in_group;
	int ret;
C
Chris Mason 已提交
364 365 366 367
	if (num != 1)
		radix = &info->block_group_data_radix;
	else
		radix = &info->block_group_radix;
C
Chris Mason 已提交
368
	while(total) {
C
Chris Mason 已提交
369 370
		ret = radix_tree_gang_lookup(radix, (void **)&cache,
					     blocknr, 1);
C
Chris Mason 已提交
371 372 373
		if (!ret) {
			printk(KERN_CRIT "blocknr %Lu lookup failed\n",
			       blocknr);
C
Chris Mason 已提交
374
			return -1;
C
Chris Mason 已提交
375
		}
C
Chris Mason 已提交
376
		block_in_group = blocknr - cache->key.objectid;
C
Chris Mason 已提交
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
		if (block_in_group > cache->key.offset || cache->key.objectid >
		    blocknr) {
			if (radix == &info->block_group_data_radix)
				radix = &info->block_group_radix;
			else
				radix = &info->block_group_data_radix;
			ret = radix_tree_gang_lookup(radix, (void **)&cache,
						     blocknr, 1);
			if (!ret) {
				printk(KERN_CRIT "blocknr %Lu lookup failed\n",
				       blocknr);
				return -1;
			}
			block_in_group = blocknr - cache->key.objectid;
			if (block_in_group > cache->key.offset ||
			    cache->key.objectid > blocknr) {
				BUG();
			}
		}
C
Chris Mason 已提交
396
		WARN_ON(block_in_group > cache->key.offset);
C
Chris Mason 已提交
397 398
		radix_tree_tag_set(radix, cache->key.objectid +
				   cache->key.offset - 1,
C
Chris Mason 已提交
399 400 401 402 403 404
				   BTRFS_BLOCK_GROUP_DIRTY);

		old_val = btrfs_block_group_used(&cache->item);
		num = min(total, cache->key.offset - block_in_group);
		total -= num;
		blocknr += num;
C
Chris Mason 已提交
405
		if (alloc) {
C
Chris Mason 已提交
406
			old_val += num;
C
Chris Mason 已提交
407 408 409
			if (blocknr > cache->last_alloc)
				cache->last_alloc = blocknr;
		} else {
C
Chris Mason 已提交
410
			old_val -= num;
C
Chris Mason 已提交
411 412 413
			if (blocknr < cache->first_free)
				cache->first_free = blocknr;
		}
C
Chris Mason 已提交
414 415 416 417 418
		btrfs_set_block_group_used(&cache->item, old_val);
	}
	return 0;
}

C
Chris Mason 已提交
419 420 421 422 423 424 425
static int try_remove_page(struct address_space *mapping, unsigned long index)
{
	int ret;
	ret = invalidate_mapping_pages(mapping, index, index);
	return ret;
}

426 427
int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, struct
			       btrfs_root *root)
428
{
C
Chris Mason 已提交
429
	unsigned long gang[8];
C
Chris Mason 已提交
430
	struct inode *btree_inode = root->fs_info->btree_inode;
C
Chris Mason 已提交
431
	struct btrfs_block_group_cache *block_group;
432
	u64 first = 0;
433 434
	int ret;
	int i;
C
Chris Mason 已提交
435
	struct radix_tree_root *pinned_radix = &root->fs_info->pinned_radix;
436 437

	while(1) {
C
Chris Mason 已提交
438 439
		ret = find_first_radix_bit(pinned_radix, gang,
					   ARRAY_SIZE(gang));
440 441
		if (!ret)
			break;
442
		if (!first)
C
Chris Mason 已提交
443
			first = gang[0];
444
		for (i = 0; i < ret; i++) {
C
Chris Mason 已提交
445
			clear_radix_bit(pinned_radix, gang[i]);
C
Chris Mason 已提交
446 447 448 449 450 451 452 453
			block_group = lookup_block_group(root->fs_info,
							 gang[i]);
			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];
			}
C
Chris Mason 已提交
454 455 456
			try_remove_page(btree_inode->i_mapping,
					gang[i] << (PAGE_CACHE_SHIFT -
						    btree_inode->i_blkbits));
457
		}
458 459 460 461
	}
	return 0;
}

462 463
static int finish_current_insert(struct btrfs_trans_handle *trans, struct
				 btrfs_root *extent_root)
C
Chris Mason 已提交
464
{
C
Chris Mason 已提交
465
	struct btrfs_key ins;
C
Chris Mason 已提交
466
	struct btrfs_extent_item extent_item;
C
Chris Mason 已提交
467 468
	int i;
	int ret;
469 470
	u64 super_blocks_used;
	struct btrfs_fs_info *info = extent_root->fs_info;
C
Chris Mason 已提交
471

C
Chris Mason 已提交
472
	btrfs_set_extent_refs(&extent_item, 1);
C
Chris Mason 已提交
473 474
	ins.offset = 1;
	ins.flags = 0;
475
	btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
476
	btrfs_set_extent_owner(&extent_item, extent_root->root_key.objectid);
C
Chris Mason 已提交
477

478 479
	for (i = 0; i < extent_root->fs_info->extent_tree_insert_nr; i++) {
		ins.objectid = extent_root->fs_info->extent_tree_insert[i];
480 481 482
		super_blocks_used = btrfs_super_blocks_used(info->disk_super);
		btrfs_set_super_blocks_used(info->disk_super,
					    super_blocks_used + 1);
483 484
		ret = btrfs_insert_item(trans, extent_root, &ins, &extent_item,
					sizeof(extent_item));
C
Chris Mason 已提交
485 486
		BUG_ON(ret);
	}
487 488
	extent_root->fs_info->extent_tree_insert_nr = 0;
	extent_root->fs_info->extent_tree_prealloc_nr = 0;
C
Chris Mason 已提交
489 490 491
	return 0;
}

C
Chris Mason 已提交
492
static int pin_down_block(struct btrfs_root *root, u64 blocknr, int pending)
C
Chris Mason 已提交
493 494
{
	int err;
C
Chris Mason 已提交
495
	struct btrfs_header *header;
C
Chris Mason 已提交
496 497
	struct buffer_head *bh;

C
Chris Mason 已提交
498
	if (!pending) {
499
		bh = btrfs_find_tree_block(root, blocknr);
C
Chris Mason 已提交
500 501 502 503 504 505 506 507 508 509
		if (bh) {
			if (buffer_uptodate(bh)) {
				u64 transid =
				    root->fs_info->running_transaction->transid;
				header = btrfs_buffer_header(bh);
				if (btrfs_header_generation(header) ==
				    transid) {
					btrfs_block_release(root, bh);
					return 0;
				}
C
Chris Mason 已提交
510
			}
C
Chris Mason 已提交
511
			btrfs_block_release(root, bh);
C
Chris Mason 已提交
512 513
		}
		err = set_radix_bit(&root->fs_info->pinned_radix, blocknr);
C
Chris Mason 已提交
514 515 516 517 518 519
		if (!err) {
			struct btrfs_block_group_cache *cache;
			cache = lookup_block_group(root->fs_info, blocknr);
			if (cache)
				cache->pinned++;
		}
C
Chris Mason 已提交
520 521 522
	} else {
		err = set_radix_bit(&root->fs_info->pending_del_radix, blocknr);
	}
C
Chris Mason 已提交
523
	BUG_ON(err < 0);
C
Chris Mason 已提交
524 525 526
	return 0;
}

527
/*
528
 * remove an extent from the root, returns 0 on success
529
 */
530
static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
C
Chris Mason 已提交
531
			 *root, u64 blocknr, u64 num_blocks, int pin)
532
{
533
	struct btrfs_path *path;
C
Chris Mason 已提交
534
	struct btrfs_key key;
535 536
	struct btrfs_fs_info *info = root->fs_info;
	struct btrfs_root *extent_root = info->extent_root;
537
	int ret;
C
Chris Mason 已提交
538
	struct btrfs_extent_item *ei;
C
Chris Mason 已提交
539
	struct btrfs_key ins;
C
Chris Mason 已提交
540
	u32 refs;
C
Chris Mason 已提交
541

542 543
	key.objectid = blocknr;
	key.flags = 0;
544
	btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
545 546
	key.offset = num_blocks;

C
Chris Mason 已提交
547
	find_free_extent(trans, root, 0, 0, (u64)-1, &ins, 0);
548 549 550
	path = btrfs_alloc_path();
	BUG_ON(!path);
	btrfs_init_path(path);
551

552
	ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
553
	if (ret) {
554
		printk("failed to find %Lu\n", key.objectid);
C
Chris Mason 已提交
555
		btrfs_print_tree(extent_root, extent_root->node);
556
		printk("failed to find %Lu\n", key.objectid);
557 558
		BUG();
	}
559
	ei = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
C
Chris Mason 已提交
560
			    struct btrfs_extent_item);
561
	BUG_ON(ei->refs == 0);
C
Chris Mason 已提交
562 563
	refs = btrfs_extent_refs(ei) - 1;
	btrfs_set_extent_refs(ei, refs);
564
	btrfs_mark_buffer_dirty(path->nodes[0]);
C
Chris Mason 已提交
565
	if (refs == 0) {
566
		u64 super_blocks_used;
C
Chris Mason 已提交
567 568

		if (pin) {
C
Chris Mason 已提交
569
			ret = pin_down_block(root, blocknr, 0);
C
Chris Mason 已提交
570 571 572
			BUG_ON(ret);
		}

573 574 575
		super_blocks_used = btrfs_super_blocks_used(info->disk_super);
		btrfs_set_super_blocks_used(info->disk_super,
					    super_blocks_used - num_blocks);
576
		ret = btrfs_del_item(trans, extent_root, path);
577 578
		if (ret)
			BUG();
C
Chris Mason 已提交
579 580
		ret = update_block_group(trans, root, blocknr, num_blocks, 0);
		BUG_ON(ret);
581
	}
582 583
	btrfs_release_path(extent_root, path);
	btrfs_free_path(path);
584
	finish_current_insert(trans, extent_root);
585 586 587 588 589 590 591
	return ret;
}

/*
 * find all the blocks marked as pending in the radix tree and remove
 * them from the extent map
 */
592 593
static int del_pending_extents(struct btrfs_trans_handle *trans, struct
			       btrfs_root *extent_root)
594 595
{
	int ret;
C
Chris Mason 已提交
596 597
	int wret;
	int err = 0;
C
Chris Mason 已提交
598
	unsigned long gang[4];
599
	int i;
C
Chris Mason 已提交
600 601
	struct radix_tree_root *pending_radix;
	struct radix_tree_root *pinned_radix;
C
Chris Mason 已提交
602
	struct btrfs_block_group_cache *cache;
C
Chris Mason 已提交
603 604 605

	pending_radix = &extent_root->fs_info->pending_del_radix;
	pinned_radix = &extent_root->fs_info->pinned_radix;
606 607

	while(1) {
C
Chris Mason 已提交
608 609
		ret = find_first_radix_bit(pending_radix, gang,
					   ARRAY_SIZE(gang));
610 611 612
		if (!ret)
			break;
		for (i = 0; i < ret; i++) {
C
Chris Mason 已提交
613
			wret = set_radix_bit(pinned_radix, gang[i]);
C
Chris Mason 已提交
614 615 616 617 618 619 620 621 622 623 624
			if (wret == 0) {
				cache = lookup_block_group(extent_root->fs_info,
							   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 已提交
625 626
			wret = clear_radix_bit(pending_radix, gang[i]);
			BUG_ON(wret);
627
			wret = __free_extent(trans, extent_root,
C
Chris Mason 已提交
628
					     gang[i], 1, 0);
C
Chris Mason 已提交
629 630
			if (wret)
				err = wret;
631 632
		}
	}
C
Chris Mason 已提交
633
	return err;
634 635 636 637 638
}

/*
 * remove an extent from the root, returns 0 on success
 */
639 640
int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
		      *root, u64 blocknr, u64 num_blocks, int pin)
641
{
642
	struct btrfs_root *extent_root = root->fs_info->extent_root;
643 644
	int pending_ret;
	int ret;
645

646
	if (root == extent_root) {
C
Chris Mason 已提交
647
		pin_down_block(root, blocknr, 1);
648 649
		return 0;
	}
C
Chris Mason 已提交
650
	ret = __free_extent(trans, root, blocknr, num_blocks, pin);
C
Chris Mason 已提交
651
	pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
652 653 654 655 656 657 658
	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
659
 * ins->flags = BTRFS_EXTENT_ITEM_KEY
660 661 662
 * ins->offset == number of blocks
 * Any available blocks before search_start are skipped.
 */
663 664
static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
			    *orig_root, u64 num_blocks, u64 search_start, u64
C
Chris Mason 已提交
665
			    search_end, struct btrfs_key *ins, int data)
666
{
667
	struct btrfs_path *path;
C
Chris Mason 已提交
668
	struct btrfs_key key;
669 670 671
	int ret;
	u64 hole_size = 0;
	int slot = 0;
C
Chris Mason 已提交
672
	u64 last_block = 0;
C
Chris Mason 已提交
673
	u64 test_block;
C
Chris Mason 已提交
674
	u64 orig_search_start = search_start;
675
	int start_found;
C
Chris Mason 已提交
676
	struct btrfs_leaf *l;
677
	struct btrfs_root * root = orig_root->fs_info->extent_root;
678
	struct btrfs_fs_info *info = root->fs_info;
679
	int total_needed = num_blocks;
680 681
	int total_found = 0;
	int fill_prealloc = 0;
C
Chris Mason 已提交
682
	int level;
C
Chris Mason 已提交
683
	struct btrfs_block_group_cache *block_group;
C
Chris Mason 已提交
684
	int full_scan = 0;
685

686 687 688 689
	path = btrfs_alloc_path();
	ins->flags = 0;
	btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);

C
Chris Mason 已提交
690
	level = btrfs_header_level(btrfs_buffer_header(root->node));
691 692 693
	if (num_blocks == 0) {
		fill_prealloc = 1;
		num_blocks = 1;
694
		total_needed = (min(level + 1, BTRFS_MAX_LEVEL) + 2) * 3;
695
	}
C
Chris Mason 已提交
696 697 698 699 700 701 702 703 704 705 706 707 708
	if (search_start) {
		block_group = lookup_block_group(info, search_start);
		block_group = btrfs_find_block_group(root, block_group,
						     search_start, data);
	} else {
		block_group = btrfs_find_block_group(root,
						     trans->block_group, 0,
						     data);
	}

check_failed:
	if (block_group->data != data)
		WARN_ON(1);
C
Chris Mason 已提交
709 710
	if (block_group->last_alloc > search_start)
		search_start = block_group->last_alloc;
711
	btrfs_init_path(path);
712 713 714
	ins->objectid = search_start;
	ins->offset = 0;
	start_found = 0;
715
	ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
C
Chris Mason 已提交
716 717
	if (ret < 0)
		goto error;
C
Chris Mason 已提交
718

719 720
	if (path->slots[0] > 0)
		path->slots[0]--;
721

722
	while (1) {
723 724
		l = btrfs_buffer_leaf(path->nodes[0]);
		slot = path->slots[0];
725
		if (slot >= btrfs_header_nritems(&l->header)) {
726 727 728 729
			if (fill_prealloc) {
				info->extent_tree_prealloc_nr = 0;
				total_found = 0;
			}
730
			ret = btrfs_next_leaf(root, path);
731 732
			if (ret == 0)
				continue;
C
Chris Mason 已提交
733 734
			if (ret < 0)
				goto error;
735 736
			if (!start_found) {
				ins->objectid = search_start;
737
				ins->offset = (u64)-1 - search_start;
738 739 740 741 742
				start_found = 1;
				goto check_pending;
			}
			ins->objectid = last_block > search_start ?
					last_block : search_start;
743
			ins->offset = (u64)-1 - ins->objectid;
744 745
			goto check_pending;
		}
C
Chris Mason 已提交
746
		btrfs_disk_key_to_cpu(&key, &l->items[slot].key);
C
Chris Mason 已提交
747 748
		if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY)
			goto next;
C
Chris Mason 已提交
749
		if (key.objectid >= search_start) {
750
			if (start_found) {
751 752
				if (last_block < search_start)
					last_block = search_start;
C
Chris Mason 已提交
753
				hole_size = key.objectid - last_block;
C
Chris Mason 已提交
754
				if (hole_size >= num_blocks) {
755
					ins->objectid = last_block;
C
Chris Mason 已提交
756
					ins->offset = hole_size;
757 758
					goto check_pending;
				}
759
			}
760
		}
761
		start_found = 1;
C
Chris Mason 已提交
762
		last_block = key.objectid + key.offset;
C
Chris Mason 已提交
763 764 765 766 767 768 769
		if (last_block >= block_group->key.objectid +
		    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 已提交
770
next:
771
		path->slots[0]++;
772 773 774 775 776 777
	}
	// FIXME -ENOSPC
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
	 */
778
	btrfs_release_path(root, path);
779
	BUG_ON(ins->objectid < search_start);
780
	if (ins->objectid >= btrfs_super_total_blocks(info->disk_super)) {
C
Chris Mason 已提交
781
		if (full_scan)
782
			return -ENOSPC;
C
Chris Mason 已提交
783 784 785
		search_start = orig_search_start;
		full_scan = 1;
		goto new_group;
786
	}
C
Chris Mason 已提交
787
	for (test_block = ins->objectid;
788 789
	     test_block < ins->objectid + num_blocks; test_block++) {
		if (test_radix_bit(&info->pinned_radix, test_block)) {
C
Chris Mason 已提交
790
			search_start = test_block + 1;
C
Chris Mason 已提交
791
			goto new_group;
792 793
		}
	}
794 795 796 797 798 799 800 801
	if (!fill_prealloc && info->extent_tree_insert_nr) {
		u64 last =
		  info->extent_tree_insert[info->extent_tree_insert_nr - 1];
		if (ins->objectid + num_blocks >
		    info->extent_tree_insert[0] &&
		    ins->objectid <= last) {
			search_start = last + 1;
			WARN_ON(1);
C
Chris Mason 已提交
802
			goto new_group;
803 804 805 806 807 808 809 810 811
		}
	}
	if (!fill_prealloc && info->extent_tree_prealloc_nr) {
		u64 first =
		  info->extent_tree_prealloc[info->extent_tree_prealloc_nr - 1];
		if (ins->objectid + num_blocks > first &&
		    ins->objectid <= info->extent_tree_prealloc[0]) {
			search_start = info->extent_tree_prealloc[0] + 1;
			WARN_ON(1);
C
Chris Mason 已提交
812
			goto new_group;
813 814 815 816 817 818 819 820 821
		}
	}
	if (fill_prealloc) {
		int nr;
		test_block = ins->objectid;
		while(test_block < ins->objectid + ins->offset &&
		      total_found < total_needed) {
			nr = total_needed - total_found - 1;
			BUG_ON(nr < 0);
C
Chris Mason 已提交
822
			info->extent_tree_prealloc[nr] = test_block;
823 824 825 826 827
			total_found++;
			test_block++;
		}
		if (total_found < total_needed) {
			search_start = test_block;
C
Chris Mason 已提交
828
			goto new_group;
829
		}
C
Chris Mason 已提交
830 831
		info->extent_tree_prealloc_nr = total_found;
	}
C
Chris Mason 已提交
832 833
	block_group = lookup_block_group(info, ins->objectid);
	if (block_group) {
C
Chris Mason 已提交
834 835 836
		block_group->last_alloc = ins->objectid;
		if (!data)
			trans->block_group = block_group;
837
	}
C
Chris Mason 已提交
838
	ins->offset = num_blocks;
839
	btrfs_free_path(path);
840
	return 0;
C
Chris Mason 已提交
841 842 843 844 845 846 847 848 849 850 851 852

new_group:
	if (search_start >= btrfs_super_total_blocks(info->disk_super)) {
		search_start = orig_search_start;
		full_scan = 1;
	}
	block_group = lookup_block_group(info, search_start);
	if (!full_scan)
		block_group = btrfs_find_block_group(root, block_group,
						     search_start, data);
	goto check_failed;

C
Chris Mason 已提交
853
error:
854 855
	btrfs_release_path(root, path);
	btrfs_free_path(path);
C
Chris Mason 已提交
856
	return ret;
857 858 859 860 861 862 863 864
}
/*
 * 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.
 */
865 866
int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
		       struct btrfs_root *root, u64 owner,
867
		       u64 num_blocks, u64 search_start,
C
Chris Mason 已提交
868
		       u64 search_end, struct btrfs_key *ins, int data)
869 870 871
{
	int ret;
	int pending_ret;
872 873 874
	u64 super_blocks_used;
	struct btrfs_fs_info *info = root->fs_info;
	struct btrfs_root *extent_root = info->extent_root;
C
Chris Mason 已提交
875
	struct btrfs_extent_item extent_item;
876
	struct btrfs_key prealloc_key;
C
Chris Mason 已提交
877

C
Chris Mason 已提交
878
	btrfs_set_extent_refs(&extent_item, 1);
879
	btrfs_set_extent_owner(&extent_item, owner);
880

C
Chris Mason 已提交
881
	if (root == extent_root) {
882 883
		int nr;
		BUG_ON(info->extent_tree_prealloc_nr == 0);
C
Chris Mason 已提交
884 885
		BUG_ON(num_blocks != 1);
		ins->offset = 1;
886 887 888 889 890
		info->extent_tree_prealloc_nr--;
		nr = info->extent_tree_prealloc_nr;
		ins->objectid = info->extent_tree_prealloc[nr];
		info->extent_tree_insert[info->extent_tree_insert_nr++] =
			ins->objectid;
C
Chris Mason 已提交
891 892 893
		ret = update_block_group(trans, root,
					 ins->objectid, ins->offset, 1);
		BUG_ON(ret);
894 895
		return 0;
	}
896
	/* do the real allocation */
897
	ret = find_free_extent(trans, root, num_blocks, search_start,
C
Chris Mason 已提交
898
			       search_end, ins, data);
C
Chris Mason 已提交
899 900
	if (ret)
		return ret;
901

902 903
	/* then do prealloc for the extent tree */
	ret = find_free_extent(trans, root, 0, ins->objectid + ins->offset,
C
Chris Mason 已提交
904
			       search_end, &prealloc_key, 0);
905 906 907
	if (ret)
		return ret;

908 909 910
	super_blocks_used = btrfs_super_blocks_used(info->disk_super);
	btrfs_set_super_blocks_used(info->disk_super, super_blocks_used +
				    num_blocks);
911 912
	ret = btrfs_insert_item(trans, extent_root, ins, &extent_item,
				sizeof(extent_item));
C
Chris Mason 已提交
913

914
	finish_current_insert(trans, extent_root);
C
Chris Mason 已提交
915
	pending_ret = del_pending_extents(trans, extent_root);
C
Chris Mason 已提交
916 917 918 919
	if (ret)
		return ret;
	if (pending_ret)
		return pending_ret;
C
Chris Mason 已提交
920
	ret = update_block_group(trans, root, ins->objectid, ins->offset, 1);
C
Chris Mason 已提交
921
	return 0;
922 923 924 925 926 927
}

/*
 * helper function to allocate a block for a given tree
 * returns the tree buffer or NULL.
 */
C
Chris Mason 已提交
928
struct buffer_head *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
929
					   struct btrfs_root *root, u64 hint)
930
{
C
Chris Mason 已提交
931
	struct btrfs_key ins;
932
	int ret;
C
Chris Mason 已提交
933
	struct buffer_head *buf;
934

935
	ret = btrfs_alloc_extent(trans, root, root->root_key.objectid,
C
Chris Mason 已提交
936
				 1, hint, (unsigned long)-1, &ins, 0);
937 938 939 940
	if (ret) {
		BUG();
		return NULL;
	}
C
Chris Mason 已提交
941
	BUG_ON(ret);
942
	buf = btrfs_find_create_tree_block(root, ins.objectid);
943
	set_buffer_uptodate(buf);
C
Chris Mason 已提交
944
	set_buffer_checked(buf);
945
	set_radix_bit(&trans->transaction->dirty_pages, buf->b_page->index);
946 947
	return buf;
}
948

949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966
static int drop_leaf_ref(struct btrfs_trans_handle *trans,
			 struct btrfs_root *root, struct buffer_head *cur)
{
	struct btrfs_disk_key *key;
	struct btrfs_leaf *leaf;
	struct btrfs_file_extent_item *fi;
	int i;
	int nritems;
	int ret;

	BUG_ON(!btrfs_is_leaf(btrfs_buffer_node(cur)));
	leaf = btrfs_buffer_leaf(cur);
	nritems = btrfs_header_nritems(&leaf->header);
	for (i = 0; i < nritems; i++) {
		key = &leaf->items[i].key;
		if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY)
			continue;
		fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
967 968
		if (btrfs_file_extent_type(fi) == BTRFS_FILE_EXTENT_INLINE)
			continue;
969 970 971 972 973 974 975 976 977 978 979 980 981
		/*
		 * FIXME make sure to insert a trans record that
		 * repeats the snapshot del on crash
		 */
		ret = btrfs_free_extent(trans, root,
					btrfs_file_extent_disk_blocknr(fi),
					btrfs_file_extent_disk_num_blocks(fi),
					0);
		BUG_ON(ret);
	}
	return 0;
}

C
Chris Mason 已提交
982 983 984 985
/*
 * helper function for drop_snapshot, this walks down the tree dropping ref
 * counts as it goes.
 */
986 987
static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
			  *root, struct btrfs_path *path, int *level)
C
Chris Mason 已提交
988
{
C
Chris Mason 已提交
989 990
	struct buffer_head *next;
	struct buffer_head *cur;
C
Chris Mason 已提交
991 992 993 994
	u64 blocknr;
	int ret;
	u32 refs;

995 996
	WARN_ON(*level < 0);
	WARN_ON(*level >= BTRFS_MAX_LEVEL);
C
Chris Mason 已提交
997
	ret = lookup_extent_ref(trans, root, bh_blocknr(path->nodes[*level]),
998
			       1, &refs);
C
Chris Mason 已提交
999 1000 1001
	BUG_ON(ret);
	if (refs > 1)
		goto out;
C
Chris Mason 已提交
1002 1003 1004
	/*
	 * walk down to the last node level and free all the leaves
	 */
1005
	while(*level >= 0) {
1006 1007
		WARN_ON(*level < 0);
		WARN_ON(*level >= BTRFS_MAX_LEVEL);
C
Chris Mason 已提交
1008
		cur = path->nodes[*level];
C
Chris Mason 已提交
1009 1010
		if (btrfs_header_level(btrfs_buffer_header(cur)) != *level)
			WARN_ON(1);
1011
		if (path->slots[*level] >=
C
Chris Mason 已提交
1012
		    btrfs_header_nritems(btrfs_buffer_header(cur)))
C
Chris Mason 已提交
1013
			break;
1014 1015 1016 1017 1018
		if (*level == 0) {
			ret = drop_leaf_ref(trans, root, cur);
			BUG_ON(ret);
			break;
		}
C
Chris Mason 已提交
1019 1020
		blocknr = btrfs_node_blockptr(btrfs_buffer_node(cur),
					      path->slots[*level]);
C
Chris Mason 已提交
1021
		ret = lookup_extent_ref(trans, root, blocknr, 1, &refs);
1022 1023
		BUG_ON(ret);
		if (refs != 1) {
C
Chris Mason 已提交
1024
			path->slots[*level]++;
1025
			ret = btrfs_free_extent(trans, root, blocknr, 1, 1);
C
Chris Mason 已提交
1026 1027 1028 1029
			BUG_ON(ret);
			continue;
		}
		next = read_tree_block(root, blocknr);
1030
		WARN_ON(*level <= 0);
C
Chris Mason 已提交
1031
		if (path->nodes[*level-1])
C
Chris Mason 已提交
1032
			btrfs_block_release(root, path->nodes[*level-1]);
C
Chris Mason 已提交
1033
		path->nodes[*level-1] = next;
C
Chris Mason 已提交
1034
		*level = btrfs_header_level(btrfs_buffer_header(next));
C
Chris Mason 已提交
1035 1036 1037
		path->slots[*level] = 0;
	}
out:
1038 1039
	WARN_ON(*level < 0);
	WARN_ON(*level >= BTRFS_MAX_LEVEL);
1040
	ret = btrfs_free_extent(trans, root,
1041
				bh_blocknr(path->nodes[*level]), 1, 1);
C
Chris Mason 已提交
1042
	btrfs_block_release(root, path->nodes[*level]);
C
Chris Mason 已提交
1043 1044 1045 1046 1047 1048
	path->nodes[*level] = NULL;
	*level += 1;
	BUG_ON(ret);
	return 0;
}

C
Chris Mason 已提交
1049 1050 1051 1052 1053
/*
 * 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
 */
1054 1055
static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root
			*root, struct btrfs_path *path, int *level)
C
Chris Mason 已提交
1056 1057 1058 1059
{
	int i;
	int slot;
	int ret;
C
Chris Mason 已提交
1060
	for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
C
Chris Mason 已提交
1061
		slot = path->slots[i];
C
Chris Mason 已提交
1062 1063
		if (slot < btrfs_header_nritems(
		    btrfs_buffer_header(path->nodes[i])) - 1) {
C
Chris Mason 已提交
1064 1065 1066 1067
			path->slots[i]++;
			*level = i;
			return 0;
		} else {
1068
			ret = btrfs_free_extent(trans, root,
1069
						bh_blocknr(path->nodes[*level]),
1070
						1, 1);
1071
			BUG_ON(ret);
C
Chris Mason 已提交
1072
			btrfs_block_release(root, path->nodes[*level]);
C
Chris Mason 已提交
1073
			path->nodes[*level] = NULL;
C
Chris Mason 已提交
1074 1075 1076 1077 1078 1079
			*level = i + 1;
		}
	}
	return 1;
}

C
Chris Mason 已提交
1080 1081 1082 1083 1084
/*
 * 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.
 */
1085
int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
C
Chris Mason 已提交
1086
			*root, struct buffer_head *snap)
C
Chris Mason 已提交
1087
{
1088
	int ret = 0;
C
Chris Mason 已提交
1089
	int wret;
C
Chris Mason 已提交
1090
	int level;
1091
	struct btrfs_path *path;
C
Chris Mason 已提交
1092 1093 1094
	int i;
	int orig_level;

1095 1096 1097
	path = btrfs_alloc_path();
	BUG_ON(!path);
	btrfs_init_path(path);
C
Chris Mason 已提交
1098

C
Chris Mason 已提交
1099
	level = btrfs_header_level(btrfs_buffer_header(snap));
C
Chris Mason 已提交
1100
	orig_level = level;
1101 1102
	path->nodes[level] = snap;
	path->slots[level] = 0;
C
Chris Mason 已提交
1103
	while(1) {
1104
		wret = walk_down_tree(trans, root, path, &level);
C
Chris Mason 已提交
1105
		if (wret > 0)
C
Chris Mason 已提交
1106
			break;
C
Chris Mason 已提交
1107 1108 1109
		if (wret < 0)
			ret = wret;

1110
		wret = walk_up_tree(trans, root, path, &level);
C
Chris Mason 已提交
1111
		if (wret > 0)
C
Chris Mason 已提交
1112
			break;
C
Chris Mason 已提交
1113 1114
		if (wret < 0)
			ret = wret;
C
Chris Mason 已提交
1115
		btrfs_btree_balance_dirty(root);
C
Chris Mason 已提交
1116
	}
C
Chris Mason 已提交
1117
	for (i = 0; i <= orig_level; i++) {
1118 1119
		if (path->nodes[i]) {
			btrfs_block_release(root, path->nodes[i]);
C
Chris Mason 已提交
1120
		}
C
Chris Mason 已提交
1121
	}
1122
	btrfs_free_path(path);
C
Chris Mason 已提交
1123
	return ret;
C
Chris Mason 已提交
1124
}
C
Chris Mason 已提交
1125

C
Chris Mason 已提交
1126
static int free_block_group_radix(struct radix_tree_root *radix)
C
Chris Mason 已提交
1127 1128 1129 1130 1131 1132
{
	int ret;
	struct btrfs_block_group_cache *cache[8];
	int i;

	while(1) {
C
Chris Mason 已提交
1133
		ret = radix_tree_gang_lookup(radix, (void **)cache, 0,
C
Chris Mason 已提交
1134 1135 1136 1137
					     ARRAY_SIZE(cache));
		if (!ret)
			break;
		for (i = 0; i < ret; i++) {
C
Chris Mason 已提交
1138
			radix_tree_delete(radix, cache[i]->key.objectid +
C
Chris Mason 已提交
1139 1140 1141 1142 1143 1144 1145
					  cache[i]->key.offset - 1);
			kfree(cache[i]);
		}
	}
	return 0;
}

C
Chris Mason 已提交
1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159
int btrfs_free_block_groups(struct btrfs_fs_info *info)
{
	int ret;
	int ret2;

	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;
	return 0;
}

C
Chris Mason 已提交
1160 1161 1162 1163 1164 1165 1166
int btrfs_read_block_groups(struct btrfs_root *root)
{
	struct btrfs_path *path;
	int ret;
	int err = 0;
	struct btrfs_block_group_item *bi;
	struct btrfs_block_group_cache *cache;
C
Chris Mason 已提交
1167 1168
	struct btrfs_fs_info *info = root->fs_info;
	struct radix_tree_root *radix;
C
Chris Mason 已提交
1169 1170 1171 1172
	struct btrfs_key key;
	struct btrfs_key found_key;
	struct btrfs_leaf *leaf;
	u64 group_size_blocks = BTRFS_BLOCK_GROUP_SIZE / root->blocksize;
1173
	u64 used;
C
Chris Mason 已提交
1174
	u64 nr = 0;
C
Chris Mason 已提交
1175

C
Chris Mason 已提交
1176
	root = info->extent_root;
C
Chris Mason 已提交
1177 1178 1179 1180 1181 1182 1183 1184 1185 1186
	key.objectid = 0;
	key.offset = group_size_blocks;
	key.flags = 0;
	btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);

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

	while(1) {
C
Chris Mason 已提交
1187
		ret = btrfs_search_slot(NULL, info->extent_root,
C
Chris Mason 已提交
1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204
					&key, path, 0, 0);
		if (ret != 0) {
			err = ret;
			break;
		}
		leaf = btrfs_buffer_leaf(path->nodes[0]);
		btrfs_disk_key_to_cpu(&found_key,
				      &leaf->items[path->slots[0]].key);
		cache = kmalloc(sizeof(*cache), GFP_NOFS);
		if (!cache) {
			err = -1;
			break;
		}
		bi = btrfs_item_ptr(leaf, path->slots[0],
				    struct btrfs_block_group_item);
		memcpy(&cache->item, bi, sizeof(*bi));
		memcpy(&cache->key, &found_key, sizeof(found_key));
1205 1206
		cache->last_alloc = cache->key.objectid;
		cache->first_free = cache->key.objectid;
C
Chris Mason 已提交
1207 1208
		cache->pinned = 0;
		cache->data = (nr & 1);
C
Chris Mason 已提交
1209 1210
		key.objectid = found_key.objectid + found_key.offset;
		btrfs_release_path(root, path);
C
Chris Mason 已提交
1211 1212 1213 1214 1215
		if (nr & 1)
			radix = &info->block_group_data_radix;
		else
			radix = &info->block_group_radix;
		ret = radix_tree_insert(radix, found_key.objectid +
C
Chris Mason 已提交
1216 1217 1218
					found_key.offset - 1,
					(void *)cache);
		BUG_ON(ret);
1219
		used = btrfs_block_group_used(bi);
C
Chris Mason 已提交
1220 1221
		if (used < (key.offset * 8) / 10) {
			radix_tree_tag_set(radix, found_key.objectid +
1222 1223 1224
					   found_key.offset - 1,
					   BTRFS_BLOCK_GROUP_AVAIL);
		}
C
Chris Mason 已提交
1225
		if (key.objectid >=
C
Chris Mason 已提交
1226
		    btrfs_super_total_blocks(info->disk_super))
C
Chris Mason 已提交
1227
			break;
C
Chris Mason 已提交
1228
		nr++;
C
Chris Mason 已提交
1229 1230 1231 1232 1233
	}

	btrfs_free_path(path);
	return 0;
}