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