blob: e4e68ea96483448808eaed53f7f731386a8590ec [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;
Chris Masone19caa52007-10-15 16:17:44 -0400168 u64 last;
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 Masone19caa52007-10-15 16:17:44 -0400176 last = max(search_start, cache->key.objectid);
177
Chris Masone37c9e62007-05-09 20:13:14 -0400178 while(1) {
Chris Masonf510cfe2007-10-15 16:14:48 -0400179 ret = find_first_extent_bit(&root->fs_info->free_space_cache,
180 last, &start, &end, EXTENT_DIRTY);
Chris Masone19caa52007-10-15 16:17:44 -0400181 if (ret) {
182 goto new_group;
183 }
Chris Masonf510cfe2007-10-15 16:14:48 -0400184
185 start = max(last, start);
186 last = end + 1;
187 if (end + 1 - start < num)
188 continue;
Chris Mason96b51792007-10-15 16:15:19 -0400189 if (start + num >= cache->key.objectid + cache->key.offset)
Chris Masone37c9e62007-05-09 20:13:14 -0400190 goto new_group;
Chris Masonf510cfe2007-10-15 16:14:48 -0400191 return start;
Chris Masone37c9e62007-05-09 20:13:14 -0400192 }
193out:
Chris Mason1a5bc162007-10-15 16:15:26 -0400194 return search_start;
Chris Masone37c9e62007-05-09 20:13:14 -0400195
196new_group:
Chris Masone19caa52007-10-15 16:17:44 -0400197 last = cache->key.objectid + cache->key.offset;
198 cache = btrfs_lookup_block_group(root->fs_info, last);
Chris Masone37c9e62007-05-09 20:13:14 -0400199 if (!cache) {
Chris Mason1a5bc162007-10-15 16:15:26 -0400200 return search_start;
Chris Masone37c9e62007-05-09 20:13:14 -0400201 }
Chris Masone19caa52007-10-15 16:17:44 -0400202 cache = btrfs_find_block_group(root, cache, last, data, 0);
Chris Masone37c9e62007-05-09 20:13:14 -0400203 *cache_ret = cache;
204 goto again;
205}
206
Chris Mason84f54cf2007-06-12 07:43:08 -0400207static u64 div_factor(u64 num, int factor)
208{
209 num *= factor;
210 do_div(num, 10);
211 return num;
212}
213
Chris Mason31f3c992007-04-30 15:25:45 -0400214struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
215 struct btrfs_block_group_cache
Chris Masonbe744172007-05-06 10:15:01 -0400216 *hint, u64 search_start,
Chris Masonde428b62007-05-18 13:28:27 -0400217 int data, int owner)
Chris Masoncd1bc462007-04-27 10:08:34 -0400218{
Chris Mason96b51792007-10-15 16:15:19 -0400219 struct btrfs_block_group_cache *cache;
220 struct extent_map_tree *block_group_cache;
Chris Mason31f3c992007-04-30 15:25:45 -0400221 struct btrfs_block_group_cache *found_group = NULL;
Chris Masoncd1bc462007-04-27 10:08:34 -0400222 struct btrfs_fs_info *info = root->fs_info;
223 u64 used;
Chris Mason31f3c992007-04-30 15:25:45 -0400224 u64 last = 0;
225 u64 hint_last;
Chris Mason96b51792007-10-15 16:15:19 -0400226 u64 start;
227 u64 end;
228 u64 free_check;
229 u64 ptr;
230 int bit;
Chris Masoncd1bc462007-04-27 10:08:34 -0400231 int ret;
Chris Mason31f3c992007-04-30 15:25:45 -0400232 int full_search = 0;
Chris Masonde428b62007-05-18 13:28:27 -0400233 int factor = 8;
Chris Mason1e2677e2007-05-29 16:52:18 -0400234 int data_swap = 0;
Chris Masonde428b62007-05-18 13:28:27 -0400235
Chris Mason96b51792007-10-15 16:15:19 -0400236 block_group_cache = &info->block_group_cache;
237
Chris Masonde428b62007-05-18 13:28:27 -0400238 if (!owner)
239 factor = 5;
Chris Masonbe744172007-05-06 10:15:01 -0400240
Chris Mason96b51792007-10-15 16:15:19 -0400241 if (data)
242 bit = BLOCK_GROUP_DATA;
243 else
244 bit = BLOCK_GROUP_METADATA;
Chris Masonbe744172007-05-06 10:15:01 -0400245
246 if (search_start) {
247 struct btrfs_block_group_cache *shint;
Chris Mason5276aed2007-06-11 21:33:38 -0400248 shint = btrfs_lookup_block_group(info, search_start);
Yane9fe3952007-08-29 09:11:44 -0400249 if (shint && shint->data == data) {
Chris Masonbe744172007-05-06 10:15:01 -0400250 used = btrfs_block_group_used(&shint->item);
Chris Mason1a5bc162007-10-15 16:15:26 -0400251 if (used < 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 Mason1a5bc162007-10-15 16:15:26 -0400258 if (used < div_factor(hint->key.offset, factor)) {
Chris Mason31f3c992007-04-30 15:25:45 -0400259 return hint;
260 }
Chris Masone19caa52007-10-15 16:17:44 -0400261 last = hint->key.objectid + hint->key.offset;
Chris Mason31f3c992007-04-30 15:25:45 -0400262 hint_last = last;
263 } else {
Chris Masone37c9e62007-05-09 20:13:14 -0400264 if (hint)
265 hint_last = max(hint->key.objectid, search_start);
266 else
267 hint_last = search_start;
268
269 last = hint_last;
Chris Mason31f3c992007-04-30 15:25:45 -0400270 }
Chris Mason31f3c992007-04-30 15:25:45 -0400271again:
Chris Masoncd1bc462007-04-27 10:08:34 -0400272 while(1) {
Chris Mason96b51792007-10-15 16:15:19 -0400273 ret = find_first_extent_bit(block_group_cache, last,
274 &start, &end, bit);
275 if (ret)
Chris Masoncd1bc462007-04-27 10:08:34 -0400276 break;
Chris Mason96b51792007-10-15 16:15:19 -0400277
278 ret = get_state_private(block_group_cache, start, &ptr);
279 if (ret)
280 break;
281
282 cache = (struct btrfs_block_group_cache *)ptr;
283 last = cache->key.objectid + cache->key.offset;
284 used = btrfs_block_group_used(&cache->item);
285
286 if (full_search)
287 free_check = cache->key.offset;
288 else
289 free_check = div_factor(cache->key.offset, factor);
290
Chris Mason1a5bc162007-10-15 16:15:26 -0400291 if (used < free_check) {
Chris Mason96b51792007-10-15 16:15:19 -0400292 found_group = cache;
293 goto found;
Chris Masoncd1bc462007-04-27 10:08:34 -0400294 }
Chris Masonde428b62007-05-18 13:28:27 -0400295 cond_resched();
Chris Masoncd1bc462007-04-27 10:08:34 -0400296 }
Chris Mason31f3c992007-04-30 15:25:45 -0400297 if (!full_search) {
Chris Masonbe744172007-05-06 10:15:01 -0400298 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -0400299 full_search = 1;
300 goto again;
301 }
Chris Mason1e2677e2007-05-29 16:52:18 -0400302 if (!data_swap) {
Chris Mason1e2677e2007-05-29 16:52:18 -0400303 data_swap = 1;
Chris Mason96b51792007-10-15 16:15:19 -0400304 bit = BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA;
Chris Mason1e2677e2007-05-29 16:52:18 -0400305 last = search_start;
306 goto again;
307 }
Chris Masonbe744172007-05-06 10:15:01 -0400308found:
Chris Mason31f3c992007-04-30 15:25:45 -0400309 return found_group;
Chris Masoncd1bc462007-04-27 10:08:34 -0400310}
311
Chris Masonb18c6682007-04-17 13:26:50 -0400312int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
313 struct btrfs_root *root,
Chris Masondb945352007-10-15 16:15:53 -0400314 u64 bytenr, u64 num_bytes)
Chris Mason02217ed2007-03-02 16:08:05 -0500315{
Chris Mason5caf2a02007-04-02 11:20:42 -0400316 struct btrfs_path *path;
Chris Mason02217ed2007-03-02 16:08:05 -0500317 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400318 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400319 struct extent_buffer *l;
Chris Mason234b63a2007-03-13 10:46:10 -0400320 struct btrfs_extent_item *item;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400321 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500322
Chris Masondb945352007-10-15 16:15:53 -0400323 WARN_ON(num_bytes < root->sectorsize);
Chris Mason5caf2a02007-04-02 11:20:42 -0400324 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -0400325 if (!path)
326 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -0400327
Chris Masondb945352007-10-15 16:15:53 -0400328 key.objectid = bytenr;
Chris Mason62e27492007-03-15 12:56:47 -0400329 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masondb945352007-10-15 16:15:53 -0400330 key.offset = num_bytes;
Chris Mason5caf2a02007-04-02 11:20:42 -0400331 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -0400332 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400333 if (ret < 0)
334 return ret;
Chris Masona429e512007-04-18 16:15:28 -0400335 if (ret != 0) {
Chris Masona28ec192007-03-06 20:08:01 -0500336 BUG();
Chris Masona429e512007-04-18 16:15:28 -0400337 }
Chris Mason02217ed2007-03-02 16:08:05 -0500338 BUG_ON(ret != 0);
Chris Mason5f39d392007-10-15 16:14:19 -0400339 l = path->nodes[0];
Chris Mason5caf2a02007-04-02 11:20:42 -0400340 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400341 refs = btrfs_extent_refs(l, item);
342 btrfs_set_extent_refs(l, item, refs + 1);
Chris Mason5caf2a02007-04-02 11:20:42 -0400343 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masona28ec192007-03-06 20:08:01 -0500344
Chris Mason5caf2a02007-04-02 11:20:42 -0400345 btrfs_release_path(root->fs_info->extent_root, path);
346 btrfs_free_path(path);
Chris Mason9f5fae22007-03-20 14:38:32 -0400347 finish_current_insert(trans, root->fs_info->extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -0400348 del_pending_extents(trans, root->fs_info->extent_root);
Chris Mason02217ed2007-03-02 16:08:05 -0500349 return 0;
350}
351
Chris Masone9d0b132007-08-10 14:06:19 -0400352int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
353 struct btrfs_root *root)
354{
355 finish_current_insert(trans, root->fs_info->extent_root);
356 del_pending_extents(trans, root->fs_info->extent_root);
357 return 0;
358}
359
Chris Masonb18c6682007-04-17 13:26:50 -0400360static int lookup_extent_ref(struct btrfs_trans_handle *trans,
Chris Masondb945352007-10-15 16:15:53 -0400361 struct btrfs_root *root, u64 bytenr,
362 u64 num_bytes, u32 *refs)
Chris Masona28ec192007-03-06 20:08:01 -0500363{
Chris Mason5caf2a02007-04-02 11:20:42 -0400364 struct btrfs_path *path;
Chris Masona28ec192007-03-06 20:08:01 -0500365 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400366 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400367 struct extent_buffer *l;
Chris Mason234b63a2007-03-13 10:46:10 -0400368 struct btrfs_extent_item *item;
Chris Mason5caf2a02007-04-02 11:20:42 -0400369
Chris Masondb945352007-10-15 16:15:53 -0400370 WARN_ON(num_bytes < root->sectorsize);
Chris Mason5caf2a02007-04-02 11:20:42 -0400371 path = btrfs_alloc_path();
Chris Masondb945352007-10-15 16:15:53 -0400372 key.objectid = bytenr;
373 key.offset = num_bytes;
Chris Mason62e27492007-03-15 12:56:47 -0400374 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Mason5caf2a02007-04-02 11:20:42 -0400375 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -0400376 0, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400377 if (ret < 0)
378 goto out;
Chris Mason5f39d392007-10-15 16:14:19 -0400379 if (ret != 0) {
380 btrfs_print_leaf(root, path->nodes[0]);
Chris Masondb945352007-10-15 16:15:53 -0400381 printk("failed to find block number %Lu\n", bytenr);
Chris Masona28ec192007-03-06 20:08:01 -0500382 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -0400383 }
384 l = path->nodes[0];
Chris Mason5caf2a02007-04-02 11:20:42 -0400385 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400386 *refs = btrfs_extent_refs(l, item);
Chris Mason54aa1f42007-06-22 14:16:25 -0400387out:
Chris Mason5caf2a02007-04-02 11:20:42 -0400388 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -0500389 return 0;
390}
391
Chris Masonc5739bb2007-04-10 09:27:04 -0400392int btrfs_inc_root_ref(struct btrfs_trans_handle *trans,
393 struct btrfs_root *root)
394{
Chris Masondb945352007-10-15 16:15:53 -0400395 return btrfs_inc_extent_ref(trans, root, root->node->start,
396 root->node->len);
Chris Masonc5739bb2007-04-10 09:27:04 -0400397}
398
Chris Masone089f052007-03-16 16:20:31 -0400399int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400400 struct extent_buffer *buf)
Chris Mason02217ed2007-03-02 16:08:05 -0500401{
Chris Masondb945352007-10-15 16:15:53 -0400402 u64 bytenr;
Chris Mason5f39d392007-10-15 16:14:19 -0400403 u32 nritems;
404 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -0400405 struct btrfs_file_extent_item *fi;
Chris Mason02217ed2007-03-02 16:08:05 -0500406 int i;
Chris Masondb945352007-10-15 16:15:53 -0400407 int level;
Chris Mason6407bf62007-03-27 06:33:00 -0400408 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -0400409 int faili;
410 int err;
Chris Masona28ec192007-03-06 20:08:01 -0500411
Chris Mason3768f362007-03-13 16:47:54 -0400412 if (!root->ref_cows)
Chris Masona28ec192007-03-06 20:08:01 -0500413 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400414
Chris Masondb945352007-10-15 16:15:53 -0400415 level = btrfs_header_level(buf);
Chris Mason5f39d392007-10-15 16:14:19 -0400416 nritems = btrfs_header_nritems(buf);
417 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -0400418 if (level == 0) {
419 u64 disk_bytenr;
Chris Mason5f39d392007-10-15 16:14:19 -0400420 btrfs_item_key_to_cpu(buf, &key, i);
421 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason6407bf62007-03-27 06:33:00 -0400422 continue;
Chris Mason5f39d392007-10-15 16:14:19 -0400423 fi = btrfs_item_ptr(buf, i,
Chris Mason6407bf62007-03-27 06:33:00 -0400424 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400425 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason236454d2007-04-19 13:37:44 -0400426 BTRFS_FILE_EXTENT_INLINE)
427 continue;
Chris Masondb945352007-10-15 16:15:53 -0400428 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
429 if (disk_bytenr == 0)
Chris Mason3a686372007-05-24 13:35:57 -0400430 continue;
Chris Masondb945352007-10-15 16:15:53 -0400431 ret = btrfs_inc_extent_ref(trans, root, disk_bytenr,
432 btrfs_file_extent_disk_num_bytes(buf, fi));
Chris Mason54aa1f42007-06-22 14:16:25 -0400433 if (ret) {
434 faili = i;
435 goto fail;
436 }
Chris Mason6407bf62007-03-27 06:33:00 -0400437 } else {
Chris Masondb945352007-10-15 16:15:53 -0400438 bytenr = btrfs_node_blockptr(buf, i);
439 ret = btrfs_inc_extent_ref(trans, root, bytenr,
440 btrfs_level_size(root, level - 1));
Chris Mason54aa1f42007-06-22 14:16:25 -0400441 if (ret) {
442 faili = i;
443 goto fail;
444 }
Chris Mason6407bf62007-03-27 06:33:00 -0400445 }
Chris Mason02217ed2007-03-02 16:08:05 -0500446 }
447 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -0400448fail:
Chris Masonccd467d2007-06-28 15:57:36 -0400449 WARN_ON(1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400450 for (i =0; i < faili; i++) {
Chris Masondb945352007-10-15 16:15:53 -0400451 if (level == 0) {
452 u64 disk_bytenr;
Chris Mason5f39d392007-10-15 16:14:19 -0400453 btrfs_item_key_to_cpu(buf, &key, i);
454 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -0400455 continue;
Chris Mason5f39d392007-10-15 16:14:19 -0400456 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -0400457 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400458 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -0400459 BTRFS_FILE_EXTENT_INLINE)
460 continue;
Chris Masondb945352007-10-15 16:15:53 -0400461 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
462 if (disk_bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -0400463 continue;
Chris Masondb945352007-10-15 16:15:53 -0400464 err = btrfs_free_extent(trans, root, disk_bytenr,
465 btrfs_file_extent_disk_num_bytes(buf,
Chris Mason5f39d392007-10-15 16:14:19 -0400466 fi), 0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400467 BUG_ON(err);
468 } else {
Chris Masondb945352007-10-15 16:15:53 -0400469 bytenr = btrfs_node_blockptr(buf, i);
470 err = btrfs_free_extent(trans, root, bytenr,
471 btrfs_level_size(root, level - 1), 0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400472 BUG_ON(err);
473 }
474 }
475 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -0500476}
477
Chris Mason9078a3e2007-04-26 16:46:15 -0400478static int write_one_cache_group(struct btrfs_trans_handle *trans,
479 struct btrfs_root *root,
480 struct btrfs_path *path,
481 struct btrfs_block_group_cache *cache)
482{
483 int ret;
484 int pending_ret;
485 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -0400486 unsigned long bi;
487 struct extent_buffer *leaf;
Chris Mason9078a3e2007-04-26 16:46:15 -0400488
Chris Mason9078a3e2007-04-26 16:46:15 -0400489 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400490 if (ret < 0)
491 goto fail;
Chris Mason9078a3e2007-04-26 16:46:15 -0400492 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -0400493
494 leaf = path->nodes[0];
495 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
496 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
497 btrfs_mark_buffer_dirty(leaf);
Chris Mason9078a3e2007-04-26 16:46:15 -0400498 btrfs_release_path(extent_root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -0400499fail:
Chris Mason9078a3e2007-04-26 16:46:15 -0400500 finish_current_insert(trans, extent_root);
501 pending_ret = del_pending_extents(trans, extent_root);
502 if (ret)
503 return ret;
504 if (pending_ret)
505 return pending_ret;
506 return 0;
507
508}
509
Chris Mason96b51792007-10-15 16:15:19 -0400510int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
511 struct btrfs_root *root)
Chris Mason9078a3e2007-04-26 16:46:15 -0400512{
Chris Mason96b51792007-10-15 16:15:19 -0400513 struct extent_map_tree *block_group_cache;
514 struct btrfs_block_group_cache *cache;
Chris Mason9078a3e2007-04-26 16:46:15 -0400515 int ret;
516 int err = 0;
517 int werr = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -0400518 struct btrfs_path *path;
Chris Mason96b51792007-10-15 16:15:19 -0400519 u64 last = 0;
520 u64 start;
521 u64 end;
522 u64 ptr;
Chris Mason9078a3e2007-04-26 16:46:15 -0400523
Chris Mason96b51792007-10-15 16:15:19 -0400524 block_group_cache = &root->fs_info->block_group_cache;
Chris Mason9078a3e2007-04-26 16:46:15 -0400525 path = btrfs_alloc_path();
526 if (!path)
527 return -ENOMEM;
528
529 while(1) {
Chris Mason96b51792007-10-15 16:15:19 -0400530 ret = find_first_extent_bit(block_group_cache, last,
531 &start, &end, BLOCK_GROUP_DIRTY);
532 if (ret)
Chris Mason9078a3e2007-04-26 16:46:15 -0400533 break;
Chris Mason54aa1f42007-06-22 14:16:25 -0400534
Chris Mason96b51792007-10-15 16:15:19 -0400535 last = end + 1;
536 ret = get_state_private(block_group_cache, start, &ptr);
537 if (ret)
538 break;
539
540 cache = (struct btrfs_block_group_cache *)ptr;
541 err = write_one_cache_group(trans, root,
542 path, cache);
543 /*
544 * if we fail to write the cache group, we want
545 * to keep it marked dirty in hopes that a later
546 * write will work
547 */
548 if (err) {
549 werr = err;
550 continue;
Chris Mason9078a3e2007-04-26 16:46:15 -0400551 }
Chris Mason96b51792007-10-15 16:15:19 -0400552 clear_extent_bits(block_group_cache, start, end,
553 BLOCK_GROUP_DIRTY, GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -0400554 }
555 btrfs_free_path(path);
556 return werr;
557}
558
559static int update_block_group(struct btrfs_trans_handle *trans,
560 struct btrfs_root *root,
Chris Masondb945352007-10-15 16:15:53 -0400561 u64 bytenr, u64 num_bytes, int alloc,
562 int mark_free, int data)
Chris Mason9078a3e2007-04-26 16:46:15 -0400563{
564 struct btrfs_block_group_cache *cache;
565 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -0400566 u64 total = num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -0400567 u64 old_val;
Chris Masondb945352007-10-15 16:15:53 -0400568 u64 byte_in_group;
Chris Mason96b51792007-10-15 16:15:19 -0400569 u64 start;
570 u64 end;
Chris Mason3e1ad542007-05-07 20:03:49 -0400571
Chris Mason9078a3e2007-04-26 16:46:15 -0400572 while(total) {
Chris Masondb945352007-10-15 16:15:53 -0400573 cache = btrfs_lookup_block_group(info, bytenr);
Chris Mason3e1ad542007-05-07 20:03:49 -0400574 if (!cache) {
Chris Mason9078a3e2007-04-26 16:46:15 -0400575 return -1;
Chris Masoncd1bc462007-04-27 10:08:34 -0400576 }
Chris Masondb945352007-10-15 16:15:53 -0400577 byte_in_group = bytenr - cache->key.objectid;
578 WARN_ON(byte_in_group > cache->key.offset);
Chris Mason96b51792007-10-15 16:15:19 -0400579 start = cache->key.objectid;
580 end = start + cache->key.offset - 1;
581 set_extent_bits(&info->block_group_cache, start, end,
582 BLOCK_GROUP_DIRTY, GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -0400583
584 old_val = btrfs_block_group_used(&cache->item);
Chris Masondb945352007-10-15 16:15:53 -0400585 num_bytes = min(total, cache->key.offset - byte_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -0400586 if (alloc) {
Chris Mason1e2677e2007-05-29 16:52:18 -0400587 if (cache->data != data &&
Chris Mason84f54cf2007-06-12 07:43:08 -0400588 old_val < (cache->key.offset >> 1)) {
Chris Mason96b51792007-10-15 16:15:19 -0400589 int bit_to_clear;
590 int bit_to_set;
Chris Mason1e2677e2007-05-29 16:52:18 -0400591
Chris Mason96b51792007-10-15 16:15:19 -0400592 cache->data = data;
Chris Mason1e2677e2007-05-29 16:52:18 -0400593 if (data) {
Chris Mason96b51792007-10-15 16:15:19 -0400594 bit_to_clear = BLOCK_GROUP_DATA;
595 bit_to_set = BLOCK_GROUP_METADATA;
Chris Mason1e2677e2007-05-29 16:52:18 -0400596 cache->item.flags |=
597 BTRFS_BLOCK_GROUP_DATA;
598 } else {
Chris Mason96b51792007-10-15 16:15:19 -0400599 bit_to_clear = BLOCK_GROUP_METADATA;
600 bit_to_set = BLOCK_GROUP_DATA;
Chris Mason1e2677e2007-05-29 16:52:18 -0400601 cache->item.flags &=
602 ~BTRFS_BLOCK_GROUP_DATA;
603 }
Chris Mason96b51792007-10-15 16:15:19 -0400604 clear_extent_bits(&info->block_group_cache,
605 start, end, bit_to_clear,
606 GFP_NOFS);
607 set_extent_bits(&info->block_group_cache,
608 start, end, bit_to_set,
609 GFP_NOFS);
Chris Mason1e2677e2007-05-29 16:52:18 -0400610 }
Chris Masondb945352007-10-15 16:15:53 -0400611 old_val += num_bytes;
Chris Masoncd1bc462007-04-27 10:08:34 -0400612 } else {
Chris Masondb945352007-10-15 16:15:53 -0400613 old_val -= num_bytes;
Chris Masonf510cfe2007-10-15 16:14:48 -0400614 if (mark_free) {
615 set_extent_dirty(&info->free_space_cache,
Chris Masondb945352007-10-15 16:15:53 -0400616 bytenr, bytenr + num_bytes - 1,
Chris Masonf510cfe2007-10-15 16:14:48 -0400617 GFP_NOFS);
Chris Masone37c9e62007-05-09 20:13:14 -0400618 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400619 }
Chris Mason9078a3e2007-04-26 16:46:15 -0400620 btrfs_set_block_group_used(&cache->item, old_val);
Chris Masondb945352007-10-15 16:15:53 -0400621 total -= num_bytes;
622 bytenr += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -0400623 }
624 return 0;
625}
626
Chris Mason1a5bc162007-10-15 16:15:26 -0400627int btrfs_copy_pinned(struct btrfs_root *root, struct extent_map_tree *copy)
Chris Masonccd467d2007-06-28 15:57:36 -0400628{
Chris Masonccd467d2007-06-28 15:57:36 -0400629 u64 last = 0;
Chris Mason1a5bc162007-10-15 16:15:26 -0400630 u64 start;
631 u64 end;
632 struct extent_map_tree *pinned_extents = &root->fs_info->pinned_extents;
Chris Masonccd467d2007-06-28 15:57:36 -0400633 int ret;
Chris Masonccd467d2007-06-28 15:57:36 -0400634
635 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -0400636 ret = find_first_extent_bit(pinned_extents, last,
637 &start, &end, EXTENT_DIRTY);
638 if (ret)
Chris Masonccd467d2007-06-28 15:57:36 -0400639 break;
Chris Mason1a5bc162007-10-15 16:15:26 -0400640 set_extent_dirty(copy, start, end, GFP_NOFS);
641 last = end + 1;
Chris Masonccd467d2007-06-28 15:57:36 -0400642 }
643 return 0;
644}
645
646int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
647 struct btrfs_root *root,
Chris Mason1a5bc162007-10-15 16:15:26 -0400648 struct extent_map_tree *unpin)
Chris Masona28ec192007-03-06 20:08:01 -0500649{
Chris Mason1a5bc162007-10-15 16:15:26 -0400650 u64 start;
651 u64 end;
Chris Masona28ec192007-03-06 20:08:01 -0500652 int ret;
Chris Mason1a5bc162007-10-15 16:15:26 -0400653 struct extent_map_tree *pinned_extents = &root->fs_info->pinned_extents;
Chris Masonf510cfe2007-10-15 16:14:48 -0400654 struct extent_map_tree *free_space_cache;
655
656 free_space_cache = &root->fs_info->free_space_cache;
Chris Masona28ec192007-03-06 20:08:01 -0500657
658 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -0400659 ret = find_first_extent_bit(unpin, 0, &start, &end,
660 EXTENT_DIRTY);
661 if (ret)
Chris Masona28ec192007-03-06 20:08:01 -0500662 break;
Chris Mason1a5bc162007-10-15 16:15:26 -0400663
664 clear_extent_dirty(pinned_extents, start, end,
665 GFP_NOFS);
666 clear_extent_dirty(unpin, start, end, GFP_NOFS);
667 set_extent_dirty(free_space_cache, start, end, GFP_NOFS);
Chris Masona28ec192007-03-06 20:08:01 -0500668 }
669 return 0;
670}
671
Chris Masone089f052007-03-16 16:20:31 -0400672static int finish_current_insert(struct btrfs_trans_handle *trans, struct
673 btrfs_root *extent_root)
Chris Mason037e6392007-03-07 11:50:24 -0500674{
Chris Masone2fa7222007-03-12 16:22:34 -0400675 struct btrfs_key ins;
Chris Mason234b63a2007-03-13 10:46:10 -0400676 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -0500677 int ret;
Chris Mason1a5bc162007-10-15 16:15:26 -0400678 int err = 0;
679 u64 start;
680 u64 end;
Chris Mason1261ec42007-03-20 20:35:03 -0400681 struct btrfs_fs_info *info = extent_root->fs_info;
Chris Mason037e6392007-03-07 11:50:24 -0500682
Chris Mason5f39d392007-10-15 16:14:19 -0400683 btrfs_set_stack_extent_refs(&extent_item, 1);
Chris Mason62e27492007-03-15 12:56:47 -0400684 btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
Chris Mason5f39d392007-10-15 16:14:19 -0400685 btrfs_set_stack_extent_owner(&extent_item,
686 extent_root->root_key.objectid);
Chris Mason037e6392007-03-07 11:50:24 -0500687
Chris Mason26b80032007-08-08 20:17:12 -0400688 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -0400689 ret = find_first_extent_bit(&info->extent_ins, 0, &start,
690 &end, EXTENT_LOCKED);
691 if (ret)
Chris Mason26b80032007-08-08 20:17:12 -0400692 break;
693
Chris Mason1a5bc162007-10-15 16:15:26 -0400694 ins.objectid = start;
695 ins.offset = end + 1 - start;
696 err = btrfs_insert_item(trans, extent_root, &ins,
697 &extent_item, sizeof(extent_item));
698 clear_extent_bits(&info->extent_ins, start, end, EXTENT_LOCKED,
699 GFP_NOFS);
Chris Mason037e6392007-03-07 11:50:24 -0500700 }
Chris Mason037e6392007-03-07 11:50:24 -0500701 return 0;
702}
703
Chris Masondb945352007-10-15 16:15:53 -0400704static int pin_down_bytes(struct btrfs_root *root, u64 bytenr, u32 num_bytes,
705 int pending)
Chris Masone20d96d2007-03-22 12:13:20 -0400706{
Chris Mason1a5bc162007-10-15 16:15:26 -0400707 int err = 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400708 struct extent_buffer *buf;
Chris Mason78fae272007-03-25 11:35:08 -0400709
Chris Masonf4b9aa82007-03-27 11:05:53 -0400710 if (!pending) {
Chris Masondb945352007-10-15 16:15:53 -0400711 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
Chris Mason5f39d392007-10-15 16:14:19 -0400712 if (buf) {
713 if (btrfs_buffer_uptodate(buf)) {
Chris Mason2c90e5d2007-04-02 10:50:19 -0400714 u64 transid =
715 root->fs_info->running_transaction->transid;
Chris Mason5f39d392007-10-15 16:14:19 -0400716 if (btrfs_header_generation(buf) == transid) {
717 free_extent_buffer(buf);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400718 return 0;
719 }
Chris Masonf4b9aa82007-03-27 11:05:53 -0400720 }
Chris Mason5f39d392007-10-15 16:14:19 -0400721 free_extent_buffer(buf);
Chris Mason8ef97622007-03-26 10:15:30 -0400722 }
Chris Mason1a5bc162007-10-15 16:15:26 -0400723 set_extent_dirty(&root->fs_info->pinned_extents,
Chris Masondb945352007-10-15 16:15:53 -0400724 bytenr, bytenr + num_bytes - 1, GFP_NOFS);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400725 } else {
Chris Mason1a5bc162007-10-15 16:15:26 -0400726 set_extent_bits(&root->fs_info->pending_del,
Chris Masondb945352007-10-15 16:15:53 -0400727 bytenr, bytenr + num_bytes - 1,
728 EXTENT_LOCKED, GFP_NOFS);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400729 }
Chris Masonbe744172007-05-06 10:15:01 -0400730 BUG_ON(err < 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400731 return 0;
732}
733
Chris Masona28ec192007-03-06 20:08:01 -0500734/*
735 * remove an extent from the root, returns 0 on success
736 */
Chris Masone089f052007-03-16 16:20:31 -0400737static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masondb945352007-10-15 16:15:53 -0400738 *root, u64 bytenr, u64 num_bytes, int pin,
Chris Masone37c9e62007-05-09 20:13:14 -0400739 int mark_free)
Chris Masona28ec192007-03-06 20:08:01 -0500740{
Chris Mason5caf2a02007-04-02 11:20:42 -0400741 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -0400742 struct btrfs_key key;
Chris Mason1261ec42007-03-20 20:35:03 -0400743 struct btrfs_fs_info *info = root->fs_info;
744 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -0400745 struct extent_buffer *leaf;
Chris Masona28ec192007-03-06 20:08:01 -0500746 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400747 struct btrfs_extent_item *ei;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400748 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500749
Chris Masondb945352007-10-15 16:15:53 -0400750 key.objectid = bytenr;
Chris Mason62e27492007-03-15 12:56:47 -0400751 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masondb945352007-10-15 16:15:53 -0400752 key.offset = num_bytes;
Chris Masona28ec192007-03-06 20:08:01 -0500753
Chris Mason5caf2a02007-04-02 11:20:42 -0400754 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -0400755 if (!path)
756 return -ENOMEM;
757
Chris Mason5caf2a02007-04-02 11:20:42 -0400758 ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400759 if (ret < 0)
760 return ret;
761 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -0400762
763 leaf = path->nodes[0];
764 ei = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason123abc82007-03-14 14:14:43 -0400765 struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400766 refs = btrfs_extent_refs(leaf, ei);
767 BUG_ON(refs == 0);
768 refs -= 1;
769 btrfs_set_extent_refs(leaf, ei, refs);
770 btrfs_mark_buffer_dirty(leaf);
771
Chris Masoncf27e1e2007-03-13 09:49:06 -0400772 if (refs == 0) {
Chris Masondb945352007-10-15 16:15:53 -0400773 u64 super_used;
774 u64 root_used;
Chris Mason78fae272007-03-25 11:35:08 -0400775
776 if (pin) {
Chris Masondb945352007-10-15 16:15:53 -0400777 ret = pin_down_bytes(root, bytenr, num_bytes, 0);
Chris Mason78fae272007-03-25 11:35:08 -0400778 BUG_ON(ret);
779 }
780
Josef Bacik58176a92007-08-29 15:47:34 -0400781 /* block accounting for super block */
Chris Masondb945352007-10-15 16:15:53 -0400782 super_used = btrfs_super_bytes_used(&info->super_copy);
783 btrfs_set_super_bytes_used(&info->super_copy,
784 super_used - num_bytes);
Josef Bacik58176a92007-08-29 15:47:34 -0400785
786 /* block accounting for root item */
Chris Masondb945352007-10-15 16:15:53 -0400787 root_used = btrfs_root_used(&root->root_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400788 btrfs_set_root_used(&root->root_item,
Chris Masondb945352007-10-15 16:15:53 -0400789 root_used - num_bytes);
Josef Bacik58176a92007-08-29 15:47:34 -0400790
Chris Mason5caf2a02007-04-02 11:20:42 -0400791 ret = btrfs_del_item(trans, extent_root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -0400792 if (ret) {
793 return ret;
794 }
Chris Masondb945352007-10-15 16:15:53 -0400795 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
Chris Mason1e2677e2007-05-29 16:52:18 -0400796 mark_free, 0);
Chris Mason9078a3e2007-04-26 16:46:15 -0400797 BUG_ON(ret);
Chris Masona28ec192007-03-06 20:08:01 -0500798 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400799 btrfs_free_path(path);
Chris Masone089f052007-03-16 16:20:31 -0400800 finish_current_insert(trans, extent_root);
Chris Masona28ec192007-03-06 20:08:01 -0500801 return ret;
802}
803
804/*
Chris Masonfec577f2007-02-26 10:40:21 -0500805 * find all the blocks marked as pending in the radix tree and remove
806 * them from the extent map
807 */
Chris Masone089f052007-03-16 16:20:31 -0400808static int del_pending_extents(struct btrfs_trans_handle *trans, struct
809 btrfs_root *extent_root)
Chris Masonfec577f2007-02-26 10:40:21 -0500810{
811 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -0400812 int err = 0;
Chris Mason1a5bc162007-10-15 16:15:26 -0400813 u64 start;
814 u64 end;
815 struct extent_map_tree *pending_del;
816 struct extent_map_tree *pinned_extents;
Chris Mason8ef97622007-03-26 10:15:30 -0400817
Chris Mason1a5bc162007-10-15 16:15:26 -0400818 pending_del = &extent_root->fs_info->pending_del;
819 pinned_extents = &extent_root->fs_info->pinned_extents;
Chris Masonfec577f2007-02-26 10:40:21 -0500820
821 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -0400822 ret = find_first_extent_bit(pending_del, 0, &start, &end,
823 EXTENT_LOCKED);
824 if (ret)
Chris Masonfec577f2007-02-26 10:40:21 -0500825 break;
Chris Mason1a5bc162007-10-15 16:15:26 -0400826
827 set_extent_dirty(pinned_extents, start, end, GFP_NOFS);
828 clear_extent_bits(pending_del, start, end, EXTENT_LOCKED,
829 GFP_NOFS);
830 ret = __free_extent(trans, extent_root,
831 start, end + 1 - start, 0, 0);
832 if (ret)
833 err = ret;
Chris Masonfec577f2007-02-26 10:40:21 -0500834 }
Chris Masone20d96d2007-03-22 12:13:20 -0400835 return err;
Chris Masonfec577f2007-02-26 10:40:21 -0500836}
837
838/*
839 * remove an extent from the root, returns 0 on success
840 */
Chris Masone089f052007-03-16 16:20:31 -0400841int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masondb945352007-10-15 16:15:53 -0400842 *root, u64 bytenr, u64 num_bytes, int pin)
Chris Masonfec577f2007-02-26 10:40:21 -0500843{
Chris Mason9f5fae22007-03-20 14:38:32 -0400844 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Masonfec577f2007-02-26 10:40:21 -0500845 int pending_ret;
846 int ret;
Chris Masona28ec192007-03-06 20:08:01 -0500847
Chris Masondb945352007-10-15 16:15:53 -0400848 WARN_ON(num_bytes < root->sectorsize);
Chris Masona28ec192007-03-06 20:08:01 -0500849 if (root == extent_root) {
Chris Masondb945352007-10-15 16:15:53 -0400850 pin_down_bytes(root, bytenr, num_bytes, 1);
Chris Masona28ec192007-03-06 20:08:01 -0500851 return 0;
852 }
Chris Masondb945352007-10-15 16:15:53 -0400853 ret = __free_extent(trans, root, bytenr, num_bytes, pin, pin == 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400854 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
Chris Masonfec577f2007-02-26 10:40:21 -0500855 return ret ? ret : pending_ret;
856}
857
858/*
859 * walks the btree of allocated extents and find a hole of a given size.
860 * The key ins is changed to record the hole:
861 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -0400862 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -0500863 * ins->offset == number of blocks
864 * Any available blocks before search_start are skipped.
865 */
Chris Masone089f052007-03-16 16:20:31 -0400866static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masondb945352007-10-15 16:15:53 -0400867 *orig_root, u64 num_bytes, u64 empty_size,
868 u64 search_start, u64 search_end, u64 hint_byte,
Chris Masonf2654de2007-06-26 12:20:46 -0400869 struct btrfs_key *ins, u64 exclude_start,
870 u64 exclude_nr, int data)
Chris Masonfec577f2007-02-26 10:40:21 -0500871{
Chris Mason5caf2a02007-04-02 11:20:42 -0400872 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -0400873 struct btrfs_key key;
Chris Masonfec577f2007-02-26 10:40:21 -0500874 int ret;
875 u64 hole_size = 0;
876 int slot = 0;
Chris Masondb945352007-10-15 16:15:53 -0400877 u64 last_byte = 0;
Chris Masonbe744172007-05-06 10:15:01 -0400878 u64 orig_search_start = search_start;
Chris Masonfec577f2007-02-26 10:40:21 -0500879 int start_found;
Chris Mason5f39d392007-10-15 16:14:19 -0400880 struct extent_buffer *l;
Chris Mason9f5fae22007-03-20 14:38:32 -0400881 struct btrfs_root * root = orig_root->fs_info->extent_root;
Chris Masonf2458e12007-04-25 15:52:25 -0400882 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -0400883 u64 total_needed = num_bytes;
Chris Masone20d96d2007-03-22 12:13:20 -0400884 int level;
Chris Masonbe08c1b2007-05-03 09:06:49 -0400885 struct btrfs_block_group_cache *block_group;
Chris Masonbe744172007-05-06 10:15:01 -0400886 int full_scan = 0;
Chris Masonfbdc7622007-05-30 10:22:12 -0400887 int wrapped = 0;
Chris Masonfec577f2007-02-26 10:40:21 -0500888
Chris Masondb945352007-10-15 16:15:53 -0400889 WARN_ON(num_bytes < root->sectorsize);
Chris Masonb1a4d962007-04-04 15:27:52 -0400890 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
891
Chris Mason5f39d392007-10-15 16:14:19 -0400892 level = btrfs_header_level(root->node);
893
Chris Mason3e1ad542007-05-07 20:03:49 -0400894 if (search_end == (u64)-1)
Chris Masondb945352007-10-15 16:15:53 -0400895 search_end = btrfs_super_total_bytes(&info->super_copy);
896 if (hint_byte) {
897 block_group = btrfs_lookup_block_group(info, hint_byte);
Chris Masonbe744172007-05-06 10:15:01 -0400898 block_group = btrfs_find_block_group(root, block_group,
Chris Masondb945352007-10-15 16:15:53 -0400899 hint_byte, data, 1);
Chris Masonbe744172007-05-06 10:15:01 -0400900 } else {
901 block_group = btrfs_find_block_group(root,
902 trans->block_group, 0,
Chris Masonde428b62007-05-18 13:28:27 -0400903 data, 1);
Chris Masonbe744172007-05-06 10:15:01 -0400904 }
905
Chris Mason6702ed42007-08-07 16:15:09 -0400906 total_needed += empty_size;
Chris Masone0115992007-06-19 16:23:05 -0400907 path = btrfs_alloc_path();
908
Chris Masonbe744172007-05-06 10:15:01 -0400909check_failed:
Chris Masonf510cfe2007-10-15 16:14:48 -0400910 search_start = find_search_start(root, &block_group,
911 search_start, total_needed, data);
Chris Mason5caf2a02007-04-02 11:20:42 -0400912 btrfs_init_path(path);
Chris Masonfec577f2007-02-26 10:40:21 -0500913 ins->objectid = search_start;
914 ins->offset = 0;
Chris Masonfec577f2007-02-26 10:40:21 -0500915 start_found = 0;
Chris Mason2cc58cf2007-08-27 16:49:44 -0400916 path->reada = 2;
Chris Masone37c9e62007-05-09 20:13:14 -0400917
Chris Mason5caf2a02007-04-02 11:20:42 -0400918 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
Chris Mason0f70abe2007-02-28 16:46:22 -0500919 if (ret < 0)
920 goto error;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500921
Chris Masone37c9e62007-05-09 20:13:14 -0400922 if (path->slots[0] > 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400923 path->slots[0]--;
Chris Masone37c9e62007-05-09 20:13:14 -0400924 }
925
Chris Mason5f39d392007-10-15 16:14:19 -0400926 l = path->nodes[0];
927 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
928
Chris Masone37c9e62007-05-09 20:13:14 -0400929 /*
930 * a rare case, go back one key if we hit a block group item
931 * instead of an extent item
932 */
933 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY &&
934 key.objectid + key.offset >= search_start) {
935 ins->objectid = key.objectid;
936 ins->offset = key.offset - 1;
937 btrfs_release_path(root, path);
938 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
939 if (ret < 0)
940 goto error;
941
942 if (path->slots[0] > 0) {
943 path->slots[0]--;
944 }
945 }
Chris Mason0579da42007-03-07 16:15:30 -0500946
Chris Masonfec577f2007-02-26 10:40:21 -0500947 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400948 l = path->nodes[0];
Chris Mason5caf2a02007-04-02 11:20:42 -0400949 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -0400950 if (slot >= btrfs_header_nritems(l)) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400951 ret = btrfs_next_leaf(root, path);
Chris Masonfec577f2007-02-26 10:40:21 -0500952 if (ret == 0)
953 continue;
Chris Mason0f70abe2007-02-28 16:46:22 -0500954 if (ret < 0)
955 goto error;
Chris Masone19caa52007-10-15 16:17:44 -0400956
957 search_start = max(search_start,
958 block_group->key.objectid);
Chris Masonfec577f2007-02-26 10:40:21 -0500959 if (!start_found) {
960 ins->objectid = search_start;
Chris Mason3e1ad542007-05-07 20:03:49 -0400961 ins->offset = search_end - search_start;
Chris Masonfec577f2007-02-26 10:40:21 -0500962 start_found = 1;
963 goto check_pending;
964 }
Chris Masondb945352007-10-15 16:15:53 -0400965 ins->objectid = last_byte > search_start ?
966 last_byte : search_start;
Chris Mason3e1ad542007-05-07 20:03:49 -0400967 ins->offset = search_end - ins->objectid;
Chris Masone19caa52007-10-15 16:17:44 -0400968 BUG_ON(ins->objectid >= search_end);
Chris Masonfec577f2007-02-26 10:40:21 -0500969 goto check_pending;
970 }
Chris Mason5f39d392007-10-15 16:14:19 -0400971 btrfs_item_key_to_cpu(l, &key, slot);
Chris Mason96b51792007-10-15 16:15:19 -0400972
Chris Masondb945352007-10-15 16:15:53 -0400973 if (key.objectid >= search_start && key.objectid > last_byte &&
Chris Masone37c9e62007-05-09 20:13:14 -0400974 start_found) {
Chris Masondb945352007-10-15 16:15:53 -0400975 if (last_byte < search_start)
976 last_byte = search_start;
977 hole_size = key.objectid - last_byte;
978 if (hole_size >= num_bytes) {
979 ins->objectid = last_byte;
Chris Masone37c9e62007-05-09 20:13:14 -0400980 ins->offset = hole_size;
981 goto check_pending;
Chris Mason0579da42007-03-07 16:15:30 -0500982 }
Chris Masonfec577f2007-02-26 10:40:21 -0500983 }
Chris Mason96b51792007-10-15 16:15:19 -0400984 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY) {
985 if (!start_found) {
Chris Masondb945352007-10-15 16:15:53 -0400986 last_byte = key.objectid;
Chris Mason96b51792007-10-15 16:15:19 -0400987 start_found = 1;
988 }
Chris Masone37c9e62007-05-09 20:13:14 -0400989 goto next;
Chris Mason96b51792007-10-15 16:15:19 -0400990 }
991
Chris Masone37c9e62007-05-09 20:13:14 -0400992
Chris Mason0579da42007-03-07 16:15:30 -0500993 start_found = 1;
Chris Masondb945352007-10-15 16:15:53 -0400994 last_byte = key.objectid + key.offset;
Chris Masonf510cfe2007-10-15 16:14:48 -0400995
Chris Masondb945352007-10-15 16:15:53 -0400996 if (!full_scan && last_byte >= block_group->key.objectid +
Chris Masonbe744172007-05-06 10:15:01 -0400997 block_group->key.offset) {
998 btrfs_release_path(root, path);
999 search_start = block_group->key.objectid +
Chris Masone19caa52007-10-15 16:17:44 -04001000 block_group->key.offset;
Chris Masonbe744172007-05-06 10:15:01 -04001001 goto new_group;
1002 }
Chris Mason9078a3e2007-04-26 16:46:15 -04001003next:
Chris Mason5caf2a02007-04-02 11:20:42 -04001004 path->slots[0]++;
Chris Masonde428b62007-05-18 13:28:27 -04001005 cond_resched();
Chris Masonfec577f2007-02-26 10:40:21 -05001006 }
Chris Masonfec577f2007-02-26 10:40:21 -05001007check_pending:
1008 /* we have to make sure we didn't find an extent that has already
1009 * been allocated by the map tree or the original allocation
1010 */
Chris Mason5caf2a02007-04-02 11:20:42 -04001011 btrfs_release_path(root, path);
Chris Masonfec577f2007-02-26 10:40:21 -05001012 BUG_ON(ins->objectid < search_start);
Chris Masone37c9e62007-05-09 20:13:14 -04001013
Chris Masondb945352007-10-15 16:15:53 -04001014 if (ins->objectid + num_bytes >= search_end)
Chris Masoncf675822007-09-17 11:00:51 -04001015 goto enospc;
1016
Chris Masone19caa52007-10-15 16:17:44 -04001017 if (!full_scan && ins->objectid + num_bytes >= block_group->
1018 key.objectid + block_group->key.offset) {
1019 search_start = block_group->key.objectid +
1020 block_group->key.offset;
1021 goto new_group;
1022 }
Chris Mason1a5bc162007-10-15 16:15:26 -04001023 if (test_range_bit(&info->extent_ins, ins->objectid,
Chris Masondb945352007-10-15 16:15:53 -04001024 ins->objectid + num_bytes -1, EXTENT_LOCKED, 0)) {
1025 search_start = ins->objectid + num_bytes;
Chris Mason1a5bc162007-10-15 16:15:26 -04001026 goto new_group;
1027 }
1028 if (test_range_bit(&info->pinned_extents, ins->objectid,
Chris Masondb945352007-10-15 16:15:53 -04001029 ins->objectid + num_bytes -1, EXTENT_DIRTY, 0)) {
1030 search_start = ins->objectid + num_bytes;
Chris Mason1a5bc162007-10-15 16:15:26 -04001031 goto new_group;
Chris Masonfec577f2007-02-26 10:40:21 -05001032 }
Chris Masondb945352007-10-15 16:15:53 -04001033 if (exclude_nr > 0 && (ins->objectid + num_bytes > exclude_start &&
Chris Masonf2654de2007-06-26 12:20:46 -04001034 ins->objectid < exclude_start + exclude_nr)) {
1035 search_start = exclude_start + exclude_nr;
1036 goto new_group;
1037 }
Chris Masone37c9e62007-05-09 20:13:14 -04001038 if (!data) {
Chris Mason5276aed2007-06-11 21:33:38 -04001039 block_group = btrfs_lookup_block_group(info, ins->objectid);
Chris Mason26b80032007-08-08 20:17:12 -04001040 if (block_group)
1041 trans->block_group = block_group;
Chris Masoncd1bc462007-04-27 10:08:34 -04001042 }
Chris Masondb945352007-10-15 16:15:53 -04001043 ins->offset = num_bytes;
Chris Mason5caf2a02007-04-02 11:20:42 -04001044 btrfs_free_path(path);
Chris Masonfec577f2007-02-26 10:40:21 -05001045 return 0;
Chris Masonbe744172007-05-06 10:15:01 -04001046
1047new_group:
Chris Masondb945352007-10-15 16:15:53 -04001048 if (search_start + num_bytes >= search_end) {
Chris Masoncf675822007-09-17 11:00:51 -04001049enospc:
Chris Masonbe744172007-05-06 10:15:01 -04001050 search_start = orig_search_start;
Chris Masonfbdc7622007-05-30 10:22:12 -04001051 if (full_scan) {
1052 ret = -ENOSPC;
1053 goto error;
1054 }
Chris Mason6702ed42007-08-07 16:15:09 -04001055 if (wrapped) {
1056 if (!full_scan)
1057 total_needed -= empty_size;
Chris Masonfbdc7622007-05-30 10:22:12 -04001058 full_scan = 1;
Chris Mason6702ed42007-08-07 16:15:09 -04001059 } else
Chris Masonfbdc7622007-05-30 10:22:12 -04001060 wrapped = 1;
Chris Masonbe744172007-05-06 10:15:01 -04001061 }
Chris Mason5276aed2007-06-11 21:33:38 -04001062 block_group = btrfs_lookup_block_group(info, search_start);
Chris Masonfbdc7622007-05-30 10:22:12 -04001063 cond_resched();
Chris Masonbe744172007-05-06 10:15:01 -04001064 if (!full_scan)
1065 block_group = btrfs_find_block_group(root, block_group,
Chris Masonde428b62007-05-18 13:28:27 -04001066 search_start, data, 0);
Chris Masonbe744172007-05-06 10:15:01 -04001067 goto check_failed;
1068
Chris Mason0f70abe2007-02-28 16:46:22 -05001069error:
Chris Mason5caf2a02007-04-02 11:20:42 -04001070 btrfs_release_path(root, path);
1071 btrfs_free_path(path);
Chris Mason0f70abe2007-02-28 16:46:22 -05001072 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05001073}
Chris Masonfec577f2007-02-26 10:40:21 -05001074/*
Chris Masonfec577f2007-02-26 10:40:21 -05001075 * finds a free extent and does all the dirty work required for allocation
1076 * returns the key for the extent through ins, and a tree buffer for
1077 * the first block of the extent through buf.
1078 *
1079 * returns 0 if everything worked, non-zero otherwise.
1080 */
Chris Mason4d775672007-04-20 20:23:12 -04001081int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
1082 struct btrfs_root *root, u64 owner,
Chris Masondb945352007-10-15 16:15:53 -04001083 u64 num_bytes, u64 empty_size, u64 hint_byte,
Chris Masonbe08c1b2007-05-03 09:06:49 -04001084 u64 search_end, struct btrfs_key *ins, int data)
Chris Masonfec577f2007-02-26 10:40:21 -05001085{
1086 int ret;
1087 int pending_ret;
Chris Masondb945352007-10-15 16:15:53 -04001088 u64 super_used, root_used;
Chris Masonfbdc7622007-05-30 10:22:12 -04001089 u64 search_start = 0;
Chris Mason1261ec42007-03-20 20:35:03 -04001090 struct btrfs_fs_info *info = root->fs_info;
1091 struct btrfs_root *extent_root = info->extent_root;
Chris Mason234b63a2007-03-13 10:46:10 -04001092 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -05001093
Chris Mason5f39d392007-10-15 16:14:19 -04001094 btrfs_set_stack_extent_refs(&extent_item, 1);
1095 btrfs_set_stack_extent_owner(&extent_item, owner);
Chris Masonfec577f2007-02-26 10:40:21 -05001096
Chris Masondb945352007-10-15 16:15:53 -04001097 WARN_ON(num_bytes < root->sectorsize);
1098 ret = find_free_extent(trans, root, num_bytes, empty_size,
1099 search_start, search_end, hint_byte, ins,
Chris Mason26b80032007-08-08 20:17:12 -04001100 trans->alloc_exclude_start,
1101 trans->alloc_exclude_nr, data);
Chris Masonccd467d2007-06-28 15:57:36 -04001102 BUG_ON(ret);
Chris Masonf2654de2007-06-26 12:20:46 -04001103 if (ret)
1104 return ret;
1105
Josef Bacik58176a92007-08-29 15:47:34 -04001106 /* block accounting for super block */
Chris Masondb945352007-10-15 16:15:53 -04001107 super_used = btrfs_super_bytes_used(&info->super_copy);
1108 btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes);
Chris Mason26b80032007-08-08 20:17:12 -04001109
Josef Bacik58176a92007-08-29 15:47:34 -04001110 /* block accounting for root item */
Chris Masondb945352007-10-15 16:15:53 -04001111 root_used = btrfs_root_used(&root->root_item);
1112 btrfs_set_root_used(&root->root_item, root_used + num_bytes);
Josef Bacik58176a92007-08-29 15:47:34 -04001113
Chris Masonf510cfe2007-10-15 16:14:48 -04001114 clear_extent_dirty(&root->fs_info->free_space_cache,
1115 ins->objectid, ins->objectid + ins->offset - 1,
1116 GFP_NOFS);
1117
Chris Mason26b80032007-08-08 20:17:12 -04001118 if (root == extent_root) {
Chris Mason1a5bc162007-10-15 16:15:26 -04001119 set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
1120 ins->objectid + ins->offset - 1,
1121 EXTENT_LOCKED, GFP_NOFS);
Chris Masone19caa52007-10-15 16:17:44 -04001122 WARN_ON(data == 1);
Chris Mason26b80032007-08-08 20:17:12 -04001123 goto update_block;
1124 }
1125
1126 WARN_ON(trans->alloc_exclude_nr);
1127 trans->alloc_exclude_start = ins->objectid;
1128 trans->alloc_exclude_nr = ins->offset;
Chris Masone089f052007-03-16 16:20:31 -04001129 ret = btrfs_insert_item(trans, extent_root, ins, &extent_item,
1130 sizeof(extent_item));
Chris Mason037e6392007-03-07 11:50:24 -05001131
Chris Mason26b80032007-08-08 20:17:12 -04001132 trans->alloc_exclude_start = 0;
1133 trans->alloc_exclude_nr = 0;
1134
Chris Masonccd467d2007-06-28 15:57:36 -04001135 BUG_ON(ret);
Chris Masone089f052007-03-16 16:20:31 -04001136 finish_current_insert(trans, extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -04001137 pending_ret = del_pending_extents(trans, extent_root);
Chris Masonf510cfe2007-10-15 16:14:48 -04001138
Chris Masone37c9e62007-05-09 20:13:14 -04001139 if (ret) {
Chris Mason037e6392007-03-07 11:50:24 -05001140 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -04001141 }
1142 if (pending_ret) {
Chris Mason037e6392007-03-07 11:50:24 -05001143 return pending_ret;
Chris Masone37c9e62007-05-09 20:13:14 -04001144 }
Chris Mason26b80032007-08-08 20:17:12 -04001145
1146update_block:
Chris Mason1e2677e2007-05-29 16:52:18 -04001147 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0,
1148 data);
Chris Masonfabb5682007-06-07 22:13:21 -04001149 BUG_ON(ret);
Chris Mason037e6392007-03-07 11:50:24 -05001150 return 0;
Chris Masonfec577f2007-02-26 10:40:21 -05001151}
1152
1153/*
1154 * helper function to allocate a block for a given tree
1155 * returns the tree buffer or NULL.
1156 */
Chris Mason5f39d392007-10-15 16:14:19 -04001157struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Chris Masondb945352007-10-15 16:15:53 -04001158 struct btrfs_root *root,
1159 u32 blocksize, u64 hint,
Chris Mason5f39d392007-10-15 16:14:19 -04001160 u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05001161{
Chris Masone2fa7222007-03-12 16:22:34 -04001162 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -05001163 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04001164 struct extent_buffer *buf;
Chris Masonfec577f2007-02-26 10:40:21 -05001165
Chris Mason4d775672007-04-20 20:23:12 -04001166 ret = btrfs_alloc_extent(trans, root, root->root_key.objectid,
Chris Masondb945352007-10-15 16:15:53 -04001167 blocksize, empty_size, hint,
1168 (u64)-1, &ins, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05001169 if (ret) {
Chris Mason54aa1f42007-06-22 14:16:25 -04001170 BUG_ON(ret > 0);
1171 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05001172 }
Chris Masondb945352007-10-15 16:15:53 -04001173 buf = btrfs_find_create_tree_block(root, ins.objectid, blocksize);
Chris Mason54aa1f42007-06-22 14:16:25 -04001174 if (!buf) {
Chris Masondb945352007-10-15 16:15:53 -04001175 btrfs_free_extent(trans, root, ins.objectid, blocksize, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001176 return ERR_PTR(-ENOMEM);
1177 }
Chris Mason5f39d392007-10-15 16:14:19 -04001178 btrfs_set_buffer_uptodate(buf);
1179 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
1180 buf->start + buf->len - 1, GFP_NOFS);
Chris Mason6b800532007-10-15 16:17:34 -04001181 btrfs_set_buffer_defrag(buf);
Chris Masond3c2fdc2007-09-17 10:58:06 -04001182 trans->blocks_used++;
Chris Masonfec577f2007-02-26 10:40:21 -05001183 return buf;
1184}
Chris Masona28ec192007-03-06 20:08:01 -05001185
Chris Mason6407bf62007-03-27 06:33:00 -04001186static int drop_leaf_ref(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001187 struct btrfs_root *root, struct extent_buffer *leaf)
Chris Mason6407bf62007-03-27 06:33:00 -04001188{
Chris Mason5f39d392007-10-15 16:14:19 -04001189 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -04001190 struct btrfs_file_extent_item *fi;
1191 int i;
1192 int nritems;
1193 int ret;
1194
Chris Mason5f39d392007-10-15 16:14:19 -04001195 BUG_ON(!btrfs_is_leaf(leaf));
1196 nritems = btrfs_header_nritems(leaf);
Chris Mason6407bf62007-03-27 06:33:00 -04001197 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04001198 u64 disk_bytenr;
Chris Mason5f39d392007-10-15 16:14:19 -04001199
1200 btrfs_item_key_to_cpu(leaf, &key, i);
1201 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason6407bf62007-03-27 06:33:00 -04001202 continue;
1203 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001204 if (btrfs_file_extent_type(leaf, fi) ==
1205 BTRFS_FILE_EXTENT_INLINE)
Chris Mason236454d2007-04-19 13:37:44 -04001206 continue;
Chris Mason6407bf62007-03-27 06:33:00 -04001207 /*
1208 * FIXME make sure to insert a trans record that
1209 * repeats the snapshot del on crash
1210 */
Chris Masondb945352007-10-15 16:15:53 -04001211 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
1212 if (disk_bytenr == 0)
Chris Mason3a686372007-05-24 13:35:57 -04001213 continue;
Chris Masondb945352007-10-15 16:15:53 -04001214 ret = btrfs_free_extent(trans, root, disk_bytenr,
1215 btrfs_file_extent_disk_num_bytes(leaf, fi), 0);
Chris Mason6407bf62007-03-27 06:33:00 -04001216 BUG_ON(ret);
1217 }
1218 return 0;
1219}
1220
Chris Masone0115992007-06-19 16:23:05 -04001221static void reada_walk_down(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -04001222 struct extent_buffer *node)
Chris Masone0115992007-06-19 16:23:05 -04001223{
1224 int i;
1225 u32 nritems;
Chris Masondb945352007-10-15 16:15:53 -04001226 u64 bytenr;
Chris Masone0115992007-06-19 16:23:05 -04001227 int ret;
1228 u32 refs;
Chris Masondb945352007-10-15 16:15:53 -04001229 int level;
1230 u32 blocksize;
Chris Masone0115992007-06-19 16:23:05 -04001231
Chris Mason5f39d392007-10-15 16:14:19 -04001232 nritems = btrfs_header_nritems(node);
Chris Masondb945352007-10-15 16:15:53 -04001233 level = btrfs_header_level(node);
Chris Masone0115992007-06-19 16:23:05 -04001234 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04001235 bytenr = btrfs_node_blockptr(node, i);
1236 blocksize = btrfs_level_size(root, level - 1);
1237 ret = lookup_extent_ref(NULL, root, bytenr, blocksize, &refs);
Chris Masone0115992007-06-19 16:23:05 -04001238 BUG_ON(ret);
1239 if (refs != 1)
1240 continue;
Chris Mason409eb952007-08-08 20:17:12 -04001241 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masondb945352007-10-15 16:15:53 -04001242 ret = readahead_tree_block(root, bytenr, blocksize);
Chris Mason409eb952007-08-08 20:17:12 -04001243 cond_resched();
1244 mutex_lock(&root->fs_info->fs_mutex);
Chris Masone0115992007-06-19 16:23:05 -04001245 if (ret)
1246 break;
1247 }
1248}
1249
Chris Mason9aca1d52007-03-13 11:09:37 -04001250/*
1251 * helper function for drop_snapshot, this walks down the tree dropping ref
1252 * counts as it goes.
1253 */
Chris Masone089f052007-03-16 16:20:31 -04001254static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
1255 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05001256{
Chris Mason5f39d392007-10-15 16:14:19 -04001257 struct extent_buffer *next;
1258 struct extent_buffer *cur;
Chris Masondb945352007-10-15 16:15:53 -04001259 u64 bytenr;
1260 u32 blocksize;
Chris Mason20524f02007-03-10 06:35:47 -05001261 int ret;
1262 u32 refs;
1263
Chris Mason5caf2a02007-04-02 11:20:42 -04001264 WARN_ON(*level < 0);
1265 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason5f39d392007-10-15 16:14:19 -04001266 ret = lookup_extent_ref(trans, root,
Chris Masondb945352007-10-15 16:15:53 -04001267 path->nodes[*level]->start,
1268 path->nodes[*level]->len, &refs);
Chris Mason20524f02007-03-10 06:35:47 -05001269 BUG_ON(ret);
1270 if (refs > 1)
1271 goto out;
Chris Masone0115992007-06-19 16:23:05 -04001272
Chris Mason9aca1d52007-03-13 11:09:37 -04001273 /*
1274 * walk down to the last node level and free all the leaves
1275 */
Chris Mason6407bf62007-03-27 06:33:00 -04001276 while(*level >= 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001277 WARN_ON(*level < 0);
1278 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason20524f02007-03-10 06:35:47 -05001279 cur = path->nodes[*level];
Chris Masone0115992007-06-19 16:23:05 -04001280
1281 if (*level > 0 && path->slots[*level] == 0)
Chris Mason5f39d392007-10-15 16:14:19 -04001282 reada_walk_down(root, cur);
Chris Masone0115992007-06-19 16:23:05 -04001283
Chris Mason5f39d392007-10-15 16:14:19 -04001284 if (btrfs_header_level(cur) != *level)
Chris Mason2c90e5d2007-04-02 10:50:19 -04001285 WARN_ON(1);
Chris Masone0115992007-06-19 16:23:05 -04001286
Chris Mason7518a232007-03-12 12:01:18 -04001287 if (path->slots[*level] >=
Chris Mason5f39d392007-10-15 16:14:19 -04001288 btrfs_header_nritems(cur))
Chris Mason20524f02007-03-10 06:35:47 -05001289 break;
Chris Mason6407bf62007-03-27 06:33:00 -04001290 if (*level == 0) {
1291 ret = drop_leaf_ref(trans, root, cur);
1292 BUG_ON(ret);
1293 break;
1294 }
Chris Masondb945352007-10-15 16:15:53 -04001295 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
1296 blocksize = btrfs_level_size(root, *level - 1);
1297 ret = lookup_extent_ref(trans, root, bytenr, blocksize, &refs);
Chris Mason6407bf62007-03-27 06:33:00 -04001298 BUG_ON(ret);
1299 if (refs != 1) {
Chris Mason20524f02007-03-10 06:35:47 -05001300 path->slots[*level]++;
Chris Masondb945352007-10-15 16:15:53 -04001301 ret = btrfs_free_extent(trans, root, bytenr,
1302 blocksize, 1);
Chris Mason20524f02007-03-10 06:35:47 -05001303 BUG_ON(ret);
1304 continue;
1305 }
Chris Masondb945352007-10-15 16:15:53 -04001306 next = btrfs_find_tree_block(root, bytenr, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -04001307 if (!next || !btrfs_buffer_uptodate(next)) {
1308 free_extent_buffer(next);
Chris Masone9d0b132007-08-10 14:06:19 -04001309 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masondb945352007-10-15 16:15:53 -04001310 next = read_tree_block(root, bytenr, blocksize);
Chris Masone9d0b132007-08-10 14:06:19 -04001311 mutex_lock(&root->fs_info->fs_mutex);
1312
1313 /* we dropped the lock, check one more time */
Chris Masondb945352007-10-15 16:15:53 -04001314 ret = lookup_extent_ref(trans, root, bytenr,
1315 blocksize, &refs);
Chris Masone9d0b132007-08-10 14:06:19 -04001316 BUG_ON(ret);
1317 if (refs != 1) {
1318 path->slots[*level]++;
Chris Mason5f39d392007-10-15 16:14:19 -04001319 free_extent_buffer(next);
Chris Masone9d0b132007-08-10 14:06:19 -04001320 ret = btrfs_free_extent(trans, root,
Chris Masondb945352007-10-15 16:15:53 -04001321 bytenr, blocksize, 1);
Chris Masone9d0b132007-08-10 14:06:19 -04001322 BUG_ON(ret);
1323 continue;
1324 }
1325 }
Chris Mason5caf2a02007-04-02 11:20:42 -04001326 WARN_ON(*level <= 0);
Chris Mason83e15a22007-03-12 09:03:27 -04001327 if (path->nodes[*level-1])
Chris Mason5f39d392007-10-15 16:14:19 -04001328 free_extent_buffer(path->nodes[*level-1]);
Chris Mason20524f02007-03-10 06:35:47 -05001329 path->nodes[*level-1] = next;
Chris Mason5f39d392007-10-15 16:14:19 -04001330 *level = btrfs_header_level(next);
Chris Mason20524f02007-03-10 06:35:47 -05001331 path->slots[*level] = 0;
1332 }
1333out:
Chris Mason5caf2a02007-04-02 11:20:42 -04001334 WARN_ON(*level < 0);
1335 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Masondb945352007-10-15 16:15:53 -04001336 ret = btrfs_free_extent(trans, root, path->nodes[*level]->start,
1337 path->nodes[*level]->len, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001338 free_extent_buffer(path->nodes[*level]);
Chris Mason20524f02007-03-10 06:35:47 -05001339 path->nodes[*level] = NULL;
1340 *level += 1;
1341 BUG_ON(ret);
1342 return 0;
1343}
1344
Chris Mason9aca1d52007-03-13 11:09:37 -04001345/*
1346 * helper for dropping snapshots. This walks back up the tree in the path
1347 * to find the first node higher up where we haven't yet gone through
1348 * all the slots
1349 */
Chris Masone089f052007-03-16 16:20:31 -04001350static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root
1351 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05001352{
1353 int i;
1354 int slot;
1355 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04001356 struct btrfs_root_item *root_item = &root->root_item;
1357
Chris Mason234b63a2007-03-13 10:46:10 -04001358 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Mason20524f02007-03-10 06:35:47 -05001359 slot = path->slots[i];
Chris Mason5f39d392007-10-15 16:14:19 -04001360 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
1361 struct extent_buffer *node;
1362 struct btrfs_disk_key disk_key;
1363 node = path->nodes[i];
Chris Mason20524f02007-03-10 06:35:47 -05001364 path->slots[i]++;
1365 *level = i;
Chris Mason9f3a7422007-08-07 15:52:19 -04001366 WARN_ON(*level == 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001367 btrfs_node_key(node, &disk_key, path->slots[i]);
Chris Mason9f3a7422007-08-07 15:52:19 -04001368 memcpy(&root_item->drop_progress,
Chris Mason5f39d392007-10-15 16:14:19 -04001369 &disk_key, sizeof(disk_key));
Chris Mason9f3a7422007-08-07 15:52:19 -04001370 root_item->drop_level = i;
Chris Mason20524f02007-03-10 06:35:47 -05001371 return 0;
1372 } else {
Chris Masone089f052007-03-16 16:20:31 -04001373 ret = btrfs_free_extent(trans, root,
Chris Masondb945352007-10-15 16:15:53 -04001374 path->nodes[*level]->start,
1375 path->nodes[*level]->len, 1);
Chris Mason6407bf62007-03-27 06:33:00 -04001376 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04001377 free_extent_buffer(path->nodes[*level]);
Chris Mason83e15a22007-03-12 09:03:27 -04001378 path->nodes[*level] = NULL;
Chris Mason20524f02007-03-10 06:35:47 -05001379 *level = i + 1;
Chris Mason20524f02007-03-10 06:35:47 -05001380 }
1381 }
1382 return 1;
1383}
1384
Chris Mason9aca1d52007-03-13 11:09:37 -04001385/*
1386 * drop the reference count on the tree rooted at 'snap'. This traverses
1387 * the tree freeing any blocks that have a ref count of zero after being
1388 * decremented.
1389 */
Chris Masone089f052007-03-16 16:20:31 -04001390int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason9f3a7422007-08-07 15:52:19 -04001391 *root)
Chris Mason20524f02007-03-10 06:35:47 -05001392{
Chris Mason3768f362007-03-13 16:47:54 -04001393 int ret = 0;
Chris Mason9aca1d52007-03-13 11:09:37 -04001394 int wret;
Chris Mason20524f02007-03-10 06:35:47 -05001395 int level;
Chris Mason5caf2a02007-04-02 11:20:42 -04001396 struct btrfs_path *path;
Chris Mason20524f02007-03-10 06:35:47 -05001397 int i;
1398 int orig_level;
Chris Mason9f3a7422007-08-07 15:52:19 -04001399 struct btrfs_root_item *root_item = &root->root_item;
Chris Mason20524f02007-03-10 06:35:47 -05001400
Chris Mason5caf2a02007-04-02 11:20:42 -04001401 path = btrfs_alloc_path();
1402 BUG_ON(!path);
Chris Mason20524f02007-03-10 06:35:47 -05001403
Chris Mason5f39d392007-10-15 16:14:19 -04001404 level = btrfs_header_level(root->node);
Chris Mason20524f02007-03-10 06:35:47 -05001405 orig_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04001406 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
1407 path->nodes[level] = root->node;
Chris Masonf510cfe2007-10-15 16:14:48 -04001408 extent_buffer_get(root->node);
Chris Mason9f3a7422007-08-07 15:52:19 -04001409 path->slots[level] = 0;
1410 } else {
1411 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001412 struct btrfs_disk_key found_key;
1413 struct extent_buffer *node;
Chris Mason6702ed42007-08-07 16:15:09 -04001414
Chris Mason9f3a7422007-08-07 15:52:19 -04001415 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Chris Mason6702ed42007-08-07 16:15:09 -04001416 level = root_item->drop_level;
1417 path->lowest_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04001418 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason6702ed42007-08-07 16:15:09 -04001419 if (wret < 0) {
Chris Mason9f3a7422007-08-07 15:52:19 -04001420 ret = wret;
1421 goto out;
1422 }
Chris Mason5f39d392007-10-15 16:14:19 -04001423 node = path->nodes[level];
1424 btrfs_node_key(node, &found_key, path->slots[level]);
1425 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
1426 sizeof(found_key)));
Chris Mason9f3a7422007-08-07 15:52:19 -04001427 }
Chris Mason20524f02007-03-10 06:35:47 -05001428 while(1) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001429 wret = walk_down_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04001430 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05001431 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04001432 if (wret < 0)
1433 ret = wret;
1434
Chris Mason5caf2a02007-04-02 11:20:42 -04001435 wret = walk_up_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04001436 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05001437 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04001438 if (wret < 0)
1439 ret = wret;
Chris Mason409eb952007-08-08 20:17:12 -04001440 ret = -EAGAIN;
Chris Mason409eb952007-08-08 20:17:12 -04001441 break;
Chris Mason20524f02007-03-10 06:35:47 -05001442 }
Chris Mason83e15a22007-03-12 09:03:27 -04001443 for (i = 0; i <= orig_level; i++) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001444 if (path->nodes[i]) {
Chris Mason5f39d392007-10-15 16:14:19 -04001445 free_extent_buffer(path->nodes[i]);
Chris Mason0f827312007-10-15 16:18:56 -04001446 path->nodes[i] = NULL;
Chris Mason83e15a22007-03-12 09:03:27 -04001447 }
Chris Mason20524f02007-03-10 06:35:47 -05001448 }
Chris Mason9f3a7422007-08-07 15:52:19 -04001449out:
Chris Mason5caf2a02007-04-02 11:20:42 -04001450 btrfs_free_path(path);
Chris Mason9aca1d52007-03-13 11:09:37 -04001451 return ret;
Chris Mason20524f02007-03-10 06:35:47 -05001452}
Chris Mason9078a3e2007-04-26 16:46:15 -04001453
Chris Masonbe744172007-05-06 10:15:01 -04001454int btrfs_free_block_groups(struct btrfs_fs_info *info)
1455{
Chris Masonf510cfe2007-10-15 16:14:48 -04001456 u64 start;
1457 u64 end;
Chris Mason96b51792007-10-15 16:15:19 -04001458 int ret;
Chris Masonbe744172007-05-06 10:15:01 -04001459
Chris Mason96b51792007-10-15 16:15:19 -04001460 while(1) {
1461 ret = find_first_extent_bit(&info->block_group_cache, 0,
1462 &start, &end, (unsigned int)-1);
1463 if (ret)
1464 break;
1465 clear_extent_bits(&info->block_group_cache, start,
1466 end, (unsigned int)-1, GFP_NOFS);
1467 }
Chris Masone37c9e62007-05-09 20:13:14 -04001468 while(1) {
Chris Masonf510cfe2007-10-15 16:14:48 -04001469 ret = find_first_extent_bit(&info->free_space_cache, 0,
1470 &start, &end, EXTENT_DIRTY);
1471 if (ret)
Chris Masone37c9e62007-05-09 20:13:14 -04001472 break;
Chris Masonf510cfe2007-10-15 16:14:48 -04001473 clear_extent_dirty(&info->free_space_cache, start,
1474 end, GFP_NOFS);
Chris Masone37c9e62007-05-09 20:13:14 -04001475 }
Chris Masonbe744172007-05-06 10:15:01 -04001476 return 0;
1477}
1478
Chris Mason9078a3e2007-04-26 16:46:15 -04001479int btrfs_read_block_groups(struct btrfs_root *root)
1480{
1481 struct btrfs_path *path;
1482 int ret;
1483 int err = 0;
Chris Mason96b51792007-10-15 16:15:19 -04001484 int bit;
Chris Mason9078a3e2007-04-26 16:46:15 -04001485 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04001486 struct btrfs_fs_info *info = root->fs_info;
Chris Mason96b51792007-10-15 16:15:19 -04001487 struct extent_map_tree *block_group_cache;
Chris Mason9078a3e2007-04-26 16:46:15 -04001488 struct btrfs_key key;
1489 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -04001490 struct extent_buffer *leaf;
Chris Mason96b51792007-10-15 16:15:19 -04001491
1492 block_group_cache = &info->block_group_cache;
Chris Mason9078a3e2007-04-26 16:46:15 -04001493
Chris Masonbe744172007-05-06 10:15:01 -04001494 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04001495 key.objectid = 0;
Chris Masondb945352007-10-15 16:15:53 -04001496 key.offset = BTRFS_BLOCK_GROUP_SIZE;
Chris Mason9078a3e2007-04-26 16:46:15 -04001497 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
1498
1499 path = btrfs_alloc_path();
1500 if (!path)
1501 return -ENOMEM;
1502
1503 while(1) {
Chris Masonbe744172007-05-06 10:15:01 -04001504 ret = btrfs_search_slot(NULL, info->extent_root,
Chris Mason9078a3e2007-04-26 16:46:15 -04001505 &key, path, 0, 0);
1506 if (ret != 0) {
1507 err = ret;
1508 break;
1509 }
Chris Mason5f39d392007-10-15 16:14:19 -04001510 leaf = path->nodes[0];
1511 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason9078a3e2007-04-26 16:46:15 -04001512 cache = kmalloc(sizeof(*cache), GFP_NOFS);
1513 if (!cache) {
1514 err = -1;
1515 break;
1516 }
Chris Mason3e1ad542007-05-07 20:03:49 -04001517
Chris Mason5f39d392007-10-15 16:14:19 -04001518 read_extent_buffer(leaf, &cache->item,
1519 btrfs_item_ptr_offset(leaf, path->slots[0]),
1520 sizeof(cache->item));
Chris Mason9078a3e2007-04-26 16:46:15 -04001521 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Masone37c9e62007-05-09 20:13:14 -04001522 cache->cached = 0;
1523
Chris Mason9078a3e2007-04-26 16:46:15 -04001524 key.objectid = found_key.objectid + found_key.offset;
1525 btrfs_release_path(root, path);
Chris Mason5f39d392007-10-15 16:14:19 -04001526
Chris Mason96b51792007-10-15 16:15:19 -04001527 if (cache->item.flags & BTRFS_BLOCK_GROUP_DATA) {
1528 bit = BLOCK_GROUP_DATA;
1529 cache->data = 1;
1530 } else {
1531 bit = BLOCK_GROUP_METADATA;
1532 cache->data = 0;
Chris Mason31f3c992007-04-30 15:25:45 -04001533 }
Chris Mason96b51792007-10-15 16:15:19 -04001534
1535 /* use EXTENT_LOCKED to prevent merging */
1536 set_extent_bits(block_group_cache, found_key.objectid,
1537 found_key.objectid + found_key.offset - 1,
1538 bit | EXTENT_LOCKED, GFP_NOFS);
1539 set_state_private(block_group_cache, found_key.objectid,
1540 (u64)cache);
1541
Chris Mason9078a3e2007-04-26 16:46:15 -04001542 if (key.objectid >=
Chris Masondb945352007-10-15 16:15:53 -04001543 btrfs_super_total_bytes(&info->super_copy))
Chris Mason9078a3e2007-04-26 16:46:15 -04001544 break;
1545 }
1546
1547 btrfs_free_path(path);
1548 return 0;
1549}