blob: 9455974dabea6e0e6e0c16878b3b0d13121f4b48 [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
Zach Brownec6b9102007-07-11 10:00:37 -040019#include <linux/sched.h>
Chris Masonfec577f2007-02-26 10:40:21 -050020#include "ctree.h"
21#include "disk-io.h"
22#include "print-tree.h"
Chris Masone089f052007-03-16 16:20:31 -040023#include "transaction.h"
Chris Masonfec577f2007-02-26 10:40:21 -050024
Chris Masone089f052007-03-16 16:20:31 -040025static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masonfbdc7622007-05-30 10:22:12 -040026 *orig_root, u64 num_blocks, u64 search_start,
27 u64 search_end, u64 hint_block,
Chris Masonf2654de2007-06-26 12:20:46 -040028 struct btrfs_key *ins, u64 exclude_start,
29 u64 exclude_nr, int data);
Chris Masone089f052007-03-16 16:20:31 -040030static int finish_current_insert(struct btrfs_trans_handle *trans, struct
31 btrfs_root *extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -040032static int del_pending_extents(struct btrfs_trans_handle *trans, struct
33 btrfs_root *extent_root);
Chris Masonfec577f2007-02-26 10:40:21 -050034
Chris 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:
Chris Masonccd467d2007-06-28 15:57:36 -0400526 WARN_ON(1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400527 for (i =0; i < faili; i++) {
528 if (leaf) {
529 u64 disk_blocknr;
530 key = &buf_leaf->items[i].key;
531 if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY)
532 continue;
533 fi = btrfs_item_ptr(buf_leaf, i,
534 struct btrfs_file_extent_item);
535 if (btrfs_file_extent_type(fi) ==
536 BTRFS_FILE_EXTENT_INLINE)
537 continue;
538 disk_blocknr = btrfs_file_extent_disk_blocknr(fi);
539 if (disk_blocknr == 0)
540 continue;
541 err = btrfs_free_extent(trans, root, disk_blocknr,
542 btrfs_file_extent_disk_num_blocks(fi), 0);
543 BUG_ON(err);
544 } else {
545 blocknr = btrfs_node_blockptr(buf_node, i);
546 err = btrfs_free_extent(trans, root, blocknr, 1, 0);
547 BUG_ON(err);
548 }
549 }
550 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -0500551}
552
Chris Mason9078a3e2007-04-26 16:46:15 -0400553static int write_one_cache_group(struct btrfs_trans_handle *trans,
554 struct btrfs_root *root,
555 struct btrfs_path *path,
556 struct btrfs_block_group_cache *cache)
557{
558 int ret;
559 int pending_ret;
560 struct btrfs_root *extent_root = root->fs_info->extent_root;
561 struct btrfs_block_group_item *bi;
562 struct btrfs_key ins;
563
Chris Masonf2654de2007-06-26 12:20:46 -0400564 ret = find_free_extent(trans, extent_root, 0, 0, (u64)-1, 0, &ins,
565 0, 0, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400566 /* FIXME, set bit to recalc cache groups on next mount */
567 if (ret)
568 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -0400569 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400570 if (ret < 0)
571 goto fail;
Chris Mason9078a3e2007-04-26 16:46:15 -0400572 BUG_ON(ret);
573 bi = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
574 struct btrfs_block_group_item);
575 memcpy(bi, &cache->item, sizeof(*bi));
Chris Masonccd467d2007-06-28 15:57:36 -0400576 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason9078a3e2007-04-26 16:46:15 -0400577 btrfs_release_path(extent_root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -0400578fail:
Chris Mason9078a3e2007-04-26 16:46:15 -0400579 finish_current_insert(trans, extent_root);
580 pending_ret = del_pending_extents(trans, extent_root);
581 if (ret)
582 return ret;
583 if (pending_ret)
584 return pending_ret;
Chris Masonbe744172007-05-06 10:15:01 -0400585 if (cache->data)
586 cache->last_alloc = cache->first_free;
Chris Mason9078a3e2007-04-26 16:46:15 -0400587 return 0;
588
589}
590
Chris Masonbe744172007-05-06 10:15:01 -0400591static int write_dirty_block_radix(struct btrfs_trans_handle *trans,
592 struct btrfs_root *root,
593 struct radix_tree_root *radix)
Chris Mason9078a3e2007-04-26 16:46:15 -0400594{
595 struct btrfs_block_group_cache *cache[8];
596 int ret;
597 int err = 0;
598 int werr = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -0400599 int i;
600 struct btrfs_path *path;
Chris Mason54aa1f42007-06-22 14:16:25 -0400601 unsigned long off = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -0400602
603 path = btrfs_alloc_path();
604 if (!path)
605 return -ENOMEM;
606
607 while(1) {
608 ret = radix_tree_gang_lookup_tag(radix, (void **)cache,
Chris Mason54aa1f42007-06-22 14:16:25 -0400609 off, ARRAY_SIZE(cache),
Chris Mason9078a3e2007-04-26 16:46:15 -0400610 BTRFS_BLOCK_GROUP_DIRTY);
611 if (!ret)
612 break;
613 for (i = 0; i < ret; i++) {
Chris Mason54aa1f42007-06-22 14:16:25 -0400614 err = write_one_cache_group(trans, root,
615 path, cache[i]);
616 /*
617 * if we fail to write the cache group, we want
618 * to keep it marked dirty in hopes that a later
619 * write will work
620 */
621 if (err) {
622 werr = err;
623 off = cache[i]->key.objectid +
624 cache[i]->key.offset;
625 continue;
626 }
627
Chris Mason9078a3e2007-04-26 16:46:15 -0400628 radix_tree_tag_clear(radix, cache[i]->key.objectid +
629 cache[i]->key.offset - 1,
630 BTRFS_BLOCK_GROUP_DIRTY);
Chris Mason9078a3e2007-04-26 16:46:15 -0400631 }
632 }
633 btrfs_free_path(path);
634 return werr;
635}
636
Chris Masonbe744172007-05-06 10:15:01 -0400637int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
638 struct btrfs_root *root)
639{
640 int ret;
641 int ret2;
642 ret = write_dirty_block_radix(trans, root,
643 &root->fs_info->block_group_radix);
644 ret2 = write_dirty_block_radix(trans, root,
645 &root->fs_info->block_group_data_radix);
646 if (ret)
647 return ret;
648 if (ret2)
649 return ret2;
650 return 0;
651}
652
Chris Mason9078a3e2007-04-26 16:46:15 -0400653static int update_block_group(struct btrfs_trans_handle *trans,
654 struct btrfs_root *root,
Chris Mason1e2677e2007-05-29 16:52:18 -0400655 u64 blocknr, u64 num, int alloc, int mark_free,
656 int data)
Chris Mason9078a3e2007-04-26 16:46:15 -0400657{
658 struct btrfs_block_group_cache *cache;
659 struct btrfs_fs_info *info = root->fs_info;
660 u64 total = num;
661 u64 old_val;
662 u64 block_in_group;
Chris Masone37c9e62007-05-09 20:13:14 -0400663 u64 i;
Chris Mason1e2677e2007-05-29 16:52:18 -0400664 int ret;
Chris Mason3e1ad542007-05-07 20:03:49 -0400665
Chris Mason9078a3e2007-04-26 16:46:15 -0400666 while(total) {
Chris Mason5276aed2007-06-11 21:33:38 -0400667 cache = btrfs_lookup_block_group(info, blocknr);
Chris Mason3e1ad542007-05-07 20:03:49 -0400668 if (!cache) {
Chris Mason9078a3e2007-04-26 16:46:15 -0400669 return -1;
Chris Masoncd1bc462007-04-27 10:08:34 -0400670 }
Chris Mason9078a3e2007-04-26 16:46:15 -0400671 block_in_group = blocknr - cache->key.objectid;
672 WARN_ON(block_in_group > cache->key.offset);
Chris Mason3e1ad542007-05-07 20:03:49 -0400673 radix_tree_tag_set(cache->radix, cache->key.objectid +
Chris Masonbe744172007-05-06 10:15:01 -0400674 cache->key.offset - 1,
Chris Mason9078a3e2007-04-26 16:46:15 -0400675 BTRFS_BLOCK_GROUP_DIRTY);
676
677 old_val = btrfs_block_group_used(&cache->item);
678 num = min(total, cache->key.offset - block_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -0400679 if (alloc) {
Chris Masoncd1bc462007-04-27 10:08:34 -0400680 if (blocknr > cache->last_alloc)
681 cache->last_alloc = blocknr;
Chris Masone37c9e62007-05-09 20:13:14 -0400682 if (!cache->data) {
683 for (i = 0; i < num; i++) {
684 clear_radix_bit(&info->extent_map_radix,
685 blocknr + i);
686 }
687 }
Chris Mason1e2677e2007-05-29 16:52:18 -0400688 if (cache->data != data &&
Chris Mason84f54cf2007-06-12 07:43:08 -0400689 old_val < (cache->key.offset >> 1)) {
Chris Mason1e2677e2007-05-29 16:52:18 -0400690 cache->data = data;
691 radix_tree_delete(cache->radix,
692 cache->key.objectid +
693 cache->key.offset - 1);
694
695 if (data) {
696 cache->radix =
697 &info->block_group_data_radix;
698 cache->item.flags |=
699 BTRFS_BLOCK_GROUP_DATA;
700 } else {
701 cache->radix = &info->block_group_radix;
702 cache->item.flags &=
703 ~BTRFS_BLOCK_GROUP_DATA;
704 }
705 ret = radix_tree_insert(cache->radix,
706 cache->key.objectid +
707 cache->key.offset - 1,
708 (void *)cache);
709 }
710 old_val += num;
Chris Masoncd1bc462007-04-27 10:08:34 -0400711 } else {
Chris Mason9078a3e2007-04-26 16:46:15 -0400712 old_val -= num;
Chris Masoncd1bc462007-04-27 10:08:34 -0400713 if (blocknr < cache->first_free)
714 cache->first_free = blocknr;
Chris Masone37c9e62007-05-09 20:13:14 -0400715 if (!cache->data && mark_free) {
716 for (i = 0; i < num; i++) {
717 set_radix_bit(&info->extent_map_radix,
718 blocknr + i);
719 }
720 }
Chris Mason84f54cf2007-06-12 07:43:08 -0400721 if (old_val < (cache->key.offset >> 1) &&
722 old_val + num >= (cache->key.offset >> 1)) {
Chris Masone37c9e62007-05-09 20:13:14 -0400723 radix_tree_tag_set(cache->radix,
724 cache->key.objectid +
725 cache->key.offset - 1,
726 BTRFS_BLOCK_GROUP_AVAIL);
727 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400728 }
Chris Mason9078a3e2007-04-26 16:46:15 -0400729 btrfs_set_block_group_used(&cache->item, old_val);
Chris Masone37c9e62007-05-09 20:13:14 -0400730 total -= num;
731 blocknr += num;
Chris Mason9078a3e2007-04-26 16:46:15 -0400732 }
733 return 0;
734}
735
Chris Masonbe08c1b2007-05-03 09:06:49 -0400736static int try_remove_page(struct address_space *mapping, unsigned long index)
737{
738 int ret;
739 ret = invalidate_mapping_pages(mapping, index, index);
740 return ret;
741}
742
Chris Masonccd467d2007-06-28 15:57:36 -0400743int btrfs_copy_pinned(struct btrfs_root *root, struct radix_tree_root *copy)
744{
745 unsigned long gang[8];
746 u64 last = 0;
747 struct radix_tree_root *pinned_radix = &root->fs_info->pinned_radix;
748 int ret;
749 int i;
750
751 while(1) {
752 ret = find_first_radix_bit(pinned_radix, gang, last,
753 ARRAY_SIZE(gang));
754 if (!ret)
755 break;
756 for (i = 0 ; i < ret; i++) {
757 set_radix_bit(copy, gang[i]);
758 last = gang[i] + 1;
759 }
760 }
761 return 0;
762}
763
764int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
765 struct btrfs_root *root,
766 struct radix_tree_root *unpin_radix)
Chris Masona28ec192007-03-06 20:08:01 -0500767{
Chris Mason8ef97622007-03-26 10:15:30 -0400768 unsigned long gang[8];
Chris Masonbe08c1b2007-05-03 09:06:49 -0400769 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Masonbe744172007-05-06 10:15:01 -0400770 struct btrfs_block_group_cache *block_group;
Chris Mason88fd1462007-03-16 08:56:18 -0400771 u64 first = 0;
Chris Masona28ec192007-03-06 20:08:01 -0500772 int ret;
773 int i;
Chris Mason8ef97622007-03-26 10:15:30 -0400774 struct radix_tree_root *pinned_radix = &root->fs_info->pinned_radix;
Chris Masone37c9e62007-05-09 20:13:14 -0400775 struct radix_tree_root *extent_radix = &root->fs_info->extent_map_radix;
Chris Masona28ec192007-03-06 20:08:01 -0500776
777 while(1) {
Chris Masonccd467d2007-06-28 15:57:36 -0400778 ret = find_first_radix_bit(unpin_radix, gang, 0,
Chris Mason8ef97622007-03-26 10:15:30 -0400779 ARRAY_SIZE(gang));
Chris Masona28ec192007-03-06 20:08:01 -0500780 if (!ret)
781 break;
Chris Mason88fd1462007-03-16 08:56:18 -0400782 if (!first)
Chris Mason8ef97622007-03-26 10:15:30 -0400783 first = gang[0];
Chris Mason0579da42007-03-07 16:15:30 -0500784 for (i = 0; i < ret; i++) {
Chris Mason8ef97622007-03-26 10:15:30 -0400785 clear_radix_bit(pinned_radix, gang[i]);
Chris Masonccd467d2007-06-28 15:57:36 -0400786 clear_radix_bit(unpin_radix, gang[i]);
Chris Mason5276aed2007-06-11 21:33:38 -0400787 block_group = btrfs_lookup_block_group(root->fs_info,
788 gang[i]);
Chris Masonbe744172007-05-06 10:15:01 -0400789 if (block_group) {
790 WARN_ON(block_group->pinned == 0);
791 block_group->pinned--;
792 if (gang[i] < block_group->last_alloc)
793 block_group->last_alloc = gang[i];
Chris Masone37c9e62007-05-09 20:13:14 -0400794 if (gang[i] < block_group->last_prealloc)
795 block_group->last_prealloc = gang[i];
796 if (!block_group->data)
797 set_radix_bit(extent_radix, gang[i]);
Chris Masonbe744172007-05-06 10:15:01 -0400798 }
Chris Masonbe08c1b2007-05-03 09:06:49 -0400799 try_remove_page(btree_inode->i_mapping,
800 gang[i] << (PAGE_CACHE_SHIFT -
801 btree_inode->i_blkbits));
Chris Mason0579da42007-03-07 16:15:30 -0500802 }
Chris Masona28ec192007-03-06 20:08:01 -0500803 }
804 return 0;
805}
806
Chris Masone089f052007-03-16 16:20:31 -0400807static int finish_current_insert(struct btrfs_trans_handle *trans, struct
808 btrfs_root *extent_root)
Chris Mason037e6392007-03-07 11:50:24 -0500809{
Chris Masone2fa7222007-03-12 16:22:34 -0400810 struct btrfs_key ins;
Chris Mason234b63a2007-03-13 10:46:10 -0400811 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -0500812 int i;
813 int ret;
Chris Mason1261ec42007-03-20 20:35:03 -0400814 u64 super_blocks_used;
815 struct btrfs_fs_info *info = extent_root->fs_info;
Chris Mason037e6392007-03-07 11:50:24 -0500816
Chris Masoncf27e1e2007-03-13 09:49:06 -0400817 btrfs_set_extent_refs(&extent_item, 1);
Chris Mason037e6392007-03-07 11:50:24 -0500818 ins.offset = 1;
819 ins.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -0400820 btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
Chris Mason5d0c3e62007-04-23 17:01:05 -0400821 btrfs_set_extent_owner(&extent_item, extent_root->root_key.objectid);
Chris Mason037e6392007-03-07 11:50:24 -0500822
Chris Masonf2458e12007-04-25 15:52:25 -0400823 for (i = 0; i < extent_root->fs_info->extent_tree_insert_nr; i++) {
824 ins.objectid = extent_root->fs_info->extent_tree_insert[i];
Chris Mason4b52dff2007-06-26 10:06:50 -0400825 super_blocks_used = btrfs_super_blocks_used(&info->super_copy);
826 btrfs_set_super_blocks_used(&info->super_copy,
Chris Mason1261ec42007-03-20 20:35:03 -0400827 super_blocks_used + 1);
Chris Masone089f052007-03-16 16:20:31 -0400828 ret = btrfs_insert_item(trans, extent_root, &ins, &extent_item,
829 sizeof(extent_item));
Chris Mason037e6392007-03-07 11:50:24 -0500830 BUG_ON(ret);
831 }
Chris Masonf2458e12007-04-25 15:52:25 -0400832 extent_root->fs_info->extent_tree_insert_nr = 0;
Chris Mason037e6392007-03-07 11:50:24 -0500833 return 0;
834}
835
Chris Mason8ef97622007-03-26 10:15:30 -0400836static int pin_down_block(struct btrfs_root *root, u64 blocknr, int pending)
Chris Masone20d96d2007-03-22 12:13:20 -0400837{
838 int err;
Chris Mason78fae272007-03-25 11:35:08 -0400839 struct btrfs_header *header;
Chris Mason8ef97622007-03-26 10:15:30 -0400840 struct buffer_head *bh;
Chris Mason78fae272007-03-25 11:35:08 -0400841
Chris Masonf4b9aa82007-03-27 11:05:53 -0400842 if (!pending) {
Chris Masond98237b2007-03-28 13:57:48 -0400843 bh = btrfs_find_tree_block(root, blocknr);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400844 if (bh) {
845 if (buffer_uptodate(bh)) {
846 u64 transid =
847 root->fs_info->running_transaction->transid;
848 header = btrfs_buffer_header(bh);
849 if (btrfs_header_generation(header) ==
850 transid) {
851 btrfs_block_release(root, bh);
852 return 0;
853 }
Chris Masonf4b9aa82007-03-27 11:05:53 -0400854 }
Chris Masond6025572007-03-30 14:27:56 -0400855 btrfs_block_release(root, bh);
Chris Mason8ef97622007-03-26 10:15:30 -0400856 }
Chris Mason8ef97622007-03-26 10:15:30 -0400857 err = set_radix_bit(&root->fs_info->pinned_radix, blocknr);
Chris Masonbe744172007-05-06 10:15:01 -0400858 if (!err) {
859 struct btrfs_block_group_cache *cache;
Chris Mason5276aed2007-06-11 21:33:38 -0400860 cache = btrfs_lookup_block_group(root->fs_info,
861 blocknr);
Chris Masonbe744172007-05-06 10:15:01 -0400862 if (cache)
863 cache->pinned++;
864 }
Chris Masonf4b9aa82007-03-27 11:05:53 -0400865 } else {
866 err = set_radix_bit(&root->fs_info->pending_del_radix, blocknr);
867 }
Chris Masonbe744172007-05-06 10:15:01 -0400868 BUG_ON(err < 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400869 return 0;
870}
871
Chris Masona28ec192007-03-06 20:08:01 -0500872/*
873 * remove an extent from the root, returns 0 on success
874 */
Chris Masone089f052007-03-16 16:20:31 -0400875static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masone37c9e62007-05-09 20:13:14 -0400876 *root, u64 blocknr, u64 num_blocks, int pin,
877 int mark_free)
Chris Masona28ec192007-03-06 20:08:01 -0500878{
Chris Mason5caf2a02007-04-02 11:20:42 -0400879 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -0400880 struct btrfs_key key;
Chris Mason1261ec42007-03-20 20:35:03 -0400881 struct btrfs_fs_info *info = root->fs_info;
882 struct btrfs_root *extent_root = info->extent_root;
Chris Masona28ec192007-03-06 20:08:01 -0500883 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400884 struct btrfs_extent_item *ei;
Chris Masone2fa7222007-03-12 16:22:34 -0400885 struct btrfs_key ins;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400886 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500887
Chris Masona28ec192007-03-06 20:08:01 -0500888 key.objectid = blocknr;
889 key.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -0400890 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masona28ec192007-03-06 20:08:01 -0500891 key.offset = num_blocks;
892
Chris Mason5caf2a02007-04-02 11:20:42 -0400893 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -0400894 if (!path)
895 return -ENOMEM;
896
Chris Masonf2654de2007-06-26 12:20:46 -0400897 ret = find_free_extent(trans, root, 0, 0, (u64)-1, 0, &ins, 0, 0, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400898 if (ret) {
899 btrfs_free_path(path);
900 return ret;
901 }
Chris Mason5f26f772007-04-05 10:38:44 -0400902
Chris Mason5caf2a02007-04-02 11:20:42 -0400903 ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400904 if (ret < 0)
905 return ret;
906 BUG_ON(ret);
Chris Mason5caf2a02007-04-02 11:20:42 -0400907 ei = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
Chris Mason123abc82007-03-14 14:14:43 -0400908 struct btrfs_extent_item);
Chris Masona28ec192007-03-06 20:08:01 -0500909 BUG_ON(ei->refs == 0);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400910 refs = btrfs_extent_refs(ei) - 1;
911 btrfs_set_extent_refs(ei, refs);
Chris Mason5caf2a02007-04-02 11:20:42 -0400912 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400913 if (refs == 0) {
Chris Mason1261ec42007-03-20 20:35:03 -0400914 u64 super_blocks_used;
Chris Mason78fae272007-03-25 11:35:08 -0400915
916 if (pin) {
Chris Mason8ef97622007-03-26 10:15:30 -0400917 ret = pin_down_block(root, blocknr, 0);
Chris Mason78fae272007-03-25 11:35:08 -0400918 BUG_ON(ret);
919 }
920
Chris Mason4b52dff2007-06-26 10:06:50 -0400921 super_blocks_used = btrfs_super_blocks_used(&info->super_copy);
922 btrfs_set_super_blocks_used(&info->super_copy,
Chris Mason1261ec42007-03-20 20:35:03 -0400923 super_blocks_used - num_blocks);
Chris Mason5caf2a02007-04-02 11:20:42 -0400924 ret = btrfs_del_item(trans, extent_root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -0400925 if (ret) {
926 return ret;
927 }
Chris Masone37c9e62007-05-09 20:13:14 -0400928 ret = update_block_group(trans, root, blocknr, num_blocks, 0,
Chris Mason1e2677e2007-05-29 16:52:18 -0400929 mark_free, 0);
Chris Mason9078a3e2007-04-26 16:46:15 -0400930 BUG_ON(ret);
Chris Masona28ec192007-03-06 20:08:01 -0500931 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400932 btrfs_free_path(path);
Chris Masone089f052007-03-16 16:20:31 -0400933 finish_current_insert(trans, extent_root);
Chris Masona28ec192007-03-06 20:08:01 -0500934 return ret;
935}
936
937/*
Chris Masonfec577f2007-02-26 10:40:21 -0500938 * find all the blocks marked as pending in the radix tree and remove
939 * them from the extent map
940 */
Chris Masone089f052007-03-16 16:20:31 -0400941static int del_pending_extents(struct btrfs_trans_handle *trans, struct
942 btrfs_root *extent_root)
Chris Masonfec577f2007-02-26 10:40:21 -0500943{
944 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -0400945 int wret;
946 int err = 0;
Chris Mason8ef97622007-03-26 10:15:30 -0400947 unsigned long gang[4];
Chris Masonfec577f2007-02-26 10:40:21 -0500948 int i;
Chris Mason8ef97622007-03-26 10:15:30 -0400949 struct radix_tree_root *pending_radix;
950 struct radix_tree_root *pinned_radix;
Chris Masonbe744172007-05-06 10:15:01 -0400951 struct btrfs_block_group_cache *cache;
Chris Mason8ef97622007-03-26 10:15:30 -0400952
953 pending_radix = &extent_root->fs_info->pending_del_radix;
954 pinned_radix = &extent_root->fs_info->pinned_radix;
Chris Masonfec577f2007-02-26 10:40:21 -0500955
956 while(1) {
Chris Masone37c9e62007-05-09 20:13:14 -0400957 ret = find_first_radix_bit(pending_radix, gang, 0,
Chris Mason8ef97622007-03-26 10:15:30 -0400958 ARRAY_SIZE(gang));
Chris Masonfec577f2007-02-26 10:40:21 -0500959 if (!ret)
960 break;
961 for (i = 0; i < ret; i++) {
Chris Mason8ef97622007-03-26 10:15:30 -0400962 wret = set_radix_bit(pinned_radix, gang[i]);
Chris Masonbe744172007-05-06 10:15:01 -0400963 if (wret == 0) {
Chris Mason5276aed2007-06-11 21:33:38 -0400964 cache =
965 btrfs_lookup_block_group(extent_root->fs_info,
Chris Masonbe744172007-05-06 10:15:01 -0400966 gang[i]);
967 if (cache)
968 cache->pinned++;
969 }
970 if (wret < 0) {
971 printk(KERN_CRIT "set_radix_bit, err %d\n",
972 wret);
973 BUG_ON(wret < 0);
974 }
Chris Mason8ef97622007-03-26 10:15:30 -0400975 wret = clear_radix_bit(pending_radix, gang[i]);
976 BUG_ON(wret);
Chris Masond5719762007-03-23 10:01:08 -0400977 wret = __free_extent(trans, extent_root,
Chris Masone37c9e62007-05-09 20:13:14 -0400978 gang[i], 1, 0, 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400979 if (wret)
980 err = wret;
Chris Masonfec577f2007-02-26 10:40:21 -0500981 }
982 }
Chris Masone20d96d2007-03-22 12:13:20 -0400983 return err;
Chris Masonfec577f2007-02-26 10:40:21 -0500984}
985
986/*
987 * remove an extent from the root, returns 0 on success
988 */
Chris Masone089f052007-03-16 16:20:31 -0400989int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
990 *root, u64 blocknr, u64 num_blocks, int pin)
Chris Masonfec577f2007-02-26 10:40:21 -0500991{
Chris Mason9f5fae22007-03-20 14:38:32 -0400992 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Masonfec577f2007-02-26 10:40:21 -0500993 int pending_ret;
994 int ret;
Chris Masona28ec192007-03-06 20:08:01 -0500995
996 if (root == extent_root) {
Chris Mason8ef97622007-03-26 10:15:30 -0400997 pin_down_block(root, blocknr, 1);
Chris Masona28ec192007-03-06 20:08:01 -0500998 return 0;
999 }
Chris Masone37c9e62007-05-09 20:13:14 -04001000 ret = __free_extent(trans, root, blocknr, num_blocks, pin, pin == 0);
Chris Masone20d96d2007-03-22 12:13:20 -04001001 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
Chris Masonfec577f2007-02-26 10:40:21 -05001002 return ret ? ret : pending_ret;
1003}
1004
1005/*
1006 * walks the btree of allocated extents and find a hole of a given size.
1007 * The key ins is changed to record the hole:
1008 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -04001009 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -05001010 * ins->offset == number of blocks
1011 * Any available blocks before search_start are skipped.
1012 */
Chris Masone089f052007-03-16 16:20:31 -04001013static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
1014 *orig_root, u64 num_blocks, u64 search_start, u64
Chris Masonfbdc7622007-05-30 10:22:12 -04001015 search_end, u64 hint_block,
Chris Masonf2654de2007-06-26 12:20:46 -04001016 struct btrfs_key *ins, u64 exclude_start,
1017 u64 exclude_nr, int data)
Chris Masonfec577f2007-02-26 10:40:21 -05001018{
Chris Mason5caf2a02007-04-02 11:20:42 -04001019 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -04001020 struct btrfs_key key;
Chris Masonfec577f2007-02-26 10:40:21 -05001021 int ret;
1022 u64 hole_size = 0;
1023 int slot = 0;
Chris Masone20d96d2007-03-22 12:13:20 -04001024 u64 last_block = 0;
Chris Mason037e6392007-03-07 11:50:24 -05001025 u64 test_block;
Chris Masonbe744172007-05-06 10:15:01 -04001026 u64 orig_search_start = search_start;
Chris Masonfec577f2007-02-26 10:40:21 -05001027 int start_found;
Chris Mason234b63a2007-03-13 10:46:10 -04001028 struct btrfs_leaf *l;
Chris Mason9f5fae22007-03-20 14:38:32 -04001029 struct btrfs_root * root = orig_root->fs_info->extent_root;
Chris Masonf2458e12007-04-25 15:52:25 -04001030 struct btrfs_fs_info *info = root->fs_info;
Chris Mason0579da42007-03-07 16:15:30 -05001031 int total_needed = num_blocks;
Chris Masonf2458e12007-04-25 15:52:25 -04001032 int total_found = 0;
1033 int fill_prealloc = 0;
Chris Masone20d96d2007-03-22 12:13:20 -04001034 int level;
Chris Masonbe08c1b2007-05-03 09:06:49 -04001035 struct btrfs_block_group_cache *block_group;
Chris Masonbe744172007-05-06 10:15:01 -04001036 int full_scan = 0;
Chris Masonfbdc7622007-05-30 10:22:12 -04001037 int wrapped = 0;
Chris Masonde428b62007-05-18 13:28:27 -04001038 u64 limit;
Chris Masonfec577f2007-02-26 10:40:21 -05001039
Chris Masonb1a4d962007-04-04 15:27:52 -04001040 ins->flags = 0;
1041 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
1042
Chris Masone20d96d2007-03-22 12:13:20 -04001043 level = btrfs_header_level(btrfs_buffer_header(root->node));
Chris Masonf2458e12007-04-25 15:52:25 -04001044 if (num_blocks == 0) {
1045 fill_prealloc = 1;
1046 num_blocks = 1;
Chris Mason308535a2007-04-28 15:17:08 -04001047 total_needed = (min(level + 1, BTRFS_MAX_LEVEL) + 2) * 3;
Chris Masonf2458e12007-04-25 15:52:25 -04001048 }
Chris Mason85e55b12007-06-19 15:50:51 -04001049 if (fill_prealloc) {
1050 u64 first;
1051 int nr = info->extent_tree_prealloc_nr;
1052 first = info->extent_tree_prealloc[nr - 1];
1053 if (info->extent_tree_prealloc_nr >= total_needed &&
1054 first >= search_start) {
1055 ins->objectid = info->extent_tree_prealloc[0];
1056 ins->offset = 1;
1057 return 0;
1058 }
1059 info->extent_tree_prealloc_nr = 0;
1060 }
Chris Mason3e1ad542007-05-07 20:03:49 -04001061 if (search_end == (u64)-1)
Chris Mason4b52dff2007-06-26 10:06:50 -04001062 search_end = btrfs_super_total_blocks(&info->super_copy);
Chris Masonfbdc7622007-05-30 10:22:12 -04001063 if (hint_block) {
Chris Mason5276aed2007-06-11 21:33:38 -04001064 block_group = btrfs_lookup_block_group(info, hint_block);
Chris Masonbe744172007-05-06 10:15:01 -04001065 block_group = btrfs_find_block_group(root, block_group,
Chris Masonfbdc7622007-05-30 10:22:12 -04001066 hint_block, data, 1);
Chris Masonbe744172007-05-06 10:15:01 -04001067 } else {
1068 block_group = btrfs_find_block_group(root,
1069 trans->block_group, 0,
Chris Masonde428b62007-05-18 13:28:27 -04001070 data, 1);
Chris Masonbe744172007-05-06 10:15:01 -04001071 }
1072
Chris Masone0115992007-06-19 16:23:05 -04001073 path = btrfs_alloc_path();
1074
Chris Masonbe744172007-05-06 10:15:01 -04001075check_failed:
Chris Mason1e2677e2007-05-29 16:52:18 -04001076 if (!block_group->data)
Chris Masone37c9e62007-05-09 20:13:14 -04001077 search_start = find_search_start(root, &block_group,
1078 search_start, total_needed);
Chris Masonfbdc7622007-05-30 10:22:12 -04001079 else if (!full_scan)
Chris Masone37c9e62007-05-09 20:13:14 -04001080 search_start = max(block_group->last_alloc, search_start);
1081
Chris Mason5caf2a02007-04-02 11:20:42 -04001082 btrfs_init_path(path);
Chris Masonfec577f2007-02-26 10:40:21 -05001083 ins->objectid = search_start;
1084 ins->offset = 0;
Chris Masonfec577f2007-02-26 10:40:21 -05001085 start_found = 0;
Chris Masone37c9e62007-05-09 20:13:14 -04001086
Chris Mason5caf2a02007-04-02 11:20:42 -04001087 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
Chris Mason0f70abe2007-02-28 16:46:22 -05001088 if (ret < 0)
1089 goto error;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001090
Chris Masone37c9e62007-05-09 20:13:14 -04001091 if (path->slots[0] > 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001092 path->slots[0]--;
Chris Masone37c9e62007-05-09 20:13:14 -04001093 }
1094
1095 l = btrfs_buffer_leaf(path->nodes[0]);
1096 btrfs_disk_key_to_cpu(&key, &l->items[path->slots[0]].key);
1097 /*
1098 * a rare case, go back one key if we hit a block group item
1099 * instead of an extent item
1100 */
1101 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY &&
1102 key.objectid + key.offset >= search_start) {
1103 ins->objectid = key.objectid;
1104 ins->offset = key.offset - 1;
1105 btrfs_release_path(root, path);
1106 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
1107 if (ret < 0)
1108 goto error;
1109
1110 if (path->slots[0] > 0) {
1111 path->slots[0]--;
1112 }
1113 }
Chris Mason0579da42007-03-07 16:15:30 -05001114
Chris Masonfec577f2007-02-26 10:40:21 -05001115 while (1) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001116 l = btrfs_buffer_leaf(path->nodes[0]);
1117 slot = path->slots[0];
Chris Mason7518a232007-03-12 12:01:18 -04001118 if (slot >= btrfs_header_nritems(&l->header)) {
Chris Masonf2458e12007-04-25 15:52:25 -04001119 if (fill_prealloc) {
1120 info->extent_tree_prealloc_nr = 0;
1121 total_found = 0;
1122 }
Chris Masonde428b62007-05-18 13:28:27 -04001123 if (start_found)
1124 limit = last_block +
Chris Mason84f54cf2007-06-12 07:43:08 -04001125 (block_group->key.offset >> 1);
Chris Masonde428b62007-05-18 13:28:27 -04001126 else
1127 limit = search_start +
Chris Mason84f54cf2007-06-12 07:43:08 -04001128 (block_group->key.offset >> 1);
Chris Mason5caf2a02007-04-02 11:20:42 -04001129 ret = btrfs_next_leaf(root, path);
Chris Masonfec577f2007-02-26 10:40:21 -05001130 if (ret == 0)
1131 continue;
Chris Mason0f70abe2007-02-28 16:46:22 -05001132 if (ret < 0)
1133 goto error;
Chris Masonfec577f2007-02-26 10:40:21 -05001134 if (!start_found) {
1135 ins->objectid = search_start;
Chris Mason3e1ad542007-05-07 20:03:49 -04001136 ins->offset = search_end - search_start;
Chris Masonfec577f2007-02-26 10:40:21 -05001137 start_found = 1;
1138 goto check_pending;
1139 }
1140 ins->objectid = last_block > search_start ?
1141 last_block : search_start;
Chris Mason3e1ad542007-05-07 20:03:49 -04001142 ins->offset = search_end - ins->objectid;
Chris Masonfec577f2007-02-26 10:40:21 -05001143 goto check_pending;
1144 }
Chris Masone37c9e62007-05-09 20:13:14 -04001145
Chris Masone2fa7222007-03-12 16:22:34 -04001146 btrfs_disk_key_to_cpu(&key, &l->items[slot].key);
Chris Masone37c9e62007-05-09 20:13:14 -04001147 if (key.objectid >= search_start && key.objectid > last_block &&
1148 start_found) {
1149 if (last_block < search_start)
1150 last_block = search_start;
1151 hole_size = key.objectid - last_block;
1152 if (hole_size >= num_blocks) {
1153 ins->objectid = last_block;
1154 ins->offset = hole_size;
1155 goto check_pending;
Chris Mason0579da42007-03-07 16:15:30 -05001156 }
Chris Masonfec577f2007-02-26 10:40:21 -05001157 }
Chris Masone37c9e62007-05-09 20:13:14 -04001158
1159 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY)
1160 goto next;
1161
Chris Mason0579da42007-03-07 16:15:30 -05001162 start_found = 1;
Chris Masone2fa7222007-03-12 16:22:34 -04001163 last_block = key.objectid + key.offset;
Chris Masonfbdc7622007-05-30 10:22:12 -04001164 if (!full_scan && last_block >= block_group->key.objectid +
Chris Masonbe744172007-05-06 10:15:01 -04001165 block_group->key.offset) {
1166 btrfs_release_path(root, path);
1167 search_start = block_group->key.objectid +
1168 block_group->key.offset * 2;
1169 goto new_group;
1170 }
Chris Mason9078a3e2007-04-26 16:46:15 -04001171next:
Chris Mason5caf2a02007-04-02 11:20:42 -04001172 path->slots[0]++;
Chris Masonde428b62007-05-18 13:28:27 -04001173 cond_resched();
Chris Masonfec577f2007-02-26 10:40:21 -05001174 }
Chris Masonfec577f2007-02-26 10:40:21 -05001175check_pending:
1176 /* we have to make sure we didn't find an extent that has already
1177 * been allocated by the map tree or the original allocation
1178 */
Chris Mason5caf2a02007-04-02 11:20:42 -04001179 btrfs_release_path(root, path);
Chris Masonfec577f2007-02-26 10:40:21 -05001180 BUG_ON(ins->objectid < search_start);
Chris Masone37c9e62007-05-09 20:13:14 -04001181
Chris Mason3e1ad542007-05-07 20:03:49 -04001182 if (ins->objectid + num_blocks >= search_end) {
Chris Masonfbdc7622007-05-30 10:22:12 -04001183 if (full_scan) {
1184 ret = -ENOSPC;
1185 goto error;
1186 }
Chris Masonbe744172007-05-06 10:15:01 -04001187 search_start = orig_search_start;
Chris Masonfbdc7622007-05-30 10:22:12 -04001188 if (wrapped)
1189 full_scan = 1;
1190 else
1191 wrapped = 1;
Chris Masonbe744172007-05-06 10:15:01 -04001192 goto new_group;
Chris Mason06a2f9f2007-04-28 08:48:10 -04001193 }
Chris Mason037e6392007-03-07 11:50:24 -05001194 for (test_block = ins->objectid;
Chris Masonf2458e12007-04-25 15:52:25 -04001195 test_block < ins->objectid + num_blocks; test_block++) {
1196 if (test_radix_bit(&info->pinned_radix, test_block)) {
Chris Mason037e6392007-03-07 11:50:24 -05001197 search_start = test_block + 1;
Chris Masonbe744172007-05-06 10:15:01 -04001198 goto new_group;
Chris Masonfec577f2007-02-26 10:40:21 -05001199 }
1200 }
Chris Masonf2458e12007-04-25 15:52:25 -04001201 if (!fill_prealloc && info->extent_tree_insert_nr) {
1202 u64 last =
1203 info->extent_tree_insert[info->extent_tree_insert_nr - 1];
1204 if (ins->objectid + num_blocks >
1205 info->extent_tree_insert[0] &&
1206 ins->objectid <= last) {
1207 search_start = last + 1;
Chris Masone37c9e62007-05-09 20:13:14 -04001208 WARN_ON(!full_scan);
Chris Masonbe744172007-05-06 10:15:01 -04001209 goto new_group;
Chris Masonf2458e12007-04-25 15:52:25 -04001210 }
1211 }
1212 if (!fill_prealloc && info->extent_tree_prealloc_nr) {
1213 u64 first =
1214 info->extent_tree_prealloc[info->extent_tree_prealloc_nr - 1];
1215 if (ins->objectid + num_blocks > first &&
1216 ins->objectid <= info->extent_tree_prealloc[0]) {
1217 search_start = info->extent_tree_prealloc[0] + 1;
Chris Masonbe744172007-05-06 10:15:01 -04001218 goto new_group;
Chris Masonf2458e12007-04-25 15:52:25 -04001219 }
1220 }
Chris Masonf2654de2007-06-26 12:20:46 -04001221 if (exclude_nr > 0 && (ins->objectid + num_blocks > exclude_start &&
1222 ins->objectid < exclude_start + exclude_nr)) {
1223 search_start = exclude_start + exclude_nr;
1224 goto new_group;
1225 }
Chris Masonf2458e12007-04-25 15:52:25 -04001226 if (fill_prealloc) {
1227 int nr;
1228 test_block = ins->objectid;
Chris Masone37c9e62007-05-09 20:13:14 -04001229 if (test_block - info->extent_tree_prealloc[total_needed - 1] >=
1230 leaf_range(root)) {
1231 total_found = 0;
1232 info->extent_tree_prealloc_nr = total_found;
1233 }
Chris Masonf2458e12007-04-25 15:52:25 -04001234 while(test_block < ins->objectid + ins->offset &&
1235 total_found < total_needed) {
1236 nr = total_needed - total_found - 1;
1237 BUG_ON(nr < 0);
Chris Masoncd1bc462007-04-27 10:08:34 -04001238 info->extent_tree_prealloc[nr] = test_block;
Chris Masonf2458e12007-04-25 15:52:25 -04001239 total_found++;
1240 test_block++;
1241 }
1242 if (total_found < total_needed) {
1243 search_start = test_block;
Chris Masonbe744172007-05-06 10:15:01 -04001244 goto new_group;
Chris Masonf2458e12007-04-25 15:52:25 -04001245 }
Chris Masoncd1bc462007-04-27 10:08:34 -04001246 info->extent_tree_prealloc_nr = total_found;
Chris Masonf2458e12007-04-25 15:52:25 -04001247 }
Chris Masone37c9e62007-05-09 20:13:14 -04001248 if (!data) {
Chris Mason5276aed2007-06-11 21:33:38 -04001249 block_group = btrfs_lookup_block_group(info, ins->objectid);
Chris Masone37c9e62007-05-09 20:13:14 -04001250 if (block_group) {
1251 if (fill_prealloc)
1252 block_group->last_prealloc =
1253 info->extent_tree_prealloc[total_needed-1];
1254 else
1255 trans->block_group = block_group;
1256 }
Chris Masoncd1bc462007-04-27 10:08:34 -04001257 }
Chris Mason037e6392007-03-07 11:50:24 -05001258 ins->offset = num_blocks;
Chris Mason5caf2a02007-04-02 11:20:42 -04001259 btrfs_free_path(path);
Chris Masonfec577f2007-02-26 10:40:21 -05001260 return 0;
Chris Masonbe744172007-05-06 10:15:01 -04001261
1262new_group:
Chris Mason3e1ad542007-05-07 20:03:49 -04001263 if (search_start + num_blocks >= search_end) {
Chris Masonbe744172007-05-06 10:15:01 -04001264 search_start = orig_search_start;
Chris Masonfbdc7622007-05-30 10:22:12 -04001265 if (full_scan) {
1266 ret = -ENOSPC;
1267 goto error;
1268 }
1269 if (wrapped)
1270 full_scan = 1;
1271 else
1272 wrapped = 1;
Chris Masonbe744172007-05-06 10:15:01 -04001273 }
Chris Mason5276aed2007-06-11 21:33:38 -04001274 block_group = btrfs_lookup_block_group(info, search_start);
Chris Masonfbdc7622007-05-30 10:22:12 -04001275 cond_resched();
Chris Masonbe744172007-05-06 10:15:01 -04001276 if (!full_scan)
1277 block_group = btrfs_find_block_group(root, block_group,
Chris Masonde428b62007-05-18 13:28:27 -04001278 search_start, data, 0);
Chris Masonbe744172007-05-06 10:15:01 -04001279 goto check_failed;
1280
Chris Mason0f70abe2007-02-28 16:46:22 -05001281error:
Chris Mason5caf2a02007-04-02 11:20:42 -04001282 btrfs_release_path(root, path);
1283 btrfs_free_path(path);
Chris Mason0f70abe2007-02-28 16:46:22 -05001284 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05001285}
Chris Masonfec577f2007-02-26 10:40:21 -05001286/*
Chris Masonfec577f2007-02-26 10:40:21 -05001287 * finds a free extent and does all the dirty work required for allocation
1288 * returns the key for the extent through ins, and a tree buffer for
1289 * the first block of the extent through buf.
1290 *
1291 * returns 0 if everything worked, non-zero otherwise.
1292 */
Chris Mason4d775672007-04-20 20:23:12 -04001293int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
1294 struct btrfs_root *root, u64 owner,
Chris Masonfbdc7622007-05-30 10:22:12 -04001295 u64 num_blocks, u64 hint_block,
Chris Masonbe08c1b2007-05-03 09:06:49 -04001296 u64 search_end, struct btrfs_key *ins, int data)
Chris Masonfec577f2007-02-26 10:40:21 -05001297{
1298 int ret;
1299 int pending_ret;
Chris Mason1261ec42007-03-20 20:35:03 -04001300 u64 super_blocks_used;
Chris Masonfbdc7622007-05-30 10:22:12 -04001301 u64 search_start = 0;
Chris Masonf2654de2007-06-26 12:20:46 -04001302 u64 exclude_start = 0;
1303 u64 exclude_nr = 0;
Chris Mason1261ec42007-03-20 20:35:03 -04001304 struct btrfs_fs_info *info = root->fs_info;
1305 struct btrfs_root *extent_root = info->extent_root;
Chris Mason234b63a2007-03-13 10:46:10 -04001306 struct btrfs_extent_item extent_item;
Chris Masonf2458e12007-04-25 15:52:25 -04001307 struct btrfs_key prealloc_key;
Chris Mason037e6392007-03-07 11:50:24 -05001308
Chris Masoncf27e1e2007-03-13 09:49:06 -04001309 btrfs_set_extent_refs(&extent_item, 1);
Chris Mason4d775672007-04-20 20:23:12 -04001310 btrfs_set_extent_owner(&extent_item, owner);
Chris Masonfec577f2007-02-26 10:40:21 -05001311
Chris Mason037e6392007-03-07 11:50:24 -05001312 if (root == extent_root) {
Chris Masonf2458e12007-04-25 15:52:25 -04001313 int nr;
1314 BUG_ON(info->extent_tree_prealloc_nr == 0);
Chris Mason037e6392007-03-07 11:50:24 -05001315 BUG_ON(num_blocks != 1);
Chris Mason037e6392007-03-07 11:50:24 -05001316 ins->offset = 1;
Chris Masonf2458e12007-04-25 15:52:25 -04001317 info->extent_tree_prealloc_nr--;
1318 nr = info->extent_tree_prealloc_nr;
1319 ins->objectid = info->extent_tree_prealloc[nr];
1320 info->extent_tree_insert[info->extent_tree_insert_nr++] =
1321 ins->objectid;
Chris Mason9078a3e2007-04-26 16:46:15 -04001322 ret = update_block_group(trans, root,
Chris Mason1e2677e2007-05-29 16:52:18 -04001323 ins->objectid, ins->offset, 1, 0, 0);
Chris Mason9078a3e2007-04-26 16:46:15 -04001324 BUG_ON(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05001325 return 0;
1326 }
Chris Masone37c9e62007-05-09 20:13:14 -04001327
1328 /*
1329 * if we're doing a data allocation, preallocate room in the
1330 * extent tree first. This way the extent tree blocks end up
1331 * in the correct block group.
1332 */
1333 if (data) {
Chris Masonde428b62007-05-18 13:28:27 -04001334 ret = find_free_extent(trans, root, 0, 0,
Chris Masonf2654de2007-06-26 12:20:46 -04001335 search_end, 0, &prealloc_key, 0, 0, 0);
Chris Masonccd467d2007-06-28 15:57:36 -04001336 BUG_ON(ret);
Chris Mason54aa1f42007-06-22 14:16:25 -04001337 if (ret)
1338 return ret;
Chris Masonf2654de2007-06-26 12:20:46 -04001339 exclude_nr = info->extent_tree_prealloc_nr;
1340 exclude_start = info->extent_tree_prealloc[exclude_nr - 1];
Chris Masone37c9e62007-05-09 20:13:14 -04001341 }
Chris Masonfec577f2007-02-26 10:40:21 -05001342
Chris Masonf2654de2007-06-26 12:20:46 -04001343 /* do the real allocation */
1344 ret = find_free_extent(trans, root, num_blocks, search_start,
1345 search_end, hint_block, ins,
1346 exclude_start, exclude_nr, data);
Chris Masonccd467d2007-06-28 15:57:36 -04001347 BUG_ON(ret);
Chris Masonf2654de2007-06-26 12:20:46 -04001348 if (ret)
1349 return ret;
1350
Chris Masone37c9e62007-05-09 20:13:14 -04001351 /*
1352 * if we're doing a metadata allocation, preallocate space in the
1353 * extent tree second. This way, we don't create a tiny hole
1354 * in the allocation map between any unused preallocation blocks
1355 * and the metadata block we're actually allocating. On disk,
1356 * it'll go:
1357 * [block we've allocated], [used prealloc 1], [ unused prealloc ]
1358 * The unused prealloc will get reused the next time around.
1359 */
1360 if (!data) {
Chris Masonf2654de2007-06-26 12:20:46 -04001361 exclude_start = ins->objectid;
1362 exclude_nr = ins->offset;
Chris Masonccd467d2007-06-28 15:57:36 -04001363 hint_block = exclude_start + exclude_nr;
Chris Masone37c9e62007-05-09 20:13:14 -04001364 ret = find_free_extent(trans, root, 0, search_start,
Chris Masonfbdc7622007-05-30 10:22:12 -04001365 search_end, hint_block,
Chris Masonf2654de2007-06-26 12:20:46 -04001366 &prealloc_key, exclude_start,
1367 exclude_nr, 0);
Chris Masonccd467d2007-06-28 15:57:36 -04001368 BUG_ON(ret);
Chris Masonf2654de2007-06-26 12:20:46 -04001369 if (ret)
1370 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -04001371 }
Chris Masonf2458e12007-04-25 15:52:25 -04001372
Chris Mason4b52dff2007-06-26 10:06:50 -04001373 super_blocks_used = btrfs_super_blocks_used(&info->super_copy);
1374 btrfs_set_super_blocks_used(&info->super_copy, super_blocks_used +
Chris Mason1261ec42007-03-20 20:35:03 -04001375 num_blocks);
Chris Masone089f052007-03-16 16:20:31 -04001376 ret = btrfs_insert_item(trans, extent_root, ins, &extent_item,
1377 sizeof(extent_item));
Chris Mason037e6392007-03-07 11:50:24 -05001378
Chris Masonccd467d2007-06-28 15:57:36 -04001379 BUG_ON(ret);
Chris Masone089f052007-03-16 16:20:31 -04001380 finish_current_insert(trans, extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -04001381 pending_ret = del_pending_extents(trans, extent_root);
Chris Masone37c9e62007-05-09 20:13:14 -04001382 if (ret) {
Chris Mason037e6392007-03-07 11:50:24 -05001383 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -04001384 }
1385 if (pending_ret) {
Chris Mason037e6392007-03-07 11:50:24 -05001386 return pending_ret;
Chris Masone37c9e62007-05-09 20:13:14 -04001387 }
Chris Mason1e2677e2007-05-29 16:52:18 -04001388 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0,
1389 data);
Chris Masonfabb5682007-06-07 22:13:21 -04001390 BUG_ON(ret);
Chris Mason037e6392007-03-07 11:50:24 -05001391 return 0;
Chris Masonfec577f2007-02-26 10:40:21 -05001392}
1393
1394/*
1395 * helper function to allocate a block for a given tree
1396 * returns the tree buffer or NULL.
1397 */
Chris Masone20d96d2007-03-22 12:13:20 -04001398struct buffer_head *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Chris Mason31f3c992007-04-30 15:25:45 -04001399 struct btrfs_root *root, u64 hint)
Chris Masonfec577f2007-02-26 10:40:21 -05001400{
Chris Masone2fa7222007-03-12 16:22:34 -04001401 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -05001402 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -04001403 struct buffer_head *buf;
Chris Masonfec577f2007-02-26 10:40:21 -05001404
Chris Mason4d775672007-04-20 20:23:12 -04001405 ret = btrfs_alloc_extent(trans, root, root->root_key.objectid,
Chris Masonde428b62007-05-18 13:28:27 -04001406 1, hint, (unsigned long)-1, &ins, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05001407 if (ret) {
Chris Mason54aa1f42007-06-22 14:16:25 -04001408 BUG_ON(ret > 0);
1409 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05001410 }
Chris Masond98237b2007-03-28 13:57:48 -04001411 buf = btrfs_find_create_tree_block(root, ins.objectid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001412 if (!buf) {
1413 btrfs_free_extent(trans, root, ins.objectid, 1, 0);
1414 return ERR_PTR(-ENOMEM);
1415 }
Chris Masondf2ce342007-03-23 11:00:45 -04001416 set_buffer_uptodate(buf);
Chris Mason090d1872007-05-01 08:53:32 -04001417 set_buffer_checked(buf);
Chris Mason7c4452b2007-04-28 09:29:35 -04001418 set_radix_bit(&trans->transaction->dirty_pages, buf->b_page->index);
Chris Masonfec577f2007-02-26 10:40:21 -05001419 return buf;
1420}
Chris Masona28ec192007-03-06 20:08:01 -05001421
Chris Mason6407bf62007-03-27 06:33:00 -04001422static int drop_leaf_ref(struct btrfs_trans_handle *trans,
1423 struct btrfs_root *root, struct buffer_head *cur)
1424{
1425 struct btrfs_disk_key *key;
1426 struct btrfs_leaf *leaf;
1427 struct btrfs_file_extent_item *fi;
1428 int i;
1429 int nritems;
1430 int ret;
1431
1432 BUG_ON(!btrfs_is_leaf(btrfs_buffer_node(cur)));
1433 leaf = btrfs_buffer_leaf(cur);
1434 nritems = btrfs_header_nritems(&leaf->header);
1435 for (i = 0; i < nritems; i++) {
Chris Mason3a686372007-05-24 13:35:57 -04001436 u64 disk_blocknr;
Chris Mason6407bf62007-03-27 06:33:00 -04001437 key = &leaf->items[i].key;
1438 if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY)
1439 continue;
1440 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
Chris Mason236454d2007-04-19 13:37:44 -04001441 if (btrfs_file_extent_type(fi) == BTRFS_FILE_EXTENT_INLINE)
1442 continue;
Chris Mason6407bf62007-03-27 06:33:00 -04001443 /*
1444 * FIXME make sure to insert a trans record that
1445 * repeats the snapshot del on crash
1446 */
Chris Mason3a686372007-05-24 13:35:57 -04001447 disk_blocknr = btrfs_file_extent_disk_blocknr(fi);
1448 if (disk_blocknr == 0)
1449 continue;
1450 ret = btrfs_free_extent(trans, root, disk_blocknr,
Chris Mason6407bf62007-03-27 06:33:00 -04001451 btrfs_file_extent_disk_num_blocks(fi),
1452 0);
1453 BUG_ON(ret);
1454 }
1455 return 0;
1456}
1457
Chris Masone0115992007-06-19 16:23:05 -04001458static void reada_walk_down(struct btrfs_root *root,
1459 struct btrfs_node *node)
1460{
1461 int i;
1462 u32 nritems;
1463 u64 blocknr;
1464 int ret;
1465 u32 refs;
1466
1467 nritems = btrfs_header_nritems(&node->header);
1468 for (i = 0; i < nritems; i++) {
1469 blocknr = btrfs_node_blockptr(node, i);
1470 ret = lookup_extent_ref(NULL, root, blocknr, 1, &refs);
1471 BUG_ON(ret);
1472 if (refs != 1)
1473 continue;
1474 ret = readahead_tree_block(root, blocknr);
1475 if (ret)
1476 break;
1477 }
1478}
1479
Chris Mason9aca1d52007-03-13 11:09:37 -04001480/*
1481 * helper function for drop_snapshot, this walks down the tree dropping ref
1482 * counts as it goes.
1483 */
Chris Masone089f052007-03-16 16:20:31 -04001484static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
1485 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05001486{
Chris Masone20d96d2007-03-22 12:13:20 -04001487 struct buffer_head *next;
1488 struct buffer_head *cur;
Chris Mason20524f02007-03-10 06:35:47 -05001489 u64 blocknr;
1490 int ret;
1491 u32 refs;
1492
Chris Mason5caf2a02007-04-02 11:20:42 -04001493 WARN_ON(*level < 0);
1494 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Masonb18c6682007-04-17 13:26:50 -04001495 ret = lookup_extent_ref(trans, root, bh_blocknr(path->nodes[*level]),
Chris Mason6407bf62007-03-27 06:33:00 -04001496 1, &refs);
Chris Mason20524f02007-03-10 06:35:47 -05001497 BUG_ON(ret);
1498 if (refs > 1)
1499 goto out;
Chris Masone0115992007-06-19 16:23:05 -04001500
Chris Mason9aca1d52007-03-13 11:09:37 -04001501 /*
1502 * walk down to the last node level and free all the leaves
1503 */
Chris Mason6407bf62007-03-27 06:33:00 -04001504 while(*level >= 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001505 WARN_ON(*level < 0);
1506 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason20524f02007-03-10 06:35:47 -05001507 cur = path->nodes[*level];
Chris Masone0115992007-06-19 16:23:05 -04001508
1509 if (*level > 0 && path->slots[*level] == 0)
1510 reada_walk_down(root, btrfs_buffer_node(cur));
1511
Chris Mason2c90e5d2007-04-02 10:50:19 -04001512 if (btrfs_header_level(btrfs_buffer_header(cur)) != *level)
1513 WARN_ON(1);
Chris Masone0115992007-06-19 16:23:05 -04001514
Chris Mason7518a232007-03-12 12:01:18 -04001515 if (path->slots[*level] >=
Chris Masone20d96d2007-03-22 12:13:20 -04001516 btrfs_header_nritems(btrfs_buffer_header(cur)))
Chris Mason20524f02007-03-10 06:35:47 -05001517 break;
Chris Mason6407bf62007-03-27 06:33:00 -04001518 if (*level == 0) {
1519 ret = drop_leaf_ref(trans, root, cur);
1520 BUG_ON(ret);
1521 break;
1522 }
Chris Masone20d96d2007-03-22 12:13:20 -04001523 blocknr = btrfs_node_blockptr(btrfs_buffer_node(cur),
1524 path->slots[*level]);
Chris Masonb18c6682007-04-17 13:26:50 -04001525 ret = lookup_extent_ref(trans, root, blocknr, 1, &refs);
Chris Mason6407bf62007-03-27 06:33:00 -04001526 BUG_ON(ret);
1527 if (refs != 1) {
Chris Mason20524f02007-03-10 06:35:47 -05001528 path->slots[*level]++;
Chris Masone089f052007-03-16 16:20:31 -04001529 ret = btrfs_free_extent(trans, root, blocknr, 1, 1);
Chris Mason20524f02007-03-10 06:35:47 -05001530 BUG_ON(ret);
1531 continue;
1532 }
Chris Mason20524f02007-03-10 06:35:47 -05001533 next = read_tree_block(root, blocknr);
Chris Mason5caf2a02007-04-02 11:20:42 -04001534 WARN_ON(*level <= 0);
Chris Mason83e15a22007-03-12 09:03:27 -04001535 if (path->nodes[*level-1])
Chris Mason234b63a2007-03-13 10:46:10 -04001536 btrfs_block_release(root, path->nodes[*level-1]);
Chris Mason20524f02007-03-10 06:35:47 -05001537 path->nodes[*level-1] = next;
Chris Masone20d96d2007-03-22 12:13:20 -04001538 *level = btrfs_header_level(btrfs_buffer_header(next));
Chris Mason20524f02007-03-10 06:35:47 -05001539 path->slots[*level] = 0;
1540 }
1541out:
Chris Mason5caf2a02007-04-02 11:20:42 -04001542 WARN_ON(*level < 0);
1543 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason6407bf62007-03-27 06:33:00 -04001544 ret = btrfs_free_extent(trans, root,
Chris Mason7eccb902007-04-11 15:53:25 -04001545 bh_blocknr(path->nodes[*level]), 1, 1);
Chris Mason234b63a2007-03-13 10:46:10 -04001546 btrfs_block_release(root, path->nodes[*level]);
Chris Mason20524f02007-03-10 06:35:47 -05001547 path->nodes[*level] = NULL;
1548 *level += 1;
1549 BUG_ON(ret);
1550 return 0;
1551}
1552
Chris Mason9aca1d52007-03-13 11:09:37 -04001553/*
1554 * helper for dropping snapshots. This walks back up the tree in the path
1555 * to find the first node higher up where we haven't yet gone through
1556 * all the slots
1557 */
Chris Masone089f052007-03-16 16:20:31 -04001558static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root
1559 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05001560{
1561 int i;
1562 int slot;
1563 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04001564 struct btrfs_root_item *root_item = &root->root_item;
1565
Chris Mason234b63a2007-03-13 10:46:10 -04001566 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Mason20524f02007-03-10 06:35:47 -05001567 slot = path->slots[i];
Chris Masone20d96d2007-03-22 12:13:20 -04001568 if (slot < btrfs_header_nritems(
1569 btrfs_buffer_header(path->nodes[i])) - 1) {
Chris Mason9f3a7422007-08-07 15:52:19 -04001570 struct btrfs_node *node;
1571 node = btrfs_buffer_node(path->nodes[i]);
Chris Mason20524f02007-03-10 06:35:47 -05001572 path->slots[i]++;
1573 *level = i;
Chris Mason9f3a7422007-08-07 15:52:19 -04001574 WARN_ON(*level == 0);
1575 memcpy(&root_item->drop_progress,
1576 &node->ptrs[path->slots[i]].key,
1577 sizeof(root_item->drop_progress));
1578 root_item->drop_level = i;
Chris Mason20524f02007-03-10 06:35:47 -05001579 return 0;
1580 } else {
Chris Masone089f052007-03-16 16:20:31 -04001581 ret = btrfs_free_extent(trans, root,
Chris Mason7eccb902007-04-11 15:53:25 -04001582 bh_blocknr(path->nodes[*level]),
Chris Masone089f052007-03-16 16:20:31 -04001583 1, 1);
Chris Mason6407bf62007-03-27 06:33:00 -04001584 BUG_ON(ret);
Chris Mason234b63a2007-03-13 10:46:10 -04001585 btrfs_block_release(root, path->nodes[*level]);
Chris Mason83e15a22007-03-12 09:03:27 -04001586 path->nodes[*level] = NULL;
Chris Mason20524f02007-03-10 06:35:47 -05001587 *level = i + 1;
Chris Mason20524f02007-03-10 06:35:47 -05001588 }
1589 }
1590 return 1;
1591}
1592
Chris Mason9aca1d52007-03-13 11:09:37 -04001593/*
1594 * drop the reference count on the tree rooted at 'snap'. This traverses
1595 * the tree freeing any blocks that have a ref count of zero after being
1596 * decremented.
1597 */
Chris Masone089f052007-03-16 16:20:31 -04001598int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason9f3a7422007-08-07 15:52:19 -04001599 *root)
Chris Mason20524f02007-03-10 06:35:47 -05001600{
Chris Mason3768f362007-03-13 16:47:54 -04001601 int ret = 0;
Chris Mason9aca1d52007-03-13 11:09:37 -04001602 int wret;
Chris Mason20524f02007-03-10 06:35:47 -05001603 int level;
Chris Mason5caf2a02007-04-02 11:20:42 -04001604 struct btrfs_path *path;
Chris Mason20524f02007-03-10 06:35:47 -05001605 int i;
1606 int orig_level;
Chris Mason9f3a7422007-08-07 15:52:19 -04001607 int num_walks = 0;
1608 struct btrfs_root_item *root_item = &root->root_item;
Chris Mason20524f02007-03-10 06:35:47 -05001609
Chris Mason5caf2a02007-04-02 11:20:42 -04001610 path = btrfs_alloc_path();
1611 BUG_ON(!path);
Chris Mason20524f02007-03-10 06:35:47 -05001612
Chris Mason9f3a7422007-08-07 15:52:19 -04001613 level = btrfs_header_level(btrfs_buffer_header(root->node));
Chris Mason20524f02007-03-10 06:35:47 -05001614 orig_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04001615 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
1616 path->nodes[level] = root->node;
1617 path->slots[level] = 0;
1618 } else {
1619 struct btrfs_key key;
1620 struct btrfs_disk_key *found_key;
1621 struct btrfs_node *node;
1622 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
1623 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1624 if (ret < 0) {
1625 ret = wret;
1626 goto out;
1627 }
1628 level = root_item->drop_level;
1629 node = btrfs_buffer_node(path->nodes[level]);
1630 found_key = &node->ptrs[path->slots[level]].key;
1631 WARN_ON(memcmp(found_key, &root_item->drop_progress,
1632 sizeof(*found_key)));
1633 }
Chris Mason20524f02007-03-10 06:35:47 -05001634 while(1) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001635 wret = walk_down_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04001636 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05001637 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04001638 if (wret < 0)
1639 ret = wret;
1640
Chris Mason5caf2a02007-04-02 11:20:42 -04001641 wret = walk_up_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04001642 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05001643 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04001644 if (wret < 0)
1645 ret = wret;
Chris Mason9f3a7422007-08-07 15:52:19 -04001646 num_walks++;
1647 if (num_walks > 10) {
1648 struct btrfs_key key;
1649 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
1650 ret = -EAGAIN;
1651 get_bh(root->node);
1652 break;
1653 }
Chris Mason20524f02007-03-10 06:35:47 -05001654 }
Chris Mason83e15a22007-03-12 09:03:27 -04001655 for (i = 0; i <= orig_level; i++) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001656 if (path->nodes[i]) {
1657 btrfs_block_release(root, path->nodes[i]);
Chris Mason83e15a22007-03-12 09:03:27 -04001658 }
Chris Mason20524f02007-03-10 06:35:47 -05001659 }
Chris Mason9f3a7422007-08-07 15:52:19 -04001660out:
Chris Mason5caf2a02007-04-02 11:20:42 -04001661 btrfs_free_path(path);
Chris Mason9aca1d52007-03-13 11:09:37 -04001662 return ret;
Chris Mason20524f02007-03-10 06:35:47 -05001663}
Chris Mason9078a3e2007-04-26 16:46:15 -04001664
Chris Masonbe744172007-05-06 10:15:01 -04001665static int free_block_group_radix(struct radix_tree_root *radix)
Chris Mason9078a3e2007-04-26 16:46:15 -04001666{
1667 int ret;
1668 struct btrfs_block_group_cache *cache[8];
1669 int i;
1670
1671 while(1) {
Chris Masonbe744172007-05-06 10:15:01 -04001672 ret = radix_tree_gang_lookup(radix, (void **)cache, 0,
Chris Mason9078a3e2007-04-26 16:46:15 -04001673 ARRAY_SIZE(cache));
1674 if (!ret)
1675 break;
1676 for (i = 0; i < ret; i++) {
Chris Masonbe744172007-05-06 10:15:01 -04001677 radix_tree_delete(radix, cache[i]->key.objectid +
Chris Mason9078a3e2007-04-26 16:46:15 -04001678 cache[i]->key.offset - 1);
1679 kfree(cache[i]);
1680 }
1681 }
1682 return 0;
1683}
1684
Chris Masonbe744172007-05-06 10:15:01 -04001685int btrfs_free_block_groups(struct btrfs_fs_info *info)
1686{
1687 int ret;
1688 int ret2;
Chris Masone37c9e62007-05-09 20:13:14 -04001689 unsigned long gang[16];
1690 int i;
Chris Masonbe744172007-05-06 10:15:01 -04001691
1692 ret = free_block_group_radix(&info->block_group_radix);
1693 ret2 = free_block_group_radix(&info->block_group_data_radix);
1694 if (ret)
1695 return ret;
1696 if (ret2)
1697 return ret2;
Chris Masone37c9e62007-05-09 20:13:14 -04001698
1699 while(1) {
1700 ret = find_first_radix_bit(&info->extent_map_radix,
1701 gang, 0, ARRAY_SIZE(gang));
1702 if (!ret)
1703 break;
1704 for (i = 0; i < ret; i++) {
1705 clear_radix_bit(&info->extent_map_radix, gang[i]);
1706 }
1707 }
Chris Masonbe744172007-05-06 10:15:01 -04001708 return 0;
1709}
1710
Chris Mason9078a3e2007-04-26 16:46:15 -04001711int btrfs_read_block_groups(struct btrfs_root *root)
1712{
1713 struct btrfs_path *path;
1714 int ret;
1715 int err = 0;
1716 struct btrfs_block_group_item *bi;
1717 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04001718 struct btrfs_fs_info *info = root->fs_info;
1719 struct radix_tree_root *radix;
Chris Mason9078a3e2007-04-26 16:46:15 -04001720 struct btrfs_key key;
1721 struct btrfs_key found_key;
1722 struct btrfs_leaf *leaf;
Chris Mason84f54cf2007-06-12 07:43:08 -04001723 u64 group_size_blocks;
Chris Mason31f3c992007-04-30 15:25:45 -04001724 u64 used;
Chris Mason9078a3e2007-04-26 16:46:15 -04001725
Chris Mason84f54cf2007-06-12 07:43:08 -04001726 group_size_blocks = BTRFS_BLOCK_GROUP_SIZE >>
1727 root->fs_info->sb->s_blocksize_bits;
Chris Masonbe744172007-05-06 10:15:01 -04001728 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04001729 key.objectid = 0;
1730 key.offset = group_size_blocks;
1731 key.flags = 0;
1732 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
1733
1734 path = btrfs_alloc_path();
1735 if (!path)
1736 return -ENOMEM;
1737
1738 while(1) {
Chris Masonbe744172007-05-06 10:15:01 -04001739 ret = btrfs_search_slot(NULL, info->extent_root,
Chris Mason9078a3e2007-04-26 16:46:15 -04001740 &key, path, 0, 0);
1741 if (ret != 0) {
1742 err = ret;
1743 break;
1744 }
1745 leaf = btrfs_buffer_leaf(path->nodes[0]);
1746 btrfs_disk_key_to_cpu(&found_key,
1747 &leaf->items[path->slots[0]].key);
1748 cache = kmalloc(sizeof(*cache), GFP_NOFS);
1749 if (!cache) {
1750 err = -1;
1751 break;
1752 }
Chris Mason3e1ad542007-05-07 20:03:49 -04001753
Chris Mason9078a3e2007-04-26 16:46:15 -04001754 bi = btrfs_item_ptr(leaf, path->slots[0],
1755 struct btrfs_block_group_item);
Chris Mason1e2677e2007-05-29 16:52:18 -04001756 if (bi->flags & BTRFS_BLOCK_GROUP_DATA) {
1757 radix = &info->block_group_data_radix;
1758 cache->data = 1;
1759 } else {
1760 radix = &info->block_group_radix;
1761 cache->data = 0;
1762 }
1763
Chris Mason9078a3e2007-04-26 16:46:15 -04001764 memcpy(&cache->item, bi, sizeof(*bi));
1765 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Mason31f3c992007-04-30 15:25:45 -04001766 cache->last_alloc = cache->key.objectid;
1767 cache->first_free = cache->key.objectid;
Chris Masone37c9e62007-05-09 20:13:14 -04001768 cache->last_prealloc = cache->key.objectid;
Chris Masonbe744172007-05-06 10:15:01 -04001769 cache->pinned = 0;
Chris Masone37c9e62007-05-09 20:13:14 -04001770 cache->cached = 0;
1771
Chris Mason3e1ad542007-05-07 20:03:49 -04001772 cache->radix = radix;
1773
Chris Mason9078a3e2007-04-26 16:46:15 -04001774 key.objectid = found_key.objectid + found_key.offset;
1775 btrfs_release_path(root, path);
Chris Masonbe744172007-05-06 10:15:01 -04001776 ret = radix_tree_insert(radix, found_key.objectid +
Chris Mason9078a3e2007-04-26 16:46:15 -04001777 found_key.offset - 1,
1778 (void *)cache);
1779 BUG_ON(ret);
Chris Mason31f3c992007-04-30 15:25:45 -04001780 used = btrfs_block_group_used(bi);
Chris Mason84f54cf2007-06-12 07:43:08 -04001781 if (used < div_factor(key.offset, 8)) {
Chris Masonbe744172007-05-06 10:15:01 -04001782 radix_tree_tag_set(radix, found_key.objectid +
Chris Mason31f3c992007-04-30 15:25:45 -04001783 found_key.offset - 1,
1784 BTRFS_BLOCK_GROUP_AVAIL);
1785 }
Chris Mason9078a3e2007-04-26 16:46:15 -04001786 if (key.objectid >=
Chris Mason4b52dff2007-06-26 10:06:50 -04001787 btrfs_super_total_blocks(&info->super_copy))
Chris Mason9078a3e2007-04-26 16:46:15 -04001788 break;
1789 }
1790
1791 btrfs_free_path(path);
1792 return 0;
1793}