blob: 74cfbee2ff336648a621fe00017f343846fd0445 [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 Masonf510cfe2007-10-15 16:14:48 -040037 struct extent_map_tree *free_space_cache;
Chris Masone37c9e62007-05-09 20:13:14 -040038 int slot;
Chris Masone37c9e62007-05-09 20:13:14 -040039 u64 last = 0;
40 u64 hole_size;
Yan7d7d6062007-09-14 16:15:28 -040041 u64 first_free;
Chris Masone37c9e62007-05-09 20:13:14 -040042 int found = 0;
43
44 root = root->fs_info->extent_root;
Chris Masonf510cfe2007-10-15 16:14:48 -040045 free_space_cache = &root->fs_info->free_space_cache;
Chris Masone37c9e62007-05-09 20:13:14 -040046
47 if (block_group->cached)
48 return 0;
Chris Masonf510cfe2007-10-15 16:14:48 -040049
Chris Masone37c9e62007-05-09 20:13:14 -040050 path = btrfs_alloc_path();
51 if (!path)
52 return -ENOMEM;
Yan7d7d6062007-09-14 16:15:28 -040053
Chris Mason2cc58cf2007-08-27 16:49:44 -040054 path->reada = 2;
Yan7d7d6062007-09-14 16:15:28 -040055 first_free = block_group->key.objectid;
Chris Masone37c9e62007-05-09 20:13:14 -040056 key.objectid = block_group->key.objectid;
Chris Masone37c9e62007-05-09 20:13:14 -040057 key.offset = 0;
Yan7d7d6062007-09-14 16:15:28 -040058
Chris Masone37c9e62007-05-09 20:13:14 -040059 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
60 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Yan7d7d6062007-09-14 16:15:28 -040061
Chris Masone37c9e62007-05-09 20:13:14 -040062 if (ret < 0)
63 return ret;
Yan7d7d6062007-09-14 16:15:28 -040064
Chris Masone37c9e62007-05-09 20:13:14 -040065 if (ret && path->slots[0] > 0)
66 path->slots[0]--;
Yan7d7d6062007-09-14 16:15:28 -040067
Chris Masone37c9e62007-05-09 20:13:14 -040068 while(1) {
Chris Mason5f39d392007-10-15 16:14:19 -040069 leaf = path->nodes[0];
Chris Masone37c9e62007-05-09 20:13:14 -040070 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -040071 if (slot >= btrfs_header_nritems(leaf)) {
Chris Masone37c9e62007-05-09 20:13:14 -040072 ret = btrfs_next_leaf(root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -040073 if (ret < 0)
74 goto err;
Chris Masonde428b62007-05-18 13:28:27 -040075 if (ret == 0) {
Chris Masone37c9e62007-05-09 20:13:14 -040076 continue;
Chris Masonde428b62007-05-18 13:28:27 -040077 } else {
Chris Masone37c9e62007-05-09 20:13:14 -040078 break;
79 }
80 }
Yan7d7d6062007-09-14 16:15:28 -040081
Chris Mason5f39d392007-10-15 16:14:19 -040082 btrfs_item_key_to_cpu(leaf, &key, slot);
Yan7d7d6062007-09-14 16:15:28 -040083 if (key.objectid < block_group->key.objectid) {
84 if (key.objectid + key.offset > first_free)
85 first_free = key.objectid + key.offset;
86 goto next;
87 }
88
Chris Masone37c9e62007-05-09 20:13:14 -040089 if (key.objectid >= block_group->key.objectid +
90 block_group->key.offset) {
Yan7d7d6062007-09-14 16:15:28 -040091 break;
92 }
93
94 if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) {
95 if (!found) {
96 last = first_free;
97 found = 1;
Chris Masone37c9e62007-05-09 20:13:14 -040098 }
Chris Masonf510cfe2007-10-15 16:14:48 -040099 if (key.objectid > last) {
100 hole_size = key.objectid - last;
101 set_extent_dirty(free_space_cache, last,
102 last + hole_size - 1,
103 GFP_NOFS);
Chris Masone37c9e62007-05-09 20:13:14 -0400104 }
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;
Chris Masonf510cfe2007-10-15 16:14:48 -0400117 set_extent_dirty(free_space_cache, last,
118 last + hole_size - 1, GFP_NOFS);
Yan7d7d6062007-09-14 16:15:28 -0400119 }
Chris Masone37c9e62007-05-09 20:13:14 -0400120 block_group->cached = 1;
Chris Mason54aa1f42007-06-22 14:16:25 -0400121err:
Chris Masone37c9e62007-05-09 20:13:14 -0400122 btrfs_free_path(path);
123 return 0;
124}
125
Chris Mason5276aed2007-06-11 21:33:38 -0400126struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
127 btrfs_fs_info *info,
128 u64 blocknr)
Chris Masonbe744172007-05-06 10:15:01 -0400129{
130 struct btrfs_block_group_cache *block_group;
131 int ret;
132
133 ret = radix_tree_gang_lookup(&info->block_group_radix,
134 (void **)&block_group,
135 blocknr, 1);
136 if (ret) {
Chris Mason3e1ad542007-05-07 20:03:49 -0400137 if (block_group->key.objectid <= blocknr && blocknr <=
Chris Masonbe744172007-05-06 10:15:01 -0400138 block_group->key.objectid + block_group->key.offset)
139 return block_group;
140 }
141 ret = radix_tree_gang_lookup(&info->block_group_data_radix,
142 (void **)&block_group,
143 blocknr, 1);
144 if (ret) {
Chris Mason3e1ad542007-05-07 20:03:49 -0400145 if (block_group->key.objectid <= blocknr && blocknr <=
Chris Masonbe744172007-05-06 10:15:01 -0400146 block_group->key.objectid + block_group->key.offset)
147 return block_group;
148 }
Chris Masonbe744172007-05-06 10:15:01 -0400149 return NULL;
150}
151
Chris Masone37c9e62007-05-09 20:13:14 -0400152static u64 find_search_start(struct btrfs_root *root,
153 struct btrfs_block_group_cache **cache_ret,
Chris Masonf510cfe2007-10-15 16:14:48 -0400154 u64 search_start, int num, int data)
Chris Masone37c9e62007-05-09 20:13:14 -0400155{
Chris Masone37c9e62007-05-09 20:13:14 -0400156 int ret;
157 struct btrfs_block_group_cache *cache = *cache_ret;
158 u64 last = max(search_start, cache->key.objectid);
Chris Masonf510cfe2007-10-15 16:14:48 -0400159 u64 start = 0;
160 u64 end = 0;
Chris Masone37c9e62007-05-09 20:13:14 -0400161
Chris Masone37c9e62007-05-09 20:13:14 -0400162again:
Chris Mason54aa1f42007-06-22 14:16:25 -0400163 ret = cache_block_group(root, cache);
164 if (ret)
165 goto out;
Chris Masone37c9e62007-05-09 20:13:14 -0400166 while(1) {
Chris Masonf510cfe2007-10-15 16:14:48 -0400167 ret = find_first_extent_bit(&root->fs_info->free_space_cache,
168 last, &start, &end, EXTENT_DIRTY);
169 if (ret)
Chris Masone37c9e62007-05-09 20:13:14 -0400170 goto out;
Chris Masonf510cfe2007-10-15 16:14:48 -0400171
172 start = max(last, start);
173 last = end + 1;
174 if (end + 1 - start < num)
175 continue;
176 if (start + num > cache->key.objectid + cache->key.offset)
Chris Masone37c9e62007-05-09 20:13:14 -0400177 goto new_group;
Chris Masonf510cfe2007-10-15 16:14:48 -0400178 return start;
Chris Masone37c9e62007-05-09 20:13:14 -0400179 }
180out:
181 return max(cache->last_alloc, search_start);
182
183new_group:
Chris Mason5276aed2007-06-11 21:33:38 -0400184 cache = btrfs_lookup_block_group(root->fs_info,
185 last + cache->key.offset - 1);
Chris Masone37c9e62007-05-09 20:13:14 -0400186 if (!cache) {
187 return max((*cache_ret)->last_alloc, search_start);
188 }
189 cache = btrfs_find_block_group(root, cache,
Chris Masonf510cfe2007-10-15 16:14:48 -0400190 last + cache->key.offset - 1, data, 0);
Chris Masone37c9e62007-05-09 20:13:14 -0400191 *cache_ret = cache;
192 goto again;
193}
194
Chris Mason84f54cf2007-06-12 07:43:08 -0400195static u64 div_factor(u64 num, int factor)
196{
197 num *= factor;
198 do_div(num, 10);
199 return num;
200}
201
Chris Mason31f3c992007-04-30 15:25:45 -0400202struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
203 struct btrfs_block_group_cache
Chris Masonbe744172007-05-06 10:15:01 -0400204 *hint, u64 search_start,
Chris Masonde428b62007-05-18 13:28:27 -0400205 int data, int owner)
Chris Masoncd1bc462007-04-27 10:08:34 -0400206{
207 struct btrfs_block_group_cache *cache[8];
Chris Mason31f3c992007-04-30 15:25:45 -0400208 struct btrfs_block_group_cache *found_group = NULL;
Chris Masoncd1bc462007-04-27 10:08:34 -0400209 struct btrfs_fs_info *info = root->fs_info;
Chris Masonbe744172007-05-06 10:15:01 -0400210 struct radix_tree_root *radix;
Chris Mason1e2677e2007-05-29 16:52:18 -0400211 struct radix_tree_root *swap_radix;
Chris Masoncd1bc462007-04-27 10:08:34 -0400212 u64 used;
Chris Mason31f3c992007-04-30 15:25:45 -0400213 u64 last = 0;
214 u64 hint_last;
Chris Masoncd1bc462007-04-27 10:08:34 -0400215 int i;
216 int ret;
Chris Mason31f3c992007-04-30 15:25:45 -0400217 int full_search = 0;
Chris Masonde428b62007-05-18 13:28:27 -0400218 int factor = 8;
Chris Mason1e2677e2007-05-29 16:52:18 -0400219 int data_swap = 0;
Chris Masonde428b62007-05-18 13:28:27 -0400220
221 if (!owner)
222 factor = 5;
Chris Masonbe744172007-05-06 10:15:01 -0400223
Chris Mason1e2677e2007-05-29 16:52:18 -0400224 if (data) {
Chris Masonbe744172007-05-06 10:15:01 -0400225 radix = &info->block_group_data_radix;
Chris Mason1e2677e2007-05-29 16:52:18 -0400226 swap_radix = &info->block_group_radix;
227 } else {
Chris Masonbe744172007-05-06 10:15:01 -0400228 radix = &info->block_group_radix;
Chris Mason1e2677e2007-05-29 16:52:18 -0400229 swap_radix = &info->block_group_data_radix;
230 }
Chris Masonbe744172007-05-06 10:15:01 -0400231
232 if (search_start) {
233 struct btrfs_block_group_cache *shint;
Chris Mason5276aed2007-06-11 21:33:38 -0400234 shint = btrfs_lookup_block_group(info, search_start);
Yane9fe3952007-08-29 09:11:44 -0400235 if (shint && shint->data == data) {
Chris Masonbe744172007-05-06 10:15:01 -0400236 used = btrfs_block_group_used(&shint->item);
237 if (used + shint->pinned <
Chris Mason84f54cf2007-06-12 07:43:08 -0400238 div_factor(shint->key.offset, factor)) {
Chris Masonbe744172007-05-06 10:15:01 -0400239 return shint;
240 }
241 }
242 }
243 if (hint && hint->data == data) {
Chris Mason31f3c992007-04-30 15:25:45 -0400244 used = btrfs_block_group_used(&hint->item);
Chris Mason84f54cf2007-06-12 07:43:08 -0400245 if (used + hint->pinned <
246 div_factor(hint->key.offset, factor)) {
Chris Mason31f3c992007-04-30 15:25:45 -0400247 return hint;
248 }
Chris Mason84f54cf2007-06-12 07:43:08 -0400249 if (used >= div_factor(hint->key.offset, 8)) {
Chris Masonbe744172007-05-06 10:15:01 -0400250 radix_tree_tag_clear(radix,
251 hint->key.objectid +
252 hint->key.offset - 1,
253 BTRFS_BLOCK_GROUP_AVAIL);
254 }
Chris Mason8d7be552007-05-10 11:24:42 -0400255 last = hint->key.offset * 3;
Chris Masonbe744172007-05-06 10:15:01 -0400256 if (hint->key.objectid >= last)
Chris Masone37c9e62007-05-09 20:13:14 -0400257 last = max(search_start + hint->key.offset - 1,
258 hint->key.objectid - last);
Chris Masonbe744172007-05-06 10:15:01 -0400259 else
260 last = hint->key.objectid + hint->key.offset;
Chris Mason31f3c992007-04-30 15:25:45 -0400261 hint_last = last;
262 } else {
Chris Masone37c9e62007-05-09 20:13:14 -0400263 if (hint)
264 hint_last = max(hint->key.objectid, search_start);
265 else
266 hint_last = search_start;
267
268 last = hint_last;
Chris Mason31f3c992007-04-30 15:25:45 -0400269 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400270 while(1) {
Chris Masonbe744172007-05-06 10:15:01 -0400271 ret = radix_tree_gang_lookup_tag(radix, (void **)cache,
Chris Masoncd1bc462007-04-27 10:08:34 -0400272 last, ARRAY_SIZE(cache),
Chris Mason31f3c992007-04-30 15:25:45 -0400273 BTRFS_BLOCK_GROUP_AVAIL);
Chris Masoncd1bc462007-04-27 10:08:34 -0400274 if (!ret)
275 break;
276 for (i = 0; i < ret; i++) {
Chris Masonbe08c1b2007-05-03 09:06:49 -0400277 last = cache[i]->key.objectid +
278 cache[i]->key.offset;
Chris Masoncd1bc462007-04-27 10:08:34 -0400279 used = btrfs_block_group_used(&cache[i]->item);
Chris Masonbe744172007-05-06 10:15:01 -0400280 if (used + cache[i]->pinned <
Chris Mason84f54cf2007-06-12 07:43:08 -0400281 div_factor(cache[i]->key.offset, factor)) {
Chris Mason31f3c992007-04-30 15:25:45 -0400282 found_group = cache[i];
283 goto found;
Chris Masoncd1bc462007-04-27 10:08:34 -0400284 }
Chris Mason84f54cf2007-06-12 07:43:08 -0400285 if (used >= div_factor(cache[i]->key.offset, 8)) {
Chris Masonbe744172007-05-06 10:15:01 -0400286 radix_tree_tag_clear(radix,
287 cache[i]->key.objectid +
288 cache[i]->key.offset - 1,
289 BTRFS_BLOCK_GROUP_AVAIL);
290 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400291 }
Chris Masonde428b62007-05-18 13:28:27 -0400292 cond_resched();
Chris Masoncd1bc462007-04-27 10:08:34 -0400293 }
Chris Mason31f3c992007-04-30 15:25:45 -0400294 last = hint_last;
295again:
Chris Masoncd1bc462007-04-27 10:08:34 -0400296 while(1) {
Chris Masonbe744172007-05-06 10:15:01 -0400297 ret = radix_tree_gang_lookup(radix, (void **)cache,
298 last, ARRAY_SIZE(cache));
Chris Masoncd1bc462007-04-27 10:08:34 -0400299 if (!ret)
300 break;
301 for (i = 0; i < ret; i++) {
Chris Masonbe08c1b2007-05-03 09:06:49 -0400302 last = cache[i]->key.objectid +
303 cache[i]->key.offset;
Chris Masoncd1bc462007-04-27 10:08:34 -0400304 used = btrfs_block_group_used(&cache[i]->item);
Chris Masonbe744172007-05-06 10:15:01 -0400305 if (used + cache[i]->pinned < cache[i]->key.offset) {
Chris Mason31f3c992007-04-30 15:25:45 -0400306 found_group = cache[i];
307 goto found;
Chris Masoncd1bc462007-04-27 10:08:34 -0400308 }
Chris Masonbe744172007-05-06 10:15:01 -0400309 if (used >= cache[i]->key.offset) {
310 radix_tree_tag_clear(radix,
311 cache[i]->key.objectid +
312 cache[i]->key.offset - 1,
313 BTRFS_BLOCK_GROUP_AVAIL);
314 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400315 }
Chris Masonde428b62007-05-18 13:28:27 -0400316 cond_resched();
Chris Masoncd1bc462007-04-27 10:08:34 -0400317 }
Chris Mason31f3c992007-04-30 15:25:45 -0400318 if (!full_search) {
Chris Masonbe744172007-05-06 10:15:01 -0400319 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -0400320 full_search = 1;
321 goto again;
322 }
Chris Mason1e2677e2007-05-29 16:52:18 -0400323 if (!data_swap) {
324 struct radix_tree_root *tmp = radix;
325 data_swap = 1;
326 radix = swap_radix;
327 swap_radix = tmp;
328 last = search_start;
329 goto again;
330 }
Chris Mason31f3c992007-04-30 15:25:45 -0400331 if (!found_group) {
Chris Masonbe744172007-05-06 10:15:01 -0400332 ret = radix_tree_gang_lookup(radix,
Chris Mason31f3c992007-04-30 15:25:45 -0400333 (void **)&found_group, 0, 1);
Chris Mason1e2677e2007-05-29 16:52:18 -0400334 if (ret == 0) {
335 ret = radix_tree_gang_lookup(swap_radix,
336 (void **)&found_group,
337 0, 1);
338 }
Chris Mason31f3c992007-04-30 15:25:45 -0400339 BUG_ON(ret != 1);
340 }
Chris Masonbe744172007-05-06 10:15:01 -0400341found:
Chris Mason31f3c992007-04-30 15:25:45 -0400342 return found_group;
Chris Masoncd1bc462007-04-27 10:08:34 -0400343}
344
Chris Masonb18c6682007-04-17 13:26:50 -0400345int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
346 struct btrfs_root *root,
347 u64 blocknr, u64 num_blocks)
Chris Mason02217ed2007-03-02 16:08:05 -0500348{
Chris Mason5caf2a02007-04-02 11:20:42 -0400349 struct btrfs_path *path;
Chris Mason02217ed2007-03-02 16:08:05 -0500350 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400351 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400352 struct extent_buffer *l;
Chris Mason234b63a2007-03-13 10:46:10 -0400353 struct btrfs_extent_item *item;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400354 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500355
Chris Mason5caf2a02007-04-02 11:20:42 -0400356 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -0400357 if (!path)
358 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -0400359
Chris Mason02217ed2007-03-02 16:08:05 -0500360 key.objectid = blocknr;
Chris Mason62e27492007-03-15 12:56:47 -0400361 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Mason6407bf62007-03-27 06:33:00 -0400362 key.offset = num_blocks;
Chris Mason5caf2a02007-04-02 11:20:42 -0400363 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -0400364 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400365 if (ret < 0)
366 return ret;
Chris Masona429e512007-04-18 16:15:28 -0400367 if (ret != 0) {
Chris Masona28ec192007-03-06 20:08:01 -0500368 BUG();
Chris Masona429e512007-04-18 16:15:28 -0400369 }
Chris Mason02217ed2007-03-02 16:08:05 -0500370 BUG_ON(ret != 0);
Chris Mason5f39d392007-10-15 16:14:19 -0400371 l = path->nodes[0];
Chris Mason5caf2a02007-04-02 11:20:42 -0400372 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400373 refs = btrfs_extent_refs(l, item);
374 btrfs_set_extent_refs(l, item, refs + 1);
Chris Mason5caf2a02007-04-02 11:20:42 -0400375 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masona28ec192007-03-06 20:08:01 -0500376
Chris Mason5caf2a02007-04-02 11:20:42 -0400377 btrfs_release_path(root->fs_info->extent_root, path);
378 btrfs_free_path(path);
Chris Mason9f5fae22007-03-20 14:38:32 -0400379 finish_current_insert(trans, root->fs_info->extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -0400380 del_pending_extents(trans, root->fs_info->extent_root);
Chris Mason02217ed2007-03-02 16:08:05 -0500381 return 0;
382}
383
Chris Masone9d0b132007-08-10 14:06:19 -0400384int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
385 struct btrfs_root *root)
386{
387 finish_current_insert(trans, root->fs_info->extent_root);
388 del_pending_extents(trans, root->fs_info->extent_root);
389 return 0;
390}
391
Chris Masonb18c6682007-04-17 13:26:50 -0400392static int lookup_extent_ref(struct btrfs_trans_handle *trans,
393 struct btrfs_root *root, u64 blocknr,
394 u64 num_blocks, u32 *refs)
Chris Masona28ec192007-03-06 20:08:01 -0500395{
Chris Mason5caf2a02007-04-02 11:20:42 -0400396 struct btrfs_path *path;
Chris Masona28ec192007-03-06 20:08:01 -0500397 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400398 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400399 struct extent_buffer *l;
Chris Mason234b63a2007-03-13 10:46:10 -0400400 struct btrfs_extent_item *item;
Chris Mason5caf2a02007-04-02 11:20:42 -0400401
402 path = btrfs_alloc_path();
Chris Masona28ec192007-03-06 20:08:01 -0500403 key.objectid = blocknr;
Chris Mason6407bf62007-03-27 06:33:00 -0400404 key.offset = num_blocks;
Chris Mason62e27492007-03-15 12:56:47 -0400405 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Mason5caf2a02007-04-02 11:20:42 -0400406 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -0400407 0, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400408 if (ret < 0)
409 goto out;
Chris Mason5f39d392007-10-15 16:14:19 -0400410 if (ret != 0) {
411 btrfs_print_leaf(root, path->nodes[0]);
412 printk("failed to find block number %Lu\n", blocknr);
Chris Masona28ec192007-03-06 20:08:01 -0500413 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -0400414 }
415 l = path->nodes[0];
Chris Mason5caf2a02007-04-02 11:20:42 -0400416 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400417 *refs = btrfs_extent_refs(l, item);
Chris Mason54aa1f42007-06-22 14:16:25 -0400418out:
Chris Mason5caf2a02007-04-02 11:20:42 -0400419 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -0500420 return 0;
421}
422
Chris Masonc5739bb2007-04-10 09:27:04 -0400423int btrfs_inc_root_ref(struct btrfs_trans_handle *trans,
424 struct btrfs_root *root)
425{
Chris Mason5f39d392007-10-15 16:14:19 -0400426 return btrfs_inc_extent_ref(trans, root,
427 extent_buffer_blocknr(root->node), 1);
Chris Masonc5739bb2007-04-10 09:27:04 -0400428}
429
Chris Masone089f052007-03-16 16:20:31 -0400430int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400431 struct extent_buffer *buf)
Chris Mason02217ed2007-03-02 16:08:05 -0500432{
433 u64 blocknr;
Chris Mason5f39d392007-10-15 16:14:19 -0400434 u32 nritems;
435 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -0400436 struct btrfs_file_extent_item *fi;
Chris Mason02217ed2007-03-02 16:08:05 -0500437 int i;
Chris Mason6407bf62007-03-27 06:33:00 -0400438 int leaf;
439 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -0400440 int faili;
441 int err;
Chris Masona28ec192007-03-06 20:08:01 -0500442
Chris Mason3768f362007-03-13 16:47:54 -0400443 if (!root->ref_cows)
Chris Masona28ec192007-03-06 20:08:01 -0500444 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400445
446 leaf = btrfs_is_leaf(buf);
447 nritems = btrfs_header_nritems(buf);
448 for (i = 0; i < nritems; i++) {
Chris Mason6407bf62007-03-27 06:33:00 -0400449 if (leaf) {
Chris Mason3a686372007-05-24 13:35:57 -0400450 u64 disk_blocknr;
Chris Mason5f39d392007-10-15 16:14:19 -0400451 btrfs_item_key_to_cpu(buf, &key, i);
452 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason6407bf62007-03-27 06:33:00 -0400453 continue;
Chris Mason5f39d392007-10-15 16:14:19 -0400454 fi = btrfs_item_ptr(buf, i,
Chris Mason6407bf62007-03-27 06:33:00 -0400455 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400456 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason236454d2007-04-19 13:37:44 -0400457 BTRFS_FILE_EXTENT_INLINE)
458 continue;
Chris Mason5f39d392007-10-15 16:14:19 -0400459 disk_blocknr = btrfs_file_extent_disk_blocknr(buf, fi);
Chris Mason3a686372007-05-24 13:35:57 -0400460 if (disk_blocknr == 0)
461 continue;
462 ret = btrfs_inc_extent_ref(trans, root, disk_blocknr,
Chris Mason5f39d392007-10-15 16:14:19 -0400463 btrfs_file_extent_disk_num_blocks(buf, fi));
Chris Mason54aa1f42007-06-22 14:16:25 -0400464 if (ret) {
465 faili = i;
466 goto fail;
467 }
Chris Mason6407bf62007-03-27 06:33:00 -0400468 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400469 blocknr = btrfs_node_blockptr(buf, i);
Chris Masonb18c6682007-04-17 13:26:50 -0400470 ret = btrfs_inc_extent_ref(trans, root, blocknr, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400471 if (ret) {
472 faili = i;
473 goto fail;
474 }
Chris Mason6407bf62007-03-27 06:33:00 -0400475 }
Chris Mason02217ed2007-03-02 16:08:05 -0500476 }
477 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -0400478fail:
Chris Masonccd467d2007-06-28 15:57:36 -0400479 WARN_ON(1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400480 for (i =0; i < faili; i++) {
481 if (leaf) {
482 u64 disk_blocknr;
Chris Mason5f39d392007-10-15 16:14:19 -0400483 btrfs_item_key_to_cpu(buf, &key, i);
484 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -0400485 continue;
Chris Mason5f39d392007-10-15 16:14:19 -0400486 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -0400487 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400488 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -0400489 BTRFS_FILE_EXTENT_INLINE)
490 continue;
Chris Mason5f39d392007-10-15 16:14:19 -0400491 disk_blocknr = btrfs_file_extent_disk_blocknr(buf, fi);
Chris Mason54aa1f42007-06-22 14:16:25 -0400492 if (disk_blocknr == 0)
493 continue;
494 err = btrfs_free_extent(trans, root, disk_blocknr,
Chris Mason5f39d392007-10-15 16:14:19 -0400495 btrfs_file_extent_disk_num_blocks(buf,
496 fi), 0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400497 BUG_ON(err);
498 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400499 blocknr = btrfs_node_blockptr(buf, i);
Chris Mason54aa1f42007-06-22 14:16:25 -0400500 err = btrfs_free_extent(trans, root, blocknr, 1, 0);
501 BUG_ON(err);
502 }
503 }
504 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -0500505}
506
Chris Mason9078a3e2007-04-26 16:46:15 -0400507static int write_one_cache_group(struct btrfs_trans_handle *trans,
508 struct btrfs_root *root,
509 struct btrfs_path *path,
510 struct btrfs_block_group_cache *cache)
511{
512 int ret;
513 int pending_ret;
514 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -0400515 unsigned long bi;
516 struct extent_buffer *leaf;
Chris Mason9078a3e2007-04-26 16:46:15 -0400517
Chris Mason9078a3e2007-04-26 16:46:15 -0400518 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400519 if (ret < 0)
520 goto fail;
Chris Mason9078a3e2007-04-26 16:46:15 -0400521 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -0400522
523 leaf = path->nodes[0];
524 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
525 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
526 btrfs_mark_buffer_dirty(leaf);
Chris Mason9078a3e2007-04-26 16:46:15 -0400527 btrfs_release_path(extent_root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -0400528fail:
Chris Mason9078a3e2007-04-26 16:46:15 -0400529 finish_current_insert(trans, extent_root);
530 pending_ret = del_pending_extents(trans, extent_root);
531 if (ret)
532 return ret;
533 if (pending_ret)
534 return pending_ret;
Chris Masonbe744172007-05-06 10:15:01 -0400535 if (cache->data)
536 cache->last_alloc = cache->first_free;
Chris Mason9078a3e2007-04-26 16:46:15 -0400537 return 0;
538
539}
540
Chris Masonbe744172007-05-06 10:15:01 -0400541static int write_dirty_block_radix(struct btrfs_trans_handle *trans,
542 struct btrfs_root *root,
543 struct radix_tree_root *radix)
Chris Mason9078a3e2007-04-26 16:46:15 -0400544{
545 struct btrfs_block_group_cache *cache[8];
546 int ret;
547 int err = 0;
548 int werr = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -0400549 int i;
550 struct btrfs_path *path;
Chris Mason54aa1f42007-06-22 14:16:25 -0400551 unsigned long off = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -0400552
553 path = btrfs_alloc_path();
554 if (!path)
555 return -ENOMEM;
556
557 while(1) {
558 ret = radix_tree_gang_lookup_tag(radix, (void **)cache,
Chris Mason54aa1f42007-06-22 14:16:25 -0400559 off, ARRAY_SIZE(cache),
Chris Mason9078a3e2007-04-26 16:46:15 -0400560 BTRFS_BLOCK_GROUP_DIRTY);
561 if (!ret)
562 break;
563 for (i = 0; i < ret; i++) {
Chris Mason54aa1f42007-06-22 14:16:25 -0400564 err = write_one_cache_group(trans, root,
565 path, cache[i]);
566 /*
567 * if we fail to write the cache group, we want
568 * to keep it marked dirty in hopes that a later
569 * write will work
570 */
571 if (err) {
572 werr = err;
573 off = cache[i]->key.objectid +
574 cache[i]->key.offset;
575 continue;
576 }
577
Chris Mason9078a3e2007-04-26 16:46:15 -0400578 radix_tree_tag_clear(radix, cache[i]->key.objectid +
579 cache[i]->key.offset - 1,
580 BTRFS_BLOCK_GROUP_DIRTY);
Chris Mason9078a3e2007-04-26 16:46:15 -0400581 }
582 }
583 btrfs_free_path(path);
584 return werr;
585}
586
Chris Masonbe744172007-05-06 10:15:01 -0400587int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
588 struct btrfs_root *root)
589{
590 int ret;
591 int ret2;
592 ret = write_dirty_block_radix(trans, root,
593 &root->fs_info->block_group_radix);
594 ret2 = write_dirty_block_radix(trans, root,
595 &root->fs_info->block_group_data_radix);
596 if (ret)
597 return ret;
598 if (ret2)
599 return ret2;
600 return 0;
601}
602
Chris Mason9078a3e2007-04-26 16:46:15 -0400603static int update_block_group(struct btrfs_trans_handle *trans,
604 struct btrfs_root *root,
Chris Mason1e2677e2007-05-29 16:52:18 -0400605 u64 blocknr, u64 num, int alloc, int mark_free,
606 int data)
Chris Mason9078a3e2007-04-26 16:46:15 -0400607{
608 struct btrfs_block_group_cache *cache;
609 struct btrfs_fs_info *info = root->fs_info;
610 u64 total = num;
611 u64 old_val;
612 u64 block_in_group;
Chris Mason1e2677e2007-05-29 16:52:18 -0400613 int ret;
Chris Mason3e1ad542007-05-07 20:03:49 -0400614
Chris Mason9078a3e2007-04-26 16:46:15 -0400615 while(total) {
Chris Mason5276aed2007-06-11 21:33:38 -0400616 cache = btrfs_lookup_block_group(info, blocknr);
Chris Mason3e1ad542007-05-07 20:03:49 -0400617 if (!cache) {
Chris Mason9078a3e2007-04-26 16:46:15 -0400618 return -1;
Chris Masoncd1bc462007-04-27 10:08:34 -0400619 }
Chris Mason9078a3e2007-04-26 16:46:15 -0400620 block_in_group = blocknr - cache->key.objectid;
621 WARN_ON(block_in_group > cache->key.offset);
Chris Mason3e1ad542007-05-07 20:03:49 -0400622 radix_tree_tag_set(cache->radix, cache->key.objectid +
Chris Masonbe744172007-05-06 10:15:01 -0400623 cache->key.offset - 1,
Chris Mason9078a3e2007-04-26 16:46:15 -0400624 BTRFS_BLOCK_GROUP_DIRTY);
625
626 old_val = btrfs_block_group_used(&cache->item);
627 num = min(total, cache->key.offset - block_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -0400628 if (alloc) {
Chris Masoncd1bc462007-04-27 10:08:34 -0400629 if (blocknr > cache->last_alloc)
630 cache->last_alloc = blocknr;
Chris Mason1e2677e2007-05-29 16:52:18 -0400631 if (cache->data != data &&
Chris Mason84f54cf2007-06-12 07:43:08 -0400632 old_val < (cache->key.offset >> 1)) {
Chris Mason1e2677e2007-05-29 16:52:18 -0400633 cache->data = data;
634 radix_tree_delete(cache->radix,
635 cache->key.objectid +
636 cache->key.offset - 1);
637
638 if (data) {
639 cache->radix =
640 &info->block_group_data_radix;
641 cache->item.flags |=
642 BTRFS_BLOCK_GROUP_DATA;
643 } else {
644 cache->radix = &info->block_group_radix;
645 cache->item.flags &=
646 ~BTRFS_BLOCK_GROUP_DATA;
647 }
648 ret = radix_tree_insert(cache->radix,
649 cache->key.objectid +
650 cache->key.offset - 1,
651 (void *)cache);
652 }
653 old_val += num;
Chris Masoncd1bc462007-04-27 10:08:34 -0400654 } else {
Chris Mason9078a3e2007-04-26 16:46:15 -0400655 old_val -= num;
Chris Masoncd1bc462007-04-27 10:08:34 -0400656 if (blocknr < cache->first_free)
657 cache->first_free = blocknr;
Chris Masonf510cfe2007-10-15 16:14:48 -0400658 if (mark_free) {
659 set_extent_dirty(&info->free_space_cache,
660 blocknr, blocknr + num - 1,
661 GFP_NOFS);
Chris Masone37c9e62007-05-09 20:13:14 -0400662 }
Chris Mason84f54cf2007-06-12 07:43:08 -0400663 if (old_val < (cache->key.offset >> 1) &&
664 old_val + num >= (cache->key.offset >> 1)) {
Chris Masone37c9e62007-05-09 20:13:14 -0400665 radix_tree_tag_set(cache->radix,
666 cache->key.objectid +
667 cache->key.offset - 1,
668 BTRFS_BLOCK_GROUP_AVAIL);
669 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400670 }
Chris Mason9078a3e2007-04-26 16:46:15 -0400671 btrfs_set_block_group_used(&cache->item, old_val);
Chris Masone37c9e62007-05-09 20:13:14 -0400672 total -= num;
673 blocknr += num;
Chris Mason9078a3e2007-04-26 16:46:15 -0400674 }
675 return 0;
676}
677
Chris Masonccd467d2007-06-28 15:57:36 -0400678int btrfs_copy_pinned(struct btrfs_root *root, struct radix_tree_root *copy)
679{
680 unsigned long gang[8];
681 u64 last = 0;
682 struct radix_tree_root *pinned_radix = &root->fs_info->pinned_radix;
683 int ret;
684 int i;
685
686 while(1) {
687 ret = find_first_radix_bit(pinned_radix, gang, last,
688 ARRAY_SIZE(gang));
689 if (!ret)
690 break;
691 for (i = 0 ; i < ret; i++) {
692 set_radix_bit(copy, gang[i]);
693 last = gang[i] + 1;
694 }
695 }
Chris Mason26b80032007-08-08 20:17:12 -0400696 ret = find_first_radix_bit(&root->fs_info->extent_ins_radix, gang, 0,
697 ARRAY_SIZE(gang));
698 WARN_ON(ret);
Chris Masonccd467d2007-06-28 15:57:36 -0400699 return 0;
700}
701
702int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
703 struct btrfs_root *root,
704 struct radix_tree_root *unpin_radix)
Chris Masona28ec192007-03-06 20:08:01 -0500705{
Chris Mason8ef97622007-03-26 10:15:30 -0400706 unsigned long gang[8];
Chris Masonbe744172007-05-06 10:15:01 -0400707 struct btrfs_block_group_cache *block_group;
Chris Mason88fd1462007-03-16 08:56:18 -0400708 u64 first = 0;
Chris Masona28ec192007-03-06 20:08:01 -0500709 int ret;
710 int i;
Chris Mason8ef97622007-03-26 10:15:30 -0400711 struct radix_tree_root *pinned_radix = &root->fs_info->pinned_radix;
Chris Masonf510cfe2007-10-15 16:14:48 -0400712 struct extent_map_tree *free_space_cache;
713
714 free_space_cache = &root->fs_info->free_space_cache;
Chris Masona28ec192007-03-06 20:08:01 -0500715
716 while(1) {
Chris Masonccd467d2007-06-28 15:57:36 -0400717 ret = find_first_radix_bit(unpin_radix, gang, 0,
Chris Mason8ef97622007-03-26 10:15:30 -0400718 ARRAY_SIZE(gang));
Chris Masona28ec192007-03-06 20:08:01 -0500719 if (!ret)
720 break;
Chris Mason88fd1462007-03-16 08:56:18 -0400721 if (!first)
Chris Mason8ef97622007-03-26 10:15:30 -0400722 first = gang[0];
Chris Mason0579da42007-03-07 16:15:30 -0500723 for (i = 0; i < ret; i++) {
Chris Mason8ef97622007-03-26 10:15:30 -0400724 clear_radix_bit(pinned_radix, gang[i]);
Chris Masonccd467d2007-06-28 15:57:36 -0400725 clear_radix_bit(unpin_radix, gang[i]);
Chris Mason5276aed2007-06-11 21:33:38 -0400726 block_group = btrfs_lookup_block_group(root->fs_info,
727 gang[i]);
Chris Masonbe744172007-05-06 10:15:01 -0400728 if (block_group) {
729 WARN_ON(block_group->pinned == 0);
730 block_group->pinned--;
731 if (gang[i] < block_group->last_alloc)
732 block_group->last_alloc = gang[i];
Chris Masonf510cfe2007-10-15 16:14:48 -0400733 if (!block_group->data) {
734 set_extent_dirty(free_space_cache,
735 gang[i], gang[i],
736 GFP_NOFS);
737 }
Chris Masonbe744172007-05-06 10:15:01 -0400738 }
Chris Mason0579da42007-03-07 16:15:30 -0500739 }
Chris Masona28ec192007-03-06 20:08:01 -0500740 }
741 return 0;
742}
743
Chris Masone089f052007-03-16 16:20:31 -0400744static int finish_current_insert(struct btrfs_trans_handle *trans, struct
745 btrfs_root *extent_root)
Chris Mason037e6392007-03-07 11:50:24 -0500746{
Chris Masone2fa7222007-03-12 16:22:34 -0400747 struct btrfs_key ins;
Chris Mason234b63a2007-03-13 10:46:10 -0400748 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -0500749 int i;
750 int ret;
Chris Mason26b80032007-08-08 20:17:12 -0400751 int err;
752 unsigned long gang[8];
Chris Mason1261ec42007-03-20 20:35:03 -0400753 struct btrfs_fs_info *info = extent_root->fs_info;
Chris Mason037e6392007-03-07 11:50:24 -0500754
Chris Mason5f39d392007-10-15 16:14:19 -0400755 btrfs_set_stack_extent_refs(&extent_item, 1);
Chris Mason037e6392007-03-07 11:50:24 -0500756 ins.offset = 1;
Chris Mason62e27492007-03-15 12:56:47 -0400757 btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
Chris Mason5f39d392007-10-15 16:14:19 -0400758 btrfs_set_stack_extent_owner(&extent_item,
759 extent_root->root_key.objectid);
Chris Mason037e6392007-03-07 11:50:24 -0500760
Chris Mason26b80032007-08-08 20:17:12 -0400761 while(1) {
762 ret = find_first_radix_bit(&info->extent_ins_radix, gang, 0,
763 ARRAY_SIZE(gang));
764 if (!ret)
765 break;
766
767 for (i = 0; i < ret; i++) {
768 ins.objectid = gang[i];
769 err = btrfs_insert_item(trans, extent_root, &ins,
770 &extent_item,
771 sizeof(extent_item));
772 clear_radix_bit(&info->extent_ins_radix, gang[i]);
773 WARN_ON(err);
774 }
Chris Mason037e6392007-03-07 11:50:24 -0500775 }
Chris Mason037e6392007-03-07 11:50:24 -0500776 return 0;
777}
778
Chris Mason8ef97622007-03-26 10:15:30 -0400779static int pin_down_block(struct btrfs_root *root, u64 blocknr, int pending)
Chris Masone20d96d2007-03-22 12:13:20 -0400780{
781 int err;
Chris Mason5f39d392007-10-15 16:14:19 -0400782 struct extent_buffer *buf;
Chris Mason78fae272007-03-25 11:35:08 -0400783
Chris Masonf4b9aa82007-03-27 11:05:53 -0400784 if (!pending) {
Chris Mason5f39d392007-10-15 16:14:19 -0400785 buf = btrfs_find_tree_block(root, blocknr);
786 if (buf) {
787 if (btrfs_buffer_uptodate(buf)) {
Chris Mason2c90e5d2007-04-02 10:50:19 -0400788 u64 transid =
789 root->fs_info->running_transaction->transid;
Chris Mason5f39d392007-10-15 16:14:19 -0400790 if (btrfs_header_generation(buf) == transid) {
791 free_extent_buffer(buf);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400792 return 0;
793 }
Chris Masonf4b9aa82007-03-27 11:05:53 -0400794 }
Chris Mason5f39d392007-10-15 16:14:19 -0400795 free_extent_buffer(buf);
Chris Mason8ef97622007-03-26 10:15:30 -0400796 }
Chris Mason8ef97622007-03-26 10:15:30 -0400797 err = set_radix_bit(&root->fs_info->pinned_radix, blocknr);
Chris Masonbe744172007-05-06 10:15:01 -0400798 if (!err) {
799 struct btrfs_block_group_cache *cache;
Chris Mason5276aed2007-06-11 21:33:38 -0400800 cache = btrfs_lookup_block_group(root->fs_info,
801 blocknr);
Chris Masonbe744172007-05-06 10:15:01 -0400802 if (cache)
803 cache->pinned++;
804 }
Chris Masonf4b9aa82007-03-27 11:05:53 -0400805 } else {
806 err = set_radix_bit(&root->fs_info->pending_del_radix, blocknr);
807 }
Chris Masonbe744172007-05-06 10:15:01 -0400808 BUG_ON(err < 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400809 return 0;
810}
811
Chris Masona28ec192007-03-06 20:08:01 -0500812/*
813 * remove an extent from the root, returns 0 on success
814 */
Chris Masone089f052007-03-16 16:20:31 -0400815static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masone37c9e62007-05-09 20:13:14 -0400816 *root, u64 blocknr, u64 num_blocks, int pin,
817 int mark_free)
Chris Masona28ec192007-03-06 20:08:01 -0500818{
Chris Mason5caf2a02007-04-02 11:20:42 -0400819 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -0400820 struct btrfs_key key;
Chris Mason1261ec42007-03-20 20:35:03 -0400821 struct btrfs_fs_info *info = root->fs_info;
822 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -0400823 struct extent_buffer *leaf;
Chris Masona28ec192007-03-06 20:08:01 -0500824 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400825 struct btrfs_extent_item *ei;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400826 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500827
Chris Masona28ec192007-03-06 20:08:01 -0500828 key.objectid = blocknr;
Chris Mason62e27492007-03-15 12:56:47 -0400829 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masona28ec192007-03-06 20:08:01 -0500830 key.offset = num_blocks;
831
Chris Mason5caf2a02007-04-02 11:20:42 -0400832 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -0400833 if (!path)
834 return -ENOMEM;
835
Chris Mason5caf2a02007-04-02 11:20:42 -0400836 ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400837 if (ret < 0)
838 return ret;
839 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -0400840
841 leaf = path->nodes[0];
842 ei = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason123abc82007-03-14 14:14:43 -0400843 struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400844 refs = btrfs_extent_refs(leaf, ei);
845 BUG_ON(refs == 0);
846 refs -= 1;
847 btrfs_set_extent_refs(leaf, ei, refs);
848 btrfs_mark_buffer_dirty(leaf);
849
Chris Masoncf27e1e2007-03-13 09:49:06 -0400850 if (refs == 0) {
Josef Bacik58176a92007-08-29 15:47:34 -0400851 u64 super_blocks_used, root_blocks_used;
Chris Mason78fae272007-03-25 11:35:08 -0400852
853 if (pin) {
Chris Mason8ef97622007-03-26 10:15:30 -0400854 ret = pin_down_block(root, blocknr, 0);
Chris Mason78fae272007-03-25 11:35:08 -0400855 BUG_ON(ret);
856 }
857
Josef Bacik58176a92007-08-29 15:47:34 -0400858 /* block accounting for super block */
Chris Mason4b52dff2007-06-26 10:06:50 -0400859 super_blocks_used = btrfs_super_blocks_used(&info->super_copy);
860 btrfs_set_super_blocks_used(&info->super_copy,
Chris Mason1261ec42007-03-20 20:35:03 -0400861 super_blocks_used - num_blocks);
Josef Bacik58176a92007-08-29 15:47:34 -0400862
863 /* block accounting for root item */
Chris Mason5f39d392007-10-15 16:14:19 -0400864 root_blocks_used = btrfs_root_used(&root->root_item);
865 btrfs_set_root_used(&root->root_item,
Josef Bacik58176a92007-08-29 15:47:34 -0400866 root_blocks_used - num_blocks);
867
Chris Mason5caf2a02007-04-02 11:20:42 -0400868 ret = btrfs_del_item(trans, extent_root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -0400869 if (ret) {
870 return ret;
871 }
Chris Masone37c9e62007-05-09 20:13:14 -0400872 ret = update_block_group(trans, root, blocknr, num_blocks, 0,
Chris Mason1e2677e2007-05-29 16:52:18 -0400873 mark_free, 0);
Chris Mason9078a3e2007-04-26 16:46:15 -0400874 BUG_ON(ret);
Chris Masona28ec192007-03-06 20:08:01 -0500875 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400876 btrfs_free_path(path);
Chris Masone089f052007-03-16 16:20:31 -0400877 finish_current_insert(trans, extent_root);
Chris Masona28ec192007-03-06 20:08:01 -0500878 return ret;
879}
880
881/*
Chris Masonfec577f2007-02-26 10:40:21 -0500882 * find all the blocks marked as pending in the radix tree and remove
883 * them from the extent map
884 */
Chris Masone089f052007-03-16 16:20:31 -0400885static int del_pending_extents(struct btrfs_trans_handle *trans, struct
886 btrfs_root *extent_root)
Chris Masonfec577f2007-02-26 10:40:21 -0500887{
888 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -0400889 int wret;
890 int err = 0;
Chris Mason8ef97622007-03-26 10:15:30 -0400891 unsigned long gang[4];
Chris Masonfec577f2007-02-26 10:40:21 -0500892 int i;
Chris Mason8ef97622007-03-26 10:15:30 -0400893 struct radix_tree_root *pending_radix;
894 struct radix_tree_root *pinned_radix;
Chris Masonbe744172007-05-06 10:15:01 -0400895 struct btrfs_block_group_cache *cache;
Chris Mason8ef97622007-03-26 10:15:30 -0400896
897 pending_radix = &extent_root->fs_info->pending_del_radix;
898 pinned_radix = &extent_root->fs_info->pinned_radix;
Chris Masonfec577f2007-02-26 10:40:21 -0500899
900 while(1) {
Chris Masone37c9e62007-05-09 20:13:14 -0400901 ret = find_first_radix_bit(pending_radix, gang, 0,
Chris Mason8ef97622007-03-26 10:15:30 -0400902 ARRAY_SIZE(gang));
Chris Masonfec577f2007-02-26 10:40:21 -0500903 if (!ret)
904 break;
905 for (i = 0; i < ret; i++) {
Chris Mason8ef97622007-03-26 10:15:30 -0400906 wret = set_radix_bit(pinned_radix, gang[i]);
Chris Masonbe744172007-05-06 10:15:01 -0400907 if (wret == 0) {
Chris Mason5276aed2007-06-11 21:33:38 -0400908 cache =
909 btrfs_lookup_block_group(extent_root->fs_info,
Chris Masonbe744172007-05-06 10:15:01 -0400910 gang[i]);
911 if (cache)
912 cache->pinned++;
913 }
914 if (wret < 0) {
915 printk(KERN_CRIT "set_radix_bit, err %d\n",
916 wret);
917 BUG_ON(wret < 0);
918 }
Chris Mason8ef97622007-03-26 10:15:30 -0400919 wret = clear_radix_bit(pending_radix, gang[i]);
920 BUG_ON(wret);
Chris Masond5719762007-03-23 10:01:08 -0400921 wret = __free_extent(trans, extent_root,
Chris Masone37c9e62007-05-09 20:13:14 -0400922 gang[i], 1, 0, 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400923 if (wret)
924 err = wret;
Chris Masonfec577f2007-02-26 10:40:21 -0500925 }
926 }
Chris Masone20d96d2007-03-22 12:13:20 -0400927 return err;
Chris Masonfec577f2007-02-26 10:40:21 -0500928}
929
930/*
931 * remove an extent from the root, returns 0 on success
932 */
Chris Masone089f052007-03-16 16:20:31 -0400933int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
934 *root, u64 blocknr, u64 num_blocks, int pin)
Chris Masonfec577f2007-02-26 10:40:21 -0500935{
Chris Mason9f5fae22007-03-20 14:38:32 -0400936 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Masonfec577f2007-02-26 10:40:21 -0500937 int pending_ret;
938 int ret;
Chris Masona28ec192007-03-06 20:08:01 -0500939
940 if (root == extent_root) {
Chris Mason8ef97622007-03-26 10:15:30 -0400941 pin_down_block(root, blocknr, 1);
Chris Masona28ec192007-03-06 20:08:01 -0500942 return 0;
943 }
Chris Masone37c9e62007-05-09 20:13:14 -0400944 ret = __free_extent(trans, root, blocknr, num_blocks, pin, pin == 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400945 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
Chris Masonfec577f2007-02-26 10:40:21 -0500946 return ret ? ret : pending_ret;
947}
948
949/*
950 * walks the btree of allocated extents and find a hole of a given size.
951 * The key ins is changed to record the hole:
952 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -0400953 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -0500954 * ins->offset == number of blocks
955 * Any available blocks before search_start are skipped.
956 */
Chris Masone089f052007-03-16 16:20:31 -0400957static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason6702ed42007-08-07 16:15:09 -0400958 *orig_root, u64 num_blocks, u64 empty_size,
959 u64 search_start, u64 search_end, u64 hint_block,
Chris Masonf2654de2007-06-26 12:20:46 -0400960 struct btrfs_key *ins, u64 exclude_start,
961 u64 exclude_nr, int data)
Chris Masonfec577f2007-02-26 10:40:21 -0500962{
Chris Mason5caf2a02007-04-02 11:20:42 -0400963 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -0400964 struct btrfs_key key;
Chris Masonfec577f2007-02-26 10:40:21 -0500965 int ret;
966 u64 hole_size = 0;
967 int slot = 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400968 u64 last_block = 0;
Chris Mason037e6392007-03-07 11:50:24 -0500969 u64 test_block;
Chris Masonbe744172007-05-06 10:15:01 -0400970 u64 orig_search_start = search_start;
Chris Masonfec577f2007-02-26 10:40:21 -0500971 int start_found;
Chris Mason5f39d392007-10-15 16:14:19 -0400972 struct extent_buffer *l;
Chris Mason9f5fae22007-03-20 14:38:32 -0400973 struct btrfs_root * root = orig_root->fs_info->extent_root;
Chris Masonf2458e12007-04-25 15:52:25 -0400974 struct btrfs_fs_info *info = root->fs_info;
Chris Mason0579da42007-03-07 16:15:30 -0500975 int total_needed = num_blocks;
Chris Masone20d96d2007-03-22 12:13:20 -0400976 int level;
Chris Masonbe08c1b2007-05-03 09:06:49 -0400977 struct btrfs_block_group_cache *block_group;
Chris Masonbe744172007-05-06 10:15:01 -0400978 int full_scan = 0;
Chris Masonfbdc7622007-05-30 10:22:12 -0400979 int wrapped = 0;
Chris Masonf510cfe2007-10-15 16:14:48 -0400980 u64 cached_search_start = 0;
Chris Masonfec577f2007-02-26 10:40:21 -0500981
Chris Mason26b80032007-08-08 20:17:12 -0400982 WARN_ON(num_blocks < 1);
Chris Masonb1a4d962007-04-04 15:27:52 -0400983 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
984
Chris Mason5f39d392007-10-15 16:14:19 -0400985 level = btrfs_header_level(root->node);
986
Chris Mason3e1ad542007-05-07 20:03:49 -0400987 if (search_end == (u64)-1)
Chris Mason4b52dff2007-06-26 10:06:50 -0400988 search_end = btrfs_super_total_blocks(&info->super_copy);
Chris Masonfbdc7622007-05-30 10:22:12 -0400989 if (hint_block) {
Chris Mason5276aed2007-06-11 21:33:38 -0400990 block_group = btrfs_lookup_block_group(info, hint_block);
Chris Masonbe744172007-05-06 10:15:01 -0400991 block_group = btrfs_find_block_group(root, block_group,
Chris Masonfbdc7622007-05-30 10:22:12 -0400992 hint_block, data, 1);
Chris Masonbe744172007-05-06 10:15:01 -0400993 } else {
994 block_group = btrfs_find_block_group(root,
995 trans->block_group, 0,
Chris Masonde428b62007-05-18 13:28:27 -0400996 data, 1);
Chris Masonbe744172007-05-06 10:15:01 -0400997 }
998
Chris Mason6702ed42007-08-07 16:15:09 -0400999 total_needed += empty_size;
Chris Masone0115992007-06-19 16:23:05 -04001000 path = btrfs_alloc_path();
1001
Chris Masonbe744172007-05-06 10:15:01 -04001002check_failed:
Chris Masonf510cfe2007-10-15 16:14:48 -04001003 search_start = find_search_start(root, &block_group,
1004 search_start, total_needed, data);
1005 cached_search_start = search_start;
Chris Masone37c9e62007-05-09 20:13:14 -04001006
Chris Mason5caf2a02007-04-02 11:20:42 -04001007 btrfs_init_path(path);
Chris Masonfec577f2007-02-26 10:40:21 -05001008 ins->objectid = search_start;
1009 ins->offset = 0;
Chris Masonfec577f2007-02-26 10:40:21 -05001010 start_found = 0;
Chris Mason2cc58cf2007-08-27 16:49:44 -04001011 path->reada = 2;
Chris Masone37c9e62007-05-09 20:13:14 -04001012
Chris Mason5caf2a02007-04-02 11:20:42 -04001013 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
Chris Mason0f70abe2007-02-28 16:46:22 -05001014 if (ret < 0)
1015 goto error;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001016
Chris Masone37c9e62007-05-09 20:13:14 -04001017 if (path->slots[0] > 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001018 path->slots[0]--;
Chris Masone37c9e62007-05-09 20:13:14 -04001019 }
1020
Chris Mason5f39d392007-10-15 16:14:19 -04001021 l = path->nodes[0];
1022 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
1023
Chris Masone37c9e62007-05-09 20:13:14 -04001024 /*
1025 * a rare case, go back one key if we hit a block group item
1026 * instead of an extent item
1027 */
1028 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY &&
1029 key.objectid + key.offset >= search_start) {
1030 ins->objectid = key.objectid;
1031 ins->offset = key.offset - 1;
1032 btrfs_release_path(root, path);
1033 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
1034 if (ret < 0)
1035 goto error;
1036
1037 if (path->slots[0] > 0) {
1038 path->slots[0]--;
1039 }
1040 }
Chris Mason0579da42007-03-07 16:15:30 -05001041
Chris Masonfec577f2007-02-26 10:40:21 -05001042 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001043 l = path->nodes[0];
Chris Mason5caf2a02007-04-02 11:20:42 -04001044 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04001045 if (slot >= btrfs_header_nritems(l)) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001046 ret = btrfs_next_leaf(root, path);
Chris Masonfec577f2007-02-26 10:40:21 -05001047 if (ret == 0)
1048 continue;
Chris Mason0f70abe2007-02-28 16:46:22 -05001049 if (ret < 0)
1050 goto error;
Chris Masonfec577f2007-02-26 10:40:21 -05001051 if (!start_found) {
1052 ins->objectid = search_start;
Chris Mason3e1ad542007-05-07 20:03:49 -04001053 ins->offset = search_end - search_start;
Chris Masonfec577f2007-02-26 10:40:21 -05001054 start_found = 1;
1055 goto check_pending;
1056 }
1057 ins->objectid = last_block > search_start ?
1058 last_block : search_start;
Chris Mason3e1ad542007-05-07 20:03:49 -04001059 ins->offset = search_end - ins->objectid;
Chris Masonfec577f2007-02-26 10:40:21 -05001060 goto check_pending;
1061 }
Chris Masone37c9e62007-05-09 20:13:14 -04001062
Chris Mason5f39d392007-10-15 16:14:19 -04001063 btrfs_item_key_to_cpu(l, &key, slot);
Chris Masone37c9e62007-05-09 20:13:14 -04001064 if (key.objectid >= search_start && key.objectid > last_block &&
1065 start_found) {
1066 if (last_block < search_start)
1067 last_block = search_start;
1068 hole_size = key.objectid - last_block;
1069 if (hole_size >= num_blocks) {
1070 ins->objectid = last_block;
1071 ins->offset = hole_size;
1072 goto check_pending;
Chris Mason0579da42007-03-07 16:15:30 -05001073 }
Chris Masonfec577f2007-02-26 10:40:21 -05001074 }
Chris Masone37c9e62007-05-09 20:13:14 -04001075
1076 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY)
1077 goto next;
1078
Chris Mason0579da42007-03-07 16:15:30 -05001079 start_found = 1;
Chris Masone2fa7222007-03-12 16:22:34 -04001080 last_block = key.objectid + key.offset;
Chris Masonf510cfe2007-10-15 16:14:48 -04001081
Chris Masonfbdc7622007-05-30 10:22:12 -04001082 if (!full_scan && last_block >= block_group->key.objectid +
Chris Masonbe744172007-05-06 10:15:01 -04001083 block_group->key.offset) {
1084 btrfs_release_path(root, path);
1085 search_start = block_group->key.objectid +
1086 block_group->key.offset * 2;
1087 goto new_group;
1088 }
Chris Mason9078a3e2007-04-26 16:46:15 -04001089next:
Chris Mason5caf2a02007-04-02 11:20:42 -04001090 path->slots[0]++;
Chris Masonde428b62007-05-18 13:28:27 -04001091 cond_resched();
Chris Masonfec577f2007-02-26 10:40:21 -05001092 }
Chris Masonfec577f2007-02-26 10:40:21 -05001093check_pending:
1094 /* we have to make sure we didn't find an extent that has already
1095 * been allocated by the map tree or the original allocation
1096 */
Chris Mason5caf2a02007-04-02 11:20:42 -04001097 btrfs_release_path(root, path);
Chris Masonfec577f2007-02-26 10:40:21 -05001098 BUG_ON(ins->objectid < search_start);
Chris Masone37c9e62007-05-09 20:13:14 -04001099
Chris Masoncf675822007-09-17 11:00:51 -04001100 if (ins->objectid + num_blocks >= search_end)
1101 goto enospc;
1102
Chris Mason037e6392007-03-07 11:50:24 -05001103 for (test_block = ins->objectid;
Chris Masonf2458e12007-04-25 15:52:25 -04001104 test_block < ins->objectid + num_blocks; test_block++) {
Chris Mason26b80032007-08-08 20:17:12 -04001105 if (test_radix_bit(&info->pinned_radix, test_block) ||
1106 test_radix_bit(&info->extent_ins_radix, test_block)) {
Chris Mason037e6392007-03-07 11:50:24 -05001107 search_start = test_block + 1;
Chris Masonbe744172007-05-06 10:15:01 -04001108 goto new_group;
Chris Masonfec577f2007-02-26 10:40:21 -05001109 }
1110 }
Chris Masonf2654de2007-06-26 12:20:46 -04001111 if (exclude_nr > 0 && (ins->objectid + num_blocks > exclude_start &&
1112 ins->objectid < exclude_start + exclude_nr)) {
1113 search_start = exclude_start + exclude_nr;
1114 goto new_group;
1115 }
Chris Masone37c9e62007-05-09 20:13:14 -04001116 if (!data) {
Chris Mason5276aed2007-06-11 21:33:38 -04001117 block_group = btrfs_lookup_block_group(info, ins->objectid);
Chris Mason26b80032007-08-08 20:17:12 -04001118 if (block_group)
1119 trans->block_group = block_group;
Chris Masoncd1bc462007-04-27 10:08:34 -04001120 }
Chris Mason037e6392007-03-07 11:50:24 -05001121 ins->offset = num_blocks;
Chris Mason5caf2a02007-04-02 11:20:42 -04001122 btrfs_free_path(path);
Chris Masonf510cfe2007-10-15 16:14:48 -04001123 if (0 && ins->objectid != cached_search_start) {
1124printk("\tcached was %Lu found %Lu\n", cached_search_start, ins->objectid);
1125 }
Chris Masonfec577f2007-02-26 10:40:21 -05001126 return 0;
Chris Masonbe744172007-05-06 10:15:01 -04001127
1128new_group:
Chris Mason3e1ad542007-05-07 20:03:49 -04001129 if (search_start + num_blocks >= search_end) {
Chris Masoncf675822007-09-17 11:00:51 -04001130enospc:
Chris Masonbe744172007-05-06 10:15:01 -04001131 search_start = orig_search_start;
Chris Masonfbdc7622007-05-30 10:22:12 -04001132 if (full_scan) {
1133 ret = -ENOSPC;
1134 goto error;
1135 }
Chris Mason6702ed42007-08-07 16:15:09 -04001136 if (wrapped) {
1137 if (!full_scan)
1138 total_needed -= empty_size;
Chris Masonfbdc7622007-05-30 10:22:12 -04001139 full_scan = 1;
Chris Mason6702ed42007-08-07 16:15:09 -04001140 } else
Chris Masonfbdc7622007-05-30 10:22:12 -04001141 wrapped = 1;
Chris Masonbe744172007-05-06 10:15:01 -04001142 }
Chris Mason5276aed2007-06-11 21:33:38 -04001143 block_group = btrfs_lookup_block_group(info, search_start);
Chris Masonfbdc7622007-05-30 10:22:12 -04001144 cond_resched();
Chris Masonbe744172007-05-06 10:15:01 -04001145 if (!full_scan)
1146 block_group = btrfs_find_block_group(root, block_group,
Chris Masonde428b62007-05-18 13:28:27 -04001147 search_start, data, 0);
Chris Masonbe744172007-05-06 10:15:01 -04001148 goto check_failed;
1149
Chris Mason0f70abe2007-02-28 16:46:22 -05001150error:
Chris Mason5caf2a02007-04-02 11:20:42 -04001151 btrfs_release_path(root, path);
1152 btrfs_free_path(path);
Chris Mason0f70abe2007-02-28 16:46:22 -05001153 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05001154}
Chris Masonfec577f2007-02-26 10:40:21 -05001155/*
Chris Masonfec577f2007-02-26 10:40:21 -05001156 * finds a free extent and does all the dirty work required for allocation
1157 * returns the key for the extent through ins, and a tree buffer for
1158 * the first block of the extent through buf.
1159 *
1160 * returns 0 if everything worked, non-zero otherwise.
1161 */
Chris Mason4d775672007-04-20 20:23:12 -04001162int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
1163 struct btrfs_root *root, u64 owner,
Chris Mason6702ed42007-08-07 16:15:09 -04001164 u64 num_blocks, u64 empty_size, u64 hint_block,
Chris Masonbe08c1b2007-05-03 09:06:49 -04001165 u64 search_end, struct btrfs_key *ins, int data)
Chris Masonfec577f2007-02-26 10:40:21 -05001166{
1167 int ret;
1168 int pending_ret;
Josef Bacik58176a92007-08-29 15:47:34 -04001169 u64 super_blocks_used, root_blocks_used;
Chris Masonfbdc7622007-05-30 10:22:12 -04001170 u64 search_start = 0;
Chris Mason1261ec42007-03-20 20:35:03 -04001171 struct btrfs_fs_info *info = root->fs_info;
1172 struct btrfs_root *extent_root = info->extent_root;
Chris Mason234b63a2007-03-13 10:46:10 -04001173 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -05001174
Chris Mason5f39d392007-10-15 16:14:19 -04001175 btrfs_set_stack_extent_refs(&extent_item, 1);
1176 btrfs_set_stack_extent_owner(&extent_item, owner);
Chris Masonfec577f2007-02-26 10:40:21 -05001177
Chris Mason26b80032007-08-08 20:17:12 -04001178 WARN_ON(num_blocks < 1);
Chris Mason6702ed42007-08-07 16:15:09 -04001179 ret = find_free_extent(trans, root, num_blocks, empty_size,
1180 search_start, search_end, hint_block, ins,
Chris Mason26b80032007-08-08 20:17:12 -04001181 trans->alloc_exclude_start,
1182 trans->alloc_exclude_nr, data);
Chris Masonccd467d2007-06-28 15:57:36 -04001183 BUG_ON(ret);
Chris Masonf2654de2007-06-26 12:20:46 -04001184 if (ret)
1185 return ret;
1186
Josef Bacik58176a92007-08-29 15:47:34 -04001187 /* block accounting for super block */
Chris Mason4b52dff2007-06-26 10:06:50 -04001188 super_blocks_used = btrfs_super_blocks_used(&info->super_copy);
1189 btrfs_set_super_blocks_used(&info->super_copy, super_blocks_used +
Chris Mason1261ec42007-03-20 20:35:03 -04001190 num_blocks);
Chris Mason26b80032007-08-08 20:17:12 -04001191
Josef Bacik58176a92007-08-29 15:47:34 -04001192 /* block accounting for root item */
Chris Mason5f39d392007-10-15 16:14:19 -04001193 root_blocks_used = btrfs_root_used(&root->root_item);
1194 btrfs_set_root_used(&root->root_item, root_blocks_used +
Josef Bacik58176a92007-08-29 15:47:34 -04001195 num_blocks);
1196
Chris Masonf510cfe2007-10-15 16:14:48 -04001197 clear_extent_dirty(&root->fs_info->free_space_cache,
1198 ins->objectid, ins->objectid + ins->offset - 1,
1199 GFP_NOFS);
1200
Chris Mason26b80032007-08-08 20:17:12 -04001201 if (root == extent_root) {
1202 BUG_ON(num_blocks != 1);
1203 set_radix_bit(&root->fs_info->extent_ins_radix, ins->objectid);
1204 goto update_block;
1205 }
1206
1207 WARN_ON(trans->alloc_exclude_nr);
1208 trans->alloc_exclude_start = ins->objectid;
1209 trans->alloc_exclude_nr = ins->offset;
Chris Masone089f052007-03-16 16:20:31 -04001210 ret = btrfs_insert_item(trans, extent_root, ins, &extent_item,
1211 sizeof(extent_item));
Chris Mason037e6392007-03-07 11:50:24 -05001212
Chris Mason26b80032007-08-08 20:17:12 -04001213 trans->alloc_exclude_start = 0;
1214 trans->alloc_exclude_nr = 0;
1215
Chris Masonccd467d2007-06-28 15:57:36 -04001216 BUG_ON(ret);
Chris Masone089f052007-03-16 16:20:31 -04001217 finish_current_insert(trans, extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -04001218 pending_ret = del_pending_extents(trans, extent_root);
Chris Masonf510cfe2007-10-15 16:14:48 -04001219
Chris Masone37c9e62007-05-09 20:13:14 -04001220 if (ret) {
Chris Mason037e6392007-03-07 11:50:24 -05001221 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -04001222 }
1223 if (pending_ret) {
Chris Mason037e6392007-03-07 11:50:24 -05001224 return pending_ret;
Chris Masone37c9e62007-05-09 20:13:14 -04001225 }
Chris Mason26b80032007-08-08 20:17:12 -04001226
1227update_block:
Chris Mason1e2677e2007-05-29 16:52:18 -04001228 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0,
1229 data);
Chris Masonfabb5682007-06-07 22:13:21 -04001230 BUG_ON(ret);
Chris Mason037e6392007-03-07 11:50:24 -05001231 return 0;
Chris Masonfec577f2007-02-26 10:40:21 -05001232}
1233
1234/*
1235 * helper function to allocate a block for a given tree
1236 * returns the tree buffer or NULL.
1237 */
Chris Mason5f39d392007-10-15 16:14:19 -04001238struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
1239 struct btrfs_root *root, u64 hint,
1240 u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05001241{
Chris Masone2fa7222007-03-12 16:22:34 -04001242 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -05001243 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04001244 struct extent_buffer *buf;
Chris Masonfec577f2007-02-26 10:40:21 -05001245
Chris Mason4d775672007-04-20 20:23:12 -04001246 ret = btrfs_alloc_extent(trans, root, root->root_key.objectid,
Yane9fe3952007-08-29 09:11:44 -04001247 1, empty_size, hint, (u64)-1, &ins, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05001248 if (ret) {
Chris Mason54aa1f42007-06-22 14:16:25 -04001249 BUG_ON(ret > 0);
1250 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05001251 }
Chris Masond98237b2007-03-28 13:57:48 -04001252 buf = btrfs_find_create_tree_block(root, ins.objectid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001253 if (!buf) {
1254 btrfs_free_extent(trans, root, ins.objectid, 1, 0);
1255 return ERR_PTR(-ENOMEM);
1256 }
Chris Mason5f39d392007-10-15 16:14:19 -04001257 btrfs_set_buffer_uptodate(buf);
Chris Masonf510cfe2007-10-15 16:14:48 -04001258 buf->alloc_addr = (unsigned long)__builtin_return_address(0);
Chris Mason5f39d392007-10-15 16:14:19 -04001259 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
1260 buf->start + buf->len - 1, GFP_NOFS);
1261 /*
Chris Mason090d1872007-05-01 08:53:32 -04001262 set_buffer_checked(buf);
Chris Masonf2183bd2007-08-10 14:42:37 -04001263 set_buffer_defrag(buf);
Chris Mason5f39d392007-10-15 16:14:19 -04001264 */
1265 /* FIXME!!!!!!!!!!!!!!!!
1266 set_radix_bit(&trans->transaction->dirty_pages, buf->pages[0]->index);
1267 */
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001268 trans->blocks_used++;
Chris Masonfec577f2007-02-26 10:40:21 -05001269 return buf;
1270}
Chris Masona28ec192007-03-06 20:08:01 -05001271
Chris Mason6407bf62007-03-27 06:33:00 -04001272static int drop_leaf_ref(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001273 struct btrfs_root *root, struct extent_buffer *leaf)
Chris Mason6407bf62007-03-27 06:33:00 -04001274{
Chris Mason5f39d392007-10-15 16:14:19 -04001275 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -04001276 struct btrfs_file_extent_item *fi;
1277 int i;
1278 int nritems;
1279 int ret;
1280
Chris Mason5f39d392007-10-15 16:14:19 -04001281 BUG_ON(!btrfs_is_leaf(leaf));
1282 nritems = btrfs_header_nritems(leaf);
Chris Mason6407bf62007-03-27 06:33:00 -04001283 for (i = 0; i < nritems; i++) {
Chris Mason3a686372007-05-24 13:35:57 -04001284 u64 disk_blocknr;
Chris Mason5f39d392007-10-15 16:14:19 -04001285
1286 btrfs_item_key_to_cpu(leaf, &key, i);
1287 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason6407bf62007-03-27 06:33:00 -04001288 continue;
1289 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001290 if (btrfs_file_extent_type(leaf, fi) ==
1291 BTRFS_FILE_EXTENT_INLINE)
Chris Mason236454d2007-04-19 13:37:44 -04001292 continue;
Chris Mason6407bf62007-03-27 06:33:00 -04001293 /*
1294 * FIXME make sure to insert a trans record that
1295 * repeats the snapshot del on crash
1296 */
Chris Mason5f39d392007-10-15 16:14:19 -04001297 disk_blocknr = btrfs_file_extent_disk_blocknr(leaf, fi);
Chris Mason3a686372007-05-24 13:35:57 -04001298 if (disk_blocknr == 0)
1299 continue;
1300 ret = btrfs_free_extent(trans, root, disk_blocknr,
Chris Mason5f39d392007-10-15 16:14:19 -04001301 btrfs_file_extent_disk_num_blocks(leaf, fi), 0);
Chris Mason6407bf62007-03-27 06:33:00 -04001302 BUG_ON(ret);
1303 }
1304 return 0;
1305}
1306
Chris Masone0115992007-06-19 16:23:05 -04001307static void reada_walk_down(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -04001308 struct extent_buffer *node)
Chris Masone0115992007-06-19 16:23:05 -04001309{
1310 int i;
1311 u32 nritems;
1312 u64 blocknr;
1313 int ret;
1314 u32 refs;
1315
Chris Mason5f39d392007-10-15 16:14:19 -04001316 nritems = btrfs_header_nritems(node);
Chris Masone0115992007-06-19 16:23:05 -04001317 for (i = 0; i < nritems; i++) {
1318 blocknr = btrfs_node_blockptr(node, i);
1319 ret = lookup_extent_ref(NULL, root, blocknr, 1, &refs);
1320 BUG_ON(ret);
1321 if (refs != 1)
1322 continue;
Chris Mason409eb952007-08-08 20:17:12 -04001323 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masone0115992007-06-19 16:23:05 -04001324 ret = readahead_tree_block(root, blocknr);
Chris Mason409eb952007-08-08 20:17:12 -04001325 cond_resched();
1326 mutex_lock(&root->fs_info->fs_mutex);
Chris Masone0115992007-06-19 16:23:05 -04001327 if (ret)
1328 break;
1329 }
1330}
1331
Chris Mason9aca1d52007-03-13 11:09:37 -04001332/*
1333 * helper function for drop_snapshot, this walks down the tree dropping ref
1334 * counts as it goes.
1335 */
Chris Masone089f052007-03-16 16:20:31 -04001336static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
1337 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05001338{
Chris Mason5f39d392007-10-15 16:14:19 -04001339 struct extent_buffer *next;
1340 struct extent_buffer *cur;
Chris Mason20524f02007-03-10 06:35:47 -05001341 u64 blocknr;
1342 int ret;
1343 u32 refs;
1344
Chris Mason5caf2a02007-04-02 11:20:42 -04001345 WARN_ON(*level < 0);
1346 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason5f39d392007-10-15 16:14:19 -04001347 ret = lookup_extent_ref(trans, root,
1348 extent_buffer_blocknr(path->nodes[*level]),
1349 1, &refs);
Chris Mason20524f02007-03-10 06:35:47 -05001350 BUG_ON(ret);
1351 if (refs > 1)
1352 goto out;
Chris Masone0115992007-06-19 16:23:05 -04001353
Chris Mason9aca1d52007-03-13 11:09:37 -04001354 /*
1355 * walk down to the last node level and free all the leaves
1356 */
Chris Mason6407bf62007-03-27 06:33:00 -04001357 while(*level >= 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001358 WARN_ON(*level < 0);
1359 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason20524f02007-03-10 06:35:47 -05001360 cur = path->nodes[*level];
Chris Masone0115992007-06-19 16:23:05 -04001361
1362 if (*level > 0 && path->slots[*level] == 0)
Chris Mason5f39d392007-10-15 16:14:19 -04001363 reada_walk_down(root, cur);
Chris Masone0115992007-06-19 16:23:05 -04001364
Chris Mason5f39d392007-10-15 16:14:19 -04001365 if (btrfs_header_level(cur) != *level)
Chris Mason2c90e5d2007-04-02 10:50:19 -04001366 WARN_ON(1);
Chris Masone0115992007-06-19 16:23:05 -04001367
Chris Mason7518a232007-03-12 12:01:18 -04001368 if (path->slots[*level] >=
Chris Mason5f39d392007-10-15 16:14:19 -04001369 btrfs_header_nritems(cur))
Chris Mason20524f02007-03-10 06:35:47 -05001370 break;
Chris Mason6407bf62007-03-27 06:33:00 -04001371 if (*level == 0) {
1372 ret = drop_leaf_ref(trans, root, cur);
1373 BUG_ON(ret);
1374 break;
1375 }
Chris Mason5f39d392007-10-15 16:14:19 -04001376 blocknr = btrfs_node_blockptr(cur, path->slots[*level]);
Chris Masonb18c6682007-04-17 13:26:50 -04001377 ret = lookup_extent_ref(trans, root, blocknr, 1, &refs);
Chris Mason6407bf62007-03-27 06:33:00 -04001378 BUG_ON(ret);
1379 if (refs != 1) {
Chris Mason20524f02007-03-10 06:35:47 -05001380 path->slots[*level]++;
Chris Masone089f052007-03-16 16:20:31 -04001381 ret = btrfs_free_extent(trans, root, blocknr, 1, 1);
Chris Mason20524f02007-03-10 06:35:47 -05001382 BUG_ON(ret);
1383 continue;
1384 }
Chris Masone9d0b132007-08-10 14:06:19 -04001385 next = btrfs_find_tree_block(root, blocknr);
Chris Mason5f39d392007-10-15 16:14:19 -04001386 if (!next || !btrfs_buffer_uptodate(next)) {
1387 free_extent_buffer(next);
Chris Masone9d0b132007-08-10 14:06:19 -04001388 mutex_unlock(&root->fs_info->fs_mutex);
1389 next = read_tree_block(root, blocknr);
1390 mutex_lock(&root->fs_info->fs_mutex);
1391
1392 /* we dropped the lock, check one more time */
1393 ret = lookup_extent_ref(trans, root, blocknr, 1, &refs);
1394 BUG_ON(ret);
1395 if (refs != 1) {
1396 path->slots[*level]++;
Chris Mason5f39d392007-10-15 16:14:19 -04001397 free_extent_buffer(next);
Chris Masone9d0b132007-08-10 14:06:19 -04001398 ret = btrfs_free_extent(trans, root,
1399 blocknr, 1, 1);
1400 BUG_ON(ret);
1401 continue;
1402 }
1403 }
Chris Mason5caf2a02007-04-02 11:20:42 -04001404 WARN_ON(*level <= 0);
Chris Mason83e15a22007-03-12 09:03:27 -04001405 if (path->nodes[*level-1])
Chris Mason5f39d392007-10-15 16:14:19 -04001406 free_extent_buffer(path->nodes[*level-1]);
Chris Mason20524f02007-03-10 06:35:47 -05001407 path->nodes[*level-1] = next;
Chris Mason5f39d392007-10-15 16:14:19 -04001408 *level = btrfs_header_level(next);
Chris Mason20524f02007-03-10 06:35:47 -05001409 path->slots[*level] = 0;
1410 }
1411out:
Chris Mason5caf2a02007-04-02 11:20:42 -04001412 WARN_ON(*level < 0);
1413 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason6407bf62007-03-27 06:33:00 -04001414 ret = btrfs_free_extent(trans, root,
Chris Mason5f39d392007-10-15 16:14:19 -04001415 extent_buffer_blocknr(path->nodes[*level]), 1, 1);
1416 free_extent_buffer(path->nodes[*level]);
Chris Mason20524f02007-03-10 06:35:47 -05001417 path->nodes[*level] = NULL;
1418 *level += 1;
1419 BUG_ON(ret);
1420 return 0;
1421}
1422
Chris Mason9aca1d52007-03-13 11:09:37 -04001423/*
1424 * helper for dropping snapshots. This walks back up the tree in the path
1425 * to find the first node higher up where we haven't yet gone through
1426 * all the slots
1427 */
Chris Masone089f052007-03-16 16:20:31 -04001428static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root
1429 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05001430{
1431 int i;
1432 int slot;
1433 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04001434 struct btrfs_root_item *root_item = &root->root_item;
1435
Chris Mason234b63a2007-03-13 10:46:10 -04001436 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Mason20524f02007-03-10 06:35:47 -05001437 slot = path->slots[i];
Chris Mason5f39d392007-10-15 16:14:19 -04001438 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
1439 struct extent_buffer *node;
1440 struct btrfs_disk_key disk_key;
1441 node = path->nodes[i];
Chris Mason20524f02007-03-10 06:35:47 -05001442 path->slots[i]++;
1443 *level = i;
Chris Mason9f3a7422007-08-07 15:52:19 -04001444 WARN_ON(*level == 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001445 btrfs_node_key(node, &disk_key, path->slots[i]);
Chris Mason9f3a7422007-08-07 15:52:19 -04001446 memcpy(&root_item->drop_progress,
Chris Mason5f39d392007-10-15 16:14:19 -04001447 &disk_key, sizeof(disk_key));
Chris Mason9f3a7422007-08-07 15:52:19 -04001448 root_item->drop_level = i;
Chris Mason20524f02007-03-10 06:35:47 -05001449 return 0;
1450 } else {
Chris Masone089f052007-03-16 16:20:31 -04001451 ret = btrfs_free_extent(trans, root,
Chris Mason5f39d392007-10-15 16:14:19 -04001452 extent_buffer_blocknr(path->nodes[*level]),
1453 1, 1);
Chris Mason6407bf62007-03-27 06:33:00 -04001454 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04001455 free_extent_buffer(path->nodes[*level]);
Chris Mason83e15a22007-03-12 09:03:27 -04001456 path->nodes[*level] = NULL;
Chris Mason20524f02007-03-10 06:35:47 -05001457 *level = i + 1;
Chris Mason20524f02007-03-10 06:35:47 -05001458 }
1459 }
1460 return 1;
1461}
1462
Chris Mason9aca1d52007-03-13 11:09:37 -04001463/*
1464 * drop the reference count on the tree rooted at 'snap'. This traverses
1465 * the tree freeing any blocks that have a ref count of zero after being
1466 * decremented.
1467 */
Chris Masone089f052007-03-16 16:20:31 -04001468int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason9f3a7422007-08-07 15:52:19 -04001469 *root)
Chris Mason20524f02007-03-10 06:35:47 -05001470{
Chris Mason3768f362007-03-13 16:47:54 -04001471 int ret = 0;
Chris Mason9aca1d52007-03-13 11:09:37 -04001472 int wret;
Chris Mason20524f02007-03-10 06:35:47 -05001473 int level;
Chris Mason5caf2a02007-04-02 11:20:42 -04001474 struct btrfs_path *path;
Chris Mason20524f02007-03-10 06:35:47 -05001475 int i;
1476 int orig_level;
Chris Mason9f3a7422007-08-07 15:52:19 -04001477 struct btrfs_root_item *root_item = &root->root_item;
Chris Mason20524f02007-03-10 06:35:47 -05001478
Chris Mason5caf2a02007-04-02 11:20:42 -04001479 path = btrfs_alloc_path();
1480 BUG_ON(!path);
Chris Mason20524f02007-03-10 06:35:47 -05001481
Chris Mason5f39d392007-10-15 16:14:19 -04001482 level = btrfs_header_level(root->node);
Chris Mason20524f02007-03-10 06:35:47 -05001483 orig_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04001484 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
1485 path->nodes[level] = root->node;
Chris Masonf510cfe2007-10-15 16:14:48 -04001486 extent_buffer_get(root->node);
Chris Mason9f3a7422007-08-07 15:52:19 -04001487 path->slots[level] = 0;
1488 } else {
1489 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001490 struct btrfs_disk_key found_key;
1491 struct extent_buffer *node;
Chris Mason6702ed42007-08-07 16:15:09 -04001492
Chris Mason9f3a7422007-08-07 15:52:19 -04001493 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Chris Mason6702ed42007-08-07 16:15:09 -04001494 level = root_item->drop_level;
1495 path->lowest_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04001496 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason6702ed42007-08-07 16:15:09 -04001497 if (wret < 0) {
Chris Mason9f3a7422007-08-07 15:52:19 -04001498 ret = wret;
1499 goto out;
1500 }
Chris Mason5f39d392007-10-15 16:14:19 -04001501 node = path->nodes[level];
1502 btrfs_node_key(node, &found_key, path->slots[level]);
1503 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
1504 sizeof(found_key)));
Chris Mason9f3a7422007-08-07 15:52:19 -04001505 }
Chris Mason20524f02007-03-10 06:35:47 -05001506 while(1) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001507 wret = walk_down_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04001508 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05001509 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04001510 if (wret < 0)
1511 ret = wret;
1512
Chris Mason5caf2a02007-04-02 11:20:42 -04001513 wret = walk_up_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04001514 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05001515 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04001516 if (wret < 0)
1517 ret = wret;
Chris Mason409eb952007-08-08 20:17:12 -04001518 ret = -EAGAIN;
Chris Mason409eb952007-08-08 20:17:12 -04001519 break;
Chris Mason20524f02007-03-10 06:35:47 -05001520 }
Chris Mason83e15a22007-03-12 09:03:27 -04001521 for (i = 0; i <= orig_level; i++) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001522 if (path->nodes[i]) {
Chris Mason5f39d392007-10-15 16:14:19 -04001523 free_extent_buffer(path->nodes[i]);
Chris Mason6702ed42007-08-07 16:15:09 -04001524 path->nodes[i] = 0;
Chris Mason83e15a22007-03-12 09:03:27 -04001525 }
Chris Mason20524f02007-03-10 06:35:47 -05001526 }
Chris Mason9f3a7422007-08-07 15:52:19 -04001527out:
Chris Mason5caf2a02007-04-02 11:20:42 -04001528 btrfs_free_path(path);
Chris Mason9aca1d52007-03-13 11:09:37 -04001529 return ret;
Chris Mason20524f02007-03-10 06:35:47 -05001530}
Chris Mason9078a3e2007-04-26 16:46:15 -04001531
Chris Masonbe744172007-05-06 10:15:01 -04001532static int free_block_group_radix(struct radix_tree_root *radix)
Chris Mason9078a3e2007-04-26 16:46:15 -04001533{
1534 int ret;
1535 struct btrfs_block_group_cache *cache[8];
1536 int i;
1537
1538 while(1) {
Chris Masonbe744172007-05-06 10:15:01 -04001539 ret = radix_tree_gang_lookup(radix, (void **)cache, 0,
Chris Mason9078a3e2007-04-26 16:46:15 -04001540 ARRAY_SIZE(cache));
1541 if (!ret)
1542 break;
1543 for (i = 0; i < ret; i++) {
Chris Masonbe744172007-05-06 10:15:01 -04001544 radix_tree_delete(radix, cache[i]->key.objectid +
Chris Mason9078a3e2007-04-26 16:46:15 -04001545 cache[i]->key.offset - 1);
1546 kfree(cache[i]);
1547 }
1548 }
1549 return 0;
1550}
1551
Chris Masonbe744172007-05-06 10:15:01 -04001552int btrfs_free_block_groups(struct btrfs_fs_info *info)
1553{
1554 int ret;
1555 int ret2;
Chris Masonf510cfe2007-10-15 16:14:48 -04001556 u64 start;
1557 u64 end;
Chris Masonbe744172007-05-06 10:15:01 -04001558
1559 ret = free_block_group_radix(&info->block_group_radix);
1560 ret2 = free_block_group_radix(&info->block_group_data_radix);
1561 if (ret)
1562 return ret;
1563 if (ret2)
1564 return ret2;
Chris Masone37c9e62007-05-09 20:13:14 -04001565
1566 while(1) {
Chris Masonf510cfe2007-10-15 16:14:48 -04001567 ret = find_first_extent_bit(&info->free_space_cache, 0,
1568 &start, &end, EXTENT_DIRTY);
1569 if (ret)
Chris Masone37c9e62007-05-09 20:13:14 -04001570 break;
Chris Masonf510cfe2007-10-15 16:14:48 -04001571 clear_extent_dirty(&info->free_space_cache, start,
1572 end, GFP_NOFS);
Chris Masone37c9e62007-05-09 20:13:14 -04001573 }
Chris Masonbe744172007-05-06 10:15:01 -04001574 return 0;
1575}
1576
Chris Mason9078a3e2007-04-26 16:46:15 -04001577int btrfs_read_block_groups(struct btrfs_root *root)
1578{
1579 struct btrfs_path *path;
1580 int ret;
1581 int err = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04001582 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04001583 struct btrfs_fs_info *info = root->fs_info;
1584 struct radix_tree_root *radix;
Chris Mason9078a3e2007-04-26 16:46:15 -04001585 struct btrfs_key key;
1586 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -04001587 struct extent_buffer *leaf;
Chris Mason84f54cf2007-06-12 07:43:08 -04001588 u64 group_size_blocks;
Chris Mason31f3c992007-04-30 15:25:45 -04001589 u64 used;
Chris Mason9078a3e2007-04-26 16:46:15 -04001590
Chris Mason84f54cf2007-06-12 07:43:08 -04001591 group_size_blocks = BTRFS_BLOCK_GROUP_SIZE >>
1592 root->fs_info->sb->s_blocksize_bits;
Chris Masonbe744172007-05-06 10:15:01 -04001593 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04001594 key.objectid = 0;
1595 key.offset = group_size_blocks;
Chris Mason9078a3e2007-04-26 16:46:15 -04001596 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
1597
1598 path = btrfs_alloc_path();
1599 if (!path)
1600 return -ENOMEM;
1601
1602 while(1) {
Chris Masonbe744172007-05-06 10:15:01 -04001603 ret = btrfs_search_slot(NULL, info->extent_root,
Chris Mason9078a3e2007-04-26 16:46:15 -04001604 &key, path, 0, 0);
1605 if (ret != 0) {
1606 err = ret;
1607 break;
1608 }
Chris Mason5f39d392007-10-15 16:14:19 -04001609 leaf = path->nodes[0];
1610 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason9078a3e2007-04-26 16:46:15 -04001611 cache = kmalloc(sizeof(*cache), GFP_NOFS);
1612 if (!cache) {
1613 err = -1;
1614 break;
1615 }
Chris Mason3e1ad542007-05-07 20:03:49 -04001616
Chris Mason5f39d392007-10-15 16:14:19 -04001617 read_extent_buffer(leaf, &cache->item,
1618 btrfs_item_ptr_offset(leaf, path->slots[0]),
1619 sizeof(cache->item));
1620 if (cache->item.flags & BTRFS_BLOCK_GROUP_DATA) {
Chris Mason1e2677e2007-05-29 16:52:18 -04001621 radix = &info->block_group_data_radix;
1622 cache->data = 1;
1623 } else {
1624 radix = &info->block_group_radix;
1625 cache->data = 0;
1626 }
1627
Chris Mason9078a3e2007-04-26 16:46:15 -04001628 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Mason31f3c992007-04-30 15:25:45 -04001629 cache->last_alloc = cache->key.objectid;
1630 cache->first_free = cache->key.objectid;
Chris Masonbe744172007-05-06 10:15:01 -04001631 cache->pinned = 0;
Chris Masone37c9e62007-05-09 20:13:14 -04001632 cache->cached = 0;
1633
Chris Mason3e1ad542007-05-07 20:03:49 -04001634 cache->radix = radix;
1635
Chris Mason9078a3e2007-04-26 16:46:15 -04001636 key.objectid = found_key.objectid + found_key.offset;
1637 btrfs_release_path(root, path);
Chris Mason5f39d392007-10-15 16:14:19 -04001638
Chris Masonbe744172007-05-06 10:15:01 -04001639 ret = radix_tree_insert(radix, found_key.objectid +
Chris Mason9078a3e2007-04-26 16:46:15 -04001640 found_key.offset - 1,
1641 (void *)cache);
1642 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04001643 used = btrfs_block_group_used(&cache->item);
Chris Mason84f54cf2007-06-12 07:43:08 -04001644 if (used < div_factor(key.offset, 8)) {
Chris Masonbe744172007-05-06 10:15:01 -04001645 radix_tree_tag_set(radix, found_key.objectid +
Chris Mason31f3c992007-04-30 15:25:45 -04001646 found_key.offset - 1,
1647 BTRFS_BLOCK_GROUP_AVAIL);
1648 }
Chris Mason9078a3e2007-04-26 16:46:15 -04001649 if (key.objectid >=
Chris Mason4b52dff2007-06-26 10:06:50 -04001650 btrfs_super_total_blocks(&info->super_copy))
Chris Mason9078a3e2007-04-26 16:46:15 -04001651 break;
1652 }
1653
1654 btrfs_free_path(path);
1655 return 0;
1656}