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