blob: d560831c10a7d31591094019cd19af451656a7ec [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 Mason5caf2a02007-04-02 11:20:42 -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 Mason5caf2a02007-04-02 11:20:42 -040028 path = btrfs_alloc_path();
29 BUG_ON(!path);
30 btrfs_init_path(path);
Chris Mason02217ed2007-03-02 16:08:05 -050031 key.objectid = blocknr;
32 key.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -040033 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Mason6407bf62007-03-27 06:33:00 -040034 key.offset = num_blocks;
Chris Mason5caf2a02007-04-02 11:20:42 -040035 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -040036 0, 1);
Chris Masona28ec192007-03-06 20:08:01 -050037 if (ret != 0)
38 BUG();
Chris Mason02217ed2007-03-02 16:08:05 -050039 BUG_ON(ret != 0);
Chris Mason5caf2a02007-04-02 11:20:42 -040040 l = btrfs_buffer_leaf(path->nodes[0]);
41 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Masoncf27e1e2007-03-13 09:49:06 -040042 refs = btrfs_extent_refs(item);
43 btrfs_set_extent_refs(item, refs + 1);
Chris Mason5caf2a02007-04-02 11:20:42 -040044 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masona28ec192007-03-06 20:08:01 -050045
Chris Mason5caf2a02007-04-02 11:20:42 -040046 btrfs_release_path(root->fs_info->extent_root, path);
47 btrfs_free_path(path);
Chris Mason9f5fae22007-03-20 14:38:32 -040048 finish_current_insert(trans, root->fs_info->extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -040049 del_pending_extents(trans, root->fs_info->extent_root);
Chris Mason02217ed2007-03-02 16:08:05 -050050 return 0;
51}
52
Chris Masone089f052007-03-16 16:20:31 -040053static int lookup_block_ref(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason6407bf62007-03-27 06:33:00 -040054 *root, u64 blocknr, u64 num_blocks, u32 *refs)
Chris Masona28ec192007-03-06 20:08:01 -050055{
Chris Mason5caf2a02007-04-02 11:20:42 -040056 struct btrfs_path *path;
Chris Masona28ec192007-03-06 20:08:01 -050057 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -040058 struct btrfs_key key;
Chris Mason234b63a2007-03-13 10:46:10 -040059 struct btrfs_leaf *l;
60 struct btrfs_extent_item *item;
Chris Mason5caf2a02007-04-02 11:20:42 -040061
62 path = btrfs_alloc_path();
63 btrfs_init_path(path);
Chris Masona28ec192007-03-06 20:08:01 -050064 key.objectid = blocknr;
Chris Mason6407bf62007-03-27 06:33:00 -040065 key.offset = num_blocks;
Chris Mason62e27492007-03-15 12:56:47 -040066 key.flags = 0;
67 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Mason5caf2a02007-04-02 11:20:42 -040068 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -040069 0, 0);
Chris Masona28ec192007-03-06 20:08:01 -050070 if (ret != 0)
71 BUG();
Chris Mason5caf2a02007-04-02 11:20:42 -040072 l = btrfs_buffer_leaf(path->nodes[0]);
73 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Masoncf27e1e2007-03-13 09:49:06 -040074 *refs = btrfs_extent_refs(item);
Chris Mason5caf2a02007-04-02 11:20:42 -040075 btrfs_release_path(root->fs_info->extent_root, path);
76 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -050077 return 0;
78}
79
Chris Masonc5739bb2007-04-10 09:27:04 -040080int btrfs_inc_root_ref(struct btrfs_trans_handle *trans,
81 struct btrfs_root *root)
82{
Chris Mason7eccb902007-04-11 15:53:25 -040083 return inc_block_ref(trans, root, bh_blocknr(root->node), 1);
Chris Masonc5739bb2007-04-10 09:27:04 -040084}
85
Chris Masone089f052007-03-16 16:20:31 -040086int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Masone20d96d2007-03-22 12:13:20 -040087 struct buffer_head *buf)
Chris Mason02217ed2007-03-02 16:08:05 -050088{
89 u64 blocknr;
Chris Masone20d96d2007-03-22 12:13:20 -040090 struct btrfs_node *buf_node;
Chris Mason6407bf62007-03-27 06:33:00 -040091 struct btrfs_leaf *buf_leaf;
92 struct btrfs_disk_key *key;
93 struct btrfs_file_extent_item *fi;
Chris Mason02217ed2007-03-02 16:08:05 -050094 int i;
Chris Mason6407bf62007-03-27 06:33:00 -040095 int leaf;
96 int ret;
Chris Masona28ec192007-03-06 20:08:01 -050097
Chris Mason3768f362007-03-13 16:47:54 -040098 if (!root->ref_cows)
Chris Masona28ec192007-03-06 20:08:01 -050099 return 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400100 buf_node = btrfs_buffer_node(buf);
Chris Mason6407bf62007-03-27 06:33:00 -0400101 leaf = btrfs_is_leaf(buf_node);
102 buf_leaf = btrfs_buffer_leaf(buf);
Chris Masone20d96d2007-03-22 12:13:20 -0400103 for (i = 0; i < btrfs_header_nritems(&buf_node->header); i++) {
Chris Mason6407bf62007-03-27 06:33:00 -0400104 if (leaf) {
105 key = &buf_leaf->items[i].key;
106 if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY)
107 continue;
108 fi = btrfs_item_ptr(buf_leaf, i,
109 struct btrfs_file_extent_item);
110 ret = inc_block_ref(trans, root,
111 btrfs_file_extent_disk_blocknr(fi),
112 btrfs_file_extent_disk_num_blocks(fi));
113 BUG_ON(ret);
114 } else {
115 blocknr = btrfs_node_blockptr(buf_node, i);
116 ret = inc_block_ref(trans, root, blocknr, 1);
117 BUG_ON(ret);
118 }
Chris Mason02217ed2007-03-02 16:08:05 -0500119 }
120 return 0;
121}
122
Chris Masone089f052007-03-16 16:20:31 -0400123int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, struct
124 btrfs_root *root)
Chris Masona28ec192007-03-06 20:08:01 -0500125{
Chris Mason8ef97622007-03-26 10:15:30 -0400126 unsigned long gang[8];
Chris Mason88fd1462007-03-16 08:56:18 -0400127 u64 first = 0;
Chris Masona28ec192007-03-06 20:08:01 -0500128 int ret;
129 int i;
Chris Mason8ef97622007-03-26 10:15:30 -0400130 struct radix_tree_root *pinned_radix = &root->fs_info->pinned_radix;
Chris Masona28ec192007-03-06 20:08:01 -0500131
132 while(1) {
Chris Mason8ef97622007-03-26 10:15:30 -0400133 ret = find_first_radix_bit(pinned_radix, gang,
134 ARRAY_SIZE(gang));
Chris Masona28ec192007-03-06 20:08:01 -0500135 if (!ret)
136 break;
Chris Mason88fd1462007-03-16 08:56:18 -0400137 if (!first)
Chris Mason8ef97622007-03-26 10:15:30 -0400138 first = gang[0];
Chris Mason0579da42007-03-07 16:15:30 -0500139 for (i = 0; i < ret; i++) {
Chris Mason8ef97622007-03-26 10:15:30 -0400140 clear_radix_bit(pinned_radix, gang[i]);
Chris Mason0579da42007-03-07 16:15:30 -0500141 }
Chris Masona28ec192007-03-06 20:08:01 -0500142 }
Chris Masond5719762007-03-23 10:01:08 -0400143 if (root->fs_info->last_insert.objectid > first)
144 root->fs_info->last_insert.objectid = first;
Chris Mason9f5fae22007-03-20 14:38:32 -0400145 root->fs_info->last_insert.offset = 0;
Chris Masona28ec192007-03-06 20:08:01 -0500146 return 0;
147}
148
Chris Masone089f052007-03-16 16:20:31 -0400149static int finish_current_insert(struct btrfs_trans_handle *trans, struct
150 btrfs_root *extent_root)
Chris Mason037e6392007-03-07 11:50:24 -0500151{
Chris Masone2fa7222007-03-12 16:22:34 -0400152 struct btrfs_key ins;
Chris Mason234b63a2007-03-13 10:46:10 -0400153 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -0500154 int i;
155 int ret;
Chris Mason1261ec42007-03-20 20:35:03 -0400156 u64 super_blocks_used;
157 struct btrfs_fs_info *info = extent_root->fs_info;
Chris Mason037e6392007-03-07 11:50:24 -0500158
Chris Masoncf27e1e2007-03-13 09:49:06 -0400159 btrfs_set_extent_refs(&extent_item, 1);
Chris Mason037e6392007-03-07 11:50:24 -0500160 ins.offset = 1;
161 ins.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -0400162 btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
Chris Mason037e6392007-03-07 11:50:24 -0500163
Chris Mason9f5fae22007-03-20 14:38:32 -0400164 for (i = 0; i < extent_root->fs_info->current_insert.flags; i++) {
165 ins.objectid = extent_root->fs_info->current_insert.objectid +
166 i;
Chris Mason1261ec42007-03-20 20:35:03 -0400167 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
168 btrfs_set_super_blocks_used(info->disk_super,
169 super_blocks_used + 1);
Chris Masone089f052007-03-16 16:20:31 -0400170 ret = btrfs_insert_item(trans, extent_root, &ins, &extent_item,
171 sizeof(extent_item));
Chris Mason037e6392007-03-07 11:50:24 -0500172 BUG_ON(ret);
173 }
Chris Mason9f5fae22007-03-20 14:38:32 -0400174 extent_root->fs_info->current_insert.offset = 0;
Chris Mason037e6392007-03-07 11:50:24 -0500175 return 0;
176}
177
Chris Mason8ef97622007-03-26 10:15:30 -0400178static int pin_down_block(struct btrfs_root *root, u64 blocknr, int pending)
Chris Masone20d96d2007-03-22 12:13:20 -0400179{
180 int err;
Chris Mason78fae272007-03-25 11:35:08 -0400181 struct btrfs_header *header;
Chris Mason8ef97622007-03-26 10:15:30 -0400182 struct buffer_head *bh;
Chris Mason78fae272007-03-25 11:35:08 -0400183
Chris Masonf4b9aa82007-03-27 11:05:53 -0400184 if (!pending) {
Chris Masond98237b2007-03-28 13:57:48 -0400185 bh = btrfs_find_tree_block(root, blocknr);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400186 if (bh) {
187 if (buffer_uptodate(bh)) {
188 u64 transid =
189 root->fs_info->running_transaction->transid;
190 header = btrfs_buffer_header(bh);
191 if (btrfs_header_generation(header) ==
192 transid) {
193 btrfs_block_release(root, bh);
194 return 0;
195 }
Chris Masonf4b9aa82007-03-27 11:05:53 -0400196 }
Chris Masond6025572007-03-30 14:27:56 -0400197 btrfs_block_release(root, bh);
Chris Mason8ef97622007-03-26 10:15:30 -0400198 }
Chris Mason8ef97622007-03-26 10:15:30 -0400199 err = set_radix_bit(&root->fs_info->pinned_radix, blocknr);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400200 } else {
201 err = set_radix_bit(&root->fs_info->pending_del_radix, blocknr);
202 }
Chris Mason8ef97622007-03-26 10:15:30 -0400203 BUG_ON(err);
Chris Masone20d96d2007-03-22 12:13:20 -0400204 return 0;
205}
206
Chris Masona28ec192007-03-06 20:08:01 -0500207/*
208 * remove an extent from the root, returns 0 on success
209 */
Chris Masone089f052007-03-16 16:20:31 -0400210static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason78fae272007-03-25 11:35:08 -0400211 *root, u64 blocknr, u64 num_blocks, int pin)
Chris Masona28ec192007-03-06 20:08:01 -0500212{
Chris Mason5caf2a02007-04-02 11:20:42 -0400213 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -0400214 struct btrfs_key key;
Chris Mason1261ec42007-03-20 20:35:03 -0400215 struct btrfs_fs_info *info = root->fs_info;
216 struct btrfs_root *extent_root = info->extent_root;
Chris Masona28ec192007-03-06 20:08:01 -0500217 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400218 struct btrfs_extent_item *ei;
Chris Masone2fa7222007-03-12 16:22:34 -0400219 struct btrfs_key ins;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400220 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500221
Chris Masona28ec192007-03-06 20:08:01 -0500222 key.objectid = blocknr;
223 key.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -0400224 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masona28ec192007-03-06 20:08:01 -0500225 key.offset = num_blocks;
226
Chris Masone089f052007-03-16 16:20:31 -0400227 find_free_extent(trans, root, 0, 0, (u64)-1, &ins);
Chris Mason5caf2a02007-04-02 11:20:42 -0400228 path = btrfs_alloc_path();
229 BUG_ON(!path);
230 btrfs_init_path(path);
Chris Mason5f26f772007-04-05 10:38:44 -0400231
Chris Mason5caf2a02007-04-02 11:20:42 -0400232 ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
Chris Masona28ec192007-03-06 20:08:01 -0500233 if (ret) {
Chris Mason2e635a22007-03-21 11:12:56 -0400234 printk("failed to find %Lu\n", key.objectid);
Chris Mason234b63a2007-03-13 10:46:10 -0400235 btrfs_print_tree(extent_root, extent_root->node);
Chris Mason2e635a22007-03-21 11:12:56 -0400236 printk("failed to find %Lu\n", key.objectid);
Chris Masona28ec192007-03-06 20:08:01 -0500237 BUG();
238 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400239 ei = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
Chris Mason123abc82007-03-14 14:14:43 -0400240 struct btrfs_extent_item);
Chris Masona28ec192007-03-06 20:08:01 -0500241 BUG_ON(ei->refs == 0);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400242 refs = btrfs_extent_refs(ei) - 1;
243 btrfs_set_extent_refs(ei, refs);
Chris Mason5caf2a02007-04-02 11:20:42 -0400244 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400245 if (refs == 0) {
Chris Mason1261ec42007-03-20 20:35:03 -0400246 u64 super_blocks_used;
Chris Mason78fae272007-03-25 11:35:08 -0400247
248 if (pin) {
Chris Mason8ef97622007-03-26 10:15:30 -0400249 ret = pin_down_block(root, blocknr, 0);
Chris Mason78fae272007-03-25 11:35:08 -0400250 BUG_ON(ret);
251 }
252
Chris Mason1261ec42007-03-20 20:35:03 -0400253 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
254 btrfs_set_super_blocks_used(info->disk_super,
255 super_blocks_used - num_blocks);
Chris Mason5caf2a02007-04-02 11:20:42 -0400256 ret = btrfs_del_item(trans, extent_root, path);
Chris Masona28ec192007-03-06 20:08:01 -0500257 if (ret)
258 BUG();
259 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400260 btrfs_release_path(extent_root, path);
261 btrfs_free_path(path);
Chris Masone089f052007-03-16 16:20:31 -0400262 finish_current_insert(trans, extent_root);
Chris Masona28ec192007-03-06 20:08:01 -0500263 return ret;
264}
265
266/*
Chris Masonfec577f2007-02-26 10:40:21 -0500267 * find all the blocks marked as pending in the radix tree and remove
268 * them from the extent map
269 */
Chris Masone089f052007-03-16 16:20:31 -0400270static int del_pending_extents(struct btrfs_trans_handle *trans, struct
271 btrfs_root *extent_root)
Chris Masonfec577f2007-02-26 10:40:21 -0500272{
273 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -0400274 int wret;
275 int err = 0;
Chris Mason8ef97622007-03-26 10:15:30 -0400276 unsigned long gang[4];
Chris Masonfec577f2007-02-26 10:40:21 -0500277 int i;
Chris Mason8ef97622007-03-26 10:15:30 -0400278 struct radix_tree_root *pending_radix;
279 struct radix_tree_root *pinned_radix;
280
281 pending_radix = &extent_root->fs_info->pending_del_radix;
282 pinned_radix = &extent_root->fs_info->pinned_radix;
Chris Masonfec577f2007-02-26 10:40:21 -0500283
284 while(1) {
Chris Mason8ef97622007-03-26 10:15:30 -0400285 ret = find_first_radix_bit(pending_radix, gang,
286 ARRAY_SIZE(gang));
Chris Masonfec577f2007-02-26 10:40:21 -0500287 if (!ret)
288 break;
289 for (i = 0; i < ret; i++) {
Chris Mason8ef97622007-03-26 10:15:30 -0400290 wret = set_radix_bit(pinned_radix, gang[i]);
291 BUG_ON(wret);
292 wret = clear_radix_bit(pending_radix, gang[i]);
293 BUG_ON(wret);
Chris Masond5719762007-03-23 10:01:08 -0400294 wret = __free_extent(trans, extent_root,
Chris Mason8ef97622007-03-26 10:15:30 -0400295 gang[i], 1, 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400296 if (wret)
297 err = wret;
Chris Masonfec577f2007-02-26 10:40:21 -0500298 }
299 }
Chris Masone20d96d2007-03-22 12:13:20 -0400300 return err;
Chris Masonfec577f2007-02-26 10:40:21 -0500301}
302
303/*
304 * remove an extent from the root, returns 0 on success
305 */
Chris Masone089f052007-03-16 16:20:31 -0400306int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
307 *root, u64 blocknr, u64 num_blocks, int pin)
Chris Masonfec577f2007-02-26 10:40:21 -0500308{
Chris Mason9f5fae22007-03-20 14:38:32 -0400309 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Masonfec577f2007-02-26 10:40:21 -0500310 int pending_ret;
311 int ret;
Chris Masona28ec192007-03-06 20:08:01 -0500312
313 if (root == extent_root) {
Chris Mason8ef97622007-03-26 10:15:30 -0400314 pin_down_block(root, blocknr, 1);
Chris Masona28ec192007-03-06 20:08:01 -0500315 return 0;
316 }
Chris Mason78fae272007-03-25 11:35:08 -0400317 ret = __free_extent(trans, root, blocknr, num_blocks, pin);
Chris Masone20d96d2007-03-22 12:13:20 -0400318 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
Chris Masonfec577f2007-02-26 10:40:21 -0500319 return ret ? ret : pending_ret;
320}
321
322/*
323 * walks the btree of allocated extents and find a hole of a given size.
324 * The key ins is changed to record the hole:
325 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -0400326 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -0500327 * ins->offset == number of blocks
328 * Any available blocks before search_start are skipped.
329 */
Chris Masone089f052007-03-16 16:20:31 -0400330static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
331 *orig_root, u64 num_blocks, u64 search_start, u64
332 search_end, struct btrfs_key *ins)
Chris Masonfec577f2007-02-26 10:40:21 -0500333{
Chris Mason5caf2a02007-04-02 11:20:42 -0400334 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -0400335 struct btrfs_key key;
Chris Masonfec577f2007-02-26 10:40:21 -0500336 int ret;
337 u64 hole_size = 0;
338 int slot = 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400339 u64 last_block = 0;
Chris Mason037e6392007-03-07 11:50:24 -0500340 u64 test_block;
Chris Masonfec577f2007-02-26 10:40:21 -0500341 int start_found;
Chris Mason234b63a2007-03-13 10:46:10 -0400342 struct btrfs_leaf *l;
Chris Mason9f5fae22007-03-20 14:38:32 -0400343 struct btrfs_root * root = orig_root->fs_info->extent_root;
Chris Mason0579da42007-03-07 16:15:30 -0500344 int total_needed = num_blocks;
Chris Masone20d96d2007-03-22 12:13:20 -0400345 int level;
Chris Masonfec577f2007-02-26 10:40:21 -0500346
Chris Masonb1a4d962007-04-04 15:27:52 -0400347 path = btrfs_alloc_path();
348 ins->flags = 0;
349 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
350
Chris Masone20d96d2007-03-22 12:13:20 -0400351 level = btrfs_header_level(btrfs_buffer_header(root->node));
352 total_needed += (level + 1) * 3;
Chris Masonb1a4d962007-04-04 15:27:52 -0400353 if (root->fs_info->last_insert.objectid == 0 && search_end == (u64)-1) {
354 struct btrfs_disk_key *last_key;
355 btrfs_init_path(path);
356 ins->objectid = (u64)-1;
357 ins->offset = (u64)-1;
358 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
359 if (ret < 0)
360 goto error;
361 BUG_ON(ret == 0);
362 if (path->slots[0] > 0)
363 path->slots[0]--;
364 l = btrfs_buffer_leaf(path->nodes[0]);
365 last_key = &l->items[path->slots[0]].key;
366 search_start = btrfs_disk_key_objectid(last_key);
367 }
Chris Mason9f5fae22007-03-20 14:38:32 -0400368 if (root->fs_info->last_insert.objectid > search_start)
369 search_start = root->fs_info->last_insert.objectid;
Chris Mason62e27492007-03-15 12:56:47 -0400370
Chris Masonfec577f2007-02-26 10:40:21 -0500371check_failed:
Chris Mason5caf2a02007-04-02 11:20:42 -0400372 btrfs_init_path(path);
Chris Masonfec577f2007-02-26 10:40:21 -0500373 ins->objectid = search_start;
374 ins->offset = 0;
Chris Masonfec577f2007-02-26 10:40:21 -0500375 start_found = 0;
Chris Mason5caf2a02007-04-02 11:20:42 -0400376 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
Chris Mason0f70abe2007-02-28 16:46:22 -0500377 if (ret < 0)
378 goto error;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500379
Chris Mason5caf2a02007-04-02 11:20:42 -0400380 if (path->slots[0] > 0)
381 path->slots[0]--;
Chris Mason0579da42007-03-07 16:15:30 -0500382
Chris Masonfec577f2007-02-26 10:40:21 -0500383 while (1) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400384 l = btrfs_buffer_leaf(path->nodes[0]);
385 slot = path->slots[0];
Chris Mason7518a232007-03-12 12:01:18 -0400386 if (slot >= btrfs_header_nritems(&l->header)) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400387 ret = btrfs_next_leaf(root, path);
Chris Masonfec577f2007-02-26 10:40:21 -0500388 if (ret == 0)
389 continue;
Chris Mason0f70abe2007-02-28 16:46:22 -0500390 if (ret < 0)
391 goto error;
Chris Masonfec577f2007-02-26 10:40:21 -0500392 if (!start_found) {
393 ins->objectid = search_start;
Chris Mason037e6392007-03-07 11:50:24 -0500394 ins->offset = (u64)-1;
Chris Masonfec577f2007-02-26 10:40:21 -0500395 start_found = 1;
396 goto check_pending;
397 }
398 ins->objectid = last_block > search_start ?
399 last_block : search_start;
Chris Mason037e6392007-03-07 11:50:24 -0500400 ins->offset = (u64)-1;
Chris Masonfec577f2007-02-26 10:40:21 -0500401 goto check_pending;
402 }
Chris Masone2fa7222007-03-12 16:22:34 -0400403 btrfs_disk_key_to_cpu(&key, &l->items[slot].key);
404 if (key.objectid >= search_start) {
Chris Masonfec577f2007-02-26 10:40:21 -0500405 if (start_found) {
Chris Mason0579da42007-03-07 16:15:30 -0500406 if (last_block < search_start)
407 last_block = search_start;
Chris Masone2fa7222007-03-12 16:22:34 -0400408 hole_size = key.objectid - last_block;
Chris Mason037e6392007-03-07 11:50:24 -0500409 if (hole_size > total_needed) {
Chris Masonfec577f2007-02-26 10:40:21 -0500410 ins->objectid = last_block;
Chris Mason037e6392007-03-07 11:50:24 -0500411 ins->offset = hole_size;
Chris Masonfec577f2007-02-26 10:40:21 -0500412 goto check_pending;
413 }
Chris Mason0579da42007-03-07 16:15:30 -0500414 }
Chris Masonfec577f2007-02-26 10:40:21 -0500415 }
Chris Mason0579da42007-03-07 16:15:30 -0500416 start_found = 1;
Chris Masone2fa7222007-03-12 16:22:34 -0400417 last_block = key.objectid + key.offset;
Chris Mason5caf2a02007-04-02 11:20:42 -0400418 path->slots[0]++;
Chris Masonfec577f2007-02-26 10:40:21 -0500419 }
420 // FIXME -ENOSPC
421check_pending:
422 /* we have to make sure we didn't find an extent that has already
423 * been allocated by the map tree or the original allocation
424 */
Chris Mason5caf2a02007-04-02 11:20:42 -0400425 btrfs_release_path(root, path);
Chris Masonfec577f2007-02-26 10:40:21 -0500426 BUG_ON(ins->objectid < search_start);
Chris Mason037e6392007-03-07 11:50:24 -0500427 for (test_block = ins->objectid;
428 test_block < ins->objectid + total_needed; test_block++) {
Chris Mason8ef97622007-03-26 10:15:30 -0400429 if (test_radix_bit(&root->fs_info->pinned_radix,
Chris Mason9f5fae22007-03-20 14:38:32 -0400430 test_block)) {
Chris Mason037e6392007-03-07 11:50:24 -0500431 search_start = test_block + 1;
Chris Masonfec577f2007-02-26 10:40:21 -0500432 goto check_failed;
433 }
434 }
Chris Mason9f5fae22007-03-20 14:38:32 -0400435 BUG_ON(root->fs_info->current_insert.offset);
436 root->fs_info->current_insert.offset = total_needed - num_blocks;
437 root->fs_info->current_insert.objectid = ins->objectid + num_blocks;
438 root->fs_info->current_insert.flags = 0;
439 root->fs_info->last_insert.objectid = ins->objectid;
Chris Mason037e6392007-03-07 11:50:24 -0500440 ins->offset = num_blocks;
Chris Mason5caf2a02007-04-02 11:20:42 -0400441 btrfs_free_path(path);
Chris Masonfec577f2007-02-26 10:40:21 -0500442 return 0;
Chris Mason0f70abe2007-02-28 16:46:22 -0500443error:
Chris Mason5caf2a02007-04-02 11:20:42 -0400444 btrfs_release_path(root, path);
445 btrfs_free_path(path);
Chris Mason0f70abe2007-02-28 16:46:22 -0500446 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -0500447}
448
449/*
Chris Masonfec577f2007-02-26 10:40:21 -0500450 * finds a free extent and does all the dirty work required for allocation
451 * returns the key for the extent through ins, and a tree buffer for
452 * the first block of the extent through buf.
453 *
454 * returns 0 if everything worked, non-zero otherwise.
455 */
Chris Masondee26a92007-03-26 16:00:06 -0400456int btrfs_alloc_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masone089f052007-03-16 16:20:31 -0400457 *root, u64 num_blocks, u64 search_start, u64
Chris Masond0dbc6242007-04-10 12:36:36 -0400458 search_end, struct btrfs_key *ins)
Chris Masonfec577f2007-02-26 10:40:21 -0500459{
460 int ret;
461 int pending_ret;
Chris Mason1261ec42007-03-20 20:35:03 -0400462 u64 super_blocks_used;
463 struct btrfs_fs_info *info = root->fs_info;
464 struct btrfs_root *extent_root = info->extent_root;
Chris Mason234b63a2007-03-13 10:46:10 -0400465 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -0500466
Chris Masoncf27e1e2007-03-13 09:49:06 -0400467 btrfs_set_extent_refs(&extent_item, 1);
Chris Masonfec577f2007-02-26 10:40:21 -0500468
Chris Mason037e6392007-03-07 11:50:24 -0500469 if (root == extent_root) {
Chris Mason9f5fae22007-03-20 14:38:32 -0400470 BUG_ON(extent_root->fs_info->current_insert.offset == 0);
Chris Mason037e6392007-03-07 11:50:24 -0500471 BUG_ON(num_blocks != 1);
Chris Mason9f5fae22007-03-20 14:38:32 -0400472 BUG_ON(extent_root->fs_info->current_insert.flags ==
473 extent_root->fs_info->current_insert.offset);
Chris Mason037e6392007-03-07 11:50:24 -0500474 ins->offset = 1;
Chris Mason9f5fae22007-03-20 14:38:32 -0400475 ins->objectid = extent_root->fs_info->current_insert.objectid +
476 extent_root->fs_info->current_insert.flags++;
Chris Masonfec577f2007-02-26 10:40:21 -0500477 return 0;
478 }
Chris Masone089f052007-03-16 16:20:31 -0400479 ret = find_free_extent(trans, root, num_blocks, search_start,
Chris Mason037e6392007-03-07 11:50:24 -0500480 search_end, ins);
481 if (ret)
482 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -0500483
Chris Mason1261ec42007-03-20 20:35:03 -0400484 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
485 btrfs_set_super_blocks_used(info->disk_super, super_blocks_used +
486 num_blocks);
Chris Masone089f052007-03-16 16:20:31 -0400487 ret = btrfs_insert_item(trans, extent_root, ins, &extent_item,
488 sizeof(extent_item));
Chris Mason037e6392007-03-07 11:50:24 -0500489
Chris Masone089f052007-03-16 16:20:31 -0400490 finish_current_insert(trans, extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -0400491 pending_ret = del_pending_extents(trans, extent_root);
Chris Mason037e6392007-03-07 11:50:24 -0500492 if (ret)
493 return ret;
494 if (pending_ret)
495 return pending_ret;
496 return 0;
Chris Masonfec577f2007-02-26 10:40:21 -0500497}
498
499/*
500 * helper function to allocate a block for a given tree
501 * returns the tree buffer or NULL.
502 */
Chris Masone20d96d2007-03-22 12:13:20 -0400503struct buffer_head *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Chris Masone089f052007-03-16 16:20:31 -0400504 struct btrfs_root *root)
Chris Masonfec577f2007-02-26 10:40:21 -0500505{
Chris Masone2fa7222007-03-12 16:22:34 -0400506 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -0500507 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -0400508 struct buffer_head *buf;
Chris Masonfec577f2007-02-26 10:40:21 -0500509
Chris Masond0dbc6242007-04-10 12:36:36 -0400510 ret = btrfs_alloc_extent(trans, root, 1, 0, (unsigned long)-1, &ins);
Chris Masonfec577f2007-02-26 10:40:21 -0500511 if (ret) {
512 BUG();
513 return NULL;
514 }
Chris Masond98237b2007-03-28 13:57:48 -0400515 buf = btrfs_find_create_tree_block(root, ins.objectid);
Chris Masondf2ce342007-03-23 11:00:45 -0400516 set_buffer_uptodate(buf);
Chris Masonfec577f2007-02-26 10:40:21 -0500517 return buf;
518}
Chris Masona28ec192007-03-06 20:08:01 -0500519
Chris Mason6407bf62007-03-27 06:33:00 -0400520static int drop_leaf_ref(struct btrfs_trans_handle *trans,
521 struct btrfs_root *root, struct buffer_head *cur)
522{
523 struct btrfs_disk_key *key;
524 struct btrfs_leaf *leaf;
525 struct btrfs_file_extent_item *fi;
526 int i;
527 int nritems;
528 int ret;
529
530 BUG_ON(!btrfs_is_leaf(btrfs_buffer_node(cur)));
531 leaf = btrfs_buffer_leaf(cur);
532 nritems = btrfs_header_nritems(&leaf->header);
533 for (i = 0; i < nritems; i++) {
534 key = &leaf->items[i].key;
535 if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY)
536 continue;
537 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
538 /*
539 * FIXME make sure to insert a trans record that
540 * repeats the snapshot del on crash
541 */
542 ret = btrfs_free_extent(trans, root,
543 btrfs_file_extent_disk_blocknr(fi),
544 btrfs_file_extent_disk_num_blocks(fi),
545 0);
546 BUG_ON(ret);
547 }
548 return 0;
549}
550
Chris Mason9aca1d52007-03-13 11:09:37 -0400551/*
552 * helper function for drop_snapshot, this walks down the tree dropping ref
553 * counts as it goes.
554 */
Chris Masone089f052007-03-16 16:20:31 -0400555static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
556 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -0500557{
Chris Masone20d96d2007-03-22 12:13:20 -0400558 struct buffer_head *next;
559 struct buffer_head *cur;
Chris Mason20524f02007-03-10 06:35:47 -0500560 u64 blocknr;
561 int ret;
562 u32 refs;
563
Chris Mason5caf2a02007-04-02 11:20:42 -0400564 WARN_ON(*level < 0);
565 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason7eccb902007-04-11 15:53:25 -0400566 ret = lookup_block_ref(trans, root, bh_blocknr(path->nodes[*level]),
Chris Mason6407bf62007-03-27 06:33:00 -0400567 1, &refs);
Chris Mason20524f02007-03-10 06:35:47 -0500568 BUG_ON(ret);
569 if (refs > 1)
570 goto out;
Chris Mason9aca1d52007-03-13 11:09:37 -0400571 /*
572 * walk down to the last node level and free all the leaves
573 */
Chris Mason6407bf62007-03-27 06:33:00 -0400574 while(*level >= 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400575 WARN_ON(*level < 0);
576 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason20524f02007-03-10 06:35:47 -0500577 cur = path->nodes[*level];
Chris Mason2c90e5d2007-04-02 10:50:19 -0400578 if (btrfs_header_level(btrfs_buffer_header(cur)) != *level)
579 WARN_ON(1);
Chris Mason7518a232007-03-12 12:01:18 -0400580 if (path->slots[*level] >=
Chris Masone20d96d2007-03-22 12:13:20 -0400581 btrfs_header_nritems(btrfs_buffer_header(cur)))
Chris Mason20524f02007-03-10 06:35:47 -0500582 break;
Chris Mason6407bf62007-03-27 06:33:00 -0400583 if (*level == 0) {
584 ret = drop_leaf_ref(trans, root, cur);
585 BUG_ON(ret);
586 break;
587 }
Chris Masone20d96d2007-03-22 12:13:20 -0400588 blocknr = btrfs_node_blockptr(btrfs_buffer_node(cur),
589 path->slots[*level]);
Chris Mason6407bf62007-03-27 06:33:00 -0400590 ret = lookup_block_ref(trans, root, blocknr, 1, &refs);
591 BUG_ON(ret);
592 if (refs != 1) {
Chris Mason20524f02007-03-10 06:35:47 -0500593 path->slots[*level]++;
Chris Masone089f052007-03-16 16:20:31 -0400594 ret = btrfs_free_extent(trans, root, blocknr, 1, 1);
Chris Mason20524f02007-03-10 06:35:47 -0500595 BUG_ON(ret);
596 continue;
597 }
Chris Mason20524f02007-03-10 06:35:47 -0500598 next = read_tree_block(root, blocknr);
Chris Mason5caf2a02007-04-02 11:20:42 -0400599 WARN_ON(*level <= 0);
Chris Mason83e15a22007-03-12 09:03:27 -0400600 if (path->nodes[*level-1])
Chris Mason234b63a2007-03-13 10:46:10 -0400601 btrfs_block_release(root, path->nodes[*level-1]);
Chris Mason20524f02007-03-10 06:35:47 -0500602 path->nodes[*level-1] = next;
Chris Masone20d96d2007-03-22 12:13:20 -0400603 *level = btrfs_header_level(btrfs_buffer_header(next));
Chris Mason20524f02007-03-10 06:35:47 -0500604 path->slots[*level] = 0;
605 }
606out:
Chris Mason5caf2a02007-04-02 11:20:42 -0400607 WARN_ON(*level < 0);
608 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason6407bf62007-03-27 06:33:00 -0400609 ret = btrfs_free_extent(trans, root,
Chris Mason7eccb902007-04-11 15:53:25 -0400610 bh_blocknr(path->nodes[*level]), 1, 1);
Chris Mason234b63a2007-03-13 10:46:10 -0400611 btrfs_block_release(root, path->nodes[*level]);
Chris Mason20524f02007-03-10 06:35:47 -0500612 path->nodes[*level] = NULL;
613 *level += 1;
614 BUG_ON(ret);
615 return 0;
616}
617
Chris Mason9aca1d52007-03-13 11:09:37 -0400618/*
619 * helper for dropping snapshots. This walks back up the tree in the path
620 * to find the first node higher up where we haven't yet gone through
621 * all the slots
622 */
Chris Masone089f052007-03-16 16:20:31 -0400623static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root
624 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -0500625{
626 int i;
627 int slot;
628 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400629 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Mason20524f02007-03-10 06:35:47 -0500630 slot = path->slots[i];
Chris Masone20d96d2007-03-22 12:13:20 -0400631 if (slot < btrfs_header_nritems(
632 btrfs_buffer_header(path->nodes[i])) - 1) {
Chris Mason20524f02007-03-10 06:35:47 -0500633 path->slots[i]++;
634 *level = i;
635 return 0;
636 } else {
Chris Masone089f052007-03-16 16:20:31 -0400637 ret = btrfs_free_extent(trans, root,
Chris Mason7eccb902007-04-11 15:53:25 -0400638 bh_blocknr(path->nodes[*level]),
Chris Masone089f052007-03-16 16:20:31 -0400639 1, 1);
Chris Mason6407bf62007-03-27 06:33:00 -0400640 BUG_ON(ret);
Chris Mason234b63a2007-03-13 10:46:10 -0400641 btrfs_block_release(root, path->nodes[*level]);
Chris Mason83e15a22007-03-12 09:03:27 -0400642 path->nodes[*level] = NULL;
Chris Mason20524f02007-03-10 06:35:47 -0500643 *level = i + 1;
Chris Mason20524f02007-03-10 06:35:47 -0500644 }
645 }
646 return 1;
647}
648
Chris Mason9aca1d52007-03-13 11:09:37 -0400649/*
650 * drop the reference count on the tree rooted at 'snap'. This traverses
651 * the tree freeing any blocks that have a ref count of zero after being
652 * decremented.
653 */
Chris Masone089f052007-03-16 16:20:31 -0400654int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masone20d96d2007-03-22 12:13:20 -0400655 *root, struct buffer_head *snap)
Chris Mason20524f02007-03-10 06:35:47 -0500656{
Chris Mason3768f362007-03-13 16:47:54 -0400657 int ret = 0;
Chris Mason9aca1d52007-03-13 11:09:37 -0400658 int wret;
Chris Mason20524f02007-03-10 06:35:47 -0500659 int level;
Chris Mason5caf2a02007-04-02 11:20:42 -0400660 struct btrfs_path *path;
Chris Mason20524f02007-03-10 06:35:47 -0500661 int i;
662 int orig_level;
663
Chris Mason5caf2a02007-04-02 11:20:42 -0400664 path = btrfs_alloc_path();
665 BUG_ON(!path);
666 btrfs_init_path(path);
Chris Mason20524f02007-03-10 06:35:47 -0500667
Chris Masone20d96d2007-03-22 12:13:20 -0400668 level = btrfs_header_level(btrfs_buffer_header(snap));
Chris Mason20524f02007-03-10 06:35:47 -0500669 orig_level = level;
Chris Mason5caf2a02007-04-02 11:20:42 -0400670 path->nodes[level] = snap;
671 path->slots[level] = 0;
Chris Mason20524f02007-03-10 06:35:47 -0500672 while(1) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400673 wret = walk_down_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -0400674 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -0500675 break;
Chris Mason9aca1d52007-03-13 11:09:37 -0400676 if (wret < 0)
677 ret = wret;
678
Chris Mason5caf2a02007-04-02 11:20:42 -0400679 wret = walk_up_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -0400680 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -0500681 break;
Chris Mason9aca1d52007-03-13 11:09:37 -0400682 if (wret < 0)
683 ret = wret;
Chris Mason20524f02007-03-10 06:35:47 -0500684 }
Chris Mason83e15a22007-03-12 09:03:27 -0400685 for (i = 0; i <= orig_level; i++) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400686 if (path->nodes[i]) {
687 btrfs_block_release(root, path->nodes[i]);
Chris Mason83e15a22007-03-12 09:03:27 -0400688 }
Chris Mason20524f02007-03-10 06:35:47 -0500689 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400690 btrfs_free_path(path);
Chris Mason9aca1d52007-03-13 11:09:37 -0400691 return ret;
Chris Mason20524f02007-03-10 06:35:47 -0500692}