blob: a6969538bca201b6366b2dcb4f67f9fb9912d773 [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
85 if (root == root->extent_root)
86 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 Mason234b63a2007-03-13 10:46:10 -040099 struct btrfs_root *extent_root = root->extent_root;
Chris Masona28ec192007-03-06 20:08:01 -0500100 unsigned long gang[8];
101 int ret;
102 int i;
103
104 while(1) {
105 ret = radix_tree_gang_lookup(&extent_root->pinned_radix,
106 (void **)gang, 0,
107 ARRAY_SIZE(gang));
108 if (!ret)
109 break;
Chris Mason0579da42007-03-07 16:15:30 -0500110 for (i = 0; i < ret; i++) {
Chris Masona28ec192007-03-06 20:08:01 -0500111 radix_tree_delete(&extent_root->pinned_radix, gang[i]);
Chris Mason0579da42007-03-07 16:15:30 -0500112 }
Chris Masona28ec192007-03-06 20:08:01 -0500113 }
Chris Mason0579da42007-03-07 16:15:30 -0500114 extent_root->last_insert.objectid = 0;
115 extent_root->last_insert.offset = 0;
Chris Masona28ec192007-03-06 20:08:01 -0500116 return 0;
117}
118
Chris Mason234b63a2007-03-13 10:46:10 -0400119static int finish_current_insert(struct btrfs_root *extent_root)
Chris Mason037e6392007-03-07 11:50:24 -0500120{
Chris Masone2fa7222007-03-12 16:22:34 -0400121 struct btrfs_key ins;
Chris Mason234b63a2007-03-13 10:46:10 -0400122 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -0500123 int i;
124 int ret;
125
Chris Masoncf27e1e2007-03-13 09:49:06 -0400126 btrfs_set_extent_refs(&extent_item, 1);
127 btrfs_set_extent_owner(&extent_item,
128 btrfs_header_parentid(&extent_root->node->node.header));
Chris Mason037e6392007-03-07 11:50:24 -0500129 ins.offset = 1;
130 ins.flags = 0;
131
132 for (i = 0; i < extent_root->current_insert.flags; i++) {
133 ins.objectid = extent_root->current_insert.objectid + i;
Chris Mason234b63a2007-03-13 10:46:10 -0400134 ret = btrfs_insert_item(extent_root, &ins, &extent_item,
Chris Mason037e6392007-03-07 11:50:24 -0500135 sizeof(extent_item));
136 BUG_ON(ret);
137 }
138 extent_root->current_insert.offset = 0;
139 return 0;
140}
141
Chris Masona28ec192007-03-06 20:08:01 -0500142/*
143 * remove an extent from the root, returns 0 on success
144 */
Chris Mason234b63a2007-03-13 10:46:10 -0400145static int __free_extent(struct btrfs_root *root, u64 blocknr, u64 num_blocks)
Chris Masona28ec192007-03-06 20:08:01 -0500146{
Chris Mason234b63a2007-03-13 10:46:10 -0400147 struct btrfs_path path;
Chris Masone2fa7222007-03-12 16:22:34 -0400148 struct btrfs_key key;
Chris Mason234b63a2007-03-13 10:46:10 -0400149 struct btrfs_root *extent_root = root->extent_root;
Chris Masona28ec192007-03-06 20:08:01 -0500150 int ret;
Chris Mason0783fcf2007-03-12 20:12:07 -0400151 struct btrfs_item *item;
Chris Mason234b63a2007-03-13 10:46:10 -0400152 struct btrfs_extent_item *ei;
Chris Masone2fa7222007-03-12 16:22:34 -0400153 struct btrfs_key ins;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400154 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500155
Chris Masona28ec192007-03-06 20:08:01 -0500156 key.objectid = blocknr;
157 key.flags = 0;
158 key.offset = num_blocks;
159
Chris Mason037e6392007-03-07 11:50:24 -0500160 find_free_extent(root, 0, 0, (u64)-1, &ins);
Chris Mason234b63a2007-03-13 10:46:10 -0400161 btrfs_init_path(&path);
162 ret = btrfs_search_slot(extent_root, &key, &path, -1, 1);
Chris Masona28ec192007-03-06 20:08:01 -0500163 if (ret) {
164 printf("failed to find %Lu\n", key.objectid);
Chris Mason234b63a2007-03-13 10:46:10 -0400165 btrfs_print_tree(extent_root, extent_root->node);
Chris Masona28ec192007-03-06 20:08:01 -0500166 printf("failed to find %Lu\n", key.objectid);
167 BUG();
168 }
169 item = path.nodes[0]->leaf.items + path.slots[0];
Chris Mason234b63a2007-03-13 10:46:10 -0400170 ei = (struct btrfs_extent_item *)(path.nodes[0]->leaf.data +
Chris Mason0783fcf2007-03-12 20:12:07 -0400171 btrfs_item_offset(item));
Chris Masona28ec192007-03-06 20:08:01 -0500172 BUG_ON(ei->refs == 0);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400173 refs = btrfs_extent_refs(ei) - 1;
174 btrfs_set_extent_refs(ei, refs);
175 if (refs == 0) {
Chris Masona28ec192007-03-06 20:08:01 -0500176 if (root == extent_root) {
177 int err;
178 radix_tree_preload(GFP_KERNEL);
179 err = radix_tree_insert(&extent_root->pinned_radix,
180 blocknr, (void *)blocknr);
181 BUG_ON(err);
182 radix_tree_preload_end();
183 }
Chris Mason234b63a2007-03-13 10:46:10 -0400184 ret = btrfs_del_item(extent_root, &path);
Chris Mason0579da42007-03-07 16:15:30 -0500185 if (root != extent_root &&
186 extent_root->last_insert.objectid < blocknr)
187 extent_root->last_insert.objectid = blocknr;
Chris Masona28ec192007-03-06 20:08:01 -0500188 if (ret)
189 BUG();
190 }
Chris Mason234b63a2007-03-13 10:46:10 -0400191 btrfs_release_path(extent_root, &path);
Chris Mason037e6392007-03-07 11:50:24 -0500192 finish_current_insert(extent_root);
Chris Masona28ec192007-03-06 20:08:01 -0500193 return ret;
194}
195
196/*
Chris Masonfec577f2007-02-26 10:40:21 -0500197 * find all the blocks marked as pending in the radix tree and remove
198 * them from the extent map
199 */
Chris Mason234b63a2007-03-13 10:46:10 -0400200static int del_pending_extents(struct btrfs_root *extent_root)
Chris Masonfec577f2007-02-26 10:40:21 -0500201{
202 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400203 struct btrfs_buffer *gang[4];
Chris Masonfec577f2007-02-26 10:40:21 -0500204 int i;
Chris Masonfec577f2007-02-26 10:40:21 -0500205
206 while(1) {
207 ret = radix_tree_gang_lookup_tag(&extent_root->cache_radix,
208 (void **)gang, 0,
209 ARRAY_SIZE(gang),
Chris Masona28ec192007-03-06 20:08:01 -0500210 CTREE_EXTENT_PENDING_DEL);
Chris Masonfec577f2007-02-26 10:40:21 -0500211 if (!ret)
212 break;
213 for (i = 0; i < ret; i++) {
Chris Masona28ec192007-03-06 20:08:01 -0500214 ret = __free_extent(extent_root, gang[i]->blocknr, 1);
Chris Masonfec577f2007-02-26 10:40:21 -0500215 radix_tree_tag_clear(&extent_root->cache_radix,
216 gang[i]->blocknr,
Chris Masona28ec192007-03-06 20:08:01 -0500217 CTREE_EXTENT_PENDING_DEL);
Chris Mason234b63a2007-03-13 10:46:10 -0400218 btrfs_block_release(extent_root, gang[i]);
Chris Masonfec577f2007-02-26 10:40:21 -0500219 }
220 }
221 return 0;
222}
223
Chris Mason234b63a2007-03-13 10:46:10 -0400224static int run_pending(struct btrfs_root *extent_root)
Chris Masona28ec192007-03-06 20:08:01 -0500225{
226 while(radix_tree_tagged(&extent_root->cache_radix,
Chris Mason037e6392007-03-07 11:50:24 -0500227 CTREE_EXTENT_PENDING_DEL))
Chris Masona28ec192007-03-06 20:08:01 -0500228 del_pending_extents(extent_root);
Chris Masona28ec192007-03-06 20:08:01 -0500229 return 0;
230}
231
232
Chris Masonfec577f2007-02-26 10:40:21 -0500233/*
234 * remove an extent from the root, returns 0 on success
235 */
Chris Mason234b63a2007-03-13 10:46:10 -0400236int btrfs_free_extent(struct btrfs_root *root, u64 blocknr, u64 num_blocks)
Chris Masonfec577f2007-02-26 10:40:21 -0500237{
Chris Masone2fa7222007-03-12 16:22:34 -0400238 struct btrfs_key key;
Chris Mason234b63a2007-03-13 10:46:10 -0400239 struct btrfs_root *extent_root = root->extent_root;
240 struct btrfs_buffer *t;
Chris Masonfec577f2007-02-26 10:40:21 -0500241 int pending_ret;
242 int ret;
Chris Masona28ec192007-03-06 20:08:01 -0500243
244 if (root == extent_root) {
245 t = find_tree_block(root, blocknr);
Chris Mason037e6392007-03-07 11:50:24 -0500246 radix_tree_tag_set(&root->cache_radix, blocknr,
Chris Masona28ec192007-03-06 20:08:01 -0500247 CTREE_EXTENT_PENDING_DEL);
Chris Masona28ec192007-03-06 20:08:01 -0500248 return 0;
249 }
Chris Masonfec577f2007-02-26 10:40:21 -0500250 key.objectid = blocknr;
251 key.flags = 0;
252 key.offset = num_blocks;
Chris Masona28ec192007-03-06 20:08:01 -0500253 ret = __free_extent(root, blocknr, num_blocks);
254 pending_ret = run_pending(root->extent_root);
Chris Masonfec577f2007-02-26 10:40:21 -0500255 return ret ? ret : pending_ret;
256}
257
258/*
259 * walks the btree of allocated extents and find a hole of a given size.
260 * The key ins is changed to record the hole:
261 * ins->objectid == block start
262 * ins->flags = 0
263 * ins->offset == number of blocks
264 * Any available blocks before search_start are skipped.
265 */
Chris Mason234b63a2007-03-13 10:46:10 -0400266static int find_free_extent(struct btrfs_root *orig_root, u64 num_blocks,
Chris Masone2fa7222007-03-12 16:22:34 -0400267 u64 search_start, u64 search_end,
268 struct btrfs_key *ins)
Chris Masonfec577f2007-02-26 10:40:21 -0500269{
Chris Mason234b63a2007-03-13 10:46:10 -0400270 struct btrfs_path path;
Chris Masone2fa7222007-03-12 16:22:34 -0400271 struct btrfs_key key;
Chris Masonfec577f2007-02-26 10:40:21 -0500272 int ret;
273 u64 hole_size = 0;
274 int slot = 0;
275 u64 last_block;
Chris Mason037e6392007-03-07 11:50:24 -0500276 u64 test_block;
Chris Masonfec577f2007-02-26 10:40:21 -0500277 int start_found;
Chris Mason234b63a2007-03-13 10:46:10 -0400278 struct btrfs_leaf *l;
279 struct btrfs_root * root = orig_root->extent_root;
Chris Mason0579da42007-03-07 16:15:30 -0500280 int total_needed = num_blocks;
Chris Masonfec577f2007-02-26 10:40:21 -0500281
Chris Mason7518a232007-03-12 12:01:18 -0400282 total_needed += (btrfs_header_level(&root->node->node.header) + 1) * 3;
Chris Mason0579da42007-03-07 16:15:30 -0500283 if (root->last_insert.objectid > search_start)
284 search_start = root->last_insert.objectid;
Chris Masonfec577f2007-02-26 10:40:21 -0500285check_failed:
Chris Mason234b63a2007-03-13 10:46:10 -0400286 btrfs_init_path(&path);
Chris Masonfec577f2007-02-26 10:40:21 -0500287 ins->objectid = search_start;
288 ins->offset = 0;
289 ins->flags = 0;
290 start_found = 0;
Chris Mason234b63a2007-03-13 10:46:10 -0400291 ret = btrfs_search_slot(root, ins, &path, 0, 0);
Chris Mason0f70abe2007-02-28 16:46:22 -0500292 if (ret < 0)
293 goto error;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500294
Chris Mason0579da42007-03-07 16:15:30 -0500295 if (path.slots[0] > 0)
296 path.slots[0]--;
297
Chris Masonfec577f2007-02-26 10:40:21 -0500298 while (1) {
299 l = &path.nodes[0]->leaf;
300 slot = path.slots[0];
Chris Mason7518a232007-03-12 12:01:18 -0400301 if (slot >= btrfs_header_nritems(&l->header)) {
Chris Mason234b63a2007-03-13 10:46:10 -0400302 ret = btrfs_next_leaf(root, &path);
Chris Masonfec577f2007-02-26 10:40:21 -0500303 if (ret == 0)
304 continue;
Chris Mason0f70abe2007-02-28 16:46:22 -0500305 if (ret < 0)
306 goto error;
Chris Masonfec577f2007-02-26 10:40:21 -0500307 if (!start_found) {
308 ins->objectid = search_start;
Chris Mason037e6392007-03-07 11:50:24 -0500309 ins->offset = (u64)-1;
Chris Masonfec577f2007-02-26 10:40:21 -0500310 start_found = 1;
311 goto check_pending;
312 }
313 ins->objectid = last_block > search_start ?
314 last_block : search_start;
Chris Mason037e6392007-03-07 11:50:24 -0500315 ins->offset = (u64)-1;
Chris Masonfec577f2007-02-26 10:40:21 -0500316 goto check_pending;
317 }
Chris Masone2fa7222007-03-12 16:22:34 -0400318 btrfs_disk_key_to_cpu(&key, &l->items[slot].key);
319 if (key.objectid >= search_start) {
Chris Masonfec577f2007-02-26 10:40:21 -0500320 if (start_found) {
Chris Mason0579da42007-03-07 16:15:30 -0500321 if (last_block < search_start)
322 last_block = search_start;
Chris Masone2fa7222007-03-12 16:22:34 -0400323 hole_size = key.objectid - last_block;
Chris Mason037e6392007-03-07 11:50:24 -0500324 if (hole_size > total_needed) {
Chris Masonfec577f2007-02-26 10:40:21 -0500325 ins->objectid = last_block;
Chris Mason037e6392007-03-07 11:50:24 -0500326 ins->offset = hole_size;
Chris Masonfec577f2007-02-26 10:40:21 -0500327 goto check_pending;
328 }
Chris Mason0579da42007-03-07 16:15:30 -0500329 }
Chris Masonfec577f2007-02-26 10:40:21 -0500330 }
Chris Mason0579da42007-03-07 16:15:30 -0500331 start_found = 1;
Chris Masone2fa7222007-03-12 16:22:34 -0400332 last_block = key.objectid + key.offset;
Chris Masonfec577f2007-02-26 10:40:21 -0500333 path.slots[0]++;
334 }
335 // FIXME -ENOSPC
336check_pending:
337 /* we have to make sure we didn't find an extent that has already
338 * been allocated by the map tree or the original allocation
339 */
Chris Mason234b63a2007-03-13 10:46:10 -0400340 btrfs_release_path(root, &path);
Chris Masonfec577f2007-02-26 10:40:21 -0500341 BUG_ON(ins->objectid < search_start);
Chris Mason037e6392007-03-07 11:50:24 -0500342 for (test_block = ins->objectid;
343 test_block < ins->objectid + total_needed; test_block++) {
344 if (radix_tree_lookup(&root->pinned_radix, test_block)) {
345 search_start = test_block + 1;
Chris Masonfec577f2007-02-26 10:40:21 -0500346 goto check_failed;
347 }
348 }
Chris Mason037e6392007-03-07 11:50:24 -0500349 BUG_ON(root->current_insert.offset);
Chris Mason0579da42007-03-07 16:15:30 -0500350 root->current_insert.offset = total_needed - num_blocks;
Chris Mason037e6392007-03-07 11:50:24 -0500351 root->current_insert.objectid = ins->objectid + num_blocks;
352 root->current_insert.flags = 0;
Chris Mason0579da42007-03-07 16:15:30 -0500353 root->last_insert.objectid = ins->objectid;
Chris Mason037e6392007-03-07 11:50:24 -0500354 ins->offset = num_blocks;
Chris Masonfec577f2007-02-26 10:40:21 -0500355 return 0;
Chris Mason0f70abe2007-02-28 16:46:22 -0500356error:
Chris Mason234b63a2007-03-13 10:46:10 -0400357 btrfs_release_path(root, &path);
Chris Mason0f70abe2007-02-28 16:46:22 -0500358 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -0500359}
360
361/*
Chris Masonfec577f2007-02-26 10:40:21 -0500362 * finds a free extent and does all the dirty work required for allocation
363 * returns the key for the extent through ins, and a tree buffer for
364 * the first block of the extent through buf.
365 *
366 * returns 0 if everything worked, non-zero otherwise.
367 */
Chris Mason9aca1d52007-03-13 11:09:37 -0400368static int alloc_extent(struct btrfs_root *root, u64 num_blocks,
369 u64 search_start, u64 search_end, u64 owner,
370 struct btrfs_key *ins)
Chris Masonfec577f2007-02-26 10:40:21 -0500371{
372 int ret;
373 int pending_ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400374 struct btrfs_root *extent_root = root->extent_root;
375 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -0500376
Chris Masoncf27e1e2007-03-13 09:49:06 -0400377 btrfs_set_extent_refs(&extent_item, 1);
378 btrfs_set_extent_owner(&extent_item, owner);
Chris Masonfec577f2007-02-26 10:40:21 -0500379
Chris Mason037e6392007-03-07 11:50:24 -0500380 if (root == extent_root) {
381 BUG_ON(extent_root->current_insert.offset == 0);
382 BUG_ON(num_blocks != 1);
383 BUG_ON(extent_root->current_insert.flags ==
384 extent_root->current_insert.offset);
385 ins->offset = 1;
386 ins->objectid = extent_root->current_insert.objectid +
387 extent_root->current_insert.flags++;
Chris Masonfec577f2007-02-26 10:40:21 -0500388 return 0;
389 }
Chris Mason037e6392007-03-07 11:50:24 -0500390 ret = find_free_extent(root, num_blocks, search_start,
391 search_end, ins);
392 if (ret)
393 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -0500394
Chris Mason234b63a2007-03-13 10:46:10 -0400395 ret = btrfs_insert_item(extent_root, ins, &extent_item,
Chris Mason037e6392007-03-07 11:50:24 -0500396 sizeof(extent_item));
397
398 finish_current_insert(extent_root);
399 pending_ret = run_pending(extent_root);
400 if (ret)
401 return ret;
402 if (pending_ret)
403 return pending_ret;
404 return 0;
Chris Masonfec577f2007-02-26 10:40:21 -0500405}
406
407/*
408 * helper function to allocate a block for a given tree
409 * returns the tree buffer or NULL.
410 */
Chris Mason234b63a2007-03-13 10:46:10 -0400411struct btrfs_buffer *btrfs_alloc_free_block(struct btrfs_root *root)
Chris Masonfec577f2007-02-26 10:40:21 -0500412{
Chris Masone2fa7222007-03-12 16:22:34 -0400413 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -0500414 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400415 struct btrfs_buffer *buf;
Chris Masonfec577f2007-02-26 10:40:21 -0500416
417 ret = alloc_extent(root, 1, 0, (unsigned long)-1,
Chris Mason7518a232007-03-12 12:01:18 -0400418 btrfs_header_parentid(&root->node->node.header),
Chris Mason037e6392007-03-07 11:50:24 -0500419 &ins);
Chris Masonfec577f2007-02-26 10:40:21 -0500420 if (ret) {
421 BUG();
422 return NULL;
423 }
Chris Mason037e6392007-03-07 11:50:24 -0500424 buf = find_tree_block(root, ins.objectid);
425 dirty_tree_block(root, buf);
Chris Masonfec577f2007-02-26 10:40:21 -0500426 return buf;
427}
Chris Masona28ec192007-03-06 20:08:01 -0500428
Chris Mason9aca1d52007-03-13 11:09:37 -0400429/*
430 * helper function for drop_snapshot, this walks down the tree dropping ref
431 * counts as it goes.
432 */
433static int walk_down_tree(struct btrfs_root *root,
434 struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -0500435{
Chris Mason234b63a2007-03-13 10:46:10 -0400436 struct btrfs_buffer *next;
437 struct btrfs_buffer *cur;
Chris Mason20524f02007-03-10 06:35:47 -0500438 u64 blocknr;
439 int ret;
440 u32 refs;
441
442 ret = lookup_block_ref(root, path->nodes[*level]->blocknr, &refs);
443 BUG_ON(ret);
444 if (refs > 1)
445 goto out;
Chris Mason9aca1d52007-03-13 11:09:37 -0400446 /*
447 * walk down to the last node level and free all the leaves
448 */
Chris Mason20524f02007-03-10 06:35:47 -0500449 while(*level > 0) {
450 cur = path->nodes[*level];
Chris Mason7518a232007-03-12 12:01:18 -0400451 if (path->slots[*level] >=
452 btrfs_header_nritems(&cur->node.header))
Chris Mason20524f02007-03-10 06:35:47 -0500453 break;
Chris Mason1d4f8a02007-03-13 09:28:32 -0400454 blocknr = btrfs_node_blockptr(&cur->node, path->slots[*level]);
Chris Mason20524f02007-03-10 06:35:47 -0500455 ret = lookup_block_ref(root, blocknr, &refs);
456 if (refs != 1 || *level == 1) {
457 path->slots[*level]++;
Chris Mason234b63a2007-03-13 10:46:10 -0400458 ret = btrfs_free_extent(root, blocknr, 1);
Chris Mason20524f02007-03-10 06:35:47 -0500459 BUG_ON(ret);
460 continue;
461 }
462 BUG_ON(ret);
463 next = read_tree_block(root, blocknr);
Chris Mason83e15a22007-03-12 09:03:27 -0400464 if (path->nodes[*level-1])
Chris Mason234b63a2007-03-13 10:46:10 -0400465 btrfs_block_release(root, path->nodes[*level-1]);
Chris Mason20524f02007-03-10 06:35:47 -0500466 path->nodes[*level-1] = next;
Chris Mason7518a232007-03-12 12:01:18 -0400467 *level = btrfs_header_level(&next->node.header);
Chris Mason20524f02007-03-10 06:35:47 -0500468 path->slots[*level] = 0;
469 }
470out:
Chris Mason234b63a2007-03-13 10:46:10 -0400471 ret = btrfs_free_extent(root, path->nodes[*level]->blocknr, 1);
472 btrfs_block_release(root, path->nodes[*level]);
Chris Mason20524f02007-03-10 06:35:47 -0500473 path->nodes[*level] = NULL;
474 *level += 1;
475 BUG_ON(ret);
476 return 0;
477}
478
Chris Mason9aca1d52007-03-13 11:09:37 -0400479/*
480 * helper for dropping snapshots. This walks back up the tree in the path
481 * to find the first node higher up where we haven't yet gone through
482 * all the slots
483 */
484static int walk_up_tree(struct btrfs_root *root, struct btrfs_path *path,
485 int *level)
Chris Mason20524f02007-03-10 06:35:47 -0500486{
487 int i;
488 int slot;
489 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400490 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Mason20524f02007-03-10 06:35:47 -0500491 slot = path->slots[i];
Chris Mason7518a232007-03-12 12:01:18 -0400492 if (slot <
493 btrfs_header_nritems(&path->nodes[i]->node.header)- 1) {
Chris Mason20524f02007-03-10 06:35:47 -0500494 path->slots[i]++;
495 *level = i;
496 return 0;
497 } else {
Chris Mason234b63a2007-03-13 10:46:10 -0400498 ret = btrfs_free_extent(root,
Chris Mason20524f02007-03-10 06:35:47 -0500499 path->nodes[*level]->blocknr, 1);
Chris Mason234b63a2007-03-13 10:46:10 -0400500 btrfs_block_release(root, path->nodes[*level]);
Chris Mason83e15a22007-03-12 09:03:27 -0400501 path->nodes[*level] = NULL;
Chris Mason20524f02007-03-10 06:35:47 -0500502 *level = i + 1;
503 BUG_ON(ret);
504 }
505 }
506 return 1;
507}
508
Chris Mason9aca1d52007-03-13 11:09:37 -0400509/*
510 * drop the reference count on the tree rooted at 'snap'. This traverses
511 * the tree freeing any blocks that have a ref count of zero after being
512 * decremented.
513 */
Chris Mason234b63a2007-03-13 10:46:10 -0400514int btrfs_drop_snapshot(struct btrfs_root *root, struct btrfs_buffer *snap)
Chris Mason20524f02007-03-10 06:35:47 -0500515{
Chris Mason9aca1d52007-03-13 11:09:37 -0400516 int ret = 0;;
517 int wret;
Chris Mason20524f02007-03-10 06:35:47 -0500518 int level;
Chris Mason234b63a2007-03-13 10:46:10 -0400519 struct btrfs_path path;
Chris Mason20524f02007-03-10 06:35:47 -0500520 int i;
521 int orig_level;
522
Chris Mason234b63a2007-03-13 10:46:10 -0400523 btrfs_init_path(&path);
Chris Mason20524f02007-03-10 06:35:47 -0500524
Chris Mason7518a232007-03-12 12:01:18 -0400525 level = btrfs_header_level(&snap->node.header);
Chris Mason20524f02007-03-10 06:35:47 -0500526 orig_level = level;
527 path.nodes[level] = snap;
528 path.slots[level] = 0;
529 while(1) {
Chris Mason9aca1d52007-03-13 11:09:37 -0400530 wret = walk_down_tree(root, &path, &level);
531 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -0500532 break;
Chris Mason9aca1d52007-03-13 11:09:37 -0400533 if (wret < 0)
534 ret = wret;
535
536 wret = walk_up_tree(root, &path, &level);
537 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -0500538 break;
Chris Mason9aca1d52007-03-13 11:09:37 -0400539 if (wret < 0)
540 ret = wret;
Chris Mason20524f02007-03-10 06:35:47 -0500541 }
Chris Mason83e15a22007-03-12 09:03:27 -0400542 for (i = 0; i <= orig_level; i++) {
543 if (path.nodes[i]) {
Chris Mason234b63a2007-03-13 10:46:10 -0400544 btrfs_block_release(root, path.nodes[i]);
Chris Mason83e15a22007-03-12 09:03:27 -0400545 }
Chris Mason20524f02007-03-10 06:35:47 -0500546 }
Chris Mason9aca1d52007-03-13 11:09:37 -0400547 return ret;
Chris Mason20524f02007-03-10 06:35:47 -0500548}