blob: 089c41cbca74dffcbd92e3de0ef98db999e470ef [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;
Chris Mason5f39d392007-10-15 16:14:19 -040036 struct extent_buffer *leaf;
Chris Masone37c9e62007-05-09 20:13:14 -040037 struct radix_tree_root *extent_radix;
38 int slot;
39 u64 i;
40 u64 last = 0;
41 u64 hole_size;
Yan7d7d6062007-09-14 16:15:28 -040042 u64 first_free;
Chris Masone37c9e62007-05-09 20:13:14 -040043 int found = 0;
44
45 root = root->fs_info->extent_root;
46 extent_radix = &root->fs_info->extent_map_radix;
47
48 if (block_group->cached)
49 return 0;
50 if (block_group->data)
51 return 0;
52 path = btrfs_alloc_path();
53 if (!path)
54 return -ENOMEM;
Yan7d7d6062007-09-14 16:15:28 -040055
Chris Mason2cc58cf2007-08-27 16:49:44 -040056 path->reada = 2;
Yan7d7d6062007-09-14 16:15:28 -040057 first_free = block_group->key.objectid;
Chris Masone37c9e62007-05-09 20:13:14 -040058 key.objectid = block_group->key.objectid;
Chris Masone37c9e62007-05-09 20:13:14 -040059 key.offset = 0;
Yan7d7d6062007-09-14 16:15:28 -040060
Chris Masone37c9e62007-05-09 20:13:14 -040061 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
62 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Yan7d7d6062007-09-14 16:15:28 -040063
Chris Masone37c9e62007-05-09 20:13:14 -040064 if (ret < 0)
65 return ret;
Yan7d7d6062007-09-14 16:15:28 -040066
Chris Masone37c9e62007-05-09 20:13:14 -040067 if (ret && path->slots[0] > 0)
68 path->slots[0]--;
Yan7d7d6062007-09-14 16:15:28 -040069
Chris Masone37c9e62007-05-09 20:13:14 -040070 while(1) {
Chris Mason5f39d392007-10-15 16:14:19 -040071 leaf = path->nodes[0];
Chris Masone37c9e62007-05-09 20:13:14 -040072 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -040073 if (slot >= btrfs_header_nritems(leaf)) {
Chris Masone37c9e62007-05-09 20:13:14 -040074 ret = btrfs_next_leaf(root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -040075 if (ret < 0)
76 goto err;
Chris Masonde428b62007-05-18 13:28:27 -040077 if (ret == 0) {
Chris Masone37c9e62007-05-09 20:13:14 -040078 continue;
Chris Masonde428b62007-05-18 13:28:27 -040079 } else {
Chris Masone37c9e62007-05-09 20:13:14 -040080 break;
81 }
82 }
Yan7d7d6062007-09-14 16:15:28 -040083
Chris Mason5f39d392007-10-15 16:14:19 -040084 btrfs_item_key_to_cpu(leaf, &key, slot);
Yan7d7d6062007-09-14 16:15:28 -040085 if (key.objectid < block_group->key.objectid) {
86 if (key.objectid + key.offset > first_free)
87 first_free = key.objectid + key.offset;
88 goto next;
89 }
90
Chris Masone37c9e62007-05-09 20:13:14 -040091 if (key.objectid >= block_group->key.objectid +
92 block_group->key.offset) {
Yan7d7d6062007-09-14 16:15:28 -040093 break;
94 }
95
96 if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) {
97 if (!found) {
98 last = first_free;
99 found = 1;
Chris Masone37c9e62007-05-09 20:13:14 -0400100 }
Yan7d7d6062007-09-14 16:15:28 -0400101 hole_size = key.objectid - last;
Chris Masone37c9e62007-05-09 20:13:14 -0400102 for (i = 0; i < hole_size; i++) {
103 set_radix_bit(extent_radix, last + i);
104 }
Yan7d7d6062007-09-14 16:15:28 -0400105 last = key.objectid + key.offset;
Chris Masone37c9e62007-05-09 20:13:14 -0400106 }
Yan7d7d6062007-09-14 16:15:28 -0400107next:
Chris Masone37c9e62007-05-09 20:13:14 -0400108 path->slots[0]++;
109 }
110
Yan7d7d6062007-09-14 16:15:28 -0400111 if (!found)
112 last = first_free;
113 if (block_group->key.objectid +
114 block_group->key.offset > last) {
115 hole_size = block_group->key.objectid +
116 block_group->key.offset - last;
117 for (i = 0; i < hole_size; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -0400118 set_radix_bit(extent_radix, last + i);
Yan7d7d6062007-09-14 16:15:28 -0400119 }
120 }
Chris Masone37c9e62007-05-09 20:13:14 -0400121 block_group->cached = 1;
Chris Mason54aa1f42007-06-22 14:16:25 -0400122err:
Chris Masone37c9e62007-05-09 20:13:14 -0400123 btrfs_free_path(path);
124 return 0;
125}
126
Chris Mason5276aed2007-06-11 21:33:38 -0400127struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
128 btrfs_fs_info *info,
129 u64 blocknr)
Chris Masonbe744172007-05-06 10:15:01 -0400130{
131 struct btrfs_block_group_cache *block_group;
132 int ret;
133
134 ret = radix_tree_gang_lookup(&info->block_group_radix,
135 (void **)&block_group,
136 blocknr, 1);
137 if (ret) {
Chris Mason3e1ad542007-05-07 20:03:49 -0400138 if (block_group->key.objectid <= blocknr && blocknr <=
Chris Masonbe744172007-05-06 10:15:01 -0400139 block_group->key.objectid + block_group->key.offset)
140 return block_group;
141 }
142 ret = radix_tree_gang_lookup(&info->block_group_data_radix,
143 (void **)&block_group,
144 blocknr, 1);
145 if (ret) {
Chris Mason3e1ad542007-05-07 20:03:49 -0400146 if (block_group->key.objectid <= blocknr && blocknr <=
Chris Masonbe744172007-05-06 10:15:01 -0400147 block_group->key.objectid + block_group->key.offset)
148 return block_group;
149 }
Chris Masonbe744172007-05-06 10:15:01 -0400150 return NULL;
151}
152
Chris Masone37c9e62007-05-09 20:13:14 -0400153static u64 leaf_range(struct btrfs_root *root)
154{
155 u64 size = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason84f54cf2007-06-12 07:43:08 -0400156 do_div(size, sizeof(struct btrfs_extent_item) +
157 sizeof(struct btrfs_item));
Chris Masone37c9e62007-05-09 20:13:14 -0400158 return size;
159}
160
161static u64 find_search_start(struct btrfs_root *root,
162 struct btrfs_block_group_cache **cache_ret,
163 u64 search_start, int num)
164{
165 unsigned long gang[8];
166 int ret;
167 struct btrfs_block_group_cache *cache = *cache_ret;
168 u64 last = max(search_start, cache->key.objectid);
169
170 if (cache->data)
171 goto out;
Chris Masone37c9e62007-05-09 20:13:14 -0400172again:
Chris Mason54aa1f42007-06-22 14:16:25 -0400173 ret = cache_block_group(root, cache);
174 if (ret)
175 goto out;
Chris Masone37c9e62007-05-09 20:13:14 -0400176 while(1) {
177 ret = find_first_radix_bit(&root->fs_info->extent_map_radix,
178 gang, last, ARRAY_SIZE(gang));
179 if (!ret)
180 goto out;
181 last = gang[ret-1] + 1;
182 if (num > 1) {
183 if (ret != ARRAY_SIZE(gang)) {
184 goto new_group;
185 }
186 if (gang[ret-1] - gang[0] > leaf_range(root)) {
187 continue;
188 }
189 }
190 if (gang[0] >= cache->key.objectid + cache->key.offset) {
191 goto new_group;
192 }
193 return gang[0];
194 }
195out:
196 return max(cache->last_alloc, search_start);
197
198new_group:
Chris Mason5276aed2007-06-11 21:33:38 -0400199 cache = btrfs_lookup_block_group(root->fs_info,
200 last + cache->key.offset - 1);
Chris Masone37c9e62007-05-09 20:13:14 -0400201 if (!cache) {
202 return max((*cache_ret)->last_alloc, search_start);
203 }
204 cache = btrfs_find_block_group(root, cache,
Chris Masonde428b62007-05-18 13:28:27 -0400205 last + cache->key.offset - 1, 0, 0);
Chris Masone37c9e62007-05-09 20:13:14 -0400206 *cache_ret = cache;
207 goto again;
208}
209
Chris Mason84f54cf2007-06-12 07:43:08 -0400210static u64 div_factor(u64 num, int factor)
211{
212 num *= factor;
213 do_div(num, 10);
214 return num;
215}
216
Chris Mason31f3c992007-04-30 15:25:45 -0400217struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
218 struct btrfs_block_group_cache
Chris Masonbe744172007-05-06 10:15:01 -0400219 *hint, u64 search_start,
Chris Masonde428b62007-05-18 13:28:27 -0400220 int data, int owner)
Chris Masoncd1bc462007-04-27 10:08:34 -0400221{
222 struct btrfs_block_group_cache *cache[8];
Chris Mason31f3c992007-04-30 15:25:45 -0400223 struct btrfs_block_group_cache *found_group = NULL;
Chris Masoncd1bc462007-04-27 10:08:34 -0400224 struct btrfs_fs_info *info = root->fs_info;
Chris Masonbe744172007-05-06 10:15:01 -0400225 struct radix_tree_root *radix;
Chris Mason1e2677e2007-05-29 16:52:18 -0400226 struct radix_tree_root *swap_radix;
Chris Masoncd1bc462007-04-27 10:08:34 -0400227 u64 used;
Chris Mason31f3c992007-04-30 15:25:45 -0400228 u64 last = 0;
229 u64 hint_last;
Chris Masoncd1bc462007-04-27 10:08:34 -0400230 int i;
231 int ret;
Chris Mason31f3c992007-04-30 15:25:45 -0400232 int full_search = 0;
Chris Masonde428b62007-05-18 13:28:27 -0400233 int factor = 8;
Chris Mason1e2677e2007-05-29 16:52:18 -0400234 int data_swap = 0;
Chris Masonde428b62007-05-18 13:28:27 -0400235
236 if (!owner)
237 factor = 5;
Chris Masonbe744172007-05-06 10:15:01 -0400238
Chris Mason1e2677e2007-05-29 16:52:18 -0400239 if (data) {
Chris Masonbe744172007-05-06 10:15:01 -0400240 radix = &info->block_group_data_radix;
Chris Mason1e2677e2007-05-29 16:52:18 -0400241 swap_radix = &info->block_group_radix;
242 } else {
Chris Masonbe744172007-05-06 10:15:01 -0400243 radix = &info->block_group_radix;
Chris Mason1e2677e2007-05-29 16:52:18 -0400244 swap_radix = &info->block_group_data_radix;
245 }
Chris Masonbe744172007-05-06 10:15:01 -0400246
247 if (search_start) {
248 struct btrfs_block_group_cache *shint;
Chris Mason5276aed2007-06-11 21:33:38 -0400249 shint = btrfs_lookup_block_group(info, search_start);
Yane9fe3952007-08-29 09:11:44 -0400250 if (shint && shint->data == data) {
Chris Masonbe744172007-05-06 10:15:01 -0400251 used = btrfs_block_group_used(&shint->item);
252 if (used + shint->pinned <
Chris Mason84f54cf2007-06-12 07:43:08 -0400253 div_factor(shint->key.offset, factor)) {
Chris Masonbe744172007-05-06 10:15:01 -0400254 return shint;
255 }
256 }
257 }
258 if (hint && hint->data == data) {
Chris Mason31f3c992007-04-30 15:25:45 -0400259 used = btrfs_block_group_used(&hint->item);
Chris Mason84f54cf2007-06-12 07:43:08 -0400260 if (used + hint->pinned <
261 div_factor(hint->key.offset, factor)) {
Chris Mason31f3c992007-04-30 15:25:45 -0400262 return hint;
263 }
Chris Mason84f54cf2007-06-12 07:43:08 -0400264 if (used >= div_factor(hint->key.offset, 8)) {
Chris Masonbe744172007-05-06 10:15:01 -0400265 radix_tree_tag_clear(radix,
266 hint->key.objectid +
267 hint->key.offset - 1,
268 BTRFS_BLOCK_GROUP_AVAIL);
269 }
Chris Mason8d7be552007-05-10 11:24:42 -0400270 last = hint->key.offset * 3;
Chris Masonbe744172007-05-06 10:15:01 -0400271 if (hint->key.objectid >= last)
Chris Masone37c9e62007-05-09 20:13:14 -0400272 last = max(search_start + hint->key.offset - 1,
273 hint->key.objectid - last);
Chris Masonbe744172007-05-06 10:15:01 -0400274 else
275 last = hint->key.objectid + hint->key.offset;
Chris Mason31f3c992007-04-30 15:25:45 -0400276 hint_last = last;
277 } else {
Chris Masone37c9e62007-05-09 20:13:14 -0400278 if (hint)
279 hint_last = max(hint->key.objectid, search_start);
280 else
281 hint_last = search_start;
282
283 last = hint_last;
Chris Mason31f3c992007-04-30 15:25:45 -0400284 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400285 while(1) {
Chris Masonbe744172007-05-06 10:15:01 -0400286 ret = radix_tree_gang_lookup_tag(radix, (void **)cache,
Chris Masoncd1bc462007-04-27 10:08:34 -0400287 last, ARRAY_SIZE(cache),
Chris Mason31f3c992007-04-30 15:25:45 -0400288 BTRFS_BLOCK_GROUP_AVAIL);
Chris Masoncd1bc462007-04-27 10:08:34 -0400289 if (!ret)
290 break;
291 for (i = 0; i < ret; i++) {
Chris Masonbe08c1b2007-05-03 09:06:49 -0400292 last = cache[i]->key.objectid +
293 cache[i]->key.offset;
Chris Masoncd1bc462007-04-27 10:08:34 -0400294 used = btrfs_block_group_used(&cache[i]->item);
Chris Masonbe744172007-05-06 10:15:01 -0400295 if (used + cache[i]->pinned <
Chris Mason84f54cf2007-06-12 07:43:08 -0400296 div_factor(cache[i]->key.offset, factor)) {
Chris Mason31f3c992007-04-30 15:25:45 -0400297 found_group = cache[i];
298 goto found;
Chris Masoncd1bc462007-04-27 10:08:34 -0400299 }
Chris Mason84f54cf2007-06-12 07:43:08 -0400300 if (used >= div_factor(cache[i]->key.offset, 8)) {
Chris Masonbe744172007-05-06 10:15:01 -0400301 radix_tree_tag_clear(radix,
302 cache[i]->key.objectid +
303 cache[i]->key.offset - 1,
304 BTRFS_BLOCK_GROUP_AVAIL);
305 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400306 }
Chris Masonde428b62007-05-18 13:28:27 -0400307 cond_resched();
Chris Masoncd1bc462007-04-27 10:08:34 -0400308 }
Chris Mason31f3c992007-04-30 15:25:45 -0400309 last = hint_last;
310again:
Chris Masoncd1bc462007-04-27 10:08:34 -0400311 while(1) {
Chris Masonbe744172007-05-06 10:15:01 -0400312 ret = radix_tree_gang_lookup(radix, (void **)cache,
313 last, ARRAY_SIZE(cache));
Chris Masoncd1bc462007-04-27 10:08:34 -0400314 if (!ret)
315 break;
316 for (i = 0; i < ret; i++) {
Chris Masonbe08c1b2007-05-03 09:06:49 -0400317 last = cache[i]->key.objectid +
318 cache[i]->key.offset;
Chris Masoncd1bc462007-04-27 10:08:34 -0400319 used = btrfs_block_group_used(&cache[i]->item);
Chris Masonbe744172007-05-06 10:15:01 -0400320 if (used + cache[i]->pinned < cache[i]->key.offset) {
Chris Mason31f3c992007-04-30 15:25:45 -0400321 found_group = cache[i];
322 goto found;
Chris Masoncd1bc462007-04-27 10:08:34 -0400323 }
Chris Masonbe744172007-05-06 10:15:01 -0400324 if (used >= cache[i]->key.offset) {
325 radix_tree_tag_clear(radix,
326 cache[i]->key.objectid +
327 cache[i]->key.offset - 1,
328 BTRFS_BLOCK_GROUP_AVAIL);
329 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400330 }
Chris Masonde428b62007-05-18 13:28:27 -0400331 cond_resched();
Chris Masoncd1bc462007-04-27 10:08:34 -0400332 }
Chris Mason31f3c992007-04-30 15:25:45 -0400333 if (!full_search) {
Chris Masonbe744172007-05-06 10:15:01 -0400334 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -0400335 full_search = 1;
336 goto again;
337 }
Chris Mason1e2677e2007-05-29 16:52:18 -0400338 if (!data_swap) {
339 struct radix_tree_root *tmp = radix;
340 data_swap = 1;
341 radix = swap_radix;
342 swap_radix = tmp;
343 last = search_start;
344 goto again;
345 }
Chris Mason31f3c992007-04-30 15:25:45 -0400346 if (!found_group) {
Chris Masonbe744172007-05-06 10:15:01 -0400347 ret = radix_tree_gang_lookup(radix,
Chris Mason31f3c992007-04-30 15:25:45 -0400348 (void **)&found_group, 0, 1);
Chris Mason1e2677e2007-05-29 16:52:18 -0400349 if (ret == 0) {
350 ret = radix_tree_gang_lookup(swap_radix,
351 (void **)&found_group,
352 0, 1);
353 }
Chris Mason31f3c992007-04-30 15:25:45 -0400354 BUG_ON(ret != 1);
355 }
Chris Masonbe744172007-05-06 10:15:01 -0400356found:
Chris Mason31f3c992007-04-30 15:25:45 -0400357 return found_group;
Chris Masoncd1bc462007-04-27 10:08:34 -0400358}
359
Chris Masonb18c6682007-04-17 13:26:50 -0400360int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
361 struct btrfs_root *root,
362 u64 blocknr, u64 num_blocks)
Chris Mason02217ed2007-03-02 16:08:05 -0500363{
Chris Mason5caf2a02007-04-02 11:20:42 -0400364 struct btrfs_path *path;
Chris Mason02217ed2007-03-02 16:08:05 -0500365 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400366 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400367 struct extent_buffer *l;
Chris Mason234b63a2007-03-13 10:46:10 -0400368 struct btrfs_extent_item *item;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400369 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500370
Chris Mason5caf2a02007-04-02 11:20:42 -0400371 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -0400372 if (!path)
373 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -0400374
Chris Mason02217ed2007-03-02 16:08:05 -0500375 key.objectid = blocknr;
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 Mason5f39d392007-10-15 16:14:19 -0400386 l = path->nodes[0];
Chris Mason5caf2a02007-04-02 11:20:42 -0400387 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400388 refs = btrfs_extent_refs(l, item);
389 btrfs_set_extent_refs(l, item, refs + 1);
Chris Mason5caf2a02007-04-02 11:20:42 -0400390 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masona28ec192007-03-06 20:08:01 -0500391
Chris Mason5caf2a02007-04-02 11:20:42 -0400392 btrfs_release_path(root->fs_info->extent_root, path);
393 btrfs_free_path(path);
Chris Mason9f5fae22007-03-20 14:38:32 -0400394 finish_current_insert(trans, root->fs_info->extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -0400395 del_pending_extents(trans, root->fs_info->extent_root);
Chris Mason02217ed2007-03-02 16:08:05 -0500396 return 0;
397}
398
Chris Masone9d0b132007-08-10 14:06:19 -0400399int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
400 struct btrfs_root *root)
401{
402 finish_current_insert(trans, root->fs_info->extent_root);
403 del_pending_extents(trans, root->fs_info->extent_root);
404 return 0;
405}
406
Chris Masonb18c6682007-04-17 13:26:50 -0400407static int lookup_extent_ref(struct btrfs_trans_handle *trans,
408 struct btrfs_root *root, u64 blocknr,
409 u64 num_blocks, u32 *refs)
Chris Masona28ec192007-03-06 20:08:01 -0500410{
Chris Mason5caf2a02007-04-02 11:20:42 -0400411 struct btrfs_path *path;
Chris Masona28ec192007-03-06 20:08:01 -0500412 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400413 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400414 struct extent_buffer *l;
Chris Mason234b63a2007-03-13 10:46:10 -0400415 struct btrfs_extent_item *item;
Chris Mason5caf2a02007-04-02 11:20:42 -0400416
417 path = btrfs_alloc_path();
Chris Masona28ec192007-03-06 20:08:01 -0500418 key.objectid = blocknr;
Chris Mason6407bf62007-03-27 06:33:00 -0400419 key.offset = num_blocks;
Chris Mason62e27492007-03-15 12:56:47 -0400420 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Mason5caf2a02007-04-02 11:20:42 -0400421 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -0400422 0, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400423 if (ret < 0)
424 goto out;
Chris Mason5f39d392007-10-15 16:14:19 -0400425 if (ret != 0) {
426 btrfs_print_leaf(root, path->nodes[0]);
427 printk("failed to find block number %Lu\n", blocknr);
Chris Masona28ec192007-03-06 20:08:01 -0500428 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -0400429 }
430 l = path->nodes[0];
Chris Mason5caf2a02007-04-02 11:20:42 -0400431 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400432 *refs = btrfs_extent_refs(l, item);
Chris Mason54aa1f42007-06-22 14:16:25 -0400433out:
Chris Mason5caf2a02007-04-02 11:20:42 -0400434 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -0500435 return 0;
436}
437
Chris Masonc5739bb2007-04-10 09:27:04 -0400438int btrfs_inc_root_ref(struct btrfs_trans_handle *trans,
439 struct btrfs_root *root)
440{
Chris Mason5f39d392007-10-15 16:14:19 -0400441 return btrfs_inc_extent_ref(trans, root,
442 extent_buffer_blocknr(root->node), 1);
Chris Masonc5739bb2007-04-10 09:27:04 -0400443}
444
Chris Masone089f052007-03-16 16:20:31 -0400445int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400446 struct extent_buffer *buf)
Chris Mason02217ed2007-03-02 16:08:05 -0500447{
448 u64 blocknr;
Chris Mason5f39d392007-10-15 16:14:19 -0400449 u32 nritems;
450 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -0400451 struct btrfs_file_extent_item *fi;
Chris Mason02217ed2007-03-02 16:08:05 -0500452 int i;
Chris Mason6407bf62007-03-27 06:33:00 -0400453 int leaf;
454 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -0400455 int faili;
456 int err;
Chris Masona28ec192007-03-06 20:08:01 -0500457
Chris Mason3768f362007-03-13 16:47:54 -0400458 if (!root->ref_cows)
Chris Masona28ec192007-03-06 20:08:01 -0500459 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400460
461 leaf = btrfs_is_leaf(buf);
462 nritems = btrfs_header_nritems(buf);
463 for (i = 0; i < nritems; i++) {
Chris Mason6407bf62007-03-27 06:33:00 -0400464 if (leaf) {
Chris Mason3a686372007-05-24 13:35:57 -0400465 u64 disk_blocknr;
Chris Mason5f39d392007-10-15 16:14:19 -0400466 btrfs_item_key_to_cpu(buf, &key, i);
467 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason6407bf62007-03-27 06:33:00 -0400468 continue;
Chris Mason5f39d392007-10-15 16:14:19 -0400469 fi = btrfs_item_ptr(buf, i,
Chris Mason6407bf62007-03-27 06:33:00 -0400470 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400471 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason236454d2007-04-19 13:37:44 -0400472 BTRFS_FILE_EXTENT_INLINE)
473 continue;
Chris Mason5f39d392007-10-15 16:14:19 -0400474 disk_blocknr = btrfs_file_extent_disk_blocknr(buf, fi);
Chris Mason3a686372007-05-24 13:35:57 -0400475 if (disk_blocknr == 0)
476 continue;
477 ret = btrfs_inc_extent_ref(trans, root, disk_blocknr,
Chris Mason5f39d392007-10-15 16:14:19 -0400478 btrfs_file_extent_disk_num_blocks(buf, fi));
Chris Mason54aa1f42007-06-22 14:16:25 -0400479 if (ret) {
480 faili = i;
481 goto fail;
482 }
Chris Mason6407bf62007-03-27 06:33:00 -0400483 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400484 blocknr = btrfs_node_blockptr(buf, i);
Chris Masonb18c6682007-04-17 13:26:50 -0400485 ret = btrfs_inc_extent_ref(trans, root, blocknr, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400486 if (ret) {
487 faili = i;
488 goto fail;
489 }
Chris Mason6407bf62007-03-27 06:33:00 -0400490 }
Chris Mason02217ed2007-03-02 16:08:05 -0500491 }
492 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -0400493fail:
Chris Masonccd467d2007-06-28 15:57:36 -0400494 WARN_ON(1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400495 for (i =0; i < faili; i++) {
496 if (leaf) {
497 u64 disk_blocknr;
Chris Mason5f39d392007-10-15 16:14:19 -0400498 btrfs_item_key_to_cpu(buf, &key, i);
499 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -0400500 continue;
Chris Mason5f39d392007-10-15 16:14:19 -0400501 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -0400502 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400503 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -0400504 BTRFS_FILE_EXTENT_INLINE)
505 continue;
Chris Mason5f39d392007-10-15 16:14:19 -0400506 disk_blocknr = btrfs_file_extent_disk_blocknr(buf, fi);
Chris Mason54aa1f42007-06-22 14:16:25 -0400507 if (disk_blocknr == 0)
508 continue;
509 err = btrfs_free_extent(trans, root, disk_blocknr,
Chris Mason5f39d392007-10-15 16:14:19 -0400510 btrfs_file_extent_disk_num_blocks(buf,
511 fi), 0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400512 BUG_ON(err);
513 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400514 blocknr = btrfs_node_blockptr(buf, i);
Chris Mason54aa1f42007-06-22 14:16:25 -0400515 err = btrfs_free_extent(trans, root, blocknr, 1, 0);
516 BUG_ON(err);
517 }
518 }
519 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -0500520}
521
Chris Mason9078a3e2007-04-26 16:46:15 -0400522static int write_one_cache_group(struct btrfs_trans_handle *trans,
523 struct btrfs_root *root,
524 struct btrfs_path *path,
525 struct btrfs_block_group_cache *cache)
526{
527 int ret;
528 int pending_ret;
529 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -0400530 unsigned long bi;
531 struct extent_buffer *leaf;
Chris Mason9078a3e2007-04-26 16:46:15 -0400532
Chris Mason9078a3e2007-04-26 16:46:15 -0400533 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400534 if (ret < 0)
535 goto fail;
Chris Mason9078a3e2007-04-26 16:46:15 -0400536 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -0400537
538 leaf = path->nodes[0];
539 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
540 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
541 btrfs_mark_buffer_dirty(leaf);
Chris Mason9078a3e2007-04-26 16:46:15 -0400542 btrfs_release_path(extent_root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -0400543fail:
Chris Mason9078a3e2007-04-26 16:46:15 -0400544 finish_current_insert(trans, extent_root);
545 pending_ret = del_pending_extents(trans, extent_root);
546 if (ret)
547 return ret;
548 if (pending_ret)
549 return pending_ret;
Chris Masonbe744172007-05-06 10:15:01 -0400550 if (cache->data)
551 cache->last_alloc = cache->first_free;
Chris Mason9078a3e2007-04-26 16:46:15 -0400552 return 0;
553
554}
555
Chris Masonbe744172007-05-06 10:15:01 -0400556static int write_dirty_block_radix(struct btrfs_trans_handle *trans,
557 struct btrfs_root *root,
558 struct radix_tree_root *radix)
Chris Mason9078a3e2007-04-26 16:46:15 -0400559{
560 struct btrfs_block_group_cache *cache[8];
561 int ret;
562 int err = 0;
563 int werr = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -0400564 int i;
565 struct btrfs_path *path;
Chris Mason54aa1f42007-06-22 14:16:25 -0400566 unsigned long off = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -0400567
568 path = btrfs_alloc_path();
569 if (!path)
570 return -ENOMEM;
571
572 while(1) {
573 ret = radix_tree_gang_lookup_tag(radix, (void **)cache,
Chris Mason54aa1f42007-06-22 14:16:25 -0400574 off, ARRAY_SIZE(cache),
Chris Mason9078a3e2007-04-26 16:46:15 -0400575 BTRFS_BLOCK_GROUP_DIRTY);
576 if (!ret)
577 break;
578 for (i = 0; i < ret; i++) {
Chris Mason54aa1f42007-06-22 14:16:25 -0400579 err = write_one_cache_group(trans, root,
580 path, cache[i]);
581 /*
582 * if we fail to write the cache group, we want
583 * to keep it marked dirty in hopes that a later
584 * write will work
585 */
586 if (err) {
587 werr = err;
588 off = cache[i]->key.objectid +
589 cache[i]->key.offset;
590 continue;
591 }
592
Chris Mason9078a3e2007-04-26 16:46:15 -0400593 radix_tree_tag_clear(radix, cache[i]->key.objectid +
594 cache[i]->key.offset - 1,
595 BTRFS_BLOCK_GROUP_DIRTY);
Chris Mason9078a3e2007-04-26 16:46:15 -0400596 }
597 }
598 btrfs_free_path(path);
599 return werr;
600}
601
Chris Masonbe744172007-05-06 10:15:01 -0400602int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
603 struct btrfs_root *root)
604{
605 int ret;
606 int ret2;
607 ret = write_dirty_block_radix(trans, root,
608 &root->fs_info->block_group_radix);
609 ret2 = write_dirty_block_radix(trans, root,
610 &root->fs_info->block_group_data_radix);
611 if (ret)
612 return ret;
613 if (ret2)
614 return ret2;
615 return 0;
616}
617
Chris Mason9078a3e2007-04-26 16:46:15 -0400618static int update_block_group(struct btrfs_trans_handle *trans,
619 struct btrfs_root *root,
Chris Mason1e2677e2007-05-29 16:52:18 -0400620 u64 blocknr, u64 num, int alloc, int mark_free,
621 int data)
Chris Mason9078a3e2007-04-26 16:46:15 -0400622{
623 struct btrfs_block_group_cache *cache;
624 struct btrfs_fs_info *info = root->fs_info;
625 u64 total = num;
626 u64 old_val;
627 u64 block_in_group;
Chris Masone37c9e62007-05-09 20:13:14 -0400628 u64 i;
Chris Mason1e2677e2007-05-29 16:52:18 -0400629 int ret;
Chris Mason3e1ad542007-05-07 20:03:49 -0400630
Chris Mason9078a3e2007-04-26 16:46:15 -0400631 while(total) {
Chris Mason5276aed2007-06-11 21:33:38 -0400632 cache = btrfs_lookup_block_group(info, blocknr);
Chris Mason3e1ad542007-05-07 20:03:49 -0400633 if (!cache) {
Chris Mason9078a3e2007-04-26 16:46:15 -0400634 return -1;
Chris Masoncd1bc462007-04-27 10:08:34 -0400635 }
Chris Mason9078a3e2007-04-26 16:46:15 -0400636 block_in_group = blocknr - cache->key.objectid;
637 WARN_ON(block_in_group > cache->key.offset);
Chris Mason3e1ad542007-05-07 20:03:49 -0400638 radix_tree_tag_set(cache->radix, cache->key.objectid +
Chris Masonbe744172007-05-06 10:15:01 -0400639 cache->key.offset - 1,
Chris Mason9078a3e2007-04-26 16:46:15 -0400640 BTRFS_BLOCK_GROUP_DIRTY);
641
642 old_val = btrfs_block_group_used(&cache->item);
643 num = min(total, cache->key.offset - block_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -0400644 if (alloc) {
Chris Masoncd1bc462007-04-27 10:08:34 -0400645 if (blocknr > cache->last_alloc)
646 cache->last_alloc = blocknr;
Chris Masone37c9e62007-05-09 20:13:14 -0400647 if (!cache->data) {
648 for (i = 0; i < num; i++) {
649 clear_radix_bit(&info->extent_map_radix,
650 blocknr + i);
651 }
652 }
Chris Mason1e2677e2007-05-29 16:52:18 -0400653 if (cache->data != data &&
Chris Mason84f54cf2007-06-12 07:43:08 -0400654 old_val < (cache->key.offset >> 1)) {
Chris Mason1e2677e2007-05-29 16:52:18 -0400655 cache->data = data;
656 radix_tree_delete(cache->radix,
657 cache->key.objectid +
658 cache->key.offset - 1);
659
660 if (data) {
661 cache->radix =
662 &info->block_group_data_radix;
663 cache->item.flags |=
664 BTRFS_BLOCK_GROUP_DATA;
665 } else {
666 cache->radix = &info->block_group_radix;
667 cache->item.flags &=
668 ~BTRFS_BLOCK_GROUP_DATA;
669 }
670 ret = radix_tree_insert(cache->radix,
671 cache->key.objectid +
672 cache->key.offset - 1,
673 (void *)cache);
674 }
675 old_val += num;
Chris Masoncd1bc462007-04-27 10:08:34 -0400676 } else {
Chris Mason9078a3e2007-04-26 16:46:15 -0400677 old_val -= num;
Chris Masoncd1bc462007-04-27 10:08:34 -0400678 if (blocknr < cache->first_free)
679 cache->first_free = blocknr;
Chris Masone37c9e62007-05-09 20:13:14 -0400680 if (!cache->data && mark_free) {
681 for (i = 0; i < num; i++) {
682 set_radix_bit(&info->extent_map_radix,
683 blocknr + i);
684 }
685 }
Chris Mason84f54cf2007-06-12 07:43:08 -0400686 if (old_val < (cache->key.offset >> 1) &&
687 old_val + num >= (cache->key.offset >> 1)) {
Chris Masone37c9e62007-05-09 20:13:14 -0400688 radix_tree_tag_set(cache->radix,
689 cache->key.objectid +
690 cache->key.offset - 1,
691 BTRFS_BLOCK_GROUP_AVAIL);
692 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400693 }
Chris Mason9078a3e2007-04-26 16:46:15 -0400694 btrfs_set_block_group_used(&cache->item, old_val);
Chris Masone37c9e62007-05-09 20:13:14 -0400695 total -= num;
696 blocknr += num;
Chris Mason9078a3e2007-04-26 16:46:15 -0400697 }
698 return 0;
699}
700
Chris Masonccd467d2007-06-28 15:57:36 -0400701int btrfs_copy_pinned(struct btrfs_root *root, struct radix_tree_root *copy)
702{
703 unsigned long gang[8];
704 u64 last = 0;
705 struct radix_tree_root *pinned_radix = &root->fs_info->pinned_radix;
706 int ret;
707 int i;
708
709 while(1) {
710 ret = find_first_radix_bit(pinned_radix, gang, last,
711 ARRAY_SIZE(gang));
712 if (!ret)
713 break;
714 for (i = 0 ; i < ret; i++) {
715 set_radix_bit(copy, gang[i]);
716 last = gang[i] + 1;
717 }
718 }
Chris Mason26b80032007-08-08 20:17:12 -0400719 ret = find_first_radix_bit(&root->fs_info->extent_ins_radix, gang, 0,
720 ARRAY_SIZE(gang));
721 WARN_ON(ret);
Chris Masonccd467d2007-06-28 15:57:36 -0400722 return 0;
723}
724
725int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
726 struct btrfs_root *root,
727 struct radix_tree_root *unpin_radix)
Chris Masona28ec192007-03-06 20:08:01 -0500728{
Chris Mason8ef97622007-03-26 10:15:30 -0400729 unsigned long gang[8];
Chris Masonbe744172007-05-06 10:15:01 -0400730 struct btrfs_block_group_cache *block_group;
Chris Mason88fd1462007-03-16 08:56:18 -0400731 u64 first = 0;
Chris Masona28ec192007-03-06 20:08:01 -0500732 int ret;
733 int i;
Chris Mason8ef97622007-03-26 10:15:30 -0400734 struct radix_tree_root *pinned_radix = &root->fs_info->pinned_radix;
Chris Masone37c9e62007-05-09 20:13:14 -0400735 struct radix_tree_root *extent_radix = &root->fs_info->extent_map_radix;
Chris Masona28ec192007-03-06 20:08:01 -0500736
737 while(1) {
Chris Masonccd467d2007-06-28 15:57:36 -0400738 ret = find_first_radix_bit(unpin_radix, gang, 0,
Chris Mason8ef97622007-03-26 10:15:30 -0400739 ARRAY_SIZE(gang));
Chris Masona28ec192007-03-06 20:08:01 -0500740 if (!ret)
741 break;
Chris Mason88fd1462007-03-16 08:56:18 -0400742 if (!first)
Chris Mason8ef97622007-03-26 10:15:30 -0400743 first = gang[0];
Chris Mason0579da42007-03-07 16:15:30 -0500744 for (i = 0; i < ret; i++) {
Chris Mason8ef97622007-03-26 10:15:30 -0400745 clear_radix_bit(pinned_radix, gang[i]);
Chris Masonccd467d2007-06-28 15:57:36 -0400746 clear_radix_bit(unpin_radix, gang[i]);
Chris Mason5276aed2007-06-11 21:33:38 -0400747 block_group = btrfs_lookup_block_group(root->fs_info,
748 gang[i]);
Chris Masonbe744172007-05-06 10:15:01 -0400749 if (block_group) {
750 WARN_ON(block_group->pinned == 0);
751 block_group->pinned--;
752 if (gang[i] < block_group->last_alloc)
753 block_group->last_alloc = gang[i];
Chris Masone37c9e62007-05-09 20:13:14 -0400754 if (!block_group->data)
755 set_radix_bit(extent_radix, gang[i]);
Chris Masonbe744172007-05-06 10:15:01 -0400756 }
Chris Mason0579da42007-03-07 16:15:30 -0500757 }
Chris Masona28ec192007-03-06 20:08:01 -0500758 }
759 return 0;
760}
761
Chris Masone089f052007-03-16 16:20:31 -0400762static int finish_current_insert(struct btrfs_trans_handle *trans, struct
763 btrfs_root *extent_root)
Chris Mason037e6392007-03-07 11:50:24 -0500764{
Chris Masone2fa7222007-03-12 16:22:34 -0400765 struct btrfs_key ins;
Chris Mason234b63a2007-03-13 10:46:10 -0400766 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -0500767 int i;
768 int ret;
Chris Mason26b80032007-08-08 20:17:12 -0400769 int err;
770 unsigned long gang[8];
Chris Mason1261ec42007-03-20 20:35:03 -0400771 struct btrfs_fs_info *info = extent_root->fs_info;
Chris Mason037e6392007-03-07 11:50:24 -0500772
Chris Mason5f39d392007-10-15 16:14:19 -0400773 btrfs_set_stack_extent_refs(&extent_item, 1);
Chris Mason037e6392007-03-07 11:50:24 -0500774 ins.offset = 1;
Chris Mason62e27492007-03-15 12:56:47 -0400775 btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
Chris Mason5f39d392007-10-15 16:14:19 -0400776 btrfs_set_stack_extent_owner(&extent_item,
777 extent_root->root_key.objectid);
Chris Mason037e6392007-03-07 11:50:24 -0500778
Chris Mason26b80032007-08-08 20:17:12 -0400779 while(1) {
780 ret = find_first_radix_bit(&info->extent_ins_radix, gang, 0,
781 ARRAY_SIZE(gang));
782 if (!ret)
783 break;
784
785 for (i = 0; i < ret; i++) {
786 ins.objectid = gang[i];
787 err = btrfs_insert_item(trans, extent_root, &ins,
788 &extent_item,
789 sizeof(extent_item));
790 clear_radix_bit(&info->extent_ins_radix, gang[i]);
791 WARN_ON(err);
792 }
Chris Mason037e6392007-03-07 11:50:24 -0500793 }
Chris Mason037e6392007-03-07 11:50:24 -0500794 return 0;
795}
796
Chris Mason8ef97622007-03-26 10:15:30 -0400797static int pin_down_block(struct btrfs_root *root, u64 blocknr, int pending)
Chris Masone20d96d2007-03-22 12:13:20 -0400798{
799 int err;
Chris Mason5f39d392007-10-15 16:14:19 -0400800 struct extent_buffer *buf;
Chris Mason78fae272007-03-25 11:35:08 -0400801
Chris Masonf4b9aa82007-03-27 11:05:53 -0400802 if (!pending) {
Chris Mason5f39d392007-10-15 16:14:19 -0400803 buf = btrfs_find_tree_block(root, blocknr);
804 if (buf) {
805 if (btrfs_buffer_uptodate(buf)) {
Chris Mason2c90e5d2007-04-02 10:50:19 -0400806 u64 transid =
807 root->fs_info->running_transaction->transid;
Chris Mason5f39d392007-10-15 16:14:19 -0400808 if (btrfs_header_generation(buf) == transid) {
809 free_extent_buffer(buf);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400810 return 0;
811 }
Chris Masonf4b9aa82007-03-27 11:05:53 -0400812 }
Chris Mason5f39d392007-10-15 16:14:19 -0400813 free_extent_buffer(buf);
Chris Mason8ef97622007-03-26 10:15:30 -0400814 }
Chris Mason8ef97622007-03-26 10:15:30 -0400815 err = set_radix_bit(&root->fs_info->pinned_radix, blocknr);
Chris Masonbe744172007-05-06 10:15:01 -0400816 if (!err) {
817 struct btrfs_block_group_cache *cache;
Chris Mason5276aed2007-06-11 21:33:38 -0400818 cache = btrfs_lookup_block_group(root->fs_info,
819 blocknr);
Chris Masonbe744172007-05-06 10:15:01 -0400820 if (cache)
821 cache->pinned++;
822 }
Chris Masonf4b9aa82007-03-27 11:05:53 -0400823 } else {
824 err = set_radix_bit(&root->fs_info->pending_del_radix, blocknr);
825 }
Chris Masonbe744172007-05-06 10:15:01 -0400826 BUG_ON(err < 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400827 return 0;
828}
829
Chris Masona28ec192007-03-06 20:08:01 -0500830/*
831 * remove an extent from the root, returns 0 on success
832 */
Chris Masone089f052007-03-16 16:20:31 -0400833static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masone37c9e62007-05-09 20:13:14 -0400834 *root, u64 blocknr, u64 num_blocks, int pin,
835 int mark_free)
Chris Masona28ec192007-03-06 20:08:01 -0500836{
Chris Mason5caf2a02007-04-02 11:20:42 -0400837 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -0400838 struct btrfs_key key;
Chris Mason1261ec42007-03-20 20:35:03 -0400839 struct btrfs_fs_info *info = root->fs_info;
840 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -0400841 struct extent_buffer *leaf;
Chris Masona28ec192007-03-06 20:08:01 -0500842 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400843 struct btrfs_extent_item *ei;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400844 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500845
Chris Masona28ec192007-03-06 20:08:01 -0500846 key.objectid = blocknr;
Chris Mason62e27492007-03-15 12:56:47 -0400847 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masona28ec192007-03-06 20:08:01 -0500848 key.offset = num_blocks;
849
Chris Mason5caf2a02007-04-02 11:20:42 -0400850 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -0400851 if (!path)
852 return -ENOMEM;
853
Chris Mason5caf2a02007-04-02 11:20:42 -0400854 ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400855 if (ret < 0)
856 return ret;
857 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -0400858
859 leaf = path->nodes[0];
860 ei = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason123abc82007-03-14 14:14:43 -0400861 struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400862 refs = btrfs_extent_refs(leaf, ei);
863 BUG_ON(refs == 0);
864 refs -= 1;
865 btrfs_set_extent_refs(leaf, ei, refs);
866 btrfs_mark_buffer_dirty(leaf);
867
Chris Masoncf27e1e2007-03-13 09:49:06 -0400868 if (refs == 0) {
Josef Bacik58176a92007-08-29 15:47:34 -0400869 u64 super_blocks_used, root_blocks_used;
Chris Mason78fae272007-03-25 11:35:08 -0400870
871 if (pin) {
Chris Mason8ef97622007-03-26 10:15:30 -0400872 ret = pin_down_block(root, blocknr, 0);
Chris Mason78fae272007-03-25 11:35:08 -0400873 BUG_ON(ret);
874 }
875
Josef Bacik58176a92007-08-29 15:47:34 -0400876 /* block accounting for super block */
Chris Mason4b52dff2007-06-26 10:06:50 -0400877 super_blocks_used = btrfs_super_blocks_used(&info->super_copy);
878 btrfs_set_super_blocks_used(&info->super_copy,
Chris Mason1261ec42007-03-20 20:35:03 -0400879 super_blocks_used - num_blocks);
Josef Bacik58176a92007-08-29 15:47:34 -0400880
881 /* block accounting for root item */
Chris Mason5f39d392007-10-15 16:14:19 -0400882 root_blocks_used = btrfs_root_used(&root->root_item);
883 btrfs_set_root_used(&root->root_item,
Josef Bacik58176a92007-08-29 15:47:34 -0400884 root_blocks_used - num_blocks);
885
Chris Mason5caf2a02007-04-02 11:20:42 -0400886 ret = btrfs_del_item(trans, extent_root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -0400887 if (ret) {
888 return ret;
889 }
Chris Masone37c9e62007-05-09 20:13:14 -0400890 ret = update_block_group(trans, root, blocknr, num_blocks, 0,
Chris Mason1e2677e2007-05-29 16:52:18 -0400891 mark_free, 0);
Chris Mason9078a3e2007-04-26 16:46:15 -0400892 BUG_ON(ret);
Chris Masona28ec192007-03-06 20:08:01 -0500893 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400894 btrfs_free_path(path);
Chris Masone089f052007-03-16 16:20:31 -0400895 finish_current_insert(trans, extent_root);
Chris Masona28ec192007-03-06 20:08:01 -0500896 return ret;
897}
898
899/*
Chris Masonfec577f2007-02-26 10:40:21 -0500900 * find all the blocks marked as pending in the radix tree and remove
901 * them from the extent map
902 */
Chris Masone089f052007-03-16 16:20:31 -0400903static int del_pending_extents(struct btrfs_trans_handle *trans, struct
904 btrfs_root *extent_root)
Chris Masonfec577f2007-02-26 10:40:21 -0500905{
906 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -0400907 int wret;
908 int err = 0;
Chris Mason8ef97622007-03-26 10:15:30 -0400909 unsigned long gang[4];
Chris Masonfec577f2007-02-26 10:40:21 -0500910 int i;
Chris Mason8ef97622007-03-26 10:15:30 -0400911 struct radix_tree_root *pending_radix;
912 struct radix_tree_root *pinned_radix;
Chris Masonbe744172007-05-06 10:15:01 -0400913 struct btrfs_block_group_cache *cache;
Chris Mason8ef97622007-03-26 10:15:30 -0400914
915 pending_radix = &extent_root->fs_info->pending_del_radix;
916 pinned_radix = &extent_root->fs_info->pinned_radix;
Chris Masonfec577f2007-02-26 10:40:21 -0500917
918 while(1) {
Chris Masone37c9e62007-05-09 20:13:14 -0400919 ret = find_first_radix_bit(pending_radix, gang, 0,
Chris Mason8ef97622007-03-26 10:15:30 -0400920 ARRAY_SIZE(gang));
Chris Masonfec577f2007-02-26 10:40:21 -0500921 if (!ret)
922 break;
923 for (i = 0; i < ret; i++) {
Chris Mason8ef97622007-03-26 10:15:30 -0400924 wret = set_radix_bit(pinned_radix, gang[i]);
Chris Masonbe744172007-05-06 10:15:01 -0400925 if (wret == 0) {
Chris Mason5276aed2007-06-11 21:33:38 -0400926 cache =
927 btrfs_lookup_block_group(extent_root->fs_info,
Chris Masonbe744172007-05-06 10:15:01 -0400928 gang[i]);
929 if (cache)
930 cache->pinned++;
931 }
932 if (wret < 0) {
933 printk(KERN_CRIT "set_radix_bit, err %d\n",
934 wret);
935 BUG_ON(wret < 0);
936 }
Chris Mason8ef97622007-03-26 10:15:30 -0400937 wret = clear_radix_bit(pending_radix, gang[i]);
938 BUG_ON(wret);
Chris Masond5719762007-03-23 10:01:08 -0400939 wret = __free_extent(trans, extent_root,
Chris Masone37c9e62007-05-09 20:13:14 -0400940 gang[i], 1, 0, 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400941 if (wret)
942 err = wret;
Chris Masonfec577f2007-02-26 10:40:21 -0500943 }
944 }
Chris Masone20d96d2007-03-22 12:13:20 -0400945 return err;
Chris Masonfec577f2007-02-26 10:40:21 -0500946}
947
948/*
949 * remove an extent from the root, returns 0 on success
950 */
Chris Masone089f052007-03-16 16:20:31 -0400951int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
952 *root, u64 blocknr, u64 num_blocks, int pin)
Chris Masonfec577f2007-02-26 10:40:21 -0500953{
Chris Mason9f5fae22007-03-20 14:38:32 -0400954 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Masonfec577f2007-02-26 10:40:21 -0500955 int pending_ret;
956 int ret;
Chris Masona28ec192007-03-06 20:08:01 -0500957
958 if (root == extent_root) {
Chris Mason8ef97622007-03-26 10:15:30 -0400959 pin_down_block(root, blocknr, 1);
Chris Masona28ec192007-03-06 20:08:01 -0500960 return 0;
961 }
Chris Masone37c9e62007-05-09 20:13:14 -0400962 ret = __free_extent(trans, root, blocknr, num_blocks, pin, pin == 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400963 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
Chris Masonfec577f2007-02-26 10:40:21 -0500964 return ret ? ret : pending_ret;
965}
966
967/*
968 * walks the btree of allocated extents and find a hole of a given size.
969 * The key ins is changed to record the hole:
970 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -0400971 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -0500972 * ins->offset == number of blocks
973 * Any available blocks before search_start are skipped.
974 */
Chris Masone089f052007-03-16 16:20:31 -0400975static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason6702ed42007-08-07 16:15:09 -0400976 *orig_root, u64 num_blocks, u64 empty_size,
977 u64 search_start, u64 search_end, u64 hint_block,
Chris Masonf2654de2007-06-26 12:20:46 -0400978 struct btrfs_key *ins, u64 exclude_start,
979 u64 exclude_nr, int data)
Chris Masonfec577f2007-02-26 10:40:21 -0500980{
Chris Mason5caf2a02007-04-02 11:20:42 -0400981 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -0400982 struct btrfs_key key;
Chris Masonfec577f2007-02-26 10:40:21 -0500983 int ret;
984 u64 hole_size = 0;
985 int slot = 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400986 u64 last_block = 0;
Chris Mason037e6392007-03-07 11:50:24 -0500987 u64 test_block;
Chris Masonbe744172007-05-06 10:15:01 -0400988 u64 orig_search_start = search_start;
Chris Masonfec577f2007-02-26 10:40:21 -0500989 int start_found;
Chris Mason5f39d392007-10-15 16:14:19 -0400990 struct extent_buffer *l;
Chris Mason9f5fae22007-03-20 14:38:32 -0400991 struct btrfs_root * root = orig_root->fs_info->extent_root;
Chris Masonf2458e12007-04-25 15:52:25 -0400992 struct btrfs_fs_info *info = root->fs_info;
Chris Mason0579da42007-03-07 16:15:30 -0500993 int total_needed = num_blocks;
Chris Masone20d96d2007-03-22 12:13:20 -0400994 int level;
Chris Masonbe08c1b2007-05-03 09:06:49 -0400995 struct btrfs_block_group_cache *block_group;
Chris Masonbe744172007-05-06 10:15:01 -0400996 int full_scan = 0;
Chris Masonfbdc7622007-05-30 10:22:12 -0400997 int wrapped = 0;
Chris Masonfec577f2007-02-26 10:40:21 -0500998
Chris Mason26b80032007-08-08 20:17:12 -0400999 WARN_ON(num_blocks < 1);
Chris Masonb1a4d962007-04-04 15:27:52 -04001000 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
1001
Chris Mason5f39d392007-10-15 16:14:19 -04001002 level = btrfs_header_level(root->node);
1003
Chris Mason3e1ad542007-05-07 20:03:49 -04001004 if (search_end == (u64)-1)
Chris Mason4b52dff2007-06-26 10:06:50 -04001005 search_end = btrfs_super_total_blocks(&info->super_copy);
Chris Masonfbdc7622007-05-30 10:22:12 -04001006 if (hint_block) {
Chris Mason5276aed2007-06-11 21:33:38 -04001007 block_group = btrfs_lookup_block_group(info, hint_block);
Chris Masonbe744172007-05-06 10:15:01 -04001008 block_group = btrfs_find_block_group(root, block_group,
Chris Masonfbdc7622007-05-30 10:22:12 -04001009 hint_block, data, 1);
Chris Masonbe744172007-05-06 10:15:01 -04001010 } else {
1011 block_group = btrfs_find_block_group(root,
1012 trans->block_group, 0,
Chris Masonde428b62007-05-18 13:28:27 -04001013 data, 1);
Chris Masonbe744172007-05-06 10:15:01 -04001014 }
1015
Chris Mason6702ed42007-08-07 16:15:09 -04001016 total_needed += empty_size;
Chris Masone0115992007-06-19 16:23:05 -04001017 path = btrfs_alloc_path();
1018
Chris Masonbe744172007-05-06 10:15:01 -04001019check_failed:
Chris Mason1e2677e2007-05-29 16:52:18 -04001020 if (!block_group->data)
Chris Masone37c9e62007-05-09 20:13:14 -04001021 search_start = find_search_start(root, &block_group,
1022 search_start, total_needed);
Chris Masonfbdc7622007-05-30 10:22:12 -04001023 else if (!full_scan)
Chris Masone37c9e62007-05-09 20:13:14 -04001024 search_start = max(block_group->last_alloc, search_start);
1025
Chris Mason5caf2a02007-04-02 11:20:42 -04001026 btrfs_init_path(path);
Chris Masonfec577f2007-02-26 10:40:21 -05001027 ins->objectid = search_start;
1028 ins->offset = 0;
Chris Masonfec577f2007-02-26 10:40:21 -05001029 start_found = 0;
Chris Mason2cc58cf2007-08-27 16:49:44 -04001030 path->reada = 2;
Chris Masone37c9e62007-05-09 20:13:14 -04001031
Chris Mason5caf2a02007-04-02 11:20:42 -04001032 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
Chris Mason0f70abe2007-02-28 16:46:22 -05001033 if (ret < 0)
1034 goto error;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001035
Chris Masone37c9e62007-05-09 20:13:14 -04001036 if (path->slots[0] > 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001037 path->slots[0]--;
Chris Masone37c9e62007-05-09 20:13:14 -04001038 }
1039
Chris Mason5f39d392007-10-15 16:14:19 -04001040 l = path->nodes[0];
1041 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
1042
Chris Masone37c9e62007-05-09 20:13:14 -04001043 /*
1044 * a rare case, go back one key if we hit a block group item
1045 * instead of an extent item
1046 */
1047 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY &&
1048 key.objectid + key.offset >= search_start) {
1049 ins->objectid = key.objectid;
1050 ins->offset = key.offset - 1;
1051 btrfs_release_path(root, path);
1052 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
1053 if (ret < 0)
1054 goto error;
1055
1056 if (path->slots[0] > 0) {
1057 path->slots[0]--;
1058 }
1059 }
Chris Mason0579da42007-03-07 16:15:30 -05001060
Chris Masonfec577f2007-02-26 10:40:21 -05001061 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001062 l = path->nodes[0];
Chris Mason5caf2a02007-04-02 11:20:42 -04001063 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04001064 if (slot >= btrfs_header_nritems(l)) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001065 ret = btrfs_next_leaf(root, path);
Chris Masonfec577f2007-02-26 10:40:21 -05001066 if (ret == 0)
1067 continue;
Chris Mason0f70abe2007-02-28 16:46:22 -05001068 if (ret < 0)
1069 goto error;
Chris Masonfec577f2007-02-26 10:40:21 -05001070 if (!start_found) {
1071 ins->objectid = search_start;
Chris Mason3e1ad542007-05-07 20:03:49 -04001072 ins->offset = search_end - search_start;
Chris Masonfec577f2007-02-26 10:40:21 -05001073 start_found = 1;
1074 goto check_pending;
1075 }
1076 ins->objectid = last_block > search_start ?
1077 last_block : search_start;
Chris Mason3e1ad542007-05-07 20:03:49 -04001078 ins->offset = search_end - ins->objectid;
Chris Masonfec577f2007-02-26 10:40:21 -05001079 goto check_pending;
1080 }
Chris Masone37c9e62007-05-09 20:13:14 -04001081
Chris Mason5f39d392007-10-15 16:14:19 -04001082 btrfs_item_key_to_cpu(l, &key, slot);
Chris Masone37c9e62007-05-09 20:13:14 -04001083 if (key.objectid >= search_start && key.objectid > last_block &&
1084 start_found) {
1085 if (last_block < search_start)
1086 last_block = search_start;
1087 hole_size = key.objectid - last_block;
1088 if (hole_size >= num_blocks) {
1089 ins->objectid = last_block;
1090 ins->offset = hole_size;
1091 goto check_pending;
Chris Mason0579da42007-03-07 16:15:30 -05001092 }
Chris Masonfec577f2007-02-26 10:40:21 -05001093 }
Chris Masone37c9e62007-05-09 20:13:14 -04001094
1095 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY)
1096 goto next;
1097
Chris Mason0579da42007-03-07 16:15:30 -05001098 start_found = 1;
Chris Masone2fa7222007-03-12 16:22:34 -04001099 last_block = key.objectid + key.offset;
Chris Masonfbdc7622007-05-30 10:22:12 -04001100 if (!full_scan && last_block >= block_group->key.objectid +
Chris Masonbe744172007-05-06 10:15:01 -04001101 block_group->key.offset) {
1102 btrfs_release_path(root, path);
1103 search_start = block_group->key.objectid +
1104 block_group->key.offset * 2;
1105 goto new_group;
1106 }
Chris Mason9078a3e2007-04-26 16:46:15 -04001107next:
Chris Mason5caf2a02007-04-02 11:20:42 -04001108 path->slots[0]++;
Chris Masonde428b62007-05-18 13:28:27 -04001109 cond_resched();
Chris Masonfec577f2007-02-26 10:40:21 -05001110 }
Chris Masonfec577f2007-02-26 10:40:21 -05001111check_pending:
1112 /* we have to make sure we didn't find an extent that has already
1113 * been allocated by the map tree or the original allocation
1114 */
Chris Mason5caf2a02007-04-02 11:20:42 -04001115 btrfs_release_path(root, path);
Chris Masonfec577f2007-02-26 10:40:21 -05001116 BUG_ON(ins->objectid < search_start);
Chris Masone37c9e62007-05-09 20:13:14 -04001117
Chris Masoncf675822007-09-17 11:00:51 -04001118 if (ins->objectid + num_blocks >= search_end)
1119 goto enospc;
1120
Chris Mason037e6392007-03-07 11:50:24 -05001121 for (test_block = ins->objectid;
Chris Masonf2458e12007-04-25 15:52:25 -04001122 test_block < ins->objectid + num_blocks; test_block++) {
Chris Mason26b80032007-08-08 20:17:12 -04001123 if (test_radix_bit(&info->pinned_radix, test_block) ||
1124 test_radix_bit(&info->extent_ins_radix, test_block)) {
Chris Mason037e6392007-03-07 11:50:24 -05001125 search_start = test_block + 1;
Chris Masonbe744172007-05-06 10:15:01 -04001126 goto new_group;
Chris Masonfec577f2007-02-26 10:40:21 -05001127 }
1128 }
Chris Masonf2654de2007-06-26 12:20:46 -04001129 if (exclude_nr > 0 && (ins->objectid + num_blocks > exclude_start &&
1130 ins->objectid < exclude_start + exclude_nr)) {
1131 search_start = exclude_start + exclude_nr;
1132 goto new_group;
1133 }
Chris Masone37c9e62007-05-09 20:13:14 -04001134 if (!data) {
Chris Mason5276aed2007-06-11 21:33:38 -04001135 block_group = btrfs_lookup_block_group(info, ins->objectid);
Chris Mason26b80032007-08-08 20:17:12 -04001136 if (block_group)
1137 trans->block_group = block_group;
Chris Masoncd1bc462007-04-27 10:08:34 -04001138 }
Chris Mason037e6392007-03-07 11:50:24 -05001139 ins->offset = num_blocks;
Chris Mason5caf2a02007-04-02 11:20:42 -04001140 btrfs_free_path(path);
Chris Masonfec577f2007-02-26 10:40:21 -05001141 return 0;
Chris Masonbe744172007-05-06 10:15:01 -04001142
1143new_group:
Chris Mason3e1ad542007-05-07 20:03:49 -04001144 if (search_start + num_blocks >= search_end) {
Chris Masoncf675822007-09-17 11:00:51 -04001145enospc:
Chris Masonbe744172007-05-06 10:15:01 -04001146 search_start = orig_search_start;
Chris Masonfbdc7622007-05-30 10:22:12 -04001147 if (full_scan) {
1148 ret = -ENOSPC;
1149 goto error;
1150 }
Chris Mason6702ed42007-08-07 16:15:09 -04001151 if (wrapped) {
1152 if (!full_scan)
1153 total_needed -= empty_size;
Chris Masonfbdc7622007-05-30 10:22:12 -04001154 full_scan = 1;
Chris Mason6702ed42007-08-07 16:15:09 -04001155 } else
Chris Masonfbdc7622007-05-30 10:22:12 -04001156 wrapped = 1;
Chris Masonbe744172007-05-06 10:15:01 -04001157 }
Chris Mason5276aed2007-06-11 21:33:38 -04001158 block_group = btrfs_lookup_block_group(info, search_start);
Chris Masonfbdc7622007-05-30 10:22:12 -04001159 cond_resched();
Chris Masonbe744172007-05-06 10:15:01 -04001160 if (!full_scan)
1161 block_group = btrfs_find_block_group(root, block_group,
Chris Masonde428b62007-05-18 13:28:27 -04001162 search_start, data, 0);
Chris Masonbe744172007-05-06 10:15:01 -04001163 goto check_failed;
1164
Chris Mason0f70abe2007-02-28 16:46:22 -05001165error:
Chris Mason5caf2a02007-04-02 11:20:42 -04001166 btrfs_release_path(root, path);
1167 btrfs_free_path(path);
Chris Mason0f70abe2007-02-28 16:46:22 -05001168 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05001169}
Chris Masonfec577f2007-02-26 10:40:21 -05001170/*
Chris Masonfec577f2007-02-26 10:40:21 -05001171 * finds a free extent and does all the dirty work required for allocation
1172 * returns the key for the extent through ins, and a tree buffer for
1173 * the first block of the extent through buf.
1174 *
1175 * returns 0 if everything worked, non-zero otherwise.
1176 */
Chris Mason4d775672007-04-20 20:23:12 -04001177int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
1178 struct btrfs_root *root, u64 owner,
Chris Mason6702ed42007-08-07 16:15:09 -04001179 u64 num_blocks, u64 empty_size, u64 hint_block,
Chris Masonbe08c1b2007-05-03 09:06:49 -04001180 u64 search_end, struct btrfs_key *ins, int data)
Chris Masonfec577f2007-02-26 10:40:21 -05001181{
1182 int ret;
1183 int pending_ret;
Josef Bacik58176a92007-08-29 15:47:34 -04001184 u64 super_blocks_used, root_blocks_used;
Chris Masonfbdc7622007-05-30 10:22:12 -04001185 u64 search_start = 0;
Chris Mason1261ec42007-03-20 20:35:03 -04001186 struct btrfs_fs_info *info = root->fs_info;
1187 struct btrfs_root *extent_root = info->extent_root;
Chris Mason234b63a2007-03-13 10:46:10 -04001188 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -05001189
Chris Mason5f39d392007-10-15 16:14:19 -04001190 btrfs_set_stack_extent_refs(&extent_item, 1);
1191 btrfs_set_stack_extent_owner(&extent_item, owner);
Chris Masonfec577f2007-02-26 10:40:21 -05001192
Chris Mason26b80032007-08-08 20:17:12 -04001193 WARN_ON(num_blocks < 1);
Chris Mason6702ed42007-08-07 16:15:09 -04001194 ret = find_free_extent(trans, root, num_blocks, empty_size,
1195 search_start, search_end, hint_block, ins,
Chris Mason26b80032007-08-08 20:17:12 -04001196 trans->alloc_exclude_start,
1197 trans->alloc_exclude_nr, data);
Chris Masonccd467d2007-06-28 15:57:36 -04001198 BUG_ON(ret);
Chris Masonf2654de2007-06-26 12:20:46 -04001199 if (ret)
1200 return ret;
1201
Josef Bacik58176a92007-08-29 15:47:34 -04001202 /* block accounting for super block */
Chris Mason4b52dff2007-06-26 10:06:50 -04001203 super_blocks_used = btrfs_super_blocks_used(&info->super_copy);
1204 btrfs_set_super_blocks_used(&info->super_copy, super_blocks_used +
Chris Mason1261ec42007-03-20 20:35:03 -04001205 num_blocks);
Chris Mason26b80032007-08-08 20:17:12 -04001206
Josef Bacik58176a92007-08-29 15:47:34 -04001207 /* block accounting for root item */
Chris Mason5f39d392007-10-15 16:14:19 -04001208 root_blocks_used = btrfs_root_used(&root->root_item);
1209 btrfs_set_root_used(&root->root_item, root_blocks_used +
Josef Bacik58176a92007-08-29 15:47:34 -04001210 num_blocks);
1211
Chris Mason26b80032007-08-08 20:17:12 -04001212 if (root == extent_root) {
1213 BUG_ON(num_blocks != 1);
1214 set_radix_bit(&root->fs_info->extent_ins_radix, ins->objectid);
1215 goto update_block;
1216 }
1217
1218 WARN_ON(trans->alloc_exclude_nr);
1219 trans->alloc_exclude_start = ins->objectid;
1220 trans->alloc_exclude_nr = ins->offset;
Chris Masone089f052007-03-16 16:20:31 -04001221 ret = btrfs_insert_item(trans, extent_root, ins, &extent_item,
1222 sizeof(extent_item));
Chris Mason037e6392007-03-07 11:50:24 -05001223
Chris Mason26b80032007-08-08 20:17:12 -04001224 trans->alloc_exclude_start = 0;
1225 trans->alloc_exclude_nr = 0;
1226
Chris Masonccd467d2007-06-28 15:57:36 -04001227 BUG_ON(ret);
Chris Masone089f052007-03-16 16:20:31 -04001228 finish_current_insert(trans, extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -04001229 pending_ret = del_pending_extents(trans, extent_root);
Chris Masone37c9e62007-05-09 20:13:14 -04001230 if (ret) {
Chris Mason037e6392007-03-07 11:50:24 -05001231 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -04001232 }
1233 if (pending_ret) {
Chris Mason037e6392007-03-07 11:50:24 -05001234 return pending_ret;
Chris Masone37c9e62007-05-09 20:13:14 -04001235 }
Chris Mason26b80032007-08-08 20:17:12 -04001236
1237update_block:
Chris Mason1e2677e2007-05-29 16:52:18 -04001238 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0,
1239 data);
Chris Masonfabb5682007-06-07 22:13:21 -04001240 BUG_ON(ret);
Chris Mason037e6392007-03-07 11:50:24 -05001241 return 0;
Chris Masonfec577f2007-02-26 10:40:21 -05001242}
1243
1244/*
1245 * helper function to allocate a block for a given tree
1246 * returns the tree buffer or NULL.
1247 */
Chris Mason5f39d392007-10-15 16:14:19 -04001248struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
1249 struct btrfs_root *root, u64 hint,
1250 u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05001251{
Chris Masone2fa7222007-03-12 16:22:34 -04001252 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -05001253 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04001254 struct extent_buffer *buf;
Chris Masonfec577f2007-02-26 10:40:21 -05001255
Chris Mason4d775672007-04-20 20:23:12 -04001256 ret = btrfs_alloc_extent(trans, root, root->root_key.objectid,
Yane9fe3952007-08-29 09:11:44 -04001257 1, empty_size, hint, (u64)-1, &ins, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05001258 if (ret) {
Chris Mason54aa1f42007-06-22 14:16:25 -04001259 BUG_ON(ret > 0);
1260 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05001261 }
Chris Masond98237b2007-03-28 13:57:48 -04001262 buf = btrfs_find_create_tree_block(root, ins.objectid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001263 if (!buf) {
1264 btrfs_free_extent(trans, root, ins.objectid, 1, 0);
1265 return ERR_PTR(-ENOMEM);
1266 }
Chris Mason5f39d392007-10-15 16:14:19 -04001267 btrfs_set_buffer_uptodate(buf);
1268 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
1269 buf->start + buf->len - 1, GFP_NOFS);
1270 /*
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 Mason5f39d392007-10-15 16:14:19 -04001273 */
1274 /* FIXME!!!!!!!!!!!!!!!!
1275 set_radix_bit(&trans->transaction->dirty_pages, buf->pages[0]->index);
1276 */
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001277 trans->blocks_used++;
Chris Masonfec577f2007-02-26 10:40:21 -05001278 return buf;
1279}
Chris Masona28ec192007-03-06 20:08:01 -05001280
Chris Mason6407bf62007-03-27 06:33:00 -04001281static int drop_leaf_ref(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001282 struct btrfs_root *root, struct extent_buffer *leaf)
Chris Mason6407bf62007-03-27 06:33:00 -04001283{
Chris Mason5f39d392007-10-15 16:14:19 -04001284 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -04001285 struct btrfs_file_extent_item *fi;
1286 int i;
1287 int nritems;
1288 int ret;
1289
Chris Mason5f39d392007-10-15 16:14:19 -04001290 BUG_ON(!btrfs_is_leaf(leaf));
1291 nritems = btrfs_header_nritems(leaf);
Chris Mason6407bf62007-03-27 06:33:00 -04001292 for (i = 0; i < nritems; i++) {
Chris Mason3a686372007-05-24 13:35:57 -04001293 u64 disk_blocknr;
Chris Mason5f39d392007-10-15 16:14:19 -04001294
1295 btrfs_item_key_to_cpu(leaf, &key, i);
1296 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason6407bf62007-03-27 06:33:00 -04001297 continue;
1298 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001299 if (btrfs_file_extent_type(leaf, fi) ==
1300 BTRFS_FILE_EXTENT_INLINE)
Chris Mason236454d2007-04-19 13:37:44 -04001301 continue;
Chris Mason6407bf62007-03-27 06:33:00 -04001302 /*
1303 * FIXME make sure to insert a trans record that
1304 * repeats the snapshot del on crash
1305 */
Chris Mason5f39d392007-10-15 16:14:19 -04001306 disk_blocknr = btrfs_file_extent_disk_blocknr(leaf, fi);
Chris Mason3a686372007-05-24 13:35:57 -04001307 if (disk_blocknr == 0)
1308 continue;
1309 ret = btrfs_free_extent(trans, root, disk_blocknr,
Chris Mason5f39d392007-10-15 16:14:19 -04001310 btrfs_file_extent_disk_num_blocks(leaf, fi), 0);
Chris Mason6407bf62007-03-27 06:33:00 -04001311 BUG_ON(ret);
1312 }
1313 return 0;
1314}
1315
Chris Masone0115992007-06-19 16:23:05 -04001316static void reada_walk_down(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -04001317 struct extent_buffer *node)
Chris Masone0115992007-06-19 16:23:05 -04001318{
1319 int i;
1320 u32 nritems;
1321 u64 blocknr;
1322 int ret;
1323 u32 refs;
1324
Chris Mason5f39d392007-10-15 16:14:19 -04001325 nritems = btrfs_header_nritems(node);
Chris Masone0115992007-06-19 16:23:05 -04001326 for (i = 0; i < nritems; i++) {
1327 blocknr = btrfs_node_blockptr(node, i);
1328 ret = lookup_extent_ref(NULL, root, blocknr, 1, &refs);
1329 BUG_ON(ret);
1330 if (refs != 1)
1331 continue;
Chris Mason409eb952007-08-08 20:17:12 -04001332 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masone0115992007-06-19 16:23:05 -04001333 ret = readahead_tree_block(root, blocknr);
Chris Mason409eb952007-08-08 20:17:12 -04001334 cond_resched();
1335 mutex_lock(&root->fs_info->fs_mutex);
Chris Masone0115992007-06-19 16:23:05 -04001336 if (ret)
1337 break;
1338 }
1339}
1340
Chris Mason9aca1d52007-03-13 11:09:37 -04001341/*
1342 * helper function for drop_snapshot, this walks down the tree dropping ref
1343 * counts as it goes.
1344 */
Chris Masone089f052007-03-16 16:20:31 -04001345static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
1346 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05001347{
Chris Mason5f39d392007-10-15 16:14:19 -04001348 struct extent_buffer *next;
1349 struct extent_buffer *cur;
Chris Mason20524f02007-03-10 06:35:47 -05001350 u64 blocknr;
1351 int ret;
1352 u32 refs;
1353
Chris Mason5caf2a02007-04-02 11:20:42 -04001354 WARN_ON(*level < 0);
1355 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason5f39d392007-10-15 16:14:19 -04001356 ret = lookup_extent_ref(trans, root,
1357 extent_buffer_blocknr(path->nodes[*level]),
1358 1, &refs);
Chris Mason20524f02007-03-10 06:35:47 -05001359 BUG_ON(ret);
1360 if (refs > 1)
1361 goto out;
Chris Masone0115992007-06-19 16:23:05 -04001362
Chris Mason9aca1d52007-03-13 11:09:37 -04001363 /*
1364 * walk down to the last node level and free all the leaves
1365 */
Chris Mason6407bf62007-03-27 06:33:00 -04001366 while(*level >= 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001367 WARN_ON(*level < 0);
1368 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason20524f02007-03-10 06:35:47 -05001369 cur = path->nodes[*level];
Chris Masone0115992007-06-19 16:23:05 -04001370
1371 if (*level > 0 && path->slots[*level] == 0)
Chris Mason5f39d392007-10-15 16:14:19 -04001372 reada_walk_down(root, cur);
Chris Masone0115992007-06-19 16:23:05 -04001373
Chris Mason5f39d392007-10-15 16:14:19 -04001374 if (btrfs_header_level(cur) != *level)
Chris Mason2c90e5d2007-04-02 10:50:19 -04001375 WARN_ON(1);
Chris Masone0115992007-06-19 16:23:05 -04001376
Chris Mason7518a232007-03-12 12:01:18 -04001377 if (path->slots[*level] >=
Chris Mason5f39d392007-10-15 16:14:19 -04001378 btrfs_header_nritems(cur))
Chris Mason20524f02007-03-10 06:35:47 -05001379 break;
Chris Mason6407bf62007-03-27 06:33:00 -04001380 if (*level == 0) {
1381 ret = drop_leaf_ref(trans, root, cur);
1382 BUG_ON(ret);
1383 break;
1384 }
Chris Mason5f39d392007-10-15 16:14:19 -04001385 blocknr = btrfs_node_blockptr(cur, path->slots[*level]);
Chris Masonb18c6682007-04-17 13:26:50 -04001386 ret = lookup_extent_ref(trans, root, blocknr, 1, &refs);
Chris Mason6407bf62007-03-27 06:33:00 -04001387 BUG_ON(ret);
1388 if (refs != 1) {
Chris Mason20524f02007-03-10 06:35:47 -05001389 path->slots[*level]++;
Chris Masone089f052007-03-16 16:20:31 -04001390 ret = btrfs_free_extent(trans, root, blocknr, 1, 1);
Chris Mason20524f02007-03-10 06:35:47 -05001391 BUG_ON(ret);
1392 continue;
1393 }
Chris Masone9d0b132007-08-10 14:06:19 -04001394 next = btrfs_find_tree_block(root, blocknr);
Chris Mason5f39d392007-10-15 16:14:19 -04001395 if (!next || !btrfs_buffer_uptodate(next)) {
1396 free_extent_buffer(next);
Chris Masone9d0b132007-08-10 14:06:19 -04001397 mutex_unlock(&root->fs_info->fs_mutex);
1398 next = read_tree_block(root, blocknr);
1399 mutex_lock(&root->fs_info->fs_mutex);
1400
1401 /* we dropped the lock, check one more time */
1402 ret = lookup_extent_ref(trans, root, blocknr, 1, &refs);
1403 BUG_ON(ret);
1404 if (refs != 1) {
1405 path->slots[*level]++;
Chris Mason5f39d392007-10-15 16:14:19 -04001406 free_extent_buffer(next);
Chris Masone9d0b132007-08-10 14:06:19 -04001407 ret = btrfs_free_extent(trans, root,
1408 blocknr, 1, 1);
1409 BUG_ON(ret);
1410 continue;
1411 }
1412 }
Chris Mason5caf2a02007-04-02 11:20:42 -04001413 WARN_ON(*level <= 0);
Chris Mason83e15a22007-03-12 09:03:27 -04001414 if (path->nodes[*level-1])
Chris Mason5f39d392007-10-15 16:14:19 -04001415 free_extent_buffer(path->nodes[*level-1]);
Chris Mason20524f02007-03-10 06:35:47 -05001416 path->nodes[*level-1] = next;
Chris Mason5f39d392007-10-15 16:14:19 -04001417 *level = btrfs_header_level(next);
Chris Mason20524f02007-03-10 06:35:47 -05001418 path->slots[*level] = 0;
1419 }
1420out:
Chris Mason5caf2a02007-04-02 11:20:42 -04001421 WARN_ON(*level < 0);
1422 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason6407bf62007-03-27 06:33:00 -04001423 ret = btrfs_free_extent(trans, root,
Chris Mason5f39d392007-10-15 16:14:19 -04001424 extent_buffer_blocknr(path->nodes[*level]), 1, 1);
1425 free_extent_buffer(path->nodes[*level]);
Chris Mason20524f02007-03-10 06:35:47 -05001426 path->nodes[*level] = NULL;
1427 *level += 1;
1428 BUG_ON(ret);
1429 return 0;
1430}
1431
Chris Mason9aca1d52007-03-13 11:09:37 -04001432/*
1433 * helper for dropping snapshots. This walks back up the tree in the path
1434 * to find the first node higher up where we haven't yet gone through
1435 * all the slots
1436 */
Chris Masone089f052007-03-16 16:20:31 -04001437static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root
1438 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05001439{
1440 int i;
1441 int slot;
1442 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04001443 struct btrfs_root_item *root_item = &root->root_item;
1444
Chris Mason234b63a2007-03-13 10:46:10 -04001445 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Mason20524f02007-03-10 06:35:47 -05001446 slot = path->slots[i];
Chris Mason5f39d392007-10-15 16:14:19 -04001447 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
1448 struct extent_buffer *node;
1449 struct btrfs_disk_key disk_key;
1450 node = path->nodes[i];
Chris Mason20524f02007-03-10 06:35:47 -05001451 path->slots[i]++;
1452 *level = i;
Chris Mason9f3a7422007-08-07 15:52:19 -04001453 WARN_ON(*level == 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001454 btrfs_node_key(node, &disk_key, path->slots[i]);
Chris Mason9f3a7422007-08-07 15:52:19 -04001455 memcpy(&root_item->drop_progress,
Chris Mason5f39d392007-10-15 16:14:19 -04001456 &disk_key, sizeof(disk_key));
Chris Mason9f3a7422007-08-07 15:52:19 -04001457 root_item->drop_level = i;
Chris Mason20524f02007-03-10 06:35:47 -05001458 return 0;
1459 } else {
Chris Masone089f052007-03-16 16:20:31 -04001460 ret = btrfs_free_extent(trans, root,
Chris Mason5f39d392007-10-15 16:14:19 -04001461 extent_buffer_blocknr(path->nodes[*level]),
1462 1, 1);
Chris Mason6407bf62007-03-27 06:33:00 -04001463 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04001464 free_extent_buffer(path->nodes[*level]);
Chris Mason83e15a22007-03-12 09:03:27 -04001465 path->nodes[*level] = NULL;
Chris Mason20524f02007-03-10 06:35:47 -05001466 *level = i + 1;
Chris Mason20524f02007-03-10 06:35:47 -05001467 }
1468 }
1469 return 1;
1470}
1471
Chris Mason9aca1d52007-03-13 11:09:37 -04001472/*
1473 * drop the reference count on the tree rooted at 'snap'. This traverses
1474 * the tree freeing any blocks that have a ref count of zero after being
1475 * decremented.
1476 */
Chris Masone089f052007-03-16 16:20:31 -04001477int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason9f3a7422007-08-07 15:52:19 -04001478 *root)
Chris Mason20524f02007-03-10 06:35:47 -05001479{
Chris Mason3768f362007-03-13 16:47:54 -04001480 int ret = 0;
Chris Mason9aca1d52007-03-13 11:09:37 -04001481 int wret;
Chris Mason20524f02007-03-10 06:35:47 -05001482 int level;
Chris Mason5caf2a02007-04-02 11:20:42 -04001483 struct btrfs_path *path;
Chris Mason20524f02007-03-10 06:35:47 -05001484 int i;
1485 int orig_level;
Chris Mason9f3a7422007-08-07 15:52:19 -04001486 struct btrfs_root_item *root_item = &root->root_item;
Chris Mason20524f02007-03-10 06:35:47 -05001487
Chris Mason5caf2a02007-04-02 11:20:42 -04001488 path = btrfs_alloc_path();
1489 BUG_ON(!path);
Chris Mason20524f02007-03-10 06:35:47 -05001490
Chris Mason5f39d392007-10-15 16:14:19 -04001491 level = btrfs_header_level(root->node);
Chris Mason20524f02007-03-10 06:35:47 -05001492 orig_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04001493 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
1494 path->nodes[level] = root->node;
1495 path->slots[level] = 0;
1496 } else {
1497 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001498 struct btrfs_disk_key found_key;
1499 struct extent_buffer *node;
Chris Mason6702ed42007-08-07 16:15:09 -04001500
Chris Mason9f3a7422007-08-07 15:52:19 -04001501 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Chris Mason6702ed42007-08-07 16:15:09 -04001502 level = root_item->drop_level;
1503 path->lowest_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04001504 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason6702ed42007-08-07 16:15:09 -04001505 if (wret < 0) {
Chris Mason9f3a7422007-08-07 15:52:19 -04001506 ret = wret;
1507 goto out;
1508 }
Chris Mason5f39d392007-10-15 16:14:19 -04001509 node = path->nodes[level];
1510 btrfs_node_key(node, &found_key, path->slots[level]);
1511 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
1512 sizeof(found_key)));
Chris Mason9f3a7422007-08-07 15:52:19 -04001513 }
Chris Mason20524f02007-03-10 06:35:47 -05001514 while(1) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001515 wret = walk_down_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04001516 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05001517 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04001518 if (wret < 0)
1519 ret = wret;
1520
Chris Mason5caf2a02007-04-02 11:20:42 -04001521 wret = walk_up_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04001522 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05001523 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04001524 if (wret < 0)
1525 ret = wret;
Chris Mason409eb952007-08-08 20:17:12 -04001526 ret = -EAGAIN;
Chris Mason5f39d392007-10-15 16:14:19 -04001527 extent_buffer_get(root->node);
Chris Mason409eb952007-08-08 20:17:12 -04001528 break;
Chris Mason20524f02007-03-10 06:35:47 -05001529 }
Chris Mason83e15a22007-03-12 09:03:27 -04001530 for (i = 0; i <= orig_level; i++) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001531 if (path->nodes[i]) {
Chris Mason5f39d392007-10-15 16:14:19 -04001532 free_extent_buffer(path->nodes[i]);
Chris Mason6702ed42007-08-07 16:15:09 -04001533 path->nodes[i] = 0;
Chris Mason83e15a22007-03-12 09:03:27 -04001534 }
Chris Mason20524f02007-03-10 06:35:47 -05001535 }
Chris Mason9f3a7422007-08-07 15:52:19 -04001536out:
Chris Mason5caf2a02007-04-02 11:20:42 -04001537 btrfs_free_path(path);
Chris Mason9aca1d52007-03-13 11:09:37 -04001538 return ret;
Chris Mason20524f02007-03-10 06:35:47 -05001539}
Chris Mason9078a3e2007-04-26 16:46:15 -04001540
Chris Masonbe744172007-05-06 10:15:01 -04001541static int free_block_group_radix(struct radix_tree_root *radix)
Chris Mason9078a3e2007-04-26 16:46:15 -04001542{
1543 int ret;
1544 struct btrfs_block_group_cache *cache[8];
1545 int i;
1546
1547 while(1) {
Chris Masonbe744172007-05-06 10:15:01 -04001548 ret = radix_tree_gang_lookup(radix, (void **)cache, 0,
Chris Mason9078a3e2007-04-26 16:46:15 -04001549 ARRAY_SIZE(cache));
1550 if (!ret)
1551 break;
1552 for (i = 0; i < ret; i++) {
Chris Masonbe744172007-05-06 10:15:01 -04001553 radix_tree_delete(radix, cache[i]->key.objectid +
Chris Mason9078a3e2007-04-26 16:46:15 -04001554 cache[i]->key.offset - 1);
1555 kfree(cache[i]);
1556 }
1557 }
1558 return 0;
1559}
1560
Chris Masonbe744172007-05-06 10:15:01 -04001561int btrfs_free_block_groups(struct btrfs_fs_info *info)
1562{
1563 int ret;
1564 int ret2;
Chris Masone37c9e62007-05-09 20:13:14 -04001565 unsigned long gang[16];
1566 int i;
Chris Masonbe744172007-05-06 10:15:01 -04001567
1568 ret = free_block_group_radix(&info->block_group_radix);
1569 ret2 = free_block_group_radix(&info->block_group_data_radix);
1570 if (ret)
1571 return ret;
1572 if (ret2)
1573 return ret2;
Chris Masone37c9e62007-05-09 20:13:14 -04001574
1575 while(1) {
1576 ret = find_first_radix_bit(&info->extent_map_radix,
1577 gang, 0, ARRAY_SIZE(gang));
1578 if (!ret)
1579 break;
1580 for (i = 0; i < ret; i++) {
1581 clear_radix_bit(&info->extent_map_radix, gang[i]);
1582 }
1583 }
Chris Masonbe744172007-05-06 10:15:01 -04001584 return 0;
1585}
1586
Chris Mason9078a3e2007-04-26 16:46:15 -04001587int btrfs_read_block_groups(struct btrfs_root *root)
1588{
1589 struct btrfs_path *path;
1590 int ret;
1591 int err = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04001592 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04001593 struct btrfs_fs_info *info = root->fs_info;
1594 struct radix_tree_root *radix;
Chris Mason9078a3e2007-04-26 16:46:15 -04001595 struct btrfs_key key;
1596 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -04001597 struct extent_buffer *leaf;
Chris Mason84f54cf2007-06-12 07:43:08 -04001598 u64 group_size_blocks;
Chris Mason31f3c992007-04-30 15:25:45 -04001599 u64 used;
Chris Mason9078a3e2007-04-26 16:46:15 -04001600
Chris Mason84f54cf2007-06-12 07:43:08 -04001601 group_size_blocks = BTRFS_BLOCK_GROUP_SIZE >>
1602 root->fs_info->sb->s_blocksize_bits;
Chris Masonbe744172007-05-06 10:15:01 -04001603 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04001604 key.objectid = 0;
1605 key.offset = group_size_blocks;
Chris Mason9078a3e2007-04-26 16:46:15 -04001606 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
1607
1608 path = btrfs_alloc_path();
1609 if (!path)
1610 return -ENOMEM;
1611
1612 while(1) {
Chris Masonbe744172007-05-06 10:15:01 -04001613 ret = btrfs_search_slot(NULL, info->extent_root,
Chris Mason9078a3e2007-04-26 16:46:15 -04001614 &key, path, 0, 0);
1615 if (ret != 0) {
1616 err = ret;
1617 break;
1618 }
Chris Mason5f39d392007-10-15 16:14:19 -04001619 leaf = path->nodes[0];
1620 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason9078a3e2007-04-26 16:46:15 -04001621 cache = kmalloc(sizeof(*cache), GFP_NOFS);
1622 if (!cache) {
1623 err = -1;
1624 break;
1625 }
Chris Mason3e1ad542007-05-07 20:03:49 -04001626
Chris Mason5f39d392007-10-15 16:14:19 -04001627 read_extent_buffer(leaf, &cache->item,
1628 btrfs_item_ptr_offset(leaf, path->slots[0]),
1629 sizeof(cache->item));
1630 if (cache->item.flags & BTRFS_BLOCK_GROUP_DATA) {
Chris Mason1e2677e2007-05-29 16:52:18 -04001631 radix = &info->block_group_data_radix;
1632 cache->data = 1;
1633 } else {
1634 radix = &info->block_group_radix;
1635 cache->data = 0;
1636 }
1637
Chris Mason9078a3e2007-04-26 16:46:15 -04001638 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 Mason5f39d392007-10-15 16:14:19 -04001648
Chris Masonbe744172007-05-06 10:15:01 -04001649 ret = radix_tree_insert(radix, found_key.objectid +
Chris Mason9078a3e2007-04-26 16:46:15 -04001650 found_key.offset - 1,
1651 (void *)cache);
1652 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04001653 used = btrfs_block_group_used(&cache->item);
Chris Mason84f54cf2007-06-12 07:43:08 -04001654 if (used < div_factor(key.offset, 8)) {
Chris Masonbe744172007-05-06 10:15:01 -04001655 radix_tree_tag_set(radix, found_key.objectid +
Chris Mason31f3c992007-04-30 15:25:45 -04001656 found_key.offset - 1,
1657 BTRFS_BLOCK_GROUP_AVAIL);
1658 }
Chris Mason9078a3e2007-04-26 16:46:15 -04001659 if (key.objectid >=
Chris Mason4b52dff2007-06-26 10:06:50 -04001660 btrfs_super_total_blocks(&info->super_copy))
Chris Mason9078a3e2007-04-26 16:46:15 -04001661 break;
1662 }
1663
1664 btrfs_free_path(path);
1665 return 0;
1666}