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