blob: d4f1ec328399e904ad30274e90bfe1d65e316a5b [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 Mason234b63a2007-03-13 10:46:10 -0400146 struct btrfs_extent_item *ei;
Chris Masone2fa7222007-03-12 16:22:34 -0400147 struct btrfs_key ins;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400148 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500149
Chris Masona28ec192007-03-06 20:08:01 -0500150 key.objectid = blocknr;
151 key.flags = 0;
152 key.offset = num_blocks;
153
Chris Mason037e6392007-03-07 11:50:24 -0500154 find_free_extent(root, 0, 0, (u64)-1, &ins);
Chris Mason234b63a2007-03-13 10:46:10 -0400155 btrfs_init_path(&path);
156 ret = btrfs_search_slot(extent_root, &key, &path, -1, 1);
Chris Masona28ec192007-03-06 20:08:01 -0500157 if (ret) {
158 printf("failed to find %Lu\n", key.objectid);
Chris Mason234b63a2007-03-13 10:46:10 -0400159 btrfs_print_tree(extent_root, extent_root->node);
Chris Masona28ec192007-03-06 20:08:01 -0500160 printf("failed to find %Lu\n", key.objectid);
161 BUG();
162 }
Chris Mason123abc82007-03-14 14:14:43 -0400163 ei = btrfs_item_ptr(&path.nodes[0]->leaf, path.slots[0],
164 struct btrfs_extent_item);
Chris Masona28ec192007-03-06 20:08:01 -0500165 BUG_ON(ei->refs == 0);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400166 refs = btrfs_extent_refs(ei) - 1;
167 btrfs_set_extent_refs(ei, refs);
168 if (refs == 0) {
Chris Mason3768f362007-03-13 16:47:54 -0400169 if (!root->ref_cows) {
Chris Masona28ec192007-03-06 20:08:01 -0500170 int err;
171 radix_tree_preload(GFP_KERNEL);
172 err = radix_tree_insert(&extent_root->pinned_radix,
173 blocknr, (void *)blocknr);
174 BUG_ON(err);
175 radix_tree_preload_end();
176 }
Chris Mason234b63a2007-03-13 10:46:10 -0400177 ret = btrfs_del_item(extent_root, &path);
Chris Mason0579da42007-03-07 16:15:30 -0500178 if (root != extent_root &&
Chris Mason71087492007-03-14 09:20:39 -0400179 extent_root->last_insert.objectid > blocknr)
Chris Mason0579da42007-03-07 16:15:30 -0500180 extent_root->last_insert.objectid = blocknr;
Chris Masona28ec192007-03-06 20:08:01 -0500181 if (ret)
182 BUG();
183 }
Chris Mason234b63a2007-03-13 10:46:10 -0400184 btrfs_release_path(extent_root, &path);
Chris Mason037e6392007-03-07 11:50:24 -0500185 finish_current_insert(extent_root);
Chris Masona28ec192007-03-06 20:08:01 -0500186 return ret;
187}
188
189/*
Chris Masonfec577f2007-02-26 10:40:21 -0500190 * find all the blocks marked as pending in the radix tree and remove
191 * them from the extent map
192 */
Chris Mason234b63a2007-03-13 10:46:10 -0400193static int del_pending_extents(struct btrfs_root *extent_root)
Chris Masonfec577f2007-02-26 10:40:21 -0500194{
195 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400196 struct btrfs_buffer *gang[4];
Chris Masonfec577f2007-02-26 10:40:21 -0500197 int i;
Chris Masonfec577f2007-02-26 10:40:21 -0500198
199 while(1) {
200 ret = radix_tree_gang_lookup_tag(&extent_root->cache_radix,
201 (void **)gang, 0,
202 ARRAY_SIZE(gang),
Chris Masona28ec192007-03-06 20:08:01 -0500203 CTREE_EXTENT_PENDING_DEL);
Chris Masonfec577f2007-02-26 10:40:21 -0500204 if (!ret)
205 break;
206 for (i = 0; i < ret; i++) {
Chris Masona28ec192007-03-06 20:08:01 -0500207 ret = __free_extent(extent_root, gang[i]->blocknr, 1);
Chris Masonfec577f2007-02-26 10:40:21 -0500208 radix_tree_tag_clear(&extent_root->cache_radix,
209 gang[i]->blocknr,
Chris Masona28ec192007-03-06 20:08:01 -0500210 CTREE_EXTENT_PENDING_DEL);
Chris Mason234b63a2007-03-13 10:46:10 -0400211 btrfs_block_release(extent_root, gang[i]);
Chris Masonfec577f2007-02-26 10:40:21 -0500212 }
213 }
214 return 0;
215}
216
Chris Mason234b63a2007-03-13 10:46:10 -0400217static int run_pending(struct btrfs_root *extent_root)
Chris Masona28ec192007-03-06 20:08:01 -0500218{
219 while(radix_tree_tagged(&extent_root->cache_radix,
Chris Mason037e6392007-03-07 11:50:24 -0500220 CTREE_EXTENT_PENDING_DEL))
Chris Masona28ec192007-03-06 20:08:01 -0500221 del_pending_extents(extent_root);
Chris Masona28ec192007-03-06 20:08:01 -0500222 return 0;
223}
224
225
Chris Masonfec577f2007-02-26 10:40:21 -0500226/*
227 * remove an extent from the root, returns 0 on success
228 */
Chris Mason234b63a2007-03-13 10:46:10 -0400229int btrfs_free_extent(struct btrfs_root *root, u64 blocknr, u64 num_blocks)
Chris Masonfec577f2007-02-26 10:40:21 -0500230{
Chris Masone2fa7222007-03-12 16:22:34 -0400231 struct btrfs_key key;
Chris Mason234b63a2007-03-13 10:46:10 -0400232 struct btrfs_root *extent_root = root->extent_root;
233 struct btrfs_buffer *t;
Chris Masonfec577f2007-02-26 10:40:21 -0500234 int pending_ret;
235 int ret;
Chris Masona28ec192007-03-06 20:08:01 -0500236
237 if (root == extent_root) {
238 t = find_tree_block(root, blocknr);
Chris Mason037e6392007-03-07 11:50:24 -0500239 radix_tree_tag_set(&root->cache_radix, blocknr,
Chris Masona28ec192007-03-06 20:08:01 -0500240 CTREE_EXTENT_PENDING_DEL);
Chris Masona28ec192007-03-06 20:08:01 -0500241 return 0;
242 }
Chris Masonfec577f2007-02-26 10:40:21 -0500243 key.objectid = blocknr;
244 key.flags = 0;
245 key.offset = num_blocks;
Chris Masona28ec192007-03-06 20:08:01 -0500246 ret = __free_extent(root, blocknr, num_blocks);
247 pending_ret = run_pending(root->extent_root);
Chris Masonfec577f2007-02-26 10:40:21 -0500248 return ret ? ret : pending_ret;
249}
250
251/*
252 * walks the btree of allocated extents and find a hole of a given size.
253 * The key ins is changed to record the hole:
254 * ins->objectid == block start
255 * ins->flags = 0
256 * ins->offset == number of blocks
257 * Any available blocks before search_start are skipped.
258 */
Chris Mason234b63a2007-03-13 10:46:10 -0400259static int find_free_extent(struct btrfs_root *orig_root, u64 num_blocks,
Chris Masone2fa7222007-03-12 16:22:34 -0400260 u64 search_start, u64 search_end,
261 struct btrfs_key *ins)
Chris Masonfec577f2007-02-26 10:40:21 -0500262{
Chris Mason234b63a2007-03-13 10:46:10 -0400263 struct btrfs_path path;
Chris Masone2fa7222007-03-12 16:22:34 -0400264 struct btrfs_key key;
Chris Masonfec577f2007-02-26 10:40:21 -0500265 int ret;
266 u64 hole_size = 0;
267 int slot = 0;
268 u64 last_block;
Chris Mason037e6392007-03-07 11:50:24 -0500269 u64 test_block;
Chris Masonfec577f2007-02-26 10:40:21 -0500270 int start_found;
Chris Mason234b63a2007-03-13 10:46:10 -0400271 struct btrfs_leaf *l;
272 struct btrfs_root * root = orig_root->extent_root;
Chris Mason0579da42007-03-07 16:15:30 -0500273 int total_needed = num_blocks;
Chris Masonfec577f2007-02-26 10:40:21 -0500274
Chris Mason7518a232007-03-12 12:01:18 -0400275 total_needed += (btrfs_header_level(&root->node->node.header) + 1) * 3;
Chris Mason0579da42007-03-07 16:15:30 -0500276 if (root->last_insert.objectid > search_start)
277 search_start = root->last_insert.objectid;
Chris Masonfec577f2007-02-26 10:40:21 -0500278check_failed:
Chris Mason234b63a2007-03-13 10:46:10 -0400279 btrfs_init_path(&path);
Chris Masonfec577f2007-02-26 10:40:21 -0500280 ins->objectid = search_start;
281 ins->offset = 0;
282 ins->flags = 0;
283 start_found = 0;
Chris Mason234b63a2007-03-13 10:46:10 -0400284 ret = btrfs_search_slot(root, ins, &path, 0, 0);
Chris Mason0f70abe2007-02-28 16:46:22 -0500285 if (ret < 0)
286 goto error;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500287
Chris Mason0579da42007-03-07 16:15:30 -0500288 if (path.slots[0] > 0)
289 path.slots[0]--;
290
Chris Masonfec577f2007-02-26 10:40:21 -0500291 while (1) {
292 l = &path.nodes[0]->leaf;
293 slot = path.slots[0];
Chris Mason7518a232007-03-12 12:01:18 -0400294 if (slot >= btrfs_header_nritems(&l->header)) {
Chris Mason234b63a2007-03-13 10:46:10 -0400295 ret = btrfs_next_leaf(root, &path);
Chris Masonfec577f2007-02-26 10:40:21 -0500296 if (ret == 0)
297 continue;
Chris Mason0f70abe2007-02-28 16:46:22 -0500298 if (ret < 0)
299 goto error;
Chris Masonfec577f2007-02-26 10:40:21 -0500300 if (!start_found) {
301 ins->objectid = search_start;
Chris Mason037e6392007-03-07 11:50:24 -0500302 ins->offset = (u64)-1;
Chris Masonfec577f2007-02-26 10:40:21 -0500303 start_found = 1;
304 goto check_pending;
305 }
306 ins->objectid = last_block > search_start ?
307 last_block : search_start;
Chris Mason037e6392007-03-07 11:50:24 -0500308 ins->offset = (u64)-1;
Chris Masonfec577f2007-02-26 10:40:21 -0500309 goto check_pending;
310 }
Chris Masone2fa7222007-03-12 16:22:34 -0400311 btrfs_disk_key_to_cpu(&key, &l->items[slot].key);
312 if (key.objectid >= search_start) {
Chris Masonfec577f2007-02-26 10:40:21 -0500313 if (start_found) {
Chris Mason0579da42007-03-07 16:15:30 -0500314 if (last_block < search_start)
315 last_block = search_start;
Chris Masone2fa7222007-03-12 16:22:34 -0400316 hole_size = key.objectid - last_block;
Chris Mason037e6392007-03-07 11:50:24 -0500317 if (hole_size > total_needed) {
Chris Masonfec577f2007-02-26 10:40:21 -0500318 ins->objectid = last_block;
Chris Mason037e6392007-03-07 11:50:24 -0500319 ins->offset = hole_size;
Chris Masonfec577f2007-02-26 10:40:21 -0500320 goto check_pending;
321 }
Chris Mason0579da42007-03-07 16:15:30 -0500322 }
Chris Masonfec577f2007-02-26 10:40:21 -0500323 }
Chris Mason0579da42007-03-07 16:15:30 -0500324 start_found = 1;
Chris Masone2fa7222007-03-12 16:22:34 -0400325 last_block = key.objectid + key.offset;
Chris Masonfec577f2007-02-26 10:40:21 -0500326 path.slots[0]++;
327 }
328 // FIXME -ENOSPC
329check_pending:
330 /* we have to make sure we didn't find an extent that has already
331 * been allocated by the map tree or the original allocation
332 */
Chris Mason234b63a2007-03-13 10:46:10 -0400333 btrfs_release_path(root, &path);
Chris Masonfec577f2007-02-26 10:40:21 -0500334 BUG_ON(ins->objectid < search_start);
Chris Mason037e6392007-03-07 11:50:24 -0500335 for (test_block = ins->objectid;
336 test_block < ins->objectid + total_needed; test_block++) {
337 if (radix_tree_lookup(&root->pinned_radix, test_block)) {
338 search_start = test_block + 1;
Chris Masonfec577f2007-02-26 10:40:21 -0500339 goto check_failed;
340 }
341 }
Chris Mason037e6392007-03-07 11:50:24 -0500342 BUG_ON(root->current_insert.offset);
Chris Mason0579da42007-03-07 16:15:30 -0500343 root->current_insert.offset = total_needed - num_blocks;
Chris Mason037e6392007-03-07 11:50:24 -0500344 root->current_insert.objectid = ins->objectid + num_blocks;
345 root->current_insert.flags = 0;
Chris Mason0579da42007-03-07 16:15:30 -0500346 root->last_insert.objectid = ins->objectid;
Chris Mason037e6392007-03-07 11:50:24 -0500347 ins->offset = num_blocks;
Chris Masonfec577f2007-02-26 10:40:21 -0500348 return 0;
Chris Mason0f70abe2007-02-28 16:46:22 -0500349error:
Chris Mason234b63a2007-03-13 10:46:10 -0400350 btrfs_release_path(root, &path);
Chris Mason0f70abe2007-02-28 16:46:22 -0500351 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -0500352}
353
354/*
Chris Masonfec577f2007-02-26 10:40:21 -0500355 * finds a free extent and does all the dirty work required for allocation
356 * returns the key for the extent through ins, and a tree buffer for
357 * the first block of the extent through buf.
358 *
359 * returns 0 if everything worked, non-zero otherwise.
360 */
Chris Mason9aca1d52007-03-13 11:09:37 -0400361static int alloc_extent(struct btrfs_root *root, u64 num_blocks,
362 u64 search_start, u64 search_end, u64 owner,
363 struct btrfs_key *ins)
Chris Masonfec577f2007-02-26 10:40:21 -0500364{
365 int ret;
366 int pending_ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400367 struct btrfs_root *extent_root = root->extent_root;
368 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -0500369
Chris Masoncf27e1e2007-03-13 09:49:06 -0400370 btrfs_set_extent_refs(&extent_item, 1);
371 btrfs_set_extent_owner(&extent_item, owner);
Chris Masonfec577f2007-02-26 10:40:21 -0500372
Chris Mason037e6392007-03-07 11:50:24 -0500373 if (root == extent_root) {
374 BUG_ON(extent_root->current_insert.offset == 0);
375 BUG_ON(num_blocks != 1);
376 BUG_ON(extent_root->current_insert.flags ==
377 extent_root->current_insert.offset);
378 ins->offset = 1;
379 ins->objectid = extent_root->current_insert.objectid +
380 extent_root->current_insert.flags++;
Chris Masonfec577f2007-02-26 10:40:21 -0500381 return 0;
382 }
Chris Mason037e6392007-03-07 11:50:24 -0500383 ret = find_free_extent(root, num_blocks, search_start,
384 search_end, ins);
385 if (ret)
386 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -0500387
Chris Mason234b63a2007-03-13 10:46:10 -0400388 ret = btrfs_insert_item(extent_root, ins, &extent_item,
Chris Mason037e6392007-03-07 11:50:24 -0500389 sizeof(extent_item));
390
391 finish_current_insert(extent_root);
392 pending_ret = run_pending(extent_root);
393 if (ret)
394 return ret;
395 if (pending_ret)
396 return pending_ret;
397 return 0;
Chris Masonfec577f2007-02-26 10:40:21 -0500398}
399
400/*
401 * helper function to allocate a block for a given tree
402 * returns the tree buffer or NULL.
403 */
Chris Mason234b63a2007-03-13 10:46:10 -0400404struct btrfs_buffer *btrfs_alloc_free_block(struct btrfs_root *root)
Chris Masonfec577f2007-02-26 10:40:21 -0500405{
Chris Masone2fa7222007-03-12 16:22:34 -0400406 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -0500407 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400408 struct btrfs_buffer *buf;
Chris Masonfec577f2007-02-26 10:40:21 -0500409
410 ret = alloc_extent(root, 1, 0, (unsigned long)-1,
Chris Mason7518a232007-03-12 12:01:18 -0400411 btrfs_header_parentid(&root->node->node.header),
Chris Mason037e6392007-03-07 11:50:24 -0500412 &ins);
Chris Masonfec577f2007-02-26 10:40:21 -0500413 if (ret) {
414 BUG();
415 return NULL;
416 }
Chris Mason037e6392007-03-07 11:50:24 -0500417 buf = find_tree_block(root, ins.objectid);
418 dirty_tree_block(root, buf);
Chris Masonfec577f2007-02-26 10:40:21 -0500419 return buf;
420}
Chris Masona28ec192007-03-06 20:08:01 -0500421
Chris Mason9aca1d52007-03-13 11:09:37 -0400422/*
423 * helper function for drop_snapshot, this walks down the tree dropping ref
424 * counts as it goes.
425 */
426static int walk_down_tree(struct btrfs_root *root,
427 struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -0500428{
Chris Mason234b63a2007-03-13 10:46:10 -0400429 struct btrfs_buffer *next;
430 struct btrfs_buffer *cur;
Chris Mason20524f02007-03-10 06:35:47 -0500431 u64 blocknr;
432 int ret;
433 u32 refs;
434
435 ret = lookup_block_ref(root, path->nodes[*level]->blocknr, &refs);
436 BUG_ON(ret);
437 if (refs > 1)
438 goto out;
Chris Mason9aca1d52007-03-13 11:09:37 -0400439 /*
440 * walk down to the last node level and free all the leaves
441 */
Chris Mason20524f02007-03-10 06:35:47 -0500442 while(*level > 0) {
443 cur = path->nodes[*level];
Chris Mason7518a232007-03-12 12:01:18 -0400444 if (path->slots[*level] >=
445 btrfs_header_nritems(&cur->node.header))
Chris Mason20524f02007-03-10 06:35:47 -0500446 break;
Chris Mason1d4f8a02007-03-13 09:28:32 -0400447 blocknr = btrfs_node_blockptr(&cur->node, path->slots[*level]);
Chris Mason20524f02007-03-10 06:35:47 -0500448 ret = lookup_block_ref(root, blocknr, &refs);
449 if (refs != 1 || *level == 1) {
450 path->slots[*level]++;
Chris Mason234b63a2007-03-13 10:46:10 -0400451 ret = btrfs_free_extent(root, blocknr, 1);
Chris Mason20524f02007-03-10 06:35:47 -0500452 BUG_ON(ret);
453 continue;
454 }
455 BUG_ON(ret);
456 next = read_tree_block(root, blocknr);
Chris Mason83e15a22007-03-12 09:03:27 -0400457 if (path->nodes[*level-1])
Chris Mason234b63a2007-03-13 10:46:10 -0400458 btrfs_block_release(root, path->nodes[*level-1]);
Chris Mason20524f02007-03-10 06:35:47 -0500459 path->nodes[*level-1] = next;
Chris Mason7518a232007-03-12 12:01:18 -0400460 *level = btrfs_header_level(&next->node.header);
Chris Mason20524f02007-03-10 06:35:47 -0500461 path->slots[*level] = 0;
462 }
463out:
Chris Mason234b63a2007-03-13 10:46:10 -0400464 ret = btrfs_free_extent(root, path->nodes[*level]->blocknr, 1);
465 btrfs_block_release(root, path->nodes[*level]);
Chris Mason20524f02007-03-10 06:35:47 -0500466 path->nodes[*level] = NULL;
467 *level += 1;
468 BUG_ON(ret);
469 return 0;
470}
471
Chris Mason9aca1d52007-03-13 11:09:37 -0400472/*
473 * helper for dropping snapshots. This walks back up the tree in the path
474 * to find the first node higher up where we haven't yet gone through
475 * all the slots
476 */
477static int walk_up_tree(struct btrfs_root *root, struct btrfs_path *path,
478 int *level)
Chris Mason20524f02007-03-10 06:35:47 -0500479{
480 int i;
481 int slot;
482 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400483 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Mason20524f02007-03-10 06:35:47 -0500484 slot = path->slots[i];
Chris Mason7518a232007-03-12 12:01:18 -0400485 if (slot <
486 btrfs_header_nritems(&path->nodes[i]->node.header)- 1) {
Chris Mason20524f02007-03-10 06:35:47 -0500487 path->slots[i]++;
488 *level = i;
489 return 0;
490 } else {
Chris Mason234b63a2007-03-13 10:46:10 -0400491 ret = btrfs_free_extent(root,
Chris Mason20524f02007-03-10 06:35:47 -0500492 path->nodes[*level]->blocknr, 1);
Chris Mason234b63a2007-03-13 10:46:10 -0400493 btrfs_block_release(root, path->nodes[*level]);
Chris Mason83e15a22007-03-12 09:03:27 -0400494 path->nodes[*level] = NULL;
Chris Mason20524f02007-03-10 06:35:47 -0500495 *level = i + 1;
496 BUG_ON(ret);
497 }
498 }
499 return 1;
500}
501
Chris Mason9aca1d52007-03-13 11:09:37 -0400502/*
503 * drop the reference count on the tree rooted at 'snap'. This traverses
504 * the tree freeing any blocks that have a ref count of zero after being
505 * decremented.
506 */
Chris Mason234b63a2007-03-13 10:46:10 -0400507int btrfs_drop_snapshot(struct btrfs_root *root, struct btrfs_buffer *snap)
Chris Mason20524f02007-03-10 06:35:47 -0500508{
Chris Mason3768f362007-03-13 16:47:54 -0400509 int ret = 0;
Chris Mason9aca1d52007-03-13 11:09:37 -0400510 int wret;
Chris Mason20524f02007-03-10 06:35:47 -0500511 int level;
Chris Mason234b63a2007-03-13 10:46:10 -0400512 struct btrfs_path path;
Chris Mason20524f02007-03-10 06:35:47 -0500513 int i;
514 int orig_level;
515
Chris Mason234b63a2007-03-13 10:46:10 -0400516 btrfs_init_path(&path);
Chris Mason20524f02007-03-10 06:35:47 -0500517
Chris Mason7518a232007-03-12 12:01:18 -0400518 level = btrfs_header_level(&snap->node.header);
Chris Mason20524f02007-03-10 06:35:47 -0500519 orig_level = level;
520 path.nodes[level] = snap;
521 path.slots[level] = 0;
522 while(1) {
Chris Mason9aca1d52007-03-13 11:09:37 -0400523 wret = walk_down_tree(root, &path, &level);
524 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -0500525 break;
Chris Mason9aca1d52007-03-13 11:09:37 -0400526 if (wret < 0)
527 ret = wret;
528
529 wret = walk_up_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;
Chris Mason20524f02007-03-10 06:35:47 -0500534 }
Chris Mason83e15a22007-03-12 09:03:27 -0400535 for (i = 0; i <= orig_level; i++) {
536 if (path.nodes[i]) {
Chris Mason234b63a2007-03-13 10:46:10 -0400537 btrfs_block_release(root, path.nodes[i]);
Chris Mason83e15a22007-03-12 09:03:27 -0400538 }
Chris Mason20524f02007-03-10 06:35:47 -0500539 }
Chris Mason9aca1d52007-03-13 11:09:37 -0400540 return ret;
Chris Mason20524f02007-03-10 06:35:47 -0500541}