blob: 0e20d1c42fca59fbb28a8bf13cf5980c33368d93 [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
9 search_end, struct btrfs_key *ins);
10static 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 Mason31f3c992007-04-30 15:25:45 -040015struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
16 struct btrfs_block_group_cache
17 *hint, int data)
Chris Masoncd1bc462007-04-27 10:08:34 -040018{
19 struct btrfs_block_group_cache *cache[8];
Chris Mason31f3c992007-04-30 15:25:45 -040020 struct btrfs_block_group_cache *found_group = NULL;
Chris Masoncd1bc462007-04-27 10:08:34 -040021 struct btrfs_fs_info *info = root->fs_info;
22 u64 used;
Chris Mason31f3c992007-04-30 15:25:45 -040023 u64 last = 0;
24 u64 hint_last;
Chris Masoncd1bc462007-04-27 10:08:34 -040025 int i;
26 int ret;
Chris Mason31f3c992007-04-30 15:25:45 -040027 int full_search = 0;
28 if (hint) {
29 used = btrfs_block_group_used(&hint->item);
30 if (used < (hint->key.offset * 2) / 3) {
31 return hint;
32 }
33 radix_tree_tag_clear(&info->block_group_radix,
34 hint->key.objectid + hint->key.offset - 1,
35 BTRFS_BLOCK_GROUP_AVAIL);
36 last = hint->key.objectid + hint->key.offset;
37 hint_last = last;
38 } else {
39 hint_last = 0;
40 last = 0;
41 }
Chris Masoncd1bc462007-04-27 10:08:34 -040042 while(1) {
43 ret = radix_tree_gang_lookup_tag(&info->block_group_radix,
44 (void **)cache,
45 last, ARRAY_SIZE(cache),
Chris Mason31f3c992007-04-30 15:25:45 -040046 BTRFS_BLOCK_GROUP_AVAIL);
Chris Masoncd1bc462007-04-27 10:08:34 -040047 if (!ret)
48 break;
49 for (i = 0; i < ret; i++) {
50 used = btrfs_block_group_used(&cache[i]->item);
Chris Mason31f3c992007-04-30 15:25:45 -040051 if (used < (cache[i]->key.offset * 2) / 3) {
Chris Masoncd1bc462007-04-27 10:08:34 -040052 info->block_group_cache = cache[i];
Chris Mason31f3c992007-04-30 15:25:45 -040053 found_group = cache[i];
54 goto found;
Chris Masoncd1bc462007-04-27 10:08:34 -040055 }
Chris Mason31f3c992007-04-30 15:25:45 -040056 radix_tree_tag_clear(&info->block_group_radix,
57 cache[i]->key.objectid +
58 cache[i]->key.offset - 1,
59 BTRFS_BLOCK_GROUP_AVAIL);
Chris Masoncd1bc462007-04-27 10:08:34 -040060 last = cache[i]->key.objectid +
Chris Mason31f3c992007-04-30 15:25:45 -040061 cache[i]->key.offset;
Chris Masoncd1bc462007-04-27 10:08:34 -040062 }
63 }
Chris Mason31f3c992007-04-30 15:25:45 -040064 last = hint_last;
65again:
Chris Masoncd1bc462007-04-27 10:08:34 -040066 while(1) {
67 ret = radix_tree_gang_lookup(&info->block_group_radix,
68 (void **)cache,
69 last, ARRAY_SIZE(cache));
70 if (!ret)
71 break;
72 for (i = 0; i < ret; i++) {
73 used = btrfs_block_group_used(&cache[i]->item);
Chris Mason31f3c992007-04-30 15:25:45 -040074 if (used < cache[i]->key.offset) {
Chris Masoncd1bc462007-04-27 10:08:34 -040075 info->block_group_cache = cache[i];
Chris Mason31f3c992007-04-30 15:25:45 -040076 found_group = cache[i];
77 goto found;
Chris Masoncd1bc462007-04-27 10:08:34 -040078 }
Chris Mason31f3c992007-04-30 15:25:45 -040079 radix_tree_tag_clear(&info->block_group_radix,
80 cache[i]->key.objectid +
81 cache[i]->key.offset - 1,
82 BTRFS_BLOCK_GROUP_AVAIL);
Chris Masoncd1bc462007-04-27 10:08:34 -040083 last = cache[i]->key.objectid +
Chris Mason31f3c992007-04-30 15:25:45 -040084 cache[i]->key.offset;
Chris Masoncd1bc462007-04-27 10:08:34 -040085 }
86 }
87 info->block_group_cache = NULL;
Chris Mason31f3c992007-04-30 15:25:45 -040088 if (!full_search) {
89 last = 0;
90 full_search = 1;
91 goto again;
92 }
93found:
94 if (!found_group) {
95 ret = radix_tree_gang_lookup(&info->block_group_radix,
96 (void **)&found_group, 0, 1);
97 BUG_ON(ret != 1);
98 }
99 return found_group;
Chris Masoncd1bc462007-04-27 10:08:34 -0400100}
101
Chris Masonb18c6682007-04-17 13:26:50 -0400102int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
103 struct btrfs_root *root,
104 u64 blocknr, u64 num_blocks)
Chris Mason02217ed2007-03-02 16:08:05 -0500105{
Chris Mason5caf2a02007-04-02 11:20:42 -0400106 struct btrfs_path *path;
Chris Mason02217ed2007-03-02 16:08:05 -0500107 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400108 struct btrfs_key key;
Chris Mason234b63a2007-03-13 10:46:10 -0400109 struct btrfs_leaf *l;
110 struct btrfs_extent_item *item;
Chris Masone2fa7222007-03-12 16:22:34 -0400111 struct btrfs_key ins;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400112 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500113
Chris Mason9f5fae22007-03-20 14:38:32 -0400114 find_free_extent(trans, root->fs_info->extent_root, 0, 0, (u64)-1,
115 &ins);
Chris Mason5caf2a02007-04-02 11:20:42 -0400116 path = btrfs_alloc_path();
117 BUG_ON(!path);
118 btrfs_init_path(path);
Chris Mason02217ed2007-03-02 16:08:05 -0500119 key.objectid = blocknr;
120 key.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -0400121 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Mason6407bf62007-03-27 06:33:00 -0400122 key.offset = num_blocks;
Chris Mason5caf2a02007-04-02 11:20:42 -0400123 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -0400124 0, 1);
Chris Masona429e512007-04-18 16:15:28 -0400125 if (ret != 0) {
126printk("can't find block %Lu %Lu\n", blocknr, num_blocks);
Chris Masona28ec192007-03-06 20:08:01 -0500127 BUG();
Chris Masona429e512007-04-18 16:15:28 -0400128 }
Chris Mason02217ed2007-03-02 16:08:05 -0500129 BUG_ON(ret != 0);
Chris Mason5caf2a02007-04-02 11:20:42 -0400130 l = btrfs_buffer_leaf(path->nodes[0]);
131 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400132 refs = btrfs_extent_refs(item);
133 btrfs_set_extent_refs(item, refs + 1);
Chris Mason5caf2a02007-04-02 11:20:42 -0400134 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masona28ec192007-03-06 20:08:01 -0500135
Chris Mason5caf2a02007-04-02 11:20:42 -0400136 btrfs_release_path(root->fs_info->extent_root, path);
137 btrfs_free_path(path);
Chris Mason9f5fae22007-03-20 14:38:32 -0400138 finish_current_insert(trans, root->fs_info->extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -0400139 del_pending_extents(trans, root->fs_info->extent_root);
Chris Mason02217ed2007-03-02 16:08:05 -0500140 return 0;
141}
142
Chris Masonb18c6682007-04-17 13:26:50 -0400143static int lookup_extent_ref(struct btrfs_trans_handle *trans,
144 struct btrfs_root *root, u64 blocknr,
145 u64 num_blocks, u32 *refs)
Chris Masona28ec192007-03-06 20:08:01 -0500146{
Chris Mason5caf2a02007-04-02 11:20:42 -0400147 struct btrfs_path *path;
Chris Masona28ec192007-03-06 20:08:01 -0500148 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400149 struct btrfs_key key;
Chris Mason234b63a2007-03-13 10:46:10 -0400150 struct btrfs_leaf *l;
151 struct btrfs_extent_item *item;
Chris Mason5caf2a02007-04-02 11:20:42 -0400152
153 path = btrfs_alloc_path();
154 btrfs_init_path(path);
Chris Masona28ec192007-03-06 20:08:01 -0500155 key.objectid = blocknr;
Chris Mason6407bf62007-03-27 06:33:00 -0400156 key.offset = num_blocks;
Chris Mason62e27492007-03-15 12:56:47 -0400157 key.flags = 0;
158 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Mason5caf2a02007-04-02 11:20:42 -0400159 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -0400160 0, 0);
Chris Masona28ec192007-03-06 20:08:01 -0500161 if (ret != 0)
162 BUG();
Chris Mason5caf2a02007-04-02 11:20:42 -0400163 l = btrfs_buffer_leaf(path->nodes[0]);
164 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400165 *refs = btrfs_extent_refs(item);
Chris Mason5caf2a02007-04-02 11:20:42 -0400166 btrfs_release_path(root->fs_info->extent_root, path);
167 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -0500168 return 0;
169}
170
Chris Masonc5739bb2007-04-10 09:27:04 -0400171int btrfs_inc_root_ref(struct btrfs_trans_handle *trans,
172 struct btrfs_root *root)
173{
Chris Masonb18c6682007-04-17 13:26:50 -0400174 return btrfs_inc_extent_ref(trans, root, bh_blocknr(root->node), 1);
Chris Masonc5739bb2007-04-10 09:27:04 -0400175}
176
Chris Masone089f052007-03-16 16:20:31 -0400177int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Masone20d96d2007-03-22 12:13:20 -0400178 struct buffer_head *buf)
Chris Mason02217ed2007-03-02 16:08:05 -0500179{
180 u64 blocknr;
Chris Masone20d96d2007-03-22 12:13:20 -0400181 struct btrfs_node *buf_node;
Chris Mason6407bf62007-03-27 06:33:00 -0400182 struct btrfs_leaf *buf_leaf;
183 struct btrfs_disk_key *key;
184 struct btrfs_file_extent_item *fi;
Chris Mason02217ed2007-03-02 16:08:05 -0500185 int i;
Chris Mason6407bf62007-03-27 06:33:00 -0400186 int leaf;
187 int ret;
Chris Masona28ec192007-03-06 20:08:01 -0500188
Chris Mason3768f362007-03-13 16:47:54 -0400189 if (!root->ref_cows)
Chris Masona28ec192007-03-06 20:08:01 -0500190 return 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400191 buf_node = btrfs_buffer_node(buf);
Chris Mason6407bf62007-03-27 06:33:00 -0400192 leaf = btrfs_is_leaf(buf_node);
193 buf_leaf = btrfs_buffer_leaf(buf);
Chris Masone20d96d2007-03-22 12:13:20 -0400194 for (i = 0; i < btrfs_header_nritems(&buf_node->header); i++) {
Chris Mason6407bf62007-03-27 06:33:00 -0400195 if (leaf) {
196 key = &buf_leaf->items[i].key;
197 if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY)
198 continue;
199 fi = btrfs_item_ptr(buf_leaf, i,
200 struct btrfs_file_extent_item);
Chris Mason236454df2007-04-19 13:37:44 -0400201 if (btrfs_file_extent_type(fi) ==
202 BTRFS_FILE_EXTENT_INLINE)
203 continue;
Chris Masonb18c6682007-04-17 13:26:50 -0400204 ret = btrfs_inc_extent_ref(trans, root,
Chris Mason6407bf62007-03-27 06:33:00 -0400205 btrfs_file_extent_disk_blocknr(fi),
206 btrfs_file_extent_disk_num_blocks(fi));
207 BUG_ON(ret);
208 } else {
209 blocknr = btrfs_node_blockptr(buf_node, i);
Chris Masonb18c6682007-04-17 13:26:50 -0400210 ret = btrfs_inc_extent_ref(trans, root, blocknr, 1);
Chris Mason6407bf62007-03-27 06:33:00 -0400211 BUG_ON(ret);
212 }
Chris Mason02217ed2007-03-02 16:08:05 -0500213 }
214 return 0;
215}
216
Chris Mason9078a3e2007-04-26 16:46:15 -0400217static int write_one_cache_group(struct btrfs_trans_handle *trans,
218 struct btrfs_root *root,
219 struct btrfs_path *path,
220 struct btrfs_block_group_cache *cache)
221{
222 int ret;
223 int pending_ret;
224 struct btrfs_root *extent_root = root->fs_info->extent_root;
225 struct btrfs_block_group_item *bi;
226 struct btrfs_key ins;
227
228 find_free_extent(trans, extent_root, 0, 0, (u64)-1, &ins);
229 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
230 BUG_ON(ret);
231 bi = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
232 struct btrfs_block_group_item);
233 memcpy(bi, &cache->item, sizeof(*bi));
234 mark_buffer_dirty(path->nodes[0]);
235 btrfs_release_path(extent_root, path);
236
237 finish_current_insert(trans, extent_root);
238 pending_ret = del_pending_extents(trans, extent_root);
239 if (ret)
240 return ret;
241 if (pending_ret)
242 return pending_ret;
243 return 0;
244
245}
246
247int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
248 struct btrfs_root *root)
249{
250 struct btrfs_block_group_cache *cache[8];
251 int ret;
252 int err = 0;
253 int werr = 0;
254 struct radix_tree_root *radix = &root->fs_info->block_group_radix;
255 int i;
256 struct btrfs_path *path;
257
258 path = btrfs_alloc_path();
259 if (!path)
260 return -ENOMEM;
261
262 while(1) {
263 ret = radix_tree_gang_lookup_tag(radix, (void **)cache,
264 0, ARRAY_SIZE(cache),
265 BTRFS_BLOCK_GROUP_DIRTY);
266 if (!ret)
267 break;
268 for (i = 0; i < ret; i++) {
269 radix_tree_tag_clear(radix, cache[i]->key.objectid +
270 cache[i]->key.offset - 1,
271 BTRFS_BLOCK_GROUP_DIRTY);
272 err = write_one_cache_group(trans, root,
273 path, cache[i]);
274 if (err)
275 werr = err;
Chris Mason31f3c992007-04-30 15:25:45 -0400276 cache[i]->last_alloc = cache[i]->first_free;
Chris Mason9078a3e2007-04-26 16:46:15 -0400277 }
278 }
279 btrfs_free_path(path);
280 return werr;
281}
282
283static int update_block_group(struct btrfs_trans_handle *trans,
284 struct btrfs_root *root,
285 u64 blocknr, u64 num, int alloc)
286{
287 struct btrfs_block_group_cache *cache;
288 struct btrfs_fs_info *info = root->fs_info;
289 u64 total = num;
290 u64 old_val;
291 u64 block_in_group;
292 int ret;
293 while(total) {
294 ret = radix_tree_gang_lookup(&info->block_group_radix,
295 (void **)&cache, blocknr, 1);
Chris Masoncd1bc462007-04-27 10:08:34 -0400296 if (!ret) {
297 printk(KERN_CRIT "blocknr %Lu lookup failed\n",
298 blocknr);
Chris Mason9078a3e2007-04-26 16:46:15 -0400299 return -1;
Chris Masoncd1bc462007-04-27 10:08:34 -0400300 }
Chris Mason9078a3e2007-04-26 16:46:15 -0400301 block_in_group = blocknr - cache->key.objectid;
302 WARN_ON(block_in_group > cache->key.offset);
303 radix_tree_tag_set(&info->block_group_radix,
304 cache->key.objectid + cache->key.offset - 1,
305 BTRFS_BLOCK_GROUP_DIRTY);
306
307 old_val = btrfs_block_group_used(&cache->item);
308 num = min(total, cache->key.offset - block_in_group);
309 total -= num;
310 blocknr += num;
Chris Masoncd1bc462007-04-27 10:08:34 -0400311 if (alloc) {
Chris Mason9078a3e2007-04-26 16:46:15 -0400312 old_val += num;
Chris Masoncd1bc462007-04-27 10:08:34 -0400313 if (blocknr > cache->last_alloc)
314 cache->last_alloc = blocknr;
315 } else {
Chris Mason9078a3e2007-04-26 16:46:15 -0400316 old_val -= num;
Chris Masoncd1bc462007-04-27 10:08:34 -0400317 if (blocknr < cache->first_free)
318 cache->first_free = blocknr;
319 }
Chris Mason9078a3e2007-04-26 16:46:15 -0400320 btrfs_set_block_group_used(&cache->item, old_val);
321 }
322 return 0;
323}
324
Chris Masone089f052007-03-16 16:20:31 -0400325int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, struct
326 btrfs_root *root)
Chris Masona28ec192007-03-06 20:08:01 -0500327{
Chris Mason8ef97622007-03-26 10:15:30 -0400328 unsigned long gang[8];
Chris Mason88fd1462007-03-16 08:56:18 -0400329 u64 first = 0;
Chris Masona28ec192007-03-06 20:08:01 -0500330 int ret;
331 int i;
Chris Mason8ef97622007-03-26 10:15:30 -0400332 struct radix_tree_root *pinned_radix = &root->fs_info->pinned_radix;
Chris Masona28ec192007-03-06 20:08:01 -0500333
334 while(1) {
Chris Mason8ef97622007-03-26 10:15:30 -0400335 ret = find_first_radix_bit(pinned_radix, gang,
336 ARRAY_SIZE(gang));
Chris Masona28ec192007-03-06 20:08:01 -0500337 if (!ret)
338 break;
Chris Mason88fd1462007-03-16 08:56:18 -0400339 if (!first)
Chris Mason8ef97622007-03-26 10:15:30 -0400340 first = gang[0];
Chris Mason0579da42007-03-07 16:15:30 -0500341 for (i = 0; i < ret; i++) {
Chris Mason8ef97622007-03-26 10:15:30 -0400342 clear_radix_bit(pinned_radix, gang[i]);
Chris Mason0579da42007-03-07 16:15:30 -0500343 }
Chris Masona28ec192007-03-06 20:08:01 -0500344 }
345 return 0;
346}
347
Chris Masone089f052007-03-16 16:20:31 -0400348static int finish_current_insert(struct btrfs_trans_handle *trans, struct
349 btrfs_root *extent_root)
Chris Mason037e6392007-03-07 11:50:24 -0500350{
Chris Masone2fa7222007-03-12 16:22:34 -0400351 struct btrfs_key ins;
Chris Mason234b63a2007-03-13 10:46:10 -0400352 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -0500353 int i;
354 int ret;
Chris Mason1261ec42007-03-20 20:35:03 -0400355 u64 super_blocks_used;
356 struct btrfs_fs_info *info = extent_root->fs_info;
Chris Mason037e6392007-03-07 11:50:24 -0500357
Chris Masoncf27e1e2007-03-13 09:49:06 -0400358 btrfs_set_extent_refs(&extent_item, 1);
Chris Mason037e6392007-03-07 11:50:24 -0500359 ins.offset = 1;
360 ins.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -0400361 btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
Chris Mason5d0c3e62007-04-23 17:01:05 -0400362 btrfs_set_extent_owner(&extent_item, extent_root->root_key.objectid);
Chris Mason037e6392007-03-07 11:50:24 -0500363
Chris Masonf2458e12007-04-25 15:52:25 -0400364 for (i = 0; i < extent_root->fs_info->extent_tree_insert_nr; i++) {
365 ins.objectid = extent_root->fs_info->extent_tree_insert[i];
Chris Mason1261ec42007-03-20 20:35:03 -0400366 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
367 btrfs_set_super_blocks_used(info->disk_super,
368 super_blocks_used + 1);
Chris Masone089f052007-03-16 16:20:31 -0400369 ret = btrfs_insert_item(trans, extent_root, &ins, &extent_item,
370 sizeof(extent_item));
Chris Mason037e6392007-03-07 11:50:24 -0500371 BUG_ON(ret);
372 }
Chris Masonf2458e12007-04-25 15:52:25 -0400373 extent_root->fs_info->extent_tree_insert_nr = 0;
374 extent_root->fs_info->extent_tree_prealloc_nr = 0;
Chris Mason037e6392007-03-07 11:50:24 -0500375 return 0;
376}
377
Chris Mason8ef97622007-03-26 10:15:30 -0400378static int pin_down_block(struct btrfs_root *root, u64 blocknr, int pending)
Chris Masone20d96d2007-03-22 12:13:20 -0400379{
380 int err;
Chris Mason78fae272007-03-25 11:35:08 -0400381 struct btrfs_header *header;
Chris Mason8ef97622007-03-26 10:15:30 -0400382 struct buffer_head *bh;
Chris Mason78fae272007-03-25 11:35:08 -0400383
Chris Masonf4b9aa82007-03-27 11:05:53 -0400384 if (!pending) {
Chris Masond98237b2007-03-28 13:57:48 -0400385 bh = btrfs_find_tree_block(root, blocknr);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400386 if (bh) {
387 if (buffer_uptodate(bh)) {
388 u64 transid =
389 root->fs_info->running_transaction->transid;
390 header = btrfs_buffer_header(bh);
391 if (btrfs_header_generation(header) ==
392 transid) {
393 btrfs_block_release(root, bh);
394 return 0;
395 }
Chris Masonf4b9aa82007-03-27 11:05:53 -0400396 }
Chris Masond6025572007-03-30 14:27:56 -0400397 btrfs_block_release(root, bh);
Chris Mason8ef97622007-03-26 10:15:30 -0400398 }
Chris Mason8ef97622007-03-26 10:15:30 -0400399 err = set_radix_bit(&root->fs_info->pinned_radix, blocknr);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400400 } else {
401 err = set_radix_bit(&root->fs_info->pending_del_radix, blocknr);
402 }
Chris Mason8ef97622007-03-26 10:15:30 -0400403 BUG_ON(err);
Chris Masone20d96d2007-03-22 12:13:20 -0400404 return 0;
405}
406
Chris Masona28ec192007-03-06 20:08:01 -0500407/*
408 * remove an extent from the root, returns 0 on success
409 */
Chris Masone089f052007-03-16 16:20:31 -0400410static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason78fae272007-03-25 11:35:08 -0400411 *root, u64 blocknr, u64 num_blocks, int pin)
Chris Masona28ec192007-03-06 20:08:01 -0500412{
Chris Mason5caf2a02007-04-02 11:20:42 -0400413 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -0400414 struct btrfs_key key;
Chris Mason1261ec42007-03-20 20:35:03 -0400415 struct btrfs_fs_info *info = root->fs_info;
416 struct btrfs_root *extent_root = info->extent_root;
Chris Masona28ec192007-03-06 20:08:01 -0500417 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400418 struct btrfs_extent_item *ei;
Chris Masone2fa7222007-03-12 16:22:34 -0400419 struct btrfs_key ins;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400420 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500421
Chris Masona28ec192007-03-06 20:08:01 -0500422 key.objectid = blocknr;
423 key.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -0400424 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masona28ec192007-03-06 20:08:01 -0500425 key.offset = num_blocks;
426
Chris Masone089f052007-03-16 16:20:31 -0400427 find_free_extent(trans, root, 0, 0, (u64)-1, &ins);
Chris Mason5caf2a02007-04-02 11:20:42 -0400428 path = btrfs_alloc_path();
429 BUG_ON(!path);
430 btrfs_init_path(path);
Chris Mason5f26f772007-04-05 10:38:44 -0400431
Chris Mason5caf2a02007-04-02 11:20:42 -0400432 ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
Chris Masona28ec192007-03-06 20:08:01 -0500433 if (ret) {
Chris Mason2e635a22007-03-21 11:12:56 -0400434 printk("failed to find %Lu\n", key.objectid);
Chris Mason234b63a2007-03-13 10:46:10 -0400435 btrfs_print_tree(extent_root, extent_root->node);
Chris Mason2e635a22007-03-21 11:12:56 -0400436 printk("failed to find %Lu\n", key.objectid);
Chris Masona28ec192007-03-06 20:08:01 -0500437 BUG();
438 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400439 ei = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
Chris Mason123abc82007-03-14 14:14:43 -0400440 struct btrfs_extent_item);
Chris Masona28ec192007-03-06 20:08:01 -0500441 BUG_ON(ei->refs == 0);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400442 refs = btrfs_extent_refs(ei) - 1;
443 btrfs_set_extent_refs(ei, refs);
Chris Mason5caf2a02007-04-02 11:20:42 -0400444 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400445 if (refs == 0) {
Chris Mason1261ec42007-03-20 20:35:03 -0400446 u64 super_blocks_used;
Chris Mason78fae272007-03-25 11:35:08 -0400447
448 if (pin) {
Chris Mason8ef97622007-03-26 10:15:30 -0400449 ret = pin_down_block(root, blocknr, 0);
Chris Mason78fae272007-03-25 11:35:08 -0400450 BUG_ON(ret);
451 }
452
Chris Mason1261ec42007-03-20 20:35:03 -0400453 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
454 btrfs_set_super_blocks_used(info->disk_super,
455 super_blocks_used - num_blocks);
Chris Mason5caf2a02007-04-02 11:20:42 -0400456 ret = btrfs_del_item(trans, extent_root, path);
Chris Masona28ec192007-03-06 20:08:01 -0500457 if (ret)
458 BUG();
Chris Mason9078a3e2007-04-26 16:46:15 -0400459 ret = update_block_group(trans, root, blocknr, num_blocks, 0);
460 BUG_ON(ret);
Chris Masona28ec192007-03-06 20:08:01 -0500461 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400462 btrfs_release_path(extent_root, path);
463 btrfs_free_path(path);
Chris Masone089f052007-03-16 16:20:31 -0400464 finish_current_insert(trans, extent_root);
Chris Masona28ec192007-03-06 20:08:01 -0500465 return ret;
466}
467
468/*
Chris Masonfec577f2007-02-26 10:40:21 -0500469 * find all the blocks marked as pending in the radix tree and remove
470 * them from the extent map
471 */
Chris Masone089f052007-03-16 16:20:31 -0400472static int del_pending_extents(struct btrfs_trans_handle *trans, struct
473 btrfs_root *extent_root)
Chris Masonfec577f2007-02-26 10:40:21 -0500474{
475 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -0400476 int wret;
477 int err = 0;
Chris Mason8ef97622007-03-26 10:15:30 -0400478 unsigned long gang[4];
Chris Masonfec577f2007-02-26 10:40:21 -0500479 int i;
Chris Mason8ef97622007-03-26 10:15:30 -0400480 struct radix_tree_root *pending_radix;
481 struct radix_tree_root *pinned_radix;
482
483 pending_radix = &extent_root->fs_info->pending_del_radix;
484 pinned_radix = &extent_root->fs_info->pinned_radix;
Chris Masonfec577f2007-02-26 10:40:21 -0500485
486 while(1) {
Chris Mason8ef97622007-03-26 10:15:30 -0400487 ret = find_first_radix_bit(pending_radix, gang,
488 ARRAY_SIZE(gang));
Chris Masonfec577f2007-02-26 10:40:21 -0500489 if (!ret)
490 break;
491 for (i = 0; i < ret; i++) {
Chris Mason8ef97622007-03-26 10:15:30 -0400492 wret = set_radix_bit(pinned_radix, gang[i]);
493 BUG_ON(wret);
494 wret = clear_radix_bit(pending_radix, gang[i]);
495 BUG_ON(wret);
Chris Masond5719762007-03-23 10:01:08 -0400496 wret = __free_extent(trans, extent_root,
Chris Mason8ef97622007-03-26 10:15:30 -0400497 gang[i], 1, 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400498 if (wret)
499 err = wret;
Chris Masonfec577f2007-02-26 10:40:21 -0500500 }
501 }
Chris Masone20d96d2007-03-22 12:13:20 -0400502 return err;
Chris Masonfec577f2007-02-26 10:40:21 -0500503}
504
505/*
506 * remove an extent from the root, returns 0 on success
507 */
Chris Masone089f052007-03-16 16:20:31 -0400508int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
509 *root, u64 blocknr, u64 num_blocks, int pin)
Chris Masonfec577f2007-02-26 10:40:21 -0500510{
Chris Mason9f5fae22007-03-20 14:38:32 -0400511 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Masonfec577f2007-02-26 10:40:21 -0500512 int pending_ret;
513 int ret;
Chris Masona28ec192007-03-06 20:08:01 -0500514
515 if (root == extent_root) {
Chris Mason8ef97622007-03-26 10:15:30 -0400516 pin_down_block(root, blocknr, 1);
Chris Masona28ec192007-03-06 20:08:01 -0500517 return 0;
518 }
Chris Mason78fae272007-03-25 11:35:08 -0400519 ret = __free_extent(trans, root, blocknr, num_blocks, pin);
Chris Masone20d96d2007-03-22 12:13:20 -0400520 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
Chris Masonfec577f2007-02-26 10:40:21 -0500521 return ret ? ret : pending_ret;
522}
523
524/*
525 * walks the btree of allocated extents and find a hole of a given size.
526 * The key ins is changed to record the hole:
527 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -0400528 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -0500529 * ins->offset == number of blocks
530 * Any available blocks before search_start are skipped.
531 */
Chris Masone089f052007-03-16 16:20:31 -0400532static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
533 *orig_root, u64 num_blocks, u64 search_start, u64
534 search_end, struct btrfs_key *ins)
Chris Masonfec577f2007-02-26 10:40:21 -0500535{
Chris Mason5caf2a02007-04-02 11:20:42 -0400536 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -0400537 struct btrfs_key key;
Chris Masonfec577f2007-02-26 10:40:21 -0500538 int ret;
539 u64 hole_size = 0;
540 int slot = 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400541 u64 last_block = 0;
Chris Mason037e6392007-03-07 11:50:24 -0500542 u64 test_block;
Chris Masonfec577f2007-02-26 10:40:21 -0500543 int start_found;
Chris Mason234b63a2007-03-13 10:46:10 -0400544 struct btrfs_leaf *l;
Chris Mason9f5fae22007-03-20 14:38:32 -0400545 struct btrfs_root * root = orig_root->fs_info->extent_root;
Chris Masonf2458e12007-04-25 15:52:25 -0400546 struct btrfs_fs_info *info = root->fs_info;
Chris Mason0579da42007-03-07 16:15:30 -0500547 int total_needed = num_blocks;
Chris Masonf2458e12007-04-25 15:52:25 -0400548 int total_found = 0;
549 int fill_prealloc = 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400550 int level;
Chris Mason31f3c992007-04-30 15:25:45 -0400551 int update_block_group = 0;
552 struct btrfs_block_group_cache *hint_block_group;
Chris Masonfec577f2007-02-26 10:40:21 -0500553
Chris Masonb1a4d962007-04-04 15:27:52 -0400554 path = btrfs_alloc_path();
555 ins->flags = 0;
556 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
557
Chris Masone20d96d2007-03-22 12:13:20 -0400558 level = btrfs_header_level(btrfs_buffer_header(root->node));
Chris Mason31f3c992007-04-30 15:25:45 -0400559 /* find search start here */
560 if (0 && search_start && num_blocks) {
561 u64 used;
562 ret = radix_tree_gang_lookup(&info->block_group_radix,
563 (void **)&hint_block_group,
564 search_start, 1);
565 if (ret) {
566 used = btrfs_block_group_used(&hint_block_group->item);
567 if (used > (hint_block_group->key.offset * 9) / 10)
568 search_start = 0;
569 else if (search_start < hint_block_group->last_alloc)
570 search_start = hint_block_group->last_alloc;
571 } else {
572 search_start = 0;
573 }
574 }
Chris Masonf2458e12007-04-25 15:52:25 -0400575 if (num_blocks == 0) {
576 fill_prealloc = 1;
577 num_blocks = 1;
Chris Mason308535a2007-04-28 15:17:08 -0400578 total_needed = (min(level + 1, BTRFS_MAX_LEVEL) + 2) * 3;
Chris Masonf2458e12007-04-25 15:52:25 -0400579 }
Chris Mason31f3c992007-04-30 15:25:45 -0400580 if (1 || !search_start) {
581 trans->block_group = btrfs_find_block_group(root,
582 trans->block_group,
583 0);
584 if (trans->block_group->last_alloc > search_start)
585 search_start = trans->block_group->last_alloc;
586 update_block_group = 1;
587 }
Chris Masonfec577f2007-02-26 10:40:21 -0500588check_failed:
Chris Mason5caf2a02007-04-02 11:20:42 -0400589 btrfs_init_path(path);
Chris Masonfec577f2007-02-26 10:40:21 -0500590 ins->objectid = search_start;
591 ins->offset = 0;
Chris Masonfec577f2007-02-26 10:40:21 -0500592 start_found = 0;
Chris Mason5caf2a02007-04-02 11:20:42 -0400593 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
Chris Mason0f70abe2007-02-28 16:46:22 -0500594 if (ret < 0)
595 goto error;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500596
Chris Mason5caf2a02007-04-02 11:20:42 -0400597 if (path->slots[0] > 0)
598 path->slots[0]--;
Chris Mason0579da42007-03-07 16:15:30 -0500599
Chris Masonfec577f2007-02-26 10:40:21 -0500600 while (1) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400601 l = btrfs_buffer_leaf(path->nodes[0]);
602 slot = path->slots[0];
Chris Mason7518a232007-03-12 12:01:18 -0400603 if (slot >= btrfs_header_nritems(&l->header)) {
Chris Masonf2458e12007-04-25 15:52:25 -0400604 if (fill_prealloc) {
605 info->extent_tree_prealloc_nr = 0;
606 total_found = 0;
607 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400608 ret = btrfs_next_leaf(root, path);
Chris Masonfec577f2007-02-26 10:40:21 -0500609 if (ret == 0)
610 continue;
Chris Mason0f70abe2007-02-28 16:46:22 -0500611 if (ret < 0)
612 goto error;
Chris Masonfec577f2007-02-26 10:40:21 -0500613 if (!start_found) {
614 ins->objectid = search_start;
Chris Masonf2458e12007-04-25 15:52:25 -0400615 ins->offset = (u64)-1 - search_start;
Chris Masonfec577f2007-02-26 10:40:21 -0500616 start_found = 1;
617 goto check_pending;
618 }
619 ins->objectid = last_block > search_start ?
620 last_block : search_start;
Chris Masonf2458e12007-04-25 15:52:25 -0400621 ins->offset = (u64)-1 - ins->objectid;
Chris Masonfec577f2007-02-26 10:40:21 -0500622 goto check_pending;
623 }
Chris Masone2fa7222007-03-12 16:22:34 -0400624 btrfs_disk_key_to_cpu(&key, &l->items[slot].key);
Chris Mason9078a3e2007-04-26 16:46:15 -0400625 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY)
626 goto next;
Chris Masone2fa7222007-03-12 16:22:34 -0400627 if (key.objectid >= search_start) {
Chris Masonfec577f2007-02-26 10:40:21 -0500628 if (start_found) {
Chris Mason0579da42007-03-07 16:15:30 -0500629 if (last_block < search_start)
630 last_block = search_start;
Chris Masone2fa7222007-03-12 16:22:34 -0400631 hole_size = key.objectid - last_block;
Chris Mason28b8bb92007-04-27 11:42:05 -0400632 if (hole_size >= num_blocks) {
Chris Masonfec577f2007-02-26 10:40:21 -0500633 ins->objectid = last_block;
Chris Mason037e6392007-03-07 11:50:24 -0500634 ins->offset = hole_size;
Chris Masonfec577f2007-02-26 10:40:21 -0500635 goto check_pending;
636 }
Chris Mason0579da42007-03-07 16:15:30 -0500637 }
Chris Masonfec577f2007-02-26 10:40:21 -0500638 }
Chris Mason0579da42007-03-07 16:15:30 -0500639 start_found = 1;
Chris Masone2fa7222007-03-12 16:22:34 -0400640 last_block = key.objectid + key.offset;
Chris Mason9078a3e2007-04-26 16:46:15 -0400641next:
Chris Mason5caf2a02007-04-02 11:20:42 -0400642 path->slots[0]++;
Chris Masonfec577f2007-02-26 10:40:21 -0500643 }
644 // FIXME -ENOSPC
645check_pending:
646 /* we have to make sure we didn't find an extent that has already
647 * been allocated by the map tree or the original allocation
648 */
Chris Mason5caf2a02007-04-02 11:20:42 -0400649 btrfs_release_path(root, path);
Chris Masonfec577f2007-02-26 10:40:21 -0500650 BUG_ON(ins->objectid < search_start);
Chris Mason06a2f9f2007-04-28 08:48:10 -0400651 if (ins->objectid >= btrfs_super_total_blocks(info->disk_super)) {
652 if (search_start == 0)
653 return -ENOSPC;
654 search_start = 0;
655 goto check_failed;
656 }
Chris Mason037e6392007-03-07 11:50:24 -0500657 for (test_block = ins->objectid;
Chris Masonf2458e12007-04-25 15:52:25 -0400658 test_block < ins->objectid + num_blocks; test_block++) {
659 if (test_radix_bit(&info->pinned_radix, test_block)) {
Chris Mason037e6392007-03-07 11:50:24 -0500660 search_start = test_block + 1;
Chris Masonfec577f2007-02-26 10:40:21 -0500661 goto check_failed;
662 }
663 }
Chris Masonf2458e12007-04-25 15:52:25 -0400664 if (!fill_prealloc && info->extent_tree_insert_nr) {
665 u64 last =
666 info->extent_tree_insert[info->extent_tree_insert_nr - 1];
667 if (ins->objectid + num_blocks >
668 info->extent_tree_insert[0] &&
669 ins->objectid <= last) {
670 search_start = last + 1;
671 WARN_ON(1);
672 goto check_failed;
673 }
674 }
675 if (!fill_prealloc && info->extent_tree_prealloc_nr) {
676 u64 first =
677 info->extent_tree_prealloc[info->extent_tree_prealloc_nr - 1];
678 if (ins->objectid + num_blocks > first &&
679 ins->objectid <= info->extent_tree_prealloc[0]) {
680 search_start = info->extent_tree_prealloc[0] + 1;
681 WARN_ON(1);
682 goto check_failed;
683 }
684 }
685 if (fill_prealloc) {
686 int nr;
687 test_block = ins->objectid;
688 while(test_block < ins->objectid + ins->offset &&
689 total_found < total_needed) {
690 nr = total_needed - total_found - 1;
691 BUG_ON(nr < 0);
Chris Masoncd1bc462007-04-27 10:08:34 -0400692 info->extent_tree_prealloc[nr] = test_block;
Chris Masonf2458e12007-04-25 15:52:25 -0400693 total_found++;
694 test_block++;
695 }
696 if (total_found < total_needed) {
697 search_start = test_block;
698 goto check_failed;
699 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400700 info->extent_tree_prealloc_nr = total_found;
Chris Masonf2458e12007-04-25 15:52:25 -0400701 }
Chris Mason31f3c992007-04-30 15:25:45 -0400702 if (update_block_group) {
703 ret = radix_tree_gang_lookup(&info->block_group_radix,
704 (void **)&trans->block_group,
705 ins->objectid, 1);
706 if (ret) {
707 trans->block_group->last_alloc = ins->objectid;
708 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400709 }
Chris Mason037e6392007-03-07 11:50:24 -0500710 ins->offset = num_blocks;
Chris Mason5caf2a02007-04-02 11:20:42 -0400711 btrfs_free_path(path);
Chris Masonfec577f2007-02-26 10:40:21 -0500712 return 0;
Chris Mason0f70abe2007-02-28 16:46:22 -0500713error:
Chris Mason5caf2a02007-04-02 11:20:42 -0400714 btrfs_release_path(root, path);
715 btrfs_free_path(path);
Chris Mason0f70abe2007-02-28 16:46:22 -0500716 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -0500717}
Chris Masonfec577f2007-02-26 10:40:21 -0500718/*
Chris Masonfec577f2007-02-26 10:40:21 -0500719 * finds a free extent and does all the dirty work required for allocation
720 * returns the key for the extent through ins, and a tree buffer for
721 * the first block of the extent through buf.
722 *
723 * returns 0 if everything worked, non-zero otherwise.
724 */
Chris Mason4d775672007-04-20 20:23:12 -0400725int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
726 struct btrfs_root *root, u64 owner,
Chris Masonc62a1922007-04-24 12:07:39 -0400727 u64 num_blocks, u64 search_start,
Chris Mason4d775672007-04-20 20:23:12 -0400728 u64 search_end, struct btrfs_key *ins)
Chris Masonfec577f2007-02-26 10:40:21 -0500729{
730 int ret;
731 int pending_ret;
Chris Mason1261ec42007-03-20 20:35:03 -0400732 u64 super_blocks_used;
733 struct btrfs_fs_info *info = root->fs_info;
734 struct btrfs_root *extent_root = info->extent_root;
Chris Mason234b63a2007-03-13 10:46:10 -0400735 struct btrfs_extent_item extent_item;
Chris Masonf2458e12007-04-25 15:52:25 -0400736 struct btrfs_key prealloc_key;
Chris Mason037e6392007-03-07 11:50:24 -0500737
Chris Masoncf27e1e2007-03-13 09:49:06 -0400738 btrfs_set_extent_refs(&extent_item, 1);
Chris Mason4d775672007-04-20 20:23:12 -0400739 btrfs_set_extent_owner(&extent_item, owner);
Chris Masonfec577f2007-02-26 10:40:21 -0500740
Chris Mason037e6392007-03-07 11:50:24 -0500741 if (root == extent_root) {
Chris Masonf2458e12007-04-25 15:52:25 -0400742 int nr;
743 BUG_ON(info->extent_tree_prealloc_nr == 0);
Chris Mason037e6392007-03-07 11:50:24 -0500744 BUG_ON(num_blocks != 1);
Chris Mason037e6392007-03-07 11:50:24 -0500745 ins->offset = 1;
Chris Masonf2458e12007-04-25 15:52:25 -0400746 info->extent_tree_prealloc_nr--;
747 nr = info->extent_tree_prealloc_nr;
748 ins->objectid = info->extent_tree_prealloc[nr];
749 info->extent_tree_insert[info->extent_tree_insert_nr++] =
750 ins->objectid;
Chris Mason9078a3e2007-04-26 16:46:15 -0400751 ret = update_block_group(trans, root,
752 ins->objectid, ins->offset, 1);
753 BUG_ON(ret);
Chris Masonfec577f2007-02-26 10:40:21 -0500754 return 0;
755 }
Chris Masonf2458e12007-04-25 15:52:25 -0400756 /* do the real allocation */
Chris Masone089f052007-03-16 16:20:31 -0400757 ret = find_free_extent(trans, root, num_blocks, search_start,
Chris Mason037e6392007-03-07 11:50:24 -0500758 search_end, ins);
759 if (ret)
760 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -0500761
Chris Masonf2458e12007-04-25 15:52:25 -0400762 /* then do prealloc for the extent tree */
763 ret = find_free_extent(trans, root, 0, ins->objectid + ins->offset,
764 search_end, &prealloc_key);
765 if (ret)
766 return ret;
767
Chris Mason1261ec42007-03-20 20:35:03 -0400768 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
769 btrfs_set_super_blocks_used(info->disk_super, super_blocks_used +
770 num_blocks);
Chris Masone089f052007-03-16 16:20:31 -0400771 ret = btrfs_insert_item(trans, extent_root, ins, &extent_item,
772 sizeof(extent_item));
Chris Mason037e6392007-03-07 11:50:24 -0500773
Chris Masone089f052007-03-16 16:20:31 -0400774 finish_current_insert(trans, extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -0400775 pending_ret = del_pending_extents(trans, extent_root);
Chris Mason037e6392007-03-07 11:50:24 -0500776 if (ret)
777 return ret;
778 if (pending_ret)
779 return pending_ret;
Chris Mason9078a3e2007-04-26 16:46:15 -0400780 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1);
Chris Mason037e6392007-03-07 11:50:24 -0500781 return 0;
Chris Masonfec577f2007-02-26 10:40:21 -0500782}
783
784/*
785 * helper function to allocate a block for a given tree
786 * returns the tree buffer or NULL.
787 */
Chris Masone20d96d2007-03-22 12:13:20 -0400788struct buffer_head *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Chris Mason31f3c992007-04-30 15:25:45 -0400789 struct btrfs_root *root, u64 hint)
Chris Masonfec577f2007-02-26 10:40:21 -0500790{
Chris Masone2fa7222007-03-12 16:22:34 -0400791 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -0500792 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -0400793 struct buffer_head *buf;
Chris Masonfec577f2007-02-26 10:40:21 -0500794
Chris Mason4d775672007-04-20 20:23:12 -0400795 ret = btrfs_alloc_extent(trans, root, root->root_key.objectid,
Chris Mason31f3c992007-04-30 15:25:45 -0400796 1, hint, (unsigned long)-1, &ins);
Chris Masonfec577f2007-02-26 10:40:21 -0500797 if (ret) {
798 BUG();
799 return NULL;
800 }
Chris Mason9078a3e2007-04-26 16:46:15 -0400801 BUG_ON(ret);
Chris Masond98237b2007-03-28 13:57:48 -0400802 buf = btrfs_find_create_tree_block(root, ins.objectid);
Chris Masondf2ce342007-03-23 11:00:45 -0400803 set_buffer_uptodate(buf);
Chris Mason090d1872007-05-01 08:53:32 -0400804 set_buffer_checked(buf);
Chris Mason7c4452b2007-04-28 09:29:35 -0400805 set_radix_bit(&trans->transaction->dirty_pages, buf->b_page->index);
Chris Masonfec577f2007-02-26 10:40:21 -0500806 return buf;
807}
Chris Masona28ec192007-03-06 20:08:01 -0500808
Chris Mason6407bf62007-03-27 06:33:00 -0400809static int drop_leaf_ref(struct btrfs_trans_handle *trans,
810 struct btrfs_root *root, struct buffer_head *cur)
811{
812 struct btrfs_disk_key *key;
813 struct btrfs_leaf *leaf;
814 struct btrfs_file_extent_item *fi;
815 int i;
816 int nritems;
817 int ret;
818
819 BUG_ON(!btrfs_is_leaf(btrfs_buffer_node(cur)));
820 leaf = btrfs_buffer_leaf(cur);
821 nritems = btrfs_header_nritems(&leaf->header);
822 for (i = 0; i < nritems; i++) {
823 key = &leaf->items[i].key;
824 if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY)
825 continue;
826 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
Chris Mason236454df2007-04-19 13:37:44 -0400827 if (btrfs_file_extent_type(fi) == BTRFS_FILE_EXTENT_INLINE)
828 continue;
Chris Mason6407bf62007-03-27 06:33:00 -0400829 /*
830 * FIXME make sure to insert a trans record that
831 * repeats the snapshot del on crash
832 */
833 ret = btrfs_free_extent(trans, root,
834 btrfs_file_extent_disk_blocknr(fi),
835 btrfs_file_extent_disk_num_blocks(fi),
836 0);
837 BUG_ON(ret);
838 }
839 return 0;
840}
841
Chris Mason9aca1d52007-03-13 11:09:37 -0400842/*
843 * helper function for drop_snapshot, this walks down the tree dropping ref
844 * counts as it goes.
845 */
Chris Masone089f052007-03-16 16:20:31 -0400846static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
847 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -0500848{
Chris Masone20d96d2007-03-22 12:13:20 -0400849 struct buffer_head *next;
850 struct buffer_head *cur;
Chris Mason20524f02007-03-10 06:35:47 -0500851 u64 blocknr;
852 int ret;
853 u32 refs;
854
Chris Mason5caf2a02007-04-02 11:20:42 -0400855 WARN_ON(*level < 0);
856 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Masonb18c6682007-04-17 13:26:50 -0400857 ret = lookup_extent_ref(trans, root, bh_blocknr(path->nodes[*level]),
Chris Mason6407bf62007-03-27 06:33:00 -0400858 1, &refs);
Chris Mason20524f02007-03-10 06:35:47 -0500859 BUG_ON(ret);
860 if (refs > 1)
861 goto out;
Chris Mason9aca1d52007-03-13 11:09:37 -0400862 /*
863 * walk down to the last node level and free all the leaves
864 */
Chris Mason6407bf62007-03-27 06:33:00 -0400865 while(*level >= 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400866 WARN_ON(*level < 0);
867 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason20524f02007-03-10 06:35:47 -0500868 cur = path->nodes[*level];
Chris Mason2c90e5d2007-04-02 10:50:19 -0400869 if (btrfs_header_level(btrfs_buffer_header(cur)) != *level)
870 WARN_ON(1);
Chris Mason7518a232007-03-12 12:01:18 -0400871 if (path->slots[*level] >=
Chris Masone20d96d2007-03-22 12:13:20 -0400872 btrfs_header_nritems(btrfs_buffer_header(cur)))
Chris Mason20524f02007-03-10 06:35:47 -0500873 break;
Chris Mason6407bf62007-03-27 06:33:00 -0400874 if (*level == 0) {
875 ret = drop_leaf_ref(trans, root, cur);
876 BUG_ON(ret);
877 break;
878 }
Chris Masone20d96d2007-03-22 12:13:20 -0400879 blocknr = btrfs_node_blockptr(btrfs_buffer_node(cur),
880 path->slots[*level]);
Chris Masonb18c6682007-04-17 13:26:50 -0400881 ret = lookup_extent_ref(trans, root, blocknr, 1, &refs);
Chris Mason6407bf62007-03-27 06:33:00 -0400882 BUG_ON(ret);
883 if (refs != 1) {
Chris Mason20524f02007-03-10 06:35:47 -0500884 path->slots[*level]++;
Chris Masone089f052007-03-16 16:20:31 -0400885 ret = btrfs_free_extent(trans, root, blocknr, 1, 1);
Chris Mason20524f02007-03-10 06:35:47 -0500886 BUG_ON(ret);
887 continue;
888 }
Chris Mason20524f02007-03-10 06:35:47 -0500889 next = read_tree_block(root, blocknr);
Chris Mason5caf2a02007-04-02 11:20:42 -0400890 WARN_ON(*level <= 0);
Chris Mason83e15a22007-03-12 09:03:27 -0400891 if (path->nodes[*level-1])
Chris Mason234b63a2007-03-13 10:46:10 -0400892 btrfs_block_release(root, path->nodes[*level-1]);
Chris Mason20524f02007-03-10 06:35:47 -0500893 path->nodes[*level-1] = next;
Chris Masone20d96d2007-03-22 12:13:20 -0400894 *level = btrfs_header_level(btrfs_buffer_header(next));
Chris Mason20524f02007-03-10 06:35:47 -0500895 path->slots[*level] = 0;
896 }
897out:
Chris Mason5caf2a02007-04-02 11:20:42 -0400898 WARN_ON(*level < 0);
899 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason6407bf62007-03-27 06:33:00 -0400900 ret = btrfs_free_extent(trans, root,
Chris Mason7eccb902007-04-11 15:53:25 -0400901 bh_blocknr(path->nodes[*level]), 1, 1);
Chris Mason234b63a2007-03-13 10:46:10 -0400902 btrfs_block_release(root, path->nodes[*level]);
Chris Mason20524f02007-03-10 06:35:47 -0500903 path->nodes[*level] = NULL;
904 *level += 1;
905 BUG_ON(ret);
906 return 0;
907}
908
Chris Mason9aca1d52007-03-13 11:09:37 -0400909/*
910 * helper for dropping snapshots. This walks back up the tree in the path
911 * to find the first node higher up where we haven't yet gone through
912 * all the slots
913 */
Chris Masone089f052007-03-16 16:20:31 -0400914static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root
915 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -0500916{
917 int i;
918 int slot;
919 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400920 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Mason20524f02007-03-10 06:35:47 -0500921 slot = path->slots[i];
Chris Masone20d96d2007-03-22 12:13:20 -0400922 if (slot < btrfs_header_nritems(
923 btrfs_buffer_header(path->nodes[i])) - 1) {
Chris Mason20524f02007-03-10 06:35:47 -0500924 path->slots[i]++;
925 *level = i;
926 return 0;
927 } else {
Chris Masone089f052007-03-16 16:20:31 -0400928 ret = btrfs_free_extent(trans, root,
Chris Mason7eccb902007-04-11 15:53:25 -0400929 bh_blocknr(path->nodes[*level]),
Chris Masone089f052007-03-16 16:20:31 -0400930 1, 1);
Chris Mason6407bf62007-03-27 06:33:00 -0400931 BUG_ON(ret);
Chris Mason234b63a2007-03-13 10:46:10 -0400932 btrfs_block_release(root, path->nodes[*level]);
Chris Mason83e15a22007-03-12 09:03:27 -0400933 path->nodes[*level] = NULL;
Chris Mason20524f02007-03-10 06:35:47 -0500934 *level = i + 1;
Chris Mason20524f02007-03-10 06:35:47 -0500935 }
936 }
937 return 1;
938}
939
Chris Mason9aca1d52007-03-13 11:09:37 -0400940/*
941 * drop the reference count on the tree rooted at 'snap'. This traverses
942 * the tree freeing any blocks that have a ref count of zero after being
943 * decremented.
944 */
Chris Masone089f052007-03-16 16:20:31 -0400945int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masone20d96d2007-03-22 12:13:20 -0400946 *root, struct buffer_head *snap)
Chris Mason20524f02007-03-10 06:35:47 -0500947{
Chris Mason3768f362007-03-13 16:47:54 -0400948 int ret = 0;
Chris Mason9aca1d52007-03-13 11:09:37 -0400949 int wret;
Chris Mason20524f02007-03-10 06:35:47 -0500950 int level;
Chris Mason5caf2a02007-04-02 11:20:42 -0400951 struct btrfs_path *path;
Chris Mason20524f02007-03-10 06:35:47 -0500952 int i;
953 int orig_level;
954
Chris Mason5caf2a02007-04-02 11:20:42 -0400955 path = btrfs_alloc_path();
956 BUG_ON(!path);
957 btrfs_init_path(path);
Chris Mason20524f02007-03-10 06:35:47 -0500958
Chris Masone20d96d2007-03-22 12:13:20 -0400959 level = btrfs_header_level(btrfs_buffer_header(snap));
Chris Mason20524f02007-03-10 06:35:47 -0500960 orig_level = level;
Chris Mason5caf2a02007-04-02 11:20:42 -0400961 path->nodes[level] = snap;
962 path->slots[level] = 0;
Chris Mason20524f02007-03-10 06:35:47 -0500963 while(1) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400964 wret = walk_down_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -0400965 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -0500966 break;
Chris Mason9aca1d52007-03-13 11:09:37 -0400967 if (wret < 0)
968 ret = wret;
969
Chris Mason5caf2a02007-04-02 11:20:42 -0400970 wret = walk_up_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -0400971 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -0500972 break;
Chris Mason9aca1d52007-03-13 11:09:37 -0400973 if (wret < 0)
974 ret = wret;
Chris Mason35b7e472007-05-02 15:53:43 -0400975 btrfs_btree_balance_dirty(root);
Chris Mason20524f02007-03-10 06:35:47 -0500976 }
Chris Mason83e15a22007-03-12 09:03:27 -0400977 for (i = 0; i <= orig_level; i++) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400978 if (path->nodes[i]) {
979 btrfs_block_release(root, path->nodes[i]);
Chris Mason83e15a22007-03-12 09:03:27 -0400980 }
Chris Mason20524f02007-03-10 06:35:47 -0500981 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400982 btrfs_free_path(path);
Chris Mason9aca1d52007-03-13 11:09:37 -0400983 return ret;
Chris Mason20524f02007-03-10 06:35:47 -0500984}
Chris Mason9078a3e2007-04-26 16:46:15 -0400985
986int btrfs_free_block_groups(struct btrfs_fs_info *info)
987{
988 int ret;
989 struct btrfs_block_group_cache *cache[8];
990 int i;
991
992 while(1) {
993 ret = radix_tree_gang_lookup(&info->block_group_radix,
994 (void **)cache, 0,
995 ARRAY_SIZE(cache));
996 if (!ret)
997 break;
998 for (i = 0; i < ret; i++) {
999 radix_tree_delete(&info->block_group_radix,
1000 cache[i]->key.objectid +
1001 cache[i]->key.offset - 1);
1002 kfree(cache[i]);
1003 }
1004 }
1005 return 0;
1006}
1007
1008int btrfs_read_block_groups(struct btrfs_root *root)
1009{
1010 struct btrfs_path *path;
1011 int ret;
1012 int err = 0;
1013 struct btrfs_block_group_item *bi;
1014 struct btrfs_block_group_cache *cache;
1015 struct btrfs_key key;
1016 struct btrfs_key found_key;
1017 struct btrfs_leaf *leaf;
1018 u64 group_size_blocks = BTRFS_BLOCK_GROUP_SIZE / root->blocksize;
Chris Mason31f3c992007-04-30 15:25:45 -04001019 u64 used;
Chris Mason9078a3e2007-04-26 16:46:15 -04001020
1021 root = root->fs_info->extent_root;
1022 key.objectid = 0;
1023 key.offset = group_size_blocks;
1024 key.flags = 0;
1025 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
1026
1027 path = btrfs_alloc_path();
1028 if (!path)
1029 return -ENOMEM;
1030
1031 while(1) {
1032 ret = btrfs_search_slot(NULL, root->fs_info->extent_root,
1033 &key, path, 0, 0);
1034 if (ret != 0) {
1035 err = ret;
1036 break;
1037 }
1038 leaf = btrfs_buffer_leaf(path->nodes[0]);
1039 btrfs_disk_key_to_cpu(&found_key,
1040 &leaf->items[path->slots[0]].key);
1041 cache = kmalloc(sizeof(*cache), GFP_NOFS);
1042 if (!cache) {
1043 err = -1;
1044 break;
1045 }
1046 bi = btrfs_item_ptr(leaf, path->slots[0],
1047 struct btrfs_block_group_item);
1048 memcpy(&cache->item, bi, sizeof(*bi));
1049 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Mason31f3c992007-04-30 15:25:45 -04001050 cache->last_alloc = cache->key.objectid;
1051 cache->first_free = cache->key.objectid;
Chris Mason9078a3e2007-04-26 16:46:15 -04001052 key.objectid = found_key.objectid + found_key.offset;
1053 btrfs_release_path(root, path);
1054 ret = radix_tree_insert(&root->fs_info->block_group_radix,
1055 found_key.objectid +
1056 found_key.offset - 1,
1057 (void *)cache);
1058 BUG_ON(ret);
Chris Mason31f3c992007-04-30 15:25:45 -04001059 used = btrfs_block_group_used(bi);
1060 if (used < (key.offset * 2) / 3) {
1061 radix_tree_tag_set(&root->fs_info->block_group_radix,
1062 found_key.objectid +
1063 found_key.offset - 1,
1064 BTRFS_BLOCK_GROUP_AVAIL);
1065 }
Chris Mason9078a3e2007-04-26 16:46:15 -04001066 if (key.objectid >=
1067 btrfs_super_total_blocks(root->fs_info->disk_super))
1068 break;
1069 }
1070
1071 btrfs_free_path(path);
1072 return 0;
1073}