blob: c5ae51893f7874db4d75fbe5b7177a89e8564d8f [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 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;
Chris Masonbe08c1b2007-05-03 09:06:49 -040028 if (!data && hint) {
Chris Mason31f3c992007-04-30 15:25:45 -040029 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++) {
Chris Masonbe08c1b2007-05-03 09:06:49 -040050 last = cache[i]->key.objectid +
51 cache[i]->key.offset;
52 if (!full_search && !data &&
53 (cache[i]->key.objectid & cache[i]->key.offset))
54 continue;
55 if (!full_search && data &&
56 (cache[i]->key.objectid & cache[i]->key.offset) == 0)
57 continue;
Chris Masoncd1bc462007-04-27 10:08:34 -040058 used = btrfs_block_group_used(&cache[i]->item);
Chris Mason31f3c992007-04-30 15:25:45 -040059 if (used < (cache[i]->key.offset * 2) / 3) {
Chris Masoncd1bc462007-04-27 10:08:34 -040060 info->block_group_cache = cache[i];
Chris Mason31f3c992007-04-30 15:25:45 -040061 found_group = cache[i];
62 goto found;
Chris Masoncd1bc462007-04-27 10:08:34 -040063 }
Chris Mason31f3c992007-04-30 15:25:45 -040064 radix_tree_tag_clear(&info->block_group_radix,
65 cache[i]->key.objectid +
66 cache[i]->key.offset - 1,
67 BTRFS_BLOCK_GROUP_AVAIL);
Chris Masoncd1bc462007-04-27 10:08:34 -040068 }
69 }
Chris Mason31f3c992007-04-30 15:25:45 -040070 last = hint_last;
71again:
Chris Masoncd1bc462007-04-27 10:08:34 -040072 while(1) {
73 ret = radix_tree_gang_lookup(&info->block_group_radix,
74 (void **)cache,
75 last, ARRAY_SIZE(cache));
76 if (!ret)
77 break;
78 for (i = 0; i < ret; i++) {
Chris Masonbe08c1b2007-05-03 09:06:49 -040079 last = cache[i]->key.objectid +
80 cache[i]->key.offset;
81 if (!full_search && !data &&
82 (cache[i]->key.objectid & cache[i]->key.offset))
83 continue;
84 if (!full_search && data &&
85 (cache[i]->key.objectid & cache[i]->key.offset) == 0)
86 continue;
Chris Masoncd1bc462007-04-27 10:08:34 -040087 used = btrfs_block_group_used(&cache[i]->item);
Chris Mason31f3c992007-04-30 15:25:45 -040088 if (used < cache[i]->key.offset) {
Chris Masoncd1bc462007-04-27 10:08:34 -040089 info->block_group_cache = cache[i];
Chris Mason31f3c992007-04-30 15:25:45 -040090 found_group = cache[i];
91 goto found;
Chris Masoncd1bc462007-04-27 10:08:34 -040092 }
Chris Mason31f3c992007-04-30 15:25:45 -040093 radix_tree_tag_clear(&info->block_group_radix,
94 cache[i]->key.objectid +
95 cache[i]->key.offset - 1,
96 BTRFS_BLOCK_GROUP_AVAIL);
Chris Masoncd1bc462007-04-27 10:08:34 -040097 }
98 }
99 info->block_group_cache = NULL;
Chris Mason31f3c992007-04-30 15:25:45 -0400100 if (!full_search) {
101 last = 0;
102 full_search = 1;
103 goto again;
104 }
105found:
106 if (!found_group) {
107 ret = radix_tree_gang_lookup(&info->block_group_radix,
108 (void **)&found_group, 0, 1);
109 BUG_ON(ret != 1);
110 }
111 return found_group;
Chris Masoncd1bc462007-04-27 10:08:34 -0400112}
113
Chris Masonb18c6682007-04-17 13:26:50 -0400114int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
115 struct btrfs_root *root,
116 u64 blocknr, u64 num_blocks)
Chris Mason02217ed2007-03-02 16:08:05 -0500117{
Chris Mason5caf2a02007-04-02 11:20:42 -0400118 struct btrfs_path *path;
Chris Mason02217ed2007-03-02 16:08:05 -0500119 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400120 struct btrfs_key key;
Chris Mason234b63a2007-03-13 10:46:10 -0400121 struct btrfs_leaf *l;
122 struct btrfs_extent_item *item;
Chris Masone2fa7222007-03-12 16:22:34 -0400123 struct btrfs_key ins;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400124 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500125
Chris Mason9f5fae22007-03-20 14:38:32 -0400126 find_free_extent(trans, root->fs_info->extent_root, 0, 0, (u64)-1,
Chris Masonbe08c1b2007-05-03 09:06:49 -0400127 &ins, 0);
Chris Mason5caf2a02007-04-02 11:20:42 -0400128 path = btrfs_alloc_path();
129 BUG_ON(!path);
130 btrfs_init_path(path);
Chris Mason02217ed2007-03-02 16:08:05 -0500131 key.objectid = blocknr;
132 key.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -0400133 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Mason6407bf62007-03-27 06:33:00 -0400134 key.offset = num_blocks;
Chris Mason5caf2a02007-04-02 11:20:42 -0400135 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -0400136 0, 1);
Chris Masona429e512007-04-18 16:15:28 -0400137 if (ret != 0) {
138printk("can't find block %Lu %Lu\n", blocknr, num_blocks);
Chris Masona28ec192007-03-06 20:08:01 -0500139 BUG();
Chris Masona429e512007-04-18 16:15:28 -0400140 }
Chris Mason02217ed2007-03-02 16:08:05 -0500141 BUG_ON(ret != 0);
Chris Mason5caf2a02007-04-02 11:20:42 -0400142 l = btrfs_buffer_leaf(path->nodes[0]);
143 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400144 refs = btrfs_extent_refs(item);
145 btrfs_set_extent_refs(item, refs + 1);
Chris Mason5caf2a02007-04-02 11:20:42 -0400146 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masona28ec192007-03-06 20:08:01 -0500147
Chris Mason5caf2a02007-04-02 11:20:42 -0400148 btrfs_release_path(root->fs_info->extent_root, path);
149 btrfs_free_path(path);
Chris Mason9f5fae22007-03-20 14:38:32 -0400150 finish_current_insert(trans, root->fs_info->extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -0400151 del_pending_extents(trans, root->fs_info->extent_root);
Chris Mason02217ed2007-03-02 16:08:05 -0500152 return 0;
153}
154
Chris Masonb18c6682007-04-17 13:26:50 -0400155static int lookup_extent_ref(struct btrfs_trans_handle *trans,
156 struct btrfs_root *root, u64 blocknr,
157 u64 num_blocks, u32 *refs)
Chris Masona28ec192007-03-06 20:08:01 -0500158{
Chris Mason5caf2a02007-04-02 11:20:42 -0400159 struct btrfs_path *path;
Chris Masona28ec192007-03-06 20:08:01 -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 Mason5caf2a02007-04-02 11:20:42 -0400164
165 path = btrfs_alloc_path();
166 btrfs_init_path(path);
Chris Masona28ec192007-03-06 20:08:01 -0500167 key.objectid = blocknr;
Chris Mason6407bf62007-03-27 06:33:00 -0400168 key.offset = num_blocks;
Chris Mason62e27492007-03-15 12:56:47 -0400169 key.flags = 0;
170 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Mason5caf2a02007-04-02 11:20:42 -0400171 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -0400172 0, 0);
Chris Masona28ec192007-03-06 20:08:01 -0500173 if (ret != 0)
174 BUG();
Chris Mason5caf2a02007-04-02 11:20:42 -0400175 l = btrfs_buffer_leaf(path->nodes[0]);
176 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400177 *refs = btrfs_extent_refs(item);
Chris Mason5caf2a02007-04-02 11:20:42 -0400178 btrfs_release_path(root->fs_info->extent_root, path);
179 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -0500180 return 0;
181}
182
Chris Masonc5739bb2007-04-10 09:27:04 -0400183int btrfs_inc_root_ref(struct btrfs_trans_handle *trans,
184 struct btrfs_root *root)
185{
Chris Masonb18c6682007-04-17 13:26:50 -0400186 return btrfs_inc_extent_ref(trans, root, bh_blocknr(root->node), 1);
Chris Masonc5739bb2007-04-10 09:27:04 -0400187}
188
Chris Masone089f052007-03-16 16:20:31 -0400189int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Masone20d96d2007-03-22 12:13:20 -0400190 struct buffer_head *buf)
Chris Mason02217ed2007-03-02 16:08:05 -0500191{
192 u64 blocknr;
Chris Masone20d96d2007-03-22 12:13:20 -0400193 struct btrfs_node *buf_node;
Chris Mason6407bf62007-03-27 06:33:00 -0400194 struct btrfs_leaf *buf_leaf;
195 struct btrfs_disk_key *key;
196 struct btrfs_file_extent_item *fi;
Chris Mason02217ed2007-03-02 16:08:05 -0500197 int i;
Chris Mason6407bf62007-03-27 06:33:00 -0400198 int leaf;
199 int ret;
Chris Masona28ec192007-03-06 20:08:01 -0500200
Chris Mason3768f362007-03-13 16:47:54 -0400201 if (!root->ref_cows)
Chris Masona28ec192007-03-06 20:08:01 -0500202 return 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400203 buf_node = btrfs_buffer_node(buf);
Chris Mason6407bf62007-03-27 06:33:00 -0400204 leaf = btrfs_is_leaf(buf_node);
205 buf_leaf = btrfs_buffer_leaf(buf);
Chris Masone20d96d2007-03-22 12:13:20 -0400206 for (i = 0; i < btrfs_header_nritems(&buf_node->header); i++) {
Chris Mason6407bf62007-03-27 06:33:00 -0400207 if (leaf) {
208 key = &buf_leaf->items[i].key;
209 if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY)
210 continue;
211 fi = btrfs_item_ptr(buf_leaf, i,
212 struct btrfs_file_extent_item);
Chris Mason236454d2007-04-19 13:37:44 -0400213 if (btrfs_file_extent_type(fi) ==
214 BTRFS_FILE_EXTENT_INLINE)
215 continue;
Chris Masonb18c6682007-04-17 13:26:50 -0400216 ret = btrfs_inc_extent_ref(trans, root,
Chris Mason6407bf62007-03-27 06:33:00 -0400217 btrfs_file_extent_disk_blocknr(fi),
218 btrfs_file_extent_disk_num_blocks(fi));
219 BUG_ON(ret);
220 } else {
221 blocknr = btrfs_node_blockptr(buf_node, i);
Chris Masonb18c6682007-04-17 13:26:50 -0400222 ret = btrfs_inc_extent_ref(trans, root, blocknr, 1);
Chris Mason6407bf62007-03-27 06:33:00 -0400223 BUG_ON(ret);
224 }
Chris Mason02217ed2007-03-02 16:08:05 -0500225 }
226 return 0;
227}
228
Chris Mason9078a3e2007-04-26 16:46:15 -0400229static int write_one_cache_group(struct btrfs_trans_handle *trans,
230 struct btrfs_root *root,
231 struct btrfs_path *path,
232 struct btrfs_block_group_cache *cache)
233{
234 int ret;
235 int pending_ret;
236 struct btrfs_root *extent_root = root->fs_info->extent_root;
237 struct btrfs_block_group_item *bi;
238 struct btrfs_key ins;
239
Chris Masonbe08c1b2007-05-03 09:06:49 -0400240 find_free_extent(trans, extent_root, 0, 0, (u64)-1, &ins, 0);
Chris Mason9078a3e2007-04-26 16:46:15 -0400241 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
242 BUG_ON(ret);
243 bi = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
244 struct btrfs_block_group_item);
245 memcpy(bi, &cache->item, sizeof(*bi));
246 mark_buffer_dirty(path->nodes[0]);
247 btrfs_release_path(extent_root, path);
248
249 finish_current_insert(trans, extent_root);
250 pending_ret = del_pending_extents(trans, extent_root);
251 if (ret)
252 return ret;
253 if (pending_ret)
254 return pending_ret;
255 return 0;
256
257}
258
259int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
260 struct btrfs_root *root)
261{
262 struct btrfs_block_group_cache *cache[8];
263 int ret;
264 int err = 0;
265 int werr = 0;
266 struct radix_tree_root *radix = &root->fs_info->block_group_radix;
267 int i;
268 struct btrfs_path *path;
269
270 path = btrfs_alloc_path();
271 if (!path)
272 return -ENOMEM;
273
274 while(1) {
275 ret = radix_tree_gang_lookup_tag(radix, (void **)cache,
276 0, ARRAY_SIZE(cache),
277 BTRFS_BLOCK_GROUP_DIRTY);
278 if (!ret)
279 break;
280 for (i = 0; i < ret; i++) {
281 radix_tree_tag_clear(radix, cache[i]->key.objectid +
282 cache[i]->key.offset - 1,
283 BTRFS_BLOCK_GROUP_DIRTY);
284 err = write_one_cache_group(trans, root,
285 path, cache[i]);
286 if (err)
287 werr = err;
Chris Mason31f3c992007-04-30 15:25:45 -0400288 cache[i]->last_alloc = cache[i]->first_free;
Chris Mason9078a3e2007-04-26 16:46:15 -0400289 }
290 }
291 btrfs_free_path(path);
292 return werr;
293}
294
295static int update_block_group(struct btrfs_trans_handle *trans,
296 struct btrfs_root *root,
297 u64 blocknr, u64 num, int alloc)
298{
299 struct btrfs_block_group_cache *cache;
300 struct btrfs_fs_info *info = root->fs_info;
301 u64 total = num;
302 u64 old_val;
303 u64 block_in_group;
304 int ret;
305 while(total) {
306 ret = radix_tree_gang_lookup(&info->block_group_radix,
307 (void **)&cache, blocknr, 1);
Chris Masoncd1bc462007-04-27 10:08:34 -0400308 if (!ret) {
309 printk(KERN_CRIT "blocknr %Lu lookup failed\n",
310 blocknr);
Chris Mason9078a3e2007-04-26 16:46:15 -0400311 return -1;
Chris Masoncd1bc462007-04-27 10:08:34 -0400312 }
Chris Mason9078a3e2007-04-26 16:46:15 -0400313 block_in_group = blocknr - cache->key.objectid;
314 WARN_ON(block_in_group > cache->key.offset);
315 radix_tree_tag_set(&info->block_group_radix,
316 cache->key.objectid + cache->key.offset - 1,
317 BTRFS_BLOCK_GROUP_DIRTY);
318
319 old_val = btrfs_block_group_used(&cache->item);
320 num = min(total, cache->key.offset - block_in_group);
321 total -= num;
322 blocknr += num;
Chris Masoncd1bc462007-04-27 10:08:34 -0400323 if (alloc) {
Chris Mason9078a3e2007-04-26 16:46:15 -0400324 old_val += num;
Chris Masoncd1bc462007-04-27 10:08:34 -0400325 if (blocknr > cache->last_alloc)
326 cache->last_alloc = blocknr;
327 } else {
Chris Mason9078a3e2007-04-26 16:46:15 -0400328 old_val -= num;
Chris Masoncd1bc462007-04-27 10:08:34 -0400329 if (blocknr < cache->first_free)
330 cache->first_free = blocknr;
331 }
Chris Mason9078a3e2007-04-26 16:46:15 -0400332 btrfs_set_block_group_used(&cache->item, old_val);
333 }
334 return 0;
335}
336
Chris Masonbe08c1b2007-05-03 09:06:49 -0400337static int try_remove_page(struct address_space *mapping, unsigned long index)
338{
339 int ret;
340 ret = invalidate_mapping_pages(mapping, index, index);
341 return ret;
342}
343
Chris Masone089f052007-03-16 16:20:31 -0400344int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, struct
345 btrfs_root *root)
Chris Masona28ec192007-03-06 20:08:01 -0500346{
Chris Mason8ef97622007-03-26 10:15:30 -0400347 unsigned long gang[8];
Chris Masonbe08c1b2007-05-03 09:06:49 -0400348 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Mason88fd1462007-03-16 08:56:18 -0400349 u64 first = 0;
Chris Masona28ec192007-03-06 20:08:01 -0500350 int ret;
351 int i;
Chris Mason8ef97622007-03-26 10:15:30 -0400352 struct radix_tree_root *pinned_radix = &root->fs_info->pinned_radix;
Chris Masona28ec192007-03-06 20:08:01 -0500353
354 while(1) {
Chris Mason8ef97622007-03-26 10:15:30 -0400355 ret = find_first_radix_bit(pinned_radix, gang,
356 ARRAY_SIZE(gang));
Chris Masona28ec192007-03-06 20:08:01 -0500357 if (!ret)
358 break;
Chris Mason88fd1462007-03-16 08:56:18 -0400359 if (!first)
Chris Mason8ef97622007-03-26 10:15:30 -0400360 first = gang[0];
Chris Mason0579da42007-03-07 16:15:30 -0500361 for (i = 0; i < ret; i++) {
Chris Mason8ef97622007-03-26 10:15:30 -0400362 clear_radix_bit(pinned_radix, gang[i]);
Chris Masonbe08c1b2007-05-03 09:06:49 -0400363 try_remove_page(btree_inode->i_mapping,
364 gang[i] << (PAGE_CACHE_SHIFT -
365 btree_inode->i_blkbits));
Chris Mason0579da42007-03-07 16:15:30 -0500366 }
Chris Masona28ec192007-03-06 20:08:01 -0500367 }
368 return 0;
369}
370
Chris Masone089f052007-03-16 16:20:31 -0400371static int finish_current_insert(struct btrfs_trans_handle *trans, struct
372 btrfs_root *extent_root)
Chris Mason037e6392007-03-07 11:50:24 -0500373{
Chris Masone2fa7222007-03-12 16:22:34 -0400374 struct btrfs_key ins;
Chris Mason234b63a2007-03-13 10:46:10 -0400375 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -0500376 int i;
377 int ret;
Chris Mason1261ec42007-03-20 20:35:03 -0400378 u64 super_blocks_used;
379 struct btrfs_fs_info *info = extent_root->fs_info;
Chris Mason037e6392007-03-07 11:50:24 -0500380
Chris Masoncf27e1e2007-03-13 09:49:06 -0400381 btrfs_set_extent_refs(&extent_item, 1);
Chris Mason037e6392007-03-07 11:50:24 -0500382 ins.offset = 1;
383 ins.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -0400384 btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
Chris Mason5d0c3e62007-04-23 17:01:05 -0400385 btrfs_set_extent_owner(&extent_item, extent_root->root_key.objectid);
Chris Mason037e6392007-03-07 11:50:24 -0500386
Chris Masonf2458e12007-04-25 15:52:25 -0400387 for (i = 0; i < extent_root->fs_info->extent_tree_insert_nr; i++) {
388 ins.objectid = extent_root->fs_info->extent_tree_insert[i];
Chris Mason1261ec42007-03-20 20:35:03 -0400389 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
390 btrfs_set_super_blocks_used(info->disk_super,
391 super_blocks_used + 1);
Chris Masone089f052007-03-16 16:20:31 -0400392 ret = btrfs_insert_item(trans, extent_root, &ins, &extent_item,
393 sizeof(extent_item));
Chris Mason037e6392007-03-07 11:50:24 -0500394 BUG_ON(ret);
395 }
Chris Masonf2458e12007-04-25 15:52:25 -0400396 extent_root->fs_info->extent_tree_insert_nr = 0;
397 extent_root->fs_info->extent_tree_prealloc_nr = 0;
Chris Mason037e6392007-03-07 11:50:24 -0500398 return 0;
399}
400
Chris Mason8ef97622007-03-26 10:15:30 -0400401static int pin_down_block(struct btrfs_root *root, u64 blocknr, int pending)
Chris Masone20d96d2007-03-22 12:13:20 -0400402{
403 int err;
Chris Mason78fae272007-03-25 11:35:08 -0400404 struct btrfs_header *header;
Chris Mason8ef97622007-03-26 10:15:30 -0400405 struct buffer_head *bh;
Chris Mason78fae272007-03-25 11:35:08 -0400406
Chris Masonf4b9aa82007-03-27 11:05:53 -0400407 if (!pending) {
Chris Masond98237b2007-03-28 13:57:48 -0400408 bh = btrfs_find_tree_block(root, blocknr);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400409 if (bh) {
410 if (buffer_uptodate(bh)) {
411 u64 transid =
412 root->fs_info->running_transaction->transid;
413 header = btrfs_buffer_header(bh);
414 if (btrfs_header_generation(header) ==
415 transid) {
416 btrfs_block_release(root, bh);
417 return 0;
418 }
Chris Masonf4b9aa82007-03-27 11:05:53 -0400419 }
Chris Masond6025572007-03-30 14:27:56 -0400420 btrfs_block_release(root, bh);
Chris Mason8ef97622007-03-26 10:15:30 -0400421 }
Chris Mason8ef97622007-03-26 10:15:30 -0400422 err = set_radix_bit(&root->fs_info->pinned_radix, blocknr);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400423 } else {
424 err = set_radix_bit(&root->fs_info->pending_del_radix, blocknr);
425 }
Chris Mason8ef97622007-03-26 10:15:30 -0400426 BUG_ON(err);
Chris Masone20d96d2007-03-22 12:13:20 -0400427 return 0;
428}
429
Chris Masona28ec192007-03-06 20:08:01 -0500430/*
431 * remove an extent from the root, returns 0 on success
432 */
Chris Masone089f052007-03-16 16:20:31 -0400433static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason78fae272007-03-25 11:35:08 -0400434 *root, u64 blocknr, u64 num_blocks, int pin)
Chris Masona28ec192007-03-06 20:08:01 -0500435{
Chris Mason5caf2a02007-04-02 11:20:42 -0400436 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -0400437 struct btrfs_key key;
Chris Mason1261ec42007-03-20 20:35:03 -0400438 struct btrfs_fs_info *info = root->fs_info;
439 struct btrfs_root *extent_root = info->extent_root;
Chris Masona28ec192007-03-06 20:08:01 -0500440 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400441 struct btrfs_extent_item *ei;
Chris Masone2fa7222007-03-12 16:22:34 -0400442 struct btrfs_key ins;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400443 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500444
Chris Masona28ec192007-03-06 20:08:01 -0500445 key.objectid = blocknr;
446 key.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -0400447 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masona28ec192007-03-06 20:08:01 -0500448 key.offset = num_blocks;
449
Chris Masonbe08c1b2007-05-03 09:06:49 -0400450 find_free_extent(trans, root, 0, 0, (u64)-1, &ins, 0);
Chris Mason5caf2a02007-04-02 11:20:42 -0400451 path = btrfs_alloc_path();
452 BUG_ON(!path);
453 btrfs_init_path(path);
Chris Mason5f26f772007-04-05 10:38:44 -0400454
Chris Mason5caf2a02007-04-02 11:20:42 -0400455 ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
Chris Masona28ec192007-03-06 20:08:01 -0500456 if (ret) {
Chris Mason2e635a22007-03-21 11:12:56 -0400457 printk("failed to find %Lu\n", key.objectid);
Chris Mason234b63a2007-03-13 10:46:10 -0400458 btrfs_print_tree(extent_root, extent_root->node);
Chris Mason2e635a22007-03-21 11:12:56 -0400459 printk("failed to find %Lu\n", key.objectid);
Chris Masona28ec192007-03-06 20:08:01 -0500460 BUG();
461 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400462 ei = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
Chris Mason123abc82007-03-14 14:14:43 -0400463 struct btrfs_extent_item);
Chris Masona28ec192007-03-06 20:08:01 -0500464 BUG_ON(ei->refs == 0);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400465 refs = btrfs_extent_refs(ei) - 1;
466 btrfs_set_extent_refs(ei, refs);
Chris Mason5caf2a02007-04-02 11:20:42 -0400467 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400468 if (refs == 0) {
Chris Mason1261ec42007-03-20 20:35:03 -0400469 u64 super_blocks_used;
Chris Mason78fae272007-03-25 11:35:08 -0400470
471 if (pin) {
Chris Mason8ef97622007-03-26 10:15:30 -0400472 ret = pin_down_block(root, blocknr, 0);
Chris Mason78fae272007-03-25 11:35:08 -0400473 BUG_ON(ret);
474 }
475
Chris Mason1261ec42007-03-20 20:35:03 -0400476 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
477 btrfs_set_super_blocks_used(info->disk_super,
478 super_blocks_used - num_blocks);
Chris Mason5caf2a02007-04-02 11:20:42 -0400479 ret = btrfs_del_item(trans, extent_root, path);
Chris Masona28ec192007-03-06 20:08:01 -0500480 if (ret)
481 BUG();
Chris Mason9078a3e2007-04-26 16:46:15 -0400482 ret = update_block_group(trans, root, blocknr, num_blocks, 0);
483 BUG_ON(ret);
Chris Masona28ec192007-03-06 20:08:01 -0500484 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400485 btrfs_release_path(extent_root, path);
486 btrfs_free_path(path);
Chris Masone089f052007-03-16 16:20:31 -0400487 finish_current_insert(trans, extent_root);
Chris Masona28ec192007-03-06 20:08:01 -0500488 return ret;
489}
490
491/*
Chris Masonfec577f2007-02-26 10:40:21 -0500492 * find all the blocks marked as pending in the radix tree and remove
493 * them from the extent map
494 */
Chris Masone089f052007-03-16 16:20:31 -0400495static int del_pending_extents(struct btrfs_trans_handle *trans, struct
496 btrfs_root *extent_root)
Chris Masonfec577f2007-02-26 10:40:21 -0500497{
498 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -0400499 int wret;
500 int err = 0;
Chris Mason8ef97622007-03-26 10:15:30 -0400501 unsigned long gang[4];
Chris Masonfec577f2007-02-26 10:40:21 -0500502 int i;
Chris Mason8ef97622007-03-26 10:15:30 -0400503 struct radix_tree_root *pending_radix;
504 struct radix_tree_root *pinned_radix;
505
506 pending_radix = &extent_root->fs_info->pending_del_radix;
507 pinned_radix = &extent_root->fs_info->pinned_radix;
Chris Masonfec577f2007-02-26 10:40:21 -0500508
509 while(1) {
Chris Mason8ef97622007-03-26 10:15:30 -0400510 ret = find_first_radix_bit(pending_radix, gang,
511 ARRAY_SIZE(gang));
Chris Masonfec577f2007-02-26 10:40:21 -0500512 if (!ret)
513 break;
514 for (i = 0; i < ret; i++) {
Chris Mason8ef97622007-03-26 10:15:30 -0400515 wret = set_radix_bit(pinned_radix, gang[i]);
516 BUG_ON(wret);
517 wret = clear_radix_bit(pending_radix, gang[i]);
518 BUG_ON(wret);
Chris Masond5719762007-03-23 10:01:08 -0400519 wret = __free_extent(trans, extent_root,
Chris Mason8ef97622007-03-26 10:15:30 -0400520 gang[i], 1, 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400521 if (wret)
522 err = wret;
Chris Masonfec577f2007-02-26 10:40:21 -0500523 }
524 }
Chris Masone20d96d2007-03-22 12:13:20 -0400525 return err;
Chris Masonfec577f2007-02-26 10:40:21 -0500526}
527
528/*
529 * remove an extent from the root, returns 0 on success
530 */
Chris Masone089f052007-03-16 16:20:31 -0400531int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
532 *root, u64 blocknr, u64 num_blocks, int pin)
Chris Masonfec577f2007-02-26 10:40:21 -0500533{
Chris Mason9f5fae22007-03-20 14:38:32 -0400534 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Masonfec577f2007-02-26 10:40:21 -0500535 int pending_ret;
536 int ret;
Chris Masona28ec192007-03-06 20:08:01 -0500537
538 if (root == extent_root) {
Chris Mason8ef97622007-03-26 10:15:30 -0400539 pin_down_block(root, blocknr, 1);
Chris Masona28ec192007-03-06 20:08:01 -0500540 return 0;
541 }
Chris Mason78fae272007-03-25 11:35:08 -0400542 ret = __free_extent(trans, root, blocknr, num_blocks, pin);
Chris Masone20d96d2007-03-22 12:13:20 -0400543 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
Chris Masonfec577f2007-02-26 10:40:21 -0500544 return ret ? ret : pending_ret;
545}
546
547/*
548 * walks the btree of allocated extents and find a hole of a given size.
549 * The key ins is changed to record the hole:
550 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -0400551 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -0500552 * ins->offset == number of blocks
553 * Any available blocks before search_start are skipped.
554 */
Chris Masone089f052007-03-16 16:20:31 -0400555static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
556 *orig_root, u64 num_blocks, u64 search_start, u64
Chris Masonbe08c1b2007-05-03 09:06:49 -0400557 search_end, struct btrfs_key *ins, int data)
Chris Masonfec577f2007-02-26 10:40:21 -0500558{
Chris Mason5caf2a02007-04-02 11:20:42 -0400559 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -0400560 struct btrfs_key key;
Chris Masonfec577f2007-02-26 10:40:21 -0500561 int ret;
562 u64 hole_size = 0;
563 int slot = 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400564 u64 last_block = 0;
Chris Mason037e6392007-03-07 11:50:24 -0500565 u64 test_block;
Chris Masonfec577f2007-02-26 10:40:21 -0500566 int start_found;
Chris Mason234b63a2007-03-13 10:46:10 -0400567 struct btrfs_leaf *l;
Chris Mason9f5fae22007-03-20 14:38:32 -0400568 struct btrfs_root * root = orig_root->fs_info->extent_root;
Chris Masonf2458e12007-04-25 15:52:25 -0400569 struct btrfs_fs_info *info = root->fs_info;
Chris Mason0579da42007-03-07 16:15:30 -0500570 int total_needed = num_blocks;
Chris Masonf2458e12007-04-25 15:52:25 -0400571 int total_found = 0;
572 int fill_prealloc = 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400573 int level;
Chris Masonbe08c1b2007-05-03 09:06:49 -0400574 struct btrfs_block_group_cache *block_group;
Chris Masonfec577f2007-02-26 10:40:21 -0500575
Chris Masonb1a4d962007-04-04 15:27:52 -0400576 path = btrfs_alloc_path();
577 ins->flags = 0;
578 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
579
Chris Masone20d96d2007-03-22 12:13:20 -0400580 level = btrfs_header_level(btrfs_buffer_header(root->node));
Chris Masonf2458e12007-04-25 15:52:25 -0400581 if (num_blocks == 0) {
582 fill_prealloc = 1;
583 num_blocks = 1;
Chris Mason308535a2007-04-28 15:17:08 -0400584 total_needed = (min(level + 1, BTRFS_MAX_LEVEL) + 2) * 3;
Chris Masonf2458e12007-04-25 15:52:25 -0400585 }
Chris Masonbe08c1b2007-05-03 09:06:49 -0400586 block_group = btrfs_find_block_group(root, trans->block_group, data);
587 if (block_group->last_alloc > search_start)
588 search_start = block_group->last_alloc;
Chris Masonfec577f2007-02-26 10:40:21 -0500589check_failed:
Chris Mason5caf2a02007-04-02 11:20:42 -0400590 btrfs_init_path(path);
Chris Masonfec577f2007-02-26 10:40:21 -0500591 ins->objectid = search_start;
592 ins->offset = 0;
Chris Masonfec577f2007-02-26 10:40:21 -0500593 start_found = 0;
Chris Mason5caf2a02007-04-02 11:20:42 -0400594 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
Chris Mason0f70abe2007-02-28 16:46:22 -0500595 if (ret < 0)
596 goto error;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500597
Chris Mason5caf2a02007-04-02 11:20:42 -0400598 if (path->slots[0] > 0)
599 path->slots[0]--;
Chris Mason0579da42007-03-07 16:15:30 -0500600
Chris Masonfec577f2007-02-26 10:40:21 -0500601 while (1) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400602 l = btrfs_buffer_leaf(path->nodes[0]);
603 slot = path->slots[0];
Chris Mason7518a232007-03-12 12:01:18 -0400604 if (slot >= btrfs_header_nritems(&l->header)) {
Chris Masonf2458e12007-04-25 15:52:25 -0400605 if (fill_prealloc) {
606 info->extent_tree_prealloc_nr = 0;
607 total_found = 0;
608 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400609 ret = btrfs_next_leaf(root, path);
Chris Masonfec577f2007-02-26 10:40:21 -0500610 if (ret == 0)
611 continue;
Chris Mason0f70abe2007-02-28 16:46:22 -0500612 if (ret < 0)
613 goto error;
Chris Masonfec577f2007-02-26 10:40:21 -0500614 if (!start_found) {
615 ins->objectid = search_start;
Chris Masonf2458e12007-04-25 15:52:25 -0400616 ins->offset = (u64)-1 - search_start;
Chris Masonfec577f2007-02-26 10:40:21 -0500617 start_found = 1;
618 goto check_pending;
619 }
620 ins->objectid = last_block > search_start ?
621 last_block : search_start;
Chris Masonf2458e12007-04-25 15:52:25 -0400622 ins->offset = (u64)-1 - ins->objectid;
Chris Masonfec577f2007-02-26 10:40:21 -0500623 goto check_pending;
624 }
Chris Masone2fa7222007-03-12 16:22:34 -0400625 btrfs_disk_key_to_cpu(&key, &l->items[slot].key);
Chris Mason9078a3e2007-04-26 16:46:15 -0400626 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY)
627 goto next;
Chris Masone2fa7222007-03-12 16:22:34 -0400628 if (key.objectid >= search_start) {
Chris Masonfec577f2007-02-26 10:40:21 -0500629 if (start_found) {
Chris Mason0579da42007-03-07 16:15:30 -0500630 if (last_block < search_start)
631 last_block = search_start;
Chris Masone2fa7222007-03-12 16:22:34 -0400632 hole_size = key.objectid - last_block;
Chris Mason28b8bb92007-04-27 11:42:05 -0400633 if (hole_size >= num_blocks) {
Chris Masonfec577f2007-02-26 10:40:21 -0500634 ins->objectid = last_block;
Chris Mason037e6392007-03-07 11:50:24 -0500635 ins->offset = hole_size;
Chris Masonfec577f2007-02-26 10:40:21 -0500636 goto check_pending;
637 }
Chris Mason0579da42007-03-07 16:15:30 -0500638 }
Chris Masonfec577f2007-02-26 10:40:21 -0500639 }
Chris Mason0579da42007-03-07 16:15:30 -0500640 start_found = 1;
Chris Masone2fa7222007-03-12 16:22:34 -0400641 last_block = key.objectid + key.offset;
Chris Mason9078a3e2007-04-26 16:46:15 -0400642next:
Chris Mason5caf2a02007-04-02 11:20:42 -0400643 path->slots[0]++;
Chris Masonfec577f2007-02-26 10:40:21 -0500644 }
645 // FIXME -ENOSPC
646check_pending:
647 /* we have to make sure we didn't find an extent that has already
648 * been allocated by the map tree or the original allocation
649 */
Chris Mason5caf2a02007-04-02 11:20:42 -0400650 btrfs_release_path(root, path);
Chris Masonfec577f2007-02-26 10:40:21 -0500651 BUG_ON(ins->objectid < search_start);
Chris Mason06a2f9f2007-04-28 08:48:10 -0400652 if (ins->objectid >= btrfs_super_total_blocks(info->disk_super)) {
653 if (search_start == 0)
654 return -ENOSPC;
655 search_start = 0;
656 goto check_failed;
657 }
Chris Mason037e6392007-03-07 11:50:24 -0500658 for (test_block = ins->objectid;
Chris Masonf2458e12007-04-25 15:52:25 -0400659 test_block < ins->objectid + num_blocks; test_block++) {
660 if (test_radix_bit(&info->pinned_radix, test_block)) {
Chris Mason037e6392007-03-07 11:50:24 -0500661 search_start = test_block + 1;
Chris Masonfec577f2007-02-26 10:40:21 -0500662 goto check_failed;
663 }
664 }
Chris Masonf2458e12007-04-25 15:52:25 -0400665 if (!fill_prealloc && info->extent_tree_insert_nr) {
666 u64 last =
667 info->extent_tree_insert[info->extent_tree_insert_nr - 1];
668 if (ins->objectid + num_blocks >
669 info->extent_tree_insert[0] &&
670 ins->objectid <= last) {
671 search_start = last + 1;
672 WARN_ON(1);
673 goto check_failed;
674 }
675 }
676 if (!fill_prealloc && info->extent_tree_prealloc_nr) {
677 u64 first =
678 info->extent_tree_prealloc[info->extent_tree_prealloc_nr - 1];
679 if (ins->objectid + num_blocks > first &&
680 ins->objectid <= info->extent_tree_prealloc[0]) {
681 search_start = info->extent_tree_prealloc[0] + 1;
682 WARN_ON(1);
683 goto check_failed;
684 }
685 }
686 if (fill_prealloc) {
687 int nr;
688 test_block = ins->objectid;
689 while(test_block < ins->objectid + ins->offset &&
690 total_found < total_needed) {
691 nr = total_needed - total_found - 1;
692 BUG_ON(nr < 0);
Chris Masoncd1bc462007-04-27 10:08:34 -0400693 info->extent_tree_prealloc[nr] = test_block;
Chris Masonf2458e12007-04-25 15:52:25 -0400694 total_found++;
695 test_block++;
696 }
697 if (total_found < total_needed) {
698 search_start = test_block;
699 goto check_failed;
700 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400701 info->extent_tree_prealloc_nr = total_found;
Chris Masonf2458e12007-04-25 15:52:25 -0400702 }
Chris Masonbe08c1b2007-05-03 09:06:49 -0400703 ret = radix_tree_gang_lookup(&info->block_group_radix,
704 (void **)&block_group,
705 ins->objectid, 1);
706 if (ret) {
707 block_group->last_alloc = ins->objectid;
708 if (!data)
709 trans->block_group = block_group;
Chris Masoncd1bc462007-04-27 10:08:34 -0400710 }
Chris Mason037e6392007-03-07 11:50:24 -0500711 ins->offset = num_blocks;
Chris Mason5caf2a02007-04-02 11:20:42 -0400712 btrfs_free_path(path);
Chris Masonfec577f2007-02-26 10:40:21 -0500713 return 0;
Chris Mason0f70abe2007-02-28 16:46:22 -0500714error:
Chris Mason5caf2a02007-04-02 11:20:42 -0400715 btrfs_release_path(root, path);
716 btrfs_free_path(path);
Chris Mason0f70abe2007-02-28 16:46:22 -0500717 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -0500718}
Chris Masonfec577f2007-02-26 10:40:21 -0500719/*
Chris Masonfec577f2007-02-26 10:40:21 -0500720 * finds a free extent and does all the dirty work required for allocation
721 * returns the key for the extent through ins, and a tree buffer for
722 * the first block of the extent through buf.
723 *
724 * returns 0 if everything worked, non-zero otherwise.
725 */
Chris Mason4d775672007-04-20 20:23:12 -0400726int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
727 struct btrfs_root *root, u64 owner,
Chris Masonc62a1922007-04-24 12:07:39 -0400728 u64 num_blocks, u64 search_start,
Chris Masonbe08c1b2007-05-03 09:06:49 -0400729 u64 search_end, struct btrfs_key *ins, int data)
Chris Masonfec577f2007-02-26 10:40:21 -0500730{
731 int ret;
732 int pending_ret;
Chris Mason1261ec42007-03-20 20:35:03 -0400733 u64 super_blocks_used;
734 struct btrfs_fs_info *info = root->fs_info;
735 struct btrfs_root *extent_root = info->extent_root;
Chris Mason234b63a2007-03-13 10:46:10 -0400736 struct btrfs_extent_item extent_item;
Chris Masonf2458e12007-04-25 15:52:25 -0400737 struct btrfs_key prealloc_key;
Chris Mason037e6392007-03-07 11:50:24 -0500738
Chris Masoncf27e1e2007-03-13 09:49:06 -0400739 btrfs_set_extent_refs(&extent_item, 1);
Chris Mason4d775672007-04-20 20:23:12 -0400740 btrfs_set_extent_owner(&extent_item, owner);
Chris Masonfec577f2007-02-26 10:40:21 -0500741
Chris Mason037e6392007-03-07 11:50:24 -0500742 if (root == extent_root) {
Chris Masonf2458e12007-04-25 15:52:25 -0400743 int nr;
744 BUG_ON(info->extent_tree_prealloc_nr == 0);
Chris Mason037e6392007-03-07 11:50:24 -0500745 BUG_ON(num_blocks != 1);
Chris Mason037e6392007-03-07 11:50:24 -0500746 ins->offset = 1;
Chris Masonf2458e12007-04-25 15:52:25 -0400747 info->extent_tree_prealloc_nr--;
748 nr = info->extent_tree_prealloc_nr;
749 ins->objectid = info->extent_tree_prealloc[nr];
750 info->extent_tree_insert[info->extent_tree_insert_nr++] =
751 ins->objectid;
Chris Mason9078a3e2007-04-26 16:46:15 -0400752 ret = update_block_group(trans, root,
753 ins->objectid, ins->offset, 1);
754 BUG_ON(ret);
Chris Masonfec577f2007-02-26 10:40:21 -0500755 return 0;
756 }
Chris Masonf2458e12007-04-25 15:52:25 -0400757 /* do the real allocation */
Chris Masone089f052007-03-16 16:20:31 -0400758 ret = find_free_extent(trans, root, num_blocks, search_start,
Chris Masonbe08c1b2007-05-03 09:06:49 -0400759 search_end, ins, data);
Chris Mason037e6392007-03-07 11:50:24 -0500760 if (ret)
761 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -0500762
Chris Masonf2458e12007-04-25 15:52:25 -0400763 /* then do prealloc for the extent tree */
764 ret = find_free_extent(trans, root, 0, ins->objectid + ins->offset,
Chris Masonbe08c1b2007-05-03 09:06:49 -0400765 search_end, &prealloc_key, 0);
Chris Masonf2458e12007-04-25 15:52:25 -0400766 if (ret)
767 return ret;
768
Chris Mason1261ec42007-03-20 20:35:03 -0400769 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
770 btrfs_set_super_blocks_used(info->disk_super, super_blocks_used +
771 num_blocks);
Chris Masone089f052007-03-16 16:20:31 -0400772 ret = btrfs_insert_item(trans, extent_root, ins, &extent_item,
773 sizeof(extent_item));
Chris Mason037e6392007-03-07 11:50:24 -0500774
Chris Masone089f052007-03-16 16:20:31 -0400775 finish_current_insert(trans, extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -0400776 pending_ret = del_pending_extents(trans, extent_root);
Chris Mason037e6392007-03-07 11:50:24 -0500777 if (ret)
778 return ret;
779 if (pending_ret)
780 return pending_ret;
Chris Mason9078a3e2007-04-26 16:46:15 -0400781 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1);
Chris Mason037e6392007-03-07 11:50:24 -0500782 return 0;
Chris Masonfec577f2007-02-26 10:40:21 -0500783}
784
785/*
786 * helper function to allocate a block for a given tree
787 * returns the tree buffer or NULL.
788 */
Chris Masone20d96d2007-03-22 12:13:20 -0400789struct buffer_head *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Chris Mason31f3c992007-04-30 15:25:45 -0400790 struct btrfs_root *root, u64 hint)
Chris Masonfec577f2007-02-26 10:40:21 -0500791{
Chris Masone2fa7222007-03-12 16:22:34 -0400792 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -0500793 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -0400794 struct buffer_head *buf;
Chris Masonfec577f2007-02-26 10:40:21 -0500795
Chris Mason4d775672007-04-20 20:23:12 -0400796 ret = btrfs_alloc_extent(trans, root, root->root_key.objectid,
Chris Masonbe08c1b2007-05-03 09:06:49 -0400797 1, 0, (unsigned long)-1, &ins, 0);
Chris Masonfec577f2007-02-26 10:40:21 -0500798 if (ret) {
799 BUG();
800 return NULL;
801 }
Chris Mason9078a3e2007-04-26 16:46:15 -0400802 BUG_ON(ret);
Chris Masond98237b2007-03-28 13:57:48 -0400803 buf = btrfs_find_create_tree_block(root, ins.objectid);
Chris Masondf2ce342007-03-23 11:00:45 -0400804 set_buffer_uptodate(buf);
Chris Mason090d1872007-05-01 08:53:32 -0400805 set_buffer_checked(buf);
Chris Mason7c4452b2007-04-28 09:29:35 -0400806 set_radix_bit(&trans->transaction->dirty_pages, buf->b_page->index);
Chris Masonfec577f2007-02-26 10:40:21 -0500807 return buf;
808}
Chris Masona28ec192007-03-06 20:08:01 -0500809
Chris Mason6407bf62007-03-27 06:33:00 -0400810static int drop_leaf_ref(struct btrfs_trans_handle *trans,
811 struct btrfs_root *root, struct buffer_head *cur)
812{
813 struct btrfs_disk_key *key;
814 struct btrfs_leaf *leaf;
815 struct btrfs_file_extent_item *fi;
816 int i;
817 int nritems;
818 int ret;
819
820 BUG_ON(!btrfs_is_leaf(btrfs_buffer_node(cur)));
821 leaf = btrfs_buffer_leaf(cur);
822 nritems = btrfs_header_nritems(&leaf->header);
823 for (i = 0; i < nritems; i++) {
824 key = &leaf->items[i].key;
825 if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY)
826 continue;
827 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
Chris Mason236454d2007-04-19 13:37:44 -0400828 if (btrfs_file_extent_type(fi) == BTRFS_FILE_EXTENT_INLINE)
829 continue;
Chris Mason6407bf62007-03-27 06:33:00 -0400830 /*
831 * FIXME make sure to insert a trans record that
832 * repeats the snapshot del on crash
833 */
834 ret = btrfs_free_extent(trans, root,
835 btrfs_file_extent_disk_blocknr(fi),
836 btrfs_file_extent_disk_num_blocks(fi),
837 0);
838 BUG_ON(ret);
839 }
840 return 0;
841}
842
Chris Mason9aca1d52007-03-13 11:09:37 -0400843/*
844 * helper function for drop_snapshot, this walks down the tree dropping ref
845 * counts as it goes.
846 */
Chris Masone089f052007-03-16 16:20:31 -0400847static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
848 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -0500849{
Chris Masone20d96d2007-03-22 12:13:20 -0400850 struct buffer_head *next;
851 struct buffer_head *cur;
Chris Mason20524f02007-03-10 06:35:47 -0500852 u64 blocknr;
853 int ret;
854 u32 refs;
855
Chris Mason5caf2a02007-04-02 11:20:42 -0400856 WARN_ON(*level < 0);
857 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Masonb18c6682007-04-17 13:26:50 -0400858 ret = lookup_extent_ref(trans, root, bh_blocknr(path->nodes[*level]),
Chris Mason6407bf62007-03-27 06:33:00 -0400859 1, &refs);
Chris Mason20524f02007-03-10 06:35:47 -0500860 BUG_ON(ret);
861 if (refs > 1)
862 goto out;
Chris Mason9aca1d52007-03-13 11:09:37 -0400863 /*
864 * walk down to the last node level and free all the leaves
865 */
Chris Mason6407bf62007-03-27 06:33:00 -0400866 while(*level >= 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400867 WARN_ON(*level < 0);
868 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason20524f02007-03-10 06:35:47 -0500869 cur = path->nodes[*level];
Chris Mason2c90e5d2007-04-02 10:50:19 -0400870 if (btrfs_header_level(btrfs_buffer_header(cur)) != *level)
871 WARN_ON(1);
Chris Mason7518a232007-03-12 12:01:18 -0400872 if (path->slots[*level] >=
Chris Masone20d96d2007-03-22 12:13:20 -0400873 btrfs_header_nritems(btrfs_buffer_header(cur)))
Chris Mason20524f02007-03-10 06:35:47 -0500874 break;
Chris Mason6407bf62007-03-27 06:33:00 -0400875 if (*level == 0) {
876 ret = drop_leaf_ref(trans, root, cur);
877 BUG_ON(ret);
878 break;
879 }
Chris Masone20d96d2007-03-22 12:13:20 -0400880 blocknr = btrfs_node_blockptr(btrfs_buffer_node(cur),
881 path->slots[*level]);
Chris Masonb18c6682007-04-17 13:26:50 -0400882 ret = lookup_extent_ref(trans, root, blocknr, 1, &refs);
Chris Mason6407bf62007-03-27 06:33:00 -0400883 BUG_ON(ret);
884 if (refs != 1) {
Chris Mason20524f02007-03-10 06:35:47 -0500885 path->slots[*level]++;
Chris Masone089f052007-03-16 16:20:31 -0400886 ret = btrfs_free_extent(trans, root, blocknr, 1, 1);
Chris Mason20524f02007-03-10 06:35:47 -0500887 BUG_ON(ret);
888 continue;
889 }
Chris Mason20524f02007-03-10 06:35:47 -0500890 next = read_tree_block(root, blocknr);
Chris Mason5caf2a02007-04-02 11:20:42 -0400891 WARN_ON(*level <= 0);
Chris Mason83e15a22007-03-12 09:03:27 -0400892 if (path->nodes[*level-1])
Chris Mason234b63a2007-03-13 10:46:10 -0400893 btrfs_block_release(root, path->nodes[*level-1]);
Chris Mason20524f02007-03-10 06:35:47 -0500894 path->nodes[*level-1] = next;
Chris Masone20d96d2007-03-22 12:13:20 -0400895 *level = btrfs_header_level(btrfs_buffer_header(next));
Chris Mason20524f02007-03-10 06:35:47 -0500896 path->slots[*level] = 0;
897 }
898out:
Chris Mason5caf2a02007-04-02 11:20:42 -0400899 WARN_ON(*level < 0);
900 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason6407bf62007-03-27 06:33:00 -0400901 ret = btrfs_free_extent(trans, root,
Chris Mason7eccb902007-04-11 15:53:25 -0400902 bh_blocknr(path->nodes[*level]), 1, 1);
Chris Mason234b63a2007-03-13 10:46:10 -0400903 btrfs_block_release(root, path->nodes[*level]);
Chris Mason20524f02007-03-10 06:35:47 -0500904 path->nodes[*level] = NULL;
905 *level += 1;
906 BUG_ON(ret);
907 return 0;
908}
909
Chris Mason9aca1d52007-03-13 11:09:37 -0400910/*
911 * helper for dropping snapshots. This walks back up the tree in the path
912 * to find the first node higher up where we haven't yet gone through
913 * all the slots
914 */
Chris Masone089f052007-03-16 16:20:31 -0400915static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root
916 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -0500917{
918 int i;
919 int slot;
920 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400921 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Mason20524f02007-03-10 06:35:47 -0500922 slot = path->slots[i];
Chris Masone20d96d2007-03-22 12:13:20 -0400923 if (slot < btrfs_header_nritems(
924 btrfs_buffer_header(path->nodes[i])) - 1) {
Chris Mason20524f02007-03-10 06:35:47 -0500925 path->slots[i]++;
926 *level = i;
927 return 0;
928 } else {
Chris Masone089f052007-03-16 16:20:31 -0400929 ret = btrfs_free_extent(trans, root,
Chris Mason7eccb902007-04-11 15:53:25 -0400930 bh_blocknr(path->nodes[*level]),
Chris Masone089f052007-03-16 16:20:31 -0400931 1, 1);
Chris Mason6407bf62007-03-27 06:33:00 -0400932 BUG_ON(ret);
Chris Mason234b63a2007-03-13 10:46:10 -0400933 btrfs_block_release(root, path->nodes[*level]);
Chris Mason83e15a22007-03-12 09:03:27 -0400934 path->nodes[*level] = NULL;
Chris Mason20524f02007-03-10 06:35:47 -0500935 *level = i + 1;
Chris Mason20524f02007-03-10 06:35:47 -0500936 }
937 }
938 return 1;
939}
940
Chris Mason9aca1d52007-03-13 11:09:37 -0400941/*
942 * drop the reference count on the tree rooted at 'snap'. This traverses
943 * the tree freeing any blocks that have a ref count of zero after being
944 * decremented.
945 */
Chris Masone089f052007-03-16 16:20:31 -0400946int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masone20d96d2007-03-22 12:13:20 -0400947 *root, struct buffer_head *snap)
Chris Mason20524f02007-03-10 06:35:47 -0500948{
Chris Mason3768f362007-03-13 16:47:54 -0400949 int ret = 0;
Chris Mason9aca1d52007-03-13 11:09:37 -0400950 int wret;
Chris Mason20524f02007-03-10 06:35:47 -0500951 int level;
Chris Mason5caf2a02007-04-02 11:20:42 -0400952 struct btrfs_path *path;
Chris Mason20524f02007-03-10 06:35:47 -0500953 int i;
954 int orig_level;
955
Chris Mason5caf2a02007-04-02 11:20:42 -0400956 path = btrfs_alloc_path();
957 BUG_ON(!path);
958 btrfs_init_path(path);
Chris Mason20524f02007-03-10 06:35:47 -0500959
Chris Masone20d96d2007-03-22 12:13:20 -0400960 level = btrfs_header_level(btrfs_buffer_header(snap));
Chris Mason20524f02007-03-10 06:35:47 -0500961 orig_level = level;
Chris Mason5caf2a02007-04-02 11:20:42 -0400962 path->nodes[level] = snap;
963 path->slots[level] = 0;
Chris Mason20524f02007-03-10 06:35:47 -0500964 while(1) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400965 wret = walk_down_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -0400966 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -0500967 break;
Chris Mason9aca1d52007-03-13 11:09:37 -0400968 if (wret < 0)
969 ret = wret;
970
Chris Mason5caf2a02007-04-02 11:20:42 -0400971 wret = walk_up_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -0400972 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -0500973 break;
Chris Mason9aca1d52007-03-13 11:09:37 -0400974 if (wret < 0)
975 ret = wret;
Chris Mason35b7e472007-05-02 15:53:43 -0400976 btrfs_btree_balance_dirty(root);
Chris Mason20524f02007-03-10 06:35:47 -0500977 }
Chris Mason83e15a22007-03-12 09:03:27 -0400978 for (i = 0; i <= orig_level; i++) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400979 if (path->nodes[i]) {
980 btrfs_block_release(root, path->nodes[i]);
Chris Mason83e15a22007-03-12 09:03:27 -0400981 }
Chris Mason20524f02007-03-10 06:35:47 -0500982 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400983 btrfs_free_path(path);
Chris Mason9aca1d52007-03-13 11:09:37 -0400984 return ret;
Chris Mason20524f02007-03-10 06:35:47 -0500985}
Chris Mason9078a3e2007-04-26 16:46:15 -0400986
987int btrfs_free_block_groups(struct btrfs_fs_info *info)
988{
989 int ret;
990 struct btrfs_block_group_cache *cache[8];
991 int i;
992
993 while(1) {
994 ret = radix_tree_gang_lookup(&info->block_group_radix,
995 (void **)cache, 0,
996 ARRAY_SIZE(cache));
997 if (!ret)
998 break;
999 for (i = 0; i < ret; i++) {
1000 radix_tree_delete(&info->block_group_radix,
1001 cache[i]->key.objectid +
1002 cache[i]->key.offset - 1);
1003 kfree(cache[i]);
1004 }
1005 }
1006 return 0;
1007}
1008
1009int btrfs_read_block_groups(struct btrfs_root *root)
1010{
1011 struct btrfs_path *path;
1012 int ret;
1013 int err = 0;
1014 struct btrfs_block_group_item *bi;
1015 struct btrfs_block_group_cache *cache;
1016 struct btrfs_key key;
1017 struct btrfs_key found_key;
1018 struct btrfs_leaf *leaf;
1019 u64 group_size_blocks = BTRFS_BLOCK_GROUP_SIZE / root->blocksize;
Chris Mason31f3c992007-04-30 15:25:45 -04001020 u64 used;
Chris Mason9078a3e2007-04-26 16:46:15 -04001021
1022 root = root->fs_info->extent_root;
1023 key.objectid = 0;
1024 key.offset = group_size_blocks;
1025 key.flags = 0;
1026 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
1027
1028 path = btrfs_alloc_path();
1029 if (!path)
1030 return -ENOMEM;
1031
1032 while(1) {
1033 ret = btrfs_search_slot(NULL, root->fs_info->extent_root,
1034 &key, path, 0, 0);
1035 if (ret != 0) {
1036 err = ret;
1037 break;
1038 }
1039 leaf = btrfs_buffer_leaf(path->nodes[0]);
1040 btrfs_disk_key_to_cpu(&found_key,
1041 &leaf->items[path->slots[0]].key);
1042 cache = kmalloc(sizeof(*cache), GFP_NOFS);
1043 if (!cache) {
1044 err = -1;
1045 break;
1046 }
1047 bi = btrfs_item_ptr(leaf, path->slots[0],
1048 struct btrfs_block_group_item);
1049 memcpy(&cache->item, bi, sizeof(*bi));
1050 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Mason31f3c992007-04-30 15:25:45 -04001051 cache->last_alloc = cache->key.objectid;
1052 cache->first_free = cache->key.objectid;
Chris Mason9078a3e2007-04-26 16:46:15 -04001053 key.objectid = found_key.objectid + found_key.offset;
1054 btrfs_release_path(root, path);
1055 ret = radix_tree_insert(&root->fs_info->block_group_radix,
1056 found_key.objectid +
1057 found_key.offset - 1,
1058 (void *)cache);
1059 BUG_ON(ret);
Chris Mason31f3c992007-04-30 15:25:45 -04001060 used = btrfs_block_group_used(bi);
1061 if (used < (key.offset * 2) / 3) {
1062 radix_tree_tag_set(&root->fs_info->block_group_radix,
1063 found_key.objectid +
1064 found_key.offset - 1,
1065 BTRFS_BLOCK_GROUP_AVAIL);
1066 }
Chris Mason9078a3e2007-04-26 16:46:15 -04001067 if (key.objectid >=
1068 btrfs_super_total_blocks(root->fs_info->disk_super))
1069 break;
1070 }
1071
1072 btrfs_free_path(path);
1073 return 0;
1074}