blob: e3af2c035687db23d44207c2b8086cda72ba51d2 [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);
Chris Masone20d96d2007-03-22 12:13:20 -040013static int del_pending_extents(struct btrfs_trans_handle *trans, struct
14 btrfs_root *extent_root);
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 Masone20d96d2007-03-22 12:13:20 -040023#define CTREE_EXTENT_PINNED 1
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);
Chris Masone20d96d2007-03-22 12:13:20 -040048 l = btrfs_buffer_leaf(path.nodes[0]);
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 Mason9f5fae22007-03-20 14:38:32 -040053 btrfs_release_path(root->fs_info->extent_root, &path);
54 finish_current_insert(trans, root->fs_info->extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -040055 del_pending_extents(trans, root->fs_info->extent_root);
Chris Mason02217ed2007-03-02 16:08:05 -050056 return 0;
57}
58
Chris Masone089f052007-03-16 16:20:31 -040059static int lookup_block_ref(struct btrfs_trans_handle *trans, struct btrfs_root
60 *root, u64 blocknr, u32 *refs)
Chris Masona28ec192007-03-06 20:08:01 -050061{
Chris Mason234b63a2007-03-13 10:46:10 -040062 struct btrfs_path path;
Chris Masona28ec192007-03-06 20:08:01 -050063 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -040064 struct btrfs_key key;
Chris Mason234b63a2007-03-13 10:46:10 -040065 struct btrfs_leaf *l;
66 struct btrfs_extent_item *item;
67 btrfs_init_path(&path);
Chris Masona28ec192007-03-06 20:08:01 -050068 key.objectid = blocknr;
Chris Masona28ec192007-03-06 20:08:01 -050069 key.offset = 1;
Chris Mason62e27492007-03-15 12:56:47 -040070 key.flags = 0;
71 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Mason9f5fae22007-03-20 14:38:32 -040072 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, &path,
73 0, 0);
Chris Masona28ec192007-03-06 20:08:01 -050074 if (ret != 0)
75 BUG();
Chris Masone20d96d2007-03-22 12:13:20 -040076 l = btrfs_buffer_leaf(path.nodes[0]);
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 Mason9f5fae22007-03-20 14:38:32 -040079 btrfs_release_path(root->fs_info->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,
Chris Masone20d96d2007-03-22 12:13:20 -040084 struct buffer_head *buf)
Chris Mason02217ed2007-03-02 16:08:05 -050085{
86 u64 blocknr;
Chris Masone20d96d2007-03-22 12:13:20 -040087 struct btrfs_node *buf_node;
Chris Mason02217ed2007-03-02 16:08:05 -050088 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 Masone20d96d2007-03-22 12:13:20 -040092 buf_node = btrfs_buffer_node(buf);
93 if (btrfs_is_leaf(buf_node))
Chris Masona28ec192007-03-06 20:08:01 -050094 return 0;
95
Chris Masone20d96d2007-03-22 12:13:20 -040096 for (i = 0; i < btrfs_header_nritems(&buf_node->header); i++) {
97 blocknr = btrfs_node_blockptr(buf_node, i);
Chris Masone089f052007-03-16 16:20:31 -040098 inc_block_ref(trans, root, blocknr);
Chris Mason02217ed2007-03-02 16:08:05 -050099 }
100 return 0;
101}
102
Chris Masone089f052007-03-16 16:20:31 -0400103int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, struct
104 btrfs_root *root)
Chris Masona28ec192007-03-06 20:08:01 -0500105{
Chris Masona28ec192007-03-06 20:08:01 -0500106 unsigned long gang[8];
Chris Mason88fd1462007-03-16 08:56:18 -0400107 u64 first = 0;
Chris Masona28ec192007-03-06 20:08:01 -0500108 int ret;
109 int i;
110
111 while(1) {
Chris Masone20d96d2007-03-22 12:13:20 -0400112 ret = radix_tree_gang_lookup_tag(&root->fs_info->pinned_radix,
Chris Mason9f5fae22007-03-20 14:38:32 -0400113 (void **)gang, 0,
Chris Masone20d96d2007-03-22 12:13:20 -0400114 ARRAY_SIZE(gang),
115 CTREE_EXTENT_PINNED);
Chris Masona28ec192007-03-06 20:08:01 -0500116 if (!ret)
117 break;
Chris Mason88fd1462007-03-16 08:56:18 -0400118 if (!first)
119 first = gang[0];
Chris Mason0579da42007-03-07 16:15:30 -0500120 for (i = 0; i < ret; i++) {
Chris Mason9f5fae22007-03-20 14:38:32 -0400121 radix_tree_delete(&root->fs_info->pinned_radix,
122 gang[i]);
Chris Mason0579da42007-03-07 16:15:30 -0500123 }
Chris Masona28ec192007-03-06 20:08:01 -0500124 }
Chris Mason9f5fae22007-03-20 14:38:32 -0400125 root->fs_info->last_insert.objectid = first;
126 root->fs_info->last_insert.offset = 0;
Chris Masona28ec192007-03-06 20:08:01 -0500127 return 0;
128}
129
Chris Masone089f052007-03-16 16:20:31 -0400130static int finish_current_insert(struct btrfs_trans_handle *trans, struct
131 btrfs_root *extent_root)
Chris Mason037e6392007-03-07 11:50:24 -0500132{
Chris Masone2fa7222007-03-12 16:22:34 -0400133 struct btrfs_key ins;
Chris Mason234b63a2007-03-13 10:46:10 -0400134 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -0500135 int i;
136 int ret;
Chris Mason1261ec42007-03-20 20:35:03 -0400137 u64 super_blocks_used;
138 struct btrfs_fs_info *info = extent_root->fs_info;
Chris Mason037e6392007-03-07 11:50:24 -0500139
Chris Masoncf27e1e2007-03-13 09:49:06 -0400140 btrfs_set_extent_refs(&extent_item, 1);
141 btrfs_set_extent_owner(&extent_item,
Chris Masone20d96d2007-03-22 12:13:20 -0400142 btrfs_header_parentid(btrfs_buffer_header(extent_root->node)));
Chris Mason037e6392007-03-07 11:50:24 -0500143 ins.offset = 1;
144 ins.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -0400145 btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
Chris Mason037e6392007-03-07 11:50:24 -0500146
Chris Mason9f5fae22007-03-20 14:38:32 -0400147 for (i = 0; i < extent_root->fs_info->current_insert.flags; i++) {
148 ins.objectid = extent_root->fs_info->current_insert.objectid +
149 i;
Chris Mason1261ec42007-03-20 20:35:03 -0400150 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
151 btrfs_set_super_blocks_used(info->disk_super,
152 super_blocks_used + 1);
Chris Masone089f052007-03-16 16:20:31 -0400153 ret = btrfs_insert_item(trans, extent_root, &ins, &extent_item,
154 sizeof(extent_item));
Chris Mason037e6392007-03-07 11:50:24 -0500155 BUG_ON(ret);
156 }
Chris Mason9f5fae22007-03-20 14:38:32 -0400157 extent_root->fs_info->current_insert.offset = 0;
Chris Mason037e6392007-03-07 11:50:24 -0500158 return 0;
159}
160
Chris Masone20d96d2007-03-22 12:13:20 -0400161static int pin_down_block(struct btrfs_root *root, u64 blocknr, int tag)
162{
163 int err;
164 err = radix_tree_insert(&root->fs_info->pinned_radix,
165 blocknr, (void *)blocknr);
166 BUG_ON(err);
167 if (err)
168 return err;
169 radix_tree_tag_set(&root->fs_info->pinned_radix, blocknr,
170 tag);
171 return 0;
172}
173
Chris Masona28ec192007-03-06 20:08:01 -0500174/*
175 * remove an extent from the root, returns 0 on success
176 */
Chris Masone089f052007-03-16 16:20:31 -0400177static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masone20d96d2007-03-22 12:13:20 -0400178 *root, u64 blocknr, u64 num_blocks)
Chris Masona28ec192007-03-06 20:08:01 -0500179{
Chris Mason234b63a2007-03-13 10:46:10 -0400180 struct btrfs_path path;
Chris Masone2fa7222007-03-12 16:22:34 -0400181 struct btrfs_key key;
Chris Mason1261ec42007-03-20 20:35:03 -0400182 struct btrfs_fs_info *info = root->fs_info;
183 struct btrfs_root *extent_root = info->extent_root;
Chris Masona28ec192007-03-06 20:08:01 -0500184 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400185 struct btrfs_extent_item *ei;
Chris Masone2fa7222007-03-12 16:22:34 -0400186 struct btrfs_key ins;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400187 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500188
Chris Masona28ec192007-03-06 20:08:01 -0500189 key.objectid = blocknr;
190 key.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -0400191 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masona28ec192007-03-06 20:08:01 -0500192 key.offset = num_blocks;
193
Chris Masone089f052007-03-16 16:20:31 -0400194 find_free_extent(trans, root, 0, 0, (u64)-1, &ins);
Chris Mason234b63a2007-03-13 10:46:10 -0400195 btrfs_init_path(&path);
Chris Masone089f052007-03-16 16:20:31 -0400196 ret = btrfs_search_slot(trans, extent_root, &key, &path, -1, 1);
Chris Masona28ec192007-03-06 20:08:01 -0500197 if (ret) {
Chris Mason2e635a22007-03-21 11:12:56 -0400198 printk("failed to find %Lu\n", key.objectid);
Chris Mason234b63a2007-03-13 10:46:10 -0400199 btrfs_print_tree(extent_root, extent_root->node);
Chris Mason2e635a22007-03-21 11:12:56 -0400200 printk("failed to find %Lu\n", key.objectid);
Chris Masona28ec192007-03-06 20:08:01 -0500201 BUG();
202 }
Chris Masone20d96d2007-03-22 12:13:20 -0400203 ei = btrfs_item_ptr(btrfs_buffer_leaf(path.nodes[0]), path.slots[0],
Chris Mason123abc82007-03-14 14:14:43 -0400204 struct btrfs_extent_item);
Chris Masona28ec192007-03-06 20:08:01 -0500205 BUG_ON(ei->refs == 0);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400206 refs = btrfs_extent_refs(ei) - 1;
207 btrfs_set_extent_refs(ei, refs);
208 if (refs == 0) {
Chris Mason1261ec42007-03-20 20:35:03 -0400209 u64 super_blocks_used;
Chris Mason1261ec42007-03-20 20:35:03 -0400210 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
211 btrfs_set_super_blocks_used(info->disk_super,
212 super_blocks_used - num_blocks);
Chris Masone089f052007-03-16 16:20:31 -0400213 ret = btrfs_del_item(trans, extent_root, &path);
Chris Masone20d96d2007-03-22 12:13:20 -0400214 if (extent_root->fs_info->last_insert.objectid >
Chris Mason9f5fae22007-03-20 14:38:32 -0400215 blocknr)
216 extent_root->fs_info->last_insert.objectid = blocknr;
Chris Masona28ec192007-03-06 20:08:01 -0500217 if (ret)
218 BUG();
219 }
Chris Mason234b63a2007-03-13 10:46:10 -0400220 btrfs_release_path(extent_root, &path);
Chris Masone089f052007-03-16 16:20:31 -0400221 finish_current_insert(trans, extent_root);
Chris Masona28ec192007-03-06 20:08:01 -0500222 return ret;
223}
224
225/*
Chris Masonfec577f2007-02-26 10:40:21 -0500226 * find all the blocks marked as pending in the radix tree and remove
227 * them from the extent map
228 */
Chris Masone089f052007-03-16 16:20:31 -0400229static int del_pending_extents(struct btrfs_trans_handle *trans, struct
230 btrfs_root *extent_root)
Chris Masonfec577f2007-02-26 10:40:21 -0500231{
232 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -0400233 int wret;
234 int err = 0;
235 unsigned long gang[4];
Chris Masonfec577f2007-02-26 10:40:21 -0500236 int i;
Chris Masone20d96d2007-03-22 12:13:20 -0400237 struct radix_tree_root *radix = &extent_root->fs_info->pinned_radix;
Chris Masonfec577f2007-02-26 10:40:21 -0500238
239 while(1) {
Chris Mason9f5fae22007-03-20 14:38:32 -0400240 ret = radix_tree_gang_lookup_tag(
Chris Masone20d96d2007-03-22 12:13:20 -0400241 &extent_root->fs_info->pinned_radix,
Chris Mason9f5fae22007-03-20 14:38:32 -0400242 (void **)gang, 0,
243 ARRAY_SIZE(gang),
244 CTREE_EXTENT_PENDING_DEL);
Chris Masonfec577f2007-02-26 10:40:21 -0500245 if (!ret)
246 break;
247 for (i = 0; i < ret; i++) {
Chris Masone20d96d2007-03-22 12:13:20 -0400248 radix_tree_tag_set(radix, gang[i], CTREE_EXTENT_PINNED);
249 radix_tree_tag_clear(radix, gang[i],
Chris Mason9f5fae22007-03-20 14:38:32 -0400250 CTREE_EXTENT_PENDING_DEL);
Chris Masone20d96d2007-03-22 12:13:20 -0400251 wret = __free_extent(trans, extent_root, gang[i], 1);
252 if (wret)
253 err = wret;
Chris Masonfec577f2007-02-26 10:40:21 -0500254 }
255 }
Chris Masone20d96d2007-03-22 12:13:20 -0400256 return err;
Chris Masonfec577f2007-02-26 10:40:21 -0500257}
258
259/*
260 * remove an extent from the root, returns 0 on success
261 */
Chris Masone089f052007-03-16 16:20:31 -0400262int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
263 *root, u64 blocknr, u64 num_blocks, int pin)
Chris Masonfec577f2007-02-26 10:40:21 -0500264{
Chris Mason9f5fae22007-03-20 14:38:32 -0400265 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Masone20d96d2007-03-22 12:13:20 -0400266 struct buffer_head *t;
Chris Masonfec577f2007-02-26 10:40:21 -0500267 int pending_ret;
268 int ret;
Chris Masona28ec192007-03-06 20:08:01 -0500269
270 if (root == extent_root) {
271 t = find_tree_block(root, blocknr);
Chris Masone20d96d2007-03-22 12:13:20 -0400272 pin_down_block(root, blocknr, CTREE_EXTENT_PENDING_DEL);
Chris Masona28ec192007-03-06 20:08:01 -0500273 return 0;
274 }
Chris Masone20d96d2007-03-22 12:13:20 -0400275 if (pin) {
276 ret = pin_down_block(root, blocknr, CTREE_EXTENT_PINNED);
277 BUG_ON(ret);
278 }
279 ret = __free_extent(trans, root, blocknr, num_blocks);
280 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
Chris Masonfec577f2007-02-26 10:40:21 -0500281 return ret ? ret : pending_ret;
282}
283
284/*
285 * walks the btree of allocated extents and find a hole of a given size.
286 * The key ins is changed to record the hole:
287 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -0400288 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -0500289 * ins->offset == number of blocks
290 * Any available blocks before search_start are skipped.
291 */
Chris Masone089f052007-03-16 16:20:31 -0400292static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
293 *orig_root, u64 num_blocks, u64 search_start, u64
294 search_end, struct btrfs_key *ins)
Chris Masonfec577f2007-02-26 10:40:21 -0500295{
Chris Mason234b63a2007-03-13 10:46:10 -0400296 struct btrfs_path path;
Chris Masone2fa7222007-03-12 16:22:34 -0400297 struct btrfs_key key;
Chris Masonfec577f2007-02-26 10:40:21 -0500298 int ret;
299 u64 hole_size = 0;
300 int slot = 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400301 u64 last_block = 0;
Chris Mason037e6392007-03-07 11:50:24 -0500302 u64 test_block;
Chris Masonfec577f2007-02-26 10:40:21 -0500303 int start_found;
Chris Mason234b63a2007-03-13 10:46:10 -0400304 struct btrfs_leaf *l;
Chris Mason9f5fae22007-03-20 14:38:32 -0400305 struct btrfs_root * root = orig_root->fs_info->extent_root;
Chris Mason0579da42007-03-07 16:15:30 -0500306 int total_needed = num_blocks;
Chris Masone20d96d2007-03-22 12:13:20 -0400307 int level;
Chris Masonfec577f2007-02-26 10:40:21 -0500308
Chris Masone20d96d2007-03-22 12:13:20 -0400309 level = btrfs_header_level(btrfs_buffer_header(root->node));
310 total_needed += (level + 1) * 3;
Chris Mason9f5fae22007-03-20 14:38:32 -0400311 if (root->fs_info->last_insert.objectid > search_start)
312 search_start = root->fs_info->last_insert.objectid;
Chris Mason62e27492007-03-15 12:56:47 -0400313
314 ins->flags = 0;
315 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
316
Chris Masonfec577f2007-02-26 10:40:21 -0500317check_failed:
Chris Mason234b63a2007-03-13 10:46:10 -0400318 btrfs_init_path(&path);
Chris Masonfec577f2007-02-26 10:40:21 -0500319 ins->objectid = search_start;
320 ins->offset = 0;
Chris Masonfec577f2007-02-26 10:40:21 -0500321 start_found = 0;
Chris Masone089f052007-03-16 16:20:31 -0400322 ret = btrfs_search_slot(trans, root, ins, &path, 0, 0);
Chris Mason0f70abe2007-02-28 16:46:22 -0500323 if (ret < 0)
324 goto error;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500325
Chris Mason0579da42007-03-07 16:15:30 -0500326 if (path.slots[0] > 0)
327 path.slots[0]--;
328
Chris Masonfec577f2007-02-26 10:40:21 -0500329 while (1) {
Chris Masone20d96d2007-03-22 12:13:20 -0400330 l = btrfs_buffer_leaf(path.nodes[0]);
Chris Masonfec577f2007-02-26 10:40:21 -0500331 slot = path.slots[0];
Chris Mason7518a232007-03-12 12:01:18 -0400332 if (slot >= btrfs_header_nritems(&l->header)) {
Chris Mason234b63a2007-03-13 10:46:10 -0400333 ret = btrfs_next_leaf(root, &path);
Chris Masonfec577f2007-02-26 10:40:21 -0500334 if (ret == 0)
335 continue;
Chris Mason0f70abe2007-02-28 16:46:22 -0500336 if (ret < 0)
337 goto error;
Chris Masonfec577f2007-02-26 10:40:21 -0500338 if (!start_found) {
339 ins->objectid = search_start;
Chris Mason037e6392007-03-07 11:50:24 -0500340 ins->offset = (u64)-1;
Chris Masonfec577f2007-02-26 10:40:21 -0500341 start_found = 1;
342 goto check_pending;
343 }
344 ins->objectid = last_block > search_start ?
345 last_block : search_start;
Chris Mason037e6392007-03-07 11:50:24 -0500346 ins->offset = (u64)-1;
Chris Masonfec577f2007-02-26 10:40:21 -0500347 goto check_pending;
348 }
Chris Masone2fa7222007-03-12 16:22:34 -0400349 btrfs_disk_key_to_cpu(&key, &l->items[slot].key);
350 if (key.objectid >= search_start) {
Chris Masonfec577f2007-02-26 10:40:21 -0500351 if (start_found) {
Chris Mason0579da42007-03-07 16:15:30 -0500352 if (last_block < search_start)
353 last_block = search_start;
Chris Masone2fa7222007-03-12 16:22:34 -0400354 hole_size = key.objectid - last_block;
Chris Mason037e6392007-03-07 11:50:24 -0500355 if (hole_size > total_needed) {
Chris Masonfec577f2007-02-26 10:40:21 -0500356 ins->objectid = last_block;
Chris Mason037e6392007-03-07 11:50:24 -0500357 ins->offset = hole_size;
Chris Masonfec577f2007-02-26 10:40:21 -0500358 goto check_pending;
359 }
Chris Mason0579da42007-03-07 16:15:30 -0500360 }
Chris Masonfec577f2007-02-26 10:40:21 -0500361 }
Chris Mason0579da42007-03-07 16:15:30 -0500362 start_found = 1;
Chris Masone2fa7222007-03-12 16:22:34 -0400363 last_block = key.objectid + key.offset;
Chris Masonfec577f2007-02-26 10:40:21 -0500364 path.slots[0]++;
365 }
366 // FIXME -ENOSPC
367check_pending:
368 /* we have to make sure we didn't find an extent that has already
369 * been allocated by the map tree or the original allocation
370 */
Chris Mason234b63a2007-03-13 10:46:10 -0400371 btrfs_release_path(root, &path);
Chris Masonfec577f2007-02-26 10:40:21 -0500372 BUG_ON(ins->objectid < search_start);
Chris Mason037e6392007-03-07 11:50:24 -0500373 for (test_block = ins->objectid;
374 test_block < ins->objectid + total_needed; test_block++) {
Chris Mason9f5fae22007-03-20 14:38:32 -0400375 if (radix_tree_lookup(&root->fs_info->pinned_radix,
376 test_block)) {
Chris Mason037e6392007-03-07 11:50:24 -0500377 search_start = test_block + 1;
Chris Masonfec577f2007-02-26 10:40:21 -0500378 goto check_failed;
379 }
380 }
Chris Mason9f5fae22007-03-20 14:38:32 -0400381 BUG_ON(root->fs_info->current_insert.offset);
382 root->fs_info->current_insert.offset = total_needed - num_blocks;
383 root->fs_info->current_insert.objectid = ins->objectid + num_blocks;
384 root->fs_info->current_insert.flags = 0;
385 root->fs_info->last_insert.objectid = ins->objectid;
Chris Mason037e6392007-03-07 11:50:24 -0500386 ins->offset = num_blocks;
Chris Masonfec577f2007-02-26 10:40:21 -0500387 return 0;
Chris Mason0f70abe2007-02-28 16:46:22 -0500388error:
Chris Mason234b63a2007-03-13 10:46:10 -0400389 btrfs_release_path(root, &path);
Chris Mason0f70abe2007-02-28 16:46:22 -0500390 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -0500391}
392
393/*
Chris Masonfec577f2007-02-26 10:40:21 -0500394 * finds a free extent and does all the dirty work required for allocation
395 * returns the key for the extent through ins, and a tree buffer for
396 * the first block of the extent through buf.
397 *
398 * returns 0 if everything worked, non-zero otherwise.
399 */
Chris Masone089f052007-03-16 16:20:31 -0400400static int alloc_extent(struct btrfs_trans_handle *trans, struct btrfs_root
401 *root, u64 num_blocks, u64 search_start, u64
402 search_end, u64 owner, struct btrfs_key *ins)
Chris Masonfec577f2007-02-26 10:40:21 -0500403{
404 int ret;
405 int pending_ret;
Chris Mason1261ec42007-03-20 20:35:03 -0400406 u64 super_blocks_used;
407 struct btrfs_fs_info *info = root->fs_info;
408 struct btrfs_root *extent_root = info->extent_root;
Chris Mason234b63a2007-03-13 10:46:10 -0400409 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -0500410
Chris Masoncf27e1e2007-03-13 09:49:06 -0400411 btrfs_set_extent_refs(&extent_item, 1);
412 btrfs_set_extent_owner(&extent_item, owner);
Chris Masonfec577f2007-02-26 10:40:21 -0500413
Chris Mason037e6392007-03-07 11:50:24 -0500414 if (root == extent_root) {
Chris Mason9f5fae22007-03-20 14:38:32 -0400415 BUG_ON(extent_root->fs_info->current_insert.offset == 0);
Chris Mason037e6392007-03-07 11:50:24 -0500416 BUG_ON(num_blocks != 1);
Chris Mason9f5fae22007-03-20 14:38:32 -0400417 BUG_ON(extent_root->fs_info->current_insert.flags ==
418 extent_root->fs_info->current_insert.offset);
Chris Mason037e6392007-03-07 11:50:24 -0500419 ins->offset = 1;
Chris Mason9f5fae22007-03-20 14:38:32 -0400420 ins->objectid = extent_root->fs_info->current_insert.objectid +
421 extent_root->fs_info->current_insert.flags++;
Chris Masonfec577f2007-02-26 10:40:21 -0500422 return 0;
423 }
Chris Masone089f052007-03-16 16:20:31 -0400424 ret = find_free_extent(trans, root, num_blocks, search_start,
Chris Mason037e6392007-03-07 11:50:24 -0500425 search_end, ins);
426 if (ret)
427 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -0500428
Chris Mason1261ec42007-03-20 20:35:03 -0400429 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
430 btrfs_set_super_blocks_used(info->disk_super, super_blocks_used +
431 num_blocks);
Chris Masone089f052007-03-16 16:20:31 -0400432 ret = btrfs_insert_item(trans, extent_root, ins, &extent_item,
433 sizeof(extent_item));
Chris Mason037e6392007-03-07 11:50:24 -0500434
Chris Masone089f052007-03-16 16:20:31 -0400435 finish_current_insert(trans, extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -0400436 pending_ret = del_pending_extents(trans, extent_root);
Chris Mason037e6392007-03-07 11:50:24 -0500437 if (ret)
438 return ret;
439 if (pending_ret)
440 return pending_ret;
441 return 0;
Chris Masonfec577f2007-02-26 10:40:21 -0500442}
443
444/*
445 * helper function to allocate a block for a given tree
446 * returns the tree buffer or NULL.
447 */
Chris Masone20d96d2007-03-22 12:13:20 -0400448struct buffer_head *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Chris Masone089f052007-03-16 16:20:31 -0400449 struct btrfs_root *root)
Chris Masonfec577f2007-02-26 10:40:21 -0500450{
Chris Masone2fa7222007-03-12 16:22:34 -0400451 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -0500452 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -0400453 struct buffer_head *buf;
Chris Masonfec577f2007-02-26 10:40:21 -0500454
Chris Masone089f052007-03-16 16:20:31 -0400455 ret = alloc_extent(trans, root, 1, 0, (unsigned long)-1,
Chris Masone20d96d2007-03-22 12:13:20 -0400456 btrfs_header_parentid(btrfs_buffer_header(root->node)), &ins);
Chris Masonfec577f2007-02-26 10:40:21 -0500457 if (ret) {
458 BUG();
459 return NULL;
460 }
Chris Mason037e6392007-03-07 11:50:24 -0500461 buf = find_tree_block(root, ins.objectid);
Chris Masone089f052007-03-16 16:20:31 -0400462 dirty_tree_block(trans, root, buf);
Chris Masonfec577f2007-02-26 10:40:21 -0500463 return buf;
464}
Chris Masona28ec192007-03-06 20:08:01 -0500465
Chris Mason9aca1d52007-03-13 11:09:37 -0400466/*
467 * helper function for drop_snapshot, this walks down the tree dropping ref
468 * counts as it goes.
469 */
Chris Masone089f052007-03-16 16:20:31 -0400470static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
471 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -0500472{
Chris Masone20d96d2007-03-22 12:13:20 -0400473 struct buffer_head *next;
474 struct buffer_head *cur;
Chris Mason20524f02007-03-10 06:35:47 -0500475 u64 blocknr;
476 int ret;
477 u32 refs;
478
Chris Masone20d96d2007-03-22 12:13:20 -0400479 ret = lookup_block_ref(trans, root, path->nodes[*level]->b_blocknr,
Chris Masone089f052007-03-16 16:20:31 -0400480 &refs);
Chris Mason20524f02007-03-10 06:35:47 -0500481 BUG_ON(ret);
482 if (refs > 1)
483 goto out;
Chris Mason9aca1d52007-03-13 11:09:37 -0400484 /*
485 * walk down to the last node level and free all the leaves
486 */
Chris Mason20524f02007-03-10 06:35:47 -0500487 while(*level > 0) {
488 cur = path->nodes[*level];
Chris Mason7518a232007-03-12 12:01:18 -0400489 if (path->slots[*level] >=
Chris Masone20d96d2007-03-22 12:13:20 -0400490 btrfs_header_nritems(btrfs_buffer_header(cur)))
Chris Mason20524f02007-03-10 06:35:47 -0500491 break;
Chris Masone20d96d2007-03-22 12:13:20 -0400492 blocknr = btrfs_node_blockptr(btrfs_buffer_node(cur),
493 path->slots[*level]);
Chris Masone089f052007-03-16 16:20:31 -0400494 ret = lookup_block_ref(trans, root, blocknr, &refs);
Chris Mason20524f02007-03-10 06:35:47 -0500495 if (refs != 1 || *level == 1) {
496 path->slots[*level]++;
Chris Masone089f052007-03-16 16:20:31 -0400497 ret = btrfs_free_extent(trans, root, blocknr, 1, 1);
Chris Mason20524f02007-03-10 06:35:47 -0500498 BUG_ON(ret);
499 continue;
500 }
501 BUG_ON(ret);
502 next = read_tree_block(root, blocknr);
Chris Mason83e15a22007-03-12 09:03:27 -0400503 if (path->nodes[*level-1])
Chris Mason234b63a2007-03-13 10:46:10 -0400504 btrfs_block_release(root, path->nodes[*level-1]);
Chris Mason20524f02007-03-10 06:35:47 -0500505 path->nodes[*level-1] = next;
Chris Masone20d96d2007-03-22 12:13:20 -0400506 *level = btrfs_header_level(btrfs_buffer_header(next));
Chris Mason20524f02007-03-10 06:35:47 -0500507 path->slots[*level] = 0;
508 }
509out:
Chris Masone20d96d2007-03-22 12:13:20 -0400510 ret = btrfs_free_extent(trans, root, path->nodes[*level]->b_blocknr,
511 1, 1);
Chris Mason234b63a2007-03-13 10:46:10 -0400512 btrfs_block_release(root, path->nodes[*level]);
Chris Mason20524f02007-03-10 06:35:47 -0500513 path->nodes[*level] = NULL;
514 *level += 1;
515 BUG_ON(ret);
516 return 0;
517}
518
Chris Mason9aca1d52007-03-13 11:09:37 -0400519/*
520 * helper for dropping snapshots. This walks back up the tree in the path
521 * to find the first node higher up where we haven't yet gone through
522 * all the slots
523 */
Chris Masone089f052007-03-16 16:20:31 -0400524static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root
525 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -0500526{
527 int i;
528 int slot;
529 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400530 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Mason20524f02007-03-10 06:35:47 -0500531 slot = path->slots[i];
Chris Masone20d96d2007-03-22 12:13:20 -0400532 if (slot < btrfs_header_nritems(
533 btrfs_buffer_header(path->nodes[i])) - 1) {
Chris Mason20524f02007-03-10 06:35:47 -0500534 path->slots[i]++;
535 *level = i;
536 return 0;
537 } else {
Chris Masone089f052007-03-16 16:20:31 -0400538 ret = btrfs_free_extent(trans, root,
Chris Masone20d96d2007-03-22 12:13:20 -0400539 path->nodes[*level]->b_blocknr,
Chris Masone089f052007-03-16 16:20:31 -0400540 1, 1);
Chris Mason234b63a2007-03-13 10:46:10 -0400541 btrfs_block_release(root, path->nodes[*level]);
Chris Mason83e15a22007-03-12 09:03:27 -0400542 path->nodes[*level] = NULL;
Chris Mason20524f02007-03-10 06:35:47 -0500543 *level = i + 1;
544 BUG_ON(ret);
545 }
546 }
547 return 1;
548}
549
Chris Mason9aca1d52007-03-13 11:09:37 -0400550/*
551 * drop the reference count on the tree rooted at 'snap'. This traverses
552 * the tree freeing any blocks that have a ref count of zero after being
553 * decremented.
554 */
Chris Masone089f052007-03-16 16:20:31 -0400555int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masone20d96d2007-03-22 12:13:20 -0400556 *root, struct buffer_head *snap)
Chris Mason20524f02007-03-10 06:35:47 -0500557{
Chris Mason3768f362007-03-13 16:47:54 -0400558 int ret = 0;
Chris Mason9aca1d52007-03-13 11:09:37 -0400559 int wret;
Chris Mason20524f02007-03-10 06:35:47 -0500560 int level;
Chris Mason234b63a2007-03-13 10:46:10 -0400561 struct btrfs_path path;
Chris Mason20524f02007-03-10 06:35:47 -0500562 int i;
563 int orig_level;
564
Chris Mason234b63a2007-03-13 10:46:10 -0400565 btrfs_init_path(&path);
Chris Mason20524f02007-03-10 06:35:47 -0500566
Chris Masone20d96d2007-03-22 12:13:20 -0400567 level = btrfs_header_level(btrfs_buffer_header(snap));
Chris Mason20524f02007-03-10 06:35:47 -0500568 orig_level = level;
569 path.nodes[level] = snap;
570 path.slots[level] = 0;
571 while(1) {
Chris Masone089f052007-03-16 16:20:31 -0400572 wret = walk_down_tree(trans, root, &path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -0400573 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -0500574 break;
Chris Mason9aca1d52007-03-13 11:09:37 -0400575 if (wret < 0)
576 ret = wret;
577
Chris Masone089f052007-03-16 16:20:31 -0400578 wret = walk_up_tree(trans, root, &path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -0400579 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -0500580 break;
Chris Mason9aca1d52007-03-13 11:09:37 -0400581 if (wret < 0)
582 ret = wret;
Chris Mason20524f02007-03-10 06:35:47 -0500583 }
Chris Mason83e15a22007-03-12 09:03:27 -0400584 for (i = 0; i <= orig_level; i++) {
585 if (path.nodes[i]) {
Chris Mason234b63a2007-03-13 10:46:10 -0400586 btrfs_block_release(root, path.nodes[i]);
Chris Mason83e15a22007-03-12 09:03:27 -0400587 }
Chris Mason20524f02007-03-10 06:35:47 -0500588 }
Chris Mason9aca1d52007-03-13 11:09:37 -0400589 return ret;
Chris Mason20524f02007-03-10 06:35:47 -0500590}