blob: f261a8326cdf6825bdf2fab461ca43f894ecdca8 [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 finish_current_insert(struct btrfs_trans_handle *trans, struct
26 btrfs_root *extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -040027static int del_pending_extents(struct btrfs_trans_handle *trans, struct
28 btrfs_root *extent_root);
Chris Masonfec577f2007-02-26 10:40:21 -050029
Chris Masone37c9e62007-05-09 20:13:14 -040030static int cache_block_group(struct btrfs_root *root,
31 struct btrfs_block_group_cache *block_group)
32{
33 struct btrfs_path *path;
34 int ret;
35 struct btrfs_key key;
36 struct btrfs_leaf *leaf;
37 struct radix_tree_root *extent_radix;
38 int slot;
39 u64 i;
40 u64 last = 0;
41 u64 hole_size;
Yan7d7d6062007-09-14 16:15:28 -040042 u64 first_free;
Chris Masone37c9e62007-05-09 20:13:14 -040043 int found = 0;
44
45 root = root->fs_info->extent_root;
46 extent_radix = &root->fs_info->extent_map_radix;
47
48 if (block_group->cached)
49 return 0;
50 if (block_group->data)
51 return 0;
52 path = btrfs_alloc_path();
53 if (!path)
54 return -ENOMEM;
Yan7d7d6062007-09-14 16:15:28 -040055
Chris Mason2cc58cf2007-08-27 16:49:44 -040056 path->reada = 2;
Yan7d7d6062007-09-14 16:15:28 -040057 first_free = block_group->key.objectid;
Chris Masone37c9e62007-05-09 20:13:14 -040058 key.objectid = block_group->key.objectid;
59 key.flags = 0;
60 key.offset = 0;
Yan7d7d6062007-09-14 16:15:28 -040061
Chris Masone37c9e62007-05-09 20:13:14 -040062 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
63 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Yan7d7d6062007-09-14 16:15:28 -040064
Chris Masone37c9e62007-05-09 20:13:14 -040065 if (ret < 0)
66 return ret;
Yan7d7d6062007-09-14 16:15:28 -040067
Chris Masone37c9e62007-05-09 20:13:14 -040068 if (ret && path->slots[0] > 0)
69 path->slots[0]--;
Yan7d7d6062007-09-14 16:15:28 -040070
Chris Masone37c9e62007-05-09 20:13:14 -040071 while(1) {
72 leaf = btrfs_buffer_leaf(path->nodes[0]);
73 slot = path->slots[0];
74 if (slot >= btrfs_header_nritems(&leaf->header)) {
75 ret = btrfs_next_leaf(root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -040076 if (ret < 0)
77 goto err;
Chris Masonde428b62007-05-18 13:28:27 -040078 if (ret == 0) {
Chris Masone37c9e62007-05-09 20:13:14 -040079 continue;
Chris Masonde428b62007-05-18 13:28:27 -040080 } else {
Chris Masone37c9e62007-05-09 20:13:14 -040081 break;
82 }
83 }
Yan7d7d6062007-09-14 16:15:28 -040084
Chris Masone37c9e62007-05-09 20:13:14 -040085 btrfs_disk_key_to_cpu(&key, &leaf->items[slot].key);
Yan7d7d6062007-09-14 16:15:28 -040086 if (key.objectid < block_group->key.objectid) {
87 if (key.objectid + key.offset > first_free)
88 first_free = key.objectid + key.offset;
89 goto next;
90 }
91
Chris Masone37c9e62007-05-09 20:13:14 -040092 if (key.objectid >= block_group->key.objectid +
93 block_group->key.offset) {
Yan7d7d6062007-09-14 16:15:28 -040094 break;
95 }
96
97 if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) {
98 if (!found) {
99 last = first_free;
100 found = 1;
Chris Masone37c9e62007-05-09 20:13:14 -0400101 }
Yan7d7d6062007-09-14 16:15:28 -0400102 hole_size = key.objectid - last;
Chris Masone37c9e62007-05-09 20:13:14 -0400103 for (i = 0; i < hole_size; i++) {
104 set_radix_bit(extent_radix, last + i);
105 }
Yan7d7d6062007-09-14 16:15:28 -0400106 last = key.objectid + key.offset;
Chris Masone37c9e62007-05-09 20:13:14 -0400107 }
Yan7d7d6062007-09-14 16:15:28 -0400108next:
Chris Masone37c9e62007-05-09 20:13:14 -0400109 path->slots[0]++;
110 }
111
Yan7d7d6062007-09-14 16:15:28 -0400112 if (!found)
113 last = first_free;
114 if (block_group->key.objectid +
115 block_group->key.offset > last) {
116 hole_size = block_group->key.objectid +
117 block_group->key.offset - last;
118 for (i = 0; i < hole_size; i++) {
119 set_radix_bit(extent_radix,
120 last + i);
121 }
122 }
Chris Masone37c9e62007-05-09 20:13:14 -0400123 block_group->cached = 1;
Chris Mason54aa1f42007-06-22 14:16:25 -0400124err:
Chris Masone37c9e62007-05-09 20:13:14 -0400125 btrfs_free_path(path);
126 return 0;
127}
128
Chris Mason5276aed2007-06-11 21:33:38 -0400129struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
130 btrfs_fs_info *info,
131 u64 blocknr)
Chris Masonbe744172007-05-06 10:15:01 -0400132{
133 struct btrfs_block_group_cache *block_group;
134 int ret;
135
136 ret = radix_tree_gang_lookup(&info->block_group_radix,
137 (void **)&block_group,
138 blocknr, 1);
139 if (ret) {
Chris Mason3e1ad542007-05-07 20:03:49 -0400140 if (block_group->key.objectid <= blocknr && blocknr <=
Chris Masonbe744172007-05-06 10:15:01 -0400141 block_group->key.objectid + block_group->key.offset)
142 return block_group;
143 }
144 ret = radix_tree_gang_lookup(&info->block_group_data_radix,
145 (void **)&block_group,
146 blocknr, 1);
147 if (ret) {
Chris Mason3e1ad542007-05-07 20:03:49 -0400148 if (block_group->key.objectid <= blocknr && blocknr <=
Chris Masonbe744172007-05-06 10:15:01 -0400149 block_group->key.objectid + block_group->key.offset)
150 return block_group;
151 }
Chris Masonbe744172007-05-06 10:15:01 -0400152 return NULL;
153}
154
Chris Masone37c9e62007-05-09 20:13:14 -0400155static u64 leaf_range(struct btrfs_root *root)
156{
157 u64 size = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason84f54cf2007-06-12 07:43:08 -0400158 do_div(size, sizeof(struct btrfs_extent_item) +
159 sizeof(struct btrfs_item));
Chris Masone37c9e62007-05-09 20:13:14 -0400160 return size;
161}
162
163static u64 find_search_start(struct btrfs_root *root,
164 struct btrfs_block_group_cache **cache_ret,
165 u64 search_start, int num)
166{
167 unsigned long gang[8];
168 int ret;
169 struct btrfs_block_group_cache *cache = *cache_ret;
170 u64 last = max(search_start, cache->key.objectid);
171
172 if (cache->data)
173 goto out;
Chris Masone37c9e62007-05-09 20:13:14 -0400174again:
Chris Mason54aa1f42007-06-22 14:16:25 -0400175 ret = cache_block_group(root, cache);
176 if (ret)
177 goto out;
Chris Masone37c9e62007-05-09 20:13:14 -0400178 while(1) {
179 ret = find_first_radix_bit(&root->fs_info->extent_map_radix,
180 gang, last, ARRAY_SIZE(gang));
181 if (!ret)
182 goto out;
183 last = gang[ret-1] + 1;
184 if (num > 1) {
185 if (ret != ARRAY_SIZE(gang)) {
186 goto new_group;
187 }
188 if (gang[ret-1] - gang[0] > leaf_range(root)) {
189 continue;
190 }
191 }
192 if (gang[0] >= cache->key.objectid + cache->key.offset) {
193 goto new_group;
194 }
195 return gang[0];
196 }
197out:
198 return max(cache->last_alloc, search_start);
199
200new_group:
Chris Mason5276aed2007-06-11 21:33:38 -0400201 cache = btrfs_lookup_block_group(root->fs_info,
202 last + cache->key.offset - 1);
Chris Masone37c9e62007-05-09 20:13:14 -0400203 if (!cache) {
204 return max((*cache_ret)->last_alloc, search_start);
205 }
206 cache = btrfs_find_block_group(root, cache,
Chris Masonde428b62007-05-18 13:28:27 -0400207 last + cache->key.offset - 1, 0, 0);
Chris Masone37c9e62007-05-09 20:13:14 -0400208 *cache_ret = cache;
209 goto again;
210}
211
Chris Mason84f54cf2007-06-12 07:43:08 -0400212static u64 div_factor(u64 num, int factor)
213{
214 num *= factor;
215 do_div(num, 10);
216 return num;
217}
218
Chris Mason31f3c992007-04-30 15:25:45 -0400219struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
220 struct btrfs_block_group_cache
Chris Masonbe744172007-05-06 10:15:01 -0400221 *hint, u64 search_start,
Chris Masonde428b62007-05-18 13:28:27 -0400222 int data, int owner)
Chris Masoncd1bc462007-04-27 10:08:34 -0400223{
224 struct btrfs_block_group_cache *cache[8];
Chris Mason31f3c992007-04-30 15:25:45 -0400225 struct btrfs_block_group_cache *found_group = NULL;
Chris Masoncd1bc462007-04-27 10:08:34 -0400226 struct btrfs_fs_info *info = root->fs_info;
Chris Masonbe744172007-05-06 10:15:01 -0400227 struct radix_tree_root *radix;
Chris Mason1e2677e2007-05-29 16:52:18 -0400228 struct radix_tree_root *swap_radix;
Chris Masoncd1bc462007-04-27 10:08:34 -0400229 u64 used;
Chris Mason31f3c992007-04-30 15:25:45 -0400230 u64 last = 0;
231 u64 hint_last;
Chris Masoncd1bc462007-04-27 10:08:34 -0400232 int i;
233 int ret;
Chris Mason31f3c992007-04-30 15:25:45 -0400234 int full_search = 0;
Chris Masonde428b62007-05-18 13:28:27 -0400235 int factor = 8;
Chris Mason1e2677e2007-05-29 16:52:18 -0400236 int data_swap = 0;
Chris Masonde428b62007-05-18 13:28:27 -0400237
238 if (!owner)
239 factor = 5;
Chris Masonbe744172007-05-06 10:15:01 -0400240
Chris Mason1e2677e2007-05-29 16:52:18 -0400241 if (data) {
Chris Masonbe744172007-05-06 10:15:01 -0400242 radix = &info->block_group_data_radix;
Chris Mason1e2677e2007-05-29 16:52:18 -0400243 swap_radix = &info->block_group_radix;
244 } else {
Chris Masonbe744172007-05-06 10:15:01 -0400245 radix = &info->block_group_radix;
Chris Mason1e2677e2007-05-29 16:52:18 -0400246 swap_radix = &info->block_group_data_radix;
247 }
Chris Masonbe744172007-05-06 10:15:01 -0400248
249 if (search_start) {
250 struct btrfs_block_group_cache *shint;
Chris Mason5276aed2007-06-11 21:33:38 -0400251 shint = btrfs_lookup_block_group(info, search_start);
Yane9fe3952007-08-29 09:11:44 -0400252 if (shint && shint->data == data) {
Chris Masonbe744172007-05-06 10:15:01 -0400253 used = btrfs_block_group_used(&shint->item);
254 if (used + shint->pinned <
Chris Mason84f54cf2007-06-12 07:43:08 -0400255 div_factor(shint->key.offset, factor)) {
Chris Masonbe744172007-05-06 10:15:01 -0400256 return shint;
257 }
258 }
259 }
260 if (hint && hint->data == data) {
Chris Mason31f3c992007-04-30 15:25:45 -0400261 used = btrfs_block_group_used(&hint->item);
Chris Mason84f54cf2007-06-12 07:43:08 -0400262 if (used + hint->pinned <
263 div_factor(hint->key.offset, factor)) {
Chris Mason31f3c992007-04-30 15:25:45 -0400264 return hint;
265 }
Chris Mason84f54cf2007-06-12 07:43:08 -0400266 if (used >= div_factor(hint->key.offset, 8)) {
Chris Masonbe744172007-05-06 10:15:01 -0400267 radix_tree_tag_clear(radix,
268 hint->key.objectid +
269 hint->key.offset - 1,
270 BTRFS_BLOCK_GROUP_AVAIL);
271 }
Chris Mason8d7be552007-05-10 11:24:42 -0400272 last = hint->key.offset * 3;
Chris Masonbe744172007-05-06 10:15:01 -0400273 if (hint->key.objectid >= last)
Chris Masone37c9e62007-05-09 20:13:14 -0400274 last = max(search_start + hint->key.offset - 1,
275 hint->key.objectid - last);
Chris Masonbe744172007-05-06 10:15:01 -0400276 else
277 last = hint->key.objectid + hint->key.offset;
Chris Mason31f3c992007-04-30 15:25:45 -0400278 hint_last = last;
279 } else {
Chris Masone37c9e62007-05-09 20:13:14 -0400280 if (hint)
281 hint_last = max(hint->key.objectid, search_start);
282 else
283 hint_last = search_start;
284
285 last = hint_last;
Chris Mason31f3c992007-04-30 15:25:45 -0400286 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400287 while(1) {
Chris Masonbe744172007-05-06 10:15:01 -0400288 ret = radix_tree_gang_lookup_tag(radix, (void **)cache,
Chris Masoncd1bc462007-04-27 10:08:34 -0400289 last, ARRAY_SIZE(cache),
Chris Mason31f3c992007-04-30 15:25:45 -0400290 BTRFS_BLOCK_GROUP_AVAIL);
Chris Masoncd1bc462007-04-27 10:08:34 -0400291 if (!ret)
292 break;
293 for (i = 0; i < ret; i++) {
Chris Masonbe08c1b2007-05-03 09:06:49 -0400294 last = cache[i]->key.objectid +
295 cache[i]->key.offset;
Chris Masoncd1bc462007-04-27 10:08:34 -0400296 used = btrfs_block_group_used(&cache[i]->item);
Chris Masonbe744172007-05-06 10:15:01 -0400297 if (used + cache[i]->pinned <
Chris Mason84f54cf2007-06-12 07:43:08 -0400298 div_factor(cache[i]->key.offset, factor)) {
Chris Mason31f3c992007-04-30 15:25:45 -0400299 found_group = cache[i];
300 goto found;
Chris Masoncd1bc462007-04-27 10:08:34 -0400301 }
Chris Mason84f54cf2007-06-12 07:43:08 -0400302 if (used >= div_factor(cache[i]->key.offset, 8)) {
Chris Masonbe744172007-05-06 10:15:01 -0400303 radix_tree_tag_clear(radix,
304 cache[i]->key.objectid +
305 cache[i]->key.offset - 1,
306 BTRFS_BLOCK_GROUP_AVAIL);
307 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400308 }
Chris Masonde428b62007-05-18 13:28:27 -0400309 cond_resched();
Chris Masoncd1bc462007-04-27 10:08:34 -0400310 }
Chris Mason31f3c992007-04-30 15:25:45 -0400311 last = hint_last;
312again:
Chris Masoncd1bc462007-04-27 10:08:34 -0400313 while(1) {
Chris Masonbe744172007-05-06 10:15:01 -0400314 ret = radix_tree_gang_lookup(radix, (void **)cache,
315 last, ARRAY_SIZE(cache));
Chris Masoncd1bc462007-04-27 10:08:34 -0400316 if (!ret)
317 break;
318 for (i = 0; i < ret; i++) {
Chris Masonbe08c1b2007-05-03 09:06:49 -0400319 last = cache[i]->key.objectid +
320 cache[i]->key.offset;
Chris Masoncd1bc462007-04-27 10:08:34 -0400321 used = btrfs_block_group_used(&cache[i]->item);
Chris Masonbe744172007-05-06 10:15:01 -0400322 if (used + cache[i]->pinned < cache[i]->key.offset) {
Chris Mason31f3c992007-04-30 15:25:45 -0400323 found_group = cache[i];
324 goto found;
Chris Masoncd1bc462007-04-27 10:08:34 -0400325 }
Chris Masonbe744172007-05-06 10:15:01 -0400326 if (used >= cache[i]->key.offset) {
327 radix_tree_tag_clear(radix,
328 cache[i]->key.objectid +
329 cache[i]->key.offset - 1,
330 BTRFS_BLOCK_GROUP_AVAIL);
331 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400332 }
Chris Masonde428b62007-05-18 13:28:27 -0400333 cond_resched();
Chris Masoncd1bc462007-04-27 10:08:34 -0400334 }
Chris Mason31f3c992007-04-30 15:25:45 -0400335 if (!full_search) {
Chris Masonbe744172007-05-06 10:15:01 -0400336 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -0400337 full_search = 1;
338 goto again;
339 }
Chris Mason1e2677e2007-05-29 16:52:18 -0400340 if (!data_swap) {
341 struct radix_tree_root *tmp = radix;
342 data_swap = 1;
343 radix = swap_radix;
344 swap_radix = tmp;
345 last = search_start;
346 goto again;
347 }
Chris Mason31f3c992007-04-30 15:25:45 -0400348 if (!found_group) {
Chris Masonbe744172007-05-06 10:15:01 -0400349 ret = radix_tree_gang_lookup(radix,
Chris Mason31f3c992007-04-30 15:25:45 -0400350 (void **)&found_group, 0, 1);
Chris Mason1e2677e2007-05-29 16:52:18 -0400351 if (ret == 0) {
352 ret = radix_tree_gang_lookup(swap_radix,
353 (void **)&found_group,
354 0, 1);
355 }
Chris Mason31f3c992007-04-30 15:25:45 -0400356 BUG_ON(ret != 1);
357 }
Chris Masonbe744172007-05-06 10:15:01 -0400358found:
Chris Mason31f3c992007-04-30 15:25:45 -0400359 return found_group;
Chris Masoncd1bc462007-04-27 10:08:34 -0400360}
361
Chris Masonb18c6682007-04-17 13:26:50 -0400362int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
363 struct btrfs_root *root,
364 u64 blocknr, u64 num_blocks)
Chris Mason02217ed2007-03-02 16:08:05 -0500365{
Chris Mason5caf2a02007-04-02 11:20:42 -0400366 struct btrfs_path *path;
Chris Mason02217ed2007-03-02 16:08:05 -0500367 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400368 struct btrfs_key key;
Chris Mason234b63a2007-03-13 10:46:10 -0400369 struct btrfs_leaf *l;
370 struct btrfs_extent_item *item;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400371 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500372
Chris Mason5caf2a02007-04-02 11:20:42 -0400373 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -0400374 if (!path)
375 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -0400376
Chris Mason02217ed2007-03-02 16:08:05 -0500377 key.objectid = blocknr;
378 key.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -0400379 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Mason6407bf62007-03-27 06:33:00 -0400380 key.offset = num_blocks;
Chris Mason5caf2a02007-04-02 11:20:42 -0400381 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -0400382 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400383 if (ret < 0)
384 return ret;
Chris Masona429e512007-04-18 16:15:28 -0400385 if (ret != 0) {
Chris Masona28ec192007-03-06 20:08:01 -0500386 BUG();
Chris Masona429e512007-04-18 16:15:28 -0400387 }
Chris Mason02217ed2007-03-02 16:08:05 -0500388 BUG_ON(ret != 0);
Chris Mason5caf2a02007-04-02 11:20:42 -0400389 l = btrfs_buffer_leaf(path->nodes[0]);
390 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400391 refs = btrfs_extent_refs(item);
392 btrfs_set_extent_refs(item, refs + 1);
Chris Mason5caf2a02007-04-02 11:20:42 -0400393 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masona28ec192007-03-06 20:08:01 -0500394
Chris Mason5caf2a02007-04-02 11:20:42 -0400395 btrfs_release_path(root->fs_info->extent_root, path);
396 btrfs_free_path(path);
Chris Mason9f5fae22007-03-20 14:38:32 -0400397 finish_current_insert(trans, root->fs_info->extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -0400398 del_pending_extents(trans, root->fs_info->extent_root);
Chris Mason02217ed2007-03-02 16:08:05 -0500399 return 0;
400}
401
Chris Masone9d0b132007-08-10 14:06:19 -0400402int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
403 struct btrfs_root *root)
404{
405 finish_current_insert(trans, root->fs_info->extent_root);
406 del_pending_extents(trans, root->fs_info->extent_root);
407 return 0;
408}
409
Chris Masonb18c6682007-04-17 13:26:50 -0400410static int lookup_extent_ref(struct btrfs_trans_handle *trans,
411 struct btrfs_root *root, u64 blocknr,
412 u64 num_blocks, u32 *refs)
Chris Masona28ec192007-03-06 20:08:01 -0500413{
Chris Mason5caf2a02007-04-02 11:20:42 -0400414 struct btrfs_path *path;
Chris Masona28ec192007-03-06 20:08:01 -0500415 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400416 struct btrfs_key key;
Chris Mason234b63a2007-03-13 10:46:10 -0400417 struct btrfs_leaf *l;
418 struct btrfs_extent_item *item;
Chris Mason5caf2a02007-04-02 11:20:42 -0400419
420 path = btrfs_alloc_path();
Chris Masona28ec192007-03-06 20:08:01 -0500421 key.objectid = blocknr;
Chris Mason6407bf62007-03-27 06:33:00 -0400422 key.offset = num_blocks;
Chris Mason62e27492007-03-15 12:56:47 -0400423 key.flags = 0;
424 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Mason5caf2a02007-04-02 11:20:42 -0400425 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -0400426 0, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400427 if (ret < 0)
428 goto out;
Chris Masona28ec192007-03-06 20:08:01 -0500429 if (ret != 0)
430 BUG();
Chris Mason5caf2a02007-04-02 11:20:42 -0400431 l = btrfs_buffer_leaf(path->nodes[0]);
432 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400433 *refs = btrfs_extent_refs(item);
Chris Mason54aa1f42007-06-22 14:16:25 -0400434out:
Chris Mason5caf2a02007-04-02 11:20:42 -0400435 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -0500436 return 0;
437}
438
Chris Masonc5739bb2007-04-10 09:27:04 -0400439int btrfs_inc_root_ref(struct btrfs_trans_handle *trans,
440 struct btrfs_root *root)
441{
Chris Masonb18c6682007-04-17 13:26:50 -0400442 return btrfs_inc_extent_ref(trans, root, bh_blocknr(root->node), 1);
Chris Masonc5739bb2007-04-10 09:27:04 -0400443}
444
Chris Masone089f052007-03-16 16:20:31 -0400445int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Masone20d96d2007-03-22 12:13:20 -0400446 struct buffer_head *buf)
Chris Mason02217ed2007-03-02 16:08:05 -0500447{
448 u64 blocknr;
Chris Masone20d96d2007-03-22 12:13:20 -0400449 struct btrfs_node *buf_node;
Chris Mason6407bf62007-03-27 06:33:00 -0400450 struct btrfs_leaf *buf_leaf;
451 struct btrfs_disk_key *key;
452 struct btrfs_file_extent_item *fi;
Chris Mason02217ed2007-03-02 16:08:05 -0500453 int i;
Chris Mason6407bf62007-03-27 06:33:00 -0400454 int leaf;
455 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -0400456 int faili;
457 int err;
Chris Masona28ec192007-03-06 20:08:01 -0500458
Chris Mason3768f362007-03-13 16:47:54 -0400459 if (!root->ref_cows)
Chris Masona28ec192007-03-06 20:08:01 -0500460 return 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400461 buf_node = btrfs_buffer_node(buf);
Chris Mason6407bf62007-03-27 06:33:00 -0400462 leaf = btrfs_is_leaf(buf_node);
463 buf_leaf = btrfs_buffer_leaf(buf);
Chris Masone20d96d2007-03-22 12:13:20 -0400464 for (i = 0; i < btrfs_header_nritems(&buf_node->header); i++) {
Chris Mason6407bf62007-03-27 06:33:00 -0400465 if (leaf) {
Chris Mason3a686372007-05-24 13:35:57 -0400466 u64 disk_blocknr;
Chris Mason6407bf62007-03-27 06:33:00 -0400467 key = &buf_leaf->items[i].key;
468 if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY)
469 continue;
470 fi = btrfs_item_ptr(buf_leaf, i,
471 struct btrfs_file_extent_item);
Chris Mason236454d2007-04-19 13:37:44 -0400472 if (btrfs_file_extent_type(fi) ==
473 BTRFS_FILE_EXTENT_INLINE)
474 continue;
Chris Mason3a686372007-05-24 13:35:57 -0400475 disk_blocknr = btrfs_file_extent_disk_blocknr(fi);
476 if (disk_blocknr == 0)
477 continue;
478 ret = btrfs_inc_extent_ref(trans, root, disk_blocknr,
Chris Mason6407bf62007-03-27 06:33:00 -0400479 btrfs_file_extent_disk_num_blocks(fi));
Chris Mason54aa1f42007-06-22 14:16:25 -0400480 if (ret) {
481 faili = i;
482 goto fail;
483 }
Chris Mason6407bf62007-03-27 06:33:00 -0400484 } else {
485 blocknr = btrfs_node_blockptr(buf_node, i);
Chris Masonb18c6682007-04-17 13:26:50 -0400486 ret = btrfs_inc_extent_ref(trans, root, blocknr, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400487 if (ret) {
488 faili = i;
489 goto fail;
490 }
Chris Mason6407bf62007-03-27 06:33:00 -0400491 }
Chris Mason02217ed2007-03-02 16:08:05 -0500492 }
493 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -0400494fail:
Chris Masonccd467d2007-06-28 15:57:36 -0400495 WARN_ON(1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400496 for (i =0; i < faili; i++) {
497 if (leaf) {
498 u64 disk_blocknr;
499 key = &buf_leaf->items[i].key;
500 if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY)
501 continue;
502 fi = btrfs_item_ptr(buf_leaf, i,
503 struct btrfs_file_extent_item);
504 if (btrfs_file_extent_type(fi) ==
505 BTRFS_FILE_EXTENT_INLINE)
506 continue;
507 disk_blocknr = btrfs_file_extent_disk_blocknr(fi);
508 if (disk_blocknr == 0)
509 continue;
510 err = btrfs_free_extent(trans, root, disk_blocknr,
511 btrfs_file_extent_disk_num_blocks(fi), 0);
512 BUG_ON(err);
513 } else {
514 blocknr = btrfs_node_blockptr(buf_node, i);
515 err = btrfs_free_extent(trans, root, blocknr, 1, 0);
516 BUG_ON(err);
517 }
518 }
519 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -0500520}
521
Chris Mason9078a3e2007-04-26 16:46:15 -0400522static int write_one_cache_group(struct btrfs_trans_handle *trans,
523 struct btrfs_root *root,
524 struct btrfs_path *path,
525 struct btrfs_block_group_cache *cache)
526{
527 int ret;
528 int pending_ret;
529 struct btrfs_root *extent_root = root->fs_info->extent_root;
530 struct btrfs_block_group_item *bi;
Chris Mason9078a3e2007-04-26 16:46:15 -0400531
Chris Mason9078a3e2007-04-26 16:46:15 -0400532 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400533 if (ret < 0)
534 goto fail;
Chris Mason9078a3e2007-04-26 16:46:15 -0400535 BUG_ON(ret);
536 bi = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
537 struct btrfs_block_group_item);
538 memcpy(bi, &cache->item, sizeof(*bi));
Chris Masonccd467d2007-06-28 15:57:36 -0400539 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason9078a3e2007-04-26 16:46:15 -0400540 btrfs_release_path(extent_root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -0400541fail:
Chris Mason9078a3e2007-04-26 16:46:15 -0400542 finish_current_insert(trans, extent_root);
543 pending_ret = del_pending_extents(trans, extent_root);
544 if (ret)
545 return ret;
546 if (pending_ret)
547 return pending_ret;
Chris Masonbe744172007-05-06 10:15:01 -0400548 if (cache->data)
549 cache->last_alloc = cache->first_free;
Chris Mason9078a3e2007-04-26 16:46:15 -0400550 return 0;
551
552}
553
Chris Masonbe744172007-05-06 10:15:01 -0400554static int write_dirty_block_radix(struct btrfs_trans_handle *trans,
555 struct btrfs_root *root,
556 struct radix_tree_root *radix)
Chris Mason9078a3e2007-04-26 16:46:15 -0400557{
558 struct btrfs_block_group_cache *cache[8];
559 int ret;
560 int err = 0;
561 int werr = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -0400562 int i;
563 struct btrfs_path *path;
Chris Mason54aa1f42007-06-22 14:16:25 -0400564 unsigned long off = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -0400565
566 path = btrfs_alloc_path();
567 if (!path)
568 return -ENOMEM;
569
570 while(1) {
571 ret = radix_tree_gang_lookup_tag(radix, (void **)cache,
Chris Mason54aa1f42007-06-22 14:16:25 -0400572 off, ARRAY_SIZE(cache),
Chris Mason9078a3e2007-04-26 16:46:15 -0400573 BTRFS_BLOCK_GROUP_DIRTY);
574 if (!ret)
575 break;
576 for (i = 0; i < ret; i++) {
Chris Mason54aa1f42007-06-22 14:16:25 -0400577 err = write_one_cache_group(trans, root,
578 path, cache[i]);
579 /*
580 * if we fail to write the cache group, we want
581 * to keep it marked dirty in hopes that a later
582 * write will work
583 */
584 if (err) {
585 werr = err;
586 off = cache[i]->key.objectid +
587 cache[i]->key.offset;
588 continue;
589 }
590
Chris Mason9078a3e2007-04-26 16:46:15 -0400591 radix_tree_tag_clear(radix, cache[i]->key.objectid +
592 cache[i]->key.offset - 1,
593 BTRFS_BLOCK_GROUP_DIRTY);
Chris Mason9078a3e2007-04-26 16:46:15 -0400594 }
595 }
596 btrfs_free_path(path);
597 return werr;
598}
599
Chris Masonbe744172007-05-06 10:15:01 -0400600int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
601 struct btrfs_root *root)
602{
603 int ret;
604 int ret2;
605 ret = write_dirty_block_radix(trans, root,
606 &root->fs_info->block_group_radix);
607 ret2 = write_dirty_block_radix(trans, root,
608 &root->fs_info->block_group_data_radix);
609 if (ret)
610 return ret;
611 if (ret2)
612 return ret2;
613 return 0;
614}
615
Chris Mason9078a3e2007-04-26 16:46:15 -0400616static int update_block_group(struct btrfs_trans_handle *trans,
617 struct btrfs_root *root,
Chris Mason1e2677e2007-05-29 16:52:18 -0400618 u64 blocknr, u64 num, int alloc, int mark_free,
619 int data)
Chris Mason9078a3e2007-04-26 16:46:15 -0400620{
621 struct btrfs_block_group_cache *cache;
622 struct btrfs_fs_info *info = root->fs_info;
623 u64 total = num;
624 u64 old_val;
625 u64 block_in_group;
Chris Masone37c9e62007-05-09 20:13:14 -0400626 u64 i;
Chris Mason1e2677e2007-05-29 16:52:18 -0400627 int ret;
Chris Mason3e1ad542007-05-07 20:03:49 -0400628
Chris Mason9078a3e2007-04-26 16:46:15 -0400629 while(total) {
Chris Mason5276aed2007-06-11 21:33:38 -0400630 cache = btrfs_lookup_block_group(info, blocknr);
Chris Mason3e1ad542007-05-07 20:03:49 -0400631 if (!cache) {
Chris Mason9078a3e2007-04-26 16:46:15 -0400632 return -1;
Chris Masoncd1bc462007-04-27 10:08:34 -0400633 }
Chris Mason9078a3e2007-04-26 16:46:15 -0400634 block_in_group = blocknr - cache->key.objectid;
635 WARN_ON(block_in_group > cache->key.offset);
Chris Mason3e1ad542007-05-07 20:03:49 -0400636 radix_tree_tag_set(cache->radix, cache->key.objectid +
Chris Masonbe744172007-05-06 10:15:01 -0400637 cache->key.offset - 1,
Chris Mason9078a3e2007-04-26 16:46:15 -0400638 BTRFS_BLOCK_GROUP_DIRTY);
639
640 old_val = btrfs_block_group_used(&cache->item);
641 num = min(total, cache->key.offset - block_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -0400642 if (alloc) {
Chris Masoncd1bc462007-04-27 10:08:34 -0400643 if (blocknr > cache->last_alloc)
644 cache->last_alloc = blocknr;
Chris Masone37c9e62007-05-09 20:13:14 -0400645 if (!cache->data) {
646 for (i = 0; i < num; i++) {
647 clear_radix_bit(&info->extent_map_radix,
648 blocknr + i);
649 }
650 }
Chris Mason1e2677e2007-05-29 16:52:18 -0400651 if (cache->data != data &&
Chris Mason84f54cf2007-06-12 07:43:08 -0400652 old_val < (cache->key.offset >> 1)) {
Chris Mason1e2677e2007-05-29 16:52:18 -0400653 cache->data = data;
654 radix_tree_delete(cache->radix,
655 cache->key.objectid +
656 cache->key.offset - 1);
657
658 if (data) {
659 cache->radix =
660 &info->block_group_data_radix;
661 cache->item.flags |=
662 BTRFS_BLOCK_GROUP_DATA;
663 } else {
664 cache->radix = &info->block_group_radix;
665 cache->item.flags &=
666 ~BTRFS_BLOCK_GROUP_DATA;
667 }
668 ret = radix_tree_insert(cache->radix,
669 cache->key.objectid +
670 cache->key.offset - 1,
671 (void *)cache);
672 }
673 old_val += num;
Chris Masoncd1bc462007-04-27 10:08:34 -0400674 } else {
Chris Mason9078a3e2007-04-26 16:46:15 -0400675 old_val -= num;
Chris Masoncd1bc462007-04-27 10:08:34 -0400676 if (blocknr < cache->first_free)
677 cache->first_free = blocknr;
Chris Masone37c9e62007-05-09 20:13:14 -0400678 if (!cache->data && mark_free) {
679 for (i = 0; i < num; i++) {
680 set_radix_bit(&info->extent_map_radix,
681 blocknr + i);
682 }
683 }
Chris Mason84f54cf2007-06-12 07:43:08 -0400684 if (old_val < (cache->key.offset >> 1) &&
685 old_val + num >= (cache->key.offset >> 1)) {
Chris Masone37c9e62007-05-09 20:13:14 -0400686 radix_tree_tag_set(cache->radix,
687 cache->key.objectid +
688 cache->key.offset - 1,
689 BTRFS_BLOCK_GROUP_AVAIL);
690 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400691 }
Chris Mason9078a3e2007-04-26 16:46:15 -0400692 btrfs_set_block_group_used(&cache->item, old_val);
Chris Masone37c9e62007-05-09 20:13:14 -0400693 total -= num;
694 blocknr += num;
Chris Mason9078a3e2007-04-26 16:46:15 -0400695 }
696 return 0;
697}
698
Chris Masonccd467d2007-06-28 15:57:36 -0400699int btrfs_copy_pinned(struct btrfs_root *root, struct radix_tree_root *copy)
700{
701 unsigned long gang[8];
702 u64 last = 0;
703 struct radix_tree_root *pinned_radix = &root->fs_info->pinned_radix;
704 int ret;
705 int i;
706
707 while(1) {
708 ret = find_first_radix_bit(pinned_radix, gang, last,
709 ARRAY_SIZE(gang));
710 if (!ret)
711 break;
712 for (i = 0 ; i < ret; i++) {
713 set_radix_bit(copy, gang[i]);
714 last = gang[i] + 1;
715 }
716 }
Chris Mason26b80032007-08-08 20:17:12 -0400717 ret = find_first_radix_bit(&root->fs_info->extent_ins_radix, gang, 0,
718 ARRAY_SIZE(gang));
719 WARN_ON(ret);
Chris Masonccd467d2007-06-28 15:57:36 -0400720 return 0;
721}
722
723int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
724 struct btrfs_root *root,
725 struct radix_tree_root *unpin_radix)
Chris Masona28ec192007-03-06 20:08:01 -0500726{
Chris Mason8ef97622007-03-26 10:15:30 -0400727 unsigned long gang[8];
Chris Masonbe744172007-05-06 10:15:01 -0400728 struct btrfs_block_group_cache *block_group;
Chris Mason88fd1462007-03-16 08:56:18 -0400729 u64 first = 0;
Chris Masona28ec192007-03-06 20:08:01 -0500730 int ret;
731 int i;
Chris Mason8ef97622007-03-26 10:15:30 -0400732 struct radix_tree_root *pinned_radix = &root->fs_info->pinned_radix;
Chris Masone37c9e62007-05-09 20:13:14 -0400733 struct radix_tree_root *extent_radix = &root->fs_info->extent_map_radix;
Chris Masona28ec192007-03-06 20:08:01 -0500734
735 while(1) {
Chris Masonccd467d2007-06-28 15:57:36 -0400736 ret = find_first_radix_bit(unpin_radix, gang, 0,
Chris Mason8ef97622007-03-26 10:15:30 -0400737 ARRAY_SIZE(gang));
Chris Masona28ec192007-03-06 20:08:01 -0500738 if (!ret)
739 break;
Chris Mason88fd1462007-03-16 08:56:18 -0400740 if (!first)
Chris Mason8ef97622007-03-26 10:15:30 -0400741 first = gang[0];
Chris Mason0579da42007-03-07 16:15:30 -0500742 for (i = 0; i < ret; i++) {
Chris Mason8ef97622007-03-26 10:15:30 -0400743 clear_radix_bit(pinned_radix, gang[i]);
Chris Masonccd467d2007-06-28 15:57:36 -0400744 clear_radix_bit(unpin_radix, gang[i]);
Chris Mason5276aed2007-06-11 21:33:38 -0400745 block_group = btrfs_lookup_block_group(root->fs_info,
746 gang[i]);
Chris Masonbe744172007-05-06 10:15:01 -0400747 if (block_group) {
748 WARN_ON(block_group->pinned == 0);
749 block_group->pinned--;
750 if (gang[i] < block_group->last_alloc)
751 block_group->last_alloc = gang[i];
Chris Masone37c9e62007-05-09 20:13:14 -0400752 if (!block_group->data)
753 set_radix_bit(extent_radix, gang[i]);
Chris Masonbe744172007-05-06 10:15:01 -0400754 }
Chris Mason0579da42007-03-07 16:15:30 -0500755 }
Chris Masona28ec192007-03-06 20:08:01 -0500756 }
757 return 0;
758}
759
Chris Masone089f052007-03-16 16:20:31 -0400760static int finish_current_insert(struct btrfs_trans_handle *trans, struct
761 btrfs_root *extent_root)
Chris Mason037e6392007-03-07 11:50:24 -0500762{
Chris Masone2fa7222007-03-12 16:22:34 -0400763 struct btrfs_key ins;
Chris Mason234b63a2007-03-13 10:46:10 -0400764 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -0500765 int i;
766 int ret;
Chris Mason26b80032007-08-08 20:17:12 -0400767 int err;
768 unsigned long gang[8];
Chris Mason1261ec42007-03-20 20:35:03 -0400769 struct btrfs_fs_info *info = extent_root->fs_info;
Chris Mason037e6392007-03-07 11:50:24 -0500770
Chris Masoncf27e1e2007-03-13 09:49:06 -0400771 btrfs_set_extent_refs(&extent_item, 1);
Chris Mason037e6392007-03-07 11:50:24 -0500772 ins.offset = 1;
773 ins.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -0400774 btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
Chris Mason5d0c3e62007-04-23 17:01:05 -0400775 btrfs_set_extent_owner(&extent_item, extent_root->root_key.objectid);
Chris Mason037e6392007-03-07 11:50:24 -0500776
Chris Mason26b80032007-08-08 20:17:12 -0400777 while(1) {
778 ret = find_first_radix_bit(&info->extent_ins_radix, gang, 0,
779 ARRAY_SIZE(gang));
780 if (!ret)
781 break;
782
783 for (i = 0; i < ret; i++) {
784 ins.objectid = gang[i];
785 err = btrfs_insert_item(trans, extent_root, &ins,
786 &extent_item,
787 sizeof(extent_item));
788 clear_radix_bit(&info->extent_ins_radix, gang[i]);
789 WARN_ON(err);
790 }
Chris Mason037e6392007-03-07 11:50:24 -0500791 }
Chris Mason037e6392007-03-07 11:50:24 -0500792 return 0;
793}
794
Chris Mason8ef97622007-03-26 10:15:30 -0400795static int pin_down_block(struct btrfs_root *root, u64 blocknr, int pending)
Chris Masone20d96d2007-03-22 12:13:20 -0400796{
797 int err;
Chris Mason78fae272007-03-25 11:35:08 -0400798 struct btrfs_header *header;
Chris Mason8ef97622007-03-26 10:15:30 -0400799 struct buffer_head *bh;
Chris Mason78fae272007-03-25 11:35:08 -0400800
Chris Masonf4b9aa82007-03-27 11:05:53 -0400801 if (!pending) {
Chris Masond98237b2007-03-28 13:57:48 -0400802 bh = btrfs_find_tree_block(root, blocknr);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400803 if (bh) {
804 if (buffer_uptodate(bh)) {
805 u64 transid =
806 root->fs_info->running_transaction->transid;
807 header = btrfs_buffer_header(bh);
808 if (btrfs_header_generation(header) ==
809 transid) {
810 btrfs_block_release(root, bh);
811 return 0;
812 }
Chris Masonf4b9aa82007-03-27 11:05:53 -0400813 }
Chris Masond6025572007-03-30 14:27:56 -0400814 btrfs_block_release(root, bh);
Chris Mason8ef97622007-03-26 10:15:30 -0400815 }
Chris Mason8ef97622007-03-26 10:15:30 -0400816 err = set_radix_bit(&root->fs_info->pinned_radix, blocknr);
Chris Masonbe744172007-05-06 10:15:01 -0400817 if (!err) {
818 struct btrfs_block_group_cache *cache;
Chris Mason5276aed2007-06-11 21:33:38 -0400819 cache = btrfs_lookup_block_group(root->fs_info,
820 blocknr);
Chris Masonbe744172007-05-06 10:15:01 -0400821 if (cache)
822 cache->pinned++;
823 }
Chris Masonf4b9aa82007-03-27 11:05:53 -0400824 } else {
825 err = set_radix_bit(&root->fs_info->pending_del_radix, blocknr);
826 }
Chris Masonbe744172007-05-06 10:15:01 -0400827 BUG_ON(err < 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400828 return 0;
829}
830
Chris Masona28ec192007-03-06 20:08:01 -0500831/*
832 * remove an extent from the root, returns 0 on success
833 */
Chris Masone089f052007-03-16 16:20:31 -0400834static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masone37c9e62007-05-09 20:13:14 -0400835 *root, u64 blocknr, u64 num_blocks, int pin,
836 int mark_free)
Chris Masona28ec192007-03-06 20:08:01 -0500837{
Chris Mason5caf2a02007-04-02 11:20:42 -0400838 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -0400839 struct btrfs_key key;
Chris Mason1261ec42007-03-20 20:35:03 -0400840 struct btrfs_fs_info *info = root->fs_info;
841 struct btrfs_root *extent_root = info->extent_root;
Chris Masona28ec192007-03-06 20:08:01 -0500842 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400843 struct btrfs_extent_item *ei;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400844 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500845
Chris Masona28ec192007-03-06 20:08:01 -0500846 key.objectid = blocknr;
847 key.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -0400848 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masona28ec192007-03-06 20:08:01 -0500849 key.offset = num_blocks;
850
Chris Mason5caf2a02007-04-02 11:20:42 -0400851 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -0400852 if (!path)
853 return -ENOMEM;
854
Chris Mason5caf2a02007-04-02 11:20:42 -0400855 ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400856 if (ret < 0)
857 return ret;
858 BUG_ON(ret);
Chris Mason5caf2a02007-04-02 11:20:42 -0400859 ei = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
Chris Mason123abc82007-03-14 14:14:43 -0400860 struct btrfs_extent_item);
Chris Masona28ec192007-03-06 20:08:01 -0500861 BUG_ON(ei->refs == 0);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400862 refs = btrfs_extent_refs(ei) - 1;
863 btrfs_set_extent_refs(ei, refs);
Chris Mason5caf2a02007-04-02 11:20:42 -0400864 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400865 if (refs == 0) {
Josef Bacik58176a92007-08-29 15:47:34 -0400866 u64 super_blocks_used, root_blocks_used;
Chris Mason78fae272007-03-25 11:35:08 -0400867
868 if (pin) {
Chris Mason8ef97622007-03-26 10:15:30 -0400869 ret = pin_down_block(root, blocknr, 0);
Chris Mason78fae272007-03-25 11:35:08 -0400870 BUG_ON(ret);
871 }
872
Josef Bacik58176a92007-08-29 15:47:34 -0400873 /* block accounting for super block */
Chris Mason4b52dff2007-06-26 10:06:50 -0400874 super_blocks_used = btrfs_super_blocks_used(&info->super_copy);
875 btrfs_set_super_blocks_used(&info->super_copy,
Chris Mason1261ec42007-03-20 20:35:03 -0400876 super_blocks_used - num_blocks);
Josef Bacik58176a92007-08-29 15:47:34 -0400877
878 /* block accounting for root item */
879 root_blocks_used = btrfs_root_blocks_used(&root->root_item);
880 btrfs_set_root_blocks_used(&root->root_item,
881 root_blocks_used - num_blocks);
882
Chris Mason5caf2a02007-04-02 11:20:42 -0400883 ret = btrfs_del_item(trans, extent_root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -0400884 if (ret) {
885 return ret;
886 }
Chris Masone37c9e62007-05-09 20:13:14 -0400887 ret = update_block_group(trans, root, blocknr, num_blocks, 0,
Chris Mason1e2677e2007-05-29 16:52:18 -0400888 mark_free, 0);
Chris Mason9078a3e2007-04-26 16:46:15 -0400889 BUG_ON(ret);
Chris Masona28ec192007-03-06 20:08:01 -0500890 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400891 btrfs_free_path(path);
Chris Masone089f052007-03-16 16:20:31 -0400892 finish_current_insert(trans, extent_root);
Chris Masona28ec192007-03-06 20:08:01 -0500893 return ret;
894}
895
896/*
Chris Masonfec577f2007-02-26 10:40:21 -0500897 * find all the blocks marked as pending in the radix tree and remove
898 * them from the extent map
899 */
Chris Masone089f052007-03-16 16:20:31 -0400900static int del_pending_extents(struct btrfs_trans_handle *trans, struct
901 btrfs_root *extent_root)
Chris Masonfec577f2007-02-26 10:40:21 -0500902{
903 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -0400904 int wret;
905 int err = 0;
Chris Mason8ef97622007-03-26 10:15:30 -0400906 unsigned long gang[4];
Chris Masonfec577f2007-02-26 10:40:21 -0500907 int i;
Chris Mason8ef97622007-03-26 10:15:30 -0400908 struct radix_tree_root *pending_radix;
909 struct radix_tree_root *pinned_radix;
Chris Masonbe744172007-05-06 10:15:01 -0400910 struct btrfs_block_group_cache *cache;
Chris Mason8ef97622007-03-26 10:15:30 -0400911
912 pending_radix = &extent_root->fs_info->pending_del_radix;
913 pinned_radix = &extent_root->fs_info->pinned_radix;
Chris Masonfec577f2007-02-26 10:40:21 -0500914
915 while(1) {
Chris Masone37c9e62007-05-09 20:13:14 -0400916 ret = find_first_radix_bit(pending_radix, gang, 0,
Chris Mason8ef97622007-03-26 10:15:30 -0400917 ARRAY_SIZE(gang));
Chris Masonfec577f2007-02-26 10:40:21 -0500918 if (!ret)
919 break;
920 for (i = 0; i < ret; i++) {
Chris Mason8ef97622007-03-26 10:15:30 -0400921 wret = set_radix_bit(pinned_radix, gang[i]);
Chris Masonbe744172007-05-06 10:15:01 -0400922 if (wret == 0) {
Chris Mason5276aed2007-06-11 21:33:38 -0400923 cache =
924 btrfs_lookup_block_group(extent_root->fs_info,
Chris Masonbe744172007-05-06 10:15:01 -0400925 gang[i]);
926 if (cache)
927 cache->pinned++;
928 }
929 if (wret < 0) {
930 printk(KERN_CRIT "set_radix_bit, err %d\n",
931 wret);
932 BUG_ON(wret < 0);
933 }
Chris Mason8ef97622007-03-26 10:15:30 -0400934 wret = clear_radix_bit(pending_radix, gang[i]);
935 BUG_ON(wret);
Chris Masond5719762007-03-23 10:01:08 -0400936 wret = __free_extent(trans, extent_root,
Chris Masone37c9e62007-05-09 20:13:14 -0400937 gang[i], 1, 0, 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400938 if (wret)
939 err = wret;
Chris Masonfec577f2007-02-26 10:40:21 -0500940 }
941 }
Chris Masone20d96d2007-03-22 12:13:20 -0400942 return err;
Chris Masonfec577f2007-02-26 10:40:21 -0500943}
944
945/*
946 * remove an extent from the root, returns 0 on success
947 */
Chris Masone089f052007-03-16 16:20:31 -0400948int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
949 *root, u64 blocknr, u64 num_blocks, int pin)
Chris Masonfec577f2007-02-26 10:40:21 -0500950{
Chris Mason9f5fae22007-03-20 14:38:32 -0400951 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Masonfec577f2007-02-26 10:40:21 -0500952 int pending_ret;
953 int ret;
Chris Masona28ec192007-03-06 20:08:01 -0500954
955 if (root == extent_root) {
Chris Mason8ef97622007-03-26 10:15:30 -0400956 pin_down_block(root, blocknr, 1);
Chris Masona28ec192007-03-06 20:08:01 -0500957 return 0;
958 }
Chris Masone37c9e62007-05-09 20:13:14 -0400959 ret = __free_extent(trans, root, blocknr, num_blocks, pin, pin == 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400960 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
Chris Masonfec577f2007-02-26 10:40:21 -0500961 return ret ? ret : pending_ret;
962}
963
964/*
965 * walks the btree of allocated extents and find a hole of a given size.
966 * The key ins is changed to record the hole:
967 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -0400968 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -0500969 * ins->offset == number of blocks
970 * Any available blocks before search_start are skipped.
971 */
Chris Masone089f052007-03-16 16:20:31 -0400972static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason6702ed42007-08-07 16:15:09 -0400973 *orig_root, u64 num_blocks, u64 empty_size,
974 u64 search_start, u64 search_end, u64 hint_block,
Chris Masonf2654de2007-06-26 12:20:46 -0400975 struct btrfs_key *ins, u64 exclude_start,
976 u64 exclude_nr, int data)
Chris Masonfec577f2007-02-26 10:40:21 -0500977{
Chris Mason5caf2a02007-04-02 11:20:42 -0400978 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -0400979 struct btrfs_key key;
Chris Masonfec577f2007-02-26 10:40:21 -0500980 int ret;
981 u64 hole_size = 0;
982 int slot = 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400983 u64 last_block = 0;
Chris Mason037e6392007-03-07 11:50:24 -0500984 u64 test_block;
Chris Masonbe744172007-05-06 10:15:01 -0400985 u64 orig_search_start = search_start;
Chris Masonfec577f2007-02-26 10:40:21 -0500986 int start_found;
Chris Mason234b63a2007-03-13 10:46:10 -0400987 struct btrfs_leaf *l;
Chris Mason9f5fae22007-03-20 14:38:32 -0400988 struct btrfs_root * root = orig_root->fs_info->extent_root;
Chris Masonf2458e12007-04-25 15:52:25 -0400989 struct btrfs_fs_info *info = root->fs_info;
Chris Mason0579da42007-03-07 16:15:30 -0500990 int total_needed = num_blocks;
Chris Masone20d96d2007-03-22 12:13:20 -0400991 int level;
Chris Masonbe08c1b2007-05-03 09:06:49 -0400992 struct btrfs_block_group_cache *block_group;
Chris Masonbe744172007-05-06 10:15:01 -0400993 int full_scan = 0;
Chris Masonfbdc7622007-05-30 10:22:12 -0400994 int wrapped = 0;
Chris Masonfec577f2007-02-26 10:40:21 -0500995
Chris Mason26b80032007-08-08 20:17:12 -0400996 WARN_ON(num_blocks < 1);
Chris Masonb1a4d962007-04-04 15:27:52 -0400997 ins->flags = 0;
998 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
999
Chris Masone20d96d2007-03-22 12:13:20 -04001000 level = btrfs_header_level(btrfs_buffer_header(root->node));
Chris Mason3e1ad542007-05-07 20:03:49 -04001001 if (search_end == (u64)-1)
Chris Mason4b52dff2007-06-26 10:06:50 -04001002 search_end = btrfs_super_total_blocks(&info->super_copy);
Chris Masonfbdc7622007-05-30 10:22:12 -04001003 if (hint_block) {
Chris Mason5276aed2007-06-11 21:33:38 -04001004 block_group = btrfs_lookup_block_group(info, hint_block);
Chris Masonbe744172007-05-06 10:15:01 -04001005 block_group = btrfs_find_block_group(root, block_group,
Chris Masonfbdc7622007-05-30 10:22:12 -04001006 hint_block, data, 1);
Chris Masonbe744172007-05-06 10:15:01 -04001007 } else {
1008 block_group = btrfs_find_block_group(root,
1009 trans->block_group, 0,
Chris Masonde428b62007-05-18 13:28:27 -04001010 data, 1);
Chris Masonbe744172007-05-06 10:15:01 -04001011 }
1012
Chris Mason6702ed42007-08-07 16:15:09 -04001013 total_needed += empty_size;
Chris Masone0115992007-06-19 16:23:05 -04001014 path = btrfs_alloc_path();
1015
Chris Masonbe744172007-05-06 10:15:01 -04001016check_failed:
Chris Mason1e2677e2007-05-29 16:52:18 -04001017 if (!block_group->data)
Chris Masone37c9e62007-05-09 20:13:14 -04001018 search_start = find_search_start(root, &block_group,
1019 search_start, total_needed);
Chris Masonfbdc7622007-05-30 10:22:12 -04001020 else if (!full_scan)
Chris Masone37c9e62007-05-09 20:13:14 -04001021 search_start = max(block_group->last_alloc, search_start);
1022
Chris Mason5caf2a02007-04-02 11:20:42 -04001023 btrfs_init_path(path);
Chris Masonfec577f2007-02-26 10:40:21 -05001024 ins->objectid = search_start;
1025 ins->offset = 0;
Chris Masonfec577f2007-02-26 10:40:21 -05001026 start_found = 0;
Chris Mason2cc58cf2007-08-27 16:49:44 -04001027 path->reada = 2;
Chris Masone37c9e62007-05-09 20:13:14 -04001028
Chris Mason5caf2a02007-04-02 11:20:42 -04001029 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
Chris Mason0f70abe2007-02-28 16:46:22 -05001030 if (ret < 0)
1031 goto error;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001032
Chris Masone37c9e62007-05-09 20:13:14 -04001033 if (path->slots[0] > 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001034 path->slots[0]--;
Chris Masone37c9e62007-05-09 20:13:14 -04001035 }
1036
1037 l = btrfs_buffer_leaf(path->nodes[0]);
1038 btrfs_disk_key_to_cpu(&key, &l->items[path->slots[0]].key);
1039 /*
1040 * a rare case, go back one key if we hit a block group item
1041 * instead of an extent item
1042 */
1043 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY &&
1044 key.objectid + key.offset >= search_start) {
1045 ins->objectid = key.objectid;
1046 ins->offset = key.offset - 1;
1047 btrfs_release_path(root, path);
1048 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
1049 if (ret < 0)
1050 goto error;
1051
1052 if (path->slots[0] > 0) {
1053 path->slots[0]--;
1054 }
1055 }
Chris Mason0579da42007-03-07 16:15:30 -05001056
Chris Masonfec577f2007-02-26 10:40:21 -05001057 while (1) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001058 l = btrfs_buffer_leaf(path->nodes[0]);
1059 slot = path->slots[0];
Chris Mason7518a232007-03-12 12:01:18 -04001060 if (slot >= btrfs_header_nritems(&l->header)) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001061 ret = btrfs_next_leaf(root, path);
Chris Masonfec577f2007-02-26 10:40:21 -05001062 if (ret == 0)
1063 continue;
Chris Mason0f70abe2007-02-28 16:46:22 -05001064 if (ret < 0)
1065 goto error;
Chris Masonfec577f2007-02-26 10:40:21 -05001066 if (!start_found) {
1067 ins->objectid = search_start;
Chris Mason3e1ad542007-05-07 20:03:49 -04001068 ins->offset = search_end - search_start;
Chris Masonfec577f2007-02-26 10:40:21 -05001069 start_found = 1;
1070 goto check_pending;
1071 }
1072 ins->objectid = last_block > search_start ?
1073 last_block : search_start;
Chris Mason3e1ad542007-05-07 20:03:49 -04001074 ins->offset = search_end - ins->objectid;
Chris Masonfec577f2007-02-26 10:40:21 -05001075 goto check_pending;
1076 }
Chris Masone37c9e62007-05-09 20:13:14 -04001077
Chris Masone2fa7222007-03-12 16:22:34 -04001078 btrfs_disk_key_to_cpu(&key, &l->items[slot].key);
Chris Masone37c9e62007-05-09 20:13:14 -04001079 if (key.objectid >= search_start && key.objectid > last_block &&
1080 start_found) {
1081 if (last_block < search_start)
1082 last_block = search_start;
1083 hole_size = key.objectid - last_block;
1084 if (hole_size >= num_blocks) {
1085 ins->objectid = last_block;
1086 ins->offset = hole_size;
1087 goto check_pending;
Chris Mason0579da42007-03-07 16:15:30 -05001088 }
Chris Masonfec577f2007-02-26 10:40:21 -05001089 }
Chris Masone37c9e62007-05-09 20:13:14 -04001090
1091 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY)
1092 goto next;
1093
Chris Mason0579da42007-03-07 16:15:30 -05001094 start_found = 1;
Chris Masone2fa7222007-03-12 16:22:34 -04001095 last_block = key.objectid + key.offset;
Chris Masonfbdc7622007-05-30 10:22:12 -04001096 if (!full_scan && last_block >= block_group->key.objectid +
Chris Masonbe744172007-05-06 10:15:01 -04001097 block_group->key.offset) {
1098 btrfs_release_path(root, path);
1099 search_start = block_group->key.objectid +
1100 block_group->key.offset * 2;
1101 goto new_group;
1102 }
Chris Mason9078a3e2007-04-26 16:46:15 -04001103next:
Chris Mason5caf2a02007-04-02 11:20:42 -04001104 path->slots[0]++;
Chris Masonde428b62007-05-18 13:28:27 -04001105 cond_resched();
Chris Masonfec577f2007-02-26 10:40:21 -05001106 }
Chris Masonfec577f2007-02-26 10:40:21 -05001107check_pending:
1108 /* we have to make sure we didn't find an extent that has already
1109 * been allocated by the map tree or the original allocation
1110 */
Chris Mason5caf2a02007-04-02 11:20:42 -04001111 btrfs_release_path(root, path);
Chris Masonfec577f2007-02-26 10:40:21 -05001112 BUG_ON(ins->objectid < search_start);
Chris Masone37c9e62007-05-09 20:13:14 -04001113
Chris Masoncf675822007-09-17 11:00:51 -04001114 if (ins->objectid + num_blocks >= search_end)
1115 goto enospc;
1116
Chris Mason037e6392007-03-07 11:50:24 -05001117 for (test_block = ins->objectid;
Chris Masonf2458e12007-04-25 15:52:25 -04001118 test_block < ins->objectid + num_blocks; test_block++) {
Chris Mason26b80032007-08-08 20:17:12 -04001119 if (test_radix_bit(&info->pinned_radix, test_block) ||
1120 test_radix_bit(&info->extent_ins_radix, test_block)) {
Chris Mason037e6392007-03-07 11:50:24 -05001121 search_start = test_block + 1;
Chris Masonbe744172007-05-06 10:15:01 -04001122 goto new_group;
Chris Masonfec577f2007-02-26 10:40:21 -05001123 }
1124 }
Chris Masonf2654de2007-06-26 12:20:46 -04001125 if (exclude_nr > 0 && (ins->objectid + num_blocks > exclude_start &&
1126 ins->objectid < exclude_start + exclude_nr)) {
1127 search_start = exclude_start + exclude_nr;
1128 goto new_group;
1129 }
Chris Masone37c9e62007-05-09 20:13:14 -04001130 if (!data) {
Chris Mason5276aed2007-06-11 21:33:38 -04001131 block_group = btrfs_lookup_block_group(info, ins->objectid);
Chris Mason26b80032007-08-08 20:17:12 -04001132 if (block_group)
1133 trans->block_group = block_group;
Chris Masoncd1bc462007-04-27 10:08:34 -04001134 }
Chris Mason037e6392007-03-07 11:50:24 -05001135 ins->offset = num_blocks;
Chris Mason5caf2a02007-04-02 11:20:42 -04001136 btrfs_free_path(path);
Chris Masonfec577f2007-02-26 10:40:21 -05001137 return 0;
Chris Masonbe744172007-05-06 10:15:01 -04001138
1139new_group:
Chris Mason3e1ad542007-05-07 20:03:49 -04001140 if (search_start + num_blocks >= search_end) {
Chris Masoncf675822007-09-17 11:00:51 -04001141enospc:
Chris Masonbe744172007-05-06 10:15:01 -04001142 search_start = orig_search_start;
Chris Masonfbdc7622007-05-30 10:22:12 -04001143 if (full_scan) {
1144 ret = -ENOSPC;
1145 goto error;
1146 }
Chris Mason6702ed42007-08-07 16:15:09 -04001147 if (wrapped) {
1148 if (!full_scan)
1149 total_needed -= empty_size;
Chris Masonfbdc7622007-05-30 10:22:12 -04001150 full_scan = 1;
Chris Mason6702ed42007-08-07 16:15:09 -04001151 } else
Chris Masonfbdc7622007-05-30 10:22:12 -04001152 wrapped = 1;
Chris Masonbe744172007-05-06 10:15:01 -04001153 }
Chris Mason5276aed2007-06-11 21:33:38 -04001154 block_group = btrfs_lookup_block_group(info, search_start);
Chris Masonfbdc7622007-05-30 10:22:12 -04001155 cond_resched();
Chris Masonbe744172007-05-06 10:15:01 -04001156 if (!full_scan)
1157 block_group = btrfs_find_block_group(root, block_group,
Chris Masonde428b62007-05-18 13:28:27 -04001158 search_start, data, 0);
Chris Masonbe744172007-05-06 10:15:01 -04001159 goto check_failed;
1160
Chris Mason0f70abe2007-02-28 16:46:22 -05001161error:
Chris Mason5caf2a02007-04-02 11:20:42 -04001162 btrfs_release_path(root, path);
1163 btrfs_free_path(path);
Chris Mason0f70abe2007-02-28 16:46:22 -05001164 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05001165}
Chris Masonfec577f2007-02-26 10:40:21 -05001166/*
Chris Masonfec577f2007-02-26 10:40:21 -05001167 * finds a free extent and does all the dirty work required for allocation
1168 * returns the key for the extent through ins, and a tree buffer for
1169 * the first block of the extent through buf.
1170 *
1171 * returns 0 if everything worked, non-zero otherwise.
1172 */
Chris Mason4d775672007-04-20 20:23:12 -04001173int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
1174 struct btrfs_root *root, u64 owner,
Chris Mason6702ed42007-08-07 16:15:09 -04001175 u64 num_blocks, u64 empty_size, u64 hint_block,
Chris Masonbe08c1b2007-05-03 09:06:49 -04001176 u64 search_end, struct btrfs_key *ins, int data)
Chris Masonfec577f2007-02-26 10:40:21 -05001177{
1178 int ret;
1179 int pending_ret;
Josef Bacik58176a92007-08-29 15:47:34 -04001180 u64 super_blocks_used, root_blocks_used;
Chris Masonfbdc7622007-05-30 10:22:12 -04001181 u64 search_start = 0;
Chris Mason1261ec42007-03-20 20:35:03 -04001182 struct btrfs_fs_info *info = root->fs_info;
1183 struct btrfs_root *extent_root = info->extent_root;
Chris Mason234b63a2007-03-13 10:46:10 -04001184 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -05001185
Chris Masoncf27e1e2007-03-13 09:49:06 -04001186 btrfs_set_extent_refs(&extent_item, 1);
Chris Mason4d775672007-04-20 20:23:12 -04001187 btrfs_set_extent_owner(&extent_item, owner);
Chris Masonfec577f2007-02-26 10:40:21 -05001188
Chris Mason26b80032007-08-08 20:17:12 -04001189 WARN_ON(num_blocks < 1);
Chris Mason6702ed42007-08-07 16:15:09 -04001190 ret = find_free_extent(trans, root, num_blocks, empty_size,
1191 search_start, search_end, hint_block, ins,
Chris Mason26b80032007-08-08 20:17:12 -04001192 trans->alloc_exclude_start,
1193 trans->alloc_exclude_nr, data);
Chris Masonccd467d2007-06-28 15:57:36 -04001194 BUG_ON(ret);
Chris Masonf2654de2007-06-26 12:20:46 -04001195 if (ret)
1196 return ret;
1197
Josef Bacik58176a92007-08-29 15:47:34 -04001198 /* block accounting for super block */
Chris Mason4b52dff2007-06-26 10:06:50 -04001199 super_blocks_used = btrfs_super_blocks_used(&info->super_copy);
1200 btrfs_set_super_blocks_used(&info->super_copy, super_blocks_used +
Chris Mason1261ec42007-03-20 20:35:03 -04001201 num_blocks);
Chris Mason26b80032007-08-08 20:17:12 -04001202
Josef Bacik58176a92007-08-29 15:47:34 -04001203 /* block accounting for root item */
1204 root_blocks_used = btrfs_root_blocks_used(&root->root_item);
1205 btrfs_set_root_blocks_used(&root->root_item, root_blocks_used +
1206 num_blocks);
1207
Chris Mason26b80032007-08-08 20:17:12 -04001208 if (root == extent_root) {
1209 BUG_ON(num_blocks != 1);
1210 set_radix_bit(&root->fs_info->extent_ins_radix, ins->objectid);
1211 goto update_block;
1212 }
1213
1214 WARN_ON(trans->alloc_exclude_nr);
1215 trans->alloc_exclude_start = ins->objectid;
1216 trans->alloc_exclude_nr = ins->offset;
Chris Masone089f052007-03-16 16:20:31 -04001217 ret = btrfs_insert_item(trans, extent_root, ins, &extent_item,
1218 sizeof(extent_item));
Chris Mason037e6392007-03-07 11:50:24 -05001219
Chris Mason26b80032007-08-08 20:17:12 -04001220 trans->alloc_exclude_start = 0;
1221 trans->alloc_exclude_nr = 0;
1222
Chris Masonccd467d2007-06-28 15:57:36 -04001223 BUG_ON(ret);
Chris Masone089f052007-03-16 16:20:31 -04001224 finish_current_insert(trans, extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -04001225 pending_ret = del_pending_extents(trans, extent_root);
Chris Masone37c9e62007-05-09 20:13:14 -04001226 if (ret) {
Chris Mason037e6392007-03-07 11:50:24 -05001227 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -04001228 }
1229 if (pending_ret) {
Chris Mason037e6392007-03-07 11:50:24 -05001230 return pending_ret;
Chris Masone37c9e62007-05-09 20:13:14 -04001231 }
Chris Mason26b80032007-08-08 20:17:12 -04001232
1233update_block:
Chris Mason1e2677e2007-05-29 16:52:18 -04001234 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0,
1235 data);
Chris Masonfabb5682007-06-07 22:13:21 -04001236 BUG_ON(ret);
Chris Mason037e6392007-03-07 11:50:24 -05001237 return 0;
Chris Masonfec577f2007-02-26 10:40:21 -05001238}
1239
1240/*
1241 * helper function to allocate a block for a given tree
1242 * returns the tree buffer or NULL.
1243 */
Chris Masone20d96d2007-03-22 12:13:20 -04001244struct buffer_head *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Chris Mason6702ed42007-08-07 16:15:09 -04001245 struct btrfs_root *root, u64 hint,
1246 u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05001247{
Chris Masone2fa7222007-03-12 16:22:34 -04001248 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -05001249 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -04001250 struct buffer_head *buf;
Chris Masonfec577f2007-02-26 10:40:21 -05001251
Chris Mason4d775672007-04-20 20:23:12 -04001252 ret = btrfs_alloc_extent(trans, root, root->root_key.objectid,
Yane9fe3952007-08-29 09:11:44 -04001253 1, empty_size, hint, (u64)-1, &ins, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05001254 if (ret) {
Chris Mason54aa1f42007-06-22 14:16:25 -04001255 BUG_ON(ret > 0);
1256 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05001257 }
Chris Masond98237b2007-03-28 13:57:48 -04001258 buf = btrfs_find_create_tree_block(root, ins.objectid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001259 if (!buf) {
1260 btrfs_free_extent(trans, root, ins.objectid, 1, 0);
1261 return ERR_PTR(-ENOMEM);
1262 }
Chris Mason6702ed42007-08-07 16:15:09 -04001263 WARN_ON(buffer_dirty(buf));
Chris Masondf2ce342007-03-23 11:00:45 -04001264 set_buffer_uptodate(buf);
Chris Mason090d1872007-05-01 08:53:32 -04001265 set_buffer_checked(buf);
Chris Masonf2183bd2007-08-10 14:42:37 -04001266 set_buffer_defrag(buf);
Chris Mason7c4452b2007-04-28 09:29:35 -04001267 set_radix_bit(&trans->transaction->dirty_pages, buf->b_page->index);
Chris Masond3c2fdc2007-09-17 10:58:06 -04001268 trans->blocks_used++;
Chris Masonfec577f2007-02-26 10:40:21 -05001269 return buf;
1270}
Chris Masona28ec192007-03-06 20:08:01 -05001271
Chris Mason6407bf62007-03-27 06:33:00 -04001272static int drop_leaf_ref(struct btrfs_trans_handle *trans,
1273 struct btrfs_root *root, struct buffer_head *cur)
1274{
1275 struct btrfs_disk_key *key;
1276 struct btrfs_leaf *leaf;
1277 struct btrfs_file_extent_item *fi;
1278 int i;
1279 int nritems;
1280 int ret;
1281
1282 BUG_ON(!btrfs_is_leaf(btrfs_buffer_node(cur)));
1283 leaf = btrfs_buffer_leaf(cur);
1284 nritems = btrfs_header_nritems(&leaf->header);
1285 for (i = 0; i < nritems; i++) {
Chris Mason3a686372007-05-24 13:35:57 -04001286 u64 disk_blocknr;
Chris Mason6407bf62007-03-27 06:33:00 -04001287 key = &leaf->items[i].key;
1288 if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY)
1289 continue;
1290 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
Chris Mason236454d2007-04-19 13:37:44 -04001291 if (btrfs_file_extent_type(fi) == BTRFS_FILE_EXTENT_INLINE)
1292 continue;
Chris Mason6407bf62007-03-27 06:33:00 -04001293 /*
1294 * FIXME make sure to insert a trans record that
1295 * repeats the snapshot del on crash
1296 */
Chris Mason3a686372007-05-24 13:35:57 -04001297 disk_blocknr = btrfs_file_extent_disk_blocknr(fi);
1298 if (disk_blocknr == 0)
1299 continue;
1300 ret = btrfs_free_extent(trans, root, disk_blocknr,
Chris Mason6407bf62007-03-27 06:33:00 -04001301 btrfs_file_extent_disk_num_blocks(fi),
1302 0);
1303 BUG_ON(ret);
1304 }
1305 return 0;
1306}
1307
Chris Masone0115992007-06-19 16:23:05 -04001308static void reada_walk_down(struct btrfs_root *root,
1309 struct btrfs_node *node)
1310{
1311 int i;
1312 u32 nritems;
1313 u64 blocknr;
1314 int ret;
1315 u32 refs;
1316
1317 nritems = btrfs_header_nritems(&node->header);
1318 for (i = 0; i < nritems; i++) {
1319 blocknr = btrfs_node_blockptr(node, i);
1320 ret = lookup_extent_ref(NULL, root, blocknr, 1, &refs);
1321 BUG_ON(ret);
1322 if (refs != 1)
1323 continue;
Chris Mason409eb952007-08-08 20:17:12 -04001324 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masone0115992007-06-19 16:23:05 -04001325 ret = readahead_tree_block(root, blocknr);
Chris Mason409eb952007-08-08 20:17:12 -04001326 cond_resched();
1327 mutex_lock(&root->fs_info->fs_mutex);
Chris Masone0115992007-06-19 16:23:05 -04001328 if (ret)
1329 break;
1330 }
1331}
1332
Chris Mason9aca1d52007-03-13 11:09:37 -04001333/*
1334 * helper function for drop_snapshot, this walks down the tree dropping ref
1335 * counts as it goes.
1336 */
Chris Masone089f052007-03-16 16:20:31 -04001337static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
1338 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05001339{
Chris Masone20d96d2007-03-22 12:13:20 -04001340 struct buffer_head *next;
1341 struct buffer_head *cur;
Chris Mason20524f02007-03-10 06:35:47 -05001342 u64 blocknr;
1343 int ret;
1344 u32 refs;
1345
Chris Mason5caf2a02007-04-02 11:20:42 -04001346 WARN_ON(*level < 0);
1347 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Masonb18c6682007-04-17 13:26:50 -04001348 ret = lookup_extent_ref(trans, root, bh_blocknr(path->nodes[*level]),
Chris Mason6407bf62007-03-27 06:33:00 -04001349 1, &refs);
Chris Mason20524f02007-03-10 06:35:47 -05001350 BUG_ON(ret);
1351 if (refs > 1)
1352 goto out;
Chris Masone0115992007-06-19 16:23:05 -04001353
Chris Mason9aca1d52007-03-13 11:09:37 -04001354 /*
1355 * walk down to the last node level and free all the leaves
1356 */
Chris Mason6407bf62007-03-27 06:33:00 -04001357 while(*level >= 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001358 WARN_ON(*level < 0);
1359 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason20524f02007-03-10 06:35:47 -05001360 cur = path->nodes[*level];
Chris Masone0115992007-06-19 16:23:05 -04001361
1362 if (*level > 0 && path->slots[*level] == 0)
1363 reada_walk_down(root, btrfs_buffer_node(cur));
1364
Chris Mason2c90e5d2007-04-02 10:50:19 -04001365 if (btrfs_header_level(btrfs_buffer_header(cur)) != *level)
1366 WARN_ON(1);
Chris Masone0115992007-06-19 16:23:05 -04001367
Chris Mason7518a232007-03-12 12:01:18 -04001368 if (path->slots[*level] >=
Chris Masone20d96d2007-03-22 12:13:20 -04001369 btrfs_header_nritems(btrfs_buffer_header(cur)))
Chris Mason20524f02007-03-10 06:35:47 -05001370 break;
Chris Mason6407bf62007-03-27 06:33:00 -04001371 if (*level == 0) {
1372 ret = drop_leaf_ref(trans, root, cur);
1373 BUG_ON(ret);
1374 break;
1375 }
Chris Masone20d96d2007-03-22 12:13:20 -04001376 blocknr = btrfs_node_blockptr(btrfs_buffer_node(cur),
1377 path->slots[*level]);
Chris Masonb18c6682007-04-17 13:26:50 -04001378 ret = lookup_extent_ref(trans, root, blocknr, 1, &refs);
Chris Mason6407bf62007-03-27 06:33:00 -04001379 BUG_ON(ret);
1380 if (refs != 1) {
Chris Mason20524f02007-03-10 06:35:47 -05001381 path->slots[*level]++;
Chris Masone089f052007-03-16 16:20:31 -04001382 ret = btrfs_free_extent(trans, root, blocknr, 1, 1);
Chris Mason20524f02007-03-10 06:35:47 -05001383 BUG_ON(ret);
1384 continue;
1385 }
Chris Masone9d0b132007-08-10 14:06:19 -04001386 next = btrfs_find_tree_block(root, blocknr);
1387 if (!next || !buffer_uptodate(next)) {
1388 brelse(next);
1389 mutex_unlock(&root->fs_info->fs_mutex);
1390 next = read_tree_block(root, blocknr);
1391 mutex_lock(&root->fs_info->fs_mutex);
1392
1393 /* we dropped the lock, check one more time */
1394 ret = lookup_extent_ref(trans, root, blocknr, 1, &refs);
1395 BUG_ON(ret);
1396 if (refs != 1) {
1397 path->slots[*level]++;
1398 brelse(next);
1399 ret = btrfs_free_extent(trans, root,
1400 blocknr, 1, 1);
1401 BUG_ON(ret);
1402 continue;
1403 }
1404 }
Chris Mason5caf2a02007-04-02 11:20:42 -04001405 WARN_ON(*level <= 0);
Chris Mason83e15a22007-03-12 09:03:27 -04001406 if (path->nodes[*level-1])
Chris Mason234b63a2007-03-13 10:46:10 -04001407 btrfs_block_release(root, path->nodes[*level-1]);
Chris Mason20524f02007-03-10 06:35:47 -05001408 path->nodes[*level-1] = next;
Chris Masone20d96d2007-03-22 12:13:20 -04001409 *level = btrfs_header_level(btrfs_buffer_header(next));
Chris Mason20524f02007-03-10 06:35:47 -05001410 path->slots[*level] = 0;
1411 }
1412out:
Chris Mason5caf2a02007-04-02 11:20:42 -04001413 WARN_ON(*level < 0);
1414 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason6407bf62007-03-27 06:33:00 -04001415 ret = btrfs_free_extent(trans, root,
Chris Mason7eccb902007-04-11 15:53:25 -04001416 bh_blocknr(path->nodes[*level]), 1, 1);
Chris Mason234b63a2007-03-13 10:46:10 -04001417 btrfs_block_release(root, path->nodes[*level]);
Chris Mason20524f02007-03-10 06:35:47 -05001418 path->nodes[*level] = NULL;
1419 *level += 1;
1420 BUG_ON(ret);
1421 return 0;
1422}
1423
Chris Mason9aca1d52007-03-13 11:09:37 -04001424/*
1425 * helper for dropping snapshots. This walks back up the tree in the path
1426 * to find the first node higher up where we haven't yet gone through
1427 * all the slots
1428 */
Chris Masone089f052007-03-16 16:20:31 -04001429static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root
1430 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05001431{
1432 int i;
1433 int slot;
1434 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04001435 struct btrfs_root_item *root_item = &root->root_item;
1436
Chris Mason234b63a2007-03-13 10:46:10 -04001437 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Mason20524f02007-03-10 06:35:47 -05001438 slot = path->slots[i];
Chris Masone20d96d2007-03-22 12:13:20 -04001439 if (slot < btrfs_header_nritems(
1440 btrfs_buffer_header(path->nodes[i])) - 1) {
Chris Mason9f3a7422007-08-07 15:52:19 -04001441 struct btrfs_node *node;
1442 node = btrfs_buffer_node(path->nodes[i]);
Chris Mason20524f02007-03-10 06:35:47 -05001443 path->slots[i]++;
1444 *level = i;
Chris Mason9f3a7422007-08-07 15:52:19 -04001445 WARN_ON(*level == 0);
1446 memcpy(&root_item->drop_progress,
1447 &node->ptrs[path->slots[i]].key,
1448 sizeof(root_item->drop_progress));
1449 root_item->drop_level = i;
Chris Mason20524f02007-03-10 06:35:47 -05001450 return 0;
1451 } else {
Chris Masone089f052007-03-16 16:20:31 -04001452 ret = btrfs_free_extent(trans, root,
Chris Mason7eccb902007-04-11 15:53:25 -04001453 bh_blocknr(path->nodes[*level]),
Chris Masone089f052007-03-16 16:20:31 -04001454 1, 1);
Chris Mason6407bf62007-03-27 06:33:00 -04001455 BUG_ON(ret);
Chris Mason234b63a2007-03-13 10:46:10 -04001456 btrfs_block_release(root, path->nodes[*level]);
Chris Mason83e15a22007-03-12 09:03:27 -04001457 path->nodes[*level] = NULL;
Chris Mason20524f02007-03-10 06:35:47 -05001458 *level = i + 1;
Chris Mason20524f02007-03-10 06:35:47 -05001459 }
1460 }
1461 return 1;
1462}
1463
Chris Mason9aca1d52007-03-13 11:09:37 -04001464/*
1465 * drop the reference count on the tree rooted at 'snap'. This traverses
1466 * the tree freeing any blocks that have a ref count of zero after being
1467 * decremented.
1468 */
Chris Masone089f052007-03-16 16:20:31 -04001469int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason9f3a7422007-08-07 15:52:19 -04001470 *root)
Chris Mason20524f02007-03-10 06:35:47 -05001471{
Chris Mason3768f362007-03-13 16:47:54 -04001472 int ret = 0;
Chris Mason9aca1d52007-03-13 11:09:37 -04001473 int wret;
Chris Mason20524f02007-03-10 06:35:47 -05001474 int level;
Chris Mason5caf2a02007-04-02 11:20:42 -04001475 struct btrfs_path *path;
Chris Mason20524f02007-03-10 06:35:47 -05001476 int i;
1477 int orig_level;
Chris Mason9f3a7422007-08-07 15:52:19 -04001478 struct btrfs_root_item *root_item = &root->root_item;
Chris Mason20524f02007-03-10 06:35:47 -05001479
Chris Mason5caf2a02007-04-02 11:20:42 -04001480 path = btrfs_alloc_path();
1481 BUG_ON(!path);
Chris Mason20524f02007-03-10 06:35:47 -05001482
Chris Mason9f3a7422007-08-07 15:52:19 -04001483 level = btrfs_header_level(btrfs_buffer_header(root->node));
Chris Mason20524f02007-03-10 06:35:47 -05001484 orig_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04001485 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
1486 path->nodes[level] = root->node;
1487 path->slots[level] = 0;
1488 } else {
1489 struct btrfs_key key;
1490 struct btrfs_disk_key *found_key;
1491 struct btrfs_node *node;
Chris Mason6702ed42007-08-07 16:15:09 -04001492
Chris Mason9f3a7422007-08-07 15:52:19 -04001493 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Chris Mason6702ed42007-08-07 16:15:09 -04001494 level = root_item->drop_level;
1495 path->lowest_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04001496 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason6702ed42007-08-07 16:15:09 -04001497 if (wret < 0) {
Chris Mason9f3a7422007-08-07 15:52:19 -04001498 ret = wret;
1499 goto out;
1500 }
Chris Mason9f3a7422007-08-07 15:52:19 -04001501 node = btrfs_buffer_node(path->nodes[level]);
1502 found_key = &node->ptrs[path->slots[level]].key;
1503 WARN_ON(memcmp(found_key, &root_item->drop_progress,
1504 sizeof(*found_key)));
1505 }
Chris Mason20524f02007-03-10 06:35:47 -05001506 while(1) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001507 wret = walk_down_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04001508 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05001509 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04001510 if (wret < 0)
1511 ret = wret;
1512
Chris Mason5caf2a02007-04-02 11:20:42 -04001513 wret = walk_up_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04001514 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05001515 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04001516 if (wret < 0)
1517 ret = wret;
Chris Mason409eb952007-08-08 20:17:12 -04001518 ret = -EAGAIN;
1519 get_bh(root->node);
1520 break;
Chris Mason20524f02007-03-10 06:35:47 -05001521 }
Chris Mason83e15a22007-03-12 09:03:27 -04001522 for (i = 0; i <= orig_level; i++) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001523 if (path->nodes[i]) {
1524 btrfs_block_release(root, path->nodes[i]);
Chris Mason6702ed42007-08-07 16:15:09 -04001525 path->nodes[i] = 0;
Chris Mason83e15a22007-03-12 09:03:27 -04001526 }
Chris Mason20524f02007-03-10 06:35:47 -05001527 }
Chris Mason9f3a7422007-08-07 15:52:19 -04001528out:
Chris Mason5caf2a02007-04-02 11:20:42 -04001529 btrfs_free_path(path);
Chris Mason9aca1d52007-03-13 11:09:37 -04001530 return ret;
Chris Mason20524f02007-03-10 06:35:47 -05001531}
Chris Mason9078a3e2007-04-26 16:46:15 -04001532
Chris Masonbe744172007-05-06 10:15:01 -04001533static int free_block_group_radix(struct radix_tree_root *radix)
Chris Mason9078a3e2007-04-26 16:46:15 -04001534{
1535 int ret;
1536 struct btrfs_block_group_cache *cache[8];
1537 int i;
1538
1539 while(1) {
Chris Masonbe744172007-05-06 10:15:01 -04001540 ret = radix_tree_gang_lookup(radix, (void **)cache, 0,
Chris Mason9078a3e2007-04-26 16:46:15 -04001541 ARRAY_SIZE(cache));
1542 if (!ret)
1543 break;
1544 for (i = 0; i < ret; i++) {
Chris Masonbe744172007-05-06 10:15:01 -04001545 radix_tree_delete(radix, cache[i]->key.objectid +
Chris Mason9078a3e2007-04-26 16:46:15 -04001546 cache[i]->key.offset - 1);
1547 kfree(cache[i]);
1548 }
1549 }
1550 return 0;
1551}
1552
Chris Masonbe744172007-05-06 10:15:01 -04001553int btrfs_free_block_groups(struct btrfs_fs_info *info)
1554{
1555 int ret;
1556 int ret2;
Chris Masone37c9e62007-05-09 20:13:14 -04001557 unsigned long gang[16];
1558 int i;
Chris Masonbe744172007-05-06 10:15:01 -04001559
1560 ret = free_block_group_radix(&info->block_group_radix);
1561 ret2 = free_block_group_radix(&info->block_group_data_radix);
1562 if (ret)
1563 return ret;
1564 if (ret2)
1565 return ret2;
Chris Masone37c9e62007-05-09 20:13:14 -04001566
1567 while(1) {
1568 ret = find_first_radix_bit(&info->extent_map_radix,
1569 gang, 0, ARRAY_SIZE(gang));
1570 if (!ret)
1571 break;
1572 for (i = 0; i < ret; i++) {
1573 clear_radix_bit(&info->extent_map_radix, gang[i]);
1574 }
1575 }
Chris Masonbe744172007-05-06 10:15:01 -04001576 return 0;
1577}
1578
Chris Mason9078a3e2007-04-26 16:46:15 -04001579int btrfs_read_block_groups(struct btrfs_root *root)
1580{
1581 struct btrfs_path *path;
1582 int ret;
1583 int err = 0;
1584 struct btrfs_block_group_item *bi;
1585 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04001586 struct btrfs_fs_info *info = root->fs_info;
1587 struct radix_tree_root *radix;
Chris Mason9078a3e2007-04-26 16:46:15 -04001588 struct btrfs_key key;
1589 struct btrfs_key found_key;
1590 struct btrfs_leaf *leaf;
Chris Mason84f54cf2007-06-12 07:43:08 -04001591 u64 group_size_blocks;
Chris Mason31f3c992007-04-30 15:25:45 -04001592 u64 used;
Chris Mason9078a3e2007-04-26 16:46:15 -04001593
Chris Mason84f54cf2007-06-12 07:43:08 -04001594 group_size_blocks = BTRFS_BLOCK_GROUP_SIZE >>
1595 root->fs_info->sb->s_blocksize_bits;
Chris Masonbe744172007-05-06 10:15:01 -04001596 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04001597 key.objectid = 0;
1598 key.offset = group_size_blocks;
1599 key.flags = 0;
1600 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
1601
1602 path = btrfs_alloc_path();
1603 if (!path)
1604 return -ENOMEM;
1605
1606 while(1) {
Chris Masonbe744172007-05-06 10:15:01 -04001607 ret = btrfs_search_slot(NULL, info->extent_root,
Chris Mason9078a3e2007-04-26 16:46:15 -04001608 &key, path, 0, 0);
1609 if (ret != 0) {
1610 err = ret;
1611 break;
1612 }
1613 leaf = btrfs_buffer_leaf(path->nodes[0]);
1614 btrfs_disk_key_to_cpu(&found_key,
1615 &leaf->items[path->slots[0]].key);
1616 cache = kmalloc(sizeof(*cache), GFP_NOFS);
1617 if (!cache) {
1618 err = -1;
1619 break;
1620 }
Chris Mason3e1ad542007-05-07 20:03:49 -04001621
Chris Mason9078a3e2007-04-26 16:46:15 -04001622 bi = btrfs_item_ptr(leaf, path->slots[0],
1623 struct btrfs_block_group_item);
Chris Mason1e2677e2007-05-29 16:52:18 -04001624 if (bi->flags & BTRFS_BLOCK_GROUP_DATA) {
1625 radix = &info->block_group_data_radix;
1626 cache->data = 1;
1627 } else {
1628 radix = &info->block_group_radix;
1629 cache->data = 0;
1630 }
1631
Chris Mason9078a3e2007-04-26 16:46:15 -04001632 memcpy(&cache->item, bi, sizeof(*bi));
1633 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Mason31f3c992007-04-30 15:25:45 -04001634 cache->last_alloc = cache->key.objectid;
1635 cache->first_free = cache->key.objectid;
Chris Masonbe744172007-05-06 10:15:01 -04001636 cache->pinned = 0;
Chris Masone37c9e62007-05-09 20:13:14 -04001637 cache->cached = 0;
1638
Chris Mason3e1ad542007-05-07 20:03:49 -04001639 cache->radix = radix;
1640
Chris Mason9078a3e2007-04-26 16:46:15 -04001641 key.objectid = found_key.objectid + found_key.offset;
1642 btrfs_release_path(root, path);
Chris Masonbe744172007-05-06 10:15:01 -04001643 ret = radix_tree_insert(radix, found_key.objectid +
Chris Mason9078a3e2007-04-26 16:46:15 -04001644 found_key.offset - 1,
1645 (void *)cache);
1646 BUG_ON(ret);
Chris Mason31f3c992007-04-30 15:25:45 -04001647 used = btrfs_block_group_used(bi);
Chris Mason84f54cf2007-06-12 07:43:08 -04001648 if (used < div_factor(key.offset, 8)) {
Chris Masonbe744172007-05-06 10:15:01 -04001649 radix_tree_tag_set(radix, found_key.objectid +
Chris Mason31f3c992007-04-30 15:25:45 -04001650 found_key.offset - 1,
1651 BTRFS_BLOCK_GROUP_AVAIL);
1652 }
Chris Mason9078a3e2007-04-26 16:46:15 -04001653 if (key.objectid >=
Chris Mason4b52dff2007-06-26 10:06:50 -04001654 btrfs_super_total_blocks(&info->super_copy))
Chris Mason9078a3e2007-04-26 16:46:15 -04001655 break;
1656 }
1657
1658 btrfs_free_path(path);
1659 return 0;
1660}