blob: 37b87e28a2f317d8f972b73cb602167314fc12c7 [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 Masond6025572007-03-30 14:27:56 -040042 btrfs_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 Masonf4b9aa82007-03-27 11:05:53 -0400174 if (!pending) {
Chris Masond98237b2007-03-28 13:57:48 -0400175 bh = btrfs_find_tree_block(root, blocknr);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400176 if (bh) {
177 if (buffer_uptodate(bh)) {
178 u64 transid =
179 root->fs_info->running_transaction->transid;
180 header = btrfs_buffer_header(bh);
181 if (btrfs_header_generation(header) ==
182 transid) {
183 btrfs_block_release(root, bh);
184 return 0;
185 }
Chris Masonf4b9aa82007-03-27 11:05:53 -0400186 }
Chris Masond6025572007-03-30 14:27:56 -0400187 btrfs_block_release(root, bh);
Chris Mason8ef97622007-03-26 10:15:30 -0400188 }
Chris Mason8ef97622007-03-26 10:15:30 -0400189 err = set_radix_bit(&root->fs_info->pinned_radix, blocknr);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400190 } else {
191 err = set_radix_bit(&root->fs_info->pending_del_radix, blocknr);
192 }
Chris Mason8ef97622007-03-26 10:15:30 -0400193 BUG_ON(err);
Chris Masone20d96d2007-03-22 12:13:20 -0400194 return 0;
195}
196
Chris Masona28ec192007-03-06 20:08:01 -0500197/*
198 * remove an extent from the root, returns 0 on success
199 */
Chris Masone089f052007-03-16 16:20:31 -0400200static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason78fae272007-03-25 11:35:08 -0400201 *root, u64 blocknr, u64 num_blocks, int pin)
Chris Masona28ec192007-03-06 20:08:01 -0500202{
Chris Mason234b63a2007-03-13 10:46:10 -0400203 struct btrfs_path path;
Chris Masone2fa7222007-03-12 16:22:34 -0400204 struct btrfs_key key;
Chris Mason1261ec42007-03-20 20:35:03 -0400205 struct btrfs_fs_info *info = root->fs_info;
206 struct btrfs_root *extent_root = info->extent_root;
Chris Masona28ec192007-03-06 20:08:01 -0500207 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400208 struct btrfs_extent_item *ei;
Chris Masone2fa7222007-03-12 16:22:34 -0400209 struct btrfs_key ins;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400210 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500211
Chris Masona28ec192007-03-06 20:08:01 -0500212 key.objectid = blocknr;
213 key.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -0400214 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masona28ec192007-03-06 20:08:01 -0500215 key.offset = num_blocks;
216
Chris Masone089f052007-03-16 16:20:31 -0400217 find_free_extent(trans, root, 0, 0, (u64)-1, &ins);
Chris Mason234b63a2007-03-13 10:46:10 -0400218 btrfs_init_path(&path);
Chris Masone089f052007-03-16 16:20:31 -0400219 ret = btrfs_search_slot(trans, extent_root, &key, &path, -1, 1);
Chris Masona28ec192007-03-06 20:08:01 -0500220 if (ret) {
Chris Mason2e635a22007-03-21 11:12:56 -0400221 printk("failed to find %Lu\n", key.objectid);
Chris Mason234b63a2007-03-13 10:46:10 -0400222 btrfs_print_tree(extent_root, extent_root->node);
Chris Mason2e635a22007-03-21 11:12:56 -0400223 printk("failed to find %Lu\n", key.objectid);
Chris Masona28ec192007-03-06 20:08:01 -0500224 BUG();
225 }
Chris Masone20d96d2007-03-22 12:13:20 -0400226 ei = btrfs_item_ptr(btrfs_buffer_leaf(path.nodes[0]), path.slots[0],
Chris Mason123abc82007-03-14 14:14:43 -0400227 struct btrfs_extent_item);
Chris Masona28ec192007-03-06 20:08:01 -0500228 BUG_ON(ei->refs == 0);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400229 refs = btrfs_extent_refs(ei) - 1;
230 btrfs_set_extent_refs(ei, refs);
Chris Masond6025572007-03-30 14:27:56 -0400231 btrfs_mark_buffer_dirty(path.nodes[0]);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400232 if (refs == 0) {
Chris Mason1261ec42007-03-20 20:35:03 -0400233 u64 super_blocks_used;
Chris Mason78fae272007-03-25 11:35:08 -0400234
235 if (pin) {
Chris Mason8ef97622007-03-26 10:15:30 -0400236 ret = pin_down_block(root, blocknr, 0);
Chris Mason78fae272007-03-25 11:35:08 -0400237 BUG_ON(ret);
238 }
239
Chris Mason1261ec42007-03-20 20:35:03 -0400240 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
241 btrfs_set_super_blocks_used(info->disk_super,
242 super_blocks_used - num_blocks);
Chris Masone089f052007-03-16 16:20:31 -0400243 ret = btrfs_del_item(trans, extent_root, &path);
Chris Mason78fae272007-03-25 11:35:08 -0400244 if (extent_root->fs_info->last_insert.objectid > blocknr)
Chris Mason9f5fae22007-03-20 14:38:32 -0400245 extent_root->fs_info->last_insert.objectid = blocknr;
Chris Masona28ec192007-03-06 20:08:01 -0500246 if (ret)
247 BUG();
248 }
Chris Mason234b63a2007-03-13 10:46:10 -0400249 btrfs_release_path(extent_root, &path);
Chris Masone089f052007-03-16 16:20:31 -0400250 finish_current_insert(trans, extent_root);
Chris Masona28ec192007-03-06 20:08:01 -0500251 return ret;
252}
253
254/*
Chris Masonfec577f2007-02-26 10:40:21 -0500255 * find all the blocks marked as pending in the radix tree and remove
256 * them from the extent map
257 */
Chris Masone089f052007-03-16 16:20:31 -0400258static int del_pending_extents(struct btrfs_trans_handle *trans, struct
259 btrfs_root *extent_root)
Chris Masonfec577f2007-02-26 10:40:21 -0500260{
261 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -0400262 int wret;
263 int err = 0;
Chris Mason8ef97622007-03-26 10:15:30 -0400264 unsigned long gang[4];
Chris Masonfec577f2007-02-26 10:40:21 -0500265 int i;
Chris Mason8ef97622007-03-26 10:15:30 -0400266 struct radix_tree_root *pending_radix;
267 struct radix_tree_root *pinned_radix;
268
269 pending_radix = &extent_root->fs_info->pending_del_radix;
270 pinned_radix = &extent_root->fs_info->pinned_radix;
Chris Masonfec577f2007-02-26 10:40:21 -0500271
272 while(1) {
Chris Mason8ef97622007-03-26 10:15:30 -0400273 ret = find_first_radix_bit(pending_radix, gang,
274 ARRAY_SIZE(gang));
Chris Masonfec577f2007-02-26 10:40:21 -0500275 if (!ret)
276 break;
277 for (i = 0; i < ret; i++) {
Chris Mason8ef97622007-03-26 10:15:30 -0400278 wret = set_radix_bit(pinned_radix, gang[i]);
279 BUG_ON(wret);
280 wret = clear_radix_bit(pending_radix, gang[i]);
281 BUG_ON(wret);
Chris Masond5719762007-03-23 10:01:08 -0400282 wret = __free_extent(trans, extent_root,
Chris Mason8ef97622007-03-26 10:15:30 -0400283 gang[i], 1, 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400284 if (wret)
285 err = wret;
Chris Masonfec577f2007-02-26 10:40:21 -0500286 }
287 }
Chris Masone20d96d2007-03-22 12:13:20 -0400288 return err;
Chris Masonfec577f2007-02-26 10:40:21 -0500289}
290
291/*
292 * remove an extent from the root, returns 0 on success
293 */
Chris Masone089f052007-03-16 16:20:31 -0400294int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
295 *root, u64 blocknr, u64 num_blocks, int pin)
Chris Masonfec577f2007-02-26 10:40:21 -0500296{
Chris Mason9f5fae22007-03-20 14:38:32 -0400297 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Masonfec577f2007-02-26 10:40:21 -0500298 int pending_ret;
299 int ret;
Chris Masona28ec192007-03-06 20:08:01 -0500300
301 if (root == extent_root) {
Chris Mason8ef97622007-03-26 10:15:30 -0400302 pin_down_block(root, blocknr, 1);
Chris Masona28ec192007-03-06 20:08:01 -0500303 return 0;
304 }
Chris Mason78fae272007-03-25 11:35:08 -0400305 ret = __free_extent(trans, root, blocknr, num_blocks, pin);
Chris Masone20d96d2007-03-22 12:13:20 -0400306 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
Chris Masonfec577f2007-02-26 10:40:21 -0500307 return ret ? ret : pending_ret;
308}
309
310/*
311 * walks the btree of allocated extents and find a hole of a given size.
312 * The key ins is changed to record the hole:
313 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -0400314 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -0500315 * ins->offset == number of blocks
316 * Any available blocks before search_start are skipped.
317 */
Chris Masone089f052007-03-16 16:20:31 -0400318static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
319 *orig_root, u64 num_blocks, u64 search_start, u64
320 search_end, struct btrfs_key *ins)
Chris Masonfec577f2007-02-26 10:40:21 -0500321{
Chris Mason234b63a2007-03-13 10:46:10 -0400322 struct btrfs_path path;
Chris Masone2fa7222007-03-12 16:22:34 -0400323 struct btrfs_key key;
Chris Masonfec577f2007-02-26 10:40:21 -0500324 int ret;
325 u64 hole_size = 0;
326 int slot = 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400327 u64 last_block = 0;
Chris Mason037e6392007-03-07 11:50:24 -0500328 u64 test_block;
Chris Masonfec577f2007-02-26 10:40:21 -0500329 int start_found;
Chris Mason234b63a2007-03-13 10:46:10 -0400330 struct btrfs_leaf *l;
Chris Mason9f5fae22007-03-20 14:38:32 -0400331 struct btrfs_root * root = orig_root->fs_info->extent_root;
Chris Mason0579da42007-03-07 16:15:30 -0500332 int total_needed = num_blocks;
Chris Masone20d96d2007-03-22 12:13:20 -0400333 int level;
Chris Masonfec577f2007-02-26 10:40:21 -0500334
Chris Masone20d96d2007-03-22 12:13:20 -0400335 level = btrfs_header_level(btrfs_buffer_header(root->node));
336 total_needed += (level + 1) * 3;
Chris Mason9f5fae22007-03-20 14:38:32 -0400337 if (root->fs_info->last_insert.objectid > search_start)
338 search_start = root->fs_info->last_insert.objectid;
Chris Mason62e27492007-03-15 12:56:47 -0400339
340 ins->flags = 0;
341 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
342
Chris Masonfec577f2007-02-26 10:40:21 -0500343check_failed:
Chris Mason234b63a2007-03-13 10:46:10 -0400344 btrfs_init_path(&path);
Chris Masonfec577f2007-02-26 10:40:21 -0500345 ins->objectid = search_start;
346 ins->offset = 0;
Chris Masonfec577f2007-02-26 10:40:21 -0500347 start_found = 0;
Chris Masone089f052007-03-16 16:20:31 -0400348 ret = btrfs_search_slot(trans, root, ins, &path, 0, 0);
Chris Mason0f70abe2007-02-28 16:46:22 -0500349 if (ret < 0)
350 goto error;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500351
Chris Mason0579da42007-03-07 16:15:30 -0500352 if (path.slots[0] > 0)
353 path.slots[0]--;
354
Chris Masonfec577f2007-02-26 10:40:21 -0500355 while (1) {
Chris Masone20d96d2007-03-22 12:13:20 -0400356 l = btrfs_buffer_leaf(path.nodes[0]);
Chris Masonfec577f2007-02-26 10:40:21 -0500357 slot = path.slots[0];
Chris Mason7518a232007-03-12 12:01:18 -0400358 if (slot >= btrfs_header_nritems(&l->header)) {
Chris Mason234b63a2007-03-13 10:46:10 -0400359 ret = btrfs_next_leaf(root, &path);
Chris Masonfec577f2007-02-26 10:40:21 -0500360 if (ret == 0)
361 continue;
Chris Mason0f70abe2007-02-28 16:46:22 -0500362 if (ret < 0)
363 goto error;
Chris Masonfec577f2007-02-26 10:40:21 -0500364 if (!start_found) {
365 ins->objectid = search_start;
Chris Mason037e6392007-03-07 11:50:24 -0500366 ins->offset = (u64)-1;
Chris Masonfec577f2007-02-26 10:40:21 -0500367 start_found = 1;
368 goto check_pending;
369 }
370 ins->objectid = last_block > search_start ?
371 last_block : search_start;
Chris Mason037e6392007-03-07 11:50:24 -0500372 ins->offset = (u64)-1;
Chris Masonfec577f2007-02-26 10:40:21 -0500373 goto check_pending;
374 }
Chris Masone2fa7222007-03-12 16:22:34 -0400375 btrfs_disk_key_to_cpu(&key, &l->items[slot].key);
376 if (key.objectid >= search_start) {
Chris Masonfec577f2007-02-26 10:40:21 -0500377 if (start_found) {
Chris Mason0579da42007-03-07 16:15:30 -0500378 if (last_block < search_start)
379 last_block = search_start;
Chris Masone2fa7222007-03-12 16:22:34 -0400380 hole_size = key.objectid - last_block;
Chris Mason037e6392007-03-07 11:50:24 -0500381 if (hole_size > total_needed) {
Chris Masonfec577f2007-02-26 10:40:21 -0500382 ins->objectid = last_block;
Chris Mason037e6392007-03-07 11:50:24 -0500383 ins->offset = hole_size;
Chris Masonfec577f2007-02-26 10:40:21 -0500384 goto check_pending;
385 }
Chris Mason0579da42007-03-07 16:15:30 -0500386 }
Chris Masonfec577f2007-02-26 10:40:21 -0500387 }
Chris Mason0579da42007-03-07 16:15:30 -0500388 start_found = 1;
Chris Masone2fa7222007-03-12 16:22:34 -0400389 last_block = key.objectid + key.offset;
Chris Masonfec577f2007-02-26 10:40:21 -0500390 path.slots[0]++;
391 }
392 // FIXME -ENOSPC
393check_pending:
394 /* we have to make sure we didn't find an extent that has already
395 * been allocated by the map tree or the original allocation
396 */
Chris Mason234b63a2007-03-13 10:46:10 -0400397 btrfs_release_path(root, &path);
Chris Masonfec577f2007-02-26 10:40:21 -0500398 BUG_ON(ins->objectid < search_start);
Chris Mason037e6392007-03-07 11:50:24 -0500399 for (test_block = ins->objectid;
400 test_block < ins->objectid + total_needed; test_block++) {
Chris Mason8ef97622007-03-26 10:15:30 -0400401 if (test_radix_bit(&root->fs_info->pinned_radix,
Chris Mason9f5fae22007-03-20 14:38:32 -0400402 test_block)) {
Chris Mason037e6392007-03-07 11:50:24 -0500403 search_start = test_block + 1;
Chris Masonfec577f2007-02-26 10:40:21 -0500404 goto check_failed;
405 }
406 }
Chris Mason9f5fae22007-03-20 14:38:32 -0400407 BUG_ON(root->fs_info->current_insert.offset);
408 root->fs_info->current_insert.offset = total_needed - num_blocks;
409 root->fs_info->current_insert.objectid = ins->objectid + num_blocks;
410 root->fs_info->current_insert.flags = 0;
411 root->fs_info->last_insert.objectid = ins->objectid;
Chris Mason037e6392007-03-07 11:50:24 -0500412 ins->offset = num_blocks;
Chris Masonfec577f2007-02-26 10:40:21 -0500413 return 0;
Chris Mason0f70abe2007-02-28 16:46:22 -0500414error:
Chris Mason234b63a2007-03-13 10:46:10 -0400415 btrfs_release_path(root, &path);
Chris Mason0f70abe2007-02-28 16:46:22 -0500416 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -0500417}
418
419/*
Chris Masonfec577f2007-02-26 10:40:21 -0500420 * finds a free extent and does all the dirty work required for allocation
421 * returns the key for the extent through ins, and a tree buffer for
422 * the first block of the extent through buf.
423 *
424 * returns 0 if everything worked, non-zero otherwise.
425 */
Chris Masondee26a92007-03-26 16:00:06 -0400426int btrfs_alloc_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masone089f052007-03-16 16:20:31 -0400427 *root, u64 num_blocks, u64 search_start, u64
428 search_end, u64 owner, struct btrfs_key *ins)
Chris Masonfec577f2007-02-26 10:40:21 -0500429{
430 int ret;
431 int pending_ret;
Chris Mason1261ec42007-03-20 20:35:03 -0400432 u64 super_blocks_used;
433 struct btrfs_fs_info *info = root->fs_info;
434 struct btrfs_root *extent_root = info->extent_root;
Chris Mason234b63a2007-03-13 10:46:10 -0400435 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -0500436
Chris Masoncf27e1e2007-03-13 09:49:06 -0400437 btrfs_set_extent_refs(&extent_item, 1);
438 btrfs_set_extent_owner(&extent_item, owner);
Chris Masonfec577f2007-02-26 10:40:21 -0500439
Chris Mason037e6392007-03-07 11:50:24 -0500440 if (root == extent_root) {
Chris Mason9f5fae22007-03-20 14:38:32 -0400441 BUG_ON(extent_root->fs_info->current_insert.offset == 0);
Chris Mason037e6392007-03-07 11:50:24 -0500442 BUG_ON(num_blocks != 1);
Chris Mason9f5fae22007-03-20 14:38:32 -0400443 BUG_ON(extent_root->fs_info->current_insert.flags ==
444 extent_root->fs_info->current_insert.offset);
Chris Mason037e6392007-03-07 11:50:24 -0500445 ins->offset = 1;
Chris Mason9f5fae22007-03-20 14:38:32 -0400446 ins->objectid = extent_root->fs_info->current_insert.objectid +
447 extent_root->fs_info->current_insert.flags++;
Chris Masonfec577f2007-02-26 10:40:21 -0500448 return 0;
449 }
Chris Masone089f052007-03-16 16:20:31 -0400450 ret = find_free_extent(trans, root, num_blocks, search_start,
Chris Mason037e6392007-03-07 11:50:24 -0500451 search_end, ins);
452 if (ret)
453 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -0500454
Chris Mason1261ec42007-03-20 20:35:03 -0400455 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
456 btrfs_set_super_blocks_used(info->disk_super, super_blocks_used +
457 num_blocks);
Chris Masone089f052007-03-16 16:20:31 -0400458 ret = btrfs_insert_item(trans, extent_root, ins, &extent_item,
459 sizeof(extent_item));
Chris Mason037e6392007-03-07 11:50:24 -0500460
Chris Masone089f052007-03-16 16:20:31 -0400461 finish_current_insert(trans, extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -0400462 pending_ret = del_pending_extents(trans, extent_root);
Chris Mason037e6392007-03-07 11:50:24 -0500463 if (ret)
464 return ret;
465 if (pending_ret)
466 return pending_ret;
467 return 0;
Chris Masonfec577f2007-02-26 10:40:21 -0500468}
469
470/*
471 * helper function to allocate a block for a given tree
472 * returns the tree buffer or NULL.
473 */
Chris Masone20d96d2007-03-22 12:13:20 -0400474struct buffer_head *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Chris Masone089f052007-03-16 16:20:31 -0400475 struct btrfs_root *root)
Chris Masonfec577f2007-02-26 10:40:21 -0500476{
Chris Masone2fa7222007-03-12 16:22:34 -0400477 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -0500478 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -0400479 struct buffer_head *buf;
Chris Masonfec577f2007-02-26 10:40:21 -0500480
Chris Masondee26a92007-03-26 16:00:06 -0400481 ret = btrfs_alloc_extent(trans, root, 1, 0, (unsigned long)-1,
Chris Masone20d96d2007-03-22 12:13:20 -0400482 btrfs_header_parentid(btrfs_buffer_header(root->node)), &ins);
Chris Masonfec577f2007-02-26 10:40:21 -0500483 if (ret) {
484 BUG();
485 return NULL;
486 }
Chris Masond98237b2007-03-28 13:57:48 -0400487 buf = btrfs_find_create_tree_block(root, ins.objectid);
Chris Masondf2ce342007-03-23 11:00:45 -0400488 set_buffer_uptodate(buf);
Chris Masonfec577f2007-02-26 10:40:21 -0500489 return buf;
490}
Chris Masona28ec192007-03-06 20:08:01 -0500491
Chris Mason6407bf62007-03-27 06:33:00 -0400492static int drop_leaf_ref(struct btrfs_trans_handle *trans,
493 struct btrfs_root *root, struct buffer_head *cur)
494{
495 struct btrfs_disk_key *key;
496 struct btrfs_leaf *leaf;
497 struct btrfs_file_extent_item *fi;
498 int i;
499 int nritems;
500 int ret;
501
502 BUG_ON(!btrfs_is_leaf(btrfs_buffer_node(cur)));
503 leaf = btrfs_buffer_leaf(cur);
504 nritems = btrfs_header_nritems(&leaf->header);
505 for (i = 0; i < nritems; i++) {
506 key = &leaf->items[i].key;
507 if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY)
508 continue;
509 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
510 /*
511 * FIXME make sure to insert a trans record that
512 * repeats the snapshot del on crash
513 */
514 ret = btrfs_free_extent(trans, root,
515 btrfs_file_extent_disk_blocknr(fi),
516 btrfs_file_extent_disk_num_blocks(fi),
517 0);
518 BUG_ON(ret);
519 }
520 return 0;
521}
522
Chris Mason9aca1d52007-03-13 11:09:37 -0400523/*
524 * helper function for drop_snapshot, this walks down the tree dropping ref
525 * counts as it goes.
526 */
Chris Masone089f052007-03-16 16:20:31 -0400527static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
528 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -0500529{
Chris Masone20d96d2007-03-22 12:13:20 -0400530 struct buffer_head *next;
531 struct buffer_head *cur;
Chris Mason20524f02007-03-10 06:35:47 -0500532 u64 blocknr;
533 int ret;
534 u32 refs;
535
Chris Masone20d96d2007-03-22 12:13:20 -0400536 ret = lookup_block_ref(trans, root, path->nodes[*level]->b_blocknr,
Chris Mason6407bf62007-03-27 06:33:00 -0400537 1, &refs);
Chris Mason20524f02007-03-10 06:35:47 -0500538 BUG_ON(ret);
539 if (refs > 1)
540 goto out;
Chris Mason9aca1d52007-03-13 11:09:37 -0400541 /*
542 * walk down to the last node level and free all the leaves
543 */
Chris Mason6407bf62007-03-27 06:33:00 -0400544 while(*level >= 0) {
Chris Mason20524f02007-03-10 06:35:47 -0500545 cur = path->nodes[*level];
Chris Mason2c90e5d2007-04-02 10:50:19 -0400546 if (btrfs_header_level(btrfs_buffer_header(cur)) != *level)
547 WARN_ON(1);
Chris Mason7518a232007-03-12 12:01:18 -0400548 if (path->slots[*level] >=
Chris Masone20d96d2007-03-22 12:13:20 -0400549 btrfs_header_nritems(btrfs_buffer_header(cur)))
Chris Mason20524f02007-03-10 06:35:47 -0500550 break;
Chris Mason6407bf62007-03-27 06:33:00 -0400551 if (*level == 0) {
552 ret = drop_leaf_ref(trans, root, cur);
553 BUG_ON(ret);
554 break;
555 }
Chris Masone20d96d2007-03-22 12:13:20 -0400556 blocknr = btrfs_node_blockptr(btrfs_buffer_node(cur),
557 path->slots[*level]);
Chris Mason6407bf62007-03-27 06:33:00 -0400558 ret = lookup_block_ref(trans, root, blocknr, 1, &refs);
559 BUG_ON(ret);
560 if (refs != 1) {
Chris Mason20524f02007-03-10 06:35:47 -0500561 path->slots[*level]++;
Chris Masone089f052007-03-16 16:20:31 -0400562 ret = btrfs_free_extent(trans, root, blocknr, 1, 1);
Chris Mason20524f02007-03-10 06:35:47 -0500563 BUG_ON(ret);
564 continue;
565 }
Chris Mason20524f02007-03-10 06:35:47 -0500566 next = read_tree_block(root, blocknr);
Chris Mason83e15a22007-03-12 09:03:27 -0400567 if (path->nodes[*level-1])
Chris Mason234b63a2007-03-13 10:46:10 -0400568 btrfs_block_release(root, path->nodes[*level-1]);
Chris Mason20524f02007-03-10 06:35:47 -0500569 path->nodes[*level-1] = next;
Chris Masone20d96d2007-03-22 12:13:20 -0400570 *level = btrfs_header_level(btrfs_buffer_header(next));
Chris Mason20524f02007-03-10 06:35:47 -0500571 path->slots[*level] = 0;
572 }
573out:
Chris Mason6407bf62007-03-27 06:33:00 -0400574 ret = btrfs_free_extent(trans, root,
575 path->nodes[*level]->b_blocknr, 1, 1);
Chris Mason234b63a2007-03-13 10:46:10 -0400576 btrfs_block_release(root, path->nodes[*level]);
Chris Mason20524f02007-03-10 06:35:47 -0500577 path->nodes[*level] = NULL;
578 *level += 1;
579 BUG_ON(ret);
580 return 0;
581}
582
Chris Mason9aca1d52007-03-13 11:09:37 -0400583/*
584 * helper for dropping snapshots. This walks back up the tree in the path
585 * to find the first node higher up where we haven't yet gone through
586 * all the slots
587 */
Chris Masone089f052007-03-16 16:20:31 -0400588static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root
589 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -0500590{
591 int i;
592 int slot;
593 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400594 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Mason20524f02007-03-10 06:35:47 -0500595 slot = path->slots[i];
Chris Masone20d96d2007-03-22 12:13:20 -0400596 if (slot < btrfs_header_nritems(
597 btrfs_buffer_header(path->nodes[i])) - 1) {
Chris Mason20524f02007-03-10 06:35:47 -0500598 path->slots[i]++;
599 *level = i;
600 return 0;
601 } else {
Chris Masone089f052007-03-16 16:20:31 -0400602 ret = btrfs_free_extent(trans, root,
Chris Masone20d96d2007-03-22 12:13:20 -0400603 path->nodes[*level]->b_blocknr,
Chris Masone089f052007-03-16 16:20:31 -0400604 1, 1);
Chris Mason6407bf62007-03-27 06:33:00 -0400605 BUG_ON(ret);
Chris Mason234b63a2007-03-13 10:46:10 -0400606 btrfs_block_release(root, path->nodes[*level]);
Chris Mason83e15a22007-03-12 09:03:27 -0400607 path->nodes[*level] = NULL;
Chris Mason20524f02007-03-10 06:35:47 -0500608 *level = i + 1;
Chris Mason20524f02007-03-10 06:35:47 -0500609 }
610 }
611 return 1;
612}
613
Chris Mason9aca1d52007-03-13 11:09:37 -0400614/*
615 * drop the reference count on the tree rooted at 'snap'. This traverses
616 * the tree freeing any blocks that have a ref count of zero after being
617 * decremented.
618 */
Chris Masone089f052007-03-16 16:20:31 -0400619int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masone20d96d2007-03-22 12:13:20 -0400620 *root, struct buffer_head *snap)
Chris Mason20524f02007-03-10 06:35:47 -0500621{
Chris Mason3768f362007-03-13 16:47:54 -0400622 int ret = 0;
Chris Mason9aca1d52007-03-13 11:09:37 -0400623 int wret;
Chris Mason20524f02007-03-10 06:35:47 -0500624 int level;
Chris Mason234b63a2007-03-13 10:46:10 -0400625 struct btrfs_path path;
Chris Mason20524f02007-03-10 06:35:47 -0500626 int i;
627 int orig_level;
628
Chris Mason234b63a2007-03-13 10:46:10 -0400629 btrfs_init_path(&path);
Chris Mason20524f02007-03-10 06:35:47 -0500630
Chris Masone20d96d2007-03-22 12:13:20 -0400631 level = btrfs_header_level(btrfs_buffer_header(snap));
Chris Mason20524f02007-03-10 06:35:47 -0500632 orig_level = level;
633 path.nodes[level] = snap;
634 path.slots[level] = 0;
635 while(1) {
Chris Masone089f052007-03-16 16:20:31 -0400636 wret = walk_down_tree(trans, root, &path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -0400637 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -0500638 break;
Chris Mason9aca1d52007-03-13 11:09:37 -0400639 if (wret < 0)
640 ret = wret;
641
Chris Masone089f052007-03-16 16:20:31 -0400642 wret = walk_up_tree(trans, root, &path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -0400643 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -0500644 break;
Chris Mason9aca1d52007-03-13 11:09:37 -0400645 if (wret < 0)
646 ret = wret;
Chris Mason20524f02007-03-10 06:35:47 -0500647 }
Chris Mason83e15a22007-03-12 09:03:27 -0400648 for (i = 0; i <= orig_level; i++) {
649 if (path.nodes[i]) {
Chris Mason234b63a2007-03-13 10:46:10 -0400650 btrfs_block_release(root, path.nodes[i]);
Chris Mason83e15a22007-03-12 09:03:27 -0400651 }
Chris Mason20524f02007-03-10 06:35:47 -0500652 }
Chris Mason9aca1d52007-03-13 11:09:37 -0400653 return ret;
Chris Mason20524f02007-03-10 06:35:47 -0500654}