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