blob: 62051a36664a001e4836c07ba1ba70b7e9670aff [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 Masoncd1bc462007-04-27 10:08:34 -040015static int find_search_start(struct btrfs_root *root, int data)
16{
17 struct btrfs_block_group_cache *cache[8];
18 struct btrfs_fs_info *info = root->fs_info;
19 u64 used;
20 u64 last;
21 int i;
22 int ret;
23
24 cache[0] = info->block_group_cache;
25 if (!cache[0])
26 goto find_new;
27 used = btrfs_block_group_used(&cache[0]->item);
28 if (used < (cache[0]->key.offset * 3 / 2))
29 return 0;
30find_new:
31 last = 0;
32 while(1) {
33 ret = radix_tree_gang_lookup_tag(&info->block_group_radix,
34 (void **)cache,
35 last, ARRAY_SIZE(cache),
36 BTRFS_BLOCK_GROUP_DIRTY);
37 if (!ret)
38 break;
39 for (i = 0; i < ret; i++) {
40 used = btrfs_block_group_used(&cache[i]->item);
41 if (used < (cache[i]->key.offset * 3 / 2)) {
42 info->block_group_cache = cache[i];
43 cache[i]->last_alloc = cache[i]->first_free;
44 return 0;
45 }
46 last = cache[i]->key.objectid +
47 cache[i]->key.offset - 1;
48 }
49 }
50 last = 0;
51 while(1) {
52 ret = radix_tree_gang_lookup(&info->block_group_radix,
53 (void **)cache,
54 last, ARRAY_SIZE(cache));
55 if (!ret)
56 break;
57 for (i = 0; i < ret; i++) {
58 used = btrfs_block_group_used(&cache[i]->item);
59 if (used < (cache[i]->key.offset * 3 / 2)) {
60 info->block_group_cache = cache[i];
61 cache[i]->last_alloc = cache[i]->first_free;
62 return 0;
63 }
64 last = cache[i]->key.objectid +
65 cache[i]->key.offset - 1;
66 }
67 }
68 info->block_group_cache = NULL;
69 return 0;
70}
71
Chris Masonb18c6682007-04-17 13:26:50 -040072int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
73 struct btrfs_root *root,
74 u64 blocknr, u64 num_blocks)
Chris Mason02217ed2007-03-02 16:08:05 -050075{
Chris Mason5caf2a02007-04-02 11:20:42 -040076 struct btrfs_path *path;
Chris Mason02217ed2007-03-02 16:08:05 -050077 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -040078 struct btrfs_key key;
Chris Mason234b63a2007-03-13 10:46:10 -040079 struct btrfs_leaf *l;
80 struct btrfs_extent_item *item;
Chris Masone2fa7222007-03-12 16:22:34 -040081 struct btrfs_key ins;
Chris Masoncf27e1e2007-03-13 09:49:06 -040082 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -050083
Chris Mason9f5fae22007-03-20 14:38:32 -040084 find_free_extent(trans, root->fs_info->extent_root, 0, 0, (u64)-1,
85 &ins);
Chris Mason5caf2a02007-04-02 11:20:42 -040086 path = btrfs_alloc_path();
87 BUG_ON(!path);
88 btrfs_init_path(path);
Chris Mason02217ed2007-03-02 16:08:05 -050089 key.objectid = blocknr;
90 key.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -040091 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Mason6407bf62007-03-27 06:33:00 -040092 key.offset = num_blocks;
Chris Mason5caf2a02007-04-02 11:20:42 -040093 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -040094 0, 1);
Chris Masona429e512007-04-18 16:15:28 -040095 if (ret != 0) {
96printk("can't find block %Lu %Lu\n", blocknr, num_blocks);
Chris Masona28ec192007-03-06 20:08:01 -050097 BUG();
Chris Masona429e512007-04-18 16:15:28 -040098 }
Chris Mason02217ed2007-03-02 16:08:05 -050099 BUG_ON(ret != 0);
Chris Mason5caf2a02007-04-02 11:20:42 -0400100 l = btrfs_buffer_leaf(path->nodes[0]);
101 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400102 refs = btrfs_extent_refs(item);
103 btrfs_set_extent_refs(item, refs + 1);
Chris Mason5caf2a02007-04-02 11:20:42 -0400104 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masona28ec192007-03-06 20:08:01 -0500105
Chris Mason5caf2a02007-04-02 11:20:42 -0400106 btrfs_release_path(root->fs_info->extent_root, path);
107 btrfs_free_path(path);
Chris Mason9f5fae22007-03-20 14:38:32 -0400108 finish_current_insert(trans, root->fs_info->extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -0400109 del_pending_extents(trans, root->fs_info->extent_root);
Chris Mason02217ed2007-03-02 16:08:05 -0500110 return 0;
111}
112
Chris Masonb18c6682007-04-17 13:26:50 -0400113static int lookup_extent_ref(struct btrfs_trans_handle *trans,
114 struct btrfs_root *root, u64 blocknr,
115 u64 num_blocks, u32 *refs)
Chris Masona28ec192007-03-06 20:08:01 -0500116{
Chris Mason5caf2a02007-04-02 11:20:42 -0400117 struct btrfs_path *path;
Chris Masona28ec192007-03-06 20:08:01 -0500118 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400119 struct btrfs_key key;
Chris Mason234b63a2007-03-13 10:46:10 -0400120 struct btrfs_leaf *l;
121 struct btrfs_extent_item *item;
Chris Mason5caf2a02007-04-02 11:20:42 -0400122
123 path = btrfs_alloc_path();
124 btrfs_init_path(path);
Chris Masona28ec192007-03-06 20:08:01 -0500125 key.objectid = blocknr;
Chris Mason6407bf62007-03-27 06:33:00 -0400126 key.offset = num_blocks;
Chris Mason62e27492007-03-15 12:56:47 -0400127 key.flags = 0;
128 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Mason5caf2a02007-04-02 11:20:42 -0400129 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -0400130 0, 0);
Chris Masona28ec192007-03-06 20:08:01 -0500131 if (ret != 0)
132 BUG();
Chris Mason5caf2a02007-04-02 11:20:42 -0400133 l = btrfs_buffer_leaf(path->nodes[0]);
134 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400135 *refs = btrfs_extent_refs(item);
Chris Mason5caf2a02007-04-02 11:20:42 -0400136 btrfs_release_path(root->fs_info->extent_root, path);
137 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -0500138 return 0;
139}
140
Chris Masonc5739bb2007-04-10 09:27:04 -0400141int btrfs_inc_root_ref(struct btrfs_trans_handle *trans,
142 struct btrfs_root *root)
143{
Chris Masonb18c6682007-04-17 13:26:50 -0400144 return btrfs_inc_extent_ref(trans, root, bh_blocknr(root->node), 1);
Chris Masonc5739bb2007-04-10 09:27:04 -0400145}
146
Chris Masone089f052007-03-16 16:20:31 -0400147int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Masone20d96d2007-03-22 12:13:20 -0400148 struct buffer_head *buf)
Chris Mason02217ed2007-03-02 16:08:05 -0500149{
150 u64 blocknr;
Chris Masone20d96d2007-03-22 12:13:20 -0400151 struct btrfs_node *buf_node;
Chris Mason6407bf62007-03-27 06:33:00 -0400152 struct btrfs_leaf *buf_leaf;
153 struct btrfs_disk_key *key;
154 struct btrfs_file_extent_item *fi;
Chris Mason02217ed2007-03-02 16:08:05 -0500155 int i;
Chris Mason6407bf62007-03-27 06:33:00 -0400156 int leaf;
157 int ret;
Chris Masona28ec192007-03-06 20:08:01 -0500158
Chris Mason3768f362007-03-13 16:47:54 -0400159 if (!root->ref_cows)
Chris Masona28ec192007-03-06 20:08:01 -0500160 return 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400161 buf_node = btrfs_buffer_node(buf);
Chris Mason6407bf62007-03-27 06:33:00 -0400162 leaf = btrfs_is_leaf(buf_node);
163 buf_leaf = btrfs_buffer_leaf(buf);
Chris Masone20d96d2007-03-22 12:13:20 -0400164 for (i = 0; i < btrfs_header_nritems(&buf_node->header); i++) {
Chris Mason6407bf62007-03-27 06:33:00 -0400165 if (leaf) {
166 key = &buf_leaf->items[i].key;
167 if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY)
168 continue;
169 fi = btrfs_item_ptr(buf_leaf, i,
170 struct btrfs_file_extent_item);
Chris Mason236454df2007-04-19 13:37:44 -0400171 if (btrfs_file_extent_type(fi) ==
172 BTRFS_FILE_EXTENT_INLINE)
173 continue;
Chris Masonb18c6682007-04-17 13:26:50 -0400174 ret = btrfs_inc_extent_ref(trans, root,
Chris Mason6407bf62007-03-27 06:33:00 -0400175 btrfs_file_extent_disk_blocknr(fi),
176 btrfs_file_extent_disk_num_blocks(fi));
177 BUG_ON(ret);
178 } else {
179 blocknr = btrfs_node_blockptr(buf_node, i);
Chris Masonb18c6682007-04-17 13:26:50 -0400180 ret = btrfs_inc_extent_ref(trans, root, blocknr, 1);
Chris Mason6407bf62007-03-27 06:33:00 -0400181 BUG_ON(ret);
182 }
Chris Mason02217ed2007-03-02 16:08:05 -0500183 }
184 return 0;
185}
186
Chris Mason9078a3e2007-04-26 16:46:15 -0400187static int write_one_cache_group(struct btrfs_trans_handle *trans,
188 struct btrfs_root *root,
189 struct btrfs_path *path,
190 struct btrfs_block_group_cache *cache)
191{
192 int ret;
193 int pending_ret;
194 struct btrfs_root *extent_root = root->fs_info->extent_root;
195 struct btrfs_block_group_item *bi;
196 struct btrfs_key ins;
197
198 find_free_extent(trans, extent_root, 0, 0, (u64)-1, &ins);
199 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
200 BUG_ON(ret);
201 bi = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
202 struct btrfs_block_group_item);
203 memcpy(bi, &cache->item, sizeof(*bi));
204 mark_buffer_dirty(path->nodes[0]);
205 btrfs_release_path(extent_root, path);
206
207 finish_current_insert(trans, extent_root);
208 pending_ret = del_pending_extents(trans, extent_root);
209 if (ret)
210 return ret;
211 if (pending_ret)
212 return pending_ret;
213 return 0;
214
215}
216
217int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
218 struct btrfs_root *root)
219{
220 struct btrfs_block_group_cache *cache[8];
221 int ret;
222 int err = 0;
223 int werr = 0;
224 struct radix_tree_root *radix = &root->fs_info->block_group_radix;
225 int i;
226 struct btrfs_path *path;
227
228 path = btrfs_alloc_path();
229 if (!path)
230 return -ENOMEM;
231
232 while(1) {
233 ret = radix_tree_gang_lookup_tag(radix, (void **)cache,
234 0, ARRAY_SIZE(cache),
235 BTRFS_BLOCK_GROUP_DIRTY);
236 if (!ret)
237 break;
238 for (i = 0; i < ret; i++) {
239 radix_tree_tag_clear(radix, cache[i]->key.objectid +
240 cache[i]->key.offset - 1,
241 BTRFS_BLOCK_GROUP_DIRTY);
242 err = write_one_cache_group(trans, root,
243 path, cache[i]);
244 if (err)
245 werr = err;
246 }
247 }
248 btrfs_free_path(path);
249 return werr;
250}
251
252static int update_block_group(struct btrfs_trans_handle *trans,
253 struct btrfs_root *root,
254 u64 blocknr, u64 num, int alloc)
255{
256 struct btrfs_block_group_cache *cache;
257 struct btrfs_fs_info *info = root->fs_info;
258 u64 total = num;
259 u64 old_val;
260 u64 block_in_group;
261 int ret;
262 while(total) {
263 ret = radix_tree_gang_lookup(&info->block_group_radix,
264 (void **)&cache, blocknr, 1);
Chris Masoncd1bc462007-04-27 10:08:34 -0400265 if (!ret) {
266 printk(KERN_CRIT "blocknr %Lu lookup failed\n",
267 blocknr);
Chris Mason9078a3e2007-04-26 16:46:15 -0400268 return -1;
Chris Masoncd1bc462007-04-27 10:08:34 -0400269 }
Chris Mason9078a3e2007-04-26 16:46:15 -0400270 block_in_group = blocknr - cache->key.objectid;
271 WARN_ON(block_in_group > cache->key.offset);
272 radix_tree_tag_set(&info->block_group_radix,
273 cache->key.objectid + cache->key.offset - 1,
274 BTRFS_BLOCK_GROUP_DIRTY);
275
276 old_val = btrfs_block_group_used(&cache->item);
277 num = min(total, cache->key.offset - block_in_group);
278 total -= num;
279 blocknr += num;
Chris Masoncd1bc462007-04-27 10:08:34 -0400280 if (alloc) {
Chris Mason9078a3e2007-04-26 16:46:15 -0400281 old_val += num;
Chris Masoncd1bc462007-04-27 10:08:34 -0400282 if (blocknr > cache->last_alloc)
283 cache->last_alloc = blocknr;
284 } else {
Chris Mason9078a3e2007-04-26 16:46:15 -0400285 old_val -= num;
Chris Masoncd1bc462007-04-27 10:08:34 -0400286 if (blocknr < cache->first_free)
287 cache->first_free = blocknr;
288 }
Chris Mason9078a3e2007-04-26 16:46:15 -0400289 btrfs_set_block_group_used(&cache->item, old_val);
290 }
291 return 0;
292}
293
Chris Mason06a2f9f2007-04-28 08:48:10 -0400294static int try_remove_page(struct address_space *mapping, unsigned long index)
295{
296 int ret;
297 ret = invalidate_mapping_pages(mapping, index, index);
298 return ret;
299}
300
Chris Masone089f052007-03-16 16:20:31 -0400301int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, struct
302 btrfs_root *root)
Chris Masona28ec192007-03-06 20:08:01 -0500303{
Chris Mason8ef97622007-03-26 10:15:30 -0400304 unsigned long gang[8];
Chris Mason06a2f9f2007-04-28 08:48:10 -0400305 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Mason88fd1462007-03-16 08:56:18 -0400306 u64 first = 0;
Chris Masona28ec192007-03-06 20:08:01 -0500307 int ret;
308 int i;
Chris Mason8ef97622007-03-26 10:15:30 -0400309 struct radix_tree_root *pinned_radix = &root->fs_info->pinned_radix;
Chris Masona28ec192007-03-06 20:08:01 -0500310
311 while(1) {
Chris Mason8ef97622007-03-26 10:15:30 -0400312 ret = find_first_radix_bit(pinned_radix, gang,
313 ARRAY_SIZE(gang));
Chris Masona28ec192007-03-06 20:08:01 -0500314 if (!ret)
315 break;
Chris Mason88fd1462007-03-16 08:56:18 -0400316 if (!first)
Chris Mason8ef97622007-03-26 10:15:30 -0400317 first = gang[0];
Chris Mason0579da42007-03-07 16:15:30 -0500318 for (i = 0; i < ret; i++) {
Chris Mason8ef97622007-03-26 10:15:30 -0400319 clear_radix_bit(pinned_radix, gang[i]);
Chris Mason06a2f9f2007-04-28 08:48:10 -0400320 try_remove_page(btree_inode->i_mapping,
321 gang[i] << (PAGE_CACHE_SHIFT -
322 btree_inode->i_blkbits));
Chris Mason0579da42007-03-07 16:15:30 -0500323 }
Chris Masona28ec192007-03-06 20:08:01 -0500324 }
Chris Mason28b8bb92007-04-27 11:42:05 -0400325 if (root->fs_info->block_group_cache) {
326 root->fs_info->block_group_cache->last_alloc =
327 root->fs_info->block_group_cache->first_free;
328 }
Chris Masona28ec192007-03-06 20:08:01 -0500329 return 0;
330}
331
Chris Masone089f052007-03-16 16:20:31 -0400332static int finish_current_insert(struct btrfs_trans_handle *trans, struct
333 btrfs_root *extent_root)
Chris Mason037e6392007-03-07 11:50:24 -0500334{
Chris Masone2fa7222007-03-12 16:22:34 -0400335 struct btrfs_key ins;
Chris Mason234b63a2007-03-13 10:46:10 -0400336 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -0500337 int i;
338 int ret;
Chris Mason1261ec42007-03-20 20:35:03 -0400339 u64 super_blocks_used;
340 struct btrfs_fs_info *info = extent_root->fs_info;
Chris Mason037e6392007-03-07 11:50:24 -0500341
Chris Masoncf27e1e2007-03-13 09:49:06 -0400342 btrfs_set_extent_refs(&extent_item, 1);
Chris Mason037e6392007-03-07 11:50:24 -0500343 ins.offset = 1;
344 ins.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -0400345 btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
Chris Mason5d0c3e62007-04-23 17:01:05 -0400346 btrfs_set_extent_owner(&extent_item, extent_root->root_key.objectid);
Chris Mason037e6392007-03-07 11:50:24 -0500347
Chris Masonf2458e12007-04-25 15:52:25 -0400348 for (i = 0; i < extent_root->fs_info->extent_tree_insert_nr; i++) {
349 ins.objectid = extent_root->fs_info->extent_tree_insert[i];
Chris Mason1261ec42007-03-20 20:35:03 -0400350 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
351 btrfs_set_super_blocks_used(info->disk_super,
352 super_blocks_used + 1);
Chris Masone089f052007-03-16 16:20:31 -0400353 ret = btrfs_insert_item(trans, extent_root, &ins, &extent_item,
354 sizeof(extent_item));
Chris Mason037e6392007-03-07 11:50:24 -0500355 BUG_ON(ret);
356 }
Chris Masonf2458e12007-04-25 15:52:25 -0400357 extent_root->fs_info->extent_tree_insert_nr = 0;
358 extent_root->fs_info->extent_tree_prealloc_nr = 0;
Chris Mason037e6392007-03-07 11:50:24 -0500359 return 0;
360}
361
Chris Mason8ef97622007-03-26 10:15:30 -0400362static int pin_down_block(struct btrfs_root *root, u64 blocknr, int pending)
Chris Masone20d96d2007-03-22 12:13:20 -0400363{
364 int err;
Chris Mason78fae272007-03-25 11:35:08 -0400365 struct btrfs_header *header;
Chris Mason8ef97622007-03-26 10:15:30 -0400366 struct buffer_head *bh;
Chris Mason78fae272007-03-25 11:35:08 -0400367
Chris Masonf4b9aa82007-03-27 11:05:53 -0400368 if (!pending) {
Chris Masond98237b2007-03-28 13:57:48 -0400369 bh = btrfs_find_tree_block(root, blocknr);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400370 if (bh) {
371 if (buffer_uptodate(bh)) {
372 u64 transid =
373 root->fs_info->running_transaction->transid;
374 header = btrfs_buffer_header(bh);
375 if (btrfs_header_generation(header) ==
376 transid) {
377 btrfs_block_release(root, bh);
378 return 0;
379 }
Chris Masonf4b9aa82007-03-27 11:05:53 -0400380 }
Chris Masond6025572007-03-30 14:27:56 -0400381 btrfs_block_release(root, bh);
Chris Mason8ef97622007-03-26 10:15:30 -0400382 }
Chris Mason8ef97622007-03-26 10:15:30 -0400383 err = set_radix_bit(&root->fs_info->pinned_radix, blocknr);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400384 } else {
385 err = set_radix_bit(&root->fs_info->pending_del_radix, blocknr);
386 }
Chris Mason8ef97622007-03-26 10:15:30 -0400387 BUG_ON(err);
Chris Masone20d96d2007-03-22 12:13:20 -0400388 return 0;
389}
390
Chris Masona28ec192007-03-06 20:08:01 -0500391/*
392 * remove an extent from the root, returns 0 on success
393 */
Chris Masone089f052007-03-16 16:20:31 -0400394static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason78fae272007-03-25 11:35:08 -0400395 *root, u64 blocknr, u64 num_blocks, int pin)
Chris Masona28ec192007-03-06 20:08:01 -0500396{
Chris Mason5caf2a02007-04-02 11:20:42 -0400397 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -0400398 struct btrfs_key key;
Chris Mason1261ec42007-03-20 20:35:03 -0400399 struct btrfs_fs_info *info = root->fs_info;
400 struct btrfs_root *extent_root = info->extent_root;
Chris Masona28ec192007-03-06 20:08:01 -0500401 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400402 struct btrfs_extent_item *ei;
Chris Masone2fa7222007-03-12 16:22:34 -0400403 struct btrfs_key ins;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400404 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500405
Chris Masona28ec192007-03-06 20:08:01 -0500406 key.objectid = blocknr;
407 key.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -0400408 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masona28ec192007-03-06 20:08:01 -0500409 key.offset = num_blocks;
410
Chris Masone089f052007-03-16 16:20:31 -0400411 find_free_extent(trans, root, 0, 0, (u64)-1, &ins);
Chris Mason5caf2a02007-04-02 11:20:42 -0400412 path = btrfs_alloc_path();
413 BUG_ON(!path);
414 btrfs_init_path(path);
Chris Mason5f26f772007-04-05 10:38:44 -0400415
Chris Mason5caf2a02007-04-02 11:20:42 -0400416 ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
Chris Masona28ec192007-03-06 20:08:01 -0500417 if (ret) {
Chris Mason2e635a22007-03-21 11:12:56 -0400418 printk("failed to find %Lu\n", key.objectid);
Chris Mason234b63a2007-03-13 10:46:10 -0400419 btrfs_print_tree(extent_root, extent_root->node);
Chris Mason2e635a22007-03-21 11:12:56 -0400420 printk("failed to find %Lu\n", key.objectid);
Chris Masona28ec192007-03-06 20:08:01 -0500421 BUG();
422 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400423 ei = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
Chris Mason123abc82007-03-14 14:14:43 -0400424 struct btrfs_extent_item);
Chris Masona28ec192007-03-06 20:08:01 -0500425 BUG_ON(ei->refs == 0);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400426 refs = btrfs_extent_refs(ei) - 1;
427 btrfs_set_extent_refs(ei, refs);
Chris Mason5caf2a02007-04-02 11:20:42 -0400428 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400429 if (refs == 0) {
Chris Mason1261ec42007-03-20 20:35:03 -0400430 u64 super_blocks_used;
Chris Mason78fae272007-03-25 11:35:08 -0400431
432 if (pin) {
Chris Mason8ef97622007-03-26 10:15:30 -0400433 ret = pin_down_block(root, blocknr, 0);
Chris Mason78fae272007-03-25 11:35:08 -0400434 BUG_ON(ret);
435 }
436
Chris Mason1261ec42007-03-20 20:35:03 -0400437 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
438 btrfs_set_super_blocks_used(info->disk_super,
439 super_blocks_used - num_blocks);
Chris Mason5caf2a02007-04-02 11:20:42 -0400440 ret = btrfs_del_item(trans, extent_root, path);
Chris Masona28ec192007-03-06 20:08:01 -0500441 if (ret)
442 BUG();
Chris Mason9078a3e2007-04-26 16:46:15 -0400443 ret = update_block_group(trans, root, blocknr, num_blocks, 0);
444 BUG_ON(ret);
Chris Masona28ec192007-03-06 20:08:01 -0500445 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400446 btrfs_release_path(extent_root, path);
447 btrfs_free_path(path);
Chris Masone089f052007-03-16 16:20:31 -0400448 finish_current_insert(trans, extent_root);
Chris Masona28ec192007-03-06 20:08:01 -0500449 return ret;
450}
451
452/*
Chris Masonfec577f2007-02-26 10:40:21 -0500453 * find all the blocks marked as pending in the radix tree and remove
454 * them from the extent map
455 */
Chris Masone089f052007-03-16 16:20:31 -0400456static int del_pending_extents(struct btrfs_trans_handle *trans, struct
457 btrfs_root *extent_root)
Chris Masonfec577f2007-02-26 10:40:21 -0500458{
459 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -0400460 int wret;
461 int err = 0;
Chris Mason8ef97622007-03-26 10:15:30 -0400462 unsigned long gang[4];
Chris Masonfec577f2007-02-26 10:40:21 -0500463 int i;
Chris Mason8ef97622007-03-26 10:15:30 -0400464 struct radix_tree_root *pending_radix;
465 struct radix_tree_root *pinned_radix;
466
467 pending_radix = &extent_root->fs_info->pending_del_radix;
468 pinned_radix = &extent_root->fs_info->pinned_radix;
Chris Masonfec577f2007-02-26 10:40:21 -0500469
470 while(1) {
Chris Mason8ef97622007-03-26 10:15:30 -0400471 ret = find_first_radix_bit(pending_radix, gang,
472 ARRAY_SIZE(gang));
Chris Masonfec577f2007-02-26 10:40:21 -0500473 if (!ret)
474 break;
475 for (i = 0; i < ret; i++) {
Chris Mason8ef97622007-03-26 10:15:30 -0400476 wret = set_radix_bit(pinned_radix, gang[i]);
477 BUG_ON(wret);
478 wret = clear_radix_bit(pending_radix, gang[i]);
479 BUG_ON(wret);
Chris Masond5719762007-03-23 10:01:08 -0400480 wret = __free_extent(trans, extent_root,
Chris Mason8ef97622007-03-26 10:15:30 -0400481 gang[i], 1, 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400482 if (wret)
483 err = wret;
Chris Masonfec577f2007-02-26 10:40:21 -0500484 }
485 }
Chris Masone20d96d2007-03-22 12:13:20 -0400486 return err;
Chris Masonfec577f2007-02-26 10:40:21 -0500487}
488
489/*
490 * remove an extent from the root, returns 0 on success
491 */
Chris Masone089f052007-03-16 16:20:31 -0400492int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
493 *root, u64 blocknr, u64 num_blocks, int pin)
Chris Masonfec577f2007-02-26 10:40:21 -0500494{
Chris Mason9f5fae22007-03-20 14:38:32 -0400495 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Masonfec577f2007-02-26 10:40:21 -0500496 int pending_ret;
497 int ret;
Chris Masona28ec192007-03-06 20:08:01 -0500498
499 if (root == extent_root) {
Chris Mason8ef97622007-03-26 10:15:30 -0400500 pin_down_block(root, blocknr, 1);
Chris Masona28ec192007-03-06 20:08:01 -0500501 return 0;
502 }
Chris Mason78fae272007-03-25 11:35:08 -0400503 ret = __free_extent(trans, root, blocknr, num_blocks, pin);
Chris Masone20d96d2007-03-22 12:13:20 -0400504 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
Chris Masonfec577f2007-02-26 10:40:21 -0500505 return ret ? ret : pending_ret;
506}
507
508/*
509 * walks the btree of allocated extents and find a hole of a given size.
510 * The key ins is changed to record the hole:
511 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -0400512 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -0500513 * ins->offset == number of blocks
514 * Any available blocks before search_start are skipped.
515 */
Chris Masone089f052007-03-16 16:20:31 -0400516static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
517 *orig_root, u64 num_blocks, u64 search_start, u64
518 search_end, struct btrfs_key *ins)
Chris Masonfec577f2007-02-26 10:40:21 -0500519{
Chris Mason5caf2a02007-04-02 11:20:42 -0400520 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -0400521 struct btrfs_key key;
Chris Masonfec577f2007-02-26 10:40:21 -0500522 int ret;
523 u64 hole_size = 0;
524 int slot = 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400525 u64 last_block = 0;
Chris Mason037e6392007-03-07 11:50:24 -0500526 u64 test_block;
Chris Masonfec577f2007-02-26 10:40:21 -0500527 int start_found;
Chris Mason234b63a2007-03-13 10:46:10 -0400528 struct btrfs_leaf *l;
Chris Mason9f5fae22007-03-20 14:38:32 -0400529 struct btrfs_root * root = orig_root->fs_info->extent_root;
Chris Masonf2458e12007-04-25 15:52:25 -0400530 struct btrfs_fs_info *info = root->fs_info;
Chris Mason0579da42007-03-07 16:15:30 -0500531 int total_needed = num_blocks;
Chris Masonf2458e12007-04-25 15:52:25 -0400532 int total_found = 0;
533 int fill_prealloc = 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400534 int level;
Chris Masonfec577f2007-02-26 10:40:21 -0500535
Chris Masonb1a4d962007-04-04 15:27:52 -0400536 path = btrfs_alloc_path();
537 ins->flags = 0;
538 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
539
Chris Masone20d96d2007-03-22 12:13:20 -0400540 level = btrfs_header_level(btrfs_buffer_header(root->node));
Chris Masonf2458e12007-04-25 15:52:25 -0400541 if (num_blocks == 0) {
542 fill_prealloc = 1;
543 num_blocks = 1;
Chris Mason308535a2007-04-28 15:17:08 -0400544 total_needed = (min(level + 1, BTRFS_MAX_LEVEL) + 2) * 3;
Chris Masonf2458e12007-04-25 15:52:25 -0400545 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400546 find_search_start(root, 0);
547 if (info->block_group_cache &&
548 info->block_group_cache->last_alloc > search_start)
549 search_start = info->block_group_cache->last_alloc;
Chris Mason62e27492007-03-15 12:56:47 -0400550
Chris Masonfec577f2007-02-26 10:40:21 -0500551check_failed:
Chris Mason5caf2a02007-04-02 11:20:42 -0400552 btrfs_init_path(path);
Chris Masonfec577f2007-02-26 10:40:21 -0500553 ins->objectid = search_start;
554 ins->offset = 0;
Chris Masonfec577f2007-02-26 10:40:21 -0500555 start_found = 0;
Chris Mason5caf2a02007-04-02 11:20:42 -0400556 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
Chris Mason0f70abe2007-02-28 16:46:22 -0500557 if (ret < 0)
558 goto error;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500559
Chris Mason5caf2a02007-04-02 11:20:42 -0400560 if (path->slots[0] > 0)
561 path->slots[0]--;
Chris Mason0579da42007-03-07 16:15:30 -0500562
Chris Masonfec577f2007-02-26 10:40:21 -0500563 while (1) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400564 l = btrfs_buffer_leaf(path->nodes[0]);
565 slot = path->slots[0];
Chris Mason7518a232007-03-12 12:01:18 -0400566 if (slot >= btrfs_header_nritems(&l->header)) {
Chris Masonf2458e12007-04-25 15:52:25 -0400567 if (fill_prealloc) {
568 info->extent_tree_prealloc_nr = 0;
569 total_found = 0;
570 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400571 ret = btrfs_next_leaf(root, path);
Chris Masonfec577f2007-02-26 10:40:21 -0500572 if (ret == 0)
573 continue;
Chris Mason0f70abe2007-02-28 16:46:22 -0500574 if (ret < 0)
575 goto error;
Chris Masonfec577f2007-02-26 10:40:21 -0500576 if (!start_found) {
577 ins->objectid = search_start;
Chris Masonf2458e12007-04-25 15:52:25 -0400578 ins->offset = (u64)-1 - search_start;
Chris Masonfec577f2007-02-26 10:40:21 -0500579 start_found = 1;
580 goto check_pending;
581 }
582 ins->objectid = last_block > search_start ?
583 last_block : search_start;
Chris Masonf2458e12007-04-25 15:52:25 -0400584 ins->offset = (u64)-1 - ins->objectid;
Chris Masonfec577f2007-02-26 10:40:21 -0500585 goto check_pending;
586 }
Chris Masone2fa7222007-03-12 16:22:34 -0400587 btrfs_disk_key_to_cpu(&key, &l->items[slot].key);
Chris Mason9078a3e2007-04-26 16:46:15 -0400588 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY)
589 goto next;
Chris Masone2fa7222007-03-12 16:22:34 -0400590 if (key.objectid >= search_start) {
Chris Masonfec577f2007-02-26 10:40:21 -0500591 if (start_found) {
Chris Mason0579da42007-03-07 16:15:30 -0500592 if (last_block < search_start)
593 last_block = search_start;
Chris Masone2fa7222007-03-12 16:22:34 -0400594 hole_size = key.objectid - last_block;
Chris Mason28b8bb92007-04-27 11:42:05 -0400595 if (hole_size >= num_blocks) {
Chris Masonfec577f2007-02-26 10:40:21 -0500596 ins->objectid = last_block;
Chris Mason037e6392007-03-07 11:50:24 -0500597 ins->offset = hole_size;
Chris Masonfec577f2007-02-26 10:40:21 -0500598 goto check_pending;
599 }
Chris Mason0579da42007-03-07 16:15:30 -0500600 }
Chris Masonfec577f2007-02-26 10:40:21 -0500601 }
Chris Mason0579da42007-03-07 16:15:30 -0500602 start_found = 1;
Chris Masone2fa7222007-03-12 16:22:34 -0400603 last_block = key.objectid + key.offset;
Chris Mason9078a3e2007-04-26 16:46:15 -0400604next:
Chris Mason5caf2a02007-04-02 11:20:42 -0400605 path->slots[0]++;
Chris Masonfec577f2007-02-26 10:40:21 -0500606 }
607 // FIXME -ENOSPC
608check_pending:
609 /* we have to make sure we didn't find an extent that has already
610 * been allocated by the map tree or the original allocation
611 */
Chris Mason5caf2a02007-04-02 11:20:42 -0400612 btrfs_release_path(root, path);
Chris Masonfec577f2007-02-26 10:40:21 -0500613 BUG_ON(ins->objectid < search_start);
Chris Mason06a2f9f2007-04-28 08:48:10 -0400614 if (ins->objectid >= btrfs_super_total_blocks(info->disk_super)) {
615 if (search_start == 0)
616 return -ENOSPC;
617 search_start = 0;
618 goto check_failed;
619 }
Chris Mason037e6392007-03-07 11:50:24 -0500620 for (test_block = ins->objectid;
Chris Masonf2458e12007-04-25 15:52:25 -0400621 test_block < ins->objectid + num_blocks; test_block++) {
622 if (test_radix_bit(&info->pinned_radix, test_block)) {
Chris Mason037e6392007-03-07 11:50:24 -0500623 search_start = test_block + 1;
Chris Masonfec577f2007-02-26 10:40:21 -0500624 goto check_failed;
625 }
626 }
Chris Masonf2458e12007-04-25 15:52:25 -0400627 if (!fill_prealloc && info->extent_tree_insert_nr) {
628 u64 last =
629 info->extent_tree_insert[info->extent_tree_insert_nr - 1];
630 if (ins->objectid + num_blocks >
631 info->extent_tree_insert[0] &&
632 ins->objectid <= last) {
633 search_start = last + 1;
634 WARN_ON(1);
635 goto check_failed;
636 }
637 }
638 if (!fill_prealloc && info->extent_tree_prealloc_nr) {
639 u64 first =
640 info->extent_tree_prealloc[info->extent_tree_prealloc_nr - 1];
641 if (ins->objectid + num_blocks > first &&
642 ins->objectid <= info->extent_tree_prealloc[0]) {
643 search_start = info->extent_tree_prealloc[0] + 1;
644 WARN_ON(1);
645 goto check_failed;
646 }
647 }
648 if (fill_prealloc) {
649 int nr;
650 test_block = ins->objectid;
651 while(test_block < ins->objectid + ins->offset &&
652 total_found < total_needed) {
653 nr = total_needed - total_found - 1;
654 BUG_ON(nr < 0);
Chris Masoncd1bc462007-04-27 10:08:34 -0400655 info->extent_tree_prealloc[nr] = test_block;
Chris Masonf2458e12007-04-25 15:52:25 -0400656 total_found++;
657 test_block++;
658 }
659 if (total_found < total_needed) {
660 search_start = test_block;
661 goto check_failed;
662 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400663 info->extent_tree_prealloc_nr = total_found;
Chris Masonf2458e12007-04-25 15:52:25 -0400664 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400665 ret = radix_tree_gang_lookup(&info->block_group_radix,
666 (void **)&info->block_group_cache,
667 ins->objectid, 1);
668 if (ret) {
669 info->block_group_cache->last_alloc = ins->objectid;
670 }
Chris Mason037e6392007-03-07 11:50:24 -0500671 ins->offset = num_blocks;
Chris Mason5caf2a02007-04-02 11:20:42 -0400672 btrfs_free_path(path);
Chris Masonfec577f2007-02-26 10:40:21 -0500673 return 0;
Chris Mason0f70abe2007-02-28 16:46:22 -0500674error:
Chris Mason5caf2a02007-04-02 11:20:42 -0400675 btrfs_release_path(root, path);
676 btrfs_free_path(path);
Chris Mason0f70abe2007-02-28 16:46:22 -0500677 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -0500678}
Chris Masonfec577f2007-02-26 10:40:21 -0500679/*
Chris Masonfec577f2007-02-26 10:40:21 -0500680 * finds a free extent and does all the dirty work required for allocation
681 * returns the key for the extent through ins, and a tree buffer for
682 * the first block of the extent through buf.
683 *
684 * returns 0 if everything worked, non-zero otherwise.
685 */
Chris Mason4d775672007-04-20 20:23:12 -0400686int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
687 struct btrfs_root *root, u64 owner,
Chris Masonc62a1922007-04-24 12:07:39 -0400688 u64 num_blocks, u64 search_start,
Chris Mason4d775672007-04-20 20:23:12 -0400689 u64 search_end, struct btrfs_key *ins)
Chris Masonfec577f2007-02-26 10:40:21 -0500690{
691 int ret;
692 int pending_ret;
Chris Mason1261ec42007-03-20 20:35:03 -0400693 u64 super_blocks_used;
694 struct btrfs_fs_info *info = root->fs_info;
695 struct btrfs_root *extent_root = info->extent_root;
Chris Mason234b63a2007-03-13 10:46:10 -0400696 struct btrfs_extent_item extent_item;
Chris Masonf2458e12007-04-25 15:52:25 -0400697 struct btrfs_key prealloc_key;
Chris Mason037e6392007-03-07 11:50:24 -0500698
Chris Masoncf27e1e2007-03-13 09:49:06 -0400699 btrfs_set_extent_refs(&extent_item, 1);
Chris Mason4d775672007-04-20 20:23:12 -0400700 btrfs_set_extent_owner(&extent_item, owner);
Chris Masonfec577f2007-02-26 10:40:21 -0500701
Chris Mason037e6392007-03-07 11:50:24 -0500702 if (root == extent_root) {
Chris Masonf2458e12007-04-25 15:52:25 -0400703 int nr;
704 BUG_ON(info->extent_tree_prealloc_nr == 0);
Chris Mason037e6392007-03-07 11:50:24 -0500705 BUG_ON(num_blocks != 1);
Chris Mason037e6392007-03-07 11:50:24 -0500706 ins->offset = 1;
Chris Masonf2458e12007-04-25 15:52:25 -0400707 info->extent_tree_prealloc_nr--;
708 nr = info->extent_tree_prealloc_nr;
709 ins->objectid = info->extent_tree_prealloc[nr];
710 info->extent_tree_insert[info->extent_tree_insert_nr++] =
711 ins->objectid;
Chris Mason9078a3e2007-04-26 16:46:15 -0400712 ret = update_block_group(trans, root,
713 ins->objectid, ins->offset, 1);
714 BUG_ON(ret);
Chris Masonfec577f2007-02-26 10:40:21 -0500715 return 0;
716 }
Chris Masonf2458e12007-04-25 15:52:25 -0400717 /* do the real allocation */
Chris Masone089f052007-03-16 16:20:31 -0400718 ret = find_free_extent(trans, root, num_blocks, search_start,
Chris Mason037e6392007-03-07 11:50:24 -0500719 search_end, ins);
720 if (ret)
721 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -0500722
Chris Masonf2458e12007-04-25 15:52:25 -0400723 /* then do prealloc for the extent tree */
724 ret = find_free_extent(trans, root, 0, ins->objectid + ins->offset,
725 search_end, &prealloc_key);
726 if (ret)
727 return ret;
728
Chris Mason1261ec42007-03-20 20:35:03 -0400729 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
730 btrfs_set_super_blocks_used(info->disk_super, super_blocks_used +
731 num_blocks);
Chris Masone089f052007-03-16 16:20:31 -0400732 ret = btrfs_insert_item(trans, extent_root, ins, &extent_item,
733 sizeof(extent_item));
Chris Mason037e6392007-03-07 11:50:24 -0500734
Chris Masone089f052007-03-16 16:20:31 -0400735 finish_current_insert(trans, extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -0400736 pending_ret = del_pending_extents(trans, extent_root);
Chris Mason037e6392007-03-07 11:50:24 -0500737 if (ret)
738 return ret;
739 if (pending_ret)
740 return pending_ret;
Chris Mason9078a3e2007-04-26 16:46:15 -0400741 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1);
Chris Mason037e6392007-03-07 11:50:24 -0500742 return 0;
Chris Masonfec577f2007-02-26 10:40:21 -0500743}
744
745/*
746 * helper function to allocate a block for a given tree
747 * returns the tree buffer or NULL.
748 */
Chris Masone20d96d2007-03-22 12:13:20 -0400749struct buffer_head *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Chris Mason4d775672007-04-20 20:23:12 -0400750 struct btrfs_root *root)
Chris Masonfec577f2007-02-26 10:40:21 -0500751{
Chris Masone2fa7222007-03-12 16:22:34 -0400752 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -0500753 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -0400754 struct buffer_head *buf;
Chris Masonfec577f2007-02-26 10:40:21 -0500755
Chris Mason4d775672007-04-20 20:23:12 -0400756 ret = btrfs_alloc_extent(trans, root, root->root_key.objectid,
Chris Mason4d775672007-04-20 20:23:12 -0400757 1, 0, (unsigned long)-1, &ins);
Chris Masonfec577f2007-02-26 10:40:21 -0500758 if (ret) {
759 BUG();
760 return NULL;
761 }
Chris Mason9078a3e2007-04-26 16:46:15 -0400762 BUG_ON(ret);
Chris Masond98237b2007-03-28 13:57:48 -0400763 buf = btrfs_find_create_tree_block(root, ins.objectid);
Chris Masondf2ce342007-03-23 11:00:45 -0400764 set_buffer_uptodate(buf);
Chris Mason7c4452b2007-04-28 09:29:35 -0400765 set_radix_bit(&trans->transaction->dirty_pages, buf->b_page->index);
Chris Masonfec577f2007-02-26 10:40:21 -0500766 return buf;
767}
Chris Masona28ec192007-03-06 20:08:01 -0500768
Chris Mason6407bf62007-03-27 06:33:00 -0400769static int drop_leaf_ref(struct btrfs_trans_handle *trans,
770 struct btrfs_root *root, struct buffer_head *cur)
771{
772 struct btrfs_disk_key *key;
773 struct btrfs_leaf *leaf;
774 struct btrfs_file_extent_item *fi;
775 int i;
776 int nritems;
777 int ret;
778
779 BUG_ON(!btrfs_is_leaf(btrfs_buffer_node(cur)));
780 leaf = btrfs_buffer_leaf(cur);
781 nritems = btrfs_header_nritems(&leaf->header);
782 for (i = 0; i < nritems; i++) {
783 key = &leaf->items[i].key;
784 if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY)
785 continue;
786 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
Chris Mason236454df2007-04-19 13:37:44 -0400787 if (btrfs_file_extent_type(fi) == BTRFS_FILE_EXTENT_INLINE)
788 continue;
Chris Mason6407bf62007-03-27 06:33:00 -0400789 /*
790 * FIXME make sure to insert a trans record that
791 * repeats the snapshot del on crash
792 */
793 ret = btrfs_free_extent(trans, root,
794 btrfs_file_extent_disk_blocknr(fi),
795 btrfs_file_extent_disk_num_blocks(fi),
796 0);
797 BUG_ON(ret);
798 }
799 return 0;
800}
801
Chris Mason9aca1d52007-03-13 11:09:37 -0400802/*
803 * helper function for drop_snapshot, this walks down the tree dropping ref
804 * counts as it goes.
805 */
Chris Masone089f052007-03-16 16:20:31 -0400806static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
807 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -0500808{
Chris Masone20d96d2007-03-22 12:13:20 -0400809 struct buffer_head *next;
810 struct buffer_head *cur;
Chris Mason20524f02007-03-10 06:35:47 -0500811 u64 blocknr;
812 int ret;
813 u32 refs;
814
Chris Mason5caf2a02007-04-02 11:20:42 -0400815 WARN_ON(*level < 0);
816 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Masonb18c6682007-04-17 13:26:50 -0400817 ret = lookup_extent_ref(trans, root, bh_blocknr(path->nodes[*level]),
Chris Mason6407bf62007-03-27 06:33:00 -0400818 1, &refs);
Chris Mason20524f02007-03-10 06:35:47 -0500819 BUG_ON(ret);
820 if (refs > 1)
821 goto out;
Chris Mason9aca1d52007-03-13 11:09:37 -0400822 /*
823 * walk down to the last node level and free all the leaves
824 */
Chris Mason6407bf62007-03-27 06:33:00 -0400825 while(*level >= 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400826 WARN_ON(*level < 0);
827 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason20524f02007-03-10 06:35:47 -0500828 cur = path->nodes[*level];
Chris Mason2c90e5d2007-04-02 10:50:19 -0400829 if (btrfs_header_level(btrfs_buffer_header(cur)) != *level)
830 WARN_ON(1);
Chris Mason7518a232007-03-12 12:01:18 -0400831 if (path->slots[*level] >=
Chris Masone20d96d2007-03-22 12:13:20 -0400832 btrfs_header_nritems(btrfs_buffer_header(cur)))
Chris Mason20524f02007-03-10 06:35:47 -0500833 break;
Chris Mason6407bf62007-03-27 06:33:00 -0400834 if (*level == 0) {
835 ret = drop_leaf_ref(trans, root, cur);
836 BUG_ON(ret);
837 break;
838 }
Chris Masone20d96d2007-03-22 12:13:20 -0400839 blocknr = btrfs_node_blockptr(btrfs_buffer_node(cur),
840 path->slots[*level]);
Chris Masonb18c6682007-04-17 13:26:50 -0400841 ret = lookup_extent_ref(trans, root, blocknr, 1, &refs);
Chris Mason6407bf62007-03-27 06:33:00 -0400842 BUG_ON(ret);
843 if (refs != 1) {
Chris Mason20524f02007-03-10 06:35:47 -0500844 path->slots[*level]++;
Chris Masone089f052007-03-16 16:20:31 -0400845 ret = btrfs_free_extent(trans, root, blocknr, 1, 1);
Chris Mason20524f02007-03-10 06:35:47 -0500846 BUG_ON(ret);
847 continue;
848 }
Chris Mason20524f02007-03-10 06:35:47 -0500849 next = read_tree_block(root, blocknr);
Chris Mason5caf2a02007-04-02 11:20:42 -0400850 WARN_ON(*level <= 0);
Chris Mason83e15a22007-03-12 09:03:27 -0400851 if (path->nodes[*level-1])
Chris Mason234b63a2007-03-13 10:46:10 -0400852 btrfs_block_release(root, path->nodes[*level-1]);
Chris Mason20524f02007-03-10 06:35:47 -0500853 path->nodes[*level-1] = next;
Chris Masone20d96d2007-03-22 12:13:20 -0400854 *level = btrfs_header_level(btrfs_buffer_header(next));
Chris Mason20524f02007-03-10 06:35:47 -0500855 path->slots[*level] = 0;
856 }
857out:
Chris Mason5caf2a02007-04-02 11:20:42 -0400858 WARN_ON(*level < 0);
859 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason6407bf62007-03-27 06:33:00 -0400860 ret = btrfs_free_extent(trans, root,
Chris Mason7eccb902007-04-11 15:53:25 -0400861 bh_blocknr(path->nodes[*level]), 1, 1);
Chris Mason234b63a2007-03-13 10:46:10 -0400862 btrfs_block_release(root, path->nodes[*level]);
Chris Mason20524f02007-03-10 06:35:47 -0500863 path->nodes[*level] = NULL;
864 *level += 1;
865 BUG_ON(ret);
866 return 0;
867}
868
Chris Mason9aca1d52007-03-13 11:09:37 -0400869/*
870 * helper for dropping snapshots. This walks back up the tree in the path
871 * to find the first node higher up where we haven't yet gone through
872 * all the slots
873 */
Chris Masone089f052007-03-16 16:20:31 -0400874static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root
875 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -0500876{
877 int i;
878 int slot;
879 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400880 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Mason20524f02007-03-10 06:35:47 -0500881 slot = path->slots[i];
Chris Masone20d96d2007-03-22 12:13:20 -0400882 if (slot < btrfs_header_nritems(
883 btrfs_buffer_header(path->nodes[i])) - 1) {
Chris Mason20524f02007-03-10 06:35:47 -0500884 path->slots[i]++;
885 *level = i;
886 return 0;
887 } else {
Chris Masone089f052007-03-16 16:20:31 -0400888 ret = btrfs_free_extent(trans, root,
Chris Mason7eccb902007-04-11 15:53:25 -0400889 bh_blocknr(path->nodes[*level]),
Chris Masone089f052007-03-16 16:20:31 -0400890 1, 1);
Chris Mason6407bf62007-03-27 06:33:00 -0400891 BUG_ON(ret);
Chris Mason234b63a2007-03-13 10:46:10 -0400892 btrfs_block_release(root, path->nodes[*level]);
Chris Mason83e15a22007-03-12 09:03:27 -0400893 path->nodes[*level] = NULL;
Chris Mason20524f02007-03-10 06:35:47 -0500894 *level = i + 1;
Chris Mason20524f02007-03-10 06:35:47 -0500895 }
896 }
897 return 1;
898}
899
Chris Mason9aca1d52007-03-13 11:09:37 -0400900/*
901 * drop the reference count on the tree rooted at 'snap'. This traverses
902 * the tree freeing any blocks that have a ref count of zero after being
903 * decremented.
904 */
Chris Masone089f052007-03-16 16:20:31 -0400905int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masone20d96d2007-03-22 12:13:20 -0400906 *root, struct buffer_head *snap)
Chris Mason20524f02007-03-10 06:35:47 -0500907{
Chris Mason3768f362007-03-13 16:47:54 -0400908 int ret = 0;
Chris Mason9aca1d52007-03-13 11:09:37 -0400909 int wret;
Chris Mason20524f02007-03-10 06:35:47 -0500910 int level;
Chris Mason5caf2a02007-04-02 11:20:42 -0400911 struct btrfs_path *path;
Chris Mason20524f02007-03-10 06:35:47 -0500912 int i;
913 int orig_level;
914
Chris Mason5caf2a02007-04-02 11:20:42 -0400915 path = btrfs_alloc_path();
916 BUG_ON(!path);
917 btrfs_init_path(path);
Chris Mason20524f02007-03-10 06:35:47 -0500918
Chris Masone20d96d2007-03-22 12:13:20 -0400919 level = btrfs_header_level(btrfs_buffer_header(snap));
Chris Mason20524f02007-03-10 06:35:47 -0500920 orig_level = level;
Chris Mason5caf2a02007-04-02 11:20:42 -0400921 path->nodes[level] = snap;
922 path->slots[level] = 0;
Chris Mason20524f02007-03-10 06:35:47 -0500923 while(1) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400924 wret = walk_down_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -0400925 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -0500926 break;
Chris Mason9aca1d52007-03-13 11:09:37 -0400927 if (wret < 0)
928 ret = wret;
929
Chris Mason5caf2a02007-04-02 11:20:42 -0400930 wret = walk_up_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -0400931 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -0500932 break;
Chris Mason9aca1d52007-03-13 11:09:37 -0400933 if (wret < 0)
934 ret = wret;
Chris Mason20524f02007-03-10 06:35:47 -0500935 }
Chris Mason83e15a22007-03-12 09:03:27 -0400936 for (i = 0; i <= orig_level; i++) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400937 if (path->nodes[i]) {
938 btrfs_block_release(root, path->nodes[i]);
Chris Mason83e15a22007-03-12 09:03:27 -0400939 }
Chris Mason20524f02007-03-10 06:35:47 -0500940 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400941 btrfs_free_path(path);
Chris Mason9aca1d52007-03-13 11:09:37 -0400942 return ret;
Chris Mason20524f02007-03-10 06:35:47 -0500943}
Chris Mason9078a3e2007-04-26 16:46:15 -0400944
945int btrfs_free_block_groups(struct btrfs_fs_info *info)
946{
947 int ret;
948 struct btrfs_block_group_cache *cache[8];
949 int i;
950
951 while(1) {
952 ret = radix_tree_gang_lookup(&info->block_group_radix,
953 (void **)cache, 0,
954 ARRAY_SIZE(cache));
955 if (!ret)
956 break;
957 for (i = 0; i < ret; i++) {
958 radix_tree_delete(&info->block_group_radix,
959 cache[i]->key.objectid +
960 cache[i]->key.offset - 1);
961 kfree(cache[i]);
962 }
963 }
964 return 0;
965}
966
967int btrfs_read_block_groups(struct btrfs_root *root)
968{
969 struct btrfs_path *path;
970 int ret;
971 int err = 0;
972 struct btrfs_block_group_item *bi;
973 struct btrfs_block_group_cache *cache;
974 struct btrfs_key key;
975 struct btrfs_key found_key;
976 struct btrfs_leaf *leaf;
977 u64 group_size_blocks = BTRFS_BLOCK_GROUP_SIZE / root->blocksize;
978
979 root = root->fs_info->extent_root;
980 key.objectid = 0;
981 key.offset = group_size_blocks;
982 key.flags = 0;
983 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
984
985 path = btrfs_alloc_path();
986 if (!path)
987 return -ENOMEM;
988
989 while(1) {
990 ret = btrfs_search_slot(NULL, root->fs_info->extent_root,
991 &key, path, 0, 0);
992 if (ret != 0) {
993 err = ret;
994 break;
995 }
996 leaf = btrfs_buffer_leaf(path->nodes[0]);
997 btrfs_disk_key_to_cpu(&found_key,
998 &leaf->items[path->slots[0]].key);
999 cache = kmalloc(sizeof(*cache), GFP_NOFS);
1000 if (!cache) {
1001 err = -1;
1002 break;
1003 }
1004 bi = btrfs_item_ptr(leaf, path->slots[0],
1005 struct btrfs_block_group_item);
1006 memcpy(&cache->item, bi, sizeof(*bi));
1007 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Masoncd1bc462007-04-27 10:08:34 -04001008 cache->last_alloc = 0;
1009 cache->first_free = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04001010 key.objectid = found_key.objectid + found_key.offset;
1011 btrfs_release_path(root, path);
1012 ret = radix_tree_insert(&root->fs_info->block_group_radix,
1013 found_key.objectid +
1014 found_key.offset - 1,
1015 (void *)cache);
1016 BUG_ON(ret);
1017 if (key.objectid >=
1018 btrfs_super_total_blocks(root->fs_info->disk_super))
1019 break;
1020 }
1021
1022 btrfs_free_path(path);
1023 return 0;
1024}