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

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

25 26 27 28
#define BLOCK_GROUP_DATA EXTENT_WRITEBACK
#define BLOCK_GROUP_METADATA EXTENT_UPTODATE
#define BLOCK_GROUP_DIRTY EXTENT_DIRTY

29 30
static int finish_current_insert(struct btrfs_trans_handle *trans, struct
				 btrfs_root *extent_root);
C
Chris Mason 已提交
31 32
static int del_pending_extents(struct btrfs_trans_handle *trans, struct
			       btrfs_root *extent_root);
33

34 35 36 37 38 39
static int cache_block_group(struct btrfs_root *root,
			     struct btrfs_block_group_cache *block_group)
{
	struct btrfs_path *path;
	int ret;
	struct btrfs_key key;
40
	struct extent_buffer *leaf;
41
	struct extent_map_tree *free_space_cache;
42 43 44
	int slot;
	u64 last = 0;
	u64 hole_size;
45
	u64 first_free;
46 47
	int found = 0;

48 49 50
	if (!block_group)
		return 0;

51
	root = root->fs_info->extent_root;
52
	free_space_cache = &root->fs_info->free_space_cache;
53 54 55

	if (block_group->cached)
		return 0;
56

57 58 59
	path = btrfs_alloc_path();
	if (!path)
		return -ENOMEM;
60

61
	path->reada = 2;
62
	first_free = block_group->key.objectid;
63 64
	key.objectid = block_group->key.objectid;
	key.offset = 0;
65

66 67
	btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
	ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
68

69 70
	if (ret < 0)
		return ret;
71

72 73
	if (ret && path->slots[0] > 0)
		path->slots[0]--;
74

75
	while(1) {
76
		leaf = path->nodes[0];
77
		slot = path->slots[0];
78
		if (slot >= btrfs_header_nritems(leaf)) {
79
			ret = btrfs_next_leaf(root, path);
80 81
			if (ret < 0)
				goto err;
82
			if (ret == 0) {
83
				continue;
84
			} else {
85 86 87
				break;
			}
		}
88

89
		btrfs_item_key_to_cpu(leaf, &key, slot);
90 91 92 93 94 95
		if (key.objectid < block_group->key.objectid) {
			if (key.objectid + key.offset > first_free)
				first_free = key.objectid + key.offset;
			goto next;
		}

96 97 98 99
		if (key.objectid >= block_group->key.objectid +
		    block_group->key.offset) {
			break;
		}
100

101 102
		if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) {
			if (!found) {
103
				last = first_free;
104 105
				found = 1;
			}
106 107 108 109 110
			if (key.objectid > last) {
				hole_size = key.objectid - last;
				set_extent_dirty(free_space_cache, last,
						 last + hole_size - 1,
						 GFP_NOFS);
111 112
			}
			last = key.objectid + key.offset;
113
		}
114
next:
115 116 117
		path->slots[0]++;
	}

118 119 120 121 122 123
	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;
124 125
		set_extent_dirty(free_space_cache, last,
				 last + hole_size - 1, GFP_NOFS);
126
	}
127
	block_group->cached = 1;
128
err:
129 130 131 132
	btrfs_free_path(path);
	return 0;
}

133 134
struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
							 btrfs_fs_info *info,
135
							 u64 bytenr)
C
Chris Mason 已提交
136
{
137 138 139 140 141
	struct extent_map_tree *block_group_cache;
	struct btrfs_block_group_cache *block_group = NULL;
	u64 ptr;
	u64 start;
	u64 end;
C
Chris Mason 已提交
142 143
	int ret;

144 145
	block_group_cache = &info->block_group_cache;
	ret = find_first_extent_bit(block_group_cache,
146
				    bytenr, &start, &end,
147
				    BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA);
C
Chris Mason 已提交
148
	if (ret) {
149
		return NULL;
C
Chris Mason 已提交
150
	}
151 152 153 154
	ret = get_state_private(block_group_cache, start, &ptr);
	if (ret)
		return NULL;

J
Jens Axboe 已提交
155
	block_group = (struct btrfs_block_group_cache *)(unsigned long)ptr;
Y
Yan 已提交
156
	if (block_group->key.objectid <= bytenr && bytenr <
157 158
	    block_group->key.objectid + block_group->key.offset)
		return block_group;
C
Chris Mason 已提交
159 160
	return NULL;
}
161 162
static u64 find_search_start(struct btrfs_root *root,
			     struct btrfs_block_group_cache **cache_ret,
163 164
			     u64 search_start, int num,
			     int data, int full_scan)
165 166 167
{
	int ret;
	struct btrfs_block_group_cache *cache = *cache_ret;
168
	u64 last;
169 170
	u64 start = 0;
	u64 end = 0;
171
	u64 cache_miss = 0;
172
	int wrapped = 0;
173

174 175 176 177 178
	if (!cache) {
		cache = btrfs_lookup_block_group(root->fs_info, search_start);
		if (!cache)
			return search_start;
	}
179
again:
180 181 182
	ret = cache_block_group(root, cache);
	if (ret)
		goto out;
183

184 185
	last = max(search_start, cache->key.objectid);

186
	while(1) {
187 188
		ret = find_first_extent_bit(&root->fs_info->free_space_cache,
					    last, &start, &end, EXTENT_DIRTY);
189
		if (ret) {
190 191
			if (!cache_miss)
				cache_miss = last;
192 193
			goto new_group;
		}
194 195 196

		start = max(last, start);
		last = end + 1;
197 198 199
		if (last - start < num) {
			if (last == cache->key.objectid + cache->key.offset)
				cache_miss = start;
200
			continue;
201 202
		}
		if (data != BTRFS_BLOCK_GROUP_MIXED &&
Y
Yan 已提交
203
		    start + num > cache->key.objectid + cache->key.offset)
204
			goto new_group;
205
		return start;
206 207
	}
out:
208
	return search_start;
209 210

new_group:
211
	last = cache->key.objectid + cache->key.offset;
212
wrapped:
213
	cache = btrfs_lookup_block_group(root->fs_info, last);
214
	if (!cache) {
215
no_cache:
216 217 218 219 220 221
		if (!wrapped) {
			wrapped = 1;
			last = search_start;
			data = BTRFS_BLOCK_GROUP_MIXED;
			goto wrapped;
		}
222
		return search_start;
223
	}
224 225 226 227 228
	if (cache_miss && !cache->cached) {
		cache_block_group(root, cache);
		last = cache_miss;
		cache = btrfs_lookup_block_group(root->fs_info, last);
	}
229 230
	if (!full_scan)
		cache = btrfs_find_block_group(root, cache, last, data, 0);
231 232
	if (!cache)
		goto no_cache;
233
	*cache_ret = cache;
234
	cache_miss = 0;
235 236 237
	goto again;
}

