blob: 2937fd9aba747e5b2556ed2422368f6e220b0f9a [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) {
26 if (block_group->key.objectid <= blocknr && blocknr <
27 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) {
34 if (block_group->key.objectid <= blocknr && blocknr <
35 block_group->key.objectid + block_group->key.offset)
36 return block_group;
37 }
38printk("lookup_block_group fails for blocknr %Lu\n", blocknr);
39 return NULL;
40}
41
Chris Mason31f3c992007-04-30 15:25:45 -040042struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
43 struct btrfs_block_group_cache
Chris Masonbe744172007-05-06 10:15:01 -040044 *hint, u64 search_start,
45 int data)
Chris Masoncd1bc462007-04-27 10:08:34 -040046{
47 struct btrfs_block_group_cache *cache[8];
Chris Mason31f3c992007-04-30 15:25:45 -040048 struct btrfs_block_group_cache *found_group = NULL;
Chris Masoncd1bc462007-04-27 10:08:34 -040049 struct btrfs_fs_info *info = root->fs_info;
Chris Masonbe744172007-05-06 10:15:01 -040050 struct radix_tree_root *radix;
Chris Masoncd1bc462007-04-27 10:08:34 -040051 u64 used;
Chris Mason31f3c992007-04-30 15:25:45 -040052 u64 last = 0;
53 u64 hint_last;
Chris Masoncd1bc462007-04-27 10:08:34 -040054 int i;
55 int ret;
Chris Mason31f3c992007-04-30 15:25:45 -040056 int full_search = 0;
Chris Masonbe744172007-05-06 10:15:01 -040057
58 if (data)
59 radix = &info->block_group_data_radix;
60 else
61 radix = &info->block_group_radix;
62
63 if (search_start) {
64 struct btrfs_block_group_cache *shint;
65 shint = lookup_block_group(info, search_start);
66 if (shint->data == data) {
67 used = btrfs_block_group_used(&shint->item);
68 if (used + shint->pinned <
69 (shint->key.offset * 8) / 10) {
70 return shint;
71 }
72 }
73 }
74 if (hint && hint->data == data) {
Chris Mason31f3c992007-04-30 15:25:45 -040075 used = btrfs_block_group_used(&hint->item);
Chris Masonbe744172007-05-06 10:15:01 -040076 if (used + hint->pinned < (hint->key.offset * 8) / 10) {
Chris Mason31f3c992007-04-30 15:25:45 -040077 return hint;
78 }
Chris Masonbe744172007-05-06 10:15:01 -040079 if (used >= (hint->key.offset * 8) / 10) {
80 radix_tree_tag_clear(radix,
81 hint->key.objectid +
82 hint->key.offset - 1,
83 BTRFS_BLOCK_GROUP_AVAIL);
84 }
85 last = hint->key.offset * 2;
86 if (hint->key.objectid >= last)
87 last = max(search_start, hint->key.objectid - last);
88 else
89 last = hint->key.objectid + hint->key.offset;
Chris Mason31f3c992007-04-30 15:25:45 -040090 hint_last = last;
91 } else {
Chris Masonbe744172007-05-06 10:15:01 -040092 hint_last = search_start;
93 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -040094 }
Chris Masoncd1bc462007-04-27 10:08:34 -040095 while(1) {
Chris Masonbe744172007-05-06 10:15:01 -040096 ret = radix_tree_gang_lookup_tag(radix, (void **)cache,
Chris Masoncd1bc462007-04-27 10:08:34 -040097 last, ARRAY_SIZE(cache),
Chris Mason31f3c992007-04-30 15:25:45 -040098 BTRFS_BLOCK_GROUP_AVAIL);
Chris Masoncd1bc462007-04-27 10:08:34 -040099 if (!ret)
100 break;
101 for (i = 0; i < ret; i++) {
Chris Masonbe08c1b2007-05-03 09:06:49 -0400102 last = cache[i]->key.objectid +
103 cache[i]->key.offset;
Chris Masoncd1bc462007-04-27 10:08:34 -0400104 used = btrfs_block_group_used(&cache[i]->item);
Chris Masonbe744172007-05-06 10:15:01 -0400105 if (used + cache[i]->pinned <
106 (cache[i]->key.offset * 8) / 10) {
Chris Mason31f3c992007-04-30 15:25:45 -0400107 found_group = cache[i];
108 goto found;
Chris Masoncd1bc462007-04-27 10:08:34 -0400109 }
Chris Masonbe744172007-05-06 10:15:01 -0400110 if (used >= (cache[i]->key.offset * 8) / 10) {
111 radix_tree_tag_clear(radix,
112 cache[i]->key.objectid +
113 cache[i]->key.offset - 1,
114 BTRFS_BLOCK_GROUP_AVAIL);
115 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400116 }
117 }
Chris Mason31f3c992007-04-30 15:25:45 -0400118 last = hint_last;
119again:
Chris Masoncd1bc462007-04-27 10:08:34 -0400120 while(1) {
Chris Masonbe744172007-05-06 10:15:01 -0400121 ret = radix_tree_gang_lookup(radix, (void **)cache,
122 last, ARRAY_SIZE(cache));
Chris Masoncd1bc462007-04-27 10:08:34 -0400123 if (!ret)
124 break;
125 for (i = 0; i < ret; i++) {
Chris Masonbe08c1b2007-05-03 09:06:49 -0400126 last = cache[i]->key.objectid +
127 cache[i]->key.offset;
Chris Masoncd1bc462007-04-27 10:08:34 -0400128 used = btrfs_block_group_used(&cache[i]->item);
Chris Masonbe744172007-05-06 10:15:01 -0400129 if (used + cache[i]->pinned < cache[i]->key.offset) {
Chris Mason31f3c992007-04-30 15:25:45 -0400130 found_group = cache[i];
131 goto found;
Chris Masoncd1bc462007-04-27 10:08:34 -0400132 }
Chris Masonbe744172007-05-06 10:15:01 -0400133 if (used >= cache[i]->key.offset) {
134 radix_tree_tag_clear(radix,
135 cache[i]->key.objectid +
136 cache[i]->key.offset - 1,
137 BTRFS_BLOCK_GROUP_AVAIL);
138 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400139 }
140 }
Chris Mason31f3c992007-04-30 15:25:45 -0400141 if (!full_search) {
Chris Masonbe744172007-05-06 10:15:01 -0400142 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -0400143 full_search = 1;
144 goto again;
145 }
Chris Mason31f3c992007-04-30 15:25:45 -0400146 if (!found_group) {
Chris Masonbe744172007-05-06 10:15:01 -0400147 ret = radix_tree_gang_lookup(radix,
Chris Mason31f3c992007-04-30 15:25:45 -0400148 (void **)&found_group, 0, 1);
149 BUG_ON(ret != 1);
150 }
Chris Masonbe744172007-05-06 10:15:01 -0400151found:
Chris Mason31f3c992007-04-30 15:25:45 -0400152 return found_group;
Chris Masoncd1bc462007-04-27 10:08:34 -0400153}
154
Chris Masonb18c6682007-04-17 13:26:50 -0400155int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
156 struct btrfs_root *root,
157 u64 blocknr, u64 num_blocks)
Chris Mason02217ed2007-03-02 16:08:05 -0500158{
Chris Mason5caf2a02007-04-02 11:20:42 -0400159 struct btrfs_path *path;
Chris Mason02217ed2007-03-02 16:08:05 -0500160 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400161 struct btrfs_key key;
Chris Mason234b63a2007-03-13 10:46:10 -0400162 struct btrfs_leaf *l;
163 struct btrfs_extent_item *item;
Chris Masone2fa7222007-03-12 16:22:34 -0400164 struct btrfs_key ins;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400165 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500166
Chris Mason9f5fae22007-03-20 14:38:32 -0400167 find_free_extent(trans, root->fs_info->extent_root, 0, 0, (u64)-1,
Chris Masonbe08c1b2007-05-03 09:06:49 -0400168 &ins, 0);
Chris Mason5caf2a02007-04-02 11:20:42 -0400169 path = btrfs_alloc_path();
170 BUG_ON(!path);
171 btrfs_init_path(path);
Chris Mason02217ed2007-03-02 16:08:05 -0500172 key.objectid = blocknr;
173 key.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -0400174 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Mason6407bf62007-03-27 06:33:00 -0400175 key.offset = num_blocks;
Chris Mason5caf2a02007-04-02 11:20:42 -0400176 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -0400177 0, 1);
Chris Masona429e512007-04-18 16:15:28 -0400178 if (ret != 0) {
179printk("can't find block %Lu %Lu\n", blocknr, num_blocks);
Chris Masona28ec192007-03-06 20:08:01 -0500180 BUG();
Chris Masona429e512007-04-18 16:15:28 -0400181 }
Chris Mason02217ed2007-03-02 16:08:05 -0500182 BUG_ON(ret != 0);
Chris Mason5caf2a02007-04-02 11:20:42 -0400183 l = btrfs_buffer_leaf(path->nodes[0]);
184 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400185 refs = btrfs_extent_refs(item);
186 btrfs_set_extent_refs(item, refs + 1);
Chris Mason5caf2a02007-04-02 11:20:42 -0400187 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masona28ec192007-03-06 20:08:01 -0500188
Chris Mason5caf2a02007-04-02 11:20:42 -0400189 btrfs_release_path(root->fs_info->extent_root, path);
190 btrfs_free_path(path);
Chris Mason9f5fae22007-03-20 14:38:32 -0400191 finish_current_insert(trans, root->fs_info->extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -0400192 del_pending_extents(trans, root->fs_info->extent_root);
Chris Mason02217ed2007-03-02 16:08:05 -0500193 return 0;
194}
195
Chris Masonb18c6682007-04-17 13:26:50 -0400196static int lookup_extent_ref(struct btrfs_trans_handle *trans,
197 struct btrfs_root *root, u64 blocknr,
198 u64 num_blocks, u32 *refs)
Chris Masona28ec192007-03-06 20:08:01 -0500199{
Chris Mason5caf2a02007-04-02 11:20:42 -0400200 struct btrfs_path *path;
Chris Masona28ec192007-03-06 20:08:01 -0500201 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400202 struct btrfs_key key;
Chris Mason234b63a2007-03-13 10:46:10 -0400203 struct btrfs_leaf *l;
204 struct btrfs_extent_item *item;
Chris Mason5caf2a02007-04-02 11:20:42 -0400205
206 path = btrfs_alloc_path();
207 btrfs_init_path(path);
Chris Masona28ec192007-03-06 20:08:01 -0500208 key.objectid = blocknr;
Chris Mason6407bf62007-03-27 06:33:00 -0400209 key.offset = num_blocks;
Chris Mason62e27492007-03-15 12:56:47 -0400210 key.flags = 0;
211 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Mason5caf2a02007-04-02 11:20:42 -0400212 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -0400213 0, 0);
Chris Masona28ec192007-03-06 20:08:01 -0500214 if (ret != 0)
215 BUG();
Chris Mason5caf2a02007-04-02 11:20:42 -0400216 l = btrfs_buffer_leaf(path->nodes[0]);
217 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400218 *refs = btrfs_extent_refs(item);
Chris Mason5caf2a02007-04-02 11:20:42 -0400219 btrfs_release_path(root->fs_info->extent_root, path);
220 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -0500221 return 0;
222}
223
Chris Masonc5739bb2007-04-10 09:27:04 -0400224int btrfs_inc_root_ref(struct btrfs_trans_handle *trans,
225 struct btrfs_root *root)
226{
Chris Masonb18c6682007-04-17 13:26:50 -0400227 return btrfs_inc_extent_ref(trans, root, bh_blocknr(root->node), 1);
Chris Masonc5739bb2007-04-10 09:27:04 -0400228}
229
Chris Masone089f052007-03-16 16:20:31 -0400230int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Masone20d96d2007-03-22 12:13:20 -0400231 struct buffer_head *buf)
Chris Mason02217ed2007-03-02 16:08:05 -0500232{
233 u64 blocknr;
Chris Masone20d96d2007-03-22 12:13:20 -0400234 struct btrfs_node *buf_node;
Chris Mason6407bf62007-03-27 06:33:00 -0400235 struct btrfs_leaf *buf_leaf;
236 struct btrfs_disk_key *key;
237 struct btrfs_file_extent_item *fi;
Chris Mason02217ed2007-03-02 16:08:05 -0500238 int i;
Chris Mason6407bf62007-03-27 06:33:00 -0400239 int leaf;
240 int ret;
Chris Masona28ec192007-03-06 20:08:01 -0500241
Chris Mason3768f362007-03-13 16:47:54 -0400242 if (!root->ref_cows)
Chris Masona28ec192007-03-06 20:08:01 -0500243 return 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400244 buf_node = btrfs_buffer_node(buf);
Chris Mason6407bf62007-03-27 06:33:00 -0400245 leaf = btrfs_is_leaf(buf_node);
246 buf_leaf = btrfs_buffer_leaf(buf);
Chris Masone20d96d2007-03-22 12:13:20 -0400247 for (i = 0; i < btrfs_header_nritems(&buf_node->header); i++) {
Chris Mason6407bf62007-03-27 06:33:00 -0400248 if (leaf) {
249 key = &buf_leaf->items[i].key;
250 if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY)
251 continue;
252 fi = btrfs_item_ptr(buf_leaf, i,
253 struct btrfs_file_extent_item);
Chris Mason236454d2007-04-19 13:37:44 -0400254 if (btrfs_file_extent_type(fi) ==
255 BTRFS_FILE_EXTENT_INLINE)
256 continue;
Chris Masonb18c6682007-04-17 13:26:50 -0400257 ret = btrfs_inc_extent_ref(trans, root,
Chris Mason6407bf62007-03-27 06:33:00 -0400258 btrfs_file_extent_disk_blocknr(fi),
259 btrfs_file_extent_disk_num_blocks(fi));
260 BUG_ON(ret);
261 } else {
262 blocknr = btrfs_node_blockptr(buf_node, i);
Chris Masonb18c6682007-04-17 13:26:50 -0400263 ret = btrfs_inc_extent_ref(trans, root, blocknr, 1);
Chris Mason6407bf62007-03-27 06:33:00 -0400264 BUG_ON(ret);
265 }
Chris Mason02217ed2007-03-02 16:08:05 -0500266 }
267 return 0;
268}
269
Chris Mason9078a3e2007-04-26 16:46:15 -0400270static int write_one_cache_group(struct btrfs_trans_handle *trans,
271 struct btrfs_root *root,
272 struct btrfs_path *path,
273 struct btrfs_block_group_cache *cache)
274{
275 int ret;
276 int pending_ret;
277 struct btrfs_root *extent_root = root->fs_info->extent_root;
278 struct btrfs_block_group_item *bi;
279 struct btrfs_key ins;
280
Chris Masonbe08c1b2007-05-03 09:06:49 -0400281 find_free_extent(trans, extent_root, 0, 0, (u64)-1, &ins, 0);
Chris Mason9078a3e2007-04-26 16:46:15 -0400282 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
283 BUG_ON(ret);
284 bi = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
285 struct btrfs_block_group_item);
286 memcpy(bi, &cache->item, sizeof(*bi));
287 mark_buffer_dirty(path->nodes[0]);
288 btrfs_release_path(extent_root, path);
289
290 finish_current_insert(trans, extent_root);
291 pending_ret = del_pending_extents(trans, extent_root);
292 if (ret)
293 return ret;
294 if (pending_ret)
295 return pending_ret;
Chris Masonbe744172007-05-06 10:15:01 -0400296 if (cache->data)
297 cache->last_alloc = cache->first_free;
Chris Mason9078a3e2007-04-26 16:46:15 -0400298 return 0;
299
300}
301
Chris Masonbe744172007-05-06 10:15:01 -0400302static int write_dirty_block_radix(struct btrfs_trans_handle *trans,
303 struct btrfs_root *root,
304 struct radix_tree_root *radix)
Chris Mason9078a3e2007-04-26 16:46:15 -0400305{
306 struct btrfs_block_group_cache *cache[8];
307 int ret;
308 int err = 0;
309 int werr = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -0400310 int i;
311 struct btrfs_path *path;
312
313 path = btrfs_alloc_path();
314 if (!path)
315 return -ENOMEM;
316
317 while(1) {
318 ret = radix_tree_gang_lookup_tag(radix, (void **)cache,
319 0, ARRAY_SIZE(cache),
320 BTRFS_BLOCK_GROUP_DIRTY);
321 if (!ret)
322 break;
323 for (i = 0; i < ret; i++) {
324 radix_tree_tag_clear(radix, cache[i]->key.objectid +
325 cache[i]->key.offset - 1,
326 BTRFS_BLOCK_GROUP_DIRTY);
327 err = write_one_cache_group(trans, root,
328 path, cache[i]);
329 if (err)
330 werr = err;
331 }
332 }
333 btrfs_free_path(path);
334 return werr;
335}
336
Chris Masonbe744172007-05-06 10:15:01 -0400337int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
338 struct btrfs_root *root)
339{
340 int ret;
341 int ret2;
342 ret = write_dirty_block_radix(trans, root,
343 &root->fs_info->block_group_radix);
344 ret2 = write_dirty_block_radix(trans, root,
345 &root->fs_info->block_group_data_radix);
346 if (ret)
347 return ret;
348 if (ret2)
349 return ret2;
350 return 0;
351}
352
Chris Mason9078a3e2007-04-26 16:46:15 -0400353static int update_block_group(struct btrfs_trans_handle *trans,
354 struct btrfs_root *root,
355 u64 blocknr, u64 num, int alloc)
356{
357 struct btrfs_block_group_cache *cache;
358 struct btrfs_fs_info *info = root->fs_info;
Chris Masonbe744172007-05-06 10:15:01 -0400359 struct radix_tree_root *radix;
Chris Mason9078a3e2007-04-26 16:46:15 -0400360 u64 total = num;
361 u64 old_val;
362 u64 block_in_group;
363 int ret;
Chris Masonbe744172007-05-06 10:15:01 -0400364 if (num != 1)
365 radix = &info->block_group_data_radix;
366 else
367 radix = &info->block_group_radix;
Chris Mason9078a3e2007-04-26 16:46:15 -0400368 while(total) {
Chris Masonbe744172007-05-06 10:15:01 -0400369 ret = radix_tree_gang_lookup(radix, (void **)&cache,
370 blocknr, 1);
Chris Masoncd1bc462007-04-27 10:08:34 -0400371 if (!ret) {
372 printk(KERN_CRIT "blocknr %Lu lookup failed\n",
373 blocknr);
Chris Mason9078a3e2007-04-26 16:46:15 -0400374 return -1;
Chris Masoncd1bc462007-04-27 10:08:34 -0400375 }
Chris Mason9078a3e2007-04-26 16:46:15 -0400376 block_in_group = blocknr - cache->key.objectid;
Chris Masonbe744172007-05-06 10:15:01 -0400377 if (block_in_group > cache->key.offset || cache->key.objectid >
378 blocknr) {
379 if (radix == &info->block_group_data_radix)
380 radix = &info->block_group_radix;
381 else
382 radix = &info->block_group_data_radix;
383 ret = radix_tree_gang_lookup(radix, (void **)&cache,
384 blocknr, 1);
385 if (!ret) {
386 printk(KERN_CRIT "blocknr %Lu lookup failed\n",
387 blocknr);
388 return -1;
389 }
390 block_in_group = blocknr - cache->key.objectid;
391 if (block_in_group > cache->key.offset ||
392 cache->key.objectid > blocknr) {
393 BUG();
394 }
395 }
Chris Mason9078a3e2007-04-26 16:46:15 -0400396 WARN_ON(block_in_group > cache->key.offset);
Chris Masonbe744172007-05-06 10:15:01 -0400397 radix_tree_tag_set(radix, cache->key.objectid +
398 cache->key.offset - 1,
Chris Mason9078a3e2007-04-26 16:46:15 -0400399 BTRFS_BLOCK_GROUP_DIRTY);
400
401 old_val = btrfs_block_group_used(&cache->item);
402 num = min(total, cache->key.offset - block_in_group);
403 total -= num;
404 blocknr += num;
Chris Masoncd1bc462007-04-27 10:08:34 -0400405 if (alloc) {
Chris Mason9078a3e2007-04-26 16:46:15 -0400406 old_val += num;
Chris Masoncd1bc462007-04-27 10:08:34 -0400407 if (blocknr > cache->last_alloc)
408 cache->last_alloc = blocknr;
409 } else {
Chris Mason9078a3e2007-04-26 16:46:15 -0400410 old_val -= num;
Chris Masoncd1bc462007-04-27 10:08:34 -0400411 if (blocknr < cache->first_free)
412 cache->first_free = blocknr;
413 }
Chris Mason9078a3e2007-04-26 16:46:15 -0400414 btrfs_set_block_group_used(&cache->item, old_val);
415 }
416 return 0;
417}
418
Chris Masonbe08c1b2007-05-03 09:06:49 -0400419static int try_remove_page(struct address_space *mapping, unsigned long index)
420{
421 int ret;
422 ret = invalidate_mapping_pages(mapping, index, index);
423 return ret;
424}
425
Chris Masone089f052007-03-16 16:20:31 -0400426int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, struct
427 btrfs_root *root)
Chris Masona28ec192007-03-06 20:08:01 -0500428{
Chris Mason8ef97622007-03-26 10:15:30 -0400429 unsigned long gang[8];
Chris Masonbe08c1b2007-05-03 09:06:49 -0400430 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Masonbe744172007-05-06 10:15:01 -0400431 struct btrfs_block_group_cache *block_group;
Chris Mason88fd1462007-03-16 08:56:18 -0400432 u64 first = 0;
Chris Masona28ec192007-03-06 20:08:01 -0500433 int ret;
434 int i;
Chris Mason8ef97622007-03-26 10:15:30 -0400435 struct radix_tree_root *pinned_radix = &root->fs_info->pinned_radix;
Chris Masona28ec192007-03-06 20:08:01 -0500436
437 while(1) {
Chris Mason8ef97622007-03-26 10:15:30 -0400438 ret = find_first_radix_bit(pinned_radix, gang,
439 ARRAY_SIZE(gang));
Chris Masona28ec192007-03-06 20:08:01 -0500440 if (!ret)
441 break;
Chris Mason88fd1462007-03-16 08:56:18 -0400442 if (!first)
Chris Mason8ef97622007-03-26 10:15:30 -0400443 first = gang[0];
Chris Mason0579da42007-03-07 16:15:30 -0500444 for (i = 0; i < ret; i++) {
Chris Mason8ef97622007-03-26 10:15:30 -0400445 clear_radix_bit(pinned_radix, gang[i]);
Chris Masonbe744172007-05-06 10:15:01 -0400446 block_group = lookup_block_group(root->fs_info,
447 gang[i]);
448 if (block_group) {
449 WARN_ON(block_group->pinned == 0);
450 block_group->pinned--;
451 if (gang[i] < block_group->last_alloc)
452 block_group->last_alloc = gang[i];
453 }
Chris Masonbe08c1b2007-05-03 09:06:49 -0400454 try_remove_page(btree_inode->i_mapping,
455 gang[i] << (PAGE_CACHE_SHIFT -
456 btree_inode->i_blkbits));
Chris Mason0579da42007-03-07 16:15:30 -0500457 }
Chris Masona28ec192007-03-06 20:08:01 -0500458 }
459 return 0;
460}
461
Chris Masone089f052007-03-16 16:20:31 -0400462static int finish_current_insert(struct btrfs_trans_handle *trans, struct
463 btrfs_root *extent_root)
Chris Mason037e6392007-03-07 11:50:24 -0500464{
Chris Masone2fa7222007-03-12 16:22:34 -0400465 struct btrfs_key ins;
Chris Mason234b63a2007-03-13 10:46:10 -0400466 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -0500467 int i;
468 int ret;
Chris Mason1261ec42007-03-20 20:35:03 -0400469 u64 super_blocks_used;
470 struct btrfs_fs_info *info = extent_root->fs_info;
Chris Mason037e6392007-03-07 11:50:24 -0500471
Chris Masoncf27e1e2007-03-13 09:49:06 -0400472 btrfs_set_extent_refs(&extent_item, 1);
Chris Mason037e6392007-03-07 11:50:24 -0500473 ins.offset = 1;
474 ins.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -0400475 btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
Chris Mason5d0c3e62007-04-23 17:01:05 -0400476 btrfs_set_extent_owner(&extent_item, extent_root->root_key.objectid);
Chris Mason037e6392007-03-07 11:50:24 -0500477
Chris Masonf2458e12007-04-25 15:52:25 -0400478 for (i = 0; i < extent_root->fs_info->extent_tree_insert_nr; i++) {
479 ins.objectid = extent_root->fs_info->extent_tree_insert[i];
Chris Mason1261ec42007-03-20 20:35:03 -0400480 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
481 btrfs_set_super_blocks_used(info->disk_super,
482 super_blocks_used + 1);
Chris Masone089f052007-03-16 16:20:31 -0400483 ret = btrfs_insert_item(trans, extent_root, &ins, &extent_item,
484 sizeof(extent_item));
Chris Mason037e6392007-03-07 11:50:24 -0500485 BUG_ON(ret);
486 }
Chris Masonf2458e12007-04-25 15:52:25 -0400487 extent_root->fs_info->extent_tree_insert_nr = 0;
488 extent_root->fs_info->extent_tree_prealloc_nr = 0;
Chris Mason037e6392007-03-07 11:50:24 -0500489 return 0;
490}
491
Chris Mason8ef97622007-03-26 10:15:30 -0400492static int pin_down_block(struct btrfs_root *root, u64 blocknr, int pending)
Chris Masone20d96d2007-03-22 12:13:20 -0400493{
494 int err;
Chris Mason78fae272007-03-25 11:35:08 -0400495 struct btrfs_header *header;
Chris Mason8ef97622007-03-26 10:15:30 -0400496 struct buffer_head *bh;
Chris Mason78fae272007-03-25 11:35:08 -0400497
Chris Masonf4b9aa82007-03-27 11:05:53 -0400498 if (!pending) {
Chris Masond98237b2007-03-28 13:57:48 -0400499 bh = btrfs_find_tree_block(root, blocknr);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400500 if (bh) {
501 if (buffer_uptodate(bh)) {
502 u64 transid =
503 root->fs_info->running_transaction->transid;
504 header = btrfs_buffer_header(bh);
505 if (btrfs_header_generation(header) ==
506 transid) {
507 btrfs_block_release(root, bh);
508 return 0;
509 }
Chris Masonf4b9aa82007-03-27 11:05:53 -0400510 }
Chris Masond6025572007-03-30 14:27:56 -0400511 btrfs_block_release(root, bh);
Chris Mason8ef97622007-03-26 10:15:30 -0400512 }
Chris Mason8ef97622007-03-26 10:15:30 -0400513 err = set_radix_bit(&root->fs_info->pinned_radix, blocknr);
Chris Masonbe744172007-05-06 10:15:01 -0400514 if (!err) {
515 struct btrfs_block_group_cache *cache;
516 cache = lookup_block_group(root->fs_info, blocknr);
517 if (cache)
518 cache->pinned++;
519 }
Chris Masonf4b9aa82007-03-27 11:05:53 -0400520 } else {
521 err = set_radix_bit(&root->fs_info->pending_del_radix, blocknr);
522 }
Chris Masonbe744172007-05-06 10:15:01 -0400523 BUG_ON(err < 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400524 return 0;
525}
526
Chris Masona28ec192007-03-06 20:08:01 -0500527/*
528 * remove an extent from the root, returns 0 on success
529 */
Chris Masone089f052007-03-16 16:20:31 -0400530static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason78fae272007-03-25 11:35:08 -0400531 *root, u64 blocknr, u64 num_blocks, int pin)
Chris Masona28ec192007-03-06 20:08:01 -0500532{
Chris Mason5caf2a02007-04-02 11:20:42 -0400533 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -0400534 struct btrfs_key key;
Chris Mason1261ec42007-03-20 20:35:03 -0400535 struct btrfs_fs_info *info = root->fs_info;
536 struct btrfs_root *extent_root = info->extent_root;
Chris Masona28ec192007-03-06 20:08:01 -0500537 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400538 struct btrfs_extent_item *ei;
Chris Masone2fa7222007-03-12 16:22:34 -0400539 struct btrfs_key ins;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400540 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500541
Chris Masona28ec192007-03-06 20:08:01 -0500542 key.objectid = blocknr;
543 key.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -0400544 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masona28ec192007-03-06 20:08:01 -0500545 key.offset = num_blocks;
546
Chris Masonbe08c1b2007-05-03 09:06:49 -0400547 find_free_extent(trans, root, 0, 0, (u64)-1, &ins, 0);
Chris Mason5caf2a02007-04-02 11:20:42 -0400548 path = btrfs_alloc_path();
549 BUG_ON(!path);
550 btrfs_init_path(path);
Chris Mason5f26f772007-04-05 10:38:44 -0400551
Chris Mason5caf2a02007-04-02 11:20:42 -0400552 ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
Chris Masona28ec192007-03-06 20:08:01 -0500553 if (ret) {
Chris Mason2e635a22007-03-21 11:12:56 -0400554 printk("failed to find %Lu\n", key.objectid);
Chris Mason234b63a2007-03-13 10:46:10 -0400555 btrfs_print_tree(extent_root, extent_root->node);
Chris Mason2e635a22007-03-21 11:12:56 -0400556 printk("failed to find %Lu\n", key.objectid);
Chris Masona28ec192007-03-06 20:08:01 -0500557 BUG();
558 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400559 ei = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
Chris Mason123abc82007-03-14 14:14:43 -0400560 struct btrfs_extent_item);
Chris Masona28ec192007-03-06 20:08:01 -0500561 BUG_ON(ei->refs == 0);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400562 refs = btrfs_extent_refs(ei) - 1;
563 btrfs_set_extent_refs(ei, refs);
Chris Mason5caf2a02007-04-02 11:20:42 -0400564 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400565 if (refs == 0) {
Chris Mason1261ec42007-03-20 20:35:03 -0400566 u64 super_blocks_used;
Chris Mason78fae272007-03-25 11:35:08 -0400567
568 if (pin) {
Chris Mason8ef97622007-03-26 10:15:30 -0400569 ret = pin_down_block(root, blocknr, 0);
Chris Mason78fae272007-03-25 11:35:08 -0400570 BUG_ON(ret);
571 }
572
Chris Mason1261ec42007-03-20 20:35:03 -0400573 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
574 btrfs_set_super_blocks_used(info->disk_super,
575 super_blocks_used - num_blocks);
Chris Mason5caf2a02007-04-02 11:20:42 -0400576 ret = btrfs_del_item(trans, extent_root, path);
Chris Masona28ec192007-03-06 20:08:01 -0500577 if (ret)
578 BUG();
Chris Mason9078a3e2007-04-26 16:46:15 -0400579 ret = update_block_group(trans, root, blocknr, num_blocks, 0);
580 BUG_ON(ret);
Chris Masona28ec192007-03-06 20:08:01 -0500581 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400582 btrfs_release_path(extent_root, path);
583 btrfs_free_path(path);
Chris Masone089f052007-03-16 16:20:31 -0400584 finish_current_insert(trans, extent_root);
Chris Masona28ec192007-03-06 20:08:01 -0500585 return ret;
586}
587
588/*
Chris Masonfec577f2007-02-26 10:40:21 -0500589 * find all the blocks marked as pending in the radix tree and remove
590 * them from the extent map
591 */
Chris Masone089f052007-03-16 16:20:31 -0400592static int del_pending_extents(struct btrfs_trans_handle *trans, struct
593 btrfs_root *extent_root)
Chris Masonfec577f2007-02-26 10:40:21 -0500594{
595 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -0400596 int wret;
597 int err = 0;
Chris Mason8ef97622007-03-26 10:15:30 -0400598 unsigned long gang[4];
Chris Masonfec577f2007-02-26 10:40:21 -0500599 int i;
Chris Mason8ef97622007-03-26 10:15:30 -0400600 struct radix_tree_root *pending_radix;
601 struct radix_tree_root *pinned_radix;
Chris Masonbe744172007-05-06 10:15:01 -0400602 struct btrfs_block_group_cache *cache;
Chris Mason8ef97622007-03-26 10:15:30 -0400603
604 pending_radix = &extent_root->fs_info->pending_del_radix;
605 pinned_radix = &extent_root->fs_info->pinned_radix;
Chris Masonfec577f2007-02-26 10:40:21 -0500606
607 while(1) {
Chris Mason8ef97622007-03-26 10:15:30 -0400608 ret = find_first_radix_bit(pending_radix, gang,
609 ARRAY_SIZE(gang));
Chris Masonfec577f2007-02-26 10:40:21 -0500610 if (!ret)
611 break;
612 for (i = 0; i < ret; i++) {
Chris Mason8ef97622007-03-26 10:15:30 -0400613 wret = set_radix_bit(pinned_radix, gang[i]);
Chris Masonbe744172007-05-06 10:15:01 -0400614 if (wret == 0) {
615 cache = lookup_block_group(extent_root->fs_info,
616 gang[i]);
617 if (cache)
618 cache->pinned++;
619 }
620 if (wret < 0) {
621 printk(KERN_CRIT "set_radix_bit, err %d\n",
622 wret);
623 BUG_ON(wret < 0);
624 }
Chris Mason8ef97622007-03-26 10:15:30 -0400625 wret = clear_radix_bit(pending_radix, gang[i]);
626 BUG_ON(wret);
Chris Masond5719762007-03-23 10:01:08 -0400627 wret = __free_extent(trans, extent_root,
Chris Mason8ef97622007-03-26 10:15:30 -0400628 gang[i], 1, 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400629 if (wret)
630 err = wret;
Chris Masonfec577f2007-02-26 10:40:21 -0500631 }
632 }
Chris Masone20d96d2007-03-22 12:13:20 -0400633 return err;
Chris Masonfec577f2007-02-26 10:40:21 -0500634}
635
636/*
637 * remove an extent from the root, returns 0 on success
638 */
Chris Masone089f052007-03-16 16:20:31 -0400639int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
640 *root, u64 blocknr, u64 num_blocks, int pin)
Chris Masonfec577f2007-02-26 10:40:21 -0500641{
Chris Mason9f5fae22007-03-20 14:38:32 -0400642 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Masonfec577f2007-02-26 10:40:21 -0500643 int pending_ret;
644 int ret;
Chris Masona28ec192007-03-06 20:08:01 -0500645
646 if (root == extent_root) {
Chris Mason8ef97622007-03-26 10:15:30 -0400647 pin_down_block(root, blocknr, 1);
Chris Masona28ec192007-03-06 20:08:01 -0500648 return 0;
649 }
Chris Mason78fae272007-03-25 11:35:08 -0400650 ret = __free_extent(trans, root, blocknr, num_blocks, pin);
Chris Masone20d96d2007-03-22 12:13:20 -0400651 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
Chris Masonfec577f2007-02-26 10:40:21 -0500652 return ret ? ret : pending_ret;
653}
654
655/*
656 * walks the btree of allocated extents and find a hole of a given size.
657 * The key ins is changed to record the hole:
658 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -0400659 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -0500660 * ins->offset == number of blocks
661 * Any available blocks before search_start are skipped.
662 */
Chris Masone089f052007-03-16 16:20:31 -0400663static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
664 *orig_root, u64 num_blocks, u64 search_start, u64
Chris Masonbe08c1b2007-05-03 09:06:49 -0400665 search_end, struct btrfs_key *ins, int data)
Chris Masonfec577f2007-02-26 10:40:21 -0500666{
Chris Mason5caf2a02007-04-02 11:20:42 -0400667 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -0400668 struct btrfs_key key;
Chris Masonfec577f2007-02-26 10:40:21 -0500669 int ret;
670 u64 hole_size = 0;
671 int slot = 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400672 u64 last_block = 0;
Chris Mason037e6392007-03-07 11:50:24 -0500673 u64 test_block;
Chris Masonbe744172007-05-06 10:15:01 -0400674 u64 orig_search_start = search_start;
Chris Masonfec577f2007-02-26 10:40:21 -0500675 int start_found;
Chris Mason234b63a2007-03-13 10:46:10 -0400676 struct btrfs_leaf *l;
Chris Mason9f5fae22007-03-20 14:38:32 -0400677 struct btrfs_root * root = orig_root->fs_info->extent_root;
Chris Masonf2458e12007-04-25 15:52:25 -0400678 struct btrfs_fs_info *info = root->fs_info;
Chris Mason0579da42007-03-07 16:15:30 -0500679 int total_needed = num_blocks;
Chris Masonf2458e12007-04-25 15:52:25 -0400680 int total_found = 0;
681 int fill_prealloc = 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400682 int level;
Chris Masonbe08c1b2007-05-03 09:06:49 -0400683 struct btrfs_block_group_cache *block_group;
Chris Masonbe744172007-05-06 10:15:01 -0400684 int full_scan = 0;
Chris Masonfec577f2007-02-26 10:40:21 -0500685
Chris Masonb1a4d962007-04-04 15:27:52 -0400686 path = btrfs_alloc_path();
687 ins->flags = 0;
688 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
689
Chris Masone20d96d2007-03-22 12:13:20 -0400690 level = btrfs_header_level(btrfs_buffer_header(root->node));
Chris Masonf2458e12007-04-25 15:52:25 -0400691 if (num_blocks == 0) {
692 fill_prealloc = 1;
693 num_blocks = 1;
Chris Mason308535a2007-04-28 15:17:08 -0400694 total_needed = (min(level + 1, BTRFS_MAX_LEVEL) + 2) * 3;
Chris Masonf2458e12007-04-25 15:52:25 -0400695 }
Chris Masonbe744172007-05-06 10:15:01 -0400696 if (search_start) {
697 block_group = lookup_block_group(info, search_start);
698 block_group = btrfs_find_block_group(root, block_group,
699 search_start, data);
700 } else {
701 block_group = btrfs_find_block_group(root,
702 trans->block_group, 0,
703 data);
704 }
705
706check_failed:
707 if (block_group->data != data)
708 WARN_ON(1);
Chris Masonbe08c1b2007-05-03 09:06:49 -0400709 if (block_group->last_alloc > search_start)
710 search_start = block_group->last_alloc;
Chris Mason5caf2a02007-04-02 11:20:42 -0400711 btrfs_init_path(path);
Chris Masonfec577f2007-02-26 10:40:21 -0500712 ins->objectid = search_start;
713 ins->offset = 0;
Chris Masonfec577f2007-02-26 10:40:21 -0500714 start_found = 0;
Chris Mason5caf2a02007-04-02 11:20:42 -0400715 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
Chris Mason0f70abe2007-02-28 16:46:22 -0500716 if (ret < 0)
717 goto error;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500718
Chris Mason5caf2a02007-04-02 11:20:42 -0400719 if (path->slots[0] > 0)
720 path->slots[0]--;
Chris Mason0579da42007-03-07 16:15:30 -0500721
Chris Masonfec577f2007-02-26 10:40:21 -0500722 while (1) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400723 l = btrfs_buffer_leaf(path->nodes[0]);
724 slot = path->slots[0];
Chris Mason7518a232007-03-12 12:01:18 -0400725 if (slot >= btrfs_header_nritems(&l->header)) {
Chris Masonf2458e12007-04-25 15:52:25 -0400726 if (fill_prealloc) {
727 info->extent_tree_prealloc_nr = 0;
728 total_found = 0;
729 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400730 ret = btrfs_next_leaf(root, path);
Chris Masonfec577f2007-02-26 10:40:21 -0500731 if (ret == 0)
732 continue;
Chris Mason0f70abe2007-02-28 16:46:22 -0500733 if (ret < 0)
734 goto error;
Chris Masonfec577f2007-02-26 10:40:21 -0500735 if (!start_found) {
736 ins->objectid = search_start;
Chris Masonf2458e12007-04-25 15:52:25 -0400737 ins->offset = (u64)-1 - search_start;
Chris Masonfec577f2007-02-26 10:40:21 -0500738 start_found = 1;
739 goto check_pending;
740 }
741 ins->objectid = last_block > search_start ?
742 last_block : search_start;
Chris Masonf2458e12007-04-25 15:52:25 -0400743 ins->offset = (u64)-1 - ins->objectid;
Chris Masonfec577f2007-02-26 10:40:21 -0500744 goto check_pending;
745 }
Chris Masone2fa7222007-03-12 16:22:34 -0400746 btrfs_disk_key_to_cpu(&key, &l->items[slot].key);
Chris Mason9078a3e2007-04-26 16:46:15 -0400747 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY)
748 goto next;
Chris Masone2fa7222007-03-12 16:22:34 -0400749 if (key.objectid >= search_start) {
Chris Masonfec577f2007-02-26 10:40:21 -0500750 if (start_found) {
Chris Mason0579da42007-03-07 16:15:30 -0500751 if (last_block < search_start)
752 last_block = search_start;
Chris Masone2fa7222007-03-12 16:22:34 -0400753 hole_size = key.objectid - last_block;
Chris Mason28b8bb92007-04-27 11:42:05 -0400754 if (hole_size >= num_blocks) {
Chris Masonfec577f2007-02-26 10:40:21 -0500755 ins->objectid = last_block;
Chris Mason037e6392007-03-07 11:50:24 -0500756 ins->offset = hole_size;
Chris Masonfec577f2007-02-26 10:40:21 -0500757 goto check_pending;
758 }
Chris Mason0579da42007-03-07 16:15:30 -0500759 }
Chris Masonfec577f2007-02-26 10:40:21 -0500760 }
Chris Mason0579da42007-03-07 16:15:30 -0500761 start_found = 1;
Chris Masone2fa7222007-03-12 16:22:34 -0400762 last_block = key.objectid + key.offset;
Chris Masonbe744172007-05-06 10:15:01 -0400763 if (last_block >= block_group->key.objectid +
764 block_group->key.offset) {
765 btrfs_release_path(root, path);
766 search_start = block_group->key.objectid +
767 block_group->key.offset * 2;
768 goto new_group;
769 }
Chris Mason9078a3e2007-04-26 16:46:15 -0400770next:
Chris Mason5caf2a02007-04-02 11:20:42 -0400771 path->slots[0]++;
Chris Masonfec577f2007-02-26 10:40:21 -0500772 }
773 // FIXME -ENOSPC
774check_pending:
775 /* we have to make sure we didn't find an extent that has already
776 * been allocated by the map tree or the original allocation
777 */
Chris Mason5caf2a02007-04-02 11:20:42 -0400778 btrfs_release_path(root, path);
Chris Masonfec577f2007-02-26 10:40:21 -0500779 BUG_ON(ins->objectid < search_start);
Chris Mason06a2f9f2007-04-28 08:48:10 -0400780 if (ins->objectid >= btrfs_super_total_blocks(info->disk_super)) {
Chris Masonbe744172007-05-06 10:15:01 -0400781 if (full_scan)
Chris Mason06a2f9f2007-04-28 08:48:10 -0400782 return -ENOSPC;
Chris Masonbe744172007-05-06 10:15:01 -0400783 search_start = orig_search_start;
784 full_scan = 1;
785 goto new_group;
Chris Mason06a2f9f2007-04-28 08:48:10 -0400786 }
Chris Mason037e6392007-03-07 11:50:24 -0500787 for (test_block = ins->objectid;
Chris Masonf2458e12007-04-25 15:52:25 -0400788 test_block < ins->objectid + num_blocks; test_block++) {
789 if (test_radix_bit(&info->pinned_radix, test_block)) {
Chris Mason037e6392007-03-07 11:50:24 -0500790 search_start = test_block + 1;
Chris Masonbe744172007-05-06 10:15:01 -0400791 goto new_group;
Chris Masonfec577f2007-02-26 10:40:21 -0500792 }
793 }
Chris Masonf2458e12007-04-25 15:52:25 -0400794 if (!fill_prealloc && info->extent_tree_insert_nr) {
795 u64 last =
796 info->extent_tree_insert[info->extent_tree_insert_nr - 1];
797 if (ins->objectid + num_blocks >
798 info->extent_tree_insert[0] &&
799 ins->objectid <= last) {
800 search_start = last + 1;
801 WARN_ON(1);
Chris Masonbe744172007-05-06 10:15:01 -0400802 goto new_group;
Chris Masonf2458e12007-04-25 15:52:25 -0400803 }
804 }
805 if (!fill_prealloc && info->extent_tree_prealloc_nr) {
806 u64 first =
807 info->extent_tree_prealloc[info->extent_tree_prealloc_nr - 1];
808 if (ins->objectid + num_blocks > first &&
809 ins->objectid <= info->extent_tree_prealloc[0]) {
810 search_start = info->extent_tree_prealloc[0] + 1;
811 WARN_ON(1);
Chris Masonbe744172007-05-06 10:15:01 -0400812 goto new_group;
Chris Masonf2458e12007-04-25 15:52:25 -0400813 }
814 }
815 if (fill_prealloc) {
816 int nr;
817 test_block = ins->objectid;
818 while(test_block < ins->objectid + ins->offset &&
819 total_found < total_needed) {
820 nr = total_needed - total_found - 1;
821 BUG_ON(nr < 0);
Chris Masoncd1bc462007-04-27 10:08:34 -0400822 info->extent_tree_prealloc[nr] = test_block;
Chris Masonf2458e12007-04-25 15:52:25 -0400823 total_found++;
824 test_block++;
825 }
826 if (total_found < total_needed) {
827 search_start = test_block;
Chris Masonbe744172007-05-06 10:15:01 -0400828 goto new_group;
Chris Masonf2458e12007-04-25 15:52:25 -0400829 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400830 info->extent_tree_prealloc_nr = total_found;
Chris Masonf2458e12007-04-25 15:52:25 -0400831 }
Chris Masonbe744172007-05-06 10:15:01 -0400832 block_group = lookup_block_group(info, ins->objectid);
833 if (block_group) {
Chris Masonbe08c1b2007-05-03 09:06:49 -0400834 block_group->last_alloc = ins->objectid;
835 if (!data)
836 trans->block_group = block_group;
Chris Masoncd1bc462007-04-27 10:08:34 -0400837 }
Chris Mason037e6392007-03-07 11:50:24 -0500838 ins->offset = num_blocks;
Chris Mason5caf2a02007-04-02 11:20:42 -0400839 btrfs_free_path(path);
Chris Masonfec577f2007-02-26 10:40:21 -0500840 return 0;
Chris Masonbe744172007-05-06 10:15:01 -0400841
842new_group:
843 if (search_start >= btrfs_super_total_blocks(info->disk_super)) {
844 search_start = orig_search_start;
845 full_scan = 1;
846 }
847 block_group = lookup_block_group(info, search_start);
848 if (!full_scan)
849 block_group = btrfs_find_block_group(root, block_group,
850 search_start, data);
851 goto check_failed;
852
Chris Mason0f70abe2007-02-28 16:46:22 -0500853error:
Chris Mason5caf2a02007-04-02 11:20:42 -0400854 btrfs_release_path(root, path);
855 btrfs_free_path(path);
Chris Mason0f70abe2007-02-28 16:46:22 -0500856 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -0500857}
Chris Masonfec577f2007-02-26 10:40:21 -0500858/*
Chris Masonfec577f2007-02-26 10:40:21 -0500859 * finds a free extent and does all the dirty work required for allocation
860 * returns the key for the extent through ins, and a tree buffer for
861 * the first block of the extent through buf.
862 *
863 * returns 0 if everything worked, non-zero otherwise.
864 */
Chris Mason4d775672007-04-20 20:23:12 -0400865int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
866 struct btrfs_root *root, u64 owner,
Chris Masonc62a1922007-04-24 12:07:39 -0400867 u64 num_blocks, u64 search_start,
Chris Masonbe08c1b2007-05-03 09:06:49 -0400868 u64 search_end, struct btrfs_key *ins, int data)
Chris Masonfec577f2007-02-26 10:40:21 -0500869{
870 int ret;
871 int pending_ret;
Chris Mason1261ec42007-03-20 20:35:03 -0400872 u64 super_blocks_used;
873 struct btrfs_fs_info *info = root->fs_info;
874 struct btrfs_root *extent_root = info->extent_root;
Chris Mason234b63a2007-03-13 10:46:10 -0400875 struct btrfs_extent_item extent_item;
Chris Masonf2458e12007-04-25 15:52:25 -0400876 struct btrfs_key prealloc_key;
Chris Mason037e6392007-03-07 11:50:24 -0500877
Chris Masoncf27e1e2007-03-13 09:49:06 -0400878 btrfs_set_extent_refs(&extent_item, 1);
Chris Mason4d775672007-04-20 20:23:12 -0400879 btrfs_set_extent_owner(&extent_item, owner);
Chris Masonfec577f2007-02-26 10:40:21 -0500880
Chris Mason037e6392007-03-07 11:50:24 -0500881 if (root == extent_root) {
Chris Masonf2458e12007-04-25 15:52:25 -0400882 int nr;
883 BUG_ON(info->extent_tree_prealloc_nr == 0);
Chris Mason037e6392007-03-07 11:50:24 -0500884 BUG_ON(num_blocks != 1);
Chris Mason037e6392007-03-07 11:50:24 -0500885 ins->offset = 1;
Chris Masonf2458e12007-04-25 15:52:25 -0400886 info->extent_tree_prealloc_nr--;
887 nr = info->extent_tree_prealloc_nr;
888 ins->objectid = info->extent_tree_prealloc[nr];
889 info->extent_tree_insert[info->extent_tree_insert_nr++] =
890 ins->objectid;
Chris Mason9078a3e2007-04-26 16:46:15 -0400891 ret = update_block_group(trans, root,
892 ins->objectid, ins->offset, 1);
893 BUG_ON(ret);
Chris Masonfec577f2007-02-26 10:40:21 -0500894 return 0;
895 }
Chris Masonf2458e12007-04-25 15:52:25 -0400896 /* do the real allocation */
Chris Masone089f052007-03-16 16:20:31 -0400897 ret = find_free_extent(trans, root, num_blocks, search_start,
Chris Masonbe08c1b2007-05-03 09:06:49 -0400898 search_end, ins, data);
Chris Mason037e6392007-03-07 11:50:24 -0500899 if (ret)
900 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -0500901
Chris Masonf2458e12007-04-25 15:52:25 -0400902 /* then do prealloc for the extent tree */
903 ret = find_free_extent(trans, root, 0, ins->objectid + ins->offset,
Chris Masonbe08c1b2007-05-03 09:06:49 -0400904 search_end, &prealloc_key, 0);
Chris Masonf2458e12007-04-25 15:52:25 -0400905 if (ret)
906 return ret;
907
Chris Mason1261ec42007-03-20 20:35:03 -0400908 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
909 btrfs_set_super_blocks_used(info->disk_super, super_blocks_used +
910 num_blocks);
Chris Masone089f052007-03-16 16:20:31 -0400911 ret = btrfs_insert_item(trans, extent_root, ins, &extent_item,
912 sizeof(extent_item));
Chris Mason037e6392007-03-07 11:50:24 -0500913
Chris Masone089f052007-03-16 16:20:31 -0400914 finish_current_insert(trans, extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -0400915 pending_ret = del_pending_extents(trans, extent_root);
Chris Mason037e6392007-03-07 11:50:24 -0500916 if (ret)
917 return ret;
918 if (pending_ret)
919 return pending_ret;
Chris Mason9078a3e2007-04-26 16:46:15 -0400920 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1);
Chris Mason037e6392007-03-07 11:50:24 -0500921 return 0;
Chris Masonfec577f2007-02-26 10:40:21 -0500922}
923
924/*
925 * helper function to allocate a block for a given tree
926 * returns the tree buffer or NULL.
927 */
Chris Masone20d96d2007-03-22 12:13:20 -0400928struct buffer_head *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Chris Mason31f3c992007-04-30 15:25:45 -0400929 struct btrfs_root *root, u64 hint)
Chris Masonfec577f2007-02-26 10:40:21 -0500930{
Chris Masone2fa7222007-03-12 16:22:34 -0400931 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -0500932 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -0400933 struct buffer_head *buf;
Chris Masonfec577f2007-02-26 10:40:21 -0500934
Chris Mason4d775672007-04-20 20:23:12 -0400935 ret = btrfs_alloc_extent(trans, root, root->root_key.objectid,
Chris Masonbe744172007-05-06 10:15:01 -0400936 1, hint, (unsigned long)-1, &ins, 0);
Chris Masonfec577f2007-02-26 10:40:21 -0500937 if (ret) {
938 BUG();
939 return NULL;
940 }
Chris Mason9078a3e2007-04-26 16:46:15 -0400941 BUG_ON(ret);
Chris Masond98237b2007-03-28 13:57:48 -0400942 buf = btrfs_find_create_tree_block(root, ins.objectid);
Chris Masondf2ce342007-03-23 11:00:45 -0400943 set_buffer_uptodate(buf);
Chris Mason090d1872007-05-01 08:53:32 -0400944 set_buffer_checked(buf);
Chris Mason7c4452b2007-04-28 09:29:35 -0400945 set_radix_bit(&trans->transaction->dirty_pages, buf->b_page->index);
Chris Masonfec577f2007-02-26 10:40:21 -0500946 return buf;
947}
Chris Masona28ec192007-03-06 20:08:01 -0500948
Chris Mason6407bf62007-03-27 06:33:00 -0400949static int drop_leaf_ref(struct btrfs_trans_handle *trans,
950 struct btrfs_root *root, struct buffer_head *cur)
951{
952 struct btrfs_disk_key *key;
953 struct btrfs_leaf *leaf;
954 struct btrfs_file_extent_item *fi;
955 int i;
956 int nritems;
957 int ret;
958
959 BUG_ON(!btrfs_is_leaf(btrfs_buffer_node(cur)));
960 leaf = btrfs_buffer_leaf(cur);
961 nritems = btrfs_header_nritems(&leaf->header);
962 for (i = 0; i < nritems; i++) {
963 key = &leaf->items[i].key;
964 if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY)
965 continue;
966 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
Chris Mason236454d2007-04-19 13:37:44 -0400967 if (btrfs_file_extent_type(fi) == BTRFS_FILE_EXTENT_INLINE)
968 continue;
Chris Mason6407bf62007-03-27 06:33:00 -0400969 /*
970 * FIXME make sure to insert a trans record that
971 * repeats the snapshot del on crash
972 */
973 ret = btrfs_free_extent(trans, root,
974 btrfs_file_extent_disk_blocknr(fi),
975 btrfs_file_extent_disk_num_blocks(fi),
976 0);
977 BUG_ON(ret);
978 }
979 return 0;
980}
981
Chris Mason9aca1d52007-03-13 11:09:37 -0400982/*
983 * helper function for drop_snapshot, this walks down the tree dropping ref
984 * counts as it goes.
985 */
Chris Masone089f052007-03-16 16:20:31 -0400986static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
987 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -0500988{
Chris Masone20d96d2007-03-22 12:13:20 -0400989 struct buffer_head *next;
990 struct buffer_head *cur;
Chris Mason20524f02007-03-10 06:35:47 -0500991 u64 blocknr;
992 int ret;
993 u32 refs;
994
Chris Mason5caf2a02007-04-02 11:20:42 -0400995 WARN_ON(*level < 0);
996 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Masonb18c6682007-04-17 13:26:50 -0400997 ret = lookup_extent_ref(trans, root, bh_blocknr(path->nodes[*level]),
Chris Mason6407bf62007-03-27 06:33:00 -0400998 1, &refs);
Chris Mason20524f02007-03-10 06:35:47 -0500999 BUG_ON(ret);
1000 if (refs > 1)
1001 goto out;
Chris Mason9aca1d52007-03-13 11:09:37 -04001002 /*
1003 * walk down to the last node level and free all the leaves
1004 */
Chris Mason6407bf62007-03-27 06:33:00 -04001005 while(*level >= 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001006 WARN_ON(*level < 0);
1007 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason20524f02007-03-10 06:35:47 -05001008 cur = path->nodes[*level];
Chris Mason2c90e5d2007-04-02 10:50:19 -04001009 if (btrfs_header_level(btrfs_buffer_header(cur)) != *level)
1010 WARN_ON(1);
Chris Mason7518a232007-03-12 12:01:18 -04001011 if (path->slots[*level] >=
Chris Masone20d96d2007-03-22 12:13:20 -04001012 btrfs_header_nritems(btrfs_buffer_header(cur)))
Chris Mason20524f02007-03-10 06:35:47 -05001013 break;
Chris Mason6407bf62007-03-27 06:33:00 -04001014 if (*level == 0) {
1015 ret = drop_leaf_ref(trans, root, cur);
1016 BUG_ON(ret);
1017 break;
1018 }
Chris Masone20d96d2007-03-22 12:13:20 -04001019 blocknr = btrfs_node_blockptr(btrfs_buffer_node(cur),
1020 path->slots[*level]);
Chris Masonb18c6682007-04-17 13:26:50 -04001021 ret = lookup_extent_ref(trans, root, blocknr, 1, &refs);
Chris Mason6407bf62007-03-27 06:33:00 -04001022 BUG_ON(ret);
1023 if (refs != 1) {
Chris Mason20524f02007-03-10 06:35:47 -05001024 path->slots[*level]++;
Chris Masone089f052007-03-16 16:20:31 -04001025 ret = btrfs_free_extent(trans, root, blocknr, 1, 1);
Chris Mason20524f02007-03-10 06:35:47 -05001026 BUG_ON(ret);
1027 continue;
1028 }
Chris Mason20524f02007-03-10 06:35:47 -05001029 next = read_tree_block(root, blocknr);
Chris Mason5caf2a02007-04-02 11:20:42 -04001030 WARN_ON(*level <= 0);
Chris Mason83e15a22007-03-12 09:03:27 -04001031 if (path->nodes[*level-1])
Chris Mason234b63a2007-03-13 10:46:10 -04001032 btrfs_block_release(root, path->nodes[*level-1]);
Chris Mason20524f02007-03-10 06:35:47 -05001033 path->nodes[*level-1] = next;
Chris Masone20d96d2007-03-22 12:13:20 -04001034 *level = btrfs_header_level(btrfs_buffer_header(next));
Chris Mason20524f02007-03-10 06:35:47 -05001035 path->slots[*level] = 0;
1036 }
1037out:
Chris Mason5caf2a02007-04-02 11:20:42 -04001038 WARN_ON(*level < 0);
1039 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason6407bf62007-03-27 06:33:00 -04001040 ret = btrfs_free_extent(trans, root,
Chris Mason7eccb902007-04-11 15:53:25 -04001041 bh_blocknr(path->nodes[*level]), 1, 1);
Chris Mason234b63a2007-03-13 10:46:10 -04001042 btrfs_block_release(root, path->nodes[*level]);
Chris Mason20524f02007-03-10 06:35:47 -05001043 path->nodes[*level] = NULL;
1044 *level += 1;
1045 BUG_ON(ret);
1046 return 0;
1047}
1048
Chris Mason9aca1d52007-03-13 11:09:37 -04001049/*
1050 * helper for dropping snapshots. This walks back up the tree in the path
1051 * to find the first node higher up where we haven't yet gone through
1052 * all the slots
1053 */
Chris Masone089f052007-03-16 16:20:31 -04001054static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root
1055 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05001056{
1057 int i;
1058 int slot;
1059 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -04001060 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Mason20524f02007-03-10 06:35:47 -05001061 slot = path->slots[i];
Chris Masone20d96d2007-03-22 12:13:20 -04001062 if (slot < btrfs_header_nritems(
1063 btrfs_buffer_header(path->nodes[i])) - 1) {
Chris Mason20524f02007-03-10 06:35:47 -05001064 path->slots[i]++;
1065 *level = i;
1066 return 0;
1067 } else {
Chris Masone089f052007-03-16 16:20:31 -04001068 ret = btrfs_free_extent(trans, root,
Chris Mason7eccb902007-04-11 15:53:25 -04001069 bh_blocknr(path->nodes[*level]),
Chris Masone089f052007-03-16 16:20:31 -04001070 1, 1);
Chris Mason6407bf62007-03-27 06:33:00 -04001071 BUG_ON(ret);
Chris Mason234b63a2007-03-13 10:46:10 -04001072 btrfs_block_release(root, path->nodes[*level]);
Chris Mason83e15a22007-03-12 09:03:27 -04001073 path->nodes[*level] = NULL;
Chris Mason20524f02007-03-10 06:35:47 -05001074 *level = i + 1;
Chris Mason20524f02007-03-10 06:35:47 -05001075 }
1076 }
1077 return 1;
1078}
1079
Chris Mason9aca1d52007-03-13 11:09:37 -04001080/*
1081 * drop the reference count on the tree rooted at 'snap'. This traverses
1082 * the tree freeing any blocks that have a ref count of zero after being
1083 * decremented.
1084 */
Chris Masone089f052007-03-16 16:20:31 -04001085int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masone20d96d2007-03-22 12:13:20 -04001086 *root, struct buffer_head *snap)
Chris Mason20524f02007-03-10 06:35:47 -05001087{
Chris Mason3768f362007-03-13 16:47:54 -04001088 int ret = 0;
Chris Mason9aca1d52007-03-13 11:09:37 -04001089 int wret;
Chris Mason20524f02007-03-10 06:35:47 -05001090 int level;
Chris Mason5caf2a02007-04-02 11:20:42 -04001091 struct btrfs_path *path;
Chris Mason20524f02007-03-10 06:35:47 -05001092 int i;
1093 int orig_level;
1094
Chris Mason5caf2a02007-04-02 11:20:42 -04001095 path = btrfs_alloc_path();
1096 BUG_ON(!path);
1097 btrfs_init_path(path);
Chris Mason20524f02007-03-10 06:35:47 -05001098
Chris Masone20d96d2007-03-22 12:13:20 -04001099 level = btrfs_header_level(btrfs_buffer_header(snap));
Chris Mason20524f02007-03-10 06:35:47 -05001100 orig_level = level;
Chris Mason5caf2a02007-04-02 11:20:42 -04001101 path->nodes[level] = snap;
1102 path->slots[level] = 0;
Chris Mason20524f02007-03-10 06:35:47 -05001103 while(1) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001104 wret = walk_down_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04001105 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05001106 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04001107 if (wret < 0)
1108 ret = wret;
1109
Chris Mason5caf2a02007-04-02 11:20:42 -04001110 wret = walk_up_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04001111 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05001112 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04001113 if (wret < 0)
1114 ret = wret;
Chris Mason35b7e472007-05-02 15:53:43 -04001115 btrfs_btree_balance_dirty(root);
Chris Mason20524f02007-03-10 06:35:47 -05001116 }
Chris Mason83e15a22007-03-12 09:03:27 -04001117 for (i = 0; i <= orig_level; i++) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001118 if (path->nodes[i]) {
1119 btrfs_block_release(root, path->nodes[i]);
Chris Mason83e15a22007-03-12 09:03:27 -04001120 }
Chris Mason20524f02007-03-10 06:35:47 -05001121 }
Chris Mason5caf2a02007-04-02 11:20:42 -04001122 btrfs_free_path(path);
Chris Mason9aca1d52007-03-13 11:09:37 -04001123 return ret;
Chris Mason20524f02007-03-10 06:35:47 -05001124}
Chris Mason9078a3e2007-04-26 16:46:15 -04001125
Chris Masonbe744172007-05-06 10:15:01 -04001126static int free_block_group_radix(struct radix_tree_root *radix)
Chris Mason9078a3e2007-04-26 16:46:15 -04001127{
1128 int ret;
1129 struct btrfs_block_group_cache *cache[8];
1130 int i;
1131
1132 while(1) {
Chris Masonbe744172007-05-06 10:15:01 -04001133 ret = radix_tree_gang_lookup(radix, (void **)cache, 0,
Chris Mason9078a3e2007-04-26 16:46:15 -04001134 ARRAY_SIZE(cache));
1135 if (!ret)
1136 break;
1137 for (i = 0; i < ret; i++) {
Chris Masonbe744172007-05-06 10:15:01 -04001138 radix_tree_delete(radix, cache[i]->key.objectid +
Chris Mason9078a3e2007-04-26 16:46:15 -04001139 cache[i]->key.offset - 1);
1140 kfree(cache[i]);
1141 }
1142 }
1143 return 0;
1144}
1145
Chris Masonbe744172007-05-06 10:15:01 -04001146int btrfs_free_block_groups(struct btrfs_fs_info *info)
1147{
1148 int ret;
1149 int ret2;
1150
1151 ret = free_block_group_radix(&info->block_group_radix);
1152 ret2 = free_block_group_radix(&info->block_group_data_radix);
1153 if (ret)
1154 return ret;
1155 if (ret2)
1156 return ret2;
1157 return 0;
1158}
1159
Chris Mason9078a3e2007-04-26 16:46:15 -04001160int btrfs_read_block_groups(struct btrfs_root *root)
1161{
1162 struct btrfs_path *path;
1163 int ret;
1164 int err = 0;
1165 struct btrfs_block_group_item *bi;
1166 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04001167 struct btrfs_fs_info *info = root->fs_info;
1168 struct radix_tree_root *radix;
Chris Mason9078a3e2007-04-26 16:46:15 -04001169 struct btrfs_key key;
1170 struct btrfs_key found_key;
1171 struct btrfs_leaf *leaf;
1172 u64 group_size_blocks = BTRFS_BLOCK_GROUP_SIZE / root->blocksize;
Chris Mason31f3c992007-04-30 15:25:45 -04001173 u64 used;
Chris Masonbe744172007-05-06 10:15:01 -04001174 u64 nr = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04001175
Chris Masonbe744172007-05-06 10:15:01 -04001176 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04001177 key.objectid = 0;
1178 key.offset = group_size_blocks;
1179 key.flags = 0;
1180 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
1181
1182 path = btrfs_alloc_path();
1183 if (!path)
1184 return -ENOMEM;
1185
1186 while(1) {
Chris Masonbe744172007-05-06 10:15:01 -04001187 ret = btrfs_search_slot(NULL, info->extent_root,
Chris Mason9078a3e2007-04-26 16:46:15 -04001188 &key, path, 0, 0);
1189 if (ret != 0) {
1190 err = ret;
1191 break;
1192 }
1193 leaf = btrfs_buffer_leaf(path->nodes[0]);
1194 btrfs_disk_key_to_cpu(&found_key,
1195 &leaf->items[path->slots[0]].key);
1196 cache = kmalloc(sizeof(*cache), GFP_NOFS);
1197 if (!cache) {
1198 err = -1;
1199 break;
1200 }
1201 bi = btrfs_item_ptr(leaf, path->slots[0],
1202 struct btrfs_block_group_item);
1203 memcpy(&cache->item, bi, sizeof(*bi));
1204 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Mason31f3c992007-04-30 15:25:45 -04001205 cache->last_alloc = cache->key.objectid;
1206 cache->first_free = cache->key.objectid;
Chris Masonbe744172007-05-06 10:15:01 -04001207 cache->pinned = 0;
1208 cache->data = (nr & 1);
Chris Mason9078a3e2007-04-26 16:46:15 -04001209 key.objectid = found_key.objectid + found_key.offset;
1210 btrfs_release_path(root, path);
Chris Masonbe744172007-05-06 10:15:01 -04001211 if (nr & 1)
1212 radix = &info->block_group_data_radix;
1213 else
1214 radix = &info->block_group_radix;
1215 ret = radix_tree_insert(radix, found_key.objectid +
Chris Mason9078a3e2007-04-26 16:46:15 -04001216 found_key.offset - 1,
1217 (void *)cache);
1218 BUG_ON(ret);
Chris Mason31f3c992007-04-30 15:25:45 -04001219 used = btrfs_block_group_used(bi);
Chris Masonbe744172007-05-06 10:15:01 -04001220 if (used < (key.offset * 8) / 10) {
1221 radix_tree_tag_set(radix, found_key.objectid +
Chris Mason31f3c992007-04-30 15:25:45 -04001222 found_key.offset - 1,
1223 BTRFS_BLOCK_GROUP_AVAIL);
1224 }
Chris Mason9078a3e2007-04-26 16:46:15 -04001225 if (key.objectid >=
Chris Masonbe744172007-05-06 10:15:01 -04001226 btrfs_super_total_blocks(info->disk_super))
Chris Mason9078a3e2007-04-26 16:46:15 -04001227 break;
Chris Masonbe744172007-05-06 10:15:01 -04001228 nr++;
Chris Mason9078a3e2007-04-26 16:46:15 -04001229 }
1230
1231 btrfs_free_path(path);
1232 return 0;
1233}