blob: c81e14162ef1a4ec8abb79693da5665caef32134 [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];
98 int ret;
99 int i;
100
101 while(1) {
Chris Mason3768f362007-03-13 16:47:54 -0400102 ret = radix_tree_gang_lookup(&root->pinned_radix,
Chris Masona28ec192007-03-06 20:08:01 -0500103 (void **)gang, 0,
104 ARRAY_SIZE(gang));
105 if (!ret)
106 break;
Chris Mason0579da42007-03-07 16:15:30 -0500107 for (i = 0; i < ret; i++) {
Chris Mason3768f362007-03-13 16:47:54 -0400108 radix_tree_delete(&root->pinned_radix, gang[i]);
Chris Mason0579da42007-03-07 16:15:30 -0500109 }
Chris Masona28ec192007-03-06 20:08:01 -0500110 }
Chris Mason3768f362007-03-13 16:47:54 -0400111 root->last_insert.objectid = 0;
112 root->last_insert.offset = 0;
Chris Masona28ec192007-03-06 20:08:01 -0500113 return 0;
114}
115
Chris Mason234b63a2007-03-13 10:46:10 -0400116static int finish_current_insert(struct btrfs_root *extent_root)
Chris Mason037e6392007-03-07 11:50:24 -0500117{
Chris Masone2fa7222007-03-12 16:22:34 -0400118 struct btrfs_key ins;
Chris Mason234b63a2007-03-13 10:46:10 -0400119 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -0500120 int i;
121 int ret;
122
Chris Masoncf27e1e2007-03-13 09:49:06 -0400123 btrfs_set_extent_refs(&extent_item, 1);
124 btrfs_set_extent_owner(&extent_item,
125 btrfs_header_parentid(&extent_root->node->node.header));
Chris Mason037e6392007-03-07 11:50:24 -0500126 ins.offset = 1;
127 ins.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -0400128 btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
Chris Mason037e6392007-03-07 11:50:24 -0500129
130 for (i = 0; i < extent_root->current_insert.flags; i++) {
131 ins.objectid = extent_root->current_insert.objectid + i;
Chris Mason234b63a2007-03-13 10:46:10 -0400132 ret = btrfs_insert_item(extent_root, &ins, &extent_item,
Chris Mason037e6392007-03-07 11:50:24 -0500133 sizeof(extent_item));
134 BUG_ON(ret);
135 }
136 extent_root->current_insert.offset = 0;
137 return 0;
138}
139
Chris Masona28ec192007-03-06 20:08:01 -0500140/*
141 * remove an extent from the root, returns 0 on success
142 */
Chris Mason234b63a2007-03-13 10:46:10 -0400143static int __free_extent(struct btrfs_root *root, u64 blocknr, u64 num_blocks)
Chris Masona28ec192007-03-06 20:08:01 -0500144{
Chris Mason234b63a2007-03-13 10:46:10 -0400145 struct btrfs_path path;
Chris Masone2fa7222007-03-12 16:22:34 -0400146 struct btrfs_key key;
Chris Mason234b63a2007-03-13 10:46:10 -0400147 struct btrfs_root *extent_root = root->extent_root;
Chris Masona28ec192007-03-06 20:08:01 -0500148 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400149 struct btrfs_extent_item *ei;
Chris Masone2fa7222007-03-12 16:22:34 -0400150 struct btrfs_key ins;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400151 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500152
Chris Masona28ec192007-03-06 20:08:01 -0500153 key.objectid = blocknr;
154 key.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -0400155 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masona28ec192007-03-06 20:08:01 -0500156 key.offset = num_blocks;
157
Chris Mason037e6392007-03-07 11:50:24 -0500158 find_free_extent(root, 0, 0, (u64)-1, &ins);
Chris Mason234b63a2007-03-13 10:46:10 -0400159 btrfs_init_path(&path);
160 ret = btrfs_search_slot(extent_root, &key, &path, -1, 1);
Chris Masona28ec192007-03-06 20:08:01 -0500161 if (ret) {
162 printf("failed to find %Lu\n", key.objectid);
Chris Mason234b63a2007-03-13 10:46:10 -0400163 btrfs_print_tree(extent_root, extent_root->node);
Chris Masona28ec192007-03-06 20:08:01 -0500164 printf("failed to find %Lu\n", key.objectid);
165 BUG();
166 }
Chris Mason123abc82007-03-14 14:14:43 -0400167 ei = btrfs_item_ptr(&path.nodes[0]->leaf, path.slots[0],
168 struct btrfs_extent_item);
Chris Masona28ec192007-03-06 20:08:01 -0500169 BUG_ON(ei->refs == 0);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400170 refs = btrfs_extent_refs(ei) - 1;
171 btrfs_set_extent_refs(ei, refs);
172 if (refs == 0) {
Chris Mason3768f362007-03-13 16:47:54 -0400173 if (!root->ref_cows) {
Chris Masona28ec192007-03-06 20:08:01 -0500174 int err;
175 radix_tree_preload(GFP_KERNEL);
176 err = radix_tree_insert(&extent_root->pinned_radix,
177 blocknr, (void *)blocknr);
178 BUG_ON(err);
179 radix_tree_preload_end();
180 }
Chris Mason234b63a2007-03-13 10:46:10 -0400181 ret = btrfs_del_item(extent_root, &path);
Chris Mason0579da42007-03-07 16:15:30 -0500182 if (root != extent_root &&
Chris Mason71087492007-03-14 09:20:39 -0400183 extent_root->last_insert.objectid > blocknr)
Chris Mason0579da42007-03-07 16:15:30 -0500184 extent_root->last_insert.objectid = blocknr;
Chris Masona28ec192007-03-06 20:08:01 -0500185 if (ret)
186 BUG();
187 }
Chris Mason234b63a2007-03-13 10:46:10 -0400188 btrfs_release_path(extent_root, &path);
Chris Mason037e6392007-03-07 11:50:24 -0500189 finish_current_insert(extent_root);
Chris Masona28ec192007-03-06 20:08:01 -0500190 return ret;
191}
192
193/*
Chris Masonfec577f2007-02-26 10:40:21 -0500194 * find all the blocks marked as pending in the radix tree and remove
195 * them from the extent map
196 */
Chris Mason234b63a2007-03-13 10:46:10 -0400197static int del_pending_extents(struct btrfs_root *extent_root)
Chris Masonfec577f2007-02-26 10:40:21 -0500198{
199 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400200 struct btrfs_buffer *gang[4];
Chris Masonfec577f2007-02-26 10:40:21 -0500201 int i;
Chris Masonfec577f2007-02-26 10:40:21 -0500202
203 while(1) {
204 ret = radix_tree_gang_lookup_tag(&extent_root->cache_radix,
205 (void **)gang, 0,
206 ARRAY_SIZE(gang),
Chris Masona28ec192007-03-06 20:08:01 -0500207 CTREE_EXTENT_PENDING_DEL);
Chris Masonfec577f2007-02-26 10:40:21 -0500208 if (!ret)
209 break;
210 for (i = 0; i < ret; i++) {
Chris Masona28ec192007-03-06 20:08:01 -0500211 ret = __free_extent(extent_root, gang[i]->blocknr, 1);
Chris Masonfec577f2007-02-26 10:40:21 -0500212 radix_tree_tag_clear(&extent_root->cache_radix,
213 gang[i]->blocknr,
Chris Masona28ec192007-03-06 20:08:01 -0500214 CTREE_EXTENT_PENDING_DEL);
Chris Mason234b63a2007-03-13 10:46:10 -0400215 btrfs_block_release(extent_root, gang[i]);
Chris Masonfec577f2007-02-26 10:40:21 -0500216 }
217 }
218 return 0;
219}
220
Chris Mason234b63a2007-03-13 10:46:10 -0400221static int run_pending(struct btrfs_root *extent_root)
Chris Masona28ec192007-03-06 20:08:01 -0500222{
223 while(radix_tree_tagged(&extent_root->cache_radix,
Chris Mason037e6392007-03-07 11:50:24 -0500224 CTREE_EXTENT_PENDING_DEL))
Chris Masona28ec192007-03-06 20:08:01 -0500225 del_pending_extents(extent_root);
Chris Masona28ec192007-03-06 20:08:01 -0500226 return 0;
227}
228
229
Chris Masonfec577f2007-02-26 10:40:21 -0500230/*
231 * remove an extent from the root, returns 0 on success
232 */
Chris Mason234b63a2007-03-13 10:46:10 -0400233int btrfs_free_extent(struct btrfs_root *root, u64 blocknr, u64 num_blocks)
Chris Masonfec577f2007-02-26 10:40:21 -0500234{
Chris Mason234b63a2007-03-13 10:46:10 -0400235 struct btrfs_root *extent_root = root->extent_root;
236 struct btrfs_buffer *t;
Chris Masonfec577f2007-02-26 10:40:21 -0500237 int pending_ret;
238 int ret;
Chris Masona28ec192007-03-06 20:08:01 -0500239
240 if (root == extent_root) {
241 t = find_tree_block(root, blocknr);
Chris Mason037e6392007-03-07 11:50:24 -0500242 radix_tree_tag_set(&root->cache_radix, blocknr,
Chris Masona28ec192007-03-06 20:08:01 -0500243 CTREE_EXTENT_PENDING_DEL);
Chris Masona28ec192007-03-06 20:08:01 -0500244 return 0;
245 }
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
Chris Mason62e27492007-03-15 12:56:47 -0400255 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -0500256 * 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 Mason62e27492007-03-15 12:56:47 -0400278
279 ins->flags = 0;
280 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
281
Chris Masonfec577f2007-02-26 10:40:21 -0500282check_failed:
Chris Mason234b63a2007-03-13 10:46:10 -0400283 btrfs_init_path(&path);
Chris Masonfec577f2007-02-26 10:40:21 -0500284 ins->objectid = search_start;
285 ins->offset = 0;
Chris Masonfec577f2007-02-26 10:40:21 -0500286 start_found = 0;
Chris Mason234b63a2007-03-13 10:46:10 -0400287 ret = btrfs_search_slot(root, ins, &path, 0, 0);
Chris Mason0f70abe2007-02-28 16:46:22 -0500288 if (ret < 0)
289 goto error;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500290
Chris Mason0579da42007-03-07 16:15:30 -0500291 if (path.slots[0] > 0)
292 path.slots[0]--;
293
Chris Masonfec577f2007-02-26 10:40:21 -0500294 while (1) {
295 l = &path.nodes[0]->leaf;
296 slot = path.slots[0];
Chris Mason7518a232007-03-12 12:01:18 -0400297 if (slot >= btrfs_header_nritems(&l->header)) {
Chris Mason234b63a2007-03-13 10:46:10 -0400298 ret = btrfs_next_leaf(root, &path);
Chris Masonfec577f2007-02-26 10:40:21 -0500299 if (ret == 0)
300 continue;
Chris Mason0f70abe2007-02-28 16:46:22 -0500301 if (ret < 0)
302 goto error;
Chris Masonfec577f2007-02-26 10:40:21 -0500303 if (!start_found) {
304 ins->objectid = search_start;
Chris Mason037e6392007-03-07 11:50:24 -0500305 ins->offset = (u64)-1;
Chris Masonfec577f2007-02-26 10:40:21 -0500306 start_found = 1;
307 goto check_pending;
308 }
309 ins->objectid = last_block > search_start ?
310 last_block : search_start;
Chris Mason037e6392007-03-07 11:50:24 -0500311 ins->offset = (u64)-1;
Chris Masonfec577f2007-02-26 10:40:21 -0500312 goto check_pending;
313 }
Chris Masone2fa7222007-03-12 16:22:34 -0400314 btrfs_disk_key_to_cpu(&key, &l->items[slot].key);
315 if (key.objectid >= search_start) {
Chris Masonfec577f2007-02-26 10:40:21 -0500316 if (start_found) {
Chris Mason0579da42007-03-07 16:15:30 -0500317 if (last_block < search_start)
318 last_block = search_start;
Chris Masone2fa7222007-03-12 16:22:34 -0400319 hole_size = key.objectid - last_block;
Chris Mason037e6392007-03-07 11:50:24 -0500320 if (hole_size > total_needed) {
Chris Masonfec577f2007-02-26 10:40:21 -0500321 ins->objectid = last_block;
Chris Mason037e6392007-03-07 11:50:24 -0500322 ins->offset = hole_size;
Chris Masonfec577f2007-02-26 10:40:21 -0500323 goto check_pending;
324 }
Chris Mason0579da42007-03-07 16:15:30 -0500325 }
Chris Masonfec577f2007-02-26 10:40:21 -0500326 }
Chris Mason0579da42007-03-07 16:15:30 -0500327 start_found = 1;
Chris Masone2fa7222007-03-12 16:22:34 -0400328 last_block = key.objectid + key.offset;
Chris Masonfec577f2007-02-26 10:40:21 -0500329 path.slots[0]++;
330 }
331 // FIXME -ENOSPC
332check_pending:
333 /* we have to make sure we didn't find an extent that has already
334 * been allocated by the map tree or the original allocation
335 */
Chris Mason234b63a2007-03-13 10:46:10 -0400336 btrfs_release_path(root, &path);
Chris Masonfec577f2007-02-26 10:40:21 -0500337 BUG_ON(ins->objectid < search_start);
Chris Mason037e6392007-03-07 11:50:24 -0500338 for (test_block = ins->objectid;
339 test_block < ins->objectid + total_needed; test_block++) {
340 if (radix_tree_lookup(&root->pinned_radix, test_block)) {
341 search_start = test_block + 1;
Chris Masonfec577f2007-02-26 10:40:21 -0500342 goto check_failed;
343 }
344 }
Chris Mason037e6392007-03-07 11:50:24 -0500345 BUG_ON(root->current_insert.offset);
Chris Mason0579da42007-03-07 16:15:30 -0500346 root->current_insert.offset = total_needed - num_blocks;
Chris Mason037e6392007-03-07 11:50:24 -0500347 root->current_insert.objectid = ins->objectid + num_blocks;
348 root->current_insert.flags = 0;
Chris Mason0579da42007-03-07 16:15:30 -0500349 root->last_insert.objectid = ins->objectid;
Chris Mason037e6392007-03-07 11:50:24 -0500350 ins->offset = num_blocks;
Chris Masonfec577f2007-02-26 10:40:21 -0500351 return 0;
Chris Mason0f70abe2007-02-28 16:46:22 -0500352error:
Chris Mason234b63a2007-03-13 10:46:10 -0400353 btrfs_release_path(root, &path);
Chris Mason0f70abe2007-02-28 16:46:22 -0500354 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -0500355}
356
357/*
Chris Masonfec577f2007-02-26 10:40:21 -0500358 * finds a free extent and does all the dirty work required for allocation
359 * returns the key for the extent through ins, and a tree buffer for
360 * the first block of the extent through buf.
361 *
362 * returns 0 if everything worked, non-zero otherwise.
363 */
Chris Mason9aca1d52007-03-13 11:09:37 -0400364static int alloc_extent(struct btrfs_root *root, u64 num_blocks,
365 u64 search_start, u64 search_end, u64 owner,
366 struct btrfs_key *ins)
Chris Masonfec577f2007-02-26 10:40:21 -0500367{
368 int ret;
369 int pending_ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400370 struct btrfs_root *extent_root = root->extent_root;
371 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -0500372
Chris Masoncf27e1e2007-03-13 09:49:06 -0400373 btrfs_set_extent_refs(&extent_item, 1);
374 btrfs_set_extent_owner(&extent_item, owner);
Chris Masonfec577f2007-02-26 10:40:21 -0500375
Chris Mason037e6392007-03-07 11:50:24 -0500376 if (root == extent_root) {
377 BUG_ON(extent_root->current_insert.offset == 0);
378 BUG_ON(num_blocks != 1);
379 BUG_ON(extent_root->current_insert.flags ==
380 extent_root->current_insert.offset);
381 ins->offset = 1;
382 ins->objectid = extent_root->current_insert.objectid +
383 extent_root->current_insert.flags++;
Chris Masonfec577f2007-02-26 10:40:21 -0500384 return 0;
385 }
Chris Mason037e6392007-03-07 11:50:24 -0500386 ret = find_free_extent(root, num_blocks, search_start,
387 search_end, ins);
388 if (ret)
389 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -0500390
Chris Mason234b63a2007-03-13 10:46:10 -0400391 ret = btrfs_insert_item(extent_root, ins, &extent_item,
Chris Mason037e6392007-03-07 11:50:24 -0500392 sizeof(extent_item));
393
394 finish_current_insert(extent_root);
395 pending_ret = run_pending(extent_root);
396 if (ret)
397 return ret;
398 if (pending_ret)
399 return pending_ret;
400 return 0;
Chris Masonfec577f2007-02-26 10:40:21 -0500401}
402
403/*
404 * helper function to allocate a block for a given tree
405 * returns the tree buffer or NULL.
406 */
Chris Mason234b63a2007-03-13 10:46:10 -0400407struct btrfs_buffer *btrfs_alloc_free_block(struct btrfs_root *root)
Chris Masonfec577f2007-02-26 10:40:21 -0500408{
Chris Masone2fa7222007-03-12 16:22:34 -0400409 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -0500410 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400411 struct btrfs_buffer *buf;
Chris Masonfec577f2007-02-26 10:40:21 -0500412
413 ret = alloc_extent(root, 1, 0, (unsigned long)-1,
Chris Mason7518a232007-03-12 12:01:18 -0400414 btrfs_header_parentid(&root->node->node.header),
Chris Mason037e6392007-03-07 11:50:24 -0500415 &ins);
Chris Masonfec577f2007-02-26 10:40:21 -0500416 if (ret) {
417 BUG();
418 return NULL;
419 }
Chris Mason037e6392007-03-07 11:50:24 -0500420 buf = find_tree_block(root, ins.objectid);
421 dirty_tree_block(root, buf);
Chris Masonfec577f2007-02-26 10:40:21 -0500422 return buf;
423}
Chris Masona28ec192007-03-06 20:08:01 -0500424
Chris Mason9aca1d52007-03-13 11:09:37 -0400425/*
426 * helper function for drop_snapshot, this walks down the tree dropping ref
427 * counts as it goes.
428 */
429static int walk_down_tree(struct btrfs_root *root,
430 struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -0500431{
Chris Mason234b63a2007-03-13 10:46:10 -0400432 struct btrfs_buffer *next;
433 struct btrfs_buffer *cur;
Chris Mason20524f02007-03-10 06:35:47 -0500434 u64 blocknr;
435 int ret;
436 u32 refs;
437
438 ret = lookup_block_ref(root, path->nodes[*level]->blocknr, &refs);
439 BUG_ON(ret);
440 if (refs > 1)
441 goto out;
Chris Mason9aca1d52007-03-13 11:09:37 -0400442 /*
443 * walk down to the last node level and free all the leaves
444 */
Chris Mason20524f02007-03-10 06:35:47 -0500445 while(*level > 0) {
446 cur = path->nodes[*level];
Chris Mason7518a232007-03-12 12:01:18 -0400447 if (path->slots[*level] >=
448 btrfs_header_nritems(&cur->node.header))
Chris Mason20524f02007-03-10 06:35:47 -0500449 break;
Chris Mason1d4f8a02007-03-13 09:28:32 -0400450 blocknr = btrfs_node_blockptr(&cur->node, path->slots[*level]);
Chris Mason20524f02007-03-10 06:35:47 -0500451 ret = lookup_block_ref(root, blocknr, &refs);
452 if (refs != 1 || *level == 1) {
453 path->slots[*level]++;
Chris Mason234b63a2007-03-13 10:46:10 -0400454 ret = btrfs_free_extent(root, blocknr, 1);
Chris Mason20524f02007-03-10 06:35:47 -0500455 BUG_ON(ret);
456 continue;
457 }
458 BUG_ON(ret);
459 next = read_tree_block(root, blocknr);
Chris Mason83e15a22007-03-12 09:03:27 -0400460 if (path->nodes[*level-1])
Chris Mason234b63a2007-03-13 10:46:10 -0400461 btrfs_block_release(root, path->nodes[*level-1]);
Chris Mason20524f02007-03-10 06:35:47 -0500462 path->nodes[*level-1] = next;
Chris Mason7518a232007-03-12 12:01:18 -0400463 *level = btrfs_header_level(&next->node.header);
Chris Mason20524f02007-03-10 06:35:47 -0500464 path->slots[*level] = 0;
465 }
466out:
Chris Mason234b63a2007-03-13 10:46:10 -0400467 ret = btrfs_free_extent(root, path->nodes[*level]->blocknr, 1);
468 btrfs_block_release(root, path->nodes[*level]);
Chris Mason20524f02007-03-10 06:35:47 -0500469 path->nodes[*level] = NULL;
470 *level += 1;
471 BUG_ON(ret);
472 return 0;
473}
474
Chris Mason9aca1d52007-03-13 11:09:37 -0400475/*
476 * helper for dropping snapshots. This walks back up the tree in the path
477 * to find the first node higher up where we haven't yet gone through
478 * all the slots
479 */
480static int walk_up_tree(struct btrfs_root *root, struct btrfs_path *path,
481 int *level)
Chris Mason20524f02007-03-10 06:35:47 -0500482{
483 int i;
484 int slot;
485 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400486 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Mason20524f02007-03-10 06:35:47 -0500487 slot = path->slots[i];
Chris Mason7518a232007-03-12 12:01:18 -0400488 if (slot <
489 btrfs_header_nritems(&path->nodes[i]->node.header)- 1) {
Chris Mason20524f02007-03-10 06:35:47 -0500490 path->slots[i]++;
491 *level = i;
492 return 0;
493 } else {
Chris Mason234b63a2007-03-13 10:46:10 -0400494 ret = btrfs_free_extent(root,
Chris Mason20524f02007-03-10 06:35:47 -0500495 path->nodes[*level]->blocknr, 1);
Chris Mason234b63a2007-03-13 10:46:10 -0400496 btrfs_block_release(root, path->nodes[*level]);
Chris Mason83e15a22007-03-12 09:03:27 -0400497 path->nodes[*level] = NULL;
Chris Mason20524f02007-03-10 06:35:47 -0500498 *level = i + 1;
499 BUG_ON(ret);
500 }
501 }
502 return 1;
503}
504
Chris Mason9aca1d52007-03-13 11:09:37 -0400505/*
506 * drop the reference count on the tree rooted at 'snap'. This traverses
507 * the tree freeing any blocks that have a ref count of zero after being
508 * decremented.
509 */
Chris Mason234b63a2007-03-13 10:46:10 -0400510int btrfs_drop_snapshot(struct btrfs_root *root, struct btrfs_buffer *snap)
Chris Mason20524f02007-03-10 06:35:47 -0500511{
Chris Mason3768f362007-03-13 16:47:54 -0400512 int ret = 0;
Chris Mason9aca1d52007-03-13 11:09:37 -0400513 int wret;
Chris Mason20524f02007-03-10 06:35:47 -0500514 int level;
Chris Mason234b63a2007-03-13 10:46:10 -0400515 struct btrfs_path path;
Chris Mason20524f02007-03-10 06:35:47 -0500516 int i;
517 int orig_level;
518
Chris Mason234b63a2007-03-13 10:46:10 -0400519 btrfs_init_path(&path);
Chris Mason20524f02007-03-10 06:35:47 -0500520
Chris Mason7518a232007-03-12 12:01:18 -0400521 level = btrfs_header_level(&snap->node.header);
Chris Mason20524f02007-03-10 06:35:47 -0500522 orig_level = level;
523 path.nodes[level] = snap;
524 path.slots[level] = 0;
525 while(1) {
Chris Mason9aca1d52007-03-13 11:09:37 -0400526 wret = walk_down_tree(root, &path, &level);
527 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -0500528 break;
Chris Mason9aca1d52007-03-13 11:09:37 -0400529 if (wret < 0)
530 ret = wret;
531
532 wret = walk_up_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;
Chris Mason20524f02007-03-10 06:35:47 -0500537 }
Chris Mason83e15a22007-03-12 09:03:27 -0400538 for (i = 0; i <= orig_level; i++) {
539 if (path.nodes[i]) {
Chris Mason234b63a2007-03-13 10:46:10 -0400540 btrfs_block_release(root, path.nodes[i]);
Chris Mason83e15a22007-03-12 09:03:27 -0400541 }
Chris Mason20524f02007-03-10 06:35:47 -0500542 }
Chris Mason9aca1d52007-03-13 11:09:37 -0400543 return ret;
Chris Mason20524f02007-03-10 06:35:47 -0500544}