C
Chris Mason 已提交
238 239
static u64 div_factor(u64 num, int factor)
{
240 241
	if (factor == 10)
		return num;
C
Chris Mason 已提交
242 243 244 245 246
	num *= factor;
	do_div(num, 10);
	return num;
}

247 248
struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
						 struct btrfs_block_group_cache
C
Chris Mason 已提交
249
						 *hint, u64 search_start,
250
						 int data, int owner)
C
Chris Mason 已提交
251
{
252 253
	struct btrfs_block_group_cache *cache;
	struct extent_map_tree *block_group_cache;
254
	struct btrfs_block_group_cache *found_group = NULL;
C
Chris Mason 已提交
255 256
	struct btrfs_fs_info *info = root->fs_info;
	u64 used;
257 258
	u64 last = 0;
	u64 hint_last;
259 260 261 262 263
	u64 start;
	u64 end;
	u64 free_check;
	u64 ptr;
	int bit;
C
Chris Mason 已提交
264
	int ret;
265
	int full_search = 0;
266
	int factor = 8;
C
Chris Mason 已提交
267
	int data_swap = 0;
268

269 270
	block_group_cache = &info->block_group_cache;

271
	if (!owner)
272
		factor = 8;
C
Chris Mason 已提交
273

274
	if (data == BTRFS_BLOCK_GROUP_MIXED) {
275
		bit = BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA;
276 277
		factor = 10;
	} else if (data)
278 279 280
		bit = BLOCK_GROUP_DATA;
	else
		bit = BLOCK_GROUP_METADATA;
C
Chris Mason 已提交
281 282 283

	if (search_start) {
		struct btrfs_block_group_cache *shint;
284
		shint = btrfs_lookup_block_group(info, search_start);
285 286
		if (shint && (shint->data == data ||
			      shint->data == BTRFS_BLOCK_GROUP_MIXED)) {
C
Chris Mason 已提交
287
			used = btrfs_block_group_used(&shint->item);
288 289
			if (used + shint->pinned <
			    div_factor(shint->key.offset, factor)) {
C
Chris Mason 已提交
290 291 292 293
				return shint;
			}
		}
	}
294 295
	if (hint && (hint->data == data ||
		     hint->data == BTRFS_BLOCK_GROUP_MIXED)) {
296
		used = btrfs_block_group_used(&hint->item);
297 298
		if (used + hint->pinned <
		    div_factor(hint->key.offset, factor)) {
299 300
			return hint;
		}
301
		last = hint->key.objectid + hint->key.offset;
302 303
		hint_last = last;
	} else {
304 305 306 307 308 309
		if (hint)
			hint_last = max(hint->key.objectid, search_start);
		else
			hint_last = search_start;

		last = hint_last;
310 311
	}
again:
C
Chris Mason 已提交
312
	while(1) {
313 314 315
		ret = find_first_extent_bit(block_group_cache, last,
					    &start, &end, bit);
		if (ret)
C
Chris Mason 已提交
316
			break;
317 318 319 320 321

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

J
Jens Axboe 已提交
322
		cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
323 324 325 326 327 328 329
		last = cache->key.objectid + cache->key.offset;
		used = btrfs_block_group_used(&cache->item);

		if (full_search)
			free_check = cache->key.offset;
		else
			free_check = div_factor(cache->key.offset, factor);
330
		if (used + cache->pinned < free_check) {
331 332
			found_group = cache;
			goto found;
C
Chris Mason 已提交
333
		}
334
		cond_resched();
C
Chris Mason 已提交
335
	}
336
	if (!full_search) {
C
Chris Mason 已提交
337
		last = search_start;
338 339 340
		full_search = 1;
		goto again;
	}
C
Chris Mason 已提交
341 342
	if (!data_swap) {
		data_swap = 1;
343
		bit = BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA;
C
Chris Mason 已提交
344 345 346
		last = search_start;
		goto again;
	}
C
Chris Mason 已提交
347
found:
348
	return found_group;
C
Chris Mason 已提交
349 350
}

C
Chris Mason 已提交
351 352
int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
				struct btrfs_root *root,
353
				u64 bytenr, u64 num_bytes)
C
Chris Mason 已提交
354
{
355
	struct btrfs_path *path;
C
Chris Mason 已提交
356
	int ret;
C
Chris Mason 已提交
357
	struct btrfs_key key;
358
	struct extent_buffer *l;
C
Chris Mason 已提交
359
	struct btrfs_extent_item *item;
C
Chris Mason 已提交
360
	u32 refs;
C
Chris Mason 已提交
361

362
	WARN_ON(num_bytes < root->sectorsize);
363
	path = btrfs_alloc_path();
364 365
	if (!path)
		return -ENOMEM;
366

367
	key.objectid = bytenr;
368
	btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
369
	key.offset = num_bytes;
370
	ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
371
				0, 1);
372 373
	if (ret < 0)
		return ret;
374
	if (ret != 0) {
375
		BUG();
376
	}
C
Chris Mason 已提交
377
	BUG_ON(ret != 0);
378
	l = path->nodes[0];
379
	item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
380 381
	refs = btrfs_extent_refs(l, item);
	btrfs_set_extent_refs(l, item, refs + 1);
382
	btrfs_mark_buffer_dirty(path->nodes[0]);
383

384 385
	btrfs_release_path(root->fs_info->extent_root, path);
	btrfs_free_path(path);
386
	finish_current_insert(trans, root->fs_info->extent_root);
C
Chris Mason 已提交
387
	del_pending_extents(trans, root->fs_info->extent_root);
C
Chris Mason 已提交
388 389 390
	return 0;
}

391 392 393 394 395 396 397 398
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 已提交
399
static int lookup_extent_ref(struct btrfs_trans_handle *trans,
400 401
			     struct btrfs_root *root, u64 bytenr,
			     u64 num_bytes, u32 *refs)
402
{
403
	struct btrfs_path *path;
404
	int ret;
C
Chris Mason 已提交
405
	struct btrfs_key key;
406
	struct extent_buffer *l;
C
Chris Mason 已提交
407
	struct btrfs_extent_item *item;
408

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

C
Chris Mason 已提交
431 432 433
int btrfs_inc_root_ref(struct btrfs_trans_handle *trans,
		       struct btrfs_root *root)
{
434 435
	return btrfs_inc_extent_ref(trans, root, root->node->start,
				    root->node->len);
C
Chris Mason 已提交
436 437
}

438
int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
439
		  struct extent_buffer *buf)
