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