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