C
Chris Mason 已提交
440
{
441
	u64 bytenr;
442 443
	u32 nritems;
	struct btrfs_key key;
444
	struct btrfs_file_extent_item *fi;
C
Chris Mason 已提交
445
	int i;
446
	int level;
447
	int ret;
448 449
	int faili;
	int err;
450

451
	if (!root->ref_cows)
452
		return 0;
453

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

C
Chris Mason 已提交
517 518 519 520 521 522 523 524
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;
525 526
	unsigned long bi;
	struct extent_buffer *leaf;
C
Chris Mason 已提交
527 528

	ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
529 530
	if (ret < 0)
		goto fail;
C
Chris Mason 已提交
531
	BUG_ON(ret);
532 533 534 535 536

	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 已提交
537
	btrfs_release_path(extent_root, path);
538
fail:
C
Chris Mason 已提交
539 540 541 542 543 544 545 546 547 548
	finish_current_insert(trans, extent_root);
	pending_ret = del_pending_extents(trans, extent_root);
	if (ret)
		return ret;
	if (pending_ret)
		return pending_ret;
	return 0;

}

549 550
int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
				   struct btrfs_root *root)
C
Chris Mason 已提交
551
{
552 553
	struct extent_map_tree *block_group_cache;
	struct btrfs_block_group_cache *cache;
C
Chris Mason 已提交
554 555 556 557
	int ret;
	int err = 0;
	int werr = 0;
	struct btrfs_path *path;
558 559 560 561
	u64 last = 0;
	u64 start;
	u64 end;
	u64 ptr;
C
Chris Mason 已提交
562

563
	block_group_cache = &root->fs_info->block_group_cache;
C
Chris Mason 已提交
564 565 566 567 568
	path = btrfs_alloc_path();
	if (!path)
		return -ENOMEM;

	while(1) {
569 570 571
		ret = find_first_extent_bit(block_group_cache, last,
					    &start, &end, BLOCK_GROUP_DIRTY);
		if (ret)
C
Chris Mason 已提交
572
			break;
573

574 575 576 577 578
		last = end + 1;
		ret = get_state_private(block_group_cache, start, &ptr);
		if (ret)
			break;

J
Jens Axboe 已提交
579
		cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
580 581 582 583 584 585 586 587 588 589
		err = write_one_cache_group(trans, root,
					    path, cache);
		/*
		 * if we fail to write the cache group, we want
		 * to keep it marked dirty in hopes that a later
		 * write will work
		 */
		if (err) {
			werr = err;
			continue;
C
Chris Mason 已提交
590
		}
591 592
		clear_extent_bits(block_group_cache, start, end,
				  BLOCK_GROUP_DIRTY, GFP_NOFS);
C
Chris Mason 已提交
593 594 595 596 597 598 599
	}
	btrfs_free_path(path);
	return werr;
}

static int update_block_group(struct btrfs_trans_handle *trans,
			      struct btrfs_root *root,
600 601
			      u64 bytenr, u64 num_bytes, int alloc,
			      int mark_free, int data)
