blob: 3767744f659ecf8810c9699d31f212fe270fd249 [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 Mason4beb1b82007-03-14 10:31:29 -040044 item = btrfs_item_ptr(l, path.slots[0], struct btrfs_extent_item);
Chris Masoncf27e1e2007-03-13 09:49:06 -040045 refs = btrfs_extent_refs(item);
46 btrfs_set_extent_refs(item, refs + 1);
Chris Masona28ec192007-03-06 20:08:01 -050047
Chris Mason02217ed2007-03-02 16:08:05 -050048 BUG_ON(list_empty(&path.nodes[0]->dirty));
Chris Mason234b63a2007-03-13 10:46:10 -040049 btrfs_release_path(root->extent_root, &path);
Chris Mason037e6392007-03-07 11:50:24 -050050 finish_current_insert(root->extent_root);
51 run_pending(root->extent_root);
Chris Mason02217ed2007-03-02 16:08:05 -050052 return 0;
53}
54
Chris Mason234b63a2007-03-13 10:46:10 -040055static int lookup_block_ref(struct btrfs_root *root, u64 blocknr, u32 *refs)
Chris Masona28ec192007-03-06 20:08:01 -050056{
Chris Mason234b63a2007-03-13 10:46:10 -040057 struct btrfs_path path;
Chris Masona28ec192007-03-06 20:08:01 -050058 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -040059 struct btrfs_key key;
Chris Mason234b63a2007-03-13 10:46:10 -040060 struct btrfs_leaf *l;
61 struct btrfs_extent_item *item;
62 btrfs_init_path(&path);
Chris Masona28ec192007-03-06 20:08:01 -050063 key.objectid = blocknr;
64 key.flags = 0;
65 key.offset = 1;
Chris Mason234b63a2007-03-13 10:46:10 -040066 ret = btrfs_search_slot(root->extent_root, &key, &path, 0, 0);
Chris Masona28ec192007-03-06 20:08:01 -050067 if (ret != 0)
68 BUG();
69 l = &path.nodes[0]->leaf;
Chris Mason4beb1b82007-03-14 10:31:29 -040070 item = btrfs_item_ptr(l, path.slots[0], struct btrfs_extent_item);
Chris Masoncf27e1e2007-03-13 09:49:06 -040071 *refs = btrfs_extent_refs(item);
Chris Mason234b63a2007-03-13 10:46:10 -040072 btrfs_release_path(root->extent_root, &path);
Chris Masona28ec192007-03-06 20:08:01 -050073 return 0;
74}
75
Chris Mason234b63a2007-03-13 10:46:10 -040076int btrfs_inc_ref(struct btrfs_root *root, struct btrfs_buffer *buf)
Chris Mason02217ed2007-03-02 16:08:05 -050077{
78 u64 blocknr;
79 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 Mason7518a232007-03-12 12:01:18 -040083 if (btrfs_is_leaf(&buf->node))
Chris Masona28ec192007-03-06 20:08:01 -050084 return 0;
85
Chris Mason7518a232007-03-12 12:01:18 -040086 for (i = 0; i < btrfs_header_nritems(&buf->node.header); i++) {
Chris Mason1d4f8a02007-03-13 09:28:32 -040087 blocknr = btrfs_node_blockptr(&buf->node, i);
Chris Mason02217ed2007-03-02 16:08:05 -050088 inc_block_ref(root, blocknr);
89 }
90 return 0;
91}
92
Chris Mason234b63a2007-03-13 10:46:10 -040093int btrfs_finish_extent_commit(struct btrfs_root *root)
Chris Masona28ec192007-03-06 20:08:01 -050094{
Chris Masona28ec192007-03-06 20:08:01 -050095 unsigned long gang[8];
96 int ret;
97 int i;
98
99 while(1) {
Chris Mason3768f362007-03-13 16:47:54 -0400100 ret = radix_tree_gang_lookup(&root->pinned_radix,
Chris Masona28ec192007-03-06 20:08:01 -0500101 (void **)gang, 0,
102 ARRAY_SIZE(gang));
103 if (!ret)
104 break;
Chris Mason0579da42007-03-07 16:15:30 -0500105 for (i = 0; i < ret; i++) {
Chris Mason3768f362007-03-13 16:47:54 -0400106 radix_tree_delete(&root->pinned_radix, gang[i]);
Chris Mason0579da42007-03-07 16:15:30 -0500107 }
Chris Masona28ec192007-03-06 20:08:01 -0500108 }
Chris Mason3768f362007-03-13 16:47:54 -0400109 root->last_insert.objectid = 0;
110 root->last_insert.offset = 0;
Chris Masona28ec192007-03-06 20:08:01 -0500111 return 0;
112}
113
Chris Mason234b63a2007-03-13 10:46:10 -0400114static int finish_current_insert(struct btrfs_root *extent_root)
Chris Mason037e6392007-03-07 11:50:24 -0500115{
Chris Masone2fa7222007-03-12 16:22:34 -0400116 struct btrfs_key ins;
Chris Mason234b63a2007-03-13 10:46:10 -0400117 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -0500118 int i;
119 int ret;
120
Chris Masoncf27e1e2007-03-13 09:49:06 -0400121 btrfs_set_extent_refs(&extent_item, 1);
122 btrfs_set_extent_owner(&extent_item,
123 btrfs_header_parentid(&extent_root->node->node.header));
Chris Mason037e6392007-03-07 11:50:24 -0500124 ins.offset = 1;
125 ins.flags = 0;
126
127 for (i = 0; i < extent_root->current_insert.flags; i++) {
128 ins.objectid = extent_root->current_insert.objectid + i;
Chris Mason234b63a2007-03-13 10:46:10 -0400129 ret = btrfs_insert_item(extent_root, &ins, &extent_item,
Chris Mason037e6392007-03-07 11:50:24 -0500130 sizeof(extent_item));
131 BUG_ON(ret);
132 }
133 extent_root->current_insert.offset = 0;
134 return 0;
135}
136
Chris Masona28ec192007-03-06 20:08:01 -0500137/*
138 * remove an extent from the root, returns 0 on success
139 */
Chris Mason234b63a2007-03-13 10:46:10 -0400140static int __free_extent(struct btrfs_root *root, u64 blocknr, u64 num_blocks)
Chris Masona28ec192007-03-06 20:08:01 -0500141{
Chris Mason234b63a2007-03-13 10:46:10 -0400142 struct btrfs_path path;
Chris Masone2fa7222007-03-12 16:22:34 -0400143 struct btrfs_key key;
Chris Mason234b63a2007-03-13 10:46:10 -0400144 struct btrfs_root *extent_root = root->extent_root;
Chris Masona28ec192007-03-06 20:08:01 -0500145 int ret;
Chris Mason0783fcf2007-03-12 20:12:07 -0400146 struct btrfs_item *item;
Chris Mason234b63a2007-03-13 10:46:10 -0400147 struct btrfs_extent_item *ei;
Chris Masone2fa7222007-03-12 16:22:34 -0400148 struct btrfs_key ins;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400149 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500150
Chris Masona28ec192007-03-06 20:08:01 -0500151 key.objectid = blocknr;
152 key.flags = 0;
153 key.offset = num_blocks;
154
Chris Mason037e6392007-03-07 11:50:24 -0500155 find_free_extent(root, 0, 0, (u64)-1, &ins);
Chris Mason234b63a2007-03-13 10:46:10 -0400156 btrfs_init_path(&path);
157 ret = btrfs_search_slot(extent_root, &key, &path, -1, 1);
Chris Masona28ec192007-03-06 20:08:01 -0500158 if (ret) {
159 printf("failed to find %Lu\n", key.objectid);
Chris Mason234b63a2007-03-13 10:46:10 -0400160 btrfs_print_tree(extent_root, extent_root->node);
Chris Masona28ec192007-03-06 20:08:01 -0500161 printf("failed to find %Lu\n", key.objectid);
162 BUG();
163 }
164 item = path.nodes[0]->leaf.items + path.slots[0];
Chris Mason234b63a2007-03-13 10:46:10 -0400165 ei = (struct btrfs_extent_item *)(path.nodes[0]->leaf.data +
Chris Mason0783fcf2007-03-12 20:12:07 -0400166 btrfs_item_offset(item));
Chris Masona28ec192007-03-06 20:08:01 -0500167 BUG_ON(ei->refs == 0);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400168 refs = btrfs_extent_refs(ei) - 1;
169 btrfs_set_extent_refs(ei, refs);
170 if (refs == 0) {
Chris Mason3768f362007-03-13 16:47:54 -0400171 if (!root->ref_cows) {
Chris Masona28ec192007-03-06 20:08:01 -0500172 int err;
173 radix_tree_preload(GFP_KERNEL);
174 err = radix_tree_insert(&extent_root->pinned_radix,
175 blocknr, (void *)blocknr);
176 BUG_ON(err);
177 radix_tree_preload_end();
178 }
Chris Mason234b63a2007-03-13 10:46:10 -0400179 ret = btrfs_del_item(extent_root, &path);
Chris Mason0579da42007-03-07 16:15:30 -0500180 if (root != extent_root &&
Chris Mason71087492007-03-14 09:20:39 -0400181 extent_root->last_insert.objectid > blocknr)
Chris Mason0579da42007-03-07 16:15:30 -0500182 extent_root->last_insert.objectid = blocknr;
Chris Masona28ec192007-03-06 20:08:01 -0500183 if (ret)
184 BUG();
185 }
Chris Mason234b63a2007-03-13 10:46:10 -0400186 btrfs_release_path(extent_root, &path);
Chris Mason037e6392007-03-07 11:50:24 -0500187 finish_current_insert(extent_root);
Chris Masona28ec192007-03-06 20:08:01 -0500188 return ret;
189}
190
191/*
Chris Masonfec577f2007-02-26 10:40:21 -0500192 * find all the blocks marked as pending in the radix tree and remove
193 * them from the extent map
194 */
Chris Mason234b63a2007-03-13 10:46:10 -0400195static int del_pending_extents(struct btrfs_root *extent_root)
Chris Masonfec577f2007-02-26 10:40:21 -0500196{
197 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400198 struct btrfs_buffer *gang[4];
Chris Masonfec577f2007-02-26 10:40:21 -0500199 int i;
Chris Masonfec577f2007-02-26 10:40:21 -0500200
201 while(1) {
202 ret = radix_tree_gang_lookup_tag(&extent_root->cache_radix,
203 (void **)gang, 0,
204 ARRAY_SIZE(gang),
Chris Masona28ec192007-03-06 20:08:01 -0500205 CTREE_EXTENT_PENDING_DEL);
Chris Masonfec577f2007-02-26 10:40:21 -0500206 if (!ret)
207 break;
208 for (i = 0; i < ret; i++) {
Chris Masona28ec192007-03-06 20:08:01 -0500209 ret = __free_extent(extent_root, gang[i]->blocknr, 1);
Chris Masonfec577f2007-02-26 10:40:21 -0500210 radix_tree_tag_clear(&extent_root->cache_radix,
211 gang[i]->blocknr,
Chris Masona28ec192007-03-06 20:08:01 -0500212 CTREE_EXTENT_PENDING_DEL);
Chris Mason234b63a2007-03-13 10:46:10 -0400213 btrfs_block_release(extent_root, gang[i]);
Chris Masonfec577f2007-02-26 10:40:21 -0500214 }
215 }
216 return 0;
217}
218
Chris Mason234b63a2007-03-13 10:46:10 -0400219static int run_pending(struct btrfs_root *extent_root)
Chris Masona28ec192007-03-06 20:08:01 -0500220{
221 while(radix_tree_tagged(&extent_root->cache_radix,
Chris Mason037e6392007-03-07 11:50:24 -0500222 CTREE_EXTENT_PENDING_DEL))
Chris Masona28ec192007-03-06 20:08:01 -0500223 del_pending_extents(extent_root);
Chris Masona28ec192007-03-06 20:08:01 -0500224 return 0;
225}
226
227
Chris Masonfec577f2007-02-26 10:40:21 -0500228/*
229 * remove an extent from the root, returns 0 on success
230 */
Chris Mason234b63a2007-03-13 10:46:10 -0400231int btrfs_free_extent(struct btrfs_root *root, u64 blocknr, u64 num_blocks)
Chris Masonfec577f2007-02-26 10:40:21 -0500232{
Chris Masone2fa7222007-03-12 16:22:34 -0400233 struct btrfs_key key;
Chris Mason234b63a2007-03-13 10:46:10 -0400234 struct btrfs_root *extent_root = root->extent_root;
235 struct btrfs_buffer *t;
Chris Masonfec577f2007-02-26 10:40:21 -0500236 int pending_ret;
237 int ret;
Chris Masona28ec192007-03-06 20:08:01 -0500238
239 if (root == extent_root) {
240 t = find_tree_block(root, blocknr);
Chris Mason037e6392007-03-07 11:50:24 -0500241 radix_tree_tag_set(&root->cache_radix, blocknr,
Chris Masona28ec192007-03-06 20:08:01 -0500242 CTREE_EXTENT_PENDING_DEL);
Chris Masona28ec192007-03-06 20:08:01 -0500243 return 0;
244 }
Chris Masonfec577f2007-02-26 10:40:21 -0500245 key.objectid = blocknr;
246 key.flags = 0;
247 key.offset = num_blocks;
Chris Masona28ec192007-03-06 20:08:01 -0500248 ret = __free_extent(root, blocknr, num_blocks);
249 pending_ret = run_pending(root->extent_root);
Chris Masonfec577f2007-02-26 10:40:21 -0500250 return ret ? ret : pending_ret;
251}
252
253/*
254 * walks the btree of allocated extents and find a hole of a given size.
255 * The key ins is changed to record the hole:
256 * ins->objectid == block start
257 * ins->flags = 0
258 * ins->offset == number of blocks
259 * Any available blocks before search_start are skipped.
260 */
Chris Mason234b63a2007-03-13 10:46:10 -0400261static int find_free_extent(struct btrfs_root *orig_root, u64 num_blocks,
Chris Masone2fa7222007-03-12 16:22:34 -0400262 u64 search_start, u64 search_end,
263 struct btrfs_key *ins)
Chris Masonfec577f2007-02-26 10:40:21 -0500264{
Chris Mason234b63a2007-03-13 10:46:10 -0400265 struct btrfs_path path;
Chris Masone2fa7222007-03-12 16:22:34 -0400266 struct btrfs_key key;
Chris Masonfec577f2007-02-26 10:40:21 -0500267 int ret;
268 u64 hole_size = 0;
269 int slot = 0;
270 u64 last_block;
Chris Mason037e6392007-03-07 11:50:24 -0500271 u64 test_block;
Chris Masonfec577f2007-02-26 10:40:21 -0500272 int start_found;
Chris Mason234b63a2007-03-13 10:46:10 -0400273 struct btrfs_leaf *l;
274 struct btrfs_root * root = orig_root->extent_root;
Chris Mason0579da42007-03-07 16:15:30 -0500275 int total_needed = num_blocks;
Chris Masonfec577f2007-02-26 10:40:21 -0500276
Chris Mason7518a232007-03-12 12:01:18 -0400277 total_needed += (btrfs_header_level(&root->node->node.header) + 1) * 3;
Chris Mason0579da42007-03-07 16:15:30 -0500278 if (root->last_insert.objectid > search_start)
279 search_start = root->last_insert.objectid;
Chris Masonfec577f2007-02-26 10:40:21 -0500280check_failed:
Chris Mason234b63a2007-03-13 10:46:10 -0400281 btrfs_init_path(&path);
Chris Masonfec577f2007-02-26 10:40:21 -0500282 ins->objectid = search_start;
283 ins->offset = 0;
284 ins->flags = 0;
285 start_found = 0;
Chris Mason234b63a2007-03-13 10:46:10 -0400286 ret = btrfs_search_slot(root, ins, &path, 0, 0);
Chris Mason0f70abe2007-02-28 16:46:22 -0500287 if (ret < 0)
288 goto error;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500289
Chris Mason0579da42007-03-07 16:15:30 -0500290 if (path.slots[0] > 0)
291 path.slots[0]--;
292
Chris Masonfec577f2007-02-26 10:40:21 -0500293 while (1) {
294 l = &path.nodes[0]->leaf;
295 slot = path.slots[0];
Chris Mason7518a232007-03-12 12:01:18 -0400296 if (slot >= btrfs_header_nritems(&l->header)) {
Chris Mason234b63a2007-03-13 10:46:10 -0400297 ret = btrfs_next_leaf(root, &path);
Chris Masonfec577f2007-02-26 10:40:21 -0500298 if (ret == 0)
299 continue;
Chris Mason0f70abe2007-02-28 16:46:22 -0500300 if (ret < 0)
301 goto error;
Chris Masonfec577f2007-02-26 10:40:21 -0500302 if (!start_found) {
303 ins->objectid = search_start;
Chris Mason037e6392007-03-07 11:50:24 -0500304 ins->offset = (u64)-1;
Chris Masonfec577f2007-02-26 10:40:21 -0500305 start_found = 1;
306 goto check_pending;
307 }
308 ins->objectid = last_block > search_start ?
309 last_block : search_start;
Chris Mason037e6392007-03-07 11:50:24 -0500310 ins->offset = (u64)-1;
Chris Masonfec577f2007-02-26 10:40:21 -0500311 goto check_pending;
312 }
Chris Masone2fa7222007-03-12 16:22:34 -0400313 btrfs_disk_key_to_cpu(&key, &l->items[slot].key);
314 if (key.objectid >= search_start) {
Chris Masonfec577f2007-02-26 10:40:21 -0500315 if (start_found) {
Chris Mason0579da42007-03-07 16:15:30 -0500316 if (last_block < search_start)
317 last_block = search_start;
Chris Masone2fa7222007-03-12 16:22:34 -0400318 hole_size = key.objectid - last_block;
Chris Mason037e6392007-03-07 11:50:24 -0500319 if (hole_size > total_needed) {
Chris Masonfec577f2007-02-26 10:40:21 -0500320 ins->objectid = last_block;
Chris Mason037e6392007-03-07 11:50:24 -0500321 ins->offset = hole_size;
Chris Masonfec577f2007-02-26 10:40:21 -0500322 goto check_pending;
323 }
Chris Mason0579da42007-03-07 16:15:30 -0500324 }
Chris Masonfec577f2007-02-26 10:40:21 -0500325 }
Chris Mason0579da42007-03-07 16:15:30 -0500326 start_found = 1;
Chris Masone2fa7222007-03-12 16:22:34 -0400327 last_block = key.objectid + key.offset;
Chris Masonfec577f2007-02-26 10:40:21 -0500328 path.slots[0]++;
329 }
330 // FIXME -ENOSPC
331check_pending:
332 /* we have to make sure we didn't find an extent that has already
333 * been allocated by the map tree or the original allocation
334 */
Chris Mason234b63a2007-03-13 10:46:10 -0400335 btrfs_release_path(root, &path);
Chris Masonfec577f2007-02-26 10:40:21 -0500336 BUG_ON(ins->objectid < search_start);
Chris Mason037e6392007-03-07 11:50:24 -0500337 for (test_block = ins->objectid;
338 test_block < ins->objectid + total_needed; test_block++) {
339 if (radix_tree_lookup(&root->pinned_radix, test_block)) {
340 search_start = test_block + 1;
Chris Masonfec577f2007-02-26 10:40:21 -0500341 goto check_failed;
342 }
343 }
Chris Mason037e6392007-03-07 11:50:24 -0500344 BUG_ON(root->current_insert.offset);
Chris Mason0579da42007-03-07 16:15:30 -0500345 root->current_insert.offset = total_needed - num_blocks;
Chris Mason037e6392007-03-07 11:50:24 -0500346 root->current_insert.objectid = ins->objectid + num_blocks;
347 root->current_insert.flags = 0;
Chris Mason0579da42007-03-07 16:15:30 -0500348 root->last_insert.objectid = ins->objectid;
Chris Mason037e6392007-03-07 11:50:24 -0500349 ins->offset = num_blocks;
Chris Masonfec577f2007-02-26 10:40:21 -0500350 return 0;
Chris Mason0f70abe2007-02-28 16:46:22 -0500351error:
Chris Mason234b63a2007-03-13 10:46:10 -0400352 btrfs_release_path(root, &path);
Chris Mason0f70abe2007-02-28 16:46:22 -0500353 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -0500354}
355
356/*
Chris Masonfec577f2007-02-26 10:40:21 -0500357 * finds a free extent and does all the dirty work required for allocation
358 * returns the key for the extent through ins, and a tree buffer for
359 * the first block of the extent through buf.
360 *
361 * returns 0 if everything worked, non-zero otherwise.
362 */
Chris Mason9aca1d52007-03-13 11:09:37 -0400363static int alloc_extent(struct btrfs_root *root, u64 num_blocks,
364 u64 search_start, u64 search_end, u64 owner,
365 struct btrfs_key *ins)
Chris Masonfec577f2007-02-26 10:40:21 -0500366{
367 int ret;
368 int pending_ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400369 struct btrfs_root *extent_root = root->extent_root;
370 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -0500371
Chris Masoncf27e1e2007-03-13 09:49:06 -0400372 btrfs_set_extent_refs(&extent_item, 1);
373 btrfs_set_extent_owner(&extent_item, owner);
Chris Masonfec577f2007-02-26 10:40:21 -0500374
Chris Mason037e6392007-03-07 11:50:24 -0500375 if (root == extent_root) {
376 BUG_ON(extent_root->current_insert.offset == 0);
377 BUG_ON(num_blocks != 1);
378 BUG_ON(extent_root->current_insert.flags ==
379 extent_root->current_insert.offset);
380 ins->offset = 1;
381 ins->objectid = extent_root->current_insert.objectid +
382 extent_root->current_insert.flags++;
Chris Masonfec577f2007-02-26 10:40:21 -0500383 return 0;
384 }
Chris Mason037e6392007-03-07 11:50:24 -0500385 ret = find_free_extent(root, num_blocks, search_start,
386 search_end, ins);
387 if (ret)
388 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -0500389
Chris Mason234b63a2007-03-13 10:46:10 -0400390 ret = btrfs_insert_item(extent_root, ins, &extent_item,
Chris Mason037e6392007-03-07 11:50:24 -0500391 sizeof(extent_item));
392
393 finish_current_insert(extent_root);
394 pending_ret = run_pending(extent_root);
395 if (ret)
396 return ret;
397 if (pending_ret)
398 return pending_ret;
399 return 0;
Chris Masonfec577f2007-02-26 10:40:21 -0500400}
401
402/*
403 * helper function to allocate a block for a given tree
404 * returns the tree buffer or NULL.
405 */
Chris Mason234b63a2007-03-13 10:46:10 -0400406struct btrfs_buffer *btrfs_alloc_free_block(struct btrfs_root *root)
Chris Masonfec577f2007-02-26 10:40:21 -0500407{
Chris Masone2fa7222007-03-12 16:22:34 -0400408 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -0500409 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400410 struct btrfs_buffer *buf;
Chris Masonfec577f2007-02-26 10:40:21 -0500411
412 ret = alloc_extent(root, 1, 0, (unsigned long)-1,
Chris Mason7518a232007-03-12 12:01:18 -0400413 btrfs_header_parentid(&root->node->node.header),
Chris Mason037e6392007-03-07 11:50:24 -0500414 &ins);
Chris Masonfec577f2007-02-26 10:40:21 -0500415 if (ret) {
416 BUG();
417 return NULL;
418 }
Chris Mason037e6392007-03-07 11:50:24 -0500419 buf = find_tree_block(root, ins.objectid);
420 dirty_tree_block(root, buf);
Chris Masonfec577f2007-02-26 10:40:21 -0500421 return buf;
422}
Chris Masona28ec192007-03-06 20:08:01 -0500423
Chris Mason9aca1d52007-03-13 11:09:37 -0400424/*
425 * helper function for drop_snapshot, this walks down the tree dropping ref
426 * counts as it goes.
427 */
428static int walk_down_tree(struct btrfs_root *root,
429 struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -0500430{
Chris Mason234b63a2007-03-13 10:46:10 -0400431 struct btrfs_buffer *next;
432 struct btrfs_buffer *cur;
Chris Mason20524f02007-03-10 06:35:47 -0500433 u64 blocknr;
434 int ret;
435 u32 refs;
436
437 ret = lookup_block_ref(root, path->nodes[*level]->blocknr, &refs);
438 BUG_ON(ret);
439 if (refs > 1)
440 goto out;
Chris Mason9aca1d52007-03-13 11:09:37 -0400441 /*
442 * walk down to the last node level and free all the leaves
443 */
Chris Mason20524f02007-03-10 06:35:47 -0500444 while(*level > 0) {
445 cur = path->nodes[*level];
Chris Mason7518a232007-03-12 12:01:18 -0400446 if (path->slots[*level] >=
447 btrfs_header_nritems(&cur->node.header))
Chris Mason20524f02007-03-10 06:35:47 -0500448 break;
Chris Mason1d4f8a02007-03-13 09:28:32 -0400449 blocknr = btrfs_node_blockptr(&cur->node, path->slots[*level]);
Chris Mason20524f02007-03-10 06:35:47 -0500450 ret = lookup_block_ref(root, blocknr, &refs);
451 if (refs != 1 || *level == 1) {
452 path->slots[*level]++;
Chris Mason234b63a2007-03-13 10:46:10 -0400453 ret = btrfs_free_extent(root, blocknr, 1);
Chris Mason20524f02007-03-10 06:35:47 -0500454 BUG_ON(ret);
455 continue;
456 }
457 BUG_ON(ret);
458 next = read_tree_block(root, blocknr);
Chris Mason83e15a22007-03-12 09:03:27 -0400459 if (path->nodes[*level-1])
Chris Mason234b63a2007-03-13 10:46:10 -0400460 btrfs_block_release(root, path->nodes[*level-1]);
Chris Mason20524f02007-03-10 06:35:47 -0500461 path->nodes[*level-1] = next;
Chris Mason7518a232007-03-12 12:01:18 -0400462 *level = btrfs_header_level(&next->node.header);
Chris Mason20524f02007-03-10 06:35:47 -0500463 path->slots[*level] = 0;
464 }
465out:
Chris Mason234b63a2007-03-13 10:46:10 -0400466 ret = btrfs_free_extent(root, path->nodes[*level]->blocknr, 1);
467 btrfs_block_release(root, path->nodes[*level]);
Chris Mason20524f02007-03-10 06:35:47 -0500468 path->nodes[*level] = NULL;
469 *level += 1;
470 BUG_ON(ret);
471 return 0;
472}
473
Chris Mason9aca1d52007-03-13 11:09:37 -0400474/*
475 * helper for dropping snapshots. This walks back up the tree in the path
476 * to find the first node higher up where we haven't yet gone through
477 * all the slots
478 */
479static int walk_up_tree(struct btrfs_root *root, struct btrfs_path *path,
480 int *level)
Chris Mason20524f02007-03-10 06:35:47 -0500481{
482 int i;
483 int slot;
484 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400485 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Mason20524f02007-03-10 06:35:47 -0500486 slot = path->slots[i];
Chris Mason7518a232007-03-12 12:01:18 -0400487 if (slot <
488 btrfs_header_nritems(&path->nodes[i]->node.header)- 1) {
Chris Mason20524f02007-03-10 06:35:47 -0500489 path->slots[i]++;
490 *level = i;
491 return 0;
492 } else {
Chris Mason234b63a2007-03-13 10:46:10 -0400493 ret = btrfs_free_extent(root,
Chris Mason20524f02007-03-10 06:35:47 -0500494 path->nodes[*level]->blocknr, 1);
Chris Mason234b63a2007-03-13 10:46:10 -0400495 btrfs_block_release(root, path->nodes[*level]);
Chris Mason83e15a22007-03-12 09:03:27 -0400496 path->nodes[*level] = NULL;
Chris Mason20524f02007-03-10 06:35:47 -0500497 *level = i + 1;
498 BUG_ON(ret);
499 }
500 }
501 return 1;
502}
503
Chris Mason9aca1d52007-03-13 11:09:37 -0400504/*
505 * drop the reference count on the tree rooted at 'snap'. This traverses
506 * the tree freeing any blocks that have a ref count of zero after being
507 * decremented.
508 */
Chris Mason234b63a2007-03-13 10:46:10 -0400509int btrfs_drop_snapshot(struct btrfs_root *root, struct btrfs_buffer *snap)
Chris Mason20524f02007-03-10 06:35:47 -0500510{
Chris Mason3768f362007-03-13 16:47:54 -0400511 int ret = 0;
Chris Mason9aca1d52007-03-13 11:09:37 -0400512 int wret;
Chris Mason20524f02007-03-10 06:35:47 -0500513 int level;
Chris Mason234b63a2007-03-13 10:46:10 -0400514 struct btrfs_path path;
Chris Mason20524f02007-03-10 06:35:47 -0500515 int i;
516 int orig_level;
517
Chris Mason234b63a2007-03-13 10:46:10 -0400518 btrfs_init_path(&path);
Chris Mason20524f02007-03-10 06:35:47 -0500519
Chris Mason7518a232007-03-12 12:01:18 -0400520 level = btrfs_header_level(&snap->node.header);
Chris Mason20524f02007-03-10 06:35:47 -0500521 orig_level = level;
522 path.nodes[level] = snap;
523 path.slots[level] = 0;
524 while(1) {
Chris Mason9aca1d52007-03-13 11:09:37 -0400525 wret = walk_down_tree(root, &path, &level);
526 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -0500527 break;
Chris Mason9aca1d52007-03-13 11:09:37 -0400528 if (wret < 0)
529 ret = wret;
530
531 wret = walk_up_tree(root, &path, &level);
532 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -0500533 break;
Chris Mason9aca1d52007-03-13 11:09:37 -0400534 if (wret < 0)
535 ret = wret;
Chris Mason20524f02007-03-10 06:35:47 -0500536 }
Chris Mason83e15a22007-03-12 09:03:27 -0400537 for (i = 0; i <= orig_level; i++) {
538 if (path.nodes[i]) {
Chris Mason234b63a2007-03-13 10:46:10 -0400539 btrfs_block_release(root, path.nodes[i]);
Chris Mason83e15a22007-03-12 09:03:27 -0400540 }
Chris Mason20524f02007-03-10 06:35:47 -0500541 }
Chris Mason9aca1d52007-03-13 11:09:37 -0400542 return ret;
Chris Mason20524f02007-03-10 06:35:47 -0500543}