Btrfs: extent_map optimizations to cut down on CPU usage
[linux-2.6-block.git] / fs / btrfs / extent-tree.c
CommitLineData
6cbd5570
CM
1/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
ec6b910f 19#include <linux/sched.h>
fec577fb
CM
20#include "ctree.h"
21#include "disk-io.h"
22#include "print-tree.h"
e089f05c 23#include "transaction.h"
fec577fb 24
96b5179d
CM
25#define BLOCK_GROUP_DATA EXTENT_WRITEBACK
26#define BLOCK_GROUP_METADATA EXTENT_UPTODATE
27#define BLOCK_GROUP_DIRTY EXTENT_DIRTY
28
e089f05c
CM
29static int finish_current_insert(struct btrfs_trans_handle *trans, struct
30 btrfs_root *extent_root);
e20d96d6
CM
31static int del_pending_extents(struct btrfs_trans_handle *trans, struct
32 btrfs_root *extent_root);
fec577fb 33
e37c9e69
CM
34static int cache_block_group(struct btrfs_root *root,
35 struct btrfs_block_group_cache *block_group)
36{
37 struct btrfs_path *path;
38 int ret;
39 struct btrfs_key key;
5f39d397 40 struct extent_buffer *leaf;
f510cfec 41 struct extent_map_tree *free_space_cache;
e37c9e69 42 int slot;
e37c9e69
CM
43 u64 last = 0;
44 u64 hole_size;
7d7d6068 45 u64 first_free;
e37c9e69
CM
46 int found = 0;
47
48 root = root->fs_info->extent_root;
f510cfec 49 free_space_cache = &root->fs_info->free_space_cache;
e37c9e69
CM
50
51 if (block_group->cached)
52 return 0;
f510cfec 53
e37c9e69
CM
54 path = btrfs_alloc_path();
55 if (!path)
56 return -ENOMEM;
7d7d6068 57
2cc58cf2 58 path->reada = 2;
7d7d6068 59 first_free = block_group->key.objectid;
e37c9e69 60 key.objectid = block_group->key.objectid;
e37c9e69 61 key.offset = 0;
7d7d6068 62
e37c9e69
CM
63 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
64 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
7d7d6068 65
e37c9e69
CM
66 if (ret < 0)
67 return ret;
7d7d6068 68
e37c9e69
CM
69 if (ret && path->slots[0] > 0)
70 path->slots[0]--;
7d7d6068 71
e37c9e69 72 while(1) {
5f39d397 73 leaf = path->nodes[0];
e37c9e69 74 slot = path->slots[0];
5f39d397 75 if (slot >= btrfs_header_nritems(leaf)) {
e37c9e69 76 ret = btrfs_next_leaf(root, path);
54aa1f4d
CM
77 if (ret < 0)
78 goto err;
de428b63 79 if (ret == 0) {
e37c9e69 80 continue;
de428b63 81 } else {
e37c9e69
CM
82 break;
83 }
84 }
7d7d6068 85
5f39d397 86 btrfs_item_key_to_cpu(leaf, &key, slot);
7d7d6068
Y
87 if (key.objectid < block_group->key.objectid) {
88 if (key.objectid + key.offset > first_free)
89 first_free = key.objectid + key.offset;
90 goto next;
91 }
92
e37c9e69
CM
93 if (key.objectid >= block_group->key.objectid +
94 block_group->key.offset) {
e37c9e69
CM
95 break;
96 }
7d7d6068 97
e37c9e69
CM
98 if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) {
99 if (!found) {
7d7d6068 100 last = first_free;
e37c9e69 101 found = 1;
e37c9e69 102 }
f510cfec
CM
103 if (key.objectid > last) {
104 hole_size = key.objectid - last;
105 set_extent_dirty(free_space_cache, last,
106 last + hole_size - 1,
107 GFP_NOFS);
7d7d6068
Y
108 }
109 last = key.objectid + key.offset;
e37c9e69 110 }
7d7d6068 111next:
e37c9e69
CM
112 path->slots[0]++;
113 }
114
7d7d6068
Y
115 if (!found)
116 last = first_free;
117 if (block_group->key.objectid +
118 block_group->key.offset > last) {
119 hole_size = block_group->key.objectid +
120 block_group->key.offset - last;
f510cfec
CM
121 set_extent_dirty(free_space_cache, last,
122 last + hole_size - 1, GFP_NOFS);
7d7d6068 123 }
e37c9e69 124 block_group->cached = 1;
54aa1f4d 125err:
e37c9e69
CM
126 btrfs_free_path(path);
127 return 0;
128}
129
5276aeda
CM
130struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
131 btrfs_fs_info *info,
db94535d 132 u64 bytenr)
be744175 133{
96b5179d
CM
134 struct extent_map_tree *block_group_cache;
135 struct btrfs_block_group_cache *block_group = NULL;
136 u64 ptr;
137 u64 start;
138 u64 end;
be744175
CM
139 int ret;
140
96b5179d
CM
141 block_group_cache = &info->block_group_cache;
142 ret = find_first_extent_bit(block_group_cache,
db94535d 143 bytenr, &start, &end,
96b5179d 144 BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA);
be744175 145 if (ret) {
96b5179d 146 return NULL;
be744175 147 }
96b5179d
CM
148 ret = get_state_private(block_group_cache, start, &ptr);
149 if (ret)
150 return NULL;
151
152 block_group = (struct btrfs_block_group_cache *)ptr;
153
154
db94535d 155 if (block_group->key.objectid <= bytenr && bytenr <=
96b5179d
CM
156 block_group->key.objectid + block_group->key.offset)
157 return block_group;
158
be744175
CM
159 return NULL;
160}
161
e37c9e69
CM
162static u64 find_search_start(struct btrfs_root *root,
163 struct btrfs_block_group_cache **cache_ret,
f510cfec 164 u64 search_start, int num, int data)
e37c9e69 165{
e37c9e69
CM
166 int ret;
167 struct btrfs_block_group_cache *cache = *cache_ret;
e19caa5f 168 u64 last;
f510cfec
CM
169 u64 start = 0;
170 u64 end = 0;
e37c9e69 171
e37c9e69 172again:
54aa1f4d
CM
173 ret = cache_block_group(root, cache);
174 if (ret)
175 goto out;
e19caa5f
CM
176 last = max(search_start, cache->key.objectid);
177
e37c9e69 178 while(1) {
f510cfec
CM
179 ret = find_first_extent_bit(&root->fs_info->free_space_cache,
180 last, &start, &end, EXTENT_DIRTY);
e19caa5f
CM
181 if (ret) {
182 goto new_group;
183 }
f510cfec
CM
184
185 start = max(last, start);
186 last = end + 1;
187 if (end + 1 - start < num)
188 continue;
96b5179d 189 if (start + num >= cache->key.objectid + cache->key.offset)
e37c9e69 190 goto new_group;
f510cfec 191 return start;
e37c9e69
CM
192 }
193out:
1a5bc167 194 return search_start;
e37c9e69
CM
195
196new_group:
e19caa5f
CM
197 last = cache->key.objectid + cache->key.offset;
198 cache = btrfs_lookup_block_group(root->fs_info, last);
e37c9e69 199 if (!cache) {
1a5bc167 200 return search_start;
e37c9e69 201 }
e19caa5f 202 cache = btrfs_find_block_group(root, cache, last, data, 0);
e37c9e69
CM
203 *cache_ret = cache;
204 goto again;
205}
206
84f54cfa
CM
207static u64 div_factor(u64 num, int factor)
208{
209 num *= factor;
210 do_div(num, 10);
211 return num;
212}
213
31f3c99b
CM
214struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
215 struct btrfs_block_group_cache
be744175 216 *hint, u64 search_start,
de428b63 217 int data, int owner)
cd1bc465 218{
96b5179d
CM
219 struct btrfs_block_group_cache *cache;
220 struct extent_map_tree *block_group_cache;
31f3c99b 221 struct btrfs_block_group_cache *found_group = NULL;
cd1bc465
CM
222 struct btrfs_fs_info *info = root->fs_info;
223 u64 used;
31f3c99b
CM
224 u64 last = 0;
225 u64 hint_last;
96b5179d
CM
226 u64 start;
227 u64 end;
228 u64 free_check;
229 u64 ptr;
230 int bit;
cd1bc465 231 int ret;
31f3c99b 232 int full_search = 0;
de428b63 233 int factor = 8;
1e2677e0 234 int data_swap = 0;
de428b63 235
96b5179d
CM
236 block_group_cache = &info->block_group_cache;
237
de428b63
CM
238 if (!owner)
239 factor = 5;
be744175 240
96b5179d
CM
241 if (data)
242 bit = BLOCK_GROUP_DATA;
243 else
244 bit = BLOCK_GROUP_METADATA;
be744175
CM
245
246 if (search_start) {
247 struct btrfs_block_group_cache *shint;
5276aeda 248 shint = btrfs_lookup_block_group(info, search_start);
e9fe395e 249 if (shint && shint->data == data) {
be744175 250 used = btrfs_block_group_used(&shint->item);
1a5bc167 251 if (used < div_factor(shint->key.offset, factor)) {
be744175
CM
252 return shint;
253 }
254 }
255 }
256 if (hint && hint->data == data) {
31f3c99b 257 used = btrfs_block_group_used(&hint->item);
1a5bc167 258 if (used < div_factor(hint->key.offset, factor)) {
31f3c99b
CM
259 return hint;
260 }
e19caa5f 261 last = hint->key.objectid + hint->key.offset;
31f3c99b
CM
262 hint_last = last;
263 } else {
e37c9e69
CM
264 if (hint)
265 hint_last = max(hint->key.objectid, search_start);
266 else
267 hint_last = search_start;
268
269 last = hint_last;
31f3c99b 270 }
31f3c99b 271again:
cd1bc465 272 while(1) {
96b5179d
CM
273 ret = find_first_extent_bit(block_group_cache, last,
274 &start, &end, bit);
275 if (ret)
cd1bc465 276 break;
96b5179d
CM
277
278 ret = get_state_private(block_group_cache, start, &ptr);
279 if (ret)
280 break;
281
282 cache = (struct btrfs_block_group_cache *)ptr;
283 last = cache->key.objectid + cache->key.offset;
284 used = btrfs_block_group_used(&cache->item);
285
286 if (full_search)
287 free_check = cache->key.offset;
288 else
289 free_check = div_factor(cache->key.offset, factor);
290
1a5bc167 291 if (used < free_check) {
96b5179d
CM
292 found_group = cache;
293 goto found;
cd1bc465 294 }
de428b63 295 cond_resched();
cd1bc465 296 }
31f3c99b 297 if (!full_search) {
be744175 298 last = search_start;
31f3c99b
CM
299 full_search = 1;
300 goto again;
301 }
1e2677e0 302 if (!data_swap) {
1e2677e0 303 data_swap = 1;
96b5179d 304 bit = BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA;
1e2677e0
CM
305 last = search_start;
306 goto again;
307 }
be744175 308found:
31f3c99b 309 return found_group;
cd1bc465
CM
310}
311
b18c6685
CM
312int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
313 struct btrfs_root *root,
db94535d 314 u64 bytenr, u64 num_bytes)
02217ed2 315{
5caf2a00 316 struct btrfs_path *path;
02217ed2 317 int ret;
e2fa7227 318 struct btrfs_key key;
5f39d397 319 struct extent_buffer *l;
234b63a0 320 struct btrfs_extent_item *item;
cf27e1ee 321 u32 refs;
037e6390 322
db94535d 323 WARN_ON(num_bytes < root->sectorsize);
5caf2a00 324 path = btrfs_alloc_path();
54aa1f4d
CM
325 if (!path)
326 return -ENOMEM;
26b8003f 327
db94535d 328 key.objectid = bytenr;
62e2749e 329 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
db94535d 330 key.offset = num_bytes;
5caf2a00 331 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
9f5fae2f 332 0, 1);
54aa1f4d
CM
333 if (ret < 0)
334 return ret;
a429e513 335 if (ret != 0) {
a28ec197 336 BUG();
a429e513 337 }
02217ed2 338 BUG_ON(ret != 0);
5f39d397 339 l = path->nodes[0];
5caf2a00 340 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
5f39d397
CM
341 refs = btrfs_extent_refs(l, item);
342 btrfs_set_extent_refs(l, item, refs + 1);
5caf2a00 343 btrfs_mark_buffer_dirty(path->nodes[0]);
a28ec197 344
5caf2a00
CM
345 btrfs_release_path(root->fs_info->extent_root, path);
346 btrfs_free_path(path);
9f5fae2f 347 finish_current_insert(trans, root->fs_info->extent_root);
e20d96d6 348 del_pending_extents(trans, root->fs_info->extent_root);
02217ed2
CM
349 return 0;
350}
351
e9d0b13b
CM
352int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
353 struct btrfs_root *root)
354{
355 finish_current_insert(trans, root->fs_info->extent_root);
356 del_pending_extents(trans, root->fs_info->extent_root);
357 return 0;
358}
359
b18c6685 360static int lookup_extent_ref(struct btrfs_trans_handle *trans,
db94535d
CM
361 struct btrfs_root *root, u64 bytenr,
362 u64 num_bytes, u32 *refs)
a28ec197 363{
5caf2a00 364 struct btrfs_path *path;
a28ec197 365 int ret;
e2fa7227 366 struct btrfs_key key;
5f39d397 367 struct extent_buffer *l;
234b63a0 368 struct btrfs_extent_item *item;
5caf2a00 369
db94535d 370 WARN_ON(num_bytes < root->sectorsize);
5caf2a00 371 path = btrfs_alloc_path();
db94535d
CM
372 key.objectid = bytenr;
373 key.offset = num_bytes;
62e2749e 374 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
5caf2a00 375 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
9f5fae2f 376 0, 0);
54aa1f4d
CM
377 if (ret < 0)
378 goto out;
5f39d397
CM
379 if (ret != 0) {
380 btrfs_print_leaf(root, path->nodes[0]);
db94535d 381 printk("failed to find block number %Lu\n", bytenr);
a28ec197 382 BUG();
5f39d397
CM
383 }
384 l = path->nodes[0];
5caf2a00 385 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
5f39d397 386 *refs = btrfs_extent_refs(l, item);
54aa1f4d 387out:
5caf2a00 388 btrfs_free_path(path);
a28ec197
CM
389 return 0;
390}
391
c5739bba
CM
392int btrfs_inc_root_ref(struct btrfs_trans_handle *trans,
393 struct btrfs_root *root)
394{
db94535d
CM
395 return btrfs_inc_extent_ref(trans, root, root->node->start,
396 root->node->len);
c5739bba
CM
397}
398
e089f05c 399int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
5f39d397 400 struct extent_buffer *buf)
02217ed2 401{
db94535d 402 u64 bytenr;
5f39d397
CM
403 u32 nritems;
404 struct btrfs_key key;
6407bf6d 405 struct btrfs_file_extent_item *fi;
02217ed2 406 int i;
db94535d 407 int level;
6407bf6d 408 int ret;
54aa1f4d
CM
409 int faili;
410 int err;
a28ec197 411
3768f368 412 if (!root->ref_cows)
a28ec197 413 return 0;
5f39d397 414
db94535d 415 level = btrfs_header_level(buf);
5f39d397
CM
416 nritems = btrfs_header_nritems(buf);
417 for (i = 0; i < nritems; i++) {
db94535d
CM
418 if (level == 0) {
419 u64 disk_bytenr;
5f39d397
CM
420 btrfs_item_key_to_cpu(buf, &key, i);
421 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
6407bf6d 422 continue;
5f39d397 423 fi = btrfs_item_ptr(buf, i,
6407bf6d 424 struct btrfs_file_extent_item);
5f39d397 425 if (btrfs_file_extent_type(buf, fi) ==
236454df
CM
426 BTRFS_FILE_EXTENT_INLINE)
427 continue;
db94535d
CM
428 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
429 if (disk_bytenr == 0)
3a686375 430 continue;
db94535d
CM
431 ret = btrfs_inc_extent_ref(trans, root, disk_bytenr,
432 btrfs_file_extent_disk_num_bytes(buf, fi));
54aa1f4d
CM
433 if (ret) {
434 faili = i;
435 goto fail;
436 }
6407bf6d 437 } else {
db94535d
CM
438 bytenr = btrfs_node_blockptr(buf, i);
439 ret = btrfs_inc_extent_ref(trans, root, bytenr,
440 btrfs_level_size(root, level - 1));
54aa1f4d
CM
441 if (ret) {
442 faili = i;
443 goto fail;
444 }
6407bf6d 445 }
02217ed2
CM
446 }
447 return 0;
54aa1f4d 448fail:
ccd467d6 449 WARN_ON(1);
54aa1f4d 450 for (i =0; i < faili; i++) {
db94535d
CM
451 if (level == 0) {
452 u64 disk_bytenr;
5f39d397
CM
453 btrfs_item_key_to_cpu(buf, &key, i);
454 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
54aa1f4d 455 continue;
5f39d397 456 fi = btrfs_item_ptr(buf, i,
54aa1f4d 457 struct btrfs_file_extent_item);
5f39d397 458 if (btrfs_file_extent_type(buf, fi) ==
54aa1f4d
CM
459 BTRFS_FILE_EXTENT_INLINE)
460 continue;
db94535d
CM
461 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
462 if (disk_bytenr == 0)
54aa1f4d 463 continue;
db94535d
CM
464 err = btrfs_free_extent(trans, root, disk_bytenr,
465 btrfs_file_extent_disk_num_bytes(buf,
5f39d397 466 fi), 0);
54aa1f4d
CM
467 BUG_ON(err);
468 } else {
db94535d
CM
469 bytenr = btrfs_node_blockptr(buf, i);
470 err = btrfs_free_extent(trans, root, bytenr,
471 btrfs_level_size(root, level - 1), 0);
54aa1f4d
CM
472 BUG_ON(err);
473 }
474 }
475 return ret;
02217ed2
CM
476}
477
9078a3e1
CM
478static int write_one_cache_group(struct btrfs_trans_handle *trans,
479 struct btrfs_root *root,
480 struct btrfs_path *path,
481 struct btrfs_block_group_cache *cache)
482{
483 int ret;
484 int pending_ret;
485 struct btrfs_root *extent_root = root->fs_info->extent_root;
5f39d397
CM
486 unsigned long bi;
487 struct extent_buffer *leaf;
9078a3e1 488
9078a3e1 489 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
54aa1f4d
CM
490 if (ret < 0)
491 goto fail;
9078a3e1 492 BUG_ON(ret);
5f39d397
CM
493
494 leaf = path->nodes[0];
495 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
496 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
497 btrfs_mark_buffer_dirty(leaf);
9078a3e1 498 btrfs_release_path(extent_root, path);
54aa1f4d 499fail:
9078a3e1
CM
500 finish_current_insert(trans, extent_root);
501 pending_ret = del_pending_extents(trans, extent_root);
502 if (ret)
503 return ret;
504 if (pending_ret)
505 return pending_ret;
506 return 0;
507
508}
509
96b5179d
CM
510int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
511 struct btrfs_root *root)
9078a3e1 512{
96b5179d
CM
513 struct extent_map_tree *block_group_cache;
514 struct btrfs_block_group_cache *cache;
9078a3e1
CM
515 int ret;
516 int err = 0;
517 int werr = 0;
9078a3e1 518 struct btrfs_path *path;
96b5179d
CM
519 u64 last = 0;
520 u64 start;
521 u64 end;
522 u64 ptr;
9078a3e1 523
96b5179d 524 block_group_cache = &root->fs_info->block_group_cache;
9078a3e1
CM
525 path = btrfs_alloc_path();
526 if (!path)
527 return -ENOMEM;
528
529 while(1) {
96b5179d
CM
530 ret = find_first_extent_bit(block_group_cache, last,
531 &start, &end, BLOCK_GROUP_DIRTY);
532 if (ret)
9078a3e1 533 break;
54aa1f4d 534
96b5179d
CM
535 last = end + 1;
536 ret = get_state_private(block_group_cache, start, &ptr);
537 if (ret)
538 break;
539
540 cache = (struct btrfs_block_group_cache *)ptr;
541 err = write_one_cache_group(trans, root,
542 path, cache);
543 /*
544 * if we fail to write the cache group, we want
545 * to keep it marked dirty in hopes that a later
546 * write will work
547 */
548 if (err) {
549 werr = err;
550 continue;
9078a3e1 551 }
96b5179d
CM
552 clear_extent_bits(block_group_cache, start, end,
553 BLOCK_GROUP_DIRTY, GFP_NOFS);
9078a3e1
CM
554 }
555 btrfs_free_path(path);
556 return werr;
557}
558
559static int update_block_group(struct btrfs_trans_handle *trans,
560 struct btrfs_root *root,
db94535d
CM
561 u64 bytenr, u64 num_bytes, int alloc,
562 int mark_free, int data)
9078a3e1
CM
563{
564 struct btrfs_block_group_cache *cache;
565 struct btrfs_fs_info *info = root->fs_info;
db94535d 566 u64 total = num_bytes;
9078a3e1 567 u64 old_val;
db94535d 568 u64 byte_in_group;
96b5179d
CM
569 u64 start;
570 u64 end;
3e1ad54f 571
9078a3e1 572 while(total) {
db94535d 573 cache = btrfs_lookup_block_group(info, bytenr);
3e1ad54f 574 if (!cache) {
9078a3e1 575 return -1;
cd1bc465 576 }
db94535d
CM
577 byte_in_group = bytenr - cache->key.objectid;
578 WARN_ON(byte_in_group > cache->key.offset);
96b5179d
CM
579 start = cache->key.objectid;
580 end = start + cache->key.offset - 1;
581 set_extent_bits(&info->block_group_cache, start, end,
582 BLOCK_GROUP_DIRTY, GFP_NOFS);
9078a3e1
CM
583
584 old_val = btrfs_block_group_used(&cache->item);
db94535d 585 num_bytes = min(total, cache->key.offset - byte_in_group);
cd1bc465 586 if (alloc) {
1e2677e0 587 if (cache->data != data &&
84f54cfa 588 old_val < (cache->key.offset >> 1)) {
96b5179d
CM
589 int bit_to_clear;
590 int bit_to_set;
1e2677e0 591
96b5179d 592 cache->data = data;
1e2677e0 593 if (data) {
96b5179d
CM
594 bit_to_clear = BLOCK_GROUP_DATA;
595 bit_to_set = BLOCK_GROUP_METADATA;
1e2677e0
CM
596 cache->item.flags |=
597 BTRFS_BLOCK_GROUP_DATA;
598 } else {
96b5179d
CM
599 bit_to_clear = BLOCK_GROUP_METADATA;
600 bit_to_set = BLOCK_GROUP_DATA;
1e2677e0
CM
601 cache->item.flags &=
602 ~BTRFS_BLOCK_GROUP_DATA;
603 }
96b5179d
CM
604 clear_extent_bits(&info->block_group_cache,
605 start, end, bit_to_clear,
606 GFP_NOFS);
607 set_extent_bits(&info->block_group_cache,
608 start, end, bit_to_set,
609 GFP_NOFS);
1e2677e0 610 }
db94535d 611 old_val += num_bytes;
cd1bc465 612 } else {
db94535d 613 old_val -= num_bytes;
f510cfec
CM
614 if (mark_free) {
615 set_extent_dirty(&info->free_space_cache,
db94535d 616 bytenr, bytenr + num_bytes - 1,
f510cfec 617 GFP_NOFS);
e37c9e69 618 }
cd1bc465 619 }
9078a3e1 620 btrfs_set_block_group_used(&cache->item, old_val);
db94535d
CM
621 total -= num_bytes;
622 bytenr += num_bytes;
9078a3e1
CM
623 }
624 return 0;
625}
626
1a5bc167 627int btrfs_copy_pinned(struct btrfs_root *root, struct extent_map_tree *copy)
ccd467d6 628{
ccd467d6 629 u64 last = 0;
1a5bc167
CM
630 u64 start;
631 u64 end;
632 struct extent_map_tree *pinned_extents = &root->fs_info->pinned_extents;
ccd467d6 633 int ret;
ccd467d6
CM
634
635 while(1) {
1a5bc167
CM
636 ret = find_first_extent_bit(pinned_extents, last,
637 &start, &end, EXTENT_DIRTY);
638 if (ret)
ccd467d6 639 break;
1a5bc167
CM
640 set_extent_dirty(copy, start, end, GFP_NOFS);
641 last = end + 1;
ccd467d6
CM
642 }
643 return 0;
644}
645
646int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
647 struct btrfs_root *root,
1a5bc167 648 struct extent_map_tree *unpin)
a28ec197 649{
1a5bc167
CM
650 u64 start;
651 u64 end;
a28ec197 652 int ret;
1a5bc167 653 struct extent_map_tree *pinned_extents = &root->fs_info->pinned_extents;
f510cfec
CM
654 struct extent_map_tree *free_space_cache;
655
656 free_space_cache = &root->fs_info->free_space_cache;
a28ec197
CM
657
658 while(1) {
1a5bc167
CM
659 ret = find_first_extent_bit(unpin, 0, &start, &end,
660 EXTENT_DIRTY);
661 if (ret)
a28ec197 662 break;
1a5bc167
CM
663
664 clear_extent_dirty(pinned_extents, start, end,
665 GFP_NOFS);
666 clear_extent_dirty(unpin, start, end, GFP_NOFS);
667 set_extent_dirty(free_space_cache, start, end, GFP_NOFS);
a28ec197
CM
668 }
669 return 0;
670}
671
e089f05c
CM
672static int finish_current_insert(struct btrfs_trans_handle *trans, struct
673 btrfs_root *extent_root)
037e6390 674{
e2fa7227 675 struct btrfs_key ins;
234b63a0 676 struct btrfs_extent_item extent_item;
037e6390 677 int ret;
1a5bc167
CM
678 int err = 0;
679 u64 start;
680 u64 end;
1261ec42 681 struct btrfs_fs_info *info = extent_root->fs_info;
037e6390 682
5f39d397 683 btrfs_set_stack_extent_refs(&extent_item, 1);
62e2749e 684 btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
5f39d397
CM
685 btrfs_set_stack_extent_owner(&extent_item,
686 extent_root->root_key.objectid);
037e6390 687
26b8003f 688 while(1) {
1a5bc167
CM
689 ret = find_first_extent_bit(&info->extent_ins, 0, &start,
690 &end, EXTENT_LOCKED);
691 if (ret)
26b8003f
CM
692 break;
693
1a5bc167
CM
694 ins.objectid = start;
695 ins.offset = end + 1 - start;
696 err = btrfs_insert_item(trans, extent_root, &ins,
697 &extent_item, sizeof(extent_item));
698 clear_extent_bits(&info->extent_ins, start, end, EXTENT_LOCKED,
699 GFP_NOFS);
037e6390 700 }
037e6390
CM
701 return 0;
702}
703
db94535d
CM
704static int pin_down_bytes(struct btrfs_root *root, u64 bytenr, u32 num_bytes,
705 int pending)
e20d96d6 706{
1a5bc167 707 int err = 0;
5f39d397 708 struct extent_buffer *buf;
8ef97622 709
f4b9aa8d 710 if (!pending) {
db94535d 711 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
5f39d397
CM
712 if (buf) {
713 if (btrfs_buffer_uptodate(buf)) {
2c90e5d6
CM
714 u64 transid =
715 root->fs_info->running_transaction->transid;
5f39d397
CM
716 if (btrfs_header_generation(buf) == transid) {
717 free_extent_buffer(buf);
2c90e5d6
CM
718 return 0;
719 }
f4b9aa8d 720 }
5f39d397 721 free_extent_buffer(buf);
8ef97622 722 }
1a5bc167 723 set_extent_dirty(&root->fs_info->pinned_extents,
db94535d 724 bytenr, bytenr + num_bytes - 1, GFP_NOFS);
f4b9aa8d 725 } else {
1a5bc167 726 set_extent_bits(&root->fs_info->pending_del,
db94535d
CM
727 bytenr, bytenr + num_bytes - 1,
728 EXTENT_LOCKED, GFP_NOFS);
f4b9aa8d 729 }
be744175 730 BUG_ON(err < 0);
e20d96d6
CM
731 return 0;
732}
733
fec577fb 734/*
a28ec197 735 * remove an extent from the root, returns 0 on success
fec577fb 736 */
e089f05c 737static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
db94535d 738 *root, u64 bytenr, u64 num_bytes, int pin,
e37c9e69 739 int mark_free)
a28ec197 740{
5caf2a00 741 struct btrfs_path *path;
e2fa7227 742 struct btrfs_key key;
1261ec42
CM
743 struct btrfs_fs_info *info = root->fs_info;
744 struct btrfs_root *extent_root = info->extent_root;
5f39d397 745 struct extent_buffer *leaf;
a28ec197 746 int ret;
234b63a0 747 struct btrfs_extent_item *ei;
cf27e1ee 748 u32 refs;
037e6390 749
db94535d 750 key.objectid = bytenr;
62e2749e 751 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
db94535d 752 key.offset = num_bytes;
a28ec197 753
5caf2a00 754 path = btrfs_alloc_path();
54aa1f4d
CM
755 if (!path)
756 return -ENOMEM;
5f26f772 757
54aa1f4d
CM
758 ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
759 if (ret < 0)
760 return ret;
761 BUG_ON(ret);
5f39d397
CM
762
763 leaf = path->nodes[0];
764 ei = btrfs_item_ptr(leaf, path->slots[0],
123abc88 765 struct btrfs_extent_item);
5f39d397
CM
766 refs = btrfs_extent_refs(leaf, ei);
767 BUG_ON(refs == 0);
768 refs -= 1;
769 btrfs_set_extent_refs(leaf, ei, refs);
770 btrfs_mark_buffer_dirty(leaf);
771
cf27e1ee 772 if (refs == 0) {
db94535d
CM
773 u64 super_used;
774 u64 root_used;
78fae27e
CM
775
776 if (pin) {
db94535d 777 ret = pin_down_bytes(root, bytenr, num_bytes, 0);
78fae27e
CM
778 BUG_ON(ret);
779 }
780
58176a96 781 /* block accounting for super block */
db94535d
CM
782 super_used = btrfs_super_bytes_used(&info->super_copy);
783 btrfs_set_super_bytes_used(&info->super_copy,
784 super_used - num_bytes);
58176a96
JB
785
786 /* block accounting for root item */
db94535d 787 root_used = btrfs_root_used(&root->root_item);
5f39d397 788 btrfs_set_root_used(&root->root_item,
db94535d 789 root_used - num_bytes);
58176a96 790
5caf2a00 791 ret = btrfs_del_item(trans, extent_root, path);
54aa1f4d
CM
792 if (ret) {
793 return ret;
794 }
db94535d 795 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
1e2677e0 796 mark_free, 0);
9078a3e1 797 BUG_ON(ret);
a28ec197 798 }
5caf2a00 799 btrfs_free_path(path);
e089f05c 800 finish_current_insert(trans, extent_root);
a28ec197
CM
801 return ret;
802}
803
a28ec197
CM
804/*
805 * find all the blocks marked as pending in the radix tree and remove
806 * them from the extent map
807 */
e089f05c
CM
808static int del_pending_extents(struct btrfs_trans_handle *trans, struct
809 btrfs_root *extent_root)
a28ec197
CM
810{
811 int ret;
e20d96d6 812 int err = 0;
1a5bc167
CM
813 u64 start;
814 u64 end;
815 struct extent_map_tree *pending_del;
816 struct extent_map_tree *pinned_extents;
8ef97622 817
1a5bc167
CM
818 pending_del = &extent_root->fs_info->pending_del;
819 pinned_extents = &extent_root->fs_info->pinned_extents;
a28ec197
CM
820
821 while(1) {
1a5bc167
CM
822 ret = find_first_extent_bit(pending_del, 0, &start, &end,
823 EXTENT_LOCKED);
824 if (ret)
a28ec197 825 break;
1a5bc167
CM
826
827 set_extent_dirty(pinned_extents, start, end, GFP_NOFS);
828 clear_extent_bits(pending_del, start, end, EXTENT_LOCKED,
829 GFP_NOFS);
830 ret = __free_extent(trans, extent_root,
831 start, end + 1 - start, 0, 0);
832 if (ret)
833 err = ret;
fec577fb 834 }
e20d96d6 835 return err;
fec577fb
CM
836}
837
838/*
839 * remove an extent from the root, returns 0 on success
840 */
e089f05c 841int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
db94535d 842 *root, u64 bytenr, u64 num_bytes, int pin)
fec577fb 843{
9f5fae2f 844 struct btrfs_root *extent_root = root->fs_info->extent_root;
fec577fb
CM
845 int pending_ret;
846 int ret;
a28ec197 847
db94535d 848 WARN_ON(num_bytes < root->sectorsize);
fec577fb 849 if (root == extent_root) {
db94535d 850 pin_down_bytes(root, bytenr, num_bytes, 1);
fec577fb
CM
851 return 0;
852 }
db94535d 853 ret = __free_extent(trans, root, bytenr, num_bytes, pin, pin == 0);
e20d96d6 854 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
fec577fb
CM
855 return ret ? ret : pending_ret;
856}
857
858/*
859 * walks the btree of allocated extents and find a hole of a given size.
860 * The key ins is changed to record the hole:
861 * ins->objectid == block start
62e2749e 862 * ins->flags = BTRFS_EXTENT_ITEM_KEY
fec577fb
CM
863 * ins->offset == number of blocks
864 * Any available blocks before search_start are skipped.
865 */
e089f05c 866static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
db94535d
CM
867 *orig_root, u64 num_bytes, u64 empty_size,
868 u64 search_start, u64 search_end, u64 hint_byte,
f2654de4
CM
869 struct btrfs_key *ins, u64 exclude_start,
870 u64 exclude_nr, int data)
fec577fb 871{
5caf2a00 872 struct btrfs_path *path;
e2fa7227 873 struct btrfs_key key;
fec577fb
CM
874 int ret;
875 u64 hole_size = 0;
876 int slot = 0;
db94535d 877 u64 last_byte = 0;
be744175 878 u64 orig_search_start = search_start;
fec577fb 879 int start_found;
5f39d397 880 struct extent_buffer *l;
9f5fae2f 881 struct btrfs_root * root = orig_root->fs_info->extent_root;
f2458e1d 882 struct btrfs_fs_info *info = root->fs_info;
db94535d 883 u64 total_needed = num_bytes;
e20d96d6 884 int level;
be08c1b9 885 struct btrfs_block_group_cache *block_group;
be744175 886 int full_scan = 0;
fbdc762b 887 int wrapped = 0;
fec577fb 888
db94535d 889 WARN_ON(num_bytes < root->sectorsize);
b1a4d965
CM
890 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
891
5f39d397
CM
892 level = btrfs_header_level(root->node);
893
3e1ad54f 894 if (search_end == (u64)-1)
db94535d
CM
895 search_end = btrfs_super_total_bytes(&info->super_copy);
896 if (hint_byte) {
897 block_group = btrfs_lookup_block_group(info, hint_byte);
be744175 898 block_group = btrfs_find_block_group(root, block_group,
db94535d 899 hint_byte, data, 1);
be744175
CM
900 } else {
901 block_group = btrfs_find_block_group(root,
902 trans->block_group, 0,
de428b63 903 data, 1);
be744175
CM
904 }
905
6702ed49 906 total_needed += empty_size;
e011599b
CM
907 path = btrfs_alloc_path();
908
be744175 909check_failed:
f510cfec
CM
910 search_start = find_search_start(root, &block_group,
911 search_start, total_needed, data);
5caf2a00 912 btrfs_init_path(path);
fec577fb
CM
913 ins->objectid = search_start;
914 ins->offset = 0;
fec577fb 915 start_found = 0;
2cc58cf2 916 path->reada = 2;
e37c9e69 917
5caf2a00 918 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
0f70abe2
CM
919 if (ret < 0)
920 goto error;
aa5d6bed 921
e37c9e69 922 if (path->slots[0] > 0) {
5caf2a00 923 path->slots[0]--;
e37c9e69
CM
924 }
925
5f39d397
CM
926 l = path->nodes[0];
927 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
928
e37c9e69
CM
929 /*
930 * a rare case, go back one key if we hit a block group item
931 * instead of an extent item
932 */
933 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY &&
934 key.objectid + key.offset >= search_start) {
935 ins->objectid = key.objectid;
936 ins->offset = key.offset - 1;
937 btrfs_release_path(root, path);
938 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
939 if (ret < 0)
940 goto error;
941
942 if (path->slots[0] > 0) {
943 path->slots[0]--;
944 }
945 }
0579da42 946
fec577fb 947 while (1) {
5f39d397 948 l = path->nodes[0];
5caf2a00 949 slot = path->slots[0];
5f39d397 950 if (slot >= btrfs_header_nritems(l)) {
5caf2a00 951 ret = btrfs_next_leaf(root, path);
fec577fb
CM
952 if (ret == 0)
953 continue;
0f70abe2
CM
954 if (ret < 0)
955 goto error;
e19caa5f
CM
956
957 search_start = max(search_start,
958 block_group->key.objectid);
fec577fb
CM
959 if (!start_found) {
960 ins->objectid = search_start;
3e1ad54f 961 ins->offset = search_end - search_start;
fec577fb
CM
962 start_found = 1;
963 goto check_pending;
964 }
db94535d
CM
965 ins->objectid = last_byte > search_start ?
966 last_byte : search_start;
3e1ad54f 967 ins->offset = search_end - ins->objectid;
e19caa5f 968 BUG_ON(ins->objectid >= search_end);
fec577fb
CM
969 goto check_pending;
970 }
5f39d397 971 btrfs_item_key_to_cpu(l, &key, slot);
96b5179d 972
db94535d 973 if (key.objectid >= search_start && key.objectid > last_byte &&
e37c9e69 974 start_found) {
db94535d
CM
975 if (last_byte < search_start)
976 last_byte = search_start;
977 hole_size = key.objectid - last_byte;
978 if (hole_size >= num_bytes) {
979 ins->objectid = last_byte;
e37c9e69
CM
980 ins->offset = hole_size;
981 goto check_pending;
0579da42 982 }
fec577fb 983 }
96b5179d
CM
984 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY) {
985 if (!start_found) {
db94535d 986 last_byte = key.objectid;
96b5179d
CM
987 start_found = 1;
988 }
e37c9e69 989 goto next;
96b5179d
CM
990 }
991
e37c9e69 992
0579da42 993 start_found = 1;
db94535d 994 last_byte = key.objectid + key.offset;
f510cfec 995
db94535d 996 if (!full_scan && last_byte >= block_group->key.objectid +
be744175
CM
997 block_group->key.offset) {
998 btrfs_release_path(root, path);
999 search_start = block_group->key.objectid +
e19caa5f 1000 block_group->key.offset;
be744175
CM
1001 goto new_group;
1002 }
9078a3e1 1003next:
5caf2a00 1004 path->slots[0]++;
de428b63 1005 cond_resched();
fec577fb 1006 }
fec577fb
CM
1007check_pending:
1008 /* we have to make sure we didn't find an extent that has already
1009 * been allocated by the map tree or the original allocation
1010 */
5caf2a00 1011 btrfs_release_path(root, path);
fec577fb 1012 BUG_ON(ins->objectid < search_start);
e37c9e69 1013
db94535d 1014 if (ins->objectid + num_bytes >= search_end)
cf67582b
CM
1015 goto enospc;
1016
e19caa5f
CM
1017 if (!full_scan && ins->objectid + num_bytes >= block_group->
1018 key.objectid + block_group->key.offset) {
1019 search_start = block_group->key.objectid +
1020 block_group->key.offset;
1021 goto new_group;
1022 }
1a5bc167 1023 if (test_range_bit(&info->extent_ins, ins->objectid,
db94535d
CM
1024 ins->objectid + num_bytes -1, EXTENT_LOCKED, 0)) {
1025 search_start = ins->objectid + num_bytes;
1a5bc167
CM
1026 goto new_group;
1027 }
1028 if (test_range_bit(&info->pinned_extents, ins->objectid,
db94535d
CM
1029 ins->objectid + num_bytes -1, EXTENT_DIRTY, 0)) {
1030 search_start = ins->objectid + num_bytes;
1a5bc167 1031 goto new_group;
fec577fb 1032 }
db94535d 1033 if (exclude_nr > 0 && (ins->objectid + num_bytes > exclude_start &&
f2654de4
CM
1034 ins->objectid < exclude_start + exclude_nr)) {
1035 search_start = exclude_start + exclude_nr;
1036 goto new_group;
1037 }
e37c9e69 1038 if (!data) {
5276aeda 1039 block_group = btrfs_lookup_block_group(info, ins->objectid);
26b8003f
CM
1040 if (block_group)
1041 trans->block_group = block_group;
f2458e1d 1042 }
db94535d 1043 ins->offset = num_bytes;
5caf2a00 1044 btrfs_free_path(path);
fec577fb 1045 return 0;
be744175
CM
1046
1047new_group:
db94535d 1048 if (search_start + num_bytes >= search_end) {
cf67582b 1049enospc:
be744175 1050 search_start = orig_search_start;
fbdc762b
CM
1051 if (full_scan) {
1052 ret = -ENOSPC;
1053 goto error;
1054 }
6702ed49
CM
1055 if (wrapped) {
1056 if (!full_scan)
1057 total_needed -= empty_size;
fbdc762b 1058 full_scan = 1;
6702ed49 1059 } else
fbdc762b 1060 wrapped = 1;
be744175 1061 }
5276aeda 1062 block_group = btrfs_lookup_block_group(info, search_start);
fbdc762b 1063 cond_resched();
be744175
CM
1064 if (!full_scan)
1065 block_group = btrfs_find_block_group(root, block_group,
de428b63 1066 search_start, data, 0);
be744175
CM
1067 goto check_failed;
1068
0f70abe2 1069error:
5caf2a00
CM
1070 btrfs_release_path(root, path);
1071 btrfs_free_path(path);
0f70abe2 1072 return ret;
fec577fb 1073}
fec577fb
CM
1074/*
1075 * finds a free extent and does all the dirty work required for allocation
1076 * returns the key for the extent through ins, and a tree buffer for
1077 * the first block of the extent through buf.
1078 *
1079 * returns 0 if everything worked, non-zero otherwise.
1080 */
4d775673
CM
1081int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
1082 struct btrfs_root *root, u64 owner,
db94535d 1083 u64 num_bytes, u64 empty_size, u64 hint_byte,
be08c1b9 1084 u64 search_end, struct btrfs_key *ins, int data)
fec577fb
CM
1085{
1086 int ret;
1087 int pending_ret;
db94535d 1088 u64 super_used, root_used;
fbdc762b 1089 u64 search_start = 0;
1261ec42
CM
1090 struct btrfs_fs_info *info = root->fs_info;
1091 struct btrfs_root *extent_root = info->extent_root;
234b63a0 1092 struct btrfs_extent_item extent_item;
037e6390 1093
5f39d397
CM
1094 btrfs_set_stack_extent_refs(&extent_item, 1);
1095 btrfs_set_stack_extent_owner(&extent_item, owner);
fec577fb 1096
db94535d
CM
1097 WARN_ON(num_bytes < root->sectorsize);
1098 ret = find_free_extent(trans, root, num_bytes, empty_size,
1099 search_start, search_end, hint_byte, ins,
26b8003f
CM
1100 trans->alloc_exclude_start,
1101 trans->alloc_exclude_nr, data);
ccd467d6 1102 BUG_ON(ret);
f2654de4
CM
1103 if (ret)
1104 return ret;
fec577fb 1105
58176a96 1106 /* block accounting for super block */
db94535d
CM
1107 super_used = btrfs_super_bytes_used(&info->super_copy);
1108 btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes);
26b8003f 1109
58176a96 1110 /* block accounting for root item */
db94535d
CM
1111 root_used = btrfs_root_used(&root->root_item);
1112 btrfs_set_root_used(&root->root_item, root_used + num_bytes);
58176a96 1113
f510cfec
CM
1114 clear_extent_dirty(&root->fs_info->free_space_cache,
1115 ins->objectid, ins->objectid + ins->offset - 1,
1116 GFP_NOFS);
1117
26b8003f 1118 if (root == extent_root) {
1a5bc167
CM
1119 set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
1120 ins->objectid + ins->offset - 1,
1121 EXTENT_LOCKED, GFP_NOFS);
e19caa5f 1122 WARN_ON(data == 1);
26b8003f
CM
1123 goto update_block;
1124 }
1125
1126 WARN_ON(trans->alloc_exclude_nr);
1127 trans->alloc_exclude_start = ins->objectid;
1128 trans->alloc_exclude_nr = ins->offset;
e089f05c
CM
1129 ret = btrfs_insert_item(trans, extent_root, ins, &extent_item,
1130 sizeof(extent_item));
037e6390 1131
26b8003f
CM
1132 trans->alloc_exclude_start = 0;
1133 trans->alloc_exclude_nr = 0;
1134
ccd467d6 1135 BUG_ON(ret);
e089f05c 1136 finish_current_insert(trans, extent_root);
e20d96d6 1137 pending_ret = del_pending_extents(trans, extent_root);
f510cfec 1138
e37c9e69 1139 if (ret) {
037e6390 1140 return ret;
e37c9e69
CM
1141 }
1142 if (pending_ret) {
037e6390 1143 return pending_ret;
e37c9e69 1144 }
26b8003f
CM
1145
1146update_block:
1e2677e0
CM
1147 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0,
1148 data);
fabb5681 1149 BUG_ON(ret);
037e6390 1150 return 0;
fec577fb
CM
1151}
1152
1153/*
1154 * helper function to allocate a block for a given tree
1155 * returns the tree buffer or NULL.
1156 */
5f39d397 1157struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
db94535d
CM
1158 struct btrfs_root *root,
1159 u32 blocksize, u64 hint,
5f39d397 1160 u64 empty_size)
fec577fb 1161{
e2fa7227 1162 struct btrfs_key ins;
fec577fb 1163 int ret;
5f39d397 1164 struct extent_buffer *buf;
fec577fb 1165
4d775673 1166 ret = btrfs_alloc_extent(trans, root, root->root_key.objectid,
db94535d
CM
1167 blocksize, empty_size, hint,
1168 (u64)-1, &ins, 0);
fec577fb 1169 if (ret) {
54aa1f4d
CM
1170 BUG_ON(ret > 0);
1171 return ERR_PTR(ret);
fec577fb 1172 }
db94535d 1173 buf = btrfs_find_create_tree_block(root, ins.objectid, blocksize);
54aa1f4d 1174 if (!buf) {
db94535d 1175 btrfs_free_extent(trans, root, ins.objectid, blocksize, 0);
54aa1f4d
CM
1176 return ERR_PTR(-ENOMEM);
1177 }
5f39d397
CM
1178 btrfs_set_buffer_uptodate(buf);
1179 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
1180 buf->start + buf->len - 1, GFP_NOFS);
6b80053d 1181 btrfs_set_buffer_defrag(buf);
d3c2fdcf 1182 trans->blocks_used++;
fec577fb
CM
1183 return buf;
1184}
a28ec197 1185
6407bf6d 1186static int drop_leaf_ref(struct btrfs_trans_handle *trans,
5f39d397 1187 struct btrfs_root *root, struct extent_buffer *leaf)
6407bf6d 1188{
5f39d397 1189 struct btrfs_key key;
6407bf6d
CM
1190 struct btrfs_file_extent_item *fi;
1191 int i;
1192 int nritems;
1193 int ret;
1194
5f39d397
CM
1195 BUG_ON(!btrfs_is_leaf(leaf));
1196 nritems = btrfs_header_nritems(leaf);
6407bf6d 1197 for (i = 0; i < nritems; i++) {
db94535d 1198 u64 disk_bytenr;
5f39d397
CM
1199
1200 btrfs_item_key_to_cpu(leaf, &key, i);
1201 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
6407bf6d
CM
1202 continue;
1203 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
5f39d397
CM
1204 if (btrfs_file_extent_type(leaf, fi) ==
1205 BTRFS_FILE_EXTENT_INLINE)
236454df 1206 continue;
6407bf6d
CM
1207 /*
1208 * FIXME make sure to insert a trans record that
1209 * repeats the snapshot del on crash
1210 */
db94535d
CM
1211 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
1212 if (disk_bytenr == 0)
3a686375 1213 continue;
db94535d
CM
1214 ret = btrfs_free_extent(trans, root, disk_bytenr,
1215 btrfs_file_extent_disk_num_bytes(leaf, fi), 0);
6407bf6d
CM
1216 BUG_ON(ret);
1217 }
1218 return 0;
1219}
1220
e011599b 1221static void reada_walk_down(struct btrfs_root *root,
5f39d397 1222 struct extent_buffer *node)
e011599b
CM
1223{
1224 int i;
1225 u32 nritems;
db94535d 1226 u64 bytenr;
e011599b
CM
1227 int ret;
1228 u32 refs;
db94535d
CM
1229 int level;
1230 u32 blocksize;
e011599b 1231
5f39d397 1232 nritems = btrfs_header_nritems(node);
db94535d 1233 level = btrfs_header_level(node);
e011599b 1234 for (i = 0; i < nritems; i++) {
db94535d
CM
1235 bytenr = btrfs_node_blockptr(node, i);
1236 blocksize = btrfs_level_size(root, level - 1);
1237 ret = lookup_extent_ref(NULL, root, bytenr, blocksize, &refs);
e011599b
CM
1238 BUG_ON(ret);
1239 if (refs != 1)
1240 continue;
409eb95d 1241 mutex_unlock(&root->fs_info->fs_mutex);
db94535d 1242 ret = readahead_tree_block(root, bytenr, blocksize);
409eb95d
CM
1243 cond_resched();
1244 mutex_lock(&root->fs_info->fs_mutex);
e011599b
CM
1245 if (ret)
1246 break;
1247 }
1248}
1249
9aca1d51
CM
1250/*
1251 * helper function for drop_snapshot, this walks down the tree dropping ref
1252 * counts as it goes.
1253 */
e089f05c
CM
1254static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
1255 *root, struct btrfs_path *path, int *level)
20524f02 1256{
5f39d397
CM
1257 struct extent_buffer *next;
1258 struct extent_buffer *cur;
db94535d
CM
1259 u64 bytenr;
1260 u32 blocksize;
20524f02
CM
1261 int ret;
1262 u32 refs;
1263
5caf2a00
CM
1264 WARN_ON(*level < 0);
1265 WARN_ON(*level >= BTRFS_MAX_LEVEL);
5f39d397 1266 ret = lookup_extent_ref(trans, root,
db94535d
CM
1267 path->nodes[*level]->start,
1268 path->nodes[*level]->len, &refs);
20524f02
CM
1269 BUG_ON(ret);
1270 if (refs > 1)
1271 goto out;
e011599b 1272
9aca1d51
CM
1273 /*
1274 * walk down to the last node level and free all the leaves
1275 */
6407bf6d 1276 while(*level >= 0) {
5caf2a00
CM
1277 WARN_ON(*level < 0);
1278 WARN_ON(*level >= BTRFS_MAX_LEVEL);
20524f02 1279 cur = path->nodes[*level];
e011599b
CM
1280
1281 if (*level > 0 && path->slots[*level] == 0)
5f39d397 1282 reada_walk_down(root, cur);
e011599b 1283
5f39d397 1284 if (btrfs_header_level(cur) != *level)
2c90e5d6 1285 WARN_ON(1);
e011599b 1286
7518a238 1287 if (path->slots[*level] >=
5f39d397 1288 btrfs_header_nritems(cur))
20524f02 1289 break;
6407bf6d
CM
1290 if (*level == 0) {
1291 ret = drop_leaf_ref(trans, root, cur);
1292 BUG_ON(ret);
1293 break;
1294 }
db94535d
CM
1295 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
1296 blocksize = btrfs_level_size(root, *level - 1);
1297 ret = lookup_extent_ref(trans, root, bytenr, blocksize, &refs);
6407bf6d
CM
1298 BUG_ON(ret);
1299 if (refs != 1) {
20524f02 1300 path->slots[*level]++;
db94535d
CM
1301 ret = btrfs_free_extent(trans, root, bytenr,
1302 blocksize, 1);
20524f02
CM
1303 BUG_ON(ret);
1304 continue;
1305 }
db94535d 1306 next = btrfs_find_tree_block(root, bytenr, blocksize);
5f39d397
CM
1307 if (!next || !btrfs_buffer_uptodate(next)) {
1308 free_extent_buffer(next);
e9d0b13b 1309 mutex_unlock(&root->fs_info->fs_mutex);
db94535d 1310 next = read_tree_block(root, bytenr, blocksize);
e9d0b13b
CM
1311 mutex_lock(&root->fs_info->fs_mutex);
1312
1313 /* we dropped the lock, check one more time */
db94535d
CM
1314 ret = lookup_extent_ref(trans, root, bytenr,
1315 blocksize, &refs);
e9d0b13b
CM
1316 BUG_ON(ret);
1317 if (refs != 1) {
1318 path->slots[*level]++;
5f39d397 1319 free_extent_buffer(next);
e9d0b13b 1320 ret = btrfs_free_extent(trans, root,
db94535d 1321 bytenr, blocksize, 1);
e9d0b13b
CM
1322 BUG_ON(ret);
1323 continue;
1324 }
1325 }
5caf2a00 1326 WARN_ON(*level <= 0);
83e15a28 1327 if (path->nodes[*level-1])
5f39d397 1328 free_extent_buffer(path->nodes[*level-1]);
20524f02 1329 path->nodes[*level-1] = next;
5f39d397 1330 *level = btrfs_header_level(next);
20524f02
CM
1331 path->slots[*level] = 0;
1332 }
1333out:
5caf2a00
CM
1334 WARN_ON(*level < 0);
1335 WARN_ON(*level >= BTRFS_MAX_LEVEL);
db94535d
CM
1336 ret = btrfs_free_extent(trans, root, path->nodes[*level]->start,
1337 path->nodes[*level]->len, 1);
5f39d397 1338 free_extent_buffer(path->nodes[*level]);
20524f02
CM
1339 path->nodes[*level] = NULL;
1340 *level += 1;
1341 BUG_ON(ret);
1342 return 0;
1343}
1344
9aca1d51
CM
1345/*
1346 * helper for dropping snapshots. This walks back up the tree in the path
1347 * to find the first node higher up where we haven't yet gone through
1348 * all the slots
1349 */
e089f05c
CM
1350static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root
1351 *root, struct btrfs_path *path, int *level)
20524f02
CM
1352{
1353 int i;
1354 int slot;
1355 int ret;
9f3a7427
CM
1356 struct btrfs_root_item *root_item = &root->root_item;
1357
234b63a0 1358 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
20524f02 1359 slot = path->slots[i];
5f39d397
CM
1360 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
1361 struct extent_buffer *node;
1362 struct btrfs_disk_key disk_key;
1363 node = path->nodes[i];
20524f02
CM
1364 path->slots[i]++;
1365 *level = i;
9f3a7427 1366 WARN_ON(*level == 0);
5f39d397 1367 btrfs_node_key(node, &disk_key, path->slots[i]);
9f3a7427 1368 memcpy(&root_item->drop_progress,
5f39d397 1369 &disk_key, sizeof(disk_key));
9f3a7427 1370 root_item->drop_level = i;
20524f02
CM
1371 return 0;
1372 } else {
e089f05c 1373 ret = btrfs_free_extent(trans, root,
db94535d
CM
1374 path->nodes[*level]->start,
1375 path->nodes[*level]->len, 1);
6407bf6d 1376 BUG_ON(ret);
5f39d397 1377 free_extent_buffer(path->nodes[*level]);
83e15a28 1378 path->nodes[*level] = NULL;
20524f02 1379 *level = i + 1;
20524f02
CM
1380 }
1381 }
1382 return 1;
1383}
1384
9aca1d51
CM
1385/*
1386 * drop the reference count on the tree rooted at 'snap'. This traverses
1387 * the tree freeing any blocks that have a ref count of zero after being
1388 * decremented.
1389 */
e089f05c 1390int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
9f3a7427 1391 *root)
20524f02 1392{
3768f368 1393 int ret = 0;
9aca1d51 1394 int wret;
20524f02 1395 int level;
5caf2a00 1396 struct btrfs_path *path;
20524f02
CM
1397 int i;
1398 int orig_level;
9f3a7427 1399 struct btrfs_root_item *root_item = &root->root_item;
20524f02 1400
5caf2a00
CM
1401 path = btrfs_alloc_path();
1402 BUG_ON(!path);
20524f02 1403
5f39d397 1404 level = btrfs_header_level(root->node);
20524f02 1405 orig_level = level;
9f3a7427
CM
1406 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
1407 path->nodes[level] = root->node;
f510cfec 1408 extent_buffer_get(root->node);
9f3a7427
CM
1409 path->slots[level] = 0;
1410 } else {
1411 struct btrfs_key key;
5f39d397
CM
1412 struct btrfs_disk_key found_key;
1413 struct extent_buffer *node;
6702ed49 1414
9f3a7427 1415 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
6702ed49
CM
1416 level = root_item->drop_level;
1417 path->lowest_level = level;
9f3a7427 1418 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6702ed49 1419 if (wret < 0) {
9f3a7427
CM
1420 ret = wret;
1421 goto out;
1422 }
5f39d397
CM
1423 node = path->nodes[level];
1424 btrfs_node_key(node, &found_key, path->slots[level]);
1425 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
1426 sizeof(found_key)));
9f3a7427 1427 }
20524f02 1428 while(1) {
5caf2a00 1429 wret = walk_down_tree(trans, root, path, &level);
9aca1d51 1430 if (wret > 0)
20524f02 1431 break;
9aca1d51
CM
1432 if (wret < 0)
1433 ret = wret;
1434
5caf2a00 1435 wret = walk_up_tree(trans, root, path, &level);
9aca1d51 1436 if (wret > 0)
20524f02 1437 break;
9aca1d51
CM
1438 if (wret < 0)
1439 ret = wret;
409eb95d 1440 ret = -EAGAIN;
409eb95d 1441 break;
20524f02 1442 }
83e15a28 1443 for (i = 0; i <= orig_level; i++) {
5caf2a00 1444 if (path->nodes[i]) {
5f39d397 1445 free_extent_buffer(path->nodes[i]);
6702ed49 1446 path->nodes[i] = 0;
83e15a28 1447 }
20524f02 1448 }
9f3a7427 1449out:
5caf2a00 1450 btrfs_free_path(path);
9aca1d51 1451 return ret;
20524f02 1452}
9078a3e1 1453
96b5179d 1454int btrfs_free_block_groups(struct btrfs_fs_info *info)
9078a3e1 1455{
96b5179d
CM
1456 u64 start;
1457 u64 end;
9078a3e1 1458 int ret;
9078a3e1
CM
1459
1460 while(1) {
96b5179d
CM
1461 ret = find_first_extent_bit(&info->block_group_cache, 0,
1462 &start, &end, (unsigned int)-1);
1463 if (ret)
9078a3e1 1464 break;
96b5179d
CM
1465 clear_extent_bits(&info->block_group_cache, start,
1466 end, (unsigned int)-1, GFP_NOFS);
9078a3e1 1467 }
e37c9e69 1468 while(1) {
f510cfec
CM
1469 ret = find_first_extent_bit(&info->free_space_cache, 0,
1470 &start, &end, EXTENT_DIRTY);
1471 if (ret)
e37c9e69 1472 break;
f510cfec
CM
1473 clear_extent_dirty(&info->free_space_cache, start,
1474 end, GFP_NOFS);
e37c9e69 1475 }
be744175
CM
1476 return 0;
1477}
1478
9078a3e1
CM
1479int btrfs_read_block_groups(struct btrfs_root *root)
1480{
1481 struct btrfs_path *path;
1482 int ret;
1483 int err = 0;
96b5179d 1484 int bit;
9078a3e1 1485 struct btrfs_block_group_cache *cache;
be744175 1486 struct btrfs_fs_info *info = root->fs_info;
96b5179d 1487 struct extent_map_tree *block_group_cache;
9078a3e1
CM
1488 struct btrfs_key key;
1489 struct btrfs_key found_key;
5f39d397 1490 struct extent_buffer *leaf;
96b5179d
CM
1491
1492 block_group_cache = &info->block_group_cache;
9078a3e1 1493
be744175 1494 root = info->extent_root;
9078a3e1 1495 key.objectid = 0;
db94535d 1496 key.offset = BTRFS_BLOCK_GROUP_SIZE;
9078a3e1
CM
1497 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
1498
1499 path = btrfs_alloc_path();
1500 if (!path)
1501 return -ENOMEM;
1502
1503 while(1) {
be744175 1504 ret = btrfs_search_slot(NULL, info->extent_root,
9078a3e1
CM
1505 &key, path, 0, 0);
1506 if (ret != 0) {
1507 err = ret;
1508 break;
1509 }
5f39d397
CM
1510 leaf = path->nodes[0];
1511 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
9078a3e1
CM
1512 cache = kmalloc(sizeof(*cache), GFP_NOFS);
1513 if (!cache) {
1514 err = -1;
1515 break;
1516 }
3e1ad54f 1517
5f39d397
CM
1518 read_extent_buffer(leaf, &cache->item,
1519 btrfs_item_ptr_offset(leaf, path->slots[0]),
1520 sizeof(cache->item));
9078a3e1 1521 memcpy(&cache->key, &found_key, sizeof(found_key));
e37c9e69
CM
1522 cache->cached = 0;
1523
9078a3e1
CM
1524 key.objectid = found_key.objectid + found_key.offset;
1525 btrfs_release_path(root, path);
5f39d397 1526
96b5179d
CM
1527 if (cache->item.flags & BTRFS_BLOCK_GROUP_DATA) {
1528 bit = BLOCK_GROUP_DATA;
1529 cache->data = 1;
1530 } else {
1531 bit = BLOCK_GROUP_METADATA;
1532 cache->data = 0;
31f3c99b 1533 }
96b5179d
CM
1534
1535 /* use EXTENT_LOCKED to prevent merging */
1536 set_extent_bits(block_group_cache, found_key.objectid,
1537 found_key.objectid + found_key.offset - 1,
1538 bit | EXTENT_LOCKED, GFP_NOFS);
1539 set_state_private(block_group_cache, found_key.objectid,
1540 (u64)cache);
1541
9078a3e1 1542 if (key.objectid >=
db94535d 1543 btrfs_super_total_bytes(&info->super_copy))
9078a3e1
CM
1544 break;
1545 }
1546
1547 btrfs_free_path(path);
1548 return 0;
1549}