blob: 3edfc300289fbc4d63ffce0be797fee5aec498bb [file] [log] [blame]
Chris Mason2e635a22007-03-21 11:12:56 -04001#include <linux/module.h>
Chris Masonfec577f2007-02-26 10:40:21 -05002#include "ctree.h"
3#include "disk-io.h"
4#include "print-tree.h"
Chris Masone089f052007-03-16 16:20:31 -04005#include "transaction.h"
Chris Masonfec577f2007-02-26 10:40:21 -05006
Chris Masone089f052007-03-16 16:20:31 -04007static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
8 *orig_root, u64 num_blocks, u64 search_start, u64
Chris Masonbe08c1b2007-05-03 09:06:49 -04009 search_end, struct btrfs_key *ins, int data);
Chris Masone089f052007-03-16 16:20:31 -040010static int finish_current_insert(struct btrfs_trans_handle *trans, struct
11 btrfs_root *extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -040012static int del_pending_extents(struct btrfs_trans_handle *trans, struct
13 btrfs_root *extent_root);
Chris Masonfec577f2007-02-26 10:40:21 -050014
Chris Masonbe744172007-05-06 10:15:01 -040015static struct btrfs_block_group_cache *lookup_block_group(struct
16 btrfs_fs_info *info,
17 u64 blocknr)
18{
19 struct btrfs_block_group_cache *block_group;
20 int ret;
21
22 ret = radix_tree_gang_lookup(&info->block_group_radix,
23 (void **)&block_group,
24 blocknr, 1);
25 if (ret) {
Chris Mason3e1ad542007-05-07 20:03:49 -040026 if (block_group->key.objectid <= blocknr && blocknr <=
Chris Masonbe744172007-05-06 10:15:01 -040027 block_group->key.objectid + block_group->key.offset)
28 return block_group;
29 }
30 ret = radix_tree_gang_lookup(&info->block_group_data_radix,
31 (void **)&block_group,
32 blocknr, 1);
33 if (ret) {
Chris Mason3e1ad542007-05-07 20:03:49 -040034 if (block_group->key.objectid <= blocknr && blocknr <=
Chris Masonbe744172007-05-06 10:15:01 -040035 block_group->key.objectid + block_group->key.offset)
36 return block_group;
37 }
Chris Mason3e1ad542007-05-07 20:03:49 -040038 WARN_ON(1);
39 printk("lookup_block_group fails for blocknr %Lu\n", blocknr);
40 printk("last ret was %d\n", ret);
41 if (ret) {
42 printk("last block group was %Lu %Lu\n", block_group->key.objectid, block_group->key.offset);
43 }
Chris Masonbe744172007-05-06 10:15:01 -040044 return NULL;
45}
46
Chris Mason31f3c992007-04-30 15:25:45 -040047struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
48 struct btrfs_block_group_cache
Chris Masonbe744172007-05-06 10:15:01 -040049 *hint, u64 search_start,
50 int data)
Chris Masoncd1bc462007-04-27 10:08:34 -040051{
52 struct btrfs_block_group_cache *cache[8];
Chris Mason31f3c992007-04-30 15:25:45 -040053 struct btrfs_block_group_cache *found_group = NULL;
Chris Masoncd1bc462007-04-27 10:08:34 -040054 struct btrfs_fs_info *info = root->fs_info;
Chris Masonbe744172007-05-06 10:15:01 -040055 struct radix_tree_root *radix;
Chris Masoncd1bc462007-04-27 10:08:34 -040056 u64 used;
Chris Mason31f3c992007-04-30 15:25:45 -040057 u64 last = 0;
58 u64 hint_last;
Chris Masoncd1bc462007-04-27 10:08:34 -040059 int i;
60 int ret;
Chris Mason31f3c992007-04-30 15:25:45 -040061 int full_search = 0;
Chris Masonbe744172007-05-06 10:15:01 -040062
63 if (data)
64 radix = &info->block_group_data_radix;
65 else
66 radix = &info->block_group_radix;
67
68 if (search_start) {
69 struct btrfs_block_group_cache *shint;
70 shint = lookup_block_group(info, search_start);
71 if (shint->data == data) {
72 used = btrfs_block_group_used(&shint->item);
73 if (used + shint->pinned <
74 (shint->key.offset * 8) / 10) {
75 return shint;
76 }
77 }
78 }
79 if (hint && hint->data == data) {
Chris Mason31f3c992007-04-30 15:25:45 -040080 used = btrfs_block_group_used(&hint->item);
Chris Masonbe744172007-05-06 10:15:01 -040081 if (used + hint->pinned < (hint->key.offset * 8) / 10) {
Chris Mason31f3c992007-04-30 15:25:45 -040082 return hint;
83 }
Chris Masonbe744172007-05-06 10:15:01 -040084 if (used >= (hint->key.offset * 8) / 10) {
85 radix_tree_tag_clear(radix,
86 hint->key.objectid +
87 hint->key.offset - 1,
88 BTRFS_BLOCK_GROUP_AVAIL);
89 }
90 last = hint->key.offset * 2;
91 if (hint->key.objectid >= last)
92 last = max(search_start, hint->key.objectid - last);
93 else
94 last = hint->key.objectid + hint->key.offset;
Chris Mason31f3c992007-04-30 15:25:45 -040095 hint_last = last;
96 } else {
Chris Masonbe744172007-05-06 10:15:01 -040097 hint_last = search_start;
98 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -040099 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400100 while(1) {
Chris Masonbe744172007-05-06 10:15:01 -0400101 ret = radix_tree_gang_lookup_tag(radix, (void **)cache,
Chris Masoncd1bc462007-04-27 10:08:34 -0400102 last, ARRAY_SIZE(cache),
Chris Mason31f3c992007-04-30 15:25:45 -0400103 BTRFS_BLOCK_GROUP_AVAIL);
Chris Masoncd1bc462007-04-27 10:08:34 -0400104 if (!ret)
105 break;
106 for (i = 0; i < ret; i++) {
Chris Masonbe08c1b2007-05-03 09:06:49 -0400107 last = cache[i]->key.objectid +
108 cache[i]->key.offset;
Chris Masoncd1bc462007-04-27 10:08:34 -0400109 used = btrfs_block_group_used(&cache[i]->item);
Chris Masonbe744172007-05-06 10:15:01 -0400110 if (used + cache[i]->pinned <
111 (cache[i]->key.offset * 8) / 10) {
Chris Mason31f3c992007-04-30 15:25:45 -0400112 found_group = cache[i];
113 goto found;
Chris Masoncd1bc462007-04-27 10:08:34 -0400114 }
Chris Masonbe744172007-05-06 10:15:01 -0400115 if (used >= (cache[i]->key.offset * 8) / 10) {
116 radix_tree_tag_clear(radix,
117 cache[i]->key.objectid +
118 cache[i]->key.offset - 1,
119 BTRFS_BLOCK_GROUP_AVAIL);
120 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400121 }
122 }
Chris Mason31f3c992007-04-30 15:25:45 -0400123 last = hint_last;
124again:
Chris Masoncd1bc462007-04-27 10:08:34 -0400125 while(1) {
Chris Masonbe744172007-05-06 10:15:01 -0400126 ret = radix_tree_gang_lookup(radix, (void **)cache,
127 last, ARRAY_SIZE(cache));
Chris Masoncd1bc462007-04-27 10:08:34 -0400128 if (!ret)
129 break;
130 for (i = 0; i < ret; i++) {
Chris Masonbe08c1b2007-05-03 09:06:49 -0400131 last = cache[i]->key.objectid +
132 cache[i]->key.offset;
Chris Masoncd1bc462007-04-27 10:08:34 -0400133 used = btrfs_block_group_used(&cache[i]->item);
Chris Masonbe744172007-05-06 10:15:01 -0400134 if (used + cache[i]->pinned < cache[i]->key.offset) {
Chris Mason31f3c992007-04-30 15:25:45 -0400135 found_group = cache[i];
136 goto found;
Chris Masoncd1bc462007-04-27 10:08:34 -0400137 }
Chris Masonbe744172007-05-06 10:15:01 -0400138 if (used >= cache[i]->key.offset) {
139 radix_tree_tag_clear(radix,
140 cache[i]->key.objectid +
141 cache[i]->key.offset - 1,
142 BTRFS_BLOCK_GROUP_AVAIL);
143 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400144 }
145 }
Chris Mason31f3c992007-04-30 15:25:45 -0400146 if (!full_search) {
Chris Masonbe744172007-05-06 10:15:01 -0400147 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -0400148 full_search = 1;
149 goto again;
150 }
Chris Mason31f3c992007-04-30 15:25:45 -0400151 if (!found_group) {
Chris Masonbe744172007-05-06 10:15:01 -0400152 ret = radix_tree_gang_lookup(radix,
Chris Mason31f3c992007-04-30 15:25:45 -0400153 (void **)&found_group, 0, 1);
154 BUG_ON(ret != 1);
155 }
Chris Masonbe744172007-05-06 10:15:01 -0400156found:
Chris Mason31f3c992007-04-30 15:25:45 -0400157 return found_group;
Chris Masoncd1bc462007-04-27 10:08:34 -0400158}
159
Chris Masonb18c6682007-04-17 13:26:50 -0400160int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
161 struct btrfs_root *root,
162 u64 blocknr, u64 num_blocks)
Chris Mason02217ed2007-03-02 16:08:05 -0500163{
Chris Mason5caf2a02007-04-02 11:20:42 -0400164 struct btrfs_path *path;
Chris Mason02217ed2007-03-02 16:08:05 -0500165 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400166 struct btrfs_key key;
Chris Mason234b63a2007-03-13 10:46:10 -0400167 struct btrfs_leaf *l;
168 struct btrfs_extent_item *item;
Chris Masone2fa7222007-03-12 16:22:34 -0400169 struct btrfs_key ins;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400170 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500171
Chris Mason9f5fae22007-03-20 14:38:32 -0400172 find_free_extent(trans, root->fs_info->extent_root, 0, 0, (u64)-1,
Chris Masonbe08c1b2007-05-03 09:06:49 -0400173 &ins, 0);
Chris Mason5caf2a02007-04-02 11:20:42 -0400174 path = btrfs_alloc_path();
175 BUG_ON(!path);
176 btrfs_init_path(path);
Chris Mason02217ed2007-03-02 16:08:05 -0500177 key.objectid = blocknr;
178 key.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -0400179 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Mason6407bf62007-03-27 06:33:00 -0400180 key.offset = num_blocks;
Chris Mason5caf2a02007-04-02 11:20:42 -0400181 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -0400182 0, 1);
Chris Masona429e512007-04-18 16:15:28 -0400183 if (ret != 0) {
184printk("can't find block %Lu %Lu\n", blocknr, num_blocks);
Chris Masona28ec192007-03-06 20:08:01 -0500185 BUG();
Chris Masona429e512007-04-18 16:15:28 -0400186 }
Chris Mason02217ed2007-03-02 16:08:05 -0500187 BUG_ON(ret != 0);
Chris Mason5caf2a02007-04-02 11:20:42 -0400188 l = btrfs_buffer_leaf(path->nodes[0]);
189 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400190 refs = btrfs_extent_refs(item);
191 btrfs_set_extent_refs(item, refs + 1);
Chris Mason5caf2a02007-04-02 11:20:42 -0400192 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masona28ec192007-03-06 20:08:01 -0500193
Chris Mason5caf2a02007-04-02 11:20:42 -0400194 btrfs_release_path(root->fs_info->extent_root, path);
195 btrfs_free_path(path);
Chris Mason9f5fae22007-03-20 14:38:32 -0400196 finish_current_insert(trans, root->fs_info->extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -0400197 del_pending_extents(trans, root->fs_info->extent_root);
Chris Mason02217ed2007-03-02 16:08:05 -0500198 return 0;
199}
200
Chris Masonb18c6682007-04-17 13:26:50 -0400201static int lookup_extent_ref(struct btrfs_trans_handle *trans,
202 struct btrfs_root *root, u64 blocknr,
203 u64 num_blocks, u32 *refs)
Chris Masona28ec192007-03-06 20:08:01 -0500204{
Chris Mason5caf2a02007-04-02 11:20:42 -0400205 struct btrfs_path *path;
Chris Masona28ec192007-03-06 20:08:01 -0500206 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400207 struct btrfs_key key;
Chris Mason234b63a2007-03-13 10:46:10 -0400208 struct btrfs_leaf *l;
209 struct btrfs_extent_item *item;
Chris Mason5caf2a02007-04-02 11:20:42 -0400210
211 path = btrfs_alloc_path();
212 btrfs_init_path(path);
Chris Masona28ec192007-03-06 20:08:01 -0500213 key.objectid = blocknr;
Chris Mason6407bf62007-03-27 06:33:00 -0400214 key.offset = num_blocks;
Chris Mason62e27492007-03-15 12:56:47 -0400215 key.flags = 0;
216 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Mason5caf2a02007-04-02 11:20:42 -0400217 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -0400218 0, 0);
Chris Masona28ec192007-03-06 20:08:01 -0500219 if (ret != 0)
220 BUG();
Chris Mason5caf2a02007-04-02 11:20:42 -0400221 l = btrfs_buffer_leaf(path->nodes[0]);
222 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400223 *refs = btrfs_extent_refs(item);
Chris Mason5caf2a02007-04-02 11:20:42 -0400224 btrfs_release_path(root->fs_info->extent_root, path);
225 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -0500226 return 0;
227}
228
Chris Masonc5739bb2007-04-10 09:27:04 -0400229int btrfs_inc_root_ref(struct btrfs_trans_handle *trans,
230 struct btrfs_root *root)
231{
Chris Masonb18c6682007-04-17 13:26:50 -0400232 return btrfs_inc_extent_ref(trans, root, bh_blocknr(root->node), 1);
Chris Masonc5739bb2007-04-10 09:27:04 -0400233}
234
Chris Masone089f052007-03-16 16:20:31 -0400235int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Masone20d96d2007-03-22 12:13:20 -0400236 struct buffer_head *buf)
Chris Mason02217ed2007-03-02 16:08:05 -0500237{
238 u64 blocknr;
Chris Masone20d96d2007-03-22 12:13:20 -0400239 struct btrfs_node *buf_node;
Chris Mason6407bf62007-03-27 06:33:00 -0400240 struct btrfs_leaf *buf_leaf;
241 struct btrfs_disk_key *key;
242 struct btrfs_file_extent_item *fi;
Chris Mason02217ed2007-03-02 16:08:05 -0500243 int i;
Chris Mason6407bf62007-03-27 06:33:00 -0400244 int leaf;
245 int ret;
Chris Masona28ec192007-03-06 20:08:01 -0500246
Chris Mason3768f362007-03-13 16:47:54 -0400247 if (!root->ref_cows)
Chris Masona28ec192007-03-06 20:08:01 -0500248 return 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400249 buf_node = btrfs_buffer_node(buf);
Chris Mason6407bf62007-03-27 06:33:00 -0400250 leaf = btrfs_is_leaf(buf_node);
251 buf_leaf = btrfs_buffer_leaf(buf);
Chris Masone20d96d2007-03-22 12:13:20 -0400252 for (i = 0; i < btrfs_header_nritems(&buf_node->header); i++) {
Chris Mason6407bf62007-03-27 06:33:00 -0400253 if (leaf) {
254 key = &buf_leaf->items[i].key;
255 if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY)
256 continue;
257 fi = btrfs_item_ptr(buf_leaf, i,
258 struct btrfs_file_extent_item);
Chris Mason236454d2007-04-19 13:37:44 -0400259 if (btrfs_file_extent_type(fi) ==
260 BTRFS_FILE_EXTENT_INLINE)
261 continue;
Chris Masonb18c6682007-04-17 13:26:50 -0400262 ret = btrfs_inc_extent_ref(trans, root,
Chris Mason6407bf62007-03-27 06:33:00 -0400263 btrfs_file_extent_disk_blocknr(fi),
264 btrfs_file_extent_disk_num_blocks(fi));
265 BUG_ON(ret);
266 } else {
267 blocknr = btrfs_node_blockptr(buf_node, i);
Chris Masonb18c6682007-04-17 13:26:50 -0400268 ret = btrfs_inc_extent_ref(trans, root, blocknr, 1);
Chris Mason6407bf62007-03-27 06:33:00 -0400269 BUG_ON(ret);
270 }
Chris Mason02217ed2007-03-02 16:08:05 -0500271 }
272 return 0;
273}
274
Chris Mason9078a3e2007-04-26 16:46:15 -0400275static int write_one_cache_group(struct btrfs_trans_handle *trans,
276 struct btrfs_root *root,
277 struct btrfs_path *path,
278 struct btrfs_block_group_cache *cache)
279{
280 int ret;
281 int pending_ret;
282 struct btrfs_root *extent_root = root->fs_info->extent_root;
283 struct btrfs_block_group_item *bi;
284 struct btrfs_key ins;
285
Chris Masonbe08c1b2007-05-03 09:06:49 -0400286 find_free_extent(trans, extent_root, 0, 0, (u64)-1, &ins, 0);
Chris Mason9078a3e2007-04-26 16:46:15 -0400287 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
288 BUG_ON(ret);
289 bi = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
290 struct btrfs_block_group_item);
291 memcpy(bi, &cache->item, sizeof(*bi));
292 mark_buffer_dirty(path->nodes[0]);
293 btrfs_release_path(extent_root, path);
294
295 finish_current_insert(trans, extent_root);
296 pending_ret = del_pending_extents(trans, extent_root);
297 if (ret)
298 return ret;
299 if (pending_ret)
300 return pending_ret;
Chris Masonbe744172007-05-06 10:15:01 -0400301 if (cache->data)
302 cache->last_alloc = cache->first_free;
Chris Mason9078a3e2007-04-26 16:46:15 -0400303 return 0;
304
305}
306
Chris Masonbe744172007-05-06 10:15:01 -0400307static int write_dirty_block_radix(struct btrfs_trans_handle *trans,
308 struct btrfs_root *root,
309 struct radix_tree_root *radix)
Chris Mason9078a3e2007-04-26 16:46:15 -0400310{
311 struct btrfs_block_group_cache *cache[8];
312 int ret;
313 int err = 0;
314 int werr = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -0400315 int i;
316 struct btrfs_path *path;
317
318 path = btrfs_alloc_path();
319 if (!path)
320 return -ENOMEM;
321
322 while(1) {
323 ret = radix_tree_gang_lookup_tag(radix, (void **)cache,
324 0, ARRAY_SIZE(cache),
325 BTRFS_BLOCK_GROUP_DIRTY);
326 if (!ret)
327 break;
328 for (i = 0; i < ret; i++) {
329 radix_tree_tag_clear(radix, cache[i]->key.objectid +
330 cache[i]->key.offset - 1,
331 BTRFS_BLOCK_GROUP_DIRTY);
332 err = write_one_cache_group(trans, root,
333 path, cache[i]);
334 if (err)
335 werr = err;
336 }
337 }
338 btrfs_free_path(path);
339 return werr;
340}
341
Chris Masonbe744172007-05-06 10:15:01 -0400342int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
343 struct btrfs_root *root)
344{
345 int ret;
346 int ret2;
347 ret = write_dirty_block_radix(trans, root,
348 &root->fs_info->block_group_radix);
349 ret2 = write_dirty_block_radix(trans, root,
350 &root->fs_info->block_group_data_radix);
351 if (ret)
352 return ret;
353 if (ret2)
354 return ret2;
355 return 0;
356}
357
Chris Mason9078a3e2007-04-26 16:46:15 -0400358static int update_block_group(struct btrfs_trans_handle *trans,
359 struct btrfs_root *root,
360 u64 blocknr, u64 num, int alloc)
361{
362 struct btrfs_block_group_cache *cache;
363 struct btrfs_fs_info *info = root->fs_info;
364 u64 total = num;
365 u64 old_val;
366 u64 block_in_group;
Chris Mason3e1ad542007-05-07 20:03:49 -0400367
Chris Mason9078a3e2007-04-26 16:46:15 -0400368 while(total) {
Chris Mason3e1ad542007-05-07 20:03:49 -0400369 cache = lookup_block_group(info, blocknr);
370 if (!cache) {
Chris Masoncd1bc462007-04-27 10:08:34 -0400371 printk(KERN_CRIT "blocknr %Lu lookup failed\n",
372 blocknr);
Chris Mason9078a3e2007-04-26 16:46:15 -0400373 return -1;
Chris Masoncd1bc462007-04-27 10:08:34 -0400374 }
Chris Mason9078a3e2007-04-26 16:46:15 -0400375 block_in_group = blocknr - cache->key.objectid;
376 WARN_ON(block_in_group > cache->key.offset);
Chris Mason3e1ad542007-05-07 20:03:49 -0400377 radix_tree_tag_set(cache->radix, cache->key.objectid +
Chris Masonbe744172007-05-06 10:15:01 -0400378 cache->key.offset - 1,
Chris Mason9078a3e2007-04-26 16:46:15 -0400379 BTRFS_BLOCK_GROUP_DIRTY);
380
381 old_val = btrfs_block_group_used(&cache->item);
382 num = min(total, cache->key.offset - block_in_group);
383 total -= num;
384 blocknr += num;
Chris Masoncd1bc462007-04-27 10:08:34 -0400385 if (alloc) {
Chris Mason9078a3e2007-04-26 16:46:15 -0400386 old_val += num;
Chris Masoncd1bc462007-04-27 10:08:34 -0400387 if (blocknr > cache->last_alloc)
388 cache->last_alloc = blocknr;
389 } else {
Chris Mason9078a3e2007-04-26 16:46:15 -0400390 old_val -= num;
Chris Masoncd1bc462007-04-27 10:08:34 -0400391 if (blocknr < cache->first_free)
392 cache->first_free = blocknr;
393 }
Chris Mason9078a3e2007-04-26 16:46:15 -0400394 btrfs_set_block_group_used(&cache->item, old_val);
395 }
396 return 0;
397}
398
Chris Masonbe08c1b2007-05-03 09:06:49 -0400399static int try_remove_page(struct address_space *mapping, unsigned long index)
400{
401 int ret;
402 ret = invalidate_mapping_pages(mapping, index, index);
403 return ret;
404}
405
Chris Masone089f052007-03-16 16:20:31 -0400406int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, struct
407 btrfs_root *root)
Chris Masona28ec192007-03-06 20:08:01 -0500408{
Chris Mason8ef97622007-03-26 10:15:30 -0400409 unsigned long gang[8];
Chris Masonbe08c1b2007-05-03 09:06:49 -0400410 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Masonbe744172007-05-06 10:15:01 -0400411 struct btrfs_block_group_cache *block_group;
Chris Mason88fd1462007-03-16 08:56:18 -0400412 u64 first = 0;
Chris Masona28ec192007-03-06 20:08:01 -0500413 int ret;
414 int i;
Chris Mason8ef97622007-03-26 10:15:30 -0400415 struct radix_tree_root *pinned_radix = &root->fs_info->pinned_radix;
Chris Masona28ec192007-03-06 20:08:01 -0500416
417 while(1) {
Chris Mason8ef97622007-03-26 10:15:30 -0400418 ret = find_first_radix_bit(pinned_radix, gang,
419 ARRAY_SIZE(gang));
Chris Masona28ec192007-03-06 20:08:01 -0500420 if (!ret)
421 break;
Chris Mason88fd1462007-03-16 08:56:18 -0400422 if (!first)
Chris Mason8ef97622007-03-26 10:15:30 -0400423 first = gang[0];
Chris Mason0579da42007-03-07 16:15:30 -0500424 for (i = 0; i < ret; i++) {
Chris Mason8ef97622007-03-26 10:15:30 -0400425 clear_radix_bit(pinned_radix, gang[i]);
Chris Masonbe744172007-05-06 10:15:01 -0400426 block_group = lookup_block_group(root->fs_info,
427 gang[i]);
428 if (block_group) {
429 WARN_ON(block_group->pinned == 0);
430 block_group->pinned--;
431 if (gang[i] < block_group->last_alloc)
432 block_group->last_alloc = gang[i];
433 }
Chris Masonbe08c1b2007-05-03 09:06:49 -0400434 try_remove_page(btree_inode->i_mapping,
435 gang[i] << (PAGE_CACHE_SHIFT -
436 btree_inode->i_blkbits));
Chris Mason0579da42007-03-07 16:15:30 -0500437 }
Chris Masona28ec192007-03-06 20:08:01 -0500438 }
439 return 0;
440}
441
Chris Masone089f052007-03-16 16:20:31 -0400442static int finish_current_insert(struct btrfs_trans_handle *trans, struct
443 btrfs_root *extent_root)
Chris Mason037e6392007-03-07 11:50:24 -0500444{
Chris Masone2fa7222007-03-12 16:22:34 -0400445 struct btrfs_key ins;
Chris Mason234b63a2007-03-13 10:46:10 -0400446 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -0500447 int i;
448 int ret;
Chris Mason1261ec42007-03-20 20:35:03 -0400449 u64 super_blocks_used;
450 struct btrfs_fs_info *info = extent_root->fs_info;
Chris Mason037e6392007-03-07 11:50:24 -0500451
Chris Masoncf27e1e2007-03-13 09:49:06 -0400452 btrfs_set_extent_refs(&extent_item, 1);
Chris Mason037e6392007-03-07 11:50:24 -0500453 ins.offset = 1;
454 ins.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -0400455 btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
Chris Mason5d0c3e62007-04-23 17:01:05 -0400456 btrfs_set_extent_owner(&extent_item, extent_root->root_key.objectid);
Chris Mason037e6392007-03-07 11:50:24 -0500457
Chris Masonf2458e12007-04-25 15:52:25 -0400458 for (i = 0; i < extent_root->fs_info->extent_tree_insert_nr; i++) {
459 ins.objectid = extent_root->fs_info->extent_tree_insert[i];
Chris Mason1261ec42007-03-20 20:35:03 -0400460 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
461 btrfs_set_super_blocks_used(info->disk_super,
462 super_blocks_used + 1);
Chris Masone089f052007-03-16 16:20:31 -0400463 ret = btrfs_insert_item(trans, extent_root, &ins, &extent_item,
464 sizeof(extent_item));
Chris Mason037e6392007-03-07 11:50:24 -0500465 BUG_ON(ret);
466 }
Chris Masonf2458e12007-04-25 15:52:25 -0400467 extent_root->fs_info->extent_tree_insert_nr = 0;
468 extent_root->fs_info->extent_tree_prealloc_nr = 0;
Chris Mason037e6392007-03-07 11:50:24 -0500469 return 0;
470}
471
Chris Mason8ef97622007-03-26 10:15:30 -0400472static int pin_down_block(struct btrfs_root *root, u64 blocknr, int pending)
Chris Masone20d96d2007-03-22 12:13:20 -0400473{
474 int err;
Chris Mason78fae272007-03-25 11:35:08 -0400475 struct btrfs_header *header;
Chris Mason8ef97622007-03-26 10:15:30 -0400476 struct buffer_head *bh;
Chris Mason78fae272007-03-25 11:35:08 -0400477
Chris Masonf4b9aa82007-03-27 11:05:53 -0400478 if (!pending) {
Chris Masond98237b2007-03-28 13:57:48 -0400479 bh = btrfs_find_tree_block(root, blocknr);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400480 if (bh) {
481 if (buffer_uptodate(bh)) {
482 u64 transid =
483 root->fs_info->running_transaction->transid;
484 header = btrfs_buffer_header(bh);
485 if (btrfs_header_generation(header) ==
486 transid) {
487 btrfs_block_release(root, bh);
488 return 0;
489 }
Chris Masonf4b9aa82007-03-27 11:05:53 -0400490 }
Chris Masond6025572007-03-30 14:27:56 -0400491 btrfs_block_release(root, bh);
Chris Mason8ef97622007-03-26 10:15:30 -0400492 }
Chris Mason8ef97622007-03-26 10:15:30 -0400493 err = set_radix_bit(&root->fs_info->pinned_radix, blocknr);
Chris Masonbe744172007-05-06 10:15:01 -0400494 if (!err) {
495 struct btrfs_block_group_cache *cache;
496 cache = lookup_block_group(root->fs_info, blocknr);
497 if (cache)
498 cache->pinned++;
499 }
Chris Masonf4b9aa82007-03-27 11:05:53 -0400500 } else {
501 err = set_radix_bit(&root->fs_info->pending_del_radix, blocknr);
502 }
Chris Masonbe744172007-05-06 10:15:01 -0400503 BUG_ON(err < 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400504 return 0;
505}
506
Chris Masona28ec192007-03-06 20:08:01 -0500507/*
508 * remove an extent from the root, returns 0 on success
509 */
Chris Masone089f052007-03-16 16:20:31 -0400510static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason78fae272007-03-25 11:35:08 -0400511 *root, u64 blocknr, u64 num_blocks, int pin)
Chris Masona28ec192007-03-06 20:08:01 -0500512{
Chris Mason5caf2a02007-04-02 11:20:42 -0400513 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -0400514 struct btrfs_key key;
Chris Mason1261ec42007-03-20 20:35:03 -0400515 struct btrfs_fs_info *info = root->fs_info;
516 struct btrfs_root *extent_root = info->extent_root;
Chris Masona28ec192007-03-06 20:08:01 -0500517 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400518 struct btrfs_extent_item *ei;
Chris Masone2fa7222007-03-12 16:22:34 -0400519 struct btrfs_key ins;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400520 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500521
Chris Masona28ec192007-03-06 20:08:01 -0500522 key.objectid = blocknr;
523 key.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -0400524 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masona28ec192007-03-06 20:08:01 -0500525 key.offset = num_blocks;
526
Chris Masonbe08c1b2007-05-03 09:06:49 -0400527 find_free_extent(trans, root, 0, 0, (u64)-1, &ins, 0);
Chris Mason5caf2a02007-04-02 11:20:42 -0400528 path = btrfs_alloc_path();
529 BUG_ON(!path);
530 btrfs_init_path(path);
Chris Mason5f26f772007-04-05 10:38:44 -0400531
Chris Mason5caf2a02007-04-02 11:20:42 -0400532 ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
Chris Masona28ec192007-03-06 20:08:01 -0500533 if (ret) {
Chris Mason2e635a22007-03-21 11:12:56 -0400534 printk("failed to find %Lu\n", key.objectid);
Chris Mason234b63a2007-03-13 10:46:10 -0400535 btrfs_print_tree(extent_root, extent_root->node);
Chris Mason2e635a22007-03-21 11:12:56 -0400536 printk("failed to find %Lu\n", key.objectid);
Chris Masona28ec192007-03-06 20:08:01 -0500537 BUG();
538 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400539 ei = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
Chris Mason123abc82007-03-14 14:14:43 -0400540 struct btrfs_extent_item);
Chris Masona28ec192007-03-06 20:08:01 -0500541 BUG_ON(ei->refs == 0);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400542 refs = btrfs_extent_refs(ei) - 1;
543 btrfs_set_extent_refs(ei, refs);
Chris Mason5caf2a02007-04-02 11:20:42 -0400544 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400545 if (refs == 0) {
Chris Mason1261ec42007-03-20 20:35:03 -0400546 u64 super_blocks_used;
Chris Mason78fae272007-03-25 11:35:08 -0400547
548 if (pin) {
Chris Mason8ef97622007-03-26 10:15:30 -0400549 ret = pin_down_block(root, blocknr, 0);
Chris Mason78fae272007-03-25 11:35:08 -0400550 BUG_ON(ret);
551 }
552
Chris Mason1261ec42007-03-20 20:35:03 -0400553 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
554 btrfs_set_super_blocks_used(info->disk_super,
555 super_blocks_used - num_blocks);
Chris Mason5caf2a02007-04-02 11:20:42 -0400556 ret = btrfs_del_item(trans, extent_root, path);
Chris Masona28ec192007-03-06 20:08:01 -0500557 if (ret)
558 BUG();
Chris Mason9078a3e2007-04-26 16:46:15 -0400559 ret = update_block_group(trans, root, blocknr, num_blocks, 0);
560 BUG_ON(ret);
Chris Masona28ec192007-03-06 20:08:01 -0500561 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400562 btrfs_release_path(extent_root, path);
563 btrfs_free_path(path);
Chris Masone089f052007-03-16 16:20:31 -0400564 finish_current_insert(trans, extent_root);
Chris Masona28ec192007-03-06 20:08:01 -0500565 return ret;
566}
567
568/*
Chris Masonfec577f2007-02-26 10:40:21 -0500569 * find all the blocks marked as pending in the radix tree and remove
570 * them from the extent map
571 */
Chris Masone089f052007-03-16 16:20:31 -0400572static int del_pending_extents(struct btrfs_trans_handle *trans, struct
573 btrfs_root *extent_root)
Chris Masonfec577f2007-02-26 10:40:21 -0500574{
575 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -0400576 int wret;
577 int err = 0;
Chris Mason8ef97622007-03-26 10:15:30 -0400578 unsigned long gang[4];
Chris Masonfec577f2007-02-26 10:40:21 -0500579 int i;
Chris Mason8ef97622007-03-26 10:15:30 -0400580 struct radix_tree_root *pending_radix;
581 struct radix_tree_root *pinned_radix;
Chris Masonbe744172007-05-06 10:15:01 -0400582 struct btrfs_block_group_cache *cache;
Chris Mason8ef97622007-03-26 10:15:30 -0400583
584 pending_radix = &extent_root->fs_info->pending_del_radix;
585 pinned_radix = &extent_root->fs_info->pinned_radix;
Chris Masonfec577f2007-02-26 10:40:21 -0500586
587 while(1) {
Chris Mason8ef97622007-03-26 10:15:30 -0400588 ret = find_first_radix_bit(pending_radix, gang,
589 ARRAY_SIZE(gang));
Chris Masonfec577f2007-02-26 10:40:21 -0500590 if (!ret)
591 break;
592 for (i = 0; i < ret; i++) {
Chris Mason8ef97622007-03-26 10:15:30 -0400593 wret = set_radix_bit(pinned_radix, gang[i]);
Chris Masonbe744172007-05-06 10:15:01 -0400594 if (wret == 0) {
595 cache = lookup_block_group(extent_root->fs_info,
596 gang[i]);
597 if (cache)
598 cache->pinned++;
599 }
600 if (wret < 0) {
601 printk(KERN_CRIT "set_radix_bit, err %d\n",
602 wret);
603 BUG_ON(wret < 0);
604 }
Chris Mason8ef97622007-03-26 10:15:30 -0400605 wret = clear_radix_bit(pending_radix, gang[i]);
606 BUG_ON(wret);
Chris Masond5719762007-03-23 10:01:08 -0400607 wret = __free_extent(trans, extent_root,
Chris Mason8ef97622007-03-26 10:15:30 -0400608 gang[i], 1, 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400609 if (wret)
610 err = wret;
Chris Masonfec577f2007-02-26 10:40:21 -0500611 }
612 }
Chris Masone20d96d2007-03-22 12:13:20 -0400613 return err;
Chris Masonfec577f2007-02-26 10:40:21 -0500614}
615
616/*
617 * remove an extent from the root, returns 0 on success
618 */
Chris Masone089f052007-03-16 16:20:31 -0400619int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
620 *root, u64 blocknr, u64 num_blocks, int pin)
Chris Masonfec577f2007-02-26 10:40:21 -0500621{
Chris Mason9f5fae22007-03-20 14:38:32 -0400622 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Masonfec577f2007-02-26 10:40:21 -0500623 int pending_ret;
624 int ret;
Chris Masona28ec192007-03-06 20:08:01 -0500625
626 if (root == extent_root) {
Chris Mason8ef97622007-03-26 10:15:30 -0400627 pin_down_block(root, blocknr, 1);
Chris Masona28ec192007-03-06 20:08:01 -0500628 return 0;
629 }
Chris Mason78fae272007-03-25 11:35:08 -0400630 ret = __free_extent(trans, root, blocknr, num_blocks, pin);
Chris Masone20d96d2007-03-22 12:13:20 -0400631 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
Chris Masonfec577f2007-02-26 10:40:21 -0500632 return ret ? ret : pending_ret;
633}
634
635/*
636 * walks the btree of allocated extents and find a hole of a given size.
637 * The key ins is changed to record the hole:
638 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -0400639 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -0500640 * ins->offset == number of blocks
641 * Any available blocks before search_start are skipped.
642 */
Chris Masone089f052007-03-16 16:20:31 -0400643static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
644 *orig_root, u64 num_blocks, u64 search_start, u64
Chris Masonbe08c1b2007-05-03 09:06:49 -0400645 search_end, struct btrfs_key *ins, int data)
Chris Masonfec577f2007-02-26 10:40:21 -0500646{
Chris Mason5caf2a02007-04-02 11:20:42 -0400647 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -0400648 struct btrfs_key key;
Chris Masonfec577f2007-02-26 10:40:21 -0500649 int ret;
650 u64 hole_size = 0;
651 int slot = 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400652 u64 last_block = 0;
Chris Mason037e6392007-03-07 11:50:24 -0500653 u64 test_block;
Chris Masonbe744172007-05-06 10:15:01 -0400654 u64 orig_search_start = search_start;
Chris Masonfec577f2007-02-26 10:40:21 -0500655 int start_found;
Chris Mason234b63a2007-03-13 10:46:10 -0400656 struct btrfs_leaf *l;
Chris Mason9f5fae22007-03-20 14:38:32 -0400657 struct btrfs_root * root = orig_root->fs_info->extent_root;
Chris Masonf2458e12007-04-25 15:52:25 -0400658 struct btrfs_fs_info *info = root->fs_info;
Chris Mason0579da42007-03-07 16:15:30 -0500659 int total_needed = num_blocks;
Chris Masonf2458e12007-04-25 15:52:25 -0400660 int total_found = 0;
661 int fill_prealloc = 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400662 int level;
Chris Masonbe08c1b2007-05-03 09:06:49 -0400663 struct btrfs_block_group_cache *block_group;
Chris Masonbe744172007-05-06 10:15:01 -0400664 int full_scan = 0;
Chris Masonfec577f2007-02-26 10:40:21 -0500665
Chris Masonb1a4d962007-04-04 15:27:52 -0400666 path = btrfs_alloc_path();
667 ins->flags = 0;
668 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
669
Chris Masone20d96d2007-03-22 12:13:20 -0400670 level = btrfs_header_level(btrfs_buffer_header(root->node));
Chris Masonf2458e12007-04-25 15:52:25 -0400671 if (num_blocks == 0) {
672 fill_prealloc = 1;
673 num_blocks = 1;
Chris Mason308535a2007-04-28 15:17:08 -0400674 total_needed = (min(level + 1, BTRFS_MAX_LEVEL) + 2) * 3;
Chris Masonf2458e12007-04-25 15:52:25 -0400675 }
Chris Mason3e1ad542007-05-07 20:03:49 -0400676 if (search_end == (u64)-1)
677 search_end = btrfs_super_total_blocks(info->disk_super);
Chris Masonbe744172007-05-06 10:15:01 -0400678 if (search_start) {
679 block_group = lookup_block_group(info, search_start);
680 block_group = btrfs_find_block_group(root, block_group,
681 search_start, data);
682 } else {
683 block_group = btrfs_find_block_group(root,
684 trans->block_group, 0,
685 data);
686 }
687
688check_failed:
Chris Mason3e1ad542007-05-07 20:03:49 -0400689 if (!full_scan && block_group->data != data)
Chris Masonbe744172007-05-06 10:15:01 -0400690 WARN_ON(1);
Chris Masonbe08c1b2007-05-03 09:06:49 -0400691 if (block_group->last_alloc > search_start)
692 search_start = block_group->last_alloc;
Chris Mason5caf2a02007-04-02 11:20:42 -0400693 btrfs_init_path(path);
Chris Masonfec577f2007-02-26 10:40:21 -0500694 ins->objectid = search_start;
695 ins->offset = 0;
Chris Masonfec577f2007-02-26 10:40:21 -0500696 start_found = 0;
Chris Mason5caf2a02007-04-02 11:20:42 -0400697 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
Chris Mason0f70abe2007-02-28 16:46:22 -0500698 if (ret < 0)
699 goto error;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500700
Chris Mason5caf2a02007-04-02 11:20:42 -0400701 if (path->slots[0] > 0)
702 path->slots[0]--;
Chris Mason0579da42007-03-07 16:15:30 -0500703
Chris Masonfec577f2007-02-26 10:40:21 -0500704 while (1) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400705 l = btrfs_buffer_leaf(path->nodes[0]);
706 slot = path->slots[0];
Chris Mason7518a232007-03-12 12:01:18 -0400707 if (slot >= btrfs_header_nritems(&l->header)) {
Chris Masonf2458e12007-04-25 15:52:25 -0400708 if (fill_prealloc) {
709 info->extent_tree_prealloc_nr = 0;
710 total_found = 0;
711 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400712 ret = btrfs_next_leaf(root, path);
Chris Masonfec577f2007-02-26 10:40:21 -0500713 if (ret == 0)
714 continue;
Chris Mason0f70abe2007-02-28 16:46:22 -0500715 if (ret < 0)
716 goto error;
Chris Masonfec577f2007-02-26 10:40:21 -0500717 if (!start_found) {
718 ins->objectid = search_start;
Chris Mason3e1ad542007-05-07 20:03:49 -0400719 ins->offset = search_end - search_start;
Chris Masonfec577f2007-02-26 10:40:21 -0500720 start_found = 1;
721 goto check_pending;
722 }
723 ins->objectid = last_block > search_start ?
724 last_block : search_start;
Chris Mason3e1ad542007-05-07 20:03:49 -0400725 ins->offset = search_end - ins->objectid;
Chris Masonfec577f2007-02-26 10:40:21 -0500726 goto check_pending;
727 }
Chris Masone2fa7222007-03-12 16:22:34 -0400728 btrfs_disk_key_to_cpu(&key, &l->items[slot].key);
Chris Mason9078a3e2007-04-26 16:46:15 -0400729 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY)
730 goto next;
Chris Masone2fa7222007-03-12 16:22:34 -0400731 if (key.objectid >= search_start) {
Chris Masonfec577f2007-02-26 10:40:21 -0500732 if (start_found) {
Chris Mason0579da42007-03-07 16:15:30 -0500733 if (last_block < search_start)
734 last_block = search_start;
Chris Masone2fa7222007-03-12 16:22:34 -0400735 hole_size = key.objectid - last_block;
Chris Mason28b8bb92007-04-27 11:42:05 -0400736 if (hole_size >= num_blocks) {
Chris Masonfec577f2007-02-26 10:40:21 -0500737 ins->objectid = last_block;
Chris Mason037e6392007-03-07 11:50:24 -0500738 ins->offset = hole_size;
Chris Masonfec577f2007-02-26 10:40:21 -0500739 goto check_pending;
740 }
Chris Mason0579da42007-03-07 16:15:30 -0500741 }
Chris Masonfec577f2007-02-26 10:40:21 -0500742 }
Chris Mason0579da42007-03-07 16:15:30 -0500743 start_found = 1;
Chris Masone2fa7222007-03-12 16:22:34 -0400744 last_block = key.objectid + key.offset;
Chris Masonbe744172007-05-06 10:15:01 -0400745 if (last_block >= block_group->key.objectid +
746 block_group->key.offset) {
747 btrfs_release_path(root, path);
748 search_start = block_group->key.objectid +
749 block_group->key.offset * 2;
750 goto new_group;
751 }
Chris Mason9078a3e2007-04-26 16:46:15 -0400752next:
Chris Mason5caf2a02007-04-02 11:20:42 -0400753 path->slots[0]++;
Chris Masonfec577f2007-02-26 10:40:21 -0500754 }
755 // FIXME -ENOSPC
756check_pending:
757 /* we have to make sure we didn't find an extent that has already
758 * been allocated by the map tree or the original allocation
759 */
Chris Mason5caf2a02007-04-02 11:20:42 -0400760 btrfs_release_path(root, path);
Chris Masonfec577f2007-02-26 10:40:21 -0500761 BUG_ON(ins->objectid < search_start);
Chris Mason3e1ad542007-05-07 20:03:49 -0400762 if (ins->objectid + num_blocks >= search_end) {
Chris Masonbe744172007-05-06 10:15:01 -0400763 if (full_scan)
Chris Mason06a2f9f2007-04-28 08:48:10 -0400764 return -ENOSPC;
Chris Masonbe744172007-05-06 10:15:01 -0400765 search_start = orig_search_start;
766 full_scan = 1;
767 goto new_group;
Chris Mason06a2f9f2007-04-28 08:48:10 -0400768 }
Chris Mason037e6392007-03-07 11:50:24 -0500769 for (test_block = ins->objectid;
Chris Masonf2458e12007-04-25 15:52:25 -0400770 test_block < ins->objectid + num_blocks; test_block++) {
771 if (test_radix_bit(&info->pinned_radix, test_block)) {
Chris Mason037e6392007-03-07 11:50:24 -0500772 search_start = test_block + 1;
Chris Masonbe744172007-05-06 10:15:01 -0400773 goto new_group;
Chris Masonfec577f2007-02-26 10:40:21 -0500774 }
775 }
Chris Masonf2458e12007-04-25 15:52:25 -0400776 if (!fill_prealloc && info->extent_tree_insert_nr) {
777 u64 last =
778 info->extent_tree_insert[info->extent_tree_insert_nr - 1];
779 if (ins->objectid + num_blocks >
780 info->extent_tree_insert[0] &&
781 ins->objectid <= last) {
782 search_start = last + 1;
783 WARN_ON(1);
Chris Masonbe744172007-05-06 10:15:01 -0400784 goto new_group;
Chris Masonf2458e12007-04-25 15:52:25 -0400785 }
786 }
787 if (!fill_prealloc && info->extent_tree_prealloc_nr) {
788 u64 first =
789 info->extent_tree_prealloc[info->extent_tree_prealloc_nr - 1];
790 if (ins->objectid + num_blocks > first &&
791 ins->objectid <= info->extent_tree_prealloc[0]) {
792 search_start = info->extent_tree_prealloc[0] + 1;
793 WARN_ON(1);
Chris Masonbe744172007-05-06 10:15:01 -0400794 goto new_group;
Chris Masonf2458e12007-04-25 15:52:25 -0400795 }
796 }
797 if (fill_prealloc) {
798 int nr;
799 test_block = ins->objectid;
800 while(test_block < ins->objectid + ins->offset &&
801 total_found < total_needed) {
802 nr = total_needed - total_found - 1;
803 BUG_ON(nr < 0);
Chris Masoncd1bc462007-04-27 10:08:34 -0400804 info->extent_tree_prealloc[nr] = test_block;
Chris Masonf2458e12007-04-25 15:52:25 -0400805 total_found++;
806 test_block++;
807 }
808 if (total_found < total_needed) {
809 search_start = test_block;
Chris Masonbe744172007-05-06 10:15:01 -0400810 goto new_group;
Chris Masonf2458e12007-04-25 15:52:25 -0400811 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400812 info->extent_tree_prealloc_nr = total_found;
Chris Masonf2458e12007-04-25 15:52:25 -0400813 }
Chris Masonbe744172007-05-06 10:15:01 -0400814 block_group = lookup_block_group(info, ins->objectid);
815 if (block_group) {
Chris Masonbe08c1b2007-05-03 09:06:49 -0400816 block_group->last_alloc = ins->objectid;
817 if (!data)
818 trans->block_group = block_group;
Chris Masoncd1bc462007-04-27 10:08:34 -0400819 }
Chris Mason037e6392007-03-07 11:50:24 -0500820 ins->offset = num_blocks;
Chris Mason5caf2a02007-04-02 11:20:42 -0400821 btrfs_free_path(path);
Chris Masonfec577f2007-02-26 10:40:21 -0500822 return 0;
Chris Masonbe744172007-05-06 10:15:01 -0400823
824new_group:
Chris Mason3e1ad542007-05-07 20:03:49 -0400825 if (search_start + num_blocks >= search_end) {
Chris Masonbe744172007-05-06 10:15:01 -0400826 search_start = orig_search_start;
827 full_scan = 1;
828 }
829 block_group = lookup_block_group(info, search_start);
830 if (!full_scan)
831 block_group = btrfs_find_block_group(root, block_group,
832 search_start, data);
833 goto check_failed;
834
Chris Mason0f70abe2007-02-28 16:46:22 -0500835error:
Chris Mason5caf2a02007-04-02 11:20:42 -0400836 btrfs_release_path(root, path);
837 btrfs_free_path(path);
Chris Mason0f70abe2007-02-28 16:46:22 -0500838 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -0500839}
Chris Masonfec577f2007-02-26 10:40:21 -0500840/*
Chris Masonfec577f2007-02-26 10:40:21 -0500841 * finds a free extent and does all the dirty work required for allocation
842 * returns the key for the extent through ins, and a tree buffer for
843 * the first block of the extent through buf.
844 *
845 * returns 0 if everything worked, non-zero otherwise.
846 */
Chris Mason4d775672007-04-20 20:23:12 -0400847int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
848 struct btrfs_root *root, u64 owner,
Chris Masonc62a1922007-04-24 12:07:39 -0400849 u64 num_blocks, u64 search_start,
Chris Masonbe08c1b2007-05-03 09:06:49 -0400850 u64 search_end, struct btrfs_key *ins, int data)
Chris Masonfec577f2007-02-26 10:40:21 -0500851{
852 int ret;
853 int pending_ret;
Chris Mason1261ec42007-03-20 20:35:03 -0400854 u64 super_blocks_used;
855 struct btrfs_fs_info *info = root->fs_info;
856 struct btrfs_root *extent_root = info->extent_root;
Chris Mason234b63a2007-03-13 10:46:10 -0400857 struct btrfs_extent_item extent_item;
Chris Masonf2458e12007-04-25 15:52:25 -0400858 struct btrfs_key prealloc_key;
Chris Mason037e6392007-03-07 11:50:24 -0500859
Chris Masoncf27e1e2007-03-13 09:49:06 -0400860 btrfs_set_extent_refs(&extent_item, 1);
Chris Mason4d775672007-04-20 20:23:12 -0400861 btrfs_set_extent_owner(&extent_item, owner);
Chris Masonfec577f2007-02-26 10:40:21 -0500862
Chris Mason037e6392007-03-07 11:50:24 -0500863 if (root == extent_root) {
Chris Masonf2458e12007-04-25 15:52:25 -0400864 int nr;
865 BUG_ON(info->extent_tree_prealloc_nr == 0);
Chris Mason037e6392007-03-07 11:50:24 -0500866 BUG_ON(num_blocks != 1);
Chris Mason037e6392007-03-07 11:50:24 -0500867 ins->offset = 1;
Chris Masonf2458e12007-04-25 15:52:25 -0400868 info->extent_tree_prealloc_nr--;
869 nr = info->extent_tree_prealloc_nr;
870 ins->objectid = info->extent_tree_prealloc[nr];
871 info->extent_tree_insert[info->extent_tree_insert_nr++] =
872 ins->objectid;
Chris Mason9078a3e2007-04-26 16:46:15 -0400873 ret = update_block_group(trans, root,
874 ins->objectid, ins->offset, 1);
875 BUG_ON(ret);
Chris Masonfec577f2007-02-26 10:40:21 -0500876 return 0;
877 }
Chris Masonf2458e12007-04-25 15:52:25 -0400878 /* do the real allocation */
Chris Masone089f052007-03-16 16:20:31 -0400879 ret = find_free_extent(trans, root, num_blocks, search_start,
Chris Masonbe08c1b2007-05-03 09:06:49 -0400880 search_end, ins, data);
Chris Mason037e6392007-03-07 11:50:24 -0500881 if (ret)
882 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -0500883
Chris Masonf2458e12007-04-25 15:52:25 -0400884 /* then do prealloc for the extent tree */
Chris Mason3e1ad542007-05-07 20:03:49 -0400885 if (ins->objectid + ins->offset >= search_end)
886 search_end = ins->objectid - 1;
887 else
888 search_start = ins->objectid + ins->offset;
889
890 ret = find_free_extent(trans, root, 0, search_start,
Chris Masonbe08c1b2007-05-03 09:06:49 -0400891 search_end, &prealloc_key, 0);
Chris Masonf2458e12007-04-25 15:52:25 -0400892 if (ret)
893 return ret;
894
Chris Mason1261ec42007-03-20 20:35:03 -0400895 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
896 btrfs_set_super_blocks_used(info->disk_super, super_blocks_used +
897 num_blocks);
Chris Masone089f052007-03-16 16:20:31 -0400898 ret = btrfs_insert_item(trans, extent_root, ins, &extent_item,
899 sizeof(extent_item));
Chris Mason037e6392007-03-07 11:50:24 -0500900
Chris Masone089f052007-03-16 16:20:31 -0400901 finish_current_insert(trans, extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -0400902 pending_ret = del_pending_extents(trans, extent_root);
Chris Mason037e6392007-03-07 11:50:24 -0500903 if (ret)
904 return ret;
905 if (pending_ret)
906 return pending_ret;
Chris Mason9078a3e2007-04-26 16:46:15 -0400907 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1);
Chris Mason037e6392007-03-07 11:50:24 -0500908 return 0;
Chris Masonfec577f2007-02-26 10:40:21 -0500909}
910
911/*
912 * helper function to allocate a block for a given tree
913 * returns the tree buffer or NULL.
914 */
Chris Masone20d96d2007-03-22 12:13:20 -0400915struct buffer_head *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Chris Mason31f3c992007-04-30 15:25:45 -0400916 struct btrfs_root *root, u64 hint)
Chris Masonfec577f2007-02-26 10:40:21 -0500917{
Chris Masone2fa7222007-03-12 16:22:34 -0400918 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -0500919 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -0400920 struct buffer_head *buf;
Chris Masonfec577f2007-02-26 10:40:21 -0500921
Chris Mason4d775672007-04-20 20:23:12 -0400922 ret = btrfs_alloc_extent(trans, root, root->root_key.objectid,
Chris Masonbe744172007-05-06 10:15:01 -0400923 1, hint, (unsigned long)-1, &ins, 0);
Chris Masonfec577f2007-02-26 10:40:21 -0500924 if (ret) {
925 BUG();
926 return NULL;
927 }
Chris Mason9078a3e2007-04-26 16:46:15 -0400928 BUG_ON(ret);
Chris Masond98237b2007-03-28 13:57:48 -0400929 buf = btrfs_find_create_tree_block(root, ins.objectid);
Chris Masondf2ce342007-03-23 11:00:45 -0400930 set_buffer_uptodate(buf);
Chris Mason090d1872007-05-01 08:53:32 -0400931 set_buffer_checked(buf);
Chris Mason7c4452b2007-04-28 09:29:35 -0400932 set_radix_bit(&trans->transaction->dirty_pages, buf->b_page->index);
Chris Masonfec577f2007-02-26 10:40:21 -0500933 return buf;
934}
Chris Masona28ec192007-03-06 20:08:01 -0500935
Chris Mason6407bf62007-03-27 06:33:00 -0400936static int drop_leaf_ref(struct btrfs_trans_handle *trans,
937 struct btrfs_root *root, struct buffer_head *cur)
938{
939 struct btrfs_disk_key *key;
940 struct btrfs_leaf *leaf;
941 struct btrfs_file_extent_item *fi;
942 int i;
943 int nritems;
944 int ret;
945
946 BUG_ON(!btrfs_is_leaf(btrfs_buffer_node(cur)));
947 leaf = btrfs_buffer_leaf(cur);
948 nritems = btrfs_header_nritems(&leaf->header);
949 for (i = 0; i < nritems; i++) {
950 key = &leaf->items[i].key;
951 if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY)
952 continue;
953 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
Chris Mason236454d2007-04-19 13:37:44 -0400954 if (btrfs_file_extent_type(fi) == BTRFS_FILE_EXTENT_INLINE)
955 continue;
Chris Mason6407bf62007-03-27 06:33:00 -0400956 /*
957 * FIXME make sure to insert a trans record that
958 * repeats the snapshot del on crash
959 */
960 ret = btrfs_free_extent(trans, root,
961 btrfs_file_extent_disk_blocknr(fi),
962 btrfs_file_extent_disk_num_blocks(fi),
963 0);
964 BUG_ON(ret);
965 }
966 return 0;
967}
968
Chris Mason9aca1d52007-03-13 11:09:37 -0400969/*
970 * helper function for drop_snapshot, this walks down the tree dropping ref
971 * counts as it goes.
972 */
Chris Masone089f052007-03-16 16:20:31 -0400973static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
974 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -0500975{
Chris Masone20d96d2007-03-22 12:13:20 -0400976 struct buffer_head *next;
977 struct buffer_head *cur;
Chris Mason20524f02007-03-10 06:35:47 -0500978 u64 blocknr;
979 int ret;
980 u32 refs;
981
Chris Mason5caf2a02007-04-02 11:20:42 -0400982 WARN_ON(*level < 0);
983 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Masonb18c6682007-04-17 13:26:50 -0400984 ret = lookup_extent_ref(trans, root, bh_blocknr(path->nodes[*level]),
Chris Mason6407bf62007-03-27 06:33:00 -0400985 1, &refs);
Chris Mason20524f02007-03-10 06:35:47 -0500986 BUG_ON(ret);
987 if (refs > 1)
988 goto out;
Chris Mason9aca1d52007-03-13 11:09:37 -0400989 /*
990 * walk down to the last node level and free all the leaves
991 */
Chris Mason6407bf62007-03-27 06:33:00 -0400992 while(*level >= 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400993 WARN_ON(*level < 0);
994 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason20524f02007-03-10 06:35:47 -0500995 cur = path->nodes[*level];
Chris Mason2c90e5d2007-04-02 10:50:19 -0400996 if (btrfs_header_level(btrfs_buffer_header(cur)) != *level)
997 WARN_ON(1);
Chris Mason7518a232007-03-12 12:01:18 -0400998 if (path->slots[*level] >=
Chris Masone20d96d2007-03-22 12:13:20 -0400999 btrfs_header_nritems(btrfs_buffer_header(cur)))
Chris Mason20524f02007-03-10 06:35:47 -05001000 break;
Chris Mason6407bf62007-03-27 06:33:00 -04001001 if (*level == 0) {
1002 ret = drop_leaf_ref(trans, root, cur);
1003 BUG_ON(ret);
1004 break;
1005 }
Chris Masone20d96d2007-03-22 12:13:20 -04001006 blocknr = btrfs_node_blockptr(btrfs_buffer_node(cur),
1007 path->slots[*level]);
Chris Masonb18c6682007-04-17 13:26:50 -04001008 ret = lookup_extent_ref(trans, root, blocknr, 1, &refs);
Chris Mason6407bf62007-03-27 06:33:00 -04001009 BUG_ON(ret);
1010 if (refs != 1) {
Chris Mason20524f02007-03-10 06:35:47 -05001011 path->slots[*level]++;
Chris Masone089f052007-03-16 16:20:31 -04001012 ret = btrfs_free_extent(trans, root, blocknr, 1, 1);
Chris Mason20524f02007-03-10 06:35:47 -05001013 BUG_ON(ret);
1014 continue;
1015 }
Chris Mason20524f02007-03-10 06:35:47 -05001016 next = read_tree_block(root, blocknr);
Chris Mason5caf2a02007-04-02 11:20:42 -04001017 WARN_ON(*level <= 0);
Chris Mason83e15a22007-03-12 09:03:27 -04001018 if (path->nodes[*level-1])
Chris Mason234b63a2007-03-13 10:46:10 -04001019 btrfs_block_release(root, path->nodes[*level-1]);
Chris Mason20524f02007-03-10 06:35:47 -05001020 path->nodes[*level-1] = next;
Chris Masone20d96d2007-03-22 12:13:20 -04001021 *level = btrfs_header_level(btrfs_buffer_header(next));
Chris Mason20524f02007-03-10 06:35:47 -05001022 path->slots[*level] = 0;
1023 }
1024out:
Chris Mason5caf2a02007-04-02 11:20:42 -04001025 WARN_ON(*level < 0);
1026 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason6407bf62007-03-27 06:33:00 -04001027 ret = btrfs_free_extent(trans, root,
Chris Mason7eccb902007-04-11 15:53:25 -04001028 bh_blocknr(path->nodes[*level]), 1, 1);
Chris Mason234b63a2007-03-13 10:46:10 -04001029 btrfs_block_release(root, path->nodes[*level]);
Chris Mason20524f02007-03-10 06:35:47 -05001030 path->nodes[*level] = NULL;
1031 *level += 1;
1032 BUG_ON(ret);
1033 return 0;
1034}
1035
Chris Mason9aca1d52007-03-13 11:09:37 -04001036/*
1037 * helper for dropping snapshots. This walks back up the tree in the path
1038 * to find the first node higher up where we haven't yet gone through
1039 * all the slots
1040 */
Chris Masone089f052007-03-16 16:20:31 -04001041static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root
1042 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05001043{
1044 int i;
1045 int slot;
1046 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -04001047 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Mason20524f02007-03-10 06:35:47 -05001048 slot = path->slots[i];
Chris Masone20d96d2007-03-22 12:13:20 -04001049 if (slot < btrfs_header_nritems(
1050 btrfs_buffer_header(path->nodes[i])) - 1) {
Chris Mason20524f02007-03-10 06:35:47 -05001051 path->slots[i]++;
1052 *level = i;
1053 return 0;
1054 } else {
Chris Masone089f052007-03-16 16:20:31 -04001055 ret = btrfs_free_extent(trans, root,
Chris Mason7eccb902007-04-11 15:53:25 -04001056 bh_blocknr(path->nodes[*level]),
Chris Masone089f052007-03-16 16:20:31 -04001057 1, 1);
Chris Mason6407bf62007-03-27 06:33:00 -04001058 BUG_ON(ret);
Chris Mason234b63a2007-03-13 10:46:10 -04001059 btrfs_block_release(root, path->nodes[*level]);
Chris Mason83e15a22007-03-12 09:03:27 -04001060 path->nodes[*level] = NULL;
Chris Mason20524f02007-03-10 06:35:47 -05001061 *level = i + 1;
Chris Mason20524f02007-03-10 06:35:47 -05001062 }
1063 }
1064 return 1;
1065}
1066
Chris Mason9aca1d52007-03-13 11:09:37 -04001067/*
1068 * drop the reference count on the tree rooted at 'snap'. This traverses
1069 * the tree freeing any blocks that have a ref count of zero after being
1070 * decremented.
1071 */
Chris Masone089f052007-03-16 16:20:31 -04001072int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masone20d96d2007-03-22 12:13:20 -04001073 *root, struct buffer_head *snap)
Chris Mason20524f02007-03-10 06:35:47 -05001074{
Chris Mason3768f362007-03-13 16:47:54 -04001075 int ret = 0;
Chris Mason9aca1d52007-03-13 11:09:37 -04001076 int wret;
Chris Mason20524f02007-03-10 06:35:47 -05001077 int level;
Chris Mason5caf2a02007-04-02 11:20:42 -04001078 struct btrfs_path *path;
Chris Mason20524f02007-03-10 06:35:47 -05001079 int i;
1080 int orig_level;
1081
Chris Mason5caf2a02007-04-02 11:20:42 -04001082 path = btrfs_alloc_path();
1083 BUG_ON(!path);
1084 btrfs_init_path(path);
Chris Mason20524f02007-03-10 06:35:47 -05001085
Chris Masone20d96d2007-03-22 12:13:20 -04001086 level = btrfs_header_level(btrfs_buffer_header(snap));
Chris Mason20524f02007-03-10 06:35:47 -05001087 orig_level = level;
Chris Mason5caf2a02007-04-02 11:20:42 -04001088 path->nodes[level] = snap;
1089 path->slots[level] = 0;
Chris Mason20524f02007-03-10 06:35:47 -05001090 while(1) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001091 wret = walk_down_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04001092 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05001093 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04001094 if (wret < 0)
1095 ret = wret;
1096
Chris Mason5caf2a02007-04-02 11:20:42 -04001097 wret = walk_up_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04001098 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05001099 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04001100 if (wret < 0)
1101 ret = wret;
Chris Mason35b7e472007-05-02 15:53:43 -04001102 btrfs_btree_balance_dirty(root);
Chris Mason20524f02007-03-10 06:35:47 -05001103 }
Chris Mason83e15a22007-03-12 09:03:27 -04001104 for (i = 0; i <= orig_level; i++) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001105 if (path->nodes[i]) {
1106 btrfs_block_release(root, path->nodes[i]);
Chris Mason83e15a22007-03-12 09:03:27 -04001107 }
Chris Mason20524f02007-03-10 06:35:47 -05001108 }
Chris Mason5caf2a02007-04-02 11:20:42 -04001109 btrfs_free_path(path);
Chris Mason9aca1d52007-03-13 11:09:37 -04001110 return ret;
Chris Mason20524f02007-03-10 06:35:47 -05001111}
Chris Mason9078a3e2007-04-26 16:46:15 -04001112
Chris Masonbe744172007-05-06 10:15:01 -04001113static int free_block_group_radix(struct radix_tree_root *radix)
Chris Mason9078a3e2007-04-26 16:46:15 -04001114{
1115 int ret;
1116 struct btrfs_block_group_cache *cache[8];
1117 int i;
1118
1119 while(1) {
Chris Masonbe744172007-05-06 10:15:01 -04001120 ret = radix_tree_gang_lookup(radix, (void **)cache, 0,
Chris Mason9078a3e2007-04-26 16:46:15 -04001121 ARRAY_SIZE(cache));
1122 if (!ret)
1123 break;
1124 for (i = 0; i < ret; i++) {
Chris Masonbe744172007-05-06 10:15:01 -04001125 radix_tree_delete(radix, cache[i]->key.objectid +
Chris Mason9078a3e2007-04-26 16:46:15 -04001126 cache[i]->key.offset - 1);
1127 kfree(cache[i]);
1128 }
1129 }
1130 return 0;
1131}
1132
Chris Masonbe744172007-05-06 10:15:01 -04001133int btrfs_free_block_groups(struct btrfs_fs_info *info)
1134{
1135 int ret;
1136 int ret2;
1137
1138 ret = free_block_group_radix(&info->block_group_radix);
1139 ret2 = free_block_group_radix(&info->block_group_data_radix);
1140 if (ret)
1141 return ret;
1142 if (ret2)
1143 return ret2;
1144 return 0;
1145}
1146
Chris Mason9078a3e2007-04-26 16:46:15 -04001147int btrfs_read_block_groups(struct btrfs_root *root)
1148{
1149 struct btrfs_path *path;
1150 int ret;
1151 int err = 0;
1152 struct btrfs_block_group_item *bi;
1153 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04001154 struct btrfs_fs_info *info = root->fs_info;
1155 struct radix_tree_root *radix;
Chris Mason9078a3e2007-04-26 16:46:15 -04001156 struct btrfs_key key;
1157 struct btrfs_key found_key;
1158 struct btrfs_leaf *leaf;
1159 u64 group_size_blocks = BTRFS_BLOCK_GROUP_SIZE / root->blocksize;
Chris Mason31f3c992007-04-30 15:25:45 -04001160 u64 used;
Chris Masonbe744172007-05-06 10:15:01 -04001161 u64 nr = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04001162
Chris Masonbe744172007-05-06 10:15:01 -04001163 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04001164 key.objectid = 0;
1165 key.offset = group_size_blocks;
1166 key.flags = 0;
1167 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
1168
1169 path = btrfs_alloc_path();
1170 if (!path)
1171 return -ENOMEM;
1172
1173 while(1) {
Chris Masonbe744172007-05-06 10:15:01 -04001174 ret = btrfs_search_slot(NULL, info->extent_root,
Chris Mason9078a3e2007-04-26 16:46:15 -04001175 &key, path, 0, 0);
1176 if (ret != 0) {
1177 err = ret;
1178 break;
1179 }
1180 leaf = btrfs_buffer_leaf(path->nodes[0]);
1181 btrfs_disk_key_to_cpu(&found_key,
1182 &leaf->items[path->slots[0]].key);
1183 cache = kmalloc(sizeof(*cache), GFP_NOFS);
1184 if (!cache) {
1185 err = -1;
1186 break;
1187 }
Chris Mason3e1ad542007-05-07 20:03:49 -04001188
1189 if (nr & 1)
1190 radix = &info->block_group_data_radix;
1191 else
1192 radix = &info->block_group_radix;
1193
Chris Mason9078a3e2007-04-26 16:46:15 -04001194 bi = btrfs_item_ptr(leaf, path->slots[0],
1195 struct btrfs_block_group_item);
1196 memcpy(&cache->item, bi, sizeof(*bi));
1197 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Mason31f3c992007-04-30 15:25:45 -04001198 cache->last_alloc = cache->key.objectid;
1199 cache->first_free = cache->key.objectid;
Chris Masonbe744172007-05-06 10:15:01 -04001200 cache->pinned = 0;
1201 cache->data = (nr & 1);
Chris Mason3e1ad542007-05-07 20:03:49 -04001202 cache->radix = radix;
1203
Chris Mason9078a3e2007-04-26 16:46:15 -04001204 key.objectid = found_key.objectid + found_key.offset;
1205 btrfs_release_path(root, path);
Chris Masonbe744172007-05-06 10:15:01 -04001206 ret = radix_tree_insert(radix, found_key.objectid +
Chris Mason9078a3e2007-04-26 16:46:15 -04001207 found_key.offset - 1,
1208 (void *)cache);
1209 BUG_ON(ret);
Chris Mason31f3c992007-04-30 15:25:45 -04001210 used = btrfs_block_group_used(bi);
Chris Masonbe744172007-05-06 10:15:01 -04001211 if (used < (key.offset * 8) / 10) {
1212 radix_tree_tag_set(radix, found_key.objectid +
Chris Mason31f3c992007-04-30 15:25:45 -04001213 found_key.offset - 1,
1214 BTRFS_BLOCK_GROUP_AVAIL);
1215 }
Chris Mason9078a3e2007-04-26 16:46:15 -04001216 if (key.objectid >=
Chris Masonbe744172007-05-06 10:15:01 -04001217 btrfs_super_total_blocks(info->disk_super))
Chris Mason9078a3e2007-04-26 16:46:15 -04001218 break;
Chris Masonbe744172007-05-06 10:15:01 -04001219 nr++;
Chris Mason9078a3e2007-04-26 16:46:15 -04001220 }
1221
1222 btrfs_free_path(path);
1223 return 0;
1224}