blob: e3c6bfea3751e32d69a7143c857d2ce9b3a771a3 [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 Masonde428b62007-05-18 13:28:27 -040015static void reada_extent_leaves(struct btrfs_root *root,
16 struct btrfs_path *path, u64 limit)
17{
18 struct btrfs_node *node;
19 int i;
20 int nritems;
21 u64 item_objectid;
22 u64 blocknr;
23 int slot;
24 int ret;
25
26 if (!path->nodes[1])
27 return;
28 node = btrfs_buffer_node(path->nodes[1]);
29 slot = path->slots[1] + 1;
30 nritems = btrfs_header_nritems(&node->header);
31 for (i = slot; i < nritems && i < slot + 8; i++) {
32 item_objectid = btrfs_disk_key_objectid(&node->ptrs[i].key);
33 if (item_objectid > limit)
34 break;
35 blocknr = btrfs_node_blockptr(node, i);
36 ret = readahead_tree_block(root, blocknr);
37 if (ret)
38 break;
39 }
40}
41
Chris Masone37c9e62007-05-09 20:13:14 -040042static int cache_block_group(struct btrfs_root *root,
43 struct btrfs_block_group_cache *block_group)
44{
45 struct btrfs_path *path;
46 int ret;
47 struct btrfs_key key;
48 struct btrfs_leaf *leaf;
49 struct radix_tree_root *extent_radix;
50 int slot;
51 u64 i;
52 u64 last = 0;
53 u64 hole_size;
Chris Masonde428b62007-05-18 13:28:27 -040054 u64 limit;
Chris Masone37c9e62007-05-09 20:13:14 -040055 int found = 0;
56
57 root = root->fs_info->extent_root;
58 extent_radix = &root->fs_info->extent_map_radix;
59
60 if (block_group->cached)
61 return 0;
62 if (block_group->data)
63 return 0;
64 path = btrfs_alloc_path();
65 if (!path)
66 return -ENOMEM;
67printk("cache block group %Lu\n", block_group->key.objectid);
68 key.objectid = block_group->key.objectid;
69 key.flags = 0;
70 key.offset = 0;
71 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
72 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
73 if (ret < 0)
74 return ret;
75 if (ret && path->slots[0] > 0)
76 path->slots[0]--;
Chris Masonde428b62007-05-18 13:28:27 -040077 limit = block_group->key.objectid + block_group->key.offset;
78 reada_extent_leaves(root, path, limit);
Chris Masone37c9e62007-05-09 20:13:14 -040079 while(1) {
80 leaf = btrfs_buffer_leaf(path->nodes[0]);
81 slot = path->slots[0];
82 if (slot >= btrfs_header_nritems(&leaf->header)) {
Chris Masonde428b62007-05-18 13:28:27 -040083 reada_extent_leaves(root, path, limit);
Chris Masone37c9e62007-05-09 20:13:14 -040084 ret = btrfs_next_leaf(root, path);
Chris Masonde428b62007-05-18 13:28:27 -040085 if (ret == 0) {
Chris Masone37c9e62007-05-09 20:13:14 -040086 continue;
Chris Masonde428b62007-05-18 13:28:27 -040087 } else {
Chris Masone37c9e62007-05-09 20:13:14 -040088 if (found) {
89 hole_size = block_group->key.objectid +
90 block_group->key.offset - last;
91 } else {
92 last = block_group->key.objectid;
93 hole_size = block_group->key.offset;
94 }
95 for (i = 0; i < hole_size; i++) {
96 set_radix_bit(extent_radix,
97 last + i);
98 }
99 break;
100 }
101 }
102 btrfs_disk_key_to_cpu(&key, &leaf->items[slot].key);
103 if (key.objectid >= block_group->key.objectid +
104 block_group->key.offset) {
105 if (found) {
106 hole_size = block_group->key.objectid +
107 block_group->key.offset - last;
108 } else {
109 last = block_group->key.objectid;
110 hole_size = block_group->key.offset;
111 }
112 for (i = 0; i < hole_size; i++) {
113 set_radix_bit(extent_radix, last + i);
114 }
115 break;
116 }
117 if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) {
118 if (!found) {
119 last = key.objectid + key.offset;
120 found = 1;
121 } else {
122 hole_size = key.objectid - last;
123 for (i = 0; i < hole_size; i++) {
124 set_radix_bit(extent_radix, last + i);
125 }
126 last = key.objectid + key.offset;
127 }
128 }
129 path->slots[0]++;
130 }
131
132 block_group->cached = 1;
133 btrfs_free_path(path);
134 return 0;
135}
136
Chris Masonbe744172007-05-06 10:15:01 -0400137static struct btrfs_block_group_cache *lookup_block_group(struct
138 btrfs_fs_info *info,
139 u64 blocknr)
140{
141 struct btrfs_block_group_cache *block_group;
142 int ret;
143
144 ret = radix_tree_gang_lookup(&info->block_group_radix,
145 (void **)&block_group,
146 blocknr, 1);
147 if (ret) {
Chris Mason3e1ad542007-05-07 20:03:49 -0400148 if (block_group->key.objectid <= blocknr && blocknr <=
Chris Masonbe744172007-05-06 10:15:01 -0400149 block_group->key.objectid + block_group->key.offset)
150 return block_group;
151 }
152 ret = radix_tree_gang_lookup(&info->block_group_data_radix,
153 (void **)&block_group,
154 blocknr, 1);
155 if (ret) {
Chris Mason3e1ad542007-05-07 20:03:49 -0400156 if (block_group->key.objectid <= blocknr && blocknr <=
Chris Masonbe744172007-05-06 10:15:01 -0400157 block_group->key.objectid + block_group->key.offset)
158 return block_group;
159 }
Chris Mason3e1ad542007-05-07 20:03:49 -0400160 WARN_ON(1);
161 printk("lookup_block_group fails for blocknr %Lu\n", blocknr);
162 printk("last ret was %d\n", ret);
163 if (ret) {
164 printk("last block group was %Lu %Lu\n", block_group->key.objectid, block_group->key.offset);
165 }
Chris Masonbe744172007-05-06 10:15:01 -0400166 return NULL;
167}
168
Chris Masone37c9e62007-05-09 20:13:14 -0400169static u64 leaf_range(struct btrfs_root *root)
170{
171 u64 size = BTRFS_LEAF_DATA_SIZE(root);
172 size = size / (sizeof(struct btrfs_extent_item) +
173 sizeof(struct btrfs_item));
174 return size;
175}
176
177static u64 find_search_start(struct btrfs_root *root,
178 struct btrfs_block_group_cache **cache_ret,
179 u64 search_start, int num)
180{
181 unsigned long gang[8];
182 int ret;
183 struct btrfs_block_group_cache *cache = *cache_ret;
184 u64 last = max(search_start, cache->key.objectid);
185
186 if (cache->data)
187 goto out;
188 if (num > 1) {
189 last = max(last, cache->last_prealloc);
190 }
191again:
192 cache_block_group(root, cache);
193 while(1) {
194 ret = find_first_radix_bit(&root->fs_info->extent_map_radix,
195 gang, last, ARRAY_SIZE(gang));
196 if (!ret)
197 goto out;
198 last = gang[ret-1] + 1;
199 if (num > 1) {
200 if (ret != ARRAY_SIZE(gang)) {
201 goto new_group;
202 }
203 if (gang[ret-1] - gang[0] > leaf_range(root)) {
204 continue;
205 }
206 }
207 if (gang[0] >= cache->key.objectid + cache->key.offset) {
208 goto new_group;
209 }
210 return gang[0];
211 }
212out:
213 return max(cache->last_alloc, search_start);
214
215new_group:
216 cache = lookup_block_group(root->fs_info, last + cache->key.offset - 1);
217 if (!cache) {
218 return max((*cache_ret)->last_alloc, search_start);
219 }
220 cache = btrfs_find_block_group(root, cache,
Chris Masonde428b62007-05-18 13:28:27 -0400221 last + cache->key.offset - 1, 0, 0);
Chris Masone37c9e62007-05-09 20:13:14 -0400222 *cache_ret = cache;
223 goto again;
224}
225
Chris Mason31f3c992007-04-30 15:25:45 -0400226struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
227 struct btrfs_block_group_cache
Chris Masonbe744172007-05-06 10:15:01 -0400228 *hint, u64 search_start,
Chris Masonde428b62007-05-18 13:28:27 -0400229 int data, int owner)
Chris Masoncd1bc462007-04-27 10:08:34 -0400230{
231 struct btrfs_block_group_cache *cache[8];
Chris Mason31f3c992007-04-30 15:25:45 -0400232 struct btrfs_block_group_cache *found_group = NULL;
Chris Masoncd1bc462007-04-27 10:08:34 -0400233 struct btrfs_fs_info *info = root->fs_info;
Chris Masonbe744172007-05-06 10:15:01 -0400234 struct radix_tree_root *radix;
Chris Masoncd1bc462007-04-27 10:08:34 -0400235 u64 used;
Chris Mason31f3c992007-04-30 15:25:45 -0400236 u64 last = 0;
237 u64 hint_last;
Chris Masoncd1bc462007-04-27 10:08:34 -0400238 int i;
239 int ret;
Chris Mason31f3c992007-04-30 15:25:45 -0400240 int full_search = 0;
Chris Masonde428b62007-05-18 13:28:27 -0400241 int factor = 8;
242
243 if (!owner)
244 factor = 5;
Chris Masonbe744172007-05-06 10:15:01 -0400245
246 if (data)
247 radix = &info->block_group_data_radix;
248 else
249 radix = &info->block_group_radix;
250
251 if (search_start) {
252 struct btrfs_block_group_cache *shint;
253 shint = lookup_block_group(info, search_start);
254 if (shint->data == data) {
255 used = btrfs_block_group_used(&shint->item);
256 if (used + shint->pinned <
Chris Masonde428b62007-05-18 13:28:27 -0400257 (shint->key.offset * factor) / 10) {
Chris Masonbe744172007-05-06 10:15:01 -0400258 return shint;
259 }
260 }
261 }
262 if (hint && hint->data == data) {
Chris Mason31f3c992007-04-30 15:25:45 -0400263 used = btrfs_block_group_used(&hint->item);
Chris Masonde428b62007-05-18 13:28:27 -0400264 if (used + hint->pinned < (hint->key.offset * factor) / 10) {
Chris Mason31f3c992007-04-30 15:25:45 -0400265 return hint;
266 }
Chris Masonbe744172007-05-06 10:15:01 -0400267 if (used >= (hint->key.offset * 8) / 10) {
268 radix_tree_tag_clear(radix,
269 hint->key.objectid +
270 hint->key.offset - 1,
271 BTRFS_BLOCK_GROUP_AVAIL);
272 }
Chris Mason8d7be552007-05-10 11:24:42 -0400273 last = hint->key.offset * 3;
Chris Masonbe744172007-05-06 10:15:01 -0400274 if (hint->key.objectid >= last)
Chris Masone37c9e62007-05-09 20:13:14 -0400275 last = max(search_start + hint->key.offset - 1,
276 hint->key.objectid - last);
Chris Masonbe744172007-05-06 10:15:01 -0400277 else
278 last = hint->key.objectid + hint->key.offset;
Chris Mason31f3c992007-04-30 15:25:45 -0400279 hint_last = last;
280 } else {
Chris Masone37c9e62007-05-09 20:13:14 -0400281 if (hint)
282 hint_last = max(hint->key.objectid, search_start);
283 else
284 hint_last = search_start;
285
286 last = hint_last;
Chris Mason31f3c992007-04-30 15:25:45 -0400287 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400288 while(1) {
Chris Masonbe744172007-05-06 10:15:01 -0400289 ret = radix_tree_gang_lookup_tag(radix, (void **)cache,
Chris Masoncd1bc462007-04-27 10:08:34 -0400290 last, ARRAY_SIZE(cache),
Chris Mason31f3c992007-04-30 15:25:45 -0400291 BTRFS_BLOCK_GROUP_AVAIL);
Chris Masoncd1bc462007-04-27 10:08:34 -0400292 if (!ret)
293 break;
294 for (i = 0; i < ret; i++) {
Chris Masonbe08c1b2007-05-03 09:06:49 -0400295 last = cache[i]->key.objectid +
296 cache[i]->key.offset;
Chris Masoncd1bc462007-04-27 10:08:34 -0400297 used = btrfs_block_group_used(&cache[i]->item);
Chris Masonbe744172007-05-06 10:15:01 -0400298 if (used + cache[i]->pinned <
Chris Masonde428b62007-05-18 13:28:27 -0400299 (cache[i]->key.offset * factor) / 10) {
Chris Mason31f3c992007-04-30 15:25:45 -0400300 found_group = cache[i];
301 goto found;
Chris Masoncd1bc462007-04-27 10:08:34 -0400302 }
Chris Masonbe744172007-05-06 10:15:01 -0400303 if (used >= (cache[i]->key.offset * 8) / 10) {
304 radix_tree_tag_clear(radix,
305 cache[i]->key.objectid +
306 cache[i]->key.offset - 1,
307 BTRFS_BLOCK_GROUP_AVAIL);
308 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400309 }
Chris Masonde428b62007-05-18 13:28:27 -0400310 cond_resched();
Chris Masoncd1bc462007-04-27 10:08:34 -0400311 }
Chris Mason31f3c992007-04-30 15:25:45 -0400312 last = hint_last;
313again:
Chris Masoncd1bc462007-04-27 10:08:34 -0400314 while(1) {
Chris Masonbe744172007-05-06 10:15:01 -0400315 ret = radix_tree_gang_lookup(radix, (void **)cache,
316 last, ARRAY_SIZE(cache));
Chris Masoncd1bc462007-04-27 10:08:34 -0400317 if (!ret)
318 break;
319 for (i = 0; i < ret; i++) {
Chris Masonbe08c1b2007-05-03 09:06:49 -0400320 last = cache[i]->key.objectid +
321 cache[i]->key.offset;
Chris Masoncd1bc462007-04-27 10:08:34 -0400322 used = btrfs_block_group_used(&cache[i]->item);
Chris Masonbe744172007-05-06 10:15:01 -0400323 if (used + cache[i]->pinned < cache[i]->key.offset) {
Chris Mason31f3c992007-04-30 15:25:45 -0400324 found_group = cache[i];
325 goto found;
Chris Masoncd1bc462007-04-27 10:08:34 -0400326 }
Chris Masonbe744172007-05-06 10:15:01 -0400327 if (used >= cache[i]->key.offset) {
328 radix_tree_tag_clear(radix,
329 cache[i]->key.objectid +
330 cache[i]->key.offset - 1,
331 BTRFS_BLOCK_GROUP_AVAIL);
332 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400333 }
Chris Masonde428b62007-05-18 13:28:27 -0400334 cond_resched();
Chris Masoncd1bc462007-04-27 10:08:34 -0400335 }
Chris Mason31f3c992007-04-30 15:25:45 -0400336 if (!full_search) {
Chris Masonde428b62007-05-18 13:28:27 -0400337printk("find block group doing full search data %d start %Lu\n", data, search_start);
Chris Masonbe744172007-05-06 10:15:01 -0400338 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -0400339 full_search = 1;
340 goto again;
341 }
Chris Mason31f3c992007-04-30 15:25:45 -0400342 if (!found_group) {
Chris Masonde428b62007-05-18 13:28:27 -0400343printk("find block group bailing to zero data %d\n", data);
Chris Masonbe744172007-05-06 10:15:01 -0400344 ret = radix_tree_gang_lookup(radix,
Chris Mason31f3c992007-04-30 15:25:45 -0400345 (void **)&found_group, 0, 1);
346 BUG_ON(ret != 1);
347 }
Chris Masonbe744172007-05-06 10:15:01 -0400348found:
Chris Mason31f3c992007-04-30 15:25:45 -0400349 return found_group;
Chris Masoncd1bc462007-04-27 10:08:34 -0400350}
351
Chris Masonb18c6682007-04-17 13:26:50 -0400352int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
353 struct btrfs_root *root,
354 u64 blocknr, u64 num_blocks)
Chris Mason02217ed2007-03-02 16:08:05 -0500355{
Chris Mason5caf2a02007-04-02 11:20:42 -0400356 struct btrfs_path *path;
Chris Mason02217ed2007-03-02 16:08:05 -0500357 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400358 struct btrfs_key key;
Chris Mason234b63a2007-03-13 10:46:10 -0400359 struct btrfs_leaf *l;
360 struct btrfs_extent_item *item;
Chris Masone2fa7222007-03-12 16:22:34 -0400361 struct btrfs_key ins;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400362 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500363
Chris Mason9f5fae22007-03-20 14:38:32 -0400364 find_free_extent(trans, root->fs_info->extent_root, 0, 0, (u64)-1,
Chris Masonbe08c1b2007-05-03 09:06:49 -0400365 &ins, 0);
Chris Mason5caf2a02007-04-02 11:20:42 -0400366 path = btrfs_alloc_path();
367 BUG_ON(!path);
368 btrfs_init_path(path);
Chris Mason02217ed2007-03-02 16:08:05 -0500369 key.objectid = blocknr;
370 key.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -0400371 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Mason6407bf62007-03-27 06:33:00 -0400372 key.offset = num_blocks;
Chris Mason5caf2a02007-04-02 11:20:42 -0400373 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -0400374 0, 1);
Chris Masona429e512007-04-18 16:15:28 -0400375 if (ret != 0) {
376printk("can't find block %Lu %Lu\n", blocknr, num_blocks);
Chris Masona28ec192007-03-06 20:08:01 -0500377 BUG();
Chris Masona429e512007-04-18 16:15:28 -0400378 }
Chris Mason02217ed2007-03-02 16:08:05 -0500379 BUG_ON(ret != 0);
Chris Mason5caf2a02007-04-02 11:20:42 -0400380 l = btrfs_buffer_leaf(path->nodes[0]);
381 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400382 refs = btrfs_extent_refs(item);
383 btrfs_set_extent_refs(item, refs + 1);
Chris Mason5caf2a02007-04-02 11:20:42 -0400384 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masona28ec192007-03-06 20:08:01 -0500385
Chris Mason5caf2a02007-04-02 11:20:42 -0400386 btrfs_release_path(root->fs_info->extent_root, path);
387 btrfs_free_path(path);
Chris Mason9f5fae22007-03-20 14:38:32 -0400388 finish_current_insert(trans, root->fs_info->extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -0400389 del_pending_extents(trans, root->fs_info->extent_root);
Chris Mason02217ed2007-03-02 16:08:05 -0500390 return 0;
391}
392
Chris Masonb18c6682007-04-17 13:26:50 -0400393static int lookup_extent_ref(struct btrfs_trans_handle *trans,
394 struct btrfs_root *root, u64 blocknr,
395 u64 num_blocks, u32 *refs)
Chris Masona28ec192007-03-06 20:08:01 -0500396{
Chris Mason5caf2a02007-04-02 11:20:42 -0400397 struct btrfs_path *path;
Chris Masona28ec192007-03-06 20:08:01 -0500398 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400399 struct btrfs_key key;
Chris Mason234b63a2007-03-13 10:46:10 -0400400 struct btrfs_leaf *l;
401 struct btrfs_extent_item *item;
Chris Mason5caf2a02007-04-02 11:20:42 -0400402
403 path = btrfs_alloc_path();
404 btrfs_init_path(path);
Chris Masona28ec192007-03-06 20:08:01 -0500405 key.objectid = blocknr;
Chris Mason6407bf62007-03-27 06:33:00 -0400406 key.offset = num_blocks;
Chris Mason62e27492007-03-15 12:56:47 -0400407 key.flags = 0;
408 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Mason5caf2a02007-04-02 11:20:42 -0400409 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -0400410 0, 0);
Chris Masona28ec192007-03-06 20:08:01 -0500411 if (ret != 0)
412 BUG();
Chris Mason5caf2a02007-04-02 11:20:42 -0400413 l = btrfs_buffer_leaf(path->nodes[0]);
414 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400415 *refs = btrfs_extent_refs(item);
Chris Mason5caf2a02007-04-02 11:20:42 -0400416 btrfs_release_path(root->fs_info->extent_root, path);
417 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -0500418 return 0;
419}
420
Chris Masonc5739bb2007-04-10 09:27:04 -0400421int btrfs_inc_root_ref(struct btrfs_trans_handle *trans,
422 struct btrfs_root *root)
423{
Chris Masonb18c6682007-04-17 13:26:50 -0400424 return btrfs_inc_extent_ref(trans, root, bh_blocknr(root->node), 1);
Chris Masonc5739bb2007-04-10 09:27:04 -0400425}
426
Chris Masone089f052007-03-16 16:20:31 -0400427int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Masone20d96d2007-03-22 12:13:20 -0400428 struct buffer_head *buf)
Chris Mason02217ed2007-03-02 16:08:05 -0500429{
430 u64 blocknr;
Chris Masone20d96d2007-03-22 12:13:20 -0400431 struct btrfs_node *buf_node;
Chris Mason6407bf62007-03-27 06:33:00 -0400432 struct btrfs_leaf *buf_leaf;
433 struct btrfs_disk_key *key;
434 struct btrfs_file_extent_item *fi;
Chris Mason02217ed2007-03-02 16:08:05 -0500435 int i;
Chris Mason6407bf62007-03-27 06:33:00 -0400436 int leaf;
437 int ret;
Chris Masona28ec192007-03-06 20:08:01 -0500438
Chris Mason3768f362007-03-13 16:47:54 -0400439 if (!root->ref_cows)
Chris Masona28ec192007-03-06 20:08:01 -0500440 return 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400441 buf_node = btrfs_buffer_node(buf);
Chris Mason6407bf62007-03-27 06:33:00 -0400442 leaf = btrfs_is_leaf(buf_node);
443 buf_leaf = btrfs_buffer_leaf(buf);
Chris Masone20d96d2007-03-22 12:13:20 -0400444 for (i = 0; i < btrfs_header_nritems(&buf_node->header); i++) {
Chris Mason6407bf62007-03-27 06:33:00 -0400445 if (leaf) {
446 key = &buf_leaf->items[i].key;
447 if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY)
448 continue;
449 fi = btrfs_item_ptr(buf_leaf, i,
450 struct btrfs_file_extent_item);
Chris Mason236454df2007-04-19 13:37:44 -0400451 if (btrfs_file_extent_type(fi) ==
452 BTRFS_FILE_EXTENT_INLINE)
453 continue;
Chris Masonb18c6682007-04-17 13:26:50 -0400454 ret = btrfs_inc_extent_ref(trans, root,
Chris Mason6407bf62007-03-27 06:33:00 -0400455 btrfs_file_extent_disk_blocknr(fi),
456 btrfs_file_extent_disk_num_blocks(fi));
457 BUG_ON(ret);
458 } else {
459 blocknr = btrfs_node_blockptr(buf_node, i);
Chris Masonb18c6682007-04-17 13:26:50 -0400460 ret = btrfs_inc_extent_ref(trans, root, blocknr, 1);
Chris Mason6407bf62007-03-27 06:33:00 -0400461 BUG_ON(ret);
462 }
Chris Mason02217ed2007-03-02 16:08:05 -0500463 }
464 return 0;
465}
466
Chris Mason9078a3e2007-04-26 16:46:15 -0400467static int write_one_cache_group(struct btrfs_trans_handle *trans,
468 struct btrfs_root *root,
469 struct btrfs_path *path,
470 struct btrfs_block_group_cache *cache)
471{
472 int ret;
473 int pending_ret;
474 struct btrfs_root *extent_root = root->fs_info->extent_root;
475 struct btrfs_block_group_item *bi;
476 struct btrfs_key ins;
477
Chris Masonbe08c1b2007-05-03 09:06:49 -0400478 find_free_extent(trans, extent_root, 0, 0, (u64)-1, &ins, 0);
Chris Mason9078a3e2007-04-26 16:46:15 -0400479 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
480 BUG_ON(ret);
481 bi = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
482 struct btrfs_block_group_item);
483 memcpy(bi, &cache->item, sizeof(*bi));
484 mark_buffer_dirty(path->nodes[0]);
485 btrfs_release_path(extent_root, path);
486
487 finish_current_insert(trans, extent_root);
488 pending_ret = del_pending_extents(trans, extent_root);
489 if (ret)
490 return ret;
491 if (pending_ret)
492 return pending_ret;
Chris Masonbe744172007-05-06 10:15:01 -0400493 if (cache->data)
494 cache->last_alloc = cache->first_free;
Chris Mason9078a3e2007-04-26 16:46:15 -0400495 return 0;
496
497}
498
Chris Masonbe744172007-05-06 10:15:01 -0400499static int write_dirty_block_radix(struct btrfs_trans_handle *trans,
500 struct btrfs_root *root,
501 struct radix_tree_root *radix)
Chris Mason9078a3e2007-04-26 16:46:15 -0400502{
503 struct btrfs_block_group_cache *cache[8];
504 int ret;
505 int err = 0;
506 int werr = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -0400507 int i;
508 struct btrfs_path *path;
509
510 path = btrfs_alloc_path();
511 if (!path)
512 return -ENOMEM;
513
514 while(1) {
515 ret = radix_tree_gang_lookup_tag(radix, (void **)cache,
516 0, ARRAY_SIZE(cache),
517 BTRFS_BLOCK_GROUP_DIRTY);
518 if (!ret)
519 break;
520 for (i = 0; i < ret; i++) {
521 radix_tree_tag_clear(radix, cache[i]->key.objectid +
522 cache[i]->key.offset - 1,
523 BTRFS_BLOCK_GROUP_DIRTY);
524 err = write_one_cache_group(trans, root,
525 path, cache[i]);
526 if (err)
527 werr = err;
528 }
529 }
530 btrfs_free_path(path);
531 return werr;
532}
533
Chris Masonbe744172007-05-06 10:15:01 -0400534int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
535 struct btrfs_root *root)
536{
537 int ret;
538 int ret2;
539 ret = write_dirty_block_radix(trans, root,
540 &root->fs_info->block_group_radix);
541 ret2 = write_dirty_block_radix(trans, root,
542 &root->fs_info->block_group_data_radix);
543 if (ret)
544 return ret;
545 if (ret2)
546 return ret2;
547 return 0;
548}
549
Chris Mason9078a3e2007-04-26 16:46:15 -0400550static int update_block_group(struct btrfs_trans_handle *trans,
551 struct btrfs_root *root,
Chris Masone37c9e62007-05-09 20:13:14 -0400552 u64 blocknr, u64 num, int alloc, int mark_free)
Chris Mason9078a3e2007-04-26 16:46:15 -0400553{
554 struct btrfs_block_group_cache *cache;
555 struct btrfs_fs_info *info = root->fs_info;
556 u64 total = num;
557 u64 old_val;
558 u64 block_in_group;
Chris Masone37c9e62007-05-09 20:13:14 -0400559 u64 i;
Chris Mason3e1ad542007-05-07 20:03:49 -0400560
Chris Mason9078a3e2007-04-26 16:46:15 -0400561 while(total) {
Chris Mason3e1ad542007-05-07 20:03:49 -0400562 cache = lookup_block_group(info, blocknr);
563 if (!cache) {
Chris Masoncd1bc462007-04-27 10:08:34 -0400564 printk(KERN_CRIT "blocknr %Lu lookup failed\n",
565 blocknr);
Chris Mason9078a3e2007-04-26 16:46:15 -0400566 return -1;
Chris Masoncd1bc462007-04-27 10:08:34 -0400567 }
Chris Mason9078a3e2007-04-26 16:46:15 -0400568 block_in_group = blocknr - cache->key.objectid;
569 WARN_ON(block_in_group > cache->key.offset);
Chris Mason3e1ad542007-05-07 20:03:49 -0400570 radix_tree_tag_set(cache->radix, cache->key.objectid +
Chris Masonbe744172007-05-06 10:15:01 -0400571 cache->key.offset - 1,
Chris Mason9078a3e2007-04-26 16:46:15 -0400572 BTRFS_BLOCK_GROUP_DIRTY);
573
574 old_val = btrfs_block_group_used(&cache->item);
575 num = min(total, cache->key.offset - block_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -0400576 if (alloc) {
Chris Mason9078a3e2007-04-26 16:46:15 -0400577 old_val += num;
Chris Masoncd1bc462007-04-27 10:08:34 -0400578 if (blocknr > cache->last_alloc)
579 cache->last_alloc = blocknr;
Chris Masone37c9e62007-05-09 20:13:14 -0400580 if (!cache->data) {
581 for (i = 0; i < num; i++) {
582 clear_radix_bit(&info->extent_map_radix,
583 blocknr + i);
584 }
585 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400586 } else {
Chris Mason9078a3e2007-04-26 16:46:15 -0400587 old_val -= num;
Chris Masoncd1bc462007-04-27 10:08:34 -0400588 if (blocknr < cache->first_free)
589 cache->first_free = blocknr;
Chris Masone37c9e62007-05-09 20:13:14 -0400590 if (!cache->data && mark_free) {
591 for (i = 0; i < num; i++) {
592 set_radix_bit(&info->extent_map_radix,
593 blocknr + i);
594 }
595 }
Chris Masonde428b62007-05-18 13:28:27 -0400596 if (old_val < (cache->key.offset * 5) / 10 &&
597 old_val + num >= (cache->key.offset * 5) / 10) {
Chris Masone37c9e62007-05-09 20:13:14 -0400598printk("group %Lu now available\n", cache->key.objectid);
599 radix_tree_tag_set(cache->radix,
600 cache->key.objectid +
601 cache->key.offset - 1,
602 BTRFS_BLOCK_GROUP_AVAIL);
603 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400604 }
Chris Mason9078a3e2007-04-26 16:46:15 -0400605 btrfs_set_block_group_used(&cache->item, old_val);
Chris Masone37c9e62007-05-09 20:13:14 -0400606 total -= num;
607 blocknr += num;
Chris Mason9078a3e2007-04-26 16:46:15 -0400608 }
609 return 0;
610}
611
Chris Masonbe08c1b2007-05-03 09:06:49 -0400612static int try_remove_page(struct address_space *mapping, unsigned long index)
613{
614 int ret;
615 ret = invalidate_mapping_pages(mapping, index, index);
616 return ret;
617}
618
Chris Masone089f052007-03-16 16:20:31 -0400619int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, struct
620 btrfs_root *root)
Chris Masona28ec192007-03-06 20:08:01 -0500621{
Chris Mason8ef97622007-03-26 10:15:30 -0400622 unsigned long gang[8];
Chris Masonbe08c1b2007-05-03 09:06:49 -0400623 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Masonbe744172007-05-06 10:15:01 -0400624 struct btrfs_block_group_cache *block_group;
Chris Mason88fd1462007-03-16 08:56:18 -0400625 u64 first = 0;
Chris Masona28ec192007-03-06 20:08:01 -0500626 int ret;
627 int i;
Chris Mason8ef97622007-03-26 10:15:30 -0400628 struct radix_tree_root *pinned_radix = &root->fs_info->pinned_radix;
Chris Masone37c9e62007-05-09 20:13:14 -0400629 struct radix_tree_root *extent_radix = &root->fs_info->extent_map_radix;
Chris Masona28ec192007-03-06 20:08:01 -0500630
631 while(1) {
Chris Masone37c9e62007-05-09 20:13:14 -0400632 ret = find_first_radix_bit(pinned_radix, gang, 0,
Chris Mason8ef97622007-03-26 10:15:30 -0400633 ARRAY_SIZE(gang));
Chris Masona28ec192007-03-06 20:08:01 -0500634 if (!ret)
635 break;
Chris Mason88fd1462007-03-16 08:56:18 -0400636 if (!first)
Chris Mason8ef97622007-03-26 10:15:30 -0400637 first = gang[0];
Chris Mason0579da42007-03-07 16:15:30 -0500638 for (i = 0; i < ret; i++) {
Chris Mason8ef97622007-03-26 10:15:30 -0400639 clear_radix_bit(pinned_radix, gang[i]);
Chris Masonbe744172007-05-06 10:15:01 -0400640 block_group = lookup_block_group(root->fs_info,
641 gang[i]);
642 if (block_group) {
643 WARN_ON(block_group->pinned == 0);
644 block_group->pinned--;
645 if (gang[i] < block_group->last_alloc)
646 block_group->last_alloc = gang[i];
Chris Masone37c9e62007-05-09 20:13:14 -0400647 if (gang[i] < block_group->last_prealloc)
648 block_group->last_prealloc = gang[i];
649 if (!block_group->data)
650 set_radix_bit(extent_radix, gang[i]);
Chris Masonbe744172007-05-06 10:15:01 -0400651 }
Chris Masonbe08c1b2007-05-03 09:06:49 -0400652 try_remove_page(btree_inode->i_mapping,
653 gang[i] << (PAGE_CACHE_SHIFT -
654 btree_inode->i_blkbits));
Chris Mason0579da42007-03-07 16:15:30 -0500655 }
Chris Masona28ec192007-03-06 20:08:01 -0500656 }
657 return 0;
658}
659
Chris Masone089f052007-03-16 16:20:31 -0400660static int finish_current_insert(struct btrfs_trans_handle *trans, struct
661 btrfs_root *extent_root)
Chris Mason037e6392007-03-07 11:50:24 -0500662{
Chris Masone2fa7222007-03-12 16:22:34 -0400663 struct btrfs_key ins;
Chris Mason234b63a2007-03-13 10:46:10 -0400664 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -0500665 int i;
666 int ret;
Chris Mason1261ec42007-03-20 20:35:03 -0400667 u64 super_blocks_used;
668 struct btrfs_fs_info *info = extent_root->fs_info;
Chris Mason037e6392007-03-07 11:50:24 -0500669
Chris Masoncf27e1e2007-03-13 09:49:06 -0400670 btrfs_set_extent_refs(&extent_item, 1);
Chris Mason037e6392007-03-07 11:50:24 -0500671 ins.offset = 1;
672 ins.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -0400673 btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
Chris Mason5d0c3e62007-04-23 17:01:05 -0400674 btrfs_set_extent_owner(&extent_item, extent_root->root_key.objectid);
Chris Mason037e6392007-03-07 11:50:24 -0500675
Chris Masonf2458e12007-04-25 15:52:25 -0400676 for (i = 0; i < extent_root->fs_info->extent_tree_insert_nr; i++) {
677 ins.objectid = extent_root->fs_info->extent_tree_insert[i];
Chris Mason1261ec42007-03-20 20:35:03 -0400678 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
679 btrfs_set_super_blocks_used(info->disk_super,
680 super_blocks_used + 1);
Chris Masone089f052007-03-16 16:20:31 -0400681 ret = btrfs_insert_item(trans, extent_root, &ins, &extent_item,
682 sizeof(extent_item));
Chris Mason037e6392007-03-07 11:50:24 -0500683 BUG_ON(ret);
684 }
Chris Masonf2458e12007-04-25 15:52:25 -0400685 extent_root->fs_info->extent_tree_insert_nr = 0;
686 extent_root->fs_info->extent_tree_prealloc_nr = 0;
Chris Mason037e6392007-03-07 11:50:24 -0500687 return 0;
688}
689
Chris Mason8ef97622007-03-26 10:15:30 -0400690static int pin_down_block(struct btrfs_root *root, u64 blocknr, int pending)
Chris Masone20d96d2007-03-22 12:13:20 -0400691{
692 int err;
Chris Mason78fae272007-03-25 11:35:08 -0400693 struct btrfs_header *header;
Chris Mason8ef97622007-03-26 10:15:30 -0400694 struct buffer_head *bh;
Chris Mason78fae272007-03-25 11:35:08 -0400695
Chris Masonf4b9aa82007-03-27 11:05:53 -0400696 if (!pending) {
Chris Masond98237b2007-03-28 13:57:48 -0400697 bh = btrfs_find_tree_block(root, blocknr);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400698 if (bh) {
699 if (buffer_uptodate(bh)) {
700 u64 transid =
701 root->fs_info->running_transaction->transid;
702 header = btrfs_buffer_header(bh);
703 if (btrfs_header_generation(header) ==
704 transid) {
705 btrfs_block_release(root, bh);
706 return 0;
707 }
Chris Masonf4b9aa82007-03-27 11:05:53 -0400708 }
Chris Masond6025572007-03-30 14:27:56 -0400709 btrfs_block_release(root, bh);
Chris Mason8ef97622007-03-26 10:15:30 -0400710 }
Chris Mason8ef97622007-03-26 10:15:30 -0400711 err = set_radix_bit(&root->fs_info->pinned_radix, blocknr);
Chris Masonbe744172007-05-06 10:15:01 -0400712 if (!err) {
713 struct btrfs_block_group_cache *cache;
714 cache = lookup_block_group(root->fs_info, blocknr);
715 if (cache)
716 cache->pinned++;
717 }
Chris Masonf4b9aa82007-03-27 11:05:53 -0400718 } else {
719 err = set_radix_bit(&root->fs_info->pending_del_radix, blocknr);
720 }
Chris Masonbe744172007-05-06 10:15:01 -0400721 BUG_ON(err < 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400722 return 0;
723}
724
Chris Masona28ec192007-03-06 20:08:01 -0500725/*
726 * remove an extent from the root, returns 0 on success
727 */
Chris Masone089f052007-03-16 16:20:31 -0400728static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masone37c9e62007-05-09 20:13:14 -0400729 *root, u64 blocknr, u64 num_blocks, int pin,
730 int mark_free)
Chris Masona28ec192007-03-06 20:08:01 -0500731{
Chris Mason5caf2a02007-04-02 11:20:42 -0400732 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -0400733 struct btrfs_key key;
Chris Mason1261ec42007-03-20 20:35:03 -0400734 struct btrfs_fs_info *info = root->fs_info;
735 struct btrfs_root *extent_root = info->extent_root;
Chris Masona28ec192007-03-06 20:08:01 -0500736 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400737 struct btrfs_extent_item *ei;
Chris Masone2fa7222007-03-12 16:22:34 -0400738 struct btrfs_key ins;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400739 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500740
Chris Masona28ec192007-03-06 20:08:01 -0500741 key.objectid = blocknr;
742 key.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -0400743 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masona28ec192007-03-06 20:08:01 -0500744 key.offset = num_blocks;
745
Chris Masonbe08c1b2007-05-03 09:06:49 -0400746 find_free_extent(trans, root, 0, 0, (u64)-1, &ins, 0);
Chris Mason5caf2a02007-04-02 11:20:42 -0400747 path = btrfs_alloc_path();
748 BUG_ON(!path);
749 btrfs_init_path(path);
Chris Mason5f26f772007-04-05 10:38:44 -0400750
Chris Mason5caf2a02007-04-02 11:20:42 -0400751 ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
Chris Masona28ec192007-03-06 20:08:01 -0500752 if (ret) {
Chris Mason2e635a22007-03-21 11:12:56 -0400753 printk("failed to find %Lu\n", key.objectid);
Chris Mason234b63a2007-03-13 10:46:10 -0400754 btrfs_print_tree(extent_root, extent_root->node);
Chris Mason2e635a22007-03-21 11:12:56 -0400755 printk("failed to find %Lu\n", key.objectid);
Chris Masona28ec192007-03-06 20:08:01 -0500756 BUG();
757 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400758 ei = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
Chris Mason123abc82007-03-14 14:14:43 -0400759 struct btrfs_extent_item);
Chris Masona28ec192007-03-06 20:08:01 -0500760 BUG_ON(ei->refs == 0);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400761 refs = btrfs_extent_refs(ei) - 1;
762 btrfs_set_extent_refs(ei, refs);
Chris Mason5caf2a02007-04-02 11:20:42 -0400763 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400764 if (refs == 0) {
Chris Mason1261ec42007-03-20 20:35:03 -0400765 u64 super_blocks_used;
Chris Mason78fae272007-03-25 11:35:08 -0400766
767 if (pin) {
Chris Mason8ef97622007-03-26 10:15:30 -0400768 ret = pin_down_block(root, blocknr, 0);
Chris Mason78fae272007-03-25 11:35:08 -0400769 BUG_ON(ret);
770 }
771
Chris Mason1261ec42007-03-20 20:35:03 -0400772 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
773 btrfs_set_super_blocks_used(info->disk_super,
774 super_blocks_used - num_blocks);
Chris Mason5caf2a02007-04-02 11:20:42 -0400775 ret = btrfs_del_item(trans, extent_root, path);
Chris Masona28ec192007-03-06 20:08:01 -0500776 if (ret)
777 BUG();
Chris Masone37c9e62007-05-09 20:13:14 -0400778 ret = update_block_group(trans, root, blocknr, num_blocks, 0,
779 mark_free);
Chris Mason9078a3e2007-04-26 16:46:15 -0400780 BUG_ON(ret);
Chris Masona28ec192007-03-06 20:08:01 -0500781 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400782 btrfs_free_path(path);
Chris Masone089f052007-03-16 16:20:31 -0400783 finish_current_insert(trans, extent_root);
Chris Masona28ec192007-03-06 20:08:01 -0500784 return ret;
785}
786
787/*
Chris Masonfec577f2007-02-26 10:40:21 -0500788 * find all the blocks marked as pending in the radix tree and remove
789 * them from the extent map
790 */
Chris Masone089f052007-03-16 16:20:31 -0400791static int del_pending_extents(struct btrfs_trans_handle *trans, struct
792 btrfs_root *extent_root)
Chris Masonfec577f2007-02-26 10:40:21 -0500793{
794 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -0400795 int wret;
796 int err = 0;
Chris Mason8ef97622007-03-26 10:15:30 -0400797 unsigned long gang[4];
Chris Masonfec577f2007-02-26 10:40:21 -0500798 int i;
Chris Mason8ef97622007-03-26 10:15:30 -0400799 struct radix_tree_root *pending_radix;
800 struct radix_tree_root *pinned_radix;
Chris Masonbe744172007-05-06 10:15:01 -0400801 struct btrfs_block_group_cache *cache;
Chris Mason8ef97622007-03-26 10:15:30 -0400802
803 pending_radix = &extent_root->fs_info->pending_del_radix;
804 pinned_radix = &extent_root->fs_info->pinned_radix;
Chris Masonfec577f2007-02-26 10:40:21 -0500805
806 while(1) {
Chris Masone37c9e62007-05-09 20:13:14 -0400807 ret = find_first_radix_bit(pending_radix, gang, 0,
Chris Mason8ef97622007-03-26 10:15:30 -0400808 ARRAY_SIZE(gang));
Chris Masonfec577f2007-02-26 10:40:21 -0500809 if (!ret)
810 break;
811 for (i = 0; i < ret; i++) {
Chris Mason8ef97622007-03-26 10:15:30 -0400812 wret = set_radix_bit(pinned_radix, gang[i]);
Chris Masonbe744172007-05-06 10:15:01 -0400813 if (wret == 0) {
814 cache = lookup_block_group(extent_root->fs_info,
815 gang[i]);
816 if (cache)
817 cache->pinned++;
818 }
819 if (wret < 0) {
820 printk(KERN_CRIT "set_radix_bit, err %d\n",
821 wret);
822 BUG_ON(wret < 0);
823 }
Chris Mason8ef97622007-03-26 10:15:30 -0400824 wret = clear_radix_bit(pending_radix, gang[i]);
825 BUG_ON(wret);
Chris Masond5719762007-03-23 10:01:08 -0400826 wret = __free_extent(trans, extent_root,
Chris Masone37c9e62007-05-09 20:13:14 -0400827 gang[i], 1, 0, 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400828 if (wret)
829 err = wret;
Chris Masonfec577f2007-02-26 10:40:21 -0500830 }
831 }
Chris Masone20d96d2007-03-22 12:13:20 -0400832 return err;
Chris Masonfec577f2007-02-26 10:40:21 -0500833}
834
835/*
836 * remove an extent from the root, returns 0 on success
837 */
Chris Masone089f052007-03-16 16:20:31 -0400838int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
839 *root, u64 blocknr, u64 num_blocks, int pin)
Chris Masonfec577f2007-02-26 10:40:21 -0500840{
Chris Mason9f5fae22007-03-20 14:38:32 -0400841 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Masonfec577f2007-02-26 10:40:21 -0500842 int pending_ret;
843 int ret;
Chris Masona28ec192007-03-06 20:08:01 -0500844
845 if (root == extent_root) {
Chris Mason8ef97622007-03-26 10:15:30 -0400846 pin_down_block(root, blocknr, 1);
Chris Masona28ec192007-03-06 20:08:01 -0500847 return 0;
848 }
Chris Masone37c9e62007-05-09 20:13:14 -0400849 ret = __free_extent(trans, root, blocknr, num_blocks, pin, pin == 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400850 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
Chris Masonfec577f2007-02-26 10:40:21 -0500851 return ret ? ret : pending_ret;
852}
853
854/*
855 * walks the btree of allocated extents and find a hole of a given size.
856 * The key ins is changed to record the hole:
857 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -0400858 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -0500859 * ins->offset == number of blocks
860 * Any available blocks before search_start are skipped.
861 */
Chris Masone089f052007-03-16 16:20:31 -0400862static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
863 *orig_root, u64 num_blocks, u64 search_start, u64
Chris Masonbe08c1b2007-05-03 09:06:49 -0400864 search_end, struct btrfs_key *ins, int data)
Chris Masonfec577f2007-02-26 10:40:21 -0500865{
Chris Mason5caf2a02007-04-02 11:20:42 -0400866 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -0400867 struct btrfs_key key;
Chris Masonfec577f2007-02-26 10:40:21 -0500868 int ret;
869 u64 hole_size = 0;
870 int slot = 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400871 u64 last_block = 0;
Chris Mason037e6392007-03-07 11:50:24 -0500872 u64 test_block;
Chris Masonbe744172007-05-06 10:15:01 -0400873 u64 orig_search_start = search_start;
Chris Masonfec577f2007-02-26 10:40:21 -0500874 int start_found;
Chris Mason234b63a2007-03-13 10:46:10 -0400875 struct btrfs_leaf *l;
Chris Mason9f5fae22007-03-20 14:38:32 -0400876 struct btrfs_root * root = orig_root->fs_info->extent_root;
Chris Masonf2458e12007-04-25 15:52:25 -0400877 struct btrfs_fs_info *info = root->fs_info;
Chris Mason0579da42007-03-07 16:15:30 -0500878 int total_needed = num_blocks;
Chris Masonf2458e12007-04-25 15:52:25 -0400879 int total_found = 0;
880 int fill_prealloc = 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400881 int level;
Chris Masonbe08c1b2007-05-03 09:06:49 -0400882 struct btrfs_block_group_cache *block_group;
Chris Masonbe744172007-05-06 10:15:01 -0400883 int full_scan = 0;
Chris Masonde428b62007-05-18 13:28:27 -0400884 u64 limit;
Chris Masonfec577f2007-02-26 10:40:21 -0500885
Chris Masonb1a4d962007-04-04 15:27:52 -0400886 path = btrfs_alloc_path();
887 ins->flags = 0;
888 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
889
Chris Masone20d96d2007-03-22 12:13:20 -0400890 level = btrfs_header_level(btrfs_buffer_header(root->node));
Chris Masonf2458e12007-04-25 15:52:25 -0400891 if (num_blocks == 0) {
892 fill_prealloc = 1;
893 num_blocks = 1;
Chris Mason308535a2007-04-28 15:17:08 -0400894 total_needed = (min(level + 1, BTRFS_MAX_LEVEL) + 2) * 3;
Chris Masonf2458e12007-04-25 15:52:25 -0400895 }
Chris Mason3e1ad542007-05-07 20:03:49 -0400896 if (search_end == (u64)-1)
897 search_end = btrfs_super_total_blocks(info->disk_super);
Chris Masonbe744172007-05-06 10:15:01 -0400898 if (search_start) {
899 block_group = lookup_block_group(info, search_start);
900 block_group = btrfs_find_block_group(root, block_group,
Chris Masonde428b62007-05-18 13:28:27 -0400901 search_start, data, 1);
Chris Masonbe744172007-05-06 10:15:01 -0400902 } else {
903 block_group = btrfs_find_block_group(root,
904 trans->block_group, 0,
Chris Masonde428b62007-05-18 13:28:27 -0400905 data, 1);
Chris Masonbe744172007-05-06 10:15:01 -0400906 }
907
908check_failed:
Chris Mason3e1ad542007-05-07 20:03:49 -0400909 if (!full_scan && block_group->data != data)
Chris Masonbe744172007-05-06 10:15:01 -0400910 WARN_ON(1);
Chris Masone37c9e62007-05-09 20:13:14 -0400911
912 if (!data)
913 search_start = find_search_start(root, &block_group,
914 search_start, total_needed);
915 else
916 search_start = max(block_group->last_alloc, search_start);
917
Chris Mason5caf2a02007-04-02 11:20:42 -0400918 btrfs_init_path(path);
Chris Masonfec577f2007-02-26 10:40:21 -0500919 ins->objectid = search_start;
920 ins->offset = 0;
Chris Masonfec577f2007-02-26 10:40:21 -0500921 start_found = 0;
Chris Masone37c9e62007-05-09 20:13:14 -0400922
Chris Mason5caf2a02007-04-02 11:20:42 -0400923 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
Chris Mason0f70abe2007-02-28 16:46:22 -0500924 if (ret < 0)
925 goto error;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500926
Chris Masone37c9e62007-05-09 20:13:14 -0400927 if (path->slots[0] > 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400928 path->slots[0]--;
Chris Masone37c9e62007-05-09 20:13:14 -0400929 }
930
931 l = btrfs_buffer_leaf(path->nodes[0]);
932 btrfs_disk_key_to_cpu(&key, &l->items[path->slots[0]].key);
933 /*
934 * a rare case, go back one key if we hit a block group item
935 * instead of an extent item
936 */
937 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY &&
938 key.objectid + key.offset >= search_start) {
939 ins->objectid = key.objectid;
940 ins->offset = key.offset - 1;
941 btrfs_release_path(root, path);
942 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
943 if (ret < 0)
944 goto error;
945
946 if (path->slots[0] > 0) {
947 path->slots[0]--;
948 }
949 }
Chris Mason0579da42007-03-07 16:15:30 -0500950
Chris Masonfec577f2007-02-26 10:40:21 -0500951 while (1) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400952 l = btrfs_buffer_leaf(path->nodes[0]);
953 slot = path->slots[0];
Chris Mason7518a232007-03-12 12:01:18 -0400954 if (slot >= btrfs_header_nritems(&l->header)) {
Chris Masonf2458e12007-04-25 15:52:25 -0400955 if (fill_prealloc) {
956 info->extent_tree_prealloc_nr = 0;
957 total_found = 0;
958 }
Chris Masonde428b62007-05-18 13:28:27 -0400959 if (start_found)
960 limit = last_block +
961 block_group->key.offset / 2;
962 else
963 limit = search_start +
964 block_group->key.offset / 2;
Chris Mason5caf2a02007-04-02 11:20:42 -0400965 ret = btrfs_next_leaf(root, path);
Chris Masonfec577f2007-02-26 10:40:21 -0500966 if (ret == 0)
967 continue;
Chris Mason0f70abe2007-02-28 16:46:22 -0500968 if (ret < 0)
969 goto error;
Chris Masonfec577f2007-02-26 10:40:21 -0500970 if (!start_found) {
971 ins->objectid = search_start;
Chris Mason3e1ad542007-05-07 20:03:49 -0400972 ins->offset = search_end - search_start;
Chris Masonfec577f2007-02-26 10:40:21 -0500973 start_found = 1;
974 goto check_pending;
975 }
976 ins->objectid = last_block > search_start ?
977 last_block : search_start;
Chris Mason3e1ad542007-05-07 20:03:49 -0400978 ins->offset = search_end - ins->objectid;
Chris Masonfec577f2007-02-26 10:40:21 -0500979 goto check_pending;
980 }
Chris Masone37c9e62007-05-09 20:13:14 -0400981
Chris Masone2fa7222007-03-12 16:22:34 -0400982 btrfs_disk_key_to_cpu(&key, &l->items[slot].key);
Chris Masone37c9e62007-05-09 20:13:14 -0400983 if (key.objectid >= search_start && key.objectid > last_block &&
984 start_found) {
985 if (last_block < search_start)
986 last_block = search_start;
987 hole_size = key.objectid - last_block;
988 if (hole_size >= num_blocks) {
989 ins->objectid = last_block;
990 ins->offset = hole_size;
991 goto check_pending;
Chris Mason0579da42007-03-07 16:15:30 -0500992 }
Chris Masonfec577f2007-02-26 10:40:21 -0500993 }
Chris Masone37c9e62007-05-09 20:13:14 -0400994
995 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY)
996 goto next;
997
Chris Mason0579da42007-03-07 16:15:30 -0500998 start_found = 1;
Chris Masone2fa7222007-03-12 16:22:34 -0400999 last_block = key.objectid + key.offset;
Chris Masonbe744172007-05-06 10:15:01 -04001000 if (last_block >= block_group->key.objectid +
1001 block_group->key.offset) {
1002 btrfs_release_path(root, path);
1003 search_start = block_group->key.objectid +
1004 block_group->key.offset * 2;
1005 goto new_group;
1006 }
Chris Mason9078a3e2007-04-26 16:46:15 -04001007next:
Chris Mason5caf2a02007-04-02 11:20:42 -04001008 path->slots[0]++;
Chris Masonde428b62007-05-18 13:28:27 -04001009 cond_resched();
Chris Masonfec577f2007-02-26 10:40:21 -05001010 }
1011 // FIXME -ENOSPC
1012check_pending:
1013 /* we have to make sure we didn't find an extent that has already
1014 * been allocated by the map tree or the original allocation
1015 */
Chris Mason5caf2a02007-04-02 11:20:42 -04001016 btrfs_release_path(root, path);
Chris Masonfec577f2007-02-26 10:40:21 -05001017 BUG_ON(ins->objectid < search_start);
Chris Masone37c9e62007-05-09 20:13:14 -04001018
Chris Mason3e1ad542007-05-07 20:03:49 -04001019 if (ins->objectid + num_blocks >= search_end) {
Chris Masonbe744172007-05-06 10:15:01 -04001020 if (full_scan)
Chris Mason06a2f9f2007-04-28 08:48:10 -04001021 return -ENOSPC;
Chris Masonbe744172007-05-06 10:15:01 -04001022 search_start = orig_search_start;
1023 full_scan = 1;
1024 goto new_group;
Chris Mason06a2f9f2007-04-28 08:48:10 -04001025 }
Chris Mason037e6392007-03-07 11:50:24 -05001026 for (test_block = ins->objectid;
Chris Masonf2458e12007-04-25 15:52:25 -04001027 test_block < ins->objectid + num_blocks; test_block++) {
1028 if (test_radix_bit(&info->pinned_radix, test_block)) {
Chris Mason037e6392007-03-07 11:50:24 -05001029 search_start = test_block + 1;
Chris Masonbe744172007-05-06 10:15:01 -04001030 goto new_group;
Chris Masonfec577f2007-02-26 10:40:21 -05001031 }
1032 }
Chris Masonf2458e12007-04-25 15:52:25 -04001033 if (!fill_prealloc && info->extent_tree_insert_nr) {
1034 u64 last =
1035 info->extent_tree_insert[info->extent_tree_insert_nr - 1];
1036 if (ins->objectid + num_blocks >
1037 info->extent_tree_insert[0] &&
1038 ins->objectid <= last) {
1039 search_start = last + 1;
Chris Masone37c9e62007-05-09 20:13:14 -04001040 WARN_ON(!full_scan);
Chris Masonbe744172007-05-06 10:15:01 -04001041 goto new_group;
Chris Masonf2458e12007-04-25 15:52:25 -04001042 }
1043 }
1044 if (!fill_prealloc && info->extent_tree_prealloc_nr) {
1045 u64 first =
1046 info->extent_tree_prealloc[info->extent_tree_prealloc_nr - 1];
1047 if (ins->objectid + num_blocks > first &&
1048 ins->objectid <= info->extent_tree_prealloc[0]) {
1049 search_start = info->extent_tree_prealloc[0] + 1;
Chris Masone37c9e62007-05-09 20:13:14 -04001050 WARN_ON(!full_scan);
Chris Masonbe744172007-05-06 10:15:01 -04001051 goto new_group;
Chris Masonf2458e12007-04-25 15:52:25 -04001052 }
1053 }
1054 if (fill_prealloc) {
1055 int nr;
1056 test_block = ins->objectid;
Chris Masone37c9e62007-05-09 20:13:14 -04001057 if (test_block - info->extent_tree_prealloc[total_needed - 1] >=
1058 leaf_range(root)) {
1059 total_found = 0;
1060 info->extent_tree_prealloc_nr = total_found;
1061 }
Chris Masonf2458e12007-04-25 15:52:25 -04001062 while(test_block < ins->objectid + ins->offset &&
1063 total_found < total_needed) {
1064 nr = total_needed - total_found - 1;
1065 BUG_ON(nr < 0);
Chris Masoncd1bc462007-04-27 10:08:34 -04001066 info->extent_tree_prealloc[nr] = test_block;
Chris Masonf2458e12007-04-25 15:52:25 -04001067 total_found++;
1068 test_block++;
1069 }
1070 if (total_found < total_needed) {
1071 search_start = test_block;
Chris Masonbe744172007-05-06 10:15:01 -04001072 goto new_group;
Chris Masonf2458e12007-04-25 15:52:25 -04001073 }
Chris Masoncd1bc462007-04-27 10:08:34 -04001074 info->extent_tree_prealloc_nr = total_found;
Chris Masonf2458e12007-04-25 15:52:25 -04001075 }
Chris Masone37c9e62007-05-09 20:13:14 -04001076 if (!data) {
1077 block_group = lookup_block_group(info, ins->objectid);
1078 if (block_group) {
1079 if (fill_prealloc)
1080 block_group->last_prealloc =
1081 info->extent_tree_prealloc[total_needed-1];
1082 else
1083 trans->block_group = block_group;
1084 }
Chris Masoncd1bc462007-04-27 10:08:34 -04001085 }
Chris Mason037e6392007-03-07 11:50:24 -05001086 ins->offset = num_blocks;
Chris Mason5caf2a02007-04-02 11:20:42 -04001087 btrfs_free_path(path);
Chris Masonfec577f2007-02-26 10:40:21 -05001088 return 0;
Chris Masonbe744172007-05-06 10:15:01 -04001089
1090new_group:
Chris Mason3e1ad542007-05-07 20:03:49 -04001091 if (search_start + num_blocks >= search_end) {
Chris Masonbe744172007-05-06 10:15:01 -04001092 search_start = orig_search_start;
Chris Masone37c9e62007-05-09 20:13:14 -04001093printk("doing full scan!\n");
Chris Masonbe744172007-05-06 10:15:01 -04001094 full_scan = 1;
1095 }
1096 block_group = lookup_block_group(info, search_start);
1097 if (!full_scan)
1098 block_group = btrfs_find_block_group(root, block_group,
Chris Masonde428b62007-05-18 13:28:27 -04001099 search_start, data, 0);
1100 cond_resched();
Chris Masonbe744172007-05-06 10:15:01 -04001101 goto check_failed;
1102
Chris Mason0f70abe2007-02-28 16:46:22 -05001103error:
Chris Mason5caf2a02007-04-02 11:20:42 -04001104 btrfs_release_path(root, path);
1105 btrfs_free_path(path);
Chris Mason0f70abe2007-02-28 16:46:22 -05001106 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05001107}
Chris Masonfec577f2007-02-26 10:40:21 -05001108/*
Chris Masonfec577f2007-02-26 10:40:21 -05001109 * finds a free extent and does all the dirty work required for allocation
1110 * returns the key for the extent through ins, and a tree buffer for
1111 * the first block of the extent through buf.
1112 *
1113 * returns 0 if everything worked, non-zero otherwise.
1114 */
Chris Mason4d775672007-04-20 20:23:12 -04001115int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
1116 struct btrfs_root *root, u64 owner,
Chris Masonc62a1922007-04-24 12:07:39 -04001117 u64 num_blocks, u64 search_start,
Chris Masonbe08c1b2007-05-03 09:06:49 -04001118 u64 search_end, struct btrfs_key *ins, int data)
Chris Masonfec577f2007-02-26 10:40:21 -05001119{
1120 int ret;
1121 int pending_ret;
Chris Mason1261ec42007-03-20 20:35:03 -04001122 u64 super_blocks_used;
1123 struct btrfs_fs_info *info = root->fs_info;
1124 struct btrfs_root *extent_root = info->extent_root;
Chris Mason234b63a2007-03-13 10:46:10 -04001125 struct btrfs_extent_item extent_item;
Chris Masonf2458e12007-04-25 15:52:25 -04001126 struct btrfs_key prealloc_key;
Chris Mason037e6392007-03-07 11:50:24 -05001127
Chris Masoncf27e1e2007-03-13 09:49:06 -04001128 btrfs_set_extent_refs(&extent_item, 1);
Chris Mason4d775672007-04-20 20:23:12 -04001129 btrfs_set_extent_owner(&extent_item, owner);
Chris Masonfec577f2007-02-26 10:40:21 -05001130
Chris Mason037e6392007-03-07 11:50:24 -05001131 if (root == extent_root) {
Chris Masonf2458e12007-04-25 15:52:25 -04001132 int nr;
1133 BUG_ON(info->extent_tree_prealloc_nr == 0);
Chris Mason037e6392007-03-07 11:50:24 -05001134 BUG_ON(num_blocks != 1);
Chris Mason037e6392007-03-07 11:50:24 -05001135 ins->offset = 1;
Chris Masonf2458e12007-04-25 15:52:25 -04001136 info->extent_tree_prealloc_nr--;
1137 nr = info->extent_tree_prealloc_nr;
1138 ins->objectid = info->extent_tree_prealloc[nr];
1139 info->extent_tree_insert[info->extent_tree_insert_nr++] =
1140 ins->objectid;
Chris Mason9078a3e2007-04-26 16:46:15 -04001141 ret = update_block_group(trans, root,
Chris Masone37c9e62007-05-09 20:13:14 -04001142 ins->objectid, ins->offset, 1, 0);
Chris Mason9078a3e2007-04-26 16:46:15 -04001143 BUG_ON(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05001144 return 0;
1145 }
Chris Masone37c9e62007-05-09 20:13:14 -04001146
1147 /*
1148 * if we're doing a data allocation, preallocate room in the
1149 * extent tree first. This way the extent tree blocks end up
1150 * in the correct block group.
1151 */
1152 if (data) {
Chris Masonde428b62007-05-18 13:28:27 -04001153 ret = find_free_extent(trans, root, 0, 0,
Chris Masone37c9e62007-05-09 20:13:14 -04001154 search_end, &prealloc_key, 0);
1155 if (ret) {
1156 return ret;
1157 }
1158 if (prealloc_key.objectid + prealloc_key.offset >= search_end) {
1159 int nr = info->extent_tree_prealloc_nr;
1160 search_end = info->extent_tree_prealloc[nr - 1] - 1;
1161 } else {
1162 search_start = info->extent_tree_prealloc[0] + 1;
1163 }
1164 }
Chris Masonf2458e12007-04-25 15:52:25 -04001165 /* do the real allocation */
Chris Masone089f052007-03-16 16:20:31 -04001166 ret = find_free_extent(trans, root, num_blocks, search_start,
Chris Masonbe08c1b2007-05-03 09:06:49 -04001167 search_end, ins, data);
Chris Masone37c9e62007-05-09 20:13:14 -04001168 if (ret) {
Chris Mason037e6392007-03-07 11:50:24 -05001169 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -04001170 }
Chris Masonfec577f2007-02-26 10:40:21 -05001171
Chris Masone37c9e62007-05-09 20:13:14 -04001172 /*
1173 * if we're doing a metadata allocation, preallocate space in the
1174 * extent tree second. This way, we don't create a tiny hole
1175 * in the allocation map between any unused preallocation blocks
1176 * and the metadata block we're actually allocating. On disk,
1177 * it'll go:
1178 * [block we've allocated], [used prealloc 1], [ unused prealloc ]
1179 * The unused prealloc will get reused the next time around.
1180 */
1181 if (!data) {
1182 if (ins->objectid + ins->offset >= search_end)
1183 search_end = ins->objectid - 1;
1184 else
1185 search_start = ins->objectid + ins->offset;
Chris Mason3e1ad542007-05-07 20:03:49 -04001186
Chris Masone37c9e62007-05-09 20:13:14 -04001187 ret = find_free_extent(trans, root, 0, search_start,
1188 search_end, &prealloc_key, 0);
1189 if (ret) {
1190 return ret;
1191 }
1192 }
Chris Masonf2458e12007-04-25 15:52:25 -04001193
Chris Mason1261ec42007-03-20 20:35:03 -04001194 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
1195 btrfs_set_super_blocks_used(info->disk_super, super_blocks_used +
1196 num_blocks);
Chris Masone089f052007-03-16 16:20:31 -04001197 ret = btrfs_insert_item(trans, extent_root, ins, &extent_item,
1198 sizeof(extent_item));
Chris Mason037e6392007-03-07 11:50:24 -05001199
Chris Masone089f052007-03-16 16:20:31 -04001200 finish_current_insert(trans, extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -04001201 pending_ret = del_pending_extents(trans, extent_root);
Chris Masone37c9e62007-05-09 20:13:14 -04001202 if (ret) {
Chris Mason037e6392007-03-07 11:50:24 -05001203 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -04001204 }
1205 if (pending_ret) {
Chris Mason037e6392007-03-07 11:50:24 -05001206 return pending_ret;
Chris Masone37c9e62007-05-09 20:13:14 -04001207 }
1208 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0);
Chris Mason037e6392007-03-07 11:50:24 -05001209 return 0;
Chris Masonfec577f2007-02-26 10:40:21 -05001210}
1211
1212/*
1213 * helper function to allocate a block for a given tree
1214 * returns the tree buffer or NULL.
1215 */
Chris Masone20d96d2007-03-22 12:13:20 -04001216struct buffer_head *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Chris Mason31f3c992007-04-30 15:25:45 -04001217 struct btrfs_root *root, u64 hint)
Chris Masonfec577f2007-02-26 10:40:21 -05001218{
Chris Masone2fa7222007-03-12 16:22:34 -04001219 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -05001220 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -04001221 struct buffer_head *buf;
Chris Masonfec577f2007-02-26 10:40:21 -05001222
Chris Mason4d775672007-04-20 20:23:12 -04001223 ret = btrfs_alloc_extent(trans, root, root->root_key.objectid,
Chris Masonde428b62007-05-18 13:28:27 -04001224 1, hint, (unsigned long)-1, &ins, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05001225 if (ret) {
1226 BUG();
1227 return NULL;
1228 }
Chris Mason9078a3e2007-04-26 16:46:15 -04001229 BUG_ON(ret);
Chris Masond98237b2007-03-28 13:57:48 -04001230 buf = btrfs_find_create_tree_block(root, ins.objectid);
Chris Masondf2ce342007-03-23 11:00:45 -04001231 set_buffer_uptodate(buf);
Chris Mason090d1872007-05-01 08:53:32 -04001232 set_buffer_checked(buf);
Chris Mason7c4452b2007-04-28 09:29:35 -04001233 set_radix_bit(&trans->transaction->dirty_pages, buf->b_page->index);
Chris Masonfec577f2007-02-26 10:40:21 -05001234 return buf;
1235}
Chris Masona28ec192007-03-06 20:08:01 -05001236
Chris Mason6407bf62007-03-27 06:33:00 -04001237static int drop_leaf_ref(struct btrfs_trans_handle *trans,
1238 struct btrfs_root *root, struct buffer_head *cur)
1239{
1240 struct btrfs_disk_key *key;
1241 struct btrfs_leaf *leaf;
1242 struct btrfs_file_extent_item *fi;
1243 int i;
1244 int nritems;
1245 int ret;
1246
1247 BUG_ON(!btrfs_is_leaf(btrfs_buffer_node(cur)));
1248 leaf = btrfs_buffer_leaf(cur);
1249 nritems = btrfs_header_nritems(&leaf->header);
1250 for (i = 0; i < nritems; i++) {
1251 key = &leaf->items[i].key;
1252 if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY)
1253 continue;
1254 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
Chris Mason236454df2007-04-19 13:37:44 -04001255 if (btrfs_file_extent_type(fi) == BTRFS_FILE_EXTENT_INLINE)
1256 continue;
Chris Mason6407bf62007-03-27 06:33:00 -04001257 /*
1258 * FIXME make sure to insert a trans record that
1259 * repeats the snapshot del on crash
1260 */
1261 ret = btrfs_free_extent(trans, root,
1262 btrfs_file_extent_disk_blocknr(fi),
1263 btrfs_file_extent_disk_num_blocks(fi),
1264 0);
1265 BUG_ON(ret);
1266 }
1267 return 0;
1268}
1269
Chris Mason9aca1d52007-03-13 11:09:37 -04001270/*
1271 * helper function for drop_snapshot, this walks down the tree dropping ref
1272 * counts as it goes.
1273 */
Chris Masone089f052007-03-16 16:20:31 -04001274static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
1275 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05001276{
Chris Masone20d96d2007-03-22 12:13:20 -04001277 struct buffer_head *next;
1278 struct buffer_head *cur;
Chris Mason20524f02007-03-10 06:35:47 -05001279 u64 blocknr;
1280 int ret;
1281 u32 refs;
1282
Chris Mason5caf2a02007-04-02 11:20:42 -04001283 WARN_ON(*level < 0);
1284 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Masonb18c6682007-04-17 13:26:50 -04001285 ret = lookup_extent_ref(trans, root, bh_blocknr(path->nodes[*level]),
Chris Mason6407bf62007-03-27 06:33:00 -04001286 1, &refs);
Chris Mason20524f02007-03-10 06:35:47 -05001287 BUG_ON(ret);
1288 if (refs > 1)
1289 goto out;
Chris Mason9aca1d52007-03-13 11:09:37 -04001290 /*
1291 * walk down to the last node level and free all the leaves
1292 */
Chris Mason6407bf62007-03-27 06:33:00 -04001293 while(*level >= 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001294 WARN_ON(*level < 0);
1295 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason20524f02007-03-10 06:35:47 -05001296 cur = path->nodes[*level];
Chris Mason2c90e5d2007-04-02 10:50:19 -04001297 if (btrfs_header_level(btrfs_buffer_header(cur)) != *level)
1298 WARN_ON(1);
Chris Mason7518a232007-03-12 12:01:18 -04001299 if (path->slots[*level] >=
Chris Masone20d96d2007-03-22 12:13:20 -04001300 btrfs_header_nritems(btrfs_buffer_header(cur)))
Chris Mason20524f02007-03-10 06:35:47 -05001301 break;
Chris Mason6407bf62007-03-27 06:33:00 -04001302 if (*level == 0) {
1303 ret = drop_leaf_ref(trans, root, cur);
1304 BUG_ON(ret);
1305 break;
1306 }
Chris Masone20d96d2007-03-22 12:13:20 -04001307 blocknr = btrfs_node_blockptr(btrfs_buffer_node(cur),
1308 path->slots[*level]);
Chris Masonb18c6682007-04-17 13:26:50 -04001309 ret = lookup_extent_ref(trans, root, blocknr, 1, &refs);
Chris Mason6407bf62007-03-27 06:33:00 -04001310 BUG_ON(ret);
1311 if (refs != 1) {
Chris Mason20524f02007-03-10 06:35:47 -05001312 path->slots[*level]++;
Chris Masone089f052007-03-16 16:20:31 -04001313 ret = btrfs_free_extent(trans, root, blocknr, 1, 1);
Chris Mason20524f02007-03-10 06:35:47 -05001314 BUG_ON(ret);
1315 continue;
1316 }
Chris Mason20524f02007-03-10 06:35:47 -05001317 next = read_tree_block(root, blocknr);
Chris Mason5caf2a02007-04-02 11:20:42 -04001318 WARN_ON(*level <= 0);
Chris Mason83e15a22007-03-12 09:03:27 -04001319 if (path->nodes[*level-1])
Chris Mason234b63a2007-03-13 10:46:10 -04001320 btrfs_block_release(root, path->nodes[*level-1]);
Chris Mason20524f02007-03-10 06:35:47 -05001321 path->nodes[*level-1] = next;
Chris Masone20d96d2007-03-22 12:13:20 -04001322 *level = btrfs_header_level(btrfs_buffer_header(next));
Chris Mason20524f02007-03-10 06:35:47 -05001323 path->slots[*level] = 0;
1324 }
1325out:
Chris Mason5caf2a02007-04-02 11:20:42 -04001326 WARN_ON(*level < 0);
1327 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason6407bf62007-03-27 06:33:00 -04001328 ret = btrfs_free_extent(trans, root,
Chris Mason7eccb902007-04-11 15:53:25 -04001329 bh_blocknr(path->nodes[*level]), 1, 1);
Chris Mason234b63a2007-03-13 10:46:10 -04001330 btrfs_block_release(root, path->nodes[*level]);
Chris Mason20524f02007-03-10 06:35:47 -05001331 path->nodes[*level] = NULL;
1332 *level += 1;
1333 BUG_ON(ret);
1334 return 0;
1335}
1336
Chris Mason9aca1d52007-03-13 11:09:37 -04001337/*
1338 * helper for dropping snapshots. This walks back up the tree in the path
1339 * to find the first node higher up where we haven't yet gone through
1340 * all the slots
1341 */
Chris Masone089f052007-03-16 16:20:31 -04001342static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root
1343 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05001344{
1345 int i;
1346 int slot;
1347 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -04001348 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Mason20524f02007-03-10 06:35:47 -05001349 slot = path->slots[i];
Chris Masone20d96d2007-03-22 12:13:20 -04001350 if (slot < btrfs_header_nritems(
1351 btrfs_buffer_header(path->nodes[i])) - 1) {
Chris Mason20524f02007-03-10 06:35:47 -05001352 path->slots[i]++;
1353 *level = i;
1354 return 0;
1355 } else {
Chris Masone089f052007-03-16 16:20:31 -04001356 ret = btrfs_free_extent(trans, root,
Chris Mason7eccb902007-04-11 15:53:25 -04001357 bh_blocknr(path->nodes[*level]),
Chris Masone089f052007-03-16 16:20:31 -04001358 1, 1);
Chris Mason6407bf62007-03-27 06:33:00 -04001359 BUG_ON(ret);
Chris Mason234b63a2007-03-13 10:46:10 -04001360 btrfs_block_release(root, path->nodes[*level]);
Chris Mason83e15a22007-03-12 09:03:27 -04001361 path->nodes[*level] = NULL;
Chris Mason20524f02007-03-10 06:35:47 -05001362 *level = i + 1;
Chris Mason20524f02007-03-10 06:35:47 -05001363 }
1364 }
1365 return 1;
1366}
1367
Chris Mason9aca1d52007-03-13 11:09:37 -04001368/*
1369 * drop the reference count on the tree rooted at 'snap'. This traverses
1370 * the tree freeing any blocks that have a ref count of zero after being
1371 * decremented.
1372 */
Chris Masone089f052007-03-16 16:20:31 -04001373int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masone20d96d2007-03-22 12:13:20 -04001374 *root, struct buffer_head *snap)
Chris Mason20524f02007-03-10 06:35:47 -05001375{
Chris Mason3768f362007-03-13 16:47:54 -04001376 int ret = 0;
Chris Mason9aca1d52007-03-13 11:09:37 -04001377 int wret;
Chris Mason20524f02007-03-10 06:35:47 -05001378 int level;
Chris Mason5caf2a02007-04-02 11:20:42 -04001379 struct btrfs_path *path;
Chris Mason20524f02007-03-10 06:35:47 -05001380 int i;
1381 int orig_level;
1382
Chris Mason5caf2a02007-04-02 11:20:42 -04001383 path = btrfs_alloc_path();
1384 BUG_ON(!path);
1385 btrfs_init_path(path);
Chris Mason20524f02007-03-10 06:35:47 -05001386
Chris Masone20d96d2007-03-22 12:13:20 -04001387 level = btrfs_header_level(btrfs_buffer_header(snap));
Chris Mason20524f02007-03-10 06:35:47 -05001388 orig_level = level;
Chris Mason5caf2a02007-04-02 11:20:42 -04001389 path->nodes[level] = snap;
1390 path->slots[level] = 0;
Chris Mason20524f02007-03-10 06:35:47 -05001391 while(1) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001392 wret = walk_down_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04001393 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05001394 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04001395 if (wret < 0)
1396 ret = wret;
1397
Chris Mason5caf2a02007-04-02 11:20:42 -04001398 wret = walk_up_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04001399 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05001400 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04001401 if (wret < 0)
1402 ret = wret;
Chris Mason35b7e472007-05-02 15:53:43 -04001403 btrfs_btree_balance_dirty(root);
Chris Mason20524f02007-03-10 06:35:47 -05001404 }
Chris Mason83e15a22007-03-12 09:03:27 -04001405 for (i = 0; i <= orig_level; i++) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001406 if (path->nodes[i]) {
1407 btrfs_block_release(root, path->nodes[i]);
Chris Mason83e15a22007-03-12 09:03:27 -04001408 }
Chris Mason20524f02007-03-10 06:35:47 -05001409 }
Chris Mason5caf2a02007-04-02 11:20:42 -04001410 btrfs_free_path(path);
Chris Mason9aca1d52007-03-13 11:09:37 -04001411 return ret;
Chris Mason20524f02007-03-10 06:35:47 -05001412}
Chris Mason9078a3e2007-04-26 16:46:15 -04001413
Chris Masonbe744172007-05-06 10:15:01 -04001414static int free_block_group_radix(struct radix_tree_root *radix)
Chris Mason9078a3e2007-04-26 16:46:15 -04001415{
1416 int ret;
1417 struct btrfs_block_group_cache *cache[8];
1418 int i;
1419
1420 while(1) {
Chris Masonbe744172007-05-06 10:15:01 -04001421 ret = radix_tree_gang_lookup(radix, (void **)cache, 0,
Chris Mason9078a3e2007-04-26 16:46:15 -04001422 ARRAY_SIZE(cache));
1423 if (!ret)
1424 break;
1425 for (i = 0; i < ret; i++) {
Chris Masonbe744172007-05-06 10:15:01 -04001426 radix_tree_delete(radix, cache[i]->key.objectid +
Chris Mason9078a3e2007-04-26 16:46:15 -04001427 cache[i]->key.offset - 1);
1428 kfree(cache[i]);
1429 }
1430 }
1431 return 0;
1432}
1433
Chris Masonbe744172007-05-06 10:15:01 -04001434int btrfs_free_block_groups(struct btrfs_fs_info *info)
1435{
1436 int ret;
1437 int ret2;
Chris Masone37c9e62007-05-09 20:13:14 -04001438 unsigned long gang[16];
1439 int i;
Chris Masonbe744172007-05-06 10:15:01 -04001440
1441 ret = free_block_group_radix(&info->block_group_radix);
1442 ret2 = free_block_group_radix(&info->block_group_data_radix);
1443 if (ret)
1444 return ret;
1445 if (ret2)
1446 return ret2;
Chris Masone37c9e62007-05-09 20:13:14 -04001447
1448 while(1) {
1449 ret = find_first_radix_bit(&info->extent_map_radix,
1450 gang, 0, ARRAY_SIZE(gang));
1451 if (!ret)
1452 break;
1453 for (i = 0; i < ret; i++) {
1454 clear_radix_bit(&info->extent_map_radix, gang[i]);
1455 }
1456 }
Chris Masonbe744172007-05-06 10:15:01 -04001457 return 0;
1458}
1459
Chris Mason9078a3e2007-04-26 16:46:15 -04001460int btrfs_read_block_groups(struct btrfs_root *root)
1461{
1462 struct btrfs_path *path;
1463 int ret;
1464 int err = 0;
1465 struct btrfs_block_group_item *bi;
1466 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04001467 struct btrfs_fs_info *info = root->fs_info;
1468 struct radix_tree_root *radix;
Chris Mason9078a3e2007-04-26 16:46:15 -04001469 struct btrfs_key key;
1470 struct btrfs_key found_key;
1471 struct btrfs_leaf *leaf;
1472 u64 group_size_blocks = BTRFS_BLOCK_GROUP_SIZE / root->blocksize;
Chris Mason31f3c992007-04-30 15:25:45 -04001473 u64 used;
Chris Masonbe744172007-05-06 10:15:01 -04001474 u64 nr = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04001475
Chris Masonbe744172007-05-06 10:15:01 -04001476 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04001477 key.objectid = 0;
1478 key.offset = group_size_blocks;
1479 key.flags = 0;
1480 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
1481
1482 path = btrfs_alloc_path();
1483 if (!path)
1484 return -ENOMEM;
1485
1486 while(1) {
Chris Masonbe744172007-05-06 10:15:01 -04001487 ret = btrfs_search_slot(NULL, info->extent_root,
Chris Mason9078a3e2007-04-26 16:46:15 -04001488 &key, path, 0, 0);
1489 if (ret != 0) {
1490 err = ret;
1491 break;
1492 }
1493 leaf = btrfs_buffer_leaf(path->nodes[0]);
1494 btrfs_disk_key_to_cpu(&found_key,
1495 &leaf->items[path->slots[0]].key);
1496 cache = kmalloc(sizeof(*cache), GFP_NOFS);
1497 if (!cache) {
1498 err = -1;
1499 break;
1500 }
Chris Mason3e1ad542007-05-07 20:03:49 -04001501
Chris Masone37c9e62007-05-09 20:13:14 -04001502 if (nr % 3)
Chris Mason3e1ad542007-05-07 20:03:49 -04001503 radix = &info->block_group_data_radix;
1504 else
1505 radix = &info->block_group_radix;
1506
Chris Mason9078a3e2007-04-26 16:46:15 -04001507 bi = btrfs_item_ptr(leaf, path->slots[0],
1508 struct btrfs_block_group_item);
1509 memcpy(&cache->item, bi, sizeof(*bi));
1510 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Mason31f3c992007-04-30 15:25:45 -04001511 cache->last_alloc = cache->key.objectid;
1512 cache->first_free = cache->key.objectid;
Chris Masone37c9e62007-05-09 20:13:14 -04001513 cache->last_prealloc = cache->key.objectid;
Chris Masonbe744172007-05-06 10:15:01 -04001514 cache->pinned = 0;
Chris Masone37c9e62007-05-09 20:13:14 -04001515 cache->cached = 0;
1516
1517 if (nr % 3)
1518 cache->data = 1;
1519 else
1520 cache->data = 0;
Chris Mason3e1ad542007-05-07 20:03:49 -04001521 cache->radix = radix;
1522
Chris Mason9078a3e2007-04-26 16:46:15 -04001523 key.objectid = found_key.objectid + found_key.offset;
1524 btrfs_release_path(root, path);
Chris Masonbe744172007-05-06 10:15:01 -04001525 ret = radix_tree_insert(radix, found_key.objectid +
Chris Mason9078a3e2007-04-26 16:46:15 -04001526 found_key.offset - 1,
1527 (void *)cache);
1528 BUG_ON(ret);
Chris Mason31f3c992007-04-30 15:25:45 -04001529 used = btrfs_block_group_used(bi);
Chris Masonbe744172007-05-06 10:15:01 -04001530 if (used < (key.offset * 8) / 10) {
1531 radix_tree_tag_set(radix, found_key.objectid +
Chris Mason31f3c992007-04-30 15:25:45 -04001532 found_key.offset - 1,
1533 BTRFS_BLOCK_GROUP_AVAIL);
1534 }
Chris Mason9078a3e2007-04-26 16:46:15 -04001535 if (key.objectid >=
Chris Masonbe744172007-05-06 10:15:01 -04001536 btrfs_super_total_blocks(info->disk_super))
Chris Mason9078a3e2007-04-26 16:46:15 -04001537 break;
Chris Masonbe744172007-05-06 10:15:01 -04001538 nr++;
Chris Mason9078a3e2007-04-26 16:46:15 -04001539 }
1540
1541 btrfs_free_path(path);
1542 return 0;
1543}