blob: 7405bd5301af75db41c6f70c81cc2878c1b00ef7 [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 Mason257d0ce2007-11-07 21:08:16 -0500171 u64 cache_miss = 0;
Chris Masonf84a8b32007-11-06 10:26:29 -0500172 int wrapped = 0;
Chris Masone37c9e62007-05-09 20:13:14 -0400173
Chris Masone37c9e62007-05-09 20:13:14 -0400174again:
Chris Mason54aa1f42007-06-22 14:16:25 -0400175 ret = cache_block_group(root, cache);
176 if (ret)
177 goto out;
Chris Masonf84a8b32007-11-06 10:26:29 -0500178
Chris Masone19caa52007-10-15 16:17:44 -0400179 last = max(search_start, cache->key.objectid);
180
Chris Masone37c9e62007-05-09 20:13:14 -0400181 while(1) {
Chris Masonf510cfe2007-10-15 16:14:48 -0400182 ret = find_first_extent_bit(&root->fs_info->free_space_cache,
183 last, &start, &end, EXTENT_DIRTY);
Chris Masone19caa52007-10-15 16:17:44 -0400184 if (ret) {
Chris Mason257d0ce2007-11-07 21:08:16 -0500185 if (!cache_miss)
186 cache_miss = last;
Chris Masone19caa52007-10-15 16:17:44 -0400187 goto new_group;
188 }
Chris Masonf510cfe2007-10-15 16:14:48 -0400189
190 start = max(last, start);
191 last = end + 1;
Chris Mason257d0ce2007-11-07 21:08:16 -0500192 if (last - start < num) {
193 if (last == cache->key.objectid + cache->key.offset)
194 cache_miss = start;
Chris Masonf510cfe2007-10-15 16:14:48 -0400195 continue;
Chris Mason257d0ce2007-11-07 21:08:16 -0500196 }
197 if (data != BTRFS_BLOCK_GROUP_MIXED &&
198 start + num >= cache->key.objectid + cache->key.offset)
Chris Masone37c9e62007-05-09 20:13:14 -0400199 goto new_group;
Chris Masonf510cfe2007-10-15 16:14:48 -0400200 return start;
Chris Masone37c9e62007-05-09 20:13:14 -0400201 }
202out:
Chris Mason1a5bc162007-10-15 16:15:26 -0400203 return search_start;
Chris Masone37c9e62007-05-09 20:13:14 -0400204
205new_group:
Chris Masone19caa52007-10-15 16:17:44 -0400206 last = cache->key.objectid + cache->key.offset;
Chris Masonf84a8b32007-11-06 10:26:29 -0500207wrapped:
Chris Masone19caa52007-10-15 16:17:44 -0400208 cache = btrfs_lookup_block_group(root->fs_info, last);
Chris Masone37c9e62007-05-09 20:13:14 -0400209 if (!cache) {
Chris Masonf84a8b32007-11-06 10:26:29 -0500210 if (!wrapped) {
211 wrapped = 1;
212 last = search_start;
213 data = BTRFS_BLOCK_GROUP_MIXED;
214 goto wrapped;
215 }
Chris Mason1a5bc162007-10-15 16:15:26 -0400216 return search_start;
Chris Masone37c9e62007-05-09 20:13:14 -0400217 }
Chris Mason257d0ce2007-11-07 21:08:16 -0500218 if (cache_miss && !cache->cached) {
219 cache_block_group(root, cache);
220 last = cache_miss;
221
222 cache = btrfs_lookup_block_group(root->fs_info, last);
223 }
Chris Masone19caa52007-10-15 16:17:44 -0400224 cache = btrfs_find_block_group(root, cache, last, data, 0);
Chris Masone37c9e62007-05-09 20:13:14 -0400225 *cache_ret = cache;
Chris Mason257d0ce2007-11-07 21:08:16 -0500226 cache_miss = 0;
Chris Masone37c9e62007-05-09 20:13:14 -0400227 goto again;
228}
229
Chris Mason84f54cf2007-06-12 07:43:08 -0400230static u64 div_factor(u64 num, int factor)
231{
Chris Mason257d0ce2007-11-07 21:08:16 -0500232 if (factor == 10)
233 return num;
Chris Mason84f54cf2007-06-12 07:43:08 -0400234 num *= factor;
235 do_div(num, 10);
236 return num;
237}
238
Chris Mason31f3c992007-04-30 15:25:45 -0400239struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
240 struct btrfs_block_group_cache
Chris Masonbe744172007-05-06 10:15:01 -0400241 *hint, u64 search_start,
Chris Masonde428b62007-05-18 13:28:27 -0400242 int data, int owner)
Chris Masoncd1bc462007-04-27 10:08:34 -0400243{
Chris Mason96b51792007-10-15 16:15:19 -0400244 struct btrfs_block_group_cache *cache;
245 struct extent_map_tree *block_group_cache;
Chris Mason31f3c992007-04-30 15:25:45 -0400246 struct btrfs_block_group_cache *found_group = NULL;
Chris Masoncd1bc462007-04-27 10:08:34 -0400247 struct btrfs_fs_info *info = root->fs_info;
248 u64 used;
Chris Mason31f3c992007-04-30 15:25:45 -0400249 u64 last = 0;
250 u64 hint_last;
Chris Mason96b51792007-10-15 16:15:19 -0400251 u64 start;
252 u64 end;
253 u64 free_check;
254 u64 ptr;
255 int bit;
Chris Masoncd1bc462007-04-27 10:08:34 -0400256 int ret;
Chris Mason31f3c992007-04-30 15:25:45 -0400257 int full_search = 0;
Chris Masonde428b62007-05-18 13:28:27 -0400258 int factor = 8;
Chris Mason1e2677e2007-05-29 16:52:18 -0400259 int data_swap = 0;
Chris Masonde428b62007-05-18 13:28:27 -0400260
Chris Mason96b51792007-10-15 16:15:19 -0400261 block_group_cache = &info->block_group_cache;
262
Chris Masonde428b62007-05-18 13:28:27 -0400263 if (!owner)
Chris Masonf84a8b32007-11-06 10:26:29 -0500264 factor = 8;
Chris Masonbe744172007-05-06 10:15:01 -0400265
Chris Mason257d0ce2007-11-07 21:08:16 -0500266 if (data == BTRFS_BLOCK_GROUP_MIXED) {
Chris Masonf84a8b32007-11-06 10:26:29 -0500267 bit = BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA;
Chris Mason257d0ce2007-11-07 21:08:16 -0500268 factor = 10;
269 } else if (data)
Chris Mason96b51792007-10-15 16:15:19 -0400270 bit = BLOCK_GROUP_DATA;
271 else
272 bit = BLOCK_GROUP_METADATA;
Chris Masonbe744172007-05-06 10:15:01 -0400273
274 if (search_start) {
275 struct btrfs_block_group_cache *shint;
Chris Mason5276aed2007-06-11 21:33:38 -0400276 shint = btrfs_lookup_block_group(info, search_start);
Chris Masonf84a8b32007-11-06 10:26:29 -0500277 if (shint && (shint->data == data ||
278 shint->data == BTRFS_BLOCK_GROUP_MIXED)) {
Chris Masonbe744172007-05-06 10:15:01 -0400279 used = btrfs_block_group_used(&shint->item);
Chris Mason1a5bc162007-10-15 16:15:26 -0400280 if (used < div_factor(shint->key.offset, factor)) {
Chris Masonbe744172007-05-06 10:15:01 -0400281 return shint;
282 }
283 }
284 }
Chris Masonf84a8b32007-11-06 10:26:29 -0500285 if (hint && (hint->data == data ||
286 hint->data == BTRFS_BLOCK_GROUP_MIXED)) {
Chris Mason31f3c992007-04-30 15:25:45 -0400287 used = btrfs_block_group_used(&hint->item);
Chris Mason1a5bc162007-10-15 16:15:26 -0400288 if (used < div_factor(hint->key.offset, factor)) {
Chris Mason31f3c992007-04-30 15:25:45 -0400289 return hint;
290 }
Chris Masone19caa52007-10-15 16:17:44 -0400291 last = hint->key.objectid + hint->key.offset;
Chris Mason31f3c992007-04-30 15:25:45 -0400292 hint_last = last;
293 } else {
Chris Masone37c9e62007-05-09 20:13:14 -0400294 if (hint)
295 hint_last = max(hint->key.objectid, search_start);
296 else
297 hint_last = search_start;
298
299 last = hint_last;
Chris Mason31f3c992007-04-30 15:25:45 -0400300 }
Chris Mason31f3c992007-04-30 15:25:45 -0400301again:
Chris Masoncd1bc462007-04-27 10:08:34 -0400302 while(1) {
Chris Mason96b51792007-10-15 16:15:19 -0400303 ret = find_first_extent_bit(block_group_cache, last,
304 &start, &end, bit);
305 if (ret)
Chris Masoncd1bc462007-04-27 10:08:34 -0400306 break;
Chris Mason96b51792007-10-15 16:15:19 -0400307
308 ret = get_state_private(block_group_cache, start, &ptr);
309 if (ret)
310 break;
311
Jens Axboeae2f5412007-10-19 09:22:59 -0400312 cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
Chris Mason96b51792007-10-15 16:15:19 -0400313 last = cache->key.objectid + cache->key.offset;
314 used = btrfs_block_group_used(&cache->item);
315
316 if (full_search)
317 free_check = cache->key.offset;
318 else
319 free_check = div_factor(cache->key.offset, factor);
320
Chris Mason1a5bc162007-10-15 16:15:26 -0400321 if (used < free_check) {
Chris Mason96b51792007-10-15 16:15:19 -0400322 found_group = cache;
323 goto found;
Chris Masoncd1bc462007-04-27 10:08:34 -0400324 }
Chris Masonde428b62007-05-18 13:28:27 -0400325 cond_resched();
Chris Masoncd1bc462007-04-27 10:08:34 -0400326 }
Chris Mason31f3c992007-04-30 15:25:45 -0400327 if (!full_search) {
Chris Masonbe744172007-05-06 10:15:01 -0400328 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -0400329 full_search = 1;
330 goto again;
331 }
Chris Mason1e2677e2007-05-29 16:52:18 -0400332 if (!data_swap) {
Chris Mason1e2677e2007-05-29 16:52:18 -0400333 data_swap = 1;
Chris Mason96b51792007-10-15 16:15:19 -0400334 bit = BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA;
Chris Mason1e2677e2007-05-29 16:52:18 -0400335 last = search_start;
336 goto again;
337 }
Chris Masonbe744172007-05-06 10:15:01 -0400338found:
Chris Mason31f3c992007-04-30 15:25:45 -0400339 return found_group;
Chris Masoncd1bc462007-04-27 10:08:34 -0400340}
341
Chris Masonb18c6682007-04-17 13:26:50 -0400342int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
343 struct btrfs_root *root,
Chris Masondb945352007-10-15 16:15:53 -0400344 u64 bytenr, u64 num_bytes)
Chris Mason02217ed2007-03-02 16:08:05 -0500345{
Chris Mason5caf2a02007-04-02 11:20:42 -0400346 struct btrfs_path *path;
Chris Mason02217ed2007-03-02 16:08:05 -0500347 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400348 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400349 struct extent_buffer *l;
Chris Mason234b63a2007-03-13 10:46:10 -0400350 struct btrfs_extent_item *item;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400351 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500352
Chris Masondb945352007-10-15 16:15:53 -0400353 WARN_ON(num_bytes < root->sectorsize);
Chris Mason5caf2a02007-04-02 11:20:42 -0400354 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -0400355 if (!path)
356 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -0400357
Chris Masondb945352007-10-15 16:15:53 -0400358 key.objectid = bytenr;
Chris Mason62e27492007-03-15 12:56:47 -0400359 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masondb945352007-10-15 16:15:53 -0400360 key.offset = num_bytes;
Chris Mason5caf2a02007-04-02 11:20:42 -0400361 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -0400362 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400363 if (ret < 0)
364 return ret;
Chris Masona429e512007-04-18 16:15:28 -0400365 if (ret != 0) {
Chris Masona28ec192007-03-06 20:08:01 -0500366 BUG();
Chris Masona429e512007-04-18 16:15:28 -0400367 }
Chris Mason02217ed2007-03-02 16:08:05 -0500368 BUG_ON(ret != 0);
Chris Mason5f39d392007-10-15 16:14:19 -0400369 l = path->nodes[0];
Chris Mason5caf2a02007-04-02 11:20:42 -0400370 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400371 refs = btrfs_extent_refs(l, item);
372 btrfs_set_extent_refs(l, item, refs + 1);
Chris Mason5caf2a02007-04-02 11:20:42 -0400373 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masona28ec192007-03-06 20:08:01 -0500374
Chris Mason5caf2a02007-04-02 11:20:42 -0400375 btrfs_release_path(root->fs_info->extent_root, path);
376 btrfs_free_path(path);
Chris Mason9f5fae22007-03-20 14:38:32 -0400377 finish_current_insert(trans, root->fs_info->extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -0400378 del_pending_extents(trans, root->fs_info->extent_root);
Chris Mason02217ed2007-03-02 16:08:05 -0500379 return 0;
380}
381
Chris Masone9d0b132007-08-10 14:06:19 -0400382int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
383 struct btrfs_root *root)
384{
385 finish_current_insert(trans, root->fs_info->extent_root);
386 del_pending_extents(trans, root->fs_info->extent_root);
387 return 0;
388}
389
Chris Masonb18c6682007-04-17 13:26:50 -0400390static int lookup_extent_ref(struct btrfs_trans_handle *trans,
Chris Masondb945352007-10-15 16:15:53 -0400391 struct btrfs_root *root, u64 bytenr,
392 u64 num_bytes, u32 *refs)
Chris Masona28ec192007-03-06 20:08:01 -0500393{
Chris Mason5caf2a02007-04-02 11:20:42 -0400394 struct btrfs_path *path;
Chris Masona28ec192007-03-06 20:08:01 -0500395 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400396 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400397 struct extent_buffer *l;
Chris Mason234b63a2007-03-13 10:46:10 -0400398 struct btrfs_extent_item *item;
Chris Mason5caf2a02007-04-02 11:20:42 -0400399
Chris Masondb945352007-10-15 16:15:53 -0400400 WARN_ON(num_bytes < root->sectorsize);
Chris Mason5caf2a02007-04-02 11:20:42 -0400401 path = btrfs_alloc_path();
Chris Masondb945352007-10-15 16:15:53 -0400402 key.objectid = bytenr;
403 key.offset = num_bytes;
Chris Mason62e27492007-03-15 12:56:47 -0400404 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Mason5caf2a02007-04-02 11:20:42 -0400405 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -0400406 0, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400407 if (ret < 0)
408 goto out;
Chris Mason5f39d392007-10-15 16:14:19 -0400409 if (ret != 0) {
410 btrfs_print_leaf(root, path->nodes[0]);
Chris Masondb945352007-10-15 16:15:53 -0400411 printk("failed to find block number %Lu\n", bytenr);
Chris Masona28ec192007-03-06 20:08:01 -0500412 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -0400413 }
414 l = path->nodes[0];
Chris Mason5caf2a02007-04-02 11:20:42 -0400415 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400416 *refs = btrfs_extent_refs(l, item);
Chris Mason54aa1f42007-06-22 14:16:25 -0400417out:
Chris Mason5caf2a02007-04-02 11:20:42 -0400418 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -0500419 return 0;
420}
421
Chris Masonc5739bb2007-04-10 09:27:04 -0400422int btrfs_inc_root_ref(struct btrfs_trans_handle *trans,
423 struct btrfs_root *root)
424{
Chris Masondb945352007-10-15 16:15:53 -0400425 return btrfs_inc_extent_ref(trans, root, root->node->start,
426 root->node->len);
Chris Masonc5739bb2007-04-10 09:27:04 -0400427}
428
Chris Masone089f052007-03-16 16:20:31 -0400429int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400430 struct extent_buffer *buf)
Chris Mason02217ed2007-03-02 16:08:05 -0500431{
Chris Masondb945352007-10-15 16:15:53 -0400432 u64 bytenr;
Chris Mason5f39d392007-10-15 16:14:19 -0400433 u32 nritems;
434 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -0400435 struct btrfs_file_extent_item *fi;
Chris Mason02217ed2007-03-02 16:08:05 -0500436 int i;
Chris Masondb945352007-10-15 16:15:53 -0400437 int level;
Chris Mason6407bf62007-03-27 06:33:00 -0400438 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -0400439 int faili;
440 int err;
Chris Masona28ec192007-03-06 20:08:01 -0500441
Chris Mason3768f362007-03-13 16:47:54 -0400442 if (!root->ref_cows)
Chris Masona28ec192007-03-06 20:08:01 -0500443 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400444
Chris Masondb945352007-10-15 16:15:53 -0400445 level = btrfs_header_level(buf);
Chris Mason5f39d392007-10-15 16:14:19 -0400446 nritems = btrfs_header_nritems(buf);
447 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -0400448 if (level == 0) {
449 u64 disk_bytenr;
Chris Mason5f39d392007-10-15 16:14:19 -0400450 btrfs_item_key_to_cpu(buf, &key, i);
451 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason6407bf62007-03-27 06:33:00 -0400452 continue;
Chris Mason5f39d392007-10-15 16:14:19 -0400453 fi = btrfs_item_ptr(buf, i,
Chris Mason6407bf62007-03-27 06:33:00 -0400454 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400455 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason236454d2007-04-19 13:37:44 -0400456 BTRFS_FILE_EXTENT_INLINE)
457 continue;
Chris Masondb945352007-10-15 16:15:53 -0400458 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
459 if (disk_bytenr == 0)
Chris Mason3a686372007-05-24 13:35:57 -0400460 continue;
Chris Masondb945352007-10-15 16:15:53 -0400461 ret = btrfs_inc_extent_ref(trans, root, disk_bytenr,
462 btrfs_file_extent_disk_num_bytes(buf, fi));
Chris Mason54aa1f42007-06-22 14:16:25 -0400463 if (ret) {
464 faili = i;
465 goto fail;
466 }
Chris Mason6407bf62007-03-27 06:33:00 -0400467 } else {
Chris Masondb945352007-10-15 16:15:53 -0400468 bytenr = btrfs_node_blockptr(buf, i);
469 ret = btrfs_inc_extent_ref(trans, root, bytenr,
470 btrfs_level_size(root, level - 1));
Chris Mason54aa1f42007-06-22 14:16:25 -0400471 if (ret) {
472 faili = i;
473 goto fail;
474 }
Chris Mason6407bf62007-03-27 06:33:00 -0400475 }
Chris Mason02217ed2007-03-02 16:08:05 -0500476 }
477 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -0400478fail:
Chris Masonccd467d2007-06-28 15:57:36 -0400479 WARN_ON(1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400480 for (i =0; i < faili; i++) {
Chris Masondb945352007-10-15 16:15:53 -0400481 if (level == 0) {
482 u64 disk_bytenr;
Chris Mason5f39d392007-10-15 16:14:19 -0400483 btrfs_item_key_to_cpu(buf, &key, i);
484 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -0400485 continue;
Chris Mason5f39d392007-10-15 16:14:19 -0400486 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -0400487 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400488 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -0400489 BTRFS_FILE_EXTENT_INLINE)
490 continue;
Chris Masondb945352007-10-15 16:15:53 -0400491 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
492 if (disk_bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -0400493 continue;
Chris Masondb945352007-10-15 16:15:53 -0400494 err = btrfs_free_extent(trans, root, disk_bytenr,
495 btrfs_file_extent_disk_num_bytes(buf,
Chris Mason5f39d392007-10-15 16:14:19 -0400496 fi), 0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400497 BUG_ON(err);
498 } else {
Chris Masondb945352007-10-15 16:15:53 -0400499 bytenr = btrfs_node_blockptr(buf, i);
500 err = btrfs_free_extent(trans, root, bytenr,
501 btrfs_level_size(root, level - 1), 0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400502 BUG_ON(err);
503 }
504 }
505 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -0500506}
507
Chris Mason9078a3e2007-04-26 16:46:15 -0400508static int write_one_cache_group(struct btrfs_trans_handle *trans,
509 struct btrfs_root *root,
510 struct btrfs_path *path,
511 struct btrfs_block_group_cache *cache)
512{
513 int ret;
514 int pending_ret;
515 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -0400516 unsigned long bi;
517 struct extent_buffer *leaf;
Chris Mason9078a3e2007-04-26 16:46:15 -0400518
Chris Mason9078a3e2007-04-26 16:46:15 -0400519 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400520 if (ret < 0)
521 goto fail;
Chris Mason9078a3e2007-04-26 16:46:15 -0400522 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -0400523
524 leaf = path->nodes[0];
525 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
526 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
527 btrfs_mark_buffer_dirty(leaf);
Chris Mason9078a3e2007-04-26 16:46:15 -0400528 btrfs_release_path(extent_root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -0400529fail:
Chris Mason9078a3e2007-04-26 16:46:15 -0400530 finish_current_insert(trans, extent_root);
531 pending_ret = del_pending_extents(trans, extent_root);
532 if (ret)
533 return ret;
534 if (pending_ret)
535 return pending_ret;
536 return 0;
537
538}
539
Chris Mason96b51792007-10-15 16:15:19 -0400540int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
541 struct btrfs_root *root)
Chris Mason9078a3e2007-04-26 16:46:15 -0400542{
Chris Mason96b51792007-10-15 16:15:19 -0400543 struct extent_map_tree *block_group_cache;
544 struct btrfs_block_group_cache *cache;
Chris Mason9078a3e2007-04-26 16:46:15 -0400545 int ret;
546 int err = 0;
547 int werr = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -0400548 struct btrfs_path *path;
Chris Mason96b51792007-10-15 16:15:19 -0400549 u64 last = 0;
550 u64 start;
551 u64 end;
552 u64 ptr;
Chris Mason9078a3e2007-04-26 16:46:15 -0400553
Chris Mason96b51792007-10-15 16:15:19 -0400554 block_group_cache = &root->fs_info->block_group_cache;
Chris Mason9078a3e2007-04-26 16:46:15 -0400555 path = btrfs_alloc_path();
556 if (!path)
557 return -ENOMEM;
558
559 while(1) {
Chris Mason96b51792007-10-15 16:15:19 -0400560 ret = find_first_extent_bit(block_group_cache, last,
561 &start, &end, BLOCK_GROUP_DIRTY);
562 if (ret)
Chris Mason9078a3e2007-04-26 16:46:15 -0400563 break;
Chris Mason54aa1f42007-06-22 14:16:25 -0400564
Chris Mason96b51792007-10-15 16:15:19 -0400565 last = end + 1;
566 ret = get_state_private(block_group_cache, start, &ptr);
567 if (ret)
568 break;
569
Jens Axboeae2f5412007-10-19 09:22:59 -0400570 cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
Chris Mason96b51792007-10-15 16:15:19 -0400571 err = write_one_cache_group(trans, root,
572 path, cache);
573 /*
574 * if we fail to write the cache group, we want
575 * to keep it marked dirty in hopes that a later
576 * write will work
577 */
578 if (err) {
579 werr = err;
580 continue;
Chris Mason9078a3e2007-04-26 16:46:15 -0400581 }
Chris Mason96b51792007-10-15 16:15:19 -0400582 clear_extent_bits(block_group_cache, start, end,
583 BLOCK_GROUP_DIRTY, GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -0400584 }
585 btrfs_free_path(path);
586 return werr;
587}
588
589static int update_block_group(struct btrfs_trans_handle *trans,
590 struct btrfs_root *root,
Chris Masondb945352007-10-15 16:15:53 -0400591 u64 bytenr, u64 num_bytes, int alloc,
592 int mark_free, int data)
Chris Mason9078a3e2007-04-26 16:46:15 -0400593{
594 struct btrfs_block_group_cache *cache;
595 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -0400596 u64 total = num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -0400597 u64 old_val;
Chris Masondb945352007-10-15 16:15:53 -0400598 u64 byte_in_group;
Chris Mason96b51792007-10-15 16:15:19 -0400599 u64 start;
600 u64 end;
Chris Mason3e1ad542007-05-07 20:03:49 -0400601
Chris Mason9078a3e2007-04-26 16:46:15 -0400602 while(total) {
Chris Masondb945352007-10-15 16:15:53 -0400603 cache = btrfs_lookup_block_group(info, bytenr);
Chris Mason3e1ad542007-05-07 20:03:49 -0400604 if (!cache) {
Chris Mason9078a3e2007-04-26 16:46:15 -0400605 return -1;
Chris Masoncd1bc462007-04-27 10:08:34 -0400606 }
Chris Masondb945352007-10-15 16:15:53 -0400607 byte_in_group = bytenr - cache->key.objectid;
608 WARN_ON(byte_in_group > cache->key.offset);
Chris Mason96b51792007-10-15 16:15:19 -0400609 start = cache->key.objectid;
610 end = start + cache->key.offset - 1;
611 set_extent_bits(&info->block_group_cache, start, end,
612 BLOCK_GROUP_DIRTY, GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -0400613
614 old_val = btrfs_block_group_used(&cache->item);
Chris Masondb945352007-10-15 16:15:53 -0400615 num_bytes = min(total, cache->key.offset - byte_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -0400616 if (alloc) {
Chris Mason1e2677e2007-05-29 16:52:18 -0400617 if (cache->data != data &&
Chris Mason84f54cf2007-06-12 07:43:08 -0400618 old_val < (cache->key.offset >> 1)) {
Chris Mason96b51792007-10-15 16:15:19 -0400619 int bit_to_clear;
620 int bit_to_set;
Chris Mason96b51792007-10-15 16:15:19 -0400621 cache->data = data;
Chris Mason1e2677e2007-05-29 16:52:18 -0400622 if (data) {
Yanb97f9202007-11-01 11:28:41 -0400623 bit_to_clear = BLOCK_GROUP_METADATA;
624 bit_to_set = BLOCK_GROUP_DATA;
Chris Masonf84a8b32007-11-06 10:26:29 -0500625 cache->item.flags &=
626 ~BTRFS_BLOCK_GROUP_MIXED;
Chris Mason1e2677e2007-05-29 16:52:18 -0400627 cache->item.flags |=
628 BTRFS_BLOCK_GROUP_DATA;
629 } else {
Yanb97f9202007-11-01 11:28:41 -0400630 bit_to_clear = BLOCK_GROUP_DATA;
631 bit_to_set = BLOCK_GROUP_METADATA;
Chris Mason1e2677e2007-05-29 16:52:18 -0400632 cache->item.flags &=
Chris Masonf84a8b32007-11-06 10:26:29 -0500633 ~BTRFS_BLOCK_GROUP_MIXED;
634 cache->item.flags &=
Chris Mason1e2677e2007-05-29 16:52:18 -0400635 ~BTRFS_BLOCK_GROUP_DATA;
636 }
Chris Mason96b51792007-10-15 16:15:19 -0400637 clear_extent_bits(&info->block_group_cache,
638 start, end, bit_to_clear,
639 GFP_NOFS);
640 set_extent_bits(&info->block_group_cache,
641 start, end, bit_to_set,
642 GFP_NOFS);
Chris Masonf84a8b32007-11-06 10:26:29 -0500643 } else if (cache->data != data &&
644 cache->data != BTRFS_BLOCK_GROUP_MIXED) {
645 cache->data = BTRFS_BLOCK_GROUP_MIXED;
646 set_extent_bits(&info->block_group_cache,
647 start, end,
648 BLOCK_GROUP_DATA |
649 BLOCK_GROUP_METADATA,
650 GFP_NOFS);
Chris Mason1e2677e2007-05-29 16:52:18 -0400651 }
Chris Masondb945352007-10-15 16:15:53 -0400652 old_val += num_bytes;
Chris Masoncd1bc462007-04-27 10:08:34 -0400653 } else {
Chris Masondb945352007-10-15 16:15:53 -0400654 old_val -= num_bytes;
Chris Masonf510cfe2007-10-15 16:14:48 -0400655 if (mark_free) {
656 set_extent_dirty(&info->free_space_cache,
Chris Masondb945352007-10-15 16:15:53 -0400657 bytenr, bytenr + num_bytes - 1,
Chris Masonf510cfe2007-10-15 16:14:48 -0400658 GFP_NOFS);
Chris Masone37c9e62007-05-09 20:13:14 -0400659 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400660 }
Chris Mason9078a3e2007-04-26 16:46:15 -0400661 btrfs_set_block_group_used(&cache->item, old_val);
Chris Masondb945352007-10-15 16:15:53 -0400662 total -= num_bytes;
663 bytenr += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -0400664 }
665 return 0;
666}
667
Chris Mason1a5bc162007-10-15 16:15:26 -0400668int btrfs_copy_pinned(struct btrfs_root *root, struct extent_map_tree *copy)
Chris Masonccd467d2007-06-28 15:57:36 -0400669{
Chris Masonccd467d2007-06-28 15:57:36 -0400670 u64 last = 0;
Chris Mason1a5bc162007-10-15 16:15:26 -0400671 u64 start;
672 u64 end;
673 struct extent_map_tree *pinned_extents = &root->fs_info->pinned_extents;
Chris Masonccd467d2007-06-28 15:57:36 -0400674 int ret;
Chris Masonccd467d2007-06-28 15:57:36 -0400675
676 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -0400677 ret = find_first_extent_bit(pinned_extents, last,
678 &start, &end, EXTENT_DIRTY);
679 if (ret)
Chris Masonccd467d2007-06-28 15:57:36 -0400680 break;
Chris Mason1a5bc162007-10-15 16:15:26 -0400681 set_extent_dirty(copy, start, end, GFP_NOFS);
682 last = end + 1;
Chris Masonccd467d2007-06-28 15:57:36 -0400683 }
684 return 0;
685}
686
687int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
688 struct btrfs_root *root,
Chris Mason1a5bc162007-10-15 16:15:26 -0400689 struct extent_map_tree *unpin)
Chris Masona28ec192007-03-06 20:08:01 -0500690{
Chris Mason1a5bc162007-10-15 16:15:26 -0400691 u64 start;
692 u64 end;
Chris Masona28ec192007-03-06 20:08:01 -0500693 int ret;
Chris Mason1a5bc162007-10-15 16:15:26 -0400694 struct extent_map_tree *pinned_extents = &root->fs_info->pinned_extents;
Chris Masonf510cfe2007-10-15 16:14:48 -0400695 struct extent_map_tree *free_space_cache;
696
697 free_space_cache = &root->fs_info->free_space_cache;
Chris Masona28ec192007-03-06 20:08:01 -0500698
699 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -0400700 ret = find_first_extent_bit(unpin, 0, &start, &end,
701 EXTENT_DIRTY);
702 if (ret)
Chris Masona28ec192007-03-06 20:08:01 -0500703 break;
Chris Mason1a5bc162007-10-15 16:15:26 -0400704
705 clear_extent_dirty(pinned_extents, start, end,
706 GFP_NOFS);
707 clear_extent_dirty(unpin, start, end, GFP_NOFS);
708 set_extent_dirty(free_space_cache, start, end, GFP_NOFS);
Chris Masona28ec192007-03-06 20:08:01 -0500709 }
710 return 0;
711}
712
Chris Masone089f052007-03-16 16:20:31 -0400713static int finish_current_insert(struct btrfs_trans_handle *trans, struct
714 btrfs_root *extent_root)
Chris Mason037e6392007-03-07 11:50:24 -0500715{
Chris Masone2fa7222007-03-12 16:22:34 -0400716 struct btrfs_key ins;
Chris Mason234b63a2007-03-13 10:46:10 -0400717 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -0500718 int ret;
Chris Mason1a5bc162007-10-15 16:15:26 -0400719 int err = 0;
720 u64 start;
721 u64 end;
Chris Mason1261ec42007-03-20 20:35:03 -0400722 struct btrfs_fs_info *info = extent_root->fs_info;
Chris Mason037e6392007-03-07 11:50:24 -0500723
Chris Mason5f39d392007-10-15 16:14:19 -0400724 btrfs_set_stack_extent_refs(&extent_item, 1);
Chris Mason62e27492007-03-15 12:56:47 -0400725 btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
Chris Mason5f39d392007-10-15 16:14:19 -0400726 btrfs_set_stack_extent_owner(&extent_item,
727 extent_root->root_key.objectid);
Chris Mason037e6392007-03-07 11:50:24 -0500728
Chris Mason26b80032007-08-08 20:17:12 -0400729 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -0400730 ret = find_first_extent_bit(&info->extent_ins, 0, &start,
731 &end, EXTENT_LOCKED);
732 if (ret)
Chris Mason26b80032007-08-08 20:17:12 -0400733 break;
734
Chris Mason1a5bc162007-10-15 16:15:26 -0400735 ins.objectid = start;
736 ins.offset = end + 1 - start;
737 err = btrfs_insert_item(trans, extent_root, &ins,
738 &extent_item, sizeof(extent_item));
739 clear_extent_bits(&info->extent_ins, start, end, EXTENT_LOCKED,
740 GFP_NOFS);
Chris Mason037e6392007-03-07 11:50:24 -0500741 }
Chris Mason037e6392007-03-07 11:50:24 -0500742 return 0;
743}
744
Chris Masondb945352007-10-15 16:15:53 -0400745static int pin_down_bytes(struct btrfs_root *root, u64 bytenr, u32 num_bytes,
746 int pending)
Chris Masone20d96d2007-03-22 12:13:20 -0400747{
Chris Mason1a5bc162007-10-15 16:15:26 -0400748 int err = 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400749 struct extent_buffer *buf;
Chris Mason78fae272007-03-25 11:35:08 -0400750
Chris Masonf4b9aa82007-03-27 11:05:53 -0400751 if (!pending) {
Chris Masondb945352007-10-15 16:15:53 -0400752 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
Chris Mason5f39d392007-10-15 16:14:19 -0400753 if (buf) {
754 if (btrfs_buffer_uptodate(buf)) {
Chris Mason2c90e5d2007-04-02 10:50:19 -0400755 u64 transid =
756 root->fs_info->running_transaction->transid;
Chris Mason5f39d392007-10-15 16:14:19 -0400757 if (btrfs_header_generation(buf) == transid) {
758 free_extent_buffer(buf);
Yanc5492282007-11-06 10:25:25 -0500759 return 1;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400760 }
Chris Masonf4b9aa82007-03-27 11:05:53 -0400761 }
Chris Mason5f39d392007-10-15 16:14:19 -0400762 free_extent_buffer(buf);
Chris Mason8ef97622007-03-26 10:15:30 -0400763 }
Chris Mason1a5bc162007-10-15 16:15:26 -0400764 set_extent_dirty(&root->fs_info->pinned_extents,
Chris Masondb945352007-10-15 16:15:53 -0400765 bytenr, bytenr + num_bytes - 1, GFP_NOFS);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400766 } else {
Chris Mason1a5bc162007-10-15 16:15:26 -0400767 set_extent_bits(&root->fs_info->pending_del,
Chris Masondb945352007-10-15 16:15:53 -0400768 bytenr, bytenr + num_bytes - 1,
769 EXTENT_LOCKED, GFP_NOFS);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400770 }
Chris Masonbe744172007-05-06 10:15:01 -0400771 BUG_ON(err < 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400772 return 0;
773}
774
Chris Masona28ec192007-03-06 20:08:01 -0500775/*
776 * remove an extent from the root, returns 0 on success
777 */
Chris Masone089f052007-03-16 16:20:31 -0400778static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masondb945352007-10-15 16:15:53 -0400779 *root, u64 bytenr, u64 num_bytes, int pin,
Chris Masone37c9e62007-05-09 20:13:14 -0400780 int mark_free)
Chris Masona28ec192007-03-06 20:08:01 -0500781{
Chris Mason5caf2a02007-04-02 11:20:42 -0400782 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -0400783 struct btrfs_key key;
Chris Mason1261ec42007-03-20 20:35:03 -0400784 struct btrfs_fs_info *info = root->fs_info;
785 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -0400786 struct extent_buffer *leaf;
Chris Masona28ec192007-03-06 20:08:01 -0500787 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400788 struct btrfs_extent_item *ei;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400789 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500790
Chris Masondb945352007-10-15 16:15:53 -0400791 key.objectid = bytenr;
Chris Mason62e27492007-03-15 12:56:47 -0400792 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masondb945352007-10-15 16:15:53 -0400793 key.offset = num_bytes;
Chris Masona28ec192007-03-06 20:08:01 -0500794
Chris Mason5caf2a02007-04-02 11:20:42 -0400795 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -0400796 if (!path)
797 return -ENOMEM;
798
Chris Mason5caf2a02007-04-02 11:20:42 -0400799 ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400800 if (ret < 0)
801 return ret;
802 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -0400803
804 leaf = path->nodes[0];
805 ei = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason123abc82007-03-14 14:14:43 -0400806 struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400807 refs = btrfs_extent_refs(leaf, ei);
808 BUG_ON(refs == 0);
809 refs -= 1;
810 btrfs_set_extent_refs(leaf, ei, refs);
811 btrfs_mark_buffer_dirty(leaf);
812
Chris Masoncf27e1e2007-03-13 09:49:06 -0400813 if (refs == 0) {
Chris Masondb945352007-10-15 16:15:53 -0400814 u64 super_used;
815 u64 root_used;
Chris Mason78fae272007-03-25 11:35:08 -0400816
817 if (pin) {
Chris Masondb945352007-10-15 16:15:53 -0400818 ret = pin_down_bytes(root, bytenr, num_bytes, 0);
Yanc5492282007-11-06 10:25:25 -0500819 if (ret > 0)
820 mark_free = 1;
821 BUG_ON(ret < 0);
Chris Mason78fae272007-03-25 11:35:08 -0400822 }
823
Josef Bacik58176a92007-08-29 15:47:34 -0400824 /* block accounting for super block */
Chris Masondb945352007-10-15 16:15:53 -0400825 super_used = btrfs_super_bytes_used(&info->super_copy);
826 btrfs_set_super_bytes_used(&info->super_copy,
827 super_used - num_bytes);
Josef Bacik58176a92007-08-29 15:47:34 -0400828
829 /* block accounting for root item */
Chris Masondb945352007-10-15 16:15:53 -0400830 root_used = btrfs_root_used(&root->root_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400831 btrfs_set_root_used(&root->root_item,
Chris Masondb945352007-10-15 16:15:53 -0400832 root_used - num_bytes);
Josef Bacik58176a92007-08-29 15:47:34 -0400833
Chris Mason5caf2a02007-04-02 11:20:42 -0400834 ret = btrfs_del_item(trans, extent_root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -0400835 if (ret) {
836 return ret;
837 }
Chris Masondb945352007-10-15 16:15:53 -0400838 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
Chris Mason1e2677e2007-05-29 16:52:18 -0400839 mark_free, 0);
Chris Mason9078a3e2007-04-26 16:46:15 -0400840 BUG_ON(ret);
Chris Masona28ec192007-03-06 20:08:01 -0500841 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400842 btrfs_free_path(path);
Chris Masone089f052007-03-16 16:20:31 -0400843 finish_current_insert(trans, extent_root);
Chris Masona28ec192007-03-06 20:08:01 -0500844 return ret;
845}
846
847/*
Chris Masonfec577f2007-02-26 10:40:21 -0500848 * find all the blocks marked as pending in the radix tree and remove
849 * them from the extent map
850 */
Chris Masone089f052007-03-16 16:20:31 -0400851static int del_pending_extents(struct btrfs_trans_handle *trans, struct
852 btrfs_root *extent_root)
Chris Masonfec577f2007-02-26 10:40:21 -0500853{
854 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -0400855 int err = 0;
Chris Mason1a5bc162007-10-15 16:15:26 -0400856 u64 start;
857 u64 end;
858 struct extent_map_tree *pending_del;
859 struct extent_map_tree *pinned_extents;
Chris Mason8ef97622007-03-26 10:15:30 -0400860
Chris Mason1a5bc162007-10-15 16:15:26 -0400861 pending_del = &extent_root->fs_info->pending_del;
862 pinned_extents = &extent_root->fs_info->pinned_extents;
Chris Masonfec577f2007-02-26 10:40:21 -0500863
864 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -0400865 ret = find_first_extent_bit(pending_del, 0, &start, &end,
866 EXTENT_LOCKED);
867 if (ret)
Chris Masonfec577f2007-02-26 10:40:21 -0500868 break;
Chris Mason1a5bc162007-10-15 16:15:26 -0400869
870 set_extent_dirty(pinned_extents, start, end, GFP_NOFS);
871 clear_extent_bits(pending_del, start, end, EXTENT_LOCKED,
872 GFP_NOFS);
873 ret = __free_extent(trans, extent_root,
874 start, end + 1 - start, 0, 0);
875 if (ret)
876 err = ret;
Chris Masonfec577f2007-02-26 10:40:21 -0500877 }
Chris Masone20d96d2007-03-22 12:13:20 -0400878 return err;
Chris Masonfec577f2007-02-26 10:40:21 -0500879}
880
881/*
882 * remove an extent from the root, returns 0 on success
883 */
Chris Masone089f052007-03-16 16:20:31 -0400884int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masondb945352007-10-15 16:15:53 -0400885 *root, u64 bytenr, u64 num_bytes, int pin)
Chris Masonfec577f2007-02-26 10:40:21 -0500886{
Chris Mason9f5fae22007-03-20 14:38:32 -0400887 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Masonfec577f2007-02-26 10:40:21 -0500888 int pending_ret;
889 int ret;
Chris Masona28ec192007-03-06 20:08:01 -0500890
Chris Masondb945352007-10-15 16:15:53 -0400891 WARN_ON(num_bytes < root->sectorsize);
Chris Masona28ec192007-03-06 20:08:01 -0500892 if (root == extent_root) {
Chris Masondb945352007-10-15 16:15:53 -0400893 pin_down_bytes(root, bytenr, num_bytes, 1);
Chris Masona28ec192007-03-06 20:08:01 -0500894 return 0;
895 }
Chris Masondb945352007-10-15 16:15:53 -0400896 ret = __free_extent(trans, root, bytenr, num_bytes, pin, pin == 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400897 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
Chris Masonfec577f2007-02-26 10:40:21 -0500898 return ret ? ret : pending_ret;
899}
900
901/*
902 * walks the btree of allocated extents and find a hole of a given size.
903 * The key ins is changed to record the hole:
904 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -0400905 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -0500906 * ins->offset == number of blocks
907 * Any available blocks before search_start are skipped.
908 */
Chris Masone089f052007-03-16 16:20:31 -0400909static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masondb945352007-10-15 16:15:53 -0400910 *orig_root, u64 num_bytes, u64 empty_size,
911 u64 search_start, u64 search_end, u64 hint_byte,
Chris Masonf2654de2007-06-26 12:20:46 -0400912 struct btrfs_key *ins, u64 exclude_start,
913 u64 exclude_nr, int data)
Chris Masonfec577f2007-02-26 10:40:21 -0500914{
Chris Mason5caf2a02007-04-02 11:20:42 -0400915 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -0400916 struct btrfs_key key;
Chris Masonfec577f2007-02-26 10:40:21 -0500917 int ret;
918 u64 hole_size = 0;
919 int slot = 0;
Chris Masondb945352007-10-15 16:15:53 -0400920 u64 last_byte = 0;
Chris Masonbe744172007-05-06 10:15:01 -0400921 u64 orig_search_start = search_start;
Chris Masonfec577f2007-02-26 10:40:21 -0500922 int start_found;
Chris Mason5f39d392007-10-15 16:14:19 -0400923 struct extent_buffer *l;
Chris Mason9f5fae22007-03-20 14:38:32 -0400924 struct btrfs_root * root = orig_root->fs_info->extent_root;
Chris Masonf2458e12007-04-25 15:52:25 -0400925 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -0400926 u64 total_needed = num_bytes;
Chris Masone20d96d2007-03-22 12:13:20 -0400927 int level;
Chris Masonbe08c1b2007-05-03 09:06:49 -0400928 struct btrfs_block_group_cache *block_group;
Chris Masonbe744172007-05-06 10:15:01 -0400929 int full_scan = 0;
Chris Masonfbdc7622007-05-30 10:22:12 -0400930 int wrapped = 0;
Chris Masonf84a8b32007-11-06 10:26:29 -0500931 u64 cached_start;
Chris Masonfec577f2007-02-26 10:40:21 -0500932
Chris Masondb945352007-10-15 16:15:53 -0400933 WARN_ON(num_bytes < root->sectorsize);
Chris Masonb1a4d962007-04-04 15:27:52 -0400934 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
935
Chris Mason5f39d392007-10-15 16:14:19 -0400936 level = btrfs_header_level(root->node);
937
Chris Mason257d0ce2007-11-07 21:08:16 -0500938 if (num_bytes >= 96 * 1024 * 1024 && hint_byte) {
939 data = BTRFS_BLOCK_GROUP_MIXED;
940 }
941
Chris Mason3e1ad542007-05-07 20:03:49 -0400942 if (search_end == (u64)-1)
Chris Masondb945352007-10-15 16:15:53 -0400943 search_end = btrfs_super_total_bytes(&info->super_copy);
944 if (hint_byte) {
945 block_group = btrfs_lookup_block_group(info, hint_byte);
Chris Masonbe744172007-05-06 10:15:01 -0400946 block_group = btrfs_find_block_group(root, block_group,
Chris Masondb945352007-10-15 16:15:53 -0400947 hint_byte, data, 1);
Chris Masonbe744172007-05-06 10:15:01 -0400948 } else {
949 block_group = btrfs_find_block_group(root,
950 trans->block_group, 0,
Chris Masonde428b62007-05-18 13:28:27 -0400951 data, 1);
Chris Masonbe744172007-05-06 10:15:01 -0400952 }
953
Chris Mason6702ed42007-08-07 16:15:09 -0400954 total_needed += empty_size;
Chris Masone0115992007-06-19 16:23:05 -0400955 path = btrfs_alloc_path();
956
Chris Masonbe744172007-05-06 10:15:01 -0400957check_failed:
Chris Masonf510cfe2007-10-15 16:14:48 -0400958 search_start = find_search_start(root, &block_group,
959 search_start, total_needed, data);
Chris Masonf84a8b32007-11-06 10:26:29 -0500960 cached_start = search_start;
Chris Mason257d0ce2007-11-07 21:08:16 -0500961
Chris Mason5caf2a02007-04-02 11:20:42 -0400962 btrfs_init_path(path);
Chris Masonfec577f2007-02-26 10:40:21 -0500963 ins->objectid = search_start;
964 ins->offset = 0;
Chris Masonfec577f2007-02-26 10:40:21 -0500965 start_found = 0;
Chris Mason2cc58cf2007-08-27 16:49:44 -0400966 path->reada = 2;
Chris Masone37c9e62007-05-09 20:13:14 -0400967
Chris Mason5caf2a02007-04-02 11:20:42 -0400968 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
Chris Mason0f70abe2007-02-28 16:46:22 -0500969 if (ret < 0)
970 goto error;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500971
Chris Masone37c9e62007-05-09 20:13:14 -0400972 if (path->slots[0] > 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400973 path->slots[0]--;
Chris Masone37c9e62007-05-09 20:13:14 -0400974 }
975
Chris Mason5f39d392007-10-15 16:14:19 -0400976 l = path->nodes[0];
977 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
978
Chris Masone37c9e62007-05-09 20:13:14 -0400979 /*
980 * a rare case, go back one key if we hit a block group item
981 * instead of an extent item
982 */
983 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY &&
984 key.objectid + key.offset >= search_start) {
985 ins->objectid = key.objectid;
986 ins->offset = key.offset - 1;
987 btrfs_release_path(root, path);
988 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
989 if (ret < 0)
990 goto error;
991
992 if (path->slots[0] > 0) {
993 path->slots[0]--;
994 }
995 }
Chris Mason0579da42007-03-07 16:15:30 -0500996
Chris Masonfec577f2007-02-26 10:40:21 -0500997 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400998 l = path->nodes[0];
Chris Mason5caf2a02007-04-02 11:20:42 -0400999 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04001000 if (slot >= btrfs_header_nritems(l)) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001001 ret = btrfs_next_leaf(root, path);
Chris Masonfec577f2007-02-26 10:40:21 -05001002 if (ret == 0)
1003 continue;
Chris Mason0f70abe2007-02-28 16:46:22 -05001004 if (ret < 0)
1005 goto error;
Chris Masone19caa52007-10-15 16:17:44 -04001006
1007 search_start = max(search_start,
1008 block_group->key.objectid);
Chris Masonfec577f2007-02-26 10:40:21 -05001009 if (!start_found) {
1010 ins->objectid = search_start;
Chris Mason3e1ad542007-05-07 20:03:49 -04001011 ins->offset = search_end - search_start;
Chris Masonfec577f2007-02-26 10:40:21 -05001012 start_found = 1;
1013 goto check_pending;
1014 }
Chris Masondb945352007-10-15 16:15:53 -04001015 ins->objectid = last_byte > search_start ?
1016 last_byte : search_start;
Chris Mason3e1ad542007-05-07 20:03:49 -04001017 ins->offset = search_end - ins->objectid;
Chris Masone19caa52007-10-15 16:17:44 -04001018 BUG_ON(ins->objectid >= search_end);
Chris Masonfec577f2007-02-26 10:40:21 -05001019 goto check_pending;
1020 }
Chris Mason5f39d392007-10-15 16:14:19 -04001021 btrfs_item_key_to_cpu(l, &key, slot);
Chris Mason96b51792007-10-15 16:15:19 -04001022
Chris Masondb945352007-10-15 16:15:53 -04001023 if (key.objectid >= search_start && key.objectid > last_byte &&
Chris Masone37c9e62007-05-09 20:13:14 -04001024 start_found) {
Chris Masondb945352007-10-15 16:15:53 -04001025 if (last_byte < search_start)
1026 last_byte = search_start;
1027 hole_size = key.objectid - last_byte;
1028 if (hole_size >= num_bytes) {
1029 ins->objectid = last_byte;
Chris Masone37c9e62007-05-09 20:13:14 -04001030 ins->offset = hole_size;
1031 goto check_pending;
Chris Mason0579da42007-03-07 16:15:30 -05001032 }
Chris Masonfec577f2007-02-26 10:40:21 -05001033 }
Chris Mason96b51792007-10-15 16:15:19 -04001034 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY) {
1035 if (!start_found) {
Chris Masondb945352007-10-15 16:15:53 -04001036 last_byte = key.objectid;
Chris Mason96b51792007-10-15 16:15:19 -04001037 start_found = 1;
1038 }
Chris Masone37c9e62007-05-09 20:13:14 -04001039 goto next;
Chris Mason96b51792007-10-15 16:15:19 -04001040 }
1041
Chris Masone37c9e62007-05-09 20:13:14 -04001042
Chris Mason0579da42007-03-07 16:15:30 -05001043 start_found = 1;
Chris Masondb945352007-10-15 16:15:53 -04001044 last_byte = key.objectid + key.offset;
Chris Masonf510cfe2007-10-15 16:14:48 -04001045
Chris Mason257d0ce2007-11-07 21:08:16 -05001046 if (!full_scan && data != BTRFS_BLOCK_GROUP_MIXED &&
1047 last_byte >= block_group->key.objectid +
Chris Masonbe744172007-05-06 10:15:01 -04001048 block_group->key.offset) {
1049 btrfs_release_path(root, path);
1050 search_start = block_group->key.objectid +
Chris Masone19caa52007-10-15 16:17:44 -04001051 block_group->key.offset;
Chris Masonbe744172007-05-06 10:15:01 -04001052 goto new_group;
1053 }
Chris Mason9078a3e2007-04-26 16:46:15 -04001054next:
Chris Mason5caf2a02007-04-02 11:20:42 -04001055 path->slots[0]++;
Chris Masonde428b62007-05-18 13:28:27 -04001056 cond_resched();
Chris Masonfec577f2007-02-26 10:40:21 -05001057 }
Chris Masonfec577f2007-02-26 10:40:21 -05001058check_pending:
1059 /* we have to make sure we didn't find an extent that has already
1060 * been allocated by the map tree or the original allocation
1061 */
Chris Mason5caf2a02007-04-02 11:20:42 -04001062 btrfs_release_path(root, path);
Chris Masonfec577f2007-02-26 10:40:21 -05001063 BUG_ON(ins->objectid < search_start);
Chris Masone37c9e62007-05-09 20:13:14 -04001064
Chris Masondb945352007-10-15 16:15:53 -04001065 if (ins->objectid + num_bytes >= search_end)
Chris Masoncf675822007-09-17 11:00:51 -04001066 goto enospc;
1067
Chris Mason257d0ce2007-11-07 21:08:16 -05001068 if (!full_scan && data != BTRFS_BLOCK_GROUP_MIXED &&
1069 ins->objectid + num_bytes >= block_group->
Chris Masone19caa52007-10-15 16:17:44 -04001070 key.objectid + block_group->key.offset) {
1071 search_start = block_group->key.objectid +
1072 block_group->key.offset;
1073 goto new_group;
1074 }
Chris Mason1a5bc162007-10-15 16:15:26 -04001075 if (test_range_bit(&info->extent_ins, ins->objectid,
Chris Masondb945352007-10-15 16:15:53 -04001076 ins->objectid + num_bytes -1, EXTENT_LOCKED, 0)) {
1077 search_start = ins->objectid + num_bytes;
Chris Mason1a5bc162007-10-15 16:15:26 -04001078 goto new_group;
1079 }
1080 if (test_range_bit(&info->pinned_extents, ins->objectid,
Chris Masondb945352007-10-15 16:15:53 -04001081 ins->objectid + num_bytes -1, EXTENT_DIRTY, 0)) {
1082 search_start = ins->objectid + num_bytes;
Chris Mason1a5bc162007-10-15 16:15:26 -04001083 goto new_group;
Chris Masonfec577f2007-02-26 10:40:21 -05001084 }
Chris Masondb945352007-10-15 16:15:53 -04001085 if (exclude_nr > 0 && (ins->objectid + num_bytes > exclude_start &&
Chris Masonf2654de2007-06-26 12:20:46 -04001086 ins->objectid < exclude_start + exclude_nr)) {
1087 search_start = exclude_start + exclude_nr;
1088 goto new_group;
1089 }
Chris Masone37c9e62007-05-09 20:13:14 -04001090 if (!data) {
Chris Mason5276aed2007-06-11 21:33:38 -04001091 block_group = btrfs_lookup_block_group(info, ins->objectid);
Chris Mason26b80032007-08-08 20:17:12 -04001092 if (block_group)
1093 trans->block_group = block_group;
Chris Masoncd1bc462007-04-27 10:08:34 -04001094 }
Chris Masondb945352007-10-15 16:15:53 -04001095 ins->offset = num_bytes;
Chris Mason5caf2a02007-04-02 11:20:42 -04001096 btrfs_free_path(path);
Chris Masonfec577f2007-02-26 10:40:21 -05001097 return 0;
Chris Masonbe744172007-05-06 10:15:01 -04001098
1099new_group:
Chris Masondb945352007-10-15 16:15:53 -04001100 if (search_start + num_bytes >= search_end) {
Chris Masoncf675822007-09-17 11:00:51 -04001101enospc:
Chris Masonbe744172007-05-06 10:15:01 -04001102 search_start = orig_search_start;
Chris Masonfbdc7622007-05-30 10:22:12 -04001103 if (full_scan) {
1104 ret = -ENOSPC;
1105 goto error;
1106 }
Chris Mason6702ed42007-08-07 16:15:09 -04001107 if (wrapped) {
1108 if (!full_scan)
1109 total_needed -= empty_size;
Chris Masonfbdc7622007-05-30 10:22:12 -04001110 full_scan = 1;
Chris Mason6702ed42007-08-07 16:15:09 -04001111 } else
Chris Masonfbdc7622007-05-30 10:22:12 -04001112 wrapped = 1;
Chris Masonbe744172007-05-06 10:15:01 -04001113 }
Chris Mason5276aed2007-06-11 21:33:38 -04001114 block_group = btrfs_lookup_block_group(info, search_start);
Chris Masonfbdc7622007-05-30 10:22:12 -04001115 cond_resched();
Chris Masonbe744172007-05-06 10:15:01 -04001116 if (!full_scan)
1117 block_group = btrfs_find_block_group(root, block_group,
Chris Masonde428b62007-05-18 13:28:27 -04001118 search_start, data, 0);
Chris Masonbe744172007-05-06 10:15:01 -04001119 goto check_failed;
1120
Chris Mason0f70abe2007-02-28 16:46:22 -05001121error:
Chris Mason5caf2a02007-04-02 11:20:42 -04001122 btrfs_release_path(root, path);
1123 btrfs_free_path(path);
Chris Mason0f70abe2007-02-28 16:46:22 -05001124 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05001125}
Chris Masonfec577f2007-02-26 10:40:21 -05001126/*
Chris Masonfec577f2007-02-26 10:40:21 -05001127 * finds a free extent and does all the dirty work required for allocation
1128 * returns the key for the extent through ins, and a tree buffer for
1129 * the first block of the extent through buf.
1130 *
1131 * returns 0 if everything worked, non-zero otherwise.
1132 */
Chris Mason4d775672007-04-20 20:23:12 -04001133int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
1134 struct btrfs_root *root, u64 owner,
Chris Masondb945352007-10-15 16:15:53 -04001135 u64 num_bytes, u64 empty_size, u64 hint_byte,
Chris Masonbe08c1b2007-05-03 09:06:49 -04001136 u64 search_end, struct btrfs_key *ins, int data)
Chris Masonfec577f2007-02-26 10:40:21 -05001137{
1138 int ret;
1139 int pending_ret;
Chris Masondb945352007-10-15 16:15:53 -04001140 u64 super_used, root_used;
Chris Masonfbdc7622007-05-30 10:22:12 -04001141 u64 search_start = 0;
Chris Mason1261ec42007-03-20 20:35:03 -04001142 struct btrfs_fs_info *info = root->fs_info;
1143 struct btrfs_root *extent_root = info->extent_root;
Chris Mason234b63a2007-03-13 10:46:10 -04001144 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -05001145
Chris Mason5f39d392007-10-15 16:14:19 -04001146 btrfs_set_stack_extent_refs(&extent_item, 1);
1147 btrfs_set_stack_extent_owner(&extent_item, owner);
Chris Masonfec577f2007-02-26 10:40:21 -05001148
Chris Masondb945352007-10-15 16:15:53 -04001149 WARN_ON(num_bytes < root->sectorsize);
1150 ret = find_free_extent(trans, root, num_bytes, empty_size,
1151 search_start, search_end, hint_byte, ins,
Chris Mason26b80032007-08-08 20:17:12 -04001152 trans->alloc_exclude_start,
1153 trans->alloc_exclude_nr, data);
Chris Masonccd467d2007-06-28 15:57:36 -04001154 BUG_ON(ret);
Chris Masonf2654de2007-06-26 12:20:46 -04001155 if (ret)
1156 return ret;
1157
Josef Bacik58176a92007-08-29 15:47:34 -04001158 /* block accounting for super block */
Chris Masondb945352007-10-15 16:15:53 -04001159 super_used = btrfs_super_bytes_used(&info->super_copy);
1160 btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes);
Chris Mason26b80032007-08-08 20:17:12 -04001161
Josef Bacik58176a92007-08-29 15:47:34 -04001162 /* block accounting for root item */
Chris Masondb945352007-10-15 16:15:53 -04001163 root_used = btrfs_root_used(&root->root_item);
1164 btrfs_set_root_used(&root->root_item, root_used + num_bytes);
Josef Bacik58176a92007-08-29 15:47:34 -04001165
Chris Masonf510cfe2007-10-15 16:14:48 -04001166 clear_extent_dirty(&root->fs_info->free_space_cache,
1167 ins->objectid, ins->objectid + ins->offset - 1,
1168 GFP_NOFS);
1169
Chris Mason26b80032007-08-08 20:17:12 -04001170 if (root == extent_root) {
Chris Mason1a5bc162007-10-15 16:15:26 -04001171 set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
1172 ins->objectid + ins->offset - 1,
1173 EXTENT_LOCKED, GFP_NOFS);
Chris Masone19caa52007-10-15 16:17:44 -04001174 WARN_ON(data == 1);
Chris Mason26b80032007-08-08 20:17:12 -04001175 goto update_block;
1176 }
1177
1178 WARN_ON(trans->alloc_exclude_nr);
1179 trans->alloc_exclude_start = ins->objectid;
1180 trans->alloc_exclude_nr = ins->offset;
Chris Masone089f052007-03-16 16:20:31 -04001181 ret = btrfs_insert_item(trans, extent_root, ins, &extent_item,
1182 sizeof(extent_item));
Chris Mason037e6392007-03-07 11:50:24 -05001183
Chris Mason26b80032007-08-08 20:17:12 -04001184 trans->alloc_exclude_start = 0;
1185 trans->alloc_exclude_nr = 0;
1186
Chris Masonccd467d2007-06-28 15:57:36 -04001187 BUG_ON(ret);
Chris Masone089f052007-03-16 16:20:31 -04001188 finish_current_insert(trans, extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -04001189 pending_ret = del_pending_extents(trans, extent_root);
Chris Masonf510cfe2007-10-15 16:14:48 -04001190
Chris Masone37c9e62007-05-09 20:13:14 -04001191 if (ret) {
Chris Mason037e6392007-03-07 11:50:24 -05001192 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -04001193 }
1194 if (pending_ret) {
Chris Mason037e6392007-03-07 11:50:24 -05001195 return pending_ret;
Chris Masone37c9e62007-05-09 20:13:14 -04001196 }
Chris Mason26b80032007-08-08 20:17:12 -04001197
1198update_block:
Chris Mason1e2677e2007-05-29 16:52:18 -04001199 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0,
1200 data);
Chris Masonfabb5682007-06-07 22:13:21 -04001201 BUG_ON(ret);
Chris Mason037e6392007-03-07 11:50:24 -05001202 return 0;
Chris Masonfec577f2007-02-26 10:40:21 -05001203}
1204
1205/*
1206 * helper function to allocate a block for a given tree
1207 * returns the tree buffer or NULL.
1208 */
Chris Mason5f39d392007-10-15 16:14:19 -04001209struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Chris Masondb945352007-10-15 16:15:53 -04001210 struct btrfs_root *root,
1211 u32 blocksize, u64 hint,
Chris Mason5f39d392007-10-15 16:14:19 -04001212 u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05001213{
Chris Masone2fa7222007-03-12 16:22:34 -04001214 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -05001215 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04001216 struct extent_buffer *buf;
Chris Masonfec577f2007-02-26 10:40:21 -05001217
Chris Mason4d775672007-04-20 20:23:12 -04001218 ret = btrfs_alloc_extent(trans, root, root->root_key.objectid,
Chris Masondb945352007-10-15 16:15:53 -04001219 blocksize, empty_size, hint,
1220 (u64)-1, &ins, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05001221 if (ret) {
Chris Mason54aa1f42007-06-22 14:16:25 -04001222 BUG_ON(ret > 0);
1223 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05001224 }
Chris Masondb945352007-10-15 16:15:53 -04001225 buf = btrfs_find_create_tree_block(root, ins.objectid, blocksize);
Chris Mason54aa1f42007-06-22 14:16:25 -04001226 if (!buf) {
Chris Masondb945352007-10-15 16:15:53 -04001227 btrfs_free_extent(trans, root, ins.objectid, blocksize, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001228 return ERR_PTR(-ENOMEM);
1229 }
Chris Mason5f39d392007-10-15 16:14:19 -04001230 btrfs_set_buffer_uptodate(buf);
1231 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
1232 buf->start + buf->len - 1, GFP_NOFS);
Chris Mason19c00dd2007-10-15 16:19:22 -04001233 set_extent_bits(&BTRFS_I(root->fs_info->btree_inode)->extent_tree,
1234 buf->start, buf->start + buf->len - 1,
1235 EXTENT_CSUM, GFP_NOFS);
1236 buf->flags |= EXTENT_CSUM;
Chris Mason6b800532007-10-15 16:17:34 -04001237 btrfs_set_buffer_defrag(buf);
Chris Masond3c2fdc2007-09-17 10:58:06 -04001238 trans->blocks_used++;
Chris Masonfec577f2007-02-26 10:40:21 -05001239 return buf;
1240}
Chris Masona28ec192007-03-06 20:08:01 -05001241
Chris Mason6407bf62007-03-27 06:33:00 -04001242static int drop_leaf_ref(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001243 struct btrfs_root *root, struct extent_buffer *leaf)
Chris Mason6407bf62007-03-27 06:33:00 -04001244{
Chris Mason5f39d392007-10-15 16:14:19 -04001245 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -04001246 struct btrfs_file_extent_item *fi;
1247 int i;
1248 int nritems;
1249 int ret;
1250
Chris Mason5f39d392007-10-15 16:14:19 -04001251 BUG_ON(!btrfs_is_leaf(leaf));
1252 nritems = btrfs_header_nritems(leaf);
Chris Mason6407bf62007-03-27 06:33:00 -04001253 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04001254 u64 disk_bytenr;
Chris Mason5f39d392007-10-15 16:14:19 -04001255
1256 btrfs_item_key_to_cpu(leaf, &key, i);
1257 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason6407bf62007-03-27 06:33:00 -04001258 continue;
1259 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001260 if (btrfs_file_extent_type(leaf, fi) ==
1261 BTRFS_FILE_EXTENT_INLINE)
Chris Mason236454d2007-04-19 13:37:44 -04001262 continue;
Chris Mason6407bf62007-03-27 06:33:00 -04001263 /*
1264 * FIXME make sure to insert a trans record that
1265 * repeats the snapshot del on crash
1266 */
Chris Masondb945352007-10-15 16:15:53 -04001267 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
1268 if (disk_bytenr == 0)
Chris Mason3a686372007-05-24 13:35:57 -04001269 continue;
Chris Masondb945352007-10-15 16:15:53 -04001270 ret = btrfs_free_extent(trans, root, disk_bytenr,
1271 btrfs_file_extent_disk_num_bytes(leaf, fi), 0);
Chris Mason6407bf62007-03-27 06:33:00 -04001272 BUG_ON(ret);
1273 }
1274 return 0;
1275}
1276
Chris Masone0115992007-06-19 16:23:05 -04001277static void reada_walk_down(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -04001278 struct extent_buffer *node)
Chris Masone0115992007-06-19 16:23:05 -04001279{
1280 int i;
1281 u32 nritems;
Chris Masondb945352007-10-15 16:15:53 -04001282 u64 bytenr;
Chris Masone0115992007-06-19 16:23:05 -04001283 int ret;
1284 u32 refs;
Chris Masondb945352007-10-15 16:15:53 -04001285 int level;
1286 u32 blocksize;
Chris Masone0115992007-06-19 16:23:05 -04001287
Chris Mason5f39d392007-10-15 16:14:19 -04001288 nritems = btrfs_header_nritems(node);
Chris Masondb945352007-10-15 16:15:53 -04001289 level = btrfs_header_level(node);
Chris Masone0115992007-06-19 16:23:05 -04001290 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04001291 bytenr = btrfs_node_blockptr(node, i);
1292 blocksize = btrfs_level_size(root, level - 1);
1293 ret = lookup_extent_ref(NULL, root, bytenr, blocksize, &refs);
Chris Masone0115992007-06-19 16:23:05 -04001294 BUG_ON(ret);
1295 if (refs != 1)
1296 continue;
Chris Mason409eb952007-08-08 20:17:12 -04001297 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masondb945352007-10-15 16:15:53 -04001298 ret = readahead_tree_block(root, bytenr, blocksize);
Chris Mason409eb952007-08-08 20:17:12 -04001299 cond_resched();
1300 mutex_lock(&root->fs_info->fs_mutex);
Chris Masone0115992007-06-19 16:23:05 -04001301 if (ret)
1302 break;
1303 }
1304}
1305
Chris Mason9aca1d52007-03-13 11:09:37 -04001306/*
1307 * helper function for drop_snapshot, this walks down the tree dropping ref
1308 * counts as it goes.
1309 */
Chris Masone089f052007-03-16 16:20:31 -04001310static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
1311 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05001312{
Chris Mason5f39d392007-10-15 16:14:19 -04001313 struct extent_buffer *next;
1314 struct extent_buffer *cur;
Chris Masondb945352007-10-15 16:15:53 -04001315 u64 bytenr;
1316 u32 blocksize;
Chris Mason20524f02007-03-10 06:35:47 -05001317 int ret;
1318 u32 refs;
1319
Chris Mason5caf2a02007-04-02 11:20:42 -04001320 WARN_ON(*level < 0);
1321 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason5f39d392007-10-15 16:14:19 -04001322 ret = lookup_extent_ref(trans, root,
Chris Masondb945352007-10-15 16:15:53 -04001323 path->nodes[*level]->start,
1324 path->nodes[*level]->len, &refs);
Chris Mason20524f02007-03-10 06:35:47 -05001325 BUG_ON(ret);
1326 if (refs > 1)
1327 goto out;
Chris Masone0115992007-06-19 16:23:05 -04001328
Chris Mason9aca1d52007-03-13 11:09:37 -04001329 /*
1330 * walk down to the last node level and free all the leaves
1331 */
Chris Mason6407bf62007-03-27 06:33:00 -04001332 while(*level >= 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001333 WARN_ON(*level < 0);
1334 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason20524f02007-03-10 06:35:47 -05001335 cur = path->nodes[*level];
Chris Masone0115992007-06-19 16:23:05 -04001336
1337 if (*level > 0 && path->slots[*level] == 0)
Chris Mason5f39d392007-10-15 16:14:19 -04001338 reada_walk_down(root, cur);
Chris Masone0115992007-06-19 16:23:05 -04001339
Chris Mason5f39d392007-10-15 16:14:19 -04001340 if (btrfs_header_level(cur) != *level)
Chris Mason2c90e5d2007-04-02 10:50:19 -04001341 WARN_ON(1);
Chris Masone0115992007-06-19 16:23:05 -04001342
Chris Mason7518a232007-03-12 12:01:18 -04001343 if (path->slots[*level] >=
Chris Mason5f39d392007-10-15 16:14:19 -04001344 btrfs_header_nritems(cur))
Chris Mason20524f02007-03-10 06:35:47 -05001345 break;
Chris Mason6407bf62007-03-27 06:33:00 -04001346 if (*level == 0) {
1347 ret = drop_leaf_ref(trans, root, cur);
1348 BUG_ON(ret);
1349 break;
1350 }
Chris Masondb945352007-10-15 16:15:53 -04001351 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
1352 blocksize = btrfs_level_size(root, *level - 1);
1353 ret = lookup_extent_ref(trans, root, bytenr, blocksize, &refs);
Chris Mason6407bf62007-03-27 06:33:00 -04001354 BUG_ON(ret);
1355 if (refs != 1) {
Chris Mason20524f02007-03-10 06:35:47 -05001356 path->slots[*level]++;
Chris Masondb945352007-10-15 16:15:53 -04001357 ret = btrfs_free_extent(trans, root, bytenr,
1358 blocksize, 1);
Chris Mason20524f02007-03-10 06:35:47 -05001359 BUG_ON(ret);
1360 continue;
1361 }
Chris Masondb945352007-10-15 16:15:53 -04001362 next = btrfs_find_tree_block(root, bytenr, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -04001363 if (!next || !btrfs_buffer_uptodate(next)) {
1364 free_extent_buffer(next);
Chris Masone9d0b132007-08-10 14:06:19 -04001365 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masondb945352007-10-15 16:15:53 -04001366 next = read_tree_block(root, bytenr, blocksize);
Chris Masone9d0b132007-08-10 14:06:19 -04001367 mutex_lock(&root->fs_info->fs_mutex);
1368
1369 /* we dropped the lock, check one more time */
Chris Masondb945352007-10-15 16:15:53 -04001370 ret = lookup_extent_ref(trans, root, bytenr,
1371 blocksize, &refs);
Chris Masone9d0b132007-08-10 14:06:19 -04001372 BUG_ON(ret);
1373 if (refs != 1) {
1374 path->slots[*level]++;
Chris Mason5f39d392007-10-15 16:14:19 -04001375 free_extent_buffer(next);
Chris Masone9d0b132007-08-10 14:06:19 -04001376 ret = btrfs_free_extent(trans, root,
Chris Masondb945352007-10-15 16:15:53 -04001377 bytenr, blocksize, 1);
Chris Masone9d0b132007-08-10 14:06:19 -04001378 BUG_ON(ret);
1379 continue;
1380 }
1381 }
Chris Mason5caf2a02007-04-02 11:20:42 -04001382 WARN_ON(*level <= 0);
Chris Mason83e15a22007-03-12 09:03:27 -04001383 if (path->nodes[*level-1])
Chris Mason5f39d392007-10-15 16:14:19 -04001384 free_extent_buffer(path->nodes[*level-1]);
Chris Mason20524f02007-03-10 06:35:47 -05001385 path->nodes[*level-1] = next;
Chris Mason5f39d392007-10-15 16:14:19 -04001386 *level = btrfs_header_level(next);
Chris Mason20524f02007-03-10 06:35:47 -05001387 path->slots[*level] = 0;
1388 }
1389out:
Chris Mason5caf2a02007-04-02 11:20:42 -04001390 WARN_ON(*level < 0);
1391 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Masondb945352007-10-15 16:15:53 -04001392 ret = btrfs_free_extent(trans, root, path->nodes[*level]->start,
1393 path->nodes[*level]->len, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001394 free_extent_buffer(path->nodes[*level]);
Chris Mason20524f02007-03-10 06:35:47 -05001395 path->nodes[*level] = NULL;
1396 *level += 1;
1397 BUG_ON(ret);
1398 return 0;
1399}
1400
Chris Mason9aca1d52007-03-13 11:09:37 -04001401/*
1402 * helper for dropping snapshots. This walks back up the tree in the path
1403 * to find the first node higher up where we haven't yet gone through
1404 * all the slots
1405 */
Chris Masone089f052007-03-16 16:20:31 -04001406static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root
1407 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05001408{
1409 int i;
1410 int slot;
1411 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04001412 struct btrfs_root_item *root_item = &root->root_item;
1413
Chris Mason234b63a2007-03-13 10:46:10 -04001414 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Mason20524f02007-03-10 06:35:47 -05001415 slot = path->slots[i];
Chris Mason5f39d392007-10-15 16:14:19 -04001416 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
1417 struct extent_buffer *node;
1418 struct btrfs_disk_key disk_key;
1419 node = path->nodes[i];
Chris Mason20524f02007-03-10 06:35:47 -05001420 path->slots[i]++;
1421 *level = i;
Chris Mason9f3a7422007-08-07 15:52:19 -04001422 WARN_ON(*level == 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001423 btrfs_node_key(node, &disk_key, path->slots[i]);
Chris Mason9f3a7422007-08-07 15:52:19 -04001424 memcpy(&root_item->drop_progress,
Chris Mason5f39d392007-10-15 16:14:19 -04001425 &disk_key, sizeof(disk_key));
Chris Mason9f3a7422007-08-07 15:52:19 -04001426 root_item->drop_level = i;
Chris Mason20524f02007-03-10 06:35:47 -05001427 return 0;
1428 } else {
Chris Masone089f052007-03-16 16:20:31 -04001429 ret = btrfs_free_extent(trans, root,
Chris Masondb945352007-10-15 16:15:53 -04001430 path->nodes[*level]->start,
1431 path->nodes[*level]->len, 1);
Chris Mason6407bf62007-03-27 06:33:00 -04001432 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04001433 free_extent_buffer(path->nodes[*level]);
Chris Mason83e15a22007-03-12 09:03:27 -04001434 path->nodes[*level] = NULL;
Chris Mason20524f02007-03-10 06:35:47 -05001435 *level = i + 1;
Chris Mason20524f02007-03-10 06:35:47 -05001436 }
1437 }
1438 return 1;
1439}
1440
Chris Mason9aca1d52007-03-13 11:09:37 -04001441/*
1442 * drop the reference count on the tree rooted at 'snap'. This traverses
1443 * the tree freeing any blocks that have a ref count of zero after being
1444 * decremented.
1445 */
Chris Masone089f052007-03-16 16:20:31 -04001446int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason9f3a7422007-08-07 15:52:19 -04001447 *root)
Chris Mason20524f02007-03-10 06:35:47 -05001448{
Chris Mason3768f362007-03-13 16:47:54 -04001449 int ret = 0;
Chris Mason9aca1d52007-03-13 11:09:37 -04001450 int wret;
Chris Mason20524f02007-03-10 06:35:47 -05001451 int level;
Chris Mason5caf2a02007-04-02 11:20:42 -04001452 struct btrfs_path *path;
Chris Mason20524f02007-03-10 06:35:47 -05001453 int i;
1454 int orig_level;
Chris Mason9f3a7422007-08-07 15:52:19 -04001455 struct btrfs_root_item *root_item = &root->root_item;
Chris Mason20524f02007-03-10 06:35:47 -05001456
Chris Mason5caf2a02007-04-02 11:20:42 -04001457 path = btrfs_alloc_path();
1458 BUG_ON(!path);
Chris Mason20524f02007-03-10 06:35:47 -05001459
Chris Mason5f39d392007-10-15 16:14:19 -04001460 level = btrfs_header_level(root->node);
Chris Mason20524f02007-03-10 06:35:47 -05001461 orig_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04001462 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
1463 path->nodes[level] = root->node;
Chris Masonf510cfe2007-10-15 16:14:48 -04001464 extent_buffer_get(root->node);
Chris Mason9f3a7422007-08-07 15:52:19 -04001465 path->slots[level] = 0;
1466 } else {
1467 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001468 struct btrfs_disk_key found_key;
1469 struct extent_buffer *node;
Chris Mason6702ed42007-08-07 16:15:09 -04001470
Chris Mason9f3a7422007-08-07 15:52:19 -04001471 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Chris Mason6702ed42007-08-07 16:15:09 -04001472 level = root_item->drop_level;
1473 path->lowest_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04001474 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason6702ed42007-08-07 16:15:09 -04001475 if (wret < 0) {
Chris Mason9f3a7422007-08-07 15:52:19 -04001476 ret = wret;
1477 goto out;
1478 }
Chris Mason5f39d392007-10-15 16:14:19 -04001479 node = path->nodes[level];
1480 btrfs_node_key(node, &found_key, path->slots[level]);
1481 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
1482 sizeof(found_key)));
Chris Mason9f3a7422007-08-07 15:52:19 -04001483 }
Chris Mason20524f02007-03-10 06:35:47 -05001484 while(1) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001485 wret = walk_down_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04001486 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05001487 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04001488 if (wret < 0)
1489 ret = wret;
1490
Chris Mason5caf2a02007-04-02 11:20:42 -04001491 wret = walk_up_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04001492 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05001493 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04001494 if (wret < 0)
1495 ret = wret;
Chris Mason409eb952007-08-08 20:17:12 -04001496 ret = -EAGAIN;
Chris Mason409eb952007-08-08 20:17:12 -04001497 break;
Chris Mason20524f02007-03-10 06:35:47 -05001498 }
Chris Mason83e15a22007-03-12 09:03:27 -04001499 for (i = 0; i <= orig_level; i++) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001500 if (path->nodes[i]) {
Chris Mason5f39d392007-10-15 16:14:19 -04001501 free_extent_buffer(path->nodes[i]);
Chris Mason0f827312007-10-15 16:18:56 -04001502 path->nodes[i] = NULL;
Chris Mason83e15a22007-03-12 09:03:27 -04001503 }
Chris Mason20524f02007-03-10 06:35:47 -05001504 }
Chris Mason9f3a7422007-08-07 15:52:19 -04001505out:
Chris Mason5caf2a02007-04-02 11:20:42 -04001506 btrfs_free_path(path);
Chris Mason9aca1d52007-03-13 11:09:37 -04001507 return ret;
Chris Mason20524f02007-03-10 06:35:47 -05001508}
Chris Mason9078a3e2007-04-26 16:46:15 -04001509
Chris Masonbe744172007-05-06 10:15:01 -04001510int btrfs_free_block_groups(struct btrfs_fs_info *info)
1511{
Chris Masonf510cfe2007-10-15 16:14:48 -04001512 u64 start;
1513 u64 end;
Yanb97f9202007-11-01 11:28:41 -04001514 u64 ptr;
Chris Mason96b51792007-10-15 16:15:19 -04001515 int ret;
Chris Mason96b51792007-10-15 16:15:19 -04001516 while(1) {
1517 ret = find_first_extent_bit(&info->block_group_cache, 0,
1518 &start, &end, (unsigned int)-1);
1519 if (ret)
1520 break;
Yanb97f9202007-11-01 11:28:41 -04001521 ret = get_state_private(&info->block_group_cache, start, &ptr);
1522 if (!ret)
1523 kfree((void *)(unsigned long)ptr);
Chris Mason96b51792007-10-15 16:15:19 -04001524 clear_extent_bits(&info->block_group_cache, start,
1525 end, (unsigned int)-1, GFP_NOFS);
1526 }
Chris Masone37c9e62007-05-09 20:13:14 -04001527 while(1) {
Chris Masonf510cfe2007-10-15 16:14:48 -04001528 ret = find_first_extent_bit(&info->free_space_cache, 0,
1529 &start, &end, EXTENT_DIRTY);
1530 if (ret)
Chris Masone37c9e62007-05-09 20:13:14 -04001531 break;
Chris Masonf510cfe2007-10-15 16:14:48 -04001532 clear_extent_dirty(&info->free_space_cache, start,
1533 end, GFP_NOFS);
Chris Masone37c9e62007-05-09 20:13:14 -04001534 }
Chris Masonbe744172007-05-06 10:15:01 -04001535 return 0;
1536}
1537
Chris Mason9078a3e2007-04-26 16:46:15 -04001538int btrfs_read_block_groups(struct btrfs_root *root)
1539{
1540 struct btrfs_path *path;
1541 int ret;
1542 int err = 0;
Chris Mason96b51792007-10-15 16:15:19 -04001543 int bit;
Chris Mason9078a3e2007-04-26 16:46:15 -04001544 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04001545 struct btrfs_fs_info *info = root->fs_info;
Chris Mason96b51792007-10-15 16:15:19 -04001546 struct extent_map_tree *block_group_cache;
Chris Mason9078a3e2007-04-26 16:46:15 -04001547 struct btrfs_key key;
1548 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -04001549 struct extent_buffer *leaf;
Chris Mason96b51792007-10-15 16:15:19 -04001550
1551 block_group_cache = &info->block_group_cache;
Chris Mason9078a3e2007-04-26 16:46:15 -04001552
Chris Masonbe744172007-05-06 10:15:01 -04001553 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04001554 key.objectid = 0;
Chris Masondb945352007-10-15 16:15:53 -04001555 key.offset = BTRFS_BLOCK_GROUP_SIZE;
Chris Mason9078a3e2007-04-26 16:46:15 -04001556 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
1557
1558 path = btrfs_alloc_path();
1559 if (!path)
1560 return -ENOMEM;
1561
1562 while(1) {
Chris Masonbe744172007-05-06 10:15:01 -04001563 ret = btrfs_search_slot(NULL, info->extent_root,
Chris Mason9078a3e2007-04-26 16:46:15 -04001564 &key, path, 0, 0);
1565 if (ret != 0) {
1566 err = ret;
1567 break;
1568 }
Chris Mason5f39d392007-10-15 16:14:19 -04001569 leaf = path->nodes[0];
1570 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason9078a3e2007-04-26 16:46:15 -04001571 cache = kmalloc(sizeof(*cache), GFP_NOFS);
1572 if (!cache) {
1573 err = -1;
1574 break;
1575 }
Chris Mason3e1ad542007-05-07 20:03:49 -04001576
Chris Mason5f39d392007-10-15 16:14:19 -04001577 read_extent_buffer(leaf, &cache->item,
1578 btrfs_item_ptr_offset(leaf, path->slots[0]),
1579 sizeof(cache->item));
Chris Mason9078a3e2007-04-26 16:46:15 -04001580 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Masone37c9e62007-05-09 20:13:14 -04001581 cache->cached = 0;
1582
Chris Mason9078a3e2007-04-26 16:46:15 -04001583 key.objectid = found_key.objectid + found_key.offset;
1584 btrfs_release_path(root, path);
Chris Mason5f39d392007-10-15 16:14:19 -04001585
Chris Masonf84a8b32007-11-06 10:26:29 -05001586 if (cache->item.flags & BTRFS_BLOCK_GROUP_MIXED) {
1587 bit = BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA;
1588 cache->data = BTRFS_BLOCK_GROUP_MIXED;
1589 } else if (cache->item.flags & BTRFS_BLOCK_GROUP_DATA) {
Chris Mason96b51792007-10-15 16:15:19 -04001590 bit = BLOCK_GROUP_DATA;
Chris Masonf84a8b32007-11-06 10:26:29 -05001591 cache->data = BTRFS_BLOCK_GROUP_DATA;
Chris Mason96b51792007-10-15 16:15:19 -04001592 } else {
1593 bit = BLOCK_GROUP_METADATA;
1594 cache->data = 0;
Chris Mason31f3c992007-04-30 15:25:45 -04001595 }
Chris Mason96b51792007-10-15 16:15:19 -04001596
1597 /* use EXTENT_LOCKED to prevent merging */
1598 set_extent_bits(block_group_cache, found_key.objectid,
1599 found_key.objectid + found_key.offset - 1,
1600 bit | EXTENT_LOCKED, GFP_NOFS);
1601 set_state_private(block_group_cache, found_key.objectid,
Jens Axboeae2f5412007-10-19 09:22:59 -04001602 (unsigned long)cache);
Chris Mason96b51792007-10-15 16:15:19 -04001603
Chris Mason9078a3e2007-04-26 16:46:15 -04001604 if (key.objectid >=
Chris Masondb945352007-10-15 16:15:53 -04001605 btrfs_super_total_bytes(&info->super_copy))
Chris Mason9078a3e2007-04-26 16:46:15 -04001606 break;
1607 }
1608
1609 btrfs_free_path(path);
1610 return 0;
1611}