blob: 85616b458e18cb5e39f653a14cea3e9c81000305 [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 Masonbe744172007-05-06 10:15:01 -0400138static struct btrfs_block_group_cache *lookup_block_group(struct
139 btrfs_fs_info *info,
140 u64 blocknr)
141{
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:
211 cache = lookup_block_group(root->fs_info, last + cache->key.offset - 1);
212 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 Mason31f3c992007-04-30 15:25:45 -0400221struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
222 struct btrfs_block_group_cache
Chris Masonbe744172007-05-06 10:15:01 -0400223 *hint, u64 search_start,
Chris Masonde428b62007-05-18 13:28:27 -0400224 int data, int owner)
Chris Masoncd1bc462007-04-27 10:08:34 -0400225{
226 struct btrfs_block_group_cache *cache[8];
Chris Mason31f3c992007-04-30 15:25:45 -0400227 struct btrfs_block_group_cache *found_group = NULL;
Chris Masoncd1bc462007-04-27 10:08:34 -0400228 struct btrfs_fs_info *info = root->fs_info;
Chris Masonbe744172007-05-06 10:15:01 -0400229 struct radix_tree_root *radix;
Chris Mason1e2677e2007-05-29 16:52:18 -0400230 struct radix_tree_root *swap_radix;
Chris Masoncd1bc462007-04-27 10:08:34 -0400231 u64 used;
Chris Mason31f3c992007-04-30 15:25:45 -0400232 u64 last = 0;
233 u64 hint_last;
Chris Masoncd1bc462007-04-27 10:08:34 -0400234 int i;
235 int ret;
Chris Mason31f3c992007-04-30 15:25:45 -0400236 int full_search = 0;
Chris Masonde428b62007-05-18 13:28:27 -0400237 int factor = 8;
Chris Mason1e2677e2007-05-29 16:52:18 -0400238 int data_swap = 0;
Chris Masonde428b62007-05-18 13:28:27 -0400239
240 if (!owner)
241 factor = 5;
Chris Masonbe744172007-05-06 10:15:01 -0400242
Chris Mason1e2677e2007-05-29 16:52:18 -0400243 if (data) {
Chris Masonbe744172007-05-06 10:15:01 -0400244 radix = &info->block_group_data_radix;
Chris Mason1e2677e2007-05-29 16:52:18 -0400245 swap_radix = &info->block_group_radix;
246 } else {
Chris Masonbe744172007-05-06 10:15:01 -0400247 radix = &info->block_group_radix;
Chris Mason1e2677e2007-05-29 16:52:18 -0400248 swap_radix = &info->block_group_data_radix;
249 }
Chris Masonbe744172007-05-06 10:15:01 -0400250
251 if (search_start) {
252 struct btrfs_block_group_cache *shint;
253 shint = lookup_block_group(info, search_start);
254 if (shint->data == data) {
255 used = btrfs_block_group_used(&shint->item);
256 if (used + shint->pinned <
Chris Masonde428b62007-05-18 13:28:27 -0400257 (shint->key.offset * factor) / 10) {
Chris Masonbe744172007-05-06 10:15:01 -0400258 return shint;
259 }
260 }
261 }
262 if (hint && hint->data == data) {
Chris Mason31f3c992007-04-30 15:25:45 -0400263 used = btrfs_block_group_used(&hint->item);
Chris Masonde428b62007-05-18 13:28:27 -0400264 if (used + hint->pinned < (hint->key.offset * factor) / 10) {
Chris Mason31f3c992007-04-30 15:25:45 -0400265 return hint;
266 }
Chris Masonbe744172007-05-06 10:15:01 -0400267 if (used >= (hint->key.offset * 8) / 10) {
268 radix_tree_tag_clear(radix,
269 hint->key.objectid +
270 hint->key.offset - 1,
271 BTRFS_BLOCK_GROUP_AVAIL);
272 }
Chris Mason8d7be552007-05-10 11:24:42 -0400273 last = hint->key.offset * 3;
Chris Masonbe744172007-05-06 10:15:01 -0400274 if (hint->key.objectid >= last)
Chris Masone37c9e62007-05-09 20:13:14 -0400275 last = max(search_start + hint->key.offset - 1,
276 hint->key.objectid - last);
Chris Masonbe744172007-05-06 10:15:01 -0400277 else
278 last = hint->key.objectid + hint->key.offset;
Chris Mason31f3c992007-04-30 15:25:45 -0400279 hint_last = last;
280 } else {
Chris Masone37c9e62007-05-09 20:13:14 -0400281 if (hint)
282 hint_last = max(hint->key.objectid, search_start);
283 else
284 hint_last = search_start;
285
286 last = hint_last;
Chris Mason31f3c992007-04-30 15:25:45 -0400287 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400288 while(1) {
Chris Masonbe744172007-05-06 10:15:01 -0400289 ret = radix_tree_gang_lookup_tag(radix, (void **)cache,
Chris Masoncd1bc462007-04-27 10:08:34 -0400290 last, ARRAY_SIZE(cache),
Chris Mason31f3c992007-04-30 15:25:45 -0400291 BTRFS_BLOCK_GROUP_AVAIL);
Chris Masoncd1bc462007-04-27 10:08:34 -0400292 if (!ret)
293 break;
294 for (i = 0; i < ret; i++) {
Chris Masonbe08c1b2007-05-03 09:06:49 -0400295 last = cache[i]->key.objectid +
296 cache[i]->key.offset;
Chris Masoncd1bc462007-04-27 10:08:34 -0400297 used = btrfs_block_group_used(&cache[i]->item);
Chris Masonbe744172007-05-06 10:15:01 -0400298 if (used + cache[i]->pinned <
Chris Masonde428b62007-05-18 13:28:27 -0400299 (cache[i]->key.offset * factor) / 10) {
Chris Mason31f3c992007-04-30 15:25:45 -0400300 found_group = cache[i];
301 goto found;
Chris Masoncd1bc462007-04-27 10:08:34 -0400302 }
Chris Masonbe744172007-05-06 10:15:01 -0400303 if (used >= (cache[i]->key.offset * 8) / 10) {
304 radix_tree_tag_clear(radix,
305 cache[i]->key.objectid +
306 cache[i]->key.offset - 1,
307 BTRFS_BLOCK_GROUP_AVAIL);
308 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400309 }
Chris Masonde428b62007-05-18 13:28:27 -0400310 cond_resched();
Chris Masoncd1bc462007-04-27 10:08:34 -0400311 }
Chris Mason31f3c992007-04-30 15:25:45 -0400312 last = hint_last;
313again:
Chris Masoncd1bc462007-04-27 10:08:34 -0400314 while(1) {
Chris Masonbe744172007-05-06 10:15:01 -0400315 ret = radix_tree_gang_lookup(radix, (void **)cache,
316 last, ARRAY_SIZE(cache));
Chris Masoncd1bc462007-04-27 10:08:34 -0400317 if (!ret)
318 break;
319 for (i = 0; i < ret; i++) {
Chris Masonbe08c1b2007-05-03 09:06:49 -0400320 last = cache[i]->key.objectid +
321 cache[i]->key.offset;
Chris Masoncd1bc462007-04-27 10:08:34 -0400322 used = btrfs_block_group_used(&cache[i]->item);
Chris Masonbe744172007-05-06 10:15:01 -0400323 if (used + cache[i]->pinned < cache[i]->key.offset) {
Chris Mason31f3c992007-04-30 15:25:45 -0400324 found_group = cache[i];
325 goto found;
Chris Masoncd1bc462007-04-27 10:08:34 -0400326 }
Chris Masonbe744172007-05-06 10:15:01 -0400327 if (used >= cache[i]->key.offset) {
328 radix_tree_tag_clear(radix,
329 cache[i]->key.objectid +
330 cache[i]->key.offset - 1,
331 BTRFS_BLOCK_GROUP_AVAIL);
332 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400333 }
Chris Masonde428b62007-05-18 13:28:27 -0400334 cond_resched();
Chris Masoncd1bc462007-04-27 10:08:34 -0400335 }
Chris Mason31f3c992007-04-30 15:25:45 -0400336 if (!full_search) {
Chris Masonbe744172007-05-06 10:15:01 -0400337 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -0400338 full_search = 1;
339 goto again;
340 }
Chris Mason1e2677e2007-05-29 16:52:18 -0400341 if (!data_swap) {
342 struct radix_tree_root *tmp = radix;
343 data_swap = 1;
344 radix = swap_radix;
345 swap_radix = tmp;
346 last = search_start;
347 goto again;
348 }
Chris Mason31f3c992007-04-30 15:25:45 -0400349 if (!found_group) {
Chris Masonde428b62007-05-18 13:28:27 -0400350printk("find block group bailing to zero data %d\n", data);
Chris Masonbe744172007-05-06 10:15:01 -0400351 ret = radix_tree_gang_lookup(radix,
Chris Mason31f3c992007-04-30 15:25:45 -0400352 (void **)&found_group, 0, 1);
Chris Mason1e2677e2007-05-29 16:52:18 -0400353 if (ret == 0) {
354 ret = radix_tree_gang_lookup(swap_radix,
355 (void **)&found_group,
356 0, 1);
357 }
Chris Mason31f3c992007-04-30 15:25:45 -0400358 BUG_ON(ret != 1);
359 }
Chris Masonbe744172007-05-06 10:15:01 -0400360found:
Chris Mason31f3c992007-04-30 15:25:45 -0400361 return found_group;
Chris Masoncd1bc462007-04-27 10:08:34 -0400362}
363
Chris Masonb18c6682007-04-17 13:26:50 -0400364int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
365 struct btrfs_root *root,
366 u64 blocknr, u64 num_blocks)
Chris Mason02217ed2007-03-02 16:08:05 -0500367{
Chris Mason5caf2a02007-04-02 11:20:42 -0400368 struct btrfs_path *path;
Chris Mason02217ed2007-03-02 16:08:05 -0500369 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400370 struct btrfs_key key;
Chris Mason234b63a2007-03-13 10:46:10 -0400371 struct btrfs_leaf *l;
372 struct btrfs_extent_item *item;
Chris Masone2fa7222007-03-12 16:22:34 -0400373 struct btrfs_key ins;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400374 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500375
Chris Masonfbdc7622007-05-30 10:22:12 -0400376 find_free_extent(trans, root->fs_info->extent_root, 0, 0, (u64)-1, 0,
Chris Masonbe08c1b2007-05-03 09:06:49 -0400377 &ins, 0);
Chris Mason5caf2a02007-04-02 11:20:42 -0400378 path = btrfs_alloc_path();
379 BUG_ON(!path);
380 btrfs_init_path(path);
Chris Mason02217ed2007-03-02 16:08:05 -0500381 key.objectid = blocknr;
382 key.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -0400383 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Mason6407bf62007-03-27 06:33:00 -0400384 key.offset = num_blocks;
Chris Mason5caf2a02007-04-02 11:20:42 -0400385 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -0400386 0, 1);
Chris Masona429e512007-04-18 16:15:28 -0400387 if (ret != 0) {
388printk("can't find block %Lu %Lu\n", blocknr, num_blocks);
Chris Masona28ec192007-03-06 20:08:01 -0500389 BUG();
Chris Masona429e512007-04-18 16:15:28 -0400390 }
Chris Mason02217ed2007-03-02 16:08:05 -0500391 BUG_ON(ret != 0);
Chris Mason5caf2a02007-04-02 11:20:42 -0400392 l = btrfs_buffer_leaf(path->nodes[0]);
393 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400394 refs = btrfs_extent_refs(item);
395 btrfs_set_extent_refs(item, refs + 1);
Chris Mason5caf2a02007-04-02 11:20:42 -0400396 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masona28ec192007-03-06 20:08:01 -0500397
Chris Mason5caf2a02007-04-02 11:20:42 -0400398 btrfs_release_path(root->fs_info->extent_root, path);
399 btrfs_free_path(path);
Chris Mason9f5fae22007-03-20 14:38:32 -0400400 finish_current_insert(trans, root->fs_info->extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -0400401 del_pending_extents(trans, root->fs_info->extent_root);
Chris Mason02217ed2007-03-02 16:08:05 -0500402 return 0;
403}
404
Chris Masonb18c6682007-04-17 13:26:50 -0400405static int lookup_extent_ref(struct btrfs_trans_handle *trans,
406 struct btrfs_root *root, u64 blocknr,
407 u64 num_blocks, u32 *refs)
Chris Masona28ec192007-03-06 20:08:01 -0500408{
Chris Mason5caf2a02007-04-02 11:20:42 -0400409 struct btrfs_path *path;
Chris Masona28ec192007-03-06 20:08:01 -0500410 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400411 struct btrfs_key key;
Chris Mason234b63a2007-03-13 10:46:10 -0400412 struct btrfs_leaf *l;
413 struct btrfs_extent_item *item;
Chris Mason5caf2a02007-04-02 11:20:42 -0400414
415 path = btrfs_alloc_path();
416 btrfs_init_path(path);
Chris Masona28ec192007-03-06 20:08:01 -0500417 key.objectid = blocknr;
Chris Mason6407bf62007-03-27 06:33:00 -0400418 key.offset = num_blocks;
Chris Mason62e27492007-03-15 12:56:47 -0400419 key.flags = 0;
420 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Mason5caf2a02007-04-02 11:20:42 -0400421 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -0400422 0, 0);
Chris Masona28ec192007-03-06 20:08:01 -0500423 if (ret != 0)
424 BUG();
Chris Mason5caf2a02007-04-02 11:20:42 -0400425 l = btrfs_buffer_leaf(path->nodes[0]);
426 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400427 *refs = btrfs_extent_refs(item);
Chris Mason5caf2a02007-04-02 11:20:42 -0400428 btrfs_release_path(root->fs_info->extent_root, path);
429 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -0500430 return 0;
431}
432
Chris Masonc5739bb2007-04-10 09:27:04 -0400433int btrfs_inc_root_ref(struct btrfs_trans_handle *trans,
434 struct btrfs_root *root)
435{
Chris Masonb18c6682007-04-17 13:26:50 -0400436 return btrfs_inc_extent_ref(trans, root, bh_blocknr(root->node), 1);
Chris Masonc5739bb2007-04-10 09:27:04 -0400437}
438
Chris Masone089f052007-03-16 16:20:31 -0400439int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Masone20d96d2007-03-22 12:13:20 -0400440 struct buffer_head *buf)
Chris Mason02217ed2007-03-02 16:08:05 -0500441{
442 u64 blocknr;
Chris Masone20d96d2007-03-22 12:13:20 -0400443 struct btrfs_node *buf_node;
Chris Mason6407bf62007-03-27 06:33:00 -0400444 struct btrfs_leaf *buf_leaf;
445 struct btrfs_disk_key *key;
446 struct btrfs_file_extent_item *fi;
Chris Mason02217ed2007-03-02 16:08:05 -0500447 int i;
Chris Mason6407bf62007-03-27 06:33:00 -0400448 int leaf;
449 int ret;
Chris Masona28ec192007-03-06 20:08:01 -0500450
Chris Mason3768f362007-03-13 16:47:54 -0400451 if (!root->ref_cows)
Chris Masona28ec192007-03-06 20:08:01 -0500452 return 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400453 buf_node = btrfs_buffer_node(buf);
Chris Mason6407bf62007-03-27 06:33:00 -0400454 leaf = btrfs_is_leaf(buf_node);
455 buf_leaf = btrfs_buffer_leaf(buf);
Chris Masone20d96d2007-03-22 12:13:20 -0400456 for (i = 0; i < btrfs_header_nritems(&buf_node->header); i++) {
Chris Mason6407bf62007-03-27 06:33:00 -0400457 if (leaf) {
Chris Mason3a686372007-05-24 13:35:57 -0400458 u64 disk_blocknr;
Chris Mason6407bf62007-03-27 06:33:00 -0400459 key = &buf_leaf->items[i].key;
460 if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY)
461 continue;
462 fi = btrfs_item_ptr(buf_leaf, i,
463 struct btrfs_file_extent_item);
Chris Mason236454d2007-04-19 13:37:44 -0400464 if (btrfs_file_extent_type(fi) ==
465 BTRFS_FILE_EXTENT_INLINE)
466 continue;
Chris Mason3a686372007-05-24 13:35:57 -0400467 disk_blocknr = btrfs_file_extent_disk_blocknr(fi);
468 if (disk_blocknr == 0)
469 continue;
470 ret = btrfs_inc_extent_ref(trans, root, disk_blocknr,
Chris Mason6407bf62007-03-27 06:33:00 -0400471 btrfs_file_extent_disk_num_blocks(fi));
472 BUG_ON(ret);
473 } else {
474 blocknr = btrfs_node_blockptr(buf_node, i);
Chris Masonb18c6682007-04-17 13:26:50 -0400475 ret = btrfs_inc_extent_ref(trans, root, blocknr, 1);
Chris Mason6407bf62007-03-27 06:33:00 -0400476 BUG_ON(ret);
477 }
Chris Mason02217ed2007-03-02 16:08:05 -0500478 }
479 return 0;
480}
481
Chris Mason9078a3e2007-04-26 16:46:15 -0400482static int write_one_cache_group(struct btrfs_trans_handle *trans,
483 struct btrfs_root *root,
484 struct btrfs_path *path,
485 struct btrfs_block_group_cache *cache)
486{
487 int ret;
488 int pending_ret;
489 struct btrfs_root *extent_root = root->fs_info->extent_root;
490 struct btrfs_block_group_item *bi;
491 struct btrfs_key ins;
492
Chris Masonfbdc7622007-05-30 10:22:12 -0400493 find_free_extent(trans, extent_root, 0, 0, (u64)-1, 0, &ins, 0);
Chris Mason9078a3e2007-04-26 16:46:15 -0400494 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
495 BUG_ON(ret);
496 bi = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
497 struct btrfs_block_group_item);
498 memcpy(bi, &cache->item, sizeof(*bi));
499 mark_buffer_dirty(path->nodes[0]);
500 btrfs_release_path(extent_root, path);
501
502 finish_current_insert(trans, extent_root);
503 pending_ret = del_pending_extents(trans, extent_root);
504 if (ret)
505 return ret;
506 if (pending_ret)
507 return pending_ret;
Chris Masonbe744172007-05-06 10:15:01 -0400508 if (cache->data)
509 cache->last_alloc = cache->first_free;
Chris Mason9078a3e2007-04-26 16:46:15 -0400510 return 0;
511
512}
513
Chris Masonbe744172007-05-06 10:15:01 -0400514static int write_dirty_block_radix(struct btrfs_trans_handle *trans,
515 struct btrfs_root *root,
516 struct radix_tree_root *radix)
Chris Mason9078a3e2007-04-26 16:46:15 -0400517{
518 struct btrfs_block_group_cache *cache[8];
519 int ret;
520 int err = 0;
521 int werr = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -0400522 int i;
523 struct btrfs_path *path;
524
525 path = btrfs_alloc_path();
526 if (!path)
527 return -ENOMEM;
528
529 while(1) {
530 ret = radix_tree_gang_lookup_tag(radix, (void **)cache,
531 0, ARRAY_SIZE(cache),
532 BTRFS_BLOCK_GROUP_DIRTY);
533 if (!ret)
534 break;
535 for (i = 0; i < ret; i++) {
536 radix_tree_tag_clear(radix, cache[i]->key.objectid +
537 cache[i]->key.offset - 1,
538 BTRFS_BLOCK_GROUP_DIRTY);
539 err = write_one_cache_group(trans, root,
540 path, cache[i]);
541 if (err)
542 werr = err;
543 }
544 }
545 btrfs_free_path(path);
546 return werr;
547}
548
Chris Masonbe744172007-05-06 10:15:01 -0400549int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
550 struct btrfs_root *root)
551{
552 int ret;
553 int ret2;
554 ret = write_dirty_block_radix(trans, root,
555 &root->fs_info->block_group_radix);
556 ret2 = write_dirty_block_radix(trans, root,
557 &root->fs_info->block_group_data_radix);
558 if (ret)
559 return ret;
560 if (ret2)
561 return ret2;
562 return 0;
563}
564
Chris Mason9078a3e2007-04-26 16:46:15 -0400565static int update_block_group(struct btrfs_trans_handle *trans,
566 struct btrfs_root *root,
Chris Mason1e2677e2007-05-29 16:52:18 -0400567 u64 blocknr, u64 num, int alloc, int mark_free,
568 int data)
Chris Mason9078a3e2007-04-26 16:46:15 -0400569{
570 struct btrfs_block_group_cache *cache;
571 struct btrfs_fs_info *info = root->fs_info;
572 u64 total = num;
573 u64 old_val;
574 u64 block_in_group;
Chris Masone37c9e62007-05-09 20:13:14 -0400575 u64 i;
Chris Mason1e2677e2007-05-29 16:52:18 -0400576 int ret;
Chris Mason3e1ad542007-05-07 20:03:49 -0400577
Chris Mason9078a3e2007-04-26 16:46:15 -0400578 while(total) {
Chris Mason3e1ad542007-05-07 20:03:49 -0400579 cache = lookup_block_group(info, blocknr);
580 if (!cache) {
Chris Masoncd1bc462007-04-27 10:08:34 -0400581 printk(KERN_CRIT "blocknr %Lu lookup failed\n",
582 blocknr);
Chris Mason9078a3e2007-04-26 16:46:15 -0400583 return -1;
Chris Masoncd1bc462007-04-27 10:08:34 -0400584 }
Chris Mason9078a3e2007-04-26 16:46:15 -0400585 block_in_group = blocknr - cache->key.objectid;
586 WARN_ON(block_in_group > cache->key.offset);
Chris Mason3e1ad542007-05-07 20:03:49 -0400587 radix_tree_tag_set(cache->radix, cache->key.objectid +
Chris Masonbe744172007-05-06 10:15:01 -0400588 cache->key.offset - 1,
Chris Mason9078a3e2007-04-26 16:46:15 -0400589 BTRFS_BLOCK_GROUP_DIRTY);
590
591 old_val = btrfs_block_group_used(&cache->item);
592 num = min(total, cache->key.offset - block_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -0400593 if (alloc) {
Chris Masoncd1bc462007-04-27 10:08:34 -0400594 if (blocknr > cache->last_alloc)
595 cache->last_alloc = blocknr;
Chris Masone37c9e62007-05-09 20:13:14 -0400596 if (!cache->data) {
597 for (i = 0; i < num; i++) {
598 clear_radix_bit(&info->extent_map_radix,
599 blocknr + i);
600 }
601 }
Chris Mason1e2677e2007-05-29 16:52:18 -0400602 if (cache->data != data &&
603 old_val < cache->key.offset / 2) {
604printk("changing block group %Lu from %d to %d\n", cache->key.objectid, cache->data, data);
605 cache->data = data;
606 radix_tree_delete(cache->radix,
607 cache->key.objectid +
608 cache->key.offset - 1);
609
610 if (data) {
611 cache->radix =
612 &info->block_group_data_radix;
613 cache->item.flags |=
614 BTRFS_BLOCK_GROUP_DATA;
615 } else {
616 cache->radix = &info->block_group_radix;
617 cache->item.flags &=
618 ~BTRFS_BLOCK_GROUP_DATA;
619 }
620 ret = radix_tree_insert(cache->radix,
621 cache->key.objectid +
622 cache->key.offset - 1,
623 (void *)cache);
624 }
625 old_val += num;
Chris Masoncd1bc462007-04-27 10:08:34 -0400626 } else {
Chris Mason9078a3e2007-04-26 16:46:15 -0400627 old_val -= num;
Chris Masoncd1bc462007-04-27 10:08:34 -0400628 if (blocknr < cache->first_free)
629 cache->first_free = blocknr;
Chris Masone37c9e62007-05-09 20:13:14 -0400630 if (!cache->data && mark_free) {
631 for (i = 0; i < num; i++) {
632 set_radix_bit(&info->extent_map_radix,
633 blocknr + i);
634 }
635 }
Chris Mason1e2677e2007-05-29 16:52:18 -0400636 if (old_val < cache->key.offset / 2 &&
637 old_val + num >= cache->key.offset / 2) {
Chris Masone37c9e62007-05-09 20:13:14 -0400638printk("group %Lu now available\n", cache->key.objectid);
639 radix_tree_tag_set(cache->radix,
640 cache->key.objectid +
641 cache->key.offset - 1,
642 BTRFS_BLOCK_GROUP_AVAIL);
643 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400644 }
Chris Mason9078a3e2007-04-26 16:46:15 -0400645 btrfs_set_block_group_used(&cache->item, old_val);
Chris Masone37c9e62007-05-09 20:13:14 -0400646 total -= num;
647 blocknr += num;
Chris Mason9078a3e2007-04-26 16:46:15 -0400648 }
649 return 0;
650}
651
Chris Masonbe08c1b2007-05-03 09:06:49 -0400652static int try_remove_page(struct address_space *mapping, unsigned long index)
653{
654 int ret;
655 ret = invalidate_mapping_pages(mapping, index, index);
656 return ret;
657}
658
Chris Masone089f052007-03-16 16:20:31 -0400659int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, struct
660 btrfs_root *root)
Chris Masona28ec192007-03-06 20:08:01 -0500661{
Chris Mason8ef97622007-03-26 10:15:30 -0400662 unsigned long gang[8];
Chris Masonbe08c1b2007-05-03 09:06:49 -0400663 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Masonbe744172007-05-06 10:15:01 -0400664 struct btrfs_block_group_cache *block_group;
Chris Mason88fd1462007-03-16 08:56:18 -0400665 u64 first = 0;
Chris Masona28ec192007-03-06 20:08:01 -0500666 int ret;
667 int i;
Chris Mason8ef97622007-03-26 10:15:30 -0400668 struct radix_tree_root *pinned_radix = &root->fs_info->pinned_radix;
Chris Masone37c9e62007-05-09 20:13:14 -0400669 struct radix_tree_root *extent_radix = &root->fs_info->extent_map_radix;
Chris Masona28ec192007-03-06 20:08:01 -0500670
671 while(1) {
Chris Masone37c9e62007-05-09 20:13:14 -0400672 ret = find_first_radix_bit(pinned_radix, gang, 0,
Chris Mason8ef97622007-03-26 10:15:30 -0400673 ARRAY_SIZE(gang));
Chris Masona28ec192007-03-06 20:08:01 -0500674 if (!ret)
675 break;
Chris Mason88fd1462007-03-16 08:56:18 -0400676 if (!first)
Chris Mason8ef97622007-03-26 10:15:30 -0400677 first = gang[0];
Chris Mason0579da42007-03-07 16:15:30 -0500678 for (i = 0; i < ret; i++) {
Chris Mason8ef97622007-03-26 10:15:30 -0400679 clear_radix_bit(pinned_radix, gang[i]);
Chris Masonbe744172007-05-06 10:15:01 -0400680 block_group = lookup_block_group(root->fs_info,
681 gang[i]);
682 if (block_group) {
683 WARN_ON(block_group->pinned == 0);
684 block_group->pinned--;
685 if (gang[i] < block_group->last_alloc)
686 block_group->last_alloc = gang[i];
Chris Masone37c9e62007-05-09 20:13:14 -0400687 if (gang[i] < block_group->last_prealloc)
688 block_group->last_prealloc = gang[i];
689 if (!block_group->data)
690 set_radix_bit(extent_radix, gang[i]);
Chris Masonbe744172007-05-06 10:15:01 -0400691 }
Chris Masonbe08c1b2007-05-03 09:06:49 -0400692 try_remove_page(btree_inode->i_mapping,
693 gang[i] << (PAGE_CACHE_SHIFT -
694 btree_inode->i_blkbits));
Chris Mason0579da42007-03-07 16:15:30 -0500695 }
Chris Masona28ec192007-03-06 20:08:01 -0500696 }
697 return 0;
698}
699
Chris Masone089f052007-03-16 16:20:31 -0400700static int finish_current_insert(struct btrfs_trans_handle *trans, struct
701 btrfs_root *extent_root)
Chris Mason037e6392007-03-07 11:50:24 -0500702{
Chris Masone2fa7222007-03-12 16:22:34 -0400703 struct btrfs_key ins;
Chris Mason234b63a2007-03-13 10:46:10 -0400704 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -0500705 int i;
706 int ret;
Chris Mason1261ec42007-03-20 20:35:03 -0400707 u64 super_blocks_used;
708 struct btrfs_fs_info *info = extent_root->fs_info;
Chris Mason037e6392007-03-07 11:50:24 -0500709
Chris Masoncf27e1e2007-03-13 09:49:06 -0400710 btrfs_set_extent_refs(&extent_item, 1);
Chris Mason037e6392007-03-07 11:50:24 -0500711 ins.offset = 1;
712 ins.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -0400713 btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
Chris Mason5d0c3e62007-04-23 17:01:05 -0400714 btrfs_set_extent_owner(&extent_item, extent_root->root_key.objectid);
Chris Mason037e6392007-03-07 11:50:24 -0500715
Chris Masonf2458e12007-04-25 15:52:25 -0400716 for (i = 0; i < extent_root->fs_info->extent_tree_insert_nr; i++) {
717 ins.objectid = extent_root->fs_info->extent_tree_insert[i];
Chris Mason1261ec42007-03-20 20:35:03 -0400718 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
719 btrfs_set_super_blocks_used(info->disk_super,
720 super_blocks_used + 1);
Chris Masone089f052007-03-16 16:20:31 -0400721 ret = btrfs_insert_item(trans, extent_root, &ins, &extent_item,
722 sizeof(extent_item));
Chris Mason037e6392007-03-07 11:50:24 -0500723 BUG_ON(ret);
724 }
Chris Masonf2458e12007-04-25 15:52:25 -0400725 extent_root->fs_info->extent_tree_insert_nr = 0;
726 extent_root->fs_info->extent_tree_prealloc_nr = 0;
Chris Mason037e6392007-03-07 11:50:24 -0500727 return 0;
728}
729
Chris Mason8ef97622007-03-26 10:15:30 -0400730static int pin_down_block(struct btrfs_root *root, u64 blocknr, int pending)
Chris Masone20d96d2007-03-22 12:13:20 -0400731{
732 int err;
Chris Mason78fae272007-03-25 11:35:08 -0400733 struct btrfs_header *header;
Chris Mason8ef97622007-03-26 10:15:30 -0400734 struct buffer_head *bh;
Chris Mason78fae272007-03-25 11:35:08 -0400735
Chris Masonf4b9aa82007-03-27 11:05:53 -0400736 if (!pending) {
Chris Masond98237b2007-03-28 13:57:48 -0400737 bh = btrfs_find_tree_block(root, blocknr);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400738 if (bh) {
739 if (buffer_uptodate(bh)) {
740 u64 transid =
741 root->fs_info->running_transaction->transid;
742 header = btrfs_buffer_header(bh);
743 if (btrfs_header_generation(header) ==
744 transid) {
745 btrfs_block_release(root, bh);
746 return 0;
747 }
Chris Masonf4b9aa82007-03-27 11:05:53 -0400748 }
Chris Masond6025572007-03-30 14:27:56 -0400749 btrfs_block_release(root, bh);
Chris Mason8ef97622007-03-26 10:15:30 -0400750 }
Chris Mason8ef97622007-03-26 10:15:30 -0400751 err = set_radix_bit(&root->fs_info->pinned_radix, blocknr);
Chris Masonbe744172007-05-06 10:15:01 -0400752 if (!err) {
753 struct btrfs_block_group_cache *cache;
754 cache = lookup_block_group(root->fs_info, blocknr);
755 if (cache)
756 cache->pinned++;
757 }
Chris Masonf4b9aa82007-03-27 11:05:53 -0400758 } else {
759 err = set_radix_bit(&root->fs_info->pending_del_radix, blocknr);
760 }
Chris Masonbe744172007-05-06 10:15:01 -0400761 BUG_ON(err < 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400762 return 0;
763}
764
Chris Masona28ec192007-03-06 20:08:01 -0500765/*
766 * remove an extent from the root, returns 0 on success
767 */
Chris Masone089f052007-03-16 16:20:31 -0400768static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masone37c9e62007-05-09 20:13:14 -0400769 *root, u64 blocknr, u64 num_blocks, int pin,
770 int mark_free)
Chris Masona28ec192007-03-06 20:08:01 -0500771{
Chris Mason5caf2a02007-04-02 11:20:42 -0400772 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -0400773 struct btrfs_key key;
Chris Mason1261ec42007-03-20 20:35:03 -0400774 struct btrfs_fs_info *info = root->fs_info;
775 struct btrfs_root *extent_root = info->extent_root;
Chris Masona28ec192007-03-06 20:08:01 -0500776 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400777 struct btrfs_extent_item *ei;
Chris Masone2fa7222007-03-12 16:22:34 -0400778 struct btrfs_key ins;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400779 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500780
Chris Masona28ec192007-03-06 20:08:01 -0500781 key.objectid = blocknr;
782 key.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -0400783 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masona28ec192007-03-06 20:08:01 -0500784 key.offset = num_blocks;
785
Chris Masonfbdc7622007-05-30 10:22:12 -0400786 find_free_extent(trans, root, 0, 0, (u64)-1, 0, &ins, 0);
Chris Mason5caf2a02007-04-02 11:20:42 -0400787 path = btrfs_alloc_path();
788 BUG_ON(!path);
789 btrfs_init_path(path);
Chris Mason5f26f772007-04-05 10:38:44 -0400790
Chris Mason5caf2a02007-04-02 11:20:42 -0400791 ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
Chris Masona28ec192007-03-06 20:08:01 -0500792 if (ret) {
Chris Mason2e635a22007-03-21 11:12:56 -0400793 printk("failed to find %Lu\n", key.objectid);
Chris Mason234b63a2007-03-13 10:46:10 -0400794 btrfs_print_tree(extent_root, extent_root->node);
Chris Mason2e635a22007-03-21 11:12:56 -0400795 printk("failed to find %Lu\n", key.objectid);
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) {
854 cache = lookup_block_group(extent_root->fs_info,
855 gang[i]);
856 if (cache)
857 cache->pinned++;
858 }
859 if (wret < 0) {
860 printk(KERN_CRIT "set_radix_bit, err %d\n",
861 wret);
862 BUG_ON(wret < 0);
863 }
Chris Mason8ef97622007-03-26 10:15:30 -0400864 wret = clear_radix_bit(pending_radix, gang[i]);
865 BUG_ON(wret);
Chris Masond5719762007-03-23 10:01:08 -0400866 wret = __free_extent(trans, extent_root,
Chris Masone37c9e62007-05-09 20:13:14 -0400867 gang[i], 1, 0, 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400868 if (wret)
869 err = wret;
Chris Masonfec577f2007-02-26 10:40:21 -0500870 }
871 }
Chris Masone20d96d2007-03-22 12:13:20 -0400872 return err;
Chris Masonfec577f2007-02-26 10:40:21 -0500873}
874
875/*
876 * remove an extent from the root, returns 0 on success
877 */
Chris Masone089f052007-03-16 16:20:31 -0400878int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
879 *root, u64 blocknr, u64 num_blocks, int pin)
Chris Masonfec577f2007-02-26 10:40:21 -0500880{
Chris Mason9f5fae22007-03-20 14:38:32 -0400881 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Masonfec577f2007-02-26 10:40:21 -0500882 int pending_ret;
883 int ret;
Chris Masona28ec192007-03-06 20:08:01 -0500884
885 if (root == extent_root) {
Chris Mason8ef97622007-03-26 10:15:30 -0400886 pin_down_block(root, blocknr, 1);
Chris Masona28ec192007-03-06 20:08:01 -0500887 return 0;
888 }
Chris Masone37c9e62007-05-09 20:13:14 -0400889 ret = __free_extent(trans, root, blocknr, num_blocks, pin, pin == 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400890 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
Chris Masonfec577f2007-02-26 10:40:21 -0500891 return ret ? ret : pending_ret;
892}
893
894/*
895 * walks the btree of allocated extents and find a hole of a given size.
896 * The key ins is changed to record the hole:
897 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -0400898 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -0500899 * ins->offset == number of blocks
900 * Any available blocks before search_start are skipped.
901 */
Chris Masone089f052007-03-16 16:20:31 -0400902static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
903 *orig_root, u64 num_blocks, u64 search_start, u64
Chris Masonfbdc7622007-05-30 10:22:12 -0400904 search_end, u64 hint_block,
905 struct btrfs_key *ins, int data)
Chris Masonfec577f2007-02-26 10:40:21 -0500906{
Chris Mason5caf2a02007-04-02 11:20:42 -0400907 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -0400908 struct btrfs_key key;
Chris Masonfec577f2007-02-26 10:40:21 -0500909 int ret;
910 u64 hole_size = 0;
911 int slot = 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400912 u64 last_block = 0;
Chris Mason037e6392007-03-07 11:50:24 -0500913 u64 test_block;
Chris Masonbe744172007-05-06 10:15:01 -0400914 u64 orig_search_start = search_start;
Chris Masonfec577f2007-02-26 10:40:21 -0500915 int start_found;
Chris Mason234b63a2007-03-13 10:46:10 -0400916 struct btrfs_leaf *l;
Chris Mason9f5fae22007-03-20 14:38:32 -0400917 struct btrfs_root * root = orig_root->fs_info->extent_root;
Chris Masonf2458e12007-04-25 15:52:25 -0400918 struct btrfs_fs_info *info = root->fs_info;
Chris Mason0579da42007-03-07 16:15:30 -0500919 int total_needed = num_blocks;
Chris Masonf2458e12007-04-25 15:52:25 -0400920 int total_found = 0;
921 int fill_prealloc = 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400922 int level;
Chris Masonbe08c1b2007-05-03 09:06:49 -0400923 struct btrfs_block_group_cache *block_group;
Chris Masonbe744172007-05-06 10:15:01 -0400924 int full_scan = 0;
Chris Masonfbdc7622007-05-30 10:22:12 -0400925 int wrapped = 0;
Chris Masonde428b62007-05-18 13:28:27 -0400926 u64 limit;
Chris Masonfec577f2007-02-26 10:40:21 -0500927
Chris Masonb1a4d962007-04-04 15:27:52 -0400928 path = btrfs_alloc_path();
929 ins->flags = 0;
930 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
931
Chris Masone20d96d2007-03-22 12:13:20 -0400932 level = btrfs_header_level(btrfs_buffer_header(root->node));
Chris Masonf2458e12007-04-25 15:52:25 -0400933 if (num_blocks == 0) {
934 fill_prealloc = 1;
935 num_blocks = 1;
Chris Mason308535a2007-04-28 15:17:08 -0400936 total_needed = (min(level + 1, BTRFS_MAX_LEVEL) + 2) * 3;
Chris Masonf2458e12007-04-25 15:52:25 -0400937 }
Chris Mason3e1ad542007-05-07 20:03:49 -0400938 if (search_end == (u64)-1)
939 search_end = btrfs_super_total_blocks(info->disk_super);
Chris Masonfbdc7622007-05-30 10:22:12 -0400940 if (hint_block) {
941 block_group = lookup_block_group(info, hint_block);
Chris Masonbe744172007-05-06 10:15:01 -0400942 block_group = btrfs_find_block_group(root, block_group,
Chris Masonfbdc7622007-05-30 10:22:12 -0400943 hint_block, data, 1);
Chris Masonbe744172007-05-06 10:15:01 -0400944 } else {
945 block_group = btrfs_find_block_group(root,
946 trans->block_group, 0,
Chris Masonde428b62007-05-18 13:28:27 -0400947 data, 1);
Chris Masonbe744172007-05-06 10:15:01 -0400948 }
949
950check_failed:
Chris Mason1e2677e2007-05-29 16:52:18 -0400951 if (!block_group->data)
Chris Masone37c9e62007-05-09 20:13:14 -0400952 search_start = find_search_start(root, &block_group,
953 search_start, total_needed);
Chris Masonfbdc7622007-05-30 10:22:12 -0400954 else if (!full_scan)
Chris Masone37c9e62007-05-09 20:13:14 -0400955 search_start = max(block_group->last_alloc, search_start);
956
Chris Mason5caf2a02007-04-02 11:20:42 -0400957 btrfs_init_path(path);
Chris Masonfec577f2007-02-26 10:40:21 -0500958 ins->objectid = search_start;
959 ins->offset = 0;
Chris Masonfec577f2007-02-26 10:40:21 -0500960 start_found = 0;
Chris Masone37c9e62007-05-09 20:13:14 -0400961
Chris Mason5caf2a02007-04-02 11:20:42 -0400962 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
Chris Mason0f70abe2007-02-28 16:46:22 -0500963 if (ret < 0)
964 goto error;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500965
Chris Masone37c9e62007-05-09 20:13:14 -0400966 if (path->slots[0] > 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400967 path->slots[0]--;
Chris Masone37c9e62007-05-09 20:13:14 -0400968 }
969
970 l = btrfs_buffer_leaf(path->nodes[0]);
971 btrfs_disk_key_to_cpu(&key, &l->items[path->slots[0]].key);
972 /*
973 * a rare case, go back one key if we hit a block group item
974 * instead of an extent item
975 */
976 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY &&
977 key.objectid + key.offset >= search_start) {
978 ins->objectid = key.objectid;
979 ins->offset = key.offset - 1;
980 btrfs_release_path(root, path);
981 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
982 if (ret < 0)
983 goto error;
984
985 if (path->slots[0] > 0) {
986 path->slots[0]--;
987 }
988 }
Chris Mason0579da42007-03-07 16:15:30 -0500989
Chris Masonfec577f2007-02-26 10:40:21 -0500990 while (1) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400991 l = btrfs_buffer_leaf(path->nodes[0]);
992 slot = path->slots[0];
Chris Mason7518a232007-03-12 12:01:18 -0400993 if (slot >= btrfs_header_nritems(&l->header)) {
Chris Masonf2458e12007-04-25 15:52:25 -0400994 if (fill_prealloc) {
995 info->extent_tree_prealloc_nr = 0;
996 total_found = 0;
997 }
Chris Masonde428b62007-05-18 13:28:27 -0400998 if (start_found)
999 limit = last_block +
1000 block_group->key.offset / 2;
1001 else
1002 limit = search_start +
1003 block_group->key.offset / 2;
Chris Mason5caf2a02007-04-02 11:20:42 -04001004 ret = btrfs_next_leaf(root, path);
Chris Masonfec577f2007-02-26 10:40:21 -05001005 if (ret == 0)
1006 continue;
Chris Mason0f70abe2007-02-28 16:46:22 -05001007 if (ret < 0)
1008 goto error;
Chris Masonfec577f2007-02-26 10:40:21 -05001009 if (!start_found) {
1010 ins->objectid = search_start;
Chris Mason3e1ad542007-05-07 20:03:49 -04001011 ins->offset = search_end - search_start;
Chris Masonfec577f2007-02-26 10:40:21 -05001012 start_found = 1;
1013 goto check_pending;
1014 }
1015 ins->objectid = last_block > search_start ?
1016 last_block : search_start;
Chris Mason3e1ad542007-05-07 20:03:49 -04001017 ins->offset = search_end - ins->objectid;
Chris Masonfec577f2007-02-26 10:40:21 -05001018 goto check_pending;
1019 }
Chris Masone37c9e62007-05-09 20:13:14 -04001020
Chris Masone2fa7222007-03-12 16:22:34 -04001021 btrfs_disk_key_to_cpu(&key, &l->items[slot].key);
Chris Masone37c9e62007-05-09 20:13:14 -04001022 if (key.objectid >= search_start && key.objectid > last_block &&
1023 start_found) {
1024 if (last_block < search_start)
1025 last_block = search_start;
1026 hole_size = key.objectid - last_block;
1027 if (hole_size >= num_blocks) {
1028 ins->objectid = last_block;
1029 ins->offset = hole_size;
1030 goto check_pending;
Chris Mason0579da42007-03-07 16:15:30 -05001031 }
Chris Masonfec577f2007-02-26 10:40:21 -05001032 }
Chris Masone37c9e62007-05-09 20:13:14 -04001033
1034 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY)
1035 goto next;
1036
Chris Mason0579da42007-03-07 16:15:30 -05001037 start_found = 1;
Chris Masone2fa7222007-03-12 16:22:34 -04001038 last_block = key.objectid + key.offset;
Chris Masonfbdc7622007-05-30 10:22:12 -04001039 if (!full_scan && last_block >= block_group->key.objectid +
Chris Masonbe744172007-05-06 10:15:01 -04001040 block_group->key.offset) {
1041 btrfs_release_path(root, path);
1042 search_start = block_group->key.objectid +
1043 block_group->key.offset * 2;
1044 goto new_group;
1045 }
Chris Mason9078a3e2007-04-26 16:46:15 -04001046next:
Chris Mason5caf2a02007-04-02 11:20:42 -04001047 path->slots[0]++;
Chris Masonde428b62007-05-18 13:28:27 -04001048 cond_resched();
Chris Masonfec577f2007-02-26 10:40:21 -05001049 }
1050 // FIXME -ENOSPC
1051check_pending:
1052 /* we have to make sure we didn't find an extent that has already
1053 * been allocated by the map tree or the original allocation
1054 */
Chris Mason5caf2a02007-04-02 11:20:42 -04001055 btrfs_release_path(root, path);
Chris Masonfec577f2007-02-26 10:40:21 -05001056 BUG_ON(ins->objectid < search_start);
Chris Masone37c9e62007-05-09 20:13:14 -04001057
Chris Mason3e1ad542007-05-07 20:03:49 -04001058 if (ins->objectid + num_blocks >= search_end) {
Chris Masonfbdc7622007-05-30 10:22:12 -04001059 if (full_scan) {
1060 ret = -ENOSPC;
1061 goto error;
1062 }
Chris Masonbe744172007-05-06 10:15:01 -04001063 search_start = orig_search_start;
Chris Masonfbdc7622007-05-30 10:22:12 -04001064 if (wrapped)
1065 full_scan = 1;
1066 else
1067 wrapped = 1;
Chris Masonbe744172007-05-06 10:15:01 -04001068 goto new_group;
Chris Mason06a2f9f2007-04-28 08:48:10 -04001069 }
Chris Mason037e6392007-03-07 11:50:24 -05001070 for (test_block = ins->objectid;
Chris Masonf2458e12007-04-25 15:52:25 -04001071 test_block < ins->objectid + num_blocks; test_block++) {
1072 if (test_radix_bit(&info->pinned_radix, test_block)) {
Chris Mason037e6392007-03-07 11:50:24 -05001073 search_start = test_block + 1;
Chris Masonbe744172007-05-06 10:15:01 -04001074 goto new_group;
Chris Masonfec577f2007-02-26 10:40:21 -05001075 }
1076 }
Chris Masonf2458e12007-04-25 15:52:25 -04001077 if (!fill_prealloc && info->extent_tree_insert_nr) {
1078 u64 last =
1079 info->extent_tree_insert[info->extent_tree_insert_nr - 1];
1080 if (ins->objectid + num_blocks >
1081 info->extent_tree_insert[0] &&
1082 ins->objectid <= last) {
1083 search_start = last + 1;
Chris Masone37c9e62007-05-09 20:13:14 -04001084 WARN_ON(!full_scan);
Chris Masonbe744172007-05-06 10:15:01 -04001085 goto new_group;
Chris Masonf2458e12007-04-25 15:52:25 -04001086 }
1087 }
1088 if (!fill_prealloc && info->extent_tree_prealloc_nr) {
1089 u64 first =
1090 info->extent_tree_prealloc[info->extent_tree_prealloc_nr - 1];
1091 if (ins->objectid + num_blocks > first &&
1092 ins->objectid <= info->extent_tree_prealloc[0]) {
1093 search_start = info->extent_tree_prealloc[0] + 1;
Chris Masone37c9e62007-05-09 20:13:14 -04001094 WARN_ON(!full_scan);
Chris Masonbe744172007-05-06 10:15:01 -04001095 goto new_group;
Chris Masonf2458e12007-04-25 15:52:25 -04001096 }
1097 }
1098 if (fill_prealloc) {
1099 int nr;
1100 test_block = ins->objectid;
Chris Masone37c9e62007-05-09 20:13:14 -04001101 if (test_block - info->extent_tree_prealloc[total_needed - 1] >=
1102 leaf_range(root)) {
1103 total_found = 0;
1104 info->extent_tree_prealloc_nr = total_found;
1105 }
Chris Masonf2458e12007-04-25 15:52:25 -04001106 while(test_block < ins->objectid + ins->offset &&
1107 total_found < total_needed) {
1108 nr = total_needed - total_found - 1;
1109 BUG_ON(nr < 0);
Chris Masoncd1bc462007-04-27 10:08:34 -04001110 info->extent_tree_prealloc[nr] = test_block;
Chris Masonf2458e12007-04-25 15:52:25 -04001111 total_found++;
1112 test_block++;
1113 }
1114 if (total_found < total_needed) {
1115 search_start = test_block;
Chris Masonbe744172007-05-06 10:15:01 -04001116 goto new_group;
Chris Masonf2458e12007-04-25 15:52:25 -04001117 }
Chris Masoncd1bc462007-04-27 10:08:34 -04001118 info->extent_tree_prealloc_nr = total_found;
Chris Masonf2458e12007-04-25 15:52:25 -04001119 }
Chris Masone37c9e62007-05-09 20:13:14 -04001120 if (!data) {
1121 block_group = lookup_block_group(info, ins->objectid);
1122 if (block_group) {
1123 if (fill_prealloc)
1124 block_group->last_prealloc =
1125 info->extent_tree_prealloc[total_needed-1];
1126 else
1127 trans->block_group = block_group;
1128 }
Chris Masoncd1bc462007-04-27 10:08:34 -04001129 }
Chris Mason037e6392007-03-07 11:50:24 -05001130 ins->offset = num_blocks;
Chris Mason5caf2a02007-04-02 11:20:42 -04001131 btrfs_free_path(path);
Chris Masonfec577f2007-02-26 10:40:21 -05001132 return 0;
Chris Masonbe744172007-05-06 10:15:01 -04001133
1134new_group:
Chris Mason3e1ad542007-05-07 20:03:49 -04001135 if (search_start + num_blocks >= search_end) {
Chris Masonbe744172007-05-06 10:15:01 -04001136 search_start = orig_search_start;
Chris Masonfbdc7622007-05-30 10:22:12 -04001137 if (full_scan) {
1138 ret = -ENOSPC;
1139 goto error;
1140 }
1141 if (wrapped)
1142 full_scan = 1;
1143 else
1144 wrapped = 1;
Chris Masonbe744172007-05-06 10:15:01 -04001145 }
1146 block_group = lookup_block_group(info, search_start);
Chris Masonfbdc7622007-05-30 10:22:12 -04001147 cond_resched();
Chris Masonbe744172007-05-06 10:15:01 -04001148 if (!full_scan)
1149 block_group = btrfs_find_block_group(root, block_group,
Chris Masonde428b62007-05-18 13:28:27 -04001150 search_start, data, 0);
Chris Masonbe744172007-05-06 10:15:01 -04001151 goto check_failed;
1152
Chris Mason0f70abe2007-02-28 16:46:22 -05001153error:
Chris Mason5caf2a02007-04-02 11:20:42 -04001154 btrfs_release_path(root, path);
1155 btrfs_free_path(path);
Chris Mason0f70abe2007-02-28 16:46:22 -05001156 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05001157}
Chris Masonfec577f2007-02-26 10:40:21 -05001158/*
Chris Masonfec577f2007-02-26 10:40:21 -05001159 * finds a free extent and does all the dirty work required for allocation
1160 * returns the key for the extent through ins, and a tree buffer for
1161 * the first block of the extent through buf.
1162 *
1163 * returns 0 if everything worked, non-zero otherwise.
1164 */
Chris Mason4d775672007-04-20 20:23:12 -04001165int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
1166 struct btrfs_root *root, u64 owner,
Chris Masonfbdc7622007-05-30 10:22:12 -04001167 u64 num_blocks, u64 hint_block,
Chris Masonbe08c1b2007-05-03 09:06:49 -04001168 u64 search_end, struct btrfs_key *ins, int data)
Chris Masonfec577f2007-02-26 10:40:21 -05001169{
1170 int ret;
1171 int pending_ret;
Chris Mason1261ec42007-03-20 20:35:03 -04001172 u64 super_blocks_used;
Chris Masonfbdc7622007-05-30 10:22:12 -04001173 u64 search_start = 0;
Chris Mason1261ec42007-03-20 20:35:03 -04001174 struct btrfs_fs_info *info = root->fs_info;
1175 struct btrfs_root *extent_root = info->extent_root;
Chris Mason234b63a2007-03-13 10:46:10 -04001176 struct btrfs_extent_item extent_item;
Chris Masonf2458e12007-04-25 15:52:25 -04001177 struct btrfs_key prealloc_key;
Chris Mason037e6392007-03-07 11:50:24 -05001178
Chris Masoncf27e1e2007-03-13 09:49:06 -04001179 btrfs_set_extent_refs(&extent_item, 1);
Chris Mason4d775672007-04-20 20:23:12 -04001180 btrfs_set_extent_owner(&extent_item, owner);
Chris Masonfec577f2007-02-26 10:40:21 -05001181
Chris Mason037e6392007-03-07 11:50:24 -05001182 if (root == extent_root) {
Chris Masonf2458e12007-04-25 15:52:25 -04001183 int nr;
1184 BUG_ON(info->extent_tree_prealloc_nr == 0);
Chris Mason037e6392007-03-07 11:50:24 -05001185 BUG_ON(num_blocks != 1);
Chris Mason037e6392007-03-07 11:50:24 -05001186 ins->offset = 1;
Chris Masonf2458e12007-04-25 15:52:25 -04001187 info->extent_tree_prealloc_nr--;
1188 nr = info->extent_tree_prealloc_nr;
1189 ins->objectid = info->extent_tree_prealloc[nr];
1190 info->extent_tree_insert[info->extent_tree_insert_nr++] =
1191 ins->objectid;
Chris Mason9078a3e2007-04-26 16:46:15 -04001192 ret = update_block_group(trans, root,
Chris Mason1e2677e2007-05-29 16:52:18 -04001193 ins->objectid, ins->offset, 1, 0, 0);
Chris Mason9078a3e2007-04-26 16:46:15 -04001194 BUG_ON(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05001195 return 0;
1196 }
Chris Masone37c9e62007-05-09 20:13:14 -04001197
1198 /*
1199 * if we're doing a data allocation, preallocate room in the
1200 * extent tree first. This way the extent tree blocks end up
1201 * in the correct block group.
1202 */
1203 if (data) {
Chris Masonde428b62007-05-18 13:28:27 -04001204 ret = find_free_extent(trans, root, 0, 0,
Chris Masonfbdc7622007-05-30 10:22:12 -04001205 search_end, 0, &prealloc_key, 0);
Chris Masone37c9e62007-05-09 20:13:14 -04001206 if (ret) {
1207 return ret;
1208 }
1209 if (prealloc_key.objectid + prealloc_key.offset >= search_end) {
1210 int nr = info->extent_tree_prealloc_nr;
1211 search_end = info->extent_tree_prealloc[nr - 1] - 1;
1212 } else {
1213 search_start = info->extent_tree_prealloc[0] + 1;
1214 }
1215 }
Chris Masonfbdc7622007-05-30 10:22:12 -04001216 if (hint_block < search_start)
1217 hint_block = search_start;
Chris Masonf2458e12007-04-25 15:52:25 -04001218 /* do the real allocation */
Chris Masone089f052007-03-16 16:20:31 -04001219 ret = find_free_extent(trans, root, num_blocks, search_start,
Chris Masonfbdc7622007-05-30 10:22:12 -04001220 search_end, hint_block, ins, data);
Chris Masone37c9e62007-05-09 20:13:14 -04001221 if (ret) {
Chris Mason037e6392007-03-07 11:50:24 -05001222 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -04001223 }
Chris Masonfec577f2007-02-26 10:40:21 -05001224
Chris Masone37c9e62007-05-09 20:13:14 -04001225 /*
1226 * if we're doing a metadata allocation, preallocate space in the
1227 * extent tree second. This way, we don't create a tiny hole
1228 * in the allocation map between any unused preallocation blocks
1229 * and the metadata block we're actually allocating. On disk,
1230 * it'll go:
1231 * [block we've allocated], [used prealloc 1], [ unused prealloc ]
1232 * The unused prealloc will get reused the next time around.
1233 */
1234 if (!data) {
1235 if (ins->objectid + ins->offset >= search_end)
1236 search_end = ins->objectid - 1;
1237 else
1238 search_start = ins->objectid + ins->offset;
Chris Mason3e1ad542007-05-07 20:03:49 -04001239
Chris Masonfbdc7622007-05-30 10:22:12 -04001240 if (hint_block < search_start)
1241 hint_block = search_start;
1242
Chris Masone37c9e62007-05-09 20:13:14 -04001243 ret = find_free_extent(trans, root, 0, search_start,
Chris Masonfbdc7622007-05-30 10:22:12 -04001244 search_end, hint_block,
1245 &prealloc_key, 0);
Chris Masone37c9e62007-05-09 20:13:14 -04001246 if (ret) {
1247 return ret;
1248 }
1249 }
Chris Masonf2458e12007-04-25 15:52:25 -04001250
Chris Mason1261ec42007-03-20 20:35:03 -04001251 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
1252 btrfs_set_super_blocks_used(info->disk_super, super_blocks_used +
1253 num_blocks);
Chris Masone089f052007-03-16 16:20:31 -04001254 ret = btrfs_insert_item(trans, extent_root, ins, &extent_item,
1255 sizeof(extent_item));
Chris Mason037e6392007-03-07 11:50:24 -05001256
Chris Masone089f052007-03-16 16:20:31 -04001257 finish_current_insert(trans, extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -04001258 pending_ret = del_pending_extents(trans, extent_root);
Chris Masone37c9e62007-05-09 20:13:14 -04001259 if (ret) {
Chris Mason037e6392007-03-07 11:50:24 -05001260 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -04001261 }
1262 if (pending_ret) {
Chris Mason037e6392007-03-07 11:50:24 -05001263 return pending_ret;
Chris Masone37c9e62007-05-09 20:13:14 -04001264 }
Chris Mason1e2677e2007-05-29 16:52:18 -04001265 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0,
1266 data);
Chris Mason037e6392007-03-07 11:50:24 -05001267 return 0;
Chris Masonfec577f2007-02-26 10:40:21 -05001268}
1269
1270/*
1271 * helper function to allocate a block for a given tree
1272 * returns the tree buffer or NULL.
1273 */
Chris Masone20d96d2007-03-22 12:13:20 -04001274struct buffer_head *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Chris Mason31f3c992007-04-30 15:25:45 -04001275 struct btrfs_root *root, u64 hint)
Chris Masonfec577f2007-02-26 10:40:21 -05001276{
Chris Masone2fa7222007-03-12 16:22:34 -04001277 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -05001278 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -04001279 struct buffer_head *buf;
Chris Masonfec577f2007-02-26 10:40:21 -05001280
Chris Mason4d775672007-04-20 20:23:12 -04001281 ret = btrfs_alloc_extent(trans, root, root->root_key.objectid,
Chris Masonde428b62007-05-18 13:28:27 -04001282 1, hint, (unsigned long)-1, &ins, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05001283 if (ret) {
1284 BUG();
1285 return NULL;
1286 }
Chris Mason9078a3e2007-04-26 16:46:15 -04001287 BUG_ON(ret);
Chris Masond98237b2007-03-28 13:57:48 -04001288 buf = btrfs_find_create_tree_block(root, ins.objectid);
Chris Masondf2ce342007-03-23 11:00:45 -04001289 set_buffer_uptodate(buf);
Chris Mason090d1872007-05-01 08:53:32 -04001290 set_buffer_checked(buf);
Chris Mason7c4452b2007-04-28 09:29:35 -04001291 set_radix_bit(&trans->transaction->dirty_pages, buf->b_page->index);
Chris Masonfec577f2007-02-26 10:40:21 -05001292 return buf;
1293}
Chris Masona28ec192007-03-06 20:08:01 -05001294
Chris Mason6407bf62007-03-27 06:33:00 -04001295static int drop_leaf_ref(struct btrfs_trans_handle *trans,
1296 struct btrfs_root *root, struct buffer_head *cur)
1297{
1298 struct btrfs_disk_key *key;
1299 struct btrfs_leaf *leaf;
1300 struct btrfs_file_extent_item *fi;
1301 int i;
1302 int nritems;
1303 int ret;
1304
1305 BUG_ON(!btrfs_is_leaf(btrfs_buffer_node(cur)));
1306 leaf = btrfs_buffer_leaf(cur);
1307 nritems = btrfs_header_nritems(&leaf->header);
1308 for (i = 0; i < nritems; i++) {
Chris Mason3a686372007-05-24 13:35:57 -04001309 u64 disk_blocknr;
Chris Mason6407bf62007-03-27 06:33:00 -04001310 key = &leaf->items[i].key;
1311 if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY)
1312 continue;
1313 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
Chris Mason236454d2007-04-19 13:37:44 -04001314 if (btrfs_file_extent_type(fi) == BTRFS_FILE_EXTENT_INLINE)
1315 continue;
Chris Mason6407bf62007-03-27 06:33:00 -04001316 /*
1317 * FIXME make sure to insert a trans record that
1318 * repeats the snapshot del on crash
1319 */
Chris Mason3a686372007-05-24 13:35:57 -04001320 disk_blocknr = btrfs_file_extent_disk_blocknr(fi);
1321 if (disk_blocknr == 0)
1322 continue;
1323 ret = btrfs_free_extent(trans, root, disk_blocknr,
Chris Mason6407bf62007-03-27 06:33:00 -04001324 btrfs_file_extent_disk_num_blocks(fi),
1325 0);
1326 BUG_ON(ret);
1327 }
1328 return 0;
1329}
1330
Chris Mason9aca1d52007-03-13 11:09:37 -04001331/*
1332 * helper function for drop_snapshot, this walks down the tree dropping ref
1333 * counts as it goes.
1334 */
Chris Masone089f052007-03-16 16:20:31 -04001335static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
1336 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05001337{
Chris Masone20d96d2007-03-22 12:13:20 -04001338 struct buffer_head *next;
1339 struct buffer_head *cur;
Chris Mason20524f02007-03-10 06:35:47 -05001340 u64 blocknr;
1341 int ret;
1342 u32 refs;
1343
Chris Mason5caf2a02007-04-02 11:20:42 -04001344 WARN_ON(*level < 0);
1345 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Masonb18c6682007-04-17 13:26:50 -04001346 ret = lookup_extent_ref(trans, root, bh_blocknr(path->nodes[*level]),
Chris Mason6407bf62007-03-27 06:33:00 -04001347 1, &refs);
Chris Mason20524f02007-03-10 06:35:47 -05001348 BUG_ON(ret);
1349 if (refs > 1)
1350 goto out;
Chris Mason9aca1d52007-03-13 11:09:37 -04001351 /*
1352 * walk down to the last node level and free all the leaves
1353 */
Chris Mason6407bf62007-03-27 06:33:00 -04001354 while(*level >= 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001355 WARN_ON(*level < 0);
1356 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason20524f02007-03-10 06:35:47 -05001357 cur = path->nodes[*level];
Chris Mason2c90e5d2007-04-02 10:50:19 -04001358 if (btrfs_header_level(btrfs_buffer_header(cur)) != *level)
1359 WARN_ON(1);
Chris Mason7518a232007-03-12 12:01:18 -04001360 if (path->slots[*level] >=
Chris Masone20d96d2007-03-22 12:13:20 -04001361 btrfs_header_nritems(btrfs_buffer_header(cur)))
Chris Mason20524f02007-03-10 06:35:47 -05001362 break;
Chris Mason6407bf62007-03-27 06:33:00 -04001363 if (*level == 0) {
1364 ret = drop_leaf_ref(trans, root, cur);
1365 BUG_ON(ret);
1366 break;
1367 }
Chris Masone20d96d2007-03-22 12:13:20 -04001368 blocknr = btrfs_node_blockptr(btrfs_buffer_node(cur),
1369 path->slots[*level]);
Chris Masonb18c6682007-04-17 13:26:50 -04001370 ret = lookup_extent_ref(trans, root, blocknr, 1, &refs);
Chris Mason6407bf62007-03-27 06:33:00 -04001371 BUG_ON(ret);
1372 if (refs != 1) {
Chris Mason20524f02007-03-10 06:35:47 -05001373 path->slots[*level]++;
Chris Masone089f052007-03-16 16:20:31 -04001374 ret = btrfs_free_extent(trans, root, blocknr, 1, 1);
Chris Mason20524f02007-03-10 06:35:47 -05001375 BUG_ON(ret);
1376 continue;
1377 }
Chris Mason20524f02007-03-10 06:35:47 -05001378 next = read_tree_block(root, blocknr);
Chris Mason5caf2a02007-04-02 11:20:42 -04001379 WARN_ON(*level <= 0);
Chris Mason83e15a22007-03-12 09:03:27 -04001380 if (path->nodes[*level-1])
Chris Mason234b63a2007-03-13 10:46:10 -04001381 btrfs_block_release(root, path->nodes[*level-1]);
Chris Mason20524f02007-03-10 06:35:47 -05001382 path->nodes[*level-1] = next;
Chris Masone20d96d2007-03-22 12:13:20 -04001383 *level = btrfs_header_level(btrfs_buffer_header(next));
Chris Mason20524f02007-03-10 06:35:47 -05001384 path->slots[*level] = 0;
1385 }
1386out:
Chris Mason5caf2a02007-04-02 11:20:42 -04001387 WARN_ON(*level < 0);
1388 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason6407bf62007-03-27 06:33:00 -04001389 ret = btrfs_free_extent(trans, root,
Chris Mason7eccb902007-04-11 15:53:25 -04001390 bh_blocknr(path->nodes[*level]), 1, 1);
Chris Mason234b63a2007-03-13 10:46:10 -04001391 btrfs_block_release(root, path->nodes[*level]);
Chris Mason20524f02007-03-10 06:35:47 -05001392 path->nodes[*level] = NULL;
1393 *level += 1;
1394 BUG_ON(ret);
1395 return 0;
1396}
1397
Chris Mason9aca1d52007-03-13 11:09:37 -04001398/*
1399 * helper for dropping snapshots. This walks back up the tree in the path
1400 * to find the first node higher up where we haven't yet gone through
1401 * all the slots
1402 */
Chris Masone089f052007-03-16 16:20:31 -04001403static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root
1404 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05001405{
1406 int i;
1407 int slot;
1408 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -04001409 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Mason20524f02007-03-10 06:35:47 -05001410 slot = path->slots[i];
Chris Masone20d96d2007-03-22 12:13:20 -04001411 if (slot < btrfs_header_nritems(
1412 btrfs_buffer_header(path->nodes[i])) - 1) {
Chris Mason20524f02007-03-10 06:35:47 -05001413 path->slots[i]++;
1414 *level = i;
1415 return 0;
1416 } else {
Chris Masone089f052007-03-16 16:20:31 -04001417 ret = btrfs_free_extent(trans, root,
Chris Mason7eccb902007-04-11 15:53:25 -04001418 bh_blocknr(path->nodes[*level]),
Chris Masone089f052007-03-16 16:20:31 -04001419 1, 1);
Chris Mason6407bf62007-03-27 06:33:00 -04001420 BUG_ON(ret);
Chris Mason234b63a2007-03-13 10:46:10 -04001421 btrfs_block_release(root, path->nodes[*level]);
Chris Mason83e15a22007-03-12 09:03:27 -04001422 path->nodes[*level] = NULL;
Chris Mason20524f02007-03-10 06:35:47 -05001423 *level = i + 1;
Chris Mason20524f02007-03-10 06:35:47 -05001424 }
1425 }
1426 return 1;
1427}
1428
Chris Mason9aca1d52007-03-13 11:09:37 -04001429/*
1430 * drop the reference count on the tree rooted at 'snap'. This traverses
1431 * the tree freeing any blocks that have a ref count of zero after being
1432 * decremented.
1433 */
Chris Masone089f052007-03-16 16:20:31 -04001434int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masone20d96d2007-03-22 12:13:20 -04001435 *root, struct buffer_head *snap)
Chris Mason20524f02007-03-10 06:35:47 -05001436{
Chris Mason3768f362007-03-13 16:47:54 -04001437 int ret = 0;
Chris Mason9aca1d52007-03-13 11:09:37 -04001438 int wret;
Chris Mason20524f02007-03-10 06:35:47 -05001439 int level;
Chris Mason5caf2a02007-04-02 11:20:42 -04001440 struct btrfs_path *path;
Chris Mason20524f02007-03-10 06:35:47 -05001441 int i;
1442 int orig_level;
1443
Chris Mason5caf2a02007-04-02 11:20:42 -04001444 path = btrfs_alloc_path();
1445 BUG_ON(!path);
1446 btrfs_init_path(path);
Chris Mason20524f02007-03-10 06:35:47 -05001447
Chris Masone20d96d2007-03-22 12:13:20 -04001448 level = btrfs_header_level(btrfs_buffer_header(snap));
Chris Mason20524f02007-03-10 06:35:47 -05001449 orig_level = level;
Chris Mason5caf2a02007-04-02 11:20:42 -04001450 path->nodes[level] = snap;
1451 path->slots[level] = 0;
Chris Mason20524f02007-03-10 06:35:47 -05001452 while(1) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001453 wret = walk_down_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04001454 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05001455 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04001456 if (wret < 0)
1457 ret = wret;
1458
Chris Mason5caf2a02007-04-02 11:20:42 -04001459 wret = walk_up_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04001460 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05001461 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04001462 if (wret < 0)
1463 ret = wret;
Chris Mason35b7e472007-05-02 15:53:43 -04001464 btrfs_btree_balance_dirty(root);
Chris Mason20524f02007-03-10 06:35:47 -05001465 }
Chris Mason83e15a22007-03-12 09:03:27 -04001466 for (i = 0; i <= orig_level; i++) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001467 if (path->nodes[i]) {
1468 btrfs_block_release(root, path->nodes[i]);
Chris Mason83e15a22007-03-12 09:03:27 -04001469 }
Chris Mason20524f02007-03-10 06:35:47 -05001470 }
Chris Mason5caf2a02007-04-02 11:20:42 -04001471 btrfs_free_path(path);
Chris Mason9aca1d52007-03-13 11:09:37 -04001472 return ret;
Chris Mason20524f02007-03-10 06:35:47 -05001473}
Chris Mason9078a3e2007-04-26 16:46:15 -04001474
Chris Masonbe744172007-05-06 10:15:01 -04001475static int free_block_group_radix(struct radix_tree_root *radix)
Chris Mason9078a3e2007-04-26 16:46:15 -04001476{
1477 int ret;
1478 struct btrfs_block_group_cache *cache[8];
1479 int i;
1480
1481 while(1) {
Chris Masonbe744172007-05-06 10:15:01 -04001482 ret = radix_tree_gang_lookup(radix, (void **)cache, 0,
Chris Mason9078a3e2007-04-26 16:46:15 -04001483 ARRAY_SIZE(cache));
1484 if (!ret)
1485 break;
1486 for (i = 0; i < ret; i++) {
Chris Masonbe744172007-05-06 10:15:01 -04001487 radix_tree_delete(radix, cache[i]->key.objectid +
Chris Mason9078a3e2007-04-26 16:46:15 -04001488 cache[i]->key.offset - 1);
1489 kfree(cache[i]);
1490 }
1491 }
1492 return 0;
1493}
1494
Chris Masonbe744172007-05-06 10:15:01 -04001495int btrfs_free_block_groups(struct btrfs_fs_info *info)
1496{
1497 int ret;
1498 int ret2;
Chris Masone37c9e62007-05-09 20:13:14 -04001499 unsigned long gang[16];
1500 int i;
Chris Masonbe744172007-05-06 10:15:01 -04001501
1502 ret = free_block_group_radix(&info->block_group_radix);
1503 ret2 = free_block_group_radix(&info->block_group_data_radix);
1504 if (ret)
1505 return ret;
1506 if (ret2)
1507 return ret2;
Chris Masone37c9e62007-05-09 20:13:14 -04001508
1509 while(1) {
1510 ret = find_first_radix_bit(&info->extent_map_radix,
1511 gang, 0, ARRAY_SIZE(gang));
1512 if (!ret)
1513 break;
1514 for (i = 0; i < ret; i++) {
1515 clear_radix_bit(&info->extent_map_radix, gang[i]);
1516 }
1517 }
Chris Masonbe744172007-05-06 10:15:01 -04001518 return 0;
1519}
1520
Chris Mason9078a3e2007-04-26 16:46:15 -04001521int btrfs_read_block_groups(struct btrfs_root *root)
1522{
1523 struct btrfs_path *path;
1524 int ret;
1525 int err = 0;
1526 struct btrfs_block_group_item *bi;
1527 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04001528 struct btrfs_fs_info *info = root->fs_info;
1529 struct radix_tree_root *radix;
Chris Mason9078a3e2007-04-26 16:46:15 -04001530 struct btrfs_key key;
1531 struct btrfs_key found_key;
1532 struct btrfs_leaf *leaf;
1533 u64 group_size_blocks = BTRFS_BLOCK_GROUP_SIZE / root->blocksize;
Chris Mason31f3c992007-04-30 15:25:45 -04001534 u64 used;
Chris Mason9078a3e2007-04-26 16:46:15 -04001535
Chris Masonbe744172007-05-06 10:15:01 -04001536 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04001537 key.objectid = 0;
1538 key.offset = group_size_blocks;
1539 key.flags = 0;
1540 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
1541
1542 path = btrfs_alloc_path();
1543 if (!path)
1544 return -ENOMEM;
1545
1546 while(1) {
Chris Masonbe744172007-05-06 10:15:01 -04001547 ret = btrfs_search_slot(NULL, info->extent_root,
Chris Mason9078a3e2007-04-26 16:46:15 -04001548 &key, path, 0, 0);
1549 if (ret != 0) {
1550 err = ret;
1551 break;
1552 }
1553 leaf = btrfs_buffer_leaf(path->nodes[0]);
1554 btrfs_disk_key_to_cpu(&found_key,
1555 &leaf->items[path->slots[0]].key);
1556 cache = kmalloc(sizeof(*cache), GFP_NOFS);
1557 if (!cache) {
1558 err = -1;
1559 break;
1560 }
Chris Mason3e1ad542007-05-07 20:03:49 -04001561
Chris Mason9078a3e2007-04-26 16:46:15 -04001562 bi = btrfs_item_ptr(leaf, path->slots[0],
1563 struct btrfs_block_group_item);
Chris Mason1e2677e2007-05-29 16:52:18 -04001564 if (bi->flags & BTRFS_BLOCK_GROUP_DATA) {
1565 radix = &info->block_group_data_radix;
1566 cache->data = 1;
1567 } else {
1568 radix = &info->block_group_radix;
1569 cache->data = 0;
1570 }
1571
Chris Mason9078a3e2007-04-26 16:46:15 -04001572 memcpy(&cache->item, bi, sizeof(*bi));
1573 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Mason31f3c992007-04-30 15:25:45 -04001574 cache->last_alloc = cache->key.objectid;
1575 cache->first_free = cache->key.objectid;
Chris Masone37c9e62007-05-09 20:13:14 -04001576 cache->last_prealloc = cache->key.objectid;
Chris Masonbe744172007-05-06 10:15:01 -04001577 cache->pinned = 0;
Chris Masone37c9e62007-05-09 20:13:14 -04001578 cache->cached = 0;
1579
Chris Mason3e1ad542007-05-07 20:03:49 -04001580 cache->radix = radix;
1581
Chris Mason9078a3e2007-04-26 16:46:15 -04001582 key.objectid = found_key.objectid + found_key.offset;
1583 btrfs_release_path(root, path);
Chris Masonbe744172007-05-06 10:15:01 -04001584 ret = radix_tree_insert(radix, found_key.objectid +
Chris Mason9078a3e2007-04-26 16:46:15 -04001585 found_key.offset - 1,
1586 (void *)cache);
1587 BUG_ON(ret);
Chris Mason31f3c992007-04-30 15:25:45 -04001588 used = btrfs_block_group_used(bi);
Chris Masonbe744172007-05-06 10:15:01 -04001589 if (used < (key.offset * 8) / 10) {
1590 radix_tree_tag_set(radix, found_key.objectid +
Chris Mason31f3c992007-04-30 15:25:45 -04001591 found_key.offset - 1,
1592 BTRFS_BLOCK_GROUP_AVAIL);
1593 }
Chris Mason9078a3e2007-04-26 16:46:15 -04001594 if (key.objectid >=
Chris Masonbe744172007-05-06 10:15:01 -04001595 btrfs_super_total_blocks(info->disk_super))
Chris Mason9078a3e2007-04-26 16:46:15 -04001596 break;
1597 }
1598
1599 btrfs_free_path(path);
1600 return 0;
1601}