blob: 3418bb62b99676a0abe536a507ff586b31b871c8 [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;
Chris Masonde428b62007-05-18 13:28:27 -040042 u64 limit;
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;
Chris Mason3c69fae2007-08-07 15:52:22 -040055 path->reada = 1;
Chris Masone37c9e62007-05-09 20:13:14 -040056 key.objectid = block_group->key.objectid;
57 key.flags = 0;
58 key.offset = 0;
59 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
60 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
61 if (ret < 0)
62 return ret;
63 if (ret && path->slots[0] > 0)
64 path->slots[0]--;
Chris Masonde428b62007-05-18 13:28:27 -040065 limit = block_group->key.objectid + block_group->key.offset;
Chris Masone37c9e62007-05-09 20:13:14 -040066 while(1) {
67 leaf = btrfs_buffer_leaf(path->nodes[0]);
68 slot = path->slots[0];
69 if (slot >= btrfs_header_nritems(&leaf->header)) {
70 ret = btrfs_next_leaf(root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -040071 if (ret < 0)
72 goto err;
Chris Masonde428b62007-05-18 13:28:27 -040073 if (ret == 0) {
Chris Masone37c9e62007-05-09 20:13:14 -040074 continue;
Chris Masonde428b62007-05-18 13:28:27 -040075 } else {
Chris Masone37c9e62007-05-09 20:13:14 -040076 if (found) {
77 hole_size = block_group->key.objectid +
78 block_group->key.offset - last;
79 } else {
80 last = block_group->key.objectid;
81 hole_size = block_group->key.offset;
82 }
83 for (i = 0; i < hole_size; i++) {
84 set_radix_bit(extent_radix,
85 last + i);
86 }
87 break;
88 }
89 }
90 btrfs_disk_key_to_cpu(&key, &leaf->items[slot].key);
91 if (key.objectid >= block_group->key.objectid +
92 block_group->key.offset) {
93 if (found) {
94 hole_size = block_group->key.objectid +
95 block_group->key.offset - last;
96 } else {
97 last = block_group->key.objectid;
98 hole_size = block_group->key.offset;
99 }
100 for (i = 0; i < hole_size; i++) {
101 set_radix_bit(extent_radix, last + i);
102 }
103 break;
104 }
105 if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) {
106 if (!found) {
107 last = key.objectid + key.offset;
108 found = 1;
109 } else {
110 hole_size = key.objectid - last;
111 for (i = 0; i < hole_size; i++) {
112 set_radix_bit(extent_radix, last + i);
113 }
114 last = key.objectid + key.offset;
115 }
116 }
117 path->slots[0]++;
118 }
119
120 block_group->cached = 1;
Chris Mason54aa1f42007-06-22 14:16:25 -0400121err:
Chris Masone37c9e62007-05-09 20:13:14 -0400122 btrfs_free_path(path);
123 return 0;
124}
125
Chris Mason5276aed2007-06-11 21:33:38 -0400126struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
127 btrfs_fs_info *info,
128 u64 blocknr)
Chris Masonbe744172007-05-06 10:15:01 -0400129{
130 struct btrfs_block_group_cache *block_group;
131 int ret;
132
133 ret = radix_tree_gang_lookup(&info->block_group_radix,
134 (void **)&block_group,
135 blocknr, 1);
136 if (ret) {
Chris Mason3e1ad542007-05-07 20:03:49 -0400137 if (block_group->key.objectid <= blocknr && blocknr <=
Chris Masonbe744172007-05-06 10:15:01 -0400138 block_group->key.objectid + block_group->key.offset)
139 return block_group;
140 }
141 ret = radix_tree_gang_lookup(&info->block_group_data_radix,
142 (void **)&block_group,
143 blocknr, 1);
144 if (ret) {
Chris Mason3e1ad542007-05-07 20:03:49 -0400145 if (block_group->key.objectid <= blocknr && blocknr <=
Chris Masonbe744172007-05-06 10:15:01 -0400146 block_group->key.objectid + block_group->key.offset)
147 return block_group;
148 }
Chris Masonbe744172007-05-06 10:15:01 -0400149 return NULL;
150}
151
Chris Masone37c9e62007-05-09 20:13:14 -0400152static u64 leaf_range(struct btrfs_root *root)
153{
154 u64 size = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason84f54cf2007-06-12 07:43:08 -0400155 do_div(size, sizeof(struct btrfs_extent_item) +
156 sizeof(struct btrfs_item));
Chris Masone37c9e62007-05-09 20:13:14 -0400157 return size;
158}
159
160static u64 find_search_start(struct btrfs_root *root,
161 struct btrfs_block_group_cache **cache_ret,
162 u64 search_start, int num)
163{
164 unsigned long gang[8];
165 int ret;
166 struct btrfs_block_group_cache *cache = *cache_ret;
167 u64 last = max(search_start, cache->key.objectid);
168
169 if (cache->data)
170 goto out;
Chris Masone37c9e62007-05-09 20:13:14 -0400171again:
Chris Mason54aa1f42007-06-22 14:16:25 -0400172 ret = cache_block_group(root, cache);
173 if (ret)
174 goto out;
Chris Masone37c9e62007-05-09 20:13:14 -0400175 while(1) {
176 ret = find_first_radix_bit(&root->fs_info->extent_map_radix,
177 gang, last, ARRAY_SIZE(gang));
178 if (!ret)
179 goto out;
180 last = gang[ret-1] + 1;
181 if (num > 1) {
182 if (ret != ARRAY_SIZE(gang)) {
183 goto new_group;
184 }
185 if (gang[ret-1] - gang[0] > leaf_range(root)) {
186 continue;
187 }
188 }
189 if (gang[0] >= cache->key.objectid + cache->key.offset) {
190 goto new_group;
191 }
192 return gang[0];
193 }
194out:
195 return max(cache->last_alloc, search_start);
196
197new_group:
Chris Mason5276aed2007-06-11 21:33:38 -0400198 cache = btrfs_lookup_block_group(root->fs_info,
199 last + cache->key.offset - 1);
Chris Masone37c9e62007-05-09 20:13:14 -0400200 if (!cache) {
201 return max((*cache_ret)->last_alloc, search_start);
202 }
203 cache = btrfs_find_block_group(root, cache,
Chris Masonde428b62007-05-18 13:28:27 -0400204 last + cache->key.offset - 1, 0, 0);
Chris Masone37c9e62007-05-09 20:13:14 -0400205 *cache_ret = cache;
206 goto again;
207}
208
Chris Mason84f54cf2007-06-12 07:43:08 -0400209static u64 div_factor(u64 num, int factor)
210{
211 num *= factor;
212 do_div(num, 10);
213 return num;
214}
215
Chris Mason31f3c992007-04-30 15:25:45 -0400216struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
217 struct btrfs_block_group_cache
Chris Masonbe744172007-05-06 10:15:01 -0400218 *hint, u64 search_start,
Chris Masonde428b62007-05-18 13:28:27 -0400219 int data, int owner)
Chris Masoncd1bc462007-04-27 10:08:34 -0400220{
221 struct btrfs_block_group_cache *cache[8];
Chris Mason31f3c992007-04-30 15:25:45 -0400222 struct btrfs_block_group_cache *found_group = NULL;
Chris Masoncd1bc462007-04-27 10:08:34 -0400223 struct btrfs_fs_info *info = root->fs_info;
Chris Masonbe744172007-05-06 10:15:01 -0400224 struct radix_tree_root *radix;
Chris Mason1e2677e2007-05-29 16:52:18 -0400225 struct radix_tree_root *swap_radix;
Chris Masoncd1bc462007-04-27 10:08:34 -0400226 u64 used;
Chris Mason31f3c992007-04-30 15:25:45 -0400227 u64 last = 0;
228 u64 hint_last;
Chris Masoncd1bc462007-04-27 10:08:34 -0400229 int i;
230 int ret;
Chris Mason31f3c992007-04-30 15:25:45 -0400231 int full_search = 0;
Chris Masonde428b62007-05-18 13:28:27 -0400232 int factor = 8;
Chris Mason1e2677e2007-05-29 16:52:18 -0400233 int data_swap = 0;
Chris Masonde428b62007-05-18 13:28:27 -0400234
235 if (!owner)
236 factor = 5;
Chris Masonbe744172007-05-06 10:15:01 -0400237
Chris Mason1e2677e2007-05-29 16:52:18 -0400238 if (data) {
Chris Masonbe744172007-05-06 10:15:01 -0400239 radix = &info->block_group_data_radix;
Chris Mason1e2677e2007-05-29 16:52:18 -0400240 swap_radix = &info->block_group_radix;
241 } else {
Chris Masonbe744172007-05-06 10:15:01 -0400242 radix = &info->block_group_radix;
Chris Mason1e2677e2007-05-29 16:52:18 -0400243 swap_radix = &info->block_group_data_radix;
244 }
Chris Masonbe744172007-05-06 10:15:01 -0400245
246 if (search_start) {
247 struct btrfs_block_group_cache *shint;
Chris Mason5276aed2007-06-11 21:33:38 -0400248 shint = btrfs_lookup_block_group(info, search_start);
Chris Masonbe744172007-05-06 10:15:01 -0400249 if (shint->data == data) {
250 used = btrfs_block_group_used(&shint->item);
251 if (used + shint->pinned <
Chris Mason84f54cf2007-06-12 07:43:08 -0400252 div_factor(shint->key.offset, factor)) {
Chris Masonbe744172007-05-06 10:15:01 -0400253 return shint;
254 }
255 }
256 }
257 if (hint && hint->data == data) {
Chris Mason31f3c992007-04-30 15:25:45 -0400258 used = btrfs_block_group_used(&hint->item);
Chris Mason84f54cf2007-06-12 07:43:08 -0400259 if (used + hint->pinned <
260 div_factor(hint->key.offset, factor)) {
Chris Mason31f3c992007-04-30 15:25:45 -0400261 return hint;
262 }
Chris Mason84f54cf2007-06-12 07:43:08 -0400263 if (used >= div_factor(hint->key.offset, 8)) {
Chris Masonbe744172007-05-06 10:15:01 -0400264 radix_tree_tag_clear(radix,
265 hint->key.objectid +
266 hint->key.offset - 1,
267 BTRFS_BLOCK_GROUP_AVAIL);
268 }
Chris Mason8d7be552007-05-10 11:24:42 -0400269 last = hint->key.offset * 3;
Chris Masonbe744172007-05-06 10:15:01 -0400270 if (hint->key.objectid >= last)
Chris Masone37c9e62007-05-09 20:13:14 -0400271 last = max(search_start + hint->key.offset - 1,
272 hint->key.objectid - last);
Chris Masonbe744172007-05-06 10:15:01 -0400273 else
274 last = hint->key.objectid + hint->key.offset;
Chris Mason31f3c992007-04-30 15:25:45 -0400275 hint_last = last;
276 } else {
Chris Masone37c9e62007-05-09 20:13:14 -0400277 if (hint)
278 hint_last = max(hint->key.objectid, search_start);
279 else
280 hint_last = search_start;
281
282 last = hint_last;
Chris Mason31f3c992007-04-30 15:25:45 -0400283 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400284 while(1) {
Chris Masonbe744172007-05-06 10:15:01 -0400285 ret = radix_tree_gang_lookup_tag(radix, (void **)cache,
Chris Masoncd1bc462007-04-27 10:08:34 -0400286 last, ARRAY_SIZE(cache),
Chris Mason31f3c992007-04-30 15:25:45 -0400287 BTRFS_BLOCK_GROUP_AVAIL);
Chris Masoncd1bc462007-04-27 10:08:34 -0400288 if (!ret)
289 break;
290 for (i = 0; i < ret; i++) {
Chris Masonbe08c1b2007-05-03 09:06:49 -0400291 last = cache[i]->key.objectid +
292 cache[i]->key.offset;
Chris Masoncd1bc462007-04-27 10:08:34 -0400293 used = btrfs_block_group_used(&cache[i]->item);
Chris Masonbe744172007-05-06 10:15:01 -0400294 if (used + cache[i]->pinned <
Chris Mason84f54cf2007-06-12 07:43:08 -0400295 div_factor(cache[i]->key.offset, factor)) {
Chris Mason31f3c992007-04-30 15:25:45 -0400296 found_group = cache[i];
297 goto found;
Chris Masoncd1bc462007-04-27 10:08:34 -0400298 }
Chris Mason84f54cf2007-06-12 07:43:08 -0400299 if (used >= div_factor(cache[i]->key.offset, 8)) {
Chris Masonbe744172007-05-06 10:15:01 -0400300 radix_tree_tag_clear(radix,
301 cache[i]->key.objectid +
302 cache[i]->key.offset - 1,
303 BTRFS_BLOCK_GROUP_AVAIL);
304 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400305 }
Chris Masonde428b62007-05-18 13:28:27 -0400306 cond_resched();
Chris Masoncd1bc462007-04-27 10:08:34 -0400307 }
Chris Mason31f3c992007-04-30 15:25:45 -0400308 last = hint_last;
309again:
Chris Masoncd1bc462007-04-27 10:08:34 -0400310 while(1) {
Chris Masonbe744172007-05-06 10:15:01 -0400311 ret = radix_tree_gang_lookup(radix, (void **)cache,
312 last, ARRAY_SIZE(cache));
Chris Masoncd1bc462007-04-27 10:08:34 -0400313 if (!ret)
314 break;
315 for (i = 0; i < ret; i++) {
Chris Masonbe08c1b2007-05-03 09:06:49 -0400316 last = cache[i]->key.objectid +
317 cache[i]->key.offset;
Chris Masoncd1bc462007-04-27 10:08:34 -0400318 used = btrfs_block_group_used(&cache[i]->item);
Chris Masonbe744172007-05-06 10:15:01 -0400319 if (used + cache[i]->pinned < cache[i]->key.offset) {
Chris Mason31f3c992007-04-30 15:25:45 -0400320 found_group = cache[i];
321 goto found;
Chris Masoncd1bc462007-04-27 10:08:34 -0400322 }
Chris Masonbe744172007-05-06 10:15:01 -0400323 if (used >= cache[i]->key.offset) {
324 radix_tree_tag_clear(radix,
325 cache[i]->key.objectid +
326 cache[i]->key.offset - 1,
327 BTRFS_BLOCK_GROUP_AVAIL);
328 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400329 }
Chris Masonde428b62007-05-18 13:28:27 -0400330 cond_resched();
Chris Masoncd1bc462007-04-27 10:08:34 -0400331 }
Chris Mason31f3c992007-04-30 15:25:45 -0400332 if (!full_search) {
Chris Masonbe744172007-05-06 10:15:01 -0400333 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -0400334 full_search = 1;
335 goto again;
336 }
Chris Mason1e2677e2007-05-29 16:52:18 -0400337 if (!data_swap) {
338 struct radix_tree_root *tmp = radix;
339 data_swap = 1;
340 radix = swap_radix;
341 swap_radix = tmp;
342 last = search_start;
343 goto again;
344 }
Chris Mason31f3c992007-04-30 15:25:45 -0400345 if (!found_group) {
Chris Masonbe744172007-05-06 10:15:01 -0400346 ret = radix_tree_gang_lookup(radix,
Chris Mason31f3c992007-04-30 15:25:45 -0400347 (void **)&found_group, 0, 1);
Chris Mason1e2677e2007-05-29 16:52:18 -0400348 if (ret == 0) {
349 ret = radix_tree_gang_lookup(swap_radix,
350 (void **)&found_group,
351 0, 1);
352 }
Chris Mason31f3c992007-04-30 15:25:45 -0400353 BUG_ON(ret != 1);
354 }
Chris Masonbe744172007-05-06 10:15:01 -0400355found:
Chris Mason31f3c992007-04-30 15:25:45 -0400356 return found_group;
Chris Masoncd1bc462007-04-27 10:08:34 -0400357}
358
Chris Masonb18c6682007-04-17 13:26:50 -0400359int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
360 struct btrfs_root *root,
361 u64 blocknr, u64 num_blocks)
Chris Mason02217ed2007-03-02 16:08:05 -0500362{
Chris Mason5caf2a02007-04-02 11:20:42 -0400363 struct btrfs_path *path;
Chris Mason02217ed2007-03-02 16:08:05 -0500364 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400365 struct btrfs_key key;
Chris Mason234b63a2007-03-13 10:46:10 -0400366 struct btrfs_leaf *l;
367 struct btrfs_extent_item *item;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400368 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500369
Chris Mason5caf2a02007-04-02 11:20:42 -0400370 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -0400371 if (!path)
372 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -0400373
Chris Mason02217ed2007-03-02 16:08:05 -0500374 key.objectid = blocknr;
375 key.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -0400376 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Mason6407bf62007-03-27 06:33:00 -0400377 key.offset = num_blocks;
Chris Mason5caf2a02007-04-02 11:20:42 -0400378 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -0400379 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400380 if (ret < 0)
381 return ret;
Chris Masona429e512007-04-18 16:15:28 -0400382 if (ret != 0) {
Chris Masona28ec192007-03-06 20:08:01 -0500383 BUG();
Chris Masona429e512007-04-18 16:15:28 -0400384 }
Chris Mason02217ed2007-03-02 16:08:05 -0500385 BUG_ON(ret != 0);
Chris Mason5caf2a02007-04-02 11:20:42 -0400386 l = btrfs_buffer_leaf(path->nodes[0]);
387 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400388 refs = btrfs_extent_refs(item);
389 btrfs_set_extent_refs(item, refs + 1);
Chris Mason5caf2a02007-04-02 11:20:42 -0400390 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masona28ec192007-03-06 20:08:01 -0500391
Chris Mason5caf2a02007-04-02 11:20:42 -0400392 btrfs_release_path(root->fs_info->extent_root, path);
393 btrfs_free_path(path);
Chris Mason9f5fae22007-03-20 14:38:32 -0400394 finish_current_insert(trans, root->fs_info->extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -0400395 del_pending_extents(trans, root->fs_info->extent_root);
Chris Mason02217ed2007-03-02 16:08:05 -0500396 return 0;
397}
398
Chris Masone9d0b132007-08-10 14:06:19 -0400399int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
400 struct btrfs_root *root)
401{
402 finish_current_insert(trans, root->fs_info->extent_root);
403 del_pending_extents(trans, root->fs_info->extent_root);
404 return 0;
405}
406
Chris Masonb18c6682007-04-17 13:26:50 -0400407static int lookup_extent_ref(struct btrfs_trans_handle *trans,
408 struct btrfs_root *root, u64 blocknr,
409 u64 num_blocks, u32 *refs)
Chris Masona28ec192007-03-06 20:08:01 -0500410{
Chris Mason5caf2a02007-04-02 11:20:42 -0400411 struct btrfs_path *path;
Chris Masona28ec192007-03-06 20:08:01 -0500412 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400413 struct btrfs_key key;
Chris Mason234b63a2007-03-13 10:46:10 -0400414 struct btrfs_leaf *l;
415 struct btrfs_extent_item *item;
Chris Mason5caf2a02007-04-02 11:20:42 -0400416
417 path = btrfs_alloc_path();
Chris Masona28ec192007-03-06 20:08:01 -0500418 key.objectid = blocknr;
Chris Mason6407bf62007-03-27 06:33:00 -0400419 key.offset = num_blocks;
Chris Mason62e27492007-03-15 12:56:47 -0400420 key.flags = 0;
421 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Mason5caf2a02007-04-02 11:20:42 -0400422 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -0400423 0, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400424 if (ret < 0)
425 goto out;
Chris Masona28ec192007-03-06 20:08:01 -0500426 if (ret != 0)
427 BUG();
Chris Mason5caf2a02007-04-02 11:20:42 -0400428 l = btrfs_buffer_leaf(path->nodes[0]);
429 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400430 *refs = btrfs_extent_refs(item);
Chris Mason54aa1f42007-06-22 14:16:25 -0400431out:
Chris Mason5caf2a02007-04-02 11:20:42 -0400432 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -0500433 return 0;
434}
435
Chris Masonc5739bb2007-04-10 09:27:04 -0400436int btrfs_inc_root_ref(struct btrfs_trans_handle *trans,
437 struct btrfs_root *root)
438{
Chris Masonb18c6682007-04-17 13:26:50 -0400439 return btrfs_inc_extent_ref(trans, root, bh_blocknr(root->node), 1);
Chris Masonc5739bb2007-04-10 09:27:04 -0400440}
441
Chris Masone089f052007-03-16 16:20:31 -0400442int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Masone20d96d2007-03-22 12:13:20 -0400443 struct buffer_head *buf)
Chris Mason02217ed2007-03-02 16:08:05 -0500444{
445 u64 blocknr;
Chris Masone20d96d2007-03-22 12:13:20 -0400446 struct btrfs_node *buf_node;
Chris Mason6407bf62007-03-27 06:33:00 -0400447 struct btrfs_leaf *buf_leaf;
448 struct btrfs_disk_key *key;
449 struct btrfs_file_extent_item *fi;
Chris Mason02217ed2007-03-02 16:08:05 -0500450 int i;
Chris Mason6407bf62007-03-27 06:33:00 -0400451 int leaf;
452 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -0400453 int faili;
454 int err;
Chris Masona28ec192007-03-06 20:08:01 -0500455
Chris Mason3768f362007-03-13 16:47:54 -0400456 if (!root->ref_cows)
Chris Masona28ec192007-03-06 20:08:01 -0500457 return 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400458 buf_node = btrfs_buffer_node(buf);
Chris Mason6407bf62007-03-27 06:33:00 -0400459 leaf = btrfs_is_leaf(buf_node);
460 buf_leaf = btrfs_buffer_leaf(buf);
Chris Masone20d96d2007-03-22 12:13:20 -0400461 for (i = 0; i < btrfs_header_nritems(&buf_node->header); i++) {
Chris Mason6407bf62007-03-27 06:33:00 -0400462 if (leaf) {
Chris Mason3a686372007-05-24 13:35:57 -0400463 u64 disk_blocknr;
Chris Mason6407bf62007-03-27 06:33:00 -0400464 key = &buf_leaf->items[i].key;
465 if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY)
466 continue;
467 fi = btrfs_item_ptr(buf_leaf, i,
468 struct btrfs_file_extent_item);
Chris Mason236454d2007-04-19 13:37:44 -0400469 if (btrfs_file_extent_type(fi) ==
470 BTRFS_FILE_EXTENT_INLINE)
471 continue;
Chris Mason3a686372007-05-24 13:35:57 -0400472 disk_blocknr = btrfs_file_extent_disk_blocknr(fi);
473 if (disk_blocknr == 0)
474 continue;
475 ret = btrfs_inc_extent_ref(trans, root, disk_blocknr,
Chris Mason6407bf62007-03-27 06:33:00 -0400476 btrfs_file_extent_disk_num_blocks(fi));
Chris Mason54aa1f42007-06-22 14:16:25 -0400477 if (ret) {
478 faili = i;
479 goto fail;
480 }
Chris Mason6407bf62007-03-27 06:33:00 -0400481 } else {
482 blocknr = btrfs_node_blockptr(buf_node, i);
Chris Masonb18c6682007-04-17 13:26:50 -0400483 ret = btrfs_inc_extent_ref(trans, root, blocknr, 1);
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 }
Chris Mason02217ed2007-03-02 16:08:05 -0500489 }
490 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -0400491fail:
Chris Masonccd467d2007-06-28 15:57:36 -0400492 WARN_ON(1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400493 for (i =0; i < faili; i++) {
494 if (leaf) {
495 u64 disk_blocknr;
496 key = &buf_leaf->items[i].key;
497 if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY)
498 continue;
499 fi = btrfs_item_ptr(buf_leaf, i,
500 struct btrfs_file_extent_item);
501 if (btrfs_file_extent_type(fi) ==
502 BTRFS_FILE_EXTENT_INLINE)
503 continue;
504 disk_blocknr = btrfs_file_extent_disk_blocknr(fi);
505 if (disk_blocknr == 0)
506 continue;
507 err = btrfs_free_extent(trans, root, disk_blocknr,
508 btrfs_file_extent_disk_num_blocks(fi), 0);
509 BUG_ON(err);
510 } else {
511 blocknr = btrfs_node_blockptr(buf_node, i);
512 err = btrfs_free_extent(trans, root, blocknr, 1, 0);
513 BUG_ON(err);
514 }
515 }
516 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -0500517}
518
Chris Mason9078a3e2007-04-26 16:46:15 -0400519static int write_one_cache_group(struct btrfs_trans_handle *trans,
520 struct btrfs_root *root,
521 struct btrfs_path *path,
522 struct btrfs_block_group_cache *cache)
523{
524 int ret;
525 int pending_ret;
526 struct btrfs_root *extent_root = root->fs_info->extent_root;
527 struct btrfs_block_group_item *bi;
Chris Mason9078a3e2007-04-26 16:46:15 -0400528
Chris Mason9078a3e2007-04-26 16:46:15 -0400529 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400530 if (ret < 0)
531 goto fail;
Chris Mason9078a3e2007-04-26 16:46:15 -0400532 BUG_ON(ret);
533 bi = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
534 struct btrfs_block_group_item);
535 memcpy(bi, &cache->item, sizeof(*bi));
Chris Masonccd467d2007-06-28 15:57:36 -0400536 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason9078a3e2007-04-26 16:46:15 -0400537 btrfs_release_path(extent_root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -0400538fail:
Chris Mason9078a3e2007-04-26 16:46:15 -0400539 finish_current_insert(trans, extent_root);
540 pending_ret = del_pending_extents(trans, extent_root);
541 if (ret)
542 return ret;
543 if (pending_ret)
544 return pending_ret;
Chris Masonbe744172007-05-06 10:15:01 -0400545 if (cache->data)
546 cache->last_alloc = cache->first_free;
Chris Mason9078a3e2007-04-26 16:46:15 -0400547 return 0;
548
549}
550
Chris Masonbe744172007-05-06 10:15:01 -0400551static int write_dirty_block_radix(struct btrfs_trans_handle *trans,
552 struct btrfs_root *root,
553 struct radix_tree_root *radix)
Chris Mason9078a3e2007-04-26 16:46:15 -0400554{
555 struct btrfs_block_group_cache *cache[8];
556 int ret;
557 int err = 0;
558 int werr = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -0400559 int i;
560 struct btrfs_path *path;
Chris Mason54aa1f42007-06-22 14:16:25 -0400561 unsigned long off = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -0400562
563 path = btrfs_alloc_path();
564 if (!path)
565 return -ENOMEM;
566
567 while(1) {
568 ret = radix_tree_gang_lookup_tag(radix, (void **)cache,
Chris Mason54aa1f42007-06-22 14:16:25 -0400569 off, ARRAY_SIZE(cache),
Chris Mason9078a3e2007-04-26 16:46:15 -0400570 BTRFS_BLOCK_GROUP_DIRTY);
571 if (!ret)
572 break;
573 for (i = 0; i < ret; i++) {
Chris Mason54aa1f42007-06-22 14:16:25 -0400574 err = write_one_cache_group(trans, root,
575 path, cache[i]);
576 /*
577 * if we fail to write the cache group, we want
578 * to keep it marked dirty in hopes that a later
579 * write will work
580 */
581 if (err) {
582 werr = err;
583 off = cache[i]->key.objectid +
584 cache[i]->key.offset;
585 continue;
586 }
587
Chris Mason9078a3e2007-04-26 16:46:15 -0400588 radix_tree_tag_clear(radix, cache[i]->key.objectid +
589 cache[i]->key.offset - 1,
590 BTRFS_BLOCK_GROUP_DIRTY);
Chris Mason9078a3e2007-04-26 16:46:15 -0400591 }
592 }
593 btrfs_free_path(path);
594 return werr;
595}
596
Chris Masonbe744172007-05-06 10:15:01 -0400597int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
598 struct btrfs_root *root)
599{
600 int ret;
601 int ret2;
602 ret = write_dirty_block_radix(trans, root,
603 &root->fs_info->block_group_radix);
604 ret2 = write_dirty_block_radix(trans, root,
605 &root->fs_info->block_group_data_radix);
606 if (ret)
607 return ret;
608 if (ret2)
609 return ret2;
610 return 0;
611}
612
Chris Mason9078a3e2007-04-26 16:46:15 -0400613static int update_block_group(struct btrfs_trans_handle *trans,
614 struct btrfs_root *root,
Chris Mason1e2677e2007-05-29 16:52:18 -0400615 u64 blocknr, u64 num, int alloc, int mark_free,
616 int data)
Chris Mason9078a3e2007-04-26 16:46:15 -0400617{
618 struct btrfs_block_group_cache *cache;
619 struct btrfs_fs_info *info = root->fs_info;
620 u64 total = num;
621 u64 old_val;
622 u64 block_in_group;
Chris Masone37c9e62007-05-09 20:13:14 -0400623 u64 i;
Chris Mason1e2677e2007-05-29 16:52:18 -0400624 int ret;
Chris Mason3e1ad542007-05-07 20:03:49 -0400625
Chris Mason9078a3e2007-04-26 16:46:15 -0400626 while(total) {
Chris Mason5276aed2007-06-11 21:33:38 -0400627 cache = btrfs_lookup_block_group(info, blocknr);
Chris Mason3e1ad542007-05-07 20:03:49 -0400628 if (!cache) {
Chris Mason9078a3e2007-04-26 16:46:15 -0400629 return -1;
Chris Masoncd1bc462007-04-27 10:08:34 -0400630 }
Chris Mason9078a3e2007-04-26 16:46:15 -0400631 block_in_group = blocknr - cache->key.objectid;
632 WARN_ON(block_in_group > cache->key.offset);
Chris Mason3e1ad542007-05-07 20:03:49 -0400633 radix_tree_tag_set(cache->radix, cache->key.objectid +
Chris Masonbe744172007-05-06 10:15:01 -0400634 cache->key.offset - 1,
Chris Mason9078a3e2007-04-26 16:46:15 -0400635 BTRFS_BLOCK_GROUP_DIRTY);
636
637 old_val = btrfs_block_group_used(&cache->item);
638 num = min(total, cache->key.offset - block_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -0400639 if (alloc) {
Chris Masoncd1bc462007-04-27 10:08:34 -0400640 if (blocknr > cache->last_alloc)
641 cache->last_alloc = blocknr;
Chris Masone37c9e62007-05-09 20:13:14 -0400642 if (!cache->data) {
643 for (i = 0; i < num; i++) {
644 clear_radix_bit(&info->extent_map_radix,
645 blocknr + i);
646 }
647 }
Chris Mason1e2677e2007-05-29 16:52:18 -0400648 if (cache->data != data &&
Chris Mason84f54cf2007-06-12 07:43:08 -0400649 old_val < (cache->key.offset >> 1)) {
Chris Mason1e2677e2007-05-29 16:52:18 -0400650 cache->data = data;
651 radix_tree_delete(cache->radix,
652 cache->key.objectid +
653 cache->key.offset - 1);
654
655 if (data) {
656 cache->radix =
657 &info->block_group_data_radix;
658 cache->item.flags |=
659 BTRFS_BLOCK_GROUP_DATA;
660 } else {
661 cache->radix = &info->block_group_radix;
662 cache->item.flags &=
663 ~BTRFS_BLOCK_GROUP_DATA;
664 }
665 ret = radix_tree_insert(cache->radix,
666 cache->key.objectid +
667 cache->key.offset - 1,
668 (void *)cache);
669 }
670 old_val += num;
Chris Masoncd1bc462007-04-27 10:08:34 -0400671 } else {
Chris Mason9078a3e2007-04-26 16:46:15 -0400672 old_val -= num;
Chris Masoncd1bc462007-04-27 10:08:34 -0400673 if (blocknr < cache->first_free)
674 cache->first_free = blocknr;
Chris Masone37c9e62007-05-09 20:13:14 -0400675 if (!cache->data && mark_free) {
676 for (i = 0; i < num; i++) {
677 set_radix_bit(&info->extent_map_radix,
678 blocknr + i);
679 }
680 }
Chris Mason84f54cf2007-06-12 07:43:08 -0400681 if (old_val < (cache->key.offset >> 1) &&
682 old_val + num >= (cache->key.offset >> 1)) {
Chris Masone37c9e62007-05-09 20:13:14 -0400683 radix_tree_tag_set(cache->radix,
684 cache->key.objectid +
685 cache->key.offset - 1,
686 BTRFS_BLOCK_GROUP_AVAIL);
687 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400688 }
Chris Mason9078a3e2007-04-26 16:46:15 -0400689 btrfs_set_block_group_used(&cache->item, old_val);
Chris Masone37c9e62007-05-09 20:13:14 -0400690 total -= num;
691 blocknr += num;
Chris Mason9078a3e2007-04-26 16:46:15 -0400692 }
693 return 0;
694}
695
Chris Masonccd467d2007-06-28 15:57:36 -0400696int btrfs_copy_pinned(struct btrfs_root *root, struct radix_tree_root *copy)
697{
698 unsigned long gang[8];
699 u64 last = 0;
700 struct radix_tree_root *pinned_radix = &root->fs_info->pinned_radix;
701 int ret;
702 int i;
703
704 while(1) {
705 ret = find_first_radix_bit(pinned_radix, gang, last,
706 ARRAY_SIZE(gang));
707 if (!ret)
708 break;
709 for (i = 0 ; i < ret; i++) {
710 set_radix_bit(copy, gang[i]);
711 last = gang[i] + 1;
712 }
713 }
Chris Mason26b80032007-08-08 20:17:12 -0400714 ret = find_first_radix_bit(&root->fs_info->extent_ins_radix, gang, 0,
715 ARRAY_SIZE(gang));
716 WARN_ON(ret);
Chris Masonccd467d2007-06-28 15:57:36 -0400717 return 0;
718}
719
720int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
721 struct btrfs_root *root,
722 struct radix_tree_root *unpin_radix)
Chris Masona28ec192007-03-06 20:08:01 -0500723{
Chris Mason8ef97622007-03-26 10:15:30 -0400724 unsigned long gang[8];
Chris Masonbe744172007-05-06 10:15:01 -0400725 struct btrfs_block_group_cache *block_group;
Chris Mason88fd1462007-03-16 08:56:18 -0400726 u64 first = 0;
Chris Masona28ec192007-03-06 20:08:01 -0500727 int ret;
728 int i;
Chris Mason8ef97622007-03-26 10:15:30 -0400729 struct radix_tree_root *pinned_radix = &root->fs_info->pinned_radix;
Chris Masone37c9e62007-05-09 20:13:14 -0400730 struct radix_tree_root *extent_radix = &root->fs_info->extent_map_radix;
Chris Masona28ec192007-03-06 20:08:01 -0500731
732 while(1) {
Chris Masonccd467d2007-06-28 15:57:36 -0400733 ret = find_first_radix_bit(unpin_radix, gang, 0,
Chris Mason8ef97622007-03-26 10:15:30 -0400734 ARRAY_SIZE(gang));
Chris Masona28ec192007-03-06 20:08:01 -0500735 if (!ret)
736 break;
Chris Mason88fd1462007-03-16 08:56:18 -0400737 if (!first)
Chris Mason8ef97622007-03-26 10:15:30 -0400738 first = gang[0];
Chris Mason0579da42007-03-07 16:15:30 -0500739 for (i = 0; i < ret; i++) {
Chris Mason8ef97622007-03-26 10:15:30 -0400740 clear_radix_bit(pinned_radix, gang[i]);
Chris Masonccd467d2007-06-28 15:57:36 -0400741 clear_radix_bit(unpin_radix, gang[i]);
Chris Mason5276aed2007-06-11 21:33:38 -0400742 block_group = btrfs_lookup_block_group(root->fs_info,
743 gang[i]);
Chris Masonbe744172007-05-06 10:15:01 -0400744 if (block_group) {
745 WARN_ON(block_group->pinned == 0);
746 block_group->pinned--;
747 if (gang[i] < block_group->last_alloc)
748 block_group->last_alloc = gang[i];
Chris Masone37c9e62007-05-09 20:13:14 -0400749 if (!block_group->data)
750 set_radix_bit(extent_radix, gang[i]);
Chris Masonbe744172007-05-06 10:15:01 -0400751 }
Chris Mason0579da42007-03-07 16:15:30 -0500752 }
Chris Masona28ec192007-03-06 20:08:01 -0500753 }
754 return 0;
755}
756
Chris Masone089f052007-03-16 16:20:31 -0400757static int finish_current_insert(struct btrfs_trans_handle *trans, struct
758 btrfs_root *extent_root)
Chris Mason037e6392007-03-07 11:50:24 -0500759{
Chris Masone2fa7222007-03-12 16:22:34 -0400760 struct btrfs_key ins;
Chris Mason234b63a2007-03-13 10:46:10 -0400761 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -0500762 int i;
763 int ret;
Chris Mason26b80032007-08-08 20:17:12 -0400764 int err;
765 unsigned long gang[8];
Chris Mason1261ec42007-03-20 20:35:03 -0400766 struct btrfs_fs_info *info = extent_root->fs_info;
Chris Mason037e6392007-03-07 11:50:24 -0500767
Chris Masoncf27e1e2007-03-13 09:49:06 -0400768 btrfs_set_extent_refs(&extent_item, 1);
Chris Mason037e6392007-03-07 11:50:24 -0500769 ins.offset = 1;
770 ins.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -0400771 btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
Chris Mason5d0c3e62007-04-23 17:01:05 -0400772 btrfs_set_extent_owner(&extent_item, extent_root->root_key.objectid);
Chris Mason037e6392007-03-07 11:50:24 -0500773
Chris Mason26b80032007-08-08 20:17:12 -0400774 while(1) {
775 ret = find_first_radix_bit(&info->extent_ins_radix, gang, 0,
776 ARRAY_SIZE(gang));
777 if (!ret)
778 break;
779
780 for (i = 0; i < ret; i++) {
781 ins.objectid = gang[i];
782 err = btrfs_insert_item(trans, extent_root, &ins,
783 &extent_item,
784 sizeof(extent_item));
785 clear_radix_bit(&info->extent_ins_radix, gang[i]);
786 WARN_ON(err);
787 }
Chris Mason037e6392007-03-07 11:50:24 -0500788 }
Chris Mason037e6392007-03-07 11:50:24 -0500789 return 0;
790}
791
Chris Mason8ef97622007-03-26 10:15:30 -0400792static int pin_down_block(struct btrfs_root *root, u64 blocknr, int pending)
Chris Masone20d96d2007-03-22 12:13:20 -0400793{
794 int err;
Chris Mason78fae272007-03-25 11:35:08 -0400795 struct btrfs_header *header;
Chris Mason8ef97622007-03-26 10:15:30 -0400796 struct buffer_head *bh;
Chris Mason78fae272007-03-25 11:35:08 -0400797
Chris Masonf4b9aa82007-03-27 11:05:53 -0400798 if (!pending) {
Chris Masond98237b2007-03-28 13:57:48 -0400799 bh = btrfs_find_tree_block(root, blocknr);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400800 if (bh) {
801 if (buffer_uptodate(bh)) {
802 u64 transid =
803 root->fs_info->running_transaction->transid;
804 header = btrfs_buffer_header(bh);
805 if (btrfs_header_generation(header) ==
806 transid) {
807 btrfs_block_release(root, bh);
808 return 0;
809 }
Chris Masonf4b9aa82007-03-27 11:05:53 -0400810 }
Chris Masond6025572007-03-30 14:27:56 -0400811 btrfs_block_release(root, bh);
Chris Mason8ef97622007-03-26 10:15:30 -0400812 }
Chris Mason8ef97622007-03-26 10:15:30 -0400813 err = set_radix_bit(&root->fs_info->pinned_radix, blocknr);
Chris Masonbe744172007-05-06 10:15:01 -0400814 if (!err) {
815 struct btrfs_block_group_cache *cache;
Chris Mason5276aed2007-06-11 21:33:38 -0400816 cache = btrfs_lookup_block_group(root->fs_info,
817 blocknr);
Chris Masonbe744172007-05-06 10:15:01 -0400818 if (cache)
819 cache->pinned++;
820 }
Chris Masonf4b9aa82007-03-27 11:05:53 -0400821 } else {
822 err = set_radix_bit(&root->fs_info->pending_del_radix, blocknr);
823 }
Chris Masonbe744172007-05-06 10:15:01 -0400824 BUG_ON(err < 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400825 return 0;
826}
827
Chris Masona28ec192007-03-06 20:08:01 -0500828/*
829 * remove an extent from the root, returns 0 on success
830 */
Chris Masone089f052007-03-16 16:20:31 -0400831static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masone37c9e62007-05-09 20:13:14 -0400832 *root, u64 blocknr, u64 num_blocks, int pin,
833 int mark_free)
Chris Masona28ec192007-03-06 20:08:01 -0500834{
Chris Mason5caf2a02007-04-02 11:20:42 -0400835 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -0400836 struct btrfs_key key;
Chris Mason1261ec42007-03-20 20:35:03 -0400837 struct btrfs_fs_info *info = root->fs_info;
838 struct btrfs_root *extent_root = info->extent_root;
Chris Masona28ec192007-03-06 20:08:01 -0500839 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400840 struct btrfs_extent_item *ei;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400841 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500842
Chris Masona28ec192007-03-06 20:08:01 -0500843 key.objectid = blocknr;
844 key.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -0400845 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masona28ec192007-03-06 20:08:01 -0500846 key.offset = num_blocks;
847
Chris Mason5caf2a02007-04-02 11:20:42 -0400848 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -0400849 if (!path)
850 return -ENOMEM;
851
Chris Mason5caf2a02007-04-02 11:20:42 -0400852 ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400853 if (ret < 0)
854 return ret;
855 BUG_ON(ret);
Chris Mason5caf2a02007-04-02 11:20:42 -0400856 ei = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
Chris Mason123abc82007-03-14 14:14:43 -0400857 struct btrfs_extent_item);
Chris Masona28ec192007-03-06 20:08:01 -0500858 BUG_ON(ei->refs == 0);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400859 refs = btrfs_extent_refs(ei) - 1;
860 btrfs_set_extent_refs(ei, refs);
Chris Mason5caf2a02007-04-02 11:20:42 -0400861 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400862 if (refs == 0) {
Chris Mason1261ec42007-03-20 20:35:03 -0400863 u64 super_blocks_used;
Chris Mason78fae272007-03-25 11:35:08 -0400864
865 if (pin) {
Chris Mason8ef97622007-03-26 10:15:30 -0400866 ret = pin_down_block(root, blocknr, 0);
Chris Mason78fae272007-03-25 11:35:08 -0400867 BUG_ON(ret);
868 }
869
Chris Mason4b52dff2007-06-26 10:06:50 -0400870 super_blocks_used = btrfs_super_blocks_used(&info->super_copy);
871 btrfs_set_super_blocks_used(&info->super_copy,
Chris Mason1261ec42007-03-20 20:35:03 -0400872 super_blocks_used - num_blocks);
Chris Mason5caf2a02007-04-02 11:20:42 -0400873 ret = btrfs_del_item(trans, extent_root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -0400874 if (ret) {
875 return ret;
876 }
Chris Masone37c9e62007-05-09 20:13:14 -0400877 ret = update_block_group(trans, root, blocknr, num_blocks, 0,
Chris Mason1e2677e2007-05-29 16:52:18 -0400878 mark_free, 0);
Chris Mason9078a3e2007-04-26 16:46:15 -0400879 BUG_ON(ret);
Chris Masona28ec192007-03-06 20:08:01 -0500880 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400881 btrfs_free_path(path);
Chris Masone089f052007-03-16 16:20:31 -0400882 finish_current_insert(trans, extent_root);
Chris Masona28ec192007-03-06 20:08:01 -0500883 return ret;
884}
885
886/*
Chris Masonfec577f2007-02-26 10:40:21 -0500887 * find all the blocks marked as pending in the radix tree and remove
888 * them from the extent map
889 */
Chris Masone089f052007-03-16 16:20:31 -0400890static int del_pending_extents(struct btrfs_trans_handle *trans, struct
891 btrfs_root *extent_root)
Chris Masonfec577f2007-02-26 10:40:21 -0500892{
893 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -0400894 int wret;
895 int err = 0;
Chris Mason8ef97622007-03-26 10:15:30 -0400896 unsigned long gang[4];
Chris Masonfec577f2007-02-26 10:40:21 -0500897 int i;
Chris Mason8ef97622007-03-26 10:15:30 -0400898 struct radix_tree_root *pending_radix;
899 struct radix_tree_root *pinned_radix;
Chris Masonbe744172007-05-06 10:15:01 -0400900 struct btrfs_block_group_cache *cache;
Chris Mason8ef97622007-03-26 10:15:30 -0400901
902 pending_radix = &extent_root->fs_info->pending_del_radix;
903 pinned_radix = &extent_root->fs_info->pinned_radix;
Chris Masonfec577f2007-02-26 10:40:21 -0500904
905 while(1) {
Chris Masone37c9e62007-05-09 20:13:14 -0400906 ret = find_first_radix_bit(pending_radix, gang, 0,
Chris Mason8ef97622007-03-26 10:15:30 -0400907 ARRAY_SIZE(gang));
Chris Masonfec577f2007-02-26 10:40:21 -0500908 if (!ret)
909 break;
910 for (i = 0; i < ret; i++) {
Chris Mason8ef97622007-03-26 10:15:30 -0400911 wret = set_radix_bit(pinned_radix, gang[i]);
Chris Masonbe744172007-05-06 10:15:01 -0400912 if (wret == 0) {
Chris Mason5276aed2007-06-11 21:33:38 -0400913 cache =
914 btrfs_lookup_block_group(extent_root->fs_info,
Chris Masonbe744172007-05-06 10:15:01 -0400915 gang[i]);
916 if (cache)
917 cache->pinned++;
918 }
919 if (wret < 0) {
920 printk(KERN_CRIT "set_radix_bit, err %d\n",
921 wret);
922 BUG_ON(wret < 0);
923 }
Chris Mason8ef97622007-03-26 10:15:30 -0400924 wret = clear_radix_bit(pending_radix, gang[i]);
925 BUG_ON(wret);
Chris Masond5719762007-03-23 10:01:08 -0400926 wret = __free_extent(trans, extent_root,
Chris Masone37c9e62007-05-09 20:13:14 -0400927 gang[i], 1, 0, 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400928 if (wret)
929 err = wret;
Chris Masonfec577f2007-02-26 10:40:21 -0500930 }
931 }
Chris Masone20d96d2007-03-22 12:13:20 -0400932 return err;
Chris Masonfec577f2007-02-26 10:40:21 -0500933}
934
935/*
936 * remove an extent from the root, returns 0 on success
937 */
Chris Masone089f052007-03-16 16:20:31 -0400938int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
939 *root, u64 blocknr, u64 num_blocks, int pin)
Chris Masonfec577f2007-02-26 10:40:21 -0500940{
Chris Mason9f5fae22007-03-20 14:38:32 -0400941 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Masonfec577f2007-02-26 10:40:21 -0500942 int pending_ret;
943 int ret;
Chris Masona28ec192007-03-06 20:08:01 -0500944
945 if (root == extent_root) {
Chris Mason8ef97622007-03-26 10:15:30 -0400946 pin_down_block(root, blocknr, 1);
Chris Masona28ec192007-03-06 20:08:01 -0500947 return 0;
948 }
Chris Masone37c9e62007-05-09 20:13:14 -0400949 ret = __free_extent(trans, root, blocknr, num_blocks, pin, pin == 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400950 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
Chris Masonfec577f2007-02-26 10:40:21 -0500951 return ret ? ret : pending_ret;
952}
953
954/*
955 * walks the btree of allocated extents and find a hole of a given size.
956 * The key ins is changed to record the hole:
957 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -0400958 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -0500959 * ins->offset == number of blocks
960 * Any available blocks before search_start are skipped.
961 */
Chris Masone089f052007-03-16 16:20:31 -0400962static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason6702ed42007-08-07 16:15:09 -0400963 *orig_root, u64 num_blocks, u64 empty_size,
964 u64 search_start, u64 search_end, u64 hint_block,
Chris Masonf2654de2007-06-26 12:20:46 -0400965 struct btrfs_key *ins, u64 exclude_start,
966 u64 exclude_nr, int data)
Chris Masonfec577f2007-02-26 10:40:21 -0500967{
Chris Mason5caf2a02007-04-02 11:20:42 -0400968 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -0400969 struct btrfs_key key;
Chris Masonfec577f2007-02-26 10:40:21 -0500970 int ret;
971 u64 hole_size = 0;
972 int slot = 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400973 u64 last_block = 0;
Chris Mason037e6392007-03-07 11:50:24 -0500974 u64 test_block;
Chris Masonbe744172007-05-06 10:15:01 -0400975 u64 orig_search_start = search_start;
Chris Masonfec577f2007-02-26 10:40:21 -0500976 int start_found;
Chris Mason234b63a2007-03-13 10:46:10 -0400977 struct btrfs_leaf *l;
Chris Mason9f5fae22007-03-20 14:38:32 -0400978 struct btrfs_root * root = orig_root->fs_info->extent_root;
Chris Masonf2458e12007-04-25 15:52:25 -0400979 struct btrfs_fs_info *info = root->fs_info;
Chris Mason0579da42007-03-07 16:15:30 -0500980 int total_needed = num_blocks;
Chris Masone20d96d2007-03-22 12:13:20 -0400981 int level;
Chris Masonbe08c1b2007-05-03 09:06:49 -0400982 struct btrfs_block_group_cache *block_group;
Chris Masonbe744172007-05-06 10:15:01 -0400983 int full_scan = 0;
Chris Masonfbdc7622007-05-30 10:22:12 -0400984 int wrapped = 0;
Chris Masonde428b62007-05-18 13:28:27 -0400985 u64 limit;
Chris Masonfec577f2007-02-26 10:40:21 -0500986
Chris Mason26b80032007-08-08 20:17:12 -0400987 WARN_ON(num_blocks < 1);
Chris Masonb1a4d962007-04-04 15:27:52 -0400988 ins->flags = 0;
989 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
990
Chris Masone20d96d2007-03-22 12:13:20 -0400991 level = btrfs_header_level(btrfs_buffer_header(root->node));
Chris Mason3e1ad542007-05-07 20:03:49 -0400992 if (search_end == (u64)-1)
Chris Mason4b52dff2007-06-26 10:06:50 -0400993 search_end = btrfs_super_total_blocks(&info->super_copy);
Chris Masonfbdc7622007-05-30 10:22:12 -0400994 if (hint_block) {
Chris Mason5276aed2007-06-11 21:33:38 -0400995 block_group = btrfs_lookup_block_group(info, hint_block);
Chris Masonbe744172007-05-06 10:15:01 -0400996 block_group = btrfs_find_block_group(root, block_group,
Chris Masonfbdc7622007-05-30 10:22:12 -0400997 hint_block, data, 1);
Chris Masonbe744172007-05-06 10:15:01 -0400998 } else {
999 block_group = btrfs_find_block_group(root,
1000 trans->block_group, 0,
Chris Masonde428b62007-05-18 13:28:27 -04001001 data, 1);
Chris Masonbe744172007-05-06 10:15:01 -04001002 }
1003
Chris Mason6702ed42007-08-07 16:15:09 -04001004 total_needed += empty_size;
Chris Masone0115992007-06-19 16:23:05 -04001005 path = btrfs_alloc_path();
1006
Chris Masonbe744172007-05-06 10:15:01 -04001007check_failed:
Chris Mason1e2677e2007-05-29 16:52:18 -04001008 if (!block_group->data)
Chris Masone37c9e62007-05-09 20:13:14 -04001009 search_start = find_search_start(root, &block_group,
1010 search_start, total_needed);
Chris Masonfbdc7622007-05-30 10:22:12 -04001011 else if (!full_scan)
Chris Masone37c9e62007-05-09 20:13:14 -04001012 search_start = max(block_group->last_alloc, search_start);
1013
Chris Mason5caf2a02007-04-02 11:20:42 -04001014 btrfs_init_path(path);
Chris Masonfec577f2007-02-26 10:40:21 -05001015 ins->objectid = search_start;
1016 ins->offset = 0;
Chris Masonfec577f2007-02-26 10:40:21 -05001017 start_found = 0;
Chris Masone37c9e62007-05-09 20:13:14 -04001018
Chris Mason5caf2a02007-04-02 11:20:42 -04001019 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
Chris Mason0f70abe2007-02-28 16:46:22 -05001020 if (ret < 0)
1021 goto error;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001022
Chris Masone37c9e62007-05-09 20:13:14 -04001023 if (path->slots[0] > 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001024 path->slots[0]--;
Chris Masone37c9e62007-05-09 20:13:14 -04001025 }
1026
1027 l = btrfs_buffer_leaf(path->nodes[0]);
1028 btrfs_disk_key_to_cpu(&key, &l->items[path->slots[0]].key);
1029 /*
1030 * a rare case, go back one key if we hit a block group item
1031 * instead of an extent item
1032 */
1033 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY &&
1034 key.objectid + key.offset >= search_start) {
1035 ins->objectid = key.objectid;
1036 ins->offset = key.offset - 1;
1037 btrfs_release_path(root, path);
1038 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
1039 if (ret < 0)
1040 goto error;
1041
1042 if (path->slots[0] > 0) {
1043 path->slots[0]--;
1044 }
1045 }
Chris Mason0579da42007-03-07 16:15:30 -05001046
Chris Masonfec577f2007-02-26 10:40:21 -05001047 while (1) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001048 l = btrfs_buffer_leaf(path->nodes[0]);
1049 slot = path->slots[0];
Chris Mason7518a232007-03-12 12:01:18 -04001050 if (slot >= btrfs_header_nritems(&l->header)) {
Chris Masonde428b62007-05-18 13:28:27 -04001051 if (start_found)
1052 limit = last_block +
Chris Mason84f54cf2007-06-12 07:43:08 -04001053 (block_group->key.offset >> 1);
Chris Masonde428b62007-05-18 13:28:27 -04001054 else
1055 limit = search_start +
Chris Mason84f54cf2007-06-12 07:43:08 -04001056 (block_group->key.offset >> 1);
Chris Mason5caf2a02007-04-02 11:20:42 -04001057 ret = btrfs_next_leaf(root, path);
Chris Masonfec577f2007-02-26 10:40:21 -05001058 if (ret == 0)
1059 continue;
Chris Mason0f70abe2007-02-28 16:46:22 -05001060 if (ret < 0)
1061 goto error;
Chris Masonfec577f2007-02-26 10:40:21 -05001062 if (!start_found) {
1063 ins->objectid = search_start;
Chris Mason3e1ad542007-05-07 20:03:49 -04001064 ins->offset = search_end - search_start;
Chris Masonfec577f2007-02-26 10:40:21 -05001065 start_found = 1;
1066 goto check_pending;
1067 }
1068 ins->objectid = last_block > search_start ?
1069 last_block : search_start;
Chris Mason3e1ad542007-05-07 20:03:49 -04001070 ins->offset = search_end - ins->objectid;
Chris Masonfec577f2007-02-26 10:40:21 -05001071 goto check_pending;
1072 }
Chris Masone37c9e62007-05-09 20:13:14 -04001073
Chris Masone2fa7222007-03-12 16:22:34 -04001074 btrfs_disk_key_to_cpu(&key, &l->items[slot].key);
Chris Masone37c9e62007-05-09 20:13:14 -04001075 if (key.objectid >= search_start && key.objectid > last_block &&
1076 start_found) {
1077 if (last_block < search_start)
1078 last_block = search_start;
1079 hole_size = key.objectid - last_block;
1080 if (hole_size >= num_blocks) {
1081 ins->objectid = last_block;
1082 ins->offset = hole_size;
1083 goto check_pending;
Chris Mason0579da42007-03-07 16:15:30 -05001084 }
Chris Masonfec577f2007-02-26 10:40:21 -05001085 }
Chris Masone37c9e62007-05-09 20:13:14 -04001086
1087 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY)
1088 goto next;
1089
Chris Mason0579da42007-03-07 16:15:30 -05001090 start_found = 1;
Chris Masone2fa7222007-03-12 16:22:34 -04001091 last_block = key.objectid + key.offset;
Chris Masonfbdc7622007-05-30 10:22:12 -04001092 if (!full_scan && last_block >= block_group->key.objectid +
Chris Masonbe744172007-05-06 10:15:01 -04001093 block_group->key.offset) {
1094 btrfs_release_path(root, path);
1095 search_start = block_group->key.objectid +
1096 block_group->key.offset * 2;
1097 goto new_group;
1098 }
Chris Mason9078a3e2007-04-26 16:46:15 -04001099next:
Chris Mason5caf2a02007-04-02 11:20:42 -04001100 path->slots[0]++;
Chris Masonde428b62007-05-18 13:28:27 -04001101 cond_resched();
Chris Masonfec577f2007-02-26 10:40:21 -05001102 }
Chris Masonfec577f2007-02-26 10:40:21 -05001103check_pending:
1104 /* we have to make sure we didn't find an extent that has already
1105 * been allocated by the map tree or the original allocation
1106 */
Chris Mason5caf2a02007-04-02 11:20:42 -04001107 btrfs_release_path(root, path);
Chris Masonfec577f2007-02-26 10:40:21 -05001108 BUG_ON(ins->objectid < search_start);
Chris Masone37c9e62007-05-09 20:13:14 -04001109
Chris Mason3e1ad542007-05-07 20:03:49 -04001110 if (ins->objectid + num_blocks >= search_end) {
Chris Masonfbdc7622007-05-30 10:22:12 -04001111 if (full_scan) {
1112 ret = -ENOSPC;
1113 goto error;
1114 }
Chris Masonbe744172007-05-06 10:15:01 -04001115 search_start = orig_search_start;
Chris Mason6702ed42007-08-07 16:15:09 -04001116 if (wrapped) {
1117 if (!full_scan)
1118 total_needed -= empty_size;
Chris Masonfbdc7622007-05-30 10:22:12 -04001119 full_scan = 1;
Chris Mason6702ed42007-08-07 16:15:09 -04001120 } else
Chris Masonfbdc7622007-05-30 10:22:12 -04001121 wrapped = 1;
Chris Masonbe744172007-05-06 10:15:01 -04001122 goto new_group;
Chris Mason06a2f9f2007-04-28 08:48:10 -04001123 }
Chris Mason037e6392007-03-07 11:50:24 -05001124 for (test_block = ins->objectid;
Chris Masonf2458e12007-04-25 15:52:25 -04001125 test_block < ins->objectid + num_blocks; test_block++) {
Chris Mason26b80032007-08-08 20:17:12 -04001126 if (test_radix_bit(&info->pinned_radix, test_block) ||
1127 test_radix_bit(&info->extent_ins_radix, test_block)) {
Chris Mason037e6392007-03-07 11:50:24 -05001128 search_start = test_block + 1;
Chris Masonbe744172007-05-06 10:15:01 -04001129 goto new_group;
Chris Masonfec577f2007-02-26 10:40:21 -05001130 }
1131 }
Chris Masonf2654de2007-06-26 12:20:46 -04001132 if (exclude_nr > 0 && (ins->objectid + num_blocks > exclude_start &&
1133 ins->objectid < exclude_start + exclude_nr)) {
1134 search_start = exclude_start + exclude_nr;
1135 goto new_group;
1136 }
Chris Masone37c9e62007-05-09 20:13:14 -04001137 if (!data) {
Chris Mason5276aed2007-06-11 21:33:38 -04001138 block_group = btrfs_lookup_block_group(info, ins->objectid);
Chris Mason26b80032007-08-08 20:17:12 -04001139 if (block_group)
1140 trans->block_group = block_group;
Chris Masoncd1bc462007-04-27 10:08:34 -04001141 }
Chris Mason037e6392007-03-07 11:50:24 -05001142 ins->offset = num_blocks;
Chris Mason5caf2a02007-04-02 11:20:42 -04001143 btrfs_free_path(path);
Chris Masonfec577f2007-02-26 10:40:21 -05001144 return 0;
Chris Masonbe744172007-05-06 10:15:01 -04001145
1146new_group:
Chris Mason3e1ad542007-05-07 20:03:49 -04001147 if (search_start + num_blocks >= search_end) {
Chris Masonbe744172007-05-06 10:15:01 -04001148 search_start = orig_search_start;
Chris Masonfbdc7622007-05-30 10:22:12 -04001149 if (full_scan) {
1150 ret = -ENOSPC;
1151 goto error;
1152 }
Chris Mason6702ed42007-08-07 16:15:09 -04001153 if (wrapped) {
1154 if (!full_scan)
1155 total_needed -= empty_size;
Chris Masonfbdc7622007-05-30 10:22:12 -04001156 full_scan = 1;
Chris Mason6702ed42007-08-07 16:15:09 -04001157 } else
Chris Masonfbdc7622007-05-30 10:22:12 -04001158 wrapped = 1;
Chris Masonbe744172007-05-06 10:15:01 -04001159 }
Chris Mason5276aed2007-06-11 21:33:38 -04001160 block_group = btrfs_lookup_block_group(info, search_start);
Chris Masonfbdc7622007-05-30 10:22:12 -04001161 cond_resched();
Chris Masonbe744172007-05-06 10:15:01 -04001162 if (!full_scan)
1163 block_group = btrfs_find_block_group(root, block_group,
Chris Masonde428b62007-05-18 13:28:27 -04001164 search_start, data, 0);
Chris Masonbe744172007-05-06 10:15:01 -04001165 goto check_failed;
1166
Chris Mason0f70abe2007-02-28 16:46:22 -05001167error:
Chris Mason5caf2a02007-04-02 11:20:42 -04001168 btrfs_release_path(root, path);
1169 btrfs_free_path(path);
Chris Mason0f70abe2007-02-28 16:46:22 -05001170 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05001171}
Chris Masonfec577f2007-02-26 10:40:21 -05001172/*
Chris Masonfec577f2007-02-26 10:40:21 -05001173 * finds a free extent and does all the dirty work required for allocation
1174 * returns the key for the extent through ins, and a tree buffer for
1175 * the first block of the extent through buf.
1176 *
1177 * returns 0 if everything worked, non-zero otherwise.
1178 */
Chris Mason4d775672007-04-20 20:23:12 -04001179int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
1180 struct btrfs_root *root, u64 owner,
Chris Mason6702ed42007-08-07 16:15:09 -04001181 u64 num_blocks, u64 empty_size, u64 hint_block,
Chris Masonbe08c1b2007-05-03 09:06:49 -04001182 u64 search_end, struct btrfs_key *ins, int data)
Chris Masonfec577f2007-02-26 10:40:21 -05001183{
1184 int ret;
1185 int pending_ret;
Chris Mason1261ec42007-03-20 20:35:03 -04001186 u64 super_blocks_used;
Chris Masonfbdc7622007-05-30 10:22:12 -04001187 u64 search_start = 0;
Chris Mason1261ec42007-03-20 20:35:03 -04001188 struct btrfs_fs_info *info = root->fs_info;
1189 struct btrfs_root *extent_root = info->extent_root;
Chris Mason234b63a2007-03-13 10:46:10 -04001190 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -05001191
Chris Masoncf27e1e2007-03-13 09:49:06 -04001192 btrfs_set_extent_refs(&extent_item, 1);
Chris Mason4d775672007-04-20 20:23:12 -04001193 btrfs_set_extent_owner(&extent_item, owner);
Chris Masonfec577f2007-02-26 10:40:21 -05001194
Chris Mason26b80032007-08-08 20:17:12 -04001195 WARN_ON(num_blocks < 1);
Chris Mason6702ed42007-08-07 16:15:09 -04001196 ret = find_free_extent(trans, root, num_blocks, empty_size,
1197 search_start, search_end, hint_block, ins,
Chris Mason26b80032007-08-08 20:17:12 -04001198 trans->alloc_exclude_start,
1199 trans->alloc_exclude_nr, data);
Chris Masonccd467d2007-06-28 15:57:36 -04001200 BUG_ON(ret);
Chris Masonf2654de2007-06-26 12:20:46 -04001201 if (ret)
1202 return ret;
1203
Chris Mason4b52dff2007-06-26 10:06:50 -04001204 super_blocks_used = btrfs_super_blocks_used(&info->super_copy);
1205 btrfs_set_super_blocks_used(&info->super_copy, super_blocks_used +
Chris Mason1261ec42007-03-20 20:35:03 -04001206 num_blocks);
Chris Mason26b80032007-08-08 20:17:12 -04001207
1208 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,
Chris Mason6702ed42007-08-07 16:15:09 -04001253 1, empty_size, hint,
1254 (unsigned long)-1, &ins, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05001255 if (ret) {
Chris Mason54aa1f42007-06-22 14:16:25 -04001256 BUG_ON(ret > 0);
1257 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05001258 }
Chris Masond98237b2007-03-28 13:57:48 -04001259 buf = btrfs_find_create_tree_block(root, ins.objectid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001260 if (!buf) {
1261 btrfs_free_extent(trans, root, ins.objectid, 1, 0);
1262 return ERR_PTR(-ENOMEM);
1263 }
Chris Mason6702ed42007-08-07 16:15:09 -04001264 WARN_ON(buffer_dirty(buf));
Chris Masondf2ce342007-03-23 11:00:45 -04001265 set_buffer_uptodate(buf);
Chris Mason090d1872007-05-01 08:53:32 -04001266 set_buffer_checked(buf);
Chris Mason7c4452b2007-04-28 09:29:35 -04001267 set_radix_bit(&trans->transaction->dirty_pages, buf->b_page->index);
Chris Masonfec577f2007-02-26 10:40:21 -05001268 return buf;
1269}
Chris Masona28ec192007-03-06 20:08:01 -05001270
Chris Mason6407bf62007-03-27 06:33:00 -04001271static int drop_leaf_ref(struct btrfs_trans_handle *trans,
1272 struct btrfs_root *root, struct buffer_head *cur)
1273{
1274 struct btrfs_disk_key *key;
1275 struct btrfs_leaf *leaf;
1276 struct btrfs_file_extent_item *fi;
1277 int i;
1278 int nritems;
1279 int ret;
1280
1281 BUG_ON(!btrfs_is_leaf(btrfs_buffer_node(cur)));
1282 leaf = btrfs_buffer_leaf(cur);
1283 nritems = btrfs_header_nritems(&leaf->header);
1284 for (i = 0; i < nritems; i++) {
Chris Mason3a686372007-05-24 13:35:57 -04001285 u64 disk_blocknr;
Chris Mason6407bf62007-03-27 06:33:00 -04001286 key = &leaf->items[i].key;
1287 if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY)
1288 continue;
1289 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
Chris Mason236454d2007-04-19 13:37:44 -04001290 if (btrfs_file_extent_type(fi) == BTRFS_FILE_EXTENT_INLINE)
1291 continue;
Chris Mason6407bf62007-03-27 06:33:00 -04001292 /*
1293 * FIXME make sure to insert a trans record that
1294 * repeats the snapshot del on crash
1295 */
Chris Mason3a686372007-05-24 13:35:57 -04001296 disk_blocknr = btrfs_file_extent_disk_blocknr(fi);
1297 if (disk_blocknr == 0)
1298 continue;
1299 ret = btrfs_free_extent(trans, root, disk_blocknr,
Chris Mason6407bf62007-03-27 06:33:00 -04001300 btrfs_file_extent_disk_num_blocks(fi),
1301 0);
1302 BUG_ON(ret);
1303 }
1304 return 0;
1305}
1306
Chris Masone0115992007-06-19 16:23:05 -04001307static void reada_walk_down(struct btrfs_root *root,
1308 struct btrfs_node *node)
1309{
1310 int i;
1311 u32 nritems;
1312 u64 blocknr;
1313 int ret;
1314 u32 refs;
1315
1316 nritems = btrfs_header_nritems(&node->header);
1317 for (i = 0; i < nritems; i++) {
1318 blocknr = btrfs_node_blockptr(node, i);
1319 ret = lookup_extent_ref(NULL, root, blocknr, 1, &refs);
1320 BUG_ON(ret);
1321 if (refs != 1)
1322 continue;
Chris Mason409eb952007-08-08 20:17:12 -04001323 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masone0115992007-06-19 16:23:05 -04001324 ret = readahead_tree_block(root, blocknr);
Chris Mason409eb952007-08-08 20:17:12 -04001325 cond_resched();
1326 mutex_lock(&root->fs_info->fs_mutex);
Chris Masone0115992007-06-19 16:23:05 -04001327 if (ret)
1328 break;
1329 }
1330}
1331
Chris Mason9aca1d52007-03-13 11:09:37 -04001332/*
1333 * helper function for drop_snapshot, this walks down the tree dropping ref
1334 * counts as it goes.
1335 */
Chris Masone089f052007-03-16 16:20:31 -04001336static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
1337 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05001338{
Chris Masone20d96d2007-03-22 12:13:20 -04001339 struct buffer_head *next;
1340 struct buffer_head *cur;
Chris Mason20524f02007-03-10 06:35:47 -05001341 u64 blocknr;
1342 int ret;
1343 u32 refs;
1344
Chris Mason5caf2a02007-04-02 11:20:42 -04001345 WARN_ON(*level < 0);
1346 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Masonb18c6682007-04-17 13:26:50 -04001347 ret = lookup_extent_ref(trans, root, bh_blocknr(path->nodes[*level]),
Chris Mason6407bf62007-03-27 06:33:00 -04001348 1, &refs);
Chris Mason20524f02007-03-10 06:35:47 -05001349 BUG_ON(ret);
1350 if (refs > 1)
1351 goto out;
Chris Masone0115992007-06-19 16:23:05 -04001352
Chris Mason9aca1d52007-03-13 11:09:37 -04001353 /*
1354 * walk down to the last node level and free all the leaves
1355 */
Chris Mason6407bf62007-03-27 06:33:00 -04001356 while(*level >= 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001357 WARN_ON(*level < 0);
1358 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason20524f02007-03-10 06:35:47 -05001359 cur = path->nodes[*level];
Chris Masone0115992007-06-19 16:23:05 -04001360
1361 if (*level > 0 && path->slots[*level] == 0)
1362 reada_walk_down(root, btrfs_buffer_node(cur));
1363
Chris Mason2c90e5d2007-04-02 10:50:19 -04001364 if (btrfs_header_level(btrfs_buffer_header(cur)) != *level)
1365 WARN_ON(1);
Chris Masone0115992007-06-19 16:23:05 -04001366
Chris Mason7518a232007-03-12 12:01:18 -04001367 if (path->slots[*level] >=
Chris Masone20d96d2007-03-22 12:13:20 -04001368 btrfs_header_nritems(btrfs_buffer_header(cur)))
Chris Mason20524f02007-03-10 06:35:47 -05001369 break;
Chris Mason6407bf62007-03-27 06:33:00 -04001370 if (*level == 0) {
1371 ret = drop_leaf_ref(trans, root, cur);
1372 BUG_ON(ret);
1373 break;
1374 }
Chris Masone20d96d2007-03-22 12:13:20 -04001375 blocknr = btrfs_node_blockptr(btrfs_buffer_node(cur),
1376 path->slots[*level]);
Chris Masonb18c6682007-04-17 13:26:50 -04001377 ret = lookup_extent_ref(trans, root, blocknr, 1, &refs);
Chris Mason6407bf62007-03-27 06:33:00 -04001378 BUG_ON(ret);
1379 if (refs != 1) {
Chris Mason20524f02007-03-10 06:35:47 -05001380 path->slots[*level]++;
Chris Masone089f052007-03-16 16:20:31 -04001381 ret = btrfs_free_extent(trans, root, blocknr, 1, 1);
Chris Mason20524f02007-03-10 06:35:47 -05001382 BUG_ON(ret);
1383 continue;
1384 }
Chris Masone9d0b132007-08-10 14:06:19 -04001385 next = btrfs_find_tree_block(root, blocknr);
1386 if (!next || !buffer_uptodate(next)) {
1387 brelse(next);
1388 mutex_unlock(&root->fs_info->fs_mutex);
1389 next = read_tree_block(root, blocknr);
1390 mutex_lock(&root->fs_info->fs_mutex);
1391
1392 /* we dropped the lock, check one more time */
1393 ret = lookup_extent_ref(trans, root, blocknr, 1, &refs);
1394 BUG_ON(ret);
1395 if (refs != 1) {
1396 path->slots[*level]++;
1397 brelse(next);
1398 ret = btrfs_free_extent(trans, root,
1399 blocknr, 1, 1);
1400 BUG_ON(ret);
1401 continue;
1402 }
1403 }
Chris Mason5caf2a02007-04-02 11:20:42 -04001404 WARN_ON(*level <= 0);
Chris Mason83e15a22007-03-12 09:03:27 -04001405 if (path->nodes[*level-1])
Chris Mason234b63a2007-03-13 10:46:10 -04001406 btrfs_block_release(root, path->nodes[*level-1]);
Chris Mason20524f02007-03-10 06:35:47 -05001407 path->nodes[*level-1] = next;
Chris Masone20d96d2007-03-22 12:13:20 -04001408 *level = btrfs_header_level(btrfs_buffer_header(next));
Chris Mason20524f02007-03-10 06:35:47 -05001409 path->slots[*level] = 0;
1410 }
1411out:
Chris Mason5caf2a02007-04-02 11:20:42 -04001412 WARN_ON(*level < 0);
1413 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason6407bf62007-03-27 06:33:00 -04001414 ret = btrfs_free_extent(trans, root,
Chris Mason7eccb902007-04-11 15:53:25 -04001415 bh_blocknr(path->nodes[*level]), 1, 1);
Chris Mason234b63a2007-03-13 10:46:10 -04001416 btrfs_block_release(root, path->nodes[*level]);
Chris Mason20524f02007-03-10 06:35:47 -05001417 path->nodes[*level] = NULL;
1418 *level += 1;
1419 BUG_ON(ret);
1420 return 0;
1421}
1422
Chris Mason9aca1d52007-03-13 11:09:37 -04001423/*
1424 * helper for dropping snapshots. This walks back up the tree in the path
1425 * to find the first node higher up where we haven't yet gone through
1426 * all the slots
1427 */
Chris Masone089f052007-03-16 16:20:31 -04001428static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root
1429 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05001430{
1431 int i;
1432 int slot;
1433 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04001434 struct btrfs_root_item *root_item = &root->root_item;
1435
Chris Mason234b63a2007-03-13 10:46:10 -04001436 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Mason20524f02007-03-10 06:35:47 -05001437 slot = path->slots[i];
Chris Masone20d96d2007-03-22 12:13:20 -04001438 if (slot < btrfs_header_nritems(
1439 btrfs_buffer_header(path->nodes[i])) - 1) {
Chris Mason9f3a7422007-08-07 15:52:19 -04001440 struct btrfs_node *node;
1441 node = btrfs_buffer_node(path->nodes[i]);
Chris Mason20524f02007-03-10 06:35:47 -05001442 path->slots[i]++;
1443 *level = i;
Chris Mason9f3a7422007-08-07 15:52:19 -04001444 WARN_ON(*level == 0);
1445 memcpy(&root_item->drop_progress,
1446 &node->ptrs[path->slots[i]].key,
1447 sizeof(root_item->drop_progress));
1448 root_item->drop_level = i;
Chris Mason20524f02007-03-10 06:35:47 -05001449 return 0;
1450 } else {
Chris Masone089f052007-03-16 16:20:31 -04001451 ret = btrfs_free_extent(trans, root,
Chris Mason7eccb902007-04-11 15:53:25 -04001452 bh_blocknr(path->nodes[*level]),
Chris Masone089f052007-03-16 16:20:31 -04001453 1, 1);
Chris Mason6407bf62007-03-27 06:33:00 -04001454 BUG_ON(ret);
Chris Mason234b63a2007-03-13 10:46:10 -04001455 btrfs_block_release(root, path->nodes[*level]);
Chris Mason83e15a22007-03-12 09:03:27 -04001456 path->nodes[*level] = NULL;
Chris Mason20524f02007-03-10 06:35:47 -05001457 *level = i + 1;
Chris Mason20524f02007-03-10 06:35:47 -05001458 }
1459 }
1460 return 1;
1461}
1462
Chris Mason9aca1d52007-03-13 11:09:37 -04001463/*
1464 * drop the reference count on the tree rooted at 'snap'. This traverses
1465 * the tree freeing any blocks that have a ref count of zero after being
1466 * decremented.
1467 */
Chris Masone089f052007-03-16 16:20:31 -04001468int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason9f3a7422007-08-07 15:52:19 -04001469 *root)
Chris Mason20524f02007-03-10 06:35:47 -05001470{
Chris Mason3768f362007-03-13 16:47:54 -04001471 int ret = 0;
Chris Mason9aca1d52007-03-13 11:09:37 -04001472 int wret;
Chris Mason20524f02007-03-10 06:35:47 -05001473 int level;
Chris Mason5caf2a02007-04-02 11:20:42 -04001474 struct btrfs_path *path;
Chris Mason20524f02007-03-10 06:35:47 -05001475 int i;
1476 int orig_level;
Chris Mason9f3a7422007-08-07 15:52:19 -04001477 struct btrfs_root_item *root_item = &root->root_item;
Chris Mason20524f02007-03-10 06:35:47 -05001478
Chris Mason5caf2a02007-04-02 11:20:42 -04001479 path = btrfs_alloc_path();
1480 BUG_ON(!path);
Chris Mason20524f02007-03-10 06:35:47 -05001481
Chris Mason9f3a7422007-08-07 15:52:19 -04001482 level = btrfs_header_level(btrfs_buffer_header(root->node));
Chris Mason20524f02007-03-10 06:35:47 -05001483 orig_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04001484 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
1485 path->nodes[level] = root->node;
1486 path->slots[level] = 0;
1487 } else {
1488 struct btrfs_key key;
1489 struct btrfs_disk_key *found_key;
1490 struct btrfs_node *node;
Chris Mason6702ed42007-08-07 16:15:09 -04001491
Chris Mason9f3a7422007-08-07 15:52:19 -04001492 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Chris Mason6702ed42007-08-07 16:15:09 -04001493 level = root_item->drop_level;
1494 path->lowest_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04001495 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason6702ed42007-08-07 16:15:09 -04001496 if (wret < 0) {
Chris Mason9f3a7422007-08-07 15:52:19 -04001497 ret = wret;
1498 goto out;
1499 }
Chris Mason9f3a7422007-08-07 15:52:19 -04001500 node = btrfs_buffer_node(path->nodes[level]);
1501 found_key = &node->ptrs[path->slots[level]].key;
1502 WARN_ON(memcmp(found_key, &root_item->drop_progress,
1503 sizeof(*found_key)));
1504 }
Chris Mason20524f02007-03-10 06:35:47 -05001505 while(1) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001506 wret = walk_down_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04001507 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05001508 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04001509 if (wret < 0)
1510 ret = wret;
1511
Chris Mason5caf2a02007-04-02 11:20:42 -04001512 wret = walk_up_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04001513 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05001514 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04001515 if (wret < 0)
1516 ret = wret;
Chris Mason409eb952007-08-08 20:17:12 -04001517 ret = -EAGAIN;
1518 get_bh(root->node);
1519 break;
Chris Mason20524f02007-03-10 06:35:47 -05001520 }
Chris Mason83e15a22007-03-12 09:03:27 -04001521 for (i = 0; i <= orig_level; i++) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001522 if (path->nodes[i]) {
1523 btrfs_block_release(root, path->nodes[i]);
Chris Mason6702ed42007-08-07 16:15:09 -04001524 path->nodes[i] = 0;
Chris Mason83e15a22007-03-12 09:03:27 -04001525 }
Chris Mason20524f02007-03-10 06:35:47 -05001526 }
Chris Mason9f3a7422007-08-07 15:52:19 -04001527out:
Chris Mason5caf2a02007-04-02 11:20:42 -04001528 btrfs_free_path(path);
Chris Mason9aca1d52007-03-13 11:09:37 -04001529 return ret;
Chris Mason20524f02007-03-10 06:35:47 -05001530}
Chris Mason9078a3e2007-04-26 16:46:15 -04001531
Chris Masonbe744172007-05-06 10:15:01 -04001532static int free_block_group_radix(struct radix_tree_root *radix)
Chris Mason9078a3e2007-04-26 16:46:15 -04001533{
1534 int ret;
1535 struct btrfs_block_group_cache *cache[8];
1536 int i;
1537
1538 while(1) {
Chris Masonbe744172007-05-06 10:15:01 -04001539 ret = radix_tree_gang_lookup(radix, (void **)cache, 0,
Chris Mason9078a3e2007-04-26 16:46:15 -04001540 ARRAY_SIZE(cache));
1541 if (!ret)
1542 break;
1543 for (i = 0; i < ret; i++) {
Chris Masonbe744172007-05-06 10:15:01 -04001544 radix_tree_delete(radix, cache[i]->key.objectid +
Chris Mason9078a3e2007-04-26 16:46:15 -04001545 cache[i]->key.offset - 1);
1546 kfree(cache[i]);
1547 }
1548 }
1549 return 0;
1550}
1551
Chris Masonbe744172007-05-06 10:15:01 -04001552int btrfs_free_block_groups(struct btrfs_fs_info *info)
1553{
1554 int ret;
1555 int ret2;
Chris Masone37c9e62007-05-09 20:13:14 -04001556 unsigned long gang[16];
1557 int i;
Chris Masonbe744172007-05-06 10:15:01 -04001558
1559 ret = free_block_group_radix(&info->block_group_radix);
1560 ret2 = free_block_group_radix(&info->block_group_data_radix);
1561 if (ret)
1562 return ret;
1563 if (ret2)
1564 return ret2;
Chris Masone37c9e62007-05-09 20:13:14 -04001565
1566 while(1) {
1567 ret = find_first_radix_bit(&info->extent_map_radix,
1568 gang, 0, ARRAY_SIZE(gang));
1569 if (!ret)
1570 break;
1571 for (i = 0; i < ret; i++) {
1572 clear_radix_bit(&info->extent_map_radix, gang[i]);
1573 }
1574 }
Chris Masonbe744172007-05-06 10:15:01 -04001575 return 0;
1576}
1577
Chris Mason9078a3e2007-04-26 16:46:15 -04001578int btrfs_read_block_groups(struct btrfs_root *root)
1579{
1580 struct btrfs_path *path;
1581 int ret;
1582 int err = 0;
1583 struct btrfs_block_group_item *bi;
1584 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04001585 struct btrfs_fs_info *info = root->fs_info;
1586 struct radix_tree_root *radix;
Chris Mason9078a3e2007-04-26 16:46:15 -04001587 struct btrfs_key key;
1588 struct btrfs_key found_key;
1589 struct btrfs_leaf *leaf;
Chris Mason84f54cf2007-06-12 07:43:08 -04001590 u64 group_size_blocks;
Chris Mason31f3c992007-04-30 15:25:45 -04001591 u64 used;
Chris Mason9078a3e2007-04-26 16:46:15 -04001592
Chris Mason84f54cf2007-06-12 07:43:08 -04001593 group_size_blocks = BTRFS_BLOCK_GROUP_SIZE >>
1594 root->fs_info->sb->s_blocksize_bits;
Chris Masonbe744172007-05-06 10:15:01 -04001595 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04001596 key.objectid = 0;
1597 key.offset = group_size_blocks;
1598 key.flags = 0;
1599 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
1600
1601 path = btrfs_alloc_path();
1602 if (!path)
1603 return -ENOMEM;
1604
1605 while(1) {
Chris Masonbe744172007-05-06 10:15:01 -04001606 ret = btrfs_search_slot(NULL, info->extent_root,
Chris Mason9078a3e2007-04-26 16:46:15 -04001607 &key, path, 0, 0);
1608 if (ret != 0) {
1609 err = ret;
1610 break;
1611 }
1612 leaf = btrfs_buffer_leaf(path->nodes[0]);
1613 btrfs_disk_key_to_cpu(&found_key,
1614 &leaf->items[path->slots[0]].key);
1615 cache = kmalloc(sizeof(*cache), GFP_NOFS);
1616 if (!cache) {
1617 err = -1;
1618 break;
1619 }
Chris Mason3e1ad542007-05-07 20:03:49 -04001620
Chris Mason9078a3e2007-04-26 16:46:15 -04001621 bi = btrfs_item_ptr(leaf, path->slots[0],
1622 struct btrfs_block_group_item);
Chris Mason1e2677e2007-05-29 16:52:18 -04001623 if (bi->flags & BTRFS_BLOCK_GROUP_DATA) {
1624 radix = &info->block_group_data_radix;
1625 cache->data = 1;
1626 } else {
1627 radix = &info->block_group_radix;
1628 cache->data = 0;
1629 }
1630
Chris Mason9078a3e2007-04-26 16:46:15 -04001631 memcpy(&cache->item, bi, sizeof(*bi));
1632 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Mason31f3c992007-04-30 15:25:45 -04001633 cache->last_alloc = cache->key.objectid;
1634 cache->first_free = cache->key.objectid;
Chris Masonbe744172007-05-06 10:15:01 -04001635 cache->pinned = 0;
Chris Masone37c9e62007-05-09 20:13:14 -04001636 cache->cached = 0;
1637
Chris Mason3e1ad542007-05-07 20:03:49 -04001638 cache->radix = radix;
1639
Chris Mason9078a3e2007-04-26 16:46:15 -04001640 key.objectid = found_key.objectid + found_key.offset;
1641 btrfs_release_path(root, path);
Chris Masonbe744172007-05-06 10:15:01 -04001642 ret = radix_tree_insert(radix, found_key.objectid +
Chris Mason9078a3e2007-04-26 16:46:15 -04001643 found_key.offset - 1,
1644 (void *)cache);
1645 BUG_ON(ret);
Chris Mason31f3c992007-04-30 15:25:45 -04001646 used = btrfs_block_group_used(bi);
Chris Mason84f54cf2007-06-12 07:43:08 -04001647 if (used < div_factor(key.offset, 8)) {
Chris Masonbe744172007-05-06 10:15:01 -04001648 radix_tree_tag_set(radix, found_key.objectid +
Chris Mason31f3c992007-04-30 15:25:45 -04001649 found_key.offset - 1,
1650 BTRFS_BLOCK_GROUP_AVAIL);
1651 }
Chris Mason9078a3e2007-04-26 16:46:15 -04001652 if (key.objectid >=
Chris Mason4b52dff2007-06-26 10:06:50 -04001653 btrfs_super_total_blocks(&info->super_copy))
Chris Mason9078a3e2007-04-26 16:46:15 -04001654 break;
1655 }
1656
1657 btrfs_free_path(path);
1658 return 0;
1659}