blob: 21f39b40469785f0be48a2a3771053e86eab3395 [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"
8
Chris Mason234b63a2007-03-13 10:46:10 -04009static int find_free_extent(struct btrfs_root *orig_root, u64 num_blocks,
Chris Masone2fa7222007-03-12 16:22:34 -040010 u64 search_start, u64 search_end,
11 struct btrfs_key *ins);
Chris Mason234b63a2007-03-13 10:46:10 -040012static int finish_current_insert(struct btrfs_root *extent_root);
13static int run_pending(struct btrfs_root *extent_root);
Chris Mason037e6392007-03-07 11:50:24 -050014
Chris Masonfec577f2007-02-26 10:40:21 -050015/*
16 * pending extents are blocks that we're trying to allocate in the extent
17 * map while trying to grow the map because of other allocations. To avoid
18 * recursing, they are tagged in the radix tree and cleaned up after
19 * other allocations are done. The pending tag is also used in the same
20 * manner for deletes.
21 */
Chris Mason037e6392007-03-07 11:50:24 -050022#define CTREE_EXTENT_PENDING_DEL 0
Chris Masonfec577f2007-02-26 10:40:21 -050023
Chris Mason234b63a2007-03-13 10:46:10 -040024static int inc_block_ref(struct btrfs_root *root, u64 blocknr)
Chris Mason02217ed2007-03-02 16:08:05 -050025{
Chris Mason234b63a2007-03-13 10:46:10 -040026 struct btrfs_path path;
Chris Mason02217ed2007-03-02 16:08:05 -050027 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -040028 struct btrfs_key key;
Chris Mason234b63a2007-03-13 10:46:10 -040029 struct btrfs_leaf *l;
30 struct btrfs_extent_item *item;
Chris Masone2fa7222007-03-12 16:22:34 -040031 struct btrfs_key ins;
Chris Masoncf27e1e2007-03-13 09:49:06 -040032 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -050033
34 find_free_extent(root->extent_root, 0, 0, (u64)-1, &ins);
Chris Mason234b63a2007-03-13 10:46:10 -040035 btrfs_init_path(&path);
Chris Mason02217ed2007-03-02 16:08:05 -050036 key.objectid = blocknr;
37 key.flags = 0;
38 key.offset = 1;
Chris Mason234b63a2007-03-13 10:46:10 -040039 ret = btrfs_search_slot(root->extent_root, &key, &path, 0, 1);
Chris Masona28ec192007-03-06 20:08:01 -050040 if (ret != 0)
41 BUG();
Chris Mason02217ed2007-03-02 16:08:05 -050042 BUG_ON(ret != 0);
43 l = &path.nodes[0]->leaf;
Chris Mason234b63a2007-03-13 10:46:10 -040044 item = (struct btrfs_extent_item *)(l->data +
45 btrfs_item_offset(l->items +
46 path.slots[0]));
Chris Masoncf27e1e2007-03-13 09:49:06 -040047 refs = btrfs_extent_refs(item);
48 btrfs_set_extent_refs(item, refs + 1);
Chris Masona28ec192007-03-06 20:08:01 -050049
Chris Mason02217ed2007-03-02 16:08:05 -050050 BUG_ON(list_empty(&path.nodes[0]->dirty));
Chris Mason234b63a2007-03-13 10:46:10 -040051 btrfs_release_path(root->extent_root, &path);
Chris Mason037e6392007-03-07 11:50:24 -050052 finish_current_insert(root->extent_root);
53 run_pending(root->extent_root);
Chris Mason02217ed2007-03-02 16:08:05 -050054 return 0;
55}
56
Chris Mason234b63a2007-03-13 10:46:10 -040057static int lookup_block_ref(struct btrfs_root *root, u64 blocknr, u32 *refs)
Chris Masona28ec192007-03-06 20:08:01 -050058{
Chris Mason234b63a2007-03-13 10:46:10 -040059 struct btrfs_path path;
Chris Masona28ec192007-03-06 20:08:01 -050060 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -040061 struct btrfs_key key;
Chris Mason234b63a2007-03-13 10:46:10 -040062 struct btrfs_leaf *l;
63 struct btrfs_extent_item *item;
64 btrfs_init_path(&path);
Chris Masona28ec192007-03-06 20:08:01 -050065 key.objectid = blocknr;
66 key.flags = 0;
67 key.offset = 1;
Chris Mason234b63a2007-03-13 10:46:10 -040068 ret = btrfs_search_slot(root->extent_root, &key, &path, 0, 0);
Chris Masona28ec192007-03-06 20:08:01 -050069 if (ret != 0)
70 BUG();
71 l = &path.nodes[0]->leaf;
Chris Mason234b63a2007-03-13 10:46:10 -040072 item = (struct btrfs_extent_item *)(l->data +
Chris Mason0783fcf2007-03-12 20:12:07 -040073 btrfs_item_offset(l->items +
74 path.slots[0]));
Chris Masoncf27e1e2007-03-13 09:49:06 -040075 *refs = btrfs_extent_refs(item);
Chris Mason234b63a2007-03-13 10:46:10 -040076 btrfs_release_path(root->extent_root, &path);
Chris Masona28ec192007-03-06 20:08:01 -050077 return 0;
78}
79
Chris Mason234b63a2007-03-13 10:46:10 -040080int btrfs_inc_ref(struct btrfs_root *root, struct btrfs_buffer *buf)
Chris Mason02217ed2007-03-02 16:08:05 -050081{
82 u64 blocknr;
83 int i;
Chris Masona28ec192007-03-06 20:08:01 -050084
Chris Mason3768f362007-03-13 16:47:54 -040085 if (!root->ref_cows)
Chris Masona28ec192007-03-06 20:08:01 -050086 return 0;
Chris Mason7518a232007-03-12 12:01:18 -040087 if (btrfs_is_leaf(&buf->node))
Chris Masona28ec192007-03-06 20:08:01 -050088 return 0;
89
Chris Mason7518a232007-03-12 12:01:18 -040090 for (i = 0; i < btrfs_header_nritems(&buf->node.header); i++) {
Chris Mason1d4f8a02007-03-13 09:28:32 -040091 blocknr = btrfs_node_blockptr(&buf->node, i);
Chris Mason02217ed2007-03-02 16:08:05 -050092 inc_block_ref(root, blocknr);
93 }
94 return 0;
95}
96
Chris Mason234b63a2007-03-13 10:46:10 -040097int btrfs_finish_extent_commit(struct btrfs_root *root)
Chris Masona28ec192007-03-06 20:08:01 -050098{
Chris Masona28ec192007-03-06 20:08:01 -050099 unsigned long gang[8];
100 int ret;
101 int i;
102
103 while(1) {
Chris Mason3768f362007-03-13 16:47:54 -0400104 ret = radix_tree_gang_lookup(&root->pinned_radix,
Chris Masona28ec192007-03-06 20:08:01 -0500105 (void **)gang, 0,
106 ARRAY_SIZE(gang));
107 if (!ret)
108 break;
Chris Mason0579da42007-03-07 16:15:30 -0500109 for (i = 0; i < ret; i++) {
Chris Mason3768f362007-03-13 16:47:54 -0400110 radix_tree_delete(&root->pinned_radix, gang[i]);
Chris Mason0579da42007-03-07 16:15:30 -0500111 }
Chris Masona28ec192007-03-06 20:08:01 -0500112 }
Chris Mason3768f362007-03-13 16:47:54 -0400113 root->last_insert.objectid = 0;
114 root->last_insert.offset = 0;
Chris Masona28ec192007-03-06 20:08:01 -0500115 return 0;
116}
117
Chris Mason234b63a2007-03-13 10:46:10 -0400118static int finish_current_insert(struct btrfs_root *extent_root)
Chris Mason037e6392007-03-07 11:50:24 -0500119{
Chris Masone2fa7222007-03-12 16:22:34 -0400120 struct btrfs_key ins;
Chris Mason234b63a2007-03-13 10:46:10 -0400121 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -0500122 int i;
123 int ret;
124
Chris Masoncf27e1e2007-03-13 09:49:06 -0400125 btrfs_set_extent_refs(&extent_item, 1);
126 btrfs_set_extent_owner(&extent_item,
127 btrfs_header_parentid(&extent_root->node->node.header));
Chris Mason037e6392007-03-07 11:50:24 -0500128 ins.offset = 1;
129 ins.flags = 0;
130
131 for (i = 0; i < extent_root->current_insert.flags; i++) {
132 ins.objectid = extent_root->current_insert.objectid + i;
Chris Mason234b63a2007-03-13 10:46:10 -0400133 ret = btrfs_insert_item(extent_root, &ins, &extent_item,
Chris Mason037e6392007-03-07 11:50:24 -0500134 sizeof(extent_item));
135 BUG_ON(ret);
136 }
137 extent_root->current_insert.offset = 0;
138 return 0;
139}
140
Chris Masona28ec192007-03-06 20:08:01 -0500141/*
142 * remove an extent from the root, returns 0 on success
143 */
Chris Mason234b63a2007-03-13 10:46:10 -0400144static int __free_extent(struct btrfs_root *root, u64 blocknr, u64 num_blocks)
Chris Masona28ec192007-03-06 20:08:01 -0500145{
Chris Mason234b63a2007-03-13 10:46:10 -0400146 struct btrfs_path path;
Chris Masone2fa7222007-03-12 16:22:34 -0400147 struct btrfs_key key;
Chris Mason234b63a2007-03-13 10:46:10 -0400148 struct btrfs_root *extent_root = root->extent_root;
Chris Masona28ec192007-03-06 20:08:01 -0500149 int ret;
Chris Mason0783fcf2007-03-12 20:12:07 -0400150 struct btrfs_item *item;
Chris Mason234b63a2007-03-13 10:46:10 -0400151 struct btrfs_extent_item *ei;
Chris Masone2fa7222007-03-12 16:22:34 -0400152 struct btrfs_key ins;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400153 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500154
Chris Masona28ec192007-03-06 20:08:01 -0500155 key.objectid = blocknr;
156 key.flags = 0;
157 key.offset = num_blocks;
158
Chris Mason037e6392007-03-07 11:50:24 -0500159 find_free_extent(root, 0, 0, (u64)-1, &ins);
Chris Mason234b63a2007-03-13 10:46:10 -0400160 btrfs_init_path(&path);
161 ret = btrfs_search_slot(extent_root, &key, &path, -1, 1);
Chris Masona28ec192007-03-06 20:08:01 -0500162 if (ret) {
163 printf("failed to find %Lu\n", key.objectid);
Chris Mason234b63a2007-03-13 10:46:10 -0400164 btrfs_print_tree(extent_root, extent_root->node);
Chris Masona28ec192007-03-06 20:08:01 -0500165 printf("failed to find %Lu\n", key.objectid);
166 BUG();
167 }
168 item = path.nodes[0]->leaf.items + path.slots[0];
Chris Mason234b63a2007-03-13 10:46:10 -0400169 ei = (struct btrfs_extent_item *)(path.nodes[0]->leaf.data +
Chris Mason0783fcf2007-03-12 20:12:07 -0400170 btrfs_item_offset(item));
Chris Masona28ec192007-03-06 20:08:01 -0500171 BUG_ON(ei->refs == 0);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400172 refs = btrfs_extent_refs(ei) - 1;
173 btrfs_set_extent_refs(ei, refs);
174 if (refs == 0) {
Chris Mason3768f362007-03-13 16:47:54 -0400175 if (!root->ref_cows) {
Chris Masona28ec192007-03-06 20:08:01 -0500176 int err;
177 radix_tree_preload(GFP_KERNEL);
178 err = radix_tree_insert(&extent_root->pinned_radix,
179 blocknr, (void *)blocknr);
180 BUG_ON(err);
181 radix_tree_preload_end();
182 }
Chris Mason234b63a2007-03-13 10:46:10 -0400183 ret = btrfs_del_item(extent_root, &path);
Chris Mason0579da42007-03-07 16:15:30 -0500184 if (root != extent_root &&
185 extent_root->last_insert.objectid < blocknr)
186 extent_root->last_insert.objectid = blocknr;
Chris Masona28ec192007-03-06 20:08:01 -0500187 if (ret)
188 BUG();
189 }
Chris Mason234b63a2007-03-13 10:46:10 -0400190 btrfs_release_path(extent_root, &path);
Chris Mason037e6392007-03-07 11:50:24 -0500191 finish_current_insert(extent_root);
Chris Masona28ec192007-03-06 20:08:01 -0500192 return ret;
193}
194
195/*
Chris Masonfec577f2007-02-26 10:40:21 -0500196 * find all the blocks marked as pending in the radix tree and remove
197 * them from the extent map
198 */
Chris Mason234b63a2007-03-13 10:46:10 -0400199static int del_pending_extents(struct btrfs_root *extent_root)
Chris Masonfec577f2007-02-26 10:40:21 -0500200{
201 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400202 struct btrfs_buffer *gang[4];
Chris Masonfec577f2007-02-26 10:40:21 -0500203 int i;
Chris Masonfec577f2007-02-26 10:40:21 -0500204
205 while(1) {
206 ret = radix_tree_gang_lookup_tag(&extent_root->cache_radix,
207 (void **)gang, 0,
208 ARRAY_SIZE(gang),
Chris Masona28ec192007-03-06 20:08:01 -0500209 CTREE_EXTENT_PENDING_DEL);
Chris Masonfec577f2007-02-26 10:40:21 -0500210 if (!ret)
211 break;
212 for (i = 0; i < ret; i++) {
Chris Masona28ec192007-03-06 20:08:01 -0500213 ret = __free_extent(extent_root, gang[i]->blocknr, 1);
Chris Masonfec577f2007-02-26 10:40:21 -0500214 radix_tree_tag_clear(&extent_root->cache_radix,
215 gang[i]->blocknr,
Chris Masona28ec192007-03-06 20:08:01 -0500216 CTREE_EXTENT_PENDING_DEL);
Chris Mason234b63a2007-03-13 10:46:10 -0400217 btrfs_block_release(extent_root, gang[i]);
Chris Masonfec577f2007-02-26 10:40:21 -0500218 }
219 }
220 return 0;
221}
222
Chris Mason234b63a2007-03-13 10:46:10 -0400223static int run_pending(struct btrfs_root *extent_root)
Chris Masona28ec192007-03-06 20:08:01 -0500224{
225 while(radix_tree_tagged(&extent_root->cache_radix,
Chris Mason037e6392007-03-07 11:50:24 -0500226 CTREE_EXTENT_PENDING_DEL))
Chris Masona28ec192007-03-06 20:08:01 -0500227 del_pending_extents(extent_root);
Chris Masona28ec192007-03-06 20:08:01 -0500228 return 0;
229}
230
231
Chris Masonfec577f2007-02-26 10:40:21 -0500232/*
233 * remove an extent from the root, returns 0 on success
234 */
Chris Mason234b63a2007-03-13 10:46:10 -0400235int btrfs_free_extent(struct btrfs_root *root, u64 blocknr, u64 num_blocks)
Chris Masonfec577f2007-02-26 10:40:21 -0500236{
Chris Masone2fa7222007-03-12 16:22:34 -0400237 struct btrfs_key key;
Chris Mason234b63a2007-03-13 10:46:10 -0400238 struct btrfs_root *extent_root = root->extent_root;
239 struct btrfs_buffer *t;
Chris Masonfec577f2007-02-26 10:40:21 -0500240 int pending_ret;
241 int ret;
Chris Masona28ec192007-03-06 20:08:01 -0500242
243 if (root == extent_root) {
244 t = find_tree_block(root, blocknr);
Chris Mason037e6392007-03-07 11:50:24 -0500245 radix_tree_tag_set(&root->cache_radix, blocknr,
Chris Masona28ec192007-03-06 20:08:01 -0500246 CTREE_EXTENT_PENDING_DEL);
Chris Masona28ec192007-03-06 20:08:01 -0500247 return 0;
248 }
Chris Masonfec577f2007-02-26 10:40:21 -0500249 key.objectid = blocknr;
250 key.flags = 0;
251 key.offset = num_blocks;
Chris Masona28ec192007-03-06 20:08:01 -0500252 ret = __free_extent(root, blocknr, num_blocks);
253 pending_ret = run_pending(root->extent_root);
Chris Masonfec577f2007-02-26 10:40:21 -0500254 return ret ? ret : pending_ret;
255}
256
257/*
258 * walks the btree of allocated extents and find a hole of a given size.
259 * The key ins is changed to record the hole:
260 * ins->objectid == block start
261 * ins->flags = 0
262 * ins->offset == number of blocks
263 * Any available blocks before search_start are skipped.
264 */
Chris Mason234b63a2007-03-13 10:46:10 -0400265static int find_free_extent(struct btrfs_root *orig_root, u64 num_blocks,
Chris Masone2fa7222007-03-12 16:22:34 -0400266 u64 search_start, u64 search_end,
267 struct btrfs_key *ins)
Chris Masonfec577f2007-02-26 10:40:21 -0500268{
Chris Mason234b63a2007-03-13 10:46:10 -0400269 struct btrfs_path path;
Chris Masone2fa7222007-03-12 16:22:34 -0400270 struct btrfs_key key;
Chris Masonfec577f2007-02-26 10:40:21 -0500271 int ret;
272 u64 hole_size = 0;
273 int slot = 0;
274 u64 last_block;
Chris Mason037e6392007-03-07 11:50:24 -0500275 u64 test_block;
Chris Masonfec577f2007-02-26 10:40:21 -0500276 int start_found;
Chris Mason234b63a2007-03-13 10:46:10 -0400277 struct btrfs_leaf *l;
278 struct btrfs_root * root = orig_root->extent_root;
Chris Mason0579da42007-03-07 16:15:30 -0500279 int total_needed = num_blocks;
Chris Masonfec577f2007-02-26 10:40:21 -0500280
Chris Mason7518a232007-03-12 12:01:18 -0400281 total_needed += (btrfs_header_level(&root->node->node.header) + 1) * 3;
Chris Mason0579da42007-03-07 16:15:30 -0500282 if (root->last_insert.objectid > search_start)
283 search_start = root->last_insert.objectid;
Chris Masonfec577f2007-02-26 10:40:21 -0500284check_failed:
Chris Mason234b63a2007-03-13 10:46:10 -0400285 btrfs_init_path(&path);
Chris Masonfec577f2007-02-26 10:40:21 -0500286 ins->objectid = search_start;
287 ins->offset = 0;
288 ins->flags = 0;
289 start_found = 0;
Chris Mason234b63a2007-03-13 10:46:10 -0400290 ret = btrfs_search_slot(root, ins, &path, 0, 0);
Chris Mason0f70abe2007-02-28 16:46:22 -0500291 if (ret < 0)
292 goto error;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500293
Chris Mason0579da42007-03-07 16:15:30 -0500294 if (path.slots[0] > 0)
295 path.slots[0]--;
296
Chris Masonfec577f2007-02-26 10:40:21 -0500297 while (1) {
298 l = &path.nodes[0]->leaf;
299 slot = path.slots[0];
Chris Mason7518a232007-03-12 12:01:18 -0400300 if (slot >= btrfs_header_nritems(&l->header)) {
Chris Mason234b63a2007-03-13 10:46:10 -0400301 ret = btrfs_next_leaf(root, &path);
Chris Masonfec577f2007-02-26 10:40:21 -0500302 if (ret == 0)
303 continue;
Chris Mason0f70abe2007-02-28 16:46:22 -0500304 if (ret < 0)
305 goto error;
Chris Masonfec577f2007-02-26 10:40:21 -0500306 if (!start_found) {
307 ins->objectid = search_start;
Chris Mason037e6392007-03-07 11:50:24 -0500308 ins->offset = (u64)-1;
Chris Masonfec577f2007-02-26 10:40:21 -0500309 start_found = 1;
310 goto check_pending;
311 }
312 ins->objectid = last_block > search_start ?
313 last_block : search_start;
Chris Mason037e6392007-03-07 11:50:24 -0500314 ins->offset = (u64)-1;
Chris Masonfec577f2007-02-26 10:40:21 -0500315 goto check_pending;
316 }
Chris Masone2fa7222007-03-12 16:22:34 -0400317 btrfs_disk_key_to_cpu(&key, &l->items[slot].key);
318 if (key.objectid >= search_start) {
Chris Masonfec577f2007-02-26 10:40:21 -0500319 if (start_found) {
Chris Mason0579da42007-03-07 16:15:30 -0500320 if (last_block < search_start)
321 last_block = search_start;
Chris Masone2fa7222007-03-12 16:22:34 -0400322 hole_size = key.objectid - last_block;
Chris Mason037e6392007-03-07 11:50:24 -0500323 if (hole_size > total_needed) {
Chris Masonfec577f2007-02-26 10:40:21 -0500324 ins->objectid = last_block;
Chris Mason037e6392007-03-07 11:50:24 -0500325 ins->offset = hole_size;
Chris Masonfec577f2007-02-26 10:40:21 -0500326 goto check_pending;
327 }
Chris Mason0579da42007-03-07 16:15:30 -0500328 }
Chris Masonfec577f2007-02-26 10:40:21 -0500329 }
Chris Mason0579da42007-03-07 16:15:30 -0500330 start_found = 1;
Chris Masone2fa7222007-03-12 16:22:34 -0400331 last_block = key.objectid + key.offset;
Chris Masonfec577f2007-02-26 10:40:21 -0500332 path.slots[0]++;
333 }
334 // FIXME -ENOSPC
335check_pending:
336 /* we have to make sure we didn't find an extent that has already
337 * been allocated by the map tree or the original allocation
338 */
Chris Mason234b63a2007-03-13 10:46:10 -0400339 btrfs_release_path(root, &path);
Chris Masonfec577f2007-02-26 10:40:21 -0500340 BUG_ON(ins->objectid < search_start);
Chris Mason037e6392007-03-07 11:50:24 -0500341 for (test_block = ins->objectid;
342 test_block < ins->objectid + total_needed; test_block++) {
343 if (radix_tree_lookup(&root->pinned_radix, test_block)) {
344 search_start = test_block + 1;
Chris Masonfec577f2007-02-26 10:40:21 -0500345 goto check_failed;
346 }
347 }
Chris Mason037e6392007-03-07 11:50:24 -0500348 BUG_ON(root->current_insert.offset);
Chris Mason0579da42007-03-07 16:15:30 -0500349 root->current_insert.offset = total_needed - num_blocks;
Chris Mason037e6392007-03-07 11:50:24 -0500350 root->current_insert.objectid = ins->objectid + num_blocks;
351 root->current_insert.flags = 0;
Chris Mason0579da42007-03-07 16:15:30 -0500352 root->last_insert.objectid = ins->objectid;
Chris Mason037e6392007-03-07 11:50:24 -0500353 ins->offset = num_blocks;
Chris Masonfec577f2007-02-26 10:40:21 -0500354 return 0;
Chris Mason0f70abe2007-02-28 16:46:22 -0500355error:
Chris Mason234b63a2007-03-13 10:46:10 -0400356 btrfs_release_path(root, &path);
Chris Mason0f70abe2007-02-28 16:46:22 -0500357 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -0500358}
359
360/*
Chris Masonfec577f2007-02-26 10:40:21 -0500361 * finds a free extent and does all the dirty work required for allocation
362 * returns the key for the extent through ins, and a tree buffer for
363 * the first block of the extent through buf.
364 *
365 * returns 0 if everything worked, non-zero otherwise.
366 */
Chris Mason9aca1d52007-03-13 11:09:37 -0400367static int alloc_extent(struct btrfs_root *root, u64 num_blocks,
368 u64 search_start, u64 search_end, u64 owner,
369 struct btrfs_key *ins)
Chris Masonfec577f2007-02-26 10:40:21 -0500370{
371 int ret;
372 int pending_ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400373 struct btrfs_root *extent_root = root->extent_root;
374 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -0500375
Chris Masoncf27e1e2007-03-13 09:49:06 -0400376 btrfs_set_extent_refs(&extent_item, 1);
377 btrfs_set_extent_owner(&extent_item, owner);
Chris Masonfec577f2007-02-26 10:40:21 -0500378
Chris Mason037e6392007-03-07 11:50:24 -0500379 if (root == extent_root) {
380 BUG_ON(extent_root->current_insert.offset == 0);
381 BUG_ON(num_blocks != 1);
382 BUG_ON(extent_root->current_insert.flags ==
383 extent_root->current_insert.offset);
384 ins->offset = 1;
385 ins->objectid = extent_root->current_insert.objectid +
386 extent_root->current_insert.flags++;
Chris Masonfec577f2007-02-26 10:40:21 -0500387 return 0;
388 }
Chris Mason037e6392007-03-07 11:50:24 -0500389 ret = find_free_extent(root, num_blocks, search_start,
390 search_end, ins);
391 if (ret)
392 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -0500393
Chris Mason234b63a2007-03-13 10:46:10 -0400394 ret = btrfs_insert_item(extent_root, ins, &extent_item,
Chris Mason037e6392007-03-07 11:50:24 -0500395 sizeof(extent_item));
396
397 finish_current_insert(extent_root);
398 pending_ret = run_pending(extent_root);
399 if (ret)
400 return ret;
401 if (pending_ret)
402 return pending_ret;
403 return 0;
Chris Masonfec577f2007-02-26 10:40:21 -0500404}
405
406/*
407 * helper function to allocate a block for a given tree
408 * returns the tree buffer or NULL.
409 */
Chris Mason234b63a2007-03-13 10:46:10 -0400410struct btrfs_buffer *btrfs_alloc_free_block(struct btrfs_root *root)
Chris Masonfec577f2007-02-26 10:40:21 -0500411{
Chris Masone2fa7222007-03-12 16:22:34 -0400412 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -0500413 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400414 struct btrfs_buffer *buf;
Chris Masonfec577f2007-02-26 10:40:21 -0500415
416 ret = alloc_extent(root, 1, 0, (unsigned long)-1,
Chris Mason7518a232007-03-12 12:01:18 -0400417 btrfs_header_parentid(&root->node->node.header),
Chris Mason037e6392007-03-07 11:50:24 -0500418 &ins);
Chris Masonfec577f2007-02-26 10:40:21 -0500419 if (ret) {
420 BUG();
421 return NULL;
422 }
Chris Mason037e6392007-03-07 11:50:24 -0500423 buf = find_tree_block(root, ins.objectid);
424 dirty_tree_block(root, buf);
Chris Masonfec577f2007-02-26 10:40:21 -0500425 return buf;
426}
Chris Masona28ec192007-03-06 20:08:01 -0500427
Chris Mason9aca1d52007-03-13 11:09:37 -0400428/*
429 * helper function for drop_snapshot, this walks down the tree dropping ref
430 * counts as it goes.
431 */
432static int walk_down_tree(struct btrfs_root *root,
433 struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -0500434{
Chris Mason234b63a2007-03-13 10:46:10 -0400435 struct btrfs_buffer *next;
436 struct btrfs_buffer *cur;
Chris Mason20524f02007-03-10 06:35:47 -0500437 u64 blocknr;
438 int ret;
439 u32 refs;
440
441 ret = lookup_block_ref(root, path->nodes[*level]->blocknr, &refs);
442 BUG_ON(ret);
443 if (refs > 1)
444 goto out;
Chris Mason9aca1d52007-03-13 11:09:37 -0400445 /*
446 * walk down to the last node level and free all the leaves
447 */
Chris Mason20524f02007-03-10 06:35:47 -0500448 while(*level > 0) {
449 cur = path->nodes[*level];
Chris Mason7518a232007-03-12 12:01:18 -0400450 if (path->slots[*level] >=
451 btrfs_header_nritems(&cur->node.header))
Chris Mason20524f02007-03-10 06:35:47 -0500452 break;
Chris Mason1d4f8a02007-03-13 09:28:32 -0400453 blocknr = btrfs_node_blockptr(&cur->node, path->slots[*level]);
Chris Mason20524f02007-03-10 06:35:47 -0500454 ret = lookup_block_ref(root, blocknr, &refs);
455 if (refs != 1 || *level == 1) {
456 path->slots[*level]++;
Chris Mason234b63a2007-03-13 10:46:10 -0400457 ret = btrfs_free_extent(root, blocknr, 1);
Chris Mason20524f02007-03-10 06:35:47 -0500458 BUG_ON(ret);
459 continue;
460 }
461 BUG_ON(ret);
462 next = read_tree_block(root, blocknr);
Chris Mason83e15a22007-03-12 09:03:27 -0400463 if (path->nodes[*level-1])
Chris Mason234b63a2007-03-13 10:46:10 -0400464 btrfs_block_release(root, path->nodes[*level-1]);
Chris Mason20524f02007-03-10 06:35:47 -0500465 path->nodes[*level-1] = next;
Chris Mason7518a232007-03-12 12:01:18 -0400466 *level = btrfs_header_level(&next->node.header);
Chris Mason20524f02007-03-10 06:35:47 -0500467 path->slots[*level] = 0;
468 }
469out:
Chris Mason234b63a2007-03-13 10:46:10 -0400470 ret = btrfs_free_extent(root, path->nodes[*level]->blocknr, 1);
471 btrfs_block_release(root, path->nodes[*level]);
Chris Mason20524f02007-03-10 06:35:47 -0500472 path->nodes[*level] = NULL;
473 *level += 1;
474 BUG_ON(ret);
475 return 0;
476}
477
Chris Mason9aca1d52007-03-13 11:09:37 -0400478/*
479 * helper for dropping snapshots. This walks back up the tree in the path
480 * to find the first node higher up where we haven't yet gone through
481 * all the slots
482 */
483static int walk_up_tree(struct btrfs_root *root, struct btrfs_path *path,
484 int *level)
Chris Mason20524f02007-03-10 06:35:47 -0500485{
486 int i;
487 int slot;
488 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400489 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Mason20524f02007-03-10 06:35:47 -0500490 slot = path->slots[i];
Chris Mason7518a232007-03-12 12:01:18 -0400491 if (slot <
492 btrfs_header_nritems(&path->nodes[i]->node.header)- 1) {
Chris Mason20524f02007-03-10 06:35:47 -0500493 path->slots[i]++;
494 *level = i;
495 return 0;
496 } else {
Chris Mason234b63a2007-03-13 10:46:10 -0400497 ret = btrfs_free_extent(root,
Chris Mason20524f02007-03-10 06:35:47 -0500498 path->nodes[*level]->blocknr, 1);
Chris Mason234b63a2007-03-13 10:46:10 -0400499 btrfs_block_release(root, path->nodes[*level]);
Chris Mason83e15a22007-03-12 09:03:27 -0400500 path->nodes[*level] = NULL;
Chris Mason20524f02007-03-10 06:35:47 -0500501 *level = i + 1;
502 BUG_ON(ret);
503 }
504 }
505 return 1;
506}
507
Chris Mason9aca1d52007-03-13 11:09:37 -0400508/*
509 * drop the reference count on the tree rooted at 'snap'. This traverses
510 * the tree freeing any blocks that have a ref count of zero after being
511 * decremented.
512 */
Chris Mason234b63a2007-03-13 10:46:10 -0400513int btrfs_drop_snapshot(struct btrfs_root *root, struct btrfs_buffer *snap)
Chris Mason20524f02007-03-10 06:35:47 -0500514{
Chris Mason3768f362007-03-13 16:47:54 -0400515 int ret = 0;
Chris Mason9aca1d52007-03-13 11:09:37 -0400516 int wret;
Chris Mason20524f02007-03-10 06:35:47 -0500517 int level;
Chris Mason234b63a2007-03-13 10:46:10 -0400518 struct btrfs_path path;
Chris Mason20524f02007-03-10 06:35:47 -0500519 int i;
520 int orig_level;
521
Chris Mason234b63a2007-03-13 10:46:10 -0400522 btrfs_init_path(&path);
Chris Mason20524f02007-03-10 06:35:47 -0500523
Chris Mason7518a232007-03-12 12:01:18 -0400524 level = btrfs_header_level(&snap->node.header);
Chris Mason20524f02007-03-10 06:35:47 -0500525 orig_level = level;
526 path.nodes[level] = snap;
527 path.slots[level] = 0;
528 while(1) {
Chris Mason9aca1d52007-03-13 11:09:37 -0400529 wret = walk_down_tree(root, &path, &level);
530 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -0500531 break;
Chris Mason9aca1d52007-03-13 11:09:37 -0400532 if (wret < 0)
533 ret = wret;
534
535 wret = walk_up_tree(root, &path, &level);
536 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -0500537 break;
Chris Mason9aca1d52007-03-13 11:09:37 -0400538 if (wret < 0)
539 ret = wret;
Chris Mason20524f02007-03-10 06:35:47 -0500540 }
Chris Mason83e15a22007-03-12 09:03:27 -0400541 for (i = 0; i <= orig_level; i++) {
542 if (path.nodes[i]) {
Chris Mason234b63a2007-03-13 10:46:10 -0400543 btrfs_block_release(root, path.nodes[i]);
Chris Mason83e15a22007-03-12 09:03:27 -0400544 }
Chris Mason20524f02007-03-10 06:35:47 -0500545 }
Chris Mason9aca1d52007-03-13 11:09:37 -0400546 return ret;
Chris Mason20524f02007-03-10 06:35:47 -0500547}