blob: 4d4fc48c0a31037143958dc12b9ae7b078650bf2 [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 Masone089f052007-03-16 16:20:31 -040015static int inc_block_ref(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason6407bf62007-03-27 06:33:00 -040016 *root, u64 blocknr, u64 num_blocks)
Chris Mason02217ed2007-03-02 16:08:05 -050017{
Chris Mason234b63a2007-03-13 10:46:10 -040018 struct btrfs_path path;
Chris Mason02217ed2007-03-02 16:08:05 -050019 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -040020 struct btrfs_key key;
Chris Mason234b63a2007-03-13 10:46:10 -040021 struct btrfs_leaf *l;
22 struct btrfs_extent_item *item;
Chris Masone2fa7222007-03-12 16:22:34 -040023 struct btrfs_key ins;
Chris Masoncf27e1e2007-03-13 09:49:06 -040024 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -050025
Chris Mason9f5fae22007-03-20 14:38:32 -040026 find_free_extent(trans, root->fs_info->extent_root, 0, 0, (u64)-1,
27 &ins);
Chris Mason234b63a2007-03-13 10:46:10 -040028 btrfs_init_path(&path);
Chris Mason02217ed2007-03-02 16:08:05 -050029 key.objectid = blocknr;
30 key.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -040031 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Mason6407bf62007-03-27 06:33:00 -040032 key.offset = num_blocks;
Chris Mason9f5fae22007-03-20 14:38:32 -040033 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, &path,
34 0, 1);
Chris Masona28ec192007-03-06 20:08:01 -050035 if (ret != 0)
36 BUG();
Chris Mason02217ed2007-03-02 16:08:05 -050037 BUG_ON(ret != 0);
Chris Masone20d96d2007-03-22 12:13:20 -040038 l = btrfs_buffer_leaf(path.nodes[0]);
Chris Mason4beb1b82007-03-14 10:31:29 -040039 item = btrfs_item_ptr(l, path.slots[0], struct btrfs_extent_item);
Chris Masoncf27e1e2007-03-13 09:49:06 -040040 refs = btrfs_extent_refs(item);
41 btrfs_set_extent_refs(item, refs + 1);
Chris Masond5719762007-03-23 10:01:08 -040042 mark_buffer_dirty(path.nodes[0]);
Chris Masona28ec192007-03-06 20:08:01 -050043
Chris Mason9f5fae22007-03-20 14:38:32 -040044 btrfs_release_path(root->fs_info->extent_root, &path);
45 finish_current_insert(trans, root->fs_info->extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -040046 del_pending_extents(trans, root->fs_info->extent_root);
Chris Mason02217ed2007-03-02 16:08:05 -050047 return 0;
48}
49
Chris Masone089f052007-03-16 16:20:31 -040050static int lookup_block_ref(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason6407bf62007-03-27 06:33:00 -040051 *root, u64 blocknr, u64 num_blocks, u32 *refs)
Chris Masona28ec192007-03-06 20:08:01 -050052{
Chris Mason234b63a2007-03-13 10:46:10 -040053 struct btrfs_path path;
Chris Masona28ec192007-03-06 20:08:01 -050054 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -040055 struct btrfs_key key;
Chris Mason234b63a2007-03-13 10:46:10 -040056 struct btrfs_leaf *l;
57 struct btrfs_extent_item *item;
58 btrfs_init_path(&path);
Chris Masona28ec192007-03-06 20:08:01 -050059 key.objectid = blocknr;
Chris Mason6407bf62007-03-27 06:33:00 -040060 key.offset = num_blocks;
Chris Mason62e27492007-03-15 12:56:47 -040061 key.flags = 0;
62 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Mason9f5fae22007-03-20 14:38:32 -040063 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, &path,
64 0, 0);
Chris Masona28ec192007-03-06 20:08:01 -050065 if (ret != 0)
66 BUG();
Chris Masone20d96d2007-03-22 12:13:20 -040067 l = btrfs_buffer_leaf(path.nodes[0]);
Chris Mason4beb1b82007-03-14 10:31:29 -040068 item = btrfs_item_ptr(l, path.slots[0], struct btrfs_extent_item);
Chris Masoncf27e1e2007-03-13 09:49:06 -040069 *refs = btrfs_extent_refs(item);
Chris Mason9f5fae22007-03-20 14:38:32 -040070 btrfs_release_path(root->fs_info->extent_root, &path);
Chris Masona28ec192007-03-06 20:08:01 -050071 return 0;
72}
73
Chris Masone089f052007-03-16 16:20:31 -040074int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Masone20d96d2007-03-22 12:13:20 -040075 struct buffer_head *buf)
Chris Mason02217ed2007-03-02 16:08:05 -050076{
77 u64 blocknr;
Chris Masone20d96d2007-03-22 12:13:20 -040078 struct btrfs_node *buf_node;
Chris Mason6407bf62007-03-27 06:33:00 -040079 struct btrfs_leaf *buf_leaf;
80 struct btrfs_disk_key *key;
81 struct btrfs_file_extent_item *fi;
Chris Mason02217ed2007-03-02 16:08:05 -050082 int i;
Chris Mason6407bf62007-03-27 06:33:00 -040083 int leaf;
84 int ret;
Chris Masona28ec192007-03-06 20:08:01 -050085
Chris Mason3768f362007-03-13 16:47:54 -040086 if (!root->ref_cows)
Chris Masona28ec192007-03-06 20:08:01 -050087 return 0;
Chris Masone20d96d2007-03-22 12:13:20 -040088 buf_node = btrfs_buffer_node(buf);
Chris Mason6407bf62007-03-27 06:33:00 -040089 leaf = btrfs_is_leaf(buf_node);
90 buf_leaf = btrfs_buffer_leaf(buf);
Chris Masone20d96d2007-03-22 12:13:20 -040091 for (i = 0; i < btrfs_header_nritems(&buf_node->header); i++) {
Chris Mason6407bf62007-03-27 06:33:00 -040092 if (leaf) {
93 key = &buf_leaf->items[i].key;
94 if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY)
95 continue;
96 fi = btrfs_item_ptr(buf_leaf, i,
97 struct btrfs_file_extent_item);
98 ret = inc_block_ref(trans, root,
99 btrfs_file_extent_disk_blocknr(fi),
100 btrfs_file_extent_disk_num_blocks(fi));
101 BUG_ON(ret);
102 } else {
103 blocknr = btrfs_node_blockptr(buf_node, i);
104 ret = inc_block_ref(trans, root, blocknr, 1);
105 BUG_ON(ret);
106 }
Chris Mason02217ed2007-03-02 16:08:05 -0500107 }
108 return 0;
109}
110
Chris Masone089f052007-03-16 16:20:31 -0400111int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, struct
112 btrfs_root *root)
Chris Masona28ec192007-03-06 20:08:01 -0500113{
Chris Mason8ef97622007-03-26 10:15:30 -0400114 unsigned long gang[8];
Chris Mason88fd1462007-03-16 08:56:18 -0400115 u64 first = 0;
Chris Masona28ec192007-03-06 20:08:01 -0500116 int ret;
117 int i;
Chris Mason8ef97622007-03-26 10:15:30 -0400118 struct radix_tree_root *pinned_radix = &root->fs_info->pinned_radix;
Chris Masona28ec192007-03-06 20:08:01 -0500119
120 while(1) {
Chris Mason8ef97622007-03-26 10:15:30 -0400121 ret = find_first_radix_bit(pinned_radix, gang,
122 ARRAY_SIZE(gang));
Chris Masona28ec192007-03-06 20:08:01 -0500123 if (!ret)
124 break;
Chris Mason88fd1462007-03-16 08:56:18 -0400125 if (!first)
Chris Mason8ef97622007-03-26 10:15:30 -0400126 first = gang[0];
Chris Mason0579da42007-03-07 16:15:30 -0500127 for (i = 0; i < ret; i++) {
Chris Mason8ef97622007-03-26 10:15:30 -0400128 clear_radix_bit(pinned_radix, gang[i]);
Chris Mason0579da42007-03-07 16:15:30 -0500129 }
Chris Masona28ec192007-03-06 20:08:01 -0500130 }
Chris Masond5719762007-03-23 10:01:08 -0400131 if (root->fs_info->last_insert.objectid > first)
132 root->fs_info->last_insert.objectid = first;
Chris Mason9f5fae22007-03-20 14:38:32 -0400133 root->fs_info->last_insert.offset = 0;
Chris Masona28ec192007-03-06 20:08:01 -0500134 return 0;
135}
136
Chris Masone089f052007-03-16 16:20:31 -0400137static int finish_current_insert(struct btrfs_trans_handle *trans, struct
138 btrfs_root *extent_root)
Chris Mason037e6392007-03-07 11:50:24 -0500139{
Chris Masone2fa7222007-03-12 16:22:34 -0400140 struct btrfs_key ins;
Chris Mason234b63a2007-03-13 10:46:10 -0400141 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -0500142 int i;
143 int ret;
Chris Mason1261ec42007-03-20 20:35:03 -0400144 u64 super_blocks_used;
145 struct btrfs_fs_info *info = extent_root->fs_info;
Chris Mason037e6392007-03-07 11:50:24 -0500146
Chris Masoncf27e1e2007-03-13 09:49:06 -0400147 btrfs_set_extent_refs(&extent_item, 1);
148 btrfs_set_extent_owner(&extent_item,
Chris Masone20d96d2007-03-22 12:13:20 -0400149 btrfs_header_parentid(btrfs_buffer_header(extent_root->node)));
Chris Mason037e6392007-03-07 11:50:24 -0500150 ins.offset = 1;
151 ins.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -0400152 btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
Chris Mason037e6392007-03-07 11:50:24 -0500153
Chris Mason9f5fae22007-03-20 14:38:32 -0400154 for (i = 0; i < extent_root->fs_info->current_insert.flags; i++) {
155 ins.objectid = extent_root->fs_info->current_insert.objectid +
156 i;
Chris Mason1261ec42007-03-20 20:35:03 -0400157 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
158 btrfs_set_super_blocks_used(info->disk_super,
159 super_blocks_used + 1);
Chris Masone089f052007-03-16 16:20:31 -0400160 ret = btrfs_insert_item(trans, extent_root, &ins, &extent_item,
161 sizeof(extent_item));
Chris Mason037e6392007-03-07 11:50:24 -0500162 BUG_ON(ret);
163 }
Chris Mason9f5fae22007-03-20 14:38:32 -0400164 extent_root->fs_info->current_insert.offset = 0;
Chris Mason037e6392007-03-07 11:50:24 -0500165 return 0;
166}
167
Chris Mason8ef97622007-03-26 10:15:30 -0400168static int pin_down_block(struct btrfs_root *root, u64 blocknr, int pending)
Chris Masone20d96d2007-03-22 12:13:20 -0400169{
170 int err;
Chris Mason78fae272007-03-25 11:35:08 -0400171 struct btrfs_header *header;
Chris Mason8ef97622007-03-26 10:15:30 -0400172 struct buffer_head *bh;
Chris Mason78fae272007-03-25 11:35:08 -0400173
Chris Mason8ef97622007-03-26 10:15:30 -0400174 bh = sb_find_get_block(root->fs_info->sb, blocknr);
175 if (bh) {
176 header = btrfs_buffer_header(bh);
177 if (btrfs_header_generation(header) ==
178 root->fs_info->running_transaction->transid) {
179 brelse(bh);
180 return 0;
181 }
Chris Mason78fae272007-03-25 11:35:08 -0400182 brelse(bh);
Chris Mason8ef97622007-03-26 10:15:30 -0400183 }
184 if (pending)
185 err = set_radix_bit(&root->fs_info->pending_del_radix, blocknr);
186 else
187 err = set_radix_bit(&root->fs_info->pinned_radix, blocknr);
188 BUG_ON(err);
Chris Masone20d96d2007-03-22 12:13:20 -0400189 return 0;
190}
191
Chris Masona28ec192007-03-06 20:08:01 -0500192/*
193 * remove an extent from the root, returns 0 on success
194 */
Chris Masone089f052007-03-16 16:20:31 -0400195static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason78fae272007-03-25 11:35:08 -0400196 *root, u64 blocknr, u64 num_blocks, int pin)
Chris Masona28ec192007-03-06 20:08:01 -0500197{
Chris Mason234b63a2007-03-13 10:46:10 -0400198 struct btrfs_path path;
Chris Masone2fa7222007-03-12 16:22:34 -0400199 struct btrfs_key key;
Chris Mason1261ec42007-03-20 20:35:03 -0400200 struct btrfs_fs_info *info = root->fs_info;
201 struct btrfs_root *extent_root = info->extent_root;
Chris Masona28ec192007-03-06 20:08:01 -0500202 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400203 struct btrfs_extent_item *ei;
Chris Masone2fa7222007-03-12 16:22:34 -0400204 struct btrfs_key ins;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400205 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500206
Chris Masona28ec192007-03-06 20:08:01 -0500207 key.objectid = blocknr;
208 key.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -0400209 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masona28ec192007-03-06 20:08:01 -0500210 key.offset = num_blocks;
211
Chris Masone089f052007-03-16 16:20:31 -0400212 find_free_extent(trans, root, 0, 0, (u64)-1, &ins);
Chris Mason234b63a2007-03-13 10:46:10 -0400213 btrfs_init_path(&path);
Chris Masone089f052007-03-16 16:20:31 -0400214 ret = btrfs_search_slot(trans, extent_root, &key, &path, -1, 1);
Chris Masona28ec192007-03-06 20:08:01 -0500215 if (ret) {
Chris Mason2e635a22007-03-21 11:12:56 -0400216 printk("failed to find %Lu\n", key.objectid);
Chris Mason234b63a2007-03-13 10:46:10 -0400217 btrfs_print_tree(extent_root, extent_root->node);
Chris Mason2e635a22007-03-21 11:12:56 -0400218 printk("failed to find %Lu\n", key.objectid);
Chris Masona28ec192007-03-06 20:08:01 -0500219 BUG();
220 }
Chris Masone20d96d2007-03-22 12:13:20 -0400221 ei = btrfs_item_ptr(btrfs_buffer_leaf(path.nodes[0]), path.slots[0],
Chris Mason123abc82007-03-14 14:14:43 -0400222 struct btrfs_extent_item);
Chris Masona28ec192007-03-06 20:08:01 -0500223 BUG_ON(ei->refs == 0);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400224 refs = btrfs_extent_refs(ei) - 1;
225 btrfs_set_extent_refs(ei, refs);
226 if (refs == 0) {
Chris Mason1261ec42007-03-20 20:35:03 -0400227 u64 super_blocks_used;
Chris Mason78fae272007-03-25 11:35:08 -0400228
229 if (pin) {
Chris Mason8ef97622007-03-26 10:15:30 -0400230 ret = pin_down_block(root, blocknr, 0);
Chris Mason78fae272007-03-25 11:35:08 -0400231 BUG_ON(ret);
232 }
233
Chris Mason1261ec42007-03-20 20:35:03 -0400234 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
235 btrfs_set_super_blocks_used(info->disk_super,
236 super_blocks_used - num_blocks);
Chris Masone089f052007-03-16 16:20:31 -0400237 ret = btrfs_del_item(trans, extent_root, &path);
Chris Mason78fae272007-03-25 11:35:08 -0400238 if (extent_root->fs_info->last_insert.objectid > blocknr)
Chris Mason9f5fae22007-03-20 14:38:32 -0400239 extent_root->fs_info->last_insert.objectid = blocknr;
Chris Masona28ec192007-03-06 20:08:01 -0500240 if (ret)
241 BUG();
242 }
Chris Masond5719762007-03-23 10:01:08 -0400243 mark_buffer_dirty(path.nodes[0]);
Chris Mason234b63a2007-03-13 10:46:10 -0400244 btrfs_release_path(extent_root, &path);
Chris Masone089f052007-03-16 16:20:31 -0400245 finish_current_insert(trans, extent_root);
Chris Masona28ec192007-03-06 20:08:01 -0500246 return ret;
247}
248
249/*
Chris Masonfec577f2007-02-26 10:40:21 -0500250 * find all the blocks marked as pending in the radix tree and remove
251 * them from the extent map
252 */
Chris Masone089f052007-03-16 16:20:31 -0400253static int del_pending_extents(struct btrfs_trans_handle *trans, struct
254 btrfs_root *extent_root)
Chris Masonfec577f2007-02-26 10:40:21 -0500255{
256 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -0400257 int wret;
258 int err = 0;
Chris Mason8ef97622007-03-26 10:15:30 -0400259 unsigned long gang[4];
Chris Masonfec577f2007-02-26 10:40:21 -0500260 int i;
Chris Mason8ef97622007-03-26 10:15:30 -0400261 struct radix_tree_root *pending_radix;
262 struct radix_tree_root *pinned_radix;
263
264 pending_radix = &extent_root->fs_info->pending_del_radix;
265 pinned_radix = &extent_root->fs_info->pinned_radix;
Chris Masonfec577f2007-02-26 10:40:21 -0500266
267 while(1) {
Chris Mason8ef97622007-03-26 10:15:30 -0400268 ret = find_first_radix_bit(pending_radix, gang,
269 ARRAY_SIZE(gang));
Chris Masonfec577f2007-02-26 10:40:21 -0500270 if (!ret)
271 break;
272 for (i = 0; i < ret; i++) {
Chris Mason8ef97622007-03-26 10:15:30 -0400273 wret = set_radix_bit(pinned_radix, gang[i]);
274 BUG_ON(wret);
275 wret = clear_radix_bit(pending_radix, gang[i]);
276 BUG_ON(wret);
Chris Masond5719762007-03-23 10:01:08 -0400277 wret = __free_extent(trans, extent_root,
Chris Mason8ef97622007-03-26 10:15:30 -0400278 gang[i], 1, 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400279 if (wret)
280 err = wret;
Chris Masonfec577f2007-02-26 10:40:21 -0500281 }
282 }
Chris Masone20d96d2007-03-22 12:13:20 -0400283 return err;
Chris Masonfec577f2007-02-26 10:40:21 -0500284}
285
286/*
287 * remove an extent from the root, returns 0 on success
288 */
Chris Masone089f052007-03-16 16:20:31 -0400289int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
290 *root, u64 blocknr, u64 num_blocks, int pin)
Chris Masonfec577f2007-02-26 10:40:21 -0500291{
Chris Mason9f5fae22007-03-20 14:38:32 -0400292 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Masone20d96d2007-03-22 12:13:20 -0400293 struct buffer_head *t;
Chris Masonfec577f2007-02-26 10:40:21 -0500294 int pending_ret;
295 int ret;
Chris Masona28ec192007-03-06 20:08:01 -0500296
297 if (root == extent_root) {
298 t = find_tree_block(root, blocknr);
Chris Mason8ef97622007-03-26 10:15:30 -0400299 pin_down_block(root, blocknr, 1);
Chris Masona28ec192007-03-06 20:08:01 -0500300 return 0;
301 }
Chris Mason78fae272007-03-25 11:35:08 -0400302 ret = __free_extent(trans, root, blocknr, num_blocks, pin);
Chris Masone20d96d2007-03-22 12:13:20 -0400303 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
Chris Masonfec577f2007-02-26 10:40:21 -0500304 return ret ? ret : pending_ret;
305}
306
307/*
308 * walks the btree of allocated extents and find a hole of a given size.
309 * The key ins is changed to record the hole:
310 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -0400311 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -0500312 * ins->offset == number of blocks
313 * Any available blocks before search_start are skipped.
314 */
Chris Masone089f052007-03-16 16:20:31 -0400315static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
316 *orig_root, u64 num_blocks, u64 search_start, u64
317 search_end, struct btrfs_key *ins)
Chris Masonfec577f2007-02-26 10:40:21 -0500318{
Chris Mason234b63a2007-03-13 10:46:10 -0400319 struct btrfs_path path;
Chris Masone2fa7222007-03-12 16:22:34 -0400320 struct btrfs_key key;
Chris Masonfec577f2007-02-26 10:40:21 -0500321 int ret;
322 u64 hole_size = 0;
323 int slot = 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400324 u64 last_block = 0;
Chris Mason037e6392007-03-07 11:50:24 -0500325 u64 test_block;
Chris Masonfec577f2007-02-26 10:40:21 -0500326 int start_found;
Chris Mason234b63a2007-03-13 10:46:10 -0400327 struct btrfs_leaf *l;
Chris Mason9f5fae22007-03-20 14:38:32 -0400328 struct btrfs_root * root = orig_root->fs_info->extent_root;
Chris Mason0579da42007-03-07 16:15:30 -0500329 int total_needed = num_blocks;
Chris Masone20d96d2007-03-22 12:13:20 -0400330 int level;
Chris Masonfec577f2007-02-26 10:40:21 -0500331
Chris Masone20d96d2007-03-22 12:13:20 -0400332 level = btrfs_header_level(btrfs_buffer_header(root->node));
333 total_needed += (level + 1) * 3;
Chris Mason9f5fae22007-03-20 14:38:32 -0400334 if (root->fs_info->last_insert.objectid > search_start)
335 search_start = root->fs_info->last_insert.objectid;
Chris Mason62e27492007-03-15 12:56:47 -0400336
337 ins->flags = 0;
338 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
339
Chris Masonfec577f2007-02-26 10:40:21 -0500340check_failed:
Chris Mason234b63a2007-03-13 10:46:10 -0400341 btrfs_init_path(&path);
Chris Masonfec577f2007-02-26 10:40:21 -0500342 ins->objectid = search_start;
343 ins->offset = 0;
Chris Masonfec577f2007-02-26 10:40:21 -0500344 start_found = 0;
Chris Masone089f052007-03-16 16:20:31 -0400345 ret = btrfs_search_slot(trans, root, ins, &path, 0, 0);
Chris Mason0f70abe2007-02-28 16:46:22 -0500346 if (ret < 0)
347 goto error;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500348
Chris Mason0579da42007-03-07 16:15:30 -0500349 if (path.slots[0] > 0)
350 path.slots[0]--;
351
Chris Masonfec577f2007-02-26 10:40:21 -0500352 while (1) {
Chris Masone20d96d2007-03-22 12:13:20 -0400353 l = btrfs_buffer_leaf(path.nodes[0]);
Chris Masonfec577f2007-02-26 10:40:21 -0500354 slot = path.slots[0];
Chris Mason7518a232007-03-12 12:01:18 -0400355 if (slot >= btrfs_header_nritems(&l->header)) {
Chris Mason234b63a2007-03-13 10:46:10 -0400356 ret = btrfs_next_leaf(root, &path);
Chris Masonfec577f2007-02-26 10:40:21 -0500357 if (ret == 0)
358 continue;
Chris Mason0f70abe2007-02-28 16:46:22 -0500359 if (ret < 0)
360 goto error;
Chris Masonfec577f2007-02-26 10:40:21 -0500361 if (!start_found) {
362 ins->objectid = search_start;
Chris Mason037e6392007-03-07 11:50:24 -0500363 ins->offset = (u64)-1;
Chris Masonfec577f2007-02-26 10:40:21 -0500364 start_found = 1;
365 goto check_pending;
366 }
367 ins->objectid = last_block > search_start ?
368 last_block : search_start;
Chris Mason037e6392007-03-07 11:50:24 -0500369 ins->offset = (u64)-1;
Chris Masonfec577f2007-02-26 10:40:21 -0500370 goto check_pending;
371 }
Chris Masone2fa7222007-03-12 16:22:34 -0400372 btrfs_disk_key_to_cpu(&key, &l->items[slot].key);
373 if (key.objectid >= search_start) {
Chris Masonfec577f2007-02-26 10:40:21 -0500374 if (start_found) {
Chris Mason0579da42007-03-07 16:15:30 -0500375 if (last_block < search_start)
376 last_block = search_start;
Chris Masone2fa7222007-03-12 16:22:34 -0400377 hole_size = key.objectid - last_block;
Chris Mason037e6392007-03-07 11:50:24 -0500378 if (hole_size > total_needed) {
Chris Masonfec577f2007-02-26 10:40:21 -0500379 ins->objectid = last_block;
Chris Mason037e6392007-03-07 11:50:24 -0500380 ins->offset = hole_size;
Chris Masonfec577f2007-02-26 10:40:21 -0500381 goto check_pending;
382 }
Chris Mason0579da42007-03-07 16:15:30 -0500383 }
Chris Masonfec577f2007-02-26 10:40:21 -0500384 }
Chris Mason0579da42007-03-07 16:15:30 -0500385 start_found = 1;
Chris Masone2fa7222007-03-12 16:22:34 -0400386 last_block = key.objectid + key.offset;
Chris Masonfec577f2007-02-26 10:40:21 -0500387 path.slots[0]++;
388 }
389 // FIXME -ENOSPC
390check_pending:
391 /* we have to make sure we didn't find an extent that has already
392 * been allocated by the map tree or the original allocation
393 */
Chris Mason234b63a2007-03-13 10:46:10 -0400394 btrfs_release_path(root, &path);
Chris Masonfec577f2007-02-26 10:40:21 -0500395 BUG_ON(ins->objectid < search_start);
Chris Mason037e6392007-03-07 11:50:24 -0500396 for (test_block = ins->objectid;
397 test_block < ins->objectid + total_needed; test_block++) {
Chris Mason8ef97622007-03-26 10:15:30 -0400398 if (test_radix_bit(&root->fs_info->pinned_radix,
Chris Mason9f5fae22007-03-20 14:38:32 -0400399 test_block)) {
Chris Mason037e6392007-03-07 11:50:24 -0500400 search_start = test_block + 1;
Chris Masonfec577f2007-02-26 10:40:21 -0500401 goto check_failed;
402 }
403 }
Chris Mason9f5fae22007-03-20 14:38:32 -0400404 BUG_ON(root->fs_info->current_insert.offset);
405 root->fs_info->current_insert.offset = total_needed - num_blocks;
406 root->fs_info->current_insert.objectid = ins->objectid + num_blocks;
407 root->fs_info->current_insert.flags = 0;
408 root->fs_info->last_insert.objectid = ins->objectid;
Chris Mason037e6392007-03-07 11:50:24 -0500409 ins->offset = num_blocks;
Chris Masonfec577f2007-02-26 10:40:21 -0500410 return 0;
Chris Mason0f70abe2007-02-28 16:46:22 -0500411error:
Chris Mason234b63a2007-03-13 10:46:10 -0400412 btrfs_release_path(root, &path);
Chris Mason0f70abe2007-02-28 16:46:22 -0500413 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -0500414}
415
416/*
Chris Masonfec577f2007-02-26 10:40:21 -0500417 * finds a free extent and does all the dirty work required for allocation
418 * returns the key for the extent through ins, and a tree buffer for
419 * the first block of the extent through buf.
420 *
421 * returns 0 if everything worked, non-zero otherwise.
422 */
Chris Masondee26a92007-03-26 16:00:06 -0400423int btrfs_alloc_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masone089f052007-03-16 16:20:31 -0400424 *root, u64 num_blocks, u64 search_start, u64
425 search_end, u64 owner, struct btrfs_key *ins)
Chris Masonfec577f2007-02-26 10:40:21 -0500426{
427 int ret;
428 int pending_ret;
Chris Mason1261ec42007-03-20 20:35:03 -0400429 u64 super_blocks_used;
430 struct btrfs_fs_info *info = root->fs_info;
431 struct btrfs_root *extent_root = info->extent_root;
Chris Mason234b63a2007-03-13 10:46:10 -0400432 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -0500433
Chris Masoncf27e1e2007-03-13 09:49:06 -0400434 btrfs_set_extent_refs(&extent_item, 1);
435 btrfs_set_extent_owner(&extent_item, owner);
Chris Masonfec577f2007-02-26 10:40:21 -0500436
Chris Mason037e6392007-03-07 11:50:24 -0500437 if (root == extent_root) {
Chris Mason9f5fae22007-03-20 14:38:32 -0400438 BUG_ON(extent_root->fs_info->current_insert.offset == 0);
Chris Mason037e6392007-03-07 11:50:24 -0500439 BUG_ON(num_blocks != 1);
Chris Mason9f5fae22007-03-20 14:38:32 -0400440 BUG_ON(extent_root->fs_info->current_insert.flags ==
441 extent_root->fs_info->current_insert.offset);
Chris Mason037e6392007-03-07 11:50:24 -0500442 ins->offset = 1;
Chris Mason9f5fae22007-03-20 14:38:32 -0400443 ins->objectid = extent_root->fs_info->current_insert.objectid +
444 extent_root->fs_info->current_insert.flags++;
Chris Masonfec577f2007-02-26 10:40:21 -0500445 return 0;
446 }
Chris Masone089f052007-03-16 16:20:31 -0400447 ret = find_free_extent(trans, root, num_blocks, search_start,
Chris Mason037e6392007-03-07 11:50:24 -0500448 search_end, ins);
449 if (ret)
450 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -0500451
Chris Mason1261ec42007-03-20 20:35:03 -0400452 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
453 btrfs_set_super_blocks_used(info->disk_super, super_blocks_used +
454 num_blocks);
Chris Masone089f052007-03-16 16:20:31 -0400455 ret = btrfs_insert_item(trans, extent_root, ins, &extent_item,
456 sizeof(extent_item));
Chris Mason037e6392007-03-07 11:50:24 -0500457
Chris Masone089f052007-03-16 16:20:31 -0400458 finish_current_insert(trans, extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -0400459 pending_ret = del_pending_extents(trans, extent_root);
Chris Mason037e6392007-03-07 11:50:24 -0500460 if (ret)
461 return ret;
462 if (pending_ret)
463 return pending_ret;
464 return 0;
Chris Masonfec577f2007-02-26 10:40:21 -0500465}
466
467/*
468 * helper function to allocate a block for a given tree
469 * returns the tree buffer or NULL.
470 */
Chris Masone20d96d2007-03-22 12:13:20 -0400471struct buffer_head *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Chris Masone089f052007-03-16 16:20:31 -0400472 struct btrfs_root *root)
Chris Masonfec577f2007-02-26 10:40:21 -0500473{
Chris Masone2fa7222007-03-12 16:22:34 -0400474 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -0500475 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -0400476 struct buffer_head *buf;
Chris Masonfec577f2007-02-26 10:40:21 -0500477
Chris Masondee26a92007-03-26 16:00:06 -0400478 ret = btrfs_alloc_extent(trans, root, 1, 0, (unsigned long)-1,
Chris Masone20d96d2007-03-22 12:13:20 -0400479 btrfs_header_parentid(btrfs_buffer_header(root->node)), &ins);
Chris Masonfec577f2007-02-26 10:40:21 -0500480 if (ret) {
481 BUG();
482 return NULL;
483 }
Chris Mason037e6392007-03-07 11:50:24 -0500484 buf = find_tree_block(root, ins.objectid);
Chris Masondf2ce342007-03-23 11:00:45 -0400485 set_buffer_uptodate(buf);
Chris Masonfec577f2007-02-26 10:40:21 -0500486 return buf;
487}
Chris Masona28ec192007-03-06 20:08:01 -0500488
Chris Mason6407bf62007-03-27 06:33:00 -0400489static int drop_leaf_ref(struct btrfs_trans_handle *trans,
490 struct btrfs_root *root, struct buffer_head *cur)
491{
492 struct btrfs_disk_key *key;
493 struct btrfs_leaf *leaf;
494 struct btrfs_file_extent_item *fi;
495 int i;
496 int nritems;
497 int ret;
498
499 BUG_ON(!btrfs_is_leaf(btrfs_buffer_node(cur)));
500 leaf = btrfs_buffer_leaf(cur);
501 nritems = btrfs_header_nritems(&leaf->header);
502 for (i = 0; i < nritems; i++) {
503 key = &leaf->items[i].key;
504 if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY)
505 continue;
506 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
507 /*
508 * FIXME make sure to insert a trans record that
509 * repeats the snapshot del on crash
510 */
511 ret = btrfs_free_extent(trans, root,
512 btrfs_file_extent_disk_blocknr(fi),
513 btrfs_file_extent_disk_num_blocks(fi),
514 0);
515 BUG_ON(ret);
516 }
517 return 0;
518}
519
Chris Mason9aca1d52007-03-13 11:09:37 -0400520/*
521 * helper function for drop_snapshot, this walks down the tree dropping ref
522 * counts as it goes.
523 */
Chris Masone089f052007-03-16 16:20:31 -0400524static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
525 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -0500526{
Chris Masone20d96d2007-03-22 12:13:20 -0400527 struct buffer_head *next;
528 struct buffer_head *cur;
Chris Mason20524f02007-03-10 06:35:47 -0500529 u64 blocknr;
530 int ret;
531 u32 refs;
532
Chris Masone20d96d2007-03-22 12:13:20 -0400533 ret = lookup_block_ref(trans, root, path->nodes[*level]->b_blocknr,
Chris Mason6407bf62007-03-27 06:33:00 -0400534 1, &refs);
Chris Mason20524f02007-03-10 06:35:47 -0500535 BUG_ON(ret);
536 if (refs > 1)
537 goto out;
Chris Mason9aca1d52007-03-13 11:09:37 -0400538 /*
539 * walk down to the last node level and free all the leaves
540 */
Chris Mason6407bf62007-03-27 06:33:00 -0400541 while(*level >= 0) {
Chris Mason20524f02007-03-10 06:35:47 -0500542 cur = path->nodes[*level];
Chris Mason7518a232007-03-12 12:01:18 -0400543 if (path->slots[*level] >=
Chris Masone20d96d2007-03-22 12:13:20 -0400544 btrfs_header_nritems(btrfs_buffer_header(cur)))
Chris Mason20524f02007-03-10 06:35:47 -0500545 break;
Chris Mason6407bf62007-03-27 06:33:00 -0400546 if (*level == 0) {
547 ret = drop_leaf_ref(trans, root, cur);
548 BUG_ON(ret);
549 break;
550 }
Chris Masone20d96d2007-03-22 12:13:20 -0400551 blocknr = btrfs_node_blockptr(btrfs_buffer_node(cur),
552 path->slots[*level]);
Chris Mason6407bf62007-03-27 06:33:00 -0400553 ret = lookup_block_ref(trans, root, blocknr, 1, &refs);
554 BUG_ON(ret);
555 if (refs != 1) {
Chris Mason20524f02007-03-10 06:35:47 -0500556 path->slots[*level]++;
Chris Masone089f052007-03-16 16:20:31 -0400557 ret = btrfs_free_extent(trans, root, blocknr, 1, 1);
Chris Mason20524f02007-03-10 06:35:47 -0500558 BUG_ON(ret);
559 continue;
560 }
Chris Mason20524f02007-03-10 06:35:47 -0500561 next = read_tree_block(root, blocknr);
Chris Mason83e15a22007-03-12 09:03:27 -0400562 if (path->nodes[*level-1])
Chris Mason234b63a2007-03-13 10:46:10 -0400563 btrfs_block_release(root, path->nodes[*level-1]);
Chris Mason20524f02007-03-10 06:35:47 -0500564 path->nodes[*level-1] = next;
Chris Masone20d96d2007-03-22 12:13:20 -0400565 *level = btrfs_header_level(btrfs_buffer_header(next));
Chris Mason20524f02007-03-10 06:35:47 -0500566 path->slots[*level] = 0;
567 }
568out:
Chris Mason6407bf62007-03-27 06:33:00 -0400569 ret = btrfs_free_extent(trans, root,
570 path->nodes[*level]->b_blocknr, 1, 1);
Chris Mason234b63a2007-03-13 10:46:10 -0400571 btrfs_block_release(root, path->nodes[*level]);
Chris Mason20524f02007-03-10 06:35:47 -0500572 path->nodes[*level] = NULL;
573 *level += 1;
574 BUG_ON(ret);
575 return 0;
576}
577
Chris Mason9aca1d52007-03-13 11:09:37 -0400578/*
579 * helper for dropping snapshots. This walks back up the tree in the path
580 * to find the first node higher up where we haven't yet gone through
581 * all the slots
582 */
Chris Masone089f052007-03-16 16:20:31 -0400583static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root
584 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -0500585{
586 int i;
587 int slot;
588 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400589 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Mason20524f02007-03-10 06:35:47 -0500590 slot = path->slots[i];
Chris Masone20d96d2007-03-22 12:13:20 -0400591 if (slot < btrfs_header_nritems(
592 btrfs_buffer_header(path->nodes[i])) - 1) {
Chris Mason20524f02007-03-10 06:35:47 -0500593 path->slots[i]++;
594 *level = i;
595 return 0;
596 } else {
Chris Masone089f052007-03-16 16:20:31 -0400597 ret = btrfs_free_extent(trans, root,
Chris Masone20d96d2007-03-22 12:13:20 -0400598 path->nodes[*level]->b_blocknr,
Chris Masone089f052007-03-16 16:20:31 -0400599 1, 1);
Chris Mason6407bf62007-03-27 06:33:00 -0400600 BUG_ON(ret);
Chris Mason234b63a2007-03-13 10:46:10 -0400601 btrfs_block_release(root, path->nodes[*level]);
Chris Mason83e15a22007-03-12 09:03:27 -0400602 path->nodes[*level] = NULL;
Chris Mason20524f02007-03-10 06:35:47 -0500603 *level = i + 1;
Chris Mason20524f02007-03-10 06:35:47 -0500604 }
605 }
606 return 1;
607}
608
Chris Mason9aca1d52007-03-13 11:09:37 -0400609/*
610 * drop the reference count on the tree rooted at 'snap'. This traverses
611 * the tree freeing any blocks that have a ref count of zero after being
612 * decremented.
613 */
Chris Masone089f052007-03-16 16:20:31 -0400614int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masone20d96d2007-03-22 12:13:20 -0400615 *root, struct buffer_head *snap)
Chris Mason20524f02007-03-10 06:35:47 -0500616{
Chris Mason3768f362007-03-13 16:47:54 -0400617 int ret = 0;
Chris Mason9aca1d52007-03-13 11:09:37 -0400618 int wret;
Chris Mason20524f02007-03-10 06:35:47 -0500619 int level;
Chris Mason234b63a2007-03-13 10:46:10 -0400620 struct btrfs_path path;
Chris Mason20524f02007-03-10 06:35:47 -0500621 int i;
622 int orig_level;
623
Chris Mason234b63a2007-03-13 10:46:10 -0400624 btrfs_init_path(&path);
Chris Mason20524f02007-03-10 06:35:47 -0500625
Chris Masone20d96d2007-03-22 12:13:20 -0400626 level = btrfs_header_level(btrfs_buffer_header(snap));
Chris Mason20524f02007-03-10 06:35:47 -0500627 orig_level = level;
628 path.nodes[level] = snap;
629 path.slots[level] = 0;
630 while(1) {
Chris Masone089f052007-03-16 16:20:31 -0400631 wret = walk_down_tree(trans, root, &path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -0400632 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -0500633 break;
Chris Mason9aca1d52007-03-13 11:09:37 -0400634 if (wret < 0)
635 ret = wret;
636
Chris Masone089f052007-03-16 16:20:31 -0400637 wret = walk_up_tree(trans, root, &path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -0400638 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -0500639 break;
Chris Mason9aca1d52007-03-13 11:09:37 -0400640 if (wret < 0)
641 ret = wret;
Chris Mason20524f02007-03-10 06:35:47 -0500642 }
Chris Mason83e15a22007-03-12 09:03:27 -0400643 for (i = 0; i <= orig_level; i++) {
644 if (path.nodes[i]) {
Chris Mason234b63a2007-03-13 10:46:10 -0400645 btrfs_block_release(root, path.nodes[i]);
Chris Mason83e15a22007-03-12 09:03:27 -0400646 }
Chris Mason20524f02007-03-10 06:35:47 -0500647 }
Chris Mason9aca1d52007-03-13 11:09:37 -0400648 return ret;
Chris Mason20524f02007-03-10 06:35:47 -0500649}