blob: 6a7a9ba75fd9a95ff46dbb988f30d305e227ba21 [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;
Yan5cf66422007-11-16 14:57:09 -0500153 if (block_group->key.objectid <= bytenr && bytenr <
Chris Mason96b51792007-10-15 16:15:19 -0400154 block_group->key.objectid + block_group->key.offset)
155 return block_group;
Chris Masonbe744172007-05-06 10:15:01 -0400156 return NULL;
157}
Chris Masone37c9e62007-05-09 20:13:14 -0400158static u64 find_search_start(struct btrfs_root *root,
159 struct btrfs_block_group_cache **cache_ret,
Yan5e5745d2007-11-16 14:57:09 -0500160 u64 search_start, int num,
161 int data, int full_scan)
Chris Masone37c9e62007-05-09 20:13:14 -0400162{
Chris Masone37c9e62007-05-09 20:13:14 -0400163 int ret;
164 struct btrfs_block_group_cache *cache = *cache_ret;
Chris Masone19caa52007-10-15 16:17:44 -0400165 u64 last;
Chris Masonf510cfe2007-10-15 16:14:48 -0400166 u64 start = 0;
167 u64 end = 0;
Chris Mason257d0ce2007-11-07 21:08:16 -0500168 u64 cache_miss = 0;
Chris Masonf84a8b32007-11-06 10:26:29 -0500169 int wrapped = 0;
Chris Masone37c9e62007-05-09 20:13:14 -0400170
Chris Masone37c9e62007-05-09 20:13:14 -0400171again:
Chris Mason54aa1f42007-06-22 14:16:25 -0400172 ret = cache_block_group(root, cache);
173 if (ret)
174 goto out;
Chris Masonf84a8b32007-11-06 10:26:29 -0500175
Chris Masone19caa52007-10-15 16:17:44 -0400176 last = max(search_start, cache->key.objectid);
177
Chris Masone37c9e62007-05-09 20:13:14 -0400178 while(1) {
Chris Masonf510cfe2007-10-15 16:14:48 -0400179 ret = find_first_extent_bit(&root->fs_info->free_space_cache,
180 last, &start, &end, EXTENT_DIRTY);
Chris Masone19caa52007-10-15 16:17:44 -0400181 if (ret) {
Chris Mason257d0ce2007-11-07 21:08:16 -0500182 if (!cache_miss)
183 cache_miss = last;
Chris Masone19caa52007-10-15 16:17:44 -0400184 goto new_group;
185 }
Chris Masonf510cfe2007-10-15 16:14:48 -0400186
187 start = max(last, start);
188 last = end + 1;
Chris Mason257d0ce2007-11-07 21:08:16 -0500189 if (last - start < num) {
190 if (last == cache->key.objectid + cache->key.offset)
191 cache_miss = start;
Chris Masonf510cfe2007-10-15 16:14:48 -0400192 continue;
Chris Mason257d0ce2007-11-07 21:08:16 -0500193 }
194 if (data != BTRFS_BLOCK_GROUP_MIXED &&
Yan5cf66422007-11-16 14:57:09 -0500195 start + num > cache->key.objectid + cache->key.offset)
Chris Masone37c9e62007-05-09 20:13:14 -0400196 goto new_group;
Chris Masonf510cfe2007-10-15 16:14:48 -0400197 return start;
Chris Masone37c9e62007-05-09 20:13:14 -0400198 }
199out:
Chris Mason1a5bc1672007-10-15 16:15:26 -0400200 return search_start;
Chris Masone37c9e62007-05-09 20:13:14 -0400201
202new_group:
Chris Masone19caa52007-10-15 16:17:44 -0400203 last = cache->key.objectid + cache->key.offset;
Chris Masonf84a8b32007-11-06 10:26:29 -0500204wrapped:
Chris Masone19caa52007-10-15 16:17:44 -0400205 cache = btrfs_lookup_block_group(root->fs_info, last);
Chris Masone37c9e62007-05-09 20:13:14 -0400206 if (!cache) {
Chris Masonf84a8b32007-11-06 10:26:29 -0500207 if (!wrapped) {
208 wrapped = 1;
209 last = search_start;
210 data = BTRFS_BLOCK_GROUP_MIXED;
211 goto wrapped;
212 }
Chris Mason1a5bc1672007-10-15 16:15:26 -0400213 return search_start;
Chris Masone37c9e62007-05-09 20:13:14 -0400214 }
Chris Mason257d0ce2007-11-07 21:08:16 -0500215 if (cache_miss && !cache->cached) {
216 cache_block_group(root, cache);
217 last = cache_miss;
Chris Mason257d0ce2007-11-07 21:08:16 -0500218 cache = btrfs_lookup_block_group(root->fs_info, last);
219 }
Yan5e5745d2007-11-16 14:57:09 -0500220 if (!full_scan)
221 cache = btrfs_find_block_group(root, cache, last, data, 0);
Chris Masone37c9e62007-05-09 20:13:14 -0400222 *cache_ret = cache;
Chris Mason257d0ce2007-11-07 21:08:16 -0500223 cache_miss = 0;
Chris Masone37c9e62007-05-09 20:13:14 -0400224 goto again;
225}
226
Chris Mason84f54cf2007-06-12 07:43:08 -0400227static u64 div_factor(u64 num, int factor)
228{
Chris Mason257d0ce2007-11-07 21:08:16 -0500229 if (factor == 10)
230 return num;
Chris Mason84f54cf2007-06-12 07:43:08 -0400231 num *= factor;
232 do_div(num, 10);
233 return num;
234}
235
Chris Mason31f3c992007-04-30 15:25:45 -0400236struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
237 struct btrfs_block_group_cache
Chris Masonbe744172007-05-06 10:15:01 -0400238 *hint, u64 search_start,
Chris Masonde428b62007-05-18 13:28:27 -0400239 int data, int owner)
Chris Masoncd1bc462007-04-27 10:08:34 -0400240{
Chris Mason96b51792007-10-15 16:15:19 -0400241 struct btrfs_block_group_cache *cache;
242 struct extent_map_tree *block_group_cache;
Chris Mason31f3c992007-04-30 15:25:45 -0400243 struct btrfs_block_group_cache *found_group = NULL;
Chris Masoncd1bc462007-04-27 10:08:34 -0400244 struct btrfs_fs_info *info = root->fs_info;
245 u64 used;
Chris Mason31f3c992007-04-30 15:25:45 -0400246 u64 last = 0;
247 u64 hint_last;
Chris Mason96b51792007-10-15 16:15:19 -0400248 u64 start;
249 u64 end;
250 u64 free_check;
251 u64 ptr;
252 int bit;
Chris Masoncd1bc462007-04-27 10:08:34 -0400253 int ret;
Chris Mason31f3c992007-04-30 15:25:45 -0400254 int full_search = 0;
Chris Masonde428b62007-05-18 13:28:27 -0400255 int factor = 8;
Chris Mason1e2677e2007-05-29 16:52:18 -0400256 int data_swap = 0;
Chris Masonde428b62007-05-18 13:28:27 -0400257
Chris Mason96b51792007-10-15 16:15:19 -0400258 block_group_cache = &info->block_group_cache;
259
Chris Masonde428b62007-05-18 13:28:27 -0400260 if (!owner)
Chris Masonf84a8b32007-11-06 10:26:29 -0500261 factor = 8;
Chris Masonbe744172007-05-06 10:15:01 -0400262
Chris Mason257d0ce2007-11-07 21:08:16 -0500263 if (data == BTRFS_BLOCK_GROUP_MIXED) {
Chris Masonf84a8b32007-11-06 10:26:29 -0500264 bit = BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA;
Chris Mason257d0ce2007-11-07 21:08:16 -0500265 factor = 10;
266 } else if (data)
Chris Mason96b51792007-10-15 16:15:19 -0400267 bit = BLOCK_GROUP_DATA;
268 else
269 bit = BLOCK_GROUP_METADATA;
Chris Masonbe744172007-05-06 10:15:01 -0400270
271 if (search_start) {
272 struct btrfs_block_group_cache *shint;
Chris Mason5276aed2007-06-11 21:33:38 -0400273 shint = btrfs_lookup_block_group(info, search_start);
Chris Masonf84a8b32007-11-06 10:26:29 -0500274 if (shint && (shint->data == data ||
275 shint->data == BTRFS_BLOCK_GROUP_MIXED)) {
Chris Masonbe744172007-05-06 10:15:01 -0400276 used = btrfs_block_group_used(&shint->item);
Yan324ae4d2007-11-16 14:57:08 -0500277 if (used + shint->pinned <
278 div_factor(shint->key.offset, factor)) {
Chris Masonbe744172007-05-06 10:15:01 -0400279 return shint;
280 }
281 }
282 }
Chris Masonf84a8b32007-11-06 10:26:29 -0500283 if (hint && (hint->data == data ||
284 hint->data == BTRFS_BLOCK_GROUP_MIXED)) {
Chris Mason31f3c992007-04-30 15:25:45 -0400285 used = btrfs_block_group_used(&hint->item);
Yan324ae4d2007-11-16 14:57:08 -0500286 if (used + hint->pinned <
287 div_factor(hint->key.offset, factor)) {
Chris Mason31f3c992007-04-30 15:25:45 -0400288 return hint;
289 }
Chris Masone19caa52007-10-15 16:17:44 -0400290 last = hint->key.objectid + hint->key.offset;
Chris Mason31f3c992007-04-30 15:25:45 -0400291 hint_last = last;
292 } else {
Chris Masone37c9e62007-05-09 20:13:14 -0400293 if (hint)
294 hint_last = max(hint->key.objectid, search_start);
295 else
296 hint_last = search_start;
297
298 last = hint_last;
Chris Mason31f3c992007-04-30 15:25:45 -0400299 }
Chris Mason31f3c992007-04-30 15:25:45 -0400300again:
Chris Masoncd1bc462007-04-27 10:08:34 -0400301 while(1) {
Chris Mason96b51792007-10-15 16:15:19 -0400302 ret = find_first_extent_bit(block_group_cache, last,
303 &start, &end, bit);
304 if (ret)
Chris Masoncd1bc462007-04-27 10:08:34 -0400305 break;
Chris Mason96b51792007-10-15 16:15:19 -0400306
307 ret = get_state_private(block_group_cache, start, &ptr);
308 if (ret)
309 break;
310
Jens Axboeae2f5412007-10-19 09:22:59 -0400311 cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
Chris Mason96b51792007-10-15 16:15:19 -0400312 last = cache->key.objectid + cache->key.offset;
313 used = btrfs_block_group_used(&cache->item);
314
315 if (full_search)
316 free_check = cache->key.offset;
317 else
318 free_check = div_factor(cache->key.offset, factor);
Yan324ae4d2007-11-16 14:57:08 -0500319 if (used + cache->pinned < free_check) {
Chris Mason96b51792007-10-15 16:15:19 -0400320 found_group = cache;
321 goto found;
Chris Masoncd1bc462007-04-27 10:08:34 -0400322 }
Chris Masonde428b62007-05-18 13:28:27 -0400323 cond_resched();
Chris Masoncd1bc462007-04-27 10:08:34 -0400324 }
Chris Mason31f3c992007-04-30 15:25:45 -0400325 if (!full_search) {
Chris Masonbe744172007-05-06 10:15:01 -0400326 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -0400327 full_search = 1;
328 goto again;
329 }
Chris Mason1e2677e2007-05-29 16:52:18 -0400330 if (!data_swap) {
Chris Mason1e2677e2007-05-29 16:52:18 -0400331 data_swap = 1;
Chris Mason96b51792007-10-15 16:15:19 -0400332 bit = BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA;
Chris Mason1e2677e2007-05-29 16:52:18 -0400333 last = search_start;
334 goto again;
335 }
Chris Masonbe744172007-05-06 10:15:01 -0400336found:
Chris Mason31f3c992007-04-30 15:25:45 -0400337 return found_group;
Chris Masoncd1bc462007-04-27 10:08:34 -0400338}
339
Chris Masonb18c6682007-04-17 13:26:50 -0400340int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
341 struct btrfs_root *root,
Chris Masondb945352007-10-15 16:15:53 -0400342 u64 bytenr, u64 num_bytes)
Chris Mason02217ed2007-03-02 16:08:05 -0500343{
Chris Mason5caf2a02007-04-02 11:20:42 -0400344 struct btrfs_path *path;
Chris Mason02217ed2007-03-02 16:08:05 -0500345 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400346 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400347 struct extent_buffer *l;
Chris Mason234b63a2007-03-13 10:46:10 -0400348 struct btrfs_extent_item *item;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400349 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500350
Chris Masondb945352007-10-15 16:15:53 -0400351 WARN_ON(num_bytes < root->sectorsize);
Chris Mason5caf2a02007-04-02 11:20:42 -0400352 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -0400353 if (!path)
354 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -0400355
Chris Masondb945352007-10-15 16:15:53 -0400356 key.objectid = bytenr;
Chris Mason62e27492007-03-15 12:56:47 -0400357 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masondb945352007-10-15 16:15:53 -0400358 key.offset = num_bytes;
Chris Mason5caf2a02007-04-02 11:20:42 -0400359 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -0400360 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400361 if (ret < 0)
362 return ret;
Chris Masona429e512007-04-18 16:15:28 -0400363 if (ret != 0) {
Chris Masona28ec192007-03-06 20:08:01 -0500364 BUG();
Chris Masona429e512007-04-18 16:15:28 -0400365 }
Chris Mason02217ed2007-03-02 16:08:05 -0500366 BUG_ON(ret != 0);
Chris Mason5f39d392007-10-15 16:14:19 -0400367 l = path->nodes[0];
Chris Mason5caf2a02007-04-02 11:20:42 -0400368 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400369 refs = btrfs_extent_refs(l, item);
370 btrfs_set_extent_refs(l, item, refs + 1);
Chris Mason5caf2a02007-04-02 11:20:42 -0400371 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masona28ec192007-03-06 20:08:01 -0500372
Chris Mason5caf2a02007-04-02 11:20:42 -0400373 btrfs_release_path(root->fs_info->extent_root, path);
374 btrfs_free_path(path);
Chris Mason9f5fae22007-03-20 14:38:32 -0400375 finish_current_insert(trans, root->fs_info->extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -0400376 del_pending_extents(trans, root->fs_info->extent_root);
Chris Mason02217ed2007-03-02 16:08:05 -0500377 return 0;
378}
379
Chris Masone9d0b132007-08-10 14:06:19 -0400380int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
381 struct btrfs_root *root)
382{
383 finish_current_insert(trans, root->fs_info->extent_root);
384 del_pending_extents(trans, root->fs_info->extent_root);
385 return 0;
386}
387
Chris Masonb18c6682007-04-17 13:26:50 -0400388static int lookup_extent_ref(struct btrfs_trans_handle *trans,
Chris Masondb945352007-10-15 16:15:53 -0400389 struct btrfs_root *root, u64 bytenr,
390 u64 num_bytes, u32 *refs)
Chris Masona28ec192007-03-06 20:08:01 -0500391{
Chris Mason5caf2a02007-04-02 11:20:42 -0400392 struct btrfs_path *path;
Chris Masona28ec192007-03-06 20:08:01 -0500393 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400394 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400395 struct extent_buffer *l;
Chris Mason234b63a2007-03-13 10:46:10 -0400396 struct btrfs_extent_item *item;
Chris Mason5caf2a02007-04-02 11:20:42 -0400397
Chris Masondb945352007-10-15 16:15:53 -0400398 WARN_ON(num_bytes < root->sectorsize);
Chris Mason5caf2a02007-04-02 11:20:42 -0400399 path = btrfs_alloc_path();
Chris Masondb945352007-10-15 16:15:53 -0400400 key.objectid = bytenr;
401 key.offset = num_bytes;
Chris Mason62e27492007-03-15 12:56:47 -0400402 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Mason5caf2a02007-04-02 11:20:42 -0400403 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -0400404 0, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400405 if (ret < 0)
406 goto out;
Chris Mason5f39d392007-10-15 16:14:19 -0400407 if (ret != 0) {
408 btrfs_print_leaf(root, path->nodes[0]);
Chris Masondb945352007-10-15 16:15:53 -0400409 printk("failed to find block number %Lu\n", bytenr);
Chris Masona28ec192007-03-06 20:08:01 -0500410 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -0400411 }
412 l = path->nodes[0];
Chris Mason5caf2a02007-04-02 11:20:42 -0400413 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400414 *refs = btrfs_extent_refs(l, item);
Chris Mason54aa1f42007-06-22 14:16:25 -0400415out:
Chris Mason5caf2a02007-04-02 11:20:42 -0400416 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -0500417 return 0;
418}
419
Chris Masonc5739bb2007-04-10 09:27:04 -0400420int btrfs_inc_root_ref(struct btrfs_trans_handle *trans,
421 struct btrfs_root *root)
422{
Chris Masondb945352007-10-15 16:15:53 -0400423 return btrfs_inc_extent_ref(trans, root, root->node->start,
424 root->node->len);
Chris Masonc5739bb2007-04-10 09:27:04 -0400425}
426
Chris Masone089f052007-03-16 16:20:31 -0400427int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400428 struct extent_buffer *buf)
Chris Mason02217ed2007-03-02 16:08:05 -0500429{
Chris Masondb945352007-10-15 16:15:53 -0400430 u64 bytenr;
Chris Mason5f39d392007-10-15 16:14:19 -0400431 u32 nritems;
432 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -0400433 struct btrfs_file_extent_item *fi;
Chris Mason02217ed2007-03-02 16:08:05 -0500434 int i;
Chris Masondb945352007-10-15 16:15:53 -0400435 int level;
Chris Mason6407bf62007-03-27 06:33:00 -0400436 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -0400437 int faili;
438 int err;
Chris Masona28ec192007-03-06 20:08:01 -0500439
Chris Mason3768f362007-03-13 16:47:54 -0400440 if (!root->ref_cows)
Chris Masona28ec192007-03-06 20:08:01 -0500441 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400442
Chris Masondb945352007-10-15 16:15:53 -0400443 level = btrfs_header_level(buf);
Chris Mason5f39d392007-10-15 16:14:19 -0400444 nritems = btrfs_header_nritems(buf);
445 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -0400446 if (level == 0) {
447 u64 disk_bytenr;
Chris Mason5f39d392007-10-15 16:14:19 -0400448 btrfs_item_key_to_cpu(buf, &key, i);
449 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason6407bf62007-03-27 06:33:00 -0400450 continue;
Chris Mason5f39d392007-10-15 16:14:19 -0400451 fi = btrfs_item_ptr(buf, i,
Chris Mason6407bf62007-03-27 06:33:00 -0400452 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400453 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason236454df2007-04-19 13:37:44 -0400454 BTRFS_FILE_EXTENT_INLINE)
455 continue;
Chris Masondb945352007-10-15 16:15:53 -0400456 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
457 if (disk_bytenr == 0)
Chris Mason3a686372007-05-24 13:35:57 -0400458 continue;
Chris Masondb945352007-10-15 16:15:53 -0400459 ret = btrfs_inc_extent_ref(trans, root, disk_bytenr,
460 btrfs_file_extent_disk_num_bytes(buf, fi));
Chris Mason54aa1f42007-06-22 14:16:25 -0400461 if (ret) {
462 faili = i;
463 goto fail;
464 }
Chris Mason6407bf62007-03-27 06:33:00 -0400465 } else {
Chris Masondb945352007-10-15 16:15:53 -0400466 bytenr = btrfs_node_blockptr(buf, i);
467 ret = btrfs_inc_extent_ref(trans, root, bytenr,
468 btrfs_level_size(root, level - 1));
Chris Mason54aa1f42007-06-22 14:16:25 -0400469 if (ret) {
470 faili = i;
471 goto fail;
472 }
Chris Mason6407bf62007-03-27 06:33:00 -0400473 }
Chris Mason02217ed2007-03-02 16:08:05 -0500474 }
475 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -0400476fail:
Chris Masonccd467d2007-06-28 15:57:36 -0400477 WARN_ON(1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400478 for (i =0; i < faili; i++) {
Chris Masondb945352007-10-15 16:15:53 -0400479 if (level == 0) {
480 u64 disk_bytenr;
Chris Mason5f39d392007-10-15 16:14:19 -0400481 btrfs_item_key_to_cpu(buf, &key, i);
482 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -0400483 continue;
Chris Mason5f39d392007-10-15 16:14:19 -0400484 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -0400485 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400486 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -0400487 BTRFS_FILE_EXTENT_INLINE)
488 continue;
Chris Masondb945352007-10-15 16:15:53 -0400489 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
490 if (disk_bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -0400491 continue;
Chris Masondb945352007-10-15 16:15:53 -0400492 err = btrfs_free_extent(trans, root, disk_bytenr,
493 btrfs_file_extent_disk_num_bytes(buf,
Chris Mason5f39d392007-10-15 16:14:19 -0400494 fi), 0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400495 BUG_ON(err);
496 } else {
Chris Masondb945352007-10-15 16:15:53 -0400497 bytenr = btrfs_node_blockptr(buf, i);
498 err = btrfs_free_extent(trans, root, bytenr,
499 btrfs_level_size(root, level - 1), 0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400500 BUG_ON(err);
501 }
502 }
503 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -0500504}
505
Chris Mason9078a3e2007-04-26 16:46:15 -0400506static int write_one_cache_group(struct btrfs_trans_handle *trans,
507 struct btrfs_root *root,
508 struct btrfs_path *path,
509 struct btrfs_block_group_cache *cache)
510{
511 int ret;
512 int pending_ret;
513 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -0400514 unsigned long bi;
515 struct extent_buffer *leaf;
Chris Mason9078a3e2007-04-26 16:46:15 -0400516
Chris Mason9078a3e2007-04-26 16:46:15 -0400517 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400518 if (ret < 0)
519 goto fail;
Chris Mason9078a3e2007-04-26 16:46:15 -0400520 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -0400521
522 leaf = path->nodes[0];
523 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
524 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
525 btrfs_mark_buffer_dirty(leaf);
Chris Mason9078a3e2007-04-26 16:46:15 -0400526 btrfs_release_path(extent_root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -0400527fail:
Chris Mason9078a3e2007-04-26 16:46:15 -0400528 finish_current_insert(trans, extent_root);
529 pending_ret = del_pending_extents(trans, extent_root);
530 if (ret)
531 return ret;
532 if (pending_ret)
533 return pending_ret;
534 return 0;
535
536}
537
Chris Mason96b51792007-10-15 16:15:19 -0400538int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
539 struct btrfs_root *root)
Chris Mason9078a3e2007-04-26 16:46:15 -0400540{
Chris Mason96b51792007-10-15 16:15:19 -0400541 struct extent_map_tree *block_group_cache;
542 struct btrfs_block_group_cache *cache;
Chris Mason9078a3e2007-04-26 16:46:15 -0400543 int ret;
544 int err = 0;
545 int werr = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -0400546 struct btrfs_path *path;
Chris Mason96b51792007-10-15 16:15:19 -0400547 u64 last = 0;
548 u64 start;
549 u64 end;
550 u64 ptr;
Chris Mason9078a3e2007-04-26 16:46:15 -0400551
Chris Mason96b51792007-10-15 16:15:19 -0400552 block_group_cache = &root->fs_info->block_group_cache;
Chris Mason9078a3e2007-04-26 16:46:15 -0400553 path = btrfs_alloc_path();
554 if (!path)
555 return -ENOMEM;
556
557 while(1) {
Chris Mason96b51792007-10-15 16:15:19 -0400558 ret = find_first_extent_bit(block_group_cache, last,
559 &start, &end, BLOCK_GROUP_DIRTY);
560 if (ret)
Chris Mason9078a3e2007-04-26 16:46:15 -0400561 break;
Chris Mason54aa1f42007-06-22 14:16:25 -0400562
Chris Mason96b51792007-10-15 16:15:19 -0400563 last = end + 1;
564 ret = get_state_private(block_group_cache, start, &ptr);
565 if (ret)
566 break;
567
Jens Axboeae2f5412007-10-19 09:22:59 -0400568 cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
Chris Mason96b51792007-10-15 16:15:19 -0400569 err = write_one_cache_group(trans, root,
570 path, cache);
571 /*
572 * if we fail to write the cache group, we want
573 * to keep it marked dirty in hopes that a later
574 * write will work
575 */
576 if (err) {
577 werr = err;
578 continue;
Chris Mason9078a3e2007-04-26 16:46:15 -0400579 }
Chris Mason96b51792007-10-15 16:15:19 -0400580 clear_extent_bits(block_group_cache, start, end,
581 BLOCK_GROUP_DIRTY, GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -0400582 }
583 btrfs_free_path(path);
584 return werr;
585}
586
587static int update_block_group(struct btrfs_trans_handle *trans,
588 struct btrfs_root *root,
Chris Masondb945352007-10-15 16:15:53 -0400589 u64 bytenr, u64 num_bytes, int alloc,
590 int mark_free, int data)
Chris Mason9078a3e2007-04-26 16:46:15 -0400591{
592 struct btrfs_block_group_cache *cache;
593 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -0400594 u64 total = num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -0400595 u64 old_val;
Chris Masondb945352007-10-15 16:15:53 -0400596 u64 byte_in_group;
Chris Mason96b51792007-10-15 16:15:19 -0400597 u64 start;
598 u64 end;
Chris Mason3e1ad542007-05-07 20:03:49 -0400599
Chris Mason9078a3e2007-04-26 16:46:15 -0400600 while(total) {
Chris Masondb945352007-10-15 16:15:53 -0400601 cache = btrfs_lookup_block_group(info, bytenr);
Chris Mason3e1ad542007-05-07 20:03:49 -0400602 if (!cache) {
Chris Mason9078a3e2007-04-26 16:46:15 -0400603 return -1;
Chris Masoncd1bc462007-04-27 10:08:34 -0400604 }
Chris Masondb945352007-10-15 16:15:53 -0400605 byte_in_group = bytenr - cache->key.objectid;
606 WARN_ON(byte_in_group > cache->key.offset);
Chris Mason96b51792007-10-15 16:15:19 -0400607 start = cache->key.objectid;
608 end = start + cache->key.offset - 1;
609 set_extent_bits(&info->block_group_cache, start, end,
610 BLOCK_GROUP_DIRTY, GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -0400611
612 old_val = btrfs_block_group_used(&cache->item);
Chris Masondb945352007-10-15 16:15:53 -0400613 num_bytes = min(total, cache->key.offset - byte_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -0400614 if (alloc) {
Chris Mason1e2677e2007-05-29 16:52:18 -0400615 if (cache->data != data &&
Chris Mason84f54cf2007-06-12 07:43:08 -0400616 old_val < (cache->key.offset >> 1)) {
Chris Mason96b51792007-10-15 16:15:19 -0400617 int bit_to_clear;
618 int bit_to_set;
Chris Mason96b51792007-10-15 16:15:19 -0400619 cache->data = data;
Chris Mason1e2677e2007-05-29 16:52:18 -0400620 if (data) {
Yanb97f9202007-11-01 11:28:41 -0400621 bit_to_clear = BLOCK_GROUP_METADATA;
622 bit_to_set = BLOCK_GROUP_DATA;
Chris Masonf84a8b32007-11-06 10:26:29 -0500623 cache->item.flags &=
624 ~BTRFS_BLOCK_GROUP_MIXED;
Chris Mason1e2677e2007-05-29 16:52:18 -0400625 cache->item.flags |=
626 BTRFS_BLOCK_GROUP_DATA;
627 } else {
Yanb97f9202007-11-01 11:28:41 -0400628 bit_to_clear = BLOCK_GROUP_DATA;
629 bit_to_set = BLOCK_GROUP_METADATA;
Chris Mason1e2677e2007-05-29 16:52:18 -0400630 cache->item.flags &=
Chris Masonf84a8b32007-11-06 10:26:29 -0500631 ~BTRFS_BLOCK_GROUP_MIXED;
632 cache->item.flags &=
Chris Mason1e2677e2007-05-29 16:52:18 -0400633 ~BTRFS_BLOCK_GROUP_DATA;
634 }
Chris Mason96b51792007-10-15 16:15:19 -0400635 clear_extent_bits(&info->block_group_cache,
636 start, end, bit_to_clear,
637 GFP_NOFS);
638 set_extent_bits(&info->block_group_cache,
639 start, end, bit_to_set,
640 GFP_NOFS);
Chris Masonf84a8b32007-11-06 10:26:29 -0500641 } else if (cache->data != data &&
642 cache->data != BTRFS_BLOCK_GROUP_MIXED) {
643 cache->data = BTRFS_BLOCK_GROUP_MIXED;
644 set_extent_bits(&info->block_group_cache,
645 start, end,
646 BLOCK_GROUP_DATA |
647 BLOCK_GROUP_METADATA,
648 GFP_NOFS);
Chris Mason1e2677e2007-05-29 16:52:18 -0400649 }
Chris Masondb945352007-10-15 16:15:53 -0400650 old_val += num_bytes;
Chris Masoncd1bc462007-04-27 10:08:34 -0400651 } else {
Chris Masondb945352007-10-15 16:15:53 -0400652 old_val -= num_bytes;
Chris Masonf510cfe2007-10-15 16:14:48 -0400653 if (mark_free) {
654 set_extent_dirty(&info->free_space_cache,
Chris Masondb945352007-10-15 16:15:53 -0400655 bytenr, bytenr + num_bytes - 1,
Chris Masonf510cfe2007-10-15 16:14:48 -0400656 GFP_NOFS);
Chris Masone37c9e62007-05-09 20:13:14 -0400657 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400658 }
Chris Mason9078a3e2007-04-26 16:46:15 -0400659 btrfs_set_block_group_used(&cache->item, old_val);
Chris Masondb945352007-10-15 16:15:53 -0400660 total -= num_bytes;
661 bytenr += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -0400662 }
663 return 0;
664}
Yan324ae4d2007-11-16 14:57:08 -0500665static int update_pinned_extents(struct btrfs_root *root,
666 u64 bytenr, u64 num, int pin)
667{
668 u64 len;
669 struct btrfs_block_group_cache *cache;
670 struct btrfs_fs_info *fs_info = root->fs_info;
671
672 if (pin) {
673 set_extent_dirty(&fs_info->pinned_extents,
674 bytenr, bytenr + num - 1, GFP_NOFS);
675 } else {
676 clear_extent_dirty(&fs_info->pinned_extents,
677 bytenr, bytenr + num - 1, GFP_NOFS);
678 }
679 while (num > 0) {
680 cache = btrfs_lookup_block_group(fs_info, bytenr);
681 WARN_ON(!cache);
682 len = min(num, cache->key.offset -
683 (bytenr - cache->key.objectid));
684 if (pin) {
685 cache->pinned += len;
686 fs_info->total_pinned += len;
687 } else {
688 cache->pinned -= len;
689 fs_info->total_pinned -= len;
690 }
691 bytenr += len;
692 num -= len;
693 }
694 return 0;
695}
Chris Mason9078a3e2007-04-26 16:46:15 -0400696
Chris Mason1a5bc1672007-10-15 16:15:26 -0400697int btrfs_copy_pinned(struct btrfs_root *root, struct extent_map_tree *copy)
Chris Masonccd467d2007-06-28 15:57:36 -0400698{
Chris Masonccd467d2007-06-28 15:57:36 -0400699 u64 last = 0;
Chris Mason1a5bc1672007-10-15 16:15:26 -0400700 u64 start;
701 u64 end;
702 struct extent_map_tree *pinned_extents = &root->fs_info->pinned_extents;
Chris Masonccd467d2007-06-28 15:57:36 -0400703 int ret;
Chris Masonccd467d2007-06-28 15:57:36 -0400704
705 while(1) {
Chris Mason1a5bc1672007-10-15 16:15:26 -0400706 ret = find_first_extent_bit(pinned_extents, last,
707 &start, &end, EXTENT_DIRTY);
708 if (ret)
Chris Masonccd467d2007-06-28 15:57:36 -0400709 break;
Chris Mason1a5bc1672007-10-15 16:15:26 -0400710 set_extent_dirty(copy, start, end, GFP_NOFS);
711 last = end + 1;
Chris Masonccd467d2007-06-28 15:57:36 -0400712 }
713 return 0;
714}
715
716int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
717 struct btrfs_root *root,
Chris Mason1a5bc1672007-10-15 16:15:26 -0400718 struct extent_map_tree *unpin)
Chris Masona28ec192007-03-06 20:08:01 -0500719{
Chris Mason1a5bc1672007-10-15 16:15:26 -0400720 u64 start;
721 u64 end;
Chris Masona28ec192007-03-06 20:08:01 -0500722 int ret;
Chris Masonf510cfe2007-10-15 16:14:48 -0400723 struct extent_map_tree *free_space_cache;
Chris Masonf510cfe2007-10-15 16:14:48 -0400724 free_space_cache = &root->fs_info->free_space_cache;
Chris Masona28ec192007-03-06 20:08:01 -0500725
726 while(1) {
Chris Mason1a5bc1672007-10-15 16:15:26 -0400727 ret = find_first_extent_bit(unpin, 0, &start, &end,
728 EXTENT_DIRTY);
729 if (ret)
Chris Masona28ec192007-03-06 20:08:01 -0500730 break;
Yan324ae4d2007-11-16 14:57:08 -0500731 update_pinned_extents(root, start, end + 1 - start, 0);
Chris Mason1a5bc1672007-10-15 16:15:26 -0400732 clear_extent_dirty(unpin, start, end, GFP_NOFS);
733 set_extent_dirty(free_space_cache, start, end, GFP_NOFS);
Chris Masona28ec192007-03-06 20:08:01 -0500734 }
735 return 0;
736}
737
Chris Masone089f052007-03-16 16:20:31 -0400738static int finish_current_insert(struct btrfs_trans_handle *trans, struct
739 btrfs_root *extent_root)
Chris Mason037e6392007-03-07 11:50:24 -0500740{
Chris Masone2fa7222007-03-12 16:22:34 -0400741 struct btrfs_key ins;
Chris Mason234b63a2007-03-13 10:46:10 -0400742 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -0500743 int ret;
Chris Mason1a5bc1672007-10-15 16:15:26 -0400744 int err = 0;
745 u64 start;
746 u64 end;
Chris Mason1261ec42007-03-20 20:35:03 -0400747 struct btrfs_fs_info *info = extent_root->fs_info;
Chris Mason037e6392007-03-07 11:50:24 -0500748
Chris Mason5f39d392007-10-15 16:14:19 -0400749 btrfs_set_stack_extent_refs(&extent_item, 1);
Chris Mason62e27492007-03-15 12:56:47 -0400750 btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
Chris Mason5f39d392007-10-15 16:14:19 -0400751 btrfs_set_stack_extent_owner(&extent_item,
752 extent_root->root_key.objectid);
Chris Mason037e6392007-03-07 11:50:24 -0500753
Chris Mason26b80032007-08-08 20:17:12 -0400754 while(1) {
Chris Mason1a5bc1672007-10-15 16:15:26 -0400755 ret = find_first_extent_bit(&info->extent_ins, 0, &start,
756 &end, EXTENT_LOCKED);
757 if (ret)
Chris Mason26b80032007-08-08 20:17:12 -0400758 break;
759
Chris Mason1a5bc1672007-10-15 16:15:26 -0400760 ins.objectid = start;
761 ins.offset = end + 1 - start;
762 err = btrfs_insert_item(trans, extent_root, &ins,
763 &extent_item, sizeof(extent_item));
764 clear_extent_bits(&info->extent_ins, start, end, EXTENT_LOCKED,
765 GFP_NOFS);
Chris Mason037e6392007-03-07 11:50:24 -0500766 }
Chris Mason037e6392007-03-07 11:50:24 -0500767 return 0;
768}
769
Chris Masondb945352007-10-15 16:15:53 -0400770static int pin_down_bytes(struct btrfs_root *root, u64 bytenr, u32 num_bytes,
771 int pending)
Chris Masone20d96d2007-03-22 12:13:20 -0400772{
Chris Mason1a5bc1672007-10-15 16:15:26 -0400773 int err = 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400774 struct extent_buffer *buf;
Chris Mason78fae272007-03-25 11:35:08 -0400775
Chris Masonf4b9aa82007-03-27 11:05:53 -0400776 if (!pending) {
Chris Masondb945352007-10-15 16:15:53 -0400777 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
Chris Mason5f39d392007-10-15 16:14:19 -0400778 if (buf) {
779 if (btrfs_buffer_uptodate(buf)) {
Chris Mason2c90e5d2007-04-02 10:50:19 -0400780 u64 transid =
781 root->fs_info->running_transaction->transid;
Chris Mason5f39d392007-10-15 16:14:19 -0400782 if (btrfs_header_generation(buf) == transid) {
783 free_extent_buffer(buf);
Yanc5492282007-11-06 10:25:25 -0500784 return 1;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400785 }
Chris Masonf4b9aa82007-03-27 11:05:53 -0400786 }
Chris Mason5f39d392007-10-15 16:14:19 -0400787 free_extent_buffer(buf);
Chris Mason8ef97622007-03-26 10:15:30 -0400788 }
Yan324ae4d2007-11-16 14:57:08 -0500789 update_pinned_extents(root, bytenr, num_bytes, 1);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400790 } else {
Chris Mason1a5bc1672007-10-15 16:15:26 -0400791 set_extent_bits(&root->fs_info->pending_del,
Chris Masondb945352007-10-15 16:15:53 -0400792 bytenr, bytenr + num_bytes - 1,
793 EXTENT_LOCKED, GFP_NOFS);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400794 }
Chris Masonbe744172007-05-06 10:15:01 -0400795 BUG_ON(err < 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400796 return 0;
797}
798
Chris Masona28ec192007-03-06 20:08:01 -0500799/*
800 * remove an extent from the root, returns 0 on success
801 */
Chris Masone089f052007-03-16 16:20:31 -0400802static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masondb945352007-10-15 16:15:53 -0400803 *root, u64 bytenr, u64 num_bytes, int pin,
Chris Masone37c9e62007-05-09 20:13:14 -0400804 int mark_free)
Chris Masona28ec192007-03-06 20:08:01 -0500805{
Chris Mason5caf2a02007-04-02 11:20:42 -0400806 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -0400807 struct btrfs_key key;
Chris Mason1261ec42007-03-20 20:35:03 -0400808 struct btrfs_fs_info *info = root->fs_info;
809 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -0400810 struct extent_buffer *leaf;
Chris Masona28ec192007-03-06 20:08:01 -0500811 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400812 struct btrfs_extent_item *ei;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400813 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500814
Chris Masondb945352007-10-15 16:15:53 -0400815 key.objectid = bytenr;
Chris Mason62e27492007-03-15 12:56:47 -0400816 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masondb945352007-10-15 16:15:53 -0400817 key.offset = num_bytes;
Chris Masona28ec192007-03-06 20:08:01 -0500818
Chris Mason5caf2a02007-04-02 11:20:42 -0400819 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -0400820 if (!path)
821 return -ENOMEM;
822
Chris Mason5caf2a02007-04-02 11:20:42 -0400823 ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400824 if (ret < 0)
825 return ret;
826 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -0400827
828 leaf = path->nodes[0];
829 ei = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason123abc82007-03-14 14:14:43 -0400830 struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400831 refs = btrfs_extent_refs(leaf, ei);
832 BUG_ON(refs == 0);
833 refs -= 1;
834 btrfs_set_extent_refs(leaf, ei, refs);
835 btrfs_mark_buffer_dirty(leaf);
836
Chris Masoncf27e1e2007-03-13 09:49:06 -0400837 if (refs == 0) {
Chris Masondb945352007-10-15 16:15:53 -0400838 u64 super_used;
839 u64 root_used;
Chris Mason78fae272007-03-25 11:35:08 -0400840
841 if (pin) {
Chris Masondb945352007-10-15 16:15:53 -0400842 ret = pin_down_bytes(root, bytenr, num_bytes, 0);
Yanc5492282007-11-06 10:25:25 -0500843 if (ret > 0)
844 mark_free = 1;
845 BUG_ON(ret < 0);
Chris Mason78fae272007-03-25 11:35:08 -0400846 }
847
Josef Bacik58176a92007-08-29 15:47:34 -0400848 /* block accounting for super block */
Chris Masondb945352007-10-15 16:15:53 -0400849 super_used = btrfs_super_bytes_used(&info->super_copy);
850 btrfs_set_super_bytes_used(&info->super_copy,
851 super_used - num_bytes);
Josef Bacik58176a92007-08-29 15:47:34 -0400852
853 /* block accounting for root item */
Chris Masondb945352007-10-15 16:15:53 -0400854 root_used = btrfs_root_used(&root->root_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400855 btrfs_set_root_used(&root->root_item,
Chris Masondb945352007-10-15 16:15:53 -0400856 root_used - num_bytes);
Josef Bacik58176a92007-08-29 15:47:34 -0400857
Chris Mason5caf2a02007-04-02 11:20:42 -0400858 ret = btrfs_del_item(trans, extent_root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -0400859 if (ret) {
860 return ret;
861 }
Chris Masondb945352007-10-15 16:15:53 -0400862 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
Chris Mason1e2677e2007-05-29 16:52:18 -0400863 mark_free, 0);
Chris Mason9078a3e2007-04-26 16:46:15 -0400864 BUG_ON(ret);
Chris Masona28ec192007-03-06 20:08:01 -0500865 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400866 btrfs_free_path(path);
Chris Masone089f052007-03-16 16:20:31 -0400867 finish_current_insert(trans, extent_root);
Chris Masona28ec192007-03-06 20:08:01 -0500868 return ret;
869}
870
871/*
Chris Masonfec577f2007-02-26 10:40:21 -0500872 * find all the blocks marked as pending in the radix tree and remove
873 * them from the extent map
874 */
Chris Masone089f052007-03-16 16:20:31 -0400875static int del_pending_extents(struct btrfs_trans_handle *trans, struct
876 btrfs_root *extent_root)
Chris Masonfec577f2007-02-26 10:40:21 -0500877{
878 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -0400879 int err = 0;
Chris Mason1a5bc1672007-10-15 16:15:26 -0400880 u64 start;
881 u64 end;
882 struct extent_map_tree *pending_del;
883 struct extent_map_tree *pinned_extents;
Chris Mason8ef97622007-03-26 10:15:30 -0400884
Chris Mason1a5bc1672007-10-15 16:15:26 -0400885 pending_del = &extent_root->fs_info->pending_del;
886 pinned_extents = &extent_root->fs_info->pinned_extents;
Chris Masonfec577f2007-02-26 10:40:21 -0500887
888 while(1) {
Chris Mason1a5bc1672007-10-15 16:15:26 -0400889 ret = find_first_extent_bit(pending_del, 0, &start, &end,
890 EXTENT_LOCKED);
891 if (ret)
Chris Masonfec577f2007-02-26 10:40:21 -0500892 break;
Yan324ae4d2007-11-16 14:57:08 -0500893 update_pinned_extents(extent_root, start, end + 1 - start, 1);
Chris Mason1a5bc1672007-10-15 16:15:26 -0400894 clear_extent_bits(pending_del, start, end, EXTENT_LOCKED,
895 GFP_NOFS);
896 ret = __free_extent(trans, extent_root,
897 start, end + 1 - start, 0, 0);
898 if (ret)
899 err = ret;
Chris Masonfec577f2007-02-26 10:40:21 -0500900 }
Chris Masone20d96d2007-03-22 12:13:20 -0400901 return err;
Chris Masonfec577f2007-02-26 10:40:21 -0500902}
903
904/*
905 * remove an extent from the root, returns 0 on success
906 */
Chris Masone089f052007-03-16 16:20:31 -0400907int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masondb945352007-10-15 16:15:53 -0400908 *root, u64 bytenr, u64 num_bytes, int pin)
Chris Masonfec577f2007-02-26 10:40:21 -0500909{
Chris Mason9f5fae22007-03-20 14:38:32 -0400910 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Masonfec577f2007-02-26 10:40:21 -0500911 int pending_ret;
912 int ret;
Chris Masona28ec192007-03-06 20:08:01 -0500913
Chris Masondb945352007-10-15 16:15:53 -0400914 WARN_ON(num_bytes < root->sectorsize);
Chris Masona28ec192007-03-06 20:08:01 -0500915 if (root == extent_root) {
Chris Masondb945352007-10-15 16:15:53 -0400916 pin_down_bytes(root, bytenr, num_bytes, 1);
Chris Masona28ec192007-03-06 20:08:01 -0500917 return 0;
918 }
Chris Masondb945352007-10-15 16:15:53 -0400919 ret = __free_extent(trans, root, bytenr, num_bytes, pin, pin == 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400920 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
Chris Masonfec577f2007-02-26 10:40:21 -0500921 return ret ? ret : pending_ret;
922}
923
924/*
925 * walks the btree of allocated extents and find a hole of a given size.
926 * The key ins is changed to record the hole:
927 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -0400928 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -0500929 * ins->offset == number of blocks
930 * Any available blocks before search_start are skipped.
931 */
Chris Masone089f052007-03-16 16:20:31 -0400932static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masondb945352007-10-15 16:15:53 -0400933 *orig_root, u64 num_bytes, u64 empty_size,
934 u64 search_start, u64 search_end, u64 hint_byte,
Chris Masonf2654de2007-06-26 12:20:46 -0400935 struct btrfs_key *ins, u64 exclude_start,
936 u64 exclude_nr, int data)
Chris Masonfec577f2007-02-26 10:40:21 -0500937{
Chris Mason5caf2a02007-04-02 11:20:42 -0400938 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -0400939 struct btrfs_key key;
Chris Masonfec577f2007-02-26 10:40:21 -0500940 int ret;
941 u64 hole_size = 0;
942 int slot = 0;
Chris Masondb945352007-10-15 16:15:53 -0400943 u64 last_byte = 0;
Chris Masonbe744172007-05-06 10:15:01 -0400944 u64 orig_search_start = search_start;
Chris Masonfec577f2007-02-26 10:40:21 -0500945 int start_found;
Chris Mason5f39d392007-10-15 16:14:19 -0400946 struct extent_buffer *l;
Chris Mason9f5fae22007-03-20 14:38:32 -0400947 struct btrfs_root * root = orig_root->fs_info->extent_root;
Chris Masonf2458e12007-04-25 15:52:25 -0400948 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -0400949 u64 total_needed = num_bytes;
Chris Masone20d96d2007-03-22 12:13:20 -0400950 int level;
Chris Masonbe08c1b2007-05-03 09:06:49 -0400951 struct btrfs_block_group_cache *block_group;
Chris Masonbe744172007-05-06 10:15:01 -0400952 int full_scan = 0;
Chris Masonfbdc7622007-05-30 10:22:12 -0400953 int wrapped = 0;
Chris Masonf84a8b32007-11-06 10:26:29 -0500954 u64 cached_start;
Chris Masonfec577f2007-02-26 10:40:21 -0500955
Chris Masondb945352007-10-15 16:15:53 -0400956 WARN_ON(num_bytes < root->sectorsize);
Chris Masonb1a4d962007-04-04 15:27:52 -0400957 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
958
Chris Mason5f39d392007-10-15 16:14:19 -0400959 level = btrfs_header_level(root->node);
960
Chris Mason257d0ce2007-11-07 21:08:16 -0500961 if (num_bytes >= 96 * 1024 * 1024 && hint_byte) {
962 data = BTRFS_BLOCK_GROUP_MIXED;
963 }
964
Chris Mason3e1ad542007-05-07 20:03:49 -0400965 if (search_end == (u64)-1)
Chris Masondb945352007-10-15 16:15:53 -0400966 search_end = btrfs_super_total_bytes(&info->super_copy);
967 if (hint_byte) {
968 block_group = btrfs_lookup_block_group(info, hint_byte);
Chris Masonbe744172007-05-06 10:15:01 -0400969 block_group = btrfs_find_block_group(root, block_group,
Chris Masondb945352007-10-15 16:15:53 -0400970 hint_byte, data, 1);
Chris Masonbe744172007-05-06 10:15:01 -0400971 } else {
972 block_group = btrfs_find_block_group(root,
973 trans->block_group, 0,
Chris Masonde428b62007-05-18 13:28:27 -0400974 data, 1);
Chris Masonbe744172007-05-06 10:15:01 -0400975 }
976
Chris Mason6702ed42007-08-07 16:15:09 -0400977 total_needed += empty_size;
Chris Masone0115992007-06-19 16:23:05 -0400978 path = btrfs_alloc_path();
Chris Masonbe744172007-05-06 10:15:01 -0400979check_failed:
Yan5e5745d2007-11-16 14:57:09 -0500980 search_start = find_search_start(root, &block_group, search_start,
981 total_needed, data, full_scan);
Chris Masonf84a8b32007-11-06 10:26:29 -0500982 cached_start = search_start;
Chris Mason5caf2a02007-04-02 11:20:42 -0400983 btrfs_init_path(path);
Chris Masonfec577f2007-02-26 10:40:21 -0500984 ins->objectid = search_start;
985 ins->offset = 0;
Chris Masonfec577f2007-02-26 10:40:21 -0500986 start_found = 0;
Chris Mason2cc58cf2007-08-27 16:49:44 -0400987 path->reada = 2;
Chris Masone37c9e62007-05-09 20:13:14 -0400988
Chris Mason5caf2a02007-04-02 11:20:42 -0400989 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
Chris Mason0f70abe2007-02-28 16:46:22 -0500990 if (ret < 0)
991 goto error;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500992
Chris Masone37c9e62007-05-09 20:13:14 -0400993 if (path->slots[0] > 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400994 path->slots[0]--;
Chris Masone37c9e62007-05-09 20:13:14 -0400995 }
996
Chris Mason5f39d392007-10-15 16:14:19 -0400997 l = path->nodes[0];
998 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
999
Chris Masone37c9e62007-05-09 20:13:14 -04001000 /*
1001 * a rare case, go back one key if we hit a block group item
1002 * instead of an extent item
1003 */
1004 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY &&
1005 key.objectid + key.offset >= search_start) {
1006 ins->objectid = key.objectid;
1007 ins->offset = key.offset - 1;
1008 btrfs_release_path(root, path);
1009 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
1010 if (ret < 0)
1011 goto error;
1012
1013 if (path->slots[0] > 0) {
1014 path->slots[0]--;
1015 }
1016 }
Chris Mason0579da42007-03-07 16:15:30 -05001017
Chris Masonfec577f2007-02-26 10:40:21 -05001018 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001019 l = path->nodes[0];
Chris Mason5caf2a02007-04-02 11:20:42 -04001020 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04001021 if (slot >= btrfs_header_nritems(l)) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001022 ret = btrfs_next_leaf(root, path);
Chris Masonfec577f2007-02-26 10:40:21 -05001023 if (ret == 0)
1024 continue;
Chris Mason0f70abe2007-02-28 16:46:22 -05001025 if (ret < 0)
1026 goto error;
Chris Masone19caa52007-10-15 16:17:44 -04001027
1028 search_start = max(search_start,
1029 block_group->key.objectid);
Chris Masonfec577f2007-02-26 10:40:21 -05001030 if (!start_found) {
1031 ins->objectid = search_start;
Chris Mason3e1ad542007-05-07 20:03:49 -04001032 ins->offset = search_end - search_start;
Chris Masonfec577f2007-02-26 10:40:21 -05001033 start_found = 1;
1034 goto check_pending;
1035 }
Chris Masondb945352007-10-15 16:15:53 -04001036 ins->objectid = last_byte > search_start ?
1037 last_byte : search_start;
Chris Mason3e1ad542007-05-07 20:03:49 -04001038 ins->offset = search_end - ins->objectid;
Chris Masone19caa52007-10-15 16:17:44 -04001039 BUG_ON(ins->objectid >= search_end);
Chris Masonfec577f2007-02-26 10:40:21 -05001040 goto check_pending;
1041 }
Chris Mason5f39d392007-10-15 16:14:19 -04001042 btrfs_item_key_to_cpu(l, &key, slot);
Chris Mason96b51792007-10-15 16:15:19 -04001043
Chris Masondb945352007-10-15 16:15:53 -04001044 if (key.objectid >= search_start && key.objectid > last_byte &&
Chris Masone37c9e62007-05-09 20:13:14 -04001045 start_found) {
Chris Masondb945352007-10-15 16:15:53 -04001046 if (last_byte < search_start)
1047 last_byte = search_start;
1048 hole_size = key.objectid - last_byte;
1049 if (hole_size >= num_bytes) {
1050 ins->objectid = last_byte;
Chris Masone37c9e62007-05-09 20:13:14 -04001051 ins->offset = hole_size;
1052 goto check_pending;
Chris Mason0579da42007-03-07 16:15:30 -05001053 }
Chris Masonfec577f2007-02-26 10:40:21 -05001054 }
Chris Mason96b51792007-10-15 16:15:19 -04001055 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY) {
1056 if (!start_found) {
Chris Masondb945352007-10-15 16:15:53 -04001057 last_byte = key.objectid;
Chris Mason96b51792007-10-15 16:15:19 -04001058 start_found = 1;
1059 }
Chris Masone37c9e62007-05-09 20:13:14 -04001060 goto next;
Chris Mason96b51792007-10-15 16:15:19 -04001061 }
1062
Chris Masone37c9e62007-05-09 20:13:14 -04001063
Chris Mason0579da42007-03-07 16:15:30 -05001064 start_found = 1;
Chris Masondb945352007-10-15 16:15:53 -04001065 last_byte = key.objectid + key.offset;
Chris Masonf510cfe2007-10-15 16:14:48 -04001066
Chris Mason257d0ce2007-11-07 21:08:16 -05001067 if (!full_scan && data != BTRFS_BLOCK_GROUP_MIXED &&
1068 last_byte >= block_group->key.objectid +
Chris Masonbe744172007-05-06 10:15:01 -04001069 block_group->key.offset) {
1070 btrfs_release_path(root, path);
1071 search_start = block_group->key.objectid +
Chris Masone19caa52007-10-15 16:17:44 -04001072 block_group->key.offset;
Chris Masonbe744172007-05-06 10:15:01 -04001073 goto new_group;
1074 }
Chris Mason9078a3e2007-04-26 16:46:15 -04001075next:
Chris Mason5caf2a02007-04-02 11:20:42 -04001076 path->slots[0]++;
Chris Masonde428b62007-05-18 13:28:27 -04001077 cond_resched();
Chris Masonfec577f2007-02-26 10:40:21 -05001078 }
Chris Masonfec577f2007-02-26 10:40:21 -05001079check_pending:
1080 /* we have to make sure we didn't find an extent that has already
1081 * been allocated by the map tree or the original allocation
1082 */
Chris Mason5caf2a02007-04-02 11:20:42 -04001083 btrfs_release_path(root, path);
Chris Masonfec577f2007-02-26 10:40:21 -05001084 BUG_ON(ins->objectid < search_start);
Chris Masone37c9e62007-05-09 20:13:14 -04001085
Chris Masondb945352007-10-15 16:15:53 -04001086 if (ins->objectid + num_bytes >= search_end)
Chris Masoncf675822007-09-17 11:00:51 -04001087 goto enospc;
Chris Mason257d0ce2007-11-07 21:08:16 -05001088 if (!full_scan && data != BTRFS_BLOCK_GROUP_MIXED &&
Yan5cf66422007-11-16 14:57:09 -05001089 ins->objectid + num_bytes > block_group->
Chris Masone19caa52007-10-15 16:17:44 -04001090 key.objectid + block_group->key.offset) {
1091 search_start = block_group->key.objectid +
1092 block_group->key.offset;
1093 goto new_group;
1094 }
Chris Mason1a5bc1672007-10-15 16:15:26 -04001095 if (test_range_bit(&info->extent_ins, ins->objectid,
Chris Masondb945352007-10-15 16:15:53 -04001096 ins->objectid + num_bytes -1, EXTENT_LOCKED, 0)) {
1097 search_start = ins->objectid + num_bytes;
Chris Mason1a5bc1672007-10-15 16:15:26 -04001098 goto new_group;
1099 }
1100 if (test_range_bit(&info->pinned_extents, ins->objectid,
Chris Masondb945352007-10-15 16:15:53 -04001101 ins->objectid + num_bytes -1, EXTENT_DIRTY, 0)) {
1102 search_start = ins->objectid + num_bytes;
Chris Mason1a5bc1672007-10-15 16:15:26 -04001103 goto new_group;
Chris Masonfec577f2007-02-26 10:40:21 -05001104 }
Chris Masondb945352007-10-15 16:15:53 -04001105 if (exclude_nr > 0 && (ins->objectid + num_bytes > exclude_start &&
Chris Masonf2654de2007-06-26 12:20:46 -04001106 ins->objectid < exclude_start + exclude_nr)) {
1107 search_start = exclude_start + exclude_nr;
1108 goto new_group;
1109 }
Chris Masone37c9e62007-05-09 20:13:14 -04001110 if (!data) {
Chris Mason5276aed2007-06-11 21:33:38 -04001111 block_group = btrfs_lookup_block_group(info, ins->objectid);
Chris Mason26b80032007-08-08 20:17:12 -04001112 if (block_group)
1113 trans->block_group = block_group;
Chris Masoncd1bc462007-04-27 10:08:34 -04001114 }
Chris Masondb945352007-10-15 16:15:53 -04001115 ins->offset = num_bytes;
Chris Mason5caf2a02007-04-02 11:20:42 -04001116 btrfs_free_path(path);
Chris Masonfec577f2007-02-26 10:40:21 -05001117 return 0;
Chris Masonbe744172007-05-06 10:15:01 -04001118
1119new_group:
Chris Masondb945352007-10-15 16:15:53 -04001120 if (search_start + num_bytes >= search_end) {
Chris Masoncf675822007-09-17 11:00:51 -04001121enospc:
Chris Masonbe744172007-05-06 10:15:01 -04001122 search_start = orig_search_start;
Chris Masonfbdc7622007-05-30 10:22:12 -04001123 if (full_scan) {
1124 ret = -ENOSPC;
1125 goto error;
1126 }
Chris Mason6702ed42007-08-07 16:15:09 -04001127 if (wrapped) {
1128 if (!full_scan)
1129 total_needed -= empty_size;
Chris Masonfbdc7622007-05-30 10:22:12 -04001130 full_scan = 1;
Chris Mason6702ed42007-08-07 16:15:09 -04001131 } else
Chris Masonfbdc7622007-05-30 10:22:12 -04001132 wrapped = 1;
Chris Masonbe744172007-05-06 10:15:01 -04001133 }
Chris Mason5276aed2007-06-11 21:33:38 -04001134 block_group = btrfs_lookup_block_group(info, search_start);
Chris Masonfbdc7622007-05-30 10:22:12 -04001135 cond_resched();
Chris Masonbe744172007-05-06 10:15:01 -04001136 if (!full_scan)
1137 block_group = btrfs_find_block_group(root, block_group,
Chris Masonde428b62007-05-18 13:28:27 -04001138 search_start, data, 0);
Chris Masonbe744172007-05-06 10:15:01 -04001139 goto check_failed;
1140
Chris Mason0f70abe2007-02-28 16:46:22 -05001141error:
Chris Mason5caf2a02007-04-02 11:20:42 -04001142 btrfs_release_path(root, path);
1143 btrfs_free_path(path);
Chris Mason0f70abe2007-02-28 16:46:22 -05001144 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05001145}
Chris Masonfec577f2007-02-26 10:40:21 -05001146/*
Chris Masonfec577f2007-02-26 10:40:21 -05001147 * finds a free extent and does all the dirty work required for allocation
1148 * returns the key for the extent through ins, and a tree buffer for
1149 * the first block of the extent through buf.
1150 *
1151 * returns 0 if everything worked, non-zero otherwise.
1152 */
Chris Mason4d775672007-04-20 20:23:12 -04001153int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
1154 struct btrfs_root *root, u64 owner,
Chris Masondb945352007-10-15 16:15:53 -04001155 u64 num_bytes, u64 empty_size, u64 hint_byte,
Chris Masonbe08c1b2007-05-03 09:06:49 -04001156 u64 search_end, struct btrfs_key *ins, int data)
Chris Masonfec577f2007-02-26 10:40:21 -05001157{
1158 int ret;
1159 int pending_ret;
Chris Masondb945352007-10-15 16:15:53 -04001160 u64 super_used, root_used;
Chris Masonfbdc7622007-05-30 10:22:12 -04001161 u64 search_start = 0;
Chris Mason1261ec42007-03-20 20:35:03 -04001162 struct btrfs_fs_info *info = root->fs_info;
1163 struct btrfs_root *extent_root = info->extent_root;
Chris Mason234b63a2007-03-13 10:46:10 -04001164 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -05001165
Chris Mason5f39d392007-10-15 16:14:19 -04001166 btrfs_set_stack_extent_refs(&extent_item, 1);
1167 btrfs_set_stack_extent_owner(&extent_item, owner);
Chris Masonfec577f2007-02-26 10:40:21 -05001168
Chris Masondb945352007-10-15 16:15:53 -04001169 WARN_ON(num_bytes < root->sectorsize);
1170 ret = find_free_extent(trans, root, num_bytes, empty_size,
1171 search_start, search_end, hint_byte, ins,
Chris Mason26b80032007-08-08 20:17:12 -04001172 trans->alloc_exclude_start,
1173 trans->alloc_exclude_nr, data);
Chris Masonccd467d2007-06-28 15:57:36 -04001174 BUG_ON(ret);
Chris Masonf2654de2007-06-26 12:20:46 -04001175 if (ret)
1176 return ret;
1177
Josef Bacik58176a92007-08-29 15:47:34 -04001178 /* block accounting for super block */
Chris Masondb945352007-10-15 16:15:53 -04001179 super_used = btrfs_super_bytes_used(&info->super_copy);
1180 btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes);
Chris Mason26b80032007-08-08 20:17:12 -04001181
Josef Bacik58176a92007-08-29 15:47:34 -04001182 /* block accounting for root item */
Chris Masondb945352007-10-15 16:15:53 -04001183 root_used = btrfs_root_used(&root->root_item);
1184 btrfs_set_root_used(&root->root_item, root_used + num_bytes);
Josef Bacik58176a92007-08-29 15:47:34 -04001185
Chris Masonf510cfe2007-10-15 16:14:48 -04001186 clear_extent_dirty(&root->fs_info->free_space_cache,
1187 ins->objectid, ins->objectid + ins->offset - 1,
1188 GFP_NOFS);
1189
Chris Mason26b80032007-08-08 20:17:12 -04001190 if (root == extent_root) {
Chris Mason1a5bc1672007-10-15 16:15:26 -04001191 set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
1192 ins->objectid + ins->offset - 1,
1193 EXTENT_LOCKED, GFP_NOFS);
Chris Masone19caa52007-10-15 16:17:44 -04001194 WARN_ON(data == 1);
Chris Mason26b80032007-08-08 20:17:12 -04001195 goto update_block;
1196 }
1197
1198 WARN_ON(trans->alloc_exclude_nr);
1199 trans->alloc_exclude_start = ins->objectid;
1200 trans->alloc_exclude_nr = ins->offset;
Chris Masone089f052007-03-16 16:20:31 -04001201 ret = btrfs_insert_item(trans, extent_root, ins, &extent_item,
1202 sizeof(extent_item));
Chris Mason037e6392007-03-07 11:50:24 -05001203
Chris Mason26b80032007-08-08 20:17:12 -04001204 trans->alloc_exclude_start = 0;
1205 trans->alloc_exclude_nr = 0;
1206
Chris Masonccd467d2007-06-28 15:57:36 -04001207 BUG_ON(ret);
Chris Masone089f052007-03-16 16:20:31 -04001208 finish_current_insert(trans, extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -04001209 pending_ret = del_pending_extents(trans, extent_root);
Chris Masonf510cfe2007-10-15 16:14:48 -04001210
Chris Masone37c9e62007-05-09 20:13:14 -04001211 if (ret) {
Chris Mason037e6392007-03-07 11:50:24 -05001212 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -04001213 }
1214 if (pending_ret) {
Chris Mason037e6392007-03-07 11:50:24 -05001215 return pending_ret;
Chris Masone37c9e62007-05-09 20:13:14 -04001216 }
Chris Mason26b80032007-08-08 20:17:12 -04001217
1218update_block:
Chris Mason1e2677e2007-05-29 16:52:18 -04001219 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0,
1220 data);
Chris Masonfabb5682007-06-07 22:13:21 -04001221 BUG_ON(ret);
Chris Mason037e6392007-03-07 11:50:24 -05001222 return 0;
Chris Masonfec577f2007-02-26 10:40:21 -05001223}
1224
1225/*
1226 * helper function to allocate a block for a given tree
1227 * returns the tree buffer or NULL.
1228 */
Chris Mason5f39d392007-10-15 16:14:19 -04001229struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Chris Masondb945352007-10-15 16:15:53 -04001230 struct btrfs_root *root,
1231 u32 blocksize, u64 hint,
Chris Mason5f39d392007-10-15 16:14:19 -04001232 u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05001233{
Chris Masone2fa7222007-03-12 16:22:34 -04001234 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -05001235 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04001236 struct extent_buffer *buf;
Chris Masonfec577f2007-02-26 10:40:21 -05001237
Chris Mason4d775672007-04-20 20:23:12 -04001238 ret = btrfs_alloc_extent(trans, root, root->root_key.objectid,
Chris Masondb945352007-10-15 16:15:53 -04001239 blocksize, empty_size, hint,
1240 (u64)-1, &ins, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05001241 if (ret) {
Chris Mason54aa1f42007-06-22 14:16:25 -04001242 BUG_ON(ret > 0);
1243 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05001244 }
Chris Masondb945352007-10-15 16:15:53 -04001245 buf = btrfs_find_create_tree_block(root, ins.objectid, blocksize);
Chris Mason54aa1f42007-06-22 14:16:25 -04001246 if (!buf) {
Chris Masondb945352007-10-15 16:15:53 -04001247 btrfs_free_extent(trans, root, ins.objectid, blocksize, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001248 return ERR_PTR(-ENOMEM);
1249 }
Chris Mason5f39d392007-10-15 16:14:19 -04001250 btrfs_set_buffer_uptodate(buf);
1251 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
1252 buf->start + buf->len - 1, GFP_NOFS);
Chris Mason19c00dd2007-10-15 16:19:22 -04001253 set_extent_bits(&BTRFS_I(root->fs_info->btree_inode)->extent_tree,
1254 buf->start, buf->start + buf->len - 1,
1255 EXTENT_CSUM, GFP_NOFS);
1256 buf->flags |= EXTENT_CSUM;
Chris Mason6b800532007-10-15 16:17:34 -04001257 btrfs_set_buffer_defrag(buf);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001258 trans->blocks_used++;
Chris Masonfec577f2007-02-26 10:40:21 -05001259 return buf;
1260}
Chris Masona28ec192007-03-06 20:08:01 -05001261
Chris Mason6407bf62007-03-27 06:33:00 -04001262static int drop_leaf_ref(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001263 struct btrfs_root *root, struct extent_buffer *leaf)
Chris Mason6407bf62007-03-27 06:33:00 -04001264{
Chris Mason5f39d392007-10-15 16:14:19 -04001265 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -04001266 struct btrfs_file_extent_item *fi;
1267 int i;
1268 int nritems;
1269 int ret;
1270
Chris Mason5f39d392007-10-15 16:14:19 -04001271 BUG_ON(!btrfs_is_leaf(leaf));
1272 nritems = btrfs_header_nritems(leaf);
Chris Mason6407bf62007-03-27 06:33:00 -04001273 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04001274 u64 disk_bytenr;
Chris Mason5f39d392007-10-15 16:14:19 -04001275
1276 btrfs_item_key_to_cpu(leaf, &key, i);
1277 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason6407bf62007-03-27 06:33:00 -04001278 continue;
1279 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001280 if (btrfs_file_extent_type(leaf, fi) ==
1281 BTRFS_FILE_EXTENT_INLINE)
Chris Mason236454df2007-04-19 13:37:44 -04001282 continue;
Chris Mason6407bf62007-03-27 06:33:00 -04001283 /*
1284 * FIXME make sure to insert a trans record that
1285 * repeats the snapshot del on crash
1286 */
Chris Masondb945352007-10-15 16:15:53 -04001287 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
1288 if (disk_bytenr == 0)
Chris Mason3a686372007-05-24 13:35:57 -04001289 continue;
Chris Masondb945352007-10-15 16:15:53 -04001290 ret = btrfs_free_extent(trans, root, disk_bytenr,
1291 btrfs_file_extent_disk_num_bytes(leaf, fi), 0);
Chris Mason6407bf62007-03-27 06:33:00 -04001292 BUG_ON(ret);
1293 }
1294 return 0;
1295}
1296
Chris Masone0115992007-06-19 16:23:05 -04001297static void reada_walk_down(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -04001298 struct extent_buffer *node)
Chris Masone0115992007-06-19 16:23:05 -04001299{
1300 int i;
1301 u32 nritems;
Chris Masondb945352007-10-15 16:15:53 -04001302 u64 bytenr;
Chris Masone0115992007-06-19 16:23:05 -04001303 int ret;
1304 u32 refs;
Chris Masondb945352007-10-15 16:15:53 -04001305 int level;
1306 u32 blocksize;
Chris Masone0115992007-06-19 16:23:05 -04001307
Chris Mason5f39d392007-10-15 16:14:19 -04001308 nritems = btrfs_header_nritems(node);
Chris Masondb945352007-10-15 16:15:53 -04001309 level = btrfs_header_level(node);
Chris Masone0115992007-06-19 16:23:05 -04001310 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04001311 bytenr = btrfs_node_blockptr(node, i);
1312 blocksize = btrfs_level_size(root, level - 1);
1313 ret = lookup_extent_ref(NULL, root, bytenr, blocksize, &refs);
Chris Masone0115992007-06-19 16:23:05 -04001314 BUG_ON(ret);
1315 if (refs != 1)
1316 continue;
Chris Mason409eb952007-08-08 20:17:12 -04001317 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masondb945352007-10-15 16:15:53 -04001318 ret = readahead_tree_block(root, bytenr, blocksize);
Chris Mason409eb952007-08-08 20:17:12 -04001319 cond_resched();
1320 mutex_lock(&root->fs_info->fs_mutex);
Chris Masone0115992007-06-19 16:23:05 -04001321 if (ret)
1322 break;
1323 }
1324}
1325
Chris Mason9aca1d52007-03-13 11:09:37 -04001326/*
1327 * helper function for drop_snapshot, this walks down the tree dropping ref
1328 * counts as it goes.
1329 */
Chris Masone089f052007-03-16 16:20:31 -04001330static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
1331 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05001332{
Chris Mason5f39d392007-10-15 16:14:19 -04001333 struct extent_buffer *next;
1334 struct extent_buffer *cur;
Chris Masondb945352007-10-15 16:15:53 -04001335 u64 bytenr;
1336 u32 blocksize;
Chris Mason20524f02007-03-10 06:35:47 -05001337 int ret;
1338 u32 refs;
1339
Chris Mason5caf2a02007-04-02 11:20:42 -04001340 WARN_ON(*level < 0);
1341 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason5f39d392007-10-15 16:14:19 -04001342 ret = lookup_extent_ref(trans, root,
Chris Masondb945352007-10-15 16:15:53 -04001343 path->nodes[*level]->start,
1344 path->nodes[*level]->len, &refs);
Chris Mason20524f02007-03-10 06:35:47 -05001345 BUG_ON(ret);
1346 if (refs > 1)
1347 goto out;
Chris Masone0115992007-06-19 16:23:05 -04001348
Chris Mason9aca1d52007-03-13 11:09:37 -04001349 /*
1350 * walk down to the last node level and free all the leaves
1351 */
Chris Mason6407bf62007-03-27 06:33:00 -04001352 while(*level >= 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001353 WARN_ON(*level < 0);
1354 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason20524f02007-03-10 06:35:47 -05001355 cur = path->nodes[*level];
Chris Masone0115992007-06-19 16:23:05 -04001356
1357 if (*level > 0 && path->slots[*level] == 0)
Chris Mason5f39d392007-10-15 16:14:19 -04001358 reada_walk_down(root, cur);
Chris Masone0115992007-06-19 16:23:05 -04001359
Chris Mason5f39d392007-10-15 16:14:19 -04001360 if (btrfs_header_level(cur) != *level)
Chris Mason2c90e5d2007-04-02 10:50:19 -04001361 WARN_ON(1);
Chris Masone0115992007-06-19 16:23:05 -04001362
Chris Mason7518a232007-03-12 12:01:18 -04001363 if (path->slots[*level] >=
Chris Mason5f39d392007-10-15 16:14:19 -04001364 btrfs_header_nritems(cur))
Chris Mason20524f02007-03-10 06:35:47 -05001365 break;
Chris Mason6407bf62007-03-27 06:33:00 -04001366 if (*level == 0) {
1367 ret = drop_leaf_ref(trans, root, cur);
1368 BUG_ON(ret);
1369 break;
1370 }
Chris Masondb945352007-10-15 16:15:53 -04001371 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
1372 blocksize = btrfs_level_size(root, *level - 1);
1373 ret = lookup_extent_ref(trans, root, bytenr, blocksize, &refs);
Chris Mason6407bf62007-03-27 06:33:00 -04001374 BUG_ON(ret);
1375 if (refs != 1) {
Chris Mason20524f02007-03-10 06:35:47 -05001376 path->slots[*level]++;
Chris Masondb945352007-10-15 16:15:53 -04001377 ret = btrfs_free_extent(trans, root, bytenr,
1378 blocksize, 1);
Chris Mason20524f02007-03-10 06:35:47 -05001379 BUG_ON(ret);
1380 continue;
1381 }
Chris Masondb945352007-10-15 16:15:53 -04001382 next = btrfs_find_tree_block(root, bytenr, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -04001383 if (!next || !btrfs_buffer_uptodate(next)) {
1384 free_extent_buffer(next);
Chris Masone9d0b132007-08-10 14:06:19 -04001385 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masondb945352007-10-15 16:15:53 -04001386 next = read_tree_block(root, bytenr, blocksize);
Chris Masone9d0b132007-08-10 14:06:19 -04001387 mutex_lock(&root->fs_info->fs_mutex);
1388
1389 /* we dropped the lock, check one more time */
Chris Masondb945352007-10-15 16:15:53 -04001390 ret = lookup_extent_ref(trans, root, bytenr,
1391 blocksize, &refs);
Chris Masone9d0b132007-08-10 14:06:19 -04001392 BUG_ON(ret);
1393 if (refs != 1) {
1394 path->slots[*level]++;
Chris Mason5f39d392007-10-15 16:14:19 -04001395 free_extent_buffer(next);
Chris Masone9d0b132007-08-10 14:06:19 -04001396 ret = btrfs_free_extent(trans, root,
Chris Masondb945352007-10-15 16:15:53 -04001397 bytenr, blocksize, 1);
Chris Masone9d0b132007-08-10 14:06:19 -04001398 BUG_ON(ret);
1399 continue;
1400 }
1401 }
Chris Mason5caf2a02007-04-02 11:20:42 -04001402 WARN_ON(*level <= 0);
Chris Mason83e15a22007-03-12 09:03:27 -04001403 if (path->nodes[*level-1])
Chris Mason5f39d392007-10-15 16:14:19 -04001404 free_extent_buffer(path->nodes[*level-1]);
Chris Mason20524f02007-03-10 06:35:47 -05001405 path->nodes[*level-1] = next;
Chris Mason5f39d392007-10-15 16:14:19 -04001406 *level = btrfs_header_level(next);
Chris Mason20524f02007-03-10 06:35:47 -05001407 path->slots[*level] = 0;
1408 }
1409out:
Chris Mason5caf2a02007-04-02 11:20:42 -04001410 WARN_ON(*level < 0);
1411 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Masondb945352007-10-15 16:15:53 -04001412 ret = btrfs_free_extent(trans, root, path->nodes[*level]->start,
1413 path->nodes[*level]->len, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001414 free_extent_buffer(path->nodes[*level]);
Chris Mason20524f02007-03-10 06:35:47 -05001415 path->nodes[*level] = NULL;
1416 *level += 1;
1417 BUG_ON(ret);
1418 return 0;
1419}
1420
Chris Mason9aca1d52007-03-13 11:09:37 -04001421/*
1422 * helper for dropping snapshots. This walks back up the tree in the path
1423 * to find the first node higher up where we haven't yet gone through
1424 * all the slots
1425 */
Chris Masone089f052007-03-16 16:20:31 -04001426static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root
1427 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05001428{
1429 int i;
1430 int slot;
1431 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04001432 struct btrfs_root_item *root_item = &root->root_item;
1433
Chris Mason234b63a2007-03-13 10:46:10 -04001434 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Mason20524f02007-03-10 06:35:47 -05001435 slot = path->slots[i];
Chris Mason5f39d392007-10-15 16:14:19 -04001436 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
1437 struct extent_buffer *node;
1438 struct btrfs_disk_key disk_key;
1439 node = path->nodes[i];
Chris Mason20524f02007-03-10 06:35:47 -05001440 path->slots[i]++;
1441 *level = i;
Chris Mason9f3a7422007-08-07 15:52:19 -04001442 WARN_ON(*level == 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001443 btrfs_node_key(node, &disk_key, path->slots[i]);
Chris Mason9f3a7422007-08-07 15:52:19 -04001444 memcpy(&root_item->drop_progress,
Chris Mason5f39d392007-10-15 16:14:19 -04001445 &disk_key, sizeof(disk_key));
Chris Mason9f3a7422007-08-07 15:52:19 -04001446 root_item->drop_level = i;
Chris Mason20524f02007-03-10 06:35:47 -05001447 return 0;
1448 } else {
Chris Masone089f052007-03-16 16:20:31 -04001449 ret = btrfs_free_extent(trans, root,
Chris Masondb945352007-10-15 16:15:53 -04001450 path->nodes[*level]->start,
1451 path->nodes[*level]->len, 1);
Chris Mason6407bf62007-03-27 06:33:00 -04001452 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04001453 free_extent_buffer(path->nodes[*level]);
Chris Mason83e15a22007-03-12 09:03:27 -04001454 path->nodes[*level] = NULL;
Chris Mason20524f02007-03-10 06:35:47 -05001455 *level = i + 1;
Chris Mason20524f02007-03-10 06:35:47 -05001456 }
1457 }
1458 return 1;
1459}
1460
Chris Mason9aca1d52007-03-13 11:09:37 -04001461/*
1462 * drop the reference count on the tree rooted at 'snap'. This traverses
1463 * the tree freeing any blocks that have a ref count of zero after being
1464 * decremented.
1465 */
Chris Masone089f052007-03-16 16:20:31 -04001466int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason9f3a7422007-08-07 15:52:19 -04001467 *root)
Chris Mason20524f02007-03-10 06:35:47 -05001468{
Chris Mason3768f362007-03-13 16:47:54 -04001469 int ret = 0;
Chris Mason9aca1d52007-03-13 11:09:37 -04001470 int wret;
Chris Mason20524f02007-03-10 06:35:47 -05001471 int level;
Chris Mason5caf2a02007-04-02 11:20:42 -04001472 struct btrfs_path *path;
Chris Mason20524f02007-03-10 06:35:47 -05001473 int i;
1474 int orig_level;
Chris Mason9f3a7422007-08-07 15:52:19 -04001475 struct btrfs_root_item *root_item = &root->root_item;
Chris Mason20524f02007-03-10 06:35:47 -05001476
Chris Mason5caf2a02007-04-02 11:20:42 -04001477 path = btrfs_alloc_path();
1478 BUG_ON(!path);
Chris Mason20524f02007-03-10 06:35:47 -05001479
Chris Mason5f39d392007-10-15 16:14:19 -04001480 level = btrfs_header_level(root->node);
Chris Mason20524f02007-03-10 06:35:47 -05001481 orig_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04001482 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
1483 path->nodes[level] = root->node;
Chris Masonf510cfe2007-10-15 16:14:48 -04001484 extent_buffer_get(root->node);
Chris Mason9f3a7422007-08-07 15:52:19 -04001485 path->slots[level] = 0;
1486 } else {
1487 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001488 struct btrfs_disk_key found_key;
1489 struct extent_buffer *node;
Chris Mason6702ed42007-08-07 16:15:09 -04001490
Chris Mason9f3a7422007-08-07 15:52:19 -04001491 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Chris Mason6702ed42007-08-07 16:15:09 -04001492 level = root_item->drop_level;
1493 path->lowest_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04001494 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason6702ed42007-08-07 16:15:09 -04001495 if (wret < 0) {
Chris Mason9f3a7422007-08-07 15:52:19 -04001496 ret = wret;
1497 goto out;
1498 }
Chris Mason5f39d392007-10-15 16:14:19 -04001499 node = path->nodes[level];
1500 btrfs_node_key(node, &found_key, path->slots[level]);
1501 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
1502 sizeof(found_key)));
Chris Mason9f3a7422007-08-07 15:52:19 -04001503 }
Chris Mason20524f02007-03-10 06:35:47 -05001504 while(1) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001505 wret = walk_down_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04001506 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05001507 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04001508 if (wret < 0)
1509 ret = wret;
1510
Chris Mason5caf2a02007-04-02 11:20:42 -04001511 wret = walk_up_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04001512 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05001513 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04001514 if (wret < 0)
1515 ret = wret;
Chris Mason409eb952007-08-08 20:17:12 -04001516 ret = -EAGAIN;
Chris Mason409eb952007-08-08 20:17:12 -04001517 break;
Chris Mason20524f02007-03-10 06:35:47 -05001518 }
Chris Mason83e15a22007-03-12 09:03:27 -04001519 for (i = 0; i <= orig_level; i++) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001520 if (path->nodes[i]) {
Chris Mason5f39d392007-10-15 16:14:19 -04001521 free_extent_buffer(path->nodes[i]);
Chris Mason0f827312007-10-15 16:18:56 -04001522 path->nodes[i] = NULL;
Chris Mason83e15a22007-03-12 09:03:27 -04001523 }
Chris Mason20524f02007-03-10 06:35:47 -05001524 }
Chris Mason9f3a7422007-08-07 15:52:19 -04001525out:
Chris Mason5caf2a02007-04-02 11:20:42 -04001526 btrfs_free_path(path);
Chris Mason9aca1d52007-03-13 11:09:37 -04001527 return ret;
Chris Mason20524f02007-03-10 06:35:47 -05001528}
Chris Mason9078a3e2007-04-26 16:46:15 -04001529
Chris Masonbe744172007-05-06 10:15:01 -04001530int btrfs_free_block_groups(struct btrfs_fs_info *info)
1531{
Chris Masonf510cfe2007-10-15 16:14:48 -04001532 u64 start;
1533 u64 end;
Yanb97f9202007-11-01 11:28:41 -04001534 u64 ptr;
Chris Mason96b51792007-10-15 16:15:19 -04001535 int ret;
Chris Mason96b51792007-10-15 16:15:19 -04001536 while(1) {
1537 ret = find_first_extent_bit(&info->block_group_cache, 0,
1538 &start, &end, (unsigned int)-1);
1539 if (ret)
1540 break;
Yanb97f9202007-11-01 11:28:41 -04001541 ret = get_state_private(&info->block_group_cache, start, &ptr);
1542 if (!ret)
1543 kfree((void *)(unsigned long)ptr);
Chris Mason96b51792007-10-15 16:15:19 -04001544 clear_extent_bits(&info->block_group_cache, start,
1545 end, (unsigned int)-1, GFP_NOFS);
1546 }
Chris Masone37c9e62007-05-09 20:13:14 -04001547 while(1) {
Chris Masonf510cfe2007-10-15 16:14:48 -04001548 ret = find_first_extent_bit(&info->free_space_cache, 0,
1549 &start, &end, EXTENT_DIRTY);
1550 if (ret)
Chris Masone37c9e62007-05-09 20:13:14 -04001551 break;
Chris Masonf510cfe2007-10-15 16:14:48 -04001552 clear_extent_dirty(&info->free_space_cache, start,
1553 end, GFP_NOFS);
Chris Masone37c9e62007-05-09 20:13:14 -04001554 }
Chris Masonbe744172007-05-06 10:15:01 -04001555 return 0;
1556}
1557
Chris Mason9078a3e2007-04-26 16:46:15 -04001558int btrfs_read_block_groups(struct btrfs_root *root)
1559{
1560 struct btrfs_path *path;
1561 int ret;
1562 int err = 0;
Chris Mason96b51792007-10-15 16:15:19 -04001563 int bit;
Chris Mason9078a3e2007-04-26 16:46:15 -04001564 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04001565 struct btrfs_fs_info *info = root->fs_info;
Chris Mason96b51792007-10-15 16:15:19 -04001566 struct extent_map_tree *block_group_cache;
Chris Mason9078a3e2007-04-26 16:46:15 -04001567 struct btrfs_key key;
1568 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -04001569 struct extent_buffer *leaf;
Chris Mason96b51792007-10-15 16:15:19 -04001570
1571 block_group_cache = &info->block_group_cache;
Chris Mason9078a3e2007-04-26 16:46:15 -04001572
Chris Masonbe744172007-05-06 10:15:01 -04001573 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04001574 key.objectid = 0;
Chris Masondb945352007-10-15 16:15:53 -04001575 key.offset = BTRFS_BLOCK_GROUP_SIZE;
Chris Mason9078a3e2007-04-26 16:46:15 -04001576 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
1577
1578 path = btrfs_alloc_path();
1579 if (!path)
1580 return -ENOMEM;
1581
1582 while(1) {
Chris Masonbe744172007-05-06 10:15:01 -04001583 ret = btrfs_search_slot(NULL, info->extent_root,
Chris Mason9078a3e2007-04-26 16:46:15 -04001584 &key, path, 0, 0);
1585 if (ret != 0) {
1586 err = ret;
1587 break;
1588 }
Chris Mason5f39d392007-10-15 16:14:19 -04001589 leaf = path->nodes[0];
1590 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason9078a3e2007-04-26 16:46:15 -04001591 cache = kmalloc(sizeof(*cache), GFP_NOFS);
1592 if (!cache) {
1593 err = -1;
1594 break;
1595 }
Chris Mason3e1ad542007-05-07 20:03:49 -04001596
Chris Mason5f39d392007-10-15 16:14:19 -04001597 read_extent_buffer(leaf, &cache->item,
1598 btrfs_item_ptr_offset(leaf, path->slots[0]),
1599 sizeof(cache->item));
Chris Mason9078a3e2007-04-26 16:46:15 -04001600 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Masone37c9e62007-05-09 20:13:14 -04001601 cache->cached = 0;
Yan324ae4d2007-11-16 14:57:08 -05001602 cache->pinned = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04001603 key.objectid = found_key.objectid + found_key.offset;
1604 btrfs_release_path(root, path);
Chris Mason5f39d392007-10-15 16:14:19 -04001605
Chris Masonf84a8b32007-11-06 10:26:29 -05001606 if (cache->item.flags & BTRFS_BLOCK_GROUP_MIXED) {
1607 bit = BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA;
1608 cache->data = BTRFS_BLOCK_GROUP_MIXED;
1609 } else if (cache->item.flags & BTRFS_BLOCK_GROUP_DATA) {
Chris Mason96b51792007-10-15 16:15:19 -04001610 bit = BLOCK_GROUP_DATA;
Chris Masonf84a8b32007-11-06 10:26:29 -05001611 cache->data = BTRFS_BLOCK_GROUP_DATA;
Chris Mason96b51792007-10-15 16:15:19 -04001612 } else {
1613 bit = BLOCK_GROUP_METADATA;
1614 cache->data = 0;
Chris Mason31f3c992007-04-30 15:25:45 -04001615 }
Chris Mason96b51792007-10-15 16:15:19 -04001616
1617 /* use EXTENT_LOCKED to prevent merging */
1618 set_extent_bits(block_group_cache, found_key.objectid,
1619 found_key.objectid + found_key.offset - 1,
1620 bit | EXTENT_LOCKED, GFP_NOFS);
1621 set_state_private(block_group_cache, found_key.objectid,
Jens Axboeae2f5412007-10-19 09:22:59 -04001622 (unsigned long)cache);
Chris Mason96b51792007-10-15 16:15:19 -04001623
Chris Mason9078a3e2007-04-26 16:46:15 -04001624 if (key.objectid >=
Chris Masondb945352007-10-15 16:15:53 -04001625 btrfs_super_total_bytes(&info->super_copy))
Chris Mason9078a3e2007-04-26 16:46:15 -04001626 break;
1627 }
1628
1629 btrfs_free_path(path);
1630 return 0;
1631}