blob: f509ffa38d03aba3334250fdd528d117b7b5996c [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
Chris Masonfbdc7622007-05-30 10:22:12 -04008 *orig_root, u64 num_blocks, u64 search_start,
9 u64 search_end, u64 hint_block,
10 struct btrfs_key *ins, int data);
Chris Masone089f052007-03-16 16:20:31 -040011static int finish_current_insert(struct btrfs_trans_handle *trans, struct
12 btrfs_root *extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -040013static int del_pending_extents(struct btrfs_trans_handle *trans, struct
14 btrfs_root *extent_root);
Chris Masonfec577f2007-02-26 10:40:21 -050015
Chris Masonde428b62007-05-18 13:28:27 -040016static void reada_extent_leaves(struct btrfs_root *root,
17 struct btrfs_path *path, u64 limit)
18{
19 struct btrfs_node *node;
20 int i;
21 int nritems;
22 u64 item_objectid;
23 u64 blocknr;
24 int slot;
25 int ret;
26
27 if (!path->nodes[1])
28 return;
29 node = btrfs_buffer_node(path->nodes[1]);
30 slot = path->slots[1] + 1;
31 nritems = btrfs_header_nritems(&node->header);
32 for (i = slot; i < nritems && i < slot + 8; i++) {
33 item_objectid = btrfs_disk_key_objectid(&node->ptrs[i].key);
34 if (item_objectid > limit)
35 break;
36 blocknr = btrfs_node_blockptr(node, i);
37 ret = readahead_tree_block(root, blocknr);
38 if (ret)
39 break;
40 }
41}
42
Chris Masone37c9e62007-05-09 20:13:14 -040043static int cache_block_group(struct btrfs_root *root,
44 struct btrfs_block_group_cache *block_group)
45{
46 struct btrfs_path *path;
47 int ret;
48 struct btrfs_key key;
49 struct btrfs_leaf *leaf;
50 struct radix_tree_root *extent_radix;
51 int slot;
52 u64 i;
53 u64 last = 0;
54 u64 hole_size;
Chris Masonde428b62007-05-18 13:28:27 -040055 u64 limit;
Chris Masone37c9e62007-05-09 20:13:14 -040056 int found = 0;
57
58 root = root->fs_info->extent_root;
59 extent_radix = &root->fs_info->extent_map_radix;
60
61 if (block_group->cached)
62 return 0;
63 if (block_group->data)
64 return 0;
65 path = btrfs_alloc_path();
66 if (!path)
67 return -ENOMEM;
68printk("cache block group %Lu\n", block_group->key.objectid);
69 key.objectid = block_group->key.objectid;
70 key.flags = 0;
71 key.offset = 0;
72 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
73 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
74 if (ret < 0)
75 return ret;
76 if (ret && path->slots[0] > 0)
77 path->slots[0]--;
Chris Masonde428b62007-05-18 13:28:27 -040078 limit = block_group->key.objectid + block_group->key.offset;
79 reada_extent_leaves(root, path, limit);
Chris Masone37c9e62007-05-09 20:13:14 -040080 while(1) {
81 leaf = btrfs_buffer_leaf(path->nodes[0]);
82 slot = path->slots[0];
83 if (slot >= btrfs_header_nritems(&leaf->header)) {
Chris Masonde428b62007-05-18 13:28:27 -040084 reada_extent_leaves(root, path, limit);
Chris Masone37c9e62007-05-09 20:13:14 -040085 ret = btrfs_next_leaf(root, path);
Chris Masonde428b62007-05-18 13:28:27 -040086 if (ret == 0) {
Chris Masone37c9e62007-05-09 20:13:14 -040087 continue;
Chris Masonde428b62007-05-18 13:28:27 -040088 } else {
Chris Masone37c9e62007-05-09 20:13:14 -040089 if (found) {
90 hole_size = block_group->key.objectid +
91 block_group->key.offset - last;
92 } else {
93 last = block_group->key.objectid;
94 hole_size = block_group->key.offset;
95 }
96 for (i = 0; i < hole_size; i++) {
97 set_radix_bit(extent_radix,
98 last + i);
99 }
100 break;
101 }
102 }
103 btrfs_disk_key_to_cpu(&key, &leaf->items[slot].key);
104 if (key.objectid >= block_group->key.objectid +
105 block_group->key.offset) {
106 if (found) {
107 hole_size = block_group->key.objectid +
108 block_group->key.offset - last;
109 } else {
110 last = block_group->key.objectid;
111 hole_size = block_group->key.offset;
112 }
113 for (i = 0; i < hole_size; i++) {
114 set_radix_bit(extent_radix, last + i);
115 }
116 break;
117 }
118 if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) {
119 if (!found) {
120 last = key.objectid + key.offset;
121 found = 1;
122 } else {
123 hole_size = key.objectid - last;
124 for (i = 0; i < hole_size; i++) {
125 set_radix_bit(extent_radix, last + i);
126 }
127 last = key.objectid + key.offset;
128 }
129 }
130 path->slots[0]++;
131 }
132
133 block_group->cached = 1;
134 btrfs_free_path(path);
135 return 0;
136}
137
Chris Mason5276aed2007-06-11 21:33:38 -0400138struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
139 btrfs_fs_info *info,
140 u64 blocknr)
Chris Masonbe744172007-05-06 10:15:01 -0400141{
142 struct btrfs_block_group_cache *block_group;
143 int ret;
144
145 ret = radix_tree_gang_lookup(&info->block_group_radix,
146 (void **)&block_group,
147 blocknr, 1);
148 if (ret) {
Chris Mason3e1ad542007-05-07 20:03:49 -0400149 if (block_group->key.objectid <= blocknr && blocknr <=
Chris Masonbe744172007-05-06 10:15:01 -0400150 block_group->key.objectid + block_group->key.offset)
151 return block_group;
152 }
153 ret = radix_tree_gang_lookup(&info->block_group_data_radix,
154 (void **)&block_group,
155 blocknr, 1);
156 if (ret) {
Chris Mason3e1ad542007-05-07 20:03:49 -0400157 if (block_group->key.objectid <= blocknr && blocknr <=
Chris Masonbe744172007-05-06 10:15:01 -0400158 block_group->key.objectid + block_group->key.offset)
159 return block_group;
160 }
Chris Masonbe744172007-05-06 10:15:01 -0400161 return NULL;
162}
163
Chris Masone37c9e62007-05-09 20:13:14 -0400164static u64 leaf_range(struct btrfs_root *root)
165{
166 u64 size = BTRFS_LEAF_DATA_SIZE(root);
167 size = size / (sizeof(struct btrfs_extent_item) +
168 sizeof(struct btrfs_item));
169 return size;
170}
171
172static u64 find_search_start(struct btrfs_root *root,
173 struct btrfs_block_group_cache **cache_ret,
174 u64 search_start, int num)
175{
176 unsigned long gang[8];
177 int ret;
178 struct btrfs_block_group_cache *cache = *cache_ret;
179 u64 last = max(search_start, cache->key.objectid);
180
181 if (cache->data)
182 goto out;
183 if (num > 1) {
184 last = max(last, cache->last_prealloc);
185 }
186again:
187 cache_block_group(root, cache);
188 while(1) {
189 ret = find_first_radix_bit(&root->fs_info->extent_map_radix,
190 gang, last, ARRAY_SIZE(gang));
191 if (!ret)
192 goto out;
193 last = gang[ret-1] + 1;
194 if (num > 1) {
195 if (ret != ARRAY_SIZE(gang)) {
196 goto new_group;
197 }
198 if (gang[ret-1] - gang[0] > leaf_range(root)) {
199 continue;
200 }
201 }
202 if (gang[0] >= cache->key.objectid + cache->key.offset) {
203 goto new_group;
204 }
205 return gang[0];
206 }
207out:
208 return max(cache->last_alloc, search_start);
209
210new_group:
Chris Mason5276aed2007-06-11 21:33:38 -0400211 cache = btrfs_lookup_block_group(root->fs_info,
212 last + cache->key.offset - 1);
Chris Masone37c9e62007-05-09 20:13:14 -0400213 if (!cache) {
214 return max((*cache_ret)->last_alloc, search_start);
215 }
216 cache = btrfs_find_block_group(root, cache,
Chris Masonde428b62007-05-18 13:28:27 -0400217 last + cache->key.offset - 1, 0, 0);
Chris Masone37c9e62007-05-09 20:13:14 -0400218 *cache_ret = cache;
219 goto again;
220}
221
Chris Mason31f3c992007-04-30 15:25:45 -0400222struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
223 struct btrfs_block_group_cache
Chris Masonbe744172007-05-06 10:15:01 -0400224 *hint, u64 search_start,
Chris Masonde428b62007-05-18 13:28:27 -0400225 int data, int owner)
Chris Masoncd1bc462007-04-27 10:08:34 -0400226{
227 struct btrfs_block_group_cache *cache[8];
Chris Mason31f3c992007-04-30 15:25:45 -0400228 struct btrfs_block_group_cache *found_group = NULL;
Chris Masoncd1bc462007-04-27 10:08:34 -0400229 struct btrfs_fs_info *info = root->fs_info;
Chris Masonbe744172007-05-06 10:15:01 -0400230 struct radix_tree_root *radix;
Chris Mason1e2677e2007-05-29 16:52:18 -0400231 struct radix_tree_root *swap_radix;
Chris Masoncd1bc462007-04-27 10:08:34 -0400232 u64 used;
Chris Mason31f3c992007-04-30 15:25:45 -0400233 u64 last = 0;
234 u64 hint_last;
Chris Masoncd1bc462007-04-27 10:08:34 -0400235 int i;
236 int ret;
Chris Mason31f3c992007-04-30 15:25:45 -0400237 int full_search = 0;
Chris Masonde428b62007-05-18 13:28:27 -0400238 int factor = 8;
Chris Mason1e2677e2007-05-29 16:52:18 -0400239 int data_swap = 0;
Chris Masonde428b62007-05-18 13:28:27 -0400240
241 if (!owner)
242 factor = 5;
Chris Masonbe744172007-05-06 10:15:01 -0400243
Chris Mason1e2677e2007-05-29 16:52:18 -0400244 if (data) {
Chris Masonbe744172007-05-06 10:15:01 -0400245 radix = &info->block_group_data_radix;
Chris Mason1e2677e2007-05-29 16:52:18 -0400246 swap_radix = &info->block_group_radix;
247 } else {
Chris Masonbe744172007-05-06 10:15:01 -0400248 radix = &info->block_group_radix;
Chris Mason1e2677e2007-05-29 16:52:18 -0400249 swap_radix = &info->block_group_data_radix;
250 }
Chris Masonbe744172007-05-06 10:15:01 -0400251
252 if (search_start) {
253 struct btrfs_block_group_cache *shint;
Chris Mason5276aed2007-06-11 21:33:38 -0400254 shint = btrfs_lookup_block_group(info, search_start);
Chris Masonbe744172007-05-06 10:15:01 -0400255 if (shint->data == data) {
256 used = btrfs_block_group_used(&shint->item);
257 if (used + shint->pinned <
Chris Masonde428b62007-05-18 13:28:27 -0400258 (shint->key.offset * factor) / 10) {
Chris Masonbe744172007-05-06 10:15:01 -0400259 return shint;
260 }
261 }
262 }
263 if (hint && hint->data == data) {
Chris Mason31f3c992007-04-30 15:25:45 -0400264 used = btrfs_block_group_used(&hint->item);
Chris Masonde428b62007-05-18 13:28:27 -0400265 if (used + hint->pinned < (hint->key.offset * factor) / 10) {
Chris Mason31f3c992007-04-30 15:25:45 -0400266 return hint;
267 }
Chris Masonbe744172007-05-06 10:15:01 -0400268 if (used >= (hint->key.offset * 8) / 10) {
269 radix_tree_tag_clear(radix,
270 hint->key.objectid +
271 hint->key.offset - 1,
272 BTRFS_BLOCK_GROUP_AVAIL);
273 }
Chris Mason8d7be552007-05-10 11:24:42 -0400274 last = hint->key.offset * 3;
Chris Masonbe744172007-05-06 10:15:01 -0400275 if (hint->key.objectid >= last)
Chris Masone37c9e62007-05-09 20:13:14 -0400276 last = max(search_start + hint->key.offset - 1,
277 hint->key.objectid - last);
Chris Masonbe744172007-05-06 10:15:01 -0400278 else
279 last = hint->key.objectid + hint->key.offset;
Chris Mason31f3c992007-04-30 15:25:45 -0400280 hint_last = last;
281 } else {
Chris Masone37c9e62007-05-09 20:13:14 -0400282 if (hint)
283 hint_last = max(hint->key.objectid, search_start);
284 else
285 hint_last = search_start;
286
287 last = hint_last;
Chris Mason31f3c992007-04-30 15:25:45 -0400288 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400289 while(1) {
Chris Masonbe744172007-05-06 10:15:01 -0400290 ret = radix_tree_gang_lookup_tag(radix, (void **)cache,
Chris Masoncd1bc462007-04-27 10:08:34 -0400291 last, ARRAY_SIZE(cache),
Chris Mason31f3c992007-04-30 15:25:45 -0400292 BTRFS_BLOCK_GROUP_AVAIL);
Chris Masoncd1bc462007-04-27 10:08:34 -0400293 if (!ret)
294 break;
295 for (i = 0; i < ret; i++) {
Chris Masonbe08c1b2007-05-03 09:06:49 -0400296 last = cache[i]->key.objectid +
297 cache[i]->key.offset;
Chris Masoncd1bc462007-04-27 10:08:34 -0400298 used = btrfs_block_group_used(&cache[i]->item);
Chris Masonbe744172007-05-06 10:15:01 -0400299 if (used + cache[i]->pinned <
Chris Masonde428b62007-05-18 13:28:27 -0400300 (cache[i]->key.offset * factor) / 10) {
Chris Mason31f3c992007-04-30 15:25:45 -0400301 found_group = cache[i];
302 goto found;
Chris Masoncd1bc462007-04-27 10:08:34 -0400303 }
Chris Masonbe744172007-05-06 10:15:01 -0400304 if (used >= (cache[i]->key.offset * 8) / 10) {
305 radix_tree_tag_clear(radix,
306 cache[i]->key.objectid +
307 cache[i]->key.offset - 1,
308 BTRFS_BLOCK_GROUP_AVAIL);
309 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400310 }
Chris Masonde428b62007-05-18 13:28:27 -0400311 cond_resched();
Chris Masoncd1bc462007-04-27 10:08:34 -0400312 }
Chris Mason31f3c992007-04-30 15:25:45 -0400313 last = hint_last;
314again:
Chris Masoncd1bc462007-04-27 10:08:34 -0400315 while(1) {
Chris Masonbe744172007-05-06 10:15:01 -0400316 ret = radix_tree_gang_lookup(radix, (void **)cache,
317 last, ARRAY_SIZE(cache));
Chris Masoncd1bc462007-04-27 10:08:34 -0400318 if (!ret)
319 break;
320 for (i = 0; i < ret; i++) {
Chris Masonbe08c1b2007-05-03 09:06:49 -0400321 last = cache[i]->key.objectid +
322 cache[i]->key.offset;
Chris Masoncd1bc462007-04-27 10:08:34 -0400323 used = btrfs_block_group_used(&cache[i]->item);
Chris Masonbe744172007-05-06 10:15:01 -0400324 if (used + cache[i]->pinned < cache[i]->key.offset) {
Chris Mason31f3c992007-04-30 15:25:45 -0400325 found_group = cache[i];
326 goto found;
Chris Masoncd1bc462007-04-27 10:08:34 -0400327 }
Chris Masonbe744172007-05-06 10:15:01 -0400328 if (used >= cache[i]->key.offset) {
329 radix_tree_tag_clear(radix,
330 cache[i]->key.objectid +
331 cache[i]->key.offset - 1,
332 BTRFS_BLOCK_GROUP_AVAIL);
333 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400334 }
Chris Masonde428b62007-05-18 13:28:27 -0400335 cond_resched();
Chris Masoncd1bc462007-04-27 10:08:34 -0400336 }
Chris Mason31f3c992007-04-30 15:25:45 -0400337 if (!full_search) {
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 Mason1e2677e2007-05-29 16:52:18 -0400342 if (!data_swap) {
343 struct radix_tree_root *tmp = radix;
344 data_swap = 1;
345 radix = swap_radix;
346 swap_radix = tmp;
347 last = search_start;
348 goto again;
349 }
Chris Mason31f3c992007-04-30 15:25:45 -0400350 if (!found_group) {
Chris Masonde428b62007-05-18 13:28:27 -0400351printk("find block group bailing to zero data %d\n", data);
Chris Masonbe744172007-05-06 10:15:01 -0400352 ret = radix_tree_gang_lookup(radix,
Chris Mason31f3c992007-04-30 15:25:45 -0400353 (void **)&found_group, 0, 1);
Chris Mason1e2677e2007-05-29 16:52:18 -0400354 if (ret == 0) {
355 ret = radix_tree_gang_lookup(swap_radix,
356 (void **)&found_group,
357 0, 1);
358 }
Chris Mason31f3c992007-04-30 15:25:45 -0400359 BUG_ON(ret != 1);
360 }
Chris Masonbe744172007-05-06 10:15:01 -0400361found:
Chris Mason31f3c992007-04-30 15:25:45 -0400362 return found_group;
Chris Masoncd1bc462007-04-27 10:08:34 -0400363}
364
Chris Masonb18c6682007-04-17 13:26:50 -0400365int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
366 struct btrfs_root *root,
367 u64 blocknr, u64 num_blocks)
Chris Mason02217ed2007-03-02 16:08:05 -0500368{
Chris Mason5caf2a02007-04-02 11:20:42 -0400369 struct btrfs_path *path;
Chris Mason02217ed2007-03-02 16:08:05 -0500370 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400371 struct btrfs_key key;
Chris Mason234b63a2007-03-13 10:46:10 -0400372 struct btrfs_leaf *l;
373 struct btrfs_extent_item *item;
Chris Masone2fa7222007-03-12 16:22:34 -0400374 struct btrfs_key ins;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400375 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500376
Chris Masonfbdc7622007-05-30 10:22:12 -0400377 find_free_extent(trans, root->fs_info->extent_root, 0, 0, (u64)-1, 0,
Chris Masonbe08c1b2007-05-03 09:06:49 -0400378 &ins, 0);
Chris Mason5caf2a02007-04-02 11:20:42 -0400379 path = btrfs_alloc_path();
380 BUG_ON(!path);
381 btrfs_init_path(path);
Chris Mason02217ed2007-03-02 16:08:05 -0500382 key.objectid = blocknr;
383 key.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -0400384 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Mason6407bf62007-03-27 06:33:00 -0400385 key.offset = num_blocks;
Chris Mason5caf2a02007-04-02 11:20:42 -0400386 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -0400387 0, 1);
Chris Masona429e512007-04-18 16:15:28 -0400388 if (ret != 0) {
389printk("can't find block %Lu %Lu\n", blocknr, num_blocks);
Chris Masona28ec192007-03-06 20:08:01 -0500390 BUG();
Chris Masona429e512007-04-18 16:15:28 -0400391 }
Chris Mason02217ed2007-03-02 16:08:05 -0500392 BUG_ON(ret != 0);
Chris Mason5caf2a02007-04-02 11:20:42 -0400393 l = btrfs_buffer_leaf(path->nodes[0]);
394 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400395 refs = btrfs_extent_refs(item);
396 btrfs_set_extent_refs(item, refs + 1);
Chris Mason5caf2a02007-04-02 11:20:42 -0400397 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masona28ec192007-03-06 20:08:01 -0500398
Chris Mason5caf2a02007-04-02 11:20:42 -0400399 btrfs_release_path(root->fs_info->extent_root, path);
400 btrfs_free_path(path);
Chris Mason9f5fae22007-03-20 14:38:32 -0400401 finish_current_insert(trans, root->fs_info->extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -0400402 del_pending_extents(trans, root->fs_info->extent_root);
Chris Mason02217ed2007-03-02 16:08:05 -0500403 return 0;
404}
405
Chris Masonb18c6682007-04-17 13:26:50 -0400406static int lookup_extent_ref(struct btrfs_trans_handle *trans,
407 struct btrfs_root *root, u64 blocknr,
408 u64 num_blocks, u32 *refs)
Chris Masona28ec192007-03-06 20:08:01 -0500409{
Chris Mason5caf2a02007-04-02 11:20:42 -0400410 struct btrfs_path *path;
Chris Masona28ec192007-03-06 20:08:01 -0500411 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400412 struct btrfs_key key;
Chris Mason234b63a2007-03-13 10:46:10 -0400413 struct btrfs_leaf *l;
414 struct btrfs_extent_item *item;
Chris Mason5caf2a02007-04-02 11:20:42 -0400415
416 path = btrfs_alloc_path();
417 btrfs_init_path(path);
Chris Masona28ec192007-03-06 20:08:01 -0500418 key.objectid = blocknr;
Chris Mason6407bf62007-03-27 06:33:00 -0400419 key.offset = num_blocks;
Chris Mason62e27492007-03-15 12:56:47 -0400420 key.flags = 0;
421 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Mason5caf2a02007-04-02 11:20:42 -0400422 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -0400423 0, 0);
Chris Masona28ec192007-03-06 20:08:01 -0500424 if (ret != 0)
425 BUG();
Chris Mason5caf2a02007-04-02 11:20:42 -0400426 l = btrfs_buffer_leaf(path->nodes[0]);
427 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400428 *refs = btrfs_extent_refs(item);
Chris Mason5caf2a02007-04-02 11:20:42 -0400429 btrfs_release_path(root->fs_info->extent_root, path);
430 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -0500431 return 0;
432}
433
Chris Masonc5739bb2007-04-10 09:27:04 -0400434int btrfs_inc_root_ref(struct btrfs_trans_handle *trans,
435 struct btrfs_root *root)
436{
Chris Masonb18c6682007-04-17 13:26:50 -0400437 return btrfs_inc_extent_ref(trans, root, bh_blocknr(root->node), 1);
Chris Masonc5739bb2007-04-10 09:27:04 -0400438}
439
Chris Masone089f052007-03-16 16:20:31 -0400440int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Masone20d96d2007-03-22 12:13:20 -0400441 struct buffer_head *buf)
Chris Mason02217ed2007-03-02 16:08:05 -0500442{
443 u64 blocknr;
Chris Masone20d96d2007-03-22 12:13:20 -0400444 struct btrfs_node *buf_node;
Chris Mason6407bf62007-03-27 06:33:00 -0400445 struct btrfs_leaf *buf_leaf;
446 struct btrfs_disk_key *key;
447 struct btrfs_file_extent_item *fi;
Chris Mason02217ed2007-03-02 16:08:05 -0500448 int i;
Chris Mason6407bf62007-03-27 06:33:00 -0400449 int leaf;
450 int ret;
Chris Masona28ec192007-03-06 20:08:01 -0500451
Chris Mason3768f362007-03-13 16:47:54 -0400452 if (!root->ref_cows)
Chris Masona28ec192007-03-06 20:08:01 -0500453 return 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400454 buf_node = btrfs_buffer_node(buf);
Chris Mason6407bf62007-03-27 06:33:00 -0400455 leaf = btrfs_is_leaf(buf_node);
456 buf_leaf = btrfs_buffer_leaf(buf);
Chris Masone20d96d2007-03-22 12:13:20 -0400457 for (i = 0; i < btrfs_header_nritems(&buf_node->header); i++) {
Chris Mason6407bf62007-03-27 06:33:00 -0400458 if (leaf) {
Chris Mason3a686372007-05-24 13:35:57 -0400459 u64 disk_blocknr;
Chris Mason6407bf62007-03-27 06:33:00 -0400460 key = &buf_leaf->items[i].key;
461 if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY)
462 continue;
463 fi = btrfs_item_ptr(buf_leaf, i,
464 struct btrfs_file_extent_item);
Chris Mason236454d2007-04-19 13:37:44 -0400465 if (btrfs_file_extent_type(fi) ==
466 BTRFS_FILE_EXTENT_INLINE)
467 continue;
Chris Mason3a686372007-05-24 13:35:57 -0400468 disk_blocknr = btrfs_file_extent_disk_blocknr(fi);
469 if (disk_blocknr == 0)
470 continue;
471 ret = btrfs_inc_extent_ref(trans, root, disk_blocknr,
Chris Mason6407bf62007-03-27 06:33:00 -0400472 btrfs_file_extent_disk_num_blocks(fi));
473 BUG_ON(ret);
474 } else {
475 blocknr = btrfs_node_blockptr(buf_node, i);
Chris Masonb18c6682007-04-17 13:26:50 -0400476 ret = btrfs_inc_extent_ref(trans, root, blocknr, 1);
Chris Mason6407bf62007-03-27 06:33:00 -0400477 BUG_ON(ret);
478 }
Chris Mason02217ed2007-03-02 16:08:05 -0500479 }
480 return 0;
481}
482
Chris Mason9078a3e2007-04-26 16:46:15 -0400483static int write_one_cache_group(struct btrfs_trans_handle *trans,
484 struct btrfs_root *root,
485 struct btrfs_path *path,
486 struct btrfs_block_group_cache *cache)
487{
488 int ret;
489 int pending_ret;
490 struct btrfs_root *extent_root = root->fs_info->extent_root;
491 struct btrfs_block_group_item *bi;
492 struct btrfs_key ins;
493
Chris Masonfbdc7622007-05-30 10:22:12 -0400494 find_free_extent(trans, extent_root, 0, 0, (u64)-1, 0, &ins, 0);
Chris Mason9078a3e2007-04-26 16:46:15 -0400495 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
496 BUG_ON(ret);
497 bi = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
498 struct btrfs_block_group_item);
499 memcpy(bi, &cache->item, sizeof(*bi));
500 mark_buffer_dirty(path->nodes[0]);
501 btrfs_release_path(extent_root, path);
502
503 finish_current_insert(trans, extent_root);
504 pending_ret = del_pending_extents(trans, extent_root);
505 if (ret)
506 return ret;
507 if (pending_ret)
508 return pending_ret;
Chris Masonbe744172007-05-06 10:15:01 -0400509 if (cache->data)
510 cache->last_alloc = cache->first_free;
Chris Mason9078a3e2007-04-26 16:46:15 -0400511 return 0;
512
513}
514
Chris Masonbe744172007-05-06 10:15:01 -0400515static int write_dirty_block_radix(struct btrfs_trans_handle *trans,
516 struct btrfs_root *root,
517 struct radix_tree_root *radix)
Chris Mason9078a3e2007-04-26 16:46:15 -0400518{
519 struct btrfs_block_group_cache *cache[8];
520 int ret;
521 int err = 0;
522 int werr = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -0400523 int i;
524 struct btrfs_path *path;
525
526 path = btrfs_alloc_path();
527 if (!path)
528 return -ENOMEM;
529
530 while(1) {
531 ret = radix_tree_gang_lookup_tag(radix, (void **)cache,
532 0, ARRAY_SIZE(cache),
533 BTRFS_BLOCK_GROUP_DIRTY);
534 if (!ret)
535 break;
536 for (i = 0; i < ret; i++) {
537 radix_tree_tag_clear(radix, cache[i]->key.objectid +
538 cache[i]->key.offset - 1,
539 BTRFS_BLOCK_GROUP_DIRTY);
540 err = write_one_cache_group(trans, root,
541 path, cache[i]);
542 if (err)
543 werr = err;
544 }
545 }
546 btrfs_free_path(path);
547 return werr;
548}
549
Chris Masonbe744172007-05-06 10:15:01 -0400550int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
551 struct btrfs_root *root)
552{
553 int ret;
554 int ret2;
555 ret = write_dirty_block_radix(trans, root,
556 &root->fs_info->block_group_radix);
557 ret2 = write_dirty_block_radix(trans, root,
558 &root->fs_info->block_group_data_radix);
559 if (ret)
560 return ret;
561 if (ret2)
562 return ret2;
563 return 0;
564}
565
Chris Mason9078a3e2007-04-26 16:46:15 -0400566static int update_block_group(struct btrfs_trans_handle *trans,
567 struct btrfs_root *root,
Chris Mason1e2677e2007-05-29 16:52:18 -0400568 u64 blocknr, u64 num, int alloc, int mark_free,
569 int data)
Chris Mason9078a3e2007-04-26 16:46:15 -0400570{
571 struct btrfs_block_group_cache *cache;
572 struct btrfs_fs_info *info = root->fs_info;
573 u64 total = num;
574 u64 old_val;
575 u64 block_in_group;
Chris Masone37c9e62007-05-09 20:13:14 -0400576 u64 i;
Chris Mason1e2677e2007-05-29 16:52:18 -0400577 int ret;
Chris Mason3e1ad542007-05-07 20:03:49 -0400578
Chris Mason9078a3e2007-04-26 16:46:15 -0400579 while(total) {
Chris Mason5276aed2007-06-11 21:33:38 -0400580 cache = btrfs_lookup_block_group(info, blocknr);
Chris Mason3e1ad542007-05-07 20:03:49 -0400581 if (!cache) {
Chris Masoncd1bc462007-04-27 10:08:34 -0400582 printk(KERN_CRIT "blocknr %Lu lookup failed\n",
583 blocknr);
Chris Mason9078a3e2007-04-26 16:46:15 -0400584 return -1;
Chris Masoncd1bc462007-04-27 10:08:34 -0400585 }
Chris Mason9078a3e2007-04-26 16:46:15 -0400586 block_in_group = blocknr - cache->key.objectid;
587 WARN_ON(block_in_group > cache->key.offset);
Chris Mason3e1ad542007-05-07 20:03:49 -0400588 radix_tree_tag_set(cache->radix, cache->key.objectid +
Chris Masonbe744172007-05-06 10:15:01 -0400589 cache->key.offset - 1,
Chris Mason9078a3e2007-04-26 16:46:15 -0400590 BTRFS_BLOCK_GROUP_DIRTY);
591
592 old_val = btrfs_block_group_used(&cache->item);
593 num = min(total, cache->key.offset - block_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -0400594 if (alloc) {
Chris Masoncd1bc462007-04-27 10:08:34 -0400595 if (blocknr > cache->last_alloc)
596 cache->last_alloc = blocknr;
Chris Masone37c9e62007-05-09 20:13:14 -0400597 if (!cache->data) {
598 for (i = 0; i < num; i++) {
599 clear_radix_bit(&info->extent_map_radix,
600 blocknr + i);
601 }
602 }
Chris Mason1e2677e2007-05-29 16:52:18 -0400603 if (cache->data != data &&
604 old_val < cache->key.offset / 2) {
605printk("changing block group %Lu from %d to %d\n", cache->key.objectid, cache->data, data);
606 cache->data = data;
607 radix_tree_delete(cache->radix,
608 cache->key.objectid +
609 cache->key.offset - 1);
610
611 if (data) {
612 cache->radix =
613 &info->block_group_data_radix;
614 cache->item.flags |=
615 BTRFS_BLOCK_GROUP_DATA;
616 } else {
617 cache->radix = &info->block_group_radix;
618 cache->item.flags &=
619 ~BTRFS_BLOCK_GROUP_DATA;
620 }
621 ret = radix_tree_insert(cache->radix,
622 cache->key.objectid +
623 cache->key.offset - 1,
624 (void *)cache);
625 }
626 old_val += num;
Chris Masoncd1bc462007-04-27 10:08:34 -0400627 } else {
Chris Mason9078a3e2007-04-26 16:46:15 -0400628 old_val -= num;
Chris Masoncd1bc462007-04-27 10:08:34 -0400629 if (blocknr < cache->first_free)
630 cache->first_free = blocknr;
Chris Masone37c9e62007-05-09 20:13:14 -0400631 if (!cache->data && mark_free) {
632 for (i = 0; i < num; i++) {
633 set_radix_bit(&info->extent_map_radix,
634 blocknr + i);
635 }
636 }
Chris Mason1e2677e2007-05-29 16:52:18 -0400637 if (old_val < cache->key.offset / 2 &&
638 old_val + num >= cache->key.offset / 2) {
Chris Masone37c9e62007-05-09 20:13:14 -0400639printk("group %Lu now available\n", cache->key.objectid);
640 radix_tree_tag_set(cache->radix,
641 cache->key.objectid +
642 cache->key.offset - 1,
643 BTRFS_BLOCK_GROUP_AVAIL);
644 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400645 }
Chris Mason9078a3e2007-04-26 16:46:15 -0400646 btrfs_set_block_group_used(&cache->item, old_val);
Chris Masone37c9e62007-05-09 20:13:14 -0400647 total -= num;
648 blocknr += num;
Chris Mason9078a3e2007-04-26 16:46:15 -0400649 }
650 return 0;
651}
652
Chris Masonbe08c1b2007-05-03 09:06:49 -0400653static int try_remove_page(struct address_space *mapping, unsigned long index)
654{
655 int ret;
656 ret = invalidate_mapping_pages(mapping, index, index);
657 return ret;
658}
659
Chris Masone089f052007-03-16 16:20:31 -0400660int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, struct
661 btrfs_root *root)
Chris Masona28ec192007-03-06 20:08:01 -0500662{
Chris Mason8ef97622007-03-26 10:15:30 -0400663 unsigned long gang[8];
Chris Masonbe08c1b2007-05-03 09:06:49 -0400664 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Masonbe744172007-05-06 10:15:01 -0400665 struct btrfs_block_group_cache *block_group;
Chris Mason88fd1462007-03-16 08:56:18 -0400666 u64 first = 0;
Chris Masona28ec192007-03-06 20:08:01 -0500667 int ret;
668 int i;
Chris Mason8ef97622007-03-26 10:15:30 -0400669 struct radix_tree_root *pinned_radix = &root->fs_info->pinned_radix;
Chris Masone37c9e62007-05-09 20:13:14 -0400670 struct radix_tree_root *extent_radix = &root->fs_info->extent_map_radix;
Chris Masona28ec192007-03-06 20:08:01 -0500671
672 while(1) {
Chris Masone37c9e62007-05-09 20:13:14 -0400673 ret = find_first_radix_bit(pinned_radix, gang, 0,
Chris Mason8ef97622007-03-26 10:15:30 -0400674 ARRAY_SIZE(gang));
Chris Masona28ec192007-03-06 20:08:01 -0500675 if (!ret)
676 break;
Chris Mason88fd1462007-03-16 08:56:18 -0400677 if (!first)
Chris Mason8ef97622007-03-26 10:15:30 -0400678 first = gang[0];
Chris Mason0579da42007-03-07 16:15:30 -0500679 for (i = 0; i < ret; i++) {
Chris Mason8ef97622007-03-26 10:15:30 -0400680 clear_radix_bit(pinned_radix, gang[i]);
Chris Mason5276aed2007-06-11 21:33:38 -0400681 block_group = btrfs_lookup_block_group(root->fs_info,
682 gang[i]);
Chris Masonbe744172007-05-06 10:15:01 -0400683 if (block_group) {
684 WARN_ON(block_group->pinned == 0);
685 block_group->pinned--;
686 if (gang[i] < block_group->last_alloc)
687 block_group->last_alloc = gang[i];
Chris Masone37c9e62007-05-09 20:13:14 -0400688 if (gang[i] < block_group->last_prealloc)
689 block_group->last_prealloc = gang[i];
690 if (!block_group->data)
691 set_radix_bit(extent_radix, gang[i]);
Chris Masonbe744172007-05-06 10:15:01 -0400692 }
Chris Masonbe08c1b2007-05-03 09:06:49 -0400693 try_remove_page(btree_inode->i_mapping,
694 gang[i] << (PAGE_CACHE_SHIFT -
695 btree_inode->i_blkbits));
Chris Mason0579da42007-03-07 16:15:30 -0500696 }
Chris Masona28ec192007-03-06 20:08:01 -0500697 }
698 return 0;
699}
700
Chris Masone089f052007-03-16 16:20:31 -0400701static int finish_current_insert(struct btrfs_trans_handle *trans, struct
702 btrfs_root *extent_root)
Chris Mason037e6392007-03-07 11:50:24 -0500703{
Chris Masone2fa7222007-03-12 16:22:34 -0400704 struct btrfs_key ins;
Chris Mason234b63a2007-03-13 10:46:10 -0400705 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -0500706 int i;
707 int ret;
Chris Mason1261ec42007-03-20 20:35:03 -0400708 u64 super_blocks_used;
709 struct btrfs_fs_info *info = extent_root->fs_info;
Chris Mason037e6392007-03-07 11:50:24 -0500710
Chris Masoncf27e1e2007-03-13 09:49:06 -0400711 btrfs_set_extent_refs(&extent_item, 1);
Chris Mason037e6392007-03-07 11:50:24 -0500712 ins.offset = 1;
713 ins.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -0400714 btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
Chris Mason5d0c3e62007-04-23 17:01:05 -0400715 btrfs_set_extent_owner(&extent_item, extent_root->root_key.objectid);
Chris Mason037e6392007-03-07 11:50:24 -0500716
Chris Masonf2458e12007-04-25 15:52:25 -0400717 for (i = 0; i < extent_root->fs_info->extent_tree_insert_nr; i++) {
718 ins.objectid = extent_root->fs_info->extent_tree_insert[i];
Chris Mason1261ec42007-03-20 20:35:03 -0400719 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
720 btrfs_set_super_blocks_used(info->disk_super,
721 super_blocks_used + 1);
Chris Masone089f052007-03-16 16:20:31 -0400722 ret = btrfs_insert_item(trans, extent_root, &ins, &extent_item,
723 sizeof(extent_item));
Chris Mason037e6392007-03-07 11:50:24 -0500724 BUG_ON(ret);
725 }
Chris Masonf2458e12007-04-25 15:52:25 -0400726 extent_root->fs_info->extent_tree_insert_nr = 0;
727 extent_root->fs_info->extent_tree_prealloc_nr = 0;
Chris Mason037e6392007-03-07 11:50:24 -0500728 return 0;
729}
730
Chris Mason8ef97622007-03-26 10:15:30 -0400731static int pin_down_block(struct btrfs_root *root, u64 blocknr, int pending)
Chris Masone20d96d2007-03-22 12:13:20 -0400732{
733 int err;
Chris Mason78fae272007-03-25 11:35:08 -0400734 struct btrfs_header *header;
Chris Mason8ef97622007-03-26 10:15:30 -0400735 struct buffer_head *bh;
Chris Mason78fae272007-03-25 11:35:08 -0400736
Chris Masonf4b9aa82007-03-27 11:05:53 -0400737 if (!pending) {
Chris Masond98237b2007-03-28 13:57:48 -0400738 bh = btrfs_find_tree_block(root, blocknr);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400739 if (bh) {
740 if (buffer_uptodate(bh)) {
741 u64 transid =
742 root->fs_info->running_transaction->transid;
743 header = btrfs_buffer_header(bh);
744 if (btrfs_header_generation(header) ==
745 transid) {
746 btrfs_block_release(root, bh);
747 return 0;
748 }
Chris Masonf4b9aa82007-03-27 11:05:53 -0400749 }
Chris Masond6025572007-03-30 14:27:56 -0400750 btrfs_block_release(root, bh);
Chris Mason8ef97622007-03-26 10:15:30 -0400751 }
Chris Mason8ef97622007-03-26 10:15:30 -0400752 err = set_radix_bit(&root->fs_info->pinned_radix, blocknr);
Chris Masonbe744172007-05-06 10:15:01 -0400753 if (!err) {
754 struct btrfs_block_group_cache *cache;
Chris Mason5276aed2007-06-11 21:33:38 -0400755 cache = btrfs_lookup_block_group(root->fs_info,
756 blocknr);
Chris Masonbe744172007-05-06 10:15:01 -0400757 if (cache)
758 cache->pinned++;
759 }
Chris Masonf4b9aa82007-03-27 11:05:53 -0400760 } else {
761 err = set_radix_bit(&root->fs_info->pending_del_radix, blocknr);
762 }
Chris Masonbe744172007-05-06 10:15:01 -0400763 BUG_ON(err < 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400764 return 0;
765}
766
Chris Masona28ec192007-03-06 20:08:01 -0500767/*
768 * remove an extent from the root, returns 0 on success
769 */
Chris Masone089f052007-03-16 16:20:31 -0400770static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masone37c9e62007-05-09 20:13:14 -0400771 *root, u64 blocknr, u64 num_blocks, int pin,
772 int mark_free)
Chris Masona28ec192007-03-06 20:08:01 -0500773{
Chris Mason5caf2a02007-04-02 11:20:42 -0400774 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -0400775 struct btrfs_key key;
Chris Mason1261ec42007-03-20 20:35:03 -0400776 struct btrfs_fs_info *info = root->fs_info;
777 struct btrfs_root *extent_root = info->extent_root;
Chris Masona28ec192007-03-06 20:08:01 -0500778 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400779 struct btrfs_extent_item *ei;
Chris Masone2fa7222007-03-12 16:22:34 -0400780 struct btrfs_key ins;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400781 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500782
Chris Masona28ec192007-03-06 20:08:01 -0500783 key.objectid = blocknr;
784 key.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -0400785 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masona28ec192007-03-06 20:08:01 -0500786 key.offset = num_blocks;
787
Chris Masonfbdc7622007-05-30 10:22:12 -0400788 find_free_extent(trans, root, 0, 0, (u64)-1, 0, &ins, 0);
Chris Mason5caf2a02007-04-02 11:20:42 -0400789 path = btrfs_alloc_path();
790 BUG_ON(!path);
791 btrfs_init_path(path);
Chris Mason5f26f772007-04-05 10:38:44 -0400792
Chris Mason5caf2a02007-04-02 11:20:42 -0400793 ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
Chris Masona28ec192007-03-06 20:08:01 -0500794 if (ret) {
Chris Mason2e635a22007-03-21 11:12:56 -0400795 printk("failed to find %Lu\n", key.objectid);
Chris Mason234b63a2007-03-13 10:46:10 -0400796 btrfs_print_tree(extent_root, extent_root->node);
Chris Mason2e635a22007-03-21 11:12:56 -0400797 printk("failed to find %Lu\n", key.objectid);
Chris Masona28ec192007-03-06 20:08:01 -0500798 BUG();
799 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400800 ei = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
Chris Mason123abc82007-03-14 14:14:43 -0400801 struct btrfs_extent_item);
Chris Masona28ec192007-03-06 20:08:01 -0500802 BUG_ON(ei->refs == 0);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400803 refs = btrfs_extent_refs(ei) - 1;
804 btrfs_set_extent_refs(ei, refs);
Chris Mason5caf2a02007-04-02 11:20:42 -0400805 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400806 if (refs == 0) {
Chris Mason1261ec42007-03-20 20:35:03 -0400807 u64 super_blocks_used;
Chris Mason78fae272007-03-25 11:35:08 -0400808
809 if (pin) {
Chris Mason8ef97622007-03-26 10:15:30 -0400810 ret = pin_down_block(root, blocknr, 0);
Chris Mason78fae272007-03-25 11:35:08 -0400811 BUG_ON(ret);
812 }
813
Chris Mason1261ec42007-03-20 20:35:03 -0400814 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
815 btrfs_set_super_blocks_used(info->disk_super,
816 super_blocks_used - num_blocks);
Chris Mason5caf2a02007-04-02 11:20:42 -0400817 ret = btrfs_del_item(trans, extent_root, path);
Chris Masona28ec192007-03-06 20:08:01 -0500818 if (ret)
819 BUG();
Chris Masone37c9e62007-05-09 20:13:14 -0400820 ret = update_block_group(trans, root, blocknr, num_blocks, 0,
Chris Mason1e2677e2007-05-29 16:52:18 -0400821 mark_free, 0);
Chris Mason9078a3e2007-04-26 16:46:15 -0400822 BUG_ON(ret);
Chris Masona28ec192007-03-06 20:08:01 -0500823 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400824 btrfs_free_path(path);
Chris Masone089f052007-03-16 16:20:31 -0400825 finish_current_insert(trans, extent_root);
Chris Masona28ec192007-03-06 20:08:01 -0500826 return ret;
827}
828
829/*
Chris Masonfec577f2007-02-26 10:40:21 -0500830 * find all the blocks marked as pending in the radix tree and remove
831 * them from the extent map
832 */
Chris Masone089f052007-03-16 16:20:31 -0400833static int del_pending_extents(struct btrfs_trans_handle *trans, struct
834 btrfs_root *extent_root)
Chris Masonfec577f2007-02-26 10:40:21 -0500835{
836 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -0400837 int wret;
838 int err = 0;
Chris Mason8ef97622007-03-26 10:15:30 -0400839 unsigned long gang[4];
Chris Masonfec577f2007-02-26 10:40:21 -0500840 int i;
Chris Mason8ef97622007-03-26 10:15:30 -0400841 struct radix_tree_root *pending_radix;
842 struct radix_tree_root *pinned_radix;
Chris Masonbe744172007-05-06 10:15:01 -0400843 struct btrfs_block_group_cache *cache;
Chris Mason8ef97622007-03-26 10:15:30 -0400844
845 pending_radix = &extent_root->fs_info->pending_del_radix;
846 pinned_radix = &extent_root->fs_info->pinned_radix;
Chris Masonfec577f2007-02-26 10:40:21 -0500847
848 while(1) {
Chris Masone37c9e62007-05-09 20:13:14 -0400849 ret = find_first_radix_bit(pending_radix, gang, 0,
Chris Mason8ef97622007-03-26 10:15:30 -0400850 ARRAY_SIZE(gang));
Chris Masonfec577f2007-02-26 10:40:21 -0500851 if (!ret)
852 break;
853 for (i = 0; i < ret; i++) {
Chris Mason8ef97622007-03-26 10:15:30 -0400854 wret = set_radix_bit(pinned_radix, gang[i]);
Chris Masonbe744172007-05-06 10:15:01 -0400855 if (wret == 0) {
Chris Mason5276aed2007-06-11 21:33:38 -0400856 cache =
857 btrfs_lookup_block_group(extent_root->fs_info,
Chris Masonbe744172007-05-06 10:15:01 -0400858 gang[i]);
859 if (cache)
860 cache->pinned++;
861 }
862 if (wret < 0) {
863 printk(KERN_CRIT "set_radix_bit, err %d\n",
864 wret);
865 BUG_ON(wret < 0);
866 }
Chris Mason8ef97622007-03-26 10:15:30 -0400867 wret = clear_radix_bit(pending_radix, gang[i]);
868 BUG_ON(wret);
Chris Masond5719762007-03-23 10:01:08 -0400869 wret = __free_extent(trans, extent_root,
Chris Masone37c9e62007-05-09 20:13:14 -0400870 gang[i], 1, 0, 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400871 if (wret)
872 err = wret;
Chris Masonfec577f2007-02-26 10:40:21 -0500873 }
874 }
Chris Masone20d96d2007-03-22 12:13:20 -0400875 return err;
Chris Masonfec577f2007-02-26 10:40:21 -0500876}
877
878/*
879 * remove an extent from the root, returns 0 on success
880 */
Chris Masone089f052007-03-16 16:20:31 -0400881int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
882 *root, u64 blocknr, u64 num_blocks, int pin)
Chris Masonfec577f2007-02-26 10:40:21 -0500883{
Chris Mason9f5fae22007-03-20 14:38:32 -0400884 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Masonfec577f2007-02-26 10:40:21 -0500885 int pending_ret;
886 int ret;
Chris Masona28ec192007-03-06 20:08:01 -0500887
888 if (root == extent_root) {
Chris Mason8ef97622007-03-26 10:15:30 -0400889 pin_down_block(root, blocknr, 1);
Chris Masona28ec192007-03-06 20:08:01 -0500890 return 0;
891 }
Chris Masone37c9e62007-05-09 20:13:14 -0400892 ret = __free_extent(trans, root, blocknr, num_blocks, pin, pin == 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400893 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
Chris Masonfec577f2007-02-26 10:40:21 -0500894 return ret ? ret : pending_ret;
895}
896
897/*
898 * walks the btree of allocated extents and find a hole of a given size.
899 * The key ins is changed to record the hole:
900 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -0400901 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -0500902 * ins->offset == number of blocks
903 * Any available blocks before search_start are skipped.
904 */
Chris Masone089f052007-03-16 16:20:31 -0400905static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
906 *orig_root, u64 num_blocks, u64 search_start, u64
Chris Masonfbdc7622007-05-30 10:22:12 -0400907 search_end, u64 hint_block,
908 struct btrfs_key *ins, int data)
Chris Masonfec577f2007-02-26 10:40:21 -0500909{
Chris Mason5caf2a02007-04-02 11:20:42 -0400910 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -0400911 struct btrfs_key key;
Chris Masonfec577f2007-02-26 10:40:21 -0500912 int ret;
913 u64 hole_size = 0;
914 int slot = 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400915 u64 last_block = 0;
Chris Mason037e6392007-03-07 11:50:24 -0500916 u64 test_block;
Chris Masonbe744172007-05-06 10:15:01 -0400917 u64 orig_search_start = search_start;
Chris Masonfec577f2007-02-26 10:40:21 -0500918 int start_found;
Chris Mason234b63a2007-03-13 10:46:10 -0400919 struct btrfs_leaf *l;
Chris Mason9f5fae22007-03-20 14:38:32 -0400920 struct btrfs_root * root = orig_root->fs_info->extent_root;
Chris Masonf2458e12007-04-25 15:52:25 -0400921 struct btrfs_fs_info *info = root->fs_info;
Chris Mason0579da42007-03-07 16:15:30 -0500922 int total_needed = num_blocks;
Chris Masonf2458e12007-04-25 15:52:25 -0400923 int total_found = 0;
924 int fill_prealloc = 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400925 int level;
Chris Masonbe08c1b2007-05-03 09:06:49 -0400926 struct btrfs_block_group_cache *block_group;
Chris Masonbe744172007-05-06 10:15:01 -0400927 int full_scan = 0;
Chris Masonfbdc7622007-05-30 10:22:12 -0400928 int wrapped = 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 Masonfbdc7622007-05-30 10:22:12 -0400943 if (hint_block) {
Chris Mason5276aed2007-06-11 21:33:38 -0400944 block_group = btrfs_lookup_block_group(info, hint_block);
Chris Masonbe744172007-05-06 10:15:01 -0400945 block_group = btrfs_find_block_group(root, block_group,
Chris Masonfbdc7622007-05-30 10:22:12 -0400946 hint_block, 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);
Chris Masonfbdc7622007-05-30 10:22:12 -0400957 else if (!full_scan)
Chris Masone37c9e62007-05-09 20:13:14 -0400958 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 Masonfbdc7622007-05-30 10:22:12 -04001042 if (!full_scan && last_block >= block_group->key.objectid +
Chris Masonbe744172007-05-06 10:15:01 -04001043 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 Masonfbdc7622007-05-30 10:22:12 -04001062 if (full_scan) {
1063 ret = -ENOSPC;
1064 goto error;
1065 }
Chris Masonbe744172007-05-06 10:15:01 -04001066 search_start = orig_search_start;
Chris Masonfbdc7622007-05-30 10:22:12 -04001067 if (wrapped)
1068 full_scan = 1;
1069 else
1070 wrapped = 1;
Chris Masonbe744172007-05-06 10:15:01 -04001071 goto new_group;
Chris Mason06a2f9f2007-04-28 08:48:10 -04001072 }
Chris Mason037e6392007-03-07 11:50:24 -05001073 for (test_block = ins->objectid;
Chris Masonf2458e12007-04-25 15:52:25 -04001074 test_block < ins->objectid + num_blocks; test_block++) {
1075 if (test_radix_bit(&info->pinned_radix, test_block)) {
Chris Mason037e6392007-03-07 11:50:24 -05001076 search_start = test_block + 1;
Chris Masonbe744172007-05-06 10:15:01 -04001077 goto new_group;
Chris Masonfec577f2007-02-26 10:40:21 -05001078 }
1079 }
Chris Masonf2458e12007-04-25 15:52:25 -04001080 if (!fill_prealloc && info->extent_tree_insert_nr) {
1081 u64 last =
1082 info->extent_tree_insert[info->extent_tree_insert_nr - 1];
1083 if (ins->objectid + num_blocks >
1084 info->extent_tree_insert[0] &&
1085 ins->objectid <= last) {
1086 search_start = last + 1;
Chris Masone37c9e62007-05-09 20:13:14 -04001087 WARN_ON(!full_scan);
Chris Masonbe744172007-05-06 10:15:01 -04001088 goto new_group;
Chris Masonf2458e12007-04-25 15:52:25 -04001089 }
1090 }
1091 if (!fill_prealloc && info->extent_tree_prealloc_nr) {
1092 u64 first =
1093 info->extent_tree_prealloc[info->extent_tree_prealloc_nr - 1];
1094 if (ins->objectid + num_blocks > first &&
1095 ins->objectid <= info->extent_tree_prealloc[0]) {
1096 search_start = info->extent_tree_prealloc[0] + 1;
Chris Masone37c9e62007-05-09 20:13:14 -04001097 WARN_ON(!full_scan);
Chris Masonbe744172007-05-06 10:15:01 -04001098 goto new_group;
Chris Masonf2458e12007-04-25 15:52:25 -04001099 }
1100 }
1101 if (fill_prealloc) {
1102 int nr;
1103 test_block = ins->objectid;
Chris Masone37c9e62007-05-09 20:13:14 -04001104 if (test_block - info->extent_tree_prealloc[total_needed - 1] >=
1105 leaf_range(root)) {
1106 total_found = 0;
1107 info->extent_tree_prealloc_nr = total_found;
1108 }
Chris Masonf2458e12007-04-25 15:52:25 -04001109 while(test_block < ins->objectid + ins->offset &&
1110 total_found < total_needed) {
1111 nr = total_needed - total_found - 1;
1112 BUG_ON(nr < 0);
Chris Masoncd1bc462007-04-27 10:08:34 -04001113 info->extent_tree_prealloc[nr] = test_block;
Chris Masonf2458e12007-04-25 15:52:25 -04001114 total_found++;
1115 test_block++;
1116 }
1117 if (total_found < total_needed) {
1118 search_start = test_block;
Chris Masonbe744172007-05-06 10:15:01 -04001119 goto new_group;
Chris Masonf2458e12007-04-25 15:52:25 -04001120 }
Chris Masoncd1bc462007-04-27 10:08:34 -04001121 info->extent_tree_prealloc_nr = total_found;
Chris Masonf2458e12007-04-25 15:52:25 -04001122 }
Chris Masone37c9e62007-05-09 20:13:14 -04001123 if (!data) {
Chris Mason5276aed2007-06-11 21:33:38 -04001124 block_group = btrfs_lookup_block_group(info, ins->objectid);
Chris Masone37c9e62007-05-09 20:13:14 -04001125 if (block_group) {
1126 if (fill_prealloc)
1127 block_group->last_prealloc =
1128 info->extent_tree_prealloc[total_needed-1];
1129 else
1130 trans->block_group = block_group;
1131 }
Chris Masoncd1bc462007-04-27 10:08:34 -04001132 }
Chris Mason037e6392007-03-07 11:50:24 -05001133 ins->offset = num_blocks;
Chris Mason5caf2a02007-04-02 11:20:42 -04001134 btrfs_free_path(path);
Chris Masonfec577f2007-02-26 10:40:21 -05001135 return 0;
Chris Masonbe744172007-05-06 10:15:01 -04001136
1137new_group:
Chris Mason3e1ad542007-05-07 20:03:49 -04001138 if (search_start + num_blocks >= search_end) {
Chris Masonbe744172007-05-06 10:15:01 -04001139 search_start = orig_search_start;
Chris Masonfbdc7622007-05-30 10:22:12 -04001140 if (full_scan) {
1141 ret = -ENOSPC;
1142 goto error;
1143 }
1144 if (wrapped)
1145 full_scan = 1;
1146 else
1147 wrapped = 1;
Chris Masonbe744172007-05-06 10:15:01 -04001148 }
Chris Mason5276aed2007-06-11 21:33:38 -04001149 block_group = btrfs_lookup_block_group(info, search_start);
Chris Masonfbdc7622007-05-30 10:22:12 -04001150 cond_resched();
Chris Masonbe744172007-05-06 10:15:01 -04001151 if (!full_scan)
1152 block_group = btrfs_find_block_group(root, block_group,
Chris Masonde428b62007-05-18 13:28:27 -04001153 search_start, data, 0);
Chris Masonbe744172007-05-06 10:15:01 -04001154 goto check_failed;
1155
Chris Mason0f70abe2007-02-28 16:46:22 -05001156error:
Chris Mason5caf2a02007-04-02 11:20:42 -04001157 btrfs_release_path(root, path);
1158 btrfs_free_path(path);
Chris Mason0f70abe2007-02-28 16:46:22 -05001159 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05001160}
Chris Masonfec577f2007-02-26 10:40:21 -05001161/*
Chris Masonfec577f2007-02-26 10:40:21 -05001162 * finds a free extent and does all the dirty work required for allocation
1163 * returns the key for the extent through ins, and a tree buffer for
1164 * the first block of the extent through buf.
1165 *
1166 * returns 0 if everything worked, non-zero otherwise.
1167 */
Chris Mason4d775672007-04-20 20:23:12 -04001168int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
1169 struct btrfs_root *root, u64 owner,
Chris Masonfbdc7622007-05-30 10:22:12 -04001170 u64 num_blocks, u64 hint_block,
Chris Masonbe08c1b2007-05-03 09:06:49 -04001171 u64 search_end, struct btrfs_key *ins, int data)
Chris Masonfec577f2007-02-26 10:40:21 -05001172{
1173 int ret;
1174 int pending_ret;
Chris Mason1261ec42007-03-20 20:35:03 -04001175 u64 super_blocks_used;
Chris Masonfbdc7622007-05-30 10:22:12 -04001176 u64 search_start = 0;
Chris Mason1261ec42007-03-20 20:35:03 -04001177 struct btrfs_fs_info *info = root->fs_info;
1178 struct btrfs_root *extent_root = info->extent_root;
Chris Mason234b63a2007-03-13 10:46:10 -04001179 struct btrfs_extent_item extent_item;
Chris Masonf2458e12007-04-25 15:52:25 -04001180 struct btrfs_key prealloc_key;
Chris Mason037e6392007-03-07 11:50:24 -05001181
Chris Masoncf27e1e2007-03-13 09:49:06 -04001182 btrfs_set_extent_refs(&extent_item, 1);
Chris Mason4d775672007-04-20 20:23:12 -04001183 btrfs_set_extent_owner(&extent_item, owner);
Chris Masonfec577f2007-02-26 10:40:21 -05001184
Chris Mason037e6392007-03-07 11:50:24 -05001185 if (root == extent_root) {
Chris Masonf2458e12007-04-25 15:52:25 -04001186 int nr;
1187 BUG_ON(info->extent_tree_prealloc_nr == 0);
Chris Mason037e6392007-03-07 11:50:24 -05001188 BUG_ON(num_blocks != 1);
Chris Mason037e6392007-03-07 11:50:24 -05001189 ins->offset = 1;
Chris Masonf2458e12007-04-25 15:52:25 -04001190 info->extent_tree_prealloc_nr--;
1191 nr = info->extent_tree_prealloc_nr;
1192 ins->objectid = info->extent_tree_prealloc[nr];
1193 info->extent_tree_insert[info->extent_tree_insert_nr++] =
1194 ins->objectid;
Chris Mason9078a3e2007-04-26 16:46:15 -04001195 ret = update_block_group(trans, root,
Chris Mason1e2677e2007-05-29 16:52:18 -04001196 ins->objectid, ins->offset, 1, 0, 0);
Chris Mason9078a3e2007-04-26 16:46:15 -04001197 BUG_ON(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05001198 return 0;
1199 }
Chris Masone37c9e62007-05-09 20:13:14 -04001200
1201 /*
1202 * if we're doing a data allocation, preallocate room in the
1203 * extent tree first. This way the extent tree blocks end up
1204 * in the correct block group.
1205 */
1206 if (data) {
Chris Masonde428b62007-05-18 13:28:27 -04001207 ret = find_free_extent(trans, root, 0, 0,
Chris Masonfbdc7622007-05-30 10:22:12 -04001208 search_end, 0, &prealloc_key, 0);
Chris Masone37c9e62007-05-09 20:13:14 -04001209 if (ret) {
1210 return ret;
1211 }
1212 if (prealloc_key.objectid + prealloc_key.offset >= search_end) {
1213 int nr = info->extent_tree_prealloc_nr;
1214 search_end = info->extent_tree_prealloc[nr - 1] - 1;
1215 } else {
1216 search_start = info->extent_tree_prealloc[0] + 1;
1217 }
1218 }
Chris Masonfbdc7622007-05-30 10:22:12 -04001219 if (hint_block < search_start)
1220 hint_block = search_start;
Chris Masonf2458e12007-04-25 15:52:25 -04001221 /* do the real allocation */
Chris Masone089f052007-03-16 16:20:31 -04001222 ret = find_free_extent(trans, root, num_blocks, search_start,
Chris Masonfbdc7622007-05-30 10:22:12 -04001223 search_end, hint_block, ins, data);
Chris Masone37c9e62007-05-09 20:13:14 -04001224 if (ret) {
Chris Mason037e6392007-03-07 11:50:24 -05001225 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -04001226 }
Chris Masonfec577f2007-02-26 10:40:21 -05001227
Chris Masone37c9e62007-05-09 20:13:14 -04001228 /*
1229 * if we're doing a metadata allocation, preallocate space in the
1230 * extent tree second. This way, we don't create a tiny hole
1231 * in the allocation map between any unused preallocation blocks
1232 * and the metadata block we're actually allocating. On disk,
1233 * it'll go:
1234 * [block we've allocated], [used prealloc 1], [ unused prealloc ]
1235 * The unused prealloc will get reused the next time around.
1236 */
1237 if (!data) {
1238 if (ins->objectid + ins->offset >= search_end)
1239 search_end = ins->objectid - 1;
1240 else
1241 search_start = ins->objectid + ins->offset;
Chris Mason3e1ad542007-05-07 20:03:49 -04001242
Chris Masonfbdc7622007-05-30 10:22:12 -04001243 if (hint_block < search_start)
1244 hint_block = search_start;
1245
Chris Masone37c9e62007-05-09 20:13:14 -04001246 ret = find_free_extent(trans, root, 0, search_start,
Chris Masonfbdc7622007-05-30 10:22:12 -04001247 search_end, hint_block,
1248 &prealloc_key, 0);
Chris Masone37c9e62007-05-09 20:13:14 -04001249 if (ret) {
1250 return ret;
1251 }
1252 }
Chris Masonf2458e12007-04-25 15:52:25 -04001253
Chris Mason1261ec42007-03-20 20:35:03 -04001254 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
1255 btrfs_set_super_blocks_used(info->disk_super, super_blocks_used +
1256 num_blocks);
Chris Masone089f052007-03-16 16:20:31 -04001257 ret = btrfs_insert_item(trans, extent_root, ins, &extent_item,
1258 sizeof(extent_item));
Chris Mason037e6392007-03-07 11:50:24 -05001259
Chris Masone089f052007-03-16 16:20:31 -04001260 finish_current_insert(trans, extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -04001261 pending_ret = del_pending_extents(trans, extent_root);
Chris Masone37c9e62007-05-09 20:13:14 -04001262 if (ret) {
Chris Mason037e6392007-03-07 11:50:24 -05001263 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -04001264 }
1265 if (pending_ret) {
Chris Mason037e6392007-03-07 11:50:24 -05001266 return pending_ret;
Chris Masone37c9e62007-05-09 20:13:14 -04001267 }
Chris Mason1e2677e2007-05-29 16:52:18 -04001268 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0,
1269 data);
Chris Masonfabb5682007-06-07 22:13:21 -04001270 BUG_ON(ret);
Chris Mason037e6392007-03-07 11:50:24 -05001271 return 0;
Chris Masonfec577f2007-02-26 10:40:21 -05001272}
1273
1274/*
1275 * helper function to allocate a block for a given tree
1276 * returns the tree buffer or NULL.
1277 */
Chris Masone20d96d2007-03-22 12:13:20 -04001278struct buffer_head *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Chris Mason31f3c992007-04-30 15:25:45 -04001279 struct btrfs_root *root, u64 hint)
Chris Masonfec577f2007-02-26 10:40:21 -05001280{
Chris Masone2fa7222007-03-12 16:22:34 -04001281 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -05001282 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -04001283 struct buffer_head *buf;
Chris Masonfec577f2007-02-26 10:40:21 -05001284
Chris Mason4d775672007-04-20 20:23:12 -04001285 ret = btrfs_alloc_extent(trans, root, root->root_key.objectid,
Chris Masonde428b62007-05-18 13:28:27 -04001286 1, hint, (unsigned long)-1, &ins, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05001287 if (ret) {
1288 BUG();
1289 return NULL;
1290 }
Chris Mason9078a3e2007-04-26 16:46:15 -04001291 BUG_ON(ret);
Chris Masond98237b2007-03-28 13:57:48 -04001292 buf = btrfs_find_create_tree_block(root, ins.objectid);
Chris Masondf2ce342007-03-23 11:00:45 -04001293 set_buffer_uptodate(buf);
Chris Mason090d1872007-05-01 08:53:32 -04001294 set_buffer_checked(buf);
Chris Mason7c4452b2007-04-28 09:29:35 -04001295 set_radix_bit(&trans->transaction->dirty_pages, buf->b_page->index);
Chris Masonfec577f2007-02-26 10:40:21 -05001296 return buf;
1297}
Chris Masona28ec192007-03-06 20:08:01 -05001298
Chris Mason6407bf62007-03-27 06:33:00 -04001299static int drop_leaf_ref(struct btrfs_trans_handle *trans,
1300 struct btrfs_root *root, struct buffer_head *cur)
1301{
1302 struct btrfs_disk_key *key;
1303 struct btrfs_leaf *leaf;
1304 struct btrfs_file_extent_item *fi;
1305 int i;
1306 int nritems;
1307 int ret;
1308
1309 BUG_ON(!btrfs_is_leaf(btrfs_buffer_node(cur)));
1310 leaf = btrfs_buffer_leaf(cur);
1311 nritems = btrfs_header_nritems(&leaf->header);
1312 for (i = 0; i < nritems; i++) {
Chris Mason3a686372007-05-24 13:35:57 -04001313 u64 disk_blocknr;
Chris Mason6407bf62007-03-27 06:33:00 -04001314 key = &leaf->items[i].key;
1315 if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY)
1316 continue;
1317 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
Chris Mason236454d2007-04-19 13:37:44 -04001318 if (btrfs_file_extent_type(fi) == BTRFS_FILE_EXTENT_INLINE)
1319 continue;
Chris Mason6407bf62007-03-27 06:33:00 -04001320 /*
1321 * FIXME make sure to insert a trans record that
1322 * repeats the snapshot del on crash
1323 */
Chris Mason3a686372007-05-24 13:35:57 -04001324 disk_blocknr = btrfs_file_extent_disk_blocknr(fi);
1325 if (disk_blocknr == 0)
1326 continue;
1327 ret = btrfs_free_extent(trans, root, disk_blocknr,
Chris Mason6407bf62007-03-27 06:33:00 -04001328 btrfs_file_extent_disk_num_blocks(fi),
1329 0);
1330 BUG_ON(ret);
1331 }
1332 return 0;
1333}
1334
Chris Mason9aca1d52007-03-13 11:09:37 -04001335/*
1336 * helper function for drop_snapshot, this walks down the tree dropping ref
1337 * counts as it goes.
1338 */
Chris Masone089f052007-03-16 16:20:31 -04001339static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
1340 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05001341{
Chris Masone20d96d2007-03-22 12:13:20 -04001342 struct buffer_head *next;
1343 struct buffer_head *cur;
Chris Mason20524f02007-03-10 06:35:47 -05001344 u64 blocknr;
1345 int ret;
1346 u32 refs;
1347
Chris Mason5caf2a02007-04-02 11:20:42 -04001348 WARN_ON(*level < 0);
1349 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Masonb18c6682007-04-17 13:26:50 -04001350 ret = lookup_extent_ref(trans, root, bh_blocknr(path->nodes[*level]),
Chris Mason6407bf62007-03-27 06:33:00 -04001351 1, &refs);
Chris Mason20524f02007-03-10 06:35:47 -05001352 BUG_ON(ret);
1353 if (refs > 1)
1354 goto out;
Chris Mason9aca1d52007-03-13 11:09:37 -04001355 /*
1356 * walk down to the last node level and free all the leaves
1357 */
Chris Mason6407bf62007-03-27 06:33:00 -04001358 while(*level >= 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001359 WARN_ON(*level < 0);
1360 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason20524f02007-03-10 06:35:47 -05001361 cur = path->nodes[*level];
Chris Mason2c90e5d2007-04-02 10:50:19 -04001362 if (btrfs_header_level(btrfs_buffer_header(cur)) != *level)
1363 WARN_ON(1);
Chris Mason7518a232007-03-12 12:01:18 -04001364 if (path->slots[*level] >=
Chris Masone20d96d2007-03-22 12:13:20 -04001365 btrfs_header_nritems(btrfs_buffer_header(cur)))
Chris Mason20524f02007-03-10 06:35:47 -05001366 break;
Chris Mason6407bf62007-03-27 06:33:00 -04001367 if (*level == 0) {
1368 ret = drop_leaf_ref(trans, root, cur);
1369 BUG_ON(ret);
1370 break;
1371 }
Chris Masone20d96d2007-03-22 12:13:20 -04001372 blocknr = btrfs_node_blockptr(btrfs_buffer_node(cur),
1373 path->slots[*level]);
Chris Masonb18c6682007-04-17 13:26:50 -04001374 ret = lookup_extent_ref(trans, root, blocknr, 1, &refs);
Chris Mason6407bf62007-03-27 06:33:00 -04001375 BUG_ON(ret);
1376 if (refs != 1) {
Chris Mason20524f02007-03-10 06:35:47 -05001377 path->slots[*level]++;
Chris Masone089f052007-03-16 16:20:31 -04001378 ret = btrfs_free_extent(trans, root, blocknr, 1, 1);
Chris Mason20524f02007-03-10 06:35:47 -05001379 BUG_ON(ret);
1380 continue;
1381 }
Chris Mason20524f02007-03-10 06:35:47 -05001382 next = read_tree_block(root, blocknr);
Chris Mason5caf2a02007-04-02 11:20:42 -04001383 WARN_ON(*level <= 0);
Chris Mason83e15a22007-03-12 09:03:27 -04001384 if (path->nodes[*level-1])
Chris Mason234b63a2007-03-13 10:46:10 -04001385 btrfs_block_release(root, path->nodes[*level-1]);
Chris Mason20524f02007-03-10 06:35:47 -05001386 path->nodes[*level-1] = next;
Chris Masone20d96d2007-03-22 12:13:20 -04001387 *level = btrfs_header_level(btrfs_buffer_header(next));
Chris Mason20524f02007-03-10 06:35:47 -05001388 path->slots[*level] = 0;
1389 }
1390out:
Chris Mason5caf2a02007-04-02 11:20:42 -04001391 WARN_ON(*level < 0);
1392 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason6407bf62007-03-27 06:33:00 -04001393 ret = btrfs_free_extent(trans, root,
Chris Mason7eccb902007-04-11 15:53:25 -04001394 bh_blocknr(path->nodes[*level]), 1, 1);
Chris Mason234b63a2007-03-13 10:46:10 -04001395 btrfs_block_release(root, path->nodes[*level]);
Chris Mason20524f02007-03-10 06:35:47 -05001396 path->nodes[*level] = NULL;
1397 *level += 1;
1398 BUG_ON(ret);
1399 return 0;
1400}
1401
Chris Mason9aca1d52007-03-13 11:09:37 -04001402/*
1403 * helper for dropping snapshots. This walks back up the tree in the path
1404 * to find the first node higher up where we haven't yet gone through
1405 * all the slots
1406 */
Chris Masone089f052007-03-16 16:20:31 -04001407static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root
1408 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05001409{
1410 int i;
1411 int slot;
1412 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -04001413 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Mason20524f02007-03-10 06:35:47 -05001414 slot = path->slots[i];
Chris Masone20d96d2007-03-22 12:13:20 -04001415 if (slot < btrfs_header_nritems(
1416 btrfs_buffer_header(path->nodes[i])) - 1) {
Chris Mason20524f02007-03-10 06:35:47 -05001417 path->slots[i]++;
1418 *level = i;
1419 return 0;
1420 } else {
Chris Masone089f052007-03-16 16:20:31 -04001421 ret = btrfs_free_extent(trans, root,
Chris Mason7eccb902007-04-11 15:53:25 -04001422 bh_blocknr(path->nodes[*level]),
Chris Masone089f052007-03-16 16:20:31 -04001423 1, 1);
Chris Mason6407bf62007-03-27 06:33:00 -04001424 BUG_ON(ret);
Chris Mason234b63a2007-03-13 10:46:10 -04001425 btrfs_block_release(root, path->nodes[*level]);
Chris Mason83e15a22007-03-12 09:03:27 -04001426 path->nodes[*level] = NULL;
Chris Mason20524f02007-03-10 06:35:47 -05001427 *level = i + 1;
Chris Mason20524f02007-03-10 06:35:47 -05001428 }
1429 }
1430 return 1;
1431}
1432
Chris Mason9aca1d52007-03-13 11:09:37 -04001433/*
1434 * drop the reference count on the tree rooted at 'snap'. This traverses
1435 * the tree freeing any blocks that have a ref count of zero after being
1436 * decremented.
1437 */
Chris Masone089f052007-03-16 16:20:31 -04001438int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masone20d96d2007-03-22 12:13:20 -04001439 *root, struct buffer_head *snap)
Chris Mason20524f02007-03-10 06:35:47 -05001440{
Chris Mason3768f362007-03-13 16:47:54 -04001441 int ret = 0;
Chris Mason9aca1d52007-03-13 11:09:37 -04001442 int wret;
Chris Mason20524f02007-03-10 06:35:47 -05001443 int level;
Chris Mason5caf2a02007-04-02 11:20:42 -04001444 struct btrfs_path *path;
Chris Mason20524f02007-03-10 06:35:47 -05001445 int i;
1446 int orig_level;
1447
Chris Mason5caf2a02007-04-02 11:20:42 -04001448 path = btrfs_alloc_path();
1449 BUG_ON(!path);
1450 btrfs_init_path(path);
Chris Mason20524f02007-03-10 06:35:47 -05001451
Chris Masone20d96d2007-03-22 12:13:20 -04001452 level = btrfs_header_level(btrfs_buffer_header(snap));
Chris Mason20524f02007-03-10 06:35:47 -05001453 orig_level = level;
Chris Mason5caf2a02007-04-02 11:20:42 -04001454 path->nodes[level] = snap;
1455 path->slots[level] = 0;
Chris Mason20524f02007-03-10 06:35:47 -05001456 while(1) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001457 wret = walk_down_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04001458 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05001459 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04001460 if (wret < 0)
1461 ret = wret;
1462
Chris Mason5caf2a02007-04-02 11:20:42 -04001463 wret = walk_up_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04001464 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05001465 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04001466 if (wret < 0)
1467 ret = wret;
Chris Mason35b7e472007-05-02 15:53:43 -04001468 btrfs_btree_balance_dirty(root);
Chris Mason20524f02007-03-10 06:35:47 -05001469 }
Chris Mason83e15a22007-03-12 09:03:27 -04001470 for (i = 0; i <= orig_level; i++) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001471 if (path->nodes[i]) {
1472 btrfs_block_release(root, path->nodes[i]);
Chris Mason83e15a22007-03-12 09:03:27 -04001473 }
Chris Mason20524f02007-03-10 06:35:47 -05001474 }
Chris Mason5caf2a02007-04-02 11:20:42 -04001475 btrfs_free_path(path);
Chris Mason9aca1d52007-03-13 11:09:37 -04001476 return ret;
Chris Mason20524f02007-03-10 06:35:47 -05001477}
Chris Mason9078a3e2007-04-26 16:46:15 -04001478
Chris Masonbe744172007-05-06 10:15:01 -04001479static int free_block_group_radix(struct radix_tree_root *radix)
Chris Mason9078a3e2007-04-26 16:46:15 -04001480{
1481 int ret;
1482 struct btrfs_block_group_cache *cache[8];
1483 int i;
1484
1485 while(1) {
Chris Masonbe744172007-05-06 10:15:01 -04001486 ret = radix_tree_gang_lookup(radix, (void **)cache, 0,
Chris Mason9078a3e2007-04-26 16:46:15 -04001487 ARRAY_SIZE(cache));
1488 if (!ret)
1489 break;
1490 for (i = 0; i < ret; i++) {
Chris Masonbe744172007-05-06 10:15:01 -04001491 radix_tree_delete(radix, cache[i]->key.objectid +
Chris Mason9078a3e2007-04-26 16:46:15 -04001492 cache[i]->key.offset - 1);
1493 kfree(cache[i]);
1494 }
1495 }
1496 return 0;
1497}
1498
Chris Masonbe744172007-05-06 10:15:01 -04001499int btrfs_free_block_groups(struct btrfs_fs_info *info)
1500{
1501 int ret;
1502 int ret2;
Chris Masone37c9e62007-05-09 20:13:14 -04001503 unsigned long gang[16];
1504 int i;
Chris Masonbe744172007-05-06 10:15:01 -04001505
1506 ret = free_block_group_radix(&info->block_group_radix);
1507 ret2 = free_block_group_radix(&info->block_group_data_radix);
1508 if (ret)
1509 return ret;
1510 if (ret2)
1511 return ret2;
Chris Masone37c9e62007-05-09 20:13:14 -04001512
1513 while(1) {
1514 ret = find_first_radix_bit(&info->extent_map_radix,
1515 gang, 0, ARRAY_SIZE(gang));
1516 if (!ret)
1517 break;
1518 for (i = 0; i < ret; i++) {
1519 clear_radix_bit(&info->extent_map_radix, gang[i]);
1520 }
1521 }
Chris Masonbe744172007-05-06 10:15:01 -04001522 return 0;
1523}
1524
Chris Mason9078a3e2007-04-26 16:46:15 -04001525int btrfs_read_block_groups(struct btrfs_root *root)
1526{
1527 struct btrfs_path *path;
1528 int ret;
1529 int err = 0;
1530 struct btrfs_block_group_item *bi;
1531 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04001532 struct btrfs_fs_info *info = root->fs_info;
1533 struct radix_tree_root *radix;
Chris Mason9078a3e2007-04-26 16:46:15 -04001534 struct btrfs_key key;
1535 struct btrfs_key found_key;
1536 struct btrfs_leaf *leaf;
1537 u64 group_size_blocks = BTRFS_BLOCK_GROUP_SIZE / root->blocksize;
Chris Mason31f3c992007-04-30 15:25:45 -04001538 u64 used;
Chris Mason9078a3e2007-04-26 16:46:15 -04001539
Chris Masonbe744172007-05-06 10:15:01 -04001540 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04001541 key.objectid = 0;
1542 key.offset = group_size_blocks;
1543 key.flags = 0;
1544 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
1545
1546 path = btrfs_alloc_path();
1547 if (!path)
1548 return -ENOMEM;
1549
1550 while(1) {
Chris Masonbe744172007-05-06 10:15:01 -04001551 ret = btrfs_search_slot(NULL, info->extent_root,
Chris Mason9078a3e2007-04-26 16:46:15 -04001552 &key, path, 0, 0);
1553 if (ret != 0) {
1554 err = ret;
1555 break;
1556 }
1557 leaf = btrfs_buffer_leaf(path->nodes[0]);
1558 btrfs_disk_key_to_cpu(&found_key,
1559 &leaf->items[path->slots[0]].key);
1560 cache = kmalloc(sizeof(*cache), GFP_NOFS);
1561 if (!cache) {
1562 err = -1;
1563 break;
1564 }
Chris Mason3e1ad542007-05-07 20:03:49 -04001565
Chris Mason9078a3e2007-04-26 16:46:15 -04001566 bi = btrfs_item_ptr(leaf, path->slots[0],
1567 struct btrfs_block_group_item);
Chris Mason1e2677e2007-05-29 16:52:18 -04001568 if (bi->flags & BTRFS_BLOCK_GROUP_DATA) {
1569 radix = &info->block_group_data_radix;
1570 cache->data = 1;
1571 } else {
1572 radix = &info->block_group_radix;
1573 cache->data = 0;
1574 }
1575
Chris Mason9078a3e2007-04-26 16:46:15 -04001576 memcpy(&cache->item, bi, sizeof(*bi));
1577 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Mason31f3c992007-04-30 15:25:45 -04001578 cache->last_alloc = cache->key.objectid;
1579 cache->first_free = cache->key.objectid;
Chris Masone37c9e62007-05-09 20:13:14 -04001580 cache->last_prealloc = cache->key.objectid;
Chris Masonbe744172007-05-06 10:15:01 -04001581 cache->pinned = 0;
Chris Masone37c9e62007-05-09 20:13:14 -04001582 cache->cached = 0;
1583
Chris Mason3e1ad542007-05-07 20:03:49 -04001584 cache->radix = radix;
1585
Chris Mason9078a3e2007-04-26 16:46:15 -04001586 key.objectid = found_key.objectid + found_key.offset;
1587 btrfs_release_path(root, path);
Chris Masonbe744172007-05-06 10:15:01 -04001588 ret = radix_tree_insert(radix, found_key.objectid +
Chris Mason9078a3e2007-04-26 16:46:15 -04001589 found_key.offset - 1,
1590 (void *)cache);
1591 BUG_ON(ret);
Chris Mason31f3c992007-04-30 15:25:45 -04001592 used = btrfs_block_group_used(bi);
Chris Masonbe744172007-05-06 10:15:01 -04001593 if (used < (key.offset * 8) / 10) {
1594 radix_tree_tag_set(radix, found_key.objectid +
Chris Mason31f3c992007-04-30 15:25:45 -04001595 found_key.offset - 1,
1596 BTRFS_BLOCK_GROUP_AVAIL);
1597 }
Chris Mason9078a3e2007-04-26 16:46:15 -04001598 if (key.objectid >=
Chris Masonbe744172007-05-06 10:15:01 -04001599 btrfs_super_total_blocks(info->disk_super))
Chris Mason9078a3e2007-04-26 16:46:15 -04001600 break;
1601 }
1602
1603 btrfs_free_path(path);
1604 return 0;
1605}