blob: 4bc639565d1c08b2a10ac79dbfeb8f17dc63f48d [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 Mason96b51792007-10-15 16:15:19 -040025#define BLOCK_GROUP_DATA EXTENT_WRITEBACK
26#define BLOCK_GROUP_METADATA EXTENT_UPTODATE
27#define BLOCK_GROUP_DIRTY EXTENT_DIRTY
28
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 Masone37c9e62007-05-09 20:13:14 -040034static int cache_block_group(struct btrfs_root *root,
35 struct btrfs_block_group_cache *block_group)
36{
37 struct btrfs_path *path;
38 int ret;
39 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -040040 struct extent_buffer *leaf;
Chris Masonf510cfe2007-10-15 16:14:48 -040041 struct extent_map_tree *free_space_cache;
Chris Masone37c9e62007-05-09 20:13:14 -040042 int slot;
Chris Masone37c9e62007-05-09 20:13:14 -040043 u64 last = 0;
44 u64 hole_size;
Yan7d7d6062007-09-14 16:15:28 -040045 u64 first_free;
Chris Masone37c9e62007-05-09 20:13:14 -040046 int found = 0;
47
48 root = root->fs_info->extent_root;
Chris Masonf510cfe2007-10-15 16:14:48 -040049 free_space_cache = &root->fs_info->free_space_cache;
Chris Masone37c9e62007-05-09 20:13:14 -040050
51 if (block_group->cached)
52 return 0;
Chris Masonf510cfe2007-10-15 16:14:48 -040053
Chris Masone37c9e62007-05-09 20:13:14 -040054 path = btrfs_alloc_path();
55 if (!path)
56 return -ENOMEM;
Yan7d7d6062007-09-14 16:15:28 -040057
Chris Mason2cc58cf2007-08-27 16:49:44 -040058 path->reada = 2;
Yan7d7d6062007-09-14 16:15:28 -040059 first_free = block_group->key.objectid;
Chris Masone37c9e62007-05-09 20:13:14 -040060 key.objectid = block_group->key.objectid;
Chris Masone37c9e62007-05-09 20:13:14 -040061 key.offset = 0;
Yan7d7d6062007-09-14 16:15:28 -040062
Chris Masone37c9e62007-05-09 20:13:14 -040063 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
64 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Yan7d7d6062007-09-14 16:15:28 -040065
Chris Masone37c9e62007-05-09 20:13:14 -040066 if (ret < 0)
67 return ret;
Yan7d7d6062007-09-14 16:15:28 -040068
Chris Masone37c9e62007-05-09 20:13:14 -040069 if (ret && path->slots[0] > 0)
70 path->slots[0]--;
Yan7d7d6062007-09-14 16:15:28 -040071
Chris Masone37c9e62007-05-09 20:13:14 -040072 while(1) {
Chris Mason5f39d392007-10-15 16:14:19 -040073 leaf = path->nodes[0];
Chris Masone37c9e62007-05-09 20:13:14 -040074 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -040075 if (slot >= btrfs_header_nritems(leaf)) {
Chris Masone37c9e62007-05-09 20:13:14 -040076 ret = btrfs_next_leaf(root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -040077 if (ret < 0)
78 goto err;
Chris Masonde428b62007-05-18 13:28:27 -040079 if (ret == 0) {
Chris Masone37c9e62007-05-09 20:13:14 -040080 continue;
Chris Masonde428b62007-05-18 13:28:27 -040081 } else {
Chris Masone37c9e62007-05-09 20:13:14 -040082 break;
83 }
84 }
Yan7d7d6062007-09-14 16:15:28 -040085
Chris Mason5f39d392007-10-15 16:14:19 -040086 btrfs_item_key_to_cpu(leaf, &key, slot);
Yan7d7d6062007-09-14 16:15:28 -040087 if (key.objectid < block_group->key.objectid) {
88 if (key.objectid + key.offset > first_free)
89 first_free = key.objectid + key.offset;
90 goto next;
91 }
92
Chris Masone37c9e62007-05-09 20:13:14 -040093 if (key.objectid >= block_group->key.objectid +
94 block_group->key.offset) {
Yan7d7d6062007-09-14 16:15:28 -040095 break;
96 }
97
98 if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) {
99 if (!found) {
100 last = first_free;
101 found = 1;
Chris Masone37c9e62007-05-09 20:13:14 -0400102 }
Chris Masonf510cfe2007-10-15 16:14:48 -0400103 if (key.objectid > last) {
104 hole_size = key.objectid - last;
105 set_extent_dirty(free_space_cache, last,
106 last + hole_size - 1,
107 GFP_NOFS);
Chris Masone37c9e62007-05-09 20:13:14 -0400108 }
Yan7d7d6062007-09-14 16:15:28 -0400109 last = key.objectid + key.offset;
Chris Masone37c9e62007-05-09 20:13:14 -0400110 }
Yan7d7d6062007-09-14 16:15:28 -0400111next:
Chris Masone37c9e62007-05-09 20:13:14 -0400112 path->slots[0]++;
113 }
114
Yan7d7d6062007-09-14 16:15:28 -0400115 if (!found)
116 last = first_free;
117 if (block_group->key.objectid +
118 block_group->key.offset > last) {
119 hole_size = block_group->key.objectid +
120 block_group->key.offset - last;
Chris Masonf510cfe2007-10-15 16:14:48 -0400121 set_extent_dirty(free_space_cache, last,
122 last + hole_size - 1, GFP_NOFS);
Yan7d7d6062007-09-14 16:15:28 -0400123 }
Chris Masone37c9e62007-05-09 20:13:14 -0400124 block_group->cached = 1;
Chris Mason54aa1f42007-06-22 14:16:25 -0400125err:
Chris Masone37c9e62007-05-09 20:13:14 -0400126 btrfs_free_path(path);
127 return 0;
128}
129
Chris Mason5276aed2007-06-11 21:33:38 -0400130struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
131 btrfs_fs_info *info,
132 u64 blocknr)
Chris Masonbe744172007-05-06 10:15:01 -0400133{
Chris Mason96b51792007-10-15 16:15:19 -0400134 struct extent_map_tree *block_group_cache;
135 struct btrfs_block_group_cache *block_group = NULL;
136 u64 ptr;
137 u64 start;
138 u64 end;
Chris Masonbe744172007-05-06 10:15:01 -0400139 int ret;
140
Chris Mason96b51792007-10-15 16:15:19 -0400141 block_group_cache = &info->block_group_cache;
142 ret = find_first_extent_bit(block_group_cache,
143 blocknr, &start, &end,
144 BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA);
Chris Masonbe744172007-05-06 10:15:01 -0400145 if (ret) {
Chris Mason96b51792007-10-15 16:15:19 -0400146 return NULL;
Chris Masonbe744172007-05-06 10:15:01 -0400147 }
Chris Mason96b51792007-10-15 16:15:19 -0400148 ret = get_state_private(block_group_cache, start, &ptr);
149 if (ret)
150 return NULL;
151
152 block_group = (struct btrfs_block_group_cache *)ptr;
153
154
155 if (block_group->key.objectid <= blocknr && blocknr <=
156 block_group->key.objectid + block_group->key.offset)
157 return block_group;
158
Chris Masonbe744172007-05-06 10:15:01 -0400159 return NULL;
160}
161
Chris Masone37c9e62007-05-09 20:13:14 -0400162static u64 find_search_start(struct btrfs_root *root,
163 struct btrfs_block_group_cache **cache_ret,
Chris Masonf510cfe2007-10-15 16:14:48 -0400164 u64 search_start, int num, int data)
Chris Masone37c9e62007-05-09 20:13:14 -0400165{
Chris Masone37c9e62007-05-09 20:13:14 -0400166 int ret;
167 struct btrfs_block_group_cache *cache = *cache_ret;
168 u64 last = max(search_start, cache->key.objectid);
Chris Masonf510cfe2007-10-15 16:14:48 -0400169 u64 start = 0;
170 u64 end = 0;
Chris Masone37c9e62007-05-09 20:13:14 -0400171
Chris Masone37c9e62007-05-09 20:13:14 -0400172again:
Chris Mason54aa1f42007-06-22 14:16:25 -0400173 ret = cache_block_group(root, cache);
174 if (ret)
175 goto out;
Chris Masone37c9e62007-05-09 20:13:14 -0400176 while(1) {
Chris Masonf510cfe2007-10-15 16:14:48 -0400177 ret = find_first_extent_bit(&root->fs_info->free_space_cache,
178 last, &start, &end, EXTENT_DIRTY);
179 if (ret)
Chris Masone37c9e62007-05-09 20:13:14 -0400180 goto out;
Chris Masonf510cfe2007-10-15 16:14:48 -0400181
182 start = max(last, start);
183 last = end + 1;
184 if (end + 1 - start < num)
185 continue;
Chris Mason96b51792007-10-15 16:15:19 -0400186 if (start + num >= cache->key.objectid + cache->key.offset)
Chris Masone37c9e62007-05-09 20:13:14 -0400187 goto new_group;
Chris Masonf510cfe2007-10-15 16:14:48 -0400188 return start;
Chris Masone37c9e62007-05-09 20:13:14 -0400189 }
190out:
191 return max(cache->last_alloc, search_start);
192
193new_group:
Chris Mason5276aed2007-06-11 21:33:38 -0400194 cache = btrfs_lookup_block_group(root->fs_info,
195 last + cache->key.offset - 1);
Chris Masone37c9e62007-05-09 20:13:14 -0400196 if (!cache) {
197 return max((*cache_ret)->last_alloc, search_start);
198 }
199 cache = btrfs_find_block_group(root, cache,
Chris Masonf510cfe2007-10-15 16:14:48 -0400200 last + cache->key.offset - 1, data, 0);
Chris Masone37c9e62007-05-09 20:13:14 -0400201 *cache_ret = cache;
Chris Mason96b51792007-10-15 16:15:19 -0400202 last = min(cache->key.objectid, last);
Chris Masone37c9e62007-05-09 20:13:14 -0400203 goto again;
204}
205
Chris Mason84f54cf2007-06-12 07:43:08 -0400206static u64 div_factor(u64 num, int factor)
207{
208 num *= factor;
209 do_div(num, 10);
210 return num;
211}
212
Chris Mason31f3c992007-04-30 15:25:45 -0400213struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
214 struct btrfs_block_group_cache
Chris Masonbe744172007-05-06 10:15:01 -0400215 *hint, u64 search_start,
Chris Masonde428b62007-05-18 13:28:27 -0400216 int data, int owner)
Chris Masoncd1bc462007-04-27 10:08:34 -0400217{
Chris Mason96b51792007-10-15 16:15:19 -0400218 struct btrfs_block_group_cache *cache;
219 struct extent_map_tree *block_group_cache;
Chris Mason31f3c992007-04-30 15:25:45 -0400220 struct btrfs_block_group_cache *found_group = NULL;
Chris Masoncd1bc462007-04-27 10:08:34 -0400221 struct btrfs_fs_info *info = root->fs_info;
222 u64 used;
Chris Mason31f3c992007-04-30 15:25:45 -0400223 u64 last = 0;
224 u64 hint_last;
Chris Mason96b51792007-10-15 16:15:19 -0400225 u64 start;
226 u64 end;
227 u64 free_check;
228 u64 ptr;
229 int bit;
Chris Masoncd1bc462007-04-27 10:08:34 -0400230 int ret;
Chris Mason31f3c992007-04-30 15:25:45 -0400231 int full_search = 0;
Chris Masonde428b62007-05-18 13:28:27 -0400232 int factor = 8;
Chris Mason1e2677e2007-05-29 16:52:18 -0400233 int data_swap = 0;
Chris Masonde428b62007-05-18 13:28:27 -0400234
Chris Mason96b51792007-10-15 16:15:19 -0400235 block_group_cache = &info->block_group_cache;
236
Chris Masonde428b62007-05-18 13:28:27 -0400237 if (!owner)
238 factor = 5;
Chris Masonbe744172007-05-06 10:15:01 -0400239
Chris Mason96b51792007-10-15 16:15:19 -0400240 if (data)
241 bit = BLOCK_GROUP_DATA;
242 else
243 bit = BLOCK_GROUP_METADATA;
Chris Masonbe744172007-05-06 10:15:01 -0400244
245 if (search_start) {
246 struct btrfs_block_group_cache *shint;
Chris Mason5276aed2007-06-11 21:33:38 -0400247 shint = btrfs_lookup_block_group(info, search_start);
Yane9fe3952007-08-29 09:11:44 -0400248 if (shint && shint->data == data) {
Chris Masonbe744172007-05-06 10:15:01 -0400249 used = btrfs_block_group_used(&shint->item);
250 if (used + shint->pinned <
Chris Mason84f54cf2007-06-12 07:43:08 -0400251 div_factor(shint->key.offset, factor)) {
Chris Masonbe744172007-05-06 10:15:01 -0400252 return shint;
253 }
254 }
255 }
256 if (hint && hint->data == data) {
Chris Mason31f3c992007-04-30 15:25:45 -0400257 used = btrfs_block_group_used(&hint->item);
Chris Mason84f54cf2007-06-12 07:43:08 -0400258 if (used + hint->pinned <
259 div_factor(hint->key.offset, factor)) {
Chris Mason31f3c992007-04-30 15:25:45 -0400260 return hint;
261 }
Chris Mason8d7be552007-05-10 11:24:42 -0400262 last = hint->key.offset * 3;
Chris Masonbe744172007-05-06 10:15:01 -0400263 if (hint->key.objectid >= last)
Chris Masone37c9e62007-05-09 20:13:14 -0400264 last = max(search_start + hint->key.offset - 1,
265 hint->key.objectid - last);
Chris Masonbe744172007-05-06 10:15:01 -0400266 else
267 last = hint->key.objectid + hint->key.offset;
Chris Mason31f3c992007-04-30 15:25:45 -0400268 hint_last = last;
269 } else {
Chris Masone37c9e62007-05-09 20:13:14 -0400270 if (hint)
271 hint_last = max(hint->key.objectid, search_start);
272 else
273 hint_last = search_start;
274
275 last = hint_last;
Chris Mason31f3c992007-04-30 15:25:45 -0400276 }
Chris Mason31f3c992007-04-30 15:25:45 -0400277again:
Chris Masoncd1bc462007-04-27 10:08:34 -0400278 while(1) {
Chris Mason96b51792007-10-15 16:15:19 -0400279 ret = find_first_extent_bit(block_group_cache, last,
280 &start, &end, bit);
281 if (ret)
Chris Masoncd1bc462007-04-27 10:08:34 -0400282 break;
Chris Mason96b51792007-10-15 16:15:19 -0400283
284 ret = get_state_private(block_group_cache, start, &ptr);
285 if (ret)
286 break;
287
288 cache = (struct btrfs_block_group_cache *)ptr;
289 last = cache->key.objectid + cache->key.offset;
290 used = btrfs_block_group_used(&cache->item);
291
292 if (full_search)
293 free_check = cache->key.offset;
294 else
295 free_check = div_factor(cache->key.offset, factor);
296
297 if (used + cache->pinned < free_check) {
298 found_group = cache;
299 goto found;
Chris Masoncd1bc462007-04-27 10:08:34 -0400300 }
Chris Masonde428b62007-05-18 13:28:27 -0400301 cond_resched();
Chris Masoncd1bc462007-04-27 10:08:34 -0400302 }
Chris Mason31f3c992007-04-30 15:25:45 -0400303 if (!full_search) {
Chris Masonbe744172007-05-06 10:15:01 -0400304 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -0400305 full_search = 1;
306 goto again;
307 }
Chris Mason1e2677e2007-05-29 16:52:18 -0400308 if (!data_swap) {
Chris Mason1e2677e2007-05-29 16:52:18 -0400309 data_swap = 1;
Chris Mason96b51792007-10-15 16:15:19 -0400310 bit = BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA;
Chris Mason1e2677e2007-05-29 16:52:18 -0400311 last = search_start;
312 goto again;
313 }
Chris Masonbe744172007-05-06 10:15:01 -0400314found:
Chris Mason31f3c992007-04-30 15:25:45 -0400315 return found_group;
Chris Masoncd1bc462007-04-27 10:08:34 -0400316}
317
Chris Masonb18c6682007-04-17 13:26:50 -0400318int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
319 struct btrfs_root *root,
320 u64 blocknr, u64 num_blocks)
Chris Mason02217ed2007-03-02 16:08:05 -0500321{
Chris Mason5caf2a02007-04-02 11:20:42 -0400322 struct btrfs_path *path;
Chris Mason02217ed2007-03-02 16:08:05 -0500323 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400324 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400325 struct extent_buffer *l;
Chris Mason234b63a2007-03-13 10:46:10 -0400326 struct btrfs_extent_item *item;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400327 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500328
Chris Mason5caf2a02007-04-02 11:20:42 -0400329 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -0400330 if (!path)
331 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -0400332
Chris Mason02217ed2007-03-02 16:08:05 -0500333 key.objectid = blocknr;
Chris Mason62e27492007-03-15 12:56:47 -0400334 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Mason6407bf62007-03-27 06:33:00 -0400335 key.offset = num_blocks;
Chris Mason5caf2a02007-04-02 11:20:42 -0400336 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -0400337 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400338 if (ret < 0)
339 return ret;
Chris Masona429e512007-04-18 16:15:28 -0400340 if (ret != 0) {
Chris Masona28ec192007-03-06 20:08:01 -0500341 BUG();
Chris Masona429e512007-04-18 16:15:28 -0400342 }
Chris Mason02217ed2007-03-02 16:08:05 -0500343 BUG_ON(ret != 0);
Chris Mason5f39d392007-10-15 16:14:19 -0400344 l = path->nodes[0];
Chris Mason5caf2a02007-04-02 11:20:42 -0400345 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400346 refs = btrfs_extent_refs(l, item);
347 btrfs_set_extent_refs(l, item, refs + 1);
Chris Mason5caf2a02007-04-02 11:20:42 -0400348 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masona28ec192007-03-06 20:08:01 -0500349
Chris Mason5caf2a02007-04-02 11:20:42 -0400350 btrfs_release_path(root->fs_info->extent_root, path);
351 btrfs_free_path(path);
Chris Mason9f5fae22007-03-20 14:38:32 -0400352 finish_current_insert(trans, root->fs_info->extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -0400353 del_pending_extents(trans, root->fs_info->extent_root);
Chris Mason02217ed2007-03-02 16:08:05 -0500354 return 0;
355}
356
Chris Masone9d0b132007-08-10 14:06:19 -0400357int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
358 struct btrfs_root *root)
359{
360 finish_current_insert(trans, root->fs_info->extent_root);
361 del_pending_extents(trans, root->fs_info->extent_root);
362 return 0;
363}
364
Chris Masonb18c6682007-04-17 13:26:50 -0400365static int lookup_extent_ref(struct btrfs_trans_handle *trans,
366 struct btrfs_root *root, u64 blocknr,
367 u64 num_blocks, u32 *refs)
Chris Masona28ec192007-03-06 20:08:01 -0500368{
Chris Mason5caf2a02007-04-02 11:20:42 -0400369 struct btrfs_path *path;
Chris Masona28ec192007-03-06 20:08:01 -0500370 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400371 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400372 struct extent_buffer *l;
Chris Mason234b63a2007-03-13 10:46:10 -0400373 struct btrfs_extent_item *item;
Chris Mason5caf2a02007-04-02 11:20:42 -0400374
375 path = btrfs_alloc_path();
Chris Masona28ec192007-03-06 20:08:01 -0500376 key.objectid = blocknr;
Chris Mason6407bf62007-03-27 06:33:00 -0400377 key.offset = num_blocks;
Chris Mason62e27492007-03-15 12:56:47 -0400378 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Mason5caf2a02007-04-02 11:20:42 -0400379 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -0400380 0, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400381 if (ret < 0)
382 goto out;
Chris Mason5f39d392007-10-15 16:14:19 -0400383 if (ret != 0) {
384 btrfs_print_leaf(root, path->nodes[0]);
385 printk("failed to find block number %Lu\n", blocknr);
Chris Masona28ec192007-03-06 20:08:01 -0500386 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -0400387 }
388 l = path->nodes[0];
Chris Mason5caf2a02007-04-02 11:20:42 -0400389 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400390 *refs = btrfs_extent_refs(l, item);
Chris Mason54aa1f42007-06-22 14:16:25 -0400391out:
Chris Mason5caf2a02007-04-02 11:20:42 -0400392 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -0500393 return 0;
394}
395
Chris Masonc5739bb2007-04-10 09:27:04 -0400396int btrfs_inc_root_ref(struct btrfs_trans_handle *trans,
397 struct btrfs_root *root)
398{
Chris Mason5f39d392007-10-15 16:14:19 -0400399 return btrfs_inc_extent_ref(trans, root,
400 extent_buffer_blocknr(root->node), 1);
Chris Masonc5739bb2007-04-10 09:27:04 -0400401}
402
Chris Masone089f052007-03-16 16:20:31 -0400403int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400404 struct extent_buffer *buf)
Chris Mason02217ed2007-03-02 16:08:05 -0500405{
406 u64 blocknr;
Chris Mason5f39d392007-10-15 16:14:19 -0400407 u32 nritems;
408 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -0400409 struct btrfs_file_extent_item *fi;
Chris Mason02217ed2007-03-02 16:08:05 -0500410 int i;
Chris Mason6407bf62007-03-27 06:33:00 -0400411 int leaf;
412 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -0400413 int faili;
414 int err;
Chris Masona28ec192007-03-06 20:08:01 -0500415
Chris Mason3768f362007-03-13 16:47:54 -0400416 if (!root->ref_cows)
Chris Masona28ec192007-03-06 20:08:01 -0500417 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400418
419 leaf = btrfs_is_leaf(buf);
420 nritems = btrfs_header_nritems(buf);
421 for (i = 0; i < nritems; i++) {
Chris Mason6407bf62007-03-27 06:33:00 -0400422 if (leaf) {
Chris Mason3a686372007-05-24 13:35:57 -0400423 u64 disk_blocknr;
Chris Mason5f39d392007-10-15 16:14:19 -0400424 btrfs_item_key_to_cpu(buf, &key, i);
425 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason6407bf62007-03-27 06:33:00 -0400426 continue;
Chris Mason5f39d392007-10-15 16:14:19 -0400427 fi = btrfs_item_ptr(buf, i,
Chris Mason6407bf62007-03-27 06:33:00 -0400428 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400429 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason236454df2007-04-19 13:37:44 -0400430 BTRFS_FILE_EXTENT_INLINE)
431 continue;
Chris Mason5f39d392007-10-15 16:14:19 -0400432 disk_blocknr = btrfs_file_extent_disk_blocknr(buf, fi);
Chris Mason3a686372007-05-24 13:35:57 -0400433 if (disk_blocknr == 0)
434 continue;
435 ret = btrfs_inc_extent_ref(trans, root, disk_blocknr,
Chris Mason5f39d392007-10-15 16:14:19 -0400436 btrfs_file_extent_disk_num_blocks(buf, fi));
Chris Mason54aa1f42007-06-22 14:16:25 -0400437 if (ret) {
438 faili = i;
439 goto fail;
440 }
Chris Mason6407bf62007-03-27 06:33:00 -0400441 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400442 blocknr = btrfs_node_blockptr(buf, i);
Chris Masonb18c6682007-04-17 13:26:50 -0400443 ret = btrfs_inc_extent_ref(trans, root, blocknr, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400444 if (ret) {
445 faili = i;
446 goto fail;
447 }
Chris Mason6407bf62007-03-27 06:33:00 -0400448 }
Chris Mason02217ed2007-03-02 16:08:05 -0500449 }
450 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -0400451fail:
Chris Masonccd467d2007-06-28 15:57:36 -0400452 WARN_ON(1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400453 for (i =0; i < faili; i++) {
454 if (leaf) {
455 u64 disk_blocknr;
Chris Mason5f39d392007-10-15 16:14:19 -0400456 btrfs_item_key_to_cpu(buf, &key, i);
457 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -0400458 continue;
Chris Mason5f39d392007-10-15 16:14:19 -0400459 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -0400460 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400461 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -0400462 BTRFS_FILE_EXTENT_INLINE)
463 continue;
Chris Mason5f39d392007-10-15 16:14:19 -0400464 disk_blocknr = btrfs_file_extent_disk_blocknr(buf, fi);
Chris Mason54aa1f42007-06-22 14:16:25 -0400465 if (disk_blocknr == 0)
466 continue;
467 err = btrfs_free_extent(trans, root, disk_blocknr,
Chris Mason5f39d392007-10-15 16:14:19 -0400468 btrfs_file_extent_disk_num_blocks(buf,
469 fi), 0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400470 BUG_ON(err);
471 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400472 blocknr = btrfs_node_blockptr(buf, i);
Chris Mason54aa1f42007-06-22 14:16:25 -0400473 err = btrfs_free_extent(trans, root, blocknr, 1, 0);
474 BUG_ON(err);
475 }
476 }
477 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -0500478}
479
Chris Mason9078a3e2007-04-26 16:46:15 -0400480static int write_one_cache_group(struct btrfs_trans_handle *trans,
481 struct btrfs_root *root,
482 struct btrfs_path *path,
483 struct btrfs_block_group_cache *cache)
484{
485 int ret;
486 int pending_ret;
487 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -0400488 unsigned long bi;
489 struct extent_buffer *leaf;
Chris Mason9078a3e2007-04-26 16:46:15 -0400490
Chris Mason9078a3e2007-04-26 16:46:15 -0400491 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400492 if (ret < 0)
493 goto fail;
Chris Mason9078a3e2007-04-26 16:46:15 -0400494 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -0400495
496 leaf = path->nodes[0];
497 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
498 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
499 btrfs_mark_buffer_dirty(leaf);
Chris Mason9078a3e2007-04-26 16:46:15 -0400500 btrfs_release_path(extent_root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -0400501fail:
Chris Mason9078a3e2007-04-26 16:46:15 -0400502 finish_current_insert(trans, extent_root);
503 pending_ret = del_pending_extents(trans, extent_root);
504 if (ret)
505 return ret;
506 if (pending_ret)
507 return pending_ret;
Chris Masonbe744172007-05-06 10:15:01 -0400508 if (cache->data)
509 cache->last_alloc = cache->first_free;
Chris Mason9078a3e2007-04-26 16:46:15 -0400510 return 0;
511
512}
513
Chris Mason96b51792007-10-15 16:15:19 -0400514int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
515 struct btrfs_root *root)
Chris Mason9078a3e2007-04-26 16:46:15 -0400516{
Chris Mason96b51792007-10-15 16:15:19 -0400517 struct extent_map_tree *block_group_cache;
518 struct btrfs_block_group_cache *cache;
Chris Mason9078a3e2007-04-26 16:46:15 -0400519 int ret;
520 int err = 0;
521 int werr = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -0400522 struct btrfs_path *path;
Chris Mason96b51792007-10-15 16:15:19 -0400523 u64 last = 0;
524 u64 start;
525 u64 end;
526 u64 ptr;
Chris Mason9078a3e2007-04-26 16:46:15 -0400527
Chris Mason96b51792007-10-15 16:15:19 -0400528 block_group_cache = &root->fs_info->block_group_cache;
Chris Mason9078a3e2007-04-26 16:46:15 -0400529 path = btrfs_alloc_path();
530 if (!path)
531 return -ENOMEM;
532
533 while(1) {
Chris Mason96b51792007-10-15 16:15:19 -0400534 ret = find_first_extent_bit(block_group_cache, last,
535 &start, &end, BLOCK_GROUP_DIRTY);
536 if (ret)
Chris Mason9078a3e2007-04-26 16:46:15 -0400537 break;
Chris Mason54aa1f42007-06-22 14:16:25 -0400538
Chris Mason96b51792007-10-15 16:15:19 -0400539 last = end + 1;
540 ret = get_state_private(block_group_cache, start, &ptr);
541 if (ret)
542 break;
543
544 cache = (struct btrfs_block_group_cache *)ptr;
545 err = write_one_cache_group(trans, root,
546 path, cache);
547 /*
548 * if we fail to write the cache group, we want
549 * to keep it marked dirty in hopes that a later
550 * write will work
551 */
552 if (err) {
553 werr = err;
554 continue;
Chris Mason9078a3e2007-04-26 16:46:15 -0400555 }
Chris Mason96b51792007-10-15 16:15:19 -0400556 clear_extent_bits(block_group_cache, start, end,
557 BLOCK_GROUP_DIRTY, GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -0400558 }
559 btrfs_free_path(path);
560 return werr;
561}
562
563static int update_block_group(struct btrfs_trans_handle *trans,
564 struct btrfs_root *root,
Chris Mason1e2677e2007-05-29 16:52:18 -0400565 u64 blocknr, u64 num, int alloc, int mark_free,
566 int data)
Chris Mason9078a3e2007-04-26 16:46:15 -0400567{
568 struct btrfs_block_group_cache *cache;
569 struct btrfs_fs_info *info = root->fs_info;
570 u64 total = num;
571 u64 old_val;
572 u64 block_in_group;
Chris Mason96b51792007-10-15 16:15:19 -0400573 u64 start;
574 u64 end;
Chris Mason3e1ad542007-05-07 20:03:49 -0400575
Chris Mason9078a3e2007-04-26 16:46:15 -0400576 while(total) {
Chris Mason5276aed2007-06-11 21:33:38 -0400577 cache = btrfs_lookup_block_group(info, blocknr);
Chris Mason3e1ad542007-05-07 20:03:49 -0400578 if (!cache) {
Chris Mason9078a3e2007-04-26 16:46:15 -0400579 return -1;
Chris Masoncd1bc462007-04-27 10:08:34 -0400580 }
Chris Mason9078a3e2007-04-26 16:46:15 -0400581 block_in_group = blocknr - cache->key.objectid;
582 WARN_ON(block_in_group > cache->key.offset);
Chris Mason96b51792007-10-15 16:15:19 -0400583 start = cache->key.objectid;
584 end = start + cache->key.offset - 1;
585 set_extent_bits(&info->block_group_cache, start, end,
586 BLOCK_GROUP_DIRTY, GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -0400587
588 old_val = btrfs_block_group_used(&cache->item);
589 num = min(total, cache->key.offset - block_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -0400590 if (alloc) {
Chris Masoncd1bc462007-04-27 10:08:34 -0400591 if (blocknr > cache->last_alloc)
592 cache->last_alloc = blocknr;
Chris Mason1e2677e2007-05-29 16:52:18 -0400593 if (cache->data != data &&
Chris Mason84f54cf2007-06-12 07:43:08 -0400594 old_val < (cache->key.offset >> 1)) {
Chris Mason96b51792007-10-15 16:15:19 -0400595 int bit_to_clear;
596 int bit_to_set;
Chris Mason1e2677e2007-05-29 16:52:18 -0400597
Chris Mason96b51792007-10-15 16:15:19 -0400598 cache->data = data;
Chris Mason1e2677e2007-05-29 16:52:18 -0400599 if (data) {
Chris Mason96b51792007-10-15 16:15:19 -0400600 bit_to_clear = BLOCK_GROUP_DATA;
601 bit_to_set = BLOCK_GROUP_METADATA;
Chris Mason1e2677e2007-05-29 16:52:18 -0400602 cache->item.flags |=
603 BTRFS_BLOCK_GROUP_DATA;
604 } else {
Chris Mason96b51792007-10-15 16:15:19 -0400605 bit_to_clear = BLOCK_GROUP_METADATA;
606 bit_to_set = BLOCK_GROUP_DATA;
Chris Mason1e2677e2007-05-29 16:52:18 -0400607 cache->item.flags &=
608 ~BTRFS_BLOCK_GROUP_DATA;
609 }
Chris Mason96b51792007-10-15 16:15:19 -0400610 clear_extent_bits(&info->block_group_cache,
611 start, end, bit_to_clear,
612 GFP_NOFS);
613 set_extent_bits(&info->block_group_cache,
614 start, end, bit_to_set,
615 GFP_NOFS);
Chris Mason1e2677e2007-05-29 16:52:18 -0400616 }
617 old_val += num;
Chris Masoncd1bc462007-04-27 10:08:34 -0400618 } else {
Chris Mason9078a3e2007-04-26 16:46:15 -0400619 old_val -= num;
Chris Masoncd1bc462007-04-27 10:08:34 -0400620 if (blocknr < cache->first_free)
621 cache->first_free = blocknr;
Chris Masonf510cfe2007-10-15 16:14:48 -0400622 if (mark_free) {
623 set_extent_dirty(&info->free_space_cache,
624 blocknr, blocknr + num - 1,
625 GFP_NOFS);
Chris Masone37c9e62007-05-09 20:13:14 -0400626 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400627 }
Chris Mason9078a3e2007-04-26 16:46:15 -0400628 btrfs_set_block_group_used(&cache->item, old_val);
Chris Masone37c9e62007-05-09 20:13:14 -0400629 total -= num;
630 blocknr += num;
Chris Mason9078a3e2007-04-26 16:46:15 -0400631 }
632 return 0;
633}
634
Chris Masonccd467d2007-06-28 15:57:36 -0400635int btrfs_copy_pinned(struct btrfs_root *root, struct radix_tree_root *copy)
636{
637 unsigned long gang[8];
638 u64 last = 0;
639 struct radix_tree_root *pinned_radix = &root->fs_info->pinned_radix;
640 int ret;
641 int i;
642
643 while(1) {
644 ret = find_first_radix_bit(pinned_radix, gang, last,
645 ARRAY_SIZE(gang));
646 if (!ret)
647 break;
648 for (i = 0 ; i < ret; i++) {
649 set_radix_bit(copy, gang[i]);
650 last = gang[i] + 1;
651 }
652 }
Chris Mason26b80032007-08-08 20:17:12 -0400653 ret = find_first_radix_bit(&root->fs_info->extent_ins_radix, gang, 0,
654 ARRAY_SIZE(gang));
655 WARN_ON(ret);
Chris Masonccd467d2007-06-28 15:57:36 -0400656 return 0;
657}
658
659int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
660 struct btrfs_root *root,
661 struct radix_tree_root *unpin_radix)
Chris Masona28ec192007-03-06 20:08:01 -0500662{
Chris Mason8ef97622007-03-26 10:15:30 -0400663 unsigned long gang[8];
Chris Masonbe744172007-05-06 10:15:01 -0400664 struct btrfs_block_group_cache *block_group;
Chris Mason88fd1462007-03-16 08:56:18 -0400665 u64 first = 0;
Chris Masona28ec192007-03-06 20:08:01 -0500666 int ret;
667 int i;
Chris Mason8ef97622007-03-26 10:15:30 -0400668 struct radix_tree_root *pinned_radix = &root->fs_info->pinned_radix;
Chris Masonf510cfe2007-10-15 16:14:48 -0400669 struct extent_map_tree *free_space_cache;
670
671 free_space_cache = &root->fs_info->free_space_cache;
Chris Masona28ec192007-03-06 20:08:01 -0500672
673 while(1) {
Chris Masonccd467d2007-06-28 15:57:36 -0400674 ret = find_first_radix_bit(unpin_radix, gang, 0,
Chris Mason8ef97622007-03-26 10:15:30 -0400675 ARRAY_SIZE(gang));
Chris Masona28ec192007-03-06 20:08:01 -0500676 if (!ret)
677 break;
Chris Mason88fd1462007-03-16 08:56:18 -0400678 if (!first)
Chris Mason8ef97622007-03-26 10:15:30 -0400679 first = gang[0];
Chris Mason0579da42007-03-07 16:15:30 -0500680 for (i = 0; i < ret; i++) {
Chris Mason8ef97622007-03-26 10:15:30 -0400681 clear_radix_bit(pinned_radix, gang[i]);
Chris Masonccd467d2007-06-28 15:57:36 -0400682 clear_radix_bit(unpin_radix, gang[i]);
Chris Mason5276aed2007-06-11 21:33:38 -0400683 block_group = btrfs_lookup_block_group(root->fs_info,
684 gang[i]);
Chris Masonbe744172007-05-06 10:15:01 -0400685 if (block_group) {
686 WARN_ON(block_group->pinned == 0);
687 block_group->pinned--;
688 if (gang[i] < block_group->last_alloc)
689 block_group->last_alloc = gang[i];
Chris Mason96b51792007-10-15 16:15:19 -0400690 set_extent_dirty(free_space_cache,
691 gang[i], gang[i], GFP_NOFS);
Chris Masonbe744172007-05-06 10:15:01 -0400692 }
Chris Mason0579da42007-03-07 16:15:30 -0500693 }
Chris Masona28ec192007-03-06 20:08:01 -0500694 }
695 return 0;
696}
697
Chris Masone089f052007-03-16 16:20:31 -0400698static int finish_current_insert(struct btrfs_trans_handle *trans, struct
699 btrfs_root *extent_root)
Chris Mason037e6392007-03-07 11:50:24 -0500700{
Chris Masone2fa7222007-03-12 16:22:34 -0400701 struct btrfs_key ins;
Chris Mason234b63a2007-03-13 10:46:10 -0400702 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -0500703 int i;
704 int ret;
Chris Mason26b80032007-08-08 20:17:12 -0400705 int err;
706 unsigned long gang[8];
Chris Mason1261ec42007-03-20 20:35:03 -0400707 struct btrfs_fs_info *info = extent_root->fs_info;
Chris Mason037e6392007-03-07 11:50:24 -0500708
Chris Mason5f39d392007-10-15 16:14:19 -0400709 btrfs_set_stack_extent_refs(&extent_item, 1);
Chris Mason037e6392007-03-07 11:50:24 -0500710 ins.offset = 1;
Chris Mason62e27492007-03-15 12:56:47 -0400711 btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
Chris Mason5f39d392007-10-15 16:14:19 -0400712 btrfs_set_stack_extent_owner(&extent_item,
713 extent_root->root_key.objectid);
Chris Mason037e6392007-03-07 11:50:24 -0500714
Chris Mason26b80032007-08-08 20:17:12 -0400715 while(1) {
716 ret = find_first_radix_bit(&info->extent_ins_radix, gang, 0,
717 ARRAY_SIZE(gang));
718 if (!ret)
719 break;
720
721 for (i = 0; i < ret; i++) {
722 ins.objectid = gang[i];
723 err = btrfs_insert_item(trans, extent_root, &ins,
724 &extent_item,
725 sizeof(extent_item));
726 clear_radix_bit(&info->extent_ins_radix, gang[i]);
727 WARN_ON(err);
728 }
Chris Mason037e6392007-03-07 11:50:24 -0500729 }
Chris Mason037e6392007-03-07 11:50:24 -0500730 return 0;
731}
732
Chris Mason8ef97622007-03-26 10:15:30 -0400733static int pin_down_block(struct btrfs_root *root, u64 blocknr, int pending)
Chris Masone20d96d2007-03-22 12:13:20 -0400734{
735 int err;
Chris Mason5f39d392007-10-15 16:14:19 -0400736 struct extent_buffer *buf;
Chris Mason78fae272007-03-25 11:35:08 -0400737
Chris Masonf4b9aa82007-03-27 11:05:53 -0400738 if (!pending) {
Chris Mason5f39d392007-10-15 16:14:19 -0400739 buf = btrfs_find_tree_block(root, blocknr);
740 if (buf) {
741 if (btrfs_buffer_uptodate(buf)) {
Chris Mason2c90e5d2007-04-02 10:50:19 -0400742 u64 transid =
743 root->fs_info->running_transaction->transid;
Chris Mason5f39d392007-10-15 16:14:19 -0400744 if (btrfs_header_generation(buf) == transid) {
745 free_extent_buffer(buf);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400746 return 0;
747 }
Chris Masonf4b9aa82007-03-27 11:05:53 -0400748 }
Chris Mason5f39d392007-10-15 16:14:19 -0400749 free_extent_buffer(buf);
Chris Mason8ef97622007-03-26 10:15:30 -0400750 }
Chris Mason8ef97622007-03-26 10:15:30 -0400751 err = set_radix_bit(&root->fs_info->pinned_radix, blocknr);
Chris Masonbe744172007-05-06 10:15:01 -0400752 if (!err) {
753 struct btrfs_block_group_cache *cache;
Chris Mason5276aed2007-06-11 21:33:38 -0400754 cache = btrfs_lookup_block_group(root->fs_info,
755 blocknr);
Chris Masonbe744172007-05-06 10:15:01 -0400756 if (cache)
757 cache->pinned++;
758 }
Chris Masonf4b9aa82007-03-27 11:05:53 -0400759 } else {
760 err = set_radix_bit(&root->fs_info->pending_del_radix, blocknr);
761 }
Chris Masonbe744172007-05-06 10:15:01 -0400762 BUG_ON(err < 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400763 return 0;
764}
765
Chris Masona28ec192007-03-06 20:08:01 -0500766/*
767 * remove an extent from the root, returns 0 on success
768 */
Chris Masone089f052007-03-16 16:20:31 -0400769static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masone37c9e62007-05-09 20:13:14 -0400770 *root, u64 blocknr, u64 num_blocks, int pin,
771 int mark_free)
Chris Masona28ec192007-03-06 20:08:01 -0500772{
Chris Mason5caf2a02007-04-02 11:20:42 -0400773 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -0400774 struct btrfs_key key;
Chris Mason1261ec42007-03-20 20:35:03 -0400775 struct btrfs_fs_info *info = root->fs_info;
776 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -0400777 struct extent_buffer *leaf;
Chris Masona28ec192007-03-06 20:08:01 -0500778 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400779 struct btrfs_extent_item *ei;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400780 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500781
Chris Masona28ec192007-03-06 20:08:01 -0500782 key.objectid = blocknr;
Chris Mason62e27492007-03-15 12:56:47 -0400783 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masona28ec192007-03-06 20:08:01 -0500784 key.offset = num_blocks;
785
Chris Mason5caf2a02007-04-02 11:20:42 -0400786 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -0400787 if (!path)
788 return -ENOMEM;
789
Chris Mason5caf2a02007-04-02 11:20:42 -0400790 ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400791 if (ret < 0)
792 return ret;
793 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -0400794
795 leaf = path->nodes[0];
796 ei = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason123abc82007-03-14 14:14:43 -0400797 struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400798 refs = btrfs_extent_refs(leaf, ei);
799 BUG_ON(refs == 0);
800 refs -= 1;
801 btrfs_set_extent_refs(leaf, ei, refs);
802 btrfs_mark_buffer_dirty(leaf);
803
Chris Masoncf27e1e2007-03-13 09:49:06 -0400804 if (refs == 0) {
Josef Bacik58176a92007-08-29 15:47:34 -0400805 u64 super_blocks_used, root_blocks_used;
Chris Mason78fae272007-03-25 11:35:08 -0400806
807 if (pin) {
Chris Mason8ef97622007-03-26 10:15:30 -0400808 ret = pin_down_block(root, blocknr, 0);
Chris Mason78fae272007-03-25 11:35:08 -0400809 BUG_ON(ret);
810 }
811
Josef Bacik58176a92007-08-29 15:47:34 -0400812 /* block accounting for super block */
Chris Mason4b52dff2007-06-26 10:06:50 -0400813 super_blocks_used = btrfs_super_blocks_used(&info->super_copy);
814 btrfs_set_super_blocks_used(&info->super_copy,
Chris Mason1261ec42007-03-20 20:35:03 -0400815 super_blocks_used - num_blocks);
Josef Bacik58176a92007-08-29 15:47:34 -0400816
817 /* block accounting for root item */
Chris Mason5f39d392007-10-15 16:14:19 -0400818 root_blocks_used = btrfs_root_used(&root->root_item);
819 btrfs_set_root_used(&root->root_item,
Josef Bacik58176a92007-08-29 15:47:34 -0400820 root_blocks_used - num_blocks);
821
Chris Mason5caf2a02007-04-02 11:20:42 -0400822 ret = btrfs_del_item(trans, extent_root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -0400823 if (ret) {
824 return ret;
825 }
Chris Masone37c9e62007-05-09 20:13:14 -0400826 ret = update_block_group(trans, root, blocknr, num_blocks, 0,
Chris Mason1e2677e2007-05-29 16:52:18 -0400827 mark_free, 0);
Chris Mason9078a3e2007-04-26 16:46:15 -0400828 BUG_ON(ret);
Chris Masona28ec192007-03-06 20:08:01 -0500829 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400830 btrfs_free_path(path);
Chris Masone089f052007-03-16 16:20:31 -0400831 finish_current_insert(trans, extent_root);
Chris Masona28ec192007-03-06 20:08:01 -0500832 return ret;
833}
834
835/*
Chris Masonfec577f2007-02-26 10:40:21 -0500836 * find all the blocks marked as pending in the radix tree and remove
837 * them from the extent map
838 */
Chris Masone089f052007-03-16 16:20:31 -0400839static int del_pending_extents(struct btrfs_trans_handle *trans, struct
840 btrfs_root *extent_root)
Chris Masonfec577f2007-02-26 10:40:21 -0500841{
842 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -0400843 int wret;
844 int err = 0;
Chris Mason8ef97622007-03-26 10:15:30 -0400845 unsigned long gang[4];
Chris Masonfec577f2007-02-26 10:40:21 -0500846 int i;
Chris Mason8ef97622007-03-26 10:15:30 -0400847 struct radix_tree_root *pending_radix;
848 struct radix_tree_root *pinned_radix;
Chris Masonbe744172007-05-06 10:15:01 -0400849 struct btrfs_block_group_cache *cache;
Chris Mason8ef97622007-03-26 10:15:30 -0400850
851 pending_radix = &extent_root->fs_info->pending_del_radix;
852 pinned_radix = &extent_root->fs_info->pinned_radix;
Chris Masonfec577f2007-02-26 10:40:21 -0500853
854 while(1) {
Chris Masone37c9e62007-05-09 20:13:14 -0400855 ret = find_first_radix_bit(pending_radix, gang, 0,
Chris Mason8ef97622007-03-26 10:15:30 -0400856 ARRAY_SIZE(gang));
Chris Masonfec577f2007-02-26 10:40:21 -0500857 if (!ret)
858 break;
859 for (i = 0; i < ret; i++) {
Chris Mason8ef97622007-03-26 10:15:30 -0400860 wret = set_radix_bit(pinned_radix, gang[i]);
Chris Masonbe744172007-05-06 10:15:01 -0400861 if (wret == 0) {
Chris Mason5276aed2007-06-11 21:33:38 -0400862 cache =
863 btrfs_lookup_block_group(extent_root->fs_info,
Chris Masonbe744172007-05-06 10:15:01 -0400864 gang[i]);
865 if (cache)
866 cache->pinned++;
867 }
868 if (wret < 0) {
869 printk(KERN_CRIT "set_radix_bit, err %d\n",
870 wret);
871 BUG_ON(wret < 0);
872 }
Chris Mason8ef97622007-03-26 10:15:30 -0400873 wret = clear_radix_bit(pending_radix, gang[i]);
874 BUG_ON(wret);
Chris Masond5719762007-03-23 10:01:08 -0400875 wret = __free_extent(trans, extent_root,
Chris Masone37c9e62007-05-09 20:13:14 -0400876 gang[i], 1, 0, 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400877 if (wret)
878 err = wret;
Chris Masonfec577f2007-02-26 10:40:21 -0500879 }
880 }
Chris Masone20d96d2007-03-22 12:13:20 -0400881 return err;
Chris Masonfec577f2007-02-26 10:40:21 -0500882}
883
884/*
885 * remove an extent from the root, returns 0 on success
886 */
Chris Masone089f052007-03-16 16:20:31 -0400887int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
888 *root, u64 blocknr, u64 num_blocks, int pin)
Chris Masonfec577f2007-02-26 10:40:21 -0500889{
Chris Mason9f5fae22007-03-20 14:38:32 -0400890 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Masonfec577f2007-02-26 10:40:21 -0500891 int pending_ret;
892 int ret;
Chris Masona28ec192007-03-06 20:08:01 -0500893
894 if (root == extent_root) {
Chris Mason8ef97622007-03-26 10:15:30 -0400895 pin_down_block(root, blocknr, 1);
Chris Masona28ec192007-03-06 20:08:01 -0500896 return 0;
897 }
Chris Masone37c9e62007-05-09 20:13:14 -0400898 ret = __free_extent(trans, root, blocknr, num_blocks, pin, pin == 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400899 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
Chris Masonfec577f2007-02-26 10:40:21 -0500900 return ret ? ret : pending_ret;
901}
902
903/*
904 * walks the btree of allocated extents and find a hole of a given size.
905 * The key ins is changed to record the hole:
906 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -0400907 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -0500908 * ins->offset == number of blocks
909 * Any available blocks before search_start are skipped.
910 */
Chris Masone089f052007-03-16 16:20:31 -0400911static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason6702ed42007-08-07 16:15:09 -0400912 *orig_root, u64 num_blocks, u64 empty_size,
913 u64 search_start, u64 search_end, u64 hint_block,
Chris Masonf2654de2007-06-26 12:20:46 -0400914 struct btrfs_key *ins, u64 exclude_start,
915 u64 exclude_nr, int data)
Chris Masonfec577f2007-02-26 10:40:21 -0500916{
Chris Mason5caf2a02007-04-02 11:20:42 -0400917 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -0400918 struct btrfs_key key;
Chris Masonfec577f2007-02-26 10:40:21 -0500919 int ret;
920 u64 hole_size = 0;
921 int slot = 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400922 u64 last_block = 0;
Chris Mason037e6392007-03-07 11:50:24 -0500923 u64 test_block;
Chris Masonbe744172007-05-06 10:15:01 -0400924 u64 orig_search_start = search_start;
Chris Masonfec577f2007-02-26 10:40:21 -0500925 int start_found;
Chris Mason5f39d392007-10-15 16:14:19 -0400926 struct extent_buffer *l;
Chris Mason9f5fae22007-03-20 14:38:32 -0400927 struct btrfs_root * root = orig_root->fs_info->extent_root;
Chris Masonf2458e12007-04-25 15:52:25 -0400928 struct btrfs_fs_info *info = root->fs_info;
Chris Mason0579da42007-03-07 16:15:30 -0500929 int total_needed = num_blocks;
Chris Masone20d96d2007-03-22 12:13:20 -0400930 int level;
Chris Masonbe08c1b2007-05-03 09:06:49 -0400931 struct btrfs_block_group_cache *block_group;
Chris Masonbe744172007-05-06 10:15:01 -0400932 int full_scan = 0;
Chris Masonfbdc7622007-05-30 10:22:12 -0400933 int wrapped = 0;
Chris Masonf510cfe2007-10-15 16:14:48 -0400934 u64 cached_search_start = 0;
Chris Masonfec577f2007-02-26 10:40:21 -0500935
Chris Mason26b80032007-08-08 20:17:12 -0400936 WARN_ON(num_blocks < 1);
Chris Masonb1a4d962007-04-04 15:27:52 -0400937 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
938
Chris Mason5f39d392007-10-15 16:14:19 -0400939 level = btrfs_header_level(root->node);
940
Chris Mason3e1ad542007-05-07 20:03:49 -0400941 if (search_end == (u64)-1)
Chris Mason4b52dff2007-06-26 10:06:50 -0400942 search_end = btrfs_super_total_blocks(&info->super_copy);
Chris Masonfbdc7622007-05-30 10:22:12 -0400943 if (hint_block) {
Chris Mason5276aed2007-06-11 21:33:38 -0400944 block_group = btrfs_lookup_block_group(info, hint_block);
Chris Masonbe744172007-05-06 10:15:01 -0400945 block_group = btrfs_find_block_group(root, block_group,
Chris Masonfbdc7622007-05-30 10:22:12 -0400946 hint_block, data, 1);
Chris Masonbe744172007-05-06 10:15:01 -0400947 } else {
948 block_group = btrfs_find_block_group(root,
949 trans->block_group, 0,
Chris Masonde428b62007-05-18 13:28:27 -0400950 data, 1);
Chris Masonbe744172007-05-06 10:15:01 -0400951 }
952
Chris Mason6702ed42007-08-07 16:15:09 -0400953 total_needed += empty_size;
Chris Masone0115992007-06-19 16:23:05 -0400954 path = btrfs_alloc_path();
955
Chris Masonbe744172007-05-06 10:15:01 -0400956check_failed:
Chris Masonf510cfe2007-10-15 16:14:48 -0400957 search_start = find_search_start(root, &block_group,
958 search_start, total_needed, data);
959 cached_search_start = search_start;
Chris Masone37c9e62007-05-09 20:13:14 -0400960
Chris Mason5caf2a02007-04-02 11:20:42 -0400961 btrfs_init_path(path);
Chris Masonfec577f2007-02-26 10:40:21 -0500962 ins->objectid = search_start;
963 ins->offset = 0;
Chris Masonfec577f2007-02-26 10:40:21 -0500964 start_found = 0;
Chris Mason2cc58cf2007-08-27 16:49:44 -0400965 path->reada = 2;
Chris Masone37c9e62007-05-09 20:13:14 -0400966
Chris Mason5caf2a02007-04-02 11:20:42 -0400967 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
Chris Mason0f70abe2007-02-28 16:46:22 -0500968 if (ret < 0)
969 goto error;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500970
Chris Masone37c9e62007-05-09 20:13:14 -0400971 if (path->slots[0] > 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400972 path->slots[0]--;
Chris Masone37c9e62007-05-09 20:13:14 -0400973 }
974
Chris Mason5f39d392007-10-15 16:14:19 -0400975 l = path->nodes[0];
976 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
977
Chris Masone37c9e62007-05-09 20:13:14 -0400978 /*
979 * a rare case, go back one key if we hit a block group item
980 * instead of an extent item
981 */
982 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY &&
983 key.objectid + key.offset >= search_start) {
984 ins->objectid = key.objectid;
985 ins->offset = key.offset - 1;
986 btrfs_release_path(root, path);
987 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
988 if (ret < 0)
989 goto error;
990
991 if (path->slots[0] > 0) {
992 path->slots[0]--;
993 }
994 }
Chris Mason0579da42007-03-07 16:15:30 -0500995
Chris Masonfec577f2007-02-26 10:40:21 -0500996 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400997 l = path->nodes[0];
Chris Mason5caf2a02007-04-02 11:20:42 -0400998 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -0400999 if (slot >= btrfs_header_nritems(l)) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001000 ret = btrfs_next_leaf(root, path);
Chris Masonfec577f2007-02-26 10:40:21 -05001001 if (ret == 0)
1002 continue;
Chris Mason0f70abe2007-02-28 16:46:22 -05001003 if (ret < 0)
1004 goto error;
Chris Masonfec577f2007-02-26 10:40:21 -05001005 if (!start_found) {
1006 ins->objectid = search_start;
Chris Mason3e1ad542007-05-07 20:03:49 -04001007 ins->offset = search_end - search_start;
Chris Masonfec577f2007-02-26 10:40:21 -05001008 start_found = 1;
1009 goto check_pending;
1010 }
1011 ins->objectid = last_block > search_start ?
1012 last_block : search_start;
Chris Mason3e1ad542007-05-07 20:03:49 -04001013 ins->offset = search_end - ins->objectid;
Chris Masonfec577f2007-02-26 10:40:21 -05001014 goto check_pending;
1015 }
Chris Mason5f39d392007-10-15 16:14:19 -04001016 btrfs_item_key_to_cpu(l, &key, slot);
Chris Mason96b51792007-10-15 16:15:19 -04001017
Chris Masone37c9e62007-05-09 20:13:14 -04001018 if (key.objectid >= search_start && key.objectid > last_block &&
1019 start_found) {
1020 if (last_block < search_start)
1021 last_block = search_start;
1022 hole_size = key.objectid - last_block;
1023 if (hole_size >= num_blocks) {
1024 ins->objectid = last_block;
1025 ins->offset = hole_size;
1026 goto check_pending;
Chris Mason0579da42007-03-07 16:15:30 -05001027 }
Chris Masonfec577f2007-02-26 10:40:21 -05001028 }
Chris Mason96b51792007-10-15 16:15:19 -04001029 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY) {
1030 if (!start_found) {
1031 last_block = key.objectid;
1032 start_found = 1;
1033 }
Chris Masone37c9e62007-05-09 20:13:14 -04001034 goto next;
Chris Mason96b51792007-10-15 16:15:19 -04001035 }
1036
Chris Masone37c9e62007-05-09 20:13:14 -04001037
Chris Mason0579da42007-03-07 16:15:30 -05001038 start_found = 1;
Chris Masone2fa7222007-03-12 16:22:34 -04001039 last_block = key.objectid + key.offset;
Chris Masonf510cfe2007-10-15 16:14:48 -04001040
Chris Masonfbdc7622007-05-30 10:22:12 -04001041 if (!full_scan && last_block >= block_group->key.objectid +
Chris Masonbe744172007-05-06 10:15:01 -04001042 block_group->key.offset) {
1043 btrfs_release_path(root, path);
1044 search_start = block_group->key.objectid +
1045 block_group->key.offset * 2;
1046 goto new_group;
1047 }
Chris Mason9078a3e2007-04-26 16:46:15 -04001048next:
Chris Mason5caf2a02007-04-02 11:20:42 -04001049 path->slots[0]++;
Chris Masonde428b62007-05-18 13:28:27 -04001050 cond_resched();
Chris Masonfec577f2007-02-26 10:40:21 -05001051 }
Chris Masonfec577f2007-02-26 10:40:21 -05001052check_pending:
1053 /* we have to make sure we didn't find an extent that has already
1054 * been allocated by the map tree or the original allocation
1055 */
Chris Mason5caf2a02007-04-02 11:20:42 -04001056 btrfs_release_path(root, path);
Chris Masonfec577f2007-02-26 10:40:21 -05001057 BUG_ON(ins->objectid < search_start);
Chris Masone37c9e62007-05-09 20:13:14 -04001058
Chris Masoncf675822007-09-17 11:00:51 -04001059 if (ins->objectid + num_blocks >= search_end)
1060 goto enospc;
1061
Chris Mason037e6392007-03-07 11:50:24 -05001062 for (test_block = ins->objectid;
Chris Masonf2458e12007-04-25 15:52:25 -04001063 test_block < ins->objectid + num_blocks; test_block++) {
Chris Mason26b80032007-08-08 20:17:12 -04001064 if (test_radix_bit(&info->pinned_radix, test_block) ||
1065 test_radix_bit(&info->extent_ins_radix, test_block)) {
Chris Mason037e6392007-03-07 11:50:24 -05001066 search_start = test_block + 1;
Chris Masonbe744172007-05-06 10:15:01 -04001067 goto new_group;
Chris Masonfec577f2007-02-26 10:40:21 -05001068 }
1069 }
Chris Masonf2654de2007-06-26 12:20:46 -04001070 if (exclude_nr > 0 && (ins->objectid + num_blocks > exclude_start &&
1071 ins->objectid < exclude_start + exclude_nr)) {
1072 search_start = exclude_start + exclude_nr;
1073 goto new_group;
1074 }
Chris Masone37c9e62007-05-09 20:13:14 -04001075 if (!data) {
Chris Mason5276aed2007-06-11 21:33:38 -04001076 block_group = btrfs_lookup_block_group(info, ins->objectid);
Chris Mason26b80032007-08-08 20:17:12 -04001077 if (block_group)
1078 trans->block_group = block_group;
Chris Masoncd1bc462007-04-27 10:08:34 -04001079 }
Chris Mason037e6392007-03-07 11:50:24 -05001080 ins->offset = num_blocks;
Chris Mason5caf2a02007-04-02 11:20:42 -04001081 btrfs_free_path(path);
Chris Masonfec577f2007-02-26 10:40:21 -05001082 return 0;
Chris Masonbe744172007-05-06 10:15:01 -04001083
1084new_group:
Chris Mason3e1ad542007-05-07 20:03:49 -04001085 if (search_start + num_blocks >= search_end) {
Chris Masoncf675822007-09-17 11:00:51 -04001086enospc:
Chris Masonbe744172007-05-06 10:15:01 -04001087 search_start = orig_search_start;
Chris Masonfbdc7622007-05-30 10:22:12 -04001088 if (full_scan) {
1089 ret = -ENOSPC;
1090 goto error;
1091 }
Chris Mason6702ed42007-08-07 16:15:09 -04001092 if (wrapped) {
1093 if (!full_scan)
1094 total_needed -= empty_size;
Chris Masonfbdc7622007-05-30 10:22:12 -04001095 full_scan = 1;
Chris Mason6702ed42007-08-07 16:15:09 -04001096 } else
Chris Masonfbdc7622007-05-30 10:22:12 -04001097 wrapped = 1;
Chris Masonbe744172007-05-06 10:15:01 -04001098 }
Chris Mason5276aed2007-06-11 21:33:38 -04001099 block_group = btrfs_lookup_block_group(info, search_start);
Chris Masonfbdc7622007-05-30 10:22:12 -04001100 cond_resched();
Chris Masonbe744172007-05-06 10:15:01 -04001101 if (!full_scan)
1102 block_group = btrfs_find_block_group(root, block_group,
Chris Masonde428b62007-05-18 13:28:27 -04001103 search_start, data, 0);
Chris Masonbe744172007-05-06 10:15:01 -04001104 goto check_failed;
1105
Chris Mason0f70abe2007-02-28 16:46:22 -05001106error:
Chris Mason5caf2a02007-04-02 11:20:42 -04001107 btrfs_release_path(root, path);
1108 btrfs_free_path(path);
Chris Mason0f70abe2007-02-28 16:46:22 -05001109 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05001110}
Chris Masonfec577f2007-02-26 10:40:21 -05001111/*
Chris Masonfec577f2007-02-26 10:40:21 -05001112 * finds a free extent and does all the dirty work required for allocation
1113 * returns the key for the extent through ins, and a tree buffer for
1114 * the first block of the extent through buf.
1115 *
1116 * returns 0 if everything worked, non-zero otherwise.
1117 */
Chris Mason4d775672007-04-20 20:23:12 -04001118int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
1119 struct btrfs_root *root, u64 owner,
Chris Mason6702ed42007-08-07 16:15:09 -04001120 u64 num_blocks, u64 empty_size, u64 hint_block,
Chris Masonbe08c1b2007-05-03 09:06:49 -04001121 u64 search_end, struct btrfs_key *ins, int data)
Chris Masonfec577f2007-02-26 10:40:21 -05001122{
1123 int ret;
1124 int pending_ret;
Josef Bacik58176a92007-08-29 15:47:34 -04001125 u64 super_blocks_used, root_blocks_used;
Chris Masonfbdc7622007-05-30 10:22:12 -04001126 u64 search_start = 0;
Chris Mason1261ec42007-03-20 20:35:03 -04001127 struct btrfs_fs_info *info = root->fs_info;
1128 struct btrfs_root *extent_root = info->extent_root;
Chris Mason234b63a2007-03-13 10:46:10 -04001129 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -05001130
Chris Mason5f39d392007-10-15 16:14:19 -04001131 btrfs_set_stack_extent_refs(&extent_item, 1);
1132 btrfs_set_stack_extent_owner(&extent_item, owner);
Chris Masonfec577f2007-02-26 10:40:21 -05001133
Chris Mason26b80032007-08-08 20:17:12 -04001134 WARN_ON(num_blocks < 1);
Chris Mason6702ed42007-08-07 16:15:09 -04001135 ret = find_free_extent(trans, root, num_blocks, empty_size,
1136 search_start, search_end, hint_block, ins,
Chris Mason26b80032007-08-08 20:17:12 -04001137 trans->alloc_exclude_start,
1138 trans->alloc_exclude_nr, data);
Chris Masonccd467d2007-06-28 15:57:36 -04001139 BUG_ON(ret);
Chris Masonf2654de2007-06-26 12:20:46 -04001140 if (ret)
1141 return ret;
1142
Josef Bacik58176a92007-08-29 15:47:34 -04001143 /* block accounting for super block */
Chris Mason4b52dff2007-06-26 10:06:50 -04001144 super_blocks_used = btrfs_super_blocks_used(&info->super_copy);
1145 btrfs_set_super_blocks_used(&info->super_copy, super_blocks_used +
Chris Mason1261ec42007-03-20 20:35:03 -04001146 num_blocks);
Chris Mason26b80032007-08-08 20:17:12 -04001147
Josef Bacik58176a92007-08-29 15:47:34 -04001148 /* block accounting for root item */
Chris Mason5f39d392007-10-15 16:14:19 -04001149 root_blocks_used = btrfs_root_used(&root->root_item);
1150 btrfs_set_root_used(&root->root_item, root_blocks_used +
Josef Bacik58176a92007-08-29 15:47:34 -04001151 num_blocks);
1152
Chris Masonf510cfe2007-10-15 16:14:48 -04001153 clear_extent_dirty(&root->fs_info->free_space_cache,
1154 ins->objectid, ins->objectid + ins->offset - 1,
1155 GFP_NOFS);
1156
Chris Mason26b80032007-08-08 20:17:12 -04001157 if (root == extent_root) {
1158 BUG_ON(num_blocks != 1);
1159 set_radix_bit(&root->fs_info->extent_ins_radix, ins->objectid);
1160 goto update_block;
1161 }
1162
1163 WARN_ON(trans->alloc_exclude_nr);
1164 trans->alloc_exclude_start = ins->objectid;
1165 trans->alloc_exclude_nr = ins->offset;
Chris Masone089f052007-03-16 16:20:31 -04001166 ret = btrfs_insert_item(trans, extent_root, ins, &extent_item,
1167 sizeof(extent_item));
Chris Mason037e6392007-03-07 11:50:24 -05001168
Chris Mason26b80032007-08-08 20:17:12 -04001169 trans->alloc_exclude_start = 0;
1170 trans->alloc_exclude_nr = 0;
1171
Chris Masonccd467d2007-06-28 15:57:36 -04001172 BUG_ON(ret);
Chris Masone089f052007-03-16 16:20:31 -04001173 finish_current_insert(trans, extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -04001174 pending_ret = del_pending_extents(trans, extent_root);
Chris Masonf510cfe2007-10-15 16:14:48 -04001175
Chris Masone37c9e62007-05-09 20:13:14 -04001176 if (ret) {
Chris Mason037e6392007-03-07 11:50:24 -05001177 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -04001178 }
1179 if (pending_ret) {
Chris Mason037e6392007-03-07 11:50:24 -05001180 return pending_ret;
Chris Masone37c9e62007-05-09 20:13:14 -04001181 }
Chris Mason26b80032007-08-08 20:17:12 -04001182
1183update_block:
Chris Mason1e2677e2007-05-29 16:52:18 -04001184 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0,
1185 data);
Chris Masonfabb5682007-06-07 22:13:21 -04001186 BUG_ON(ret);
Chris Mason037e6392007-03-07 11:50:24 -05001187 return 0;
Chris Masonfec577f2007-02-26 10:40:21 -05001188}
1189
1190/*
1191 * helper function to allocate a block for a given tree
1192 * returns the tree buffer or NULL.
1193 */
Chris Mason5f39d392007-10-15 16:14:19 -04001194struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
1195 struct btrfs_root *root, u64 hint,
1196 u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05001197{
Chris Masone2fa7222007-03-12 16:22:34 -04001198 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -05001199 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04001200 struct extent_buffer *buf;
Chris Masonfec577f2007-02-26 10:40:21 -05001201
Chris Mason4d775672007-04-20 20:23:12 -04001202 ret = btrfs_alloc_extent(trans, root, root->root_key.objectid,
Yane9fe3952007-08-29 09:11:44 -04001203 1, empty_size, hint, (u64)-1, &ins, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05001204 if (ret) {
Chris Mason54aa1f42007-06-22 14:16:25 -04001205 BUG_ON(ret > 0);
1206 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05001207 }
Chris Masond98237b2007-03-28 13:57:48 -04001208 buf = btrfs_find_create_tree_block(root, ins.objectid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001209 if (!buf) {
1210 btrfs_free_extent(trans, root, ins.objectid, 1, 0);
1211 return ERR_PTR(-ENOMEM);
1212 }
Chris Mason5f39d392007-10-15 16:14:19 -04001213 btrfs_set_buffer_uptodate(buf);
Chris Masonf510cfe2007-10-15 16:14:48 -04001214 buf->alloc_addr = (unsigned long)__builtin_return_address(0);
Chris Mason5f39d392007-10-15 16:14:19 -04001215 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
1216 buf->start + buf->len - 1, GFP_NOFS);
1217 /*
Chris Mason090d1872007-05-01 08:53:32 -04001218 set_buffer_checked(buf);
Chris Masonf2183bd2007-08-10 14:42:37 -04001219 set_buffer_defrag(buf);
Chris Mason5f39d392007-10-15 16:14:19 -04001220 */
1221 /* FIXME!!!!!!!!!!!!!!!!
1222 set_radix_bit(&trans->transaction->dirty_pages, buf->pages[0]->index);
1223 */
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001224 trans->blocks_used++;
Chris Masonfec577f2007-02-26 10:40:21 -05001225 return buf;
1226}
Chris Masona28ec192007-03-06 20:08:01 -05001227
Chris Mason6407bf62007-03-27 06:33:00 -04001228static int drop_leaf_ref(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001229 struct btrfs_root *root, struct extent_buffer *leaf)
Chris Mason6407bf62007-03-27 06:33:00 -04001230{
Chris Mason5f39d392007-10-15 16:14:19 -04001231 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -04001232 struct btrfs_file_extent_item *fi;
1233 int i;
1234 int nritems;
1235 int ret;
1236
Chris Mason5f39d392007-10-15 16:14:19 -04001237 BUG_ON(!btrfs_is_leaf(leaf));
1238 nritems = btrfs_header_nritems(leaf);
Chris Mason6407bf62007-03-27 06:33:00 -04001239 for (i = 0; i < nritems; i++) {
Chris Mason3a686372007-05-24 13:35:57 -04001240 u64 disk_blocknr;
Chris Mason5f39d392007-10-15 16:14:19 -04001241
1242 btrfs_item_key_to_cpu(leaf, &key, i);
1243 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason6407bf62007-03-27 06:33:00 -04001244 continue;
1245 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001246 if (btrfs_file_extent_type(leaf, fi) ==
1247 BTRFS_FILE_EXTENT_INLINE)
Chris Mason236454df2007-04-19 13:37:44 -04001248 continue;
Chris Mason6407bf62007-03-27 06:33:00 -04001249 /*
1250 * FIXME make sure to insert a trans record that
1251 * repeats the snapshot del on crash
1252 */
Chris Mason5f39d392007-10-15 16:14:19 -04001253 disk_blocknr = btrfs_file_extent_disk_blocknr(leaf, fi);
Chris Mason3a686372007-05-24 13:35:57 -04001254 if (disk_blocknr == 0)
1255 continue;
1256 ret = btrfs_free_extent(trans, root, disk_blocknr,
Chris Mason5f39d392007-10-15 16:14:19 -04001257 btrfs_file_extent_disk_num_blocks(leaf, fi), 0);
Chris Mason6407bf62007-03-27 06:33:00 -04001258 BUG_ON(ret);
1259 }
1260 return 0;
1261}
1262
Chris Masone0115992007-06-19 16:23:05 -04001263static void reada_walk_down(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -04001264 struct extent_buffer *node)
Chris Masone0115992007-06-19 16:23:05 -04001265{
1266 int i;
1267 u32 nritems;
1268 u64 blocknr;
1269 int ret;
1270 u32 refs;
1271
Chris Mason5f39d392007-10-15 16:14:19 -04001272 nritems = btrfs_header_nritems(node);
Chris Masone0115992007-06-19 16:23:05 -04001273 for (i = 0; i < nritems; i++) {
1274 blocknr = btrfs_node_blockptr(node, i);
1275 ret = lookup_extent_ref(NULL, root, blocknr, 1, &refs);
1276 BUG_ON(ret);
1277 if (refs != 1)
1278 continue;
Chris Mason409eb952007-08-08 20:17:12 -04001279 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masone0115992007-06-19 16:23:05 -04001280 ret = readahead_tree_block(root, blocknr);
Chris Mason409eb952007-08-08 20:17:12 -04001281 cond_resched();
1282 mutex_lock(&root->fs_info->fs_mutex);
Chris Masone0115992007-06-19 16:23:05 -04001283 if (ret)
1284 break;
1285 }
1286}
1287
Chris Mason9aca1d52007-03-13 11:09:37 -04001288/*
1289 * helper function for drop_snapshot, this walks down the tree dropping ref
1290 * counts as it goes.
1291 */
Chris Masone089f052007-03-16 16:20:31 -04001292static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
1293 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05001294{
Chris Mason5f39d392007-10-15 16:14:19 -04001295 struct extent_buffer *next;
1296 struct extent_buffer *cur;
Chris Mason20524f02007-03-10 06:35:47 -05001297 u64 blocknr;
1298 int ret;
1299 u32 refs;
1300
Chris Mason5caf2a02007-04-02 11:20:42 -04001301 WARN_ON(*level < 0);
1302 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason5f39d392007-10-15 16:14:19 -04001303 ret = lookup_extent_ref(trans, root,
1304 extent_buffer_blocknr(path->nodes[*level]),
1305 1, &refs);
Chris Mason20524f02007-03-10 06:35:47 -05001306 BUG_ON(ret);
1307 if (refs > 1)
1308 goto out;
Chris Masone0115992007-06-19 16:23:05 -04001309
Chris Mason9aca1d52007-03-13 11:09:37 -04001310 /*
1311 * walk down to the last node level and free all the leaves
1312 */
Chris Mason6407bf62007-03-27 06:33:00 -04001313 while(*level >= 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001314 WARN_ON(*level < 0);
1315 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason20524f02007-03-10 06:35:47 -05001316 cur = path->nodes[*level];
Chris Masone0115992007-06-19 16:23:05 -04001317
1318 if (*level > 0 && path->slots[*level] == 0)
Chris Mason5f39d392007-10-15 16:14:19 -04001319 reada_walk_down(root, cur);
Chris Masone0115992007-06-19 16:23:05 -04001320
Chris Mason5f39d392007-10-15 16:14:19 -04001321 if (btrfs_header_level(cur) != *level)
Chris Mason2c90e5d2007-04-02 10:50:19 -04001322 WARN_ON(1);
Chris Masone0115992007-06-19 16:23:05 -04001323
Chris Mason7518a232007-03-12 12:01:18 -04001324 if (path->slots[*level] >=
Chris Mason5f39d392007-10-15 16:14:19 -04001325 btrfs_header_nritems(cur))
Chris Mason20524f02007-03-10 06:35:47 -05001326 break;
Chris Mason6407bf62007-03-27 06:33:00 -04001327 if (*level == 0) {
1328 ret = drop_leaf_ref(trans, root, cur);
1329 BUG_ON(ret);
1330 break;
1331 }
Chris Mason5f39d392007-10-15 16:14:19 -04001332 blocknr = btrfs_node_blockptr(cur, path->slots[*level]);
Chris Masonb18c6682007-04-17 13:26:50 -04001333 ret = lookup_extent_ref(trans, root, blocknr, 1, &refs);
Chris Mason6407bf62007-03-27 06:33:00 -04001334 BUG_ON(ret);
1335 if (refs != 1) {
Chris Mason20524f02007-03-10 06:35:47 -05001336 path->slots[*level]++;
Chris Masone089f052007-03-16 16:20:31 -04001337 ret = btrfs_free_extent(trans, root, blocknr, 1, 1);
Chris Mason20524f02007-03-10 06:35:47 -05001338 BUG_ON(ret);
1339 continue;
1340 }
Chris Masone9d0b132007-08-10 14:06:19 -04001341 next = btrfs_find_tree_block(root, blocknr);
Chris Mason5f39d392007-10-15 16:14:19 -04001342 if (!next || !btrfs_buffer_uptodate(next)) {
1343 free_extent_buffer(next);
Chris Masone9d0b132007-08-10 14:06:19 -04001344 mutex_unlock(&root->fs_info->fs_mutex);
1345 next = read_tree_block(root, blocknr);
1346 mutex_lock(&root->fs_info->fs_mutex);
1347
1348 /* we dropped the lock, check one more time */
1349 ret = lookup_extent_ref(trans, root, blocknr, 1, &refs);
1350 BUG_ON(ret);
1351 if (refs != 1) {
1352 path->slots[*level]++;
Chris Mason5f39d392007-10-15 16:14:19 -04001353 free_extent_buffer(next);
Chris Masone9d0b132007-08-10 14:06:19 -04001354 ret = btrfs_free_extent(trans, root,
1355 blocknr, 1, 1);
1356 BUG_ON(ret);
1357 continue;
1358 }
1359 }
Chris Mason5caf2a02007-04-02 11:20:42 -04001360 WARN_ON(*level <= 0);
Chris Mason83e15a22007-03-12 09:03:27 -04001361 if (path->nodes[*level-1])
Chris Mason5f39d392007-10-15 16:14:19 -04001362 free_extent_buffer(path->nodes[*level-1]);
Chris Mason20524f02007-03-10 06:35:47 -05001363 path->nodes[*level-1] = next;
Chris Mason5f39d392007-10-15 16:14:19 -04001364 *level = btrfs_header_level(next);
Chris Mason20524f02007-03-10 06:35:47 -05001365 path->slots[*level] = 0;
1366 }
1367out:
Chris Mason5caf2a02007-04-02 11:20:42 -04001368 WARN_ON(*level < 0);
1369 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason6407bf62007-03-27 06:33:00 -04001370 ret = btrfs_free_extent(trans, root,
Chris Mason5f39d392007-10-15 16:14:19 -04001371 extent_buffer_blocknr(path->nodes[*level]), 1, 1);
1372 free_extent_buffer(path->nodes[*level]);
Chris Mason20524f02007-03-10 06:35:47 -05001373 path->nodes[*level] = NULL;
1374 *level += 1;
1375 BUG_ON(ret);
1376 return 0;
1377}
1378
Chris Mason9aca1d52007-03-13 11:09:37 -04001379/*
1380 * helper for dropping snapshots. This walks back up the tree in the path
1381 * to find the first node higher up where we haven't yet gone through
1382 * all the slots
1383 */
Chris Masone089f052007-03-16 16:20:31 -04001384static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root
1385 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05001386{
1387 int i;
1388 int slot;
1389 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04001390 struct btrfs_root_item *root_item = &root->root_item;
1391
Chris Mason234b63a2007-03-13 10:46:10 -04001392 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Mason20524f02007-03-10 06:35:47 -05001393 slot = path->slots[i];
Chris Mason5f39d392007-10-15 16:14:19 -04001394 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
1395 struct extent_buffer *node;
1396 struct btrfs_disk_key disk_key;
1397 node = path->nodes[i];
Chris Mason20524f02007-03-10 06:35:47 -05001398 path->slots[i]++;
1399 *level = i;
Chris Mason9f3a7422007-08-07 15:52:19 -04001400 WARN_ON(*level == 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001401 btrfs_node_key(node, &disk_key, path->slots[i]);
Chris Mason9f3a7422007-08-07 15:52:19 -04001402 memcpy(&root_item->drop_progress,
Chris Mason5f39d392007-10-15 16:14:19 -04001403 &disk_key, sizeof(disk_key));
Chris Mason9f3a7422007-08-07 15:52:19 -04001404 root_item->drop_level = i;
Chris Mason20524f02007-03-10 06:35:47 -05001405 return 0;
1406 } else {
Chris Masone089f052007-03-16 16:20:31 -04001407 ret = btrfs_free_extent(trans, root,
Chris Mason5f39d392007-10-15 16:14:19 -04001408 extent_buffer_blocknr(path->nodes[*level]),
1409 1, 1);
Chris Mason6407bf62007-03-27 06:33:00 -04001410 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04001411 free_extent_buffer(path->nodes[*level]);
Chris Mason83e15a22007-03-12 09:03:27 -04001412 path->nodes[*level] = NULL;
Chris Mason20524f02007-03-10 06:35:47 -05001413 *level = i + 1;
Chris Mason20524f02007-03-10 06:35:47 -05001414 }
1415 }
1416 return 1;
1417}
1418
Chris Mason9aca1d52007-03-13 11:09:37 -04001419/*
1420 * drop the reference count on the tree rooted at 'snap'. This traverses
1421 * the tree freeing any blocks that have a ref count of zero after being
1422 * decremented.
1423 */
Chris Masone089f052007-03-16 16:20:31 -04001424int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason9f3a7422007-08-07 15:52:19 -04001425 *root)
Chris Mason20524f02007-03-10 06:35:47 -05001426{
Chris Mason3768f362007-03-13 16:47:54 -04001427 int ret = 0;
Chris Mason9aca1d52007-03-13 11:09:37 -04001428 int wret;
Chris Mason20524f02007-03-10 06:35:47 -05001429 int level;
Chris Mason5caf2a02007-04-02 11:20:42 -04001430 struct btrfs_path *path;
Chris Mason20524f02007-03-10 06:35:47 -05001431 int i;
1432 int orig_level;
Chris Mason9f3a7422007-08-07 15:52:19 -04001433 struct btrfs_root_item *root_item = &root->root_item;
Chris Mason20524f02007-03-10 06:35:47 -05001434
Chris Mason5caf2a02007-04-02 11:20:42 -04001435 path = btrfs_alloc_path();
1436 BUG_ON(!path);
Chris Mason20524f02007-03-10 06:35:47 -05001437
Chris Mason5f39d392007-10-15 16:14:19 -04001438 level = btrfs_header_level(root->node);
Chris Mason20524f02007-03-10 06:35:47 -05001439 orig_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04001440 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
1441 path->nodes[level] = root->node;
Chris Masonf510cfe2007-10-15 16:14:48 -04001442 extent_buffer_get(root->node);
Chris Mason9f3a7422007-08-07 15:52:19 -04001443 path->slots[level] = 0;
1444 } else {
1445 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001446 struct btrfs_disk_key found_key;
1447 struct extent_buffer *node;
Chris Mason6702ed42007-08-07 16:15:09 -04001448
Chris Mason9f3a7422007-08-07 15:52:19 -04001449 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Chris Mason6702ed42007-08-07 16:15:09 -04001450 level = root_item->drop_level;
1451 path->lowest_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04001452 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason6702ed42007-08-07 16:15:09 -04001453 if (wret < 0) {
Chris Mason9f3a7422007-08-07 15:52:19 -04001454 ret = wret;
1455 goto out;
1456 }
Chris Mason5f39d392007-10-15 16:14:19 -04001457 node = path->nodes[level];
1458 btrfs_node_key(node, &found_key, path->slots[level]);
1459 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
1460 sizeof(found_key)));
Chris Mason9f3a7422007-08-07 15:52:19 -04001461 }
Chris Mason20524f02007-03-10 06:35:47 -05001462 while(1) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001463 wret = walk_down_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04001464 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05001465 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04001466 if (wret < 0)
1467 ret = wret;
1468
Chris Mason5caf2a02007-04-02 11:20:42 -04001469 wret = walk_up_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04001470 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05001471 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04001472 if (wret < 0)
1473 ret = wret;
Chris Mason409eb952007-08-08 20:17:12 -04001474 ret = -EAGAIN;
Chris Mason409eb952007-08-08 20:17:12 -04001475 break;
Chris Mason20524f02007-03-10 06:35:47 -05001476 }
Chris Mason83e15a22007-03-12 09:03:27 -04001477 for (i = 0; i <= orig_level; i++) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001478 if (path->nodes[i]) {
Chris Mason5f39d392007-10-15 16:14:19 -04001479 free_extent_buffer(path->nodes[i]);
Chris Mason6702ed42007-08-07 16:15:09 -04001480 path->nodes[i] = 0;
Chris Mason83e15a22007-03-12 09:03:27 -04001481 }
Chris Mason20524f02007-03-10 06:35:47 -05001482 }
Chris Mason9f3a7422007-08-07 15:52:19 -04001483out:
Chris Mason5caf2a02007-04-02 11:20:42 -04001484 btrfs_free_path(path);
Chris Mason9aca1d52007-03-13 11:09:37 -04001485 return ret;
Chris Mason20524f02007-03-10 06:35:47 -05001486}
Chris Mason9078a3e2007-04-26 16:46:15 -04001487
Chris Masonbe744172007-05-06 10:15:01 -04001488int btrfs_free_block_groups(struct btrfs_fs_info *info)
1489{
Chris Masonf510cfe2007-10-15 16:14:48 -04001490 u64 start;
1491 u64 end;
Chris Mason96b51792007-10-15 16:15:19 -04001492 int ret;
Chris Masonbe744172007-05-06 10:15:01 -04001493
Chris Mason96b51792007-10-15 16:15:19 -04001494 while(1) {
1495 ret = find_first_extent_bit(&info->block_group_cache, 0,
1496 &start, &end, (unsigned int)-1);
1497 if (ret)
1498 break;
1499 clear_extent_bits(&info->block_group_cache, start,
1500 end, (unsigned int)-1, GFP_NOFS);
1501 }
Chris Masone37c9e62007-05-09 20:13:14 -04001502 while(1) {
Chris Masonf510cfe2007-10-15 16:14:48 -04001503 ret = find_first_extent_bit(&info->free_space_cache, 0,
1504 &start, &end, EXTENT_DIRTY);
1505 if (ret)
Chris Masone37c9e62007-05-09 20:13:14 -04001506 break;
Chris Masonf510cfe2007-10-15 16:14:48 -04001507 clear_extent_dirty(&info->free_space_cache, start,
1508 end, GFP_NOFS);
Chris Masone37c9e62007-05-09 20:13:14 -04001509 }
Chris Masonbe744172007-05-06 10:15:01 -04001510 return 0;
1511}
1512
Chris Mason9078a3e2007-04-26 16:46:15 -04001513int btrfs_read_block_groups(struct btrfs_root *root)
1514{
1515 struct btrfs_path *path;
1516 int ret;
1517 int err = 0;
Chris Mason96b51792007-10-15 16:15:19 -04001518 int bit;
Chris Mason9078a3e2007-04-26 16:46:15 -04001519 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04001520 struct btrfs_fs_info *info = root->fs_info;
Chris Mason96b51792007-10-15 16:15:19 -04001521 struct extent_map_tree *block_group_cache;
Chris Mason9078a3e2007-04-26 16:46:15 -04001522 struct btrfs_key key;
1523 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -04001524 struct extent_buffer *leaf;
Chris Mason84f54cf2007-06-12 07:43:08 -04001525 u64 group_size_blocks;
Chris Mason96b51792007-10-15 16:15:19 -04001526
1527 block_group_cache = &info->block_group_cache;
Chris Mason9078a3e2007-04-26 16:46:15 -04001528
Chris Mason84f54cf2007-06-12 07:43:08 -04001529 group_size_blocks = BTRFS_BLOCK_GROUP_SIZE >>
Chris Mason96b51792007-10-15 16:15:19 -04001530 info->sb->s_blocksize_bits;
1531
Chris Masonbe744172007-05-06 10:15:01 -04001532 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04001533 key.objectid = 0;
1534 key.offset = group_size_blocks;
Chris Mason9078a3e2007-04-26 16:46:15 -04001535 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
1536
1537 path = btrfs_alloc_path();
1538 if (!path)
1539 return -ENOMEM;
1540
1541 while(1) {
Chris Masonbe744172007-05-06 10:15:01 -04001542 ret = btrfs_search_slot(NULL, info->extent_root,
Chris Mason9078a3e2007-04-26 16:46:15 -04001543 &key, path, 0, 0);
1544 if (ret != 0) {
1545 err = ret;
1546 break;
1547 }
Chris Mason5f39d392007-10-15 16:14:19 -04001548 leaf = path->nodes[0];
1549 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason9078a3e2007-04-26 16:46:15 -04001550 cache = kmalloc(sizeof(*cache), GFP_NOFS);
1551 if (!cache) {
1552 err = -1;
1553 break;
1554 }
Chris Mason3e1ad542007-05-07 20:03:49 -04001555
Chris Mason5f39d392007-10-15 16:14:19 -04001556 read_extent_buffer(leaf, &cache->item,
1557 btrfs_item_ptr_offset(leaf, path->slots[0]),
1558 sizeof(cache->item));
Chris Mason9078a3e2007-04-26 16:46:15 -04001559 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Mason31f3c992007-04-30 15:25:45 -04001560 cache->last_alloc = cache->key.objectid;
1561 cache->first_free = cache->key.objectid;
Chris Masonbe744172007-05-06 10:15:01 -04001562 cache->pinned = 0;
Chris Masone37c9e62007-05-09 20:13:14 -04001563 cache->cached = 0;
1564
Chris Mason9078a3e2007-04-26 16:46:15 -04001565 key.objectid = found_key.objectid + found_key.offset;
1566 btrfs_release_path(root, path);
Chris Mason5f39d392007-10-15 16:14:19 -04001567
Chris Mason96b51792007-10-15 16:15:19 -04001568 if (cache->item.flags & BTRFS_BLOCK_GROUP_DATA) {
1569 bit = BLOCK_GROUP_DATA;
1570 cache->data = 1;
1571 } else {
1572 bit = BLOCK_GROUP_METADATA;
1573 cache->data = 0;
Chris Mason31f3c992007-04-30 15:25:45 -04001574 }
Chris Mason96b51792007-10-15 16:15:19 -04001575
1576 /* use EXTENT_LOCKED to prevent merging */
1577 set_extent_bits(block_group_cache, found_key.objectid,
1578 found_key.objectid + found_key.offset - 1,
1579 bit | EXTENT_LOCKED, GFP_NOFS);
1580 set_state_private(block_group_cache, found_key.objectid,
1581 (u64)cache);
1582
Chris Mason9078a3e2007-04-26 16:46:15 -04001583 if (key.objectid >=
Chris Mason4b52dff2007-06-26 10:06:50 -04001584 btrfs_super_total_blocks(&info->super_copy))
Chris Mason9078a3e2007-04-26 16:46:15 -04001585 break;
1586 }
1587
1588 btrfs_free_path(path);
1589 return 0;
1590}