blob: e6fe3fd388194b7fe6aac8c7e42cab38d8048410 [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 Mason236454df2007-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 Masone089f052007-03-16 16:20:31 -0400130int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, struct
131 btrfs_root *root)
Chris Masona28ec192007-03-06 20:08:01 -0500132{
Chris Mason8ef97622007-03-26 10:15:30 -0400133 unsigned long gang[8];
Chris Mason88fd1462007-03-16 08:56:18 -0400134 u64 first = 0;
Chris Masona28ec192007-03-06 20:08:01 -0500135 int ret;
136 int i;
Chris Mason8ef97622007-03-26 10:15:30 -0400137 struct radix_tree_root *pinned_radix = &root->fs_info->pinned_radix;
Chris Masona28ec192007-03-06 20:08:01 -0500138
139 while(1) {
Chris Mason8ef97622007-03-26 10:15:30 -0400140 ret = find_first_radix_bit(pinned_radix, gang,
141 ARRAY_SIZE(gang));
Chris Masona28ec192007-03-06 20:08:01 -0500142 if (!ret)
143 break;
Chris Mason88fd1462007-03-16 08:56:18 -0400144 if (!first)
Chris Mason8ef97622007-03-26 10:15:30 -0400145 first = gang[0];
Chris Mason0579da42007-03-07 16:15:30 -0500146 for (i = 0; i < ret; i++) {
Chris Mason8ef97622007-03-26 10:15:30 -0400147 clear_radix_bit(pinned_radix, gang[i]);
Chris Mason0579da42007-03-07 16:15:30 -0500148 }
Chris Masona28ec192007-03-06 20:08:01 -0500149 }
Chris Masond5719762007-03-23 10:01:08 -0400150 if (root->fs_info->last_insert.objectid > first)
151 root->fs_info->last_insert.objectid = first;
Chris Mason9f5fae22007-03-20 14:38:32 -0400152 root->fs_info->last_insert.offset = 0;
Chris Masona28ec192007-03-06 20:08:01 -0500153 return 0;
154}
155
Chris Masone089f052007-03-16 16:20:31 -0400156static int finish_current_insert(struct btrfs_trans_handle *trans, struct
157 btrfs_root *extent_root)
Chris Mason037e6392007-03-07 11:50:24 -0500158{
Chris Masone2fa7222007-03-12 16:22:34 -0400159 struct btrfs_key ins;
Chris Mason234b63a2007-03-13 10:46:10 -0400160 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -0500161 int i;
162 int ret;
Chris Mason1261ec42007-03-20 20:35:03 -0400163 u64 super_blocks_used;
164 struct btrfs_fs_info *info = extent_root->fs_info;
Chris Mason037e6392007-03-07 11:50:24 -0500165
Chris Masoncf27e1e2007-03-13 09:49:06 -0400166 btrfs_set_extent_refs(&extent_item, 1);
Chris Mason037e6392007-03-07 11:50:24 -0500167 ins.offset = 1;
168 ins.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -0400169 btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
Chris Mason5d0c3e62007-04-23 17:01:05 -0400170 btrfs_set_extent_owner(&extent_item, extent_root->root_key.objectid);
Chris Mason037e6392007-03-07 11:50:24 -0500171
Chris Masonf2458e12007-04-25 15:52:25 -0400172 for (i = 0; i < extent_root->fs_info->extent_tree_insert_nr; i++) {
173 ins.objectid = extent_root->fs_info->extent_tree_insert[i];
Chris Mason1261ec42007-03-20 20:35:03 -0400174 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
175 btrfs_set_super_blocks_used(info->disk_super,
176 super_blocks_used + 1);
Chris Masone089f052007-03-16 16:20:31 -0400177 ret = btrfs_insert_item(trans, extent_root, &ins, &extent_item,
178 sizeof(extent_item));
Chris Mason037e6392007-03-07 11:50:24 -0500179 BUG_ON(ret);
180 }
Chris Masonf2458e12007-04-25 15:52:25 -0400181 extent_root->fs_info->extent_tree_insert_nr = 0;
182 extent_root->fs_info->extent_tree_prealloc_nr = 0;
Chris Mason037e6392007-03-07 11:50:24 -0500183 return 0;
184}
185
Chris Mason8ef97622007-03-26 10:15:30 -0400186static int pin_down_block(struct btrfs_root *root, u64 blocknr, int pending)
Chris Masone20d96d2007-03-22 12:13:20 -0400187{
188 int err;
Chris Mason78fae272007-03-25 11:35:08 -0400189 struct btrfs_header *header;
Chris Mason8ef97622007-03-26 10:15:30 -0400190 struct buffer_head *bh;
Chris Mason78fae272007-03-25 11:35:08 -0400191
Chris Masonf4b9aa82007-03-27 11:05:53 -0400192 if (!pending) {
Chris Masond98237b2007-03-28 13:57:48 -0400193 bh = btrfs_find_tree_block(root, blocknr);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400194 if (bh) {
195 if (buffer_uptodate(bh)) {
196 u64 transid =
197 root->fs_info->running_transaction->transid;
198 header = btrfs_buffer_header(bh);
199 if (btrfs_header_generation(header) ==
200 transid) {
201 btrfs_block_release(root, bh);
202 return 0;
203 }
Chris Masonf4b9aa82007-03-27 11:05:53 -0400204 }
Chris Masond6025572007-03-30 14:27:56 -0400205 btrfs_block_release(root, bh);
Chris Mason8ef97622007-03-26 10:15:30 -0400206 }
Chris Mason8ef97622007-03-26 10:15:30 -0400207 err = set_radix_bit(&root->fs_info->pinned_radix, blocknr);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400208 } else {
209 err = set_radix_bit(&root->fs_info->pending_del_radix, blocknr);
210 }
Chris Mason8ef97622007-03-26 10:15:30 -0400211 BUG_ON(err);
Chris Masone20d96d2007-03-22 12:13:20 -0400212 return 0;
213}
214
Chris Masona28ec192007-03-06 20:08:01 -0500215/*
216 * remove an extent from the root, returns 0 on success
217 */
Chris Masone089f052007-03-16 16:20:31 -0400218static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason78fae272007-03-25 11:35:08 -0400219 *root, u64 blocknr, u64 num_blocks, int pin)
Chris Masona28ec192007-03-06 20:08:01 -0500220{
Chris Mason5caf2a02007-04-02 11:20:42 -0400221 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -0400222 struct btrfs_key key;
Chris Mason1261ec42007-03-20 20:35:03 -0400223 struct btrfs_fs_info *info = root->fs_info;
224 struct btrfs_root *extent_root = info->extent_root;
Chris Masona28ec192007-03-06 20:08:01 -0500225 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400226 struct btrfs_extent_item *ei;
Chris Masone2fa7222007-03-12 16:22:34 -0400227 struct btrfs_key ins;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400228 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500229
Chris Masona28ec192007-03-06 20:08:01 -0500230 key.objectid = blocknr;
231 key.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -0400232 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masona28ec192007-03-06 20:08:01 -0500233 key.offset = num_blocks;
234
Chris Masone089f052007-03-16 16:20:31 -0400235 find_free_extent(trans, root, 0, 0, (u64)-1, &ins);
Chris Mason5caf2a02007-04-02 11:20:42 -0400236 path = btrfs_alloc_path();
237 BUG_ON(!path);
238 btrfs_init_path(path);
Chris Mason5f26f772007-04-05 10:38:44 -0400239
Chris Mason5caf2a02007-04-02 11:20:42 -0400240 ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
Chris Masona28ec192007-03-06 20:08:01 -0500241 if (ret) {
Chris Mason2e635a22007-03-21 11:12:56 -0400242 printk("failed to find %Lu\n", key.objectid);
Chris Mason234b63a2007-03-13 10:46:10 -0400243 btrfs_print_tree(extent_root, extent_root->node);
Chris Mason2e635a22007-03-21 11:12:56 -0400244 printk("failed to find %Lu\n", key.objectid);
Chris Masona28ec192007-03-06 20:08:01 -0500245 BUG();
246 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400247 ei = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
Chris Mason123abc82007-03-14 14:14:43 -0400248 struct btrfs_extent_item);
Chris Masona28ec192007-03-06 20:08:01 -0500249 BUG_ON(ei->refs == 0);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400250 refs = btrfs_extent_refs(ei) - 1;
251 btrfs_set_extent_refs(ei, refs);
Chris Mason5caf2a02007-04-02 11:20:42 -0400252 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400253 if (refs == 0) {
Chris Mason1261ec42007-03-20 20:35:03 -0400254 u64 super_blocks_used;
Chris Mason78fae272007-03-25 11:35:08 -0400255
256 if (pin) {
Chris Mason8ef97622007-03-26 10:15:30 -0400257 ret = pin_down_block(root, blocknr, 0);
Chris Mason78fae272007-03-25 11:35:08 -0400258 BUG_ON(ret);
259 }
260
Chris Mason1261ec42007-03-20 20:35:03 -0400261 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
262 btrfs_set_super_blocks_used(info->disk_super,
263 super_blocks_used - num_blocks);
Chris Mason5caf2a02007-04-02 11:20:42 -0400264 ret = btrfs_del_item(trans, extent_root, path);
Chris Masona28ec192007-03-06 20:08:01 -0500265 if (ret)
266 BUG();
267 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400268 btrfs_release_path(extent_root, path);
269 btrfs_free_path(path);
Chris Masone089f052007-03-16 16:20:31 -0400270 finish_current_insert(trans, extent_root);
Chris Masona28ec192007-03-06 20:08:01 -0500271 return ret;
272}
273
274/*
Chris Masonfec577f2007-02-26 10:40:21 -0500275 * find all the blocks marked as pending in the radix tree and remove
276 * them from the extent map
277 */
Chris Masone089f052007-03-16 16:20:31 -0400278static int del_pending_extents(struct btrfs_trans_handle *trans, struct
279 btrfs_root *extent_root)
Chris Masonfec577f2007-02-26 10:40:21 -0500280{
281 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -0400282 int wret;
283 int err = 0;
Chris Mason8ef97622007-03-26 10:15:30 -0400284 unsigned long gang[4];
Chris Masonfec577f2007-02-26 10:40:21 -0500285 int i;
Chris Mason8ef97622007-03-26 10:15:30 -0400286 struct radix_tree_root *pending_radix;
287 struct radix_tree_root *pinned_radix;
288
289 pending_radix = &extent_root->fs_info->pending_del_radix;
290 pinned_radix = &extent_root->fs_info->pinned_radix;
Chris Masonfec577f2007-02-26 10:40:21 -0500291
292 while(1) {
Chris Mason8ef97622007-03-26 10:15:30 -0400293 ret = find_first_radix_bit(pending_radix, gang,
294 ARRAY_SIZE(gang));
Chris Masonfec577f2007-02-26 10:40:21 -0500295 if (!ret)
296 break;
297 for (i = 0; i < ret; i++) {
Chris Mason8ef97622007-03-26 10:15:30 -0400298 wret = set_radix_bit(pinned_radix, gang[i]);
299 BUG_ON(wret);
300 wret = clear_radix_bit(pending_radix, gang[i]);
301 BUG_ON(wret);
Chris Masond5719762007-03-23 10:01:08 -0400302 wret = __free_extent(trans, extent_root,
Chris Mason8ef97622007-03-26 10:15:30 -0400303 gang[i], 1, 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400304 if (wret)
305 err = wret;
Chris Masonfec577f2007-02-26 10:40:21 -0500306 }
307 }
Chris Masone20d96d2007-03-22 12:13:20 -0400308 return err;
Chris Masonfec577f2007-02-26 10:40:21 -0500309}
310
311/*
312 * remove an extent from the root, returns 0 on success
313 */
Chris Masone089f052007-03-16 16:20:31 -0400314int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
315 *root, u64 blocknr, u64 num_blocks, int pin)
Chris Masonfec577f2007-02-26 10:40:21 -0500316{
Chris Mason9f5fae22007-03-20 14:38:32 -0400317 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Masonfec577f2007-02-26 10:40:21 -0500318 int pending_ret;
319 int ret;
Chris Masona28ec192007-03-06 20:08:01 -0500320
321 if (root == extent_root) {
Chris Mason8ef97622007-03-26 10:15:30 -0400322 pin_down_block(root, blocknr, 1);
Chris Masona28ec192007-03-06 20:08:01 -0500323 return 0;
324 }
Chris Mason78fae272007-03-25 11:35:08 -0400325 ret = __free_extent(trans, root, blocknr, num_blocks, pin);
Chris Masone20d96d2007-03-22 12:13:20 -0400326 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
Chris Masonfec577f2007-02-26 10:40:21 -0500327 return ret ? ret : pending_ret;
328}
329
330/*
331 * walks the btree of allocated extents and find a hole of a given size.
332 * The key ins is changed to record the hole:
333 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -0400334 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -0500335 * ins->offset == number of blocks
336 * Any available blocks before search_start are skipped.
337 */
Chris Masone089f052007-03-16 16:20:31 -0400338static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
339 *orig_root, u64 num_blocks, u64 search_start, u64
340 search_end, struct btrfs_key *ins)
Chris Masonfec577f2007-02-26 10:40:21 -0500341{
Chris Mason5caf2a02007-04-02 11:20:42 -0400342 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -0400343 struct btrfs_key key;
Chris Masonfec577f2007-02-26 10:40:21 -0500344 int ret;
345 u64 hole_size = 0;
346 int slot = 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400347 u64 last_block = 0;
Chris Mason037e6392007-03-07 11:50:24 -0500348 u64 test_block;
Chris Masonfec577f2007-02-26 10:40:21 -0500349 int start_found;
Chris Mason234b63a2007-03-13 10:46:10 -0400350 struct btrfs_leaf *l;
Chris Mason9f5fae22007-03-20 14:38:32 -0400351 struct btrfs_root * root = orig_root->fs_info->extent_root;
Chris Masonf2458e12007-04-25 15:52:25 -0400352 struct btrfs_fs_info *info = root->fs_info;
Chris Mason0579da42007-03-07 16:15:30 -0500353 int total_needed = num_blocks;
Chris Masonf2458e12007-04-25 15:52:25 -0400354 int total_found = 0;
355 int fill_prealloc = 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400356 int level;
Chris Masonfec577f2007-02-26 10:40:21 -0500357
Chris Masonb1a4d962007-04-04 15:27:52 -0400358 path = btrfs_alloc_path();
359 ins->flags = 0;
360 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
361
Chris Masone20d96d2007-03-22 12:13:20 -0400362 level = btrfs_header_level(btrfs_buffer_header(root->node));
Chris Masonf2458e12007-04-25 15:52:25 -0400363 if (num_blocks == 0) {
364 fill_prealloc = 1;
365 num_blocks = 1;
366 total_needed = min(level + 2, BTRFS_MAX_LEVEL) * 3;
367 }
368 if (info->last_insert.objectid == 0 && search_end == (u64)-1) {
Chris Masonb1a4d962007-04-04 15:27:52 -0400369 struct btrfs_disk_key *last_key;
370 btrfs_init_path(path);
371 ins->objectid = (u64)-1;
372 ins->offset = (u64)-1;
373 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
374 if (ret < 0)
375 goto error;
376 BUG_ON(ret == 0);
377 if (path->slots[0] > 0)
378 path->slots[0]--;
379 l = btrfs_buffer_leaf(path->nodes[0]);
380 last_key = &l->items[path->slots[0]].key;
381 search_start = btrfs_disk_key_objectid(last_key);
382 }
Chris Masonf2458e12007-04-25 15:52:25 -0400383 if (info->last_insert.objectid > search_start)
384 search_start = info->last_insert.objectid;
Chris Mason62e27492007-03-15 12:56:47 -0400385
Chris Masonfec577f2007-02-26 10:40:21 -0500386check_failed:
Chris Mason5caf2a02007-04-02 11:20:42 -0400387 btrfs_init_path(path);
Chris Masonfec577f2007-02-26 10:40:21 -0500388 ins->objectid = search_start;
389 ins->offset = 0;
Chris Masonfec577f2007-02-26 10:40:21 -0500390 start_found = 0;
Chris Mason5caf2a02007-04-02 11:20:42 -0400391 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
Chris Mason0f70abe2007-02-28 16:46:22 -0500392 if (ret < 0)
393 goto error;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500394
Chris Mason5caf2a02007-04-02 11:20:42 -0400395 if (path->slots[0] > 0)
396 path->slots[0]--;
Chris Mason0579da42007-03-07 16:15:30 -0500397
Chris Masonfec577f2007-02-26 10:40:21 -0500398 while (1) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400399 l = btrfs_buffer_leaf(path->nodes[0]);
400 slot = path->slots[0];
Chris Mason7518a232007-03-12 12:01:18 -0400401 if (slot >= btrfs_header_nritems(&l->header)) {
Chris Masonf2458e12007-04-25 15:52:25 -0400402 if (fill_prealloc) {
403 info->extent_tree_prealloc_nr = 0;
404 total_found = 0;
405 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400406 ret = btrfs_next_leaf(root, path);
Chris Masonfec577f2007-02-26 10:40:21 -0500407 if (ret == 0)
408 continue;
Chris Mason0f70abe2007-02-28 16:46:22 -0500409 if (ret < 0)
410 goto error;
Chris Masonfec577f2007-02-26 10:40:21 -0500411 if (!start_found) {
412 ins->objectid = search_start;
Chris Masonf2458e12007-04-25 15:52:25 -0400413 ins->offset = (u64)-1 - search_start;
Chris Masonfec577f2007-02-26 10:40:21 -0500414 start_found = 1;
415 goto check_pending;
416 }
417 ins->objectid = last_block > search_start ?
418 last_block : search_start;
Chris Masonf2458e12007-04-25 15:52:25 -0400419 ins->offset = (u64)-1 - ins->objectid;
Chris Masonfec577f2007-02-26 10:40:21 -0500420 goto check_pending;
421 }
Chris Masone2fa7222007-03-12 16:22:34 -0400422 btrfs_disk_key_to_cpu(&key, &l->items[slot].key);
423 if (key.objectid >= search_start) {
Chris Masonfec577f2007-02-26 10:40:21 -0500424 if (start_found) {
Chris Mason0579da42007-03-07 16:15:30 -0500425 if (last_block < search_start)
426 last_block = search_start;
Chris Masone2fa7222007-03-12 16:22:34 -0400427 hole_size = key.objectid - last_block;
Chris Masonf2458e12007-04-25 15:52:25 -0400428 if (hole_size > num_blocks) {
Chris Masonfec577f2007-02-26 10:40:21 -0500429 ins->objectid = last_block;
Chris Mason037e6392007-03-07 11:50:24 -0500430 ins->offset = hole_size;
Chris Masonfec577f2007-02-26 10:40:21 -0500431 goto check_pending;
432 }
Chris Mason0579da42007-03-07 16:15:30 -0500433 }
Chris Masonfec577f2007-02-26 10:40:21 -0500434 }
Chris Mason0579da42007-03-07 16:15:30 -0500435 start_found = 1;
Chris Masone2fa7222007-03-12 16:22:34 -0400436 last_block = key.objectid + key.offset;
Chris Mason5caf2a02007-04-02 11:20:42 -0400437 path->slots[0]++;
Chris Masonfec577f2007-02-26 10:40:21 -0500438 }
439 // FIXME -ENOSPC
440check_pending:
441 /* we have to make sure we didn't find an extent that has already
442 * been allocated by the map tree or the original allocation
443 */
Chris Mason5caf2a02007-04-02 11:20:42 -0400444 btrfs_release_path(root, path);
Chris Masonfec577f2007-02-26 10:40:21 -0500445 BUG_ON(ins->objectid < search_start);
Chris Mason037e6392007-03-07 11:50:24 -0500446 for (test_block = ins->objectid;
Chris Masonf2458e12007-04-25 15:52:25 -0400447 test_block < ins->objectid + num_blocks; test_block++) {
448 if (test_radix_bit(&info->pinned_radix, test_block)) {
Chris Mason037e6392007-03-07 11:50:24 -0500449 search_start = test_block + 1;
Chris Masonfec577f2007-02-26 10:40:21 -0500450 goto check_failed;
451 }
452 }
Chris Masonf2458e12007-04-25 15:52:25 -0400453 if (!fill_prealloc && info->extent_tree_insert_nr) {
454 u64 last =
455 info->extent_tree_insert[info->extent_tree_insert_nr - 1];
456 if (ins->objectid + num_blocks >
457 info->extent_tree_insert[0] &&
458 ins->objectid <= last) {
459 search_start = last + 1;
460 WARN_ON(1);
461 goto check_failed;
462 }
463 }
464 if (!fill_prealloc && info->extent_tree_prealloc_nr) {
465 u64 first =
466 info->extent_tree_prealloc[info->extent_tree_prealloc_nr - 1];
467 if (ins->objectid + num_blocks > first &&
468 ins->objectid <= info->extent_tree_prealloc[0]) {
469 search_start = info->extent_tree_prealloc[0] + 1;
470 WARN_ON(1);
471 goto check_failed;
472 }
473 }
474 if (fill_prealloc) {
475 int nr;
476 test_block = ins->objectid;
477 while(test_block < ins->objectid + ins->offset &&
478 total_found < total_needed) {
479 nr = total_needed - total_found - 1;
480 BUG_ON(nr < 0);
481 root->fs_info->extent_tree_prealloc[nr] =
482 test_block;
483 total_found++;
484 test_block++;
485 }
486 if (total_found < total_needed) {
487 search_start = test_block;
488 goto check_failed;
489 }
490 root->fs_info->extent_tree_prealloc_nr = total_found;
491 }
Chris Mason9f5fae22007-03-20 14:38:32 -0400492 root->fs_info->last_insert.objectid = ins->objectid;
Chris Mason037e6392007-03-07 11:50:24 -0500493 ins->offset = num_blocks;
Chris Mason5caf2a02007-04-02 11:20:42 -0400494 btrfs_free_path(path);
Chris Masonfec577f2007-02-26 10:40:21 -0500495 return 0;
Chris Mason0f70abe2007-02-28 16:46:22 -0500496error:
Chris Mason5caf2a02007-04-02 11:20:42 -0400497 btrfs_release_path(root, path);
498 btrfs_free_path(path);
Chris Mason0f70abe2007-02-28 16:46:22 -0500499 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -0500500}
501
502/*
Chris Masonfec577f2007-02-26 10:40:21 -0500503 * finds a free extent and does all the dirty work required for allocation
504 * returns the key for the extent through ins, and a tree buffer for
505 * the first block of the extent through buf.
506 *
507 * returns 0 if everything worked, non-zero otherwise.
508 */
Chris Mason4d775672007-04-20 20:23:12 -0400509int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
510 struct btrfs_root *root, u64 owner,
Chris Masonc62a1922007-04-24 12:07:39 -0400511 u64 num_blocks, u64 search_start,
Chris Mason4d775672007-04-20 20:23:12 -0400512 u64 search_end, struct btrfs_key *ins)
Chris Masonfec577f2007-02-26 10:40:21 -0500513{
514 int ret;
515 int pending_ret;
Chris Mason1261ec42007-03-20 20:35:03 -0400516 u64 super_blocks_used;
517 struct btrfs_fs_info *info = root->fs_info;
518 struct btrfs_root *extent_root = info->extent_root;
Chris Mason234b63a2007-03-13 10:46:10 -0400519 struct btrfs_extent_item extent_item;
Chris Masonf2458e12007-04-25 15:52:25 -0400520 struct btrfs_key prealloc_key;
Chris Mason037e6392007-03-07 11:50:24 -0500521
Chris Masoncf27e1e2007-03-13 09:49:06 -0400522 btrfs_set_extent_refs(&extent_item, 1);
Chris Mason4d775672007-04-20 20:23:12 -0400523 btrfs_set_extent_owner(&extent_item, owner);
Chris Masonfec577f2007-02-26 10:40:21 -0500524
Chris Mason037e6392007-03-07 11:50:24 -0500525 if (root == extent_root) {
Chris Masonf2458e12007-04-25 15:52:25 -0400526 int nr;
527 BUG_ON(info->extent_tree_prealloc_nr == 0);
Chris Mason037e6392007-03-07 11:50:24 -0500528 BUG_ON(num_blocks != 1);
Chris Mason037e6392007-03-07 11:50:24 -0500529 ins->offset = 1;
Chris Masonf2458e12007-04-25 15:52:25 -0400530 info->extent_tree_prealloc_nr--;
531 nr = info->extent_tree_prealloc_nr;
532 ins->objectid = info->extent_tree_prealloc[nr];
533 info->extent_tree_insert[info->extent_tree_insert_nr++] =
534 ins->objectid;
Chris Masonfec577f2007-02-26 10:40:21 -0500535 return 0;
536 }
Chris Masonf2458e12007-04-25 15:52:25 -0400537 /* do the real allocation */
Chris Masone089f052007-03-16 16:20:31 -0400538 ret = find_free_extent(trans, root, num_blocks, search_start,
Chris Mason037e6392007-03-07 11:50:24 -0500539 search_end, ins);
540 if (ret)
541 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -0500542
Chris Masonf2458e12007-04-25 15:52:25 -0400543 /* then do prealloc for the extent tree */
544 ret = find_free_extent(trans, root, 0, ins->objectid + ins->offset,
545 search_end, &prealloc_key);
546 if (ret)
547 return ret;
548
Chris Mason1261ec42007-03-20 20:35:03 -0400549 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
550 btrfs_set_super_blocks_used(info->disk_super, super_blocks_used +
551 num_blocks);
Chris Masone089f052007-03-16 16:20:31 -0400552 ret = btrfs_insert_item(trans, extent_root, ins, &extent_item,
553 sizeof(extent_item));
Chris Mason037e6392007-03-07 11:50:24 -0500554
Chris Masone089f052007-03-16 16:20:31 -0400555 finish_current_insert(trans, extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -0400556 pending_ret = del_pending_extents(trans, extent_root);
Chris Mason037e6392007-03-07 11:50:24 -0500557 if (ret)
558 return ret;
559 if (pending_ret)
560 return pending_ret;
561 return 0;
Chris Masonfec577f2007-02-26 10:40:21 -0500562}
563
564/*
565 * helper function to allocate a block for a given tree
566 * returns the tree buffer or NULL.
567 */
Chris Masone20d96d2007-03-22 12:13:20 -0400568struct buffer_head *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Chris Mason4d775672007-04-20 20:23:12 -0400569 struct btrfs_root *root)
Chris Masonfec577f2007-02-26 10:40:21 -0500570{
Chris Masone2fa7222007-03-12 16:22:34 -0400571 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -0500572 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -0400573 struct buffer_head *buf;
Chris Masonfec577f2007-02-26 10:40:21 -0500574
Chris Mason4d775672007-04-20 20:23:12 -0400575 ret = btrfs_alloc_extent(trans, root, root->root_key.objectid,
Chris Mason4d775672007-04-20 20:23:12 -0400576 1, 0, (unsigned long)-1, &ins);
Chris Masonfec577f2007-02-26 10:40:21 -0500577 if (ret) {
578 BUG();
579 return NULL;
580 }
Chris Masond98237b2007-03-28 13:57:48 -0400581 buf = btrfs_find_create_tree_block(root, ins.objectid);
Chris Masondf2ce342007-03-23 11:00:45 -0400582 set_buffer_uptodate(buf);
Chris Masonfec577f2007-02-26 10:40:21 -0500583 return buf;
584}
Chris Masona28ec192007-03-06 20:08:01 -0500585
Chris Mason6407bf62007-03-27 06:33:00 -0400586static int drop_leaf_ref(struct btrfs_trans_handle *trans,
587 struct btrfs_root *root, struct buffer_head *cur)
588{
589 struct btrfs_disk_key *key;
590 struct btrfs_leaf *leaf;
591 struct btrfs_file_extent_item *fi;
592 int i;
593 int nritems;
594 int ret;
595
596 BUG_ON(!btrfs_is_leaf(btrfs_buffer_node(cur)));
597 leaf = btrfs_buffer_leaf(cur);
598 nritems = btrfs_header_nritems(&leaf->header);
599 for (i = 0; i < nritems; i++) {
600 key = &leaf->items[i].key;
601 if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY)
602 continue;
603 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
Chris Mason236454df2007-04-19 13:37:44 -0400604 if (btrfs_file_extent_type(fi) == BTRFS_FILE_EXTENT_INLINE)
605 continue;
Chris Mason6407bf62007-03-27 06:33:00 -0400606 /*
607 * FIXME make sure to insert a trans record that
608 * repeats the snapshot del on crash
609 */
610 ret = btrfs_free_extent(trans, root,
611 btrfs_file_extent_disk_blocknr(fi),
612 btrfs_file_extent_disk_num_blocks(fi),
613 0);
614 BUG_ON(ret);
615 }
616 return 0;
617}
618
Chris Mason9aca1d52007-03-13 11:09:37 -0400619/*
620 * helper function for drop_snapshot, this walks down the tree dropping ref
621 * counts as it goes.
622 */
Chris Masone089f052007-03-16 16:20:31 -0400623static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
624 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -0500625{
Chris Masone20d96d2007-03-22 12:13:20 -0400626 struct buffer_head *next;
627 struct buffer_head *cur;
Chris Mason20524f02007-03-10 06:35:47 -0500628 u64 blocknr;
629 int ret;
630 u32 refs;
631
Chris Mason5caf2a02007-04-02 11:20:42 -0400632 WARN_ON(*level < 0);
633 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Masonb18c6682007-04-17 13:26:50 -0400634 ret = lookup_extent_ref(trans, root, bh_blocknr(path->nodes[*level]),
Chris Mason6407bf62007-03-27 06:33:00 -0400635 1, &refs);
Chris Mason20524f02007-03-10 06:35:47 -0500636 BUG_ON(ret);
637 if (refs > 1)
638 goto out;
Chris Mason9aca1d52007-03-13 11:09:37 -0400639 /*
640 * walk down to the last node level and free all the leaves
641 */
Chris Mason6407bf62007-03-27 06:33:00 -0400642 while(*level >= 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400643 WARN_ON(*level < 0);
644 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason20524f02007-03-10 06:35:47 -0500645 cur = path->nodes[*level];
Chris Mason2c90e5d2007-04-02 10:50:19 -0400646 if (btrfs_header_level(btrfs_buffer_header(cur)) != *level)
647 WARN_ON(1);
Chris Mason7518a232007-03-12 12:01:18 -0400648 if (path->slots[*level] >=
Chris Masone20d96d2007-03-22 12:13:20 -0400649 btrfs_header_nritems(btrfs_buffer_header(cur)))
Chris Mason20524f02007-03-10 06:35:47 -0500650 break;
Chris Mason6407bf62007-03-27 06:33:00 -0400651 if (*level == 0) {
652 ret = drop_leaf_ref(trans, root, cur);
653 BUG_ON(ret);
654 break;
655 }
Chris Masone20d96d2007-03-22 12:13:20 -0400656 blocknr = btrfs_node_blockptr(btrfs_buffer_node(cur),
657 path->slots[*level]);
Chris Masonb18c6682007-04-17 13:26:50 -0400658 ret = lookup_extent_ref(trans, root, blocknr, 1, &refs);
Chris Mason6407bf62007-03-27 06:33:00 -0400659 BUG_ON(ret);
660 if (refs != 1) {
Chris Mason20524f02007-03-10 06:35:47 -0500661 path->slots[*level]++;
Chris Masone089f052007-03-16 16:20:31 -0400662 ret = btrfs_free_extent(trans, root, blocknr, 1, 1);
Chris Mason20524f02007-03-10 06:35:47 -0500663 BUG_ON(ret);
664 continue;
665 }
Chris Mason20524f02007-03-10 06:35:47 -0500666 next = read_tree_block(root, blocknr);
Chris Mason5caf2a02007-04-02 11:20:42 -0400667 WARN_ON(*level <= 0);
Chris Mason83e15a22007-03-12 09:03:27 -0400668 if (path->nodes[*level-1])
Chris Mason234b63a2007-03-13 10:46:10 -0400669 btrfs_block_release(root, path->nodes[*level-1]);
Chris Mason20524f02007-03-10 06:35:47 -0500670 path->nodes[*level-1] = next;
Chris Masone20d96d2007-03-22 12:13:20 -0400671 *level = btrfs_header_level(btrfs_buffer_header(next));
Chris Mason20524f02007-03-10 06:35:47 -0500672 path->slots[*level] = 0;
673 }
674out:
Chris Mason5caf2a02007-04-02 11:20:42 -0400675 WARN_ON(*level < 0);
676 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason6407bf62007-03-27 06:33:00 -0400677 ret = btrfs_free_extent(trans, root,
Chris Mason7eccb902007-04-11 15:53:25 -0400678 bh_blocknr(path->nodes[*level]), 1, 1);
Chris Mason234b63a2007-03-13 10:46:10 -0400679 btrfs_block_release(root, path->nodes[*level]);
Chris Mason20524f02007-03-10 06:35:47 -0500680 path->nodes[*level] = NULL;
681 *level += 1;
682 BUG_ON(ret);
683 return 0;
684}
685
Chris Mason9aca1d52007-03-13 11:09:37 -0400686/*
687 * helper for dropping snapshots. This walks back up the tree in the path
688 * to find the first node higher up where we haven't yet gone through
689 * all the slots
690 */
Chris Masone089f052007-03-16 16:20:31 -0400691static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root
692 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -0500693{
694 int i;
695 int slot;
696 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400697 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Mason20524f02007-03-10 06:35:47 -0500698 slot = path->slots[i];
Chris Masone20d96d2007-03-22 12:13:20 -0400699 if (slot < btrfs_header_nritems(
700 btrfs_buffer_header(path->nodes[i])) - 1) {
Chris Mason20524f02007-03-10 06:35:47 -0500701 path->slots[i]++;
702 *level = i;
703 return 0;
704 } else {
Chris Masone089f052007-03-16 16:20:31 -0400705 ret = btrfs_free_extent(trans, root,
Chris Mason7eccb902007-04-11 15:53:25 -0400706 bh_blocknr(path->nodes[*level]),
Chris Masone089f052007-03-16 16:20:31 -0400707 1, 1);
Chris Mason6407bf62007-03-27 06:33:00 -0400708 BUG_ON(ret);
Chris Mason234b63a2007-03-13 10:46:10 -0400709 btrfs_block_release(root, path->nodes[*level]);
Chris Mason83e15a22007-03-12 09:03:27 -0400710 path->nodes[*level] = NULL;
Chris Mason20524f02007-03-10 06:35:47 -0500711 *level = i + 1;
Chris Mason20524f02007-03-10 06:35:47 -0500712 }
713 }
714 return 1;
715}
716
Chris Mason9aca1d52007-03-13 11:09:37 -0400717/*
718 * drop the reference count on the tree rooted at 'snap'. This traverses
719 * the tree freeing any blocks that have a ref count of zero after being
720 * decremented.
721 */
Chris Masone089f052007-03-16 16:20:31 -0400722int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masone20d96d2007-03-22 12:13:20 -0400723 *root, struct buffer_head *snap)
Chris Mason20524f02007-03-10 06:35:47 -0500724{
Chris Mason3768f362007-03-13 16:47:54 -0400725 int ret = 0;
Chris Mason9aca1d52007-03-13 11:09:37 -0400726 int wret;
Chris Mason20524f02007-03-10 06:35:47 -0500727 int level;
Chris Mason5caf2a02007-04-02 11:20:42 -0400728 struct btrfs_path *path;
Chris Mason20524f02007-03-10 06:35:47 -0500729 int i;
730 int orig_level;
731
Chris Mason5caf2a02007-04-02 11:20:42 -0400732 path = btrfs_alloc_path();
733 BUG_ON(!path);
734 btrfs_init_path(path);
Chris Mason20524f02007-03-10 06:35:47 -0500735
Chris Masone20d96d2007-03-22 12:13:20 -0400736 level = btrfs_header_level(btrfs_buffer_header(snap));
Chris Mason20524f02007-03-10 06:35:47 -0500737 orig_level = level;
Chris Mason5caf2a02007-04-02 11:20:42 -0400738 path->nodes[level] = snap;
739 path->slots[level] = 0;
Chris Mason20524f02007-03-10 06:35:47 -0500740 while(1) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400741 wret = walk_down_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -0400742 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -0500743 break;
Chris Mason9aca1d52007-03-13 11:09:37 -0400744 if (wret < 0)
745 ret = wret;
746
Chris Mason5caf2a02007-04-02 11:20:42 -0400747 wret = walk_up_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -0400748 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -0500749 break;
Chris Mason9aca1d52007-03-13 11:09:37 -0400750 if (wret < 0)
751 ret = wret;
Chris Mason20524f02007-03-10 06:35:47 -0500752 }
Chris Mason83e15a22007-03-12 09:03:27 -0400753 for (i = 0; i <= orig_level; i++) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400754 if (path->nodes[i]) {
755 btrfs_block_release(root, path->nodes[i]);
Chris Mason83e15a22007-03-12 09:03:27 -0400756 }
Chris Mason20524f02007-03-10 06:35:47 -0500757 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400758 btrfs_free_path(path);
Chris Mason9aca1d52007-03-13 11:09:37 -0400759 return ret;
Chris Mason20524f02007-03-10 06:35:47 -0500760}