blob: cb04a70eb7e843de939a11f5c4d5cd286ece5d18 [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 Masonb18c6682007-04-17 13:26:50 -0400114 ret = btrfs_inc_extent_ref(trans, root,
Chris Mason6407bf62007-03-27 06:33:00 -0400115 btrfs_file_extent_disk_blocknr(fi),
116 btrfs_file_extent_disk_num_blocks(fi));
117 BUG_ON(ret);
118 } else {
119 blocknr = btrfs_node_blockptr(buf_node, i);
Chris Masonb18c6682007-04-17 13:26:50 -0400120 ret = btrfs_inc_extent_ref(trans, root, blocknr, 1);
Chris Mason6407bf62007-03-27 06:33:00 -0400121 BUG_ON(ret);
122 }
Chris Mason02217ed2007-03-02 16:08:05 -0500123 }
124 return 0;
125}
126
Chris Masone089f052007-03-16 16:20:31 -0400127int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, struct
128 btrfs_root *root)
Chris Masona28ec192007-03-06 20:08:01 -0500129{
Chris Mason8ef97622007-03-26 10:15:30 -0400130 unsigned long gang[8];
Chris Mason88fd1462007-03-16 08:56:18 -0400131 u64 first = 0;
Chris Masona28ec192007-03-06 20:08:01 -0500132 int ret;
133 int i;
Chris Mason8ef97622007-03-26 10:15:30 -0400134 struct radix_tree_root *pinned_radix = &root->fs_info->pinned_radix;
Chris Masona28ec192007-03-06 20:08:01 -0500135
136 while(1) {
Chris Mason8ef97622007-03-26 10:15:30 -0400137 ret = find_first_radix_bit(pinned_radix, gang,
138 ARRAY_SIZE(gang));
Chris Masona28ec192007-03-06 20:08:01 -0500139 if (!ret)
140 break;
Chris Mason88fd1462007-03-16 08:56:18 -0400141 if (!first)
Chris Mason8ef97622007-03-26 10:15:30 -0400142 first = gang[0];
Chris Mason0579da42007-03-07 16:15:30 -0500143 for (i = 0; i < ret; i++) {
Chris Mason8ef97622007-03-26 10:15:30 -0400144 clear_radix_bit(pinned_radix, gang[i]);
Chris Mason0579da42007-03-07 16:15:30 -0500145 }
Chris Masona28ec192007-03-06 20:08:01 -0500146 }
Chris Masond5719762007-03-23 10:01:08 -0400147 if (root->fs_info->last_insert.objectid > first)
148 root->fs_info->last_insert.objectid = first;
Chris Mason9f5fae22007-03-20 14:38:32 -0400149 root->fs_info->last_insert.offset = 0;
Chris Masona28ec192007-03-06 20:08:01 -0500150 return 0;
151}
152
Chris Masone089f052007-03-16 16:20:31 -0400153static int finish_current_insert(struct btrfs_trans_handle *trans, struct
154 btrfs_root *extent_root)
Chris Mason037e6392007-03-07 11:50:24 -0500155{
Chris Masone2fa7222007-03-12 16:22:34 -0400156 struct btrfs_key ins;
Chris Mason234b63a2007-03-13 10:46:10 -0400157 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -0500158 int i;
159 int ret;
Chris Mason1261ec42007-03-20 20:35:03 -0400160 u64 super_blocks_used;
161 struct btrfs_fs_info *info = extent_root->fs_info;
Chris Mason037e6392007-03-07 11:50:24 -0500162
Chris Masoncf27e1e2007-03-13 09:49:06 -0400163 btrfs_set_extent_refs(&extent_item, 1);
Chris Mason037e6392007-03-07 11:50:24 -0500164 ins.offset = 1;
165 ins.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -0400166 btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
Chris Mason037e6392007-03-07 11:50:24 -0500167
Chris Mason9f5fae22007-03-20 14:38:32 -0400168 for (i = 0; i < extent_root->fs_info->current_insert.flags; i++) {
169 ins.objectid = extent_root->fs_info->current_insert.objectid +
170 i;
Chris Mason1261ec42007-03-20 20:35:03 -0400171 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
172 btrfs_set_super_blocks_used(info->disk_super,
173 super_blocks_used + 1);
Chris Masone089f052007-03-16 16:20:31 -0400174 ret = btrfs_insert_item(trans, extent_root, &ins, &extent_item,
175 sizeof(extent_item));
Chris Mason037e6392007-03-07 11:50:24 -0500176 BUG_ON(ret);
177 }
Chris Mason9f5fae22007-03-20 14:38:32 -0400178 extent_root->fs_info->current_insert.offset = 0;
Chris Mason037e6392007-03-07 11:50:24 -0500179 return 0;
180}
181
Chris Mason8ef97622007-03-26 10:15:30 -0400182static int pin_down_block(struct btrfs_root *root, u64 blocknr, int pending)
Chris Masone20d96d2007-03-22 12:13:20 -0400183{
184 int err;
Chris Mason78fae272007-03-25 11:35:08 -0400185 struct btrfs_header *header;
Chris Mason8ef97622007-03-26 10:15:30 -0400186 struct buffer_head *bh;
Chris Mason78fae272007-03-25 11:35:08 -0400187
Chris Masonf4b9aa82007-03-27 11:05:53 -0400188 if (!pending) {
Chris Masond98237b2007-03-28 13:57:48 -0400189 bh = btrfs_find_tree_block(root, blocknr);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400190 if (bh) {
191 if (buffer_uptodate(bh)) {
192 u64 transid =
193 root->fs_info->running_transaction->transid;
194 header = btrfs_buffer_header(bh);
195 if (btrfs_header_generation(header) ==
196 transid) {
197 btrfs_block_release(root, bh);
198 return 0;
199 }
Chris Masonf4b9aa82007-03-27 11:05:53 -0400200 }
Chris Masond6025572007-03-30 14:27:56 -0400201 btrfs_block_release(root, bh);
Chris Mason8ef97622007-03-26 10:15:30 -0400202 }
Chris Mason8ef97622007-03-26 10:15:30 -0400203 err = set_radix_bit(&root->fs_info->pinned_radix, blocknr);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400204 } else {
205 err = set_radix_bit(&root->fs_info->pending_del_radix, blocknr);
206 }
Chris Mason8ef97622007-03-26 10:15:30 -0400207 BUG_ON(err);
Chris Masone20d96d2007-03-22 12:13:20 -0400208 return 0;
209}
210
Chris Masona28ec192007-03-06 20:08:01 -0500211/*
212 * remove an extent from the root, returns 0 on success
213 */
Chris Masone089f052007-03-16 16:20:31 -0400214static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason78fae272007-03-25 11:35:08 -0400215 *root, u64 blocknr, u64 num_blocks, int pin)
Chris Masona28ec192007-03-06 20:08:01 -0500216{
Chris Mason5caf2a02007-04-02 11:20:42 -0400217 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -0400218 struct btrfs_key key;
Chris Mason1261ec42007-03-20 20:35:03 -0400219 struct btrfs_fs_info *info = root->fs_info;
220 struct btrfs_root *extent_root = info->extent_root;
Chris Masona28ec192007-03-06 20:08:01 -0500221 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400222 struct btrfs_extent_item *ei;
Chris Masone2fa7222007-03-12 16:22:34 -0400223 struct btrfs_key ins;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400224 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500225
Chris Masona28ec192007-03-06 20:08:01 -0500226 key.objectid = blocknr;
227 key.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -0400228 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masona28ec192007-03-06 20:08:01 -0500229 key.offset = num_blocks;
230
Chris Masone089f052007-03-16 16:20:31 -0400231 find_free_extent(trans, root, 0, 0, (u64)-1, &ins);
Chris Mason5caf2a02007-04-02 11:20:42 -0400232 path = btrfs_alloc_path();
233 BUG_ON(!path);
234 btrfs_init_path(path);
Chris Mason5f26f772007-04-05 10:38:44 -0400235
Chris Mason5caf2a02007-04-02 11:20:42 -0400236 ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
Chris Masona28ec192007-03-06 20:08:01 -0500237 if (ret) {
Chris Mason2e635a22007-03-21 11:12:56 -0400238 printk("failed to find %Lu\n", key.objectid);
Chris Mason234b63a2007-03-13 10:46:10 -0400239 btrfs_print_tree(extent_root, extent_root->node);
Chris Mason2e635a22007-03-21 11:12:56 -0400240 printk("failed to find %Lu\n", key.objectid);
Chris Masona28ec192007-03-06 20:08:01 -0500241 BUG();
242 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400243 ei = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
Chris Mason123abc82007-03-14 14:14:43 -0400244 struct btrfs_extent_item);
Chris Masona28ec192007-03-06 20:08:01 -0500245 BUG_ON(ei->refs == 0);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400246 refs = btrfs_extent_refs(ei) - 1;
247 btrfs_set_extent_refs(ei, refs);
Chris Mason5caf2a02007-04-02 11:20:42 -0400248 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400249 if (refs == 0) {
Chris Mason1261ec42007-03-20 20:35:03 -0400250 u64 super_blocks_used;
Chris Mason78fae272007-03-25 11:35:08 -0400251
252 if (pin) {
Chris Mason8ef97622007-03-26 10:15:30 -0400253 ret = pin_down_block(root, blocknr, 0);
Chris Mason78fae272007-03-25 11:35:08 -0400254 BUG_ON(ret);
255 }
256
Chris Mason1261ec42007-03-20 20:35:03 -0400257 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
258 btrfs_set_super_blocks_used(info->disk_super,
259 super_blocks_used - num_blocks);
Chris Mason5caf2a02007-04-02 11:20:42 -0400260 ret = btrfs_del_item(trans, extent_root, path);
Chris Masona28ec192007-03-06 20:08:01 -0500261 if (ret)
262 BUG();
263 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400264 btrfs_release_path(extent_root, path);
265 btrfs_free_path(path);
Chris Masone089f052007-03-16 16:20:31 -0400266 finish_current_insert(trans, extent_root);
Chris Masona28ec192007-03-06 20:08:01 -0500267 return ret;
268}
269
270/*
Chris Masonfec577f2007-02-26 10:40:21 -0500271 * find all the blocks marked as pending in the radix tree and remove
272 * them from the extent map
273 */
Chris Masone089f052007-03-16 16:20:31 -0400274static int del_pending_extents(struct btrfs_trans_handle *trans, struct
275 btrfs_root *extent_root)
Chris Masonfec577f2007-02-26 10:40:21 -0500276{
277 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -0400278 int wret;
279 int err = 0;
Chris Mason8ef97622007-03-26 10:15:30 -0400280 unsigned long gang[4];
Chris Masonfec577f2007-02-26 10:40:21 -0500281 int i;
Chris Mason8ef97622007-03-26 10:15:30 -0400282 struct radix_tree_root *pending_radix;
283 struct radix_tree_root *pinned_radix;
284
285 pending_radix = &extent_root->fs_info->pending_del_radix;
286 pinned_radix = &extent_root->fs_info->pinned_radix;
Chris Masonfec577f2007-02-26 10:40:21 -0500287
288 while(1) {
Chris Mason8ef97622007-03-26 10:15:30 -0400289 ret = find_first_radix_bit(pending_radix, gang,
290 ARRAY_SIZE(gang));
Chris Masonfec577f2007-02-26 10:40:21 -0500291 if (!ret)
292 break;
293 for (i = 0; i < ret; i++) {
Chris Mason8ef97622007-03-26 10:15:30 -0400294 wret = set_radix_bit(pinned_radix, gang[i]);
295 BUG_ON(wret);
296 wret = clear_radix_bit(pending_radix, gang[i]);
297 BUG_ON(wret);
Chris Masond5719762007-03-23 10:01:08 -0400298 wret = __free_extent(trans, extent_root,
Chris Mason8ef97622007-03-26 10:15:30 -0400299 gang[i], 1, 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400300 if (wret)
301 err = wret;
Chris Masonfec577f2007-02-26 10:40:21 -0500302 }
303 }
Chris Masone20d96d2007-03-22 12:13:20 -0400304 return err;
Chris Masonfec577f2007-02-26 10:40:21 -0500305}
306
307/*
308 * remove an extent from the root, returns 0 on success
309 */
Chris Masone089f052007-03-16 16:20:31 -0400310int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
311 *root, u64 blocknr, u64 num_blocks, int pin)
Chris Masonfec577f2007-02-26 10:40:21 -0500312{
Chris Mason9f5fae22007-03-20 14:38:32 -0400313 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Masonfec577f2007-02-26 10:40:21 -0500314 int pending_ret;
315 int ret;
Chris Masona28ec192007-03-06 20:08:01 -0500316
317 if (root == extent_root) {
Chris Mason8ef97622007-03-26 10:15:30 -0400318 pin_down_block(root, blocknr, 1);
Chris Masona28ec192007-03-06 20:08:01 -0500319 return 0;
320 }
Chris Mason78fae272007-03-25 11:35:08 -0400321 ret = __free_extent(trans, root, blocknr, num_blocks, pin);
Chris Masone20d96d2007-03-22 12:13:20 -0400322 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
Chris Masonfec577f2007-02-26 10:40:21 -0500323 return ret ? ret : pending_ret;
324}
325
326/*
327 * walks the btree of allocated extents and find a hole of a given size.
328 * The key ins is changed to record the hole:
329 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -0400330 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -0500331 * ins->offset == number of blocks
332 * Any available blocks before search_start are skipped.
333 */
Chris Masone089f052007-03-16 16:20:31 -0400334static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
335 *orig_root, u64 num_blocks, u64 search_start, u64
336 search_end, struct btrfs_key *ins)
Chris Masonfec577f2007-02-26 10:40:21 -0500337{
Chris Mason5caf2a02007-04-02 11:20:42 -0400338 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -0400339 struct btrfs_key key;
Chris Masonfec577f2007-02-26 10:40:21 -0500340 int ret;
341 u64 hole_size = 0;
342 int slot = 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400343 u64 last_block = 0;
Chris Mason037e6392007-03-07 11:50:24 -0500344 u64 test_block;
Chris Masonfec577f2007-02-26 10:40:21 -0500345 int start_found;
Chris Mason234b63a2007-03-13 10:46:10 -0400346 struct btrfs_leaf *l;
Chris Mason9f5fae22007-03-20 14:38:32 -0400347 struct btrfs_root * root = orig_root->fs_info->extent_root;
Chris Mason0579da42007-03-07 16:15:30 -0500348 int total_needed = num_blocks;
Chris Masone20d96d2007-03-22 12:13:20 -0400349 int level;
Chris Masonfec577f2007-02-26 10:40:21 -0500350
Chris Masonb1a4d962007-04-04 15:27:52 -0400351 path = btrfs_alloc_path();
352 ins->flags = 0;
353 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
354
Chris Masone20d96d2007-03-22 12:13:20 -0400355 level = btrfs_header_level(btrfs_buffer_header(root->node));
356 total_needed += (level + 1) * 3;
Chris Masonb1a4d962007-04-04 15:27:52 -0400357 if (root->fs_info->last_insert.objectid == 0 && search_end == (u64)-1) {
358 struct btrfs_disk_key *last_key;
359 btrfs_init_path(path);
360 ins->objectid = (u64)-1;
361 ins->offset = (u64)-1;
362 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
363 if (ret < 0)
364 goto error;
365 BUG_ON(ret == 0);
366 if (path->slots[0] > 0)
367 path->slots[0]--;
368 l = btrfs_buffer_leaf(path->nodes[0]);
369 last_key = &l->items[path->slots[0]].key;
370 search_start = btrfs_disk_key_objectid(last_key);
371 }
Chris Mason9f5fae22007-03-20 14:38:32 -0400372 if (root->fs_info->last_insert.objectid > search_start)
373 search_start = root->fs_info->last_insert.objectid;
Chris Mason62e27492007-03-15 12:56:47 -0400374
Chris Masonfec577f2007-02-26 10:40:21 -0500375check_failed:
Chris Mason5caf2a02007-04-02 11:20:42 -0400376 btrfs_init_path(path);
Chris Masonfec577f2007-02-26 10:40:21 -0500377 ins->objectid = search_start;
378 ins->offset = 0;
Chris Masonfec577f2007-02-26 10:40:21 -0500379 start_found = 0;
Chris Mason5caf2a02007-04-02 11:20:42 -0400380 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
Chris Mason0f70abe2007-02-28 16:46:22 -0500381 if (ret < 0)
382 goto error;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500383
Chris Mason5caf2a02007-04-02 11:20:42 -0400384 if (path->slots[0] > 0)
385 path->slots[0]--;
Chris Mason0579da42007-03-07 16:15:30 -0500386
Chris Masonfec577f2007-02-26 10:40:21 -0500387 while (1) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400388 l = btrfs_buffer_leaf(path->nodes[0]);
389 slot = path->slots[0];
Chris Mason7518a232007-03-12 12:01:18 -0400390 if (slot >= btrfs_header_nritems(&l->header)) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400391 ret = btrfs_next_leaf(root, path);
Chris Masonfec577f2007-02-26 10:40:21 -0500392 if (ret == 0)
393 continue;
Chris Mason0f70abe2007-02-28 16:46:22 -0500394 if (ret < 0)
395 goto error;
Chris Masonfec577f2007-02-26 10:40:21 -0500396 if (!start_found) {
397 ins->objectid = search_start;
Chris Mason037e6392007-03-07 11:50:24 -0500398 ins->offset = (u64)-1;
Chris Masonfec577f2007-02-26 10:40:21 -0500399 start_found = 1;
400 goto check_pending;
401 }
402 ins->objectid = last_block > search_start ?
403 last_block : search_start;
Chris Mason037e6392007-03-07 11:50:24 -0500404 ins->offset = (u64)-1;
Chris Masonfec577f2007-02-26 10:40:21 -0500405 goto check_pending;
406 }
Chris Masone2fa7222007-03-12 16:22:34 -0400407 btrfs_disk_key_to_cpu(&key, &l->items[slot].key);
408 if (key.objectid >= search_start) {
Chris Masonfec577f2007-02-26 10:40:21 -0500409 if (start_found) {
Chris Mason0579da42007-03-07 16:15:30 -0500410 if (last_block < search_start)
411 last_block = search_start;
Chris Masone2fa7222007-03-12 16:22:34 -0400412 hole_size = key.objectid - last_block;
Chris Mason037e6392007-03-07 11:50:24 -0500413 if (hole_size > total_needed) {
Chris Masonfec577f2007-02-26 10:40:21 -0500414 ins->objectid = last_block;
Chris Mason037e6392007-03-07 11:50:24 -0500415 ins->offset = hole_size;
Chris Masonfec577f2007-02-26 10:40:21 -0500416 goto check_pending;
417 }
Chris Mason0579da42007-03-07 16:15:30 -0500418 }
Chris Masonfec577f2007-02-26 10:40:21 -0500419 }
Chris Mason0579da42007-03-07 16:15:30 -0500420 start_found = 1;
Chris Masone2fa7222007-03-12 16:22:34 -0400421 last_block = key.objectid + key.offset;
Chris Mason5caf2a02007-04-02 11:20:42 -0400422 path->slots[0]++;
Chris Masonfec577f2007-02-26 10:40:21 -0500423 }
424 // FIXME -ENOSPC
425check_pending:
426 /* we have to make sure we didn't find an extent that has already
427 * been allocated by the map tree or the original allocation
428 */
Chris Mason5caf2a02007-04-02 11:20:42 -0400429 btrfs_release_path(root, path);
Chris Masonfec577f2007-02-26 10:40:21 -0500430 BUG_ON(ins->objectid < search_start);
Chris Mason037e6392007-03-07 11:50:24 -0500431 for (test_block = ins->objectid;
432 test_block < ins->objectid + total_needed; test_block++) {
Chris Mason8ef97622007-03-26 10:15:30 -0400433 if (test_radix_bit(&root->fs_info->pinned_radix,
Chris Mason9f5fae22007-03-20 14:38:32 -0400434 test_block)) {
Chris Mason037e6392007-03-07 11:50:24 -0500435 search_start = test_block + 1;
Chris Masonfec577f2007-02-26 10:40:21 -0500436 goto check_failed;
437 }
438 }
Chris Mason9f5fae22007-03-20 14:38:32 -0400439 BUG_ON(root->fs_info->current_insert.offset);
440 root->fs_info->current_insert.offset = total_needed - num_blocks;
441 root->fs_info->current_insert.objectid = ins->objectid + num_blocks;
442 root->fs_info->current_insert.flags = 0;
443 root->fs_info->last_insert.objectid = ins->objectid;
Chris Mason037e6392007-03-07 11:50:24 -0500444 ins->offset = num_blocks;
Chris Mason5caf2a02007-04-02 11:20:42 -0400445 btrfs_free_path(path);
Chris Masonfec577f2007-02-26 10:40:21 -0500446 return 0;
Chris Mason0f70abe2007-02-28 16:46:22 -0500447error:
Chris Mason5caf2a02007-04-02 11:20:42 -0400448 btrfs_release_path(root, path);
449 btrfs_free_path(path);
Chris Mason0f70abe2007-02-28 16:46:22 -0500450 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -0500451}
452
453/*
Chris Masonfec577f2007-02-26 10:40:21 -0500454 * finds a free extent and does all the dirty work required for allocation
455 * returns the key for the extent through ins, and a tree buffer for
456 * the first block of the extent through buf.
457 *
458 * returns 0 if everything worked, non-zero otherwise.
459 */
Chris Masondee26a92007-03-26 16:00:06 -0400460int btrfs_alloc_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masone089f052007-03-16 16:20:31 -0400461 *root, u64 num_blocks, u64 search_start, u64
Chris Masond0dbc6242007-04-10 12:36:36 -0400462 search_end, struct btrfs_key *ins)
Chris Masonfec577f2007-02-26 10:40:21 -0500463{
464 int ret;
465 int pending_ret;
Chris Mason1261ec42007-03-20 20:35:03 -0400466 u64 super_blocks_used;
467 struct btrfs_fs_info *info = root->fs_info;
468 struct btrfs_root *extent_root = info->extent_root;
Chris Mason234b63a2007-03-13 10:46:10 -0400469 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -0500470
Chris Masoncf27e1e2007-03-13 09:49:06 -0400471 btrfs_set_extent_refs(&extent_item, 1);
Chris Masonfec577f2007-02-26 10:40:21 -0500472
Chris Mason037e6392007-03-07 11:50:24 -0500473 if (root == extent_root) {
Chris Mason9f5fae22007-03-20 14:38:32 -0400474 BUG_ON(extent_root->fs_info->current_insert.offset == 0);
Chris Mason037e6392007-03-07 11:50:24 -0500475 BUG_ON(num_blocks != 1);
Chris Mason9f5fae22007-03-20 14:38:32 -0400476 BUG_ON(extent_root->fs_info->current_insert.flags ==
477 extent_root->fs_info->current_insert.offset);
Chris Mason037e6392007-03-07 11:50:24 -0500478 ins->offset = 1;
Chris Mason9f5fae22007-03-20 14:38:32 -0400479 ins->objectid = extent_root->fs_info->current_insert.objectid +
480 extent_root->fs_info->current_insert.flags++;
Chris Masonfec577f2007-02-26 10:40:21 -0500481 return 0;
482 }
Chris Masone089f052007-03-16 16:20:31 -0400483 ret = find_free_extent(trans, root, num_blocks, search_start,
Chris Mason037e6392007-03-07 11:50:24 -0500484 search_end, ins);
485 if (ret)
486 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -0500487
Chris Mason1261ec42007-03-20 20:35:03 -0400488 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
489 btrfs_set_super_blocks_used(info->disk_super, super_blocks_used +
490 num_blocks);
Chris Masone089f052007-03-16 16:20:31 -0400491 ret = btrfs_insert_item(trans, extent_root, ins, &extent_item,
492 sizeof(extent_item));
Chris Mason037e6392007-03-07 11:50:24 -0500493
Chris Masone089f052007-03-16 16:20:31 -0400494 finish_current_insert(trans, extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -0400495 pending_ret = del_pending_extents(trans, extent_root);
Chris Mason037e6392007-03-07 11:50:24 -0500496 if (ret)
497 return ret;
498 if (pending_ret)
499 return pending_ret;
500 return 0;
Chris Masonfec577f2007-02-26 10:40:21 -0500501}
502
503/*
504 * helper function to allocate a block for a given tree
505 * returns the tree buffer or NULL.
506 */
Chris Masone20d96d2007-03-22 12:13:20 -0400507struct buffer_head *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Chris Masone089f052007-03-16 16:20:31 -0400508 struct btrfs_root *root)
Chris Masonfec577f2007-02-26 10:40:21 -0500509{
Chris Masone2fa7222007-03-12 16:22:34 -0400510 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -0500511 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -0400512 struct buffer_head *buf;
Chris Masonfec577f2007-02-26 10:40:21 -0500513
Chris Masond0dbc6242007-04-10 12:36:36 -0400514 ret = btrfs_alloc_extent(trans, root, 1, 0, (unsigned long)-1, &ins);
Chris Masonfec577f2007-02-26 10:40:21 -0500515 if (ret) {
516 BUG();
517 return NULL;
518 }
Chris Masond98237b2007-03-28 13:57:48 -0400519 buf = btrfs_find_create_tree_block(root, ins.objectid);
Chris Masondf2ce342007-03-23 11:00:45 -0400520 set_buffer_uptodate(buf);
Chris Masonfec577f2007-02-26 10:40:21 -0500521 return buf;
522}
Chris Masona28ec192007-03-06 20:08:01 -0500523
Chris Mason6407bf62007-03-27 06:33:00 -0400524static int drop_leaf_ref(struct btrfs_trans_handle *trans,
525 struct btrfs_root *root, struct buffer_head *cur)
526{
527 struct btrfs_disk_key *key;
528 struct btrfs_leaf *leaf;
529 struct btrfs_file_extent_item *fi;
530 int i;
531 int nritems;
532 int ret;
533
534 BUG_ON(!btrfs_is_leaf(btrfs_buffer_node(cur)));
535 leaf = btrfs_buffer_leaf(cur);
536 nritems = btrfs_header_nritems(&leaf->header);
537 for (i = 0; i < nritems; i++) {
538 key = &leaf->items[i].key;
539 if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY)
540 continue;
541 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
542 /*
543 * FIXME make sure to insert a trans record that
544 * repeats the snapshot del on crash
545 */
546 ret = btrfs_free_extent(trans, root,
547 btrfs_file_extent_disk_blocknr(fi),
548 btrfs_file_extent_disk_num_blocks(fi),
549 0);
550 BUG_ON(ret);
551 }
552 return 0;
553}
554
Chris Mason9aca1d52007-03-13 11:09:37 -0400555/*
556 * helper function for drop_snapshot, this walks down the tree dropping ref
557 * counts as it goes.
558 */
Chris Masone089f052007-03-16 16:20:31 -0400559static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
560 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -0500561{
Chris Masone20d96d2007-03-22 12:13:20 -0400562 struct buffer_head *next;
563 struct buffer_head *cur;
Chris Mason20524f02007-03-10 06:35:47 -0500564 u64 blocknr;
565 int ret;
566 u32 refs;
567
Chris Mason5caf2a02007-04-02 11:20:42 -0400568 WARN_ON(*level < 0);
569 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Masonb18c6682007-04-17 13:26:50 -0400570 ret = lookup_extent_ref(trans, root, bh_blocknr(path->nodes[*level]),
Chris Mason6407bf62007-03-27 06:33:00 -0400571 1, &refs);
Chris Mason20524f02007-03-10 06:35:47 -0500572 BUG_ON(ret);
573 if (refs > 1)
574 goto out;
Chris Mason9aca1d52007-03-13 11:09:37 -0400575 /*
576 * walk down to the last node level and free all the leaves
577 */
Chris Mason6407bf62007-03-27 06:33:00 -0400578 while(*level >= 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400579 WARN_ON(*level < 0);
580 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason20524f02007-03-10 06:35:47 -0500581 cur = path->nodes[*level];
Chris Mason2c90e5d2007-04-02 10:50:19 -0400582 if (btrfs_header_level(btrfs_buffer_header(cur)) != *level)
583 WARN_ON(1);
Chris Mason7518a232007-03-12 12:01:18 -0400584 if (path->slots[*level] >=
Chris Masone20d96d2007-03-22 12:13:20 -0400585 btrfs_header_nritems(btrfs_buffer_header(cur)))
Chris Mason20524f02007-03-10 06:35:47 -0500586 break;
Chris Mason6407bf62007-03-27 06:33:00 -0400587 if (*level == 0) {
588 ret = drop_leaf_ref(trans, root, cur);
589 BUG_ON(ret);
590 break;
591 }
Chris Masone20d96d2007-03-22 12:13:20 -0400592 blocknr = btrfs_node_blockptr(btrfs_buffer_node(cur),
593 path->slots[*level]);
Chris Masonb18c6682007-04-17 13:26:50 -0400594 ret = lookup_extent_ref(trans, root, blocknr, 1, &refs);
Chris Mason6407bf62007-03-27 06:33:00 -0400595 BUG_ON(ret);
596 if (refs != 1) {
Chris Mason20524f02007-03-10 06:35:47 -0500597 path->slots[*level]++;
Chris Masone089f052007-03-16 16:20:31 -0400598 ret = btrfs_free_extent(trans, root, blocknr, 1, 1);
Chris Mason20524f02007-03-10 06:35:47 -0500599 BUG_ON(ret);
600 continue;
601 }
Chris Mason20524f02007-03-10 06:35:47 -0500602 next = read_tree_block(root, blocknr);
Chris Mason5caf2a02007-04-02 11:20:42 -0400603 WARN_ON(*level <= 0);
Chris Mason83e15a22007-03-12 09:03:27 -0400604 if (path->nodes[*level-1])
Chris Mason234b63a2007-03-13 10:46:10 -0400605 btrfs_block_release(root, path->nodes[*level-1]);
Chris Mason20524f02007-03-10 06:35:47 -0500606 path->nodes[*level-1] = next;
Chris Masone20d96d2007-03-22 12:13:20 -0400607 *level = btrfs_header_level(btrfs_buffer_header(next));
Chris Mason20524f02007-03-10 06:35:47 -0500608 path->slots[*level] = 0;
609 }
610out:
Chris Mason5caf2a02007-04-02 11:20:42 -0400611 WARN_ON(*level < 0);
612 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason6407bf62007-03-27 06:33:00 -0400613 ret = btrfs_free_extent(trans, root,
Chris Mason7eccb902007-04-11 15:53:25 -0400614 bh_blocknr(path->nodes[*level]), 1, 1);
Chris Mason234b63a2007-03-13 10:46:10 -0400615 btrfs_block_release(root, path->nodes[*level]);
Chris Mason20524f02007-03-10 06:35:47 -0500616 path->nodes[*level] = NULL;
617 *level += 1;
618 BUG_ON(ret);
619 return 0;
620}
621
Chris Mason9aca1d52007-03-13 11:09:37 -0400622/*
623 * helper for dropping snapshots. This walks back up the tree in the path
624 * to find the first node higher up where we haven't yet gone through
625 * all the slots
626 */
Chris Masone089f052007-03-16 16:20:31 -0400627static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root
628 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -0500629{
630 int i;
631 int slot;
632 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400633 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Mason20524f02007-03-10 06:35:47 -0500634 slot = path->slots[i];
Chris Masone20d96d2007-03-22 12:13:20 -0400635 if (slot < btrfs_header_nritems(
636 btrfs_buffer_header(path->nodes[i])) - 1) {
Chris Mason20524f02007-03-10 06:35:47 -0500637 path->slots[i]++;
638 *level = i;
639 return 0;
640 } else {
Chris Masone089f052007-03-16 16:20:31 -0400641 ret = btrfs_free_extent(trans, root,
Chris Mason7eccb902007-04-11 15:53:25 -0400642 bh_blocknr(path->nodes[*level]),
Chris Masone089f052007-03-16 16:20:31 -0400643 1, 1);
Chris Mason6407bf62007-03-27 06:33:00 -0400644 BUG_ON(ret);
Chris Mason234b63a2007-03-13 10:46:10 -0400645 btrfs_block_release(root, path->nodes[*level]);
Chris Mason83e15a22007-03-12 09:03:27 -0400646 path->nodes[*level] = NULL;
Chris Mason20524f02007-03-10 06:35:47 -0500647 *level = i + 1;
Chris Mason20524f02007-03-10 06:35:47 -0500648 }
649 }
650 return 1;
651}
652
Chris Mason9aca1d52007-03-13 11:09:37 -0400653/*
654 * drop the reference count on the tree rooted at 'snap'. This traverses
655 * the tree freeing any blocks that have a ref count of zero after being
656 * decremented.
657 */
Chris Masone089f052007-03-16 16:20:31 -0400658int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masone20d96d2007-03-22 12:13:20 -0400659 *root, struct buffer_head *snap)
Chris Mason20524f02007-03-10 06:35:47 -0500660{
Chris Mason3768f362007-03-13 16:47:54 -0400661 int ret = 0;
Chris Mason9aca1d52007-03-13 11:09:37 -0400662 int wret;
Chris Mason20524f02007-03-10 06:35:47 -0500663 int level;
Chris Mason5caf2a02007-04-02 11:20:42 -0400664 struct btrfs_path *path;
Chris Mason20524f02007-03-10 06:35:47 -0500665 int i;
666 int orig_level;
667
Chris Mason5caf2a02007-04-02 11:20:42 -0400668 path = btrfs_alloc_path();
669 BUG_ON(!path);
670 btrfs_init_path(path);
Chris Mason20524f02007-03-10 06:35:47 -0500671
Chris Masone20d96d2007-03-22 12:13:20 -0400672 level = btrfs_header_level(btrfs_buffer_header(snap));
Chris Mason20524f02007-03-10 06:35:47 -0500673 orig_level = level;
Chris Mason5caf2a02007-04-02 11:20:42 -0400674 path->nodes[level] = snap;
675 path->slots[level] = 0;
Chris Mason20524f02007-03-10 06:35:47 -0500676 while(1) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400677 wret = walk_down_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -0400678 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -0500679 break;
Chris Mason9aca1d52007-03-13 11:09:37 -0400680 if (wret < 0)
681 ret = wret;
682
Chris Mason5caf2a02007-04-02 11:20:42 -0400683 wret = walk_up_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -0400684 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -0500685 break;
Chris Mason9aca1d52007-03-13 11:09:37 -0400686 if (wret < 0)
687 ret = wret;
Chris Mason20524f02007-03-10 06:35:47 -0500688 }
Chris Mason83e15a22007-03-12 09:03:27 -0400689 for (i = 0; i <= orig_level; i++) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400690 if (path->nodes[i]) {
691 btrfs_block_release(root, path->nodes[i]);
Chris Mason83e15a22007-03-12 09:03:27 -0400692 }
Chris Mason20524f02007-03-10 06:35:47 -0500693 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400694 btrfs_free_path(path);
Chris Mason9aca1d52007-03-13 11:09:37 -0400695 return ret;
Chris Mason20524f02007-03-10 06:35:47 -0500696}