blob: fe02fbfa6d59a745dd2b4f5a6be326553cd99217 [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;
Chris Masone37c9e62007-05-09 20:13:14 -040068 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 Mason5276aed2007-06-11 21:33:38 -0400137struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
138 btrfs_fs_info *info,
139 u64 blocknr)
Chris Masonbe744172007-05-06 10:15:01 -0400140{
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 Masonbe744172007-05-06 10:15:01 -0400160 return NULL;
161}
162
Chris Masone37c9e62007-05-09 20:13:14 -0400163static u64 leaf_range(struct btrfs_root *root)
164{
165 u64 size = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason84f54cf2007-06-12 07:43:08 -0400166 do_div(size, sizeof(struct btrfs_extent_item) +
167 sizeof(struct btrfs_item));
Chris Masone37c9e62007-05-09 20:13:14 -0400168 return size;
169}
170
171static u64 find_search_start(struct btrfs_root *root,
172 struct btrfs_block_group_cache **cache_ret,
173 u64 search_start, int num)
174{
175 unsigned long gang[8];
176 int ret;
177 struct btrfs_block_group_cache *cache = *cache_ret;
178 u64 last = max(search_start, cache->key.objectid);
179
180 if (cache->data)
181 goto out;
182 if (num > 1) {
183 last = max(last, cache->last_prealloc);
184 }
185again:
186 cache_block_group(root, cache);
187 while(1) {
188 ret = find_first_radix_bit(&root->fs_info->extent_map_radix,
189 gang, last, ARRAY_SIZE(gang));
190 if (!ret)
191 goto out;
192 last = gang[ret-1] + 1;
193 if (num > 1) {
194 if (ret != ARRAY_SIZE(gang)) {
195 goto new_group;
196 }
197 if (gang[ret-1] - gang[0] > leaf_range(root)) {
198 continue;
199 }
200 }
201 if (gang[0] >= cache->key.objectid + cache->key.offset) {
202 goto new_group;
203 }
204 return gang[0];
205 }
206out:
207 return max(cache->last_alloc, search_start);
208
209new_group:
Chris Mason5276aed2007-06-11 21:33:38 -0400210 cache = btrfs_lookup_block_group(root->fs_info,
211 last + cache->key.offset - 1);
Chris Masone37c9e62007-05-09 20:13:14 -0400212 if (!cache) {
213 return max((*cache_ret)->last_alloc, search_start);
214 }
215 cache = btrfs_find_block_group(root, cache,
Chris Masonde428b62007-05-18 13:28:27 -0400216 last + cache->key.offset - 1, 0, 0);
Chris Masone37c9e62007-05-09 20:13:14 -0400217 *cache_ret = cache;
218 goto again;
219}
220
Chris Mason84f54cf2007-06-12 07:43:08 -0400221static u64 div_factor(u64 num, int factor)
222{
223 num *= factor;
224 do_div(num, 10);
225 return num;
226}
227
Chris Mason31f3c992007-04-30 15:25:45 -0400228struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
229 struct btrfs_block_group_cache
Chris Masonbe744172007-05-06 10:15:01 -0400230 *hint, u64 search_start,
Chris Masonde428b62007-05-18 13:28:27 -0400231 int data, int owner)
Chris Masoncd1bc462007-04-27 10:08:34 -0400232{
233 struct btrfs_block_group_cache *cache[8];
Chris Mason31f3c992007-04-30 15:25:45 -0400234 struct btrfs_block_group_cache *found_group = NULL;
Chris Masoncd1bc462007-04-27 10:08:34 -0400235 struct btrfs_fs_info *info = root->fs_info;
Chris Masonbe744172007-05-06 10:15:01 -0400236 struct radix_tree_root *radix;
Chris Mason1e2677e2007-05-29 16:52:18 -0400237 struct radix_tree_root *swap_radix;
Chris Masoncd1bc462007-04-27 10:08:34 -0400238 u64 used;
Chris Mason31f3c992007-04-30 15:25:45 -0400239 u64 last = 0;
240 u64 hint_last;
Chris Masoncd1bc462007-04-27 10:08:34 -0400241 int i;
242 int ret;
Chris Mason31f3c992007-04-30 15:25:45 -0400243 int full_search = 0;
Chris Masonde428b62007-05-18 13:28:27 -0400244 int factor = 8;
Chris Mason1e2677e2007-05-29 16:52:18 -0400245 int data_swap = 0;
Chris Masonde428b62007-05-18 13:28:27 -0400246
247 if (!owner)
248 factor = 5;
Chris Masonbe744172007-05-06 10:15:01 -0400249
Chris Mason1e2677e2007-05-29 16:52:18 -0400250 if (data) {
Chris Masonbe744172007-05-06 10:15:01 -0400251 radix = &info->block_group_data_radix;
Chris Mason1e2677e2007-05-29 16:52:18 -0400252 swap_radix = &info->block_group_radix;
253 } else {
Chris Masonbe744172007-05-06 10:15:01 -0400254 radix = &info->block_group_radix;
Chris Mason1e2677e2007-05-29 16:52:18 -0400255 swap_radix = &info->block_group_data_radix;
256 }
Chris Masonbe744172007-05-06 10:15:01 -0400257
258 if (search_start) {
259 struct btrfs_block_group_cache *shint;
Chris Mason5276aed2007-06-11 21:33:38 -0400260 shint = btrfs_lookup_block_group(info, search_start);
Chris Masonbe744172007-05-06 10:15:01 -0400261 if (shint->data == data) {
262 used = btrfs_block_group_used(&shint->item);
263 if (used + shint->pinned <
Chris Mason84f54cf2007-06-12 07:43:08 -0400264 div_factor(shint->key.offset, factor)) {
Chris Masonbe744172007-05-06 10:15:01 -0400265 return shint;
266 }
267 }
268 }
269 if (hint && hint->data == data) {
Chris Mason31f3c992007-04-30 15:25:45 -0400270 used = btrfs_block_group_used(&hint->item);
Chris Mason84f54cf2007-06-12 07:43:08 -0400271 if (used + hint->pinned <
272 div_factor(hint->key.offset, factor)) {
Chris Mason31f3c992007-04-30 15:25:45 -0400273 return hint;
274 }
Chris Mason84f54cf2007-06-12 07:43:08 -0400275 if (used >= div_factor(hint->key.offset, 8)) {
Chris Masonbe744172007-05-06 10:15:01 -0400276 radix_tree_tag_clear(radix,
277 hint->key.objectid +
278 hint->key.offset - 1,
279 BTRFS_BLOCK_GROUP_AVAIL);
280 }
Chris Mason8d7be552007-05-10 11:24:42 -0400281 last = hint->key.offset * 3;
Chris Masonbe744172007-05-06 10:15:01 -0400282 if (hint->key.objectid >= last)
Chris Masone37c9e62007-05-09 20:13:14 -0400283 last = max(search_start + hint->key.offset - 1,
284 hint->key.objectid - last);
Chris Masonbe744172007-05-06 10:15:01 -0400285 else
286 last = hint->key.objectid + hint->key.offset;
Chris Mason31f3c992007-04-30 15:25:45 -0400287 hint_last = last;
288 } else {
Chris Masone37c9e62007-05-09 20:13:14 -0400289 if (hint)
290 hint_last = max(hint->key.objectid, search_start);
291 else
292 hint_last = search_start;
293
294 last = hint_last;
Chris Mason31f3c992007-04-30 15:25:45 -0400295 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400296 while(1) {
Chris Masonbe744172007-05-06 10:15:01 -0400297 ret = radix_tree_gang_lookup_tag(radix, (void **)cache,
Chris Masoncd1bc462007-04-27 10:08:34 -0400298 last, ARRAY_SIZE(cache),
Chris Mason31f3c992007-04-30 15:25:45 -0400299 BTRFS_BLOCK_GROUP_AVAIL);
Chris Masoncd1bc462007-04-27 10:08:34 -0400300 if (!ret)
301 break;
302 for (i = 0; i < ret; i++) {
Chris Masonbe08c1b2007-05-03 09:06:49 -0400303 last = cache[i]->key.objectid +
304 cache[i]->key.offset;
Chris Masoncd1bc462007-04-27 10:08:34 -0400305 used = btrfs_block_group_used(&cache[i]->item);
Chris Masonbe744172007-05-06 10:15:01 -0400306 if (used + cache[i]->pinned <
Chris Mason84f54cf2007-06-12 07:43:08 -0400307 div_factor(cache[i]->key.offset, factor)) {
Chris Mason31f3c992007-04-30 15:25:45 -0400308 found_group = cache[i];
309 goto found;
Chris Masoncd1bc462007-04-27 10:08:34 -0400310 }
Chris Mason84f54cf2007-06-12 07:43:08 -0400311 if (used >= div_factor(cache[i]->key.offset, 8)) {
Chris Masonbe744172007-05-06 10:15:01 -0400312 radix_tree_tag_clear(radix,
313 cache[i]->key.objectid +
314 cache[i]->key.offset - 1,
315 BTRFS_BLOCK_GROUP_AVAIL);
316 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400317 }
Chris Masonde428b62007-05-18 13:28:27 -0400318 cond_resched();
Chris Masoncd1bc462007-04-27 10:08:34 -0400319 }
Chris Mason31f3c992007-04-30 15:25:45 -0400320 last = hint_last;
321again:
Chris Masoncd1bc462007-04-27 10:08:34 -0400322 while(1) {
Chris Masonbe744172007-05-06 10:15:01 -0400323 ret = radix_tree_gang_lookup(radix, (void **)cache,
324 last, ARRAY_SIZE(cache));
Chris Masoncd1bc462007-04-27 10:08:34 -0400325 if (!ret)
326 break;
327 for (i = 0; i < ret; i++) {
Chris Masonbe08c1b2007-05-03 09:06:49 -0400328 last = cache[i]->key.objectid +
329 cache[i]->key.offset;
Chris Masoncd1bc462007-04-27 10:08:34 -0400330 used = btrfs_block_group_used(&cache[i]->item);
Chris Masonbe744172007-05-06 10:15:01 -0400331 if (used + cache[i]->pinned < cache[i]->key.offset) {
Chris Mason31f3c992007-04-30 15:25:45 -0400332 found_group = cache[i];
333 goto found;
Chris Masoncd1bc462007-04-27 10:08:34 -0400334 }
Chris Masonbe744172007-05-06 10:15:01 -0400335 if (used >= cache[i]->key.offset) {
336 radix_tree_tag_clear(radix,
337 cache[i]->key.objectid +
338 cache[i]->key.offset - 1,
339 BTRFS_BLOCK_GROUP_AVAIL);
340 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400341 }
Chris Masonde428b62007-05-18 13:28:27 -0400342 cond_resched();
Chris Masoncd1bc462007-04-27 10:08:34 -0400343 }
Chris Mason31f3c992007-04-30 15:25:45 -0400344 if (!full_search) {
Chris Masonbe744172007-05-06 10:15:01 -0400345 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -0400346 full_search = 1;
347 goto again;
348 }
Chris Mason1e2677e2007-05-29 16:52:18 -0400349 if (!data_swap) {
350 struct radix_tree_root *tmp = radix;
351 data_swap = 1;
352 radix = swap_radix;
353 swap_radix = tmp;
354 last = search_start;
355 goto again;
356 }
Chris Mason31f3c992007-04-30 15:25:45 -0400357 if (!found_group) {
Chris Masonbe744172007-05-06 10:15:01 -0400358 ret = radix_tree_gang_lookup(radix,
Chris Mason31f3c992007-04-30 15:25:45 -0400359 (void **)&found_group, 0, 1);
Chris Mason1e2677e2007-05-29 16:52:18 -0400360 if (ret == 0) {
361 ret = radix_tree_gang_lookup(swap_radix,
362 (void **)&found_group,
363 0, 1);
364 }
Chris Mason31f3c992007-04-30 15:25:45 -0400365 BUG_ON(ret != 1);
366 }
Chris Masonbe744172007-05-06 10:15:01 -0400367found:
Chris Mason31f3c992007-04-30 15:25:45 -0400368 return found_group;
Chris Masoncd1bc462007-04-27 10:08:34 -0400369}
370
Chris Masonb18c6682007-04-17 13:26:50 -0400371int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
372 struct btrfs_root *root,
373 u64 blocknr, u64 num_blocks)
Chris Mason02217ed2007-03-02 16:08:05 -0500374{
Chris Mason5caf2a02007-04-02 11:20:42 -0400375 struct btrfs_path *path;
Chris Mason02217ed2007-03-02 16:08:05 -0500376 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400377 struct btrfs_key key;
Chris Mason234b63a2007-03-13 10:46:10 -0400378 struct btrfs_leaf *l;
379 struct btrfs_extent_item *item;
Chris Masone2fa7222007-03-12 16:22:34 -0400380 struct btrfs_key ins;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400381 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500382
Chris Masonfbdc7622007-05-30 10:22:12 -0400383 find_free_extent(trans, root->fs_info->extent_root, 0, 0, (u64)-1, 0,
Chris Masonbe08c1b2007-05-03 09:06:49 -0400384 &ins, 0);
Chris Mason5caf2a02007-04-02 11:20:42 -0400385 path = btrfs_alloc_path();
386 BUG_ON(!path);
387 btrfs_init_path(path);
Chris Mason02217ed2007-03-02 16:08:05 -0500388 key.objectid = blocknr;
389 key.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -0400390 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Mason6407bf62007-03-27 06:33:00 -0400391 key.offset = num_blocks;
Chris Mason5caf2a02007-04-02 11:20:42 -0400392 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -0400393 0, 1);
Chris Masona429e512007-04-18 16:15:28 -0400394 if (ret != 0) {
Chris Masona28ec192007-03-06 20:08:01 -0500395 BUG();
Chris Masona429e512007-04-18 16:15:28 -0400396 }
Chris Mason02217ed2007-03-02 16:08:05 -0500397 BUG_ON(ret != 0);
Chris Mason5caf2a02007-04-02 11:20:42 -0400398 l = btrfs_buffer_leaf(path->nodes[0]);
399 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400400 refs = btrfs_extent_refs(item);
401 btrfs_set_extent_refs(item, refs + 1);
Chris Mason5caf2a02007-04-02 11:20:42 -0400402 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masona28ec192007-03-06 20:08:01 -0500403
Chris Mason5caf2a02007-04-02 11:20:42 -0400404 btrfs_release_path(root->fs_info->extent_root, path);
405 btrfs_free_path(path);
Chris Mason9f5fae22007-03-20 14:38:32 -0400406 finish_current_insert(trans, root->fs_info->extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -0400407 del_pending_extents(trans, root->fs_info->extent_root);
Chris Mason02217ed2007-03-02 16:08:05 -0500408 return 0;
409}
410
Chris Masonb18c6682007-04-17 13:26:50 -0400411static int lookup_extent_ref(struct btrfs_trans_handle *trans,
412 struct btrfs_root *root, u64 blocknr,
413 u64 num_blocks, u32 *refs)
Chris Masona28ec192007-03-06 20:08:01 -0500414{
Chris Mason5caf2a02007-04-02 11:20:42 -0400415 struct btrfs_path *path;
Chris Masona28ec192007-03-06 20:08:01 -0500416 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400417 struct btrfs_key key;
Chris Mason234b63a2007-03-13 10:46:10 -0400418 struct btrfs_leaf *l;
419 struct btrfs_extent_item *item;
Chris Mason5caf2a02007-04-02 11:20:42 -0400420
421 path = btrfs_alloc_path();
422 btrfs_init_path(path);
Chris Masona28ec192007-03-06 20:08:01 -0500423 key.objectid = blocknr;
Chris Mason6407bf62007-03-27 06:33:00 -0400424 key.offset = num_blocks;
Chris Mason62e27492007-03-15 12:56:47 -0400425 key.flags = 0;
426 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Mason5caf2a02007-04-02 11:20:42 -0400427 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -0400428 0, 0);
Chris Masona28ec192007-03-06 20:08:01 -0500429 if (ret != 0)
430 BUG();
Chris Mason5caf2a02007-04-02 11:20:42 -0400431 l = btrfs_buffer_leaf(path->nodes[0]);
432 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400433 *refs = btrfs_extent_refs(item);
Chris Mason5caf2a02007-04-02 11:20:42 -0400434 btrfs_release_path(root->fs_info->extent_root, path);
435 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -0500436 return 0;
437}
438
Chris Masonc5739bb2007-04-10 09:27:04 -0400439int btrfs_inc_root_ref(struct btrfs_trans_handle *trans,
440 struct btrfs_root *root)
441{
Chris Masonb18c6682007-04-17 13:26:50 -0400442 return btrfs_inc_extent_ref(trans, root, bh_blocknr(root->node), 1);
Chris Masonc5739bb2007-04-10 09:27:04 -0400443}
444
Chris Masone089f052007-03-16 16:20:31 -0400445int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Masone20d96d2007-03-22 12:13:20 -0400446 struct buffer_head *buf)
Chris Mason02217ed2007-03-02 16:08:05 -0500447{
448 u64 blocknr;
Chris Masone20d96d2007-03-22 12:13:20 -0400449 struct btrfs_node *buf_node;
Chris Mason6407bf62007-03-27 06:33:00 -0400450 struct btrfs_leaf *buf_leaf;
451 struct btrfs_disk_key *key;
452 struct btrfs_file_extent_item *fi;
Chris Mason02217ed2007-03-02 16:08:05 -0500453 int i;
Chris Mason6407bf62007-03-27 06:33:00 -0400454 int leaf;
455 int ret;
Chris Masona28ec192007-03-06 20:08:01 -0500456
Chris Mason3768f362007-03-13 16:47:54 -0400457 if (!root->ref_cows)
Chris Masona28ec192007-03-06 20:08:01 -0500458 return 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400459 buf_node = btrfs_buffer_node(buf);
Chris Mason6407bf62007-03-27 06:33:00 -0400460 leaf = btrfs_is_leaf(buf_node);
461 buf_leaf = btrfs_buffer_leaf(buf);
Chris Masone20d96d2007-03-22 12:13:20 -0400462 for (i = 0; i < btrfs_header_nritems(&buf_node->header); i++) {
Chris Mason6407bf62007-03-27 06:33:00 -0400463 if (leaf) {
Chris Mason3a686372007-05-24 13:35:57 -0400464 u64 disk_blocknr;
Chris Mason6407bf62007-03-27 06:33:00 -0400465 key = &buf_leaf->items[i].key;
466 if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY)
467 continue;
468 fi = btrfs_item_ptr(buf_leaf, i,
469 struct btrfs_file_extent_item);
Chris Mason236454d2007-04-19 13:37:44 -0400470 if (btrfs_file_extent_type(fi) ==
471 BTRFS_FILE_EXTENT_INLINE)
472 continue;
Chris Mason3a686372007-05-24 13:35:57 -0400473 disk_blocknr = btrfs_file_extent_disk_blocknr(fi);
474 if (disk_blocknr == 0)
475 continue;
476 ret = btrfs_inc_extent_ref(trans, root, disk_blocknr,
Chris Mason6407bf62007-03-27 06:33:00 -0400477 btrfs_file_extent_disk_num_blocks(fi));
478 BUG_ON(ret);
479 } else {
480 blocknr = btrfs_node_blockptr(buf_node, i);
Chris Masonb18c6682007-04-17 13:26:50 -0400481 ret = btrfs_inc_extent_ref(trans, root, blocknr, 1);
Chris Mason6407bf62007-03-27 06:33:00 -0400482 BUG_ON(ret);
483 }
Chris Mason02217ed2007-03-02 16:08:05 -0500484 }
485 return 0;
486}
487
Chris Mason9078a3e2007-04-26 16:46:15 -0400488static int write_one_cache_group(struct btrfs_trans_handle *trans,
489 struct btrfs_root *root,
490 struct btrfs_path *path,
491 struct btrfs_block_group_cache *cache)
492{
493 int ret;
494 int pending_ret;
495 struct btrfs_root *extent_root = root->fs_info->extent_root;
496 struct btrfs_block_group_item *bi;
497 struct btrfs_key ins;
498
Chris Masonfbdc7622007-05-30 10:22:12 -0400499 find_free_extent(trans, extent_root, 0, 0, (u64)-1, 0, &ins, 0);
Chris Mason9078a3e2007-04-26 16:46:15 -0400500 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
501 BUG_ON(ret);
502 bi = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
503 struct btrfs_block_group_item);
504 memcpy(bi, &cache->item, sizeof(*bi));
505 mark_buffer_dirty(path->nodes[0]);
506 btrfs_release_path(extent_root, path);
507
508 finish_current_insert(trans, extent_root);
509 pending_ret = del_pending_extents(trans, extent_root);
510 if (ret)
511 return ret;
512 if (pending_ret)
513 return pending_ret;
Chris Masonbe744172007-05-06 10:15:01 -0400514 if (cache->data)
515 cache->last_alloc = cache->first_free;
Chris Mason9078a3e2007-04-26 16:46:15 -0400516 return 0;
517
518}
519
Chris Masonbe744172007-05-06 10:15:01 -0400520static int write_dirty_block_radix(struct btrfs_trans_handle *trans,
521 struct btrfs_root *root,
522 struct radix_tree_root *radix)
Chris Mason9078a3e2007-04-26 16:46:15 -0400523{
524 struct btrfs_block_group_cache *cache[8];
525 int ret;
526 int err = 0;
527 int werr = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -0400528 int i;
529 struct btrfs_path *path;
530
531 path = btrfs_alloc_path();
532 if (!path)
533 return -ENOMEM;
534
535 while(1) {
536 ret = radix_tree_gang_lookup_tag(radix, (void **)cache,
537 0, ARRAY_SIZE(cache),
538 BTRFS_BLOCK_GROUP_DIRTY);
539 if (!ret)
540 break;
541 for (i = 0; i < ret; i++) {
542 radix_tree_tag_clear(radix, cache[i]->key.objectid +
543 cache[i]->key.offset - 1,
544 BTRFS_BLOCK_GROUP_DIRTY);
545 err = write_one_cache_group(trans, root,
546 path, cache[i]);
547 if (err)
548 werr = err;
549 }
550 }
551 btrfs_free_path(path);
552 return werr;
553}
554
Chris Masonbe744172007-05-06 10:15:01 -0400555int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
556 struct btrfs_root *root)
557{
558 int ret;
559 int ret2;
560 ret = write_dirty_block_radix(trans, root,
561 &root->fs_info->block_group_radix);
562 ret2 = write_dirty_block_radix(trans, root,
563 &root->fs_info->block_group_data_radix);
564 if (ret)
565 return ret;
566 if (ret2)
567 return ret2;
568 return 0;
569}
570
Chris Mason9078a3e2007-04-26 16:46:15 -0400571static int update_block_group(struct btrfs_trans_handle *trans,
572 struct btrfs_root *root,
Chris Mason1e2677e2007-05-29 16:52:18 -0400573 u64 blocknr, u64 num, int alloc, int mark_free,
574 int data)
Chris Mason9078a3e2007-04-26 16:46:15 -0400575{
576 struct btrfs_block_group_cache *cache;
577 struct btrfs_fs_info *info = root->fs_info;
578 u64 total = num;
579 u64 old_val;
580 u64 block_in_group;
Chris Masone37c9e62007-05-09 20:13:14 -0400581 u64 i;
Chris Mason1e2677e2007-05-29 16:52:18 -0400582 int ret;
Chris Mason3e1ad542007-05-07 20:03:49 -0400583
Chris Mason9078a3e2007-04-26 16:46:15 -0400584 while(total) {
Chris Mason5276aed2007-06-11 21:33:38 -0400585 cache = btrfs_lookup_block_group(info, blocknr);
Chris Mason3e1ad542007-05-07 20:03:49 -0400586 if (!cache) {
Chris Mason9078a3e2007-04-26 16:46:15 -0400587 return -1;
Chris Masoncd1bc462007-04-27 10:08:34 -0400588 }
Chris Mason9078a3e2007-04-26 16:46:15 -0400589 block_in_group = blocknr - cache->key.objectid;
590 WARN_ON(block_in_group > cache->key.offset);
Chris Mason3e1ad542007-05-07 20:03:49 -0400591 radix_tree_tag_set(cache->radix, cache->key.objectid +
Chris Masonbe744172007-05-06 10:15:01 -0400592 cache->key.offset - 1,
Chris Mason9078a3e2007-04-26 16:46:15 -0400593 BTRFS_BLOCK_GROUP_DIRTY);
594
595 old_val = btrfs_block_group_used(&cache->item);
596 num = min(total, cache->key.offset - block_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -0400597 if (alloc) {
Chris Masoncd1bc462007-04-27 10:08:34 -0400598 if (blocknr > cache->last_alloc)
599 cache->last_alloc = blocknr;
Chris Masone37c9e62007-05-09 20:13:14 -0400600 if (!cache->data) {
601 for (i = 0; i < num; i++) {
602 clear_radix_bit(&info->extent_map_radix,
603 blocknr + i);
604 }
605 }
Chris Mason1e2677e2007-05-29 16:52:18 -0400606 if (cache->data != data &&
Chris Mason84f54cf2007-06-12 07:43:08 -0400607 old_val < (cache->key.offset >> 1)) {
Chris Mason1e2677e2007-05-29 16:52:18 -0400608 cache->data = data;
609 radix_tree_delete(cache->radix,
610 cache->key.objectid +
611 cache->key.offset - 1);
612
613 if (data) {
614 cache->radix =
615 &info->block_group_data_radix;
616 cache->item.flags |=
617 BTRFS_BLOCK_GROUP_DATA;
618 } else {
619 cache->radix = &info->block_group_radix;
620 cache->item.flags &=
621 ~BTRFS_BLOCK_GROUP_DATA;
622 }
623 ret = radix_tree_insert(cache->radix,
624 cache->key.objectid +
625 cache->key.offset - 1,
626 (void *)cache);
627 }
628 old_val += num;
Chris Masoncd1bc462007-04-27 10:08:34 -0400629 } else {
Chris Mason9078a3e2007-04-26 16:46:15 -0400630 old_val -= num;
Chris Masoncd1bc462007-04-27 10:08:34 -0400631 if (blocknr < cache->first_free)
632 cache->first_free = blocknr;
Chris Masone37c9e62007-05-09 20:13:14 -0400633 if (!cache->data && mark_free) {
634 for (i = 0; i < num; i++) {
635 set_radix_bit(&info->extent_map_radix,
636 blocknr + i);
637 }
638 }
Chris Mason84f54cf2007-06-12 07:43:08 -0400639 if (old_val < (cache->key.offset >> 1) &&
640 old_val + num >= (cache->key.offset >> 1)) {
Chris Masone37c9e62007-05-09 20:13:14 -0400641 radix_tree_tag_set(cache->radix,
642 cache->key.objectid +
643 cache->key.offset - 1,
644 BTRFS_BLOCK_GROUP_AVAIL);
645 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400646 }
Chris Mason9078a3e2007-04-26 16:46:15 -0400647 btrfs_set_block_group_used(&cache->item, old_val);
Chris Masone37c9e62007-05-09 20:13:14 -0400648 total -= num;
649 blocknr += num;
Chris Mason9078a3e2007-04-26 16:46:15 -0400650 }
651 return 0;
652}
653
Chris Masonbe08c1b2007-05-03 09:06:49 -0400654static int try_remove_page(struct address_space *mapping, unsigned long index)
655{
656 int ret;
657 ret = invalidate_mapping_pages(mapping, index, index);
658 return ret;
659}
660
Chris Masone089f052007-03-16 16:20:31 -0400661int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, struct
662 btrfs_root *root)
Chris Masona28ec192007-03-06 20:08:01 -0500663{
Chris Mason8ef97622007-03-26 10:15:30 -0400664 unsigned long gang[8];
Chris Masonbe08c1b2007-05-03 09:06:49 -0400665 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Masonbe744172007-05-06 10:15:01 -0400666 struct btrfs_block_group_cache *block_group;
Chris Mason88fd1462007-03-16 08:56:18 -0400667 u64 first = 0;
Chris Masona28ec192007-03-06 20:08:01 -0500668 int ret;
669 int i;
Chris Mason8ef97622007-03-26 10:15:30 -0400670 struct radix_tree_root *pinned_radix = &root->fs_info->pinned_radix;
Chris Masone37c9e62007-05-09 20:13:14 -0400671 struct radix_tree_root *extent_radix = &root->fs_info->extent_map_radix;
Chris Masona28ec192007-03-06 20:08:01 -0500672
673 while(1) {
Chris Masone37c9e62007-05-09 20:13:14 -0400674 ret = find_first_radix_bit(pinned_radix, gang, 0,
Chris Mason8ef97622007-03-26 10:15:30 -0400675 ARRAY_SIZE(gang));
Chris Masona28ec192007-03-06 20:08:01 -0500676 if (!ret)
677 break;
Chris Mason88fd1462007-03-16 08:56:18 -0400678 if (!first)
Chris Mason8ef97622007-03-26 10:15:30 -0400679 first = gang[0];
Chris Mason0579da42007-03-07 16:15:30 -0500680 for (i = 0; i < ret; i++) {
Chris Mason8ef97622007-03-26 10:15:30 -0400681 clear_radix_bit(pinned_radix, gang[i]);
Chris Mason5276aed2007-06-11 21:33:38 -0400682 block_group = btrfs_lookup_block_group(root->fs_info,
683 gang[i]);
Chris Masonbe744172007-05-06 10:15:01 -0400684 if (block_group) {
685 WARN_ON(block_group->pinned == 0);
686 block_group->pinned--;
687 if (gang[i] < block_group->last_alloc)
688 block_group->last_alloc = gang[i];
Chris Masone37c9e62007-05-09 20:13:14 -0400689 if (gang[i] < block_group->last_prealloc)
690 block_group->last_prealloc = gang[i];
691 if (!block_group->data)
692 set_radix_bit(extent_radix, gang[i]);
Chris Masonbe744172007-05-06 10:15:01 -0400693 }
Chris Masonbe08c1b2007-05-03 09:06:49 -0400694 try_remove_page(btree_inode->i_mapping,
695 gang[i] << (PAGE_CACHE_SHIFT -
696 btree_inode->i_blkbits));
Chris Mason0579da42007-03-07 16:15:30 -0500697 }
Chris Masona28ec192007-03-06 20:08:01 -0500698 }
699 return 0;
700}
701
Chris Masone089f052007-03-16 16:20:31 -0400702static int finish_current_insert(struct btrfs_trans_handle *trans, struct
703 btrfs_root *extent_root)
Chris Mason037e6392007-03-07 11:50:24 -0500704{
Chris Masone2fa7222007-03-12 16:22:34 -0400705 struct btrfs_key ins;
Chris Mason234b63a2007-03-13 10:46:10 -0400706 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -0500707 int i;
708 int ret;
Chris Mason1261ec42007-03-20 20:35:03 -0400709 u64 super_blocks_used;
710 struct btrfs_fs_info *info = extent_root->fs_info;
Chris Mason037e6392007-03-07 11:50:24 -0500711
Chris Masoncf27e1e2007-03-13 09:49:06 -0400712 btrfs_set_extent_refs(&extent_item, 1);
Chris Mason037e6392007-03-07 11:50:24 -0500713 ins.offset = 1;
714 ins.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -0400715 btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
Chris Mason5d0c3e62007-04-23 17:01:05 -0400716 btrfs_set_extent_owner(&extent_item, extent_root->root_key.objectid);
Chris Mason037e6392007-03-07 11:50:24 -0500717
Chris Masonf2458e12007-04-25 15:52:25 -0400718 for (i = 0; i < extent_root->fs_info->extent_tree_insert_nr; i++) {
719 ins.objectid = extent_root->fs_info->extent_tree_insert[i];
Chris Mason1261ec42007-03-20 20:35:03 -0400720 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
721 btrfs_set_super_blocks_used(info->disk_super,
722 super_blocks_used + 1);
Chris Masone089f052007-03-16 16:20:31 -0400723 ret = btrfs_insert_item(trans, extent_root, &ins, &extent_item,
724 sizeof(extent_item));
Chris Mason037e6392007-03-07 11:50:24 -0500725 BUG_ON(ret);
726 }
Chris Masonf2458e12007-04-25 15:52:25 -0400727 extent_root->fs_info->extent_tree_insert_nr = 0;
728 extent_root->fs_info->extent_tree_prealloc_nr = 0;
Chris Mason037e6392007-03-07 11:50:24 -0500729 return 0;
730}
731
Chris Mason8ef97622007-03-26 10:15:30 -0400732static int pin_down_block(struct btrfs_root *root, u64 blocknr, int pending)
Chris Masone20d96d2007-03-22 12:13:20 -0400733{
734 int err;
Chris Mason78fae272007-03-25 11:35:08 -0400735 struct btrfs_header *header;
Chris Mason8ef97622007-03-26 10:15:30 -0400736 struct buffer_head *bh;
Chris Mason78fae272007-03-25 11:35:08 -0400737
Chris Masonf4b9aa82007-03-27 11:05:53 -0400738 if (!pending) {
Chris Masond98237b2007-03-28 13:57:48 -0400739 bh = btrfs_find_tree_block(root, blocknr);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400740 if (bh) {
741 if (buffer_uptodate(bh)) {
742 u64 transid =
743 root->fs_info->running_transaction->transid;
744 header = btrfs_buffer_header(bh);
745 if (btrfs_header_generation(header) ==
746 transid) {
747 btrfs_block_release(root, bh);
748 return 0;
749 }
Chris Masonf4b9aa82007-03-27 11:05:53 -0400750 }
Chris Masond6025572007-03-30 14:27:56 -0400751 btrfs_block_release(root, bh);
Chris Mason8ef97622007-03-26 10:15:30 -0400752 }
Chris Mason8ef97622007-03-26 10:15:30 -0400753 err = set_radix_bit(&root->fs_info->pinned_radix, blocknr);
Chris Masonbe744172007-05-06 10:15:01 -0400754 if (!err) {
755 struct btrfs_block_group_cache *cache;
Chris Mason5276aed2007-06-11 21:33:38 -0400756 cache = btrfs_lookup_block_group(root->fs_info,
757 blocknr);
Chris Masonbe744172007-05-06 10:15:01 -0400758 if (cache)
759 cache->pinned++;
760 }
Chris Masonf4b9aa82007-03-27 11:05:53 -0400761 } else {
762 err = set_radix_bit(&root->fs_info->pending_del_radix, blocknr);
763 }
Chris Masonbe744172007-05-06 10:15:01 -0400764 BUG_ON(err < 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400765 return 0;
766}
767
Chris Masona28ec192007-03-06 20:08:01 -0500768/*
769 * remove an extent from the root, returns 0 on success
770 */
Chris Masone089f052007-03-16 16:20:31 -0400771static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masone37c9e62007-05-09 20:13:14 -0400772 *root, u64 blocknr, u64 num_blocks, int pin,
773 int mark_free)
Chris Masona28ec192007-03-06 20:08:01 -0500774{
Chris Mason5caf2a02007-04-02 11:20:42 -0400775 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -0400776 struct btrfs_key key;
Chris Mason1261ec42007-03-20 20:35:03 -0400777 struct btrfs_fs_info *info = root->fs_info;
778 struct btrfs_root *extent_root = info->extent_root;
Chris Masona28ec192007-03-06 20:08:01 -0500779 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400780 struct btrfs_extent_item *ei;
Chris Masone2fa7222007-03-12 16:22:34 -0400781 struct btrfs_key ins;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400782 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500783
Chris Masona28ec192007-03-06 20:08:01 -0500784 key.objectid = blocknr;
785 key.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -0400786 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masona28ec192007-03-06 20:08:01 -0500787 key.offset = num_blocks;
788
Chris Masonfbdc7622007-05-30 10:22:12 -0400789 find_free_extent(trans, root, 0, 0, (u64)-1, 0, &ins, 0);
Chris Mason5caf2a02007-04-02 11:20:42 -0400790 path = btrfs_alloc_path();
791 BUG_ON(!path);
792 btrfs_init_path(path);
Chris Mason5f26f772007-04-05 10:38:44 -0400793
Chris Mason5caf2a02007-04-02 11:20:42 -0400794 ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
Chris Masona28ec192007-03-06 20:08:01 -0500795 if (ret) {
Chris Masona28ec192007-03-06 20:08:01 -0500796 BUG();
797 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400798 ei = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
Chris Mason123abc82007-03-14 14:14:43 -0400799 struct btrfs_extent_item);
Chris Masona28ec192007-03-06 20:08:01 -0500800 BUG_ON(ei->refs == 0);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400801 refs = btrfs_extent_refs(ei) - 1;
802 btrfs_set_extent_refs(ei, refs);
Chris Mason5caf2a02007-04-02 11:20:42 -0400803 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400804 if (refs == 0) {
Chris Mason1261ec42007-03-20 20:35:03 -0400805 u64 super_blocks_used;
Chris Mason78fae272007-03-25 11:35:08 -0400806
807 if (pin) {
Chris Mason8ef97622007-03-26 10:15:30 -0400808 ret = pin_down_block(root, blocknr, 0);
Chris Mason78fae272007-03-25 11:35:08 -0400809 BUG_ON(ret);
810 }
811
Chris Mason1261ec42007-03-20 20:35:03 -0400812 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
813 btrfs_set_super_blocks_used(info->disk_super,
814 super_blocks_used - num_blocks);
Chris Mason5caf2a02007-04-02 11:20:42 -0400815 ret = btrfs_del_item(trans, extent_root, path);
Chris Masona28ec192007-03-06 20:08:01 -0500816 if (ret)
817 BUG();
Chris Masone37c9e62007-05-09 20:13:14 -0400818 ret = update_block_group(trans, root, blocknr, num_blocks, 0,
Chris Mason1e2677e2007-05-29 16:52:18 -0400819 mark_free, 0);
Chris Mason9078a3e2007-04-26 16:46:15 -0400820 BUG_ON(ret);
Chris Masona28ec192007-03-06 20:08:01 -0500821 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400822 btrfs_free_path(path);
Chris Masone089f052007-03-16 16:20:31 -0400823 finish_current_insert(trans, extent_root);
Chris Masona28ec192007-03-06 20:08:01 -0500824 return ret;
825}
826
827/*
Chris Masonfec577f2007-02-26 10:40:21 -0500828 * find all the blocks marked as pending in the radix tree and remove
829 * them from the extent map
830 */
Chris Masone089f052007-03-16 16:20:31 -0400831static int del_pending_extents(struct btrfs_trans_handle *trans, struct
832 btrfs_root *extent_root)
Chris Masonfec577f2007-02-26 10:40:21 -0500833{
834 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -0400835 int wret;
836 int err = 0;
Chris Mason8ef97622007-03-26 10:15:30 -0400837 unsigned long gang[4];
Chris Masonfec577f2007-02-26 10:40:21 -0500838 int i;
Chris Mason8ef97622007-03-26 10:15:30 -0400839 struct radix_tree_root *pending_radix;
840 struct radix_tree_root *pinned_radix;
Chris Masonbe744172007-05-06 10:15:01 -0400841 struct btrfs_block_group_cache *cache;
Chris Mason8ef97622007-03-26 10:15:30 -0400842
843 pending_radix = &extent_root->fs_info->pending_del_radix;
844 pinned_radix = &extent_root->fs_info->pinned_radix;
Chris Masonfec577f2007-02-26 10:40:21 -0500845
846 while(1) {
Chris Masone37c9e62007-05-09 20:13:14 -0400847 ret = find_first_radix_bit(pending_radix, gang, 0,
Chris Mason8ef97622007-03-26 10:15:30 -0400848 ARRAY_SIZE(gang));
Chris Masonfec577f2007-02-26 10:40:21 -0500849 if (!ret)
850 break;
851 for (i = 0; i < ret; i++) {
Chris Mason8ef97622007-03-26 10:15:30 -0400852 wret = set_radix_bit(pinned_radix, gang[i]);
Chris Masonbe744172007-05-06 10:15:01 -0400853 if (wret == 0) {
Chris Mason5276aed2007-06-11 21:33:38 -0400854 cache =
855 btrfs_lookup_block_group(extent_root->fs_info,
Chris Masonbe744172007-05-06 10:15:01 -0400856 gang[i]);
857 if (cache)
858 cache->pinned++;
859 }
860 if (wret < 0) {
861 printk(KERN_CRIT "set_radix_bit, err %d\n",
862 wret);
863 BUG_ON(wret < 0);
864 }
Chris Mason8ef97622007-03-26 10:15:30 -0400865 wret = clear_radix_bit(pending_radix, gang[i]);
866 BUG_ON(wret);
Chris Masond5719762007-03-23 10:01:08 -0400867 wret = __free_extent(trans, extent_root,
Chris Masone37c9e62007-05-09 20:13:14 -0400868 gang[i], 1, 0, 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400869 if (wret)
870 err = wret;
Chris Masonfec577f2007-02-26 10:40:21 -0500871 }
872 }
Chris Masone20d96d2007-03-22 12:13:20 -0400873 return err;
Chris Masonfec577f2007-02-26 10:40:21 -0500874}
875
876/*
877 * remove an extent from the root, returns 0 on success
878 */
Chris Masone089f052007-03-16 16:20:31 -0400879int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
880 *root, u64 blocknr, u64 num_blocks, int pin)
Chris Masonfec577f2007-02-26 10:40:21 -0500881{
Chris Mason9f5fae22007-03-20 14:38:32 -0400882 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Masonfec577f2007-02-26 10:40:21 -0500883 int pending_ret;
884 int ret;
Chris Masona28ec192007-03-06 20:08:01 -0500885
886 if (root == extent_root) {
Chris Mason8ef97622007-03-26 10:15:30 -0400887 pin_down_block(root, blocknr, 1);
Chris Masona28ec192007-03-06 20:08:01 -0500888 return 0;
889 }
Chris Masone37c9e62007-05-09 20:13:14 -0400890 ret = __free_extent(trans, root, blocknr, num_blocks, pin, pin == 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400891 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
Chris Masonfec577f2007-02-26 10:40:21 -0500892 return ret ? ret : pending_ret;
893}
894
895/*
896 * walks the btree of allocated extents and find a hole of a given size.
897 * The key ins is changed to record the hole:
898 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -0400899 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -0500900 * ins->offset == number of blocks
901 * Any available blocks before search_start are skipped.
902 */
Chris Masone089f052007-03-16 16:20:31 -0400903static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
904 *orig_root, u64 num_blocks, u64 search_start, u64
Chris Masonfbdc7622007-05-30 10:22:12 -0400905 search_end, u64 hint_block,
906 struct btrfs_key *ins, int data)
Chris Masonfec577f2007-02-26 10:40:21 -0500907{
Chris Mason5caf2a02007-04-02 11:20:42 -0400908 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -0400909 struct btrfs_key key;
Chris Masonfec577f2007-02-26 10:40:21 -0500910 int ret;
911 u64 hole_size = 0;
912 int slot = 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400913 u64 last_block = 0;
Chris Mason037e6392007-03-07 11:50:24 -0500914 u64 test_block;
Chris Masonbe744172007-05-06 10:15:01 -0400915 u64 orig_search_start = search_start;
Chris Masonfec577f2007-02-26 10:40:21 -0500916 int start_found;
Chris Mason234b63a2007-03-13 10:46:10 -0400917 struct btrfs_leaf *l;
Chris Mason9f5fae22007-03-20 14:38:32 -0400918 struct btrfs_root * root = orig_root->fs_info->extent_root;
Chris Masonf2458e12007-04-25 15:52:25 -0400919 struct btrfs_fs_info *info = root->fs_info;
Chris Mason0579da42007-03-07 16:15:30 -0500920 int total_needed = num_blocks;
Chris Masonf2458e12007-04-25 15:52:25 -0400921 int total_found = 0;
922 int fill_prealloc = 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400923 int level;
Chris Masonbe08c1b2007-05-03 09:06:49 -0400924 struct btrfs_block_group_cache *block_group;
Chris Masonbe744172007-05-06 10:15:01 -0400925 int full_scan = 0;
Chris Masonfbdc7622007-05-30 10:22:12 -0400926 int wrapped = 0;
Chris Masonde428b62007-05-18 13:28:27 -0400927 u64 limit;
Chris Masonfec577f2007-02-26 10:40:21 -0500928
Chris Masonb1a4d962007-04-04 15:27:52 -0400929 path = btrfs_alloc_path();
930 ins->flags = 0;
931 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
932
Chris Masone20d96d2007-03-22 12:13:20 -0400933 level = btrfs_header_level(btrfs_buffer_header(root->node));
Chris Masonf2458e12007-04-25 15:52:25 -0400934 if (num_blocks == 0) {
935 fill_prealloc = 1;
936 num_blocks = 1;
Chris Mason308535a2007-04-28 15:17:08 -0400937 total_needed = (min(level + 1, BTRFS_MAX_LEVEL) + 2) * 3;
Chris Masonf2458e12007-04-25 15:52:25 -0400938 }
Chris Mason3e1ad542007-05-07 20:03:49 -0400939 if (search_end == (u64)-1)
940 search_end = btrfs_super_total_blocks(info->disk_super);
Chris Masonfbdc7622007-05-30 10:22:12 -0400941 if (hint_block) {
Chris Mason5276aed2007-06-11 21:33:38 -0400942 block_group = btrfs_lookup_block_group(info, hint_block);
Chris Masonbe744172007-05-06 10:15:01 -0400943 block_group = btrfs_find_block_group(root, block_group,
Chris Masonfbdc7622007-05-30 10:22:12 -0400944 hint_block, data, 1);
Chris Masonbe744172007-05-06 10:15:01 -0400945 } else {
946 block_group = btrfs_find_block_group(root,
947 trans->block_group, 0,
Chris Masonde428b62007-05-18 13:28:27 -0400948 data, 1);
Chris Masonbe744172007-05-06 10:15:01 -0400949 }
950
951check_failed:
Chris Mason1e2677e2007-05-29 16:52:18 -0400952 if (!block_group->data)
Chris Masone37c9e62007-05-09 20:13:14 -0400953 search_start = find_search_start(root, &block_group,
954 search_start, total_needed);
Chris Masonfbdc7622007-05-30 10:22:12 -0400955 else if (!full_scan)
Chris Masone37c9e62007-05-09 20:13:14 -0400956 search_start = max(block_group->last_alloc, search_start);
957
Chris Mason5caf2a02007-04-02 11:20:42 -0400958 btrfs_init_path(path);
Chris Masonfec577f2007-02-26 10:40:21 -0500959 ins->objectid = search_start;
960 ins->offset = 0;
Chris Masonfec577f2007-02-26 10:40:21 -0500961 start_found = 0;
Chris Masone37c9e62007-05-09 20:13:14 -0400962
Chris Mason5caf2a02007-04-02 11:20:42 -0400963 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
Chris Mason0f70abe2007-02-28 16:46:22 -0500964 if (ret < 0)
965 goto error;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500966
Chris Masone37c9e62007-05-09 20:13:14 -0400967 if (path->slots[0] > 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400968 path->slots[0]--;
Chris Masone37c9e62007-05-09 20:13:14 -0400969 }
970
971 l = btrfs_buffer_leaf(path->nodes[0]);
972 btrfs_disk_key_to_cpu(&key, &l->items[path->slots[0]].key);
973 /*
974 * a rare case, go back one key if we hit a block group item
975 * instead of an extent item
976 */
977 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY &&
978 key.objectid + key.offset >= search_start) {
979 ins->objectid = key.objectid;
980 ins->offset = key.offset - 1;
981 btrfs_release_path(root, path);
982 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
983 if (ret < 0)
984 goto error;
985
986 if (path->slots[0] > 0) {
987 path->slots[0]--;
988 }
989 }
Chris Mason0579da42007-03-07 16:15:30 -0500990
Chris Masonfec577f2007-02-26 10:40:21 -0500991 while (1) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400992 l = btrfs_buffer_leaf(path->nodes[0]);
993 slot = path->slots[0];
Chris Mason7518a232007-03-12 12:01:18 -0400994 if (slot >= btrfs_header_nritems(&l->header)) {
Chris Masonf2458e12007-04-25 15:52:25 -0400995 if (fill_prealloc) {
996 info->extent_tree_prealloc_nr = 0;
997 total_found = 0;
998 }
Chris Masonde428b62007-05-18 13:28:27 -0400999 if (start_found)
1000 limit = last_block +
Chris Mason84f54cf2007-06-12 07:43:08 -04001001 (block_group->key.offset >> 1);
Chris Masonde428b62007-05-18 13:28:27 -04001002 else
1003 limit = search_start +
Chris Mason84f54cf2007-06-12 07:43:08 -04001004 (block_group->key.offset >> 1);
Chris Mason5caf2a02007-04-02 11:20:42 -04001005 ret = btrfs_next_leaf(root, path);
Chris Masonfec577f2007-02-26 10:40:21 -05001006 if (ret == 0)
1007 continue;
Chris Mason0f70abe2007-02-28 16:46:22 -05001008 if (ret < 0)
1009 goto error;
Chris Masonfec577f2007-02-26 10:40:21 -05001010 if (!start_found) {
1011 ins->objectid = search_start;
Chris Mason3e1ad542007-05-07 20:03:49 -04001012 ins->offset = search_end - search_start;
Chris Masonfec577f2007-02-26 10:40:21 -05001013 start_found = 1;
1014 goto check_pending;
1015 }
1016 ins->objectid = last_block > search_start ?
1017 last_block : search_start;
Chris Mason3e1ad542007-05-07 20:03:49 -04001018 ins->offset = search_end - ins->objectid;
Chris Masonfec577f2007-02-26 10:40:21 -05001019 goto check_pending;
1020 }
Chris Masone37c9e62007-05-09 20:13:14 -04001021
Chris Masone2fa7222007-03-12 16:22:34 -04001022 btrfs_disk_key_to_cpu(&key, &l->items[slot].key);
Chris Masone37c9e62007-05-09 20:13:14 -04001023 if (key.objectid >= search_start && key.objectid > last_block &&
1024 start_found) {
1025 if (last_block < search_start)
1026 last_block = search_start;
1027 hole_size = key.objectid - last_block;
1028 if (hole_size >= num_blocks) {
1029 ins->objectid = last_block;
1030 ins->offset = hole_size;
1031 goto check_pending;
Chris Mason0579da42007-03-07 16:15:30 -05001032 }
Chris Masonfec577f2007-02-26 10:40:21 -05001033 }
Chris Masone37c9e62007-05-09 20:13:14 -04001034
1035 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY)
1036 goto next;
1037
Chris Mason0579da42007-03-07 16:15:30 -05001038 start_found = 1;
Chris Masone2fa7222007-03-12 16:22:34 -04001039 last_block = key.objectid + key.offset;
Chris Masonfbdc7622007-05-30 10:22:12 -04001040 if (!full_scan && last_block >= block_group->key.objectid +
Chris Masonbe744172007-05-06 10:15:01 -04001041 block_group->key.offset) {
1042 btrfs_release_path(root, path);
1043 search_start = block_group->key.objectid +
1044 block_group->key.offset * 2;
1045 goto new_group;
1046 }
Chris Mason9078a3e2007-04-26 16:46:15 -04001047next:
Chris Mason5caf2a02007-04-02 11:20:42 -04001048 path->slots[0]++;
Chris Masonde428b62007-05-18 13:28:27 -04001049 cond_resched();
Chris Masonfec577f2007-02-26 10:40:21 -05001050 }
1051 // FIXME -ENOSPC
1052check_pending:
1053 /* we have to make sure we didn't find an extent that has already
1054 * been allocated by the map tree or the original allocation
1055 */
Chris Mason5caf2a02007-04-02 11:20:42 -04001056 btrfs_release_path(root, path);
Chris Masonfec577f2007-02-26 10:40:21 -05001057 BUG_ON(ins->objectid < search_start);
Chris Masone37c9e62007-05-09 20:13:14 -04001058
Chris Mason3e1ad542007-05-07 20:03:49 -04001059 if (ins->objectid + num_blocks >= search_end) {
Chris Masonfbdc7622007-05-30 10:22:12 -04001060 if (full_scan) {
1061 ret = -ENOSPC;
1062 goto error;
1063 }
Chris Masonbe744172007-05-06 10:15:01 -04001064 search_start = orig_search_start;
Chris Masonfbdc7622007-05-30 10:22:12 -04001065 if (wrapped)
1066 full_scan = 1;
1067 else
1068 wrapped = 1;
Chris Masonbe744172007-05-06 10:15:01 -04001069 goto new_group;
Chris Mason06a2f9f2007-04-28 08:48:10 -04001070 }
Chris Mason037e6392007-03-07 11:50:24 -05001071 for (test_block = ins->objectid;
Chris Masonf2458e12007-04-25 15:52:25 -04001072 test_block < ins->objectid + num_blocks; test_block++) {
1073 if (test_radix_bit(&info->pinned_radix, test_block)) {
Chris Mason037e6392007-03-07 11:50:24 -05001074 search_start = test_block + 1;
Chris Masonbe744172007-05-06 10:15:01 -04001075 goto new_group;
Chris Masonfec577f2007-02-26 10:40:21 -05001076 }
1077 }
Chris Masonf2458e12007-04-25 15:52:25 -04001078 if (!fill_prealloc && info->extent_tree_insert_nr) {
1079 u64 last =
1080 info->extent_tree_insert[info->extent_tree_insert_nr - 1];
1081 if (ins->objectid + num_blocks >
1082 info->extent_tree_insert[0] &&
1083 ins->objectid <= last) {
1084 search_start = last + 1;
Chris Masone37c9e62007-05-09 20:13:14 -04001085 WARN_ON(!full_scan);
Chris Masonbe744172007-05-06 10:15:01 -04001086 goto new_group;
Chris Masonf2458e12007-04-25 15:52:25 -04001087 }
1088 }
1089 if (!fill_prealloc && info->extent_tree_prealloc_nr) {
1090 u64 first =
1091 info->extent_tree_prealloc[info->extent_tree_prealloc_nr - 1];
1092 if (ins->objectid + num_blocks > first &&
1093 ins->objectid <= info->extent_tree_prealloc[0]) {
1094 search_start = info->extent_tree_prealloc[0] + 1;
Chris Masone37c9e62007-05-09 20:13:14 -04001095 WARN_ON(!full_scan);
Chris Masonbe744172007-05-06 10:15:01 -04001096 goto new_group;
Chris Masonf2458e12007-04-25 15:52:25 -04001097 }
1098 }
1099 if (fill_prealloc) {
1100 int nr;
1101 test_block = ins->objectid;
Chris Masone37c9e62007-05-09 20:13:14 -04001102 if (test_block - info->extent_tree_prealloc[total_needed - 1] >=
1103 leaf_range(root)) {
1104 total_found = 0;
1105 info->extent_tree_prealloc_nr = total_found;
1106 }
Chris Masonf2458e12007-04-25 15:52:25 -04001107 while(test_block < ins->objectid + ins->offset &&
1108 total_found < total_needed) {
1109 nr = total_needed - total_found - 1;
1110 BUG_ON(nr < 0);
Chris Masoncd1bc462007-04-27 10:08:34 -04001111 info->extent_tree_prealloc[nr] = test_block;
Chris Masonf2458e12007-04-25 15:52:25 -04001112 total_found++;
1113 test_block++;
1114 }
1115 if (total_found < total_needed) {
1116 search_start = test_block;
Chris Masonbe744172007-05-06 10:15:01 -04001117 goto new_group;
Chris Masonf2458e12007-04-25 15:52:25 -04001118 }
Chris Masoncd1bc462007-04-27 10:08:34 -04001119 info->extent_tree_prealloc_nr = total_found;
Chris Masonf2458e12007-04-25 15:52:25 -04001120 }
Chris Masone37c9e62007-05-09 20:13:14 -04001121 if (!data) {
Chris Mason5276aed2007-06-11 21:33:38 -04001122 block_group = btrfs_lookup_block_group(info, ins->objectid);
Chris Masone37c9e62007-05-09 20:13:14 -04001123 if (block_group) {
1124 if (fill_prealloc)
1125 block_group->last_prealloc =
1126 info->extent_tree_prealloc[total_needed-1];
1127 else
1128 trans->block_group = block_group;
1129 }
Chris Masoncd1bc462007-04-27 10:08:34 -04001130 }
Chris Mason037e6392007-03-07 11:50:24 -05001131 ins->offset = num_blocks;
Chris Mason5caf2a02007-04-02 11:20:42 -04001132 btrfs_free_path(path);
Chris Masonfec577f2007-02-26 10:40:21 -05001133 return 0;
Chris Masonbe744172007-05-06 10:15:01 -04001134
1135new_group:
Chris Mason3e1ad542007-05-07 20:03:49 -04001136 if (search_start + num_blocks >= search_end) {
Chris Masonbe744172007-05-06 10:15:01 -04001137 search_start = orig_search_start;
Chris Masonfbdc7622007-05-30 10:22:12 -04001138 if (full_scan) {
1139 ret = -ENOSPC;
1140 goto error;
1141 }
1142 if (wrapped)
1143 full_scan = 1;
1144 else
1145 wrapped = 1;
Chris Masonbe744172007-05-06 10:15:01 -04001146 }
Chris Mason5276aed2007-06-11 21:33:38 -04001147 block_group = btrfs_lookup_block_group(info, search_start);
Chris Masonfbdc7622007-05-30 10:22:12 -04001148 cond_resched();
Chris Masonbe744172007-05-06 10:15:01 -04001149 if (!full_scan)
1150 block_group = btrfs_find_block_group(root, block_group,
Chris Masonde428b62007-05-18 13:28:27 -04001151 search_start, data, 0);
Chris Masonbe744172007-05-06 10:15:01 -04001152 goto check_failed;
1153
Chris Mason0f70abe2007-02-28 16:46:22 -05001154error:
Chris Mason5caf2a02007-04-02 11:20:42 -04001155 btrfs_release_path(root, path);
1156 btrfs_free_path(path);
Chris Mason0f70abe2007-02-28 16:46:22 -05001157 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05001158}
Chris Masonfec577f2007-02-26 10:40:21 -05001159/*
Chris Masonfec577f2007-02-26 10:40:21 -05001160 * finds a free extent and does all the dirty work required for allocation
1161 * returns the key for the extent through ins, and a tree buffer for
1162 * the first block of the extent through buf.
1163 *
1164 * returns 0 if everything worked, non-zero otherwise.
1165 */
Chris Mason4d775672007-04-20 20:23:12 -04001166int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
1167 struct btrfs_root *root, u64 owner,
Chris Masonfbdc7622007-05-30 10:22:12 -04001168 u64 num_blocks, u64 hint_block,
Chris Masonbe08c1b2007-05-03 09:06:49 -04001169 u64 search_end, struct btrfs_key *ins, int data)
Chris Masonfec577f2007-02-26 10:40:21 -05001170{
1171 int ret;
1172 int pending_ret;
Chris Mason1261ec42007-03-20 20:35:03 -04001173 u64 super_blocks_used;
Chris Masonfbdc7622007-05-30 10:22:12 -04001174 u64 search_start = 0;
Chris Mason1261ec42007-03-20 20:35:03 -04001175 struct btrfs_fs_info *info = root->fs_info;
1176 struct btrfs_root *extent_root = info->extent_root;
Chris Mason234b63a2007-03-13 10:46:10 -04001177 struct btrfs_extent_item extent_item;
Chris Masonf2458e12007-04-25 15:52:25 -04001178 struct btrfs_key prealloc_key;
Chris Mason037e6392007-03-07 11:50:24 -05001179
Chris Masoncf27e1e2007-03-13 09:49:06 -04001180 btrfs_set_extent_refs(&extent_item, 1);
Chris Mason4d775672007-04-20 20:23:12 -04001181 btrfs_set_extent_owner(&extent_item, owner);
Chris Masonfec577f2007-02-26 10:40:21 -05001182
Chris Mason037e6392007-03-07 11:50:24 -05001183 if (root == extent_root) {
Chris Masonf2458e12007-04-25 15:52:25 -04001184 int nr;
1185 BUG_ON(info->extent_tree_prealloc_nr == 0);
Chris Mason037e6392007-03-07 11:50:24 -05001186 BUG_ON(num_blocks != 1);
Chris Mason037e6392007-03-07 11:50:24 -05001187 ins->offset = 1;
Chris Masonf2458e12007-04-25 15:52:25 -04001188 info->extent_tree_prealloc_nr--;
1189 nr = info->extent_tree_prealloc_nr;
1190 ins->objectid = info->extent_tree_prealloc[nr];
1191 info->extent_tree_insert[info->extent_tree_insert_nr++] =
1192 ins->objectid;
Chris Mason9078a3e2007-04-26 16:46:15 -04001193 ret = update_block_group(trans, root,
Chris Mason1e2677e2007-05-29 16:52:18 -04001194 ins->objectid, ins->offset, 1, 0, 0);
Chris Mason9078a3e2007-04-26 16:46:15 -04001195 BUG_ON(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05001196 return 0;
1197 }
Chris Masone37c9e62007-05-09 20:13:14 -04001198
1199 /*
1200 * if we're doing a data allocation, preallocate room in the
1201 * extent tree first. This way the extent tree blocks end up
1202 * in the correct block group.
1203 */
1204 if (data) {
Chris Masonde428b62007-05-18 13:28:27 -04001205 ret = find_free_extent(trans, root, 0, 0,
Chris Masonfbdc7622007-05-30 10:22:12 -04001206 search_end, 0, &prealloc_key, 0);
Chris Masone37c9e62007-05-09 20:13:14 -04001207 if (ret) {
1208 return ret;
1209 }
1210 if (prealloc_key.objectid + prealloc_key.offset >= search_end) {
1211 int nr = info->extent_tree_prealloc_nr;
1212 search_end = info->extent_tree_prealloc[nr - 1] - 1;
1213 } else {
1214 search_start = info->extent_tree_prealloc[0] + 1;
1215 }
1216 }
Chris Masonfbdc7622007-05-30 10:22:12 -04001217 if (hint_block < search_start)
1218 hint_block = search_start;
Chris Masonf2458e12007-04-25 15:52:25 -04001219 /* do the real allocation */
Chris Masone089f052007-03-16 16:20:31 -04001220 ret = find_free_extent(trans, root, num_blocks, search_start,
Chris Masonfbdc7622007-05-30 10:22:12 -04001221 search_end, hint_block, ins, data);
Chris Masone37c9e62007-05-09 20:13:14 -04001222 if (ret) {
Chris Mason037e6392007-03-07 11:50:24 -05001223 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -04001224 }
Chris Masonfec577f2007-02-26 10:40:21 -05001225
Chris Masone37c9e62007-05-09 20:13:14 -04001226 /*
1227 * if we're doing a metadata allocation, preallocate space in the
1228 * extent tree second. This way, we don't create a tiny hole
1229 * in the allocation map between any unused preallocation blocks
1230 * and the metadata block we're actually allocating. On disk,
1231 * it'll go:
1232 * [block we've allocated], [used prealloc 1], [ unused prealloc ]
1233 * The unused prealloc will get reused the next time around.
1234 */
1235 if (!data) {
1236 if (ins->objectid + ins->offset >= search_end)
1237 search_end = ins->objectid - 1;
1238 else
1239 search_start = ins->objectid + ins->offset;
Chris Mason3e1ad542007-05-07 20:03:49 -04001240
Chris Masonfbdc7622007-05-30 10:22:12 -04001241 if (hint_block < search_start)
1242 hint_block = search_start;
1243
Chris Masone37c9e62007-05-09 20:13:14 -04001244 ret = find_free_extent(trans, root, 0, search_start,
Chris Masonfbdc7622007-05-30 10:22:12 -04001245 search_end, hint_block,
1246 &prealloc_key, 0);
Chris Masone37c9e62007-05-09 20:13:14 -04001247 if (ret) {
1248 return ret;
1249 }
1250 }
Chris Masonf2458e12007-04-25 15:52:25 -04001251
Chris Mason1261ec42007-03-20 20:35:03 -04001252 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
1253 btrfs_set_super_blocks_used(info->disk_super, super_blocks_used +
1254 num_blocks);
Chris Masone089f052007-03-16 16:20:31 -04001255 ret = btrfs_insert_item(trans, extent_root, ins, &extent_item,
1256 sizeof(extent_item));
Chris Mason037e6392007-03-07 11:50:24 -05001257
Chris Masone089f052007-03-16 16:20:31 -04001258 finish_current_insert(trans, extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -04001259 pending_ret = del_pending_extents(trans, extent_root);
Chris Masone37c9e62007-05-09 20:13:14 -04001260 if (ret) {
Chris Mason037e6392007-03-07 11:50:24 -05001261 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -04001262 }
1263 if (pending_ret) {
Chris Mason037e6392007-03-07 11:50:24 -05001264 return pending_ret;
Chris Masone37c9e62007-05-09 20:13:14 -04001265 }
Chris Mason1e2677e2007-05-29 16:52:18 -04001266 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0,
1267 data);
Chris Masonfabb5682007-06-07 22:13:21 -04001268 BUG_ON(ret);
Chris Mason037e6392007-03-07 11:50:24 -05001269 return 0;
Chris Masonfec577f2007-02-26 10:40:21 -05001270}
1271
1272/*
1273 * helper function to allocate a block for a given tree
1274 * returns the tree buffer or NULL.
1275 */
Chris Masone20d96d2007-03-22 12:13:20 -04001276struct buffer_head *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Chris Mason31f3c992007-04-30 15:25:45 -04001277 struct btrfs_root *root, u64 hint)
Chris Masonfec577f2007-02-26 10:40:21 -05001278{
Chris Masone2fa7222007-03-12 16:22:34 -04001279 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -05001280 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -04001281 struct buffer_head *buf;
Chris Masonfec577f2007-02-26 10:40:21 -05001282
Chris Mason4d775672007-04-20 20:23:12 -04001283 ret = btrfs_alloc_extent(trans, root, root->root_key.objectid,
Chris Masonde428b62007-05-18 13:28:27 -04001284 1, hint, (unsigned long)-1, &ins, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05001285 if (ret) {
1286 BUG();
1287 return NULL;
1288 }
Chris Mason9078a3e2007-04-26 16:46:15 -04001289 BUG_ON(ret);
Chris Masond98237b2007-03-28 13:57:48 -04001290 buf = btrfs_find_create_tree_block(root, ins.objectid);
Chris Masondf2ce342007-03-23 11:00:45 -04001291 set_buffer_uptodate(buf);
Chris Mason090d1872007-05-01 08:53:32 -04001292 set_buffer_checked(buf);
Chris Mason7c4452b2007-04-28 09:29:35 -04001293 set_radix_bit(&trans->transaction->dirty_pages, buf->b_page->index);
Chris Masonfec577f2007-02-26 10:40:21 -05001294 return buf;
1295}
Chris Masona28ec192007-03-06 20:08:01 -05001296
Chris Mason6407bf62007-03-27 06:33:00 -04001297static int drop_leaf_ref(struct btrfs_trans_handle *trans,
1298 struct btrfs_root *root, struct buffer_head *cur)
1299{
1300 struct btrfs_disk_key *key;
1301 struct btrfs_leaf *leaf;
1302 struct btrfs_file_extent_item *fi;
1303 int i;
1304 int nritems;
1305 int ret;
1306
1307 BUG_ON(!btrfs_is_leaf(btrfs_buffer_node(cur)));
1308 leaf = btrfs_buffer_leaf(cur);
1309 nritems = btrfs_header_nritems(&leaf->header);
1310 for (i = 0; i < nritems; i++) {
Chris Mason3a686372007-05-24 13:35:57 -04001311 u64 disk_blocknr;
Chris Mason6407bf62007-03-27 06:33:00 -04001312 key = &leaf->items[i].key;
1313 if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY)
1314 continue;
1315 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
Chris Mason236454d2007-04-19 13:37:44 -04001316 if (btrfs_file_extent_type(fi) == BTRFS_FILE_EXTENT_INLINE)
1317 continue;
Chris Mason6407bf62007-03-27 06:33:00 -04001318 /*
1319 * FIXME make sure to insert a trans record that
1320 * repeats the snapshot del on crash
1321 */
Chris Mason3a686372007-05-24 13:35:57 -04001322 disk_blocknr = btrfs_file_extent_disk_blocknr(fi);
1323 if (disk_blocknr == 0)
1324 continue;
1325 ret = btrfs_free_extent(trans, root, disk_blocknr,
Chris Mason6407bf62007-03-27 06:33:00 -04001326 btrfs_file_extent_disk_num_blocks(fi),
1327 0);
1328 BUG_ON(ret);
1329 }
1330 return 0;
1331}
1332
Chris Mason9aca1d52007-03-13 11:09:37 -04001333/*
1334 * helper function for drop_snapshot, this walks down the tree dropping ref
1335 * counts as it goes.
1336 */
Chris Masone089f052007-03-16 16:20:31 -04001337static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
1338 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05001339{
Chris Masone20d96d2007-03-22 12:13:20 -04001340 struct buffer_head *next;
1341 struct buffer_head *cur;
Chris Mason20524f02007-03-10 06:35:47 -05001342 u64 blocknr;
1343 int ret;
1344 u32 refs;
1345
Chris Mason5caf2a02007-04-02 11:20:42 -04001346 WARN_ON(*level < 0);
1347 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Masonb18c6682007-04-17 13:26:50 -04001348 ret = lookup_extent_ref(trans, root, bh_blocknr(path->nodes[*level]),
Chris Mason6407bf62007-03-27 06:33:00 -04001349 1, &refs);
Chris Mason20524f02007-03-10 06:35:47 -05001350 BUG_ON(ret);
1351 if (refs > 1)
1352 goto out;
Chris Mason9aca1d52007-03-13 11:09:37 -04001353 /*
1354 * walk down to the last node level and free all the leaves
1355 */
Chris Mason6407bf62007-03-27 06:33:00 -04001356 while(*level >= 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001357 WARN_ON(*level < 0);
1358 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason20524f02007-03-10 06:35:47 -05001359 cur = path->nodes[*level];
Chris Mason2c90e5d2007-04-02 10:50:19 -04001360 if (btrfs_header_level(btrfs_buffer_header(cur)) != *level)
1361 WARN_ON(1);
Chris Mason7518a232007-03-12 12:01:18 -04001362 if (path->slots[*level] >=
Chris Masone20d96d2007-03-22 12:13:20 -04001363 btrfs_header_nritems(btrfs_buffer_header(cur)))
Chris Mason20524f02007-03-10 06:35:47 -05001364 break;
Chris Mason6407bf62007-03-27 06:33:00 -04001365 if (*level == 0) {
1366 ret = drop_leaf_ref(trans, root, cur);
1367 BUG_ON(ret);
1368 break;
1369 }
Chris Masone20d96d2007-03-22 12:13:20 -04001370 blocknr = btrfs_node_blockptr(btrfs_buffer_node(cur),
1371 path->slots[*level]);
Chris Masonb18c6682007-04-17 13:26:50 -04001372 ret = lookup_extent_ref(trans, root, blocknr, 1, &refs);
Chris Mason6407bf62007-03-27 06:33:00 -04001373 BUG_ON(ret);
1374 if (refs != 1) {
Chris Mason20524f02007-03-10 06:35:47 -05001375 path->slots[*level]++;
Chris Masone089f052007-03-16 16:20:31 -04001376 ret = btrfs_free_extent(trans, root, blocknr, 1, 1);
Chris Mason20524f02007-03-10 06:35:47 -05001377 BUG_ON(ret);
1378 continue;
1379 }
Chris Mason20524f02007-03-10 06:35:47 -05001380 next = read_tree_block(root, blocknr);
Chris Mason5caf2a02007-04-02 11:20:42 -04001381 WARN_ON(*level <= 0);
Chris Mason83e15a22007-03-12 09:03:27 -04001382 if (path->nodes[*level-1])
Chris Mason234b63a2007-03-13 10:46:10 -04001383 btrfs_block_release(root, path->nodes[*level-1]);
Chris Mason20524f02007-03-10 06:35:47 -05001384 path->nodes[*level-1] = next;
Chris Masone20d96d2007-03-22 12:13:20 -04001385 *level = btrfs_header_level(btrfs_buffer_header(next));
Chris Mason20524f02007-03-10 06:35:47 -05001386 path->slots[*level] = 0;
1387 }
1388out:
Chris Mason5caf2a02007-04-02 11:20:42 -04001389 WARN_ON(*level < 0);
1390 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason6407bf62007-03-27 06:33:00 -04001391 ret = btrfs_free_extent(trans, root,
Chris Mason7eccb902007-04-11 15:53:25 -04001392 bh_blocknr(path->nodes[*level]), 1, 1);
Chris Mason234b63a2007-03-13 10:46:10 -04001393 btrfs_block_release(root, path->nodes[*level]);
Chris Mason20524f02007-03-10 06:35:47 -05001394 path->nodes[*level] = NULL;
1395 *level += 1;
1396 BUG_ON(ret);
1397 return 0;
1398}
1399
Chris Mason9aca1d52007-03-13 11:09:37 -04001400/*
1401 * helper for dropping snapshots. This walks back up the tree in the path
1402 * to find the first node higher up where we haven't yet gone through
1403 * all the slots
1404 */
Chris Masone089f052007-03-16 16:20:31 -04001405static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root
1406 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05001407{
1408 int i;
1409 int slot;
1410 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -04001411 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Mason20524f02007-03-10 06:35:47 -05001412 slot = path->slots[i];
Chris Masone20d96d2007-03-22 12:13:20 -04001413 if (slot < btrfs_header_nritems(
1414 btrfs_buffer_header(path->nodes[i])) - 1) {
Chris Mason20524f02007-03-10 06:35:47 -05001415 path->slots[i]++;
1416 *level = i;
1417 return 0;
1418 } else {
Chris Masone089f052007-03-16 16:20:31 -04001419 ret = btrfs_free_extent(trans, root,
Chris Mason7eccb902007-04-11 15:53:25 -04001420 bh_blocknr(path->nodes[*level]),
Chris Masone089f052007-03-16 16:20:31 -04001421 1, 1);
Chris Mason6407bf62007-03-27 06:33:00 -04001422 BUG_ON(ret);
Chris Mason234b63a2007-03-13 10:46:10 -04001423 btrfs_block_release(root, path->nodes[*level]);
Chris Mason83e15a22007-03-12 09:03:27 -04001424 path->nodes[*level] = NULL;
Chris Mason20524f02007-03-10 06:35:47 -05001425 *level = i + 1;
Chris Mason20524f02007-03-10 06:35:47 -05001426 }
1427 }
1428 return 1;
1429}
1430
Chris Mason9aca1d52007-03-13 11:09:37 -04001431/*
1432 * drop the reference count on the tree rooted at 'snap'. This traverses
1433 * the tree freeing any blocks that have a ref count of zero after being
1434 * decremented.
1435 */
Chris Masone089f052007-03-16 16:20:31 -04001436int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masone20d96d2007-03-22 12:13:20 -04001437 *root, struct buffer_head *snap)
Chris Mason20524f02007-03-10 06:35:47 -05001438{
Chris Mason3768f362007-03-13 16:47:54 -04001439 int ret = 0;
Chris Mason9aca1d52007-03-13 11:09:37 -04001440 int wret;
Chris Mason20524f02007-03-10 06:35:47 -05001441 int level;
Chris Mason5caf2a02007-04-02 11:20:42 -04001442 struct btrfs_path *path;
Chris Mason20524f02007-03-10 06:35:47 -05001443 int i;
1444 int orig_level;
1445
Chris Mason5caf2a02007-04-02 11:20:42 -04001446 path = btrfs_alloc_path();
1447 BUG_ON(!path);
1448 btrfs_init_path(path);
Chris Mason20524f02007-03-10 06:35:47 -05001449
Chris Masone20d96d2007-03-22 12:13:20 -04001450 level = btrfs_header_level(btrfs_buffer_header(snap));
Chris Mason20524f02007-03-10 06:35:47 -05001451 orig_level = level;
Chris Mason5caf2a02007-04-02 11:20:42 -04001452 path->nodes[level] = snap;
1453 path->slots[level] = 0;
Chris Mason20524f02007-03-10 06:35:47 -05001454 while(1) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001455 wret = walk_down_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04001456 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05001457 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04001458 if (wret < 0)
1459 ret = wret;
1460
Chris Mason5caf2a02007-04-02 11:20:42 -04001461 wret = walk_up_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04001462 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05001463 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04001464 if (wret < 0)
1465 ret = wret;
Chris Mason35b7e472007-05-02 15:53:43 -04001466 btrfs_btree_balance_dirty(root);
Chris Mason20524f02007-03-10 06:35:47 -05001467 }
Chris Mason83e15a22007-03-12 09:03:27 -04001468 for (i = 0; i <= orig_level; i++) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001469 if (path->nodes[i]) {
1470 btrfs_block_release(root, path->nodes[i]);
Chris Mason83e15a22007-03-12 09:03:27 -04001471 }
Chris Mason20524f02007-03-10 06:35:47 -05001472 }
Chris Mason5caf2a02007-04-02 11:20:42 -04001473 btrfs_free_path(path);
Chris Mason9aca1d52007-03-13 11:09:37 -04001474 return ret;
Chris Mason20524f02007-03-10 06:35:47 -05001475}
Chris Mason9078a3e2007-04-26 16:46:15 -04001476
Chris Masonbe744172007-05-06 10:15:01 -04001477static int free_block_group_radix(struct radix_tree_root *radix)
Chris Mason9078a3e2007-04-26 16:46:15 -04001478{
1479 int ret;
1480 struct btrfs_block_group_cache *cache[8];
1481 int i;
1482
1483 while(1) {
Chris Masonbe744172007-05-06 10:15:01 -04001484 ret = radix_tree_gang_lookup(radix, (void **)cache, 0,
Chris Mason9078a3e2007-04-26 16:46:15 -04001485 ARRAY_SIZE(cache));
1486 if (!ret)
1487 break;
1488 for (i = 0; i < ret; i++) {
Chris Masonbe744172007-05-06 10:15:01 -04001489 radix_tree_delete(radix, cache[i]->key.objectid +
Chris Mason9078a3e2007-04-26 16:46:15 -04001490 cache[i]->key.offset - 1);
1491 kfree(cache[i]);
1492 }
1493 }
1494 return 0;
1495}
1496
Chris Masonbe744172007-05-06 10:15:01 -04001497int btrfs_free_block_groups(struct btrfs_fs_info *info)
1498{
1499 int ret;
1500 int ret2;
Chris Masone37c9e62007-05-09 20:13:14 -04001501 unsigned long gang[16];
1502 int i;
Chris Masonbe744172007-05-06 10:15:01 -04001503
1504 ret = free_block_group_radix(&info->block_group_radix);
1505 ret2 = free_block_group_radix(&info->block_group_data_radix);
1506 if (ret)
1507 return ret;
1508 if (ret2)
1509 return ret2;
Chris Masone37c9e62007-05-09 20:13:14 -04001510
1511 while(1) {
1512 ret = find_first_radix_bit(&info->extent_map_radix,
1513 gang, 0, ARRAY_SIZE(gang));
1514 if (!ret)
1515 break;
1516 for (i = 0; i < ret; i++) {
1517 clear_radix_bit(&info->extent_map_radix, gang[i]);
1518 }
1519 }
Chris Masonbe744172007-05-06 10:15:01 -04001520 return 0;
1521}
1522
Chris Mason9078a3e2007-04-26 16:46:15 -04001523int btrfs_read_block_groups(struct btrfs_root *root)
1524{
1525 struct btrfs_path *path;
1526 int ret;
1527 int err = 0;
1528 struct btrfs_block_group_item *bi;
1529 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04001530 struct btrfs_fs_info *info = root->fs_info;
1531 struct radix_tree_root *radix;
Chris Mason9078a3e2007-04-26 16:46:15 -04001532 struct btrfs_key key;
1533 struct btrfs_key found_key;
1534 struct btrfs_leaf *leaf;
Chris Mason84f54cf2007-06-12 07:43:08 -04001535 u64 group_size_blocks;
Chris Mason31f3c992007-04-30 15:25:45 -04001536 u64 used;
Chris Mason9078a3e2007-04-26 16:46:15 -04001537
Chris Mason84f54cf2007-06-12 07:43:08 -04001538 group_size_blocks = BTRFS_BLOCK_GROUP_SIZE >>
1539 root->fs_info->sb->s_blocksize_bits;
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 Mason84f54cf2007-06-12 07:43:08 -04001593 if (used < div_factor(key.offset, 8)) {
Chris Masonbe744172007-05-06 10:15:01 -04001594 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}