blob: 369b960fce45b201e47a1d1fa8a5353154f4d3da [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);
Chris Mason78fae272007-03-25 11:35:08 -0400168 struct btrfs_header *header;
Chris Masond5719762007-03-23 10:01:08 -0400169 BUG_ON(!bh);
Chris Mason78fae272007-03-25 11:35:08 -0400170
171 header = btrfs_buffer_header(bh);
172 if (btrfs_header_generation(header) ==
173 root->fs_info->running_transaction->transid) {
174 return 0;
175 }
176
Chris Masone20d96d2007-03-22 12:13:20 -0400177 err = radix_tree_insert(&root->fs_info->pinned_radix,
Chris Masond5719762007-03-23 10:01:08 -0400178 blocknr, bh);
Chris Masond561c022007-03-23 19:47:49 -0400179 if (err && err != -EEXIST) {
180 BUG();
Chris Masone20d96d2007-03-22 12:13:20 -0400181 return err;
Chris Masond561c022007-03-23 19:47:49 -0400182 }
Chris Mason78fae272007-03-25 11:35:08 -0400183 if (err == -EEXIST)
184 brelse(bh);
Chris Masone20d96d2007-03-22 12:13:20 -0400185 radix_tree_tag_set(&root->fs_info->pinned_radix, blocknr,
186 tag);
187 return 0;
188}
189
Chris Masona28ec192007-03-06 20:08:01 -0500190/*
191 * remove an extent from the root, returns 0 on success
192 */
Chris Masone089f052007-03-16 16:20:31 -0400193static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason78fae272007-03-25 11:35:08 -0400194 *root, u64 blocknr, u64 num_blocks, int pin)
Chris Masona28ec192007-03-06 20:08:01 -0500195{
Chris Mason234b63a2007-03-13 10:46:10 -0400196 struct btrfs_path path;
Chris Masone2fa7222007-03-12 16:22:34 -0400197 struct btrfs_key key;
Chris Mason1261ec42007-03-20 20:35:03 -0400198 struct btrfs_fs_info *info = root->fs_info;
199 struct btrfs_root *extent_root = info->extent_root;
Chris Masona28ec192007-03-06 20:08:01 -0500200 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400201 struct btrfs_extent_item *ei;
Chris Masone2fa7222007-03-12 16:22:34 -0400202 struct btrfs_key ins;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400203 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500204
Chris Masona28ec192007-03-06 20:08:01 -0500205 key.objectid = blocknr;
206 key.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -0400207 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masona28ec192007-03-06 20:08:01 -0500208 key.offset = num_blocks;
209
Chris Masone089f052007-03-16 16:20:31 -0400210 find_free_extent(trans, root, 0, 0, (u64)-1, &ins);
Chris Mason234b63a2007-03-13 10:46:10 -0400211 btrfs_init_path(&path);
Chris Masone089f052007-03-16 16:20:31 -0400212 ret = btrfs_search_slot(trans, extent_root, &key, &path, -1, 1);
Chris Masona28ec192007-03-06 20:08:01 -0500213 if (ret) {
Chris Mason2e635a22007-03-21 11:12:56 -0400214 printk("failed to find %Lu\n", key.objectid);
Chris Mason234b63a2007-03-13 10:46:10 -0400215 btrfs_print_tree(extent_root, extent_root->node);
Chris Mason2e635a22007-03-21 11:12:56 -0400216 printk("failed to find %Lu\n", key.objectid);
Chris Masona28ec192007-03-06 20:08:01 -0500217 BUG();
218 }
Chris Masone20d96d2007-03-22 12:13:20 -0400219 ei = btrfs_item_ptr(btrfs_buffer_leaf(path.nodes[0]), path.slots[0],
Chris Mason123abc82007-03-14 14:14:43 -0400220 struct btrfs_extent_item);
Chris Masona28ec192007-03-06 20:08:01 -0500221 BUG_ON(ei->refs == 0);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400222 refs = btrfs_extent_refs(ei) - 1;
223 btrfs_set_extent_refs(ei, refs);
224 if (refs == 0) {
Chris Mason1261ec42007-03-20 20:35:03 -0400225 u64 super_blocks_used;
Chris Mason78fae272007-03-25 11:35:08 -0400226
227 if (pin) {
228 ret = pin_down_block(root, blocknr,
229 CTREE_EXTENT_PINNED);
230 BUG_ON(ret);
231 }
232
Chris Mason1261ec42007-03-20 20:35:03 -0400233 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
234 btrfs_set_super_blocks_used(info->disk_super,
235 super_blocks_used - num_blocks);
Chris Masone089f052007-03-16 16:20:31 -0400236 ret = btrfs_del_item(trans, extent_root, &path);
Chris Mason78fae272007-03-25 11:35:08 -0400237 if (extent_root->fs_info->last_insert.objectid > blocknr)
Chris Mason9f5fae22007-03-20 14:38:32 -0400238 extent_root->fs_info->last_insert.objectid = blocknr;
Chris Masona28ec192007-03-06 20:08:01 -0500239 if (ret)
240 BUG();
241 }
Chris Masond5719762007-03-23 10:01:08 -0400242 mark_buffer_dirty(path.nodes[0]);
Chris Mason234b63a2007-03-13 10:46:10 -0400243 btrfs_release_path(extent_root, &path);
Chris Masone089f052007-03-16 16:20:31 -0400244 finish_current_insert(trans, extent_root);
Chris Masona28ec192007-03-06 20:08:01 -0500245 return ret;
246}
247
248/*
Chris Masonfec577f2007-02-26 10:40:21 -0500249 * find all the blocks marked as pending in the radix tree and remove
250 * them from the extent map
251 */
Chris Masone089f052007-03-16 16:20:31 -0400252static int del_pending_extents(struct btrfs_trans_handle *trans, struct
253 btrfs_root *extent_root)
Chris Masonfec577f2007-02-26 10:40:21 -0500254{
255 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -0400256 int wret;
257 int err = 0;
Chris Masond5719762007-03-23 10:01:08 -0400258 struct buffer_head *gang[4];
Chris Masonfec577f2007-02-26 10:40:21 -0500259 int i;
Chris Masone20d96d2007-03-22 12:13:20 -0400260 struct radix_tree_root *radix = &extent_root->fs_info->pinned_radix;
Chris Masonfec577f2007-02-26 10:40:21 -0500261
262 while(1) {
Chris Mason9f5fae22007-03-20 14:38:32 -0400263 ret = radix_tree_gang_lookup_tag(
Chris Masone20d96d2007-03-22 12:13:20 -0400264 &extent_root->fs_info->pinned_radix,
Chris Mason9f5fae22007-03-20 14:38:32 -0400265 (void **)gang, 0,
266 ARRAY_SIZE(gang),
267 CTREE_EXTENT_PENDING_DEL);
Chris Masonfec577f2007-02-26 10:40:21 -0500268 if (!ret)
269 break;
270 for (i = 0; i < ret; i++) {
Chris Masond5719762007-03-23 10:01:08 -0400271 radix_tree_tag_set(radix, gang[i]->b_blocknr,
272 CTREE_EXTENT_PINNED);
273 radix_tree_tag_clear(radix, gang[i]->b_blocknr,
Chris Mason9f5fae22007-03-20 14:38:32 -0400274 CTREE_EXTENT_PENDING_DEL);
Chris Masond5719762007-03-23 10:01:08 -0400275 wret = __free_extent(trans, extent_root,
Chris Mason78fae272007-03-25 11:35:08 -0400276 gang[i]->b_blocknr, 1, 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400277 if (wret)
278 err = wret;
Chris Masonfec577f2007-02-26 10:40:21 -0500279 }
280 }
Chris Masone20d96d2007-03-22 12:13:20 -0400281 return err;
Chris Masonfec577f2007-02-26 10:40:21 -0500282}
283
284/*
285 * remove an extent from the root, returns 0 on success
286 */
Chris Masone089f052007-03-16 16:20:31 -0400287int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
288 *root, u64 blocknr, u64 num_blocks, int pin)
Chris Masonfec577f2007-02-26 10:40:21 -0500289{
Chris Mason9f5fae22007-03-20 14:38:32 -0400290 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Masone20d96d2007-03-22 12:13:20 -0400291 struct buffer_head *t;
Chris Masonfec577f2007-02-26 10:40:21 -0500292 int pending_ret;
293 int ret;
Chris Masona28ec192007-03-06 20:08:01 -0500294
295 if (root == extent_root) {
296 t = find_tree_block(root, blocknr);
Chris Masone20d96d2007-03-22 12:13:20 -0400297 pin_down_block(root, blocknr, CTREE_EXTENT_PENDING_DEL);
Chris Masona28ec192007-03-06 20:08:01 -0500298 return 0;
299 }
Chris Mason78fae272007-03-25 11:35:08 -0400300 ret = __free_extent(trans, root, blocknr, num_blocks, pin);
Chris Masone20d96d2007-03-22 12:13:20 -0400301 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
Chris Masonfec577f2007-02-26 10:40:21 -0500302 return ret ? ret : pending_ret;
303}
304
305/*
306 * walks the btree of allocated extents and find a hole of a given size.
307 * The key ins is changed to record the hole:
308 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -0400309 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -0500310 * ins->offset == number of blocks
311 * Any available blocks before search_start are skipped.
312 */
Chris Masone089f052007-03-16 16:20:31 -0400313static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
314 *orig_root, u64 num_blocks, u64 search_start, u64
315 search_end, struct btrfs_key *ins)
Chris Masonfec577f2007-02-26 10:40:21 -0500316{
Chris Mason234b63a2007-03-13 10:46:10 -0400317 struct btrfs_path path;
Chris Masone2fa7222007-03-12 16:22:34 -0400318 struct btrfs_key key;
Chris Masonfec577f2007-02-26 10:40:21 -0500319 int ret;
320 u64 hole_size = 0;
321 int slot = 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400322 u64 last_block = 0;
Chris Mason037e6392007-03-07 11:50:24 -0500323 u64 test_block;
Chris Masonfec577f2007-02-26 10:40:21 -0500324 int start_found;
Chris Mason234b63a2007-03-13 10:46:10 -0400325 struct btrfs_leaf *l;
Chris Mason9f5fae22007-03-20 14:38:32 -0400326 struct btrfs_root * root = orig_root->fs_info->extent_root;
Chris Mason0579da42007-03-07 16:15:30 -0500327 int total_needed = num_blocks;
Chris Masone20d96d2007-03-22 12:13:20 -0400328 int level;
Chris Masonfec577f2007-02-26 10:40:21 -0500329
Chris Masone20d96d2007-03-22 12:13:20 -0400330 level = btrfs_header_level(btrfs_buffer_header(root->node));
331 total_needed += (level + 1) * 3;
Chris Mason9f5fae22007-03-20 14:38:32 -0400332 if (root->fs_info->last_insert.objectid > search_start)
333 search_start = root->fs_info->last_insert.objectid;
Chris Mason62e27492007-03-15 12:56:47 -0400334
335 ins->flags = 0;
336 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
337
Chris Masonfec577f2007-02-26 10:40:21 -0500338check_failed:
Chris Mason234b63a2007-03-13 10:46:10 -0400339 btrfs_init_path(&path);
Chris Masonfec577f2007-02-26 10:40:21 -0500340 ins->objectid = search_start;
341 ins->offset = 0;
Chris Masonfec577f2007-02-26 10:40:21 -0500342 start_found = 0;
Chris Masone089f052007-03-16 16:20:31 -0400343 ret = btrfs_search_slot(trans, root, ins, &path, 0, 0);
Chris Mason0f70abe2007-02-28 16:46:22 -0500344 if (ret < 0)
345 goto error;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500346
Chris Mason0579da42007-03-07 16:15:30 -0500347 if (path.slots[0] > 0)
348 path.slots[0]--;
349
Chris Masonfec577f2007-02-26 10:40:21 -0500350 while (1) {
Chris Masone20d96d2007-03-22 12:13:20 -0400351 l = btrfs_buffer_leaf(path.nodes[0]);
Chris Masonfec577f2007-02-26 10:40:21 -0500352 slot = path.slots[0];
Chris Mason7518a232007-03-12 12:01:18 -0400353 if (slot >= btrfs_header_nritems(&l->header)) {
Chris Mason234b63a2007-03-13 10:46:10 -0400354 ret = btrfs_next_leaf(root, &path);
Chris Masonfec577f2007-02-26 10:40:21 -0500355 if (ret == 0)
356 continue;
Chris Mason0f70abe2007-02-28 16:46:22 -0500357 if (ret < 0)
358 goto error;
Chris Masonfec577f2007-02-26 10:40:21 -0500359 if (!start_found) {
360 ins->objectid = search_start;
Chris Mason037e6392007-03-07 11:50:24 -0500361 ins->offset = (u64)-1;
Chris Masonfec577f2007-02-26 10:40:21 -0500362 start_found = 1;
363 goto check_pending;
364 }
365 ins->objectid = last_block > search_start ?
366 last_block : search_start;
Chris Mason037e6392007-03-07 11:50:24 -0500367 ins->offset = (u64)-1;
Chris Masonfec577f2007-02-26 10:40:21 -0500368 goto check_pending;
369 }
Chris Masone2fa7222007-03-12 16:22:34 -0400370 btrfs_disk_key_to_cpu(&key, &l->items[slot].key);
371 if (key.objectid >= search_start) {
Chris Masonfec577f2007-02-26 10:40:21 -0500372 if (start_found) {
Chris Mason0579da42007-03-07 16:15:30 -0500373 if (last_block < search_start)
374 last_block = search_start;
Chris Masone2fa7222007-03-12 16:22:34 -0400375 hole_size = key.objectid - last_block;
Chris Mason037e6392007-03-07 11:50:24 -0500376 if (hole_size > total_needed) {
Chris Masonfec577f2007-02-26 10:40:21 -0500377 ins->objectid = last_block;
Chris Mason037e6392007-03-07 11:50:24 -0500378 ins->offset = hole_size;
Chris Masonfec577f2007-02-26 10:40:21 -0500379 goto check_pending;
380 }
Chris Mason0579da42007-03-07 16:15:30 -0500381 }
Chris Masonfec577f2007-02-26 10:40:21 -0500382 }
Chris Mason0579da42007-03-07 16:15:30 -0500383 start_found = 1;
Chris Masone2fa7222007-03-12 16:22:34 -0400384 last_block = key.objectid + key.offset;
Chris Masonfec577f2007-02-26 10:40:21 -0500385 path.slots[0]++;
386 }
387 // FIXME -ENOSPC
388check_pending:
389 /* we have to make sure we didn't find an extent that has already
390 * been allocated by the map tree or the original allocation
391 */
Chris Mason234b63a2007-03-13 10:46:10 -0400392 btrfs_release_path(root, &path);
Chris Masonfec577f2007-02-26 10:40:21 -0500393 BUG_ON(ins->objectid < search_start);
Chris Mason037e6392007-03-07 11:50:24 -0500394 for (test_block = ins->objectid;
395 test_block < ins->objectid + total_needed; test_block++) {
Chris Mason9f5fae22007-03-20 14:38:32 -0400396 if (radix_tree_lookup(&root->fs_info->pinned_radix,
397 test_block)) {
Chris Mason037e6392007-03-07 11:50:24 -0500398 search_start = test_block + 1;
Chris Masonfec577f2007-02-26 10:40:21 -0500399 goto check_failed;
400 }
401 }
Chris Mason9f5fae22007-03-20 14:38:32 -0400402 BUG_ON(root->fs_info->current_insert.offset);
403 root->fs_info->current_insert.offset = total_needed - num_blocks;
404 root->fs_info->current_insert.objectid = ins->objectid + num_blocks;
405 root->fs_info->current_insert.flags = 0;
406 root->fs_info->last_insert.objectid = ins->objectid;
Chris Mason037e6392007-03-07 11:50:24 -0500407 ins->offset = num_blocks;
Chris Masonfec577f2007-02-26 10:40:21 -0500408 return 0;
Chris Mason0f70abe2007-02-28 16:46:22 -0500409error:
Chris Mason234b63a2007-03-13 10:46:10 -0400410 btrfs_release_path(root, &path);
Chris Mason0f70abe2007-02-28 16:46:22 -0500411 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -0500412}
413
414/*
Chris Masonfec577f2007-02-26 10:40:21 -0500415 * finds a free extent and does all the dirty work required for allocation
416 * returns the key for the extent through ins, and a tree buffer for
417 * the first block of the extent through buf.
418 *
419 * returns 0 if everything worked, non-zero otherwise.
420 */
Chris Masone089f052007-03-16 16:20:31 -0400421static int alloc_extent(struct btrfs_trans_handle *trans, struct btrfs_root
422 *root, u64 num_blocks, u64 search_start, u64
423 search_end, u64 owner, struct btrfs_key *ins)
Chris Masonfec577f2007-02-26 10:40:21 -0500424{
425 int ret;
426 int pending_ret;
Chris Mason1261ec42007-03-20 20:35:03 -0400427 u64 super_blocks_used;
428 struct btrfs_fs_info *info = root->fs_info;
429 struct btrfs_root *extent_root = info->extent_root;
Chris Mason234b63a2007-03-13 10:46:10 -0400430 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -0500431
Chris Masoncf27e1e2007-03-13 09:49:06 -0400432 btrfs_set_extent_refs(&extent_item, 1);
433 btrfs_set_extent_owner(&extent_item, owner);
Chris Masonfec577f2007-02-26 10:40:21 -0500434
Chris Mason037e6392007-03-07 11:50:24 -0500435 if (root == extent_root) {
Chris Mason9f5fae22007-03-20 14:38:32 -0400436 BUG_ON(extent_root->fs_info->current_insert.offset == 0);
Chris Mason037e6392007-03-07 11:50:24 -0500437 BUG_ON(num_blocks != 1);
Chris Mason9f5fae22007-03-20 14:38:32 -0400438 BUG_ON(extent_root->fs_info->current_insert.flags ==
439 extent_root->fs_info->current_insert.offset);
Chris Mason037e6392007-03-07 11:50:24 -0500440 ins->offset = 1;
Chris Mason9f5fae22007-03-20 14:38:32 -0400441 ins->objectid = extent_root->fs_info->current_insert.objectid +
442 extent_root->fs_info->current_insert.flags++;
Chris Masonfec577f2007-02-26 10:40:21 -0500443 return 0;
444 }
Chris Masone089f052007-03-16 16:20:31 -0400445 ret = find_free_extent(trans, root, num_blocks, search_start,
Chris Mason037e6392007-03-07 11:50:24 -0500446 search_end, ins);
447 if (ret)
448 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -0500449
Chris Mason1261ec42007-03-20 20:35:03 -0400450 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
451 btrfs_set_super_blocks_used(info->disk_super, super_blocks_used +
452 num_blocks);
Chris Masone089f052007-03-16 16:20:31 -0400453 ret = btrfs_insert_item(trans, extent_root, ins, &extent_item,
454 sizeof(extent_item));
Chris Mason037e6392007-03-07 11:50:24 -0500455
Chris Masone089f052007-03-16 16:20:31 -0400456 finish_current_insert(trans, extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -0400457 pending_ret = del_pending_extents(trans, extent_root);
Chris Mason037e6392007-03-07 11:50:24 -0500458 if (ret)
459 return ret;
460 if (pending_ret)
461 return pending_ret;
462 return 0;
Chris Masonfec577f2007-02-26 10:40:21 -0500463}
464
465/*
466 * helper function to allocate a block for a given tree
467 * returns the tree buffer or NULL.
468 */
Chris Masone20d96d2007-03-22 12:13:20 -0400469struct buffer_head *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Chris Masone089f052007-03-16 16:20:31 -0400470 struct btrfs_root *root)
Chris Masonfec577f2007-02-26 10:40:21 -0500471{
Chris Masone2fa7222007-03-12 16:22:34 -0400472 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -0500473 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -0400474 struct buffer_head *buf;
Chris Masonfec577f2007-02-26 10:40:21 -0500475
Chris Masone089f052007-03-16 16:20:31 -0400476 ret = alloc_extent(trans, root, 1, 0, (unsigned long)-1,
Chris Masone20d96d2007-03-22 12:13:20 -0400477 btrfs_header_parentid(btrfs_buffer_header(root->node)), &ins);
Chris Masonfec577f2007-02-26 10:40:21 -0500478 if (ret) {
479 BUG();
480 return NULL;
481 }
Chris Mason037e6392007-03-07 11:50:24 -0500482 buf = find_tree_block(root, ins.objectid);
Chris Masondf2ce342007-03-23 11:00:45 -0400483 set_buffer_uptodate(buf);
Chris Masonfec577f2007-02-26 10:40:21 -0500484 return buf;
485}
Chris Masona28ec192007-03-06 20:08:01 -0500486
Chris Mason9aca1d52007-03-13 11:09:37 -0400487/*
488 * helper function for drop_snapshot, this walks down the tree dropping ref
489 * counts as it goes.
490 */
Chris Masone089f052007-03-16 16:20:31 -0400491static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
492 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -0500493{
Chris Masone20d96d2007-03-22 12:13:20 -0400494 struct buffer_head *next;
495 struct buffer_head *cur;
Chris Mason20524f02007-03-10 06:35:47 -0500496 u64 blocknr;
497 int ret;
498 u32 refs;
499
Chris Masone20d96d2007-03-22 12:13:20 -0400500 ret = lookup_block_ref(trans, root, path->nodes[*level]->b_blocknr,
Chris Masone089f052007-03-16 16:20:31 -0400501 &refs);
Chris Mason20524f02007-03-10 06:35:47 -0500502 BUG_ON(ret);
503 if (refs > 1)
504 goto out;
Chris Mason9aca1d52007-03-13 11:09:37 -0400505 /*
506 * walk down to the last node level and free all the leaves
507 */
Chris Mason20524f02007-03-10 06:35:47 -0500508 while(*level > 0) {
509 cur = path->nodes[*level];
Chris Mason7518a232007-03-12 12:01:18 -0400510 if (path->slots[*level] >=
Chris Masone20d96d2007-03-22 12:13:20 -0400511 btrfs_header_nritems(btrfs_buffer_header(cur)))
Chris Mason20524f02007-03-10 06:35:47 -0500512 break;
Chris Masone20d96d2007-03-22 12:13:20 -0400513 blocknr = btrfs_node_blockptr(btrfs_buffer_node(cur),
514 path->slots[*level]);
Chris Masone089f052007-03-16 16:20:31 -0400515 ret = lookup_block_ref(trans, root, blocknr, &refs);
Chris Mason20524f02007-03-10 06:35:47 -0500516 if (refs != 1 || *level == 1) {
517 path->slots[*level]++;
Chris Masone089f052007-03-16 16:20:31 -0400518 ret = btrfs_free_extent(trans, root, blocknr, 1, 1);
Chris Mason20524f02007-03-10 06:35:47 -0500519 BUG_ON(ret);
520 continue;
521 }
522 BUG_ON(ret);
523 next = read_tree_block(root, blocknr);
Chris Mason83e15a22007-03-12 09:03:27 -0400524 if (path->nodes[*level-1])
Chris Mason234b63a2007-03-13 10:46:10 -0400525 btrfs_block_release(root, path->nodes[*level-1]);
Chris Mason20524f02007-03-10 06:35:47 -0500526 path->nodes[*level-1] = next;
Chris Masone20d96d2007-03-22 12:13:20 -0400527 *level = btrfs_header_level(btrfs_buffer_header(next));
Chris Mason20524f02007-03-10 06:35:47 -0500528 path->slots[*level] = 0;
529 }
530out:
Chris Masone20d96d2007-03-22 12:13:20 -0400531 ret = btrfs_free_extent(trans, root, path->nodes[*level]->b_blocknr,
532 1, 1);
Chris Mason234b63a2007-03-13 10:46:10 -0400533 btrfs_block_release(root, path->nodes[*level]);
Chris Mason20524f02007-03-10 06:35:47 -0500534 path->nodes[*level] = NULL;
535 *level += 1;
536 BUG_ON(ret);
537 return 0;
538}
539
Chris Mason9aca1d52007-03-13 11:09:37 -0400540/*
541 * helper for dropping snapshots. This walks back up the tree in the path
542 * to find the first node higher up where we haven't yet gone through
543 * all the slots
544 */
Chris Masone089f052007-03-16 16:20:31 -0400545static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root
546 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -0500547{
548 int i;
549 int slot;
550 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400551 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Mason20524f02007-03-10 06:35:47 -0500552 slot = path->slots[i];
Chris Masone20d96d2007-03-22 12:13:20 -0400553 if (slot < btrfs_header_nritems(
554 btrfs_buffer_header(path->nodes[i])) - 1) {
Chris Mason20524f02007-03-10 06:35:47 -0500555 path->slots[i]++;
556 *level = i;
557 return 0;
558 } else {
Chris Masone089f052007-03-16 16:20:31 -0400559 ret = btrfs_free_extent(trans, root,
Chris Masone20d96d2007-03-22 12:13:20 -0400560 path->nodes[*level]->b_blocknr,
Chris Masone089f052007-03-16 16:20:31 -0400561 1, 1);
Chris Mason234b63a2007-03-13 10:46:10 -0400562 btrfs_block_release(root, path->nodes[*level]);
Chris Mason83e15a22007-03-12 09:03:27 -0400563 path->nodes[*level] = NULL;
Chris Mason20524f02007-03-10 06:35:47 -0500564 *level = i + 1;
565 BUG_ON(ret);
566 }
567 }
568 return 1;
569}
570
Chris Mason9aca1d52007-03-13 11:09:37 -0400571/*
572 * drop the reference count on the tree rooted at 'snap'. This traverses
573 * the tree freeing any blocks that have a ref count of zero after being
574 * decremented.
575 */
Chris Masone089f052007-03-16 16:20:31 -0400576int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masone20d96d2007-03-22 12:13:20 -0400577 *root, struct buffer_head *snap)
Chris Mason20524f02007-03-10 06:35:47 -0500578{
Chris Mason3768f362007-03-13 16:47:54 -0400579 int ret = 0;
Chris Mason9aca1d52007-03-13 11:09:37 -0400580 int wret;
Chris Mason20524f02007-03-10 06:35:47 -0500581 int level;
Chris Mason234b63a2007-03-13 10:46:10 -0400582 struct btrfs_path path;
Chris Mason20524f02007-03-10 06:35:47 -0500583 int i;
584 int orig_level;
585
Chris Mason234b63a2007-03-13 10:46:10 -0400586 btrfs_init_path(&path);
Chris Mason20524f02007-03-10 06:35:47 -0500587
Chris Masone20d96d2007-03-22 12:13:20 -0400588 level = btrfs_header_level(btrfs_buffer_header(snap));
Chris Mason20524f02007-03-10 06:35:47 -0500589 orig_level = level;
590 path.nodes[level] = snap;
591 path.slots[level] = 0;
592 while(1) {
Chris Masone089f052007-03-16 16:20:31 -0400593 wret = walk_down_tree(trans, root, &path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -0400594 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -0500595 break;
Chris Mason9aca1d52007-03-13 11:09:37 -0400596 if (wret < 0)
597 ret = wret;
598
Chris Masone089f052007-03-16 16:20:31 -0400599 wret = walk_up_tree(trans, root, &path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -0400600 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -0500601 break;
Chris Mason9aca1d52007-03-13 11:09:37 -0400602 if (wret < 0)
603 ret = wret;
Chris Mason20524f02007-03-10 06:35:47 -0500604 }
Chris Mason83e15a22007-03-12 09:03:27 -0400605 for (i = 0; i <= orig_level; i++) {
606 if (path.nodes[i]) {
Chris Mason234b63a2007-03-13 10:46:10 -0400607 btrfs_block_release(root, path.nodes[i]);
Chris Mason83e15a22007-03-12 09:03:27 -0400608 }
Chris Mason20524f02007-03-10 06:35:47 -0500609 }
Chris Mason9aca1d52007-03-13 11:09:37 -0400610 return ret;
Chris Mason20524f02007-03-10 06:35:47 -0500611}