blob: 4a40282b45f772dc8d32c351b9a3121dccfaedb6 [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;
Chris Mason62e27492007-03-15 12:56:47 -040038 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Mason02217ed2007-03-02 16:08:05 -050039 key.offset = 1;
Chris Mason234b63a2007-03-13 10:46:10 -040040 ret = btrfs_search_slot(root->extent_root, &key, &path, 0, 1);
Chris Masona28ec192007-03-06 20:08:01 -050041 if (ret != 0)
42 BUG();
Chris Mason02217ed2007-03-02 16:08:05 -050043 BUG_ON(ret != 0);
44 l = &path.nodes[0]->leaf;
Chris Mason4beb1b82007-03-14 10:31:29 -040045 item = btrfs_item_ptr(l, path.slots[0], struct btrfs_extent_item);
Chris Masoncf27e1e2007-03-13 09:49:06 -040046 refs = btrfs_extent_refs(item);
47 btrfs_set_extent_refs(item, refs + 1);
Chris Masona28ec192007-03-06 20:08:01 -050048
Chris Mason02217ed2007-03-02 16:08:05 -050049 BUG_ON(list_empty(&path.nodes[0]->dirty));
Chris Mason234b63a2007-03-13 10:46:10 -040050 btrfs_release_path(root->extent_root, &path);
Chris Mason037e6392007-03-07 11:50:24 -050051 finish_current_insert(root->extent_root);
52 run_pending(root->extent_root);
Chris Mason02217ed2007-03-02 16:08:05 -050053 return 0;
54}
55
Chris Mason234b63a2007-03-13 10:46:10 -040056static int lookup_block_ref(struct btrfs_root *root, u64 blocknr, u32 *refs)
Chris Masona28ec192007-03-06 20:08:01 -050057{
Chris Mason234b63a2007-03-13 10:46:10 -040058 struct btrfs_path path;
Chris Masona28ec192007-03-06 20:08:01 -050059 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -040060 struct btrfs_key key;
Chris Mason234b63a2007-03-13 10:46:10 -040061 struct btrfs_leaf *l;
62 struct btrfs_extent_item *item;
63 btrfs_init_path(&path);
Chris Masona28ec192007-03-06 20:08:01 -050064 key.objectid = blocknr;
Chris Masona28ec192007-03-06 20:08:01 -050065 key.offset = 1;
Chris Mason62e27492007-03-15 12:56:47 -040066 key.flags = 0;
67 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
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 Mason4beb1b82007-03-14 10:31:29 -040072 item = btrfs_item_ptr(l, path.slots[0], struct btrfs_extent_item);
Chris Masoncf27e1e2007-03-13 09:49:06 -040073 *refs = btrfs_extent_refs(item);
Chris Mason234b63a2007-03-13 10:46:10 -040074 btrfs_release_path(root->extent_root, &path);
Chris Masona28ec192007-03-06 20:08:01 -050075 return 0;
76}
77
Chris Mason234b63a2007-03-13 10:46:10 -040078int btrfs_inc_ref(struct btrfs_root *root, struct btrfs_buffer *buf)
Chris Mason02217ed2007-03-02 16:08:05 -050079{
80 u64 blocknr;
81 int i;
Chris Masona28ec192007-03-06 20:08:01 -050082
Chris Mason3768f362007-03-13 16:47:54 -040083 if (!root->ref_cows)
Chris Masona28ec192007-03-06 20:08:01 -050084 return 0;
Chris Mason7518a232007-03-12 12:01:18 -040085 if (btrfs_is_leaf(&buf->node))
Chris Masona28ec192007-03-06 20:08:01 -050086 return 0;
87
Chris Mason7518a232007-03-12 12:01:18 -040088 for (i = 0; i < btrfs_header_nritems(&buf->node.header); i++) {
Chris Mason1d4f8a02007-03-13 09:28:32 -040089 blocknr = btrfs_node_blockptr(&buf->node, i);
Chris Mason02217ed2007-03-02 16:08:05 -050090 inc_block_ref(root, blocknr);
91 }
92 return 0;
93}
94
Chris Mason234b63a2007-03-13 10:46:10 -040095int btrfs_finish_extent_commit(struct btrfs_root *root)
Chris Masona28ec192007-03-06 20:08:01 -050096{
Chris Masona28ec192007-03-06 20:08:01 -050097 unsigned long gang[8];
Chris Mason88fd1462007-03-16 08:56:18 -040098 u64 first = 0;
Chris Masona28ec192007-03-06 20:08:01 -050099 int ret;
100 int i;
101
102 while(1) {
Chris Mason3768f362007-03-13 16:47:54 -0400103 ret = radix_tree_gang_lookup(&root->pinned_radix,
Chris Masona28ec192007-03-06 20:08:01 -0500104 (void **)gang, 0,
105 ARRAY_SIZE(gang));
106 if (!ret)
107 break;
Chris Mason88fd1462007-03-16 08:56:18 -0400108 if (!first)
109 first = gang[0];
Chris Mason0579da42007-03-07 16:15:30 -0500110 for (i = 0; i < ret; i++) {
Chris Mason3768f362007-03-13 16:47:54 -0400111 radix_tree_delete(&root->pinned_radix, gang[i]);
Chris Mason0579da42007-03-07 16:15:30 -0500112 }
Chris Masona28ec192007-03-06 20:08:01 -0500113 }
Chris Mason88fd1462007-03-16 08:56:18 -0400114 root->last_insert.objectid = first;
Chris Mason3768f362007-03-13 16:47:54 -0400115 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;
Chris Mason62e27492007-03-15 12:56:47 -0400131 btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
Chris Mason037e6392007-03-07 11:50:24 -0500132
133 for (i = 0; i < extent_root->current_insert.flags; i++) {
134 ins.objectid = extent_root->current_insert.objectid + i;
Chris Mason234b63a2007-03-13 10:46:10 -0400135 ret = btrfs_insert_item(extent_root, &ins, &extent_item,
Chris Mason037e6392007-03-07 11:50:24 -0500136 sizeof(extent_item));
137 BUG_ON(ret);
138 }
139 extent_root->current_insert.offset = 0;
140 return 0;
141}
142
Chris Masona28ec192007-03-06 20:08:01 -0500143/*
144 * remove an extent from the root, returns 0 on success
145 */
Chris Mason88fd1462007-03-16 08:56:18 -0400146static int __free_extent(struct btrfs_root *root, u64 blocknr, u64 num_blocks,
147 int pin)
Chris Masona28ec192007-03-06 20:08:01 -0500148{
Chris Mason234b63a2007-03-13 10:46:10 -0400149 struct btrfs_path path;
Chris Masone2fa7222007-03-12 16:22:34 -0400150 struct btrfs_key key;
Chris Mason234b63a2007-03-13 10:46:10 -0400151 struct btrfs_root *extent_root = root->extent_root;
Chris Masona28ec192007-03-06 20:08:01 -0500152 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400153 struct btrfs_extent_item *ei;
Chris Masone2fa7222007-03-12 16:22:34 -0400154 struct btrfs_key ins;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400155 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500156
Chris Mason88fd1462007-03-16 08:56:18 -0400157 BUG_ON(pin && num_blocks != 1);
Chris Masona28ec192007-03-06 20:08:01 -0500158 key.objectid = blocknr;
159 key.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -0400160 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masona28ec192007-03-06 20:08:01 -0500161 key.offset = num_blocks;
162
Chris Mason037e6392007-03-07 11:50:24 -0500163 find_free_extent(root, 0, 0, (u64)-1, &ins);
Chris Mason234b63a2007-03-13 10:46:10 -0400164 btrfs_init_path(&path);
165 ret = btrfs_search_slot(extent_root, &key, &path, -1, 1);
Chris Masona28ec192007-03-06 20:08:01 -0500166 if (ret) {
167 printf("failed to find %Lu\n", key.objectid);
Chris Mason234b63a2007-03-13 10:46:10 -0400168 btrfs_print_tree(extent_root, extent_root->node);
Chris Masona28ec192007-03-06 20:08:01 -0500169 printf("failed to find %Lu\n", key.objectid);
170 BUG();
171 }
Chris Mason123abc82007-03-14 14:14:43 -0400172 ei = btrfs_item_ptr(&path.nodes[0]->leaf, path.slots[0],
173 struct btrfs_extent_item);
Chris Masona28ec192007-03-06 20:08:01 -0500174 BUG_ON(ei->refs == 0);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400175 refs = btrfs_extent_refs(ei) - 1;
176 btrfs_set_extent_refs(ei, refs);
177 if (refs == 0) {
Chris Mason88fd1462007-03-16 08:56:18 -0400178 if (pin) {
Chris Masona28ec192007-03-06 20:08:01 -0500179 int err;
180 radix_tree_preload(GFP_KERNEL);
181 err = radix_tree_insert(&extent_root->pinned_radix,
182 blocknr, (void *)blocknr);
183 BUG_ON(err);
184 radix_tree_preload_end();
185 }
Chris Mason234b63a2007-03-13 10:46:10 -0400186 ret = btrfs_del_item(extent_root, &path);
Chris Mason88fd1462007-03-16 08:56:18 -0400187 if (!pin && extent_root->last_insert.objectid > blocknr)
Chris Mason0579da42007-03-07 16:15:30 -0500188 extent_root->last_insert.objectid = blocknr;
Chris Masona28ec192007-03-06 20:08:01 -0500189 if (ret)
190 BUG();
191 }
Chris Mason234b63a2007-03-13 10:46:10 -0400192 btrfs_release_path(extent_root, &path);
Chris Mason037e6392007-03-07 11:50:24 -0500193 finish_current_insert(extent_root);
Chris Masona28ec192007-03-06 20:08:01 -0500194 return ret;
195}
196
197/*
Chris Masonfec577f2007-02-26 10:40:21 -0500198 * find all the blocks marked as pending in the radix tree and remove
199 * them from the extent map
200 */
Chris Mason234b63a2007-03-13 10:46:10 -0400201static int del_pending_extents(struct btrfs_root *extent_root)
Chris Masonfec577f2007-02-26 10:40:21 -0500202{
203 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400204 struct btrfs_buffer *gang[4];
Chris Masonfec577f2007-02-26 10:40:21 -0500205 int i;
Chris Masonfec577f2007-02-26 10:40:21 -0500206
207 while(1) {
208 ret = radix_tree_gang_lookup_tag(&extent_root->cache_radix,
209 (void **)gang, 0,
210 ARRAY_SIZE(gang),
Chris Masona28ec192007-03-06 20:08:01 -0500211 CTREE_EXTENT_PENDING_DEL);
Chris Masonfec577f2007-02-26 10:40:21 -0500212 if (!ret)
213 break;
214 for (i = 0; i < ret; i++) {
Chris Mason88fd1462007-03-16 08:56:18 -0400215 ret = __free_extent(extent_root,
216 gang[i]->blocknr, 1, 1);
Chris Masonfec577f2007-02-26 10:40:21 -0500217 radix_tree_tag_clear(&extent_root->cache_radix,
218 gang[i]->blocknr,
Chris Masona28ec192007-03-06 20:08:01 -0500219 CTREE_EXTENT_PENDING_DEL);
Chris Mason234b63a2007-03-13 10:46:10 -0400220 btrfs_block_release(extent_root, gang[i]);
Chris Masonfec577f2007-02-26 10:40:21 -0500221 }
222 }
223 return 0;
224}
225
Chris Mason234b63a2007-03-13 10:46:10 -0400226static int run_pending(struct btrfs_root *extent_root)
Chris Masona28ec192007-03-06 20:08:01 -0500227{
228 while(radix_tree_tagged(&extent_root->cache_radix,
Chris Mason037e6392007-03-07 11:50:24 -0500229 CTREE_EXTENT_PENDING_DEL))
Chris Masona28ec192007-03-06 20:08:01 -0500230 del_pending_extents(extent_root);
Chris Masona28ec192007-03-06 20:08:01 -0500231 return 0;
232}
233
234
Chris Masonfec577f2007-02-26 10:40:21 -0500235/*
236 * remove an extent from the root, returns 0 on success
237 */
Chris Mason88fd1462007-03-16 08:56:18 -0400238int btrfs_free_extent(struct btrfs_root *root, u64 blocknr, u64 num_blocks,
239 int pin)
Chris Masonfec577f2007-02-26 10:40:21 -0500240{
Chris Mason234b63a2007-03-13 10:46:10 -0400241 struct btrfs_root *extent_root = root->extent_root;
242 struct btrfs_buffer *t;
Chris Masonfec577f2007-02-26 10:40:21 -0500243 int pending_ret;
244 int ret;
Chris Masona28ec192007-03-06 20:08:01 -0500245
246 if (root == extent_root) {
247 t = find_tree_block(root, blocknr);
Chris Mason037e6392007-03-07 11:50:24 -0500248 radix_tree_tag_set(&root->cache_radix, blocknr,
Chris Masona28ec192007-03-06 20:08:01 -0500249 CTREE_EXTENT_PENDING_DEL);
Chris Masona28ec192007-03-06 20:08:01 -0500250 return 0;
251 }
Chris Mason88fd1462007-03-16 08:56:18 -0400252 ret = __free_extent(root, blocknr, num_blocks, pin);
Chris Masona28ec192007-03-06 20:08:01 -0500253 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
Chris Mason62e27492007-03-15 12:56:47 -0400261 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -0500262 * 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 Mason62e27492007-03-15 12:56:47 -0400284
285 ins->flags = 0;
286 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
287
Chris Masonfec577f2007-02-26 10:40:21 -0500288check_failed:
Chris Mason234b63a2007-03-13 10:46:10 -0400289 btrfs_init_path(&path);
Chris Masonfec577f2007-02-26 10:40:21 -0500290 ins->objectid = search_start;
291 ins->offset = 0;
Chris Masonfec577f2007-02-26 10:40:21 -0500292 start_found = 0;
Chris Mason234b63a2007-03-13 10:46:10 -0400293 ret = btrfs_search_slot(root, ins, &path, 0, 0);
Chris Mason0f70abe2007-02-28 16:46:22 -0500294 if (ret < 0)
295 goto error;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500296
Chris Mason0579da42007-03-07 16:15:30 -0500297 if (path.slots[0] > 0)
298 path.slots[0]--;
299
Chris Masonfec577f2007-02-26 10:40:21 -0500300 while (1) {
301 l = &path.nodes[0]->leaf;
302 slot = path.slots[0];
Chris Mason7518a232007-03-12 12:01:18 -0400303 if (slot >= btrfs_header_nritems(&l->header)) {
Chris Mason234b63a2007-03-13 10:46:10 -0400304 ret = btrfs_next_leaf(root, &path);
Chris Masonfec577f2007-02-26 10:40:21 -0500305 if (ret == 0)
306 continue;
Chris Mason0f70abe2007-02-28 16:46:22 -0500307 if (ret < 0)
308 goto error;
Chris Masonfec577f2007-02-26 10:40:21 -0500309 if (!start_found) {
310 ins->objectid = search_start;
Chris Mason037e6392007-03-07 11:50:24 -0500311 ins->offset = (u64)-1;
Chris Masonfec577f2007-02-26 10:40:21 -0500312 start_found = 1;
313 goto check_pending;
314 }
315 ins->objectid = last_block > search_start ?
316 last_block : search_start;
Chris Mason037e6392007-03-07 11:50:24 -0500317 ins->offset = (u64)-1;
Chris Masonfec577f2007-02-26 10:40:21 -0500318 goto check_pending;
319 }
Chris Masone2fa7222007-03-12 16:22:34 -0400320 btrfs_disk_key_to_cpu(&key, &l->items[slot].key);
321 if (key.objectid >= search_start) {
Chris Masonfec577f2007-02-26 10:40:21 -0500322 if (start_found) {
Chris Mason0579da42007-03-07 16:15:30 -0500323 if (last_block < search_start)
324 last_block = search_start;
Chris Masone2fa7222007-03-12 16:22:34 -0400325 hole_size = key.objectid - last_block;
Chris Mason037e6392007-03-07 11:50:24 -0500326 if (hole_size > total_needed) {
Chris Masonfec577f2007-02-26 10:40:21 -0500327 ins->objectid = last_block;
Chris Mason037e6392007-03-07 11:50:24 -0500328 ins->offset = hole_size;
Chris Masonfec577f2007-02-26 10:40:21 -0500329 goto check_pending;
330 }
Chris Mason0579da42007-03-07 16:15:30 -0500331 }
Chris Masonfec577f2007-02-26 10:40:21 -0500332 }
Chris Mason0579da42007-03-07 16:15:30 -0500333 start_found = 1;
Chris Masone2fa7222007-03-12 16:22:34 -0400334 last_block = key.objectid + key.offset;
Chris Masonfec577f2007-02-26 10:40:21 -0500335 path.slots[0]++;
336 }
337 // FIXME -ENOSPC
338check_pending:
339 /* we have to make sure we didn't find an extent that has already
340 * been allocated by the map tree or the original allocation
341 */
Chris Mason234b63a2007-03-13 10:46:10 -0400342 btrfs_release_path(root, &path);
Chris Masonfec577f2007-02-26 10:40:21 -0500343 BUG_ON(ins->objectid < search_start);
Chris Mason037e6392007-03-07 11:50:24 -0500344 for (test_block = ins->objectid;
345 test_block < ins->objectid + total_needed; test_block++) {
346 if (radix_tree_lookup(&root->pinned_radix, test_block)) {
347 search_start = test_block + 1;
Chris Masonfec577f2007-02-26 10:40:21 -0500348 goto check_failed;
349 }
350 }
Chris Mason037e6392007-03-07 11:50:24 -0500351 BUG_ON(root->current_insert.offset);
Chris Mason0579da42007-03-07 16:15:30 -0500352 root->current_insert.offset = total_needed - num_blocks;
Chris Mason037e6392007-03-07 11:50:24 -0500353 root->current_insert.objectid = ins->objectid + num_blocks;
354 root->current_insert.flags = 0;
Chris Mason0579da42007-03-07 16:15:30 -0500355 root->last_insert.objectid = ins->objectid;
Chris Mason037e6392007-03-07 11:50:24 -0500356 ins->offset = num_blocks;
Chris Masonfec577f2007-02-26 10:40:21 -0500357 return 0;
Chris Mason0f70abe2007-02-28 16:46:22 -0500358error:
Chris Mason234b63a2007-03-13 10:46:10 -0400359 btrfs_release_path(root, &path);
Chris Mason0f70abe2007-02-28 16:46:22 -0500360 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -0500361}
362
363/*
Chris Masonfec577f2007-02-26 10:40:21 -0500364 * finds a free extent and does all the dirty work required for allocation
365 * returns the key for the extent through ins, and a tree buffer for
366 * the first block of the extent through buf.
367 *
368 * returns 0 if everything worked, non-zero otherwise.
369 */
Chris Mason9aca1d52007-03-13 11:09:37 -0400370static int alloc_extent(struct btrfs_root *root, u64 num_blocks,
371 u64 search_start, u64 search_end, u64 owner,
372 struct btrfs_key *ins)
Chris Masonfec577f2007-02-26 10:40:21 -0500373{
374 int ret;
375 int pending_ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400376 struct btrfs_root *extent_root = root->extent_root;
377 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -0500378
Chris Masoncf27e1e2007-03-13 09:49:06 -0400379 btrfs_set_extent_refs(&extent_item, 1);
380 btrfs_set_extent_owner(&extent_item, owner);
Chris Masonfec577f2007-02-26 10:40:21 -0500381
Chris Mason037e6392007-03-07 11:50:24 -0500382 if (root == extent_root) {
383 BUG_ON(extent_root->current_insert.offset == 0);
384 BUG_ON(num_blocks != 1);
385 BUG_ON(extent_root->current_insert.flags ==
386 extent_root->current_insert.offset);
387 ins->offset = 1;
388 ins->objectid = extent_root->current_insert.objectid +
389 extent_root->current_insert.flags++;
Chris Masonfec577f2007-02-26 10:40:21 -0500390 return 0;
391 }
Chris Mason037e6392007-03-07 11:50:24 -0500392 ret = find_free_extent(root, num_blocks, search_start,
393 search_end, ins);
394 if (ret)
395 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -0500396
Chris Mason234b63a2007-03-13 10:46:10 -0400397 ret = btrfs_insert_item(extent_root, ins, &extent_item,
Chris Mason037e6392007-03-07 11:50:24 -0500398 sizeof(extent_item));
399
400 finish_current_insert(extent_root);
401 pending_ret = run_pending(extent_root);
402 if (ret)
403 return ret;
404 if (pending_ret)
405 return pending_ret;
406 return 0;
Chris Masonfec577f2007-02-26 10:40:21 -0500407}
408
409/*
410 * helper function to allocate a block for a given tree
411 * returns the tree buffer or NULL.
412 */
Chris Mason234b63a2007-03-13 10:46:10 -0400413struct btrfs_buffer *btrfs_alloc_free_block(struct btrfs_root *root)
Chris Masonfec577f2007-02-26 10:40:21 -0500414{
Chris Masone2fa7222007-03-12 16:22:34 -0400415 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -0500416 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400417 struct btrfs_buffer *buf;
Chris Masonfec577f2007-02-26 10:40:21 -0500418
419 ret = alloc_extent(root, 1, 0, (unsigned long)-1,
Chris Mason7518a232007-03-12 12:01:18 -0400420 btrfs_header_parentid(&root->node->node.header),
Chris Mason037e6392007-03-07 11:50:24 -0500421 &ins);
Chris Masonfec577f2007-02-26 10:40:21 -0500422 if (ret) {
423 BUG();
424 return NULL;
425 }
Chris Mason037e6392007-03-07 11:50:24 -0500426 buf = find_tree_block(root, ins.objectid);
427 dirty_tree_block(root, buf);
Chris Masonfec577f2007-02-26 10:40:21 -0500428 return buf;
429}
Chris Masona28ec192007-03-06 20:08:01 -0500430
Chris Mason9aca1d52007-03-13 11:09:37 -0400431/*
432 * helper function for drop_snapshot, this walks down the tree dropping ref
433 * counts as it goes.
434 */
435static int walk_down_tree(struct btrfs_root *root,
436 struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -0500437{
Chris Mason234b63a2007-03-13 10:46:10 -0400438 struct btrfs_buffer *next;
439 struct btrfs_buffer *cur;
Chris Mason20524f02007-03-10 06:35:47 -0500440 u64 blocknr;
441 int ret;
442 u32 refs;
443
444 ret = lookup_block_ref(root, path->nodes[*level]->blocknr, &refs);
445 BUG_ON(ret);
446 if (refs > 1)
447 goto out;
Chris Mason9aca1d52007-03-13 11:09:37 -0400448 /*
449 * walk down to the last node level and free all the leaves
450 */
Chris Mason20524f02007-03-10 06:35:47 -0500451 while(*level > 0) {
452 cur = path->nodes[*level];
Chris Mason7518a232007-03-12 12:01:18 -0400453 if (path->slots[*level] >=
454 btrfs_header_nritems(&cur->node.header))
Chris Mason20524f02007-03-10 06:35:47 -0500455 break;
Chris Mason1d4f8a02007-03-13 09:28:32 -0400456 blocknr = btrfs_node_blockptr(&cur->node, path->slots[*level]);
Chris Mason20524f02007-03-10 06:35:47 -0500457 ret = lookup_block_ref(root, blocknr, &refs);
458 if (refs != 1 || *level == 1) {
459 path->slots[*level]++;
Chris Mason88fd1462007-03-16 08:56:18 -0400460 ret = btrfs_free_extent(root, blocknr, 1, 1);
Chris Mason20524f02007-03-10 06:35:47 -0500461 BUG_ON(ret);
462 continue;
463 }
464 BUG_ON(ret);
465 next = read_tree_block(root, blocknr);
Chris Mason83e15a22007-03-12 09:03:27 -0400466 if (path->nodes[*level-1])
Chris Mason234b63a2007-03-13 10:46:10 -0400467 btrfs_block_release(root, path->nodes[*level-1]);
Chris Mason20524f02007-03-10 06:35:47 -0500468 path->nodes[*level-1] = next;
Chris Mason7518a232007-03-12 12:01:18 -0400469 *level = btrfs_header_level(&next->node.header);
Chris Mason20524f02007-03-10 06:35:47 -0500470 path->slots[*level] = 0;
471 }
472out:
Chris Mason88fd1462007-03-16 08:56:18 -0400473 ret = btrfs_free_extent(root, path->nodes[*level]->blocknr, 1, 1);
Chris Mason234b63a2007-03-13 10:46:10 -0400474 btrfs_block_release(root, path->nodes[*level]);
Chris Mason20524f02007-03-10 06:35:47 -0500475 path->nodes[*level] = NULL;
476 *level += 1;
477 BUG_ON(ret);
478 return 0;
479}
480
Chris Mason9aca1d52007-03-13 11:09:37 -0400481/*
482 * helper for dropping snapshots. This walks back up the tree in the path
483 * to find the first node higher up where we haven't yet gone through
484 * all the slots
485 */
486static int walk_up_tree(struct btrfs_root *root, struct btrfs_path *path,
487 int *level)
Chris Mason20524f02007-03-10 06:35:47 -0500488{
489 int i;
490 int slot;
491 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400492 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Mason20524f02007-03-10 06:35:47 -0500493 slot = path->slots[i];
Chris Mason7518a232007-03-12 12:01:18 -0400494 if (slot <
495 btrfs_header_nritems(&path->nodes[i]->node.header)- 1) {
Chris Mason20524f02007-03-10 06:35:47 -0500496 path->slots[i]++;
497 *level = i;
498 return 0;
499 } else {
Chris Mason234b63a2007-03-13 10:46:10 -0400500 ret = btrfs_free_extent(root,
Chris Mason88fd1462007-03-16 08:56:18 -0400501 path->nodes[*level]->blocknr, 1, 1);
Chris Mason234b63a2007-03-13 10:46:10 -0400502 btrfs_block_release(root, path->nodes[*level]);
Chris Mason83e15a22007-03-12 09:03:27 -0400503 path->nodes[*level] = NULL;
Chris Mason20524f02007-03-10 06:35:47 -0500504 *level = i + 1;
505 BUG_ON(ret);
506 }
507 }
508 return 1;
509}
510
Chris Mason9aca1d52007-03-13 11:09:37 -0400511/*
512 * drop the reference count on the tree rooted at 'snap'. This traverses
513 * the tree freeing any blocks that have a ref count of zero after being
514 * decremented.
515 */
Chris Mason234b63a2007-03-13 10:46:10 -0400516int btrfs_drop_snapshot(struct btrfs_root *root, struct btrfs_buffer *snap)
Chris Mason20524f02007-03-10 06:35:47 -0500517{
Chris Mason3768f362007-03-13 16:47:54 -0400518 int ret = 0;
Chris Mason9aca1d52007-03-13 11:09:37 -0400519 int wret;
Chris Mason20524f02007-03-10 06:35:47 -0500520 int level;
Chris Mason234b63a2007-03-13 10:46:10 -0400521 struct btrfs_path path;
Chris Mason20524f02007-03-10 06:35:47 -0500522 int i;
523 int orig_level;
524
Chris Mason234b63a2007-03-13 10:46:10 -0400525 btrfs_init_path(&path);
Chris Mason20524f02007-03-10 06:35:47 -0500526
Chris Mason7518a232007-03-12 12:01:18 -0400527 level = btrfs_header_level(&snap->node.header);
Chris Mason20524f02007-03-10 06:35:47 -0500528 orig_level = level;
529 path.nodes[level] = snap;
530 path.slots[level] = 0;
531 while(1) {
Chris Mason9aca1d52007-03-13 11:09:37 -0400532 wret = walk_down_tree(root, &path, &level);
533 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -0500534 break;
Chris Mason9aca1d52007-03-13 11:09:37 -0400535 if (wret < 0)
536 ret = wret;
537
538 wret = walk_up_tree(root, &path, &level);
539 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -0500540 break;
Chris Mason9aca1d52007-03-13 11:09:37 -0400541 if (wret < 0)
542 ret = wret;
Chris Mason20524f02007-03-10 06:35:47 -0500543 }
Chris Mason83e15a22007-03-12 09:03:27 -0400544 for (i = 0; i <= orig_level; i++) {
545 if (path.nodes[i]) {
Chris Mason234b63a2007-03-13 10:46:10 -0400546 btrfs_block_release(root, path.nodes[i]);
Chris Mason83e15a22007-03-12 09:03:27 -0400547 }
Chris Mason20524f02007-03-10 06:35:47 -0500548 }
Chris Mason9aca1d52007-03-13 11:09:37 -0400549 return ret;
Chris Mason20524f02007-03-10 06:35:47 -0500550}