blob: b6dc020bdde9e83c08d78d68a6f0803c9f8a0cdb [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 Mason1e2677e2007-05-29 16:52:18 -0400235 struct radix_tree_root *swap_radix;
Chris Masoncd1bc462007-04-27 10:08:34 -0400236 u64 used;
Chris Mason31f3c992007-04-30 15:25:45 -0400237 u64 last = 0;
238 u64 hint_last;
Chris Masoncd1bc462007-04-27 10:08:34 -0400239 int i;
240 int ret;
Chris Mason31f3c992007-04-30 15:25:45 -0400241 int full_search = 0;
Chris Masonde428b62007-05-18 13:28:27 -0400242 int factor = 8;
Chris Mason1e2677e2007-05-29 16:52:18 -0400243 int data_swap = 0;
Chris Masonde428b62007-05-18 13:28:27 -0400244
245 if (!owner)
246 factor = 5;
Chris Masonbe744172007-05-06 10:15:01 -0400247
Chris Mason1e2677e2007-05-29 16:52:18 -0400248 if (data) {
Chris Masonbe744172007-05-06 10:15:01 -0400249 radix = &info->block_group_data_radix;
Chris Mason1e2677e2007-05-29 16:52:18 -0400250 swap_radix = &info->block_group_radix;
251 } else {
Chris Masonbe744172007-05-06 10:15:01 -0400252 radix = &info->block_group_radix;
Chris Mason1e2677e2007-05-29 16:52:18 -0400253 swap_radix = &info->block_group_data_radix;
254 }
Chris Masonbe744172007-05-06 10:15:01 -0400255
256 if (search_start) {
257 struct btrfs_block_group_cache *shint;
258 shint = lookup_block_group(info, search_start);
259 if (shint->data == data) {
260 used = btrfs_block_group_used(&shint->item);
261 if (used + shint->pinned <
Chris Masonde428b62007-05-18 13:28:27 -0400262 (shint->key.offset * factor) / 10) {
Chris Masonbe744172007-05-06 10:15:01 -0400263 return shint;
264 }
265 }
266 }
267 if (hint && hint->data == data) {
Chris Mason31f3c992007-04-30 15:25:45 -0400268 used = btrfs_block_group_used(&hint->item);
Chris Masonde428b62007-05-18 13:28:27 -0400269 if (used + hint->pinned < (hint->key.offset * factor) / 10) {
Chris Mason31f3c992007-04-30 15:25:45 -0400270 return hint;
271 }
Chris Masonbe744172007-05-06 10:15:01 -0400272 if (used >= (hint->key.offset * 8) / 10) {
273 radix_tree_tag_clear(radix,
274 hint->key.objectid +
275 hint->key.offset - 1,
276 BTRFS_BLOCK_GROUP_AVAIL);
277 }
Chris Mason8d7be552007-05-10 11:24:42 -0400278 last = hint->key.offset * 3;
Chris Masonbe744172007-05-06 10:15:01 -0400279 if (hint->key.objectid >= last)
Chris Masone37c9e62007-05-09 20:13:14 -0400280 last = max(search_start + hint->key.offset - 1,
281 hint->key.objectid - last);
Chris Masonbe744172007-05-06 10:15:01 -0400282 else
283 last = hint->key.objectid + hint->key.offset;
Chris Mason31f3c992007-04-30 15:25:45 -0400284 hint_last = last;
285 } else {
Chris Masone37c9e62007-05-09 20:13:14 -0400286 if (hint)
287 hint_last = max(hint->key.objectid, search_start);
288 else
289 hint_last = search_start;
290
291 last = hint_last;
Chris Mason31f3c992007-04-30 15:25:45 -0400292 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400293 while(1) {
Chris Masonbe744172007-05-06 10:15:01 -0400294 ret = radix_tree_gang_lookup_tag(radix, (void **)cache,
Chris Masoncd1bc462007-04-27 10:08:34 -0400295 last, ARRAY_SIZE(cache),
Chris Mason31f3c992007-04-30 15:25:45 -0400296 BTRFS_BLOCK_GROUP_AVAIL);
Chris Masoncd1bc462007-04-27 10:08:34 -0400297 if (!ret)
298 break;
299 for (i = 0; i < ret; i++) {
Chris Masonbe08c1b2007-05-03 09:06:49 -0400300 last = cache[i]->key.objectid +
301 cache[i]->key.offset;
Chris Masoncd1bc462007-04-27 10:08:34 -0400302 used = btrfs_block_group_used(&cache[i]->item);
Chris Masonbe744172007-05-06 10:15:01 -0400303 if (used + cache[i]->pinned <
Chris Masonde428b62007-05-18 13:28:27 -0400304 (cache[i]->key.offset * factor) / 10) {
Chris Mason31f3c992007-04-30 15:25:45 -0400305 found_group = cache[i];
306 goto found;
Chris Masoncd1bc462007-04-27 10:08:34 -0400307 }
Chris Masonbe744172007-05-06 10:15:01 -0400308 if (used >= (cache[i]->key.offset * 8) / 10) {
309 radix_tree_tag_clear(radix,
310 cache[i]->key.objectid +
311 cache[i]->key.offset - 1,
312 BTRFS_BLOCK_GROUP_AVAIL);
313 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400314 }
Chris Masonde428b62007-05-18 13:28:27 -0400315 cond_resched();
Chris Masoncd1bc462007-04-27 10:08:34 -0400316 }
Chris Mason31f3c992007-04-30 15:25:45 -0400317 last = hint_last;
318again:
Chris Masoncd1bc462007-04-27 10:08:34 -0400319 while(1) {
Chris Masonbe744172007-05-06 10:15:01 -0400320 ret = radix_tree_gang_lookup(radix, (void **)cache,
321 last, ARRAY_SIZE(cache));
Chris Masoncd1bc462007-04-27 10:08:34 -0400322 if (!ret)
323 break;
324 for (i = 0; i < ret; i++) {
Chris Masonbe08c1b2007-05-03 09:06:49 -0400325 last = cache[i]->key.objectid +
326 cache[i]->key.offset;
Chris Masoncd1bc462007-04-27 10:08:34 -0400327 used = btrfs_block_group_used(&cache[i]->item);
Chris Masonbe744172007-05-06 10:15:01 -0400328 if (used + cache[i]->pinned < cache[i]->key.offset) {
Chris Mason31f3c992007-04-30 15:25:45 -0400329 found_group = cache[i];
330 goto found;
Chris Masoncd1bc462007-04-27 10:08:34 -0400331 }
Chris Masonbe744172007-05-06 10:15:01 -0400332 if (used >= cache[i]->key.offset) {
333 radix_tree_tag_clear(radix,
334 cache[i]->key.objectid +
335 cache[i]->key.offset - 1,
336 BTRFS_BLOCK_GROUP_AVAIL);
337 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400338 }
Chris Masonde428b62007-05-18 13:28:27 -0400339 cond_resched();
Chris Masoncd1bc462007-04-27 10:08:34 -0400340 }
Chris Mason31f3c992007-04-30 15:25:45 -0400341 if (!full_search) {
Chris Masonbe744172007-05-06 10:15:01 -0400342 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -0400343 full_search = 1;
344 goto again;
345 }
Chris Mason1e2677e2007-05-29 16:52:18 -0400346 if (!data_swap) {
347 struct radix_tree_root *tmp = radix;
348 data_swap = 1;
349 radix = swap_radix;
350 swap_radix = tmp;
351 last = search_start;
352 goto again;
353 }
Chris Mason31f3c992007-04-30 15:25:45 -0400354 if (!found_group) {
Chris Masonde428b62007-05-18 13:28:27 -0400355printk("find block group bailing to zero data %d\n", data);
Chris Masonbe744172007-05-06 10:15:01 -0400356 ret = radix_tree_gang_lookup(radix,
Chris Mason31f3c992007-04-30 15:25:45 -0400357 (void **)&found_group, 0, 1);
Chris Mason1e2677e2007-05-29 16:52:18 -0400358 if (ret == 0) {
359 ret = radix_tree_gang_lookup(swap_radix,
360 (void **)&found_group,
361 0, 1);
362 }
Chris Mason31f3c992007-04-30 15:25:45 -0400363 BUG_ON(ret != 1);
364 }
Chris Masonbe744172007-05-06 10:15:01 -0400365found:
Chris Mason31f3c992007-04-30 15:25:45 -0400366 return found_group;
Chris Masoncd1bc462007-04-27 10:08:34 -0400367}
368
Chris Masonb18c6682007-04-17 13:26:50 -0400369int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
370 struct btrfs_root *root,
371 u64 blocknr, u64 num_blocks)
Chris Mason02217ed2007-03-02 16:08:05 -0500372{
Chris Mason5caf2a02007-04-02 11:20:42 -0400373 struct btrfs_path *path;
Chris Mason02217ed2007-03-02 16:08:05 -0500374 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400375 struct btrfs_key key;
Chris Mason234b63a2007-03-13 10:46:10 -0400376 struct btrfs_leaf *l;
377 struct btrfs_extent_item *item;
Chris Masone2fa7222007-03-12 16:22:34 -0400378 struct btrfs_key ins;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400379 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500380
Chris Mason9f5fae22007-03-20 14:38:32 -0400381 find_free_extent(trans, root->fs_info->extent_root, 0, 0, (u64)-1,
Chris Masonbe08c1b2007-05-03 09:06:49 -0400382 &ins, 0);
Chris Mason5caf2a02007-04-02 11:20:42 -0400383 path = btrfs_alloc_path();
384 BUG_ON(!path);
385 btrfs_init_path(path);
Chris Mason02217ed2007-03-02 16:08:05 -0500386 key.objectid = blocknr;
387 key.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -0400388 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Mason6407bf62007-03-27 06:33:00 -0400389 key.offset = num_blocks;
Chris Mason5caf2a02007-04-02 11:20:42 -0400390 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -0400391 0, 1);
Chris Masona429e512007-04-18 16:15:28 -0400392 if (ret != 0) {
393printk("can't find block %Lu %Lu\n", blocknr, num_blocks);
Chris Masona28ec192007-03-06 20:08:01 -0500394 BUG();
Chris Masona429e512007-04-18 16:15:28 -0400395 }
Chris Mason02217ed2007-03-02 16:08:05 -0500396 BUG_ON(ret != 0);
Chris Mason5caf2a02007-04-02 11:20:42 -0400397 l = btrfs_buffer_leaf(path->nodes[0]);
398 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400399 refs = btrfs_extent_refs(item);
400 btrfs_set_extent_refs(item, refs + 1);
Chris Mason5caf2a02007-04-02 11:20:42 -0400401 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masona28ec192007-03-06 20:08:01 -0500402
Chris Mason5caf2a02007-04-02 11:20:42 -0400403 btrfs_release_path(root->fs_info->extent_root, path);
404 btrfs_free_path(path);
Chris Mason9f5fae22007-03-20 14:38:32 -0400405 finish_current_insert(trans, root->fs_info->extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -0400406 del_pending_extents(trans, root->fs_info->extent_root);
Chris Mason02217ed2007-03-02 16:08:05 -0500407 return 0;
408}
409
Chris Masonb18c6682007-04-17 13:26:50 -0400410static int lookup_extent_ref(struct btrfs_trans_handle *trans,
411 struct btrfs_root *root, u64 blocknr,
412 u64 num_blocks, u32 *refs)
Chris Masona28ec192007-03-06 20:08:01 -0500413{
Chris Mason5caf2a02007-04-02 11:20:42 -0400414 struct btrfs_path *path;
Chris Masona28ec192007-03-06 20:08:01 -0500415 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400416 struct btrfs_key key;
Chris Mason234b63a2007-03-13 10:46:10 -0400417 struct btrfs_leaf *l;
418 struct btrfs_extent_item *item;
Chris Mason5caf2a02007-04-02 11:20:42 -0400419
420 path = btrfs_alloc_path();
421 btrfs_init_path(path);
Chris Masona28ec192007-03-06 20:08:01 -0500422 key.objectid = blocknr;
Chris Mason6407bf62007-03-27 06:33:00 -0400423 key.offset = num_blocks;
Chris Mason62e27492007-03-15 12:56:47 -0400424 key.flags = 0;
425 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Mason5caf2a02007-04-02 11:20:42 -0400426 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -0400427 0, 0);
Chris Masona28ec192007-03-06 20:08:01 -0500428 if (ret != 0)
429 BUG();
Chris Mason5caf2a02007-04-02 11:20:42 -0400430 l = btrfs_buffer_leaf(path->nodes[0]);
431 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400432 *refs = btrfs_extent_refs(item);
Chris Mason5caf2a02007-04-02 11:20:42 -0400433 btrfs_release_path(root->fs_info->extent_root, path);
434 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -0500435 return 0;
436}
437
Chris Masonc5739bb2007-04-10 09:27:04 -0400438int btrfs_inc_root_ref(struct btrfs_trans_handle *trans,
439 struct btrfs_root *root)
440{
Chris Masonb18c6682007-04-17 13:26:50 -0400441 return btrfs_inc_extent_ref(trans, root, bh_blocknr(root->node), 1);
Chris Masonc5739bb2007-04-10 09:27:04 -0400442}
443
Chris Masone089f052007-03-16 16:20:31 -0400444int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Masone20d96d2007-03-22 12:13:20 -0400445 struct buffer_head *buf)
Chris Mason02217ed2007-03-02 16:08:05 -0500446{
447 u64 blocknr;
Chris Masone20d96d2007-03-22 12:13:20 -0400448 struct btrfs_node *buf_node;
Chris Mason6407bf62007-03-27 06:33:00 -0400449 struct btrfs_leaf *buf_leaf;
450 struct btrfs_disk_key *key;
451 struct btrfs_file_extent_item *fi;
Chris Mason02217ed2007-03-02 16:08:05 -0500452 int i;
Chris Mason6407bf62007-03-27 06:33:00 -0400453 int leaf;
454 int ret;
Chris Masona28ec192007-03-06 20:08:01 -0500455
Chris Mason3768f362007-03-13 16:47:54 -0400456 if (!root->ref_cows)
Chris Masona28ec192007-03-06 20:08:01 -0500457 return 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400458 buf_node = btrfs_buffer_node(buf);
Chris Mason6407bf62007-03-27 06:33:00 -0400459 leaf = btrfs_is_leaf(buf_node);
460 buf_leaf = btrfs_buffer_leaf(buf);
Chris Masone20d96d2007-03-22 12:13:20 -0400461 for (i = 0; i < btrfs_header_nritems(&buf_node->header); i++) {
Chris Mason6407bf62007-03-27 06:33:00 -0400462 if (leaf) {
Chris Mason3a686372007-05-24 13:35:57 -0400463 u64 disk_blocknr;
Chris Mason6407bf62007-03-27 06:33:00 -0400464 key = &buf_leaf->items[i].key;
465 if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY)
466 continue;
467 fi = btrfs_item_ptr(buf_leaf, i,
468 struct btrfs_file_extent_item);
Chris Mason236454d2007-04-19 13:37:44 -0400469 if (btrfs_file_extent_type(fi) ==
470 BTRFS_FILE_EXTENT_INLINE)
471 continue;
Chris Mason3a686372007-05-24 13:35:57 -0400472 disk_blocknr = btrfs_file_extent_disk_blocknr(fi);
473 if (disk_blocknr == 0)
474 continue;
475 ret = btrfs_inc_extent_ref(trans, root, disk_blocknr,
Chris Mason6407bf62007-03-27 06:33:00 -0400476 btrfs_file_extent_disk_num_blocks(fi));
477 BUG_ON(ret);
478 } else {
479 blocknr = btrfs_node_blockptr(buf_node, i);
Chris Masonb18c6682007-04-17 13:26:50 -0400480 ret = btrfs_inc_extent_ref(trans, root, blocknr, 1);
Chris Mason6407bf62007-03-27 06:33:00 -0400481 BUG_ON(ret);
482 }
Chris Mason02217ed2007-03-02 16:08:05 -0500483 }
484 return 0;
485}
486
Chris Mason9078a3e2007-04-26 16:46:15 -0400487static int write_one_cache_group(struct btrfs_trans_handle *trans,
488 struct btrfs_root *root,
489 struct btrfs_path *path,
490 struct btrfs_block_group_cache *cache)
491{
492 int ret;
493 int pending_ret;
494 struct btrfs_root *extent_root = root->fs_info->extent_root;
495 struct btrfs_block_group_item *bi;
496 struct btrfs_key ins;
497
Chris Masonbe08c1b2007-05-03 09:06:49 -0400498 find_free_extent(trans, extent_root, 0, 0, (u64)-1, &ins, 0);
Chris Mason9078a3e2007-04-26 16:46:15 -0400499 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
500 BUG_ON(ret);
501 bi = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
502 struct btrfs_block_group_item);
503 memcpy(bi, &cache->item, sizeof(*bi));
504 mark_buffer_dirty(path->nodes[0]);
505 btrfs_release_path(extent_root, path);
506
507 finish_current_insert(trans, extent_root);
508 pending_ret = del_pending_extents(trans, extent_root);
509 if (ret)
510 return ret;
511 if (pending_ret)
512 return pending_ret;
Chris Masonbe744172007-05-06 10:15:01 -0400513 if (cache->data)
514 cache->last_alloc = cache->first_free;
Chris Mason9078a3e2007-04-26 16:46:15 -0400515 return 0;
516
517}
518
Chris Masonbe744172007-05-06 10:15:01 -0400519static int write_dirty_block_radix(struct btrfs_trans_handle *trans,
520 struct btrfs_root *root,
521 struct radix_tree_root *radix)
Chris Mason9078a3e2007-04-26 16:46:15 -0400522{
523 struct btrfs_block_group_cache *cache[8];
524 int ret;
525 int err = 0;
526 int werr = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -0400527 int i;
528 struct btrfs_path *path;
529
530 path = btrfs_alloc_path();
531 if (!path)
532 return -ENOMEM;
533
534 while(1) {
535 ret = radix_tree_gang_lookup_tag(radix, (void **)cache,
536 0, ARRAY_SIZE(cache),
537 BTRFS_BLOCK_GROUP_DIRTY);
538 if (!ret)
539 break;
540 for (i = 0; i < ret; i++) {
541 radix_tree_tag_clear(radix, cache[i]->key.objectid +
542 cache[i]->key.offset - 1,
543 BTRFS_BLOCK_GROUP_DIRTY);
544 err = write_one_cache_group(trans, root,
545 path, cache[i]);
546 if (err)
547 werr = err;
548 }
549 }
550 btrfs_free_path(path);
551 return werr;
552}
553
Chris Masonbe744172007-05-06 10:15:01 -0400554int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
555 struct btrfs_root *root)
556{
557 int ret;
558 int ret2;
559 ret = write_dirty_block_radix(trans, root,
560 &root->fs_info->block_group_radix);
561 ret2 = write_dirty_block_radix(trans, root,
562 &root->fs_info->block_group_data_radix);
563 if (ret)
564 return ret;
565 if (ret2)
566 return ret2;
567 return 0;
568}
569
Chris Mason9078a3e2007-04-26 16:46:15 -0400570static int update_block_group(struct btrfs_trans_handle *trans,
571 struct btrfs_root *root,
Chris Mason1e2677e2007-05-29 16:52:18 -0400572 u64 blocknr, u64 num, int alloc, int mark_free,
573 int data)
Chris Mason9078a3e2007-04-26 16:46:15 -0400574{
575 struct btrfs_block_group_cache *cache;
576 struct btrfs_fs_info *info = root->fs_info;
577 u64 total = num;
578 u64 old_val;
579 u64 block_in_group;
Chris Masone37c9e62007-05-09 20:13:14 -0400580 u64 i;
Chris Mason1e2677e2007-05-29 16:52:18 -0400581 int ret;
Chris Mason3e1ad542007-05-07 20:03:49 -0400582
Chris Mason9078a3e2007-04-26 16:46:15 -0400583 while(total) {
Chris Mason3e1ad542007-05-07 20:03:49 -0400584 cache = lookup_block_group(info, blocknr);
585 if (!cache) {
Chris Masoncd1bc462007-04-27 10:08:34 -0400586 printk(KERN_CRIT "blocknr %Lu lookup failed\n",
587 blocknr);
Chris Mason9078a3e2007-04-26 16:46:15 -0400588 return -1;
Chris Masoncd1bc462007-04-27 10:08:34 -0400589 }
Chris Mason9078a3e2007-04-26 16:46:15 -0400590 block_in_group = blocknr - cache->key.objectid;
591 WARN_ON(block_in_group > cache->key.offset);
Chris Mason3e1ad542007-05-07 20:03:49 -0400592 radix_tree_tag_set(cache->radix, cache->key.objectid +
Chris Masonbe744172007-05-06 10:15:01 -0400593 cache->key.offset - 1,
Chris Mason9078a3e2007-04-26 16:46:15 -0400594 BTRFS_BLOCK_GROUP_DIRTY);
595
596 old_val = btrfs_block_group_used(&cache->item);
597 num = min(total, cache->key.offset - block_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -0400598 if (alloc) {
Chris Masoncd1bc462007-04-27 10:08:34 -0400599 if (blocknr > cache->last_alloc)
600 cache->last_alloc = blocknr;
Chris Masone37c9e62007-05-09 20:13:14 -0400601 if (!cache->data) {
602 for (i = 0; i < num; i++) {
603 clear_radix_bit(&info->extent_map_radix,
604 blocknr + i);
605 }
606 }
Chris Mason1e2677e2007-05-29 16:52:18 -0400607 if (cache->data != data &&
608 old_val < cache->key.offset / 2) {
609printk("changing block group %Lu from %d to %d\n", cache->key.objectid, cache->data, data);
610 cache->data = data;
611 radix_tree_delete(cache->radix,
612 cache->key.objectid +
613 cache->key.offset - 1);
614
615 if (data) {
616 cache->radix =
617 &info->block_group_data_radix;
618 cache->item.flags |=
619 BTRFS_BLOCK_GROUP_DATA;
620 } else {
621 cache->radix = &info->block_group_radix;
622 cache->item.flags &=
623 ~BTRFS_BLOCK_GROUP_DATA;
624 }
625 ret = radix_tree_insert(cache->radix,
626 cache->key.objectid +
627 cache->key.offset - 1,
628 (void *)cache);
629 }
630 old_val += num;
Chris Masoncd1bc462007-04-27 10:08:34 -0400631 } else {
Chris Mason9078a3e2007-04-26 16:46:15 -0400632 old_val -= num;
Chris Masoncd1bc462007-04-27 10:08:34 -0400633 if (blocknr < cache->first_free)
634 cache->first_free = blocknr;
Chris Masone37c9e62007-05-09 20:13:14 -0400635 if (!cache->data && mark_free) {
636 for (i = 0; i < num; i++) {
637 set_radix_bit(&info->extent_map_radix,
638 blocknr + i);
639 }
640 }
Chris Mason1e2677e2007-05-29 16:52:18 -0400641 if (old_val < cache->key.offset / 2 &&
642 old_val + num >= cache->key.offset / 2) {
Chris Masone37c9e62007-05-09 20:13:14 -0400643printk("group %Lu now available\n", cache->key.objectid);
644 radix_tree_tag_set(cache->radix,
645 cache->key.objectid +
646 cache->key.offset - 1,
647 BTRFS_BLOCK_GROUP_AVAIL);
648 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400649 }
Chris Mason9078a3e2007-04-26 16:46:15 -0400650 btrfs_set_block_group_used(&cache->item, old_val);
Chris Masone37c9e62007-05-09 20:13:14 -0400651 total -= num;
652 blocknr += num;
Chris Mason9078a3e2007-04-26 16:46:15 -0400653 }
654 return 0;
655}
656
Chris Masonbe08c1b2007-05-03 09:06:49 -0400657static int try_remove_page(struct address_space *mapping, unsigned long index)
658{
659 int ret;
660 ret = invalidate_mapping_pages(mapping, index, index);
661 return ret;
662}
663
Chris Masone089f052007-03-16 16:20:31 -0400664int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, struct
665 btrfs_root *root)
Chris Masona28ec192007-03-06 20:08:01 -0500666{
Chris Mason8ef97622007-03-26 10:15:30 -0400667 unsigned long gang[8];
Chris Masonbe08c1b2007-05-03 09:06:49 -0400668 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Masonbe744172007-05-06 10:15:01 -0400669 struct btrfs_block_group_cache *block_group;
Chris Mason88fd1462007-03-16 08:56:18 -0400670 u64 first = 0;
Chris Masona28ec192007-03-06 20:08:01 -0500671 int ret;
672 int i;
Chris Mason8ef97622007-03-26 10:15:30 -0400673 struct radix_tree_root *pinned_radix = &root->fs_info->pinned_radix;
Chris Masone37c9e62007-05-09 20:13:14 -0400674 struct radix_tree_root *extent_radix = &root->fs_info->extent_map_radix;
Chris Masona28ec192007-03-06 20:08:01 -0500675
676 while(1) {
Chris Masone37c9e62007-05-09 20:13:14 -0400677 ret = find_first_radix_bit(pinned_radix, gang, 0,
Chris Mason8ef97622007-03-26 10:15:30 -0400678 ARRAY_SIZE(gang));
Chris Masona28ec192007-03-06 20:08:01 -0500679 if (!ret)
680 break;
Chris Mason88fd1462007-03-16 08:56:18 -0400681 if (!first)
Chris Mason8ef97622007-03-26 10:15:30 -0400682 first = gang[0];
Chris Mason0579da42007-03-07 16:15:30 -0500683 for (i = 0; i < ret; i++) {
Chris Mason8ef97622007-03-26 10:15:30 -0400684 clear_radix_bit(pinned_radix, gang[i]);
Chris Masonbe744172007-05-06 10:15:01 -0400685 block_group = lookup_block_group(root->fs_info,
686 gang[i]);
687 if (block_group) {
688 WARN_ON(block_group->pinned == 0);
689 block_group->pinned--;
690 if (gang[i] < block_group->last_alloc)
691 block_group->last_alloc = gang[i];
Chris Masone37c9e62007-05-09 20:13:14 -0400692 if (gang[i] < block_group->last_prealloc)
693 block_group->last_prealloc = gang[i];
694 if (!block_group->data)
695 set_radix_bit(extent_radix, gang[i]);
Chris Masonbe744172007-05-06 10:15:01 -0400696 }
Chris Masonbe08c1b2007-05-03 09:06:49 -0400697 try_remove_page(btree_inode->i_mapping,
698 gang[i] << (PAGE_CACHE_SHIFT -
699 btree_inode->i_blkbits));
Chris Mason0579da42007-03-07 16:15:30 -0500700 }
Chris Masona28ec192007-03-06 20:08:01 -0500701 }
702 return 0;
703}
704
Chris Masone089f052007-03-16 16:20:31 -0400705static int finish_current_insert(struct btrfs_trans_handle *trans, struct
706 btrfs_root *extent_root)
Chris Mason037e6392007-03-07 11:50:24 -0500707{
Chris Masone2fa7222007-03-12 16:22:34 -0400708 struct btrfs_key ins;
Chris Mason234b63a2007-03-13 10:46:10 -0400709 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -0500710 int i;
711 int ret;
Chris Mason1261ec42007-03-20 20:35:03 -0400712 u64 super_blocks_used;
713 struct btrfs_fs_info *info = extent_root->fs_info;
Chris Mason037e6392007-03-07 11:50:24 -0500714
Chris Masoncf27e1e2007-03-13 09:49:06 -0400715 btrfs_set_extent_refs(&extent_item, 1);
Chris Mason037e6392007-03-07 11:50:24 -0500716 ins.offset = 1;
717 ins.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -0400718 btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
Chris Mason5d0c3e62007-04-23 17:01:05 -0400719 btrfs_set_extent_owner(&extent_item, extent_root->root_key.objectid);
Chris Mason037e6392007-03-07 11:50:24 -0500720
Chris Masonf2458e12007-04-25 15:52:25 -0400721 for (i = 0; i < extent_root->fs_info->extent_tree_insert_nr; i++) {
722 ins.objectid = extent_root->fs_info->extent_tree_insert[i];
Chris Mason1261ec42007-03-20 20:35:03 -0400723 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
724 btrfs_set_super_blocks_used(info->disk_super,
725 super_blocks_used + 1);
Chris Masone089f052007-03-16 16:20:31 -0400726 ret = btrfs_insert_item(trans, extent_root, &ins, &extent_item,
727 sizeof(extent_item));
Chris Mason037e6392007-03-07 11:50:24 -0500728 BUG_ON(ret);
729 }
Chris Masonf2458e12007-04-25 15:52:25 -0400730 extent_root->fs_info->extent_tree_insert_nr = 0;
731 extent_root->fs_info->extent_tree_prealloc_nr = 0;
Chris Mason037e6392007-03-07 11:50:24 -0500732 return 0;
733}
734
Chris Mason8ef97622007-03-26 10:15:30 -0400735static int pin_down_block(struct btrfs_root *root, u64 blocknr, int pending)
Chris Masone20d96d2007-03-22 12:13:20 -0400736{
737 int err;
Chris Mason78fae272007-03-25 11:35:08 -0400738 struct btrfs_header *header;
Chris Mason8ef97622007-03-26 10:15:30 -0400739 struct buffer_head *bh;
Chris Mason78fae272007-03-25 11:35:08 -0400740
Chris Masonf4b9aa82007-03-27 11:05:53 -0400741 if (!pending) {
Chris Masond98237b2007-03-28 13:57:48 -0400742 bh = btrfs_find_tree_block(root, blocknr);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400743 if (bh) {
744 if (buffer_uptodate(bh)) {
745 u64 transid =
746 root->fs_info->running_transaction->transid;
747 header = btrfs_buffer_header(bh);
748 if (btrfs_header_generation(header) ==
749 transid) {
750 btrfs_block_release(root, bh);
751 return 0;
752 }
Chris Masonf4b9aa82007-03-27 11:05:53 -0400753 }
Chris Masond6025572007-03-30 14:27:56 -0400754 btrfs_block_release(root, bh);
Chris Mason8ef97622007-03-26 10:15:30 -0400755 }
Chris Mason8ef97622007-03-26 10:15:30 -0400756 err = set_radix_bit(&root->fs_info->pinned_radix, blocknr);
Chris Masonbe744172007-05-06 10:15:01 -0400757 if (!err) {
758 struct btrfs_block_group_cache *cache;
759 cache = lookup_block_group(root->fs_info, blocknr);
760 if (cache)
761 cache->pinned++;
762 }
Chris Masonf4b9aa82007-03-27 11:05:53 -0400763 } else {
764 err = set_radix_bit(&root->fs_info->pending_del_radix, blocknr);
765 }
Chris Masonbe744172007-05-06 10:15:01 -0400766 BUG_ON(err < 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400767 return 0;
768}
769
Chris Masona28ec192007-03-06 20:08:01 -0500770/*
771 * remove an extent from the root, returns 0 on success
772 */
Chris Masone089f052007-03-16 16:20:31 -0400773static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masone37c9e62007-05-09 20:13:14 -0400774 *root, u64 blocknr, u64 num_blocks, int pin,
775 int mark_free)
Chris Masona28ec192007-03-06 20:08:01 -0500776{
Chris Mason5caf2a02007-04-02 11:20:42 -0400777 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -0400778 struct btrfs_key key;
Chris Mason1261ec42007-03-20 20:35:03 -0400779 struct btrfs_fs_info *info = root->fs_info;
780 struct btrfs_root *extent_root = info->extent_root;
Chris Masona28ec192007-03-06 20:08:01 -0500781 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400782 struct btrfs_extent_item *ei;
Chris Masone2fa7222007-03-12 16:22:34 -0400783 struct btrfs_key ins;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400784 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500785
Chris Masona28ec192007-03-06 20:08:01 -0500786 key.objectid = blocknr;
787 key.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -0400788 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masona28ec192007-03-06 20:08:01 -0500789 key.offset = num_blocks;
790
Chris Masonbe08c1b2007-05-03 09:06:49 -0400791 find_free_extent(trans, root, 0, 0, (u64)-1, &ins, 0);
Chris Mason5caf2a02007-04-02 11:20:42 -0400792 path = btrfs_alloc_path();
793 BUG_ON(!path);
794 btrfs_init_path(path);
Chris Mason5f26f772007-04-05 10:38:44 -0400795
Chris Mason5caf2a02007-04-02 11:20:42 -0400796 ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
Chris Masona28ec192007-03-06 20:08:01 -0500797 if (ret) {
Chris Mason2e635a22007-03-21 11:12:56 -0400798 printk("failed to find %Lu\n", key.objectid);
Chris Mason234b63a2007-03-13 10:46:10 -0400799 btrfs_print_tree(extent_root, extent_root->node);
Chris Mason2e635a22007-03-21 11:12:56 -0400800 printk("failed to find %Lu\n", key.objectid);
Chris Masona28ec192007-03-06 20:08:01 -0500801 BUG();
802 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400803 ei = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
Chris Mason123abc82007-03-14 14:14:43 -0400804 struct btrfs_extent_item);
Chris Masona28ec192007-03-06 20:08:01 -0500805 BUG_ON(ei->refs == 0);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400806 refs = btrfs_extent_refs(ei) - 1;
807 btrfs_set_extent_refs(ei, refs);
Chris Mason5caf2a02007-04-02 11:20:42 -0400808 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400809 if (refs == 0) {
Chris Mason1261ec42007-03-20 20:35:03 -0400810 u64 super_blocks_used;
Chris Mason78fae272007-03-25 11:35:08 -0400811
812 if (pin) {
Chris Mason8ef97622007-03-26 10:15:30 -0400813 ret = pin_down_block(root, blocknr, 0);
Chris Mason78fae272007-03-25 11:35:08 -0400814 BUG_ON(ret);
815 }
816
Chris Mason1261ec42007-03-20 20:35:03 -0400817 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
818 btrfs_set_super_blocks_used(info->disk_super,
819 super_blocks_used - num_blocks);
Chris Mason5caf2a02007-04-02 11:20:42 -0400820 ret = btrfs_del_item(trans, extent_root, path);
Chris Masona28ec192007-03-06 20:08:01 -0500821 if (ret)
822 BUG();
Chris Masone37c9e62007-05-09 20:13:14 -0400823 ret = update_block_group(trans, root, blocknr, num_blocks, 0,
Chris Mason1e2677e2007-05-29 16:52:18 -0400824 mark_free, 0);
Chris Mason9078a3e2007-04-26 16:46:15 -0400825 BUG_ON(ret);
Chris Masona28ec192007-03-06 20:08:01 -0500826 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400827 btrfs_free_path(path);
Chris Masone089f052007-03-16 16:20:31 -0400828 finish_current_insert(trans, extent_root);
Chris Masona28ec192007-03-06 20:08:01 -0500829 return ret;
830}
831
832/*
Chris Masonfec577f2007-02-26 10:40:21 -0500833 * find all the blocks marked as pending in the radix tree and remove
834 * them from the extent map
835 */
Chris Masone089f052007-03-16 16:20:31 -0400836static int del_pending_extents(struct btrfs_trans_handle *trans, struct
837 btrfs_root *extent_root)
Chris Masonfec577f2007-02-26 10:40:21 -0500838{
839 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -0400840 int wret;
841 int err = 0;
Chris Mason8ef97622007-03-26 10:15:30 -0400842 unsigned long gang[4];
Chris Masonfec577f2007-02-26 10:40:21 -0500843 int i;
Chris Mason8ef97622007-03-26 10:15:30 -0400844 struct radix_tree_root *pending_radix;
845 struct radix_tree_root *pinned_radix;
Chris Masonbe744172007-05-06 10:15:01 -0400846 struct btrfs_block_group_cache *cache;
Chris Mason8ef97622007-03-26 10:15:30 -0400847
848 pending_radix = &extent_root->fs_info->pending_del_radix;
849 pinned_radix = &extent_root->fs_info->pinned_radix;
Chris Masonfec577f2007-02-26 10:40:21 -0500850
851 while(1) {
Chris Masone37c9e62007-05-09 20:13:14 -0400852 ret = find_first_radix_bit(pending_radix, gang, 0,
Chris Mason8ef97622007-03-26 10:15:30 -0400853 ARRAY_SIZE(gang));
Chris Masonfec577f2007-02-26 10:40:21 -0500854 if (!ret)
855 break;
856 for (i = 0; i < ret; i++) {
Chris Mason8ef97622007-03-26 10:15:30 -0400857 wret = set_radix_bit(pinned_radix, gang[i]);
Chris Masonbe744172007-05-06 10:15:01 -0400858 if (wret == 0) {
859 cache = lookup_block_group(extent_root->fs_info,
860 gang[i]);
861 if (cache)
862 cache->pinned++;
863 }
864 if (wret < 0) {
865 printk(KERN_CRIT "set_radix_bit, err %d\n",
866 wret);
867 BUG_ON(wret < 0);
868 }
Chris Mason8ef97622007-03-26 10:15:30 -0400869 wret = clear_radix_bit(pending_radix, gang[i]);
870 BUG_ON(wret);
Chris Masond5719762007-03-23 10:01:08 -0400871 wret = __free_extent(trans, extent_root,
Chris Masone37c9e62007-05-09 20:13:14 -0400872 gang[i], 1, 0, 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400873 if (wret)
874 err = wret;
Chris Masonfec577f2007-02-26 10:40:21 -0500875 }
876 }
Chris Masone20d96d2007-03-22 12:13:20 -0400877 return err;
Chris Masonfec577f2007-02-26 10:40:21 -0500878}
879
880/*
881 * remove an extent from the root, returns 0 on success
882 */
Chris Masone089f052007-03-16 16:20:31 -0400883int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
884 *root, u64 blocknr, u64 num_blocks, int pin)
Chris Masonfec577f2007-02-26 10:40:21 -0500885{
Chris Mason9f5fae22007-03-20 14:38:32 -0400886 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Masonfec577f2007-02-26 10:40:21 -0500887 int pending_ret;
888 int ret;
Chris Masona28ec192007-03-06 20:08:01 -0500889
890 if (root == extent_root) {
Chris Mason8ef97622007-03-26 10:15:30 -0400891 pin_down_block(root, blocknr, 1);
Chris Masona28ec192007-03-06 20:08:01 -0500892 return 0;
893 }
Chris Masone37c9e62007-05-09 20:13:14 -0400894 ret = __free_extent(trans, root, blocknr, num_blocks, pin, pin == 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400895 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
Chris Masonfec577f2007-02-26 10:40:21 -0500896 return ret ? ret : pending_ret;
897}
898
899/*
900 * walks the btree of allocated extents and find a hole of a given size.
901 * The key ins is changed to record the hole:
902 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -0400903 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -0500904 * ins->offset == number of blocks
905 * Any available blocks before search_start are skipped.
906 */
Chris Masone089f052007-03-16 16:20:31 -0400907static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
908 *orig_root, u64 num_blocks, u64 search_start, u64
Chris Masonbe08c1b2007-05-03 09:06:49 -0400909 search_end, struct btrfs_key *ins, int data)
Chris Masonfec577f2007-02-26 10:40:21 -0500910{
Chris Mason5caf2a02007-04-02 11:20:42 -0400911 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -0400912 struct btrfs_key key;
Chris Masonfec577f2007-02-26 10:40:21 -0500913 int ret;
914 u64 hole_size = 0;
915 int slot = 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400916 u64 last_block = 0;
Chris Mason037e6392007-03-07 11:50:24 -0500917 u64 test_block;
Chris Masonbe744172007-05-06 10:15:01 -0400918 u64 orig_search_start = search_start;
Chris Masonfec577f2007-02-26 10:40:21 -0500919 int start_found;
Chris Mason234b63a2007-03-13 10:46:10 -0400920 struct btrfs_leaf *l;
Chris Mason9f5fae22007-03-20 14:38:32 -0400921 struct btrfs_root * root = orig_root->fs_info->extent_root;
Chris Masonf2458e12007-04-25 15:52:25 -0400922 struct btrfs_fs_info *info = root->fs_info;
Chris Mason0579da42007-03-07 16:15:30 -0500923 int total_needed = num_blocks;
Chris Masonf2458e12007-04-25 15:52:25 -0400924 int total_found = 0;
925 int fill_prealloc = 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400926 int level;
Chris Masonbe08c1b2007-05-03 09:06:49 -0400927 struct btrfs_block_group_cache *block_group;
Chris Masonbe744172007-05-06 10:15:01 -0400928 int full_scan = 0;
Chris Masonde428b62007-05-18 13:28:27 -0400929 u64 limit;
Chris Masonfec577f2007-02-26 10:40:21 -0500930
Chris Masonb1a4d962007-04-04 15:27:52 -0400931 path = btrfs_alloc_path();
932 ins->flags = 0;
933 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
934
Chris Masone20d96d2007-03-22 12:13:20 -0400935 level = btrfs_header_level(btrfs_buffer_header(root->node));
Chris Masonf2458e12007-04-25 15:52:25 -0400936 if (num_blocks == 0) {
937 fill_prealloc = 1;
938 num_blocks = 1;
Chris Mason308535a2007-04-28 15:17:08 -0400939 total_needed = (min(level + 1, BTRFS_MAX_LEVEL) + 2) * 3;
Chris Masonf2458e12007-04-25 15:52:25 -0400940 }
Chris Mason3e1ad542007-05-07 20:03:49 -0400941 if (search_end == (u64)-1)
942 search_end = btrfs_super_total_blocks(info->disk_super);
Chris Masonbe744172007-05-06 10:15:01 -0400943 if (search_start) {
944 block_group = lookup_block_group(info, search_start);
945 block_group = btrfs_find_block_group(root, block_group,
Chris Masonde428b62007-05-18 13:28:27 -0400946 search_start, data, 1);
Chris Masonbe744172007-05-06 10:15:01 -0400947 } else {
948 block_group = btrfs_find_block_group(root,
949 trans->block_group, 0,
Chris Masonde428b62007-05-18 13:28:27 -0400950 data, 1);
Chris Masonbe744172007-05-06 10:15:01 -0400951 }
952
953check_failed:
Chris Mason1e2677e2007-05-29 16:52:18 -0400954 if (!block_group->data)
Chris Masone37c9e62007-05-09 20:13:14 -0400955 search_start = find_search_start(root, &block_group,
956 search_start, total_needed);
957 else
958 search_start = max(block_group->last_alloc, search_start);
959
Chris Mason5caf2a02007-04-02 11:20:42 -0400960 btrfs_init_path(path);
Chris Masonfec577f2007-02-26 10:40:21 -0500961 ins->objectid = search_start;
962 ins->offset = 0;
Chris Masonfec577f2007-02-26 10:40:21 -0500963 start_found = 0;
Chris Masone37c9e62007-05-09 20:13:14 -0400964
Chris Mason5caf2a02007-04-02 11:20:42 -0400965 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
Chris Mason0f70abe2007-02-28 16:46:22 -0500966 if (ret < 0)
967 goto error;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500968
Chris Masone37c9e62007-05-09 20:13:14 -0400969 if (path->slots[0] > 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400970 path->slots[0]--;
Chris Masone37c9e62007-05-09 20:13:14 -0400971 }
972
973 l = btrfs_buffer_leaf(path->nodes[0]);
974 btrfs_disk_key_to_cpu(&key, &l->items[path->slots[0]].key);
975 /*
976 * a rare case, go back one key if we hit a block group item
977 * instead of an extent item
978 */
979 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY &&
980 key.objectid + key.offset >= search_start) {
981 ins->objectid = key.objectid;
982 ins->offset = key.offset - 1;
983 btrfs_release_path(root, path);
984 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
985 if (ret < 0)
986 goto error;
987
988 if (path->slots[0] > 0) {
989 path->slots[0]--;
990 }
991 }
Chris Mason0579da42007-03-07 16:15:30 -0500992
Chris Masonfec577f2007-02-26 10:40:21 -0500993 while (1) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400994 l = btrfs_buffer_leaf(path->nodes[0]);
995 slot = path->slots[0];
Chris Mason7518a232007-03-12 12:01:18 -0400996 if (slot >= btrfs_header_nritems(&l->header)) {
Chris Masonf2458e12007-04-25 15:52:25 -0400997 if (fill_prealloc) {
998 info->extent_tree_prealloc_nr = 0;
999 total_found = 0;
1000 }
Chris Masonde428b62007-05-18 13:28:27 -04001001 if (start_found)
1002 limit = last_block +
1003 block_group->key.offset / 2;
1004 else
1005 limit = search_start +
1006 block_group->key.offset / 2;
Chris Mason5caf2a02007-04-02 11:20:42 -04001007 ret = btrfs_next_leaf(root, path);
Chris Masonfec577f2007-02-26 10:40:21 -05001008 if (ret == 0)
1009 continue;
Chris Mason0f70abe2007-02-28 16:46:22 -05001010 if (ret < 0)
1011 goto error;
Chris Masonfec577f2007-02-26 10:40:21 -05001012 if (!start_found) {
1013 ins->objectid = search_start;
Chris Mason3e1ad542007-05-07 20:03:49 -04001014 ins->offset = search_end - search_start;
Chris Masonfec577f2007-02-26 10:40:21 -05001015 start_found = 1;
1016 goto check_pending;
1017 }
1018 ins->objectid = last_block > search_start ?
1019 last_block : search_start;
Chris Mason3e1ad542007-05-07 20:03:49 -04001020 ins->offset = search_end - ins->objectid;
Chris Masonfec577f2007-02-26 10:40:21 -05001021 goto check_pending;
1022 }
Chris Masone37c9e62007-05-09 20:13:14 -04001023
Chris Masone2fa7222007-03-12 16:22:34 -04001024 btrfs_disk_key_to_cpu(&key, &l->items[slot].key);
Chris Masone37c9e62007-05-09 20:13:14 -04001025 if (key.objectid >= search_start && key.objectid > last_block &&
1026 start_found) {
1027 if (last_block < search_start)
1028 last_block = search_start;
1029 hole_size = key.objectid - last_block;
1030 if (hole_size >= num_blocks) {
1031 ins->objectid = last_block;
1032 ins->offset = hole_size;
1033 goto check_pending;
Chris Mason0579da42007-03-07 16:15:30 -05001034 }
Chris Masonfec577f2007-02-26 10:40:21 -05001035 }
Chris Masone37c9e62007-05-09 20:13:14 -04001036
1037 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY)
1038 goto next;
1039
Chris Mason0579da42007-03-07 16:15:30 -05001040 start_found = 1;
Chris Masone2fa7222007-03-12 16:22:34 -04001041 last_block = key.objectid + key.offset;
Chris Masonbe744172007-05-06 10:15:01 -04001042 if (last_block >= block_group->key.objectid +
1043 block_group->key.offset) {
1044 btrfs_release_path(root, path);
1045 search_start = block_group->key.objectid +
1046 block_group->key.offset * 2;
1047 goto new_group;
1048 }
Chris Mason9078a3e2007-04-26 16:46:15 -04001049next:
Chris Mason5caf2a02007-04-02 11:20:42 -04001050 path->slots[0]++;
Chris Masonde428b62007-05-18 13:28:27 -04001051 cond_resched();
Chris Masonfec577f2007-02-26 10:40:21 -05001052 }
1053 // FIXME -ENOSPC
1054check_pending:
1055 /* we have to make sure we didn't find an extent that has already
1056 * been allocated by the map tree or the original allocation
1057 */
Chris Mason5caf2a02007-04-02 11:20:42 -04001058 btrfs_release_path(root, path);
Chris Masonfec577f2007-02-26 10:40:21 -05001059 BUG_ON(ins->objectid < search_start);
Chris Masone37c9e62007-05-09 20:13:14 -04001060
Chris Mason3e1ad542007-05-07 20:03:49 -04001061 if (ins->objectid + num_blocks >= search_end) {
Chris Masonbe744172007-05-06 10:15:01 -04001062 if (full_scan)
Chris Mason06a2f9f2007-04-28 08:48:10 -04001063 return -ENOSPC;
Chris Masonbe744172007-05-06 10:15:01 -04001064 search_start = orig_search_start;
1065 full_scan = 1;
1066 goto new_group;
Chris Mason06a2f9f2007-04-28 08:48:10 -04001067 }
Chris Mason037e6392007-03-07 11:50:24 -05001068 for (test_block = ins->objectid;
Chris Masonf2458e12007-04-25 15:52:25 -04001069 test_block < ins->objectid + num_blocks; test_block++) {
1070 if (test_radix_bit(&info->pinned_radix, test_block)) {
Chris Mason037e6392007-03-07 11:50:24 -05001071 search_start = test_block + 1;
Chris Masonbe744172007-05-06 10:15:01 -04001072 goto new_group;
Chris Masonfec577f2007-02-26 10:40:21 -05001073 }
1074 }
Chris Masonf2458e12007-04-25 15:52:25 -04001075 if (!fill_prealloc && info->extent_tree_insert_nr) {
1076 u64 last =
1077 info->extent_tree_insert[info->extent_tree_insert_nr - 1];
1078 if (ins->objectid + num_blocks >
1079 info->extent_tree_insert[0] &&
1080 ins->objectid <= last) {
1081 search_start = last + 1;
Chris Masone37c9e62007-05-09 20:13:14 -04001082 WARN_ON(!full_scan);
Chris Masonbe744172007-05-06 10:15:01 -04001083 goto new_group;
Chris Masonf2458e12007-04-25 15:52:25 -04001084 }
1085 }
1086 if (!fill_prealloc && info->extent_tree_prealloc_nr) {
1087 u64 first =
1088 info->extent_tree_prealloc[info->extent_tree_prealloc_nr - 1];
1089 if (ins->objectid + num_blocks > first &&
1090 ins->objectid <= info->extent_tree_prealloc[0]) {
1091 search_start = info->extent_tree_prealloc[0] + 1;
Chris Masone37c9e62007-05-09 20:13:14 -04001092 WARN_ON(!full_scan);
Chris Masonbe744172007-05-06 10:15:01 -04001093 goto new_group;
Chris Masonf2458e12007-04-25 15:52:25 -04001094 }
1095 }
1096 if (fill_prealloc) {
1097 int nr;
1098 test_block = ins->objectid;
Chris Masone37c9e62007-05-09 20:13:14 -04001099 if (test_block - info->extent_tree_prealloc[total_needed - 1] >=
1100 leaf_range(root)) {
1101 total_found = 0;
1102 info->extent_tree_prealloc_nr = total_found;
1103 }
Chris Masonf2458e12007-04-25 15:52:25 -04001104 while(test_block < ins->objectid + ins->offset &&
1105 total_found < total_needed) {
1106 nr = total_needed - total_found - 1;
1107 BUG_ON(nr < 0);
Chris Masoncd1bc462007-04-27 10:08:34 -04001108 info->extent_tree_prealloc[nr] = test_block;
Chris Masonf2458e12007-04-25 15:52:25 -04001109 total_found++;
1110 test_block++;
1111 }
1112 if (total_found < total_needed) {
1113 search_start = test_block;
Chris Masonbe744172007-05-06 10:15:01 -04001114 goto new_group;
Chris Masonf2458e12007-04-25 15:52:25 -04001115 }
Chris Masoncd1bc462007-04-27 10:08:34 -04001116 info->extent_tree_prealloc_nr = total_found;
Chris Masonf2458e12007-04-25 15:52:25 -04001117 }
Chris Masone37c9e62007-05-09 20:13:14 -04001118 if (!data) {
1119 block_group = lookup_block_group(info, ins->objectid);
1120 if (block_group) {
1121 if (fill_prealloc)
1122 block_group->last_prealloc =
1123 info->extent_tree_prealloc[total_needed-1];
1124 else
1125 trans->block_group = block_group;
1126 }
Chris Masoncd1bc462007-04-27 10:08:34 -04001127 }
Chris Mason037e6392007-03-07 11:50:24 -05001128 ins->offset = num_blocks;
Chris Mason5caf2a02007-04-02 11:20:42 -04001129 btrfs_free_path(path);
Chris Masonfec577f2007-02-26 10:40:21 -05001130 return 0;
Chris Masonbe744172007-05-06 10:15:01 -04001131
1132new_group:
Chris Mason3e1ad542007-05-07 20:03:49 -04001133 if (search_start + num_blocks >= search_end) {
Chris Masonbe744172007-05-06 10:15:01 -04001134 search_start = orig_search_start;
Chris Masone37c9e62007-05-09 20:13:14 -04001135printk("doing full scan!\n");
Chris Masonbe744172007-05-06 10:15:01 -04001136 full_scan = 1;
1137 }
1138 block_group = lookup_block_group(info, search_start);
1139 if (!full_scan)
1140 block_group = btrfs_find_block_group(root, block_group,
Chris Masonde428b62007-05-18 13:28:27 -04001141 search_start, data, 0);
1142 cond_resched();
Chris Masonbe744172007-05-06 10:15:01 -04001143 goto check_failed;
1144
Chris Mason0f70abe2007-02-28 16:46:22 -05001145error:
Chris Mason5caf2a02007-04-02 11:20:42 -04001146 btrfs_release_path(root, path);
1147 btrfs_free_path(path);
Chris Mason0f70abe2007-02-28 16:46:22 -05001148 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05001149}
Chris Masonfec577f2007-02-26 10:40:21 -05001150/*
Chris Masonfec577f2007-02-26 10:40:21 -05001151 * finds a free extent and does all the dirty work required for allocation
1152 * returns the key for the extent through ins, and a tree buffer for
1153 * the first block of the extent through buf.
1154 *
1155 * returns 0 if everything worked, non-zero otherwise.
1156 */
Chris Mason4d775672007-04-20 20:23:12 -04001157int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
1158 struct btrfs_root *root, u64 owner,
Chris Masonc62a1922007-04-24 12:07:39 -04001159 u64 num_blocks, u64 search_start,
Chris Masonbe08c1b2007-05-03 09:06:49 -04001160 u64 search_end, struct btrfs_key *ins, int data)
Chris Masonfec577f2007-02-26 10:40:21 -05001161{
1162 int ret;
1163 int pending_ret;
Chris Mason1261ec42007-03-20 20:35:03 -04001164 u64 super_blocks_used;
1165 struct btrfs_fs_info *info = root->fs_info;
1166 struct btrfs_root *extent_root = info->extent_root;
Chris Mason234b63a2007-03-13 10:46:10 -04001167 struct btrfs_extent_item extent_item;
Chris Masonf2458e12007-04-25 15:52:25 -04001168 struct btrfs_key prealloc_key;
Chris Mason037e6392007-03-07 11:50:24 -05001169
Chris Masoncf27e1e2007-03-13 09:49:06 -04001170 btrfs_set_extent_refs(&extent_item, 1);
Chris Mason4d775672007-04-20 20:23:12 -04001171 btrfs_set_extent_owner(&extent_item, owner);
Chris Masonfec577f2007-02-26 10:40:21 -05001172
Chris Mason037e6392007-03-07 11:50:24 -05001173 if (root == extent_root) {
Chris Masonf2458e12007-04-25 15:52:25 -04001174 int nr;
1175 BUG_ON(info->extent_tree_prealloc_nr == 0);
Chris Mason037e6392007-03-07 11:50:24 -05001176 BUG_ON(num_blocks != 1);
Chris Mason037e6392007-03-07 11:50:24 -05001177 ins->offset = 1;
Chris Masonf2458e12007-04-25 15:52:25 -04001178 info->extent_tree_prealloc_nr--;
1179 nr = info->extent_tree_prealloc_nr;
1180 ins->objectid = info->extent_tree_prealloc[nr];
1181 info->extent_tree_insert[info->extent_tree_insert_nr++] =
1182 ins->objectid;
Chris Mason9078a3e2007-04-26 16:46:15 -04001183 ret = update_block_group(trans, root,
Chris Mason1e2677e2007-05-29 16:52:18 -04001184 ins->objectid, ins->offset, 1, 0, 0);
Chris Mason9078a3e2007-04-26 16:46:15 -04001185 BUG_ON(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05001186 return 0;
1187 }
Chris Masone37c9e62007-05-09 20:13:14 -04001188
1189 /*
1190 * if we're doing a data allocation, preallocate room in the
1191 * extent tree first. This way the extent tree blocks end up
1192 * in the correct block group.
1193 */
1194 if (data) {
Chris Masonde428b62007-05-18 13:28:27 -04001195 ret = find_free_extent(trans, root, 0, 0,
Chris Masone37c9e62007-05-09 20:13:14 -04001196 search_end, &prealloc_key, 0);
1197 if (ret) {
1198 return ret;
1199 }
1200 if (prealloc_key.objectid + prealloc_key.offset >= search_end) {
1201 int nr = info->extent_tree_prealloc_nr;
1202 search_end = info->extent_tree_prealloc[nr - 1] - 1;
1203 } else {
1204 search_start = info->extent_tree_prealloc[0] + 1;
1205 }
1206 }
Chris Masonf2458e12007-04-25 15:52:25 -04001207 /* do the real allocation */
Chris Masone089f052007-03-16 16:20:31 -04001208 ret = find_free_extent(trans, root, num_blocks, search_start,
Chris Masonbe08c1b2007-05-03 09:06:49 -04001209 search_end, ins, data);
Chris Masone37c9e62007-05-09 20:13:14 -04001210 if (ret) {
Chris Mason037e6392007-03-07 11:50:24 -05001211 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -04001212 }
Chris Masonfec577f2007-02-26 10:40:21 -05001213
Chris Masone37c9e62007-05-09 20:13:14 -04001214 /*
1215 * if we're doing a metadata allocation, preallocate space in the
1216 * extent tree second. This way, we don't create a tiny hole
1217 * in the allocation map between any unused preallocation blocks
1218 * and the metadata block we're actually allocating. On disk,
1219 * it'll go:
1220 * [block we've allocated], [used prealloc 1], [ unused prealloc ]
1221 * The unused prealloc will get reused the next time around.
1222 */
1223 if (!data) {
1224 if (ins->objectid + ins->offset >= search_end)
1225 search_end = ins->objectid - 1;
1226 else
1227 search_start = ins->objectid + ins->offset;
Chris Mason3e1ad542007-05-07 20:03:49 -04001228
Chris Masone37c9e62007-05-09 20:13:14 -04001229 ret = find_free_extent(trans, root, 0, search_start,
1230 search_end, &prealloc_key, 0);
1231 if (ret) {
1232 return ret;
1233 }
1234 }
Chris Masonf2458e12007-04-25 15:52:25 -04001235
Chris Mason1261ec42007-03-20 20:35:03 -04001236 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
1237 btrfs_set_super_blocks_used(info->disk_super, super_blocks_used +
1238 num_blocks);
Chris Masone089f052007-03-16 16:20:31 -04001239 ret = btrfs_insert_item(trans, extent_root, ins, &extent_item,
1240 sizeof(extent_item));
Chris Mason037e6392007-03-07 11:50:24 -05001241
Chris Masone089f052007-03-16 16:20:31 -04001242 finish_current_insert(trans, extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -04001243 pending_ret = del_pending_extents(trans, extent_root);
Chris Masone37c9e62007-05-09 20:13:14 -04001244 if (ret) {
Chris Mason037e6392007-03-07 11:50:24 -05001245 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -04001246 }
1247 if (pending_ret) {
Chris Mason037e6392007-03-07 11:50:24 -05001248 return pending_ret;
Chris Masone37c9e62007-05-09 20:13:14 -04001249 }
Chris Mason1e2677e2007-05-29 16:52:18 -04001250 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0,
1251 data);
Chris Mason037e6392007-03-07 11:50:24 -05001252 return 0;
Chris Masonfec577f2007-02-26 10:40:21 -05001253}
1254
1255/*
1256 * helper function to allocate a block for a given tree
1257 * returns the tree buffer or NULL.
1258 */
Chris Masone20d96d2007-03-22 12:13:20 -04001259struct buffer_head *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Chris Mason31f3c992007-04-30 15:25:45 -04001260 struct btrfs_root *root, u64 hint)
Chris Masonfec577f2007-02-26 10:40:21 -05001261{
Chris Masone2fa7222007-03-12 16:22:34 -04001262 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -05001263 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -04001264 struct buffer_head *buf;
Chris Masonfec577f2007-02-26 10:40:21 -05001265
Chris Mason4d775672007-04-20 20:23:12 -04001266 ret = btrfs_alloc_extent(trans, root, root->root_key.objectid,
Chris Masonde428b62007-05-18 13:28:27 -04001267 1, hint, (unsigned long)-1, &ins, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05001268 if (ret) {
1269 BUG();
1270 return NULL;
1271 }
Chris Mason9078a3e2007-04-26 16:46:15 -04001272 BUG_ON(ret);
Chris Masond98237b2007-03-28 13:57:48 -04001273 buf = btrfs_find_create_tree_block(root, ins.objectid);
Chris Masondf2ce342007-03-23 11:00:45 -04001274 set_buffer_uptodate(buf);
Chris Mason090d1872007-05-01 08:53:32 -04001275 set_buffer_checked(buf);
Chris Mason7c4452b2007-04-28 09:29:35 -04001276 set_radix_bit(&trans->transaction->dirty_pages, buf->b_page->index);
Chris Masonfec577f2007-02-26 10:40:21 -05001277 return buf;
1278}
Chris Masona28ec192007-03-06 20:08:01 -05001279
Chris Mason6407bf62007-03-27 06:33:00 -04001280static int drop_leaf_ref(struct btrfs_trans_handle *trans,
1281 struct btrfs_root *root, struct buffer_head *cur)
1282{
1283 struct btrfs_disk_key *key;
1284 struct btrfs_leaf *leaf;
1285 struct btrfs_file_extent_item *fi;
1286 int i;
1287 int nritems;
1288 int ret;
1289
1290 BUG_ON(!btrfs_is_leaf(btrfs_buffer_node(cur)));
1291 leaf = btrfs_buffer_leaf(cur);
1292 nritems = btrfs_header_nritems(&leaf->header);
1293 for (i = 0; i < nritems; i++) {
Chris Mason3a686372007-05-24 13:35:57 -04001294 u64 disk_blocknr;
Chris Mason6407bf62007-03-27 06:33:00 -04001295 key = &leaf->items[i].key;
1296 if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY)
1297 continue;
1298 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
Chris Mason236454d2007-04-19 13:37:44 -04001299 if (btrfs_file_extent_type(fi) == BTRFS_FILE_EXTENT_INLINE)
1300 continue;
Chris Mason6407bf62007-03-27 06:33:00 -04001301 /*
1302 * FIXME make sure to insert a trans record that
1303 * repeats the snapshot del on crash
1304 */
Chris Mason3a686372007-05-24 13:35:57 -04001305 disk_blocknr = btrfs_file_extent_disk_blocknr(fi);
1306 if (disk_blocknr == 0)
1307 continue;
1308 ret = btrfs_free_extent(trans, root, disk_blocknr,
Chris Mason6407bf62007-03-27 06:33:00 -04001309 btrfs_file_extent_disk_num_blocks(fi),
1310 0);
1311 BUG_ON(ret);
1312 }
1313 return 0;
1314}
1315
Chris Mason9aca1d52007-03-13 11:09:37 -04001316/*
1317 * helper function for drop_snapshot, this walks down the tree dropping ref
1318 * counts as it goes.
1319 */
Chris Masone089f052007-03-16 16:20:31 -04001320static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
1321 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05001322{
Chris Masone20d96d2007-03-22 12:13:20 -04001323 struct buffer_head *next;
1324 struct buffer_head *cur;
Chris Mason20524f02007-03-10 06:35:47 -05001325 u64 blocknr;
1326 int ret;
1327 u32 refs;
1328
Chris Mason5caf2a02007-04-02 11:20:42 -04001329 WARN_ON(*level < 0);
1330 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Masonb18c6682007-04-17 13:26:50 -04001331 ret = lookup_extent_ref(trans, root, bh_blocknr(path->nodes[*level]),
Chris Mason6407bf62007-03-27 06:33:00 -04001332 1, &refs);
Chris Mason20524f02007-03-10 06:35:47 -05001333 BUG_ON(ret);
1334 if (refs > 1)
1335 goto out;
Chris Mason9aca1d52007-03-13 11:09:37 -04001336 /*
1337 * walk down to the last node level and free all the leaves
1338 */
Chris Mason6407bf62007-03-27 06:33:00 -04001339 while(*level >= 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001340 WARN_ON(*level < 0);
1341 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason20524f02007-03-10 06:35:47 -05001342 cur = path->nodes[*level];
Chris Mason2c90e5d2007-04-02 10:50:19 -04001343 if (btrfs_header_level(btrfs_buffer_header(cur)) != *level)
1344 WARN_ON(1);
Chris Mason7518a232007-03-12 12:01:18 -04001345 if (path->slots[*level] >=
Chris Masone20d96d2007-03-22 12:13:20 -04001346 btrfs_header_nritems(btrfs_buffer_header(cur)))
Chris Mason20524f02007-03-10 06:35:47 -05001347 break;
Chris Mason6407bf62007-03-27 06:33:00 -04001348 if (*level == 0) {
1349 ret = drop_leaf_ref(trans, root, cur);
1350 BUG_ON(ret);
1351 break;
1352 }
Chris Masone20d96d2007-03-22 12:13:20 -04001353 blocknr = btrfs_node_blockptr(btrfs_buffer_node(cur),
1354 path->slots[*level]);
Chris Masonb18c6682007-04-17 13:26:50 -04001355 ret = lookup_extent_ref(trans, root, blocknr, 1, &refs);
Chris Mason6407bf62007-03-27 06:33:00 -04001356 BUG_ON(ret);
1357 if (refs != 1) {
Chris Mason20524f02007-03-10 06:35:47 -05001358 path->slots[*level]++;
Chris Masone089f052007-03-16 16:20:31 -04001359 ret = btrfs_free_extent(trans, root, blocknr, 1, 1);
Chris Mason20524f02007-03-10 06:35:47 -05001360 BUG_ON(ret);
1361 continue;
1362 }
Chris Mason20524f02007-03-10 06:35:47 -05001363 next = read_tree_block(root, blocknr);
Chris Mason5caf2a02007-04-02 11:20:42 -04001364 WARN_ON(*level <= 0);
Chris Mason83e15a22007-03-12 09:03:27 -04001365 if (path->nodes[*level-1])
Chris Mason234b63a2007-03-13 10:46:10 -04001366 btrfs_block_release(root, path->nodes[*level-1]);
Chris Mason20524f02007-03-10 06:35:47 -05001367 path->nodes[*level-1] = next;
Chris Masone20d96d2007-03-22 12:13:20 -04001368 *level = btrfs_header_level(btrfs_buffer_header(next));
Chris Mason20524f02007-03-10 06:35:47 -05001369 path->slots[*level] = 0;
1370 }
1371out:
Chris Mason5caf2a02007-04-02 11:20:42 -04001372 WARN_ON(*level < 0);
1373 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason6407bf62007-03-27 06:33:00 -04001374 ret = btrfs_free_extent(trans, root,
Chris Mason7eccb902007-04-11 15:53:25 -04001375 bh_blocknr(path->nodes[*level]), 1, 1);
Chris Mason234b63a2007-03-13 10:46:10 -04001376 btrfs_block_release(root, path->nodes[*level]);
Chris Mason20524f02007-03-10 06:35:47 -05001377 path->nodes[*level] = NULL;
1378 *level += 1;
1379 BUG_ON(ret);
1380 return 0;
1381}
1382
Chris Mason9aca1d52007-03-13 11:09:37 -04001383/*
1384 * helper for dropping snapshots. This walks back up the tree in the path
1385 * to find the first node higher up where we haven't yet gone through
1386 * all the slots
1387 */
Chris Masone089f052007-03-16 16:20:31 -04001388static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root
1389 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05001390{
1391 int i;
1392 int slot;
1393 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -04001394 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Mason20524f02007-03-10 06:35:47 -05001395 slot = path->slots[i];
Chris Masone20d96d2007-03-22 12:13:20 -04001396 if (slot < btrfs_header_nritems(
1397 btrfs_buffer_header(path->nodes[i])) - 1) {
Chris Mason20524f02007-03-10 06:35:47 -05001398 path->slots[i]++;
1399 *level = i;
1400 return 0;
1401 } else {
Chris Masone089f052007-03-16 16:20:31 -04001402 ret = btrfs_free_extent(trans, root,
Chris Mason7eccb902007-04-11 15:53:25 -04001403 bh_blocknr(path->nodes[*level]),
Chris Masone089f052007-03-16 16:20:31 -04001404 1, 1);
Chris Mason6407bf62007-03-27 06:33:00 -04001405 BUG_ON(ret);
Chris Mason234b63a2007-03-13 10:46:10 -04001406 btrfs_block_release(root, path->nodes[*level]);
Chris Mason83e15a22007-03-12 09:03:27 -04001407 path->nodes[*level] = NULL;
Chris Mason20524f02007-03-10 06:35:47 -05001408 *level = i + 1;
Chris Mason20524f02007-03-10 06:35:47 -05001409 }
1410 }
1411 return 1;
1412}
1413
Chris Mason9aca1d52007-03-13 11:09:37 -04001414/*
1415 * drop the reference count on the tree rooted at 'snap'. This traverses
1416 * the tree freeing any blocks that have a ref count of zero after being
1417 * decremented.
1418 */
Chris Masone089f052007-03-16 16:20:31 -04001419int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masone20d96d2007-03-22 12:13:20 -04001420 *root, struct buffer_head *snap)
Chris Mason20524f02007-03-10 06:35:47 -05001421{
Chris Mason3768f362007-03-13 16:47:54 -04001422 int ret = 0;
Chris Mason9aca1d52007-03-13 11:09:37 -04001423 int wret;
Chris Mason20524f02007-03-10 06:35:47 -05001424 int level;
Chris Mason5caf2a02007-04-02 11:20:42 -04001425 struct btrfs_path *path;
Chris Mason20524f02007-03-10 06:35:47 -05001426 int i;
1427 int orig_level;
1428
Chris Mason5caf2a02007-04-02 11:20:42 -04001429 path = btrfs_alloc_path();
1430 BUG_ON(!path);
1431 btrfs_init_path(path);
Chris Mason20524f02007-03-10 06:35:47 -05001432
Chris Masone20d96d2007-03-22 12:13:20 -04001433 level = btrfs_header_level(btrfs_buffer_header(snap));
Chris Mason20524f02007-03-10 06:35:47 -05001434 orig_level = level;
Chris Mason5caf2a02007-04-02 11:20:42 -04001435 path->nodes[level] = snap;
1436 path->slots[level] = 0;
Chris Mason20524f02007-03-10 06:35:47 -05001437 while(1) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001438 wret = walk_down_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04001439 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05001440 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04001441 if (wret < 0)
1442 ret = wret;
1443
Chris Mason5caf2a02007-04-02 11:20:42 -04001444 wret = walk_up_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04001445 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05001446 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04001447 if (wret < 0)
1448 ret = wret;
Chris Mason35b7e472007-05-02 15:53:43 -04001449 btrfs_btree_balance_dirty(root);
Chris Mason20524f02007-03-10 06:35:47 -05001450 }
Chris Mason83e15a22007-03-12 09:03:27 -04001451 for (i = 0; i <= orig_level; i++) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001452 if (path->nodes[i]) {
1453 btrfs_block_release(root, path->nodes[i]);
Chris Mason83e15a22007-03-12 09:03:27 -04001454 }
Chris Mason20524f02007-03-10 06:35:47 -05001455 }
Chris Mason5caf2a02007-04-02 11:20:42 -04001456 btrfs_free_path(path);
Chris Mason9aca1d52007-03-13 11:09:37 -04001457 return ret;
Chris Mason20524f02007-03-10 06:35:47 -05001458}
Chris Mason9078a3e2007-04-26 16:46:15 -04001459
Chris Masonbe744172007-05-06 10:15:01 -04001460static int free_block_group_radix(struct radix_tree_root *radix)
Chris Mason9078a3e2007-04-26 16:46:15 -04001461{
1462 int ret;
1463 struct btrfs_block_group_cache *cache[8];
1464 int i;
1465
1466 while(1) {
Chris Masonbe744172007-05-06 10:15:01 -04001467 ret = radix_tree_gang_lookup(radix, (void **)cache, 0,
Chris Mason9078a3e2007-04-26 16:46:15 -04001468 ARRAY_SIZE(cache));
1469 if (!ret)
1470 break;
1471 for (i = 0; i < ret; i++) {
Chris Masonbe744172007-05-06 10:15:01 -04001472 radix_tree_delete(radix, cache[i]->key.objectid +
Chris Mason9078a3e2007-04-26 16:46:15 -04001473 cache[i]->key.offset - 1);
1474 kfree(cache[i]);
1475 }
1476 }
1477 return 0;
1478}
1479
Chris Masonbe744172007-05-06 10:15:01 -04001480int btrfs_free_block_groups(struct btrfs_fs_info *info)
1481{
1482 int ret;
1483 int ret2;
Chris Masone37c9e62007-05-09 20:13:14 -04001484 unsigned long gang[16];
1485 int i;
Chris Masonbe744172007-05-06 10:15:01 -04001486
1487 ret = free_block_group_radix(&info->block_group_radix);
1488 ret2 = free_block_group_radix(&info->block_group_data_radix);
1489 if (ret)
1490 return ret;
1491 if (ret2)
1492 return ret2;
Chris Masone37c9e62007-05-09 20:13:14 -04001493
1494 while(1) {
1495 ret = find_first_radix_bit(&info->extent_map_radix,
1496 gang, 0, ARRAY_SIZE(gang));
1497 if (!ret)
1498 break;
1499 for (i = 0; i < ret; i++) {
1500 clear_radix_bit(&info->extent_map_radix, gang[i]);
1501 }
1502 }
Chris Masonbe744172007-05-06 10:15:01 -04001503 return 0;
1504}
1505
Chris Mason9078a3e2007-04-26 16:46:15 -04001506int btrfs_read_block_groups(struct btrfs_root *root)
1507{
1508 struct btrfs_path *path;
1509 int ret;
1510 int err = 0;
1511 struct btrfs_block_group_item *bi;
1512 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04001513 struct btrfs_fs_info *info = root->fs_info;
1514 struct radix_tree_root *radix;
Chris Mason9078a3e2007-04-26 16:46:15 -04001515 struct btrfs_key key;
1516 struct btrfs_key found_key;
1517 struct btrfs_leaf *leaf;
1518 u64 group_size_blocks = BTRFS_BLOCK_GROUP_SIZE / root->blocksize;
Chris Mason31f3c992007-04-30 15:25:45 -04001519 u64 used;
Chris Mason9078a3e2007-04-26 16:46:15 -04001520
Chris Masonbe744172007-05-06 10:15:01 -04001521 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04001522 key.objectid = 0;
1523 key.offset = group_size_blocks;
1524 key.flags = 0;
1525 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
1526
1527 path = btrfs_alloc_path();
1528 if (!path)
1529 return -ENOMEM;
1530
1531 while(1) {
Chris Masonbe744172007-05-06 10:15:01 -04001532 ret = btrfs_search_slot(NULL, info->extent_root,
Chris Mason9078a3e2007-04-26 16:46:15 -04001533 &key, path, 0, 0);
1534 if (ret != 0) {
1535 err = ret;
1536 break;
1537 }
1538 leaf = btrfs_buffer_leaf(path->nodes[0]);
1539 btrfs_disk_key_to_cpu(&found_key,
1540 &leaf->items[path->slots[0]].key);
1541 cache = kmalloc(sizeof(*cache), GFP_NOFS);
1542 if (!cache) {
1543 err = -1;
1544 break;
1545 }
Chris Mason3e1ad542007-05-07 20:03:49 -04001546
Chris Mason9078a3e2007-04-26 16:46:15 -04001547 bi = btrfs_item_ptr(leaf, path->slots[0],
1548 struct btrfs_block_group_item);
Chris Mason1e2677e2007-05-29 16:52:18 -04001549 if (bi->flags & BTRFS_BLOCK_GROUP_DATA) {
1550 radix = &info->block_group_data_radix;
1551 cache->data = 1;
1552 } else {
1553 radix = &info->block_group_radix;
1554 cache->data = 0;
1555 }
1556
Chris Mason9078a3e2007-04-26 16:46:15 -04001557 memcpy(&cache->item, bi, sizeof(*bi));
1558 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Mason31f3c992007-04-30 15:25:45 -04001559 cache->last_alloc = cache->key.objectid;
1560 cache->first_free = cache->key.objectid;
Chris Masone37c9e62007-05-09 20:13:14 -04001561 cache->last_prealloc = cache->key.objectid;
Chris Masonbe744172007-05-06 10:15:01 -04001562 cache->pinned = 0;
Chris Masone37c9e62007-05-09 20:13:14 -04001563 cache->cached = 0;
1564
Chris Mason3e1ad542007-05-07 20:03:49 -04001565 cache->radix = radix;
1566
Chris Mason9078a3e2007-04-26 16:46:15 -04001567 key.objectid = found_key.objectid + found_key.offset;
1568 btrfs_release_path(root, path);
Chris Masonbe744172007-05-06 10:15:01 -04001569 ret = radix_tree_insert(radix, found_key.objectid +
Chris Mason9078a3e2007-04-26 16:46:15 -04001570 found_key.offset - 1,
1571 (void *)cache);
1572 BUG_ON(ret);
Chris Mason31f3c992007-04-30 15:25:45 -04001573 used = btrfs_block_group_used(bi);
Chris Masonbe744172007-05-06 10:15:01 -04001574 if (used < (key.offset * 8) / 10) {
1575 radix_tree_tag_set(radix, found_key.objectid +
Chris Mason31f3c992007-04-30 15:25:45 -04001576 found_key.offset - 1,
1577 BTRFS_BLOCK_GROUP_AVAIL);
1578 }
Chris Mason9078a3e2007-04-26 16:46:15 -04001579 if (key.objectid >=
Chris Masonbe744172007-05-06 10:15:01 -04001580 btrfs_super_total_blocks(info->disk_super))
Chris Mason9078a3e2007-04-26 16:46:15 -04001581 break;
1582 }
1583
1584 btrfs_free_path(path);
1585 return 0;
1586}