blob: 01dc3057928737dd1b8f6c068a8c2b6b9f317ce1 [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
Chris Mason2e635a22007-03-21 11:12:56 -040019#include <linux/module.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 Masonde428b62007-05-18 13:28:27 -040035static void reada_extent_leaves(struct btrfs_root *root,
36 struct btrfs_path *path, u64 limit)
37{
38 struct btrfs_node *node;
39 int i;
40 int nritems;
41 u64 item_objectid;
42 u64 blocknr;
43 int slot;
44 int ret;
45
46 if (!path->nodes[1])
47 return;
48 node = btrfs_buffer_node(path->nodes[1]);
49 slot = path->slots[1] + 1;
50 nritems = btrfs_header_nritems(&node->header);
51 for (i = slot; i < nritems && i < slot + 8; i++) {
52 item_objectid = btrfs_disk_key_objectid(&node->ptrs[i].key);
53 if (item_objectid > limit)
54 break;
55 blocknr = btrfs_node_blockptr(node, i);
56 ret = readahead_tree_block(root, blocknr);
57 if (ret)
58 break;
59 }
60}
61
Chris Masone37c9e62007-05-09 20:13:14 -040062static int cache_block_group(struct btrfs_root *root,
63 struct btrfs_block_group_cache *block_group)
64{
65 struct btrfs_path *path;
66 int ret;
67 struct btrfs_key key;
68 struct btrfs_leaf *leaf;
69 struct radix_tree_root *extent_radix;
70 int slot;
71 u64 i;
72 u64 last = 0;
73 u64 hole_size;
Chris Masonde428b62007-05-18 13:28:27 -040074 u64 limit;
Chris Masone37c9e62007-05-09 20:13:14 -040075 int found = 0;
76
77 root = root->fs_info->extent_root;
78 extent_radix = &root->fs_info->extent_map_radix;
79
80 if (block_group->cached)
81 return 0;
82 if (block_group->data)
83 return 0;
84 path = btrfs_alloc_path();
85 if (!path)
86 return -ENOMEM;
Chris Masone37c9e62007-05-09 20:13:14 -040087 key.objectid = block_group->key.objectid;
88 key.flags = 0;
89 key.offset = 0;
90 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
91 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
92 if (ret < 0)
93 return ret;
94 if (ret && path->slots[0] > 0)
95 path->slots[0]--;
Chris Masonde428b62007-05-18 13:28:27 -040096 limit = block_group->key.objectid + block_group->key.offset;
97 reada_extent_leaves(root, path, limit);
Chris Masone37c9e62007-05-09 20:13:14 -040098 while(1) {
99 leaf = btrfs_buffer_leaf(path->nodes[0]);
100 slot = path->slots[0];
101 if (slot >= btrfs_header_nritems(&leaf->header)) {
Chris Masonde428b62007-05-18 13:28:27 -0400102 reada_extent_leaves(root, path, limit);
Chris Masone37c9e62007-05-09 20:13:14 -0400103 ret = btrfs_next_leaf(root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -0400104 if (ret < 0)
105 goto err;
Chris Masonde428b62007-05-18 13:28:27 -0400106 if (ret == 0) {
Chris Masone37c9e62007-05-09 20:13:14 -0400107 continue;
Chris Masonde428b62007-05-18 13:28:27 -0400108 } else {
Chris Masone37c9e62007-05-09 20:13:14 -0400109 if (found) {
110 hole_size = block_group->key.objectid +
111 block_group->key.offset - last;
112 } else {
113 last = block_group->key.objectid;
114 hole_size = block_group->key.offset;
115 }
116 for (i = 0; i < hole_size; i++) {
117 set_radix_bit(extent_radix,
118 last + i);
119 }
120 break;
121 }
122 }
123 btrfs_disk_key_to_cpu(&key, &leaf->items[slot].key);
124 if (key.objectid >= block_group->key.objectid +
125 block_group->key.offset) {
126 if (found) {
127 hole_size = block_group->key.objectid +
128 block_group->key.offset - last;
129 } else {
130 last = block_group->key.objectid;
131 hole_size = block_group->key.offset;
132 }
133 for (i = 0; i < hole_size; i++) {
134 set_radix_bit(extent_radix, last + i);
135 }
136 break;
137 }
138 if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) {
139 if (!found) {
140 last = key.objectid + key.offset;
141 found = 1;
142 } else {
143 hole_size = key.objectid - last;
144 for (i = 0; i < hole_size; i++) {
145 set_radix_bit(extent_radix, last + i);
146 }
147 last = key.objectid + key.offset;
148 }
149 }
150 path->slots[0]++;
151 }
152
153 block_group->cached = 1;
Chris Mason54aa1f42007-06-22 14:16:25 -0400154err:
Chris Masone37c9e62007-05-09 20:13:14 -0400155 btrfs_free_path(path);
156 return 0;
157}
158
Chris Mason5276aed2007-06-11 21:33:38 -0400159struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
160 btrfs_fs_info *info,
161 u64 blocknr)
Chris Masonbe744172007-05-06 10:15:01 -0400162{
163 struct btrfs_block_group_cache *block_group;
164 int ret;
165
166 ret = radix_tree_gang_lookup(&info->block_group_radix,
167 (void **)&block_group,
168 blocknr, 1);
169 if (ret) {
Chris Mason3e1ad542007-05-07 20:03:49 -0400170 if (block_group->key.objectid <= blocknr && blocknr <=
Chris Masonbe744172007-05-06 10:15:01 -0400171 block_group->key.objectid + block_group->key.offset)
172 return block_group;
173 }
174 ret = radix_tree_gang_lookup(&info->block_group_data_radix,
175 (void **)&block_group,
176 blocknr, 1);
177 if (ret) {
Chris Mason3e1ad542007-05-07 20:03:49 -0400178 if (block_group->key.objectid <= blocknr && blocknr <=
Chris Masonbe744172007-05-06 10:15:01 -0400179 block_group->key.objectid + block_group->key.offset)
180 return block_group;
181 }
Chris Masonbe744172007-05-06 10:15:01 -0400182 return NULL;
183}
184
Chris Masone37c9e62007-05-09 20:13:14 -0400185static u64 leaf_range(struct btrfs_root *root)
186{
187 u64 size = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason84f54cf2007-06-12 07:43:08 -0400188 do_div(size, sizeof(struct btrfs_extent_item) +
189 sizeof(struct btrfs_item));
Chris Masone37c9e62007-05-09 20:13:14 -0400190 return size;
191}
192
193static u64 find_search_start(struct btrfs_root *root,
194 struct btrfs_block_group_cache **cache_ret,
195 u64 search_start, int num)
196{
197 unsigned long gang[8];
198 int ret;
199 struct btrfs_block_group_cache *cache = *cache_ret;
200 u64 last = max(search_start, cache->key.objectid);
201
202 if (cache->data)
203 goto out;
204 if (num > 1) {
205 last = max(last, cache->last_prealloc);
206 }
207again:
Chris Mason54aa1f42007-06-22 14:16:25 -0400208 ret = cache_block_group(root, cache);
209 if (ret)
210 goto out;
Chris Masone37c9e62007-05-09 20:13:14 -0400211 while(1) {
212 ret = find_first_radix_bit(&root->fs_info->extent_map_radix,
213 gang, last, ARRAY_SIZE(gang));
214 if (!ret)
215 goto out;
216 last = gang[ret-1] + 1;
217 if (num > 1) {
218 if (ret != ARRAY_SIZE(gang)) {
219 goto new_group;
220 }
221 if (gang[ret-1] - gang[0] > leaf_range(root)) {
222 continue;
223 }
224 }
225 if (gang[0] >= cache->key.objectid + cache->key.offset) {
226 goto new_group;
227 }
228 return gang[0];
229 }
230out:
231 return max(cache->last_alloc, search_start);
232
233new_group:
Chris Mason5276aed2007-06-11 21:33:38 -0400234 cache = btrfs_lookup_block_group(root->fs_info,
235 last + cache->key.offset - 1);
Chris Masone37c9e62007-05-09 20:13:14 -0400236 if (!cache) {
237 return max((*cache_ret)->last_alloc, search_start);
238 }
239 cache = btrfs_find_block_group(root, cache,
Chris Masonde428b62007-05-18 13:28:27 -0400240 last + cache->key.offset - 1, 0, 0);
Chris Masone37c9e62007-05-09 20:13:14 -0400241 *cache_ret = cache;
242 goto again;
243}
244
Chris Mason84f54cf2007-06-12 07:43:08 -0400245static u64 div_factor(u64 num, int factor)
246{
247 num *= factor;
248 do_div(num, 10);
249 return num;
250}
251
Chris Mason31f3c992007-04-30 15:25:45 -0400252struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
253 struct btrfs_block_group_cache
Chris Masonbe744172007-05-06 10:15:01 -0400254 *hint, u64 search_start,
Chris Masonde428b62007-05-18 13:28:27 -0400255 int data, int owner)
Chris Masoncd1bc462007-04-27 10:08:34 -0400256{
257 struct btrfs_block_group_cache *cache[8];
Chris Mason31f3c992007-04-30 15:25:45 -0400258 struct btrfs_block_group_cache *found_group = NULL;
Chris Masoncd1bc462007-04-27 10:08:34 -0400259 struct btrfs_fs_info *info = root->fs_info;
Chris Masonbe744172007-05-06 10:15:01 -0400260 struct radix_tree_root *radix;
Chris Mason1e2677e2007-05-29 16:52:18 -0400261 struct radix_tree_root *swap_radix;
Chris Masoncd1bc462007-04-27 10:08:34 -0400262 u64 used;
Chris Mason31f3c992007-04-30 15:25:45 -0400263 u64 last = 0;
264 u64 hint_last;
Chris Masoncd1bc462007-04-27 10:08:34 -0400265 int i;
266 int ret;
Chris Mason31f3c992007-04-30 15:25:45 -0400267 int full_search = 0;
Chris Masonde428b62007-05-18 13:28:27 -0400268 int factor = 8;
Chris Mason1e2677e2007-05-29 16:52:18 -0400269 int data_swap = 0;
Chris Masonde428b62007-05-18 13:28:27 -0400270
271 if (!owner)
272 factor = 5;
Chris Masonbe744172007-05-06 10:15:01 -0400273
Chris Mason1e2677e2007-05-29 16:52:18 -0400274 if (data) {
Chris Masonbe744172007-05-06 10:15:01 -0400275 radix = &info->block_group_data_radix;
Chris Mason1e2677e2007-05-29 16:52:18 -0400276 swap_radix = &info->block_group_radix;
277 } else {
Chris Masonbe744172007-05-06 10:15:01 -0400278 radix = &info->block_group_radix;
Chris Mason1e2677e2007-05-29 16:52:18 -0400279 swap_radix = &info->block_group_data_radix;
280 }
Chris Masonbe744172007-05-06 10:15:01 -0400281
282 if (search_start) {
283 struct btrfs_block_group_cache *shint;
Chris Mason5276aed2007-06-11 21:33:38 -0400284 shint = btrfs_lookup_block_group(info, search_start);
Chris Masonbe744172007-05-06 10:15:01 -0400285 if (shint->data == data) {
286 used = btrfs_block_group_used(&shint->item);
287 if (used + shint->pinned <
Chris Mason84f54cf2007-06-12 07:43:08 -0400288 div_factor(shint->key.offset, factor)) {
Chris Masonbe744172007-05-06 10:15:01 -0400289 return shint;
290 }
291 }
292 }
293 if (hint && hint->data == data) {
Chris Mason31f3c992007-04-30 15:25:45 -0400294 used = btrfs_block_group_used(&hint->item);
Chris Mason84f54cf2007-06-12 07:43:08 -0400295 if (used + hint->pinned <
296 div_factor(hint->key.offset, factor)) {
Chris Mason31f3c992007-04-30 15:25:45 -0400297 return hint;
298 }
Chris Mason84f54cf2007-06-12 07:43:08 -0400299 if (used >= div_factor(hint->key.offset, 8)) {
Chris Masonbe744172007-05-06 10:15:01 -0400300 radix_tree_tag_clear(radix,
301 hint->key.objectid +
302 hint->key.offset - 1,
303 BTRFS_BLOCK_GROUP_AVAIL);
304 }
Chris Mason8d7be552007-05-10 11:24:42 -0400305 last = hint->key.offset * 3;
Chris Masonbe744172007-05-06 10:15:01 -0400306 if (hint->key.objectid >= last)
Chris Masone37c9e62007-05-09 20:13:14 -0400307 last = max(search_start + hint->key.offset - 1,
308 hint->key.objectid - last);
Chris Masonbe744172007-05-06 10:15:01 -0400309 else
310 last = hint->key.objectid + hint->key.offset;
Chris Mason31f3c992007-04-30 15:25:45 -0400311 hint_last = last;
312 } else {
Chris Masone37c9e62007-05-09 20:13:14 -0400313 if (hint)
314 hint_last = max(hint->key.objectid, search_start);
315 else
316 hint_last = search_start;
317
318 last = hint_last;
Chris Mason31f3c992007-04-30 15:25:45 -0400319 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400320 while(1) {
Chris Masonbe744172007-05-06 10:15:01 -0400321 ret = radix_tree_gang_lookup_tag(radix, (void **)cache,
Chris Masoncd1bc462007-04-27 10:08:34 -0400322 last, ARRAY_SIZE(cache),
Chris Mason31f3c992007-04-30 15:25:45 -0400323 BTRFS_BLOCK_GROUP_AVAIL);
Chris Masoncd1bc462007-04-27 10:08:34 -0400324 if (!ret)
325 break;
326 for (i = 0; i < ret; i++) {
Chris Masonbe08c1b2007-05-03 09:06:49 -0400327 last = cache[i]->key.objectid +
328 cache[i]->key.offset;
Chris Masoncd1bc462007-04-27 10:08:34 -0400329 used = btrfs_block_group_used(&cache[i]->item);
Chris Masonbe744172007-05-06 10:15:01 -0400330 if (used + cache[i]->pinned <
Chris Mason84f54cf2007-06-12 07:43:08 -0400331 div_factor(cache[i]->key.offset, factor)) {
Chris Mason31f3c992007-04-30 15:25:45 -0400332 found_group = cache[i];
333 goto found;
Chris Masoncd1bc462007-04-27 10:08:34 -0400334 }
Chris Mason84f54cf2007-06-12 07:43:08 -0400335 if (used >= div_factor(cache[i]->key.offset, 8)) {
Chris Masonbe744172007-05-06 10:15:01 -0400336 radix_tree_tag_clear(radix,
337 cache[i]->key.objectid +
338 cache[i]->key.offset - 1,
339 BTRFS_BLOCK_GROUP_AVAIL);
340 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400341 }
Chris Masonde428b62007-05-18 13:28:27 -0400342 cond_resched();
Chris Masoncd1bc462007-04-27 10:08:34 -0400343 }
Chris Mason31f3c992007-04-30 15:25:45 -0400344 last = hint_last;
345again:
Chris Masoncd1bc462007-04-27 10:08:34 -0400346 while(1) {
Chris Masonbe744172007-05-06 10:15:01 -0400347 ret = radix_tree_gang_lookup(radix, (void **)cache,
348 last, ARRAY_SIZE(cache));
Chris Masoncd1bc462007-04-27 10:08:34 -0400349 if (!ret)
350 break;
351 for (i = 0; i < ret; i++) {
Chris Masonbe08c1b2007-05-03 09:06:49 -0400352 last = cache[i]->key.objectid +
353 cache[i]->key.offset;
Chris Masoncd1bc462007-04-27 10:08:34 -0400354 used = btrfs_block_group_used(&cache[i]->item);
Chris Masonbe744172007-05-06 10:15:01 -0400355 if (used + cache[i]->pinned < cache[i]->key.offset) {
Chris Mason31f3c992007-04-30 15:25:45 -0400356 found_group = cache[i];
357 goto found;
Chris Masoncd1bc462007-04-27 10:08:34 -0400358 }
Chris Masonbe744172007-05-06 10:15:01 -0400359 if (used >= cache[i]->key.offset) {
360 radix_tree_tag_clear(radix,
361 cache[i]->key.objectid +
362 cache[i]->key.offset - 1,
363 BTRFS_BLOCK_GROUP_AVAIL);
364 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400365 }
Chris Masonde428b62007-05-18 13:28:27 -0400366 cond_resched();
Chris Masoncd1bc462007-04-27 10:08:34 -0400367 }
Chris Mason31f3c992007-04-30 15:25:45 -0400368 if (!full_search) {
Chris Masonbe744172007-05-06 10:15:01 -0400369 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -0400370 full_search = 1;
371 goto again;
372 }
Chris Mason1e2677e2007-05-29 16:52:18 -0400373 if (!data_swap) {
374 struct radix_tree_root *tmp = radix;
375 data_swap = 1;
376 radix = swap_radix;
377 swap_radix = tmp;
378 last = search_start;
379 goto again;
380 }
Chris Mason31f3c992007-04-30 15:25:45 -0400381 if (!found_group) {
Chris Masonbe744172007-05-06 10:15:01 -0400382 ret = radix_tree_gang_lookup(radix,
Chris Mason31f3c992007-04-30 15:25:45 -0400383 (void **)&found_group, 0, 1);
Chris Mason1e2677e2007-05-29 16:52:18 -0400384 if (ret == 0) {
385 ret = radix_tree_gang_lookup(swap_radix,
386 (void **)&found_group,
387 0, 1);
388 }
Chris Mason31f3c992007-04-30 15:25:45 -0400389 BUG_ON(ret != 1);
390 }
Chris Masonbe744172007-05-06 10:15:01 -0400391found:
Chris Mason31f3c992007-04-30 15:25:45 -0400392 return found_group;
Chris Masoncd1bc462007-04-27 10:08:34 -0400393}
394
Chris Masonb18c6682007-04-17 13:26:50 -0400395int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
396 struct btrfs_root *root,
397 u64 blocknr, u64 num_blocks)
Chris Mason02217ed2007-03-02 16:08:05 -0500398{
Chris Mason5caf2a02007-04-02 11:20:42 -0400399 struct btrfs_path *path;
Chris Mason02217ed2007-03-02 16:08:05 -0500400 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400401 struct btrfs_key key;
Chris Mason234b63a2007-03-13 10:46:10 -0400402 struct btrfs_leaf *l;
403 struct btrfs_extent_item *item;
Chris Masone2fa7222007-03-12 16:22:34 -0400404 struct btrfs_key ins;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400405 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500406
Chris Mason5caf2a02007-04-02 11:20:42 -0400407 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -0400408 if (!path)
409 return -ENOMEM;
410 ret = find_free_extent(trans, root->fs_info->extent_root, 0, 0,
Chris Masonf2654de2007-06-26 12:20:46 -0400411 (u64)-1, 0, &ins, 0, 0, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400412 if (ret) {
413 btrfs_free_path(path);
414 return ret;
415 }
Chris Mason02217ed2007-03-02 16:08:05 -0500416 key.objectid = blocknr;
417 key.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -0400418 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Mason6407bf62007-03-27 06:33:00 -0400419 key.offset = num_blocks;
Chris Mason5caf2a02007-04-02 11:20:42 -0400420 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -0400421 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400422 if (ret < 0)
423 return ret;
Chris Masona429e512007-04-18 16:15:28 -0400424 if (ret != 0) {
Chris Masona28ec192007-03-06 20:08:01 -0500425 BUG();
Chris Masona429e512007-04-18 16:15:28 -0400426 }
Chris Mason02217ed2007-03-02 16:08:05 -0500427 BUG_ON(ret != 0);
Chris Mason5caf2a02007-04-02 11:20:42 -0400428 l = btrfs_buffer_leaf(path->nodes[0]);
429 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400430 refs = btrfs_extent_refs(item);
431 btrfs_set_extent_refs(item, refs + 1);
Chris Mason5caf2a02007-04-02 11:20:42 -0400432 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masona28ec192007-03-06 20:08:01 -0500433
Chris Mason5caf2a02007-04-02 11:20:42 -0400434 btrfs_release_path(root->fs_info->extent_root, path);
435 btrfs_free_path(path);
Chris Mason9f5fae22007-03-20 14:38:32 -0400436 finish_current_insert(trans, root->fs_info->extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -0400437 del_pending_extents(trans, root->fs_info->extent_root);
Chris Mason02217ed2007-03-02 16:08:05 -0500438 return 0;
439}
440
Chris Masonb18c6682007-04-17 13:26:50 -0400441static int lookup_extent_ref(struct btrfs_trans_handle *trans,
442 struct btrfs_root *root, u64 blocknr,
443 u64 num_blocks, u32 *refs)
Chris Masona28ec192007-03-06 20:08:01 -0500444{
Chris Mason5caf2a02007-04-02 11:20:42 -0400445 struct btrfs_path *path;
Chris Masona28ec192007-03-06 20:08:01 -0500446 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400447 struct btrfs_key key;
Chris Mason234b63a2007-03-13 10:46:10 -0400448 struct btrfs_leaf *l;
449 struct btrfs_extent_item *item;
Chris Mason5caf2a02007-04-02 11:20:42 -0400450
451 path = btrfs_alloc_path();
Chris Masona28ec192007-03-06 20:08:01 -0500452 key.objectid = blocknr;
Chris Mason6407bf62007-03-27 06:33:00 -0400453 key.offset = num_blocks;
Chris Mason62e27492007-03-15 12:56:47 -0400454 key.flags = 0;
455 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Mason5caf2a02007-04-02 11:20:42 -0400456 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -0400457 0, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400458 if (ret < 0)
459 goto out;
Chris Masona28ec192007-03-06 20:08:01 -0500460 if (ret != 0)
461 BUG();
Chris Mason5caf2a02007-04-02 11:20:42 -0400462 l = btrfs_buffer_leaf(path->nodes[0]);
463 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400464 *refs = btrfs_extent_refs(item);
Chris Mason54aa1f42007-06-22 14:16:25 -0400465out:
Chris Mason5caf2a02007-04-02 11:20:42 -0400466 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -0500467 return 0;
468}
469
Chris Masonc5739bb2007-04-10 09:27:04 -0400470int btrfs_inc_root_ref(struct btrfs_trans_handle *trans,
471 struct btrfs_root *root)
472{
Chris Masonb18c6682007-04-17 13:26:50 -0400473 return btrfs_inc_extent_ref(trans, root, bh_blocknr(root->node), 1);
Chris Masonc5739bb2007-04-10 09:27:04 -0400474}
475
Chris Masone089f052007-03-16 16:20:31 -0400476int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Masone20d96d2007-03-22 12:13:20 -0400477 struct buffer_head *buf)
Chris Mason02217ed2007-03-02 16:08:05 -0500478{
479 u64 blocknr;
Chris Masone20d96d2007-03-22 12:13:20 -0400480 struct btrfs_node *buf_node;
Chris Mason6407bf62007-03-27 06:33:00 -0400481 struct btrfs_leaf *buf_leaf;
482 struct btrfs_disk_key *key;
483 struct btrfs_file_extent_item *fi;
Chris Mason02217ed2007-03-02 16:08:05 -0500484 int i;
Chris Mason6407bf62007-03-27 06:33:00 -0400485 int leaf;
486 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -0400487 int faili;
488 int err;
Chris Masona28ec192007-03-06 20:08:01 -0500489
Chris Mason3768f362007-03-13 16:47:54 -0400490 if (!root->ref_cows)
Chris Masona28ec192007-03-06 20:08:01 -0500491 return 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400492 buf_node = btrfs_buffer_node(buf);
Chris Mason6407bf62007-03-27 06:33:00 -0400493 leaf = btrfs_is_leaf(buf_node);
494 buf_leaf = btrfs_buffer_leaf(buf);
Chris Masone20d96d2007-03-22 12:13:20 -0400495 for (i = 0; i < btrfs_header_nritems(&buf_node->header); i++) {
Chris Mason6407bf62007-03-27 06:33:00 -0400496 if (leaf) {
Chris Mason3a686372007-05-24 13:35:57 -0400497 u64 disk_blocknr;
Chris Mason6407bf62007-03-27 06:33:00 -0400498 key = &buf_leaf->items[i].key;
499 if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY)
500 continue;
501 fi = btrfs_item_ptr(buf_leaf, i,
502 struct btrfs_file_extent_item);
Chris Mason236454d2007-04-19 13:37:44 -0400503 if (btrfs_file_extent_type(fi) ==
504 BTRFS_FILE_EXTENT_INLINE)
505 continue;
Chris Mason3a686372007-05-24 13:35:57 -0400506 disk_blocknr = btrfs_file_extent_disk_blocknr(fi);
507 if (disk_blocknr == 0)
508 continue;
509 ret = btrfs_inc_extent_ref(trans, root, disk_blocknr,
Chris Mason6407bf62007-03-27 06:33:00 -0400510 btrfs_file_extent_disk_num_blocks(fi));
Chris Mason54aa1f42007-06-22 14:16:25 -0400511 if (ret) {
512 faili = i;
513 goto fail;
514 }
Chris Mason6407bf62007-03-27 06:33:00 -0400515 } else {
516 blocknr = btrfs_node_blockptr(buf_node, i);
Chris Masonb18c6682007-04-17 13:26:50 -0400517 ret = btrfs_inc_extent_ref(trans, root, blocknr, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400518 if (ret) {
519 faili = i;
520 goto fail;
521 }
Chris Mason6407bf62007-03-27 06:33:00 -0400522 }
Chris Mason02217ed2007-03-02 16:08:05 -0500523 }
524 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -0400525fail:
526 for (i =0; i < faili; i++) {
527 if (leaf) {
528 u64 disk_blocknr;
529 key = &buf_leaf->items[i].key;
530 if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY)
531 continue;
532 fi = btrfs_item_ptr(buf_leaf, i,
533 struct btrfs_file_extent_item);
534 if (btrfs_file_extent_type(fi) ==
535 BTRFS_FILE_EXTENT_INLINE)
536 continue;
537 disk_blocknr = btrfs_file_extent_disk_blocknr(fi);
538 if (disk_blocknr == 0)
539 continue;
540 err = btrfs_free_extent(trans, root, disk_blocknr,
541 btrfs_file_extent_disk_num_blocks(fi), 0);
542 BUG_ON(err);
543 } else {
544 blocknr = btrfs_node_blockptr(buf_node, i);
545 err = btrfs_free_extent(trans, root, blocknr, 1, 0);
546 BUG_ON(err);
547 }
548 }
549 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -0500550}
551
Chris Mason9078a3e2007-04-26 16:46:15 -0400552static int write_one_cache_group(struct btrfs_trans_handle *trans,
553 struct btrfs_root *root,
554 struct btrfs_path *path,
555 struct btrfs_block_group_cache *cache)
556{
557 int ret;
558 int pending_ret;
559 struct btrfs_root *extent_root = root->fs_info->extent_root;
560 struct btrfs_block_group_item *bi;
561 struct btrfs_key ins;
562
Chris Masonf2654de2007-06-26 12:20:46 -0400563 ret = find_free_extent(trans, extent_root, 0, 0, (u64)-1, 0, &ins,
564 0, 0, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400565 /* FIXME, set bit to recalc cache groups on next mount */
566 if (ret)
567 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -0400568 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400569 if (ret < 0)
570 goto fail;
Chris Mason9078a3e2007-04-26 16:46:15 -0400571 BUG_ON(ret);
572 bi = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
573 struct btrfs_block_group_item);
574 memcpy(bi, &cache->item, sizeof(*bi));
575 mark_buffer_dirty(path->nodes[0]);
576 btrfs_release_path(extent_root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -0400577fail:
Chris Mason9078a3e2007-04-26 16:46:15 -0400578 finish_current_insert(trans, extent_root);
579 pending_ret = del_pending_extents(trans, extent_root);
580 if (ret)
581 return ret;
582 if (pending_ret)
583 return pending_ret;
Chris Masonbe744172007-05-06 10:15:01 -0400584 if (cache->data)
585 cache->last_alloc = cache->first_free;
Chris Mason9078a3e2007-04-26 16:46:15 -0400586 return 0;
587
588}
589
Chris Masonbe744172007-05-06 10:15:01 -0400590static int write_dirty_block_radix(struct btrfs_trans_handle *trans,
591 struct btrfs_root *root,
592 struct radix_tree_root *radix)
Chris Mason9078a3e2007-04-26 16:46:15 -0400593{
594 struct btrfs_block_group_cache *cache[8];
595 int ret;
596 int err = 0;
597 int werr = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -0400598 int i;
599 struct btrfs_path *path;
Chris Mason54aa1f42007-06-22 14:16:25 -0400600 unsigned long off = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -0400601
602 path = btrfs_alloc_path();
603 if (!path)
604 return -ENOMEM;
605
606 while(1) {
607 ret = radix_tree_gang_lookup_tag(radix, (void **)cache,
Chris Mason54aa1f42007-06-22 14:16:25 -0400608 off, ARRAY_SIZE(cache),
Chris Mason9078a3e2007-04-26 16:46:15 -0400609 BTRFS_BLOCK_GROUP_DIRTY);
610 if (!ret)
611 break;
612 for (i = 0; i < ret; i++) {
Chris Mason54aa1f42007-06-22 14:16:25 -0400613 err = write_one_cache_group(trans, root,
614 path, cache[i]);
615 /*
616 * if we fail to write the cache group, we want
617 * to keep it marked dirty in hopes that a later
618 * write will work
619 */
620 if (err) {
621 werr = err;
622 off = cache[i]->key.objectid +
623 cache[i]->key.offset;
624 continue;
625 }
626
Chris Mason9078a3e2007-04-26 16:46:15 -0400627 radix_tree_tag_clear(radix, cache[i]->key.objectid +
628 cache[i]->key.offset - 1,
629 BTRFS_BLOCK_GROUP_DIRTY);
Chris Mason9078a3e2007-04-26 16:46:15 -0400630 }
631 }
632 btrfs_free_path(path);
633 return werr;
634}
635
Chris Masonbe744172007-05-06 10:15:01 -0400636int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
637 struct btrfs_root *root)
638{
639 int ret;
640 int ret2;
641 ret = write_dirty_block_radix(trans, root,
642 &root->fs_info->block_group_radix);
643 ret2 = write_dirty_block_radix(trans, root,
644 &root->fs_info->block_group_data_radix);
645 if (ret)
646 return ret;
647 if (ret2)
648 return ret2;
649 return 0;
650}
651
Chris Mason9078a3e2007-04-26 16:46:15 -0400652static int update_block_group(struct btrfs_trans_handle *trans,
653 struct btrfs_root *root,
Chris Mason1e2677e2007-05-29 16:52:18 -0400654 u64 blocknr, u64 num, int alloc, int mark_free,
655 int data)
Chris Mason9078a3e2007-04-26 16:46:15 -0400656{
657 struct btrfs_block_group_cache *cache;
658 struct btrfs_fs_info *info = root->fs_info;
659 u64 total = num;
660 u64 old_val;
661 u64 block_in_group;
Chris Masone37c9e62007-05-09 20:13:14 -0400662 u64 i;
Chris Mason1e2677e2007-05-29 16:52:18 -0400663 int ret;
Chris Mason3e1ad542007-05-07 20:03:49 -0400664
Chris Mason9078a3e2007-04-26 16:46:15 -0400665 while(total) {
Chris Mason5276aed2007-06-11 21:33:38 -0400666 cache = btrfs_lookup_block_group(info, blocknr);
Chris Mason3e1ad542007-05-07 20:03:49 -0400667 if (!cache) {
Chris Mason9078a3e2007-04-26 16:46:15 -0400668 return -1;
Chris Masoncd1bc462007-04-27 10:08:34 -0400669 }
Chris Mason9078a3e2007-04-26 16:46:15 -0400670 block_in_group = blocknr - cache->key.objectid;
671 WARN_ON(block_in_group > cache->key.offset);
Chris Mason3e1ad542007-05-07 20:03:49 -0400672 radix_tree_tag_set(cache->radix, cache->key.objectid +
Chris Masonbe744172007-05-06 10:15:01 -0400673 cache->key.offset - 1,
Chris Mason9078a3e2007-04-26 16:46:15 -0400674 BTRFS_BLOCK_GROUP_DIRTY);
675
676 old_val = btrfs_block_group_used(&cache->item);
677 num = min(total, cache->key.offset - block_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -0400678 if (alloc) {
Chris Masoncd1bc462007-04-27 10:08:34 -0400679 if (blocknr > cache->last_alloc)
680 cache->last_alloc = blocknr;
Chris Masone37c9e62007-05-09 20:13:14 -0400681 if (!cache->data) {
682 for (i = 0; i < num; i++) {
683 clear_radix_bit(&info->extent_map_radix,
684 blocknr + i);
685 }
686 }
Chris Mason1e2677e2007-05-29 16:52:18 -0400687 if (cache->data != data &&
Chris Mason84f54cf2007-06-12 07:43:08 -0400688 old_val < (cache->key.offset >> 1)) {
Chris Mason1e2677e2007-05-29 16:52:18 -0400689 cache->data = data;
690 radix_tree_delete(cache->radix,
691 cache->key.objectid +
692 cache->key.offset - 1);
693
694 if (data) {
695 cache->radix =
696 &info->block_group_data_radix;
697 cache->item.flags |=
698 BTRFS_BLOCK_GROUP_DATA;
699 } else {
700 cache->radix = &info->block_group_radix;
701 cache->item.flags &=
702 ~BTRFS_BLOCK_GROUP_DATA;
703 }
704 ret = radix_tree_insert(cache->radix,
705 cache->key.objectid +
706 cache->key.offset - 1,
707 (void *)cache);
708 }
709 old_val += num;
Chris Masoncd1bc462007-04-27 10:08:34 -0400710 } else {
Chris Mason9078a3e2007-04-26 16:46:15 -0400711 old_val -= num;
Chris Masoncd1bc462007-04-27 10:08:34 -0400712 if (blocknr < cache->first_free)
713 cache->first_free = blocknr;
Chris Masone37c9e62007-05-09 20:13:14 -0400714 if (!cache->data && mark_free) {
715 for (i = 0; i < num; i++) {
716 set_radix_bit(&info->extent_map_radix,
717 blocknr + i);
718 }
719 }
Chris Mason84f54cf2007-06-12 07:43:08 -0400720 if (old_val < (cache->key.offset >> 1) &&
721 old_val + num >= (cache->key.offset >> 1)) {
Chris Masone37c9e62007-05-09 20:13:14 -0400722 radix_tree_tag_set(cache->radix,
723 cache->key.objectid +
724 cache->key.offset - 1,
725 BTRFS_BLOCK_GROUP_AVAIL);
726 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400727 }
Chris Mason9078a3e2007-04-26 16:46:15 -0400728 btrfs_set_block_group_used(&cache->item, old_val);
Chris Masone37c9e62007-05-09 20:13:14 -0400729 total -= num;
730 blocknr += num;
Chris Mason9078a3e2007-04-26 16:46:15 -0400731 }
732 return 0;
733}
734
Chris Masonbe08c1b2007-05-03 09:06:49 -0400735static int try_remove_page(struct address_space *mapping, unsigned long index)
736{
737 int ret;
738 ret = invalidate_mapping_pages(mapping, index, index);
739 return ret;
740}
741
Chris Masone089f052007-03-16 16:20:31 -0400742int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, struct
743 btrfs_root *root)
Chris Masona28ec192007-03-06 20:08:01 -0500744{
Chris Mason8ef97622007-03-26 10:15:30 -0400745 unsigned long gang[8];
Chris Masonbe08c1b2007-05-03 09:06:49 -0400746 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Masonbe744172007-05-06 10:15:01 -0400747 struct btrfs_block_group_cache *block_group;
Chris Mason88fd1462007-03-16 08:56:18 -0400748 u64 first = 0;
Chris Masona28ec192007-03-06 20:08:01 -0500749 int ret;
750 int i;
Chris Mason8ef97622007-03-26 10:15:30 -0400751 struct radix_tree_root *pinned_radix = &root->fs_info->pinned_radix;
Chris Masone37c9e62007-05-09 20:13:14 -0400752 struct radix_tree_root *extent_radix = &root->fs_info->extent_map_radix;
Chris Masona28ec192007-03-06 20:08:01 -0500753
754 while(1) {
Chris Masone37c9e62007-05-09 20:13:14 -0400755 ret = find_first_radix_bit(pinned_radix, gang, 0,
Chris Mason8ef97622007-03-26 10:15:30 -0400756 ARRAY_SIZE(gang));
Chris Masona28ec192007-03-06 20:08:01 -0500757 if (!ret)
758 break;
Chris Mason88fd1462007-03-16 08:56:18 -0400759 if (!first)
Chris Mason8ef97622007-03-26 10:15:30 -0400760 first = gang[0];
Chris Mason0579da42007-03-07 16:15:30 -0500761 for (i = 0; i < ret; i++) {
Chris Mason8ef97622007-03-26 10:15:30 -0400762 clear_radix_bit(pinned_radix, gang[i]);
Chris Mason5276aed2007-06-11 21:33:38 -0400763 block_group = btrfs_lookup_block_group(root->fs_info,
764 gang[i]);
Chris Masonbe744172007-05-06 10:15:01 -0400765 if (block_group) {
766 WARN_ON(block_group->pinned == 0);
767 block_group->pinned--;
768 if (gang[i] < block_group->last_alloc)
769 block_group->last_alloc = gang[i];
Chris Masone37c9e62007-05-09 20:13:14 -0400770 if (gang[i] < block_group->last_prealloc)
771 block_group->last_prealloc = gang[i];
772 if (!block_group->data)
773 set_radix_bit(extent_radix, gang[i]);
Chris Masonbe744172007-05-06 10:15:01 -0400774 }
Chris Masonbe08c1b2007-05-03 09:06:49 -0400775 try_remove_page(btree_inode->i_mapping,
776 gang[i] << (PAGE_CACHE_SHIFT -
777 btree_inode->i_blkbits));
Chris Mason0579da42007-03-07 16:15:30 -0500778 }
Chris Masona28ec192007-03-06 20:08:01 -0500779 }
780 return 0;
781}
782
Chris Masone089f052007-03-16 16:20:31 -0400783static int finish_current_insert(struct btrfs_trans_handle *trans, struct
784 btrfs_root *extent_root)
Chris Mason037e6392007-03-07 11:50:24 -0500785{
Chris Masone2fa7222007-03-12 16:22:34 -0400786 struct btrfs_key ins;
Chris Mason234b63a2007-03-13 10:46:10 -0400787 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -0500788 int i;
789 int ret;
Chris Mason1261ec42007-03-20 20:35:03 -0400790 u64 super_blocks_used;
791 struct btrfs_fs_info *info = extent_root->fs_info;
Chris Mason037e6392007-03-07 11:50:24 -0500792
Chris Masoncf27e1e2007-03-13 09:49:06 -0400793 btrfs_set_extent_refs(&extent_item, 1);
Chris Mason037e6392007-03-07 11:50:24 -0500794 ins.offset = 1;
795 ins.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -0400796 btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
Chris Mason5d0c3e62007-04-23 17:01:05 -0400797 btrfs_set_extent_owner(&extent_item, extent_root->root_key.objectid);
Chris Mason037e6392007-03-07 11:50:24 -0500798
Chris Masonf2458e12007-04-25 15:52:25 -0400799 for (i = 0; i < extent_root->fs_info->extent_tree_insert_nr; i++) {
800 ins.objectid = extent_root->fs_info->extent_tree_insert[i];
Chris Mason4b52dff2007-06-26 10:06:50 -0400801 super_blocks_used = btrfs_super_blocks_used(&info->super_copy);
802 btrfs_set_super_blocks_used(&info->super_copy,
Chris Mason1261ec42007-03-20 20:35:03 -0400803 super_blocks_used + 1);
Chris Masone089f052007-03-16 16:20:31 -0400804 ret = btrfs_insert_item(trans, extent_root, &ins, &extent_item,
805 sizeof(extent_item));
Chris Mason037e6392007-03-07 11:50:24 -0500806 BUG_ON(ret);
807 }
Chris Masonf2458e12007-04-25 15:52:25 -0400808 extent_root->fs_info->extent_tree_insert_nr = 0;
Chris Mason037e6392007-03-07 11:50:24 -0500809 return 0;
810}
811
Chris Mason8ef97622007-03-26 10:15:30 -0400812static int pin_down_block(struct btrfs_root *root, u64 blocknr, int pending)
Chris Masone20d96d2007-03-22 12:13:20 -0400813{
814 int err;
Chris Mason78fae272007-03-25 11:35:08 -0400815 struct btrfs_header *header;
Chris Mason8ef97622007-03-26 10:15:30 -0400816 struct buffer_head *bh;
Chris Mason78fae272007-03-25 11:35:08 -0400817
Chris Masonf4b9aa82007-03-27 11:05:53 -0400818 if (!pending) {
Chris Masond98237b2007-03-28 13:57:48 -0400819 bh = btrfs_find_tree_block(root, blocknr);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400820 if (bh) {
821 if (buffer_uptodate(bh)) {
822 u64 transid =
823 root->fs_info->running_transaction->transid;
824 header = btrfs_buffer_header(bh);
825 if (btrfs_header_generation(header) ==
826 transid) {
827 btrfs_block_release(root, bh);
828 return 0;
829 }
Chris Masonf4b9aa82007-03-27 11:05:53 -0400830 }
Chris Masond6025572007-03-30 14:27:56 -0400831 btrfs_block_release(root, bh);
Chris Mason8ef97622007-03-26 10:15:30 -0400832 }
Chris Mason8ef97622007-03-26 10:15:30 -0400833 err = set_radix_bit(&root->fs_info->pinned_radix, blocknr);
Chris Masonbe744172007-05-06 10:15:01 -0400834 if (!err) {
835 struct btrfs_block_group_cache *cache;
Chris Mason5276aed2007-06-11 21:33:38 -0400836 cache = btrfs_lookup_block_group(root->fs_info,
837 blocknr);
Chris Masonbe744172007-05-06 10:15:01 -0400838 if (cache)
839 cache->pinned++;
840 }
Chris Masonf4b9aa82007-03-27 11:05:53 -0400841 } else {
842 err = set_radix_bit(&root->fs_info->pending_del_radix, blocknr);
843 }
Chris Masonbe744172007-05-06 10:15:01 -0400844 BUG_ON(err < 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400845 return 0;
846}
847
Chris Masona28ec192007-03-06 20:08:01 -0500848/*
849 * remove an extent from the root, returns 0 on success
850 */
Chris Masone089f052007-03-16 16:20:31 -0400851static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masone37c9e62007-05-09 20:13:14 -0400852 *root, u64 blocknr, u64 num_blocks, int pin,
853 int mark_free)
Chris Masona28ec192007-03-06 20:08:01 -0500854{
Chris Mason5caf2a02007-04-02 11:20:42 -0400855 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -0400856 struct btrfs_key key;
Chris Mason1261ec42007-03-20 20:35:03 -0400857 struct btrfs_fs_info *info = root->fs_info;
858 struct btrfs_root *extent_root = info->extent_root;
Chris Masona28ec192007-03-06 20:08:01 -0500859 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400860 struct btrfs_extent_item *ei;
Chris Masone2fa7222007-03-12 16:22:34 -0400861 struct btrfs_key ins;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400862 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500863
Chris Masona28ec192007-03-06 20:08:01 -0500864 key.objectid = blocknr;
865 key.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -0400866 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masona28ec192007-03-06 20:08:01 -0500867 key.offset = num_blocks;
868
Chris Mason5caf2a02007-04-02 11:20:42 -0400869 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -0400870 if (!path)
871 return -ENOMEM;
872
Chris Masonf2654de2007-06-26 12:20:46 -0400873 ret = find_free_extent(trans, root, 0, 0, (u64)-1, 0, &ins, 0, 0, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400874 if (ret) {
875 btrfs_free_path(path);
876 return ret;
877 }
Chris Mason5f26f772007-04-05 10:38:44 -0400878
Chris Mason5caf2a02007-04-02 11:20:42 -0400879 ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400880 if (ret < 0)
881 return ret;
882 BUG_ON(ret);
Chris Mason5caf2a02007-04-02 11:20:42 -0400883 ei = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
Chris Mason123abc82007-03-14 14:14:43 -0400884 struct btrfs_extent_item);
Chris Masona28ec192007-03-06 20:08:01 -0500885 BUG_ON(ei->refs == 0);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400886 refs = btrfs_extent_refs(ei) - 1;
887 btrfs_set_extent_refs(ei, refs);
Chris Mason5caf2a02007-04-02 11:20:42 -0400888 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400889 if (refs == 0) {
Chris Mason1261ec42007-03-20 20:35:03 -0400890 u64 super_blocks_used;
Chris Mason78fae272007-03-25 11:35:08 -0400891
892 if (pin) {
Chris Mason8ef97622007-03-26 10:15:30 -0400893 ret = pin_down_block(root, blocknr, 0);
Chris Mason78fae272007-03-25 11:35:08 -0400894 BUG_ON(ret);
895 }
896
Chris Mason4b52dff2007-06-26 10:06:50 -0400897 super_blocks_used = btrfs_super_blocks_used(&info->super_copy);
898 btrfs_set_super_blocks_used(&info->super_copy,
Chris Mason1261ec42007-03-20 20:35:03 -0400899 super_blocks_used - num_blocks);
Chris Mason5caf2a02007-04-02 11:20:42 -0400900 ret = btrfs_del_item(trans, extent_root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -0400901 if (ret) {
902 return ret;
903 }
Chris Masone37c9e62007-05-09 20:13:14 -0400904 ret = update_block_group(trans, root, blocknr, num_blocks, 0,
Chris Mason1e2677e2007-05-29 16:52:18 -0400905 mark_free, 0);
Chris Mason9078a3e2007-04-26 16:46:15 -0400906 BUG_ON(ret);
Chris Masona28ec192007-03-06 20:08:01 -0500907 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400908 btrfs_free_path(path);
Chris Masone089f052007-03-16 16:20:31 -0400909 finish_current_insert(trans, extent_root);
Chris Masona28ec192007-03-06 20:08:01 -0500910 return ret;
911}
912
913/*
Chris Masonfec577f2007-02-26 10:40:21 -0500914 * find all the blocks marked as pending in the radix tree and remove
915 * them from the extent map
916 */
Chris Masone089f052007-03-16 16:20:31 -0400917static int del_pending_extents(struct btrfs_trans_handle *trans, struct
918 btrfs_root *extent_root)
Chris Masonfec577f2007-02-26 10:40:21 -0500919{
920 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -0400921 int wret;
922 int err = 0;
Chris Mason8ef97622007-03-26 10:15:30 -0400923 unsigned long gang[4];
Chris Masonfec577f2007-02-26 10:40:21 -0500924 int i;
Chris Mason8ef97622007-03-26 10:15:30 -0400925 struct radix_tree_root *pending_radix;
926 struct radix_tree_root *pinned_radix;
Chris Masonbe744172007-05-06 10:15:01 -0400927 struct btrfs_block_group_cache *cache;
Chris Mason8ef97622007-03-26 10:15:30 -0400928
929 pending_radix = &extent_root->fs_info->pending_del_radix;
930 pinned_radix = &extent_root->fs_info->pinned_radix;
Chris Masonfec577f2007-02-26 10:40:21 -0500931
932 while(1) {
Chris Masone37c9e62007-05-09 20:13:14 -0400933 ret = find_first_radix_bit(pending_radix, gang, 0,
Chris Mason8ef97622007-03-26 10:15:30 -0400934 ARRAY_SIZE(gang));
Chris Masonfec577f2007-02-26 10:40:21 -0500935 if (!ret)
936 break;
937 for (i = 0; i < ret; i++) {
Chris Mason8ef97622007-03-26 10:15:30 -0400938 wret = set_radix_bit(pinned_radix, gang[i]);
Chris Masonbe744172007-05-06 10:15:01 -0400939 if (wret == 0) {
Chris Mason5276aed2007-06-11 21:33:38 -0400940 cache =
941 btrfs_lookup_block_group(extent_root->fs_info,
Chris Masonbe744172007-05-06 10:15:01 -0400942 gang[i]);
943 if (cache)
944 cache->pinned++;
945 }
946 if (wret < 0) {
947 printk(KERN_CRIT "set_radix_bit, err %d\n",
948 wret);
949 BUG_ON(wret < 0);
950 }
Chris Mason8ef97622007-03-26 10:15:30 -0400951 wret = clear_radix_bit(pending_radix, gang[i]);
952 BUG_ON(wret);
Chris Masond5719762007-03-23 10:01:08 -0400953 wret = __free_extent(trans, extent_root,
Chris Masone37c9e62007-05-09 20:13:14 -0400954 gang[i], 1, 0, 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400955 if (wret)
956 err = wret;
Chris Masonfec577f2007-02-26 10:40:21 -0500957 }
958 }
Chris Masone20d96d2007-03-22 12:13:20 -0400959 return err;
Chris Masonfec577f2007-02-26 10:40:21 -0500960}
961
962/*
963 * remove an extent from the root, returns 0 on success
964 */
Chris Masone089f052007-03-16 16:20:31 -0400965int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
966 *root, u64 blocknr, u64 num_blocks, int pin)
Chris Masonfec577f2007-02-26 10:40:21 -0500967{
Chris Mason9f5fae22007-03-20 14:38:32 -0400968 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Masonfec577f2007-02-26 10:40:21 -0500969 int pending_ret;
970 int ret;
Chris Masona28ec192007-03-06 20:08:01 -0500971
972 if (root == extent_root) {
Chris Mason8ef97622007-03-26 10:15:30 -0400973 pin_down_block(root, blocknr, 1);
Chris Masona28ec192007-03-06 20:08:01 -0500974 return 0;
975 }
Chris Masone37c9e62007-05-09 20:13:14 -0400976 ret = __free_extent(trans, root, blocknr, num_blocks, pin, pin == 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400977 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
Chris Masonfec577f2007-02-26 10:40:21 -0500978 return ret ? ret : pending_ret;
979}
980
981/*
982 * walks the btree of allocated extents and find a hole of a given size.
983 * The key ins is changed to record the hole:
984 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -0400985 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -0500986 * ins->offset == number of blocks
987 * Any available blocks before search_start are skipped.
988 */
Chris Masone089f052007-03-16 16:20:31 -0400989static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
990 *orig_root, u64 num_blocks, u64 search_start, u64
Chris Masonfbdc7622007-05-30 10:22:12 -0400991 search_end, u64 hint_block,
Chris Masonf2654de2007-06-26 12:20:46 -0400992 struct btrfs_key *ins, u64 exclude_start,
993 u64 exclude_nr, int data)
Chris Masonfec577f2007-02-26 10:40:21 -0500994{
Chris Mason5caf2a02007-04-02 11:20:42 -0400995 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -0400996 struct btrfs_key key;
Chris Masonfec577f2007-02-26 10:40:21 -0500997 int ret;
998 u64 hole_size = 0;
999 int slot = 0;
Chris Masone20d96d2007-03-22 12:13:20 -04001000 u64 last_block = 0;
Chris Mason037e6392007-03-07 11:50:24 -05001001 u64 test_block;
Chris Masonbe744172007-05-06 10:15:01 -04001002 u64 orig_search_start = search_start;
Chris Masonfec577f2007-02-26 10:40:21 -05001003 int start_found;
Chris Mason234b63a2007-03-13 10:46:10 -04001004 struct btrfs_leaf *l;
Chris Mason9f5fae22007-03-20 14:38:32 -04001005 struct btrfs_root * root = orig_root->fs_info->extent_root;
Chris Masonf2458e12007-04-25 15:52:25 -04001006 struct btrfs_fs_info *info = root->fs_info;
Chris Mason0579da42007-03-07 16:15:30 -05001007 int total_needed = num_blocks;
Chris Masonf2458e12007-04-25 15:52:25 -04001008 int total_found = 0;
1009 int fill_prealloc = 0;
Chris Masone20d96d2007-03-22 12:13:20 -04001010 int level;
Chris Masonbe08c1b2007-05-03 09:06:49 -04001011 struct btrfs_block_group_cache *block_group;
Chris Masonbe744172007-05-06 10:15:01 -04001012 int full_scan = 0;
Chris Masonfbdc7622007-05-30 10:22:12 -04001013 int wrapped = 0;
Chris Masonde428b62007-05-18 13:28:27 -04001014 u64 limit;
Chris Masonfec577f2007-02-26 10:40:21 -05001015
Chris Masonb1a4d962007-04-04 15:27:52 -04001016 ins->flags = 0;
1017 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
1018
Chris Masone20d96d2007-03-22 12:13:20 -04001019 level = btrfs_header_level(btrfs_buffer_header(root->node));
Chris Masonf2458e12007-04-25 15:52:25 -04001020 if (num_blocks == 0) {
1021 fill_prealloc = 1;
1022 num_blocks = 1;
Chris Mason308535a2007-04-28 15:17:08 -04001023 total_needed = (min(level + 1, BTRFS_MAX_LEVEL) + 2) * 3;
Chris Masonf2458e12007-04-25 15:52:25 -04001024 }
Chris Mason85e55b12007-06-19 15:50:51 -04001025 if (fill_prealloc) {
1026 u64 first;
1027 int nr = info->extent_tree_prealloc_nr;
1028 first = info->extent_tree_prealloc[nr - 1];
1029 if (info->extent_tree_prealloc_nr >= total_needed &&
1030 first >= search_start) {
1031 ins->objectid = info->extent_tree_prealloc[0];
1032 ins->offset = 1;
1033 return 0;
1034 }
1035 info->extent_tree_prealloc_nr = 0;
1036 }
Chris Mason3e1ad542007-05-07 20:03:49 -04001037 if (search_end == (u64)-1)
Chris Mason4b52dff2007-06-26 10:06:50 -04001038 search_end = btrfs_super_total_blocks(&info->super_copy);
Chris Masonfbdc7622007-05-30 10:22:12 -04001039 if (hint_block) {
Chris Mason5276aed2007-06-11 21:33:38 -04001040 block_group = btrfs_lookup_block_group(info, hint_block);
Chris Masonbe744172007-05-06 10:15:01 -04001041 block_group = btrfs_find_block_group(root, block_group,
Chris Masonfbdc7622007-05-30 10:22:12 -04001042 hint_block, data, 1);
Chris Masonbe744172007-05-06 10:15:01 -04001043 } else {
1044 block_group = btrfs_find_block_group(root,
1045 trans->block_group, 0,
Chris Masonde428b62007-05-18 13:28:27 -04001046 data, 1);
Chris Masonbe744172007-05-06 10:15:01 -04001047 }
1048
Chris Masone0115992007-06-19 16:23:05 -04001049 path = btrfs_alloc_path();
1050
Chris Masonbe744172007-05-06 10:15:01 -04001051check_failed:
Chris Mason1e2677e2007-05-29 16:52:18 -04001052 if (!block_group->data)
Chris Masone37c9e62007-05-09 20:13:14 -04001053 search_start = find_search_start(root, &block_group,
1054 search_start, total_needed);
Chris Masonfbdc7622007-05-30 10:22:12 -04001055 else if (!full_scan)
Chris Masone37c9e62007-05-09 20:13:14 -04001056 search_start = max(block_group->last_alloc, search_start);
1057
Chris Mason5caf2a02007-04-02 11:20:42 -04001058 btrfs_init_path(path);
Chris Masonfec577f2007-02-26 10:40:21 -05001059 ins->objectid = search_start;
1060 ins->offset = 0;
Chris Masonfec577f2007-02-26 10:40:21 -05001061 start_found = 0;
Chris Masone37c9e62007-05-09 20:13:14 -04001062
Chris Mason5caf2a02007-04-02 11:20:42 -04001063 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
Chris Mason0f70abe2007-02-28 16:46:22 -05001064 if (ret < 0)
1065 goto error;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001066
Chris Masone37c9e62007-05-09 20:13:14 -04001067 if (path->slots[0] > 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001068 path->slots[0]--;
Chris Masone37c9e62007-05-09 20:13:14 -04001069 }
1070
1071 l = btrfs_buffer_leaf(path->nodes[0]);
1072 btrfs_disk_key_to_cpu(&key, &l->items[path->slots[0]].key);
1073 /*
1074 * a rare case, go back one key if we hit a block group item
1075 * instead of an extent item
1076 */
1077 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY &&
1078 key.objectid + key.offset >= search_start) {
1079 ins->objectid = key.objectid;
1080 ins->offset = key.offset - 1;
1081 btrfs_release_path(root, path);
1082 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
1083 if (ret < 0)
1084 goto error;
1085
1086 if (path->slots[0] > 0) {
1087 path->slots[0]--;
1088 }
1089 }
Chris Mason0579da42007-03-07 16:15:30 -05001090
Chris Masonfec577f2007-02-26 10:40:21 -05001091 while (1) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001092 l = btrfs_buffer_leaf(path->nodes[0]);
1093 slot = path->slots[0];
Chris Mason7518a232007-03-12 12:01:18 -04001094 if (slot >= btrfs_header_nritems(&l->header)) {
Chris Masonf2458e12007-04-25 15:52:25 -04001095 if (fill_prealloc) {
1096 info->extent_tree_prealloc_nr = 0;
1097 total_found = 0;
1098 }
Chris Masonde428b62007-05-18 13:28:27 -04001099 if (start_found)
1100 limit = last_block +
Chris Mason84f54cf2007-06-12 07:43:08 -04001101 (block_group->key.offset >> 1);
Chris Masonde428b62007-05-18 13:28:27 -04001102 else
1103 limit = search_start +
Chris Mason84f54cf2007-06-12 07:43:08 -04001104 (block_group->key.offset >> 1);
Chris Mason5caf2a02007-04-02 11:20:42 -04001105 ret = btrfs_next_leaf(root, path);
Chris Masonfec577f2007-02-26 10:40:21 -05001106 if (ret == 0)
1107 continue;
Chris Mason0f70abe2007-02-28 16:46:22 -05001108 if (ret < 0)
1109 goto error;
Chris Masonfec577f2007-02-26 10:40:21 -05001110 if (!start_found) {
1111 ins->objectid = search_start;
Chris Mason3e1ad542007-05-07 20:03:49 -04001112 ins->offset = search_end - search_start;
Chris Masonfec577f2007-02-26 10:40:21 -05001113 start_found = 1;
1114 goto check_pending;
1115 }
1116 ins->objectid = last_block > search_start ?
1117 last_block : search_start;
Chris Mason3e1ad542007-05-07 20:03:49 -04001118 ins->offset = search_end - ins->objectid;
Chris Masonfec577f2007-02-26 10:40:21 -05001119 goto check_pending;
1120 }
Chris Masone37c9e62007-05-09 20:13:14 -04001121
Chris Masone2fa7222007-03-12 16:22:34 -04001122 btrfs_disk_key_to_cpu(&key, &l->items[slot].key);
Chris Masone37c9e62007-05-09 20:13:14 -04001123 if (key.objectid >= search_start && key.objectid > last_block &&
1124 start_found) {
1125 if (last_block < search_start)
1126 last_block = search_start;
1127 hole_size = key.objectid - last_block;
1128 if (hole_size >= num_blocks) {
1129 ins->objectid = last_block;
1130 ins->offset = hole_size;
1131 goto check_pending;
Chris Mason0579da42007-03-07 16:15:30 -05001132 }
Chris Masonfec577f2007-02-26 10:40:21 -05001133 }
Chris Masone37c9e62007-05-09 20:13:14 -04001134
1135 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY)
1136 goto next;
1137
Chris Mason0579da42007-03-07 16:15:30 -05001138 start_found = 1;
Chris Masone2fa7222007-03-12 16:22:34 -04001139 last_block = key.objectid + key.offset;
Chris Masonfbdc7622007-05-30 10:22:12 -04001140 if (!full_scan && last_block >= block_group->key.objectid +
Chris Masonbe744172007-05-06 10:15:01 -04001141 block_group->key.offset) {
1142 btrfs_release_path(root, path);
1143 search_start = block_group->key.objectid +
1144 block_group->key.offset * 2;
1145 goto new_group;
1146 }
Chris Mason9078a3e2007-04-26 16:46:15 -04001147next:
Chris Mason5caf2a02007-04-02 11:20:42 -04001148 path->slots[0]++;
Chris Masonde428b62007-05-18 13:28:27 -04001149 cond_resched();
Chris Masonfec577f2007-02-26 10:40:21 -05001150 }
Chris Masonfec577f2007-02-26 10:40:21 -05001151check_pending:
1152 /* we have to make sure we didn't find an extent that has already
1153 * been allocated by the map tree or the original allocation
1154 */
Chris Mason5caf2a02007-04-02 11:20:42 -04001155 btrfs_release_path(root, path);
Chris Masonfec577f2007-02-26 10:40:21 -05001156 BUG_ON(ins->objectid < search_start);
Chris Masone37c9e62007-05-09 20:13:14 -04001157
Chris Mason3e1ad542007-05-07 20:03:49 -04001158 if (ins->objectid + num_blocks >= search_end) {
Chris Masonfbdc7622007-05-30 10:22:12 -04001159 if (full_scan) {
1160 ret = -ENOSPC;
1161 goto error;
1162 }
Chris Masonbe744172007-05-06 10:15:01 -04001163 search_start = orig_search_start;
Chris Masonfbdc7622007-05-30 10:22:12 -04001164 if (wrapped)
1165 full_scan = 1;
1166 else
1167 wrapped = 1;
Chris Masonbe744172007-05-06 10:15:01 -04001168 goto new_group;
Chris Mason06a2f9f2007-04-28 08:48:10 -04001169 }
Chris Mason037e6392007-03-07 11:50:24 -05001170 for (test_block = ins->objectid;
Chris Masonf2458e12007-04-25 15:52:25 -04001171 test_block < ins->objectid + num_blocks; test_block++) {
1172 if (test_radix_bit(&info->pinned_radix, test_block)) {
Chris Mason037e6392007-03-07 11:50:24 -05001173 search_start = test_block + 1;
Chris Masonbe744172007-05-06 10:15:01 -04001174 goto new_group;
Chris Masonfec577f2007-02-26 10:40:21 -05001175 }
1176 }
Chris Masonf2458e12007-04-25 15:52:25 -04001177 if (!fill_prealloc && info->extent_tree_insert_nr) {
1178 u64 last =
1179 info->extent_tree_insert[info->extent_tree_insert_nr - 1];
1180 if (ins->objectid + num_blocks >
1181 info->extent_tree_insert[0] &&
1182 ins->objectid <= last) {
1183 search_start = last + 1;
Chris Masone37c9e62007-05-09 20:13:14 -04001184 WARN_ON(!full_scan);
Chris Masonbe744172007-05-06 10:15:01 -04001185 goto new_group;
Chris Masonf2458e12007-04-25 15:52:25 -04001186 }
1187 }
1188 if (!fill_prealloc && info->extent_tree_prealloc_nr) {
1189 u64 first =
1190 info->extent_tree_prealloc[info->extent_tree_prealloc_nr - 1];
1191 if (ins->objectid + num_blocks > first &&
1192 ins->objectid <= info->extent_tree_prealloc[0]) {
1193 search_start = info->extent_tree_prealloc[0] + 1;
Chris Masonbe744172007-05-06 10:15:01 -04001194 goto new_group;
Chris Masonf2458e12007-04-25 15:52:25 -04001195 }
1196 }
Chris Masonf2654de2007-06-26 12:20:46 -04001197 if (exclude_nr > 0 && (ins->objectid + num_blocks > exclude_start &&
1198 ins->objectid < exclude_start + exclude_nr)) {
1199 search_start = exclude_start + exclude_nr;
1200 goto new_group;
1201 }
Chris Masonf2458e12007-04-25 15:52:25 -04001202 if (fill_prealloc) {
1203 int nr;
1204 test_block = ins->objectid;
Chris Masone37c9e62007-05-09 20:13:14 -04001205 if (test_block - info->extent_tree_prealloc[total_needed - 1] >=
1206 leaf_range(root)) {
1207 total_found = 0;
1208 info->extent_tree_prealloc_nr = total_found;
1209 }
Chris Masonf2458e12007-04-25 15:52:25 -04001210 while(test_block < ins->objectid + ins->offset &&
1211 total_found < total_needed) {
1212 nr = total_needed - total_found - 1;
1213 BUG_ON(nr < 0);
Chris Masoncd1bc462007-04-27 10:08:34 -04001214 info->extent_tree_prealloc[nr] = test_block;
Chris Masonf2458e12007-04-25 15:52:25 -04001215 total_found++;
1216 test_block++;
1217 }
1218 if (total_found < total_needed) {
1219 search_start = test_block;
Chris Masonbe744172007-05-06 10:15:01 -04001220 goto new_group;
Chris Masonf2458e12007-04-25 15:52:25 -04001221 }
Chris Masoncd1bc462007-04-27 10:08:34 -04001222 info->extent_tree_prealloc_nr = total_found;
Chris Masonf2458e12007-04-25 15:52:25 -04001223 }
Chris Masone37c9e62007-05-09 20:13:14 -04001224 if (!data) {
Chris Mason5276aed2007-06-11 21:33:38 -04001225 block_group = btrfs_lookup_block_group(info, ins->objectid);
Chris Masone37c9e62007-05-09 20:13:14 -04001226 if (block_group) {
1227 if (fill_prealloc)
1228 block_group->last_prealloc =
1229 info->extent_tree_prealloc[total_needed-1];
1230 else
1231 trans->block_group = block_group;
1232 }
Chris Masoncd1bc462007-04-27 10:08:34 -04001233 }
Chris Mason037e6392007-03-07 11:50:24 -05001234 ins->offset = num_blocks;
Chris Mason5caf2a02007-04-02 11:20:42 -04001235 btrfs_free_path(path);
Chris Masonfec577f2007-02-26 10:40:21 -05001236 return 0;
Chris Masonbe744172007-05-06 10:15:01 -04001237
1238new_group:
Chris Mason3e1ad542007-05-07 20:03:49 -04001239 if (search_start + num_blocks >= search_end) {
Chris Masonbe744172007-05-06 10:15:01 -04001240 search_start = orig_search_start;
Chris Masonfbdc7622007-05-30 10:22:12 -04001241 if (full_scan) {
1242 ret = -ENOSPC;
1243 goto error;
1244 }
1245 if (wrapped)
1246 full_scan = 1;
1247 else
1248 wrapped = 1;
Chris Masonbe744172007-05-06 10:15:01 -04001249 }
Chris Mason5276aed2007-06-11 21:33:38 -04001250 block_group = btrfs_lookup_block_group(info, search_start);
Chris Masonfbdc7622007-05-30 10:22:12 -04001251 cond_resched();
Chris Masonbe744172007-05-06 10:15:01 -04001252 if (!full_scan)
1253 block_group = btrfs_find_block_group(root, block_group,
Chris Masonde428b62007-05-18 13:28:27 -04001254 search_start, data, 0);
Chris Masonbe744172007-05-06 10:15:01 -04001255 goto check_failed;
1256
Chris Mason0f70abe2007-02-28 16:46:22 -05001257error:
Chris Mason5caf2a02007-04-02 11:20:42 -04001258 btrfs_release_path(root, path);
1259 btrfs_free_path(path);
Chris Mason0f70abe2007-02-28 16:46:22 -05001260 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05001261}
Chris Masonfec577f2007-02-26 10:40:21 -05001262/*
Chris Masonfec577f2007-02-26 10:40:21 -05001263 * finds a free extent and does all the dirty work required for allocation
1264 * returns the key for the extent through ins, and a tree buffer for
1265 * the first block of the extent through buf.
1266 *
1267 * returns 0 if everything worked, non-zero otherwise.
1268 */
Chris Mason4d775672007-04-20 20:23:12 -04001269int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
1270 struct btrfs_root *root, u64 owner,
Chris Masonfbdc7622007-05-30 10:22:12 -04001271 u64 num_blocks, u64 hint_block,
Chris Masonbe08c1b2007-05-03 09:06:49 -04001272 u64 search_end, struct btrfs_key *ins, int data)
Chris Masonfec577f2007-02-26 10:40:21 -05001273{
1274 int ret;
1275 int pending_ret;
Chris Mason1261ec42007-03-20 20:35:03 -04001276 u64 super_blocks_used;
Chris Masonfbdc7622007-05-30 10:22:12 -04001277 u64 search_start = 0;
Chris Masonf2654de2007-06-26 12:20:46 -04001278 u64 exclude_start = 0;
1279 u64 exclude_nr = 0;
Chris Mason1261ec42007-03-20 20:35:03 -04001280 struct btrfs_fs_info *info = root->fs_info;
1281 struct btrfs_root *extent_root = info->extent_root;
Chris Mason234b63a2007-03-13 10:46:10 -04001282 struct btrfs_extent_item extent_item;
Chris Masonf2458e12007-04-25 15:52:25 -04001283 struct btrfs_key prealloc_key;
Chris Mason037e6392007-03-07 11:50:24 -05001284
Chris Masoncf27e1e2007-03-13 09:49:06 -04001285 btrfs_set_extent_refs(&extent_item, 1);
Chris Mason4d775672007-04-20 20:23:12 -04001286 btrfs_set_extent_owner(&extent_item, owner);
Chris Masonfec577f2007-02-26 10:40:21 -05001287
Chris Mason037e6392007-03-07 11:50:24 -05001288 if (root == extent_root) {
Chris Masonf2458e12007-04-25 15:52:25 -04001289 int nr;
1290 BUG_ON(info->extent_tree_prealloc_nr == 0);
Chris Mason037e6392007-03-07 11:50:24 -05001291 BUG_ON(num_blocks != 1);
Chris Mason037e6392007-03-07 11:50:24 -05001292 ins->offset = 1;
Chris Masonf2458e12007-04-25 15:52:25 -04001293 info->extent_tree_prealloc_nr--;
1294 nr = info->extent_tree_prealloc_nr;
1295 ins->objectid = info->extent_tree_prealloc[nr];
1296 info->extent_tree_insert[info->extent_tree_insert_nr++] =
1297 ins->objectid;
Chris Mason9078a3e2007-04-26 16:46:15 -04001298 ret = update_block_group(trans, root,
Chris Mason1e2677e2007-05-29 16:52:18 -04001299 ins->objectid, ins->offset, 1, 0, 0);
Chris Mason9078a3e2007-04-26 16:46:15 -04001300 BUG_ON(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05001301 return 0;
1302 }
Chris Masone37c9e62007-05-09 20:13:14 -04001303
1304 /*
1305 * if we're doing a data allocation, preallocate room in the
1306 * extent tree first. This way the extent tree blocks end up
1307 * in the correct block group.
1308 */
1309 if (data) {
Chris Masonde428b62007-05-18 13:28:27 -04001310 ret = find_free_extent(trans, root, 0, 0,
Chris Masonf2654de2007-06-26 12:20:46 -04001311 search_end, 0, &prealloc_key, 0, 0, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001312 if (ret)
1313 return ret;
Chris Masonf2654de2007-06-26 12:20:46 -04001314 exclude_nr = info->extent_tree_prealloc_nr;
1315 exclude_start = info->extent_tree_prealloc[exclude_nr - 1];
Chris Masone37c9e62007-05-09 20:13:14 -04001316 }
Chris Masonfec577f2007-02-26 10:40:21 -05001317
Chris Masonf2654de2007-06-26 12:20:46 -04001318 /* do the real allocation */
1319 ret = find_free_extent(trans, root, num_blocks, search_start,
1320 search_end, hint_block, ins,
1321 exclude_start, exclude_nr, data);
1322 if (ret)
1323 return ret;
1324
Chris Masone37c9e62007-05-09 20:13:14 -04001325 /*
1326 * if we're doing a metadata allocation, preallocate space in the
1327 * extent tree second. This way, we don't create a tiny hole
1328 * in the allocation map between any unused preallocation blocks
1329 * and the metadata block we're actually allocating. On disk,
1330 * it'll go:
1331 * [block we've allocated], [used prealloc 1], [ unused prealloc ]
1332 * The unused prealloc will get reused the next time around.
1333 */
1334 if (!data) {
Chris Masonf2654de2007-06-26 12:20:46 -04001335 exclude_start = ins->objectid;
1336 exclude_nr = ins->offset;
Chris Masone37c9e62007-05-09 20:13:14 -04001337 ret = find_free_extent(trans, root, 0, search_start,
Chris Masonfbdc7622007-05-30 10:22:12 -04001338 search_end, hint_block,
Chris Masonf2654de2007-06-26 12:20:46 -04001339 &prealloc_key, exclude_start,
1340 exclude_nr, 0);
1341 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 Masone089f052007-03-16 16:20:31 -04001351 finish_current_insert(trans, extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -04001352 pending_ret = del_pending_extents(trans, extent_root);
Chris Masone37c9e62007-05-09 20:13:14 -04001353 if (ret) {
Chris Mason037e6392007-03-07 11:50:24 -05001354 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -04001355 }
1356 if (pending_ret) {
Chris Mason037e6392007-03-07 11:50:24 -05001357 return pending_ret;
Chris Masone37c9e62007-05-09 20:13:14 -04001358 }
Chris Mason1e2677e2007-05-29 16:52:18 -04001359 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0,
1360 data);
Chris Masonfabb5682007-06-07 22:13:21 -04001361 BUG_ON(ret);
Chris Mason037e6392007-03-07 11:50:24 -05001362 return 0;
Chris Masonfec577f2007-02-26 10:40:21 -05001363}
1364
1365/*
1366 * helper function to allocate a block for a given tree
1367 * returns the tree buffer or NULL.
1368 */
Chris Masone20d96d2007-03-22 12:13:20 -04001369struct buffer_head *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Chris Mason31f3c992007-04-30 15:25:45 -04001370 struct btrfs_root *root, u64 hint)
Chris Masonfec577f2007-02-26 10:40:21 -05001371{
Chris Masone2fa7222007-03-12 16:22:34 -04001372 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -05001373 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -04001374 struct buffer_head *buf;
Chris Masonfec577f2007-02-26 10:40:21 -05001375
Chris Mason4d775672007-04-20 20:23:12 -04001376 ret = btrfs_alloc_extent(trans, root, root->root_key.objectid,
Chris Masonde428b62007-05-18 13:28:27 -04001377 1, hint, (unsigned long)-1, &ins, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05001378 if (ret) {
Chris Mason54aa1f42007-06-22 14:16:25 -04001379 BUG_ON(ret > 0);
1380 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05001381 }
Chris Masond98237b2007-03-28 13:57:48 -04001382 buf = btrfs_find_create_tree_block(root, ins.objectid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001383 if (!buf) {
1384 btrfs_free_extent(trans, root, ins.objectid, 1, 0);
1385 return ERR_PTR(-ENOMEM);
1386 }
Chris Masondf2ce342007-03-23 11:00:45 -04001387 set_buffer_uptodate(buf);
Chris Mason090d1872007-05-01 08:53:32 -04001388 set_buffer_checked(buf);
Chris Mason7c4452b2007-04-28 09:29:35 -04001389 set_radix_bit(&trans->transaction->dirty_pages, buf->b_page->index);
Chris Masonfec577f2007-02-26 10:40:21 -05001390 return buf;
1391}
Chris Masona28ec192007-03-06 20:08:01 -05001392
Chris Mason6407bf62007-03-27 06:33:00 -04001393static int drop_leaf_ref(struct btrfs_trans_handle *trans,
1394 struct btrfs_root *root, struct buffer_head *cur)
1395{
1396 struct btrfs_disk_key *key;
1397 struct btrfs_leaf *leaf;
1398 struct btrfs_file_extent_item *fi;
1399 int i;
1400 int nritems;
1401 int ret;
1402
1403 BUG_ON(!btrfs_is_leaf(btrfs_buffer_node(cur)));
1404 leaf = btrfs_buffer_leaf(cur);
1405 nritems = btrfs_header_nritems(&leaf->header);
1406 for (i = 0; i < nritems; i++) {
Chris Mason3a686372007-05-24 13:35:57 -04001407 u64 disk_blocknr;
Chris Mason6407bf62007-03-27 06:33:00 -04001408 key = &leaf->items[i].key;
1409 if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY)
1410 continue;
1411 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
Chris Mason236454d2007-04-19 13:37:44 -04001412 if (btrfs_file_extent_type(fi) == BTRFS_FILE_EXTENT_INLINE)
1413 continue;
Chris Mason6407bf62007-03-27 06:33:00 -04001414 /*
1415 * FIXME make sure to insert a trans record that
1416 * repeats the snapshot del on crash
1417 */
Chris Mason3a686372007-05-24 13:35:57 -04001418 disk_blocknr = btrfs_file_extent_disk_blocknr(fi);
1419 if (disk_blocknr == 0)
1420 continue;
1421 ret = btrfs_free_extent(trans, root, disk_blocknr,
Chris Mason6407bf62007-03-27 06:33:00 -04001422 btrfs_file_extent_disk_num_blocks(fi),
1423 0);
1424 BUG_ON(ret);
1425 }
1426 return 0;
1427}
1428
Chris Masone0115992007-06-19 16:23:05 -04001429static void reada_walk_down(struct btrfs_root *root,
1430 struct btrfs_node *node)
1431{
1432 int i;
1433 u32 nritems;
1434 u64 blocknr;
1435 int ret;
1436 u32 refs;
1437
1438 nritems = btrfs_header_nritems(&node->header);
1439 for (i = 0; i < nritems; i++) {
1440 blocknr = btrfs_node_blockptr(node, i);
1441 ret = lookup_extent_ref(NULL, root, blocknr, 1, &refs);
1442 BUG_ON(ret);
1443 if (refs != 1)
1444 continue;
1445 ret = readahead_tree_block(root, blocknr);
1446 if (ret)
1447 break;
1448 }
1449}
1450
Chris Mason9aca1d52007-03-13 11:09:37 -04001451/*
1452 * helper function for drop_snapshot, this walks down the tree dropping ref
1453 * counts as it goes.
1454 */
Chris Masone089f052007-03-16 16:20:31 -04001455static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
1456 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05001457{
Chris Masone20d96d2007-03-22 12:13:20 -04001458 struct buffer_head *next;
1459 struct buffer_head *cur;
Chris Mason20524f02007-03-10 06:35:47 -05001460 u64 blocknr;
1461 int ret;
1462 u32 refs;
1463
Chris Mason5caf2a02007-04-02 11:20:42 -04001464 WARN_ON(*level < 0);
1465 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Masonb18c6682007-04-17 13:26:50 -04001466 ret = lookup_extent_ref(trans, root, bh_blocknr(path->nodes[*level]),
Chris Mason6407bf62007-03-27 06:33:00 -04001467 1, &refs);
Chris Mason20524f02007-03-10 06:35:47 -05001468 BUG_ON(ret);
1469 if (refs > 1)
1470 goto out;
Chris Masone0115992007-06-19 16:23:05 -04001471
Chris Mason9aca1d52007-03-13 11:09:37 -04001472 /*
1473 * walk down to the last node level and free all the leaves
1474 */
Chris Mason6407bf62007-03-27 06:33:00 -04001475 while(*level >= 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001476 WARN_ON(*level < 0);
1477 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason20524f02007-03-10 06:35:47 -05001478 cur = path->nodes[*level];
Chris Masone0115992007-06-19 16:23:05 -04001479
1480 if (*level > 0 && path->slots[*level] == 0)
1481 reada_walk_down(root, btrfs_buffer_node(cur));
1482
Chris Mason2c90e5d2007-04-02 10:50:19 -04001483 if (btrfs_header_level(btrfs_buffer_header(cur)) != *level)
1484 WARN_ON(1);
Chris Masone0115992007-06-19 16:23:05 -04001485
Chris Mason7518a232007-03-12 12:01:18 -04001486 if (path->slots[*level] >=
Chris Masone20d96d2007-03-22 12:13:20 -04001487 btrfs_header_nritems(btrfs_buffer_header(cur)))
Chris Mason20524f02007-03-10 06:35:47 -05001488 break;
Chris Mason6407bf62007-03-27 06:33:00 -04001489 if (*level == 0) {
1490 ret = drop_leaf_ref(trans, root, cur);
1491 BUG_ON(ret);
1492 break;
1493 }
Chris Masone20d96d2007-03-22 12:13:20 -04001494 blocknr = btrfs_node_blockptr(btrfs_buffer_node(cur),
1495 path->slots[*level]);
Chris Masonb18c6682007-04-17 13:26:50 -04001496 ret = lookup_extent_ref(trans, root, blocknr, 1, &refs);
Chris Mason6407bf62007-03-27 06:33:00 -04001497 BUG_ON(ret);
1498 if (refs != 1) {
Chris Mason20524f02007-03-10 06:35:47 -05001499 path->slots[*level]++;
Chris Masone089f052007-03-16 16:20:31 -04001500 ret = btrfs_free_extent(trans, root, blocknr, 1, 1);
Chris Mason20524f02007-03-10 06:35:47 -05001501 BUG_ON(ret);
1502 continue;
1503 }
Chris Mason20524f02007-03-10 06:35:47 -05001504 next = read_tree_block(root, blocknr);
Chris Mason5caf2a02007-04-02 11:20:42 -04001505 WARN_ON(*level <= 0);
Chris Mason83e15a22007-03-12 09:03:27 -04001506 if (path->nodes[*level-1])
Chris Mason234b63a2007-03-13 10:46:10 -04001507 btrfs_block_release(root, path->nodes[*level-1]);
Chris Mason20524f02007-03-10 06:35:47 -05001508 path->nodes[*level-1] = next;
Chris Masone20d96d2007-03-22 12:13:20 -04001509 *level = btrfs_header_level(btrfs_buffer_header(next));
Chris Mason20524f02007-03-10 06:35:47 -05001510 path->slots[*level] = 0;
1511 }
1512out:
Chris Mason5caf2a02007-04-02 11:20:42 -04001513 WARN_ON(*level < 0);
1514 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason6407bf62007-03-27 06:33:00 -04001515 ret = btrfs_free_extent(trans, root,
Chris Mason7eccb902007-04-11 15:53:25 -04001516 bh_blocknr(path->nodes[*level]), 1, 1);
Chris Mason234b63a2007-03-13 10:46:10 -04001517 btrfs_block_release(root, path->nodes[*level]);
Chris Mason20524f02007-03-10 06:35:47 -05001518 path->nodes[*level] = NULL;
1519 *level += 1;
1520 BUG_ON(ret);
1521 return 0;
1522}
1523
Chris Mason9aca1d52007-03-13 11:09:37 -04001524/*
1525 * helper for dropping snapshots. This walks back up the tree in the path
1526 * to find the first node higher up where we haven't yet gone through
1527 * all the slots
1528 */
Chris Masone089f052007-03-16 16:20:31 -04001529static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root
1530 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05001531{
1532 int i;
1533 int slot;
1534 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -04001535 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Mason20524f02007-03-10 06:35:47 -05001536 slot = path->slots[i];
Chris Masone20d96d2007-03-22 12:13:20 -04001537 if (slot < btrfs_header_nritems(
1538 btrfs_buffer_header(path->nodes[i])) - 1) {
Chris Mason20524f02007-03-10 06:35:47 -05001539 path->slots[i]++;
1540 *level = i;
1541 return 0;
1542 } else {
Chris Masone089f052007-03-16 16:20:31 -04001543 ret = btrfs_free_extent(trans, root,
Chris Mason7eccb902007-04-11 15:53:25 -04001544 bh_blocknr(path->nodes[*level]),
Chris Masone089f052007-03-16 16:20:31 -04001545 1, 1);
Chris Mason6407bf62007-03-27 06:33:00 -04001546 BUG_ON(ret);
Chris Mason234b63a2007-03-13 10:46:10 -04001547 btrfs_block_release(root, path->nodes[*level]);
Chris Mason83e15a22007-03-12 09:03:27 -04001548 path->nodes[*level] = NULL;
Chris Mason20524f02007-03-10 06:35:47 -05001549 *level = i + 1;
Chris Mason20524f02007-03-10 06:35:47 -05001550 }
1551 }
1552 return 1;
1553}
1554
Chris Mason9aca1d52007-03-13 11:09:37 -04001555/*
1556 * drop the reference count on the tree rooted at 'snap'. This traverses
1557 * the tree freeing any blocks that have a ref count of zero after being
1558 * decremented.
1559 */
Chris Masone089f052007-03-16 16:20:31 -04001560int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masone20d96d2007-03-22 12:13:20 -04001561 *root, struct buffer_head *snap)
Chris Mason20524f02007-03-10 06:35:47 -05001562{
Chris Mason3768f362007-03-13 16:47:54 -04001563 int ret = 0;
Chris Mason9aca1d52007-03-13 11:09:37 -04001564 int wret;
Chris Mason20524f02007-03-10 06:35:47 -05001565 int level;
Chris Mason5caf2a02007-04-02 11:20:42 -04001566 struct btrfs_path *path;
Chris Mason20524f02007-03-10 06:35:47 -05001567 int i;
1568 int orig_level;
1569
Chris Mason5caf2a02007-04-02 11:20:42 -04001570 path = btrfs_alloc_path();
1571 BUG_ON(!path);
Chris Mason20524f02007-03-10 06:35:47 -05001572
Chris Masone20d96d2007-03-22 12:13:20 -04001573 level = btrfs_header_level(btrfs_buffer_header(snap));
Chris Mason20524f02007-03-10 06:35:47 -05001574 orig_level = level;
Chris Mason5caf2a02007-04-02 11:20:42 -04001575 path->nodes[level] = snap;
1576 path->slots[level] = 0;
Chris Mason20524f02007-03-10 06:35:47 -05001577 while(1) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001578 wret = walk_down_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04001579 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05001580 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04001581 if (wret < 0)
1582 ret = wret;
1583
Chris Mason5caf2a02007-04-02 11:20:42 -04001584 wret = walk_up_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04001585 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05001586 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04001587 if (wret < 0)
1588 ret = wret;
Chris Mason20524f02007-03-10 06:35:47 -05001589 }
Chris Mason83e15a22007-03-12 09:03:27 -04001590 for (i = 0; i <= orig_level; i++) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001591 if (path->nodes[i]) {
1592 btrfs_block_release(root, path->nodes[i]);
Chris Mason83e15a22007-03-12 09:03:27 -04001593 }
Chris Mason20524f02007-03-10 06:35:47 -05001594 }
Chris Mason5caf2a02007-04-02 11:20:42 -04001595 btrfs_free_path(path);
Chris Mason9aca1d52007-03-13 11:09:37 -04001596 return ret;
Chris Mason20524f02007-03-10 06:35:47 -05001597}
Chris Mason9078a3e2007-04-26 16:46:15 -04001598
Chris Masonbe744172007-05-06 10:15:01 -04001599static int free_block_group_radix(struct radix_tree_root *radix)
Chris Mason9078a3e2007-04-26 16:46:15 -04001600{
1601 int ret;
1602 struct btrfs_block_group_cache *cache[8];
1603 int i;
1604
1605 while(1) {
Chris Masonbe744172007-05-06 10:15:01 -04001606 ret = radix_tree_gang_lookup(radix, (void **)cache, 0,
Chris Mason9078a3e2007-04-26 16:46:15 -04001607 ARRAY_SIZE(cache));
1608 if (!ret)
1609 break;
1610 for (i = 0; i < ret; i++) {
Chris Masonbe744172007-05-06 10:15:01 -04001611 radix_tree_delete(radix, cache[i]->key.objectid +
Chris Mason9078a3e2007-04-26 16:46:15 -04001612 cache[i]->key.offset - 1);
1613 kfree(cache[i]);
1614 }
1615 }
1616 return 0;
1617}
1618
Chris Masonbe744172007-05-06 10:15:01 -04001619int btrfs_free_block_groups(struct btrfs_fs_info *info)
1620{
1621 int ret;
1622 int ret2;
Chris Masone37c9e62007-05-09 20:13:14 -04001623 unsigned long gang[16];
1624 int i;
Chris Masonbe744172007-05-06 10:15:01 -04001625
1626 ret = free_block_group_radix(&info->block_group_radix);
1627 ret2 = free_block_group_radix(&info->block_group_data_radix);
1628 if (ret)
1629 return ret;
1630 if (ret2)
1631 return ret2;
Chris Masone37c9e62007-05-09 20:13:14 -04001632
1633 while(1) {
1634 ret = find_first_radix_bit(&info->extent_map_radix,
1635 gang, 0, ARRAY_SIZE(gang));
1636 if (!ret)
1637 break;
1638 for (i = 0; i < ret; i++) {
1639 clear_radix_bit(&info->extent_map_radix, gang[i]);
1640 }
1641 }
Chris Masonbe744172007-05-06 10:15:01 -04001642 return 0;
1643}
1644
Chris Mason9078a3e2007-04-26 16:46:15 -04001645int btrfs_read_block_groups(struct btrfs_root *root)
1646{
1647 struct btrfs_path *path;
1648 int ret;
1649 int err = 0;
1650 struct btrfs_block_group_item *bi;
1651 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04001652 struct btrfs_fs_info *info = root->fs_info;
1653 struct radix_tree_root *radix;
Chris Mason9078a3e2007-04-26 16:46:15 -04001654 struct btrfs_key key;
1655 struct btrfs_key found_key;
1656 struct btrfs_leaf *leaf;
Chris Mason84f54cf2007-06-12 07:43:08 -04001657 u64 group_size_blocks;
Chris Mason31f3c992007-04-30 15:25:45 -04001658 u64 used;
Chris Mason9078a3e2007-04-26 16:46:15 -04001659
Chris Mason84f54cf2007-06-12 07:43:08 -04001660 group_size_blocks = BTRFS_BLOCK_GROUP_SIZE >>
1661 root->fs_info->sb->s_blocksize_bits;
Chris Masonbe744172007-05-06 10:15:01 -04001662 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04001663 key.objectid = 0;
1664 key.offset = group_size_blocks;
1665 key.flags = 0;
1666 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
1667
1668 path = btrfs_alloc_path();
1669 if (!path)
1670 return -ENOMEM;
1671
1672 while(1) {
Chris Masonbe744172007-05-06 10:15:01 -04001673 ret = btrfs_search_slot(NULL, info->extent_root,
Chris Mason9078a3e2007-04-26 16:46:15 -04001674 &key, path, 0, 0);
1675 if (ret != 0) {
1676 err = ret;
1677 break;
1678 }
1679 leaf = btrfs_buffer_leaf(path->nodes[0]);
1680 btrfs_disk_key_to_cpu(&found_key,
1681 &leaf->items[path->slots[0]].key);
1682 cache = kmalloc(sizeof(*cache), GFP_NOFS);
1683 if (!cache) {
1684 err = -1;
1685 break;
1686 }
Chris Mason3e1ad542007-05-07 20:03:49 -04001687
Chris Mason9078a3e2007-04-26 16:46:15 -04001688 bi = btrfs_item_ptr(leaf, path->slots[0],
1689 struct btrfs_block_group_item);
Chris Mason1e2677e2007-05-29 16:52:18 -04001690 if (bi->flags & BTRFS_BLOCK_GROUP_DATA) {
1691 radix = &info->block_group_data_radix;
1692 cache->data = 1;
1693 } else {
1694 radix = &info->block_group_radix;
1695 cache->data = 0;
1696 }
1697
Chris Mason9078a3e2007-04-26 16:46:15 -04001698 memcpy(&cache->item, bi, sizeof(*bi));
1699 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Mason31f3c992007-04-30 15:25:45 -04001700 cache->last_alloc = cache->key.objectid;
1701 cache->first_free = cache->key.objectid;
Chris Masone37c9e62007-05-09 20:13:14 -04001702 cache->last_prealloc = cache->key.objectid;
Chris Masonbe744172007-05-06 10:15:01 -04001703 cache->pinned = 0;
Chris Masone37c9e62007-05-09 20:13:14 -04001704 cache->cached = 0;
1705
Chris Mason3e1ad542007-05-07 20:03:49 -04001706 cache->radix = radix;
1707
Chris Mason9078a3e2007-04-26 16:46:15 -04001708 key.objectid = found_key.objectid + found_key.offset;
1709 btrfs_release_path(root, path);
Chris Masonbe744172007-05-06 10:15:01 -04001710 ret = radix_tree_insert(radix, found_key.objectid +
Chris Mason9078a3e2007-04-26 16:46:15 -04001711 found_key.offset - 1,
1712 (void *)cache);
1713 BUG_ON(ret);
Chris Mason31f3c992007-04-30 15:25:45 -04001714 used = btrfs_block_group_used(bi);
Chris Mason84f54cf2007-06-12 07:43:08 -04001715 if (used < div_factor(key.offset, 8)) {
Chris Masonbe744172007-05-06 10:15:01 -04001716 radix_tree_tag_set(radix, found_key.objectid +
Chris Mason31f3c992007-04-30 15:25:45 -04001717 found_key.offset - 1,
1718 BTRFS_BLOCK_GROUP_AVAIL);
1719 }
Chris Mason9078a3e2007-04-26 16:46:15 -04001720 if (key.objectid >=
Chris Mason4b52dff2007-06-26 10:06:50 -04001721 btrfs_super_total_blocks(&info->super_copy))
Chris Mason9078a3e2007-04-26 16:46:15 -04001722 break;
1723 }
1724
1725 btrfs_free_path(path);
1726 return 0;
1727}