blob: 477466d167a4cf2954c21135846e542507b1cc2a [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
Zach Brownec6b9102007-07-11 10:00:37 -040019#include <linux/sched.h>
Chris Masonfec577f2007-02-26 10:40:21 -050020#include "ctree.h"
21#include "disk-io.h"
22#include "print-tree.h"
Chris Masone089f052007-03-16 16:20:31 -040023#include "transaction.h"
Chris Masonfec577f2007-02-26 10:40:21 -050024
Chris Mason96b51792007-10-15 16:15:19 -040025#define BLOCK_GROUP_DATA EXTENT_WRITEBACK
26#define BLOCK_GROUP_METADATA EXTENT_UPTODATE
27#define BLOCK_GROUP_DIRTY EXTENT_DIRTY
28
Chris Masone089f052007-03-16 16:20:31 -040029static int finish_current_insert(struct btrfs_trans_handle *trans, struct
30 btrfs_root *extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -040031static int del_pending_extents(struct btrfs_trans_handle *trans, struct
32 btrfs_root *extent_root);
Chris Masonfec577f2007-02-26 10:40:21 -050033
Chris Masone37c9e62007-05-09 20:13:14 -040034static int cache_block_group(struct btrfs_root *root,
35 struct btrfs_block_group_cache *block_group)
36{
37 struct btrfs_path *path;
38 int ret;
39 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -040040 struct extent_buffer *leaf;
Chris Masonf510cfe2007-10-15 16:14:48 -040041 struct extent_map_tree *free_space_cache;
Chris Masone37c9e62007-05-09 20:13:14 -040042 int slot;
Chris Masone37c9e62007-05-09 20:13:14 -040043 u64 last = 0;
44 u64 hole_size;
Yan7d7d6062007-09-14 16:15:28 -040045 u64 first_free;
Chris Masone37c9e62007-05-09 20:13:14 -040046 int found = 0;
47
48 root = root->fs_info->extent_root;
Chris Masonf510cfe2007-10-15 16:14:48 -040049 free_space_cache = &root->fs_info->free_space_cache;
Chris Masone37c9e62007-05-09 20:13:14 -040050
51 if (block_group->cached)
52 return 0;
Chris Masonf510cfe2007-10-15 16:14:48 -040053
Chris Masone37c9e62007-05-09 20:13:14 -040054 path = btrfs_alloc_path();
55 if (!path)
56 return -ENOMEM;
Yan7d7d6062007-09-14 16:15:28 -040057
Chris Mason2cc58cf2007-08-27 16:49:44 -040058 path->reada = 2;
Yan7d7d6062007-09-14 16:15:28 -040059 first_free = block_group->key.objectid;
Chris Masone37c9e62007-05-09 20:13:14 -040060 key.objectid = block_group->key.objectid;
Chris Masone37c9e62007-05-09 20:13:14 -040061 key.offset = 0;
Yan7d7d6062007-09-14 16:15:28 -040062
Chris Masone37c9e62007-05-09 20:13:14 -040063 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
64 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Yan7d7d6062007-09-14 16:15:28 -040065
Chris Masone37c9e62007-05-09 20:13:14 -040066 if (ret < 0)
67 return ret;
Yan7d7d6062007-09-14 16:15:28 -040068
Chris Masone37c9e62007-05-09 20:13:14 -040069 if (ret && path->slots[0] > 0)
70 path->slots[0]--;
Yan7d7d6062007-09-14 16:15:28 -040071
Chris Masone37c9e62007-05-09 20:13:14 -040072 while(1) {
Chris Mason5f39d392007-10-15 16:14:19 -040073 leaf = path->nodes[0];
Chris Masone37c9e62007-05-09 20:13:14 -040074 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -040075 if (slot >= btrfs_header_nritems(leaf)) {
Chris Masone37c9e62007-05-09 20:13:14 -040076 ret = btrfs_next_leaf(root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -040077 if (ret < 0)
78 goto err;
Chris Masonde428b62007-05-18 13:28:27 -040079 if (ret == 0) {
Chris Masone37c9e62007-05-09 20:13:14 -040080 continue;
Chris Masonde428b62007-05-18 13:28:27 -040081 } else {
Chris Masone37c9e62007-05-09 20:13:14 -040082 break;
83 }
84 }
Yan7d7d6062007-09-14 16:15:28 -040085
Chris Mason5f39d392007-10-15 16:14:19 -040086 btrfs_item_key_to_cpu(leaf, &key, slot);
Yan7d7d6062007-09-14 16:15:28 -040087 if (key.objectid < block_group->key.objectid) {
88 if (key.objectid + key.offset > first_free)
89 first_free = key.objectid + key.offset;
90 goto next;
91 }
92
Chris Masone37c9e62007-05-09 20:13:14 -040093 if (key.objectid >= block_group->key.objectid +
94 block_group->key.offset) {
Yan7d7d6062007-09-14 16:15:28 -040095 break;
96 }
97
98 if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) {
99 if (!found) {
100 last = first_free;
101 found = 1;
Chris Masone37c9e62007-05-09 20:13:14 -0400102 }
Chris Masonf510cfe2007-10-15 16:14:48 -0400103 if (key.objectid > last) {
104 hole_size = key.objectid - last;
105 set_extent_dirty(free_space_cache, last,
106 last + hole_size - 1,
107 GFP_NOFS);
Chris Masone37c9e62007-05-09 20:13:14 -0400108 }
Yan7d7d6062007-09-14 16:15:28 -0400109 last = key.objectid + key.offset;
Chris Masone37c9e62007-05-09 20:13:14 -0400110 }
Yan7d7d6062007-09-14 16:15:28 -0400111next:
Chris Masone37c9e62007-05-09 20:13:14 -0400112 path->slots[0]++;
113 }
114
Yan7d7d6062007-09-14 16:15:28 -0400115 if (!found)
116 last = first_free;
117 if (block_group->key.objectid +
118 block_group->key.offset > last) {
119 hole_size = block_group->key.objectid +
120 block_group->key.offset - last;
Chris Masonf510cfe2007-10-15 16:14:48 -0400121 set_extent_dirty(free_space_cache, last,
122 last + hole_size - 1, GFP_NOFS);
Yan7d7d6062007-09-14 16:15:28 -0400123 }
Chris Masone37c9e62007-05-09 20:13:14 -0400124 block_group->cached = 1;
Chris Mason54aa1f42007-06-22 14:16:25 -0400125err:
Chris Masone37c9e62007-05-09 20:13:14 -0400126 btrfs_free_path(path);
127 return 0;
128}
129
Chris Mason5276aed2007-06-11 21:33:38 -0400130struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
131 btrfs_fs_info *info,
132 u64 blocknr)
Chris Masonbe744172007-05-06 10:15:01 -0400133{
Chris Mason96b51792007-10-15 16:15:19 -0400134 struct extent_map_tree *block_group_cache;
135 struct btrfs_block_group_cache *block_group = NULL;
136 u64 ptr;
137 u64 start;
138 u64 end;
Chris Masonbe744172007-05-06 10:15:01 -0400139 int ret;
140
Chris Mason96b51792007-10-15 16:15:19 -0400141 block_group_cache = &info->block_group_cache;
142 ret = find_first_extent_bit(block_group_cache,
143 blocknr, &start, &end,
144 BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA);
Chris Masonbe744172007-05-06 10:15:01 -0400145 if (ret) {
Chris Mason96b51792007-10-15 16:15:19 -0400146 return NULL;
Chris Masonbe744172007-05-06 10:15:01 -0400147 }
Chris Mason96b51792007-10-15 16:15:19 -0400148 ret = get_state_private(block_group_cache, start, &ptr);
149 if (ret)
150 return NULL;
151
152 block_group = (struct btrfs_block_group_cache *)ptr;
153
154
155 if (block_group->key.objectid <= blocknr && blocknr <=
156 block_group->key.objectid + block_group->key.offset)
157 return block_group;
158
Chris Masonbe744172007-05-06 10:15:01 -0400159 return NULL;
160}
161
Chris Masone37c9e62007-05-09 20:13:14 -0400162static u64 find_search_start(struct btrfs_root *root,
163 struct btrfs_block_group_cache **cache_ret,
Chris Masonf510cfe2007-10-15 16:14:48 -0400164 u64 search_start, int num, int data)
Chris Masone37c9e62007-05-09 20:13:14 -0400165{
Chris Masone37c9e62007-05-09 20:13:14 -0400166 int ret;
167 struct btrfs_block_group_cache *cache = *cache_ret;
168 u64 last = max(search_start, cache->key.objectid);
Chris Masonf510cfe2007-10-15 16:14:48 -0400169 u64 start = 0;
170 u64 end = 0;
Chris Masone37c9e62007-05-09 20:13:14 -0400171
Chris Masone37c9e62007-05-09 20:13:14 -0400172again:
Chris Mason54aa1f42007-06-22 14:16:25 -0400173 ret = cache_block_group(root, cache);
174 if (ret)
175 goto out;
Chris Masone37c9e62007-05-09 20:13:14 -0400176 while(1) {
Chris Masonf510cfe2007-10-15 16:14:48 -0400177 ret = find_first_extent_bit(&root->fs_info->free_space_cache,
178 last, &start, &end, EXTENT_DIRTY);
179 if (ret)
Chris Masone37c9e62007-05-09 20:13:14 -0400180 goto out;
Chris Masonf510cfe2007-10-15 16:14:48 -0400181
182 start = max(last, start);
183 last = end + 1;
184 if (end + 1 - start < num)
185 continue;
Chris Mason96b51792007-10-15 16:15:19 -0400186 if (start + num >= cache->key.objectid + cache->key.offset)
Chris Masone37c9e62007-05-09 20:13:14 -0400187 goto new_group;
Chris Masonf510cfe2007-10-15 16:14:48 -0400188 return start;
Chris Masone37c9e62007-05-09 20:13:14 -0400189 }
190out:
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,
318 u64 blocknr, u64 num_blocks)
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 Mason5caf2a02007-04-02 11:20:42 -0400327 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -0400328 if (!path)
329 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -0400330
Chris Mason02217ed2007-03-02 16:08:05 -0500331 key.objectid = blocknr;
Chris Mason62e27492007-03-15 12:56:47 -0400332 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Mason6407bf62007-03-27 06:33:00 -0400333 key.offset = num_blocks;
Chris Mason5caf2a02007-04-02 11:20:42 -0400334 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -0400335 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400336 if (ret < 0)
337 return ret;
Chris Masona429e512007-04-18 16:15:28 -0400338 if (ret != 0) {
Chris Masona28ec192007-03-06 20:08:01 -0500339 BUG();
Chris Masona429e512007-04-18 16:15:28 -0400340 }
Chris Mason02217ed2007-03-02 16:08:05 -0500341 BUG_ON(ret != 0);
Chris Mason5f39d392007-10-15 16:14:19 -0400342 l = path->nodes[0];
Chris Mason5caf2a02007-04-02 11:20:42 -0400343 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400344 refs = btrfs_extent_refs(l, item);
345 btrfs_set_extent_refs(l, item, refs + 1);
Chris Mason5caf2a02007-04-02 11:20:42 -0400346 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masona28ec192007-03-06 20:08:01 -0500347
Chris Mason5caf2a02007-04-02 11:20:42 -0400348 btrfs_release_path(root->fs_info->extent_root, path);
349 btrfs_free_path(path);
Chris Mason9f5fae22007-03-20 14:38:32 -0400350 finish_current_insert(trans, root->fs_info->extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -0400351 del_pending_extents(trans, root->fs_info->extent_root);
Chris Mason02217ed2007-03-02 16:08:05 -0500352 return 0;
353}
354
Chris Masone9d0b132007-08-10 14:06:19 -0400355int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
356 struct btrfs_root *root)
357{
358 finish_current_insert(trans, root->fs_info->extent_root);
359 del_pending_extents(trans, root->fs_info->extent_root);
360 return 0;
361}
362
Chris Masonb18c6682007-04-17 13:26:50 -0400363static int lookup_extent_ref(struct btrfs_trans_handle *trans,
364 struct btrfs_root *root, u64 blocknr,
365 u64 num_blocks, u32 *refs)
Chris Masona28ec192007-03-06 20:08:01 -0500366{
Chris Mason5caf2a02007-04-02 11:20:42 -0400367 struct btrfs_path *path;
Chris Masona28ec192007-03-06 20:08:01 -0500368 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400369 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400370 struct extent_buffer *l;
Chris Mason234b63a2007-03-13 10:46:10 -0400371 struct btrfs_extent_item *item;
Chris Mason5caf2a02007-04-02 11:20:42 -0400372
373 path = btrfs_alloc_path();
Chris Masona28ec192007-03-06 20:08:01 -0500374 key.objectid = blocknr;
Chris Mason6407bf62007-03-27 06:33:00 -0400375 key.offset = num_blocks;
Chris Mason62e27492007-03-15 12:56:47 -0400376 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Mason5caf2a02007-04-02 11:20:42 -0400377 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -0400378 0, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400379 if (ret < 0)
380 goto out;
Chris Mason5f39d392007-10-15 16:14:19 -0400381 if (ret != 0) {
382 btrfs_print_leaf(root, path->nodes[0]);
383 printk("failed to find block number %Lu\n", blocknr);
Chris Masona28ec192007-03-06 20:08:01 -0500384 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -0400385 }
386 l = path->nodes[0];
Chris Mason5caf2a02007-04-02 11:20:42 -0400387 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400388 *refs = btrfs_extent_refs(l, item);
Chris Mason54aa1f42007-06-22 14:16:25 -0400389out:
Chris Mason5caf2a02007-04-02 11:20:42 -0400390 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -0500391 return 0;
392}
393
Chris Masonc5739bb2007-04-10 09:27:04 -0400394int btrfs_inc_root_ref(struct btrfs_trans_handle *trans,
395 struct btrfs_root *root)
396{
Chris Mason5f39d392007-10-15 16:14:19 -0400397 return btrfs_inc_extent_ref(trans, root,
398 extent_buffer_blocknr(root->node), 1);
Chris Masonc5739bb2007-04-10 09:27:04 -0400399}
400
Chris Masone089f052007-03-16 16:20:31 -0400401int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400402 struct extent_buffer *buf)
Chris Mason02217ed2007-03-02 16:08:05 -0500403{
404 u64 blocknr;
Chris Mason5f39d392007-10-15 16:14:19 -0400405 u32 nritems;
406 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -0400407 struct btrfs_file_extent_item *fi;
Chris Mason02217ed2007-03-02 16:08:05 -0500408 int i;
Chris Mason6407bf62007-03-27 06:33:00 -0400409 int leaf;
410 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -0400411 int faili;
412 int err;
Chris Masona28ec192007-03-06 20:08:01 -0500413
Chris Mason3768f362007-03-13 16:47:54 -0400414 if (!root->ref_cows)
Chris Masona28ec192007-03-06 20:08:01 -0500415 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400416
417 leaf = btrfs_is_leaf(buf);
418 nritems = btrfs_header_nritems(buf);
419 for (i = 0; i < nritems; i++) {
Chris Mason6407bf62007-03-27 06:33:00 -0400420 if (leaf) {
Chris Mason3a686372007-05-24 13:35:57 -0400421 u64 disk_blocknr;
Chris Mason5f39d392007-10-15 16:14:19 -0400422 btrfs_item_key_to_cpu(buf, &key, i);
423 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason6407bf62007-03-27 06:33:00 -0400424 continue;
Chris Mason5f39d392007-10-15 16:14:19 -0400425 fi = btrfs_item_ptr(buf, i,
Chris Mason6407bf62007-03-27 06:33:00 -0400426 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400427 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason236454d2007-04-19 13:37:44 -0400428 BTRFS_FILE_EXTENT_INLINE)
429 continue;
Chris Mason5f39d392007-10-15 16:14:19 -0400430 disk_blocknr = btrfs_file_extent_disk_blocknr(buf, fi);
Chris Mason3a686372007-05-24 13:35:57 -0400431 if (disk_blocknr == 0)
432 continue;
433 ret = btrfs_inc_extent_ref(trans, root, disk_blocknr,
Chris Mason5f39d392007-10-15 16:14:19 -0400434 btrfs_file_extent_disk_num_blocks(buf, fi));
Chris Mason54aa1f42007-06-22 14:16:25 -0400435 if (ret) {
436 faili = i;
437 goto fail;
438 }
Chris Mason6407bf62007-03-27 06:33:00 -0400439 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400440 blocknr = btrfs_node_blockptr(buf, i);
Chris Masonb18c6682007-04-17 13:26:50 -0400441 ret = btrfs_inc_extent_ref(trans, root, blocknr, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400442 if (ret) {
443 faili = i;
444 goto fail;
445 }
Chris Mason6407bf62007-03-27 06:33:00 -0400446 }
Chris Mason02217ed2007-03-02 16:08:05 -0500447 }
448 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -0400449fail:
Chris Masonccd467d2007-06-28 15:57:36 -0400450 WARN_ON(1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400451 for (i =0; i < faili; i++) {
452 if (leaf) {
453 u64 disk_blocknr;
Chris Mason5f39d392007-10-15 16:14:19 -0400454 btrfs_item_key_to_cpu(buf, &key, i);
455 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -0400456 continue;
Chris Mason5f39d392007-10-15 16:14:19 -0400457 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -0400458 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400459 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -0400460 BTRFS_FILE_EXTENT_INLINE)
461 continue;
Chris Mason5f39d392007-10-15 16:14:19 -0400462 disk_blocknr = btrfs_file_extent_disk_blocknr(buf, fi);
Chris Mason54aa1f42007-06-22 14:16:25 -0400463 if (disk_blocknr == 0)
464 continue;
465 err = btrfs_free_extent(trans, root, disk_blocknr,
Chris Mason5f39d392007-10-15 16:14:19 -0400466 btrfs_file_extent_disk_num_blocks(buf,
467 fi), 0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400468 BUG_ON(err);
469 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400470 blocknr = btrfs_node_blockptr(buf, i);
Chris Mason54aa1f42007-06-22 14:16:25 -0400471 err = btrfs_free_extent(trans, root, blocknr, 1, 0);
472 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 Mason1e2677e2007-05-29 16:52:18 -0400561 u64 blocknr, u64 num, int alloc, int mark_free,
562 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;
566 u64 total = num;
567 u64 old_val;
568 u64 block_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 Mason5276aed2007-06-11 21:33:38 -0400573 cache = btrfs_lookup_block_group(info, blocknr);
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 Mason9078a3e2007-04-26 16:46:15 -0400577 block_in_group = blocknr - cache->key.objectid;
578 WARN_ON(block_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);
585 num = min(total, cache->key.offset - block_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 }
611 old_val += num;
Chris Masoncd1bc462007-04-27 10:08:34 -0400612 } else {
Chris Mason9078a3e2007-04-26 16:46:15 -0400613 old_val -= num;
Chris Masonf510cfe2007-10-15 16:14:48 -0400614 if (mark_free) {
615 set_extent_dirty(&info->free_space_cache,
616 blocknr, blocknr + num - 1,
617 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 Masone37c9e62007-05-09 20:13:14 -0400621 total -= num;
622 blocknr += num;
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 Mason8ef97622007-03-26 10:15:30 -0400704static int pin_down_block(struct btrfs_root *root, u64 blocknr, int pending)
Chris Masone20d96d2007-03-22 12:13:20 -0400705{
Chris Mason1a5bc162007-10-15 16:15:26 -0400706 int err = 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400707 struct extent_buffer *buf;
Chris Mason78fae272007-03-25 11:35:08 -0400708
Chris Masonf4b9aa82007-03-27 11:05:53 -0400709 if (!pending) {
Chris Mason5f39d392007-10-15 16:14:19 -0400710 buf = btrfs_find_tree_block(root, blocknr);
711 if (buf) {
712 if (btrfs_buffer_uptodate(buf)) {
Chris Mason2c90e5d2007-04-02 10:50:19 -0400713 u64 transid =
714 root->fs_info->running_transaction->transid;
Chris Mason5f39d392007-10-15 16:14:19 -0400715 if (btrfs_header_generation(buf) == transid) {
716 free_extent_buffer(buf);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400717 return 0;
718 }
Chris Masonf4b9aa82007-03-27 11:05:53 -0400719 }
Chris Mason5f39d392007-10-15 16:14:19 -0400720 free_extent_buffer(buf);
Chris Mason8ef97622007-03-26 10:15:30 -0400721 }
Chris Mason1a5bc162007-10-15 16:15:26 -0400722 set_extent_dirty(&root->fs_info->pinned_extents,
723 blocknr, blocknr, GFP_NOFS);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400724 } else {
Chris Mason1a5bc162007-10-15 16:15:26 -0400725 set_extent_bits(&root->fs_info->pending_del,
726 blocknr, blocknr, EXTENT_LOCKED, GFP_NOFS);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400727 }
Chris Masonbe744172007-05-06 10:15:01 -0400728 BUG_ON(err < 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400729 return 0;
730}
731
Chris Masona28ec192007-03-06 20:08:01 -0500732/*
733 * remove an extent from the root, returns 0 on success
734 */
Chris Masone089f052007-03-16 16:20:31 -0400735static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masone37c9e62007-05-09 20:13:14 -0400736 *root, u64 blocknr, u64 num_blocks, int pin,
737 int mark_free)
Chris Masona28ec192007-03-06 20:08:01 -0500738{
Chris Mason5caf2a02007-04-02 11:20:42 -0400739 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -0400740 struct btrfs_key key;
Chris Mason1261ec42007-03-20 20:35:03 -0400741 struct btrfs_fs_info *info = root->fs_info;
742 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -0400743 struct extent_buffer *leaf;
Chris Masona28ec192007-03-06 20:08:01 -0500744 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400745 struct btrfs_extent_item *ei;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400746 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500747
Chris Masona28ec192007-03-06 20:08:01 -0500748 key.objectid = blocknr;
Chris Mason62e27492007-03-15 12:56:47 -0400749 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masona28ec192007-03-06 20:08:01 -0500750 key.offset = num_blocks;
751
Chris Mason5caf2a02007-04-02 11:20:42 -0400752 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -0400753 if (!path)
754 return -ENOMEM;
755
Chris Mason5caf2a02007-04-02 11:20:42 -0400756 ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400757 if (ret < 0)
758 return ret;
759 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -0400760
761 leaf = path->nodes[0];
762 ei = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason123abc82007-03-14 14:14:43 -0400763 struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400764 refs = btrfs_extent_refs(leaf, ei);
765 BUG_ON(refs == 0);
766 refs -= 1;
767 btrfs_set_extent_refs(leaf, ei, refs);
768 btrfs_mark_buffer_dirty(leaf);
769
Chris Masoncf27e1e2007-03-13 09:49:06 -0400770 if (refs == 0) {
Josef Bacik58176a92007-08-29 15:47:34 -0400771 u64 super_blocks_used, root_blocks_used;
Chris Mason78fae272007-03-25 11:35:08 -0400772
773 if (pin) {
Chris Mason8ef97622007-03-26 10:15:30 -0400774 ret = pin_down_block(root, blocknr, 0);
Chris Mason78fae272007-03-25 11:35:08 -0400775 BUG_ON(ret);
776 }
777
Josef Bacik58176a92007-08-29 15:47:34 -0400778 /* block accounting for super block */
Chris Mason4b52dff2007-06-26 10:06:50 -0400779 super_blocks_used = btrfs_super_blocks_used(&info->super_copy);
780 btrfs_set_super_blocks_used(&info->super_copy,
Chris Mason1261ec42007-03-20 20:35:03 -0400781 super_blocks_used - num_blocks);
Josef Bacik58176a92007-08-29 15:47:34 -0400782
783 /* block accounting for root item */
Chris Mason5f39d392007-10-15 16:14:19 -0400784 root_blocks_used = btrfs_root_used(&root->root_item);
785 btrfs_set_root_used(&root->root_item,
Josef Bacik58176a92007-08-29 15:47:34 -0400786 root_blocks_used - num_blocks);
787
Chris Mason5caf2a02007-04-02 11:20:42 -0400788 ret = btrfs_del_item(trans, extent_root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -0400789 if (ret) {
790 return ret;
791 }
Chris Masone37c9e62007-05-09 20:13:14 -0400792 ret = update_block_group(trans, root, blocknr, num_blocks, 0,
Chris Mason1e2677e2007-05-29 16:52:18 -0400793 mark_free, 0);
Chris Mason9078a3e2007-04-26 16:46:15 -0400794 BUG_ON(ret);
Chris Masona28ec192007-03-06 20:08:01 -0500795 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400796 btrfs_free_path(path);
Chris Masone089f052007-03-16 16:20:31 -0400797 finish_current_insert(trans, extent_root);
Chris Masona28ec192007-03-06 20:08:01 -0500798 return ret;
799}
800
801/*
Chris Masonfec577f2007-02-26 10:40:21 -0500802 * find all the blocks marked as pending in the radix tree and remove
803 * them from the extent map
804 */
Chris Masone089f052007-03-16 16:20:31 -0400805static int del_pending_extents(struct btrfs_trans_handle *trans, struct
806 btrfs_root *extent_root)
Chris Masonfec577f2007-02-26 10:40:21 -0500807{
808 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -0400809 int err = 0;
Chris Mason1a5bc162007-10-15 16:15:26 -0400810 u64 start;
811 u64 end;
812 struct extent_map_tree *pending_del;
813 struct extent_map_tree *pinned_extents;
Chris Mason8ef97622007-03-26 10:15:30 -0400814
Chris Mason1a5bc162007-10-15 16:15:26 -0400815 pending_del = &extent_root->fs_info->pending_del;
816 pinned_extents = &extent_root->fs_info->pinned_extents;
Chris Masonfec577f2007-02-26 10:40:21 -0500817
818 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -0400819 ret = find_first_extent_bit(pending_del, 0, &start, &end,
820 EXTENT_LOCKED);
821 if (ret)
Chris Masonfec577f2007-02-26 10:40:21 -0500822 break;
Chris Mason1a5bc162007-10-15 16:15:26 -0400823
824 set_extent_dirty(pinned_extents, start, end, GFP_NOFS);
825 clear_extent_bits(pending_del, start, end, EXTENT_LOCKED,
826 GFP_NOFS);
827 ret = __free_extent(trans, extent_root,
828 start, end + 1 - start, 0, 0);
829 if (ret)
830 err = ret;
Chris Masonfec577f2007-02-26 10:40:21 -0500831 }
Chris Masone20d96d2007-03-22 12:13:20 -0400832 return err;
Chris Masonfec577f2007-02-26 10:40:21 -0500833}
834
835/*
836 * remove an extent from the root, returns 0 on success
837 */
Chris Masone089f052007-03-16 16:20:31 -0400838int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
839 *root, u64 blocknr, u64 num_blocks, int pin)
Chris Masonfec577f2007-02-26 10:40:21 -0500840{
Chris Mason9f5fae22007-03-20 14:38:32 -0400841 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Masonfec577f2007-02-26 10:40:21 -0500842 int pending_ret;
843 int ret;
Chris Masona28ec192007-03-06 20:08:01 -0500844
845 if (root == extent_root) {
Chris Mason8ef97622007-03-26 10:15:30 -0400846 pin_down_block(root, blocknr, 1);
Chris Masona28ec192007-03-06 20:08:01 -0500847 return 0;
848 }
Chris Masone37c9e62007-05-09 20:13:14 -0400849 ret = __free_extent(trans, root, blocknr, num_blocks, pin, pin == 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400850 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
Chris Masonfec577f2007-02-26 10:40:21 -0500851 return ret ? ret : pending_ret;
852}
853
854/*
855 * walks the btree of allocated extents and find a hole of a given size.
856 * The key ins is changed to record the hole:
857 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -0400858 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -0500859 * ins->offset == number of blocks
860 * Any available blocks before search_start are skipped.
861 */
Chris Masone089f052007-03-16 16:20:31 -0400862static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason6702ed42007-08-07 16:15:09 -0400863 *orig_root, u64 num_blocks, u64 empty_size,
864 u64 search_start, u64 search_end, u64 hint_block,
Chris Masonf2654de2007-06-26 12:20:46 -0400865 struct btrfs_key *ins, u64 exclude_start,
866 u64 exclude_nr, int data)
Chris Masonfec577f2007-02-26 10:40:21 -0500867{
Chris Mason5caf2a02007-04-02 11:20:42 -0400868 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -0400869 struct btrfs_key key;
Chris Masonfec577f2007-02-26 10:40:21 -0500870 int ret;
871 u64 hole_size = 0;
872 int slot = 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400873 u64 last_block = 0;
Chris Masonbe744172007-05-06 10:15:01 -0400874 u64 orig_search_start = search_start;
Chris Masonfec577f2007-02-26 10:40:21 -0500875 int start_found;
Chris Mason5f39d392007-10-15 16:14:19 -0400876 struct extent_buffer *l;
Chris Mason9f5fae22007-03-20 14:38:32 -0400877 struct btrfs_root * root = orig_root->fs_info->extent_root;
Chris Masonf2458e12007-04-25 15:52:25 -0400878 struct btrfs_fs_info *info = root->fs_info;
Chris Mason0579da42007-03-07 16:15:30 -0500879 int total_needed = num_blocks;
Chris Masone20d96d2007-03-22 12:13:20 -0400880 int level;
Chris Masonbe08c1b2007-05-03 09:06:49 -0400881 struct btrfs_block_group_cache *block_group;
Chris Masonbe744172007-05-06 10:15:01 -0400882 int full_scan = 0;
Chris Masonfbdc7622007-05-30 10:22:12 -0400883 int wrapped = 0;
Chris Masonf510cfe2007-10-15 16:14:48 -0400884 u64 cached_search_start = 0;
Chris Masonfec577f2007-02-26 10:40:21 -0500885
Chris Mason26b80032007-08-08 20:17:12 -0400886 WARN_ON(num_blocks < 1);
Chris Masonb1a4d962007-04-04 15:27:52 -0400887 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
888
Chris Mason5f39d392007-10-15 16:14:19 -0400889 level = btrfs_header_level(root->node);
890
Chris Mason3e1ad542007-05-07 20:03:49 -0400891 if (search_end == (u64)-1)
Chris Mason4b52dff2007-06-26 10:06:50 -0400892 search_end = btrfs_super_total_blocks(&info->super_copy);
Chris Masonfbdc7622007-05-30 10:22:12 -0400893 if (hint_block) {
Chris Mason5276aed2007-06-11 21:33:38 -0400894 block_group = btrfs_lookup_block_group(info, hint_block);
Chris Masonbe744172007-05-06 10:15:01 -0400895 block_group = btrfs_find_block_group(root, block_group,
Chris Masonfbdc7622007-05-30 10:22:12 -0400896 hint_block, data, 1);
Chris Masonbe744172007-05-06 10:15:01 -0400897 } else {
898 block_group = btrfs_find_block_group(root,
899 trans->block_group, 0,
Chris Masonde428b62007-05-18 13:28:27 -0400900 data, 1);
Chris Masonbe744172007-05-06 10:15:01 -0400901 }
902
Chris Mason6702ed42007-08-07 16:15:09 -0400903 total_needed += empty_size;
Chris Masone0115992007-06-19 16:23:05 -0400904 path = btrfs_alloc_path();
905
Chris Masonbe744172007-05-06 10:15:01 -0400906check_failed:
Chris Masonf510cfe2007-10-15 16:14:48 -0400907 search_start = find_search_start(root, &block_group,
908 search_start, total_needed, data);
909 cached_search_start = search_start;
Chris Masone37c9e62007-05-09 20:13:14 -0400910
Chris Mason5caf2a02007-04-02 11:20:42 -0400911 btrfs_init_path(path);
Chris Masonfec577f2007-02-26 10:40:21 -0500912 ins->objectid = search_start;
913 ins->offset = 0;
Chris Masonfec577f2007-02-26 10:40:21 -0500914 start_found = 0;
Chris Mason2cc58cf2007-08-27 16:49:44 -0400915 path->reada = 2;
Chris Masone37c9e62007-05-09 20:13:14 -0400916
Chris Mason5caf2a02007-04-02 11:20:42 -0400917 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
Chris Mason0f70abe2007-02-28 16:46:22 -0500918 if (ret < 0)
919 goto error;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500920
Chris Masone37c9e62007-05-09 20:13:14 -0400921 if (path->slots[0] > 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400922 path->slots[0]--;
Chris Masone37c9e62007-05-09 20:13:14 -0400923 }
924
Chris Mason5f39d392007-10-15 16:14:19 -0400925 l = path->nodes[0];
926 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
927
Chris Masone37c9e62007-05-09 20:13:14 -0400928 /*
929 * a rare case, go back one key if we hit a block group item
930 * instead of an extent item
931 */
932 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY &&
933 key.objectid + key.offset >= search_start) {
934 ins->objectid = key.objectid;
935 ins->offset = key.offset - 1;
936 btrfs_release_path(root, path);
937 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
938 if (ret < 0)
939 goto error;
940
941 if (path->slots[0] > 0) {
942 path->slots[0]--;
943 }
944 }
Chris Mason0579da42007-03-07 16:15:30 -0500945
Chris Masonfec577f2007-02-26 10:40:21 -0500946 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400947 l = path->nodes[0];
Chris Mason5caf2a02007-04-02 11:20:42 -0400948 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -0400949 if (slot >= btrfs_header_nritems(l)) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400950 ret = btrfs_next_leaf(root, path);
Chris Masonfec577f2007-02-26 10:40:21 -0500951 if (ret == 0)
952 continue;
Chris Mason0f70abe2007-02-28 16:46:22 -0500953 if (ret < 0)
954 goto error;
Chris Masonfec577f2007-02-26 10:40:21 -0500955 if (!start_found) {
956 ins->objectid = search_start;
Chris Mason3e1ad542007-05-07 20:03:49 -0400957 ins->offset = search_end - search_start;
Chris Masonfec577f2007-02-26 10:40:21 -0500958 start_found = 1;
959 goto check_pending;
960 }
961 ins->objectid = last_block > search_start ?
962 last_block : search_start;
Chris Mason3e1ad542007-05-07 20:03:49 -0400963 ins->offset = search_end - ins->objectid;
Chris Masonfec577f2007-02-26 10:40:21 -0500964 goto check_pending;
965 }
Chris Mason5f39d392007-10-15 16:14:19 -0400966 btrfs_item_key_to_cpu(l, &key, slot);
Chris Mason96b51792007-10-15 16:15:19 -0400967
Chris Masone37c9e62007-05-09 20:13:14 -0400968 if (key.objectid >= search_start && key.objectid > last_block &&
969 start_found) {
970 if (last_block < search_start)
971 last_block = search_start;
972 hole_size = key.objectid - last_block;
973 if (hole_size >= num_blocks) {
974 ins->objectid = last_block;
975 ins->offset = hole_size;
976 goto check_pending;
Chris Mason0579da42007-03-07 16:15:30 -0500977 }
Chris Masonfec577f2007-02-26 10:40:21 -0500978 }
Chris Mason96b51792007-10-15 16:15:19 -0400979 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY) {
980 if (!start_found) {
981 last_block = key.objectid;
982 start_found = 1;
983 }
Chris Masone37c9e62007-05-09 20:13:14 -0400984 goto next;
Chris Mason96b51792007-10-15 16:15:19 -0400985 }
986
Chris Masone37c9e62007-05-09 20:13:14 -0400987
Chris Mason0579da42007-03-07 16:15:30 -0500988 start_found = 1;
Chris Masone2fa7222007-03-12 16:22:34 -0400989 last_block = key.objectid + key.offset;
Chris Masonf510cfe2007-10-15 16:14:48 -0400990
Chris Masonfbdc7622007-05-30 10:22:12 -0400991 if (!full_scan && last_block >= block_group->key.objectid +
Chris Masonbe744172007-05-06 10:15:01 -0400992 block_group->key.offset) {
993 btrfs_release_path(root, path);
994 search_start = block_group->key.objectid +
995 block_group->key.offset * 2;
996 goto new_group;
997 }
Chris Mason9078a3e2007-04-26 16:46:15 -0400998next:
Chris Mason5caf2a02007-04-02 11:20:42 -0400999 path->slots[0]++;
Chris Masonde428b62007-05-18 13:28:27 -04001000 cond_resched();
Chris Masonfec577f2007-02-26 10:40:21 -05001001 }
Chris Masonfec577f2007-02-26 10:40:21 -05001002check_pending:
1003 /* we have to make sure we didn't find an extent that has already
1004 * been allocated by the map tree or the original allocation
1005 */
Chris Mason5caf2a02007-04-02 11:20:42 -04001006 btrfs_release_path(root, path);
Chris Masonfec577f2007-02-26 10:40:21 -05001007 BUG_ON(ins->objectid < search_start);
Chris Masone37c9e62007-05-09 20:13:14 -04001008
Chris Masoncf675822007-09-17 11:00:51 -04001009 if (ins->objectid + num_blocks >= search_end)
1010 goto enospc;
1011
Chris Mason1a5bc162007-10-15 16:15:26 -04001012 if (test_range_bit(&info->extent_ins, ins->objectid,
1013 ins->objectid + num_blocks -1, EXTENT_LOCKED, 0)) {
1014 search_start = ins->objectid + num_blocks;
1015 goto new_group;
1016 }
1017 if (test_range_bit(&info->pinned_extents, ins->objectid,
1018 ins->objectid + num_blocks -1, EXTENT_DIRTY, 0)) {
1019 search_start = ins->objectid + num_blocks;
1020 goto new_group;
Chris Masonfec577f2007-02-26 10:40:21 -05001021 }
Chris Masonf2654de2007-06-26 12:20:46 -04001022 if (exclude_nr > 0 && (ins->objectid + num_blocks > exclude_start &&
1023 ins->objectid < exclude_start + exclude_nr)) {
1024 search_start = exclude_start + exclude_nr;
1025 goto new_group;
1026 }
Chris Masone37c9e62007-05-09 20:13:14 -04001027 if (!data) {
Chris Mason5276aed2007-06-11 21:33:38 -04001028 block_group = btrfs_lookup_block_group(info, ins->objectid);
Chris Mason26b80032007-08-08 20:17:12 -04001029 if (block_group)
1030 trans->block_group = block_group;
Chris Masoncd1bc462007-04-27 10:08:34 -04001031 }
Chris Mason037e6392007-03-07 11:50:24 -05001032 ins->offset = num_blocks;
Chris Mason5caf2a02007-04-02 11:20:42 -04001033 btrfs_free_path(path);
Chris Masonfec577f2007-02-26 10:40:21 -05001034 return 0;
Chris Masonbe744172007-05-06 10:15:01 -04001035
1036new_group:
Chris Mason3e1ad542007-05-07 20:03:49 -04001037 if (search_start + num_blocks >= search_end) {
Chris Masoncf675822007-09-17 11:00:51 -04001038enospc:
Chris Masonbe744172007-05-06 10:15:01 -04001039 search_start = orig_search_start;
Chris Masonfbdc7622007-05-30 10:22:12 -04001040 if (full_scan) {
1041 ret = -ENOSPC;
1042 goto error;
1043 }
Chris Mason6702ed42007-08-07 16:15:09 -04001044 if (wrapped) {
1045 if (!full_scan)
1046 total_needed -= empty_size;
Chris Masonfbdc7622007-05-30 10:22:12 -04001047 full_scan = 1;
Chris Mason6702ed42007-08-07 16:15:09 -04001048 } else
Chris Masonfbdc7622007-05-30 10:22:12 -04001049 wrapped = 1;
Chris Masonbe744172007-05-06 10:15:01 -04001050 }
Chris Mason5276aed2007-06-11 21:33:38 -04001051 block_group = btrfs_lookup_block_group(info, search_start);
Chris Masonfbdc7622007-05-30 10:22:12 -04001052 cond_resched();
Chris Masonbe744172007-05-06 10:15:01 -04001053 if (!full_scan)
1054 block_group = btrfs_find_block_group(root, block_group,
Chris Masonde428b62007-05-18 13:28:27 -04001055 search_start, data, 0);
Chris Masonbe744172007-05-06 10:15:01 -04001056 goto check_failed;
1057
Chris Mason0f70abe2007-02-28 16:46:22 -05001058error:
Chris Mason5caf2a02007-04-02 11:20:42 -04001059 btrfs_release_path(root, path);
1060 btrfs_free_path(path);
Chris Mason0f70abe2007-02-28 16:46:22 -05001061 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05001062}
Chris Masonfec577f2007-02-26 10:40:21 -05001063/*
Chris Masonfec577f2007-02-26 10:40:21 -05001064 * finds a free extent and does all the dirty work required for allocation
1065 * returns the key for the extent through ins, and a tree buffer for
1066 * the first block of the extent through buf.
1067 *
1068 * returns 0 if everything worked, non-zero otherwise.
1069 */
Chris Mason4d775672007-04-20 20:23:12 -04001070int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
1071 struct btrfs_root *root, u64 owner,
Chris Mason6702ed42007-08-07 16:15:09 -04001072 u64 num_blocks, u64 empty_size, u64 hint_block,
Chris Masonbe08c1b2007-05-03 09:06:49 -04001073 u64 search_end, struct btrfs_key *ins, int data)
Chris Masonfec577f2007-02-26 10:40:21 -05001074{
1075 int ret;
1076 int pending_ret;
Josef Bacik58176a92007-08-29 15:47:34 -04001077 u64 super_blocks_used, root_blocks_used;
Chris Masonfbdc7622007-05-30 10:22:12 -04001078 u64 search_start = 0;
Chris Mason1261ec42007-03-20 20:35:03 -04001079 struct btrfs_fs_info *info = root->fs_info;
1080 struct btrfs_root *extent_root = info->extent_root;
Chris Mason234b63a2007-03-13 10:46:10 -04001081 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -05001082
Chris Mason5f39d392007-10-15 16:14:19 -04001083 btrfs_set_stack_extent_refs(&extent_item, 1);
1084 btrfs_set_stack_extent_owner(&extent_item, owner);
Chris Masonfec577f2007-02-26 10:40:21 -05001085
Chris Mason26b80032007-08-08 20:17:12 -04001086 WARN_ON(num_blocks < 1);
Chris Mason6702ed42007-08-07 16:15:09 -04001087 ret = find_free_extent(trans, root, num_blocks, empty_size,
1088 search_start, search_end, hint_block, ins,
Chris Mason26b80032007-08-08 20:17:12 -04001089 trans->alloc_exclude_start,
1090 trans->alloc_exclude_nr, data);
Chris Masonccd467d2007-06-28 15:57:36 -04001091 BUG_ON(ret);
Chris Masonf2654de2007-06-26 12:20:46 -04001092 if (ret)
1093 return ret;
1094
Josef Bacik58176a92007-08-29 15:47:34 -04001095 /* block accounting for super block */
Chris Mason4b52dff2007-06-26 10:06:50 -04001096 super_blocks_used = btrfs_super_blocks_used(&info->super_copy);
1097 btrfs_set_super_blocks_used(&info->super_copy, super_blocks_used +
Chris Mason1261ec42007-03-20 20:35:03 -04001098 num_blocks);
Chris Mason26b80032007-08-08 20:17:12 -04001099
Josef Bacik58176a92007-08-29 15:47:34 -04001100 /* block accounting for root item */
Chris Mason5f39d392007-10-15 16:14:19 -04001101 root_blocks_used = btrfs_root_used(&root->root_item);
1102 btrfs_set_root_used(&root->root_item, root_blocks_used +
Josef Bacik58176a92007-08-29 15:47:34 -04001103 num_blocks);
1104
Chris Masonf510cfe2007-10-15 16:14:48 -04001105 clear_extent_dirty(&root->fs_info->free_space_cache,
1106 ins->objectid, ins->objectid + ins->offset - 1,
1107 GFP_NOFS);
1108
Chris Mason26b80032007-08-08 20:17:12 -04001109 if (root == extent_root) {
1110 BUG_ON(num_blocks != 1);
Chris Mason1a5bc162007-10-15 16:15:26 -04001111 set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
1112 ins->objectid + ins->offset - 1,
1113 EXTENT_LOCKED, GFP_NOFS);
Chris Mason26b80032007-08-08 20:17:12 -04001114 goto update_block;
1115 }
1116
1117 WARN_ON(trans->alloc_exclude_nr);
1118 trans->alloc_exclude_start = ins->objectid;
1119 trans->alloc_exclude_nr = ins->offset;
Chris Masone089f052007-03-16 16:20:31 -04001120 ret = btrfs_insert_item(trans, extent_root, ins, &extent_item,
1121 sizeof(extent_item));
Chris Mason037e6392007-03-07 11:50:24 -05001122
Chris Mason26b80032007-08-08 20:17:12 -04001123 trans->alloc_exclude_start = 0;
1124 trans->alloc_exclude_nr = 0;
1125
Chris Masonccd467d2007-06-28 15:57:36 -04001126 BUG_ON(ret);
Chris Masone089f052007-03-16 16:20:31 -04001127 finish_current_insert(trans, extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -04001128 pending_ret = del_pending_extents(trans, extent_root);
Chris Masonf510cfe2007-10-15 16:14:48 -04001129
Chris Masone37c9e62007-05-09 20:13:14 -04001130 if (ret) {
Chris Mason037e6392007-03-07 11:50:24 -05001131 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -04001132 }
1133 if (pending_ret) {
Chris Mason037e6392007-03-07 11:50:24 -05001134 return pending_ret;
Chris Masone37c9e62007-05-09 20:13:14 -04001135 }
Chris Mason26b80032007-08-08 20:17:12 -04001136
1137update_block:
Chris Mason1e2677e2007-05-29 16:52:18 -04001138 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0,
1139 data);
Chris Masonfabb5682007-06-07 22:13:21 -04001140 BUG_ON(ret);
Chris Mason037e6392007-03-07 11:50:24 -05001141 return 0;
Chris Masonfec577f2007-02-26 10:40:21 -05001142}
1143
1144/*
1145 * helper function to allocate a block for a given tree
1146 * returns the tree buffer or NULL.
1147 */
Chris Mason5f39d392007-10-15 16:14:19 -04001148struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
1149 struct btrfs_root *root, u64 hint,
1150 u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05001151{
Chris Masone2fa7222007-03-12 16:22:34 -04001152 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -05001153 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04001154 struct extent_buffer *buf;
Chris Masonfec577f2007-02-26 10:40:21 -05001155
Chris Mason4d775672007-04-20 20:23:12 -04001156 ret = btrfs_alloc_extent(trans, root, root->root_key.objectid,
Yane9fe3952007-08-29 09:11:44 -04001157 1, empty_size, hint, (u64)-1, &ins, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05001158 if (ret) {
Chris Mason54aa1f42007-06-22 14:16:25 -04001159 BUG_ON(ret > 0);
1160 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05001161 }
Chris Masond98237b2007-03-28 13:57:48 -04001162 buf = btrfs_find_create_tree_block(root, ins.objectid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001163 if (!buf) {
1164 btrfs_free_extent(trans, root, ins.objectid, 1, 0);
1165 return ERR_PTR(-ENOMEM);
1166 }
Chris Mason5f39d392007-10-15 16:14:19 -04001167 btrfs_set_buffer_uptodate(buf);
Chris Masonf510cfe2007-10-15 16:14:48 -04001168 buf->alloc_addr = (unsigned long)__builtin_return_address(0);
Chris Mason5f39d392007-10-15 16:14:19 -04001169 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
1170 buf->start + buf->len - 1, GFP_NOFS);
1171 /*
Chris Mason090d1872007-05-01 08:53:32 -04001172 set_buffer_checked(buf);
Chris Masonf2183bd2007-08-10 14:42:37 -04001173 set_buffer_defrag(buf);
Chris Mason5f39d392007-10-15 16:14:19 -04001174 */
1175 /* FIXME!!!!!!!!!!!!!!!!
1176 set_radix_bit(&trans->transaction->dirty_pages, buf->pages[0]->index);
1177 */
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001178 trans->blocks_used++;
Chris Masonfec577f2007-02-26 10:40:21 -05001179 return buf;
1180}
Chris Masona28ec192007-03-06 20:08:01 -05001181
Chris Mason6407bf62007-03-27 06:33:00 -04001182static int drop_leaf_ref(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001183 struct btrfs_root *root, struct extent_buffer *leaf)
Chris Mason6407bf62007-03-27 06:33:00 -04001184{
Chris Mason5f39d392007-10-15 16:14:19 -04001185 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -04001186 struct btrfs_file_extent_item *fi;
1187 int i;
1188 int nritems;
1189 int ret;
1190
Chris Mason5f39d392007-10-15 16:14:19 -04001191 BUG_ON(!btrfs_is_leaf(leaf));
1192 nritems = btrfs_header_nritems(leaf);
Chris Mason6407bf62007-03-27 06:33:00 -04001193 for (i = 0; i < nritems; i++) {
Chris Mason3a686372007-05-24 13:35:57 -04001194 u64 disk_blocknr;
Chris Mason5f39d392007-10-15 16:14:19 -04001195
1196 btrfs_item_key_to_cpu(leaf, &key, i);
1197 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason6407bf62007-03-27 06:33:00 -04001198 continue;
1199 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001200 if (btrfs_file_extent_type(leaf, fi) ==
1201 BTRFS_FILE_EXTENT_INLINE)
Chris Mason236454d2007-04-19 13:37:44 -04001202 continue;
Chris Mason6407bf62007-03-27 06:33:00 -04001203 /*
1204 * FIXME make sure to insert a trans record that
1205 * repeats the snapshot del on crash
1206 */
Chris Mason5f39d392007-10-15 16:14:19 -04001207 disk_blocknr = btrfs_file_extent_disk_blocknr(leaf, fi);
Chris Mason3a686372007-05-24 13:35:57 -04001208 if (disk_blocknr == 0)
1209 continue;
1210 ret = btrfs_free_extent(trans, root, disk_blocknr,
Chris Mason5f39d392007-10-15 16:14:19 -04001211 btrfs_file_extent_disk_num_blocks(leaf, fi), 0);
Chris Mason6407bf62007-03-27 06:33:00 -04001212 BUG_ON(ret);
1213 }
1214 return 0;
1215}
1216
Chris Masone0115992007-06-19 16:23:05 -04001217static void reada_walk_down(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -04001218 struct extent_buffer *node)
Chris Masone0115992007-06-19 16:23:05 -04001219{
1220 int i;
1221 u32 nritems;
1222 u64 blocknr;
1223 int ret;
1224 u32 refs;
1225
Chris Mason5f39d392007-10-15 16:14:19 -04001226 nritems = btrfs_header_nritems(node);
Chris Masone0115992007-06-19 16:23:05 -04001227 for (i = 0; i < nritems; i++) {
1228 blocknr = btrfs_node_blockptr(node, i);
1229 ret = lookup_extent_ref(NULL, root, blocknr, 1, &refs);
1230 BUG_ON(ret);
1231 if (refs != 1)
1232 continue;
Chris Mason409eb952007-08-08 20:17:12 -04001233 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masone0115992007-06-19 16:23:05 -04001234 ret = readahead_tree_block(root, blocknr);
Chris Mason409eb952007-08-08 20:17:12 -04001235 cond_resched();
1236 mutex_lock(&root->fs_info->fs_mutex);
Chris Masone0115992007-06-19 16:23:05 -04001237 if (ret)
1238 break;
1239 }
1240}
1241
Chris Mason9aca1d52007-03-13 11:09:37 -04001242/*
1243 * helper function for drop_snapshot, this walks down the tree dropping ref
1244 * counts as it goes.
1245 */
Chris Masone089f052007-03-16 16:20:31 -04001246static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
1247 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05001248{
Chris Mason5f39d392007-10-15 16:14:19 -04001249 struct extent_buffer *next;
1250 struct extent_buffer *cur;
Chris Mason20524f02007-03-10 06:35:47 -05001251 u64 blocknr;
1252 int ret;
1253 u32 refs;
1254
Chris Mason5caf2a02007-04-02 11:20:42 -04001255 WARN_ON(*level < 0);
1256 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason5f39d392007-10-15 16:14:19 -04001257 ret = lookup_extent_ref(trans, root,
1258 extent_buffer_blocknr(path->nodes[*level]),
1259 1, &refs);
Chris Mason20524f02007-03-10 06:35:47 -05001260 BUG_ON(ret);
1261 if (refs > 1)
1262 goto out;
Chris Masone0115992007-06-19 16:23:05 -04001263
Chris Mason9aca1d52007-03-13 11:09:37 -04001264 /*
1265 * walk down to the last node level and free all the leaves
1266 */
Chris Mason6407bf62007-03-27 06:33:00 -04001267 while(*level >= 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001268 WARN_ON(*level < 0);
1269 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason20524f02007-03-10 06:35:47 -05001270 cur = path->nodes[*level];
Chris Masone0115992007-06-19 16:23:05 -04001271
1272 if (*level > 0 && path->slots[*level] == 0)
Chris Mason5f39d392007-10-15 16:14:19 -04001273 reada_walk_down(root, cur);
Chris Masone0115992007-06-19 16:23:05 -04001274
Chris Mason5f39d392007-10-15 16:14:19 -04001275 if (btrfs_header_level(cur) != *level)
Chris Mason2c90e5d2007-04-02 10:50:19 -04001276 WARN_ON(1);
Chris Masone0115992007-06-19 16:23:05 -04001277
Chris Mason7518a232007-03-12 12:01:18 -04001278 if (path->slots[*level] >=
Chris Mason5f39d392007-10-15 16:14:19 -04001279 btrfs_header_nritems(cur))
Chris Mason20524f02007-03-10 06:35:47 -05001280 break;
Chris Mason6407bf62007-03-27 06:33:00 -04001281 if (*level == 0) {
1282 ret = drop_leaf_ref(trans, root, cur);
1283 BUG_ON(ret);
1284 break;
1285 }
Chris Mason5f39d392007-10-15 16:14:19 -04001286 blocknr = btrfs_node_blockptr(cur, path->slots[*level]);
Chris Masonb18c6682007-04-17 13:26:50 -04001287 ret = lookup_extent_ref(trans, root, blocknr, 1, &refs);
Chris Mason6407bf62007-03-27 06:33:00 -04001288 BUG_ON(ret);
1289 if (refs != 1) {
Chris Mason20524f02007-03-10 06:35:47 -05001290 path->slots[*level]++;
Chris Masone089f052007-03-16 16:20:31 -04001291 ret = btrfs_free_extent(trans, root, blocknr, 1, 1);
Chris Mason20524f02007-03-10 06:35:47 -05001292 BUG_ON(ret);
1293 continue;
1294 }
Chris Masone9d0b132007-08-10 14:06:19 -04001295 next = btrfs_find_tree_block(root, blocknr);
Chris Mason5f39d392007-10-15 16:14:19 -04001296 if (!next || !btrfs_buffer_uptodate(next)) {
1297 free_extent_buffer(next);
Chris Masone9d0b132007-08-10 14:06:19 -04001298 mutex_unlock(&root->fs_info->fs_mutex);
1299 next = read_tree_block(root, blocknr);
1300 mutex_lock(&root->fs_info->fs_mutex);
1301
1302 /* we dropped the lock, check one more time */
1303 ret = lookup_extent_ref(trans, root, blocknr, 1, &refs);
1304 BUG_ON(ret);
1305 if (refs != 1) {
1306 path->slots[*level]++;
Chris Mason5f39d392007-10-15 16:14:19 -04001307 free_extent_buffer(next);
Chris Masone9d0b132007-08-10 14:06:19 -04001308 ret = btrfs_free_extent(trans, root,
1309 blocknr, 1, 1);
1310 BUG_ON(ret);
1311 continue;
1312 }
1313 }
Chris Mason5caf2a02007-04-02 11:20:42 -04001314 WARN_ON(*level <= 0);
Chris Mason83e15a22007-03-12 09:03:27 -04001315 if (path->nodes[*level-1])
Chris Mason5f39d392007-10-15 16:14:19 -04001316 free_extent_buffer(path->nodes[*level-1]);
Chris Mason20524f02007-03-10 06:35:47 -05001317 path->nodes[*level-1] = next;
Chris Mason5f39d392007-10-15 16:14:19 -04001318 *level = btrfs_header_level(next);
Chris Mason20524f02007-03-10 06:35:47 -05001319 path->slots[*level] = 0;
1320 }
1321out:
Chris Mason5caf2a02007-04-02 11:20:42 -04001322 WARN_ON(*level < 0);
1323 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason6407bf62007-03-27 06:33:00 -04001324 ret = btrfs_free_extent(trans, root,
Chris Mason5f39d392007-10-15 16:14:19 -04001325 extent_buffer_blocknr(path->nodes[*level]), 1, 1);
1326 free_extent_buffer(path->nodes[*level]);
Chris Mason20524f02007-03-10 06:35:47 -05001327 path->nodes[*level] = NULL;
1328 *level += 1;
1329 BUG_ON(ret);
1330 return 0;
1331}
1332
Chris Mason9aca1d52007-03-13 11:09:37 -04001333/*
1334 * helper for dropping snapshots. This walks back up the tree in the path
1335 * to find the first node higher up where we haven't yet gone through
1336 * all the slots
1337 */
Chris Masone089f052007-03-16 16:20:31 -04001338static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root
1339 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05001340{
1341 int i;
1342 int slot;
1343 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04001344 struct btrfs_root_item *root_item = &root->root_item;
1345
Chris Mason234b63a2007-03-13 10:46:10 -04001346 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Mason20524f02007-03-10 06:35:47 -05001347 slot = path->slots[i];
Chris Mason5f39d392007-10-15 16:14:19 -04001348 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
1349 struct extent_buffer *node;
1350 struct btrfs_disk_key disk_key;
1351 node = path->nodes[i];
Chris Mason20524f02007-03-10 06:35:47 -05001352 path->slots[i]++;
1353 *level = i;
Chris Mason9f3a7422007-08-07 15:52:19 -04001354 WARN_ON(*level == 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001355 btrfs_node_key(node, &disk_key, path->slots[i]);
Chris Mason9f3a7422007-08-07 15:52:19 -04001356 memcpy(&root_item->drop_progress,
Chris Mason5f39d392007-10-15 16:14:19 -04001357 &disk_key, sizeof(disk_key));
Chris Mason9f3a7422007-08-07 15:52:19 -04001358 root_item->drop_level = i;
Chris Mason20524f02007-03-10 06:35:47 -05001359 return 0;
1360 } else {
Chris Masone089f052007-03-16 16:20:31 -04001361 ret = btrfs_free_extent(trans, root,
Chris Mason5f39d392007-10-15 16:14:19 -04001362 extent_buffer_blocknr(path->nodes[*level]),
1363 1, 1);
Chris Mason6407bf62007-03-27 06:33:00 -04001364 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04001365 free_extent_buffer(path->nodes[*level]);
Chris Mason83e15a22007-03-12 09:03:27 -04001366 path->nodes[*level] = NULL;
Chris Mason20524f02007-03-10 06:35:47 -05001367 *level = i + 1;
Chris Mason20524f02007-03-10 06:35:47 -05001368 }
1369 }
1370 return 1;
1371}
1372
Chris Mason9aca1d52007-03-13 11:09:37 -04001373/*
1374 * drop the reference count on the tree rooted at 'snap'. This traverses
1375 * the tree freeing any blocks that have a ref count of zero after being
1376 * decremented.
1377 */
Chris Masone089f052007-03-16 16:20:31 -04001378int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason9f3a7422007-08-07 15:52:19 -04001379 *root)
Chris Mason20524f02007-03-10 06:35:47 -05001380{
Chris Mason3768f362007-03-13 16:47:54 -04001381 int ret = 0;
Chris Mason9aca1d52007-03-13 11:09:37 -04001382 int wret;
Chris Mason20524f02007-03-10 06:35:47 -05001383 int level;
Chris Mason5caf2a02007-04-02 11:20:42 -04001384 struct btrfs_path *path;
Chris Mason20524f02007-03-10 06:35:47 -05001385 int i;
1386 int orig_level;
Chris Mason9f3a7422007-08-07 15:52:19 -04001387 struct btrfs_root_item *root_item = &root->root_item;
Chris Mason20524f02007-03-10 06:35:47 -05001388
Chris Mason5caf2a02007-04-02 11:20:42 -04001389 path = btrfs_alloc_path();
1390 BUG_ON(!path);
Chris Mason20524f02007-03-10 06:35:47 -05001391
Chris Mason5f39d392007-10-15 16:14:19 -04001392 level = btrfs_header_level(root->node);
Chris Mason20524f02007-03-10 06:35:47 -05001393 orig_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04001394 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
1395 path->nodes[level] = root->node;
Chris Masonf510cfe2007-10-15 16:14:48 -04001396 extent_buffer_get(root->node);
Chris Mason9f3a7422007-08-07 15:52:19 -04001397 path->slots[level] = 0;
1398 } else {
1399 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001400 struct btrfs_disk_key found_key;
1401 struct extent_buffer *node;
Chris Mason6702ed42007-08-07 16:15:09 -04001402
Chris Mason9f3a7422007-08-07 15:52:19 -04001403 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Chris Mason6702ed42007-08-07 16:15:09 -04001404 level = root_item->drop_level;
1405 path->lowest_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04001406 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason6702ed42007-08-07 16:15:09 -04001407 if (wret < 0) {
Chris Mason9f3a7422007-08-07 15:52:19 -04001408 ret = wret;
1409 goto out;
1410 }
Chris Mason5f39d392007-10-15 16:14:19 -04001411 node = path->nodes[level];
1412 btrfs_node_key(node, &found_key, path->slots[level]);
1413 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
1414 sizeof(found_key)));
Chris Mason9f3a7422007-08-07 15:52:19 -04001415 }
Chris Mason20524f02007-03-10 06:35:47 -05001416 while(1) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001417 wret = walk_down_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04001418 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05001419 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04001420 if (wret < 0)
1421 ret = wret;
1422
Chris Mason5caf2a02007-04-02 11:20:42 -04001423 wret = walk_up_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04001424 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05001425 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04001426 if (wret < 0)
1427 ret = wret;
Chris Mason409eb952007-08-08 20:17:12 -04001428 ret = -EAGAIN;
Chris Mason409eb952007-08-08 20:17:12 -04001429 break;
Chris Mason20524f02007-03-10 06:35:47 -05001430 }
Chris Mason83e15a22007-03-12 09:03:27 -04001431 for (i = 0; i <= orig_level; i++) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001432 if (path->nodes[i]) {
Chris Mason5f39d392007-10-15 16:14:19 -04001433 free_extent_buffer(path->nodes[i]);
Chris Mason6702ed42007-08-07 16:15:09 -04001434 path->nodes[i] = 0;
Chris Mason83e15a22007-03-12 09:03:27 -04001435 }
Chris Mason20524f02007-03-10 06:35:47 -05001436 }
Chris Mason9f3a7422007-08-07 15:52:19 -04001437out:
Chris Mason5caf2a02007-04-02 11:20:42 -04001438 btrfs_free_path(path);
Chris Mason9aca1d52007-03-13 11:09:37 -04001439 return ret;
Chris Mason20524f02007-03-10 06:35:47 -05001440}
Chris Mason9078a3e2007-04-26 16:46:15 -04001441
Chris Masonbe744172007-05-06 10:15:01 -04001442int btrfs_free_block_groups(struct btrfs_fs_info *info)
1443{
Chris Masonf510cfe2007-10-15 16:14:48 -04001444 u64 start;
1445 u64 end;
Chris Mason96b51792007-10-15 16:15:19 -04001446 int ret;
Chris Masonbe744172007-05-06 10:15:01 -04001447
Chris Mason96b51792007-10-15 16:15:19 -04001448 while(1) {
1449 ret = find_first_extent_bit(&info->block_group_cache, 0,
1450 &start, &end, (unsigned int)-1);
1451 if (ret)
1452 break;
1453 clear_extent_bits(&info->block_group_cache, start,
1454 end, (unsigned int)-1, GFP_NOFS);
1455 }
Chris Masone37c9e62007-05-09 20:13:14 -04001456 while(1) {
Chris Masonf510cfe2007-10-15 16:14:48 -04001457 ret = find_first_extent_bit(&info->free_space_cache, 0,
1458 &start, &end, EXTENT_DIRTY);
1459 if (ret)
Chris Masone37c9e62007-05-09 20:13:14 -04001460 break;
Chris Masonf510cfe2007-10-15 16:14:48 -04001461 clear_extent_dirty(&info->free_space_cache, start,
1462 end, GFP_NOFS);
Chris Masone37c9e62007-05-09 20:13:14 -04001463 }
Chris Masonbe744172007-05-06 10:15:01 -04001464 return 0;
1465}
1466
Chris Mason9078a3e2007-04-26 16:46:15 -04001467int btrfs_read_block_groups(struct btrfs_root *root)
1468{
1469 struct btrfs_path *path;
1470 int ret;
1471 int err = 0;
Chris Mason96b51792007-10-15 16:15:19 -04001472 int bit;
Chris Mason9078a3e2007-04-26 16:46:15 -04001473 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04001474 struct btrfs_fs_info *info = root->fs_info;
Chris Mason96b51792007-10-15 16:15:19 -04001475 struct extent_map_tree *block_group_cache;
Chris Mason9078a3e2007-04-26 16:46:15 -04001476 struct btrfs_key key;
1477 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -04001478 struct extent_buffer *leaf;
Chris Mason84f54cf2007-06-12 07:43:08 -04001479 u64 group_size_blocks;
Chris Mason96b51792007-10-15 16:15:19 -04001480
1481 block_group_cache = &info->block_group_cache;
Chris Mason9078a3e2007-04-26 16:46:15 -04001482
Chris Mason84f54cf2007-06-12 07:43:08 -04001483 group_size_blocks = BTRFS_BLOCK_GROUP_SIZE >>
Chris Mason96b51792007-10-15 16:15:19 -04001484 info->sb->s_blocksize_bits;
1485
Chris Masonbe744172007-05-06 10:15:01 -04001486 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04001487 key.objectid = 0;
1488 key.offset = group_size_blocks;
Chris Mason9078a3e2007-04-26 16:46:15 -04001489 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
1490
1491 path = btrfs_alloc_path();
1492 if (!path)
1493 return -ENOMEM;
1494
1495 while(1) {
Chris Masonbe744172007-05-06 10:15:01 -04001496 ret = btrfs_search_slot(NULL, info->extent_root,
Chris Mason9078a3e2007-04-26 16:46:15 -04001497 &key, path, 0, 0);
1498 if (ret != 0) {
1499 err = ret;
1500 break;
1501 }
Chris Mason5f39d392007-10-15 16:14:19 -04001502 leaf = path->nodes[0];
1503 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason9078a3e2007-04-26 16:46:15 -04001504 cache = kmalloc(sizeof(*cache), GFP_NOFS);
1505 if (!cache) {
1506 err = -1;
1507 break;
1508 }
Chris Mason3e1ad542007-05-07 20:03:49 -04001509
Chris Mason5f39d392007-10-15 16:14:19 -04001510 read_extent_buffer(leaf, &cache->item,
1511 btrfs_item_ptr_offset(leaf, path->slots[0]),
1512 sizeof(cache->item));
Chris Mason9078a3e2007-04-26 16:46:15 -04001513 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Masone37c9e62007-05-09 20:13:14 -04001514 cache->cached = 0;
1515
Chris Mason9078a3e2007-04-26 16:46:15 -04001516 key.objectid = found_key.objectid + found_key.offset;
1517 btrfs_release_path(root, path);
Chris Mason5f39d392007-10-15 16:14:19 -04001518
Chris Mason96b51792007-10-15 16:15:19 -04001519 if (cache->item.flags & BTRFS_BLOCK_GROUP_DATA) {
1520 bit = BLOCK_GROUP_DATA;
1521 cache->data = 1;
1522 } else {
1523 bit = BLOCK_GROUP_METADATA;
1524 cache->data = 0;
Chris Mason31f3c992007-04-30 15:25:45 -04001525 }
Chris Mason96b51792007-10-15 16:15:19 -04001526
1527 /* use EXTENT_LOCKED to prevent merging */
1528 set_extent_bits(block_group_cache, found_key.objectid,
1529 found_key.objectid + found_key.offset - 1,
1530 bit | EXTENT_LOCKED, GFP_NOFS);
1531 set_state_private(block_group_cache, found_key.objectid,
1532 (u64)cache);
1533
Chris Mason9078a3e2007-04-26 16:46:15 -04001534 if (key.objectid >=
Chris Mason4b52dff2007-06-26 10:06:50 -04001535 btrfs_super_total_blocks(&info->super_copy))
Chris Mason9078a3e2007-04-26 16:46:15 -04001536 break;
1537 }
1538
1539 btrfs_free_path(path);
1540 return 0;
1541}