blob: 09eeeb4d9d285c4f0aab5bacf039e5c7b7713e35 [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;
137
Chris Masoncf27e1e2007-03-13 09:49:06 -0400138 btrfs_set_extent_refs(&extent_item, 1);
139 btrfs_set_extent_owner(&extent_item,
140 btrfs_header_parentid(&extent_root->node->node.header));
Chris Mason037e6392007-03-07 11:50:24 -0500141 ins.offset = 1;
142 ins.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -0400143 btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
Chris Mason037e6392007-03-07 11:50:24 -0500144
Chris Mason9f5fae22007-03-20 14:38:32 -0400145 for (i = 0; i < extent_root->fs_info->current_insert.flags; i++) {
146 ins.objectid = extent_root->fs_info->current_insert.objectid +
147 i;
Chris Masone089f052007-03-16 16:20:31 -0400148 ret = btrfs_insert_item(trans, extent_root, &ins, &extent_item,
149 sizeof(extent_item));
Chris Mason037e6392007-03-07 11:50:24 -0500150 BUG_ON(ret);
151 }
Chris Mason9f5fae22007-03-20 14:38:32 -0400152 extent_root->fs_info->current_insert.offset = 0;
Chris Mason037e6392007-03-07 11:50:24 -0500153 return 0;
154}
155
Chris Masona28ec192007-03-06 20:08:01 -0500156/*
157 * remove an extent from the root, returns 0 on success
158 */
Chris Masone089f052007-03-16 16:20:31 -0400159static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
160 *root, u64 blocknr, u64 num_blocks, int pin)
Chris Masona28ec192007-03-06 20:08:01 -0500161{
Chris Mason234b63a2007-03-13 10:46:10 -0400162 struct btrfs_path path;
Chris Masone2fa7222007-03-12 16:22:34 -0400163 struct btrfs_key key;
Chris Mason9f5fae22007-03-20 14:38:32 -0400164 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Masona28ec192007-03-06 20:08:01 -0500165 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400166 struct btrfs_extent_item *ei;
Chris Masone2fa7222007-03-12 16:22:34 -0400167 struct btrfs_key ins;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400168 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500169
Chris Mason88fd1462007-03-16 08:56:18 -0400170 BUG_ON(pin && num_blocks != 1);
Chris Masona28ec192007-03-06 20:08:01 -0500171 key.objectid = blocknr;
172 key.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -0400173 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masona28ec192007-03-06 20:08:01 -0500174 key.offset = num_blocks;
175
Chris Masone089f052007-03-16 16:20:31 -0400176 find_free_extent(trans, root, 0, 0, (u64)-1, &ins);
Chris Mason234b63a2007-03-13 10:46:10 -0400177 btrfs_init_path(&path);
Chris Masone089f052007-03-16 16:20:31 -0400178 ret = btrfs_search_slot(trans, extent_root, &key, &path, -1, 1);
Chris Masona28ec192007-03-06 20:08:01 -0500179 if (ret) {
180 printf("failed to find %Lu\n", key.objectid);
Chris Mason234b63a2007-03-13 10:46:10 -0400181 btrfs_print_tree(extent_root, extent_root->node);
Chris Masona28ec192007-03-06 20:08:01 -0500182 printf("failed to find %Lu\n", key.objectid);
183 BUG();
184 }
Chris Mason123abc82007-03-14 14:14:43 -0400185 ei = btrfs_item_ptr(&path.nodes[0]->leaf, path.slots[0],
186 struct btrfs_extent_item);
Chris Masona28ec192007-03-06 20:08:01 -0500187 BUG_ON(ei->refs == 0);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400188 refs = btrfs_extent_refs(ei) - 1;
189 btrfs_set_extent_refs(ei, refs);
190 if (refs == 0) {
Chris Mason88fd1462007-03-16 08:56:18 -0400191 if (pin) {
Chris Masona28ec192007-03-06 20:08:01 -0500192 int err;
193 radix_tree_preload(GFP_KERNEL);
Chris Mason9f5fae22007-03-20 14:38:32 -0400194 err = radix_tree_insert(
195 &extent_root->fs_info->pinned_radix,
196 blocknr, (void *)blocknr);
Chris Masona28ec192007-03-06 20:08:01 -0500197 BUG_ON(err);
198 radix_tree_preload_end();
199 }
Chris Masone089f052007-03-16 16:20:31 -0400200 ret = btrfs_del_item(trans, extent_root, &path);
Chris Mason9f5fae22007-03-20 14:38:32 -0400201 if (!pin && extent_root->fs_info->last_insert.objectid >
202 blocknr)
203 extent_root->fs_info->last_insert.objectid = blocknr;
Chris Masona28ec192007-03-06 20:08:01 -0500204 if (ret)
205 BUG();
206 }
Chris Mason234b63a2007-03-13 10:46:10 -0400207 btrfs_release_path(extent_root, &path);
Chris Masone089f052007-03-16 16:20:31 -0400208 finish_current_insert(trans, extent_root);
Chris Masona28ec192007-03-06 20:08:01 -0500209 return ret;
210}
211
212/*
Chris Masonfec577f2007-02-26 10:40:21 -0500213 * find all the blocks marked as pending in the radix tree and remove
214 * them from the extent map
215 */
Chris Masone089f052007-03-16 16:20:31 -0400216static int del_pending_extents(struct btrfs_trans_handle *trans, struct
217 btrfs_root *extent_root)
Chris Masonfec577f2007-02-26 10:40:21 -0500218{
219 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400220 struct btrfs_buffer *gang[4];
Chris Masonfec577f2007-02-26 10:40:21 -0500221 int i;
Chris Masonfec577f2007-02-26 10:40:21 -0500222
223 while(1) {
Chris Mason9f5fae22007-03-20 14:38:32 -0400224 ret = radix_tree_gang_lookup_tag(
225 &extent_root->fs_info->cache_radix,
226 (void **)gang, 0,
227 ARRAY_SIZE(gang),
228 CTREE_EXTENT_PENDING_DEL);
Chris Masonfec577f2007-02-26 10:40:21 -0500229 if (!ret)
230 break;
231 for (i = 0; i < ret; i++) {
Chris Masone089f052007-03-16 16:20:31 -0400232 ret = __free_extent(trans, extent_root,
Chris Mason88fd1462007-03-16 08:56:18 -0400233 gang[i]->blocknr, 1, 1);
Chris Mason9f5fae22007-03-20 14:38:32 -0400234 radix_tree_tag_clear(&extent_root->fs_info->cache_radix,
235 gang[i]->blocknr,
236 CTREE_EXTENT_PENDING_DEL);
Chris Mason234b63a2007-03-13 10:46:10 -0400237 btrfs_block_release(extent_root, gang[i]);
Chris Masonfec577f2007-02-26 10:40:21 -0500238 }
239 }
240 return 0;
241}
242
Chris Masone089f052007-03-16 16:20:31 -0400243static int run_pending(struct btrfs_trans_handle *trans, struct btrfs_root
244 *extent_root)
Chris Masona28ec192007-03-06 20:08:01 -0500245{
Chris Mason9f5fae22007-03-20 14:38:32 -0400246 while(radix_tree_tagged(&extent_root->fs_info->cache_radix,
247 CTREE_EXTENT_PENDING_DEL))
Chris Masone089f052007-03-16 16:20:31 -0400248 del_pending_extents(trans, extent_root);
Chris Masona28ec192007-03-06 20:08:01 -0500249 return 0;
250}
251
252
Chris Masonfec577f2007-02-26 10:40:21 -0500253/*
254 * remove an extent from the root, returns 0 on success
255 */
Chris Masone089f052007-03-16 16:20:31 -0400256int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
257 *root, u64 blocknr, u64 num_blocks, int pin)
Chris Masonfec577f2007-02-26 10:40:21 -0500258{
Chris Mason9f5fae22007-03-20 14:38:32 -0400259 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Mason234b63a2007-03-13 10:46:10 -0400260 struct btrfs_buffer *t;
Chris Masonfec577f2007-02-26 10:40:21 -0500261 int pending_ret;
262 int ret;
Chris Masona28ec192007-03-06 20:08:01 -0500263
264 if (root == extent_root) {
265 t = find_tree_block(root, blocknr);
Chris Mason9f5fae22007-03-20 14:38:32 -0400266 radix_tree_tag_set(&root->fs_info->cache_radix, blocknr,
Chris Masona28ec192007-03-06 20:08:01 -0500267 CTREE_EXTENT_PENDING_DEL);
Chris Masona28ec192007-03-06 20:08:01 -0500268 return 0;
269 }
Chris Masone089f052007-03-16 16:20:31 -0400270 ret = __free_extent(trans, root, blocknr, num_blocks, pin);
Chris Mason9f5fae22007-03-20 14:38:32 -0400271 pending_ret = run_pending(trans, root->fs_info->extent_root);
Chris Masonfec577f2007-02-26 10:40:21 -0500272 return ret ? ret : pending_ret;
273}
274
275/*
276 * walks the btree of allocated extents and find a hole of a given size.
277 * The key ins is changed to record the hole:
278 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -0400279 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -0500280 * ins->offset == number of blocks
281 * Any available blocks before search_start are skipped.
282 */
Chris Masone089f052007-03-16 16:20:31 -0400283static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
284 *orig_root, u64 num_blocks, u64 search_start, u64
285 search_end, struct btrfs_key *ins)
Chris Masonfec577f2007-02-26 10:40:21 -0500286{
Chris Mason234b63a2007-03-13 10:46:10 -0400287 struct btrfs_path path;
Chris Masone2fa7222007-03-12 16:22:34 -0400288 struct btrfs_key key;
Chris Masonfec577f2007-02-26 10:40:21 -0500289 int ret;
290 u64 hole_size = 0;
291 int slot = 0;
292 u64 last_block;
Chris Mason037e6392007-03-07 11:50:24 -0500293 u64 test_block;
Chris Masonfec577f2007-02-26 10:40:21 -0500294 int start_found;
Chris Mason234b63a2007-03-13 10:46:10 -0400295 struct btrfs_leaf *l;
Chris Mason9f5fae22007-03-20 14:38:32 -0400296 struct btrfs_root * root = orig_root->fs_info->extent_root;
Chris Mason0579da42007-03-07 16:15:30 -0500297 int total_needed = num_blocks;
Chris Masonfec577f2007-02-26 10:40:21 -0500298
Chris Mason7518a232007-03-12 12:01:18 -0400299 total_needed += (btrfs_header_level(&root->node->node.header) + 1) * 3;
Chris Mason9f5fae22007-03-20 14:38:32 -0400300 if (root->fs_info->last_insert.objectid > search_start)
301 search_start = root->fs_info->last_insert.objectid;
Chris Mason62e27492007-03-15 12:56:47 -0400302
303 ins->flags = 0;
304 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
305
Chris Masonfec577f2007-02-26 10:40:21 -0500306check_failed:
Chris Mason234b63a2007-03-13 10:46:10 -0400307 btrfs_init_path(&path);
Chris Masonfec577f2007-02-26 10:40:21 -0500308 ins->objectid = search_start;
309 ins->offset = 0;
Chris Masonfec577f2007-02-26 10:40:21 -0500310 start_found = 0;
Chris Masone089f052007-03-16 16:20:31 -0400311 ret = btrfs_search_slot(trans, root, ins, &path, 0, 0);
Chris Mason0f70abe2007-02-28 16:46:22 -0500312 if (ret < 0)
313 goto error;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500314
Chris Mason0579da42007-03-07 16:15:30 -0500315 if (path.slots[0] > 0)
316 path.slots[0]--;
317
Chris Masonfec577f2007-02-26 10:40:21 -0500318 while (1) {
319 l = &path.nodes[0]->leaf;
320 slot = path.slots[0];
Chris Mason7518a232007-03-12 12:01:18 -0400321 if (slot >= btrfs_header_nritems(&l->header)) {
Chris Mason234b63a2007-03-13 10:46:10 -0400322 ret = btrfs_next_leaf(root, &path);
Chris Masonfec577f2007-02-26 10:40:21 -0500323 if (ret == 0)
324 continue;
Chris Mason0f70abe2007-02-28 16:46:22 -0500325 if (ret < 0)
326 goto error;
Chris Masonfec577f2007-02-26 10:40:21 -0500327 if (!start_found) {
328 ins->objectid = search_start;
Chris Mason037e6392007-03-07 11:50:24 -0500329 ins->offset = (u64)-1;
Chris Masonfec577f2007-02-26 10:40:21 -0500330 start_found = 1;
331 goto check_pending;
332 }
333 ins->objectid = last_block > search_start ?
334 last_block : search_start;
Chris Mason037e6392007-03-07 11:50:24 -0500335 ins->offset = (u64)-1;
Chris Masonfec577f2007-02-26 10:40:21 -0500336 goto check_pending;
337 }
Chris Masone2fa7222007-03-12 16:22:34 -0400338 btrfs_disk_key_to_cpu(&key, &l->items[slot].key);
339 if (key.objectid >= search_start) {
Chris Masonfec577f2007-02-26 10:40:21 -0500340 if (start_found) {
Chris Mason0579da42007-03-07 16:15:30 -0500341 if (last_block < search_start)
342 last_block = search_start;
Chris Masone2fa7222007-03-12 16:22:34 -0400343 hole_size = key.objectid - last_block;
Chris Mason037e6392007-03-07 11:50:24 -0500344 if (hole_size > total_needed) {
Chris Masonfec577f2007-02-26 10:40:21 -0500345 ins->objectid = last_block;
Chris Mason037e6392007-03-07 11:50:24 -0500346 ins->offset = hole_size;
Chris Masonfec577f2007-02-26 10:40:21 -0500347 goto check_pending;
348 }
Chris Mason0579da42007-03-07 16:15:30 -0500349 }
Chris Masonfec577f2007-02-26 10:40:21 -0500350 }
Chris Mason0579da42007-03-07 16:15:30 -0500351 start_found = 1;
Chris Masone2fa7222007-03-12 16:22:34 -0400352 last_block = key.objectid + key.offset;
Chris Masonfec577f2007-02-26 10:40:21 -0500353 path.slots[0]++;
354 }
355 // FIXME -ENOSPC
356check_pending:
357 /* we have to make sure we didn't find an extent that has already
358 * been allocated by the map tree or the original allocation
359 */
Chris Mason234b63a2007-03-13 10:46:10 -0400360 btrfs_release_path(root, &path);
Chris Masonfec577f2007-02-26 10:40:21 -0500361 BUG_ON(ins->objectid < search_start);
Chris Mason037e6392007-03-07 11:50:24 -0500362 for (test_block = ins->objectid;
363 test_block < ins->objectid + total_needed; test_block++) {
Chris Mason9f5fae22007-03-20 14:38:32 -0400364 if (radix_tree_lookup(&root->fs_info->pinned_radix,
365 test_block)) {
Chris Mason037e6392007-03-07 11:50:24 -0500366 search_start = test_block + 1;
Chris Masonfec577f2007-02-26 10:40:21 -0500367 goto check_failed;
368 }
369 }
Chris Mason9f5fae22007-03-20 14:38:32 -0400370 BUG_ON(root->fs_info->current_insert.offset);
371 root->fs_info->current_insert.offset = total_needed - num_blocks;
372 root->fs_info->current_insert.objectid = ins->objectid + num_blocks;
373 root->fs_info->current_insert.flags = 0;
374 root->fs_info->last_insert.objectid = ins->objectid;
Chris Mason037e6392007-03-07 11:50:24 -0500375 ins->offset = num_blocks;
Chris Masonfec577f2007-02-26 10:40:21 -0500376 return 0;
Chris Mason0f70abe2007-02-28 16:46:22 -0500377error:
Chris Mason234b63a2007-03-13 10:46:10 -0400378 btrfs_release_path(root, &path);
Chris Mason0f70abe2007-02-28 16:46:22 -0500379 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -0500380}
381
382/*
Chris Masonfec577f2007-02-26 10:40:21 -0500383 * finds a free extent and does all the dirty work required for allocation
384 * returns the key for the extent through ins, and a tree buffer for
385 * the first block of the extent through buf.
386 *
387 * returns 0 if everything worked, non-zero otherwise.
388 */
Chris Masone089f052007-03-16 16:20:31 -0400389static int alloc_extent(struct btrfs_trans_handle *trans, struct btrfs_root
390 *root, u64 num_blocks, u64 search_start, u64
391 search_end, u64 owner, struct btrfs_key *ins)
Chris Masonfec577f2007-02-26 10:40:21 -0500392{
393 int ret;
394 int pending_ret;
Chris Mason9f5fae22007-03-20 14:38:32 -0400395 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Mason234b63a2007-03-13 10:46:10 -0400396 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -0500397
Chris Masoncf27e1e2007-03-13 09:49:06 -0400398 btrfs_set_extent_refs(&extent_item, 1);
399 btrfs_set_extent_owner(&extent_item, owner);
Chris Masonfec577f2007-02-26 10:40:21 -0500400
Chris Mason037e6392007-03-07 11:50:24 -0500401 if (root == extent_root) {
Chris Mason9f5fae22007-03-20 14:38:32 -0400402 BUG_ON(extent_root->fs_info->current_insert.offset == 0);
Chris Mason037e6392007-03-07 11:50:24 -0500403 BUG_ON(num_blocks != 1);
Chris Mason9f5fae22007-03-20 14:38:32 -0400404 BUG_ON(extent_root->fs_info->current_insert.flags ==
405 extent_root->fs_info->current_insert.offset);
Chris Mason037e6392007-03-07 11:50:24 -0500406 ins->offset = 1;
Chris Mason9f5fae22007-03-20 14:38:32 -0400407 ins->objectid = extent_root->fs_info->current_insert.objectid +
408 extent_root->fs_info->current_insert.flags++;
Chris Masonfec577f2007-02-26 10:40:21 -0500409 return 0;
410 }
Chris Masone089f052007-03-16 16:20:31 -0400411 ret = find_free_extent(trans, root, num_blocks, search_start,
Chris Mason037e6392007-03-07 11:50:24 -0500412 search_end, ins);
413 if (ret)
414 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -0500415
Chris Masone089f052007-03-16 16:20:31 -0400416 ret = btrfs_insert_item(trans, extent_root, ins, &extent_item,
417 sizeof(extent_item));
Chris Mason037e6392007-03-07 11:50:24 -0500418
Chris Masone089f052007-03-16 16:20:31 -0400419 finish_current_insert(trans, extent_root);
420 pending_ret = run_pending(trans, extent_root);
Chris Mason037e6392007-03-07 11:50:24 -0500421 if (ret)
422 return ret;
423 if (pending_ret)
424 return pending_ret;
425 return 0;
Chris Masonfec577f2007-02-26 10:40:21 -0500426}
427
428/*
429 * helper function to allocate a block for a given tree
430 * returns the tree buffer or NULL.
431 */
Chris Masone089f052007-03-16 16:20:31 -0400432struct btrfs_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
433 struct btrfs_root *root)
Chris Masonfec577f2007-02-26 10:40:21 -0500434{
Chris Masone2fa7222007-03-12 16:22:34 -0400435 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -0500436 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400437 struct btrfs_buffer *buf;
Chris Masonfec577f2007-02-26 10:40:21 -0500438
Chris Masone089f052007-03-16 16:20:31 -0400439 ret = alloc_extent(trans, root, 1, 0, (unsigned long)-1,
Chris Mason7518a232007-03-12 12:01:18 -0400440 btrfs_header_parentid(&root->node->node.header),
Chris Mason037e6392007-03-07 11:50:24 -0500441 &ins);
Chris Masonfec577f2007-02-26 10:40:21 -0500442 if (ret) {
443 BUG();
444 return NULL;
445 }
Chris Mason037e6392007-03-07 11:50:24 -0500446 buf = find_tree_block(root, ins.objectid);
Chris Masone089f052007-03-16 16:20:31 -0400447 dirty_tree_block(trans, root, buf);
Chris Masonfec577f2007-02-26 10:40:21 -0500448 return buf;
449}
Chris Masona28ec192007-03-06 20:08:01 -0500450
Chris Mason9aca1d52007-03-13 11:09:37 -0400451/*
452 * helper function for drop_snapshot, this walks down the tree dropping ref
453 * counts as it goes.
454 */
Chris Masone089f052007-03-16 16:20:31 -0400455static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
456 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -0500457{
Chris Mason234b63a2007-03-13 10:46:10 -0400458 struct btrfs_buffer *next;
459 struct btrfs_buffer *cur;
Chris Mason20524f02007-03-10 06:35:47 -0500460 u64 blocknr;
461 int ret;
462 u32 refs;
463
Chris Masone089f052007-03-16 16:20:31 -0400464 ret = lookup_block_ref(trans, root, path->nodes[*level]->blocknr,
465 &refs);
Chris Mason20524f02007-03-10 06:35:47 -0500466 BUG_ON(ret);
467 if (refs > 1)
468 goto out;
Chris Mason9aca1d52007-03-13 11:09:37 -0400469 /*
470 * walk down to the last node level and free all the leaves
471 */
Chris Mason20524f02007-03-10 06:35:47 -0500472 while(*level > 0) {
473 cur = path->nodes[*level];
Chris Mason7518a232007-03-12 12:01:18 -0400474 if (path->slots[*level] >=
475 btrfs_header_nritems(&cur->node.header))
Chris Mason20524f02007-03-10 06:35:47 -0500476 break;
Chris Mason1d4f8a02007-03-13 09:28:32 -0400477 blocknr = btrfs_node_blockptr(&cur->node, path->slots[*level]);
Chris Masone089f052007-03-16 16:20:31 -0400478 ret = lookup_block_ref(trans, root, blocknr, &refs);
Chris Mason20524f02007-03-10 06:35:47 -0500479 if (refs != 1 || *level == 1) {
480 path->slots[*level]++;
Chris Masone089f052007-03-16 16:20:31 -0400481 ret = btrfs_free_extent(trans, root, blocknr, 1, 1);
Chris Mason20524f02007-03-10 06:35:47 -0500482 BUG_ON(ret);
483 continue;
484 }
485 BUG_ON(ret);
486 next = read_tree_block(root, blocknr);
Chris Mason83e15a22007-03-12 09:03:27 -0400487 if (path->nodes[*level-1])
Chris Mason234b63a2007-03-13 10:46:10 -0400488 btrfs_block_release(root, path->nodes[*level-1]);
Chris Mason20524f02007-03-10 06:35:47 -0500489 path->nodes[*level-1] = next;
Chris Mason7518a232007-03-12 12:01:18 -0400490 *level = btrfs_header_level(&next->node.header);
Chris Mason20524f02007-03-10 06:35:47 -0500491 path->slots[*level] = 0;
492 }
493out:
Chris Masone089f052007-03-16 16:20:31 -0400494 ret = btrfs_free_extent(trans, root, path->nodes[*level]->blocknr, 1,
495 1);
Chris Mason234b63a2007-03-13 10:46:10 -0400496 btrfs_block_release(root, path->nodes[*level]);
Chris Mason20524f02007-03-10 06:35:47 -0500497 path->nodes[*level] = NULL;
498 *level += 1;
499 BUG_ON(ret);
500 return 0;
501}
502
Chris Mason9aca1d52007-03-13 11:09:37 -0400503/*
504 * helper for dropping snapshots. This walks back up the tree in the path
505 * to find the first node higher up where we haven't yet gone through
506 * all the slots
507 */
Chris Masone089f052007-03-16 16:20:31 -0400508static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root
509 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -0500510{
511 int i;
512 int slot;
513 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400514 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Mason20524f02007-03-10 06:35:47 -0500515 slot = path->slots[i];
Chris Mason7518a232007-03-12 12:01:18 -0400516 if (slot <
517 btrfs_header_nritems(&path->nodes[i]->node.header)- 1) {
Chris Mason20524f02007-03-10 06:35:47 -0500518 path->slots[i]++;
519 *level = i;
520 return 0;
521 } else {
Chris Masone089f052007-03-16 16:20:31 -0400522 ret = btrfs_free_extent(trans, root,
523 path->nodes[*level]->blocknr,
524 1, 1);
Chris Mason234b63a2007-03-13 10:46:10 -0400525 btrfs_block_release(root, path->nodes[*level]);
Chris Mason83e15a22007-03-12 09:03:27 -0400526 path->nodes[*level] = NULL;
Chris Mason20524f02007-03-10 06:35:47 -0500527 *level = i + 1;
528 BUG_ON(ret);
529 }
530 }
531 return 1;
532}
533
Chris Mason9aca1d52007-03-13 11:09:37 -0400534/*
535 * drop the reference count on the tree rooted at 'snap'. This traverses
536 * the tree freeing any blocks that have a ref count of zero after being
537 * decremented.
538 */
Chris Masone089f052007-03-16 16:20:31 -0400539int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
540 *root, struct btrfs_buffer *snap)
Chris Mason20524f02007-03-10 06:35:47 -0500541{
Chris Mason3768f362007-03-13 16:47:54 -0400542 int ret = 0;
Chris Mason9aca1d52007-03-13 11:09:37 -0400543 int wret;
Chris Mason20524f02007-03-10 06:35:47 -0500544 int level;
Chris Mason234b63a2007-03-13 10:46:10 -0400545 struct btrfs_path path;
Chris Mason20524f02007-03-10 06:35:47 -0500546 int i;
547 int orig_level;
548
Chris Mason234b63a2007-03-13 10:46:10 -0400549 btrfs_init_path(&path);
Chris Mason20524f02007-03-10 06:35:47 -0500550
Chris Mason7518a232007-03-12 12:01:18 -0400551 level = btrfs_header_level(&snap->node.header);
Chris Mason20524f02007-03-10 06:35:47 -0500552 orig_level = level;
553 path.nodes[level] = snap;
554 path.slots[level] = 0;
555 while(1) {
Chris Masone089f052007-03-16 16:20:31 -0400556 wret = walk_down_tree(trans, root, &path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -0400557 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -0500558 break;
Chris Mason9aca1d52007-03-13 11:09:37 -0400559 if (wret < 0)
560 ret = wret;
561
Chris Masone089f052007-03-16 16:20:31 -0400562 wret = walk_up_tree(trans, root, &path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -0400563 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -0500564 break;
Chris Mason9aca1d52007-03-13 11:09:37 -0400565 if (wret < 0)
566 ret = wret;
Chris Mason20524f02007-03-10 06:35:47 -0500567 }
Chris Mason83e15a22007-03-12 09:03:27 -0400568 for (i = 0; i <= orig_level; i++) {
569 if (path.nodes[i]) {
Chris Mason234b63a2007-03-13 10:46:10 -0400570 btrfs_block_release(root, path.nodes[i]);
Chris Mason83e15a22007-03-12 09:03:27 -0400571 }
Chris Mason20524f02007-03-10 06:35:47 -0500572 }
Chris Mason9aca1d52007-03-13 11:09:37 -0400573 return ret;
Chris Mason20524f02007-03-10 06:35:47 -0500574}