blob: 2cee9df001f66587eb4bf8035f2b179bb448feb2 [file] [log] [blame]
Chris Mason2e635a22007-03-21 11:12:56 -04001#include <linux/module.h>
Chris Masonfec577f2007-02-26 10:40:21 -05002#include "ctree.h"
3#include "disk-io.h"
4#include "print-tree.h"
Chris Masone089f052007-03-16 16:20:31 -04005#include "transaction.h"
Chris Masonfec577f2007-02-26 10:40:21 -05006
Chris Masone089f052007-03-16 16:20:31 -04007static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
8 *orig_root, u64 num_blocks, u64 search_start, u64
9 search_end, struct btrfs_key *ins);
10static int finish_current_insert(struct btrfs_trans_handle *trans, struct
11 btrfs_root *extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -040012static int del_pending_extents(struct btrfs_trans_handle *trans, struct
13 btrfs_root *extent_root);
Chris Masonfec577f2007-02-26 10:40:21 -050014
Chris Masonb18c6682007-04-17 13:26:50 -040015int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
16 struct btrfs_root *root,
17 u64 blocknr, u64 num_blocks)
Chris Mason02217ed2007-03-02 16:08:05 -050018{
Chris Mason5caf2a02007-04-02 11:20:42 -040019 struct btrfs_path *path;
Chris Mason02217ed2007-03-02 16:08:05 -050020 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -040021 struct btrfs_key key;
Chris Mason234b63a2007-03-13 10:46:10 -040022 struct btrfs_leaf *l;
23 struct btrfs_extent_item *item;
Chris Masone2fa7222007-03-12 16:22:34 -040024 struct btrfs_key ins;
Chris Masoncf27e1e2007-03-13 09:49:06 -040025 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -050026
Chris Mason9f5fae22007-03-20 14:38:32 -040027 find_free_extent(trans, root->fs_info->extent_root, 0, 0, (u64)-1,
28 &ins);
Chris Mason5caf2a02007-04-02 11:20:42 -040029 path = btrfs_alloc_path();
30 BUG_ON(!path);
31 btrfs_init_path(path);
Chris Mason02217ed2007-03-02 16:08:05 -050032 key.objectid = blocknr;
33 key.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -040034 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Mason6407bf62007-03-27 06:33:00 -040035 key.offset = num_blocks;
Chris Mason5caf2a02007-04-02 11:20:42 -040036 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -040037 0, 1);
Chris Masona28ec192007-03-06 20:08:01 -050038 if (ret != 0)
39 BUG();
Chris Mason02217ed2007-03-02 16:08:05 -050040 BUG_ON(ret != 0);
Chris Mason5caf2a02007-04-02 11:20:42 -040041 l = btrfs_buffer_leaf(path->nodes[0]);
42 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Masoncf27e1e2007-03-13 09:49:06 -040043 refs = btrfs_extent_refs(item);
44 btrfs_set_extent_refs(item, refs + 1);
Chris Mason5caf2a02007-04-02 11:20:42 -040045 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masona28ec192007-03-06 20:08:01 -050046
Chris Mason5caf2a02007-04-02 11:20:42 -040047 btrfs_release_path(root->fs_info->extent_root, path);
48 btrfs_free_path(path);
Chris Mason9f5fae22007-03-20 14:38:32 -040049 finish_current_insert(trans, root->fs_info->extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -040050 del_pending_extents(trans, root->fs_info->extent_root);
Chris Mason02217ed2007-03-02 16:08:05 -050051 return 0;
52}
53
Chris Masonb18c6682007-04-17 13:26:50 -040054static int lookup_extent_ref(struct btrfs_trans_handle *trans,
55 struct btrfs_root *root, u64 blocknr,
56 u64 num_blocks, u32 *refs)
Chris Masona28ec192007-03-06 20:08:01 -050057{
Chris Mason5caf2a02007-04-02 11:20:42 -040058 struct btrfs_path *path;
Chris Masona28ec192007-03-06 20:08:01 -050059 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -040060 struct btrfs_key key;
Chris Mason234b63a2007-03-13 10:46:10 -040061 struct btrfs_leaf *l;
62 struct btrfs_extent_item *item;
Chris Mason5caf2a02007-04-02 11:20:42 -040063
64 path = btrfs_alloc_path();
65 btrfs_init_path(path);
Chris Masona28ec192007-03-06 20:08:01 -050066 key.objectid = blocknr;
Chris Mason6407bf62007-03-27 06:33:00 -040067 key.offset = num_blocks;
Chris Mason62e27492007-03-15 12:56:47 -040068 key.flags = 0;
69 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Mason5caf2a02007-04-02 11:20:42 -040070 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -040071 0, 0);
Chris Masona28ec192007-03-06 20:08:01 -050072 if (ret != 0)
73 BUG();
Chris Mason5caf2a02007-04-02 11:20:42 -040074 l = btrfs_buffer_leaf(path->nodes[0]);
75 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Masoncf27e1e2007-03-13 09:49:06 -040076 *refs = btrfs_extent_refs(item);
Chris Mason5caf2a02007-04-02 11:20:42 -040077 btrfs_release_path(root->fs_info->extent_root, path);
78 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -050079 return 0;
80}
81
Chris Masonc5739bb2007-04-10 09:27:04 -040082int btrfs_inc_root_ref(struct btrfs_trans_handle *trans,
83 struct btrfs_root *root)
84{
Chris Masonb18c6682007-04-17 13:26:50 -040085 return btrfs_inc_extent_ref(trans, root, bh_blocknr(root->node), 1);
Chris Masonc5739bb2007-04-10 09:27:04 -040086}
87
Chris Masone089f052007-03-16 16:20:31 -040088int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Masone20d96d2007-03-22 12:13:20 -040089 struct buffer_head *buf)
Chris Mason02217ed2007-03-02 16:08:05 -050090{
91 u64 blocknr;
Chris Masone20d96d2007-03-22 12:13:20 -040092 struct btrfs_node *buf_node;
Chris Mason6407bf62007-03-27 06:33:00 -040093 struct btrfs_leaf *buf_leaf;
94 struct btrfs_disk_key *key;
95 struct btrfs_file_extent_item *fi;
Chris Mason02217ed2007-03-02 16:08:05 -050096 int i;
Chris Mason6407bf62007-03-27 06:33:00 -040097 int leaf;
98 int ret;
Chris Masona28ec192007-03-06 20:08:01 -050099
Chris Mason3768f362007-03-13 16:47:54 -0400100 if (!root->ref_cows)
Chris Masona28ec192007-03-06 20:08:01 -0500101 return 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400102 buf_node = btrfs_buffer_node(buf);
Chris Mason6407bf62007-03-27 06:33:00 -0400103 leaf = btrfs_is_leaf(buf_node);
104 buf_leaf = btrfs_buffer_leaf(buf);
Chris Masone20d96d2007-03-22 12:13:20 -0400105 for (i = 0; i < btrfs_header_nritems(&buf_node->header); i++) {
Chris Mason6407bf62007-03-27 06:33:00 -0400106 if (leaf) {
107 key = &buf_leaf->items[i].key;
108 if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY)
109 continue;
110 fi = btrfs_item_ptr(buf_leaf, i,
111 struct btrfs_file_extent_item);
Chris Masonb18c6682007-04-17 13:26:50 -0400112 ret = btrfs_inc_extent_ref(trans, root,
Chris Mason6407bf62007-03-27 06:33:00 -0400113 btrfs_file_extent_disk_blocknr(fi),
114 btrfs_file_extent_disk_num_blocks(fi));
115 BUG_ON(ret);
116 } else {
117 blocknr = btrfs_node_blockptr(buf_node, i);
Chris Masonb18c6682007-04-17 13:26:50 -0400118 ret = btrfs_inc_extent_ref(trans, root, blocknr, 1);
Chris Mason6407bf62007-03-27 06:33:00 -0400119 BUG_ON(ret);
120 }
Chris Mason02217ed2007-03-02 16:08:05 -0500121 }
122 return 0;
123}
124
Chris Masone089f052007-03-16 16:20:31 -0400125int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, struct
126 btrfs_root *root)
Chris Masona28ec192007-03-06 20:08:01 -0500127{
Chris Mason8ef97622007-03-26 10:15:30 -0400128 unsigned long gang[8];
Chris Mason88fd1462007-03-16 08:56:18 -0400129 u64 first = 0;
Chris Masona28ec192007-03-06 20:08:01 -0500130 int ret;
131 int i;
Chris Mason8ef97622007-03-26 10:15:30 -0400132 struct radix_tree_root *pinned_radix = &root->fs_info->pinned_radix;
Chris Masona28ec192007-03-06 20:08:01 -0500133
134 while(1) {
Chris Mason8ef97622007-03-26 10:15:30 -0400135 ret = find_first_radix_bit(pinned_radix, gang,
136 ARRAY_SIZE(gang));
Chris Masona28ec192007-03-06 20:08:01 -0500137 if (!ret)
138 break;
Chris Mason88fd1462007-03-16 08:56:18 -0400139 if (!first)
Chris Mason8ef97622007-03-26 10:15:30 -0400140 first = gang[0];
Chris Mason0579da42007-03-07 16:15:30 -0500141 for (i = 0; i < ret; i++) {
Chris Mason8ef97622007-03-26 10:15:30 -0400142 clear_radix_bit(pinned_radix, gang[i]);
Chris Mason0579da42007-03-07 16:15:30 -0500143 }
Chris Masona28ec192007-03-06 20:08:01 -0500144 }
Chris Masond5719762007-03-23 10:01:08 -0400145 if (root->fs_info->last_insert.objectid > first)
146 root->fs_info->last_insert.objectid = first;
Chris Mason9f5fae22007-03-20 14:38:32 -0400147 root->fs_info->last_insert.offset = 0;
Chris Masona28ec192007-03-06 20:08:01 -0500148 return 0;
149}
150
Chris Masone089f052007-03-16 16:20:31 -0400151static int finish_current_insert(struct btrfs_trans_handle *trans, struct
152 btrfs_root *extent_root)
Chris Mason037e6392007-03-07 11:50:24 -0500153{
Chris Masone2fa7222007-03-12 16:22:34 -0400154 struct btrfs_key ins;
Chris Mason234b63a2007-03-13 10:46:10 -0400155 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -0500156 int i;
157 int ret;
Chris Mason1261ec42007-03-20 20:35:03 -0400158 u64 super_blocks_used;
159 struct btrfs_fs_info *info = extent_root->fs_info;
Chris Mason037e6392007-03-07 11:50:24 -0500160
Chris Masoncf27e1e2007-03-13 09:49:06 -0400161 btrfs_set_extent_refs(&extent_item, 1);
Chris Mason037e6392007-03-07 11:50:24 -0500162 ins.offset = 1;
163 ins.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -0400164 btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
Chris Mason037e6392007-03-07 11:50:24 -0500165
Chris Mason9f5fae22007-03-20 14:38:32 -0400166 for (i = 0; i < extent_root->fs_info->current_insert.flags; i++) {
167 ins.objectid = extent_root->fs_info->current_insert.objectid +
168 i;
Chris Mason1261ec42007-03-20 20:35:03 -0400169 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
170 btrfs_set_super_blocks_used(info->disk_super,
171 super_blocks_used + 1);
Chris Masone089f052007-03-16 16:20:31 -0400172 ret = btrfs_insert_item(trans, extent_root, &ins, &extent_item,
173 sizeof(extent_item));
Chris Mason037e6392007-03-07 11:50:24 -0500174 BUG_ON(ret);
175 }
Chris Mason9f5fae22007-03-20 14:38:32 -0400176 extent_root->fs_info->current_insert.offset = 0;
Chris Mason037e6392007-03-07 11:50:24 -0500177 return 0;
178}
179
Chris Mason8ef97622007-03-26 10:15:30 -0400180static int pin_down_block(struct btrfs_root *root, u64 blocknr, int pending)
Chris Masone20d96d2007-03-22 12:13:20 -0400181{
182 int err;
Chris Mason78fae272007-03-25 11:35:08 -0400183 struct btrfs_header *header;
Chris Mason8ef97622007-03-26 10:15:30 -0400184 struct buffer_head *bh;
Chris Mason78fae272007-03-25 11:35:08 -0400185
Chris Masonf4b9aa82007-03-27 11:05:53 -0400186 if (!pending) {
Chris Masond98237b2007-03-28 13:57:48 -0400187 bh = btrfs_find_tree_block(root, blocknr);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400188 if (bh) {
189 if (buffer_uptodate(bh)) {
190 u64 transid =
191 root->fs_info->running_transaction->transid;
192 header = btrfs_buffer_header(bh);
193 if (btrfs_header_generation(header) ==
194 transid) {
195 btrfs_block_release(root, bh);
196 return 0;
197 }
Chris Masonf4b9aa82007-03-27 11:05:53 -0400198 }
Chris Masond6025572007-03-30 14:27:56 -0400199 btrfs_block_release(root, bh);
Chris Mason8ef97622007-03-26 10:15:30 -0400200 }
Chris Mason8ef97622007-03-26 10:15:30 -0400201 err = set_radix_bit(&root->fs_info->pinned_radix, blocknr);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400202 } else {
203 err = set_radix_bit(&root->fs_info->pending_del_radix, blocknr);
204 }
Chris Mason8ef97622007-03-26 10:15:30 -0400205 BUG_ON(err);
Chris Masone20d96d2007-03-22 12:13:20 -0400206 return 0;
207}
208
Chris Masona28ec192007-03-06 20:08:01 -0500209/*
210 * remove an extent from the root, returns 0 on success
211 */
Chris Masone089f052007-03-16 16:20:31 -0400212static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason78fae272007-03-25 11:35:08 -0400213 *root, u64 blocknr, u64 num_blocks, int pin)
Chris Masona28ec192007-03-06 20:08:01 -0500214{
Chris Mason5caf2a02007-04-02 11:20:42 -0400215 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -0400216 struct btrfs_key key;
Chris Mason1261ec42007-03-20 20:35:03 -0400217 struct btrfs_fs_info *info = root->fs_info;
218 struct btrfs_root *extent_root = info->extent_root;
Chris Masona28ec192007-03-06 20:08:01 -0500219 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400220 struct btrfs_extent_item *ei;
Chris Masone2fa7222007-03-12 16:22:34 -0400221 struct btrfs_key ins;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400222 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500223
Chris Masona28ec192007-03-06 20:08:01 -0500224 key.objectid = blocknr;
225 key.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -0400226 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masona28ec192007-03-06 20:08:01 -0500227 key.offset = num_blocks;
228
Chris Masone089f052007-03-16 16:20:31 -0400229 find_free_extent(trans, root, 0, 0, (u64)-1, &ins);
Chris Mason5caf2a02007-04-02 11:20:42 -0400230 path = btrfs_alloc_path();
231 BUG_ON(!path);
232 btrfs_init_path(path);
Chris Mason5f26f772007-04-05 10:38:44 -0400233
Chris Mason5caf2a02007-04-02 11:20:42 -0400234 ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
Chris Masona28ec192007-03-06 20:08:01 -0500235 if (ret) {
Chris Mason2e635a22007-03-21 11:12:56 -0400236 printk("failed to find %Lu\n", key.objectid);
Chris Mason234b63a2007-03-13 10:46:10 -0400237 btrfs_print_tree(extent_root, extent_root->node);
Chris Mason2e635a22007-03-21 11:12:56 -0400238 printk("failed to find %Lu\n", key.objectid);
Chris Masona28ec192007-03-06 20:08:01 -0500239 BUG();
240 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400241 ei = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
Chris Mason123abc82007-03-14 14:14:43 -0400242 struct btrfs_extent_item);
Chris Masona28ec192007-03-06 20:08:01 -0500243 BUG_ON(ei->refs == 0);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400244 refs = btrfs_extent_refs(ei) - 1;
245 btrfs_set_extent_refs(ei, refs);
Chris Mason5caf2a02007-04-02 11:20:42 -0400246 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400247 if (refs == 0) {
Chris Mason1261ec42007-03-20 20:35:03 -0400248 u64 super_blocks_used;
Chris Mason78fae272007-03-25 11:35:08 -0400249
250 if (pin) {
Chris Mason8ef97622007-03-26 10:15:30 -0400251 ret = pin_down_block(root, blocknr, 0);
Chris Mason78fae272007-03-25 11:35:08 -0400252 BUG_ON(ret);
253 }
254
Chris Mason1261ec42007-03-20 20:35:03 -0400255 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
256 btrfs_set_super_blocks_used(info->disk_super,
257 super_blocks_used - num_blocks);
Chris Mason5caf2a02007-04-02 11:20:42 -0400258 ret = btrfs_del_item(trans, extent_root, path);
Chris Masona28ec192007-03-06 20:08:01 -0500259 if (ret)
260 BUG();
261 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400262 btrfs_release_path(extent_root, path);
263 btrfs_free_path(path);
Chris Masone089f052007-03-16 16:20:31 -0400264 finish_current_insert(trans, extent_root);
Chris Masona28ec192007-03-06 20:08:01 -0500265 return ret;
266}
267
268/*
Chris Masonfec577f2007-02-26 10:40:21 -0500269 * find all the blocks marked as pending in the radix tree and remove
270 * them from the extent map
271 */
Chris Masone089f052007-03-16 16:20:31 -0400272static int del_pending_extents(struct btrfs_trans_handle *trans, struct
273 btrfs_root *extent_root)
Chris Masonfec577f2007-02-26 10:40:21 -0500274{
275 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -0400276 int wret;
277 int err = 0;
Chris Mason8ef97622007-03-26 10:15:30 -0400278 unsigned long gang[4];
Chris Masonfec577f2007-02-26 10:40:21 -0500279 int i;
Chris Mason8ef97622007-03-26 10:15:30 -0400280 struct radix_tree_root *pending_radix;
281 struct radix_tree_root *pinned_radix;
282
283 pending_radix = &extent_root->fs_info->pending_del_radix;
284 pinned_radix = &extent_root->fs_info->pinned_radix;
Chris Masonfec577f2007-02-26 10:40:21 -0500285
286 while(1) {
Chris Mason8ef97622007-03-26 10:15:30 -0400287 ret = find_first_radix_bit(pending_radix, gang,
288 ARRAY_SIZE(gang));
Chris Masonfec577f2007-02-26 10:40:21 -0500289 if (!ret)
290 break;
291 for (i = 0; i < ret; i++) {
Chris Mason8ef97622007-03-26 10:15:30 -0400292 wret = set_radix_bit(pinned_radix, gang[i]);
293 BUG_ON(wret);
294 wret = clear_radix_bit(pending_radix, gang[i]);
295 BUG_ON(wret);
Chris Masond5719762007-03-23 10:01:08 -0400296 wret = __free_extent(trans, extent_root,
Chris Mason8ef97622007-03-26 10:15:30 -0400297 gang[i], 1, 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400298 if (wret)
299 err = wret;
Chris Masonfec577f2007-02-26 10:40:21 -0500300 }
301 }
Chris Masone20d96d2007-03-22 12:13:20 -0400302 return err;
Chris Masonfec577f2007-02-26 10:40:21 -0500303}
304
305/*
306 * remove an extent from the root, returns 0 on success
307 */
Chris Masone089f052007-03-16 16:20:31 -0400308int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
309 *root, u64 blocknr, u64 num_blocks, int pin)
Chris Masonfec577f2007-02-26 10:40:21 -0500310{
Chris Mason9f5fae22007-03-20 14:38:32 -0400311 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Masonfec577f2007-02-26 10:40:21 -0500312 int pending_ret;
313 int ret;
Chris Masona28ec192007-03-06 20:08:01 -0500314
315 if (root == extent_root) {
Chris Mason8ef97622007-03-26 10:15:30 -0400316 pin_down_block(root, blocknr, 1);
Chris Masona28ec192007-03-06 20:08:01 -0500317 return 0;
318 }
Chris Mason78fae272007-03-25 11:35:08 -0400319 ret = __free_extent(trans, root, blocknr, num_blocks, pin);
Chris Masone20d96d2007-03-22 12:13:20 -0400320 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
Chris Masonfec577f2007-02-26 10:40:21 -0500321 return ret ? ret : pending_ret;
322}
323
324/*
325 * walks the btree of allocated extents and find a hole of a given size.
326 * The key ins is changed to record the hole:
327 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -0400328 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -0500329 * ins->offset == number of blocks
330 * Any available blocks before search_start are skipped.
331 */
Chris Masone089f052007-03-16 16:20:31 -0400332static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
333 *orig_root, u64 num_blocks, u64 search_start, u64
334 search_end, struct btrfs_key *ins)
Chris Masonfec577f2007-02-26 10:40:21 -0500335{
Chris Mason5caf2a02007-04-02 11:20:42 -0400336 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -0400337 struct btrfs_key key;
Chris Masonfec577f2007-02-26 10:40:21 -0500338 int ret;
339 u64 hole_size = 0;
340 int slot = 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400341 u64 last_block = 0;
Chris Mason037e6392007-03-07 11:50:24 -0500342 u64 test_block;
Chris Masonfec577f2007-02-26 10:40:21 -0500343 int start_found;
Chris Mason234b63a2007-03-13 10:46:10 -0400344 struct btrfs_leaf *l;
Chris Mason9f5fae22007-03-20 14:38:32 -0400345 struct btrfs_root * root = orig_root->fs_info->extent_root;
Chris Mason0579da42007-03-07 16:15:30 -0500346 int total_needed = num_blocks;
Chris Masone20d96d2007-03-22 12:13:20 -0400347 int level;
Chris Masonfec577f2007-02-26 10:40:21 -0500348
Chris Masonb1a4d962007-04-04 15:27:52 -0400349 path = btrfs_alloc_path();
350 ins->flags = 0;
351 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
352
Chris Masone20d96d2007-03-22 12:13:20 -0400353 level = btrfs_header_level(btrfs_buffer_header(root->node));
354 total_needed += (level + 1) * 3;
Chris Masonb1a4d962007-04-04 15:27:52 -0400355 if (root->fs_info->last_insert.objectid == 0 && search_end == (u64)-1) {
356 struct btrfs_disk_key *last_key;
357 btrfs_init_path(path);
358 ins->objectid = (u64)-1;
359 ins->offset = (u64)-1;
360 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
361 if (ret < 0)
362 goto error;
363 BUG_ON(ret == 0);
364 if (path->slots[0] > 0)
365 path->slots[0]--;
366 l = btrfs_buffer_leaf(path->nodes[0]);
367 last_key = &l->items[path->slots[0]].key;
368 search_start = btrfs_disk_key_objectid(last_key);
369 }
Chris Mason9f5fae22007-03-20 14:38:32 -0400370 if (root->fs_info->last_insert.objectid > search_start)
371 search_start = root->fs_info->last_insert.objectid;
Chris Mason62e27492007-03-15 12:56:47 -0400372
Chris Masonfec577f2007-02-26 10:40:21 -0500373check_failed:
Chris Mason5caf2a02007-04-02 11:20:42 -0400374 btrfs_init_path(path);
Chris Masonfec577f2007-02-26 10:40:21 -0500375 ins->objectid = search_start;
376 ins->offset = 0;
Chris Masonfec577f2007-02-26 10:40:21 -0500377 start_found = 0;
Chris Mason5caf2a02007-04-02 11:20:42 -0400378 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
Chris Mason0f70abe2007-02-28 16:46:22 -0500379 if (ret < 0)
380 goto error;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500381
Chris Mason5caf2a02007-04-02 11:20:42 -0400382 if (path->slots[0] > 0)
383 path->slots[0]--;
Chris Mason0579da42007-03-07 16:15:30 -0500384
Chris Masonfec577f2007-02-26 10:40:21 -0500385 while (1) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400386 l = btrfs_buffer_leaf(path->nodes[0]);
387 slot = path->slots[0];
Chris Mason7518a232007-03-12 12:01:18 -0400388 if (slot >= btrfs_header_nritems(&l->header)) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400389 ret = btrfs_next_leaf(root, path);
Chris Masonfec577f2007-02-26 10:40:21 -0500390 if (ret == 0)
391 continue;
Chris Mason0f70abe2007-02-28 16:46:22 -0500392 if (ret < 0)
393 goto error;
Chris Masonfec577f2007-02-26 10:40:21 -0500394 if (!start_found) {
395 ins->objectid = search_start;
Chris Mason037e6392007-03-07 11:50:24 -0500396 ins->offset = (u64)-1;
Chris Masonfec577f2007-02-26 10:40:21 -0500397 start_found = 1;
398 goto check_pending;
399 }
400 ins->objectid = last_block > search_start ?
401 last_block : search_start;
Chris Mason037e6392007-03-07 11:50:24 -0500402 ins->offset = (u64)-1;
Chris Masonfec577f2007-02-26 10:40:21 -0500403 goto check_pending;
404 }
Chris Masone2fa7222007-03-12 16:22:34 -0400405 btrfs_disk_key_to_cpu(&key, &l->items[slot].key);
406 if (key.objectid >= search_start) {
Chris Masonfec577f2007-02-26 10:40:21 -0500407 if (start_found) {
Chris Mason0579da42007-03-07 16:15:30 -0500408 if (last_block < search_start)
409 last_block = search_start;
Chris Masone2fa7222007-03-12 16:22:34 -0400410 hole_size = key.objectid - last_block;
Chris Mason037e6392007-03-07 11:50:24 -0500411 if (hole_size > total_needed) {
Chris Masonfec577f2007-02-26 10:40:21 -0500412 ins->objectid = last_block;
Chris Mason037e6392007-03-07 11:50:24 -0500413 ins->offset = hole_size;
Chris Masonfec577f2007-02-26 10:40:21 -0500414 goto check_pending;
415 }
Chris Mason0579da42007-03-07 16:15:30 -0500416 }
Chris Masonfec577f2007-02-26 10:40:21 -0500417 }
Chris Mason0579da42007-03-07 16:15:30 -0500418 start_found = 1;
Chris Masone2fa7222007-03-12 16:22:34 -0400419 last_block = key.objectid + key.offset;
Chris Mason5caf2a02007-04-02 11:20:42 -0400420 path->slots[0]++;
Chris Masonfec577f2007-02-26 10:40:21 -0500421 }
422 // FIXME -ENOSPC
423check_pending:
424 /* we have to make sure we didn't find an extent that has already
425 * been allocated by the map tree or the original allocation
426 */
Chris Mason5caf2a02007-04-02 11:20:42 -0400427 btrfs_release_path(root, path);
Chris Masonfec577f2007-02-26 10:40:21 -0500428 BUG_ON(ins->objectid < search_start);
Chris Mason037e6392007-03-07 11:50:24 -0500429 for (test_block = ins->objectid;
430 test_block < ins->objectid + total_needed; test_block++) {
Chris Mason8ef97622007-03-26 10:15:30 -0400431 if (test_radix_bit(&root->fs_info->pinned_radix,
Chris Mason9f5fae22007-03-20 14:38:32 -0400432 test_block)) {
Chris Mason037e6392007-03-07 11:50:24 -0500433 search_start = test_block + 1;
Chris Masonfec577f2007-02-26 10:40:21 -0500434 goto check_failed;
435 }
436 }
Chris Mason9f5fae22007-03-20 14:38:32 -0400437 BUG_ON(root->fs_info->current_insert.offset);
438 root->fs_info->current_insert.offset = total_needed - num_blocks;
439 root->fs_info->current_insert.objectid = ins->objectid + num_blocks;
440 root->fs_info->current_insert.flags = 0;
441 root->fs_info->last_insert.objectid = ins->objectid;
Chris Mason037e6392007-03-07 11:50:24 -0500442 ins->offset = num_blocks;
Chris Mason5caf2a02007-04-02 11:20:42 -0400443 btrfs_free_path(path);
Chris Masonfec577f2007-02-26 10:40:21 -0500444 return 0;
Chris Mason0f70abe2007-02-28 16:46:22 -0500445error:
Chris Mason5caf2a02007-04-02 11:20:42 -0400446 btrfs_release_path(root, path);
447 btrfs_free_path(path);
Chris Mason0f70abe2007-02-28 16:46:22 -0500448 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -0500449}
450
451/*
Chris Masonfec577f2007-02-26 10:40:21 -0500452 * finds a free extent and does all the dirty work required for allocation
453 * returns the key for the extent through ins, and a tree buffer for
454 * the first block of the extent through buf.
455 *
456 * returns 0 if everything worked, non-zero otherwise.
457 */
Chris Masondee26a92007-03-26 16:00:06 -0400458int btrfs_alloc_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masone089f052007-03-16 16:20:31 -0400459 *root, u64 num_blocks, u64 search_start, u64
Chris Masond0dbc6242007-04-10 12:36:36 -0400460 search_end, struct btrfs_key *ins)
Chris Masonfec577f2007-02-26 10:40:21 -0500461{
462 int ret;
463 int pending_ret;
Chris Mason1261ec42007-03-20 20:35:03 -0400464 u64 super_blocks_used;
465 struct btrfs_fs_info *info = root->fs_info;
466 struct btrfs_root *extent_root = info->extent_root;
Chris Mason234b63a2007-03-13 10:46:10 -0400467 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -0500468
Chris Masoncf27e1e2007-03-13 09:49:06 -0400469 btrfs_set_extent_refs(&extent_item, 1);
Chris Masonfec577f2007-02-26 10:40:21 -0500470
Chris Mason037e6392007-03-07 11:50:24 -0500471 if (root == extent_root) {
Chris Mason9f5fae22007-03-20 14:38:32 -0400472 BUG_ON(extent_root->fs_info->current_insert.offset == 0);
Chris Mason037e6392007-03-07 11:50:24 -0500473 BUG_ON(num_blocks != 1);
Chris Mason9f5fae22007-03-20 14:38:32 -0400474 BUG_ON(extent_root->fs_info->current_insert.flags ==
475 extent_root->fs_info->current_insert.offset);
Chris Mason037e6392007-03-07 11:50:24 -0500476 ins->offset = 1;
Chris Mason9f5fae22007-03-20 14:38:32 -0400477 ins->objectid = extent_root->fs_info->current_insert.objectid +
478 extent_root->fs_info->current_insert.flags++;
Chris Masonfec577f2007-02-26 10:40:21 -0500479 return 0;
480 }
Chris Masone089f052007-03-16 16:20:31 -0400481 ret = find_free_extent(trans, root, num_blocks, search_start,
Chris Mason037e6392007-03-07 11:50:24 -0500482 search_end, ins);
483 if (ret)
484 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -0500485
Chris Mason1261ec42007-03-20 20:35:03 -0400486 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
487 btrfs_set_super_blocks_used(info->disk_super, super_blocks_used +
488 num_blocks);
Chris Masone089f052007-03-16 16:20:31 -0400489 ret = btrfs_insert_item(trans, extent_root, ins, &extent_item,
490 sizeof(extent_item));
Chris Mason037e6392007-03-07 11:50:24 -0500491
Chris Masone089f052007-03-16 16:20:31 -0400492 finish_current_insert(trans, extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -0400493 pending_ret = del_pending_extents(trans, extent_root);
Chris Mason037e6392007-03-07 11:50:24 -0500494 if (ret)
495 return ret;
496 if (pending_ret)
497 return pending_ret;
498 return 0;
Chris Masonfec577f2007-02-26 10:40:21 -0500499}
500
501/*
502 * helper function to allocate a block for a given tree
503 * returns the tree buffer or NULL.
504 */
Chris Masone20d96d2007-03-22 12:13:20 -0400505struct buffer_head *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Chris Masone089f052007-03-16 16:20:31 -0400506 struct btrfs_root *root)
Chris Masonfec577f2007-02-26 10:40:21 -0500507{
Chris Masone2fa7222007-03-12 16:22:34 -0400508 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -0500509 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -0400510 struct buffer_head *buf;
Chris Masonfec577f2007-02-26 10:40:21 -0500511
Chris Masond0dbc6242007-04-10 12:36:36 -0400512 ret = btrfs_alloc_extent(trans, root, 1, 0, (unsigned long)-1, &ins);
Chris Masonfec577f2007-02-26 10:40:21 -0500513 if (ret) {
514 BUG();
515 return NULL;
516 }
Chris Masond98237b2007-03-28 13:57:48 -0400517 buf = btrfs_find_create_tree_block(root, ins.objectid);
Chris Masondf2ce342007-03-23 11:00:45 -0400518 set_buffer_uptodate(buf);
Chris Masonfec577f2007-02-26 10:40:21 -0500519 return buf;
520}
Chris Masona28ec192007-03-06 20:08:01 -0500521
Chris Mason6407bf62007-03-27 06:33:00 -0400522static int drop_leaf_ref(struct btrfs_trans_handle *trans,
523 struct btrfs_root *root, struct buffer_head *cur)
524{
525 struct btrfs_disk_key *key;
526 struct btrfs_leaf *leaf;
527 struct btrfs_file_extent_item *fi;
528 int i;
529 int nritems;
530 int ret;
531
532 BUG_ON(!btrfs_is_leaf(btrfs_buffer_node(cur)));
533 leaf = btrfs_buffer_leaf(cur);
534 nritems = btrfs_header_nritems(&leaf->header);
535 for (i = 0; i < nritems; i++) {
536 key = &leaf->items[i].key;
537 if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY)
538 continue;
539 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
540 /*
541 * FIXME make sure to insert a trans record that
542 * repeats the snapshot del on crash
543 */
544 ret = btrfs_free_extent(trans, root,
545 btrfs_file_extent_disk_blocknr(fi),
546 btrfs_file_extent_disk_num_blocks(fi),
547 0);
548 BUG_ON(ret);
549 }
550 return 0;
551}
552
Chris Mason9aca1d52007-03-13 11:09:37 -0400553/*
554 * helper function for drop_snapshot, this walks down the tree dropping ref
555 * counts as it goes.
556 */
Chris Masone089f052007-03-16 16:20:31 -0400557static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
558 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -0500559{
Chris Masone20d96d2007-03-22 12:13:20 -0400560 struct buffer_head *next;
561 struct buffer_head *cur;
Chris Mason20524f02007-03-10 06:35:47 -0500562 u64 blocknr;
563 int ret;
564 u32 refs;
565
Chris Mason5caf2a02007-04-02 11:20:42 -0400566 WARN_ON(*level < 0);
567 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Masonb18c6682007-04-17 13:26:50 -0400568 ret = lookup_extent_ref(trans, root, bh_blocknr(path->nodes[*level]),
Chris Mason6407bf62007-03-27 06:33:00 -0400569 1, &refs);
Chris Mason20524f02007-03-10 06:35:47 -0500570 BUG_ON(ret);
571 if (refs > 1)
572 goto out;
Chris Mason9aca1d52007-03-13 11:09:37 -0400573 /*
574 * walk down to the last node level and free all the leaves
575 */
Chris Mason6407bf62007-03-27 06:33:00 -0400576 while(*level >= 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400577 WARN_ON(*level < 0);
578 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason20524f02007-03-10 06:35:47 -0500579 cur = path->nodes[*level];
Chris Mason2c90e5d2007-04-02 10:50:19 -0400580 if (btrfs_header_level(btrfs_buffer_header(cur)) != *level)
581 WARN_ON(1);
Chris Mason7518a232007-03-12 12:01:18 -0400582 if (path->slots[*level] >=
Chris Masone20d96d2007-03-22 12:13:20 -0400583 btrfs_header_nritems(btrfs_buffer_header(cur)))
Chris Mason20524f02007-03-10 06:35:47 -0500584 break;
Chris Mason6407bf62007-03-27 06:33:00 -0400585 if (*level == 0) {
586 ret = drop_leaf_ref(trans, root, cur);
587 BUG_ON(ret);
588 break;
589 }
Chris Masone20d96d2007-03-22 12:13:20 -0400590 blocknr = btrfs_node_blockptr(btrfs_buffer_node(cur),
591 path->slots[*level]);
Chris Masonb18c6682007-04-17 13:26:50 -0400592 ret = lookup_extent_ref(trans, root, blocknr, 1, &refs);
Chris Mason6407bf62007-03-27 06:33:00 -0400593 BUG_ON(ret);
594 if (refs != 1) {
Chris Mason20524f02007-03-10 06:35:47 -0500595 path->slots[*level]++;
Chris Masone089f052007-03-16 16:20:31 -0400596 ret = btrfs_free_extent(trans, root, blocknr, 1, 1);
Chris Mason20524f02007-03-10 06:35:47 -0500597 BUG_ON(ret);
598 continue;
599 }
Chris Mason20524f02007-03-10 06:35:47 -0500600 next = read_tree_block(root, blocknr);
Chris Mason5caf2a02007-04-02 11:20:42 -0400601 WARN_ON(*level <= 0);
Chris Mason83e15a22007-03-12 09:03:27 -0400602 if (path->nodes[*level-1])
Chris Mason234b63a2007-03-13 10:46:10 -0400603 btrfs_block_release(root, path->nodes[*level-1]);
Chris Mason20524f02007-03-10 06:35:47 -0500604 path->nodes[*level-1] = next;
Chris Masone20d96d2007-03-22 12:13:20 -0400605 *level = btrfs_header_level(btrfs_buffer_header(next));
Chris Mason20524f02007-03-10 06:35:47 -0500606 path->slots[*level] = 0;
607 }
608out:
Chris Mason5caf2a02007-04-02 11:20:42 -0400609 WARN_ON(*level < 0);
610 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason6407bf62007-03-27 06:33:00 -0400611 ret = btrfs_free_extent(trans, root,
Chris Mason7eccb902007-04-11 15:53:25 -0400612 bh_blocknr(path->nodes[*level]), 1, 1);
Chris Mason234b63a2007-03-13 10:46:10 -0400613 btrfs_block_release(root, path->nodes[*level]);
Chris Mason20524f02007-03-10 06:35:47 -0500614 path->nodes[*level] = NULL;
615 *level += 1;
616 BUG_ON(ret);
617 return 0;
618}
619
Chris Mason9aca1d52007-03-13 11:09:37 -0400620/*
621 * helper for dropping snapshots. This walks back up the tree in the path
622 * to find the first node higher up where we haven't yet gone through
623 * all the slots
624 */
Chris Masone089f052007-03-16 16:20:31 -0400625static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root
626 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -0500627{
628 int i;
629 int slot;
630 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400631 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Mason20524f02007-03-10 06:35:47 -0500632 slot = path->slots[i];
Chris Masone20d96d2007-03-22 12:13:20 -0400633 if (slot < btrfs_header_nritems(
634 btrfs_buffer_header(path->nodes[i])) - 1) {
Chris Mason20524f02007-03-10 06:35:47 -0500635 path->slots[i]++;
636 *level = i;
637 return 0;
638 } else {
Chris Masone089f052007-03-16 16:20:31 -0400639 ret = btrfs_free_extent(trans, root,
Chris Mason7eccb902007-04-11 15:53:25 -0400640 bh_blocknr(path->nodes[*level]),
Chris Masone089f052007-03-16 16:20:31 -0400641 1, 1);
Chris Mason6407bf62007-03-27 06:33:00 -0400642 BUG_ON(ret);
Chris Mason234b63a2007-03-13 10:46:10 -0400643 btrfs_block_release(root, path->nodes[*level]);
Chris Mason83e15a22007-03-12 09:03:27 -0400644 path->nodes[*level] = NULL;
Chris Mason20524f02007-03-10 06:35:47 -0500645 *level = i + 1;
Chris Mason20524f02007-03-10 06:35:47 -0500646 }
647 }
648 return 1;
649}
650
Chris Mason9aca1d52007-03-13 11:09:37 -0400651/*
652 * drop the reference count on the tree rooted at 'snap'. This traverses
653 * the tree freeing any blocks that have a ref count of zero after being
654 * decremented.
655 */
Chris Masone089f052007-03-16 16:20:31 -0400656int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masone20d96d2007-03-22 12:13:20 -0400657 *root, struct buffer_head *snap)
Chris Mason20524f02007-03-10 06:35:47 -0500658{
Chris Mason3768f362007-03-13 16:47:54 -0400659 int ret = 0;
Chris Mason9aca1d52007-03-13 11:09:37 -0400660 int wret;
Chris Mason20524f02007-03-10 06:35:47 -0500661 int level;
Chris Mason5caf2a02007-04-02 11:20:42 -0400662 struct btrfs_path *path;
Chris Mason20524f02007-03-10 06:35:47 -0500663 int i;
664 int orig_level;
665
Chris Mason5caf2a02007-04-02 11:20:42 -0400666 path = btrfs_alloc_path();
667 BUG_ON(!path);
668 btrfs_init_path(path);
Chris Mason20524f02007-03-10 06:35:47 -0500669
Chris Masone20d96d2007-03-22 12:13:20 -0400670 level = btrfs_header_level(btrfs_buffer_header(snap));
Chris Mason20524f02007-03-10 06:35:47 -0500671 orig_level = level;
Chris Mason5caf2a02007-04-02 11:20:42 -0400672 path->nodes[level] = snap;
673 path->slots[level] = 0;
Chris Mason20524f02007-03-10 06:35:47 -0500674 while(1) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400675 wret = walk_down_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -0400676 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -0500677 break;
Chris Mason9aca1d52007-03-13 11:09:37 -0400678 if (wret < 0)
679 ret = wret;
680
Chris Mason5caf2a02007-04-02 11:20:42 -0400681 wret = walk_up_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -0400682 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -0500683 break;
Chris Mason9aca1d52007-03-13 11:09:37 -0400684 if (wret < 0)
685 ret = wret;
Chris Mason20524f02007-03-10 06:35:47 -0500686 }
Chris Mason83e15a22007-03-12 09:03:27 -0400687 for (i = 0; i <= orig_level; i++) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400688 if (path->nodes[i]) {
689 btrfs_block_release(root, path->nodes[i]);
Chris Mason83e15a22007-03-12 09:03:27 -0400690 }
Chris Mason20524f02007-03-10 06:35:47 -0500691 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400692 btrfs_free_path(path);
Chris Mason9aca1d52007-03-13 11:09:37 -0400693 return ret;
Chris Mason20524f02007-03-10 06:35:47 -0500694}