blob: 0b0c947194ae42f4316b6b86577d9eb8654c8723 [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,
Chris Masondb945352007-10-15 16:15:53 -0400132 u64 bytenr)
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,
Chris Masondb945352007-10-15 16:15:53 -0400143 bytenr, &start, &end,
Chris Mason96b51792007-10-15 16:15:19 -0400144 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
Chris Masondb945352007-10-15 16:15:53 -0400155 if (block_group->key.objectid <= bytenr && bytenr <=
Chris Mason96b51792007-10-15 16:15:19 -0400156 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:
Chris Mason1a5bc162007-10-15 16:15:26 -0400191 return search_start;
Chris Masone37c9e62007-05-09 20:13:14 -0400192
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) {
Chris Mason1a5bc162007-10-15 16:15:26 -0400197 return search_start;
Chris Masone37c9e62007-05-09 20:13:14 -0400198 }
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);
Chris Mason1a5bc162007-10-15 16:15:26 -0400250 if (used < div_factor(shint->key.offset, factor)) {
Chris Masonbe744172007-05-06 10:15:01 -0400251 return shint;
252 }
253 }
254 }
255 if (hint && hint->data == data) {
Chris Mason31f3c992007-04-30 15:25:45 -0400256 used = btrfs_block_group_used(&hint->item);
Chris Mason1a5bc162007-10-15 16:15:26 -0400257 if (used < div_factor(hint->key.offset, factor)) {
Chris Mason31f3c992007-04-30 15:25:45 -0400258 return hint;
259 }
Chris Mason8d7be552007-05-10 11:24:42 -0400260 last = hint->key.offset * 3;
Chris Masonbe744172007-05-06 10:15:01 -0400261 if (hint->key.objectid >= last)
Chris Masone37c9e62007-05-09 20:13:14 -0400262 last = max(search_start + hint->key.offset - 1,
263 hint->key.objectid - last);
Chris Masonbe744172007-05-06 10:15:01 -0400264 else
265 last = hint->key.objectid + hint->key.offset;
Chris Mason31f3c992007-04-30 15:25:45 -0400266 hint_last = last;
267 } else {
Chris Masone37c9e62007-05-09 20:13:14 -0400268 if (hint)
269 hint_last = max(hint->key.objectid, search_start);
270 else
271 hint_last = search_start;
272
273 last = hint_last;
Chris Mason31f3c992007-04-30 15:25:45 -0400274 }
Chris Mason31f3c992007-04-30 15:25:45 -0400275again:
Chris Masoncd1bc462007-04-27 10:08:34 -0400276 while(1) {
Chris Mason96b51792007-10-15 16:15:19 -0400277 ret = find_first_extent_bit(block_group_cache, last,
278 &start, &end, bit);
279 if (ret)
Chris Masoncd1bc462007-04-27 10:08:34 -0400280 break;
Chris Mason96b51792007-10-15 16:15:19 -0400281
282 ret = get_state_private(block_group_cache, start, &ptr);
283 if (ret)
284 break;
285
286 cache = (struct btrfs_block_group_cache *)ptr;
287 last = cache->key.objectid + cache->key.offset;
288 used = btrfs_block_group_used(&cache->item);
289
290 if (full_search)
291 free_check = cache->key.offset;
292 else
293 free_check = div_factor(cache->key.offset, factor);
294
Chris Mason1a5bc162007-10-15 16:15:26 -0400295 if (used < free_check) {
Chris Mason96b51792007-10-15 16:15:19 -0400296 found_group = cache;
297 goto found;
Chris Masoncd1bc462007-04-27 10:08:34 -0400298 }
Chris Masonde428b62007-05-18 13:28:27 -0400299 cond_resched();
Chris Masoncd1bc462007-04-27 10:08:34 -0400300 }
Chris Mason31f3c992007-04-30 15:25:45 -0400301 if (!full_search) {
Chris Masonbe744172007-05-06 10:15:01 -0400302 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -0400303 full_search = 1;
304 goto again;
305 }
Chris Mason1e2677e2007-05-29 16:52:18 -0400306 if (!data_swap) {
Chris Mason1e2677e2007-05-29 16:52:18 -0400307 data_swap = 1;
Chris Mason96b51792007-10-15 16:15:19 -0400308 bit = BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA;
Chris Mason1e2677e2007-05-29 16:52:18 -0400309 last = search_start;
310 goto again;
311 }
Chris Masonbe744172007-05-06 10:15:01 -0400312found:
Chris Mason31f3c992007-04-30 15:25:45 -0400313 return found_group;
Chris Masoncd1bc462007-04-27 10:08:34 -0400314}
315
Chris Masonb18c6682007-04-17 13:26:50 -0400316int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
317 struct btrfs_root *root,
Chris Masondb945352007-10-15 16:15:53 -0400318 u64 bytenr, u64 num_bytes)
Chris Mason02217ed2007-03-02 16:08:05 -0500319{
Chris Mason5caf2a02007-04-02 11:20:42 -0400320 struct btrfs_path *path;
Chris Mason02217ed2007-03-02 16:08:05 -0500321 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400322 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400323 struct extent_buffer *l;
Chris Mason234b63a2007-03-13 10:46:10 -0400324 struct btrfs_extent_item *item;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400325 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500326
Chris Masondb945352007-10-15 16:15:53 -0400327 WARN_ON(num_bytes < root->sectorsize);
Chris Mason5caf2a02007-04-02 11:20:42 -0400328 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -0400329 if (!path)
330 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -0400331
Chris Masondb945352007-10-15 16:15:53 -0400332 key.objectid = bytenr;
Chris Mason62e27492007-03-15 12:56:47 -0400333 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masondb945352007-10-15 16:15:53 -0400334 key.offset = num_bytes;
Chris Mason5caf2a02007-04-02 11:20:42 -0400335 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -0400336 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400337 if (ret < 0)
338 return ret;
Chris Masona429e512007-04-18 16:15:28 -0400339 if (ret != 0) {
Chris Masona28ec192007-03-06 20:08:01 -0500340 BUG();
Chris Masona429e512007-04-18 16:15:28 -0400341 }
Chris Mason02217ed2007-03-02 16:08:05 -0500342 BUG_ON(ret != 0);
Chris Mason5f39d392007-10-15 16:14:19 -0400343 l = path->nodes[0];
Chris Mason5caf2a02007-04-02 11:20:42 -0400344 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400345 refs = btrfs_extent_refs(l, item);
346 btrfs_set_extent_refs(l, item, refs + 1);
Chris Mason5caf2a02007-04-02 11:20:42 -0400347 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masona28ec192007-03-06 20:08:01 -0500348
Chris Mason5caf2a02007-04-02 11:20:42 -0400349 btrfs_release_path(root->fs_info->extent_root, path);
350 btrfs_free_path(path);
Chris Mason9f5fae22007-03-20 14:38:32 -0400351 finish_current_insert(trans, root->fs_info->extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -0400352 del_pending_extents(trans, root->fs_info->extent_root);
Chris Mason02217ed2007-03-02 16:08:05 -0500353 return 0;
354}
355
Chris Masone9d0b132007-08-10 14:06:19 -0400356int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
357 struct btrfs_root *root)
358{
359 finish_current_insert(trans, root->fs_info->extent_root);
360 del_pending_extents(trans, root->fs_info->extent_root);
361 return 0;
362}
363
Chris Masonb18c6682007-04-17 13:26:50 -0400364static int lookup_extent_ref(struct btrfs_trans_handle *trans,
Chris Masondb945352007-10-15 16:15:53 -0400365 struct btrfs_root *root, u64 bytenr,
366 u64 num_bytes, u32 *refs)
Chris Masona28ec192007-03-06 20:08:01 -0500367{
Chris Mason5caf2a02007-04-02 11:20:42 -0400368 struct btrfs_path *path;
Chris Masona28ec192007-03-06 20:08:01 -0500369 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400370 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400371 struct extent_buffer *l;
Chris Mason234b63a2007-03-13 10:46:10 -0400372 struct btrfs_extent_item *item;
Chris Mason5caf2a02007-04-02 11:20:42 -0400373
Chris Masondb945352007-10-15 16:15:53 -0400374 WARN_ON(num_bytes < root->sectorsize);
Chris Mason5caf2a02007-04-02 11:20:42 -0400375 path = btrfs_alloc_path();
Chris Masondb945352007-10-15 16:15:53 -0400376 key.objectid = bytenr;
377 key.offset = num_bytes;
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]);
Chris Masondb945352007-10-15 16:15:53 -0400385 printk("failed to find block number %Lu\n", bytenr);
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 Masondb945352007-10-15 16:15:53 -0400399 return btrfs_inc_extent_ref(trans, root, root->node->start,
400 root->node->len);
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{
Chris Masondb945352007-10-15 16:15:53 -0400406 u64 bytenr;
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 Masondb945352007-10-15 16:15:53 -0400411 int level;
Chris Mason6407bf62007-03-27 06:33:00 -0400412 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
Chris Masondb945352007-10-15 16:15:53 -0400419 level = btrfs_header_level(buf);
Chris Mason5f39d392007-10-15 16:14:19 -0400420 nritems = btrfs_header_nritems(buf);
421 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -0400422 if (level == 0) {
423 u64 disk_bytenr;
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 Mason236454d2007-04-19 13:37:44 -0400430 BTRFS_FILE_EXTENT_INLINE)
431 continue;
Chris Masondb945352007-10-15 16:15:53 -0400432 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
433 if (disk_bytenr == 0)
Chris Mason3a686372007-05-24 13:35:57 -0400434 continue;
Chris Masondb945352007-10-15 16:15:53 -0400435 ret = btrfs_inc_extent_ref(trans, root, disk_bytenr,
436 btrfs_file_extent_disk_num_bytes(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 Masondb945352007-10-15 16:15:53 -0400442 bytenr = btrfs_node_blockptr(buf, i);
443 ret = btrfs_inc_extent_ref(trans, root, bytenr,
444 btrfs_level_size(root, level - 1));
Chris Mason54aa1f42007-06-22 14:16:25 -0400445 if (ret) {
446 faili = i;
447 goto fail;
448 }
Chris Mason6407bf62007-03-27 06:33:00 -0400449 }
Chris Mason02217ed2007-03-02 16:08:05 -0500450 }
451 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -0400452fail:
Chris Masonccd467d2007-06-28 15:57:36 -0400453 WARN_ON(1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400454 for (i =0; i < faili; i++) {
Chris Masondb945352007-10-15 16:15:53 -0400455 if (level == 0) {
456 u64 disk_bytenr;
Chris Mason5f39d392007-10-15 16:14:19 -0400457 btrfs_item_key_to_cpu(buf, &key, i);
458 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -0400459 continue;
Chris Mason5f39d392007-10-15 16:14:19 -0400460 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -0400461 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400462 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -0400463 BTRFS_FILE_EXTENT_INLINE)
464 continue;
Chris Masondb945352007-10-15 16:15:53 -0400465 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
466 if (disk_bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -0400467 continue;
Chris Masondb945352007-10-15 16:15:53 -0400468 err = btrfs_free_extent(trans, root, disk_bytenr,
469 btrfs_file_extent_disk_num_bytes(buf,
Chris Mason5f39d392007-10-15 16:14:19 -0400470 fi), 0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400471 BUG_ON(err);
472 } else {
Chris Masondb945352007-10-15 16:15:53 -0400473 bytenr = btrfs_node_blockptr(buf, i);
474 err = btrfs_free_extent(trans, root, bytenr,
475 btrfs_level_size(root, level - 1), 0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400476 BUG_ON(err);
477 }
478 }
479 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -0500480}
481
Chris Mason9078a3e2007-04-26 16:46:15 -0400482static int write_one_cache_group(struct btrfs_trans_handle *trans,
483 struct btrfs_root *root,
484 struct btrfs_path *path,
485 struct btrfs_block_group_cache *cache)
486{
487 int ret;
488 int pending_ret;
489 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -0400490 unsigned long bi;
491 struct extent_buffer *leaf;
Chris Mason9078a3e2007-04-26 16:46:15 -0400492
Chris Mason9078a3e2007-04-26 16:46:15 -0400493 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400494 if (ret < 0)
495 goto fail;
Chris Mason9078a3e2007-04-26 16:46:15 -0400496 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -0400497
498 leaf = path->nodes[0];
499 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
500 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
501 btrfs_mark_buffer_dirty(leaf);
Chris Mason9078a3e2007-04-26 16:46:15 -0400502 btrfs_release_path(extent_root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -0400503fail:
Chris Mason9078a3e2007-04-26 16:46:15 -0400504 finish_current_insert(trans, extent_root);
505 pending_ret = del_pending_extents(trans, extent_root);
506 if (ret)
507 return ret;
508 if (pending_ret)
509 return pending_ret;
510 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 Masondb945352007-10-15 16:15:53 -0400565 u64 bytenr, u64 num_bytes, int alloc,
566 int mark_free, 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;
Chris Masondb945352007-10-15 16:15:53 -0400570 u64 total = num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -0400571 u64 old_val;
Chris Masondb945352007-10-15 16:15:53 -0400572 u64 byte_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 Masondb945352007-10-15 16:15:53 -0400577 cache = btrfs_lookup_block_group(info, bytenr);
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 Masondb945352007-10-15 16:15:53 -0400581 byte_in_group = bytenr - cache->key.objectid;
582 WARN_ON(byte_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);
Chris Masondb945352007-10-15 16:15:53 -0400589 num_bytes = min(total, cache->key.offset - byte_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -0400590 if (alloc) {
Chris Mason1e2677e2007-05-29 16:52:18 -0400591 if (cache->data != data &&
Chris Mason84f54cf2007-06-12 07:43:08 -0400592 old_val < (cache->key.offset >> 1)) {
Chris Mason96b51792007-10-15 16:15:19 -0400593 int bit_to_clear;
594 int bit_to_set;
Chris Mason1e2677e2007-05-29 16:52:18 -0400595
Chris Mason96b51792007-10-15 16:15:19 -0400596 cache->data = data;
Chris Mason1e2677e2007-05-29 16:52:18 -0400597 if (data) {
Chris Mason96b51792007-10-15 16:15:19 -0400598 bit_to_clear = BLOCK_GROUP_DATA;
599 bit_to_set = BLOCK_GROUP_METADATA;
Chris Mason1e2677e2007-05-29 16:52:18 -0400600 cache->item.flags |=
601 BTRFS_BLOCK_GROUP_DATA;
602 } else {
Chris Mason96b51792007-10-15 16:15:19 -0400603 bit_to_clear = BLOCK_GROUP_METADATA;
604 bit_to_set = BLOCK_GROUP_DATA;
Chris Mason1e2677e2007-05-29 16:52:18 -0400605 cache->item.flags &=
606 ~BTRFS_BLOCK_GROUP_DATA;
607 }
Chris Mason96b51792007-10-15 16:15:19 -0400608 clear_extent_bits(&info->block_group_cache,
609 start, end, bit_to_clear,
610 GFP_NOFS);
611 set_extent_bits(&info->block_group_cache,
612 start, end, bit_to_set,
613 GFP_NOFS);
Chris Mason1e2677e2007-05-29 16:52:18 -0400614 }
Chris Masondb945352007-10-15 16:15:53 -0400615 old_val += num_bytes;
Chris Masoncd1bc462007-04-27 10:08:34 -0400616 } else {
Chris Masondb945352007-10-15 16:15:53 -0400617 old_val -= num_bytes;
Chris Masonf510cfe2007-10-15 16:14:48 -0400618 if (mark_free) {
619 set_extent_dirty(&info->free_space_cache,
Chris Masondb945352007-10-15 16:15:53 -0400620 bytenr, bytenr + num_bytes - 1,
Chris Masonf510cfe2007-10-15 16:14:48 -0400621 GFP_NOFS);
Chris Masone37c9e62007-05-09 20:13:14 -0400622 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400623 }
Chris Mason9078a3e2007-04-26 16:46:15 -0400624 btrfs_set_block_group_used(&cache->item, old_val);
Chris Masondb945352007-10-15 16:15:53 -0400625 total -= num_bytes;
626 bytenr += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -0400627 }
628 return 0;
629}
630
Chris Mason1a5bc162007-10-15 16:15:26 -0400631int btrfs_copy_pinned(struct btrfs_root *root, struct extent_map_tree *copy)
Chris Masonccd467d2007-06-28 15:57:36 -0400632{
Chris Masonccd467d2007-06-28 15:57:36 -0400633 u64 last = 0;
Chris Mason1a5bc162007-10-15 16:15:26 -0400634 u64 start;
635 u64 end;
636 struct extent_map_tree *pinned_extents = &root->fs_info->pinned_extents;
Chris Masonccd467d2007-06-28 15:57:36 -0400637 int ret;
Chris Masonccd467d2007-06-28 15:57:36 -0400638
639 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -0400640 ret = find_first_extent_bit(pinned_extents, last,
641 &start, &end, EXTENT_DIRTY);
642 if (ret)
Chris Masonccd467d2007-06-28 15:57:36 -0400643 break;
Chris Mason1a5bc162007-10-15 16:15:26 -0400644 set_extent_dirty(copy, start, end, GFP_NOFS);
645 last = end + 1;
Chris Masonccd467d2007-06-28 15:57:36 -0400646 }
647 return 0;
648}
649
650int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
651 struct btrfs_root *root,
Chris Mason1a5bc162007-10-15 16:15:26 -0400652 struct extent_map_tree *unpin)
Chris Masona28ec192007-03-06 20:08:01 -0500653{
Chris Mason1a5bc162007-10-15 16:15:26 -0400654 u64 start;
655 u64 end;
Chris Masona28ec192007-03-06 20:08:01 -0500656 int ret;
Chris Mason1a5bc162007-10-15 16:15:26 -0400657 struct extent_map_tree *pinned_extents = &root->fs_info->pinned_extents;
Chris Masonf510cfe2007-10-15 16:14:48 -0400658 struct extent_map_tree *free_space_cache;
659
660 free_space_cache = &root->fs_info->free_space_cache;
Chris Masona28ec192007-03-06 20:08:01 -0500661
662 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -0400663 ret = find_first_extent_bit(unpin, 0, &start, &end,
664 EXTENT_DIRTY);
665 if (ret)
Chris Masona28ec192007-03-06 20:08:01 -0500666 break;
Chris Mason1a5bc162007-10-15 16:15:26 -0400667
668 clear_extent_dirty(pinned_extents, start, end,
669 GFP_NOFS);
670 clear_extent_dirty(unpin, start, end, GFP_NOFS);
671 set_extent_dirty(free_space_cache, start, end, GFP_NOFS);
Chris Masona28ec192007-03-06 20:08:01 -0500672 }
673 return 0;
674}
675
Chris Masone089f052007-03-16 16:20:31 -0400676static int finish_current_insert(struct btrfs_trans_handle *trans, struct
677 btrfs_root *extent_root)
Chris Mason037e6392007-03-07 11:50:24 -0500678{
Chris Masone2fa7222007-03-12 16:22:34 -0400679 struct btrfs_key ins;
Chris Mason234b63a2007-03-13 10:46:10 -0400680 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -0500681 int ret;
Chris Mason1a5bc162007-10-15 16:15:26 -0400682 int err = 0;
683 u64 start;
684 u64 end;
Chris Mason1261ec42007-03-20 20:35:03 -0400685 struct btrfs_fs_info *info = extent_root->fs_info;
Chris Mason037e6392007-03-07 11:50:24 -0500686
Chris Mason5f39d392007-10-15 16:14:19 -0400687 btrfs_set_stack_extent_refs(&extent_item, 1);
Chris Mason62e27492007-03-15 12:56:47 -0400688 btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
Chris Mason5f39d392007-10-15 16:14:19 -0400689 btrfs_set_stack_extent_owner(&extent_item,
690 extent_root->root_key.objectid);
Chris Mason037e6392007-03-07 11:50:24 -0500691
Chris Mason26b80032007-08-08 20:17:12 -0400692 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -0400693 ret = find_first_extent_bit(&info->extent_ins, 0, &start,
694 &end, EXTENT_LOCKED);
695 if (ret)
Chris Mason26b80032007-08-08 20:17:12 -0400696 break;
697
Chris Mason1a5bc162007-10-15 16:15:26 -0400698 ins.objectid = start;
699 ins.offset = end + 1 - start;
700 err = btrfs_insert_item(trans, extent_root, &ins,
701 &extent_item, sizeof(extent_item));
702 clear_extent_bits(&info->extent_ins, start, end, EXTENT_LOCKED,
703 GFP_NOFS);
Chris Mason037e6392007-03-07 11:50:24 -0500704 }
Chris Mason037e6392007-03-07 11:50:24 -0500705 return 0;
706}
707
Chris Masondb945352007-10-15 16:15:53 -0400708static int pin_down_bytes(struct btrfs_root *root, u64 bytenr, u32 num_bytes,
709 int pending)
Chris Masone20d96d2007-03-22 12:13:20 -0400710{
Chris Mason1a5bc162007-10-15 16:15:26 -0400711 int err = 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400712 struct extent_buffer *buf;
Chris Mason78fae272007-03-25 11:35:08 -0400713
Chris Masonf4b9aa82007-03-27 11:05:53 -0400714 if (!pending) {
Chris Masondb945352007-10-15 16:15:53 -0400715 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
Chris Mason5f39d392007-10-15 16:14:19 -0400716 if (buf) {
717 if (btrfs_buffer_uptodate(buf)) {
Chris Mason2c90e5d2007-04-02 10:50:19 -0400718 u64 transid =
719 root->fs_info->running_transaction->transid;
Chris Mason5f39d392007-10-15 16:14:19 -0400720 if (btrfs_header_generation(buf) == transid) {
721 free_extent_buffer(buf);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400722 return 0;
723 }
Chris Masonf4b9aa82007-03-27 11:05:53 -0400724 }
Chris Mason5f39d392007-10-15 16:14:19 -0400725 free_extent_buffer(buf);
Chris Mason8ef97622007-03-26 10:15:30 -0400726 }
Chris Mason1a5bc162007-10-15 16:15:26 -0400727 set_extent_dirty(&root->fs_info->pinned_extents,
Chris Masondb945352007-10-15 16:15:53 -0400728 bytenr, bytenr + num_bytes - 1, GFP_NOFS);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400729 } else {
Chris Mason1a5bc162007-10-15 16:15:26 -0400730 set_extent_bits(&root->fs_info->pending_del,
Chris Masondb945352007-10-15 16:15:53 -0400731 bytenr, bytenr + num_bytes - 1,
732 EXTENT_LOCKED, GFP_NOFS);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400733 }
Chris Masonbe744172007-05-06 10:15:01 -0400734 BUG_ON(err < 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400735 return 0;
736}
737
Chris Masona28ec192007-03-06 20:08:01 -0500738/*
739 * remove an extent from the root, returns 0 on success
740 */
Chris Masone089f052007-03-16 16:20:31 -0400741static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masondb945352007-10-15 16:15:53 -0400742 *root, u64 bytenr, u64 num_bytes, int pin,
Chris Masone37c9e62007-05-09 20:13:14 -0400743 int mark_free)
Chris Masona28ec192007-03-06 20:08:01 -0500744{
Chris Mason5caf2a02007-04-02 11:20:42 -0400745 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -0400746 struct btrfs_key key;
Chris Mason1261ec42007-03-20 20:35:03 -0400747 struct btrfs_fs_info *info = root->fs_info;
748 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -0400749 struct extent_buffer *leaf;
Chris Masona28ec192007-03-06 20:08:01 -0500750 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400751 struct btrfs_extent_item *ei;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400752 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500753
Chris Masondb945352007-10-15 16:15:53 -0400754 key.objectid = bytenr;
Chris Mason62e27492007-03-15 12:56:47 -0400755 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masondb945352007-10-15 16:15:53 -0400756 key.offset = num_bytes;
Chris Masona28ec192007-03-06 20:08:01 -0500757
Chris Mason5caf2a02007-04-02 11:20:42 -0400758 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -0400759 if (!path)
760 return -ENOMEM;
761
Chris Mason5caf2a02007-04-02 11:20:42 -0400762 ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400763 if (ret < 0)
764 return ret;
765 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -0400766
767 leaf = path->nodes[0];
768 ei = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason123abc82007-03-14 14:14:43 -0400769 struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400770 refs = btrfs_extent_refs(leaf, ei);
771 BUG_ON(refs == 0);
772 refs -= 1;
773 btrfs_set_extent_refs(leaf, ei, refs);
774 btrfs_mark_buffer_dirty(leaf);
775
Chris Masoncf27e1e2007-03-13 09:49:06 -0400776 if (refs == 0) {
Chris Masondb945352007-10-15 16:15:53 -0400777 u64 super_used;
778 u64 root_used;
Chris Mason78fae272007-03-25 11:35:08 -0400779
780 if (pin) {
Chris Masondb945352007-10-15 16:15:53 -0400781 ret = pin_down_bytes(root, bytenr, num_bytes, 0);
Chris Mason78fae272007-03-25 11:35:08 -0400782 BUG_ON(ret);
783 }
784
Josef Bacik58176a92007-08-29 15:47:34 -0400785 /* block accounting for super block */
Chris Masondb945352007-10-15 16:15:53 -0400786 super_used = btrfs_super_bytes_used(&info->super_copy);
787 btrfs_set_super_bytes_used(&info->super_copy,
788 super_used - num_bytes);
Josef Bacik58176a92007-08-29 15:47:34 -0400789
790 /* block accounting for root item */
Chris Masondb945352007-10-15 16:15:53 -0400791 root_used = btrfs_root_used(&root->root_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400792 btrfs_set_root_used(&root->root_item,
Chris Masondb945352007-10-15 16:15:53 -0400793 root_used - num_bytes);
Josef Bacik58176a92007-08-29 15:47:34 -0400794
Chris Mason5caf2a02007-04-02 11:20:42 -0400795 ret = btrfs_del_item(trans, extent_root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -0400796 if (ret) {
797 return ret;
798 }
Chris Masondb945352007-10-15 16:15:53 -0400799 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
Chris Mason1e2677e2007-05-29 16:52:18 -0400800 mark_free, 0);
Chris Mason9078a3e2007-04-26 16:46:15 -0400801 BUG_ON(ret);
Chris Masona28ec192007-03-06 20:08:01 -0500802 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400803 btrfs_free_path(path);
Chris Masone089f052007-03-16 16:20:31 -0400804 finish_current_insert(trans, extent_root);
Chris Masona28ec192007-03-06 20:08:01 -0500805 return ret;
806}
807
808/*
Chris Masonfec577f2007-02-26 10:40:21 -0500809 * find all the blocks marked as pending in the radix tree and remove
810 * them from the extent map
811 */
Chris Masone089f052007-03-16 16:20:31 -0400812static int del_pending_extents(struct btrfs_trans_handle *trans, struct
813 btrfs_root *extent_root)
Chris Masonfec577f2007-02-26 10:40:21 -0500814{
815 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -0400816 int err = 0;
Chris Mason1a5bc162007-10-15 16:15:26 -0400817 u64 start;
818 u64 end;
819 struct extent_map_tree *pending_del;
820 struct extent_map_tree *pinned_extents;
Chris Mason8ef97622007-03-26 10:15:30 -0400821
Chris Mason1a5bc162007-10-15 16:15:26 -0400822 pending_del = &extent_root->fs_info->pending_del;
823 pinned_extents = &extent_root->fs_info->pinned_extents;
Chris Masonfec577f2007-02-26 10:40:21 -0500824
825 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -0400826 ret = find_first_extent_bit(pending_del, 0, &start, &end,
827 EXTENT_LOCKED);
828 if (ret)
Chris Masonfec577f2007-02-26 10:40:21 -0500829 break;
Chris Mason1a5bc162007-10-15 16:15:26 -0400830
831 set_extent_dirty(pinned_extents, start, end, GFP_NOFS);
832 clear_extent_bits(pending_del, start, end, EXTENT_LOCKED,
833 GFP_NOFS);
834 ret = __free_extent(trans, extent_root,
835 start, end + 1 - start, 0, 0);
836 if (ret)
837 err = ret;
Chris Masonfec577f2007-02-26 10:40:21 -0500838 }
Chris Masone20d96d2007-03-22 12:13:20 -0400839 return err;
Chris Masonfec577f2007-02-26 10:40:21 -0500840}
841
842/*
843 * remove an extent from the root, returns 0 on success
844 */
Chris Masone089f052007-03-16 16:20:31 -0400845int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masondb945352007-10-15 16:15:53 -0400846 *root, u64 bytenr, u64 num_bytes, int pin)
Chris Masonfec577f2007-02-26 10:40:21 -0500847{
Chris Mason9f5fae22007-03-20 14:38:32 -0400848 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Masonfec577f2007-02-26 10:40:21 -0500849 int pending_ret;
850 int ret;
Chris Masona28ec192007-03-06 20:08:01 -0500851
Chris Masondb945352007-10-15 16:15:53 -0400852 WARN_ON(num_bytes < root->sectorsize);
Chris Masona28ec192007-03-06 20:08:01 -0500853 if (root == extent_root) {
Chris Masondb945352007-10-15 16:15:53 -0400854 pin_down_bytes(root, bytenr, num_bytes, 1);
Chris Masona28ec192007-03-06 20:08:01 -0500855 return 0;
856 }
Chris Masondb945352007-10-15 16:15:53 -0400857 ret = __free_extent(trans, root, bytenr, num_bytes, pin, pin == 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400858 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
Chris Masonfec577f2007-02-26 10:40:21 -0500859 return ret ? ret : pending_ret;
860}
861
862/*
863 * walks the btree of allocated extents and find a hole of a given size.
864 * The key ins is changed to record the hole:
865 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -0400866 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -0500867 * ins->offset == number of blocks
868 * Any available blocks before search_start are skipped.
869 */
Chris Masone089f052007-03-16 16:20:31 -0400870static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masondb945352007-10-15 16:15:53 -0400871 *orig_root, u64 num_bytes, u64 empty_size,
872 u64 search_start, u64 search_end, u64 hint_byte,
Chris Masonf2654de2007-06-26 12:20:46 -0400873 struct btrfs_key *ins, u64 exclude_start,
874 u64 exclude_nr, int data)
Chris Masonfec577f2007-02-26 10:40:21 -0500875{
Chris Mason5caf2a02007-04-02 11:20:42 -0400876 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -0400877 struct btrfs_key key;
Chris Masonfec577f2007-02-26 10:40:21 -0500878 int ret;
879 u64 hole_size = 0;
880 int slot = 0;
Chris Masondb945352007-10-15 16:15:53 -0400881 u64 last_byte = 0;
Chris Masonbe744172007-05-06 10:15:01 -0400882 u64 orig_search_start = search_start;
Chris Masonfec577f2007-02-26 10:40:21 -0500883 int start_found;
Chris Mason5f39d392007-10-15 16:14:19 -0400884 struct extent_buffer *l;
Chris Mason9f5fae22007-03-20 14:38:32 -0400885 struct btrfs_root * root = orig_root->fs_info->extent_root;
Chris Masonf2458e12007-04-25 15:52:25 -0400886 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -0400887 u64 total_needed = num_bytes;
Chris Masone20d96d2007-03-22 12:13:20 -0400888 int level;
Chris Masonbe08c1b2007-05-03 09:06:49 -0400889 struct btrfs_block_group_cache *block_group;
Chris Masonbe744172007-05-06 10:15:01 -0400890 int full_scan = 0;
Chris Masonfbdc7622007-05-30 10:22:12 -0400891 int wrapped = 0;
Chris Masonfec577f2007-02-26 10:40:21 -0500892
Chris Masondb945352007-10-15 16:15:53 -0400893 WARN_ON(num_bytes < root->sectorsize);
Chris Masonb1a4d962007-04-04 15:27:52 -0400894 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
895
Chris Mason5f39d392007-10-15 16:14:19 -0400896 level = btrfs_header_level(root->node);
897
Chris Mason3e1ad542007-05-07 20:03:49 -0400898 if (search_end == (u64)-1)
Chris Masondb945352007-10-15 16:15:53 -0400899 search_end = btrfs_super_total_bytes(&info->super_copy);
900 if (hint_byte) {
901 block_group = btrfs_lookup_block_group(info, hint_byte);
Chris Masonbe744172007-05-06 10:15:01 -0400902 block_group = btrfs_find_block_group(root, block_group,
Chris Masondb945352007-10-15 16:15:53 -0400903 hint_byte, data, 1);
Chris Masonbe744172007-05-06 10:15:01 -0400904 } else {
905 block_group = btrfs_find_block_group(root,
906 trans->block_group, 0,
Chris Masonde428b62007-05-18 13:28:27 -0400907 data, 1);
Chris Masonbe744172007-05-06 10:15:01 -0400908 }
909
Chris Mason6702ed42007-08-07 16:15:09 -0400910 total_needed += empty_size;
Chris Masone0115992007-06-19 16:23:05 -0400911 path = btrfs_alloc_path();
912
Chris Masonbe744172007-05-06 10:15:01 -0400913check_failed:
Chris Masonf510cfe2007-10-15 16:14:48 -0400914 search_start = find_search_start(root, &block_group,
915 search_start, total_needed, data);
Chris Masone37c9e62007-05-09 20:13:14 -0400916
Chris Mason5caf2a02007-04-02 11:20:42 -0400917 btrfs_init_path(path);
Chris Masonfec577f2007-02-26 10:40:21 -0500918 ins->objectid = search_start;
919 ins->offset = 0;
Chris Masonfec577f2007-02-26 10:40:21 -0500920 start_found = 0;
Chris Mason2cc58cf2007-08-27 16:49:44 -0400921 path->reada = 2;
Chris Masone37c9e62007-05-09 20:13:14 -0400922
Chris Mason5caf2a02007-04-02 11:20:42 -0400923 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
Chris Mason0f70abe2007-02-28 16:46:22 -0500924 if (ret < 0)
925 goto error;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500926
Chris Masone37c9e62007-05-09 20:13:14 -0400927 if (path->slots[0] > 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400928 path->slots[0]--;
Chris Masone37c9e62007-05-09 20:13:14 -0400929 }
930
Chris Mason5f39d392007-10-15 16:14:19 -0400931 l = path->nodes[0];
932 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
933
Chris Masone37c9e62007-05-09 20:13:14 -0400934 /*
935 * a rare case, go back one key if we hit a block group item
936 * instead of an extent item
937 */
938 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY &&
939 key.objectid + key.offset >= search_start) {
940 ins->objectid = key.objectid;
941 ins->offset = key.offset - 1;
942 btrfs_release_path(root, path);
943 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
944 if (ret < 0)
945 goto error;
946
947 if (path->slots[0] > 0) {
948 path->slots[0]--;
949 }
950 }
Chris Mason0579da42007-03-07 16:15:30 -0500951
Chris Masonfec577f2007-02-26 10:40:21 -0500952 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400953 l = path->nodes[0];
Chris Mason5caf2a02007-04-02 11:20:42 -0400954 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -0400955 if (slot >= btrfs_header_nritems(l)) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400956 ret = btrfs_next_leaf(root, path);
Chris Masonfec577f2007-02-26 10:40:21 -0500957 if (ret == 0)
958 continue;
Chris Mason0f70abe2007-02-28 16:46:22 -0500959 if (ret < 0)
960 goto error;
Chris Masonfec577f2007-02-26 10:40:21 -0500961 if (!start_found) {
962 ins->objectid = search_start;
Chris Mason3e1ad542007-05-07 20:03:49 -0400963 ins->offset = search_end - search_start;
Chris Masonfec577f2007-02-26 10:40:21 -0500964 start_found = 1;
965 goto check_pending;
966 }
Chris Masondb945352007-10-15 16:15:53 -0400967 ins->objectid = last_byte > search_start ?
968 last_byte : search_start;
Chris Mason3e1ad542007-05-07 20:03:49 -0400969 ins->offset = search_end - ins->objectid;
Chris Masonfec577f2007-02-26 10:40:21 -0500970 goto check_pending;
971 }
Chris Mason5f39d392007-10-15 16:14:19 -0400972 btrfs_item_key_to_cpu(l, &key, slot);
Chris Mason96b51792007-10-15 16:15:19 -0400973
Chris Masondb945352007-10-15 16:15:53 -0400974 if (key.objectid >= search_start && key.objectid > last_byte &&
Chris Masone37c9e62007-05-09 20:13:14 -0400975 start_found) {
Chris Masondb945352007-10-15 16:15:53 -0400976 if (last_byte < search_start)
977 last_byte = search_start;
978 hole_size = key.objectid - last_byte;
979 if (hole_size >= num_bytes) {
980 ins->objectid = last_byte;
Chris Masone37c9e62007-05-09 20:13:14 -0400981 ins->offset = hole_size;
982 goto check_pending;
Chris Mason0579da42007-03-07 16:15:30 -0500983 }
Chris Masonfec577f2007-02-26 10:40:21 -0500984 }
Chris Mason96b51792007-10-15 16:15:19 -0400985 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY) {
986 if (!start_found) {
Chris Masondb945352007-10-15 16:15:53 -0400987 last_byte = key.objectid;
Chris Mason96b51792007-10-15 16:15:19 -0400988 start_found = 1;
989 }
Chris Masone37c9e62007-05-09 20:13:14 -0400990 goto next;
Chris Mason96b51792007-10-15 16:15:19 -0400991 }
992
Chris Masone37c9e62007-05-09 20:13:14 -0400993
Chris Mason0579da42007-03-07 16:15:30 -0500994 start_found = 1;
Chris Masondb945352007-10-15 16:15:53 -0400995 last_byte = key.objectid + key.offset;
Chris Masonf510cfe2007-10-15 16:14:48 -0400996
Chris Masondb945352007-10-15 16:15:53 -0400997 if (!full_scan && last_byte >= block_group->key.objectid +
Chris Masonbe744172007-05-06 10:15:01 -0400998 block_group->key.offset) {
999 btrfs_release_path(root, path);
1000 search_start = block_group->key.objectid +
1001 block_group->key.offset * 2;
1002 goto new_group;
1003 }
Chris Mason9078a3e2007-04-26 16:46:15 -04001004next:
Chris Mason5caf2a02007-04-02 11:20:42 -04001005 path->slots[0]++;
Chris Masonde428b62007-05-18 13:28:27 -04001006 cond_resched();
Chris Masonfec577f2007-02-26 10:40:21 -05001007 }
Chris Masonfec577f2007-02-26 10:40:21 -05001008check_pending:
1009 /* we have to make sure we didn't find an extent that has already
1010 * been allocated by the map tree or the original allocation
1011 */
Chris Mason5caf2a02007-04-02 11:20:42 -04001012 btrfs_release_path(root, path);
Chris Masonfec577f2007-02-26 10:40:21 -05001013 BUG_ON(ins->objectid < search_start);
Chris Masone37c9e62007-05-09 20:13:14 -04001014
Chris Masondb945352007-10-15 16:15:53 -04001015 if (ins->objectid + num_bytes >= search_end)
Chris Masoncf675822007-09-17 11:00:51 -04001016 goto enospc;
1017
Chris Mason1a5bc162007-10-15 16:15:26 -04001018 if (test_range_bit(&info->extent_ins, ins->objectid,
Chris Masondb945352007-10-15 16:15:53 -04001019 ins->objectid + num_bytes -1, EXTENT_LOCKED, 0)) {
1020 search_start = ins->objectid + num_bytes;
Chris Mason1a5bc162007-10-15 16:15:26 -04001021 goto new_group;
1022 }
1023 if (test_range_bit(&info->pinned_extents, ins->objectid,
Chris Masondb945352007-10-15 16:15:53 -04001024 ins->objectid + num_bytes -1, EXTENT_DIRTY, 0)) {
1025 search_start = ins->objectid + num_bytes;
Chris Mason1a5bc162007-10-15 16:15:26 -04001026 goto new_group;
Chris Masonfec577f2007-02-26 10:40:21 -05001027 }
Chris Masondb945352007-10-15 16:15:53 -04001028 if (exclude_nr > 0 && (ins->objectid + num_bytes > exclude_start &&
Chris Masonf2654de2007-06-26 12:20:46 -04001029 ins->objectid < exclude_start + exclude_nr)) {
1030 search_start = exclude_start + exclude_nr;
1031 goto new_group;
1032 }
Chris Masone37c9e62007-05-09 20:13:14 -04001033 if (!data) {
Chris Mason5276aed2007-06-11 21:33:38 -04001034 block_group = btrfs_lookup_block_group(info, ins->objectid);
Chris Mason26b80032007-08-08 20:17:12 -04001035 if (block_group)
1036 trans->block_group = block_group;
Chris Masoncd1bc462007-04-27 10:08:34 -04001037 }
Chris Masondb945352007-10-15 16:15:53 -04001038 ins->offset = num_bytes;
Chris Mason5caf2a02007-04-02 11:20:42 -04001039 btrfs_free_path(path);
Chris Masonfec577f2007-02-26 10:40:21 -05001040 return 0;
Chris Masonbe744172007-05-06 10:15:01 -04001041
1042new_group:
Chris Masondb945352007-10-15 16:15:53 -04001043 if (search_start + num_bytes >= search_end) {
Chris Masoncf675822007-09-17 11:00:51 -04001044enospc:
Chris Masonbe744172007-05-06 10:15:01 -04001045 search_start = orig_search_start;
Chris Masonfbdc7622007-05-30 10:22:12 -04001046 if (full_scan) {
1047 ret = -ENOSPC;
1048 goto error;
1049 }
Chris Mason6702ed42007-08-07 16:15:09 -04001050 if (wrapped) {
1051 if (!full_scan)
1052 total_needed -= empty_size;
Chris Masonfbdc7622007-05-30 10:22:12 -04001053 full_scan = 1;
Chris Mason6702ed42007-08-07 16:15:09 -04001054 } else
Chris Masonfbdc7622007-05-30 10:22:12 -04001055 wrapped = 1;
Chris Masonbe744172007-05-06 10:15:01 -04001056 }
Chris Mason5276aed2007-06-11 21:33:38 -04001057 block_group = btrfs_lookup_block_group(info, search_start);
Chris Masonfbdc7622007-05-30 10:22:12 -04001058 cond_resched();
Chris Masonbe744172007-05-06 10:15:01 -04001059 if (!full_scan)
1060 block_group = btrfs_find_block_group(root, block_group,
Chris Masonde428b62007-05-18 13:28:27 -04001061 search_start, data, 0);
Chris Masonbe744172007-05-06 10:15:01 -04001062 goto check_failed;
1063
Chris Mason0f70abe2007-02-28 16:46:22 -05001064error:
Chris Mason5caf2a02007-04-02 11:20:42 -04001065 btrfs_release_path(root, path);
1066 btrfs_free_path(path);
Chris Mason0f70abe2007-02-28 16:46:22 -05001067 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05001068}
Chris Masonfec577f2007-02-26 10:40:21 -05001069/*
Chris Masonfec577f2007-02-26 10:40:21 -05001070 * finds a free extent and does all the dirty work required for allocation
1071 * returns the key for the extent through ins, and a tree buffer for
1072 * the first block of the extent through buf.
1073 *
1074 * returns 0 if everything worked, non-zero otherwise.
1075 */
Chris Mason4d775672007-04-20 20:23:12 -04001076int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
1077 struct btrfs_root *root, u64 owner,
Chris Masondb945352007-10-15 16:15:53 -04001078 u64 num_bytes, u64 empty_size, u64 hint_byte,
Chris Masonbe08c1b2007-05-03 09:06:49 -04001079 u64 search_end, struct btrfs_key *ins, int data)
Chris Masonfec577f2007-02-26 10:40:21 -05001080{
1081 int ret;
1082 int pending_ret;
Chris Masondb945352007-10-15 16:15:53 -04001083 u64 super_used, root_used;
Chris Masonfbdc7622007-05-30 10:22:12 -04001084 u64 search_start = 0;
Chris Mason1261ec42007-03-20 20:35:03 -04001085 struct btrfs_fs_info *info = root->fs_info;
1086 struct btrfs_root *extent_root = info->extent_root;
Chris Mason234b63a2007-03-13 10:46:10 -04001087 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -05001088
Chris Mason5f39d392007-10-15 16:14:19 -04001089 btrfs_set_stack_extent_refs(&extent_item, 1);
1090 btrfs_set_stack_extent_owner(&extent_item, owner);
Chris Masonfec577f2007-02-26 10:40:21 -05001091
Chris Masondb945352007-10-15 16:15:53 -04001092 WARN_ON(num_bytes < root->sectorsize);
1093 ret = find_free_extent(trans, root, num_bytes, empty_size,
1094 search_start, search_end, hint_byte, ins,
Chris Mason26b80032007-08-08 20:17:12 -04001095 trans->alloc_exclude_start,
1096 trans->alloc_exclude_nr, data);
Chris Masonccd467d2007-06-28 15:57:36 -04001097 BUG_ON(ret);
Chris Masonf2654de2007-06-26 12:20:46 -04001098 if (ret)
1099 return ret;
1100
Josef Bacik58176a92007-08-29 15:47:34 -04001101 /* block accounting for super block */
Chris Masondb945352007-10-15 16:15:53 -04001102 super_used = btrfs_super_bytes_used(&info->super_copy);
1103 btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes);
Chris Mason26b80032007-08-08 20:17:12 -04001104
Josef Bacik58176a92007-08-29 15:47:34 -04001105 /* block accounting for root item */
Chris Masondb945352007-10-15 16:15:53 -04001106 root_used = btrfs_root_used(&root->root_item);
1107 btrfs_set_root_used(&root->root_item, root_used + num_bytes);
Josef Bacik58176a92007-08-29 15:47:34 -04001108
Chris Masonf510cfe2007-10-15 16:14:48 -04001109 clear_extent_dirty(&root->fs_info->free_space_cache,
1110 ins->objectid, ins->objectid + ins->offset - 1,
1111 GFP_NOFS);
1112
Chris Mason26b80032007-08-08 20:17:12 -04001113 if (root == extent_root) {
Chris Mason1a5bc162007-10-15 16:15:26 -04001114 set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
1115 ins->objectid + ins->offset - 1,
1116 EXTENT_LOCKED, GFP_NOFS);
Chris Mason26b80032007-08-08 20:17:12 -04001117 goto update_block;
1118 }
1119
1120 WARN_ON(trans->alloc_exclude_nr);
1121 trans->alloc_exclude_start = ins->objectid;
1122 trans->alloc_exclude_nr = ins->offset;
Chris Masone089f052007-03-16 16:20:31 -04001123 ret = btrfs_insert_item(trans, extent_root, ins, &extent_item,
1124 sizeof(extent_item));
Chris Mason037e6392007-03-07 11:50:24 -05001125
Chris Mason26b80032007-08-08 20:17:12 -04001126 trans->alloc_exclude_start = 0;
1127 trans->alloc_exclude_nr = 0;
1128
Chris Masonccd467d2007-06-28 15:57:36 -04001129 BUG_ON(ret);
Chris Masone089f052007-03-16 16:20:31 -04001130 finish_current_insert(trans, extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -04001131 pending_ret = del_pending_extents(trans, extent_root);
Chris Masonf510cfe2007-10-15 16:14:48 -04001132
Chris Masone37c9e62007-05-09 20:13:14 -04001133 if (ret) {
Chris Mason037e6392007-03-07 11:50:24 -05001134 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -04001135 }
1136 if (pending_ret) {
Chris Mason037e6392007-03-07 11:50:24 -05001137 return pending_ret;
Chris Masone37c9e62007-05-09 20:13:14 -04001138 }
Chris Mason26b80032007-08-08 20:17:12 -04001139
1140update_block:
Chris Mason1e2677e2007-05-29 16:52:18 -04001141 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0,
1142 data);
Chris Masonfabb5682007-06-07 22:13:21 -04001143 BUG_ON(ret);
Chris Mason037e6392007-03-07 11:50:24 -05001144 return 0;
Chris Masonfec577f2007-02-26 10:40:21 -05001145}
1146
1147/*
1148 * helper function to allocate a block for a given tree
1149 * returns the tree buffer or NULL.
1150 */
Chris Mason5f39d392007-10-15 16:14:19 -04001151struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Chris Masondb945352007-10-15 16:15:53 -04001152 struct btrfs_root *root,
1153 u32 blocksize, u64 hint,
Chris Mason5f39d392007-10-15 16:14:19 -04001154 u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05001155{
Chris Masone2fa7222007-03-12 16:22:34 -04001156 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -05001157 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04001158 struct extent_buffer *buf;
Chris Masonfec577f2007-02-26 10:40:21 -05001159
Chris Mason4d775672007-04-20 20:23:12 -04001160 ret = btrfs_alloc_extent(trans, root, root->root_key.objectid,
Chris Masondb945352007-10-15 16:15:53 -04001161 blocksize, empty_size, hint,
1162 (u64)-1, &ins, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05001163 if (ret) {
Chris Mason54aa1f42007-06-22 14:16:25 -04001164 BUG_ON(ret > 0);
1165 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05001166 }
Chris Masondb945352007-10-15 16:15:53 -04001167 buf = btrfs_find_create_tree_block(root, ins.objectid, blocksize);
Chris Mason54aa1f42007-06-22 14:16:25 -04001168 if (!buf) {
Chris Masondb945352007-10-15 16:15:53 -04001169 btrfs_free_extent(trans, root, ins.objectid, blocksize, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001170 return ERR_PTR(-ENOMEM);
1171 }
Chris Mason5f39d392007-10-15 16:14:19 -04001172 btrfs_set_buffer_uptodate(buf);
Chris Masonf510cfe2007-10-15 16:14:48 -04001173 buf->alloc_addr = (unsigned long)__builtin_return_address(0);
Chris Mason5f39d392007-10-15 16:14:19 -04001174 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
1175 buf->start + buf->len - 1, GFP_NOFS);
Chris Mason6b800532007-10-15 16:17:34 -04001176 btrfs_set_buffer_defrag(buf);
Chris Masond3c2fdc2007-09-17 10:58:06 -04001177 trans->blocks_used++;
Chris Masonfec577f2007-02-26 10:40:21 -05001178 return buf;
1179}
Chris Masona28ec192007-03-06 20:08:01 -05001180
Chris Mason6407bf62007-03-27 06:33:00 -04001181static int drop_leaf_ref(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001182 struct btrfs_root *root, struct extent_buffer *leaf)
Chris Mason6407bf62007-03-27 06:33:00 -04001183{
Chris Mason5f39d392007-10-15 16:14:19 -04001184 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -04001185 struct btrfs_file_extent_item *fi;
1186 int i;
1187 int nritems;
1188 int ret;
1189
Chris Mason5f39d392007-10-15 16:14:19 -04001190 BUG_ON(!btrfs_is_leaf(leaf));
1191 nritems = btrfs_header_nritems(leaf);
Chris Mason6407bf62007-03-27 06:33:00 -04001192 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04001193 u64 disk_bytenr;
Chris Mason5f39d392007-10-15 16:14:19 -04001194
1195 btrfs_item_key_to_cpu(leaf, &key, i);
1196 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason6407bf62007-03-27 06:33:00 -04001197 continue;
1198 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001199 if (btrfs_file_extent_type(leaf, fi) ==
1200 BTRFS_FILE_EXTENT_INLINE)
Chris Mason236454d2007-04-19 13:37:44 -04001201 continue;
Chris Mason6407bf62007-03-27 06:33:00 -04001202 /*
1203 * FIXME make sure to insert a trans record that
1204 * repeats the snapshot del on crash
1205 */
Chris Masondb945352007-10-15 16:15:53 -04001206 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
1207 if (disk_bytenr == 0)
Chris Mason3a686372007-05-24 13:35:57 -04001208 continue;
Chris Masondb945352007-10-15 16:15:53 -04001209 ret = btrfs_free_extent(trans, root, disk_bytenr,
1210 btrfs_file_extent_disk_num_bytes(leaf, fi), 0);
Chris Mason6407bf62007-03-27 06:33:00 -04001211 BUG_ON(ret);
1212 }
1213 return 0;
1214}
1215
Chris Masone0115992007-06-19 16:23:05 -04001216static void reada_walk_down(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -04001217 struct extent_buffer *node)
Chris Masone0115992007-06-19 16:23:05 -04001218{
1219 int i;
1220 u32 nritems;
Chris Masondb945352007-10-15 16:15:53 -04001221 u64 bytenr;
Chris Masone0115992007-06-19 16:23:05 -04001222 int ret;
1223 u32 refs;
Chris Masondb945352007-10-15 16:15:53 -04001224 int level;
1225 u32 blocksize;
Chris Masone0115992007-06-19 16:23:05 -04001226
Chris Mason5f39d392007-10-15 16:14:19 -04001227 nritems = btrfs_header_nritems(node);
Chris Masondb945352007-10-15 16:15:53 -04001228 level = btrfs_header_level(node);
Chris Masone0115992007-06-19 16:23:05 -04001229 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04001230 bytenr = btrfs_node_blockptr(node, i);
1231 blocksize = btrfs_level_size(root, level - 1);
1232 ret = lookup_extent_ref(NULL, root, bytenr, blocksize, &refs);
Chris Masone0115992007-06-19 16:23:05 -04001233 BUG_ON(ret);
1234 if (refs != 1)
1235 continue;
Chris Mason409eb952007-08-08 20:17:12 -04001236 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masondb945352007-10-15 16:15:53 -04001237 ret = readahead_tree_block(root, bytenr, blocksize);
Chris Mason409eb952007-08-08 20:17:12 -04001238 cond_resched();
1239 mutex_lock(&root->fs_info->fs_mutex);
Chris Masone0115992007-06-19 16:23:05 -04001240 if (ret)
1241 break;
1242 }
1243}
1244
Chris Mason9aca1d52007-03-13 11:09:37 -04001245/*
1246 * helper function for drop_snapshot, this walks down the tree dropping ref
1247 * counts as it goes.
1248 */
Chris Masone089f052007-03-16 16:20:31 -04001249static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
1250 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05001251{
Chris Mason5f39d392007-10-15 16:14:19 -04001252 struct extent_buffer *next;
1253 struct extent_buffer *cur;
Chris Masondb945352007-10-15 16:15:53 -04001254 u64 bytenr;
1255 u32 blocksize;
Chris Mason20524f02007-03-10 06:35:47 -05001256 int ret;
1257 u32 refs;
1258
Chris Mason5caf2a02007-04-02 11:20:42 -04001259 WARN_ON(*level < 0);
1260 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason5f39d392007-10-15 16:14:19 -04001261 ret = lookup_extent_ref(trans, root,
Chris Masondb945352007-10-15 16:15:53 -04001262 path->nodes[*level]->start,
1263 path->nodes[*level]->len, &refs);
Chris Mason20524f02007-03-10 06:35:47 -05001264 BUG_ON(ret);
1265 if (refs > 1)
1266 goto out;
Chris Masone0115992007-06-19 16:23:05 -04001267
Chris Mason9aca1d52007-03-13 11:09:37 -04001268 /*
1269 * walk down to the last node level and free all the leaves
1270 */
Chris Mason6407bf62007-03-27 06:33:00 -04001271 while(*level >= 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001272 WARN_ON(*level < 0);
1273 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason20524f02007-03-10 06:35:47 -05001274 cur = path->nodes[*level];
Chris Masone0115992007-06-19 16:23:05 -04001275
1276 if (*level > 0 && path->slots[*level] == 0)
Chris Mason5f39d392007-10-15 16:14:19 -04001277 reada_walk_down(root, cur);
Chris Masone0115992007-06-19 16:23:05 -04001278
Chris Mason5f39d392007-10-15 16:14:19 -04001279 if (btrfs_header_level(cur) != *level)
Chris Mason2c90e5d2007-04-02 10:50:19 -04001280 WARN_ON(1);
Chris Masone0115992007-06-19 16:23:05 -04001281
Chris Mason7518a232007-03-12 12:01:18 -04001282 if (path->slots[*level] >=
Chris Mason5f39d392007-10-15 16:14:19 -04001283 btrfs_header_nritems(cur))
Chris Mason20524f02007-03-10 06:35:47 -05001284 break;
Chris Mason6407bf62007-03-27 06:33:00 -04001285 if (*level == 0) {
1286 ret = drop_leaf_ref(trans, root, cur);
1287 BUG_ON(ret);
1288 break;
1289 }
Chris Masondb945352007-10-15 16:15:53 -04001290 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
1291 blocksize = btrfs_level_size(root, *level - 1);
1292 ret = lookup_extent_ref(trans, root, bytenr, blocksize, &refs);
Chris Mason6407bf62007-03-27 06:33:00 -04001293 BUG_ON(ret);
1294 if (refs != 1) {
Chris Mason20524f02007-03-10 06:35:47 -05001295 path->slots[*level]++;
Chris Masondb945352007-10-15 16:15:53 -04001296 ret = btrfs_free_extent(trans, root, bytenr,
1297 blocksize, 1);
Chris Mason20524f02007-03-10 06:35:47 -05001298 BUG_ON(ret);
1299 continue;
1300 }
Chris Masondb945352007-10-15 16:15:53 -04001301 next = btrfs_find_tree_block(root, bytenr, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -04001302 if (!next || !btrfs_buffer_uptodate(next)) {
1303 free_extent_buffer(next);
Chris Masone9d0b132007-08-10 14:06:19 -04001304 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masondb945352007-10-15 16:15:53 -04001305 next = read_tree_block(root, bytenr, blocksize);
Chris Masone9d0b132007-08-10 14:06:19 -04001306 mutex_lock(&root->fs_info->fs_mutex);
1307
1308 /* we dropped the lock, check one more time */
Chris Masondb945352007-10-15 16:15:53 -04001309 ret = lookup_extent_ref(trans, root, bytenr,
1310 blocksize, &refs);
Chris Masone9d0b132007-08-10 14:06:19 -04001311 BUG_ON(ret);
1312 if (refs != 1) {
1313 path->slots[*level]++;
Chris Mason5f39d392007-10-15 16:14:19 -04001314 free_extent_buffer(next);
Chris Masone9d0b132007-08-10 14:06:19 -04001315 ret = btrfs_free_extent(trans, root,
Chris Masondb945352007-10-15 16:15:53 -04001316 bytenr, blocksize, 1);
Chris Masone9d0b132007-08-10 14:06:19 -04001317 BUG_ON(ret);
1318 continue;
1319 }
1320 }
Chris Mason5caf2a02007-04-02 11:20:42 -04001321 WARN_ON(*level <= 0);
Chris Mason83e15a22007-03-12 09:03:27 -04001322 if (path->nodes[*level-1])
Chris Mason5f39d392007-10-15 16:14:19 -04001323 free_extent_buffer(path->nodes[*level-1]);
Chris Mason20524f02007-03-10 06:35:47 -05001324 path->nodes[*level-1] = next;
Chris Mason5f39d392007-10-15 16:14:19 -04001325 *level = btrfs_header_level(next);
Chris Mason20524f02007-03-10 06:35:47 -05001326 path->slots[*level] = 0;
1327 }
1328out:
Chris Mason5caf2a02007-04-02 11:20:42 -04001329 WARN_ON(*level < 0);
1330 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Masondb945352007-10-15 16:15:53 -04001331 ret = btrfs_free_extent(trans, root, path->nodes[*level]->start,
1332 path->nodes[*level]->len, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001333 free_extent_buffer(path->nodes[*level]);
Chris Mason20524f02007-03-10 06:35:47 -05001334 path->nodes[*level] = NULL;
1335 *level += 1;
1336 BUG_ON(ret);
1337 return 0;
1338}
1339
Chris Mason9aca1d52007-03-13 11:09:37 -04001340/*
1341 * helper for dropping snapshots. This walks back up the tree in the path
1342 * to find the first node higher up where we haven't yet gone through
1343 * all the slots
1344 */
Chris Masone089f052007-03-16 16:20:31 -04001345static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root
1346 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05001347{
1348 int i;
1349 int slot;
1350 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04001351 struct btrfs_root_item *root_item = &root->root_item;
1352
Chris Mason234b63a2007-03-13 10:46:10 -04001353 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Mason20524f02007-03-10 06:35:47 -05001354 slot = path->slots[i];
Chris Mason5f39d392007-10-15 16:14:19 -04001355 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
1356 struct extent_buffer *node;
1357 struct btrfs_disk_key disk_key;
1358 node = path->nodes[i];
Chris Mason20524f02007-03-10 06:35:47 -05001359 path->slots[i]++;
1360 *level = i;
Chris Mason9f3a7422007-08-07 15:52:19 -04001361 WARN_ON(*level == 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001362 btrfs_node_key(node, &disk_key, path->slots[i]);
Chris Mason9f3a7422007-08-07 15:52:19 -04001363 memcpy(&root_item->drop_progress,
Chris Mason5f39d392007-10-15 16:14:19 -04001364 &disk_key, sizeof(disk_key));
Chris Mason9f3a7422007-08-07 15:52:19 -04001365 root_item->drop_level = i;
Chris Mason20524f02007-03-10 06:35:47 -05001366 return 0;
1367 } else {
Chris Masone089f052007-03-16 16:20:31 -04001368 ret = btrfs_free_extent(trans, root,
Chris Masondb945352007-10-15 16:15:53 -04001369 path->nodes[*level]->start,
1370 path->nodes[*level]->len, 1);
Chris Mason6407bf62007-03-27 06:33:00 -04001371 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04001372 free_extent_buffer(path->nodes[*level]);
Chris Mason83e15a22007-03-12 09:03:27 -04001373 path->nodes[*level] = NULL;
Chris Mason20524f02007-03-10 06:35:47 -05001374 *level = i + 1;
Chris Mason20524f02007-03-10 06:35:47 -05001375 }
1376 }
1377 return 1;
1378}
1379
Chris Mason9aca1d52007-03-13 11:09:37 -04001380/*
1381 * drop the reference count on the tree rooted at 'snap'. This traverses
1382 * the tree freeing any blocks that have a ref count of zero after being
1383 * decremented.
1384 */
Chris Masone089f052007-03-16 16:20:31 -04001385int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason9f3a7422007-08-07 15:52:19 -04001386 *root)
Chris Mason20524f02007-03-10 06:35:47 -05001387{
Chris Mason3768f362007-03-13 16:47:54 -04001388 int ret = 0;
Chris Mason9aca1d52007-03-13 11:09:37 -04001389 int wret;
Chris Mason20524f02007-03-10 06:35:47 -05001390 int level;
Chris Mason5caf2a02007-04-02 11:20:42 -04001391 struct btrfs_path *path;
Chris Mason20524f02007-03-10 06:35:47 -05001392 int i;
1393 int orig_level;
Chris Mason9f3a7422007-08-07 15:52:19 -04001394 struct btrfs_root_item *root_item = &root->root_item;
Chris Mason20524f02007-03-10 06:35:47 -05001395
Chris Mason5caf2a02007-04-02 11:20:42 -04001396 path = btrfs_alloc_path();
1397 BUG_ON(!path);
Chris Mason20524f02007-03-10 06:35:47 -05001398
Chris Mason5f39d392007-10-15 16:14:19 -04001399 level = btrfs_header_level(root->node);
Chris Mason20524f02007-03-10 06:35:47 -05001400 orig_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04001401 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
1402 path->nodes[level] = root->node;
Chris Masonf510cfe2007-10-15 16:14:48 -04001403 extent_buffer_get(root->node);
Chris Mason9f3a7422007-08-07 15:52:19 -04001404 path->slots[level] = 0;
1405 } else {
1406 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001407 struct btrfs_disk_key found_key;
1408 struct extent_buffer *node;
Chris Mason6702ed42007-08-07 16:15:09 -04001409
Chris Mason9f3a7422007-08-07 15:52:19 -04001410 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Chris Mason6702ed42007-08-07 16:15:09 -04001411 level = root_item->drop_level;
1412 path->lowest_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04001413 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason6702ed42007-08-07 16:15:09 -04001414 if (wret < 0) {
Chris Mason9f3a7422007-08-07 15:52:19 -04001415 ret = wret;
1416 goto out;
1417 }
Chris Mason5f39d392007-10-15 16:14:19 -04001418 node = path->nodes[level];
1419 btrfs_node_key(node, &found_key, path->slots[level]);
1420 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
1421 sizeof(found_key)));
Chris Mason9f3a7422007-08-07 15:52:19 -04001422 }
Chris Mason20524f02007-03-10 06:35:47 -05001423 while(1) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001424 wret = walk_down_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04001425 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05001426 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04001427 if (wret < 0)
1428 ret = wret;
1429
Chris Mason5caf2a02007-04-02 11:20:42 -04001430 wret = walk_up_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04001431 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05001432 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04001433 if (wret < 0)
1434 ret = wret;
Chris Mason409eb952007-08-08 20:17:12 -04001435 ret = -EAGAIN;
Chris Mason409eb952007-08-08 20:17:12 -04001436 break;
Chris Mason20524f02007-03-10 06:35:47 -05001437 }
Chris Mason83e15a22007-03-12 09:03:27 -04001438 for (i = 0; i <= orig_level; i++) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001439 if (path->nodes[i]) {
Chris Mason5f39d392007-10-15 16:14:19 -04001440 free_extent_buffer(path->nodes[i]);
Chris Mason6702ed42007-08-07 16:15:09 -04001441 path->nodes[i] = 0;
Chris Mason83e15a22007-03-12 09:03:27 -04001442 }
Chris Mason20524f02007-03-10 06:35:47 -05001443 }
Chris Mason9f3a7422007-08-07 15:52:19 -04001444out:
Chris Mason5caf2a02007-04-02 11:20:42 -04001445 btrfs_free_path(path);
Chris Mason9aca1d52007-03-13 11:09:37 -04001446 return ret;
Chris Mason20524f02007-03-10 06:35:47 -05001447}
Chris Mason9078a3e2007-04-26 16:46:15 -04001448
Chris Masonbe744172007-05-06 10:15:01 -04001449int btrfs_free_block_groups(struct btrfs_fs_info *info)
1450{
Chris Masonf510cfe2007-10-15 16:14:48 -04001451 u64 start;
1452 u64 end;
Chris Mason96b51792007-10-15 16:15:19 -04001453 int ret;
Chris Masonbe744172007-05-06 10:15:01 -04001454
Chris Mason96b51792007-10-15 16:15:19 -04001455 while(1) {
1456 ret = find_first_extent_bit(&info->block_group_cache, 0,
1457 &start, &end, (unsigned int)-1);
1458 if (ret)
1459 break;
1460 clear_extent_bits(&info->block_group_cache, start,
1461 end, (unsigned int)-1, GFP_NOFS);
1462 }
Chris Masone37c9e62007-05-09 20:13:14 -04001463 while(1) {
Chris Masonf510cfe2007-10-15 16:14:48 -04001464 ret = find_first_extent_bit(&info->free_space_cache, 0,
1465 &start, &end, EXTENT_DIRTY);
1466 if (ret)
Chris Masone37c9e62007-05-09 20:13:14 -04001467 break;
Chris Masonf510cfe2007-10-15 16:14:48 -04001468 clear_extent_dirty(&info->free_space_cache, start,
1469 end, GFP_NOFS);
Chris Masone37c9e62007-05-09 20:13:14 -04001470 }
Chris Masonbe744172007-05-06 10:15:01 -04001471 return 0;
1472}
1473
Chris Mason9078a3e2007-04-26 16:46:15 -04001474int btrfs_read_block_groups(struct btrfs_root *root)
1475{
1476 struct btrfs_path *path;
1477 int ret;
1478 int err = 0;
Chris Mason96b51792007-10-15 16:15:19 -04001479 int bit;
Chris Mason9078a3e2007-04-26 16:46:15 -04001480 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04001481 struct btrfs_fs_info *info = root->fs_info;
Chris Mason96b51792007-10-15 16:15:19 -04001482 struct extent_map_tree *block_group_cache;
Chris Mason9078a3e2007-04-26 16:46:15 -04001483 struct btrfs_key key;
1484 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -04001485 struct extent_buffer *leaf;
Chris Mason96b51792007-10-15 16:15:19 -04001486
1487 block_group_cache = &info->block_group_cache;
Chris Mason9078a3e2007-04-26 16:46:15 -04001488
Chris Masonbe744172007-05-06 10:15:01 -04001489 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04001490 key.objectid = 0;
Chris Masondb945352007-10-15 16:15:53 -04001491 key.offset = BTRFS_BLOCK_GROUP_SIZE;
Chris Mason9078a3e2007-04-26 16:46:15 -04001492 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
1493
1494 path = btrfs_alloc_path();
1495 if (!path)
1496 return -ENOMEM;
1497
1498 while(1) {
Chris Masonbe744172007-05-06 10:15:01 -04001499 ret = btrfs_search_slot(NULL, info->extent_root,
Chris Mason9078a3e2007-04-26 16:46:15 -04001500 &key, path, 0, 0);
1501 if (ret != 0) {
1502 err = ret;
1503 break;
1504 }
Chris Mason5f39d392007-10-15 16:14:19 -04001505 leaf = path->nodes[0];
1506 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason9078a3e2007-04-26 16:46:15 -04001507 cache = kmalloc(sizeof(*cache), GFP_NOFS);
1508 if (!cache) {
1509 err = -1;
1510 break;
1511 }
Chris Mason3e1ad542007-05-07 20:03:49 -04001512
Chris Mason5f39d392007-10-15 16:14:19 -04001513 read_extent_buffer(leaf, &cache->item,
1514 btrfs_item_ptr_offset(leaf, path->slots[0]),
1515 sizeof(cache->item));
Chris Mason9078a3e2007-04-26 16:46:15 -04001516 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Masone37c9e62007-05-09 20:13:14 -04001517 cache->cached = 0;
1518
Chris Mason9078a3e2007-04-26 16:46:15 -04001519 key.objectid = found_key.objectid + found_key.offset;
1520 btrfs_release_path(root, path);
Chris Mason5f39d392007-10-15 16:14:19 -04001521
Chris Mason96b51792007-10-15 16:15:19 -04001522 if (cache->item.flags & BTRFS_BLOCK_GROUP_DATA) {
1523 bit = BLOCK_GROUP_DATA;
1524 cache->data = 1;
1525 } else {
1526 bit = BLOCK_GROUP_METADATA;
1527 cache->data = 0;
Chris Mason31f3c992007-04-30 15:25:45 -04001528 }
Chris Mason96b51792007-10-15 16:15:19 -04001529
1530 /* use EXTENT_LOCKED to prevent merging */
1531 set_extent_bits(block_group_cache, found_key.objectid,
1532 found_key.objectid + found_key.offset - 1,
1533 bit | EXTENT_LOCKED, GFP_NOFS);
1534 set_state_private(block_group_cache, found_key.objectid,
1535 (u64)cache);
1536
Chris Mason9078a3e2007-04-26 16:46:15 -04001537 if (key.objectid >=
Chris Masondb945352007-10-15 16:15:53 -04001538 btrfs_super_total_bytes(&info->super_copy))
Chris Mason9078a3e2007-04-26 16:46:15 -04001539 break;
1540 }
1541
1542 btrfs_free_path(path);
1543 return 0;
1544}