Btrfs: fix page cache memory leak
[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
9 search_end, struct btrfs_key *ins);
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
31f3c99b
CM
15struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
16 struct btrfs_block_group_cache
17 *hint, int data)
cd1bc465
CM
18{
19 struct btrfs_block_group_cache *cache[8];
31f3c99b 20 struct btrfs_block_group_cache *found_group = NULL;
cd1bc465
CM
21 struct btrfs_fs_info *info = root->fs_info;
22 u64 used;
31f3c99b
CM
23 u64 last = 0;
24 u64 hint_last;
cd1bc465
CM
25 int i;
26 int ret;
31f3c99b
CM
27 int full_search = 0;
28 if (hint) {
29 used = btrfs_block_group_used(&hint->item);
30 if (used < (hint->key.offset * 2) / 3) {
31 return hint;
32 }
33 radix_tree_tag_clear(&info->block_group_radix,
34 hint->key.objectid + hint->key.offset - 1,
35 BTRFS_BLOCK_GROUP_AVAIL);
36 last = hint->key.objectid + hint->key.offset;
37 hint_last = last;
38 } else {
39 hint_last = 0;
40 last = 0;
41 }
cd1bc465
CM
42 while(1) {
43 ret = radix_tree_gang_lookup_tag(&info->block_group_radix,
44 (void **)cache,
45 last, ARRAY_SIZE(cache),
31f3c99b 46 BTRFS_BLOCK_GROUP_AVAIL);
cd1bc465
CM
47 if (!ret)
48 break;
49 for (i = 0; i < ret; i++) {
50 used = btrfs_block_group_used(&cache[i]->item);
31f3c99b 51 if (used < (cache[i]->key.offset * 2) / 3) {
cd1bc465 52 info->block_group_cache = cache[i];
31f3c99b
CM
53 found_group = cache[i];
54 goto found;
cd1bc465 55 }
31f3c99b
CM
56 radix_tree_tag_clear(&info->block_group_radix,
57 cache[i]->key.objectid +
58 cache[i]->key.offset - 1,
59 BTRFS_BLOCK_GROUP_AVAIL);
cd1bc465 60 last = cache[i]->key.objectid +
31f3c99b 61 cache[i]->key.offset;
cd1bc465
CM
62 }
63 }
31f3c99b
CM
64 last = hint_last;
65again:
cd1bc465
CM
66 while(1) {
67 ret = radix_tree_gang_lookup(&info->block_group_radix,
68 (void **)cache,
69 last, ARRAY_SIZE(cache));
70 if (!ret)
71 break;
72 for (i = 0; i < ret; i++) {
73 used = btrfs_block_group_used(&cache[i]->item);
31f3c99b 74 if (used < cache[i]->key.offset) {
cd1bc465 75 info->block_group_cache = cache[i];
31f3c99b
CM
76 found_group = cache[i];
77 goto found;
cd1bc465 78 }
31f3c99b
CM
79 radix_tree_tag_clear(&info->block_group_radix,
80 cache[i]->key.objectid +
81 cache[i]->key.offset - 1,
82 BTRFS_BLOCK_GROUP_AVAIL);
cd1bc465 83 last = cache[i]->key.objectid +
31f3c99b 84 cache[i]->key.offset;
cd1bc465
CM
85 }
86 }
87 info->block_group_cache = NULL;
31f3c99b
CM
88 if (!full_search) {
89 last = 0;
90 full_search = 1;
91 goto again;
92 }
93found:
94 if (!found_group) {
95 ret = radix_tree_gang_lookup(&info->block_group_radix,
96 (void **)&found_group, 0, 1);
97 BUG_ON(ret != 1);
98 }
99 return found_group;
cd1bc465
CM
100}
101
b18c6685
CM
102int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
103 struct btrfs_root *root,
104 u64 blocknr, u64 num_blocks)
02217ed2 105{
5caf2a00 106 struct btrfs_path *path;
02217ed2 107 int ret;
e2fa7227 108 struct btrfs_key key;
234b63a0
CM
109 struct btrfs_leaf *l;
110 struct btrfs_extent_item *item;
e2fa7227 111 struct btrfs_key ins;
cf27e1ee 112 u32 refs;
037e6390 113
9f5fae2f
CM
114 find_free_extent(trans, root->fs_info->extent_root, 0, 0, (u64)-1,
115 &ins);
5caf2a00
CM
116 path = btrfs_alloc_path();
117 BUG_ON(!path);
118 btrfs_init_path(path);
02217ed2
CM
119 key.objectid = blocknr;
120 key.flags = 0;
62e2749e 121 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
6407bf6d 122 key.offset = num_blocks;
5caf2a00 123 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
9f5fae2f 124 0, 1);
a429e513
CM
125 if (ret != 0) {
126printk("can't find block %Lu %Lu\n", blocknr, num_blocks);
a28ec197 127 BUG();
a429e513 128 }
02217ed2 129 BUG_ON(ret != 0);
5caf2a00
CM
130 l = btrfs_buffer_leaf(path->nodes[0]);
131 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
cf27e1ee
CM
132 refs = btrfs_extent_refs(item);
133 btrfs_set_extent_refs(item, refs + 1);
5caf2a00 134 btrfs_mark_buffer_dirty(path->nodes[0]);
a28ec197 135
5caf2a00
CM
136 btrfs_release_path(root->fs_info->extent_root, path);
137 btrfs_free_path(path);
9f5fae2f 138 finish_current_insert(trans, root->fs_info->extent_root);
e20d96d6 139 del_pending_extents(trans, root->fs_info->extent_root);
02217ed2
CM
140 return 0;
141}
142
b18c6685
CM
143static int lookup_extent_ref(struct btrfs_trans_handle *trans,
144 struct btrfs_root *root, u64 blocknr,
145 u64 num_blocks, u32 *refs)
a28ec197 146{
5caf2a00 147 struct btrfs_path *path;
a28ec197 148 int ret;
e2fa7227 149 struct btrfs_key key;
234b63a0
CM
150 struct btrfs_leaf *l;
151 struct btrfs_extent_item *item;
5caf2a00
CM
152
153 path = btrfs_alloc_path();
154 btrfs_init_path(path);
a28ec197 155 key.objectid = blocknr;
6407bf6d 156 key.offset = num_blocks;
62e2749e
CM
157 key.flags = 0;
158 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
5caf2a00 159 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
9f5fae2f 160 0, 0);
a28ec197
CM
161 if (ret != 0)
162 BUG();
5caf2a00
CM
163 l = btrfs_buffer_leaf(path->nodes[0]);
164 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
cf27e1ee 165 *refs = btrfs_extent_refs(item);
5caf2a00
CM
166 btrfs_release_path(root->fs_info->extent_root, path);
167 btrfs_free_path(path);
a28ec197
CM
168 return 0;
169}
170
c5739bba
CM
171int btrfs_inc_root_ref(struct btrfs_trans_handle *trans,
172 struct btrfs_root *root)
173{
b18c6685 174 return btrfs_inc_extent_ref(trans, root, bh_blocknr(root->node), 1);
c5739bba
CM
175}
176
e089f05c 177int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
e20d96d6 178 struct buffer_head *buf)
02217ed2
CM
179{
180 u64 blocknr;
e20d96d6 181 struct btrfs_node *buf_node;
6407bf6d
CM
182 struct btrfs_leaf *buf_leaf;
183 struct btrfs_disk_key *key;
184 struct btrfs_file_extent_item *fi;
02217ed2 185 int i;
6407bf6d
CM
186 int leaf;
187 int ret;
a28ec197 188
3768f368 189 if (!root->ref_cows)
a28ec197 190 return 0;
e20d96d6 191 buf_node = btrfs_buffer_node(buf);
6407bf6d
CM
192 leaf = btrfs_is_leaf(buf_node);
193 buf_leaf = btrfs_buffer_leaf(buf);
e20d96d6 194 for (i = 0; i < btrfs_header_nritems(&buf_node->header); i++) {
6407bf6d
CM
195 if (leaf) {
196 key = &buf_leaf->items[i].key;
197 if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY)
198 continue;
199 fi = btrfs_item_ptr(buf_leaf, i,
200 struct btrfs_file_extent_item);
236454df
CM
201 if (btrfs_file_extent_type(fi) ==
202 BTRFS_FILE_EXTENT_INLINE)
203 continue;
b18c6685 204 ret = btrfs_inc_extent_ref(trans, root,
6407bf6d
CM
205 btrfs_file_extent_disk_blocknr(fi),
206 btrfs_file_extent_disk_num_blocks(fi));
207 BUG_ON(ret);
208 } else {
209 blocknr = btrfs_node_blockptr(buf_node, i);
b18c6685 210 ret = btrfs_inc_extent_ref(trans, root, blocknr, 1);
6407bf6d
CM
211 BUG_ON(ret);
212 }
02217ed2
CM
213 }
214 return 0;
215}
216
9078a3e1
CM
217static int write_one_cache_group(struct btrfs_trans_handle *trans,
218 struct btrfs_root *root,
219 struct btrfs_path *path,
220 struct btrfs_block_group_cache *cache)
221{
222 int ret;
223 int pending_ret;
224 struct btrfs_root *extent_root = root->fs_info->extent_root;
225 struct btrfs_block_group_item *bi;
226 struct btrfs_key ins;
227
228 find_free_extent(trans, extent_root, 0, 0, (u64)-1, &ins);
229 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
230 BUG_ON(ret);
231 bi = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
232 struct btrfs_block_group_item);
233 memcpy(bi, &cache->item, sizeof(*bi));
234 mark_buffer_dirty(path->nodes[0]);
235 btrfs_release_path(extent_root, path);
236
237 finish_current_insert(trans, extent_root);
238 pending_ret = del_pending_extents(trans, extent_root);
239 if (ret)
240 return ret;
241 if (pending_ret)
242 return pending_ret;
243 return 0;
244
245}
246
247int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
248 struct btrfs_root *root)
249{
250 struct btrfs_block_group_cache *cache[8];
251 int ret;
252 int err = 0;
253 int werr = 0;
254 struct radix_tree_root *radix = &root->fs_info->block_group_radix;
255 int i;
256 struct btrfs_path *path;
257
258 path = btrfs_alloc_path();
259 if (!path)
260 return -ENOMEM;
261
262 while(1) {
263 ret = radix_tree_gang_lookup_tag(radix, (void **)cache,
264 0, ARRAY_SIZE(cache),
265 BTRFS_BLOCK_GROUP_DIRTY);
266 if (!ret)
267 break;
268 for (i = 0; i < ret; i++) {
269 radix_tree_tag_clear(radix, cache[i]->key.objectid +
270 cache[i]->key.offset - 1,
271 BTRFS_BLOCK_GROUP_DIRTY);
272 err = write_one_cache_group(trans, root,
273 path, cache[i]);
274 if (err)
275 werr = err;
31f3c99b 276 cache[i]->last_alloc = cache[i]->first_free;
9078a3e1
CM
277 }
278 }
279 btrfs_free_path(path);
280 return werr;
281}
282
283static int update_block_group(struct btrfs_trans_handle *trans,
284 struct btrfs_root *root,
285 u64 blocknr, u64 num, int alloc)
286{
287 struct btrfs_block_group_cache *cache;
288 struct btrfs_fs_info *info = root->fs_info;
289 u64 total = num;
290 u64 old_val;
291 u64 block_in_group;
292 int ret;
293 while(total) {
294 ret = radix_tree_gang_lookup(&info->block_group_radix,
295 (void **)&cache, blocknr, 1);
cd1bc465
CM
296 if (!ret) {
297 printk(KERN_CRIT "blocknr %Lu lookup failed\n",
298 blocknr);
9078a3e1 299 return -1;
cd1bc465 300 }
9078a3e1
CM
301 block_in_group = blocknr - cache->key.objectid;
302 WARN_ON(block_in_group > cache->key.offset);
303 radix_tree_tag_set(&info->block_group_radix,
304 cache->key.objectid + cache->key.offset - 1,
305 BTRFS_BLOCK_GROUP_DIRTY);
306
307 old_val = btrfs_block_group_used(&cache->item);
308 num = min(total, cache->key.offset - block_in_group);
309 total -= num;
310 blocknr += num;
cd1bc465 311 if (alloc) {
9078a3e1 312 old_val += num;
cd1bc465
CM
313 if (blocknr > cache->last_alloc)
314 cache->last_alloc = blocknr;
315 } else {
9078a3e1 316 old_val -= num;
cd1bc465
CM
317 if (blocknr < cache->first_free)
318 cache->first_free = blocknr;
319 }
9078a3e1
CM
320 btrfs_set_block_group_used(&cache->item, old_val);
321 }
322 return 0;
323}
324
e089f05c
CM
325int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, struct
326 btrfs_root *root)
a28ec197 327{
8ef97622 328 unsigned long gang[8];
88fd146c 329 u64 first = 0;
a28ec197
CM
330 int ret;
331 int i;
8ef97622 332 struct radix_tree_root *pinned_radix = &root->fs_info->pinned_radix;
a28ec197
CM
333
334 while(1) {
8ef97622
CM
335 ret = find_first_radix_bit(pinned_radix, gang,
336 ARRAY_SIZE(gang));
a28ec197
CM
337 if (!ret)
338 break;
88fd146c 339 if (!first)
8ef97622 340 first = gang[0];
0579da42 341 for (i = 0; i < ret; i++) {
8ef97622 342 clear_radix_bit(pinned_radix, gang[i]);
0579da42 343 }
a28ec197
CM
344 }
345 return 0;
346}
347
e089f05c
CM
348static int finish_current_insert(struct btrfs_trans_handle *trans, struct
349 btrfs_root *extent_root)
037e6390 350{
e2fa7227 351 struct btrfs_key ins;
234b63a0 352 struct btrfs_extent_item extent_item;
037e6390
CM
353 int i;
354 int ret;
1261ec42
CM
355 u64 super_blocks_used;
356 struct btrfs_fs_info *info = extent_root->fs_info;
037e6390 357
cf27e1ee 358 btrfs_set_extent_refs(&extent_item, 1);
037e6390
CM
359 ins.offset = 1;
360 ins.flags = 0;
62e2749e 361 btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
5d0c3e60 362 btrfs_set_extent_owner(&extent_item, extent_root->root_key.objectid);
037e6390 363
f2458e1d
CM
364 for (i = 0; i < extent_root->fs_info->extent_tree_insert_nr; i++) {
365 ins.objectid = extent_root->fs_info->extent_tree_insert[i];
1261ec42
CM
366 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
367 btrfs_set_super_blocks_used(info->disk_super,
368 super_blocks_used + 1);
e089f05c
CM
369 ret = btrfs_insert_item(trans, extent_root, &ins, &extent_item,
370 sizeof(extent_item));
037e6390
CM
371 BUG_ON(ret);
372 }
f2458e1d
CM
373 extent_root->fs_info->extent_tree_insert_nr = 0;
374 extent_root->fs_info->extent_tree_prealloc_nr = 0;
037e6390
CM
375 return 0;
376}
377
8ef97622 378static int pin_down_block(struct btrfs_root *root, u64 blocknr, int pending)
e20d96d6
CM
379{
380 int err;
78fae27e 381 struct btrfs_header *header;
8ef97622
CM
382 struct buffer_head *bh;
383
f4b9aa8d 384 if (!pending) {
d98237b3 385 bh = btrfs_find_tree_block(root, blocknr);
2c90e5d6
CM
386 if (bh) {
387 if (buffer_uptodate(bh)) {
388 u64 transid =
389 root->fs_info->running_transaction->transid;
390 header = btrfs_buffer_header(bh);
391 if (btrfs_header_generation(header) ==
392 transid) {
393 btrfs_block_release(root, bh);
394 return 0;
395 }
f4b9aa8d 396 }
d6025579 397 btrfs_block_release(root, bh);
8ef97622 398 }
8ef97622 399 err = set_radix_bit(&root->fs_info->pinned_radix, blocknr);
f4b9aa8d
CM
400 } else {
401 err = set_radix_bit(&root->fs_info->pending_del_radix, blocknr);
402 }
8ef97622 403 BUG_ON(err);
e20d96d6
CM
404 return 0;
405}
406
fec577fb 407/*
a28ec197 408 * remove an extent from the root, returns 0 on success
fec577fb 409 */
e089f05c 410static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
78fae27e 411 *root, u64 blocknr, u64 num_blocks, int pin)
a28ec197 412{
5caf2a00 413 struct btrfs_path *path;
e2fa7227 414 struct btrfs_key key;
1261ec42
CM
415 struct btrfs_fs_info *info = root->fs_info;
416 struct btrfs_root *extent_root = info->extent_root;
a28ec197 417 int ret;
234b63a0 418 struct btrfs_extent_item *ei;
e2fa7227 419 struct btrfs_key ins;
cf27e1ee 420 u32 refs;
037e6390 421
a28ec197
CM
422 key.objectid = blocknr;
423 key.flags = 0;
62e2749e 424 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
a28ec197
CM
425 key.offset = num_blocks;
426
e089f05c 427 find_free_extent(trans, root, 0, 0, (u64)-1, &ins);
5caf2a00
CM
428 path = btrfs_alloc_path();
429 BUG_ON(!path);
430 btrfs_init_path(path);
5f26f772 431
5caf2a00 432 ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
a28ec197 433 if (ret) {
2e635a27 434 printk("failed to find %Lu\n", key.objectid);
234b63a0 435 btrfs_print_tree(extent_root, extent_root->node);
2e635a27 436 printk("failed to find %Lu\n", key.objectid);
a28ec197
CM
437 BUG();
438 }
5caf2a00 439 ei = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
123abc88 440 struct btrfs_extent_item);
a28ec197 441 BUG_ON(ei->refs == 0);
cf27e1ee
CM
442 refs = btrfs_extent_refs(ei) - 1;
443 btrfs_set_extent_refs(ei, refs);
5caf2a00 444 btrfs_mark_buffer_dirty(path->nodes[0]);
cf27e1ee 445 if (refs == 0) {
1261ec42 446 u64 super_blocks_used;
78fae27e
CM
447
448 if (pin) {
8ef97622 449 ret = pin_down_block(root, blocknr, 0);
78fae27e
CM
450 BUG_ON(ret);
451 }
452
1261ec42
CM
453 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
454 btrfs_set_super_blocks_used(info->disk_super,
455 super_blocks_used - num_blocks);
5caf2a00 456 ret = btrfs_del_item(trans, extent_root, path);
a28ec197
CM
457 if (ret)
458 BUG();
9078a3e1
CM
459 ret = update_block_group(trans, root, blocknr, num_blocks, 0);
460 BUG_ON(ret);
a28ec197 461 }
5caf2a00
CM
462 btrfs_release_path(extent_root, path);
463 btrfs_free_path(path);
e089f05c 464 finish_current_insert(trans, extent_root);
a28ec197
CM
465 return ret;
466}
467
a28ec197
CM
468/*
469 * find all the blocks marked as pending in the radix tree and remove
470 * them from the extent map
471 */
e089f05c
CM
472static int del_pending_extents(struct btrfs_trans_handle *trans, struct
473 btrfs_root *extent_root)
a28ec197
CM
474{
475 int ret;
e20d96d6
CM
476 int wret;
477 int err = 0;
8ef97622 478 unsigned long gang[4];
a28ec197 479 int i;
8ef97622
CM
480 struct radix_tree_root *pending_radix;
481 struct radix_tree_root *pinned_radix;
482
483 pending_radix = &extent_root->fs_info->pending_del_radix;
484 pinned_radix = &extent_root->fs_info->pinned_radix;
a28ec197
CM
485
486 while(1) {
8ef97622
CM
487 ret = find_first_radix_bit(pending_radix, gang,
488 ARRAY_SIZE(gang));
a28ec197
CM
489 if (!ret)
490 break;
491 for (i = 0; i < ret; i++) {
8ef97622
CM
492 wret = set_radix_bit(pinned_radix, gang[i]);
493 BUG_ON(wret);
494 wret = clear_radix_bit(pending_radix, gang[i]);
495 BUG_ON(wret);
d5719762 496 wret = __free_extent(trans, extent_root,
8ef97622 497 gang[i], 1, 0);
e20d96d6
CM
498 if (wret)
499 err = wret;
fec577fb
CM
500 }
501 }
e20d96d6 502 return err;
fec577fb
CM
503}
504
505/*
506 * remove an extent from the root, returns 0 on success
507 */
e089f05c
CM
508int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
509 *root, u64 blocknr, u64 num_blocks, int pin)
fec577fb 510{
9f5fae2f 511 struct btrfs_root *extent_root = root->fs_info->extent_root;
fec577fb
CM
512 int pending_ret;
513 int ret;
a28ec197 514
fec577fb 515 if (root == extent_root) {
8ef97622 516 pin_down_block(root, blocknr, 1);
fec577fb
CM
517 return 0;
518 }
78fae27e 519 ret = __free_extent(trans, root, blocknr, num_blocks, pin);
e20d96d6 520 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
fec577fb
CM
521 return ret ? ret : pending_ret;
522}
523
524/*
525 * walks the btree of allocated extents and find a hole of a given size.
526 * The key ins is changed to record the hole:
527 * ins->objectid == block start
62e2749e 528 * ins->flags = BTRFS_EXTENT_ITEM_KEY
fec577fb
CM
529 * ins->offset == number of blocks
530 * Any available blocks before search_start are skipped.
531 */
e089f05c
CM
532static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
533 *orig_root, u64 num_blocks, u64 search_start, u64
534 search_end, struct btrfs_key *ins)
fec577fb 535{
5caf2a00 536 struct btrfs_path *path;
e2fa7227 537 struct btrfs_key key;
fec577fb
CM
538 int ret;
539 u64 hole_size = 0;
540 int slot = 0;
e20d96d6 541 u64 last_block = 0;
037e6390 542 u64 test_block;
fec577fb 543 int start_found;
234b63a0 544 struct btrfs_leaf *l;
9f5fae2f 545 struct btrfs_root * root = orig_root->fs_info->extent_root;
f2458e1d 546 struct btrfs_fs_info *info = root->fs_info;
0579da42 547 int total_needed = num_blocks;
f2458e1d
CM
548 int total_found = 0;
549 int fill_prealloc = 0;
e20d96d6 550 int level;
31f3c99b
CM
551 int update_block_group = 0;
552 struct btrfs_block_group_cache *hint_block_group;
fec577fb 553
b1a4d965
CM
554 path = btrfs_alloc_path();
555 ins->flags = 0;
556 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
557
e20d96d6 558 level = btrfs_header_level(btrfs_buffer_header(root->node));
31f3c99b
CM
559 /* find search start here */
560 if (0 && search_start && num_blocks) {
561 u64 used;
562 ret = radix_tree_gang_lookup(&info->block_group_radix,
563 (void **)&hint_block_group,
564 search_start, 1);
565 if (ret) {
566 used = btrfs_block_group_used(&hint_block_group->item);
567 if (used > (hint_block_group->key.offset * 9) / 10)
568 search_start = 0;
569 else if (search_start < hint_block_group->last_alloc)
570 search_start = hint_block_group->last_alloc;
571 } else {
572 search_start = 0;
573 }
574 }
f2458e1d
CM
575 if (num_blocks == 0) {
576 fill_prealloc = 1;
577 num_blocks = 1;
308535a0 578 total_needed = (min(level + 1, BTRFS_MAX_LEVEL) + 2) * 3;
f2458e1d 579 }
31f3c99b
CM
580 if (1 || !search_start) {
581 trans->block_group = btrfs_find_block_group(root,
582 trans->block_group,
583 0);
584 if (trans->block_group->last_alloc > search_start)
585 search_start = trans->block_group->last_alloc;
586 update_block_group = 1;
587 }
fec577fb 588check_failed:
5caf2a00 589 btrfs_init_path(path);
fec577fb
CM
590 ins->objectid = search_start;
591 ins->offset = 0;
fec577fb 592 start_found = 0;
5caf2a00 593 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
0f70abe2
CM
594 if (ret < 0)
595 goto error;
aa5d6bed 596
5caf2a00
CM
597 if (path->slots[0] > 0)
598 path->slots[0]--;
0579da42 599
fec577fb 600 while (1) {
5caf2a00
CM
601 l = btrfs_buffer_leaf(path->nodes[0]);
602 slot = path->slots[0];
7518a238 603 if (slot >= btrfs_header_nritems(&l->header)) {
f2458e1d
CM
604 if (fill_prealloc) {
605 info->extent_tree_prealloc_nr = 0;
606 total_found = 0;
607 }
5caf2a00 608 ret = btrfs_next_leaf(root, path);
fec577fb
CM
609 if (ret == 0)
610 continue;
0f70abe2
CM
611 if (ret < 0)
612 goto error;
fec577fb
CM
613 if (!start_found) {
614 ins->objectid = search_start;
f2458e1d 615 ins->offset = (u64)-1 - search_start;
fec577fb
CM
616 start_found = 1;
617 goto check_pending;
618 }
619 ins->objectid = last_block > search_start ?
620 last_block : search_start;
f2458e1d 621 ins->offset = (u64)-1 - ins->objectid;
fec577fb
CM
622 goto check_pending;
623 }
e2fa7227 624 btrfs_disk_key_to_cpu(&key, &l->items[slot].key);
9078a3e1
CM
625 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY)
626 goto next;
e2fa7227 627 if (key.objectid >= search_start) {
fec577fb 628 if (start_found) {
0579da42
CM
629 if (last_block < search_start)
630 last_block = search_start;
e2fa7227 631 hole_size = key.objectid - last_block;
28b8bb9e 632 if (hole_size >= num_blocks) {
fec577fb 633 ins->objectid = last_block;
037e6390 634 ins->offset = hole_size;
fec577fb
CM
635 goto check_pending;
636 }
0579da42 637 }
fec577fb 638 }
0579da42 639 start_found = 1;
e2fa7227 640 last_block = key.objectid + key.offset;
9078a3e1 641next:
5caf2a00 642 path->slots[0]++;
fec577fb
CM
643 }
644 // FIXME -ENOSPC
645check_pending:
646 /* we have to make sure we didn't find an extent that has already
647 * been allocated by the map tree or the original allocation
648 */
5caf2a00 649 btrfs_release_path(root, path);
fec577fb 650 BUG_ON(ins->objectid < search_start);
06a2f9fa
CM
651 if (ins->objectid >= btrfs_super_total_blocks(info->disk_super)) {
652 if (search_start == 0)
653 return -ENOSPC;
654 search_start = 0;
655 goto check_failed;
656 }
037e6390 657 for (test_block = ins->objectid;
f2458e1d
CM
658 test_block < ins->objectid + num_blocks; test_block++) {
659 if (test_radix_bit(&info->pinned_radix, test_block)) {
037e6390 660 search_start = test_block + 1;
fec577fb
CM
661 goto check_failed;
662 }
663 }
f2458e1d
CM
664 if (!fill_prealloc && info->extent_tree_insert_nr) {
665 u64 last =
666 info->extent_tree_insert[info->extent_tree_insert_nr - 1];
667 if (ins->objectid + num_blocks >
668 info->extent_tree_insert[0] &&
669 ins->objectid <= last) {
670 search_start = last + 1;
671 WARN_ON(1);
672 goto check_failed;
673 }
674 }
675 if (!fill_prealloc && info->extent_tree_prealloc_nr) {
676 u64 first =
677 info->extent_tree_prealloc[info->extent_tree_prealloc_nr - 1];
678 if (ins->objectid + num_blocks > first &&
679 ins->objectid <= info->extent_tree_prealloc[0]) {
680 search_start = info->extent_tree_prealloc[0] + 1;
681 WARN_ON(1);
682 goto check_failed;
683 }
684 }
685 if (fill_prealloc) {
686 int nr;
687 test_block = ins->objectid;
688 while(test_block < ins->objectid + ins->offset &&
689 total_found < total_needed) {
690 nr = total_needed - total_found - 1;
691 BUG_ON(nr < 0);
cd1bc465 692 info->extent_tree_prealloc[nr] = test_block;
f2458e1d
CM
693 total_found++;
694 test_block++;
695 }
696 if (total_found < total_needed) {
697 search_start = test_block;
698 goto check_failed;
699 }
cd1bc465
CM
700 info->extent_tree_prealloc_nr = total_found;
701 }
31f3c99b
CM
702 if (update_block_group) {
703 ret = radix_tree_gang_lookup(&info->block_group_radix,
704 (void **)&trans->block_group,
705 ins->objectid, 1);
706 if (ret) {
707 trans->block_group->last_alloc = ins->objectid;
708 }
f2458e1d 709 }
037e6390 710 ins->offset = num_blocks;
5caf2a00 711 btrfs_free_path(path);
fec577fb 712 return 0;
0f70abe2 713error:
5caf2a00
CM
714 btrfs_release_path(root, path);
715 btrfs_free_path(path);
0f70abe2 716 return ret;
fec577fb 717}
fec577fb
CM
718/*
719 * finds a free extent and does all the dirty work required for allocation
720 * returns the key for the extent through ins, and a tree buffer for
721 * the first block of the extent through buf.
722 *
723 * returns 0 if everything worked, non-zero otherwise.
724 */
4d775673
CM
725int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
726 struct btrfs_root *root, u64 owner,
c62a1920 727 u64 num_blocks, u64 search_start,
4d775673 728 u64 search_end, struct btrfs_key *ins)
fec577fb
CM
729{
730 int ret;
731 int pending_ret;
1261ec42
CM
732 u64 super_blocks_used;
733 struct btrfs_fs_info *info = root->fs_info;
734 struct btrfs_root *extent_root = info->extent_root;
234b63a0 735 struct btrfs_extent_item extent_item;
f2458e1d 736 struct btrfs_key prealloc_key;
037e6390 737
cf27e1ee 738 btrfs_set_extent_refs(&extent_item, 1);
4d775673 739 btrfs_set_extent_owner(&extent_item, owner);
fec577fb 740
037e6390 741 if (root == extent_root) {
f2458e1d
CM
742 int nr;
743 BUG_ON(info->extent_tree_prealloc_nr == 0);
037e6390 744 BUG_ON(num_blocks != 1);
037e6390 745 ins->offset = 1;
f2458e1d
CM
746 info->extent_tree_prealloc_nr--;
747 nr = info->extent_tree_prealloc_nr;
748 ins->objectid = info->extent_tree_prealloc[nr];
749 info->extent_tree_insert[info->extent_tree_insert_nr++] =
750 ins->objectid;
9078a3e1
CM
751 ret = update_block_group(trans, root,
752 ins->objectid, ins->offset, 1);
753 BUG_ON(ret);
fec577fb
CM
754 return 0;
755 }
f2458e1d 756 /* do the real allocation */
e089f05c 757 ret = find_free_extent(trans, root, num_blocks, search_start,
037e6390
CM
758 search_end, ins);
759 if (ret)
760 return ret;
fec577fb 761
f2458e1d
CM
762 /* then do prealloc for the extent tree */
763 ret = find_free_extent(trans, root, 0, ins->objectid + ins->offset,
764 search_end, &prealloc_key);
765 if (ret)
766 return ret;
767
1261ec42
CM
768 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
769 btrfs_set_super_blocks_used(info->disk_super, super_blocks_used +
770 num_blocks);
e089f05c
CM
771 ret = btrfs_insert_item(trans, extent_root, ins, &extent_item,
772 sizeof(extent_item));
037e6390 773
e089f05c 774 finish_current_insert(trans, extent_root);
e20d96d6 775 pending_ret = del_pending_extents(trans, extent_root);
037e6390
CM
776 if (ret)
777 return ret;
778 if (pending_ret)
779 return pending_ret;
9078a3e1 780 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1);
037e6390 781 return 0;
fec577fb
CM
782}
783
784/*
785 * helper function to allocate a block for a given tree
786 * returns the tree buffer or NULL.
787 */
e20d96d6 788struct buffer_head *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
31f3c99b 789 struct btrfs_root *root, u64 hint)
fec577fb 790{
e2fa7227 791 struct btrfs_key ins;
fec577fb 792 int ret;
e20d96d6 793 struct buffer_head *buf;
fec577fb 794
4d775673 795 ret = btrfs_alloc_extent(trans, root, root->root_key.objectid,
31f3c99b 796 1, hint, (unsigned long)-1, &ins);
fec577fb
CM
797 if (ret) {
798 BUG();
799 return NULL;
800 }
9078a3e1 801 BUG_ON(ret);
d98237b3 802 buf = btrfs_find_create_tree_block(root, ins.objectid);
df2ce34c 803 set_buffer_uptodate(buf);
090d1875 804 set_buffer_checked(buf);
7c4452b9 805 set_radix_bit(&trans->transaction->dirty_pages, buf->b_page->index);
fec577fb
CM
806 return buf;
807}
a28ec197 808
6407bf6d
CM
809static int drop_leaf_ref(struct btrfs_trans_handle *trans,
810 struct btrfs_root *root, struct buffer_head *cur)
811{
812 struct btrfs_disk_key *key;
813 struct btrfs_leaf *leaf;
814 struct btrfs_file_extent_item *fi;
815 int i;
816 int nritems;
817 int ret;
818
819 BUG_ON(!btrfs_is_leaf(btrfs_buffer_node(cur)));
820 leaf = btrfs_buffer_leaf(cur);
821 nritems = btrfs_header_nritems(&leaf->header);
822 for (i = 0; i < nritems; i++) {
823 key = &leaf->items[i].key;
824 if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY)
825 continue;
826 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
236454df
CM
827 if (btrfs_file_extent_type(fi) == BTRFS_FILE_EXTENT_INLINE)
828 continue;
6407bf6d
CM
829 /*
830 * FIXME make sure to insert a trans record that
831 * repeats the snapshot del on crash
832 */
833 ret = btrfs_free_extent(trans, root,
834 btrfs_file_extent_disk_blocknr(fi),
835 btrfs_file_extent_disk_num_blocks(fi),
836 0);
837 BUG_ON(ret);
838 }
839 return 0;
840}
841
9aca1d51
CM
842/*
843 * helper function for drop_snapshot, this walks down the tree dropping ref
844 * counts as it goes.
845 */
e089f05c
CM
846static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
847 *root, struct btrfs_path *path, int *level)
20524f02 848{
e20d96d6
CM
849 struct buffer_head *next;
850 struct buffer_head *cur;
20524f02
CM
851 u64 blocknr;
852 int ret;
853 u32 refs;
854
5caf2a00
CM
855 WARN_ON(*level < 0);
856 WARN_ON(*level >= BTRFS_MAX_LEVEL);
b18c6685 857 ret = lookup_extent_ref(trans, root, bh_blocknr(path->nodes[*level]),
6407bf6d 858 1, &refs);
20524f02
CM
859 BUG_ON(ret);
860 if (refs > 1)
861 goto out;
9aca1d51
CM
862 /*
863 * walk down to the last node level and free all the leaves
864 */
6407bf6d 865 while(*level >= 0) {
5caf2a00
CM
866 WARN_ON(*level < 0);
867 WARN_ON(*level >= BTRFS_MAX_LEVEL);
20524f02 868 cur = path->nodes[*level];
2c90e5d6
CM
869 if (btrfs_header_level(btrfs_buffer_header(cur)) != *level)
870 WARN_ON(1);
7518a238 871 if (path->slots[*level] >=
e20d96d6 872 btrfs_header_nritems(btrfs_buffer_header(cur)))
20524f02 873 break;
6407bf6d
CM
874 if (*level == 0) {
875 ret = drop_leaf_ref(trans, root, cur);
876 BUG_ON(ret);
877 break;
878 }
e20d96d6
CM
879 blocknr = btrfs_node_blockptr(btrfs_buffer_node(cur),
880 path->slots[*level]);
b18c6685 881 ret = lookup_extent_ref(trans, root, blocknr, 1, &refs);
6407bf6d
CM
882 BUG_ON(ret);
883 if (refs != 1) {
20524f02 884 path->slots[*level]++;
e089f05c 885 ret = btrfs_free_extent(trans, root, blocknr, 1, 1);
20524f02
CM
886 BUG_ON(ret);
887 continue;
888 }
20524f02 889 next = read_tree_block(root, blocknr);
5caf2a00 890 WARN_ON(*level <= 0);
83e15a28 891 if (path->nodes[*level-1])
234b63a0 892 btrfs_block_release(root, path->nodes[*level-1]);
20524f02 893 path->nodes[*level-1] = next;
e20d96d6 894 *level = btrfs_header_level(btrfs_buffer_header(next));
20524f02
CM
895 path->slots[*level] = 0;
896 }
897out:
5caf2a00
CM
898 WARN_ON(*level < 0);
899 WARN_ON(*level >= BTRFS_MAX_LEVEL);
6407bf6d 900 ret = btrfs_free_extent(trans, root,
7eccb903 901 bh_blocknr(path->nodes[*level]), 1, 1);
234b63a0 902 btrfs_block_release(root, path->nodes[*level]);
20524f02
CM
903 path->nodes[*level] = NULL;
904 *level += 1;
905 BUG_ON(ret);
906 return 0;
907}
908
9aca1d51
CM
909/*
910 * helper for dropping snapshots. This walks back up the tree in the path
911 * to find the first node higher up where we haven't yet gone through
912 * all the slots
913 */
e089f05c
CM
914static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root
915 *root, struct btrfs_path *path, int *level)
20524f02
CM
916{
917 int i;
918 int slot;
919 int ret;
234b63a0 920 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
20524f02 921 slot = path->slots[i];
e20d96d6
CM
922 if (slot < btrfs_header_nritems(
923 btrfs_buffer_header(path->nodes[i])) - 1) {
20524f02
CM
924 path->slots[i]++;
925 *level = i;
926 return 0;
927 } else {
e089f05c 928 ret = btrfs_free_extent(trans, root,
7eccb903 929 bh_blocknr(path->nodes[*level]),
e089f05c 930 1, 1);
6407bf6d 931 BUG_ON(ret);
234b63a0 932 btrfs_block_release(root, path->nodes[*level]);
83e15a28 933 path->nodes[*level] = NULL;
20524f02 934 *level = i + 1;
20524f02
CM
935 }
936 }
937 return 1;
938}
939
9aca1d51
CM
940/*
941 * drop the reference count on the tree rooted at 'snap'. This traverses
942 * the tree freeing any blocks that have a ref count of zero after being
943 * decremented.
944 */
e089f05c 945int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
e20d96d6 946 *root, struct buffer_head *snap)
20524f02 947{
3768f368 948 int ret = 0;
9aca1d51 949 int wret;
20524f02 950 int level;
5caf2a00 951 struct btrfs_path *path;
20524f02
CM
952 int i;
953 int orig_level;
954
5caf2a00
CM
955 path = btrfs_alloc_path();
956 BUG_ON(!path);
957 btrfs_init_path(path);
20524f02 958
e20d96d6 959 level = btrfs_header_level(btrfs_buffer_header(snap));
20524f02 960 orig_level = level;
5caf2a00
CM
961 path->nodes[level] = snap;
962 path->slots[level] = 0;
20524f02 963 while(1) {
5caf2a00 964 wret = walk_down_tree(trans, root, path, &level);
9aca1d51 965 if (wret > 0)
20524f02 966 break;
9aca1d51
CM
967 if (wret < 0)
968 ret = wret;
969
5caf2a00 970 wret = walk_up_tree(trans, root, path, &level);
9aca1d51 971 if (wret > 0)
20524f02 972 break;
9aca1d51
CM
973 if (wret < 0)
974 ret = wret;
35b7e476 975 btrfs_btree_balance_dirty(root);
20524f02 976 }
83e15a28 977 for (i = 0; i <= orig_level; i++) {
5caf2a00
CM
978 if (path->nodes[i]) {
979 btrfs_block_release(root, path->nodes[i]);
83e15a28 980 }
20524f02 981 }
5caf2a00 982 btrfs_free_path(path);
9aca1d51 983 return ret;
20524f02 984}
9078a3e1
CM
985
986int btrfs_free_block_groups(struct btrfs_fs_info *info)
987{
988 int ret;
989 struct btrfs_block_group_cache *cache[8];
990 int i;
991
992 while(1) {
993 ret = radix_tree_gang_lookup(&info->block_group_radix,
994 (void **)cache, 0,
995 ARRAY_SIZE(cache));
996 if (!ret)
997 break;
998 for (i = 0; i < ret; i++) {
999 radix_tree_delete(&info->block_group_radix,
1000 cache[i]->key.objectid +
1001 cache[i]->key.offset - 1);
1002 kfree(cache[i]);
1003 }
1004 }
1005 return 0;
1006}
1007
1008int btrfs_read_block_groups(struct btrfs_root *root)
1009{
1010 struct btrfs_path *path;
1011 int ret;
1012 int err = 0;
1013 struct btrfs_block_group_item *bi;
1014 struct btrfs_block_group_cache *cache;
1015 struct btrfs_key key;
1016 struct btrfs_key found_key;
1017 struct btrfs_leaf *leaf;
1018 u64 group_size_blocks = BTRFS_BLOCK_GROUP_SIZE / root->blocksize;
31f3c99b 1019 u64 used;
9078a3e1
CM
1020
1021 root = root->fs_info->extent_root;
1022 key.objectid = 0;
1023 key.offset = group_size_blocks;
1024 key.flags = 0;
1025 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
1026
1027 path = btrfs_alloc_path();
1028 if (!path)
1029 return -ENOMEM;
1030
1031 while(1) {
1032 ret = btrfs_search_slot(NULL, root->fs_info->extent_root,
1033 &key, path, 0, 0);
1034 if (ret != 0) {
1035 err = ret;
1036 break;
1037 }
1038 leaf = btrfs_buffer_leaf(path->nodes[0]);
1039 btrfs_disk_key_to_cpu(&found_key,
1040 &leaf->items[path->slots[0]].key);
1041 cache = kmalloc(sizeof(*cache), GFP_NOFS);
1042 if (!cache) {
1043 err = -1;
1044 break;
1045 }
1046 bi = btrfs_item_ptr(leaf, path->slots[0],
1047 struct btrfs_block_group_item);
1048 memcpy(&cache->item, bi, sizeof(*bi));
1049 memcpy(&cache->key, &found_key, sizeof(found_key));
31f3c99b
CM
1050 cache->last_alloc = cache->key.objectid;
1051 cache->first_free = cache->key.objectid;
9078a3e1
CM
1052 key.objectid = found_key.objectid + found_key.offset;
1053 btrfs_release_path(root, path);
1054 ret = radix_tree_insert(&root->fs_info->block_group_radix,
1055 found_key.objectid +
1056 found_key.offset - 1,
1057 (void *)cache);
1058 BUG_ON(ret);
31f3c99b
CM
1059 used = btrfs_block_group_used(bi);
1060 if (used < (key.offset * 2) / 3) {
1061 radix_tree_tag_set(&root->fs_info->block_group_radix,
1062 found_key.objectid +
1063 found_key.offset - 1,
1064 BTRFS_BLOCK_GROUP_AVAIL);
1065 }
9078a3e1
CM
1066 if (key.objectid >=
1067 btrfs_super_total_blocks(root->fs_info->disk_super))
1068 break;
1069 }
1070
1071 btrfs_free_path(path);
1072 return 0;
1073}