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