blob: 3ac9da453472bfdbc629de7bc33cb25c5c681fd4 [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
Chris Masonbe08c1b2007-05-03 09:06:49 -04009 search_end, struct btrfs_key *ins, int data);
Chris Masone089f052007-03-16 16:20:31 -040010static 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 Masone37c9e62007-05-09 20:13:14 -040015static int cache_block_group(struct btrfs_root *root,
16 struct btrfs_block_group_cache *block_group)
17{
18 struct btrfs_path *path;
19 int ret;
20 struct btrfs_key key;
21 struct btrfs_leaf *leaf;
22 struct radix_tree_root *extent_radix;
23 int slot;
24 u64 i;
25 u64 last = 0;
26 u64 hole_size;
27 int found = 0;
28
29 root = root->fs_info->extent_root;
30 extent_radix = &root->fs_info->extent_map_radix;
31
32 if (block_group->cached)
33 return 0;
34 if (block_group->data)
35 return 0;
36 path = btrfs_alloc_path();
37 if (!path)
38 return -ENOMEM;
39printk("cache block group %Lu\n", block_group->key.objectid);
40 key.objectid = block_group->key.objectid;
41 key.flags = 0;
42 key.offset = 0;
43 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
44 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
45 if (ret < 0)
46 return ret;
47 if (ret && path->slots[0] > 0)
48 path->slots[0]--;
49 while(1) {
50 leaf = btrfs_buffer_leaf(path->nodes[0]);
51 slot = path->slots[0];
52 if (slot >= btrfs_header_nritems(&leaf->header)) {
53 ret = btrfs_next_leaf(root, path);
54 if (ret == 0)
55 continue;
56 else {
57 if (found) {
58 hole_size = block_group->key.objectid +
59 block_group->key.offset - last;
60 } else {
61 last = block_group->key.objectid;
62 hole_size = block_group->key.offset;
63 }
64 for (i = 0; i < hole_size; i++) {
65 set_radix_bit(extent_radix,
66 last + i);
67 }
68 break;
69 }
70 }
71 btrfs_disk_key_to_cpu(&key, &leaf->items[slot].key);
72 if (key.objectid >= block_group->key.objectid +
73 block_group->key.offset) {
74 if (found) {
75 hole_size = block_group->key.objectid +
76 block_group->key.offset - last;
77 } else {
78 last = block_group->key.objectid;
79 hole_size = block_group->key.offset;
80 }
81 for (i = 0; i < hole_size; i++) {
82 set_radix_bit(extent_radix, last + i);
83 }
84 break;
85 }
86 if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) {
87 if (!found) {
88 last = key.objectid + key.offset;
89 found = 1;
90 } else {
91 hole_size = key.objectid - last;
92 for (i = 0; i < hole_size; i++) {
93 set_radix_bit(extent_radix, last + i);
94 }
95 last = key.objectid + key.offset;
96 }
97 }
98 path->slots[0]++;
99 }
100
101 block_group->cached = 1;
102 btrfs_free_path(path);
103 return 0;
104}
105
Chris Masonbe744172007-05-06 10:15:01 -0400106static struct btrfs_block_group_cache *lookup_block_group(struct
107 btrfs_fs_info *info,
108 u64 blocknr)
109{
110 struct btrfs_block_group_cache *block_group;
111 int ret;
112
113 ret = radix_tree_gang_lookup(&info->block_group_radix,
114 (void **)&block_group,
115 blocknr, 1);
116 if (ret) {
Chris Mason3e1ad542007-05-07 20:03:49 -0400117 if (block_group->key.objectid <= blocknr && blocknr <=
Chris Masonbe744172007-05-06 10:15:01 -0400118 block_group->key.objectid + block_group->key.offset)
119 return block_group;
120 }
121 ret = radix_tree_gang_lookup(&info->block_group_data_radix,
122 (void **)&block_group,
123 blocknr, 1);
124 if (ret) {
Chris Mason3e1ad542007-05-07 20:03:49 -0400125 if (block_group->key.objectid <= blocknr && blocknr <=
Chris Masonbe744172007-05-06 10:15:01 -0400126 block_group->key.objectid + block_group->key.offset)
127 return block_group;
128 }
Chris Mason3e1ad542007-05-07 20:03:49 -0400129 WARN_ON(1);
130 printk("lookup_block_group fails for blocknr %Lu\n", blocknr);
131 printk("last ret was %d\n", ret);
132 if (ret) {
133 printk("last block group was %Lu %Lu\n", block_group->key.objectid, block_group->key.offset);
134 }
Chris Masonbe744172007-05-06 10:15:01 -0400135 return NULL;
136}
137
Chris Masone37c9e62007-05-09 20:13:14 -0400138static u64 leaf_range(struct btrfs_root *root)
139{
140 u64 size = BTRFS_LEAF_DATA_SIZE(root);
141 size = size / (sizeof(struct btrfs_extent_item) +
142 sizeof(struct btrfs_item));
143 return size;
144}
145
146static u64 find_search_start(struct btrfs_root *root,
147 struct btrfs_block_group_cache **cache_ret,
148 u64 search_start, int num)
149{
150 unsigned long gang[8];
151 int ret;
152 struct btrfs_block_group_cache *cache = *cache_ret;
153 u64 last = max(search_start, cache->key.objectid);
154
155 if (cache->data)
156 goto out;
157 if (num > 1) {
158 last = max(last, cache->last_prealloc);
159 }
160again:
161 cache_block_group(root, cache);
162 while(1) {
163 ret = find_first_radix_bit(&root->fs_info->extent_map_radix,
164 gang, last, ARRAY_SIZE(gang));
165 if (!ret)
166 goto out;
167 last = gang[ret-1] + 1;
168 if (num > 1) {
169 if (ret != ARRAY_SIZE(gang)) {
170 goto new_group;
171 }
172 if (gang[ret-1] - gang[0] > leaf_range(root)) {
173 continue;
174 }
175 }
176 if (gang[0] >= cache->key.objectid + cache->key.offset) {
177 goto new_group;
178 }
179 return gang[0];
180 }
181out:
182 return max(cache->last_alloc, search_start);
183
184new_group:
185 cache = lookup_block_group(root->fs_info, last + cache->key.offset - 1);
186 if (!cache) {
187 return max((*cache_ret)->last_alloc, search_start);
188 }
189 cache = btrfs_find_block_group(root, cache,
190 last + cache->key.offset - 1, 0);
191 *cache_ret = cache;
192 goto again;
193}
194
Chris Mason31f3c992007-04-30 15:25:45 -0400195struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
196 struct btrfs_block_group_cache
Chris Masonbe744172007-05-06 10:15:01 -0400197 *hint, u64 search_start,
198 int data)
Chris Masoncd1bc462007-04-27 10:08:34 -0400199{
200 struct btrfs_block_group_cache *cache[8];
Chris Mason31f3c992007-04-30 15:25:45 -0400201 struct btrfs_block_group_cache *found_group = NULL;
Chris Masoncd1bc462007-04-27 10:08:34 -0400202 struct btrfs_fs_info *info = root->fs_info;
Chris Masonbe744172007-05-06 10:15:01 -0400203 struct radix_tree_root *radix;
Chris Masoncd1bc462007-04-27 10:08:34 -0400204 u64 used;
Chris Mason31f3c992007-04-30 15:25:45 -0400205 u64 last = 0;
206 u64 hint_last;
Chris Masoncd1bc462007-04-27 10:08:34 -0400207 int i;
208 int ret;
Chris Mason31f3c992007-04-30 15:25:45 -0400209 int full_search = 0;
Chris Masonbe744172007-05-06 10:15:01 -0400210
211 if (data)
212 radix = &info->block_group_data_radix;
213 else
214 radix = &info->block_group_radix;
215
216 if (search_start) {
217 struct btrfs_block_group_cache *shint;
218 shint = lookup_block_group(info, search_start);
219 if (shint->data == data) {
220 used = btrfs_block_group_used(&shint->item);
221 if (used + shint->pinned <
222 (shint->key.offset * 8) / 10) {
223 return shint;
224 }
225 }
226 }
227 if (hint && hint->data == data) {
Chris Mason31f3c992007-04-30 15:25:45 -0400228 used = btrfs_block_group_used(&hint->item);
Chris Masonbe744172007-05-06 10:15:01 -0400229 if (used + hint->pinned < (hint->key.offset * 8) / 10) {
Chris Mason31f3c992007-04-30 15:25:45 -0400230 return hint;
231 }
Chris Masonbe744172007-05-06 10:15:01 -0400232 if (used >= (hint->key.offset * 8) / 10) {
233 radix_tree_tag_clear(radix,
234 hint->key.objectid +
235 hint->key.offset - 1,
236 BTRFS_BLOCK_GROUP_AVAIL);
237 }
238 last = hint->key.offset * 2;
239 if (hint->key.objectid >= last)
Chris Masone37c9e62007-05-09 20:13:14 -0400240 last = max(search_start + hint->key.offset - 1,
241 hint->key.objectid - last);
Chris Masonbe744172007-05-06 10:15:01 -0400242 else
243 last = hint->key.objectid + hint->key.offset;
Chris Mason31f3c992007-04-30 15:25:45 -0400244 hint_last = last;
245 } else {
Chris Masone37c9e62007-05-09 20:13:14 -0400246 if (hint)
247 hint_last = max(hint->key.objectid, search_start);
248 else
249 hint_last = search_start;
250
251 last = hint_last;
Chris Mason31f3c992007-04-30 15:25:45 -0400252 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400253 while(1) {
Chris Masonbe744172007-05-06 10:15:01 -0400254 ret = radix_tree_gang_lookup_tag(radix, (void **)cache,
Chris Masoncd1bc462007-04-27 10:08:34 -0400255 last, ARRAY_SIZE(cache),
Chris Mason31f3c992007-04-30 15:25:45 -0400256 BTRFS_BLOCK_GROUP_AVAIL);
Chris Masoncd1bc462007-04-27 10:08:34 -0400257 if (!ret)
258 break;
259 for (i = 0; i < ret; i++) {
Chris Masonbe08c1b2007-05-03 09:06:49 -0400260 last = cache[i]->key.objectid +
261 cache[i]->key.offset;
Chris Masoncd1bc462007-04-27 10:08:34 -0400262 used = btrfs_block_group_used(&cache[i]->item);
Chris Masonbe744172007-05-06 10:15:01 -0400263 if (used + cache[i]->pinned <
264 (cache[i]->key.offset * 8) / 10) {
Chris Mason31f3c992007-04-30 15:25:45 -0400265 found_group = cache[i];
266 goto found;
Chris Masoncd1bc462007-04-27 10:08:34 -0400267 }
Chris Masonbe744172007-05-06 10:15:01 -0400268 if (used >= (cache[i]->key.offset * 8) / 10) {
269 radix_tree_tag_clear(radix,
270 cache[i]->key.objectid +
271 cache[i]->key.offset - 1,
272 BTRFS_BLOCK_GROUP_AVAIL);
273 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400274 }
275 }
Chris Mason31f3c992007-04-30 15:25:45 -0400276 last = hint_last;
277again:
Chris Masoncd1bc462007-04-27 10:08:34 -0400278 while(1) {
Chris Masonbe744172007-05-06 10:15:01 -0400279 ret = radix_tree_gang_lookup(radix, (void **)cache,
280 last, ARRAY_SIZE(cache));
Chris Masoncd1bc462007-04-27 10:08:34 -0400281 if (!ret)
282 break;
283 for (i = 0; i < ret; i++) {
Chris Masonbe08c1b2007-05-03 09:06:49 -0400284 last = cache[i]->key.objectid +
285 cache[i]->key.offset;
Chris Masoncd1bc462007-04-27 10:08:34 -0400286 used = btrfs_block_group_used(&cache[i]->item);
Chris Masonbe744172007-05-06 10:15:01 -0400287 if (used + cache[i]->pinned < cache[i]->key.offset) {
Chris Mason31f3c992007-04-30 15:25:45 -0400288 found_group = cache[i];
289 goto found;
Chris Masoncd1bc462007-04-27 10:08:34 -0400290 }
Chris Masonbe744172007-05-06 10:15:01 -0400291 if (used >= cache[i]->key.offset) {
292 radix_tree_tag_clear(radix,
293 cache[i]->key.objectid +
294 cache[i]->key.offset - 1,
295 BTRFS_BLOCK_GROUP_AVAIL);
296 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400297 }
298 }
Chris Mason31f3c992007-04-30 15:25:45 -0400299 if (!full_search) {
Chris Masonbe744172007-05-06 10:15:01 -0400300 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -0400301 full_search = 1;
302 goto again;
303 }
Chris Mason31f3c992007-04-30 15:25:45 -0400304 if (!found_group) {
Chris Masonbe744172007-05-06 10:15:01 -0400305 ret = radix_tree_gang_lookup(radix,
Chris Mason31f3c992007-04-30 15:25:45 -0400306 (void **)&found_group, 0, 1);
307 BUG_ON(ret != 1);
308 }
Chris Masonbe744172007-05-06 10:15:01 -0400309found:
Chris Mason31f3c992007-04-30 15:25:45 -0400310 return found_group;
Chris Masoncd1bc462007-04-27 10:08:34 -0400311}
312
Chris Masonb18c6682007-04-17 13:26:50 -0400313int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
314 struct btrfs_root *root,
315 u64 blocknr, u64 num_blocks)
Chris Mason02217ed2007-03-02 16:08:05 -0500316{
Chris Mason5caf2a02007-04-02 11:20:42 -0400317 struct btrfs_path *path;
Chris Mason02217ed2007-03-02 16:08:05 -0500318 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400319 struct btrfs_key key;
Chris Mason234b63a2007-03-13 10:46:10 -0400320 struct btrfs_leaf *l;
321 struct btrfs_extent_item *item;
Chris Masone2fa7222007-03-12 16:22:34 -0400322 struct btrfs_key ins;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400323 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500324
Chris Mason9f5fae22007-03-20 14:38:32 -0400325 find_free_extent(trans, root->fs_info->extent_root, 0, 0, (u64)-1,
Chris Masonbe08c1b2007-05-03 09:06:49 -0400326 &ins, 0);
Chris Mason5caf2a02007-04-02 11:20:42 -0400327 path = btrfs_alloc_path();
328 BUG_ON(!path);
329 btrfs_init_path(path);
Chris Mason02217ed2007-03-02 16:08:05 -0500330 key.objectid = blocknr;
331 key.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -0400332 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Mason6407bf62007-03-27 06:33:00 -0400333 key.offset = num_blocks;
Chris Mason5caf2a02007-04-02 11:20:42 -0400334 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -0400335 0, 1);
Chris Masona429e512007-04-18 16:15:28 -0400336 if (ret != 0) {
337printk("can't find block %Lu %Lu\n", blocknr, num_blocks);
Chris Masona28ec192007-03-06 20:08:01 -0500338 BUG();
Chris Masona429e512007-04-18 16:15:28 -0400339 }
Chris Mason02217ed2007-03-02 16:08:05 -0500340 BUG_ON(ret != 0);
Chris Mason5caf2a02007-04-02 11:20:42 -0400341 l = btrfs_buffer_leaf(path->nodes[0]);
342 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400343 refs = btrfs_extent_refs(item);
344 btrfs_set_extent_refs(item, refs + 1);
Chris Mason5caf2a02007-04-02 11:20:42 -0400345 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masona28ec192007-03-06 20:08:01 -0500346
Chris Mason5caf2a02007-04-02 11:20:42 -0400347 btrfs_release_path(root->fs_info->extent_root, path);
348 btrfs_free_path(path);
Chris Mason9f5fae22007-03-20 14:38:32 -0400349 finish_current_insert(trans, root->fs_info->extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -0400350 del_pending_extents(trans, root->fs_info->extent_root);
Chris Mason02217ed2007-03-02 16:08:05 -0500351 return 0;
352}
353
Chris Masonb18c6682007-04-17 13:26:50 -0400354static int lookup_extent_ref(struct btrfs_trans_handle *trans,
355 struct btrfs_root *root, u64 blocknr,
356 u64 num_blocks, u32 *refs)
Chris Masona28ec192007-03-06 20:08:01 -0500357{
Chris Mason5caf2a02007-04-02 11:20:42 -0400358 struct btrfs_path *path;
Chris Masona28ec192007-03-06 20:08:01 -0500359 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400360 struct btrfs_key key;
Chris Mason234b63a2007-03-13 10:46:10 -0400361 struct btrfs_leaf *l;
362 struct btrfs_extent_item *item;
Chris Mason5caf2a02007-04-02 11:20:42 -0400363
364 path = btrfs_alloc_path();
365 btrfs_init_path(path);
Chris Masona28ec192007-03-06 20:08:01 -0500366 key.objectid = blocknr;
Chris Mason6407bf62007-03-27 06:33:00 -0400367 key.offset = num_blocks;
Chris Mason62e27492007-03-15 12:56:47 -0400368 key.flags = 0;
369 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Mason5caf2a02007-04-02 11:20:42 -0400370 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -0400371 0, 0);
Chris Masona28ec192007-03-06 20:08:01 -0500372 if (ret != 0)
373 BUG();
Chris Mason5caf2a02007-04-02 11:20:42 -0400374 l = btrfs_buffer_leaf(path->nodes[0]);
375 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400376 *refs = btrfs_extent_refs(item);
Chris Mason5caf2a02007-04-02 11:20:42 -0400377 btrfs_release_path(root->fs_info->extent_root, path);
378 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -0500379 return 0;
380}
381
Chris Masonc5739bb2007-04-10 09:27:04 -0400382int btrfs_inc_root_ref(struct btrfs_trans_handle *trans,
383 struct btrfs_root *root)
384{
Chris Masonb18c6682007-04-17 13:26:50 -0400385 return btrfs_inc_extent_ref(trans, root, bh_blocknr(root->node), 1);
Chris Masonc5739bb2007-04-10 09:27:04 -0400386}
387
Chris Masone089f052007-03-16 16:20:31 -0400388int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Masone20d96d2007-03-22 12:13:20 -0400389 struct buffer_head *buf)
Chris Mason02217ed2007-03-02 16:08:05 -0500390{
391 u64 blocknr;
Chris Masone20d96d2007-03-22 12:13:20 -0400392 struct btrfs_node *buf_node;
Chris Mason6407bf62007-03-27 06:33:00 -0400393 struct btrfs_leaf *buf_leaf;
394 struct btrfs_disk_key *key;
395 struct btrfs_file_extent_item *fi;
Chris Mason02217ed2007-03-02 16:08:05 -0500396 int i;
Chris Mason6407bf62007-03-27 06:33:00 -0400397 int leaf;
398 int ret;
Chris Masona28ec192007-03-06 20:08:01 -0500399
Chris Mason3768f362007-03-13 16:47:54 -0400400 if (!root->ref_cows)
Chris Masona28ec192007-03-06 20:08:01 -0500401 return 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400402 buf_node = btrfs_buffer_node(buf);
Chris Mason6407bf62007-03-27 06:33:00 -0400403 leaf = btrfs_is_leaf(buf_node);
404 buf_leaf = btrfs_buffer_leaf(buf);
Chris Masone20d96d2007-03-22 12:13:20 -0400405 for (i = 0; i < btrfs_header_nritems(&buf_node->header); i++) {
Chris Mason6407bf62007-03-27 06:33:00 -0400406 if (leaf) {
407 key = &buf_leaf->items[i].key;
408 if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY)
409 continue;
410 fi = btrfs_item_ptr(buf_leaf, i,
411 struct btrfs_file_extent_item);
Chris Mason236454d2007-04-19 13:37:44 -0400412 if (btrfs_file_extent_type(fi) ==
413 BTRFS_FILE_EXTENT_INLINE)
414 continue;
Chris Masonb18c6682007-04-17 13:26:50 -0400415 ret = btrfs_inc_extent_ref(trans, root,
Chris Mason6407bf62007-03-27 06:33:00 -0400416 btrfs_file_extent_disk_blocknr(fi),
417 btrfs_file_extent_disk_num_blocks(fi));
418 BUG_ON(ret);
419 } else {
420 blocknr = btrfs_node_blockptr(buf_node, i);
Chris Masonb18c6682007-04-17 13:26:50 -0400421 ret = btrfs_inc_extent_ref(trans, root, blocknr, 1);
Chris Mason6407bf62007-03-27 06:33:00 -0400422 BUG_ON(ret);
423 }
Chris Mason02217ed2007-03-02 16:08:05 -0500424 }
425 return 0;
426}
427
Chris Mason9078a3e2007-04-26 16:46:15 -0400428static int write_one_cache_group(struct btrfs_trans_handle *trans,
429 struct btrfs_root *root,
430 struct btrfs_path *path,
431 struct btrfs_block_group_cache *cache)
432{
433 int ret;
434 int pending_ret;
435 struct btrfs_root *extent_root = root->fs_info->extent_root;
436 struct btrfs_block_group_item *bi;
437 struct btrfs_key ins;
438
Chris Masonbe08c1b2007-05-03 09:06:49 -0400439 find_free_extent(trans, extent_root, 0, 0, (u64)-1, &ins, 0);
Chris Mason9078a3e2007-04-26 16:46:15 -0400440 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
441 BUG_ON(ret);
442 bi = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
443 struct btrfs_block_group_item);
444 memcpy(bi, &cache->item, sizeof(*bi));
445 mark_buffer_dirty(path->nodes[0]);
446 btrfs_release_path(extent_root, path);
447
448 finish_current_insert(trans, extent_root);
449 pending_ret = del_pending_extents(trans, extent_root);
450 if (ret)
451 return ret;
452 if (pending_ret)
453 return pending_ret;
Chris Masonbe744172007-05-06 10:15:01 -0400454 if (cache->data)
455 cache->last_alloc = cache->first_free;
Chris Mason9078a3e2007-04-26 16:46:15 -0400456 return 0;
457
458}
459
Chris Masonbe744172007-05-06 10:15:01 -0400460static int write_dirty_block_radix(struct btrfs_trans_handle *trans,
461 struct btrfs_root *root,
462 struct radix_tree_root *radix)
Chris Mason9078a3e2007-04-26 16:46:15 -0400463{
464 struct btrfs_block_group_cache *cache[8];
465 int ret;
466 int err = 0;
467 int werr = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -0400468 int i;
469 struct btrfs_path *path;
470
471 path = btrfs_alloc_path();
472 if (!path)
473 return -ENOMEM;
474
475 while(1) {
476 ret = radix_tree_gang_lookup_tag(radix, (void **)cache,
477 0, ARRAY_SIZE(cache),
478 BTRFS_BLOCK_GROUP_DIRTY);
479 if (!ret)
480 break;
481 for (i = 0; i < ret; i++) {
482 radix_tree_tag_clear(radix, cache[i]->key.objectid +
483 cache[i]->key.offset - 1,
484 BTRFS_BLOCK_GROUP_DIRTY);
485 err = write_one_cache_group(trans, root,
486 path, cache[i]);
487 if (err)
488 werr = err;
489 }
490 }
491 btrfs_free_path(path);
492 return werr;
493}
494
Chris Masonbe744172007-05-06 10:15:01 -0400495int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
496 struct btrfs_root *root)
497{
498 int ret;
499 int ret2;
500 ret = write_dirty_block_radix(trans, root,
501 &root->fs_info->block_group_radix);
502 ret2 = write_dirty_block_radix(trans, root,
503 &root->fs_info->block_group_data_radix);
504 if (ret)
505 return ret;
506 if (ret2)
507 return ret2;
508 return 0;
509}
510
Chris Mason9078a3e2007-04-26 16:46:15 -0400511static int update_block_group(struct btrfs_trans_handle *trans,
512 struct btrfs_root *root,
Chris Masone37c9e62007-05-09 20:13:14 -0400513 u64 blocknr, u64 num, int alloc, int mark_free)
Chris Mason9078a3e2007-04-26 16:46:15 -0400514{
515 struct btrfs_block_group_cache *cache;
516 struct btrfs_fs_info *info = root->fs_info;
517 u64 total = num;
518 u64 old_val;
519 u64 block_in_group;
Chris Masone37c9e62007-05-09 20:13:14 -0400520 u64 i;
Chris Mason3e1ad542007-05-07 20:03:49 -0400521
Chris Mason9078a3e2007-04-26 16:46:15 -0400522 while(total) {
Chris Mason3e1ad542007-05-07 20:03:49 -0400523 cache = lookup_block_group(info, blocknr);
524 if (!cache) {
Chris Masoncd1bc462007-04-27 10:08:34 -0400525 printk(KERN_CRIT "blocknr %Lu lookup failed\n",
526 blocknr);
Chris Mason9078a3e2007-04-26 16:46:15 -0400527 return -1;
Chris Masoncd1bc462007-04-27 10:08:34 -0400528 }
Chris Mason9078a3e2007-04-26 16:46:15 -0400529 block_in_group = blocknr - cache->key.objectid;
530 WARN_ON(block_in_group > cache->key.offset);
Chris Mason3e1ad542007-05-07 20:03:49 -0400531 radix_tree_tag_set(cache->radix, cache->key.objectid +
Chris Masonbe744172007-05-06 10:15:01 -0400532 cache->key.offset - 1,
Chris Mason9078a3e2007-04-26 16:46:15 -0400533 BTRFS_BLOCK_GROUP_DIRTY);
534
535 old_val = btrfs_block_group_used(&cache->item);
536 num = min(total, cache->key.offset - block_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -0400537 if (alloc) {
Chris Mason9078a3e2007-04-26 16:46:15 -0400538 old_val += num;
Chris Masoncd1bc462007-04-27 10:08:34 -0400539 if (blocknr > cache->last_alloc)
540 cache->last_alloc = blocknr;
Chris Masone37c9e62007-05-09 20:13:14 -0400541 if (!cache->data) {
542 for (i = 0; i < num; i++) {
543 clear_radix_bit(&info->extent_map_radix,
544 blocknr + i);
545 }
546 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400547 } else {
Chris Mason9078a3e2007-04-26 16:46:15 -0400548 old_val -= num;
Chris Masoncd1bc462007-04-27 10:08:34 -0400549 if (blocknr < cache->first_free)
550 cache->first_free = blocknr;
Chris Masone37c9e62007-05-09 20:13:14 -0400551 if (!cache->data && mark_free) {
552 for (i = 0; i < num; i++) {
553 set_radix_bit(&info->extent_map_radix,
554 blocknr + i);
555 }
556 }
557 if (old_val < (cache->key.offset * 8) / 10 &&
558 old_val + num >= (cache->key.offset * 8) / 10) {
559printk("group %Lu now available\n", cache->key.objectid);
560 radix_tree_tag_set(cache->radix,
561 cache->key.objectid +
562 cache->key.offset - 1,
563 BTRFS_BLOCK_GROUP_AVAIL);
564 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400565 }
Chris Mason9078a3e2007-04-26 16:46:15 -0400566 btrfs_set_block_group_used(&cache->item, old_val);
Chris Masone37c9e62007-05-09 20:13:14 -0400567 total -= num;
568 blocknr += num;
Chris Mason9078a3e2007-04-26 16:46:15 -0400569 }
570 return 0;
571}
572
Chris Masonbe08c1b2007-05-03 09:06:49 -0400573static int try_remove_page(struct address_space *mapping, unsigned long index)
574{
575 int ret;
576 ret = invalidate_mapping_pages(mapping, index, index);
577 return ret;
578}
579
Chris Masone089f052007-03-16 16:20:31 -0400580int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, struct
581 btrfs_root *root)
Chris Masona28ec192007-03-06 20:08:01 -0500582{
Chris Mason8ef97622007-03-26 10:15:30 -0400583 unsigned long gang[8];
Chris Masonbe08c1b2007-05-03 09:06:49 -0400584 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Masonbe744172007-05-06 10:15:01 -0400585 struct btrfs_block_group_cache *block_group;
Chris Mason88fd1462007-03-16 08:56:18 -0400586 u64 first = 0;
Chris Masona28ec192007-03-06 20:08:01 -0500587 int ret;
588 int i;
Chris Mason8ef97622007-03-26 10:15:30 -0400589 struct radix_tree_root *pinned_radix = &root->fs_info->pinned_radix;
Chris Masone37c9e62007-05-09 20:13:14 -0400590 struct radix_tree_root *extent_radix = &root->fs_info->extent_map_radix;
Chris Masona28ec192007-03-06 20:08:01 -0500591
592 while(1) {
Chris Masone37c9e62007-05-09 20:13:14 -0400593 ret = find_first_radix_bit(pinned_radix, gang, 0,
Chris Mason8ef97622007-03-26 10:15:30 -0400594 ARRAY_SIZE(gang));
Chris Masona28ec192007-03-06 20:08:01 -0500595 if (!ret)
596 break;
Chris Mason88fd1462007-03-16 08:56:18 -0400597 if (!first)
Chris Mason8ef97622007-03-26 10:15:30 -0400598 first = gang[0];
Chris Mason0579da42007-03-07 16:15:30 -0500599 for (i = 0; i < ret; i++) {
Chris Mason8ef97622007-03-26 10:15:30 -0400600 clear_radix_bit(pinned_radix, gang[i]);
Chris Masonbe744172007-05-06 10:15:01 -0400601 block_group = lookup_block_group(root->fs_info,
602 gang[i]);
603 if (block_group) {
604 WARN_ON(block_group->pinned == 0);
605 block_group->pinned--;
606 if (gang[i] < block_group->last_alloc)
607 block_group->last_alloc = gang[i];
Chris Masone37c9e62007-05-09 20:13:14 -0400608 if (gang[i] < block_group->last_prealloc)
609 block_group->last_prealloc = gang[i];
610 if (!block_group->data)
611 set_radix_bit(extent_radix, gang[i]);
Chris Masonbe744172007-05-06 10:15:01 -0400612 }
Chris Masonbe08c1b2007-05-03 09:06:49 -0400613 try_remove_page(btree_inode->i_mapping,
614 gang[i] << (PAGE_CACHE_SHIFT -
615 btree_inode->i_blkbits));
Chris Mason0579da42007-03-07 16:15:30 -0500616 }
Chris Masona28ec192007-03-06 20:08:01 -0500617 }
618 return 0;
619}
620
Chris Masone089f052007-03-16 16:20:31 -0400621static int finish_current_insert(struct btrfs_trans_handle *trans, struct
622 btrfs_root *extent_root)
Chris Mason037e6392007-03-07 11:50:24 -0500623{
Chris Masone2fa7222007-03-12 16:22:34 -0400624 struct btrfs_key ins;
Chris Mason234b63a2007-03-13 10:46:10 -0400625 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -0500626 int i;
627 int ret;
Chris Mason1261ec42007-03-20 20:35:03 -0400628 u64 super_blocks_used;
629 struct btrfs_fs_info *info = extent_root->fs_info;
Chris Mason037e6392007-03-07 11:50:24 -0500630
Chris Masoncf27e1e2007-03-13 09:49:06 -0400631 btrfs_set_extent_refs(&extent_item, 1);
Chris Mason037e6392007-03-07 11:50:24 -0500632 ins.offset = 1;
633 ins.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -0400634 btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
Chris Mason5d0c3e62007-04-23 17:01:05 -0400635 btrfs_set_extent_owner(&extent_item, extent_root->root_key.objectid);
Chris Mason037e6392007-03-07 11:50:24 -0500636
Chris Masonf2458e12007-04-25 15:52:25 -0400637 for (i = 0; i < extent_root->fs_info->extent_tree_insert_nr; i++) {
638 ins.objectid = extent_root->fs_info->extent_tree_insert[i];
Chris Mason1261ec42007-03-20 20:35:03 -0400639 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
640 btrfs_set_super_blocks_used(info->disk_super,
641 super_blocks_used + 1);
Chris Masone089f052007-03-16 16:20:31 -0400642 ret = btrfs_insert_item(trans, extent_root, &ins, &extent_item,
643 sizeof(extent_item));
Chris Mason037e6392007-03-07 11:50:24 -0500644 BUG_ON(ret);
645 }
Chris Masonf2458e12007-04-25 15:52:25 -0400646 extent_root->fs_info->extent_tree_insert_nr = 0;
647 extent_root->fs_info->extent_tree_prealloc_nr = 0;
Chris Mason037e6392007-03-07 11:50:24 -0500648 return 0;
649}
650
Chris Mason8ef97622007-03-26 10:15:30 -0400651static int pin_down_block(struct btrfs_root *root, u64 blocknr, int pending)
Chris Masone20d96d2007-03-22 12:13:20 -0400652{
653 int err;
Chris Mason78fae272007-03-25 11:35:08 -0400654 struct btrfs_header *header;
Chris Mason8ef97622007-03-26 10:15:30 -0400655 struct buffer_head *bh;
Chris Mason78fae272007-03-25 11:35:08 -0400656
Chris Masonf4b9aa82007-03-27 11:05:53 -0400657 if (!pending) {
Chris Masond98237b2007-03-28 13:57:48 -0400658 bh = btrfs_find_tree_block(root, blocknr);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400659 if (bh) {
660 if (buffer_uptodate(bh)) {
661 u64 transid =
662 root->fs_info->running_transaction->transid;
663 header = btrfs_buffer_header(bh);
664 if (btrfs_header_generation(header) ==
665 transid) {
666 btrfs_block_release(root, bh);
667 return 0;
668 }
Chris Masonf4b9aa82007-03-27 11:05:53 -0400669 }
Chris Masond6025572007-03-30 14:27:56 -0400670 btrfs_block_release(root, bh);
Chris Mason8ef97622007-03-26 10:15:30 -0400671 }
Chris Mason8ef97622007-03-26 10:15:30 -0400672 err = set_radix_bit(&root->fs_info->pinned_radix, blocknr);
Chris Masonbe744172007-05-06 10:15:01 -0400673 if (!err) {
674 struct btrfs_block_group_cache *cache;
675 cache = lookup_block_group(root->fs_info, blocknr);
676 if (cache)
677 cache->pinned++;
678 }
Chris Masonf4b9aa82007-03-27 11:05:53 -0400679 } else {
680 err = set_radix_bit(&root->fs_info->pending_del_radix, blocknr);
681 }
Chris Masonbe744172007-05-06 10:15:01 -0400682 BUG_ON(err < 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400683 return 0;
684}
685
Chris Masona28ec192007-03-06 20:08:01 -0500686/*
687 * remove an extent from the root, returns 0 on success
688 */
Chris Masone089f052007-03-16 16:20:31 -0400689static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masone37c9e62007-05-09 20:13:14 -0400690 *root, u64 blocknr, u64 num_blocks, int pin,
691 int mark_free)
Chris Masona28ec192007-03-06 20:08:01 -0500692{
Chris Mason5caf2a02007-04-02 11:20:42 -0400693 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -0400694 struct btrfs_key key;
Chris Mason1261ec42007-03-20 20:35:03 -0400695 struct btrfs_fs_info *info = root->fs_info;
696 struct btrfs_root *extent_root = info->extent_root;
Chris Masona28ec192007-03-06 20:08:01 -0500697 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400698 struct btrfs_extent_item *ei;
Chris Masone2fa7222007-03-12 16:22:34 -0400699 struct btrfs_key ins;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400700 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500701
Chris Masona28ec192007-03-06 20:08:01 -0500702 key.objectid = blocknr;
703 key.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -0400704 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masona28ec192007-03-06 20:08:01 -0500705 key.offset = num_blocks;
706
Chris Masonbe08c1b2007-05-03 09:06:49 -0400707 find_free_extent(trans, root, 0, 0, (u64)-1, &ins, 0);
Chris Mason5caf2a02007-04-02 11:20:42 -0400708 path = btrfs_alloc_path();
709 BUG_ON(!path);
710 btrfs_init_path(path);
Chris Mason5f26f772007-04-05 10:38:44 -0400711
Chris Mason5caf2a02007-04-02 11:20:42 -0400712 ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
Chris Masona28ec192007-03-06 20:08:01 -0500713 if (ret) {
Chris Mason2e635a22007-03-21 11:12:56 -0400714 printk("failed to find %Lu\n", key.objectid);
Chris Mason234b63a2007-03-13 10:46:10 -0400715 btrfs_print_tree(extent_root, extent_root->node);
Chris Mason2e635a22007-03-21 11:12:56 -0400716 printk("failed to find %Lu\n", key.objectid);
Chris Masona28ec192007-03-06 20:08:01 -0500717 BUG();
718 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400719 ei = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
Chris Mason123abc82007-03-14 14:14:43 -0400720 struct btrfs_extent_item);
Chris Masona28ec192007-03-06 20:08:01 -0500721 BUG_ON(ei->refs == 0);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400722 refs = btrfs_extent_refs(ei) - 1;
723 btrfs_set_extent_refs(ei, refs);
Chris Mason5caf2a02007-04-02 11:20:42 -0400724 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400725 if (refs == 0) {
Chris Mason1261ec42007-03-20 20:35:03 -0400726 u64 super_blocks_used;
Chris Mason78fae272007-03-25 11:35:08 -0400727
728 if (pin) {
Chris Mason8ef97622007-03-26 10:15:30 -0400729 ret = pin_down_block(root, blocknr, 0);
Chris Mason78fae272007-03-25 11:35:08 -0400730 BUG_ON(ret);
731 }
732
Chris Mason1261ec42007-03-20 20:35:03 -0400733 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
734 btrfs_set_super_blocks_used(info->disk_super,
735 super_blocks_used - num_blocks);
Chris Mason5caf2a02007-04-02 11:20:42 -0400736 ret = btrfs_del_item(trans, extent_root, path);
Chris Masona28ec192007-03-06 20:08:01 -0500737 if (ret)
738 BUG();
Chris Masone37c9e62007-05-09 20:13:14 -0400739 ret = update_block_group(trans, root, blocknr, num_blocks, 0,
740 mark_free);
Chris Mason9078a3e2007-04-26 16:46:15 -0400741 BUG_ON(ret);
Chris Masona28ec192007-03-06 20:08:01 -0500742 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400743 btrfs_free_path(path);
Chris Masone089f052007-03-16 16:20:31 -0400744 finish_current_insert(trans, extent_root);
Chris Masona28ec192007-03-06 20:08:01 -0500745 return ret;
746}
747
748/*
Chris Masonfec577f2007-02-26 10:40:21 -0500749 * find all the blocks marked as pending in the radix tree and remove
750 * them from the extent map
751 */
Chris Masone089f052007-03-16 16:20:31 -0400752static int del_pending_extents(struct btrfs_trans_handle *trans, struct
753 btrfs_root *extent_root)
Chris Masonfec577f2007-02-26 10:40:21 -0500754{
755 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -0400756 int wret;
757 int err = 0;
Chris Mason8ef97622007-03-26 10:15:30 -0400758 unsigned long gang[4];
Chris Masonfec577f2007-02-26 10:40:21 -0500759 int i;
Chris Mason8ef97622007-03-26 10:15:30 -0400760 struct radix_tree_root *pending_radix;
761 struct radix_tree_root *pinned_radix;
Chris Masonbe744172007-05-06 10:15:01 -0400762 struct btrfs_block_group_cache *cache;
Chris Mason8ef97622007-03-26 10:15:30 -0400763
764 pending_radix = &extent_root->fs_info->pending_del_radix;
765 pinned_radix = &extent_root->fs_info->pinned_radix;
Chris Masonfec577f2007-02-26 10:40:21 -0500766
767 while(1) {
Chris Masone37c9e62007-05-09 20:13:14 -0400768 ret = find_first_radix_bit(pending_radix, gang, 0,
Chris Mason8ef97622007-03-26 10:15:30 -0400769 ARRAY_SIZE(gang));
Chris Masonfec577f2007-02-26 10:40:21 -0500770 if (!ret)
771 break;
772 for (i = 0; i < ret; i++) {
Chris Mason8ef97622007-03-26 10:15:30 -0400773 wret = set_radix_bit(pinned_radix, gang[i]);
Chris Masonbe744172007-05-06 10:15:01 -0400774 if (wret == 0) {
775 cache = lookup_block_group(extent_root->fs_info,
776 gang[i]);
777 if (cache)
778 cache->pinned++;
779 }
780 if (wret < 0) {
781 printk(KERN_CRIT "set_radix_bit, err %d\n",
782 wret);
783 BUG_ON(wret < 0);
784 }
Chris Mason8ef97622007-03-26 10:15:30 -0400785 wret = clear_radix_bit(pending_radix, gang[i]);
786 BUG_ON(wret);
Chris Masond5719762007-03-23 10:01:08 -0400787 wret = __free_extent(trans, extent_root,
Chris Masone37c9e62007-05-09 20:13:14 -0400788 gang[i], 1, 0, 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400789 if (wret)
790 err = wret;
Chris Masonfec577f2007-02-26 10:40:21 -0500791 }
792 }
Chris Masone20d96d2007-03-22 12:13:20 -0400793 return err;
Chris Masonfec577f2007-02-26 10:40:21 -0500794}
795
796/*
797 * remove an extent from the root, returns 0 on success
798 */
Chris Masone089f052007-03-16 16:20:31 -0400799int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
800 *root, u64 blocknr, u64 num_blocks, int pin)
Chris Masonfec577f2007-02-26 10:40:21 -0500801{
Chris Mason9f5fae22007-03-20 14:38:32 -0400802 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Masonfec577f2007-02-26 10:40:21 -0500803 int pending_ret;
804 int ret;
Chris Masona28ec192007-03-06 20:08:01 -0500805
806 if (root == extent_root) {
Chris Mason8ef97622007-03-26 10:15:30 -0400807 pin_down_block(root, blocknr, 1);
Chris Masona28ec192007-03-06 20:08:01 -0500808 return 0;
809 }
Chris Masone37c9e62007-05-09 20:13:14 -0400810 ret = __free_extent(trans, root, blocknr, num_blocks, pin, pin == 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400811 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
Chris Masonfec577f2007-02-26 10:40:21 -0500812 return ret ? ret : pending_ret;
813}
814
815/*
816 * walks the btree of allocated extents and find a hole of a given size.
817 * The key ins is changed to record the hole:
818 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -0400819 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -0500820 * ins->offset == number of blocks
821 * Any available blocks before search_start are skipped.
822 */
Chris Masone089f052007-03-16 16:20:31 -0400823static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
824 *orig_root, u64 num_blocks, u64 search_start, u64
Chris Masonbe08c1b2007-05-03 09:06:49 -0400825 search_end, struct btrfs_key *ins, int data)
Chris Masonfec577f2007-02-26 10:40:21 -0500826{
Chris Mason5caf2a02007-04-02 11:20:42 -0400827 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -0400828 struct btrfs_key key;
Chris Masonfec577f2007-02-26 10:40:21 -0500829 int ret;
830 u64 hole_size = 0;
831 int slot = 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400832 u64 last_block = 0;
Chris Mason037e6392007-03-07 11:50:24 -0500833 u64 test_block;
Chris Masonbe744172007-05-06 10:15:01 -0400834 u64 orig_search_start = search_start;
Chris Masonfec577f2007-02-26 10:40:21 -0500835 int start_found;
Chris Mason234b63a2007-03-13 10:46:10 -0400836 struct btrfs_leaf *l;
Chris Mason9f5fae22007-03-20 14:38:32 -0400837 struct btrfs_root * root = orig_root->fs_info->extent_root;
Chris Masonf2458e12007-04-25 15:52:25 -0400838 struct btrfs_fs_info *info = root->fs_info;
Chris Mason0579da42007-03-07 16:15:30 -0500839 int total_needed = num_blocks;
Chris Masonf2458e12007-04-25 15:52:25 -0400840 int total_found = 0;
841 int fill_prealloc = 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400842 int level;
Chris Masonbe08c1b2007-05-03 09:06:49 -0400843 struct btrfs_block_group_cache *block_group;
Chris Masonbe744172007-05-06 10:15:01 -0400844 int full_scan = 0;
Chris Masonfec577f2007-02-26 10:40:21 -0500845
Chris Masonb1a4d962007-04-04 15:27:52 -0400846 path = btrfs_alloc_path();
847 ins->flags = 0;
848 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
849
Chris Masone20d96d2007-03-22 12:13:20 -0400850 level = btrfs_header_level(btrfs_buffer_header(root->node));
Chris Masonf2458e12007-04-25 15:52:25 -0400851 if (num_blocks == 0) {
852 fill_prealloc = 1;
853 num_blocks = 1;
Chris Mason308535a2007-04-28 15:17:08 -0400854 total_needed = (min(level + 1, BTRFS_MAX_LEVEL) + 2) * 3;
Chris Masonf2458e12007-04-25 15:52:25 -0400855 }
Chris Mason3e1ad542007-05-07 20:03:49 -0400856 if (search_end == (u64)-1)
857 search_end = btrfs_super_total_blocks(info->disk_super);
Chris Masonbe744172007-05-06 10:15:01 -0400858 if (search_start) {
859 block_group = lookup_block_group(info, search_start);
860 block_group = btrfs_find_block_group(root, block_group,
861 search_start, data);
862 } else {
863 block_group = btrfs_find_block_group(root,
864 trans->block_group, 0,
865 data);
866 }
867
868check_failed:
Chris Mason3e1ad542007-05-07 20:03:49 -0400869 if (!full_scan && block_group->data != data)
Chris Masonbe744172007-05-06 10:15:01 -0400870 WARN_ON(1);
Chris Masone37c9e62007-05-09 20:13:14 -0400871
872 if (!data)
873 search_start = find_search_start(root, &block_group,
874 search_start, total_needed);
875 else
876 search_start = max(block_group->last_alloc, search_start);
877
Chris Mason5caf2a02007-04-02 11:20:42 -0400878 btrfs_init_path(path);
Chris Masonfec577f2007-02-26 10:40:21 -0500879 ins->objectid = search_start;
880 ins->offset = 0;
Chris Masonfec577f2007-02-26 10:40:21 -0500881 start_found = 0;
Chris Masone37c9e62007-05-09 20:13:14 -0400882
Chris Mason5caf2a02007-04-02 11:20:42 -0400883 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
Chris Mason0f70abe2007-02-28 16:46:22 -0500884 if (ret < 0)
885 goto error;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500886
Chris Masone37c9e62007-05-09 20:13:14 -0400887 if (path->slots[0] > 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400888 path->slots[0]--;
Chris Masone37c9e62007-05-09 20:13:14 -0400889 }
890
891 l = btrfs_buffer_leaf(path->nodes[0]);
892 btrfs_disk_key_to_cpu(&key, &l->items[path->slots[0]].key);
893 /*
894 * a rare case, go back one key if we hit a block group item
895 * instead of an extent item
896 */
897 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY &&
898 key.objectid + key.offset >= search_start) {
899 ins->objectid = key.objectid;
900 ins->offset = key.offset - 1;
901 btrfs_release_path(root, path);
902 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
903 if (ret < 0)
904 goto error;
905
906 if (path->slots[0] > 0) {
907 path->slots[0]--;
908 }
909 }
Chris Mason0579da42007-03-07 16:15:30 -0500910
Chris Masonfec577f2007-02-26 10:40:21 -0500911 while (1) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400912 l = btrfs_buffer_leaf(path->nodes[0]);
913 slot = path->slots[0];
Chris Mason7518a232007-03-12 12:01:18 -0400914 if (slot >= btrfs_header_nritems(&l->header)) {
Chris Masonf2458e12007-04-25 15:52:25 -0400915 if (fill_prealloc) {
916 info->extent_tree_prealloc_nr = 0;
917 total_found = 0;
918 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400919 ret = btrfs_next_leaf(root, path);
Chris Masonfec577f2007-02-26 10:40:21 -0500920 if (ret == 0)
921 continue;
Chris Mason0f70abe2007-02-28 16:46:22 -0500922 if (ret < 0)
923 goto error;
Chris Masonfec577f2007-02-26 10:40:21 -0500924 if (!start_found) {
925 ins->objectid = search_start;
Chris Mason3e1ad542007-05-07 20:03:49 -0400926 ins->offset = search_end - search_start;
Chris Masonfec577f2007-02-26 10:40:21 -0500927 start_found = 1;
928 goto check_pending;
929 }
930 ins->objectid = last_block > search_start ?
931 last_block : search_start;
Chris Mason3e1ad542007-05-07 20:03:49 -0400932 ins->offset = search_end - ins->objectid;
Chris Masonfec577f2007-02-26 10:40:21 -0500933 goto check_pending;
934 }
Chris Masone37c9e62007-05-09 20:13:14 -0400935
Chris Masone2fa7222007-03-12 16:22:34 -0400936 btrfs_disk_key_to_cpu(&key, &l->items[slot].key);
Chris Masone37c9e62007-05-09 20:13:14 -0400937 if (key.objectid >= search_start && key.objectid > last_block &&
938 start_found) {
939 if (last_block < search_start)
940 last_block = search_start;
941 hole_size = key.objectid - last_block;
942 if (hole_size >= num_blocks) {
943 ins->objectid = last_block;
944 ins->offset = hole_size;
945 goto check_pending;
Chris Mason0579da42007-03-07 16:15:30 -0500946 }
Chris Masonfec577f2007-02-26 10:40:21 -0500947 }
Chris Masone37c9e62007-05-09 20:13:14 -0400948
949 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY)
950 goto next;
951
Chris Mason0579da42007-03-07 16:15:30 -0500952 start_found = 1;
Chris Masone2fa7222007-03-12 16:22:34 -0400953 last_block = key.objectid + key.offset;
Chris Masonbe744172007-05-06 10:15:01 -0400954 if (last_block >= block_group->key.objectid +
955 block_group->key.offset) {
956 btrfs_release_path(root, path);
957 search_start = block_group->key.objectid +
958 block_group->key.offset * 2;
959 goto new_group;
960 }
Chris Mason9078a3e2007-04-26 16:46:15 -0400961next:
Chris Mason5caf2a02007-04-02 11:20:42 -0400962 path->slots[0]++;
Chris Masonfec577f2007-02-26 10:40:21 -0500963 }
964 // FIXME -ENOSPC
965check_pending:
966 /* we have to make sure we didn't find an extent that has already
967 * been allocated by the map tree or the original allocation
968 */
Chris Mason5caf2a02007-04-02 11:20:42 -0400969 btrfs_release_path(root, path);
Chris Masonfec577f2007-02-26 10:40:21 -0500970 BUG_ON(ins->objectid < search_start);
Chris Masone37c9e62007-05-09 20:13:14 -0400971
Chris Mason3e1ad542007-05-07 20:03:49 -0400972 if (ins->objectid + num_blocks >= search_end) {
Chris Masonbe744172007-05-06 10:15:01 -0400973 if (full_scan)
Chris Mason06a2f9f2007-04-28 08:48:10 -0400974 return -ENOSPC;
Chris Masonbe744172007-05-06 10:15:01 -0400975 search_start = orig_search_start;
976 full_scan = 1;
977 goto new_group;
Chris Mason06a2f9f2007-04-28 08:48:10 -0400978 }
Chris Mason037e6392007-03-07 11:50:24 -0500979 for (test_block = ins->objectid;
Chris Masonf2458e12007-04-25 15:52:25 -0400980 test_block < ins->objectid + num_blocks; test_block++) {
981 if (test_radix_bit(&info->pinned_radix, test_block)) {
Chris Mason037e6392007-03-07 11:50:24 -0500982 search_start = test_block + 1;
Chris Masonbe744172007-05-06 10:15:01 -0400983 goto new_group;
Chris Masonfec577f2007-02-26 10:40:21 -0500984 }
985 }
Chris Masonf2458e12007-04-25 15:52:25 -0400986 if (!fill_prealloc && info->extent_tree_insert_nr) {
987 u64 last =
988 info->extent_tree_insert[info->extent_tree_insert_nr - 1];
989 if (ins->objectid + num_blocks >
990 info->extent_tree_insert[0] &&
991 ins->objectid <= last) {
992 search_start = last + 1;
Chris Masone37c9e62007-05-09 20:13:14 -0400993 WARN_ON(!full_scan);
Chris Masonbe744172007-05-06 10:15:01 -0400994 goto new_group;
Chris Masonf2458e12007-04-25 15:52:25 -0400995 }
996 }
997 if (!fill_prealloc && info->extent_tree_prealloc_nr) {
998 u64 first =
999 info->extent_tree_prealloc[info->extent_tree_prealloc_nr - 1];
1000 if (ins->objectid + num_blocks > first &&
1001 ins->objectid <= info->extent_tree_prealloc[0]) {
1002 search_start = info->extent_tree_prealloc[0] + 1;
Chris Masone37c9e62007-05-09 20:13:14 -04001003 WARN_ON(!full_scan);
Chris Masonbe744172007-05-06 10:15:01 -04001004 goto new_group;
Chris Masonf2458e12007-04-25 15:52:25 -04001005 }
1006 }
1007 if (fill_prealloc) {
1008 int nr;
1009 test_block = ins->objectid;
Chris Masone37c9e62007-05-09 20:13:14 -04001010 if (test_block - info->extent_tree_prealloc[total_needed - 1] >=
1011 leaf_range(root)) {
1012 total_found = 0;
1013 info->extent_tree_prealloc_nr = total_found;
1014 }
Chris Masonf2458e12007-04-25 15:52:25 -04001015 while(test_block < ins->objectid + ins->offset &&
1016 total_found < total_needed) {
1017 nr = total_needed - total_found - 1;
1018 BUG_ON(nr < 0);
Chris Masoncd1bc462007-04-27 10:08:34 -04001019 info->extent_tree_prealloc[nr] = test_block;
Chris Masonf2458e12007-04-25 15:52:25 -04001020 total_found++;
1021 test_block++;
1022 }
1023 if (total_found < total_needed) {
1024 search_start = test_block;
Chris Masonbe744172007-05-06 10:15:01 -04001025 goto new_group;
Chris Masonf2458e12007-04-25 15:52:25 -04001026 }
Chris Masoncd1bc462007-04-27 10:08:34 -04001027 info->extent_tree_prealloc_nr = total_found;
Chris Masonf2458e12007-04-25 15:52:25 -04001028 }
Chris Masone37c9e62007-05-09 20:13:14 -04001029 if (!data) {
1030 block_group = lookup_block_group(info, ins->objectid);
1031 if (block_group) {
1032 if (fill_prealloc)
1033 block_group->last_prealloc =
1034 info->extent_tree_prealloc[total_needed-1];
1035 else
1036 trans->block_group = block_group;
1037 }
Chris Masoncd1bc462007-04-27 10:08:34 -04001038 }
Chris Mason037e6392007-03-07 11:50:24 -05001039 ins->offset = num_blocks;
Chris Mason5caf2a02007-04-02 11:20:42 -04001040 btrfs_free_path(path);
Chris Masonfec577f2007-02-26 10:40:21 -05001041 return 0;
Chris Masonbe744172007-05-06 10:15:01 -04001042
1043new_group:
Chris Mason3e1ad542007-05-07 20:03:49 -04001044 if (search_start + num_blocks >= search_end) {
Chris Masonbe744172007-05-06 10:15:01 -04001045 search_start = orig_search_start;
Chris Masone37c9e62007-05-09 20:13:14 -04001046printk("doing full scan!\n");
Chris Masonbe744172007-05-06 10:15:01 -04001047 full_scan = 1;
1048 }
1049 block_group = lookup_block_group(info, search_start);
1050 if (!full_scan)
1051 block_group = btrfs_find_block_group(root, block_group,
1052 search_start, data);
1053 goto check_failed;
1054
Chris Mason0f70abe2007-02-28 16:46:22 -05001055error:
Chris Mason5caf2a02007-04-02 11:20:42 -04001056 btrfs_release_path(root, path);
1057 btrfs_free_path(path);
Chris Mason0f70abe2007-02-28 16:46:22 -05001058 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05001059}
Chris Masonfec577f2007-02-26 10:40:21 -05001060/*
Chris Masonfec577f2007-02-26 10:40:21 -05001061 * finds a free extent and does all the dirty work required for allocation
1062 * returns the key for the extent through ins, and a tree buffer for
1063 * the first block of the extent through buf.
1064 *
1065 * returns 0 if everything worked, non-zero otherwise.
1066 */
Chris Mason4d775672007-04-20 20:23:12 -04001067int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
1068 struct btrfs_root *root, u64 owner,
Chris Masonc62a1922007-04-24 12:07:39 -04001069 u64 num_blocks, u64 search_start,
Chris Masonbe08c1b2007-05-03 09:06:49 -04001070 u64 search_end, struct btrfs_key *ins, int data)
Chris Masonfec577f2007-02-26 10:40:21 -05001071{
1072 int ret;
1073 int pending_ret;
Chris Mason1261ec42007-03-20 20:35:03 -04001074 u64 super_blocks_used;
1075 struct btrfs_fs_info *info = root->fs_info;
1076 struct btrfs_root *extent_root = info->extent_root;
Chris Mason234b63a2007-03-13 10:46:10 -04001077 struct btrfs_extent_item extent_item;
Chris Masonf2458e12007-04-25 15:52:25 -04001078 struct btrfs_key prealloc_key;
Chris Mason037e6392007-03-07 11:50:24 -05001079
Chris Masoncf27e1e2007-03-13 09:49:06 -04001080 btrfs_set_extent_refs(&extent_item, 1);
Chris Mason4d775672007-04-20 20:23:12 -04001081 btrfs_set_extent_owner(&extent_item, owner);
Chris Masonfec577f2007-02-26 10:40:21 -05001082
Chris Mason037e6392007-03-07 11:50:24 -05001083 if (root == extent_root) {
Chris Masonf2458e12007-04-25 15:52:25 -04001084 int nr;
1085 BUG_ON(info->extent_tree_prealloc_nr == 0);
Chris Mason037e6392007-03-07 11:50:24 -05001086 BUG_ON(num_blocks != 1);
Chris Mason037e6392007-03-07 11:50:24 -05001087 ins->offset = 1;
Chris Masonf2458e12007-04-25 15:52:25 -04001088 info->extent_tree_prealloc_nr--;
1089 nr = info->extent_tree_prealloc_nr;
1090 ins->objectid = info->extent_tree_prealloc[nr];
1091 info->extent_tree_insert[info->extent_tree_insert_nr++] =
1092 ins->objectid;
Chris Mason9078a3e2007-04-26 16:46:15 -04001093 ret = update_block_group(trans, root,
Chris Masone37c9e62007-05-09 20:13:14 -04001094 ins->objectid, ins->offset, 1, 0);
Chris Mason9078a3e2007-04-26 16:46:15 -04001095 BUG_ON(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05001096 return 0;
1097 }
Chris Masone37c9e62007-05-09 20:13:14 -04001098
1099 /*
1100 * if we're doing a data allocation, preallocate room in the
1101 * extent tree first. This way the extent tree blocks end up
1102 * in the correct block group.
1103 */
1104 if (data) {
1105 ret = find_free_extent(trans, root, 0, search_start,
1106 search_end, &prealloc_key, 0);
1107 if (ret) {
1108 return ret;
1109 }
1110 if (prealloc_key.objectid + prealloc_key.offset >= search_end) {
1111 int nr = info->extent_tree_prealloc_nr;
1112 search_end = info->extent_tree_prealloc[nr - 1] - 1;
1113 } else {
1114 search_start = info->extent_tree_prealloc[0] + 1;
1115 }
1116 }
Chris Masonf2458e12007-04-25 15:52:25 -04001117 /* do the real allocation */
Chris Masone089f052007-03-16 16:20:31 -04001118 ret = find_free_extent(trans, root, num_blocks, search_start,
Chris Masonbe08c1b2007-05-03 09:06:49 -04001119 search_end, ins, data);
Chris Masone37c9e62007-05-09 20:13:14 -04001120 if (ret) {
Chris Mason037e6392007-03-07 11:50:24 -05001121 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -04001122 }
Chris Masonfec577f2007-02-26 10:40:21 -05001123
Chris Masone37c9e62007-05-09 20:13:14 -04001124 /*
1125 * if we're doing a metadata allocation, preallocate space in the
1126 * extent tree second. This way, we don't create a tiny hole
1127 * in the allocation map between any unused preallocation blocks
1128 * and the metadata block we're actually allocating. On disk,
1129 * it'll go:
1130 * [block we've allocated], [used prealloc 1], [ unused prealloc ]
1131 * The unused prealloc will get reused the next time around.
1132 */
1133 if (!data) {
1134 if (ins->objectid + ins->offset >= search_end)
1135 search_end = ins->objectid - 1;
1136 else
1137 search_start = ins->objectid + ins->offset;
Chris Mason3e1ad542007-05-07 20:03:49 -04001138
Chris Masone37c9e62007-05-09 20:13:14 -04001139 ret = find_free_extent(trans, root, 0, search_start,
1140 search_end, &prealloc_key, 0);
1141 if (ret) {
1142 return ret;
1143 }
1144 }
Chris Masonf2458e12007-04-25 15:52:25 -04001145
Chris Mason1261ec42007-03-20 20:35:03 -04001146 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
1147 btrfs_set_super_blocks_used(info->disk_super, super_blocks_used +
1148 num_blocks);
Chris Masone089f052007-03-16 16:20:31 -04001149 ret = btrfs_insert_item(trans, extent_root, ins, &extent_item,
1150 sizeof(extent_item));
Chris Mason037e6392007-03-07 11:50:24 -05001151
Chris Masone089f052007-03-16 16:20:31 -04001152 finish_current_insert(trans, extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -04001153 pending_ret = del_pending_extents(trans, extent_root);
Chris Masone37c9e62007-05-09 20:13:14 -04001154 if (ret) {
Chris Mason037e6392007-03-07 11:50:24 -05001155 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -04001156 }
1157 if (pending_ret) {
Chris Mason037e6392007-03-07 11:50:24 -05001158 return pending_ret;
Chris Masone37c9e62007-05-09 20:13:14 -04001159 }
1160 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0);
Chris Mason037e6392007-03-07 11:50:24 -05001161 return 0;
Chris Masonfec577f2007-02-26 10:40:21 -05001162}
1163
1164/*
1165 * helper function to allocate a block for a given tree
1166 * returns the tree buffer or NULL.
1167 */
Chris Masone20d96d2007-03-22 12:13:20 -04001168struct buffer_head *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Chris Mason31f3c992007-04-30 15:25:45 -04001169 struct btrfs_root *root, u64 hint)
Chris Masonfec577f2007-02-26 10:40:21 -05001170{
Chris Masone2fa7222007-03-12 16:22:34 -04001171 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -05001172 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -04001173 struct buffer_head *buf;
Chris Masonfec577f2007-02-26 10:40:21 -05001174
Chris Mason4d775672007-04-20 20:23:12 -04001175 ret = btrfs_alloc_extent(trans, root, root->root_key.objectid,
Chris Masone37c9e62007-05-09 20:13:14 -04001176 1, 0, (unsigned long)-1, &ins, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05001177 if (ret) {
1178 BUG();
1179 return NULL;
1180 }
Chris Mason9078a3e2007-04-26 16:46:15 -04001181 BUG_ON(ret);
Chris Masond98237b2007-03-28 13:57:48 -04001182 buf = btrfs_find_create_tree_block(root, ins.objectid);
Chris Masondf2ce342007-03-23 11:00:45 -04001183 set_buffer_uptodate(buf);
Chris Mason090d1872007-05-01 08:53:32 -04001184 set_buffer_checked(buf);
Chris Mason7c4452b2007-04-28 09:29:35 -04001185 set_radix_bit(&trans->transaction->dirty_pages, buf->b_page->index);
Chris Masonfec577f2007-02-26 10:40:21 -05001186 return buf;
1187}
Chris Masona28ec192007-03-06 20:08:01 -05001188
Chris Mason6407bf62007-03-27 06:33:00 -04001189static int drop_leaf_ref(struct btrfs_trans_handle *trans,
1190 struct btrfs_root *root, struct buffer_head *cur)
1191{
1192 struct btrfs_disk_key *key;
1193 struct btrfs_leaf *leaf;
1194 struct btrfs_file_extent_item *fi;
1195 int i;
1196 int nritems;
1197 int ret;
1198
1199 BUG_ON(!btrfs_is_leaf(btrfs_buffer_node(cur)));
1200 leaf = btrfs_buffer_leaf(cur);
1201 nritems = btrfs_header_nritems(&leaf->header);
1202 for (i = 0; i < nritems; i++) {
1203 key = &leaf->items[i].key;
1204 if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY)
1205 continue;
1206 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
Chris Mason236454d2007-04-19 13:37:44 -04001207 if (btrfs_file_extent_type(fi) == BTRFS_FILE_EXTENT_INLINE)
1208 continue;
Chris Mason6407bf62007-03-27 06:33:00 -04001209 /*
1210 * FIXME make sure to insert a trans record that
1211 * repeats the snapshot del on crash
1212 */
1213 ret = btrfs_free_extent(trans, root,
1214 btrfs_file_extent_disk_blocknr(fi),
1215 btrfs_file_extent_disk_num_blocks(fi),
1216 0);
1217 BUG_ON(ret);
1218 }
1219 return 0;
1220}
1221
Chris Mason9aca1d52007-03-13 11:09:37 -04001222/*
1223 * helper function for drop_snapshot, this walks down the tree dropping ref
1224 * counts as it goes.
1225 */
Chris Masone089f052007-03-16 16:20:31 -04001226static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
1227 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05001228{
Chris Masone20d96d2007-03-22 12:13:20 -04001229 struct buffer_head *next;
1230 struct buffer_head *cur;
Chris Mason20524f02007-03-10 06:35:47 -05001231 u64 blocknr;
1232 int ret;
1233 u32 refs;
1234
Chris Mason5caf2a02007-04-02 11:20:42 -04001235 WARN_ON(*level < 0);
1236 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Masonb18c6682007-04-17 13:26:50 -04001237 ret = lookup_extent_ref(trans, root, bh_blocknr(path->nodes[*level]),
Chris Mason6407bf62007-03-27 06:33:00 -04001238 1, &refs);
Chris Mason20524f02007-03-10 06:35:47 -05001239 BUG_ON(ret);
1240 if (refs > 1)
1241 goto out;
Chris Mason9aca1d52007-03-13 11:09:37 -04001242 /*
1243 * walk down to the last node level and free all the leaves
1244 */
Chris Mason6407bf62007-03-27 06:33:00 -04001245 while(*level >= 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001246 WARN_ON(*level < 0);
1247 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason20524f02007-03-10 06:35:47 -05001248 cur = path->nodes[*level];
Chris Mason2c90e5d2007-04-02 10:50:19 -04001249 if (btrfs_header_level(btrfs_buffer_header(cur)) != *level)
1250 WARN_ON(1);
Chris Mason7518a232007-03-12 12:01:18 -04001251 if (path->slots[*level] >=
Chris Masone20d96d2007-03-22 12:13:20 -04001252 btrfs_header_nritems(btrfs_buffer_header(cur)))
Chris Mason20524f02007-03-10 06:35:47 -05001253 break;
Chris Mason6407bf62007-03-27 06:33:00 -04001254 if (*level == 0) {
1255 ret = drop_leaf_ref(trans, root, cur);
1256 BUG_ON(ret);
1257 break;
1258 }
Chris Masone20d96d2007-03-22 12:13:20 -04001259 blocknr = btrfs_node_blockptr(btrfs_buffer_node(cur),
1260 path->slots[*level]);
Chris Masonb18c6682007-04-17 13:26:50 -04001261 ret = lookup_extent_ref(trans, root, blocknr, 1, &refs);
Chris Mason6407bf62007-03-27 06:33:00 -04001262 BUG_ON(ret);
1263 if (refs != 1) {
Chris Mason20524f02007-03-10 06:35:47 -05001264 path->slots[*level]++;
Chris Masone089f052007-03-16 16:20:31 -04001265 ret = btrfs_free_extent(trans, root, blocknr, 1, 1);
Chris Mason20524f02007-03-10 06:35:47 -05001266 BUG_ON(ret);
1267 continue;
1268 }
Chris Mason20524f02007-03-10 06:35:47 -05001269 next = read_tree_block(root, blocknr);
Chris Mason5caf2a02007-04-02 11:20:42 -04001270 WARN_ON(*level <= 0);
Chris Mason83e15a22007-03-12 09:03:27 -04001271 if (path->nodes[*level-1])
Chris Mason234b63a2007-03-13 10:46:10 -04001272 btrfs_block_release(root, path->nodes[*level-1]);
Chris Mason20524f02007-03-10 06:35:47 -05001273 path->nodes[*level-1] = next;
Chris Masone20d96d2007-03-22 12:13:20 -04001274 *level = btrfs_header_level(btrfs_buffer_header(next));
Chris Mason20524f02007-03-10 06:35:47 -05001275 path->slots[*level] = 0;
1276 }
1277out:
Chris Mason5caf2a02007-04-02 11:20:42 -04001278 WARN_ON(*level < 0);
1279 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason6407bf62007-03-27 06:33:00 -04001280 ret = btrfs_free_extent(trans, root,
Chris Mason7eccb902007-04-11 15:53:25 -04001281 bh_blocknr(path->nodes[*level]), 1, 1);
Chris Mason234b63a2007-03-13 10:46:10 -04001282 btrfs_block_release(root, path->nodes[*level]);
Chris Mason20524f02007-03-10 06:35:47 -05001283 path->nodes[*level] = NULL;
1284 *level += 1;
1285 BUG_ON(ret);
1286 return 0;
1287}
1288
Chris Mason9aca1d52007-03-13 11:09:37 -04001289/*
1290 * helper for dropping snapshots. This walks back up the tree in the path
1291 * to find the first node higher up where we haven't yet gone through
1292 * all the slots
1293 */
Chris Masone089f052007-03-16 16:20:31 -04001294static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root
1295 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05001296{
1297 int i;
1298 int slot;
1299 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -04001300 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Mason20524f02007-03-10 06:35:47 -05001301 slot = path->slots[i];
Chris Masone20d96d2007-03-22 12:13:20 -04001302 if (slot < btrfs_header_nritems(
1303 btrfs_buffer_header(path->nodes[i])) - 1) {
Chris Mason20524f02007-03-10 06:35:47 -05001304 path->slots[i]++;
1305 *level = i;
1306 return 0;
1307 } else {
Chris Masone089f052007-03-16 16:20:31 -04001308 ret = btrfs_free_extent(trans, root,
Chris Mason7eccb902007-04-11 15:53:25 -04001309 bh_blocknr(path->nodes[*level]),
Chris Masone089f052007-03-16 16:20:31 -04001310 1, 1);
Chris Mason6407bf62007-03-27 06:33:00 -04001311 BUG_ON(ret);
Chris Mason234b63a2007-03-13 10:46:10 -04001312 btrfs_block_release(root, path->nodes[*level]);
Chris Mason83e15a22007-03-12 09:03:27 -04001313 path->nodes[*level] = NULL;
Chris Mason20524f02007-03-10 06:35:47 -05001314 *level = i + 1;
Chris Mason20524f02007-03-10 06:35:47 -05001315 }
1316 }
1317 return 1;
1318}
1319
Chris Mason9aca1d52007-03-13 11:09:37 -04001320/*
1321 * drop the reference count on the tree rooted at 'snap'. This traverses
1322 * the tree freeing any blocks that have a ref count of zero after being
1323 * decremented.
1324 */
Chris Masone089f052007-03-16 16:20:31 -04001325int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masone20d96d2007-03-22 12:13:20 -04001326 *root, struct buffer_head *snap)
Chris Mason20524f02007-03-10 06:35:47 -05001327{
Chris Mason3768f362007-03-13 16:47:54 -04001328 int ret = 0;
Chris Mason9aca1d52007-03-13 11:09:37 -04001329 int wret;
Chris Mason20524f02007-03-10 06:35:47 -05001330 int level;
Chris Mason5caf2a02007-04-02 11:20:42 -04001331 struct btrfs_path *path;
Chris Mason20524f02007-03-10 06:35:47 -05001332 int i;
1333 int orig_level;
1334
Chris Mason5caf2a02007-04-02 11:20:42 -04001335 path = btrfs_alloc_path();
1336 BUG_ON(!path);
1337 btrfs_init_path(path);
Chris Mason20524f02007-03-10 06:35:47 -05001338
Chris Masone20d96d2007-03-22 12:13:20 -04001339 level = btrfs_header_level(btrfs_buffer_header(snap));
Chris Mason20524f02007-03-10 06:35:47 -05001340 orig_level = level;
Chris Mason5caf2a02007-04-02 11:20:42 -04001341 path->nodes[level] = snap;
1342 path->slots[level] = 0;
Chris Mason20524f02007-03-10 06:35:47 -05001343 while(1) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001344 wret = walk_down_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04001345 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05001346 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04001347 if (wret < 0)
1348 ret = wret;
1349
Chris Mason5caf2a02007-04-02 11:20:42 -04001350 wret = walk_up_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04001351 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05001352 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04001353 if (wret < 0)
1354 ret = wret;
Chris Mason35b7e472007-05-02 15:53:43 -04001355 btrfs_btree_balance_dirty(root);
Chris Mason20524f02007-03-10 06:35:47 -05001356 }
Chris Mason83e15a22007-03-12 09:03:27 -04001357 for (i = 0; i <= orig_level; i++) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001358 if (path->nodes[i]) {
1359 btrfs_block_release(root, path->nodes[i]);
Chris Mason83e15a22007-03-12 09:03:27 -04001360 }
Chris Mason20524f02007-03-10 06:35:47 -05001361 }
Chris Mason5caf2a02007-04-02 11:20:42 -04001362 btrfs_free_path(path);
Chris Mason9aca1d52007-03-13 11:09:37 -04001363 return ret;
Chris Mason20524f02007-03-10 06:35:47 -05001364}
Chris Mason9078a3e2007-04-26 16:46:15 -04001365
Chris Masonbe744172007-05-06 10:15:01 -04001366static int free_block_group_radix(struct radix_tree_root *radix)
Chris Mason9078a3e2007-04-26 16:46:15 -04001367{
1368 int ret;
1369 struct btrfs_block_group_cache *cache[8];
1370 int i;
1371
1372 while(1) {
Chris Masonbe744172007-05-06 10:15:01 -04001373 ret = radix_tree_gang_lookup(radix, (void **)cache, 0,
Chris Mason9078a3e2007-04-26 16:46:15 -04001374 ARRAY_SIZE(cache));
1375 if (!ret)
1376 break;
1377 for (i = 0; i < ret; i++) {
Chris Masonbe744172007-05-06 10:15:01 -04001378 radix_tree_delete(radix, cache[i]->key.objectid +
Chris Mason9078a3e2007-04-26 16:46:15 -04001379 cache[i]->key.offset - 1);
1380 kfree(cache[i]);
1381 }
1382 }
1383 return 0;
1384}
1385
Chris Masonbe744172007-05-06 10:15:01 -04001386int btrfs_free_block_groups(struct btrfs_fs_info *info)
1387{
1388 int ret;
1389 int ret2;
Chris Masone37c9e62007-05-09 20:13:14 -04001390 unsigned long gang[16];
1391 int i;
Chris Masonbe744172007-05-06 10:15:01 -04001392
1393 ret = free_block_group_radix(&info->block_group_radix);
1394 ret2 = free_block_group_radix(&info->block_group_data_radix);
1395 if (ret)
1396 return ret;
1397 if (ret2)
1398 return ret2;
Chris Masone37c9e62007-05-09 20:13:14 -04001399
1400 while(1) {
1401 ret = find_first_radix_bit(&info->extent_map_radix,
1402 gang, 0, ARRAY_SIZE(gang));
1403 if (!ret)
1404 break;
1405 for (i = 0; i < ret; i++) {
1406 clear_radix_bit(&info->extent_map_radix, gang[i]);
1407 }
1408 }
Chris Masonbe744172007-05-06 10:15:01 -04001409 return 0;
1410}
1411
Chris Mason9078a3e2007-04-26 16:46:15 -04001412int btrfs_read_block_groups(struct btrfs_root *root)
1413{
1414 struct btrfs_path *path;
1415 int ret;
1416 int err = 0;
1417 struct btrfs_block_group_item *bi;
1418 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04001419 struct btrfs_fs_info *info = root->fs_info;
1420 struct radix_tree_root *radix;
Chris Mason9078a3e2007-04-26 16:46:15 -04001421 struct btrfs_key key;
1422 struct btrfs_key found_key;
1423 struct btrfs_leaf *leaf;
1424 u64 group_size_blocks = BTRFS_BLOCK_GROUP_SIZE / root->blocksize;
Chris Mason31f3c992007-04-30 15:25:45 -04001425 u64 used;
Chris Masonbe744172007-05-06 10:15:01 -04001426 u64 nr = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04001427
Chris Masonbe744172007-05-06 10:15:01 -04001428 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04001429 key.objectid = 0;
1430 key.offset = group_size_blocks;
1431 key.flags = 0;
1432 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
1433
1434 path = btrfs_alloc_path();
1435 if (!path)
1436 return -ENOMEM;
1437
1438 while(1) {
Chris Masonbe744172007-05-06 10:15:01 -04001439 ret = btrfs_search_slot(NULL, info->extent_root,
Chris Mason9078a3e2007-04-26 16:46:15 -04001440 &key, path, 0, 0);
1441 if (ret != 0) {
1442 err = ret;
1443 break;
1444 }
1445 leaf = btrfs_buffer_leaf(path->nodes[0]);
1446 btrfs_disk_key_to_cpu(&found_key,
1447 &leaf->items[path->slots[0]].key);
1448 cache = kmalloc(sizeof(*cache), GFP_NOFS);
1449 if (!cache) {
1450 err = -1;
1451 break;
1452 }
Chris Mason3e1ad542007-05-07 20:03:49 -04001453
Chris Masone37c9e62007-05-09 20:13:14 -04001454 if (nr % 3)
Chris Mason3e1ad542007-05-07 20:03:49 -04001455 radix = &info->block_group_data_radix;
1456 else
1457 radix = &info->block_group_radix;
1458
Chris Mason9078a3e2007-04-26 16:46:15 -04001459 bi = btrfs_item_ptr(leaf, path->slots[0],
1460 struct btrfs_block_group_item);
1461 memcpy(&cache->item, bi, sizeof(*bi));
1462 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Mason31f3c992007-04-30 15:25:45 -04001463 cache->last_alloc = cache->key.objectid;
1464 cache->first_free = cache->key.objectid;
Chris Masone37c9e62007-05-09 20:13:14 -04001465 cache->last_prealloc = cache->key.objectid;
Chris Masonbe744172007-05-06 10:15:01 -04001466 cache->pinned = 0;
Chris Masone37c9e62007-05-09 20:13:14 -04001467 cache->cached = 0;
1468
1469 if (nr % 3)
1470 cache->data = 1;
1471 else
1472 cache->data = 0;
Chris Mason3e1ad542007-05-07 20:03:49 -04001473 cache->radix = radix;
1474
Chris Mason9078a3e2007-04-26 16:46:15 -04001475 key.objectid = found_key.objectid + found_key.offset;
1476 btrfs_release_path(root, path);
Chris Masonbe744172007-05-06 10:15:01 -04001477 ret = radix_tree_insert(radix, found_key.objectid +
Chris Mason9078a3e2007-04-26 16:46:15 -04001478 found_key.offset - 1,
1479 (void *)cache);
1480 BUG_ON(ret);
Chris Mason31f3c992007-04-30 15:25:45 -04001481 used = btrfs_block_group_used(bi);
Chris Masonbe744172007-05-06 10:15:01 -04001482 if (used < (key.offset * 8) / 10) {
1483 radix_tree_tag_set(radix, found_key.objectid +
Chris Mason31f3c992007-04-30 15:25:45 -04001484 found_key.offset - 1,
1485 BTRFS_BLOCK_GROUP_AVAIL);
1486 }
Chris Mason9078a3e2007-04-26 16:46:15 -04001487 if (key.objectid >=
Chris Masonbe744172007-05-06 10:15:01 -04001488 btrfs_super_total_blocks(info->disk_super))
Chris Mason9078a3e2007-04-26 16:46:15 -04001489 break;
Chris Masonbe744172007-05-06 10:15:01 -04001490 nr++;
Chris Mason9078a3e2007-04-26 16:46:15 -04001491 }
1492
1493 btrfs_free_path(path);
1494 return 0;
1495}