blob: 8025e9f8ef194b0ff089d2fe4a74c8528548f76a [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,
28 struct btrfs_key *ins, int data);
Chris Masone089f052007-03-16 16:20:31 -040029static int finish_current_insert(struct btrfs_trans_handle *trans, struct
30 btrfs_root *extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -040031static int del_pending_extents(struct btrfs_trans_handle *trans, struct
32 btrfs_root *extent_root);
Chris Masonfec577f2007-02-26 10:40:21 -050033
Chris Masonde428b62007-05-18 13:28:27 -040034static void reada_extent_leaves(struct btrfs_root *root,
35 struct btrfs_path *path, u64 limit)
36{
37 struct btrfs_node *node;
38 int i;
39 int nritems;
40 u64 item_objectid;
41 u64 blocknr;
42 int slot;
43 int ret;
44
45 if (!path->nodes[1])
46 return;
47 node = btrfs_buffer_node(path->nodes[1]);
48 slot = path->slots[1] + 1;
49 nritems = btrfs_header_nritems(&node->header);
50 for (i = slot; i < nritems && i < slot + 8; i++) {
51 item_objectid = btrfs_disk_key_objectid(&node->ptrs[i].key);
52 if (item_objectid > limit)
53 break;
54 blocknr = btrfs_node_blockptr(node, i);
55 ret = readahead_tree_block(root, blocknr);
56 if (ret)
57 break;
58 }
59}
60
Chris Masone37c9e62007-05-09 20:13:14 -040061static int cache_block_group(struct btrfs_root *root,
62 struct btrfs_block_group_cache *block_group)
63{
64 struct btrfs_path *path;
65 int ret;
66 struct btrfs_key key;
67 struct btrfs_leaf *leaf;
68 struct radix_tree_root *extent_radix;
69 int slot;
70 u64 i;
71 u64 last = 0;
72 u64 hole_size;
Chris Masonde428b62007-05-18 13:28:27 -040073 u64 limit;
Chris Masone37c9e62007-05-09 20:13:14 -040074 int found = 0;
75
76 root = root->fs_info->extent_root;
77 extent_radix = &root->fs_info->extent_map_radix;
78
79 if (block_group->cached)
80 return 0;
81 if (block_group->data)
82 return 0;
83 path = btrfs_alloc_path();
84 if (!path)
85 return -ENOMEM;
Chris Masone37c9e62007-05-09 20:13:14 -040086 key.objectid = block_group->key.objectid;
87 key.flags = 0;
88 key.offset = 0;
89 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
90 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
91 if (ret < 0)
92 return ret;
93 if (ret && path->slots[0] > 0)
94 path->slots[0]--;
Chris Masonde428b62007-05-18 13:28:27 -040095 limit = block_group->key.objectid + block_group->key.offset;
96 reada_extent_leaves(root, path, limit);
Chris Masone37c9e62007-05-09 20:13:14 -040097 while(1) {
98 leaf = btrfs_buffer_leaf(path->nodes[0]);
99 slot = path->slots[0];
100 if (slot >= btrfs_header_nritems(&leaf->header)) {
Chris Masonde428b62007-05-18 13:28:27 -0400101 reada_extent_leaves(root, path, limit);
Chris Masone37c9e62007-05-09 20:13:14 -0400102 ret = btrfs_next_leaf(root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -0400103 if (ret < 0)
104 goto err;
Chris Masonde428b62007-05-18 13:28:27 -0400105 if (ret == 0) {
Chris Masone37c9e62007-05-09 20:13:14 -0400106 continue;
Chris Masonde428b62007-05-18 13:28:27 -0400107 } else {
Chris Masone37c9e62007-05-09 20:13:14 -0400108 if (found) {
109 hole_size = block_group->key.objectid +
110 block_group->key.offset - last;
111 } else {
112 last = block_group->key.objectid;
113 hole_size = block_group->key.offset;
114 }
115 for (i = 0; i < hole_size; i++) {
116 set_radix_bit(extent_radix,
117 last + i);
118 }
119 break;
120 }
121 }
122 btrfs_disk_key_to_cpu(&key, &leaf->items[slot].key);
123 if (key.objectid >= block_group->key.objectid +
124 block_group->key.offset) {
125 if (found) {
126 hole_size = block_group->key.objectid +
127 block_group->key.offset - last;
128 } else {
129 last = block_group->key.objectid;
130 hole_size = block_group->key.offset;
131 }
132 for (i = 0; i < hole_size; i++) {
133 set_radix_bit(extent_radix, last + i);
134 }
135 break;
136 }
137 if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) {
138 if (!found) {
139 last = key.objectid + key.offset;
140 found = 1;
141 } else {
142 hole_size = key.objectid - last;
143 for (i = 0; i < hole_size; i++) {
144 set_radix_bit(extent_radix, last + i);
145 }
146 last = key.objectid + key.offset;
147 }
148 }
149 path->slots[0]++;
150 }
151
152 block_group->cached = 1;
Chris Mason54aa1f42007-06-22 14:16:25 -0400153err:
Chris Masone37c9e62007-05-09 20:13:14 -0400154 btrfs_free_path(path);
155 return 0;
156}
157
Chris Mason5276aed2007-06-11 21:33:38 -0400158struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
159 btrfs_fs_info *info,
160 u64 blocknr)
Chris Masonbe744172007-05-06 10:15:01 -0400161{
162 struct btrfs_block_group_cache *block_group;
163 int ret;
164
165 ret = radix_tree_gang_lookup(&info->block_group_radix,
166 (void **)&block_group,
167 blocknr, 1);
168 if (ret) {
Chris Mason3e1ad542007-05-07 20:03:49 -0400169 if (block_group->key.objectid <= blocknr && blocknr <=
Chris Masonbe744172007-05-06 10:15:01 -0400170 block_group->key.objectid + block_group->key.offset)
171 return block_group;
172 }
173 ret = radix_tree_gang_lookup(&info->block_group_data_radix,
174 (void **)&block_group,
175 blocknr, 1);
176 if (ret) {
Chris Mason3e1ad542007-05-07 20:03:49 -0400177 if (block_group->key.objectid <= blocknr && blocknr <=
Chris Masonbe744172007-05-06 10:15:01 -0400178 block_group->key.objectid + block_group->key.offset)
179 return block_group;
180 }
Chris Masonbe744172007-05-06 10:15:01 -0400181 return NULL;
182}
183
Chris Masone37c9e62007-05-09 20:13:14 -0400184static u64 leaf_range(struct btrfs_root *root)
185{
186 u64 size = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason84f54cf2007-06-12 07:43:08 -0400187 do_div(size, sizeof(struct btrfs_extent_item) +
188 sizeof(struct btrfs_item));
Chris Masone37c9e62007-05-09 20:13:14 -0400189 return size;
190}
191
192static u64 find_search_start(struct btrfs_root *root,
193 struct btrfs_block_group_cache **cache_ret,
194 u64 search_start, int num)
195{
196 unsigned long gang[8];
197 int ret;
198 struct btrfs_block_group_cache *cache = *cache_ret;
199 u64 last = max(search_start, cache->key.objectid);
200
201 if (cache->data)
202 goto out;
203 if (num > 1) {
204 last = max(last, cache->last_prealloc);
205 }
206again:
Chris Mason54aa1f42007-06-22 14:16:25 -0400207 ret = cache_block_group(root, cache);
208 if (ret)
209 goto out;
Chris Masone37c9e62007-05-09 20:13:14 -0400210 while(1) {
211 ret = find_first_radix_bit(&root->fs_info->extent_map_radix,
212 gang, last, ARRAY_SIZE(gang));
213 if (!ret)
214 goto out;
215 last = gang[ret-1] + 1;
216 if (num > 1) {
217 if (ret != ARRAY_SIZE(gang)) {
218 goto new_group;
219 }
220 if (gang[ret-1] - gang[0] > leaf_range(root)) {
221 continue;
222 }
223 }
224 if (gang[0] >= cache->key.objectid + cache->key.offset) {
225 goto new_group;
226 }
227 return gang[0];
228 }
229out:
230 return max(cache->last_alloc, search_start);
231
232new_group:
Chris Mason5276aed2007-06-11 21:33:38 -0400233 cache = btrfs_lookup_block_group(root->fs_info,
234 last + cache->key.offset - 1);
Chris Masone37c9e62007-05-09 20:13:14 -0400235 if (!cache) {
236 return max((*cache_ret)->last_alloc, search_start);
237 }
238 cache = btrfs_find_block_group(root, cache,
Chris Masonde428b62007-05-18 13:28:27 -0400239 last + cache->key.offset - 1, 0, 0);
Chris Masone37c9e62007-05-09 20:13:14 -0400240 *cache_ret = cache;
241 goto again;
242}
243
Chris Mason84f54cf2007-06-12 07:43:08 -0400244static u64 div_factor(u64 num, int factor)
245{
246 num *= factor;
247 do_div(num, 10);
248 return num;
249}
250
Chris Mason31f3c992007-04-30 15:25:45 -0400251struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
252 struct btrfs_block_group_cache
Chris Masonbe744172007-05-06 10:15:01 -0400253 *hint, u64 search_start,
Chris Masonde428b62007-05-18 13:28:27 -0400254 int data, int owner)
Chris Masoncd1bc462007-04-27 10:08:34 -0400255{
256 struct btrfs_block_group_cache *cache[8];
Chris Mason31f3c992007-04-30 15:25:45 -0400257 struct btrfs_block_group_cache *found_group = NULL;
Chris Masoncd1bc462007-04-27 10:08:34 -0400258 struct btrfs_fs_info *info = root->fs_info;
Chris Masonbe744172007-05-06 10:15:01 -0400259 struct radix_tree_root *radix;
Chris Mason1e2677e2007-05-29 16:52:18 -0400260 struct radix_tree_root *swap_radix;
Chris Masoncd1bc462007-04-27 10:08:34 -0400261 u64 used;
Chris Mason31f3c992007-04-30 15:25:45 -0400262 u64 last = 0;
263 u64 hint_last;
Chris Masoncd1bc462007-04-27 10:08:34 -0400264 int i;
265 int ret;
Chris Mason31f3c992007-04-30 15:25:45 -0400266 int full_search = 0;
Chris Masonde428b62007-05-18 13:28:27 -0400267 int factor = 8;
Chris Mason1e2677e2007-05-29 16:52:18 -0400268 int data_swap = 0;
Chris Masonde428b62007-05-18 13:28:27 -0400269
270 if (!owner)
271 factor = 5;
Chris Masonbe744172007-05-06 10:15:01 -0400272
Chris Mason1e2677e2007-05-29 16:52:18 -0400273 if (data) {
Chris Masonbe744172007-05-06 10:15:01 -0400274 radix = &info->block_group_data_radix;
Chris Mason1e2677e2007-05-29 16:52:18 -0400275 swap_radix = &info->block_group_radix;
276 } else {
Chris Masonbe744172007-05-06 10:15:01 -0400277 radix = &info->block_group_radix;
Chris Mason1e2677e2007-05-29 16:52:18 -0400278 swap_radix = &info->block_group_data_radix;
279 }
Chris Masonbe744172007-05-06 10:15:01 -0400280
281 if (search_start) {
282 struct btrfs_block_group_cache *shint;
Chris Mason5276aed2007-06-11 21:33:38 -0400283 shint = btrfs_lookup_block_group(info, search_start);
Chris Masonbe744172007-05-06 10:15:01 -0400284 if (shint->data == data) {
285 used = btrfs_block_group_used(&shint->item);
286 if (used + shint->pinned <
Chris Mason84f54cf2007-06-12 07:43:08 -0400287 div_factor(shint->key.offset, factor)) {
Chris Masonbe744172007-05-06 10:15:01 -0400288 return shint;
289 }
290 }
291 }
292 if (hint && hint->data == data) {
Chris Mason31f3c992007-04-30 15:25:45 -0400293 used = btrfs_block_group_used(&hint->item);
Chris Mason84f54cf2007-06-12 07:43:08 -0400294 if (used + hint->pinned <
295 div_factor(hint->key.offset, factor)) {
Chris Mason31f3c992007-04-30 15:25:45 -0400296 return hint;
297 }
Chris Mason84f54cf2007-06-12 07:43:08 -0400298 if (used >= div_factor(hint->key.offset, 8)) {
Chris Masonbe744172007-05-06 10:15:01 -0400299 radix_tree_tag_clear(radix,
300 hint->key.objectid +
301 hint->key.offset - 1,
302 BTRFS_BLOCK_GROUP_AVAIL);
303 }
Chris Mason8d7be552007-05-10 11:24:42 -0400304 last = hint->key.offset * 3;
Chris Masonbe744172007-05-06 10:15:01 -0400305 if (hint->key.objectid >= last)
Chris Masone37c9e62007-05-09 20:13:14 -0400306 last = max(search_start + hint->key.offset - 1,
307 hint->key.objectid - last);
Chris Masonbe744172007-05-06 10:15:01 -0400308 else
309 last = hint->key.objectid + hint->key.offset;
Chris Mason31f3c992007-04-30 15:25:45 -0400310 hint_last = last;
311 } else {
Chris Masone37c9e62007-05-09 20:13:14 -0400312 if (hint)
313 hint_last = max(hint->key.objectid, search_start);
314 else
315 hint_last = search_start;
316
317 last = hint_last;
Chris Mason31f3c992007-04-30 15:25:45 -0400318 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400319 while(1) {
Chris Masonbe744172007-05-06 10:15:01 -0400320 ret = radix_tree_gang_lookup_tag(radix, (void **)cache,
Chris Masoncd1bc462007-04-27 10:08:34 -0400321 last, ARRAY_SIZE(cache),
Chris Mason31f3c992007-04-30 15:25:45 -0400322 BTRFS_BLOCK_GROUP_AVAIL);
Chris Masoncd1bc462007-04-27 10:08:34 -0400323 if (!ret)
324 break;
325 for (i = 0; i < ret; i++) {
Chris Masonbe08c1b2007-05-03 09:06:49 -0400326 last = cache[i]->key.objectid +
327 cache[i]->key.offset;
Chris Masoncd1bc462007-04-27 10:08:34 -0400328 used = btrfs_block_group_used(&cache[i]->item);
Chris Masonbe744172007-05-06 10:15:01 -0400329 if (used + cache[i]->pinned <
Chris Mason84f54cf2007-06-12 07:43:08 -0400330 div_factor(cache[i]->key.offset, factor)) {
Chris Mason31f3c992007-04-30 15:25:45 -0400331 found_group = cache[i];
332 goto found;
Chris Masoncd1bc462007-04-27 10:08:34 -0400333 }
Chris Mason84f54cf2007-06-12 07:43:08 -0400334 if (used >= div_factor(cache[i]->key.offset, 8)) {
Chris Masonbe744172007-05-06 10:15:01 -0400335 radix_tree_tag_clear(radix,
336 cache[i]->key.objectid +
337 cache[i]->key.offset - 1,
338 BTRFS_BLOCK_GROUP_AVAIL);
339 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400340 }
Chris Masonde428b62007-05-18 13:28:27 -0400341 cond_resched();
Chris Masoncd1bc462007-04-27 10:08:34 -0400342 }
Chris Mason31f3c992007-04-30 15:25:45 -0400343 last = hint_last;
344again:
Chris Masoncd1bc462007-04-27 10:08:34 -0400345 while(1) {
Chris Masonbe744172007-05-06 10:15:01 -0400346 ret = radix_tree_gang_lookup(radix, (void **)cache,
347 last, ARRAY_SIZE(cache));
Chris Masoncd1bc462007-04-27 10:08:34 -0400348 if (!ret)
349 break;
350 for (i = 0; i < ret; i++) {
Chris Masonbe08c1b2007-05-03 09:06:49 -0400351 last = cache[i]->key.objectid +
352 cache[i]->key.offset;
Chris Masoncd1bc462007-04-27 10:08:34 -0400353 used = btrfs_block_group_used(&cache[i]->item);
Chris Masonbe744172007-05-06 10:15:01 -0400354 if (used + cache[i]->pinned < cache[i]->key.offset) {
Chris Mason31f3c992007-04-30 15:25:45 -0400355 found_group = cache[i];
356 goto found;
Chris Masoncd1bc462007-04-27 10:08:34 -0400357 }
Chris Masonbe744172007-05-06 10:15:01 -0400358 if (used >= cache[i]->key.offset) {
359 radix_tree_tag_clear(radix,
360 cache[i]->key.objectid +
361 cache[i]->key.offset - 1,
362 BTRFS_BLOCK_GROUP_AVAIL);
363 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400364 }
Chris Masonde428b62007-05-18 13:28:27 -0400365 cond_resched();
Chris Masoncd1bc462007-04-27 10:08:34 -0400366 }
Chris Mason31f3c992007-04-30 15:25:45 -0400367 if (!full_search) {
Chris Masonbe744172007-05-06 10:15:01 -0400368 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -0400369 full_search = 1;
370 goto again;
371 }
Chris Mason1e2677e2007-05-29 16:52:18 -0400372 if (!data_swap) {
373 struct radix_tree_root *tmp = radix;
374 data_swap = 1;
375 radix = swap_radix;
376 swap_radix = tmp;
377 last = search_start;
378 goto again;
379 }
Chris Mason31f3c992007-04-30 15:25:45 -0400380 if (!found_group) {
Chris Masonbe744172007-05-06 10:15:01 -0400381 ret = radix_tree_gang_lookup(radix,
Chris Mason31f3c992007-04-30 15:25:45 -0400382 (void **)&found_group, 0, 1);
Chris Mason1e2677e2007-05-29 16:52:18 -0400383 if (ret == 0) {
384 ret = radix_tree_gang_lookup(swap_radix,
385 (void **)&found_group,
386 0, 1);
387 }
Chris Mason31f3c992007-04-30 15:25:45 -0400388 BUG_ON(ret != 1);
389 }
Chris Masonbe744172007-05-06 10:15:01 -0400390found:
Chris Mason31f3c992007-04-30 15:25:45 -0400391 return found_group;
Chris Masoncd1bc462007-04-27 10:08:34 -0400392}
393
Chris Masonb18c6682007-04-17 13:26:50 -0400394int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
395 struct btrfs_root *root,
396 u64 blocknr, u64 num_blocks)
Chris Mason02217ed2007-03-02 16:08:05 -0500397{
Chris Mason5caf2a02007-04-02 11:20:42 -0400398 struct btrfs_path *path;
Chris Mason02217ed2007-03-02 16:08:05 -0500399 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400400 struct btrfs_key key;
Chris Mason234b63a2007-03-13 10:46:10 -0400401 struct btrfs_leaf *l;
402 struct btrfs_extent_item *item;
Chris Masone2fa7222007-03-12 16:22:34 -0400403 struct btrfs_key ins;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400404 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500405
Chris Mason5caf2a02007-04-02 11:20:42 -0400406 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -0400407 if (!path)
408 return -ENOMEM;
409 ret = find_free_extent(trans, root->fs_info->extent_root, 0, 0,
410 (u64)-1, 0, &ins, 0);
411 if (ret) {
412 btrfs_free_path(path);
413 return ret;
414 }
Chris Mason02217ed2007-03-02 16:08:05 -0500415 key.objectid = blocknr;
416 key.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -0400417 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Mason6407bf62007-03-27 06:33:00 -0400418 key.offset = num_blocks;
Chris Mason5caf2a02007-04-02 11:20:42 -0400419 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -0400420 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400421 if (ret < 0)
422 return ret;
Chris Masona429e512007-04-18 16:15:28 -0400423 if (ret != 0) {
Chris Masona28ec192007-03-06 20:08:01 -0500424 BUG();
Chris Masona429e512007-04-18 16:15:28 -0400425 }
Chris Mason02217ed2007-03-02 16:08:05 -0500426 BUG_ON(ret != 0);
Chris Mason5caf2a02007-04-02 11:20:42 -0400427 l = btrfs_buffer_leaf(path->nodes[0]);
428 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400429 refs = btrfs_extent_refs(item);
430 btrfs_set_extent_refs(item, refs + 1);
Chris Mason5caf2a02007-04-02 11:20:42 -0400431 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masona28ec192007-03-06 20:08:01 -0500432
Chris Mason5caf2a02007-04-02 11:20:42 -0400433 btrfs_release_path(root->fs_info->extent_root, path);
434 btrfs_free_path(path);
Chris Mason9f5fae22007-03-20 14:38:32 -0400435 finish_current_insert(trans, root->fs_info->extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -0400436 del_pending_extents(trans, root->fs_info->extent_root);
Chris Mason02217ed2007-03-02 16:08:05 -0500437 return 0;
438}
439
Chris Masonb18c6682007-04-17 13:26:50 -0400440static int lookup_extent_ref(struct btrfs_trans_handle *trans,
441 struct btrfs_root *root, u64 blocknr,
442 u64 num_blocks, u32 *refs)
Chris Masona28ec192007-03-06 20:08:01 -0500443{
Chris Mason5caf2a02007-04-02 11:20:42 -0400444 struct btrfs_path *path;
Chris Masona28ec192007-03-06 20:08:01 -0500445 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400446 struct btrfs_key key;
Chris Mason234b63a2007-03-13 10:46:10 -0400447 struct btrfs_leaf *l;
448 struct btrfs_extent_item *item;
Chris Mason5caf2a02007-04-02 11:20:42 -0400449
450 path = btrfs_alloc_path();
Chris Masona28ec192007-03-06 20:08:01 -0500451 key.objectid = blocknr;
Chris Mason6407bf62007-03-27 06:33:00 -0400452 key.offset = num_blocks;
Chris Mason62e27492007-03-15 12:56:47 -0400453 key.flags = 0;
454 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Mason5caf2a02007-04-02 11:20:42 -0400455 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -0400456 0, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400457 if (ret < 0)
458 goto out;
Chris Masona28ec192007-03-06 20:08:01 -0500459 if (ret != 0)
460 BUG();
Chris Mason5caf2a02007-04-02 11:20:42 -0400461 l = btrfs_buffer_leaf(path->nodes[0]);
462 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400463 *refs = btrfs_extent_refs(item);
Chris Mason54aa1f42007-06-22 14:16:25 -0400464out:
Chris Mason5caf2a02007-04-02 11:20:42 -0400465 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -0500466 return 0;
467}
468
Chris Masonc5739bb2007-04-10 09:27:04 -0400469int btrfs_inc_root_ref(struct btrfs_trans_handle *trans,
470 struct btrfs_root *root)
471{
Chris Masonb18c6682007-04-17 13:26:50 -0400472 return btrfs_inc_extent_ref(trans, root, bh_blocknr(root->node), 1);
Chris Masonc5739bb2007-04-10 09:27:04 -0400473}
474
Chris Masone089f052007-03-16 16:20:31 -0400475int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Masone20d96d2007-03-22 12:13:20 -0400476 struct buffer_head *buf)
Chris Mason02217ed2007-03-02 16:08:05 -0500477{
478 u64 blocknr;
Chris Masone20d96d2007-03-22 12:13:20 -0400479 struct btrfs_node *buf_node;
Chris Mason6407bf62007-03-27 06:33:00 -0400480 struct btrfs_leaf *buf_leaf;
481 struct btrfs_disk_key *key;
482 struct btrfs_file_extent_item *fi;
Chris Mason02217ed2007-03-02 16:08:05 -0500483 int i;
Chris Mason6407bf62007-03-27 06:33:00 -0400484 int leaf;
485 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -0400486 int faili;
487 int err;
Chris Masona28ec192007-03-06 20:08:01 -0500488
Chris Mason3768f362007-03-13 16:47:54 -0400489 if (!root->ref_cows)
Chris Masona28ec192007-03-06 20:08:01 -0500490 return 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400491 buf_node = btrfs_buffer_node(buf);
Chris Mason6407bf62007-03-27 06:33:00 -0400492 leaf = btrfs_is_leaf(buf_node);
493 buf_leaf = btrfs_buffer_leaf(buf);
Chris Masone20d96d2007-03-22 12:13:20 -0400494 for (i = 0; i < btrfs_header_nritems(&buf_node->header); i++) {
Chris Mason6407bf62007-03-27 06:33:00 -0400495 if (leaf) {
Chris Mason3a686372007-05-24 13:35:57 -0400496 u64 disk_blocknr;
Chris Mason6407bf62007-03-27 06:33:00 -0400497 key = &buf_leaf->items[i].key;
498 if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY)
499 continue;
500 fi = btrfs_item_ptr(buf_leaf, i,
501 struct btrfs_file_extent_item);
Chris Mason236454d2007-04-19 13:37:44 -0400502 if (btrfs_file_extent_type(fi) ==
503 BTRFS_FILE_EXTENT_INLINE)
504 continue;
Chris Mason3a686372007-05-24 13:35:57 -0400505 disk_blocknr = btrfs_file_extent_disk_blocknr(fi);
506 if (disk_blocknr == 0)
507 continue;
508 ret = btrfs_inc_extent_ref(trans, root, disk_blocknr,
Chris Mason6407bf62007-03-27 06:33:00 -0400509 btrfs_file_extent_disk_num_blocks(fi));
Chris Mason54aa1f42007-06-22 14:16:25 -0400510 if (ret) {
511 faili = i;
512 goto fail;
513 }
Chris Mason6407bf62007-03-27 06:33:00 -0400514 } else {
515 blocknr = btrfs_node_blockptr(buf_node, i);
Chris Masonb18c6682007-04-17 13:26:50 -0400516 ret = btrfs_inc_extent_ref(trans, root, blocknr, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400517 if (ret) {
518 faili = i;
519 goto fail;
520 }
Chris Mason6407bf62007-03-27 06:33:00 -0400521 }
Chris Mason02217ed2007-03-02 16:08:05 -0500522 }
523 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -0400524fail:
525 for (i =0; i < faili; i++) {
526 if (leaf) {
527 u64 disk_blocknr;
528 key = &buf_leaf->items[i].key;
529 if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY)
530 continue;
531 fi = btrfs_item_ptr(buf_leaf, i,
532 struct btrfs_file_extent_item);
533 if (btrfs_file_extent_type(fi) ==
534 BTRFS_FILE_EXTENT_INLINE)
535 continue;
536 disk_blocknr = btrfs_file_extent_disk_blocknr(fi);
537 if (disk_blocknr == 0)
538 continue;
539 err = btrfs_free_extent(trans, root, disk_blocknr,
540 btrfs_file_extent_disk_num_blocks(fi), 0);
541 BUG_ON(err);
542 } else {
543 blocknr = btrfs_node_blockptr(buf_node, i);
544 err = btrfs_free_extent(trans, root, blocknr, 1, 0);
545 BUG_ON(err);
546 }
547 }
548 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -0500549}
550
Chris Mason9078a3e2007-04-26 16:46:15 -0400551static int write_one_cache_group(struct btrfs_trans_handle *trans,
552 struct btrfs_root *root,
553 struct btrfs_path *path,
554 struct btrfs_block_group_cache *cache)
555{
556 int ret;
557 int pending_ret;
558 struct btrfs_root *extent_root = root->fs_info->extent_root;
559 struct btrfs_block_group_item *bi;
560 struct btrfs_key ins;
561
Chris Mason54aa1f42007-06-22 14:16:25 -0400562 ret = find_free_extent(trans, extent_root, 0, 0, (u64)-1, 0, &ins, 0);
563 /* FIXME, set bit to recalc cache groups on next mount */
564 if (ret)
565 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -0400566 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400567 if (ret < 0)
568 goto fail;
Chris Mason9078a3e2007-04-26 16:46:15 -0400569 BUG_ON(ret);
570 bi = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
571 struct btrfs_block_group_item);
572 memcpy(bi, &cache->item, sizeof(*bi));
573 mark_buffer_dirty(path->nodes[0]);
574 btrfs_release_path(extent_root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -0400575fail:
Chris Mason9078a3e2007-04-26 16:46:15 -0400576 finish_current_insert(trans, extent_root);
577 pending_ret = del_pending_extents(trans, extent_root);
578 if (ret)
579 return ret;
580 if (pending_ret)
581 return pending_ret;
Chris Masonbe744172007-05-06 10:15:01 -0400582 if (cache->data)
583 cache->last_alloc = cache->first_free;
Chris Mason9078a3e2007-04-26 16:46:15 -0400584 return 0;
585
586}
587
Chris Masonbe744172007-05-06 10:15:01 -0400588static int write_dirty_block_radix(struct btrfs_trans_handle *trans,
589 struct btrfs_root *root,
590 struct radix_tree_root *radix)
Chris Mason9078a3e2007-04-26 16:46:15 -0400591{
592 struct btrfs_block_group_cache *cache[8];
593 int ret;
594 int err = 0;
595 int werr = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -0400596 int i;
597 struct btrfs_path *path;
Chris Mason54aa1f42007-06-22 14:16:25 -0400598 unsigned long off = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -0400599
600 path = btrfs_alloc_path();
601 if (!path)
602 return -ENOMEM;
603
604 while(1) {
605 ret = radix_tree_gang_lookup_tag(radix, (void **)cache,
Chris Mason54aa1f42007-06-22 14:16:25 -0400606 off, ARRAY_SIZE(cache),
Chris Mason9078a3e2007-04-26 16:46:15 -0400607 BTRFS_BLOCK_GROUP_DIRTY);
608 if (!ret)
609 break;
610 for (i = 0; i < ret; i++) {
Chris Mason54aa1f42007-06-22 14:16:25 -0400611 err = write_one_cache_group(trans, root,
612 path, cache[i]);
613 /*
614 * if we fail to write the cache group, we want
615 * to keep it marked dirty in hopes that a later
616 * write will work
617 */
618 if (err) {
619 werr = err;
620 off = cache[i]->key.objectid +
621 cache[i]->key.offset;
622 continue;
623 }
624
Chris Mason9078a3e2007-04-26 16:46:15 -0400625 radix_tree_tag_clear(radix, cache[i]->key.objectid +
626 cache[i]->key.offset - 1,
627 BTRFS_BLOCK_GROUP_DIRTY);
Chris Mason9078a3e2007-04-26 16:46:15 -0400628 }
629 }
630 btrfs_free_path(path);
631 return werr;
632}
633
Chris Masonbe744172007-05-06 10:15:01 -0400634int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
635 struct btrfs_root *root)
636{
637 int ret;
638 int ret2;
639 ret = write_dirty_block_radix(trans, root,
640 &root->fs_info->block_group_radix);
641 ret2 = write_dirty_block_radix(trans, root,
642 &root->fs_info->block_group_data_radix);
643 if (ret)
644 return ret;
645 if (ret2)
646 return ret2;
647 return 0;
648}
649
Chris Mason9078a3e2007-04-26 16:46:15 -0400650static int update_block_group(struct btrfs_trans_handle *trans,
651 struct btrfs_root *root,
Chris Mason1e2677e2007-05-29 16:52:18 -0400652 u64 blocknr, u64 num, int alloc, int mark_free,
653 int data)
Chris Mason9078a3e2007-04-26 16:46:15 -0400654{
655 struct btrfs_block_group_cache *cache;
656 struct btrfs_fs_info *info = root->fs_info;
657 u64 total = num;
658 u64 old_val;
659 u64 block_in_group;
Chris Masone37c9e62007-05-09 20:13:14 -0400660 u64 i;
Chris Mason1e2677e2007-05-29 16:52:18 -0400661 int ret;
Chris Mason3e1ad542007-05-07 20:03:49 -0400662
Chris Mason9078a3e2007-04-26 16:46:15 -0400663 while(total) {
Chris Mason5276aed2007-06-11 21:33:38 -0400664 cache = btrfs_lookup_block_group(info, blocknr);
Chris Mason3e1ad542007-05-07 20:03:49 -0400665 if (!cache) {
Chris Mason9078a3e2007-04-26 16:46:15 -0400666 return -1;
Chris Masoncd1bc462007-04-27 10:08:34 -0400667 }
Chris Mason9078a3e2007-04-26 16:46:15 -0400668 block_in_group = blocknr - cache->key.objectid;
669 WARN_ON(block_in_group > cache->key.offset);
Chris Mason3e1ad542007-05-07 20:03:49 -0400670 radix_tree_tag_set(cache->radix, cache->key.objectid +
Chris Masonbe744172007-05-06 10:15:01 -0400671 cache->key.offset - 1,
Chris Mason9078a3e2007-04-26 16:46:15 -0400672 BTRFS_BLOCK_GROUP_DIRTY);
673
674 old_val = btrfs_block_group_used(&cache->item);
675 num = min(total, cache->key.offset - block_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -0400676 if (alloc) {
Chris Masoncd1bc462007-04-27 10:08:34 -0400677 if (blocknr > cache->last_alloc)
678 cache->last_alloc = blocknr;
Chris Masone37c9e62007-05-09 20:13:14 -0400679 if (!cache->data) {
680 for (i = 0; i < num; i++) {
681 clear_radix_bit(&info->extent_map_radix,
682 blocknr + i);
683 }
684 }
Chris Mason1e2677e2007-05-29 16:52:18 -0400685 if (cache->data != data &&
Chris Mason84f54cf2007-06-12 07:43:08 -0400686 old_val < (cache->key.offset >> 1)) {
Chris Mason1e2677e2007-05-29 16:52:18 -0400687 cache->data = data;
688 radix_tree_delete(cache->radix,
689 cache->key.objectid +
690 cache->key.offset - 1);
691
692 if (data) {
693 cache->radix =
694 &info->block_group_data_radix;
695 cache->item.flags |=
696 BTRFS_BLOCK_GROUP_DATA;
697 } else {
698 cache->radix = &info->block_group_radix;
699 cache->item.flags &=
700 ~BTRFS_BLOCK_GROUP_DATA;
701 }
702 ret = radix_tree_insert(cache->radix,
703 cache->key.objectid +
704 cache->key.offset - 1,
705 (void *)cache);
706 }
707 old_val += num;
Chris Masoncd1bc462007-04-27 10:08:34 -0400708 } else {
Chris Mason9078a3e2007-04-26 16:46:15 -0400709 old_val -= num;
Chris Masoncd1bc462007-04-27 10:08:34 -0400710 if (blocknr < cache->first_free)
711 cache->first_free = blocknr;
Chris Masone37c9e62007-05-09 20:13:14 -0400712 if (!cache->data && mark_free) {
713 for (i = 0; i < num; i++) {
714 set_radix_bit(&info->extent_map_radix,
715 blocknr + i);
716 }
717 }
Chris Mason84f54cf2007-06-12 07:43:08 -0400718 if (old_val < (cache->key.offset >> 1) &&
719 old_val + num >= (cache->key.offset >> 1)) {
Chris Masone37c9e62007-05-09 20:13:14 -0400720 radix_tree_tag_set(cache->radix,
721 cache->key.objectid +
722 cache->key.offset - 1,
723 BTRFS_BLOCK_GROUP_AVAIL);
724 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400725 }
Chris Mason9078a3e2007-04-26 16:46:15 -0400726 btrfs_set_block_group_used(&cache->item, old_val);
Chris Masone37c9e62007-05-09 20:13:14 -0400727 total -= num;
728 blocknr += num;
Chris Mason9078a3e2007-04-26 16:46:15 -0400729 }
730 return 0;
731}
732
Chris Masonbe08c1b2007-05-03 09:06:49 -0400733static int try_remove_page(struct address_space *mapping, unsigned long index)
734{
735 int ret;
736 ret = invalidate_mapping_pages(mapping, index, index);
737 return ret;
738}
739
Chris Masone089f052007-03-16 16:20:31 -0400740int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, struct
741 btrfs_root *root)
Chris Masona28ec192007-03-06 20:08:01 -0500742{
Chris Mason8ef97622007-03-26 10:15:30 -0400743 unsigned long gang[8];
Chris Masonbe08c1b2007-05-03 09:06:49 -0400744 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Masonbe744172007-05-06 10:15:01 -0400745 struct btrfs_block_group_cache *block_group;
Chris Mason88fd1462007-03-16 08:56:18 -0400746 u64 first = 0;
Chris Masona28ec192007-03-06 20:08:01 -0500747 int ret;
748 int i;
Chris Mason8ef97622007-03-26 10:15:30 -0400749 struct radix_tree_root *pinned_radix = &root->fs_info->pinned_radix;
Chris Masone37c9e62007-05-09 20:13:14 -0400750 struct radix_tree_root *extent_radix = &root->fs_info->extent_map_radix;
Chris Masona28ec192007-03-06 20:08:01 -0500751
752 while(1) {
Chris Masone37c9e62007-05-09 20:13:14 -0400753 ret = find_first_radix_bit(pinned_radix, gang, 0,
Chris Mason8ef97622007-03-26 10:15:30 -0400754 ARRAY_SIZE(gang));
Chris Masona28ec192007-03-06 20:08:01 -0500755 if (!ret)
756 break;
Chris Mason88fd1462007-03-16 08:56:18 -0400757 if (!first)
Chris Mason8ef97622007-03-26 10:15:30 -0400758 first = gang[0];
Chris Mason0579da42007-03-07 16:15:30 -0500759 for (i = 0; i < ret; i++) {
Chris Mason8ef97622007-03-26 10:15:30 -0400760 clear_radix_bit(pinned_radix, gang[i]);
Chris Mason5276aed2007-06-11 21:33:38 -0400761 block_group = btrfs_lookup_block_group(root->fs_info,
762 gang[i]);
Chris Masonbe744172007-05-06 10:15:01 -0400763 if (block_group) {
764 WARN_ON(block_group->pinned == 0);
765 block_group->pinned--;
766 if (gang[i] < block_group->last_alloc)
767 block_group->last_alloc = gang[i];
Chris Masone37c9e62007-05-09 20:13:14 -0400768 if (gang[i] < block_group->last_prealloc)
769 block_group->last_prealloc = gang[i];
770 if (!block_group->data)
771 set_radix_bit(extent_radix, gang[i]);
Chris Masonbe744172007-05-06 10:15:01 -0400772 }
Chris Masonbe08c1b2007-05-03 09:06:49 -0400773 try_remove_page(btree_inode->i_mapping,
774 gang[i] << (PAGE_CACHE_SHIFT -
775 btree_inode->i_blkbits));
Chris Mason0579da42007-03-07 16:15:30 -0500776 }
Chris Masona28ec192007-03-06 20:08:01 -0500777 }
778 return 0;
779}
780
Chris Masone089f052007-03-16 16:20:31 -0400781static int finish_current_insert(struct btrfs_trans_handle *trans, struct
782 btrfs_root *extent_root)
Chris Mason037e6392007-03-07 11:50:24 -0500783{
Chris Masone2fa7222007-03-12 16:22:34 -0400784 struct btrfs_key ins;
Chris Mason234b63a2007-03-13 10:46:10 -0400785 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -0500786 int i;
787 int ret;
Chris Mason1261ec42007-03-20 20:35:03 -0400788 u64 super_blocks_used;
789 struct btrfs_fs_info *info = extent_root->fs_info;
Chris Mason037e6392007-03-07 11:50:24 -0500790
Chris Masoncf27e1e2007-03-13 09:49:06 -0400791 btrfs_set_extent_refs(&extent_item, 1);
Chris Mason037e6392007-03-07 11:50:24 -0500792 ins.offset = 1;
793 ins.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -0400794 btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
Chris Mason5d0c3e62007-04-23 17:01:05 -0400795 btrfs_set_extent_owner(&extent_item, extent_root->root_key.objectid);
Chris Mason037e6392007-03-07 11:50:24 -0500796
Chris Masonf2458e12007-04-25 15:52:25 -0400797 for (i = 0; i < extent_root->fs_info->extent_tree_insert_nr; i++) {
798 ins.objectid = extent_root->fs_info->extent_tree_insert[i];
Chris Mason1261ec42007-03-20 20:35:03 -0400799 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
800 btrfs_set_super_blocks_used(info->disk_super,
801 super_blocks_used + 1);
Chris Masone089f052007-03-16 16:20:31 -0400802 ret = btrfs_insert_item(trans, extent_root, &ins, &extent_item,
803 sizeof(extent_item));
Chris Mason037e6392007-03-07 11:50:24 -0500804 BUG_ON(ret);
805 }
Chris Masonf2458e12007-04-25 15:52:25 -0400806 extent_root->fs_info->extent_tree_insert_nr = 0;
Chris Mason037e6392007-03-07 11:50:24 -0500807 return 0;
808}
809
Chris Mason8ef97622007-03-26 10:15:30 -0400810static int pin_down_block(struct btrfs_root *root, u64 blocknr, int pending)
Chris Masone20d96d2007-03-22 12:13:20 -0400811{
812 int err;
Chris Mason78fae272007-03-25 11:35:08 -0400813 struct btrfs_header *header;
Chris Mason8ef97622007-03-26 10:15:30 -0400814 struct buffer_head *bh;
Chris Mason78fae272007-03-25 11:35:08 -0400815
Chris Masonf4b9aa82007-03-27 11:05:53 -0400816 if (!pending) {
Chris Masond98237b2007-03-28 13:57:48 -0400817 bh = btrfs_find_tree_block(root, blocknr);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400818 if (bh) {
819 if (buffer_uptodate(bh)) {
820 u64 transid =
821 root->fs_info->running_transaction->transid;
822 header = btrfs_buffer_header(bh);
823 if (btrfs_header_generation(header) ==
824 transid) {
825 btrfs_block_release(root, bh);
826 return 0;
827 }
Chris Masonf4b9aa82007-03-27 11:05:53 -0400828 }
Chris Masond6025572007-03-30 14:27:56 -0400829 btrfs_block_release(root, bh);
Chris Mason8ef97622007-03-26 10:15:30 -0400830 }
Chris Mason8ef97622007-03-26 10:15:30 -0400831 err = set_radix_bit(&root->fs_info->pinned_radix, blocknr);
Chris Masonbe744172007-05-06 10:15:01 -0400832 if (!err) {
833 struct btrfs_block_group_cache *cache;
Chris Mason5276aed2007-06-11 21:33:38 -0400834 cache = btrfs_lookup_block_group(root->fs_info,
835 blocknr);
Chris Masonbe744172007-05-06 10:15:01 -0400836 if (cache)
837 cache->pinned++;
838 }
Chris Masonf4b9aa82007-03-27 11:05:53 -0400839 } else {
840 err = set_radix_bit(&root->fs_info->pending_del_radix, blocknr);
841 }
Chris Masonbe744172007-05-06 10:15:01 -0400842 BUG_ON(err < 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400843 return 0;
844}
845
Chris Masona28ec192007-03-06 20:08:01 -0500846/*
847 * remove an extent from the root, returns 0 on success
848 */
Chris Masone089f052007-03-16 16:20:31 -0400849static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masone37c9e62007-05-09 20:13:14 -0400850 *root, u64 blocknr, u64 num_blocks, int pin,
851 int mark_free)
Chris Masona28ec192007-03-06 20:08:01 -0500852{
Chris Mason5caf2a02007-04-02 11:20:42 -0400853 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -0400854 struct btrfs_key key;
Chris Mason1261ec42007-03-20 20:35:03 -0400855 struct btrfs_fs_info *info = root->fs_info;
856 struct btrfs_root *extent_root = info->extent_root;
Chris Masona28ec192007-03-06 20:08:01 -0500857 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400858 struct btrfs_extent_item *ei;
Chris Masone2fa7222007-03-12 16:22:34 -0400859 struct btrfs_key ins;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400860 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500861
Chris Masona28ec192007-03-06 20:08:01 -0500862 key.objectid = blocknr;
863 key.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -0400864 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masona28ec192007-03-06 20:08:01 -0500865 key.offset = num_blocks;
866
Chris Mason5caf2a02007-04-02 11:20:42 -0400867 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -0400868 if (!path)
869 return -ENOMEM;
870
871 ret = find_free_extent(trans, root, 0, 0, (u64)-1, 0, &ins, 0);
872 if (ret) {
873 btrfs_free_path(path);
874 return ret;
875 }
Chris Mason5f26f772007-04-05 10:38:44 -0400876
Chris Mason5caf2a02007-04-02 11:20:42 -0400877 ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400878 if (ret < 0)
879 return ret;
880 BUG_ON(ret);
Chris Mason5caf2a02007-04-02 11:20:42 -0400881 ei = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
Chris Mason123abc82007-03-14 14:14:43 -0400882 struct btrfs_extent_item);
Chris Masona28ec192007-03-06 20:08:01 -0500883 BUG_ON(ei->refs == 0);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400884 refs = btrfs_extent_refs(ei) - 1;
885 btrfs_set_extent_refs(ei, refs);
Chris Mason5caf2a02007-04-02 11:20:42 -0400886 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400887 if (refs == 0) {
Chris Mason1261ec42007-03-20 20:35:03 -0400888 u64 super_blocks_used;
Chris Mason78fae272007-03-25 11:35:08 -0400889
890 if (pin) {
Chris Mason8ef97622007-03-26 10:15:30 -0400891 ret = pin_down_block(root, blocknr, 0);
Chris Mason78fae272007-03-25 11:35:08 -0400892 BUG_ON(ret);
893 }
894
Chris Mason1261ec42007-03-20 20:35:03 -0400895 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
896 btrfs_set_super_blocks_used(info->disk_super,
897 super_blocks_used - num_blocks);
Chris Mason5caf2a02007-04-02 11:20:42 -0400898 ret = btrfs_del_item(trans, extent_root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -0400899 if (ret) {
900 return ret;
901 }
Chris Masone37c9e62007-05-09 20:13:14 -0400902 ret = update_block_group(trans, root, blocknr, num_blocks, 0,
Chris Mason1e2677e2007-05-29 16:52:18 -0400903 mark_free, 0);
Chris Mason9078a3e2007-04-26 16:46:15 -0400904 BUG_ON(ret);
Chris Masona28ec192007-03-06 20:08:01 -0500905 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400906 btrfs_free_path(path);
Chris Masone089f052007-03-16 16:20:31 -0400907 finish_current_insert(trans, extent_root);
Chris Masona28ec192007-03-06 20:08:01 -0500908 return ret;
909}
910
911/*
Chris Masonfec577f2007-02-26 10:40:21 -0500912 * find all the blocks marked as pending in the radix tree and remove
913 * them from the extent map
914 */
Chris Masone089f052007-03-16 16:20:31 -0400915static int del_pending_extents(struct btrfs_trans_handle *trans, struct
916 btrfs_root *extent_root)
Chris Masonfec577f2007-02-26 10:40:21 -0500917{
918 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -0400919 int wret;
920 int err = 0;
Chris Mason8ef97622007-03-26 10:15:30 -0400921 unsigned long gang[4];
Chris Masonfec577f2007-02-26 10:40:21 -0500922 int i;
Chris Mason8ef97622007-03-26 10:15:30 -0400923 struct radix_tree_root *pending_radix;
924 struct radix_tree_root *pinned_radix;
Chris Masonbe744172007-05-06 10:15:01 -0400925 struct btrfs_block_group_cache *cache;
Chris Mason8ef97622007-03-26 10:15:30 -0400926
927 pending_radix = &extent_root->fs_info->pending_del_radix;
928 pinned_radix = &extent_root->fs_info->pinned_radix;
Chris Masonfec577f2007-02-26 10:40:21 -0500929
930 while(1) {
Chris Masone37c9e62007-05-09 20:13:14 -0400931 ret = find_first_radix_bit(pending_radix, gang, 0,
Chris Mason8ef97622007-03-26 10:15:30 -0400932 ARRAY_SIZE(gang));
Chris Masonfec577f2007-02-26 10:40:21 -0500933 if (!ret)
934 break;
935 for (i = 0; i < ret; i++) {
Chris Mason8ef97622007-03-26 10:15:30 -0400936 wret = set_radix_bit(pinned_radix, gang[i]);
Chris Masonbe744172007-05-06 10:15:01 -0400937 if (wret == 0) {
Chris Mason5276aed2007-06-11 21:33:38 -0400938 cache =
939 btrfs_lookup_block_group(extent_root->fs_info,
Chris Masonbe744172007-05-06 10:15:01 -0400940 gang[i]);
941 if (cache)
942 cache->pinned++;
943 }
944 if (wret < 0) {
945 printk(KERN_CRIT "set_radix_bit, err %d\n",
946 wret);
947 BUG_ON(wret < 0);
948 }
Chris Mason8ef97622007-03-26 10:15:30 -0400949 wret = clear_radix_bit(pending_radix, gang[i]);
950 BUG_ON(wret);
Chris Masond5719762007-03-23 10:01:08 -0400951 wret = __free_extent(trans, extent_root,
Chris Masone37c9e62007-05-09 20:13:14 -0400952 gang[i], 1, 0, 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400953 if (wret)
954 err = wret;
Chris Masonfec577f2007-02-26 10:40:21 -0500955 }
956 }
Chris Masone20d96d2007-03-22 12:13:20 -0400957 return err;
Chris Masonfec577f2007-02-26 10:40:21 -0500958}
959
960/*
961 * remove an extent from the root, returns 0 on success
962 */
Chris Masone089f052007-03-16 16:20:31 -0400963int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
964 *root, u64 blocknr, u64 num_blocks, int pin)
Chris Masonfec577f2007-02-26 10:40:21 -0500965{
Chris Mason9f5fae22007-03-20 14:38:32 -0400966 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Masonfec577f2007-02-26 10:40:21 -0500967 int pending_ret;
968 int ret;
Chris Masona28ec192007-03-06 20:08:01 -0500969
970 if (root == extent_root) {
Chris Mason8ef97622007-03-26 10:15:30 -0400971 pin_down_block(root, blocknr, 1);
Chris Masona28ec192007-03-06 20:08:01 -0500972 return 0;
973 }
Chris Masone37c9e62007-05-09 20:13:14 -0400974 ret = __free_extent(trans, root, blocknr, num_blocks, pin, pin == 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400975 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
Chris Masonfec577f2007-02-26 10:40:21 -0500976 return ret ? ret : pending_ret;
977}
978
979/*
980 * walks the btree of allocated extents and find a hole of a given size.
981 * The key ins is changed to record the hole:
982 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -0400983 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -0500984 * ins->offset == number of blocks
985 * Any available blocks before search_start are skipped.
986 */
Chris Masone089f052007-03-16 16:20:31 -0400987static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
988 *orig_root, u64 num_blocks, u64 search_start, u64
Chris Masonfbdc7622007-05-30 10:22:12 -0400989 search_end, u64 hint_block,
990 struct btrfs_key *ins, int data)
Chris Masonfec577f2007-02-26 10:40:21 -0500991{
Chris Mason5caf2a02007-04-02 11:20:42 -0400992 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -0400993 struct btrfs_key key;
Chris Masonfec577f2007-02-26 10:40:21 -0500994 int ret;
995 u64 hole_size = 0;
996 int slot = 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400997 u64 last_block = 0;
Chris Mason037e6392007-03-07 11:50:24 -0500998 u64 test_block;
Chris Masonbe744172007-05-06 10:15:01 -0400999 u64 orig_search_start = search_start;
Chris Masonfec577f2007-02-26 10:40:21 -05001000 int start_found;
Chris Mason234b63a2007-03-13 10:46:10 -04001001 struct btrfs_leaf *l;
Chris Mason9f5fae22007-03-20 14:38:32 -04001002 struct btrfs_root * root = orig_root->fs_info->extent_root;
Chris Masonf2458e12007-04-25 15:52:25 -04001003 struct btrfs_fs_info *info = root->fs_info;
Chris Mason0579da42007-03-07 16:15:30 -05001004 int total_needed = num_blocks;
Chris Masonf2458e12007-04-25 15:52:25 -04001005 int total_found = 0;
1006 int fill_prealloc = 0;
Chris Masone20d96d2007-03-22 12:13:20 -04001007 int level;
Chris Masonbe08c1b2007-05-03 09:06:49 -04001008 struct btrfs_block_group_cache *block_group;
Chris Masonbe744172007-05-06 10:15:01 -04001009 int full_scan = 0;
Chris Masonfbdc7622007-05-30 10:22:12 -04001010 int wrapped = 0;
Chris Masonde428b62007-05-18 13:28:27 -04001011 u64 limit;
Chris Masonfec577f2007-02-26 10:40:21 -05001012
Chris Masonb1a4d962007-04-04 15:27:52 -04001013 ins->flags = 0;
1014 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
1015
Chris Masone20d96d2007-03-22 12:13:20 -04001016 level = btrfs_header_level(btrfs_buffer_header(root->node));
Chris Masonf2458e12007-04-25 15:52:25 -04001017 if (num_blocks == 0) {
1018 fill_prealloc = 1;
1019 num_blocks = 1;
Chris Mason308535a2007-04-28 15:17:08 -04001020 total_needed = (min(level + 1, BTRFS_MAX_LEVEL) + 2) * 3;
Chris Masonf2458e12007-04-25 15:52:25 -04001021 }
Chris Mason85e55b12007-06-19 15:50:51 -04001022 if (fill_prealloc) {
1023 u64 first;
1024 int nr = info->extent_tree_prealloc_nr;
1025 first = info->extent_tree_prealloc[nr - 1];
1026 if (info->extent_tree_prealloc_nr >= total_needed &&
1027 first >= search_start) {
1028 ins->objectid = info->extent_tree_prealloc[0];
1029 ins->offset = 1;
1030 return 0;
1031 }
1032 info->extent_tree_prealloc_nr = 0;
1033 }
Chris Mason3e1ad542007-05-07 20:03:49 -04001034 if (search_end == (u64)-1)
1035 search_end = btrfs_super_total_blocks(info->disk_super);
Chris Masonfbdc7622007-05-30 10:22:12 -04001036 if (hint_block) {
Chris Mason5276aed2007-06-11 21:33:38 -04001037 block_group = btrfs_lookup_block_group(info, hint_block);
Chris Masonbe744172007-05-06 10:15:01 -04001038 block_group = btrfs_find_block_group(root, block_group,
Chris Masonfbdc7622007-05-30 10:22:12 -04001039 hint_block, data, 1);
Chris Masonbe744172007-05-06 10:15:01 -04001040 } else {
1041 block_group = btrfs_find_block_group(root,
1042 trans->block_group, 0,
Chris Masonde428b62007-05-18 13:28:27 -04001043 data, 1);
Chris Masonbe744172007-05-06 10:15:01 -04001044 }
1045
Chris Masone0115992007-06-19 16:23:05 -04001046 path = btrfs_alloc_path();
1047
Chris Masonbe744172007-05-06 10:15:01 -04001048check_failed:
Chris Mason1e2677e2007-05-29 16:52:18 -04001049 if (!block_group->data)
Chris Masone37c9e62007-05-09 20:13:14 -04001050 search_start = find_search_start(root, &block_group,
1051 search_start, total_needed);
Chris Masonfbdc7622007-05-30 10:22:12 -04001052 else if (!full_scan)
Chris Masone37c9e62007-05-09 20:13:14 -04001053 search_start = max(block_group->last_alloc, search_start);
1054
Chris Mason5caf2a02007-04-02 11:20:42 -04001055 btrfs_init_path(path);
Chris Masonfec577f2007-02-26 10:40:21 -05001056 ins->objectid = search_start;
1057 ins->offset = 0;
Chris Masonfec577f2007-02-26 10:40:21 -05001058 start_found = 0;
Chris Masone37c9e62007-05-09 20:13:14 -04001059
Chris Mason5caf2a02007-04-02 11:20:42 -04001060 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
Chris Mason0f70abe2007-02-28 16:46:22 -05001061 if (ret < 0)
1062 goto error;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001063
Chris Masone37c9e62007-05-09 20:13:14 -04001064 if (path->slots[0] > 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001065 path->slots[0]--;
Chris Masone37c9e62007-05-09 20:13:14 -04001066 }
1067
1068 l = btrfs_buffer_leaf(path->nodes[0]);
1069 btrfs_disk_key_to_cpu(&key, &l->items[path->slots[0]].key);
1070 /*
1071 * a rare case, go back one key if we hit a block group item
1072 * instead of an extent item
1073 */
1074 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY &&
1075 key.objectid + key.offset >= search_start) {
1076 ins->objectid = key.objectid;
1077 ins->offset = key.offset - 1;
1078 btrfs_release_path(root, path);
1079 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
1080 if (ret < 0)
1081 goto error;
1082
1083 if (path->slots[0] > 0) {
1084 path->slots[0]--;
1085 }
1086 }
Chris Mason0579da42007-03-07 16:15:30 -05001087
Chris Masonfec577f2007-02-26 10:40:21 -05001088 while (1) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001089 l = btrfs_buffer_leaf(path->nodes[0]);
1090 slot = path->slots[0];
Chris Mason7518a232007-03-12 12:01:18 -04001091 if (slot >= btrfs_header_nritems(&l->header)) {
Chris Masonf2458e12007-04-25 15:52:25 -04001092 if (fill_prealloc) {
1093 info->extent_tree_prealloc_nr = 0;
1094 total_found = 0;
1095 }
Chris Masonde428b62007-05-18 13:28:27 -04001096 if (start_found)
1097 limit = last_block +
Chris Mason84f54cf2007-06-12 07:43:08 -04001098 (block_group->key.offset >> 1);
Chris Masonde428b62007-05-18 13:28:27 -04001099 else
1100 limit = search_start +
Chris Mason84f54cf2007-06-12 07:43:08 -04001101 (block_group->key.offset >> 1);
Chris Mason5caf2a02007-04-02 11:20:42 -04001102 ret = btrfs_next_leaf(root, path);
Chris Masonfec577f2007-02-26 10:40:21 -05001103 if (ret == 0)
1104 continue;
Chris Mason0f70abe2007-02-28 16:46:22 -05001105 if (ret < 0)
1106 goto error;
Chris Masonfec577f2007-02-26 10:40:21 -05001107 if (!start_found) {
1108 ins->objectid = search_start;
Chris Mason3e1ad542007-05-07 20:03:49 -04001109 ins->offset = search_end - search_start;
Chris Masonfec577f2007-02-26 10:40:21 -05001110 start_found = 1;
1111 goto check_pending;
1112 }
1113 ins->objectid = last_block > search_start ?
1114 last_block : search_start;
Chris Mason3e1ad542007-05-07 20:03:49 -04001115 ins->offset = search_end - ins->objectid;
Chris Masonfec577f2007-02-26 10:40:21 -05001116 goto check_pending;
1117 }
Chris Masone37c9e62007-05-09 20:13:14 -04001118
Chris Masone2fa7222007-03-12 16:22:34 -04001119 btrfs_disk_key_to_cpu(&key, &l->items[slot].key);
Chris Masone37c9e62007-05-09 20:13:14 -04001120 if (key.objectid >= search_start && key.objectid > last_block &&
1121 start_found) {
1122 if (last_block < search_start)
1123 last_block = search_start;
1124 hole_size = key.objectid - last_block;
1125 if (hole_size >= num_blocks) {
1126 ins->objectid = last_block;
1127 ins->offset = hole_size;
1128 goto check_pending;
Chris Mason0579da42007-03-07 16:15:30 -05001129 }
Chris Masonfec577f2007-02-26 10:40:21 -05001130 }
Chris Masone37c9e62007-05-09 20:13:14 -04001131
1132 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY)
1133 goto next;
1134
Chris Mason0579da42007-03-07 16:15:30 -05001135 start_found = 1;
Chris Masone2fa7222007-03-12 16:22:34 -04001136 last_block = key.objectid + key.offset;
Chris Masonfbdc7622007-05-30 10:22:12 -04001137 if (!full_scan && last_block >= block_group->key.objectid +
Chris Masonbe744172007-05-06 10:15:01 -04001138 block_group->key.offset) {
1139 btrfs_release_path(root, path);
1140 search_start = block_group->key.objectid +
1141 block_group->key.offset * 2;
1142 goto new_group;
1143 }
Chris Mason9078a3e2007-04-26 16:46:15 -04001144next:
Chris Mason5caf2a02007-04-02 11:20:42 -04001145 path->slots[0]++;
Chris Masonde428b62007-05-18 13:28:27 -04001146 cond_resched();
Chris Masonfec577f2007-02-26 10:40:21 -05001147 }
Chris Masonfec577f2007-02-26 10:40:21 -05001148check_pending:
1149 /* we have to make sure we didn't find an extent that has already
1150 * been allocated by the map tree or the original allocation
1151 */
Chris Mason5caf2a02007-04-02 11:20:42 -04001152 btrfs_release_path(root, path);
Chris Masonfec577f2007-02-26 10:40:21 -05001153 BUG_ON(ins->objectid < search_start);
Chris Masone37c9e62007-05-09 20:13:14 -04001154
Chris Mason3e1ad542007-05-07 20:03:49 -04001155 if (ins->objectid + num_blocks >= search_end) {
Chris Masonfbdc7622007-05-30 10:22:12 -04001156 if (full_scan) {
1157 ret = -ENOSPC;
1158 goto error;
1159 }
Chris Masonbe744172007-05-06 10:15:01 -04001160 search_start = orig_search_start;
Chris Masonfbdc7622007-05-30 10:22:12 -04001161 if (wrapped)
1162 full_scan = 1;
1163 else
1164 wrapped = 1;
Chris Masonbe744172007-05-06 10:15:01 -04001165 goto new_group;
Chris Mason06a2f9f2007-04-28 08:48:10 -04001166 }
Chris Mason037e6392007-03-07 11:50:24 -05001167 for (test_block = ins->objectid;
Chris Masonf2458e12007-04-25 15:52:25 -04001168 test_block < ins->objectid + num_blocks; test_block++) {
1169 if (test_radix_bit(&info->pinned_radix, test_block)) {
Chris Mason037e6392007-03-07 11:50:24 -05001170 search_start = test_block + 1;
Chris Masonbe744172007-05-06 10:15:01 -04001171 goto new_group;
Chris Masonfec577f2007-02-26 10:40:21 -05001172 }
1173 }
Chris Masonf2458e12007-04-25 15:52:25 -04001174 if (!fill_prealloc && info->extent_tree_insert_nr) {
1175 u64 last =
1176 info->extent_tree_insert[info->extent_tree_insert_nr - 1];
1177 if (ins->objectid + num_blocks >
1178 info->extent_tree_insert[0] &&
1179 ins->objectid <= last) {
1180 search_start = last + 1;
Chris Masone37c9e62007-05-09 20:13:14 -04001181 WARN_ON(!full_scan);
Chris Masonbe744172007-05-06 10:15:01 -04001182 goto new_group;
Chris Masonf2458e12007-04-25 15:52:25 -04001183 }
1184 }
1185 if (!fill_prealloc && info->extent_tree_prealloc_nr) {
1186 u64 first =
1187 info->extent_tree_prealloc[info->extent_tree_prealloc_nr - 1];
1188 if (ins->objectid + num_blocks > first &&
1189 ins->objectid <= info->extent_tree_prealloc[0]) {
1190 search_start = info->extent_tree_prealloc[0] + 1;
Chris Masonbe744172007-05-06 10:15:01 -04001191 goto new_group;
Chris Masonf2458e12007-04-25 15:52:25 -04001192 }
1193 }
1194 if (fill_prealloc) {
1195 int nr;
1196 test_block = ins->objectid;
Chris Masone37c9e62007-05-09 20:13:14 -04001197 if (test_block - info->extent_tree_prealloc[total_needed - 1] >=
1198 leaf_range(root)) {
1199 total_found = 0;
1200 info->extent_tree_prealloc_nr = total_found;
1201 }
Chris Masonf2458e12007-04-25 15:52:25 -04001202 while(test_block < ins->objectid + ins->offset &&
1203 total_found < total_needed) {
1204 nr = total_needed - total_found - 1;
1205 BUG_ON(nr < 0);
Chris Masoncd1bc462007-04-27 10:08:34 -04001206 info->extent_tree_prealloc[nr] = test_block;
Chris Masonf2458e12007-04-25 15:52:25 -04001207 total_found++;
1208 test_block++;
1209 }
1210 if (total_found < total_needed) {
1211 search_start = test_block;
Chris Masonbe744172007-05-06 10:15:01 -04001212 goto new_group;
Chris Masonf2458e12007-04-25 15:52:25 -04001213 }
Chris Masoncd1bc462007-04-27 10:08:34 -04001214 info->extent_tree_prealloc_nr = total_found;
Chris Masonf2458e12007-04-25 15:52:25 -04001215 }
Chris Masone37c9e62007-05-09 20:13:14 -04001216 if (!data) {
Chris Mason5276aed2007-06-11 21:33:38 -04001217 block_group = btrfs_lookup_block_group(info, ins->objectid);
Chris Masone37c9e62007-05-09 20:13:14 -04001218 if (block_group) {
1219 if (fill_prealloc)
1220 block_group->last_prealloc =
1221 info->extent_tree_prealloc[total_needed-1];
1222 else
1223 trans->block_group = block_group;
1224 }
Chris Masoncd1bc462007-04-27 10:08:34 -04001225 }
Chris Mason037e6392007-03-07 11:50:24 -05001226 ins->offset = num_blocks;
Chris Mason5caf2a02007-04-02 11:20:42 -04001227 btrfs_free_path(path);
Chris Masonfec577f2007-02-26 10:40:21 -05001228 return 0;
Chris Masonbe744172007-05-06 10:15:01 -04001229
1230new_group:
Chris Mason3e1ad542007-05-07 20:03:49 -04001231 if (search_start + num_blocks >= search_end) {
Chris Masonbe744172007-05-06 10:15:01 -04001232 search_start = orig_search_start;
Chris Masonfbdc7622007-05-30 10:22:12 -04001233 if (full_scan) {
1234 ret = -ENOSPC;
1235 goto error;
1236 }
1237 if (wrapped)
1238 full_scan = 1;
1239 else
1240 wrapped = 1;
Chris Masonbe744172007-05-06 10:15:01 -04001241 }
Chris Mason5276aed2007-06-11 21:33:38 -04001242 block_group = btrfs_lookup_block_group(info, search_start);
Chris Masonfbdc7622007-05-30 10:22:12 -04001243 cond_resched();
Chris Masonbe744172007-05-06 10:15:01 -04001244 if (!full_scan)
1245 block_group = btrfs_find_block_group(root, block_group,
Chris Masonde428b62007-05-18 13:28:27 -04001246 search_start, data, 0);
Chris Masonbe744172007-05-06 10:15:01 -04001247 goto check_failed;
1248
Chris Mason0f70abe2007-02-28 16:46:22 -05001249error:
Chris Mason5caf2a02007-04-02 11:20:42 -04001250 btrfs_release_path(root, path);
1251 btrfs_free_path(path);
Chris Mason0f70abe2007-02-28 16:46:22 -05001252 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05001253}
Chris Masonfec577f2007-02-26 10:40:21 -05001254/*
Chris Masonfec577f2007-02-26 10:40:21 -05001255 * finds a free extent and does all the dirty work required for allocation
1256 * returns the key for the extent through ins, and a tree buffer for
1257 * the first block of the extent through buf.
1258 *
1259 * returns 0 if everything worked, non-zero otherwise.
1260 */
Chris Mason4d775672007-04-20 20:23:12 -04001261int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
1262 struct btrfs_root *root, u64 owner,
Chris Masonfbdc7622007-05-30 10:22:12 -04001263 u64 num_blocks, u64 hint_block,
Chris Masonbe08c1b2007-05-03 09:06:49 -04001264 u64 search_end, struct btrfs_key *ins, int data)
Chris Masonfec577f2007-02-26 10:40:21 -05001265{
1266 int ret;
1267 int pending_ret;
Chris Mason1261ec42007-03-20 20:35:03 -04001268 u64 super_blocks_used;
Chris Masonfbdc7622007-05-30 10:22:12 -04001269 u64 search_start = 0;
Chris Mason1261ec42007-03-20 20:35:03 -04001270 struct btrfs_fs_info *info = root->fs_info;
1271 struct btrfs_root *extent_root = info->extent_root;
Chris Mason234b63a2007-03-13 10:46:10 -04001272 struct btrfs_extent_item extent_item;
Chris Masonf2458e12007-04-25 15:52:25 -04001273 struct btrfs_key prealloc_key;
Chris Mason037e6392007-03-07 11:50:24 -05001274
Chris Masoncf27e1e2007-03-13 09:49:06 -04001275 btrfs_set_extent_refs(&extent_item, 1);
Chris Mason4d775672007-04-20 20:23:12 -04001276 btrfs_set_extent_owner(&extent_item, owner);
Chris Masonfec577f2007-02-26 10:40:21 -05001277
Chris Mason037e6392007-03-07 11:50:24 -05001278 if (root == extent_root) {
Chris Masonf2458e12007-04-25 15:52:25 -04001279 int nr;
1280 BUG_ON(info->extent_tree_prealloc_nr == 0);
Chris Mason037e6392007-03-07 11:50:24 -05001281 BUG_ON(num_blocks != 1);
Chris Mason037e6392007-03-07 11:50:24 -05001282 ins->offset = 1;
Chris Masonf2458e12007-04-25 15:52:25 -04001283 info->extent_tree_prealloc_nr--;
1284 nr = info->extent_tree_prealloc_nr;
1285 ins->objectid = info->extent_tree_prealloc[nr];
1286 info->extent_tree_insert[info->extent_tree_insert_nr++] =
1287 ins->objectid;
Chris Mason9078a3e2007-04-26 16:46:15 -04001288 ret = update_block_group(trans, root,
Chris Mason1e2677e2007-05-29 16:52:18 -04001289 ins->objectid, ins->offset, 1, 0, 0);
Chris Mason9078a3e2007-04-26 16:46:15 -04001290 BUG_ON(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05001291 return 0;
1292 }
Chris Masone37c9e62007-05-09 20:13:14 -04001293
1294 /*
1295 * if we're doing a data allocation, preallocate room in the
1296 * extent tree first. This way the extent tree blocks end up
1297 * in the correct block group.
1298 */
1299 if (data) {
Chris Masonde428b62007-05-18 13:28:27 -04001300 ret = find_free_extent(trans, root, 0, 0,
Chris Masonfbdc7622007-05-30 10:22:12 -04001301 search_end, 0, &prealloc_key, 0);
Chris Masone37c9e62007-05-09 20:13:14 -04001302 if (ret) {
1303 return ret;
1304 }
1305 if (prealloc_key.objectid + prealloc_key.offset >= search_end) {
1306 int nr = info->extent_tree_prealloc_nr;
1307 search_end = info->extent_tree_prealloc[nr - 1] - 1;
1308 } else {
1309 search_start = info->extent_tree_prealloc[0] + 1;
1310 }
1311 }
Chris Masonfbdc7622007-05-30 10:22:12 -04001312 if (hint_block < search_start)
1313 hint_block = search_start;
Chris Masonf2458e12007-04-25 15:52:25 -04001314 /* do the real allocation */
Chris Masone089f052007-03-16 16:20:31 -04001315 ret = find_free_extent(trans, root, num_blocks, search_start,
Chris Masonfbdc7622007-05-30 10:22:12 -04001316 search_end, hint_block, ins, data);
Chris Masone37c9e62007-05-09 20:13:14 -04001317 if (ret) {
Chris Mason54aa1f42007-06-22 14:16:25 -04001318 if (search_start == 0)
1319 return ret;
1320 search_end = search_start - 1;
1321 search_start = 0;
1322 hint_block = search_start;
1323 ret = find_free_extent(trans, root, num_blocks, search_start,
1324 search_end, hint_block, ins, data);
1325 if (ret)
1326 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -04001327 }
Chris Masonfec577f2007-02-26 10:40:21 -05001328
Chris Masone37c9e62007-05-09 20:13:14 -04001329 /*
1330 * if we're doing a metadata allocation, preallocate space in the
1331 * extent tree second. This way, we don't create a tiny hole
1332 * in the allocation map between any unused preallocation blocks
1333 * and the metadata block we're actually allocating. On disk,
1334 * it'll go:
1335 * [block we've allocated], [used prealloc 1], [ unused prealloc ]
1336 * The unused prealloc will get reused the next time around.
1337 */
1338 if (!data) {
1339 if (ins->objectid + ins->offset >= search_end)
1340 search_end = ins->objectid - 1;
1341 else
1342 search_start = ins->objectid + ins->offset;
Chris Mason3e1ad542007-05-07 20:03:49 -04001343
Chris Masonfbdc7622007-05-30 10:22:12 -04001344 if (hint_block < search_start)
1345 hint_block = search_start;
1346
Chris Masone37c9e62007-05-09 20:13:14 -04001347 ret = find_free_extent(trans, root, 0, search_start,
Chris Masonfbdc7622007-05-30 10:22:12 -04001348 search_end, hint_block,
1349 &prealloc_key, 0);
Chris Masone37c9e62007-05-09 20:13:14 -04001350 if (ret) {
Chris Mason54aa1f42007-06-22 14:16:25 -04001351 if (search_start == 0)
1352 return ret;
1353 search_end = search_start - 1;
1354 search_start = 0;
1355 hint_block = search_start;
1356 ret = find_free_extent(trans, root, 0, search_start,
1357 search_end, hint_block,
1358 &prealloc_key, 0);
1359 if (ret)
1360 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -04001361 }
1362 }
Chris Masonf2458e12007-04-25 15:52:25 -04001363
Chris Mason1261ec42007-03-20 20:35:03 -04001364 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
1365 btrfs_set_super_blocks_used(info->disk_super, super_blocks_used +
1366 num_blocks);
Chris Masone089f052007-03-16 16:20:31 -04001367 ret = btrfs_insert_item(trans, extent_root, ins, &extent_item,
1368 sizeof(extent_item));
Chris Mason037e6392007-03-07 11:50:24 -05001369
Chris Masone089f052007-03-16 16:20:31 -04001370 finish_current_insert(trans, extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -04001371 pending_ret = del_pending_extents(trans, extent_root);
Chris Masone37c9e62007-05-09 20:13:14 -04001372 if (ret) {
Chris Mason037e6392007-03-07 11:50:24 -05001373 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -04001374 }
1375 if (pending_ret) {
Chris Mason037e6392007-03-07 11:50:24 -05001376 return pending_ret;
Chris Masone37c9e62007-05-09 20:13:14 -04001377 }
Chris Mason1e2677e2007-05-29 16:52:18 -04001378 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0,
1379 data);
Chris Masonfabb5682007-06-07 22:13:21 -04001380 BUG_ON(ret);
Chris Mason037e6392007-03-07 11:50:24 -05001381 return 0;
Chris Masonfec577f2007-02-26 10:40:21 -05001382}
1383
1384/*
1385 * helper function to allocate a block for a given tree
1386 * returns the tree buffer or NULL.
1387 */
Chris Masone20d96d2007-03-22 12:13:20 -04001388struct buffer_head *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Chris Mason31f3c992007-04-30 15:25:45 -04001389 struct btrfs_root *root, u64 hint)
Chris Masonfec577f2007-02-26 10:40:21 -05001390{
Chris Masone2fa7222007-03-12 16:22:34 -04001391 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -05001392 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -04001393 struct buffer_head *buf;
Chris Masonfec577f2007-02-26 10:40:21 -05001394
Chris Mason4d775672007-04-20 20:23:12 -04001395 ret = btrfs_alloc_extent(trans, root, root->root_key.objectid,
Chris Masonde428b62007-05-18 13:28:27 -04001396 1, hint, (unsigned long)-1, &ins, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05001397 if (ret) {
Chris Mason54aa1f42007-06-22 14:16:25 -04001398 BUG_ON(ret > 0);
1399 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05001400 }
Chris Masond98237b2007-03-28 13:57:48 -04001401 buf = btrfs_find_create_tree_block(root, ins.objectid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001402 if (!buf) {
1403 btrfs_free_extent(trans, root, ins.objectid, 1, 0);
1404 return ERR_PTR(-ENOMEM);
1405 }
Chris Masondf2ce342007-03-23 11:00:45 -04001406 set_buffer_uptodate(buf);
Chris Mason090d1872007-05-01 08:53:32 -04001407 set_buffer_checked(buf);
Chris Mason7c4452b2007-04-28 09:29:35 -04001408 set_radix_bit(&trans->transaction->dirty_pages, buf->b_page->index);
Chris Masonfec577f2007-02-26 10:40:21 -05001409 return buf;
1410}
Chris Masona28ec192007-03-06 20:08:01 -05001411
Chris Mason6407bf62007-03-27 06:33:00 -04001412static int drop_leaf_ref(struct btrfs_trans_handle *trans,
1413 struct btrfs_root *root, struct buffer_head *cur)
1414{
1415 struct btrfs_disk_key *key;
1416 struct btrfs_leaf *leaf;
1417 struct btrfs_file_extent_item *fi;
1418 int i;
1419 int nritems;
1420 int ret;
1421
1422 BUG_ON(!btrfs_is_leaf(btrfs_buffer_node(cur)));
1423 leaf = btrfs_buffer_leaf(cur);
1424 nritems = btrfs_header_nritems(&leaf->header);
1425 for (i = 0; i < nritems; i++) {
Chris Mason3a686372007-05-24 13:35:57 -04001426 u64 disk_blocknr;
Chris Mason6407bf62007-03-27 06:33:00 -04001427 key = &leaf->items[i].key;
1428 if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY)
1429 continue;
1430 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
Chris Mason236454d2007-04-19 13:37:44 -04001431 if (btrfs_file_extent_type(fi) == BTRFS_FILE_EXTENT_INLINE)
1432 continue;
Chris Mason6407bf62007-03-27 06:33:00 -04001433 /*
1434 * FIXME make sure to insert a trans record that
1435 * repeats the snapshot del on crash
1436 */
Chris Mason3a686372007-05-24 13:35:57 -04001437 disk_blocknr = btrfs_file_extent_disk_blocknr(fi);
1438 if (disk_blocknr == 0)
1439 continue;
1440 ret = btrfs_free_extent(trans, root, disk_blocknr,
Chris Mason6407bf62007-03-27 06:33:00 -04001441 btrfs_file_extent_disk_num_blocks(fi),
1442 0);
1443 BUG_ON(ret);
1444 }
1445 return 0;
1446}
1447
Chris Masone0115992007-06-19 16:23:05 -04001448static void reada_walk_down(struct btrfs_root *root,
1449 struct btrfs_node *node)
1450{
1451 int i;
1452 u32 nritems;
1453 u64 blocknr;
1454 int ret;
1455 u32 refs;
1456
1457 nritems = btrfs_header_nritems(&node->header);
1458 for (i = 0; i < nritems; i++) {
1459 blocknr = btrfs_node_blockptr(node, i);
1460 ret = lookup_extent_ref(NULL, root, blocknr, 1, &refs);
1461 BUG_ON(ret);
1462 if (refs != 1)
1463 continue;
1464 ret = readahead_tree_block(root, blocknr);
1465 if (ret)
1466 break;
1467 }
1468}
1469
Chris Mason9aca1d52007-03-13 11:09:37 -04001470/*
1471 * helper function for drop_snapshot, this walks down the tree dropping ref
1472 * counts as it goes.
1473 */
Chris Masone089f052007-03-16 16:20:31 -04001474static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
1475 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05001476{
Chris Masone20d96d2007-03-22 12:13:20 -04001477 struct buffer_head *next;
1478 struct buffer_head *cur;
Chris Mason20524f02007-03-10 06:35:47 -05001479 u64 blocknr;
1480 int ret;
1481 u32 refs;
1482
Chris Mason5caf2a02007-04-02 11:20:42 -04001483 WARN_ON(*level < 0);
1484 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Masonb18c6682007-04-17 13:26:50 -04001485 ret = lookup_extent_ref(trans, root, bh_blocknr(path->nodes[*level]),
Chris Mason6407bf62007-03-27 06:33:00 -04001486 1, &refs);
Chris Mason20524f02007-03-10 06:35:47 -05001487 BUG_ON(ret);
1488 if (refs > 1)
1489 goto out;
Chris Masone0115992007-06-19 16:23:05 -04001490
Chris Mason9aca1d52007-03-13 11:09:37 -04001491 /*
1492 * walk down to the last node level and free all the leaves
1493 */
Chris Mason6407bf62007-03-27 06:33:00 -04001494 while(*level >= 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001495 WARN_ON(*level < 0);
1496 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason20524f02007-03-10 06:35:47 -05001497 cur = path->nodes[*level];
Chris Masone0115992007-06-19 16:23:05 -04001498
1499 if (*level > 0 && path->slots[*level] == 0)
1500 reada_walk_down(root, btrfs_buffer_node(cur));
1501
Chris Mason2c90e5d2007-04-02 10:50:19 -04001502 if (btrfs_header_level(btrfs_buffer_header(cur)) != *level)
1503 WARN_ON(1);
Chris Masone0115992007-06-19 16:23:05 -04001504
Chris Mason7518a232007-03-12 12:01:18 -04001505 if (path->slots[*level] >=
Chris Masone20d96d2007-03-22 12:13:20 -04001506 btrfs_header_nritems(btrfs_buffer_header(cur)))
Chris Mason20524f02007-03-10 06:35:47 -05001507 break;
Chris Mason6407bf62007-03-27 06:33:00 -04001508 if (*level == 0) {
1509 ret = drop_leaf_ref(trans, root, cur);
1510 BUG_ON(ret);
1511 break;
1512 }
Chris Masone20d96d2007-03-22 12:13:20 -04001513 blocknr = btrfs_node_blockptr(btrfs_buffer_node(cur),
1514 path->slots[*level]);
Chris Masonb18c6682007-04-17 13:26:50 -04001515 ret = lookup_extent_ref(trans, root, blocknr, 1, &refs);
Chris Mason6407bf62007-03-27 06:33:00 -04001516 BUG_ON(ret);
1517 if (refs != 1) {
Chris Mason20524f02007-03-10 06:35:47 -05001518 path->slots[*level]++;
Chris Masone089f052007-03-16 16:20:31 -04001519 ret = btrfs_free_extent(trans, root, blocknr, 1, 1);
Chris Mason20524f02007-03-10 06:35:47 -05001520 BUG_ON(ret);
1521 continue;
1522 }
Chris Mason20524f02007-03-10 06:35:47 -05001523 next = read_tree_block(root, blocknr);
Chris Mason5caf2a02007-04-02 11:20:42 -04001524 WARN_ON(*level <= 0);
Chris Mason83e15a22007-03-12 09:03:27 -04001525 if (path->nodes[*level-1])
Chris Mason234b63a2007-03-13 10:46:10 -04001526 btrfs_block_release(root, path->nodes[*level-1]);
Chris Mason20524f02007-03-10 06:35:47 -05001527 path->nodes[*level-1] = next;
Chris Masone20d96d2007-03-22 12:13:20 -04001528 *level = btrfs_header_level(btrfs_buffer_header(next));
Chris Mason20524f02007-03-10 06:35:47 -05001529 path->slots[*level] = 0;
1530 }
1531out:
Chris Mason5caf2a02007-04-02 11:20:42 -04001532 WARN_ON(*level < 0);
1533 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason6407bf62007-03-27 06:33:00 -04001534 ret = btrfs_free_extent(trans, root,
Chris Mason7eccb902007-04-11 15:53:25 -04001535 bh_blocknr(path->nodes[*level]), 1, 1);
Chris Mason234b63a2007-03-13 10:46:10 -04001536 btrfs_block_release(root, path->nodes[*level]);
Chris Mason20524f02007-03-10 06:35:47 -05001537 path->nodes[*level] = NULL;
1538 *level += 1;
1539 BUG_ON(ret);
1540 return 0;
1541}
1542
Chris Mason9aca1d52007-03-13 11:09:37 -04001543/*
1544 * helper for dropping snapshots. This walks back up the tree in the path
1545 * to find the first node higher up where we haven't yet gone through
1546 * all the slots
1547 */
Chris Masone089f052007-03-16 16:20:31 -04001548static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root
1549 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05001550{
1551 int i;
1552 int slot;
1553 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -04001554 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Mason20524f02007-03-10 06:35:47 -05001555 slot = path->slots[i];
Chris Masone20d96d2007-03-22 12:13:20 -04001556 if (slot < btrfs_header_nritems(
1557 btrfs_buffer_header(path->nodes[i])) - 1) {
Chris Mason20524f02007-03-10 06:35:47 -05001558 path->slots[i]++;
1559 *level = i;
1560 return 0;
1561 } else {
Chris Masone089f052007-03-16 16:20:31 -04001562 ret = btrfs_free_extent(trans, root,
Chris Mason7eccb902007-04-11 15:53:25 -04001563 bh_blocknr(path->nodes[*level]),
Chris Masone089f052007-03-16 16:20:31 -04001564 1, 1);
Chris Mason6407bf62007-03-27 06:33:00 -04001565 BUG_ON(ret);
Chris Mason234b63a2007-03-13 10:46:10 -04001566 btrfs_block_release(root, path->nodes[*level]);
Chris Mason83e15a22007-03-12 09:03:27 -04001567 path->nodes[*level] = NULL;
Chris Mason20524f02007-03-10 06:35:47 -05001568 *level = i + 1;
Chris Mason20524f02007-03-10 06:35:47 -05001569 }
1570 }
1571 return 1;
1572}
1573
Chris Mason9aca1d52007-03-13 11:09:37 -04001574/*
1575 * drop the reference count on the tree rooted at 'snap'. This traverses
1576 * the tree freeing any blocks that have a ref count of zero after being
1577 * decremented.
1578 */
Chris Masone089f052007-03-16 16:20:31 -04001579int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masone20d96d2007-03-22 12:13:20 -04001580 *root, struct buffer_head *snap)
Chris Mason20524f02007-03-10 06:35:47 -05001581{
Chris Mason3768f362007-03-13 16:47:54 -04001582 int ret = 0;
Chris Mason9aca1d52007-03-13 11:09:37 -04001583 int wret;
Chris Mason20524f02007-03-10 06:35:47 -05001584 int level;
Chris Mason5caf2a02007-04-02 11:20:42 -04001585 struct btrfs_path *path;
Chris Mason20524f02007-03-10 06:35:47 -05001586 int i;
1587 int orig_level;
1588
Chris Mason5caf2a02007-04-02 11:20:42 -04001589 path = btrfs_alloc_path();
1590 BUG_ON(!path);
Chris Mason20524f02007-03-10 06:35:47 -05001591
Chris Masone20d96d2007-03-22 12:13:20 -04001592 level = btrfs_header_level(btrfs_buffer_header(snap));
Chris Mason20524f02007-03-10 06:35:47 -05001593 orig_level = level;
Chris Mason5caf2a02007-04-02 11:20:42 -04001594 path->nodes[level] = snap;
1595 path->slots[level] = 0;
Chris Mason20524f02007-03-10 06:35:47 -05001596 while(1) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001597 wret = walk_down_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04001598 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05001599 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04001600 if (wret < 0)
1601 ret = wret;
1602
Chris Mason5caf2a02007-04-02 11:20:42 -04001603 wret = walk_up_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04001604 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05001605 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04001606 if (wret < 0)
1607 ret = wret;
Chris Mason20524f02007-03-10 06:35:47 -05001608 }
Chris Mason83e15a22007-03-12 09:03:27 -04001609 for (i = 0; i <= orig_level; i++) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001610 if (path->nodes[i]) {
1611 btrfs_block_release(root, path->nodes[i]);
Chris Mason83e15a22007-03-12 09:03:27 -04001612 }
Chris Mason20524f02007-03-10 06:35:47 -05001613 }
Chris Mason5caf2a02007-04-02 11:20:42 -04001614 btrfs_free_path(path);
Chris Mason9aca1d52007-03-13 11:09:37 -04001615 return ret;
Chris Mason20524f02007-03-10 06:35:47 -05001616}
Chris Mason9078a3e2007-04-26 16:46:15 -04001617
Chris Masonbe744172007-05-06 10:15:01 -04001618static int free_block_group_radix(struct radix_tree_root *radix)
Chris Mason9078a3e2007-04-26 16:46:15 -04001619{
1620 int ret;
1621 struct btrfs_block_group_cache *cache[8];
1622 int i;
1623
1624 while(1) {
Chris Masonbe744172007-05-06 10:15:01 -04001625 ret = radix_tree_gang_lookup(radix, (void **)cache, 0,
Chris Mason9078a3e2007-04-26 16:46:15 -04001626 ARRAY_SIZE(cache));
1627 if (!ret)
1628 break;
1629 for (i = 0; i < ret; i++) {
Chris Masonbe744172007-05-06 10:15:01 -04001630 radix_tree_delete(radix, cache[i]->key.objectid +
Chris Mason9078a3e2007-04-26 16:46:15 -04001631 cache[i]->key.offset - 1);
1632 kfree(cache[i]);
1633 }
1634 }
1635 return 0;
1636}
1637
Chris Masonbe744172007-05-06 10:15:01 -04001638int btrfs_free_block_groups(struct btrfs_fs_info *info)
1639{
1640 int ret;
1641 int ret2;
Chris Masone37c9e62007-05-09 20:13:14 -04001642 unsigned long gang[16];
1643 int i;
Chris Masonbe744172007-05-06 10:15:01 -04001644
1645 ret = free_block_group_radix(&info->block_group_radix);
1646 ret2 = free_block_group_radix(&info->block_group_data_radix);
1647 if (ret)
1648 return ret;
1649 if (ret2)
1650 return ret2;
Chris Masone37c9e62007-05-09 20:13:14 -04001651
1652 while(1) {
1653 ret = find_first_radix_bit(&info->extent_map_radix,
1654 gang, 0, ARRAY_SIZE(gang));
1655 if (!ret)
1656 break;
1657 for (i = 0; i < ret; i++) {
1658 clear_radix_bit(&info->extent_map_radix, gang[i]);
1659 }
1660 }
Chris Masonbe744172007-05-06 10:15:01 -04001661 return 0;
1662}
1663
Chris Mason9078a3e2007-04-26 16:46:15 -04001664int btrfs_read_block_groups(struct btrfs_root *root)
1665{
1666 struct btrfs_path *path;
1667 int ret;
1668 int err = 0;
1669 struct btrfs_block_group_item *bi;
1670 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04001671 struct btrfs_fs_info *info = root->fs_info;
1672 struct radix_tree_root *radix;
Chris Mason9078a3e2007-04-26 16:46:15 -04001673 struct btrfs_key key;
1674 struct btrfs_key found_key;
1675 struct btrfs_leaf *leaf;
Chris Mason84f54cf2007-06-12 07:43:08 -04001676 u64 group_size_blocks;
Chris Mason31f3c992007-04-30 15:25:45 -04001677 u64 used;
Chris Mason9078a3e2007-04-26 16:46:15 -04001678
Chris Mason84f54cf2007-06-12 07:43:08 -04001679 group_size_blocks = BTRFS_BLOCK_GROUP_SIZE >>
1680 root->fs_info->sb->s_blocksize_bits;
Chris Masonbe744172007-05-06 10:15:01 -04001681 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04001682 key.objectid = 0;
1683 key.offset = group_size_blocks;
1684 key.flags = 0;
1685 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
1686
1687 path = btrfs_alloc_path();
1688 if (!path)
1689 return -ENOMEM;
1690
1691 while(1) {
Chris Masonbe744172007-05-06 10:15:01 -04001692 ret = btrfs_search_slot(NULL, info->extent_root,
Chris Mason9078a3e2007-04-26 16:46:15 -04001693 &key, path, 0, 0);
1694 if (ret != 0) {
1695 err = ret;
1696 break;
1697 }
1698 leaf = btrfs_buffer_leaf(path->nodes[0]);
1699 btrfs_disk_key_to_cpu(&found_key,
1700 &leaf->items[path->slots[0]].key);
1701 cache = kmalloc(sizeof(*cache), GFP_NOFS);
1702 if (!cache) {
1703 err = -1;
1704 break;
1705 }
Chris Mason3e1ad542007-05-07 20:03:49 -04001706
Chris Mason9078a3e2007-04-26 16:46:15 -04001707 bi = btrfs_item_ptr(leaf, path->slots[0],
1708 struct btrfs_block_group_item);
Chris Mason1e2677e2007-05-29 16:52:18 -04001709 if (bi->flags & BTRFS_BLOCK_GROUP_DATA) {
1710 radix = &info->block_group_data_radix;
1711 cache->data = 1;
1712 } else {
1713 radix = &info->block_group_radix;
1714 cache->data = 0;
1715 }
1716
Chris Mason9078a3e2007-04-26 16:46:15 -04001717 memcpy(&cache->item, bi, sizeof(*bi));
1718 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Mason31f3c992007-04-30 15:25:45 -04001719 cache->last_alloc = cache->key.objectid;
1720 cache->first_free = cache->key.objectid;
Chris Masone37c9e62007-05-09 20:13:14 -04001721 cache->last_prealloc = cache->key.objectid;
Chris Masonbe744172007-05-06 10:15:01 -04001722 cache->pinned = 0;
Chris Masone37c9e62007-05-09 20:13:14 -04001723 cache->cached = 0;
1724
Chris Mason3e1ad542007-05-07 20:03:49 -04001725 cache->radix = radix;
1726
Chris Mason9078a3e2007-04-26 16:46:15 -04001727 key.objectid = found_key.objectid + found_key.offset;
1728 btrfs_release_path(root, path);
Chris Masonbe744172007-05-06 10:15:01 -04001729 ret = radix_tree_insert(radix, found_key.objectid +
Chris Mason9078a3e2007-04-26 16:46:15 -04001730 found_key.offset - 1,
1731 (void *)cache);
1732 BUG_ON(ret);
Chris Mason31f3c992007-04-30 15:25:45 -04001733 used = btrfs_block_group_used(bi);
Chris Mason84f54cf2007-06-12 07:43:08 -04001734 if (used < div_factor(key.offset, 8)) {
Chris Masonbe744172007-05-06 10:15:01 -04001735 radix_tree_tag_set(radix, found_key.objectid +
Chris Mason31f3c992007-04-30 15:25:45 -04001736 found_key.offset - 1,
1737 BTRFS_BLOCK_GROUP_AVAIL);
1738 }
Chris Mason9078a3e2007-04-26 16:46:15 -04001739 if (key.objectid >=
Chris Masonbe744172007-05-06 10:15:01 -04001740 btrfs_super_total_blocks(info->disk_super))
Chris Mason9078a3e2007-04-26 16:46:15 -04001741 break;
1742 }
1743
1744 btrfs_free_path(path);
1745 return 0;
1746}