blob: 5d4d5d8db8efa3f7cc3a9e23bc98affee85aa215 [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 find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masonfbdc7622007-05-30 10:22:12 -040026 *orig_root, u64 num_blocks, u64 search_start,
27 u64 search_end, u64 hint_block,
Chris Masonf2654de2007-06-26 12:20:46 -040028 struct btrfs_key *ins, u64 exclude_start,
29 u64 exclude_nr, int data);
Chris Masone089f052007-03-16 16:20:31 -040030static int finish_current_insert(struct btrfs_trans_handle *trans, struct
31 btrfs_root *extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -040032static int del_pending_extents(struct btrfs_trans_handle *trans, struct
33 btrfs_root *extent_root);
Chris Masonfec577f2007-02-26 10:40:21 -050034
Chris Masone37c9e62007-05-09 20:13:14 -040035static int cache_block_group(struct btrfs_root *root,
36 struct btrfs_block_group_cache *block_group)
37{
38 struct btrfs_path *path;
39 int ret;
40 struct btrfs_key key;
41 struct btrfs_leaf *leaf;
42 struct radix_tree_root *extent_radix;
43 int slot;
44 u64 i;
45 u64 last = 0;
46 u64 hole_size;
Chris Masonde428b62007-05-18 13:28:27 -040047 u64 limit;
Chris Masone37c9e62007-05-09 20:13:14 -040048 int found = 0;
49
50 root = root->fs_info->extent_root;
51 extent_radix = &root->fs_info->extent_map_radix;
52
53 if (block_group->cached)
54 return 0;
55 if (block_group->data)
56 return 0;
57 path = btrfs_alloc_path();
58 if (!path)
59 return -ENOMEM;
Chris Mason3c69fae2007-08-07 15:52:22 -040060 path->reada = 1;
Chris Masone37c9e62007-05-09 20:13:14 -040061 key.objectid = block_group->key.objectid;
62 key.flags = 0;
63 key.offset = 0;
64 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
65 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
66 if (ret < 0)
67 return ret;
68 if (ret && path->slots[0] > 0)
69 path->slots[0]--;
Chris Masonde428b62007-05-18 13:28:27 -040070 limit = block_group->key.objectid + block_group->key.offset;
Chris Masone37c9e62007-05-09 20:13:14 -040071 while(1) {
72 leaf = btrfs_buffer_leaf(path->nodes[0]);
73 slot = path->slots[0];
74 if (slot >= btrfs_header_nritems(&leaf->header)) {
75 ret = btrfs_next_leaf(root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -040076 if (ret < 0)
77 goto err;
Chris Masonde428b62007-05-18 13:28:27 -040078 if (ret == 0) {
Chris Masone37c9e62007-05-09 20:13:14 -040079 continue;
Chris Masonde428b62007-05-18 13:28:27 -040080 } else {
Chris Masone37c9e62007-05-09 20:13:14 -040081 if (found) {
82 hole_size = block_group->key.objectid +
83 block_group->key.offset - last;
84 } else {
85 last = block_group->key.objectid;
86 hole_size = block_group->key.offset;
87 }
88 for (i = 0; i < hole_size; i++) {
89 set_radix_bit(extent_radix,
90 last + i);
91 }
92 break;
93 }
94 }
95 btrfs_disk_key_to_cpu(&key, &leaf->items[slot].key);
96 if (key.objectid >= block_group->key.objectid +
97 block_group->key.offset) {
98 if (found) {
99 hole_size = block_group->key.objectid +
100 block_group->key.offset - last;
101 } else {
102 last = block_group->key.objectid;
103 hole_size = block_group->key.offset;
104 }
105 for (i = 0; i < hole_size; i++) {
106 set_radix_bit(extent_radix, last + i);
107 }
108 break;
109 }
110 if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) {
111 if (!found) {
112 last = key.objectid + key.offset;
113 found = 1;
114 } else {
115 hole_size = key.objectid - last;
116 for (i = 0; i < hole_size; i++) {
117 set_radix_bit(extent_radix, last + i);
118 }
119 last = key.objectid + key.offset;
120 }
121 }
122 path->slots[0]++;
123 }
124
125 block_group->cached = 1;
Chris Mason54aa1f42007-06-22 14:16:25 -0400126err:
Chris Masone37c9e62007-05-09 20:13:14 -0400127 btrfs_free_path(path);
128 return 0;
129}
130
Chris Mason5276aed2007-06-11 21:33:38 -0400131struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
132 btrfs_fs_info *info,
133 u64 blocknr)
Chris Masonbe744172007-05-06 10:15:01 -0400134{
135 struct btrfs_block_group_cache *block_group;
136 int ret;
137
138 ret = radix_tree_gang_lookup(&info->block_group_radix,
139 (void **)&block_group,
140 blocknr, 1);
141 if (ret) {
Chris Mason3e1ad542007-05-07 20:03:49 -0400142 if (block_group->key.objectid <= blocknr && blocknr <=
Chris Masonbe744172007-05-06 10:15:01 -0400143 block_group->key.objectid + block_group->key.offset)
144 return block_group;
145 }
146 ret = radix_tree_gang_lookup(&info->block_group_data_radix,
147 (void **)&block_group,
148 blocknr, 1);
149 if (ret) {
Chris Mason3e1ad542007-05-07 20:03:49 -0400150 if (block_group->key.objectid <= blocknr && blocknr <=
Chris Masonbe744172007-05-06 10:15:01 -0400151 block_group->key.objectid + block_group->key.offset)
152 return block_group;
153 }
Chris Masonbe744172007-05-06 10:15:01 -0400154 return NULL;
155}
156
Chris Masone37c9e62007-05-09 20:13:14 -0400157static u64 leaf_range(struct btrfs_root *root)
158{
159 u64 size = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason84f54cf2007-06-12 07:43:08 -0400160 do_div(size, sizeof(struct btrfs_extent_item) +
161 sizeof(struct btrfs_item));
Chris Masone37c9e62007-05-09 20:13:14 -0400162 return size;
163}
164
165static u64 find_search_start(struct btrfs_root *root,
166 struct btrfs_block_group_cache **cache_ret,
167 u64 search_start, int num)
168{
169 unsigned long gang[8];
170 int ret;
171 struct btrfs_block_group_cache *cache = *cache_ret;
172 u64 last = max(search_start, cache->key.objectid);
173
174 if (cache->data)
175 goto out;
176 if (num > 1) {
177 last = max(last, cache->last_prealloc);
178 }
179again:
Chris Mason54aa1f42007-06-22 14:16:25 -0400180 ret = cache_block_group(root, cache);
181 if (ret)
182 goto out;
Chris Masone37c9e62007-05-09 20:13:14 -0400183 while(1) {
184 ret = find_first_radix_bit(&root->fs_info->extent_map_radix,
185 gang, last, ARRAY_SIZE(gang));
186 if (!ret)
187 goto out;
188 last = gang[ret-1] + 1;
189 if (num > 1) {
190 if (ret != ARRAY_SIZE(gang)) {
191 goto new_group;
192 }
193 if (gang[ret-1] - gang[0] > leaf_range(root)) {
194 continue;
195 }
196 }
197 if (gang[0] >= cache->key.objectid + cache->key.offset) {
198 goto new_group;
199 }
200 return gang[0];
201 }
202out:
203 return max(cache->last_alloc, search_start);
204
205new_group:
Chris Mason5276aed2007-06-11 21:33:38 -0400206 cache = btrfs_lookup_block_group(root->fs_info,
207 last + cache->key.offset - 1);
Chris Masone37c9e62007-05-09 20:13:14 -0400208 if (!cache) {
209 return max((*cache_ret)->last_alloc, search_start);
210 }
211 cache = btrfs_find_block_group(root, cache,
Chris Masonde428b62007-05-18 13:28:27 -0400212 last + cache->key.offset - 1, 0, 0);
Chris Masone37c9e62007-05-09 20:13:14 -0400213 *cache_ret = cache;
214 goto again;
215}
216
Chris Mason84f54cf2007-06-12 07:43:08 -0400217static u64 div_factor(u64 num, int factor)
218{
219 num *= factor;
220 do_div(num, 10);
221 return num;
222}
223
Chris Mason31f3c992007-04-30 15:25:45 -0400224struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
225 struct btrfs_block_group_cache
Chris Masonbe744172007-05-06 10:15:01 -0400226 *hint, u64 search_start,
Chris Masonde428b62007-05-18 13:28:27 -0400227 int data, int owner)
Chris Masoncd1bc462007-04-27 10:08:34 -0400228{
229 struct btrfs_block_group_cache *cache[8];
Chris Mason31f3c992007-04-30 15:25:45 -0400230 struct btrfs_block_group_cache *found_group = NULL;
Chris Masoncd1bc462007-04-27 10:08:34 -0400231 struct btrfs_fs_info *info = root->fs_info;
Chris Masonbe744172007-05-06 10:15:01 -0400232 struct radix_tree_root *radix;
Chris Mason1e2677e2007-05-29 16:52:18 -0400233 struct radix_tree_root *swap_radix;
Chris Masoncd1bc462007-04-27 10:08:34 -0400234 u64 used;
Chris Mason31f3c992007-04-30 15:25:45 -0400235 u64 last = 0;
236 u64 hint_last;
Chris Masoncd1bc462007-04-27 10:08:34 -0400237 int i;
238 int ret;
Chris Mason31f3c992007-04-30 15:25:45 -0400239 int full_search = 0;
Chris Masonde428b62007-05-18 13:28:27 -0400240 int factor = 8;
Chris Mason1e2677e2007-05-29 16:52:18 -0400241 int data_swap = 0;
Chris Masonde428b62007-05-18 13:28:27 -0400242
243 if (!owner)
244 factor = 5;
Chris Masonbe744172007-05-06 10:15:01 -0400245
Chris Mason1e2677e2007-05-29 16:52:18 -0400246 if (data) {
Chris Masonbe744172007-05-06 10:15:01 -0400247 radix = &info->block_group_data_radix;
Chris Mason1e2677e2007-05-29 16:52:18 -0400248 swap_radix = &info->block_group_radix;
249 } else {
Chris Masonbe744172007-05-06 10:15:01 -0400250 radix = &info->block_group_radix;
Chris Mason1e2677e2007-05-29 16:52:18 -0400251 swap_radix = &info->block_group_data_radix;
252 }
Chris Masonbe744172007-05-06 10:15:01 -0400253
254 if (search_start) {
255 struct btrfs_block_group_cache *shint;
Chris Mason5276aed2007-06-11 21:33:38 -0400256 shint = btrfs_lookup_block_group(info, search_start);
Chris Masonbe744172007-05-06 10:15:01 -0400257 if (shint->data == data) {
258 used = btrfs_block_group_used(&shint->item);
259 if (used + shint->pinned <
Chris Mason84f54cf2007-06-12 07:43:08 -0400260 div_factor(shint->key.offset, factor)) {
Chris Masonbe744172007-05-06 10:15:01 -0400261 return shint;
262 }
263 }
264 }
265 if (hint && hint->data == data) {
Chris Mason31f3c992007-04-30 15:25:45 -0400266 used = btrfs_block_group_used(&hint->item);
Chris Mason84f54cf2007-06-12 07:43:08 -0400267 if (used + hint->pinned <
268 div_factor(hint->key.offset, factor)) {
Chris Mason31f3c992007-04-30 15:25:45 -0400269 return hint;
270 }
Chris Mason84f54cf2007-06-12 07:43:08 -0400271 if (used >= div_factor(hint->key.offset, 8)) {
Chris Masonbe744172007-05-06 10:15:01 -0400272 radix_tree_tag_clear(radix,
273 hint->key.objectid +
274 hint->key.offset - 1,
275 BTRFS_BLOCK_GROUP_AVAIL);
276 }
Chris Mason8d7be552007-05-10 11:24:42 -0400277 last = hint->key.offset * 3;
Chris Masonbe744172007-05-06 10:15:01 -0400278 if (hint->key.objectid >= last)
Chris Masone37c9e62007-05-09 20:13:14 -0400279 last = max(search_start + hint->key.offset - 1,
280 hint->key.objectid - last);
Chris Masonbe744172007-05-06 10:15:01 -0400281 else
282 last = hint->key.objectid + hint->key.offset;
Chris Mason31f3c992007-04-30 15:25:45 -0400283 hint_last = last;
284 } else {
Chris Masone37c9e62007-05-09 20:13:14 -0400285 if (hint)
286 hint_last = max(hint->key.objectid, search_start);
287 else
288 hint_last = search_start;
289
290 last = hint_last;
Chris Mason31f3c992007-04-30 15:25:45 -0400291 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400292 while(1) {
Chris Masonbe744172007-05-06 10:15:01 -0400293 ret = radix_tree_gang_lookup_tag(radix, (void **)cache,
Chris Masoncd1bc462007-04-27 10:08:34 -0400294 last, ARRAY_SIZE(cache),
Chris Mason31f3c992007-04-30 15:25:45 -0400295 BTRFS_BLOCK_GROUP_AVAIL);
Chris Masoncd1bc462007-04-27 10:08:34 -0400296 if (!ret)
297 break;
298 for (i = 0; i < ret; i++) {
Chris Masonbe08c1b2007-05-03 09:06:49 -0400299 last = cache[i]->key.objectid +
300 cache[i]->key.offset;
Chris Masoncd1bc462007-04-27 10:08:34 -0400301 used = btrfs_block_group_used(&cache[i]->item);
Chris Masonbe744172007-05-06 10:15:01 -0400302 if (used + cache[i]->pinned <
Chris Mason84f54cf2007-06-12 07:43:08 -0400303 div_factor(cache[i]->key.offset, factor)) {
Chris Mason31f3c992007-04-30 15:25:45 -0400304 found_group = cache[i];
305 goto found;
Chris Masoncd1bc462007-04-27 10:08:34 -0400306 }
Chris Mason84f54cf2007-06-12 07:43:08 -0400307 if (used >= div_factor(cache[i]->key.offset, 8)) {
Chris Masonbe744172007-05-06 10:15:01 -0400308 radix_tree_tag_clear(radix,
309 cache[i]->key.objectid +
310 cache[i]->key.offset - 1,
311 BTRFS_BLOCK_GROUP_AVAIL);
312 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400313 }
Chris Masonde428b62007-05-18 13:28:27 -0400314 cond_resched();
Chris Masoncd1bc462007-04-27 10:08:34 -0400315 }
Chris Mason31f3c992007-04-30 15:25:45 -0400316 last = hint_last;
317again:
Chris Masoncd1bc462007-04-27 10:08:34 -0400318 while(1) {
Chris Masonbe744172007-05-06 10:15:01 -0400319 ret = radix_tree_gang_lookup(radix, (void **)cache,
320 last, ARRAY_SIZE(cache));
Chris Masoncd1bc462007-04-27 10:08:34 -0400321 if (!ret)
322 break;
323 for (i = 0; i < ret; i++) {
Chris Masonbe08c1b2007-05-03 09:06:49 -0400324 last = cache[i]->key.objectid +
325 cache[i]->key.offset;
Chris Masoncd1bc462007-04-27 10:08:34 -0400326 used = btrfs_block_group_used(&cache[i]->item);
Chris Masonbe744172007-05-06 10:15:01 -0400327 if (used + cache[i]->pinned < cache[i]->key.offset) {
Chris Mason31f3c992007-04-30 15:25:45 -0400328 found_group = cache[i];
329 goto found;
Chris Masoncd1bc462007-04-27 10:08:34 -0400330 }
Chris Masonbe744172007-05-06 10:15:01 -0400331 if (used >= cache[i]->key.offset) {
332 radix_tree_tag_clear(radix,
333 cache[i]->key.objectid +
334 cache[i]->key.offset - 1,
335 BTRFS_BLOCK_GROUP_AVAIL);
336 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400337 }
Chris Masonde428b62007-05-18 13:28:27 -0400338 cond_resched();
Chris Masoncd1bc462007-04-27 10:08:34 -0400339 }
Chris Mason31f3c992007-04-30 15:25:45 -0400340 if (!full_search) {
Chris Masonbe744172007-05-06 10:15:01 -0400341 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -0400342 full_search = 1;
343 goto again;
344 }
Chris Mason1e2677e2007-05-29 16:52:18 -0400345 if (!data_swap) {
346 struct radix_tree_root *tmp = radix;
347 data_swap = 1;
348 radix = swap_radix;
349 swap_radix = tmp;
350 last = search_start;
351 goto again;
352 }
Chris Mason31f3c992007-04-30 15:25:45 -0400353 if (!found_group) {
Chris Masonbe744172007-05-06 10:15:01 -0400354 ret = radix_tree_gang_lookup(radix,
Chris Mason31f3c992007-04-30 15:25:45 -0400355 (void **)&found_group, 0, 1);
Chris Mason1e2677e2007-05-29 16:52:18 -0400356 if (ret == 0) {
357 ret = radix_tree_gang_lookup(swap_radix,
358 (void **)&found_group,
359 0, 1);
360 }
Chris Mason31f3c992007-04-30 15:25:45 -0400361 BUG_ON(ret != 1);
362 }
Chris Masonbe744172007-05-06 10:15:01 -0400363found:
Chris Mason31f3c992007-04-30 15:25:45 -0400364 return found_group;
Chris Masoncd1bc462007-04-27 10:08:34 -0400365}
366
Chris Masonb18c6682007-04-17 13:26:50 -0400367int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
368 struct btrfs_root *root,
369 u64 blocknr, u64 num_blocks)
Chris Mason02217ed2007-03-02 16:08:05 -0500370{
Chris Mason5caf2a02007-04-02 11:20:42 -0400371 struct btrfs_path *path;
Chris Mason02217ed2007-03-02 16:08:05 -0500372 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400373 struct btrfs_key key;
Chris Mason234b63a2007-03-13 10:46:10 -0400374 struct btrfs_leaf *l;
375 struct btrfs_extent_item *item;
Chris Masone2fa7222007-03-12 16:22:34 -0400376 struct btrfs_key ins;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400377 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500378
Chris Mason5caf2a02007-04-02 11:20:42 -0400379 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -0400380 if (!path)
381 return -ENOMEM;
382 ret = find_free_extent(trans, root->fs_info->extent_root, 0, 0,
Chris Masonf2654de2007-06-26 12:20:46 -0400383 (u64)-1, 0, &ins, 0, 0, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400384 if (ret) {
385 btrfs_free_path(path);
386 return ret;
387 }
Chris Mason02217ed2007-03-02 16:08:05 -0500388 key.objectid = blocknr;
389 key.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -0400390 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Mason6407bf62007-03-27 06:33:00 -0400391 key.offset = num_blocks;
Chris Mason5caf2a02007-04-02 11:20:42 -0400392 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -0400393 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400394 if (ret < 0)
395 return ret;
Chris Masona429e512007-04-18 16:15:28 -0400396 if (ret != 0) {
Chris Masona28ec192007-03-06 20:08:01 -0500397 BUG();
Chris Masona429e512007-04-18 16:15:28 -0400398 }
Chris Mason02217ed2007-03-02 16:08:05 -0500399 BUG_ON(ret != 0);
Chris Mason5caf2a02007-04-02 11:20:42 -0400400 l = btrfs_buffer_leaf(path->nodes[0]);
401 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400402 refs = btrfs_extent_refs(item);
403 btrfs_set_extent_refs(item, refs + 1);
Chris Mason5caf2a02007-04-02 11:20:42 -0400404 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masona28ec192007-03-06 20:08:01 -0500405
Chris Mason5caf2a02007-04-02 11:20:42 -0400406 btrfs_release_path(root->fs_info->extent_root, path);
407 btrfs_free_path(path);
Chris Mason9f5fae22007-03-20 14:38:32 -0400408 finish_current_insert(trans, root->fs_info->extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -0400409 del_pending_extents(trans, root->fs_info->extent_root);
Chris Mason02217ed2007-03-02 16:08:05 -0500410 return 0;
411}
412
Chris Masonb18c6682007-04-17 13:26:50 -0400413static int lookup_extent_ref(struct btrfs_trans_handle *trans,
414 struct btrfs_root *root, u64 blocknr,
415 u64 num_blocks, u32 *refs)
Chris Masona28ec192007-03-06 20:08:01 -0500416{
Chris Mason5caf2a02007-04-02 11:20:42 -0400417 struct btrfs_path *path;
Chris Masona28ec192007-03-06 20:08:01 -0500418 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400419 struct btrfs_key key;
Chris Mason234b63a2007-03-13 10:46:10 -0400420 struct btrfs_leaf *l;
421 struct btrfs_extent_item *item;
Chris Mason5caf2a02007-04-02 11:20:42 -0400422
423 path = btrfs_alloc_path();
Chris Masona28ec192007-03-06 20:08:01 -0500424 key.objectid = blocknr;
Chris Mason6407bf62007-03-27 06:33:00 -0400425 key.offset = num_blocks;
Chris Mason62e27492007-03-15 12:56:47 -0400426 key.flags = 0;
427 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Mason5caf2a02007-04-02 11:20:42 -0400428 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -0400429 0, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400430 if (ret < 0)
431 goto out;
Chris Masona28ec192007-03-06 20:08:01 -0500432 if (ret != 0)
433 BUG();
Chris Mason5caf2a02007-04-02 11:20:42 -0400434 l = btrfs_buffer_leaf(path->nodes[0]);
435 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400436 *refs = btrfs_extent_refs(item);
Chris Mason54aa1f42007-06-22 14:16:25 -0400437out:
Chris Mason5caf2a02007-04-02 11:20:42 -0400438 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -0500439 return 0;
440}
441
Chris Masonc5739bb2007-04-10 09:27:04 -0400442int btrfs_inc_root_ref(struct btrfs_trans_handle *trans,
443 struct btrfs_root *root)
444{
Chris Masonb18c6682007-04-17 13:26:50 -0400445 return btrfs_inc_extent_ref(trans, root, bh_blocknr(root->node), 1);
Chris Masonc5739bb2007-04-10 09:27:04 -0400446}
447
Chris Masone089f052007-03-16 16:20:31 -0400448int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Masone20d96d2007-03-22 12:13:20 -0400449 struct buffer_head *buf)
Chris Mason02217ed2007-03-02 16:08:05 -0500450{
451 u64 blocknr;
Chris Masone20d96d2007-03-22 12:13:20 -0400452 struct btrfs_node *buf_node;
Chris Mason6407bf62007-03-27 06:33:00 -0400453 struct btrfs_leaf *buf_leaf;
454 struct btrfs_disk_key *key;
455 struct btrfs_file_extent_item *fi;
Chris Mason02217ed2007-03-02 16:08:05 -0500456 int i;
Chris Mason6407bf62007-03-27 06:33:00 -0400457 int leaf;
458 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -0400459 int faili;
460 int err;
Chris Masona28ec192007-03-06 20:08:01 -0500461
Chris Mason3768f362007-03-13 16:47:54 -0400462 if (!root->ref_cows)
Chris Masona28ec192007-03-06 20:08:01 -0500463 return 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400464 buf_node = btrfs_buffer_node(buf);
Chris Mason6407bf62007-03-27 06:33:00 -0400465 leaf = btrfs_is_leaf(buf_node);
466 buf_leaf = btrfs_buffer_leaf(buf);
Chris Masone20d96d2007-03-22 12:13:20 -0400467 for (i = 0; i < btrfs_header_nritems(&buf_node->header); i++) {
Chris Mason6407bf62007-03-27 06:33:00 -0400468 if (leaf) {
Chris Mason3a686372007-05-24 13:35:57 -0400469 u64 disk_blocknr;
Chris Mason6407bf62007-03-27 06:33:00 -0400470 key = &buf_leaf->items[i].key;
471 if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY)
472 continue;
473 fi = btrfs_item_ptr(buf_leaf, i,
474 struct btrfs_file_extent_item);
Chris Mason236454df2007-04-19 13:37:44 -0400475 if (btrfs_file_extent_type(fi) ==
476 BTRFS_FILE_EXTENT_INLINE)
477 continue;
Chris Mason3a686372007-05-24 13:35:57 -0400478 disk_blocknr = btrfs_file_extent_disk_blocknr(fi);
479 if (disk_blocknr == 0)
480 continue;
481 ret = btrfs_inc_extent_ref(trans, root, disk_blocknr,
Chris Mason6407bf62007-03-27 06:33:00 -0400482 btrfs_file_extent_disk_num_blocks(fi));
Chris Mason54aa1f42007-06-22 14:16:25 -0400483 if (ret) {
484 faili = i;
485 goto fail;
486 }
Chris Mason6407bf62007-03-27 06:33:00 -0400487 } else {
488 blocknr = btrfs_node_blockptr(buf_node, i);
Chris Masonb18c6682007-04-17 13:26:50 -0400489 ret = btrfs_inc_extent_ref(trans, root, blocknr, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400490 if (ret) {
491 faili = i;
492 goto fail;
493 }
Chris Mason6407bf62007-03-27 06:33:00 -0400494 }
Chris Mason02217ed2007-03-02 16:08:05 -0500495 }
496 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -0400497fail:
Chris Masonccd467d2007-06-28 15:57:36 -0400498 WARN_ON(1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400499 for (i =0; i < faili; i++) {
500 if (leaf) {
501 u64 disk_blocknr;
502 key = &buf_leaf->items[i].key;
503 if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY)
504 continue;
505 fi = btrfs_item_ptr(buf_leaf, i,
506 struct btrfs_file_extent_item);
507 if (btrfs_file_extent_type(fi) ==
508 BTRFS_FILE_EXTENT_INLINE)
509 continue;
510 disk_blocknr = btrfs_file_extent_disk_blocknr(fi);
511 if (disk_blocknr == 0)
512 continue;
513 err = btrfs_free_extent(trans, root, disk_blocknr,
514 btrfs_file_extent_disk_num_blocks(fi), 0);
515 BUG_ON(err);
516 } else {
517 blocknr = btrfs_node_blockptr(buf_node, i);
518 err = btrfs_free_extent(trans, root, blocknr, 1, 0);
519 BUG_ON(err);
520 }
521 }
522 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -0500523}
524
Chris Mason9078a3e2007-04-26 16:46:15 -0400525static int write_one_cache_group(struct btrfs_trans_handle *trans,
526 struct btrfs_root *root,
527 struct btrfs_path *path,
528 struct btrfs_block_group_cache *cache)
529{
530 int ret;
531 int pending_ret;
532 struct btrfs_root *extent_root = root->fs_info->extent_root;
533 struct btrfs_block_group_item *bi;
534 struct btrfs_key ins;
535
Chris Masonf2654de2007-06-26 12:20:46 -0400536 ret = find_free_extent(trans, extent_root, 0, 0, (u64)-1, 0, &ins,
537 0, 0, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400538 /* FIXME, set bit to recalc cache groups on next mount */
539 if (ret)
540 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -0400541 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400542 if (ret < 0)
543 goto fail;
Chris Mason9078a3e2007-04-26 16:46:15 -0400544 BUG_ON(ret);
545 bi = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
546 struct btrfs_block_group_item);
547 memcpy(bi, &cache->item, sizeof(*bi));
Chris Masonccd467d2007-06-28 15:57:36 -0400548 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason9078a3e2007-04-26 16:46:15 -0400549 btrfs_release_path(extent_root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -0400550fail:
Chris Mason9078a3e2007-04-26 16:46:15 -0400551 finish_current_insert(trans, extent_root);
552 pending_ret = del_pending_extents(trans, extent_root);
553 if (ret)
554 return ret;
555 if (pending_ret)
556 return pending_ret;
Chris Masonbe744172007-05-06 10:15:01 -0400557 if (cache->data)
558 cache->last_alloc = cache->first_free;
Chris Mason9078a3e2007-04-26 16:46:15 -0400559 return 0;
560
561}
562
Chris Masonbe744172007-05-06 10:15:01 -0400563static int write_dirty_block_radix(struct btrfs_trans_handle *trans,
564 struct btrfs_root *root,
565 struct radix_tree_root *radix)
Chris Mason9078a3e2007-04-26 16:46:15 -0400566{
567 struct btrfs_block_group_cache *cache[8];
568 int ret;
569 int err = 0;
570 int werr = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -0400571 int i;
572 struct btrfs_path *path;
Chris Mason54aa1f42007-06-22 14:16:25 -0400573 unsigned long off = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -0400574
575 path = btrfs_alloc_path();
576 if (!path)
577 return -ENOMEM;
578
579 while(1) {
580 ret = radix_tree_gang_lookup_tag(radix, (void **)cache,
Chris Mason54aa1f42007-06-22 14:16:25 -0400581 off, ARRAY_SIZE(cache),
Chris Mason9078a3e2007-04-26 16:46:15 -0400582 BTRFS_BLOCK_GROUP_DIRTY);
583 if (!ret)
584 break;
585 for (i = 0; i < ret; i++) {
Chris Mason54aa1f42007-06-22 14:16:25 -0400586 err = write_one_cache_group(trans, root,
587 path, cache[i]);
588 /*
589 * if we fail to write the cache group, we want
590 * to keep it marked dirty in hopes that a later
591 * write will work
592 */
593 if (err) {
594 werr = err;
595 off = cache[i]->key.objectid +
596 cache[i]->key.offset;
597 continue;
598 }
599
Chris Mason9078a3e2007-04-26 16:46:15 -0400600 radix_tree_tag_clear(radix, cache[i]->key.objectid +
601 cache[i]->key.offset - 1,
602 BTRFS_BLOCK_GROUP_DIRTY);
Chris Mason9078a3e2007-04-26 16:46:15 -0400603 }
604 }
605 btrfs_free_path(path);
606 return werr;
607}
608
Chris Masonbe744172007-05-06 10:15:01 -0400609int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
610 struct btrfs_root *root)
611{
612 int ret;
613 int ret2;
614 ret = write_dirty_block_radix(trans, root,
615 &root->fs_info->block_group_radix);
616 ret2 = write_dirty_block_radix(trans, root,
617 &root->fs_info->block_group_data_radix);
618 if (ret)
619 return ret;
620 if (ret2)
621 return ret2;
622 return 0;
623}
624
Chris Mason9078a3e2007-04-26 16:46:15 -0400625static int update_block_group(struct btrfs_trans_handle *trans,
626 struct btrfs_root *root,
Chris Mason1e2677e2007-05-29 16:52:18 -0400627 u64 blocknr, u64 num, int alloc, int mark_free,
628 int data)
Chris Mason9078a3e2007-04-26 16:46:15 -0400629{
630 struct btrfs_block_group_cache *cache;
631 struct btrfs_fs_info *info = root->fs_info;
632 u64 total = num;
633 u64 old_val;
634 u64 block_in_group;
Chris Masone37c9e62007-05-09 20:13:14 -0400635 u64 i;
Chris Mason1e2677e2007-05-29 16:52:18 -0400636 int ret;
Chris Mason3e1ad542007-05-07 20:03:49 -0400637
Chris Mason9078a3e2007-04-26 16:46:15 -0400638 while(total) {
Chris Mason5276aed2007-06-11 21:33:38 -0400639 cache = btrfs_lookup_block_group(info, blocknr);
Chris Mason3e1ad542007-05-07 20:03:49 -0400640 if (!cache) {
Chris Mason9078a3e2007-04-26 16:46:15 -0400641 return -1;
Chris Masoncd1bc462007-04-27 10:08:34 -0400642 }
Chris Mason9078a3e2007-04-26 16:46:15 -0400643 block_in_group = blocknr - cache->key.objectid;
644 WARN_ON(block_in_group > cache->key.offset);
Chris Mason3e1ad542007-05-07 20:03:49 -0400645 radix_tree_tag_set(cache->radix, cache->key.objectid +
Chris Masonbe744172007-05-06 10:15:01 -0400646 cache->key.offset - 1,
Chris Mason9078a3e2007-04-26 16:46:15 -0400647 BTRFS_BLOCK_GROUP_DIRTY);
648
649 old_val = btrfs_block_group_used(&cache->item);
650 num = min(total, cache->key.offset - block_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -0400651 if (alloc) {
Chris Masoncd1bc462007-04-27 10:08:34 -0400652 if (blocknr > cache->last_alloc)
653 cache->last_alloc = blocknr;
Chris Masone37c9e62007-05-09 20:13:14 -0400654 if (!cache->data) {
655 for (i = 0; i < num; i++) {
656 clear_radix_bit(&info->extent_map_radix,
657 blocknr + i);
658 }
659 }
Chris Mason1e2677e2007-05-29 16:52:18 -0400660 if (cache->data != data &&
Chris Mason84f54cf2007-06-12 07:43:08 -0400661 old_val < (cache->key.offset >> 1)) {
Chris Mason1e2677e2007-05-29 16:52:18 -0400662 cache->data = data;
663 radix_tree_delete(cache->radix,
664 cache->key.objectid +
665 cache->key.offset - 1);
666
667 if (data) {
668 cache->radix =
669 &info->block_group_data_radix;
670 cache->item.flags |=
671 BTRFS_BLOCK_GROUP_DATA;
672 } else {
673 cache->radix = &info->block_group_radix;
674 cache->item.flags &=
675 ~BTRFS_BLOCK_GROUP_DATA;
676 }
677 ret = radix_tree_insert(cache->radix,
678 cache->key.objectid +
679 cache->key.offset - 1,
680 (void *)cache);
681 }
682 old_val += num;
Chris Masoncd1bc462007-04-27 10:08:34 -0400683 } else {
Chris Mason9078a3e2007-04-26 16:46:15 -0400684 old_val -= num;
Chris Masoncd1bc462007-04-27 10:08:34 -0400685 if (blocknr < cache->first_free)
686 cache->first_free = blocknr;
Chris Masone37c9e62007-05-09 20:13:14 -0400687 if (!cache->data && mark_free) {
688 for (i = 0; i < num; i++) {
689 set_radix_bit(&info->extent_map_radix,
690 blocknr + i);
691 }
692 }
Chris Mason84f54cf2007-06-12 07:43:08 -0400693 if (old_val < (cache->key.offset >> 1) &&
694 old_val + num >= (cache->key.offset >> 1)) {
Chris Masone37c9e62007-05-09 20:13:14 -0400695 radix_tree_tag_set(cache->radix,
696 cache->key.objectid +
697 cache->key.offset - 1,
698 BTRFS_BLOCK_GROUP_AVAIL);
699 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400700 }
Chris Mason9078a3e2007-04-26 16:46:15 -0400701 btrfs_set_block_group_used(&cache->item, old_val);
Chris Masone37c9e62007-05-09 20:13:14 -0400702 total -= num;
703 blocknr += num;
Chris Mason9078a3e2007-04-26 16:46:15 -0400704 }
705 return 0;
706}
707
Chris Masonbe08c1b2007-05-03 09:06:49 -0400708static int try_remove_page(struct address_space *mapping, unsigned long index)
709{
710 int ret;
711 ret = invalidate_mapping_pages(mapping, index, index);
712 return ret;
713}
714
Chris Masonccd467d2007-06-28 15:57:36 -0400715int btrfs_copy_pinned(struct btrfs_root *root, struct radix_tree_root *copy)
716{
717 unsigned long gang[8];
718 u64 last = 0;
719 struct radix_tree_root *pinned_radix = &root->fs_info->pinned_radix;
720 int ret;
721 int i;
722
723 while(1) {
724 ret = find_first_radix_bit(pinned_radix, gang, last,
725 ARRAY_SIZE(gang));
726 if (!ret)
727 break;
728 for (i = 0 ; i < ret; i++) {
729 set_radix_bit(copy, gang[i]);
730 last = gang[i] + 1;
731 }
732 }
733 return 0;
734}
735
736int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
737 struct btrfs_root *root,
738 struct radix_tree_root *unpin_radix)
Chris Masona28ec192007-03-06 20:08:01 -0500739{
Chris Mason8ef97622007-03-26 10:15:30 -0400740 unsigned long gang[8];
Chris Masonbe08c1b2007-05-03 09:06:49 -0400741 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Masonbe744172007-05-06 10:15:01 -0400742 struct btrfs_block_group_cache *block_group;
Chris Mason88fd1462007-03-16 08:56:18 -0400743 u64 first = 0;
Chris Masona28ec192007-03-06 20:08:01 -0500744 int ret;
745 int i;
Chris Mason8ef97622007-03-26 10:15:30 -0400746 struct radix_tree_root *pinned_radix = &root->fs_info->pinned_radix;
Chris Masone37c9e62007-05-09 20:13:14 -0400747 struct radix_tree_root *extent_radix = &root->fs_info->extent_map_radix;
Chris Masona28ec192007-03-06 20:08:01 -0500748
749 while(1) {
Chris Masonccd467d2007-06-28 15:57:36 -0400750 ret = find_first_radix_bit(unpin_radix, gang, 0,
Chris Mason8ef97622007-03-26 10:15:30 -0400751 ARRAY_SIZE(gang));
Chris Masona28ec192007-03-06 20:08:01 -0500752 if (!ret)
753 break;
Chris Mason88fd1462007-03-16 08:56:18 -0400754 if (!first)
Chris Mason8ef97622007-03-26 10:15:30 -0400755 first = gang[0];
Chris Mason0579da42007-03-07 16:15:30 -0500756 for (i = 0; i < ret; i++) {
Chris Mason8ef97622007-03-26 10:15:30 -0400757 clear_radix_bit(pinned_radix, gang[i]);
Chris Masonccd467d2007-06-28 15:57:36 -0400758 clear_radix_bit(unpin_radix, gang[i]);
Chris Mason5276aed2007-06-11 21:33:38 -0400759 block_group = btrfs_lookup_block_group(root->fs_info,
760 gang[i]);
Chris Masonbe744172007-05-06 10:15:01 -0400761 if (block_group) {
762 WARN_ON(block_group->pinned == 0);
763 block_group->pinned--;
764 if (gang[i] < block_group->last_alloc)
765 block_group->last_alloc = gang[i];
Chris Masone37c9e62007-05-09 20:13:14 -0400766 if (gang[i] < block_group->last_prealloc)
767 block_group->last_prealloc = gang[i];
768 if (!block_group->data)
769 set_radix_bit(extent_radix, gang[i]);
Chris Masonbe744172007-05-06 10:15:01 -0400770 }
Chris Masonbe08c1b2007-05-03 09:06:49 -0400771 try_remove_page(btree_inode->i_mapping,
772 gang[i] << (PAGE_CACHE_SHIFT -
773 btree_inode->i_blkbits));
Chris Mason0579da42007-03-07 16:15:30 -0500774 }
Chris Masona28ec192007-03-06 20:08:01 -0500775 }
776 return 0;
777}
778
Chris Masone089f052007-03-16 16:20:31 -0400779static int finish_current_insert(struct btrfs_trans_handle *trans, struct
780 btrfs_root *extent_root)
Chris Mason037e6392007-03-07 11:50:24 -0500781{
Chris Masone2fa7222007-03-12 16:22:34 -0400782 struct btrfs_key ins;
Chris Mason234b63a2007-03-13 10:46:10 -0400783 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -0500784 int i;
785 int ret;
Chris Mason1261ec42007-03-20 20:35:03 -0400786 u64 super_blocks_used;
787 struct btrfs_fs_info *info = extent_root->fs_info;
Chris Mason037e6392007-03-07 11:50:24 -0500788
Chris Masoncf27e1e2007-03-13 09:49:06 -0400789 btrfs_set_extent_refs(&extent_item, 1);
Chris Mason037e6392007-03-07 11:50:24 -0500790 ins.offset = 1;
791 ins.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -0400792 btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
Chris Mason5d0c3e62007-04-23 17:01:05 -0400793 btrfs_set_extent_owner(&extent_item, extent_root->root_key.objectid);
Chris Mason037e6392007-03-07 11:50:24 -0500794
Chris Masonf2458e12007-04-25 15:52:25 -0400795 for (i = 0; i < extent_root->fs_info->extent_tree_insert_nr; i++) {
796 ins.objectid = extent_root->fs_info->extent_tree_insert[i];
Chris Mason4b52dff2007-06-26 10:06:50 -0400797 super_blocks_used = btrfs_super_blocks_used(&info->super_copy);
798 btrfs_set_super_blocks_used(&info->super_copy,
Chris Mason1261ec42007-03-20 20:35:03 -0400799 super_blocks_used + 1);
Chris Masone089f052007-03-16 16:20:31 -0400800 ret = btrfs_insert_item(trans, extent_root, &ins, &extent_item,
801 sizeof(extent_item));
Chris Mason037e6392007-03-07 11:50:24 -0500802 BUG_ON(ret);
803 }
Chris Masonf2458e12007-04-25 15:52:25 -0400804 extent_root->fs_info->extent_tree_insert_nr = 0;
Chris Mason037e6392007-03-07 11:50:24 -0500805 return 0;
806}
807
Chris Mason8ef97622007-03-26 10:15:30 -0400808static int pin_down_block(struct btrfs_root *root, u64 blocknr, int pending)
Chris Masone20d96d2007-03-22 12:13:20 -0400809{
810 int err;
Chris Mason78fae272007-03-25 11:35:08 -0400811 struct btrfs_header *header;
Chris Mason8ef97622007-03-26 10:15:30 -0400812 struct buffer_head *bh;
Chris Mason78fae272007-03-25 11:35:08 -0400813
Chris Masonf4b9aa82007-03-27 11:05:53 -0400814 if (!pending) {
Chris Masond98237b2007-03-28 13:57:48 -0400815 bh = btrfs_find_tree_block(root, blocknr);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400816 if (bh) {
817 if (buffer_uptodate(bh)) {
818 u64 transid =
819 root->fs_info->running_transaction->transid;
820 header = btrfs_buffer_header(bh);
821 if (btrfs_header_generation(header) ==
822 transid) {
823 btrfs_block_release(root, bh);
824 return 0;
825 }
Chris Masonf4b9aa82007-03-27 11:05:53 -0400826 }
Chris Masond6025572007-03-30 14:27:56 -0400827 btrfs_block_release(root, bh);
Chris Mason8ef97622007-03-26 10:15:30 -0400828 }
Chris Mason8ef97622007-03-26 10:15:30 -0400829 err = set_radix_bit(&root->fs_info->pinned_radix, blocknr);
Chris Masonbe744172007-05-06 10:15:01 -0400830 if (!err) {
831 struct btrfs_block_group_cache *cache;
Chris Mason5276aed2007-06-11 21:33:38 -0400832 cache = btrfs_lookup_block_group(root->fs_info,
833 blocknr);
Chris Masonbe744172007-05-06 10:15:01 -0400834 if (cache)
835 cache->pinned++;
836 }
Chris Masonf4b9aa82007-03-27 11:05:53 -0400837 } else {
838 err = set_radix_bit(&root->fs_info->pending_del_radix, blocknr);
839 }
Chris Masonbe744172007-05-06 10:15:01 -0400840 BUG_ON(err < 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400841 return 0;
842}
843
Chris Masona28ec192007-03-06 20:08:01 -0500844/*
845 * remove an extent from the root, returns 0 on success
846 */
Chris Masone089f052007-03-16 16:20:31 -0400847static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masone37c9e62007-05-09 20:13:14 -0400848 *root, u64 blocknr, u64 num_blocks, int pin,
849 int mark_free)
Chris Masona28ec192007-03-06 20:08:01 -0500850{
Chris Mason5caf2a02007-04-02 11:20:42 -0400851 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -0400852 struct btrfs_key key;
Chris Mason1261ec42007-03-20 20:35:03 -0400853 struct btrfs_fs_info *info = root->fs_info;
854 struct btrfs_root *extent_root = info->extent_root;
Chris Masona28ec192007-03-06 20:08:01 -0500855 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400856 struct btrfs_extent_item *ei;
Chris Masone2fa7222007-03-12 16:22:34 -0400857 struct btrfs_key ins;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400858 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500859
Chris Masona28ec192007-03-06 20:08:01 -0500860 key.objectid = blocknr;
861 key.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -0400862 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masona28ec192007-03-06 20:08:01 -0500863 key.offset = num_blocks;
864
Chris Mason5caf2a02007-04-02 11:20:42 -0400865 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -0400866 if (!path)
867 return -ENOMEM;
868
Chris Masonf2654de2007-06-26 12:20:46 -0400869 ret = find_free_extent(trans, root, 0, 0, (u64)-1, 0, &ins, 0, 0, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400870 if (ret) {
871 btrfs_free_path(path);
872 return ret;
873 }
Chris Mason5f26f772007-04-05 10:38:44 -0400874
Chris Mason5caf2a02007-04-02 11:20:42 -0400875 ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400876 if (ret < 0)
877 return ret;
878 BUG_ON(ret);
Chris Mason5caf2a02007-04-02 11:20:42 -0400879 ei = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
Chris Mason123abc82007-03-14 14:14:43 -0400880 struct btrfs_extent_item);
Chris Masona28ec192007-03-06 20:08:01 -0500881 BUG_ON(ei->refs == 0);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400882 refs = btrfs_extent_refs(ei) - 1;
883 btrfs_set_extent_refs(ei, refs);
Chris Mason5caf2a02007-04-02 11:20:42 -0400884 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400885 if (refs == 0) {
Chris Mason1261ec42007-03-20 20:35:03 -0400886 u64 super_blocks_used;
Chris Mason78fae272007-03-25 11:35:08 -0400887
888 if (pin) {
Chris Mason8ef97622007-03-26 10:15:30 -0400889 ret = pin_down_block(root, blocknr, 0);
Chris Mason78fae272007-03-25 11:35:08 -0400890 BUG_ON(ret);
891 }
892
Chris Mason4b52dff2007-06-26 10:06:50 -0400893 super_blocks_used = btrfs_super_blocks_used(&info->super_copy);
894 btrfs_set_super_blocks_used(&info->super_copy,
Chris Mason1261ec42007-03-20 20:35:03 -0400895 super_blocks_used - num_blocks);
Chris Mason5caf2a02007-04-02 11:20:42 -0400896 ret = btrfs_del_item(trans, extent_root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -0400897 if (ret) {
898 return ret;
899 }
Chris Masone37c9e62007-05-09 20:13:14 -0400900 ret = update_block_group(trans, root, blocknr, num_blocks, 0,
Chris Mason1e2677e2007-05-29 16:52:18 -0400901 mark_free, 0);
Chris Mason9078a3e2007-04-26 16:46:15 -0400902 BUG_ON(ret);
Chris Masona28ec192007-03-06 20:08:01 -0500903 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400904 btrfs_free_path(path);
Chris Masone089f052007-03-16 16:20:31 -0400905 finish_current_insert(trans, extent_root);
Chris Masona28ec192007-03-06 20:08:01 -0500906 return ret;
907}
908
909/*
Chris Masonfec577f2007-02-26 10:40:21 -0500910 * find all the blocks marked as pending in the radix tree and remove
911 * them from the extent map
912 */
Chris Masone089f052007-03-16 16:20:31 -0400913static int del_pending_extents(struct btrfs_trans_handle *trans, struct
914 btrfs_root *extent_root)
Chris Masonfec577f2007-02-26 10:40:21 -0500915{
916 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -0400917 int wret;
918 int err = 0;
Chris Mason8ef97622007-03-26 10:15:30 -0400919 unsigned long gang[4];
Chris Masonfec577f2007-02-26 10:40:21 -0500920 int i;
Chris Mason8ef97622007-03-26 10:15:30 -0400921 struct radix_tree_root *pending_radix;
922 struct radix_tree_root *pinned_radix;
Chris Masonbe744172007-05-06 10:15:01 -0400923 struct btrfs_block_group_cache *cache;
Chris Mason8ef97622007-03-26 10:15:30 -0400924
925 pending_radix = &extent_root->fs_info->pending_del_radix;
926 pinned_radix = &extent_root->fs_info->pinned_radix;
Chris Masonfec577f2007-02-26 10:40:21 -0500927
928 while(1) {
Chris Masone37c9e62007-05-09 20:13:14 -0400929 ret = find_first_radix_bit(pending_radix, gang, 0,
Chris Mason8ef97622007-03-26 10:15:30 -0400930 ARRAY_SIZE(gang));
Chris Masonfec577f2007-02-26 10:40:21 -0500931 if (!ret)
932 break;
933 for (i = 0; i < ret; i++) {
Chris Mason8ef97622007-03-26 10:15:30 -0400934 wret = set_radix_bit(pinned_radix, gang[i]);
Chris Masonbe744172007-05-06 10:15:01 -0400935 if (wret == 0) {
Chris Mason5276aed2007-06-11 21:33:38 -0400936 cache =
937 btrfs_lookup_block_group(extent_root->fs_info,
Chris Masonbe744172007-05-06 10:15:01 -0400938 gang[i]);
939 if (cache)
940 cache->pinned++;
941 }
942 if (wret < 0) {
943 printk(KERN_CRIT "set_radix_bit, err %d\n",
944 wret);
945 BUG_ON(wret < 0);
946 }
Chris Mason8ef97622007-03-26 10:15:30 -0400947 wret = clear_radix_bit(pending_radix, gang[i]);
948 BUG_ON(wret);
Chris Masond5719762007-03-23 10:01:08 -0400949 wret = __free_extent(trans, extent_root,
Chris Masone37c9e62007-05-09 20:13:14 -0400950 gang[i], 1, 0, 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400951 if (wret)
952 err = wret;
Chris Masonfec577f2007-02-26 10:40:21 -0500953 }
954 }
Chris Masone20d96d2007-03-22 12:13:20 -0400955 return err;
Chris Masonfec577f2007-02-26 10:40:21 -0500956}
957
958/*
959 * remove an extent from the root, returns 0 on success
960 */
Chris Masone089f052007-03-16 16:20:31 -0400961int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
962 *root, u64 blocknr, u64 num_blocks, int pin)
Chris Masonfec577f2007-02-26 10:40:21 -0500963{
Chris Mason9f5fae22007-03-20 14:38:32 -0400964 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Masonfec577f2007-02-26 10:40:21 -0500965 int pending_ret;
966 int ret;
Chris Masona28ec192007-03-06 20:08:01 -0500967
968 if (root == extent_root) {
Chris Mason8ef97622007-03-26 10:15:30 -0400969 pin_down_block(root, blocknr, 1);
Chris Masona28ec192007-03-06 20:08:01 -0500970 return 0;
971 }
Chris Masone37c9e62007-05-09 20:13:14 -0400972 ret = __free_extent(trans, root, blocknr, num_blocks, pin, pin == 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400973 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
Chris Masonfec577f2007-02-26 10:40:21 -0500974 return ret ? ret : pending_ret;
975}
976
977/*
978 * walks the btree of allocated extents and find a hole of a given size.
979 * The key ins is changed to record the hole:
980 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -0400981 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -0500982 * ins->offset == number of blocks
983 * Any available blocks before search_start are skipped.
984 */
Chris Masone089f052007-03-16 16:20:31 -0400985static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
986 *orig_root, u64 num_blocks, u64 search_start, u64
Chris Masonfbdc7622007-05-30 10:22:12 -0400987 search_end, u64 hint_block,
Chris Masonf2654de2007-06-26 12:20:46 -0400988 struct btrfs_key *ins, u64 exclude_start,
989 u64 exclude_nr, int data)
Chris Masonfec577f2007-02-26 10:40:21 -0500990{
Chris Mason5caf2a02007-04-02 11:20:42 -0400991 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -0400992 struct btrfs_key key;
Chris Masonfec577f2007-02-26 10:40:21 -0500993 int ret;
994 u64 hole_size = 0;
995 int slot = 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400996 u64 last_block = 0;
Chris Mason037e6392007-03-07 11:50:24 -0500997 u64 test_block;
Chris Masonbe744172007-05-06 10:15:01 -0400998 u64 orig_search_start = search_start;
Chris Masonfec577f2007-02-26 10:40:21 -0500999 int start_found;
Chris Mason234b63a2007-03-13 10:46:10 -04001000 struct btrfs_leaf *l;
Chris Mason9f5fae22007-03-20 14:38:32 -04001001 struct btrfs_root * root = orig_root->fs_info->extent_root;
Chris Masonf2458e12007-04-25 15:52:25 -04001002 struct btrfs_fs_info *info = root->fs_info;
Chris Mason0579da42007-03-07 16:15:30 -05001003 int total_needed = num_blocks;
Chris Masonf2458e12007-04-25 15:52:25 -04001004 int total_found = 0;
1005 int fill_prealloc = 0;
Chris Masone20d96d2007-03-22 12:13:20 -04001006 int level;
Chris Masonbe08c1b2007-05-03 09:06:49 -04001007 struct btrfs_block_group_cache *block_group;
Chris Masonbe744172007-05-06 10:15:01 -04001008 int full_scan = 0;
Chris Masonfbdc7622007-05-30 10:22:12 -04001009 int wrapped = 0;
Chris Masonde428b62007-05-18 13:28:27 -04001010 u64 limit;
Chris Masonfec577f2007-02-26 10:40:21 -05001011
Chris Masonb1a4d962007-04-04 15:27:52 -04001012 ins->flags = 0;
1013 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
1014
Chris Masone20d96d2007-03-22 12:13:20 -04001015 level = btrfs_header_level(btrfs_buffer_header(root->node));
Chris Masonf2458e12007-04-25 15:52:25 -04001016 if (num_blocks == 0) {
1017 fill_prealloc = 1;
1018 num_blocks = 1;
Chris Mason308535a2007-04-28 15:17:08 -04001019 total_needed = (min(level + 1, BTRFS_MAX_LEVEL) + 2) * 3;
Chris Masonf2458e12007-04-25 15:52:25 -04001020 }
Chris Mason85e55b12007-06-19 15:50:51 -04001021 if (fill_prealloc) {
1022 u64 first;
1023 int nr = info->extent_tree_prealloc_nr;
1024 first = info->extent_tree_prealloc[nr - 1];
1025 if (info->extent_tree_prealloc_nr >= total_needed &&
1026 first >= search_start) {
1027 ins->objectid = info->extent_tree_prealloc[0];
1028 ins->offset = 1;
1029 return 0;
1030 }
1031 info->extent_tree_prealloc_nr = 0;
1032 }
Chris Mason3e1ad542007-05-07 20:03:49 -04001033 if (search_end == (u64)-1)
Chris Mason4b52dff2007-06-26 10:06:50 -04001034 search_end = btrfs_super_total_blocks(&info->super_copy);
Chris Masonfbdc7622007-05-30 10:22:12 -04001035 if (hint_block) {
Chris Mason5276aed2007-06-11 21:33:38 -04001036 block_group = btrfs_lookup_block_group(info, hint_block);
Chris Masonbe744172007-05-06 10:15:01 -04001037 block_group = btrfs_find_block_group(root, block_group,
Chris Masonfbdc7622007-05-30 10:22:12 -04001038 hint_block, data, 1);
Chris Masonbe744172007-05-06 10:15:01 -04001039 } else {
1040 block_group = btrfs_find_block_group(root,
1041 trans->block_group, 0,
Chris Masonde428b62007-05-18 13:28:27 -04001042 data, 1);
Chris Masonbe744172007-05-06 10:15:01 -04001043 }
1044
Chris Masone0115992007-06-19 16:23:05 -04001045 path = btrfs_alloc_path();
1046
Chris Masonbe744172007-05-06 10:15:01 -04001047check_failed:
Chris Mason1e2677e2007-05-29 16:52:18 -04001048 if (!block_group->data)
Chris Masone37c9e62007-05-09 20:13:14 -04001049 search_start = find_search_start(root, &block_group,
1050 search_start, total_needed);
Chris Masonfbdc7622007-05-30 10:22:12 -04001051 else if (!full_scan)
Chris Masone37c9e62007-05-09 20:13:14 -04001052 search_start = max(block_group->last_alloc, search_start);
1053
Chris Mason5caf2a02007-04-02 11:20:42 -04001054 btrfs_init_path(path);
Chris Masonfec577f2007-02-26 10:40:21 -05001055 ins->objectid = search_start;
1056 ins->offset = 0;
Chris Masonfec577f2007-02-26 10:40:21 -05001057 start_found = 0;
Chris Masone37c9e62007-05-09 20:13:14 -04001058
Chris Mason5caf2a02007-04-02 11:20:42 -04001059 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
Chris Mason0f70abe2007-02-28 16:46:22 -05001060 if (ret < 0)
1061 goto error;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001062
Chris Masone37c9e62007-05-09 20:13:14 -04001063 if (path->slots[0] > 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001064 path->slots[0]--;
Chris Masone37c9e62007-05-09 20:13:14 -04001065 }
1066
1067 l = btrfs_buffer_leaf(path->nodes[0]);
1068 btrfs_disk_key_to_cpu(&key, &l->items[path->slots[0]].key);
1069 /*
1070 * a rare case, go back one key if we hit a block group item
1071 * instead of an extent item
1072 */
1073 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY &&
1074 key.objectid + key.offset >= search_start) {
1075 ins->objectid = key.objectid;
1076 ins->offset = key.offset - 1;
1077 btrfs_release_path(root, path);
1078 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
1079 if (ret < 0)
1080 goto error;
1081
1082 if (path->slots[0] > 0) {
1083 path->slots[0]--;
1084 }
1085 }
Chris Mason0579da42007-03-07 16:15:30 -05001086
Chris Masonfec577f2007-02-26 10:40:21 -05001087 while (1) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001088 l = btrfs_buffer_leaf(path->nodes[0]);
1089 slot = path->slots[0];
Chris Mason7518a232007-03-12 12:01:18 -04001090 if (slot >= btrfs_header_nritems(&l->header)) {
Chris Masonf2458e12007-04-25 15:52:25 -04001091 if (fill_prealloc) {
1092 info->extent_tree_prealloc_nr = 0;
1093 total_found = 0;
1094 }
Chris Masonde428b62007-05-18 13:28:27 -04001095 if (start_found)
1096 limit = last_block +
Chris Mason84f54cf2007-06-12 07:43:08 -04001097 (block_group->key.offset >> 1);
Chris Masonde428b62007-05-18 13:28:27 -04001098 else
1099 limit = search_start +
Chris Mason84f54cf2007-06-12 07:43:08 -04001100 (block_group->key.offset >> 1);
Chris Mason5caf2a02007-04-02 11:20:42 -04001101 ret = btrfs_next_leaf(root, path);
Chris Masonfec577f2007-02-26 10:40:21 -05001102 if (ret == 0)
1103 continue;
Chris Mason0f70abe2007-02-28 16:46:22 -05001104 if (ret < 0)
1105 goto error;
Chris Masonfec577f2007-02-26 10:40:21 -05001106 if (!start_found) {
1107 ins->objectid = search_start;
Chris Mason3e1ad542007-05-07 20:03:49 -04001108 ins->offset = search_end - search_start;
Chris Masonfec577f2007-02-26 10:40:21 -05001109 start_found = 1;
1110 goto check_pending;
1111 }
1112 ins->objectid = last_block > search_start ?
1113 last_block : search_start;
Chris Mason3e1ad542007-05-07 20:03:49 -04001114 ins->offset = search_end - ins->objectid;
Chris Masonfec577f2007-02-26 10:40:21 -05001115 goto check_pending;
1116 }
Chris Masone37c9e62007-05-09 20:13:14 -04001117
Chris Masone2fa7222007-03-12 16:22:34 -04001118 btrfs_disk_key_to_cpu(&key, &l->items[slot].key);
Chris Masone37c9e62007-05-09 20:13:14 -04001119 if (key.objectid >= search_start && key.objectid > last_block &&
1120 start_found) {
1121 if (last_block < search_start)
1122 last_block = search_start;
1123 hole_size = key.objectid - last_block;
1124 if (hole_size >= num_blocks) {
1125 ins->objectid = last_block;
1126 ins->offset = hole_size;
1127 goto check_pending;
Chris Mason0579da42007-03-07 16:15:30 -05001128 }
Chris Masonfec577f2007-02-26 10:40:21 -05001129 }
Chris Masone37c9e62007-05-09 20:13:14 -04001130
1131 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY)
1132 goto next;
1133
Chris Mason0579da42007-03-07 16:15:30 -05001134 start_found = 1;
Chris Masone2fa7222007-03-12 16:22:34 -04001135 last_block = key.objectid + key.offset;
Chris Masonfbdc7622007-05-30 10:22:12 -04001136 if (!full_scan && last_block >= block_group->key.objectid +
Chris Masonbe744172007-05-06 10:15:01 -04001137 block_group->key.offset) {
1138 btrfs_release_path(root, path);
1139 search_start = block_group->key.objectid +
1140 block_group->key.offset * 2;
1141 goto new_group;
1142 }
Chris Mason9078a3e2007-04-26 16:46:15 -04001143next:
Chris Mason5caf2a02007-04-02 11:20:42 -04001144 path->slots[0]++;
Chris Masonde428b62007-05-18 13:28:27 -04001145 cond_resched();
Chris Masonfec577f2007-02-26 10:40:21 -05001146 }
Chris Masonfec577f2007-02-26 10:40:21 -05001147check_pending:
1148 /* we have to make sure we didn't find an extent that has already
1149 * been allocated by the map tree or the original allocation
1150 */
Chris Mason5caf2a02007-04-02 11:20:42 -04001151 btrfs_release_path(root, path);
Chris Masonfec577f2007-02-26 10:40:21 -05001152 BUG_ON(ins->objectid < search_start);
Chris Masone37c9e62007-05-09 20:13:14 -04001153
Chris Mason3e1ad542007-05-07 20:03:49 -04001154 if (ins->objectid + num_blocks >= search_end) {
Chris Masonfbdc7622007-05-30 10:22:12 -04001155 if (full_scan) {
1156 ret = -ENOSPC;
1157 goto error;
1158 }
Chris Masonbe744172007-05-06 10:15:01 -04001159 search_start = orig_search_start;
Chris Masonfbdc7622007-05-30 10:22:12 -04001160 if (wrapped)
1161 full_scan = 1;
1162 else
1163 wrapped = 1;
Chris Masonbe744172007-05-06 10:15:01 -04001164 goto new_group;
Chris Mason06a2f9f2007-04-28 08:48:10 -04001165 }
Chris Mason037e6392007-03-07 11:50:24 -05001166 for (test_block = ins->objectid;
Chris Masonf2458e12007-04-25 15:52:25 -04001167 test_block < ins->objectid + num_blocks; test_block++) {
1168 if (test_radix_bit(&info->pinned_radix, test_block)) {
Chris Mason037e6392007-03-07 11:50:24 -05001169 search_start = test_block + 1;
Chris Masonbe744172007-05-06 10:15:01 -04001170 goto new_group;
Chris Masonfec577f2007-02-26 10:40:21 -05001171 }
1172 }
Chris Masonf2458e12007-04-25 15:52:25 -04001173 if (!fill_prealloc && info->extent_tree_insert_nr) {
1174 u64 last =
1175 info->extent_tree_insert[info->extent_tree_insert_nr - 1];
1176 if (ins->objectid + num_blocks >
1177 info->extent_tree_insert[0] &&
1178 ins->objectid <= last) {
1179 search_start = last + 1;
Chris Masone37c9e62007-05-09 20:13:14 -04001180 WARN_ON(!full_scan);
Chris Masonbe744172007-05-06 10:15:01 -04001181 goto new_group;
Chris Masonf2458e12007-04-25 15:52:25 -04001182 }
1183 }
1184 if (!fill_prealloc && info->extent_tree_prealloc_nr) {
1185 u64 first =
1186 info->extent_tree_prealloc[info->extent_tree_prealloc_nr - 1];
1187 if (ins->objectid + num_blocks > first &&
1188 ins->objectid <= info->extent_tree_prealloc[0]) {
1189 search_start = info->extent_tree_prealloc[0] + 1;
Chris Masonbe744172007-05-06 10:15:01 -04001190 goto new_group;
Chris Masonf2458e12007-04-25 15:52:25 -04001191 }
1192 }
Chris Masonf2654de2007-06-26 12:20:46 -04001193 if (exclude_nr > 0 && (ins->objectid + num_blocks > exclude_start &&
1194 ins->objectid < exclude_start + exclude_nr)) {
1195 search_start = exclude_start + exclude_nr;
1196 goto new_group;
1197 }
Chris Masonf2458e12007-04-25 15:52:25 -04001198 if (fill_prealloc) {
1199 int nr;
1200 test_block = ins->objectid;
Chris Masone37c9e62007-05-09 20:13:14 -04001201 if (test_block - info->extent_tree_prealloc[total_needed - 1] >=
1202 leaf_range(root)) {
1203 total_found = 0;
1204 info->extent_tree_prealloc_nr = total_found;
1205 }
Chris Masonf2458e12007-04-25 15:52:25 -04001206 while(test_block < ins->objectid + ins->offset &&
1207 total_found < total_needed) {
1208 nr = total_needed - total_found - 1;
1209 BUG_ON(nr < 0);
Chris Masoncd1bc462007-04-27 10:08:34 -04001210 info->extent_tree_prealloc[nr] = test_block;
Chris Masonf2458e12007-04-25 15:52:25 -04001211 total_found++;
1212 test_block++;
1213 }
1214 if (total_found < total_needed) {
1215 search_start = test_block;
Chris Masonbe744172007-05-06 10:15:01 -04001216 goto new_group;
Chris Masonf2458e12007-04-25 15:52:25 -04001217 }
Chris Masoncd1bc462007-04-27 10:08:34 -04001218 info->extent_tree_prealloc_nr = total_found;
Chris Masonf2458e12007-04-25 15:52:25 -04001219 }
Chris Masone37c9e62007-05-09 20:13:14 -04001220 if (!data) {
Chris Mason5276aed2007-06-11 21:33:38 -04001221 block_group = btrfs_lookup_block_group(info, ins->objectid);
Chris Masone37c9e62007-05-09 20:13:14 -04001222 if (block_group) {
1223 if (fill_prealloc)
1224 block_group->last_prealloc =
1225 info->extent_tree_prealloc[total_needed-1];
1226 else
1227 trans->block_group = block_group;
1228 }
Chris Masoncd1bc462007-04-27 10:08:34 -04001229 }
Chris Mason037e6392007-03-07 11:50:24 -05001230 ins->offset = num_blocks;
Chris Mason5caf2a02007-04-02 11:20:42 -04001231 btrfs_free_path(path);
Chris Masonfec577f2007-02-26 10:40:21 -05001232 return 0;
Chris Masonbe744172007-05-06 10:15:01 -04001233
1234new_group:
Chris Mason3e1ad542007-05-07 20:03:49 -04001235 if (search_start + num_blocks >= search_end) {
Chris Masonbe744172007-05-06 10:15:01 -04001236 search_start = orig_search_start;
Chris Masonfbdc7622007-05-30 10:22:12 -04001237 if (full_scan) {
1238 ret = -ENOSPC;
1239 goto error;
1240 }
1241 if (wrapped)
1242 full_scan = 1;
1243 else
1244 wrapped = 1;
Chris Masonbe744172007-05-06 10:15:01 -04001245 }
Chris Mason5276aed2007-06-11 21:33:38 -04001246 block_group = btrfs_lookup_block_group(info, search_start);
Chris Masonfbdc7622007-05-30 10:22:12 -04001247 cond_resched();
Chris Masonbe744172007-05-06 10:15:01 -04001248 if (!full_scan)
1249 block_group = btrfs_find_block_group(root, block_group,
Chris Masonde428b62007-05-18 13:28:27 -04001250 search_start, data, 0);
Chris Masonbe744172007-05-06 10:15:01 -04001251 goto check_failed;
1252
Chris Mason0f70abe2007-02-28 16:46:22 -05001253error:
Chris Mason5caf2a02007-04-02 11:20:42 -04001254 btrfs_release_path(root, path);
1255 btrfs_free_path(path);
Chris Mason0f70abe2007-02-28 16:46:22 -05001256 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05001257}
Chris Masonfec577f2007-02-26 10:40:21 -05001258/*
Chris Masonfec577f2007-02-26 10:40:21 -05001259 * finds a free extent and does all the dirty work required for allocation
1260 * returns the key for the extent through ins, and a tree buffer for
1261 * the first block of the extent through buf.
1262 *
1263 * returns 0 if everything worked, non-zero otherwise.
1264 */
Chris Mason4d775672007-04-20 20:23:12 -04001265int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
1266 struct btrfs_root *root, u64 owner,
Chris Masonfbdc7622007-05-30 10:22:12 -04001267 u64 num_blocks, u64 hint_block,
Chris Masonbe08c1b2007-05-03 09:06:49 -04001268 u64 search_end, struct btrfs_key *ins, int data)
Chris Masonfec577f2007-02-26 10:40:21 -05001269{
1270 int ret;
1271 int pending_ret;
Chris Mason1261ec42007-03-20 20:35:03 -04001272 u64 super_blocks_used;
Chris Masonfbdc7622007-05-30 10:22:12 -04001273 u64 search_start = 0;
Chris Masonf2654de2007-06-26 12:20:46 -04001274 u64 exclude_start = 0;
1275 u64 exclude_nr = 0;
Chris Mason1261ec42007-03-20 20:35:03 -04001276 struct btrfs_fs_info *info = root->fs_info;
1277 struct btrfs_root *extent_root = info->extent_root;
Chris Mason234b63a2007-03-13 10:46:10 -04001278 struct btrfs_extent_item extent_item;
Chris Masonf2458e12007-04-25 15:52:25 -04001279 struct btrfs_key prealloc_key;
Chris Mason037e6392007-03-07 11:50:24 -05001280
Chris Masoncf27e1e2007-03-13 09:49:06 -04001281 btrfs_set_extent_refs(&extent_item, 1);
Chris Mason4d775672007-04-20 20:23:12 -04001282 btrfs_set_extent_owner(&extent_item, owner);
Chris Masonfec577f2007-02-26 10:40:21 -05001283
Chris Mason037e6392007-03-07 11:50:24 -05001284 if (root == extent_root) {
Chris Masonf2458e12007-04-25 15:52:25 -04001285 int nr;
1286 BUG_ON(info->extent_tree_prealloc_nr == 0);
Chris Mason037e6392007-03-07 11:50:24 -05001287 BUG_ON(num_blocks != 1);
Chris Mason037e6392007-03-07 11:50:24 -05001288 ins->offset = 1;
Chris Masonf2458e12007-04-25 15:52:25 -04001289 info->extent_tree_prealloc_nr--;
1290 nr = info->extent_tree_prealloc_nr;
1291 ins->objectid = info->extent_tree_prealloc[nr];
1292 info->extent_tree_insert[info->extent_tree_insert_nr++] =
1293 ins->objectid;
Chris Mason9078a3e2007-04-26 16:46:15 -04001294 ret = update_block_group(trans, root,
Chris Mason1e2677e2007-05-29 16:52:18 -04001295 ins->objectid, ins->offset, 1, 0, 0);
Chris Mason9078a3e2007-04-26 16:46:15 -04001296 BUG_ON(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05001297 return 0;
1298 }
Chris Masone37c9e62007-05-09 20:13:14 -04001299
1300 /*
1301 * if we're doing a data allocation, preallocate room in the
1302 * extent tree first. This way the extent tree blocks end up
1303 * in the correct block group.
1304 */
1305 if (data) {
Chris Masonde428b62007-05-18 13:28:27 -04001306 ret = find_free_extent(trans, root, 0, 0,
Chris Masonf2654de2007-06-26 12:20:46 -04001307 search_end, 0, &prealloc_key, 0, 0, 0);
Chris Masonccd467d2007-06-28 15:57:36 -04001308 BUG_ON(ret);
Chris Mason54aa1f42007-06-22 14:16:25 -04001309 if (ret)
1310 return ret;
Chris Masonf2654de2007-06-26 12:20:46 -04001311 exclude_nr = info->extent_tree_prealloc_nr;
1312 exclude_start = info->extent_tree_prealloc[exclude_nr - 1];
Chris Masone37c9e62007-05-09 20:13:14 -04001313 }
Chris Masonfec577f2007-02-26 10:40:21 -05001314
Chris Masonf2654de2007-06-26 12:20:46 -04001315 /* do the real allocation */
1316 ret = find_free_extent(trans, root, num_blocks, search_start,
1317 search_end, hint_block, ins,
1318 exclude_start, exclude_nr, data);
Chris Masonccd467d2007-06-28 15:57:36 -04001319 BUG_ON(ret);
Chris Masonf2654de2007-06-26 12:20:46 -04001320 if (ret)
1321 return ret;
1322
Chris Masone37c9e62007-05-09 20:13:14 -04001323 /*
1324 * if we're doing a metadata allocation, preallocate space in the
1325 * extent tree second. This way, we don't create a tiny hole
1326 * in the allocation map between any unused preallocation blocks
1327 * and the metadata block we're actually allocating. On disk,
1328 * it'll go:
1329 * [block we've allocated], [used prealloc 1], [ unused prealloc ]
1330 * The unused prealloc will get reused the next time around.
1331 */
1332 if (!data) {
Chris Masonf2654de2007-06-26 12:20:46 -04001333 exclude_start = ins->objectid;
1334 exclude_nr = ins->offset;
Chris Masonccd467d2007-06-28 15:57:36 -04001335 hint_block = exclude_start + exclude_nr;
Chris Masone37c9e62007-05-09 20:13:14 -04001336 ret = find_free_extent(trans, root, 0, search_start,
Chris Masonfbdc7622007-05-30 10:22:12 -04001337 search_end, hint_block,
Chris Masonf2654de2007-06-26 12:20:46 -04001338 &prealloc_key, exclude_start,
1339 exclude_nr, 0);
Chris Masonccd467d2007-06-28 15:57:36 -04001340 BUG_ON(ret);
Chris Masonf2654de2007-06-26 12:20:46 -04001341 if (ret)
1342 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -04001343 }
Chris Masonf2458e12007-04-25 15:52:25 -04001344
Chris Mason4b52dff2007-06-26 10:06:50 -04001345 super_blocks_used = btrfs_super_blocks_used(&info->super_copy);
1346 btrfs_set_super_blocks_used(&info->super_copy, super_blocks_used +
Chris Mason1261ec42007-03-20 20:35:03 -04001347 num_blocks);
Chris Masone089f052007-03-16 16:20:31 -04001348 ret = btrfs_insert_item(trans, extent_root, ins, &extent_item,
1349 sizeof(extent_item));
Chris Mason037e6392007-03-07 11:50:24 -05001350
Chris Masonccd467d2007-06-28 15:57:36 -04001351 BUG_ON(ret);
Chris Masone089f052007-03-16 16:20:31 -04001352 finish_current_insert(trans, extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -04001353 pending_ret = del_pending_extents(trans, extent_root);
Chris Masone37c9e62007-05-09 20:13:14 -04001354 if (ret) {
Chris Mason037e6392007-03-07 11:50:24 -05001355 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -04001356 }
1357 if (pending_ret) {
Chris Mason037e6392007-03-07 11:50:24 -05001358 return pending_ret;
Chris Masone37c9e62007-05-09 20:13:14 -04001359 }
Chris Mason1e2677e2007-05-29 16:52:18 -04001360 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0,
1361 data);
Chris Masonfabb5682007-06-07 22:13:21 -04001362 BUG_ON(ret);
Chris Mason037e6392007-03-07 11:50:24 -05001363 return 0;
Chris Masonfec577f2007-02-26 10:40:21 -05001364}
1365
1366/*
1367 * helper function to allocate a block for a given tree
1368 * returns the tree buffer or NULL.
1369 */
Chris Masone20d96d2007-03-22 12:13:20 -04001370struct buffer_head *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Chris Mason31f3c992007-04-30 15:25:45 -04001371 struct btrfs_root *root, u64 hint)
Chris Masonfec577f2007-02-26 10:40:21 -05001372{
Chris Masone2fa7222007-03-12 16:22:34 -04001373 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -05001374 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -04001375 struct buffer_head *buf;
Chris Masonfec577f2007-02-26 10:40:21 -05001376
Chris Mason4d775672007-04-20 20:23:12 -04001377 ret = btrfs_alloc_extent(trans, root, root->root_key.objectid,
Chris Masonde428b62007-05-18 13:28:27 -04001378 1, hint, (unsigned long)-1, &ins, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05001379 if (ret) {
Chris Mason54aa1f42007-06-22 14:16:25 -04001380 BUG_ON(ret > 0);
1381 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05001382 }
Chris Masond98237b2007-03-28 13:57:48 -04001383 buf = btrfs_find_create_tree_block(root, ins.objectid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001384 if (!buf) {
1385 btrfs_free_extent(trans, root, ins.objectid, 1, 0);
1386 return ERR_PTR(-ENOMEM);
1387 }
Chris Masondf2ce342007-03-23 11:00:45 -04001388 set_buffer_uptodate(buf);
Chris Mason090d1872007-05-01 08:53:32 -04001389 set_buffer_checked(buf);
Chris Mason7c4452b2007-04-28 09:29:35 -04001390 set_radix_bit(&trans->transaction->dirty_pages, buf->b_page->index);
Chris Masonfec577f2007-02-26 10:40:21 -05001391 return buf;
1392}
Chris Masona28ec192007-03-06 20:08:01 -05001393
Chris Mason6407bf62007-03-27 06:33:00 -04001394static int drop_leaf_ref(struct btrfs_trans_handle *trans,
1395 struct btrfs_root *root, struct buffer_head *cur)
1396{
1397 struct btrfs_disk_key *key;
1398 struct btrfs_leaf *leaf;
1399 struct btrfs_file_extent_item *fi;
1400 int i;
1401 int nritems;
1402 int ret;
1403
1404 BUG_ON(!btrfs_is_leaf(btrfs_buffer_node(cur)));
1405 leaf = btrfs_buffer_leaf(cur);
1406 nritems = btrfs_header_nritems(&leaf->header);
1407 for (i = 0; i < nritems; i++) {
Chris Mason3a686372007-05-24 13:35:57 -04001408 u64 disk_blocknr;
Chris Mason6407bf62007-03-27 06:33:00 -04001409 key = &leaf->items[i].key;
1410 if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY)
1411 continue;
1412 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
Chris Mason236454df2007-04-19 13:37:44 -04001413 if (btrfs_file_extent_type(fi) == BTRFS_FILE_EXTENT_INLINE)
1414 continue;
Chris Mason6407bf62007-03-27 06:33:00 -04001415 /*
1416 * FIXME make sure to insert a trans record that
1417 * repeats the snapshot del on crash
1418 */
Chris Mason3a686372007-05-24 13:35:57 -04001419 disk_blocknr = btrfs_file_extent_disk_blocknr(fi);
1420 if (disk_blocknr == 0)
1421 continue;
1422 ret = btrfs_free_extent(trans, root, disk_blocknr,
Chris Mason6407bf62007-03-27 06:33:00 -04001423 btrfs_file_extent_disk_num_blocks(fi),
1424 0);
1425 BUG_ON(ret);
1426 }
1427 return 0;
1428}
1429
Chris Masone0115992007-06-19 16:23:05 -04001430static void reada_walk_down(struct btrfs_root *root,
1431 struct btrfs_node *node)
1432{
1433 int i;
1434 u32 nritems;
1435 u64 blocknr;
1436 int ret;
1437 u32 refs;
1438
1439 nritems = btrfs_header_nritems(&node->header);
1440 for (i = 0; i < nritems; i++) {
1441 blocknr = btrfs_node_blockptr(node, i);
1442 ret = lookup_extent_ref(NULL, root, blocknr, 1, &refs);
1443 BUG_ON(ret);
1444 if (refs != 1)
1445 continue;
1446 ret = readahead_tree_block(root, blocknr);
1447 if (ret)
1448 break;
1449 }
1450}
1451
Chris Mason9aca1d52007-03-13 11:09:37 -04001452/*
1453 * helper function for drop_snapshot, this walks down the tree dropping ref
1454 * counts as it goes.
1455 */
Chris Masone089f052007-03-16 16:20:31 -04001456static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
1457 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05001458{
Chris Masone20d96d2007-03-22 12:13:20 -04001459 struct buffer_head *next;
1460 struct buffer_head *cur;
Chris Mason20524f02007-03-10 06:35:47 -05001461 u64 blocknr;
1462 int ret;
1463 u32 refs;
1464
Chris Mason5caf2a02007-04-02 11:20:42 -04001465 WARN_ON(*level < 0);
1466 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Masonb18c6682007-04-17 13:26:50 -04001467 ret = lookup_extent_ref(trans, root, bh_blocknr(path->nodes[*level]),
Chris Mason6407bf62007-03-27 06:33:00 -04001468 1, &refs);
Chris Mason20524f02007-03-10 06:35:47 -05001469 BUG_ON(ret);
1470 if (refs > 1)
1471 goto out;
Chris Masone0115992007-06-19 16:23:05 -04001472
Chris Mason9aca1d52007-03-13 11:09:37 -04001473 /*
1474 * walk down to the last node level and free all the leaves
1475 */
Chris Mason6407bf62007-03-27 06:33:00 -04001476 while(*level >= 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001477 WARN_ON(*level < 0);
1478 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason20524f02007-03-10 06:35:47 -05001479 cur = path->nodes[*level];
Chris Masone0115992007-06-19 16:23:05 -04001480
1481 if (*level > 0 && path->slots[*level] == 0)
1482 reada_walk_down(root, btrfs_buffer_node(cur));
1483
Chris Mason2c90e5d2007-04-02 10:50:19 -04001484 if (btrfs_header_level(btrfs_buffer_header(cur)) != *level)
1485 WARN_ON(1);
Chris Masone0115992007-06-19 16:23:05 -04001486
Chris Mason7518a232007-03-12 12:01:18 -04001487 if (path->slots[*level] >=
Chris Masone20d96d2007-03-22 12:13:20 -04001488 btrfs_header_nritems(btrfs_buffer_header(cur)))
Chris Mason20524f02007-03-10 06:35:47 -05001489 break;
Chris Mason6407bf62007-03-27 06:33:00 -04001490 if (*level == 0) {
1491 ret = drop_leaf_ref(trans, root, cur);
1492 BUG_ON(ret);
1493 break;
1494 }
Chris Masone20d96d2007-03-22 12:13:20 -04001495 blocknr = btrfs_node_blockptr(btrfs_buffer_node(cur),
1496 path->slots[*level]);
Chris Masonb18c6682007-04-17 13:26:50 -04001497 ret = lookup_extent_ref(trans, root, blocknr, 1, &refs);
Chris Mason6407bf62007-03-27 06:33:00 -04001498 BUG_ON(ret);
1499 if (refs != 1) {
Chris Mason20524f02007-03-10 06:35:47 -05001500 path->slots[*level]++;
Chris Masone089f052007-03-16 16:20:31 -04001501 ret = btrfs_free_extent(trans, root, blocknr, 1, 1);
Chris Mason20524f02007-03-10 06:35:47 -05001502 BUG_ON(ret);
1503 continue;
1504 }
Chris Mason20524f02007-03-10 06:35:47 -05001505 next = read_tree_block(root, blocknr);
Chris Mason5caf2a02007-04-02 11:20:42 -04001506 WARN_ON(*level <= 0);
Chris Mason83e15a22007-03-12 09:03:27 -04001507 if (path->nodes[*level-1])
Chris Mason234b63a2007-03-13 10:46:10 -04001508 btrfs_block_release(root, path->nodes[*level-1]);
Chris Mason20524f02007-03-10 06:35:47 -05001509 path->nodes[*level-1] = next;
Chris Masone20d96d2007-03-22 12:13:20 -04001510 *level = btrfs_header_level(btrfs_buffer_header(next));
Chris Mason20524f02007-03-10 06:35:47 -05001511 path->slots[*level] = 0;
1512 }
1513out:
Chris Mason5caf2a02007-04-02 11:20:42 -04001514 WARN_ON(*level < 0);
1515 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason6407bf62007-03-27 06:33:00 -04001516 ret = btrfs_free_extent(trans, root,
Chris Mason7eccb902007-04-11 15:53:25 -04001517 bh_blocknr(path->nodes[*level]), 1, 1);
Chris Mason234b63a2007-03-13 10:46:10 -04001518 btrfs_block_release(root, path->nodes[*level]);
Chris Mason20524f02007-03-10 06:35:47 -05001519 path->nodes[*level] = NULL;
1520 *level += 1;
1521 BUG_ON(ret);
1522 return 0;
1523}
1524
Chris Mason9aca1d52007-03-13 11:09:37 -04001525/*
1526 * helper for dropping snapshots. This walks back up the tree in the path
1527 * to find the first node higher up where we haven't yet gone through
1528 * all the slots
1529 */
Chris Masone089f052007-03-16 16:20:31 -04001530static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root
1531 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05001532{
1533 int i;
1534 int slot;
1535 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04001536 struct btrfs_root_item *root_item = &root->root_item;
1537
Chris Mason234b63a2007-03-13 10:46:10 -04001538 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Mason20524f02007-03-10 06:35:47 -05001539 slot = path->slots[i];
Chris Masone20d96d2007-03-22 12:13:20 -04001540 if (slot < btrfs_header_nritems(
1541 btrfs_buffer_header(path->nodes[i])) - 1) {
Chris Mason9f3a7422007-08-07 15:52:19 -04001542 struct btrfs_node *node;
1543 node = btrfs_buffer_node(path->nodes[i]);
Chris Mason20524f02007-03-10 06:35:47 -05001544 path->slots[i]++;
1545 *level = i;
Chris Mason9f3a7422007-08-07 15:52:19 -04001546 WARN_ON(*level == 0);
1547 memcpy(&root_item->drop_progress,
1548 &node->ptrs[path->slots[i]].key,
1549 sizeof(root_item->drop_progress));
1550 root_item->drop_level = i;
Chris Mason20524f02007-03-10 06:35:47 -05001551 return 0;
1552 } else {
Chris Masone089f052007-03-16 16:20:31 -04001553 ret = btrfs_free_extent(trans, root,
Chris Mason7eccb902007-04-11 15:53:25 -04001554 bh_blocknr(path->nodes[*level]),
Chris Masone089f052007-03-16 16:20:31 -04001555 1, 1);
Chris Mason6407bf62007-03-27 06:33:00 -04001556 BUG_ON(ret);
Chris Mason234b63a2007-03-13 10:46:10 -04001557 btrfs_block_release(root, path->nodes[*level]);
Chris Mason83e15a22007-03-12 09:03:27 -04001558 path->nodes[*level] = NULL;
Chris Mason20524f02007-03-10 06:35:47 -05001559 *level = i + 1;
Chris Mason20524f02007-03-10 06:35:47 -05001560 }
1561 }
1562 return 1;
1563}
1564
Chris Mason9aca1d52007-03-13 11:09:37 -04001565/*
1566 * drop the reference count on the tree rooted at 'snap'. This traverses
1567 * the tree freeing any blocks that have a ref count of zero after being
1568 * decremented.
1569 */
Chris Masone089f052007-03-16 16:20:31 -04001570int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason9f3a7422007-08-07 15:52:19 -04001571 *root)
Chris Mason20524f02007-03-10 06:35:47 -05001572{
Chris Mason3768f362007-03-13 16:47:54 -04001573 int ret = 0;
Chris Mason9aca1d52007-03-13 11:09:37 -04001574 int wret;
Chris Mason20524f02007-03-10 06:35:47 -05001575 int level;
Chris Mason5caf2a02007-04-02 11:20:42 -04001576 struct btrfs_path *path;
Chris Mason20524f02007-03-10 06:35:47 -05001577 int i;
1578 int orig_level;
Chris Mason9f3a7422007-08-07 15:52:19 -04001579 int num_walks = 0;
1580 struct btrfs_root_item *root_item = &root->root_item;
Chris Mason20524f02007-03-10 06:35:47 -05001581
Chris Mason5caf2a02007-04-02 11:20:42 -04001582 path = btrfs_alloc_path();
1583 BUG_ON(!path);
Chris Mason20524f02007-03-10 06:35:47 -05001584
Chris Mason9f3a7422007-08-07 15:52:19 -04001585 level = btrfs_header_level(btrfs_buffer_header(root->node));
Chris Mason20524f02007-03-10 06:35:47 -05001586 orig_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04001587 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
1588 path->nodes[level] = root->node;
1589 path->slots[level] = 0;
1590 } else {
1591 struct btrfs_key key;
1592 struct btrfs_disk_key *found_key;
1593 struct btrfs_node *node;
1594 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
1595 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1596 if (ret < 0) {
1597 ret = wret;
1598 goto out;
1599 }
1600 level = root_item->drop_level;
1601 node = btrfs_buffer_node(path->nodes[level]);
1602 found_key = &node->ptrs[path->slots[level]].key;
1603 WARN_ON(memcmp(found_key, &root_item->drop_progress,
1604 sizeof(*found_key)));
1605 }
Chris Mason20524f02007-03-10 06:35:47 -05001606 while(1) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001607 wret = walk_down_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04001608 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05001609 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04001610 if (wret < 0)
1611 ret = wret;
1612
Chris Mason5caf2a02007-04-02 11:20:42 -04001613 wret = walk_up_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04001614 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05001615 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04001616 if (wret < 0)
1617 ret = wret;
Chris Mason9f3a7422007-08-07 15:52:19 -04001618 num_walks++;
1619 if (num_walks > 10) {
1620 struct btrfs_key key;
1621 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
1622 ret = -EAGAIN;
1623 get_bh(root->node);
1624 break;
1625 }
Chris Mason20524f02007-03-10 06:35:47 -05001626 }
Chris Mason83e15a22007-03-12 09:03:27 -04001627 for (i = 0; i <= orig_level; i++) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001628 if (path->nodes[i]) {
1629 btrfs_block_release(root, path->nodes[i]);
Chris Mason83e15a22007-03-12 09:03:27 -04001630 }
Chris Mason20524f02007-03-10 06:35:47 -05001631 }
Chris Mason9f3a7422007-08-07 15:52:19 -04001632out:
Chris Mason5caf2a02007-04-02 11:20:42 -04001633 btrfs_free_path(path);
Chris Mason9aca1d52007-03-13 11:09:37 -04001634 return ret;
Chris Mason20524f02007-03-10 06:35:47 -05001635}
Chris Mason9078a3e2007-04-26 16:46:15 -04001636
Chris Masonbe744172007-05-06 10:15:01 -04001637static int free_block_group_radix(struct radix_tree_root *radix)
Chris Mason9078a3e2007-04-26 16:46:15 -04001638{
1639 int ret;
1640 struct btrfs_block_group_cache *cache[8];
1641 int i;
1642
1643 while(1) {
Chris Masonbe744172007-05-06 10:15:01 -04001644 ret = radix_tree_gang_lookup(radix, (void **)cache, 0,
Chris Mason9078a3e2007-04-26 16:46:15 -04001645 ARRAY_SIZE(cache));
1646 if (!ret)
1647 break;
1648 for (i = 0; i < ret; i++) {
Chris Masonbe744172007-05-06 10:15:01 -04001649 radix_tree_delete(radix, cache[i]->key.objectid +
Chris Mason9078a3e2007-04-26 16:46:15 -04001650 cache[i]->key.offset - 1);
1651 kfree(cache[i]);
1652 }
1653 }
1654 return 0;
1655}
1656
Chris Masonbe744172007-05-06 10:15:01 -04001657int btrfs_free_block_groups(struct btrfs_fs_info *info)
1658{
1659 int ret;
1660 int ret2;
Chris Masone37c9e62007-05-09 20:13:14 -04001661 unsigned long gang[16];
1662 int i;
Chris Masonbe744172007-05-06 10:15:01 -04001663
1664 ret = free_block_group_radix(&info->block_group_radix);
1665 ret2 = free_block_group_radix(&info->block_group_data_radix);
1666 if (ret)
1667 return ret;
1668 if (ret2)
1669 return ret2;
Chris Masone37c9e62007-05-09 20:13:14 -04001670
1671 while(1) {
1672 ret = find_first_radix_bit(&info->extent_map_radix,
1673 gang, 0, ARRAY_SIZE(gang));
1674 if (!ret)
1675 break;
1676 for (i = 0; i < ret; i++) {
1677 clear_radix_bit(&info->extent_map_radix, gang[i]);
1678 }
1679 }
Chris Masonbe744172007-05-06 10:15:01 -04001680 return 0;
1681}
1682
Chris Mason9078a3e2007-04-26 16:46:15 -04001683int btrfs_read_block_groups(struct btrfs_root *root)
1684{
1685 struct btrfs_path *path;
1686 int ret;
1687 int err = 0;
1688 struct btrfs_block_group_item *bi;
1689 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04001690 struct btrfs_fs_info *info = root->fs_info;
1691 struct radix_tree_root *radix;
Chris Mason9078a3e2007-04-26 16:46:15 -04001692 struct btrfs_key key;
1693 struct btrfs_key found_key;
1694 struct btrfs_leaf *leaf;
Chris Mason84f54cf2007-06-12 07:43:08 -04001695 u64 group_size_blocks;
Chris Mason31f3c992007-04-30 15:25:45 -04001696 u64 used;
Chris Mason9078a3e2007-04-26 16:46:15 -04001697
Chris Mason84f54cf2007-06-12 07:43:08 -04001698 group_size_blocks = BTRFS_BLOCK_GROUP_SIZE >>
1699 root->fs_info->sb->s_blocksize_bits;
Chris Masonbe744172007-05-06 10:15:01 -04001700 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04001701 key.objectid = 0;
1702 key.offset = group_size_blocks;
1703 key.flags = 0;
1704 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
1705
1706 path = btrfs_alloc_path();
1707 if (!path)
1708 return -ENOMEM;
1709
1710 while(1) {
Chris Masonbe744172007-05-06 10:15:01 -04001711 ret = btrfs_search_slot(NULL, info->extent_root,
Chris Mason9078a3e2007-04-26 16:46:15 -04001712 &key, path, 0, 0);
1713 if (ret != 0) {
1714 err = ret;
1715 break;
1716 }
1717 leaf = btrfs_buffer_leaf(path->nodes[0]);
1718 btrfs_disk_key_to_cpu(&found_key,
1719 &leaf->items[path->slots[0]].key);
1720 cache = kmalloc(sizeof(*cache), GFP_NOFS);
1721 if (!cache) {
1722 err = -1;
1723 break;
1724 }
Chris Mason3e1ad542007-05-07 20:03:49 -04001725
Chris Mason9078a3e2007-04-26 16:46:15 -04001726 bi = btrfs_item_ptr(leaf, path->slots[0],
1727 struct btrfs_block_group_item);
Chris Mason1e2677e2007-05-29 16:52:18 -04001728 if (bi->flags & BTRFS_BLOCK_GROUP_DATA) {
1729 radix = &info->block_group_data_radix;
1730 cache->data = 1;
1731 } else {
1732 radix = &info->block_group_radix;
1733 cache->data = 0;
1734 }
1735
Chris Mason9078a3e2007-04-26 16:46:15 -04001736 memcpy(&cache->item, bi, sizeof(*bi));
1737 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Mason31f3c992007-04-30 15:25:45 -04001738 cache->last_alloc = cache->key.objectid;
1739 cache->first_free = cache->key.objectid;
Chris Masone37c9e62007-05-09 20:13:14 -04001740 cache->last_prealloc = cache->key.objectid;
Chris Masonbe744172007-05-06 10:15:01 -04001741 cache->pinned = 0;
Chris Masone37c9e62007-05-09 20:13:14 -04001742 cache->cached = 0;
1743
Chris Mason3e1ad542007-05-07 20:03:49 -04001744 cache->radix = radix;
1745
Chris Mason9078a3e2007-04-26 16:46:15 -04001746 key.objectid = found_key.objectid + found_key.offset;
1747 btrfs_release_path(root, path);
Chris Masonbe744172007-05-06 10:15:01 -04001748 ret = radix_tree_insert(radix, found_key.objectid +
Chris Mason9078a3e2007-04-26 16:46:15 -04001749 found_key.offset - 1,
1750 (void *)cache);
1751 BUG_ON(ret);
Chris Mason31f3c992007-04-30 15:25:45 -04001752 used = btrfs_block_group_used(bi);
Chris Mason84f54cf2007-06-12 07:43:08 -04001753 if (used < div_factor(key.offset, 8)) {
Chris Masonbe744172007-05-06 10:15:01 -04001754 radix_tree_tag_set(radix, found_key.objectid +
Chris Mason31f3c992007-04-30 15:25:45 -04001755 found_key.offset - 1,
1756 BTRFS_BLOCK_GROUP_AVAIL);
1757 }
Chris Mason9078a3e2007-04-26 16:46:15 -04001758 if (key.objectid >=
Chris Mason4b52dff2007-06-26 10:06:50 -04001759 btrfs_super_total_blocks(&info->super_copy))
Chris Mason9078a3e2007-04-26 16:46:15 -04001760 break;
1761 }
1762
1763 btrfs_free_path(path);
1764 return 0;
1765}