blob: b2faad3e8791a0908826fdc5b813f776c767b6da [file] [log] [blame]
Chris Mason2e635a22007-03-21 11:12:56 -04001#include <linux/module.h>
Chris Masonfec577f2007-02-26 10:40:21 -05002#include "ctree.h"
3#include "disk-io.h"
4#include "print-tree.h"
Chris Masone089f052007-03-16 16:20:31 -04005#include "transaction.h"
Chris Masonfec577f2007-02-26 10:40:21 -05006
Chris Masone089f052007-03-16 16:20:31 -04007static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
8 *orig_root, u64 num_blocks, u64 search_start, u64
9 search_end, struct btrfs_key *ins);
10static int finish_current_insert(struct btrfs_trans_handle *trans, struct
11 btrfs_root *extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -040012static int del_pending_extents(struct btrfs_trans_handle *trans, struct
13 btrfs_root *extent_root);
Chris Masonfec577f2007-02-26 10:40:21 -050014
Chris Masonb18c6682007-04-17 13:26:50 -040015int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
16 struct btrfs_root *root,
17 u64 blocknr, u64 num_blocks)
Chris Mason02217ed2007-03-02 16:08:05 -050018{
Chris Mason5caf2a02007-04-02 11:20:42 -040019 struct btrfs_path *path;
Chris Mason02217ed2007-03-02 16:08:05 -050020 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -040021 struct btrfs_key key;
Chris Mason234b63a2007-03-13 10:46:10 -040022 struct btrfs_leaf *l;
23 struct btrfs_extent_item *item;
Chris Masone2fa7222007-03-12 16:22:34 -040024 struct btrfs_key ins;
Chris Masoncf27e1e2007-03-13 09:49:06 -040025 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -050026
Chris Mason9f5fae22007-03-20 14:38:32 -040027 find_free_extent(trans, root->fs_info->extent_root, 0, 0, (u64)-1,
28 &ins);
Chris Mason5caf2a02007-04-02 11:20:42 -040029 path = btrfs_alloc_path();
30 BUG_ON(!path);
31 btrfs_init_path(path);
Chris Mason02217ed2007-03-02 16:08:05 -050032 key.objectid = blocknr;
33 key.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -040034 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Mason6407bf62007-03-27 06:33:00 -040035 key.offset = num_blocks;
Chris Mason5caf2a02007-04-02 11:20:42 -040036 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -040037 0, 1);
Chris Masona429e512007-04-18 16:15:28 -040038 if (ret != 0) {
39printk("can't find block %Lu %Lu\n", blocknr, num_blocks);
Chris Masona28ec192007-03-06 20:08:01 -050040 BUG();
Chris Masona429e512007-04-18 16:15:28 -040041 }
Chris Mason02217ed2007-03-02 16:08:05 -050042 BUG_ON(ret != 0);
Chris Mason5caf2a02007-04-02 11:20:42 -040043 l = btrfs_buffer_leaf(path->nodes[0]);
44 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Masoncf27e1e2007-03-13 09:49:06 -040045 refs = btrfs_extent_refs(item);
46 btrfs_set_extent_refs(item, refs + 1);
Chris Mason5caf2a02007-04-02 11:20:42 -040047 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masona28ec192007-03-06 20:08:01 -050048
Chris Mason5caf2a02007-04-02 11:20:42 -040049 btrfs_release_path(root->fs_info->extent_root, path);
50 btrfs_free_path(path);
Chris Mason9f5fae22007-03-20 14:38:32 -040051 finish_current_insert(trans, root->fs_info->extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -040052 del_pending_extents(trans, root->fs_info->extent_root);
Chris Mason02217ed2007-03-02 16:08:05 -050053 return 0;
54}
55
Chris Masonb18c6682007-04-17 13:26:50 -040056static int lookup_extent_ref(struct btrfs_trans_handle *trans,
57 struct btrfs_root *root, u64 blocknr,
58 u64 num_blocks, u32 *refs)
Chris Masona28ec192007-03-06 20:08:01 -050059{
Chris Mason5caf2a02007-04-02 11:20:42 -040060 struct btrfs_path *path;
Chris Masona28ec192007-03-06 20:08:01 -050061 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -040062 struct btrfs_key key;
Chris Mason234b63a2007-03-13 10:46:10 -040063 struct btrfs_leaf *l;
64 struct btrfs_extent_item *item;
Chris Mason5caf2a02007-04-02 11:20:42 -040065
66 path = btrfs_alloc_path();
67 btrfs_init_path(path);
Chris Masona28ec192007-03-06 20:08:01 -050068 key.objectid = blocknr;
Chris Mason6407bf62007-03-27 06:33:00 -040069 key.offset = num_blocks;
Chris Mason62e27492007-03-15 12:56:47 -040070 key.flags = 0;
71 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Mason5caf2a02007-04-02 11:20:42 -040072 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -040073 0, 0);
Chris Masona28ec192007-03-06 20:08:01 -050074 if (ret != 0)
75 BUG();
Chris Mason5caf2a02007-04-02 11:20:42 -040076 l = btrfs_buffer_leaf(path->nodes[0]);
77 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Masoncf27e1e2007-03-13 09:49:06 -040078 *refs = btrfs_extent_refs(item);
Chris Mason5caf2a02007-04-02 11:20:42 -040079 btrfs_release_path(root->fs_info->extent_root, path);
80 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -050081 return 0;
82}
83
Chris Masonc5739bb2007-04-10 09:27:04 -040084int btrfs_inc_root_ref(struct btrfs_trans_handle *trans,
85 struct btrfs_root *root)
86{
Chris Masonb18c6682007-04-17 13:26:50 -040087 return btrfs_inc_extent_ref(trans, root, bh_blocknr(root->node), 1);
Chris Masonc5739bb2007-04-10 09:27:04 -040088}
89
Chris Masone089f052007-03-16 16:20:31 -040090int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Masone20d96d2007-03-22 12:13:20 -040091 struct buffer_head *buf)
Chris Mason02217ed2007-03-02 16:08:05 -050092{
93 u64 blocknr;
Chris Masone20d96d2007-03-22 12:13:20 -040094 struct btrfs_node *buf_node;
Chris Mason6407bf62007-03-27 06:33:00 -040095 struct btrfs_leaf *buf_leaf;
96 struct btrfs_disk_key *key;
97 struct btrfs_file_extent_item *fi;
Chris Mason02217ed2007-03-02 16:08:05 -050098 int i;
Chris Mason6407bf62007-03-27 06:33:00 -040099 int leaf;
100 int ret;
Chris Masona28ec192007-03-06 20:08:01 -0500101
Chris Mason3768f362007-03-13 16:47:54 -0400102 if (!root->ref_cows)
Chris Masona28ec192007-03-06 20:08:01 -0500103 return 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400104 buf_node = btrfs_buffer_node(buf);
Chris Mason6407bf62007-03-27 06:33:00 -0400105 leaf = btrfs_is_leaf(buf_node);
106 buf_leaf = btrfs_buffer_leaf(buf);
Chris Masone20d96d2007-03-22 12:13:20 -0400107 for (i = 0; i < btrfs_header_nritems(&buf_node->header); i++) {
Chris Mason6407bf62007-03-27 06:33:00 -0400108 if (leaf) {
109 key = &buf_leaf->items[i].key;
110 if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY)
111 continue;
112 fi = btrfs_item_ptr(buf_leaf, i,
113 struct btrfs_file_extent_item);
Chris Mason236454d2007-04-19 13:37:44 -0400114 if (btrfs_file_extent_type(fi) ==
115 BTRFS_FILE_EXTENT_INLINE)
116 continue;
Chris Masonb18c6682007-04-17 13:26:50 -0400117 ret = btrfs_inc_extent_ref(trans, root,
Chris Mason6407bf62007-03-27 06:33:00 -0400118 btrfs_file_extent_disk_blocknr(fi),
119 btrfs_file_extent_disk_num_blocks(fi));
120 BUG_ON(ret);
121 } else {
122 blocknr = btrfs_node_blockptr(buf_node, i);
Chris Masonb18c6682007-04-17 13:26:50 -0400123 ret = btrfs_inc_extent_ref(trans, root, blocknr, 1);
Chris Mason6407bf62007-03-27 06:33:00 -0400124 BUG_ON(ret);
125 }
Chris Mason02217ed2007-03-02 16:08:05 -0500126 }
127 return 0;
128}
129
Chris Masone089f052007-03-16 16:20:31 -0400130int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, struct
131 btrfs_root *root)
Chris Masona28ec192007-03-06 20:08:01 -0500132{
Chris Mason8ef97622007-03-26 10:15:30 -0400133 unsigned long gang[8];
Chris Mason88fd1462007-03-16 08:56:18 -0400134 u64 first = 0;
Chris Masona28ec192007-03-06 20:08:01 -0500135 int ret;
136 int i;
Chris Mason8ef97622007-03-26 10:15:30 -0400137 struct radix_tree_root *pinned_radix = &root->fs_info->pinned_radix;
Chris Masona28ec192007-03-06 20:08:01 -0500138
139 while(1) {
Chris Mason8ef97622007-03-26 10:15:30 -0400140 ret = find_first_radix_bit(pinned_radix, gang,
141 ARRAY_SIZE(gang));
Chris Masona28ec192007-03-06 20:08:01 -0500142 if (!ret)
143 break;
Chris Mason88fd1462007-03-16 08:56:18 -0400144 if (!first)
Chris Mason8ef97622007-03-26 10:15:30 -0400145 first = gang[0];
Chris Mason0579da42007-03-07 16:15:30 -0500146 for (i = 0; i < ret; i++) {
Chris Mason8ef97622007-03-26 10:15:30 -0400147 clear_radix_bit(pinned_radix, gang[i]);
Chris Mason0579da42007-03-07 16:15:30 -0500148 }
Chris Masona28ec192007-03-06 20:08:01 -0500149 }
Chris Masond5719762007-03-23 10:01:08 -0400150 if (root->fs_info->last_insert.objectid > first)
151 root->fs_info->last_insert.objectid = first;
Chris Mason9f5fae22007-03-20 14:38:32 -0400152 root->fs_info->last_insert.offset = 0;
Chris Masona28ec192007-03-06 20:08:01 -0500153 return 0;
154}
155
Chris Masone089f052007-03-16 16:20:31 -0400156static int finish_current_insert(struct btrfs_trans_handle *trans, struct
157 btrfs_root *extent_root)
Chris Mason037e6392007-03-07 11:50:24 -0500158{
Chris Masone2fa7222007-03-12 16:22:34 -0400159 struct btrfs_key ins;
Chris Mason234b63a2007-03-13 10:46:10 -0400160 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -0500161 int i;
162 int ret;
Chris Mason1261ec42007-03-20 20:35:03 -0400163 u64 super_blocks_used;
164 struct btrfs_fs_info *info = extent_root->fs_info;
Chris Mason037e6392007-03-07 11:50:24 -0500165
Chris Masoncf27e1e2007-03-13 09:49:06 -0400166 btrfs_set_extent_refs(&extent_item, 1);
Chris Mason037e6392007-03-07 11:50:24 -0500167 ins.offset = 1;
168 ins.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -0400169 btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
Chris Mason037e6392007-03-07 11:50:24 -0500170
Chris Mason9f5fae22007-03-20 14:38:32 -0400171 for (i = 0; i < extent_root->fs_info->current_insert.flags; i++) {
172 ins.objectid = extent_root->fs_info->current_insert.objectid +
173 i;
Chris Mason1261ec42007-03-20 20:35:03 -0400174 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
175 btrfs_set_super_blocks_used(info->disk_super,
176 super_blocks_used + 1);
Chris Masone089f052007-03-16 16:20:31 -0400177 ret = btrfs_insert_item(trans, extent_root, &ins, &extent_item,
178 sizeof(extent_item));
Chris Mason037e6392007-03-07 11:50:24 -0500179 BUG_ON(ret);
180 }
Chris Mason9f5fae22007-03-20 14:38:32 -0400181 extent_root->fs_info->current_insert.offset = 0;
Chris Mason037e6392007-03-07 11:50:24 -0500182 return 0;
183}
184
Chris Mason8ef97622007-03-26 10:15:30 -0400185static int pin_down_block(struct btrfs_root *root, u64 blocknr, int pending)
Chris Masone20d96d2007-03-22 12:13:20 -0400186{
187 int err;
Chris Mason78fae272007-03-25 11:35:08 -0400188 struct btrfs_header *header;
Chris Mason8ef97622007-03-26 10:15:30 -0400189 struct buffer_head *bh;
Chris Mason78fae272007-03-25 11:35:08 -0400190
Chris Masonf4b9aa82007-03-27 11:05:53 -0400191 if (!pending) {
Chris Masond98237b2007-03-28 13:57:48 -0400192 bh = btrfs_find_tree_block(root, blocknr);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400193 if (bh) {
194 if (buffer_uptodate(bh)) {
195 u64 transid =
196 root->fs_info->running_transaction->transid;
197 header = btrfs_buffer_header(bh);
198 if (btrfs_header_generation(header) ==
199 transid) {
200 btrfs_block_release(root, bh);
201 return 0;
202 }
Chris Masonf4b9aa82007-03-27 11:05:53 -0400203 }
Chris Masond6025572007-03-30 14:27:56 -0400204 btrfs_block_release(root, bh);
Chris Mason8ef97622007-03-26 10:15:30 -0400205 }
Chris Mason8ef97622007-03-26 10:15:30 -0400206 err = set_radix_bit(&root->fs_info->pinned_radix, blocknr);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400207 } else {
208 err = set_radix_bit(&root->fs_info->pending_del_radix, blocknr);
209 }
Chris Mason8ef97622007-03-26 10:15:30 -0400210 BUG_ON(err);
Chris Masone20d96d2007-03-22 12:13:20 -0400211 return 0;
212}
213
Chris Masona28ec192007-03-06 20:08:01 -0500214/*
215 * remove an extent from the root, returns 0 on success
216 */
Chris Masone089f052007-03-16 16:20:31 -0400217static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason78fae272007-03-25 11:35:08 -0400218 *root, u64 blocknr, u64 num_blocks, int pin)
Chris Masona28ec192007-03-06 20:08:01 -0500219{
Chris Mason5caf2a02007-04-02 11:20:42 -0400220 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -0400221 struct btrfs_key key;
Chris Mason1261ec42007-03-20 20:35:03 -0400222 struct btrfs_fs_info *info = root->fs_info;
223 struct btrfs_root *extent_root = info->extent_root;
Chris Masona28ec192007-03-06 20:08:01 -0500224 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400225 struct btrfs_extent_item *ei;
Chris Masone2fa7222007-03-12 16:22:34 -0400226 struct btrfs_key ins;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400227 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500228
Chris Masona28ec192007-03-06 20:08:01 -0500229 key.objectid = blocknr;
230 key.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -0400231 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masona28ec192007-03-06 20:08:01 -0500232 key.offset = num_blocks;
233
Chris Masone089f052007-03-16 16:20:31 -0400234 find_free_extent(trans, root, 0, 0, (u64)-1, &ins);
Chris Mason5caf2a02007-04-02 11:20:42 -0400235 path = btrfs_alloc_path();
236 BUG_ON(!path);
237 btrfs_init_path(path);
Chris Mason5f26f772007-04-05 10:38:44 -0400238
Chris Mason5caf2a02007-04-02 11:20:42 -0400239 ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
Chris Masona28ec192007-03-06 20:08:01 -0500240 if (ret) {
Chris Mason2e635a22007-03-21 11:12:56 -0400241 printk("failed to find %Lu\n", key.objectid);
Chris Mason234b63a2007-03-13 10:46:10 -0400242 btrfs_print_tree(extent_root, extent_root->node);
Chris Mason2e635a22007-03-21 11:12:56 -0400243 printk("failed to find %Lu\n", key.objectid);
Chris Masona28ec192007-03-06 20:08:01 -0500244 BUG();
245 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400246 ei = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
Chris Mason123abc82007-03-14 14:14:43 -0400247 struct btrfs_extent_item);
Chris Masona28ec192007-03-06 20:08:01 -0500248 BUG_ON(ei->refs == 0);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400249 refs = btrfs_extent_refs(ei) - 1;
250 btrfs_set_extent_refs(ei, refs);
Chris Mason5caf2a02007-04-02 11:20:42 -0400251 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400252 if (refs == 0) {
Chris Mason1261ec42007-03-20 20:35:03 -0400253 u64 super_blocks_used;
Chris Mason78fae272007-03-25 11:35:08 -0400254
255 if (pin) {
Chris Mason8ef97622007-03-26 10:15:30 -0400256 ret = pin_down_block(root, blocknr, 0);
Chris Mason78fae272007-03-25 11:35:08 -0400257 BUG_ON(ret);
258 }
259
Chris Mason1261ec42007-03-20 20:35:03 -0400260 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
261 btrfs_set_super_blocks_used(info->disk_super,
262 super_blocks_used - num_blocks);
Chris Mason5caf2a02007-04-02 11:20:42 -0400263 ret = btrfs_del_item(trans, extent_root, path);
Chris Masona28ec192007-03-06 20:08:01 -0500264 if (ret)
265 BUG();
266 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400267 btrfs_release_path(extent_root, path);
268 btrfs_free_path(path);
Chris Masone089f052007-03-16 16:20:31 -0400269 finish_current_insert(trans, extent_root);
Chris Masona28ec192007-03-06 20:08:01 -0500270 return ret;
271}
272
273/*
Chris Masonfec577f2007-02-26 10:40:21 -0500274 * find all the blocks marked as pending in the radix tree and remove
275 * them from the extent map
276 */
Chris Masone089f052007-03-16 16:20:31 -0400277static int del_pending_extents(struct btrfs_trans_handle *trans, struct
278 btrfs_root *extent_root)
Chris Masonfec577f2007-02-26 10:40:21 -0500279{
280 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -0400281 int wret;
282 int err = 0;
Chris Mason8ef97622007-03-26 10:15:30 -0400283 unsigned long gang[4];
Chris Masonfec577f2007-02-26 10:40:21 -0500284 int i;
Chris Mason8ef97622007-03-26 10:15:30 -0400285 struct radix_tree_root *pending_radix;
286 struct radix_tree_root *pinned_radix;
287
288 pending_radix = &extent_root->fs_info->pending_del_radix;
289 pinned_radix = &extent_root->fs_info->pinned_radix;
Chris Masonfec577f2007-02-26 10:40:21 -0500290
291 while(1) {
Chris Mason8ef97622007-03-26 10:15:30 -0400292 ret = find_first_radix_bit(pending_radix, gang,
293 ARRAY_SIZE(gang));
Chris Masonfec577f2007-02-26 10:40:21 -0500294 if (!ret)
295 break;
296 for (i = 0; i < ret; i++) {
Chris Mason8ef97622007-03-26 10:15:30 -0400297 wret = set_radix_bit(pinned_radix, gang[i]);
298 BUG_ON(wret);
299 wret = clear_radix_bit(pending_radix, gang[i]);
300 BUG_ON(wret);
Chris Masond5719762007-03-23 10:01:08 -0400301 wret = __free_extent(trans, extent_root,
Chris Mason8ef97622007-03-26 10:15:30 -0400302 gang[i], 1, 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400303 if (wret)
304 err = wret;
Chris Masonfec577f2007-02-26 10:40:21 -0500305 }
306 }
Chris Masone20d96d2007-03-22 12:13:20 -0400307 return err;
Chris Masonfec577f2007-02-26 10:40:21 -0500308}
309
310/*
311 * remove an extent from the root, returns 0 on success
312 */
Chris Masone089f052007-03-16 16:20:31 -0400313int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
314 *root, u64 blocknr, u64 num_blocks, int pin)
Chris Masonfec577f2007-02-26 10:40:21 -0500315{
Chris Mason9f5fae22007-03-20 14:38:32 -0400316 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Masonfec577f2007-02-26 10:40:21 -0500317 int pending_ret;
318 int ret;
Chris Masona28ec192007-03-06 20:08:01 -0500319
320 if (root == extent_root) {
Chris Mason8ef97622007-03-26 10:15:30 -0400321 pin_down_block(root, blocknr, 1);
Chris Masona28ec192007-03-06 20:08:01 -0500322 return 0;
323 }
Chris Mason78fae272007-03-25 11:35:08 -0400324 ret = __free_extent(trans, root, blocknr, num_blocks, pin);
Chris Masone20d96d2007-03-22 12:13:20 -0400325 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
Chris Masonfec577f2007-02-26 10:40:21 -0500326 return ret ? ret : pending_ret;
327}
328
329/*
330 * walks the btree of allocated extents and find a hole of a given size.
331 * The key ins is changed to record the hole:
332 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -0400333 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -0500334 * ins->offset == number of blocks
335 * Any available blocks before search_start are skipped.
336 */
Chris Masone089f052007-03-16 16:20:31 -0400337static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
338 *orig_root, u64 num_blocks, u64 search_start, u64
339 search_end, struct btrfs_key *ins)
Chris Masonfec577f2007-02-26 10:40:21 -0500340{
Chris Mason5caf2a02007-04-02 11:20:42 -0400341 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -0400342 struct btrfs_key key;
Chris Masonfec577f2007-02-26 10:40:21 -0500343 int ret;
344 u64 hole_size = 0;
345 int slot = 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400346 u64 last_block = 0;
Chris Mason037e6392007-03-07 11:50:24 -0500347 u64 test_block;
Chris Masonfec577f2007-02-26 10:40:21 -0500348 int start_found;
Chris Mason234b63a2007-03-13 10:46:10 -0400349 struct btrfs_leaf *l;
Chris Mason9f5fae22007-03-20 14:38:32 -0400350 struct btrfs_root * root = orig_root->fs_info->extent_root;
Chris Mason0579da42007-03-07 16:15:30 -0500351 int total_needed = num_blocks;
Chris Masone20d96d2007-03-22 12:13:20 -0400352 int level;
Chris Masonfec577f2007-02-26 10:40:21 -0500353
Chris Masonb1a4d962007-04-04 15:27:52 -0400354 path = btrfs_alloc_path();
355 ins->flags = 0;
356 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
357
Chris Masone20d96d2007-03-22 12:13:20 -0400358 level = btrfs_header_level(btrfs_buffer_header(root->node));
359 total_needed += (level + 1) * 3;
Chris Masonb1a4d962007-04-04 15:27:52 -0400360 if (root->fs_info->last_insert.objectid == 0 && search_end == (u64)-1) {
361 struct btrfs_disk_key *last_key;
362 btrfs_init_path(path);
363 ins->objectid = (u64)-1;
364 ins->offset = (u64)-1;
365 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
366 if (ret < 0)
367 goto error;
368 BUG_ON(ret == 0);
369 if (path->slots[0] > 0)
370 path->slots[0]--;
371 l = btrfs_buffer_leaf(path->nodes[0]);
372 last_key = &l->items[path->slots[0]].key;
373 search_start = btrfs_disk_key_objectid(last_key);
374 }
Chris Mason9f5fae22007-03-20 14:38:32 -0400375 if (root->fs_info->last_insert.objectid > search_start)
376 search_start = root->fs_info->last_insert.objectid;
Chris Mason62e27492007-03-15 12:56:47 -0400377
Chris Masonfec577f2007-02-26 10:40:21 -0500378check_failed:
Chris Mason5caf2a02007-04-02 11:20:42 -0400379 btrfs_init_path(path);
Chris Masonfec577f2007-02-26 10:40:21 -0500380 ins->objectid = search_start;
381 ins->offset = 0;
Chris Masonfec577f2007-02-26 10:40:21 -0500382 start_found = 0;
Chris Mason5caf2a02007-04-02 11:20:42 -0400383 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
Chris Mason0f70abe2007-02-28 16:46:22 -0500384 if (ret < 0)
385 goto error;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500386
Chris Mason5caf2a02007-04-02 11:20:42 -0400387 if (path->slots[0] > 0)
388 path->slots[0]--;
Chris Mason0579da42007-03-07 16:15:30 -0500389
Chris Masonfec577f2007-02-26 10:40:21 -0500390 while (1) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400391 l = btrfs_buffer_leaf(path->nodes[0]);
392 slot = path->slots[0];
Chris Mason7518a232007-03-12 12:01:18 -0400393 if (slot >= btrfs_header_nritems(&l->header)) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400394 ret = btrfs_next_leaf(root, path);
Chris Masonfec577f2007-02-26 10:40:21 -0500395 if (ret == 0)
396 continue;
Chris Mason0f70abe2007-02-28 16:46:22 -0500397 if (ret < 0)
398 goto error;
Chris Masonfec577f2007-02-26 10:40:21 -0500399 if (!start_found) {
400 ins->objectid = search_start;
Chris Mason037e6392007-03-07 11:50:24 -0500401 ins->offset = (u64)-1;
Chris Masonfec577f2007-02-26 10:40:21 -0500402 start_found = 1;
403 goto check_pending;
404 }
405 ins->objectid = last_block > search_start ?
406 last_block : search_start;
Chris Mason037e6392007-03-07 11:50:24 -0500407 ins->offset = (u64)-1;
Chris Masonfec577f2007-02-26 10:40:21 -0500408 goto check_pending;
409 }
Chris Masone2fa7222007-03-12 16:22:34 -0400410 btrfs_disk_key_to_cpu(&key, &l->items[slot].key);
411 if (key.objectid >= search_start) {
Chris Masonfec577f2007-02-26 10:40:21 -0500412 if (start_found) {
Chris Mason0579da42007-03-07 16:15:30 -0500413 if (last_block < search_start)
414 last_block = search_start;
Chris Masone2fa7222007-03-12 16:22:34 -0400415 hole_size = key.objectid - last_block;
Chris Mason037e6392007-03-07 11:50:24 -0500416 if (hole_size > total_needed) {
Chris Masonfec577f2007-02-26 10:40:21 -0500417 ins->objectid = last_block;
Chris Mason037e6392007-03-07 11:50:24 -0500418 ins->offset = hole_size;
Chris Masonfec577f2007-02-26 10:40:21 -0500419 goto check_pending;
420 }
Chris Mason0579da42007-03-07 16:15:30 -0500421 }
Chris Masonfec577f2007-02-26 10:40:21 -0500422 }
Chris Mason0579da42007-03-07 16:15:30 -0500423 start_found = 1;
Chris Masone2fa7222007-03-12 16:22:34 -0400424 last_block = key.objectid + key.offset;
Chris Mason5caf2a02007-04-02 11:20:42 -0400425 path->slots[0]++;
Chris Masonfec577f2007-02-26 10:40:21 -0500426 }
427 // FIXME -ENOSPC
428check_pending:
429 /* we have to make sure we didn't find an extent that has already
430 * been allocated by the map tree or the original allocation
431 */
Chris Mason5caf2a02007-04-02 11:20:42 -0400432 btrfs_release_path(root, path);
Chris Masonfec577f2007-02-26 10:40:21 -0500433 BUG_ON(ins->objectid < search_start);
Chris Mason037e6392007-03-07 11:50:24 -0500434 for (test_block = ins->objectid;
435 test_block < ins->objectid + total_needed; test_block++) {
Chris Mason8ef97622007-03-26 10:15:30 -0400436 if (test_radix_bit(&root->fs_info->pinned_radix,
Chris Mason9f5fae22007-03-20 14:38:32 -0400437 test_block)) {
Chris Mason037e6392007-03-07 11:50:24 -0500438 search_start = test_block + 1;
Chris Masonfec577f2007-02-26 10:40:21 -0500439 goto check_failed;
440 }
441 }
Chris Mason9f5fae22007-03-20 14:38:32 -0400442 BUG_ON(root->fs_info->current_insert.offset);
443 root->fs_info->current_insert.offset = total_needed - num_blocks;
444 root->fs_info->current_insert.objectid = ins->objectid + num_blocks;
445 root->fs_info->current_insert.flags = 0;
446 root->fs_info->last_insert.objectid = ins->objectid;
Chris Mason037e6392007-03-07 11:50:24 -0500447 ins->offset = num_blocks;
Chris Mason5caf2a02007-04-02 11:20:42 -0400448 btrfs_free_path(path);
Chris Masonfec577f2007-02-26 10:40:21 -0500449 return 0;
Chris Mason0f70abe2007-02-28 16:46:22 -0500450error:
Chris Mason5caf2a02007-04-02 11:20:42 -0400451 btrfs_release_path(root, path);
452 btrfs_free_path(path);
Chris Mason0f70abe2007-02-28 16:46:22 -0500453 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -0500454}
455
456/*
Chris Masonfec577f2007-02-26 10:40:21 -0500457 * finds a free extent and does all the dirty work required for allocation
458 * returns the key for the extent through ins, and a tree buffer for
459 * the first block of the extent through buf.
460 *
461 * returns 0 if everything worked, non-zero otherwise.
462 */
Chris Masondee26a92007-03-26 16:00:06 -0400463int btrfs_alloc_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masone089f052007-03-16 16:20:31 -0400464 *root, u64 num_blocks, u64 search_start, u64
Chris Masond0dbc6242007-04-10 12:36:36 -0400465 search_end, struct btrfs_key *ins)
Chris Masonfec577f2007-02-26 10:40:21 -0500466{
467 int ret;
468 int pending_ret;
Chris Mason1261ec42007-03-20 20:35:03 -0400469 u64 super_blocks_used;
470 struct btrfs_fs_info *info = root->fs_info;
471 struct btrfs_root *extent_root = info->extent_root;
Chris Mason234b63a2007-03-13 10:46:10 -0400472 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -0500473
Chris Masoncf27e1e2007-03-13 09:49:06 -0400474 btrfs_set_extent_refs(&extent_item, 1);
Chris Masonfec577f2007-02-26 10:40:21 -0500475
Chris Mason037e6392007-03-07 11:50:24 -0500476 if (root == extent_root) {
Chris Mason9f5fae22007-03-20 14:38:32 -0400477 BUG_ON(extent_root->fs_info->current_insert.offset == 0);
Chris Mason037e6392007-03-07 11:50:24 -0500478 BUG_ON(num_blocks != 1);
Chris Mason9f5fae22007-03-20 14:38:32 -0400479 BUG_ON(extent_root->fs_info->current_insert.flags ==
480 extent_root->fs_info->current_insert.offset);
Chris Mason037e6392007-03-07 11:50:24 -0500481 ins->offset = 1;
Chris Mason9f5fae22007-03-20 14:38:32 -0400482 ins->objectid = extent_root->fs_info->current_insert.objectid +
483 extent_root->fs_info->current_insert.flags++;
Chris Masonfec577f2007-02-26 10:40:21 -0500484 return 0;
485 }
Chris Masone089f052007-03-16 16:20:31 -0400486 ret = find_free_extent(trans, root, num_blocks, search_start,
Chris Mason037e6392007-03-07 11:50:24 -0500487 search_end, ins);
488 if (ret)
489 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -0500490
Chris Mason1261ec42007-03-20 20:35:03 -0400491 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
492 btrfs_set_super_blocks_used(info->disk_super, super_blocks_used +
493 num_blocks);
Chris Masone089f052007-03-16 16:20:31 -0400494 ret = btrfs_insert_item(trans, extent_root, ins, &extent_item,
495 sizeof(extent_item));
Chris Mason037e6392007-03-07 11:50:24 -0500496
Chris Masone089f052007-03-16 16:20:31 -0400497 finish_current_insert(trans, extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -0400498 pending_ret = del_pending_extents(trans, extent_root);
Chris Mason037e6392007-03-07 11:50:24 -0500499 if (ret)
500 return ret;
501 if (pending_ret)
502 return pending_ret;
503 return 0;
Chris Masonfec577f2007-02-26 10:40:21 -0500504}
505
506/*
507 * helper function to allocate a block for a given tree
508 * returns the tree buffer or NULL.
509 */
Chris Masone20d96d2007-03-22 12:13:20 -0400510struct buffer_head *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Chris Masone089f052007-03-16 16:20:31 -0400511 struct btrfs_root *root)
Chris Masonfec577f2007-02-26 10:40:21 -0500512{
Chris Masone2fa7222007-03-12 16:22:34 -0400513 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -0500514 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -0400515 struct buffer_head *buf;
Chris Masonfec577f2007-02-26 10:40:21 -0500516
Chris Masond0dbc6242007-04-10 12:36:36 -0400517 ret = btrfs_alloc_extent(trans, root, 1, 0, (unsigned long)-1, &ins);
Chris Masonfec577f2007-02-26 10:40:21 -0500518 if (ret) {
519 BUG();
520 return NULL;
521 }
Chris Masond98237b2007-03-28 13:57:48 -0400522 buf = btrfs_find_create_tree_block(root, ins.objectid);
Chris Masondf2ce342007-03-23 11:00:45 -0400523 set_buffer_uptodate(buf);
Chris Masonfec577f2007-02-26 10:40:21 -0500524 return buf;
525}
Chris Masona28ec192007-03-06 20:08:01 -0500526
Chris Mason6407bf62007-03-27 06:33:00 -0400527static int drop_leaf_ref(struct btrfs_trans_handle *trans,
528 struct btrfs_root *root, struct buffer_head *cur)
529{
530 struct btrfs_disk_key *key;
531 struct btrfs_leaf *leaf;
532 struct btrfs_file_extent_item *fi;
533 int i;
534 int nritems;
535 int ret;
536
537 BUG_ON(!btrfs_is_leaf(btrfs_buffer_node(cur)));
538 leaf = btrfs_buffer_leaf(cur);
539 nritems = btrfs_header_nritems(&leaf->header);
540 for (i = 0; i < nritems; i++) {
541 key = &leaf->items[i].key;
542 if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY)
543 continue;
544 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
Chris Mason236454d2007-04-19 13:37:44 -0400545 if (btrfs_file_extent_type(fi) == BTRFS_FILE_EXTENT_INLINE)
546 continue;
Chris Mason6407bf62007-03-27 06:33:00 -0400547 /*
548 * FIXME make sure to insert a trans record that
549 * repeats the snapshot del on crash
550 */
551 ret = btrfs_free_extent(trans, root,
552 btrfs_file_extent_disk_blocknr(fi),
553 btrfs_file_extent_disk_num_blocks(fi),
554 0);
555 BUG_ON(ret);
556 }
557 return 0;
558}
559
Chris Mason9aca1d52007-03-13 11:09:37 -0400560/*
561 * helper function for drop_snapshot, this walks down the tree dropping ref
562 * counts as it goes.
563 */
Chris Masone089f052007-03-16 16:20:31 -0400564static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
565 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -0500566{
Chris Masone20d96d2007-03-22 12:13:20 -0400567 struct buffer_head *next;
568 struct buffer_head *cur;
Chris Mason20524f02007-03-10 06:35:47 -0500569 u64 blocknr;
570 int ret;
571 u32 refs;
572
Chris Mason5caf2a02007-04-02 11:20:42 -0400573 WARN_ON(*level < 0);
574 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Masonb18c6682007-04-17 13:26:50 -0400575 ret = lookup_extent_ref(trans, root, bh_blocknr(path->nodes[*level]),
Chris Mason6407bf62007-03-27 06:33:00 -0400576 1, &refs);
Chris Mason20524f02007-03-10 06:35:47 -0500577 BUG_ON(ret);
578 if (refs > 1)
579 goto out;
Chris Mason9aca1d52007-03-13 11:09:37 -0400580 /*
581 * walk down to the last node level and free all the leaves
582 */
Chris Mason6407bf62007-03-27 06:33:00 -0400583 while(*level >= 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400584 WARN_ON(*level < 0);
585 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason20524f02007-03-10 06:35:47 -0500586 cur = path->nodes[*level];
Chris Mason2c90e5d2007-04-02 10:50:19 -0400587 if (btrfs_header_level(btrfs_buffer_header(cur)) != *level)
588 WARN_ON(1);
Chris Mason7518a232007-03-12 12:01:18 -0400589 if (path->slots[*level] >=
Chris Masone20d96d2007-03-22 12:13:20 -0400590 btrfs_header_nritems(btrfs_buffer_header(cur)))
Chris Mason20524f02007-03-10 06:35:47 -0500591 break;
Chris Mason6407bf62007-03-27 06:33:00 -0400592 if (*level == 0) {
593 ret = drop_leaf_ref(trans, root, cur);
594 BUG_ON(ret);
595 break;
596 }
Chris Masone20d96d2007-03-22 12:13:20 -0400597 blocknr = btrfs_node_blockptr(btrfs_buffer_node(cur),
598 path->slots[*level]);
Chris Masonb18c6682007-04-17 13:26:50 -0400599 ret = lookup_extent_ref(trans, root, blocknr, 1, &refs);
Chris Mason6407bf62007-03-27 06:33:00 -0400600 BUG_ON(ret);
601 if (refs != 1) {
Chris Mason20524f02007-03-10 06:35:47 -0500602 path->slots[*level]++;
Chris Masone089f052007-03-16 16:20:31 -0400603 ret = btrfs_free_extent(trans, root, blocknr, 1, 1);
Chris Mason20524f02007-03-10 06:35:47 -0500604 BUG_ON(ret);
605 continue;
606 }
Chris Mason20524f02007-03-10 06:35:47 -0500607 next = read_tree_block(root, blocknr);
Chris Mason5caf2a02007-04-02 11:20:42 -0400608 WARN_ON(*level <= 0);
Chris Mason83e15a22007-03-12 09:03:27 -0400609 if (path->nodes[*level-1])
Chris Mason234b63a2007-03-13 10:46:10 -0400610 btrfs_block_release(root, path->nodes[*level-1]);
Chris Mason20524f02007-03-10 06:35:47 -0500611 path->nodes[*level-1] = next;
Chris Masone20d96d2007-03-22 12:13:20 -0400612 *level = btrfs_header_level(btrfs_buffer_header(next));
Chris Mason20524f02007-03-10 06:35:47 -0500613 path->slots[*level] = 0;
614 }
615out:
Chris Mason5caf2a02007-04-02 11:20:42 -0400616 WARN_ON(*level < 0);
617 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason6407bf62007-03-27 06:33:00 -0400618 ret = btrfs_free_extent(trans, root,
Chris Mason7eccb902007-04-11 15:53:25 -0400619 bh_blocknr(path->nodes[*level]), 1, 1);
Chris Mason234b63a2007-03-13 10:46:10 -0400620 btrfs_block_release(root, path->nodes[*level]);
Chris Mason20524f02007-03-10 06:35:47 -0500621 path->nodes[*level] = NULL;
622 *level += 1;
623 BUG_ON(ret);
624 return 0;
625}
626
Chris Mason9aca1d52007-03-13 11:09:37 -0400627/*
628 * helper for dropping snapshots. This walks back up the tree in the path
629 * to find the first node higher up where we haven't yet gone through
630 * all the slots
631 */
Chris Masone089f052007-03-16 16:20:31 -0400632static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root
633 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -0500634{
635 int i;
636 int slot;
637 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400638 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Mason20524f02007-03-10 06:35:47 -0500639 slot = path->slots[i];
Chris Masone20d96d2007-03-22 12:13:20 -0400640 if (slot < btrfs_header_nritems(
641 btrfs_buffer_header(path->nodes[i])) - 1) {
Chris Mason20524f02007-03-10 06:35:47 -0500642 path->slots[i]++;
643 *level = i;
644 return 0;
645 } else {
Chris Masone089f052007-03-16 16:20:31 -0400646 ret = btrfs_free_extent(trans, root,
Chris Mason7eccb902007-04-11 15:53:25 -0400647 bh_blocknr(path->nodes[*level]),
Chris Masone089f052007-03-16 16:20:31 -0400648 1, 1);
Chris Mason6407bf62007-03-27 06:33:00 -0400649 BUG_ON(ret);
Chris Mason234b63a2007-03-13 10:46:10 -0400650 btrfs_block_release(root, path->nodes[*level]);
Chris Mason83e15a22007-03-12 09:03:27 -0400651 path->nodes[*level] = NULL;
Chris Mason20524f02007-03-10 06:35:47 -0500652 *level = i + 1;
Chris Mason20524f02007-03-10 06:35:47 -0500653 }
654 }
655 return 1;
656}
657
Chris Mason9aca1d52007-03-13 11:09:37 -0400658/*
659 * drop the reference count on the tree rooted at 'snap'. This traverses
660 * the tree freeing any blocks that have a ref count of zero after being
661 * decremented.
662 */
Chris Masone089f052007-03-16 16:20:31 -0400663int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masone20d96d2007-03-22 12:13:20 -0400664 *root, struct buffer_head *snap)
Chris Mason20524f02007-03-10 06:35:47 -0500665{
Chris Mason3768f362007-03-13 16:47:54 -0400666 int ret = 0;
Chris Mason9aca1d52007-03-13 11:09:37 -0400667 int wret;
Chris Mason20524f02007-03-10 06:35:47 -0500668 int level;
Chris Mason5caf2a02007-04-02 11:20:42 -0400669 struct btrfs_path *path;
Chris Mason20524f02007-03-10 06:35:47 -0500670 int i;
671 int orig_level;
672
Chris Mason5caf2a02007-04-02 11:20:42 -0400673 path = btrfs_alloc_path();
674 BUG_ON(!path);
675 btrfs_init_path(path);
Chris Mason20524f02007-03-10 06:35:47 -0500676
Chris Masone20d96d2007-03-22 12:13:20 -0400677 level = btrfs_header_level(btrfs_buffer_header(snap));
Chris Mason20524f02007-03-10 06:35:47 -0500678 orig_level = level;
Chris Mason5caf2a02007-04-02 11:20:42 -0400679 path->nodes[level] = snap;
680 path->slots[level] = 0;
Chris Mason20524f02007-03-10 06:35:47 -0500681 while(1) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400682 wret = walk_down_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -0400683 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -0500684 break;
Chris Mason9aca1d52007-03-13 11:09:37 -0400685 if (wret < 0)
686 ret = wret;
687
Chris Mason5caf2a02007-04-02 11:20:42 -0400688 wret = walk_up_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -0400689 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -0500690 break;
Chris Mason9aca1d52007-03-13 11:09:37 -0400691 if (wret < 0)
692 ret = wret;
Chris Mason20524f02007-03-10 06:35:47 -0500693 }
Chris Mason83e15a22007-03-12 09:03:27 -0400694 for (i = 0; i <= orig_level; i++) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400695 if (path->nodes[i]) {
696 btrfs_block_release(root, path->nodes[i]);
Chris Mason83e15a22007-03-12 09:03:27 -0400697 }
Chris Mason20524f02007-03-10 06:35:47 -0500698 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400699 btrfs_free_path(path);
Chris Mason9aca1d52007-03-13 11:09:37 -0400700 return ret;
Chris Mason20524f02007-03-10 06:35:47 -0500701}