blob: 975a0eb9241b989e95b10e650b5964aeaf066c8d [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 Masonde428b62007-05-18 13:28:27 -0400103 if (ret == 0) {
Chris Masone37c9e62007-05-09 20:13:14 -0400104 continue;
Chris Masonde428b62007-05-18 13:28:27 -0400105 } else {
Chris Masone37c9e62007-05-09 20:13:14 -0400106 if (found) {
107 hole_size = block_group->key.objectid +
108 block_group->key.offset - last;
109 } else {
110 last = block_group->key.objectid;
111 hole_size = block_group->key.offset;
112 }
113 for (i = 0; i < hole_size; i++) {
114 set_radix_bit(extent_radix,
115 last + i);
116 }
117 break;
118 }
119 }
120 btrfs_disk_key_to_cpu(&key, &leaf->items[slot].key);
121 if (key.objectid >= block_group->key.objectid +
122 block_group->key.offset) {
123 if (found) {
124 hole_size = block_group->key.objectid +
125 block_group->key.offset - last;
126 } else {
127 last = block_group->key.objectid;
128 hole_size = block_group->key.offset;
129 }
130 for (i = 0; i < hole_size; i++) {
131 set_radix_bit(extent_radix, last + i);
132 }
133 break;
134 }
135 if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) {
136 if (!found) {
137 last = key.objectid + key.offset;
138 found = 1;
139 } else {
140 hole_size = key.objectid - last;
141 for (i = 0; i < hole_size; i++) {
142 set_radix_bit(extent_radix, last + i);
143 }
144 last = key.objectid + key.offset;
145 }
146 }
147 path->slots[0]++;
148 }
149
150 block_group->cached = 1;
151 btrfs_free_path(path);
152 return 0;
153}
154
Chris Mason5276aed2007-06-11 21:33:38 -0400155struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
156 btrfs_fs_info *info,
157 u64 blocknr)
Chris Masonbe744172007-05-06 10:15:01 -0400158{
159 struct btrfs_block_group_cache *block_group;
160 int ret;
161
162 ret = radix_tree_gang_lookup(&info->block_group_radix,
163 (void **)&block_group,
164 blocknr, 1);
165 if (ret) {
Chris Mason3e1ad542007-05-07 20:03:49 -0400166 if (block_group->key.objectid <= blocknr && blocknr <=
Chris Masonbe744172007-05-06 10:15:01 -0400167 block_group->key.objectid + block_group->key.offset)
168 return block_group;
169 }
170 ret = radix_tree_gang_lookup(&info->block_group_data_radix,
171 (void **)&block_group,
172 blocknr, 1);
173 if (ret) {
Chris Mason3e1ad542007-05-07 20:03:49 -0400174 if (block_group->key.objectid <= blocknr && blocknr <=
Chris Masonbe744172007-05-06 10:15:01 -0400175 block_group->key.objectid + block_group->key.offset)
176 return block_group;
177 }
Chris Masonbe744172007-05-06 10:15:01 -0400178 return NULL;
179}
180
Chris Masone37c9e62007-05-09 20:13:14 -0400181static u64 leaf_range(struct btrfs_root *root)
182{
183 u64 size = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason84f54cf2007-06-12 07:43:08 -0400184 do_div(size, sizeof(struct btrfs_extent_item) +
185 sizeof(struct btrfs_item));
Chris Masone37c9e62007-05-09 20:13:14 -0400186 return size;
187}
188
189static u64 find_search_start(struct btrfs_root *root,
190 struct btrfs_block_group_cache **cache_ret,
191 u64 search_start, int num)
192{
193 unsigned long gang[8];
194 int ret;
195 struct btrfs_block_group_cache *cache = *cache_ret;
196 u64 last = max(search_start, cache->key.objectid);
197
198 if (cache->data)
199 goto out;
200 if (num > 1) {
201 last = max(last, cache->last_prealloc);
202 }
203again:
204 cache_block_group(root, cache);
205 while(1) {
206 ret = find_first_radix_bit(&root->fs_info->extent_map_radix,
207 gang, last, ARRAY_SIZE(gang));
208 if (!ret)
209 goto out;
210 last = gang[ret-1] + 1;
211 if (num > 1) {
212 if (ret != ARRAY_SIZE(gang)) {
213 goto new_group;
214 }
215 if (gang[ret-1] - gang[0] > leaf_range(root)) {
216 continue;
217 }
218 }
219 if (gang[0] >= cache->key.objectid + cache->key.offset) {
220 goto new_group;
221 }
222 return gang[0];
223 }
224out:
225 return max(cache->last_alloc, search_start);
226
227new_group:
Chris Mason5276aed2007-06-11 21:33:38 -0400228 cache = btrfs_lookup_block_group(root->fs_info,
229 last + cache->key.offset - 1);
Chris Masone37c9e62007-05-09 20:13:14 -0400230 if (!cache) {
231 return max((*cache_ret)->last_alloc, search_start);
232 }
233 cache = btrfs_find_block_group(root, cache,
Chris Masonde428b62007-05-18 13:28:27 -0400234 last + cache->key.offset - 1, 0, 0);
Chris Masone37c9e62007-05-09 20:13:14 -0400235 *cache_ret = cache;
236 goto again;
237}
238
Chris Mason84f54cf2007-06-12 07:43:08 -0400239static u64 div_factor(u64 num, int factor)
240{
241 num *= factor;
242 do_div(num, 10);
243 return num;
244}
245
Chris Mason31f3c992007-04-30 15:25:45 -0400246struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
247 struct btrfs_block_group_cache
Chris Masonbe744172007-05-06 10:15:01 -0400248 *hint, u64 search_start,
Chris Masonde428b62007-05-18 13:28:27 -0400249 int data, int owner)
Chris Masoncd1bc462007-04-27 10:08:34 -0400250{
251 struct btrfs_block_group_cache *cache[8];
Chris Mason31f3c992007-04-30 15:25:45 -0400252 struct btrfs_block_group_cache *found_group = NULL;
Chris Masoncd1bc462007-04-27 10:08:34 -0400253 struct btrfs_fs_info *info = root->fs_info;
Chris Masonbe744172007-05-06 10:15:01 -0400254 struct radix_tree_root *radix;
Chris Mason1e2677e2007-05-29 16:52:18 -0400255 struct radix_tree_root *swap_radix;
Chris Masoncd1bc462007-04-27 10:08:34 -0400256 u64 used;
Chris Mason31f3c992007-04-30 15:25:45 -0400257 u64 last = 0;
258 u64 hint_last;
Chris Masoncd1bc462007-04-27 10:08:34 -0400259 int i;
260 int ret;
Chris Mason31f3c992007-04-30 15:25:45 -0400261 int full_search = 0;
Chris Masonde428b62007-05-18 13:28:27 -0400262 int factor = 8;
Chris Mason1e2677e2007-05-29 16:52:18 -0400263 int data_swap = 0;
Chris Masonde428b62007-05-18 13:28:27 -0400264
265 if (!owner)
266 factor = 5;
Chris Masonbe744172007-05-06 10:15:01 -0400267
Chris Mason1e2677e2007-05-29 16:52:18 -0400268 if (data) {
Chris Masonbe744172007-05-06 10:15:01 -0400269 radix = &info->block_group_data_radix;
Chris Mason1e2677e2007-05-29 16:52:18 -0400270 swap_radix = &info->block_group_radix;
271 } else {
Chris Masonbe744172007-05-06 10:15:01 -0400272 radix = &info->block_group_radix;
Chris Mason1e2677e2007-05-29 16:52:18 -0400273 swap_radix = &info->block_group_data_radix;
274 }
Chris Masonbe744172007-05-06 10:15:01 -0400275
276 if (search_start) {
277 struct btrfs_block_group_cache *shint;
Chris Mason5276aed2007-06-11 21:33:38 -0400278 shint = btrfs_lookup_block_group(info, search_start);
Chris Masonbe744172007-05-06 10:15:01 -0400279 if (shint->data == data) {
280 used = btrfs_block_group_used(&shint->item);
281 if (used + shint->pinned <
Chris Mason84f54cf2007-06-12 07:43:08 -0400282 div_factor(shint->key.offset, factor)) {
Chris Masonbe744172007-05-06 10:15:01 -0400283 return shint;
284 }
285 }
286 }
287 if (hint && hint->data == data) {
Chris Mason31f3c992007-04-30 15:25:45 -0400288 used = btrfs_block_group_used(&hint->item);
Chris Mason84f54cf2007-06-12 07:43:08 -0400289 if (used + hint->pinned <
290 div_factor(hint->key.offset, factor)) {
Chris Mason31f3c992007-04-30 15:25:45 -0400291 return hint;
292 }
Chris Mason84f54cf2007-06-12 07:43:08 -0400293 if (used >= div_factor(hint->key.offset, 8)) {
Chris Masonbe744172007-05-06 10:15:01 -0400294 radix_tree_tag_clear(radix,
295 hint->key.objectid +
296 hint->key.offset - 1,
297 BTRFS_BLOCK_GROUP_AVAIL);
298 }
Chris Mason8d7be552007-05-10 11:24:42 -0400299 last = hint->key.offset * 3;
Chris Masonbe744172007-05-06 10:15:01 -0400300 if (hint->key.objectid >= last)
Chris Masone37c9e62007-05-09 20:13:14 -0400301 last = max(search_start + hint->key.offset - 1,
302 hint->key.objectid - last);
Chris Masonbe744172007-05-06 10:15:01 -0400303 else
304 last = hint->key.objectid + hint->key.offset;
Chris Mason31f3c992007-04-30 15:25:45 -0400305 hint_last = last;
306 } else {
Chris Masone37c9e62007-05-09 20:13:14 -0400307 if (hint)
308 hint_last = max(hint->key.objectid, search_start);
309 else
310 hint_last = search_start;
311
312 last = hint_last;
Chris Mason31f3c992007-04-30 15:25:45 -0400313 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400314 while(1) {
Chris Masonbe744172007-05-06 10:15:01 -0400315 ret = radix_tree_gang_lookup_tag(radix, (void **)cache,
Chris Masoncd1bc462007-04-27 10:08:34 -0400316 last, ARRAY_SIZE(cache),
Chris Mason31f3c992007-04-30 15:25:45 -0400317 BTRFS_BLOCK_GROUP_AVAIL);
Chris Masoncd1bc462007-04-27 10:08:34 -0400318 if (!ret)
319 break;
320 for (i = 0; i < ret; i++) {
Chris Masonbe08c1b2007-05-03 09:06:49 -0400321 last = cache[i]->key.objectid +
322 cache[i]->key.offset;
Chris Masoncd1bc462007-04-27 10:08:34 -0400323 used = btrfs_block_group_used(&cache[i]->item);
Chris Masonbe744172007-05-06 10:15:01 -0400324 if (used + cache[i]->pinned <
Chris Mason84f54cf2007-06-12 07:43:08 -0400325 div_factor(cache[i]->key.offset, factor)) {
Chris Mason31f3c992007-04-30 15:25:45 -0400326 found_group = cache[i];
327 goto found;
Chris Masoncd1bc462007-04-27 10:08:34 -0400328 }
Chris Mason84f54cf2007-06-12 07:43:08 -0400329 if (used >= div_factor(cache[i]->key.offset, 8)) {
Chris Masonbe744172007-05-06 10:15:01 -0400330 radix_tree_tag_clear(radix,
331 cache[i]->key.objectid +
332 cache[i]->key.offset - 1,
333 BTRFS_BLOCK_GROUP_AVAIL);
334 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400335 }
Chris Masonde428b62007-05-18 13:28:27 -0400336 cond_resched();
Chris Masoncd1bc462007-04-27 10:08:34 -0400337 }
Chris Mason31f3c992007-04-30 15:25:45 -0400338 last = hint_last;
339again:
Chris Masoncd1bc462007-04-27 10:08:34 -0400340 while(1) {
Chris Masonbe744172007-05-06 10:15:01 -0400341 ret = radix_tree_gang_lookup(radix, (void **)cache,
342 last, ARRAY_SIZE(cache));
Chris Masoncd1bc462007-04-27 10:08:34 -0400343 if (!ret)
344 break;
345 for (i = 0; i < ret; i++) {
Chris Masonbe08c1b2007-05-03 09:06:49 -0400346 last = cache[i]->key.objectid +
347 cache[i]->key.offset;
Chris Masoncd1bc462007-04-27 10:08:34 -0400348 used = btrfs_block_group_used(&cache[i]->item);
Chris Masonbe744172007-05-06 10:15:01 -0400349 if (used + cache[i]->pinned < cache[i]->key.offset) {
Chris Mason31f3c992007-04-30 15:25:45 -0400350 found_group = cache[i];
351 goto found;
Chris Masoncd1bc462007-04-27 10:08:34 -0400352 }
Chris Masonbe744172007-05-06 10:15:01 -0400353 if (used >= cache[i]->key.offset) {
354 radix_tree_tag_clear(radix,
355 cache[i]->key.objectid +
356 cache[i]->key.offset - 1,
357 BTRFS_BLOCK_GROUP_AVAIL);
358 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400359 }
Chris Masonde428b62007-05-18 13:28:27 -0400360 cond_resched();
Chris Masoncd1bc462007-04-27 10:08:34 -0400361 }
Chris Mason31f3c992007-04-30 15:25:45 -0400362 if (!full_search) {
Chris Masonbe744172007-05-06 10:15:01 -0400363 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -0400364 full_search = 1;
365 goto again;
366 }
Chris Mason1e2677e2007-05-29 16:52:18 -0400367 if (!data_swap) {
368 struct radix_tree_root *tmp = radix;
369 data_swap = 1;
370 radix = swap_radix;
371 swap_radix = tmp;
372 last = search_start;
373 goto again;
374 }
Chris Mason31f3c992007-04-30 15:25:45 -0400375 if (!found_group) {
Chris Masonbe744172007-05-06 10:15:01 -0400376 ret = radix_tree_gang_lookup(radix,
Chris Mason31f3c992007-04-30 15:25:45 -0400377 (void **)&found_group, 0, 1);
Chris Mason1e2677e2007-05-29 16:52:18 -0400378 if (ret == 0) {
379 ret = radix_tree_gang_lookup(swap_radix,
380 (void **)&found_group,
381 0, 1);
382 }
Chris Mason31f3c992007-04-30 15:25:45 -0400383 BUG_ON(ret != 1);
384 }
Chris Masonbe744172007-05-06 10:15:01 -0400385found:
Chris Mason31f3c992007-04-30 15:25:45 -0400386 return found_group;
Chris Masoncd1bc462007-04-27 10:08:34 -0400387}
388
Chris Masonb18c6682007-04-17 13:26:50 -0400389int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
390 struct btrfs_root *root,
391 u64 blocknr, u64 num_blocks)
Chris Mason02217ed2007-03-02 16:08:05 -0500392{
Chris Mason5caf2a02007-04-02 11:20:42 -0400393 struct btrfs_path *path;
Chris Mason02217ed2007-03-02 16:08:05 -0500394 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400395 struct btrfs_key key;
Chris Mason234b63a2007-03-13 10:46:10 -0400396 struct btrfs_leaf *l;
397 struct btrfs_extent_item *item;
Chris Masone2fa7222007-03-12 16:22:34 -0400398 struct btrfs_key ins;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400399 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500400
Chris Masonfbdc7622007-05-30 10:22:12 -0400401 find_free_extent(trans, root->fs_info->extent_root, 0, 0, (u64)-1, 0,
Chris Masonbe08c1b2007-05-03 09:06:49 -0400402 &ins, 0);
Chris Mason5caf2a02007-04-02 11:20:42 -0400403 path = btrfs_alloc_path();
404 BUG_ON(!path);
405 btrfs_init_path(path);
Chris Mason02217ed2007-03-02 16:08:05 -0500406 key.objectid = blocknr;
407 key.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -0400408 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Mason6407bf62007-03-27 06:33:00 -0400409 key.offset = num_blocks;
Chris Mason5caf2a02007-04-02 11:20:42 -0400410 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -0400411 0, 1);
Chris Masona429e512007-04-18 16:15:28 -0400412 if (ret != 0) {
Chris Masona28ec192007-03-06 20:08:01 -0500413 BUG();
Chris Masona429e512007-04-18 16:15:28 -0400414 }
Chris Mason02217ed2007-03-02 16:08:05 -0500415 BUG_ON(ret != 0);
Chris Mason5caf2a02007-04-02 11:20:42 -0400416 l = btrfs_buffer_leaf(path->nodes[0]);
417 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400418 refs = btrfs_extent_refs(item);
419 btrfs_set_extent_refs(item, refs + 1);
Chris Mason5caf2a02007-04-02 11:20:42 -0400420 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masona28ec192007-03-06 20:08:01 -0500421
Chris Mason5caf2a02007-04-02 11:20:42 -0400422 btrfs_release_path(root->fs_info->extent_root, path);
423 btrfs_free_path(path);
Chris Mason9f5fae22007-03-20 14:38:32 -0400424 finish_current_insert(trans, root->fs_info->extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -0400425 del_pending_extents(trans, root->fs_info->extent_root);
Chris Mason02217ed2007-03-02 16:08:05 -0500426 return 0;
427}
428
Chris Masonb18c6682007-04-17 13:26:50 -0400429static int lookup_extent_ref(struct btrfs_trans_handle *trans,
430 struct btrfs_root *root, u64 blocknr,
431 u64 num_blocks, u32 *refs)
Chris Masona28ec192007-03-06 20:08:01 -0500432{
Chris Mason5caf2a02007-04-02 11:20:42 -0400433 struct btrfs_path *path;
Chris Masona28ec192007-03-06 20:08:01 -0500434 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400435 struct btrfs_key key;
Chris Mason234b63a2007-03-13 10:46:10 -0400436 struct btrfs_leaf *l;
437 struct btrfs_extent_item *item;
Chris Mason5caf2a02007-04-02 11:20:42 -0400438
439 path = btrfs_alloc_path();
440 btrfs_init_path(path);
Chris Masona28ec192007-03-06 20:08:01 -0500441 key.objectid = blocknr;
Chris Mason6407bf62007-03-27 06:33:00 -0400442 key.offset = num_blocks;
Chris Mason62e27492007-03-15 12:56:47 -0400443 key.flags = 0;
444 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Mason5caf2a02007-04-02 11:20:42 -0400445 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -0400446 0, 0);
Chris Masona28ec192007-03-06 20:08:01 -0500447 if (ret != 0)
448 BUG();
Chris Mason5caf2a02007-04-02 11:20:42 -0400449 l = btrfs_buffer_leaf(path->nodes[0]);
450 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400451 *refs = btrfs_extent_refs(item);
Chris Mason5caf2a02007-04-02 11:20:42 -0400452 btrfs_release_path(root->fs_info->extent_root, path);
453 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -0500454 return 0;
455}
456
Chris Masonc5739bb2007-04-10 09:27:04 -0400457int btrfs_inc_root_ref(struct btrfs_trans_handle *trans,
458 struct btrfs_root *root)
459{
Chris Masonb18c6682007-04-17 13:26:50 -0400460 return btrfs_inc_extent_ref(trans, root, bh_blocknr(root->node), 1);
Chris Masonc5739bb2007-04-10 09:27:04 -0400461}
462
Chris Masone089f052007-03-16 16:20:31 -0400463int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Masone20d96d2007-03-22 12:13:20 -0400464 struct buffer_head *buf)
Chris Mason02217ed2007-03-02 16:08:05 -0500465{
466 u64 blocknr;
Chris Masone20d96d2007-03-22 12:13:20 -0400467 struct btrfs_node *buf_node;
Chris Mason6407bf62007-03-27 06:33:00 -0400468 struct btrfs_leaf *buf_leaf;
469 struct btrfs_disk_key *key;
470 struct btrfs_file_extent_item *fi;
Chris Mason02217ed2007-03-02 16:08:05 -0500471 int i;
Chris Mason6407bf62007-03-27 06:33:00 -0400472 int leaf;
473 int ret;
Chris Masona28ec192007-03-06 20:08:01 -0500474
Chris Mason3768f362007-03-13 16:47:54 -0400475 if (!root->ref_cows)
Chris Masona28ec192007-03-06 20:08:01 -0500476 return 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400477 buf_node = btrfs_buffer_node(buf);
Chris Mason6407bf62007-03-27 06:33:00 -0400478 leaf = btrfs_is_leaf(buf_node);
479 buf_leaf = btrfs_buffer_leaf(buf);
Chris Masone20d96d2007-03-22 12:13:20 -0400480 for (i = 0; i < btrfs_header_nritems(&buf_node->header); i++) {
Chris Mason6407bf62007-03-27 06:33:00 -0400481 if (leaf) {
Chris Mason3a686372007-05-24 13:35:57 -0400482 u64 disk_blocknr;
Chris Mason6407bf62007-03-27 06:33:00 -0400483 key = &buf_leaf->items[i].key;
484 if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY)
485 continue;
486 fi = btrfs_item_ptr(buf_leaf, i,
487 struct btrfs_file_extent_item);
Chris Mason236454d2007-04-19 13:37:44 -0400488 if (btrfs_file_extent_type(fi) ==
489 BTRFS_FILE_EXTENT_INLINE)
490 continue;
Chris Mason3a686372007-05-24 13:35:57 -0400491 disk_blocknr = btrfs_file_extent_disk_blocknr(fi);
492 if (disk_blocknr == 0)
493 continue;
494 ret = btrfs_inc_extent_ref(trans, root, disk_blocknr,
Chris Mason6407bf62007-03-27 06:33:00 -0400495 btrfs_file_extent_disk_num_blocks(fi));
496 BUG_ON(ret);
497 } else {
498 blocknr = btrfs_node_blockptr(buf_node, i);
Chris Masonb18c6682007-04-17 13:26:50 -0400499 ret = btrfs_inc_extent_ref(trans, root, blocknr, 1);
Chris Mason6407bf62007-03-27 06:33:00 -0400500 BUG_ON(ret);
501 }
Chris Mason02217ed2007-03-02 16:08:05 -0500502 }
503 return 0;
504}
505
Chris Mason9078a3e2007-04-26 16:46:15 -0400506static int write_one_cache_group(struct btrfs_trans_handle *trans,
507 struct btrfs_root *root,
508 struct btrfs_path *path,
509 struct btrfs_block_group_cache *cache)
510{
511 int ret;
512 int pending_ret;
513 struct btrfs_root *extent_root = root->fs_info->extent_root;
514 struct btrfs_block_group_item *bi;
515 struct btrfs_key ins;
516
Chris Masonfbdc7622007-05-30 10:22:12 -0400517 find_free_extent(trans, extent_root, 0, 0, (u64)-1, 0, &ins, 0);
Chris Mason9078a3e2007-04-26 16:46:15 -0400518 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
519 BUG_ON(ret);
520 bi = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
521 struct btrfs_block_group_item);
522 memcpy(bi, &cache->item, sizeof(*bi));
523 mark_buffer_dirty(path->nodes[0]);
524 btrfs_release_path(extent_root, path);
525
526 finish_current_insert(trans, extent_root);
527 pending_ret = del_pending_extents(trans, extent_root);
528 if (ret)
529 return ret;
530 if (pending_ret)
531 return pending_ret;
Chris Masonbe744172007-05-06 10:15:01 -0400532 if (cache->data)
533 cache->last_alloc = cache->first_free;
Chris Mason9078a3e2007-04-26 16:46:15 -0400534 return 0;
535
536}
537
Chris Masonbe744172007-05-06 10:15:01 -0400538static int write_dirty_block_radix(struct btrfs_trans_handle *trans,
539 struct btrfs_root *root,
540 struct radix_tree_root *radix)
Chris Mason9078a3e2007-04-26 16:46:15 -0400541{
542 struct btrfs_block_group_cache *cache[8];
543 int ret;
544 int err = 0;
545 int werr = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -0400546 int i;
547 struct btrfs_path *path;
548
549 path = btrfs_alloc_path();
550 if (!path)
551 return -ENOMEM;
552
553 while(1) {
554 ret = radix_tree_gang_lookup_tag(radix, (void **)cache,
555 0, ARRAY_SIZE(cache),
556 BTRFS_BLOCK_GROUP_DIRTY);
557 if (!ret)
558 break;
559 for (i = 0; i < ret; i++) {
560 radix_tree_tag_clear(radix, cache[i]->key.objectid +
561 cache[i]->key.offset - 1,
562 BTRFS_BLOCK_GROUP_DIRTY);
563 err = write_one_cache_group(trans, root,
564 path, cache[i]);
565 if (err)
566 werr = err;
567 }
568 }
569 btrfs_free_path(path);
570 return werr;
571}
572
Chris Masonbe744172007-05-06 10:15:01 -0400573int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
574 struct btrfs_root *root)
575{
576 int ret;
577 int ret2;
578 ret = write_dirty_block_radix(trans, root,
579 &root->fs_info->block_group_radix);
580 ret2 = write_dirty_block_radix(trans, root,
581 &root->fs_info->block_group_data_radix);
582 if (ret)
583 return ret;
584 if (ret2)
585 return ret2;
586 return 0;
587}
588
Chris Mason9078a3e2007-04-26 16:46:15 -0400589static int update_block_group(struct btrfs_trans_handle *trans,
590 struct btrfs_root *root,
Chris Mason1e2677e2007-05-29 16:52:18 -0400591 u64 blocknr, u64 num, int alloc, int mark_free,
592 int data)
Chris Mason9078a3e2007-04-26 16:46:15 -0400593{
594 struct btrfs_block_group_cache *cache;
595 struct btrfs_fs_info *info = root->fs_info;
596 u64 total = num;
597 u64 old_val;
598 u64 block_in_group;
Chris Masone37c9e62007-05-09 20:13:14 -0400599 u64 i;
Chris Mason1e2677e2007-05-29 16:52:18 -0400600 int ret;
Chris Mason3e1ad542007-05-07 20:03:49 -0400601
Chris Mason9078a3e2007-04-26 16:46:15 -0400602 while(total) {
Chris Mason5276aed2007-06-11 21:33:38 -0400603 cache = btrfs_lookup_block_group(info, blocknr);
Chris Mason3e1ad542007-05-07 20:03:49 -0400604 if (!cache) {
Chris Mason9078a3e2007-04-26 16:46:15 -0400605 return -1;
Chris Masoncd1bc462007-04-27 10:08:34 -0400606 }
Chris Mason9078a3e2007-04-26 16:46:15 -0400607 block_in_group = blocknr - cache->key.objectid;
608 WARN_ON(block_in_group > cache->key.offset);
Chris Mason3e1ad542007-05-07 20:03:49 -0400609 radix_tree_tag_set(cache->radix, cache->key.objectid +
Chris Masonbe744172007-05-06 10:15:01 -0400610 cache->key.offset - 1,
Chris Mason9078a3e2007-04-26 16:46:15 -0400611 BTRFS_BLOCK_GROUP_DIRTY);
612
613 old_val = btrfs_block_group_used(&cache->item);
614 num = min(total, cache->key.offset - block_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -0400615 if (alloc) {
Chris Masoncd1bc462007-04-27 10:08:34 -0400616 if (blocknr > cache->last_alloc)
617 cache->last_alloc = blocknr;
Chris Masone37c9e62007-05-09 20:13:14 -0400618 if (!cache->data) {
619 for (i = 0; i < num; i++) {
620 clear_radix_bit(&info->extent_map_radix,
621 blocknr + i);
622 }
623 }
Chris Mason1e2677e2007-05-29 16:52:18 -0400624 if (cache->data != data &&
Chris Mason84f54cf2007-06-12 07:43:08 -0400625 old_val < (cache->key.offset >> 1)) {
Chris Mason1e2677e2007-05-29 16:52:18 -0400626 cache->data = data;
627 radix_tree_delete(cache->radix,
628 cache->key.objectid +
629 cache->key.offset - 1);
630
631 if (data) {
632 cache->radix =
633 &info->block_group_data_radix;
634 cache->item.flags |=
635 BTRFS_BLOCK_GROUP_DATA;
636 } else {
637 cache->radix = &info->block_group_radix;
638 cache->item.flags &=
639 ~BTRFS_BLOCK_GROUP_DATA;
640 }
641 ret = radix_tree_insert(cache->radix,
642 cache->key.objectid +
643 cache->key.offset - 1,
644 (void *)cache);
645 }
646 old_val += num;
Chris Masoncd1bc462007-04-27 10:08:34 -0400647 } else {
Chris Mason9078a3e2007-04-26 16:46:15 -0400648 old_val -= num;
Chris Masoncd1bc462007-04-27 10:08:34 -0400649 if (blocknr < cache->first_free)
650 cache->first_free = blocknr;
Chris Masone37c9e62007-05-09 20:13:14 -0400651 if (!cache->data && mark_free) {
652 for (i = 0; i < num; i++) {
653 set_radix_bit(&info->extent_map_radix,
654 blocknr + i);
655 }
656 }
Chris Mason84f54cf2007-06-12 07:43:08 -0400657 if (old_val < (cache->key.offset >> 1) &&
658 old_val + num >= (cache->key.offset >> 1)) {
Chris Masone37c9e62007-05-09 20:13:14 -0400659 radix_tree_tag_set(cache->radix,
660 cache->key.objectid +
661 cache->key.offset - 1,
662 BTRFS_BLOCK_GROUP_AVAIL);
663 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400664 }
Chris Mason9078a3e2007-04-26 16:46:15 -0400665 btrfs_set_block_group_used(&cache->item, old_val);
Chris Masone37c9e62007-05-09 20:13:14 -0400666 total -= num;
667 blocknr += num;
Chris Mason9078a3e2007-04-26 16:46:15 -0400668 }
669 return 0;
670}
671
Chris Masonbe08c1b2007-05-03 09:06:49 -0400672static int try_remove_page(struct address_space *mapping, unsigned long index)
673{
674 int ret;
675 ret = invalidate_mapping_pages(mapping, index, index);
676 return ret;
677}
678
Chris Masone089f052007-03-16 16:20:31 -0400679int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, struct
680 btrfs_root *root)
Chris Masona28ec192007-03-06 20:08:01 -0500681{
Chris Mason8ef97622007-03-26 10:15:30 -0400682 unsigned long gang[8];
Chris Masonbe08c1b2007-05-03 09:06:49 -0400683 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Masonbe744172007-05-06 10:15:01 -0400684 struct btrfs_block_group_cache *block_group;
Chris Mason88fd1462007-03-16 08:56:18 -0400685 u64 first = 0;
Chris Masona28ec192007-03-06 20:08:01 -0500686 int ret;
687 int i;
Chris Mason8ef97622007-03-26 10:15:30 -0400688 struct radix_tree_root *pinned_radix = &root->fs_info->pinned_radix;
Chris Masone37c9e62007-05-09 20:13:14 -0400689 struct radix_tree_root *extent_radix = &root->fs_info->extent_map_radix;
Chris Masona28ec192007-03-06 20:08:01 -0500690
691 while(1) {
Chris Masone37c9e62007-05-09 20:13:14 -0400692 ret = find_first_radix_bit(pinned_radix, gang, 0,
Chris Mason8ef97622007-03-26 10:15:30 -0400693 ARRAY_SIZE(gang));
Chris Masona28ec192007-03-06 20:08:01 -0500694 if (!ret)
695 break;
Chris Mason88fd1462007-03-16 08:56:18 -0400696 if (!first)
Chris Mason8ef97622007-03-26 10:15:30 -0400697 first = gang[0];
Chris Mason0579da42007-03-07 16:15:30 -0500698 for (i = 0; i < ret; i++) {
Chris Mason8ef97622007-03-26 10:15:30 -0400699 clear_radix_bit(pinned_radix, gang[i]);
Chris Mason5276aed2007-06-11 21:33:38 -0400700 block_group = btrfs_lookup_block_group(root->fs_info,
701 gang[i]);
Chris Masonbe744172007-05-06 10:15:01 -0400702 if (block_group) {
703 WARN_ON(block_group->pinned == 0);
704 block_group->pinned--;
705 if (gang[i] < block_group->last_alloc)
706 block_group->last_alloc = gang[i];
Chris Masone37c9e62007-05-09 20:13:14 -0400707 if (gang[i] < block_group->last_prealloc)
708 block_group->last_prealloc = gang[i];
709 if (!block_group->data)
710 set_radix_bit(extent_radix, gang[i]);
Chris Masonbe744172007-05-06 10:15:01 -0400711 }
Chris Masonbe08c1b2007-05-03 09:06:49 -0400712 try_remove_page(btree_inode->i_mapping,
713 gang[i] << (PAGE_CACHE_SHIFT -
714 btree_inode->i_blkbits));
Chris Mason0579da42007-03-07 16:15:30 -0500715 }
Chris Masona28ec192007-03-06 20:08:01 -0500716 }
717 return 0;
718}
719
Chris Masone089f052007-03-16 16:20:31 -0400720static int finish_current_insert(struct btrfs_trans_handle *trans, struct
721 btrfs_root *extent_root)
Chris Mason037e6392007-03-07 11:50:24 -0500722{
Chris Masone2fa7222007-03-12 16:22:34 -0400723 struct btrfs_key ins;
Chris Mason234b63a2007-03-13 10:46:10 -0400724 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -0500725 int i;
726 int ret;
Chris Mason1261ec42007-03-20 20:35:03 -0400727 u64 super_blocks_used;
728 struct btrfs_fs_info *info = extent_root->fs_info;
Chris Mason037e6392007-03-07 11:50:24 -0500729
Chris Masoncf27e1e2007-03-13 09:49:06 -0400730 btrfs_set_extent_refs(&extent_item, 1);
Chris Mason037e6392007-03-07 11:50:24 -0500731 ins.offset = 1;
732 ins.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -0400733 btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
Chris Mason5d0c3e62007-04-23 17:01:05 -0400734 btrfs_set_extent_owner(&extent_item, extent_root->root_key.objectid);
Chris Mason037e6392007-03-07 11:50:24 -0500735
Chris Masonf2458e12007-04-25 15:52:25 -0400736 for (i = 0; i < extent_root->fs_info->extent_tree_insert_nr; i++) {
737 ins.objectid = extent_root->fs_info->extent_tree_insert[i];
Chris Mason1261ec42007-03-20 20:35:03 -0400738 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
739 btrfs_set_super_blocks_used(info->disk_super,
740 super_blocks_used + 1);
Chris Masone089f052007-03-16 16:20:31 -0400741 ret = btrfs_insert_item(trans, extent_root, &ins, &extent_item,
742 sizeof(extent_item));
Chris Mason037e6392007-03-07 11:50:24 -0500743 BUG_ON(ret);
744 }
Chris Masonf2458e12007-04-25 15:52:25 -0400745 extent_root->fs_info->extent_tree_insert_nr = 0;
746 extent_root->fs_info->extent_tree_prealloc_nr = 0;
Chris Mason037e6392007-03-07 11:50:24 -0500747 return 0;
748}
749
Chris Mason8ef97622007-03-26 10:15:30 -0400750static int pin_down_block(struct btrfs_root *root, u64 blocknr, int pending)
Chris Masone20d96d2007-03-22 12:13:20 -0400751{
752 int err;
Chris Mason78fae272007-03-25 11:35:08 -0400753 struct btrfs_header *header;
Chris Mason8ef97622007-03-26 10:15:30 -0400754 struct buffer_head *bh;
Chris Mason78fae272007-03-25 11:35:08 -0400755
Chris Masonf4b9aa82007-03-27 11:05:53 -0400756 if (!pending) {
Chris Masond98237b2007-03-28 13:57:48 -0400757 bh = btrfs_find_tree_block(root, blocknr);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400758 if (bh) {
759 if (buffer_uptodate(bh)) {
760 u64 transid =
761 root->fs_info->running_transaction->transid;
762 header = btrfs_buffer_header(bh);
763 if (btrfs_header_generation(header) ==
764 transid) {
765 btrfs_block_release(root, bh);
766 return 0;
767 }
Chris Masonf4b9aa82007-03-27 11:05:53 -0400768 }
Chris Masond6025572007-03-30 14:27:56 -0400769 btrfs_block_release(root, bh);
Chris Mason8ef97622007-03-26 10:15:30 -0400770 }
Chris Mason8ef97622007-03-26 10:15:30 -0400771 err = set_radix_bit(&root->fs_info->pinned_radix, blocknr);
Chris Masonbe744172007-05-06 10:15:01 -0400772 if (!err) {
773 struct btrfs_block_group_cache *cache;
Chris Mason5276aed2007-06-11 21:33:38 -0400774 cache = btrfs_lookup_block_group(root->fs_info,
775 blocknr);
Chris Masonbe744172007-05-06 10:15:01 -0400776 if (cache)
777 cache->pinned++;
778 }
Chris Masonf4b9aa82007-03-27 11:05:53 -0400779 } else {
780 err = set_radix_bit(&root->fs_info->pending_del_radix, blocknr);
781 }
Chris Masonbe744172007-05-06 10:15:01 -0400782 BUG_ON(err < 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400783 return 0;
784}
785
Chris Masona28ec192007-03-06 20:08:01 -0500786/*
787 * remove an extent from the root, returns 0 on success
788 */
Chris Masone089f052007-03-16 16:20:31 -0400789static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masone37c9e62007-05-09 20:13:14 -0400790 *root, u64 blocknr, u64 num_blocks, int pin,
791 int mark_free)
Chris Masona28ec192007-03-06 20:08:01 -0500792{
Chris Mason5caf2a02007-04-02 11:20:42 -0400793 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -0400794 struct btrfs_key key;
Chris Mason1261ec42007-03-20 20:35:03 -0400795 struct btrfs_fs_info *info = root->fs_info;
796 struct btrfs_root *extent_root = info->extent_root;
Chris Masona28ec192007-03-06 20:08:01 -0500797 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400798 struct btrfs_extent_item *ei;
Chris Masone2fa7222007-03-12 16:22:34 -0400799 struct btrfs_key ins;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400800 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500801
Chris Masona28ec192007-03-06 20:08:01 -0500802 key.objectid = blocknr;
803 key.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -0400804 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masona28ec192007-03-06 20:08:01 -0500805 key.offset = num_blocks;
806
Chris Masonfbdc7622007-05-30 10:22:12 -0400807 find_free_extent(trans, root, 0, 0, (u64)-1, 0, &ins, 0);
Chris Mason5caf2a02007-04-02 11:20:42 -0400808 path = btrfs_alloc_path();
809 BUG_ON(!path);
810 btrfs_init_path(path);
Chris Mason5f26f772007-04-05 10:38:44 -0400811
Chris Mason5caf2a02007-04-02 11:20:42 -0400812 ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
Chris Masona28ec192007-03-06 20:08:01 -0500813 if (ret) {
Chris Masona28ec192007-03-06 20:08:01 -0500814 BUG();
815 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400816 ei = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
Chris Mason123abc82007-03-14 14:14:43 -0400817 struct btrfs_extent_item);
Chris Masona28ec192007-03-06 20:08:01 -0500818 BUG_ON(ei->refs == 0);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400819 refs = btrfs_extent_refs(ei) - 1;
820 btrfs_set_extent_refs(ei, refs);
Chris Mason5caf2a02007-04-02 11:20:42 -0400821 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400822 if (refs == 0) {
Chris Mason1261ec42007-03-20 20:35:03 -0400823 u64 super_blocks_used;
Chris Mason78fae272007-03-25 11:35:08 -0400824
825 if (pin) {
Chris Mason8ef97622007-03-26 10:15:30 -0400826 ret = pin_down_block(root, blocknr, 0);
Chris Mason78fae272007-03-25 11:35:08 -0400827 BUG_ON(ret);
828 }
829
Chris Mason1261ec42007-03-20 20:35:03 -0400830 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
831 btrfs_set_super_blocks_used(info->disk_super,
832 super_blocks_used - num_blocks);
Chris Mason5caf2a02007-04-02 11:20:42 -0400833 ret = btrfs_del_item(trans, extent_root, path);
Chris Masona28ec192007-03-06 20:08:01 -0500834 if (ret)
835 BUG();
Chris Masone37c9e62007-05-09 20:13:14 -0400836 ret = update_block_group(trans, root, blocknr, num_blocks, 0,
Chris Mason1e2677e2007-05-29 16:52:18 -0400837 mark_free, 0);
Chris Mason9078a3e2007-04-26 16:46:15 -0400838 BUG_ON(ret);
Chris Masona28ec192007-03-06 20:08:01 -0500839 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400840 btrfs_free_path(path);
Chris Masone089f052007-03-16 16:20:31 -0400841 finish_current_insert(trans, extent_root);
Chris Masona28ec192007-03-06 20:08:01 -0500842 return ret;
843}
844
845/*
Chris Masonfec577f2007-02-26 10:40:21 -0500846 * find all the blocks marked as pending in the radix tree and remove
847 * them from the extent map
848 */
Chris Masone089f052007-03-16 16:20:31 -0400849static int del_pending_extents(struct btrfs_trans_handle *trans, struct
850 btrfs_root *extent_root)
Chris Masonfec577f2007-02-26 10:40:21 -0500851{
852 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -0400853 int wret;
854 int err = 0;
Chris Mason8ef97622007-03-26 10:15:30 -0400855 unsigned long gang[4];
Chris Masonfec577f2007-02-26 10:40:21 -0500856 int i;
Chris Mason8ef97622007-03-26 10:15:30 -0400857 struct radix_tree_root *pending_radix;
858 struct radix_tree_root *pinned_radix;
Chris Masonbe744172007-05-06 10:15:01 -0400859 struct btrfs_block_group_cache *cache;
Chris Mason8ef97622007-03-26 10:15:30 -0400860
861 pending_radix = &extent_root->fs_info->pending_del_radix;
862 pinned_radix = &extent_root->fs_info->pinned_radix;
Chris Masonfec577f2007-02-26 10:40:21 -0500863
864 while(1) {
Chris Masone37c9e62007-05-09 20:13:14 -0400865 ret = find_first_radix_bit(pending_radix, gang, 0,
Chris Mason8ef97622007-03-26 10:15:30 -0400866 ARRAY_SIZE(gang));
Chris Masonfec577f2007-02-26 10:40:21 -0500867 if (!ret)
868 break;
869 for (i = 0; i < ret; i++) {
Chris Mason8ef97622007-03-26 10:15:30 -0400870 wret = set_radix_bit(pinned_radix, gang[i]);
Chris Masonbe744172007-05-06 10:15:01 -0400871 if (wret == 0) {
Chris Mason5276aed2007-06-11 21:33:38 -0400872 cache =
873 btrfs_lookup_block_group(extent_root->fs_info,
Chris Masonbe744172007-05-06 10:15:01 -0400874 gang[i]);
875 if (cache)
876 cache->pinned++;
877 }
878 if (wret < 0) {
879 printk(KERN_CRIT "set_radix_bit, err %d\n",
880 wret);
881 BUG_ON(wret < 0);
882 }
Chris Mason8ef97622007-03-26 10:15:30 -0400883 wret = clear_radix_bit(pending_radix, gang[i]);
884 BUG_ON(wret);
Chris Masond5719762007-03-23 10:01:08 -0400885 wret = __free_extent(trans, extent_root,
Chris Masone37c9e62007-05-09 20:13:14 -0400886 gang[i], 1, 0, 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400887 if (wret)
888 err = wret;
Chris Masonfec577f2007-02-26 10:40:21 -0500889 }
890 }
Chris Masone20d96d2007-03-22 12:13:20 -0400891 return err;
Chris Masonfec577f2007-02-26 10:40:21 -0500892}
893
894/*
895 * remove an extent from the root, returns 0 on success
896 */
Chris Masone089f052007-03-16 16:20:31 -0400897int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
898 *root, u64 blocknr, u64 num_blocks, int pin)
Chris Masonfec577f2007-02-26 10:40:21 -0500899{
Chris Mason9f5fae22007-03-20 14:38:32 -0400900 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Masonfec577f2007-02-26 10:40:21 -0500901 int pending_ret;
902 int ret;
Chris Masona28ec192007-03-06 20:08:01 -0500903
904 if (root == extent_root) {
Chris Mason8ef97622007-03-26 10:15:30 -0400905 pin_down_block(root, blocknr, 1);
Chris Masona28ec192007-03-06 20:08:01 -0500906 return 0;
907 }
Chris Masone37c9e62007-05-09 20:13:14 -0400908 ret = __free_extent(trans, root, blocknr, num_blocks, pin, pin == 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400909 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
Chris Masonfec577f2007-02-26 10:40:21 -0500910 return ret ? ret : pending_ret;
911}
912
913/*
914 * walks the btree of allocated extents and find a hole of a given size.
915 * The key ins is changed to record the hole:
916 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -0400917 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -0500918 * ins->offset == number of blocks
919 * Any available blocks before search_start are skipped.
920 */
Chris Masone089f052007-03-16 16:20:31 -0400921static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
922 *orig_root, u64 num_blocks, u64 search_start, u64
Chris Masonfbdc7622007-05-30 10:22:12 -0400923 search_end, u64 hint_block,
924 struct btrfs_key *ins, int data)
Chris Masonfec577f2007-02-26 10:40:21 -0500925{
Chris Mason5caf2a02007-04-02 11:20:42 -0400926 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -0400927 struct btrfs_key key;
Chris Masonfec577f2007-02-26 10:40:21 -0500928 int ret;
929 u64 hole_size = 0;
930 int slot = 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400931 u64 last_block = 0;
Chris Mason037e6392007-03-07 11:50:24 -0500932 u64 test_block;
Chris Masonbe744172007-05-06 10:15:01 -0400933 u64 orig_search_start = search_start;
Chris Masonfec577f2007-02-26 10:40:21 -0500934 int start_found;
Chris Mason234b63a2007-03-13 10:46:10 -0400935 struct btrfs_leaf *l;
Chris Mason9f5fae22007-03-20 14:38:32 -0400936 struct btrfs_root * root = orig_root->fs_info->extent_root;
Chris Masonf2458e12007-04-25 15:52:25 -0400937 struct btrfs_fs_info *info = root->fs_info;
Chris Mason0579da42007-03-07 16:15:30 -0500938 int total_needed = num_blocks;
Chris Masonf2458e12007-04-25 15:52:25 -0400939 int total_found = 0;
940 int fill_prealloc = 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400941 int level;
Chris Masonbe08c1b2007-05-03 09:06:49 -0400942 struct btrfs_block_group_cache *block_group;
Chris Masonbe744172007-05-06 10:15:01 -0400943 int full_scan = 0;
Chris Masonfbdc7622007-05-30 10:22:12 -0400944 int wrapped = 0;
Chris Masonde428b62007-05-18 13:28:27 -0400945 u64 limit;
Chris Masonfec577f2007-02-26 10:40:21 -0500946
Chris Masonb1a4d962007-04-04 15:27:52 -0400947 path = btrfs_alloc_path();
948 ins->flags = 0;
949 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
950
Chris Masone20d96d2007-03-22 12:13:20 -0400951 level = btrfs_header_level(btrfs_buffer_header(root->node));
Chris Masonf2458e12007-04-25 15:52:25 -0400952 if (num_blocks == 0) {
953 fill_prealloc = 1;
954 num_blocks = 1;
Chris Mason308535a2007-04-28 15:17:08 -0400955 total_needed = (min(level + 1, BTRFS_MAX_LEVEL) + 2) * 3;
Chris Masonf2458e12007-04-25 15:52:25 -0400956 }
Chris Mason3e1ad542007-05-07 20:03:49 -0400957 if (search_end == (u64)-1)
958 search_end = btrfs_super_total_blocks(info->disk_super);
Chris Masonfbdc7622007-05-30 10:22:12 -0400959 if (hint_block) {
Chris Mason5276aed2007-06-11 21:33:38 -0400960 block_group = btrfs_lookup_block_group(info, hint_block);
Chris Masonbe744172007-05-06 10:15:01 -0400961 block_group = btrfs_find_block_group(root, block_group,
Chris Masonfbdc7622007-05-30 10:22:12 -0400962 hint_block, data, 1);
Chris Masonbe744172007-05-06 10:15:01 -0400963 } else {
964 block_group = btrfs_find_block_group(root,
965 trans->block_group, 0,
Chris Masonde428b62007-05-18 13:28:27 -0400966 data, 1);
Chris Masonbe744172007-05-06 10:15:01 -0400967 }
968
969check_failed:
Chris Mason1e2677e2007-05-29 16:52:18 -0400970 if (!block_group->data)
Chris Masone37c9e62007-05-09 20:13:14 -0400971 search_start = find_search_start(root, &block_group,
972 search_start, total_needed);
Chris Masonfbdc7622007-05-30 10:22:12 -0400973 else if (!full_scan)
Chris Masone37c9e62007-05-09 20:13:14 -0400974 search_start = max(block_group->last_alloc, search_start);
975
Chris Mason5caf2a02007-04-02 11:20:42 -0400976 btrfs_init_path(path);
Chris Masonfec577f2007-02-26 10:40:21 -0500977 ins->objectid = search_start;
978 ins->offset = 0;
Chris Masonfec577f2007-02-26 10:40:21 -0500979 start_found = 0;
Chris Masone37c9e62007-05-09 20:13:14 -0400980
Chris Mason5caf2a02007-04-02 11:20:42 -0400981 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
Chris Mason0f70abe2007-02-28 16:46:22 -0500982 if (ret < 0)
983 goto error;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500984
Chris Masone37c9e62007-05-09 20:13:14 -0400985 if (path->slots[0] > 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400986 path->slots[0]--;
Chris Masone37c9e62007-05-09 20:13:14 -0400987 }
988
989 l = btrfs_buffer_leaf(path->nodes[0]);
990 btrfs_disk_key_to_cpu(&key, &l->items[path->slots[0]].key);
991 /*
992 * a rare case, go back one key if we hit a block group item
993 * instead of an extent item
994 */
995 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY &&
996 key.objectid + key.offset >= search_start) {
997 ins->objectid = key.objectid;
998 ins->offset = key.offset - 1;
999 btrfs_release_path(root, path);
1000 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
1001 if (ret < 0)
1002 goto error;
1003
1004 if (path->slots[0] > 0) {
1005 path->slots[0]--;
1006 }
1007 }
Chris Mason0579da42007-03-07 16:15:30 -05001008
Chris Masonfec577f2007-02-26 10:40:21 -05001009 while (1) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001010 l = btrfs_buffer_leaf(path->nodes[0]);
1011 slot = path->slots[0];
Chris Mason7518a232007-03-12 12:01:18 -04001012 if (slot >= btrfs_header_nritems(&l->header)) {
Chris Masonf2458e12007-04-25 15:52:25 -04001013 if (fill_prealloc) {
1014 info->extent_tree_prealloc_nr = 0;
1015 total_found = 0;
1016 }
Chris Masonde428b62007-05-18 13:28:27 -04001017 if (start_found)
1018 limit = last_block +
Chris Mason84f54cf2007-06-12 07:43:08 -04001019 (block_group->key.offset >> 1);
Chris Masonde428b62007-05-18 13:28:27 -04001020 else
1021 limit = search_start +
Chris Mason84f54cf2007-06-12 07:43:08 -04001022 (block_group->key.offset >> 1);
Chris Mason5caf2a02007-04-02 11:20:42 -04001023 ret = btrfs_next_leaf(root, path);
Chris Masonfec577f2007-02-26 10:40:21 -05001024 if (ret == 0)
1025 continue;
Chris Mason0f70abe2007-02-28 16:46:22 -05001026 if (ret < 0)
1027 goto error;
Chris Masonfec577f2007-02-26 10:40:21 -05001028 if (!start_found) {
1029 ins->objectid = search_start;
Chris Mason3e1ad542007-05-07 20:03:49 -04001030 ins->offset = search_end - search_start;
Chris Masonfec577f2007-02-26 10:40:21 -05001031 start_found = 1;
1032 goto check_pending;
1033 }
1034 ins->objectid = last_block > search_start ?
1035 last_block : search_start;
Chris Mason3e1ad542007-05-07 20:03:49 -04001036 ins->offset = search_end - ins->objectid;
Chris Masonfec577f2007-02-26 10:40:21 -05001037 goto check_pending;
1038 }
Chris Masone37c9e62007-05-09 20:13:14 -04001039
Chris Masone2fa7222007-03-12 16:22:34 -04001040 btrfs_disk_key_to_cpu(&key, &l->items[slot].key);
Chris Masone37c9e62007-05-09 20:13:14 -04001041 if (key.objectid >= search_start && key.objectid > last_block &&
1042 start_found) {
1043 if (last_block < search_start)
1044 last_block = search_start;
1045 hole_size = key.objectid - last_block;
1046 if (hole_size >= num_blocks) {
1047 ins->objectid = last_block;
1048 ins->offset = hole_size;
1049 goto check_pending;
Chris Mason0579da42007-03-07 16:15:30 -05001050 }
Chris Masonfec577f2007-02-26 10:40:21 -05001051 }
Chris Masone37c9e62007-05-09 20:13:14 -04001052
1053 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY)
1054 goto next;
1055
Chris Mason0579da42007-03-07 16:15:30 -05001056 start_found = 1;
Chris Masone2fa7222007-03-12 16:22:34 -04001057 last_block = key.objectid + key.offset;
Chris Masonfbdc7622007-05-30 10:22:12 -04001058 if (!full_scan && last_block >= block_group->key.objectid +
Chris Masonbe744172007-05-06 10:15:01 -04001059 block_group->key.offset) {
1060 btrfs_release_path(root, path);
1061 search_start = block_group->key.objectid +
1062 block_group->key.offset * 2;
1063 goto new_group;
1064 }
Chris Mason9078a3e2007-04-26 16:46:15 -04001065next:
Chris Mason5caf2a02007-04-02 11:20:42 -04001066 path->slots[0]++;
Chris Masonde428b62007-05-18 13:28:27 -04001067 cond_resched();
Chris Masonfec577f2007-02-26 10:40:21 -05001068 }
1069 // FIXME -ENOSPC
1070check_pending:
1071 /* we have to make sure we didn't find an extent that has already
1072 * been allocated by the map tree or the original allocation
1073 */
Chris Mason5caf2a02007-04-02 11:20:42 -04001074 btrfs_release_path(root, path);
Chris Masonfec577f2007-02-26 10:40:21 -05001075 BUG_ON(ins->objectid < search_start);
Chris Masone37c9e62007-05-09 20:13:14 -04001076
Chris Mason3e1ad542007-05-07 20:03:49 -04001077 if (ins->objectid + num_blocks >= search_end) {
Chris Masonfbdc7622007-05-30 10:22:12 -04001078 if (full_scan) {
1079 ret = -ENOSPC;
1080 goto error;
1081 }
Chris Masonbe744172007-05-06 10:15:01 -04001082 search_start = orig_search_start;
Chris Masonfbdc7622007-05-30 10:22:12 -04001083 if (wrapped)
1084 full_scan = 1;
1085 else
1086 wrapped = 1;
Chris Masonbe744172007-05-06 10:15:01 -04001087 goto new_group;
Chris Mason06a2f9f2007-04-28 08:48:10 -04001088 }
Chris Mason037e6392007-03-07 11:50:24 -05001089 for (test_block = ins->objectid;
Chris Masonf2458e12007-04-25 15:52:25 -04001090 test_block < ins->objectid + num_blocks; test_block++) {
1091 if (test_radix_bit(&info->pinned_radix, test_block)) {
Chris Mason037e6392007-03-07 11:50:24 -05001092 search_start = test_block + 1;
Chris Masonbe744172007-05-06 10:15:01 -04001093 goto new_group;
Chris Masonfec577f2007-02-26 10:40:21 -05001094 }
1095 }
Chris Masonf2458e12007-04-25 15:52:25 -04001096 if (!fill_prealloc && info->extent_tree_insert_nr) {
1097 u64 last =
1098 info->extent_tree_insert[info->extent_tree_insert_nr - 1];
1099 if (ins->objectid + num_blocks >
1100 info->extent_tree_insert[0] &&
1101 ins->objectid <= last) {
1102 search_start = last + 1;
Chris Masone37c9e62007-05-09 20:13:14 -04001103 WARN_ON(!full_scan);
Chris Masonbe744172007-05-06 10:15:01 -04001104 goto new_group;
Chris Masonf2458e12007-04-25 15:52:25 -04001105 }
1106 }
1107 if (!fill_prealloc && info->extent_tree_prealloc_nr) {
1108 u64 first =
1109 info->extent_tree_prealloc[info->extent_tree_prealloc_nr - 1];
1110 if (ins->objectid + num_blocks > first &&
1111 ins->objectid <= info->extent_tree_prealloc[0]) {
1112 search_start = info->extent_tree_prealloc[0] + 1;
Chris Masone37c9e62007-05-09 20:13:14 -04001113 WARN_ON(!full_scan);
Chris Masonbe744172007-05-06 10:15:01 -04001114 goto new_group;
Chris Masonf2458e12007-04-25 15:52:25 -04001115 }
1116 }
1117 if (fill_prealloc) {
1118 int nr;
1119 test_block = ins->objectid;
Chris Masone37c9e62007-05-09 20:13:14 -04001120 if (test_block - info->extent_tree_prealloc[total_needed - 1] >=
1121 leaf_range(root)) {
1122 total_found = 0;
1123 info->extent_tree_prealloc_nr = total_found;
1124 }
Chris Masonf2458e12007-04-25 15:52:25 -04001125 while(test_block < ins->objectid + ins->offset &&
1126 total_found < total_needed) {
1127 nr = total_needed - total_found - 1;
1128 BUG_ON(nr < 0);
Chris Masoncd1bc462007-04-27 10:08:34 -04001129 info->extent_tree_prealloc[nr] = test_block;
Chris Masonf2458e12007-04-25 15:52:25 -04001130 total_found++;
1131 test_block++;
1132 }
1133 if (total_found < total_needed) {
1134 search_start = test_block;
Chris Masonbe744172007-05-06 10:15:01 -04001135 goto new_group;
Chris Masonf2458e12007-04-25 15:52:25 -04001136 }
Chris Masoncd1bc462007-04-27 10:08:34 -04001137 info->extent_tree_prealloc_nr = total_found;
Chris Masonf2458e12007-04-25 15:52:25 -04001138 }
Chris Masone37c9e62007-05-09 20:13:14 -04001139 if (!data) {
Chris Mason5276aed2007-06-11 21:33:38 -04001140 block_group = btrfs_lookup_block_group(info, ins->objectid);
Chris Masone37c9e62007-05-09 20:13:14 -04001141 if (block_group) {
1142 if (fill_prealloc)
1143 block_group->last_prealloc =
1144 info->extent_tree_prealloc[total_needed-1];
1145 else
1146 trans->block_group = block_group;
1147 }
Chris Masoncd1bc462007-04-27 10:08:34 -04001148 }
Chris Mason037e6392007-03-07 11:50:24 -05001149 ins->offset = num_blocks;
Chris Mason5caf2a02007-04-02 11:20:42 -04001150 btrfs_free_path(path);
Chris Masonfec577f2007-02-26 10:40:21 -05001151 return 0;
Chris Masonbe744172007-05-06 10:15:01 -04001152
1153new_group:
Chris Mason3e1ad542007-05-07 20:03:49 -04001154 if (search_start + num_blocks >= search_end) {
Chris Masonbe744172007-05-06 10:15:01 -04001155 search_start = orig_search_start;
Chris Masonfbdc7622007-05-30 10:22:12 -04001156 if (full_scan) {
1157 ret = -ENOSPC;
1158 goto error;
1159 }
1160 if (wrapped)
1161 full_scan = 1;
1162 else
1163 wrapped = 1;
Chris Masonbe744172007-05-06 10:15:01 -04001164 }
Chris Mason5276aed2007-06-11 21:33:38 -04001165 block_group = btrfs_lookup_block_group(info, search_start);
Chris Masonfbdc7622007-05-30 10:22:12 -04001166 cond_resched();
Chris Masonbe744172007-05-06 10:15:01 -04001167 if (!full_scan)
1168 block_group = btrfs_find_block_group(root, block_group,
Chris Masonde428b62007-05-18 13:28:27 -04001169 search_start, data, 0);
Chris Masonbe744172007-05-06 10:15:01 -04001170 goto check_failed;
1171
Chris Mason0f70abe2007-02-28 16:46:22 -05001172error:
Chris Mason5caf2a02007-04-02 11:20:42 -04001173 btrfs_release_path(root, path);
1174 btrfs_free_path(path);
Chris Mason0f70abe2007-02-28 16:46:22 -05001175 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05001176}
Chris Masonfec577f2007-02-26 10:40:21 -05001177/*
Chris Masonfec577f2007-02-26 10:40:21 -05001178 * finds a free extent and does all the dirty work required for allocation
1179 * returns the key for the extent through ins, and a tree buffer for
1180 * the first block of the extent through buf.
1181 *
1182 * returns 0 if everything worked, non-zero otherwise.
1183 */
Chris Mason4d775672007-04-20 20:23:12 -04001184int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
1185 struct btrfs_root *root, u64 owner,
Chris Masonfbdc7622007-05-30 10:22:12 -04001186 u64 num_blocks, u64 hint_block,
Chris Masonbe08c1b2007-05-03 09:06:49 -04001187 u64 search_end, struct btrfs_key *ins, int data)
Chris Masonfec577f2007-02-26 10:40:21 -05001188{
1189 int ret;
1190 int pending_ret;
Chris Mason1261ec42007-03-20 20:35:03 -04001191 u64 super_blocks_used;
Chris Masonfbdc7622007-05-30 10:22:12 -04001192 u64 search_start = 0;
Chris Mason1261ec42007-03-20 20:35:03 -04001193 struct btrfs_fs_info *info = root->fs_info;
1194 struct btrfs_root *extent_root = info->extent_root;
Chris Mason234b63a2007-03-13 10:46:10 -04001195 struct btrfs_extent_item extent_item;
Chris Masonf2458e12007-04-25 15:52:25 -04001196 struct btrfs_key prealloc_key;
Chris Mason037e6392007-03-07 11:50:24 -05001197
Chris Masoncf27e1e2007-03-13 09:49:06 -04001198 btrfs_set_extent_refs(&extent_item, 1);
Chris Mason4d775672007-04-20 20:23:12 -04001199 btrfs_set_extent_owner(&extent_item, owner);
Chris Masonfec577f2007-02-26 10:40:21 -05001200
Chris Mason037e6392007-03-07 11:50:24 -05001201 if (root == extent_root) {
Chris Masonf2458e12007-04-25 15:52:25 -04001202 int nr;
1203 BUG_ON(info->extent_tree_prealloc_nr == 0);
Chris Mason037e6392007-03-07 11:50:24 -05001204 BUG_ON(num_blocks != 1);
Chris Mason037e6392007-03-07 11:50:24 -05001205 ins->offset = 1;
Chris Masonf2458e12007-04-25 15:52:25 -04001206 info->extent_tree_prealloc_nr--;
1207 nr = info->extent_tree_prealloc_nr;
1208 ins->objectid = info->extent_tree_prealloc[nr];
1209 info->extent_tree_insert[info->extent_tree_insert_nr++] =
1210 ins->objectid;
Chris Mason9078a3e2007-04-26 16:46:15 -04001211 ret = update_block_group(trans, root,
Chris Mason1e2677e2007-05-29 16:52:18 -04001212 ins->objectid, ins->offset, 1, 0, 0);
Chris Mason9078a3e2007-04-26 16:46:15 -04001213 BUG_ON(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05001214 return 0;
1215 }
Chris Masone37c9e62007-05-09 20:13:14 -04001216
1217 /*
1218 * if we're doing a data allocation, preallocate room in the
1219 * extent tree first. This way the extent tree blocks end up
1220 * in the correct block group.
1221 */
1222 if (data) {
Chris Masonde428b62007-05-18 13:28:27 -04001223 ret = find_free_extent(trans, root, 0, 0,
Chris Masonfbdc7622007-05-30 10:22:12 -04001224 search_end, 0, &prealloc_key, 0);
Chris Masone37c9e62007-05-09 20:13:14 -04001225 if (ret) {
1226 return ret;
1227 }
1228 if (prealloc_key.objectid + prealloc_key.offset >= search_end) {
1229 int nr = info->extent_tree_prealloc_nr;
1230 search_end = info->extent_tree_prealloc[nr - 1] - 1;
1231 } else {
1232 search_start = info->extent_tree_prealloc[0] + 1;
1233 }
1234 }
Chris Masonfbdc7622007-05-30 10:22:12 -04001235 if (hint_block < search_start)
1236 hint_block = search_start;
Chris Masonf2458e12007-04-25 15:52:25 -04001237 /* do the real allocation */
Chris Masone089f052007-03-16 16:20:31 -04001238 ret = find_free_extent(trans, root, num_blocks, search_start,
Chris Masonfbdc7622007-05-30 10:22:12 -04001239 search_end, hint_block, ins, data);
Chris Masone37c9e62007-05-09 20:13:14 -04001240 if (ret) {
Chris Mason037e6392007-03-07 11:50:24 -05001241 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -04001242 }
Chris Masonfec577f2007-02-26 10:40:21 -05001243
Chris Masone37c9e62007-05-09 20:13:14 -04001244 /*
1245 * if we're doing a metadata allocation, preallocate space in the
1246 * extent tree second. This way, we don't create a tiny hole
1247 * in the allocation map between any unused preallocation blocks
1248 * and the metadata block we're actually allocating. On disk,
1249 * it'll go:
1250 * [block we've allocated], [used prealloc 1], [ unused prealloc ]
1251 * The unused prealloc will get reused the next time around.
1252 */
1253 if (!data) {
1254 if (ins->objectid + ins->offset >= search_end)
1255 search_end = ins->objectid - 1;
1256 else
1257 search_start = ins->objectid + ins->offset;
Chris Mason3e1ad542007-05-07 20:03:49 -04001258
Chris Masonfbdc7622007-05-30 10:22:12 -04001259 if (hint_block < search_start)
1260 hint_block = search_start;
1261
Chris Masone37c9e62007-05-09 20:13:14 -04001262 ret = find_free_extent(trans, root, 0, search_start,
Chris Masonfbdc7622007-05-30 10:22:12 -04001263 search_end, hint_block,
1264 &prealloc_key, 0);
Chris Masone37c9e62007-05-09 20:13:14 -04001265 if (ret) {
1266 return ret;
1267 }
1268 }
Chris Masonf2458e12007-04-25 15:52:25 -04001269
Chris Mason1261ec42007-03-20 20:35:03 -04001270 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
1271 btrfs_set_super_blocks_used(info->disk_super, super_blocks_used +
1272 num_blocks);
Chris Masone089f052007-03-16 16:20:31 -04001273 ret = btrfs_insert_item(trans, extent_root, ins, &extent_item,
1274 sizeof(extent_item));
Chris Mason037e6392007-03-07 11:50:24 -05001275
Chris Masone089f052007-03-16 16:20:31 -04001276 finish_current_insert(trans, extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -04001277 pending_ret = del_pending_extents(trans, extent_root);
Chris Masone37c9e62007-05-09 20:13:14 -04001278 if (ret) {
Chris Mason037e6392007-03-07 11:50:24 -05001279 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -04001280 }
1281 if (pending_ret) {
Chris Mason037e6392007-03-07 11:50:24 -05001282 return pending_ret;
Chris Masone37c9e62007-05-09 20:13:14 -04001283 }
Chris Mason1e2677e2007-05-29 16:52:18 -04001284 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0,
1285 data);
Chris Masonfabb5682007-06-07 22:13:21 -04001286 BUG_ON(ret);
Chris Mason037e6392007-03-07 11:50:24 -05001287 return 0;
Chris Masonfec577f2007-02-26 10:40:21 -05001288}
1289
1290/*
1291 * helper function to allocate a block for a given tree
1292 * returns the tree buffer or NULL.
1293 */
Chris Masone20d96d2007-03-22 12:13:20 -04001294struct buffer_head *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Chris Mason31f3c992007-04-30 15:25:45 -04001295 struct btrfs_root *root, u64 hint)
Chris Masonfec577f2007-02-26 10:40:21 -05001296{
Chris Masone2fa7222007-03-12 16:22:34 -04001297 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -05001298 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -04001299 struct buffer_head *buf;
Chris Masonfec577f2007-02-26 10:40:21 -05001300
Chris Mason4d775672007-04-20 20:23:12 -04001301 ret = btrfs_alloc_extent(trans, root, root->root_key.objectid,
Chris Masonde428b62007-05-18 13:28:27 -04001302 1, hint, (unsigned long)-1, &ins, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05001303 if (ret) {
1304 BUG();
1305 return NULL;
1306 }
Chris Mason9078a3e2007-04-26 16:46:15 -04001307 BUG_ON(ret);
Chris Masond98237b2007-03-28 13:57:48 -04001308 buf = btrfs_find_create_tree_block(root, ins.objectid);
Chris Masondf2ce342007-03-23 11:00:45 -04001309 set_buffer_uptodate(buf);
Chris Mason090d1872007-05-01 08:53:32 -04001310 set_buffer_checked(buf);
Chris Mason7c4452b2007-04-28 09:29:35 -04001311 set_radix_bit(&trans->transaction->dirty_pages, buf->b_page->index);
Chris Masonfec577f2007-02-26 10:40:21 -05001312 return buf;
1313}
Chris Masona28ec192007-03-06 20:08:01 -05001314
Chris Mason6407bf62007-03-27 06:33:00 -04001315static int drop_leaf_ref(struct btrfs_trans_handle *trans,
1316 struct btrfs_root *root, struct buffer_head *cur)
1317{
1318 struct btrfs_disk_key *key;
1319 struct btrfs_leaf *leaf;
1320 struct btrfs_file_extent_item *fi;
1321 int i;
1322 int nritems;
1323 int ret;
1324
1325 BUG_ON(!btrfs_is_leaf(btrfs_buffer_node(cur)));
1326 leaf = btrfs_buffer_leaf(cur);
1327 nritems = btrfs_header_nritems(&leaf->header);
1328 for (i = 0; i < nritems; i++) {
Chris Mason3a686372007-05-24 13:35:57 -04001329 u64 disk_blocknr;
Chris Mason6407bf62007-03-27 06:33:00 -04001330 key = &leaf->items[i].key;
1331 if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY)
1332 continue;
1333 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
Chris Mason236454d2007-04-19 13:37:44 -04001334 if (btrfs_file_extent_type(fi) == BTRFS_FILE_EXTENT_INLINE)
1335 continue;
Chris Mason6407bf62007-03-27 06:33:00 -04001336 /*
1337 * FIXME make sure to insert a trans record that
1338 * repeats the snapshot del on crash
1339 */
Chris Mason3a686372007-05-24 13:35:57 -04001340 disk_blocknr = btrfs_file_extent_disk_blocknr(fi);
1341 if (disk_blocknr == 0)
1342 continue;
1343 ret = btrfs_free_extent(trans, root, disk_blocknr,
Chris Mason6407bf62007-03-27 06:33:00 -04001344 btrfs_file_extent_disk_num_blocks(fi),
1345 0);
1346 BUG_ON(ret);
1347 }
1348 return 0;
1349}
1350
Chris Mason9aca1d52007-03-13 11:09:37 -04001351/*
1352 * helper function for drop_snapshot, this walks down the tree dropping ref
1353 * counts as it goes.
1354 */
Chris Masone089f052007-03-16 16:20:31 -04001355static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
1356 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05001357{
Chris Masone20d96d2007-03-22 12:13:20 -04001358 struct buffer_head *next;
1359 struct buffer_head *cur;
Chris Mason20524f02007-03-10 06:35:47 -05001360 u64 blocknr;
1361 int ret;
1362 u32 refs;
1363
Chris Mason5caf2a02007-04-02 11:20:42 -04001364 WARN_ON(*level < 0);
1365 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Masonb18c6682007-04-17 13:26:50 -04001366 ret = lookup_extent_ref(trans, root, bh_blocknr(path->nodes[*level]),
Chris Mason6407bf62007-03-27 06:33:00 -04001367 1, &refs);
Chris Mason20524f02007-03-10 06:35:47 -05001368 BUG_ON(ret);
1369 if (refs > 1)
1370 goto out;
Chris Mason9aca1d52007-03-13 11:09:37 -04001371 /*
1372 * walk down to the last node level and free all the leaves
1373 */
Chris Mason6407bf62007-03-27 06:33:00 -04001374 while(*level >= 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001375 WARN_ON(*level < 0);
1376 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason20524f02007-03-10 06:35:47 -05001377 cur = path->nodes[*level];
Chris Mason2c90e5d2007-04-02 10:50:19 -04001378 if (btrfs_header_level(btrfs_buffer_header(cur)) != *level)
1379 WARN_ON(1);
Chris Mason7518a232007-03-12 12:01:18 -04001380 if (path->slots[*level] >=
Chris Masone20d96d2007-03-22 12:13:20 -04001381 btrfs_header_nritems(btrfs_buffer_header(cur)))
Chris Mason20524f02007-03-10 06:35:47 -05001382 break;
Chris Mason6407bf62007-03-27 06:33:00 -04001383 if (*level == 0) {
1384 ret = drop_leaf_ref(trans, root, cur);
1385 BUG_ON(ret);
1386 break;
1387 }
Chris Masone20d96d2007-03-22 12:13:20 -04001388 blocknr = btrfs_node_blockptr(btrfs_buffer_node(cur),
1389 path->slots[*level]);
Chris Masonb18c6682007-04-17 13:26:50 -04001390 ret = lookup_extent_ref(trans, root, blocknr, 1, &refs);
Chris Mason6407bf62007-03-27 06:33:00 -04001391 BUG_ON(ret);
1392 if (refs != 1) {
Chris Mason20524f02007-03-10 06:35:47 -05001393 path->slots[*level]++;
Chris Masone089f052007-03-16 16:20:31 -04001394 ret = btrfs_free_extent(trans, root, blocknr, 1, 1);
Chris Mason20524f02007-03-10 06:35:47 -05001395 BUG_ON(ret);
1396 continue;
1397 }
Chris Mason20524f02007-03-10 06:35:47 -05001398 next = read_tree_block(root, blocknr);
Chris Mason5caf2a02007-04-02 11:20:42 -04001399 WARN_ON(*level <= 0);
Chris Mason83e15a22007-03-12 09:03:27 -04001400 if (path->nodes[*level-1])
Chris Mason234b63a2007-03-13 10:46:10 -04001401 btrfs_block_release(root, path->nodes[*level-1]);
Chris Mason20524f02007-03-10 06:35:47 -05001402 path->nodes[*level-1] = next;
Chris Masone20d96d2007-03-22 12:13:20 -04001403 *level = btrfs_header_level(btrfs_buffer_header(next));
Chris Mason20524f02007-03-10 06:35:47 -05001404 path->slots[*level] = 0;
1405 }
1406out:
Chris Mason5caf2a02007-04-02 11:20:42 -04001407 WARN_ON(*level < 0);
1408 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason6407bf62007-03-27 06:33:00 -04001409 ret = btrfs_free_extent(trans, root,
Chris Mason7eccb902007-04-11 15:53:25 -04001410 bh_blocknr(path->nodes[*level]), 1, 1);
Chris Mason234b63a2007-03-13 10:46:10 -04001411 btrfs_block_release(root, path->nodes[*level]);
Chris Mason20524f02007-03-10 06:35:47 -05001412 path->nodes[*level] = NULL;
1413 *level += 1;
1414 BUG_ON(ret);
1415 return 0;
1416}
1417
Chris Mason9aca1d52007-03-13 11:09:37 -04001418/*
1419 * helper for dropping snapshots. This walks back up the tree in the path
1420 * to find the first node higher up where we haven't yet gone through
1421 * all the slots
1422 */
Chris Masone089f052007-03-16 16:20:31 -04001423static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root
1424 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05001425{
1426 int i;
1427 int slot;
1428 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -04001429 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Mason20524f02007-03-10 06:35:47 -05001430 slot = path->slots[i];
Chris Masone20d96d2007-03-22 12:13:20 -04001431 if (slot < btrfs_header_nritems(
1432 btrfs_buffer_header(path->nodes[i])) - 1) {
Chris Mason20524f02007-03-10 06:35:47 -05001433 path->slots[i]++;
1434 *level = i;
1435 return 0;
1436 } else {
Chris Masone089f052007-03-16 16:20:31 -04001437 ret = btrfs_free_extent(trans, root,
Chris Mason7eccb902007-04-11 15:53:25 -04001438 bh_blocknr(path->nodes[*level]),
Chris Masone089f052007-03-16 16:20:31 -04001439 1, 1);
Chris Mason6407bf62007-03-27 06:33:00 -04001440 BUG_ON(ret);
Chris Mason234b63a2007-03-13 10:46:10 -04001441 btrfs_block_release(root, path->nodes[*level]);
Chris Mason83e15a22007-03-12 09:03:27 -04001442 path->nodes[*level] = NULL;
Chris Mason20524f02007-03-10 06:35:47 -05001443 *level = i + 1;
Chris Mason20524f02007-03-10 06:35:47 -05001444 }
1445 }
1446 return 1;
1447}
1448
Chris Mason9aca1d52007-03-13 11:09:37 -04001449/*
1450 * drop the reference count on the tree rooted at 'snap'. This traverses
1451 * the tree freeing any blocks that have a ref count of zero after being
1452 * decremented.
1453 */
Chris Masone089f052007-03-16 16:20:31 -04001454int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masone20d96d2007-03-22 12:13:20 -04001455 *root, struct buffer_head *snap)
Chris Mason20524f02007-03-10 06:35:47 -05001456{
Chris Mason3768f362007-03-13 16:47:54 -04001457 int ret = 0;
Chris Mason9aca1d52007-03-13 11:09:37 -04001458 int wret;
Chris Mason20524f02007-03-10 06:35:47 -05001459 int level;
Chris Mason5caf2a02007-04-02 11:20:42 -04001460 struct btrfs_path *path;
Chris Mason20524f02007-03-10 06:35:47 -05001461 int i;
1462 int orig_level;
1463
Chris Mason5caf2a02007-04-02 11:20:42 -04001464 path = btrfs_alloc_path();
1465 BUG_ON(!path);
1466 btrfs_init_path(path);
Chris Mason20524f02007-03-10 06:35:47 -05001467
Chris Masone20d96d2007-03-22 12:13:20 -04001468 level = btrfs_header_level(btrfs_buffer_header(snap));
Chris Mason20524f02007-03-10 06:35:47 -05001469 orig_level = level;
Chris Mason5caf2a02007-04-02 11:20:42 -04001470 path->nodes[level] = snap;
1471 path->slots[level] = 0;
Chris Mason20524f02007-03-10 06:35:47 -05001472 while(1) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001473 wret = walk_down_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04001474 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05001475 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04001476 if (wret < 0)
1477 ret = wret;
1478
Chris Mason5caf2a02007-04-02 11:20:42 -04001479 wret = walk_up_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04001480 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05001481 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04001482 if (wret < 0)
1483 ret = wret;
Chris Mason35b7e472007-05-02 15:53:43 -04001484 btrfs_btree_balance_dirty(root);
Chris Mason20524f02007-03-10 06:35:47 -05001485 }
Chris Mason83e15a22007-03-12 09:03:27 -04001486 for (i = 0; i <= orig_level; i++) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001487 if (path->nodes[i]) {
1488 btrfs_block_release(root, path->nodes[i]);
Chris Mason83e15a22007-03-12 09:03:27 -04001489 }
Chris Mason20524f02007-03-10 06:35:47 -05001490 }
Chris Mason5caf2a02007-04-02 11:20:42 -04001491 btrfs_free_path(path);
Chris Mason9aca1d52007-03-13 11:09:37 -04001492 return ret;
Chris Mason20524f02007-03-10 06:35:47 -05001493}
Chris Mason9078a3e2007-04-26 16:46:15 -04001494
Chris Masonbe744172007-05-06 10:15:01 -04001495static int free_block_group_radix(struct radix_tree_root *radix)
Chris Mason9078a3e2007-04-26 16:46:15 -04001496{
1497 int ret;
1498 struct btrfs_block_group_cache *cache[8];
1499 int i;
1500
1501 while(1) {
Chris Masonbe744172007-05-06 10:15:01 -04001502 ret = radix_tree_gang_lookup(radix, (void **)cache, 0,
Chris Mason9078a3e2007-04-26 16:46:15 -04001503 ARRAY_SIZE(cache));
1504 if (!ret)
1505 break;
1506 for (i = 0; i < ret; i++) {
Chris Masonbe744172007-05-06 10:15:01 -04001507 radix_tree_delete(radix, cache[i]->key.objectid +
Chris Mason9078a3e2007-04-26 16:46:15 -04001508 cache[i]->key.offset - 1);
1509 kfree(cache[i]);
1510 }
1511 }
1512 return 0;
1513}
1514
Chris Masonbe744172007-05-06 10:15:01 -04001515int btrfs_free_block_groups(struct btrfs_fs_info *info)
1516{
1517 int ret;
1518 int ret2;
Chris Masone37c9e62007-05-09 20:13:14 -04001519 unsigned long gang[16];
1520 int i;
Chris Masonbe744172007-05-06 10:15:01 -04001521
1522 ret = free_block_group_radix(&info->block_group_radix);
1523 ret2 = free_block_group_radix(&info->block_group_data_radix);
1524 if (ret)
1525 return ret;
1526 if (ret2)
1527 return ret2;
Chris Masone37c9e62007-05-09 20:13:14 -04001528
1529 while(1) {
1530 ret = find_first_radix_bit(&info->extent_map_radix,
1531 gang, 0, ARRAY_SIZE(gang));
1532 if (!ret)
1533 break;
1534 for (i = 0; i < ret; i++) {
1535 clear_radix_bit(&info->extent_map_radix, gang[i]);
1536 }
1537 }
Chris Masonbe744172007-05-06 10:15:01 -04001538 return 0;
1539}
1540
Chris Mason9078a3e2007-04-26 16:46:15 -04001541int btrfs_read_block_groups(struct btrfs_root *root)
1542{
1543 struct btrfs_path *path;
1544 int ret;
1545 int err = 0;
1546 struct btrfs_block_group_item *bi;
1547 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04001548 struct btrfs_fs_info *info = root->fs_info;
1549 struct radix_tree_root *radix;
Chris Mason9078a3e2007-04-26 16:46:15 -04001550 struct btrfs_key key;
1551 struct btrfs_key found_key;
1552 struct btrfs_leaf *leaf;
Chris Mason84f54cf2007-06-12 07:43:08 -04001553 u64 group_size_blocks;
Chris Mason31f3c992007-04-30 15:25:45 -04001554 u64 used;
Chris Mason9078a3e2007-04-26 16:46:15 -04001555
Chris Mason84f54cf2007-06-12 07:43:08 -04001556 group_size_blocks = BTRFS_BLOCK_GROUP_SIZE >>
1557 root->fs_info->sb->s_blocksize_bits;
Chris Masonbe744172007-05-06 10:15:01 -04001558 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04001559 key.objectid = 0;
1560 key.offset = group_size_blocks;
1561 key.flags = 0;
1562 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
1563
1564 path = btrfs_alloc_path();
1565 if (!path)
1566 return -ENOMEM;
1567
1568 while(1) {
Chris Masonbe744172007-05-06 10:15:01 -04001569 ret = btrfs_search_slot(NULL, info->extent_root,
Chris Mason9078a3e2007-04-26 16:46:15 -04001570 &key, path, 0, 0);
1571 if (ret != 0) {
1572 err = ret;
1573 break;
1574 }
1575 leaf = btrfs_buffer_leaf(path->nodes[0]);
1576 btrfs_disk_key_to_cpu(&found_key,
1577 &leaf->items[path->slots[0]].key);
1578 cache = kmalloc(sizeof(*cache), GFP_NOFS);
1579 if (!cache) {
1580 err = -1;
1581 break;
1582 }
Chris Mason3e1ad542007-05-07 20:03:49 -04001583
Chris Mason9078a3e2007-04-26 16:46:15 -04001584 bi = btrfs_item_ptr(leaf, path->slots[0],
1585 struct btrfs_block_group_item);
Chris Mason1e2677e2007-05-29 16:52:18 -04001586 if (bi->flags & BTRFS_BLOCK_GROUP_DATA) {
1587 radix = &info->block_group_data_radix;
1588 cache->data = 1;
1589 } else {
1590 radix = &info->block_group_radix;
1591 cache->data = 0;
1592 }
1593
Chris Mason9078a3e2007-04-26 16:46:15 -04001594 memcpy(&cache->item, bi, sizeof(*bi));
1595 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Mason31f3c992007-04-30 15:25:45 -04001596 cache->last_alloc = cache->key.objectid;
1597 cache->first_free = cache->key.objectid;
Chris Masone37c9e62007-05-09 20:13:14 -04001598 cache->last_prealloc = cache->key.objectid;
Chris Masonbe744172007-05-06 10:15:01 -04001599 cache->pinned = 0;
Chris Masone37c9e62007-05-09 20:13:14 -04001600 cache->cached = 0;
1601
Chris Mason3e1ad542007-05-07 20:03:49 -04001602 cache->radix = radix;
1603
Chris Mason9078a3e2007-04-26 16:46:15 -04001604 key.objectid = found_key.objectid + found_key.offset;
1605 btrfs_release_path(root, path);
Chris Masonbe744172007-05-06 10:15:01 -04001606 ret = radix_tree_insert(radix, found_key.objectid +
Chris Mason9078a3e2007-04-26 16:46:15 -04001607 found_key.offset - 1,
1608 (void *)cache);
1609 BUG_ON(ret);
Chris Mason31f3c992007-04-30 15:25:45 -04001610 used = btrfs_block_group_used(bi);
Chris Mason84f54cf2007-06-12 07:43:08 -04001611 if (used < div_factor(key.offset, 8)) {
Chris Masonbe744172007-05-06 10:15:01 -04001612 radix_tree_tag_set(radix, found_key.objectid +
Chris Mason31f3c992007-04-30 15:25:45 -04001613 found_key.offset - 1,
1614 BTRFS_BLOCK_GROUP_AVAIL);
1615 }
Chris Mason9078a3e2007-04-26 16:46:15 -04001616 if (key.objectid >=
Chris Masonbe744172007-05-06 10:15:01 -04001617 btrfs_super_total_blocks(info->disk_super))
Chris Mason9078a3e2007-04-26 16:46:15 -04001618 break;
1619 }
1620
1621 btrfs_free_path(path);
1622 return 0;
1623}