blob: cd40a08dd8a666af9b0a77bb7e2cf17e16101544 [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 Masone089f052007-03-16 16:20:31 -040080int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Masone20d96d2007-03-22 12:13:20 -040081 struct buffer_head *buf)
Chris Mason02217ed2007-03-02 16:08:05 -050082{
83 u64 blocknr;
Chris Masone20d96d2007-03-22 12:13:20 -040084 struct btrfs_node *buf_node;
Chris Mason6407bf62007-03-27 06:33:00 -040085 struct btrfs_leaf *buf_leaf;
86 struct btrfs_disk_key *key;
87 struct btrfs_file_extent_item *fi;
Chris Mason02217ed2007-03-02 16:08:05 -050088 int i;
Chris Mason6407bf62007-03-27 06:33:00 -040089 int leaf;
90 int ret;
Chris Masona28ec192007-03-06 20:08:01 -050091
Chris Mason3768f362007-03-13 16:47:54 -040092 if (!root->ref_cows)
Chris Masona28ec192007-03-06 20:08:01 -050093 return 0;
Chris Masone20d96d2007-03-22 12:13:20 -040094 buf_node = btrfs_buffer_node(buf);
Chris Mason6407bf62007-03-27 06:33:00 -040095 leaf = btrfs_is_leaf(buf_node);
96 buf_leaf = btrfs_buffer_leaf(buf);
Chris Masone20d96d2007-03-22 12:13:20 -040097 for (i = 0; i < btrfs_header_nritems(&buf_node->header); i++) {
Chris Mason6407bf62007-03-27 06:33:00 -040098 if (leaf) {
99 key = &buf_leaf->items[i].key;
100 if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY)
101 continue;
102 fi = btrfs_item_ptr(buf_leaf, i,
103 struct btrfs_file_extent_item);
104 ret = inc_block_ref(trans, root,
105 btrfs_file_extent_disk_blocknr(fi),
106 btrfs_file_extent_disk_num_blocks(fi));
107 BUG_ON(ret);
108 } else {
109 blocknr = btrfs_node_blockptr(buf_node, i);
110 ret = inc_block_ref(trans, root, blocknr, 1);
111 BUG_ON(ret);
112 }
Chris Mason02217ed2007-03-02 16:08:05 -0500113 }
114 return 0;
115}
116
Chris Masone089f052007-03-16 16:20:31 -0400117int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, struct
118 btrfs_root *root)
Chris Masona28ec192007-03-06 20:08:01 -0500119{
Chris Mason8ef97622007-03-26 10:15:30 -0400120 unsigned long gang[8];
Chris Mason88fd1462007-03-16 08:56:18 -0400121 u64 first = 0;
Chris Masona28ec192007-03-06 20:08:01 -0500122 int ret;
123 int i;
Chris Mason8ef97622007-03-26 10:15:30 -0400124 struct radix_tree_root *pinned_radix = &root->fs_info->pinned_radix;
Chris Masona28ec192007-03-06 20:08:01 -0500125
126 while(1) {
Chris Mason8ef97622007-03-26 10:15:30 -0400127 ret = find_first_radix_bit(pinned_radix, gang,
128 ARRAY_SIZE(gang));
Chris Masona28ec192007-03-06 20:08:01 -0500129 if (!ret)
130 break;
Chris Mason88fd1462007-03-16 08:56:18 -0400131 if (!first)
Chris Mason8ef97622007-03-26 10:15:30 -0400132 first = gang[0];
Chris Mason0579da42007-03-07 16:15:30 -0500133 for (i = 0; i < ret; i++) {
Chris Mason8ef97622007-03-26 10:15:30 -0400134 clear_radix_bit(pinned_radix, gang[i]);
Chris Mason0579da42007-03-07 16:15:30 -0500135 }
Chris Masona28ec192007-03-06 20:08:01 -0500136 }
Chris Masond5719762007-03-23 10:01:08 -0400137 if (root->fs_info->last_insert.objectid > first)
138 root->fs_info->last_insert.objectid = first;
Chris Mason9f5fae22007-03-20 14:38:32 -0400139 root->fs_info->last_insert.offset = 0;
Chris Masona28ec192007-03-06 20:08:01 -0500140 return 0;
141}
142
Chris Masone089f052007-03-16 16:20:31 -0400143static int finish_current_insert(struct btrfs_trans_handle *trans, struct
144 btrfs_root *extent_root)
Chris Mason037e6392007-03-07 11:50:24 -0500145{
Chris Masone2fa7222007-03-12 16:22:34 -0400146 struct btrfs_key ins;
Chris Mason234b63a2007-03-13 10:46:10 -0400147 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -0500148 int i;
149 int ret;
Chris Mason1261ec42007-03-20 20:35:03 -0400150 u64 super_blocks_used;
151 struct btrfs_fs_info *info = extent_root->fs_info;
Chris Mason037e6392007-03-07 11:50:24 -0500152
Chris Masoncf27e1e2007-03-13 09:49:06 -0400153 btrfs_set_extent_refs(&extent_item, 1);
154 btrfs_set_extent_owner(&extent_item,
Chris Masone20d96d2007-03-22 12:13:20 -0400155 btrfs_header_parentid(btrfs_buffer_header(extent_root->node)));
Chris Mason037e6392007-03-07 11:50:24 -0500156 ins.offset = 1;
157 ins.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -0400158 btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
Chris Mason037e6392007-03-07 11:50:24 -0500159
Chris Mason9f5fae22007-03-20 14:38:32 -0400160 for (i = 0; i < extent_root->fs_info->current_insert.flags; i++) {
161 ins.objectid = extent_root->fs_info->current_insert.objectid +
162 i;
Chris Mason1261ec42007-03-20 20:35:03 -0400163 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
164 btrfs_set_super_blocks_used(info->disk_super,
165 super_blocks_used + 1);
Chris Masone089f052007-03-16 16:20:31 -0400166 ret = btrfs_insert_item(trans, extent_root, &ins, &extent_item,
167 sizeof(extent_item));
Chris Mason037e6392007-03-07 11:50:24 -0500168 BUG_ON(ret);
169 }
Chris Mason9f5fae22007-03-20 14:38:32 -0400170 extent_root->fs_info->current_insert.offset = 0;
Chris Mason037e6392007-03-07 11:50:24 -0500171 return 0;
172}
173
Chris Mason8ef97622007-03-26 10:15:30 -0400174static int pin_down_block(struct btrfs_root *root, u64 blocknr, int pending)
Chris Masone20d96d2007-03-22 12:13:20 -0400175{
176 int err;
Chris Mason78fae272007-03-25 11:35:08 -0400177 struct btrfs_header *header;
Chris Mason8ef97622007-03-26 10:15:30 -0400178 struct buffer_head *bh;
Chris Mason78fae272007-03-25 11:35:08 -0400179
Chris Masonf4b9aa82007-03-27 11:05:53 -0400180 if (!pending) {
Chris Masond98237b2007-03-28 13:57:48 -0400181 bh = btrfs_find_tree_block(root, blocknr);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400182 if (bh) {
183 if (buffer_uptodate(bh)) {
184 u64 transid =
185 root->fs_info->running_transaction->transid;
186 header = btrfs_buffer_header(bh);
187 if (btrfs_header_generation(header) ==
188 transid) {
189 btrfs_block_release(root, bh);
190 return 0;
191 }
Chris Masonf4b9aa82007-03-27 11:05:53 -0400192 }
Chris Masond6025572007-03-30 14:27:56 -0400193 btrfs_block_release(root, bh);
Chris Mason8ef97622007-03-26 10:15:30 -0400194 }
Chris Mason8ef97622007-03-26 10:15:30 -0400195 err = set_radix_bit(&root->fs_info->pinned_radix, blocknr);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400196 } else {
197 err = set_radix_bit(&root->fs_info->pending_del_radix, blocknr);
198 }
Chris Mason8ef97622007-03-26 10:15:30 -0400199 BUG_ON(err);
Chris Masone20d96d2007-03-22 12:13:20 -0400200 return 0;
201}
202
Chris Masona28ec192007-03-06 20:08:01 -0500203/*
204 * remove an extent from the root, returns 0 on success
205 */
Chris Masone089f052007-03-16 16:20:31 -0400206static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason78fae272007-03-25 11:35:08 -0400207 *root, u64 blocknr, u64 num_blocks, int pin)
Chris Masona28ec192007-03-06 20:08:01 -0500208{
Chris Mason5caf2a02007-04-02 11:20:42 -0400209 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -0400210 struct btrfs_key key;
Chris Mason1261ec42007-03-20 20:35:03 -0400211 struct btrfs_fs_info *info = root->fs_info;
212 struct btrfs_root *extent_root = info->extent_root;
Chris Masona28ec192007-03-06 20:08:01 -0500213 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400214 struct btrfs_extent_item *ei;
Chris Masone2fa7222007-03-12 16:22:34 -0400215 struct btrfs_key ins;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400216 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500217
Chris Masona28ec192007-03-06 20:08:01 -0500218 key.objectid = blocknr;
219 key.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -0400220 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masona28ec192007-03-06 20:08:01 -0500221 key.offset = num_blocks;
222
Chris Masone089f052007-03-16 16:20:31 -0400223 find_free_extent(trans, root, 0, 0, (u64)-1, &ins);
Chris Mason5caf2a02007-04-02 11:20:42 -0400224 path = btrfs_alloc_path();
225 BUG_ON(!path);
226 btrfs_init_path(path);
227 ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
Chris Masona28ec192007-03-06 20:08:01 -0500228 if (ret) {
Chris Mason2e635a22007-03-21 11:12:56 -0400229 printk("failed to find %Lu\n", key.objectid);
Chris Mason234b63a2007-03-13 10:46:10 -0400230 btrfs_print_tree(extent_root, extent_root->node);
Chris Mason2e635a22007-03-21 11:12:56 -0400231 printk("failed to find %Lu\n", key.objectid);
Chris Masona28ec192007-03-06 20:08:01 -0500232 BUG();
233 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400234 ei = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
Chris Mason123abc82007-03-14 14:14:43 -0400235 struct btrfs_extent_item);
Chris Masona28ec192007-03-06 20:08:01 -0500236 BUG_ON(ei->refs == 0);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400237 refs = btrfs_extent_refs(ei) - 1;
238 btrfs_set_extent_refs(ei, refs);
Chris Mason5caf2a02007-04-02 11:20:42 -0400239 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400240 if (refs == 0) {
Chris Mason1261ec42007-03-20 20:35:03 -0400241 u64 super_blocks_used;
Chris Mason78fae272007-03-25 11:35:08 -0400242
243 if (pin) {
Chris Mason8ef97622007-03-26 10:15:30 -0400244 ret = pin_down_block(root, blocknr, 0);
Chris Mason78fae272007-03-25 11:35:08 -0400245 BUG_ON(ret);
246 }
247
Chris Mason1261ec42007-03-20 20:35:03 -0400248 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
249 btrfs_set_super_blocks_used(info->disk_super,
250 super_blocks_used - num_blocks);
Chris Mason5caf2a02007-04-02 11:20:42 -0400251 ret = btrfs_del_item(trans, extent_root, path);
Chris Masona28ec192007-03-06 20:08:01 -0500252 if (ret)
253 BUG();
254 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400255 btrfs_release_path(extent_root, path);
256 btrfs_free_path(path);
Chris Masone089f052007-03-16 16:20:31 -0400257 finish_current_insert(trans, extent_root);
Chris Masona28ec192007-03-06 20:08:01 -0500258 return ret;
259}
260
261/*
Chris Masonfec577f2007-02-26 10:40:21 -0500262 * find all the blocks marked as pending in the radix tree and remove
263 * them from the extent map
264 */
Chris Masone089f052007-03-16 16:20:31 -0400265static int del_pending_extents(struct btrfs_trans_handle *trans, struct
266 btrfs_root *extent_root)
Chris Masonfec577f2007-02-26 10:40:21 -0500267{
268 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -0400269 int wret;
270 int err = 0;
Chris Mason8ef97622007-03-26 10:15:30 -0400271 unsigned long gang[4];
Chris Masonfec577f2007-02-26 10:40:21 -0500272 int i;
Chris Mason8ef97622007-03-26 10:15:30 -0400273 struct radix_tree_root *pending_radix;
274 struct radix_tree_root *pinned_radix;
275
276 pending_radix = &extent_root->fs_info->pending_del_radix;
277 pinned_radix = &extent_root->fs_info->pinned_radix;
Chris Masonfec577f2007-02-26 10:40:21 -0500278
279 while(1) {
Chris Mason8ef97622007-03-26 10:15:30 -0400280 ret = find_first_radix_bit(pending_radix, gang,
281 ARRAY_SIZE(gang));
Chris Masonfec577f2007-02-26 10:40:21 -0500282 if (!ret)
283 break;
284 for (i = 0; i < ret; i++) {
Chris Mason8ef97622007-03-26 10:15:30 -0400285 wret = set_radix_bit(pinned_radix, gang[i]);
286 BUG_ON(wret);
287 wret = clear_radix_bit(pending_radix, gang[i]);
288 BUG_ON(wret);
Chris Masond5719762007-03-23 10:01:08 -0400289 wret = __free_extent(trans, extent_root,
Chris Mason8ef97622007-03-26 10:15:30 -0400290 gang[i], 1, 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400291 if (wret)
292 err = wret;
Chris Masonfec577f2007-02-26 10:40:21 -0500293 }
294 }
Chris Masone20d96d2007-03-22 12:13:20 -0400295 return err;
Chris Masonfec577f2007-02-26 10:40:21 -0500296}
297
298/*
299 * remove an extent from the root, returns 0 on success
300 */
Chris Masone089f052007-03-16 16:20:31 -0400301int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
302 *root, u64 blocknr, u64 num_blocks, int pin)
Chris Masonfec577f2007-02-26 10:40:21 -0500303{
Chris Mason9f5fae22007-03-20 14:38:32 -0400304 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Masonfec577f2007-02-26 10:40:21 -0500305 int pending_ret;
306 int ret;
Chris Masona28ec192007-03-06 20:08:01 -0500307
308 if (root == extent_root) {
Chris Mason8ef97622007-03-26 10:15:30 -0400309 pin_down_block(root, blocknr, 1);
Chris Masona28ec192007-03-06 20:08:01 -0500310 return 0;
311 }
Chris Mason78fae272007-03-25 11:35:08 -0400312 ret = __free_extent(trans, root, blocknr, num_blocks, pin);
Chris Masone20d96d2007-03-22 12:13:20 -0400313 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
Chris Masonfec577f2007-02-26 10:40:21 -0500314 return ret ? ret : pending_ret;
315}
316
317/*
318 * walks the btree of allocated extents and find a hole of a given size.
319 * The key ins is changed to record the hole:
320 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -0400321 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -0500322 * ins->offset == number of blocks
323 * Any available blocks before search_start are skipped.
324 */
Chris Masone089f052007-03-16 16:20:31 -0400325static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
326 *orig_root, u64 num_blocks, u64 search_start, u64
327 search_end, struct btrfs_key *ins)
Chris Masonfec577f2007-02-26 10:40:21 -0500328{
Chris Mason5caf2a02007-04-02 11:20:42 -0400329 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -0400330 struct btrfs_key key;
Chris Masonfec577f2007-02-26 10:40:21 -0500331 int ret;
332 u64 hole_size = 0;
333 int slot = 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400334 u64 last_block = 0;
Chris Mason037e6392007-03-07 11:50:24 -0500335 u64 test_block;
Chris Masonfec577f2007-02-26 10:40:21 -0500336 int start_found;
Chris Mason234b63a2007-03-13 10:46:10 -0400337 struct btrfs_leaf *l;
Chris Mason9f5fae22007-03-20 14:38:32 -0400338 struct btrfs_root * root = orig_root->fs_info->extent_root;
Chris Mason0579da42007-03-07 16:15:30 -0500339 int total_needed = num_blocks;
Chris Masone20d96d2007-03-22 12:13:20 -0400340 int level;
Chris Masonfec577f2007-02-26 10:40:21 -0500341
Chris Masone20d96d2007-03-22 12:13:20 -0400342 level = btrfs_header_level(btrfs_buffer_header(root->node));
343 total_needed += (level + 1) * 3;
Chris Mason9f5fae22007-03-20 14:38:32 -0400344 if (root->fs_info->last_insert.objectid > search_start)
345 search_start = root->fs_info->last_insert.objectid;
Chris Mason62e27492007-03-15 12:56:47 -0400346
347 ins->flags = 0;
348 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
Chris Mason5caf2a02007-04-02 11:20:42 -0400349 path = btrfs_alloc_path();
Chris Mason62e27492007-03-15 12:56:47 -0400350
Chris Masonfec577f2007-02-26 10:40:21 -0500351check_failed:
Chris Mason5caf2a02007-04-02 11:20:42 -0400352 btrfs_init_path(path);
Chris Masonfec577f2007-02-26 10:40:21 -0500353 ins->objectid = search_start;
354 ins->offset = 0;
Chris Masonfec577f2007-02-26 10:40:21 -0500355 start_found = 0;
Chris Mason5caf2a02007-04-02 11:20:42 -0400356 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
Chris Mason0f70abe2007-02-28 16:46:22 -0500357 if (ret < 0)
358 goto error;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500359
Chris Mason5caf2a02007-04-02 11:20:42 -0400360 if (path->slots[0] > 0)
361 path->slots[0]--;
Chris Mason0579da42007-03-07 16:15:30 -0500362
Chris Masonfec577f2007-02-26 10:40:21 -0500363 while (1) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400364 l = btrfs_buffer_leaf(path->nodes[0]);
365 slot = path->slots[0];
Chris Mason7518a232007-03-12 12:01:18 -0400366 if (slot >= btrfs_header_nritems(&l->header)) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400367 ret = btrfs_next_leaf(root, path);
Chris Masonfec577f2007-02-26 10:40:21 -0500368 if (ret == 0)
369 continue;
Chris Mason0f70abe2007-02-28 16:46:22 -0500370 if (ret < 0)
371 goto error;
Chris Masonfec577f2007-02-26 10:40:21 -0500372 if (!start_found) {
373 ins->objectid = search_start;
Chris Mason037e6392007-03-07 11:50:24 -0500374 ins->offset = (u64)-1;
Chris Masonfec577f2007-02-26 10:40:21 -0500375 start_found = 1;
376 goto check_pending;
377 }
378 ins->objectid = last_block > search_start ?
379 last_block : search_start;
Chris Mason037e6392007-03-07 11:50:24 -0500380 ins->offset = (u64)-1;
Chris Masonfec577f2007-02-26 10:40:21 -0500381 goto check_pending;
382 }
Chris Masone2fa7222007-03-12 16:22:34 -0400383 btrfs_disk_key_to_cpu(&key, &l->items[slot].key);
384 if (key.objectid >= search_start) {
Chris Masonfec577f2007-02-26 10:40:21 -0500385 if (start_found) {
Chris Mason0579da42007-03-07 16:15:30 -0500386 if (last_block < search_start)
387 last_block = search_start;
Chris Masone2fa7222007-03-12 16:22:34 -0400388 hole_size = key.objectid - last_block;
Chris Mason037e6392007-03-07 11:50:24 -0500389 if (hole_size > total_needed) {
Chris Masonfec577f2007-02-26 10:40:21 -0500390 ins->objectid = last_block;
Chris Mason037e6392007-03-07 11:50:24 -0500391 ins->offset = hole_size;
Chris Masonfec577f2007-02-26 10:40:21 -0500392 goto check_pending;
393 }
Chris Mason0579da42007-03-07 16:15:30 -0500394 }
Chris Masonfec577f2007-02-26 10:40:21 -0500395 }
Chris Mason0579da42007-03-07 16:15:30 -0500396 start_found = 1;
Chris Masone2fa7222007-03-12 16:22:34 -0400397 last_block = key.objectid + key.offset;
Chris Mason5caf2a02007-04-02 11:20:42 -0400398 path->slots[0]++;
Chris Masonfec577f2007-02-26 10:40:21 -0500399 }
400 // FIXME -ENOSPC
401check_pending:
402 /* we have to make sure we didn't find an extent that has already
403 * been allocated by the map tree or the original allocation
404 */
Chris Mason5caf2a02007-04-02 11:20:42 -0400405 btrfs_release_path(root, path);
Chris Masonfec577f2007-02-26 10:40:21 -0500406 BUG_ON(ins->objectid < search_start);
Chris Mason037e6392007-03-07 11:50:24 -0500407 for (test_block = ins->objectid;
408 test_block < ins->objectid + total_needed; test_block++) {
Chris Mason8ef97622007-03-26 10:15:30 -0400409 if (test_radix_bit(&root->fs_info->pinned_radix,
Chris Mason9f5fae22007-03-20 14:38:32 -0400410 test_block)) {
Chris Mason037e6392007-03-07 11:50:24 -0500411 search_start = test_block + 1;
Chris Masonfec577f2007-02-26 10:40:21 -0500412 goto check_failed;
413 }
414 }
Chris Mason9f5fae22007-03-20 14:38:32 -0400415 BUG_ON(root->fs_info->current_insert.offset);
416 root->fs_info->current_insert.offset = total_needed - num_blocks;
417 root->fs_info->current_insert.objectid = ins->objectid + num_blocks;
418 root->fs_info->current_insert.flags = 0;
419 root->fs_info->last_insert.objectid = ins->objectid;
Chris Mason037e6392007-03-07 11:50:24 -0500420 ins->offset = num_blocks;
Chris Mason5caf2a02007-04-02 11:20:42 -0400421 btrfs_free_path(path);
Chris Masonfec577f2007-02-26 10:40:21 -0500422 return 0;
Chris Mason0f70abe2007-02-28 16:46:22 -0500423error:
Chris Mason5caf2a02007-04-02 11:20:42 -0400424 btrfs_release_path(root, path);
425 btrfs_free_path(path);
Chris Mason0f70abe2007-02-28 16:46:22 -0500426 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -0500427}
428
429/*
Chris Masonfec577f2007-02-26 10:40:21 -0500430 * finds a free extent and does all the dirty work required for allocation
431 * returns the key for the extent through ins, and a tree buffer for
432 * the first block of the extent through buf.
433 *
434 * returns 0 if everything worked, non-zero otherwise.
435 */
Chris Masondee26a92007-03-26 16:00:06 -0400436int btrfs_alloc_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masone089f052007-03-16 16:20:31 -0400437 *root, u64 num_blocks, u64 search_start, u64
438 search_end, u64 owner, struct btrfs_key *ins)
Chris Masonfec577f2007-02-26 10:40:21 -0500439{
440 int ret;
441 int pending_ret;
Chris Mason1261ec42007-03-20 20:35:03 -0400442 u64 super_blocks_used;
443 struct btrfs_fs_info *info = root->fs_info;
444 struct btrfs_root *extent_root = info->extent_root;
Chris Mason234b63a2007-03-13 10:46:10 -0400445 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -0500446
Chris Masoncf27e1e2007-03-13 09:49:06 -0400447 btrfs_set_extent_refs(&extent_item, 1);
448 btrfs_set_extent_owner(&extent_item, owner);
Chris Masonfec577f2007-02-26 10:40:21 -0500449
Chris Mason037e6392007-03-07 11:50:24 -0500450 if (root == extent_root) {
Chris Mason9f5fae22007-03-20 14:38:32 -0400451 BUG_ON(extent_root->fs_info->current_insert.offset == 0);
Chris Mason037e6392007-03-07 11:50:24 -0500452 BUG_ON(num_blocks != 1);
Chris Mason9f5fae22007-03-20 14:38:32 -0400453 BUG_ON(extent_root->fs_info->current_insert.flags ==
454 extent_root->fs_info->current_insert.offset);
Chris Mason037e6392007-03-07 11:50:24 -0500455 ins->offset = 1;
Chris Mason9f5fae22007-03-20 14:38:32 -0400456 ins->objectid = extent_root->fs_info->current_insert.objectid +
457 extent_root->fs_info->current_insert.flags++;
Chris Masonfec577f2007-02-26 10:40:21 -0500458 return 0;
459 }
Chris Masone089f052007-03-16 16:20:31 -0400460 ret = find_free_extent(trans, root, num_blocks, search_start,
Chris Mason037e6392007-03-07 11:50:24 -0500461 search_end, ins);
462 if (ret)
463 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -0500464
Chris Mason1261ec42007-03-20 20:35:03 -0400465 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
466 btrfs_set_super_blocks_used(info->disk_super, super_blocks_used +
467 num_blocks);
Chris Masone089f052007-03-16 16:20:31 -0400468 ret = btrfs_insert_item(trans, extent_root, ins, &extent_item,
469 sizeof(extent_item));
Chris Mason037e6392007-03-07 11:50:24 -0500470
Chris Masone089f052007-03-16 16:20:31 -0400471 finish_current_insert(trans, extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -0400472 pending_ret = del_pending_extents(trans, extent_root);
Chris Mason037e6392007-03-07 11:50:24 -0500473 if (ret)
474 return ret;
475 if (pending_ret)
476 return pending_ret;
477 return 0;
Chris Masonfec577f2007-02-26 10:40:21 -0500478}
479
480/*
481 * helper function to allocate a block for a given tree
482 * returns the tree buffer or NULL.
483 */
Chris Masone20d96d2007-03-22 12:13:20 -0400484struct buffer_head *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Chris Masone089f052007-03-16 16:20:31 -0400485 struct btrfs_root *root)
Chris Masonfec577f2007-02-26 10:40:21 -0500486{
Chris Masone2fa7222007-03-12 16:22:34 -0400487 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -0500488 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -0400489 struct buffer_head *buf;
Chris Masonfec577f2007-02-26 10:40:21 -0500490
Chris Masondee26a92007-03-26 16:00:06 -0400491 ret = btrfs_alloc_extent(trans, root, 1, 0, (unsigned long)-1,
Chris Masone20d96d2007-03-22 12:13:20 -0400492 btrfs_header_parentid(btrfs_buffer_header(root->node)), &ins);
Chris Masonfec577f2007-02-26 10:40:21 -0500493 if (ret) {
494 BUG();
495 return NULL;
496 }
Chris Masond98237b2007-03-28 13:57:48 -0400497 buf = btrfs_find_create_tree_block(root, ins.objectid);
Chris Masondf2ce342007-03-23 11:00:45 -0400498 set_buffer_uptodate(buf);
Chris Masonfec577f2007-02-26 10:40:21 -0500499 return buf;
500}
Chris Masona28ec192007-03-06 20:08:01 -0500501
Chris Mason6407bf62007-03-27 06:33:00 -0400502static int drop_leaf_ref(struct btrfs_trans_handle *trans,
503 struct btrfs_root *root, struct buffer_head *cur)
504{
505 struct btrfs_disk_key *key;
506 struct btrfs_leaf *leaf;
507 struct btrfs_file_extent_item *fi;
508 int i;
509 int nritems;
510 int ret;
511
512 BUG_ON(!btrfs_is_leaf(btrfs_buffer_node(cur)));
513 leaf = btrfs_buffer_leaf(cur);
514 nritems = btrfs_header_nritems(&leaf->header);
515 for (i = 0; i < nritems; i++) {
516 key = &leaf->items[i].key;
517 if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY)
518 continue;
519 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
520 /*
521 * FIXME make sure to insert a trans record that
522 * repeats the snapshot del on crash
523 */
524 ret = btrfs_free_extent(trans, root,
525 btrfs_file_extent_disk_blocknr(fi),
526 btrfs_file_extent_disk_num_blocks(fi),
527 0);
528 BUG_ON(ret);
529 }
530 return 0;
531}
532
Chris Mason9aca1d52007-03-13 11:09:37 -0400533/*
534 * helper function for drop_snapshot, this walks down the tree dropping ref
535 * counts as it goes.
536 */
Chris Masone089f052007-03-16 16:20:31 -0400537static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
538 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -0500539{
Chris Masone20d96d2007-03-22 12:13:20 -0400540 struct buffer_head *next;
541 struct buffer_head *cur;
Chris Mason20524f02007-03-10 06:35:47 -0500542 u64 blocknr;
543 int ret;
544 u32 refs;
545
Chris Mason5caf2a02007-04-02 11:20:42 -0400546 WARN_ON(*level < 0);
547 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Masone20d96d2007-03-22 12:13:20 -0400548 ret = lookup_block_ref(trans, root, path->nodes[*level]->b_blocknr,
Chris Mason6407bf62007-03-27 06:33:00 -0400549 1, &refs);
Chris Mason20524f02007-03-10 06:35:47 -0500550 BUG_ON(ret);
551 if (refs > 1)
552 goto out;
Chris Mason9aca1d52007-03-13 11:09:37 -0400553 /*
554 * walk down to the last node level and free all the leaves
555 */
Chris Mason6407bf62007-03-27 06:33:00 -0400556 while(*level >= 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400557 WARN_ON(*level < 0);
558 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason20524f02007-03-10 06:35:47 -0500559 cur = path->nodes[*level];
Chris Mason2c90e5d2007-04-02 10:50:19 -0400560 if (btrfs_header_level(btrfs_buffer_header(cur)) != *level)
561 WARN_ON(1);
Chris Mason7518a232007-03-12 12:01:18 -0400562 if (path->slots[*level] >=
Chris Masone20d96d2007-03-22 12:13:20 -0400563 btrfs_header_nritems(btrfs_buffer_header(cur)))
Chris Mason20524f02007-03-10 06:35:47 -0500564 break;
Chris Mason6407bf62007-03-27 06:33:00 -0400565 if (*level == 0) {
566 ret = drop_leaf_ref(trans, root, cur);
567 BUG_ON(ret);
568 break;
569 }
Chris Masone20d96d2007-03-22 12:13:20 -0400570 blocknr = btrfs_node_blockptr(btrfs_buffer_node(cur),
571 path->slots[*level]);
Chris Mason6407bf62007-03-27 06:33:00 -0400572 ret = lookup_block_ref(trans, root, blocknr, 1, &refs);
573 BUG_ON(ret);
574 if (refs != 1) {
Chris Mason20524f02007-03-10 06:35:47 -0500575 path->slots[*level]++;
Chris Masone089f052007-03-16 16:20:31 -0400576 ret = btrfs_free_extent(trans, root, blocknr, 1, 1);
Chris Mason20524f02007-03-10 06:35:47 -0500577 BUG_ON(ret);
578 continue;
579 }
Chris Mason20524f02007-03-10 06:35:47 -0500580 next = read_tree_block(root, blocknr);
Chris Mason5caf2a02007-04-02 11:20:42 -0400581 WARN_ON(*level <= 0);
Chris Mason83e15a22007-03-12 09:03:27 -0400582 if (path->nodes[*level-1])
Chris Mason234b63a2007-03-13 10:46:10 -0400583 btrfs_block_release(root, path->nodes[*level-1]);
Chris Mason20524f02007-03-10 06:35:47 -0500584 path->nodes[*level-1] = next;
Chris Masone20d96d2007-03-22 12:13:20 -0400585 *level = btrfs_header_level(btrfs_buffer_header(next));
Chris Mason20524f02007-03-10 06:35:47 -0500586 path->slots[*level] = 0;
587 }
588out:
Chris Mason5caf2a02007-04-02 11:20:42 -0400589 WARN_ON(*level < 0);
590 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason6407bf62007-03-27 06:33:00 -0400591 ret = btrfs_free_extent(trans, root,
592 path->nodes[*level]->b_blocknr, 1, 1);
Chris Mason234b63a2007-03-13 10:46:10 -0400593 btrfs_block_release(root, path->nodes[*level]);
Chris Mason20524f02007-03-10 06:35:47 -0500594 path->nodes[*level] = NULL;
595 *level += 1;
596 BUG_ON(ret);
597 return 0;
598}
599
Chris Mason9aca1d52007-03-13 11:09:37 -0400600/*
601 * helper for dropping snapshots. This walks back up the tree in the path
602 * to find the first node higher up where we haven't yet gone through
603 * all the slots
604 */
Chris Masone089f052007-03-16 16:20:31 -0400605static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root
606 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -0500607{
608 int i;
609 int slot;
610 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400611 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Mason20524f02007-03-10 06:35:47 -0500612 slot = path->slots[i];
Chris Masone20d96d2007-03-22 12:13:20 -0400613 if (slot < btrfs_header_nritems(
614 btrfs_buffer_header(path->nodes[i])) - 1) {
Chris Mason20524f02007-03-10 06:35:47 -0500615 path->slots[i]++;
616 *level = i;
617 return 0;
618 } else {
Chris Masone089f052007-03-16 16:20:31 -0400619 ret = btrfs_free_extent(trans, root,
Chris Masone20d96d2007-03-22 12:13:20 -0400620 path->nodes[*level]->b_blocknr,
Chris Masone089f052007-03-16 16:20:31 -0400621 1, 1);
Chris Mason6407bf62007-03-27 06:33:00 -0400622 BUG_ON(ret);
Chris Mason234b63a2007-03-13 10:46:10 -0400623 btrfs_block_release(root, path->nodes[*level]);
Chris Mason83e15a22007-03-12 09:03:27 -0400624 path->nodes[*level] = NULL;
Chris Mason20524f02007-03-10 06:35:47 -0500625 *level = i + 1;
Chris Mason20524f02007-03-10 06:35:47 -0500626 }
627 }
628 return 1;
629}
630
Chris Mason9aca1d52007-03-13 11:09:37 -0400631/*
632 * drop the reference count on the tree rooted at 'snap'. This traverses
633 * the tree freeing any blocks that have a ref count of zero after being
634 * decremented.
635 */
Chris Masone089f052007-03-16 16:20:31 -0400636int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masone20d96d2007-03-22 12:13:20 -0400637 *root, struct buffer_head *snap)
Chris Mason20524f02007-03-10 06:35:47 -0500638{
Chris Mason3768f362007-03-13 16:47:54 -0400639 int ret = 0;
Chris Mason9aca1d52007-03-13 11:09:37 -0400640 int wret;
Chris Mason20524f02007-03-10 06:35:47 -0500641 int level;
Chris Mason5caf2a02007-04-02 11:20:42 -0400642 struct btrfs_path *path;
Chris Mason20524f02007-03-10 06:35:47 -0500643 int i;
644 int orig_level;
645
Chris Mason5caf2a02007-04-02 11:20:42 -0400646 path = btrfs_alloc_path();
647 BUG_ON(!path);
648 btrfs_init_path(path);
Chris Mason20524f02007-03-10 06:35:47 -0500649
Chris Masone20d96d2007-03-22 12:13:20 -0400650 level = btrfs_header_level(btrfs_buffer_header(snap));
Chris Mason20524f02007-03-10 06:35:47 -0500651 orig_level = level;
Chris Mason5caf2a02007-04-02 11:20:42 -0400652 path->nodes[level] = snap;
653 path->slots[level] = 0;
Chris Mason20524f02007-03-10 06:35:47 -0500654 while(1) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400655 wret = walk_down_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -0400656 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -0500657 break;
Chris Mason9aca1d52007-03-13 11:09:37 -0400658 if (wret < 0)
659 ret = wret;
660
Chris Mason5caf2a02007-04-02 11:20:42 -0400661 wret = walk_up_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -0400662 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -0500663 break;
Chris Mason9aca1d52007-03-13 11:09:37 -0400664 if (wret < 0)
665 ret = wret;
Chris Mason20524f02007-03-10 06:35:47 -0500666 }
Chris Mason83e15a22007-03-12 09:03:27 -0400667 for (i = 0; i <= orig_level; i++) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400668 if (path->nodes[i]) {
669 btrfs_block_release(root, path->nodes[i]);
Chris Mason83e15a22007-03-12 09:03:27 -0400670 }
Chris Mason20524f02007-03-10 06:35:47 -0500671 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400672 btrfs_free_path(path);
Chris Mason9aca1d52007-03-13 11:09:37 -0400673 return ret;
Chris Mason20524f02007-03-10 06:35:47 -0500674}