blob: 8ab4954f6ad0b73f7278f21194ce2da8325cd865 [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
Chris Mason00f5c792007-11-30 10:09:33 -050048 if (!block_group)
49 return 0;
50
Chris Masone37c9e62007-05-09 20:13:14 -040051 root = root->fs_info->extent_root;
Chris Masonf510cfe2007-10-15 16:14:48 -040052 free_space_cache = &root->fs_info->free_space_cache;
Chris Masone37c9e62007-05-09 20:13:14 -040053
54 if (block_group->cached)
55 return 0;
Chris Masonf510cfe2007-10-15 16:14:48 -040056
Chris Masone37c9e62007-05-09 20:13:14 -040057 path = btrfs_alloc_path();
58 if (!path)
59 return -ENOMEM;
Yan7d7d6062007-09-14 16:15:28 -040060
Chris Mason2cc58cf2007-08-27 16:49:44 -040061 path->reada = 2;
Yan7d7d6062007-09-14 16:15:28 -040062 first_free = block_group->key.objectid;
Chris Masone37c9e62007-05-09 20:13:14 -040063 key.objectid = block_group->key.objectid;
Chris Masone37c9e62007-05-09 20:13:14 -040064 key.offset = 0;
Yan7d7d6062007-09-14 16:15:28 -040065
Chris Masone37c9e62007-05-09 20:13:14 -040066 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
67 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Yan7d7d6062007-09-14 16:15:28 -040068
Chris Masone37c9e62007-05-09 20:13:14 -040069 if (ret < 0)
70 return ret;
Yan7d7d6062007-09-14 16:15:28 -040071
Chris Masone37c9e62007-05-09 20:13:14 -040072 if (ret && path->slots[0] > 0)
73 path->slots[0]--;
Yan7d7d6062007-09-14 16:15:28 -040074
Chris Masone37c9e62007-05-09 20:13:14 -040075 while(1) {
Chris Mason5f39d392007-10-15 16:14:19 -040076 leaf = path->nodes[0];
Chris Masone37c9e62007-05-09 20:13:14 -040077 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -040078 if (slot >= btrfs_header_nritems(leaf)) {
Chris Masone37c9e62007-05-09 20:13:14 -040079 ret = btrfs_next_leaf(root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -040080 if (ret < 0)
81 goto err;
Chris Masonde428b62007-05-18 13:28:27 -040082 if (ret == 0) {
Chris Masone37c9e62007-05-09 20:13:14 -040083 continue;
Chris Masonde428b62007-05-18 13:28:27 -040084 } else {
Chris Masone37c9e62007-05-09 20:13:14 -040085 break;
86 }
87 }
Yan7d7d6062007-09-14 16:15:28 -040088
Chris Mason5f39d392007-10-15 16:14:19 -040089 btrfs_item_key_to_cpu(leaf, &key, slot);
Yan7d7d6062007-09-14 16:15:28 -040090 if (key.objectid < block_group->key.objectid) {
91 if (key.objectid + key.offset > first_free)
92 first_free = key.objectid + key.offset;
93 goto next;
94 }
95
Chris Masone37c9e62007-05-09 20:13:14 -040096 if (key.objectid >= block_group->key.objectid +
97 block_group->key.offset) {
Yan7d7d6062007-09-14 16:15:28 -040098 break;
99 }
100
101 if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) {
102 if (!found) {
103 last = first_free;
104 found = 1;
Chris Masone37c9e62007-05-09 20:13:14 -0400105 }
Chris Masonf510cfe2007-10-15 16:14:48 -0400106 if (key.objectid > last) {
107 hole_size = key.objectid - last;
108 set_extent_dirty(free_space_cache, last,
109 last + hole_size - 1,
110 GFP_NOFS);
Chris Masone37c9e62007-05-09 20:13:14 -0400111 }
Yan7d7d6062007-09-14 16:15:28 -0400112 last = key.objectid + key.offset;
Chris Masone37c9e62007-05-09 20:13:14 -0400113 }
Yan7d7d6062007-09-14 16:15:28 -0400114next:
Chris Masone37c9e62007-05-09 20:13:14 -0400115 path->slots[0]++;
116 }
117
Yan7d7d6062007-09-14 16:15:28 -0400118 if (!found)
119 last = first_free;
120 if (block_group->key.objectid +
121 block_group->key.offset > last) {
122 hole_size = block_group->key.objectid +
123 block_group->key.offset - last;
Chris Masonf510cfe2007-10-15 16:14:48 -0400124 set_extent_dirty(free_space_cache, last,
125 last + hole_size - 1, GFP_NOFS);
Yan7d7d6062007-09-14 16:15:28 -0400126 }
Chris Masone37c9e62007-05-09 20:13:14 -0400127 block_group->cached = 1;
Chris Mason54aa1f42007-06-22 14:16:25 -0400128err:
Chris Masone37c9e62007-05-09 20:13:14 -0400129 btrfs_free_path(path);
130 return 0;
131}
132
Chris Mason5276aed2007-06-11 21:33:38 -0400133struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
134 btrfs_fs_info *info,
Chris Masondb945352007-10-15 16:15:53 -0400135 u64 bytenr)
Chris Masonbe744172007-05-06 10:15:01 -0400136{
Chris Mason96b51792007-10-15 16:15:19 -0400137 struct extent_map_tree *block_group_cache;
138 struct btrfs_block_group_cache *block_group = NULL;
139 u64 ptr;
140 u64 start;
141 u64 end;
Chris Masonbe744172007-05-06 10:15:01 -0400142 int ret;
143
Chris Mason96b51792007-10-15 16:15:19 -0400144 block_group_cache = &info->block_group_cache;
145 ret = find_first_extent_bit(block_group_cache,
Chris Masondb945352007-10-15 16:15:53 -0400146 bytenr, &start, &end,
Chris Mason96b51792007-10-15 16:15:19 -0400147 BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA);
Chris Masonbe744172007-05-06 10:15:01 -0400148 if (ret) {
Chris Mason96b51792007-10-15 16:15:19 -0400149 return NULL;
Chris Masonbe744172007-05-06 10:15:01 -0400150 }
Chris Mason96b51792007-10-15 16:15:19 -0400151 ret = get_state_private(block_group_cache, start, &ptr);
152 if (ret)
153 return NULL;
154
Jens Axboeae2f5412007-10-19 09:22:59 -0400155 block_group = (struct btrfs_block_group_cache *)(unsigned long)ptr;
Yan5cf66422007-11-16 14:57:09 -0500156 if (block_group->key.objectid <= bytenr && bytenr <
Chris Mason96b51792007-10-15 16:15:19 -0400157 block_group->key.objectid + block_group->key.offset)
158 return block_group;
Chris Masonbe744172007-05-06 10:15:01 -0400159 return NULL;
160}
Chris Masone37c9e62007-05-09 20:13:14 -0400161static u64 find_search_start(struct btrfs_root *root,
162 struct btrfs_block_group_cache **cache_ret,
Yan5e5745d2007-11-16 14:57:09 -0500163 u64 search_start, int num,
164 int data, int full_scan)
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 Mason00f5c792007-11-30 10:09:33 -0500174 if (!cache) {
Chris Mason1a2b2ac2007-12-04 13:18:24 -0500175 goto out;
Chris Mason00f5c792007-11-30 10:09:33 -0500176 }
Chris Masone37c9e62007-05-09 20:13:14 -0400177again:
Chris Mason54aa1f42007-06-22 14:16:25 -0400178 ret = cache_block_group(root, cache);
179 if (ret)
180 goto out;
Chris Masonf84a8b32007-11-06 10:26:29 -0500181
Chris Masone19caa52007-10-15 16:17:44 -0400182 last = max(search_start, cache->key.objectid);
183
Chris Masone37c9e62007-05-09 20:13:14 -0400184 while(1) {
Chris Masonf510cfe2007-10-15 16:14:48 -0400185 ret = find_first_extent_bit(&root->fs_info->free_space_cache,
186 last, &start, &end, EXTENT_DIRTY);
Chris Masone19caa52007-10-15 16:17:44 -0400187 if (ret) {
Chris Mason257d0ce2007-11-07 21:08:16 -0500188 if (!cache_miss)
189 cache_miss = last;
Chris Masone19caa52007-10-15 16:17:44 -0400190 goto new_group;
191 }
Chris Masonf510cfe2007-10-15 16:14:48 -0400192
193 start = max(last, start);
194 last = end + 1;
Chris Mason257d0ce2007-11-07 21:08:16 -0500195 if (last - start < num) {
196 if (last == cache->key.objectid + cache->key.offset)
197 cache_miss = start;
Chris Masonf510cfe2007-10-15 16:14:48 -0400198 continue;
Chris Mason257d0ce2007-11-07 21:08:16 -0500199 }
200 if (data != BTRFS_BLOCK_GROUP_MIXED &&
Yan5cf66422007-11-16 14:57:09 -0500201 start + num > cache->key.objectid + cache->key.offset)
Chris Masone37c9e62007-05-09 20:13:14 -0400202 goto new_group;
Chris Masonf510cfe2007-10-15 16:14:48 -0400203 return start;
Chris Masone37c9e62007-05-09 20:13:14 -0400204 }
205out:
Chris Mason1a2b2ac2007-12-04 13:18:24 -0500206 cache = btrfs_lookup_block_group(root->fs_info, search_start);
207 if (!cache) {
208 printk("Unable to find block group for %Lu\n",
209 search_start);
210 WARN_ON(1);
211 return search_start;
212 }
Chris Mason1a5bc162007-10-15 16:15:26 -0400213 return search_start;
Chris Masone37c9e62007-05-09 20:13:14 -0400214
215new_group:
Chris Masone19caa52007-10-15 16:17:44 -0400216 last = cache->key.objectid + cache->key.offset;
Chris Masonf84a8b32007-11-06 10:26:29 -0500217wrapped:
Chris Masone19caa52007-10-15 16:17:44 -0400218 cache = btrfs_lookup_block_group(root->fs_info, last);
Chris Masone37c9e62007-05-09 20:13:14 -0400219 if (!cache) {
Chris Mason0e4de582007-11-26 10:55:49 -0500220no_cache:
Chris Masonf84a8b32007-11-06 10:26:29 -0500221 if (!wrapped) {
222 wrapped = 1;
223 last = search_start;
224 data = BTRFS_BLOCK_GROUP_MIXED;
225 goto wrapped;
226 }
Chris Mason1a2b2ac2007-12-04 13:18:24 -0500227 goto out;
Chris Masone37c9e62007-05-09 20:13:14 -0400228 }
Chris Mason257d0ce2007-11-07 21:08:16 -0500229 if (cache_miss && !cache->cached) {
230 cache_block_group(root, cache);
231 last = cache_miss;
Chris Mason257d0ce2007-11-07 21:08:16 -0500232 cache = btrfs_lookup_block_group(root->fs_info, last);
233 }
Chris Mason1a2b2ac2007-12-04 13:18:24 -0500234 cache = btrfs_find_block_group(root, cache, last, data, 0);
Chris Mason0e4de582007-11-26 10:55:49 -0500235 if (!cache)
236 goto no_cache;
Chris Masone37c9e62007-05-09 20:13:14 -0400237 *cache_ret = cache;
Chris Mason257d0ce2007-11-07 21:08:16 -0500238 cache_miss = 0;
Chris Masone37c9e62007-05-09 20:13:14 -0400239 goto again;
240}
241
Chris Mason84f54cf2007-06-12 07:43:08 -0400242static u64 div_factor(u64 num, int factor)
243{
Chris Mason257d0ce2007-11-07 21:08:16 -0500244 if (factor == 10)
245 return num;
Chris Mason84f54cf2007-06-12 07:43:08 -0400246 num *= factor;
247 do_div(num, 10);
248 return num;
249}
250
Chris Mason31f3c992007-04-30 15:25:45 -0400251struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
252 struct btrfs_block_group_cache
Chris Masonbe744172007-05-06 10:15:01 -0400253 *hint, u64 search_start,
Chris Masonde428b62007-05-18 13:28:27 -0400254 int data, int owner)
Chris Masoncd1bc462007-04-27 10:08:34 -0400255{
Chris Mason96b51792007-10-15 16:15:19 -0400256 struct btrfs_block_group_cache *cache;
257 struct extent_map_tree *block_group_cache;
Chris Mason31f3c992007-04-30 15:25:45 -0400258 struct btrfs_block_group_cache *found_group = NULL;
Chris Masoncd1bc462007-04-27 10:08:34 -0400259 struct btrfs_fs_info *info = root->fs_info;
260 u64 used;
Chris Mason31f3c992007-04-30 15:25:45 -0400261 u64 last = 0;
262 u64 hint_last;
Chris Mason96b51792007-10-15 16:15:19 -0400263 u64 start;
264 u64 end;
265 u64 free_check;
266 u64 ptr;
267 int bit;
Chris Masoncd1bc462007-04-27 10:08:34 -0400268 int ret;
Chris Mason31f3c992007-04-30 15:25:45 -0400269 int full_search = 0;
Chris Masonde428b62007-05-18 13:28:27 -0400270 int factor = 8;
Chris Mason1e2677e2007-05-29 16:52:18 -0400271 int data_swap = 0;
Chris Masonde428b62007-05-18 13:28:27 -0400272
Chris Mason96b51792007-10-15 16:15:19 -0400273 block_group_cache = &info->block_group_cache;
274
Chris Masonde428b62007-05-18 13:28:27 -0400275 if (!owner)
Chris Masonf84a8b32007-11-06 10:26:29 -0500276 factor = 8;
Chris Masonbe744172007-05-06 10:15:01 -0400277
Chris Mason257d0ce2007-11-07 21:08:16 -0500278 if (data == BTRFS_BLOCK_GROUP_MIXED) {
Chris Masonf84a8b32007-11-06 10:26:29 -0500279 bit = BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA;
Chris Mason257d0ce2007-11-07 21:08:16 -0500280 factor = 10;
281 } else if (data)
Chris Mason96b51792007-10-15 16:15:19 -0400282 bit = BLOCK_GROUP_DATA;
283 else
284 bit = BLOCK_GROUP_METADATA;
Chris Masonbe744172007-05-06 10:15:01 -0400285
286 if (search_start) {
287 struct btrfs_block_group_cache *shint;
Chris Mason5276aed2007-06-11 21:33:38 -0400288 shint = btrfs_lookup_block_group(info, search_start);
Chris Masonf84a8b32007-11-06 10:26:29 -0500289 if (shint && (shint->data == data ||
290 shint->data == BTRFS_BLOCK_GROUP_MIXED)) {
Chris Masonbe744172007-05-06 10:15:01 -0400291 used = btrfs_block_group_used(&shint->item);
Yan324ae4d2007-11-16 14:57:08 -0500292 if (used + shint->pinned <
293 div_factor(shint->key.offset, factor)) {
Chris Masonbe744172007-05-06 10:15:01 -0400294 return shint;
295 }
296 }
297 }
Chris Masonf84a8b32007-11-06 10:26:29 -0500298 if (hint && (hint->data == data ||
299 hint->data == BTRFS_BLOCK_GROUP_MIXED)) {
Chris Mason31f3c992007-04-30 15:25:45 -0400300 used = btrfs_block_group_used(&hint->item);
Yan324ae4d2007-11-16 14:57:08 -0500301 if (used + hint->pinned <
302 div_factor(hint->key.offset, factor)) {
Chris Mason31f3c992007-04-30 15:25:45 -0400303 return hint;
304 }
Chris Masone19caa52007-10-15 16:17:44 -0400305 last = hint->key.objectid + hint->key.offset;
Chris Mason31f3c992007-04-30 15:25:45 -0400306 hint_last = last;
307 } else {
Chris Masone37c9e62007-05-09 20:13:14 -0400308 if (hint)
309 hint_last = max(hint->key.objectid, search_start);
310 else
311 hint_last = search_start;
312
313 last = hint_last;
Chris Mason31f3c992007-04-30 15:25:45 -0400314 }
Chris Mason31f3c992007-04-30 15:25:45 -0400315again:
Chris Masoncd1bc462007-04-27 10:08:34 -0400316 while(1) {
Chris Mason96b51792007-10-15 16:15:19 -0400317 ret = find_first_extent_bit(block_group_cache, last,
318 &start, &end, bit);
319 if (ret)
Chris Masoncd1bc462007-04-27 10:08:34 -0400320 break;
Chris Mason96b51792007-10-15 16:15:19 -0400321
322 ret = get_state_private(block_group_cache, start, &ptr);
323 if (ret)
324 break;
325
Jens Axboeae2f5412007-10-19 09:22:59 -0400326 cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
Chris Mason96b51792007-10-15 16:15:19 -0400327 last = cache->key.objectid + cache->key.offset;
328 used = btrfs_block_group_used(&cache->item);
329
330 if (full_search)
331 free_check = cache->key.offset;
332 else
333 free_check = div_factor(cache->key.offset, factor);
Yan324ae4d2007-11-16 14:57:08 -0500334 if (used + cache->pinned < free_check) {
Chris Mason96b51792007-10-15 16:15:19 -0400335 found_group = cache;
336 goto found;
Chris Masoncd1bc462007-04-27 10:08:34 -0400337 }
Chris Masonde428b62007-05-18 13:28:27 -0400338 cond_resched();
Chris Masoncd1bc462007-04-27 10:08:34 -0400339 }
Chris Mason31f3c992007-04-30 15:25:45 -0400340 if (!full_search) {
Chris Masonbe744172007-05-06 10:15:01 -0400341 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -0400342 full_search = 1;
343 goto again;
344 }
Chris Mason1e2677e2007-05-29 16:52:18 -0400345 if (!data_swap) {
Chris Mason1e2677e2007-05-29 16:52:18 -0400346 data_swap = 1;
Chris Mason96b51792007-10-15 16:15:19 -0400347 bit = BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA;
Chris Mason1e2677e2007-05-29 16:52:18 -0400348 last = search_start;
349 goto again;
350 }
Chris Masonbe744172007-05-06 10:15:01 -0400351found:
Chris Mason31f3c992007-04-30 15:25:45 -0400352 return found_group;
Chris Masoncd1bc462007-04-27 10:08:34 -0400353}
354
Chris Masonb18c6682007-04-17 13:26:50 -0400355int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
356 struct btrfs_root *root,
Chris Masondb945352007-10-15 16:15:53 -0400357 u64 bytenr, u64 num_bytes)
Chris Mason02217ed2007-03-02 16:08:05 -0500358{
Chris Mason5caf2a02007-04-02 11:20:42 -0400359 struct btrfs_path *path;
Chris Mason02217ed2007-03-02 16:08:05 -0500360 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400361 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400362 struct extent_buffer *l;
Chris Mason234b63a2007-03-13 10:46:10 -0400363 struct btrfs_extent_item *item;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400364 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500365
Chris Masondb945352007-10-15 16:15:53 -0400366 WARN_ON(num_bytes < root->sectorsize);
Chris Mason5caf2a02007-04-02 11:20:42 -0400367 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -0400368 if (!path)
369 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -0400370
Chris Masondb945352007-10-15 16:15:53 -0400371 key.objectid = bytenr;
Chris Mason62e27492007-03-15 12:56:47 -0400372 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masondb945352007-10-15 16:15:53 -0400373 key.offset = num_bytes;
Chris Mason5caf2a02007-04-02 11:20:42 -0400374 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -0400375 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400376 if (ret < 0)
377 return ret;
Chris Masona429e512007-04-18 16:15:28 -0400378 if (ret != 0) {
Chris Masona28ec192007-03-06 20:08:01 -0500379 BUG();
Chris Masona429e512007-04-18 16:15:28 -0400380 }
Chris Mason02217ed2007-03-02 16:08:05 -0500381 BUG_ON(ret != 0);
Chris Mason5f39d392007-10-15 16:14:19 -0400382 l = path->nodes[0];
Chris Mason5caf2a02007-04-02 11:20:42 -0400383 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400384 refs = btrfs_extent_refs(l, item);
385 btrfs_set_extent_refs(l, item, refs + 1);
Chris Mason5caf2a02007-04-02 11:20:42 -0400386 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masona28ec192007-03-06 20:08:01 -0500387
Chris Mason5caf2a02007-04-02 11:20:42 -0400388 btrfs_release_path(root->fs_info->extent_root, path);
389 btrfs_free_path(path);
Chris Mason9f5fae22007-03-20 14:38:32 -0400390 finish_current_insert(trans, root->fs_info->extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -0400391 del_pending_extents(trans, root->fs_info->extent_root);
Chris Mason02217ed2007-03-02 16:08:05 -0500392 return 0;
393}
394
Chris Masone9d0b132007-08-10 14:06:19 -0400395int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
396 struct btrfs_root *root)
397{
398 finish_current_insert(trans, root->fs_info->extent_root);
399 del_pending_extents(trans, root->fs_info->extent_root);
400 return 0;
401}
402
Chris Masonb18c6682007-04-17 13:26:50 -0400403static int lookup_extent_ref(struct btrfs_trans_handle *trans,
Chris Masondb945352007-10-15 16:15:53 -0400404 struct btrfs_root *root, u64 bytenr,
405 u64 num_bytes, u32 *refs)
Chris Masona28ec192007-03-06 20:08:01 -0500406{
Chris Mason5caf2a02007-04-02 11:20:42 -0400407 struct btrfs_path *path;
Chris Masona28ec192007-03-06 20:08:01 -0500408 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400409 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400410 struct extent_buffer *l;
Chris Mason234b63a2007-03-13 10:46:10 -0400411 struct btrfs_extent_item *item;
Chris Mason5caf2a02007-04-02 11:20:42 -0400412
Chris Masondb945352007-10-15 16:15:53 -0400413 WARN_ON(num_bytes < root->sectorsize);
Chris Mason5caf2a02007-04-02 11:20:42 -0400414 path = btrfs_alloc_path();
Chris Masondb945352007-10-15 16:15:53 -0400415 key.objectid = bytenr;
416 key.offset = num_bytes;
Chris Mason62e27492007-03-15 12:56:47 -0400417 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Mason5caf2a02007-04-02 11:20:42 -0400418 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -0400419 0, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400420 if (ret < 0)
421 goto out;
Chris Mason5f39d392007-10-15 16:14:19 -0400422 if (ret != 0) {
423 btrfs_print_leaf(root, path->nodes[0]);
Chris Masondb945352007-10-15 16:15:53 -0400424 printk("failed to find block number %Lu\n", bytenr);
Chris Masona28ec192007-03-06 20:08:01 -0500425 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -0400426 }
427 l = path->nodes[0];
Chris Mason5caf2a02007-04-02 11:20:42 -0400428 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400429 *refs = btrfs_extent_refs(l, item);
Chris Mason54aa1f42007-06-22 14:16:25 -0400430out:
Chris Mason5caf2a02007-04-02 11:20:42 -0400431 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -0500432 return 0;
433}
434
Chris Masonc5739bb2007-04-10 09:27:04 -0400435int btrfs_inc_root_ref(struct btrfs_trans_handle *trans,
436 struct btrfs_root *root)
437{
Chris Masondb945352007-10-15 16:15:53 -0400438 return btrfs_inc_extent_ref(trans, root, root->node->start,
439 root->node->len);
Chris Masonc5739bb2007-04-10 09:27:04 -0400440}
441
Chris Masone089f052007-03-16 16:20:31 -0400442int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400443 struct extent_buffer *buf)
Chris Mason02217ed2007-03-02 16:08:05 -0500444{
Chris Masondb945352007-10-15 16:15:53 -0400445 u64 bytenr;
Chris Mason5f39d392007-10-15 16:14:19 -0400446 u32 nritems;
447 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -0400448 struct btrfs_file_extent_item *fi;
Chris Mason02217ed2007-03-02 16:08:05 -0500449 int i;
Chris Masondb945352007-10-15 16:15:53 -0400450 int level;
Chris Mason6407bf62007-03-27 06:33:00 -0400451 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -0400452 int faili;
453 int err;
Chris Masona28ec192007-03-06 20:08:01 -0500454
Chris Mason3768f362007-03-13 16:47:54 -0400455 if (!root->ref_cows)
Chris Masona28ec192007-03-06 20:08:01 -0500456 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400457
Chris Masondb945352007-10-15 16:15:53 -0400458 level = btrfs_header_level(buf);
Chris Mason5f39d392007-10-15 16:14:19 -0400459 nritems = btrfs_header_nritems(buf);
460 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -0400461 if (level == 0) {
462 u64 disk_bytenr;
Chris Mason5f39d392007-10-15 16:14:19 -0400463 btrfs_item_key_to_cpu(buf, &key, i);
464 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason6407bf62007-03-27 06:33:00 -0400465 continue;
Chris Mason5f39d392007-10-15 16:14:19 -0400466 fi = btrfs_item_ptr(buf, i,
Chris Mason6407bf62007-03-27 06:33:00 -0400467 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400468 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason236454d2007-04-19 13:37:44 -0400469 BTRFS_FILE_EXTENT_INLINE)
470 continue;
Chris Masondb945352007-10-15 16:15:53 -0400471 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
472 if (disk_bytenr == 0)
Chris Mason3a686372007-05-24 13:35:57 -0400473 continue;
Chris Masondb945352007-10-15 16:15:53 -0400474 ret = btrfs_inc_extent_ref(trans, root, disk_bytenr,
475 btrfs_file_extent_disk_num_bytes(buf, fi));
Chris Mason54aa1f42007-06-22 14:16:25 -0400476 if (ret) {
477 faili = i;
478 goto fail;
479 }
Chris Mason6407bf62007-03-27 06:33:00 -0400480 } else {
Chris Masondb945352007-10-15 16:15:53 -0400481 bytenr = btrfs_node_blockptr(buf, i);
482 ret = btrfs_inc_extent_ref(trans, root, bytenr,
483 btrfs_level_size(root, level - 1));
Chris Mason54aa1f42007-06-22 14:16:25 -0400484 if (ret) {
485 faili = i;
486 goto fail;
487 }
Chris Mason6407bf62007-03-27 06:33:00 -0400488 }
Chris Mason02217ed2007-03-02 16:08:05 -0500489 }
490 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -0400491fail:
Chris Masonccd467d2007-06-28 15:57:36 -0400492 WARN_ON(1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400493 for (i =0; i < faili; i++) {
Chris Masondb945352007-10-15 16:15:53 -0400494 if (level == 0) {
495 u64 disk_bytenr;
Chris Mason5f39d392007-10-15 16:14:19 -0400496 btrfs_item_key_to_cpu(buf, &key, i);
497 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -0400498 continue;
Chris Mason5f39d392007-10-15 16:14:19 -0400499 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -0400500 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400501 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -0400502 BTRFS_FILE_EXTENT_INLINE)
503 continue;
Chris Masondb945352007-10-15 16:15:53 -0400504 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
505 if (disk_bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -0400506 continue;
Chris Masondb945352007-10-15 16:15:53 -0400507 err = btrfs_free_extent(trans, root, disk_bytenr,
508 btrfs_file_extent_disk_num_bytes(buf,
Chris Mason5f39d392007-10-15 16:14:19 -0400509 fi), 0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400510 BUG_ON(err);
511 } else {
Chris Masondb945352007-10-15 16:15:53 -0400512 bytenr = btrfs_node_blockptr(buf, i);
513 err = btrfs_free_extent(trans, root, bytenr,
514 btrfs_level_size(root, level - 1), 0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400515 BUG_ON(err);
516 }
517 }
518 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -0500519}
520
Chris Mason9078a3e2007-04-26 16:46:15 -0400521static int write_one_cache_group(struct btrfs_trans_handle *trans,
522 struct btrfs_root *root,
523 struct btrfs_path *path,
524 struct btrfs_block_group_cache *cache)
525{
526 int ret;
527 int pending_ret;
528 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -0400529 unsigned long bi;
530 struct extent_buffer *leaf;
Chris Mason9078a3e2007-04-26 16:46:15 -0400531
Chris Mason9078a3e2007-04-26 16:46:15 -0400532 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400533 if (ret < 0)
534 goto fail;
Chris Mason9078a3e2007-04-26 16:46:15 -0400535 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -0400536
537 leaf = path->nodes[0];
538 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
539 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
540 btrfs_mark_buffer_dirty(leaf);
Chris Mason9078a3e2007-04-26 16:46:15 -0400541 btrfs_release_path(extent_root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -0400542fail:
Chris Mason9078a3e2007-04-26 16:46:15 -0400543 finish_current_insert(trans, extent_root);
544 pending_ret = del_pending_extents(trans, extent_root);
545 if (ret)
546 return ret;
547 if (pending_ret)
548 return pending_ret;
549 return 0;
550
551}
552
Chris Mason96b51792007-10-15 16:15:19 -0400553int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
554 struct btrfs_root *root)
Chris Mason9078a3e2007-04-26 16:46:15 -0400555{
Chris Mason96b51792007-10-15 16:15:19 -0400556 struct extent_map_tree *block_group_cache;
557 struct btrfs_block_group_cache *cache;
Chris Mason9078a3e2007-04-26 16:46:15 -0400558 int ret;
559 int err = 0;
560 int werr = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -0400561 struct btrfs_path *path;
Chris Mason96b51792007-10-15 16:15:19 -0400562 u64 last = 0;
563 u64 start;
564 u64 end;
565 u64 ptr;
Chris Mason9078a3e2007-04-26 16:46:15 -0400566
Chris Mason96b51792007-10-15 16:15:19 -0400567 block_group_cache = &root->fs_info->block_group_cache;
Chris Mason9078a3e2007-04-26 16:46:15 -0400568 path = btrfs_alloc_path();
569 if (!path)
570 return -ENOMEM;
571
572 while(1) {
Chris Mason96b51792007-10-15 16:15:19 -0400573 ret = find_first_extent_bit(block_group_cache, last,
574 &start, &end, BLOCK_GROUP_DIRTY);
575 if (ret)
Chris Mason9078a3e2007-04-26 16:46:15 -0400576 break;
Chris Mason54aa1f42007-06-22 14:16:25 -0400577
Chris Mason96b51792007-10-15 16:15:19 -0400578 last = end + 1;
579 ret = get_state_private(block_group_cache, start, &ptr);
580 if (ret)
581 break;
582
Jens Axboeae2f5412007-10-19 09:22:59 -0400583 cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
Chris Mason96b51792007-10-15 16:15:19 -0400584 err = write_one_cache_group(trans, root,
585 path, cache);
586 /*
587 * if we fail to write the cache group, we want
588 * to keep it marked dirty in hopes that a later
589 * write will work
590 */
591 if (err) {
592 werr = err;
593 continue;
Chris Mason9078a3e2007-04-26 16:46:15 -0400594 }
Chris Mason96b51792007-10-15 16:15:19 -0400595 clear_extent_bits(block_group_cache, start, end,
596 BLOCK_GROUP_DIRTY, GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -0400597 }
598 btrfs_free_path(path);
599 return werr;
600}
601
602static int update_block_group(struct btrfs_trans_handle *trans,
603 struct btrfs_root *root,
Chris Masondb945352007-10-15 16:15:53 -0400604 u64 bytenr, u64 num_bytes, int alloc,
605 int mark_free, int data)
Chris Mason9078a3e2007-04-26 16:46:15 -0400606{
607 struct btrfs_block_group_cache *cache;
608 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -0400609 u64 total = num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -0400610 u64 old_val;
Chris Masondb945352007-10-15 16:15:53 -0400611 u64 byte_in_group;
Chris Mason96b51792007-10-15 16:15:19 -0400612 u64 start;
613 u64 end;
Chris Mason3e1ad542007-05-07 20:03:49 -0400614
Chris Mason9078a3e2007-04-26 16:46:15 -0400615 while(total) {
Chris Masondb945352007-10-15 16:15:53 -0400616 cache = btrfs_lookup_block_group(info, bytenr);
Chris Mason3e1ad542007-05-07 20:03:49 -0400617 if (!cache) {
Chris Mason9078a3e2007-04-26 16:46:15 -0400618 return -1;
Chris Masoncd1bc462007-04-27 10:08:34 -0400619 }
Chris Masondb945352007-10-15 16:15:53 -0400620 byte_in_group = bytenr - cache->key.objectid;
621 WARN_ON(byte_in_group > cache->key.offset);
Chris Mason96b51792007-10-15 16:15:19 -0400622 start = cache->key.objectid;
623 end = start + cache->key.offset - 1;
624 set_extent_bits(&info->block_group_cache, start, end,
625 BLOCK_GROUP_DIRTY, GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -0400626
627 old_val = btrfs_block_group_used(&cache->item);
Chris Masondb945352007-10-15 16:15:53 -0400628 num_bytes = min(total, cache->key.offset - byte_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -0400629 if (alloc) {
Chris Mason1e2677e2007-05-29 16:52:18 -0400630 if (cache->data != data &&
Chris Mason84f54cf2007-06-12 07:43:08 -0400631 old_val < (cache->key.offset >> 1)) {
Chris Mason96b51792007-10-15 16:15:19 -0400632 int bit_to_clear;
633 int bit_to_set;
Chris Mason96b51792007-10-15 16:15:19 -0400634 cache->data = data;
Chris Mason1e2677e2007-05-29 16:52:18 -0400635 if (data) {
Yanb97f9202007-11-01 11:28:41 -0400636 bit_to_clear = BLOCK_GROUP_METADATA;
637 bit_to_set = BLOCK_GROUP_DATA;
Chris Masonf84a8b32007-11-06 10:26:29 -0500638 cache->item.flags &=
639 ~BTRFS_BLOCK_GROUP_MIXED;
Chris Mason1e2677e2007-05-29 16:52:18 -0400640 cache->item.flags |=
641 BTRFS_BLOCK_GROUP_DATA;
642 } else {
Yanb97f9202007-11-01 11:28:41 -0400643 bit_to_clear = BLOCK_GROUP_DATA;
644 bit_to_set = BLOCK_GROUP_METADATA;
Chris Mason1e2677e2007-05-29 16:52:18 -0400645 cache->item.flags &=
Chris Masonf84a8b32007-11-06 10:26:29 -0500646 ~BTRFS_BLOCK_GROUP_MIXED;
647 cache->item.flags &=
Chris Mason1e2677e2007-05-29 16:52:18 -0400648 ~BTRFS_BLOCK_GROUP_DATA;
649 }
Chris Mason96b51792007-10-15 16:15:19 -0400650 clear_extent_bits(&info->block_group_cache,
651 start, end, bit_to_clear,
652 GFP_NOFS);
653 set_extent_bits(&info->block_group_cache,
654 start, end, bit_to_set,
655 GFP_NOFS);
Chris Masonf84a8b32007-11-06 10:26:29 -0500656 } else if (cache->data != data &&
657 cache->data != BTRFS_BLOCK_GROUP_MIXED) {
658 cache->data = BTRFS_BLOCK_GROUP_MIXED;
659 set_extent_bits(&info->block_group_cache,
660 start, end,
661 BLOCK_GROUP_DATA |
662 BLOCK_GROUP_METADATA,
663 GFP_NOFS);
Chris Mason1e2677e2007-05-29 16:52:18 -0400664 }
Chris Masondb945352007-10-15 16:15:53 -0400665 old_val += num_bytes;
Chris Masoncd1bc462007-04-27 10:08:34 -0400666 } else {
Chris Masondb945352007-10-15 16:15:53 -0400667 old_val -= num_bytes;
Chris Masonf510cfe2007-10-15 16:14:48 -0400668 if (mark_free) {
669 set_extent_dirty(&info->free_space_cache,
Chris Masondb945352007-10-15 16:15:53 -0400670 bytenr, bytenr + num_bytes - 1,
Chris Masonf510cfe2007-10-15 16:14:48 -0400671 GFP_NOFS);
Chris Masone37c9e62007-05-09 20:13:14 -0400672 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400673 }
Chris Mason9078a3e2007-04-26 16:46:15 -0400674 btrfs_set_block_group_used(&cache->item, old_val);
Chris Masondb945352007-10-15 16:15:53 -0400675 total -= num_bytes;
676 bytenr += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -0400677 }
678 return 0;
679}
Yan324ae4d2007-11-16 14:57:08 -0500680static int update_pinned_extents(struct btrfs_root *root,
681 u64 bytenr, u64 num, int pin)
682{
683 u64 len;
684 struct btrfs_block_group_cache *cache;
685 struct btrfs_fs_info *fs_info = root->fs_info;
686
687 if (pin) {
688 set_extent_dirty(&fs_info->pinned_extents,
689 bytenr, bytenr + num - 1, GFP_NOFS);
690 } else {
691 clear_extent_dirty(&fs_info->pinned_extents,
692 bytenr, bytenr + num - 1, GFP_NOFS);
693 }
694 while (num > 0) {
695 cache = btrfs_lookup_block_group(fs_info, bytenr);
696 WARN_ON(!cache);
697 len = min(num, cache->key.offset -
698 (bytenr - cache->key.objectid));
699 if (pin) {
700 cache->pinned += len;
701 fs_info->total_pinned += len;
702 } else {
703 cache->pinned -= len;
704 fs_info->total_pinned -= len;
705 }
706 bytenr += len;
707 num -= len;
708 }
709 return 0;
710}
Chris Mason9078a3e2007-04-26 16:46:15 -0400711
Chris Mason1a5bc162007-10-15 16:15:26 -0400712int btrfs_copy_pinned(struct btrfs_root *root, struct extent_map_tree *copy)
Chris Masonccd467d2007-06-28 15:57:36 -0400713{
Chris Masonccd467d2007-06-28 15:57:36 -0400714 u64 last = 0;
Chris Mason1a5bc162007-10-15 16:15:26 -0400715 u64 start;
716 u64 end;
717 struct extent_map_tree *pinned_extents = &root->fs_info->pinned_extents;
Chris Masonccd467d2007-06-28 15:57:36 -0400718 int ret;
Chris Masonccd467d2007-06-28 15:57:36 -0400719
720 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -0400721 ret = find_first_extent_bit(pinned_extents, last,
722 &start, &end, EXTENT_DIRTY);
723 if (ret)
Chris Masonccd467d2007-06-28 15:57:36 -0400724 break;
Chris Mason1a5bc162007-10-15 16:15:26 -0400725 set_extent_dirty(copy, start, end, GFP_NOFS);
726 last = end + 1;
Chris Masonccd467d2007-06-28 15:57:36 -0400727 }
728 return 0;
729}
730
731int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
732 struct btrfs_root *root,
Chris Mason1a5bc162007-10-15 16:15:26 -0400733 struct extent_map_tree *unpin)
Chris Masona28ec192007-03-06 20:08:01 -0500734{
Chris Mason1a5bc162007-10-15 16:15:26 -0400735 u64 start;
736 u64 end;
Chris Masona28ec192007-03-06 20:08:01 -0500737 int ret;
Chris Masonf510cfe2007-10-15 16:14:48 -0400738 struct extent_map_tree *free_space_cache;
Chris Masonf510cfe2007-10-15 16:14:48 -0400739 free_space_cache = &root->fs_info->free_space_cache;
Chris Masona28ec192007-03-06 20:08:01 -0500740
741 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -0400742 ret = find_first_extent_bit(unpin, 0, &start, &end,
743 EXTENT_DIRTY);
744 if (ret)
Chris Masona28ec192007-03-06 20:08:01 -0500745 break;
Yan324ae4d2007-11-16 14:57:08 -0500746 update_pinned_extents(root, start, end + 1 - start, 0);
Chris Mason1a5bc162007-10-15 16:15:26 -0400747 clear_extent_dirty(unpin, start, end, GFP_NOFS);
748 set_extent_dirty(free_space_cache, start, end, GFP_NOFS);
Chris Masona28ec192007-03-06 20:08:01 -0500749 }
750 return 0;
751}
752
Chris Masone089f052007-03-16 16:20:31 -0400753static int finish_current_insert(struct btrfs_trans_handle *trans, struct
754 btrfs_root *extent_root)
Chris Mason037e6392007-03-07 11:50:24 -0500755{
Chris Masone2fa7222007-03-12 16:22:34 -0400756 struct btrfs_key ins;
Chris Mason234b63a2007-03-13 10:46:10 -0400757 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -0500758 int ret;
Chris Mason1a5bc162007-10-15 16:15:26 -0400759 int err = 0;
760 u64 start;
761 u64 end;
Chris Mason1261ec42007-03-20 20:35:03 -0400762 struct btrfs_fs_info *info = extent_root->fs_info;
Chris Mason037e6392007-03-07 11:50:24 -0500763
Chris Mason5f39d392007-10-15 16:14:19 -0400764 btrfs_set_stack_extent_refs(&extent_item, 1);
Chris Mason62e27492007-03-15 12:56:47 -0400765 btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
Chris Mason5f39d392007-10-15 16:14:19 -0400766 btrfs_set_stack_extent_owner(&extent_item,
767 extent_root->root_key.objectid);
Chris Mason037e6392007-03-07 11:50:24 -0500768
Chris Mason26b80032007-08-08 20:17:12 -0400769 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -0400770 ret = find_first_extent_bit(&info->extent_ins, 0, &start,
771 &end, EXTENT_LOCKED);
772 if (ret)
Chris Mason26b80032007-08-08 20:17:12 -0400773 break;
774
Chris Mason1a5bc162007-10-15 16:15:26 -0400775 ins.objectid = start;
776 ins.offset = end + 1 - start;
777 err = btrfs_insert_item(trans, extent_root, &ins,
778 &extent_item, sizeof(extent_item));
779 clear_extent_bits(&info->extent_ins, start, end, EXTENT_LOCKED,
780 GFP_NOFS);
Chris Mason037e6392007-03-07 11:50:24 -0500781 }
Chris Mason037e6392007-03-07 11:50:24 -0500782 return 0;
783}
784
Chris Masondb945352007-10-15 16:15:53 -0400785static int pin_down_bytes(struct btrfs_root *root, u64 bytenr, u32 num_bytes,
786 int pending)
Chris Masone20d96d2007-03-22 12:13:20 -0400787{
Chris Mason1a5bc162007-10-15 16:15:26 -0400788 int err = 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400789 struct extent_buffer *buf;
Chris Mason78fae272007-03-25 11:35:08 -0400790
Chris Masonf4b9aa82007-03-27 11:05:53 -0400791 if (!pending) {
Chris Masondb945352007-10-15 16:15:53 -0400792 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
Chris Mason5f39d392007-10-15 16:14:19 -0400793 if (buf) {
794 if (btrfs_buffer_uptodate(buf)) {
Chris Mason2c90e5d2007-04-02 10:50:19 -0400795 u64 transid =
796 root->fs_info->running_transaction->transid;
Chris Mason5f39d392007-10-15 16:14:19 -0400797 if (btrfs_header_generation(buf) == transid) {
798 free_extent_buffer(buf);
Yanc5492282007-11-06 10:25:25 -0500799 return 1;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400800 }
Chris Masonf4b9aa82007-03-27 11:05:53 -0400801 }
Chris Mason5f39d392007-10-15 16:14:19 -0400802 free_extent_buffer(buf);
Chris Mason8ef97622007-03-26 10:15:30 -0400803 }
Yan324ae4d2007-11-16 14:57:08 -0500804 update_pinned_extents(root, bytenr, num_bytes, 1);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400805 } else {
Chris Mason1a5bc162007-10-15 16:15:26 -0400806 set_extent_bits(&root->fs_info->pending_del,
Chris Masondb945352007-10-15 16:15:53 -0400807 bytenr, bytenr + num_bytes - 1,
808 EXTENT_LOCKED, GFP_NOFS);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400809 }
Chris Masonbe744172007-05-06 10:15:01 -0400810 BUG_ON(err < 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400811 return 0;
812}
813
Chris Masona28ec192007-03-06 20:08:01 -0500814/*
815 * remove an extent from the root, returns 0 on success
816 */
Chris Masone089f052007-03-16 16:20:31 -0400817static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masondb945352007-10-15 16:15:53 -0400818 *root, u64 bytenr, u64 num_bytes, int pin,
Chris Masone37c9e62007-05-09 20:13:14 -0400819 int mark_free)
Chris Masona28ec192007-03-06 20:08:01 -0500820{
Chris Mason5caf2a02007-04-02 11:20:42 -0400821 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -0400822 struct btrfs_key key;
Chris Mason1261ec42007-03-20 20:35:03 -0400823 struct btrfs_fs_info *info = root->fs_info;
824 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -0400825 struct extent_buffer *leaf;
Chris Masona28ec192007-03-06 20:08:01 -0500826 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400827 struct btrfs_extent_item *ei;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400828 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500829
Chris Masondb945352007-10-15 16:15:53 -0400830 key.objectid = bytenr;
Chris Mason62e27492007-03-15 12:56:47 -0400831 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masondb945352007-10-15 16:15:53 -0400832 key.offset = num_bytes;
Chris Masona28ec192007-03-06 20:08:01 -0500833
Chris Mason5caf2a02007-04-02 11:20:42 -0400834 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -0400835 if (!path)
836 return -ENOMEM;
837
Chris Mason5caf2a02007-04-02 11:20:42 -0400838 ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400839 if (ret < 0)
840 return ret;
841 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -0400842
843 leaf = path->nodes[0];
844 ei = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason123abc82007-03-14 14:14:43 -0400845 struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400846 refs = btrfs_extent_refs(leaf, ei);
847 BUG_ON(refs == 0);
848 refs -= 1;
849 btrfs_set_extent_refs(leaf, ei, refs);
850 btrfs_mark_buffer_dirty(leaf);
851
Chris Masoncf27e1e2007-03-13 09:49:06 -0400852 if (refs == 0) {
Chris Masondb945352007-10-15 16:15:53 -0400853 u64 super_used;
854 u64 root_used;
Chris Mason78fae272007-03-25 11:35:08 -0400855
856 if (pin) {
Chris Masondb945352007-10-15 16:15:53 -0400857 ret = pin_down_bytes(root, bytenr, num_bytes, 0);
Yanc5492282007-11-06 10:25:25 -0500858 if (ret > 0)
859 mark_free = 1;
860 BUG_ON(ret < 0);
Chris Mason78fae272007-03-25 11:35:08 -0400861 }
862
Josef Bacik58176a92007-08-29 15:47:34 -0400863 /* block accounting for super block */
Chris Masondb945352007-10-15 16:15:53 -0400864 super_used = btrfs_super_bytes_used(&info->super_copy);
865 btrfs_set_super_bytes_used(&info->super_copy,
866 super_used - num_bytes);
Josef Bacik58176a92007-08-29 15:47:34 -0400867
868 /* block accounting for root item */
Chris Masondb945352007-10-15 16:15:53 -0400869 root_used = btrfs_root_used(&root->root_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400870 btrfs_set_root_used(&root->root_item,
Chris Masondb945352007-10-15 16:15:53 -0400871 root_used - num_bytes);
Josef Bacik58176a92007-08-29 15:47:34 -0400872
Chris Mason5caf2a02007-04-02 11:20:42 -0400873 ret = btrfs_del_item(trans, extent_root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -0400874 if (ret) {
875 return ret;
876 }
Chris Masondb945352007-10-15 16:15:53 -0400877 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
Chris Mason1e2677e2007-05-29 16:52:18 -0400878 mark_free, 0);
Chris Mason9078a3e2007-04-26 16:46:15 -0400879 BUG_ON(ret);
Chris Masona28ec192007-03-06 20:08:01 -0500880 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400881 btrfs_free_path(path);
Chris Masone089f052007-03-16 16:20:31 -0400882 finish_current_insert(trans, extent_root);
Chris Masona28ec192007-03-06 20:08:01 -0500883 return ret;
884}
885
886/*
Chris Masonfec577f2007-02-26 10:40:21 -0500887 * find all the blocks marked as pending in the radix tree and remove
888 * them from the extent map
889 */
Chris Masone089f052007-03-16 16:20:31 -0400890static int del_pending_extents(struct btrfs_trans_handle *trans, struct
891 btrfs_root *extent_root)
Chris Masonfec577f2007-02-26 10:40:21 -0500892{
893 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -0400894 int err = 0;
Chris Mason1a5bc162007-10-15 16:15:26 -0400895 u64 start;
896 u64 end;
897 struct extent_map_tree *pending_del;
898 struct extent_map_tree *pinned_extents;
Chris Mason8ef97622007-03-26 10:15:30 -0400899
Chris Mason1a5bc162007-10-15 16:15:26 -0400900 pending_del = &extent_root->fs_info->pending_del;
901 pinned_extents = &extent_root->fs_info->pinned_extents;
Chris Masonfec577f2007-02-26 10:40:21 -0500902
903 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -0400904 ret = find_first_extent_bit(pending_del, 0, &start, &end,
905 EXTENT_LOCKED);
906 if (ret)
Chris Masonfec577f2007-02-26 10:40:21 -0500907 break;
Yan324ae4d2007-11-16 14:57:08 -0500908 update_pinned_extents(extent_root, start, end + 1 - start, 1);
Chris Mason1a5bc162007-10-15 16:15:26 -0400909 clear_extent_bits(pending_del, start, end, EXTENT_LOCKED,
910 GFP_NOFS);
911 ret = __free_extent(trans, extent_root,
912 start, end + 1 - start, 0, 0);
913 if (ret)
914 err = ret;
Chris Masonfec577f2007-02-26 10:40:21 -0500915 }
Chris Masone20d96d2007-03-22 12:13:20 -0400916 return err;
Chris Masonfec577f2007-02-26 10:40:21 -0500917}
918
919/*
920 * remove an extent from the root, returns 0 on success
921 */
Chris Masone089f052007-03-16 16:20:31 -0400922int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masondb945352007-10-15 16:15:53 -0400923 *root, u64 bytenr, u64 num_bytes, int pin)
Chris Masonfec577f2007-02-26 10:40:21 -0500924{
Chris Mason9f5fae22007-03-20 14:38:32 -0400925 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Masonfec577f2007-02-26 10:40:21 -0500926 int pending_ret;
927 int ret;
Chris Masona28ec192007-03-06 20:08:01 -0500928
Chris Masondb945352007-10-15 16:15:53 -0400929 WARN_ON(num_bytes < root->sectorsize);
Chris Masona28ec192007-03-06 20:08:01 -0500930 if (root == extent_root) {
Chris Masondb945352007-10-15 16:15:53 -0400931 pin_down_bytes(root, bytenr, num_bytes, 1);
Chris Masona28ec192007-03-06 20:08:01 -0500932 return 0;
933 }
Chris Masondb945352007-10-15 16:15:53 -0400934 ret = __free_extent(trans, root, bytenr, num_bytes, pin, pin == 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400935 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
Chris Masonfec577f2007-02-26 10:40:21 -0500936 return ret ? ret : pending_ret;
937}
938
Chris Mason87ee04e2007-11-30 11:30:34 -0500939static u64 stripe_align(struct btrfs_root *root, u64 val)
940{
941 u64 mask = ((u64)root->stripesize - 1);
942 u64 ret = (val + mask) & ~mask;
943 return ret;
944}
945
Chris Masonfec577f2007-02-26 10:40:21 -0500946/*
947 * walks the btree of allocated extents and find a hole of a given size.
948 * The key ins is changed to record the hole:
949 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -0400950 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -0500951 * ins->offset == number of blocks
952 * Any available blocks before search_start are skipped.
953 */
Chris Masone089f052007-03-16 16:20:31 -0400954static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masondb945352007-10-15 16:15:53 -0400955 *orig_root, u64 num_bytes, u64 empty_size,
956 u64 search_start, u64 search_end, u64 hint_byte,
Chris Masonf2654de2007-06-26 12:20:46 -0400957 struct btrfs_key *ins, u64 exclude_start,
958 u64 exclude_nr, int data)
Chris Masonfec577f2007-02-26 10:40:21 -0500959{
Chris Mason5caf2a02007-04-02 11:20:42 -0400960 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -0400961 struct btrfs_key key;
Chris Masonfec577f2007-02-26 10:40:21 -0500962 u64 hole_size = 0;
Chris Mason87ee04e2007-11-30 11:30:34 -0500963 u64 aligned;
964 int ret;
Chris Masonfec577f2007-02-26 10:40:21 -0500965 int slot = 0;
Chris Masondb945352007-10-15 16:15:53 -0400966 u64 last_byte = 0;
Chris Masonbe744172007-05-06 10:15:01 -0400967 u64 orig_search_start = search_start;
Chris Masonfec577f2007-02-26 10:40:21 -0500968 int start_found;
Chris Mason5f39d392007-10-15 16:14:19 -0400969 struct extent_buffer *l;
Chris Mason9f5fae22007-03-20 14:38:32 -0400970 struct btrfs_root * root = orig_root->fs_info->extent_root;
Chris Masonf2458e12007-04-25 15:52:25 -0400971 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -0400972 u64 total_needed = num_bytes;
Chris Masone20d96d2007-03-22 12:13:20 -0400973 int level;
Chris Masonbe08c1b2007-05-03 09:06:49 -0400974 struct btrfs_block_group_cache *block_group;
Chris Masonbe744172007-05-06 10:15:01 -0400975 int full_scan = 0;
Chris Masonfbdc7622007-05-30 10:22:12 -0400976 int wrapped = 0;
Chris Masonf84a8b32007-11-06 10:26:29 -0500977 u64 cached_start;
Chris Masonfec577f2007-02-26 10:40:21 -0500978
Chris Masondb945352007-10-15 16:15:53 -0400979 WARN_ON(num_bytes < root->sectorsize);
Chris Masonb1a4d962007-04-04 15:27:52 -0400980 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
981
Chris Mason5f39d392007-10-15 16:14:19 -0400982 level = btrfs_header_level(root->node);
983
Chris Mason015a739c2007-11-26 16:15:16 -0800984 if (num_bytes >= 32 * 1024 * 1024 && hint_byte) {
Chris Mason257d0ce2007-11-07 21:08:16 -0500985 data = BTRFS_BLOCK_GROUP_MIXED;
986 }
987
Chris Mason3e1ad542007-05-07 20:03:49 -0400988 if (search_end == (u64)-1)
Chris Masondb945352007-10-15 16:15:53 -0400989 search_end = btrfs_super_total_bytes(&info->super_copy);
990 if (hint_byte) {
991 block_group = btrfs_lookup_block_group(info, hint_byte);
Chris Mason1a2b2ac2007-12-04 13:18:24 -0500992 if (!block_group)
993 hint_byte = search_start;
Chris Masonbe744172007-05-06 10:15:01 -0400994 block_group = btrfs_find_block_group(root, block_group,
Chris Masondb945352007-10-15 16:15:53 -0400995 hint_byte, data, 1);
Chris Masonbe744172007-05-06 10:15:01 -0400996 } else {
997 block_group = btrfs_find_block_group(root,
Chris Mason1a2b2ac2007-12-04 13:18:24 -0500998 trans->block_group,
999 search_start, data, 1);
Chris Masonbe744172007-05-06 10:15:01 -04001000 }
1001
Chris Mason6702ed42007-08-07 16:15:09 -04001002 total_needed += empty_size;
Chris Masone0115992007-06-19 16:23:05 -04001003 path = btrfs_alloc_path();
Chris Masonbe744172007-05-06 10:15:01 -04001004check_failed:
Yan5e5745d2007-11-16 14:57:09 -05001005 search_start = find_search_start(root, &block_group, search_start,
1006 total_needed, data, full_scan);
Chris Mason87ee04e2007-11-30 11:30:34 -05001007 search_start = stripe_align(root, search_start);
Chris Masonf84a8b32007-11-06 10:26:29 -05001008 cached_start = search_start;
Chris Mason5caf2a02007-04-02 11:20:42 -04001009 btrfs_init_path(path);
Chris Masonfec577f2007-02-26 10:40:21 -05001010 ins->objectid = search_start;
1011 ins->offset = 0;
Chris Masonfec577f2007-02-26 10:40:21 -05001012 start_found = 0;
Chris Mason2cc58cf2007-08-27 16:49:44 -04001013 path->reada = 2;
Chris Masone37c9e62007-05-09 20:13:14 -04001014
Chris Mason5caf2a02007-04-02 11:20:42 -04001015 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
Chris Mason0f70abe2007-02-28 16:46:22 -05001016 if (ret < 0)
1017 goto error;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001018
Chris Masone37c9e62007-05-09 20:13:14 -04001019 if (path->slots[0] > 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001020 path->slots[0]--;
Chris Masone37c9e62007-05-09 20:13:14 -04001021 }
1022
Chris Mason5f39d392007-10-15 16:14:19 -04001023 l = path->nodes[0];
1024 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
1025
Chris Masone37c9e62007-05-09 20:13:14 -04001026 /*
1027 * a rare case, go back one key if we hit a block group item
1028 * instead of an extent item
1029 */
1030 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY &&
1031 key.objectid + key.offset >= search_start) {
1032 ins->objectid = key.objectid;
1033 ins->offset = key.offset - 1;
1034 btrfs_release_path(root, path);
1035 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
1036 if (ret < 0)
1037 goto error;
1038
1039 if (path->slots[0] > 0) {
1040 path->slots[0]--;
1041 }
1042 }
Chris Mason0579da42007-03-07 16:15:30 -05001043
Chris Masonfec577f2007-02-26 10:40:21 -05001044 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001045 l = path->nodes[0];
Chris Mason5caf2a02007-04-02 11:20:42 -04001046 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04001047 if (slot >= btrfs_header_nritems(l)) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001048 ret = btrfs_next_leaf(root, path);
Chris Masonfec577f2007-02-26 10:40:21 -05001049 if (ret == 0)
1050 continue;
Chris Mason0f70abe2007-02-28 16:46:22 -05001051 if (ret < 0)
1052 goto error;
Chris Masone19caa52007-10-15 16:17:44 -04001053
1054 search_start = max(search_start,
1055 block_group->key.objectid);
Chris Masonfec577f2007-02-26 10:40:21 -05001056 if (!start_found) {
Chris Mason87ee04e2007-11-30 11:30:34 -05001057 aligned = stripe_align(root, search_start);
1058 ins->objectid = aligned;
1059 if (aligned >= search_end) {
1060 ret = -ENOSPC;
1061 goto error;
1062 }
1063 ins->offset = search_end - aligned;
Chris Masonfec577f2007-02-26 10:40:21 -05001064 start_found = 1;
1065 goto check_pending;
1066 }
Chris Mason87ee04e2007-11-30 11:30:34 -05001067 ins->objectid = stripe_align(root,
1068 last_byte > search_start ?
1069 last_byte : search_start);
1070 if (search_end <= ins->objectid) {
1071 ret = -ENOSPC;
1072 goto error;
1073 }
Chris Mason3e1ad542007-05-07 20:03:49 -04001074 ins->offset = search_end - ins->objectid;
Chris Masone19caa52007-10-15 16:17:44 -04001075 BUG_ON(ins->objectid >= search_end);
Chris Masonfec577f2007-02-26 10:40:21 -05001076 goto check_pending;
1077 }
Chris Mason5f39d392007-10-15 16:14:19 -04001078 btrfs_item_key_to_cpu(l, &key, slot);
Chris Mason96b51792007-10-15 16:15:19 -04001079
Chris Masondb945352007-10-15 16:15:53 -04001080 if (key.objectid >= search_start && key.objectid > last_byte &&
Chris Masone37c9e62007-05-09 20:13:14 -04001081 start_found) {
Chris Masondb945352007-10-15 16:15:53 -04001082 if (last_byte < search_start)
1083 last_byte = search_start;
Chris Mason87ee04e2007-11-30 11:30:34 -05001084 aligned = stripe_align(root, last_byte);
1085 hole_size = key.objectid - aligned;
1086 if (key.objectid > aligned && hole_size >= num_bytes) {
1087 ins->objectid = aligned;
Chris Masone37c9e62007-05-09 20:13:14 -04001088 ins->offset = hole_size;
1089 goto check_pending;
Chris Mason0579da42007-03-07 16:15:30 -05001090 }
Chris Masonfec577f2007-02-26 10:40:21 -05001091 }
Chris Mason96b51792007-10-15 16:15:19 -04001092 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY) {
1093 if (!start_found) {
Chris Masondb945352007-10-15 16:15:53 -04001094 last_byte = key.objectid;
Chris Mason96b51792007-10-15 16:15:19 -04001095 start_found = 1;
1096 }
Chris Masone37c9e62007-05-09 20:13:14 -04001097 goto next;
Chris Mason96b51792007-10-15 16:15:19 -04001098 }
1099
Chris Masone37c9e62007-05-09 20:13:14 -04001100
Chris Mason0579da42007-03-07 16:15:30 -05001101 start_found = 1;
Chris Masondb945352007-10-15 16:15:53 -04001102 last_byte = key.objectid + key.offset;
Chris Masonf510cfe2007-10-15 16:14:48 -04001103
Chris Mason257d0ce2007-11-07 21:08:16 -05001104 if (!full_scan && data != BTRFS_BLOCK_GROUP_MIXED &&
1105 last_byte >= block_group->key.objectid +
Chris Masonbe744172007-05-06 10:15:01 -04001106 block_group->key.offset) {
1107 btrfs_release_path(root, path);
1108 search_start = block_group->key.objectid +
Chris Masone19caa52007-10-15 16:17:44 -04001109 block_group->key.offset;
Chris Masonbe744172007-05-06 10:15:01 -04001110 goto new_group;
1111 }
Chris Mason9078a3e2007-04-26 16:46:15 -04001112next:
Chris Mason5caf2a02007-04-02 11:20:42 -04001113 path->slots[0]++;
Chris Masonde428b62007-05-18 13:28:27 -04001114 cond_resched();
Chris Masonfec577f2007-02-26 10:40:21 -05001115 }
Chris Masonfec577f2007-02-26 10:40:21 -05001116check_pending:
1117 /* we have to make sure we didn't find an extent that has already
1118 * been allocated by the map tree or the original allocation
1119 */
Chris Mason5caf2a02007-04-02 11:20:42 -04001120 btrfs_release_path(root, path);
Chris Masonfec577f2007-02-26 10:40:21 -05001121 BUG_ON(ins->objectid < search_start);
Chris Masone37c9e62007-05-09 20:13:14 -04001122
Chris Masondb945352007-10-15 16:15:53 -04001123 if (ins->objectid + num_bytes >= search_end)
Chris Masoncf675822007-09-17 11:00:51 -04001124 goto enospc;
Chris Mason257d0ce2007-11-07 21:08:16 -05001125 if (!full_scan && data != BTRFS_BLOCK_GROUP_MIXED &&
Yan5cf66422007-11-16 14:57:09 -05001126 ins->objectid + num_bytes > block_group->
Chris Masone19caa52007-10-15 16:17:44 -04001127 key.objectid + block_group->key.offset) {
1128 search_start = block_group->key.objectid +
1129 block_group->key.offset;
1130 goto new_group;
1131 }
Chris Mason1a5bc162007-10-15 16:15:26 -04001132 if (test_range_bit(&info->extent_ins, ins->objectid,
Chris Masondb945352007-10-15 16:15:53 -04001133 ins->objectid + num_bytes -1, EXTENT_LOCKED, 0)) {
1134 search_start = ins->objectid + num_bytes;
Chris Mason1a5bc162007-10-15 16:15:26 -04001135 goto new_group;
1136 }
1137 if (test_range_bit(&info->pinned_extents, ins->objectid,
Chris Masondb945352007-10-15 16:15:53 -04001138 ins->objectid + num_bytes -1, EXTENT_DIRTY, 0)) {
1139 search_start = ins->objectid + num_bytes;
Chris Mason1a5bc162007-10-15 16:15:26 -04001140 goto new_group;
Chris Masonfec577f2007-02-26 10:40:21 -05001141 }
Chris Masondb945352007-10-15 16:15:53 -04001142 if (exclude_nr > 0 && (ins->objectid + num_bytes > exclude_start &&
Chris Masonf2654de2007-06-26 12:20:46 -04001143 ins->objectid < exclude_start + exclude_nr)) {
1144 search_start = exclude_start + exclude_nr;
1145 goto new_group;
1146 }
Chris Masone37c9e62007-05-09 20:13:14 -04001147 if (!data) {
Chris Mason5276aed2007-06-11 21:33:38 -04001148 block_group = btrfs_lookup_block_group(info, ins->objectid);
Chris Mason26b80032007-08-08 20:17:12 -04001149 if (block_group)
1150 trans->block_group = block_group;
Chris Masoncd1bc462007-04-27 10:08:34 -04001151 }
Chris Masondb945352007-10-15 16:15:53 -04001152 ins->offset = num_bytes;
Chris Mason5caf2a02007-04-02 11:20:42 -04001153 btrfs_free_path(path);
Chris Masonfec577f2007-02-26 10:40:21 -05001154 return 0;
Chris Masonbe744172007-05-06 10:15:01 -04001155
1156new_group:
Chris Masondb945352007-10-15 16:15:53 -04001157 if (search_start + num_bytes >= search_end) {
Chris Masoncf675822007-09-17 11:00:51 -04001158enospc:
Chris Masonbe744172007-05-06 10:15:01 -04001159 search_start = orig_search_start;
Chris Masonfbdc7622007-05-30 10:22:12 -04001160 if (full_scan) {
1161 ret = -ENOSPC;
1162 goto error;
1163 }
Chris Mason6702ed42007-08-07 16:15:09 -04001164 if (wrapped) {
1165 if (!full_scan)
1166 total_needed -= empty_size;
Chris Masonfbdc7622007-05-30 10:22:12 -04001167 full_scan = 1;
Chris Mason1a2b2ac2007-12-04 13:18:24 -05001168 data = BTRFS_BLOCK_GROUP_MIXED;
Chris Mason6702ed42007-08-07 16:15:09 -04001169 } else
Chris Masonfbdc7622007-05-30 10:22:12 -04001170 wrapped = 1;
Chris Masonbe744172007-05-06 10:15:01 -04001171 }
Chris Mason5276aed2007-06-11 21:33:38 -04001172 block_group = btrfs_lookup_block_group(info, search_start);
Chris Masonfbdc7622007-05-30 10:22:12 -04001173 cond_resched();
Chris Mason1a2b2ac2007-12-04 13:18:24 -05001174 block_group = btrfs_find_block_group(root, block_group,
1175 search_start, data, 0);
Chris Masonbe744172007-05-06 10:15:01 -04001176 goto check_failed;
1177
Chris Mason0f70abe2007-02-28 16:46:22 -05001178error:
Chris Mason5caf2a02007-04-02 11:20:42 -04001179 btrfs_release_path(root, path);
1180 btrfs_free_path(path);
Chris Mason0f70abe2007-02-28 16:46:22 -05001181 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05001182}
Chris Masonfec577f2007-02-26 10:40:21 -05001183/*
Chris Masonfec577f2007-02-26 10:40:21 -05001184 * finds a free extent and does all the dirty work required for allocation
1185 * returns the key for the extent through ins, and a tree buffer for
1186 * the first block of the extent through buf.
1187 *
1188 * returns 0 if everything worked, non-zero otherwise.
1189 */
Chris Mason4d775672007-04-20 20:23:12 -04001190int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
1191 struct btrfs_root *root, u64 owner,
Chris Masondb945352007-10-15 16:15:53 -04001192 u64 num_bytes, u64 empty_size, u64 hint_byte,
Chris Masonbe08c1b2007-05-03 09:06:49 -04001193 u64 search_end, struct btrfs_key *ins, int data)
Chris Masonfec577f2007-02-26 10:40:21 -05001194{
1195 int ret;
1196 int pending_ret;
Chris Masondb945352007-10-15 16:15:53 -04001197 u64 super_used, root_used;
Chris Masonfbdc7622007-05-30 10:22:12 -04001198 u64 search_start = 0;
Chris Mason1261ec42007-03-20 20:35:03 -04001199 struct btrfs_fs_info *info = root->fs_info;
1200 struct btrfs_root *extent_root = info->extent_root;
Chris Mason234b63a2007-03-13 10:46:10 -04001201 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -05001202
Chris Mason5f39d392007-10-15 16:14:19 -04001203 btrfs_set_stack_extent_refs(&extent_item, 1);
1204 btrfs_set_stack_extent_owner(&extent_item, owner);
Chris Masonfec577f2007-02-26 10:40:21 -05001205
Chris Masondb945352007-10-15 16:15:53 -04001206 WARN_ON(num_bytes < root->sectorsize);
1207 ret = find_free_extent(trans, root, num_bytes, empty_size,
1208 search_start, search_end, hint_byte, ins,
Chris Mason26b80032007-08-08 20:17:12 -04001209 trans->alloc_exclude_start,
1210 trans->alloc_exclude_nr, data);
Chris Masonccd467d2007-06-28 15:57:36 -04001211 BUG_ON(ret);
Chris Masonf2654de2007-06-26 12:20:46 -04001212 if (ret)
1213 return ret;
1214
Josef Bacik58176a92007-08-29 15:47:34 -04001215 /* block accounting for super block */
Chris Masondb945352007-10-15 16:15:53 -04001216 super_used = btrfs_super_bytes_used(&info->super_copy);
1217 btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes);
Chris Mason26b80032007-08-08 20:17:12 -04001218
Josef Bacik58176a92007-08-29 15:47:34 -04001219 /* block accounting for root item */
Chris Masondb945352007-10-15 16:15:53 -04001220 root_used = btrfs_root_used(&root->root_item);
1221 btrfs_set_root_used(&root->root_item, root_used + num_bytes);
Josef Bacik58176a92007-08-29 15:47:34 -04001222
Chris Masonf510cfe2007-10-15 16:14:48 -04001223 clear_extent_dirty(&root->fs_info->free_space_cache,
1224 ins->objectid, ins->objectid + ins->offset - 1,
1225 GFP_NOFS);
1226
Chris Mason26b80032007-08-08 20:17:12 -04001227 if (root == extent_root) {
Chris Mason1a5bc162007-10-15 16:15:26 -04001228 set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
1229 ins->objectid + ins->offset - 1,
1230 EXTENT_LOCKED, GFP_NOFS);
Chris Masone19caa52007-10-15 16:17:44 -04001231 WARN_ON(data == 1);
Chris Mason26b80032007-08-08 20:17:12 -04001232 goto update_block;
1233 }
1234
1235 WARN_ON(trans->alloc_exclude_nr);
1236 trans->alloc_exclude_start = ins->objectid;
1237 trans->alloc_exclude_nr = ins->offset;
Chris Masone089f052007-03-16 16:20:31 -04001238 ret = btrfs_insert_item(trans, extent_root, ins, &extent_item,
1239 sizeof(extent_item));
Chris Mason037e6392007-03-07 11:50:24 -05001240
Chris Mason26b80032007-08-08 20:17:12 -04001241 trans->alloc_exclude_start = 0;
1242 trans->alloc_exclude_nr = 0;
1243
Chris Masonccd467d2007-06-28 15:57:36 -04001244 BUG_ON(ret);
Chris Masone089f052007-03-16 16:20:31 -04001245 finish_current_insert(trans, extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -04001246 pending_ret = del_pending_extents(trans, extent_root);
Chris Masonf510cfe2007-10-15 16:14:48 -04001247
Chris Masone37c9e62007-05-09 20:13:14 -04001248 if (ret) {
Chris Mason037e6392007-03-07 11:50:24 -05001249 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -04001250 }
1251 if (pending_ret) {
Chris Mason037e6392007-03-07 11:50:24 -05001252 return pending_ret;
Chris Masone37c9e62007-05-09 20:13:14 -04001253 }
Chris Mason26b80032007-08-08 20:17:12 -04001254
1255update_block:
Chris Mason1e2677e2007-05-29 16:52:18 -04001256 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0,
1257 data);
Chris Masonfabb5682007-06-07 22:13:21 -04001258 BUG_ON(ret);
Chris Mason037e6392007-03-07 11:50:24 -05001259 return 0;
Chris Masonfec577f2007-02-26 10:40:21 -05001260}
1261
1262/*
1263 * helper function to allocate a block for a given tree
1264 * returns the tree buffer or NULL.
1265 */
Chris Mason5f39d392007-10-15 16:14:19 -04001266struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Chris Masondb945352007-10-15 16:15:53 -04001267 struct btrfs_root *root,
1268 u32 blocksize, u64 hint,
Chris Mason5f39d392007-10-15 16:14:19 -04001269 u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05001270{
Chris Masone2fa7222007-03-12 16:22:34 -04001271 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -05001272 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04001273 struct extent_buffer *buf;
Chris Masonfec577f2007-02-26 10:40:21 -05001274
Chris Mason4d775672007-04-20 20:23:12 -04001275 ret = btrfs_alloc_extent(trans, root, root->root_key.objectid,
Chris Masondb945352007-10-15 16:15:53 -04001276 blocksize, empty_size, hint,
1277 (u64)-1, &ins, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05001278 if (ret) {
Chris Mason54aa1f42007-06-22 14:16:25 -04001279 BUG_ON(ret > 0);
1280 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05001281 }
Chris Masondb945352007-10-15 16:15:53 -04001282 buf = btrfs_find_create_tree_block(root, ins.objectid, blocksize);
Chris Mason54aa1f42007-06-22 14:16:25 -04001283 if (!buf) {
Chris Masondb945352007-10-15 16:15:53 -04001284 btrfs_free_extent(trans, root, ins.objectid, blocksize, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001285 return ERR_PTR(-ENOMEM);
1286 }
Chris Mason5f39d392007-10-15 16:14:19 -04001287 btrfs_set_buffer_uptodate(buf);
1288 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
1289 buf->start + buf->len - 1, GFP_NOFS);
Chris Mason19c00dd2007-10-15 16:19:22 -04001290 set_extent_bits(&BTRFS_I(root->fs_info->btree_inode)->extent_tree,
1291 buf->start, buf->start + buf->len - 1,
1292 EXTENT_CSUM, GFP_NOFS);
1293 buf->flags |= EXTENT_CSUM;
Chris Mason6b800532007-10-15 16:17:34 -04001294 btrfs_set_buffer_defrag(buf);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001295 trans->blocks_used++;
Chris Masonfec577f2007-02-26 10:40:21 -05001296 return buf;
1297}
Chris Masona28ec192007-03-06 20:08:01 -05001298
Chris Mason6407bf62007-03-27 06:33:00 -04001299static int drop_leaf_ref(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001300 struct btrfs_root *root, struct extent_buffer *leaf)
Chris Mason6407bf62007-03-27 06:33:00 -04001301{
Chris Mason5f39d392007-10-15 16:14:19 -04001302 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -04001303 struct btrfs_file_extent_item *fi;
1304 int i;
1305 int nritems;
1306 int ret;
1307
Chris Mason5f39d392007-10-15 16:14:19 -04001308 BUG_ON(!btrfs_is_leaf(leaf));
1309 nritems = btrfs_header_nritems(leaf);
Chris Mason6407bf62007-03-27 06:33:00 -04001310 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04001311 u64 disk_bytenr;
Chris Mason5f39d392007-10-15 16:14:19 -04001312
1313 btrfs_item_key_to_cpu(leaf, &key, i);
1314 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason6407bf62007-03-27 06:33:00 -04001315 continue;
1316 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001317 if (btrfs_file_extent_type(leaf, fi) ==
1318 BTRFS_FILE_EXTENT_INLINE)
Chris Mason236454d2007-04-19 13:37:44 -04001319 continue;
Chris Mason6407bf62007-03-27 06:33:00 -04001320 /*
1321 * FIXME make sure to insert a trans record that
1322 * repeats the snapshot del on crash
1323 */
Chris Masondb945352007-10-15 16:15:53 -04001324 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
1325 if (disk_bytenr == 0)
Chris Mason3a686372007-05-24 13:35:57 -04001326 continue;
Chris Masondb945352007-10-15 16:15:53 -04001327 ret = btrfs_free_extent(trans, root, disk_bytenr,
1328 btrfs_file_extent_disk_num_bytes(leaf, fi), 0);
Chris Mason6407bf62007-03-27 06:33:00 -04001329 BUG_ON(ret);
1330 }
1331 return 0;
1332}
1333
Chris Masone0115992007-06-19 16:23:05 -04001334static void reada_walk_down(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -04001335 struct extent_buffer *node)
Chris Masone0115992007-06-19 16:23:05 -04001336{
1337 int i;
1338 u32 nritems;
Chris Masondb945352007-10-15 16:15:53 -04001339 u64 bytenr;
Chris Masone0115992007-06-19 16:23:05 -04001340 int ret;
1341 u32 refs;
Chris Masondb945352007-10-15 16:15:53 -04001342 int level;
1343 u32 blocksize;
Chris Masone0115992007-06-19 16:23:05 -04001344
Chris Mason5f39d392007-10-15 16:14:19 -04001345 nritems = btrfs_header_nritems(node);
Chris Masondb945352007-10-15 16:15:53 -04001346 level = btrfs_header_level(node);
Chris Masone0115992007-06-19 16:23:05 -04001347 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04001348 bytenr = btrfs_node_blockptr(node, i);
1349 blocksize = btrfs_level_size(root, level - 1);
1350 ret = lookup_extent_ref(NULL, root, bytenr, blocksize, &refs);
Chris Masone0115992007-06-19 16:23:05 -04001351 BUG_ON(ret);
1352 if (refs != 1)
1353 continue;
Chris Mason409eb952007-08-08 20:17:12 -04001354 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masondb945352007-10-15 16:15:53 -04001355 ret = readahead_tree_block(root, bytenr, blocksize);
Chris Mason409eb952007-08-08 20:17:12 -04001356 cond_resched();
1357 mutex_lock(&root->fs_info->fs_mutex);
Chris Masone0115992007-06-19 16:23:05 -04001358 if (ret)
1359 break;
1360 }
1361}
1362
Chris Mason9aca1d52007-03-13 11:09:37 -04001363/*
1364 * helper function for drop_snapshot, this walks down the tree dropping ref
1365 * counts as it goes.
1366 */
Chris Masone089f052007-03-16 16:20:31 -04001367static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
1368 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05001369{
Chris Mason5f39d392007-10-15 16:14:19 -04001370 struct extent_buffer *next;
1371 struct extent_buffer *cur;
Chris Masondb945352007-10-15 16:15:53 -04001372 u64 bytenr;
1373 u32 blocksize;
Chris Mason20524f02007-03-10 06:35:47 -05001374 int ret;
1375 u32 refs;
1376
Chris Mason5caf2a02007-04-02 11:20:42 -04001377 WARN_ON(*level < 0);
1378 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason5f39d392007-10-15 16:14:19 -04001379 ret = lookup_extent_ref(trans, root,
Chris Masondb945352007-10-15 16:15:53 -04001380 path->nodes[*level]->start,
1381 path->nodes[*level]->len, &refs);
Chris Mason20524f02007-03-10 06:35:47 -05001382 BUG_ON(ret);
1383 if (refs > 1)
1384 goto out;
Chris Masone0115992007-06-19 16:23:05 -04001385
Chris Mason9aca1d52007-03-13 11:09:37 -04001386 /*
1387 * walk down to the last node level and free all the leaves
1388 */
Chris Mason6407bf62007-03-27 06:33:00 -04001389 while(*level >= 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001390 WARN_ON(*level < 0);
1391 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason20524f02007-03-10 06:35:47 -05001392 cur = path->nodes[*level];
Chris Masone0115992007-06-19 16:23:05 -04001393
1394 if (*level > 0 && path->slots[*level] == 0)
Chris Mason5f39d392007-10-15 16:14:19 -04001395 reada_walk_down(root, cur);
Chris Masone0115992007-06-19 16:23:05 -04001396
Chris Mason5f39d392007-10-15 16:14:19 -04001397 if (btrfs_header_level(cur) != *level)
Chris Mason2c90e5d2007-04-02 10:50:19 -04001398 WARN_ON(1);
Chris Masone0115992007-06-19 16:23:05 -04001399
Chris Mason7518a232007-03-12 12:01:18 -04001400 if (path->slots[*level] >=
Chris Mason5f39d392007-10-15 16:14:19 -04001401 btrfs_header_nritems(cur))
Chris Mason20524f02007-03-10 06:35:47 -05001402 break;
Chris Mason6407bf62007-03-27 06:33:00 -04001403 if (*level == 0) {
1404 ret = drop_leaf_ref(trans, root, cur);
1405 BUG_ON(ret);
1406 break;
1407 }
Chris Masondb945352007-10-15 16:15:53 -04001408 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
1409 blocksize = btrfs_level_size(root, *level - 1);
1410 ret = lookup_extent_ref(trans, root, bytenr, blocksize, &refs);
Chris Mason6407bf62007-03-27 06:33:00 -04001411 BUG_ON(ret);
1412 if (refs != 1) {
Chris Mason20524f02007-03-10 06:35:47 -05001413 path->slots[*level]++;
Chris Masondb945352007-10-15 16:15:53 -04001414 ret = btrfs_free_extent(trans, root, bytenr,
1415 blocksize, 1);
Chris Mason20524f02007-03-10 06:35:47 -05001416 BUG_ON(ret);
1417 continue;
1418 }
Chris Masondb945352007-10-15 16:15:53 -04001419 next = btrfs_find_tree_block(root, bytenr, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -04001420 if (!next || !btrfs_buffer_uptodate(next)) {
1421 free_extent_buffer(next);
Chris Masone9d0b132007-08-10 14:06:19 -04001422 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masondb945352007-10-15 16:15:53 -04001423 next = read_tree_block(root, bytenr, blocksize);
Chris Masone9d0b132007-08-10 14:06:19 -04001424 mutex_lock(&root->fs_info->fs_mutex);
1425
1426 /* we dropped the lock, check one more time */
Chris Masondb945352007-10-15 16:15:53 -04001427 ret = lookup_extent_ref(trans, root, bytenr,
1428 blocksize, &refs);
Chris Masone9d0b132007-08-10 14:06:19 -04001429 BUG_ON(ret);
1430 if (refs != 1) {
1431 path->slots[*level]++;
Chris Mason5f39d392007-10-15 16:14:19 -04001432 free_extent_buffer(next);
Chris Masone9d0b132007-08-10 14:06:19 -04001433 ret = btrfs_free_extent(trans, root,
Chris Masondb945352007-10-15 16:15:53 -04001434 bytenr, blocksize, 1);
Chris Masone9d0b132007-08-10 14:06:19 -04001435 BUG_ON(ret);
1436 continue;
1437 }
1438 }
Chris Mason5caf2a02007-04-02 11:20:42 -04001439 WARN_ON(*level <= 0);
Chris Mason83e15a22007-03-12 09:03:27 -04001440 if (path->nodes[*level-1])
Chris Mason5f39d392007-10-15 16:14:19 -04001441 free_extent_buffer(path->nodes[*level-1]);
Chris Mason20524f02007-03-10 06:35:47 -05001442 path->nodes[*level-1] = next;
Chris Mason5f39d392007-10-15 16:14:19 -04001443 *level = btrfs_header_level(next);
Chris Mason20524f02007-03-10 06:35:47 -05001444 path->slots[*level] = 0;
1445 }
1446out:
Chris Mason5caf2a02007-04-02 11:20:42 -04001447 WARN_ON(*level < 0);
1448 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Masondb945352007-10-15 16:15:53 -04001449 ret = btrfs_free_extent(trans, root, path->nodes[*level]->start,
1450 path->nodes[*level]->len, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001451 free_extent_buffer(path->nodes[*level]);
Chris Mason20524f02007-03-10 06:35:47 -05001452 path->nodes[*level] = NULL;
1453 *level += 1;
1454 BUG_ON(ret);
1455 return 0;
1456}
1457
Chris Mason9aca1d52007-03-13 11:09:37 -04001458/*
1459 * helper for dropping snapshots. This walks back up the tree in the path
1460 * to find the first node higher up where we haven't yet gone through
1461 * all the slots
1462 */
Chris Masone089f052007-03-16 16:20:31 -04001463static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root
1464 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05001465{
1466 int i;
1467 int slot;
1468 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04001469 struct btrfs_root_item *root_item = &root->root_item;
1470
Chris Mason234b63a2007-03-13 10:46:10 -04001471 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Mason20524f02007-03-10 06:35:47 -05001472 slot = path->slots[i];
Chris Mason5f39d392007-10-15 16:14:19 -04001473 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
1474 struct extent_buffer *node;
1475 struct btrfs_disk_key disk_key;
1476 node = path->nodes[i];
Chris Mason20524f02007-03-10 06:35:47 -05001477 path->slots[i]++;
1478 *level = i;
Chris Mason9f3a7422007-08-07 15:52:19 -04001479 WARN_ON(*level == 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001480 btrfs_node_key(node, &disk_key, path->slots[i]);
Chris Mason9f3a7422007-08-07 15:52:19 -04001481 memcpy(&root_item->drop_progress,
Chris Mason5f39d392007-10-15 16:14:19 -04001482 &disk_key, sizeof(disk_key));
Chris Mason9f3a7422007-08-07 15:52:19 -04001483 root_item->drop_level = i;
Chris Mason20524f02007-03-10 06:35:47 -05001484 return 0;
1485 } else {
Chris Masone089f052007-03-16 16:20:31 -04001486 ret = btrfs_free_extent(trans, root,
Chris Masondb945352007-10-15 16:15:53 -04001487 path->nodes[*level]->start,
1488 path->nodes[*level]->len, 1);
Chris Mason6407bf62007-03-27 06:33:00 -04001489 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04001490 free_extent_buffer(path->nodes[*level]);
Chris Mason83e15a22007-03-12 09:03:27 -04001491 path->nodes[*level] = NULL;
Chris Mason20524f02007-03-10 06:35:47 -05001492 *level = i + 1;
Chris Mason20524f02007-03-10 06:35:47 -05001493 }
1494 }
1495 return 1;
1496}
1497
Chris Mason9aca1d52007-03-13 11:09:37 -04001498/*
1499 * drop the reference count on the tree rooted at 'snap'. This traverses
1500 * the tree freeing any blocks that have a ref count of zero after being
1501 * decremented.
1502 */
Chris Masone089f052007-03-16 16:20:31 -04001503int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason9f3a7422007-08-07 15:52:19 -04001504 *root)
Chris Mason20524f02007-03-10 06:35:47 -05001505{
Chris Mason3768f362007-03-13 16:47:54 -04001506 int ret = 0;
Chris Mason9aca1d52007-03-13 11:09:37 -04001507 int wret;
Chris Mason20524f02007-03-10 06:35:47 -05001508 int level;
Chris Mason5caf2a02007-04-02 11:20:42 -04001509 struct btrfs_path *path;
Chris Mason20524f02007-03-10 06:35:47 -05001510 int i;
1511 int orig_level;
Chris Mason9f3a7422007-08-07 15:52:19 -04001512 struct btrfs_root_item *root_item = &root->root_item;
Chris Mason20524f02007-03-10 06:35:47 -05001513
Chris Mason5caf2a02007-04-02 11:20:42 -04001514 path = btrfs_alloc_path();
1515 BUG_ON(!path);
Chris Mason20524f02007-03-10 06:35:47 -05001516
Chris Mason5f39d392007-10-15 16:14:19 -04001517 level = btrfs_header_level(root->node);
Chris Mason20524f02007-03-10 06:35:47 -05001518 orig_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04001519 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
1520 path->nodes[level] = root->node;
Chris Masonf510cfe2007-10-15 16:14:48 -04001521 extent_buffer_get(root->node);
Chris Mason9f3a7422007-08-07 15:52:19 -04001522 path->slots[level] = 0;
1523 } else {
1524 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001525 struct btrfs_disk_key found_key;
1526 struct extent_buffer *node;
Chris Mason6702ed42007-08-07 16:15:09 -04001527
Chris Mason9f3a7422007-08-07 15:52:19 -04001528 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Chris Mason6702ed42007-08-07 16:15:09 -04001529 level = root_item->drop_level;
1530 path->lowest_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04001531 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason6702ed42007-08-07 16:15:09 -04001532 if (wret < 0) {
Chris Mason9f3a7422007-08-07 15:52:19 -04001533 ret = wret;
1534 goto out;
1535 }
Chris Mason5f39d392007-10-15 16:14:19 -04001536 node = path->nodes[level];
1537 btrfs_node_key(node, &found_key, path->slots[level]);
1538 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
1539 sizeof(found_key)));
Chris Mason9f3a7422007-08-07 15:52:19 -04001540 }
Chris Mason20524f02007-03-10 06:35:47 -05001541 while(1) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001542 wret = walk_down_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04001543 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05001544 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04001545 if (wret < 0)
1546 ret = wret;
1547
Chris Mason5caf2a02007-04-02 11:20:42 -04001548 wret = walk_up_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04001549 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05001550 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04001551 if (wret < 0)
1552 ret = wret;
Chris Mason409eb952007-08-08 20:17:12 -04001553 ret = -EAGAIN;
Chris Mason409eb952007-08-08 20:17:12 -04001554 break;
Chris Mason20524f02007-03-10 06:35:47 -05001555 }
Chris Mason83e15a22007-03-12 09:03:27 -04001556 for (i = 0; i <= orig_level; i++) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001557 if (path->nodes[i]) {
Chris Mason5f39d392007-10-15 16:14:19 -04001558 free_extent_buffer(path->nodes[i]);
Chris Mason0f827312007-10-15 16:18:56 -04001559 path->nodes[i] = NULL;
Chris Mason83e15a22007-03-12 09:03:27 -04001560 }
Chris Mason20524f02007-03-10 06:35:47 -05001561 }
Chris Mason9f3a7422007-08-07 15:52:19 -04001562out:
Chris Mason5caf2a02007-04-02 11:20:42 -04001563 btrfs_free_path(path);
Chris Mason9aca1d52007-03-13 11:09:37 -04001564 return ret;
Chris Mason20524f02007-03-10 06:35:47 -05001565}
Chris Mason9078a3e2007-04-26 16:46:15 -04001566
Chris Masonbe744172007-05-06 10:15:01 -04001567int btrfs_free_block_groups(struct btrfs_fs_info *info)
1568{
Chris Masonf510cfe2007-10-15 16:14:48 -04001569 u64 start;
1570 u64 end;
Yanb97f9202007-11-01 11:28:41 -04001571 u64 ptr;
Chris Mason96b51792007-10-15 16:15:19 -04001572 int ret;
Chris Mason96b51792007-10-15 16:15:19 -04001573 while(1) {
1574 ret = find_first_extent_bit(&info->block_group_cache, 0,
1575 &start, &end, (unsigned int)-1);
1576 if (ret)
1577 break;
Yanb97f9202007-11-01 11:28:41 -04001578 ret = get_state_private(&info->block_group_cache, start, &ptr);
1579 if (!ret)
1580 kfree((void *)(unsigned long)ptr);
Chris Mason96b51792007-10-15 16:15:19 -04001581 clear_extent_bits(&info->block_group_cache, start,
1582 end, (unsigned int)-1, GFP_NOFS);
1583 }
Chris Masone37c9e62007-05-09 20:13:14 -04001584 while(1) {
Chris Masonf510cfe2007-10-15 16:14:48 -04001585 ret = find_first_extent_bit(&info->free_space_cache, 0,
1586 &start, &end, EXTENT_DIRTY);
1587 if (ret)
Chris Masone37c9e62007-05-09 20:13:14 -04001588 break;
Chris Masonf510cfe2007-10-15 16:14:48 -04001589 clear_extent_dirty(&info->free_space_cache, start,
1590 end, GFP_NOFS);
Chris Masone37c9e62007-05-09 20:13:14 -04001591 }
Chris Masonbe744172007-05-06 10:15:01 -04001592 return 0;
1593}
1594
Chris Mason9078a3e2007-04-26 16:46:15 -04001595int btrfs_read_block_groups(struct btrfs_root *root)
1596{
1597 struct btrfs_path *path;
1598 int ret;
1599 int err = 0;
Chris Mason96b51792007-10-15 16:15:19 -04001600 int bit;
Chris Mason9078a3e2007-04-26 16:46:15 -04001601 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04001602 struct btrfs_fs_info *info = root->fs_info;
Chris Mason96b51792007-10-15 16:15:19 -04001603 struct extent_map_tree *block_group_cache;
Chris Mason9078a3e2007-04-26 16:46:15 -04001604 struct btrfs_key key;
1605 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -04001606 struct extent_buffer *leaf;
Chris Mason96b51792007-10-15 16:15:19 -04001607
1608 block_group_cache = &info->block_group_cache;
Chris Mason9078a3e2007-04-26 16:46:15 -04001609
Chris Masonbe744172007-05-06 10:15:01 -04001610 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04001611 key.objectid = 0;
Chris Masondb945352007-10-15 16:15:53 -04001612 key.offset = BTRFS_BLOCK_GROUP_SIZE;
Chris Mason9078a3e2007-04-26 16:46:15 -04001613 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
1614
1615 path = btrfs_alloc_path();
1616 if (!path)
1617 return -ENOMEM;
1618
1619 while(1) {
Chris Masonbe744172007-05-06 10:15:01 -04001620 ret = btrfs_search_slot(NULL, info->extent_root,
Chris Mason9078a3e2007-04-26 16:46:15 -04001621 &key, path, 0, 0);
1622 if (ret != 0) {
1623 err = ret;
1624 break;
1625 }
Chris Mason5f39d392007-10-15 16:14:19 -04001626 leaf = path->nodes[0];
1627 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason9078a3e2007-04-26 16:46:15 -04001628 cache = kmalloc(sizeof(*cache), GFP_NOFS);
1629 if (!cache) {
1630 err = -1;
1631 break;
1632 }
Chris Mason3e1ad542007-05-07 20:03:49 -04001633
Chris Mason5f39d392007-10-15 16:14:19 -04001634 read_extent_buffer(leaf, &cache->item,
1635 btrfs_item_ptr_offset(leaf, path->slots[0]),
1636 sizeof(cache->item));
Chris Mason9078a3e2007-04-26 16:46:15 -04001637 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Masone37c9e62007-05-09 20:13:14 -04001638 cache->cached = 0;
Yan324ae4d2007-11-16 14:57:08 -05001639 cache->pinned = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04001640 key.objectid = found_key.objectid + found_key.offset;
1641 btrfs_release_path(root, path);
Chris Mason5f39d392007-10-15 16:14:19 -04001642
Chris Masonf84a8b32007-11-06 10:26:29 -05001643 if (cache->item.flags & BTRFS_BLOCK_GROUP_MIXED) {
1644 bit = BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA;
1645 cache->data = BTRFS_BLOCK_GROUP_MIXED;
1646 } else if (cache->item.flags & BTRFS_BLOCK_GROUP_DATA) {
Chris Mason96b51792007-10-15 16:15:19 -04001647 bit = BLOCK_GROUP_DATA;
Chris Masonf84a8b32007-11-06 10:26:29 -05001648 cache->data = BTRFS_BLOCK_GROUP_DATA;
Chris Mason96b51792007-10-15 16:15:19 -04001649 } else {
1650 bit = BLOCK_GROUP_METADATA;
1651 cache->data = 0;
Chris Mason31f3c992007-04-30 15:25:45 -04001652 }
Chris Mason96b51792007-10-15 16:15:19 -04001653
1654 /* use EXTENT_LOCKED to prevent merging */
1655 set_extent_bits(block_group_cache, found_key.objectid,
1656 found_key.objectid + found_key.offset - 1,
1657 bit | EXTENT_LOCKED, GFP_NOFS);
1658 set_state_private(block_group_cache, found_key.objectid,
Jens Axboeae2f5412007-10-19 09:22:59 -04001659 (unsigned long)cache);
Chris Mason96b51792007-10-15 16:15:19 -04001660
Chris Mason9078a3e2007-04-26 16:46:15 -04001661 if (key.objectid >=
Chris Masondb945352007-10-15 16:15:53 -04001662 btrfs_super_total_bytes(&info->super_copy))
Chris Mason9078a3e2007-04-26 16:46:15 -04001663 break;
1664 }
1665
1666 btrfs_free_path(path);
1667 return 0;
1668}