blob: 0bb4fc83cfd619454c2c6bc8e46b714983a72674 [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 Masonb18c6682007-04-17 13:26:50 -040015int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
16 struct btrfs_root *root,
17 u64 blocknr, u64 num_blocks)
Chris Mason02217ed2007-03-02 16:08:05 -050018{
Chris Mason5caf2a02007-04-02 11:20:42 -040019 struct btrfs_path *path;
Chris Mason02217ed2007-03-02 16:08:05 -050020 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -040021 struct btrfs_key key;
Chris Mason234b63a2007-03-13 10:46:10 -040022 struct btrfs_leaf *l;
23 struct btrfs_extent_item *item;
Chris Masone2fa7222007-03-12 16:22:34 -040024 struct btrfs_key ins;
Chris Masoncf27e1e2007-03-13 09:49:06 -040025 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -050026
Chris Mason9f5fae22007-03-20 14:38:32 -040027 find_free_extent(trans, root->fs_info->extent_root, 0, 0, (u64)-1,
28 &ins);
Chris Mason5caf2a02007-04-02 11:20:42 -040029 path = btrfs_alloc_path();
30 BUG_ON(!path);
31 btrfs_init_path(path);
Chris Mason02217ed2007-03-02 16:08:05 -050032 key.objectid = blocknr;
33 key.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -040034 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Mason6407bf62007-03-27 06:33:00 -040035 key.offset = num_blocks;
Chris Mason5caf2a02007-04-02 11:20:42 -040036 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -040037 0, 1);
Chris Masona429e512007-04-18 16:15:28 -040038 if (ret != 0) {
39printk("can't find block %Lu %Lu\n", blocknr, num_blocks);
Chris Masona28ec192007-03-06 20:08:01 -050040 BUG();
Chris Masona429e512007-04-18 16:15:28 -040041 }
Chris Mason02217ed2007-03-02 16:08:05 -050042 BUG_ON(ret != 0);
Chris Mason5caf2a02007-04-02 11:20:42 -040043 l = btrfs_buffer_leaf(path->nodes[0]);
44 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Masoncf27e1e2007-03-13 09:49:06 -040045 refs = btrfs_extent_refs(item);
46 btrfs_set_extent_refs(item, refs + 1);
Chris Mason5caf2a02007-04-02 11:20:42 -040047 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masona28ec192007-03-06 20:08:01 -050048
Chris Mason5caf2a02007-04-02 11:20:42 -040049 btrfs_release_path(root->fs_info->extent_root, path);
50 btrfs_free_path(path);
Chris Mason9f5fae22007-03-20 14:38:32 -040051 finish_current_insert(trans, root->fs_info->extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -040052 del_pending_extents(trans, root->fs_info->extent_root);
Chris Mason02217ed2007-03-02 16:08:05 -050053 return 0;
54}
55
Chris Masonb18c6682007-04-17 13:26:50 -040056static int lookup_extent_ref(struct btrfs_trans_handle *trans,
57 struct btrfs_root *root, u64 blocknr,
58 u64 num_blocks, u32 *refs)
Chris Masona28ec192007-03-06 20:08:01 -050059{
Chris Mason5caf2a02007-04-02 11:20:42 -040060 struct btrfs_path *path;
Chris Masona28ec192007-03-06 20:08:01 -050061 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -040062 struct btrfs_key key;
Chris Mason234b63a2007-03-13 10:46:10 -040063 struct btrfs_leaf *l;
64 struct btrfs_extent_item *item;
Chris Mason5caf2a02007-04-02 11:20:42 -040065
66 path = btrfs_alloc_path();
67 btrfs_init_path(path);
Chris Masona28ec192007-03-06 20:08:01 -050068 key.objectid = blocknr;
Chris Mason6407bf62007-03-27 06:33:00 -040069 key.offset = num_blocks;
Chris Mason62e27492007-03-15 12:56:47 -040070 key.flags = 0;
71 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Mason5caf2a02007-04-02 11:20:42 -040072 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -040073 0, 0);
Chris Masona28ec192007-03-06 20:08:01 -050074 if (ret != 0)
75 BUG();
Chris Mason5caf2a02007-04-02 11:20:42 -040076 l = btrfs_buffer_leaf(path->nodes[0]);
77 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Masoncf27e1e2007-03-13 09:49:06 -040078 *refs = btrfs_extent_refs(item);
Chris Mason5caf2a02007-04-02 11:20:42 -040079 btrfs_release_path(root->fs_info->extent_root, path);
80 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -050081 return 0;
82}
83
Chris Masonc5739bb2007-04-10 09:27:04 -040084int btrfs_inc_root_ref(struct btrfs_trans_handle *trans,
85 struct btrfs_root *root)
86{
Chris Masonb18c6682007-04-17 13:26:50 -040087 return btrfs_inc_extent_ref(trans, root, bh_blocknr(root->node), 1);
Chris Masonc5739bb2007-04-10 09:27:04 -040088}
89
Chris Masone089f052007-03-16 16:20:31 -040090int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Masone20d96d2007-03-22 12:13:20 -040091 struct buffer_head *buf)
Chris Mason02217ed2007-03-02 16:08:05 -050092{
93 u64 blocknr;
Chris Masone20d96d2007-03-22 12:13:20 -040094 struct btrfs_node *buf_node;
Chris Mason6407bf62007-03-27 06:33:00 -040095 struct btrfs_leaf *buf_leaf;
96 struct btrfs_disk_key *key;
97 struct btrfs_file_extent_item *fi;
Chris Mason02217ed2007-03-02 16:08:05 -050098 int i;
Chris Mason6407bf62007-03-27 06:33:00 -040099 int leaf;
100 int ret;
Chris Masona28ec192007-03-06 20:08:01 -0500101
Chris Mason3768f362007-03-13 16:47:54 -0400102 if (!root->ref_cows)
Chris Masona28ec192007-03-06 20:08:01 -0500103 return 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400104 buf_node = btrfs_buffer_node(buf);
Chris Mason6407bf62007-03-27 06:33:00 -0400105 leaf = btrfs_is_leaf(buf_node);
106 buf_leaf = btrfs_buffer_leaf(buf);
Chris Masone20d96d2007-03-22 12:13:20 -0400107 for (i = 0; i < btrfs_header_nritems(&buf_node->header); i++) {
Chris Mason6407bf62007-03-27 06:33:00 -0400108 if (leaf) {
109 key = &buf_leaf->items[i].key;
110 if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY)
111 continue;
112 fi = btrfs_item_ptr(buf_leaf, i,
113 struct btrfs_file_extent_item);
Chris Mason236454d2007-04-19 13:37:44 -0400114 if (btrfs_file_extent_type(fi) ==
115 BTRFS_FILE_EXTENT_INLINE)
116 continue;
Chris Masonb18c6682007-04-17 13:26:50 -0400117 ret = btrfs_inc_extent_ref(trans, root,
Chris Mason6407bf62007-03-27 06:33:00 -0400118 btrfs_file_extent_disk_blocknr(fi),
119 btrfs_file_extent_disk_num_blocks(fi));
120 BUG_ON(ret);
121 } else {
122 blocknr = btrfs_node_blockptr(buf_node, i);
Chris Masonb18c6682007-04-17 13:26:50 -0400123 ret = btrfs_inc_extent_ref(trans, root, blocknr, 1);
Chris Mason6407bf62007-03-27 06:33:00 -0400124 BUG_ON(ret);
125 }
Chris Mason02217ed2007-03-02 16:08:05 -0500126 }
127 return 0;
128}
129
Chris Mason9078a3e2007-04-26 16:46:15 -0400130static int write_one_cache_group(struct btrfs_trans_handle *trans,
131 struct btrfs_root *root,
132 struct btrfs_path *path,
133 struct btrfs_block_group_cache *cache)
134{
135 int ret;
136 int pending_ret;
137 struct btrfs_root *extent_root = root->fs_info->extent_root;
138 struct btrfs_block_group_item *bi;
139 struct btrfs_key ins;
140
141 find_free_extent(trans, extent_root, 0, 0, (u64)-1, &ins);
142 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
143 BUG_ON(ret);
144 bi = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
145 struct btrfs_block_group_item);
146 memcpy(bi, &cache->item, sizeof(*bi));
147 mark_buffer_dirty(path->nodes[0]);
148 btrfs_release_path(extent_root, path);
149
150 finish_current_insert(trans, extent_root);
151 pending_ret = del_pending_extents(trans, extent_root);
152 if (ret)
153 return ret;
154 if (pending_ret)
155 return pending_ret;
156 return 0;
157
158}
159
160int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
161 struct btrfs_root *root)
162{
163 struct btrfs_block_group_cache *cache[8];
164 int ret;
165 int err = 0;
166 int werr = 0;
167 struct radix_tree_root *radix = &root->fs_info->block_group_radix;
168 int i;
169 struct btrfs_path *path;
170
171 path = btrfs_alloc_path();
172 if (!path)
173 return -ENOMEM;
174
175 while(1) {
176 ret = radix_tree_gang_lookup_tag(radix, (void **)cache,
177 0, ARRAY_SIZE(cache),
178 BTRFS_BLOCK_GROUP_DIRTY);
179 if (!ret)
180 break;
181 for (i = 0; i < ret; i++) {
182 radix_tree_tag_clear(radix, cache[i]->key.objectid +
183 cache[i]->key.offset - 1,
184 BTRFS_BLOCK_GROUP_DIRTY);
185 err = write_one_cache_group(trans, root,
186 path, cache[i]);
187 if (err)
188 werr = err;
189 }
190 }
191 btrfs_free_path(path);
192 return werr;
193}
194
195static int update_block_group(struct btrfs_trans_handle *trans,
196 struct btrfs_root *root,
197 u64 blocknr, u64 num, int alloc)
198{
199 struct btrfs_block_group_cache *cache;
200 struct btrfs_fs_info *info = root->fs_info;
201 u64 total = num;
202 u64 old_val;
203 u64 block_in_group;
204 int ret;
205 while(total) {
206 ret = radix_tree_gang_lookup(&info->block_group_radix,
207 (void **)&cache, blocknr, 1);
208 if (!ret)
209 return -1;
210 block_in_group = blocknr - cache->key.objectid;
211 WARN_ON(block_in_group > cache->key.offset);
212 radix_tree_tag_set(&info->block_group_radix,
213 cache->key.objectid + cache->key.offset - 1,
214 BTRFS_BLOCK_GROUP_DIRTY);
215
216 old_val = btrfs_block_group_used(&cache->item);
217 num = min(total, cache->key.offset - block_in_group);
218 total -= num;
219 blocknr += num;
220 if (alloc)
221 old_val += num;
222 else
223 old_val -= num;
224 btrfs_set_block_group_used(&cache->item, old_val);
225 }
226 return 0;
227}
228
Chris Masone089f052007-03-16 16:20:31 -0400229int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, struct
230 btrfs_root *root)
Chris Masona28ec192007-03-06 20:08:01 -0500231{
Chris Mason8ef97622007-03-26 10:15:30 -0400232 unsigned long gang[8];
Chris Mason88fd1462007-03-16 08:56:18 -0400233 u64 first = 0;
Chris Masona28ec192007-03-06 20:08:01 -0500234 int ret;
235 int i;
Chris Mason8ef97622007-03-26 10:15:30 -0400236 struct radix_tree_root *pinned_radix = &root->fs_info->pinned_radix;
Chris Masona28ec192007-03-06 20:08:01 -0500237
238 while(1) {
Chris Mason8ef97622007-03-26 10:15:30 -0400239 ret = find_first_radix_bit(pinned_radix, gang,
240 ARRAY_SIZE(gang));
Chris Masona28ec192007-03-06 20:08:01 -0500241 if (!ret)
242 break;
Chris Mason88fd1462007-03-16 08:56:18 -0400243 if (!first)
Chris Mason8ef97622007-03-26 10:15:30 -0400244 first = gang[0];
Chris Mason0579da42007-03-07 16:15:30 -0500245 for (i = 0; i < ret; i++) {
Chris Mason8ef97622007-03-26 10:15:30 -0400246 clear_radix_bit(pinned_radix, gang[i]);
Chris Mason0579da42007-03-07 16:15:30 -0500247 }
Chris Masona28ec192007-03-06 20:08:01 -0500248 }
Chris Masond5719762007-03-23 10:01:08 -0400249 if (root->fs_info->last_insert.objectid > first)
250 root->fs_info->last_insert.objectid = first;
Chris Mason9f5fae22007-03-20 14:38:32 -0400251 root->fs_info->last_insert.offset = 0;
Chris Masona28ec192007-03-06 20:08:01 -0500252 return 0;
253}
254
Chris Masone089f052007-03-16 16:20:31 -0400255static int finish_current_insert(struct btrfs_trans_handle *trans, struct
256 btrfs_root *extent_root)
Chris Mason037e6392007-03-07 11:50:24 -0500257{
Chris Masone2fa7222007-03-12 16:22:34 -0400258 struct btrfs_key ins;
Chris Mason234b63a2007-03-13 10:46:10 -0400259 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -0500260 int i;
261 int ret;
Chris Mason1261ec42007-03-20 20:35:03 -0400262 u64 super_blocks_used;
263 struct btrfs_fs_info *info = extent_root->fs_info;
Chris Mason037e6392007-03-07 11:50:24 -0500264
Chris Masoncf27e1e2007-03-13 09:49:06 -0400265 btrfs_set_extent_refs(&extent_item, 1);
Chris Mason037e6392007-03-07 11:50:24 -0500266 ins.offset = 1;
267 ins.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -0400268 btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
Chris Mason5d0c3e62007-04-23 17:01:05 -0400269 btrfs_set_extent_owner(&extent_item, extent_root->root_key.objectid);
Chris Mason037e6392007-03-07 11:50:24 -0500270
Chris Masonf2458e12007-04-25 15:52:25 -0400271 for (i = 0; i < extent_root->fs_info->extent_tree_insert_nr; i++) {
272 ins.objectid = extent_root->fs_info->extent_tree_insert[i];
Chris Mason1261ec42007-03-20 20:35:03 -0400273 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
274 btrfs_set_super_blocks_used(info->disk_super,
275 super_blocks_used + 1);
Chris Masone089f052007-03-16 16:20:31 -0400276 ret = btrfs_insert_item(trans, extent_root, &ins, &extent_item,
277 sizeof(extent_item));
Chris Mason037e6392007-03-07 11:50:24 -0500278 BUG_ON(ret);
279 }
Chris Masonf2458e12007-04-25 15:52:25 -0400280 extent_root->fs_info->extent_tree_insert_nr = 0;
281 extent_root->fs_info->extent_tree_prealloc_nr = 0;
Chris Mason037e6392007-03-07 11:50:24 -0500282 return 0;
283}
284
Chris Mason8ef97622007-03-26 10:15:30 -0400285static int pin_down_block(struct btrfs_root *root, u64 blocknr, int pending)
Chris Masone20d96d2007-03-22 12:13:20 -0400286{
287 int err;
Chris Mason78fae272007-03-25 11:35:08 -0400288 struct btrfs_header *header;
Chris Mason8ef97622007-03-26 10:15:30 -0400289 struct buffer_head *bh;
Chris Mason78fae272007-03-25 11:35:08 -0400290
Chris Masonf4b9aa82007-03-27 11:05:53 -0400291 if (!pending) {
Chris Masond98237b2007-03-28 13:57:48 -0400292 bh = btrfs_find_tree_block(root, blocknr);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400293 if (bh) {
294 if (buffer_uptodate(bh)) {
295 u64 transid =
296 root->fs_info->running_transaction->transid;
297 header = btrfs_buffer_header(bh);
298 if (btrfs_header_generation(header) ==
299 transid) {
300 btrfs_block_release(root, bh);
301 return 0;
302 }
Chris Masonf4b9aa82007-03-27 11:05:53 -0400303 }
Chris Masond6025572007-03-30 14:27:56 -0400304 btrfs_block_release(root, bh);
Chris Mason8ef97622007-03-26 10:15:30 -0400305 }
Chris Mason8ef97622007-03-26 10:15:30 -0400306 err = set_radix_bit(&root->fs_info->pinned_radix, blocknr);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400307 } else {
308 err = set_radix_bit(&root->fs_info->pending_del_radix, blocknr);
309 }
Chris Mason8ef97622007-03-26 10:15:30 -0400310 BUG_ON(err);
Chris Masone20d96d2007-03-22 12:13:20 -0400311 return 0;
312}
313
Chris Masona28ec192007-03-06 20:08:01 -0500314/*
315 * remove an extent from the root, returns 0 on success
316 */
Chris Masone089f052007-03-16 16:20:31 -0400317static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason78fae272007-03-25 11:35:08 -0400318 *root, u64 blocknr, u64 num_blocks, int pin)
Chris Masona28ec192007-03-06 20:08:01 -0500319{
Chris Mason5caf2a02007-04-02 11:20:42 -0400320 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -0400321 struct btrfs_key key;
Chris Mason1261ec42007-03-20 20:35:03 -0400322 struct btrfs_fs_info *info = root->fs_info;
323 struct btrfs_root *extent_root = info->extent_root;
Chris Masona28ec192007-03-06 20:08:01 -0500324 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400325 struct btrfs_extent_item *ei;
Chris Masone2fa7222007-03-12 16:22:34 -0400326 struct btrfs_key ins;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400327 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500328
Chris Masona28ec192007-03-06 20:08:01 -0500329 key.objectid = blocknr;
330 key.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -0400331 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masona28ec192007-03-06 20:08:01 -0500332 key.offset = num_blocks;
333
Chris Masone089f052007-03-16 16:20:31 -0400334 find_free_extent(trans, root, 0, 0, (u64)-1, &ins);
Chris Mason5caf2a02007-04-02 11:20:42 -0400335 path = btrfs_alloc_path();
336 BUG_ON(!path);
337 btrfs_init_path(path);
Chris Mason5f26f772007-04-05 10:38:44 -0400338
Chris Mason5caf2a02007-04-02 11:20:42 -0400339 ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
Chris Masona28ec192007-03-06 20:08:01 -0500340 if (ret) {
Chris Mason2e635a22007-03-21 11:12:56 -0400341 printk("failed to find %Lu\n", key.objectid);
Chris Mason234b63a2007-03-13 10:46:10 -0400342 btrfs_print_tree(extent_root, extent_root->node);
Chris Mason2e635a22007-03-21 11:12:56 -0400343 printk("failed to find %Lu\n", key.objectid);
Chris Masona28ec192007-03-06 20:08:01 -0500344 BUG();
345 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400346 ei = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
Chris Mason123abc82007-03-14 14:14:43 -0400347 struct btrfs_extent_item);
Chris Masona28ec192007-03-06 20:08:01 -0500348 BUG_ON(ei->refs == 0);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400349 refs = btrfs_extent_refs(ei) - 1;
350 btrfs_set_extent_refs(ei, refs);
Chris Mason5caf2a02007-04-02 11:20:42 -0400351 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400352 if (refs == 0) {
Chris Mason1261ec42007-03-20 20:35:03 -0400353 u64 super_blocks_used;
Chris Mason78fae272007-03-25 11:35:08 -0400354
355 if (pin) {
Chris Mason8ef97622007-03-26 10:15:30 -0400356 ret = pin_down_block(root, blocknr, 0);
Chris Mason78fae272007-03-25 11:35:08 -0400357 BUG_ON(ret);
358 }
359
Chris Mason1261ec42007-03-20 20:35:03 -0400360 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
361 btrfs_set_super_blocks_used(info->disk_super,
362 super_blocks_used - num_blocks);
Chris Mason5caf2a02007-04-02 11:20:42 -0400363 ret = btrfs_del_item(trans, extent_root, path);
Chris Masona28ec192007-03-06 20:08:01 -0500364 if (ret)
365 BUG();
Chris Mason9078a3e2007-04-26 16:46:15 -0400366 ret = update_block_group(trans, root, blocknr, num_blocks, 0);
367 BUG_ON(ret);
Chris Masona28ec192007-03-06 20:08:01 -0500368 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400369 btrfs_release_path(extent_root, path);
370 btrfs_free_path(path);
Chris Masone089f052007-03-16 16:20:31 -0400371 finish_current_insert(trans, extent_root);
Chris Masona28ec192007-03-06 20:08:01 -0500372 return ret;
373}
374
375/*
Chris Masonfec577f2007-02-26 10:40:21 -0500376 * find all the blocks marked as pending in the radix tree and remove
377 * them from the extent map
378 */
Chris Masone089f052007-03-16 16:20:31 -0400379static int del_pending_extents(struct btrfs_trans_handle *trans, struct
380 btrfs_root *extent_root)
Chris Masonfec577f2007-02-26 10:40:21 -0500381{
382 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -0400383 int wret;
384 int err = 0;
Chris Mason8ef97622007-03-26 10:15:30 -0400385 unsigned long gang[4];
Chris Masonfec577f2007-02-26 10:40:21 -0500386 int i;
Chris Mason8ef97622007-03-26 10:15:30 -0400387 struct radix_tree_root *pending_radix;
388 struct radix_tree_root *pinned_radix;
389
390 pending_radix = &extent_root->fs_info->pending_del_radix;
391 pinned_radix = &extent_root->fs_info->pinned_radix;
Chris Masonfec577f2007-02-26 10:40:21 -0500392
393 while(1) {
Chris Mason8ef97622007-03-26 10:15:30 -0400394 ret = find_first_radix_bit(pending_radix, gang,
395 ARRAY_SIZE(gang));
Chris Masonfec577f2007-02-26 10:40:21 -0500396 if (!ret)
397 break;
398 for (i = 0; i < ret; i++) {
Chris Mason8ef97622007-03-26 10:15:30 -0400399 wret = set_radix_bit(pinned_radix, gang[i]);
400 BUG_ON(wret);
401 wret = clear_radix_bit(pending_radix, gang[i]);
402 BUG_ON(wret);
Chris Masond5719762007-03-23 10:01:08 -0400403 wret = __free_extent(trans, extent_root,
Chris Mason8ef97622007-03-26 10:15:30 -0400404 gang[i], 1, 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400405 if (wret)
406 err = wret;
Chris Masonfec577f2007-02-26 10:40:21 -0500407 }
408 }
Chris Masone20d96d2007-03-22 12:13:20 -0400409 return err;
Chris Masonfec577f2007-02-26 10:40:21 -0500410}
411
412/*
413 * remove an extent from the root, returns 0 on success
414 */
Chris Masone089f052007-03-16 16:20:31 -0400415int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
416 *root, u64 blocknr, u64 num_blocks, int pin)
Chris Masonfec577f2007-02-26 10:40:21 -0500417{
Chris Mason9f5fae22007-03-20 14:38:32 -0400418 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Masonfec577f2007-02-26 10:40:21 -0500419 int pending_ret;
420 int ret;
Chris Masona28ec192007-03-06 20:08:01 -0500421
422 if (root == extent_root) {
Chris Mason8ef97622007-03-26 10:15:30 -0400423 pin_down_block(root, blocknr, 1);
Chris Masona28ec192007-03-06 20:08:01 -0500424 return 0;
425 }
Chris Mason78fae272007-03-25 11:35:08 -0400426 ret = __free_extent(trans, root, blocknr, num_blocks, pin);
Chris Masone20d96d2007-03-22 12:13:20 -0400427 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
Chris Masonfec577f2007-02-26 10:40:21 -0500428 return ret ? ret : pending_ret;
429}
430
431/*
432 * walks the btree of allocated extents and find a hole of a given size.
433 * The key ins is changed to record the hole:
434 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -0400435 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -0500436 * ins->offset == number of blocks
437 * Any available blocks before search_start are skipped.
438 */
Chris Masone089f052007-03-16 16:20:31 -0400439static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
440 *orig_root, u64 num_blocks, u64 search_start, u64
441 search_end, struct btrfs_key *ins)
Chris Masonfec577f2007-02-26 10:40:21 -0500442{
Chris Mason5caf2a02007-04-02 11:20:42 -0400443 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -0400444 struct btrfs_key key;
Chris Masonfec577f2007-02-26 10:40:21 -0500445 int ret;
446 u64 hole_size = 0;
447 int slot = 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400448 u64 last_block = 0;
Chris Mason037e6392007-03-07 11:50:24 -0500449 u64 test_block;
Chris Masonfec577f2007-02-26 10:40:21 -0500450 int start_found;
Chris Mason234b63a2007-03-13 10:46:10 -0400451 struct btrfs_leaf *l;
Chris Mason9f5fae22007-03-20 14:38:32 -0400452 struct btrfs_root * root = orig_root->fs_info->extent_root;
Chris Masonf2458e12007-04-25 15:52:25 -0400453 struct btrfs_fs_info *info = root->fs_info;
Chris Mason0579da42007-03-07 16:15:30 -0500454 int total_needed = num_blocks;
Chris Masonf2458e12007-04-25 15:52:25 -0400455 int total_found = 0;
456 int fill_prealloc = 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400457 int level;
Chris Masonfec577f2007-02-26 10:40:21 -0500458
Chris Masonb1a4d962007-04-04 15:27:52 -0400459 path = btrfs_alloc_path();
460 ins->flags = 0;
461 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
462
Chris Masone20d96d2007-03-22 12:13:20 -0400463 level = btrfs_header_level(btrfs_buffer_header(root->node));
Chris Masonf2458e12007-04-25 15:52:25 -0400464 if (num_blocks == 0) {
465 fill_prealloc = 1;
466 num_blocks = 1;
467 total_needed = min(level + 2, BTRFS_MAX_LEVEL) * 3;
468 }
Chris Masonf2458e12007-04-25 15:52:25 -0400469 if (info->last_insert.objectid > search_start)
470 search_start = info->last_insert.objectid;
Chris Mason62e27492007-03-15 12:56:47 -0400471
Chris Masonfec577f2007-02-26 10:40:21 -0500472check_failed:
Chris Mason5caf2a02007-04-02 11:20:42 -0400473 btrfs_init_path(path);
Chris Masonfec577f2007-02-26 10:40:21 -0500474 ins->objectid = search_start;
475 ins->offset = 0;
Chris Masonfec577f2007-02-26 10:40:21 -0500476 start_found = 0;
Chris Mason5caf2a02007-04-02 11:20:42 -0400477 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
Chris Mason0f70abe2007-02-28 16:46:22 -0500478 if (ret < 0)
479 goto error;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500480
Chris Mason5caf2a02007-04-02 11:20:42 -0400481 if (path->slots[0] > 0)
482 path->slots[0]--;
Chris Mason0579da42007-03-07 16:15:30 -0500483
Chris Masonfec577f2007-02-26 10:40:21 -0500484 while (1) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400485 l = btrfs_buffer_leaf(path->nodes[0]);
486 slot = path->slots[0];
Chris Mason7518a232007-03-12 12:01:18 -0400487 if (slot >= btrfs_header_nritems(&l->header)) {
Chris Masonf2458e12007-04-25 15:52:25 -0400488 if (fill_prealloc) {
489 info->extent_tree_prealloc_nr = 0;
490 total_found = 0;
491 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400492 ret = btrfs_next_leaf(root, path);
Chris Masonfec577f2007-02-26 10:40:21 -0500493 if (ret == 0)
494 continue;
Chris Mason0f70abe2007-02-28 16:46:22 -0500495 if (ret < 0)
496 goto error;
Chris Masonfec577f2007-02-26 10:40:21 -0500497 if (!start_found) {
498 ins->objectid = search_start;
Chris Masonf2458e12007-04-25 15:52:25 -0400499 ins->offset = (u64)-1 - search_start;
Chris Masonfec577f2007-02-26 10:40:21 -0500500 start_found = 1;
501 goto check_pending;
502 }
503 ins->objectid = last_block > search_start ?
504 last_block : search_start;
Chris Masonf2458e12007-04-25 15:52:25 -0400505 ins->offset = (u64)-1 - ins->objectid;
Chris Masonfec577f2007-02-26 10:40:21 -0500506 goto check_pending;
507 }
Chris Masone2fa7222007-03-12 16:22:34 -0400508 btrfs_disk_key_to_cpu(&key, &l->items[slot].key);
Chris Mason9078a3e2007-04-26 16:46:15 -0400509 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY)
510 goto next;
Chris Masone2fa7222007-03-12 16:22:34 -0400511 if (key.objectid >= search_start) {
Chris Masonfec577f2007-02-26 10:40:21 -0500512 if (start_found) {
Chris Mason0579da42007-03-07 16:15:30 -0500513 if (last_block < search_start)
514 last_block = search_start;
Chris Masone2fa7222007-03-12 16:22:34 -0400515 hole_size = key.objectid - last_block;
Chris Masonf2458e12007-04-25 15:52:25 -0400516 if (hole_size > num_blocks) {
Chris Masonfec577f2007-02-26 10:40:21 -0500517 ins->objectid = last_block;
Chris Mason037e6392007-03-07 11:50:24 -0500518 ins->offset = hole_size;
Chris Masonfec577f2007-02-26 10:40:21 -0500519 goto check_pending;
520 }
Chris Mason0579da42007-03-07 16:15:30 -0500521 }
Chris Masonfec577f2007-02-26 10:40:21 -0500522 }
Chris Mason0579da42007-03-07 16:15:30 -0500523 start_found = 1;
Chris Masone2fa7222007-03-12 16:22:34 -0400524 last_block = key.objectid + key.offset;
Chris Mason9078a3e2007-04-26 16:46:15 -0400525next:
Chris Mason5caf2a02007-04-02 11:20:42 -0400526 path->slots[0]++;
Chris Masonfec577f2007-02-26 10:40:21 -0500527 }
528 // FIXME -ENOSPC
529check_pending:
530 /* we have to make sure we didn't find an extent that has already
531 * been allocated by the map tree or the original allocation
532 */
Chris Mason5caf2a02007-04-02 11:20:42 -0400533 btrfs_release_path(root, path);
Chris Masonfec577f2007-02-26 10:40:21 -0500534 BUG_ON(ins->objectid < search_start);
Chris Mason037e6392007-03-07 11:50:24 -0500535 for (test_block = ins->objectid;
Chris Masonf2458e12007-04-25 15:52:25 -0400536 test_block < ins->objectid + num_blocks; test_block++) {
537 if (test_radix_bit(&info->pinned_radix, test_block)) {
Chris Mason037e6392007-03-07 11:50:24 -0500538 search_start = test_block + 1;
Chris Masonfec577f2007-02-26 10:40:21 -0500539 goto check_failed;
540 }
541 }
Chris Masonf2458e12007-04-25 15:52:25 -0400542 if (!fill_prealloc && info->extent_tree_insert_nr) {
543 u64 last =
544 info->extent_tree_insert[info->extent_tree_insert_nr - 1];
545 if (ins->objectid + num_blocks >
546 info->extent_tree_insert[0] &&
547 ins->objectid <= last) {
548 search_start = last + 1;
549 WARN_ON(1);
550 goto check_failed;
551 }
552 }
553 if (!fill_prealloc && info->extent_tree_prealloc_nr) {
554 u64 first =
555 info->extent_tree_prealloc[info->extent_tree_prealloc_nr - 1];
556 if (ins->objectid + num_blocks > first &&
557 ins->objectid <= info->extent_tree_prealloc[0]) {
558 search_start = info->extent_tree_prealloc[0] + 1;
559 WARN_ON(1);
560 goto check_failed;
561 }
562 }
563 if (fill_prealloc) {
564 int nr;
565 test_block = ins->objectid;
566 while(test_block < ins->objectid + ins->offset &&
567 total_found < total_needed) {
568 nr = total_needed - total_found - 1;
569 BUG_ON(nr < 0);
570 root->fs_info->extent_tree_prealloc[nr] =
571 test_block;
572 total_found++;
573 test_block++;
574 }
575 if (total_found < total_needed) {
576 search_start = test_block;
577 goto check_failed;
578 }
579 root->fs_info->extent_tree_prealloc_nr = total_found;
580 }
Chris Mason9f5fae22007-03-20 14:38:32 -0400581 root->fs_info->last_insert.objectid = ins->objectid;
Chris Mason037e6392007-03-07 11:50:24 -0500582 ins->offset = num_blocks;
Chris Mason5caf2a02007-04-02 11:20:42 -0400583 btrfs_free_path(path);
Chris Masonfec577f2007-02-26 10:40:21 -0500584 return 0;
Chris Mason0f70abe2007-02-28 16:46:22 -0500585error:
Chris Mason5caf2a02007-04-02 11:20:42 -0400586 btrfs_release_path(root, path);
587 btrfs_free_path(path);
Chris Mason0f70abe2007-02-28 16:46:22 -0500588 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -0500589}
Chris Masonfec577f2007-02-26 10:40:21 -0500590/*
Chris Masonfec577f2007-02-26 10:40:21 -0500591 * finds a free extent and does all the dirty work required for allocation
592 * returns the key for the extent through ins, and a tree buffer for
593 * the first block of the extent through buf.
594 *
595 * returns 0 if everything worked, non-zero otherwise.
596 */
Chris Mason4d775672007-04-20 20:23:12 -0400597int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
598 struct btrfs_root *root, u64 owner,
Chris Masonc62a1922007-04-24 12:07:39 -0400599 u64 num_blocks, u64 search_start,
Chris Mason4d775672007-04-20 20:23:12 -0400600 u64 search_end, struct btrfs_key *ins)
Chris Masonfec577f2007-02-26 10:40:21 -0500601{
602 int ret;
603 int pending_ret;
Chris Mason1261ec42007-03-20 20:35:03 -0400604 u64 super_blocks_used;
605 struct btrfs_fs_info *info = root->fs_info;
606 struct btrfs_root *extent_root = info->extent_root;
Chris Mason234b63a2007-03-13 10:46:10 -0400607 struct btrfs_extent_item extent_item;
Chris Masonf2458e12007-04-25 15:52:25 -0400608 struct btrfs_key prealloc_key;
Chris Mason037e6392007-03-07 11:50:24 -0500609
Chris Masoncf27e1e2007-03-13 09:49:06 -0400610 btrfs_set_extent_refs(&extent_item, 1);
Chris Mason4d775672007-04-20 20:23:12 -0400611 btrfs_set_extent_owner(&extent_item, owner);
Chris Masonfec577f2007-02-26 10:40:21 -0500612
Chris Mason037e6392007-03-07 11:50:24 -0500613 if (root == extent_root) {
Chris Masonf2458e12007-04-25 15:52:25 -0400614 int nr;
615 BUG_ON(info->extent_tree_prealloc_nr == 0);
Chris Mason037e6392007-03-07 11:50:24 -0500616 BUG_ON(num_blocks != 1);
Chris Mason037e6392007-03-07 11:50:24 -0500617 ins->offset = 1;
Chris Masonf2458e12007-04-25 15:52:25 -0400618 info->extent_tree_prealloc_nr--;
619 nr = info->extent_tree_prealloc_nr;
620 ins->objectid = info->extent_tree_prealloc[nr];
621 info->extent_tree_insert[info->extent_tree_insert_nr++] =
622 ins->objectid;
Chris Mason9078a3e2007-04-26 16:46:15 -0400623 ret = update_block_group(trans, root,
624 ins->objectid, ins->offset, 1);
625 BUG_ON(ret);
Chris Masonfec577f2007-02-26 10:40:21 -0500626 return 0;
627 }
Chris Masonf2458e12007-04-25 15:52:25 -0400628 /* do the real allocation */
Chris Masone089f052007-03-16 16:20:31 -0400629 ret = find_free_extent(trans, root, num_blocks, search_start,
Chris Mason037e6392007-03-07 11:50:24 -0500630 search_end, ins);
631 if (ret)
632 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -0500633
Chris Masonf2458e12007-04-25 15:52:25 -0400634 /* then do prealloc for the extent tree */
635 ret = find_free_extent(trans, root, 0, ins->objectid + ins->offset,
636 search_end, &prealloc_key);
637 if (ret)
638 return ret;
639
Chris Mason1261ec42007-03-20 20:35:03 -0400640 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
641 btrfs_set_super_blocks_used(info->disk_super, super_blocks_used +
642 num_blocks);
Chris Masone089f052007-03-16 16:20:31 -0400643 ret = btrfs_insert_item(trans, extent_root, ins, &extent_item,
644 sizeof(extent_item));
Chris Mason037e6392007-03-07 11:50:24 -0500645
Chris Masone089f052007-03-16 16:20:31 -0400646 finish_current_insert(trans, extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -0400647 pending_ret = del_pending_extents(trans, extent_root);
Chris Mason037e6392007-03-07 11:50:24 -0500648 if (ret)
649 return ret;
650 if (pending_ret)
651 return pending_ret;
Chris Mason9078a3e2007-04-26 16:46:15 -0400652 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1);
Chris Mason037e6392007-03-07 11:50:24 -0500653 return 0;
Chris Masonfec577f2007-02-26 10:40:21 -0500654}
655
656/*
657 * helper function to allocate a block for a given tree
658 * returns the tree buffer or NULL.
659 */
Chris Masone20d96d2007-03-22 12:13:20 -0400660struct buffer_head *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Chris Mason4d775672007-04-20 20:23:12 -0400661 struct btrfs_root *root)
Chris Masonfec577f2007-02-26 10:40:21 -0500662{
Chris Masone2fa7222007-03-12 16:22:34 -0400663 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -0500664 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -0400665 struct buffer_head *buf;
Chris Masonfec577f2007-02-26 10:40:21 -0500666
Chris Mason4d775672007-04-20 20:23:12 -0400667 ret = btrfs_alloc_extent(trans, root, root->root_key.objectid,
Chris Mason4d775672007-04-20 20:23:12 -0400668 1, 0, (unsigned long)-1, &ins);
Chris Masonfec577f2007-02-26 10:40:21 -0500669 if (ret) {
670 BUG();
671 return NULL;
672 }
Chris Mason9078a3e2007-04-26 16:46:15 -0400673 BUG_ON(ret);
Chris Masond98237b2007-03-28 13:57:48 -0400674 buf = btrfs_find_create_tree_block(root, ins.objectid);
Chris Masondf2ce342007-03-23 11:00:45 -0400675 set_buffer_uptodate(buf);
Chris Masonfec577f2007-02-26 10:40:21 -0500676 return buf;
677}
Chris Masona28ec192007-03-06 20:08:01 -0500678
Chris Mason6407bf62007-03-27 06:33:00 -0400679static int drop_leaf_ref(struct btrfs_trans_handle *trans,
680 struct btrfs_root *root, struct buffer_head *cur)
681{
682 struct btrfs_disk_key *key;
683 struct btrfs_leaf *leaf;
684 struct btrfs_file_extent_item *fi;
685 int i;
686 int nritems;
687 int ret;
688
689 BUG_ON(!btrfs_is_leaf(btrfs_buffer_node(cur)));
690 leaf = btrfs_buffer_leaf(cur);
691 nritems = btrfs_header_nritems(&leaf->header);
692 for (i = 0; i < nritems; i++) {
693 key = &leaf->items[i].key;
694 if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY)
695 continue;
696 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
Chris Mason236454d2007-04-19 13:37:44 -0400697 if (btrfs_file_extent_type(fi) == BTRFS_FILE_EXTENT_INLINE)
698 continue;
Chris Mason6407bf62007-03-27 06:33:00 -0400699 /*
700 * FIXME make sure to insert a trans record that
701 * repeats the snapshot del on crash
702 */
703 ret = btrfs_free_extent(trans, root,
704 btrfs_file_extent_disk_blocknr(fi),
705 btrfs_file_extent_disk_num_blocks(fi),
706 0);
707 BUG_ON(ret);
708 }
709 return 0;
710}
711
Chris Mason9aca1d52007-03-13 11:09:37 -0400712/*
713 * helper function for drop_snapshot, this walks down the tree dropping ref
714 * counts as it goes.
715 */
Chris Masone089f052007-03-16 16:20:31 -0400716static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
717 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -0500718{
Chris Masone20d96d2007-03-22 12:13:20 -0400719 struct buffer_head *next;
720 struct buffer_head *cur;
Chris Mason20524f02007-03-10 06:35:47 -0500721 u64 blocknr;
722 int ret;
723 u32 refs;
724
Chris Mason5caf2a02007-04-02 11:20:42 -0400725 WARN_ON(*level < 0);
726 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Masonb18c6682007-04-17 13:26:50 -0400727 ret = lookup_extent_ref(trans, root, bh_blocknr(path->nodes[*level]),
Chris Mason6407bf62007-03-27 06:33:00 -0400728 1, &refs);
Chris Mason20524f02007-03-10 06:35:47 -0500729 BUG_ON(ret);
730 if (refs > 1)
731 goto out;
Chris Mason9aca1d52007-03-13 11:09:37 -0400732 /*
733 * walk down to the last node level and free all the leaves
734 */
Chris Mason6407bf62007-03-27 06:33:00 -0400735 while(*level >= 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400736 WARN_ON(*level < 0);
737 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason20524f02007-03-10 06:35:47 -0500738 cur = path->nodes[*level];
Chris Mason2c90e5d2007-04-02 10:50:19 -0400739 if (btrfs_header_level(btrfs_buffer_header(cur)) != *level)
740 WARN_ON(1);
Chris Mason7518a232007-03-12 12:01:18 -0400741 if (path->slots[*level] >=
Chris Masone20d96d2007-03-22 12:13:20 -0400742 btrfs_header_nritems(btrfs_buffer_header(cur)))
Chris Mason20524f02007-03-10 06:35:47 -0500743 break;
Chris Mason6407bf62007-03-27 06:33:00 -0400744 if (*level == 0) {
745 ret = drop_leaf_ref(trans, root, cur);
746 BUG_ON(ret);
747 break;
748 }
Chris Masone20d96d2007-03-22 12:13:20 -0400749 blocknr = btrfs_node_blockptr(btrfs_buffer_node(cur),
750 path->slots[*level]);
Chris Masonb18c6682007-04-17 13:26:50 -0400751 ret = lookup_extent_ref(trans, root, blocknr, 1, &refs);
Chris Mason6407bf62007-03-27 06:33:00 -0400752 BUG_ON(ret);
753 if (refs != 1) {
Chris Mason20524f02007-03-10 06:35:47 -0500754 path->slots[*level]++;
Chris Masone089f052007-03-16 16:20:31 -0400755 ret = btrfs_free_extent(trans, root, blocknr, 1, 1);
Chris Mason20524f02007-03-10 06:35:47 -0500756 BUG_ON(ret);
757 continue;
758 }
Chris Mason20524f02007-03-10 06:35:47 -0500759 next = read_tree_block(root, blocknr);
Chris Mason5caf2a02007-04-02 11:20:42 -0400760 WARN_ON(*level <= 0);
Chris Mason83e15a22007-03-12 09:03:27 -0400761 if (path->nodes[*level-1])
Chris Mason234b63a2007-03-13 10:46:10 -0400762 btrfs_block_release(root, path->nodes[*level-1]);
Chris Mason20524f02007-03-10 06:35:47 -0500763 path->nodes[*level-1] = next;
Chris Masone20d96d2007-03-22 12:13:20 -0400764 *level = btrfs_header_level(btrfs_buffer_header(next));
Chris Mason20524f02007-03-10 06:35:47 -0500765 path->slots[*level] = 0;
766 }
767out:
Chris Mason5caf2a02007-04-02 11:20:42 -0400768 WARN_ON(*level < 0);
769 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason6407bf62007-03-27 06:33:00 -0400770 ret = btrfs_free_extent(trans, root,
Chris Mason7eccb902007-04-11 15:53:25 -0400771 bh_blocknr(path->nodes[*level]), 1, 1);
Chris Mason234b63a2007-03-13 10:46:10 -0400772 btrfs_block_release(root, path->nodes[*level]);
Chris Mason20524f02007-03-10 06:35:47 -0500773 path->nodes[*level] = NULL;
774 *level += 1;
775 BUG_ON(ret);
776 return 0;
777}
778
Chris Mason9aca1d52007-03-13 11:09:37 -0400779/*
780 * helper for dropping snapshots. This walks back up the tree in the path
781 * to find the first node higher up where we haven't yet gone through
782 * all the slots
783 */
Chris Masone089f052007-03-16 16:20:31 -0400784static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root
785 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -0500786{
787 int i;
788 int slot;
789 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400790 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Mason20524f02007-03-10 06:35:47 -0500791 slot = path->slots[i];
Chris Masone20d96d2007-03-22 12:13:20 -0400792 if (slot < btrfs_header_nritems(
793 btrfs_buffer_header(path->nodes[i])) - 1) {
Chris Mason20524f02007-03-10 06:35:47 -0500794 path->slots[i]++;
795 *level = i;
796 return 0;
797 } else {
Chris Masone089f052007-03-16 16:20:31 -0400798 ret = btrfs_free_extent(trans, root,
Chris Mason7eccb902007-04-11 15:53:25 -0400799 bh_blocknr(path->nodes[*level]),
Chris Masone089f052007-03-16 16:20:31 -0400800 1, 1);
Chris Mason6407bf62007-03-27 06:33:00 -0400801 BUG_ON(ret);
Chris Mason234b63a2007-03-13 10:46:10 -0400802 btrfs_block_release(root, path->nodes[*level]);
Chris Mason83e15a22007-03-12 09:03:27 -0400803 path->nodes[*level] = NULL;
Chris Mason20524f02007-03-10 06:35:47 -0500804 *level = i + 1;
Chris Mason20524f02007-03-10 06:35:47 -0500805 }
806 }
807 return 1;
808}
809
Chris Mason9aca1d52007-03-13 11:09:37 -0400810/*
811 * drop the reference count on the tree rooted at 'snap'. This traverses
812 * the tree freeing any blocks that have a ref count of zero after being
813 * decremented.
814 */
Chris Masone089f052007-03-16 16:20:31 -0400815int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masone20d96d2007-03-22 12:13:20 -0400816 *root, struct buffer_head *snap)
Chris Mason20524f02007-03-10 06:35:47 -0500817{
Chris Mason3768f362007-03-13 16:47:54 -0400818 int ret = 0;
Chris Mason9aca1d52007-03-13 11:09:37 -0400819 int wret;
Chris Mason20524f02007-03-10 06:35:47 -0500820 int level;
Chris Mason5caf2a02007-04-02 11:20:42 -0400821 struct btrfs_path *path;
Chris Mason20524f02007-03-10 06:35:47 -0500822 int i;
823 int orig_level;
824
Chris Mason5caf2a02007-04-02 11:20:42 -0400825 path = btrfs_alloc_path();
826 BUG_ON(!path);
827 btrfs_init_path(path);
Chris Mason20524f02007-03-10 06:35:47 -0500828
Chris Masone20d96d2007-03-22 12:13:20 -0400829 level = btrfs_header_level(btrfs_buffer_header(snap));
Chris Mason20524f02007-03-10 06:35:47 -0500830 orig_level = level;
Chris Mason5caf2a02007-04-02 11:20:42 -0400831 path->nodes[level] = snap;
832 path->slots[level] = 0;
Chris Mason20524f02007-03-10 06:35:47 -0500833 while(1) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400834 wret = walk_down_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -0400835 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -0500836 break;
Chris Mason9aca1d52007-03-13 11:09:37 -0400837 if (wret < 0)
838 ret = wret;
839
Chris Mason5caf2a02007-04-02 11:20:42 -0400840 wret = walk_up_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -0400841 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -0500842 break;
Chris Mason9aca1d52007-03-13 11:09:37 -0400843 if (wret < 0)
844 ret = wret;
Chris Mason20524f02007-03-10 06:35:47 -0500845 }
Chris Mason83e15a22007-03-12 09:03:27 -0400846 for (i = 0; i <= orig_level; i++) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400847 if (path->nodes[i]) {
848 btrfs_block_release(root, path->nodes[i]);
Chris Mason83e15a22007-03-12 09:03:27 -0400849 }
Chris Mason20524f02007-03-10 06:35:47 -0500850 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400851 btrfs_free_path(path);
Chris Mason9aca1d52007-03-13 11:09:37 -0400852 return ret;
Chris Mason20524f02007-03-10 06:35:47 -0500853}
Chris Mason9078a3e2007-04-26 16:46:15 -0400854
855int btrfs_free_block_groups(struct btrfs_fs_info *info)
856{
857 int ret;
858 struct btrfs_block_group_cache *cache[8];
859 int i;
860
861 while(1) {
862 ret = radix_tree_gang_lookup(&info->block_group_radix,
863 (void **)cache, 0,
864 ARRAY_SIZE(cache));
865 if (!ret)
866 break;
867 for (i = 0; i < ret; i++) {
868 radix_tree_delete(&info->block_group_radix,
869 cache[i]->key.objectid +
870 cache[i]->key.offset - 1);
871 kfree(cache[i]);
872 }
873 }
874 return 0;
875}
876
877int btrfs_read_block_groups(struct btrfs_root *root)
878{
879 struct btrfs_path *path;
880 int ret;
881 int err = 0;
882 struct btrfs_block_group_item *bi;
883 struct btrfs_block_group_cache *cache;
884 struct btrfs_key key;
885 struct btrfs_key found_key;
886 struct btrfs_leaf *leaf;
887 u64 group_size_blocks = BTRFS_BLOCK_GROUP_SIZE / root->blocksize;
888
889 root = root->fs_info->extent_root;
890 key.objectid = 0;
891 key.offset = group_size_blocks;
892 key.flags = 0;
893 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
894
895 path = btrfs_alloc_path();
896 if (!path)
897 return -ENOMEM;
898
899 while(1) {
900 ret = btrfs_search_slot(NULL, root->fs_info->extent_root,
901 &key, path, 0, 0);
902 if (ret != 0) {
903 err = ret;
904 break;
905 }
906 leaf = btrfs_buffer_leaf(path->nodes[0]);
907 btrfs_disk_key_to_cpu(&found_key,
908 &leaf->items[path->slots[0]].key);
909 cache = kmalloc(sizeof(*cache), GFP_NOFS);
910 if (!cache) {
911 err = -1;
912 break;
913 }
914 bi = btrfs_item_ptr(leaf, path->slots[0],
915 struct btrfs_block_group_item);
916 memcpy(&cache->item, bi, sizeof(*bi));
917 memcpy(&cache->key, &found_key, sizeof(found_key));
918 key.objectid = found_key.objectid + found_key.offset;
919 btrfs_release_path(root, path);
920 ret = radix_tree_insert(&root->fs_info->block_group_radix,
921 found_key.objectid +
922 found_key.offset - 1,
923 (void *)cache);
924 BUG_ON(ret);
925 if (key.objectid >=
926 btrfs_super_total_blocks(root->fs_info->disk_super))
927 break;
928 }
929
930 btrfs_free_path(path);
931 return 0;
932}