blob: 82f6e9eed1d059fdbd2bcf07d4c4a68522c93cc2 [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
16 *root, u64 blocknr)
Chris Mason02217ed2007-03-02 16:08:05 -050017{
Chris Mason234b63a2007-03-13 10:46:10 -040018 struct btrfs_path path;
Chris Mason02217ed2007-03-02 16:08:05 -050019 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -040020 struct btrfs_key key;
Chris Mason234b63a2007-03-13 10:46:10 -040021 struct btrfs_leaf *l;
22 struct btrfs_extent_item *item;
Chris Masone2fa7222007-03-12 16:22:34 -040023 struct btrfs_key ins;
Chris Masoncf27e1e2007-03-13 09:49:06 -040024 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -050025
Chris Mason9f5fae22007-03-20 14:38:32 -040026 find_free_extent(trans, root->fs_info->extent_root, 0, 0, (u64)-1,
27 &ins);
Chris Mason234b63a2007-03-13 10:46:10 -040028 btrfs_init_path(&path);
Chris Mason02217ed2007-03-02 16:08:05 -050029 key.objectid = blocknr;
30 key.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -040031 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Mason02217ed2007-03-02 16:08:05 -050032 key.offset = 1;
Chris Mason9f5fae22007-03-20 14:38:32 -040033 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, &path,
34 0, 1);
Chris Masona28ec192007-03-06 20:08:01 -050035 if (ret != 0)
36 BUG();
Chris Mason02217ed2007-03-02 16:08:05 -050037 BUG_ON(ret != 0);
Chris Masone20d96d2007-03-22 12:13:20 -040038 l = btrfs_buffer_leaf(path.nodes[0]);
Chris Mason4beb1b82007-03-14 10:31:29 -040039 item = btrfs_item_ptr(l, path.slots[0], struct btrfs_extent_item);
Chris Masoncf27e1e2007-03-13 09:49:06 -040040 refs = btrfs_extent_refs(item);
41 btrfs_set_extent_refs(item, refs + 1);
Chris Masond5719762007-03-23 10:01:08 -040042 mark_buffer_dirty(path.nodes[0]);
Chris Masona28ec192007-03-06 20:08:01 -050043
Chris Mason9f5fae22007-03-20 14:38:32 -040044 btrfs_release_path(root->fs_info->extent_root, &path);
45 finish_current_insert(trans, root->fs_info->extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -040046 del_pending_extents(trans, root->fs_info->extent_root);
Chris Mason02217ed2007-03-02 16:08:05 -050047 return 0;
48}
49
Chris Masone089f052007-03-16 16:20:31 -040050static int lookup_block_ref(struct btrfs_trans_handle *trans, struct btrfs_root
51 *root, u64 blocknr, u32 *refs)
Chris Masona28ec192007-03-06 20:08:01 -050052{
Chris Mason234b63a2007-03-13 10:46:10 -040053 struct btrfs_path path;
Chris Masona28ec192007-03-06 20:08:01 -050054 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -040055 struct btrfs_key key;
Chris Mason234b63a2007-03-13 10:46:10 -040056 struct btrfs_leaf *l;
57 struct btrfs_extent_item *item;
58 btrfs_init_path(&path);
Chris Masona28ec192007-03-06 20:08:01 -050059 key.objectid = blocknr;
Chris Masona28ec192007-03-06 20:08:01 -050060 key.offset = 1;
Chris Mason62e27492007-03-15 12:56:47 -040061 key.flags = 0;
62 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Mason9f5fae22007-03-20 14:38:32 -040063 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, &path,
64 0, 0);
Chris Masona28ec192007-03-06 20:08:01 -050065 if (ret != 0)
66 BUG();
Chris Masone20d96d2007-03-22 12:13:20 -040067 l = btrfs_buffer_leaf(path.nodes[0]);
Chris Mason4beb1b82007-03-14 10:31:29 -040068 item = btrfs_item_ptr(l, path.slots[0], struct btrfs_extent_item);
Chris Masoncf27e1e2007-03-13 09:49:06 -040069 *refs = btrfs_extent_refs(item);
Chris Mason9f5fae22007-03-20 14:38:32 -040070 btrfs_release_path(root->fs_info->extent_root, &path);
Chris Masona28ec192007-03-06 20:08:01 -050071 return 0;
72}
73
Chris Masone089f052007-03-16 16:20:31 -040074int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Masone20d96d2007-03-22 12:13:20 -040075 struct buffer_head *buf)
Chris Mason02217ed2007-03-02 16:08:05 -050076{
77 u64 blocknr;
Chris Masone20d96d2007-03-22 12:13:20 -040078 struct btrfs_node *buf_node;
Chris Mason02217ed2007-03-02 16:08:05 -050079 int i;
Chris Masona28ec192007-03-06 20:08:01 -050080
Chris Mason3768f362007-03-13 16:47:54 -040081 if (!root->ref_cows)
Chris Masona28ec192007-03-06 20:08:01 -050082 return 0;
Chris Masone20d96d2007-03-22 12:13:20 -040083 buf_node = btrfs_buffer_node(buf);
84 if (btrfs_is_leaf(buf_node))
Chris Masona28ec192007-03-06 20:08:01 -050085 return 0;
86
Chris Masone20d96d2007-03-22 12:13:20 -040087 for (i = 0; i < btrfs_header_nritems(&buf_node->header); i++) {
88 blocknr = btrfs_node_blockptr(buf_node, i);
Chris Masone089f052007-03-16 16:20:31 -040089 inc_block_ref(trans, root, blocknr);
Chris Mason02217ed2007-03-02 16:08:05 -050090 }
91 return 0;
92}
93
Chris Masone089f052007-03-16 16:20:31 -040094int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, struct
95 btrfs_root *root)
Chris Masona28ec192007-03-06 20:08:01 -050096{
Chris Mason8ef97622007-03-26 10:15:30 -040097 unsigned long gang[8];
Chris Mason88fd1462007-03-16 08:56:18 -040098 u64 first = 0;
Chris Masona28ec192007-03-06 20:08:01 -050099 int ret;
100 int i;
Chris Mason8ef97622007-03-26 10:15:30 -0400101 struct radix_tree_root *pinned_radix = &root->fs_info->pinned_radix;
Chris Masona28ec192007-03-06 20:08:01 -0500102
103 while(1) {
Chris Mason8ef97622007-03-26 10:15:30 -0400104 ret = find_first_radix_bit(pinned_radix, gang,
105 ARRAY_SIZE(gang));
Chris Masona28ec192007-03-06 20:08:01 -0500106 if (!ret)
107 break;
Chris Mason88fd1462007-03-16 08:56:18 -0400108 if (!first)
Chris Mason8ef97622007-03-26 10:15:30 -0400109 first = gang[0];
Chris Mason0579da42007-03-07 16:15:30 -0500110 for (i = 0; i < ret; i++) {
Chris Mason8ef97622007-03-26 10:15:30 -0400111 clear_radix_bit(pinned_radix, gang[i]);
Chris Mason0579da42007-03-07 16:15:30 -0500112 }
Chris Masona28ec192007-03-06 20:08:01 -0500113 }
Chris Masond5719762007-03-23 10:01:08 -0400114 if (root->fs_info->last_insert.objectid > first)
115 root->fs_info->last_insert.objectid = first;
Chris Mason9f5fae22007-03-20 14:38:32 -0400116 root->fs_info->last_insert.offset = 0;
Chris Masona28ec192007-03-06 20:08:01 -0500117 return 0;
118}
119
Chris Masone089f052007-03-16 16:20:31 -0400120static int finish_current_insert(struct btrfs_trans_handle *trans, struct
121 btrfs_root *extent_root)
Chris Mason037e6392007-03-07 11:50:24 -0500122{
Chris Masone2fa7222007-03-12 16:22:34 -0400123 struct btrfs_key ins;
Chris Mason234b63a2007-03-13 10:46:10 -0400124 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -0500125 int i;
126 int ret;
Chris Mason1261ec42007-03-20 20:35:03 -0400127 u64 super_blocks_used;
128 struct btrfs_fs_info *info = extent_root->fs_info;
Chris Mason037e6392007-03-07 11:50:24 -0500129
Chris Masoncf27e1e2007-03-13 09:49:06 -0400130 btrfs_set_extent_refs(&extent_item, 1);
131 btrfs_set_extent_owner(&extent_item,
Chris Masone20d96d2007-03-22 12:13:20 -0400132 btrfs_header_parentid(btrfs_buffer_header(extent_root->node)));
Chris Mason037e6392007-03-07 11:50:24 -0500133 ins.offset = 1;
134 ins.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -0400135 btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
Chris Mason037e6392007-03-07 11:50:24 -0500136
Chris Mason9f5fae22007-03-20 14:38:32 -0400137 for (i = 0; i < extent_root->fs_info->current_insert.flags; i++) {
138 ins.objectid = extent_root->fs_info->current_insert.objectid +
139 i;
Chris Mason1261ec42007-03-20 20:35:03 -0400140 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
141 btrfs_set_super_blocks_used(info->disk_super,
142 super_blocks_used + 1);
Chris Masone089f052007-03-16 16:20:31 -0400143 ret = btrfs_insert_item(trans, extent_root, &ins, &extent_item,
144 sizeof(extent_item));
Chris Mason037e6392007-03-07 11:50:24 -0500145 BUG_ON(ret);
146 }
Chris Mason9f5fae22007-03-20 14:38:32 -0400147 extent_root->fs_info->current_insert.offset = 0;
Chris Mason037e6392007-03-07 11:50:24 -0500148 return 0;
149}
150
Chris Mason8ef97622007-03-26 10:15:30 -0400151static int pin_down_block(struct btrfs_root *root, u64 blocknr, int pending)
Chris Masone20d96d2007-03-22 12:13:20 -0400152{
153 int err;
Chris Mason78fae272007-03-25 11:35:08 -0400154 struct btrfs_header *header;
Chris Mason8ef97622007-03-26 10:15:30 -0400155 struct buffer_head *bh;
Chris Mason78fae272007-03-25 11:35:08 -0400156
Chris Mason8ef97622007-03-26 10:15:30 -0400157 bh = sb_find_get_block(root->fs_info->sb, blocknr);
158 if (bh) {
159 header = btrfs_buffer_header(bh);
160 if (btrfs_header_generation(header) ==
161 root->fs_info->running_transaction->transid) {
162 brelse(bh);
163 return 0;
164 }
Chris Mason78fae272007-03-25 11:35:08 -0400165 brelse(bh);
Chris Mason8ef97622007-03-26 10:15:30 -0400166 }
167 if (pending)
168 err = set_radix_bit(&root->fs_info->pending_del_radix, blocknr);
169 else
170 err = set_radix_bit(&root->fs_info->pinned_radix, blocknr);
171 BUG_ON(err);
Chris Masone20d96d2007-03-22 12:13:20 -0400172 return 0;
173}
174
Chris Masona28ec192007-03-06 20:08:01 -0500175/*
176 * remove an extent from the root, returns 0 on success
177 */
Chris Masone089f052007-03-16 16:20:31 -0400178static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason78fae272007-03-25 11:35:08 -0400179 *root, u64 blocknr, u64 num_blocks, int pin)
Chris Masona28ec192007-03-06 20:08:01 -0500180{
Chris Mason234b63a2007-03-13 10:46:10 -0400181 struct btrfs_path path;
Chris Masone2fa7222007-03-12 16:22:34 -0400182 struct btrfs_key key;
Chris Mason1261ec42007-03-20 20:35:03 -0400183 struct btrfs_fs_info *info = root->fs_info;
184 struct btrfs_root *extent_root = info->extent_root;
Chris Masona28ec192007-03-06 20:08:01 -0500185 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400186 struct btrfs_extent_item *ei;
Chris Masone2fa7222007-03-12 16:22:34 -0400187 struct btrfs_key ins;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400188 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500189
Chris Masona28ec192007-03-06 20:08:01 -0500190 key.objectid = blocknr;
191 key.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -0400192 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masona28ec192007-03-06 20:08:01 -0500193 key.offset = num_blocks;
194
Chris Masone089f052007-03-16 16:20:31 -0400195 find_free_extent(trans, root, 0, 0, (u64)-1, &ins);
Chris Mason234b63a2007-03-13 10:46:10 -0400196 btrfs_init_path(&path);
Chris Masone089f052007-03-16 16:20:31 -0400197 ret = btrfs_search_slot(trans, extent_root, &key, &path, -1, 1);
Chris Masona28ec192007-03-06 20:08:01 -0500198 if (ret) {
Chris Mason2e635a22007-03-21 11:12:56 -0400199 printk("failed to find %Lu\n", key.objectid);
Chris Mason234b63a2007-03-13 10:46:10 -0400200 btrfs_print_tree(extent_root, extent_root->node);
Chris Mason2e635a22007-03-21 11:12:56 -0400201 printk("failed to find %Lu\n", key.objectid);
Chris Masona28ec192007-03-06 20:08:01 -0500202 BUG();
203 }
Chris Masone20d96d2007-03-22 12:13:20 -0400204 ei = btrfs_item_ptr(btrfs_buffer_leaf(path.nodes[0]), path.slots[0],
Chris Mason123abc82007-03-14 14:14:43 -0400205 struct btrfs_extent_item);
Chris Masona28ec192007-03-06 20:08:01 -0500206 BUG_ON(ei->refs == 0);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400207 refs = btrfs_extent_refs(ei) - 1;
208 btrfs_set_extent_refs(ei, refs);
209 if (refs == 0) {
Chris Mason1261ec42007-03-20 20:35:03 -0400210 u64 super_blocks_used;
Chris Mason78fae272007-03-25 11:35:08 -0400211
212 if (pin) {
Chris Mason8ef97622007-03-26 10:15:30 -0400213 ret = pin_down_block(root, blocknr, 0);
Chris Mason78fae272007-03-25 11:35:08 -0400214 BUG_ON(ret);
215 }
216
Chris Mason1261ec42007-03-20 20:35:03 -0400217 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
218 btrfs_set_super_blocks_used(info->disk_super,
219 super_blocks_used - num_blocks);
Chris Masone089f052007-03-16 16:20:31 -0400220 ret = btrfs_del_item(trans, extent_root, &path);
Chris Mason78fae272007-03-25 11:35:08 -0400221 if (extent_root->fs_info->last_insert.objectid > blocknr)
Chris Mason9f5fae22007-03-20 14:38:32 -0400222 extent_root->fs_info->last_insert.objectid = blocknr;
Chris Masona28ec192007-03-06 20:08:01 -0500223 if (ret)
224 BUG();
225 }
Chris Masond5719762007-03-23 10:01:08 -0400226 mark_buffer_dirty(path.nodes[0]);
Chris Mason234b63a2007-03-13 10:46:10 -0400227 btrfs_release_path(extent_root, &path);
Chris Masone089f052007-03-16 16:20:31 -0400228 finish_current_insert(trans, extent_root);
Chris Masona28ec192007-03-06 20:08:01 -0500229 return ret;
230}
231
232/*
Chris Masonfec577f2007-02-26 10:40:21 -0500233 * find all the blocks marked as pending in the radix tree and remove
234 * them from the extent map
235 */
Chris Masone089f052007-03-16 16:20:31 -0400236static int del_pending_extents(struct btrfs_trans_handle *trans, struct
237 btrfs_root *extent_root)
Chris Masonfec577f2007-02-26 10:40:21 -0500238{
239 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -0400240 int wret;
241 int err = 0;
Chris Mason8ef97622007-03-26 10:15:30 -0400242 unsigned long gang[4];
Chris Masonfec577f2007-02-26 10:40:21 -0500243 int i;
Chris Mason8ef97622007-03-26 10:15:30 -0400244 struct radix_tree_root *pending_radix;
245 struct radix_tree_root *pinned_radix;
246
247 pending_radix = &extent_root->fs_info->pending_del_radix;
248 pinned_radix = &extent_root->fs_info->pinned_radix;
Chris Masonfec577f2007-02-26 10:40:21 -0500249
250 while(1) {
Chris Mason8ef97622007-03-26 10:15:30 -0400251 ret = find_first_radix_bit(pending_radix, gang,
252 ARRAY_SIZE(gang));
Chris Masonfec577f2007-02-26 10:40:21 -0500253 if (!ret)
254 break;
255 for (i = 0; i < ret; i++) {
Chris Mason8ef97622007-03-26 10:15:30 -0400256 wret = set_radix_bit(pinned_radix, gang[i]);
257 BUG_ON(wret);
258 wret = clear_radix_bit(pending_radix, gang[i]);
259 BUG_ON(wret);
Chris Masond5719762007-03-23 10:01:08 -0400260 wret = __free_extent(trans, extent_root,
Chris Mason8ef97622007-03-26 10:15:30 -0400261 gang[i], 1, 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400262 if (wret)
263 err = wret;
Chris Masonfec577f2007-02-26 10:40:21 -0500264 }
265 }
Chris Masone20d96d2007-03-22 12:13:20 -0400266 return err;
Chris Masonfec577f2007-02-26 10:40:21 -0500267}
268
269/*
270 * remove an extent from the root, returns 0 on success
271 */
Chris Masone089f052007-03-16 16:20:31 -0400272int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
273 *root, u64 blocknr, u64 num_blocks, int pin)
Chris Masonfec577f2007-02-26 10:40:21 -0500274{
Chris Mason9f5fae22007-03-20 14:38:32 -0400275 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Masone20d96d2007-03-22 12:13:20 -0400276 struct buffer_head *t;
Chris Masonfec577f2007-02-26 10:40:21 -0500277 int pending_ret;
278 int ret;
Chris Masona28ec192007-03-06 20:08:01 -0500279
280 if (root == extent_root) {
281 t = find_tree_block(root, blocknr);
Chris Mason8ef97622007-03-26 10:15:30 -0400282 pin_down_block(root, blocknr, 1);
Chris Masona28ec192007-03-06 20:08:01 -0500283 return 0;
284 }
Chris Mason78fae272007-03-25 11:35:08 -0400285 ret = __free_extent(trans, root, blocknr, num_blocks, pin);
Chris Masone20d96d2007-03-22 12:13:20 -0400286 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
Chris Masonfec577f2007-02-26 10:40:21 -0500287 return ret ? ret : pending_ret;
288}
289
290/*
291 * walks the btree of allocated extents and find a hole of a given size.
292 * The key ins is changed to record the hole:
293 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -0400294 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -0500295 * ins->offset == number of blocks
296 * Any available blocks before search_start are skipped.
297 */
Chris Masone089f052007-03-16 16:20:31 -0400298static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
299 *orig_root, u64 num_blocks, u64 search_start, u64
300 search_end, struct btrfs_key *ins)
Chris Masonfec577f2007-02-26 10:40:21 -0500301{
Chris Mason234b63a2007-03-13 10:46:10 -0400302 struct btrfs_path path;
Chris Masone2fa7222007-03-12 16:22:34 -0400303 struct btrfs_key key;
Chris Masonfec577f2007-02-26 10:40:21 -0500304 int ret;
305 u64 hole_size = 0;
306 int slot = 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400307 u64 last_block = 0;
Chris Mason037e6392007-03-07 11:50:24 -0500308 u64 test_block;
Chris Masonfec577f2007-02-26 10:40:21 -0500309 int start_found;
Chris Mason234b63a2007-03-13 10:46:10 -0400310 struct btrfs_leaf *l;
Chris Mason9f5fae22007-03-20 14:38:32 -0400311 struct btrfs_root * root = orig_root->fs_info->extent_root;
Chris Mason0579da42007-03-07 16:15:30 -0500312 int total_needed = num_blocks;
Chris Masone20d96d2007-03-22 12:13:20 -0400313 int level;
Chris Masonfec577f2007-02-26 10:40:21 -0500314
Chris Masone20d96d2007-03-22 12:13:20 -0400315 level = btrfs_header_level(btrfs_buffer_header(root->node));
316 total_needed += (level + 1) * 3;
Chris Mason9f5fae22007-03-20 14:38:32 -0400317 if (root->fs_info->last_insert.objectid > search_start)
318 search_start = root->fs_info->last_insert.objectid;
Chris Mason62e27492007-03-15 12:56:47 -0400319
320 ins->flags = 0;
321 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
322
Chris Masonfec577f2007-02-26 10:40:21 -0500323check_failed:
Chris Mason234b63a2007-03-13 10:46:10 -0400324 btrfs_init_path(&path);
Chris Masonfec577f2007-02-26 10:40:21 -0500325 ins->objectid = search_start;
326 ins->offset = 0;
Chris Masonfec577f2007-02-26 10:40:21 -0500327 start_found = 0;
Chris Masone089f052007-03-16 16:20:31 -0400328 ret = btrfs_search_slot(trans, root, ins, &path, 0, 0);
Chris Mason0f70abe2007-02-28 16:46:22 -0500329 if (ret < 0)
330 goto error;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500331
Chris Mason0579da42007-03-07 16:15:30 -0500332 if (path.slots[0] > 0)
333 path.slots[0]--;
334
Chris Masonfec577f2007-02-26 10:40:21 -0500335 while (1) {
Chris Masone20d96d2007-03-22 12:13:20 -0400336 l = btrfs_buffer_leaf(path.nodes[0]);
Chris Masonfec577f2007-02-26 10:40:21 -0500337 slot = path.slots[0];
Chris Mason7518a232007-03-12 12:01:18 -0400338 if (slot >= btrfs_header_nritems(&l->header)) {
Chris Mason234b63a2007-03-13 10:46:10 -0400339 ret = btrfs_next_leaf(root, &path);
Chris Masonfec577f2007-02-26 10:40:21 -0500340 if (ret == 0)
341 continue;
Chris Mason0f70abe2007-02-28 16:46:22 -0500342 if (ret < 0)
343 goto error;
Chris Masonfec577f2007-02-26 10:40:21 -0500344 if (!start_found) {
345 ins->objectid = search_start;
Chris Mason037e6392007-03-07 11:50:24 -0500346 ins->offset = (u64)-1;
Chris Masonfec577f2007-02-26 10:40:21 -0500347 start_found = 1;
348 goto check_pending;
349 }
350 ins->objectid = last_block > search_start ?
351 last_block : search_start;
Chris Mason037e6392007-03-07 11:50:24 -0500352 ins->offset = (u64)-1;
Chris Masonfec577f2007-02-26 10:40:21 -0500353 goto check_pending;
354 }
Chris Masone2fa7222007-03-12 16:22:34 -0400355 btrfs_disk_key_to_cpu(&key, &l->items[slot].key);
356 if (key.objectid >= search_start) {
Chris Masonfec577f2007-02-26 10:40:21 -0500357 if (start_found) {
Chris Mason0579da42007-03-07 16:15:30 -0500358 if (last_block < search_start)
359 last_block = search_start;
Chris Masone2fa7222007-03-12 16:22:34 -0400360 hole_size = key.objectid - last_block;
Chris Mason037e6392007-03-07 11:50:24 -0500361 if (hole_size > total_needed) {
Chris Masonfec577f2007-02-26 10:40:21 -0500362 ins->objectid = last_block;
Chris Mason037e6392007-03-07 11:50:24 -0500363 ins->offset = hole_size;
Chris Masonfec577f2007-02-26 10:40:21 -0500364 goto check_pending;
365 }
Chris Mason0579da42007-03-07 16:15:30 -0500366 }
Chris Masonfec577f2007-02-26 10:40:21 -0500367 }
Chris Mason0579da42007-03-07 16:15:30 -0500368 start_found = 1;
Chris Masone2fa7222007-03-12 16:22:34 -0400369 last_block = key.objectid + key.offset;
Chris Masonfec577f2007-02-26 10:40:21 -0500370 path.slots[0]++;
371 }
372 // FIXME -ENOSPC
373check_pending:
374 /* we have to make sure we didn't find an extent that has already
375 * been allocated by the map tree or the original allocation
376 */
Chris Mason234b63a2007-03-13 10:46:10 -0400377 btrfs_release_path(root, &path);
Chris Masonfec577f2007-02-26 10:40:21 -0500378 BUG_ON(ins->objectid < search_start);
Chris Mason037e6392007-03-07 11:50:24 -0500379 for (test_block = ins->objectid;
380 test_block < ins->objectid + total_needed; test_block++) {
Chris Mason8ef97622007-03-26 10:15:30 -0400381 if (test_radix_bit(&root->fs_info->pinned_radix,
Chris Mason9f5fae22007-03-20 14:38:32 -0400382 test_block)) {
Chris Mason037e6392007-03-07 11:50:24 -0500383 search_start = test_block + 1;
Chris Masonfec577f2007-02-26 10:40:21 -0500384 goto check_failed;
385 }
386 }
Chris Mason9f5fae22007-03-20 14:38:32 -0400387 BUG_ON(root->fs_info->current_insert.offset);
388 root->fs_info->current_insert.offset = total_needed - num_blocks;
389 root->fs_info->current_insert.objectid = ins->objectid + num_blocks;
390 root->fs_info->current_insert.flags = 0;
391 root->fs_info->last_insert.objectid = ins->objectid;
Chris Mason037e6392007-03-07 11:50:24 -0500392 ins->offset = num_blocks;
Chris Masonfec577f2007-02-26 10:40:21 -0500393 return 0;
Chris Mason0f70abe2007-02-28 16:46:22 -0500394error:
Chris Mason234b63a2007-03-13 10:46:10 -0400395 btrfs_release_path(root, &path);
Chris Mason0f70abe2007-02-28 16:46:22 -0500396 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -0500397}
398
399/*
Chris Masonfec577f2007-02-26 10:40:21 -0500400 * finds a free extent and does all the dirty work required for allocation
401 * returns the key for the extent through ins, and a tree buffer for
402 * the first block of the extent through buf.
403 *
404 * returns 0 if everything worked, non-zero otherwise.
405 */
Chris Masondee26a92007-03-26 16:00:06 -0400406int btrfs_alloc_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masone089f052007-03-16 16:20:31 -0400407 *root, u64 num_blocks, u64 search_start, u64
408 search_end, u64 owner, struct btrfs_key *ins)
Chris Masonfec577f2007-02-26 10:40:21 -0500409{
410 int ret;
411 int pending_ret;
Chris Mason1261ec42007-03-20 20:35:03 -0400412 u64 super_blocks_used;
413 struct btrfs_fs_info *info = root->fs_info;
414 struct btrfs_root *extent_root = info->extent_root;
Chris Mason234b63a2007-03-13 10:46:10 -0400415 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -0500416
Chris Masoncf27e1e2007-03-13 09:49:06 -0400417 btrfs_set_extent_refs(&extent_item, 1);
418 btrfs_set_extent_owner(&extent_item, owner);
Chris Masonfec577f2007-02-26 10:40:21 -0500419
Chris Mason037e6392007-03-07 11:50:24 -0500420 if (root == extent_root) {
Chris Mason9f5fae22007-03-20 14:38:32 -0400421 BUG_ON(extent_root->fs_info->current_insert.offset == 0);
Chris Mason037e6392007-03-07 11:50:24 -0500422 BUG_ON(num_blocks != 1);
Chris Mason9f5fae22007-03-20 14:38:32 -0400423 BUG_ON(extent_root->fs_info->current_insert.flags ==
424 extent_root->fs_info->current_insert.offset);
Chris Mason037e6392007-03-07 11:50:24 -0500425 ins->offset = 1;
Chris Mason9f5fae22007-03-20 14:38:32 -0400426 ins->objectid = extent_root->fs_info->current_insert.objectid +
427 extent_root->fs_info->current_insert.flags++;
Chris Masonfec577f2007-02-26 10:40:21 -0500428 return 0;
429 }
Chris Masone089f052007-03-16 16:20:31 -0400430 ret = find_free_extent(trans, root, num_blocks, search_start,
Chris Mason037e6392007-03-07 11:50:24 -0500431 search_end, ins);
432 if (ret)
433 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -0500434
Chris Mason1261ec42007-03-20 20:35:03 -0400435 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
436 btrfs_set_super_blocks_used(info->disk_super, super_blocks_used +
437 num_blocks);
Chris Masone089f052007-03-16 16:20:31 -0400438 ret = btrfs_insert_item(trans, extent_root, ins, &extent_item,
439 sizeof(extent_item));
Chris Mason037e6392007-03-07 11:50:24 -0500440
Chris Masone089f052007-03-16 16:20:31 -0400441 finish_current_insert(trans, extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -0400442 pending_ret = del_pending_extents(trans, extent_root);
Chris Mason037e6392007-03-07 11:50:24 -0500443 if (ret)
444 return ret;
445 if (pending_ret)
446 return pending_ret;
447 return 0;
Chris Masonfec577f2007-02-26 10:40:21 -0500448}
449
450/*
451 * helper function to allocate a block for a given tree
452 * returns the tree buffer or NULL.
453 */
Chris Masone20d96d2007-03-22 12:13:20 -0400454struct buffer_head *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Chris Masone089f052007-03-16 16:20:31 -0400455 struct btrfs_root *root)
Chris Masonfec577f2007-02-26 10:40:21 -0500456{
Chris Masone2fa7222007-03-12 16:22:34 -0400457 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -0500458 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -0400459 struct buffer_head *buf;
Chris Masonfec577f2007-02-26 10:40:21 -0500460
Chris Masondee26a92007-03-26 16:00:06 -0400461 ret = btrfs_alloc_extent(trans, root, 1, 0, (unsigned long)-1,
Chris Masone20d96d2007-03-22 12:13:20 -0400462 btrfs_header_parentid(btrfs_buffer_header(root->node)), &ins);
Chris Masonfec577f2007-02-26 10:40:21 -0500463 if (ret) {
464 BUG();
465 return NULL;
466 }
Chris Mason037e6392007-03-07 11:50:24 -0500467 buf = find_tree_block(root, ins.objectid);
Chris Masondf2ce342007-03-23 11:00:45 -0400468 set_buffer_uptodate(buf);
Chris Masonfec577f2007-02-26 10:40:21 -0500469 return buf;
470}
Chris Masona28ec192007-03-06 20:08:01 -0500471
Chris Mason9aca1d52007-03-13 11:09:37 -0400472/*
473 * helper function for drop_snapshot, this walks down the tree dropping ref
474 * counts as it goes.
475 */
Chris Masone089f052007-03-16 16:20:31 -0400476static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
477 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -0500478{
Chris Masone20d96d2007-03-22 12:13:20 -0400479 struct buffer_head *next;
480 struct buffer_head *cur;
Chris Mason20524f02007-03-10 06:35:47 -0500481 u64 blocknr;
482 int ret;
483 u32 refs;
484
Chris Masone20d96d2007-03-22 12:13:20 -0400485 ret = lookup_block_ref(trans, root, path->nodes[*level]->b_blocknr,
Chris Masone089f052007-03-16 16:20:31 -0400486 &refs);
Chris Mason20524f02007-03-10 06:35:47 -0500487 BUG_ON(ret);
488 if (refs > 1)
489 goto out;
Chris Mason9aca1d52007-03-13 11:09:37 -0400490 /*
491 * walk down to the last node level and free all the leaves
492 */
Chris Mason20524f02007-03-10 06:35:47 -0500493 while(*level > 0) {
494 cur = path->nodes[*level];
Chris Mason7518a232007-03-12 12:01:18 -0400495 if (path->slots[*level] >=
Chris Masone20d96d2007-03-22 12:13:20 -0400496 btrfs_header_nritems(btrfs_buffer_header(cur)))
Chris Mason20524f02007-03-10 06:35:47 -0500497 break;
Chris Masone20d96d2007-03-22 12:13:20 -0400498 blocknr = btrfs_node_blockptr(btrfs_buffer_node(cur),
499 path->slots[*level]);
Chris Masone089f052007-03-16 16:20:31 -0400500 ret = lookup_block_ref(trans, root, blocknr, &refs);
Chris Mason20524f02007-03-10 06:35:47 -0500501 if (refs != 1 || *level == 1) {
502 path->slots[*level]++;
Chris Masone089f052007-03-16 16:20:31 -0400503 ret = btrfs_free_extent(trans, root, blocknr, 1, 1);
Chris Mason20524f02007-03-10 06:35:47 -0500504 BUG_ON(ret);
505 continue;
506 }
507 BUG_ON(ret);
508 next = read_tree_block(root, blocknr);
Chris Mason83e15a22007-03-12 09:03:27 -0400509 if (path->nodes[*level-1])
Chris Mason234b63a2007-03-13 10:46:10 -0400510 btrfs_block_release(root, path->nodes[*level-1]);
Chris Mason20524f02007-03-10 06:35:47 -0500511 path->nodes[*level-1] = next;
Chris Masone20d96d2007-03-22 12:13:20 -0400512 *level = btrfs_header_level(btrfs_buffer_header(next));
Chris Mason20524f02007-03-10 06:35:47 -0500513 path->slots[*level] = 0;
514 }
515out:
Chris Masone20d96d2007-03-22 12:13:20 -0400516 ret = btrfs_free_extent(trans, root, path->nodes[*level]->b_blocknr,
517 1, 1);
Chris Mason234b63a2007-03-13 10:46:10 -0400518 btrfs_block_release(root, path->nodes[*level]);
Chris Mason20524f02007-03-10 06:35:47 -0500519 path->nodes[*level] = NULL;
520 *level += 1;
521 BUG_ON(ret);
522 return 0;
523}
524
Chris Mason9aca1d52007-03-13 11:09:37 -0400525/*
526 * helper for dropping snapshots. This walks back up the tree in the path
527 * to find the first node higher up where we haven't yet gone through
528 * all the slots
529 */
Chris Masone089f052007-03-16 16:20:31 -0400530static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root
531 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -0500532{
533 int i;
534 int slot;
535 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400536 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Mason20524f02007-03-10 06:35:47 -0500537 slot = path->slots[i];
Chris Masone20d96d2007-03-22 12:13:20 -0400538 if (slot < btrfs_header_nritems(
539 btrfs_buffer_header(path->nodes[i])) - 1) {
Chris Mason20524f02007-03-10 06:35:47 -0500540 path->slots[i]++;
541 *level = i;
542 return 0;
543 } else {
Chris Masone089f052007-03-16 16:20:31 -0400544 ret = btrfs_free_extent(trans, root,
Chris Masone20d96d2007-03-22 12:13:20 -0400545 path->nodes[*level]->b_blocknr,
Chris Masone089f052007-03-16 16:20:31 -0400546 1, 1);
Chris Mason234b63a2007-03-13 10:46:10 -0400547 btrfs_block_release(root, path->nodes[*level]);
Chris Mason83e15a22007-03-12 09:03:27 -0400548 path->nodes[*level] = NULL;
Chris Mason20524f02007-03-10 06:35:47 -0500549 *level = i + 1;
550 BUG_ON(ret);
551 }
552 }
553 return 1;
554}
555
Chris Mason9aca1d52007-03-13 11:09:37 -0400556/*
557 * drop the reference count on the tree rooted at 'snap'. This traverses
558 * the tree freeing any blocks that have a ref count of zero after being
559 * decremented.
560 */
Chris Masone089f052007-03-16 16:20:31 -0400561int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masone20d96d2007-03-22 12:13:20 -0400562 *root, struct buffer_head *snap)
Chris Mason20524f02007-03-10 06:35:47 -0500563{
Chris Mason3768f362007-03-13 16:47:54 -0400564 int ret = 0;
Chris Mason9aca1d52007-03-13 11:09:37 -0400565 int wret;
Chris Mason20524f02007-03-10 06:35:47 -0500566 int level;
Chris Mason234b63a2007-03-13 10:46:10 -0400567 struct btrfs_path path;
Chris Mason20524f02007-03-10 06:35:47 -0500568 int i;
569 int orig_level;
570
Chris Mason234b63a2007-03-13 10:46:10 -0400571 btrfs_init_path(&path);
Chris Mason20524f02007-03-10 06:35:47 -0500572
Chris Masone20d96d2007-03-22 12:13:20 -0400573 level = btrfs_header_level(btrfs_buffer_header(snap));
Chris Mason20524f02007-03-10 06:35:47 -0500574 orig_level = level;
575 path.nodes[level] = snap;
576 path.slots[level] = 0;
577 while(1) {
Chris Masone089f052007-03-16 16:20:31 -0400578 wret = walk_down_tree(trans, root, &path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -0400579 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -0500580 break;
Chris Mason9aca1d52007-03-13 11:09:37 -0400581 if (wret < 0)
582 ret = wret;
583
Chris Masone089f052007-03-16 16:20:31 -0400584 wret = walk_up_tree(trans, root, &path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -0400585 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -0500586 break;
Chris Mason9aca1d52007-03-13 11:09:37 -0400587 if (wret < 0)
588 ret = wret;
Chris Mason20524f02007-03-10 06:35:47 -0500589 }
Chris Mason83e15a22007-03-12 09:03:27 -0400590 for (i = 0; i <= orig_level; i++) {
591 if (path.nodes[i]) {
Chris Mason234b63a2007-03-13 10:46:10 -0400592 btrfs_block_release(root, path.nodes[i]);
Chris Mason83e15a22007-03-12 09:03:27 -0400593 }
Chris Mason20524f02007-03-10 06:35:47 -0500594 }
Chris Mason9aca1d52007-03-13 11:09:37 -0400595 return ret;
Chris Mason20524f02007-03-10 06:35:47 -0500596}