blob: 26b8d340649128d232ab3ba41e7a2f76a0905a79 [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
Zach Brownec6b9102007-07-11 10:00:37 -040019#include <linux/sched.h>
Chris Masonfec577f2007-02-26 10:40:21 -050020#include "ctree.h"
21#include "disk-io.h"
22#include "print-tree.h"
Chris Masone089f052007-03-16 16:20:31 -040023#include "transaction.h"
Chris Masonfec577f2007-02-26 10:40:21 -050024
Chris Masone089f052007-03-16 16:20:31 -040025static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason6702ed42007-08-07 16:15:09 -040026 *orig_root, u64 num_blocks, u64 empty_size,
27 u64 search_start,
Chris Masonfbdc7622007-05-30 10:22:12 -040028 u64 search_end, u64 hint_block,
Chris Masonf2654de2007-06-26 12:20:46 -040029 struct btrfs_key *ins, u64 exclude_start,
30 u64 exclude_nr, int data);
Chris Masone089f052007-03-16 16:20:31 -040031static int finish_current_insert(struct btrfs_trans_handle *trans, struct
32 btrfs_root *extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -040033static int del_pending_extents(struct btrfs_trans_handle *trans, struct
34 btrfs_root *extent_root);
Chris Masonfec577f2007-02-26 10:40:21 -050035
Chris Masone37c9e62007-05-09 20:13:14 -040036static int cache_block_group(struct btrfs_root *root,
37 struct btrfs_block_group_cache *block_group)
38{
39 struct btrfs_path *path;
40 int ret;
41 struct btrfs_key key;
42 struct btrfs_leaf *leaf;
43 struct radix_tree_root *extent_radix;
44 int slot;
45 u64 i;
46 u64 last = 0;
47 u64 hole_size;
Chris Masonde428b62007-05-18 13:28:27 -040048 u64 limit;
Chris Masone37c9e62007-05-09 20:13:14 -040049 int found = 0;
50
51 root = root->fs_info->extent_root;
52 extent_radix = &root->fs_info->extent_map_radix;
53
54 if (block_group->cached)
55 return 0;
56 if (block_group->data)
57 return 0;
58 path = btrfs_alloc_path();
59 if (!path)
60 return -ENOMEM;
Chris Mason3c69fae2007-08-07 15:52:22 -040061 path->reada = 1;
Chris Masone37c9e62007-05-09 20:13:14 -040062 key.objectid = block_group->key.objectid;
63 key.flags = 0;
64 key.offset = 0;
65 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
66 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
67 if (ret < 0)
68 return ret;
69 if (ret && path->slots[0] > 0)
70 path->slots[0]--;
Chris Masonde428b62007-05-18 13:28:27 -040071 limit = block_group->key.objectid + block_group->key.offset;
Chris Masone37c9e62007-05-09 20:13:14 -040072 while(1) {
73 leaf = btrfs_buffer_leaf(path->nodes[0]);
74 slot = path->slots[0];
75 if (slot >= btrfs_header_nritems(&leaf->header)) {
76 ret = btrfs_next_leaf(root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -040077 if (ret < 0)
78 goto err;
Chris Masonde428b62007-05-18 13:28:27 -040079 if (ret == 0) {
Chris Masone37c9e62007-05-09 20:13:14 -040080 continue;
Chris Masonde428b62007-05-18 13:28:27 -040081 } else {
Chris Masone37c9e62007-05-09 20:13:14 -040082 if (found) {
83 hole_size = block_group->key.objectid +
84 block_group->key.offset - last;
85 } else {
86 last = block_group->key.objectid;
87 hole_size = block_group->key.offset;
88 }
89 for (i = 0; i < hole_size; i++) {
90 set_radix_bit(extent_radix,
91 last + i);
92 }
93 break;
94 }
95 }
96 btrfs_disk_key_to_cpu(&key, &leaf->items[slot].key);
97 if (key.objectid >= block_group->key.objectid +
98 block_group->key.offset) {
99 if (found) {
100 hole_size = block_group->key.objectid +
101 block_group->key.offset - last;
102 } else {
103 last = block_group->key.objectid;
104 hole_size = block_group->key.offset;
105 }
106 for (i = 0; i < hole_size; i++) {
107 set_radix_bit(extent_radix, last + i);
108 }
109 break;
110 }
111 if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) {
112 if (!found) {
113 last = key.objectid + key.offset;
114 found = 1;
115 } else {
116 hole_size = key.objectid - last;
117 for (i = 0; i < hole_size; i++) {
118 set_radix_bit(extent_radix, last + i);
119 }
120 last = key.objectid + key.offset;
121 }
122 }
123 path->slots[0]++;
124 }
125
126 block_group->cached = 1;
Chris Mason54aa1f42007-06-22 14:16:25 -0400127err:
Chris Masone37c9e62007-05-09 20:13:14 -0400128 btrfs_free_path(path);
129 return 0;
130}
131
Chris Mason5276aed2007-06-11 21:33:38 -0400132struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
133 btrfs_fs_info *info,
134 u64 blocknr)
Chris Masonbe744172007-05-06 10:15:01 -0400135{
136 struct btrfs_block_group_cache *block_group;
137 int ret;
138
139 ret = radix_tree_gang_lookup(&info->block_group_radix,
140 (void **)&block_group,
141 blocknr, 1);
142 if (ret) {
Chris Mason3e1ad542007-05-07 20:03:49 -0400143 if (block_group->key.objectid <= blocknr && blocknr <=
Chris Masonbe744172007-05-06 10:15:01 -0400144 block_group->key.objectid + block_group->key.offset)
145 return block_group;
146 }
147 ret = radix_tree_gang_lookup(&info->block_group_data_radix,
148 (void **)&block_group,
149 blocknr, 1);
150 if (ret) {
Chris Mason3e1ad542007-05-07 20:03:49 -0400151 if (block_group->key.objectid <= blocknr && blocknr <=
Chris Masonbe744172007-05-06 10:15:01 -0400152 block_group->key.objectid + block_group->key.offset)
153 return block_group;
154 }
Chris Masonbe744172007-05-06 10:15:01 -0400155 return NULL;
156}
157
Chris Masone37c9e62007-05-09 20:13:14 -0400158static u64 leaf_range(struct btrfs_root *root)
159{
160 u64 size = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason84f54cf2007-06-12 07:43:08 -0400161 do_div(size, sizeof(struct btrfs_extent_item) +
162 sizeof(struct btrfs_item));
Chris Masone37c9e62007-05-09 20:13:14 -0400163 return size;
164}
165
166static u64 find_search_start(struct btrfs_root *root,
167 struct btrfs_block_group_cache **cache_ret,
168 u64 search_start, int num)
169{
170 unsigned long gang[8];
171 int ret;
172 struct btrfs_block_group_cache *cache = *cache_ret;
173 u64 last = max(search_start, cache->key.objectid);
174
175 if (cache->data)
176 goto out;
177 if (num > 1) {
178 last = max(last, cache->last_prealloc);
179 }
180again:
Chris Mason54aa1f42007-06-22 14:16:25 -0400181 ret = cache_block_group(root, cache);
182 if (ret)
183 goto out;
Chris Masone37c9e62007-05-09 20:13:14 -0400184 while(1) {
185 ret = find_first_radix_bit(&root->fs_info->extent_map_radix,
186 gang, last, ARRAY_SIZE(gang));
187 if (!ret)
188 goto out;
189 last = gang[ret-1] + 1;
190 if (num > 1) {
191 if (ret != ARRAY_SIZE(gang)) {
192 goto new_group;
193 }
194 if (gang[ret-1] - gang[0] > leaf_range(root)) {
195 continue;
196 }
197 }
198 if (gang[0] >= cache->key.objectid + cache->key.offset) {
199 goto new_group;
200 }
201 return gang[0];
202 }
203out:
204 return max(cache->last_alloc, search_start);
205
206new_group:
Chris Mason5276aed2007-06-11 21:33:38 -0400207 cache = btrfs_lookup_block_group(root->fs_info,
208 last + cache->key.offset - 1);
Chris Masone37c9e62007-05-09 20:13:14 -0400209 if (!cache) {
210 return max((*cache_ret)->last_alloc, search_start);
211 }
212 cache = btrfs_find_block_group(root, cache,
Chris Masonde428b62007-05-18 13:28:27 -0400213 last + cache->key.offset - 1, 0, 0);
Chris Masone37c9e62007-05-09 20:13:14 -0400214 *cache_ret = cache;
215 goto again;
216}
217
Chris Mason84f54cf2007-06-12 07:43:08 -0400218static u64 div_factor(u64 num, int factor)
219{
220 num *= factor;
221 do_div(num, 10);
222 return num;
223}
224
Chris Mason31f3c992007-04-30 15:25:45 -0400225struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
226 struct btrfs_block_group_cache
Chris Masonbe744172007-05-06 10:15:01 -0400227 *hint, u64 search_start,
Chris Masonde428b62007-05-18 13:28:27 -0400228 int data, int owner)
Chris Masoncd1bc462007-04-27 10:08:34 -0400229{
230 struct btrfs_block_group_cache *cache[8];
Chris Mason31f3c992007-04-30 15:25:45 -0400231 struct btrfs_block_group_cache *found_group = NULL;
Chris Masoncd1bc462007-04-27 10:08:34 -0400232 struct btrfs_fs_info *info = root->fs_info;
Chris Masonbe744172007-05-06 10:15:01 -0400233 struct radix_tree_root *radix;
Chris Mason1e2677e2007-05-29 16:52:18 -0400234 struct radix_tree_root *swap_radix;
Chris Masoncd1bc462007-04-27 10:08:34 -0400235 u64 used;
Chris Mason31f3c992007-04-30 15:25:45 -0400236 u64 last = 0;
237 u64 hint_last;
Chris Masoncd1bc462007-04-27 10:08:34 -0400238 int i;
239 int ret;
Chris Mason31f3c992007-04-30 15:25:45 -0400240 int full_search = 0;
Chris Masonde428b62007-05-18 13:28:27 -0400241 int factor = 8;
Chris Mason1e2677e2007-05-29 16:52:18 -0400242 int data_swap = 0;
Chris Masonde428b62007-05-18 13:28:27 -0400243
244 if (!owner)
245 factor = 5;
Chris Masonbe744172007-05-06 10:15:01 -0400246
Chris Mason1e2677e2007-05-29 16:52:18 -0400247 if (data) {
Chris Masonbe744172007-05-06 10:15:01 -0400248 radix = &info->block_group_data_radix;
Chris Mason1e2677e2007-05-29 16:52:18 -0400249 swap_radix = &info->block_group_radix;
250 } else {
Chris Masonbe744172007-05-06 10:15:01 -0400251 radix = &info->block_group_radix;
Chris Mason1e2677e2007-05-29 16:52:18 -0400252 swap_radix = &info->block_group_data_radix;
253 }
Chris Masonbe744172007-05-06 10:15:01 -0400254
255 if (search_start) {
256 struct btrfs_block_group_cache *shint;
Chris Mason5276aed2007-06-11 21:33:38 -0400257 shint = btrfs_lookup_block_group(info, search_start);
Chris Masonbe744172007-05-06 10:15:01 -0400258 if (shint->data == data) {
259 used = btrfs_block_group_used(&shint->item);
260 if (used + shint->pinned <
Chris Mason84f54cf2007-06-12 07:43:08 -0400261 div_factor(shint->key.offset, factor)) {
Chris Masonbe744172007-05-06 10:15:01 -0400262 return shint;
263 }
264 }
265 }
266 if (hint && hint->data == data) {
Chris Mason31f3c992007-04-30 15:25:45 -0400267 used = btrfs_block_group_used(&hint->item);
Chris Mason84f54cf2007-06-12 07:43:08 -0400268 if (used + hint->pinned <
269 div_factor(hint->key.offset, factor)) {
Chris Mason31f3c992007-04-30 15:25:45 -0400270 return hint;
271 }
Chris Mason84f54cf2007-06-12 07:43:08 -0400272 if (used >= div_factor(hint->key.offset, 8)) {
Chris Masonbe744172007-05-06 10:15:01 -0400273 radix_tree_tag_clear(radix,
274 hint->key.objectid +
275 hint->key.offset - 1,
276 BTRFS_BLOCK_GROUP_AVAIL);
277 }
Chris Mason8d7be552007-05-10 11:24:42 -0400278 last = hint->key.offset * 3;
Chris Masonbe744172007-05-06 10:15:01 -0400279 if (hint->key.objectid >= last)
Chris Masone37c9e62007-05-09 20:13:14 -0400280 last = max(search_start + hint->key.offset - 1,
281 hint->key.objectid - last);
Chris Masonbe744172007-05-06 10:15:01 -0400282 else
283 last = hint->key.objectid + hint->key.offset;
Chris Mason31f3c992007-04-30 15:25:45 -0400284 hint_last = last;
285 } else {
Chris Masone37c9e62007-05-09 20:13:14 -0400286 if (hint)
287 hint_last = max(hint->key.objectid, search_start);
288 else
289 hint_last = search_start;
290
291 last = hint_last;
Chris Mason31f3c992007-04-30 15:25:45 -0400292 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400293 while(1) {
Chris Masonbe744172007-05-06 10:15:01 -0400294 ret = radix_tree_gang_lookup_tag(radix, (void **)cache,
Chris Masoncd1bc462007-04-27 10:08:34 -0400295 last, ARRAY_SIZE(cache),
Chris Mason31f3c992007-04-30 15:25:45 -0400296 BTRFS_BLOCK_GROUP_AVAIL);
Chris Masoncd1bc462007-04-27 10:08:34 -0400297 if (!ret)
298 break;
299 for (i = 0; i < ret; i++) {
Chris Masonbe08c1b2007-05-03 09:06:49 -0400300 last = cache[i]->key.objectid +
301 cache[i]->key.offset;
Chris Masoncd1bc462007-04-27 10:08:34 -0400302 used = btrfs_block_group_used(&cache[i]->item);
Chris Masonbe744172007-05-06 10:15:01 -0400303 if (used + cache[i]->pinned <
Chris Mason84f54cf2007-06-12 07:43:08 -0400304 div_factor(cache[i]->key.offset, factor)) {
Chris Mason31f3c992007-04-30 15:25:45 -0400305 found_group = cache[i];
306 goto found;
Chris Masoncd1bc462007-04-27 10:08:34 -0400307 }
Chris Mason84f54cf2007-06-12 07:43:08 -0400308 if (used >= div_factor(cache[i]->key.offset, 8)) {
Chris Masonbe744172007-05-06 10:15:01 -0400309 radix_tree_tag_clear(radix,
310 cache[i]->key.objectid +
311 cache[i]->key.offset - 1,
312 BTRFS_BLOCK_GROUP_AVAIL);
313 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400314 }
Chris Masonde428b62007-05-18 13:28:27 -0400315 cond_resched();
Chris Masoncd1bc462007-04-27 10:08:34 -0400316 }
Chris Mason31f3c992007-04-30 15:25:45 -0400317 last = hint_last;
318again:
Chris Masoncd1bc462007-04-27 10:08:34 -0400319 while(1) {
Chris Masonbe744172007-05-06 10:15:01 -0400320 ret = radix_tree_gang_lookup(radix, (void **)cache,
321 last, ARRAY_SIZE(cache));
Chris Masoncd1bc462007-04-27 10:08:34 -0400322 if (!ret)
323 break;
324 for (i = 0; i < ret; i++) {
Chris Masonbe08c1b2007-05-03 09:06:49 -0400325 last = cache[i]->key.objectid +
326 cache[i]->key.offset;
Chris Masoncd1bc462007-04-27 10:08:34 -0400327 used = btrfs_block_group_used(&cache[i]->item);
Chris Masonbe744172007-05-06 10:15:01 -0400328 if (used + cache[i]->pinned < cache[i]->key.offset) {
Chris Mason31f3c992007-04-30 15:25:45 -0400329 found_group = cache[i];
330 goto found;
Chris Masoncd1bc462007-04-27 10:08:34 -0400331 }
Chris Masonbe744172007-05-06 10:15:01 -0400332 if (used >= cache[i]->key.offset) {
333 radix_tree_tag_clear(radix,
334 cache[i]->key.objectid +
335 cache[i]->key.offset - 1,
336 BTRFS_BLOCK_GROUP_AVAIL);
337 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400338 }
Chris Masonde428b62007-05-18 13:28:27 -0400339 cond_resched();
Chris Masoncd1bc462007-04-27 10:08:34 -0400340 }
Chris Mason31f3c992007-04-30 15:25:45 -0400341 if (!full_search) {
Chris Masonbe744172007-05-06 10:15:01 -0400342 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -0400343 full_search = 1;
344 goto again;
345 }
Chris Mason1e2677e2007-05-29 16:52:18 -0400346 if (!data_swap) {
347 struct radix_tree_root *tmp = radix;
348 data_swap = 1;
349 radix = swap_radix;
350 swap_radix = tmp;
351 last = search_start;
352 goto again;
353 }
Chris Mason31f3c992007-04-30 15:25:45 -0400354 if (!found_group) {
Chris Masonbe744172007-05-06 10:15:01 -0400355 ret = radix_tree_gang_lookup(radix,
Chris Mason31f3c992007-04-30 15:25:45 -0400356 (void **)&found_group, 0, 1);
Chris Mason1e2677e2007-05-29 16:52:18 -0400357 if (ret == 0) {
358 ret = radix_tree_gang_lookup(swap_radix,
359 (void **)&found_group,
360 0, 1);
361 }
Chris Mason31f3c992007-04-30 15:25:45 -0400362 BUG_ON(ret != 1);
363 }
Chris Masonbe744172007-05-06 10:15:01 -0400364found:
Chris Mason31f3c992007-04-30 15:25:45 -0400365 return found_group;
Chris Masoncd1bc462007-04-27 10:08:34 -0400366}
367
Chris Masonb18c6682007-04-17 13:26:50 -0400368int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
369 struct btrfs_root *root,
370 u64 blocknr, u64 num_blocks)
Chris Mason02217ed2007-03-02 16:08:05 -0500371{
Chris Mason5caf2a02007-04-02 11:20:42 -0400372 struct btrfs_path *path;
Chris Mason02217ed2007-03-02 16:08:05 -0500373 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400374 struct btrfs_key key;
Chris Mason234b63a2007-03-13 10:46:10 -0400375 struct btrfs_leaf *l;
376 struct btrfs_extent_item *item;
Chris Masone2fa7222007-03-12 16:22:34 -0400377 struct btrfs_key ins;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400378 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500379
Chris Mason5caf2a02007-04-02 11:20:42 -0400380 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -0400381 if (!path)
382 return -ENOMEM;
Chris Mason6702ed42007-08-07 16:15:09 -0400383 ret = find_free_extent(trans, root->fs_info->extent_root, 0, 0, 0,
Chris Masonf2654de2007-06-26 12:20:46 -0400384 (u64)-1, 0, &ins, 0, 0, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400385 if (ret) {
386 btrfs_free_path(path);
387 return ret;
388 }
Chris Mason02217ed2007-03-02 16:08:05 -0500389 key.objectid = blocknr;
390 key.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -0400391 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Mason6407bf62007-03-27 06:33:00 -0400392 key.offset = num_blocks;
Chris Mason5caf2a02007-04-02 11:20:42 -0400393 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -0400394 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400395 if (ret < 0)
396 return ret;
Chris Masona429e512007-04-18 16:15:28 -0400397 if (ret != 0) {
Chris Masona28ec192007-03-06 20:08:01 -0500398 BUG();
Chris Masona429e512007-04-18 16:15:28 -0400399 }
Chris Mason02217ed2007-03-02 16:08:05 -0500400 BUG_ON(ret != 0);
Chris Mason5caf2a02007-04-02 11:20:42 -0400401 l = btrfs_buffer_leaf(path->nodes[0]);
402 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400403 refs = btrfs_extent_refs(item);
404 btrfs_set_extent_refs(item, refs + 1);
Chris Mason5caf2a02007-04-02 11:20:42 -0400405 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masona28ec192007-03-06 20:08:01 -0500406
Chris Mason5caf2a02007-04-02 11:20:42 -0400407 btrfs_release_path(root->fs_info->extent_root, path);
408 btrfs_free_path(path);
Chris Mason9f5fae22007-03-20 14:38:32 -0400409 finish_current_insert(trans, root->fs_info->extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -0400410 del_pending_extents(trans, root->fs_info->extent_root);
Chris Mason02217ed2007-03-02 16:08:05 -0500411 return 0;
412}
413
Chris Masonb18c6682007-04-17 13:26:50 -0400414static int lookup_extent_ref(struct btrfs_trans_handle *trans,
415 struct btrfs_root *root, u64 blocknr,
416 u64 num_blocks, u32 *refs)
Chris Masona28ec192007-03-06 20:08:01 -0500417{
Chris Mason5caf2a02007-04-02 11:20:42 -0400418 struct btrfs_path *path;
Chris Masona28ec192007-03-06 20:08:01 -0500419 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400420 struct btrfs_key key;
Chris Mason234b63a2007-03-13 10:46:10 -0400421 struct btrfs_leaf *l;
422 struct btrfs_extent_item *item;
Chris Mason5caf2a02007-04-02 11:20:42 -0400423
424 path = btrfs_alloc_path();
Chris Masona28ec192007-03-06 20:08:01 -0500425 key.objectid = blocknr;
Chris Mason6407bf62007-03-27 06:33:00 -0400426 key.offset = num_blocks;
Chris Mason62e27492007-03-15 12:56:47 -0400427 key.flags = 0;
428 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Mason5caf2a02007-04-02 11:20:42 -0400429 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -0400430 0, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400431 if (ret < 0)
432 goto out;
Chris Masona28ec192007-03-06 20:08:01 -0500433 if (ret != 0)
434 BUG();
Chris Mason5caf2a02007-04-02 11:20:42 -0400435 l = btrfs_buffer_leaf(path->nodes[0]);
436 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400437 *refs = btrfs_extent_refs(item);
Chris Mason54aa1f42007-06-22 14:16:25 -0400438out:
Chris Mason5caf2a02007-04-02 11:20:42 -0400439 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -0500440 return 0;
441}
442
Chris Masonc5739bb2007-04-10 09:27:04 -0400443int btrfs_inc_root_ref(struct btrfs_trans_handle *trans,
444 struct btrfs_root *root)
445{
Chris Masonb18c6682007-04-17 13:26:50 -0400446 return btrfs_inc_extent_ref(trans, root, bh_blocknr(root->node), 1);
Chris Masonc5739bb2007-04-10 09:27:04 -0400447}
448
Chris Masone089f052007-03-16 16:20:31 -0400449int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Masone20d96d2007-03-22 12:13:20 -0400450 struct buffer_head *buf)
Chris Mason02217ed2007-03-02 16:08:05 -0500451{
452 u64 blocknr;
Chris Masone20d96d2007-03-22 12:13:20 -0400453 struct btrfs_node *buf_node;
Chris Mason6407bf62007-03-27 06:33:00 -0400454 struct btrfs_leaf *buf_leaf;
455 struct btrfs_disk_key *key;
456 struct btrfs_file_extent_item *fi;
Chris Mason02217ed2007-03-02 16:08:05 -0500457 int i;
Chris Mason6407bf62007-03-27 06:33:00 -0400458 int leaf;
459 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -0400460 int faili;
461 int err;
Chris Masona28ec192007-03-06 20:08:01 -0500462
Chris Mason3768f362007-03-13 16:47:54 -0400463 if (!root->ref_cows)
Chris Masona28ec192007-03-06 20:08:01 -0500464 return 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400465 buf_node = btrfs_buffer_node(buf);
Chris Mason6407bf62007-03-27 06:33:00 -0400466 leaf = btrfs_is_leaf(buf_node);
467 buf_leaf = btrfs_buffer_leaf(buf);
Chris Masone20d96d2007-03-22 12:13:20 -0400468 for (i = 0; i < btrfs_header_nritems(&buf_node->header); i++) {
Chris Mason6407bf62007-03-27 06:33:00 -0400469 if (leaf) {
Chris Mason3a686372007-05-24 13:35:57 -0400470 u64 disk_blocknr;
Chris Mason6407bf62007-03-27 06:33:00 -0400471 key = &buf_leaf->items[i].key;
472 if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY)
473 continue;
474 fi = btrfs_item_ptr(buf_leaf, i,
475 struct btrfs_file_extent_item);
Chris Mason236454d2007-04-19 13:37:44 -0400476 if (btrfs_file_extent_type(fi) ==
477 BTRFS_FILE_EXTENT_INLINE)
478 continue;
Chris Mason3a686372007-05-24 13:35:57 -0400479 disk_blocknr = btrfs_file_extent_disk_blocknr(fi);
480 if (disk_blocknr == 0)
481 continue;
482 ret = btrfs_inc_extent_ref(trans, root, disk_blocknr,
Chris Mason6407bf62007-03-27 06:33:00 -0400483 btrfs_file_extent_disk_num_blocks(fi));
Chris Mason54aa1f42007-06-22 14:16:25 -0400484 if (ret) {
485 faili = i;
486 goto fail;
487 }
Chris Mason6407bf62007-03-27 06:33:00 -0400488 } else {
489 blocknr = btrfs_node_blockptr(buf_node, i);
Chris Masonb18c6682007-04-17 13:26:50 -0400490 ret = btrfs_inc_extent_ref(trans, root, blocknr, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400491 if (ret) {
492 faili = i;
493 goto fail;
494 }
Chris Mason6407bf62007-03-27 06:33:00 -0400495 }
Chris Mason02217ed2007-03-02 16:08:05 -0500496 }
497 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -0400498fail:
Chris Masonccd467d2007-06-28 15:57:36 -0400499 WARN_ON(1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400500 for (i =0; i < faili; i++) {
501 if (leaf) {
502 u64 disk_blocknr;
503 key = &buf_leaf->items[i].key;
504 if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY)
505 continue;
506 fi = btrfs_item_ptr(buf_leaf, i,
507 struct btrfs_file_extent_item);
508 if (btrfs_file_extent_type(fi) ==
509 BTRFS_FILE_EXTENT_INLINE)
510 continue;
511 disk_blocknr = btrfs_file_extent_disk_blocknr(fi);
512 if (disk_blocknr == 0)
513 continue;
514 err = btrfs_free_extent(trans, root, disk_blocknr,
515 btrfs_file_extent_disk_num_blocks(fi), 0);
516 BUG_ON(err);
517 } else {
518 blocknr = btrfs_node_blockptr(buf_node, i);
519 err = btrfs_free_extent(trans, root, blocknr, 1, 0);
520 BUG_ON(err);
521 }
522 }
523 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -0500524}
525
Chris Mason9078a3e2007-04-26 16:46:15 -0400526static int write_one_cache_group(struct btrfs_trans_handle *trans,
527 struct btrfs_root *root,
528 struct btrfs_path *path,
529 struct btrfs_block_group_cache *cache)
530{
531 int ret;
532 int pending_ret;
533 struct btrfs_root *extent_root = root->fs_info->extent_root;
534 struct btrfs_block_group_item *bi;
535 struct btrfs_key ins;
536
Chris Mason6702ed42007-08-07 16:15:09 -0400537 ret = find_free_extent(trans, extent_root, 0, 0, 0, (u64)-1, 0, &ins,
Chris Masonf2654de2007-06-26 12:20:46 -0400538 0, 0, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400539 /* FIXME, set bit to recalc cache groups on next mount */
540 if (ret)
541 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -0400542 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400543 if (ret < 0)
544 goto fail;
Chris Mason9078a3e2007-04-26 16:46:15 -0400545 BUG_ON(ret);
546 bi = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
547 struct btrfs_block_group_item);
548 memcpy(bi, &cache->item, sizeof(*bi));
Chris Masonccd467d2007-06-28 15:57:36 -0400549 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason9078a3e2007-04-26 16:46:15 -0400550 btrfs_release_path(extent_root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -0400551fail:
Chris Mason9078a3e2007-04-26 16:46:15 -0400552 finish_current_insert(trans, extent_root);
553 pending_ret = del_pending_extents(trans, extent_root);
554 if (ret)
555 return ret;
556 if (pending_ret)
557 return pending_ret;
Chris Masonbe744172007-05-06 10:15:01 -0400558 if (cache->data)
559 cache->last_alloc = cache->first_free;
Chris Mason9078a3e2007-04-26 16:46:15 -0400560 return 0;
561
562}
563
Chris Masonbe744172007-05-06 10:15:01 -0400564static int write_dirty_block_radix(struct btrfs_trans_handle *trans,
565 struct btrfs_root *root,
566 struct radix_tree_root *radix)
Chris Mason9078a3e2007-04-26 16:46:15 -0400567{
568 struct btrfs_block_group_cache *cache[8];
569 int ret;
570 int err = 0;
571 int werr = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -0400572 int i;
573 struct btrfs_path *path;
Chris Mason54aa1f42007-06-22 14:16:25 -0400574 unsigned long off = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -0400575
576 path = btrfs_alloc_path();
577 if (!path)
578 return -ENOMEM;
579
580 while(1) {
581 ret = radix_tree_gang_lookup_tag(radix, (void **)cache,
Chris Mason54aa1f42007-06-22 14:16:25 -0400582 off, ARRAY_SIZE(cache),
Chris Mason9078a3e2007-04-26 16:46:15 -0400583 BTRFS_BLOCK_GROUP_DIRTY);
584 if (!ret)
585 break;
586 for (i = 0; i < ret; i++) {
Chris Mason54aa1f42007-06-22 14:16:25 -0400587 err = write_one_cache_group(trans, root,
588 path, cache[i]);
589 /*
590 * if we fail to write the cache group, we want
591 * to keep it marked dirty in hopes that a later
592 * write will work
593 */
594 if (err) {
595 werr = err;
596 off = cache[i]->key.objectid +
597 cache[i]->key.offset;
598 continue;
599 }
600
Chris Mason9078a3e2007-04-26 16:46:15 -0400601 radix_tree_tag_clear(radix, cache[i]->key.objectid +
602 cache[i]->key.offset - 1,
603 BTRFS_BLOCK_GROUP_DIRTY);
Chris Mason9078a3e2007-04-26 16:46:15 -0400604 }
605 }
606 btrfs_free_path(path);
607 return werr;
608}
609
Chris Masonbe744172007-05-06 10:15:01 -0400610int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
611 struct btrfs_root *root)
612{
613 int ret;
614 int ret2;
615 ret = write_dirty_block_radix(trans, root,
616 &root->fs_info->block_group_radix);
617 ret2 = write_dirty_block_radix(trans, root,
618 &root->fs_info->block_group_data_radix);
619 if (ret)
620 return ret;
621 if (ret2)
622 return ret2;
623 return 0;
624}
625
Chris Mason9078a3e2007-04-26 16:46:15 -0400626static int update_block_group(struct btrfs_trans_handle *trans,
627 struct btrfs_root *root,
Chris Mason1e2677e2007-05-29 16:52:18 -0400628 u64 blocknr, u64 num, int alloc, int mark_free,
629 int data)
Chris Mason9078a3e2007-04-26 16:46:15 -0400630{
631 struct btrfs_block_group_cache *cache;
632 struct btrfs_fs_info *info = root->fs_info;
633 u64 total = num;
634 u64 old_val;
635 u64 block_in_group;
Chris Masone37c9e62007-05-09 20:13:14 -0400636 u64 i;
Chris Mason1e2677e2007-05-29 16:52:18 -0400637 int ret;
Chris Mason3e1ad542007-05-07 20:03:49 -0400638
Chris Mason9078a3e2007-04-26 16:46:15 -0400639 while(total) {
Chris Mason5276aed2007-06-11 21:33:38 -0400640 cache = btrfs_lookup_block_group(info, blocknr);
Chris Mason3e1ad542007-05-07 20:03:49 -0400641 if (!cache) {
Chris Mason9078a3e2007-04-26 16:46:15 -0400642 return -1;
Chris Masoncd1bc462007-04-27 10:08:34 -0400643 }
Chris Mason9078a3e2007-04-26 16:46:15 -0400644 block_in_group = blocknr - cache->key.objectid;
645 WARN_ON(block_in_group > cache->key.offset);
Chris Mason3e1ad542007-05-07 20:03:49 -0400646 radix_tree_tag_set(cache->radix, cache->key.objectid +
Chris Masonbe744172007-05-06 10:15:01 -0400647 cache->key.offset - 1,
Chris Mason9078a3e2007-04-26 16:46:15 -0400648 BTRFS_BLOCK_GROUP_DIRTY);
649
650 old_val = btrfs_block_group_used(&cache->item);
651 num = min(total, cache->key.offset - block_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -0400652 if (alloc) {
Chris Masoncd1bc462007-04-27 10:08:34 -0400653 if (blocknr > cache->last_alloc)
654 cache->last_alloc = blocknr;
Chris Masone37c9e62007-05-09 20:13:14 -0400655 if (!cache->data) {
656 for (i = 0; i < num; i++) {
657 clear_radix_bit(&info->extent_map_radix,
658 blocknr + i);
659 }
660 }
Chris Mason1e2677e2007-05-29 16:52:18 -0400661 if (cache->data != data &&
Chris Mason84f54cf2007-06-12 07:43:08 -0400662 old_val < (cache->key.offset >> 1)) {
Chris Mason1e2677e2007-05-29 16:52:18 -0400663 cache->data = data;
664 radix_tree_delete(cache->radix,
665 cache->key.objectid +
666 cache->key.offset - 1);
667
668 if (data) {
669 cache->radix =
670 &info->block_group_data_radix;
671 cache->item.flags |=
672 BTRFS_BLOCK_GROUP_DATA;
673 } else {
674 cache->radix = &info->block_group_radix;
675 cache->item.flags &=
676 ~BTRFS_BLOCK_GROUP_DATA;
677 }
678 ret = radix_tree_insert(cache->radix,
679 cache->key.objectid +
680 cache->key.offset - 1,
681 (void *)cache);
682 }
683 old_val += num;
Chris Masoncd1bc462007-04-27 10:08:34 -0400684 } else {
Chris Mason9078a3e2007-04-26 16:46:15 -0400685 old_val -= num;
Chris Masoncd1bc462007-04-27 10:08:34 -0400686 if (blocknr < cache->first_free)
687 cache->first_free = blocknr;
Chris Masone37c9e62007-05-09 20:13:14 -0400688 if (!cache->data && mark_free) {
689 for (i = 0; i < num; i++) {
690 set_radix_bit(&info->extent_map_radix,
691 blocknr + i);
692 }
693 }
Chris Mason84f54cf2007-06-12 07:43:08 -0400694 if (old_val < (cache->key.offset >> 1) &&
695 old_val + num >= (cache->key.offset >> 1)) {
Chris Masone37c9e62007-05-09 20:13:14 -0400696 radix_tree_tag_set(cache->radix,
697 cache->key.objectid +
698 cache->key.offset - 1,
699 BTRFS_BLOCK_GROUP_AVAIL);
700 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400701 }
Chris Mason9078a3e2007-04-26 16:46:15 -0400702 btrfs_set_block_group_used(&cache->item, old_val);
Chris Masone37c9e62007-05-09 20:13:14 -0400703 total -= num;
704 blocknr += num;
Chris Mason9078a3e2007-04-26 16:46:15 -0400705 }
706 return 0;
707}
708
Chris Masonbe08c1b2007-05-03 09:06:49 -0400709static int try_remove_page(struct address_space *mapping, unsigned long index)
710{
711 int ret;
Chris Mason6702ed42007-08-07 16:15:09 -0400712 return 0;
Chris Masonbe08c1b2007-05-03 09:06:49 -0400713 ret = invalidate_mapping_pages(mapping, index, index);
714 return ret;
715}
716
Chris Masonccd467d2007-06-28 15:57:36 -0400717int btrfs_copy_pinned(struct btrfs_root *root, struct radix_tree_root *copy)
718{
719 unsigned long gang[8];
720 u64 last = 0;
721 struct radix_tree_root *pinned_radix = &root->fs_info->pinned_radix;
722 int ret;
723 int i;
724
725 while(1) {
726 ret = find_first_radix_bit(pinned_radix, gang, last,
727 ARRAY_SIZE(gang));
728 if (!ret)
729 break;
730 for (i = 0 ; i < ret; i++) {
731 set_radix_bit(copy, gang[i]);
732 last = gang[i] + 1;
733 }
734 }
735 return 0;
736}
737
738int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
739 struct btrfs_root *root,
740 struct radix_tree_root *unpin_radix)
Chris Masona28ec192007-03-06 20:08:01 -0500741{
Chris Mason8ef97622007-03-26 10:15:30 -0400742 unsigned long gang[8];
Chris Masonbe08c1b2007-05-03 09:06:49 -0400743 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Masonbe744172007-05-06 10:15:01 -0400744 struct btrfs_block_group_cache *block_group;
Chris Mason88fd1462007-03-16 08:56:18 -0400745 u64 first = 0;
Chris Masona28ec192007-03-06 20:08:01 -0500746 int ret;
747 int i;
Chris Mason8ef97622007-03-26 10:15:30 -0400748 struct radix_tree_root *pinned_radix = &root->fs_info->pinned_radix;
Chris Masone37c9e62007-05-09 20:13:14 -0400749 struct radix_tree_root *extent_radix = &root->fs_info->extent_map_radix;
Chris Masona28ec192007-03-06 20:08:01 -0500750
751 while(1) {
Chris Masonccd467d2007-06-28 15:57:36 -0400752 ret = find_first_radix_bit(unpin_radix, gang, 0,
Chris Mason8ef97622007-03-26 10:15:30 -0400753 ARRAY_SIZE(gang));
Chris Masona28ec192007-03-06 20:08:01 -0500754 if (!ret)
755 break;
Chris Mason88fd1462007-03-16 08:56:18 -0400756 if (!first)
Chris Mason8ef97622007-03-26 10:15:30 -0400757 first = gang[0];
Chris Mason0579da42007-03-07 16:15:30 -0500758 for (i = 0; i < ret; i++) {
Chris Mason8ef97622007-03-26 10:15:30 -0400759 clear_radix_bit(pinned_radix, gang[i]);
Chris Masonccd467d2007-06-28 15:57:36 -0400760 clear_radix_bit(unpin_radix, gang[i]);
Chris Mason5276aed2007-06-11 21:33:38 -0400761 block_group = btrfs_lookup_block_group(root->fs_info,
762 gang[i]);
Chris Masonbe744172007-05-06 10:15:01 -0400763 if (block_group) {
764 WARN_ON(block_group->pinned == 0);
765 block_group->pinned--;
766 if (gang[i] < block_group->last_alloc)
767 block_group->last_alloc = gang[i];
Chris Masone37c9e62007-05-09 20:13:14 -0400768 if (gang[i] < block_group->last_prealloc)
769 block_group->last_prealloc = gang[i];
770 if (!block_group->data)
771 set_radix_bit(extent_radix, gang[i]);
Chris Masonbe744172007-05-06 10:15:01 -0400772 }
Chris Masonbe08c1b2007-05-03 09:06:49 -0400773 try_remove_page(btree_inode->i_mapping,
774 gang[i] << (PAGE_CACHE_SHIFT -
775 btree_inode->i_blkbits));
Chris Mason0579da42007-03-07 16:15:30 -0500776 }
Chris Masona28ec192007-03-06 20:08:01 -0500777 }
778 return 0;
779}
780
Chris Masone089f052007-03-16 16:20:31 -0400781static int finish_current_insert(struct btrfs_trans_handle *trans, struct
782 btrfs_root *extent_root)
Chris Mason037e6392007-03-07 11:50:24 -0500783{
Chris Masone2fa7222007-03-12 16:22:34 -0400784 struct btrfs_key ins;
Chris Mason234b63a2007-03-13 10:46:10 -0400785 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -0500786 int i;
787 int ret;
Chris Mason1261ec42007-03-20 20:35:03 -0400788 u64 super_blocks_used;
789 struct btrfs_fs_info *info = extent_root->fs_info;
Chris Mason037e6392007-03-07 11:50:24 -0500790
Chris Masoncf27e1e2007-03-13 09:49:06 -0400791 btrfs_set_extent_refs(&extent_item, 1);
Chris Mason037e6392007-03-07 11:50:24 -0500792 ins.offset = 1;
793 ins.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -0400794 btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
Chris Mason5d0c3e62007-04-23 17:01:05 -0400795 btrfs_set_extent_owner(&extent_item, extent_root->root_key.objectid);
Chris Mason037e6392007-03-07 11:50:24 -0500796
Chris Masonf2458e12007-04-25 15:52:25 -0400797 for (i = 0; i < extent_root->fs_info->extent_tree_insert_nr; i++) {
798 ins.objectid = extent_root->fs_info->extent_tree_insert[i];
Chris Mason4b52dff2007-06-26 10:06:50 -0400799 super_blocks_used = btrfs_super_blocks_used(&info->super_copy);
800 btrfs_set_super_blocks_used(&info->super_copy,
Chris Mason1261ec42007-03-20 20:35:03 -0400801 super_blocks_used + 1);
Chris Masone089f052007-03-16 16:20:31 -0400802 ret = btrfs_insert_item(trans, extent_root, &ins, &extent_item,
803 sizeof(extent_item));
Chris Mason037e6392007-03-07 11:50:24 -0500804 BUG_ON(ret);
805 }
Chris Masonf2458e12007-04-25 15:52:25 -0400806 extent_root->fs_info->extent_tree_insert_nr = 0;
Chris Mason037e6392007-03-07 11:50:24 -0500807 return 0;
808}
809
Chris Mason8ef97622007-03-26 10:15:30 -0400810static int pin_down_block(struct btrfs_root *root, u64 blocknr, int pending)
Chris Masone20d96d2007-03-22 12:13:20 -0400811{
812 int err;
Chris Mason78fae272007-03-25 11:35:08 -0400813 struct btrfs_header *header;
Chris Mason8ef97622007-03-26 10:15:30 -0400814 struct buffer_head *bh;
Chris Mason78fae272007-03-25 11:35:08 -0400815
Chris Masonf4b9aa82007-03-27 11:05:53 -0400816 if (!pending) {
Chris Masond98237b2007-03-28 13:57:48 -0400817 bh = btrfs_find_tree_block(root, blocknr);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400818 if (bh) {
819 if (buffer_uptodate(bh)) {
820 u64 transid =
821 root->fs_info->running_transaction->transid;
822 header = btrfs_buffer_header(bh);
823 if (btrfs_header_generation(header) ==
824 transid) {
825 btrfs_block_release(root, bh);
826 return 0;
827 }
Chris Masonf4b9aa82007-03-27 11:05:53 -0400828 }
Chris Masond6025572007-03-30 14:27:56 -0400829 btrfs_block_release(root, bh);
Chris Mason8ef97622007-03-26 10:15:30 -0400830 }
Chris Mason8ef97622007-03-26 10:15:30 -0400831 err = set_radix_bit(&root->fs_info->pinned_radix, blocknr);
Chris Masonbe744172007-05-06 10:15:01 -0400832 if (!err) {
833 struct btrfs_block_group_cache *cache;
Chris Mason5276aed2007-06-11 21:33:38 -0400834 cache = btrfs_lookup_block_group(root->fs_info,
835 blocknr);
Chris Masonbe744172007-05-06 10:15:01 -0400836 if (cache)
837 cache->pinned++;
838 }
Chris Masonf4b9aa82007-03-27 11:05:53 -0400839 } else {
840 err = set_radix_bit(&root->fs_info->pending_del_radix, blocknr);
841 }
Chris Masonbe744172007-05-06 10:15:01 -0400842 BUG_ON(err < 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400843 return 0;
844}
845
Chris Masona28ec192007-03-06 20:08:01 -0500846/*
847 * remove an extent from the root, returns 0 on success
848 */
Chris Masone089f052007-03-16 16:20:31 -0400849static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masone37c9e62007-05-09 20:13:14 -0400850 *root, u64 blocknr, u64 num_blocks, int pin,
851 int mark_free)
Chris Masona28ec192007-03-06 20:08:01 -0500852{
Chris Mason5caf2a02007-04-02 11:20:42 -0400853 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -0400854 struct btrfs_key key;
Chris Mason1261ec42007-03-20 20:35:03 -0400855 struct btrfs_fs_info *info = root->fs_info;
856 struct btrfs_root *extent_root = info->extent_root;
Chris Masona28ec192007-03-06 20:08:01 -0500857 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400858 struct btrfs_extent_item *ei;
Chris Masone2fa7222007-03-12 16:22:34 -0400859 struct btrfs_key ins;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400860 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500861
Chris Masona28ec192007-03-06 20:08:01 -0500862 key.objectid = blocknr;
863 key.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -0400864 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masona28ec192007-03-06 20:08:01 -0500865 key.offset = num_blocks;
866
Chris Mason5caf2a02007-04-02 11:20:42 -0400867 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -0400868 if (!path)
869 return -ENOMEM;
870
Chris Mason6702ed42007-08-07 16:15:09 -0400871 ret = find_free_extent(trans, root, 0, 0, 0, (u64)-1, 0, &ins, 0, 0, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400872 if (ret) {
873 btrfs_free_path(path);
874 return ret;
875 }
Chris Mason5f26f772007-04-05 10:38:44 -0400876
Chris Mason5caf2a02007-04-02 11:20:42 -0400877 ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400878 if (ret < 0)
879 return ret;
880 BUG_ON(ret);
Chris Mason5caf2a02007-04-02 11:20:42 -0400881 ei = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
Chris Mason123abc82007-03-14 14:14:43 -0400882 struct btrfs_extent_item);
Chris Masona28ec192007-03-06 20:08:01 -0500883 BUG_ON(ei->refs == 0);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400884 refs = btrfs_extent_refs(ei) - 1;
885 btrfs_set_extent_refs(ei, refs);
Chris Mason5caf2a02007-04-02 11:20:42 -0400886 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400887 if (refs == 0) {
Chris Mason1261ec42007-03-20 20:35:03 -0400888 u64 super_blocks_used;
Chris Mason78fae272007-03-25 11:35:08 -0400889
890 if (pin) {
Chris Mason8ef97622007-03-26 10:15:30 -0400891 ret = pin_down_block(root, blocknr, 0);
Chris Mason78fae272007-03-25 11:35:08 -0400892 BUG_ON(ret);
893 }
894
Chris Mason4b52dff2007-06-26 10:06:50 -0400895 super_blocks_used = btrfs_super_blocks_used(&info->super_copy);
896 btrfs_set_super_blocks_used(&info->super_copy,
Chris Mason1261ec42007-03-20 20:35:03 -0400897 super_blocks_used - num_blocks);
Chris Mason5caf2a02007-04-02 11:20:42 -0400898 ret = btrfs_del_item(trans, extent_root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -0400899 if (ret) {
900 return ret;
901 }
Chris Masone37c9e62007-05-09 20:13:14 -0400902 ret = update_block_group(trans, root, blocknr, num_blocks, 0,
Chris Mason1e2677e2007-05-29 16:52:18 -0400903 mark_free, 0);
Chris Mason9078a3e2007-04-26 16:46:15 -0400904 BUG_ON(ret);
Chris Masona28ec192007-03-06 20:08:01 -0500905 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400906 btrfs_free_path(path);
Chris Masone089f052007-03-16 16:20:31 -0400907 finish_current_insert(trans, extent_root);
Chris Masona28ec192007-03-06 20:08:01 -0500908 return ret;
909}
910
911/*
Chris Masonfec577f2007-02-26 10:40:21 -0500912 * find all the blocks marked as pending in the radix tree and remove
913 * them from the extent map
914 */
Chris Masone089f052007-03-16 16:20:31 -0400915static int del_pending_extents(struct btrfs_trans_handle *trans, struct
916 btrfs_root *extent_root)
Chris Masonfec577f2007-02-26 10:40:21 -0500917{
918 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -0400919 int wret;
920 int err = 0;
Chris Mason8ef97622007-03-26 10:15:30 -0400921 unsigned long gang[4];
Chris Masonfec577f2007-02-26 10:40:21 -0500922 int i;
Chris Mason8ef97622007-03-26 10:15:30 -0400923 struct radix_tree_root *pending_radix;
924 struct radix_tree_root *pinned_radix;
Chris Masonbe744172007-05-06 10:15:01 -0400925 struct btrfs_block_group_cache *cache;
Chris Mason8ef97622007-03-26 10:15:30 -0400926
927 pending_radix = &extent_root->fs_info->pending_del_radix;
928 pinned_radix = &extent_root->fs_info->pinned_radix;
Chris Masonfec577f2007-02-26 10:40:21 -0500929
930 while(1) {
Chris Masone37c9e62007-05-09 20:13:14 -0400931 ret = find_first_radix_bit(pending_radix, gang, 0,
Chris Mason8ef97622007-03-26 10:15:30 -0400932 ARRAY_SIZE(gang));
Chris Masonfec577f2007-02-26 10:40:21 -0500933 if (!ret)
934 break;
935 for (i = 0; i < ret; i++) {
Chris Mason8ef97622007-03-26 10:15:30 -0400936 wret = set_radix_bit(pinned_radix, gang[i]);
Chris Masonbe744172007-05-06 10:15:01 -0400937 if (wret == 0) {
Chris Mason5276aed2007-06-11 21:33:38 -0400938 cache =
939 btrfs_lookup_block_group(extent_root->fs_info,
Chris Masonbe744172007-05-06 10:15:01 -0400940 gang[i]);
941 if (cache)
942 cache->pinned++;
943 }
944 if (wret < 0) {
945 printk(KERN_CRIT "set_radix_bit, err %d\n",
946 wret);
947 BUG_ON(wret < 0);
948 }
Chris Mason8ef97622007-03-26 10:15:30 -0400949 wret = clear_radix_bit(pending_radix, gang[i]);
950 BUG_ON(wret);
Chris Masond5719762007-03-23 10:01:08 -0400951 wret = __free_extent(trans, extent_root,
Chris Masone37c9e62007-05-09 20:13:14 -0400952 gang[i], 1, 0, 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400953 if (wret)
954 err = wret;
Chris Masonfec577f2007-02-26 10:40:21 -0500955 }
956 }
Chris Masone20d96d2007-03-22 12:13:20 -0400957 return err;
Chris Masonfec577f2007-02-26 10:40:21 -0500958}
959
960/*
961 * remove an extent from the root, returns 0 on success
962 */
Chris Masone089f052007-03-16 16:20:31 -0400963int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
964 *root, u64 blocknr, u64 num_blocks, int pin)
Chris Masonfec577f2007-02-26 10:40:21 -0500965{
Chris Mason9f5fae22007-03-20 14:38:32 -0400966 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Masonfec577f2007-02-26 10:40:21 -0500967 int pending_ret;
968 int ret;
Chris Masona28ec192007-03-06 20:08:01 -0500969
970 if (root == extent_root) {
Chris Mason8ef97622007-03-26 10:15:30 -0400971 pin_down_block(root, blocknr, 1);
Chris Masona28ec192007-03-06 20:08:01 -0500972 return 0;
973 }
Chris Masone37c9e62007-05-09 20:13:14 -0400974 ret = __free_extent(trans, root, blocknr, num_blocks, pin, pin == 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400975 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
Chris Masonfec577f2007-02-26 10:40:21 -0500976 return ret ? ret : pending_ret;
977}
978
979/*
980 * walks the btree of allocated extents and find a hole of a given size.
981 * The key ins is changed to record the hole:
982 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -0400983 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -0500984 * ins->offset == number of blocks
985 * Any available blocks before search_start are skipped.
986 */
Chris Masone089f052007-03-16 16:20:31 -0400987static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason6702ed42007-08-07 16:15:09 -0400988 *orig_root, u64 num_blocks, u64 empty_size,
989 u64 search_start, u64 search_end, u64 hint_block,
Chris Masonf2654de2007-06-26 12:20:46 -0400990 struct btrfs_key *ins, u64 exclude_start,
991 u64 exclude_nr, int data)
Chris Masonfec577f2007-02-26 10:40:21 -0500992{
Chris Mason5caf2a02007-04-02 11:20:42 -0400993 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -0400994 struct btrfs_key key;
Chris Masonfec577f2007-02-26 10:40:21 -0500995 int ret;
996 u64 hole_size = 0;
997 int slot = 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400998 u64 last_block = 0;
Chris Mason037e6392007-03-07 11:50:24 -0500999 u64 test_block;
Chris Masonbe744172007-05-06 10:15:01 -04001000 u64 orig_search_start = search_start;
Chris Masonfec577f2007-02-26 10:40:21 -05001001 int start_found;
Chris Mason234b63a2007-03-13 10:46:10 -04001002 struct btrfs_leaf *l;
Chris Mason9f5fae22007-03-20 14:38:32 -04001003 struct btrfs_root * root = orig_root->fs_info->extent_root;
Chris Masonf2458e12007-04-25 15:52:25 -04001004 struct btrfs_fs_info *info = root->fs_info;
Chris Mason0579da42007-03-07 16:15:30 -05001005 int total_needed = num_blocks;
Chris Masonf2458e12007-04-25 15:52:25 -04001006 int total_found = 0;
1007 int fill_prealloc = 0;
Chris Masone20d96d2007-03-22 12:13:20 -04001008 int level;
Chris Masonbe08c1b2007-05-03 09:06:49 -04001009 struct btrfs_block_group_cache *block_group;
Chris Masonbe744172007-05-06 10:15:01 -04001010 int full_scan = 0;
Chris Masonfbdc7622007-05-30 10:22:12 -04001011 int wrapped = 0;
Chris Masonde428b62007-05-18 13:28:27 -04001012 u64 limit;
Chris Masonfec577f2007-02-26 10:40:21 -05001013
Chris Masonb1a4d962007-04-04 15:27:52 -04001014 ins->flags = 0;
1015 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
1016
Chris Masone20d96d2007-03-22 12:13:20 -04001017 level = btrfs_header_level(btrfs_buffer_header(root->node));
Chris Masonf2458e12007-04-25 15:52:25 -04001018 if (num_blocks == 0) {
1019 fill_prealloc = 1;
1020 num_blocks = 1;
Chris Mason308535a2007-04-28 15:17:08 -04001021 total_needed = (min(level + 1, BTRFS_MAX_LEVEL) + 2) * 3;
Chris Masonf2458e12007-04-25 15:52:25 -04001022 }
Chris Mason85e55b12007-06-19 15:50:51 -04001023 if (fill_prealloc) {
1024 u64 first;
1025 int nr = info->extent_tree_prealloc_nr;
1026 first = info->extent_tree_prealloc[nr - 1];
1027 if (info->extent_tree_prealloc_nr >= total_needed &&
1028 first >= search_start) {
1029 ins->objectid = info->extent_tree_prealloc[0];
1030 ins->offset = 1;
1031 return 0;
1032 }
1033 info->extent_tree_prealloc_nr = 0;
1034 }
Chris Mason3e1ad542007-05-07 20:03:49 -04001035 if (search_end == (u64)-1)
Chris Mason4b52dff2007-06-26 10:06:50 -04001036 search_end = btrfs_super_total_blocks(&info->super_copy);
Chris Masonfbdc7622007-05-30 10:22:12 -04001037 if (hint_block) {
Chris Mason5276aed2007-06-11 21:33:38 -04001038 block_group = btrfs_lookup_block_group(info, hint_block);
Chris Masonbe744172007-05-06 10:15:01 -04001039 block_group = btrfs_find_block_group(root, block_group,
Chris Masonfbdc7622007-05-30 10:22:12 -04001040 hint_block, data, 1);
Chris Masonbe744172007-05-06 10:15:01 -04001041 } else {
1042 block_group = btrfs_find_block_group(root,
1043 trans->block_group, 0,
Chris Masonde428b62007-05-18 13:28:27 -04001044 data, 1);
Chris Masonbe744172007-05-06 10:15:01 -04001045 }
1046
Chris Mason6702ed42007-08-07 16:15:09 -04001047 total_needed += empty_size;
Chris Masone0115992007-06-19 16:23:05 -04001048 path = btrfs_alloc_path();
1049
Chris Masonbe744172007-05-06 10:15:01 -04001050check_failed:
Chris Mason1e2677e2007-05-29 16:52:18 -04001051 if (!block_group->data)
Chris Masone37c9e62007-05-09 20:13:14 -04001052 search_start = find_search_start(root, &block_group,
1053 search_start, total_needed);
Chris Masonfbdc7622007-05-30 10:22:12 -04001054 else if (!full_scan)
Chris Masone37c9e62007-05-09 20:13:14 -04001055 search_start = max(block_group->last_alloc, search_start);
1056
Chris Mason5caf2a02007-04-02 11:20:42 -04001057 btrfs_init_path(path);
Chris Masonfec577f2007-02-26 10:40:21 -05001058 ins->objectid = search_start;
1059 ins->offset = 0;
Chris Masonfec577f2007-02-26 10:40:21 -05001060 start_found = 0;
Chris Masone37c9e62007-05-09 20:13:14 -04001061
Chris Mason5caf2a02007-04-02 11:20:42 -04001062 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
Chris Mason0f70abe2007-02-28 16:46:22 -05001063 if (ret < 0)
1064 goto error;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001065
Chris Masone37c9e62007-05-09 20:13:14 -04001066 if (path->slots[0] > 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001067 path->slots[0]--;
Chris Masone37c9e62007-05-09 20:13:14 -04001068 }
1069
1070 l = btrfs_buffer_leaf(path->nodes[0]);
1071 btrfs_disk_key_to_cpu(&key, &l->items[path->slots[0]].key);
1072 /*
1073 * a rare case, go back one key if we hit a block group item
1074 * instead of an extent item
1075 */
1076 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY &&
1077 key.objectid + key.offset >= search_start) {
1078 ins->objectid = key.objectid;
1079 ins->offset = key.offset - 1;
1080 btrfs_release_path(root, path);
1081 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
1082 if (ret < 0)
1083 goto error;
1084
1085 if (path->slots[0] > 0) {
1086 path->slots[0]--;
1087 }
1088 }
Chris Mason0579da42007-03-07 16:15:30 -05001089
Chris Masonfec577f2007-02-26 10:40:21 -05001090 while (1) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001091 l = btrfs_buffer_leaf(path->nodes[0]);
1092 slot = path->slots[0];
Chris Mason7518a232007-03-12 12:01:18 -04001093 if (slot >= btrfs_header_nritems(&l->header)) {
Chris Masonf2458e12007-04-25 15:52:25 -04001094 if (fill_prealloc) {
1095 info->extent_tree_prealloc_nr = 0;
1096 total_found = 0;
1097 }
Chris Masonde428b62007-05-18 13:28:27 -04001098 if (start_found)
1099 limit = last_block +
Chris Mason84f54cf2007-06-12 07:43:08 -04001100 (block_group->key.offset >> 1);
Chris Masonde428b62007-05-18 13:28:27 -04001101 else
1102 limit = search_start +
Chris Mason84f54cf2007-06-12 07:43:08 -04001103 (block_group->key.offset >> 1);
Chris Mason5caf2a02007-04-02 11:20:42 -04001104 ret = btrfs_next_leaf(root, path);
Chris Masonfec577f2007-02-26 10:40:21 -05001105 if (ret == 0)
1106 continue;
Chris Mason0f70abe2007-02-28 16:46:22 -05001107 if (ret < 0)
1108 goto error;
Chris Masonfec577f2007-02-26 10:40:21 -05001109 if (!start_found) {
1110 ins->objectid = search_start;
Chris Mason3e1ad542007-05-07 20:03:49 -04001111 ins->offset = search_end - search_start;
Chris Masonfec577f2007-02-26 10:40:21 -05001112 start_found = 1;
1113 goto check_pending;
1114 }
1115 ins->objectid = last_block > search_start ?
1116 last_block : search_start;
Chris Mason3e1ad542007-05-07 20:03:49 -04001117 ins->offset = search_end - ins->objectid;
Chris Masonfec577f2007-02-26 10:40:21 -05001118 goto check_pending;
1119 }
Chris Masone37c9e62007-05-09 20:13:14 -04001120
Chris Masone2fa7222007-03-12 16:22:34 -04001121 btrfs_disk_key_to_cpu(&key, &l->items[slot].key);
Chris Masone37c9e62007-05-09 20:13:14 -04001122 if (key.objectid >= search_start && key.objectid > last_block &&
1123 start_found) {
1124 if (last_block < search_start)
1125 last_block = search_start;
1126 hole_size = key.objectid - last_block;
1127 if (hole_size >= num_blocks) {
1128 ins->objectid = last_block;
1129 ins->offset = hole_size;
1130 goto check_pending;
Chris Mason0579da42007-03-07 16:15:30 -05001131 }
Chris Masonfec577f2007-02-26 10:40:21 -05001132 }
Chris Masone37c9e62007-05-09 20:13:14 -04001133
1134 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY)
1135 goto next;
1136
Chris Mason0579da42007-03-07 16:15:30 -05001137 start_found = 1;
Chris Masone2fa7222007-03-12 16:22:34 -04001138 last_block = key.objectid + key.offset;
Chris Masonfbdc7622007-05-30 10:22:12 -04001139 if (!full_scan && last_block >= block_group->key.objectid +
Chris Masonbe744172007-05-06 10:15:01 -04001140 block_group->key.offset) {
1141 btrfs_release_path(root, path);
1142 search_start = block_group->key.objectid +
1143 block_group->key.offset * 2;
1144 goto new_group;
1145 }
Chris Mason9078a3e2007-04-26 16:46:15 -04001146next:
Chris Mason5caf2a02007-04-02 11:20:42 -04001147 path->slots[0]++;
Chris Masonde428b62007-05-18 13:28:27 -04001148 cond_resched();
Chris Masonfec577f2007-02-26 10:40:21 -05001149 }
Chris Masonfec577f2007-02-26 10:40:21 -05001150check_pending:
1151 /* we have to make sure we didn't find an extent that has already
1152 * been allocated by the map tree or the original allocation
1153 */
Chris Mason5caf2a02007-04-02 11:20:42 -04001154 btrfs_release_path(root, path);
Chris Masonfec577f2007-02-26 10:40:21 -05001155 BUG_ON(ins->objectid < search_start);
Chris Masone37c9e62007-05-09 20:13:14 -04001156
Chris Mason3e1ad542007-05-07 20:03:49 -04001157 if (ins->objectid + num_blocks >= search_end) {
Chris Masonfbdc7622007-05-30 10:22:12 -04001158 if (full_scan) {
1159 ret = -ENOSPC;
1160 goto error;
1161 }
Chris Masonbe744172007-05-06 10:15:01 -04001162 search_start = orig_search_start;
Chris Mason6702ed42007-08-07 16:15:09 -04001163 if (wrapped) {
1164 if (!full_scan)
1165 total_needed -= empty_size;
Chris Masonfbdc7622007-05-30 10:22:12 -04001166 full_scan = 1;
Chris Mason6702ed42007-08-07 16:15:09 -04001167 } else
Chris Masonfbdc7622007-05-30 10:22:12 -04001168 wrapped = 1;
Chris Masonbe744172007-05-06 10:15:01 -04001169 goto new_group;
Chris Mason06a2f9f2007-04-28 08:48:10 -04001170 }
Chris Mason037e6392007-03-07 11:50:24 -05001171 for (test_block = ins->objectid;
Chris Masonf2458e12007-04-25 15:52:25 -04001172 test_block < ins->objectid + num_blocks; test_block++) {
1173 if (test_radix_bit(&info->pinned_radix, test_block)) {
Chris Mason037e6392007-03-07 11:50:24 -05001174 search_start = test_block + 1;
Chris Masonbe744172007-05-06 10:15:01 -04001175 goto new_group;
Chris Masonfec577f2007-02-26 10:40:21 -05001176 }
1177 }
Chris Masonf2458e12007-04-25 15:52:25 -04001178 if (!fill_prealloc && info->extent_tree_insert_nr) {
1179 u64 last =
1180 info->extent_tree_insert[info->extent_tree_insert_nr - 1];
1181 if (ins->objectid + num_blocks >
1182 info->extent_tree_insert[0] &&
1183 ins->objectid <= last) {
1184 search_start = last + 1;
Chris Masone37c9e62007-05-09 20:13:14 -04001185 WARN_ON(!full_scan);
Chris Masonbe744172007-05-06 10:15:01 -04001186 goto new_group;
Chris Masonf2458e12007-04-25 15:52:25 -04001187 }
1188 }
1189 if (!fill_prealloc && info->extent_tree_prealloc_nr) {
1190 u64 first =
1191 info->extent_tree_prealloc[info->extent_tree_prealloc_nr - 1];
1192 if (ins->objectid + num_blocks > first &&
1193 ins->objectid <= info->extent_tree_prealloc[0]) {
1194 search_start = info->extent_tree_prealloc[0] + 1;
Chris Masonbe744172007-05-06 10:15:01 -04001195 goto new_group;
Chris Masonf2458e12007-04-25 15:52:25 -04001196 }
1197 }
Chris Masonf2654de2007-06-26 12:20:46 -04001198 if (exclude_nr > 0 && (ins->objectid + num_blocks > exclude_start &&
1199 ins->objectid < exclude_start + exclude_nr)) {
1200 search_start = exclude_start + exclude_nr;
1201 goto new_group;
1202 }
Chris Masonf2458e12007-04-25 15:52:25 -04001203 if (fill_prealloc) {
1204 int nr;
1205 test_block = ins->objectid;
Chris Masone37c9e62007-05-09 20:13:14 -04001206 if (test_block - info->extent_tree_prealloc[total_needed - 1] >=
1207 leaf_range(root)) {
1208 total_found = 0;
1209 info->extent_tree_prealloc_nr = total_found;
1210 }
Chris Masonf2458e12007-04-25 15:52:25 -04001211 while(test_block < ins->objectid + ins->offset &&
1212 total_found < total_needed) {
1213 nr = total_needed - total_found - 1;
1214 BUG_ON(nr < 0);
Chris Masoncd1bc462007-04-27 10:08:34 -04001215 info->extent_tree_prealloc[nr] = test_block;
Chris Masonf2458e12007-04-25 15:52:25 -04001216 total_found++;
1217 test_block++;
1218 }
1219 if (total_found < total_needed) {
1220 search_start = test_block;
Chris Masonbe744172007-05-06 10:15:01 -04001221 goto new_group;
Chris Masonf2458e12007-04-25 15:52:25 -04001222 }
Chris Masoncd1bc462007-04-27 10:08:34 -04001223 info->extent_tree_prealloc_nr = total_found;
Chris Masonf2458e12007-04-25 15:52:25 -04001224 }
Chris Masone37c9e62007-05-09 20:13:14 -04001225 if (!data) {
Chris Mason5276aed2007-06-11 21:33:38 -04001226 block_group = btrfs_lookup_block_group(info, ins->objectid);
Chris Masone37c9e62007-05-09 20:13:14 -04001227 if (block_group) {
1228 if (fill_prealloc)
1229 block_group->last_prealloc =
1230 info->extent_tree_prealloc[total_needed-1];
1231 else
1232 trans->block_group = block_group;
1233 }
Chris Masoncd1bc462007-04-27 10:08:34 -04001234 }
Chris Mason037e6392007-03-07 11:50:24 -05001235 ins->offset = num_blocks;
Chris Mason5caf2a02007-04-02 11:20:42 -04001236 btrfs_free_path(path);
Chris Masonfec577f2007-02-26 10:40:21 -05001237 return 0;
Chris Masonbe744172007-05-06 10:15:01 -04001238
1239new_group:
Chris Mason3e1ad542007-05-07 20:03:49 -04001240 if (search_start + num_blocks >= search_end) {
Chris Masonbe744172007-05-06 10:15:01 -04001241 search_start = orig_search_start;
Chris Masonfbdc7622007-05-30 10:22:12 -04001242 if (full_scan) {
1243 ret = -ENOSPC;
1244 goto error;
1245 }
Chris Mason6702ed42007-08-07 16:15:09 -04001246 if (wrapped) {
1247 if (!full_scan)
1248 total_needed -= empty_size;
Chris Masonfbdc7622007-05-30 10:22:12 -04001249 full_scan = 1;
Chris Mason6702ed42007-08-07 16:15:09 -04001250 } else
Chris Masonfbdc7622007-05-30 10:22:12 -04001251 wrapped = 1;
Chris Masonbe744172007-05-06 10:15:01 -04001252 }
Chris Mason5276aed2007-06-11 21:33:38 -04001253 block_group = btrfs_lookup_block_group(info, search_start);
Chris Masonfbdc7622007-05-30 10:22:12 -04001254 cond_resched();
Chris Masonbe744172007-05-06 10:15:01 -04001255 if (!full_scan)
1256 block_group = btrfs_find_block_group(root, block_group,
Chris Masonde428b62007-05-18 13:28:27 -04001257 search_start, data, 0);
Chris Masonbe744172007-05-06 10:15:01 -04001258 goto check_failed;
1259
Chris Mason0f70abe2007-02-28 16:46:22 -05001260error:
Chris Mason5caf2a02007-04-02 11:20:42 -04001261 btrfs_release_path(root, path);
1262 btrfs_free_path(path);
Chris Mason0f70abe2007-02-28 16:46:22 -05001263 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05001264}
Chris Masonfec577f2007-02-26 10:40:21 -05001265/*
Chris Masonfec577f2007-02-26 10:40:21 -05001266 * finds a free extent and does all the dirty work required for allocation
1267 * returns the key for the extent through ins, and a tree buffer for
1268 * the first block of the extent through buf.
1269 *
1270 * returns 0 if everything worked, non-zero otherwise.
1271 */
Chris Mason4d775672007-04-20 20:23:12 -04001272int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
1273 struct btrfs_root *root, u64 owner,
Chris Mason6702ed42007-08-07 16:15:09 -04001274 u64 num_blocks, u64 empty_size, u64 hint_block,
Chris Masonbe08c1b2007-05-03 09:06:49 -04001275 u64 search_end, struct btrfs_key *ins, int data)
Chris Masonfec577f2007-02-26 10:40:21 -05001276{
1277 int ret;
1278 int pending_ret;
Chris Mason1261ec42007-03-20 20:35:03 -04001279 u64 super_blocks_used;
Chris Masonfbdc7622007-05-30 10:22:12 -04001280 u64 search_start = 0;
Chris Masonf2654de2007-06-26 12:20:46 -04001281 u64 exclude_start = 0;
1282 u64 exclude_nr = 0;
Chris Mason1261ec42007-03-20 20:35:03 -04001283 struct btrfs_fs_info *info = root->fs_info;
1284 struct btrfs_root *extent_root = info->extent_root;
Chris Mason234b63a2007-03-13 10:46:10 -04001285 struct btrfs_extent_item extent_item;
Chris Masonf2458e12007-04-25 15:52:25 -04001286 struct btrfs_key prealloc_key;
Chris Mason037e6392007-03-07 11:50:24 -05001287
Chris Masoncf27e1e2007-03-13 09:49:06 -04001288 btrfs_set_extent_refs(&extent_item, 1);
Chris Mason4d775672007-04-20 20:23:12 -04001289 btrfs_set_extent_owner(&extent_item, owner);
Chris Masonfec577f2007-02-26 10:40:21 -05001290
Chris Mason037e6392007-03-07 11:50:24 -05001291 if (root == extent_root) {
Chris Masonf2458e12007-04-25 15:52:25 -04001292 int nr;
1293 BUG_ON(info->extent_tree_prealloc_nr == 0);
Chris Mason037e6392007-03-07 11:50:24 -05001294 BUG_ON(num_blocks != 1);
Chris Mason037e6392007-03-07 11:50:24 -05001295 ins->offset = 1;
Chris Masonf2458e12007-04-25 15:52:25 -04001296 info->extent_tree_prealloc_nr--;
1297 nr = info->extent_tree_prealloc_nr;
1298 ins->objectid = info->extent_tree_prealloc[nr];
1299 info->extent_tree_insert[info->extent_tree_insert_nr++] =
1300 ins->objectid;
Chris Mason9078a3e2007-04-26 16:46:15 -04001301 ret = update_block_group(trans, root,
Chris Mason1e2677e2007-05-29 16:52:18 -04001302 ins->objectid, ins->offset, 1, 0, 0);
Chris Mason9078a3e2007-04-26 16:46:15 -04001303 BUG_ON(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05001304 return 0;
1305 }
Chris Masone37c9e62007-05-09 20:13:14 -04001306
1307 /*
1308 * if we're doing a data allocation, preallocate room in the
1309 * extent tree first. This way the extent tree blocks end up
1310 * in the correct block group.
1311 */
1312 if (data) {
Chris Mason6702ed42007-08-07 16:15:09 -04001313 ret = find_free_extent(trans, root, 0, 0, 0,
Chris Masonf2654de2007-06-26 12:20:46 -04001314 search_end, 0, &prealloc_key, 0, 0, 0);
Chris Masonccd467d2007-06-28 15:57:36 -04001315 BUG_ON(ret);
Chris Mason54aa1f42007-06-22 14:16:25 -04001316 if (ret)
1317 return ret;
Chris Masonf2654de2007-06-26 12:20:46 -04001318 exclude_nr = info->extent_tree_prealloc_nr;
1319 exclude_start = info->extent_tree_prealloc[exclude_nr - 1];
Chris Masone37c9e62007-05-09 20:13:14 -04001320 }
Chris Masonfec577f2007-02-26 10:40:21 -05001321
Chris Masonf2654de2007-06-26 12:20:46 -04001322 /* do the real allocation */
Chris Mason6702ed42007-08-07 16:15:09 -04001323 ret = find_free_extent(trans, root, num_blocks, empty_size,
1324 search_start, search_end, hint_block, ins,
Chris Masonf2654de2007-06-26 12:20:46 -04001325 exclude_start, exclude_nr, data);
Chris Masonccd467d2007-06-28 15:57:36 -04001326 BUG_ON(ret);
Chris Masonf2654de2007-06-26 12:20:46 -04001327 if (ret)
1328 return ret;
1329
Chris Masone37c9e62007-05-09 20:13:14 -04001330 /*
1331 * if we're doing a metadata allocation, preallocate space in the
1332 * extent tree second. This way, we don't create a tiny hole
1333 * in the allocation map between any unused preallocation blocks
1334 * and the metadata block we're actually allocating. On disk,
1335 * it'll go:
1336 * [block we've allocated], [used prealloc 1], [ unused prealloc ]
1337 * The unused prealloc will get reused the next time around.
1338 */
1339 if (!data) {
Chris Masonf2654de2007-06-26 12:20:46 -04001340 exclude_start = ins->objectid;
1341 exclude_nr = ins->offset;
Chris Masonccd467d2007-06-28 15:57:36 -04001342 hint_block = exclude_start + exclude_nr;
Chris Mason6702ed42007-08-07 16:15:09 -04001343 ret = find_free_extent(trans, root, 0, 0, search_start,
Chris Masonfbdc7622007-05-30 10:22:12 -04001344 search_end, hint_block,
Chris Masonf2654de2007-06-26 12:20:46 -04001345 &prealloc_key, exclude_start,
1346 exclude_nr, 0);
Chris Masonccd467d2007-06-28 15:57:36 -04001347 BUG_ON(ret);
Chris Masonf2654de2007-06-26 12:20:46 -04001348 if (ret)
1349 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -04001350 }
Chris Masonf2458e12007-04-25 15:52:25 -04001351
Chris Mason4b52dff2007-06-26 10:06:50 -04001352 super_blocks_used = btrfs_super_blocks_used(&info->super_copy);
1353 btrfs_set_super_blocks_used(&info->super_copy, super_blocks_used +
Chris Mason1261ec42007-03-20 20:35:03 -04001354 num_blocks);
Chris Masone089f052007-03-16 16:20:31 -04001355 ret = btrfs_insert_item(trans, extent_root, ins, &extent_item,
1356 sizeof(extent_item));
Chris Mason037e6392007-03-07 11:50:24 -05001357
Chris Masonccd467d2007-06-28 15:57:36 -04001358 BUG_ON(ret);
Chris Masone089f052007-03-16 16:20:31 -04001359 finish_current_insert(trans, extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -04001360 pending_ret = del_pending_extents(trans, extent_root);
Chris Masone37c9e62007-05-09 20:13:14 -04001361 if (ret) {
Chris Mason037e6392007-03-07 11:50:24 -05001362 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -04001363 }
1364 if (pending_ret) {
Chris Mason037e6392007-03-07 11:50:24 -05001365 return pending_ret;
Chris Masone37c9e62007-05-09 20:13:14 -04001366 }
Chris Mason1e2677e2007-05-29 16:52:18 -04001367 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0,
1368 data);
Chris Masonfabb5682007-06-07 22:13:21 -04001369 BUG_ON(ret);
Chris Mason037e6392007-03-07 11:50:24 -05001370 return 0;
Chris Masonfec577f2007-02-26 10:40:21 -05001371}
1372
1373/*
1374 * helper function to allocate a block for a given tree
1375 * returns the tree buffer or NULL.
1376 */
Chris Masone20d96d2007-03-22 12:13:20 -04001377struct buffer_head *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Chris Mason6702ed42007-08-07 16:15:09 -04001378 struct btrfs_root *root, u64 hint,
1379 u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05001380{
Chris Masone2fa7222007-03-12 16:22:34 -04001381 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -05001382 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -04001383 struct buffer_head *buf;
Chris Masonfec577f2007-02-26 10:40:21 -05001384
Chris Mason4d775672007-04-20 20:23:12 -04001385 ret = btrfs_alloc_extent(trans, root, root->root_key.objectid,
Chris Mason6702ed42007-08-07 16:15:09 -04001386 1, empty_size, hint,
1387 (unsigned long)-1, &ins, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05001388 if (ret) {
Chris Mason54aa1f42007-06-22 14:16:25 -04001389 BUG_ON(ret > 0);
1390 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05001391 }
Chris Masond98237b2007-03-28 13:57:48 -04001392 buf = btrfs_find_create_tree_block(root, ins.objectid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001393 if (!buf) {
1394 btrfs_free_extent(trans, root, ins.objectid, 1, 0);
1395 return ERR_PTR(-ENOMEM);
1396 }
Chris Mason6702ed42007-08-07 16:15:09 -04001397 WARN_ON(buffer_dirty(buf));
Chris Masondf2ce342007-03-23 11:00:45 -04001398 set_buffer_uptodate(buf);
Chris Mason090d1872007-05-01 08:53:32 -04001399 set_buffer_checked(buf);
Chris Mason7c4452b2007-04-28 09:29:35 -04001400 set_radix_bit(&trans->transaction->dirty_pages, buf->b_page->index);
Chris Masonfec577f2007-02-26 10:40:21 -05001401 return buf;
1402}
Chris Masona28ec192007-03-06 20:08:01 -05001403
Chris Mason6407bf62007-03-27 06:33:00 -04001404static int drop_leaf_ref(struct btrfs_trans_handle *trans,
1405 struct btrfs_root *root, struct buffer_head *cur)
1406{
1407 struct btrfs_disk_key *key;
1408 struct btrfs_leaf *leaf;
1409 struct btrfs_file_extent_item *fi;
1410 int i;
1411 int nritems;
1412 int ret;
1413
1414 BUG_ON(!btrfs_is_leaf(btrfs_buffer_node(cur)));
1415 leaf = btrfs_buffer_leaf(cur);
1416 nritems = btrfs_header_nritems(&leaf->header);
1417 for (i = 0; i < nritems; i++) {
Chris Mason3a686372007-05-24 13:35:57 -04001418 u64 disk_blocknr;
Chris Mason6407bf62007-03-27 06:33:00 -04001419 key = &leaf->items[i].key;
1420 if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY)
1421 continue;
1422 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
Chris Mason236454d2007-04-19 13:37:44 -04001423 if (btrfs_file_extent_type(fi) == BTRFS_FILE_EXTENT_INLINE)
1424 continue;
Chris Mason6407bf62007-03-27 06:33:00 -04001425 /*
1426 * FIXME make sure to insert a trans record that
1427 * repeats the snapshot del on crash
1428 */
Chris Mason3a686372007-05-24 13:35:57 -04001429 disk_blocknr = btrfs_file_extent_disk_blocknr(fi);
1430 if (disk_blocknr == 0)
1431 continue;
1432 ret = btrfs_free_extent(trans, root, disk_blocknr,
Chris Mason6407bf62007-03-27 06:33:00 -04001433 btrfs_file_extent_disk_num_blocks(fi),
1434 0);
1435 BUG_ON(ret);
1436 }
1437 return 0;
1438}
1439
Chris Masone0115992007-06-19 16:23:05 -04001440static void reada_walk_down(struct btrfs_root *root,
1441 struct btrfs_node *node)
1442{
1443 int i;
1444 u32 nritems;
1445 u64 blocknr;
1446 int ret;
1447 u32 refs;
1448
1449 nritems = btrfs_header_nritems(&node->header);
1450 for (i = 0; i < nritems; i++) {
1451 blocknr = btrfs_node_blockptr(node, i);
1452 ret = lookup_extent_ref(NULL, root, blocknr, 1, &refs);
1453 BUG_ON(ret);
1454 if (refs != 1)
1455 continue;
1456 ret = readahead_tree_block(root, blocknr);
1457 if (ret)
1458 break;
1459 }
1460}
1461
Chris Mason9aca1d52007-03-13 11:09:37 -04001462/*
1463 * helper function for drop_snapshot, this walks down the tree dropping ref
1464 * counts as it goes.
1465 */
Chris Masone089f052007-03-16 16:20:31 -04001466static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
1467 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05001468{
Chris Masone20d96d2007-03-22 12:13:20 -04001469 struct buffer_head *next;
1470 struct buffer_head *cur;
Chris Mason20524f02007-03-10 06:35:47 -05001471 u64 blocknr;
1472 int ret;
1473 u32 refs;
1474
Chris Mason5caf2a02007-04-02 11:20:42 -04001475 WARN_ON(*level < 0);
1476 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Masonb18c6682007-04-17 13:26:50 -04001477 ret = lookup_extent_ref(trans, root, bh_blocknr(path->nodes[*level]),
Chris Mason6407bf62007-03-27 06:33:00 -04001478 1, &refs);
Chris Mason20524f02007-03-10 06:35:47 -05001479 BUG_ON(ret);
1480 if (refs > 1)
1481 goto out;
Chris Masone0115992007-06-19 16:23:05 -04001482
Chris Mason9aca1d52007-03-13 11:09:37 -04001483 /*
1484 * walk down to the last node level and free all the leaves
1485 */
Chris Mason6407bf62007-03-27 06:33:00 -04001486 while(*level >= 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001487 WARN_ON(*level < 0);
1488 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason20524f02007-03-10 06:35:47 -05001489 cur = path->nodes[*level];
Chris Masone0115992007-06-19 16:23:05 -04001490
1491 if (*level > 0 && path->slots[*level] == 0)
1492 reada_walk_down(root, btrfs_buffer_node(cur));
1493
Chris Mason2c90e5d2007-04-02 10:50:19 -04001494 if (btrfs_header_level(btrfs_buffer_header(cur)) != *level)
1495 WARN_ON(1);
Chris Masone0115992007-06-19 16:23:05 -04001496
Chris Mason7518a232007-03-12 12:01:18 -04001497 if (path->slots[*level] >=
Chris Masone20d96d2007-03-22 12:13:20 -04001498 btrfs_header_nritems(btrfs_buffer_header(cur)))
Chris Mason20524f02007-03-10 06:35:47 -05001499 break;
Chris Mason6407bf62007-03-27 06:33:00 -04001500 if (*level == 0) {
1501 ret = drop_leaf_ref(trans, root, cur);
1502 BUG_ON(ret);
1503 break;
1504 }
Chris Masone20d96d2007-03-22 12:13:20 -04001505 blocknr = btrfs_node_blockptr(btrfs_buffer_node(cur),
1506 path->slots[*level]);
Chris Masonb18c6682007-04-17 13:26:50 -04001507 ret = lookup_extent_ref(trans, root, blocknr, 1, &refs);
Chris Mason6407bf62007-03-27 06:33:00 -04001508 BUG_ON(ret);
1509 if (refs != 1) {
Chris Mason20524f02007-03-10 06:35:47 -05001510 path->slots[*level]++;
Chris Masone089f052007-03-16 16:20:31 -04001511 ret = btrfs_free_extent(trans, root, blocknr, 1, 1);
Chris Mason20524f02007-03-10 06:35:47 -05001512 BUG_ON(ret);
1513 continue;
1514 }
Chris Mason20524f02007-03-10 06:35:47 -05001515 next = read_tree_block(root, blocknr);
Chris Mason5caf2a02007-04-02 11:20:42 -04001516 WARN_ON(*level <= 0);
Chris Mason83e15a22007-03-12 09:03:27 -04001517 if (path->nodes[*level-1])
Chris Mason234b63a2007-03-13 10:46:10 -04001518 btrfs_block_release(root, path->nodes[*level-1]);
Chris Mason20524f02007-03-10 06:35:47 -05001519 path->nodes[*level-1] = next;
Chris Masone20d96d2007-03-22 12:13:20 -04001520 *level = btrfs_header_level(btrfs_buffer_header(next));
Chris Mason20524f02007-03-10 06:35:47 -05001521 path->slots[*level] = 0;
1522 }
1523out:
Chris Mason5caf2a02007-04-02 11:20:42 -04001524 WARN_ON(*level < 0);
1525 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason6407bf62007-03-27 06:33:00 -04001526 ret = btrfs_free_extent(trans, root,
Chris Mason7eccb902007-04-11 15:53:25 -04001527 bh_blocknr(path->nodes[*level]), 1, 1);
Chris Mason234b63a2007-03-13 10:46:10 -04001528 btrfs_block_release(root, path->nodes[*level]);
Chris Mason20524f02007-03-10 06:35:47 -05001529 path->nodes[*level] = NULL;
1530 *level += 1;
1531 BUG_ON(ret);
1532 return 0;
1533}
1534
Chris Mason9aca1d52007-03-13 11:09:37 -04001535/*
1536 * helper for dropping snapshots. This walks back up the tree in the path
1537 * to find the first node higher up where we haven't yet gone through
1538 * all the slots
1539 */
Chris Masone089f052007-03-16 16:20:31 -04001540static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root
1541 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05001542{
1543 int i;
1544 int slot;
1545 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04001546 struct btrfs_root_item *root_item = &root->root_item;
1547
Chris Mason234b63a2007-03-13 10:46:10 -04001548 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Mason20524f02007-03-10 06:35:47 -05001549 slot = path->slots[i];
Chris Masone20d96d2007-03-22 12:13:20 -04001550 if (slot < btrfs_header_nritems(
1551 btrfs_buffer_header(path->nodes[i])) - 1) {
Chris Mason9f3a7422007-08-07 15:52:19 -04001552 struct btrfs_node *node;
1553 node = btrfs_buffer_node(path->nodes[i]);
Chris Mason20524f02007-03-10 06:35:47 -05001554 path->slots[i]++;
1555 *level = i;
Chris Mason9f3a7422007-08-07 15:52:19 -04001556 WARN_ON(*level == 0);
1557 memcpy(&root_item->drop_progress,
1558 &node->ptrs[path->slots[i]].key,
1559 sizeof(root_item->drop_progress));
1560 root_item->drop_level = i;
Chris Mason20524f02007-03-10 06:35:47 -05001561 return 0;
1562 } else {
Chris Masone089f052007-03-16 16:20:31 -04001563 ret = btrfs_free_extent(trans, root,
Chris Mason7eccb902007-04-11 15:53:25 -04001564 bh_blocknr(path->nodes[*level]),
Chris Masone089f052007-03-16 16:20:31 -04001565 1, 1);
Chris Mason6407bf62007-03-27 06:33:00 -04001566 BUG_ON(ret);
Chris Mason234b63a2007-03-13 10:46:10 -04001567 btrfs_block_release(root, path->nodes[*level]);
Chris Mason83e15a22007-03-12 09:03:27 -04001568 path->nodes[*level] = NULL;
Chris Mason20524f02007-03-10 06:35:47 -05001569 *level = i + 1;
Chris Mason20524f02007-03-10 06:35:47 -05001570 }
1571 }
1572 return 1;
1573}
1574
Chris Mason9aca1d52007-03-13 11:09:37 -04001575/*
1576 * drop the reference count on the tree rooted at 'snap'. This traverses
1577 * the tree freeing any blocks that have a ref count of zero after being
1578 * decremented.
1579 */
Chris Masone089f052007-03-16 16:20:31 -04001580int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason9f3a7422007-08-07 15:52:19 -04001581 *root)
Chris Mason20524f02007-03-10 06:35:47 -05001582{
Chris Mason3768f362007-03-13 16:47:54 -04001583 int ret = 0;
Chris Mason9aca1d52007-03-13 11:09:37 -04001584 int wret;
Chris Mason20524f02007-03-10 06:35:47 -05001585 int level;
Chris Mason5caf2a02007-04-02 11:20:42 -04001586 struct btrfs_path *path;
Chris Mason20524f02007-03-10 06:35:47 -05001587 int i;
1588 int orig_level;
Chris Mason9f3a7422007-08-07 15:52:19 -04001589 int num_walks = 0;
1590 struct btrfs_root_item *root_item = &root->root_item;
Chris Mason20524f02007-03-10 06:35:47 -05001591
Chris Mason5caf2a02007-04-02 11:20:42 -04001592 path = btrfs_alloc_path();
1593 BUG_ON(!path);
Chris Mason20524f02007-03-10 06:35:47 -05001594
Chris Mason9f3a7422007-08-07 15:52:19 -04001595 level = btrfs_header_level(btrfs_buffer_header(root->node));
Chris Mason20524f02007-03-10 06:35:47 -05001596 orig_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04001597 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
1598 path->nodes[level] = root->node;
1599 path->slots[level] = 0;
1600 } else {
1601 struct btrfs_key key;
1602 struct btrfs_disk_key *found_key;
1603 struct btrfs_node *node;
Chris Mason6702ed42007-08-07 16:15:09 -04001604
Chris Mason9f3a7422007-08-07 15:52:19 -04001605 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Chris Mason6702ed42007-08-07 16:15:09 -04001606 level = root_item->drop_level;
1607 path->lowest_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04001608 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason6702ed42007-08-07 16:15:09 -04001609 if (wret < 0) {
Chris Mason9f3a7422007-08-07 15:52:19 -04001610 ret = wret;
1611 goto out;
1612 }
Chris Mason9f3a7422007-08-07 15:52:19 -04001613 node = btrfs_buffer_node(path->nodes[level]);
1614 found_key = &node->ptrs[path->slots[level]].key;
1615 WARN_ON(memcmp(found_key, &root_item->drop_progress,
1616 sizeof(*found_key)));
1617 }
Chris Mason20524f02007-03-10 06:35:47 -05001618 while(1) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001619 wret = walk_down_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04001620 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05001621 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04001622 if (wret < 0)
1623 ret = wret;
1624
Chris Mason5caf2a02007-04-02 11:20:42 -04001625 wret = walk_up_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04001626 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05001627 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04001628 if (wret < 0)
1629 ret = wret;
Chris Mason9f3a7422007-08-07 15:52:19 -04001630 num_walks++;
1631 if (num_walks > 10) {
Chris Mason9f3a7422007-08-07 15:52:19 -04001632 ret = -EAGAIN;
1633 get_bh(root->node);
1634 break;
1635 }
Chris Mason20524f02007-03-10 06:35:47 -05001636 }
Chris Mason83e15a22007-03-12 09:03:27 -04001637 for (i = 0; i <= orig_level; i++) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001638 if (path->nodes[i]) {
1639 btrfs_block_release(root, path->nodes[i]);
Chris Mason6702ed42007-08-07 16:15:09 -04001640 path->nodes[i] = 0;
Chris Mason83e15a22007-03-12 09:03:27 -04001641 }
Chris Mason20524f02007-03-10 06:35:47 -05001642 }
Chris Mason9f3a7422007-08-07 15:52:19 -04001643out:
Chris Mason5caf2a02007-04-02 11:20:42 -04001644 btrfs_free_path(path);
Chris Mason9aca1d52007-03-13 11:09:37 -04001645 return ret;
Chris Mason20524f02007-03-10 06:35:47 -05001646}
Chris Mason9078a3e2007-04-26 16:46:15 -04001647
Chris Masonbe744172007-05-06 10:15:01 -04001648static int free_block_group_radix(struct radix_tree_root *radix)
Chris Mason9078a3e2007-04-26 16:46:15 -04001649{
1650 int ret;
1651 struct btrfs_block_group_cache *cache[8];
1652 int i;
1653
1654 while(1) {
Chris Masonbe744172007-05-06 10:15:01 -04001655 ret = radix_tree_gang_lookup(radix, (void **)cache, 0,
Chris Mason9078a3e2007-04-26 16:46:15 -04001656 ARRAY_SIZE(cache));
1657 if (!ret)
1658 break;
1659 for (i = 0; i < ret; i++) {
Chris Masonbe744172007-05-06 10:15:01 -04001660 radix_tree_delete(radix, cache[i]->key.objectid +
Chris Mason9078a3e2007-04-26 16:46:15 -04001661 cache[i]->key.offset - 1);
1662 kfree(cache[i]);
1663 }
1664 }
1665 return 0;
1666}
1667
Chris Masonbe744172007-05-06 10:15:01 -04001668int btrfs_free_block_groups(struct btrfs_fs_info *info)
1669{
1670 int ret;
1671 int ret2;
Chris Masone37c9e62007-05-09 20:13:14 -04001672 unsigned long gang[16];
1673 int i;
Chris Masonbe744172007-05-06 10:15:01 -04001674
1675 ret = free_block_group_radix(&info->block_group_radix);
1676 ret2 = free_block_group_radix(&info->block_group_data_radix);
1677 if (ret)
1678 return ret;
1679 if (ret2)
1680 return ret2;
Chris Masone37c9e62007-05-09 20:13:14 -04001681
1682 while(1) {
1683 ret = find_first_radix_bit(&info->extent_map_radix,
1684 gang, 0, ARRAY_SIZE(gang));
1685 if (!ret)
1686 break;
1687 for (i = 0; i < ret; i++) {
1688 clear_radix_bit(&info->extent_map_radix, gang[i]);
1689 }
1690 }
Chris Masonbe744172007-05-06 10:15:01 -04001691 return 0;
1692}
1693
Chris Mason9078a3e2007-04-26 16:46:15 -04001694int btrfs_read_block_groups(struct btrfs_root *root)
1695{
1696 struct btrfs_path *path;
1697 int ret;
1698 int err = 0;
1699 struct btrfs_block_group_item *bi;
1700 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04001701 struct btrfs_fs_info *info = root->fs_info;
1702 struct radix_tree_root *radix;
Chris Mason9078a3e2007-04-26 16:46:15 -04001703 struct btrfs_key key;
1704 struct btrfs_key found_key;
1705 struct btrfs_leaf *leaf;
Chris Mason84f54cf2007-06-12 07:43:08 -04001706 u64 group_size_blocks;
Chris Mason31f3c992007-04-30 15:25:45 -04001707 u64 used;
Chris Mason9078a3e2007-04-26 16:46:15 -04001708
Chris Mason84f54cf2007-06-12 07:43:08 -04001709 group_size_blocks = BTRFS_BLOCK_GROUP_SIZE >>
1710 root->fs_info->sb->s_blocksize_bits;
Chris Masonbe744172007-05-06 10:15:01 -04001711 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04001712 key.objectid = 0;
1713 key.offset = group_size_blocks;
1714 key.flags = 0;
1715 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
1716
1717 path = btrfs_alloc_path();
1718 if (!path)
1719 return -ENOMEM;
1720
1721 while(1) {
Chris Masonbe744172007-05-06 10:15:01 -04001722 ret = btrfs_search_slot(NULL, info->extent_root,
Chris Mason9078a3e2007-04-26 16:46:15 -04001723 &key, path, 0, 0);
1724 if (ret != 0) {
1725 err = ret;
1726 break;
1727 }
1728 leaf = btrfs_buffer_leaf(path->nodes[0]);
1729 btrfs_disk_key_to_cpu(&found_key,
1730 &leaf->items[path->slots[0]].key);
1731 cache = kmalloc(sizeof(*cache), GFP_NOFS);
1732 if (!cache) {
1733 err = -1;
1734 break;
1735 }
Chris Mason3e1ad542007-05-07 20:03:49 -04001736
Chris Mason9078a3e2007-04-26 16:46:15 -04001737 bi = btrfs_item_ptr(leaf, path->slots[0],
1738 struct btrfs_block_group_item);
Chris Mason1e2677e2007-05-29 16:52:18 -04001739 if (bi->flags & BTRFS_BLOCK_GROUP_DATA) {
1740 radix = &info->block_group_data_radix;
1741 cache->data = 1;
1742 } else {
1743 radix = &info->block_group_radix;
1744 cache->data = 0;
1745 }
1746
Chris Mason9078a3e2007-04-26 16:46:15 -04001747 memcpy(&cache->item, bi, sizeof(*bi));
1748 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Mason31f3c992007-04-30 15:25:45 -04001749 cache->last_alloc = cache->key.objectid;
1750 cache->first_free = cache->key.objectid;
Chris Masone37c9e62007-05-09 20:13:14 -04001751 cache->last_prealloc = cache->key.objectid;
Chris Masonbe744172007-05-06 10:15:01 -04001752 cache->pinned = 0;
Chris Masone37c9e62007-05-09 20:13:14 -04001753 cache->cached = 0;
1754
Chris Mason3e1ad542007-05-07 20:03:49 -04001755 cache->radix = radix;
1756
Chris Mason9078a3e2007-04-26 16:46:15 -04001757 key.objectid = found_key.objectid + found_key.offset;
1758 btrfs_release_path(root, path);
Chris Masonbe744172007-05-06 10:15:01 -04001759 ret = radix_tree_insert(radix, found_key.objectid +
Chris Mason9078a3e2007-04-26 16:46:15 -04001760 found_key.offset - 1,
1761 (void *)cache);
1762 BUG_ON(ret);
Chris Mason31f3c992007-04-30 15:25:45 -04001763 used = btrfs_block_group_used(bi);
Chris Mason84f54cf2007-06-12 07:43:08 -04001764 if (used < div_factor(key.offset, 8)) {
Chris Masonbe744172007-05-06 10:15:01 -04001765 radix_tree_tag_set(radix, found_key.objectid +
Chris Mason31f3c992007-04-30 15:25:45 -04001766 found_key.offset - 1,
1767 BTRFS_BLOCK_GROUP_AVAIL);
1768 }
Chris Mason9078a3e2007-04-26 16:46:15 -04001769 if (key.objectid >=
Chris Mason4b52dff2007-06-26 10:06:50 -04001770 btrfs_super_total_blocks(&info->super_copy))
Chris Mason9078a3e2007-04-26 16:46:15 -04001771 break;
1772 }
1773
1774 btrfs_free_path(path);
1775 return 0;
1776}