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