blob: 53a7550b5c1e5b852933141518d8b19c7b441d4b [file] [log] [blame]
Chris Mason2e635a22007-03-21 11:12:56 -04001#include <linux/module.h>
2#include <linux/radix-tree.h>
Chris Masonfec577f2007-02-26 10:40:21 -05003#include "ctree.h"
4#include "disk-io.h"
5#include "print-tree.h"
Chris Masone089f052007-03-16 16:20:31 -04006#include "transaction.h"
Chris Masonfec577f2007-02-26 10:40:21 -05007
Chris Masone089f052007-03-16 16:20:31 -04008static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
9 *orig_root, u64 num_blocks, u64 search_start, u64
10 search_end, struct btrfs_key *ins);
11static int finish_current_insert(struct btrfs_trans_handle *trans, struct
12 btrfs_root *extent_root);
13static int run_pending(struct btrfs_trans_handle *trans, struct btrfs_root
14 *extent_root);
Chris Mason037e6392007-03-07 11:50:24 -050015
Chris Masonfec577f2007-02-26 10:40:21 -050016/*
17 * pending extents are blocks that we're trying to allocate in the extent
18 * map while trying to grow the map because of other allocations. To avoid
19 * recursing, they are tagged in the radix tree and cleaned up after
20 * other allocations are done. The pending tag is also used in the same
21 * manner for deletes.
22 */
Chris Mason037e6392007-03-07 11:50:24 -050023#define CTREE_EXTENT_PENDING_DEL 0
Chris Masonfec577f2007-02-26 10:40:21 -050024
Chris Masone089f052007-03-16 16:20:31 -040025static int inc_block_ref(struct btrfs_trans_handle *trans, struct btrfs_root
26 *root, u64 blocknr)
Chris Mason02217ed2007-03-02 16:08:05 -050027{
Chris Mason234b63a2007-03-13 10:46:10 -040028 struct btrfs_path path;
Chris Mason02217ed2007-03-02 16:08:05 -050029 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -040030 struct btrfs_key key;
Chris Mason234b63a2007-03-13 10:46:10 -040031 struct btrfs_leaf *l;
32 struct btrfs_extent_item *item;
Chris Masone2fa7222007-03-12 16:22:34 -040033 struct btrfs_key ins;
Chris Masoncf27e1e2007-03-13 09:49:06 -040034 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -050035
Chris Mason9f5fae22007-03-20 14:38:32 -040036 find_free_extent(trans, root->fs_info->extent_root, 0, 0, (u64)-1,
37 &ins);
Chris Mason234b63a2007-03-13 10:46:10 -040038 btrfs_init_path(&path);
Chris Mason02217ed2007-03-02 16:08:05 -050039 key.objectid = blocknr;
40 key.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -040041 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Mason02217ed2007-03-02 16:08:05 -050042 key.offset = 1;
Chris Mason9f5fae22007-03-20 14:38:32 -040043 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, &path,
44 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 Mason9f5fae22007-03-20 14:38:32 -040054 btrfs_release_path(root->fs_info->extent_root, &path);
55 finish_current_insert(trans, root->fs_info->extent_root);
56 run_pending(trans, root->fs_info->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 Mason9f5fae22007-03-20 14:38:32 -040073 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, &path,
74 0, 0);
Chris Masona28ec192007-03-06 20:08:01 -050075 if (ret != 0)
76 BUG();
77 l = &path.nodes[0]->leaf;
Chris Mason4beb1b82007-03-14 10:31:29 -040078 item = btrfs_item_ptr(l, path.slots[0], struct btrfs_extent_item);
Chris Masoncf27e1e2007-03-13 09:49:06 -040079 *refs = btrfs_extent_refs(item);
Chris Mason9f5fae22007-03-20 14:38:32 -040080 btrfs_release_path(root->fs_info->extent_root, &path);
Chris Masona28ec192007-03-06 20:08:01 -050081 return 0;
82}
83
Chris Masone089f052007-03-16 16:20:31 -040084int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
85 struct btrfs_buffer *buf)
Chris Mason02217ed2007-03-02 16:08:05 -050086{
87 u64 blocknr;
88 int i;
Chris Masona28ec192007-03-06 20:08:01 -050089
Chris Mason3768f362007-03-13 16:47:54 -040090 if (!root->ref_cows)
Chris Masona28ec192007-03-06 20:08:01 -050091 return 0;
Chris Mason7518a232007-03-12 12:01:18 -040092 if (btrfs_is_leaf(&buf->node))
Chris Masona28ec192007-03-06 20:08:01 -050093 return 0;
94
Chris Mason7518a232007-03-12 12:01:18 -040095 for (i = 0; i < btrfs_header_nritems(&buf->node.header); i++) {
Chris Mason1d4f8a02007-03-13 09:28:32 -040096 blocknr = btrfs_node_blockptr(&buf->node, i);
Chris Masone089f052007-03-16 16:20:31 -040097 inc_block_ref(trans, root, blocknr);
Chris Mason02217ed2007-03-02 16:08:05 -050098 }
99 return 0;
100}
101
Chris Masone089f052007-03-16 16:20:31 -0400102int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, struct
103 btrfs_root *root)
Chris Masona28ec192007-03-06 20:08:01 -0500104{
Chris Masona28ec192007-03-06 20:08:01 -0500105 unsigned long gang[8];
Chris Mason88fd1462007-03-16 08:56:18 -0400106 u64 first = 0;
Chris Masona28ec192007-03-06 20:08:01 -0500107 int ret;
108 int i;
109
110 while(1) {
Chris Mason9f5fae22007-03-20 14:38:32 -0400111 ret = radix_tree_gang_lookup(&root->fs_info->pinned_radix,
112 (void **)gang, 0,
113 ARRAY_SIZE(gang));
Chris Masona28ec192007-03-06 20:08:01 -0500114 if (!ret)
115 break;
Chris Mason88fd1462007-03-16 08:56:18 -0400116 if (!first)
117 first = gang[0];
Chris Mason0579da42007-03-07 16:15:30 -0500118 for (i = 0; i < ret; i++) {
Chris Mason9f5fae22007-03-20 14:38:32 -0400119 radix_tree_delete(&root->fs_info->pinned_radix,
120 gang[i]);
Chris Mason0579da42007-03-07 16:15:30 -0500121 }
Chris Masona28ec192007-03-06 20:08:01 -0500122 }
Chris Mason9f5fae22007-03-20 14:38:32 -0400123 root->fs_info->last_insert.objectid = first;
124 root->fs_info->last_insert.offset = 0;
Chris Masona28ec192007-03-06 20:08:01 -0500125 return 0;
126}
127
Chris Masone089f052007-03-16 16:20:31 -0400128static int finish_current_insert(struct btrfs_trans_handle *trans, struct
129 btrfs_root *extent_root)
Chris Mason037e6392007-03-07 11:50:24 -0500130{
Chris Masone2fa7222007-03-12 16:22:34 -0400131 struct btrfs_key ins;
Chris Mason234b63a2007-03-13 10:46:10 -0400132 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -0500133 int i;
134 int ret;
Chris Mason1261ec42007-03-20 20:35:03 -0400135 u64 super_blocks_used;
136 struct btrfs_fs_info *info = extent_root->fs_info;
Chris Mason037e6392007-03-07 11:50:24 -0500137
Chris Masoncf27e1e2007-03-13 09:49:06 -0400138 btrfs_set_extent_refs(&extent_item, 1);
139 btrfs_set_extent_owner(&extent_item,
140 btrfs_header_parentid(&extent_root->node->node.header));
Chris Mason037e6392007-03-07 11:50:24 -0500141 ins.offset = 1;
142 ins.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -0400143 btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
Chris Mason037e6392007-03-07 11:50:24 -0500144
Chris Mason9f5fae22007-03-20 14:38:32 -0400145 for (i = 0; i < extent_root->fs_info->current_insert.flags; i++) {
146 ins.objectid = extent_root->fs_info->current_insert.objectid +
147 i;
Chris Mason1261ec42007-03-20 20:35:03 -0400148 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
149 btrfs_set_super_blocks_used(info->disk_super,
150 super_blocks_used + 1);
Chris Masone089f052007-03-16 16:20:31 -0400151 ret = btrfs_insert_item(trans, extent_root, &ins, &extent_item,
152 sizeof(extent_item));
Chris Mason037e6392007-03-07 11:50:24 -0500153 BUG_ON(ret);
154 }
Chris Mason9f5fae22007-03-20 14:38:32 -0400155 extent_root->fs_info->current_insert.offset = 0;
Chris Mason037e6392007-03-07 11:50:24 -0500156 return 0;
157}
158
Chris Masona28ec192007-03-06 20:08:01 -0500159/*
160 * remove an extent from the root, returns 0 on success
161 */
Chris Masone089f052007-03-16 16:20:31 -0400162static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
163 *root, u64 blocknr, u64 num_blocks, int pin)
Chris Masona28ec192007-03-06 20:08:01 -0500164{
Chris Mason234b63a2007-03-13 10:46:10 -0400165 struct btrfs_path path;
Chris Masone2fa7222007-03-12 16:22:34 -0400166 struct btrfs_key key;
Chris Mason1261ec42007-03-20 20:35:03 -0400167 struct btrfs_fs_info *info = root->fs_info;
168 struct btrfs_root *extent_root = info->extent_root;
Chris Masona28ec192007-03-06 20:08:01 -0500169 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400170 struct btrfs_extent_item *ei;
Chris Masone2fa7222007-03-12 16:22:34 -0400171 struct btrfs_key ins;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400172 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500173
Chris Mason88fd1462007-03-16 08:56:18 -0400174 BUG_ON(pin && num_blocks != 1);
Chris Masona28ec192007-03-06 20:08:01 -0500175 key.objectid = blocknr;
176 key.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -0400177 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masona28ec192007-03-06 20:08:01 -0500178 key.offset = num_blocks;
179
Chris Masone089f052007-03-16 16:20:31 -0400180 find_free_extent(trans, root, 0, 0, (u64)-1, &ins);
Chris Mason234b63a2007-03-13 10:46:10 -0400181 btrfs_init_path(&path);
Chris Masone089f052007-03-16 16:20:31 -0400182 ret = btrfs_search_slot(trans, extent_root, &key, &path, -1, 1);
Chris Masona28ec192007-03-06 20:08:01 -0500183 if (ret) {
Chris Mason2e635a22007-03-21 11:12:56 -0400184 printk("failed to find %Lu\n", key.objectid);
Chris Mason234b63a2007-03-13 10:46:10 -0400185 btrfs_print_tree(extent_root, extent_root->node);
Chris Mason2e635a22007-03-21 11:12:56 -0400186 printk("failed to find %Lu\n", key.objectid);
Chris Masona28ec192007-03-06 20:08:01 -0500187 BUG();
188 }
Chris Mason123abc82007-03-14 14:14:43 -0400189 ei = btrfs_item_ptr(&path.nodes[0]->leaf, path.slots[0],
190 struct btrfs_extent_item);
Chris Masona28ec192007-03-06 20:08:01 -0500191 BUG_ON(ei->refs == 0);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400192 refs = btrfs_extent_refs(ei) - 1;
193 btrfs_set_extent_refs(ei, refs);
194 if (refs == 0) {
Chris Mason1261ec42007-03-20 20:35:03 -0400195 u64 super_blocks_used;
Chris Mason88fd1462007-03-16 08:56:18 -0400196 if (pin) {
Chris Masona28ec192007-03-06 20:08:01 -0500197 int err;
198 radix_tree_preload(GFP_KERNEL);
Chris Mason1261ec42007-03-20 20:35:03 -0400199 err = radix_tree_insert(&info->pinned_radix,
200 blocknr, (void *)blocknr);
Chris Masona28ec192007-03-06 20:08:01 -0500201 BUG_ON(err);
202 radix_tree_preload_end();
203 }
Chris Mason1261ec42007-03-20 20:35:03 -0400204 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
205 btrfs_set_super_blocks_used(info->disk_super,
206 super_blocks_used - num_blocks);
Chris Masone089f052007-03-16 16:20:31 -0400207 ret = btrfs_del_item(trans, extent_root, &path);
Chris Mason9f5fae22007-03-20 14:38:32 -0400208 if (!pin && extent_root->fs_info->last_insert.objectid >
209 blocknr)
210 extent_root->fs_info->last_insert.objectid = blocknr;
Chris Masona28ec192007-03-06 20:08:01 -0500211 if (ret)
212 BUG();
213 }
Chris Mason234b63a2007-03-13 10:46:10 -0400214 btrfs_release_path(extent_root, &path);
Chris Masone089f052007-03-16 16:20:31 -0400215 finish_current_insert(trans, extent_root);
Chris Masona28ec192007-03-06 20:08:01 -0500216 return ret;
217}
218
219/*
Chris Masonfec577f2007-02-26 10:40:21 -0500220 * find all the blocks marked as pending in the radix tree and remove
221 * them from the extent map
222 */
Chris Masone089f052007-03-16 16:20:31 -0400223static int del_pending_extents(struct btrfs_trans_handle *trans, struct
224 btrfs_root *extent_root)
Chris Masonfec577f2007-02-26 10:40:21 -0500225{
226 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400227 struct btrfs_buffer *gang[4];
Chris Masonfec577f2007-02-26 10:40:21 -0500228 int i;
Chris Masonfec577f2007-02-26 10:40:21 -0500229
230 while(1) {
Chris Mason9f5fae22007-03-20 14:38:32 -0400231 ret = radix_tree_gang_lookup_tag(
232 &extent_root->fs_info->cache_radix,
233 (void **)gang, 0,
234 ARRAY_SIZE(gang),
235 CTREE_EXTENT_PENDING_DEL);
Chris Masonfec577f2007-02-26 10:40:21 -0500236 if (!ret)
237 break;
238 for (i = 0; i < ret; i++) {
Chris Masone089f052007-03-16 16:20:31 -0400239 ret = __free_extent(trans, extent_root,
Chris Mason88fd1462007-03-16 08:56:18 -0400240 gang[i]->blocknr, 1, 1);
Chris Mason9f5fae22007-03-20 14:38:32 -0400241 radix_tree_tag_clear(&extent_root->fs_info->cache_radix,
242 gang[i]->blocknr,
243 CTREE_EXTENT_PENDING_DEL);
Chris Mason234b63a2007-03-13 10:46:10 -0400244 btrfs_block_release(extent_root, gang[i]);
Chris Masonfec577f2007-02-26 10:40:21 -0500245 }
246 }
247 return 0;
248}
249
Chris Masone089f052007-03-16 16:20:31 -0400250static int run_pending(struct btrfs_trans_handle *trans, struct btrfs_root
251 *extent_root)
Chris Masona28ec192007-03-06 20:08:01 -0500252{
Chris Mason9f5fae22007-03-20 14:38:32 -0400253 while(radix_tree_tagged(&extent_root->fs_info->cache_radix,
254 CTREE_EXTENT_PENDING_DEL))
Chris Masone089f052007-03-16 16:20:31 -0400255 del_pending_extents(trans, extent_root);
Chris Masona28ec192007-03-06 20:08:01 -0500256 return 0;
257}
258
259
Chris Masonfec577f2007-02-26 10:40:21 -0500260/*
261 * remove an extent from the root, returns 0 on success
262 */
Chris Masone089f052007-03-16 16:20:31 -0400263int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
264 *root, u64 blocknr, u64 num_blocks, int pin)
Chris Masonfec577f2007-02-26 10:40:21 -0500265{
Chris Mason9f5fae22007-03-20 14:38:32 -0400266 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Mason234b63a2007-03-13 10:46:10 -0400267 struct btrfs_buffer *t;
Chris Masonfec577f2007-02-26 10:40:21 -0500268 int pending_ret;
269 int ret;
Chris Masona28ec192007-03-06 20:08:01 -0500270
271 if (root == extent_root) {
272 t = find_tree_block(root, blocknr);
Chris Mason9f5fae22007-03-20 14:38:32 -0400273 radix_tree_tag_set(&root->fs_info->cache_radix, blocknr,
Chris Masona28ec192007-03-06 20:08:01 -0500274 CTREE_EXTENT_PENDING_DEL);
Chris Masona28ec192007-03-06 20:08:01 -0500275 return 0;
276 }
Chris Masone089f052007-03-16 16:20:31 -0400277 ret = __free_extent(trans, root, blocknr, num_blocks, pin);
Chris Mason9f5fae22007-03-20 14:38:32 -0400278 pending_ret = run_pending(trans, root->fs_info->extent_root);
Chris Masonfec577f2007-02-26 10:40:21 -0500279 return ret ? ret : pending_ret;
280}
281
282/*
283 * walks the btree of allocated extents and find a hole of a given size.
284 * The key ins is changed to record the hole:
285 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -0400286 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -0500287 * ins->offset == number of blocks
288 * Any available blocks before search_start are skipped.
289 */
Chris Masone089f052007-03-16 16:20:31 -0400290static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
291 *orig_root, u64 num_blocks, u64 search_start, u64
292 search_end, struct btrfs_key *ins)
Chris Masonfec577f2007-02-26 10:40:21 -0500293{
Chris Mason234b63a2007-03-13 10:46:10 -0400294 struct btrfs_path path;
Chris Masone2fa7222007-03-12 16:22:34 -0400295 struct btrfs_key key;
Chris Masonfec577f2007-02-26 10:40:21 -0500296 int ret;
297 u64 hole_size = 0;
298 int slot = 0;
299 u64 last_block;
Chris Mason037e6392007-03-07 11:50:24 -0500300 u64 test_block;
Chris Masonfec577f2007-02-26 10:40:21 -0500301 int start_found;
Chris Mason234b63a2007-03-13 10:46:10 -0400302 struct btrfs_leaf *l;
Chris Mason9f5fae22007-03-20 14:38:32 -0400303 struct btrfs_root * root = orig_root->fs_info->extent_root;
Chris Mason0579da42007-03-07 16:15:30 -0500304 int total_needed = num_blocks;
Chris Masonfec577f2007-02-26 10:40:21 -0500305
Chris Mason7518a232007-03-12 12:01:18 -0400306 total_needed += (btrfs_header_level(&root->node->node.header) + 1) * 3;
Chris Mason9f5fae22007-03-20 14:38:32 -0400307 if (root->fs_info->last_insert.objectid > search_start)
308 search_start = root->fs_info->last_insert.objectid;
Chris Mason62e27492007-03-15 12:56:47 -0400309
310 ins->flags = 0;
311 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
312
Chris Masonfec577f2007-02-26 10:40:21 -0500313check_failed:
Chris Mason234b63a2007-03-13 10:46:10 -0400314 btrfs_init_path(&path);
Chris Masonfec577f2007-02-26 10:40:21 -0500315 ins->objectid = search_start;
316 ins->offset = 0;
Chris Masonfec577f2007-02-26 10:40:21 -0500317 start_found = 0;
Chris Masone089f052007-03-16 16:20:31 -0400318 ret = btrfs_search_slot(trans, root, ins, &path, 0, 0);
Chris Mason0f70abe2007-02-28 16:46:22 -0500319 if (ret < 0)
320 goto error;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500321
Chris Mason0579da42007-03-07 16:15:30 -0500322 if (path.slots[0] > 0)
323 path.slots[0]--;
324
Chris Masonfec577f2007-02-26 10:40:21 -0500325 while (1) {
326 l = &path.nodes[0]->leaf;
327 slot = path.slots[0];
Chris Mason7518a232007-03-12 12:01:18 -0400328 if (slot >= btrfs_header_nritems(&l->header)) {
Chris Mason234b63a2007-03-13 10:46:10 -0400329 ret = btrfs_next_leaf(root, &path);
Chris Masonfec577f2007-02-26 10:40:21 -0500330 if (ret == 0)
331 continue;
Chris Mason0f70abe2007-02-28 16:46:22 -0500332 if (ret < 0)
333 goto error;
Chris Masonfec577f2007-02-26 10:40:21 -0500334 if (!start_found) {
335 ins->objectid = search_start;
Chris Mason037e6392007-03-07 11:50:24 -0500336 ins->offset = (u64)-1;
Chris Masonfec577f2007-02-26 10:40:21 -0500337 start_found = 1;
338 goto check_pending;
339 }
340 ins->objectid = last_block > search_start ?
341 last_block : search_start;
Chris Mason037e6392007-03-07 11:50:24 -0500342 ins->offset = (u64)-1;
Chris Masonfec577f2007-02-26 10:40:21 -0500343 goto check_pending;
344 }
Chris Masone2fa7222007-03-12 16:22:34 -0400345 btrfs_disk_key_to_cpu(&key, &l->items[slot].key);
346 if (key.objectid >= search_start) {
Chris Masonfec577f2007-02-26 10:40:21 -0500347 if (start_found) {
Chris Mason0579da42007-03-07 16:15:30 -0500348 if (last_block < search_start)
349 last_block = search_start;
Chris Masone2fa7222007-03-12 16:22:34 -0400350 hole_size = key.objectid - last_block;
Chris Mason037e6392007-03-07 11:50:24 -0500351 if (hole_size > total_needed) {
Chris Masonfec577f2007-02-26 10:40:21 -0500352 ins->objectid = last_block;
Chris Mason037e6392007-03-07 11:50:24 -0500353 ins->offset = hole_size;
Chris Masonfec577f2007-02-26 10:40:21 -0500354 goto check_pending;
355 }
Chris Mason0579da42007-03-07 16:15:30 -0500356 }
Chris Masonfec577f2007-02-26 10:40:21 -0500357 }
Chris Mason0579da42007-03-07 16:15:30 -0500358 start_found = 1;
Chris Masone2fa7222007-03-12 16:22:34 -0400359 last_block = key.objectid + key.offset;
Chris Masonfec577f2007-02-26 10:40:21 -0500360 path.slots[0]++;
361 }
362 // FIXME -ENOSPC
363check_pending:
364 /* we have to make sure we didn't find an extent that has already
365 * been allocated by the map tree or the original allocation
366 */
Chris Mason234b63a2007-03-13 10:46:10 -0400367 btrfs_release_path(root, &path);
Chris Masonfec577f2007-02-26 10:40:21 -0500368 BUG_ON(ins->objectid < search_start);
Chris Mason037e6392007-03-07 11:50:24 -0500369 for (test_block = ins->objectid;
370 test_block < ins->objectid + total_needed; test_block++) {
Chris Mason9f5fae22007-03-20 14:38:32 -0400371 if (radix_tree_lookup(&root->fs_info->pinned_radix,
372 test_block)) {
Chris Mason037e6392007-03-07 11:50:24 -0500373 search_start = test_block + 1;
Chris Masonfec577f2007-02-26 10:40:21 -0500374 goto check_failed;
375 }
376 }
Chris Mason9f5fae22007-03-20 14:38:32 -0400377 BUG_ON(root->fs_info->current_insert.offset);
378 root->fs_info->current_insert.offset = total_needed - num_blocks;
379 root->fs_info->current_insert.objectid = ins->objectid + num_blocks;
380 root->fs_info->current_insert.flags = 0;
381 root->fs_info->last_insert.objectid = ins->objectid;
Chris Mason037e6392007-03-07 11:50:24 -0500382 ins->offset = num_blocks;
Chris Masonfec577f2007-02-26 10:40:21 -0500383 return 0;
Chris Mason0f70abe2007-02-28 16:46:22 -0500384error:
Chris Mason234b63a2007-03-13 10:46:10 -0400385 btrfs_release_path(root, &path);
Chris Mason0f70abe2007-02-28 16:46:22 -0500386 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -0500387}
388
389/*
Chris Masonfec577f2007-02-26 10:40:21 -0500390 * finds a free extent and does all the dirty work required for allocation
391 * returns the key for the extent through ins, and a tree buffer for
392 * the first block of the extent through buf.
393 *
394 * returns 0 if everything worked, non-zero otherwise.
395 */
Chris Masone089f052007-03-16 16:20:31 -0400396static int alloc_extent(struct btrfs_trans_handle *trans, struct btrfs_root
397 *root, u64 num_blocks, u64 search_start, u64
398 search_end, u64 owner, struct btrfs_key *ins)
Chris Masonfec577f2007-02-26 10:40:21 -0500399{
400 int ret;
401 int pending_ret;
Chris Mason1261ec42007-03-20 20:35:03 -0400402 u64 super_blocks_used;
403 struct btrfs_fs_info *info = root->fs_info;
404 struct btrfs_root *extent_root = info->extent_root;
Chris Mason234b63a2007-03-13 10:46:10 -0400405 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -0500406
Chris Masoncf27e1e2007-03-13 09:49:06 -0400407 btrfs_set_extent_refs(&extent_item, 1);
408 btrfs_set_extent_owner(&extent_item, owner);
Chris Masonfec577f2007-02-26 10:40:21 -0500409
Chris Mason037e6392007-03-07 11:50:24 -0500410 if (root == extent_root) {
Chris Mason9f5fae22007-03-20 14:38:32 -0400411 BUG_ON(extent_root->fs_info->current_insert.offset == 0);
Chris Mason037e6392007-03-07 11:50:24 -0500412 BUG_ON(num_blocks != 1);
Chris Mason9f5fae22007-03-20 14:38:32 -0400413 BUG_ON(extent_root->fs_info->current_insert.flags ==
414 extent_root->fs_info->current_insert.offset);
Chris Mason037e6392007-03-07 11:50:24 -0500415 ins->offset = 1;
Chris Mason9f5fae22007-03-20 14:38:32 -0400416 ins->objectid = extent_root->fs_info->current_insert.objectid +
417 extent_root->fs_info->current_insert.flags++;
Chris Masonfec577f2007-02-26 10:40:21 -0500418 return 0;
419 }
Chris Masone089f052007-03-16 16:20:31 -0400420 ret = find_free_extent(trans, root, num_blocks, search_start,
Chris Mason037e6392007-03-07 11:50:24 -0500421 search_end, ins);
422 if (ret)
423 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -0500424
Chris Mason1261ec42007-03-20 20:35:03 -0400425 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
426 btrfs_set_super_blocks_used(info->disk_super, super_blocks_used +
427 num_blocks);
Chris Masone089f052007-03-16 16:20:31 -0400428 ret = btrfs_insert_item(trans, extent_root, ins, &extent_item,
429 sizeof(extent_item));
Chris Mason037e6392007-03-07 11:50:24 -0500430
Chris Masone089f052007-03-16 16:20:31 -0400431 finish_current_insert(trans, extent_root);
432 pending_ret = run_pending(trans, extent_root);
Chris Mason037e6392007-03-07 11:50:24 -0500433 if (ret)
434 return ret;
435 if (pending_ret)
436 return pending_ret;
437 return 0;
Chris Masonfec577f2007-02-26 10:40:21 -0500438}
439
440/*
441 * helper function to allocate a block for a given tree
442 * returns the tree buffer or NULL.
443 */
Chris Masone089f052007-03-16 16:20:31 -0400444struct btrfs_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
445 struct btrfs_root *root)
Chris Masonfec577f2007-02-26 10:40:21 -0500446{
Chris Masone2fa7222007-03-12 16:22:34 -0400447 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -0500448 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400449 struct btrfs_buffer *buf;
Chris Masonfec577f2007-02-26 10:40:21 -0500450
Chris Masone089f052007-03-16 16:20:31 -0400451 ret = alloc_extent(trans, root, 1, 0, (unsigned long)-1,
Chris Mason7518a232007-03-12 12:01:18 -0400452 btrfs_header_parentid(&root->node->node.header),
Chris Mason037e6392007-03-07 11:50:24 -0500453 &ins);
Chris Masonfec577f2007-02-26 10:40:21 -0500454 if (ret) {
455 BUG();
456 return NULL;
457 }
Chris Mason037e6392007-03-07 11:50:24 -0500458 buf = find_tree_block(root, ins.objectid);
Chris Masone089f052007-03-16 16:20:31 -0400459 dirty_tree_block(trans, root, buf);
Chris Masonfec577f2007-02-26 10:40:21 -0500460 return buf;
461}
Chris Masona28ec192007-03-06 20:08:01 -0500462
Chris Mason9aca1d52007-03-13 11:09:37 -0400463/*
464 * helper function for drop_snapshot, this walks down the tree dropping ref
465 * counts as it goes.
466 */
Chris Masone089f052007-03-16 16:20:31 -0400467static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
468 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -0500469{
Chris Mason234b63a2007-03-13 10:46:10 -0400470 struct btrfs_buffer *next;
471 struct btrfs_buffer *cur;
Chris Mason20524f02007-03-10 06:35:47 -0500472 u64 blocknr;
473 int ret;
474 u32 refs;
475
Chris Masone089f052007-03-16 16:20:31 -0400476 ret = lookup_block_ref(trans, root, path->nodes[*level]->blocknr,
477 &refs);
Chris Mason20524f02007-03-10 06:35:47 -0500478 BUG_ON(ret);
479 if (refs > 1)
480 goto out;
Chris Mason9aca1d52007-03-13 11:09:37 -0400481 /*
482 * walk down to the last node level and free all the leaves
483 */
Chris Mason20524f02007-03-10 06:35:47 -0500484 while(*level > 0) {
485 cur = path->nodes[*level];
Chris Mason7518a232007-03-12 12:01:18 -0400486 if (path->slots[*level] >=
487 btrfs_header_nritems(&cur->node.header))
Chris Mason20524f02007-03-10 06:35:47 -0500488 break;
Chris Mason1d4f8a02007-03-13 09:28:32 -0400489 blocknr = btrfs_node_blockptr(&cur->node, path->slots[*level]);
Chris Masone089f052007-03-16 16:20:31 -0400490 ret = lookup_block_ref(trans, root, blocknr, &refs);
Chris Mason20524f02007-03-10 06:35:47 -0500491 if (refs != 1 || *level == 1) {
492 path->slots[*level]++;
Chris Masone089f052007-03-16 16:20:31 -0400493 ret = btrfs_free_extent(trans, root, blocknr, 1, 1);
Chris Mason20524f02007-03-10 06:35:47 -0500494 BUG_ON(ret);
495 continue;
496 }
497 BUG_ON(ret);
498 next = read_tree_block(root, blocknr);
Chris Mason83e15a22007-03-12 09:03:27 -0400499 if (path->nodes[*level-1])
Chris Mason234b63a2007-03-13 10:46:10 -0400500 btrfs_block_release(root, path->nodes[*level-1]);
Chris Mason20524f02007-03-10 06:35:47 -0500501 path->nodes[*level-1] = next;
Chris Mason7518a232007-03-12 12:01:18 -0400502 *level = btrfs_header_level(&next->node.header);
Chris Mason20524f02007-03-10 06:35:47 -0500503 path->slots[*level] = 0;
504 }
505out:
Chris Masone089f052007-03-16 16:20:31 -0400506 ret = btrfs_free_extent(trans, root, path->nodes[*level]->blocknr, 1,
507 1);
Chris Mason234b63a2007-03-13 10:46:10 -0400508 btrfs_block_release(root, path->nodes[*level]);
Chris Mason20524f02007-03-10 06:35:47 -0500509 path->nodes[*level] = NULL;
510 *level += 1;
511 BUG_ON(ret);
512 return 0;
513}
514
Chris Mason9aca1d52007-03-13 11:09:37 -0400515/*
516 * helper for dropping snapshots. This walks back up the tree in the path
517 * to find the first node higher up where we haven't yet gone through
518 * all the slots
519 */
Chris Masone089f052007-03-16 16:20:31 -0400520static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root
521 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -0500522{
523 int i;
524 int slot;
525 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400526 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Mason20524f02007-03-10 06:35:47 -0500527 slot = path->slots[i];
Chris Mason7518a232007-03-12 12:01:18 -0400528 if (slot <
529 btrfs_header_nritems(&path->nodes[i]->node.header)- 1) {
Chris Mason20524f02007-03-10 06:35:47 -0500530 path->slots[i]++;
531 *level = i;
532 return 0;
533 } else {
Chris Masone089f052007-03-16 16:20:31 -0400534 ret = btrfs_free_extent(trans, root,
535 path->nodes[*level]->blocknr,
536 1, 1);
Chris Mason234b63a2007-03-13 10:46:10 -0400537 btrfs_block_release(root, path->nodes[*level]);
Chris Mason83e15a22007-03-12 09:03:27 -0400538 path->nodes[*level] = NULL;
Chris Mason20524f02007-03-10 06:35:47 -0500539 *level = i + 1;
540 BUG_ON(ret);
541 }
542 }
543 return 1;
544}
545
Chris Mason9aca1d52007-03-13 11:09:37 -0400546/*
547 * drop the reference count on the tree rooted at 'snap'. This traverses
548 * the tree freeing any blocks that have a ref count of zero after being
549 * decremented.
550 */
Chris Masone089f052007-03-16 16:20:31 -0400551int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
552 *root, struct btrfs_buffer *snap)
Chris Mason20524f02007-03-10 06:35:47 -0500553{
Chris Mason3768f362007-03-13 16:47:54 -0400554 int ret = 0;
Chris Mason9aca1d52007-03-13 11:09:37 -0400555 int wret;
Chris Mason20524f02007-03-10 06:35:47 -0500556 int level;
Chris Mason234b63a2007-03-13 10:46:10 -0400557 struct btrfs_path path;
Chris Mason20524f02007-03-10 06:35:47 -0500558 int i;
559 int orig_level;
560
Chris Mason234b63a2007-03-13 10:46:10 -0400561 btrfs_init_path(&path);
Chris Mason20524f02007-03-10 06:35:47 -0500562
Chris Mason7518a232007-03-12 12:01:18 -0400563 level = btrfs_header_level(&snap->node.header);
Chris Mason20524f02007-03-10 06:35:47 -0500564 orig_level = level;
565 path.nodes[level] = snap;
566 path.slots[level] = 0;
567 while(1) {
Chris Masone089f052007-03-16 16:20:31 -0400568 wret = walk_down_tree(trans, root, &path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -0400569 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -0500570 break;
Chris Mason9aca1d52007-03-13 11:09:37 -0400571 if (wret < 0)
572 ret = wret;
573
Chris Masone089f052007-03-16 16:20:31 -0400574 wret = walk_up_tree(trans, root, &path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -0400575 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -0500576 break;
Chris Mason9aca1d52007-03-13 11:09:37 -0400577 if (wret < 0)
578 ret = wret;
Chris Mason20524f02007-03-10 06:35:47 -0500579 }
Chris Mason83e15a22007-03-12 09:03:27 -0400580 for (i = 0; i <= orig_level; i++) {
581 if (path.nodes[i]) {
Chris Mason234b63a2007-03-13 10:46:10 -0400582 btrfs_block_release(root, path.nodes[i]);
Chris Mason83e15a22007-03-12 09:03:27 -0400583 }
Chris Mason20524f02007-03-10 06:35:47 -0500584 }
Chris Mason9aca1d52007-03-13 11:09:37 -0400585 return ret;
Chris Mason20524f02007-03-10 06:35:47 -0500586}