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