C
Chris Mason 已提交
602 603 604
{
	struct btrfs_block_group_cache *cache;
	struct btrfs_fs_info *info = root->fs_info;
605
	u64 total = num_bytes;
C
Chris Mason 已提交
606
	u64 old_val;
607
	u64 byte_in_group;
608 609
	u64 start;
	u64 end;
C
Chris Mason 已提交
610

C
Chris Mason 已提交
611
	while(total) {
612
		cache = btrfs_lookup_block_group(info, bytenr);
C
Chris Mason 已提交
613
		if (!cache) {
C
Chris Mason 已提交
614
			return -1;
C
Chris Mason 已提交
615
		}
616 617
		byte_in_group = bytenr - cache->key.objectid;
		WARN_ON(byte_in_group > cache->key.offset);
618 619 620 621
		start = cache->key.objectid;
		end = start + cache->key.offset - 1;
		set_extent_bits(&info->block_group_cache, start, end,
				BLOCK_GROUP_DIRTY, GFP_NOFS);
C
Chris Mason 已提交
622 623

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

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

708
int btrfs_copy_pinned(struct btrfs_root *root, struct extent_map_tree *copy)
C
Chris Mason 已提交
709 710
{
	u64 last = 0;
711 712 713
	u64 start;
	u64 end;
	struct extent_map_tree *pinned_extents = &root->fs_info->pinned_extents;
C
Chris Mason 已提交
714 715 716
	int ret;

	while(1) {
717 718 719
		ret = find_first_extent_bit(pinned_extents, last,
					    &start, &end, EXTENT_DIRTY);
		if (ret)
C
Chris Mason 已提交
720
			break;
721 722
		set_extent_dirty(copy, start, end, GFP_NOFS);
		last = end + 1;
C
Chris Mason 已提交
723 724 725 726 727 728
	}
	return 0;
}

int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
			       struct btrfs_root *root,
729
			       struct extent_map_tree *unpin)
730
{
731 732
	u64 start;
	u64 end;
733
	int ret;
734 735
	struct extent_map_tree *free_space_cache;
	free_space_cache = &root->fs_info->free_space_cache;
736 737

	while(1) {
738 739 740
		ret = find_first_extent_bit(unpin, 0, &start, &end,
					    EXTENT_DIRTY);
		if (ret)
741
			break;
742
		update_pinned_extents(root, start, end + 1 - start, 0);
743 744
		clear_extent_dirty(unpin, start, end, GFP_NOFS);
		set_extent_dirty(free_space_cache, start, end, GFP_NOFS);
745 746 747 748
	}
	return 0;
}

749 750
static int finish_current_insert(struct btrfs_trans_handle *trans, struct
				 btrfs_root *extent_root)
C
Chris Mason 已提交
751
{
C
Chris Mason 已提交
752
	struct btrfs_key ins;
C
Chris Mason 已提交
753
	struct btrfs_extent_item extent_item;
C
Chris Mason 已提交
754
	int ret;
755 756 757
	int err = 0;
	u64 start;
	u64 end;
758
	struct btrfs_fs_info *info = extent_root->fs_info;
C
Chris Mason 已提交
759

760
	btrfs_set_stack_extent_refs(&extent_item, 1);
761
	btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
762 763
	btrfs_set_stack_extent_owner(&extent_item,
				     extent_root->root_key.objectid);
C
Chris Mason 已提交
764

765
	while(1) {
766 767 768
		ret = find_first_extent_bit(&info->extent_ins, 0, &start,
					    &end, EXTENT_LOCKED);
		if (ret)
769 770
			break;

771 772 773 774 775 776
		ins.objectid = start;
		ins.offset = end + 1 - start;
		err = btrfs_insert_item(trans, extent_root, &ins,
					&extent_item, sizeof(extent_item));
		clear_extent_bits(&info->extent_ins, start, end, EXTENT_LOCKED,
				  GFP_NOFS);
C
Chris Mason 已提交
777 778 779 780
	}
	return 0;
}

781 782
static int pin_down_bytes(struct btrfs_root *root, u64 bytenr, u32 num_bytes,
			  int pending)
C
Chris Mason 已提交
783
{
784
	int err = 0;
785
	struct extent_buffer *buf;
C
Chris Mason 已提交
786

C
Chris Mason 已提交
787
	if (!pending) {
788
		buf = btrfs_find_tree_block(root, bytenr, num_bytes);
789 790
		if (buf) {
			if (btrfs_buffer_uptodate(buf)) {
C
Chris Mason 已提交
791 792
				u64 transid =
				    root->fs_info->running_transaction->transid;
793 794
				if (btrfs_header_generation(buf) == transid) {
					free_extent_buffer(buf);
795
					return 1;
C
Chris Mason 已提交
796
				}
C
Chris Mason 已提交
797
			}
798
			free_extent_buffer(buf);
C
Chris Mason 已提交
799
		}
800
		update_pinned_extents(root, bytenr, num_bytes, 1);
C
Chris Mason 已提交
801
	} else {
802
		set_extent_bits(&root->fs_info->pending_del,
803 804
				bytenr, bytenr + num_bytes - 1,
				EXTENT_LOCKED, GFP_NOFS);
C
Chris Mason 已提交
805
	}
C
Chris Mason 已提交
806
	BUG_ON(err < 0);
C
Chris Mason 已提交
807 808 809
	return 0;
}

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

826
	key.objectid = bytenr;
827
	btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
828
	key.offset = num_bytes;
829

830
	path = btrfs_alloc_path();
831 832
	if (!path)
		return -ENOMEM;
833

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

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

		if (pin) {
853
			ret = pin_down_bytes(root, bytenr, num_bytes, 0);
854 855 856
			if (ret > 0)
				mark_free = 1;
			BUG_ON(ret < 0);
C
Chris Mason 已提交
857 858
		}

859
		/* block accounting for super block */
860 861 862
		super_used = btrfs_super_bytes_used(&info->super_copy);
		btrfs_set_super_bytes_used(&info->super_copy,
					   super_used - num_bytes);
863 864

		/* block accounting for root item */
865
		root_used = btrfs_root_used(&root->root_item);
866
		btrfs_set_root_used(&root->root_item,
867
					   root_used - num_bytes);
868

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

/*
 * find all the blocks marked as pending in the radix tree and remove
 * them from the extent map
 */
886 887
static int del_pending_extents(struct btrfs_trans_handle *trans, struct
			       btrfs_root *extent_root)
888 889
{
	int ret;
C
Chris Mason 已提交
890
	int err = 0;
891 892 893 894
	u64 start;
	u64 end;
	struct extent_map_tree *pending_del;
	struct extent_map_tree *pinned_extents;
C
Chris Mason 已提交
895

896 897
	pending_del = &extent_root->fs_info->pending_del;
	pinned_extents = &extent_root->fs_info->pinned_extents;
898 899

	while(1) {
900 901 902
		ret = find_first_extent_bit(pending_del, 0, &start, &end,
					    EXTENT_LOCKED);
		if (ret)
903
			break;
904
		update_pinned_extents(extent_root, start, end + 1 - start, 1);
905 906 907 908 909 910
		clear_extent_bits(pending_del, start, end, EXTENT_LOCKED,
				  GFP_NOFS);
		ret = __free_extent(trans, extent_root,
				     start, end + 1 - start, 0, 0);
		if (ret)
			err = ret;
911
	}
C
Chris Mason 已提交
912
	return err;
913 914 915 916 917
}

/*
 * remove an extent from the root, returns 0 on success
 */
918
int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
919
		      *root, u64 bytenr, u64 num_bytes, int pin)
920
{
921
	struct btrfs_root *extent_root = root->fs_info->extent_root;
922 923
	int pending_ret;
	int ret;
924

925
	WARN_ON(num_bytes < root->sectorsize);
926
	if (root == extent_root) {
927
		pin_down_bytes(root, bytenr, num_bytes, 1);
928 929
		return 0;
	}
930
	ret = __free_extent(trans, root, bytenr, num_bytes, pin, pin == 0);
C
Chris Mason 已提交
931
	pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
932 933 934
	return ret ? ret : pending_ret;
}

935 936 937 938 939 940 941
static u64 stripe_align(struct btrfs_root *root, u64 val)
{
	u64 mask = ((u64)root->stripesize - 1);
	u64 ret = (val + mask) & ~mask;
	return ret;
}

942 943 944 945
/*
 * 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
946
 * ins->flags = BTRFS_EXTENT_ITEM_KEY
947 948 949
 * ins->offset == number of blocks
 * Any available blocks before search_start are skipped.
 */
950
static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
951 952
			    *orig_root, u64 num_bytes, u64 empty_size,
			    u64 search_start, u64 search_end, u64 hint_byte,
953 954
			    struct btrfs_key *ins, u64 exclude_start,
			    u64 exclude_nr, int data)
955
{
956
	struct btrfs_path *path;
C
Chris Mason 已提交
957
	struct btrfs_key key;
958
	u64 hole_size = 0;
959 960
	u64 aligned;
	int ret;
961
	int slot = 0;
962
	u64 last_byte = 0;
C
Chris Mason 已提交
963
	u64 orig_search_start = search_start;
964
	int start_found;
965
	struct extent_buffer *l;
966
	struct btrfs_root * root = orig_root->fs_info->extent_root;
967
	struct btrfs_fs_info *info = root->fs_info;
968
	u64 total_needed = num_bytes;
C
Chris Mason 已提交
969
	int level;
C
Chris Mason 已提交
970
	struct btrfs_block_group_cache *block_group;
C
Chris Mason 已提交
971
	int full_scan = 0;
972
	int wrapped = 0;
973
	u64 cached_start;
974

975
	WARN_ON(num_bytes < root->sectorsize);
976 977
	btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);

978 979
	level = btrfs_header_level(root->node);

980
	if (num_bytes >= 32 * 1024 * 1024 && hint_byte) {
981 982 983
		data = BTRFS_BLOCK_GROUP_MIXED;
	}

C
Chris Mason 已提交
984
	if (search_end == (u64)-1)
