blob: 9bc4ad38876dfa65a5114d04400658d04fb8294e [file] [log] [blame]
Chris Masonfec577f2007-02-26 10:40:21 -05001#include <stdio.h>
2#include <stdlib.h>
3#include "kerncompat.h"
4#include "radix-tree.h"
5#include "ctree.h"
6#include "disk-io.h"
7#include "print-tree.h"
Chris Masone089f052007-03-16 16:20:31 -04008#include "transaction.h"
Chris Masonfec577f2007-02-26 10:40:21 -05009
Chris Masone089f052007-03-16 16:20:31 -040010static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
11 *orig_root, u64 num_blocks, u64 search_start, u64
12 search_end, struct btrfs_key *ins);
13static int finish_current_insert(struct btrfs_trans_handle *trans, struct
14 btrfs_root *extent_root);
15static int run_pending(struct btrfs_trans_handle *trans, struct btrfs_root
16 *extent_root);
Chris Mason037e6392007-03-07 11:50:24 -050017
Chris Masonfec577f2007-02-26 10:40:21 -050018/*
19 * pending extents are blocks that we're trying to allocate in the extent
20 * map while trying to grow the map because of other allocations. To avoid
21 * recursing, they are tagged in the radix tree and cleaned up after
22 * other allocations are done. The pending tag is also used in the same
23 * manner for deletes.
24 */
Chris Mason037e6392007-03-07 11:50:24 -050025#define CTREE_EXTENT_PENDING_DEL 0
Chris Masonfec577f2007-02-26 10:40:21 -050026
Chris Masone089f052007-03-16 16:20:31 -040027static int inc_block_ref(struct btrfs_trans_handle *trans, struct btrfs_root
28 *root, u64 blocknr)
Chris Mason02217ed2007-03-02 16:08:05 -050029{
Chris Mason234b63a2007-03-13 10:46:10 -040030 struct btrfs_path path;
Chris Mason02217ed2007-03-02 16:08:05 -050031 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -040032 struct btrfs_key key;
Chris Mason234b63a2007-03-13 10:46:10 -040033 struct btrfs_leaf *l;
34 struct btrfs_extent_item *item;
Chris Masone2fa7222007-03-12 16:22:34 -040035 struct btrfs_key ins;
Chris Masoncf27e1e2007-03-13 09:49:06 -040036 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -050037
Chris Mason9f5fae22007-03-20 14:38:32 -040038 find_free_extent(trans, root->fs_info->extent_root, 0, 0, (u64)-1,
39 &ins);
Chris Mason234b63a2007-03-13 10:46:10 -040040 btrfs_init_path(&path);
Chris Mason02217ed2007-03-02 16:08:05 -050041 key.objectid = blocknr;
42 key.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -040043 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Mason02217ed2007-03-02 16:08:05 -050044 key.offset = 1;
Chris Mason9f5fae22007-03-20 14:38:32 -040045 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, &path,
46 0, 1);
Chris Masona28ec192007-03-06 20:08:01 -050047 if (ret != 0)
48 BUG();
Chris Mason02217ed2007-03-02 16:08:05 -050049 BUG_ON(ret != 0);
50 l = &path.nodes[0]->leaf;
Chris Mason4beb1b82007-03-14 10:31:29 -040051 item = btrfs_item_ptr(l, path.slots[0], struct btrfs_extent_item);
Chris Masoncf27e1e2007-03-13 09:49:06 -040052 refs = btrfs_extent_refs(item);
53 btrfs_set_extent_refs(item, refs + 1);
Chris Masona28ec192007-03-06 20:08:01 -050054
Chris Mason02217ed2007-03-02 16:08:05 -050055 BUG_ON(list_empty(&path.nodes[0]->dirty));
Chris Mason9f5fae22007-03-20 14:38:32 -040056 btrfs_release_path(root->fs_info->extent_root, &path);
57 finish_current_insert(trans, root->fs_info->extent_root);
58 run_pending(trans, root->fs_info->extent_root);
Chris Mason02217ed2007-03-02 16:08:05 -050059 return 0;
60}
61
Chris Masone089f052007-03-16 16:20:31 -040062static int lookup_block_ref(struct btrfs_trans_handle *trans, struct btrfs_root
63 *root, u64 blocknr, u32 *refs)
Chris Masona28ec192007-03-06 20:08:01 -050064{
Chris Mason234b63a2007-03-13 10:46:10 -040065 struct btrfs_path path;
Chris Masona28ec192007-03-06 20:08:01 -050066 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -040067 struct btrfs_key key;
Chris Mason234b63a2007-03-13 10:46:10 -040068 struct btrfs_leaf *l;
69 struct btrfs_extent_item *item;
70 btrfs_init_path(&path);
Chris Masona28ec192007-03-06 20:08:01 -050071 key.objectid = blocknr;
Chris Masona28ec192007-03-06 20:08:01 -050072 key.offset = 1;
Chris Mason62e27492007-03-15 12:56:47 -040073 key.flags = 0;
74 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Mason9f5fae22007-03-20 14:38:32 -040075 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, &path,
76 0, 0);
Chris Masona28ec192007-03-06 20:08:01 -050077 if (ret != 0)
78 BUG();
79 l = &path.nodes[0]->leaf;
Chris Mason4beb1b82007-03-14 10:31:29 -040080 item = btrfs_item_ptr(l, path.slots[0], struct btrfs_extent_item);
Chris Masoncf27e1e2007-03-13 09:49:06 -040081 *refs = btrfs_extent_refs(item);
Chris Mason9f5fae22007-03-20 14:38:32 -040082 btrfs_release_path(root->fs_info->extent_root, &path);
Chris Masona28ec192007-03-06 20:08:01 -050083 return 0;
84}
85
Chris Masone089f052007-03-16 16:20:31 -040086int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
87 struct btrfs_buffer *buf)
Chris Mason02217ed2007-03-02 16:08:05 -050088{
89 u64 blocknr;
90 int i;
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 Mason7518a232007-03-12 12:01:18 -040094 if (btrfs_is_leaf(&buf->node))
Chris Masona28ec192007-03-06 20:08:01 -050095 return 0;
96
Chris Mason7518a232007-03-12 12:01:18 -040097 for (i = 0; i < btrfs_header_nritems(&buf->node.header); i++) {
Chris Mason1d4f8a02007-03-13 09:28:32 -040098 blocknr = btrfs_node_blockptr(&buf->node, i);
Chris Masone089f052007-03-16 16:20:31 -040099 inc_block_ref(trans, root, blocknr);
Chris Mason02217ed2007-03-02 16:08:05 -0500100 }
101 return 0;
102}
103
Chris Masone089f052007-03-16 16:20:31 -0400104int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, struct
105 btrfs_root *root)
Chris Masona28ec192007-03-06 20:08:01 -0500106{
Chris Masona28ec192007-03-06 20:08:01 -0500107 unsigned long gang[8];
Chris Mason88fd1462007-03-16 08:56:18 -0400108 u64 first = 0;
Chris Masona28ec192007-03-06 20:08:01 -0500109 int ret;
110 int i;
111
112 while(1) {
Chris Mason9f5fae22007-03-20 14:38:32 -0400113 ret = radix_tree_gang_lookup(&root->fs_info->pinned_radix,
114 (void **)gang, 0,
115 ARRAY_SIZE(gang));
Chris Masona28ec192007-03-06 20:08:01 -0500116 if (!ret)
117 break;
Chris Mason88fd1462007-03-16 08:56:18 -0400118 if (!first)
119 first = gang[0];
Chris Mason0579da42007-03-07 16:15:30 -0500120 for (i = 0; i < ret; i++) {
Chris Mason9f5fae22007-03-20 14:38:32 -0400121 radix_tree_delete(&root->fs_info->pinned_radix,
122 gang[i]);
Chris Mason0579da42007-03-07 16:15:30 -0500123 }
Chris Masona28ec192007-03-06 20:08:01 -0500124 }
Chris Mason9f5fae22007-03-20 14:38:32 -0400125 root->fs_info->last_insert.objectid = first;
126 root->fs_info->last_insert.offset = 0;
Chris Masona28ec192007-03-06 20:08:01 -0500127 return 0;
128}
129
Chris Masone089f052007-03-16 16:20:31 -0400130static int finish_current_insert(struct btrfs_trans_handle *trans, struct
131 btrfs_root *extent_root)
Chris Mason037e6392007-03-07 11:50:24 -0500132{
Chris Masone2fa7222007-03-12 16:22:34 -0400133 struct btrfs_key ins;
Chris Mason234b63a2007-03-13 10:46:10 -0400134 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -0500135 int i;
136 int ret;
Chris Mason1261ec42007-03-20 20:35:03 -0400137 u64 super_blocks_used;
138 struct btrfs_fs_info *info = extent_root->fs_info;
Chris Mason037e6392007-03-07 11:50:24 -0500139
Chris Masoncf27e1e2007-03-13 09:49:06 -0400140 btrfs_set_extent_refs(&extent_item, 1);
141 btrfs_set_extent_owner(&extent_item,
142 btrfs_header_parentid(&extent_root->node->node.header));
Chris Mason037e6392007-03-07 11:50:24 -0500143 ins.offset = 1;
144 ins.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -0400145 btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
Chris Mason037e6392007-03-07 11:50:24 -0500146
Chris Mason9f5fae22007-03-20 14:38:32 -0400147 for (i = 0; i < extent_root->fs_info->current_insert.flags; i++) {
148 ins.objectid = extent_root->fs_info->current_insert.objectid +
149 i;
Chris Mason1261ec42007-03-20 20:35:03 -0400150 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
151 btrfs_set_super_blocks_used(info->disk_super,
152 super_blocks_used + 1);
Chris Masone089f052007-03-16 16:20:31 -0400153 ret = btrfs_insert_item(trans, extent_root, &ins, &extent_item,
154 sizeof(extent_item));
Chris Mason037e6392007-03-07 11:50:24 -0500155 BUG_ON(ret);
156 }
Chris Mason9f5fae22007-03-20 14:38:32 -0400157 extent_root->fs_info->current_insert.offset = 0;
Chris Mason037e6392007-03-07 11:50:24 -0500158 return 0;
159}
160
Chris Masona28ec192007-03-06 20:08:01 -0500161/*
162 * remove an extent from the root, returns 0 on success
163 */
Chris Masone089f052007-03-16 16:20:31 -0400164static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
165 *root, u64 blocknr, u64 num_blocks, int pin)
Chris Masona28ec192007-03-06 20:08:01 -0500166{
Chris Mason234b63a2007-03-13 10:46:10 -0400167 struct btrfs_path path;
Chris Masone2fa7222007-03-12 16:22:34 -0400168 struct btrfs_key key;
Chris Mason1261ec42007-03-20 20:35:03 -0400169 struct btrfs_fs_info *info = root->fs_info;
170 struct btrfs_root *extent_root = info->extent_root;
Chris Masona28ec192007-03-06 20:08:01 -0500171 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400172 struct btrfs_extent_item *ei;
Chris Masone2fa7222007-03-12 16:22:34 -0400173 struct btrfs_key ins;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400174 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500175
Chris Mason88fd1462007-03-16 08:56:18 -0400176 BUG_ON(pin && num_blocks != 1);
Chris Masona28ec192007-03-06 20:08:01 -0500177 key.objectid = blocknr;
178 key.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -0400179 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masona28ec192007-03-06 20:08:01 -0500180 key.offset = num_blocks;
181
Chris Masone089f052007-03-16 16:20:31 -0400182 find_free_extent(trans, root, 0, 0, (u64)-1, &ins);
Chris Mason234b63a2007-03-13 10:46:10 -0400183 btrfs_init_path(&path);
Chris Masone089f052007-03-16 16:20:31 -0400184 ret = btrfs_search_slot(trans, extent_root, &key, &path, -1, 1);
Chris Masona28ec192007-03-06 20:08:01 -0500185 if (ret) {
186 printf("failed to find %Lu\n", key.objectid);
Chris Mason234b63a2007-03-13 10:46:10 -0400187 btrfs_print_tree(extent_root, extent_root->node);
Chris Masona28ec192007-03-06 20:08:01 -0500188 printf("failed to find %Lu\n", key.objectid);
189 BUG();
190 }
Chris Mason123abc82007-03-14 14:14:43 -0400191 ei = btrfs_item_ptr(&path.nodes[0]->leaf, path.slots[0],
192 struct btrfs_extent_item);
Chris Masona28ec192007-03-06 20:08:01 -0500193 BUG_ON(ei->refs == 0);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400194 refs = btrfs_extent_refs(ei) - 1;
195 btrfs_set_extent_refs(ei, refs);
196 if (refs == 0) {
Chris Mason1261ec42007-03-20 20:35:03 -0400197 u64 super_blocks_used;
Chris Mason88fd1462007-03-16 08:56:18 -0400198 if (pin) {
Chris Masona28ec192007-03-06 20:08:01 -0500199 int err;
200 radix_tree_preload(GFP_KERNEL);
Chris Mason1261ec42007-03-20 20:35:03 -0400201 err = radix_tree_insert(&info->pinned_radix,
202 blocknr, (void *)blocknr);
Chris Masona28ec192007-03-06 20:08:01 -0500203 BUG_ON(err);
204 radix_tree_preload_end();
205 }
Chris Mason1261ec42007-03-20 20:35:03 -0400206 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
207 btrfs_set_super_blocks_used(info->disk_super,
208 super_blocks_used - num_blocks);
Chris Masone089f052007-03-16 16:20:31 -0400209 ret = btrfs_del_item(trans, extent_root, &path);
Chris Mason9f5fae22007-03-20 14:38:32 -0400210 if (!pin && extent_root->fs_info->last_insert.objectid >
211 blocknr)
212 extent_root->fs_info->last_insert.objectid = blocknr;
Chris Masona28ec192007-03-06 20:08:01 -0500213 if (ret)
214 BUG();
215 }
Chris Mason234b63a2007-03-13 10:46:10 -0400216 btrfs_release_path(extent_root, &path);
Chris Masone089f052007-03-16 16:20:31 -0400217 finish_current_insert(trans, extent_root);
Chris Masona28ec192007-03-06 20:08:01 -0500218 return ret;
219}
220
221/*
Chris Masonfec577f2007-02-26 10:40:21 -0500222 * find all the blocks marked as pending in the radix tree and remove
223 * them from the extent map
224 */
Chris Masone089f052007-03-16 16:20:31 -0400225static int del_pending_extents(struct btrfs_trans_handle *trans, struct
226 btrfs_root *extent_root)
Chris Masonfec577f2007-02-26 10:40:21 -0500227{
228 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400229 struct btrfs_buffer *gang[4];
Chris Masonfec577f2007-02-26 10:40:21 -0500230 int i;
Chris Masonfec577f2007-02-26 10:40:21 -0500231
232 while(1) {
Chris Mason9f5fae22007-03-20 14:38:32 -0400233 ret = radix_tree_gang_lookup_tag(
234 &extent_root->fs_info->cache_radix,
235 (void **)gang, 0,
236 ARRAY_SIZE(gang),
237 CTREE_EXTENT_PENDING_DEL);
Chris Masonfec577f2007-02-26 10:40:21 -0500238 if (!ret)
239 break;
240 for (i = 0; i < ret; i++) {
Chris Masone089f052007-03-16 16:20:31 -0400241 ret = __free_extent(trans, extent_root,
Chris Mason88fd1462007-03-16 08:56:18 -0400242 gang[i]->blocknr, 1, 1);
Chris Mason9f5fae22007-03-20 14:38:32 -0400243 radix_tree_tag_clear(&extent_root->fs_info->cache_radix,
244 gang[i]->blocknr,
245 CTREE_EXTENT_PENDING_DEL);
Chris Mason234b63a2007-03-13 10:46:10 -0400246 btrfs_block_release(extent_root, gang[i]);
Chris Masonfec577f2007-02-26 10:40:21 -0500247 }
248 }
249 return 0;
250}
251
Chris Masone089f052007-03-16 16:20:31 -0400252static int run_pending(struct btrfs_trans_handle *trans, struct btrfs_root
253 *extent_root)
Chris Masona28ec192007-03-06 20:08:01 -0500254{
Chris Mason9f5fae22007-03-20 14:38:32 -0400255 while(radix_tree_tagged(&extent_root->fs_info->cache_radix,
256 CTREE_EXTENT_PENDING_DEL))
Chris Masone089f052007-03-16 16:20:31 -0400257 del_pending_extents(trans, extent_root);
Chris Masona28ec192007-03-06 20:08:01 -0500258 return 0;
259}
260
261
Chris Masonfec577f2007-02-26 10:40:21 -0500262/*
263 * remove an extent from the root, returns 0 on success
264 */
Chris Masone089f052007-03-16 16:20:31 -0400265int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
266 *root, u64 blocknr, u64 num_blocks, int pin)
Chris Masonfec577f2007-02-26 10:40:21 -0500267{
Chris Mason9f5fae22007-03-20 14:38:32 -0400268 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Mason234b63a2007-03-13 10:46:10 -0400269 struct btrfs_buffer *t;
Chris Masonfec577f2007-02-26 10:40:21 -0500270 int pending_ret;
271 int ret;
Chris Masona28ec192007-03-06 20:08:01 -0500272
273 if (root == extent_root) {
274 t = find_tree_block(root, blocknr);
Chris Mason9f5fae22007-03-20 14:38:32 -0400275 radix_tree_tag_set(&root->fs_info->cache_radix, blocknr,
Chris Masona28ec192007-03-06 20:08:01 -0500276 CTREE_EXTENT_PENDING_DEL);
Chris Masona28ec192007-03-06 20:08:01 -0500277 return 0;
278 }
Chris Masone089f052007-03-16 16:20:31 -0400279 ret = __free_extent(trans, root, blocknr, num_blocks, pin);
Chris Mason9f5fae22007-03-20 14:38:32 -0400280 pending_ret = run_pending(trans, root->fs_info->extent_root);
Chris Masonfec577f2007-02-26 10:40:21 -0500281 return ret ? ret : pending_ret;
282}
283
284/*
285 * walks the btree of allocated extents and find a hole of a given size.
286 * The key ins is changed to record the hole:
287 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -0400288 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -0500289 * ins->offset == number of blocks
290 * Any available blocks before search_start are skipped.
291 */
Chris Masone089f052007-03-16 16:20:31 -0400292static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
293 *orig_root, u64 num_blocks, u64 search_start, u64
294 search_end, struct btrfs_key *ins)
Chris Masonfec577f2007-02-26 10:40:21 -0500295{
Chris Mason234b63a2007-03-13 10:46:10 -0400296 struct btrfs_path path;
Chris Masone2fa7222007-03-12 16:22:34 -0400297 struct btrfs_key key;
Chris Masonfec577f2007-02-26 10:40:21 -0500298 int ret;
299 u64 hole_size = 0;
300 int slot = 0;
301 u64 last_block;
Chris Mason037e6392007-03-07 11:50:24 -0500302 u64 test_block;
Chris Masonfec577f2007-02-26 10:40:21 -0500303 int start_found;
Chris Mason234b63a2007-03-13 10:46:10 -0400304 struct btrfs_leaf *l;
Chris Mason9f5fae22007-03-20 14:38:32 -0400305 struct btrfs_root * root = orig_root->fs_info->extent_root;
Chris Mason0579da42007-03-07 16:15:30 -0500306 int total_needed = num_blocks;
Chris Masonfec577f2007-02-26 10:40:21 -0500307
Chris Mason7518a232007-03-12 12:01:18 -0400308 total_needed += (btrfs_header_level(&root->node->node.header) + 1) * 3;
Chris Mason9f5fae22007-03-20 14:38:32 -0400309 if (root->fs_info->last_insert.objectid > search_start)
310 search_start = root->fs_info->last_insert.objectid;
Chris Mason62e27492007-03-15 12:56:47 -0400311
312 ins->flags = 0;
313 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
314
Chris Masonfec577f2007-02-26 10:40:21 -0500315check_failed:
Chris Mason234b63a2007-03-13 10:46:10 -0400316 btrfs_init_path(&path);
Chris Masonfec577f2007-02-26 10:40:21 -0500317 ins->objectid = search_start;
318 ins->offset = 0;
Chris Masonfec577f2007-02-26 10:40:21 -0500319 start_found = 0;
Chris Masone089f052007-03-16 16:20:31 -0400320 ret = btrfs_search_slot(trans, root, ins, &path, 0, 0);
Chris Mason0f70abe2007-02-28 16:46:22 -0500321 if (ret < 0)
322 goto error;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500323
Chris Mason0579da42007-03-07 16:15:30 -0500324 if (path.slots[0] > 0)
325 path.slots[0]--;
326
Chris Masonfec577f2007-02-26 10:40:21 -0500327 while (1) {
328 l = &path.nodes[0]->leaf;
329 slot = path.slots[0];
Chris Mason7518a232007-03-12 12:01:18 -0400330 if (slot >= btrfs_header_nritems(&l->header)) {
Chris Mason234b63a2007-03-13 10:46:10 -0400331 ret = btrfs_next_leaf(root, &path);
Chris Masonfec577f2007-02-26 10:40:21 -0500332 if (ret == 0)
333 continue;
Chris Mason0f70abe2007-02-28 16:46:22 -0500334 if (ret < 0)
335 goto error;
Chris Masonfec577f2007-02-26 10:40:21 -0500336 if (!start_found) {
337 ins->objectid = search_start;
Chris Mason037e6392007-03-07 11:50:24 -0500338 ins->offset = (u64)-1;
Chris Masonfec577f2007-02-26 10:40:21 -0500339 start_found = 1;
340 goto check_pending;
341 }
342 ins->objectid = last_block > search_start ?
343 last_block : search_start;
Chris Mason037e6392007-03-07 11:50:24 -0500344 ins->offset = (u64)-1;
Chris Masonfec577f2007-02-26 10:40:21 -0500345 goto check_pending;
346 }
Chris Masone2fa7222007-03-12 16:22:34 -0400347 btrfs_disk_key_to_cpu(&key, &l->items[slot].key);
348 if (key.objectid >= search_start) {
Chris Masonfec577f2007-02-26 10:40:21 -0500349 if (start_found) {
Chris Mason0579da42007-03-07 16:15:30 -0500350 if (last_block < search_start)
351 last_block = search_start;
Chris Masone2fa7222007-03-12 16:22:34 -0400352 hole_size = key.objectid - last_block;
Chris Mason037e6392007-03-07 11:50:24 -0500353 if (hole_size > total_needed) {
Chris Masonfec577f2007-02-26 10:40:21 -0500354 ins->objectid = last_block;
Chris Mason037e6392007-03-07 11:50:24 -0500355 ins->offset = hole_size;
Chris Masonfec577f2007-02-26 10:40:21 -0500356 goto check_pending;
357 }
Chris Mason0579da42007-03-07 16:15:30 -0500358 }
Chris Masonfec577f2007-02-26 10:40:21 -0500359 }
Chris Mason0579da42007-03-07 16:15:30 -0500360 start_found = 1;
Chris Masone2fa7222007-03-12 16:22:34 -0400361 last_block = key.objectid + key.offset;
Chris Masonfec577f2007-02-26 10:40:21 -0500362 path.slots[0]++;
363 }
364 // FIXME -ENOSPC
365check_pending:
366 /* we have to make sure we didn't find an extent that has already
367 * been allocated by the map tree or the original allocation
368 */
Chris Mason234b63a2007-03-13 10:46:10 -0400369 btrfs_release_path(root, &path);
Chris Masonfec577f2007-02-26 10:40:21 -0500370 BUG_ON(ins->objectid < search_start);
Chris Mason037e6392007-03-07 11:50:24 -0500371 for (test_block = ins->objectid;
372 test_block < ins->objectid + total_needed; test_block++) {
Chris Mason9f5fae22007-03-20 14:38:32 -0400373 if (radix_tree_lookup(&root->fs_info->pinned_radix,
374 test_block)) {
Chris Mason037e6392007-03-07 11:50:24 -0500375 search_start = test_block + 1;
Chris Masonfec577f2007-02-26 10:40:21 -0500376 goto check_failed;
377 }
378 }
Chris Mason9f5fae22007-03-20 14:38:32 -0400379 BUG_ON(root->fs_info->current_insert.offset);
380 root->fs_info->current_insert.offset = total_needed - num_blocks;
381 root->fs_info->current_insert.objectid = ins->objectid + num_blocks;
382 root->fs_info->current_insert.flags = 0;
383 root->fs_info->last_insert.objectid = ins->objectid;
Chris Mason037e6392007-03-07 11:50:24 -0500384 ins->offset = num_blocks;
Chris Masonfec577f2007-02-26 10:40:21 -0500385 return 0;
Chris Mason0f70abe2007-02-28 16:46:22 -0500386error:
Chris Mason234b63a2007-03-13 10:46:10 -0400387 btrfs_release_path(root, &path);
Chris Mason0f70abe2007-02-28 16:46:22 -0500388 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -0500389}
390
391/*
Chris Masonfec577f2007-02-26 10:40:21 -0500392 * finds a free extent and does all the dirty work required for allocation
393 * returns the key for the extent through ins, and a tree buffer for
394 * the first block of the extent through buf.
395 *
396 * returns 0 if everything worked, non-zero otherwise.
397 */
Chris Masone089f052007-03-16 16:20:31 -0400398static int alloc_extent(struct btrfs_trans_handle *trans, struct btrfs_root
399 *root, u64 num_blocks, u64 search_start, u64
400 search_end, u64 owner, struct btrfs_key *ins)
Chris Masonfec577f2007-02-26 10:40:21 -0500401{
402 int ret;
403 int pending_ret;
Chris Mason1261ec42007-03-20 20:35:03 -0400404 u64 super_blocks_used;
405 struct btrfs_fs_info *info = root->fs_info;
406 struct btrfs_root *extent_root = info->extent_root;
Chris Mason234b63a2007-03-13 10:46:10 -0400407 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -0500408
Chris Masoncf27e1e2007-03-13 09:49:06 -0400409 btrfs_set_extent_refs(&extent_item, 1);
410 btrfs_set_extent_owner(&extent_item, owner);
Chris Masonfec577f2007-02-26 10:40:21 -0500411
Chris Mason037e6392007-03-07 11:50:24 -0500412 if (root == extent_root) {
Chris Mason9f5fae22007-03-20 14:38:32 -0400413 BUG_ON(extent_root->fs_info->current_insert.offset == 0);
Chris Mason037e6392007-03-07 11:50:24 -0500414 BUG_ON(num_blocks != 1);
Chris Mason9f5fae22007-03-20 14:38:32 -0400415 BUG_ON(extent_root->fs_info->current_insert.flags ==
416 extent_root->fs_info->current_insert.offset);
Chris Mason037e6392007-03-07 11:50:24 -0500417 ins->offset = 1;
Chris Mason9f5fae22007-03-20 14:38:32 -0400418 ins->objectid = extent_root->fs_info->current_insert.objectid +
419 extent_root->fs_info->current_insert.flags++;
Chris Masonfec577f2007-02-26 10:40:21 -0500420 return 0;
421 }
Chris Masone089f052007-03-16 16:20:31 -0400422 ret = find_free_extent(trans, root, num_blocks, search_start,
Chris Mason037e6392007-03-07 11:50:24 -0500423 search_end, ins);
424 if (ret)
425 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -0500426
Chris Mason1261ec42007-03-20 20:35:03 -0400427 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
428 btrfs_set_super_blocks_used(info->disk_super, super_blocks_used +
429 num_blocks);
Chris Masone089f052007-03-16 16:20:31 -0400430 ret = btrfs_insert_item(trans, extent_root, ins, &extent_item,
431 sizeof(extent_item));
Chris Mason037e6392007-03-07 11:50:24 -0500432
Chris Masone089f052007-03-16 16:20:31 -0400433 finish_current_insert(trans, extent_root);
434 pending_ret = run_pending(trans, extent_root);
Chris Mason037e6392007-03-07 11:50:24 -0500435 if (ret)
436 return ret;
437 if (pending_ret)
438 return pending_ret;
439 return 0;
Chris Masonfec577f2007-02-26 10:40:21 -0500440}
441
442/*
443 * helper function to allocate a block for a given tree
444 * returns the tree buffer or NULL.
445 */
Chris Masone089f052007-03-16 16:20:31 -0400446struct btrfs_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
447 struct btrfs_root *root)
Chris Masonfec577f2007-02-26 10:40:21 -0500448{
Chris Masone2fa7222007-03-12 16:22:34 -0400449 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -0500450 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400451 struct btrfs_buffer *buf;
Chris Masonfec577f2007-02-26 10:40:21 -0500452
Chris Masone089f052007-03-16 16:20:31 -0400453 ret = alloc_extent(trans, root, 1, 0, (unsigned long)-1,
Chris Mason7518a232007-03-12 12:01:18 -0400454 btrfs_header_parentid(&root->node->node.header),
Chris Mason037e6392007-03-07 11:50:24 -0500455 &ins);
Chris Masonfec577f2007-02-26 10:40:21 -0500456 if (ret) {
457 BUG();
458 return NULL;
459 }
Chris Mason037e6392007-03-07 11:50:24 -0500460 buf = find_tree_block(root, ins.objectid);
Chris Masone089f052007-03-16 16:20:31 -0400461 dirty_tree_block(trans, root, buf);
Chris Masonfec577f2007-02-26 10:40:21 -0500462 return buf;
463}
Chris Masona28ec192007-03-06 20:08:01 -0500464
Chris Mason9aca1d52007-03-13 11:09:37 -0400465/*
466 * helper function for drop_snapshot, this walks down the tree dropping ref
467 * counts as it goes.
468 */
Chris Masone089f052007-03-16 16:20:31 -0400469static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
470 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -0500471{
Chris Mason234b63a2007-03-13 10:46:10 -0400472 struct btrfs_buffer *next;
473 struct btrfs_buffer *cur;
Chris Mason20524f02007-03-10 06:35:47 -0500474 u64 blocknr;
475 int ret;
476 u32 refs;
477
Chris Masone089f052007-03-16 16:20:31 -0400478 ret = lookup_block_ref(trans, root, path->nodes[*level]->blocknr,
479 &refs);
Chris Mason20524f02007-03-10 06:35:47 -0500480 BUG_ON(ret);
481 if (refs > 1)
482 goto out;
Chris Mason9aca1d52007-03-13 11:09:37 -0400483 /*
484 * walk down to the last node level and free all the leaves
485 */
Chris Mason20524f02007-03-10 06:35:47 -0500486 while(*level > 0) {
487 cur = path->nodes[*level];
Chris Mason7518a232007-03-12 12:01:18 -0400488 if (path->slots[*level] >=
489 btrfs_header_nritems(&cur->node.header))
Chris Mason20524f02007-03-10 06:35:47 -0500490 break;
Chris Mason1d4f8a02007-03-13 09:28:32 -0400491 blocknr = btrfs_node_blockptr(&cur->node, path->slots[*level]);
Chris Masone089f052007-03-16 16:20:31 -0400492 ret = lookup_block_ref(trans, root, blocknr, &refs);
Chris Mason20524f02007-03-10 06:35:47 -0500493 if (refs != 1 || *level == 1) {
494 path->slots[*level]++;
Chris Masone089f052007-03-16 16:20:31 -0400495 ret = btrfs_free_extent(trans, root, blocknr, 1, 1);
Chris Mason20524f02007-03-10 06:35:47 -0500496 BUG_ON(ret);
497 continue;
498 }
499 BUG_ON(ret);
500 next = read_tree_block(root, blocknr);
Chris Mason83e15a22007-03-12 09:03:27 -0400501 if (path->nodes[*level-1])
Chris Mason234b63a2007-03-13 10:46:10 -0400502 btrfs_block_release(root, path->nodes[*level-1]);
Chris Mason20524f02007-03-10 06:35:47 -0500503 path->nodes[*level-1] = next;
Chris Mason7518a232007-03-12 12:01:18 -0400504 *level = btrfs_header_level(&next->node.header);
Chris Mason20524f02007-03-10 06:35:47 -0500505 path->slots[*level] = 0;
506 }
507out:
Chris Masone089f052007-03-16 16:20:31 -0400508 ret = btrfs_free_extent(trans, root, path->nodes[*level]->blocknr, 1,
509 1);
Chris Mason234b63a2007-03-13 10:46:10 -0400510 btrfs_block_release(root, path->nodes[*level]);
Chris Mason20524f02007-03-10 06:35:47 -0500511 path->nodes[*level] = NULL;
512 *level += 1;
513 BUG_ON(ret);
514 return 0;
515}
516
Chris Mason9aca1d52007-03-13 11:09:37 -0400517/*
518 * helper for dropping snapshots. This walks back up the tree in the path
519 * to find the first node higher up where we haven't yet gone through
520 * all the slots
521 */
Chris Masone089f052007-03-16 16:20:31 -0400522static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root
523 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -0500524{
525 int i;
526 int slot;
527 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400528 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Mason20524f02007-03-10 06:35:47 -0500529 slot = path->slots[i];
Chris Mason7518a232007-03-12 12:01:18 -0400530 if (slot <
531 btrfs_header_nritems(&path->nodes[i]->node.header)- 1) {
Chris Mason20524f02007-03-10 06:35:47 -0500532 path->slots[i]++;
533 *level = i;
534 return 0;
535 } else {
Chris Masone089f052007-03-16 16:20:31 -0400536 ret = btrfs_free_extent(trans, root,
537 path->nodes[*level]->blocknr,
538 1, 1);
Chris Mason234b63a2007-03-13 10:46:10 -0400539 btrfs_block_release(root, path->nodes[*level]);
Chris Mason83e15a22007-03-12 09:03:27 -0400540 path->nodes[*level] = NULL;
Chris Mason20524f02007-03-10 06:35:47 -0500541 *level = i + 1;
542 BUG_ON(ret);
543 }
544 }
545 return 1;
546}
547
Chris Mason9aca1d52007-03-13 11:09:37 -0400548/*
549 * drop the reference count on the tree rooted at 'snap'. This traverses
550 * the tree freeing any blocks that have a ref count of zero after being
551 * decremented.
552 */
Chris Masone089f052007-03-16 16:20:31 -0400553int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
554 *root, struct btrfs_buffer *snap)
Chris Mason20524f02007-03-10 06:35:47 -0500555{
Chris Mason3768f362007-03-13 16:47:54 -0400556 int ret = 0;
Chris Mason9aca1d52007-03-13 11:09:37 -0400557 int wret;
Chris Mason20524f02007-03-10 06:35:47 -0500558 int level;
Chris Mason234b63a2007-03-13 10:46:10 -0400559 struct btrfs_path path;
Chris Mason20524f02007-03-10 06:35:47 -0500560 int i;
561 int orig_level;
562
Chris Mason234b63a2007-03-13 10:46:10 -0400563 btrfs_init_path(&path);
Chris Mason20524f02007-03-10 06:35:47 -0500564
Chris Mason7518a232007-03-12 12:01:18 -0400565 level = btrfs_header_level(&snap->node.header);
Chris Mason20524f02007-03-10 06:35:47 -0500566 orig_level = level;
567 path.nodes[level] = snap;
568 path.slots[level] = 0;
569 while(1) {
Chris Masone089f052007-03-16 16:20:31 -0400570 wret = walk_down_tree(trans, root, &path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -0400571 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -0500572 break;
Chris Mason9aca1d52007-03-13 11:09:37 -0400573 if (wret < 0)
574 ret = wret;
575
Chris Masone089f052007-03-16 16:20:31 -0400576 wret = walk_up_tree(trans, root, &path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -0400577 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -0500578 break;
Chris Mason9aca1d52007-03-13 11:09:37 -0400579 if (wret < 0)
580 ret = wret;
Chris Mason20524f02007-03-10 06:35:47 -0500581 }
Chris Mason83e15a22007-03-12 09:03:27 -0400582 for (i = 0; i <= orig_level; i++) {
583 if (path.nodes[i]) {
Chris Mason234b63a2007-03-13 10:46:10 -0400584 btrfs_block_release(root, path.nodes[i]);
Chris Mason83e15a22007-03-12 09:03:27 -0400585 }
Chris Mason20524f02007-03-10 06:35:47 -0500586 }
Chris Mason9aca1d52007-03-13 11:09:37 -0400587 return ret;
Chris Mason20524f02007-03-10 06:35:47 -0500588}