blob: d54ab8ef06d593ee1831429d44fd542581b9508b [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
Jens Axboeae2f5412007-10-19 09:22:59 -0400152 block_group = (struct btrfs_block_group_cache *)(unsigned long)ptr;
Chris Mason96b51792007-10-15 16:15:19 -0400153
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 Masonf84a8b32007-11-06 10:26:29 -0500171 int wrapped = 0;
Chris Masone37c9e62007-05-09 20:13:14 -0400172
Chris Masone37c9e62007-05-09 20:13:14 -0400173again:
Chris Mason54aa1f42007-06-22 14:16:25 -0400174 ret = cache_block_group(root, cache);
175 if (ret)
176 goto out;
Chris Masonf84a8b32007-11-06 10:26:29 -0500177
Chris Masone19caa52007-10-15 16:17:44 -0400178 last = max(search_start, cache->key.objectid);
179
Chris Masone37c9e62007-05-09 20:13:14 -0400180 while(1) {
Chris Masonf510cfe2007-10-15 16:14:48 -0400181 ret = find_first_extent_bit(&root->fs_info->free_space_cache,
182 last, &start, &end, EXTENT_DIRTY);
Chris Masone19caa52007-10-15 16:17:44 -0400183 if (ret) {
184 goto new_group;
185 }
Chris Masonf510cfe2007-10-15 16:14:48 -0400186
187 start = max(last, start);
188 last = end + 1;
189 if (end + 1 - start < num)
190 continue;
Chris Mason96b51792007-10-15 16:15:19 -0400191 if (start + num >= cache->key.objectid + cache->key.offset)
Chris Masone37c9e62007-05-09 20:13:14 -0400192 goto new_group;
Chris Masonf510cfe2007-10-15 16:14:48 -0400193 return start;
Chris Masone37c9e62007-05-09 20:13:14 -0400194 }
195out:
Chris Mason1a5bc162007-10-15 16:15:26 -0400196 return search_start;
Chris Masone37c9e62007-05-09 20:13:14 -0400197
198new_group:
Chris Masone19caa52007-10-15 16:17:44 -0400199 last = cache->key.objectid + cache->key.offset;
Chris Masonf84a8b32007-11-06 10:26:29 -0500200wrapped:
Chris Masone19caa52007-10-15 16:17:44 -0400201 cache = btrfs_lookup_block_group(root->fs_info, last);
Chris Masone37c9e62007-05-09 20:13:14 -0400202 if (!cache) {
Chris Masonf84a8b32007-11-06 10:26:29 -0500203 if (!wrapped) {
204 wrapped = 1;
205 last = search_start;
206 data = BTRFS_BLOCK_GROUP_MIXED;
207 goto wrapped;
208 }
Chris Mason1a5bc162007-10-15 16:15:26 -0400209 return search_start;
Chris Masone37c9e62007-05-09 20:13:14 -0400210 }
Chris Masone19caa52007-10-15 16:17:44 -0400211 cache = btrfs_find_block_group(root, cache, last, data, 0);
Chris Masone37c9e62007-05-09 20:13:14 -0400212 *cache_ret = cache;
213 goto again;
214}
215
Chris Mason84f54cf2007-06-12 07:43:08 -0400216static u64 div_factor(u64 num, int factor)
217{
218 num *= factor;
219 do_div(num, 10);
220 return num;
221}
222
Chris Mason31f3c992007-04-30 15:25:45 -0400223struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
224 struct btrfs_block_group_cache
Chris Masonbe744172007-05-06 10:15:01 -0400225 *hint, u64 search_start,
Chris Masonde428b62007-05-18 13:28:27 -0400226 int data, int owner)
Chris Masoncd1bc462007-04-27 10:08:34 -0400227{
Chris Mason96b51792007-10-15 16:15:19 -0400228 struct btrfs_block_group_cache *cache;
229 struct extent_map_tree *block_group_cache;
Chris Mason31f3c992007-04-30 15:25:45 -0400230 struct btrfs_block_group_cache *found_group = NULL;
Chris Masoncd1bc462007-04-27 10:08:34 -0400231 struct btrfs_fs_info *info = root->fs_info;
232 u64 used;
Chris Mason31f3c992007-04-30 15:25:45 -0400233 u64 last = 0;
234 u64 hint_last;
Chris Mason96b51792007-10-15 16:15:19 -0400235 u64 start;
236 u64 end;
237 u64 free_check;
238 u64 ptr;
239 int bit;
Chris Masoncd1bc462007-04-27 10:08:34 -0400240 int ret;
Chris Mason31f3c992007-04-30 15:25:45 -0400241 int full_search = 0;
Chris Masonde428b62007-05-18 13:28:27 -0400242 int factor = 8;
Chris Mason1e2677e2007-05-29 16:52:18 -0400243 int data_swap = 0;
Chris Masonde428b62007-05-18 13:28:27 -0400244
Chris Mason96b51792007-10-15 16:15:19 -0400245 block_group_cache = &info->block_group_cache;
246
Chris Masonde428b62007-05-18 13:28:27 -0400247 if (!owner)
Chris Masonf84a8b32007-11-06 10:26:29 -0500248 factor = 8;
Chris Masonbe744172007-05-06 10:15:01 -0400249
Chris Masonf84a8b32007-11-06 10:26:29 -0500250 if (data == BTRFS_BLOCK_GROUP_MIXED)
251 bit = BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA;
252 else if (data)
Chris Mason96b51792007-10-15 16:15:19 -0400253 bit = BLOCK_GROUP_DATA;
254 else
255 bit = BLOCK_GROUP_METADATA;
Chris Masonbe744172007-05-06 10:15:01 -0400256
257 if (search_start) {
258 struct btrfs_block_group_cache *shint;
Chris Mason5276aed2007-06-11 21:33:38 -0400259 shint = btrfs_lookup_block_group(info, search_start);
Chris Masonf84a8b32007-11-06 10:26:29 -0500260 if (shint && (shint->data == data ||
261 shint->data == BTRFS_BLOCK_GROUP_MIXED)) {
Chris Masonbe744172007-05-06 10:15:01 -0400262 used = btrfs_block_group_used(&shint->item);
Chris Mason1a5bc162007-10-15 16:15:26 -0400263 if (used < div_factor(shint->key.offset, factor)) {
Chris Masonbe744172007-05-06 10:15:01 -0400264 return shint;
265 }
266 }
267 }
Chris Masonf84a8b32007-11-06 10:26:29 -0500268 if (hint && (hint->data == data ||
269 hint->data == BTRFS_BLOCK_GROUP_MIXED)) {
Chris Mason31f3c992007-04-30 15:25:45 -0400270 used = btrfs_block_group_used(&hint->item);
Chris Mason1a5bc162007-10-15 16:15:26 -0400271 if (used < div_factor(hint->key.offset, factor)) {
Chris Mason31f3c992007-04-30 15:25:45 -0400272 return hint;
273 }
Chris Masone19caa52007-10-15 16:17:44 -0400274 last = hint->key.objectid + hint->key.offset;
Chris Mason31f3c992007-04-30 15:25:45 -0400275 hint_last = last;
276 } else {
Chris Masone37c9e62007-05-09 20:13:14 -0400277 if (hint)
278 hint_last = max(hint->key.objectid, search_start);
279 else
280 hint_last = search_start;
281
282 last = hint_last;
Chris Mason31f3c992007-04-30 15:25:45 -0400283 }
Chris Mason31f3c992007-04-30 15:25:45 -0400284again:
Chris Masoncd1bc462007-04-27 10:08:34 -0400285 while(1) {
Chris Mason96b51792007-10-15 16:15:19 -0400286 ret = find_first_extent_bit(block_group_cache, last,
287 &start, &end, bit);
288 if (ret)
Chris Masoncd1bc462007-04-27 10:08:34 -0400289 break;
Chris Mason96b51792007-10-15 16:15:19 -0400290
291 ret = get_state_private(block_group_cache, start, &ptr);
292 if (ret)
293 break;
294
Jens Axboeae2f5412007-10-19 09:22:59 -0400295 cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
Chris Mason96b51792007-10-15 16:15:19 -0400296 last = cache->key.objectid + cache->key.offset;
297 used = btrfs_block_group_used(&cache->item);
298
299 if (full_search)
300 free_check = cache->key.offset;
301 else
302 free_check = div_factor(cache->key.offset, factor);
303
Chris Mason1a5bc162007-10-15 16:15:26 -0400304 if (used < free_check) {
Chris Mason96b51792007-10-15 16:15:19 -0400305 found_group = cache;
306 goto found;
Chris Masoncd1bc462007-04-27 10:08:34 -0400307 }
Chris Masonde428b62007-05-18 13:28:27 -0400308 cond_resched();
Chris Masoncd1bc462007-04-27 10:08:34 -0400309 }
Chris Mason31f3c992007-04-30 15:25:45 -0400310 if (!full_search) {
Chris Masonbe744172007-05-06 10:15:01 -0400311 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -0400312 full_search = 1;
313 goto again;
314 }
Chris Mason1e2677e2007-05-29 16:52:18 -0400315 if (!data_swap) {
Chris Mason1e2677e2007-05-29 16:52:18 -0400316 data_swap = 1;
Chris Mason96b51792007-10-15 16:15:19 -0400317 bit = BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA;
Chris Mason1e2677e2007-05-29 16:52:18 -0400318 last = search_start;
319 goto again;
320 }
Chris Masonbe744172007-05-06 10:15:01 -0400321found:
Chris Mason31f3c992007-04-30 15:25:45 -0400322 return found_group;
Chris Masoncd1bc462007-04-27 10:08:34 -0400323}
324
Chris Masonb18c6682007-04-17 13:26:50 -0400325int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
326 struct btrfs_root *root,
Chris Masondb945352007-10-15 16:15:53 -0400327 u64 bytenr, u64 num_bytes)
Chris Mason02217ed2007-03-02 16:08:05 -0500328{
Chris Mason5caf2a02007-04-02 11:20:42 -0400329 struct btrfs_path *path;
Chris Mason02217ed2007-03-02 16:08:05 -0500330 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400331 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400332 struct extent_buffer *l;
Chris Mason234b63a2007-03-13 10:46:10 -0400333 struct btrfs_extent_item *item;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400334 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500335
Chris Masondb945352007-10-15 16:15:53 -0400336 WARN_ON(num_bytes < root->sectorsize);
Chris Mason5caf2a02007-04-02 11:20:42 -0400337 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -0400338 if (!path)
339 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -0400340
Chris Masondb945352007-10-15 16:15:53 -0400341 key.objectid = bytenr;
Chris Mason62e27492007-03-15 12:56:47 -0400342 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masondb945352007-10-15 16:15:53 -0400343 key.offset = num_bytes;
Chris Mason5caf2a02007-04-02 11:20:42 -0400344 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -0400345 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400346 if (ret < 0)
347 return ret;
Chris Masona429e512007-04-18 16:15:28 -0400348 if (ret != 0) {
Chris Masona28ec192007-03-06 20:08:01 -0500349 BUG();
Chris Masona429e512007-04-18 16:15:28 -0400350 }
Chris Mason02217ed2007-03-02 16:08:05 -0500351 BUG_ON(ret != 0);
Chris Mason5f39d392007-10-15 16:14:19 -0400352 l = path->nodes[0];
Chris Mason5caf2a02007-04-02 11:20:42 -0400353 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400354 refs = btrfs_extent_refs(l, item);
355 btrfs_set_extent_refs(l, item, refs + 1);
Chris Mason5caf2a02007-04-02 11:20:42 -0400356 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masona28ec192007-03-06 20:08:01 -0500357
Chris Mason5caf2a02007-04-02 11:20:42 -0400358 btrfs_release_path(root->fs_info->extent_root, path);
359 btrfs_free_path(path);
Chris Mason9f5fae22007-03-20 14:38:32 -0400360 finish_current_insert(trans, root->fs_info->extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -0400361 del_pending_extents(trans, root->fs_info->extent_root);
Chris Mason02217ed2007-03-02 16:08:05 -0500362 return 0;
363}
364
Chris Masone9d0b132007-08-10 14:06:19 -0400365int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
366 struct btrfs_root *root)
367{
368 finish_current_insert(trans, root->fs_info->extent_root);
369 del_pending_extents(trans, root->fs_info->extent_root);
370 return 0;
371}
372
Chris Masonb18c6682007-04-17 13:26:50 -0400373static int lookup_extent_ref(struct btrfs_trans_handle *trans,
Chris Masondb945352007-10-15 16:15:53 -0400374 struct btrfs_root *root, u64 bytenr,
375 u64 num_bytes, u32 *refs)
Chris Masona28ec192007-03-06 20:08:01 -0500376{
Chris Mason5caf2a02007-04-02 11:20:42 -0400377 struct btrfs_path *path;
Chris Masona28ec192007-03-06 20:08:01 -0500378 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400379 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400380 struct extent_buffer *l;
Chris Mason234b63a2007-03-13 10:46:10 -0400381 struct btrfs_extent_item *item;
Chris Mason5caf2a02007-04-02 11:20:42 -0400382
Chris Masondb945352007-10-15 16:15:53 -0400383 WARN_ON(num_bytes < root->sectorsize);
Chris Mason5caf2a02007-04-02 11:20:42 -0400384 path = btrfs_alloc_path();
Chris Masondb945352007-10-15 16:15:53 -0400385 key.objectid = bytenr;
386 key.offset = num_bytes;
Chris Mason62e27492007-03-15 12:56:47 -0400387 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Mason5caf2a02007-04-02 11:20:42 -0400388 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -0400389 0, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400390 if (ret < 0)
391 goto out;
Chris Mason5f39d392007-10-15 16:14:19 -0400392 if (ret != 0) {
393 btrfs_print_leaf(root, path->nodes[0]);
Chris Masondb945352007-10-15 16:15:53 -0400394 printk("failed to find block number %Lu\n", bytenr);
Chris Masona28ec192007-03-06 20:08:01 -0500395 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -0400396 }
397 l = path->nodes[0];
Chris Mason5caf2a02007-04-02 11:20:42 -0400398 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400399 *refs = btrfs_extent_refs(l, item);
Chris Mason54aa1f42007-06-22 14:16:25 -0400400out:
Chris Mason5caf2a02007-04-02 11:20:42 -0400401 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -0500402 return 0;
403}
404
Chris Masonc5739bb2007-04-10 09:27:04 -0400405int btrfs_inc_root_ref(struct btrfs_trans_handle *trans,
406 struct btrfs_root *root)
407{
Chris Masondb945352007-10-15 16:15:53 -0400408 return btrfs_inc_extent_ref(trans, root, root->node->start,
409 root->node->len);
Chris Masonc5739bb2007-04-10 09:27:04 -0400410}
411
Chris Masone089f052007-03-16 16:20:31 -0400412int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400413 struct extent_buffer *buf)
Chris Mason02217ed2007-03-02 16:08:05 -0500414{
Chris Masondb945352007-10-15 16:15:53 -0400415 u64 bytenr;
Chris Mason5f39d392007-10-15 16:14:19 -0400416 u32 nritems;
417 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -0400418 struct btrfs_file_extent_item *fi;
Chris Mason02217ed2007-03-02 16:08:05 -0500419 int i;
Chris Masondb945352007-10-15 16:15:53 -0400420 int level;
Chris Mason6407bf62007-03-27 06:33:00 -0400421 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -0400422 int faili;
423 int err;
Chris Masona28ec192007-03-06 20:08:01 -0500424
Chris Mason3768f362007-03-13 16:47:54 -0400425 if (!root->ref_cows)
Chris Masona28ec192007-03-06 20:08:01 -0500426 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400427
Chris Masondb945352007-10-15 16:15:53 -0400428 level = btrfs_header_level(buf);
Chris Mason5f39d392007-10-15 16:14:19 -0400429 nritems = btrfs_header_nritems(buf);
430 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -0400431 if (level == 0) {
432 u64 disk_bytenr;
Chris Mason5f39d392007-10-15 16:14:19 -0400433 btrfs_item_key_to_cpu(buf, &key, i);
434 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason6407bf62007-03-27 06:33:00 -0400435 continue;
Chris Mason5f39d392007-10-15 16:14:19 -0400436 fi = btrfs_item_ptr(buf, i,
Chris Mason6407bf62007-03-27 06:33:00 -0400437 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400438 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason236454d2007-04-19 13:37:44 -0400439 BTRFS_FILE_EXTENT_INLINE)
440 continue;
Chris Masondb945352007-10-15 16:15:53 -0400441 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
442 if (disk_bytenr == 0)
Chris Mason3a686372007-05-24 13:35:57 -0400443 continue;
Chris Masondb945352007-10-15 16:15:53 -0400444 ret = btrfs_inc_extent_ref(trans, root, disk_bytenr,
445 btrfs_file_extent_disk_num_bytes(buf, fi));
Chris Mason54aa1f42007-06-22 14:16:25 -0400446 if (ret) {
447 faili = i;
448 goto fail;
449 }
Chris Mason6407bf62007-03-27 06:33:00 -0400450 } else {
Chris Masondb945352007-10-15 16:15:53 -0400451 bytenr = btrfs_node_blockptr(buf, i);
452 ret = btrfs_inc_extent_ref(trans, root, bytenr,
453 btrfs_level_size(root, level - 1));
Chris Mason54aa1f42007-06-22 14:16:25 -0400454 if (ret) {
455 faili = i;
456 goto fail;
457 }
Chris Mason6407bf62007-03-27 06:33:00 -0400458 }
Chris Mason02217ed2007-03-02 16:08:05 -0500459 }
460 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -0400461fail:
Chris Masonccd467d2007-06-28 15:57:36 -0400462 WARN_ON(1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400463 for (i =0; i < faili; i++) {
Chris Masondb945352007-10-15 16:15:53 -0400464 if (level == 0) {
465 u64 disk_bytenr;
Chris Mason5f39d392007-10-15 16:14:19 -0400466 btrfs_item_key_to_cpu(buf, &key, i);
467 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -0400468 continue;
Chris Mason5f39d392007-10-15 16:14:19 -0400469 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -0400470 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400471 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -0400472 BTRFS_FILE_EXTENT_INLINE)
473 continue;
Chris Masondb945352007-10-15 16:15:53 -0400474 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
475 if (disk_bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -0400476 continue;
Chris Masondb945352007-10-15 16:15:53 -0400477 err = btrfs_free_extent(trans, root, disk_bytenr,
478 btrfs_file_extent_disk_num_bytes(buf,
Chris Mason5f39d392007-10-15 16:14:19 -0400479 fi), 0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400480 BUG_ON(err);
481 } else {
Chris Masondb945352007-10-15 16:15:53 -0400482 bytenr = btrfs_node_blockptr(buf, i);
483 err = btrfs_free_extent(trans, root, bytenr,
484 btrfs_level_size(root, level - 1), 0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400485 BUG_ON(err);
486 }
487 }
488 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -0500489}
490
Chris Mason9078a3e2007-04-26 16:46:15 -0400491static int write_one_cache_group(struct btrfs_trans_handle *trans,
492 struct btrfs_root *root,
493 struct btrfs_path *path,
494 struct btrfs_block_group_cache *cache)
495{
496 int ret;
497 int pending_ret;
498 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -0400499 unsigned long bi;
500 struct extent_buffer *leaf;
Chris Mason9078a3e2007-04-26 16:46:15 -0400501
Chris Mason9078a3e2007-04-26 16:46:15 -0400502 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400503 if (ret < 0)
504 goto fail;
Chris Mason9078a3e2007-04-26 16:46:15 -0400505 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -0400506
507 leaf = path->nodes[0];
508 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
509 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
510 btrfs_mark_buffer_dirty(leaf);
Chris Mason9078a3e2007-04-26 16:46:15 -0400511 btrfs_release_path(extent_root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -0400512fail:
Chris Mason9078a3e2007-04-26 16:46:15 -0400513 finish_current_insert(trans, extent_root);
514 pending_ret = del_pending_extents(trans, extent_root);
515 if (ret)
516 return ret;
517 if (pending_ret)
518 return pending_ret;
519 return 0;
520
521}
522
Chris Mason96b51792007-10-15 16:15:19 -0400523int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
524 struct btrfs_root *root)
Chris Mason9078a3e2007-04-26 16:46:15 -0400525{
Chris Mason96b51792007-10-15 16:15:19 -0400526 struct extent_map_tree *block_group_cache;
527 struct btrfs_block_group_cache *cache;
Chris Mason9078a3e2007-04-26 16:46:15 -0400528 int ret;
529 int err = 0;
530 int werr = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -0400531 struct btrfs_path *path;
Chris Mason96b51792007-10-15 16:15:19 -0400532 u64 last = 0;
533 u64 start;
534 u64 end;
535 u64 ptr;
Chris Mason9078a3e2007-04-26 16:46:15 -0400536
Chris Mason96b51792007-10-15 16:15:19 -0400537 block_group_cache = &root->fs_info->block_group_cache;
Chris Mason9078a3e2007-04-26 16:46:15 -0400538 path = btrfs_alloc_path();
539 if (!path)
540 return -ENOMEM;
541
542 while(1) {
Chris Mason96b51792007-10-15 16:15:19 -0400543 ret = find_first_extent_bit(block_group_cache, last,
544 &start, &end, BLOCK_GROUP_DIRTY);
545 if (ret)
Chris Mason9078a3e2007-04-26 16:46:15 -0400546 break;
Chris Mason54aa1f42007-06-22 14:16:25 -0400547
Chris Mason96b51792007-10-15 16:15:19 -0400548 last = end + 1;
549 ret = get_state_private(block_group_cache, start, &ptr);
550 if (ret)
551 break;
552
Jens Axboeae2f5412007-10-19 09:22:59 -0400553 cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
Chris Mason96b51792007-10-15 16:15:19 -0400554 err = write_one_cache_group(trans, root,
555 path, cache);
556 /*
557 * if we fail to write the cache group, we want
558 * to keep it marked dirty in hopes that a later
559 * write will work
560 */
561 if (err) {
562 werr = err;
563 continue;
Chris Mason9078a3e2007-04-26 16:46:15 -0400564 }
Chris Mason96b51792007-10-15 16:15:19 -0400565 clear_extent_bits(block_group_cache, start, end,
566 BLOCK_GROUP_DIRTY, GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -0400567 }
568 btrfs_free_path(path);
569 return werr;
570}
571
572static int update_block_group(struct btrfs_trans_handle *trans,
573 struct btrfs_root *root,
Chris Masondb945352007-10-15 16:15:53 -0400574 u64 bytenr, u64 num_bytes, int alloc,
575 int mark_free, int data)
Chris Mason9078a3e2007-04-26 16:46:15 -0400576{
577 struct btrfs_block_group_cache *cache;
578 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -0400579 u64 total = num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -0400580 u64 old_val;
Chris Masondb945352007-10-15 16:15:53 -0400581 u64 byte_in_group;
Chris Mason96b51792007-10-15 16:15:19 -0400582 u64 start;
583 u64 end;
Chris Mason3e1ad542007-05-07 20:03:49 -0400584
Chris Mason9078a3e2007-04-26 16:46:15 -0400585 while(total) {
Chris Masondb945352007-10-15 16:15:53 -0400586 cache = btrfs_lookup_block_group(info, bytenr);
Chris Mason3e1ad542007-05-07 20:03:49 -0400587 if (!cache) {
Chris Mason9078a3e2007-04-26 16:46:15 -0400588 return -1;
Chris Masoncd1bc462007-04-27 10:08:34 -0400589 }
Chris Masondb945352007-10-15 16:15:53 -0400590 byte_in_group = bytenr - cache->key.objectid;
591 WARN_ON(byte_in_group > cache->key.offset);
Chris Mason96b51792007-10-15 16:15:19 -0400592 start = cache->key.objectid;
593 end = start + cache->key.offset - 1;
594 set_extent_bits(&info->block_group_cache, start, end,
595 BLOCK_GROUP_DIRTY, GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -0400596
597 old_val = btrfs_block_group_used(&cache->item);
Chris Masondb945352007-10-15 16:15:53 -0400598 num_bytes = min(total, cache->key.offset - byte_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -0400599 if (alloc) {
Chris Mason1e2677e2007-05-29 16:52:18 -0400600 if (cache->data != data &&
Chris Mason84f54cf2007-06-12 07:43:08 -0400601 old_val < (cache->key.offset >> 1)) {
Chris Mason96b51792007-10-15 16:15:19 -0400602 int bit_to_clear;
603 int bit_to_set;
Chris Mason96b51792007-10-15 16:15:19 -0400604 cache->data = data;
Chris Mason1e2677e2007-05-29 16:52:18 -0400605 if (data) {
Yanb97f9202007-11-01 11:28:41 -0400606 bit_to_clear = BLOCK_GROUP_METADATA;
607 bit_to_set = BLOCK_GROUP_DATA;
Chris Masonf84a8b32007-11-06 10:26:29 -0500608 cache->item.flags &=
609 ~BTRFS_BLOCK_GROUP_MIXED;
Chris Mason1e2677e2007-05-29 16:52:18 -0400610 cache->item.flags |=
611 BTRFS_BLOCK_GROUP_DATA;
612 } else {
Yanb97f9202007-11-01 11:28:41 -0400613 bit_to_clear = BLOCK_GROUP_DATA;
614 bit_to_set = BLOCK_GROUP_METADATA;
Chris Mason1e2677e2007-05-29 16:52:18 -0400615 cache->item.flags &=
Chris Masonf84a8b32007-11-06 10:26:29 -0500616 ~BTRFS_BLOCK_GROUP_MIXED;
617 cache->item.flags &=
Chris Mason1e2677e2007-05-29 16:52:18 -0400618 ~BTRFS_BLOCK_GROUP_DATA;
619 }
Chris Mason96b51792007-10-15 16:15:19 -0400620 clear_extent_bits(&info->block_group_cache,
621 start, end, bit_to_clear,
622 GFP_NOFS);
623 set_extent_bits(&info->block_group_cache,
624 start, end, bit_to_set,
625 GFP_NOFS);
Chris Masonf84a8b32007-11-06 10:26:29 -0500626 } else if (cache->data != data &&
627 cache->data != BTRFS_BLOCK_GROUP_MIXED) {
628 cache->data = BTRFS_BLOCK_GROUP_MIXED;
629 set_extent_bits(&info->block_group_cache,
630 start, end,
631 BLOCK_GROUP_DATA |
632 BLOCK_GROUP_METADATA,
633 GFP_NOFS);
Chris Mason1e2677e2007-05-29 16:52:18 -0400634 }
Chris Masondb945352007-10-15 16:15:53 -0400635 old_val += num_bytes;
Chris Masoncd1bc462007-04-27 10:08:34 -0400636 } else {
Chris Masondb945352007-10-15 16:15:53 -0400637 old_val -= num_bytes;
Chris Masonf510cfe2007-10-15 16:14:48 -0400638 if (mark_free) {
639 set_extent_dirty(&info->free_space_cache,
Chris Masondb945352007-10-15 16:15:53 -0400640 bytenr, bytenr + num_bytes - 1,
Chris Masonf510cfe2007-10-15 16:14:48 -0400641 GFP_NOFS);
Chris Masone37c9e62007-05-09 20:13:14 -0400642 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400643 }
Chris Mason9078a3e2007-04-26 16:46:15 -0400644 btrfs_set_block_group_used(&cache->item, old_val);
Chris Masondb945352007-10-15 16:15:53 -0400645 total -= num_bytes;
646 bytenr += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -0400647 }
648 return 0;
649}
650
Chris Mason1a5bc162007-10-15 16:15:26 -0400651int btrfs_copy_pinned(struct btrfs_root *root, struct extent_map_tree *copy)
Chris Masonccd467d2007-06-28 15:57:36 -0400652{
Chris Masonccd467d2007-06-28 15:57:36 -0400653 u64 last = 0;
Chris Mason1a5bc162007-10-15 16:15:26 -0400654 u64 start;
655 u64 end;
656 struct extent_map_tree *pinned_extents = &root->fs_info->pinned_extents;
Chris Masonccd467d2007-06-28 15:57:36 -0400657 int ret;
Chris Masonccd467d2007-06-28 15:57:36 -0400658
659 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -0400660 ret = find_first_extent_bit(pinned_extents, last,
661 &start, &end, EXTENT_DIRTY);
662 if (ret)
Chris Masonccd467d2007-06-28 15:57:36 -0400663 break;
Chris Mason1a5bc162007-10-15 16:15:26 -0400664 set_extent_dirty(copy, start, end, GFP_NOFS);
665 last = end + 1;
Chris Masonccd467d2007-06-28 15:57:36 -0400666 }
667 return 0;
668}
669
670int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
671 struct btrfs_root *root,
Chris Mason1a5bc162007-10-15 16:15:26 -0400672 struct extent_map_tree *unpin)
Chris Masona28ec192007-03-06 20:08:01 -0500673{
Chris Mason1a5bc162007-10-15 16:15:26 -0400674 u64 start;
675 u64 end;
Chris Masona28ec192007-03-06 20:08:01 -0500676 int ret;
Chris Mason1a5bc162007-10-15 16:15:26 -0400677 struct extent_map_tree *pinned_extents = &root->fs_info->pinned_extents;
Chris Masonf510cfe2007-10-15 16:14:48 -0400678 struct extent_map_tree *free_space_cache;
679
680 free_space_cache = &root->fs_info->free_space_cache;
Chris Masona28ec192007-03-06 20:08:01 -0500681
682 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -0400683 ret = find_first_extent_bit(unpin, 0, &start, &end,
684 EXTENT_DIRTY);
685 if (ret)
Chris Masona28ec192007-03-06 20:08:01 -0500686 break;
Chris Mason1a5bc162007-10-15 16:15:26 -0400687
688 clear_extent_dirty(pinned_extents, start, end,
689 GFP_NOFS);
690 clear_extent_dirty(unpin, start, end, GFP_NOFS);
691 set_extent_dirty(free_space_cache, start, end, GFP_NOFS);
Chris Masona28ec192007-03-06 20:08:01 -0500692 }
693 return 0;
694}
695
Chris Masone089f052007-03-16 16:20:31 -0400696static int finish_current_insert(struct btrfs_trans_handle *trans, struct
697 btrfs_root *extent_root)
Chris Mason037e6392007-03-07 11:50:24 -0500698{
Chris Masone2fa7222007-03-12 16:22:34 -0400699 struct btrfs_key ins;
Chris Mason234b63a2007-03-13 10:46:10 -0400700 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -0500701 int ret;
Chris Mason1a5bc162007-10-15 16:15:26 -0400702 int err = 0;
703 u64 start;
704 u64 end;
Chris Mason1261ec42007-03-20 20:35:03 -0400705 struct btrfs_fs_info *info = extent_root->fs_info;
Chris Mason037e6392007-03-07 11:50:24 -0500706
Chris Mason5f39d392007-10-15 16:14:19 -0400707 btrfs_set_stack_extent_refs(&extent_item, 1);
Chris Mason62e27492007-03-15 12:56:47 -0400708 btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
Chris Mason5f39d392007-10-15 16:14:19 -0400709 btrfs_set_stack_extent_owner(&extent_item,
710 extent_root->root_key.objectid);
Chris Mason037e6392007-03-07 11:50:24 -0500711
Chris Mason26b80032007-08-08 20:17:12 -0400712 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -0400713 ret = find_first_extent_bit(&info->extent_ins, 0, &start,
714 &end, EXTENT_LOCKED);
715 if (ret)
Chris Mason26b80032007-08-08 20:17:12 -0400716 break;
717
Chris Mason1a5bc162007-10-15 16:15:26 -0400718 ins.objectid = start;
719 ins.offset = end + 1 - start;
720 err = btrfs_insert_item(trans, extent_root, &ins,
721 &extent_item, sizeof(extent_item));
722 clear_extent_bits(&info->extent_ins, start, end, EXTENT_LOCKED,
723 GFP_NOFS);
Chris Mason037e6392007-03-07 11:50:24 -0500724 }
Chris Mason037e6392007-03-07 11:50:24 -0500725 return 0;
726}
727
Chris Masondb945352007-10-15 16:15:53 -0400728static int pin_down_bytes(struct btrfs_root *root, u64 bytenr, u32 num_bytes,
729 int pending)
Chris Masone20d96d2007-03-22 12:13:20 -0400730{
Chris Mason1a5bc162007-10-15 16:15:26 -0400731 int err = 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400732 struct extent_buffer *buf;
Chris Mason78fae272007-03-25 11:35:08 -0400733
Chris Masonf4b9aa82007-03-27 11:05:53 -0400734 if (!pending) {
Chris Masondb945352007-10-15 16:15:53 -0400735 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
Chris Mason5f39d392007-10-15 16:14:19 -0400736 if (buf) {
737 if (btrfs_buffer_uptodate(buf)) {
Chris Mason2c90e5d2007-04-02 10:50:19 -0400738 u64 transid =
739 root->fs_info->running_transaction->transid;
Chris Mason5f39d392007-10-15 16:14:19 -0400740 if (btrfs_header_generation(buf) == transid) {
741 free_extent_buffer(buf);
Yanc5492282007-11-06 10:25:25 -0500742 return 1;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400743 }
Chris Masonf4b9aa82007-03-27 11:05:53 -0400744 }
Chris Mason5f39d392007-10-15 16:14:19 -0400745 free_extent_buffer(buf);
Chris Mason8ef97622007-03-26 10:15:30 -0400746 }
Chris Mason1a5bc162007-10-15 16:15:26 -0400747 set_extent_dirty(&root->fs_info->pinned_extents,
Chris Masondb945352007-10-15 16:15:53 -0400748 bytenr, bytenr + num_bytes - 1, GFP_NOFS);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400749 } else {
Chris Mason1a5bc162007-10-15 16:15:26 -0400750 set_extent_bits(&root->fs_info->pending_del,
Chris Masondb945352007-10-15 16:15:53 -0400751 bytenr, bytenr + num_bytes - 1,
752 EXTENT_LOCKED, GFP_NOFS);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400753 }
Chris Masonbe744172007-05-06 10:15:01 -0400754 BUG_ON(err < 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400755 return 0;
756}
757
Chris Masona28ec192007-03-06 20:08:01 -0500758/*
759 * remove an extent from the root, returns 0 on success
760 */
Chris Masone089f052007-03-16 16:20:31 -0400761static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masondb945352007-10-15 16:15:53 -0400762 *root, u64 bytenr, u64 num_bytes, int pin,
Chris Masone37c9e62007-05-09 20:13:14 -0400763 int mark_free)
Chris Masona28ec192007-03-06 20:08:01 -0500764{
Chris Mason5caf2a02007-04-02 11:20:42 -0400765 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -0400766 struct btrfs_key key;
Chris Mason1261ec42007-03-20 20:35:03 -0400767 struct btrfs_fs_info *info = root->fs_info;
768 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -0400769 struct extent_buffer *leaf;
Chris Masona28ec192007-03-06 20:08:01 -0500770 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400771 struct btrfs_extent_item *ei;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400772 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500773
Chris Masondb945352007-10-15 16:15:53 -0400774 key.objectid = bytenr;
Chris Mason62e27492007-03-15 12:56:47 -0400775 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masondb945352007-10-15 16:15:53 -0400776 key.offset = num_bytes;
Chris Masona28ec192007-03-06 20:08:01 -0500777
Chris Mason5caf2a02007-04-02 11:20:42 -0400778 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -0400779 if (!path)
780 return -ENOMEM;
781
Chris Mason5caf2a02007-04-02 11:20:42 -0400782 ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400783 if (ret < 0)
784 return ret;
785 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -0400786
787 leaf = path->nodes[0];
788 ei = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason123abc82007-03-14 14:14:43 -0400789 struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400790 refs = btrfs_extent_refs(leaf, ei);
791 BUG_ON(refs == 0);
792 refs -= 1;
793 btrfs_set_extent_refs(leaf, ei, refs);
794 btrfs_mark_buffer_dirty(leaf);
795
Chris Masoncf27e1e2007-03-13 09:49:06 -0400796 if (refs == 0) {
Chris Masondb945352007-10-15 16:15:53 -0400797 u64 super_used;
798 u64 root_used;
Chris Mason78fae272007-03-25 11:35:08 -0400799
800 if (pin) {
Chris Masondb945352007-10-15 16:15:53 -0400801 ret = pin_down_bytes(root, bytenr, num_bytes, 0);
Yanc5492282007-11-06 10:25:25 -0500802 if (ret > 0)
803 mark_free = 1;
804 BUG_ON(ret < 0);
Chris Mason78fae272007-03-25 11:35:08 -0400805 }
806
Josef Bacik58176a92007-08-29 15:47:34 -0400807 /* block accounting for super block */
Chris Masondb945352007-10-15 16:15:53 -0400808 super_used = btrfs_super_bytes_used(&info->super_copy);
809 btrfs_set_super_bytes_used(&info->super_copy,
810 super_used - num_bytes);
Josef Bacik58176a92007-08-29 15:47:34 -0400811
812 /* block accounting for root item */
Chris Masondb945352007-10-15 16:15:53 -0400813 root_used = btrfs_root_used(&root->root_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400814 btrfs_set_root_used(&root->root_item,
Chris Masondb945352007-10-15 16:15:53 -0400815 root_used - num_bytes);
Josef Bacik58176a92007-08-29 15:47:34 -0400816
Chris Mason5caf2a02007-04-02 11:20:42 -0400817 ret = btrfs_del_item(trans, extent_root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -0400818 if (ret) {
819 return ret;
820 }
Chris Masondb945352007-10-15 16:15:53 -0400821 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
Chris Mason1e2677e2007-05-29 16:52:18 -0400822 mark_free, 0);
Chris Mason9078a3e2007-04-26 16:46:15 -0400823 BUG_ON(ret);
Chris Masona28ec192007-03-06 20:08:01 -0500824 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400825 btrfs_free_path(path);
Chris Masone089f052007-03-16 16:20:31 -0400826 finish_current_insert(trans, extent_root);
Chris Masona28ec192007-03-06 20:08:01 -0500827 return ret;
828}
829
830/*
Chris Masonfec577f2007-02-26 10:40:21 -0500831 * find all the blocks marked as pending in the radix tree and remove
832 * them from the extent map
833 */
Chris Masone089f052007-03-16 16:20:31 -0400834static int del_pending_extents(struct btrfs_trans_handle *trans, struct
835 btrfs_root *extent_root)
Chris Masonfec577f2007-02-26 10:40:21 -0500836{
837 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -0400838 int err = 0;
Chris Mason1a5bc162007-10-15 16:15:26 -0400839 u64 start;
840 u64 end;
841 struct extent_map_tree *pending_del;
842 struct extent_map_tree *pinned_extents;
Chris Mason8ef97622007-03-26 10:15:30 -0400843
Chris Mason1a5bc162007-10-15 16:15:26 -0400844 pending_del = &extent_root->fs_info->pending_del;
845 pinned_extents = &extent_root->fs_info->pinned_extents;
Chris Masonfec577f2007-02-26 10:40:21 -0500846
847 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -0400848 ret = find_first_extent_bit(pending_del, 0, &start, &end,
849 EXTENT_LOCKED);
850 if (ret)
Chris Masonfec577f2007-02-26 10:40:21 -0500851 break;
Chris Mason1a5bc162007-10-15 16:15:26 -0400852
853 set_extent_dirty(pinned_extents, start, end, GFP_NOFS);
854 clear_extent_bits(pending_del, start, end, EXTENT_LOCKED,
855 GFP_NOFS);
856 ret = __free_extent(trans, extent_root,
857 start, end + 1 - start, 0, 0);
858 if (ret)
859 err = ret;
Chris Masonfec577f2007-02-26 10:40:21 -0500860 }
Chris Masone20d96d2007-03-22 12:13:20 -0400861 return err;
Chris Masonfec577f2007-02-26 10:40:21 -0500862}
863
864/*
865 * remove an extent from the root, returns 0 on success
866 */
Chris Masone089f052007-03-16 16:20:31 -0400867int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masondb945352007-10-15 16:15:53 -0400868 *root, u64 bytenr, u64 num_bytes, int pin)
Chris Masonfec577f2007-02-26 10:40:21 -0500869{
Chris Mason9f5fae22007-03-20 14:38:32 -0400870 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Masonfec577f2007-02-26 10:40:21 -0500871 int pending_ret;
872 int ret;
Chris Masona28ec192007-03-06 20:08:01 -0500873
Chris Masondb945352007-10-15 16:15:53 -0400874 WARN_ON(num_bytes < root->sectorsize);
Chris Masona28ec192007-03-06 20:08:01 -0500875 if (root == extent_root) {
Chris Masondb945352007-10-15 16:15:53 -0400876 pin_down_bytes(root, bytenr, num_bytes, 1);
Chris Masona28ec192007-03-06 20:08:01 -0500877 return 0;
878 }
Chris Masondb945352007-10-15 16:15:53 -0400879 ret = __free_extent(trans, root, bytenr, num_bytes, pin, pin == 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400880 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
Chris Masonfec577f2007-02-26 10:40:21 -0500881 return ret ? ret : pending_ret;
882}
883
884/*
885 * walks the btree of allocated extents and find a hole of a given size.
886 * The key ins is changed to record the hole:
887 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -0400888 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -0500889 * ins->offset == number of blocks
890 * Any available blocks before search_start are skipped.
891 */
Chris Masone089f052007-03-16 16:20:31 -0400892static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masondb945352007-10-15 16:15:53 -0400893 *orig_root, u64 num_bytes, u64 empty_size,
894 u64 search_start, u64 search_end, u64 hint_byte,
Chris Masonf2654de2007-06-26 12:20:46 -0400895 struct btrfs_key *ins, u64 exclude_start,
896 u64 exclude_nr, int data)
Chris Masonfec577f2007-02-26 10:40:21 -0500897{
Chris Mason5caf2a02007-04-02 11:20:42 -0400898 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -0400899 struct btrfs_key key;
Chris Masonfec577f2007-02-26 10:40:21 -0500900 int ret;
901 u64 hole_size = 0;
902 int slot = 0;
Chris Masondb945352007-10-15 16:15:53 -0400903 u64 last_byte = 0;
Chris Masonbe744172007-05-06 10:15:01 -0400904 u64 orig_search_start = search_start;
Chris Masonfec577f2007-02-26 10:40:21 -0500905 int start_found;
Chris Mason5f39d392007-10-15 16:14:19 -0400906 struct extent_buffer *l;
Chris Mason9f5fae22007-03-20 14:38:32 -0400907 struct btrfs_root * root = orig_root->fs_info->extent_root;
Chris Masonf2458e12007-04-25 15:52:25 -0400908 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -0400909 u64 total_needed = num_bytes;
Chris Masone20d96d2007-03-22 12:13:20 -0400910 int level;
Chris Masonbe08c1b2007-05-03 09:06:49 -0400911 struct btrfs_block_group_cache *block_group;
Chris Masonbe744172007-05-06 10:15:01 -0400912 int full_scan = 0;
Chris Masonfbdc7622007-05-30 10:22:12 -0400913 int wrapped = 0;
Chris Masonf84a8b32007-11-06 10:26:29 -0500914 u64 cached_start;
Chris Masonfec577f2007-02-26 10:40:21 -0500915
Chris Masondb945352007-10-15 16:15:53 -0400916 WARN_ON(num_bytes < root->sectorsize);
Chris Masonb1a4d962007-04-04 15:27:52 -0400917 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
918
Chris Mason5f39d392007-10-15 16:14:19 -0400919 level = btrfs_header_level(root->node);
920
Chris Mason3e1ad542007-05-07 20:03:49 -0400921 if (search_end == (u64)-1)
Chris Masondb945352007-10-15 16:15:53 -0400922 search_end = btrfs_super_total_bytes(&info->super_copy);
923 if (hint_byte) {
924 block_group = btrfs_lookup_block_group(info, hint_byte);
Chris Masonbe744172007-05-06 10:15:01 -0400925 block_group = btrfs_find_block_group(root, block_group,
Chris Masondb945352007-10-15 16:15:53 -0400926 hint_byte, data, 1);
Chris Masonbe744172007-05-06 10:15:01 -0400927 } else {
928 block_group = btrfs_find_block_group(root,
929 trans->block_group, 0,
Chris Masonde428b62007-05-18 13:28:27 -0400930 data, 1);
Chris Masonbe744172007-05-06 10:15:01 -0400931 }
932
Chris Mason6702ed42007-08-07 16:15:09 -0400933 total_needed += empty_size;
Chris Masone0115992007-06-19 16:23:05 -0400934 path = btrfs_alloc_path();
935
Chris Masonbe744172007-05-06 10:15:01 -0400936check_failed:
Chris Masonf510cfe2007-10-15 16:14:48 -0400937 search_start = find_search_start(root, &block_group,
938 search_start, total_needed, data);
Chris Masonf84a8b32007-11-06 10:26:29 -0500939 cached_start = search_start;
Chris Mason5caf2a02007-04-02 11:20:42 -0400940 btrfs_init_path(path);
Chris Masonfec577f2007-02-26 10:40:21 -0500941 ins->objectid = search_start;
942 ins->offset = 0;
Chris Masonfec577f2007-02-26 10:40:21 -0500943 start_found = 0;
Chris Mason2cc58cf2007-08-27 16:49:44 -0400944 path->reada = 2;
Chris Masone37c9e62007-05-09 20:13:14 -0400945
Chris Mason5caf2a02007-04-02 11:20:42 -0400946 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
Chris Mason0f70abe2007-02-28 16:46:22 -0500947 if (ret < 0)
948 goto error;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500949
Chris Masone37c9e62007-05-09 20:13:14 -0400950 if (path->slots[0] > 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400951 path->slots[0]--;
Chris Masone37c9e62007-05-09 20:13:14 -0400952 }
953
Chris Mason5f39d392007-10-15 16:14:19 -0400954 l = path->nodes[0];
955 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
956
Chris Masone37c9e62007-05-09 20:13:14 -0400957 /*
958 * a rare case, go back one key if we hit a block group item
959 * instead of an extent item
960 */
961 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY &&
962 key.objectid + key.offset >= search_start) {
963 ins->objectid = key.objectid;
964 ins->offset = key.offset - 1;
965 btrfs_release_path(root, path);
966 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
967 if (ret < 0)
968 goto error;
969
970 if (path->slots[0] > 0) {
971 path->slots[0]--;
972 }
973 }
Chris Mason0579da42007-03-07 16:15:30 -0500974
Chris Masonfec577f2007-02-26 10:40:21 -0500975 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400976 l = path->nodes[0];
Chris Mason5caf2a02007-04-02 11:20:42 -0400977 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -0400978 if (slot >= btrfs_header_nritems(l)) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400979 ret = btrfs_next_leaf(root, path);
Chris Masonfec577f2007-02-26 10:40:21 -0500980 if (ret == 0)
981 continue;
Chris Mason0f70abe2007-02-28 16:46:22 -0500982 if (ret < 0)
983 goto error;
Chris Masone19caa52007-10-15 16:17:44 -0400984
985 search_start = max(search_start,
986 block_group->key.objectid);
Chris Masonfec577f2007-02-26 10:40:21 -0500987 if (!start_found) {
988 ins->objectid = search_start;
Chris Mason3e1ad542007-05-07 20:03:49 -0400989 ins->offset = search_end - search_start;
Chris Masonfec577f2007-02-26 10:40:21 -0500990 start_found = 1;
991 goto check_pending;
992 }
Chris Masondb945352007-10-15 16:15:53 -0400993 ins->objectid = last_byte > search_start ?
994 last_byte : search_start;
Chris Mason3e1ad542007-05-07 20:03:49 -0400995 ins->offset = search_end - ins->objectid;
Chris Masone19caa52007-10-15 16:17:44 -0400996 BUG_ON(ins->objectid >= search_end);
Chris Masonfec577f2007-02-26 10:40:21 -0500997 goto check_pending;
998 }
Chris Mason5f39d392007-10-15 16:14:19 -0400999 btrfs_item_key_to_cpu(l, &key, slot);
Chris Mason96b51792007-10-15 16:15:19 -04001000
Chris Masondb945352007-10-15 16:15:53 -04001001 if (key.objectid >= search_start && key.objectid > last_byte &&
Chris Masone37c9e62007-05-09 20:13:14 -04001002 start_found) {
Chris Masondb945352007-10-15 16:15:53 -04001003 if (last_byte < search_start)
1004 last_byte = search_start;
1005 hole_size = key.objectid - last_byte;
1006 if (hole_size >= num_bytes) {
1007 ins->objectid = last_byte;
Chris Masone37c9e62007-05-09 20:13:14 -04001008 ins->offset = hole_size;
1009 goto check_pending;
Chris Mason0579da42007-03-07 16:15:30 -05001010 }
Chris Masonfec577f2007-02-26 10:40:21 -05001011 }
Chris Mason96b51792007-10-15 16:15:19 -04001012 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY) {
1013 if (!start_found) {
Chris Masondb945352007-10-15 16:15:53 -04001014 last_byte = key.objectid;
Chris Mason96b51792007-10-15 16:15:19 -04001015 start_found = 1;
1016 }
Chris Masone37c9e62007-05-09 20:13:14 -04001017 goto next;
Chris Mason96b51792007-10-15 16:15:19 -04001018 }
1019
Chris Masone37c9e62007-05-09 20:13:14 -04001020
Chris Mason0579da42007-03-07 16:15:30 -05001021 start_found = 1;
Chris Masondb945352007-10-15 16:15:53 -04001022 last_byte = key.objectid + key.offset;
Chris Masonf510cfe2007-10-15 16:14:48 -04001023
Chris Masondb945352007-10-15 16:15:53 -04001024 if (!full_scan && last_byte >= block_group->key.objectid +
Chris Masonbe744172007-05-06 10:15:01 -04001025 block_group->key.offset) {
1026 btrfs_release_path(root, path);
1027 search_start = block_group->key.objectid +
Chris Masone19caa52007-10-15 16:17:44 -04001028 block_group->key.offset;
Chris Masonbe744172007-05-06 10:15:01 -04001029 goto new_group;
1030 }
Chris Mason9078a3e2007-04-26 16:46:15 -04001031next:
Chris Mason5caf2a02007-04-02 11:20:42 -04001032 path->slots[0]++;
Chris Masonde428b62007-05-18 13:28:27 -04001033 cond_resched();
Chris Masonfec577f2007-02-26 10:40:21 -05001034 }
Chris Masonfec577f2007-02-26 10:40:21 -05001035check_pending:
1036 /* we have to make sure we didn't find an extent that has already
1037 * been allocated by the map tree or the original allocation
1038 */
Chris Mason5caf2a02007-04-02 11:20:42 -04001039 btrfs_release_path(root, path);
Chris Masonfec577f2007-02-26 10:40:21 -05001040 BUG_ON(ins->objectid < search_start);
Chris Masone37c9e62007-05-09 20:13:14 -04001041
Chris Masondb945352007-10-15 16:15:53 -04001042 if (ins->objectid + num_bytes >= search_end)
Chris Masoncf675822007-09-17 11:00:51 -04001043 goto enospc;
1044
Chris Masone19caa52007-10-15 16:17:44 -04001045 if (!full_scan && ins->objectid + num_bytes >= block_group->
1046 key.objectid + block_group->key.offset) {
1047 search_start = block_group->key.objectid +
1048 block_group->key.offset;
1049 goto new_group;
1050 }
Chris Mason1a5bc162007-10-15 16:15:26 -04001051 if (test_range_bit(&info->extent_ins, ins->objectid,
Chris Masondb945352007-10-15 16:15:53 -04001052 ins->objectid + num_bytes -1, EXTENT_LOCKED, 0)) {
1053 search_start = ins->objectid + num_bytes;
Chris Mason1a5bc162007-10-15 16:15:26 -04001054 goto new_group;
1055 }
1056 if (test_range_bit(&info->pinned_extents, ins->objectid,
Chris Masondb945352007-10-15 16:15:53 -04001057 ins->objectid + num_bytes -1, EXTENT_DIRTY, 0)) {
1058 search_start = ins->objectid + num_bytes;
Chris Mason1a5bc162007-10-15 16:15:26 -04001059 goto new_group;
Chris Masonfec577f2007-02-26 10:40:21 -05001060 }
Chris Masondb945352007-10-15 16:15:53 -04001061 if (exclude_nr > 0 && (ins->objectid + num_bytes > exclude_start &&
Chris Masonf2654de2007-06-26 12:20:46 -04001062 ins->objectid < exclude_start + exclude_nr)) {
1063 search_start = exclude_start + exclude_nr;
1064 goto new_group;
1065 }
Chris Masone37c9e62007-05-09 20:13:14 -04001066 if (!data) {
Chris Mason5276aed2007-06-11 21:33:38 -04001067 block_group = btrfs_lookup_block_group(info, ins->objectid);
Chris Mason26b80032007-08-08 20:17:12 -04001068 if (block_group)
1069 trans->block_group = block_group;
Chris Masoncd1bc462007-04-27 10:08:34 -04001070 }
Chris Masondb945352007-10-15 16:15:53 -04001071 ins->offset = num_bytes;
Chris Mason5caf2a02007-04-02 11:20:42 -04001072 btrfs_free_path(path);
Chris Masonfec577f2007-02-26 10:40:21 -05001073 return 0;
Chris Masonbe744172007-05-06 10:15:01 -04001074
1075new_group:
Chris Masondb945352007-10-15 16:15:53 -04001076 if (search_start + num_bytes >= search_end) {
Chris Masoncf675822007-09-17 11:00:51 -04001077enospc:
Chris Masonbe744172007-05-06 10:15:01 -04001078 search_start = orig_search_start;
Chris Masonfbdc7622007-05-30 10:22:12 -04001079 if (full_scan) {
1080 ret = -ENOSPC;
1081 goto error;
1082 }
Chris Mason6702ed42007-08-07 16:15:09 -04001083 if (wrapped) {
1084 if (!full_scan)
1085 total_needed -= empty_size;
Chris Masonfbdc7622007-05-30 10:22:12 -04001086 full_scan = 1;
Chris Mason6702ed42007-08-07 16:15:09 -04001087 } else
Chris Masonfbdc7622007-05-30 10:22:12 -04001088 wrapped = 1;
Chris Masonbe744172007-05-06 10:15:01 -04001089 }
Chris Mason5276aed2007-06-11 21:33:38 -04001090 block_group = btrfs_lookup_block_group(info, search_start);
Chris Masonfbdc7622007-05-30 10:22:12 -04001091 cond_resched();
Chris Masonbe744172007-05-06 10:15:01 -04001092 if (!full_scan)
1093 block_group = btrfs_find_block_group(root, block_group,
Chris Masonde428b62007-05-18 13:28:27 -04001094 search_start, data, 0);
Chris Masonbe744172007-05-06 10:15:01 -04001095 goto check_failed;
1096
Chris Mason0f70abe2007-02-28 16:46:22 -05001097error:
Chris Mason5caf2a02007-04-02 11:20:42 -04001098 btrfs_release_path(root, path);
1099 btrfs_free_path(path);
Chris Mason0f70abe2007-02-28 16:46:22 -05001100 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05001101}
Chris Masonfec577f2007-02-26 10:40:21 -05001102/*
Chris Masonfec577f2007-02-26 10:40:21 -05001103 * finds a free extent and does all the dirty work required for allocation
1104 * returns the key for the extent through ins, and a tree buffer for
1105 * the first block of the extent through buf.
1106 *
1107 * returns 0 if everything worked, non-zero otherwise.
1108 */
Chris Mason4d775672007-04-20 20:23:12 -04001109int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
1110 struct btrfs_root *root, u64 owner,
Chris Masondb945352007-10-15 16:15:53 -04001111 u64 num_bytes, u64 empty_size, u64 hint_byte,
Chris Masonbe08c1b2007-05-03 09:06:49 -04001112 u64 search_end, struct btrfs_key *ins, int data)
Chris Masonfec577f2007-02-26 10:40:21 -05001113{
1114 int ret;
1115 int pending_ret;
Chris Masondb945352007-10-15 16:15:53 -04001116 u64 super_used, root_used;
Chris Masonfbdc7622007-05-30 10:22:12 -04001117 u64 search_start = 0;
Chris Mason1261ec42007-03-20 20:35:03 -04001118 struct btrfs_fs_info *info = root->fs_info;
1119 struct btrfs_root *extent_root = info->extent_root;
Chris Mason234b63a2007-03-13 10:46:10 -04001120 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -05001121
Chris Mason5f39d392007-10-15 16:14:19 -04001122 btrfs_set_stack_extent_refs(&extent_item, 1);
1123 btrfs_set_stack_extent_owner(&extent_item, owner);
Chris Masonfec577f2007-02-26 10:40:21 -05001124
Chris Masondb945352007-10-15 16:15:53 -04001125 WARN_ON(num_bytes < root->sectorsize);
1126 ret = find_free_extent(trans, root, num_bytes, empty_size,
1127 search_start, search_end, hint_byte, ins,
Chris Mason26b80032007-08-08 20:17:12 -04001128 trans->alloc_exclude_start,
1129 trans->alloc_exclude_nr, data);
Chris Masonccd467d2007-06-28 15:57:36 -04001130 BUG_ON(ret);
Chris Masonf2654de2007-06-26 12:20:46 -04001131 if (ret)
1132 return ret;
1133
Josef Bacik58176a92007-08-29 15:47:34 -04001134 /* block accounting for super block */
Chris Masondb945352007-10-15 16:15:53 -04001135 super_used = btrfs_super_bytes_used(&info->super_copy);
1136 btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes);
Chris Mason26b80032007-08-08 20:17:12 -04001137
Josef Bacik58176a92007-08-29 15:47:34 -04001138 /* block accounting for root item */
Chris Masondb945352007-10-15 16:15:53 -04001139 root_used = btrfs_root_used(&root->root_item);
1140 btrfs_set_root_used(&root->root_item, root_used + num_bytes);
Josef Bacik58176a92007-08-29 15:47:34 -04001141
Chris Masonf510cfe2007-10-15 16:14:48 -04001142 clear_extent_dirty(&root->fs_info->free_space_cache,
1143 ins->objectid, ins->objectid + ins->offset - 1,
1144 GFP_NOFS);
1145
Chris Mason26b80032007-08-08 20:17:12 -04001146 if (root == extent_root) {
Chris Mason1a5bc162007-10-15 16:15:26 -04001147 set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
1148 ins->objectid + ins->offset - 1,
1149 EXTENT_LOCKED, GFP_NOFS);
Chris Masone19caa52007-10-15 16:17:44 -04001150 WARN_ON(data == 1);
Chris Mason26b80032007-08-08 20:17:12 -04001151 goto update_block;
1152 }
1153
1154 WARN_ON(trans->alloc_exclude_nr);
1155 trans->alloc_exclude_start = ins->objectid;
1156 trans->alloc_exclude_nr = ins->offset;
Chris Masone089f052007-03-16 16:20:31 -04001157 ret = btrfs_insert_item(trans, extent_root, ins, &extent_item,
1158 sizeof(extent_item));
Chris Mason037e6392007-03-07 11:50:24 -05001159
Chris Mason26b80032007-08-08 20:17:12 -04001160 trans->alloc_exclude_start = 0;
1161 trans->alloc_exclude_nr = 0;
1162
Chris Masonccd467d2007-06-28 15:57:36 -04001163 BUG_ON(ret);
Chris Masone089f052007-03-16 16:20:31 -04001164 finish_current_insert(trans, extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -04001165 pending_ret = del_pending_extents(trans, extent_root);
Chris Masonf510cfe2007-10-15 16:14:48 -04001166
Chris Masone37c9e62007-05-09 20:13:14 -04001167 if (ret) {
Chris Mason037e6392007-03-07 11:50:24 -05001168 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -04001169 }
1170 if (pending_ret) {
Chris Mason037e6392007-03-07 11:50:24 -05001171 return pending_ret;
Chris Masone37c9e62007-05-09 20:13:14 -04001172 }
Chris Mason26b80032007-08-08 20:17:12 -04001173
1174update_block:
Chris Mason1e2677e2007-05-29 16:52:18 -04001175 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0,
1176 data);
Chris Masonfabb5682007-06-07 22:13:21 -04001177 BUG_ON(ret);
Chris Mason037e6392007-03-07 11:50:24 -05001178 return 0;
Chris Masonfec577f2007-02-26 10:40:21 -05001179}
1180
1181/*
1182 * helper function to allocate a block for a given tree
1183 * returns the tree buffer or NULL.
1184 */
Chris Mason5f39d392007-10-15 16:14:19 -04001185struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Chris Masondb945352007-10-15 16:15:53 -04001186 struct btrfs_root *root,
1187 u32 blocksize, u64 hint,
Chris Mason5f39d392007-10-15 16:14:19 -04001188 u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05001189{
Chris Masone2fa7222007-03-12 16:22:34 -04001190 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -05001191 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04001192 struct extent_buffer *buf;
Chris Masonfec577f2007-02-26 10:40:21 -05001193
Chris Mason4d775672007-04-20 20:23:12 -04001194 ret = btrfs_alloc_extent(trans, root, root->root_key.objectid,
Chris Masondb945352007-10-15 16:15:53 -04001195 blocksize, empty_size, hint,
1196 (u64)-1, &ins, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05001197 if (ret) {
Chris Mason54aa1f42007-06-22 14:16:25 -04001198 BUG_ON(ret > 0);
1199 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05001200 }
Chris Masondb945352007-10-15 16:15:53 -04001201 buf = btrfs_find_create_tree_block(root, ins.objectid, blocksize);
Chris Mason54aa1f42007-06-22 14:16:25 -04001202 if (!buf) {
Chris Masondb945352007-10-15 16:15:53 -04001203 btrfs_free_extent(trans, root, ins.objectid, blocksize, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001204 return ERR_PTR(-ENOMEM);
1205 }
Chris Mason5f39d392007-10-15 16:14:19 -04001206 btrfs_set_buffer_uptodate(buf);
1207 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
1208 buf->start + buf->len - 1, GFP_NOFS);
Chris Mason19c00dd2007-10-15 16:19:22 -04001209 set_extent_bits(&BTRFS_I(root->fs_info->btree_inode)->extent_tree,
1210 buf->start, buf->start + buf->len - 1,
1211 EXTENT_CSUM, GFP_NOFS);
1212 buf->flags |= EXTENT_CSUM;
Chris Mason6b800532007-10-15 16:17:34 -04001213 btrfs_set_buffer_defrag(buf);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001214 trans->blocks_used++;
Chris Masonfec577f2007-02-26 10:40:21 -05001215 return buf;
1216}
Chris Masona28ec192007-03-06 20:08:01 -05001217
Chris Mason6407bf62007-03-27 06:33:00 -04001218static int drop_leaf_ref(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001219 struct btrfs_root *root, struct extent_buffer *leaf)
Chris Mason6407bf62007-03-27 06:33:00 -04001220{
Chris Mason5f39d392007-10-15 16:14:19 -04001221 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -04001222 struct btrfs_file_extent_item *fi;
1223 int i;
1224 int nritems;
1225 int ret;
1226
Chris Mason5f39d392007-10-15 16:14:19 -04001227 BUG_ON(!btrfs_is_leaf(leaf));
1228 nritems = btrfs_header_nritems(leaf);
Chris Mason6407bf62007-03-27 06:33:00 -04001229 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04001230 u64 disk_bytenr;
Chris Mason5f39d392007-10-15 16:14:19 -04001231
1232 btrfs_item_key_to_cpu(leaf, &key, i);
1233 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason6407bf62007-03-27 06:33:00 -04001234 continue;
1235 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001236 if (btrfs_file_extent_type(leaf, fi) ==
1237 BTRFS_FILE_EXTENT_INLINE)
Chris Mason236454d2007-04-19 13:37:44 -04001238 continue;
Chris Mason6407bf62007-03-27 06:33:00 -04001239 /*
1240 * FIXME make sure to insert a trans record that
1241 * repeats the snapshot del on crash
1242 */
Chris Masondb945352007-10-15 16:15:53 -04001243 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
1244 if (disk_bytenr == 0)
Chris Mason3a686372007-05-24 13:35:57 -04001245 continue;
Chris Masondb945352007-10-15 16:15:53 -04001246 ret = btrfs_free_extent(trans, root, disk_bytenr,
1247 btrfs_file_extent_disk_num_bytes(leaf, fi), 0);
Chris Mason6407bf62007-03-27 06:33:00 -04001248 BUG_ON(ret);
1249 }
1250 return 0;
1251}
1252
Chris Masone0115992007-06-19 16:23:05 -04001253static void reada_walk_down(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -04001254 struct extent_buffer *node)
Chris Masone0115992007-06-19 16:23:05 -04001255{
1256 int i;
1257 u32 nritems;
Chris Masondb945352007-10-15 16:15:53 -04001258 u64 bytenr;
Chris Masone0115992007-06-19 16:23:05 -04001259 int ret;
1260 u32 refs;
Chris Masondb945352007-10-15 16:15:53 -04001261 int level;
1262 u32 blocksize;
Chris Masone0115992007-06-19 16:23:05 -04001263
Chris Mason5f39d392007-10-15 16:14:19 -04001264 nritems = btrfs_header_nritems(node);
Chris Masondb945352007-10-15 16:15:53 -04001265 level = btrfs_header_level(node);
Chris Masone0115992007-06-19 16:23:05 -04001266 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04001267 bytenr = btrfs_node_blockptr(node, i);
1268 blocksize = btrfs_level_size(root, level - 1);
1269 ret = lookup_extent_ref(NULL, root, bytenr, blocksize, &refs);
Chris Masone0115992007-06-19 16:23:05 -04001270 BUG_ON(ret);
1271 if (refs != 1)
1272 continue;
Chris Mason409eb952007-08-08 20:17:12 -04001273 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masondb945352007-10-15 16:15:53 -04001274 ret = readahead_tree_block(root, bytenr, blocksize);
Chris Mason409eb952007-08-08 20:17:12 -04001275 cond_resched();
1276 mutex_lock(&root->fs_info->fs_mutex);
Chris Masone0115992007-06-19 16:23:05 -04001277 if (ret)
1278 break;
1279 }
1280}
1281
Chris Mason9aca1d52007-03-13 11:09:37 -04001282/*
1283 * helper function for drop_snapshot, this walks down the tree dropping ref
1284 * counts as it goes.
1285 */
Chris Masone089f052007-03-16 16:20:31 -04001286static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
1287 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05001288{
Chris Mason5f39d392007-10-15 16:14:19 -04001289 struct extent_buffer *next;
1290 struct extent_buffer *cur;
Chris Masondb945352007-10-15 16:15:53 -04001291 u64 bytenr;
1292 u32 blocksize;
Chris Mason20524f02007-03-10 06:35:47 -05001293 int ret;
1294 u32 refs;
1295
Chris Mason5caf2a02007-04-02 11:20:42 -04001296 WARN_ON(*level < 0);
1297 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason5f39d392007-10-15 16:14:19 -04001298 ret = lookup_extent_ref(trans, root,
Chris Masondb945352007-10-15 16:15:53 -04001299 path->nodes[*level]->start,
1300 path->nodes[*level]->len, &refs);
Chris Mason20524f02007-03-10 06:35:47 -05001301 BUG_ON(ret);
1302 if (refs > 1)
1303 goto out;
Chris Masone0115992007-06-19 16:23:05 -04001304
Chris Mason9aca1d52007-03-13 11:09:37 -04001305 /*
1306 * walk down to the last node level and free all the leaves
1307 */
Chris Mason6407bf62007-03-27 06:33:00 -04001308 while(*level >= 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001309 WARN_ON(*level < 0);
1310 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason20524f02007-03-10 06:35:47 -05001311 cur = path->nodes[*level];
Chris Masone0115992007-06-19 16:23:05 -04001312
1313 if (*level > 0 && path->slots[*level] == 0)
Chris Mason5f39d392007-10-15 16:14:19 -04001314 reada_walk_down(root, cur);
Chris Masone0115992007-06-19 16:23:05 -04001315
Chris Mason5f39d392007-10-15 16:14:19 -04001316 if (btrfs_header_level(cur) != *level)
Chris Mason2c90e5d2007-04-02 10:50:19 -04001317 WARN_ON(1);
Chris Masone0115992007-06-19 16:23:05 -04001318
Chris Mason7518a232007-03-12 12:01:18 -04001319 if (path->slots[*level] >=
Chris Mason5f39d392007-10-15 16:14:19 -04001320 btrfs_header_nritems(cur))
Chris Mason20524f02007-03-10 06:35:47 -05001321 break;
Chris Mason6407bf62007-03-27 06:33:00 -04001322 if (*level == 0) {
1323 ret = drop_leaf_ref(trans, root, cur);
1324 BUG_ON(ret);
1325 break;
1326 }
Chris Masondb945352007-10-15 16:15:53 -04001327 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
1328 blocksize = btrfs_level_size(root, *level - 1);
1329 ret = lookup_extent_ref(trans, root, bytenr, blocksize, &refs);
Chris Mason6407bf62007-03-27 06:33:00 -04001330 BUG_ON(ret);
1331 if (refs != 1) {
Chris Mason20524f02007-03-10 06:35:47 -05001332 path->slots[*level]++;
Chris Masondb945352007-10-15 16:15:53 -04001333 ret = btrfs_free_extent(trans, root, bytenr,
1334 blocksize, 1);
Chris Mason20524f02007-03-10 06:35:47 -05001335 BUG_ON(ret);
1336 continue;
1337 }
Chris Masondb945352007-10-15 16:15:53 -04001338 next = btrfs_find_tree_block(root, bytenr, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -04001339 if (!next || !btrfs_buffer_uptodate(next)) {
1340 free_extent_buffer(next);
Chris Masone9d0b132007-08-10 14:06:19 -04001341 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masondb945352007-10-15 16:15:53 -04001342 next = read_tree_block(root, bytenr, blocksize);
Chris Masone9d0b132007-08-10 14:06:19 -04001343 mutex_lock(&root->fs_info->fs_mutex);
1344
1345 /* we dropped the lock, check one more time */
Chris Masondb945352007-10-15 16:15:53 -04001346 ret = lookup_extent_ref(trans, root, bytenr,
1347 blocksize, &refs);
Chris Masone9d0b132007-08-10 14:06:19 -04001348 BUG_ON(ret);
1349 if (refs != 1) {
1350 path->slots[*level]++;
Chris Mason5f39d392007-10-15 16:14:19 -04001351 free_extent_buffer(next);
Chris Masone9d0b132007-08-10 14:06:19 -04001352 ret = btrfs_free_extent(trans, root,
Chris Masondb945352007-10-15 16:15:53 -04001353 bytenr, blocksize, 1);
Chris Masone9d0b132007-08-10 14:06:19 -04001354 BUG_ON(ret);
1355 continue;
1356 }
1357 }
Chris Mason5caf2a02007-04-02 11:20:42 -04001358 WARN_ON(*level <= 0);
Chris Mason83e15a22007-03-12 09:03:27 -04001359 if (path->nodes[*level-1])
Chris Mason5f39d392007-10-15 16:14:19 -04001360 free_extent_buffer(path->nodes[*level-1]);
Chris Mason20524f02007-03-10 06:35:47 -05001361 path->nodes[*level-1] = next;
Chris Mason5f39d392007-10-15 16:14:19 -04001362 *level = btrfs_header_level(next);
Chris Mason20524f02007-03-10 06:35:47 -05001363 path->slots[*level] = 0;
1364 }
1365out:
Chris Mason5caf2a02007-04-02 11:20:42 -04001366 WARN_ON(*level < 0);
1367 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Masondb945352007-10-15 16:15:53 -04001368 ret = btrfs_free_extent(trans, root, path->nodes[*level]->start,
1369 path->nodes[*level]->len, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001370 free_extent_buffer(path->nodes[*level]);
Chris Mason20524f02007-03-10 06:35:47 -05001371 path->nodes[*level] = NULL;
1372 *level += 1;
1373 BUG_ON(ret);
1374 return 0;
1375}
1376
Chris Mason9aca1d52007-03-13 11:09:37 -04001377/*
1378 * helper for dropping snapshots. This walks back up the tree in the path
1379 * to find the first node higher up where we haven't yet gone through
1380 * all the slots
1381 */
Chris Masone089f052007-03-16 16:20:31 -04001382static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root
1383 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05001384{
1385 int i;
1386 int slot;
1387 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04001388 struct btrfs_root_item *root_item = &root->root_item;
1389
Chris Mason234b63a2007-03-13 10:46:10 -04001390 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Mason20524f02007-03-10 06:35:47 -05001391 slot = path->slots[i];
Chris Mason5f39d392007-10-15 16:14:19 -04001392 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
1393 struct extent_buffer *node;
1394 struct btrfs_disk_key disk_key;
1395 node = path->nodes[i];
Chris Mason20524f02007-03-10 06:35:47 -05001396 path->slots[i]++;
1397 *level = i;
Chris Mason9f3a7422007-08-07 15:52:19 -04001398 WARN_ON(*level == 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001399 btrfs_node_key(node, &disk_key, path->slots[i]);
Chris Mason9f3a7422007-08-07 15:52:19 -04001400 memcpy(&root_item->drop_progress,
Chris Mason5f39d392007-10-15 16:14:19 -04001401 &disk_key, sizeof(disk_key));
Chris Mason9f3a7422007-08-07 15:52:19 -04001402 root_item->drop_level = i;
Chris Mason20524f02007-03-10 06:35:47 -05001403 return 0;
1404 } else {
Chris Masone089f052007-03-16 16:20:31 -04001405 ret = btrfs_free_extent(trans, root,
Chris Masondb945352007-10-15 16:15:53 -04001406 path->nodes[*level]->start,
1407 path->nodes[*level]->len, 1);
Chris Mason6407bf62007-03-27 06:33:00 -04001408 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04001409 free_extent_buffer(path->nodes[*level]);
Chris Mason83e15a22007-03-12 09:03:27 -04001410 path->nodes[*level] = NULL;
Chris Mason20524f02007-03-10 06:35:47 -05001411 *level = i + 1;
Chris Mason20524f02007-03-10 06:35:47 -05001412 }
1413 }
1414 return 1;
1415}
1416
Chris Mason9aca1d52007-03-13 11:09:37 -04001417/*
1418 * drop the reference count on the tree rooted at 'snap'. This traverses
1419 * the tree freeing any blocks that have a ref count of zero after being
1420 * decremented.
1421 */
Chris Masone089f052007-03-16 16:20:31 -04001422int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason9f3a7422007-08-07 15:52:19 -04001423 *root)
Chris Mason20524f02007-03-10 06:35:47 -05001424{
Chris Mason3768f362007-03-13 16:47:54 -04001425 int ret = 0;
Chris Mason9aca1d52007-03-13 11:09:37 -04001426 int wret;
Chris Mason20524f02007-03-10 06:35:47 -05001427 int level;
Chris Mason5caf2a02007-04-02 11:20:42 -04001428 struct btrfs_path *path;
Chris Mason20524f02007-03-10 06:35:47 -05001429 int i;
1430 int orig_level;
Chris Mason9f3a7422007-08-07 15:52:19 -04001431 struct btrfs_root_item *root_item = &root->root_item;
Chris Mason20524f02007-03-10 06:35:47 -05001432
Chris Mason5caf2a02007-04-02 11:20:42 -04001433 path = btrfs_alloc_path();
1434 BUG_ON(!path);
Chris Mason20524f02007-03-10 06:35:47 -05001435
Chris Mason5f39d392007-10-15 16:14:19 -04001436 level = btrfs_header_level(root->node);
Chris Mason20524f02007-03-10 06:35:47 -05001437 orig_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04001438 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
1439 path->nodes[level] = root->node;
Chris Masonf510cfe2007-10-15 16:14:48 -04001440 extent_buffer_get(root->node);
Chris Mason9f3a7422007-08-07 15:52:19 -04001441 path->slots[level] = 0;
1442 } else {
1443 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001444 struct btrfs_disk_key found_key;
1445 struct extent_buffer *node;
Chris Mason6702ed42007-08-07 16:15:09 -04001446
Chris Mason9f3a7422007-08-07 15:52:19 -04001447 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Chris Mason6702ed42007-08-07 16:15:09 -04001448 level = root_item->drop_level;
1449 path->lowest_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04001450 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason6702ed42007-08-07 16:15:09 -04001451 if (wret < 0) {
Chris Mason9f3a7422007-08-07 15:52:19 -04001452 ret = wret;
1453 goto out;
1454 }
Chris Mason5f39d392007-10-15 16:14:19 -04001455 node = path->nodes[level];
1456 btrfs_node_key(node, &found_key, path->slots[level]);
1457 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
1458 sizeof(found_key)));
Chris Mason9f3a7422007-08-07 15:52:19 -04001459 }
Chris Mason20524f02007-03-10 06:35:47 -05001460 while(1) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001461 wret = walk_down_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04001462 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05001463 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04001464 if (wret < 0)
1465 ret = wret;
1466
Chris Mason5caf2a02007-04-02 11:20:42 -04001467 wret = walk_up_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04001468 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05001469 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04001470 if (wret < 0)
1471 ret = wret;
Chris Mason409eb952007-08-08 20:17:12 -04001472 ret = -EAGAIN;
Chris Mason409eb952007-08-08 20:17:12 -04001473 break;
Chris Mason20524f02007-03-10 06:35:47 -05001474 }
Chris Mason83e15a22007-03-12 09:03:27 -04001475 for (i = 0; i <= orig_level; i++) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001476 if (path->nodes[i]) {
Chris Mason5f39d392007-10-15 16:14:19 -04001477 free_extent_buffer(path->nodes[i]);
Chris Mason0f827312007-10-15 16:18:56 -04001478 path->nodes[i] = NULL;
Chris Mason83e15a22007-03-12 09:03:27 -04001479 }
Chris Mason20524f02007-03-10 06:35:47 -05001480 }
Chris Mason9f3a7422007-08-07 15:52:19 -04001481out:
Chris Mason5caf2a02007-04-02 11:20:42 -04001482 btrfs_free_path(path);
Chris Mason9aca1d52007-03-13 11:09:37 -04001483 return ret;
Chris Mason20524f02007-03-10 06:35:47 -05001484}
Chris Mason9078a3e2007-04-26 16:46:15 -04001485
Chris Masonbe744172007-05-06 10:15:01 -04001486int btrfs_free_block_groups(struct btrfs_fs_info *info)
1487{
Chris Masonf510cfe2007-10-15 16:14:48 -04001488 u64 start;
1489 u64 end;
Yanb97f9202007-11-01 11:28:41 -04001490 u64 ptr;
Chris Mason96b51792007-10-15 16:15:19 -04001491 int ret;
Chris Mason96b51792007-10-15 16:15:19 -04001492 while(1) {
1493 ret = find_first_extent_bit(&info->block_group_cache, 0,
1494 &start, &end, (unsigned int)-1);
1495 if (ret)
1496 break;
Yanb97f9202007-11-01 11:28:41 -04001497 ret = get_state_private(&info->block_group_cache, start, &ptr);
1498 if (!ret)
1499 kfree((void *)(unsigned long)ptr);
Chris Mason96b51792007-10-15 16:15:19 -04001500 clear_extent_bits(&info->block_group_cache, start,
1501 end, (unsigned int)-1, GFP_NOFS);
1502 }
Chris Masone37c9e62007-05-09 20:13:14 -04001503 while(1) {
Chris Masonf510cfe2007-10-15 16:14:48 -04001504 ret = find_first_extent_bit(&info->free_space_cache, 0,
1505 &start, &end, EXTENT_DIRTY);
1506 if (ret)
Chris Masone37c9e62007-05-09 20:13:14 -04001507 break;
Chris Masonf510cfe2007-10-15 16:14:48 -04001508 clear_extent_dirty(&info->free_space_cache, start,
1509 end, GFP_NOFS);
Chris Masone37c9e62007-05-09 20:13:14 -04001510 }
Chris Masonbe744172007-05-06 10:15:01 -04001511 return 0;
1512}
1513
Chris Mason9078a3e2007-04-26 16:46:15 -04001514int btrfs_read_block_groups(struct btrfs_root *root)
1515{
1516 struct btrfs_path *path;
1517 int ret;
1518 int err = 0;
Chris Mason96b51792007-10-15 16:15:19 -04001519 int bit;
Chris Mason9078a3e2007-04-26 16:46:15 -04001520 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04001521 struct btrfs_fs_info *info = root->fs_info;
Chris Mason96b51792007-10-15 16:15:19 -04001522 struct extent_map_tree *block_group_cache;
Chris Mason9078a3e2007-04-26 16:46:15 -04001523 struct btrfs_key key;
1524 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -04001525 struct extent_buffer *leaf;
Chris Mason96b51792007-10-15 16:15:19 -04001526
1527 block_group_cache = &info->block_group_cache;
Chris Mason9078a3e2007-04-26 16:46:15 -04001528
Chris Masonbe744172007-05-06 10:15:01 -04001529 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04001530 key.objectid = 0;
Chris Masondb945352007-10-15 16:15:53 -04001531 key.offset = BTRFS_BLOCK_GROUP_SIZE;
Chris Mason9078a3e2007-04-26 16:46:15 -04001532 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
1533
1534 path = btrfs_alloc_path();
1535 if (!path)
1536 return -ENOMEM;
1537
1538 while(1) {
Chris Masonbe744172007-05-06 10:15:01 -04001539 ret = btrfs_search_slot(NULL, info->extent_root,
Chris Mason9078a3e2007-04-26 16:46:15 -04001540 &key, path, 0, 0);
1541 if (ret != 0) {
1542 err = ret;
1543 break;
1544 }
Chris Mason5f39d392007-10-15 16:14:19 -04001545 leaf = path->nodes[0];
1546 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason9078a3e2007-04-26 16:46:15 -04001547 cache = kmalloc(sizeof(*cache), GFP_NOFS);
1548 if (!cache) {
1549 err = -1;
1550 break;
1551 }
Chris Mason3e1ad542007-05-07 20:03:49 -04001552
Chris Mason5f39d392007-10-15 16:14:19 -04001553 read_extent_buffer(leaf, &cache->item,
1554 btrfs_item_ptr_offset(leaf, path->slots[0]),
1555 sizeof(cache->item));
Chris Mason9078a3e2007-04-26 16:46:15 -04001556 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Masone37c9e62007-05-09 20:13:14 -04001557 cache->cached = 0;
1558
Chris Mason9078a3e2007-04-26 16:46:15 -04001559 key.objectid = found_key.objectid + found_key.offset;
1560 btrfs_release_path(root, path);
Chris Mason5f39d392007-10-15 16:14:19 -04001561
Chris Masonf84a8b32007-11-06 10:26:29 -05001562 if (cache->item.flags & BTRFS_BLOCK_GROUP_MIXED) {
1563 bit = BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA;
1564 cache->data = BTRFS_BLOCK_GROUP_MIXED;
1565 } else if (cache->item.flags & BTRFS_BLOCK_GROUP_DATA) {
Chris Mason96b51792007-10-15 16:15:19 -04001566 bit = BLOCK_GROUP_DATA;
Chris Masonf84a8b32007-11-06 10:26:29 -05001567 cache->data = BTRFS_BLOCK_GROUP_DATA;
Chris Mason96b51792007-10-15 16:15:19 -04001568 } else {
1569 bit = BLOCK_GROUP_METADATA;
1570 cache->data = 0;
Chris Mason31f3c992007-04-30 15:25:45 -04001571 }
Chris Mason96b51792007-10-15 16:15:19 -04001572
1573 /* use EXTENT_LOCKED to prevent merging */
1574 set_extent_bits(block_group_cache, found_key.objectid,
1575 found_key.objectid + found_key.offset - 1,
1576 bit | EXTENT_LOCKED, GFP_NOFS);
1577 set_state_private(block_group_cache, found_key.objectid,
Jens Axboeae2f5412007-10-19 09:22:59 -04001578 (unsigned long)cache);
Chris Mason96b51792007-10-15 16:15:19 -04001579
Chris Mason9078a3e2007-04-26 16:46:15 -04001580 if (key.objectid >=
Chris Masondb945352007-10-15 16:15:53 -04001581 btrfs_super_total_bytes(&info->super_copy))
Chris Mason9078a3e2007-04-26 16:46:15 -04001582 break;
1583 }
1584
1585 btrfs_free_path(path);
1586 return 0;
1587}