985 986 987
		search_end = btrfs_super_total_bytes(&info->super_copy);
	if (hint_byte) {
		block_group = btrfs_lookup_block_group(info, hint_byte);
C
Chris Mason 已提交
988
		block_group = btrfs_find_block_group(root, block_group,
989
						     hint_byte, data, 1);
C
Chris Mason 已提交
990 991 992
	} else {
		block_group = btrfs_find_block_group(root,
						     trans->block_group, 0,
993
						     data, 1);
C
Chris Mason 已提交
994 995
	}

996
	total_needed += empty_size;
997
	path = btrfs_alloc_path();
C
Chris Mason 已提交
998
check_failed:
999 1000
	search_start = find_search_start(root, &block_group, search_start,
					 total_needed, data, full_scan);
1001
	search_start = stripe_align(root, search_start);
1002
	cached_start = search_start;
1003
	btrfs_init_path(path);
1004 1005 1006
	ins->objectid = search_start;
	ins->offset = 0;
	start_found = 0;
1007
	path->reada = 2;
1008

1009
	ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
C
Chris Mason 已提交
1010 1011
	if (ret < 0)
		goto error;
C
Chris Mason 已提交
1012

1013
	if (path->slots[0] > 0) {
1014
		path->slots[0]--;
1015 1016
	}

1017 1018 1019
	l = path->nodes[0];
	btrfs_item_key_to_cpu(l, &key, path->slots[0]);

1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036
	/*
	 * 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]--;
		}
	}
1037

1038
	while (1) {
1039
		l = path->nodes[0];
1040
		slot = path->slots[0];
1041
		if (slot >= btrfs_header_nritems(l)) {
1042
			ret = btrfs_next_leaf(root, path);
1043 1044
			if (ret == 0)
				continue;
C
Chris Mason 已提交
1045 1046
			if (ret < 0)
				goto error;
1047 1048 1049

			search_start = max(search_start,
					   block_group->key.objectid);
1050
			if (!start_found) {
1051 1052 1053 1054 1055 1056 1057
				aligned = stripe_align(root, search_start);
				ins->objectid = aligned;
				if (aligned >= search_end) {
					ret = -ENOSPC;
					goto error;
				}
				ins->offset = search_end - aligned;
1058 1059 1060
				start_found = 1;
				goto check_pending;
			}
1061 1062 1063 1064 1065 1066 1067
			ins->objectid = stripe_align(root,
						     last_byte > search_start ?
						     last_byte : search_start);
			if (search_end <= ins->objectid) {
				ret = -ENOSPC;
				goto error;
			}
C
Chris Mason 已提交
1068
			ins->offset = search_end - ins->objectid;
1069
			BUG_ON(ins->objectid >= search_end);
1070 1071
			goto check_pending;
		}
1072
		btrfs_item_key_to_cpu(l, &key, slot);
1073

1074
		if (key.objectid >= search_start && key.objectid > last_byte &&
1075
		    start_found) {
1076 1077
			if (last_byte < search_start)
				last_byte = search_start;
1078 1079 1080 1081
			aligned = stripe_align(root, last_byte);
			hole_size = key.objectid - aligned;
			if (key.objectid > aligned && hole_size >= num_bytes) {
				ins->objectid = aligned;
1082 1083
				ins->offset = hole_size;
				goto check_pending;
1084
			}
1085
		}
1086 1087
		if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY) {
			if (!start_found) {
1088
				last_byte = key.objectid;
1089 1090
				start_found = 1;
			}
1091
			goto next;
1092 1093
		}

1094

1095
		start_found = 1;
1096
		last_byte = key.objectid + key.offset;
1097

1098 1099
		if (!full_scan && data != BTRFS_BLOCK_GROUP_MIXED &&
		    last_byte >= block_group->key.objectid +
C
Chris Mason 已提交
1100 1101 1102
		    block_group->key.offset) {
			btrfs_release_path(root, path);
			search_start = block_group->key.objectid +
1103
				block_group->key.offset;
C
Chris Mason 已提交
1104 1105
			goto new_group;
		}
C
Chris Mason 已提交
1106
next:
1107
		path->slots[0]++;
1108
		cond_resched();
1109 1110 1111 1112 1113
	}
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
	 */
1114
	btrfs_release_path(root, path);
1115
	BUG_ON(ins->objectid < search_start);
1116

1117
	if (ins->objectid + num_bytes >= search_end)
1118
		goto enospc;
1119
	if (!full_scan && data != BTRFS_BLOCK_GROUP_MIXED &&
Y
Yan 已提交
1120
	    ins->objectid + num_bytes > block_group->
1121 1122 1123 1124 1125
	    key.objectid + block_group->key.offset) {
		search_start = block_group->key.objectid +
			block_group->key.offset;
		goto new_group;
	}
1126
	if (test_range_bit(&info->extent_ins, ins->objectid,
1127 1128
			   ins->objectid + num_bytes -1, EXTENT_LOCKED, 0)) {
		search_start = ins->objectid + num_bytes;
1129 1130 1131
		goto new_group;
	}
	if (test_range_bit(&info->pinned_extents, ins->objectid,
1132 1133
			   ins->objectid + num_bytes -1, EXTENT_DIRTY, 0)) {
		search_start = ins->objectid + num_bytes;
1134
		goto new_group;
1135
	}
1136
	if (exclude_nr > 0 && (ins->objectid + num_bytes > exclude_start &&
1137 1138 1139 1140
	    ins->objectid < exclude_start + exclude_nr)) {
		search_start = exclude_start + exclude_nr;
		goto new_group;
	}
1141
	if (!data) {
1142
		block_group = btrfs_lookup_block_group(info, ins->objectid);
1143 1144
		if (block_group)
			trans->block_group = block_group;
1145
	}
1146
	ins->offset = num_bytes;
1147
	btrfs_free_path(path);
1148
	return 0;
C
Chris Mason 已提交
1149 1150

new_group:
1151
	if (search_start + num_bytes >= search_end) {
1152
enospc:
C
Chris Mason 已提交
1153
		search_start = orig_search_start;
1154 1155 1156 1157
		if (full_scan) {
			ret = -ENOSPC;
			goto error;
		}
1158 1159 1160
		if (wrapped) {
			if (!full_scan)
				total_needed -= empty_size;
1161
			full_scan = 1;
1162
		} else
1163
			wrapped = 1;
C
Chris Mason 已提交
1164
	}
1165
	block_group = btrfs_lookup_block_group(info, search_start);
1166
	cond_resched();
C
Chris Mason 已提交
1167 1168
	if (!full_scan)
		block_group = btrfs_find_block_group(root, block_group,
1169
						     search_start, data, 0);
C
Chris Mason 已提交
1170 1171
	goto check_failed;

