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