blob: 0f1ebdd4e925a28dd1ab69d0772962c78ecd26d7 [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 Mason74493f72007-12-11 09:25:06 -050020#include "hash.h"
Chris Masonfec577f2007-02-26 10:40:21 -050021#include "ctree.h"
22#include "disk-io.h"
23#include "print-tree.h"
Chris Masone089f052007-03-16 16:20:31 -040024#include "transaction.h"
Chris Masonfec577f2007-02-26 10:40:21 -050025
Chris Mason96b51792007-10-15 16:15:19 -040026#define BLOCK_GROUP_DATA EXTENT_WRITEBACK
27#define BLOCK_GROUP_METADATA EXTENT_UPTODATE
28#define BLOCK_GROUP_DIRTY EXTENT_DIRTY
29
Chris Masone089f052007-03-16 16:20:31 -040030static int finish_current_insert(struct btrfs_trans_handle *trans, struct
31 btrfs_root *extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -040032static int del_pending_extents(struct btrfs_trans_handle *trans, struct
33 btrfs_root *extent_root);
Chris Masonfec577f2007-02-26 10:40:21 -050034
Chris Masone37c9e62007-05-09 20:13:14 -040035static int cache_block_group(struct btrfs_root *root,
36 struct btrfs_block_group_cache *block_group)
37{
38 struct btrfs_path *path;
39 int ret;
40 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -040041 struct extent_buffer *leaf;
Chris Masonf510cfe2007-10-15 16:14:48 -040042 struct extent_map_tree *free_space_cache;
Chris Masone37c9e62007-05-09 20:13:14 -040043 int slot;
Chris Masone37c9e62007-05-09 20:13:14 -040044 u64 last = 0;
45 u64 hole_size;
Yan7d7d6062007-09-14 16:15:28 -040046 u64 first_free;
Chris Masone37c9e62007-05-09 20:13:14 -040047 int found = 0;
48
Chris Mason00f5c792007-11-30 10:09:33 -050049 if (!block_group)
50 return 0;
51
Chris Masone37c9e62007-05-09 20:13:14 -040052 root = root->fs_info->extent_root;
Chris Masonf510cfe2007-10-15 16:14:48 -040053 free_space_cache = &root->fs_info->free_space_cache;
Chris Masone37c9e62007-05-09 20:13:14 -040054
55 if (block_group->cached)
56 return 0;
Chris Masonf510cfe2007-10-15 16:14:48 -040057
Chris Masone37c9e62007-05-09 20:13:14 -040058 path = btrfs_alloc_path();
59 if (!path)
60 return -ENOMEM;
Yan7d7d6062007-09-14 16:15:28 -040061
Chris Mason2cc58cf2007-08-27 16:49:44 -040062 path->reada = 2;
Yan7d7d6062007-09-14 16:15:28 -040063 first_free = block_group->key.objectid;
Chris Masone37c9e62007-05-09 20:13:14 -040064 key.objectid = block_group->key.objectid;
Chris Masone37c9e62007-05-09 20:13:14 -040065 key.offset = 0;
Yan7d7d6062007-09-14 16:15:28 -040066
Chris Masone37c9e62007-05-09 20:13:14 -040067 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
68 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Yan7d7d6062007-09-14 16:15:28 -040069
Chris Masone37c9e62007-05-09 20:13:14 -040070 if (ret < 0)
71 return ret;
Yan7d7d6062007-09-14 16:15:28 -040072
Chris Masone37c9e62007-05-09 20:13:14 -040073 if (ret && path->slots[0] > 0)
74 path->slots[0]--;
Yan7d7d6062007-09-14 16:15:28 -040075
Chris Masone37c9e62007-05-09 20:13:14 -040076 while(1) {
Chris Mason5f39d392007-10-15 16:14:19 -040077 leaf = path->nodes[0];
Chris Masone37c9e62007-05-09 20:13:14 -040078 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -040079 if (slot >= btrfs_header_nritems(leaf)) {
Chris Masone37c9e62007-05-09 20:13:14 -040080 ret = btrfs_next_leaf(root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -040081 if (ret < 0)
82 goto err;
Chris Masonde428b62007-05-18 13:28:27 -040083 if (ret == 0) {
Chris Masone37c9e62007-05-09 20:13:14 -040084 continue;
Chris Masonde428b62007-05-18 13:28:27 -040085 } else {
Chris Masone37c9e62007-05-09 20:13:14 -040086 break;
87 }
88 }
Yan7d7d6062007-09-14 16:15:28 -040089
Chris Mason5f39d392007-10-15 16:14:19 -040090 btrfs_item_key_to_cpu(leaf, &key, slot);
Yan7d7d6062007-09-14 16:15:28 -040091 if (key.objectid < block_group->key.objectid) {
92 if (key.objectid + key.offset > first_free)
93 first_free = key.objectid + key.offset;
94 goto next;
95 }
96
Chris Masone37c9e62007-05-09 20:13:14 -040097 if (key.objectid >= block_group->key.objectid +
98 block_group->key.offset) {
Yan7d7d6062007-09-14 16:15:28 -040099 break;
100 }
101
102 if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) {
103 if (!found) {
104 last = first_free;
105 found = 1;
Chris Masone37c9e62007-05-09 20:13:14 -0400106 }
Chris Masonf510cfe2007-10-15 16:14:48 -0400107 if (key.objectid > last) {
108 hole_size = key.objectid - last;
109 set_extent_dirty(free_space_cache, last,
110 last + hole_size - 1,
111 GFP_NOFS);
Chris Masone37c9e62007-05-09 20:13:14 -0400112 }
Yan7d7d6062007-09-14 16:15:28 -0400113 last = key.objectid + key.offset;
Chris Masone37c9e62007-05-09 20:13:14 -0400114 }
Yan7d7d6062007-09-14 16:15:28 -0400115next:
Chris Masone37c9e62007-05-09 20:13:14 -0400116 path->slots[0]++;
117 }
118
Yan7d7d6062007-09-14 16:15:28 -0400119 if (!found)
120 last = first_free;
121 if (block_group->key.objectid +
122 block_group->key.offset > last) {
123 hole_size = block_group->key.objectid +
124 block_group->key.offset - last;
Chris Masonf510cfe2007-10-15 16:14:48 -0400125 set_extent_dirty(free_space_cache, last,
126 last + hole_size - 1, GFP_NOFS);
Yan7d7d6062007-09-14 16:15:28 -0400127 }
Chris Masone37c9e62007-05-09 20:13:14 -0400128 block_group->cached = 1;
Chris Mason54aa1f42007-06-22 14:16:25 -0400129err:
Chris Masone37c9e62007-05-09 20:13:14 -0400130 btrfs_free_path(path);
131 return 0;
132}
133
Chris Mason5276aed2007-06-11 21:33:38 -0400134struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
135 btrfs_fs_info *info,
Chris Masondb945352007-10-15 16:15:53 -0400136 u64 bytenr)
Chris Masonbe744172007-05-06 10:15:01 -0400137{
Chris Mason96b51792007-10-15 16:15:19 -0400138 struct extent_map_tree *block_group_cache;
139 struct btrfs_block_group_cache *block_group = NULL;
140 u64 ptr;
141 u64 start;
142 u64 end;
Chris Masonbe744172007-05-06 10:15:01 -0400143 int ret;
144
Chris Mason96b51792007-10-15 16:15:19 -0400145 block_group_cache = &info->block_group_cache;
146 ret = find_first_extent_bit(block_group_cache,
Chris Masondb945352007-10-15 16:15:53 -0400147 bytenr, &start, &end,
Chris Mason96b51792007-10-15 16:15:19 -0400148 BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA);
Chris Masonbe744172007-05-06 10:15:01 -0400149 if (ret) {
Chris Mason96b51792007-10-15 16:15:19 -0400150 return NULL;
Chris Masonbe744172007-05-06 10:15:01 -0400151 }
Chris Mason96b51792007-10-15 16:15:19 -0400152 ret = get_state_private(block_group_cache, start, &ptr);
153 if (ret)
154 return NULL;
155
Jens Axboeae2f5412007-10-19 09:22:59 -0400156 block_group = (struct btrfs_block_group_cache *)(unsigned long)ptr;
Yan5cf66422007-11-16 14:57:09 -0500157 if (block_group->key.objectid <= bytenr && bytenr <
Chris Mason96b51792007-10-15 16:15:19 -0400158 block_group->key.objectid + block_group->key.offset)
159 return block_group;
Chris Masonbe744172007-05-06 10:15:01 -0400160 return NULL;
161}
Chris Masone37c9e62007-05-09 20:13:14 -0400162static u64 find_search_start(struct btrfs_root *root,
163 struct btrfs_block_group_cache **cache_ret,
Yan5e5745d2007-11-16 14:57:09 -0500164 u64 search_start, int num,
165 int data, int full_scan)
Chris Masone37c9e62007-05-09 20:13:14 -0400166{
Chris Masone37c9e62007-05-09 20:13:14 -0400167 int ret;
168 struct btrfs_block_group_cache *cache = *cache_ret;
Chris Masone19caa52007-10-15 16:17:44 -0400169 u64 last;
Chris Masonf510cfe2007-10-15 16:14:48 -0400170 u64 start = 0;
171 u64 end = 0;
Chris Mason257d0ce2007-11-07 21:08:16 -0500172 u64 cache_miss = 0;
Chris Masonf84a8b32007-11-06 10:26:29 -0500173 int wrapped = 0;
Chris Masone37c9e62007-05-09 20:13:14 -0400174
Chris Mason00f5c792007-11-30 10:09:33 -0500175 if (!cache) {
Chris Mason1a2b2ac2007-12-04 13:18:24 -0500176 goto out;
Chris Mason00f5c792007-11-30 10:09:33 -0500177 }
Chris Masone37c9e62007-05-09 20:13:14 -0400178again:
Chris Mason54aa1f42007-06-22 14:16:25 -0400179 ret = cache_block_group(root, cache);
180 if (ret)
181 goto out;
Chris Masonf84a8b32007-11-06 10:26:29 -0500182
Chris Masone19caa52007-10-15 16:17:44 -0400183 last = max(search_start, cache->key.objectid);
184
Chris Masone37c9e62007-05-09 20:13:14 -0400185 while(1) {
Chris Masonf510cfe2007-10-15 16:14:48 -0400186 ret = find_first_extent_bit(&root->fs_info->free_space_cache,
187 last, &start, &end, EXTENT_DIRTY);
Chris Masone19caa52007-10-15 16:17:44 -0400188 if (ret) {
Chris Mason257d0ce2007-11-07 21:08:16 -0500189 if (!cache_miss)
190 cache_miss = last;
Chris Masone19caa52007-10-15 16:17:44 -0400191 goto new_group;
192 }
Chris Masonf510cfe2007-10-15 16:14:48 -0400193
194 start = max(last, start);
195 last = end + 1;
Chris Mason257d0ce2007-11-07 21:08:16 -0500196 if (last - start < num) {
197 if (last == cache->key.objectid + cache->key.offset)
198 cache_miss = start;
Chris Masonf510cfe2007-10-15 16:14:48 -0400199 continue;
Chris Mason257d0ce2007-11-07 21:08:16 -0500200 }
201 if (data != BTRFS_BLOCK_GROUP_MIXED &&
Yan5cf66422007-11-16 14:57:09 -0500202 start + num > cache->key.objectid + cache->key.offset)
Chris Masone37c9e62007-05-09 20:13:14 -0400203 goto new_group;
Chris Masonf510cfe2007-10-15 16:14:48 -0400204 return start;
Chris Masone37c9e62007-05-09 20:13:14 -0400205 }
206out:
Chris Mason1a2b2ac2007-12-04 13:18:24 -0500207 cache = btrfs_lookup_block_group(root->fs_info, search_start);
208 if (!cache) {
209 printk("Unable to find block group for %Lu\n",
210 search_start);
211 WARN_ON(1);
212 return search_start;
213 }
Chris Mason1a5bc1672007-10-15 16:15:26 -0400214 return search_start;
Chris Masone37c9e62007-05-09 20:13:14 -0400215
216new_group:
Chris Masone19caa52007-10-15 16:17:44 -0400217 last = cache->key.objectid + cache->key.offset;
Chris Masonf84a8b32007-11-06 10:26:29 -0500218wrapped:
Chris Masone19caa52007-10-15 16:17:44 -0400219 cache = btrfs_lookup_block_group(root->fs_info, last);
Chris Masone37c9e62007-05-09 20:13:14 -0400220 if (!cache) {
Chris Mason0e4de582007-11-26 10:55:49 -0500221no_cache:
Chris Masonf84a8b32007-11-06 10:26:29 -0500222 if (!wrapped) {
223 wrapped = 1;
224 last = search_start;
225 data = BTRFS_BLOCK_GROUP_MIXED;
226 goto wrapped;
227 }
Chris Mason1a2b2ac2007-12-04 13:18:24 -0500228 goto out;
Chris Masone37c9e62007-05-09 20:13:14 -0400229 }
Chris Mason257d0ce2007-11-07 21:08:16 -0500230 if (cache_miss && !cache->cached) {
231 cache_block_group(root, cache);
232 last = cache_miss;
Chris Mason257d0ce2007-11-07 21:08:16 -0500233 cache = btrfs_lookup_block_group(root->fs_info, last);
234 }
Chris Mason1a2b2ac2007-12-04 13:18:24 -0500235 cache = btrfs_find_block_group(root, cache, last, data, 0);
Chris Mason0e4de582007-11-26 10:55:49 -0500236 if (!cache)
237 goto no_cache;
Chris Masone37c9e62007-05-09 20:13:14 -0400238 *cache_ret = cache;
Chris Mason257d0ce2007-11-07 21:08:16 -0500239 cache_miss = 0;
Chris Masone37c9e62007-05-09 20:13:14 -0400240 goto again;
241}
242
Chris Mason84f54cf2007-06-12 07:43:08 -0400243static u64 div_factor(u64 num, int factor)
244{
Chris Mason257d0ce2007-11-07 21:08:16 -0500245 if (factor == 10)
246 return num;
Chris Mason84f54cf2007-06-12 07:43:08 -0400247 num *= factor;
248 do_div(num, 10);
249 return num;
250}
251
Chris Mason31f3c992007-04-30 15:25:45 -0400252struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
253 struct btrfs_block_group_cache
Chris Masonbe744172007-05-06 10:15:01 -0400254 *hint, u64 search_start,
Chris Masonde428b62007-05-18 13:28:27 -0400255 int data, int owner)
Chris Masoncd1bc462007-04-27 10:08:34 -0400256{
Chris Mason96b51792007-10-15 16:15:19 -0400257 struct btrfs_block_group_cache *cache;
258 struct extent_map_tree *block_group_cache;
Chris Mason31f3c992007-04-30 15:25:45 -0400259 struct btrfs_block_group_cache *found_group = NULL;
Chris Masoncd1bc462007-04-27 10:08:34 -0400260 struct btrfs_fs_info *info = root->fs_info;
261 u64 used;
Chris Mason31f3c992007-04-30 15:25:45 -0400262 u64 last = 0;
263 u64 hint_last;
Chris Mason96b51792007-10-15 16:15:19 -0400264 u64 start;
265 u64 end;
266 u64 free_check;
267 u64 ptr;
268 int bit;
Chris Masoncd1bc462007-04-27 10:08:34 -0400269 int ret;
Chris Mason31f3c992007-04-30 15:25:45 -0400270 int full_search = 0;
Chris Masonde428b62007-05-18 13:28:27 -0400271 int factor = 8;
Chris Mason1e2677e2007-05-29 16:52:18 -0400272 int data_swap = 0;
Chris Masonde428b62007-05-18 13:28:27 -0400273
Chris Mason96b51792007-10-15 16:15:19 -0400274 block_group_cache = &info->block_group_cache;
275
Chris Masonde428b62007-05-18 13:28:27 -0400276 if (!owner)
Chris Masonf84a8b32007-11-06 10:26:29 -0500277 factor = 8;
Chris Masonbe744172007-05-06 10:15:01 -0400278
Chris Mason257d0ce2007-11-07 21:08:16 -0500279 if (data == BTRFS_BLOCK_GROUP_MIXED) {
Chris Masonf84a8b32007-11-06 10:26:29 -0500280 bit = BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA;
Chris Mason257d0ce2007-11-07 21:08:16 -0500281 factor = 10;
282 } else if (data)
Chris Mason96b51792007-10-15 16:15:19 -0400283 bit = BLOCK_GROUP_DATA;
284 else
285 bit = BLOCK_GROUP_METADATA;
Chris Masonbe744172007-05-06 10:15:01 -0400286
287 if (search_start) {
288 struct btrfs_block_group_cache *shint;
Chris Mason5276aed2007-06-11 21:33:38 -0400289 shint = btrfs_lookup_block_group(info, search_start);
Chris Masonf84a8b32007-11-06 10:26:29 -0500290 if (shint && (shint->data == data ||
291 shint->data == BTRFS_BLOCK_GROUP_MIXED)) {
Chris Masonbe744172007-05-06 10:15:01 -0400292 used = btrfs_block_group_used(&shint->item);
Yan324ae4d2007-11-16 14:57:08 -0500293 if (used + shint->pinned <
294 div_factor(shint->key.offset, factor)) {
Chris Masonbe744172007-05-06 10:15:01 -0400295 return shint;
296 }
297 }
298 }
Chris Masonf84a8b32007-11-06 10:26:29 -0500299 if (hint && (hint->data == data ||
300 hint->data == BTRFS_BLOCK_GROUP_MIXED)) {
Chris Mason31f3c992007-04-30 15:25:45 -0400301 used = btrfs_block_group_used(&hint->item);
Yan324ae4d2007-11-16 14:57:08 -0500302 if (used + hint->pinned <
303 div_factor(hint->key.offset, factor)) {
Chris Mason31f3c992007-04-30 15:25:45 -0400304 return hint;
305 }
Chris Masone19caa52007-10-15 16:17:44 -0400306 last = hint->key.objectid + hint->key.offset;
Chris Mason31f3c992007-04-30 15:25:45 -0400307 hint_last = last;
308 } else {
Chris Masone37c9e62007-05-09 20:13:14 -0400309 if (hint)
310 hint_last = max(hint->key.objectid, search_start);
311 else
312 hint_last = search_start;
313
314 last = hint_last;
Chris Mason31f3c992007-04-30 15:25:45 -0400315 }
Chris Mason31f3c992007-04-30 15:25:45 -0400316again:
Chris Masoncd1bc462007-04-27 10:08:34 -0400317 while(1) {
Chris Mason96b51792007-10-15 16:15:19 -0400318 ret = find_first_extent_bit(block_group_cache, last,
319 &start, &end, bit);
320 if (ret)
Chris Masoncd1bc462007-04-27 10:08:34 -0400321 break;
Chris Mason96b51792007-10-15 16:15:19 -0400322
323 ret = get_state_private(block_group_cache, start, &ptr);
324 if (ret)
325 break;
326
Jens Axboeae2f5412007-10-19 09:22:59 -0400327 cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
Chris Mason96b51792007-10-15 16:15:19 -0400328 last = cache->key.objectid + cache->key.offset;
329 used = btrfs_block_group_used(&cache->item);
330
331 if (full_search)
332 free_check = cache->key.offset;
333 else
334 free_check = div_factor(cache->key.offset, factor);
Yan324ae4d2007-11-16 14:57:08 -0500335 if (used + cache->pinned < free_check) {
Chris Mason96b51792007-10-15 16:15:19 -0400336 found_group = cache;
337 goto found;
Chris Masoncd1bc462007-04-27 10:08:34 -0400338 }
Chris Masonde428b62007-05-18 13:28:27 -0400339 cond_resched();
Chris Masoncd1bc462007-04-27 10:08:34 -0400340 }
Chris Mason31f3c992007-04-30 15:25:45 -0400341 if (!full_search) {
Chris Masonbe744172007-05-06 10:15:01 -0400342 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -0400343 full_search = 1;
344 goto again;
345 }
Chris Mason1e2677e2007-05-29 16:52:18 -0400346 if (!data_swap) {
Chris Mason1e2677e2007-05-29 16:52:18 -0400347 data_swap = 1;
Chris Mason96b51792007-10-15 16:15:19 -0400348 bit = BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA;
Chris Mason1e2677e2007-05-29 16:52:18 -0400349 last = search_start;
350 goto again;
351 }
Chris Masonbe744172007-05-06 10:15:01 -0400352found:
Chris Mason31f3c992007-04-30 15:25:45 -0400353 return found_group;
Chris Masoncd1bc462007-04-27 10:08:34 -0400354}
355
Chris Mason74493f72007-12-11 09:25:06 -0500356static u64 hash_extent_ref(u64 root_objectid, u64 root_generation,
357 u64 owner, u64 owner_offset)
358{
359 u32 high_crc = ~(u32)0;
360 u32 low_crc = ~(u32)0;
361 __le64 lenum;
362
363 lenum = cpu_to_le64(root_objectid);
364 high_crc = crc32c(high_crc, &lenum, sizeof(lenum));
365 lenum = cpu_to_le64(root_generation);
366 high_crc = crc32c(high_crc, &lenum, sizeof(lenum));
367
368 lenum = cpu_to_le64(owner);
369 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
370
371 lenum = cpu_to_le64(owner_offset);
372 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
373
374 return ((u64)high_crc << 32) | (u64)low_crc;
375}
376
377int insert_extent_ref(struct btrfs_trans_handle *trans,
378 struct btrfs_root *root,
379 struct btrfs_path *path,
380 u64 bytenr,
381 u64 root_objectid, u64 root_generation,
382 u64 owner, u64 owner_offset)
383{
384 u64 hash;
385 struct btrfs_key key;
386 struct btrfs_extent_ref ref;
387 struct extent_buffer *l;
388 struct btrfs_extent_item *item;
389 int ret;
390
391 btrfs_set_stack_ref_root(&ref, root_objectid);
392 btrfs_set_stack_ref_generation(&ref, root_generation);
393 btrfs_set_stack_ref_objectid(&ref, owner);
394 btrfs_set_stack_ref_offset(&ref, owner_offset);
395
396 ret = btrfs_name_hash(&ref, sizeof(ref), &hash);
397 key.offset = hash;
398 key.objectid = bytenr;
399 key.type = BTRFS_EXTENT_REF_KEY;
400
401 ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(ref));
402 while (ret == -EEXIST) {
403
404 }
405
406}
407
Chris Masonb18c6682007-04-17 13:26:50 -0400408int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
409 struct btrfs_root *root,
Chris Mason74493f72007-12-11 09:25:06 -0500410 u64 bytenr, u64 num_bytes,
411 u64 root_objectid, u64 root_generation,
412 u64 owner, u64 owner_offset)
Chris Mason02217ed2007-03-02 16:08:05 -0500413{
Chris Mason5caf2a02007-04-02 11:20:42 -0400414 struct btrfs_path *path;
Chris Mason02217ed2007-03-02 16:08:05 -0500415 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400416 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400417 struct extent_buffer *l;
Chris Mason234b63a2007-03-13 10:46:10 -0400418 struct btrfs_extent_item *item;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400419 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500420
Chris Masondb945352007-10-15 16:15:53 -0400421 WARN_ON(num_bytes < root->sectorsize);
Chris Mason5caf2a02007-04-02 11:20:42 -0400422 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -0400423 if (!path)
424 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -0400425
Chris Masondb945352007-10-15 16:15:53 -0400426 key.objectid = bytenr;
Chris Mason62e27492007-03-15 12:56:47 -0400427 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masondb945352007-10-15 16:15:53 -0400428 key.offset = num_bytes;
Chris Mason5caf2a02007-04-02 11:20:42 -0400429 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -0400430 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400431 if (ret < 0)
432 return ret;
Chris Masona429e512007-04-18 16:15:28 -0400433 if (ret != 0) {
Chris Masona28ec192007-03-06 20:08:01 -0500434 BUG();
Chris Masona429e512007-04-18 16:15:28 -0400435 }
Chris Mason02217ed2007-03-02 16:08:05 -0500436 BUG_ON(ret != 0);
Chris Mason5f39d392007-10-15 16:14:19 -0400437 l = path->nodes[0];
Chris Mason5caf2a02007-04-02 11:20:42 -0400438 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400439 refs = btrfs_extent_refs(l, item);
440 btrfs_set_extent_refs(l, item, refs + 1);
Chris Mason5caf2a02007-04-02 11:20:42 -0400441 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masona28ec192007-03-06 20:08:01 -0500442
Chris Mason5caf2a02007-04-02 11:20:42 -0400443 btrfs_release_path(root->fs_info->extent_root, path);
Chris Mason9f5fae22007-03-20 14:38:32 -0400444 finish_current_insert(trans, root->fs_info->extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -0400445 del_pending_extents(trans, root->fs_info->extent_root);
Chris Mason74493f72007-12-11 09:25:06 -0500446
447 btrfs_free_path(path);
Chris Mason02217ed2007-03-02 16:08:05 -0500448 return 0;
449}
450
Chris Masone9d0b132007-08-10 14:06:19 -0400451int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
452 struct btrfs_root *root)
453{
454 finish_current_insert(trans, root->fs_info->extent_root);
455 del_pending_extents(trans, root->fs_info->extent_root);
456 return 0;
457}
458
Chris Masonb18c6682007-04-17 13:26:50 -0400459static int lookup_extent_ref(struct btrfs_trans_handle *trans,
Chris Masondb945352007-10-15 16:15:53 -0400460 struct btrfs_root *root, u64 bytenr,
461 u64 num_bytes, u32 *refs)
Chris Masona28ec192007-03-06 20:08:01 -0500462{
Chris Mason5caf2a02007-04-02 11:20:42 -0400463 struct btrfs_path *path;
Chris Masona28ec192007-03-06 20:08:01 -0500464 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400465 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400466 struct extent_buffer *l;
Chris Mason234b63a2007-03-13 10:46:10 -0400467 struct btrfs_extent_item *item;
Chris Mason5caf2a02007-04-02 11:20:42 -0400468
Chris Masondb945352007-10-15 16:15:53 -0400469 WARN_ON(num_bytes < root->sectorsize);
Chris Mason5caf2a02007-04-02 11:20:42 -0400470 path = btrfs_alloc_path();
Chris Masondb945352007-10-15 16:15:53 -0400471 key.objectid = bytenr;
472 key.offset = num_bytes;
Chris Mason62e27492007-03-15 12:56:47 -0400473 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Mason5caf2a02007-04-02 11:20:42 -0400474 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -0400475 0, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400476 if (ret < 0)
477 goto out;
Chris Mason5f39d392007-10-15 16:14:19 -0400478 if (ret != 0) {
479 btrfs_print_leaf(root, path->nodes[0]);
Chris Masondb945352007-10-15 16:15:53 -0400480 printk("failed to find block number %Lu\n", bytenr);
Chris Masona28ec192007-03-06 20:08:01 -0500481 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -0400482 }
483 l = path->nodes[0];
Chris Mason5caf2a02007-04-02 11:20:42 -0400484 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400485 *refs = btrfs_extent_refs(l, item);
Chris Mason54aa1f42007-06-22 14:16:25 -0400486out:
Chris Mason5caf2a02007-04-02 11:20:42 -0400487 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -0500488 return 0;
489}
490
Chris Masonc5739bb2007-04-10 09:27:04 -0400491int btrfs_inc_root_ref(struct btrfs_trans_handle *trans,
492 struct btrfs_root *root)
493{
Chris Masondb945352007-10-15 16:15:53 -0400494 return btrfs_inc_extent_ref(trans, root, root->node->start,
495 root->node->len);
Chris Masonc5739bb2007-04-10 09:27:04 -0400496}
497
Chris Masone089f052007-03-16 16:20:31 -0400498int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400499 struct extent_buffer *buf)
Chris Mason02217ed2007-03-02 16:08:05 -0500500{
Chris Masondb945352007-10-15 16:15:53 -0400501 u64 bytenr;
Chris Mason5f39d392007-10-15 16:14:19 -0400502 u32 nritems;
503 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -0400504 struct btrfs_file_extent_item *fi;
Chris Mason02217ed2007-03-02 16:08:05 -0500505 int i;
Chris Masondb945352007-10-15 16:15:53 -0400506 int level;
Chris Mason6407bf62007-03-27 06:33:00 -0400507 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -0400508 int faili;
509 int err;
Chris Masona28ec192007-03-06 20:08:01 -0500510
Chris Mason3768f362007-03-13 16:47:54 -0400511 if (!root->ref_cows)
Chris Masona28ec192007-03-06 20:08:01 -0500512 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400513
Chris Masondb945352007-10-15 16:15:53 -0400514 level = btrfs_header_level(buf);
Chris Mason5f39d392007-10-15 16:14:19 -0400515 nritems = btrfs_header_nritems(buf);
516 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -0400517 if (level == 0) {
518 u64 disk_bytenr;
Chris Mason5f39d392007-10-15 16:14:19 -0400519 btrfs_item_key_to_cpu(buf, &key, i);
520 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason6407bf62007-03-27 06:33:00 -0400521 continue;
Chris Mason5f39d392007-10-15 16:14:19 -0400522 fi = btrfs_item_ptr(buf, i,
Chris Mason6407bf62007-03-27 06:33:00 -0400523 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400524 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason236454df2007-04-19 13:37:44 -0400525 BTRFS_FILE_EXTENT_INLINE)
526 continue;
Chris Masondb945352007-10-15 16:15:53 -0400527 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
528 if (disk_bytenr == 0)
Chris Mason3a686372007-05-24 13:35:57 -0400529 continue;
Chris Masondb945352007-10-15 16:15:53 -0400530 ret = btrfs_inc_extent_ref(trans, root, disk_bytenr,
531 btrfs_file_extent_disk_num_bytes(buf, fi));
Chris Mason54aa1f42007-06-22 14:16:25 -0400532 if (ret) {
533 faili = i;
534 goto fail;
535 }
Chris Mason6407bf62007-03-27 06:33:00 -0400536 } else {
Chris Masondb945352007-10-15 16:15:53 -0400537 bytenr = btrfs_node_blockptr(buf, i);
538 ret = btrfs_inc_extent_ref(trans, root, bytenr,
539 btrfs_level_size(root, level - 1));
Chris Mason54aa1f42007-06-22 14:16:25 -0400540 if (ret) {
541 faili = i;
542 goto fail;
543 }
Chris Mason6407bf62007-03-27 06:33:00 -0400544 }
Chris Mason02217ed2007-03-02 16:08:05 -0500545 }
546 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -0400547fail:
Chris Masonccd467d2007-06-28 15:57:36 -0400548 WARN_ON(1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400549 for (i =0; i < faili; i++) {
Chris Masondb945352007-10-15 16:15:53 -0400550 if (level == 0) {
551 u64 disk_bytenr;
Chris Mason5f39d392007-10-15 16:14:19 -0400552 btrfs_item_key_to_cpu(buf, &key, i);
553 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -0400554 continue;
Chris Mason5f39d392007-10-15 16:14:19 -0400555 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -0400556 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400557 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -0400558 BTRFS_FILE_EXTENT_INLINE)
559 continue;
Chris Masondb945352007-10-15 16:15:53 -0400560 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
561 if (disk_bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -0400562 continue;
Chris Masondb945352007-10-15 16:15:53 -0400563 err = btrfs_free_extent(trans, root, disk_bytenr,
564 btrfs_file_extent_disk_num_bytes(buf,
Chris Mason5f39d392007-10-15 16:14:19 -0400565 fi), 0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400566 BUG_ON(err);
567 } else {
Chris Masondb945352007-10-15 16:15:53 -0400568 bytenr = btrfs_node_blockptr(buf, i);
569 err = btrfs_free_extent(trans, root, bytenr,
570 btrfs_level_size(root, level - 1), 0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400571 BUG_ON(err);
572 }
573 }
574 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -0500575}
576
Chris Mason9078a3e2007-04-26 16:46:15 -0400577static int write_one_cache_group(struct btrfs_trans_handle *trans,
578 struct btrfs_root *root,
579 struct btrfs_path *path,
580 struct btrfs_block_group_cache *cache)
581{
582 int ret;
583 int pending_ret;
584 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -0400585 unsigned long bi;
586 struct extent_buffer *leaf;
Chris Mason9078a3e2007-04-26 16:46:15 -0400587
Chris Mason9078a3e2007-04-26 16:46:15 -0400588 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400589 if (ret < 0)
590 goto fail;
Chris Mason9078a3e2007-04-26 16:46:15 -0400591 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -0400592
593 leaf = path->nodes[0];
594 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
595 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
596 btrfs_mark_buffer_dirty(leaf);
Chris Mason9078a3e2007-04-26 16:46:15 -0400597 btrfs_release_path(extent_root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -0400598fail:
Chris Mason9078a3e2007-04-26 16:46:15 -0400599 finish_current_insert(trans, extent_root);
600 pending_ret = del_pending_extents(trans, extent_root);
601 if (ret)
602 return ret;
603 if (pending_ret)
604 return pending_ret;
605 return 0;
606
607}
608
Chris Mason96b51792007-10-15 16:15:19 -0400609int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
610 struct btrfs_root *root)
Chris Mason9078a3e2007-04-26 16:46:15 -0400611{
Chris Mason96b51792007-10-15 16:15:19 -0400612 struct extent_map_tree *block_group_cache;
613 struct btrfs_block_group_cache *cache;
Chris Mason9078a3e2007-04-26 16:46:15 -0400614 int ret;
615 int err = 0;
616 int werr = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -0400617 struct btrfs_path *path;
Chris Mason96b51792007-10-15 16:15:19 -0400618 u64 last = 0;
619 u64 start;
620 u64 end;
621 u64 ptr;
Chris Mason9078a3e2007-04-26 16:46:15 -0400622
Chris Mason96b51792007-10-15 16:15:19 -0400623 block_group_cache = &root->fs_info->block_group_cache;
Chris Mason9078a3e2007-04-26 16:46:15 -0400624 path = btrfs_alloc_path();
625 if (!path)
626 return -ENOMEM;
627
628 while(1) {
Chris Mason96b51792007-10-15 16:15:19 -0400629 ret = find_first_extent_bit(block_group_cache, last,
630 &start, &end, BLOCK_GROUP_DIRTY);
631 if (ret)
Chris Mason9078a3e2007-04-26 16:46:15 -0400632 break;
Chris Mason54aa1f42007-06-22 14:16:25 -0400633
Chris Mason96b51792007-10-15 16:15:19 -0400634 last = end + 1;
635 ret = get_state_private(block_group_cache, start, &ptr);
636 if (ret)
637 break;
638
Jens Axboeae2f5412007-10-19 09:22:59 -0400639 cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
Chris Mason96b51792007-10-15 16:15:19 -0400640 err = write_one_cache_group(trans, root,
641 path, cache);
642 /*
643 * if we fail to write the cache group, we want
644 * to keep it marked dirty in hopes that a later
645 * write will work
646 */
647 if (err) {
648 werr = err;
649 continue;
Chris Mason9078a3e2007-04-26 16:46:15 -0400650 }
Chris Mason96b51792007-10-15 16:15:19 -0400651 clear_extent_bits(block_group_cache, start, end,
652 BLOCK_GROUP_DIRTY, GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -0400653 }
654 btrfs_free_path(path);
655 return werr;
656}
657
658static int update_block_group(struct btrfs_trans_handle *trans,
659 struct btrfs_root *root,
Chris Masondb945352007-10-15 16:15:53 -0400660 u64 bytenr, u64 num_bytes, int alloc,
661 int mark_free, int data)
Chris Mason9078a3e2007-04-26 16:46:15 -0400662{
663 struct btrfs_block_group_cache *cache;
664 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -0400665 u64 total = num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -0400666 u64 old_val;
Chris Masondb945352007-10-15 16:15:53 -0400667 u64 byte_in_group;
Chris Mason96b51792007-10-15 16:15:19 -0400668 u64 start;
669 u64 end;
Chris Mason3e1ad542007-05-07 20:03:49 -0400670
Chris Mason9078a3e2007-04-26 16:46:15 -0400671 while(total) {
Chris Masondb945352007-10-15 16:15:53 -0400672 cache = btrfs_lookup_block_group(info, bytenr);
Chris Mason3e1ad542007-05-07 20:03:49 -0400673 if (!cache) {
Chris Mason9078a3e2007-04-26 16:46:15 -0400674 return -1;
Chris Masoncd1bc462007-04-27 10:08:34 -0400675 }
Chris Masondb945352007-10-15 16:15:53 -0400676 byte_in_group = bytenr - cache->key.objectid;
677 WARN_ON(byte_in_group > cache->key.offset);
Chris Mason96b51792007-10-15 16:15:19 -0400678 start = cache->key.objectid;
679 end = start + cache->key.offset - 1;
680 set_extent_bits(&info->block_group_cache, start, end,
681 BLOCK_GROUP_DIRTY, GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -0400682
683 old_val = btrfs_block_group_used(&cache->item);
Chris Masondb945352007-10-15 16:15:53 -0400684 num_bytes = min(total, cache->key.offset - byte_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -0400685 if (alloc) {
Chris Mason1e2677e2007-05-29 16:52:18 -0400686 if (cache->data != data &&
Chris Mason84f54cf2007-06-12 07:43:08 -0400687 old_val < (cache->key.offset >> 1)) {
Chris Mason96b51792007-10-15 16:15:19 -0400688 int bit_to_clear;
689 int bit_to_set;
Chris Mason96b51792007-10-15 16:15:19 -0400690 cache->data = data;
Chris Mason1e2677e2007-05-29 16:52:18 -0400691 if (data) {
Yanb97f9202007-11-01 11:28:41 -0400692 bit_to_clear = BLOCK_GROUP_METADATA;
693 bit_to_set = BLOCK_GROUP_DATA;
Chris Masonf84a8b32007-11-06 10:26:29 -0500694 cache->item.flags &=
695 ~BTRFS_BLOCK_GROUP_MIXED;
Chris Mason1e2677e2007-05-29 16:52:18 -0400696 cache->item.flags |=
697 BTRFS_BLOCK_GROUP_DATA;
698 } else {
Yanb97f9202007-11-01 11:28:41 -0400699 bit_to_clear = BLOCK_GROUP_DATA;
700 bit_to_set = BLOCK_GROUP_METADATA;
Chris Mason1e2677e2007-05-29 16:52:18 -0400701 cache->item.flags &=
Chris Masonf84a8b32007-11-06 10:26:29 -0500702 ~BTRFS_BLOCK_GROUP_MIXED;
703 cache->item.flags &=
Chris Mason1e2677e2007-05-29 16:52:18 -0400704 ~BTRFS_BLOCK_GROUP_DATA;
705 }
Chris Mason96b51792007-10-15 16:15:19 -0400706 clear_extent_bits(&info->block_group_cache,
707 start, end, bit_to_clear,
708 GFP_NOFS);
709 set_extent_bits(&info->block_group_cache,
710 start, end, bit_to_set,
711 GFP_NOFS);
Chris Masonf84a8b32007-11-06 10:26:29 -0500712 } else if (cache->data != data &&
713 cache->data != BTRFS_BLOCK_GROUP_MIXED) {
714 cache->data = BTRFS_BLOCK_GROUP_MIXED;
715 set_extent_bits(&info->block_group_cache,
716 start, end,
717 BLOCK_GROUP_DATA |
718 BLOCK_GROUP_METADATA,
719 GFP_NOFS);
Chris Mason1e2677e2007-05-29 16:52:18 -0400720 }
Chris Masondb945352007-10-15 16:15:53 -0400721 old_val += num_bytes;
Chris Masoncd1bc462007-04-27 10:08:34 -0400722 } else {
Chris Masondb945352007-10-15 16:15:53 -0400723 old_val -= num_bytes;
Chris Masonf510cfe2007-10-15 16:14:48 -0400724 if (mark_free) {
725 set_extent_dirty(&info->free_space_cache,
Chris Masondb945352007-10-15 16:15:53 -0400726 bytenr, bytenr + num_bytes - 1,
Chris Masonf510cfe2007-10-15 16:14:48 -0400727 GFP_NOFS);
Chris Masone37c9e62007-05-09 20:13:14 -0400728 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400729 }
Chris Mason9078a3e2007-04-26 16:46:15 -0400730 btrfs_set_block_group_used(&cache->item, old_val);
Chris Masondb945352007-10-15 16:15:53 -0400731 total -= num_bytes;
732 bytenr += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -0400733 }
734 return 0;
735}
Yan324ae4d2007-11-16 14:57:08 -0500736static int update_pinned_extents(struct btrfs_root *root,
737 u64 bytenr, u64 num, int pin)
738{
739 u64 len;
740 struct btrfs_block_group_cache *cache;
741 struct btrfs_fs_info *fs_info = root->fs_info;
742
743 if (pin) {
744 set_extent_dirty(&fs_info->pinned_extents,
745 bytenr, bytenr + num - 1, GFP_NOFS);
746 } else {
747 clear_extent_dirty(&fs_info->pinned_extents,
748 bytenr, bytenr + num - 1, GFP_NOFS);
749 }
750 while (num > 0) {
751 cache = btrfs_lookup_block_group(fs_info, bytenr);
752 WARN_ON(!cache);
753 len = min(num, cache->key.offset -
754 (bytenr - cache->key.objectid));
755 if (pin) {
756 cache->pinned += len;
757 fs_info->total_pinned += len;
758 } else {
759 cache->pinned -= len;
760 fs_info->total_pinned -= len;
761 }
762 bytenr += len;
763 num -= len;
764 }
765 return 0;
766}
Chris Mason9078a3e2007-04-26 16:46:15 -0400767
Chris Mason1a5bc1672007-10-15 16:15:26 -0400768int btrfs_copy_pinned(struct btrfs_root *root, struct extent_map_tree *copy)
Chris Masonccd467d2007-06-28 15:57:36 -0400769{
Chris Masonccd467d2007-06-28 15:57:36 -0400770 u64 last = 0;
Chris Mason1a5bc1672007-10-15 16:15:26 -0400771 u64 start;
772 u64 end;
773 struct extent_map_tree *pinned_extents = &root->fs_info->pinned_extents;
Chris Masonccd467d2007-06-28 15:57:36 -0400774 int ret;
Chris Masonccd467d2007-06-28 15:57:36 -0400775
776 while(1) {
Chris Mason1a5bc1672007-10-15 16:15:26 -0400777 ret = find_first_extent_bit(pinned_extents, last,
778 &start, &end, EXTENT_DIRTY);
779 if (ret)
Chris Masonccd467d2007-06-28 15:57:36 -0400780 break;
Chris Mason1a5bc1672007-10-15 16:15:26 -0400781 set_extent_dirty(copy, start, end, GFP_NOFS);
782 last = end + 1;
Chris Masonccd467d2007-06-28 15:57:36 -0400783 }
784 return 0;
785}
786
787int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
788 struct btrfs_root *root,
Chris Mason1a5bc1672007-10-15 16:15:26 -0400789 struct extent_map_tree *unpin)
Chris Masona28ec192007-03-06 20:08:01 -0500790{
Chris Mason1a5bc1672007-10-15 16:15:26 -0400791 u64 start;
792 u64 end;
Chris Masona28ec192007-03-06 20:08:01 -0500793 int ret;
Chris Masonf510cfe2007-10-15 16:14:48 -0400794 struct extent_map_tree *free_space_cache;
Chris Masonf510cfe2007-10-15 16:14:48 -0400795 free_space_cache = &root->fs_info->free_space_cache;
Chris Masona28ec192007-03-06 20:08:01 -0500796
797 while(1) {
Chris Mason1a5bc1672007-10-15 16:15:26 -0400798 ret = find_first_extent_bit(unpin, 0, &start, &end,
799 EXTENT_DIRTY);
800 if (ret)
Chris Masona28ec192007-03-06 20:08:01 -0500801 break;
Yan324ae4d2007-11-16 14:57:08 -0500802 update_pinned_extents(root, start, end + 1 - start, 0);
Chris Mason1a5bc1672007-10-15 16:15:26 -0400803 clear_extent_dirty(unpin, start, end, GFP_NOFS);
804 set_extent_dirty(free_space_cache, start, end, GFP_NOFS);
Chris Masona28ec192007-03-06 20:08:01 -0500805 }
806 return 0;
807}
808
Chris Masone089f052007-03-16 16:20:31 -0400809static int finish_current_insert(struct btrfs_trans_handle *trans, struct
810 btrfs_root *extent_root)
Chris Mason037e6392007-03-07 11:50:24 -0500811{
Chris Masone2fa7222007-03-12 16:22:34 -0400812 struct btrfs_key ins;
Chris Mason234b63a2007-03-13 10:46:10 -0400813 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -0500814 int ret;
Chris Mason1a5bc1672007-10-15 16:15:26 -0400815 int err = 0;
816 u64 start;
817 u64 end;
Chris Mason1261ec42007-03-20 20:35:03 -0400818 struct btrfs_fs_info *info = extent_root->fs_info;
Chris Mason037e6392007-03-07 11:50:24 -0500819
Chris Mason5f39d392007-10-15 16:14:19 -0400820 btrfs_set_stack_extent_refs(&extent_item, 1);
Chris Mason62e27492007-03-15 12:56:47 -0400821 btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
Chris Mason5f39d392007-10-15 16:14:19 -0400822 btrfs_set_stack_extent_owner(&extent_item,
823 extent_root->root_key.objectid);
Chris Mason037e6392007-03-07 11:50:24 -0500824
Chris Mason26b80032007-08-08 20:17:12 -0400825 while(1) {
Chris Mason1a5bc1672007-10-15 16:15:26 -0400826 ret = find_first_extent_bit(&info->extent_ins, 0, &start,
827 &end, EXTENT_LOCKED);
828 if (ret)
Chris Mason26b80032007-08-08 20:17:12 -0400829 break;
830
Chris Mason1a5bc1672007-10-15 16:15:26 -0400831 ins.objectid = start;
832 ins.offset = end + 1 - start;
833 err = btrfs_insert_item(trans, extent_root, &ins,
834 &extent_item, sizeof(extent_item));
835 clear_extent_bits(&info->extent_ins, start, end, EXTENT_LOCKED,
836 GFP_NOFS);
Chris Mason037e6392007-03-07 11:50:24 -0500837 }
Chris Mason037e6392007-03-07 11:50:24 -0500838 return 0;
839}
840
Chris Masondb945352007-10-15 16:15:53 -0400841static int pin_down_bytes(struct btrfs_root *root, u64 bytenr, u32 num_bytes,
842 int pending)
Chris Masone20d96d2007-03-22 12:13:20 -0400843{
Chris Mason1a5bc1672007-10-15 16:15:26 -0400844 int err = 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400845 struct extent_buffer *buf;
Chris Mason78fae272007-03-25 11:35:08 -0400846
Chris Masonf4b9aa82007-03-27 11:05:53 -0400847 if (!pending) {
Chris Masondb945352007-10-15 16:15:53 -0400848 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
Chris Mason5f39d392007-10-15 16:14:19 -0400849 if (buf) {
850 if (btrfs_buffer_uptodate(buf)) {
Chris Mason2c90e5d2007-04-02 10:50:19 -0400851 u64 transid =
852 root->fs_info->running_transaction->transid;
Chris Mason5f39d392007-10-15 16:14:19 -0400853 if (btrfs_header_generation(buf) == transid) {
854 free_extent_buffer(buf);
Yanc5492282007-11-06 10:25:25 -0500855 return 1;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400856 }
Chris Masonf4b9aa82007-03-27 11:05:53 -0400857 }
Chris Mason5f39d392007-10-15 16:14:19 -0400858 free_extent_buffer(buf);
Chris Mason8ef97622007-03-26 10:15:30 -0400859 }
Yan324ae4d2007-11-16 14:57:08 -0500860 update_pinned_extents(root, bytenr, num_bytes, 1);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400861 } else {
Chris Mason1a5bc1672007-10-15 16:15:26 -0400862 set_extent_bits(&root->fs_info->pending_del,
Chris Masondb945352007-10-15 16:15:53 -0400863 bytenr, bytenr + num_bytes - 1,
864 EXTENT_LOCKED, GFP_NOFS);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400865 }
Chris Masonbe744172007-05-06 10:15:01 -0400866 BUG_ON(err < 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400867 return 0;
868}
869
Chris Masona28ec192007-03-06 20:08:01 -0500870/*
871 * remove an extent from the root, returns 0 on success
872 */
Chris Masone089f052007-03-16 16:20:31 -0400873static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masondb945352007-10-15 16:15:53 -0400874 *root, u64 bytenr, u64 num_bytes, int pin,
Chris Masone37c9e62007-05-09 20:13:14 -0400875 int mark_free)
Chris Masona28ec192007-03-06 20:08:01 -0500876{
Chris Mason5caf2a02007-04-02 11:20:42 -0400877 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -0400878 struct btrfs_key key;
Chris Mason1261ec42007-03-20 20:35:03 -0400879 struct btrfs_fs_info *info = root->fs_info;
880 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -0400881 struct extent_buffer *leaf;
Chris Masona28ec192007-03-06 20:08:01 -0500882 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400883 struct btrfs_extent_item *ei;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400884 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500885
Chris Masondb945352007-10-15 16:15:53 -0400886 key.objectid = bytenr;
Chris Mason62e27492007-03-15 12:56:47 -0400887 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masondb945352007-10-15 16:15:53 -0400888 key.offset = num_bytes;
Chris Masona28ec192007-03-06 20:08:01 -0500889
Chris Mason5caf2a02007-04-02 11:20:42 -0400890 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -0400891 if (!path)
892 return -ENOMEM;
893
Chris Mason5caf2a02007-04-02 11:20:42 -0400894 ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400895 if (ret < 0)
896 return ret;
897 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -0400898
899 leaf = path->nodes[0];
900 ei = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason123abc82007-03-14 14:14:43 -0400901 struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400902 refs = btrfs_extent_refs(leaf, ei);
903 BUG_ON(refs == 0);
904 refs -= 1;
905 btrfs_set_extent_refs(leaf, ei, refs);
906 btrfs_mark_buffer_dirty(leaf);
907
Chris Masoncf27e1e2007-03-13 09:49:06 -0400908 if (refs == 0) {
Chris Masondb945352007-10-15 16:15:53 -0400909 u64 super_used;
910 u64 root_used;
Chris Mason78fae272007-03-25 11:35:08 -0400911
912 if (pin) {
Chris Masondb945352007-10-15 16:15:53 -0400913 ret = pin_down_bytes(root, bytenr, num_bytes, 0);
Yanc5492282007-11-06 10:25:25 -0500914 if (ret > 0)
915 mark_free = 1;
916 BUG_ON(ret < 0);
Chris Mason78fae272007-03-25 11:35:08 -0400917 }
918
Josef Bacik58176a92007-08-29 15:47:34 -0400919 /* block accounting for super block */
Chris Masondb945352007-10-15 16:15:53 -0400920 super_used = btrfs_super_bytes_used(&info->super_copy);
921 btrfs_set_super_bytes_used(&info->super_copy,
922 super_used - num_bytes);
Josef Bacik58176a92007-08-29 15:47:34 -0400923
924 /* block accounting for root item */
Chris Masondb945352007-10-15 16:15:53 -0400925 root_used = btrfs_root_used(&root->root_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400926 btrfs_set_root_used(&root->root_item,
Chris Masondb945352007-10-15 16:15:53 -0400927 root_used - num_bytes);
Josef Bacik58176a92007-08-29 15:47:34 -0400928
Chris Mason5caf2a02007-04-02 11:20:42 -0400929 ret = btrfs_del_item(trans, extent_root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -0400930 if (ret) {
931 return ret;
932 }
Chris Masondb945352007-10-15 16:15:53 -0400933 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
Chris Mason1e2677e2007-05-29 16:52:18 -0400934 mark_free, 0);
Chris Mason9078a3e2007-04-26 16:46:15 -0400935 BUG_ON(ret);
Chris Masona28ec192007-03-06 20:08:01 -0500936 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400937 btrfs_free_path(path);
Chris Masone089f052007-03-16 16:20:31 -0400938 finish_current_insert(trans, extent_root);
Chris Masona28ec192007-03-06 20:08:01 -0500939 return ret;
940}
941
942/*
Chris Masonfec577f2007-02-26 10:40:21 -0500943 * find all the blocks marked as pending in the radix tree and remove
944 * them from the extent map
945 */
Chris Masone089f052007-03-16 16:20:31 -0400946static int del_pending_extents(struct btrfs_trans_handle *trans, struct
947 btrfs_root *extent_root)
Chris Masonfec577f2007-02-26 10:40:21 -0500948{
949 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -0400950 int err = 0;
Chris Mason1a5bc1672007-10-15 16:15:26 -0400951 u64 start;
952 u64 end;
953 struct extent_map_tree *pending_del;
954 struct extent_map_tree *pinned_extents;
Chris Mason8ef97622007-03-26 10:15:30 -0400955
Chris Mason1a5bc1672007-10-15 16:15:26 -0400956 pending_del = &extent_root->fs_info->pending_del;
957 pinned_extents = &extent_root->fs_info->pinned_extents;
Chris Masonfec577f2007-02-26 10:40:21 -0500958
959 while(1) {
Chris Mason1a5bc1672007-10-15 16:15:26 -0400960 ret = find_first_extent_bit(pending_del, 0, &start, &end,
961 EXTENT_LOCKED);
962 if (ret)
Chris Masonfec577f2007-02-26 10:40:21 -0500963 break;
Yan324ae4d2007-11-16 14:57:08 -0500964 update_pinned_extents(extent_root, start, end + 1 - start, 1);
Chris Mason1a5bc1672007-10-15 16:15:26 -0400965 clear_extent_bits(pending_del, start, end, EXTENT_LOCKED,
966 GFP_NOFS);
967 ret = __free_extent(trans, extent_root,
968 start, end + 1 - start, 0, 0);
969 if (ret)
970 err = ret;
Chris Masonfec577f2007-02-26 10:40:21 -0500971 }
Chris Masone20d96d2007-03-22 12:13:20 -0400972 return err;
Chris Masonfec577f2007-02-26 10:40:21 -0500973}
974
975/*
976 * remove an extent from the root, returns 0 on success
977 */
Chris Masone089f052007-03-16 16:20:31 -0400978int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masondb945352007-10-15 16:15:53 -0400979 *root, u64 bytenr, u64 num_bytes, int pin)
Chris Masonfec577f2007-02-26 10:40:21 -0500980{
Chris Mason9f5fae22007-03-20 14:38:32 -0400981 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Masonfec577f2007-02-26 10:40:21 -0500982 int pending_ret;
983 int ret;
Chris Masona28ec192007-03-06 20:08:01 -0500984
Chris Masondb945352007-10-15 16:15:53 -0400985 WARN_ON(num_bytes < root->sectorsize);
Chris Masona28ec192007-03-06 20:08:01 -0500986 if (root == extent_root) {
Chris Masondb945352007-10-15 16:15:53 -0400987 pin_down_bytes(root, bytenr, num_bytes, 1);
Chris Masona28ec192007-03-06 20:08:01 -0500988 return 0;
989 }
Chris Masondb945352007-10-15 16:15:53 -0400990 ret = __free_extent(trans, root, bytenr, num_bytes, pin, pin == 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400991 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
Chris Masonfec577f2007-02-26 10:40:21 -0500992 return ret ? ret : pending_ret;
993}
994
Chris Mason87ee04e2007-11-30 11:30:34 -0500995static u64 stripe_align(struct btrfs_root *root, u64 val)
996{
997 u64 mask = ((u64)root->stripesize - 1);
998 u64 ret = (val + mask) & ~mask;
999 return ret;
1000}
1001
Chris Masonfec577f2007-02-26 10:40:21 -05001002/*
1003 * walks the btree of allocated extents and find a hole of a given size.
1004 * The key ins is changed to record the hole:
1005 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -04001006 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -05001007 * ins->offset == number of blocks
1008 * Any available blocks before search_start are skipped.
1009 */
Chris Masone089f052007-03-16 16:20:31 -04001010static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masondb945352007-10-15 16:15:53 -04001011 *orig_root, u64 num_bytes, u64 empty_size,
1012 u64 search_start, u64 search_end, u64 hint_byte,
Chris Masonf2654de2007-06-26 12:20:46 -04001013 struct btrfs_key *ins, u64 exclude_start,
1014 u64 exclude_nr, int data)
Chris Masonfec577f2007-02-26 10:40:21 -05001015{
Chris Mason5caf2a02007-04-02 11:20:42 -04001016 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -04001017 struct btrfs_key key;
Chris Masonfec577f2007-02-26 10:40:21 -05001018 u64 hole_size = 0;
Chris Mason87ee04e2007-11-30 11:30:34 -05001019 u64 aligned;
1020 int ret;
Chris Masonfec577f2007-02-26 10:40:21 -05001021 int slot = 0;
Chris Masondb945352007-10-15 16:15:53 -04001022 u64 last_byte = 0;
Chris Masonbe744172007-05-06 10:15:01 -04001023 u64 orig_search_start = search_start;
Chris Masonfec577f2007-02-26 10:40:21 -05001024 int start_found;
Chris Mason5f39d392007-10-15 16:14:19 -04001025 struct extent_buffer *l;
Chris Mason9f5fae22007-03-20 14:38:32 -04001026 struct btrfs_root * root = orig_root->fs_info->extent_root;
Chris Masonf2458e12007-04-25 15:52:25 -04001027 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -04001028 u64 total_needed = num_bytes;
Chris Masone20d96d2007-03-22 12:13:20 -04001029 int level;
Chris Masonbe08c1b2007-05-03 09:06:49 -04001030 struct btrfs_block_group_cache *block_group;
Chris Masonbe744172007-05-06 10:15:01 -04001031 int full_scan = 0;
Chris Masonfbdc7622007-05-30 10:22:12 -04001032 int wrapped = 0;
Chris Masonf84a8b32007-11-06 10:26:29 -05001033 u64 cached_start;
Chris Masonfec577f2007-02-26 10:40:21 -05001034
Chris Masondb945352007-10-15 16:15:53 -04001035 WARN_ON(num_bytes < root->sectorsize);
Chris Masonb1a4d962007-04-04 15:27:52 -04001036 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
1037
Chris Mason5f39d392007-10-15 16:14:19 -04001038 level = btrfs_header_level(root->node);
1039
Chris Mason015a739c2007-11-26 16:15:16 -08001040 if (num_bytes >= 32 * 1024 * 1024 && hint_byte) {
Chris Mason257d0ce2007-11-07 21:08:16 -05001041 data = BTRFS_BLOCK_GROUP_MIXED;
1042 }
1043
Chris Mason3e1ad542007-05-07 20:03:49 -04001044 if (search_end == (u64)-1)
Chris Masondb945352007-10-15 16:15:53 -04001045 search_end = btrfs_super_total_bytes(&info->super_copy);
1046 if (hint_byte) {
1047 block_group = btrfs_lookup_block_group(info, hint_byte);
Chris Mason1a2b2ac2007-12-04 13:18:24 -05001048 if (!block_group)
1049 hint_byte = search_start;
Chris Masonbe744172007-05-06 10:15:01 -04001050 block_group = btrfs_find_block_group(root, block_group,
Chris Masondb945352007-10-15 16:15:53 -04001051 hint_byte, data, 1);
Chris Masonbe744172007-05-06 10:15:01 -04001052 } else {
1053 block_group = btrfs_find_block_group(root,
Chris Mason1a2b2ac2007-12-04 13:18:24 -05001054 trans->block_group,
1055 search_start, data, 1);
Chris Masonbe744172007-05-06 10:15:01 -04001056 }
1057
Chris Mason6702ed42007-08-07 16:15:09 -04001058 total_needed += empty_size;
Chris Masone0115992007-06-19 16:23:05 -04001059 path = btrfs_alloc_path();
Chris Masonbe744172007-05-06 10:15:01 -04001060check_failed:
Yan5e5745d2007-11-16 14:57:09 -05001061 search_start = find_search_start(root, &block_group, search_start,
1062 total_needed, data, full_scan);
Chris Mason87ee04e2007-11-30 11:30:34 -05001063 search_start = stripe_align(root, search_start);
Chris Masonf84a8b32007-11-06 10:26:29 -05001064 cached_start = search_start;
Chris Mason5caf2a02007-04-02 11:20:42 -04001065 btrfs_init_path(path);
Chris Masonfec577f2007-02-26 10:40:21 -05001066 ins->objectid = search_start;
1067 ins->offset = 0;
Chris Masonfec577f2007-02-26 10:40:21 -05001068 start_found = 0;
Chris Mason2cc58cf2007-08-27 16:49:44 -04001069 path->reada = 2;
Chris Masone37c9e62007-05-09 20:13:14 -04001070
Chris Mason5caf2a02007-04-02 11:20:42 -04001071 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
Chris Mason0f70abe2007-02-28 16:46:22 -05001072 if (ret < 0)
1073 goto error;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001074
Chris Masone37c9e62007-05-09 20:13:14 -04001075 if (path->slots[0] > 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001076 path->slots[0]--;
Chris Masone37c9e62007-05-09 20:13:14 -04001077 }
1078
Chris Mason5f39d392007-10-15 16:14:19 -04001079 l = path->nodes[0];
1080 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
1081
Chris Masone37c9e62007-05-09 20:13:14 -04001082 /*
1083 * a rare case, go back one key if we hit a block group item
1084 * instead of an extent item
1085 */
1086 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY &&
1087 key.objectid + key.offset >= search_start) {
1088 ins->objectid = key.objectid;
1089 ins->offset = key.offset - 1;
1090 btrfs_release_path(root, path);
1091 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
1092 if (ret < 0)
1093 goto error;
1094
1095 if (path->slots[0] > 0) {
1096 path->slots[0]--;
1097 }
1098 }
Chris Mason0579da42007-03-07 16:15:30 -05001099
Chris Masonfec577f2007-02-26 10:40:21 -05001100 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001101 l = path->nodes[0];
Chris Mason5caf2a02007-04-02 11:20:42 -04001102 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04001103 if (slot >= btrfs_header_nritems(l)) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001104 ret = btrfs_next_leaf(root, path);
Chris Masonfec577f2007-02-26 10:40:21 -05001105 if (ret == 0)
1106 continue;
Chris Mason0f70abe2007-02-28 16:46:22 -05001107 if (ret < 0)
1108 goto error;
Chris Masone19caa52007-10-15 16:17:44 -04001109
1110 search_start = max(search_start,
1111 block_group->key.objectid);
Chris Masonfec577f2007-02-26 10:40:21 -05001112 if (!start_found) {
Chris Mason87ee04e2007-11-30 11:30:34 -05001113 aligned = stripe_align(root, search_start);
1114 ins->objectid = aligned;
1115 if (aligned >= search_end) {
1116 ret = -ENOSPC;
1117 goto error;
1118 }
1119 ins->offset = search_end - aligned;
Chris Masonfec577f2007-02-26 10:40:21 -05001120 start_found = 1;
1121 goto check_pending;
1122 }
Chris Mason87ee04e2007-11-30 11:30:34 -05001123 ins->objectid = stripe_align(root,
1124 last_byte > search_start ?
1125 last_byte : search_start);
1126 if (search_end <= ins->objectid) {
1127 ret = -ENOSPC;
1128 goto error;
1129 }
Chris Mason3e1ad542007-05-07 20:03:49 -04001130 ins->offset = search_end - ins->objectid;
Chris Masone19caa52007-10-15 16:17:44 -04001131 BUG_ON(ins->objectid >= search_end);
Chris Masonfec577f2007-02-26 10:40:21 -05001132 goto check_pending;
1133 }
Chris Mason5f39d392007-10-15 16:14:19 -04001134 btrfs_item_key_to_cpu(l, &key, slot);
Chris Mason96b51792007-10-15 16:15:19 -04001135
Chris Masondb945352007-10-15 16:15:53 -04001136 if (key.objectid >= search_start && key.objectid > last_byte &&
Chris Masone37c9e62007-05-09 20:13:14 -04001137 start_found) {
Chris Masondb945352007-10-15 16:15:53 -04001138 if (last_byte < search_start)
1139 last_byte = search_start;
Chris Mason87ee04e2007-11-30 11:30:34 -05001140 aligned = stripe_align(root, last_byte);
1141 hole_size = key.objectid - aligned;
1142 if (key.objectid > aligned && hole_size >= num_bytes) {
1143 ins->objectid = aligned;
Chris Masone37c9e62007-05-09 20:13:14 -04001144 ins->offset = hole_size;
1145 goto check_pending;
Chris Mason0579da42007-03-07 16:15:30 -05001146 }
Chris Masonfec577f2007-02-26 10:40:21 -05001147 }
Chris Mason96b51792007-10-15 16:15:19 -04001148 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY) {
1149 if (!start_found) {
Chris Masondb945352007-10-15 16:15:53 -04001150 last_byte = key.objectid;
Chris Mason96b51792007-10-15 16:15:19 -04001151 start_found = 1;
1152 }
Chris Masone37c9e62007-05-09 20:13:14 -04001153 goto next;
Chris Mason96b51792007-10-15 16:15:19 -04001154 }
1155
Chris Masone37c9e62007-05-09 20:13:14 -04001156
Chris Mason0579da42007-03-07 16:15:30 -05001157 start_found = 1;
Chris Masondb945352007-10-15 16:15:53 -04001158 last_byte = key.objectid + key.offset;
Chris Masonf510cfe2007-10-15 16:14:48 -04001159
Chris Mason257d0ce2007-11-07 21:08:16 -05001160 if (!full_scan && data != BTRFS_BLOCK_GROUP_MIXED &&
1161 last_byte >= block_group->key.objectid +
Chris Masonbe744172007-05-06 10:15:01 -04001162 block_group->key.offset) {
1163 btrfs_release_path(root, path);
1164 search_start = block_group->key.objectid +
Chris Masone19caa52007-10-15 16:17:44 -04001165 block_group->key.offset;
Chris Masonbe744172007-05-06 10:15:01 -04001166 goto new_group;
1167 }
Chris Mason9078a3e2007-04-26 16:46:15 -04001168next:
Chris Mason5caf2a02007-04-02 11:20:42 -04001169 path->slots[0]++;
Chris Masonde428b62007-05-18 13:28:27 -04001170 cond_resched();
Chris Masonfec577f2007-02-26 10:40:21 -05001171 }
Chris Masonfec577f2007-02-26 10:40:21 -05001172check_pending:
1173 /* we have to make sure we didn't find an extent that has already
1174 * been allocated by the map tree or the original allocation
1175 */
Chris Mason5caf2a02007-04-02 11:20:42 -04001176 btrfs_release_path(root, path);
Chris Masonfec577f2007-02-26 10:40:21 -05001177 BUG_ON(ins->objectid < search_start);
Chris Masone37c9e62007-05-09 20:13:14 -04001178
Chris Masondb945352007-10-15 16:15:53 -04001179 if (ins->objectid + num_bytes >= search_end)
Chris Masoncf675822007-09-17 11:00:51 -04001180 goto enospc;
Chris Mason257d0ce2007-11-07 21:08:16 -05001181 if (!full_scan && data != BTRFS_BLOCK_GROUP_MIXED &&
Yan5cf66422007-11-16 14:57:09 -05001182 ins->objectid + num_bytes > block_group->
Chris Masone19caa52007-10-15 16:17:44 -04001183 key.objectid + block_group->key.offset) {
1184 search_start = block_group->key.objectid +
1185 block_group->key.offset;
1186 goto new_group;
1187 }
Chris Mason1a5bc1672007-10-15 16:15:26 -04001188 if (test_range_bit(&info->extent_ins, ins->objectid,
Chris Masondb945352007-10-15 16:15:53 -04001189 ins->objectid + num_bytes -1, EXTENT_LOCKED, 0)) {
1190 search_start = ins->objectid + num_bytes;
Chris Mason1a5bc1672007-10-15 16:15:26 -04001191 goto new_group;
1192 }
1193 if (test_range_bit(&info->pinned_extents, ins->objectid,
Chris Masondb945352007-10-15 16:15:53 -04001194 ins->objectid + num_bytes -1, EXTENT_DIRTY, 0)) {
1195 search_start = ins->objectid + num_bytes;
Chris Mason1a5bc1672007-10-15 16:15:26 -04001196 goto new_group;
Chris Masonfec577f2007-02-26 10:40:21 -05001197 }
Chris Masondb945352007-10-15 16:15:53 -04001198 if (exclude_nr > 0 && (ins->objectid + num_bytes > exclude_start &&
Chris Masonf2654de2007-06-26 12:20:46 -04001199 ins->objectid < exclude_start + exclude_nr)) {
1200 search_start = exclude_start + exclude_nr;
1201 goto new_group;
1202 }
Chris Masone37c9e62007-05-09 20:13:14 -04001203 if (!data) {
Chris Mason5276aed2007-06-11 21:33:38 -04001204 block_group = btrfs_lookup_block_group(info, ins->objectid);
Chris Mason26b80032007-08-08 20:17:12 -04001205 if (block_group)
1206 trans->block_group = block_group;
Chris Masoncd1bc462007-04-27 10:08:34 -04001207 }
Chris Masondb945352007-10-15 16:15:53 -04001208 ins->offset = num_bytes;
Chris Mason5caf2a02007-04-02 11:20:42 -04001209 btrfs_free_path(path);
Chris Masonfec577f2007-02-26 10:40:21 -05001210 return 0;
Chris Masonbe744172007-05-06 10:15:01 -04001211
1212new_group:
Chris Masondb945352007-10-15 16:15:53 -04001213 if (search_start + num_bytes >= search_end) {
Chris Masoncf675822007-09-17 11:00:51 -04001214enospc:
Chris Masonbe744172007-05-06 10:15:01 -04001215 search_start = orig_search_start;
Chris Masonfbdc7622007-05-30 10:22:12 -04001216 if (full_scan) {
1217 ret = -ENOSPC;
1218 goto error;
1219 }
Chris Mason6702ed42007-08-07 16:15:09 -04001220 if (wrapped) {
1221 if (!full_scan)
1222 total_needed -= empty_size;
Chris Masonfbdc7622007-05-30 10:22:12 -04001223 full_scan = 1;
Chris Mason1a2b2ac2007-12-04 13:18:24 -05001224 data = BTRFS_BLOCK_GROUP_MIXED;
Chris Mason6702ed42007-08-07 16:15:09 -04001225 } else
Chris Masonfbdc7622007-05-30 10:22:12 -04001226 wrapped = 1;
Chris Masonbe744172007-05-06 10:15:01 -04001227 }
Chris Mason5276aed2007-06-11 21:33:38 -04001228 block_group = btrfs_lookup_block_group(info, search_start);
Chris Masonfbdc7622007-05-30 10:22:12 -04001229 cond_resched();
Chris Mason1a2b2ac2007-12-04 13:18:24 -05001230 block_group = btrfs_find_block_group(root, block_group,
1231 search_start, data, 0);
Chris Masonbe744172007-05-06 10:15:01 -04001232 goto check_failed;
1233
Chris Mason0f70abe2007-02-28 16:46:22 -05001234error:
Chris Mason5caf2a02007-04-02 11:20:42 -04001235 btrfs_release_path(root, path);
1236 btrfs_free_path(path);
Chris Mason0f70abe2007-02-28 16:46:22 -05001237 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05001238}
Chris Masonfec577f2007-02-26 10:40:21 -05001239/*
Chris Masonfec577f2007-02-26 10:40:21 -05001240 * finds a free extent and does all the dirty work required for allocation
1241 * returns the key for the extent through ins, and a tree buffer for
1242 * the first block of the extent through buf.
1243 *
1244 * returns 0 if everything worked, non-zero otherwise.
1245 */
Chris Mason4d775672007-04-20 20:23:12 -04001246int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
1247 struct btrfs_root *root, u64 owner,
Chris Masondb945352007-10-15 16:15:53 -04001248 u64 num_bytes, u64 empty_size, u64 hint_byte,
Chris Masonbe08c1b2007-05-03 09:06:49 -04001249 u64 search_end, struct btrfs_key *ins, int data)
Chris Masonfec577f2007-02-26 10:40:21 -05001250{
1251 int ret;
1252 int pending_ret;
Chris Masondb945352007-10-15 16:15:53 -04001253 u64 super_used, root_used;
Chris Masonfbdc7622007-05-30 10:22:12 -04001254 u64 search_start = 0;
Chris Mason1261ec42007-03-20 20:35:03 -04001255 struct btrfs_fs_info *info = root->fs_info;
1256 struct btrfs_root *extent_root = info->extent_root;
Chris Mason234b63a2007-03-13 10:46:10 -04001257 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -05001258
Chris Mason5f39d392007-10-15 16:14:19 -04001259 btrfs_set_stack_extent_refs(&extent_item, 1);
1260 btrfs_set_stack_extent_owner(&extent_item, owner);
Chris Masonfec577f2007-02-26 10:40:21 -05001261
Chris Masondb945352007-10-15 16:15:53 -04001262 WARN_ON(num_bytes < root->sectorsize);
1263 ret = find_free_extent(trans, root, num_bytes, empty_size,
1264 search_start, search_end, hint_byte, ins,
Chris Mason26b80032007-08-08 20:17:12 -04001265 trans->alloc_exclude_start,
1266 trans->alloc_exclude_nr, data);
Chris Masonccd467d2007-06-28 15:57:36 -04001267 BUG_ON(ret);
Chris Masonf2654de2007-06-26 12:20:46 -04001268 if (ret)
1269 return ret;
1270
Josef Bacik58176a92007-08-29 15:47:34 -04001271 /* block accounting for super block */
Chris Masondb945352007-10-15 16:15:53 -04001272 super_used = btrfs_super_bytes_used(&info->super_copy);
1273 btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes);
Chris Mason26b80032007-08-08 20:17:12 -04001274
Josef Bacik58176a92007-08-29 15:47:34 -04001275 /* block accounting for root item */
Chris Masondb945352007-10-15 16:15:53 -04001276 root_used = btrfs_root_used(&root->root_item);
1277 btrfs_set_root_used(&root->root_item, root_used + num_bytes);
Josef Bacik58176a92007-08-29 15:47:34 -04001278
Chris Masonf510cfe2007-10-15 16:14:48 -04001279 clear_extent_dirty(&root->fs_info->free_space_cache,
1280 ins->objectid, ins->objectid + ins->offset - 1,
1281 GFP_NOFS);
1282
Chris Mason26b80032007-08-08 20:17:12 -04001283 if (root == extent_root) {
Chris Mason1a5bc1672007-10-15 16:15:26 -04001284 set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
1285 ins->objectid + ins->offset - 1,
1286 EXTENT_LOCKED, GFP_NOFS);
Chris Masone19caa52007-10-15 16:17:44 -04001287 WARN_ON(data == 1);
Chris Mason26b80032007-08-08 20:17:12 -04001288 goto update_block;
1289 }
1290
1291 WARN_ON(trans->alloc_exclude_nr);
1292 trans->alloc_exclude_start = ins->objectid;
1293 trans->alloc_exclude_nr = ins->offset;
Chris Masone089f052007-03-16 16:20:31 -04001294 ret = btrfs_insert_item(trans, extent_root, ins, &extent_item,
1295 sizeof(extent_item));
Chris Mason037e6392007-03-07 11:50:24 -05001296
Chris Mason26b80032007-08-08 20:17:12 -04001297 trans->alloc_exclude_start = 0;
1298 trans->alloc_exclude_nr = 0;
1299
Chris Masonccd467d2007-06-28 15:57:36 -04001300 BUG_ON(ret);
Chris Masone089f052007-03-16 16:20:31 -04001301 finish_current_insert(trans, extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -04001302 pending_ret = del_pending_extents(trans, extent_root);
Chris Masonf510cfe2007-10-15 16:14:48 -04001303
Chris Masone37c9e62007-05-09 20:13:14 -04001304 if (ret) {
Chris Mason037e6392007-03-07 11:50:24 -05001305 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -04001306 }
1307 if (pending_ret) {
Chris Mason037e6392007-03-07 11:50:24 -05001308 return pending_ret;
Chris Masone37c9e62007-05-09 20:13:14 -04001309 }
Chris Mason26b80032007-08-08 20:17:12 -04001310
1311update_block:
Chris Mason1e2677e2007-05-29 16:52:18 -04001312 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0,
1313 data);
Chris Masonfabb5682007-06-07 22:13:21 -04001314 BUG_ON(ret);
Chris Mason037e6392007-03-07 11:50:24 -05001315 return 0;
Chris Masonfec577f2007-02-26 10:40:21 -05001316}
1317
1318/*
1319 * helper function to allocate a block for a given tree
1320 * returns the tree buffer or NULL.
1321 */
Chris Mason5f39d392007-10-15 16:14:19 -04001322struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Chris Masondb945352007-10-15 16:15:53 -04001323 struct btrfs_root *root,
1324 u32 blocksize, u64 hint,
Chris Mason5f39d392007-10-15 16:14:19 -04001325 u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05001326{
Chris Masone2fa7222007-03-12 16:22:34 -04001327 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -05001328 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04001329 struct extent_buffer *buf;
Chris Masonfec577f2007-02-26 10:40:21 -05001330
Chris Mason4d775672007-04-20 20:23:12 -04001331 ret = btrfs_alloc_extent(trans, root, root->root_key.objectid,
Chris Masondb945352007-10-15 16:15:53 -04001332 blocksize, empty_size, hint,
1333 (u64)-1, &ins, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05001334 if (ret) {
Chris Mason54aa1f42007-06-22 14:16:25 -04001335 BUG_ON(ret > 0);
1336 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05001337 }
Chris Masondb945352007-10-15 16:15:53 -04001338 buf = btrfs_find_create_tree_block(root, ins.objectid, blocksize);
Chris Mason54aa1f42007-06-22 14:16:25 -04001339 if (!buf) {
Chris Masondb945352007-10-15 16:15:53 -04001340 btrfs_free_extent(trans, root, ins.objectid, blocksize, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001341 return ERR_PTR(-ENOMEM);
1342 }
Chris Mason5f39d392007-10-15 16:14:19 -04001343 btrfs_set_buffer_uptodate(buf);
1344 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
1345 buf->start + buf->len - 1, GFP_NOFS);
Chris Mason19c00dd2007-10-15 16:19:22 -04001346 set_extent_bits(&BTRFS_I(root->fs_info->btree_inode)->extent_tree,
1347 buf->start, buf->start + buf->len - 1,
1348 EXTENT_CSUM, GFP_NOFS);
1349 buf->flags |= EXTENT_CSUM;
Chris Mason6b800532007-10-15 16:17:34 -04001350 btrfs_set_buffer_defrag(buf);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001351 trans->blocks_used++;
Chris Masonfec577f2007-02-26 10:40:21 -05001352 return buf;
1353}
Chris Masona28ec192007-03-06 20:08:01 -05001354
Chris Mason6407bf62007-03-27 06:33:00 -04001355static int drop_leaf_ref(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001356 struct btrfs_root *root, struct extent_buffer *leaf)
Chris Mason6407bf62007-03-27 06:33:00 -04001357{
Chris Mason5f39d392007-10-15 16:14:19 -04001358 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -04001359 struct btrfs_file_extent_item *fi;
1360 int i;
1361 int nritems;
1362 int ret;
1363
Chris Mason5f39d392007-10-15 16:14:19 -04001364 BUG_ON(!btrfs_is_leaf(leaf));
1365 nritems = btrfs_header_nritems(leaf);
Chris Mason6407bf62007-03-27 06:33:00 -04001366 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04001367 u64 disk_bytenr;
Chris Mason5f39d392007-10-15 16:14:19 -04001368
1369 btrfs_item_key_to_cpu(leaf, &key, i);
1370 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason6407bf62007-03-27 06:33:00 -04001371 continue;
1372 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001373 if (btrfs_file_extent_type(leaf, fi) ==
1374 BTRFS_FILE_EXTENT_INLINE)
Chris Mason236454df2007-04-19 13:37:44 -04001375 continue;
Chris Mason6407bf62007-03-27 06:33:00 -04001376 /*
1377 * FIXME make sure to insert a trans record that
1378 * repeats the snapshot del on crash
1379 */
Chris Masondb945352007-10-15 16:15:53 -04001380 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
1381 if (disk_bytenr == 0)
Chris Mason3a686372007-05-24 13:35:57 -04001382 continue;
Chris Masondb945352007-10-15 16:15:53 -04001383 ret = btrfs_free_extent(trans, root, disk_bytenr,
1384 btrfs_file_extent_disk_num_bytes(leaf, fi), 0);
Chris Mason6407bf62007-03-27 06:33:00 -04001385 BUG_ON(ret);
1386 }
1387 return 0;
1388}
1389
Chris Masone0115992007-06-19 16:23:05 -04001390static void reada_walk_down(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -04001391 struct extent_buffer *node)
Chris Masone0115992007-06-19 16:23:05 -04001392{
1393 int i;
1394 u32 nritems;
Chris Masondb945352007-10-15 16:15:53 -04001395 u64 bytenr;
Chris Masone0115992007-06-19 16:23:05 -04001396 int ret;
1397 u32 refs;
Chris Masondb945352007-10-15 16:15:53 -04001398 int level;
1399 u32 blocksize;
Chris Masone0115992007-06-19 16:23:05 -04001400
Chris Mason5f39d392007-10-15 16:14:19 -04001401 nritems = btrfs_header_nritems(node);
Chris Masondb945352007-10-15 16:15:53 -04001402 level = btrfs_header_level(node);
Chris Masone0115992007-06-19 16:23:05 -04001403 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04001404 bytenr = btrfs_node_blockptr(node, i);
1405 blocksize = btrfs_level_size(root, level - 1);
1406 ret = lookup_extent_ref(NULL, root, bytenr, blocksize, &refs);
Chris Masone0115992007-06-19 16:23:05 -04001407 BUG_ON(ret);
1408 if (refs != 1)
1409 continue;
Chris Mason409eb952007-08-08 20:17:12 -04001410 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masondb945352007-10-15 16:15:53 -04001411 ret = readahead_tree_block(root, bytenr, blocksize);
Chris Mason409eb952007-08-08 20:17:12 -04001412 cond_resched();
1413 mutex_lock(&root->fs_info->fs_mutex);
Chris Masone0115992007-06-19 16:23:05 -04001414 if (ret)
1415 break;
1416 }
1417}
1418
Chris Mason9aca1d52007-03-13 11:09:37 -04001419/*
1420 * helper function for drop_snapshot, this walks down the tree dropping ref
1421 * counts as it goes.
1422 */
Chris Masone089f052007-03-16 16:20:31 -04001423static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
1424 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05001425{
Chris Mason5f39d392007-10-15 16:14:19 -04001426 struct extent_buffer *next;
1427 struct extent_buffer *cur;
Chris Masondb945352007-10-15 16:15:53 -04001428 u64 bytenr;
1429 u32 blocksize;
Chris Mason20524f02007-03-10 06:35:47 -05001430 int ret;
1431 u32 refs;
1432
Chris Mason5caf2a02007-04-02 11:20:42 -04001433 WARN_ON(*level < 0);
1434 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason5f39d392007-10-15 16:14:19 -04001435 ret = lookup_extent_ref(trans, root,
Chris Masondb945352007-10-15 16:15:53 -04001436 path->nodes[*level]->start,
1437 path->nodes[*level]->len, &refs);
Chris Mason20524f02007-03-10 06:35:47 -05001438 BUG_ON(ret);
1439 if (refs > 1)
1440 goto out;
Chris Masone0115992007-06-19 16:23:05 -04001441
Chris Mason9aca1d52007-03-13 11:09:37 -04001442 /*
1443 * walk down to the last node level and free all the leaves
1444 */
Chris Mason6407bf62007-03-27 06:33:00 -04001445 while(*level >= 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001446 WARN_ON(*level < 0);
1447 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason20524f02007-03-10 06:35:47 -05001448 cur = path->nodes[*level];
Chris Masone0115992007-06-19 16:23:05 -04001449
1450 if (*level > 0 && path->slots[*level] == 0)
Chris Mason5f39d392007-10-15 16:14:19 -04001451 reada_walk_down(root, cur);
Chris Masone0115992007-06-19 16:23:05 -04001452
Chris Mason5f39d392007-10-15 16:14:19 -04001453 if (btrfs_header_level(cur) != *level)
Chris Mason2c90e5d2007-04-02 10:50:19 -04001454 WARN_ON(1);
Chris Masone0115992007-06-19 16:23:05 -04001455
Chris Mason7518a232007-03-12 12:01:18 -04001456 if (path->slots[*level] >=
Chris Mason5f39d392007-10-15 16:14:19 -04001457 btrfs_header_nritems(cur))
Chris Mason20524f02007-03-10 06:35:47 -05001458 break;
Chris Mason6407bf62007-03-27 06:33:00 -04001459 if (*level == 0) {
1460 ret = drop_leaf_ref(trans, root, cur);
1461 BUG_ON(ret);
1462 break;
1463 }
Chris Masondb945352007-10-15 16:15:53 -04001464 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
1465 blocksize = btrfs_level_size(root, *level - 1);
1466 ret = lookup_extent_ref(trans, root, bytenr, blocksize, &refs);
Chris Mason6407bf62007-03-27 06:33:00 -04001467 BUG_ON(ret);
1468 if (refs != 1) {
Chris Mason20524f02007-03-10 06:35:47 -05001469 path->slots[*level]++;
Chris Masondb945352007-10-15 16:15:53 -04001470 ret = btrfs_free_extent(trans, root, bytenr,
1471 blocksize, 1);
Chris Mason20524f02007-03-10 06:35:47 -05001472 BUG_ON(ret);
1473 continue;
1474 }
Chris Masondb945352007-10-15 16:15:53 -04001475 next = btrfs_find_tree_block(root, bytenr, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -04001476 if (!next || !btrfs_buffer_uptodate(next)) {
1477 free_extent_buffer(next);
Chris Masone9d0b132007-08-10 14:06:19 -04001478 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masondb945352007-10-15 16:15:53 -04001479 next = read_tree_block(root, bytenr, blocksize);
Chris Masone9d0b132007-08-10 14:06:19 -04001480 mutex_lock(&root->fs_info->fs_mutex);
1481
1482 /* we dropped the lock, check one more time */
Chris Masondb945352007-10-15 16:15:53 -04001483 ret = lookup_extent_ref(trans, root, bytenr,
1484 blocksize, &refs);
Chris Masone9d0b132007-08-10 14:06:19 -04001485 BUG_ON(ret);
1486 if (refs != 1) {
1487 path->slots[*level]++;
Chris Mason5f39d392007-10-15 16:14:19 -04001488 free_extent_buffer(next);
Chris Masone9d0b132007-08-10 14:06:19 -04001489 ret = btrfs_free_extent(trans, root,
Chris Masondb945352007-10-15 16:15:53 -04001490 bytenr, blocksize, 1);
Chris Masone9d0b132007-08-10 14:06:19 -04001491 BUG_ON(ret);
1492 continue;
1493 }
1494 }
Chris Mason5caf2a02007-04-02 11:20:42 -04001495 WARN_ON(*level <= 0);
Chris Mason83e15a22007-03-12 09:03:27 -04001496 if (path->nodes[*level-1])
Chris Mason5f39d392007-10-15 16:14:19 -04001497 free_extent_buffer(path->nodes[*level-1]);
Chris Mason20524f02007-03-10 06:35:47 -05001498 path->nodes[*level-1] = next;
Chris Mason5f39d392007-10-15 16:14:19 -04001499 *level = btrfs_header_level(next);
Chris Mason20524f02007-03-10 06:35:47 -05001500 path->slots[*level] = 0;
1501 }
1502out:
Chris Mason5caf2a02007-04-02 11:20:42 -04001503 WARN_ON(*level < 0);
1504 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Masondb945352007-10-15 16:15:53 -04001505 ret = btrfs_free_extent(trans, root, path->nodes[*level]->start,
1506 path->nodes[*level]->len, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001507 free_extent_buffer(path->nodes[*level]);
Chris Mason20524f02007-03-10 06:35:47 -05001508 path->nodes[*level] = NULL;
1509 *level += 1;
1510 BUG_ON(ret);
1511 return 0;
1512}
1513
Chris Mason9aca1d52007-03-13 11:09:37 -04001514/*
1515 * helper for dropping snapshots. This walks back up the tree in the path
1516 * to find the first node higher up where we haven't yet gone through
1517 * all the slots
1518 */
Chris Masone089f052007-03-16 16:20:31 -04001519static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root
1520 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05001521{
1522 int i;
1523 int slot;
1524 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04001525 struct btrfs_root_item *root_item = &root->root_item;
1526
Chris Mason234b63a2007-03-13 10:46:10 -04001527 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Mason20524f02007-03-10 06:35:47 -05001528 slot = path->slots[i];
Chris Mason5f39d392007-10-15 16:14:19 -04001529 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
1530 struct extent_buffer *node;
1531 struct btrfs_disk_key disk_key;
1532 node = path->nodes[i];
Chris Mason20524f02007-03-10 06:35:47 -05001533 path->slots[i]++;
1534 *level = i;
Chris Mason9f3a7422007-08-07 15:52:19 -04001535 WARN_ON(*level == 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001536 btrfs_node_key(node, &disk_key, path->slots[i]);
Chris Mason9f3a7422007-08-07 15:52:19 -04001537 memcpy(&root_item->drop_progress,
Chris Mason5f39d392007-10-15 16:14:19 -04001538 &disk_key, sizeof(disk_key));
Chris Mason9f3a7422007-08-07 15:52:19 -04001539 root_item->drop_level = i;
Chris Mason20524f02007-03-10 06:35:47 -05001540 return 0;
1541 } else {
Chris Masone089f052007-03-16 16:20:31 -04001542 ret = btrfs_free_extent(trans, root,
Chris Masondb945352007-10-15 16:15:53 -04001543 path->nodes[*level]->start,
1544 path->nodes[*level]->len, 1);
Chris Mason6407bf62007-03-27 06:33:00 -04001545 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04001546 free_extent_buffer(path->nodes[*level]);
Chris Mason83e15a22007-03-12 09:03:27 -04001547 path->nodes[*level] = NULL;
Chris Mason20524f02007-03-10 06:35:47 -05001548 *level = i + 1;
Chris Mason20524f02007-03-10 06:35:47 -05001549 }
1550 }
1551 return 1;
1552}
1553
Chris Mason9aca1d52007-03-13 11:09:37 -04001554/*
1555 * drop the reference count on the tree rooted at 'snap'. This traverses
1556 * the tree freeing any blocks that have a ref count of zero after being
1557 * decremented.
1558 */
Chris Masone089f052007-03-16 16:20:31 -04001559int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason9f3a7422007-08-07 15:52:19 -04001560 *root)
Chris Mason20524f02007-03-10 06:35:47 -05001561{
Chris Mason3768f362007-03-13 16:47:54 -04001562 int ret = 0;
Chris Mason9aca1d52007-03-13 11:09:37 -04001563 int wret;
Chris Mason20524f02007-03-10 06:35:47 -05001564 int level;
Chris Mason5caf2a02007-04-02 11:20:42 -04001565 struct btrfs_path *path;
Chris Mason20524f02007-03-10 06:35:47 -05001566 int i;
1567 int orig_level;
Chris Mason9f3a7422007-08-07 15:52:19 -04001568 struct btrfs_root_item *root_item = &root->root_item;
Chris Mason20524f02007-03-10 06:35:47 -05001569
Chris Mason5caf2a02007-04-02 11:20:42 -04001570 path = btrfs_alloc_path();
1571 BUG_ON(!path);
Chris Mason20524f02007-03-10 06:35:47 -05001572
Chris Mason5f39d392007-10-15 16:14:19 -04001573 level = btrfs_header_level(root->node);
Chris Mason20524f02007-03-10 06:35:47 -05001574 orig_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04001575 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
1576 path->nodes[level] = root->node;
Chris Masonf510cfe2007-10-15 16:14:48 -04001577 extent_buffer_get(root->node);
Chris Mason9f3a7422007-08-07 15:52:19 -04001578 path->slots[level] = 0;
1579 } else {
1580 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001581 struct btrfs_disk_key found_key;
1582 struct extent_buffer *node;
Chris Mason6702ed42007-08-07 16:15:09 -04001583
Chris Mason9f3a7422007-08-07 15:52:19 -04001584 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Chris Mason6702ed42007-08-07 16:15:09 -04001585 level = root_item->drop_level;
1586 path->lowest_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04001587 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason6702ed42007-08-07 16:15:09 -04001588 if (wret < 0) {
Chris Mason9f3a7422007-08-07 15:52:19 -04001589 ret = wret;
1590 goto out;
1591 }
Chris Mason5f39d392007-10-15 16:14:19 -04001592 node = path->nodes[level];
1593 btrfs_node_key(node, &found_key, path->slots[level]);
1594 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
1595 sizeof(found_key)));
Chris Mason9f3a7422007-08-07 15:52:19 -04001596 }
Chris Mason20524f02007-03-10 06:35:47 -05001597 while(1) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001598 wret = walk_down_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04001599 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05001600 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04001601 if (wret < 0)
1602 ret = wret;
1603
Chris Mason5caf2a02007-04-02 11:20:42 -04001604 wret = walk_up_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04001605 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05001606 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04001607 if (wret < 0)
1608 ret = wret;
Chris Mason409eb952007-08-08 20:17:12 -04001609 ret = -EAGAIN;
Chris Mason409eb952007-08-08 20:17:12 -04001610 break;
Chris Mason20524f02007-03-10 06:35:47 -05001611 }
Chris Mason83e15a22007-03-12 09:03:27 -04001612 for (i = 0; i <= orig_level; i++) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001613 if (path->nodes[i]) {
Chris Mason5f39d392007-10-15 16:14:19 -04001614 free_extent_buffer(path->nodes[i]);
Chris Mason0f827312007-10-15 16:18:56 -04001615 path->nodes[i] = NULL;
Chris Mason83e15a22007-03-12 09:03:27 -04001616 }
Chris Mason20524f02007-03-10 06:35:47 -05001617 }
Chris Mason9f3a7422007-08-07 15:52:19 -04001618out:
Chris Mason5caf2a02007-04-02 11:20:42 -04001619 btrfs_free_path(path);
Chris Mason9aca1d52007-03-13 11:09:37 -04001620 return ret;
Chris Mason20524f02007-03-10 06:35:47 -05001621}
Chris Mason9078a3e2007-04-26 16:46:15 -04001622
Chris Masonbe744172007-05-06 10:15:01 -04001623int btrfs_free_block_groups(struct btrfs_fs_info *info)
1624{
Chris Masonf510cfe2007-10-15 16:14:48 -04001625 u64 start;
1626 u64 end;
Yanb97f9202007-11-01 11:28:41 -04001627 u64 ptr;
Chris Mason96b51792007-10-15 16:15:19 -04001628 int ret;
Chris Mason96b51792007-10-15 16:15:19 -04001629 while(1) {
1630 ret = find_first_extent_bit(&info->block_group_cache, 0,
1631 &start, &end, (unsigned int)-1);
1632 if (ret)
1633 break;
Yanb97f9202007-11-01 11:28:41 -04001634 ret = get_state_private(&info->block_group_cache, start, &ptr);
1635 if (!ret)
1636 kfree((void *)(unsigned long)ptr);
Chris Mason96b51792007-10-15 16:15:19 -04001637 clear_extent_bits(&info->block_group_cache, start,
1638 end, (unsigned int)-1, GFP_NOFS);
1639 }
Chris Masone37c9e62007-05-09 20:13:14 -04001640 while(1) {
Chris Masonf510cfe2007-10-15 16:14:48 -04001641 ret = find_first_extent_bit(&info->free_space_cache, 0,
1642 &start, &end, EXTENT_DIRTY);
1643 if (ret)
Chris Masone37c9e62007-05-09 20:13:14 -04001644 break;
Chris Masonf510cfe2007-10-15 16:14:48 -04001645 clear_extent_dirty(&info->free_space_cache, start,
1646 end, GFP_NOFS);
Chris Masone37c9e62007-05-09 20:13:14 -04001647 }
Chris Masonbe744172007-05-06 10:15:01 -04001648 return 0;
1649}
1650
Chris Mason9078a3e2007-04-26 16:46:15 -04001651int btrfs_read_block_groups(struct btrfs_root *root)
1652{
1653 struct btrfs_path *path;
1654 int ret;
1655 int err = 0;
Chris Mason96b51792007-10-15 16:15:19 -04001656 int bit;
Chris Mason9078a3e2007-04-26 16:46:15 -04001657 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04001658 struct btrfs_fs_info *info = root->fs_info;
Chris Mason96b51792007-10-15 16:15:19 -04001659 struct extent_map_tree *block_group_cache;
Chris Mason9078a3e2007-04-26 16:46:15 -04001660 struct btrfs_key key;
1661 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -04001662 struct extent_buffer *leaf;
Chris Mason96b51792007-10-15 16:15:19 -04001663
1664 block_group_cache = &info->block_group_cache;
Chris Mason9078a3e2007-04-26 16:46:15 -04001665
Chris Masonbe744172007-05-06 10:15:01 -04001666 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04001667 key.objectid = 0;
Chris Masondb945352007-10-15 16:15:53 -04001668 key.offset = BTRFS_BLOCK_GROUP_SIZE;
Chris Mason9078a3e2007-04-26 16:46:15 -04001669 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
1670
1671 path = btrfs_alloc_path();
1672 if (!path)
1673 return -ENOMEM;
1674
1675 while(1) {
Chris Masonbe744172007-05-06 10:15:01 -04001676 ret = btrfs_search_slot(NULL, info->extent_root,
Chris Mason9078a3e2007-04-26 16:46:15 -04001677 &key, path, 0, 0);
1678 if (ret != 0) {
1679 err = ret;
1680 break;
1681 }
Chris Mason5f39d392007-10-15 16:14:19 -04001682 leaf = path->nodes[0];
1683 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason9078a3e2007-04-26 16:46:15 -04001684 cache = kmalloc(sizeof(*cache), GFP_NOFS);
1685 if (!cache) {
1686 err = -1;
1687 break;
1688 }
Chris Mason3e1ad542007-05-07 20:03:49 -04001689
Chris Mason5f39d392007-10-15 16:14:19 -04001690 read_extent_buffer(leaf, &cache->item,
1691 btrfs_item_ptr_offset(leaf, path->slots[0]),
1692 sizeof(cache->item));
Chris Mason9078a3e2007-04-26 16:46:15 -04001693 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Masone37c9e62007-05-09 20:13:14 -04001694 cache->cached = 0;
Yan324ae4d2007-11-16 14:57:08 -05001695 cache->pinned = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04001696 key.objectid = found_key.objectid + found_key.offset;
1697 btrfs_release_path(root, path);
Chris Mason5f39d392007-10-15 16:14:19 -04001698
Chris Masonf84a8b32007-11-06 10:26:29 -05001699 if (cache->item.flags & BTRFS_BLOCK_GROUP_MIXED) {
1700 bit = BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA;
1701 cache->data = BTRFS_BLOCK_GROUP_MIXED;
1702 } else if (cache->item.flags & BTRFS_BLOCK_GROUP_DATA) {
Chris Mason96b51792007-10-15 16:15:19 -04001703 bit = BLOCK_GROUP_DATA;
Chris Masonf84a8b32007-11-06 10:26:29 -05001704 cache->data = BTRFS_BLOCK_GROUP_DATA;
Chris Mason96b51792007-10-15 16:15:19 -04001705 } else {
1706 bit = BLOCK_GROUP_METADATA;
1707 cache->data = 0;
Chris Mason31f3c992007-04-30 15:25:45 -04001708 }
Chris Mason96b51792007-10-15 16:15:19 -04001709
1710 /* use EXTENT_LOCKED to prevent merging */
1711 set_extent_bits(block_group_cache, found_key.objectid,
1712 found_key.objectid + found_key.offset - 1,
1713 bit | EXTENT_LOCKED, GFP_NOFS);
1714 set_state_private(block_group_cache, found_key.objectid,
Jens Axboeae2f5412007-10-19 09:22:59 -04001715 (unsigned long)cache);
Chris Mason96b51792007-10-15 16:15:19 -04001716
Chris Mason9078a3e2007-04-26 16:46:15 -04001717 if (key.objectid >=
Chris Masondb945352007-10-15 16:15:53 -04001718 btrfs_super_total_bytes(&info->super_copy))
Chris Mason9078a3e2007-04-26 16:46:15 -04001719 break;
1720 }
1721
1722 btrfs_free_path(path);
1723 return 0;
1724}