C
Chris Mason 已提交
1172
error:
1173 1174
	btrfs_release_path(root, path);
	btrfs_free_path(path);
C
Chris Mason 已提交
1175
	return ret;
1176 1177 1178 1179 1180 1181 1182 1183
}
/*
 * 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.
 */
1184 1185
int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
		       struct btrfs_root *root, u64 owner,
1186
		       u64 num_bytes, u64 empty_size, u64 hint_byte,
C
Chris Mason 已提交
1187
		       u64 search_end, struct btrfs_key *ins, int data)
1188 1189 1190
{
	int ret;
	int pending_ret;
1191
	u64 super_used, root_used;
1192
	u64 search_start = 0;
1193 1194
	struct btrfs_fs_info *info = root->fs_info;
	struct btrfs_root *extent_root = info->extent_root;
C
Chris Mason 已提交
1195
	struct btrfs_extent_item extent_item;
C
Chris Mason 已提交
1196

1197 1198
	btrfs_set_stack_extent_refs(&extent_item, 1);
	btrfs_set_stack_extent_owner(&extent_item, owner);
1199

1200 1201 1202
	WARN_ON(num_bytes < root->sectorsize);
	ret = find_free_extent(trans, root, num_bytes, empty_size,
			       search_start, search_end, hint_byte, ins,
1203 1204
			       trans->alloc_exclude_start,
			       trans->alloc_exclude_nr, data);
C
Chris Mason 已提交
1205
	BUG_ON(ret);
1206 1207
	if (ret)
		return ret;
1208

1209
	/* block accounting for super block */
1210 1211
	super_used = btrfs_super_bytes_used(&info->super_copy);
	btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes);
1212

1213
	/* block accounting for root item */
1214 1215
	root_used = btrfs_root_used(&root->root_item);
	btrfs_set_root_used(&root->root_item, root_used + num_bytes);
1216

1217 1218 1219 1220
	clear_extent_dirty(&root->fs_info->free_space_cache,
			   ins->objectid, ins->objectid + ins->offset - 1,
			   GFP_NOFS);

1221
	if (root == extent_root) {
1222 1223 1224
		set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
				ins->objectid + ins->offset - 1,
				EXTENT_LOCKED, GFP_NOFS);
1225
		WARN_ON(data == 1);
1226 1227 1228 1229 1230 1231
		goto update_block;
	}

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

1235 1236 1237
	trans->alloc_exclude_start = 0;
	trans->alloc_exclude_nr = 0;

C
Chris Mason 已提交
1238
	BUG_ON(ret);
1239
	finish_current_insert(trans, extent_root);
C
Chris Mason 已提交
1240
	pending_ret = del_pending_extents(trans, extent_root);
1241

1242
	if (ret) {
C
Chris Mason 已提交
1243
		return ret;
1244 1245
	}
	if (pending_ret) {
C
Chris Mason 已提交
1246
		return pending_ret;
1247
	}
1248 1249

update_block:
C
Chris Mason 已提交
1250 1251
	ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0,
				 data);
C
Chris Mason 已提交
1252
	BUG_ON(ret);
C
Chris Mason 已提交
1253
	return 0;
1254 1255 1256 1257 1258 1259
}

/*
 * helper function to allocate a block for a given tree
 * returns the tree buffer or NULL.
 */
1260
struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
1261 1262
					     struct btrfs_root *root,
					     u32 blocksize, u64 hint,
1263
					     u64 empty_size)
1264
{
C
Chris Mason 已提交
1265
	struct btrfs_key ins;
1266
	int ret;
1267
	struct extent_buffer *buf;
1268

1269
	ret = btrfs_alloc_extent(trans, root, root->root_key.objectid,
1270 1271
				 blocksize, empty_size, hint,
				 (u64)-1, &ins, 0);
1272
	if (ret) {
1273 1274
		BUG_ON(ret > 0);
		return ERR_PTR(ret);
1275
	}
1276
	buf = btrfs_find_create_tree_block(root, ins.objectid, blocksize);
1277
	if (!buf) {
1278
		btrfs_free_extent(trans, root, ins.objectid, blocksize, 0);
1279 1280
		return ERR_PTR(-ENOMEM);
	}
1281 1282 1283
	btrfs_set_buffer_uptodate(buf);
	set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
			 buf->start + buf->len - 1, GFP_NOFS);
1284 1285 1286 1287
	set_extent_bits(&BTRFS_I(root->fs_info->btree_inode)->extent_tree,
			buf->start, buf->start + buf->len - 1,
			EXTENT_CSUM, GFP_NOFS);
	buf->flags |= EXTENT_CSUM;
1288
	btrfs_set_buffer_defrag(buf);
1289
	trans->blocks_used++;
1290 1291
	return buf;
}
1292

1293
static int drop_leaf_ref(struct btrfs_trans_handle *trans,
1294
			 struct btrfs_root *root, struct extent_buffer *leaf)
1295
{
1296
	struct btrfs_key key;
1297 1298 1299 1300 1301
	struct btrfs_file_extent_item *fi;
	int i;
	int nritems;
	int ret;

1302 1303
	BUG_ON(!btrfs_is_leaf(leaf));
	nritems = btrfs_header_nritems(leaf);
1304
	for (i = 0; i < nritems; i++) {
1305
		u64 disk_bytenr;
1306 1307 1308

		btrfs_item_key_to_cpu(leaf, &key, i);
		if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1309 1310
			continue;
		fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
1311 1312
		if (btrfs_file_extent_type(leaf, fi) ==
		    BTRFS_FILE_EXTENT_INLINE)
1313
			continue;
1314 1315 1316 1317
		/*
		 * FIXME make sure to insert a trans record that
		 * repeats the snapshot del on crash
		 */
1318 1319
		disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
		if (disk_bytenr == 0)
C
Chris Mason 已提交
1320
			continue;
1321 1322
		ret = btrfs_free_extent(trans, root, disk_bytenr,
				btrfs_file_extent_disk_num_bytes(leaf, fi), 0);
1323 1324 1325 1326 1327
		BUG_ON(ret);
	}
	return 0;
}

1328
static void reada_walk_down(struct btrfs_root *root,
1329
			    struct extent_buffer *node)
