blob: 71e3b311fc42feb20a2c973fd3e3da6e1b61c7f8 [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 Masoncd1bc462007-04-27 10:08:34 -0400314 root->fs_info->block_group_cache = NULL;
Chris Masona28ec192007-03-06 20:08:01 -0500315 return 0;
316}
317
Chris Masone089f052007-03-16 16:20:31 -0400318static int finish_current_insert(struct btrfs_trans_handle *trans, struct
319 btrfs_root *extent_root)
Chris Mason037e6392007-03-07 11:50:24 -0500320{
Chris Masone2fa7222007-03-12 16:22:34 -0400321 struct btrfs_key ins;
Chris Mason234b63a2007-03-13 10:46:10 -0400322 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -0500323 int i;
324 int ret;
Chris Mason1261ec42007-03-20 20:35:03 -0400325 u64 super_blocks_used;
326 struct btrfs_fs_info *info = extent_root->fs_info;
Chris Mason037e6392007-03-07 11:50:24 -0500327
Chris Masoncf27e1e2007-03-13 09:49:06 -0400328 btrfs_set_extent_refs(&extent_item, 1);
Chris Mason037e6392007-03-07 11:50:24 -0500329 ins.offset = 1;
330 ins.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -0400331 btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
Chris Mason5d0c3e62007-04-23 17:01:05 -0400332 btrfs_set_extent_owner(&extent_item, extent_root->root_key.objectid);
Chris Mason037e6392007-03-07 11:50:24 -0500333
Chris Masonf2458e12007-04-25 15:52:25 -0400334 for (i = 0; i < extent_root->fs_info->extent_tree_insert_nr; i++) {
335 ins.objectid = extent_root->fs_info->extent_tree_insert[i];
Chris Mason1261ec42007-03-20 20:35:03 -0400336 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
337 btrfs_set_super_blocks_used(info->disk_super,
338 super_blocks_used + 1);
Chris Masone089f052007-03-16 16:20:31 -0400339 ret = btrfs_insert_item(trans, extent_root, &ins, &extent_item,
340 sizeof(extent_item));
Chris Mason037e6392007-03-07 11:50:24 -0500341 BUG_ON(ret);
342 }
Chris Masonf2458e12007-04-25 15:52:25 -0400343 extent_root->fs_info->extent_tree_insert_nr = 0;
344 extent_root->fs_info->extent_tree_prealloc_nr = 0;
Chris Mason037e6392007-03-07 11:50:24 -0500345 return 0;
346}
347
Chris Mason8ef97622007-03-26 10:15:30 -0400348static int pin_down_block(struct btrfs_root *root, u64 blocknr, int pending)
Chris Masone20d96d2007-03-22 12:13:20 -0400349{
350 int err;
Chris Mason78fae272007-03-25 11:35:08 -0400351 struct btrfs_header *header;
Chris Mason8ef97622007-03-26 10:15:30 -0400352 struct buffer_head *bh;
Chris Mason78fae272007-03-25 11:35:08 -0400353
Chris Masonf4b9aa82007-03-27 11:05:53 -0400354 if (!pending) {
Chris Masond98237b2007-03-28 13:57:48 -0400355 bh = btrfs_find_tree_block(root, blocknr);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400356 if (bh) {
357 if (buffer_uptodate(bh)) {
358 u64 transid =
359 root->fs_info->running_transaction->transid;
360 header = btrfs_buffer_header(bh);
361 if (btrfs_header_generation(header) ==
362 transid) {
363 btrfs_block_release(root, bh);
364 return 0;
365 }
Chris Masonf4b9aa82007-03-27 11:05:53 -0400366 }
Chris Masond6025572007-03-30 14:27:56 -0400367 btrfs_block_release(root, bh);
Chris Mason8ef97622007-03-26 10:15:30 -0400368 }
Chris Mason8ef97622007-03-26 10:15:30 -0400369 err = set_radix_bit(&root->fs_info->pinned_radix, blocknr);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400370 } else {
371 err = set_radix_bit(&root->fs_info->pending_del_radix, blocknr);
372 }
Chris Mason8ef97622007-03-26 10:15:30 -0400373 BUG_ON(err);
Chris Masone20d96d2007-03-22 12:13:20 -0400374 return 0;
375}
376
Chris Masona28ec192007-03-06 20:08:01 -0500377/*
378 * remove an extent from the root, returns 0 on success
379 */
Chris Masone089f052007-03-16 16:20:31 -0400380static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason78fae272007-03-25 11:35:08 -0400381 *root, u64 blocknr, u64 num_blocks, int pin)
Chris Masona28ec192007-03-06 20:08:01 -0500382{
Chris Mason5caf2a02007-04-02 11:20:42 -0400383 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -0400384 struct btrfs_key key;
Chris Mason1261ec42007-03-20 20:35:03 -0400385 struct btrfs_fs_info *info = root->fs_info;
386 struct btrfs_root *extent_root = info->extent_root;
Chris Masona28ec192007-03-06 20:08:01 -0500387 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400388 struct btrfs_extent_item *ei;
Chris Masone2fa7222007-03-12 16:22:34 -0400389 struct btrfs_key ins;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400390 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500391
Chris Masona28ec192007-03-06 20:08:01 -0500392 key.objectid = blocknr;
393 key.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -0400394 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masona28ec192007-03-06 20:08:01 -0500395 key.offset = num_blocks;
396
Chris Masone089f052007-03-16 16:20:31 -0400397 find_free_extent(trans, root, 0, 0, (u64)-1, &ins);
Chris Mason5caf2a02007-04-02 11:20:42 -0400398 path = btrfs_alloc_path();
399 BUG_ON(!path);
400 btrfs_init_path(path);
Chris Mason5f26f772007-04-05 10:38:44 -0400401
Chris Mason5caf2a02007-04-02 11:20:42 -0400402 ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
Chris Masona28ec192007-03-06 20:08:01 -0500403 if (ret) {
Chris Mason2e635a22007-03-21 11:12:56 -0400404 printk("failed to find %Lu\n", key.objectid);
Chris Mason234b63a2007-03-13 10:46:10 -0400405 btrfs_print_tree(extent_root, extent_root->node);
Chris Mason2e635a22007-03-21 11:12:56 -0400406 printk("failed to find %Lu\n", key.objectid);
Chris Masona28ec192007-03-06 20:08:01 -0500407 BUG();
408 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400409 ei = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
Chris Mason123abc82007-03-14 14:14:43 -0400410 struct btrfs_extent_item);
Chris Masona28ec192007-03-06 20:08:01 -0500411 BUG_ON(ei->refs == 0);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400412 refs = btrfs_extent_refs(ei) - 1;
413 btrfs_set_extent_refs(ei, refs);
Chris Mason5caf2a02007-04-02 11:20:42 -0400414 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400415 if (refs == 0) {
Chris Mason1261ec42007-03-20 20:35:03 -0400416 u64 super_blocks_used;
Chris Mason78fae272007-03-25 11:35:08 -0400417
418 if (pin) {
Chris Mason8ef97622007-03-26 10:15:30 -0400419 ret = pin_down_block(root, blocknr, 0);
Chris Mason78fae272007-03-25 11:35:08 -0400420 BUG_ON(ret);
421 }
422
Chris Mason1261ec42007-03-20 20:35:03 -0400423 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
424 btrfs_set_super_blocks_used(info->disk_super,
425 super_blocks_used - num_blocks);
Chris Mason5caf2a02007-04-02 11:20:42 -0400426 ret = btrfs_del_item(trans, extent_root, path);
Chris Masona28ec192007-03-06 20:08:01 -0500427 if (ret)
428 BUG();
Chris Mason9078a3e2007-04-26 16:46:15 -0400429 ret = update_block_group(trans, root, blocknr, num_blocks, 0);
430 BUG_ON(ret);
Chris Masona28ec192007-03-06 20:08:01 -0500431 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400432 btrfs_release_path(extent_root, path);
433 btrfs_free_path(path);
Chris Masone089f052007-03-16 16:20:31 -0400434 finish_current_insert(trans, extent_root);
Chris Masona28ec192007-03-06 20:08:01 -0500435 return ret;
436}
437
438/*
Chris Masonfec577f2007-02-26 10:40:21 -0500439 * find all the blocks marked as pending in the radix tree and remove
440 * them from the extent map
441 */
Chris Masone089f052007-03-16 16:20:31 -0400442static int del_pending_extents(struct btrfs_trans_handle *trans, struct
443 btrfs_root *extent_root)
Chris Masonfec577f2007-02-26 10:40:21 -0500444{
445 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -0400446 int wret;
447 int err = 0;
Chris Mason8ef97622007-03-26 10:15:30 -0400448 unsigned long gang[4];
Chris Masonfec577f2007-02-26 10:40:21 -0500449 int i;
Chris Mason8ef97622007-03-26 10:15:30 -0400450 struct radix_tree_root *pending_radix;
451 struct radix_tree_root *pinned_radix;
452
453 pending_radix = &extent_root->fs_info->pending_del_radix;
454 pinned_radix = &extent_root->fs_info->pinned_radix;
Chris Masonfec577f2007-02-26 10:40:21 -0500455
456 while(1) {
Chris Mason8ef97622007-03-26 10:15:30 -0400457 ret = find_first_radix_bit(pending_radix, gang,
458 ARRAY_SIZE(gang));
Chris Masonfec577f2007-02-26 10:40:21 -0500459 if (!ret)
460 break;
461 for (i = 0; i < ret; i++) {
Chris Mason8ef97622007-03-26 10:15:30 -0400462 wret = set_radix_bit(pinned_radix, gang[i]);
463 BUG_ON(wret);
464 wret = clear_radix_bit(pending_radix, gang[i]);
465 BUG_ON(wret);
Chris Masond5719762007-03-23 10:01:08 -0400466 wret = __free_extent(trans, extent_root,
Chris Mason8ef97622007-03-26 10:15:30 -0400467 gang[i], 1, 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400468 if (wret)
469 err = wret;
Chris Masonfec577f2007-02-26 10:40:21 -0500470 }
471 }
Chris Masone20d96d2007-03-22 12:13:20 -0400472 return err;
Chris Masonfec577f2007-02-26 10:40:21 -0500473}
474
475/*
476 * remove an extent from the root, returns 0 on success
477 */
Chris Masone089f052007-03-16 16:20:31 -0400478int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
479 *root, u64 blocknr, u64 num_blocks, int pin)
Chris Masonfec577f2007-02-26 10:40:21 -0500480{
Chris Mason9f5fae22007-03-20 14:38:32 -0400481 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Masonfec577f2007-02-26 10:40:21 -0500482 int pending_ret;
483 int ret;
Chris Masona28ec192007-03-06 20:08:01 -0500484
485 if (root == extent_root) {
Chris Mason8ef97622007-03-26 10:15:30 -0400486 pin_down_block(root, blocknr, 1);
Chris Masona28ec192007-03-06 20:08:01 -0500487 return 0;
488 }
Chris Mason78fae272007-03-25 11:35:08 -0400489 ret = __free_extent(trans, root, blocknr, num_blocks, pin);
Chris Masone20d96d2007-03-22 12:13:20 -0400490 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
Chris Masonfec577f2007-02-26 10:40:21 -0500491 return ret ? ret : pending_ret;
492}
493
494/*
495 * walks the btree of allocated extents and find a hole of a given size.
496 * The key ins is changed to record the hole:
497 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -0400498 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -0500499 * ins->offset == number of blocks
500 * Any available blocks before search_start are skipped.
501 */
Chris Masone089f052007-03-16 16:20:31 -0400502static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
503 *orig_root, u64 num_blocks, u64 search_start, u64
504 search_end, struct btrfs_key *ins)
Chris Masonfec577f2007-02-26 10:40:21 -0500505{
Chris Mason5caf2a02007-04-02 11:20:42 -0400506 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -0400507 struct btrfs_key key;
Chris Masonfec577f2007-02-26 10:40:21 -0500508 int ret;
509 u64 hole_size = 0;
510 int slot = 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400511 u64 last_block = 0;
Chris Mason037e6392007-03-07 11:50:24 -0500512 u64 test_block;
Chris Masonfec577f2007-02-26 10:40:21 -0500513 int start_found;
Chris Mason234b63a2007-03-13 10:46:10 -0400514 struct btrfs_leaf *l;
Chris Mason9f5fae22007-03-20 14:38:32 -0400515 struct btrfs_root * root = orig_root->fs_info->extent_root;
Chris Masonf2458e12007-04-25 15:52:25 -0400516 struct btrfs_fs_info *info = root->fs_info;
Chris Mason0579da42007-03-07 16:15:30 -0500517 int total_needed = num_blocks;
Chris Masonf2458e12007-04-25 15:52:25 -0400518 int total_found = 0;
519 int fill_prealloc = 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400520 int level;
Chris Masonfec577f2007-02-26 10:40:21 -0500521
Chris Masonb1a4d962007-04-04 15:27:52 -0400522 path = btrfs_alloc_path();
523 ins->flags = 0;
524 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
525
Chris Masone20d96d2007-03-22 12:13:20 -0400526 level = btrfs_header_level(btrfs_buffer_header(root->node));
Chris Masonf2458e12007-04-25 15:52:25 -0400527 if (num_blocks == 0) {
528 fill_prealloc = 1;
529 num_blocks = 1;
530 total_needed = min(level + 2, BTRFS_MAX_LEVEL) * 3;
531 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400532 find_search_start(root, 0);
533 if (info->block_group_cache &&
534 info->block_group_cache->last_alloc > search_start)
535 search_start = info->block_group_cache->last_alloc;
Chris Mason62e27492007-03-15 12:56:47 -0400536
Chris Masonfec577f2007-02-26 10:40:21 -0500537check_failed:
Chris Mason5caf2a02007-04-02 11:20:42 -0400538 btrfs_init_path(path);
Chris Masonfec577f2007-02-26 10:40:21 -0500539 ins->objectid = search_start;
540 ins->offset = 0;
Chris Masonfec577f2007-02-26 10:40:21 -0500541 start_found = 0;
Chris Mason5caf2a02007-04-02 11:20:42 -0400542 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
Chris Mason0f70abe2007-02-28 16:46:22 -0500543 if (ret < 0)
544 goto error;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500545
Chris Mason5caf2a02007-04-02 11:20:42 -0400546 if (path->slots[0] > 0)
547 path->slots[0]--;
Chris Mason0579da42007-03-07 16:15:30 -0500548
Chris Masonfec577f2007-02-26 10:40:21 -0500549 while (1) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400550 l = btrfs_buffer_leaf(path->nodes[0]);
551 slot = path->slots[0];
Chris Mason7518a232007-03-12 12:01:18 -0400552 if (slot >= btrfs_header_nritems(&l->header)) {
Chris Masonf2458e12007-04-25 15:52:25 -0400553 if (fill_prealloc) {
554 info->extent_tree_prealloc_nr = 0;
555 total_found = 0;
556 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400557 ret = btrfs_next_leaf(root, path);
Chris Masonfec577f2007-02-26 10:40:21 -0500558 if (ret == 0)
559 continue;
Chris Mason0f70abe2007-02-28 16:46:22 -0500560 if (ret < 0)
561 goto error;
Chris Masonfec577f2007-02-26 10:40:21 -0500562 if (!start_found) {
563 ins->objectid = search_start;
Chris Masonf2458e12007-04-25 15:52:25 -0400564 ins->offset = (u64)-1 - search_start;
Chris Masonfec577f2007-02-26 10:40:21 -0500565 start_found = 1;
566 goto check_pending;
567 }
568 ins->objectid = last_block > search_start ?
569 last_block : search_start;
Chris Masonf2458e12007-04-25 15:52:25 -0400570 ins->offset = (u64)-1 - ins->objectid;
Chris Masonfec577f2007-02-26 10:40:21 -0500571 goto check_pending;
572 }
Chris Masone2fa7222007-03-12 16:22:34 -0400573 btrfs_disk_key_to_cpu(&key, &l->items[slot].key);
Chris Mason9078a3e2007-04-26 16:46:15 -0400574 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY)
575 goto next;
Chris Masone2fa7222007-03-12 16:22:34 -0400576 if (key.objectid >= search_start) {
Chris Masonfec577f2007-02-26 10:40:21 -0500577 if (start_found) {
Chris Mason0579da42007-03-07 16:15:30 -0500578 if (last_block < search_start)
579 last_block = search_start;
Chris Masone2fa7222007-03-12 16:22:34 -0400580 hole_size = key.objectid - last_block;
Chris Masonf2458e12007-04-25 15:52:25 -0400581 if (hole_size > num_blocks) {
Chris Masonfec577f2007-02-26 10:40:21 -0500582 ins->objectid = last_block;
Chris Mason037e6392007-03-07 11:50:24 -0500583 ins->offset = hole_size;
Chris Masonfec577f2007-02-26 10:40:21 -0500584 goto check_pending;
585 }
Chris Mason0579da42007-03-07 16:15:30 -0500586 }
Chris Masonfec577f2007-02-26 10:40:21 -0500587 }
Chris Mason0579da42007-03-07 16:15:30 -0500588 start_found = 1;
Chris Masone2fa7222007-03-12 16:22:34 -0400589 last_block = key.objectid + key.offset;
Chris Mason9078a3e2007-04-26 16:46:15 -0400590next:
Chris Mason5caf2a02007-04-02 11:20:42 -0400591 path->slots[0]++;
Chris Masonfec577f2007-02-26 10:40:21 -0500592 }
593 // FIXME -ENOSPC
594check_pending:
595 /* we have to make sure we didn't find an extent that has already
596 * been allocated by the map tree or the original allocation
597 */
Chris Mason5caf2a02007-04-02 11:20:42 -0400598 btrfs_release_path(root, path);
Chris Masonfec577f2007-02-26 10:40:21 -0500599 BUG_ON(ins->objectid < search_start);
Chris Mason037e6392007-03-07 11:50:24 -0500600 for (test_block = ins->objectid;
Chris Masonf2458e12007-04-25 15:52:25 -0400601 test_block < ins->objectid + num_blocks; test_block++) {
602 if (test_radix_bit(&info->pinned_radix, test_block)) {
Chris Mason037e6392007-03-07 11:50:24 -0500603 search_start = test_block + 1;
Chris Masonfec577f2007-02-26 10:40:21 -0500604 goto check_failed;
605 }
606 }
Chris Masonf2458e12007-04-25 15:52:25 -0400607 if (!fill_prealloc && info->extent_tree_insert_nr) {
608 u64 last =
609 info->extent_tree_insert[info->extent_tree_insert_nr - 1];
610 if (ins->objectid + num_blocks >
611 info->extent_tree_insert[0] &&
612 ins->objectid <= last) {
613 search_start = last + 1;
614 WARN_ON(1);
615 goto check_failed;
616 }
617 }
618 if (!fill_prealloc && info->extent_tree_prealloc_nr) {
619 u64 first =
620 info->extent_tree_prealloc[info->extent_tree_prealloc_nr - 1];
621 if (ins->objectid + num_blocks > first &&
622 ins->objectid <= info->extent_tree_prealloc[0]) {
623 search_start = info->extent_tree_prealloc[0] + 1;
624 WARN_ON(1);
625 goto check_failed;
626 }
627 }
628 if (fill_prealloc) {
629 int nr;
630 test_block = ins->objectid;
631 while(test_block < ins->objectid + ins->offset &&
632 total_found < total_needed) {
633 nr = total_needed - total_found - 1;
634 BUG_ON(nr < 0);
Chris Masoncd1bc462007-04-27 10:08:34 -0400635 info->extent_tree_prealloc[nr] = test_block;
Chris Masonf2458e12007-04-25 15:52:25 -0400636 total_found++;
637 test_block++;
638 }
639 if (total_found < total_needed) {
640 search_start = test_block;
641 goto check_failed;
642 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400643 info->extent_tree_prealloc_nr = total_found;
Chris Masonf2458e12007-04-25 15:52:25 -0400644 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400645 ret = radix_tree_gang_lookup(&info->block_group_radix,
646 (void **)&info->block_group_cache,
647 ins->objectid, 1);
648 if (ret) {
649 info->block_group_cache->last_alloc = ins->objectid;
650 }
Chris Mason037e6392007-03-07 11:50:24 -0500651 ins->offset = num_blocks;
Chris Mason5caf2a02007-04-02 11:20:42 -0400652 btrfs_free_path(path);
Chris Masonfec577f2007-02-26 10:40:21 -0500653 return 0;
Chris Mason0f70abe2007-02-28 16:46:22 -0500654error:
Chris Mason5caf2a02007-04-02 11:20:42 -0400655 btrfs_release_path(root, path);
656 btrfs_free_path(path);
Chris Mason0f70abe2007-02-28 16:46:22 -0500657 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -0500658}
Chris Masonfec577f2007-02-26 10:40:21 -0500659/*
Chris Masonfec577f2007-02-26 10:40:21 -0500660 * finds a free extent and does all the dirty work required for allocation
661 * returns the key for the extent through ins, and a tree buffer for
662 * the first block of the extent through buf.
663 *
664 * returns 0 if everything worked, non-zero otherwise.
665 */
Chris Mason4d775672007-04-20 20:23:12 -0400666int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
667 struct btrfs_root *root, u64 owner,
Chris Masonc62a1922007-04-24 12:07:39 -0400668 u64 num_blocks, u64 search_start,
Chris Mason4d775672007-04-20 20:23:12 -0400669 u64 search_end, struct btrfs_key *ins)
Chris Masonfec577f2007-02-26 10:40:21 -0500670{
671 int ret;
672 int pending_ret;
Chris Mason1261ec42007-03-20 20:35:03 -0400673 u64 super_blocks_used;
674 struct btrfs_fs_info *info = root->fs_info;
675 struct btrfs_root *extent_root = info->extent_root;
Chris Mason234b63a2007-03-13 10:46:10 -0400676 struct btrfs_extent_item extent_item;
Chris Masonf2458e12007-04-25 15:52:25 -0400677 struct btrfs_key prealloc_key;
Chris Mason037e6392007-03-07 11:50:24 -0500678
Chris Masoncf27e1e2007-03-13 09:49:06 -0400679 btrfs_set_extent_refs(&extent_item, 1);
Chris Mason4d775672007-04-20 20:23:12 -0400680 btrfs_set_extent_owner(&extent_item, owner);
Chris Masonfec577f2007-02-26 10:40:21 -0500681
Chris Mason037e6392007-03-07 11:50:24 -0500682 if (root == extent_root) {
Chris Masonf2458e12007-04-25 15:52:25 -0400683 int nr;
684 BUG_ON(info->extent_tree_prealloc_nr == 0);
Chris Mason037e6392007-03-07 11:50:24 -0500685 BUG_ON(num_blocks != 1);
Chris Mason037e6392007-03-07 11:50:24 -0500686 ins->offset = 1;
Chris Masonf2458e12007-04-25 15:52:25 -0400687 info->extent_tree_prealloc_nr--;
688 nr = info->extent_tree_prealloc_nr;
689 ins->objectid = info->extent_tree_prealloc[nr];
690 info->extent_tree_insert[info->extent_tree_insert_nr++] =
691 ins->objectid;
Chris Mason9078a3e2007-04-26 16:46:15 -0400692 ret = update_block_group(trans, root,
693 ins->objectid, ins->offset, 1);
694 BUG_ON(ret);
Chris Masonfec577f2007-02-26 10:40:21 -0500695 return 0;
696 }
Chris Masonf2458e12007-04-25 15:52:25 -0400697 /* do the real allocation */
Chris Masone089f052007-03-16 16:20:31 -0400698 ret = find_free_extent(trans, root, num_blocks, search_start,
Chris Mason037e6392007-03-07 11:50:24 -0500699 search_end, ins);
700 if (ret)
701 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -0500702
Chris Masonf2458e12007-04-25 15:52:25 -0400703 /* then do prealloc for the extent tree */
704 ret = find_free_extent(trans, root, 0, ins->objectid + ins->offset,
705 search_end, &prealloc_key);
706 if (ret)
707 return ret;
708
Chris Mason1261ec42007-03-20 20:35:03 -0400709 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
710 btrfs_set_super_blocks_used(info->disk_super, super_blocks_used +
711 num_blocks);
Chris Masone089f052007-03-16 16:20:31 -0400712 ret = btrfs_insert_item(trans, extent_root, ins, &extent_item,
713 sizeof(extent_item));
Chris Mason037e6392007-03-07 11:50:24 -0500714
Chris Masone089f052007-03-16 16:20:31 -0400715 finish_current_insert(trans, extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -0400716 pending_ret = del_pending_extents(trans, extent_root);
Chris Mason037e6392007-03-07 11:50:24 -0500717 if (ret)
718 return ret;
719 if (pending_ret)
720 return pending_ret;
Chris Mason9078a3e2007-04-26 16:46:15 -0400721 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1);
Chris Mason037e6392007-03-07 11:50:24 -0500722 return 0;
Chris Masonfec577f2007-02-26 10:40:21 -0500723}
724
725/*
726 * helper function to allocate a block for a given tree
727 * returns the tree buffer or NULL.
728 */
Chris Masone20d96d2007-03-22 12:13:20 -0400729struct buffer_head *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Chris Mason4d775672007-04-20 20:23:12 -0400730 struct btrfs_root *root)
Chris Masonfec577f2007-02-26 10:40:21 -0500731{
Chris Masone2fa7222007-03-12 16:22:34 -0400732 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -0500733 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -0400734 struct buffer_head *buf;
Chris Masonfec577f2007-02-26 10:40:21 -0500735
Chris Mason4d775672007-04-20 20:23:12 -0400736 ret = btrfs_alloc_extent(trans, root, root->root_key.objectid,
Chris Mason4d775672007-04-20 20:23:12 -0400737 1, 0, (unsigned long)-1, &ins);
Chris Masonfec577f2007-02-26 10:40:21 -0500738 if (ret) {
739 BUG();
740 return NULL;
741 }
Chris Mason9078a3e2007-04-26 16:46:15 -0400742 BUG_ON(ret);
Chris Masond98237b2007-03-28 13:57:48 -0400743 buf = btrfs_find_create_tree_block(root, ins.objectid);
Chris Masondf2ce342007-03-23 11:00:45 -0400744 set_buffer_uptodate(buf);
Chris Masonfec577f2007-02-26 10:40:21 -0500745 return buf;
746}
Chris Masona28ec192007-03-06 20:08:01 -0500747
Chris Mason6407bf62007-03-27 06:33:00 -0400748static int drop_leaf_ref(struct btrfs_trans_handle *trans,
749 struct btrfs_root *root, struct buffer_head *cur)
750{
751 struct btrfs_disk_key *key;
752 struct btrfs_leaf *leaf;
753 struct btrfs_file_extent_item *fi;
754 int i;
755 int nritems;
756 int ret;
757
758 BUG_ON(!btrfs_is_leaf(btrfs_buffer_node(cur)));
759 leaf = btrfs_buffer_leaf(cur);
760 nritems = btrfs_header_nritems(&leaf->header);
761 for (i = 0; i < nritems; i++) {
762 key = &leaf->items[i].key;
763 if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY)
764 continue;
765 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
Chris Mason236454df2007-04-19 13:37:44 -0400766 if (btrfs_file_extent_type(fi) == BTRFS_FILE_EXTENT_INLINE)
767 continue;
Chris Mason6407bf62007-03-27 06:33:00 -0400768 /*
769 * FIXME make sure to insert a trans record that
770 * repeats the snapshot del on crash
771 */
772 ret = btrfs_free_extent(trans, root,
773 btrfs_file_extent_disk_blocknr(fi),
774 btrfs_file_extent_disk_num_blocks(fi),
775 0);
776 BUG_ON(ret);
777 }
778 return 0;
779}
780
Chris Mason9aca1d52007-03-13 11:09:37 -0400781/*
782 * helper function for drop_snapshot, this walks down the tree dropping ref
783 * counts as it goes.
784 */
Chris Masone089f052007-03-16 16:20:31 -0400785static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
786 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -0500787{
Chris Masone20d96d2007-03-22 12:13:20 -0400788 struct buffer_head *next;
789 struct buffer_head *cur;
Chris Mason20524f02007-03-10 06:35:47 -0500790 u64 blocknr;
791 int ret;
792 u32 refs;
793
Chris Mason5caf2a02007-04-02 11:20:42 -0400794 WARN_ON(*level < 0);
795 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Masonb18c6682007-04-17 13:26:50 -0400796 ret = lookup_extent_ref(trans, root, bh_blocknr(path->nodes[*level]),
Chris Mason6407bf62007-03-27 06:33:00 -0400797 1, &refs);
Chris Mason20524f02007-03-10 06:35:47 -0500798 BUG_ON(ret);
799 if (refs > 1)
800 goto out;
Chris Mason9aca1d52007-03-13 11:09:37 -0400801 /*
802 * walk down to the last node level and free all the leaves
803 */
Chris Mason6407bf62007-03-27 06:33:00 -0400804 while(*level >= 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400805 WARN_ON(*level < 0);
806 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason20524f02007-03-10 06:35:47 -0500807 cur = path->nodes[*level];
Chris Mason2c90e5d2007-04-02 10:50:19 -0400808 if (btrfs_header_level(btrfs_buffer_header(cur)) != *level)
809 WARN_ON(1);
Chris Mason7518a232007-03-12 12:01:18 -0400810 if (path->slots[*level] >=
Chris Masone20d96d2007-03-22 12:13:20 -0400811 btrfs_header_nritems(btrfs_buffer_header(cur)))
Chris Mason20524f02007-03-10 06:35:47 -0500812 break;
Chris Mason6407bf62007-03-27 06:33:00 -0400813 if (*level == 0) {
814 ret = drop_leaf_ref(trans, root, cur);
815 BUG_ON(ret);
816 break;
817 }
Chris Masone20d96d2007-03-22 12:13:20 -0400818 blocknr = btrfs_node_blockptr(btrfs_buffer_node(cur),
819 path->slots[*level]);
Chris Masonb18c6682007-04-17 13:26:50 -0400820 ret = lookup_extent_ref(trans, root, blocknr, 1, &refs);
Chris Mason6407bf62007-03-27 06:33:00 -0400821 BUG_ON(ret);
822 if (refs != 1) {
Chris Mason20524f02007-03-10 06:35:47 -0500823 path->slots[*level]++;
Chris Masone089f052007-03-16 16:20:31 -0400824 ret = btrfs_free_extent(trans, root, blocknr, 1, 1);
Chris Mason20524f02007-03-10 06:35:47 -0500825 BUG_ON(ret);
826 continue;
827 }
Chris Mason20524f02007-03-10 06:35:47 -0500828 next = read_tree_block(root, blocknr);
Chris Mason5caf2a02007-04-02 11:20:42 -0400829 WARN_ON(*level <= 0);
Chris Mason83e15a22007-03-12 09:03:27 -0400830 if (path->nodes[*level-1])
Chris Mason234b63a2007-03-13 10:46:10 -0400831 btrfs_block_release(root, path->nodes[*level-1]);
Chris Mason20524f02007-03-10 06:35:47 -0500832 path->nodes[*level-1] = next;
Chris Masone20d96d2007-03-22 12:13:20 -0400833 *level = btrfs_header_level(btrfs_buffer_header(next));
Chris Mason20524f02007-03-10 06:35:47 -0500834 path->slots[*level] = 0;
835 }
836out:
Chris Mason5caf2a02007-04-02 11:20:42 -0400837 WARN_ON(*level < 0);
838 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason6407bf62007-03-27 06:33:00 -0400839 ret = btrfs_free_extent(trans, root,
Chris Mason7eccb902007-04-11 15:53:25 -0400840 bh_blocknr(path->nodes[*level]), 1, 1);
Chris Mason234b63a2007-03-13 10:46:10 -0400841 btrfs_block_release(root, path->nodes[*level]);
Chris Mason20524f02007-03-10 06:35:47 -0500842 path->nodes[*level] = NULL;
843 *level += 1;
844 BUG_ON(ret);
845 return 0;
846}
847
Chris Mason9aca1d52007-03-13 11:09:37 -0400848/*
849 * helper for dropping snapshots. This walks back up the tree in the path
850 * to find the first node higher up where we haven't yet gone through
851 * all the slots
852 */
Chris Masone089f052007-03-16 16:20:31 -0400853static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root
854 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -0500855{
856 int i;
857 int slot;
858 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400859 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Mason20524f02007-03-10 06:35:47 -0500860 slot = path->slots[i];
Chris Masone20d96d2007-03-22 12:13:20 -0400861 if (slot < btrfs_header_nritems(
862 btrfs_buffer_header(path->nodes[i])) - 1) {
Chris Mason20524f02007-03-10 06:35:47 -0500863 path->slots[i]++;
864 *level = i;
865 return 0;
866 } else {
Chris Masone089f052007-03-16 16:20:31 -0400867 ret = btrfs_free_extent(trans, root,
Chris Mason7eccb902007-04-11 15:53:25 -0400868 bh_blocknr(path->nodes[*level]),
Chris Masone089f052007-03-16 16:20:31 -0400869 1, 1);
Chris Mason6407bf62007-03-27 06:33:00 -0400870 BUG_ON(ret);
Chris Mason234b63a2007-03-13 10:46:10 -0400871 btrfs_block_release(root, path->nodes[*level]);
Chris Mason83e15a22007-03-12 09:03:27 -0400872 path->nodes[*level] = NULL;
Chris Mason20524f02007-03-10 06:35:47 -0500873 *level = i + 1;
Chris Mason20524f02007-03-10 06:35:47 -0500874 }
875 }
876 return 1;
877}
878
Chris Mason9aca1d52007-03-13 11:09:37 -0400879/*
880 * drop the reference count on the tree rooted at 'snap'. This traverses
881 * the tree freeing any blocks that have a ref count of zero after being
882 * decremented.
883 */
Chris Masone089f052007-03-16 16:20:31 -0400884int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masone20d96d2007-03-22 12:13:20 -0400885 *root, struct buffer_head *snap)
Chris Mason20524f02007-03-10 06:35:47 -0500886{
Chris Mason3768f362007-03-13 16:47:54 -0400887 int ret = 0;
Chris Mason9aca1d52007-03-13 11:09:37 -0400888 int wret;
Chris Mason20524f02007-03-10 06:35:47 -0500889 int level;
Chris Mason5caf2a02007-04-02 11:20:42 -0400890 struct btrfs_path *path;
Chris Mason20524f02007-03-10 06:35:47 -0500891 int i;
892 int orig_level;
893
Chris Mason5caf2a02007-04-02 11:20:42 -0400894 path = btrfs_alloc_path();
895 BUG_ON(!path);
896 btrfs_init_path(path);
Chris Mason20524f02007-03-10 06:35:47 -0500897
Chris Masone20d96d2007-03-22 12:13:20 -0400898 level = btrfs_header_level(btrfs_buffer_header(snap));
Chris Mason20524f02007-03-10 06:35:47 -0500899 orig_level = level;
Chris Mason5caf2a02007-04-02 11:20:42 -0400900 path->nodes[level] = snap;
901 path->slots[level] = 0;
Chris Mason20524f02007-03-10 06:35:47 -0500902 while(1) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400903 wret = walk_down_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -0400904 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -0500905 break;
Chris Mason9aca1d52007-03-13 11:09:37 -0400906 if (wret < 0)
907 ret = wret;
908
Chris Mason5caf2a02007-04-02 11:20:42 -0400909 wret = walk_up_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -0400910 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -0500911 break;
Chris Mason9aca1d52007-03-13 11:09:37 -0400912 if (wret < 0)
913 ret = wret;
Chris Mason20524f02007-03-10 06:35:47 -0500914 }
Chris Mason83e15a22007-03-12 09:03:27 -0400915 for (i = 0; i <= orig_level; i++) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400916 if (path->nodes[i]) {
917 btrfs_block_release(root, path->nodes[i]);
Chris Mason83e15a22007-03-12 09:03:27 -0400918 }
Chris Mason20524f02007-03-10 06:35:47 -0500919 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400920 btrfs_free_path(path);
Chris Mason9aca1d52007-03-13 11:09:37 -0400921 return ret;
Chris Mason20524f02007-03-10 06:35:47 -0500922}
Chris Mason9078a3e2007-04-26 16:46:15 -0400923
924int btrfs_free_block_groups(struct btrfs_fs_info *info)
925{
926 int ret;
927 struct btrfs_block_group_cache *cache[8];
928 int i;
929
930 while(1) {
931 ret = radix_tree_gang_lookup(&info->block_group_radix,
932 (void **)cache, 0,
933 ARRAY_SIZE(cache));
934 if (!ret)
935 break;
936 for (i = 0; i < ret; i++) {
937 radix_tree_delete(&info->block_group_radix,
938 cache[i]->key.objectid +
939 cache[i]->key.offset - 1);
940 kfree(cache[i]);
941 }
942 }
943 return 0;
944}
945
946int btrfs_read_block_groups(struct btrfs_root *root)
947{
948 struct btrfs_path *path;
949 int ret;
950 int err = 0;
951 struct btrfs_block_group_item *bi;
952 struct btrfs_block_group_cache *cache;
953 struct btrfs_key key;
954 struct btrfs_key found_key;
955 struct btrfs_leaf *leaf;
956 u64 group_size_blocks = BTRFS_BLOCK_GROUP_SIZE / root->blocksize;
957
958 root = root->fs_info->extent_root;
959 key.objectid = 0;
960 key.offset = group_size_blocks;
961 key.flags = 0;
962 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
963
964 path = btrfs_alloc_path();
965 if (!path)
966 return -ENOMEM;
967
968 while(1) {
969 ret = btrfs_search_slot(NULL, root->fs_info->extent_root,
970 &key, path, 0, 0);
971 if (ret != 0) {
972 err = ret;
973 break;
974 }
975 leaf = btrfs_buffer_leaf(path->nodes[0]);
976 btrfs_disk_key_to_cpu(&found_key,
977 &leaf->items[path->slots[0]].key);
978 cache = kmalloc(sizeof(*cache), GFP_NOFS);
979 if (!cache) {
980 err = -1;
981 break;
982 }
983 bi = btrfs_item_ptr(leaf, path->slots[0],
984 struct btrfs_block_group_item);
985 memcpy(&cache->item, bi, sizeof(*bi));
986 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Masoncd1bc462007-04-27 10:08:34 -0400987 cache->last_alloc = 0;
988 cache->first_free = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -0400989 key.objectid = found_key.objectid + found_key.offset;
990 btrfs_release_path(root, path);
991 ret = radix_tree_insert(&root->fs_info->block_group_radix,
992 found_key.objectid +
993 found_key.offset - 1,
994 (void *)cache);
995 BUG_ON(ret);
996 if (key.objectid >=
997 btrfs_super_total_blocks(root->fs_info->disk_super))
998 break;
999 }
1000
1001 btrfs_free_path(path);
1002 return 0;
1003}