blob: ba50bd7b9a785f5fea638fc6fbadc4c650eee720 [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 Masonb18c6682007-04-17 13:26:50 -0400399static int lookup_extent_ref(struct btrfs_trans_handle *trans,
400 struct btrfs_root *root, u64 blocknr,
401 u64 num_blocks, u32 *refs)
Chris Masona28ec192007-03-06 20:08:01 -0500402{
Chris Mason5caf2a02007-04-02 11:20:42 -0400403 struct btrfs_path *path;
Chris Masona28ec192007-03-06 20:08:01 -0500404 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400405 struct btrfs_key key;
Chris Mason234b63a2007-03-13 10:46:10 -0400406 struct btrfs_leaf *l;
407 struct btrfs_extent_item *item;
Chris Mason5caf2a02007-04-02 11:20:42 -0400408
409 path = btrfs_alloc_path();
Chris Masona28ec192007-03-06 20:08:01 -0500410 key.objectid = blocknr;
Chris Mason6407bf62007-03-27 06:33:00 -0400411 key.offset = num_blocks;
Chris Mason62e27492007-03-15 12:56:47 -0400412 key.flags = 0;
413 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Mason5caf2a02007-04-02 11:20:42 -0400414 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -0400415 0, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400416 if (ret < 0)
417 goto out;
Chris Masona28ec192007-03-06 20:08:01 -0500418 if (ret != 0)
419 BUG();
Chris Mason5caf2a02007-04-02 11:20:42 -0400420 l = btrfs_buffer_leaf(path->nodes[0]);
421 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400422 *refs = btrfs_extent_refs(item);
Chris Mason54aa1f42007-06-22 14:16:25 -0400423out:
Chris Mason5caf2a02007-04-02 11:20:42 -0400424 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -0500425 return 0;
426}
427
Chris Masonc5739bb2007-04-10 09:27:04 -0400428int btrfs_inc_root_ref(struct btrfs_trans_handle *trans,
429 struct btrfs_root *root)
430{
Chris Masonb18c6682007-04-17 13:26:50 -0400431 return btrfs_inc_extent_ref(trans, root, bh_blocknr(root->node), 1);
Chris Masonc5739bb2007-04-10 09:27:04 -0400432}
433
Chris Masone089f052007-03-16 16:20:31 -0400434int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Masone20d96d2007-03-22 12:13:20 -0400435 struct buffer_head *buf)
Chris Mason02217ed2007-03-02 16:08:05 -0500436{
437 u64 blocknr;
Chris Masone20d96d2007-03-22 12:13:20 -0400438 struct btrfs_node *buf_node;
Chris Mason6407bf62007-03-27 06:33:00 -0400439 struct btrfs_leaf *buf_leaf;
440 struct btrfs_disk_key *key;
441 struct btrfs_file_extent_item *fi;
Chris Mason02217ed2007-03-02 16:08:05 -0500442 int i;
Chris Mason6407bf62007-03-27 06:33:00 -0400443 int leaf;
444 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -0400445 int faili;
446 int err;
Chris Masona28ec192007-03-06 20:08:01 -0500447
Chris Mason3768f362007-03-13 16:47:54 -0400448 if (!root->ref_cows)
Chris Masona28ec192007-03-06 20:08:01 -0500449 return 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400450 buf_node = btrfs_buffer_node(buf);
Chris Mason6407bf62007-03-27 06:33:00 -0400451 leaf = btrfs_is_leaf(buf_node);
452 buf_leaf = btrfs_buffer_leaf(buf);
Chris Masone20d96d2007-03-22 12:13:20 -0400453 for (i = 0; i < btrfs_header_nritems(&buf_node->header); i++) {
Chris Mason6407bf62007-03-27 06:33:00 -0400454 if (leaf) {
Chris Mason3a686372007-05-24 13:35:57 -0400455 u64 disk_blocknr;
Chris Mason6407bf62007-03-27 06:33:00 -0400456 key = &buf_leaf->items[i].key;
457 if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY)
458 continue;
459 fi = btrfs_item_ptr(buf_leaf, i,
460 struct btrfs_file_extent_item);
Chris Mason236454df2007-04-19 13:37:44 -0400461 if (btrfs_file_extent_type(fi) ==
462 BTRFS_FILE_EXTENT_INLINE)
463 continue;
Chris Mason3a686372007-05-24 13:35:57 -0400464 disk_blocknr = btrfs_file_extent_disk_blocknr(fi);
465 if (disk_blocknr == 0)
466 continue;
467 ret = btrfs_inc_extent_ref(trans, root, disk_blocknr,
Chris Mason6407bf62007-03-27 06:33:00 -0400468 btrfs_file_extent_disk_num_blocks(fi));
Chris Mason54aa1f42007-06-22 14:16:25 -0400469 if (ret) {
470 faili = i;
471 goto fail;
472 }
Chris Mason6407bf62007-03-27 06:33:00 -0400473 } else {
474 blocknr = btrfs_node_blockptr(buf_node, i);
Chris Masonb18c6682007-04-17 13:26:50 -0400475 ret = btrfs_inc_extent_ref(trans, root, blocknr, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400476 if (ret) {
477 faili = i;
478 goto fail;
479 }
Chris Mason6407bf62007-03-27 06:33:00 -0400480 }
Chris Mason02217ed2007-03-02 16:08:05 -0500481 }
482 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -0400483fail:
Chris Masonccd467d2007-06-28 15:57:36 -0400484 WARN_ON(1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400485 for (i =0; i < faili; i++) {
486 if (leaf) {
487 u64 disk_blocknr;
488 key = &buf_leaf->items[i].key;
489 if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY)
490 continue;
491 fi = btrfs_item_ptr(buf_leaf, i,
492 struct btrfs_file_extent_item);
493 if (btrfs_file_extent_type(fi) ==
494 BTRFS_FILE_EXTENT_INLINE)
495 continue;
496 disk_blocknr = btrfs_file_extent_disk_blocknr(fi);
497 if (disk_blocknr == 0)
498 continue;
499 err = btrfs_free_extent(trans, root, disk_blocknr,
500 btrfs_file_extent_disk_num_blocks(fi), 0);
501 BUG_ON(err);
502 } else {
503 blocknr = btrfs_node_blockptr(buf_node, i);
504 err = btrfs_free_extent(trans, root, blocknr, 1, 0);
505 BUG_ON(err);
506 }
507 }
508 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -0500509}
510
Chris Mason9078a3e2007-04-26 16:46:15 -0400511static int write_one_cache_group(struct btrfs_trans_handle *trans,
512 struct btrfs_root *root,
513 struct btrfs_path *path,
514 struct btrfs_block_group_cache *cache)
515{
516 int ret;
517 int pending_ret;
518 struct btrfs_root *extent_root = root->fs_info->extent_root;
519 struct btrfs_block_group_item *bi;
Chris Mason9078a3e2007-04-26 16:46:15 -0400520
Chris Mason9078a3e2007-04-26 16:46:15 -0400521 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400522 if (ret < 0)
523 goto fail;
Chris Mason9078a3e2007-04-26 16:46:15 -0400524 BUG_ON(ret);
525 bi = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
526 struct btrfs_block_group_item);
527 memcpy(bi, &cache->item, sizeof(*bi));
Chris Masonccd467d2007-06-28 15:57:36 -0400528 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason9078a3e2007-04-26 16:46:15 -0400529 btrfs_release_path(extent_root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -0400530fail:
Chris Mason9078a3e2007-04-26 16:46:15 -0400531 finish_current_insert(trans, extent_root);
532 pending_ret = del_pending_extents(trans, extent_root);
533 if (ret)
534 return ret;
535 if (pending_ret)
536 return pending_ret;
Chris Masonbe744172007-05-06 10:15:01 -0400537 if (cache->data)
538 cache->last_alloc = cache->first_free;
Chris Mason9078a3e2007-04-26 16:46:15 -0400539 return 0;
540
541}
542
Chris Masonbe744172007-05-06 10:15:01 -0400543static int write_dirty_block_radix(struct btrfs_trans_handle *trans,
544 struct btrfs_root *root,
545 struct radix_tree_root *radix)
Chris Mason9078a3e2007-04-26 16:46:15 -0400546{
547 struct btrfs_block_group_cache *cache[8];
548 int ret;
549 int err = 0;
550 int werr = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -0400551 int i;
552 struct btrfs_path *path;
Chris Mason54aa1f42007-06-22 14:16:25 -0400553 unsigned long off = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -0400554
555 path = btrfs_alloc_path();
556 if (!path)
557 return -ENOMEM;
558
559 while(1) {
560 ret = radix_tree_gang_lookup_tag(radix, (void **)cache,
Chris Mason54aa1f42007-06-22 14:16:25 -0400561 off, ARRAY_SIZE(cache),
Chris Mason9078a3e2007-04-26 16:46:15 -0400562 BTRFS_BLOCK_GROUP_DIRTY);
563 if (!ret)
564 break;
565 for (i = 0; i < ret; i++) {
Chris Mason54aa1f42007-06-22 14:16:25 -0400566 err = write_one_cache_group(trans, root,
567 path, cache[i]);
568 /*
569 * if we fail to write the cache group, we want
570 * to keep it marked dirty in hopes that a later
571 * write will work
572 */
573 if (err) {
574 werr = err;
575 off = cache[i]->key.objectid +
576 cache[i]->key.offset;
577 continue;
578 }
579
Chris Mason9078a3e2007-04-26 16:46:15 -0400580 radix_tree_tag_clear(radix, cache[i]->key.objectid +
581 cache[i]->key.offset - 1,
582 BTRFS_BLOCK_GROUP_DIRTY);
Chris Mason9078a3e2007-04-26 16:46:15 -0400583 }
584 }
585 btrfs_free_path(path);
586 return werr;
587}
588
Chris Masonbe744172007-05-06 10:15:01 -0400589int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
590 struct btrfs_root *root)
591{
592 int ret;
593 int ret2;
594 ret = write_dirty_block_radix(trans, root,
595 &root->fs_info->block_group_radix);
596 ret2 = write_dirty_block_radix(trans, root,
597 &root->fs_info->block_group_data_radix);
598 if (ret)
599 return ret;
600 if (ret2)
601 return ret2;
602 return 0;
603}
604
Chris Mason9078a3e2007-04-26 16:46:15 -0400605static int update_block_group(struct btrfs_trans_handle *trans,
606 struct btrfs_root *root,
Chris Mason1e2677e2007-05-29 16:52:18 -0400607 u64 blocknr, u64 num, int alloc, int mark_free,
608 int data)
Chris Mason9078a3e2007-04-26 16:46:15 -0400609{
610 struct btrfs_block_group_cache *cache;
611 struct btrfs_fs_info *info = root->fs_info;
612 u64 total = num;
613 u64 old_val;
614 u64 block_in_group;
Chris Masone37c9e62007-05-09 20:13:14 -0400615 u64 i;
Chris Mason1e2677e2007-05-29 16:52:18 -0400616 int ret;
Chris Mason3e1ad542007-05-07 20:03:49 -0400617
Chris Mason9078a3e2007-04-26 16:46:15 -0400618 while(total) {
Chris Mason5276aed2007-06-11 21:33:38 -0400619 cache = btrfs_lookup_block_group(info, blocknr);
Chris Mason3e1ad542007-05-07 20:03:49 -0400620 if (!cache) {
Chris Mason9078a3e2007-04-26 16:46:15 -0400621 return -1;
Chris Masoncd1bc462007-04-27 10:08:34 -0400622 }
Chris Mason9078a3e2007-04-26 16:46:15 -0400623 block_in_group = blocknr - cache->key.objectid;
624 WARN_ON(block_in_group > cache->key.offset);
Chris Mason3e1ad542007-05-07 20:03:49 -0400625 radix_tree_tag_set(cache->radix, cache->key.objectid +
Chris Masonbe744172007-05-06 10:15:01 -0400626 cache->key.offset - 1,
Chris Mason9078a3e2007-04-26 16:46:15 -0400627 BTRFS_BLOCK_GROUP_DIRTY);
628
629 old_val = btrfs_block_group_used(&cache->item);
630 num = min(total, cache->key.offset - block_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -0400631 if (alloc) {
Chris Masoncd1bc462007-04-27 10:08:34 -0400632 if (blocknr > cache->last_alloc)
633 cache->last_alloc = blocknr;
Chris Masone37c9e62007-05-09 20:13:14 -0400634 if (!cache->data) {
635 for (i = 0; i < num; i++) {
636 clear_radix_bit(&info->extent_map_radix,
637 blocknr + i);
638 }
639 }
Chris Mason1e2677e2007-05-29 16:52:18 -0400640 if (cache->data != data &&
Chris Mason84f54cf2007-06-12 07:43:08 -0400641 old_val < (cache->key.offset >> 1)) {
Chris Mason1e2677e2007-05-29 16:52:18 -0400642 cache->data = data;
643 radix_tree_delete(cache->radix,
644 cache->key.objectid +
645 cache->key.offset - 1);
646
647 if (data) {
648 cache->radix =
649 &info->block_group_data_radix;
650 cache->item.flags |=
651 BTRFS_BLOCK_GROUP_DATA;
652 } else {
653 cache->radix = &info->block_group_radix;
654 cache->item.flags &=
655 ~BTRFS_BLOCK_GROUP_DATA;
656 }
657 ret = radix_tree_insert(cache->radix,
658 cache->key.objectid +
659 cache->key.offset - 1,
660 (void *)cache);
661 }
662 old_val += num;
Chris Masoncd1bc462007-04-27 10:08:34 -0400663 } else {
Chris Mason9078a3e2007-04-26 16:46:15 -0400664 old_val -= num;
Chris Masoncd1bc462007-04-27 10:08:34 -0400665 if (blocknr < cache->first_free)
666 cache->first_free = blocknr;
Chris Masone37c9e62007-05-09 20:13:14 -0400667 if (!cache->data && mark_free) {
668 for (i = 0; i < num; i++) {
669 set_radix_bit(&info->extent_map_radix,
670 blocknr + i);
671 }
672 }
Chris Mason84f54cf2007-06-12 07:43:08 -0400673 if (old_val < (cache->key.offset >> 1) &&
674 old_val + num >= (cache->key.offset >> 1)) {
Chris Masone37c9e62007-05-09 20:13:14 -0400675 radix_tree_tag_set(cache->radix,
676 cache->key.objectid +
677 cache->key.offset - 1,
678 BTRFS_BLOCK_GROUP_AVAIL);
679 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400680 }
Chris Mason9078a3e2007-04-26 16:46:15 -0400681 btrfs_set_block_group_used(&cache->item, old_val);
Chris Masone37c9e62007-05-09 20:13:14 -0400682 total -= num;
683 blocknr += num;
Chris Mason9078a3e2007-04-26 16:46:15 -0400684 }
685 return 0;
686}
687
Chris Masonccd467d2007-06-28 15:57:36 -0400688int btrfs_copy_pinned(struct btrfs_root *root, struct radix_tree_root *copy)
689{
690 unsigned long gang[8];
691 u64 last = 0;
692 struct radix_tree_root *pinned_radix = &root->fs_info->pinned_radix;
693 int ret;
694 int i;
695
696 while(1) {
697 ret = find_first_radix_bit(pinned_radix, gang, last,
698 ARRAY_SIZE(gang));
699 if (!ret)
700 break;
701 for (i = 0 ; i < ret; i++) {
702 set_radix_bit(copy, gang[i]);
703 last = gang[i] + 1;
704 }
705 }
Chris Mason26b80032007-08-08 20:17:12 -0400706 ret = find_first_radix_bit(&root->fs_info->extent_ins_radix, gang, 0,
707 ARRAY_SIZE(gang));
708 WARN_ON(ret);
Chris Masonccd467d2007-06-28 15:57:36 -0400709 return 0;
710}
711
712int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
713 struct btrfs_root *root,
714 struct radix_tree_root *unpin_radix)
Chris Masona28ec192007-03-06 20:08:01 -0500715{
Chris Mason8ef97622007-03-26 10:15:30 -0400716 unsigned long gang[8];
Chris Masonbe744172007-05-06 10:15:01 -0400717 struct btrfs_block_group_cache *block_group;
Chris Mason88fd1462007-03-16 08:56:18 -0400718 u64 first = 0;
Chris Masona28ec192007-03-06 20:08:01 -0500719 int ret;
720 int i;
Chris Mason8ef97622007-03-26 10:15:30 -0400721 struct radix_tree_root *pinned_radix = &root->fs_info->pinned_radix;
Chris Masone37c9e62007-05-09 20:13:14 -0400722 struct radix_tree_root *extent_radix = &root->fs_info->extent_map_radix;
Chris Masona28ec192007-03-06 20:08:01 -0500723
724 while(1) {
Chris Masonccd467d2007-06-28 15:57:36 -0400725 ret = find_first_radix_bit(unpin_radix, gang, 0,
Chris Mason8ef97622007-03-26 10:15:30 -0400726 ARRAY_SIZE(gang));
Chris Masona28ec192007-03-06 20:08:01 -0500727 if (!ret)
728 break;
Chris Mason88fd1462007-03-16 08:56:18 -0400729 if (!first)
Chris Mason8ef97622007-03-26 10:15:30 -0400730 first = gang[0];
Chris Mason0579da42007-03-07 16:15:30 -0500731 for (i = 0; i < ret; i++) {
Chris Mason8ef97622007-03-26 10:15:30 -0400732 clear_radix_bit(pinned_radix, gang[i]);
Chris Masonccd467d2007-06-28 15:57:36 -0400733 clear_radix_bit(unpin_radix, gang[i]);
Chris Mason5276aed2007-06-11 21:33:38 -0400734 block_group = btrfs_lookup_block_group(root->fs_info,
735 gang[i]);
Chris Masonbe744172007-05-06 10:15:01 -0400736 if (block_group) {
737 WARN_ON(block_group->pinned == 0);
738 block_group->pinned--;
739 if (gang[i] < block_group->last_alloc)
740 block_group->last_alloc = gang[i];
Chris Masone37c9e62007-05-09 20:13:14 -0400741 if (!block_group->data)
742 set_radix_bit(extent_radix, gang[i]);
Chris Masonbe744172007-05-06 10:15:01 -0400743 }
Chris Mason0579da42007-03-07 16:15:30 -0500744 }
Chris Masona28ec192007-03-06 20:08:01 -0500745 }
746 return 0;
747}
748
Chris Masone089f052007-03-16 16:20:31 -0400749static int finish_current_insert(struct btrfs_trans_handle *trans, struct
750 btrfs_root *extent_root)
Chris Mason037e6392007-03-07 11:50:24 -0500751{
Chris Masone2fa7222007-03-12 16:22:34 -0400752 struct btrfs_key ins;
Chris Mason234b63a2007-03-13 10:46:10 -0400753 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -0500754 int i;
755 int ret;
Chris Mason26b80032007-08-08 20:17:12 -0400756 int err;
757 unsigned long gang[8];
Chris Mason1261ec42007-03-20 20:35:03 -0400758 struct btrfs_fs_info *info = extent_root->fs_info;
Chris Mason037e6392007-03-07 11:50:24 -0500759
Chris Masoncf27e1e2007-03-13 09:49:06 -0400760 btrfs_set_extent_refs(&extent_item, 1);
Chris Mason037e6392007-03-07 11:50:24 -0500761 ins.offset = 1;
762 ins.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -0400763 btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
Chris Mason5d0c3e62007-04-23 17:01:05 -0400764 btrfs_set_extent_owner(&extent_item, extent_root->root_key.objectid);
Chris Mason037e6392007-03-07 11:50:24 -0500765
Chris Mason26b80032007-08-08 20:17:12 -0400766 while(1) {
767 ret = find_first_radix_bit(&info->extent_ins_radix, gang, 0,
768 ARRAY_SIZE(gang));
769 if (!ret)
770 break;
771
772 for (i = 0; i < ret; i++) {
773 ins.objectid = gang[i];
774 err = btrfs_insert_item(trans, extent_root, &ins,
775 &extent_item,
776 sizeof(extent_item));
777 clear_radix_bit(&info->extent_ins_radix, gang[i]);
778 WARN_ON(err);
779 }
Chris Mason037e6392007-03-07 11:50:24 -0500780 }
Chris Mason037e6392007-03-07 11:50:24 -0500781 return 0;
782}
783
Chris Mason8ef97622007-03-26 10:15:30 -0400784static int pin_down_block(struct btrfs_root *root, u64 blocknr, int pending)
Chris Masone20d96d2007-03-22 12:13:20 -0400785{
786 int err;
Chris Mason78fae272007-03-25 11:35:08 -0400787 struct btrfs_header *header;
Chris Mason8ef97622007-03-26 10:15:30 -0400788 struct buffer_head *bh;
Chris Mason78fae272007-03-25 11:35:08 -0400789
Chris Masonf4b9aa82007-03-27 11:05:53 -0400790 if (!pending) {
Chris Masond98237b2007-03-28 13:57:48 -0400791 bh = btrfs_find_tree_block(root, blocknr);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400792 if (bh) {
793 if (buffer_uptodate(bh)) {
794 u64 transid =
795 root->fs_info->running_transaction->transid;
796 header = btrfs_buffer_header(bh);
797 if (btrfs_header_generation(header) ==
798 transid) {
799 btrfs_block_release(root, bh);
800 return 0;
801 }
Chris Masonf4b9aa82007-03-27 11:05:53 -0400802 }
Chris Masond6025572007-03-30 14:27:56 -0400803 btrfs_block_release(root, bh);
Chris Mason8ef97622007-03-26 10:15:30 -0400804 }
Chris Mason8ef97622007-03-26 10:15:30 -0400805 err = set_radix_bit(&root->fs_info->pinned_radix, blocknr);
Chris Masonbe744172007-05-06 10:15:01 -0400806 if (!err) {
807 struct btrfs_block_group_cache *cache;
Chris Mason5276aed2007-06-11 21:33:38 -0400808 cache = btrfs_lookup_block_group(root->fs_info,
809 blocknr);
Chris Masonbe744172007-05-06 10:15:01 -0400810 if (cache)
811 cache->pinned++;
812 }
Chris Masonf4b9aa82007-03-27 11:05:53 -0400813 } else {
814 err = set_radix_bit(&root->fs_info->pending_del_radix, blocknr);
815 }
Chris Masonbe744172007-05-06 10:15:01 -0400816 BUG_ON(err < 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400817 return 0;
818}
819
Chris Masona28ec192007-03-06 20:08:01 -0500820/*
821 * remove an extent from the root, returns 0 on success
822 */
Chris Masone089f052007-03-16 16:20:31 -0400823static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masone37c9e62007-05-09 20:13:14 -0400824 *root, u64 blocknr, u64 num_blocks, int pin,
825 int mark_free)
Chris Masona28ec192007-03-06 20:08:01 -0500826{
Chris Mason5caf2a02007-04-02 11:20:42 -0400827 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -0400828 struct btrfs_key key;
Chris Mason1261ec42007-03-20 20:35:03 -0400829 struct btrfs_fs_info *info = root->fs_info;
830 struct btrfs_root *extent_root = info->extent_root;
Chris Masona28ec192007-03-06 20:08:01 -0500831 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400832 struct btrfs_extent_item *ei;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400833 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500834
Chris Masona28ec192007-03-06 20:08:01 -0500835 key.objectid = blocknr;
836 key.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -0400837 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masona28ec192007-03-06 20:08:01 -0500838 key.offset = num_blocks;
839
Chris Mason5caf2a02007-04-02 11:20:42 -0400840 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -0400841 if (!path)
842 return -ENOMEM;
843
Chris Mason5caf2a02007-04-02 11:20:42 -0400844 ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400845 if (ret < 0)
846 return ret;
847 BUG_ON(ret);
Chris Mason5caf2a02007-04-02 11:20:42 -0400848 ei = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
Chris Mason123abc82007-03-14 14:14:43 -0400849 struct btrfs_extent_item);
Chris Masona28ec192007-03-06 20:08:01 -0500850 BUG_ON(ei->refs == 0);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400851 refs = btrfs_extent_refs(ei) - 1;
852 btrfs_set_extent_refs(ei, refs);
Chris Mason5caf2a02007-04-02 11:20:42 -0400853 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400854 if (refs == 0) {
Chris Mason1261ec42007-03-20 20:35:03 -0400855 u64 super_blocks_used;
Chris Mason78fae272007-03-25 11:35:08 -0400856
857 if (pin) {
Chris Mason8ef97622007-03-26 10:15:30 -0400858 ret = pin_down_block(root, blocknr, 0);
Chris Mason78fae272007-03-25 11:35:08 -0400859 BUG_ON(ret);
860 }
861
Chris Mason4b52dff2007-06-26 10:06:50 -0400862 super_blocks_used = btrfs_super_blocks_used(&info->super_copy);
863 btrfs_set_super_blocks_used(&info->super_copy,
Chris Mason1261ec42007-03-20 20:35:03 -0400864 super_blocks_used - num_blocks);
Chris Mason5caf2a02007-04-02 11:20:42 -0400865 ret = btrfs_del_item(trans, extent_root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -0400866 if (ret) {
867 return ret;
868 }
Chris Masone37c9e62007-05-09 20:13:14 -0400869 ret = update_block_group(trans, root, blocknr, num_blocks, 0,
Chris Mason1e2677e2007-05-29 16:52:18 -0400870 mark_free, 0);
Chris Mason9078a3e2007-04-26 16:46:15 -0400871 BUG_ON(ret);
Chris Masona28ec192007-03-06 20:08:01 -0500872 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400873 btrfs_free_path(path);
Chris Masone089f052007-03-16 16:20:31 -0400874 finish_current_insert(trans, extent_root);
Chris Masona28ec192007-03-06 20:08:01 -0500875 return ret;
876}
877
878/*
Chris Masonfec577f2007-02-26 10:40:21 -0500879 * find all the blocks marked as pending in the radix tree and remove
880 * them from the extent map
881 */
Chris Masone089f052007-03-16 16:20:31 -0400882static int del_pending_extents(struct btrfs_trans_handle *trans, struct
883 btrfs_root *extent_root)
Chris Masonfec577f2007-02-26 10:40:21 -0500884{
885 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -0400886 int wret;
887 int err = 0;
Chris Mason8ef97622007-03-26 10:15:30 -0400888 unsigned long gang[4];
Chris Masonfec577f2007-02-26 10:40:21 -0500889 int i;
Chris Mason8ef97622007-03-26 10:15:30 -0400890 struct radix_tree_root *pending_radix;
891 struct radix_tree_root *pinned_radix;
Chris Masonbe744172007-05-06 10:15:01 -0400892 struct btrfs_block_group_cache *cache;
Chris Mason8ef97622007-03-26 10:15:30 -0400893
894 pending_radix = &extent_root->fs_info->pending_del_radix;
895 pinned_radix = &extent_root->fs_info->pinned_radix;
Chris Masonfec577f2007-02-26 10:40:21 -0500896
897 while(1) {
Chris Masone37c9e62007-05-09 20:13:14 -0400898 ret = find_first_radix_bit(pending_radix, gang, 0,
Chris Mason8ef97622007-03-26 10:15:30 -0400899 ARRAY_SIZE(gang));
Chris Masonfec577f2007-02-26 10:40:21 -0500900 if (!ret)
901 break;
902 for (i = 0; i < ret; i++) {
Chris Mason8ef97622007-03-26 10:15:30 -0400903 wret = set_radix_bit(pinned_radix, gang[i]);
Chris Masonbe744172007-05-06 10:15:01 -0400904 if (wret == 0) {
Chris Mason5276aed2007-06-11 21:33:38 -0400905 cache =
906 btrfs_lookup_block_group(extent_root->fs_info,
Chris Masonbe744172007-05-06 10:15:01 -0400907 gang[i]);
908 if (cache)
909 cache->pinned++;
910 }
911 if (wret < 0) {
912 printk(KERN_CRIT "set_radix_bit, err %d\n",
913 wret);
914 BUG_ON(wret < 0);
915 }
Chris Mason8ef97622007-03-26 10:15:30 -0400916 wret = clear_radix_bit(pending_radix, gang[i]);
917 BUG_ON(wret);
Chris Masond5719762007-03-23 10:01:08 -0400918 wret = __free_extent(trans, extent_root,
Chris Masone37c9e62007-05-09 20:13:14 -0400919 gang[i], 1, 0, 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400920 if (wret)
921 err = wret;
Chris Masonfec577f2007-02-26 10:40:21 -0500922 }
923 }
Chris Masone20d96d2007-03-22 12:13:20 -0400924 return err;
Chris Masonfec577f2007-02-26 10:40:21 -0500925}
926
927/*
928 * remove an extent from the root, returns 0 on success
929 */
Chris Masone089f052007-03-16 16:20:31 -0400930int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
931 *root, u64 blocknr, u64 num_blocks, int pin)
Chris Masonfec577f2007-02-26 10:40:21 -0500932{
Chris Mason9f5fae22007-03-20 14:38:32 -0400933 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Masonfec577f2007-02-26 10:40:21 -0500934 int pending_ret;
935 int ret;
Chris Masona28ec192007-03-06 20:08:01 -0500936
937 if (root == extent_root) {
Chris Mason8ef97622007-03-26 10:15:30 -0400938 pin_down_block(root, blocknr, 1);
Chris Masona28ec192007-03-06 20:08:01 -0500939 return 0;
940 }
Chris Masone37c9e62007-05-09 20:13:14 -0400941 ret = __free_extent(trans, root, blocknr, num_blocks, pin, pin == 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400942 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
Chris Masonfec577f2007-02-26 10:40:21 -0500943 return ret ? ret : pending_ret;
944}
945
946/*
947 * walks the btree of allocated extents and find a hole of a given size.
948 * The key ins is changed to record the hole:
949 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -0400950 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -0500951 * ins->offset == number of blocks
952 * Any available blocks before search_start are skipped.
953 */
Chris Masone089f052007-03-16 16:20:31 -0400954static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason6702ed42007-08-07 16:15:09 -0400955 *orig_root, u64 num_blocks, u64 empty_size,
956 u64 search_start, u64 search_end, u64 hint_block,
Chris Masonf2654de2007-06-26 12:20:46 -0400957 struct btrfs_key *ins, u64 exclude_start,
958 u64 exclude_nr, int data)
Chris Masonfec577f2007-02-26 10:40:21 -0500959{
Chris Mason5caf2a02007-04-02 11:20:42 -0400960 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -0400961 struct btrfs_key key;
Chris Masonfec577f2007-02-26 10:40:21 -0500962 int ret;
963 u64 hole_size = 0;
964 int slot = 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400965 u64 last_block = 0;
Chris Mason037e6392007-03-07 11:50:24 -0500966 u64 test_block;
Chris Masonbe744172007-05-06 10:15:01 -0400967 u64 orig_search_start = search_start;
Chris Masonfec577f2007-02-26 10:40:21 -0500968 int start_found;
Chris Mason234b63a2007-03-13 10:46:10 -0400969 struct btrfs_leaf *l;
Chris Mason9f5fae22007-03-20 14:38:32 -0400970 struct btrfs_root * root = orig_root->fs_info->extent_root;
Chris Masonf2458e12007-04-25 15:52:25 -0400971 struct btrfs_fs_info *info = root->fs_info;
Chris Mason0579da42007-03-07 16:15:30 -0500972 int total_needed = num_blocks;
Chris Masone20d96d2007-03-22 12:13:20 -0400973 int level;
Chris Masonbe08c1b2007-05-03 09:06:49 -0400974 struct btrfs_block_group_cache *block_group;
Chris Masonbe744172007-05-06 10:15:01 -0400975 int full_scan = 0;
Chris Masonfbdc7622007-05-30 10:22:12 -0400976 int wrapped = 0;
Chris Masonde428b62007-05-18 13:28:27 -0400977 u64 limit;
Chris Masonfec577f2007-02-26 10:40:21 -0500978
Chris Mason26b80032007-08-08 20:17:12 -0400979 WARN_ON(num_blocks < 1);
Chris Masonb1a4d962007-04-04 15:27:52 -0400980 ins->flags = 0;
981 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
982
Chris Masone20d96d2007-03-22 12:13:20 -0400983 level = btrfs_header_level(btrfs_buffer_header(root->node));
Chris Mason3e1ad542007-05-07 20:03:49 -0400984 if (search_end == (u64)-1)
Chris Mason4b52dff2007-06-26 10:06:50 -0400985 search_end = btrfs_super_total_blocks(&info->super_copy);
Chris Masonfbdc7622007-05-30 10:22:12 -0400986 if (hint_block) {
Chris Mason5276aed2007-06-11 21:33:38 -0400987 block_group = btrfs_lookup_block_group(info, hint_block);
Chris Masonbe744172007-05-06 10:15:01 -0400988 block_group = btrfs_find_block_group(root, block_group,
Chris Masonfbdc7622007-05-30 10:22:12 -0400989 hint_block, data, 1);
Chris Masonbe744172007-05-06 10:15:01 -0400990 } else {
991 block_group = btrfs_find_block_group(root,
992 trans->block_group, 0,
Chris Masonde428b62007-05-18 13:28:27 -0400993 data, 1);
Chris Masonbe744172007-05-06 10:15:01 -0400994 }
995
Chris Mason6702ed42007-08-07 16:15:09 -0400996 total_needed += empty_size;
Chris Masone0115992007-06-19 16:23:05 -0400997 path = btrfs_alloc_path();
998
Chris Masonbe744172007-05-06 10:15:01 -0400999check_failed:
Chris Mason1e2677e2007-05-29 16:52:18 -04001000 if (!block_group->data)
Chris Masone37c9e62007-05-09 20:13:14 -04001001 search_start = find_search_start(root, &block_group,
1002 search_start, total_needed);
Chris Masonfbdc7622007-05-30 10:22:12 -04001003 else if (!full_scan)
Chris Masone37c9e62007-05-09 20:13:14 -04001004 search_start = max(block_group->last_alloc, search_start);
1005
Chris Mason5caf2a02007-04-02 11:20:42 -04001006 btrfs_init_path(path);
Chris Masonfec577f2007-02-26 10:40:21 -05001007 ins->objectid = search_start;
1008 ins->offset = 0;
Chris Masonfec577f2007-02-26 10:40:21 -05001009 start_found = 0;
Chris Masone37c9e62007-05-09 20:13:14 -04001010
Chris Mason5caf2a02007-04-02 11:20:42 -04001011 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
Chris Mason0f70abe2007-02-28 16:46:22 -05001012 if (ret < 0)
1013 goto error;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001014
Chris Masone37c9e62007-05-09 20:13:14 -04001015 if (path->slots[0] > 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001016 path->slots[0]--;
Chris Masone37c9e62007-05-09 20:13:14 -04001017 }
1018
1019 l = btrfs_buffer_leaf(path->nodes[0]);
1020 btrfs_disk_key_to_cpu(&key, &l->items[path->slots[0]].key);
1021 /*
1022 * a rare case, go back one key if we hit a block group item
1023 * instead of an extent item
1024 */
1025 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY &&
1026 key.objectid + key.offset >= search_start) {
1027 ins->objectid = key.objectid;
1028 ins->offset = key.offset - 1;
1029 btrfs_release_path(root, path);
1030 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
1031 if (ret < 0)
1032 goto error;
1033
1034 if (path->slots[0] > 0) {
1035 path->slots[0]--;
1036 }
1037 }
Chris Mason0579da42007-03-07 16:15:30 -05001038
Chris Masonfec577f2007-02-26 10:40:21 -05001039 while (1) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001040 l = btrfs_buffer_leaf(path->nodes[0]);
1041 slot = path->slots[0];
Chris Mason7518a232007-03-12 12:01:18 -04001042 if (slot >= btrfs_header_nritems(&l->header)) {
Chris Masonde428b62007-05-18 13:28:27 -04001043 if (start_found)
1044 limit = last_block +
Chris Mason84f54cf2007-06-12 07:43:08 -04001045 (block_group->key.offset >> 1);
Chris Masonde428b62007-05-18 13:28:27 -04001046 else
1047 limit = search_start +
Chris Mason84f54cf2007-06-12 07:43:08 -04001048 (block_group->key.offset >> 1);
Chris Mason5caf2a02007-04-02 11:20:42 -04001049 ret = btrfs_next_leaf(root, path);
Chris Masonfec577f2007-02-26 10:40:21 -05001050 if (ret == 0)
1051 continue;
Chris Mason0f70abe2007-02-28 16:46:22 -05001052 if (ret < 0)
1053 goto error;
Chris Masonfec577f2007-02-26 10:40:21 -05001054 if (!start_found) {
1055 ins->objectid = search_start;
Chris Mason3e1ad542007-05-07 20:03:49 -04001056 ins->offset = search_end - search_start;
Chris Masonfec577f2007-02-26 10:40:21 -05001057 start_found = 1;
1058 goto check_pending;
1059 }
1060 ins->objectid = last_block > search_start ?
1061 last_block : search_start;
Chris Mason3e1ad542007-05-07 20:03:49 -04001062 ins->offset = search_end - ins->objectid;
Chris Masonfec577f2007-02-26 10:40:21 -05001063 goto check_pending;
1064 }
Chris Masone37c9e62007-05-09 20:13:14 -04001065
Chris Masone2fa7222007-03-12 16:22:34 -04001066 btrfs_disk_key_to_cpu(&key, &l->items[slot].key);
Chris Masone37c9e62007-05-09 20:13:14 -04001067 if (key.objectid >= search_start && key.objectid > last_block &&
1068 start_found) {
1069 if (last_block < search_start)
1070 last_block = search_start;
1071 hole_size = key.objectid - last_block;
1072 if (hole_size >= num_blocks) {
1073 ins->objectid = last_block;
1074 ins->offset = hole_size;
1075 goto check_pending;
Chris Mason0579da42007-03-07 16:15:30 -05001076 }
Chris Masonfec577f2007-02-26 10:40:21 -05001077 }
Chris Masone37c9e62007-05-09 20:13:14 -04001078
1079 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY)
1080 goto next;
1081
Chris Mason0579da42007-03-07 16:15:30 -05001082 start_found = 1;
Chris Masone2fa7222007-03-12 16:22:34 -04001083 last_block = key.objectid + key.offset;
Chris Masonfbdc7622007-05-30 10:22:12 -04001084 if (!full_scan && last_block >= block_group->key.objectid +
Chris Masonbe744172007-05-06 10:15:01 -04001085 block_group->key.offset) {
1086 btrfs_release_path(root, path);
1087 search_start = block_group->key.objectid +
1088 block_group->key.offset * 2;
1089 goto new_group;
1090 }
Chris Mason9078a3e2007-04-26 16:46:15 -04001091next:
Chris Mason5caf2a02007-04-02 11:20:42 -04001092 path->slots[0]++;
Chris Masonde428b62007-05-18 13:28:27 -04001093 cond_resched();
Chris Masonfec577f2007-02-26 10:40:21 -05001094 }
Chris Masonfec577f2007-02-26 10:40:21 -05001095check_pending:
1096 /* we have to make sure we didn't find an extent that has already
1097 * been allocated by the map tree or the original allocation
1098 */
Chris Mason5caf2a02007-04-02 11:20:42 -04001099 btrfs_release_path(root, path);
Chris Masonfec577f2007-02-26 10:40:21 -05001100 BUG_ON(ins->objectid < search_start);
Chris Masone37c9e62007-05-09 20:13:14 -04001101
Chris Mason3e1ad542007-05-07 20:03:49 -04001102 if (ins->objectid + num_blocks >= search_end) {
Chris Masonfbdc7622007-05-30 10:22:12 -04001103 if (full_scan) {
1104 ret = -ENOSPC;
1105 goto error;
1106 }
Chris Masonbe744172007-05-06 10:15:01 -04001107 search_start = orig_search_start;
Chris Mason6702ed42007-08-07 16:15:09 -04001108 if (wrapped) {
1109 if (!full_scan)
1110 total_needed -= empty_size;
Chris Masonfbdc7622007-05-30 10:22:12 -04001111 full_scan = 1;
Chris Mason6702ed42007-08-07 16:15:09 -04001112 } else
Chris Masonfbdc7622007-05-30 10:22:12 -04001113 wrapped = 1;
Chris Masonbe744172007-05-06 10:15:01 -04001114 goto new_group;
Chris Mason06a2f9f2007-04-28 08:48:10 -04001115 }
Chris Mason037e6392007-03-07 11:50:24 -05001116 for (test_block = ins->objectid;
Chris Masonf2458e12007-04-25 15:52:25 -04001117 test_block < ins->objectid + num_blocks; test_block++) {
Chris Mason26b80032007-08-08 20:17:12 -04001118 if (test_radix_bit(&info->pinned_radix, test_block) ||
1119 test_radix_bit(&info->extent_ins_radix, test_block)) {
Chris Mason037e6392007-03-07 11:50:24 -05001120 search_start = test_block + 1;
Chris Masonbe744172007-05-06 10:15:01 -04001121 goto new_group;
Chris Masonfec577f2007-02-26 10:40:21 -05001122 }
1123 }
Chris Masonf2654de2007-06-26 12:20:46 -04001124 if (exclude_nr > 0 && (ins->objectid + num_blocks > exclude_start &&
1125 ins->objectid < exclude_start + exclude_nr)) {
1126 search_start = exclude_start + exclude_nr;
1127 goto new_group;
1128 }
Chris Masone37c9e62007-05-09 20:13:14 -04001129 if (!data) {
Chris Mason5276aed2007-06-11 21:33:38 -04001130 block_group = btrfs_lookup_block_group(info, ins->objectid);
Chris Mason26b80032007-08-08 20:17:12 -04001131 if (block_group)
1132 trans->block_group = block_group;
Chris Masoncd1bc462007-04-27 10:08:34 -04001133 }
Chris Mason037e6392007-03-07 11:50:24 -05001134 ins->offset = num_blocks;
Chris Mason5caf2a02007-04-02 11:20:42 -04001135 btrfs_free_path(path);
Chris Masonfec577f2007-02-26 10:40:21 -05001136 return 0;
Chris Masonbe744172007-05-06 10:15:01 -04001137
1138new_group:
Chris Mason3e1ad542007-05-07 20:03:49 -04001139 if (search_start + num_blocks >= search_end) {
Chris Masonbe744172007-05-06 10:15:01 -04001140 search_start = orig_search_start;
Chris Masonfbdc7622007-05-30 10:22:12 -04001141 if (full_scan) {
1142 ret = -ENOSPC;
1143 goto error;
1144 }
Chris Mason6702ed42007-08-07 16:15:09 -04001145 if (wrapped) {
1146 if (!full_scan)
1147 total_needed -= empty_size;
Chris Masonfbdc7622007-05-30 10:22:12 -04001148 full_scan = 1;
Chris Mason6702ed42007-08-07 16:15:09 -04001149 } else
Chris Masonfbdc7622007-05-30 10:22:12 -04001150 wrapped = 1;
Chris Masonbe744172007-05-06 10:15:01 -04001151 }
Chris Mason5276aed2007-06-11 21:33:38 -04001152 block_group = btrfs_lookup_block_group(info, search_start);
Chris Masonfbdc7622007-05-30 10:22:12 -04001153 cond_resched();
Chris Masonbe744172007-05-06 10:15:01 -04001154 if (!full_scan)
1155 block_group = btrfs_find_block_group(root, block_group,
Chris Masonde428b62007-05-18 13:28:27 -04001156 search_start, data, 0);
Chris Masonbe744172007-05-06 10:15:01 -04001157 goto check_failed;
1158
Chris Mason0f70abe2007-02-28 16:46:22 -05001159error:
Chris Mason5caf2a02007-04-02 11:20:42 -04001160 btrfs_release_path(root, path);
1161 btrfs_free_path(path);
Chris Mason0f70abe2007-02-28 16:46:22 -05001162 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05001163}
Chris Masonfec577f2007-02-26 10:40:21 -05001164/*
Chris Masonfec577f2007-02-26 10:40:21 -05001165 * finds a free extent and does all the dirty work required for allocation
1166 * returns the key for the extent through ins, and a tree buffer for
1167 * the first block of the extent through buf.
1168 *
1169 * returns 0 if everything worked, non-zero otherwise.
1170 */
Chris Mason4d775672007-04-20 20:23:12 -04001171int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
1172 struct btrfs_root *root, u64 owner,
Chris Mason6702ed42007-08-07 16:15:09 -04001173 u64 num_blocks, u64 empty_size, u64 hint_block,
Chris Masonbe08c1b2007-05-03 09:06:49 -04001174 u64 search_end, struct btrfs_key *ins, int data)
Chris Masonfec577f2007-02-26 10:40:21 -05001175{
1176 int ret;
1177 int pending_ret;
Chris Mason1261ec42007-03-20 20:35:03 -04001178 u64 super_blocks_used;
Chris Masonfbdc7622007-05-30 10:22:12 -04001179 u64 search_start = 0;
Chris Mason1261ec42007-03-20 20:35:03 -04001180 struct btrfs_fs_info *info = root->fs_info;
1181 struct btrfs_root *extent_root = info->extent_root;
Chris Mason234b63a2007-03-13 10:46:10 -04001182 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -05001183
Chris Masoncf27e1e2007-03-13 09:49:06 -04001184 btrfs_set_extent_refs(&extent_item, 1);
Chris Mason4d775672007-04-20 20:23:12 -04001185 btrfs_set_extent_owner(&extent_item, owner);
Chris Masonfec577f2007-02-26 10:40:21 -05001186
Chris Mason26b80032007-08-08 20:17:12 -04001187 WARN_ON(num_blocks < 1);
Chris Mason6702ed42007-08-07 16:15:09 -04001188 ret = find_free_extent(trans, root, num_blocks, empty_size,
1189 search_start, search_end, hint_block, ins,
Chris Mason26b80032007-08-08 20:17:12 -04001190 trans->alloc_exclude_start,
1191 trans->alloc_exclude_nr, data);
Chris Masonccd467d2007-06-28 15:57:36 -04001192 BUG_ON(ret);
Chris Masonf2654de2007-06-26 12:20:46 -04001193 if (ret)
1194 return ret;
1195
Chris Mason4b52dff2007-06-26 10:06:50 -04001196 super_blocks_used = btrfs_super_blocks_used(&info->super_copy);
1197 btrfs_set_super_blocks_used(&info->super_copy, super_blocks_used +
Chris Mason1261ec42007-03-20 20:35:03 -04001198 num_blocks);
Chris Mason26b80032007-08-08 20:17:12 -04001199
1200 if (root == extent_root) {
1201 BUG_ON(num_blocks != 1);
1202 set_radix_bit(&root->fs_info->extent_ins_radix, ins->objectid);
1203 goto update_block;
1204 }
1205
1206 WARN_ON(trans->alloc_exclude_nr);
1207 trans->alloc_exclude_start = ins->objectid;
1208 trans->alloc_exclude_nr = ins->offset;
Chris Masone089f052007-03-16 16:20:31 -04001209 ret = btrfs_insert_item(trans, extent_root, ins, &extent_item,
1210 sizeof(extent_item));
Chris Mason037e6392007-03-07 11:50:24 -05001211
Chris Mason26b80032007-08-08 20:17:12 -04001212 trans->alloc_exclude_start = 0;
1213 trans->alloc_exclude_nr = 0;
1214
Chris Masonccd467d2007-06-28 15:57:36 -04001215 BUG_ON(ret);
Chris Masone089f052007-03-16 16:20:31 -04001216 finish_current_insert(trans, extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -04001217 pending_ret = del_pending_extents(trans, extent_root);
Chris Masone37c9e62007-05-09 20:13:14 -04001218 if (ret) {
Chris Mason037e6392007-03-07 11:50:24 -05001219 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -04001220 }
1221 if (pending_ret) {
Chris Mason037e6392007-03-07 11:50:24 -05001222 return pending_ret;
Chris Masone37c9e62007-05-09 20:13:14 -04001223 }
Chris Mason26b80032007-08-08 20:17:12 -04001224
1225update_block:
Chris Mason1e2677e2007-05-29 16:52:18 -04001226 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0,
1227 data);
Chris Masonfabb5682007-06-07 22:13:21 -04001228 BUG_ON(ret);
Chris Mason037e6392007-03-07 11:50:24 -05001229 return 0;
Chris Masonfec577f2007-02-26 10:40:21 -05001230}
1231
1232/*
1233 * helper function to allocate a block for a given tree
1234 * returns the tree buffer or NULL.
1235 */
Chris Masone20d96d2007-03-22 12:13:20 -04001236struct buffer_head *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Chris Mason6702ed42007-08-07 16:15:09 -04001237 struct btrfs_root *root, u64 hint,
1238 u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05001239{
Chris Masone2fa7222007-03-12 16:22:34 -04001240 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -05001241 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -04001242 struct buffer_head *buf;
Chris Masonfec577f2007-02-26 10:40:21 -05001243
Chris Mason4d775672007-04-20 20:23:12 -04001244 ret = btrfs_alloc_extent(trans, root, root->root_key.objectid,
Chris Mason6702ed42007-08-07 16:15:09 -04001245 1, empty_size, hint,
1246 (unsigned long)-1, &ins, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05001247 if (ret) {
Chris Mason54aa1f42007-06-22 14:16:25 -04001248 BUG_ON(ret > 0);
1249 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05001250 }
Chris Masond98237b2007-03-28 13:57:48 -04001251 buf = btrfs_find_create_tree_block(root, ins.objectid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001252 if (!buf) {
1253 btrfs_free_extent(trans, root, ins.objectid, 1, 0);
1254 return ERR_PTR(-ENOMEM);
1255 }
Chris Mason6702ed42007-08-07 16:15:09 -04001256 WARN_ON(buffer_dirty(buf));
Chris Masondf2ce342007-03-23 11:00:45 -04001257 set_buffer_uptodate(buf);
Chris Mason090d1872007-05-01 08:53:32 -04001258 set_buffer_checked(buf);
Chris Mason7c4452b2007-04-28 09:29:35 -04001259 set_radix_bit(&trans->transaction->dirty_pages, buf->b_page->index);
Chris Masonfec577f2007-02-26 10:40:21 -05001260 return buf;
1261}
Chris Masona28ec192007-03-06 20:08:01 -05001262
Chris Mason6407bf62007-03-27 06:33:00 -04001263static int drop_leaf_ref(struct btrfs_trans_handle *trans,
1264 struct btrfs_root *root, struct buffer_head *cur)
1265{
1266 struct btrfs_disk_key *key;
1267 struct btrfs_leaf *leaf;
1268 struct btrfs_file_extent_item *fi;
1269 int i;
1270 int nritems;
1271 int ret;
1272
1273 BUG_ON(!btrfs_is_leaf(btrfs_buffer_node(cur)));
1274 leaf = btrfs_buffer_leaf(cur);
1275 nritems = btrfs_header_nritems(&leaf->header);
1276 for (i = 0; i < nritems; i++) {
Chris Mason3a686372007-05-24 13:35:57 -04001277 u64 disk_blocknr;
Chris Mason6407bf62007-03-27 06:33:00 -04001278 key = &leaf->items[i].key;
1279 if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY)
1280 continue;
1281 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
Chris Mason236454df2007-04-19 13:37:44 -04001282 if (btrfs_file_extent_type(fi) == BTRFS_FILE_EXTENT_INLINE)
1283 continue;
Chris Mason6407bf62007-03-27 06:33:00 -04001284 /*
1285 * FIXME make sure to insert a trans record that
1286 * repeats the snapshot del on crash
1287 */
Chris Mason3a686372007-05-24 13:35:57 -04001288 disk_blocknr = btrfs_file_extent_disk_blocknr(fi);
1289 if (disk_blocknr == 0)
1290 continue;
1291 ret = btrfs_free_extent(trans, root, disk_blocknr,
Chris Mason6407bf62007-03-27 06:33:00 -04001292 btrfs_file_extent_disk_num_blocks(fi),
1293 0);
1294 BUG_ON(ret);
1295 }
1296 return 0;
1297}
1298
Chris Masone0115992007-06-19 16:23:05 -04001299static void reada_walk_down(struct btrfs_root *root,
1300 struct btrfs_node *node)
1301{
1302 int i;
1303 u32 nritems;
1304 u64 blocknr;
1305 int ret;
1306 u32 refs;
1307
1308 nritems = btrfs_header_nritems(&node->header);
1309 for (i = 0; i < nritems; i++) {
1310 blocknr = btrfs_node_blockptr(node, i);
1311 ret = lookup_extent_ref(NULL, root, blocknr, 1, &refs);
1312 BUG_ON(ret);
1313 if (refs != 1)
1314 continue;
1315 ret = readahead_tree_block(root, blocknr);
1316 if (ret)
1317 break;
1318 }
1319}
1320
Chris Mason9aca1d52007-03-13 11:09:37 -04001321/*
1322 * helper function for drop_snapshot, this walks down the tree dropping ref
1323 * counts as it goes.
1324 */
Chris Masone089f052007-03-16 16:20:31 -04001325static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
1326 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05001327{
Chris Masone20d96d2007-03-22 12:13:20 -04001328 struct buffer_head *next;
1329 struct buffer_head *cur;
Chris Mason20524f02007-03-10 06:35:47 -05001330 u64 blocknr;
1331 int ret;
1332 u32 refs;
1333
Chris Mason5caf2a02007-04-02 11:20:42 -04001334 WARN_ON(*level < 0);
1335 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Masonb18c6682007-04-17 13:26:50 -04001336 ret = lookup_extent_ref(trans, root, bh_blocknr(path->nodes[*level]),
Chris Mason6407bf62007-03-27 06:33:00 -04001337 1, &refs);
Chris Mason20524f02007-03-10 06:35:47 -05001338 BUG_ON(ret);
1339 if (refs > 1)
1340 goto out;
Chris Masone0115992007-06-19 16:23:05 -04001341
Chris Mason9aca1d52007-03-13 11:09:37 -04001342 /*
1343 * walk down to the last node level and free all the leaves
1344 */
Chris Mason6407bf62007-03-27 06:33:00 -04001345 while(*level >= 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001346 WARN_ON(*level < 0);
1347 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason20524f02007-03-10 06:35:47 -05001348 cur = path->nodes[*level];
Chris Masone0115992007-06-19 16:23:05 -04001349
1350 if (*level > 0 && path->slots[*level] == 0)
1351 reada_walk_down(root, btrfs_buffer_node(cur));
1352
Chris Mason2c90e5d2007-04-02 10:50:19 -04001353 if (btrfs_header_level(btrfs_buffer_header(cur)) != *level)
1354 WARN_ON(1);
Chris Masone0115992007-06-19 16:23:05 -04001355
Chris Mason7518a232007-03-12 12:01:18 -04001356 if (path->slots[*level] >=
Chris Masone20d96d2007-03-22 12:13:20 -04001357 btrfs_header_nritems(btrfs_buffer_header(cur)))
Chris Mason20524f02007-03-10 06:35:47 -05001358 break;
Chris Mason6407bf62007-03-27 06:33:00 -04001359 if (*level == 0) {
1360 ret = drop_leaf_ref(trans, root, cur);
1361 BUG_ON(ret);
1362 break;
1363 }
Chris Masone20d96d2007-03-22 12:13:20 -04001364 blocknr = btrfs_node_blockptr(btrfs_buffer_node(cur),
1365 path->slots[*level]);
Chris Masonb18c6682007-04-17 13:26:50 -04001366 ret = lookup_extent_ref(trans, root, blocknr, 1, &refs);
Chris Mason6407bf62007-03-27 06:33:00 -04001367 BUG_ON(ret);
1368 if (refs != 1) {
Chris Mason20524f02007-03-10 06:35:47 -05001369 path->slots[*level]++;
Chris Masone089f052007-03-16 16:20:31 -04001370 ret = btrfs_free_extent(trans, root, blocknr, 1, 1);
Chris Mason20524f02007-03-10 06:35:47 -05001371 BUG_ON(ret);
1372 continue;
1373 }
Chris Mason20524f02007-03-10 06:35:47 -05001374 next = read_tree_block(root, blocknr);
Chris Mason5caf2a02007-04-02 11:20:42 -04001375 WARN_ON(*level <= 0);
Chris Mason83e15a22007-03-12 09:03:27 -04001376 if (path->nodes[*level-1])
Chris Mason234b63a2007-03-13 10:46:10 -04001377 btrfs_block_release(root, path->nodes[*level-1]);
Chris Mason20524f02007-03-10 06:35:47 -05001378 path->nodes[*level-1] = next;
Chris Masone20d96d2007-03-22 12:13:20 -04001379 *level = btrfs_header_level(btrfs_buffer_header(next));
Chris Mason20524f02007-03-10 06:35:47 -05001380 path->slots[*level] = 0;
1381 }
1382out:
Chris Mason5caf2a02007-04-02 11:20:42 -04001383 WARN_ON(*level < 0);
1384 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason6407bf62007-03-27 06:33:00 -04001385 ret = btrfs_free_extent(trans, root,
Chris Mason7eccb902007-04-11 15:53:25 -04001386 bh_blocknr(path->nodes[*level]), 1, 1);
Chris Mason234b63a2007-03-13 10:46:10 -04001387 btrfs_block_release(root, path->nodes[*level]);
Chris Mason20524f02007-03-10 06:35:47 -05001388 path->nodes[*level] = NULL;
1389 *level += 1;
1390 BUG_ON(ret);
1391 return 0;
1392}
1393
Chris Mason9aca1d52007-03-13 11:09:37 -04001394/*
1395 * helper for dropping snapshots. This walks back up the tree in the path
1396 * to find the first node higher up where we haven't yet gone through
1397 * all the slots
1398 */
Chris Masone089f052007-03-16 16:20:31 -04001399static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root
1400 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05001401{
1402 int i;
1403 int slot;
1404 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04001405 struct btrfs_root_item *root_item = &root->root_item;
1406
Chris Mason234b63a2007-03-13 10:46:10 -04001407 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Mason20524f02007-03-10 06:35:47 -05001408 slot = path->slots[i];
Chris Masone20d96d2007-03-22 12:13:20 -04001409 if (slot < btrfs_header_nritems(
1410 btrfs_buffer_header(path->nodes[i])) - 1) {
Chris Mason9f3a7422007-08-07 15:52:19 -04001411 struct btrfs_node *node;
1412 node = btrfs_buffer_node(path->nodes[i]);
Chris Mason20524f02007-03-10 06:35:47 -05001413 path->slots[i]++;
1414 *level = i;
Chris Mason9f3a7422007-08-07 15:52:19 -04001415 WARN_ON(*level == 0);
1416 memcpy(&root_item->drop_progress,
1417 &node->ptrs[path->slots[i]].key,
1418 sizeof(root_item->drop_progress));
1419 root_item->drop_level = i;
Chris Mason20524f02007-03-10 06:35:47 -05001420 return 0;
1421 } else {
Chris Masone089f052007-03-16 16:20:31 -04001422 ret = btrfs_free_extent(trans, root,
Chris Mason7eccb902007-04-11 15:53:25 -04001423 bh_blocknr(path->nodes[*level]),
Chris Masone089f052007-03-16 16:20:31 -04001424 1, 1);
Chris Mason6407bf62007-03-27 06:33:00 -04001425 BUG_ON(ret);
Chris Mason234b63a2007-03-13 10:46:10 -04001426 btrfs_block_release(root, path->nodes[*level]);
Chris Mason83e15a22007-03-12 09:03:27 -04001427 path->nodes[*level] = NULL;
Chris Mason20524f02007-03-10 06:35:47 -05001428 *level = i + 1;
Chris Mason20524f02007-03-10 06:35:47 -05001429 }
1430 }
1431 return 1;
1432}
1433
Chris Mason9aca1d52007-03-13 11:09:37 -04001434/*
1435 * drop the reference count on the tree rooted at 'snap'. This traverses
1436 * the tree freeing any blocks that have a ref count of zero after being
1437 * decremented.
1438 */
Chris Masone089f052007-03-16 16:20:31 -04001439int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason9f3a7422007-08-07 15:52:19 -04001440 *root)
Chris Mason20524f02007-03-10 06:35:47 -05001441{
Chris Mason3768f362007-03-13 16:47:54 -04001442 int ret = 0;
Chris Mason9aca1d52007-03-13 11:09:37 -04001443 int wret;
Chris Mason20524f02007-03-10 06:35:47 -05001444 int level;
Chris Mason5caf2a02007-04-02 11:20:42 -04001445 struct btrfs_path *path;
Chris Mason20524f02007-03-10 06:35:47 -05001446 int i;
1447 int orig_level;
Chris Mason9f3a7422007-08-07 15:52:19 -04001448 int num_walks = 0;
1449 struct btrfs_root_item *root_item = &root->root_item;
Chris Mason20524f02007-03-10 06:35:47 -05001450
Chris Mason5caf2a02007-04-02 11:20:42 -04001451 path = btrfs_alloc_path();
1452 BUG_ON(!path);
Chris Mason20524f02007-03-10 06:35:47 -05001453
Chris Mason9f3a7422007-08-07 15:52:19 -04001454 level = btrfs_header_level(btrfs_buffer_header(root->node));
Chris Mason20524f02007-03-10 06:35:47 -05001455 orig_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04001456 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
1457 path->nodes[level] = root->node;
1458 path->slots[level] = 0;
1459 } else {
1460 struct btrfs_key key;
1461 struct btrfs_disk_key *found_key;
1462 struct btrfs_node *node;
Chris Mason6702ed42007-08-07 16:15:09 -04001463
Chris Mason9f3a7422007-08-07 15:52:19 -04001464 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Chris Mason6702ed42007-08-07 16:15:09 -04001465 level = root_item->drop_level;
1466 path->lowest_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04001467 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason6702ed42007-08-07 16:15:09 -04001468 if (wret < 0) {
Chris Mason9f3a7422007-08-07 15:52:19 -04001469 ret = wret;
1470 goto out;
1471 }
Chris Mason9f3a7422007-08-07 15:52:19 -04001472 node = btrfs_buffer_node(path->nodes[level]);
1473 found_key = &node->ptrs[path->slots[level]].key;
1474 WARN_ON(memcmp(found_key, &root_item->drop_progress,
1475 sizeof(*found_key)));
1476 }
Chris Mason20524f02007-03-10 06:35:47 -05001477 while(1) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001478 wret = walk_down_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04001479 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05001480 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04001481 if (wret < 0)
1482 ret = wret;
1483
Chris Mason5caf2a02007-04-02 11:20:42 -04001484 wret = walk_up_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04001485 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05001486 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04001487 if (wret < 0)
1488 ret = wret;
Chris Mason9f3a7422007-08-07 15:52:19 -04001489 num_walks++;
Chris Masonf4468e92007-08-08 10:08:58 -04001490 if (num_walks > 2) {
Chris Mason9f3a7422007-08-07 15:52:19 -04001491 ret = -EAGAIN;
1492 get_bh(root->node);
1493 break;
1494 }
Chris Mason20524f02007-03-10 06:35:47 -05001495 }
Chris Mason83e15a22007-03-12 09:03:27 -04001496 for (i = 0; i <= orig_level; i++) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001497 if (path->nodes[i]) {
1498 btrfs_block_release(root, path->nodes[i]);
Chris Mason6702ed42007-08-07 16:15:09 -04001499 path->nodes[i] = 0;
Chris Mason83e15a22007-03-12 09:03:27 -04001500 }
Chris Mason20524f02007-03-10 06:35:47 -05001501 }
Chris Mason9f3a7422007-08-07 15:52:19 -04001502out:
Chris Mason5caf2a02007-04-02 11:20:42 -04001503 btrfs_free_path(path);
Chris Mason9aca1d52007-03-13 11:09:37 -04001504 return ret;
Chris Mason20524f02007-03-10 06:35:47 -05001505}
Chris Mason9078a3e2007-04-26 16:46:15 -04001506
Chris Masonbe744172007-05-06 10:15:01 -04001507static int free_block_group_radix(struct radix_tree_root *radix)
Chris Mason9078a3e2007-04-26 16:46:15 -04001508{
1509 int ret;
1510 struct btrfs_block_group_cache *cache[8];
1511 int i;
1512
1513 while(1) {
Chris Masonbe744172007-05-06 10:15:01 -04001514 ret = radix_tree_gang_lookup(radix, (void **)cache, 0,
Chris Mason9078a3e2007-04-26 16:46:15 -04001515 ARRAY_SIZE(cache));
1516 if (!ret)
1517 break;
1518 for (i = 0; i < ret; i++) {
Chris Masonbe744172007-05-06 10:15:01 -04001519 radix_tree_delete(radix, cache[i]->key.objectid +
Chris Mason9078a3e2007-04-26 16:46:15 -04001520 cache[i]->key.offset - 1);
1521 kfree(cache[i]);
1522 }
1523 }
1524 return 0;
1525}
1526
Chris Masonbe744172007-05-06 10:15:01 -04001527int btrfs_free_block_groups(struct btrfs_fs_info *info)
1528{
1529 int ret;
1530 int ret2;
Chris Masone37c9e62007-05-09 20:13:14 -04001531 unsigned long gang[16];
1532 int i;
Chris Masonbe744172007-05-06 10:15:01 -04001533
1534 ret = free_block_group_radix(&info->block_group_radix);
1535 ret2 = free_block_group_radix(&info->block_group_data_radix);
1536 if (ret)
1537 return ret;
1538 if (ret2)
1539 return ret2;
Chris Masone37c9e62007-05-09 20:13:14 -04001540
1541 while(1) {
1542 ret = find_first_radix_bit(&info->extent_map_radix,
1543 gang, 0, ARRAY_SIZE(gang));
1544 if (!ret)
1545 break;
1546 for (i = 0; i < ret; i++) {
1547 clear_radix_bit(&info->extent_map_radix, gang[i]);
1548 }
1549 }
Chris Masonbe744172007-05-06 10:15:01 -04001550 return 0;
1551}
1552
Chris Mason9078a3e2007-04-26 16:46:15 -04001553int btrfs_read_block_groups(struct btrfs_root *root)
1554{
1555 struct btrfs_path *path;
1556 int ret;
1557 int err = 0;
1558 struct btrfs_block_group_item *bi;
1559 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04001560 struct btrfs_fs_info *info = root->fs_info;
1561 struct radix_tree_root *radix;
Chris Mason9078a3e2007-04-26 16:46:15 -04001562 struct btrfs_key key;
1563 struct btrfs_key found_key;
1564 struct btrfs_leaf *leaf;
Chris Mason84f54cf2007-06-12 07:43:08 -04001565 u64 group_size_blocks;
Chris Mason31f3c992007-04-30 15:25:45 -04001566 u64 used;
Chris Mason9078a3e2007-04-26 16:46:15 -04001567
Chris Mason84f54cf2007-06-12 07:43:08 -04001568 group_size_blocks = BTRFS_BLOCK_GROUP_SIZE >>
1569 root->fs_info->sb->s_blocksize_bits;
Chris Masonbe744172007-05-06 10:15:01 -04001570 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04001571 key.objectid = 0;
1572 key.offset = group_size_blocks;
1573 key.flags = 0;
1574 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
1575
1576 path = btrfs_alloc_path();
1577 if (!path)
1578 return -ENOMEM;
1579
1580 while(1) {
Chris Masonbe744172007-05-06 10:15:01 -04001581 ret = btrfs_search_slot(NULL, info->extent_root,
Chris Mason9078a3e2007-04-26 16:46:15 -04001582 &key, path, 0, 0);
1583 if (ret != 0) {
1584 err = ret;
1585 break;
1586 }
1587 leaf = btrfs_buffer_leaf(path->nodes[0]);
1588 btrfs_disk_key_to_cpu(&found_key,
1589 &leaf->items[path->slots[0]].key);
1590 cache = kmalloc(sizeof(*cache), GFP_NOFS);
1591 if (!cache) {
1592 err = -1;
1593 break;
1594 }
Chris Mason3e1ad542007-05-07 20:03:49 -04001595
Chris Mason9078a3e2007-04-26 16:46:15 -04001596 bi = btrfs_item_ptr(leaf, path->slots[0],
1597 struct btrfs_block_group_item);
Chris Mason1e2677e2007-05-29 16:52:18 -04001598 if (bi->flags & BTRFS_BLOCK_GROUP_DATA) {
1599 radix = &info->block_group_data_radix;
1600 cache->data = 1;
1601 } else {
1602 radix = &info->block_group_radix;
1603 cache->data = 0;
1604 }
1605
Chris Mason9078a3e2007-04-26 16:46:15 -04001606 memcpy(&cache->item, bi, sizeof(*bi));
1607 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Mason31f3c992007-04-30 15:25:45 -04001608 cache->last_alloc = cache->key.objectid;
1609 cache->first_free = cache->key.objectid;
Chris Masonbe744172007-05-06 10:15:01 -04001610 cache->pinned = 0;
Chris Masone37c9e62007-05-09 20:13:14 -04001611 cache->cached = 0;
1612
Chris Mason3e1ad542007-05-07 20:03:49 -04001613 cache->radix = radix;
1614
Chris Mason9078a3e2007-04-26 16:46:15 -04001615 key.objectid = found_key.objectid + found_key.offset;
1616 btrfs_release_path(root, path);
Chris Masonbe744172007-05-06 10:15:01 -04001617 ret = radix_tree_insert(radix, found_key.objectid +
Chris Mason9078a3e2007-04-26 16:46:15 -04001618 found_key.offset - 1,
1619 (void *)cache);
1620 BUG_ON(ret);
Chris Mason31f3c992007-04-30 15:25:45 -04001621 used = btrfs_block_group_used(bi);
Chris Mason84f54cf2007-06-12 07:43:08 -04001622 if (used < div_factor(key.offset, 8)) {
Chris Masonbe744172007-05-06 10:15:01 -04001623 radix_tree_tag_set(radix, found_key.objectid +
Chris Mason31f3c992007-04-30 15:25:45 -04001624 found_key.offset - 1,
1625 BTRFS_BLOCK_GROUP_AVAIL);
1626 }
Chris Mason9078a3e2007-04-26 16:46:15 -04001627 if (key.objectid >=
Chris Mason4b52dff2007-06-26 10:06:50 -04001628 btrfs_super_total_blocks(&info->super_copy))
Chris Mason9078a3e2007-04-26 16:46:15 -04001629 break;
1630 }
1631
1632 btrfs_free_path(path);
1633 return 0;
1634}