1330 1331 1332
{
	int i;
	u32 nritems;
1333
	u64 bytenr;
1334 1335
	int ret;
	u32 refs;
1336 1337
	int level;
	u32 blocksize;
1338

1339
	nritems = btrfs_header_nritems(node);
1340
	level = btrfs_header_level(node);
1341
	for (i = 0; i < nritems; i++) {
1342 1343 1344
		bytenr = btrfs_node_blockptr(node, i);
		blocksize = btrfs_level_size(root, level - 1);
		ret = lookup_extent_ref(NULL, root, bytenr, blocksize, &refs);
1345 1346 1347
		BUG_ON(ret);
		if (refs != 1)
			continue;
1348
		mutex_unlock(&root->fs_info->fs_mutex);
1349
		ret = readahead_tree_block(root, bytenr, blocksize);
1350 1351
		cond_resched();
		mutex_lock(&root->fs_info->fs_mutex);
1352 1353 1354 1355 1356
		if (ret)
			break;
	}
}

C
Chris Mason 已提交
1357 1358 1359 1360
/*
 * helper function for drop_snapshot, this walks down the tree dropping ref
 * counts as it goes.
 */
1361 1362
static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
			  *root, struct btrfs_path *path, int *level)
C
Chris Mason 已提交
1363
{
1364 1365
	struct extent_buffer *next;
	struct extent_buffer *cur;
1366 1367
	u64 bytenr;
	u32 blocksize;
C
Chris Mason 已提交
1368 1369 1370
	int ret;
	u32 refs;

1371 1372
	WARN_ON(*level < 0);
	WARN_ON(*level >= BTRFS_MAX_LEVEL);
1373
	ret = lookup_extent_ref(trans, root,
1374 1375
				path->nodes[*level]->start,
				path->nodes[*level]->len, &refs);
C
Chris Mason 已提交
1376 1377 1378
	BUG_ON(ret);
	if (refs > 1)
		goto out;
1379

C
Chris Mason 已提交
1380 1381 1382
	/*
	 * walk down to the last node level and free all the leaves
	 */
1383
	while(*level >= 0) {
1384 1385
		WARN_ON(*level < 0);
		WARN_ON(*level >= BTRFS_MAX_LEVEL);
C
Chris Mason 已提交
1386
		cur = path->nodes[*level];
1387 1388

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

1391
		if (btrfs_header_level(cur) != *level)
C
Chris Mason 已提交
1392
			WARN_ON(1);
1393

1394
		if (path->slots[*level] >=
1395
		    btrfs_header_nritems(cur))
C
Chris Mason 已提交
1396
			break;
1397 1398 1399 1400 1401
		if (*level == 0) {
			ret = drop_leaf_ref(trans, root, cur);
			BUG_ON(ret);
			break;
		}
1402 1403 1404
		bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
		blocksize = btrfs_level_size(root, *level - 1);
		ret = lookup_extent_ref(trans, root, bytenr, blocksize, &refs);
1405 1406
		BUG_ON(ret);
		if (refs != 1) {
C
Chris Mason 已提交
1407
			path->slots[*level]++;
1408 1409
			ret = btrfs_free_extent(trans, root, bytenr,
						blocksize, 1);
C
Chris Mason 已提交
1410 1411 1412
			BUG_ON(ret);
			continue;
		}
1413
		next = btrfs_find_tree_block(root, bytenr, blocksize);
1414 1415
		if (!next || !btrfs_buffer_uptodate(next)) {
			free_extent_buffer(next);
1416
			mutex_unlock(&root->fs_info->fs_mutex);
1417
			next = read_tree_block(root, bytenr, blocksize);
1418 1419 1420
			mutex_lock(&root->fs_info->fs_mutex);

			/* we dropped the lock, check one more time */
1421 1422
			ret = lookup_extent_ref(trans, root, bytenr,
						blocksize, &refs);
1423 1424 1425
			BUG_ON(ret);
			if (refs != 1) {
				path->slots[*level]++;
1426
				free_extent_buffer(next);
1427
				ret = btrfs_free_extent(trans, root,
1428
							bytenr, blocksize, 1);
1429 1430 1431 1432
				BUG_ON(ret);
				continue;
			}
		}
1433
		WARN_ON(*level <= 0);
C
Chris Mason 已提交
1434
		if (path->nodes[*level-1])
1435
			free_extent_buffer(path->nodes[*level-1]);
C
Chris Mason 已提交
1436
		path->nodes[*level-1] = next;
1437
		*level = btrfs_header_level(next);
C
Chris Mason 已提交
1438 1439 1440
		path->slots[*level] = 0;
	}
out:
1441 1442
	WARN_ON(*level < 0);
	WARN_ON(*level >= BTRFS_MAX_LEVEL);
1443 1444
	ret = btrfs_free_extent(trans, root, path->nodes[*level]->start,
				path->nodes[*level]->len, 1);
1445
	free_extent_buffer(path->nodes[*level]);
C
Chris Mason 已提交
1446 1447 1448 1449 1450 1451
	path->nodes[*level] = NULL;
	*level += 1;
	BUG_ON(ret);
	return 0;
}

C
Chris Mason 已提交
1452 1453 1454 1455 1456
/*
 * 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
 */
1457 1458
static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root
			*root, struct btrfs_path *path, int *level)
C
Chris Mason 已提交
1459 1460 1461 1462
{
	int i;
	int slot;
	int ret;
1463 1464
	struct btrfs_root_item *root_item = &root->root_item;

C
Chris Mason 已提交
1465
	for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
C
Chris Mason 已提交
1466
		slot = path->slots[i];
1467 1468 1469 1470
		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 已提交
1471 1472
			path->slots[i]++;
			*level = i;
1473
			WARN_ON(*level == 0);
1474
			btrfs_node_key(node, &disk_key, path->slots[i]);
1475
			memcpy(&root_item->drop_progress,
1476
			       &disk_key, sizeof(disk_key));
1477
			root_item->drop_level = i;
C
Chris Mason 已提交
1478 1479
			return 0;
		} else {
1480
			ret = btrfs_free_extent(trans, root,
1481 1482
						path->nodes[*level]->start,
						path->nodes[*level]->len, 1);
1483
			BUG_ON(ret);
1484
			free_extent_buffer(path->nodes[*level]);
C
Chris Mason 已提交
1485
			path->nodes[*level] = NULL;
C
Chris Mason 已提交
1486 1487 1488 1489 1490 1491
			*level = i + 1;
		}
	}
	return 1;
}

C
Chris Mason 已提交
1492 1493 1494 1495 1496
/*
 * 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.
 */
1497
int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
1498
			*root)
