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