blob: ff3f7c2be605dc3006200cc4974f33028e615df2 [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;
42 int found = 0;
43
44 root = root->fs_info->extent_root;
45 extent_radix = &root->fs_info->extent_map_radix;
46
47 if (block_group->cached)
48 return 0;
49 if (block_group->data)
50 return 0;
51 path = btrfs_alloc_path();
52 if (!path)
53 return -ENOMEM;
Chris Mason2cc58cf2007-08-27 16:49:44 -040054 path->reada = 2;
Chris Masone37c9e62007-05-09 20:13:14 -040055 key.objectid = block_group->key.objectid;
56 key.flags = 0;
57 key.offset = 0;
58 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
59 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
60 if (ret < 0)
61 return ret;
62 if (ret && path->slots[0] > 0)
63 path->slots[0]--;
64 while(1) {
65 leaf = btrfs_buffer_leaf(path->nodes[0]);
66 slot = path->slots[0];
67 if (slot >= btrfs_header_nritems(&leaf->header)) {
68 ret = btrfs_next_leaf(root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -040069 if (ret < 0)
70 goto err;
Chris Masonde428b62007-05-18 13:28:27 -040071 if (ret == 0) {
Chris Masone37c9e62007-05-09 20:13:14 -040072 continue;
Chris Masonde428b62007-05-18 13:28:27 -040073 } else {
Chris Masone37c9e62007-05-09 20:13:14 -040074 if (found) {
75 hole_size = block_group->key.objectid +
76 block_group->key.offset - last;
77 } else {
78 last = block_group->key.objectid;
79 hole_size = block_group->key.offset;
80 }
81 for (i = 0; i < hole_size; i++) {
82 set_radix_bit(extent_radix,
83 last + i);
84 }
85 break;
86 }
87 }
88 btrfs_disk_key_to_cpu(&key, &leaf->items[slot].key);
89 if (key.objectid >= block_group->key.objectid +
90 block_group->key.offset) {
91 if (found) {
92 hole_size = block_group->key.objectid +
93 block_group->key.offset - last;
94 } else {
95 last = block_group->key.objectid;
96 hole_size = block_group->key.offset;
97 }
98 for (i = 0; i < hole_size; i++) {
99 set_radix_bit(extent_radix, last + i);
100 }
101 break;
102 }
103 if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) {
104 if (!found) {
105 last = key.objectid + key.offset;
106 found = 1;
107 } else {
108 hole_size = key.objectid - last;
109 for (i = 0; i < hole_size; i++) {
110 set_radix_bit(extent_radix, last + i);
111 }
112 last = key.objectid + key.offset;
113 }
114 }
115 path->slots[0]++;
116 }
117
118 block_group->cached = 1;
Chris Mason54aa1f42007-06-22 14:16:25 -0400119err:
Chris Masone37c9e62007-05-09 20:13:14 -0400120 btrfs_free_path(path);
121 return 0;
122}
123
Chris Mason5276aed2007-06-11 21:33:38 -0400124struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
125 btrfs_fs_info *info,
126 u64 blocknr)
Chris Masonbe744172007-05-06 10:15:01 -0400127{
128 struct btrfs_block_group_cache *block_group;
129 int ret;
130
131 ret = radix_tree_gang_lookup(&info->block_group_radix,
132 (void **)&block_group,
133 blocknr, 1);
134 if (ret) {
Chris Mason3e1ad542007-05-07 20:03:49 -0400135 if (block_group->key.objectid <= blocknr && blocknr <=
Chris Masonbe744172007-05-06 10:15:01 -0400136 block_group->key.objectid + block_group->key.offset)
137 return block_group;
138 }
139 ret = radix_tree_gang_lookup(&info->block_group_data_radix,
140 (void **)&block_group,
141 blocknr, 1);
142 if (ret) {
Chris Mason3e1ad542007-05-07 20:03:49 -0400143 if (block_group->key.objectid <= blocknr && blocknr <=
Chris Masonbe744172007-05-06 10:15:01 -0400144 block_group->key.objectid + block_group->key.offset)
145 return block_group;
146 }
Chris Masonbe744172007-05-06 10:15:01 -0400147 return NULL;
148}
149
Chris Masone37c9e62007-05-09 20:13:14 -0400150static u64 leaf_range(struct btrfs_root *root)
151{
152 u64 size = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason84f54cf2007-06-12 07:43:08 -0400153 do_div(size, sizeof(struct btrfs_extent_item) +
154 sizeof(struct btrfs_item));
Chris Masone37c9e62007-05-09 20:13:14 -0400155 return size;
156}
157
158static u64 find_search_start(struct btrfs_root *root,
159 struct btrfs_block_group_cache **cache_ret,
160 u64 search_start, int num)
161{
162 unsigned long gang[8];
163 int ret;
164 struct btrfs_block_group_cache *cache = *cache_ret;
165 u64 last = max(search_start, cache->key.objectid);
166
167 if (cache->data)
168 goto out;
Chris Masone37c9e62007-05-09 20:13:14 -0400169again:
Chris Mason54aa1f42007-06-22 14:16:25 -0400170 ret = cache_block_group(root, cache);
171 if (ret)
172 goto out;
Chris Masone37c9e62007-05-09 20:13:14 -0400173 while(1) {
174 ret = find_first_radix_bit(&root->fs_info->extent_map_radix,
175 gang, last, ARRAY_SIZE(gang));
176 if (!ret)
177 goto out;
178 last = gang[ret-1] + 1;
179 if (num > 1) {
180 if (ret != ARRAY_SIZE(gang)) {
181 goto new_group;
182 }
183 if (gang[ret-1] - gang[0] > leaf_range(root)) {
184 continue;
185 }
186 }
187 if (gang[0] >= cache->key.objectid + cache->key.offset) {
188 goto new_group;
189 }
190 return gang[0];
191 }
192out:
193 return max(cache->last_alloc, search_start);
194
195new_group:
Chris Mason5276aed2007-06-11 21:33:38 -0400196 cache = btrfs_lookup_block_group(root->fs_info,
197 last + cache->key.offset - 1);
Chris Masone37c9e62007-05-09 20:13:14 -0400198 if (!cache) {
199 return max((*cache_ret)->last_alloc, search_start);
200 }
201 cache = btrfs_find_block_group(root, cache,
Chris Masonde428b62007-05-18 13:28:27 -0400202 last + cache->key.offset - 1, 0, 0);
Chris Masone37c9e62007-05-09 20:13:14 -0400203 *cache_ret = cache;
204 goto again;
205}
206
Chris Mason84f54cf2007-06-12 07:43:08 -0400207static u64 div_factor(u64 num, int factor)
208{
209 num *= factor;
210 do_div(num, 10);
211 return num;
212}
213
Chris Mason31f3c992007-04-30 15:25:45 -0400214struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
215 struct btrfs_block_group_cache
Chris Masonbe744172007-05-06 10:15:01 -0400216 *hint, u64 search_start,
Chris Masonde428b62007-05-18 13:28:27 -0400217 int data, int owner)
Chris Masoncd1bc462007-04-27 10:08:34 -0400218{
219 struct btrfs_block_group_cache *cache[8];
Chris Mason31f3c992007-04-30 15:25:45 -0400220 struct btrfs_block_group_cache *found_group = NULL;
Chris Masoncd1bc462007-04-27 10:08:34 -0400221 struct btrfs_fs_info *info = root->fs_info;
Chris Masonbe744172007-05-06 10:15:01 -0400222 struct radix_tree_root *radix;
Chris Mason1e2677e2007-05-29 16:52:18 -0400223 struct radix_tree_root *swap_radix;
Chris Masoncd1bc462007-04-27 10:08:34 -0400224 u64 used;
Chris Mason31f3c992007-04-30 15:25:45 -0400225 u64 last = 0;
226 u64 hint_last;
Chris Masoncd1bc462007-04-27 10:08:34 -0400227 int i;
228 int ret;
Chris Mason31f3c992007-04-30 15:25:45 -0400229 int full_search = 0;
Chris Masonde428b62007-05-18 13:28:27 -0400230 int factor = 8;
Chris Mason1e2677e2007-05-29 16:52:18 -0400231 int data_swap = 0;
Chris Masonde428b62007-05-18 13:28:27 -0400232
233 if (!owner)
234 factor = 5;
Chris Masonbe744172007-05-06 10:15:01 -0400235
Chris Mason1e2677e2007-05-29 16:52:18 -0400236 if (data) {
Chris Masonbe744172007-05-06 10:15:01 -0400237 radix = &info->block_group_data_radix;
Chris Mason1e2677e2007-05-29 16:52:18 -0400238 swap_radix = &info->block_group_radix;
239 } else {
Chris Masonbe744172007-05-06 10:15:01 -0400240 radix = &info->block_group_radix;
Chris Mason1e2677e2007-05-29 16:52:18 -0400241 swap_radix = &info->block_group_data_radix;
242 }
Chris Masonbe744172007-05-06 10:15:01 -0400243
244 if (search_start) {
245 struct btrfs_block_group_cache *shint;
Chris Mason5276aed2007-06-11 21:33:38 -0400246 shint = btrfs_lookup_block_group(info, search_start);
Chris Masonbe744172007-05-06 10:15:01 -0400247 if (shint->data == data) {
248 used = btrfs_block_group_used(&shint->item);
249 if (used + shint->pinned <
Chris Mason84f54cf2007-06-12 07:43:08 -0400250 div_factor(shint->key.offset, factor)) {
Chris Masonbe744172007-05-06 10:15:01 -0400251 return shint;
252 }
253 }
254 }
255 if (hint && hint->data == data) {
Chris Mason31f3c992007-04-30 15:25:45 -0400256 used = btrfs_block_group_used(&hint->item);
Chris Mason84f54cf2007-06-12 07:43:08 -0400257 if (used + hint->pinned <
258 div_factor(hint->key.offset, factor)) {
Chris Mason31f3c992007-04-30 15:25:45 -0400259 return hint;
260 }
Chris Mason84f54cf2007-06-12 07:43:08 -0400261 if (used >= div_factor(hint->key.offset, 8)) {
Chris Masonbe744172007-05-06 10:15:01 -0400262 radix_tree_tag_clear(radix,
263 hint->key.objectid +
264 hint->key.offset - 1,
265 BTRFS_BLOCK_GROUP_AVAIL);
266 }
Chris Mason8d7be552007-05-10 11:24:42 -0400267 last = hint->key.offset * 3;
Chris Masonbe744172007-05-06 10:15:01 -0400268 if (hint->key.objectid >= last)
Chris Masone37c9e62007-05-09 20:13:14 -0400269 last = max(search_start + hint->key.offset - 1,
270 hint->key.objectid - last);
Chris Masonbe744172007-05-06 10:15:01 -0400271 else
272 last = hint->key.objectid + hint->key.offset;
Chris Mason31f3c992007-04-30 15:25:45 -0400273 hint_last = last;
274 } else {
Chris Masone37c9e62007-05-09 20:13:14 -0400275 if (hint)
276 hint_last = max(hint->key.objectid, search_start);
277 else
278 hint_last = search_start;
279
280 last = hint_last;
Chris Mason31f3c992007-04-30 15:25:45 -0400281 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400282 while(1) {
Chris Masonbe744172007-05-06 10:15:01 -0400283 ret = radix_tree_gang_lookup_tag(radix, (void **)cache,
Chris Masoncd1bc462007-04-27 10:08:34 -0400284 last, ARRAY_SIZE(cache),
Chris Mason31f3c992007-04-30 15:25:45 -0400285 BTRFS_BLOCK_GROUP_AVAIL);
Chris Masoncd1bc462007-04-27 10:08:34 -0400286 if (!ret)
287 break;
288 for (i = 0; i < ret; i++) {
Chris Masonbe08c1b2007-05-03 09:06:49 -0400289 last = cache[i]->key.objectid +
290 cache[i]->key.offset;
Chris Masoncd1bc462007-04-27 10:08:34 -0400291 used = btrfs_block_group_used(&cache[i]->item);
Chris Masonbe744172007-05-06 10:15:01 -0400292 if (used + cache[i]->pinned <
Chris Mason84f54cf2007-06-12 07:43:08 -0400293 div_factor(cache[i]->key.offset, factor)) {
Chris Mason31f3c992007-04-30 15:25:45 -0400294 found_group = cache[i];
295 goto found;
Chris Masoncd1bc462007-04-27 10:08:34 -0400296 }
Chris Mason84f54cf2007-06-12 07:43:08 -0400297 if (used >= div_factor(cache[i]->key.offset, 8)) {
Chris Masonbe744172007-05-06 10:15:01 -0400298 radix_tree_tag_clear(radix,
299 cache[i]->key.objectid +
300 cache[i]->key.offset - 1,
301 BTRFS_BLOCK_GROUP_AVAIL);
302 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400303 }
Chris Masonde428b62007-05-18 13:28:27 -0400304 cond_resched();
Chris Masoncd1bc462007-04-27 10:08:34 -0400305 }
Chris Mason31f3c992007-04-30 15:25:45 -0400306 last = hint_last;
307again:
Chris Masoncd1bc462007-04-27 10:08:34 -0400308 while(1) {
Chris Masonbe744172007-05-06 10:15:01 -0400309 ret = radix_tree_gang_lookup(radix, (void **)cache,
310 last, ARRAY_SIZE(cache));
Chris Masoncd1bc462007-04-27 10:08:34 -0400311 if (!ret)
312 break;
313 for (i = 0; i < ret; i++) {
Chris Masonbe08c1b2007-05-03 09:06:49 -0400314 last = cache[i]->key.objectid +
315 cache[i]->key.offset;
Chris Masoncd1bc462007-04-27 10:08:34 -0400316 used = btrfs_block_group_used(&cache[i]->item);
Chris Masonbe744172007-05-06 10:15:01 -0400317 if (used + cache[i]->pinned < cache[i]->key.offset) {
Chris Mason31f3c992007-04-30 15:25:45 -0400318 found_group = cache[i];
319 goto found;
Chris Masoncd1bc462007-04-27 10:08:34 -0400320 }
Chris Masonbe744172007-05-06 10:15:01 -0400321 if (used >= cache[i]->key.offset) {
322 radix_tree_tag_clear(radix,
323 cache[i]->key.objectid +
324 cache[i]->key.offset - 1,
325 BTRFS_BLOCK_GROUP_AVAIL);
326 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400327 }
Chris Masonde428b62007-05-18 13:28:27 -0400328 cond_resched();
Chris Masoncd1bc462007-04-27 10:08:34 -0400329 }
Chris Mason31f3c992007-04-30 15:25:45 -0400330 if (!full_search) {
Chris Masonbe744172007-05-06 10:15:01 -0400331 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -0400332 full_search = 1;
333 goto again;
334 }
Chris Mason1e2677e2007-05-29 16:52:18 -0400335 if (!data_swap) {
336 struct radix_tree_root *tmp = radix;
337 data_swap = 1;
338 radix = swap_radix;
339 swap_radix = tmp;
340 last = search_start;
341 goto again;
342 }
Chris Mason31f3c992007-04-30 15:25:45 -0400343 if (!found_group) {
Chris Masonbe744172007-05-06 10:15:01 -0400344 ret = radix_tree_gang_lookup(radix,
Chris Mason31f3c992007-04-30 15:25:45 -0400345 (void **)&found_group, 0, 1);
Chris Mason1e2677e2007-05-29 16:52:18 -0400346 if (ret == 0) {
347 ret = radix_tree_gang_lookup(swap_radix,
348 (void **)&found_group,
349 0, 1);
350 }
Chris Mason31f3c992007-04-30 15:25:45 -0400351 BUG_ON(ret != 1);
352 }
Chris Masonbe744172007-05-06 10:15:01 -0400353found:
Chris Mason31f3c992007-04-30 15:25:45 -0400354 return found_group;
Chris Masoncd1bc462007-04-27 10:08:34 -0400355}
356
Chris Masonb18c6682007-04-17 13:26:50 -0400357int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
358 struct btrfs_root *root,
359 u64 blocknr, u64 num_blocks)
Chris Mason02217ed2007-03-02 16:08:05 -0500360{
Chris Mason5caf2a02007-04-02 11:20:42 -0400361 struct btrfs_path *path;
Chris Mason02217ed2007-03-02 16:08:05 -0500362 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400363 struct btrfs_key key;
Chris Mason234b63a2007-03-13 10:46:10 -0400364 struct btrfs_leaf *l;
365 struct btrfs_extent_item *item;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400366 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500367
Chris Mason5caf2a02007-04-02 11:20:42 -0400368 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -0400369 if (!path)
370 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -0400371
Chris Mason02217ed2007-03-02 16:08:05 -0500372 key.objectid = blocknr;
373 key.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -0400374 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Mason6407bf62007-03-27 06:33:00 -0400375 key.offset = num_blocks;
Chris Mason5caf2a02007-04-02 11:20:42 -0400376 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -0400377 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400378 if (ret < 0)
379 return ret;
Chris Masona429e512007-04-18 16:15:28 -0400380 if (ret != 0) {
Chris Masona28ec192007-03-06 20:08:01 -0500381 BUG();
Chris Masona429e512007-04-18 16:15:28 -0400382 }
Chris Mason02217ed2007-03-02 16:08:05 -0500383 BUG_ON(ret != 0);
Chris Mason5caf2a02007-04-02 11:20:42 -0400384 l = btrfs_buffer_leaf(path->nodes[0]);
385 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400386 refs = btrfs_extent_refs(item);
387 btrfs_set_extent_refs(item, refs + 1);
Chris Mason5caf2a02007-04-02 11:20:42 -0400388 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masona28ec192007-03-06 20:08:01 -0500389
Chris Mason5caf2a02007-04-02 11:20:42 -0400390 btrfs_release_path(root->fs_info->extent_root, path);
391 btrfs_free_path(path);
Chris Mason9f5fae22007-03-20 14:38:32 -0400392 finish_current_insert(trans, root->fs_info->extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -0400393 del_pending_extents(trans, root->fs_info->extent_root);
Chris Mason02217ed2007-03-02 16:08:05 -0500394 return 0;
395}
396
Chris Masone9d0b132007-08-10 14:06:19 -0400397int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
398 struct btrfs_root *root)
399{
400 finish_current_insert(trans, root->fs_info->extent_root);
401 del_pending_extents(trans, root->fs_info->extent_root);
402 return 0;
403}
404
Chris Masonb18c6682007-04-17 13:26:50 -0400405static int lookup_extent_ref(struct btrfs_trans_handle *trans,
406 struct btrfs_root *root, u64 blocknr,
407 u64 num_blocks, u32 *refs)
Chris Masona28ec192007-03-06 20:08:01 -0500408{
Chris Mason5caf2a02007-04-02 11:20:42 -0400409 struct btrfs_path *path;
Chris Masona28ec192007-03-06 20:08:01 -0500410 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400411 struct btrfs_key key;
Chris Mason234b63a2007-03-13 10:46:10 -0400412 struct btrfs_leaf *l;
413 struct btrfs_extent_item *item;
Chris Mason5caf2a02007-04-02 11:20:42 -0400414
415 path = btrfs_alloc_path();
Chris Masona28ec192007-03-06 20:08:01 -0500416 key.objectid = blocknr;
Chris Mason6407bf62007-03-27 06:33:00 -0400417 key.offset = num_blocks;
Chris Mason62e27492007-03-15 12:56:47 -0400418 key.flags = 0;
419 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Mason5caf2a02007-04-02 11:20:42 -0400420 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -0400421 0, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400422 if (ret < 0)
423 goto out;
Chris Masona28ec192007-03-06 20:08:01 -0500424 if (ret != 0)
425 BUG();
Chris Mason5caf2a02007-04-02 11:20:42 -0400426 l = btrfs_buffer_leaf(path->nodes[0]);
427 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400428 *refs = btrfs_extent_refs(item);
Chris Mason54aa1f42007-06-22 14:16:25 -0400429out:
Chris Mason5caf2a02007-04-02 11:20:42 -0400430 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -0500431 return 0;
432}
433
Chris Masonc5739bb2007-04-10 09:27:04 -0400434int btrfs_inc_root_ref(struct btrfs_trans_handle *trans,
435 struct btrfs_root *root)
436{
Chris Masonb18c6682007-04-17 13:26:50 -0400437 return btrfs_inc_extent_ref(trans, root, bh_blocknr(root->node), 1);
Chris Masonc5739bb2007-04-10 09:27:04 -0400438}
439
Chris Masone089f052007-03-16 16:20:31 -0400440int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Masone20d96d2007-03-22 12:13:20 -0400441 struct buffer_head *buf)
Chris Mason02217ed2007-03-02 16:08:05 -0500442{
443 u64 blocknr;
Chris Masone20d96d2007-03-22 12:13:20 -0400444 struct btrfs_node *buf_node;
Chris Mason6407bf62007-03-27 06:33:00 -0400445 struct btrfs_leaf *buf_leaf;
446 struct btrfs_disk_key *key;
447 struct btrfs_file_extent_item *fi;
Chris Mason02217ed2007-03-02 16:08:05 -0500448 int i;
Chris Mason6407bf62007-03-27 06:33:00 -0400449 int leaf;
450 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -0400451 int faili;
452 int err;
Chris Masona28ec192007-03-06 20:08:01 -0500453
Chris Mason3768f362007-03-13 16:47:54 -0400454 if (!root->ref_cows)
Chris Masona28ec192007-03-06 20:08:01 -0500455 return 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400456 buf_node = btrfs_buffer_node(buf);
Chris Mason6407bf62007-03-27 06:33:00 -0400457 leaf = btrfs_is_leaf(buf_node);
458 buf_leaf = btrfs_buffer_leaf(buf);
Chris Masone20d96d2007-03-22 12:13:20 -0400459 for (i = 0; i < btrfs_header_nritems(&buf_node->header); i++) {
Chris Mason6407bf62007-03-27 06:33:00 -0400460 if (leaf) {
Chris Mason3a686372007-05-24 13:35:57 -0400461 u64 disk_blocknr;
Chris Mason6407bf62007-03-27 06:33:00 -0400462 key = &buf_leaf->items[i].key;
463 if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY)
464 continue;
465 fi = btrfs_item_ptr(buf_leaf, i,
466 struct btrfs_file_extent_item);
Chris Mason236454d2007-04-19 13:37:44 -0400467 if (btrfs_file_extent_type(fi) ==
468 BTRFS_FILE_EXTENT_INLINE)
469 continue;
Chris Mason3a686372007-05-24 13:35:57 -0400470 disk_blocknr = btrfs_file_extent_disk_blocknr(fi);
471 if (disk_blocknr == 0)
472 continue;
473 ret = btrfs_inc_extent_ref(trans, root, disk_blocknr,
Chris Mason6407bf62007-03-27 06:33:00 -0400474 btrfs_file_extent_disk_num_blocks(fi));
Chris Mason54aa1f42007-06-22 14:16:25 -0400475 if (ret) {
476 faili = i;
477 goto fail;
478 }
Chris Mason6407bf62007-03-27 06:33:00 -0400479 } else {
480 blocknr = btrfs_node_blockptr(buf_node, i);
Chris Masonb18c6682007-04-17 13:26:50 -0400481 ret = btrfs_inc_extent_ref(trans, root, blocknr, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400482 if (ret) {
483 faili = i;
484 goto fail;
485 }
Chris Mason6407bf62007-03-27 06:33:00 -0400486 }
Chris Mason02217ed2007-03-02 16:08:05 -0500487 }
488 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -0400489fail:
Chris Masonccd467d2007-06-28 15:57:36 -0400490 WARN_ON(1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400491 for (i =0; i < faili; i++) {
492 if (leaf) {
493 u64 disk_blocknr;
494 key = &buf_leaf->items[i].key;
495 if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY)
496 continue;
497 fi = btrfs_item_ptr(buf_leaf, i,
498 struct btrfs_file_extent_item);
499 if (btrfs_file_extent_type(fi) ==
500 BTRFS_FILE_EXTENT_INLINE)
501 continue;
502 disk_blocknr = btrfs_file_extent_disk_blocknr(fi);
503 if (disk_blocknr == 0)
504 continue;
505 err = btrfs_free_extent(trans, root, disk_blocknr,
506 btrfs_file_extent_disk_num_blocks(fi), 0);
507 BUG_ON(err);
508 } else {
509 blocknr = btrfs_node_blockptr(buf_node, i);
510 err = btrfs_free_extent(trans, root, blocknr, 1, 0);
511 BUG_ON(err);
512 }
513 }
514 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -0500515}
516
Chris Mason9078a3e2007-04-26 16:46:15 -0400517static int write_one_cache_group(struct btrfs_trans_handle *trans,
518 struct btrfs_root *root,
519 struct btrfs_path *path,
520 struct btrfs_block_group_cache *cache)
521{
522 int ret;
523 int pending_ret;
524 struct btrfs_root *extent_root = root->fs_info->extent_root;
525 struct btrfs_block_group_item *bi;
Chris Mason9078a3e2007-04-26 16:46:15 -0400526
Chris Mason9078a3e2007-04-26 16:46:15 -0400527 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400528 if (ret < 0)
529 goto fail;
Chris Mason9078a3e2007-04-26 16:46:15 -0400530 BUG_ON(ret);
531 bi = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
532 struct btrfs_block_group_item);
533 memcpy(bi, &cache->item, sizeof(*bi));
Chris Masonccd467d2007-06-28 15:57:36 -0400534 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason9078a3e2007-04-26 16:46:15 -0400535 btrfs_release_path(extent_root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -0400536fail:
Chris Mason9078a3e2007-04-26 16:46:15 -0400537 finish_current_insert(trans, extent_root);
538 pending_ret = del_pending_extents(trans, extent_root);
539 if (ret)
540 return ret;
541 if (pending_ret)
542 return pending_ret;
Chris Masonbe744172007-05-06 10:15:01 -0400543 if (cache->data)
544 cache->last_alloc = cache->first_free;
Chris Mason9078a3e2007-04-26 16:46:15 -0400545 return 0;
546
547}
548
Chris Masonbe744172007-05-06 10:15:01 -0400549static int write_dirty_block_radix(struct btrfs_trans_handle *trans,
550 struct btrfs_root *root,
551 struct radix_tree_root *radix)
Chris Mason9078a3e2007-04-26 16:46:15 -0400552{
553 struct btrfs_block_group_cache *cache[8];
554 int ret;
555 int err = 0;
556 int werr = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -0400557 int i;
558 struct btrfs_path *path;
Chris Mason54aa1f42007-06-22 14:16:25 -0400559 unsigned long off = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -0400560
561 path = btrfs_alloc_path();
562 if (!path)
563 return -ENOMEM;
564
565 while(1) {
566 ret = radix_tree_gang_lookup_tag(radix, (void **)cache,
Chris Mason54aa1f42007-06-22 14:16:25 -0400567 off, ARRAY_SIZE(cache),
Chris Mason9078a3e2007-04-26 16:46:15 -0400568 BTRFS_BLOCK_GROUP_DIRTY);
569 if (!ret)
570 break;
571 for (i = 0; i < ret; i++) {
Chris Mason54aa1f42007-06-22 14:16:25 -0400572 err = write_one_cache_group(trans, root,
573 path, cache[i]);
574 /*
575 * if we fail to write the cache group, we want
576 * to keep it marked dirty in hopes that a later
577 * write will work
578 */
579 if (err) {
580 werr = err;
581 off = cache[i]->key.objectid +
582 cache[i]->key.offset;
583 continue;
584 }
585
Chris Mason9078a3e2007-04-26 16:46:15 -0400586 radix_tree_tag_clear(radix, cache[i]->key.objectid +
587 cache[i]->key.offset - 1,
588 BTRFS_BLOCK_GROUP_DIRTY);
Chris Mason9078a3e2007-04-26 16:46:15 -0400589 }
590 }
591 btrfs_free_path(path);
592 return werr;
593}
594
Chris Masonbe744172007-05-06 10:15:01 -0400595int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
596 struct btrfs_root *root)
597{
598 int ret;
599 int ret2;
600 ret = write_dirty_block_radix(trans, root,
601 &root->fs_info->block_group_radix);
602 ret2 = write_dirty_block_radix(trans, root,
603 &root->fs_info->block_group_data_radix);
604 if (ret)
605 return ret;
606 if (ret2)
607 return ret2;
608 return 0;
609}
610
Chris Mason9078a3e2007-04-26 16:46:15 -0400611static int update_block_group(struct btrfs_trans_handle *trans,
612 struct btrfs_root *root,
Chris Mason1e2677e2007-05-29 16:52:18 -0400613 u64 blocknr, u64 num, int alloc, int mark_free,
614 int data)
Chris Mason9078a3e2007-04-26 16:46:15 -0400615{
616 struct btrfs_block_group_cache *cache;
617 struct btrfs_fs_info *info = root->fs_info;
618 u64 total = num;
619 u64 old_val;
620 u64 block_in_group;
Chris Masone37c9e62007-05-09 20:13:14 -0400621 u64 i;
Chris Mason1e2677e2007-05-29 16:52:18 -0400622 int ret;
Chris Mason3e1ad542007-05-07 20:03:49 -0400623
Chris Mason9078a3e2007-04-26 16:46:15 -0400624 while(total) {
Chris Mason5276aed2007-06-11 21:33:38 -0400625 cache = btrfs_lookup_block_group(info, blocknr);
Chris Mason3e1ad542007-05-07 20:03:49 -0400626 if (!cache) {
Chris Mason9078a3e2007-04-26 16:46:15 -0400627 return -1;
Chris Masoncd1bc462007-04-27 10:08:34 -0400628 }
Chris Mason9078a3e2007-04-26 16:46:15 -0400629 block_in_group = blocknr - cache->key.objectid;
630 WARN_ON(block_in_group > cache->key.offset);
Chris Mason3e1ad542007-05-07 20:03:49 -0400631 radix_tree_tag_set(cache->radix, cache->key.objectid +
Chris Masonbe744172007-05-06 10:15:01 -0400632 cache->key.offset - 1,
Chris Mason9078a3e2007-04-26 16:46:15 -0400633 BTRFS_BLOCK_GROUP_DIRTY);
634
635 old_val = btrfs_block_group_used(&cache->item);
636 num = min(total, cache->key.offset - block_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -0400637 if (alloc) {
Chris Masoncd1bc462007-04-27 10:08:34 -0400638 if (blocknr > cache->last_alloc)
639 cache->last_alloc = blocknr;
Chris Masone37c9e62007-05-09 20:13:14 -0400640 if (!cache->data) {
641 for (i = 0; i < num; i++) {
642 clear_radix_bit(&info->extent_map_radix,
643 blocknr + i);
644 }
645 }
Chris Mason1e2677e2007-05-29 16:52:18 -0400646 if (cache->data != data &&
Chris Mason84f54cf2007-06-12 07:43:08 -0400647 old_val < (cache->key.offset >> 1)) {
Chris Mason1e2677e2007-05-29 16:52:18 -0400648 cache->data = data;
649 radix_tree_delete(cache->radix,
650 cache->key.objectid +
651 cache->key.offset - 1);
652
653 if (data) {
654 cache->radix =
655 &info->block_group_data_radix;
656 cache->item.flags |=
657 BTRFS_BLOCK_GROUP_DATA;
658 } else {
659 cache->radix = &info->block_group_radix;
660 cache->item.flags &=
661 ~BTRFS_BLOCK_GROUP_DATA;
662 }
663 ret = radix_tree_insert(cache->radix,
664 cache->key.objectid +
665 cache->key.offset - 1,
666 (void *)cache);
667 }
668 old_val += num;
Chris Masoncd1bc462007-04-27 10:08:34 -0400669 } else {
Chris Mason9078a3e2007-04-26 16:46:15 -0400670 old_val -= num;
Chris Masoncd1bc462007-04-27 10:08:34 -0400671 if (blocknr < cache->first_free)
672 cache->first_free = blocknr;
Chris Masone37c9e62007-05-09 20:13:14 -0400673 if (!cache->data && mark_free) {
674 for (i = 0; i < num; i++) {
675 set_radix_bit(&info->extent_map_radix,
676 blocknr + i);
677 }
678 }
Chris Mason84f54cf2007-06-12 07:43:08 -0400679 if (old_val < (cache->key.offset >> 1) &&
680 old_val + num >= (cache->key.offset >> 1)) {
Chris Masone37c9e62007-05-09 20:13:14 -0400681 radix_tree_tag_set(cache->radix,
682 cache->key.objectid +
683 cache->key.offset - 1,
684 BTRFS_BLOCK_GROUP_AVAIL);
685 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400686 }
Chris Mason9078a3e2007-04-26 16:46:15 -0400687 btrfs_set_block_group_used(&cache->item, old_val);
Chris Masone37c9e62007-05-09 20:13:14 -0400688 total -= num;
689 blocknr += num;
Chris Mason9078a3e2007-04-26 16:46:15 -0400690 }
691 return 0;
692}
693
Chris Masonccd467d2007-06-28 15:57:36 -0400694int btrfs_copy_pinned(struct btrfs_root *root, struct radix_tree_root *copy)
695{
696 unsigned long gang[8];
697 u64 last = 0;
698 struct radix_tree_root *pinned_radix = &root->fs_info->pinned_radix;
699 int ret;
700 int i;
701
702 while(1) {
703 ret = find_first_radix_bit(pinned_radix, gang, last,
704 ARRAY_SIZE(gang));
705 if (!ret)
706 break;
707 for (i = 0 ; i < ret; i++) {
708 set_radix_bit(copy, gang[i]);
709 last = gang[i] + 1;
710 }
711 }
Chris Mason26b80032007-08-08 20:17:12 -0400712 ret = find_first_radix_bit(&root->fs_info->extent_ins_radix, gang, 0,
713 ARRAY_SIZE(gang));
714 WARN_ON(ret);
Chris Masonccd467d2007-06-28 15:57:36 -0400715 return 0;
716}
717
718int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
719 struct btrfs_root *root,
720 struct radix_tree_root *unpin_radix)
Chris Masona28ec192007-03-06 20:08:01 -0500721{
Chris Mason8ef97622007-03-26 10:15:30 -0400722 unsigned long gang[8];
Chris Masonbe744172007-05-06 10:15:01 -0400723 struct btrfs_block_group_cache *block_group;
Chris Mason88fd1462007-03-16 08:56:18 -0400724 u64 first = 0;
Chris Masona28ec192007-03-06 20:08:01 -0500725 int ret;
726 int i;
Chris Mason8ef97622007-03-26 10:15:30 -0400727 struct radix_tree_root *pinned_radix = &root->fs_info->pinned_radix;
Chris Masone37c9e62007-05-09 20:13:14 -0400728 struct radix_tree_root *extent_radix = &root->fs_info->extent_map_radix;
Chris Masona28ec192007-03-06 20:08:01 -0500729
730 while(1) {
Chris Masonccd467d2007-06-28 15:57:36 -0400731 ret = find_first_radix_bit(unpin_radix, gang, 0,
Chris Mason8ef97622007-03-26 10:15:30 -0400732 ARRAY_SIZE(gang));
Chris Masona28ec192007-03-06 20:08:01 -0500733 if (!ret)
734 break;
Chris Mason88fd1462007-03-16 08:56:18 -0400735 if (!first)
Chris Mason8ef97622007-03-26 10:15:30 -0400736 first = gang[0];
Chris Mason0579da42007-03-07 16:15:30 -0500737 for (i = 0; i < ret; i++) {
Chris Mason8ef97622007-03-26 10:15:30 -0400738 clear_radix_bit(pinned_radix, gang[i]);
Chris Masonccd467d2007-06-28 15:57:36 -0400739 clear_radix_bit(unpin_radix, gang[i]);
Chris Mason5276aed2007-06-11 21:33:38 -0400740 block_group = btrfs_lookup_block_group(root->fs_info,
741 gang[i]);
Chris Masonbe744172007-05-06 10:15:01 -0400742 if (block_group) {
743 WARN_ON(block_group->pinned == 0);
744 block_group->pinned--;
745 if (gang[i] < block_group->last_alloc)
746 block_group->last_alloc = gang[i];
Chris Masone37c9e62007-05-09 20:13:14 -0400747 if (!block_group->data)
748 set_radix_bit(extent_radix, gang[i]);
Chris Masonbe744172007-05-06 10:15:01 -0400749 }
Chris Mason0579da42007-03-07 16:15:30 -0500750 }
Chris Masona28ec192007-03-06 20:08:01 -0500751 }
752 return 0;
753}
754
Chris Masone089f052007-03-16 16:20:31 -0400755static int finish_current_insert(struct btrfs_trans_handle *trans, struct
756 btrfs_root *extent_root)
Chris Mason037e6392007-03-07 11:50:24 -0500757{
Chris Masone2fa7222007-03-12 16:22:34 -0400758 struct btrfs_key ins;
Chris Mason234b63a2007-03-13 10:46:10 -0400759 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -0500760 int i;
761 int ret;
Chris Mason26b80032007-08-08 20:17:12 -0400762 int err;
763 unsigned long gang[8];
Chris Mason1261ec42007-03-20 20:35:03 -0400764 struct btrfs_fs_info *info = extent_root->fs_info;
Chris Mason037e6392007-03-07 11:50:24 -0500765
Chris Masoncf27e1e2007-03-13 09:49:06 -0400766 btrfs_set_extent_refs(&extent_item, 1);
Chris Mason037e6392007-03-07 11:50:24 -0500767 ins.offset = 1;
768 ins.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -0400769 btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
Chris Mason5d0c3e62007-04-23 17:01:05 -0400770 btrfs_set_extent_owner(&extent_item, extent_root->root_key.objectid);
Chris Mason037e6392007-03-07 11:50:24 -0500771
Chris Mason26b80032007-08-08 20:17:12 -0400772 while(1) {
773 ret = find_first_radix_bit(&info->extent_ins_radix, gang, 0,
774 ARRAY_SIZE(gang));
775 if (!ret)
776 break;
777
778 for (i = 0; i < ret; i++) {
779 ins.objectid = gang[i];
780 err = btrfs_insert_item(trans, extent_root, &ins,
781 &extent_item,
782 sizeof(extent_item));
783 clear_radix_bit(&info->extent_ins_radix, gang[i]);
784 WARN_ON(err);
785 }
Chris Mason037e6392007-03-07 11:50:24 -0500786 }
Chris Mason037e6392007-03-07 11:50:24 -0500787 return 0;
788}
789
Chris Mason8ef97622007-03-26 10:15:30 -0400790static int pin_down_block(struct btrfs_root *root, u64 blocknr, int pending)
Chris Masone20d96d2007-03-22 12:13:20 -0400791{
792 int err;
Chris Mason78fae272007-03-25 11:35:08 -0400793 struct btrfs_header *header;
Chris Mason8ef97622007-03-26 10:15:30 -0400794 struct buffer_head *bh;
Chris Mason78fae272007-03-25 11:35:08 -0400795
Chris Masonf4b9aa82007-03-27 11:05:53 -0400796 if (!pending) {
Chris Masond98237b2007-03-28 13:57:48 -0400797 bh = btrfs_find_tree_block(root, blocknr);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400798 if (bh) {
799 if (buffer_uptodate(bh)) {
800 u64 transid =
801 root->fs_info->running_transaction->transid;
802 header = btrfs_buffer_header(bh);
803 if (btrfs_header_generation(header) ==
804 transid) {
805 btrfs_block_release(root, bh);
806 return 0;
807 }
Chris Masonf4b9aa82007-03-27 11:05:53 -0400808 }
Chris Masond6025572007-03-30 14:27:56 -0400809 btrfs_block_release(root, bh);
Chris Mason8ef97622007-03-26 10:15:30 -0400810 }
Chris Mason8ef97622007-03-26 10:15:30 -0400811 err = set_radix_bit(&root->fs_info->pinned_radix, blocknr);
Chris Masonbe744172007-05-06 10:15:01 -0400812 if (!err) {
813 struct btrfs_block_group_cache *cache;
Chris Mason5276aed2007-06-11 21:33:38 -0400814 cache = btrfs_lookup_block_group(root->fs_info,
815 blocknr);
Chris Masonbe744172007-05-06 10:15:01 -0400816 if (cache)
817 cache->pinned++;
818 }
Chris Masonf4b9aa82007-03-27 11:05:53 -0400819 } else {
820 err = set_radix_bit(&root->fs_info->pending_del_radix, blocknr);
821 }
Chris Masonbe744172007-05-06 10:15:01 -0400822 BUG_ON(err < 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400823 return 0;
824}
825
Chris Masona28ec192007-03-06 20:08:01 -0500826/*
827 * remove an extent from the root, returns 0 on success
828 */
Chris Masone089f052007-03-16 16:20:31 -0400829static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masone37c9e62007-05-09 20:13:14 -0400830 *root, u64 blocknr, u64 num_blocks, int pin,
831 int mark_free)
Chris Masona28ec192007-03-06 20:08:01 -0500832{
Chris Mason5caf2a02007-04-02 11:20:42 -0400833 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -0400834 struct btrfs_key key;
Chris Mason1261ec42007-03-20 20:35:03 -0400835 struct btrfs_fs_info *info = root->fs_info;
836 struct btrfs_root *extent_root = info->extent_root;
Chris Masona28ec192007-03-06 20:08:01 -0500837 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400838 struct btrfs_extent_item *ei;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400839 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500840
Chris Masona28ec192007-03-06 20:08:01 -0500841 key.objectid = blocknr;
842 key.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -0400843 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masona28ec192007-03-06 20:08:01 -0500844 key.offset = num_blocks;
845
Chris Mason5caf2a02007-04-02 11:20:42 -0400846 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -0400847 if (!path)
848 return -ENOMEM;
849
Chris Mason5caf2a02007-04-02 11:20:42 -0400850 ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400851 if (ret < 0)
852 return ret;
853 BUG_ON(ret);
Chris Mason5caf2a02007-04-02 11:20:42 -0400854 ei = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
Chris Mason123abc82007-03-14 14:14:43 -0400855 struct btrfs_extent_item);
Chris Masona28ec192007-03-06 20:08:01 -0500856 BUG_ON(ei->refs == 0);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400857 refs = btrfs_extent_refs(ei) - 1;
858 btrfs_set_extent_refs(ei, refs);
Chris Mason5caf2a02007-04-02 11:20:42 -0400859 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400860 if (refs == 0) {
Josef Bacik58176a92007-08-29 15:47:34 -0400861 u64 super_blocks_used, root_blocks_used;
Chris Mason78fae272007-03-25 11:35:08 -0400862
863 if (pin) {
Chris Mason8ef97622007-03-26 10:15:30 -0400864 ret = pin_down_block(root, blocknr, 0);
Chris Mason78fae272007-03-25 11:35:08 -0400865 BUG_ON(ret);
866 }
867
Josef Bacik58176a92007-08-29 15:47:34 -0400868 /* block accounting for super block */
Chris Mason4b52dff2007-06-26 10:06:50 -0400869 super_blocks_used = btrfs_super_blocks_used(&info->super_copy);
870 btrfs_set_super_blocks_used(&info->super_copy,
Chris Mason1261ec42007-03-20 20:35:03 -0400871 super_blocks_used - num_blocks);
Josef Bacik58176a92007-08-29 15:47:34 -0400872
873 /* block accounting for root item */
874 root_blocks_used = btrfs_root_blocks_used(&root->root_item);
875 btrfs_set_root_blocks_used(&root->root_item,
876 root_blocks_used - num_blocks);
877
Chris Mason5caf2a02007-04-02 11:20:42 -0400878 ret = btrfs_del_item(trans, extent_root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -0400879 if (ret) {
880 return ret;
881 }
Chris Masone37c9e62007-05-09 20:13:14 -0400882 ret = update_block_group(trans, root, blocknr, num_blocks, 0,
Chris Mason1e2677e2007-05-29 16:52:18 -0400883 mark_free, 0);
Chris Mason9078a3e2007-04-26 16:46:15 -0400884 BUG_ON(ret);
Chris Masona28ec192007-03-06 20:08:01 -0500885 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400886 btrfs_free_path(path);
Chris Masone089f052007-03-16 16:20:31 -0400887 finish_current_insert(trans, extent_root);
Chris Masona28ec192007-03-06 20:08:01 -0500888 return ret;
889}
890
891/*
Chris Masonfec577f2007-02-26 10:40:21 -0500892 * find all the blocks marked as pending in the radix tree and remove
893 * them from the extent map
894 */
Chris Masone089f052007-03-16 16:20:31 -0400895static int del_pending_extents(struct btrfs_trans_handle *trans, struct
896 btrfs_root *extent_root)
Chris Masonfec577f2007-02-26 10:40:21 -0500897{
898 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -0400899 int wret;
900 int err = 0;
Chris Mason8ef97622007-03-26 10:15:30 -0400901 unsigned long gang[4];
Chris Masonfec577f2007-02-26 10:40:21 -0500902 int i;
Chris Mason8ef97622007-03-26 10:15:30 -0400903 struct radix_tree_root *pending_radix;
904 struct radix_tree_root *pinned_radix;
Chris Masonbe744172007-05-06 10:15:01 -0400905 struct btrfs_block_group_cache *cache;
Chris Mason8ef97622007-03-26 10:15:30 -0400906
907 pending_radix = &extent_root->fs_info->pending_del_radix;
908 pinned_radix = &extent_root->fs_info->pinned_radix;
Chris Masonfec577f2007-02-26 10:40:21 -0500909
910 while(1) {
Chris Masone37c9e62007-05-09 20:13:14 -0400911 ret = find_first_radix_bit(pending_radix, gang, 0,
Chris Mason8ef97622007-03-26 10:15:30 -0400912 ARRAY_SIZE(gang));
Chris Masonfec577f2007-02-26 10:40:21 -0500913 if (!ret)
914 break;
915 for (i = 0; i < ret; i++) {
Chris Mason8ef97622007-03-26 10:15:30 -0400916 wret = set_radix_bit(pinned_radix, gang[i]);
Chris Masonbe744172007-05-06 10:15:01 -0400917 if (wret == 0) {
Chris Mason5276aed2007-06-11 21:33:38 -0400918 cache =
919 btrfs_lookup_block_group(extent_root->fs_info,
Chris Masonbe744172007-05-06 10:15:01 -0400920 gang[i]);
921 if (cache)
922 cache->pinned++;
923 }
924 if (wret < 0) {
925 printk(KERN_CRIT "set_radix_bit, err %d\n",
926 wret);
927 BUG_ON(wret < 0);
928 }
Chris Mason8ef97622007-03-26 10:15:30 -0400929 wret = clear_radix_bit(pending_radix, gang[i]);
930 BUG_ON(wret);
Chris Masond5719762007-03-23 10:01:08 -0400931 wret = __free_extent(trans, extent_root,
Chris Masone37c9e62007-05-09 20:13:14 -0400932 gang[i], 1, 0, 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400933 if (wret)
934 err = wret;
Chris Masonfec577f2007-02-26 10:40:21 -0500935 }
936 }
Chris Masone20d96d2007-03-22 12:13:20 -0400937 return err;
Chris Masonfec577f2007-02-26 10:40:21 -0500938}
939
940/*
941 * remove an extent from the root, returns 0 on success
942 */
Chris Masone089f052007-03-16 16:20:31 -0400943int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
944 *root, u64 blocknr, u64 num_blocks, int pin)
Chris Masonfec577f2007-02-26 10:40:21 -0500945{
Chris Mason9f5fae22007-03-20 14:38:32 -0400946 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Masonfec577f2007-02-26 10:40:21 -0500947 int pending_ret;
948 int ret;
Chris Masona28ec192007-03-06 20:08:01 -0500949
950 if (root == extent_root) {
Chris Mason8ef97622007-03-26 10:15:30 -0400951 pin_down_block(root, blocknr, 1);
Chris Masona28ec192007-03-06 20:08:01 -0500952 return 0;
953 }
Chris Masone37c9e62007-05-09 20:13:14 -0400954 ret = __free_extent(trans, root, blocknr, num_blocks, pin, pin == 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400955 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
Chris Masonfec577f2007-02-26 10:40:21 -0500956 return ret ? ret : pending_ret;
957}
958
959/*
960 * walks the btree of allocated extents and find a hole of a given size.
961 * The key ins is changed to record the hole:
962 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -0400963 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -0500964 * ins->offset == number of blocks
965 * Any available blocks before search_start are skipped.
966 */
Chris Masone089f052007-03-16 16:20:31 -0400967static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason6702ed42007-08-07 16:15:09 -0400968 *orig_root, u64 num_blocks, u64 empty_size,
969 u64 search_start, u64 search_end, u64 hint_block,
Chris Masonf2654de2007-06-26 12:20:46 -0400970 struct btrfs_key *ins, u64 exclude_start,
971 u64 exclude_nr, int data)
Chris Masonfec577f2007-02-26 10:40:21 -0500972{
Chris Mason5caf2a02007-04-02 11:20:42 -0400973 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -0400974 struct btrfs_key key;
Chris Masonfec577f2007-02-26 10:40:21 -0500975 int ret;
976 u64 hole_size = 0;
977 int slot = 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400978 u64 last_block = 0;
Chris Mason037e6392007-03-07 11:50:24 -0500979 u64 test_block;
Chris Masonbe744172007-05-06 10:15:01 -0400980 u64 orig_search_start = search_start;
Chris Masonfec577f2007-02-26 10:40:21 -0500981 int start_found;
Chris Mason234b63a2007-03-13 10:46:10 -0400982 struct btrfs_leaf *l;
Chris Mason9f5fae22007-03-20 14:38:32 -0400983 struct btrfs_root * root = orig_root->fs_info->extent_root;
Chris Masonf2458e12007-04-25 15:52:25 -0400984 struct btrfs_fs_info *info = root->fs_info;
Chris Mason0579da42007-03-07 16:15:30 -0500985 int total_needed = num_blocks;
Chris Masone20d96d2007-03-22 12:13:20 -0400986 int level;
Chris Masonbe08c1b2007-05-03 09:06:49 -0400987 struct btrfs_block_group_cache *block_group;
Chris Masonbe744172007-05-06 10:15:01 -0400988 int full_scan = 0;
Chris Masonfbdc7622007-05-30 10:22:12 -0400989 int wrapped = 0;
Chris Masonfec577f2007-02-26 10:40:21 -0500990
Chris Mason26b80032007-08-08 20:17:12 -0400991 WARN_ON(num_blocks < 1);
Chris Masonb1a4d962007-04-04 15:27:52 -0400992 ins->flags = 0;
993 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
994
Chris Masone20d96d2007-03-22 12:13:20 -0400995 level = btrfs_header_level(btrfs_buffer_header(root->node));
Chris Mason3e1ad542007-05-07 20:03:49 -0400996 if (search_end == (u64)-1)
Chris Mason4b52dff2007-06-26 10:06:50 -0400997 search_end = btrfs_super_total_blocks(&info->super_copy);
Chris Masonfbdc7622007-05-30 10:22:12 -0400998 if (hint_block) {
Chris Mason5276aed2007-06-11 21:33:38 -0400999 block_group = btrfs_lookup_block_group(info, hint_block);
Chris Masonbe744172007-05-06 10:15:01 -04001000 block_group = btrfs_find_block_group(root, block_group,
Chris Masonfbdc7622007-05-30 10:22:12 -04001001 hint_block, data, 1);
Chris Masonbe744172007-05-06 10:15:01 -04001002 } else {
1003 block_group = btrfs_find_block_group(root,
1004 trans->block_group, 0,
Chris Masonde428b62007-05-18 13:28:27 -04001005 data, 1);
Chris Masonbe744172007-05-06 10:15:01 -04001006 }
1007
Chris Mason6702ed42007-08-07 16:15:09 -04001008 total_needed += empty_size;
Chris Masone0115992007-06-19 16:23:05 -04001009 path = btrfs_alloc_path();
1010
Chris Masonbe744172007-05-06 10:15:01 -04001011check_failed:
Chris Mason1e2677e2007-05-29 16:52:18 -04001012 if (!block_group->data)
Chris Masone37c9e62007-05-09 20:13:14 -04001013 search_start = find_search_start(root, &block_group,
1014 search_start, total_needed);
Chris Masonfbdc7622007-05-30 10:22:12 -04001015 else if (!full_scan)
Chris Masone37c9e62007-05-09 20:13:14 -04001016 search_start = max(block_group->last_alloc, search_start);
1017
Chris Mason5caf2a02007-04-02 11:20:42 -04001018 btrfs_init_path(path);
Chris Masonfec577f2007-02-26 10:40:21 -05001019 ins->objectid = search_start;
1020 ins->offset = 0;
Chris Masonfec577f2007-02-26 10:40:21 -05001021 start_found = 0;
Chris Mason2cc58cf2007-08-27 16:49:44 -04001022 path->reada = 2;
Chris Masone37c9e62007-05-09 20:13:14 -04001023
Chris Mason5caf2a02007-04-02 11:20:42 -04001024 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
Chris Mason0f70abe2007-02-28 16:46:22 -05001025 if (ret < 0)
1026 goto error;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001027
Chris Masone37c9e62007-05-09 20:13:14 -04001028 if (path->slots[0] > 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001029 path->slots[0]--;
Chris Masone37c9e62007-05-09 20:13:14 -04001030 }
1031
1032 l = btrfs_buffer_leaf(path->nodes[0]);
1033 btrfs_disk_key_to_cpu(&key, &l->items[path->slots[0]].key);
1034 /*
1035 * a rare case, go back one key if we hit a block group item
1036 * instead of an extent item
1037 */
1038 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY &&
1039 key.objectid + key.offset >= search_start) {
1040 ins->objectid = key.objectid;
1041 ins->offset = key.offset - 1;
1042 btrfs_release_path(root, path);
1043 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
1044 if (ret < 0)
1045 goto error;
1046
1047 if (path->slots[0] > 0) {
1048 path->slots[0]--;
1049 }
1050 }
Chris Mason0579da42007-03-07 16:15:30 -05001051
Chris Masonfec577f2007-02-26 10:40:21 -05001052 while (1) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001053 l = btrfs_buffer_leaf(path->nodes[0]);
1054 slot = path->slots[0];
Chris Mason7518a232007-03-12 12:01:18 -04001055 if (slot >= btrfs_header_nritems(&l->header)) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001056 ret = btrfs_next_leaf(root, path);
Chris Masonfec577f2007-02-26 10:40:21 -05001057 if (ret == 0)
1058 continue;
Chris Mason0f70abe2007-02-28 16:46:22 -05001059 if (ret < 0)
1060 goto error;
Chris Masonfec577f2007-02-26 10:40:21 -05001061 if (!start_found) {
1062 ins->objectid = search_start;
Chris Mason3e1ad542007-05-07 20:03:49 -04001063 ins->offset = search_end - search_start;
Chris Masonfec577f2007-02-26 10:40:21 -05001064 start_found = 1;
1065 goto check_pending;
1066 }
1067 ins->objectid = last_block > search_start ?
1068 last_block : search_start;
Chris Mason3e1ad542007-05-07 20:03:49 -04001069 ins->offset = search_end - ins->objectid;
Chris Masonfec577f2007-02-26 10:40:21 -05001070 goto check_pending;
1071 }
Chris Masone37c9e62007-05-09 20:13:14 -04001072
Chris Masone2fa7222007-03-12 16:22:34 -04001073 btrfs_disk_key_to_cpu(&key, &l->items[slot].key);
Chris Masone37c9e62007-05-09 20:13:14 -04001074 if (key.objectid >= search_start && key.objectid > last_block &&
1075 start_found) {
1076 if (last_block < search_start)
1077 last_block = search_start;
1078 hole_size = key.objectid - last_block;
1079 if (hole_size >= num_blocks) {
1080 ins->objectid = last_block;
1081 ins->offset = hole_size;
1082 goto check_pending;
Chris Mason0579da42007-03-07 16:15:30 -05001083 }
Chris Masonfec577f2007-02-26 10:40:21 -05001084 }
Chris Masone37c9e62007-05-09 20:13:14 -04001085
1086 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY)
1087 goto next;
1088
Chris Mason0579da42007-03-07 16:15:30 -05001089 start_found = 1;
Chris Masone2fa7222007-03-12 16:22:34 -04001090 last_block = key.objectid + key.offset;
Chris Masonfbdc7622007-05-30 10:22:12 -04001091 if (!full_scan && last_block >= block_group->key.objectid +
Chris Masonbe744172007-05-06 10:15:01 -04001092 block_group->key.offset) {
1093 btrfs_release_path(root, path);
1094 search_start = block_group->key.objectid +
1095 block_group->key.offset * 2;
1096 goto new_group;
1097 }
Chris Mason9078a3e2007-04-26 16:46:15 -04001098next:
Chris Mason5caf2a02007-04-02 11:20:42 -04001099 path->slots[0]++;
Chris Masonde428b62007-05-18 13:28:27 -04001100 cond_resched();
Chris Masonfec577f2007-02-26 10:40:21 -05001101 }
Chris Masonfec577f2007-02-26 10:40:21 -05001102check_pending:
1103 /* we have to make sure we didn't find an extent that has already
1104 * been allocated by the map tree or the original allocation
1105 */
Chris Mason5caf2a02007-04-02 11:20:42 -04001106 btrfs_release_path(root, path);
Chris Masonfec577f2007-02-26 10:40:21 -05001107 BUG_ON(ins->objectid < search_start);
Chris Masone37c9e62007-05-09 20:13:14 -04001108
Chris Mason3e1ad542007-05-07 20:03:49 -04001109 if (ins->objectid + num_blocks >= search_end) {
Chris Masonfbdc7622007-05-30 10:22:12 -04001110 if (full_scan) {
1111 ret = -ENOSPC;
1112 goto error;
1113 }
Chris Masonbe744172007-05-06 10:15:01 -04001114 search_start = orig_search_start;
Chris Mason6702ed42007-08-07 16:15:09 -04001115 if (wrapped) {
1116 if (!full_scan)
1117 total_needed -= empty_size;
Chris Masonfbdc7622007-05-30 10:22:12 -04001118 full_scan = 1;
Chris Mason6702ed42007-08-07 16:15:09 -04001119 } else
Chris Masonfbdc7622007-05-30 10:22:12 -04001120 wrapped = 1;
Chris Masonbe744172007-05-06 10:15:01 -04001121 goto new_group;
Chris Mason06a2f9f2007-04-28 08:48:10 -04001122 }
Chris Mason037e6392007-03-07 11:50:24 -05001123 for (test_block = ins->objectid;
Chris Masonf2458e12007-04-25 15:52:25 -04001124 test_block < ins->objectid + num_blocks; test_block++) {
Chris Mason26b80032007-08-08 20:17:12 -04001125 if (test_radix_bit(&info->pinned_radix, test_block) ||
1126 test_radix_bit(&info->extent_ins_radix, test_block)) {
Chris Mason037e6392007-03-07 11:50:24 -05001127 search_start = test_block + 1;
Chris Masonbe744172007-05-06 10:15:01 -04001128 goto new_group;
Chris Masonfec577f2007-02-26 10:40:21 -05001129 }
1130 }
Chris Masonf2654de2007-06-26 12:20:46 -04001131 if (exclude_nr > 0 && (ins->objectid + num_blocks > exclude_start &&
1132 ins->objectid < exclude_start + exclude_nr)) {
1133 search_start = exclude_start + exclude_nr;
1134 goto new_group;
1135 }
Chris Masone37c9e62007-05-09 20:13:14 -04001136 if (!data) {
Chris Mason5276aed2007-06-11 21:33:38 -04001137 block_group = btrfs_lookup_block_group(info, ins->objectid);
Chris Mason26b80032007-08-08 20:17:12 -04001138 if (block_group)
1139 trans->block_group = block_group;
Chris Masoncd1bc462007-04-27 10:08:34 -04001140 }
Chris Mason037e6392007-03-07 11:50:24 -05001141 ins->offset = num_blocks;
Chris Mason5caf2a02007-04-02 11:20:42 -04001142 btrfs_free_path(path);
Chris Masonfec577f2007-02-26 10:40:21 -05001143 return 0;
Chris Masonbe744172007-05-06 10:15:01 -04001144
1145new_group:
Chris Mason3e1ad542007-05-07 20:03:49 -04001146 if (search_start + num_blocks >= search_end) {
Chris Masonbe744172007-05-06 10:15:01 -04001147 search_start = orig_search_start;
Chris Masonfbdc7622007-05-30 10:22:12 -04001148 if (full_scan) {
1149 ret = -ENOSPC;
1150 goto error;
1151 }
Chris Mason6702ed42007-08-07 16:15:09 -04001152 if (wrapped) {
1153 if (!full_scan)
1154 total_needed -= empty_size;
Chris Masonfbdc7622007-05-30 10:22:12 -04001155 full_scan = 1;
Chris Mason6702ed42007-08-07 16:15:09 -04001156 } else
Chris Masonfbdc7622007-05-30 10:22:12 -04001157 wrapped = 1;
Chris Masonbe744172007-05-06 10:15:01 -04001158 }
Chris Mason5276aed2007-06-11 21:33:38 -04001159 block_group = btrfs_lookup_block_group(info, search_start);
Chris Masonfbdc7622007-05-30 10:22:12 -04001160 cond_resched();
Chris Masonbe744172007-05-06 10:15:01 -04001161 if (!full_scan)
1162 block_group = btrfs_find_block_group(root, block_group,
Chris Masonde428b62007-05-18 13:28:27 -04001163 search_start, data, 0);
Chris Masonbe744172007-05-06 10:15:01 -04001164 goto check_failed;
1165
Chris Mason0f70abe2007-02-28 16:46:22 -05001166error:
Chris Mason5caf2a02007-04-02 11:20:42 -04001167 btrfs_release_path(root, path);
1168 btrfs_free_path(path);
Chris Mason0f70abe2007-02-28 16:46:22 -05001169 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05001170}
Chris Masonfec577f2007-02-26 10:40:21 -05001171/*
Chris Masonfec577f2007-02-26 10:40:21 -05001172 * finds a free extent and does all the dirty work required for allocation
1173 * returns the key for the extent through ins, and a tree buffer for
1174 * the first block of the extent through buf.
1175 *
1176 * returns 0 if everything worked, non-zero otherwise.
1177 */
Chris Mason4d775672007-04-20 20:23:12 -04001178int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
1179 struct btrfs_root *root, u64 owner,
Chris Mason6702ed42007-08-07 16:15:09 -04001180 u64 num_blocks, u64 empty_size, u64 hint_block,
Chris Masonbe08c1b2007-05-03 09:06:49 -04001181 u64 search_end, struct btrfs_key *ins, int data)
Chris Masonfec577f2007-02-26 10:40:21 -05001182{
1183 int ret;
1184 int pending_ret;
Josef Bacik58176a92007-08-29 15:47:34 -04001185 u64 super_blocks_used, root_blocks_used;
Chris Masonfbdc7622007-05-30 10:22:12 -04001186 u64 search_start = 0;
Chris Mason1261ec42007-03-20 20:35:03 -04001187 struct btrfs_fs_info *info = root->fs_info;
1188 struct btrfs_root *extent_root = info->extent_root;
Chris Mason234b63a2007-03-13 10:46:10 -04001189 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -05001190
Chris Masoncf27e1e2007-03-13 09:49:06 -04001191 btrfs_set_extent_refs(&extent_item, 1);
Chris Mason4d775672007-04-20 20:23:12 -04001192 btrfs_set_extent_owner(&extent_item, owner);
Chris Masonfec577f2007-02-26 10:40:21 -05001193
Chris Mason26b80032007-08-08 20:17:12 -04001194 WARN_ON(num_blocks < 1);
Chris Mason6702ed42007-08-07 16:15:09 -04001195 ret = find_free_extent(trans, root, num_blocks, empty_size,
1196 search_start, search_end, hint_block, ins,
Chris Mason26b80032007-08-08 20:17:12 -04001197 trans->alloc_exclude_start,
1198 trans->alloc_exclude_nr, data);
Chris Masonccd467d2007-06-28 15:57:36 -04001199 BUG_ON(ret);
Chris Masonf2654de2007-06-26 12:20:46 -04001200 if (ret)
1201 return ret;
1202
Josef Bacik58176a92007-08-29 15:47:34 -04001203 /* block accounting for super block */
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
Josef Bacik58176a92007-08-29 15:47:34 -04001208 /* block accounting for root item */
1209 root_blocks_used = btrfs_root_blocks_used(&root->root_item);
1210 btrfs_set_root_blocks_used(&root->root_item, root_blocks_used +
1211 num_blocks);
1212
Chris Mason26b80032007-08-08 20:17:12 -04001213 if (root == extent_root) {
1214 BUG_ON(num_blocks != 1);
1215 set_radix_bit(&root->fs_info->extent_ins_radix, ins->objectid);
1216 goto update_block;
1217 }
1218
1219 WARN_ON(trans->alloc_exclude_nr);
1220 trans->alloc_exclude_start = ins->objectid;
1221 trans->alloc_exclude_nr = ins->offset;
Chris Masone089f052007-03-16 16:20:31 -04001222 ret = btrfs_insert_item(trans, extent_root, ins, &extent_item,
1223 sizeof(extent_item));
Chris Mason037e6392007-03-07 11:50:24 -05001224
Chris Mason26b80032007-08-08 20:17:12 -04001225 trans->alloc_exclude_start = 0;
1226 trans->alloc_exclude_nr = 0;
1227
Chris Masonccd467d2007-06-28 15:57:36 -04001228 BUG_ON(ret);
Chris Masone089f052007-03-16 16:20:31 -04001229 finish_current_insert(trans, extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -04001230 pending_ret = del_pending_extents(trans, extent_root);
Chris Masone37c9e62007-05-09 20:13:14 -04001231 if (ret) {
Chris Mason037e6392007-03-07 11:50:24 -05001232 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -04001233 }
1234 if (pending_ret) {
Chris Mason037e6392007-03-07 11:50:24 -05001235 return pending_ret;
Chris Masone37c9e62007-05-09 20:13:14 -04001236 }
Chris Mason26b80032007-08-08 20:17:12 -04001237
1238update_block:
Chris Mason1e2677e2007-05-29 16:52:18 -04001239 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0,
1240 data);
Chris Masonfabb5682007-06-07 22:13:21 -04001241 BUG_ON(ret);
Chris Mason037e6392007-03-07 11:50:24 -05001242 return 0;
Chris Masonfec577f2007-02-26 10:40:21 -05001243}
1244
1245/*
1246 * helper function to allocate a block for a given tree
1247 * returns the tree buffer or NULL.
1248 */
Chris Masone20d96d2007-03-22 12:13:20 -04001249struct buffer_head *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Chris Mason6702ed42007-08-07 16:15:09 -04001250 struct btrfs_root *root, u64 hint,
1251 u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05001252{
Chris Masone2fa7222007-03-12 16:22:34 -04001253 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -05001254 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -04001255 struct buffer_head *buf;
Chris Masonfec577f2007-02-26 10:40:21 -05001256
Chris Mason4d775672007-04-20 20:23:12 -04001257 ret = btrfs_alloc_extent(trans, root, root->root_key.objectid,
Chris Mason6702ed42007-08-07 16:15:09 -04001258 1, empty_size, hint,
1259 (unsigned long)-1, &ins, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05001260 if (ret) {
Chris Mason54aa1f42007-06-22 14:16:25 -04001261 BUG_ON(ret > 0);
1262 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05001263 }
Chris Masond98237b2007-03-28 13:57:48 -04001264 buf = btrfs_find_create_tree_block(root, ins.objectid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001265 if (!buf) {
1266 btrfs_free_extent(trans, root, ins.objectid, 1, 0);
1267 return ERR_PTR(-ENOMEM);
1268 }
Chris Mason6702ed42007-08-07 16:15:09 -04001269 WARN_ON(buffer_dirty(buf));
Chris Masondf2ce342007-03-23 11:00:45 -04001270 set_buffer_uptodate(buf);
Chris Mason090d1872007-05-01 08:53:32 -04001271 set_buffer_checked(buf);
Chris Masonf2183bd2007-08-10 14:42:37 -04001272 set_buffer_defrag(buf);
Chris Mason7c4452b2007-04-28 09:29:35 -04001273 set_radix_bit(&trans->transaction->dirty_pages, buf->b_page->index);
Chris Masonfec577f2007-02-26 10:40:21 -05001274 return buf;
1275}
Chris Masona28ec192007-03-06 20:08:01 -05001276
Chris Mason6407bf62007-03-27 06:33:00 -04001277static int drop_leaf_ref(struct btrfs_trans_handle *trans,
1278 struct btrfs_root *root, struct buffer_head *cur)
1279{
1280 struct btrfs_disk_key *key;
1281 struct btrfs_leaf *leaf;
1282 struct btrfs_file_extent_item *fi;
1283 int i;
1284 int nritems;
1285 int ret;
1286
1287 BUG_ON(!btrfs_is_leaf(btrfs_buffer_node(cur)));
1288 leaf = btrfs_buffer_leaf(cur);
1289 nritems = btrfs_header_nritems(&leaf->header);
1290 for (i = 0; i < nritems; i++) {
Chris Mason3a686372007-05-24 13:35:57 -04001291 u64 disk_blocknr;
Chris Mason6407bf62007-03-27 06:33:00 -04001292 key = &leaf->items[i].key;
1293 if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY)
1294 continue;
1295 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
Chris Mason236454d2007-04-19 13:37:44 -04001296 if (btrfs_file_extent_type(fi) == BTRFS_FILE_EXTENT_INLINE)
1297 continue;
Chris Mason6407bf62007-03-27 06:33:00 -04001298 /*
1299 * FIXME make sure to insert a trans record that
1300 * repeats the snapshot del on crash
1301 */
Chris Mason3a686372007-05-24 13:35:57 -04001302 disk_blocknr = btrfs_file_extent_disk_blocknr(fi);
1303 if (disk_blocknr == 0)
1304 continue;
1305 ret = btrfs_free_extent(trans, root, disk_blocknr,
Chris Mason6407bf62007-03-27 06:33:00 -04001306 btrfs_file_extent_disk_num_blocks(fi),
1307 0);
1308 BUG_ON(ret);
1309 }
1310 return 0;
1311}
1312
Chris Masone0115992007-06-19 16:23:05 -04001313static void reada_walk_down(struct btrfs_root *root,
1314 struct btrfs_node *node)
1315{
1316 int i;
1317 u32 nritems;
1318 u64 blocknr;
1319 int ret;
1320 u32 refs;
1321
1322 nritems = btrfs_header_nritems(&node->header);
1323 for (i = 0; i < nritems; i++) {
1324 blocknr = btrfs_node_blockptr(node, i);
1325 ret = lookup_extent_ref(NULL, root, blocknr, 1, &refs);
1326 BUG_ON(ret);
1327 if (refs != 1)
1328 continue;
Chris Mason409eb952007-08-08 20:17:12 -04001329 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masone0115992007-06-19 16:23:05 -04001330 ret = readahead_tree_block(root, blocknr);
Chris Mason409eb952007-08-08 20:17:12 -04001331 cond_resched();
1332 mutex_lock(&root->fs_info->fs_mutex);
Chris Masone0115992007-06-19 16:23:05 -04001333 if (ret)
1334 break;
1335 }
1336}
1337
Chris Mason9aca1d52007-03-13 11:09:37 -04001338/*
1339 * helper function for drop_snapshot, this walks down the tree dropping ref
1340 * counts as it goes.
1341 */
Chris Masone089f052007-03-16 16:20:31 -04001342static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
1343 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05001344{
Chris Masone20d96d2007-03-22 12:13:20 -04001345 struct buffer_head *next;
1346 struct buffer_head *cur;
Chris Mason20524f02007-03-10 06:35:47 -05001347 u64 blocknr;
1348 int ret;
1349 u32 refs;
1350
Chris Mason5caf2a02007-04-02 11:20:42 -04001351 WARN_ON(*level < 0);
1352 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Masonb18c6682007-04-17 13:26:50 -04001353 ret = lookup_extent_ref(trans, root, bh_blocknr(path->nodes[*level]),
Chris Mason6407bf62007-03-27 06:33:00 -04001354 1, &refs);
Chris Mason20524f02007-03-10 06:35:47 -05001355 BUG_ON(ret);
1356 if (refs > 1)
1357 goto out;
Chris Masone0115992007-06-19 16:23:05 -04001358
Chris Mason9aca1d52007-03-13 11:09:37 -04001359 /*
1360 * walk down to the last node level and free all the leaves
1361 */
Chris Mason6407bf62007-03-27 06:33:00 -04001362 while(*level >= 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001363 WARN_ON(*level < 0);
1364 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason20524f02007-03-10 06:35:47 -05001365 cur = path->nodes[*level];
Chris Masone0115992007-06-19 16:23:05 -04001366
1367 if (*level > 0 && path->slots[*level] == 0)
1368 reada_walk_down(root, btrfs_buffer_node(cur));
1369
Chris Mason2c90e5d2007-04-02 10:50:19 -04001370 if (btrfs_header_level(btrfs_buffer_header(cur)) != *level)
1371 WARN_ON(1);
Chris Masone0115992007-06-19 16:23:05 -04001372
Chris Mason7518a232007-03-12 12:01:18 -04001373 if (path->slots[*level] >=
Chris Masone20d96d2007-03-22 12:13:20 -04001374 btrfs_header_nritems(btrfs_buffer_header(cur)))
Chris Mason20524f02007-03-10 06:35:47 -05001375 break;
Chris Mason6407bf62007-03-27 06:33:00 -04001376 if (*level == 0) {
1377 ret = drop_leaf_ref(trans, root, cur);
1378 BUG_ON(ret);
1379 break;
1380 }
Chris Masone20d96d2007-03-22 12:13:20 -04001381 blocknr = btrfs_node_blockptr(btrfs_buffer_node(cur),
1382 path->slots[*level]);
Chris Masonb18c6682007-04-17 13:26:50 -04001383 ret = lookup_extent_ref(trans, root, blocknr, 1, &refs);
Chris Mason6407bf62007-03-27 06:33:00 -04001384 BUG_ON(ret);
1385 if (refs != 1) {
Chris Mason20524f02007-03-10 06:35:47 -05001386 path->slots[*level]++;
Chris Masone089f052007-03-16 16:20:31 -04001387 ret = btrfs_free_extent(trans, root, blocknr, 1, 1);
Chris Mason20524f02007-03-10 06:35:47 -05001388 BUG_ON(ret);
1389 continue;
1390 }
Chris Masone9d0b132007-08-10 14:06:19 -04001391 next = btrfs_find_tree_block(root, blocknr);
1392 if (!next || !buffer_uptodate(next)) {
1393 brelse(next);
1394 mutex_unlock(&root->fs_info->fs_mutex);
1395 next = read_tree_block(root, blocknr);
1396 mutex_lock(&root->fs_info->fs_mutex);
1397
1398 /* we dropped the lock, check one more time */
1399 ret = lookup_extent_ref(trans, root, blocknr, 1, &refs);
1400 BUG_ON(ret);
1401 if (refs != 1) {
1402 path->slots[*level]++;
1403 brelse(next);
1404 ret = btrfs_free_extent(trans, root,
1405 blocknr, 1, 1);
1406 BUG_ON(ret);
1407 continue;
1408 }
1409 }
Chris Mason5caf2a02007-04-02 11:20:42 -04001410 WARN_ON(*level <= 0);
Chris Mason83e15a22007-03-12 09:03:27 -04001411 if (path->nodes[*level-1])
Chris Mason234b63a2007-03-13 10:46:10 -04001412 btrfs_block_release(root, path->nodes[*level-1]);
Chris Mason20524f02007-03-10 06:35:47 -05001413 path->nodes[*level-1] = next;
Chris Masone20d96d2007-03-22 12:13:20 -04001414 *level = btrfs_header_level(btrfs_buffer_header(next));
Chris Mason20524f02007-03-10 06:35:47 -05001415 path->slots[*level] = 0;
1416 }
1417out:
Chris Mason5caf2a02007-04-02 11:20:42 -04001418 WARN_ON(*level < 0);
1419 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason6407bf62007-03-27 06:33:00 -04001420 ret = btrfs_free_extent(trans, root,
Chris Mason7eccb902007-04-11 15:53:25 -04001421 bh_blocknr(path->nodes[*level]), 1, 1);
Chris Mason234b63a2007-03-13 10:46:10 -04001422 btrfs_block_release(root, path->nodes[*level]);
Chris Mason20524f02007-03-10 06:35:47 -05001423 path->nodes[*level] = NULL;
1424 *level += 1;
1425 BUG_ON(ret);
1426 return 0;
1427}
1428
Chris Mason9aca1d52007-03-13 11:09:37 -04001429/*
1430 * helper for dropping snapshots. This walks back up the tree in the path
1431 * to find the first node higher up where we haven't yet gone through
1432 * all the slots
1433 */
Chris Masone089f052007-03-16 16:20:31 -04001434static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root
1435 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05001436{
1437 int i;
1438 int slot;
1439 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04001440 struct btrfs_root_item *root_item = &root->root_item;
1441
Chris Mason234b63a2007-03-13 10:46:10 -04001442 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Mason20524f02007-03-10 06:35:47 -05001443 slot = path->slots[i];
Chris Masone20d96d2007-03-22 12:13:20 -04001444 if (slot < btrfs_header_nritems(
1445 btrfs_buffer_header(path->nodes[i])) - 1) {
Chris Mason9f3a7422007-08-07 15:52:19 -04001446 struct btrfs_node *node;
1447 node = btrfs_buffer_node(path->nodes[i]);
Chris Mason20524f02007-03-10 06:35:47 -05001448 path->slots[i]++;
1449 *level = i;
Chris Mason9f3a7422007-08-07 15:52:19 -04001450 WARN_ON(*level == 0);
1451 memcpy(&root_item->drop_progress,
1452 &node->ptrs[path->slots[i]].key,
1453 sizeof(root_item->drop_progress));
1454 root_item->drop_level = i;
Chris Mason20524f02007-03-10 06:35:47 -05001455 return 0;
1456 } else {
Chris Masone089f052007-03-16 16:20:31 -04001457 ret = btrfs_free_extent(trans, root,
Chris Mason7eccb902007-04-11 15:53:25 -04001458 bh_blocknr(path->nodes[*level]),
Chris Masone089f052007-03-16 16:20:31 -04001459 1, 1);
Chris Mason6407bf62007-03-27 06:33:00 -04001460 BUG_ON(ret);
Chris Mason234b63a2007-03-13 10:46:10 -04001461 btrfs_block_release(root, path->nodes[*level]);
Chris Mason83e15a22007-03-12 09:03:27 -04001462 path->nodes[*level] = NULL;
Chris Mason20524f02007-03-10 06:35:47 -05001463 *level = i + 1;
Chris Mason20524f02007-03-10 06:35:47 -05001464 }
1465 }
1466 return 1;
1467}
1468
Chris Mason9aca1d52007-03-13 11:09:37 -04001469/*
1470 * drop the reference count on the tree rooted at 'snap'. This traverses
1471 * the tree freeing any blocks that have a ref count of zero after being
1472 * decremented.
1473 */
Chris Masone089f052007-03-16 16:20:31 -04001474int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason9f3a7422007-08-07 15:52:19 -04001475 *root)
Chris Mason20524f02007-03-10 06:35:47 -05001476{
Chris Mason3768f362007-03-13 16:47:54 -04001477 int ret = 0;
Chris Mason9aca1d52007-03-13 11:09:37 -04001478 int wret;
Chris Mason20524f02007-03-10 06:35:47 -05001479 int level;
Chris Mason5caf2a02007-04-02 11:20:42 -04001480 struct btrfs_path *path;
Chris Mason20524f02007-03-10 06:35:47 -05001481 int i;
1482 int orig_level;
Chris Mason9f3a7422007-08-07 15:52:19 -04001483 struct btrfs_root_item *root_item = &root->root_item;
Chris Mason20524f02007-03-10 06:35:47 -05001484
Chris Mason5caf2a02007-04-02 11:20:42 -04001485 path = btrfs_alloc_path();
1486 BUG_ON(!path);
Chris Mason20524f02007-03-10 06:35:47 -05001487
Chris Mason9f3a7422007-08-07 15:52:19 -04001488 level = btrfs_header_level(btrfs_buffer_header(root->node));
Chris Mason20524f02007-03-10 06:35:47 -05001489 orig_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04001490 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
1491 path->nodes[level] = root->node;
1492 path->slots[level] = 0;
1493 } else {
1494 struct btrfs_key key;
1495 struct btrfs_disk_key *found_key;
1496 struct btrfs_node *node;
Chris Mason6702ed42007-08-07 16:15:09 -04001497
Chris Mason9f3a7422007-08-07 15:52:19 -04001498 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Chris Mason6702ed42007-08-07 16:15:09 -04001499 level = root_item->drop_level;
1500 path->lowest_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04001501 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason6702ed42007-08-07 16:15:09 -04001502 if (wret < 0) {
Chris Mason9f3a7422007-08-07 15:52:19 -04001503 ret = wret;
1504 goto out;
1505 }
Chris Mason9f3a7422007-08-07 15:52:19 -04001506 node = btrfs_buffer_node(path->nodes[level]);
1507 found_key = &node->ptrs[path->slots[level]].key;
1508 WARN_ON(memcmp(found_key, &root_item->drop_progress,
1509 sizeof(*found_key)));
1510 }
Chris Mason20524f02007-03-10 06:35:47 -05001511 while(1) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001512 wret = walk_down_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;
1517
Chris Mason5caf2a02007-04-02 11:20:42 -04001518 wret = walk_up_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04001519 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05001520 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04001521 if (wret < 0)
1522 ret = wret;
Chris Mason409eb952007-08-08 20:17:12 -04001523 ret = -EAGAIN;
1524 get_bh(root->node);
1525 break;
Chris Mason20524f02007-03-10 06:35:47 -05001526 }
Chris Mason83e15a22007-03-12 09:03:27 -04001527 for (i = 0; i <= orig_level; i++) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001528 if (path->nodes[i]) {
1529 btrfs_block_release(root, path->nodes[i]);
Chris Mason6702ed42007-08-07 16:15:09 -04001530 path->nodes[i] = 0;
Chris Mason83e15a22007-03-12 09:03:27 -04001531 }
Chris Mason20524f02007-03-10 06:35:47 -05001532 }
Chris Mason9f3a7422007-08-07 15:52:19 -04001533out:
Chris Mason5caf2a02007-04-02 11:20:42 -04001534 btrfs_free_path(path);
Chris Mason9aca1d52007-03-13 11:09:37 -04001535 return ret;
Chris Mason20524f02007-03-10 06:35:47 -05001536}
Chris Mason9078a3e2007-04-26 16:46:15 -04001537
Chris Masonbe744172007-05-06 10:15:01 -04001538static int free_block_group_radix(struct radix_tree_root *radix)
Chris Mason9078a3e2007-04-26 16:46:15 -04001539{
1540 int ret;
1541 struct btrfs_block_group_cache *cache[8];
1542 int i;
1543
1544 while(1) {
Chris Masonbe744172007-05-06 10:15:01 -04001545 ret = radix_tree_gang_lookup(radix, (void **)cache, 0,
Chris Mason9078a3e2007-04-26 16:46:15 -04001546 ARRAY_SIZE(cache));
1547 if (!ret)
1548 break;
1549 for (i = 0; i < ret; i++) {
Chris Masonbe744172007-05-06 10:15:01 -04001550 radix_tree_delete(radix, cache[i]->key.objectid +
Chris Mason9078a3e2007-04-26 16:46:15 -04001551 cache[i]->key.offset - 1);
1552 kfree(cache[i]);
1553 }
1554 }
1555 return 0;
1556}
1557
Chris Masonbe744172007-05-06 10:15:01 -04001558int btrfs_free_block_groups(struct btrfs_fs_info *info)
1559{
1560 int ret;
1561 int ret2;
Chris Masone37c9e62007-05-09 20:13:14 -04001562 unsigned long gang[16];
1563 int i;
Chris Masonbe744172007-05-06 10:15:01 -04001564
1565 ret = free_block_group_radix(&info->block_group_radix);
1566 ret2 = free_block_group_radix(&info->block_group_data_radix);
1567 if (ret)
1568 return ret;
1569 if (ret2)
1570 return ret2;
Chris Masone37c9e62007-05-09 20:13:14 -04001571
1572 while(1) {
1573 ret = find_first_radix_bit(&info->extent_map_radix,
1574 gang, 0, ARRAY_SIZE(gang));
1575 if (!ret)
1576 break;
1577 for (i = 0; i < ret; i++) {
1578 clear_radix_bit(&info->extent_map_radix, gang[i]);
1579 }
1580 }
Chris Masonbe744172007-05-06 10:15:01 -04001581 return 0;
1582}
1583
Chris Mason9078a3e2007-04-26 16:46:15 -04001584int btrfs_read_block_groups(struct btrfs_root *root)
1585{
1586 struct btrfs_path *path;
1587 int ret;
1588 int err = 0;
1589 struct btrfs_block_group_item *bi;
1590 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04001591 struct btrfs_fs_info *info = root->fs_info;
1592 struct radix_tree_root *radix;
Chris Mason9078a3e2007-04-26 16:46:15 -04001593 struct btrfs_key key;
1594 struct btrfs_key found_key;
1595 struct btrfs_leaf *leaf;
Chris Mason84f54cf2007-06-12 07:43:08 -04001596 u64 group_size_blocks;
Chris Mason31f3c992007-04-30 15:25:45 -04001597 u64 used;
Chris Mason9078a3e2007-04-26 16:46:15 -04001598
Chris Mason84f54cf2007-06-12 07:43:08 -04001599 group_size_blocks = BTRFS_BLOCK_GROUP_SIZE >>
1600 root->fs_info->sb->s_blocksize_bits;
Chris Masonbe744172007-05-06 10:15:01 -04001601 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04001602 key.objectid = 0;
1603 key.offset = group_size_blocks;
1604 key.flags = 0;
1605 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
1606
1607 path = btrfs_alloc_path();
1608 if (!path)
1609 return -ENOMEM;
1610
1611 while(1) {
Chris Masonbe744172007-05-06 10:15:01 -04001612 ret = btrfs_search_slot(NULL, info->extent_root,
Chris Mason9078a3e2007-04-26 16:46:15 -04001613 &key, path, 0, 0);
1614 if (ret != 0) {
1615 err = ret;
1616 break;
1617 }
1618 leaf = btrfs_buffer_leaf(path->nodes[0]);
1619 btrfs_disk_key_to_cpu(&found_key,
1620 &leaf->items[path->slots[0]].key);
1621 cache = kmalloc(sizeof(*cache), GFP_NOFS);
1622 if (!cache) {
1623 err = -1;
1624 break;
1625 }
Chris Mason3e1ad542007-05-07 20:03:49 -04001626
Chris Mason9078a3e2007-04-26 16:46:15 -04001627 bi = btrfs_item_ptr(leaf, path->slots[0],
1628 struct btrfs_block_group_item);
Chris Mason1e2677e2007-05-29 16:52:18 -04001629 if (bi->flags & BTRFS_BLOCK_GROUP_DATA) {
1630 radix = &info->block_group_data_radix;
1631 cache->data = 1;
1632 } else {
1633 radix = &info->block_group_radix;
1634 cache->data = 0;
1635 }
1636
Chris Mason9078a3e2007-04-26 16:46:15 -04001637 memcpy(&cache->item, bi, sizeof(*bi));
1638 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Mason31f3c992007-04-30 15:25:45 -04001639 cache->last_alloc = cache->key.objectid;
1640 cache->first_free = cache->key.objectid;
Chris Masonbe744172007-05-06 10:15:01 -04001641 cache->pinned = 0;
Chris Masone37c9e62007-05-09 20:13:14 -04001642 cache->cached = 0;
1643
Chris Mason3e1ad542007-05-07 20:03:49 -04001644 cache->radix = radix;
1645
Chris Mason9078a3e2007-04-26 16:46:15 -04001646 key.objectid = found_key.objectid + found_key.offset;
1647 btrfs_release_path(root, path);
Chris Masonbe744172007-05-06 10:15:01 -04001648 ret = radix_tree_insert(radix, found_key.objectid +
Chris Mason9078a3e2007-04-26 16:46:15 -04001649 found_key.offset - 1,
1650 (void *)cache);
1651 BUG_ON(ret);
Chris Mason31f3c992007-04-30 15:25:45 -04001652 used = btrfs_block_group_used(bi);
Chris Mason84f54cf2007-06-12 07:43:08 -04001653 if (used < div_factor(key.offset, 8)) {
Chris Masonbe744172007-05-06 10:15:01 -04001654 radix_tree_tag_set(radix, found_key.objectid +
Chris Mason31f3c992007-04-30 15:25:45 -04001655 found_key.offset - 1,
1656 BTRFS_BLOCK_GROUP_AVAIL);
1657 }
Chris Mason9078a3e2007-04-26 16:46:15 -04001658 if (key.objectid >=
Chris Mason4b52dff2007-06-26 10:06:50 -04001659 btrfs_super_total_blocks(&info->super_copy))
Chris Mason9078a3e2007-04-26 16:46:15 -04001660 break;
1661 }
1662
1663 btrfs_free_path(path);
1664 return 0;
1665}