blob: c29b92d440e05bf11c99ee7d2c25e8ad076e38a3 [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"
Chris Masone089f052007-03-16 16:20:31 -04008#include "transaction.h"
Chris Masonfec577f2007-02-26 10:40:21 -05009
Chris Masone089f052007-03-16 16:20:31 -040010static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
11 *orig_root, u64 num_blocks, u64 search_start, u64
12 search_end, struct btrfs_key *ins);
13static int finish_current_insert(struct btrfs_trans_handle *trans, struct
14 btrfs_root *extent_root);
15static int run_pending(struct btrfs_trans_handle *trans, struct btrfs_root
16 *extent_root);
Chris Mason037e6392007-03-07 11:50:24 -050017
Chris Masonfec577f2007-02-26 10:40:21 -050018/*
19 * pending extents are blocks that we're trying to allocate in the extent
20 * map while trying to grow the map because of other allocations. To avoid
21 * recursing, they are tagged in the radix tree and cleaned up after
22 * other allocations are done. The pending tag is also used in the same
23 * manner for deletes.
24 */
Chris Mason037e6392007-03-07 11:50:24 -050025#define CTREE_EXTENT_PENDING_DEL 0
Chris Masonfec577f2007-02-26 10:40:21 -050026
Chris Masone089f052007-03-16 16:20:31 -040027static int inc_block_ref(struct btrfs_trans_handle *trans, struct btrfs_root
28 *root, u64 blocknr)
Chris Mason02217ed2007-03-02 16:08:05 -050029{
Chris Mason234b63a2007-03-13 10:46:10 -040030 struct btrfs_path path;
Chris Mason02217ed2007-03-02 16:08:05 -050031 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -040032 struct btrfs_key key;
Chris Mason234b63a2007-03-13 10:46:10 -040033 struct btrfs_leaf *l;
34 struct btrfs_extent_item *item;
Chris Masone2fa7222007-03-12 16:22:34 -040035 struct btrfs_key ins;
Chris Masoncf27e1e2007-03-13 09:49:06 -040036 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -050037
Chris Masone089f052007-03-16 16:20:31 -040038 find_free_extent(trans, root->extent_root, 0, 0, (u64)-1, &ins);
Chris Mason234b63a2007-03-13 10:46:10 -040039 btrfs_init_path(&path);
Chris Mason02217ed2007-03-02 16:08:05 -050040 key.objectid = blocknr;
41 key.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -040042 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Mason02217ed2007-03-02 16:08:05 -050043 key.offset = 1;
Chris Masone089f052007-03-16 16:20:31 -040044 ret = btrfs_search_slot(trans, root->extent_root, &key, &path, 0, 1);
Chris Masona28ec192007-03-06 20:08:01 -050045 if (ret != 0)
46 BUG();
Chris Mason02217ed2007-03-02 16:08:05 -050047 BUG_ON(ret != 0);
48 l = &path.nodes[0]->leaf;
Chris Mason4beb1b82007-03-14 10:31:29 -040049 item = btrfs_item_ptr(l, path.slots[0], struct btrfs_extent_item);
Chris Masoncf27e1e2007-03-13 09:49:06 -040050 refs = btrfs_extent_refs(item);
51 btrfs_set_extent_refs(item, refs + 1);
Chris Masona28ec192007-03-06 20:08:01 -050052
Chris Mason02217ed2007-03-02 16:08:05 -050053 BUG_ON(list_empty(&path.nodes[0]->dirty));
Chris Mason234b63a2007-03-13 10:46:10 -040054 btrfs_release_path(root->extent_root, &path);
Chris Masone089f052007-03-16 16:20:31 -040055 finish_current_insert(trans, root->extent_root);
56 run_pending(trans, root->extent_root);
Chris Mason02217ed2007-03-02 16:08:05 -050057 return 0;
58}
59
Chris Masone089f052007-03-16 16:20:31 -040060static int lookup_block_ref(struct btrfs_trans_handle *trans, struct btrfs_root
61 *root, u64 blocknr, u32 *refs)
Chris Masona28ec192007-03-06 20:08:01 -050062{
Chris Mason234b63a2007-03-13 10:46:10 -040063 struct btrfs_path path;
Chris Masona28ec192007-03-06 20:08:01 -050064 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -040065 struct btrfs_key key;
Chris Mason234b63a2007-03-13 10:46:10 -040066 struct btrfs_leaf *l;
67 struct btrfs_extent_item *item;
68 btrfs_init_path(&path);
Chris Masona28ec192007-03-06 20:08:01 -050069 key.objectid = blocknr;
Chris Masona28ec192007-03-06 20:08:01 -050070 key.offset = 1;
Chris Mason62e27492007-03-15 12:56:47 -040071 key.flags = 0;
72 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masone089f052007-03-16 16:20:31 -040073 ret = btrfs_search_slot(trans, root->extent_root, &key, &path, 0, 0);
Chris Masona28ec192007-03-06 20:08:01 -050074 if (ret != 0)
75 BUG();
76 l = &path.nodes[0]->leaf;
Chris Mason4beb1b82007-03-14 10:31:29 -040077 item = btrfs_item_ptr(l, path.slots[0], struct btrfs_extent_item);
Chris Masoncf27e1e2007-03-13 09:49:06 -040078 *refs = btrfs_extent_refs(item);
Chris Mason234b63a2007-03-13 10:46:10 -040079 btrfs_release_path(root->extent_root, &path);
Chris Masona28ec192007-03-06 20:08:01 -050080 return 0;
81}
82
Chris Masone089f052007-03-16 16:20:31 -040083int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
84 struct btrfs_buffer *buf)
Chris Mason02217ed2007-03-02 16:08:05 -050085{
86 u64 blocknr;
87 int i;
Chris Masona28ec192007-03-06 20:08:01 -050088
Chris Mason3768f362007-03-13 16:47:54 -040089 if (!root->ref_cows)
Chris Masona28ec192007-03-06 20:08:01 -050090 return 0;
Chris Mason7518a232007-03-12 12:01:18 -040091 if (btrfs_is_leaf(&buf->node))
Chris Masona28ec192007-03-06 20:08:01 -050092 return 0;
93
Chris Mason7518a232007-03-12 12:01:18 -040094 for (i = 0; i < btrfs_header_nritems(&buf->node.header); i++) {
Chris Mason1d4f8a02007-03-13 09:28:32 -040095 blocknr = btrfs_node_blockptr(&buf->node, i);
Chris Masone089f052007-03-16 16:20:31 -040096 inc_block_ref(trans, root, blocknr);
Chris Mason02217ed2007-03-02 16:08:05 -050097 }
98 return 0;
99}
100
Chris Masone089f052007-03-16 16:20:31 -0400101int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, struct
102 btrfs_root *root)
Chris Masona28ec192007-03-06 20:08:01 -0500103{
Chris Masona28ec192007-03-06 20:08:01 -0500104 unsigned long gang[8];
Chris Mason88fd1462007-03-16 08:56:18 -0400105 u64 first = 0;
Chris Masona28ec192007-03-06 20:08:01 -0500106 int ret;
107 int i;
108
109 while(1) {
Chris Mason3768f362007-03-13 16:47:54 -0400110 ret = radix_tree_gang_lookup(&root->pinned_radix,
Chris Masona28ec192007-03-06 20:08:01 -0500111 (void **)gang, 0,
112 ARRAY_SIZE(gang));
113 if (!ret)
114 break;
Chris Mason88fd1462007-03-16 08:56:18 -0400115 if (!first)
116 first = gang[0];
Chris Mason0579da42007-03-07 16:15:30 -0500117 for (i = 0; i < ret; i++) {
Chris Mason3768f362007-03-13 16:47:54 -0400118 radix_tree_delete(&root->pinned_radix, gang[i]);
Chris Mason0579da42007-03-07 16:15:30 -0500119 }
Chris Masona28ec192007-03-06 20:08:01 -0500120 }
Chris Mason88fd1462007-03-16 08:56:18 -0400121 root->last_insert.objectid = first;
Chris Mason3768f362007-03-13 16:47:54 -0400122 root->last_insert.offset = 0;
Chris Masona28ec192007-03-06 20:08:01 -0500123 return 0;
124}
125
Chris Masone089f052007-03-16 16:20:31 -0400126static int finish_current_insert(struct btrfs_trans_handle *trans, struct
127 btrfs_root *extent_root)
Chris Mason037e6392007-03-07 11:50:24 -0500128{
Chris Masone2fa7222007-03-12 16:22:34 -0400129 struct btrfs_key ins;
Chris Mason234b63a2007-03-13 10:46:10 -0400130 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -0500131 int i;
132 int ret;
133
Chris Masoncf27e1e2007-03-13 09:49:06 -0400134 btrfs_set_extent_refs(&extent_item, 1);
135 btrfs_set_extent_owner(&extent_item,
136 btrfs_header_parentid(&extent_root->node->node.header));
Chris Mason037e6392007-03-07 11:50:24 -0500137 ins.offset = 1;
138 ins.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -0400139 btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
Chris Mason037e6392007-03-07 11:50:24 -0500140
141 for (i = 0; i < extent_root->current_insert.flags; i++) {
142 ins.objectid = extent_root->current_insert.objectid + i;
Chris Masone089f052007-03-16 16:20:31 -0400143 ret = btrfs_insert_item(trans, extent_root, &ins, &extent_item,
144 sizeof(extent_item));
Chris Mason037e6392007-03-07 11:50:24 -0500145 BUG_ON(ret);
146 }
147 extent_root->current_insert.offset = 0;
148 return 0;
149}
150
Chris Masona28ec192007-03-06 20:08:01 -0500151/*
152 * remove an extent from the root, returns 0 on success
153 */
Chris Masone089f052007-03-16 16:20:31 -0400154static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
155 *root, u64 blocknr, u64 num_blocks, int pin)
Chris Masona28ec192007-03-06 20:08:01 -0500156{
Chris Mason234b63a2007-03-13 10:46:10 -0400157 struct btrfs_path path;
Chris Masone2fa7222007-03-12 16:22:34 -0400158 struct btrfs_key key;
Chris Mason234b63a2007-03-13 10:46:10 -0400159 struct btrfs_root *extent_root = root->extent_root;
Chris Masona28ec192007-03-06 20:08:01 -0500160 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400161 struct btrfs_extent_item *ei;
Chris Masone2fa7222007-03-12 16:22:34 -0400162 struct btrfs_key ins;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400163 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500164
Chris Mason88fd1462007-03-16 08:56:18 -0400165 BUG_ON(pin && num_blocks != 1);
Chris Masona28ec192007-03-06 20:08:01 -0500166 key.objectid = blocknr;
167 key.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -0400168 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masona28ec192007-03-06 20:08:01 -0500169 key.offset = num_blocks;
170
Chris Masone089f052007-03-16 16:20:31 -0400171 find_free_extent(trans, root, 0, 0, (u64)-1, &ins);
Chris Mason234b63a2007-03-13 10:46:10 -0400172 btrfs_init_path(&path);
Chris Masone089f052007-03-16 16:20:31 -0400173 ret = btrfs_search_slot(trans, extent_root, &key, &path, -1, 1);
Chris Masona28ec192007-03-06 20:08:01 -0500174 if (ret) {
175 printf("failed to find %Lu\n", key.objectid);
Chris Mason234b63a2007-03-13 10:46:10 -0400176 btrfs_print_tree(extent_root, extent_root->node);
Chris Masona28ec192007-03-06 20:08:01 -0500177 printf("failed to find %Lu\n", key.objectid);
178 BUG();
179 }
Chris Mason123abc82007-03-14 14:14:43 -0400180 ei = btrfs_item_ptr(&path.nodes[0]->leaf, path.slots[0],
181 struct btrfs_extent_item);
Chris Masona28ec192007-03-06 20:08:01 -0500182 BUG_ON(ei->refs == 0);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400183 refs = btrfs_extent_refs(ei) - 1;
184 btrfs_set_extent_refs(ei, refs);
185 if (refs == 0) {
Chris Mason88fd1462007-03-16 08:56:18 -0400186 if (pin) {
Chris Masona28ec192007-03-06 20:08:01 -0500187 int err;
188 radix_tree_preload(GFP_KERNEL);
189 err = radix_tree_insert(&extent_root->pinned_radix,
190 blocknr, (void *)blocknr);
191 BUG_ON(err);
192 radix_tree_preload_end();
193 }
Chris Masone089f052007-03-16 16:20:31 -0400194 ret = btrfs_del_item(trans, extent_root, &path);
Chris Mason88fd1462007-03-16 08:56:18 -0400195 if (!pin && extent_root->last_insert.objectid > blocknr)
Chris Mason0579da42007-03-07 16:15:30 -0500196 extent_root->last_insert.objectid = blocknr;
Chris Masona28ec192007-03-06 20:08:01 -0500197 if (ret)
198 BUG();
199 }
Chris Mason234b63a2007-03-13 10:46:10 -0400200 btrfs_release_path(extent_root, &path);
Chris Masone089f052007-03-16 16:20:31 -0400201 finish_current_insert(trans, extent_root);
Chris Masona28ec192007-03-06 20:08:01 -0500202 return ret;
203}
204
205/*
Chris Masonfec577f2007-02-26 10:40:21 -0500206 * find all the blocks marked as pending in the radix tree and remove
207 * them from the extent map
208 */
Chris Masone089f052007-03-16 16:20:31 -0400209static int del_pending_extents(struct btrfs_trans_handle *trans, struct
210 btrfs_root *extent_root)
Chris Masonfec577f2007-02-26 10:40:21 -0500211{
212 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400213 struct btrfs_buffer *gang[4];
Chris Masonfec577f2007-02-26 10:40:21 -0500214 int i;
Chris Masonfec577f2007-02-26 10:40:21 -0500215
216 while(1) {
217 ret = radix_tree_gang_lookup_tag(&extent_root->cache_radix,
218 (void **)gang, 0,
219 ARRAY_SIZE(gang),
Chris Masona28ec192007-03-06 20:08:01 -0500220 CTREE_EXTENT_PENDING_DEL);
Chris Masonfec577f2007-02-26 10:40:21 -0500221 if (!ret)
222 break;
223 for (i = 0; i < ret; i++) {
Chris Masone089f052007-03-16 16:20:31 -0400224 ret = __free_extent(trans, extent_root,
Chris Mason88fd1462007-03-16 08:56:18 -0400225 gang[i]->blocknr, 1, 1);
Chris Masonfec577f2007-02-26 10:40:21 -0500226 radix_tree_tag_clear(&extent_root->cache_radix,
227 gang[i]->blocknr,
Chris Masona28ec192007-03-06 20:08:01 -0500228 CTREE_EXTENT_PENDING_DEL);
Chris Mason234b63a2007-03-13 10:46:10 -0400229 btrfs_block_release(extent_root, gang[i]);
Chris Masonfec577f2007-02-26 10:40:21 -0500230 }
231 }
232 return 0;
233}
234
Chris Masone089f052007-03-16 16:20:31 -0400235static int run_pending(struct btrfs_trans_handle *trans, struct btrfs_root
236 *extent_root)
Chris Masona28ec192007-03-06 20:08:01 -0500237{
238 while(radix_tree_tagged(&extent_root->cache_radix,
Chris Mason037e6392007-03-07 11:50:24 -0500239 CTREE_EXTENT_PENDING_DEL))
Chris Masone089f052007-03-16 16:20:31 -0400240 del_pending_extents(trans, extent_root);
Chris Masona28ec192007-03-06 20:08:01 -0500241 return 0;
242}
243
244
Chris Masonfec577f2007-02-26 10:40:21 -0500245/*
246 * remove an extent from the root, returns 0 on success
247 */
Chris Masone089f052007-03-16 16:20:31 -0400248int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
249 *root, u64 blocknr, u64 num_blocks, int pin)
Chris Masonfec577f2007-02-26 10:40:21 -0500250{
Chris Mason234b63a2007-03-13 10:46:10 -0400251 struct btrfs_root *extent_root = root->extent_root;
252 struct btrfs_buffer *t;
Chris Masonfec577f2007-02-26 10:40:21 -0500253 int pending_ret;
254 int ret;
Chris Masona28ec192007-03-06 20:08:01 -0500255
256 if (root == extent_root) {
257 t = find_tree_block(root, blocknr);
Chris Mason037e6392007-03-07 11:50:24 -0500258 radix_tree_tag_set(&root->cache_radix, blocknr,
Chris Masona28ec192007-03-06 20:08:01 -0500259 CTREE_EXTENT_PENDING_DEL);
Chris Masona28ec192007-03-06 20:08:01 -0500260 return 0;
261 }
Chris Masone089f052007-03-16 16:20:31 -0400262 ret = __free_extent(trans, root, blocknr, num_blocks, pin);
263 pending_ret = run_pending(trans, root->extent_root);
Chris Masonfec577f2007-02-26 10:40:21 -0500264 return ret ? ret : pending_ret;
265}
266
267/*
268 * walks the btree of allocated extents and find a hole of a given size.
269 * The key ins is changed to record the hole:
270 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -0400271 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -0500272 * ins->offset == number of blocks
273 * Any available blocks before search_start are skipped.
274 */
Chris Masone089f052007-03-16 16:20:31 -0400275static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
276 *orig_root, u64 num_blocks, u64 search_start, u64
277 search_end, struct btrfs_key *ins)
Chris Masonfec577f2007-02-26 10:40:21 -0500278{
Chris Mason234b63a2007-03-13 10:46:10 -0400279 struct btrfs_path path;
Chris Masone2fa7222007-03-12 16:22:34 -0400280 struct btrfs_key key;
Chris Masonfec577f2007-02-26 10:40:21 -0500281 int ret;
282 u64 hole_size = 0;
283 int slot = 0;
284 u64 last_block;
Chris Mason037e6392007-03-07 11:50:24 -0500285 u64 test_block;
Chris Masonfec577f2007-02-26 10:40:21 -0500286 int start_found;
Chris Mason234b63a2007-03-13 10:46:10 -0400287 struct btrfs_leaf *l;
288 struct btrfs_root * root = orig_root->extent_root;
Chris Mason0579da42007-03-07 16:15:30 -0500289 int total_needed = num_blocks;
Chris Masonfec577f2007-02-26 10:40:21 -0500290
Chris Mason7518a232007-03-12 12:01:18 -0400291 total_needed += (btrfs_header_level(&root->node->node.header) + 1) * 3;
Chris Mason0579da42007-03-07 16:15:30 -0500292 if (root->last_insert.objectid > search_start)
293 search_start = root->last_insert.objectid;
Chris Mason62e27492007-03-15 12:56:47 -0400294
295 ins->flags = 0;
296 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
297
Chris Masonfec577f2007-02-26 10:40:21 -0500298check_failed:
Chris Mason234b63a2007-03-13 10:46:10 -0400299 btrfs_init_path(&path);
Chris Masonfec577f2007-02-26 10:40:21 -0500300 ins->objectid = search_start;
301 ins->offset = 0;
Chris Masonfec577f2007-02-26 10:40:21 -0500302 start_found = 0;
Chris Masone089f052007-03-16 16:20:31 -0400303 ret = btrfs_search_slot(trans, root, ins, &path, 0, 0);
Chris Mason0f70abe2007-02-28 16:46:22 -0500304 if (ret < 0)
305 goto error;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500306
Chris Mason0579da42007-03-07 16:15:30 -0500307 if (path.slots[0] > 0)
308 path.slots[0]--;
309
Chris Masonfec577f2007-02-26 10:40:21 -0500310 while (1) {
311 l = &path.nodes[0]->leaf;
312 slot = path.slots[0];
Chris Mason7518a232007-03-12 12:01:18 -0400313 if (slot >= btrfs_header_nritems(&l->header)) {
Chris Mason234b63a2007-03-13 10:46:10 -0400314 ret = btrfs_next_leaf(root, &path);
Chris Masonfec577f2007-02-26 10:40:21 -0500315 if (ret == 0)
316 continue;
Chris Mason0f70abe2007-02-28 16:46:22 -0500317 if (ret < 0)
318 goto error;
Chris Masonfec577f2007-02-26 10:40:21 -0500319 if (!start_found) {
320 ins->objectid = search_start;
Chris Mason037e6392007-03-07 11:50:24 -0500321 ins->offset = (u64)-1;
Chris Masonfec577f2007-02-26 10:40:21 -0500322 start_found = 1;
323 goto check_pending;
324 }
325 ins->objectid = last_block > search_start ?
326 last_block : search_start;
Chris Mason037e6392007-03-07 11:50:24 -0500327 ins->offset = (u64)-1;
Chris Masonfec577f2007-02-26 10:40:21 -0500328 goto check_pending;
329 }
Chris Masone2fa7222007-03-12 16:22:34 -0400330 btrfs_disk_key_to_cpu(&key, &l->items[slot].key);
331 if (key.objectid >= search_start) {
Chris Masonfec577f2007-02-26 10:40:21 -0500332 if (start_found) {
Chris Mason0579da42007-03-07 16:15:30 -0500333 if (last_block < search_start)
334 last_block = search_start;
Chris Masone2fa7222007-03-12 16:22:34 -0400335 hole_size = key.objectid - last_block;
Chris Mason037e6392007-03-07 11:50:24 -0500336 if (hole_size > total_needed) {
Chris Masonfec577f2007-02-26 10:40:21 -0500337 ins->objectid = last_block;
Chris Mason037e6392007-03-07 11:50:24 -0500338 ins->offset = hole_size;
Chris Masonfec577f2007-02-26 10:40:21 -0500339 goto check_pending;
340 }
Chris Mason0579da42007-03-07 16:15:30 -0500341 }
Chris Masonfec577f2007-02-26 10:40:21 -0500342 }
Chris Mason0579da42007-03-07 16:15:30 -0500343 start_found = 1;
Chris Masone2fa7222007-03-12 16:22:34 -0400344 last_block = key.objectid + key.offset;
Chris Masonfec577f2007-02-26 10:40:21 -0500345 path.slots[0]++;
346 }
347 // FIXME -ENOSPC
348check_pending:
349 /* we have to make sure we didn't find an extent that has already
350 * been allocated by the map tree or the original allocation
351 */
Chris Mason234b63a2007-03-13 10:46:10 -0400352 btrfs_release_path(root, &path);
Chris Masonfec577f2007-02-26 10:40:21 -0500353 BUG_ON(ins->objectid < search_start);
Chris Mason037e6392007-03-07 11:50:24 -0500354 for (test_block = ins->objectid;
355 test_block < ins->objectid + total_needed; test_block++) {
356 if (radix_tree_lookup(&root->pinned_radix, test_block)) {
357 search_start = test_block + 1;
Chris Masonfec577f2007-02-26 10:40:21 -0500358 goto check_failed;
359 }
360 }
Chris Mason037e6392007-03-07 11:50:24 -0500361 BUG_ON(root->current_insert.offset);
Chris Mason0579da42007-03-07 16:15:30 -0500362 root->current_insert.offset = total_needed - num_blocks;
Chris Mason037e6392007-03-07 11:50:24 -0500363 root->current_insert.objectid = ins->objectid + num_blocks;
364 root->current_insert.flags = 0;
Chris Mason0579da42007-03-07 16:15:30 -0500365 root->last_insert.objectid = ins->objectid;
Chris Mason037e6392007-03-07 11:50:24 -0500366 ins->offset = num_blocks;
Chris Masonfec577f2007-02-26 10:40:21 -0500367 return 0;
Chris Mason0f70abe2007-02-28 16:46:22 -0500368error:
Chris Mason234b63a2007-03-13 10:46:10 -0400369 btrfs_release_path(root, &path);
Chris Mason0f70abe2007-02-28 16:46:22 -0500370 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -0500371}
372
373/*
Chris Masonfec577f2007-02-26 10:40:21 -0500374 * finds a free extent and does all the dirty work required for allocation
375 * returns the key for the extent through ins, and a tree buffer for
376 * the first block of the extent through buf.
377 *
378 * returns 0 if everything worked, non-zero otherwise.
379 */
Chris Masone089f052007-03-16 16:20:31 -0400380static int alloc_extent(struct btrfs_trans_handle *trans, struct btrfs_root
381 *root, u64 num_blocks, u64 search_start, u64
382 search_end, u64 owner, struct btrfs_key *ins)
Chris Masonfec577f2007-02-26 10:40:21 -0500383{
384 int ret;
385 int pending_ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400386 struct btrfs_root *extent_root = root->extent_root;
387 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -0500388
Chris Masoncf27e1e2007-03-13 09:49:06 -0400389 btrfs_set_extent_refs(&extent_item, 1);
390 btrfs_set_extent_owner(&extent_item, owner);
Chris Masonfec577f2007-02-26 10:40:21 -0500391
Chris Mason037e6392007-03-07 11:50:24 -0500392 if (root == extent_root) {
393 BUG_ON(extent_root->current_insert.offset == 0);
394 BUG_ON(num_blocks != 1);
395 BUG_ON(extent_root->current_insert.flags ==
396 extent_root->current_insert.offset);
397 ins->offset = 1;
398 ins->objectid = extent_root->current_insert.objectid +
399 extent_root->current_insert.flags++;
Chris Masonfec577f2007-02-26 10:40:21 -0500400 return 0;
401 }
Chris Masone089f052007-03-16 16:20:31 -0400402 ret = find_free_extent(trans, root, num_blocks, search_start,
Chris Mason037e6392007-03-07 11:50:24 -0500403 search_end, ins);
404 if (ret)
405 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -0500406
Chris Masone089f052007-03-16 16:20:31 -0400407 ret = btrfs_insert_item(trans, extent_root, ins, &extent_item,
408 sizeof(extent_item));
Chris Mason037e6392007-03-07 11:50:24 -0500409
Chris Masone089f052007-03-16 16:20:31 -0400410 finish_current_insert(trans, extent_root);
411 pending_ret = run_pending(trans, extent_root);
Chris Mason037e6392007-03-07 11:50:24 -0500412 if (ret)
413 return ret;
414 if (pending_ret)
415 return pending_ret;
416 return 0;
Chris Masonfec577f2007-02-26 10:40:21 -0500417}
418
419/*
420 * helper function to allocate a block for a given tree
421 * returns the tree buffer or NULL.
422 */
Chris Masone089f052007-03-16 16:20:31 -0400423struct btrfs_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
424 struct btrfs_root *root)
Chris Masonfec577f2007-02-26 10:40:21 -0500425{
Chris Masone2fa7222007-03-12 16:22:34 -0400426 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -0500427 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400428 struct btrfs_buffer *buf;
Chris Masonfec577f2007-02-26 10:40:21 -0500429
Chris Masone089f052007-03-16 16:20:31 -0400430 ret = alloc_extent(trans, root, 1, 0, (unsigned long)-1,
Chris Mason7518a232007-03-12 12:01:18 -0400431 btrfs_header_parentid(&root->node->node.header),
Chris Mason037e6392007-03-07 11:50:24 -0500432 &ins);
Chris Masonfec577f2007-02-26 10:40:21 -0500433 if (ret) {
434 BUG();
435 return NULL;
436 }
Chris Mason037e6392007-03-07 11:50:24 -0500437 buf = find_tree_block(root, ins.objectid);
Chris Masone089f052007-03-16 16:20:31 -0400438 dirty_tree_block(trans, root, buf);
Chris Masonfec577f2007-02-26 10:40:21 -0500439 return buf;
440}
Chris Masona28ec192007-03-06 20:08:01 -0500441
Chris Mason9aca1d52007-03-13 11:09:37 -0400442/*
443 * helper function for drop_snapshot, this walks down the tree dropping ref
444 * counts as it goes.
445 */
Chris Masone089f052007-03-16 16:20:31 -0400446static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
447 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -0500448{
Chris Mason234b63a2007-03-13 10:46:10 -0400449 struct btrfs_buffer *next;
450 struct btrfs_buffer *cur;
Chris Mason20524f02007-03-10 06:35:47 -0500451 u64 blocknr;
452 int ret;
453 u32 refs;
454
Chris Masone089f052007-03-16 16:20:31 -0400455 ret = lookup_block_ref(trans, root, path->nodes[*level]->blocknr,
456 &refs);
Chris Mason20524f02007-03-10 06:35:47 -0500457 BUG_ON(ret);
458 if (refs > 1)
459 goto out;
Chris Mason9aca1d52007-03-13 11:09:37 -0400460 /*
461 * walk down to the last node level and free all the leaves
462 */
Chris Mason20524f02007-03-10 06:35:47 -0500463 while(*level > 0) {
464 cur = path->nodes[*level];
Chris Mason7518a232007-03-12 12:01:18 -0400465 if (path->slots[*level] >=
466 btrfs_header_nritems(&cur->node.header))
Chris Mason20524f02007-03-10 06:35:47 -0500467 break;
Chris Mason1d4f8a02007-03-13 09:28:32 -0400468 blocknr = btrfs_node_blockptr(&cur->node, path->slots[*level]);
Chris Masone089f052007-03-16 16:20:31 -0400469 ret = lookup_block_ref(trans, root, blocknr, &refs);
Chris Mason20524f02007-03-10 06:35:47 -0500470 if (refs != 1 || *level == 1) {
471 path->slots[*level]++;
Chris Masone089f052007-03-16 16:20:31 -0400472 ret = btrfs_free_extent(trans, root, blocknr, 1, 1);
Chris Mason20524f02007-03-10 06:35:47 -0500473 BUG_ON(ret);
474 continue;
475 }
476 BUG_ON(ret);
477 next = read_tree_block(root, blocknr);
Chris Mason83e15a22007-03-12 09:03:27 -0400478 if (path->nodes[*level-1])
Chris Mason234b63a2007-03-13 10:46:10 -0400479 btrfs_block_release(root, path->nodes[*level-1]);
Chris Mason20524f02007-03-10 06:35:47 -0500480 path->nodes[*level-1] = next;
Chris Mason7518a232007-03-12 12:01:18 -0400481 *level = btrfs_header_level(&next->node.header);
Chris Mason20524f02007-03-10 06:35:47 -0500482 path->slots[*level] = 0;
483 }
484out:
Chris Masone089f052007-03-16 16:20:31 -0400485 ret = btrfs_free_extent(trans, root, path->nodes[*level]->blocknr, 1,
486 1);
Chris Mason234b63a2007-03-13 10:46:10 -0400487 btrfs_block_release(root, path->nodes[*level]);
Chris Mason20524f02007-03-10 06:35:47 -0500488 path->nodes[*level] = NULL;
489 *level += 1;
490 BUG_ON(ret);
491 return 0;
492}
493
Chris Mason9aca1d52007-03-13 11:09:37 -0400494/*
495 * helper for dropping snapshots. This walks back up the tree in the path
496 * to find the first node higher up where we haven't yet gone through
497 * all the slots
498 */
Chris Masone089f052007-03-16 16:20:31 -0400499static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root
500 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -0500501{
502 int i;
503 int slot;
504 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400505 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Mason20524f02007-03-10 06:35:47 -0500506 slot = path->slots[i];
Chris Mason7518a232007-03-12 12:01:18 -0400507 if (slot <
508 btrfs_header_nritems(&path->nodes[i]->node.header)- 1) {
Chris Mason20524f02007-03-10 06:35:47 -0500509 path->slots[i]++;
510 *level = i;
511 return 0;
512 } else {
Chris Masone089f052007-03-16 16:20:31 -0400513 ret = btrfs_free_extent(trans, root,
514 path->nodes[*level]->blocknr,
515 1, 1);
Chris Mason234b63a2007-03-13 10:46:10 -0400516 btrfs_block_release(root, path->nodes[*level]);
Chris Mason83e15a22007-03-12 09:03:27 -0400517 path->nodes[*level] = NULL;
Chris Mason20524f02007-03-10 06:35:47 -0500518 *level = i + 1;
519 BUG_ON(ret);
520 }
521 }
522 return 1;
523}
524
Chris Mason9aca1d52007-03-13 11:09:37 -0400525/*
526 * drop the reference count on the tree rooted at 'snap'. This traverses
527 * the tree freeing any blocks that have a ref count of zero after being
528 * decremented.
529 */
Chris Masone089f052007-03-16 16:20:31 -0400530int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
531 *root, struct btrfs_buffer *snap)
Chris Mason20524f02007-03-10 06:35:47 -0500532{
Chris Mason3768f362007-03-13 16:47:54 -0400533 int ret = 0;
Chris Mason9aca1d52007-03-13 11:09:37 -0400534 int wret;
Chris Mason20524f02007-03-10 06:35:47 -0500535 int level;
Chris Mason234b63a2007-03-13 10:46:10 -0400536 struct btrfs_path path;
Chris Mason20524f02007-03-10 06:35:47 -0500537 int i;
538 int orig_level;
539
Chris Mason234b63a2007-03-13 10:46:10 -0400540 btrfs_init_path(&path);
Chris Mason20524f02007-03-10 06:35:47 -0500541
Chris Mason7518a232007-03-12 12:01:18 -0400542 level = btrfs_header_level(&snap->node.header);
Chris Mason20524f02007-03-10 06:35:47 -0500543 orig_level = level;
544 path.nodes[level] = snap;
545 path.slots[level] = 0;
546 while(1) {
Chris Masone089f052007-03-16 16:20:31 -0400547 wret = walk_down_tree(trans, root, &path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -0400548 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -0500549 break;
Chris Mason9aca1d52007-03-13 11:09:37 -0400550 if (wret < 0)
551 ret = wret;
552
Chris Masone089f052007-03-16 16:20:31 -0400553 wret = walk_up_tree(trans, root, &path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -0400554 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -0500555 break;
Chris Mason9aca1d52007-03-13 11:09:37 -0400556 if (wret < 0)
557 ret = wret;
Chris Mason20524f02007-03-10 06:35:47 -0500558 }
Chris Mason83e15a22007-03-12 09:03:27 -0400559 for (i = 0; i <= orig_level; i++) {
560 if (path.nodes[i]) {
Chris Mason234b63a2007-03-13 10:46:10 -0400561 btrfs_block_release(root, path.nodes[i]);
Chris Mason83e15a22007-03-12 09:03:27 -0400562 }
Chris Mason20524f02007-03-10 06:35:47 -0500563 }
Chris Mason9aca1d52007-03-13 11:09:37 -0400564 return ret;
Chris Mason20524f02007-03-10 06:35:47 -0500565}