blob: c86f0e6152f2799a727940f2abe9f26e0eb00046 [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 Masond5719762007-03-23 10:01:08 -040052 mark_buffer_dirty(path.nodes[0]);
Chris Masona28ec192007-03-06 20:08:01 -050053
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);
Chris Masone20d96d2007-03-22 12:13:20 -040056 del_pending_extents(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();
Chris Masone20d96d2007-03-22 12:13:20 -040077 l = btrfs_buffer_leaf(path.nodes[0]);
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,
Chris Masone20d96d2007-03-22 12:13:20 -040085 struct buffer_head *buf)
Chris Mason02217ed2007-03-02 16:08:05 -050086{
87 u64 blocknr;
Chris Masone20d96d2007-03-22 12:13:20 -040088 struct btrfs_node *buf_node;
Chris Mason02217ed2007-03-02 16:08:05 -050089 int i;
Chris Masona28ec192007-03-06 20:08:01 -050090
Chris Mason3768f362007-03-13 16:47:54 -040091 if (!root->ref_cows)
Chris Masona28ec192007-03-06 20:08:01 -050092 return 0;
Chris Masone20d96d2007-03-22 12:13:20 -040093 buf_node = btrfs_buffer_node(buf);
94 if (btrfs_is_leaf(buf_node))
Chris Masona28ec192007-03-06 20:08:01 -050095 return 0;
96
Chris Masone20d96d2007-03-22 12:13:20 -040097 for (i = 0; i < btrfs_header_nritems(&buf_node->header); i++) {
98 blocknr = btrfs_node_blockptr(buf_node, i);
Chris Masone089f052007-03-16 16:20:31 -040099 inc_block_ref(trans, root, blocknr);
Chris Mason02217ed2007-03-02 16:08:05 -0500100 }
101 return 0;
102}
103
Chris Masone089f052007-03-16 16:20:31 -0400104int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, struct
105 btrfs_root *root)
Chris Masona28ec192007-03-06 20:08:01 -0500106{
Chris Masond5719762007-03-23 10:01:08 -0400107 struct buffer_head *gang[8];
Chris Mason88fd1462007-03-16 08:56:18 -0400108 u64 first = 0;
Chris Masona28ec192007-03-06 20:08:01 -0500109 int ret;
110 int i;
111
112 while(1) {
Chris Masone20d96d2007-03-22 12:13:20 -0400113 ret = radix_tree_gang_lookup_tag(&root->fs_info->pinned_radix,
Chris Mason9f5fae22007-03-20 14:38:32 -0400114 (void **)gang, 0,
Chris Masone20d96d2007-03-22 12:13:20 -0400115 ARRAY_SIZE(gang),
116 CTREE_EXTENT_PINNED);
Chris Masona28ec192007-03-06 20:08:01 -0500117 if (!ret)
118 break;
Chris Mason88fd1462007-03-16 08:56:18 -0400119 if (!first)
Chris Masond5719762007-03-23 10:01:08 -0400120 first = gang[0]->b_blocknr;
Chris Mason0579da42007-03-07 16:15:30 -0500121 for (i = 0; i < ret; i++) {
Chris Mason9f5fae22007-03-20 14:38:32 -0400122 radix_tree_delete(&root->fs_info->pinned_radix,
Chris Masond5719762007-03-23 10:01:08 -0400123 gang[i]->b_blocknr);
124 brelse(gang[i]);
Chris Mason0579da42007-03-07 16:15:30 -0500125 }
Chris Masona28ec192007-03-06 20:08:01 -0500126 }
Chris Masond5719762007-03-23 10:01:08 -0400127 if (root->fs_info->last_insert.objectid > first)
128 root->fs_info->last_insert.objectid = first;
Chris Mason9f5fae22007-03-20 14:38:32 -0400129 root->fs_info->last_insert.offset = 0;
Chris Masona28ec192007-03-06 20:08:01 -0500130 return 0;
131}
132
Chris Masone089f052007-03-16 16:20:31 -0400133static int finish_current_insert(struct btrfs_trans_handle *trans, struct
134 btrfs_root *extent_root)
Chris Mason037e6392007-03-07 11:50:24 -0500135{
Chris Masone2fa7222007-03-12 16:22:34 -0400136 struct btrfs_key ins;
Chris Mason234b63a2007-03-13 10:46:10 -0400137 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -0500138 int i;
139 int ret;
Chris Mason1261ec42007-03-20 20:35:03 -0400140 u64 super_blocks_used;
141 struct btrfs_fs_info *info = extent_root->fs_info;
Chris Mason037e6392007-03-07 11:50:24 -0500142
Chris Masoncf27e1e2007-03-13 09:49:06 -0400143 btrfs_set_extent_refs(&extent_item, 1);
144 btrfs_set_extent_owner(&extent_item,
Chris Masone20d96d2007-03-22 12:13:20 -0400145 btrfs_header_parentid(btrfs_buffer_header(extent_root->node)));
Chris Mason037e6392007-03-07 11:50:24 -0500146 ins.offset = 1;
147 ins.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -0400148 btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
Chris Mason037e6392007-03-07 11:50:24 -0500149
Chris Mason9f5fae22007-03-20 14:38:32 -0400150 for (i = 0; i < extent_root->fs_info->current_insert.flags; i++) {
151 ins.objectid = extent_root->fs_info->current_insert.objectid +
152 i;
Chris Mason1261ec42007-03-20 20:35:03 -0400153 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
154 btrfs_set_super_blocks_used(info->disk_super,
155 super_blocks_used + 1);
Chris Masone089f052007-03-16 16:20:31 -0400156 ret = btrfs_insert_item(trans, extent_root, &ins, &extent_item,
157 sizeof(extent_item));
Chris Mason037e6392007-03-07 11:50:24 -0500158 BUG_ON(ret);
159 }
Chris Mason9f5fae22007-03-20 14:38:32 -0400160 extent_root->fs_info->current_insert.offset = 0;
Chris Mason037e6392007-03-07 11:50:24 -0500161 return 0;
162}
163
Chris Masone20d96d2007-03-22 12:13:20 -0400164static int pin_down_block(struct btrfs_root *root, u64 blocknr, int tag)
165{
166 int err;
Chris Masond5719762007-03-23 10:01:08 -0400167 struct buffer_head *bh = sb_getblk(root->fs_info->sb, blocknr);
168 BUG_ON(!bh);
Chris Masone20d96d2007-03-22 12:13:20 -0400169 err = radix_tree_insert(&root->fs_info->pinned_radix,
Chris Masond5719762007-03-23 10:01:08 -0400170 blocknr, bh);
Chris Masone20d96d2007-03-22 12:13:20 -0400171 BUG_ON(err);
172 if (err)
173 return err;
174 radix_tree_tag_set(&root->fs_info->pinned_radix, blocknr,
175 tag);
176 return 0;
177}
178
Chris Masona28ec192007-03-06 20:08:01 -0500179/*
180 * remove an extent from the root, returns 0 on success
181 */
Chris Masone089f052007-03-16 16:20:31 -0400182static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masone20d96d2007-03-22 12:13:20 -0400183 *root, u64 blocknr, u64 num_blocks)
Chris Masona28ec192007-03-06 20:08:01 -0500184{
Chris Mason234b63a2007-03-13 10:46:10 -0400185 struct btrfs_path path;
Chris Masone2fa7222007-03-12 16:22:34 -0400186 struct btrfs_key key;
Chris Mason1261ec42007-03-20 20:35:03 -0400187 struct btrfs_fs_info *info = root->fs_info;
188 struct btrfs_root *extent_root = info->extent_root;
Chris Masona28ec192007-03-06 20:08:01 -0500189 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400190 struct btrfs_extent_item *ei;
Chris Masone2fa7222007-03-12 16:22:34 -0400191 struct btrfs_key ins;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400192 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500193
Chris Masona28ec192007-03-06 20:08:01 -0500194 key.objectid = blocknr;
195 key.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -0400196 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masona28ec192007-03-06 20:08:01 -0500197 key.offset = num_blocks;
198
Chris Masone089f052007-03-16 16:20:31 -0400199 find_free_extent(trans, root, 0, 0, (u64)-1, &ins);
Chris Mason234b63a2007-03-13 10:46:10 -0400200 btrfs_init_path(&path);
Chris Masone089f052007-03-16 16:20:31 -0400201 ret = btrfs_search_slot(trans, extent_root, &key, &path, -1, 1);
Chris Masona28ec192007-03-06 20:08:01 -0500202 if (ret) {
Chris Mason2e635a22007-03-21 11:12:56 -0400203 printk("failed to find %Lu\n", key.objectid);
Chris Mason234b63a2007-03-13 10:46:10 -0400204 btrfs_print_tree(extent_root, extent_root->node);
Chris Mason2e635a22007-03-21 11:12:56 -0400205 printk("failed to find %Lu\n", key.objectid);
Chris Masona28ec192007-03-06 20:08:01 -0500206 BUG();
207 }
Chris Masone20d96d2007-03-22 12:13:20 -0400208 ei = btrfs_item_ptr(btrfs_buffer_leaf(path.nodes[0]), path.slots[0],
Chris Mason123abc82007-03-14 14:14:43 -0400209 struct btrfs_extent_item);
Chris Masona28ec192007-03-06 20:08:01 -0500210 BUG_ON(ei->refs == 0);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400211 refs = btrfs_extent_refs(ei) - 1;
212 btrfs_set_extent_refs(ei, refs);
213 if (refs == 0) {
Chris Mason1261ec42007-03-20 20:35:03 -0400214 u64 super_blocks_used;
Chris Mason1261ec42007-03-20 20:35:03 -0400215 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
216 btrfs_set_super_blocks_used(info->disk_super,
217 super_blocks_used - num_blocks);
Chris Masone089f052007-03-16 16:20:31 -0400218 ret = btrfs_del_item(trans, extent_root, &path);
Chris Masone20d96d2007-03-22 12:13:20 -0400219 if (extent_root->fs_info->last_insert.objectid >
Chris Mason9f5fae22007-03-20 14:38:32 -0400220 blocknr)
221 extent_root->fs_info->last_insert.objectid = blocknr;
Chris Masona28ec192007-03-06 20:08:01 -0500222 if (ret)
223 BUG();
224 }
Chris Masond5719762007-03-23 10:01:08 -0400225 mark_buffer_dirty(path.nodes[0]);
Chris Mason234b63a2007-03-13 10:46:10 -0400226 btrfs_release_path(extent_root, &path);
Chris Masone089f052007-03-16 16:20:31 -0400227 finish_current_insert(trans, extent_root);
Chris Masona28ec192007-03-06 20:08:01 -0500228 return ret;
229}
230
231/*
Chris Masonfec577f2007-02-26 10:40:21 -0500232 * find all the blocks marked as pending in the radix tree and remove
233 * them from the extent map
234 */
Chris Masone089f052007-03-16 16:20:31 -0400235static int del_pending_extents(struct btrfs_trans_handle *trans, struct
236 btrfs_root *extent_root)
Chris Masonfec577f2007-02-26 10:40:21 -0500237{
238 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -0400239 int wret;
240 int err = 0;
Chris Masond5719762007-03-23 10:01:08 -0400241 struct buffer_head *gang[4];
Chris Masonfec577f2007-02-26 10:40:21 -0500242 int i;
Chris Masone20d96d2007-03-22 12:13:20 -0400243 struct radix_tree_root *radix = &extent_root->fs_info->pinned_radix;
Chris Masonfec577f2007-02-26 10:40:21 -0500244
245 while(1) {
Chris Mason9f5fae22007-03-20 14:38:32 -0400246 ret = radix_tree_gang_lookup_tag(
Chris Masone20d96d2007-03-22 12:13:20 -0400247 &extent_root->fs_info->pinned_radix,
Chris Mason9f5fae22007-03-20 14:38:32 -0400248 (void **)gang, 0,
249 ARRAY_SIZE(gang),
250 CTREE_EXTENT_PENDING_DEL);
Chris Masonfec577f2007-02-26 10:40:21 -0500251 if (!ret)
252 break;
253 for (i = 0; i < ret; i++) {
Chris Masond5719762007-03-23 10:01:08 -0400254 radix_tree_tag_set(radix, gang[i]->b_blocknr,
255 CTREE_EXTENT_PINNED);
256 radix_tree_tag_clear(radix, gang[i]->b_blocknr,
Chris Mason9f5fae22007-03-20 14:38:32 -0400257 CTREE_EXTENT_PENDING_DEL);
Chris Masond5719762007-03-23 10:01:08 -0400258 wret = __free_extent(trans, extent_root,
259 gang[i]->b_blocknr, 1);
Chris Masone20d96d2007-03-22 12:13:20 -0400260 if (wret)
261 err = wret;
Chris Masonfec577f2007-02-26 10:40:21 -0500262 }
263 }
Chris Masone20d96d2007-03-22 12:13:20 -0400264 return err;
Chris Masonfec577f2007-02-26 10:40:21 -0500265}
266
267/*
268 * remove an extent from the root, returns 0 on success
269 */
Chris Masone089f052007-03-16 16:20:31 -0400270int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
271 *root, u64 blocknr, u64 num_blocks, int pin)
Chris Masonfec577f2007-02-26 10:40:21 -0500272{
Chris Mason9f5fae22007-03-20 14:38:32 -0400273 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Masone20d96d2007-03-22 12:13:20 -0400274 struct buffer_head *t;
Chris Masonfec577f2007-02-26 10:40:21 -0500275 int pending_ret;
276 int ret;
Chris Masona28ec192007-03-06 20:08:01 -0500277
278 if (root == extent_root) {
279 t = find_tree_block(root, blocknr);
Chris Masone20d96d2007-03-22 12:13:20 -0400280 pin_down_block(root, blocknr, CTREE_EXTENT_PENDING_DEL);
Chris Masona28ec192007-03-06 20:08:01 -0500281 return 0;
282 }
Chris Masone20d96d2007-03-22 12:13:20 -0400283 if (pin) {
284 ret = pin_down_block(root, blocknr, CTREE_EXTENT_PINNED);
285 BUG_ON(ret);
286 }
287 ret = __free_extent(trans, root, blocknr, num_blocks);
288 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
Chris Masonfec577f2007-02-26 10:40:21 -0500289 return ret ? ret : pending_ret;
290}
291
292/*
293 * walks the btree of allocated extents and find a hole of a given size.
294 * The key ins is changed to record the hole:
295 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -0400296 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -0500297 * ins->offset == number of blocks
298 * Any available blocks before search_start are skipped.
299 */
Chris Masone089f052007-03-16 16:20:31 -0400300static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
301 *orig_root, u64 num_blocks, u64 search_start, u64
302 search_end, struct btrfs_key *ins)
Chris Masonfec577f2007-02-26 10:40:21 -0500303{
Chris Mason234b63a2007-03-13 10:46:10 -0400304 struct btrfs_path path;
Chris Masone2fa7222007-03-12 16:22:34 -0400305 struct btrfs_key key;
Chris Masonfec577f2007-02-26 10:40:21 -0500306 int ret;
307 u64 hole_size = 0;
308 int slot = 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400309 u64 last_block = 0;
Chris Mason037e6392007-03-07 11:50:24 -0500310 u64 test_block;
Chris Masonfec577f2007-02-26 10:40:21 -0500311 int start_found;
Chris Mason234b63a2007-03-13 10:46:10 -0400312 struct btrfs_leaf *l;
Chris Mason9f5fae22007-03-20 14:38:32 -0400313 struct btrfs_root * root = orig_root->fs_info->extent_root;
Chris Mason0579da42007-03-07 16:15:30 -0500314 int total_needed = num_blocks;
Chris Masone20d96d2007-03-22 12:13:20 -0400315 int level;
Chris Masonfec577f2007-02-26 10:40:21 -0500316
Chris Masone20d96d2007-03-22 12:13:20 -0400317 level = btrfs_header_level(btrfs_buffer_header(root->node));
318 total_needed += (level + 1) * 3;
Chris Mason9f5fae22007-03-20 14:38:32 -0400319 if (root->fs_info->last_insert.objectid > search_start)
320 search_start = root->fs_info->last_insert.objectid;
Chris Mason62e27492007-03-15 12:56:47 -0400321
322 ins->flags = 0;
323 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
324
Chris Masonfec577f2007-02-26 10:40:21 -0500325check_failed:
Chris Mason234b63a2007-03-13 10:46:10 -0400326 btrfs_init_path(&path);
Chris Masonfec577f2007-02-26 10:40:21 -0500327 ins->objectid = search_start;
328 ins->offset = 0;
Chris Masonfec577f2007-02-26 10:40:21 -0500329 start_found = 0;
Chris Masone089f052007-03-16 16:20:31 -0400330 ret = btrfs_search_slot(trans, root, ins, &path, 0, 0);
Chris Mason0f70abe2007-02-28 16:46:22 -0500331 if (ret < 0)
332 goto error;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500333
Chris Mason0579da42007-03-07 16:15:30 -0500334 if (path.slots[0] > 0)
335 path.slots[0]--;
336
Chris Masonfec577f2007-02-26 10:40:21 -0500337 while (1) {
Chris Masone20d96d2007-03-22 12:13:20 -0400338 l = btrfs_buffer_leaf(path.nodes[0]);
Chris Masonfec577f2007-02-26 10:40:21 -0500339 slot = path.slots[0];
Chris Mason7518a232007-03-12 12:01:18 -0400340 if (slot >= btrfs_header_nritems(&l->header)) {
Chris Mason234b63a2007-03-13 10:46:10 -0400341 ret = btrfs_next_leaf(root, &path);
Chris Masonfec577f2007-02-26 10:40:21 -0500342 if (ret == 0)
343 continue;
Chris Mason0f70abe2007-02-28 16:46:22 -0500344 if (ret < 0)
345 goto error;
Chris Masonfec577f2007-02-26 10:40:21 -0500346 if (!start_found) {
347 ins->objectid = search_start;
Chris Mason037e6392007-03-07 11:50:24 -0500348 ins->offset = (u64)-1;
Chris Masonfec577f2007-02-26 10:40:21 -0500349 start_found = 1;
350 goto check_pending;
351 }
352 ins->objectid = last_block > search_start ?
353 last_block : search_start;
Chris Mason037e6392007-03-07 11:50:24 -0500354 ins->offset = (u64)-1;
Chris Masonfec577f2007-02-26 10:40:21 -0500355 goto check_pending;
356 }
Chris Masone2fa7222007-03-12 16:22:34 -0400357 btrfs_disk_key_to_cpu(&key, &l->items[slot].key);
358 if (key.objectid >= search_start) {
Chris Masonfec577f2007-02-26 10:40:21 -0500359 if (start_found) {
Chris Mason0579da42007-03-07 16:15:30 -0500360 if (last_block < search_start)
361 last_block = search_start;
Chris Masone2fa7222007-03-12 16:22:34 -0400362 hole_size = key.objectid - last_block;
Chris Mason037e6392007-03-07 11:50:24 -0500363 if (hole_size > total_needed) {
Chris Masonfec577f2007-02-26 10:40:21 -0500364 ins->objectid = last_block;
Chris Mason037e6392007-03-07 11:50:24 -0500365 ins->offset = hole_size;
Chris Masonfec577f2007-02-26 10:40:21 -0500366 goto check_pending;
367 }
Chris Mason0579da42007-03-07 16:15:30 -0500368 }
Chris Masonfec577f2007-02-26 10:40:21 -0500369 }
Chris Mason0579da42007-03-07 16:15:30 -0500370 start_found = 1;
Chris Masone2fa7222007-03-12 16:22:34 -0400371 last_block = key.objectid + key.offset;
Chris Masonfec577f2007-02-26 10:40:21 -0500372 path.slots[0]++;
373 }
374 // FIXME -ENOSPC
375check_pending:
376 /* we have to make sure we didn't find an extent that has already
377 * been allocated by the map tree or the original allocation
378 */
Chris Mason234b63a2007-03-13 10:46:10 -0400379 btrfs_release_path(root, &path);
Chris Masonfec577f2007-02-26 10:40:21 -0500380 BUG_ON(ins->objectid < search_start);
Chris Mason037e6392007-03-07 11:50:24 -0500381 for (test_block = ins->objectid;
382 test_block < ins->objectid + total_needed; test_block++) {
Chris Mason9f5fae22007-03-20 14:38:32 -0400383 if (radix_tree_lookup(&root->fs_info->pinned_radix,
384 test_block)) {
Chris Mason037e6392007-03-07 11:50:24 -0500385 search_start = test_block + 1;
Chris Masonfec577f2007-02-26 10:40:21 -0500386 goto check_failed;
387 }
388 }
Chris Mason9f5fae22007-03-20 14:38:32 -0400389 BUG_ON(root->fs_info->current_insert.offset);
390 root->fs_info->current_insert.offset = total_needed - num_blocks;
391 root->fs_info->current_insert.objectid = ins->objectid + num_blocks;
392 root->fs_info->current_insert.flags = 0;
393 root->fs_info->last_insert.objectid = ins->objectid;
Chris Mason037e6392007-03-07 11:50:24 -0500394 ins->offset = num_blocks;
Chris Masonfec577f2007-02-26 10:40:21 -0500395 return 0;
Chris Mason0f70abe2007-02-28 16:46:22 -0500396error:
Chris Mason234b63a2007-03-13 10:46:10 -0400397 btrfs_release_path(root, &path);
Chris Mason0f70abe2007-02-28 16:46:22 -0500398 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -0500399}
400
401/*
Chris Masonfec577f2007-02-26 10:40:21 -0500402 * finds a free extent and does all the dirty work required for allocation
403 * returns the key for the extent through ins, and a tree buffer for
404 * the first block of the extent through buf.
405 *
406 * returns 0 if everything worked, non-zero otherwise.
407 */
Chris Masone089f052007-03-16 16:20:31 -0400408static int alloc_extent(struct btrfs_trans_handle *trans, struct btrfs_root
409 *root, u64 num_blocks, u64 search_start, u64
410 search_end, u64 owner, struct btrfs_key *ins)
Chris Masonfec577f2007-02-26 10:40:21 -0500411{
412 int ret;
413 int pending_ret;
Chris Mason1261ec42007-03-20 20:35:03 -0400414 u64 super_blocks_used;
415 struct btrfs_fs_info *info = root->fs_info;
416 struct btrfs_root *extent_root = info->extent_root;
Chris Mason234b63a2007-03-13 10:46:10 -0400417 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -0500418
Chris Masoncf27e1e2007-03-13 09:49:06 -0400419 btrfs_set_extent_refs(&extent_item, 1);
420 btrfs_set_extent_owner(&extent_item, owner);
Chris Masonfec577f2007-02-26 10:40:21 -0500421
Chris Mason037e6392007-03-07 11:50:24 -0500422 if (root == extent_root) {
Chris Mason9f5fae22007-03-20 14:38:32 -0400423 BUG_ON(extent_root->fs_info->current_insert.offset == 0);
Chris Mason037e6392007-03-07 11:50:24 -0500424 BUG_ON(num_blocks != 1);
Chris Mason9f5fae22007-03-20 14:38:32 -0400425 BUG_ON(extent_root->fs_info->current_insert.flags ==
426 extent_root->fs_info->current_insert.offset);
Chris Mason037e6392007-03-07 11:50:24 -0500427 ins->offset = 1;
Chris Mason9f5fae22007-03-20 14:38:32 -0400428 ins->objectid = extent_root->fs_info->current_insert.objectid +
429 extent_root->fs_info->current_insert.flags++;
Chris Masonfec577f2007-02-26 10:40:21 -0500430 return 0;
431 }
Chris Masone089f052007-03-16 16:20:31 -0400432 ret = find_free_extent(trans, root, num_blocks, search_start,
Chris Mason037e6392007-03-07 11:50:24 -0500433 search_end, ins);
434 if (ret)
435 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -0500436
Chris Mason1261ec42007-03-20 20:35:03 -0400437 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
438 btrfs_set_super_blocks_used(info->disk_super, super_blocks_used +
439 num_blocks);
Chris Masone089f052007-03-16 16:20:31 -0400440 ret = btrfs_insert_item(trans, extent_root, ins, &extent_item,
441 sizeof(extent_item));
Chris Mason037e6392007-03-07 11:50:24 -0500442
Chris Masone089f052007-03-16 16:20:31 -0400443 finish_current_insert(trans, extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -0400444 pending_ret = del_pending_extents(trans, extent_root);
Chris Mason037e6392007-03-07 11:50:24 -0500445 if (ret)
446 return ret;
447 if (pending_ret)
448 return pending_ret;
449 return 0;
Chris Masonfec577f2007-02-26 10:40:21 -0500450}
451
452/*
453 * helper function to allocate a block for a given tree
454 * returns the tree buffer or NULL.
455 */
Chris Masone20d96d2007-03-22 12:13:20 -0400456struct buffer_head *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Chris Masone089f052007-03-16 16:20:31 -0400457 struct btrfs_root *root)
Chris Masonfec577f2007-02-26 10:40:21 -0500458{
Chris Masone2fa7222007-03-12 16:22:34 -0400459 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -0500460 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -0400461 struct buffer_head *buf;
Chris Masonfec577f2007-02-26 10:40:21 -0500462
Chris Masone089f052007-03-16 16:20:31 -0400463 ret = alloc_extent(trans, root, 1, 0, (unsigned long)-1,
Chris Masone20d96d2007-03-22 12:13:20 -0400464 btrfs_header_parentid(btrfs_buffer_header(root->node)), &ins);
Chris Masonfec577f2007-02-26 10:40:21 -0500465 if (ret) {
466 BUG();
467 return NULL;
468 }
Chris Mason037e6392007-03-07 11:50:24 -0500469 buf = find_tree_block(root, ins.objectid);
Chris Masondf2ce342007-03-23 11:00:45 -0400470 set_buffer_uptodate(buf);
Chris Masonfec577f2007-02-26 10:40:21 -0500471 return buf;
472}
Chris Masona28ec192007-03-06 20:08:01 -0500473
Chris Mason9aca1d52007-03-13 11:09:37 -0400474/*
475 * helper function for drop_snapshot, this walks down the tree dropping ref
476 * counts as it goes.
477 */
Chris Masone089f052007-03-16 16:20:31 -0400478static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
479 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -0500480{
Chris Masone20d96d2007-03-22 12:13:20 -0400481 struct buffer_head *next;
482 struct buffer_head *cur;
Chris Mason20524f02007-03-10 06:35:47 -0500483 u64 blocknr;
484 int ret;
485 u32 refs;
486
Chris Masone20d96d2007-03-22 12:13:20 -0400487 ret = lookup_block_ref(trans, root, path->nodes[*level]->b_blocknr,
Chris Masone089f052007-03-16 16:20:31 -0400488 &refs);
Chris Mason20524f02007-03-10 06:35:47 -0500489 BUG_ON(ret);
490 if (refs > 1)
491 goto out;
Chris Mason9aca1d52007-03-13 11:09:37 -0400492 /*
493 * walk down to the last node level and free all the leaves
494 */
Chris Mason20524f02007-03-10 06:35:47 -0500495 while(*level > 0) {
496 cur = path->nodes[*level];
Chris Mason7518a232007-03-12 12:01:18 -0400497 if (path->slots[*level] >=
Chris Masone20d96d2007-03-22 12:13:20 -0400498 btrfs_header_nritems(btrfs_buffer_header(cur)))
Chris Mason20524f02007-03-10 06:35:47 -0500499 break;
Chris Masone20d96d2007-03-22 12:13:20 -0400500 blocknr = btrfs_node_blockptr(btrfs_buffer_node(cur),
501 path->slots[*level]);
Chris Masone089f052007-03-16 16:20:31 -0400502 ret = lookup_block_ref(trans, root, blocknr, &refs);
Chris Mason20524f02007-03-10 06:35:47 -0500503 if (refs != 1 || *level == 1) {
504 path->slots[*level]++;
Chris Masone089f052007-03-16 16:20:31 -0400505 ret = btrfs_free_extent(trans, root, blocknr, 1, 1);
Chris Mason20524f02007-03-10 06:35:47 -0500506 BUG_ON(ret);
507 continue;
508 }
509 BUG_ON(ret);
510 next = read_tree_block(root, blocknr);
Chris Mason83e15a22007-03-12 09:03:27 -0400511 if (path->nodes[*level-1])
Chris Mason234b63a2007-03-13 10:46:10 -0400512 btrfs_block_release(root, path->nodes[*level-1]);
Chris Mason20524f02007-03-10 06:35:47 -0500513 path->nodes[*level-1] = next;
Chris Masone20d96d2007-03-22 12:13:20 -0400514 *level = btrfs_header_level(btrfs_buffer_header(next));
Chris Mason20524f02007-03-10 06:35:47 -0500515 path->slots[*level] = 0;
516 }
517out:
Chris Masone20d96d2007-03-22 12:13:20 -0400518 ret = btrfs_free_extent(trans, root, path->nodes[*level]->b_blocknr,
519 1, 1);
Chris Mason234b63a2007-03-13 10:46:10 -0400520 btrfs_block_release(root, path->nodes[*level]);
Chris Mason20524f02007-03-10 06:35:47 -0500521 path->nodes[*level] = NULL;
522 *level += 1;
523 BUG_ON(ret);
524 return 0;
525}
526
Chris Mason9aca1d52007-03-13 11:09:37 -0400527/*
528 * helper for dropping snapshots. This walks back up the tree in the path
529 * to find the first node higher up where we haven't yet gone through
530 * all the slots
531 */
Chris Masone089f052007-03-16 16:20:31 -0400532static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root
533 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -0500534{
535 int i;
536 int slot;
537 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400538 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Mason20524f02007-03-10 06:35:47 -0500539 slot = path->slots[i];
Chris Masone20d96d2007-03-22 12:13:20 -0400540 if (slot < btrfs_header_nritems(
541 btrfs_buffer_header(path->nodes[i])) - 1) {
Chris Mason20524f02007-03-10 06:35:47 -0500542 path->slots[i]++;
543 *level = i;
544 return 0;
545 } else {
Chris Masone089f052007-03-16 16:20:31 -0400546 ret = btrfs_free_extent(trans, root,
Chris Masone20d96d2007-03-22 12:13:20 -0400547 path->nodes[*level]->b_blocknr,
Chris Masone089f052007-03-16 16:20:31 -0400548 1, 1);
Chris Mason234b63a2007-03-13 10:46:10 -0400549 btrfs_block_release(root, path->nodes[*level]);
Chris Mason83e15a22007-03-12 09:03:27 -0400550 path->nodes[*level] = NULL;
Chris Mason20524f02007-03-10 06:35:47 -0500551 *level = i + 1;
552 BUG_ON(ret);
553 }
554 }
555 return 1;
556}
557
Chris Mason9aca1d52007-03-13 11:09:37 -0400558/*
559 * drop the reference count on the tree rooted at 'snap'. This traverses
560 * the tree freeing any blocks that have a ref count of zero after being
561 * decremented.
562 */
Chris Masone089f052007-03-16 16:20:31 -0400563int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masone20d96d2007-03-22 12:13:20 -0400564 *root, struct buffer_head *snap)
Chris Mason20524f02007-03-10 06:35:47 -0500565{
Chris Mason3768f362007-03-13 16:47:54 -0400566 int ret = 0;
Chris Mason9aca1d52007-03-13 11:09:37 -0400567 int wret;
Chris Mason20524f02007-03-10 06:35:47 -0500568 int level;
Chris Mason234b63a2007-03-13 10:46:10 -0400569 struct btrfs_path path;
Chris Mason20524f02007-03-10 06:35:47 -0500570 int i;
571 int orig_level;
572
Chris Mason234b63a2007-03-13 10:46:10 -0400573 btrfs_init_path(&path);
Chris Mason20524f02007-03-10 06:35:47 -0500574
Chris Masone20d96d2007-03-22 12:13:20 -0400575 level = btrfs_header_level(btrfs_buffer_header(snap));
Chris Mason20524f02007-03-10 06:35:47 -0500576 orig_level = level;
577 path.nodes[level] = snap;
578 path.slots[level] = 0;
579 while(1) {
Chris Masone089f052007-03-16 16:20:31 -0400580 wret = walk_down_tree(trans, root, &path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -0400581 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -0500582 break;
Chris Mason9aca1d52007-03-13 11:09:37 -0400583 if (wret < 0)
584 ret = wret;
585
Chris Masone089f052007-03-16 16:20:31 -0400586 wret = walk_up_tree(trans, root, &path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -0400587 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -0500588 break;
Chris Mason9aca1d52007-03-13 11:09:37 -0400589 if (wret < 0)
590 ret = wret;
Chris Mason20524f02007-03-10 06:35:47 -0500591 }
Chris Mason83e15a22007-03-12 09:03:27 -0400592 for (i = 0; i <= orig_level; i++) {
593 if (path.nodes[i]) {
Chris Mason234b63a2007-03-13 10:46:10 -0400594 btrfs_block_release(root, path.nodes[i]);
Chris Mason83e15a22007-03-12 09:03:27 -0400595 }
Chris Mason20524f02007-03-10 06:35:47 -0500596 }
Chris Mason9aca1d52007-03-13 11:09:37 -0400597 return ret;
Chris Mason20524f02007-03-10 06:35:47 -0500598}