blob: 9583a9ae8b798d8ca7fd664d5ab2bee771994adc [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 Masond561c022007-03-23 19:47:49 -0400171 if (err && err != -EEXIST) {
172 BUG();
Chris Masone20d96d2007-03-22 12:13:20 -0400173 return err;
Chris Masond561c022007-03-23 19:47:49 -0400174 }
Chris Masone20d96d2007-03-22 12:13:20 -0400175 radix_tree_tag_set(&root->fs_info->pinned_radix, blocknr,
176 tag);
177 return 0;
178}
179
Chris Masona28ec192007-03-06 20:08:01 -0500180/*
181 * remove an extent from the root, returns 0 on success
182 */
Chris Masone089f052007-03-16 16:20:31 -0400183static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masone20d96d2007-03-22 12:13:20 -0400184 *root, u64 blocknr, u64 num_blocks)
Chris Masona28ec192007-03-06 20:08:01 -0500185{
Chris Mason234b63a2007-03-13 10:46:10 -0400186 struct btrfs_path path;
Chris Masone2fa7222007-03-12 16:22:34 -0400187 struct btrfs_key key;
Chris Mason1261ec42007-03-20 20:35:03 -0400188 struct btrfs_fs_info *info = root->fs_info;
189 struct btrfs_root *extent_root = info->extent_root;
Chris Masona28ec192007-03-06 20:08:01 -0500190 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400191 struct btrfs_extent_item *ei;
Chris Masone2fa7222007-03-12 16:22:34 -0400192 struct btrfs_key ins;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400193 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500194
Chris Masona28ec192007-03-06 20:08:01 -0500195 key.objectid = blocknr;
196 key.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -0400197 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masona28ec192007-03-06 20:08:01 -0500198 key.offset = num_blocks;
199
Chris Masone089f052007-03-16 16:20:31 -0400200 find_free_extent(trans, root, 0, 0, (u64)-1, &ins);
Chris Mason234b63a2007-03-13 10:46:10 -0400201 btrfs_init_path(&path);
Chris Masone089f052007-03-16 16:20:31 -0400202 ret = btrfs_search_slot(trans, extent_root, &key, &path, -1, 1);
Chris Masona28ec192007-03-06 20:08:01 -0500203 if (ret) {
Chris Mason2e635a22007-03-21 11:12:56 -0400204 printk("failed to find %Lu\n", key.objectid);
Chris Mason234b63a2007-03-13 10:46:10 -0400205 btrfs_print_tree(extent_root, extent_root->node);
Chris Mason2e635a22007-03-21 11:12:56 -0400206 printk("failed to find %Lu\n", key.objectid);
Chris Masona28ec192007-03-06 20:08:01 -0500207 BUG();
208 }
Chris Masone20d96d2007-03-22 12:13:20 -0400209 ei = btrfs_item_ptr(btrfs_buffer_leaf(path.nodes[0]), path.slots[0],
Chris Mason123abc82007-03-14 14:14:43 -0400210 struct btrfs_extent_item);
Chris Masona28ec192007-03-06 20:08:01 -0500211 BUG_ON(ei->refs == 0);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400212 refs = btrfs_extent_refs(ei) - 1;
213 btrfs_set_extent_refs(ei, refs);
214 if (refs == 0) {
Chris Mason1261ec42007-03-20 20:35:03 -0400215 u64 super_blocks_used;
Chris Mason1261ec42007-03-20 20:35:03 -0400216 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
217 btrfs_set_super_blocks_used(info->disk_super,
218 super_blocks_used - num_blocks);
Chris Masone089f052007-03-16 16:20:31 -0400219 ret = btrfs_del_item(trans, extent_root, &path);
Chris Masone20d96d2007-03-22 12:13:20 -0400220 if (extent_root->fs_info->last_insert.objectid >
Chris Mason9f5fae22007-03-20 14:38:32 -0400221 blocknr)
222 extent_root->fs_info->last_insert.objectid = blocknr;
Chris Masona28ec192007-03-06 20:08:01 -0500223 if (ret)
224 BUG();
225 }
Chris Masond5719762007-03-23 10:01:08 -0400226 mark_buffer_dirty(path.nodes[0]);
Chris Mason234b63a2007-03-13 10:46:10 -0400227 btrfs_release_path(extent_root, &path);
Chris Masone089f052007-03-16 16:20:31 -0400228 finish_current_insert(trans, extent_root);
Chris Masona28ec192007-03-06 20:08:01 -0500229 return ret;
230}
231
232/*
Chris Masonfec577f2007-02-26 10:40:21 -0500233 * find all the blocks marked as pending in the radix tree and remove
234 * them from the extent map
235 */
Chris Masone089f052007-03-16 16:20:31 -0400236static int del_pending_extents(struct btrfs_trans_handle *trans, struct
237 btrfs_root *extent_root)
Chris Masonfec577f2007-02-26 10:40:21 -0500238{
239 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -0400240 int wret;
241 int err = 0;
Chris Masond5719762007-03-23 10:01:08 -0400242 struct buffer_head *gang[4];
Chris Masonfec577f2007-02-26 10:40:21 -0500243 int i;
Chris Masone20d96d2007-03-22 12:13:20 -0400244 struct radix_tree_root *radix = &extent_root->fs_info->pinned_radix;
Chris Masonfec577f2007-02-26 10:40:21 -0500245
246 while(1) {
Chris Mason9f5fae22007-03-20 14:38:32 -0400247 ret = radix_tree_gang_lookup_tag(
Chris Masone20d96d2007-03-22 12:13:20 -0400248 &extent_root->fs_info->pinned_radix,
Chris Mason9f5fae22007-03-20 14:38:32 -0400249 (void **)gang, 0,
250 ARRAY_SIZE(gang),
251 CTREE_EXTENT_PENDING_DEL);
Chris Masonfec577f2007-02-26 10:40:21 -0500252 if (!ret)
253 break;
254 for (i = 0; i < ret; i++) {
Chris Masond5719762007-03-23 10:01:08 -0400255 radix_tree_tag_set(radix, gang[i]->b_blocknr,
256 CTREE_EXTENT_PINNED);
257 radix_tree_tag_clear(radix, gang[i]->b_blocknr,
Chris Mason9f5fae22007-03-20 14:38:32 -0400258 CTREE_EXTENT_PENDING_DEL);
Chris Masond5719762007-03-23 10:01:08 -0400259 wret = __free_extent(trans, extent_root,
260 gang[i]->b_blocknr, 1);
Chris Masone20d96d2007-03-22 12:13:20 -0400261 if (wret)
262 err = wret;
Chris Masonfec577f2007-02-26 10:40:21 -0500263 }
264 }
Chris Masone20d96d2007-03-22 12:13:20 -0400265 return err;
Chris Masonfec577f2007-02-26 10:40:21 -0500266}
267
268/*
269 * remove an extent from the root, returns 0 on success
270 */
Chris Masone089f052007-03-16 16:20:31 -0400271int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
272 *root, u64 blocknr, u64 num_blocks, int pin)
Chris Masonfec577f2007-02-26 10:40:21 -0500273{
Chris Mason9f5fae22007-03-20 14:38:32 -0400274 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Masone20d96d2007-03-22 12:13:20 -0400275 struct buffer_head *t;
Chris Masonfec577f2007-02-26 10:40:21 -0500276 int pending_ret;
277 int ret;
Chris Masona28ec192007-03-06 20:08:01 -0500278
279 if (root == extent_root) {
280 t = find_tree_block(root, blocknr);
Chris Masone20d96d2007-03-22 12:13:20 -0400281 pin_down_block(root, blocknr, CTREE_EXTENT_PENDING_DEL);
Chris Masona28ec192007-03-06 20:08:01 -0500282 return 0;
283 }
Chris Masone20d96d2007-03-22 12:13:20 -0400284 if (pin) {
285 ret = pin_down_block(root, blocknr, CTREE_EXTENT_PINNED);
286 BUG_ON(ret);
287 }
288 ret = __free_extent(trans, root, blocknr, num_blocks);
289 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
Chris Masonfec577f2007-02-26 10:40:21 -0500290 return ret ? ret : pending_ret;
291}
292
293/*
294 * walks the btree of allocated extents and find a hole of a given size.
295 * The key ins is changed to record the hole:
296 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -0400297 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -0500298 * ins->offset == number of blocks
299 * Any available blocks before search_start are skipped.
300 */
Chris Masone089f052007-03-16 16:20:31 -0400301static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
302 *orig_root, u64 num_blocks, u64 search_start, u64
303 search_end, struct btrfs_key *ins)
Chris Masonfec577f2007-02-26 10:40:21 -0500304{
Chris Mason234b63a2007-03-13 10:46:10 -0400305 struct btrfs_path path;
Chris Masone2fa7222007-03-12 16:22:34 -0400306 struct btrfs_key key;
Chris Masonfec577f2007-02-26 10:40:21 -0500307 int ret;
308 u64 hole_size = 0;
309 int slot = 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400310 u64 last_block = 0;
Chris Mason037e6392007-03-07 11:50:24 -0500311 u64 test_block;
Chris Masonfec577f2007-02-26 10:40:21 -0500312 int start_found;
Chris Mason234b63a2007-03-13 10:46:10 -0400313 struct btrfs_leaf *l;
Chris Mason9f5fae22007-03-20 14:38:32 -0400314 struct btrfs_root * root = orig_root->fs_info->extent_root;
Chris Mason0579da42007-03-07 16:15:30 -0500315 int total_needed = num_blocks;
Chris Masone20d96d2007-03-22 12:13:20 -0400316 int level;
Chris Masonfec577f2007-02-26 10:40:21 -0500317
Chris Masone20d96d2007-03-22 12:13:20 -0400318 level = btrfs_header_level(btrfs_buffer_header(root->node));
319 total_needed += (level + 1) * 3;
Chris Mason9f5fae22007-03-20 14:38:32 -0400320 if (root->fs_info->last_insert.objectid > search_start)
321 search_start = root->fs_info->last_insert.objectid;
Chris Mason62e27492007-03-15 12:56:47 -0400322
323 ins->flags = 0;
324 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
325
Chris Masonfec577f2007-02-26 10:40:21 -0500326check_failed:
Chris Mason234b63a2007-03-13 10:46:10 -0400327 btrfs_init_path(&path);
Chris Masonfec577f2007-02-26 10:40:21 -0500328 ins->objectid = search_start;
329 ins->offset = 0;
Chris Masonfec577f2007-02-26 10:40:21 -0500330 start_found = 0;
Chris Masone089f052007-03-16 16:20:31 -0400331 ret = btrfs_search_slot(trans, root, ins, &path, 0, 0);
Chris Mason0f70abe2007-02-28 16:46:22 -0500332 if (ret < 0)
333 goto error;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500334
Chris Mason0579da42007-03-07 16:15:30 -0500335 if (path.slots[0] > 0)
336 path.slots[0]--;
337
Chris Masonfec577f2007-02-26 10:40:21 -0500338 while (1) {
Chris Masone20d96d2007-03-22 12:13:20 -0400339 l = btrfs_buffer_leaf(path.nodes[0]);
Chris Masonfec577f2007-02-26 10:40:21 -0500340 slot = path.slots[0];
Chris Mason7518a232007-03-12 12:01:18 -0400341 if (slot >= btrfs_header_nritems(&l->header)) {
Chris Mason234b63a2007-03-13 10:46:10 -0400342 ret = btrfs_next_leaf(root, &path);
Chris Masonfec577f2007-02-26 10:40:21 -0500343 if (ret == 0)
344 continue;
Chris Mason0f70abe2007-02-28 16:46:22 -0500345 if (ret < 0)
346 goto error;
Chris Masonfec577f2007-02-26 10:40:21 -0500347 if (!start_found) {
348 ins->objectid = search_start;
Chris Mason037e6392007-03-07 11:50:24 -0500349 ins->offset = (u64)-1;
Chris Masonfec577f2007-02-26 10:40:21 -0500350 start_found = 1;
351 goto check_pending;
352 }
353 ins->objectid = last_block > search_start ?
354 last_block : search_start;
Chris Mason037e6392007-03-07 11:50:24 -0500355 ins->offset = (u64)-1;
Chris Masonfec577f2007-02-26 10:40:21 -0500356 goto check_pending;
357 }
Chris Masone2fa7222007-03-12 16:22:34 -0400358 btrfs_disk_key_to_cpu(&key, &l->items[slot].key);
359 if (key.objectid >= search_start) {
Chris Masonfec577f2007-02-26 10:40:21 -0500360 if (start_found) {
Chris Mason0579da42007-03-07 16:15:30 -0500361 if (last_block < search_start)
362 last_block = search_start;
Chris Masone2fa7222007-03-12 16:22:34 -0400363 hole_size = key.objectid - last_block;
Chris Mason037e6392007-03-07 11:50:24 -0500364 if (hole_size > total_needed) {
Chris Masonfec577f2007-02-26 10:40:21 -0500365 ins->objectid = last_block;
Chris Mason037e6392007-03-07 11:50:24 -0500366 ins->offset = hole_size;
Chris Masonfec577f2007-02-26 10:40:21 -0500367 goto check_pending;
368 }
Chris Mason0579da42007-03-07 16:15:30 -0500369 }
Chris Masonfec577f2007-02-26 10:40:21 -0500370 }
Chris Mason0579da42007-03-07 16:15:30 -0500371 start_found = 1;
Chris Masone2fa7222007-03-12 16:22:34 -0400372 last_block = key.objectid + key.offset;
Chris Masonfec577f2007-02-26 10:40:21 -0500373 path.slots[0]++;
374 }
375 // FIXME -ENOSPC
376check_pending:
377 /* we have to make sure we didn't find an extent that has already
378 * been allocated by the map tree or the original allocation
379 */
Chris Mason234b63a2007-03-13 10:46:10 -0400380 btrfs_release_path(root, &path);
Chris Masonfec577f2007-02-26 10:40:21 -0500381 BUG_ON(ins->objectid < search_start);
Chris Mason037e6392007-03-07 11:50:24 -0500382 for (test_block = ins->objectid;
383 test_block < ins->objectid + total_needed; test_block++) {
Chris Mason9f5fae22007-03-20 14:38:32 -0400384 if (radix_tree_lookup(&root->fs_info->pinned_radix,
385 test_block)) {
Chris Mason037e6392007-03-07 11:50:24 -0500386 search_start = test_block + 1;
Chris Masonfec577f2007-02-26 10:40:21 -0500387 goto check_failed;
388 }
389 }
Chris Mason9f5fae22007-03-20 14:38:32 -0400390 BUG_ON(root->fs_info->current_insert.offset);
391 root->fs_info->current_insert.offset = total_needed - num_blocks;
392 root->fs_info->current_insert.objectid = ins->objectid + num_blocks;
393 root->fs_info->current_insert.flags = 0;
394 root->fs_info->last_insert.objectid = ins->objectid;
Chris Mason037e6392007-03-07 11:50:24 -0500395 ins->offset = num_blocks;
Chris Masonfec577f2007-02-26 10:40:21 -0500396 return 0;
Chris Mason0f70abe2007-02-28 16:46:22 -0500397error:
Chris Mason234b63a2007-03-13 10:46:10 -0400398 btrfs_release_path(root, &path);
Chris Mason0f70abe2007-02-28 16:46:22 -0500399 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -0500400}
401
402/*
Chris Masonfec577f2007-02-26 10:40:21 -0500403 * finds a free extent and does all the dirty work required for allocation
404 * returns the key for the extent through ins, and a tree buffer for
405 * the first block of the extent through buf.
406 *
407 * returns 0 if everything worked, non-zero otherwise.
408 */
Chris Masone089f052007-03-16 16:20:31 -0400409static int alloc_extent(struct btrfs_trans_handle *trans, struct btrfs_root
410 *root, u64 num_blocks, u64 search_start, u64
411 search_end, u64 owner, struct btrfs_key *ins)
Chris Masonfec577f2007-02-26 10:40:21 -0500412{
413 int ret;
414 int pending_ret;
Chris Mason1261ec42007-03-20 20:35:03 -0400415 u64 super_blocks_used;
416 struct btrfs_fs_info *info = root->fs_info;
417 struct btrfs_root *extent_root = info->extent_root;
Chris Mason234b63a2007-03-13 10:46:10 -0400418 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -0500419
Chris Masoncf27e1e2007-03-13 09:49:06 -0400420 btrfs_set_extent_refs(&extent_item, 1);
421 btrfs_set_extent_owner(&extent_item, owner);
Chris Masonfec577f2007-02-26 10:40:21 -0500422
Chris Mason037e6392007-03-07 11:50:24 -0500423 if (root == extent_root) {
Chris Mason9f5fae22007-03-20 14:38:32 -0400424 BUG_ON(extent_root->fs_info->current_insert.offset == 0);
Chris Mason037e6392007-03-07 11:50:24 -0500425 BUG_ON(num_blocks != 1);
Chris Mason9f5fae22007-03-20 14:38:32 -0400426 BUG_ON(extent_root->fs_info->current_insert.flags ==
427 extent_root->fs_info->current_insert.offset);
Chris Mason037e6392007-03-07 11:50:24 -0500428 ins->offset = 1;
Chris Mason9f5fae22007-03-20 14:38:32 -0400429 ins->objectid = extent_root->fs_info->current_insert.objectid +
430 extent_root->fs_info->current_insert.flags++;
Chris Masonfec577f2007-02-26 10:40:21 -0500431 return 0;
432 }
Chris Masone089f052007-03-16 16:20:31 -0400433 ret = find_free_extent(trans, root, num_blocks, search_start,
Chris Mason037e6392007-03-07 11:50:24 -0500434 search_end, ins);
435 if (ret)
436 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -0500437
Chris Mason1261ec42007-03-20 20:35:03 -0400438 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
439 btrfs_set_super_blocks_used(info->disk_super, super_blocks_used +
440 num_blocks);
Chris Masone089f052007-03-16 16:20:31 -0400441 ret = btrfs_insert_item(trans, extent_root, ins, &extent_item,
442 sizeof(extent_item));
Chris Mason037e6392007-03-07 11:50:24 -0500443
Chris Masone089f052007-03-16 16:20:31 -0400444 finish_current_insert(trans, extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -0400445 pending_ret = del_pending_extents(trans, extent_root);
Chris Mason037e6392007-03-07 11:50:24 -0500446 if (ret)
447 return ret;
448 if (pending_ret)
449 return pending_ret;
450 return 0;
Chris Masonfec577f2007-02-26 10:40:21 -0500451}
452
453/*
454 * helper function to allocate a block for a given tree
455 * returns the tree buffer or NULL.
456 */
Chris Masone20d96d2007-03-22 12:13:20 -0400457struct buffer_head *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Chris Masone089f052007-03-16 16:20:31 -0400458 struct btrfs_root *root)
Chris Masonfec577f2007-02-26 10:40:21 -0500459{
Chris Masone2fa7222007-03-12 16:22:34 -0400460 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -0500461 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -0400462 struct buffer_head *buf;
Chris Masonfec577f2007-02-26 10:40:21 -0500463
Chris Masone089f052007-03-16 16:20:31 -0400464 ret = alloc_extent(trans, root, 1, 0, (unsigned long)-1,
Chris Masone20d96d2007-03-22 12:13:20 -0400465 btrfs_header_parentid(btrfs_buffer_header(root->node)), &ins);
Chris Masonfec577f2007-02-26 10:40:21 -0500466 if (ret) {
467 BUG();
468 return NULL;
469 }
Chris Mason037e6392007-03-07 11:50:24 -0500470 buf = find_tree_block(root, ins.objectid);
Chris Masondf2ce342007-03-23 11:00:45 -0400471 set_buffer_uptodate(buf);
Chris Masonfec577f2007-02-26 10:40:21 -0500472 return buf;
473}
Chris Masona28ec192007-03-06 20:08:01 -0500474
Chris Mason9aca1d52007-03-13 11:09:37 -0400475/*
476 * helper function for drop_snapshot, this walks down the tree dropping ref
477 * counts as it goes.
478 */
Chris Masone089f052007-03-16 16:20:31 -0400479static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
480 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -0500481{
Chris Masone20d96d2007-03-22 12:13:20 -0400482 struct buffer_head *next;
483 struct buffer_head *cur;
Chris Mason20524f02007-03-10 06:35:47 -0500484 u64 blocknr;
485 int ret;
486 u32 refs;
487
Chris Masone20d96d2007-03-22 12:13:20 -0400488 ret = lookup_block_ref(trans, root, path->nodes[*level]->b_blocknr,
Chris Masone089f052007-03-16 16:20:31 -0400489 &refs);
Chris Mason20524f02007-03-10 06:35:47 -0500490 BUG_ON(ret);
491 if (refs > 1)
492 goto out;
Chris Mason9aca1d52007-03-13 11:09:37 -0400493 /*
494 * walk down to the last node level and free all the leaves
495 */
Chris Mason20524f02007-03-10 06:35:47 -0500496 while(*level > 0) {
497 cur = path->nodes[*level];
Chris Mason7518a232007-03-12 12:01:18 -0400498 if (path->slots[*level] >=
Chris Masone20d96d2007-03-22 12:13:20 -0400499 btrfs_header_nritems(btrfs_buffer_header(cur)))
Chris Mason20524f02007-03-10 06:35:47 -0500500 break;
Chris Masone20d96d2007-03-22 12:13:20 -0400501 blocknr = btrfs_node_blockptr(btrfs_buffer_node(cur),
502 path->slots[*level]);
Chris Masone089f052007-03-16 16:20:31 -0400503 ret = lookup_block_ref(trans, root, blocknr, &refs);
Chris Mason20524f02007-03-10 06:35:47 -0500504 if (refs != 1 || *level == 1) {
505 path->slots[*level]++;
Chris Masone089f052007-03-16 16:20:31 -0400506 ret = btrfs_free_extent(trans, root, blocknr, 1, 1);
Chris Mason20524f02007-03-10 06:35:47 -0500507 BUG_ON(ret);
508 continue;
509 }
510 BUG_ON(ret);
511 next = read_tree_block(root, blocknr);
Chris Mason83e15a22007-03-12 09:03:27 -0400512 if (path->nodes[*level-1])
Chris Mason234b63a2007-03-13 10:46:10 -0400513 btrfs_block_release(root, path->nodes[*level-1]);
Chris Mason20524f02007-03-10 06:35:47 -0500514 path->nodes[*level-1] = next;
Chris Masone20d96d2007-03-22 12:13:20 -0400515 *level = btrfs_header_level(btrfs_buffer_header(next));
Chris Mason20524f02007-03-10 06:35:47 -0500516 path->slots[*level] = 0;
517 }
518out:
Chris Masone20d96d2007-03-22 12:13:20 -0400519 ret = btrfs_free_extent(trans, root, path->nodes[*level]->b_blocknr,
520 1, 1);
Chris Mason234b63a2007-03-13 10:46:10 -0400521 btrfs_block_release(root, path->nodes[*level]);
Chris Mason20524f02007-03-10 06:35:47 -0500522 path->nodes[*level] = NULL;
523 *level += 1;
524 BUG_ON(ret);
525 return 0;
526}
527
Chris Mason9aca1d52007-03-13 11:09:37 -0400528/*
529 * helper for dropping snapshots. This walks back up the tree in the path
530 * to find the first node higher up where we haven't yet gone through
531 * all the slots
532 */
Chris Masone089f052007-03-16 16:20:31 -0400533static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root
534 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -0500535{
536 int i;
537 int slot;
538 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400539 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Mason20524f02007-03-10 06:35:47 -0500540 slot = path->slots[i];
Chris Masone20d96d2007-03-22 12:13:20 -0400541 if (slot < btrfs_header_nritems(
542 btrfs_buffer_header(path->nodes[i])) - 1) {
Chris Mason20524f02007-03-10 06:35:47 -0500543 path->slots[i]++;
544 *level = i;
545 return 0;
546 } else {
Chris Masone089f052007-03-16 16:20:31 -0400547 ret = btrfs_free_extent(trans, root,
Chris Masone20d96d2007-03-22 12:13:20 -0400548 path->nodes[*level]->b_blocknr,
Chris Masone089f052007-03-16 16:20:31 -0400549 1, 1);
Chris Mason234b63a2007-03-13 10:46:10 -0400550 btrfs_block_release(root, path->nodes[*level]);
Chris Mason83e15a22007-03-12 09:03:27 -0400551 path->nodes[*level] = NULL;
Chris Mason20524f02007-03-10 06:35:47 -0500552 *level = i + 1;
553 BUG_ON(ret);
554 }
555 }
556 return 1;
557}
558
Chris Mason9aca1d52007-03-13 11:09:37 -0400559/*
560 * drop the reference count on the tree rooted at 'snap'. This traverses
561 * the tree freeing any blocks that have a ref count of zero after being
562 * decremented.
563 */
Chris Masone089f052007-03-16 16:20:31 -0400564int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masone20d96d2007-03-22 12:13:20 -0400565 *root, struct buffer_head *snap)
Chris Mason20524f02007-03-10 06:35:47 -0500566{
Chris Mason3768f362007-03-13 16:47:54 -0400567 int ret = 0;
Chris Mason9aca1d52007-03-13 11:09:37 -0400568 int wret;
Chris Mason20524f02007-03-10 06:35:47 -0500569 int level;
Chris Mason234b63a2007-03-13 10:46:10 -0400570 struct btrfs_path path;
Chris Mason20524f02007-03-10 06:35:47 -0500571 int i;
572 int orig_level;
573
Chris Mason234b63a2007-03-13 10:46:10 -0400574 btrfs_init_path(&path);
Chris Mason20524f02007-03-10 06:35:47 -0500575
Chris Masone20d96d2007-03-22 12:13:20 -0400576 level = btrfs_header_level(btrfs_buffer_header(snap));
Chris Mason20524f02007-03-10 06:35:47 -0500577 orig_level = level;
578 path.nodes[level] = snap;
579 path.slots[level] = 0;
580 while(1) {
Chris Masone089f052007-03-16 16:20:31 -0400581 wret = walk_down_tree(trans, root, &path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -0400582 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -0500583 break;
Chris Mason9aca1d52007-03-13 11:09:37 -0400584 if (wret < 0)
585 ret = wret;
586
Chris Masone089f052007-03-16 16:20:31 -0400587 wret = walk_up_tree(trans, root, &path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -0400588 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -0500589 break;
Chris Mason9aca1d52007-03-13 11:09:37 -0400590 if (wret < 0)
591 ret = wret;
Chris Mason20524f02007-03-10 06:35:47 -0500592 }
Chris Mason83e15a22007-03-12 09:03:27 -0400593 for (i = 0; i <= orig_level; i++) {
594 if (path.nodes[i]) {
Chris Mason234b63a2007-03-13 10:46:10 -0400595 btrfs_block_release(root, path.nodes[i]);
Chris Mason83e15a22007-03-12 09:03:27 -0400596 }
Chris Mason20524f02007-03-10 06:35:47 -0500597 }
Chris Mason9aca1d52007-03-13 11:09:37 -0400598 return ret;
Chris Mason20524f02007-03-10 06:35:47 -0500599}