blob: aca5802a8a1e8659def1071cc738f9a929e2c135 [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 Masone089f052007-03-16 16:20:31 -0400294int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, struct
295 btrfs_root *root)
Chris Masona28ec192007-03-06 20:08:01 -0500296{
Chris Mason8ef97622007-03-26 10:15:30 -0400297 unsigned long gang[8];
Chris Mason88fd1462007-03-16 08:56:18 -0400298 u64 first = 0;
Chris Masona28ec192007-03-06 20:08:01 -0500299 int ret;
300 int i;
Chris Mason8ef97622007-03-26 10:15:30 -0400301 struct radix_tree_root *pinned_radix = &root->fs_info->pinned_radix;
Chris Masona28ec192007-03-06 20:08:01 -0500302
303 while(1) {
Chris Mason8ef97622007-03-26 10:15:30 -0400304 ret = find_first_radix_bit(pinned_radix, gang,
305 ARRAY_SIZE(gang));
Chris Masona28ec192007-03-06 20:08:01 -0500306 if (!ret)
307 break;
Chris Mason88fd1462007-03-16 08:56:18 -0400308 if (!first)
Chris Mason8ef97622007-03-26 10:15:30 -0400309 first = gang[0];
Chris Mason0579da42007-03-07 16:15:30 -0500310 for (i = 0; i < ret; i++) {
Chris Mason8ef97622007-03-26 10:15:30 -0400311 clear_radix_bit(pinned_radix, gang[i]);
Chris Mason0579da42007-03-07 16:15:30 -0500312 }
Chris Masona28ec192007-03-06 20:08:01 -0500313 }
Chris Mason28b8bb92007-04-27 11:42:05 -0400314 if (root->fs_info->block_group_cache) {
315 root->fs_info->block_group_cache->last_alloc =
316 root->fs_info->block_group_cache->first_free;
317 }
Chris Masona28ec192007-03-06 20:08:01 -0500318 return 0;
319}
320
Chris Masone089f052007-03-16 16:20:31 -0400321static int finish_current_insert(struct btrfs_trans_handle *trans, struct
322 btrfs_root *extent_root)
Chris Mason037e6392007-03-07 11:50:24 -0500323{
Chris Masone2fa7222007-03-12 16:22:34 -0400324 struct btrfs_key ins;
Chris Mason234b63a2007-03-13 10:46:10 -0400325 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -0500326 int i;
327 int ret;
Chris Mason1261ec42007-03-20 20:35:03 -0400328 u64 super_blocks_used;
329 struct btrfs_fs_info *info = extent_root->fs_info;
Chris Mason037e6392007-03-07 11:50:24 -0500330
Chris Masoncf27e1e2007-03-13 09:49:06 -0400331 btrfs_set_extent_refs(&extent_item, 1);
Chris Mason037e6392007-03-07 11:50:24 -0500332 ins.offset = 1;
333 ins.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -0400334 btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
Chris Mason5d0c3e62007-04-23 17:01:05 -0400335 btrfs_set_extent_owner(&extent_item, extent_root->root_key.objectid);
Chris Mason037e6392007-03-07 11:50:24 -0500336
Chris Masonf2458e12007-04-25 15:52:25 -0400337 for (i = 0; i < extent_root->fs_info->extent_tree_insert_nr; i++) {
338 ins.objectid = extent_root->fs_info->extent_tree_insert[i];
Chris Mason1261ec42007-03-20 20:35:03 -0400339 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
340 btrfs_set_super_blocks_used(info->disk_super,
341 super_blocks_used + 1);
Chris Masone089f052007-03-16 16:20:31 -0400342 ret = btrfs_insert_item(trans, extent_root, &ins, &extent_item,
343 sizeof(extent_item));
Chris Mason037e6392007-03-07 11:50:24 -0500344 BUG_ON(ret);
345 }
Chris Masonf2458e12007-04-25 15:52:25 -0400346 extent_root->fs_info->extent_tree_insert_nr = 0;
347 extent_root->fs_info->extent_tree_prealloc_nr = 0;
Chris Mason037e6392007-03-07 11:50:24 -0500348 return 0;
349}
350
Chris Mason8ef97622007-03-26 10:15:30 -0400351static int pin_down_block(struct btrfs_root *root, u64 blocknr, int pending)
Chris Masone20d96d2007-03-22 12:13:20 -0400352{
353 int err;
Chris Mason78fae272007-03-25 11:35:08 -0400354 struct btrfs_header *header;
Chris Mason8ef97622007-03-26 10:15:30 -0400355 struct buffer_head *bh;
Chris Mason78fae272007-03-25 11:35:08 -0400356
Chris Masonf4b9aa82007-03-27 11:05:53 -0400357 if (!pending) {
Chris Masond98237b2007-03-28 13:57:48 -0400358 bh = btrfs_find_tree_block(root, blocknr);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400359 if (bh) {
360 if (buffer_uptodate(bh)) {
361 u64 transid =
362 root->fs_info->running_transaction->transid;
363 header = btrfs_buffer_header(bh);
364 if (btrfs_header_generation(header) ==
365 transid) {
366 btrfs_block_release(root, bh);
367 return 0;
368 }
Chris Masonf4b9aa82007-03-27 11:05:53 -0400369 }
Chris Masond6025572007-03-30 14:27:56 -0400370 btrfs_block_release(root, bh);
Chris Mason8ef97622007-03-26 10:15:30 -0400371 }
Chris Mason8ef97622007-03-26 10:15:30 -0400372 err = set_radix_bit(&root->fs_info->pinned_radix, blocknr);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400373 } else {
374 err = set_radix_bit(&root->fs_info->pending_del_radix, blocknr);
375 }
Chris Mason8ef97622007-03-26 10:15:30 -0400376 BUG_ON(err);
Chris Masone20d96d2007-03-22 12:13:20 -0400377 return 0;
378}
379
Chris Masona28ec192007-03-06 20:08:01 -0500380/*
381 * remove an extent from the root, returns 0 on success
382 */
Chris Masone089f052007-03-16 16:20:31 -0400383static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason78fae272007-03-25 11:35:08 -0400384 *root, u64 blocknr, u64 num_blocks, int pin)
Chris Masona28ec192007-03-06 20:08:01 -0500385{
Chris Mason5caf2a02007-04-02 11:20:42 -0400386 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -0400387 struct btrfs_key key;
Chris Mason1261ec42007-03-20 20:35:03 -0400388 struct btrfs_fs_info *info = root->fs_info;
389 struct btrfs_root *extent_root = info->extent_root;
Chris Masona28ec192007-03-06 20:08:01 -0500390 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400391 struct btrfs_extent_item *ei;
Chris Masone2fa7222007-03-12 16:22:34 -0400392 struct btrfs_key ins;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400393 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500394
Chris Masona28ec192007-03-06 20:08:01 -0500395 key.objectid = blocknr;
396 key.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -0400397 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masona28ec192007-03-06 20:08:01 -0500398 key.offset = num_blocks;
399
Chris Masone089f052007-03-16 16:20:31 -0400400 find_free_extent(trans, root, 0, 0, (u64)-1, &ins);
Chris Mason5caf2a02007-04-02 11:20:42 -0400401 path = btrfs_alloc_path();
402 BUG_ON(!path);
403 btrfs_init_path(path);
Chris Mason5f26f772007-04-05 10:38:44 -0400404
Chris Mason5caf2a02007-04-02 11:20:42 -0400405 ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
Chris Masona28ec192007-03-06 20:08:01 -0500406 if (ret) {
Chris Mason2e635a22007-03-21 11:12:56 -0400407 printk("failed to find %Lu\n", key.objectid);
Chris Mason234b63a2007-03-13 10:46:10 -0400408 btrfs_print_tree(extent_root, extent_root->node);
Chris Mason2e635a22007-03-21 11:12:56 -0400409 printk("failed to find %Lu\n", key.objectid);
Chris Masona28ec192007-03-06 20:08:01 -0500410 BUG();
411 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400412 ei = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
Chris Mason123abc82007-03-14 14:14:43 -0400413 struct btrfs_extent_item);
Chris Masona28ec192007-03-06 20:08:01 -0500414 BUG_ON(ei->refs == 0);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400415 refs = btrfs_extent_refs(ei) - 1;
416 btrfs_set_extent_refs(ei, refs);
Chris Mason5caf2a02007-04-02 11:20:42 -0400417 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400418 if (refs == 0) {
Chris Mason1261ec42007-03-20 20:35:03 -0400419 u64 super_blocks_used;
Chris Mason78fae272007-03-25 11:35:08 -0400420
421 if (pin) {
Chris Mason8ef97622007-03-26 10:15:30 -0400422 ret = pin_down_block(root, blocknr, 0);
Chris Mason78fae272007-03-25 11:35:08 -0400423 BUG_ON(ret);
424 }
425
Chris Mason1261ec42007-03-20 20:35:03 -0400426 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
427 btrfs_set_super_blocks_used(info->disk_super,
428 super_blocks_used - num_blocks);
Chris Mason5caf2a02007-04-02 11:20:42 -0400429 ret = btrfs_del_item(trans, extent_root, path);
Chris Masona28ec192007-03-06 20:08:01 -0500430 if (ret)
431 BUG();
Chris Mason9078a3e2007-04-26 16:46:15 -0400432 ret = update_block_group(trans, root, blocknr, num_blocks, 0);
433 BUG_ON(ret);
Chris Masona28ec192007-03-06 20:08:01 -0500434 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400435 btrfs_release_path(extent_root, path);
436 btrfs_free_path(path);
Chris Masone089f052007-03-16 16:20:31 -0400437 finish_current_insert(trans, extent_root);
Chris Masona28ec192007-03-06 20:08:01 -0500438 return ret;
439}
440
441/*
Chris Masonfec577f2007-02-26 10:40:21 -0500442 * find all the blocks marked as pending in the radix tree and remove
443 * them from the extent map
444 */
Chris Masone089f052007-03-16 16:20:31 -0400445static int del_pending_extents(struct btrfs_trans_handle *trans, struct
446 btrfs_root *extent_root)
Chris Masonfec577f2007-02-26 10:40:21 -0500447{
448 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -0400449 int wret;
450 int err = 0;
Chris Mason8ef97622007-03-26 10:15:30 -0400451 unsigned long gang[4];
Chris Masonfec577f2007-02-26 10:40:21 -0500452 int i;
Chris Mason8ef97622007-03-26 10:15:30 -0400453 struct radix_tree_root *pending_radix;
454 struct radix_tree_root *pinned_radix;
455
456 pending_radix = &extent_root->fs_info->pending_del_radix;
457 pinned_radix = &extent_root->fs_info->pinned_radix;
Chris Masonfec577f2007-02-26 10:40:21 -0500458
459 while(1) {
Chris Mason8ef97622007-03-26 10:15:30 -0400460 ret = find_first_radix_bit(pending_radix, gang,
461 ARRAY_SIZE(gang));
Chris Masonfec577f2007-02-26 10:40:21 -0500462 if (!ret)
463 break;
464 for (i = 0; i < ret; i++) {
Chris Mason8ef97622007-03-26 10:15:30 -0400465 wret = set_radix_bit(pinned_radix, gang[i]);
466 BUG_ON(wret);
467 wret = clear_radix_bit(pending_radix, gang[i]);
468 BUG_ON(wret);
Chris Masond5719762007-03-23 10:01:08 -0400469 wret = __free_extent(trans, extent_root,
Chris Mason8ef97622007-03-26 10:15:30 -0400470 gang[i], 1, 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400471 if (wret)
472 err = wret;
Chris Masonfec577f2007-02-26 10:40:21 -0500473 }
474 }
Chris Masone20d96d2007-03-22 12:13:20 -0400475 return err;
Chris Masonfec577f2007-02-26 10:40:21 -0500476}
477
478/*
479 * remove an extent from the root, returns 0 on success
480 */
Chris Masone089f052007-03-16 16:20:31 -0400481int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
482 *root, u64 blocknr, u64 num_blocks, int pin)
Chris Masonfec577f2007-02-26 10:40:21 -0500483{
Chris Mason9f5fae22007-03-20 14:38:32 -0400484 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Masonfec577f2007-02-26 10:40:21 -0500485 int pending_ret;
486 int ret;
Chris Masona28ec192007-03-06 20:08:01 -0500487
488 if (root == extent_root) {
Chris Mason8ef97622007-03-26 10:15:30 -0400489 pin_down_block(root, blocknr, 1);
Chris Masona28ec192007-03-06 20:08:01 -0500490 return 0;
491 }
Chris Mason78fae272007-03-25 11:35:08 -0400492 ret = __free_extent(trans, root, blocknr, num_blocks, pin);
Chris Masone20d96d2007-03-22 12:13:20 -0400493 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
Chris Masonfec577f2007-02-26 10:40:21 -0500494 return ret ? ret : pending_ret;
495}
496
497/*
498 * walks the btree of allocated extents and find a hole of a given size.
499 * The key ins is changed to record the hole:
500 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -0400501 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -0500502 * ins->offset == number of blocks
503 * Any available blocks before search_start are skipped.
504 */
Chris Masone089f052007-03-16 16:20:31 -0400505static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
506 *orig_root, u64 num_blocks, u64 search_start, u64
507 search_end, struct btrfs_key *ins)
Chris Masonfec577f2007-02-26 10:40:21 -0500508{
Chris Mason5caf2a02007-04-02 11:20:42 -0400509 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -0400510 struct btrfs_key key;
Chris Masonfec577f2007-02-26 10:40:21 -0500511 int ret;
512 u64 hole_size = 0;
513 int slot = 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400514 u64 last_block = 0;
Chris Mason037e6392007-03-07 11:50:24 -0500515 u64 test_block;
Chris Masonfec577f2007-02-26 10:40:21 -0500516 int start_found;
Chris Mason234b63a2007-03-13 10:46:10 -0400517 struct btrfs_leaf *l;
Chris Mason9f5fae22007-03-20 14:38:32 -0400518 struct btrfs_root * root = orig_root->fs_info->extent_root;
Chris Masonf2458e12007-04-25 15:52:25 -0400519 struct btrfs_fs_info *info = root->fs_info;
Chris Mason0579da42007-03-07 16:15:30 -0500520 int total_needed = num_blocks;
Chris Masonf2458e12007-04-25 15:52:25 -0400521 int total_found = 0;
522 int fill_prealloc = 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400523 int level;
Chris Masonfec577f2007-02-26 10:40:21 -0500524
Chris Masonb1a4d962007-04-04 15:27:52 -0400525 path = btrfs_alloc_path();
526 ins->flags = 0;
527 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
528
Chris Masone20d96d2007-03-22 12:13:20 -0400529 level = btrfs_header_level(btrfs_buffer_header(root->node));
Chris Masonf2458e12007-04-25 15:52:25 -0400530 if (num_blocks == 0) {
531 fill_prealloc = 1;
532 num_blocks = 1;
533 total_needed = min(level + 2, BTRFS_MAX_LEVEL) * 3;
534 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400535 find_search_start(root, 0);
536 if (info->block_group_cache &&
537 info->block_group_cache->last_alloc > search_start)
538 search_start = info->block_group_cache->last_alloc;
Chris Mason62e27492007-03-15 12:56:47 -0400539
Chris Masonfec577f2007-02-26 10:40:21 -0500540check_failed:
Chris Mason5caf2a02007-04-02 11:20:42 -0400541 btrfs_init_path(path);
Chris Masonfec577f2007-02-26 10:40:21 -0500542 ins->objectid = search_start;
543 ins->offset = 0;
Chris Masonfec577f2007-02-26 10:40:21 -0500544 start_found = 0;
Chris Mason5caf2a02007-04-02 11:20:42 -0400545 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
Chris Mason0f70abe2007-02-28 16:46:22 -0500546 if (ret < 0)
547 goto error;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500548
Chris Mason5caf2a02007-04-02 11:20:42 -0400549 if (path->slots[0] > 0)
550 path->slots[0]--;
Chris Mason0579da42007-03-07 16:15:30 -0500551
Chris Masonfec577f2007-02-26 10:40:21 -0500552 while (1) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400553 l = btrfs_buffer_leaf(path->nodes[0]);
554 slot = path->slots[0];
Chris Mason7518a232007-03-12 12:01:18 -0400555 if (slot >= btrfs_header_nritems(&l->header)) {
Chris Masonf2458e12007-04-25 15:52:25 -0400556 if (fill_prealloc) {
557 info->extent_tree_prealloc_nr = 0;
558 total_found = 0;
559 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400560 ret = btrfs_next_leaf(root, path);
Chris Masonfec577f2007-02-26 10:40:21 -0500561 if (ret == 0)
562 continue;
Chris Mason0f70abe2007-02-28 16:46:22 -0500563 if (ret < 0)
564 goto error;
Chris Masonfec577f2007-02-26 10:40:21 -0500565 if (!start_found) {
566 ins->objectid = search_start;
Chris Masonf2458e12007-04-25 15:52:25 -0400567 ins->offset = (u64)-1 - search_start;
Chris Masonfec577f2007-02-26 10:40:21 -0500568 start_found = 1;
569 goto check_pending;
570 }
571 ins->objectid = last_block > search_start ?
572 last_block : search_start;
Chris Masonf2458e12007-04-25 15:52:25 -0400573 ins->offset = (u64)-1 - ins->objectid;
Chris Masonfec577f2007-02-26 10:40:21 -0500574 goto check_pending;
575 }
Chris Masone2fa7222007-03-12 16:22:34 -0400576 btrfs_disk_key_to_cpu(&key, &l->items[slot].key);
Chris Mason9078a3e2007-04-26 16:46:15 -0400577 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY)
578 goto next;
Chris Masone2fa7222007-03-12 16:22:34 -0400579 if (key.objectid >= search_start) {
Chris Masonfec577f2007-02-26 10:40:21 -0500580 if (start_found) {
Chris Mason0579da42007-03-07 16:15:30 -0500581 if (last_block < search_start)
582 last_block = search_start;
Chris Masone2fa7222007-03-12 16:22:34 -0400583 hole_size = key.objectid - last_block;
Chris Mason28b8bb92007-04-27 11:42:05 -0400584 if (hole_size >= num_blocks) {
Chris Masonfec577f2007-02-26 10:40:21 -0500585 ins->objectid = last_block;
Chris Mason037e6392007-03-07 11:50:24 -0500586 ins->offset = hole_size;
Chris Masonfec577f2007-02-26 10:40:21 -0500587 goto check_pending;
588 }
Chris Mason0579da42007-03-07 16:15:30 -0500589 }
Chris Masonfec577f2007-02-26 10:40:21 -0500590 }
Chris Mason0579da42007-03-07 16:15:30 -0500591 start_found = 1;
Chris Masone2fa7222007-03-12 16:22:34 -0400592 last_block = key.objectid + key.offset;
Chris Mason9078a3e2007-04-26 16:46:15 -0400593next:
Chris Mason5caf2a02007-04-02 11:20:42 -0400594 path->slots[0]++;
Chris Masonfec577f2007-02-26 10:40:21 -0500595 }
596 // FIXME -ENOSPC
597check_pending:
598 /* we have to make sure we didn't find an extent that has already
599 * been allocated by the map tree or the original allocation
600 */
Chris Mason5caf2a02007-04-02 11:20:42 -0400601 btrfs_release_path(root, path);
Chris Masonfec577f2007-02-26 10:40:21 -0500602 BUG_ON(ins->objectid < search_start);
Chris Mason037e6392007-03-07 11:50:24 -0500603 for (test_block = ins->objectid;
Chris Masonf2458e12007-04-25 15:52:25 -0400604 test_block < ins->objectid + num_blocks; test_block++) {
605 if (test_radix_bit(&info->pinned_radix, test_block)) {
Chris Mason037e6392007-03-07 11:50:24 -0500606 search_start = test_block + 1;
Chris Masonfec577f2007-02-26 10:40:21 -0500607 goto check_failed;
608 }
609 }
Chris Masonf2458e12007-04-25 15:52:25 -0400610 if (!fill_prealloc && info->extent_tree_insert_nr) {
611 u64 last =
612 info->extent_tree_insert[info->extent_tree_insert_nr - 1];
613 if (ins->objectid + num_blocks >
614 info->extent_tree_insert[0] &&
615 ins->objectid <= last) {
616 search_start = last + 1;
617 WARN_ON(1);
618 goto check_failed;
619 }
620 }
621 if (!fill_prealloc && info->extent_tree_prealloc_nr) {
622 u64 first =
623 info->extent_tree_prealloc[info->extent_tree_prealloc_nr - 1];
624 if (ins->objectid + num_blocks > first &&
625 ins->objectid <= info->extent_tree_prealloc[0]) {
626 search_start = info->extent_tree_prealloc[0] + 1;
627 WARN_ON(1);
628 goto check_failed;
629 }
630 }
631 if (fill_prealloc) {
632 int nr;
633 test_block = ins->objectid;
634 while(test_block < ins->objectid + ins->offset &&
635 total_found < total_needed) {
636 nr = total_needed - total_found - 1;
637 BUG_ON(nr < 0);
Chris Masoncd1bc462007-04-27 10:08:34 -0400638 info->extent_tree_prealloc[nr] = test_block;
Chris Masonf2458e12007-04-25 15:52:25 -0400639 total_found++;
640 test_block++;
641 }
642 if (total_found < total_needed) {
643 search_start = test_block;
644 goto check_failed;
645 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400646 info->extent_tree_prealloc_nr = total_found;
Chris Masonf2458e12007-04-25 15:52:25 -0400647 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400648 ret = radix_tree_gang_lookup(&info->block_group_radix,
649 (void **)&info->block_group_cache,
650 ins->objectid, 1);
651 if (ret) {
652 info->block_group_cache->last_alloc = ins->objectid;
653 }
Chris Mason037e6392007-03-07 11:50:24 -0500654 ins->offset = num_blocks;
Chris Mason5caf2a02007-04-02 11:20:42 -0400655 btrfs_free_path(path);
Chris Masonfec577f2007-02-26 10:40:21 -0500656 return 0;
Chris Mason0f70abe2007-02-28 16:46:22 -0500657error:
Chris Mason5caf2a02007-04-02 11:20:42 -0400658 btrfs_release_path(root, path);
659 btrfs_free_path(path);
Chris Mason0f70abe2007-02-28 16:46:22 -0500660 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -0500661}
Chris Masonfec577f2007-02-26 10:40:21 -0500662/*
Chris Masonfec577f2007-02-26 10:40:21 -0500663 * finds a free extent and does all the dirty work required for allocation
664 * returns the key for the extent through ins, and a tree buffer for
665 * the first block of the extent through buf.
666 *
667 * returns 0 if everything worked, non-zero otherwise.
668 */
Chris Mason4d775672007-04-20 20:23:12 -0400669int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
670 struct btrfs_root *root, u64 owner,
Chris Masonc62a1922007-04-24 12:07:39 -0400671 u64 num_blocks, u64 search_start,
Chris Mason4d775672007-04-20 20:23:12 -0400672 u64 search_end, struct btrfs_key *ins)
Chris Masonfec577f2007-02-26 10:40:21 -0500673{
674 int ret;
675 int pending_ret;
Chris Mason1261ec42007-03-20 20:35:03 -0400676 u64 super_blocks_used;
677 struct btrfs_fs_info *info = root->fs_info;
678 struct btrfs_root *extent_root = info->extent_root;
Chris Mason234b63a2007-03-13 10:46:10 -0400679 struct btrfs_extent_item extent_item;
Chris Masonf2458e12007-04-25 15:52:25 -0400680 struct btrfs_key prealloc_key;
Chris Mason037e6392007-03-07 11:50:24 -0500681
Chris Masoncf27e1e2007-03-13 09:49:06 -0400682 btrfs_set_extent_refs(&extent_item, 1);
Chris Mason4d775672007-04-20 20:23:12 -0400683 btrfs_set_extent_owner(&extent_item, owner);
Chris Masonfec577f2007-02-26 10:40:21 -0500684
Chris Mason037e6392007-03-07 11:50:24 -0500685 if (root == extent_root) {
Chris Masonf2458e12007-04-25 15:52:25 -0400686 int nr;
687 BUG_ON(info->extent_tree_prealloc_nr == 0);
Chris Mason037e6392007-03-07 11:50:24 -0500688 BUG_ON(num_blocks != 1);
Chris Mason037e6392007-03-07 11:50:24 -0500689 ins->offset = 1;
Chris Masonf2458e12007-04-25 15:52:25 -0400690 info->extent_tree_prealloc_nr--;
691 nr = info->extent_tree_prealloc_nr;
692 ins->objectid = info->extent_tree_prealloc[nr];
693 info->extent_tree_insert[info->extent_tree_insert_nr++] =
694 ins->objectid;
Chris Mason9078a3e2007-04-26 16:46:15 -0400695 ret = update_block_group(trans, root,
696 ins->objectid, ins->offset, 1);
697 BUG_ON(ret);
Chris Masonfec577f2007-02-26 10:40:21 -0500698 return 0;
699 }
Chris Masonf2458e12007-04-25 15:52:25 -0400700 /* do the real allocation */
Chris Masone089f052007-03-16 16:20:31 -0400701 ret = find_free_extent(trans, root, num_blocks, search_start,
Chris Mason037e6392007-03-07 11:50:24 -0500702 search_end, ins);
703 if (ret)
704 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -0500705
Chris Masonf2458e12007-04-25 15:52:25 -0400706 /* then do prealloc for the extent tree */
707 ret = find_free_extent(trans, root, 0, ins->objectid + ins->offset,
708 search_end, &prealloc_key);
709 if (ret)
710 return ret;
711
Chris Mason1261ec42007-03-20 20:35:03 -0400712 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
713 btrfs_set_super_blocks_used(info->disk_super, super_blocks_used +
714 num_blocks);
Chris Masone089f052007-03-16 16:20:31 -0400715 ret = btrfs_insert_item(trans, extent_root, ins, &extent_item,
716 sizeof(extent_item));
Chris Mason037e6392007-03-07 11:50:24 -0500717
Chris Masone089f052007-03-16 16:20:31 -0400718 finish_current_insert(trans, extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -0400719 pending_ret = del_pending_extents(trans, extent_root);
Chris Mason037e6392007-03-07 11:50:24 -0500720 if (ret)
721 return ret;
722 if (pending_ret)
723 return pending_ret;
Chris Mason9078a3e2007-04-26 16:46:15 -0400724 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1);
Chris Mason037e6392007-03-07 11:50:24 -0500725 return 0;
Chris Masonfec577f2007-02-26 10:40:21 -0500726}
727
728/*
729 * helper function to allocate a block for a given tree
730 * returns the tree buffer or NULL.
731 */
Chris Masone20d96d2007-03-22 12:13:20 -0400732struct buffer_head *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Chris Mason4d775672007-04-20 20:23:12 -0400733 struct btrfs_root *root)
Chris Masonfec577f2007-02-26 10:40:21 -0500734{
Chris Masone2fa7222007-03-12 16:22:34 -0400735 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -0500736 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -0400737 struct buffer_head *buf;
Chris Masonfec577f2007-02-26 10:40:21 -0500738
Chris Mason4d775672007-04-20 20:23:12 -0400739 ret = btrfs_alloc_extent(trans, root, root->root_key.objectid,
Chris Mason4d775672007-04-20 20:23:12 -0400740 1, 0, (unsigned long)-1, &ins);
Chris Masonfec577f2007-02-26 10:40:21 -0500741 if (ret) {
742 BUG();
743 return NULL;
744 }
Chris Mason9078a3e2007-04-26 16:46:15 -0400745 BUG_ON(ret);
Chris Masond98237b2007-03-28 13:57:48 -0400746 buf = btrfs_find_create_tree_block(root, ins.objectid);
Chris Masondf2ce342007-03-23 11:00:45 -0400747 set_buffer_uptodate(buf);
Chris Masonfec577f2007-02-26 10:40:21 -0500748 return buf;
749}
Chris Masona28ec192007-03-06 20:08:01 -0500750
Chris Mason6407bf62007-03-27 06:33:00 -0400751static int drop_leaf_ref(struct btrfs_trans_handle *trans,
752 struct btrfs_root *root, struct buffer_head *cur)
753{
754 struct btrfs_disk_key *key;
755 struct btrfs_leaf *leaf;
756 struct btrfs_file_extent_item *fi;
757 int i;
758 int nritems;
759 int ret;
760
761 BUG_ON(!btrfs_is_leaf(btrfs_buffer_node(cur)));
762 leaf = btrfs_buffer_leaf(cur);
763 nritems = btrfs_header_nritems(&leaf->header);
764 for (i = 0; i < nritems; i++) {
765 key = &leaf->items[i].key;
766 if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY)
767 continue;
768 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
Chris Mason236454df2007-04-19 13:37:44 -0400769 if (btrfs_file_extent_type(fi) == BTRFS_FILE_EXTENT_INLINE)
770 continue;
Chris Mason6407bf62007-03-27 06:33:00 -0400771 /*
772 * FIXME make sure to insert a trans record that
773 * repeats the snapshot del on crash
774 */
775 ret = btrfs_free_extent(trans, root,
776 btrfs_file_extent_disk_blocknr(fi),
777 btrfs_file_extent_disk_num_blocks(fi),
778 0);
779 BUG_ON(ret);
780 }
781 return 0;
782}
783
Chris Mason9aca1d52007-03-13 11:09:37 -0400784/*
785 * helper function for drop_snapshot, this walks down the tree dropping ref
786 * counts as it goes.
787 */
Chris Masone089f052007-03-16 16:20:31 -0400788static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
789 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -0500790{
Chris Masone20d96d2007-03-22 12:13:20 -0400791 struct buffer_head *next;
792 struct buffer_head *cur;
Chris Mason20524f02007-03-10 06:35:47 -0500793 u64 blocknr;
794 int ret;
795 u32 refs;
796
Chris Mason5caf2a02007-04-02 11:20:42 -0400797 WARN_ON(*level < 0);
798 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Masonb18c6682007-04-17 13:26:50 -0400799 ret = lookup_extent_ref(trans, root, bh_blocknr(path->nodes[*level]),
Chris Mason6407bf62007-03-27 06:33:00 -0400800 1, &refs);
Chris Mason20524f02007-03-10 06:35:47 -0500801 BUG_ON(ret);
802 if (refs > 1)
803 goto out;
Chris Mason9aca1d52007-03-13 11:09:37 -0400804 /*
805 * walk down to the last node level and free all the leaves
806 */
Chris Mason6407bf62007-03-27 06:33:00 -0400807 while(*level >= 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400808 WARN_ON(*level < 0);
809 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason20524f02007-03-10 06:35:47 -0500810 cur = path->nodes[*level];
Chris Mason2c90e5d2007-04-02 10:50:19 -0400811 if (btrfs_header_level(btrfs_buffer_header(cur)) != *level)
812 WARN_ON(1);
Chris Mason7518a232007-03-12 12:01:18 -0400813 if (path->slots[*level] >=
Chris Masone20d96d2007-03-22 12:13:20 -0400814 btrfs_header_nritems(btrfs_buffer_header(cur)))
Chris Mason20524f02007-03-10 06:35:47 -0500815 break;
Chris Mason6407bf62007-03-27 06:33:00 -0400816 if (*level == 0) {
817 ret = drop_leaf_ref(trans, root, cur);
818 BUG_ON(ret);
819 break;
820 }
Chris Masone20d96d2007-03-22 12:13:20 -0400821 blocknr = btrfs_node_blockptr(btrfs_buffer_node(cur),
822 path->slots[*level]);
Chris Masonb18c6682007-04-17 13:26:50 -0400823 ret = lookup_extent_ref(trans, root, blocknr, 1, &refs);
Chris Mason6407bf62007-03-27 06:33:00 -0400824 BUG_ON(ret);
825 if (refs != 1) {
Chris Mason20524f02007-03-10 06:35:47 -0500826 path->slots[*level]++;
Chris Masone089f052007-03-16 16:20:31 -0400827 ret = btrfs_free_extent(trans, root, blocknr, 1, 1);
Chris Mason20524f02007-03-10 06:35:47 -0500828 BUG_ON(ret);
829 continue;
830 }
Chris Mason20524f02007-03-10 06:35:47 -0500831 next = read_tree_block(root, blocknr);
Chris Mason5caf2a02007-04-02 11:20:42 -0400832 WARN_ON(*level <= 0);
Chris Mason83e15a22007-03-12 09:03:27 -0400833 if (path->nodes[*level-1])
Chris Mason234b63a2007-03-13 10:46:10 -0400834 btrfs_block_release(root, path->nodes[*level-1]);
Chris Mason20524f02007-03-10 06:35:47 -0500835 path->nodes[*level-1] = next;
Chris Masone20d96d2007-03-22 12:13:20 -0400836 *level = btrfs_header_level(btrfs_buffer_header(next));
Chris Mason20524f02007-03-10 06:35:47 -0500837 path->slots[*level] = 0;
838 }
839out:
Chris Mason5caf2a02007-04-02 11:20:42 -0400840 WARN_ON(*level < 0);
841 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason6407bf62007-03-27 06:33:00 -0400842 ret = btrfs_free_extent(trans, root,
Chris Mason7eccb902007-04-11 15:53:25 -0400843 bh_blocknr(path->nodes[*level]), 1, 1);
Chris Mason234b63a2007-03-13 10:46:10 -0400844 btrfs_block_release(root, path->nodes[*level]);
Chris Mason20524f02007-03-10 06:35:47 -0500845 path->nodes[*level] = NULL;
846 *level += 1;
847 BUG_ON(ret);
848 return 0;
849}
850
Chris Mason9aca1d52007-03-13 11:09:37 -0400851/*
852 * helper for dropping snapshots. This walks back up the tree in the path
853 * to find the first node higher up where we haven't yet gone through
854 * all the slots
855 */
Chris Masone089f052007-03-16 16:20:31 -0400856static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root
857 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -0500858{
859 int i;
860 int slot;
861 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400862 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Mason20524f02007-03-10 06:35:47 -0500863 slot = path->slots[i];
Chris Masone20d96d2007-03-22 12:13:20 -0400864 if (slot < btrfs_header_nritems(
865 btrfs_buffer_header(path->nodes[i])) - 1) {
Chris Mason20524f02007-03-10 06:35:47 -0500866 path->slots[i]++;
867 *level = i;
868 return 0;
869 } else {
Chris Masone089f052007-03-16 16:20:31 -0400870 ret = btrfs_free_extent(trans, root,
Chris Mason7eccb902007-04-11 15:53:25 -0400871 bh_blocknr(path->nodes[*level]),
Chris Masone089f052007-03-16 16:20:31 -0400872 1, 1);
Chris Mason6407bf62007-03-27 06:33:00 -0400873 BUG_ON(ret);
Chris Mason234b63a2007-03-13 10:46:10 -0400874 btrfs_block_release(root, path->nodes[*level]);
Chris Mason83e15a22007-03-12 09:03:27 -0400875 path->nodes[*level] = NULL;
Chris Mason20524f02007-03-10 06:35:47 -0500876 *level = i + 1;
Chris Mason20524f02007-03-10 06:35:47 -0500877 }
878 }
879 return 1;
880}
881
Chris Mason9aca1d52007-03-13 11:09:37 -0400882/*
883 * drop the reference count on the tree rooted at 'snap'. This traverses
884 * the tree freeing any blocks that have a ref count of zero after being
885 * decremented.
886 */
Chris Masone089f052007-03-16 16:20:31 -0400887int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masone20d96d2007-03-22 12:13:20 -0400888 *root, struct buffer_head *snap)
Chris Mason20524f02007-03-10 06:35:47 -0500889{
Chris Mason3768f362007-03-13 16:47:54 -0400890 int ret = 0;
Chris Mason9aca1d52007-03-13 11:09:37 -0400891 int wret;
Chris Mason20524f02007-03-10 06:35:47 -0500892 int level;
Chris Mason5caf2a02007-04-02 11:20:42 -0400893 struct btrfs_path *path;
Chris Mason20524f02007-03-10 06:35:47 -0500894 int i;
895 int orig_level;
896
Chris Mason5caf2a02007-04-02 11:20:42 -0400897 path = btrfs_alloc_path();
898 BUG_ON(!path);
899 btrfs_init_path(path);
Chris Mason20524f02007-03-10 06:35:47 -0500900
Chris Masone20d96d2007-03-22 12:13:20 -0400901 level = btrfs_header_level(btrfs_buffer_header(snap));
Chris Mason20524f02007-03-10 06:35:47 -0500902 orig_level = level;
Chris Mason5caf2a02007-04-02 11:20:42 -0400903 path->nodes[level] = snap;
904 path->slots[level] = 0;
Chris Mason20524f02007-03-10 06:35:47 -0500905 while(1) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400906 wret = walk_down_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -0400907 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -0500908 break;
Chris Mason9aca1d52007-03-13 11:09:37 -0400909 if (wret < 0)
910 ret = wret;
911
Chris Mason5caf2a02007-04-02 11:20:42 -0400912 wret = walk_up_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -0400913 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -0500914 break;
Chris Mason9aca1d52007-03-13 11:09:37 -0400915 if (wret < 0)
916 ret = wret;
Chris Mason20524f02007-03-10 06:35:47 -0500917 }
Chris Mason83e15a22007-03-12 09:03:27 -0400918 for (i = 0; i <= orig_level; i++) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400919 if (path->nodes[i]) {
920 btrfs_block_release(root, path->nodes[i]);
Chris Mason83e15a22007-03-12 09:03:27 -0400921 }
Chris Mason20524f02007-03-10 06:35:47 -0500922 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400923 btrfs_free_path(path);
Chris Mason9aca1d52007-03-13 11:09:37 -0400924 return ret;
Chris Mason20524f02007-03-10 06:35:47 -0500925}
Chris Mason9078a3e2007-04-26 16:46:15 -0400926
927int btrfs_free_block_groups(struct btrfs_fs_info *info)
928{
929 int ret;
930 struct btrfs_block_group_cache *cache[8];
931 int i;
932
933 while(1) {
934 ret = radix_tree_gang_lookup(&info->block_group_radix,
935 (void **)cache, 0,
936 ARRAY_SIZE(cache));
937 if (!ret)
938 break;
939 for (i = 0; i < ret; i++) {
940 radix_tree_delete(&info->block_group_radix,
941 cache[i]->key.objectid +
942 cache[i]->key.offset - 1);
943 kfree(cache[i]);
944 }
945 }
946 return 0;
947}
948
949int btrfs_read_block_groups(struct btrfs_root *root)
950{
951 struct btrfs_path *path;
952 int ret;
953 int err = 0;
954 struct btrfs_block_group_item *bi;
955 struct btrfs_block_group_cache *cache;
956 struct btrfs_key key;
957 struct btrfs_key found_key;
958 struct btrfs_leaf *leaf;
959 u64 group_size_blocks = BTRFS_BLOCK_GROUP_SIZE / root->blocksize;
960
961 root = root->fs_info->extent_root;
962 key.objectid = 0;
963 key.offset = group_size_blocks;
964 key.flags = 0;
965 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
966
967 path = btrfs_alloc_path();
968 if (!path)
969 return -ENOMEM;
970
971 while(1) {
972 ret = btrfs_search_slot(NULL, root->fs_info->extent_root,
973 &key, path, 0, 0);
974 if (ret != 0) {
975 err = ret;
976 break;
977 }
978 leaf = btrfs_buffer_leaf(path->nodes[0]);
979 btrfs_disk_key_to_cpu(&found_key,
980 &leaf->items[path->slots[0]].key);
981 cache = kmalloc(sizeof(*cache), GFP_NOFS);
982 if (!cache) {
983 err = -1;
984 break;
985 }
986 bi = btrfs_item_ptr(leaf, path->slots[0],
987 struct btrfs_block_group_item);
988 memcpy(&cache->item, bi, sizeof(*bi));
989 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Masoncd1bc462007-04-27 10:08:34 -0400990 cache->last_alloc = 0;
991 cache->first_free = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -0400992 key.objectid = found_key.objectid + found_key.offset;
993 btrfs_release_path(root, path);
994 ret = radix_tree_insert(&root->fs_info->block_group_radix,
995 found_key.objectid +
996 found_key.offset - 1,
997 (void *)cache);
998 BUG_ON(ret);
999 if (key.objectid >=
1000 btrfs_super_total_blocks(root->fs_info->disk_super))
1001 break;
1002 }
1003
1004 btrfs_free_path(path);
1005 return 0;
1006}