blob: 176c6dc534ba8adcbe9cdab5e8e96dd2e67cb990 [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 Masonf4b9aa82007-03-27 11:05:53 -0400174 if (!pending) {
175 bh = sb_find_get_block(root->fs_info->sb, blocknr);
176 if (bh) {
177 header = btrfs_buffer_header(bh);
178 if (btrfs_header_generation(header) ==
179 root->fs_info->running_transaction->transid) {
180 brelse(bh);
181 return 0;
182 }
Chris Mason8ef97622007-03-26 10:15:30 -0400183 brelse(bh);
Chris Mason8ef97622007-03-26 10:15:30 -0400184 }
Chris Mason8ef97622007-03-26 10:15:30 -0400185 err = set_radix_bit(&root->fs_info->pinned_radix, blocknr);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400186 } else {
187 err = set_radix_bit(&root->fs_info->pending_del_radix, blocknr);
188 }
Chris Mason8ef97622007-03-26 10:15:30 -0400189 BUG_ON(err);
Chris Masone20d96d2007-03-22 12:13:20 -0400190 return 0;
191}
192
Chris Masona28ec192007-03-06 20:08:01 -0500193/*
194 * remove an extent from the root, returns 0 on success
195 */
Chris Masone089f052007-03-16 16:20:31 -0400196static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason78fae272007-03-25 11:35:08 -0400197 *root, u64 blocknr, u64 num_blocks, int pin)
Chris Masona28ec192007-03-06 20:08:01 -0500198{
Chris Mason234b63a2007-03-13 10:46:10 -0400199 struct btrfs_path path;
Chris Masone2fa7222007-03-12 16:22:34 -0400200 struct btrfs_key key;
Chris Mason1261ec42007-03-20 20:35:03 -0400201 struct btrfs_fs_info *info = root->fs_info;
202 struct btrfs_root *extent_root = info->extent_root;
Chris Masona28ec192007-03-06 20:08:01 -0500203 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400204 struct btrfs_extent_item *ei;
Chris Masone2fa7222007-03-12 16:22:34 -0400205 struct btrfs_key ins;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400206 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500207
Chris Masona28ec192007-03-06 20:08:01 -0500208 key.objectid = blocknr;
209 key.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -0400210 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masona28ec192007-03-06 20:08:01 -0500211 key.offset = num_blocks;
212
Chris Masone089f052007-03-16 16:20:31 -0400213 find_free_extent(trans, root, 0, 0, (u64)-1, &ins);
Chris Mason234b63a2007-03-13 10:46:10 -0400214 btrfs_init_path(&path);
Chris Masone089f052007-03-16 16:20:31 -0400215 ret = btrfs_search_slot(trans, extent_root, &key, &path, -1, 1);
Chris Masona28ec192007-03-06 20:08:01 -0500216 if (ret) {
Chris Mason2e635a22007-03-21 11:12:56 -0400217 printk("failed to find %Lu\n", key.objectid);
Chris Mason234b63a2007-03-13 10:46:10 -0400218 btrfs_print_tree(extent_root, extent_root->node);
Chris Mason2e635a22007-03-21 11:12:56 -0400219 printk("failed to find %Lu\n", key.objectid);
Chris Masona28ec192007-03-06 20:08:01 -0500220 BUG();
221 }
Chris Masone20d96d2007-03-22 12:13:20 -0400222 ei = btrfs_item_ptr(btrfs_buffer_leaf(path.nodes[0]), path.slots[0],
Chris Mason123abc82007-03-14 14:14:43 -0400223 struct btrfs_extent_item);
Chris Masona28ec192007-03-06 20:08:01 -0500224 BUG_ON(ei->refs == 0);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400225 refs = btrfs_extent_refs(ei) - 1;
226 btrfs_set_extent_refs(ei, refs);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400227 mark_buffer_dirty(path.nodes[0]);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400228 if (refs == 0) {
Chris Mason1261ec42007-03-20 20:35:03 -0400229 u64 super_blocks_used;
Chris Mason78fae272007-03-25 11:35:08 -0400230
231 if (pin) {
Chris Mason8ef97622007-03-26 10:15:30 -0400232 ret = pin_down_block(root, blocknr, 0);
Chris Mason78fae272007-03-25 11:35:08 -0400233 BUG_ON(ret);
234 }
235
Chris Mason1261ec42007-03-20 20:35:03 -0400236 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
237 btrfs_set_super_blocks_used(info->disk_super,
238 super_blocks_used - num_blocks);
Chris Masone089f052007-03-16 16:20:31 -0400239 ret = btrfs_del_item(trans, extent_root, &path);
Chris Mason78fae272007-03-25 11:35:08 -0400240 if (extent_root->fs_info->last_insert.objectid > blocknr)
Chris Mason9f5fae22007-03-20 14:38:32 -0400241 extent_root->fs_info->last_insert.objectid = blocknr;
Chris Masona28ec192007-03-06 20:08:01 -0500242 if (ret)
243 BUG();
244 }
Chris Mason234b63a2007-03-13 10:46:10 -0400245 btrfs_release_path(extent_root, &path);
Chris Masone089f052007-03-16 16:20:31 -0400246 finish_current_insert(trans, extent_root);
Chris Masona28ec192007-03-06 20:08:01 -0500247 return ret;
248}
249
250/*
Chris Masonfec577f2007-02-26 10:40:21 -0500251 * find all the blocks marked as pending in the radix tree and remove
252 * them from the extent map
253 */
Chris Masone089f052007-03-16 16:20:31 -0400254static int del_pending_extents(struct btrfs_trans_handle *trans, struct
255 btrfs_root *extent_root)
Chris Masonfec577f2007-02-26 10:40:21 -0500256{
257 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -0400258 int wret;
259 int err = 0;
Chris Mason8ef97622007-03-26 10:15:30 -0400260 unsigned long gang[4];
Chris Masonfec577f2007-02-26 10:40:21 -0500261 int i;
Chris Mason8ef97622007-03-26 10:15:30 -0400262 struct radix_tree_root *pending_radix;
263 struct radix_tree_root *pinned_radix;
264
265 pending_radix = &extent_root->fs_info->pending_del_radix;
266 pinned_radix = &extent_root->fs_info->pinned_radix;
Chris Masonfec577f2007-02-26 10:40:21 -0500267
268 while(1) {
Chris Mason8ef97622007-03-26 10:15:30 -0400269 ret = find_first_radix_bit(pending_radix, gang,
270 ARRAY_SIZE(gang));
Chris Masonfec577f2007-02-26 10:40:21 -0500271 if (!ret)
272 break;
273 for (i = 0; i < ret; i++) {
Chris Mason8ef97622007-03-26 10:15:30 -0400274 wret = set_radix_bit(pinned_radix, gang[i]);
275 BUG_ON(wret);
276 wret = clear_radix_bit(pending_radix, gang[i]);
277 BUG_ON(wret);
Chris Masond5719762007-03-23 10:01:08 -0400278 wret = __free_extent(trans, extent_root,
Chris Mason8ef97622007-03-26 10:15:30 -0400279 gang[i], 1, 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400280 if (wret)
281 err = wret;
Chris Masonfec577f2007-02-26 10:40:21 -0500282 }
283 }
Chris Masone20d96d2007-03-22 12:13:20 -0400284 return err;
Chris Masonfec577f2007-02-26 10:40:21 -0500285}
286
287/*
288 * remove an extent from the root, returns 0 on success
289 */
Chris Masone089f052007-03-16 16:20:31 -0400290int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
291 *root, u64 blocknr, u64 num_blocks, int pin)
Chris Masonfec577f2007-02-26 10:40:21 -0500292{
Chris Mason9f5fae22007-03-20 14:38:32 -0400293 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Masone20d96d2007-03-22 12:13:20 -0400294 struct buffer_head *t;
Chris Masonfec577f2007-02-26 10:40:21 -0500295 int pending_ret;
296 int ret;
Chris Masona28ec192007-03-06 20:08:01 -0500297
298 if (root == extent_root) {
299 t = find_tree_block(root, blocknr);
Chris Mason8ef97622007-03-26 10:15:30 -0400300 pin_down_block(root, blocknr, 1);
Chris Masona28ec192007-03-06 20:08:01 -0500301 return 0;
302 }
Chris Mason78fae272007-03-25 11:35:08 -0400303 ret = __free_extent(trans, root, blocknr, num_blocks, pin);
Chris Masone20d96d2007-03-22 12:13:20 -0400304 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
Chris Masonfec577f2007-02-26 10:40:21 -0500305 return ret ? ret : pending_ret;
306}
307
308/*
309 * walks the btree of allocated extents and find a hole of a given size.
310 * The key ins is changed to record the hole:
311 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -0400312 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -0500313 * ins->offset == number of blocks
314 * Any available blocks before search_start are skipped.
315 */
Chris Masone089f052007-03-16 16:20:31 -0400316static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
317 *orig_root, u64 num_blocks, u64 search_start, u64
318 search_end, struct btrfs_key *ins)
Chris Masonfec577f2007-02-26 10:40:21 -0500319{
Chris Mason234b63a2007-03-13 10:46:10 -0400320 struct btrfs_path path;
Chris Masone2fa7222007-03-12 16:22:34 -0400321 struct btrfs_key key;
Chris Masonfec577f2007-02-26 10:40:21 -0500322 int ret;
323 u64 hole_size = 0;
324 int slot = 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400325 u64 last_block = 0;
Chris Mason037e6392007-03-07 11:50:24 -0500326 u64 test_block;
Chris Masonfec577f2007-02-26 10:40:21 -0500327 int start_found;
Chris Mason234b63a2007-03-13 10:46:10 -0400328 struct btrfs_leaf *l;
Chris Mason9f5fae22007-03-20 14:38:32 -0400329 struct btrfs_root * root = orig_root->fs_info->extent_root;
Chris Mason0579da42007-03-07 16:15:30 -0500330 int total_needed = num_blocks;
Chris Masone20d96d2007-03-22 12:13:20 -0400331 int level;
Chris Masonfec577f2007-02-26 10:40:21 -0500332
Chris Masone20d96d2007-03-22 12:13:20 -0400333 level = btrfs_header_level(btrfs_buffer_header(root->node));
334 total_needed += (level + 1) * 3;
Chris Mason9f5fae22007-03-20 14:38:32 -0400335 if (root->fs_info->last_insert.objectid > search_start)
336 search_start = root->fs_info->last_insert.objectid;
Chris Mason62e27492007-03-15 12:56:47 -0400337
338 ins->flags = 0;
339 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
340
Chris Masonfec577f2007-02-26 10:40:21 -0500341check_failed:
Chris Mason234b63a2007-03-13 10:46:10 -0400342 btrfs_init_path(&path);
Chris Masonfec577f2007-02-26 10:40:21 -0500343 ins->objectid = search_start;
344 ins->offset = 0;
Chris Masonfec577f2007-02-26 10:40:21 -0500345 start_found = 0;
Chris Masone089f052007-03-16 16:20:31 -0400346 ret = btrfs_search_slot(trans, root, ins, &path, 0, 0);
Chris Mason0f70abe2007-02-28 16:46:22 -0500347 if (ret < 0)
348 goto error;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500349
Chris Mason0579da42007-03-07 16:15:30 -0500350 if (path.slots[0] > 0)
351 path.slots[0]--;
352
Chris Masonfec577f2007-02-26 10:40:21 -0500353 while (1) {
Chris Masone20d96d2007-03-22 12:13:20 -0400354 l = btrfs_buffer_leaf(path.nodes[0]);
Chris Masonfec577f2007-02-26 10:40:21 -0500355 slot = path.slots[0];
Chris Mason7518a232007-03-12 12:01:18 -0400356 if (slot >= btrfs_header_nritems(&l->header)) {
Chris Mason234b63a2007-03-13 10:46:10 -0400357 ret = btrfs_next_leaf(root, &path);
Chris Masonfec577f2007-02-26 10:40:21 -0500358 if (ret == 0)
359 continue;
Chris Mason0f70abe2007-02-28 16:46:22 -0500360 if (ret < 0)
361 goto error;
Chris Masonfec577f2007-02-26 10:40:21 -0500362 if (!start_found) {
363 ins->objectid = search_start;
Chris Mason037e6392007-03-07 11:50:24 -0500364 ins->offset = (u64)-1;
Chris Masonfec577f2007-02-26 10:40:21 -0500365 start_found = 1;
366 goto check_pending;
367 }
368 ins->objectid = last_block > search_start ?
369 last_block : search_start;
Chris Mason037e6392007-03-07 11:50:24 -0500370 ins->offset = (u64)-1;
Chris Masonfec577f2007-02-26 10:40:21 -0500371 goto check_pending;
372 }
Chris Masone2fa7222007-03-12 16:22:34 -0400373 btrfs_disk_key_to_cpu(&key, &l->items[slot].key);
374 if (key.objectid >= search_start) {
Chris Masonfec577f2007-02-26 10:40:21 -0500375 if (start_found) {
Chris Mason0579da42007-03-07 16:15:30 -0500376 if (last_block < search_start)
377 last_block = search_start;
Chris Masone2fa7222007-03-12 16:22:34 -0400378 hole_size = key.objectid - last_block;
Chris Mason037e6392007-03-07 11:50:24 -0500379 if (hole_size > total_needed) {
Chris Masonfec577f2007-02-26 10:40:21 -0500380 ins->objectid = last_block;
Chris Mason037e6392007-03-07 11:50:24 -0500381 ins->offset = hole_size;
Chris Masonfec577f2007-02-26 10:40:21 -0500382 goto check_pending;
383 }
Chris Mason0579da42007-03-07 16:15:30 -0500384 }
Chris Masonfec577f2007-02-26 10:40:21 -0500385 }
Chris Mason0579da42007-03-07 16:15:30 -0500386 start_found = 1;
Chris Masone2fa7222007-03-12 16:22:34 -0400387 last_block = key.objectid + key.offset;
Chris Masonfec577f2007-02-26 10:40:21 -0500388 path.slots[0]++;
389 }
390 // FIXME -ENOSPC
391check_pending:
392 /* we have to make sure we didn't find an extent that has already
393 * been allocated by the map tree or the original allocation
394 */
Chris Mason234b63a2007-03-13 10:46:10 -0400395 btrfs_release_path(root, &path);
Chris Masonfec577f2007-02-26 10:40:21 -0500396 BUG_ON(ins->objectid < search_start);
Chris Mason037e6392007-03-07 11:50:24 -0500397 for (test_block = ins->objectid;
398 test_block < ins->objectid + total_needed; test_block++) {
Chris Mason8ef97622007-03-26 10:15:30 -0400399 if (test_radix_bit(&root->fs_info->pinned_radix,
Chris Mason9f5fae22007-03-20 14:38:32 -0400400 test_block)) {
Chris Mason037e6392007-03-07 11:50:24 -0500401 search_start = test_block + 1;
Chris Masonfec577f2007-02-26 10:40:21 -0500402 goto check_failed;
403 }
404 }
Chris Mason9f5fae22007-03-20 14:38:32 -0400405 BUG_ON(root->fs_info->current_insert.offset);
406 root->fs_info->current_insert.offset = total_needed - num_blocks;
407 root->fs_info->current_insert.objectid = ins->objectid + num_blocks;
408 root->fs_info->current_insert.flags = 0;
409 root->fs_info->last_insert.objectid = ins->objectid;
Chris Mason037e6392007-03-07 11:50:24 -0500410 ins->offset = num_blocks;
Chris Masonfec577f2007-02-26 10:40:21 -0500411 return 0;
Chris Mason0f70abe2007-02-28 16:46:22 -0500412error:
Chris Mason234b63a2007-03-13 10:46:10 -0400413 btrfs_release_path(root, &path);
Chris Mason0f70abe2007-02-28 16:46:22 -0500414 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -0500415}
416
417/*
Chris Masonfec577f2007-02-26 10:40:21 -0500418 * finds a free extent and does all the dirty work required for allocation
419 * returns the key for the extent through ins, and a tree buffer for
420 * the first block of the extent through buf.
421 *
422 * returns 0 if everything worked, non-zero otherwise.
423 */
Chris Masondee26a92007-03-26 16:00:06 -0400424int btrfs_alloc_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masone089f052007-03-16 16:20:31 -0400425 *root, u64 num_blocks, u64 search_start, u64
426 search_end, u64 owner, struct btrfs_key *ins)
Chris Masonfec577f2007-02-26 10:40:21 -0500427{
428 int ret;
429 int pending_ret;
Chris Mason1261ec42007-03-20 20:35:03 -0400430 u64 super_blocks_used;
431 struct btrfs_fs_info *info = root->fs_info;
432 struct btrfs_root *extent_root = info->extent_root;
Chris Mason234b63a2007-03-13 10:46:10 -0400433 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -0500434
Chris Masoncf27e1e2007-03-13 09:49:06 -0400435 btrfs_set_extent_refs(&extent_item, 1);
436 btrfs_set_extent_owner(&extent_item, owner);
Chris Masonfec577f2007-02-26 10:40:21 -0500437
Chris Mason037e6392007-03-07 11:50:24 -0500438 if (root == extent_root) {
Chris Mason9f5fae22007-03-20 14:38:32 -0400439 BUG_ON(extent_root->fs_info->current_insert.offset == 0);
Chris Mason037e6392007-03-07 11:50:24 -0500440 BUG_ON(num_blocks != 1);
Chris Mason9f5fae22007-03-20 14:38:32 -0400441 BUG_ON(extent_root->fs_info->current_insert.flags ==
442 extent_root->fs_info->current_insert.offset);
Chris Mason037e6392007-03-07 11:50:24 -0500443 ins->offset = 1;
Chris Mason9f5fae22007-03-20 14:38:32 -0400444 ins->objectid = extent_root->fs_info->current_insert.objectid +
445 extent_root->fs_info->current_insert.flags++;
Chris Masonfec577f2007-02-26 10:40:21 -0500446 return 0;
447 }
Chris Masone089f052007-03-16 16:20:31 -0400448 ret = find_free_extent(trans, root, num_blocks, search_start,
Chris Mason037e6392007-03-07 11:50:24 -0500449 search_end, ins);
450 if (ret)
451 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -0500452
Chris Mason1261ec42007-03-20 20:35:03 -0400453 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
454 btrfs_set_super_blocks_used(info->disk_super, super_blocks_used +
455 num_blocks);
Chris Masone089f052007-03-16 16:20:31 -0400456 ret = btrfs_insert_item(trans, extent_root, ins, &extent_item,
457 sizeof(extent_item));
Chris Mason037e6392007-03-07 11:50:24 -0500458
Chris Masone089f052007-03-16 16:20:31 -0400459 finish_current_insert(trans, extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -0400460 pending_ret = del_pending_extents(trans, extent_root);
Chris Mason037e6392007-03-07 11:50:24 -0500461 if (ret)
462 return ret;
463 if (pending_ret)
464 return pending_ret;
465 return 0;
Chris Masonfec577f2007-02-26 10:40:21 -0500466}
467
468/*
469 * helper function to allocate a block for a given tree
470 * returns the tree buffer or NULL.
471 */
Chris Masone20d96d2007-03-22 12:13:20 -0400472struct buffer_head *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Chris Masone089f052007-03-16 16:20:31 -0400473 struct btrfs_root *root)
Chris Masonfec577f2007-02-26 10:40:21 -0500474{
Chris Masone2fa7222007-03-12 16:22:34 -0400475 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -0500476 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -0400477 struct buffer_head *buf;
Chris Masonfec577f2007-02-26 10:40:21 -0500478
Chris Masondee26a92007-03-26 16:00:06 -0400479 ret = btrfs_alloc_extent(trans, root, 1, 0, (unsigned long)-1,
Chris Masone20d96d2007-03-22 12:13:20 -0400480 btrfs_header_parentid(btrfs_buffer_header(root->node)), &ins);
Chris Masonfec577f2007-02-26 10:40:21 -0500481 if (ret) {
482 BUG();
483 return NULL;
484 }
Chris Mason037e6392007-03-07 11:50:24 -0500485 buf = find_tree_block(root, ins.objectid);
Chris Masondf2ce342007-03-23 11:00:45 -0400486 set_buffer_uptodate(buf);
Chris Masonfec577f2007-02-26 10:40:21 -0500487 return buf;
488}
Chris Masona28ec192007-03-06 20:08:01 -0500489
Chris Mason6407bf62007-03-27 06:33:00 -0400490static int drop_leaf_ref(struct btrfs_trans_handle *trans,
491 struct btrfs_root *root, struct buffer_head *cur)
492{
493 struct btrfs_disk_key *key;
494 struct btrfs_leaf *leaf;
495 struct btrfs_file_extent_item *fi;
496 int i;
497 int nritems;
498 int ret;
499
500 BUG_ON(!btrfs_is_leaf(btrfs_buffer_node(cur)));
501 leaf = btrfs_buffer_leaf(cur);
502 nritems = btrfs_header_nritems(&leaf->header);
503 for (i = 0; i < nritems; i++) {
504 key = &leaf->items[i].key;
505 if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY)
506 continue;
507 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
508 /*
509 * FIXME make sure to insert a trans record that
510 * repeats the snapshot del on crash
511 */
512 ret = btrfs_free_extent(trans, root,
513 btrfs_file_extent_disk_blocknr(fi),
514 btrfs_file_extent_disk_num_blocks(fi),
515 0);
516 BUG_ON(ret);
517 }
518 return 0;
519}
520
Chris Mason9aca1d52007-03-13 11:09:37 -0400521/*
522 * helper function for drop_snapshot, this walks down the tree dropping ref
523 * counts as it goes.
524 */
Chris Masone089f052007-03-16 16:20:31 -0400525static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
526 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -0500527{
Chris Masone20d96d2007-03-22 12:13:20 -0400528 struct buffer_head *next;
529 struct buffer_head *cur;
Chris Mason20524f02007-03-10 06:35:47 -0500530 u64 blocknr;
531 int ret;
532 u32 refs;
533
Chris Masone20d96d2007-03-22 12:13:20 -0400534 ret = lookup_block_ref(trans, root, path->nodes[*level]->b_blocknr,
Chris Mason6407bf62007-03-27 06:33:00 -0400535 1, &refs);
Chris Mason20524f02007-03-10 06:35:47 -0500536 BUG_ON(ret);
537 if (refs > 1)
538 goto out;
Chris Mason9aca1d52007-03-13 11:09:37 -0400539 /*
540 * walk down to the last node level and free all the leaves
541 */
Chris Mason6407bf62007-03-27 06:33:00 -0400542 while(*level >= 0) {
Chris Mason20524f02007-03-10 06:35:47 -0500543 cur = path->nodes[*level];
Chris Mason7518a232007-03-12 12:01:18 -0400544 if (path->slots[*level] >=
Chris Masone20d96d2007-03-22 12:13:20 -0400545 btrfs_header_nritems(btrfs_buffer_header(cur)))
Chris Mason20524f02007-03-10 06:35:47 -0500546 break;
Chris Mason6407bf62007-03-27 06:33:00 -0400547 if (*level == 0) {
548 ret = drop_leaf_ref(trans, root, cur);
549 BUG_ON(ret);
550 break;
551 }
Chris Masone20d96d2007-03-22 12:13:20 -0400552 blocknr = btrfs_node_blockptr(btrfs_buffer_node(cur),
553 path->slots[*level]);
Chris Mason6407bf62007-03-27 06:33:00 -0400554 ret = lookup_block_ref(trans, root, blocknr, 1, &refs);
555 BUG_ON(ret);
556 if (refs != 1) {
Chris Mason20524f02007-03-10 06:35:47 -0500557 path->slots[*level]++;
Chris Masone089f052007-03-16 16:20:31 -0400558 ret = btrfs_free_extent(trans, root, blocknr, 1, 1);
Chris Mason20524f02007-03-10 06:35:47 -0500559 BUG_ON(ret);
560 continue;
561 }
Chris Mason20524f02007-03-10 06:35:47 -0500562 next = read_tree_block(root, blocknr);
Chris Mason83e15a22007-03-12 09:03:27 -0400563 if (path->nodes[*level-1])
Chris Mason234b63a2007-03-13 10:46:10 -0400564 btrfs_block_release(root, path->nodes[*level-1]);
Chris Mason20524f02007-03-10 06:35:47 -0500565 path->nodes[*level-1] = next;
Chris Masone20d96d2007-03-22 12:13:20 -0400566 *level = btrfs_header_level(btrfs_buffer_header(next));
Chris Mason20524f02007-03-10 06:35:47 -0500567 path->slots[*level] = 0;
568 }
569out:
Chris Mason6407bf62007-03-27 06:33:00 -0400570 ret = btrfs_free_extent(trans, root,
571 path->nodes[*level]->b_blocknr, 1, 1);
Chris Mason234b63a2007-03-13 10:46:10 -0400572 btrfs_block_release(root, path->nodes[*level]);
Chris Mason20524f02007-03-10 06:35:47 -0500573 path->nodes[*level] = NULL;
574 *level += 1;
575 BUG_ON(ret);
576 return 0;
577}
578
Chris Mason9aca1d52007-03-13 11:09:37 -0400579/*
580 * helper for dropping snapshots. This walks back up the tree in the path
581 * to find the first node higher up where we haven't yet gone through
582 * all the slots
583 */
Chris Masone089f052007-03-16 16:20:31 -0400584static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root
585 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -0500586{
587 int i;
588 int slot;
589 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400590 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Mason20524f02007-03-10 06:35:47 -0500591 slot = path->slots[i];
Chris Masone20d96d2007-03-22 12:13:20 -0400592 if (slot < btrfs_header_nritems(
593 btrfs_buffer_header(path->nodes[i])) - 1) {
Chris Mason20524f02007-03-10 06:35:47 -0500594 path->slots[i]++;
595 *level = i;
596 return 0;
597 } else {
Chris Masone089f052007-03-16 16:20:31 -0400598 ret = btrfs_free_extent(trans, root,
Chris Masone20d96d2007-03-22 12:13:20 -0400599 path->nodes[*level]->b_blocknr,
Chris Masone089f052007-03-16 16:20:31 -0400600 1, 1);
Chris Mason6407bf62007-03-27 06:33:00 -0400601 BUG_ON(ret);
Chris Mason234b63a2007-03-13 10:46:10 -0400602 btrfs_block_release(root, path->nodes[*level]);
Chris Mason83e15a22007-03-12 09:03:27 -0400603 path->nodes[*level] = NULL;
Chris Mason20524f02007-03-10 06:35:47 -0500604 *level = i + 1;
Chris Mason20524f02007-03-10 06:35:47 -0500605 }
606 }
607 return 1;
608}
609
Chris Mason9aca1d52007-03-13 11:09:37 -0400610/*
611 * drop the reference count on the tree rooted at 'snap'. This traverses
612 * the tree freeing any blocks that have a ref count of zero after being
613 * decremented.
614 */
Chris Masone089f052007-03-16 16:20:31 -0400615int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masone20d96d2007-03-22 12:13:20 -0400616 *root, struct buffer_head *snap)
Chris Mason20524f02007-03-10 06:35:47 -0500617{
Chris Mason3768f362007-03-13 16:47:54 -0400618 int ret = 0;
Chris Mason9aca1d52007-03-13 11:09:37 -0400619 int wret;
Chris Mason20524f02007-03-10 06:35:47 -0500620 int level;
Chris Mason234b63a2007-03-13 10:46:10 -0400621 struct btrfs_path path;
Chris Mason20524f02007-03-10 06:35:47 -0500622 int i;
623 int orig_level;
624
Chris Mason234b63a2007-03-13 10:46:10 -0400625 btrfs_init_path(&path);
Chris Mason20524f02007-03-10 06:35:47 -0500626
Chris Masone20d96d2007-03-22 12:13:20 -0400627 level = btrfs_header_level(btrfs_buffer_header(snap));
Chris Mason20524f02007-03-10 06:35:47 -0500628 orig_level = level;
629 path.nodes[level] = snap;
630 path.slots[level] = 0;
631 while(1) {
Chris Masone089f052007-03-16 16:20:31 -0400632 wret = walk_down_tree(trans, root, &path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -0400633 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -0500634 break;
Chris Mason9aca1d52007-03-13 11:09:37 -0400635 if (wret < 0)
636 ret = wret;
637
Chris Masone089f052007-03-16 16:20:31 -0400638 wret = walk_up_tree(trans, root, &path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -0400639 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -0500640 break;
Chris Mason9aca1d52007-03-13 11:09:37 -0400641 if (wret < 0)
642 ret = wret;
Chris Mason20524f02007-03-10 06:35:47 -0500643 }
Chris Mason83e15a22007-03-12 09:03:27 -0400644 for (i = 0; i <= orig_level; i++) {
645 if (path.nodes[i]) {
Chris Mason234b63a2007-03-13 10:46:10 -0400646 btrfs_block_release(root, path.nodes[i]);
Chris Mason83e15a22007-03-12 09:03:27 -0400647 }
Chris Mason20524f02007-03-10 06:35:47 -0500648 }
Chris Mason9aca1d52007-03-13 11:09:37 -0400649 return ret;
Chris Mason20524f02007-03-10 06:35:47 -0500650}