C
Chris Mason 已提交
1499
{
1500
	int ret = 0;
C
Chris Mason 已提交
1501
	int wret;
C
Chris Mason 已提交
1502
	int level;
1503
	struct btrfs_path *path;
C
Chris Mason 已提交
1504 1505
	int i;
	int orig_level;
1506
	struct btrfs_root_item *root_item = &root->root_item;
C
Chris Mason 已提交
1507

1508 1509
	path = btrfs_alloc_path();
	BUG_ON(!path);
C
Chris Mason 已提交
1510

1511
	level = btrfs_header_level(root->node);
C
Chris Mason 已提交
1512
	orig_level = level;
1513 1514
	if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
		path->nodes[level] = root->node;
1515
		extent_buffer_get(root->node);
1516 1517 1518
		path->slots[level] = 0;
	} else {
		struct btrfs_key key;
1519 1520
		struct btrfs_disk_key found_key;
		struct extent_buffer *node;
1521

1522
		btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
1523 1524
		level = root_item->drop_level;
		path->lowest_level = level;
1525
		wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1526
		if (wret < 0) {
1527 1528 1529
			ret = wret;
			goto out;
		}
1530 1531 1532 1533
		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)));
1534
	}
C
Chris Mason 已提交
1535
	while(1) {
1536
		wret = walk_down_tree(trans, root, path, &level);
C
Chris Mason 已提交
1537
		if (wret > 0)
C
Chris Mason 已提交
1538
			break;
C
Chris Mason 已提交
1539 1540 1541
		if (wret < 0)
			ret = wret;

1542
		wret = walk_up_tree(trans, root, path, &level);
C
Chris Mason 已提交
1543
		if (wret > 0)
C
Chris Mason 已提交
1544
			break;
C
Chris Mason 已提交
1545 1546
		if (wret < 0)
			ret = wret;
1547 1548
		ret = -EAGAIN;
		break;
C
Chris Mason 已提交
1549
	}
C
Chris Mason 已提交
1550
	for (i = 0; i <= orig_level; i++) {
1551
		if (path->nodes[i]) {
1552
			free_extent_buffer(path->nodes[i]);
1553
			path->nodes[i] = NULL;
C
Chris Mason 已提交
1554
		}
C
Chris Mason 已提交
1555
	}
1556
out:
1557
	btrfs_free_path(path);
C
Chris Mason 已提交
1558
	return ret;
C
Chris Mason 已提交
1559
}
C
Chris Mason 已提交
1560

1561
int btrfs_free_block_groups(struct btrfs_fs_info *info)
C
Chris Mason 已提交
1562
{
1563 1564
	u64 start;
	u64 end;
1565
	u64 ptr;
C
Chris Mason 已提交
1566 1567
	int ret;
	while(1) {
1568 1569 1570
		ret = find_first_extent_bit(&info->block_group_cache, 0,
					    &start, &end, (unsigned int)-1);
		if (ret)
C
Chris Mason 已提交
1571
			break;
1572 1573 1574
		ret = get_state_private(&info->block_group_cache, start, &ptr);
		if (!ret)
			kfree((void *)(unsigned long)ptr);
1575 1576
		clear_extent_bits(&info->block_group_cache, start,
				  end, (unsigned int)-1, GFP_NOFS);
C
Chris Mason 已提交
1577
	}
1578
	while(1) {
1579 1580 1581
		ret = find_first_extent_bit(&info->free_space_cache, 0,
					    &start, &end, EXTENT_DIRTY);
		if (ret)
1582
			break;
1583 1584
		clear_extent_dirty(&info->free_space_cache, start,
				   end, GFP_NOFS);
1585
	}
C
Chris Mason 已提交
1586 1587 1588
	return 0;
}

C
Chris Mason 已提交
1589 1590 1591 1592 1593
int btrfs_read_block_groups(struct btrfs_root *root)
{
	struct btrfs_path *path;
	int ret;
	int err = 0;
1594
	int bit;
C
Chris Mason 已提交
1595
	struct btrfs_block_group_cache *cache;
C
Chris Mason 已提交
1596
	struct btrfs_fs_info *info = root->fs_info;
1597
	struct extent_map_tree *block_group_cache;
C
Chris Mason 已提交
1598 1599
	struct btrfs_key key;
	struct btrfs_key found_key;
1600
	struct extent_buffer *leaf;
1601 1602

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

C
Chris Mason 已提交
1604
	root = info->extent_root;
C
Chris Mason 已提交
1605
	key.objectid = 0;
1606
	key.offset = BTRFS_BLOCK_GROUP_SIZE;
C
Chris Mason 已提交
1607 1608 1609 1610 1611 1612 1613
	btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);

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

	while(1) {
C
Chris Mason 已提交
1614
		ret = btrfs_search_slot(NULL, info->extent_root,
C
Chris Mason 已提交
1615 1616 1617 1618 1619
					&key, path, 0, 0);
		if (ret != 0) {
			err = ret;
			break;
		}
1620 1621
		leaf = path->nodes[0];
		btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
C
Chris Mason 已提交
1622 1623 1624 1625 1626
		cache = kmalloc(sizeof(*cache), GFP_NOFS);
		if (!cache) {
			err = -1;
			break;
		}
C
Chris Mason 已提交
1627

1628 1629 1630
		read_extent_buffer(leaf, &cache->item,
				   btrfs_item_ptr_offset(leaf, path->slots[0]),
				   sizeof(cache->item));
C
Chris Mason 已提交
1631
		memcpy(&cache->key, &found_key, sizeof(found_key));
1632
		cache->cached = 0;
1633
		cache->pinned = 0;
C
Chris Mason 已提交
1634 1635
		key.objectid = found_key.objectid + found_key.offset;
		btrfs_release_path(root, path);
1636

1637 1638 1639 1640
		if (cache->item.flags & BTRFS_BLOCK_GROUP_MIXED) {
			bit = BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA;
			cache->data = BTRFS_BLOCK_GROUP_MIXED;
		} else if (cache->item.flags & BTRFS_BLOCK_GROUP_DATA) {
1641
			bit = BLOCK_GROUP_DATA;
1642
			cache->data = BTRFS_BLOCK_GROUP_DATA;
1643 1644 1645
		} else {
			bit = BLOCK_GROUP_METADATA;
			cache->data = 0;
1646
		}
1647 1648 1649 1650 1651 1652

		/* use EXTENT_LOCKED to prevent merging */
		set_extent_bits(block_group_cache, found_key.objectid,
				found_key.objectid + found_key.offset - 1,
				bit | EXTENT_LOCKED, GFP_NOFS);
		set_state_private(block_group_cache, found_key.objectid,
J
Jens Axboe 已提交
1653
				  (unsigned long)cache);
1654

C
Chris Mason 已提交
1655
		if (key.objectid >=
1656
		    btrfs_super_total_bytes(&info->super_copy))
C
Chris Mason 已提交
1657 1658 1659 1660 1661 1662
			break;
	}

	btrfs_free_path(path);
	return 0;
}