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