blob: 7c953b2ecf153bbd0a101b9d6a30767acba75381 [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 Mason7bb86312007-12-11 09:25:06 -050020#include <linux/crc32c.h>
Chris Mason74493f72007-12-11 09:25:06 -050021#include "hash.h"
Chris Masonfec577f2007-02-26 10:40:21 -050022#include "ctree.h"
23#include "disk-io.h"
24#include "print-tree.h"
Chris Masone089f052007-03-16 16:20:31 -040025#include "transaction.h"
Chris Masonfec577f2007-02-26 10:40:21 -050026
Chris Mason96b51792007-10-15 16:15:19 -040027#define BLOCK_GROUP_DATA EXTENT_WRITEBACK
28#define BLOCK_GROUP_METADATA EXTENT_UPTODATE
29#define BLOCK_GROUP_DIRTY EXTENT_DIRTY
30
Chris Masone089f052007-03-16 16:20:31 -040031static int finish_current_insert(struct btrfs_trans_handle *trans, struct
32 btrfs_root *extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -040033static int del_pending_extents(struct btrfs_trans_handle *trans, struct
34 btrfs_root *extent_root);
Chris Masonfec577f2007-02-26 10:40:21 -050035
Chris Masone37c9e62007-05-09 20:13:14 -040036static int cache_block_group(struct btrfs_root *root,
37 struct btrfs_block_group_cache *block_group)
38{
39 struct btrfs_path *path;
40 int ret;
41 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -040042 struct extent_buffer *leaf;
Chris Masonf510cfe2007-10-15 16:14:48 -040043 struct extent_map_tree *free_space_cache;
Chris Masone37c9e62007-05-09 20:13:14 -040044 int slot;
Chris Masone37c9e62007-05-09 20:13:14 -040045 u64 last = 0;
46 u64 hole_size;
Yan7d7d6062007-09-14 16:15:28 -040047 u64 first_free;
Chris Masone37c9e62007-05-09 20:13:14 -040048 int found = 0;
49
Chris Mason00f5c792007-11-30 10:09:33 -050050 if (!block_group)
51 return 0;
52
Chris Masone37c9e62007-05-09 20:13:14 -040053 root = root->fs_info->extent_root;
Chris Masonf510cfe2007-10-15 16:14:48 -040054 free_space_cache = &root->fs_info->free_space_cache;
Chris Masone37c9e62007-05-09 20:13:14 -040055
56 if (block_group->cached)
57 return 0;
Chris Masonf510cfe2007-10-15 16:14:48 -040058
Chris Masone37c9e62007-05-09 20:13:14 -040059 path = btrfs_alloc_path();
60 if (!path)
61 return -ENOMEM;
Yan7d7d6062007-09-14 16:15:28 -040062
Chris Mason2cc58cf2007-08-27 16:49:44 -040063 path->reada = 2;
Yan7d7d6062007-09-14 16:15:28 -040064 first_free = block_group->key.objectid;
Chris Masone37c9e62007-05-09 20:13:14 -040065 key.objectid = block_group->key.objectid;
Chris Masone37c9e62007-05-09 20:13:14 -040066 key.offset = 0;
Yan7d7d6062007-09-14 16:15:28 -040067
Chris Masone37c9e62007-05-09 20:13:14 -040068 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
69 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Yan7d7d6062007-09-14 16:15:28 -040070
Chris Masone37c9e62007-05-09 20:13:14 -040071 if (ret < 0)
72 return ret;
Yan7d7d6062007-09-14 16:15:28 -040073
Chris Masone37c9e62007-05-09 20:13:14 -040074 if (ret && path->slots[0] > 0)
75 path->slots[0]--;
Yan7d7d6062007-09-14 16:15:28 -040076
Chris Masone37c9e62007-05-09 20:13:14 -040077 while(1) {
Chris Mason5f39d392007-10-15 16:14:19 -040078 leaf = path->nodes[0];
Chris Masone37c9e62007-05-09 20:13:14 -040079 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -040080 if (slot >= btrfs_header_nritems(leaf)) {
Chris Masone37c9e62007-05-09 20:13:14 -040081 ret = btrfs_next_leaf(root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -040082 if (ret < 0)
83 goto err;
Chris Masonde428b62007-05-18 13:28:27 -040084 if (ret == 0) {
Chris Masone37c9e62007-05-09 20:13:14 -040085 continue;
Chris Masonde428b62007-05-18 13:28:27 -040086 } else {
Chris Masone37c9e62007-05-09 20:13:14 -040087 break;
88 }
89 }
Yan7d7d6062007-09-14 16:15:28 -040090
Chris Mason5f39d392007-10-15 16:14:19 -040091 btrfs_item_key_to_cpu(leaf, &key, slot);
Yan7d7d6062007-09-14 16:15:28 -040092 if (key.objectid < block_group->key.objectid) {
Chris Mason7bb86312007-12-11 09:25:06 -050093 if (btrfs_key_type(&key) != BTRFS_EXTENT_REF_KEY &&
94 key.objectid + key.offset > first_free)
Yan7d7d6062007-09-14 16:15:28 -040095 first_free = key.objectid + key.offset;
96 goto next;
97 }
98
Chris Masone37c9e62007-05-09 20:13:14 -040099 if (key.objectid >= block_group->key.objectid +
100 block_group->key.offset) {
Yan7d7d6062007-09-14 16:15:28 -0400101 break;
102 }
103
104 if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) {
105 if (!found) {
106 last = first_free;
107 found = 1;
Chris Masone37c9e62007-05-09 20:13:14 -0400108 }
Chris Masonf510cfe2007-10-15 16:14:48 -0400109 if (key.objectid > last) {
110 hole_size = key.objectid - last;
111 set_extent_dirty(free_space_cache, last,
112 last + hole_size - 1,
113 GFP_NOFS);
Chris Masone37c9e62007-05-09 20:13:14 -0400114 }
Yan7d7d6062007-09-14 16:15:28 -0400115 last = key.objectid + key.offset;
Chris Masone37c9e62007-05-09 20:13:14 -0400116 }
Yan7d7d6062007-09-14 16:15:28 -0400117next:
Chris Masone37c9e62007-05-09 20:13:14 -0400118 path->slots[0]++;
119 }
120
Yan7d7d6062007-09-14 16:15:28 -0400121 if (!found)
122 last = first_free;
123 if (block_group->key.objectid +
124 block_group->key.offset > last) {
125 hole_size = block_group->key.objectid +
126 block_group->key.offset - last;
Chris Masonf510cfe2007-10-15 16:14:48 -0400127 set_extent_dirty(free_space_cache, last,
128 last + hole_size - 1, GFP_NOFS);
Yan7d7d6062007-09-14 16:15:28 -0400129 }
Chris Masone37c9e62007-05-09 20:13:14 -0400130 block_group->cached = 1;
Chris Mason54aa1f42007-06-22 14:16:25 -0400131err:
Chris Masone37c9e62007-05-09 20:13:14 -0400132 btrfs_free_path(path);
133 return 0;
134}
135
Chris Mason5276aed2007-06-11 21:33:38 -0400136struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
137 btrfs_fs_info *info,
Chris Masondb945352007-10-15 16:15:53 -0400138 u64 bytenr)
Chris Masonbe744172007-05-06 10:15:01 -0400139{
Chris Mason96b51792007-10-15 16:15:19 -0400140 struct extent_map_tree *block_group_cache;
141 struct btrfs_block_group_cache *block_group = NULL;
142 u64 ptr;
143 u64 start;
144 u64 end;
Chris Masonbe744172007-05-06 10:15:01 -0400145 int ret;
146
Chris Mason96b51792007-10-15 16:15:19 -0400147 block_group_cache = &info->block_group_cache;
148 ret = find_first_extent_bit(block_group_cache,
Chris Masondb945352007-10-15 16:15:53 -0400149 bytenr, &start, &end,
Chris Mason96b51792007-10-15 16:15:19 -0400150 BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA);
Chris Masonbe744172007-05-06 10:15:01 -0400151 if (ret) {
Chris Mason96b51792007-10-15 16:15:19 -0400152 return NULL;
Chris Masonbe744172007-05-06 10:15:01 -0400153 }
Chris Mason96b51792007-10-15 16:15:19 -0400154 ret = get_state_private(block_group_cache, start, &ptr);
155 if (ret)
156 return NULL;
157
Jens Axboeae2f5412007-10-19 09:22:59 -0400158 block_group = (struct btrfs_block_group_cache *)(unsigned long)ptr;
Yan5cf66422007-11-16 14:57:09 -0500159 if (block_group->key.objectid <= bytenr && bytenr <
Chris Mason96b51792007-10-15 16:15:19 -0400160 block_group->key.objectid + block_group->key.offset)
161 return block_group;
Chris Masonbe744172007-05-06 10:15:01 -0400162 return NULL;
163}
Chris Masone37c9e62007-05-09 20:13:14 -0400164static u64 find_search_start(struct btrfs_root *root,
165 struct btrfs_block_group_cache **cache_ret,
Yan5e5745d2007-11-16 14:57:09 -0500166 u64 search_start, int num,
167 int data, int full_scan)
Chris Masone37c9e62007-05-09 20:13:14 -0400168{
Chris Masone37c9e62007-05-09 20:13:14 -0400169 int ret;
170 struct btrfs_block_group_cache *cache = *cache_ret;
Chris Masone19caa52007-10-15 16:17:44 -0400171 u64 last;
Chris Masonf510cfe2007-10-15 16:14:48 -0400172 u64 start = 0;
173 u64 end = 0;
Chris Mason257d0ce2007-11-07 21:08:16 -0500174 u64 cache_miss = 0;
Chris Masonf84a8b32007-11-06 10:26:29 -0500175 int wrapped = 0;
Chris Masone37c9e62007-05-09 20:13:14 -0400176
Chris Mason00f5c792007-11-30 10:09:33 -0500177 if (!cache) {
Chris Mason1a2b2ac2007-12-04 13:18:24 -0500178 goto out;
Chris Mason00f5c792007-11-30 10:09:33 -0500179 }
Chris Masone37c9e62007-05-09 20:13:14 -0400180again:
Chris Mason54aa1f42007-06-22 14:16:25 -0400181 ret = cache_block_group(root, cache);
182 if (ret)
183 goto out;
Chris Masonf84a8b32007-11-06 10:26:29 -0500184
Chris Masone19caa52007-10-15 16:17:44 -0400185 last = max(search_start, cache->key.objectid);
186
Chris Masone37c9e62007-05-09 20:13:14 -0400187 while(1) {
Chris Masonf510cfe2007-10-15 16:14:48 -0400188 ret = find_first_extent_bit(&root->fs_info->free_space_cache,
189 last, &start, &end, EXTENT_DIRTY);
Chris Masone19caa52007-10-15 16:17:44 -0400190 if (ret) {
Chris Mason257d0ce2007-11-07 21:08:16 -0500191 if (!cache_miss)
192 cache_miss = last;
Chris Masone19caa52007-10-15 16:17:44 -0400193 goto new_group;
194 }
Chris Masonf510cfe2007-10-15 16:14:48 -0400195
196 start = max(last, start);
197 last = end + 1;
Chris Mason257d0ce2007-11-07 21:08:16 -0500198 if (last - start < num) {
199 if (last == cache->key.objectid + cache->key.offset)
200 cache_miss = start;
Chris Masonf510cfe2007-10-15 16:14:48 -0400201 continue;
Chris Mason257d0ce2007-11-07 21:08:16 -0500202 }
203 if (data != BTRFS_BLOCK_GROUP_MIXED &&
Yan5cf66422007-11-16 14:57:09 -0500204 start + num > cache->key.objectid + cache->key.offset)
Chris Masone37c9e62007-05-09 20:13:14 -0400205 goto new_group;
Chris Masonf510cfe2007-10-15 16:14:48 -0400206 return start;
Chris Masone37c9e62007-05-09 20:13:14 -0400207 }
208out:
Chris Mason1a2b2ac2007-12-04 13:18:24 -0500209 cache = btrfs_lookup_block_group(root->fs_info, search_start);
210 if (!cache) {
211 printk("Unable to find block group for %Lu\n",
212 search_start);
213 WARN_ON(1);
214 return search_start;
215 }
Chris Mason1a5bc162007-10-15 16:15:26 -0400216 return search_start;
Chris Masone37c9e62007-05-09 20:13:14 -0400217
218new_group:
Chris Masone19caa52007-10-15 16:17:44 -0400219 last = cache->key.objectid + cache->key.offset;
Chris Masonf84a8b32007-11-06 10:26:29 -0500220wrapped:
Chris Masone19caa52007-10-15 16:17:44 -0400221 cache = btrfs_lookup_block_group(root->fs_info, last);
Chris Masone37c9e62007-05-09 20:13:14 -0400222 if (!cache) {
Chris Mason0e4de582007-11-26 10:55:49 -0500223no_cache:
Chris Masonf84a8b32007-11-06 10:26:29 -0500224 if (!wrapped) {
225 wrapped = 1;
226 last = search_start;
227 data = BTRFS_BLOCK_GROUP_MIXED;
228 goto wrapped;
229 }
Chris Mason1a2b2ac2007-12-04 13:18:24 -0500230 goto out;
Chris Masone37c9e62007-05-09 20:13:14 -0400231 }
Chris Mason257d0ce2007-11-07 21:08:16 -0500232 if (cache_miss && !cache->cached) {
233 cache_block_group(root, cache);
234 last = cache_miss;
Chris Mason257d0ce2007-11-07 21:08:16 -0500235 cache = btrfs_lookup_block_group(root->fs_info, last);
236 }
Chris Mason1a2b2ac2007-12-04 13:18:24 -0500237 cache = btrfs_find_block_group(root, cache, last, data, 0);
Chris Mason0e4de582007-11-26 10:55:49 -0500238 if (!cache)
239 goto no_cache;
Chris Masone37c9e62007-05-09 20:13:14 -0400240 *cache_ret = cache;
Chris Mason257d0ce2007-11-07 21:08:16 -0500241 cache_miss = 0;
Chris Masone37c9e62007-05-09 20:13:14 -0400242 goto again;
243}
244
Chris Mason84f54cf2007-06-12 07:43:08 -0400245static u64 div_factor(u64 num, int factor)
246{
Chris Mason257d0ce2007-11-07 21:08:16 -0500247 if (factor == 10)
248 return num;
Chris Mason84f54cf2007-06-12 07:43:08 -0400249 num *= factor;
250 do_div(num, 10);
251 return num;
252}
253
Chris Mason31f3c992007-04-30 15:25:45 -0400254struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
255 struct btrfs_block_group_cache
Chris Masonbe744172007-05-06 10:15:01 -0400256 *hint, u64 search_start,
Chris Masonde428b62007-05-18 13:28:27 -0400257 int data, int owner)
Chris Masoncd1bc462007-04-27 10:08:34 -0400258{
Chris Mason96b51792007-10-15 16:15:19 -0400259 struct btrfs_block_group_cache *cache;
260 struct extent_map_tree *block_group_cache;
Chris Mason31f3c992007-04-30 15:25:45 -0400261 struct btrfs_block_group_cache *found_group = NULL;
Chris Masoncd1bc462007-04-27 10:08:34 -0400262 struct btrfs_fs_info *info = root->fs_info;
263 u64 used;
Chris Mason31f3c992007-04-30 15:25:45 -0400264 u64 last = 0;
265 u64 hint_last;
Chris Mason96b51792007-10-15 16:15:19 -0400266 u64 start;
267 u64 end;
268 u64 free_check;
269 u64 ptr;
270 int bit;
Chris Masoncd1bc462007-04-27 10:08:34 -0400271 int ret;
Chris Mason31f3c992007-04-30 15:25:45 -0400272 int full_search = 0;
Chris Masonde428b62007-05-18 13:28:27 -0400273 int factor = 8;
Chris Mason1e2677e2007-05-29 16:52:18 -0400274 int data_swap = 0;
Chris Masonde428b62007-05-18 13:28:27 -0400275
Chris Mason96b51792007-10-15 16:15:19 -0400276 block_group_cache = &info->block_group_cache;
277
Chris Masonde428b62007-05-18 13:28:27 -0400278 if (!owner)
Chris Masonf84a8b32007-11-06 10:26:29 -0500279 factor = 8;
Chris Masonbe744172007-05-06 10:15:01 -0400280
Chris Mason257d0ce2007-11-07 21:08:16 -0500281 if (data == BTRFS_BLOCK_GROUP_MIXED) {
Chris Masonf84a8b32007-11-06 10:26:29 -0500282 bit = BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA;
Chris Mason257d0ce2007-11-07 21:08:16 -0500283 factor = 10;
284 } else if (data)
Chris Mason96b51792007-10-15 16:15:19 -0400285 bit = BLOCK_GROUP_DATA;
286 else
287 bit = BLOCK_GROUP_METADATA;
Chris Masonbe744172007-05-06 10:15:01 -0400288
289 if (search_start) {
290 struct btrfs_block_group_cache *shint;
Chris Mason5276aed2007-06-11 21:33:38 -0400291 shint = btrfs_lookup_block_group(info, search_start);
Chris Masonf84a8b32007-11-06 10:26:29 -0500292 if (shint && (shint->data == data ||
293 shint->data == BTRFS_BLOCK_GROUP_MIXED)) {
Chris Masonbe744172007-05-06 10:15:01 -0400294 used = btrfs_block_group_used(&shint->item);
Yan324ae4d2007-11-16 14:57:08 -0500295 if (used + shint->pinned <
296 div_factor(shint->key.offset, factor)) {
Chris Masonbe744172007-05-06 10:15:01 -0400297 return shint;
298 }
299 }
300 }
Chris Masonf84a8b32007-11-06 10:26:29 -0500301 if (hint && (hint->data == data ||
302 hint->data == BTRFS_BLOCK_GROUP_MIXED)) {
Chris Mason31f3c992007-04-30 15:25:45 -0400303 used = btrfs_block_group_used(&hint->item);
Yan324ae4d2007-11-16 14:57:08 -0500304 if (used + hint->pinned <
305 div_factor(hint->key.offset, factor)) {
Chris Mason31f3c992007-04-30 15:25:45 -0400306 return hint;
307 }
Chris Masone19caa52007-10-15 16:17:44 -0400308 last = hint->key.objectid + hint->key.offset;
Chris Mason31f3c992007-04-30 15:25:45 -0400309 hint_last = last;
310 } else {
Chris Masone37c9e62007-05-09 20:13:14 -0400311 if (hint)
312 hint_last = max(hint->key.objectid, search_start);
313 else
314 hint_last = search_start;
315
316 last = hint_last;
Chris Mason31f3c992007-04-30 15:25:45 -0400317 }
Chris Mason31f3c992007-04-30 15:25:45 -0400318again:
Chris Masoncd1bc462007-04-27 10:08:34 -0400319 while(1) {
Chris Mason96b51792007-10-15 16:15:19 -0400320 ret = find_first_extent_bit(block_group_cache, last,
321 &start, &end, bit);
322 if (ret)
Chris Masoncd1bc462007-04-27 10:08:34 -0400323 break;
Chris Mason96b51792007-10-15 16:15:19 -0400324
325 ret = get_state_private(block_group_cache, start, &ptr);
326 if (ret)
327 break;
328
Jens Axboeae2f5412007-10-19 09:22:59 -0400329 cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
Chris Mason96b51792007-10-15 16:15:19 -0400330 last = cache->key.objectid + cache->key.offset;
331 used = btrfs_block_group_used(&cache->item);
332
333 if (full_search)
334 free_check = cache->key.offset;
335 else
336 free_check = div_factor(cache->key.offset, factor);
Yan324ae4d2007-11-16 14:57:08 -0500337 if (used + cache->pinned < free_check) {
Chris Mason96b51792007-10-15 16:15:19 -0400338 found_group = cache;
339 goto found;
Chris Masoncd1bc462007-04-27 10:08:34 -0400340 }
Chris Masonde428b62007-05-18 13:28:27 -0400341 cond_resched();
Chris Masoncd1bc462007-04-27 10:08:34 -0400342 }
Chris Mason31f3c992007-04-30 15:25:45 -0400343 if (!full_search) {
Chris Masonbe744172007-05-06 10:15:01 -0400344 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -0400345 full_search = 1;
346 goto again;
347 }
Chris Mason1e2677e2007-05-29 16:52:18 -0400348 if (!data_swap) {
Chris Mason1e2677e2007-05-29 16:52:18 -0400349 data_swap = 1;
Chris Mason96b51792007-10-15 16:15:19 -0400350 bit = BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA;
Chris Mason1e2677e2007-05-29 16:52:18 -0400351 last = search_start;
352 goto again;
353 }
Chris Masonbe744172007-05-06 10:15:01 -0400354found:
Chris Mason31f3c992007-04-30 15:25:45 -0400355 return found_group;
Chris Masoncd1bc462007-04-27 10:08:34 -0400356}
357
Chris Mason7bb86312007-12-11 09:25:06 -0500358static u64 hash_extent_ref(u64 root_objectid, u64 ref_generation,
Chris Mason74493f72007-12-11 09:25:06 -0500359 u64 owner, u64 owner_offset)
360{
361 u32 high_crc = ~(u32)0;
362 u32 low_crc = ~(u32)0;
363 __le64 lenum;
364
365 lenum = cpu_to_le64(root_objectid);
366 high_crc = crc32c(high_crc, &lenum, sizeof(lenum));
Chris Mason7bb86312007-12-11 09:25:06 -0500367 lenum = cpu_to_le64(ref_generation);
368 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
Chris Mason74493f72007-12-11 09:25:06 -0500369
Chris Mason7bb86312007-12-11 09:25:06 -0500370#if 0
Chris Mason74493f72007-12-11 09:25:06 -0500371 lenum = cpu_to_le64(owner);
372 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
Chris Mason74493f72007-12-11 09:25:06 -0500373 lenum = cpu_to_le64(owner_offset);
374 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
Chris Mason7bb86312007-12-11 09:25:06 -0500375#endif
Chris Mason74493f72007-12-11 09:25:06 -0500376 return ((u64)high_crc << 32) | (u64)low_crc;
377}
378
Chris Mason7bb86312007-12-11 09:25:06 -0500379static int match_extent_ref(struct extent_buffer *leaf,
380 struct btrfs_extent_ref *disk_ref,
381 struct btrfs_extent_ref *cpu_ref)
382{
383 int ret;
384 int len;
385
386 if (cpu_ref->objectid)
387 len = sizeof(*cpu_ref);
388 else
389 len = 2 * sizeof(u64);
390 ret = memcmp_extent_buffer(leaf, cpu_ref, (unsigned long)disk_ref,
391 len);
392 return ret == 0;
393}
394
395static int lookup_extent_backref(struct btrfs_trans_handle *trans,
396 struct btrfs_root *root,
397 struct btrfs_path *path, u64 bytenr,
398 u64 root_objectid, u64 ref_generation,
399 u64 owner, u64 owner_offset, int del)
400{
401 u64 hash;
402 struct btrfs_key key;
403 struct btrfs_key found_key;
404 struct btrfs_extent_ref ref;
405 struct extent_buffer *leaf;
406 struct btrfs_extent_ref *disk_ref;
407 int ret;
408 int ret2;
409
410 btrfs_set_stack_ref_root(&ref, root_objectid);
411 btrfs_set_stack_ref_generation(&ref, ref_generation);
412 btrfs_set_stack_ref_objectid(&ref, owner);
413 btrfs_set_stack_ref_offset(&ref, owner_offset);
414
415 hash = hash_extent_ref(root_objectid, ref_generation, owner,
416 owner_offset);
417 key.offset = hash;
418 key.objectid = bytenr;
419 key.type = BTRFS_EXTENT_REF_KEY;
420
421 while (1) {
422 ret = btrfs_search_slot(trans, root, &key, path,
423 del ? -1 : 0, del);
424 if (ret < 0)
425 goto out;
426 leaf = path->nodes[0];
427 if (ret != 0) {
428 u32 nritems = btrfs_header_nritems(leaf);
429 if (path->slots[0] >= nritems) {
430 ret2 = btrfs_next_leaf(root, path);
431 if (ret2)
432 goto out;
433 leaf = path->nodes[0];
434 }
435 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
436 if (found_key.objectid != bytenr ||
437 found_key.type != BTRFS_EXTENT_REF_KEY)
438 goto out;
439 key.offset = found_key.offset;
440 if (del) {
441 btrfs_release_path(root, path);
442 continue;
443 }
444 }
445 disk_ref = btrfs_item_ptr(path->nodes[0],
446 path->slots[0],
447 struct btrfs_extent_ref);
448 if (match_extent_ref(path->nodes[0], disk_ref, &ref)) {
449 ret = 0;
450 goto out;
451 }
452 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
453 key.offset = found_key.offset + 1;
454 btrfs_release_path(root, path);
455 }
456out:
457 return ret;
458}
459
Chris Masond8d5f3e2007-12-11 12:42:00 -0500460/*
461 * Back reference rules. Back refs have three main goals:
462 *
463 * 1) differentiate between all holders of references to an extent so that
464 * when a reference is dropped we can make sure it was a valid reference
465 * before freeing the extent.
466 *
467 * 2) Provide enough information to quickly find the holders of an extent
468 * if we notice a given block is corrupted or bad.
469 *
470 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
471 * maintenance. This is actually the same as #2, but with a slightly
472 * different use case.
473 *
474 * File extents can be referenced by:
475 *
476 * - multiple snapshots, subvolumes, or different generations in one subvol
477 * - different files inside a single subvolume (in theory, not implemented yet)
478 * - different offsets inside a file (bookend extents in file.c)
479 *
480 * The extent ref structure has fields for:
481 *
482 * - Objectid of the subvolume root
483 * - Generation number of the tree holding the reference
484 * - objectid of the file holding the reference
485 * - offset in the file corresponding to the key holding the reference
486 *
487 * When a file extent is allocated the fields are filled in:
488 * (root_key.objectid, trans->transid, inode objectid, offset in file)
489 *
490 * When a leaf is cow'd new references are added for every file extent found
491 * in the leaf. It looks the same as the create case, but trans->transid
492 * will be different when the block is cow'd.
493 *
494 * (root_key.objectid, trans->transid, inode objectid, offset in file)
495 *
496 * When a file extent is removed either during snapshot deletion or file
497 * truncation, the corresponding back reference is found
498 * by searching for:
499 *
500 * (btrfs_header_owner(leaf), btrfs_header_generation(leaf),
501 * inode objectid, offset in file)
502 *
503 * Btree extents can be referenced by:
504 *
505 * - Different subvolumes
506 * - Different generations of the same subvolume
507 *
508 * Storing sufficient information for a full reverse mapping of a btree
509 * block would require storing the lowest key of the block in the backref,
510 * and it would require updating that lowest key either before write out or
511 * every time it changed. Instead, the objectid of the lowest key is stored
512 * along with the level of the tree block. This provides a hint
513 * about where in the btree the block can be found. Searches through the
514 * btree only need to look for a pointer to that block, so they stop one
515 * level higher than the level recorded in the backref.
516 *
517 * Some btrees do not do reference counting on their extents. These
518 * include the extent tree and the tree of tree roots. Backrefs for these
519 * trees always have a generation of zero.
520 *
521 * When a tree block is created, back references are inserted:
522 *
523 * (root->root_key.objectid, trans->transid or zero, lowest_key_objectid, level)
524 *
525 * When a tree block is cow'd in a reference counted root,
526 * new back references are added for all the blocks it points to.
527 * These are of the form (trans->transid will have increased since creation):
528 *
529 * (root->root_key.objectid, trans->transid, lowest_key_objectid, level)
530 *
531 * Because the lowest_key_objectid and the level are just hints
532 * they are not used when backrefs are deleted. When a backref is deleted:
533 *
534 * if backref was for a tree root:
535 * root_objectid = root->root_key.objectid
536 * else
537 * root_objectid = btrfs_header_owner(parent)
538 *
539 * (root_objectid, btrfs_header_generation(parent) or zero, 0, 0)
540 *
541 * Back Reference Key hashing:
542 *
543 * Back references have four fields, each 64 bits long. Unfortunately,
544 * This is hashed into a single 64 bit number and placed into the key offset.
545 * The key objectid corresponds to the first byte in the extent, and the
546 * key type is set to BTRFS_EXTENT_REF_KEY
547 */
Chris Mason7bb86312007-12-11 09:25:06 -0500548int btrfs_insert_extent_backref(struct btrfs_trans_handle *trans,
549 struct btrfs_root *root,
550 struct btrfs_path *path, u64 bytenr,
551 u64 root_objectid, u64 ref_generation,
552 u64 owner, u64 owner_offset)
Chris Mason74493f72007-12-11 09:25:06 -0500553{
554 u64 hash;
555 struct btrfs_key key;
556 struct btrfs_extent_ref ref;
Chris Mason7bb86312007-12-11 09:25:06 -0500557 struct btrfs_extent_ref *disk_ref;
Chris Mason74493f72007-12-11 09:25:06 -0500558 int ret;
559
560 btrfs_set_stack_ref_root(&ref, root_objectid);
Chris Mason7bb86312007-12-11 09:25:06 -0500561 btrfs_set_stack_ref_generation(&ref, ref_generation);
Chris Mason74493f72007-12-11 09:25:06 -0500562 btrfs_set_stack_ref_objectid(&ref, owner);
563 btrfs_set_stack_ref_offset(&ref, owner_offset);
564
Chris Mason7bb86312007-12-11 09:25:06 -0500565 hash = hash_extent_ref(root_objectid, ref_generation, owner,
566 owner_offset);
Chris Mason74493f72007-12-11 09:25:06 -0500567 key.offset = hash;
568 key.objectid = bytenr;
569 key.type = BTRFS_EXTENT_REF_KEY;
570
571 ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(ref));
572 while (ret == -EEXIST) {
Chris Mason7bb86312007-12-11 09:25:06 -0500573 disk_ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
574 struct btrfs_extent_ref);
575 if (match_extent_ref(path->nodes[0], disk_ref, &ref))
576 goto out;
577 key.offset++;
578 btrfs_release_path(root, path);
579 ret = btrfs_insert_empty_item(trans, root, path, &key,
580 sizeof(ref));
Chris Mason74493f72007-12-11 09:25:06 -0500581 }
Chris Mason7bb86312007-12-11 09:25:06 -0500582 if (ret)
583 goto out;
584 disk_ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
585 struct btrfs_extent_ref);
586 write_extent_buffer(path->nodes[0], &ref, (unsigned long)disk_ref,
587 sizeof(ref));
588 btrfs_mark_buffer_dirty(path->nodes[0]);
589out:
590 btrfs_release_path(root, path);
591 return ret;
Chris Mason74493f72007-12-11 09:25:06 -0500592}
593
Chris Masonb18c6682007-04-17 13:26:50 -0400594int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
595 struct btrfs_root *root,
Chris Mason74493f72007-12-11 09:25:06 -0500596 u64 bytenr, u64 num_bytes,
Chris Mason7bb86312007-12-11 09:25:06 -0500597 u64 root_objectid, u64 ref_generation,
Chris Mason74493f72007-12-11 09:25:06 -0500598 u64 owner, u64 owner_offset)
Chris Mason02217ed2007-03-02 16:08:05 -0500599{
Chris Mason5caf2a02007-04-02 11:20:42 -0400600 struct btrfs_path *path;
Chris Mason02217ed2007-03-02 16:08:05 -0500601 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400602 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400603 struct extent_buffer *l;
Chris Mason234b63a2007-03-13 10:46:10 -0400604 struct btrfs_extent_item *item;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400605 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500606
Chris Masondb945352007-10-15 16:15:53 -0400607 WARN_ON(num_bytes < root->sectorsize);
Chris Mason5caf2a02007-04-02 11:20:42 -0400608 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -0400609 if (!path)
610 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -0400611
Chris Masondb945352007-10-15 16:15:53 -0400612 key.objectid = bytenr;
Chris Mason62e27492007-03-15 12:56:47 -0400613 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masondb945352007-10-15 16:15:53 -0400614 key.offset = num_bytes;
Chris Mason5caf2a02007-04-02 11:20:42 -0400615 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -0400616 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400617 if (ret < 0)
618 return ret;
Chris Masona429e512007-04-18 16:15:28 -0400619 if (ret != 0) {
Chris Masona28ec192007-03-06 20:08:01 -0500620 BUG();
Chris Masona429e512007-04-18 16:15:28 -0400621 }
Chris Mason02217ed2007-03-02 16:08:05 -0500622 BUG_ON(ret != 0);
Chris Mason5f39d392007-10-15 16:14:19 -0400623 l = path->nodes[0];
Chris Mason5caf2a02007-04-02 11:20:42 -0400624 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400625 refs = btrfs_extent_refs(l, item);
626 btrfs_set_extent_refs(l, item, refs + 1);
Chris Mason5caf2a02007-04-02 11:20:42 -0400627 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masona28ec192007-03-06 20:08:01 -0500628
Chris Mason5caf2a02007-04-02 11:20:42 -0400629 btrfs_release_path(root->fs_info->extent_root, path);
Chris Mason7bb86312007-12-11 09:25:06 -0500630
631 ret = btrfs_insert_extent_backref(trans, root->fs_info->extent_root,
632 path, bytenr, root_objectid,
633 ref_generation, owner, owner_offset);
634 BUG_ON(ret);
Chris Mason9f5fae22007-03-20 14:38:32 -0400635 finish_current_insert(trans, root->fs_info->extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -0400636 del_pending_extents(trans, root->fs_info->extent_root);
Chris Mason74493f72007-12-11 09:25:06 -0500637
638 btrfs_free_path(path);
Chris Mason02217ed2007-03-02 16:08:05 -0500639 return 0;
640}
641
Chris Masone9d0b132007-08-10 14:06:19 -0400642int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
643 struct btrfs_root *root)
644{
645 finish_current_insert(trans, root->fs_info->extent_root);
646 del_pending_extents(trans, root->fs_info->extent_root);
647 return 0;
648}
649
Chris Masonb18c6682007-04-17 13:26:50 -0400650static int lookup_extent_ref(struct btrfs_trans_handle *trans,
Chris Masondb945352007-10-15 16:15:53 -0400651 struct btrfs_root *root, u64 bytenr,
652 u64 num_bytes, u32 *refs)
Chris Masona28ec192007-03-06 20:08:01 -0500653{
Chris Mason5caf2a02007-04-02 11:20:42 -0400654 struct btrfs_path *path;
Chris Masona28ec192007-03-06 20:08:01 -0500655 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400656 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400657 struct extent_buffer *l;
Chris Mason234b63a2007-03-13 10:46:10 -0400658 struct btrfs_extent_item *item;
Chris Mason5caf2a02007-04-02 11:20:42 -0400659
Chris Masondb945352007-10-15 16:15:53 -0400660 WARN_ON(num_bytes < root->sectorsize);
Chris Mason5caf2a02007-04-02 11:20:42 -0400661 path = btrfs_alloc_path();
Chris Masondb945352007-10-15 16:15:53 -0400662 key.objectid = bytenr;
663 key.offset = num_bytes;
Chris Mason62e27492007-03-15 12:56:47 -0400664 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Mason5caf2a02007-04-02 11:20:42 -0400665 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -0400666 0, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400667 if (ret < 0)
668 goto out;
Chris Mason5f39d392007-10-15 16:14:19 -0400669 if (ret != 0) {
670 btrfs_print_leaf(root, path->nodes[0]);
Chris Masondb945352007-10-15 16:15:53 -0400671 printk("failed to find block number %Lu\n", bytenr);
Chris Masona28ec192007-03-06 20:08:01 -0500672 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -0400673 }
674 l = path->nodes[0];
Chris Mason5caf2a02007-04-02 11:20:42 -0400675 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400676 *refs = btrfs_extent_refs(l, item);
Chris Mason54aa1f42007-06-22 14:16:25 -0400677out:
Chris Mason5caf2a02007-04-02 11:20:42 -0400678 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -0500679 return 0;
680}
681
Chris Masonc5739bb2007-04-10 09:27:04 -0400682int btrfs_inc_root_ref(struct btrfs_trans_handle *trans,
Chris Mason7bb86312007-12-11 09:25:06 -0500683 struct btrfs_root *root, u64 owner_objectid)
Chris Masonc5739bb2007-04-10 09:27:04 -0400684{
Chris Mason7bb86312007-12-11 09:25:06 -0500685 u64 generation;
686 u64 key_objectid;
687 u64 level;
688 u32 nritems;
689 struct btrfs_disk_key disk_key;
690
691 level = btrfs_header_level(root->node);
692 generation = trans->transid;
693 nritems = btrfs_header_nritems(root->node);
694 if (nritems > 0) {
695 if (level == 0)
696 btrfs_item_key(root->node, &disk_key, 0);
697 else
698 btrfs_node_key(root->node, &disk_key, 0);
699 key_objectid = btrfs_disk_key_objectid(&disk_key);
700 } else {
701 key_objectid = 0;
702 }
Chris Masondb945352007-10-15 16:15:53 -0400703 return btrfs_inc_extent_ref(trans, root, root->node->start,
Chris Mason7bb86312007-12-11 09:25:06 -0500704 root->node->len, owner_objectid,
705 generation, 0, 0);
Chris Masonc5739bb2007-04-10 09:27:04 -0400706}
707
Chris Masone089f052007-03-16 16:20:31 -0400708int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400709 struct extent_buffer *buf)
Chris Mason02217ed2007-03-02 16:08:05 -0500710{
Chris Masondb945352007-10-15 16:15:53 -0400711 u64 bytenr;
Chris Mason5f39d392007-10-15 16:14:19 -0400712 u32 nritems;
713 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -0400714 struct btrfs_file_extent_item *fi;
Chris Mason02217ed2007-03-02 16:08:05 -0500715 int i;
Chris Masondb945352007-10-15 16:15:53 -0400716 int level;
Chris Mason6407bf62007-03-27 06:33:00 -0400717 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -0400718 int faili;
Chris Masona28ec192007-03-06 20:08:01 -0500719
Chris Mason3768f362007-03-13 16:47:54 -0400720 if (!root->ref_cows)
Chris Masona28ec192007-03-06 20:08:01 -0500721 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400722
Chris Masondb945352007-10-15 16:15:53 -0400723 level = btrfs_header_level(buf);
Chris Mason5f39d392007-10-15 16:14:19 -0400724 nritems = btrfs_header_nritems(buf);
725 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -0400726 if (level == 0) {
727 u64 disk_bytenr;
Chris Mason5f39d392007-10-15 16:14:19 -0400728 btrfs_item_key_to_cpu(buf, &key, i);
729 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason6407bf62007-03-27 06:33:00 -0400730 continue;
Chris Mason5f39d392007-10-15 16:14:19 -0400731 fi = btrfs_item_ptr(buf, i,
Chris Mason6407bf62007-03-27 06:33:00 -0400732 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400733 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason236454df2007-04-19 13:37:44 -0400734 BTRFS_FILE_EXTENT_INLINE)
735 continue;
Chris Masondb945352007-10-15 16:15:53 -0400736 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
737 if (disk_bytenr == 0)
Chris Mason3a686372007-05-24 13:35:57 -0400738 continue;
Chris Masondb945352007-10-15 16:15:53 -0400739 ret = btrfs_inc_extent_ref(trans, root, disk_bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -0500740 btrfs_file_extent_disk_num_bytes(buf, fi),
741 root->root_key.objectid, trans->transid,
742 key.objectid, key.offset);
Chris Mason54aa1f42007-06-22 14:16:25 -0400743 if (ret) {
744 faili = i;
745 goto fail;
746 }
Chris Mason6407bf62007-03-27 06:33:00 -0400747 } else {
Chris Masondb945352007-10-15 16:15:53 -0400748 bytenr = btrfs_node_blockptr(buf, i);
749 ret = btrfs_inc_extent_ref(trans, root, bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -0500750 btrfs_level_size(root, level - 1),
751 root->root_key.objectid,
752 trans->transid, 0, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400753 if (ret) {
754 faili = i;
755 goto fail;
756 }
Chris Mason6407bf62007-03-27 06:33:00 -0400757 }
Chris Mason02217ed2007-03-02 16:08:05 -0500758 }
759 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -0400760fail:
Chris Masonccd467d2007-06-28 15:57:36 -0400761 WARN_ON(1);
Chris Mason7bb86312007-12-11 09:25:06 -0500762#if 0
Chris Mason54aa1f42007-06-22 14:16:25 -0400763 for (i =0; i < faili; i++) {
Chris Masondb945352007-10-15 16:15:53 -0400764 if (level == 0) {
765 u64 disk_bytenr;
Chris Mason5f39d392007-10-15 16:14:19 -0400766 btrfs_item_key_to_cpu(buf, &key, i);
767 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -0400768 continue;
Chris Mason5f39d392007-10-15 16:14:19 -0400769 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -0400770 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400771 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -0400772 BTRFS_FILE_EXTENT_INLINE)
773 continue;
Chris Masondb945352007-10-15 16:15:53 -0400774 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
775 if (disk_bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -0400776 continue;
Chris Masondb945352007-10-15 16:15:53 -0400777 err = btrfs_free_extent(trans, root, disk_bytenr,
778 btrfs_file_extent_disk_num_bytes(buf,
Chris Mason5f39d392007-10-15 16:14:19 -0400779 fi), 0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400780 BUG_ON(err);
781 } else {
Chris Masondb945352007-10-15 16:15:53 -0400782 bytenr = btrfs_node_blockptr(buf, i);
783 err = btrfs_free_extent(trans, root, bytenr,
784 btrfs_level_size(root, level - 1), 0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400785 BUG_ON(err);
786 }
787 }
Chris Mason7bb86312007-12-11 09:25:06 -0500788#endif
Chris Mason54aa1f42007-06-22 14:16:25 -0400789 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -0500790}
791
Chris Mason9078a3e2007-04-26 16:46:15 -0400792static int write_one_cache_group(struct btrfs_trans_handle *trans,
793 struct btrfs_root *root,
794 struct btrfs_path *path,
795 struct btrfs_block_group_cache *cache)
796{
797 int ret;
798 int pending_ret;
799 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -0400800 unsigned long bi;
801 struct extent_buffer *leaf;
Chris Mason9078a3e2007-04-26 16:46:15 -0400802
Chris Mason9078a3e2007-04-26 16:46:15 -0400803 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400804 if (ret < 0)
805 goto fail;
Chris Mason9078a3e2007-04-26 16:46:15 -0400806 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -0400807
808 leaf = path->nodes[0];
809 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
810 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
811 btrfs_mark_buffer_dirty(leaf);
Chris Mason9078a3e2007-04-26 16:46:15 -0400812 btrfs_release_path(extent_root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -0400813fail:
Chris Mason9078a3e2007-04-26 16:46:15 -0400814 finish_current_insert(trans, extent_root);
815 pending_ret = del_pending_extents(trans, extent_root);
816 if (ret)
817 return ret;
818 if (pending_ret)
819 return pending_ret;
820 return 0;
821
822}
823
Chris Mason96b51792007-10-15 16:15:19 -0400824int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
825 struct btrfs_root *root)
Chris Mason9078a3e2007-04-26 16:46:15 -0400826{
Chris Mason96b51792007-10-15 16:15:19 -0400827 struct extent_map_tree *block_group_cache;
828 struct btrfs_block_group_cache *cache;
Chris Mason9078a3e2007-04-26 16:46:15 -0400829 int ret;
830 int err = 0;
831 int werr = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -0400832 struct btrfs_path *path;
Chris Mason96b51792007-10-15 16:15:19 -0400833 u64 last = 0;
834 u64 start;
835 u64 end;
836 u64 ptr;
Chris Mason9078a3e2007-04-26 16:46:15 -0400837
Chris Mason96b51792007-10-15 16:15:19 -0400838 block_group_cache = &root->fs_info->block_group_cache;
Chris Mason9078a3e2007-04-26 16:46:15 -0400839 path = btrfs_alloc_path();
840 if (!path)
841 return -ENOMEM;
842
843 while(1) {
Chris Mason96b51792007-10-15 16:15:19 -0400844 ret = find_first_extent_bit(block_group_cache, last,
845 &start, &end, BLOCK_GROUP_DIRTY);
846 if (ret)
Chris Mason9078a3e2007-04-26 16:46:15 -0400847 break;
Chris Mason54aa1f42007-06-22 14:16:25 -0400848
Chris Mason96b51792007-10-15 16:15:19 -0400849 last = end + 1;
850 ret = get_state_private(block_group_cache, start, &ptr);
851 if (ret)
852 break;
853
Jens Axboeae2f5412007-10-19 09:22:59 -0400854 cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
Chris Mason96b51792007-10-15 16:15:19 -0400855 err = write_one_cache_group(trans, root,
856 path, cache);
857 /*
858 * if we fail to write the cache group, we want
859 * to keep it marked dirty in hopes that a later
860 * write will work
861 */
862 if (err) {
863 werr = err;
864 continue;
Chris Mason9078a3e2007-04-26 16:46:15 -0400865 }
Chris Mason96b51792007-10-15 16:15:19 -0400866 clear_extent_bits(block_group_cache, start, end,
867 BLOCK_GROUP_DIRTY, GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -0400868 }
869 btrfs_free_path(path);
870 return werr;
871}
872
873static int update_block_group(struct btrfs_trans_handle *trans,
874 struct btrfs_root *root,
Chris Masondb945352007-10-15 16:15:53 -0400875 u64 bytenr, u64 num_bytes, int alloc,
876 int mark_free, int data)
Chris Mason9078a3e2007-04-26 16:46:15 -0400877{
878 struct btrfs_block_group_cache *cache;
879 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -0400880 u64 total = num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -0400881 u64 old_val;
Chris Masondb945352007-10-15 16:15:53 -0400882 u64 byte_in_group;
Chris Mason96b51792007-10-15 16:15:19 -0400883 u64 start;
884 u64 end;
Chris Mason3e1ad542007-05-07 20:03:49 -0400885
Chris Mason9078a3e2007-04-26 16:46:15 -0400886 while(total) {
Chris Masondb945352007-10-15 16:15:53 -0400887 cache = btrfs_lookup_block_group(info, bytenr);
Chris Mason3e1ad542007-05-07 20:03:49 -0400888 if (!cache) {
Chris Mason9078a3e2007-04-26 16:46:15 -0400889 return -1;
Chris Masoncd1bc462007-04-27 10:08:34 -0400890 }
Chris Masondb945352007-10-15 16:15:53 -0400891 byte_in_group = bytenr - cache->key.objectid;
892 WARN_ON(byte_in_group > cache->key.offset);
Chris Mason96b51792007-10-15 16:15:19 -0400893 start = cache->key.objectid;
894 end = start + cache->key.offset - 1;
895 set_extent_bits(&info->block_group_cache, start, end,
896 BLOCK_GROUP_DIRTY, GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -0400897
898 old_val = btrfs_block_group_used(&cache->item);
Chris Masondb945352007-10-15 16:15:53 -0400899 num_bytes = min(total, cache->key.offset - byte_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -0400900 if (alloc) {
Chris Mason1e2677e2007-05-29 16:52:18 -0400901 if (cache->data != data &&
Chris Mason84f54cf2007-06-12 07:43:08 -0400902 old_val < (cache->key.offset >> 1)) {
Chris Mason96b51792007-10-15 16:15:19 -0400903 int bit_to_clear;
904 int bit_to_set;
Chris Mason96b51792007-10-15 16:15:19 -0400905 cache->data = data;
Chris Mason1e2677e2007-05-29 16:52:18 -0400906 if (data) {
Yanb97f9202007-11-01 11:28:41 -0400907 bit_to_clear = BLOCK_GROUP_METADATA;
908 bit_to_set = BLOCK_GROUP_DATA;
Chris Masonf84a8b32007-11-06 10:26:29 -0500909 cache->item.flags &=
910 ~BTRFS_BLOCK_GROUP_MIXED;
Chris Mason1e2677e2007-05-29 16:52:18 -0400911 cache->item.flags |=
912 BTRFS_BLOCK_GROUP_DATA;
913 } else {
Yanb97f9202007-11-01 11:28:41 -0400914 bit_to_clear = BLOCK_GROUP_DATA;
915 bit_to_set = BLOCK_GROUP_METADATA;
Chris Mason1e2677e2007-05-29 16:52:18 -0400916 cache->item.flags &=
Chris Masonf84a8b32007-11-06 10:26:29 -0500917 ~BTRFS_BLOCK_GROUP_MIXED;
918 cache->item.flags &=
Chris Mason1e2677e2007-05-29 16:52:18 -0400919 ~BTRFS_BLOCK_GROUP_DATA;
920 }
Chris Mason96b51792007-10-15 16:15:19 -0400921 clear_extent_bits(&info->block_group_cache,
922 start, end, bit_to_clear,
923 GFP_NOFS);
924 set_extent_bits(&info->block_group_cache,
925 start, end, bit_to_set,
926 GFP_NOFS);
Chris Masonf84a8b32007-11-06 10:26:29 -0500927 } else if (cache->data != data &&
928 cache->data != BTRFS_BLOCK_GROUP_MIXED) {
929 cache->data = BTRFS_BLOCK_GROUP_MIXED;
930 set_extent_bits(&info->block_group_cache,
931 start, end,
932 BLOCK_GROUP_DATA |
933 BLOCK_GROUP_METADATA,
934 GFP_NOFS);
Chris Mason1e2677e2007-05-29 16:52:18 -0400935 }
Chris Masondb945352007-10-15 16:15:53 -0400936 old_val += num_bytes;
Chris Masoncd1bc462007-04-27 10:08:34 -0400937 } else {
Chris Masondb945352007-10-15 16:15:53 -0400938 old_val -= num_bytes;
Chris Masonf510cfe2007-10-15 16:14:48 -0400939 if (mark_free) {
940 set_extent_dirty(&info->free_space_cache,
Chris Masondb945352007-10-15 16:15:53 -0400941 bytenr, bytenr + num_bytes - 1,
Chris Masonf510cfe2007-10-15 16:14:48 -0400942 GFP_NOFS);
Chris Masone37c9e62007-05-09 20:13:14 -0400943 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400944 }
Chris Mason9078a3e2007-04-26 16:46:15 -0400945 btrfs_set_block_group_used(&cache->item, old_val);
Chris Masondb945352007-10-15 16:15:53 -0400946 total -= num_bytes;
947 bytenr += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -0400948 }
949 return 0;
950}
Yan324ae4d2007-11-16 14:57:08 -0500951static int update_pinned_extents(struct btrfs_root *root,
952 u64 bytenr, u64 num, int pin)
953{
954 u64 len;
955 struct btrfs_block_group_cache *cache;
956 struct btrfs_fs_info *fs_info = root->fs_info;
957
958 if (pin) {
959 set_extent_dirty(&fs_info->pinned_extents,
960 bytenr, bytenr + num - 1, GFP_NOFS);
961 } else {
962 clear_extent_dirty(&fs_info->pinned_extents,
963 bytenr, bytenr + num - 1, GFP_NOFS);
964 }
965 while (num > 0) {
966 cache = btrfs_lookup_block_group(fs_info, bytenr);
967 WARN_ON(!cache);
968 len = min(num, cache->key.offset -
969 (bytenr - cache->key.objectid));
970 if (pin) {
971 cache->pinned += len;
972 fs_info->total_pinned += len;
973 } else {
974 cache->pinned -= len;
975 fs_info->total_pinned -= len;
976 }
977 bytenr += len;
978 num -= len;
979 }
980 return 0;
981}
Chris Mason9078a3e2007-04-26 16:46:15 -0400982
Chris Mason1a5bc162007-10-15 16:15:26 -0400983int btrfs_copy_pinned(struct btrfs_root *root, struct extent_map_tree *copy)
Chris Masonccd467d2007-06-28 15:57:36 -0400984{
Chris Masonccd467d2007-06-28 15:57:36 -0400985 u64 last = 0;
Chris Mason1a5bc162007-10-15 16:15:26 -0400986 u64 start;
987 u64 end;
988 struct extent_map_tree *pinned_extents = &root->fs_info->pinned_extents;
Chris Masonccd467d2007-06-28 15:57:36 -0400989 int ret;
Chris Masonccd467d2007-06-28 15:57:36 -0400990
991 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -0400992 ret = find_first_extent_bit(pinned_extents, last,
993 &start, &end, EXTENT_DIRTY);
994 if (ret)
Chris Masonccd467d2007-06-28 15:57:36 -0400995 break;
Chris Mason1a5bc162007-10-15 16:15:26 -0400996 set_extent_dirty(copy, start, end, GFP_NOFS);
997 last = end + 1;
Chris Masonccd467d2007-06-28 15:57:36 -0400998 }
999 return 0;
1000}
1001
1002int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
1003 struct btrfs_root *root,
Chris Mason1a5bc162007-10-15 16:15:26 -04001004 struct extent_map_tree *unpin)
Chris Masona28ec192007-03-06 20:08:01 -05001005{
Chris Mason1a5bc162007-10-15 16:15:26 -04001006 u64 start;
1007 u64 end;
Chris Masona28ec192007-03-06 20:08:01 -05001008 int ret;
Chris Masonf510cfe2007-10-15 16:14:48 -04001009 struct extent_map_tree *free_space_cache;
Chris Masonf510cfe2007-10-15 16:14:48 -04001010 free_space_cache = &root->fs_info->free_space_cache;
Chris Masona28ec192007-03-06 20:08:01 -05001011
1012 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04001013 ret = find_first_extent_bit(unpin, 0, &start, &end,
1014 EXTENT_DIRTY);
1015 if (ret)
Chris Masona28ec192007-03-06 20:08:01 -05001016 break;
Yan324ae4d2007-11-16 14:57:08 -05001017 update_pinned_extents(root, start, end + 1 - start, 0);
Chris Mason1a5bc162007-10-15 16:15:26 -04001018 clear_extent_dirty(unpin, start, end, GFP_NOFS);
1019 set_extent_dirty(free_space_cache, start, end, GFP_NOFS);
Chris Masona28ec192007-03-06 20:08:01 -05001020 }
1021 return 0;
1022}
1023
Chris Masone089f052007-03-16 16:20:31 -04001024static int finish_current_insert(struct btrfs_trans_handle *trans, struct
1025 btrfs_root *extent_root)
Chris Mason037e6392007-03-07 11:50:24 -05001026{
Chris Mason7bb86312007-12-11 09:25:06 -05001027 u64 start;
1028 u64 end;
1029 struct btrfs_fs_info *info = extent_root->fs_info;
Chris Masond8d5f3e2007-12-11 12:42:00 -05001030 struct extent_buffer *eb;
Chris Mason7bb86312007-12-11 09:25:06 -05001031 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -04001032 struct btrfs_key ins;
Chris Masond8d5f3e2007-12-11 12:42:00 -05001033 struct btrfs_disk_key first;
Chris Mason234b63a2007-03-13 10:46:10 -04001034 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -05001035 int ret;
Chris Masond8d5f3e2007-12-11 12:42:00 -05001036 int level;
Chris Mason1a5bc162007-10-15 16:15:26 -04001037 int err = 0;
Chris Mason037e6392007-03-07 11:50:24 -05001038
Chris Mason5f39d392007-10-15 16:14:19 -04001039 btrfs_set_stack_extent_refs(&extent_item, 1);
Chris Mason62e27492007-03-15 12:56:47 -04001040 btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
Chris Mason7bb86312007-12-11 09:25:06 -05001041 path = btrfs_alloc_path();
Chris Mason037e6392007-03-07 11:50:24 -05001042
Chris Mason26b80032007-08-08 20:17:12 -04001043 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04001044 ret = find_first_extent_bit(&info->extent_ins, 0, &start,
1045 &end, EXTENT_LOCKED);
1046 if (ret)
Chris Mason26b80032007-08-08 20:17:12 -04001047 break;
1048
Chris Mason1a5bc162007-10-15 16:15:26 -04001049 ins.objectid = start;
1050 ins.offset = end + 1 - start;
1051 err = btrfs_insert_item(trans, extent_root, &ins,
1052 &extent_item, sizeof(extent_item));
1053 clear_extent_bits(&info->extent_ins, start, end, EXTENT_LOCKED,
1054 GFP_NOFS);
Chris Masond8d5f3e2007-12-11 12:42:00 -05001055 eb = read_tree_block(extent_root, ins.objectid, ins.offset);
1056 level = btrfs_header_level(eb);
1057 if (level == 0) {
1058 btrfs_item_key(eb, &first, 0);
1059 } else {
1060 btrfs_node_key(eb, &first, 0);
1061 }
Chris Mason7bb86312007-12-11 09:25:06 -05001062 err = btrfs_insert_extent_backref(trans, extent_root, path,
1063 start, extent_root->root_key.objectid,
Chris Masond8d5f3e2007-12-11 12:42:00 -05001064 0, btrfs_disk_key_objectid(&first),
1065 level);
Chris Mason7bb86312007-12-11 09:25:06 -05001066 BUG_ON(err);
Chris Masond8d5f3e2007-12-11 12:42:00 -05001067 free_extent_buffer(eb);
Chris Mason037e6392007-03-07 11:50:24 -05001068 }
Chris Mason7bb86312007-12-11 09:25:06 -05001069 btrfs_free_path(path);
Chris Mason037e6392007-03-07 11:50:24 -05001070 return 0;
1071}
1072
Chris Masondb945352007-10-15 16:15:53 -04001073static int pin_down_bytes(struct btrfs_root *root, u64 bytenr, u32 num_bytes,
1074 int pending)
Chris Masone20d96d2007-03-22 12:13:20 -04001075{
Chris Mason1a5bc162007-10-15 16:15:26 -04001076 int err = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001077 struct extent_buffer *buf;
Chris Mason78fae272007-03-25 11:35:08 -04001078
Chris Masonf4b9aa82007-03-27 11:05:53 -04001079 if (!pending) {
Chris Masondb945352007-10-15 16:15:53 -04001080 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
Chris Mason5f39d392007-10-15 16:14:19 -04001081 if (buf) {
1082 if (btrfs_buffer_uptodate(buf)) {
Chris Mason2c90e5d2007-04-02 10:50:19 -04001083 u64 transid =
1084 root->fs_info->running_transaction->transid;
Chris Mason5f39d392007-10-15 16:14:19 -04001085 if (btrfs_header_generation(buf) == transid) {
1086 free_extent_buffer(buf);
Yanc5492282007-11-06 10:25:25 -05001087 return 1;
Chris Mason2c90e5d2007-04-02 10:50:19 -04001088 }
Chris Masonf4b9aa82007-03-27 11:05:53 -04001089 }
Chris Mason5f39d392007-10-15 16:14:19 -04001090 free_extent_buffer(buf);
Chris Mason8ef97622007-03-26 10:15:30 -04001091 }
Yan324ae4d2007-11-16 14:57:08 -05001092 update_pinned_extents(root, bytenr, num_bytes, 1);
Chris Masonf4b9aa82007-03-27 11:05:53 -04001093 } else {
Chris Mason1a5bc162007-10-15 16:15:26 -04001094 set_extent_bits(&root->fs_info->pending_del,
Chris Masondb945352007-10-15 16:15:53 -04001095 bytenr, bytenr + num_bytes - 1,
1096 EXTENT_LOCKED, GFP_NOFS);
Chris Masonf4b9aa82007-03-27 11:05:53 -04001097 }
Chris Masonbe744172007-05-06 10:15:01 -04001098 BUG_ON(err < 0);
Chris Masone20d96d2007-03-22 12:13:20 -04001099 return 0;
1100}
1101
Chris Masona28ec192007-03-06 20:08:01 -05001102/*
1103 * remove an extent from the root, returns 0 on success
1104 */
Chris Masone089f052007-03-16 16:20:31 -04001105static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason7bb86312007-12-11 09:25:06 -05001106 *root, u64 bytenr, u64 num_bytes,
1107 u64 root_objectid, u64 ref_generation,
1108 u64 owner_objectid, u64 owner_offset, int pin,
Chris Masone37c9e62007-05-09 20:13:14 -04001109 int mark_free)
Chris Masona28ec192007-03-06 20:08:01 -05001110{
Chris Mason5caf2a02007-04-02 11:20:42 -04001111 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -04001112 struct btrfs_key key;
Chris Mason1261ec42007-03-20 20:35:03 -04001113 struct btrfs_fs_info *info = root->fs_info;
1114 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04001115 struct extent_buffer *leaf;
Chris Masona28ec192007-03-06 20:08:01 -05001116 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -04001117 struct btrfs_extent_item *ei;
Chris Masoncf27e1e2007-03-13 09:49:06 -04001118 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -05001119
Chris Masondb945352007-10-15 16:15:53 -04001120 key.objectid = bytenr;
Chris Mason62e27492007-03-15 12:56:47 -04001121 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masondb945352007-10-15 16:15:53 -04001122 key.offset = num_bytes;
Chris Masona28ec192007-03-06 20:08:01 -05001123
Chris Mason5caf2a02007-04-02 11:20:42 -04001124 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04001125 if (!path)
1126 return -ENOMEM;
1127
Chris Mason7bb86312007-12-11 09:25:06 -05001128 if (ref_generation && owner_objectid == 0 && root_objectid == 3) {
1129//printk("drop backref root %Lu gen %Lu byte %Lu\n", root_objectid, ref_generation, bytenr );
1130 }
1131 ret = lookup_extent_backref(trans, extent_root, path,
1132 bytenr, root_objectid,
1133 ref_generation,
1134 owner_objectid, owner_offset, 1);
1135 if (ret == 0) {
1136 ret = btrfs_del_item(trans, extent_root, path);
1137 } else {
1138 btrfs_print_leaf(extent_root, path->nodes[0]);
1139 WARN_ON(1);
1140 printk("Unable to find ref byte nr %Lu root %Lu "
1141 " gen %Lu owner %Lu offset %Lu\n", bytenr,
1142 root_objectid, ref_generation, owner_objectid,
1143 owner_offset);
1144 }
1145 btrfs_release_path(extent_root, path);
Chris Mason5caf2a02007-04-02 11:20:42 -04001146 ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001147 if (ret < 0)
1148 return ret;
1149 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04001150
1151 leaf = path->nodes[0];
1152 ei = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason123abc82007-03-14 14:14:43 -04001153 struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001154 refs = btrfs_extent_refs(leaf, ei);
1155 BUG_ON(refs == 0);
1156 refs -= 1;
1157 btrfs_set_extent_refs(leaf, ei, refs);
1158 btrfs_mark_buffer_dirty(leaf);
1159
Chris Masoncf27e1e2007-03-13 09:49:06 -04001160 if (refs == 0) {
Chris Masondb945352007-10-15 16:15:53 -04001161 u64 super_used;
1162 u64 root_used;
Chris Mason78fae272007-03-25 11:35:08 -04001163
1164 if (pin) {
Chris Masondb945352007-10-15 16:15:53 -04001165 ret = pin_down_bytes(root, bytenr, num_bytes, 0);
Yanc5492282007-11-06 10:25:25 -05001166 if (ret > 0)
1167 mark_free = 1;
1168 BUG_ON(ret < 0);
Chris Mason78fae272007-03-25 11:35:08 -04001169 }
1170
Josef Bacik58176a92007-08-29 15:47:34 -04001171 /* block accounting for super block */
Chris Masondb945352007-10-15 16:15:53 -04001172 super_used = btrfs_super_bytes_used(&info->super_copy);
1173 btrfs_set_super_bytes_used(&info->super_copy,
1174 super_used - num_bytes);
Josef Bacik58176a92007-08-29 15:47:34 -04001175
1176 /* block accounting for root item */
Chris Masondb945352007-10-15 16:15:53 -04001177 root_used = btrfs_root_used(&root->root_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001178 btrfs_set_root_used(&root->root_item,
Chris Masondb945352007-10-15 16:15:53 -04001179 root_used - num_bytes);
Josef Bacik58176a92007-08-29 15:47:34 -04001180
Chris Mason5caf2a02007-04-02 11:20:42 -04001181 ret = btrfs_del_item(trans, extent_root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -04001182 if (ret) {
1183 return ret;
1184 }
Chris Masondb945352007-10-15 16:15:53 -04001185 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
Chris Mason1e2677e2007-05-29 16:52:18 -04001186 mark_free, 0);
Chris Mason9078a3e2007-04-26 16:46:15 -04001187 BUG_ON(ret);
Chris Masona28ec192007-03-06 20:08:01 -05001188 }
Chris Mason5caf2a02007-04-02 11:20:42 -04001189 btrfs_free_path(path);
Chris Masone089f052007-03-16 16:20:31 -04001190 finish_current_insert(trans, extent_root);
Chris Masona28ec192007-03-06 20:08:01 -05001191 return ret;
1192}
1193
1194/*
Chris Masonfec577f2007-02-26 10:40:21 -05001195 * find all the blocks marked as pending in the radix tree and remove
1196 * them from the extent map
1197 */
Chris Masone089f052007-03-16 16:20:31 -04001198static int del_pending_extents(struct btrfs_trans_handle *trans, struct
1199 btrfs_root *extent_root)
Chris Masonfec577f2007-02-26 10:40:21 -05001200{
1201 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -04001202 int err = 0;
Chris Mason1a5bc162007-10-15 16:15:26 -04001203 u64 start;
1204 u64 end;
1205 struct extent_map_tree *pending_del;
1206 struct extent_map_tree *pinned_extents;
Chris Mason8ef97622007-03-26 10:15:30 -04001207
Chris Mason1a5bc162007-10-15 16:15:26 -04001208 pending_del = &extent_root->fs_info->pending_del;
1209 pinned_extents = &extent_root->fs_info->pinned_extents;
Chris Masonfec577f2007-02-26 10:40:21 -05001210
1211 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04001212 ret = find_first_extent_bit(pending_del, 0, &start, &end,
1213 EXTENT_LOCKED);
1214 if (ret)
Chris Masonfec577f2007-02-26 10:40:21 -05001215 break;
Yan324ae4d2007-11-16 14:57:08 -05001216 update_pinned_extents(extent_root, start, end + 1 - start, 1);
Chris Mason1a5bc162007-10-15 16:15:26 -04001217 clear_extent_bits(pending_del, start, end, EXTENT_LOCKED,
1218 GFP_NOFS);
1219 ret = __free_extent(trans, extent_root,
Chris Mason7bb86312007-12-11 09:25:06 -05001220 start, end + 1 - start,
1221 extent_root->root_key.objectid,
1222 0, 0, 0, 0, 0);
Chris Mason1a5bc162007-10-15 16:15:26 -04001223 if (ret)
1224 err = ret;
Chris Masonfec577f2007-02-26 10:40:21 -05001225 }
Chris Masone20d96d2007-03-22 12:13:20 -04001226 return err;
Chris Masonfec577f2007-02-26 10:40:21 -05001227}
1228
1229/*
1230 * remove an extent from the root, returns 0 on success
1231 */
Chris Masone089f052007-03-16 16:20:31 -04001232int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason7bb86312007-12-11 09:25:06 -05001233 *root, u64 bytenr, u64 num_bytes,
1234 u64 root_objectid, u64 ref_generation,
1235 u64 owner_objectid, u64 owner_offset, int pin)
Chris Masonfec577f2007-02-26 10:40:21 -05001236{
Chris Mason9f5fae22007-03-20 14:38:32 -04001237 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Masonfec577f2007-02-26 10:40:21 -05001238 int pending_ret;
1239 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05001240
Chris Masondb945352007-10-15 16:15:53 -04001241 WARN_ON(num_bytes < root->sectorsize);
Chris Mason7bb86312007-12-11 09:25:06 -05001242 if (!root->ref_cows)
1243 ref_generation = 0;
1244
Chris Masona28ec192007-03-06 20:08:01 -05001245 if (root == extent_root) {
Chris Masondb945352007-10-15 16:15:53 -04001246 pin_down_bytes(root, bytenr, num_bytes, 1);
Chris Masona28ec192007-03-06 20:08:01 -05001247 return 0;
1248 }
Chris Mason7bb86312007-12-11 09:25:06 -05001249 ret = __free_extent(trans, root, bytenr, num_bytes, root_objectid,
1250 ref_generation, owner_objectid, owner_offset,
1251 pin, pin == 0);
Chris Masone20d96d2007-03-22 12:13:20 -04001252 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
Chris Masonfec577f2007-02-26 10:40:21 -05001253 return ret ? ret : pending_ret;
1254}
1255
Chris Mason87ee04e2007-11-30 11:30:34 -05001256static u64 stripe_align(struct btrfs_root *root, u64 val)
1257{
1258 u64 mask = ((u64)root->stripesize - 1);
1259 u64 ret = (val + mask) & ~mask;
1260 return ret;
1261}
1262
Chris Masonfec577f2007-02-26 10:40:21 -05001263/*
1264 * walks the btree of allocated extents and find a hole of a given size.
1265 * The key ins is changed to record the hole:
1266 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -04001267 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -05001268 * ins->offset == number of blocks
1269 * Any available blocks before search_start are skipped.
1270 */
Chris Masone089f052007-03-16 16:20:31 -04001271static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masondb945352007-10-15 16:15:53 -04001272 *orig_root, u64 num_bytes, u64 empty_size,
1273 u64 search_start, u64 search_end, u64 hint_byte,
Chris Masonf2654de2007-06-26 12:20:46 -04001274 struct btrfs_key *ins, u64 exclude_start,
1275 u64 exclude_nr, int data)
Chris Masonfec577f2007-02-26 10:40:21 -05001276{
Chris Mason5caf2a02007-04-02 11:20:42 -04001277 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -04001278 struct btrfs_key key;
Chris Masonfec577f2007-02-26 10:40:21 -05001279 u64 hole_size = 0;
Chris Mason87ee04e2007-11-30 11:30:34 -05001280 u64 aligned;
1281 int ret;
Chris Masonfec577f2007-02-26 10:40:21 -05001282 int slot = 0;
Chris Masondb945352007-10-15 16:15:53 -04001283 u64 last_byte = 0;
Chris Masonbe744172007-05-06 10:15:01 -04001284 u64 orig_search_start = search_start;
Chris Masonfec577f2007-02-26 10:40:21 -05001285 int start_found;
Chris Mason5f39d392007-10-15 16:14:19 -04001286 struct extent_buffer *l;
Chris Mason9f5fae22007-03-20 14:38:32 -04001287 struct btrfs_root * root = orig_root->fs_info->extent_root;
Chris Masonf2458e12007-04-25 15:52:25 -04001288 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -04001289 u64 total_needed = num_bytes;
Chris Masone20d96d2007-03-22 12:13:20 -04001290 int level;
Chris Masonbe08c1b2007-05-03 09:06:49 -04001291 struct btrfs_block_group_cache *block_group;
Chris Masonbe744172007-05-06 10:15:01 -04001292 int full_scan = 0;
Chris Masonfbdc7622007-05-30 10:22:12 -04001293 int wrapped = 0;
Chris Masonf84a8b32007-11-06 10:26:29 -05001294 u64 cached_start;
Chris Masonfec577f2007-02-26 10:40:21 -05001295
Chris Masondb945352007-10-15 16:15:53 -04001296 WARN_ON(num_bytes < root->sectorsize);
Chris Masonb1a4d962007-04-04 15:27:52 -04001297 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
1298
Chris Mason5f39d392007-10-15 16:14:19 -04001299 level = btrfs_header_level(root->node);
1300
Chris Mason015a739c2007-11-26 16:15:16 -08001301 if (num_bytes >= 32 * 1024 * 1024 && hint_byte) {
Chris Mason257d0ce2007-11-07 21:08:16 -05001302 data = BTRFS_BLOCK_GROUP_MIXED;
1303 }
1304
Chris Mason3e1ad542007-05-07 20:03:49 -04001305 if (search_end == (u64)-1)
Chris Masondb945352007-10-15 16:15:53 -04001306 search_end = btrfs_super_total_bytes(&info->super_copy);
1307 if (hint_byte) {
1308 block_group = btrfs_lookup_block_group(info, hint_byte);
Chris Mason1a2b2ac2007-12-04 13:18:24 -05001309 if (!block_group)
1310 hint_byte = search_start;
Chris Masonbe744172007-05-06 10:15:01 -04001311 block_group = btrfs_find_block_group(root, block_group,
Chris Masondb945352007-10-15 16:15:53 -04001312 hint_byte, data, 1);
Chris Masonbe744172007-05-06 10:15:01 -04001313 } else {
1314 block_group = btrfs_find_block_group(root,
Chris Mason1a2b2ac2007-12-04 13:18:24 -05001315 trans->block_group,
1316 search_start, data, 1);
Chris Masonbe744172007-05-06 10:15:01 -04001317 }
1318
Chris Mason6702ed42007-08-07 16:15:09 -04001319 total_needed += empty_size;
Chris Masone0115992007-06-19 16:23:05 -04001320 path = btrfs_alloc_path();
Chris Masonbe744172007-05-06 10:15:01 -04001321check_failed:
Chris Mason70b043f2007-12-13 09:02:46 -05001322 if (!block_group) {
1323 block_group = btrfs_lookup_block_group(info, search_start);
1324 if (!block_group)
1325 block_group = btrfs_lookup_block_group(info,
1326 orig_search_start);
1327 }
Yan5e5745d2007-11-16 14:57:09 -05001328 search_start = find_search_start(root, &block_group, search_start,
1329 total_needed, data, full_scan);
Chris Mason87ee04e2007-11-30 11:30:34 -05001330 search_start = stripe_align(root, search_start);
Chris Masonf84a8b32007-11-06 10:26:29 -05001331 cached_start = search_start;
Chris Mason5caf2a02007-04-02 11:20:42 -04001332 btrfs_init_path(path);
Chris Masonfec577f2007-02-26 10:40:21 -05001333 ins->objectid = search_start;
1334 ins->offset = 0;
Chris Masonfec577f2007-02-26 10:40:21 -05001335 start_found = 0;
Chris Mason2cc58cf2007-08-27 16:49:44 -04001336 path->reada = 2;
Chris Masone37c9e62007-05-09 20:13:14 -04001337
Chris Mason5caf2a02007-04-02 11:20:42 -04001338 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
Chris Mason0f70abe2007-02-28 16:46:22 -05001339 if (ret < 0)
1340 goto error;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001341
Chris Masone37c9e62007-05-09 20:13:14 -04001342 if (path->slots[0] > 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001343 path->slots[0]--;
Chris Masone37c9e62007-05-09 20:13:14 -04001344 }
1345
Chris Mason5f39d392007-10-15 16:14:19 -04001346 l = path->nodes[0];
1347 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
1348
Chris Masone37c9e62007-05-09 20:13:14 -04001349 /*
Chris Mason7bb86312007-12-11 09:25:06 -05001350 * walk backwards to find the first extent item key
Chris Masone37c9e62007-05-09 20:13:14 -04001351 */
Chris Mason7bb86312007-12-11 09:25:06 -05001352 while(btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY) {
1353 if (path->slots[0] == 0) {
1354 ret = btrfs_prev_leaf(root, path);
1355 if (ret != 0) {
1356 ret = btrfs_search_slot(trans, root, ins,
1357 path, 0, 0);
1358 if (ret < 0)
1359 goto error;
1360 if (path->slots[0] > 0)
1361 path->slots[0]--;
1362 break;
1363 }
1364 } else {
Chris Masone37c9e62007-05-09 20:13:14 -04001365 path->slots[0]--;
1366 }
Chris Mason7bb86312007-12-11 09:25:06 -05001367 l = path->nodes[0];
1368 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
Chris Masone37c9e62007-05-09 20:13:14 -04001369 }
Chris Masonfec577f2007-02-26 10:40:21 -05001370 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001371 l = path->nodes[0];
Chris Mason5caf2a02007-04-02 11:20:42 -04001372 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04001373 if (slot >= btrfs_header_nritems(l)) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001374 ret = btrfs_next_leaf(root, path);
Chris Masonfec577f2007-02-26 10:40:21 -05001375 if (ret == 0)
1376 continue;
Chris Mason0f70abe2007-02-28 16:46:22 -05001377 if (ret < 0)
1378 goto error;
Chris Masone19caa52007-10-15 16:17:44 -04001379
1380 search_start = max(search_start,
1381 block_group->key.objectid);
Chris Masonfec577f2007-02-26 10:40:21 -05001382 if (!start_found) {
Chris Mason87ee04e2007-11-30 11:30:34 -05001383 aligned = stripe_align(root, search_start);
1384 ins->objectid = aligned;
1385 if (aligned >= search_end) {
1386 ret = -ENOSPC;
1387 goto error;
1388 }
1389 ins->offset = search_end - aligned;
Chris Masonfec577f2007-02-26 10:40:21 -05001390 start_found = 1;
1391 goto check_pending;
1392 }
Chris Mason87ee04e2007-11-30 11:30:34 -05001393 ins->objectid = stripe_align(root,
1394 last_byte > search_start ?
1395 last_byte : search_start);
1396 if (search_end <= ins->objectid) {
1397 ret = -ENOSPC;
1398 goto error;
1399 }
Chris Mason3e1ad542007-05-07 20:03:49 -04001400 ins->offset = search_end - ins->objectid;
Chris Masone19caa52007-10-15 16:17:44 -04001401 BUG_ON(ins->objectid >= search_end);
Chris Masonfec577f2007-02-26 10:40:21 -05001402 goto check_pending;
1403 }
Chris Mason5f39d392007-10-15 16:14:19 -04001404 btrfs_item_key_to_cpu(l, &key, slot);
Chris Mason96b51792007-10-15 16:15:19 -04001405
Chris Masondb945352007-10-15 16:15:53 -04001406 if (key.objectid >= search_start && key.objectid > last_byte &&
Chris Masone37c9e62007-05-09 20:13:14 -04001407 start_found) {
Chris Masondb945352007-10-15 16:15:53 -04001408 if (last_byte < search_start)
1409 last_byte = search_start;
Chris Mason87ee04e2007-11-30 11:30:34 -05001410 aligned = stripe_align(root, last_byte);
1411 hole_size = key.objectid - aligned;
1412 if (key.objectid > aligned && hole_size >= num_bytes) {
1413 ins->objectid = aligned;
Chris Masone37c9e62007-05-09 20:13:14 -04001414 ins->offset = hole_size;
1415 goto check_pending;
Chris Mason0579da42007-03-07 16:15:30 -05001416 }
Chris Masonfec577f2007-02-26 10:40:21 -05001417 }
Chris Mason96b51792007-10-15 16:15:19 -04001418 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY) {
Chris Mason7bb86312007-12-11 09:25:06 -05001419 if (!start_found && btrfs_key_type(&key) ==
1420 BTRFS_BLOCK_GROUP_ITEM_KEY) {
Chris Masondb945352007-10-15 16:15:53 -04001421 last_byte = key.objectid;
Chris Mason96b51792007-10-15 16:15:19 -04001422 start_found = 1;
1423 }
Chris Masone37c9e62007-05-09 20:13:14 -04001424 goto next;
Chris Mason96b51792007-10-15 16:15:19 -04001425 }
1426
Chris Masone37c9e62007-05-09 20:13:14 -04001427
Chris Mason0579da42007-03-07 16:15:30 -05001428 start_found = 1;
Chris Masondb945352007-10-15 16:15:53 -04001429 last_byte = key.objectid + key.offset;
Chris Masonf510cfe2007-10-15 16:14:48 -04001430
Chris Mason257d0ce2007-11-07 21:08:16 -05001431 if (!full_scan && data != BTRFS_BLOCK_GROUP_MIXED &&
1432 last_byte >= block_group->key.objectid +
Chris Masonbe744172007-05-06 10:15:01 -04001433 block_group->key.offset) {
1434 btrfs_release_path(root, path);
1435 search_start = block_group->key.objectid +
Chris Masone19caa52007-10-15 16:17:44 -04001436 block_group->key.offset;
Chris Masonbe744172007-05-06 10:15:01 -04001437 goto new_group;
1438 }
Chris Mason9078a3e2007-04-26 16:46:15 -04001439next:
Chris Mason5caf2a02007-04-02 11:20:42 -04001440 path->slots[0]++;
Chris Masonde428b62007-05-18 13:28:27 -04001441 cond_resched();
Chris Masonfec577f2007-02-26 10:40:21 -05001442 }
Chris Masonfec577f2007-02-26 10:40:21 -05001443check_pending:
1444 /* we have to make sure we didn't find an extent that has already
1445 * been allocated by the map tree or the original allocation
1446 */
Chris Mason5caf2a02007-04-02 11:20:42 -04001447 btrfs_release_path(root, path);
Chris Masonfec577f2007-02-26 10:40:21 -05001448 BUG_ON(ins->objectid < search_start);
Chris Masone37c9e62007-05-09 20:13:14 -04001449
Chris Masondb945352007-10-15 16:15:53 -04001450 if (ins->objectid + num_bytes >= search_end)
Chris Masoncf675822007-09-17 11:00:51 -04001451 goto enospc;
Chris Mason257d0ce2007-11-07 21:08:16 -05001452 if (!full_scan && data != BTRFS_BLOCK_GROUP_MIXED &&
Yan5cf66422007-11-16 14:57:09 -05001453 ins->objectid + num_bytes > block_group->
Chris Masone19caa52007-10-15 16:17:44 -04001454 key.objectid + block_group->key.offset) {
1455 search_start = block_group->key.objectid +
1456 block_group->key.offset;
1457 goto new_group;
1458 }
Chris Mason1a5bc162007-10-15 16:15:26 -04001459 if (test_range_bit(&info->extent_ins, ins->objectid,
Chris Masondb945352007-10-15 16:15:53 -04001460 ins->objectid + num_bytes -1, EXTENT_LOCKED, 0)) {
1461 search_start = ins->objectid + num_bytes;
Chris Mason1a5bc162007-10-15 16:15:26 -04001462 goto new_group;
1463 }
1464 if (test_range_bit(&info->pinned_extents, ins->objectid,
Chris Masondb945352007-10-15 16:15:53 -04001465 ins->objectid + num_bytes -1, EXTENT_DIRTY, 0)) {
1466 search_start = ins->objectid + num_bytes;
Chris Mason1a5bc162007-10-15 16:15:26 -04001467 goto new_group;
Chris Masonfec577f2007-02-26 10:40:21 -05001468 }
Chris Masondb945352007-10-15 16:15:53 -04001469 if (exclude_nr > 0 && (ins->objectid + num_bytes > exclude_start &&
Chris Masonf2654de2007-06-26 12:20:46 -04001470 ins->objectid < exclude_start + exclude_nr)) {
1471 search_start = exclude_start + exclude_nr;
1472 goto new_group;
1473 }
Chris Masone37c9e62007-05-09 20:13:14 -04001474 if (!data) {
Chris Mason5276aed2007-06-11 21:33:38 -04001475 block_group = btrfs_lookup_block_group(info, ins->objectid);
Chris Mason26b80032007-08-08 20:17:12 -04001476 if (block_group)
1477 trans->block_group = block_group;
Chris Masoncd1bc462007-04-27 10:08:34 -04001478 }
Chris Masondb945352007-10-15 16:15:53 -04001479 ins->offset = num_bytes;
Chris Mason5caf2a02007-04-02 11:20:42 -04001480 btrfs_free_path(path);
Chris Masonfec577f2007-02-26 10:40:21 -05001481 return 0;
Chris Masonbe744172007-05-06 10:15:01 -04001482
1483new_group:
Chris Masondb945352007-10-15 16:15:53 -04001484 if (search_start + num_bytes >= search_end) {
Chris Masoncf675822007-09-17 11:00:51 -04001485enospc:
Chris Masonbe744172007-05-06 10:15:01 -04001486 search_start = orig_search_start;
Chris Masonfbdc7622007-05-30 10:22:12 -04001487 if (full_scan) {
1488 ret = -ENOSPC;
1489 goto error;
1490 }
Chris Mason6702ed42007-08-07 16:15:09 -04001491 if (wrapped) {
1492 if (!full_scan)
1493 total_needed -= empty_size;
Chris Masonfbdc7622007-05-30 10:22:12 -04001494 full_scan = 1;
Chris Mason1a2b2ac2007-12-04 13:18:24 -05001495 data = BTRFS_BLOCK_GROUP_MIXED;
Chris Mason6702ed42007-08-07 16:15:09 -04001496 } else
Chris Masonfbdc7622007-05-30 10:22:12 -04001497 wrapped = 1;
Chris Masonbe744172007-05-06 10:15:01 -04001498 }
Chris Mason5276aed2007-06-11 21:33:38 -04001499 block_group = btrfs_lookup_block_group(info, search_start);
Chris Masonfbdc7622007-05-30 10:22:12 -04001500 cond_resched();
Chris Mason1a2b2ac2007-12-04 13:18:24 -05001501 block_group = btrfs_find_block_group(root, block_group,
1502 search_start, data, 0);
Chris Masonbe744172007-05-06 10:15:01 -04001503 goto check_failed;
1504
Chris Mason0f70abe2007-02-28 16:46:22 -05001505error:
Chris Mason5caf2a02007-04-02 11:20:42 -04001506 btrfs_release_path(root, path);
1507 btrfs_free_path(path);
Chris Mason0f70abe2007-02-28 16:46:22 -05001508 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05001509}
Chris Masonfec577f2007-02-26 10:40:21 -05001510/*
Chris Masonfec577f2007-02-26 10:40:21 -05001511 * finds a free extent and does all the dirty work required for allocation
1512 * returns the key for the extent through ins, and a tree buffer for
1513 * the first block of the extent through buf.
1514 *
1515 * returns 0 if everything worked, non-zero otherwise.
1516 */
Chris Mason4d775672007-04-20 20:23:12 -04001517int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
Chris Mason7bb86312007-12-11 09:25:06 -05001518 struct btrfs_root *root,
1519 u64 num_bytes, u64 root_objectid, u64 ref_generation,
1520 u64 owner, u64 owner_offset,
1521 u64 empty_size, u64 hint_byte,
Chris Masonbe08c1b2007-05-03 09:06:49 -04001522 u64 search_end, struct btrfs_key *ins, int data)
Chris Masonfec577f2007-02-26 10:40:21 -05001523{
1524 int ret;
1525 int pending_ret;
Chris Masondb945352007-10-15 16:15:53 -04001526 u64 super_used, root_used;
Chris Masonfbdc7622007-05-30 10:22:12 -04001527 u64 search_start = 0;
Chris Mason1261ec42007-03-20 20:35:03 -04001528 struct btrfs_fs_info *info = root->fs_info;
1529 struct btrfs_root *extent_root = info->extent_root;
Chris Mason234b63a2007-03-13 10:46:10 -04001530 struct btrfs_extent_item extent_item;
Chris Mason7bb86312007-12-11 09:25:06 -05001531 struct btrfs_path *path;
Chris Mason037e6392007-03-07 11:50:24 -05001532
Chris Mason5f39d392007-10-15 16:14:19 -04001533 btrfs_set_stack_extent_refs(&extent_item, 1);
Chris Masonfec577f2007-02-26 10:40:21 -05001534
Chris Masondb945352007-10-15 16:15:53 -04001535 WARN_ON(num_bytes < root->sectorsize);
1536 ret = find_free_extent(trans, root, num_bytes, empty_size,
1537 search_start, search_end, hint_byte, ins,
Chris Mason26b80032007-08-08 20:17:12 -04001538 trans->alloc_exclude_start,
1539 trans->alloc_exclude_nr, data);
Chris Masonccd467d2007-06-28 15:57:36 -04001540 BUG_ON(ret);
Chris Masonf2654de2007-06-26 12:20:46 -04001541 if (ret)
1542 return ret;
1543
Josef Bacik58176a92007-08-29 15:47:34 -04001544 /* block accounting for super block */
Chris Masondb945352007-10-15 16:15:53 -04001545 super_used = btrfs_super_bytes_used(&info->super_copy);
1546 btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes);
Chris Mason26b80032007-08-08 20:17:12 -04001547
Josef Bacik58176a92007-08-29 15:47:34 -04001548 /* block accounting for root item */
Chris Masondb945352007-10-15 16:15:53 -04001549 root_used = btrfs_root_used(&root->root_item);
1550 btrfs_set_root_used(&root->root_item, root_used + num_bytes);
Josef Bacik58176a92007-08-29 15:47:34 -04001551
Chris Masonf510cfe2007-10-15 16:14:48 -04001552 clear_extent_dirty(&root->fs_info->free_space_cache,
1553 ins->objectid, ins->objectid + ins->offset - 1,
1554 GFP_NOFS);
1555
Chris Mason26b80032007-08-08 20:17:12 -04001556 if (root == extent_root) {
Chris Mason1a5bc162007-10-15 16:15:26 -04001557 set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
1558 ins->objectid + ins->offset - 1,
1559 EXTENT_LOCKED, GFP_NOFS);
Chris Masone19caa52007-10-15 16:17:44 -04001560 WARN_ON(data == 1);
Chris Mason26b80032007-08-08 20:17:12 -04001561 goto update_block;
1562 }
1563
1564 WARN_ON(trans->alloc_exclude_nr);
1565 trans->alloc_exclude_start = ins->objectid;
1566 trans->alloc_exclude_nr = ins->offset;
Chris Masone089f052007-03-16 16:20:31 -04001567 ret = btrfs_insert_item(trans, extent_root, ins, &extent_item,
1568 sizeof(extent_item));
Chris Mason037e6392007-03-07 11:50:24 -05001569
Chris Mason26b80032007-08-08 20:17:12 -04001570 trans->alloc_exclude_start = 0;
1571 trans->alloc_exclude_nr = 0;
Chris Mason7bb86312007-12-11 09:25:06 -05001572 BUG_ON(ret);
1573
1574 path = btrfs_alloc_path();
1575 BUG_ON(!path);
1576 ret = btrfs_insert_extent_backref(trans, extent_root, path,
1577 ins->objectid, root_objectid,
1578 ref_generation, owner, owner_offset);
Chris Mason26b80032007-08-08 20:17:12 -04001579
Chris Masonccd467d2007-06-28 15:57:36 -04001580 BUG_ON(ret);
Chris Mason7bb86312007-12-11 09:25:06 -05001581 btrfs_free_path(path);
Chris Masone089f052007-03-16 16:20:31 -04001582 finish_current_insert(trans, extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -04001583 pending_ret = del_pending_extents(trans, extent_root);
Chris Masonf510cfe2007-10-15 16:14:48 -04001584
Chris Masone37c9e62007-05-09 20:13:14 -04001585 if (ret) {
Chris Mason037e6392007-03-07 11:50:24 -05001586 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -04001587 }
1588 if (pending_ret) {
Chris Mason037e6392007-03-07 11:50:24 -05001589 return pending_ret;
Chris Masone37c9e62007-05-09 20:13:14 -04001590 }
Chris Mason26b80032007-08-08 20:17:12 -04001591
1592update_block:
Chris Mason1e2677e2007-05-29 16:52:18 -04001593 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0,
1594 data);
Chris Masonfabb5682007-06-07 22:13:21 -04001595 BUG_ON(ret);
Chris Mason037e6392007-03-07 11:50:24 -05001596 return 0;
Chris Masonfec577f2007-02-26 10:40:21 -05001597}
1598
1599/*
1600 * helper function to allocate a block for a given tree
1601 * returns the tree buffer or NULL.
1602 */
Chris Mason5f39d392007-10-15 16:14:19 -04001603struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Chris Masondb945352007-10-15 16:15:53 -04001604 struct btrfs_root *root,
Chris Mason7bb86312007-12-11 09:25:06 -05001605 u32 blocksize,
1606 u64 root_objectid, u64 hint,
1607 u64 empty_size)
1608{
1609 u64 ref_generation;
1610
1611 if (root->ref_cows)
1612 ref_generation = trans->transid;
1613 else
1614 ref_generation = 0;
1615
1616
1617 return __btrfs_alloc_free_block(trans, root, blocksize, root_objectid,
1618 ref_generation, 0, 0, hint, empty_size);
1619}
1620
1621/*
1622 * helper function to allocate a block for a given tree
1623 * returns the tree buffer or NULL.
1624 */
1625struct extent_buffer *__btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
1626 struct btrfs_root *root,
1627 u32 blocksize,
1628 u64 root_objectid,
1629 u64 ref_generation,
1630 u64 first_objectid,
1631 int level,
1632 u64 hint,
Chris Mason5f39d392007-10-15 16:14:19 -04001633 u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05001634{
Chris Masone2fa7222007-03-12 16:22:34 -04001635 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -05001636 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04001637 struct extent_buffer *buf;
Chris Masonfec577f2007-02-26 10:40:21 -05001638
Chris Mason7bb86312007-12-11 09:25:06 -05001639 ret = btrfs_alloc_extent(trans, root, blocksize,
1640 root_objectid, ref_generation,
1641 first_objectid, level, empty_size, hint,
Chris Masondb945352007-10-15 16:15:53 -04001642 (u64)-1, &ins, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05001643 if (ret) {
Chris Mason54aa1f42007-06-22 14:16:25 -04001644 BUG_ON(ret > 0);
1645 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05001646 }
Chris Masondb945352007-10-15 16:15:53 -04001647 buf = btrfs_find_create_tree_block(root, ins.objectid, blocksize);
Chris Mason54aa1f42007-06-22 14:16:25 -04001648 if (!buf) {
Chris Mason7bb86312007-12-11 09:25:06 -05001649 btrfs_free_extent(trans, root, ins.objectid, blocksize,
1650 root->root_key.objectid, ref_generation,
1651 0, 0, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001652 return ERR_PTR(-ENOMEM);
1653 }
Chris Mason5f39d392007-10-15 16:14:19 -04001654 btrfs_set_buffer_uptodate(buf);
1655 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
1656 buf->start + buf->len - 1, GFP_NOFS);
Chris Mason19c00dd2007-10-15 16:19:22 -04001657 set_extent_bits(&BTRFS_I(root->fs_info->btree_inode)->extent_tree,
1658 buf->start, buf->start + buf->len - 1,
1659 EXTENT_CSUM, GFP_NOFS);
1660 buf->flags |= EXTENT_CSUM;
Chris Mason6b800532007-10-15 16:17:34 -04001661 btrfs_set_buffer_defrag(buf);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001662 trans->blocks_used++;
Chris Masonfec577f2007-02-26 10:40:21 -05001663 return buf;
1664}
Chris Masona28ec192007-03-06 20:08:01 -05001665
Chris Mason6407bf62007-03-27 06:33:00 -04001666static int drop_leaf_ref(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001667 struct btrfs_root *root, struct extent_buffer *leaf)
Chris Mason6407bf62007-03-27 06:33:00 -04001668{
Chris Mason7bb86312007-12-11 09:25:06 -05001669 u64 leaf_owner;
1670 u64 leaf_generation;
Chris Mason5f39d392007-10-15 16:14:19 -04001671 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -04001672 struct btrfs_file_extent_item *fi;
1673 int i;
1674 int nritems;
1675 int ret;
1676
Chris Mason5f39d392007-10-15 16:14:19 -04001677 BUG_ON(!btrfs_is_leaf(leaf));
1678 nritems = btrfs_header_nritems(leaf);
Chris Mason7bb86312007-12-11 09:25:06 -05001679 leaf_owner = btrfs_header_owner(leaf);
1680 leaf_generation = btrfs_header_generation(leaf);
1681
Chris Mason6407bf62007-03-27 06:33:00 -04001682 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04001683 u64 disk_bytenr;
Chris Mason5f39d392007-10-15 16:14:19 -04001684
1685 btrfs_item_key_to_cpu(leaf, &key, i);
1686 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason6407bf62007-03-27 06:33:00 -04001687 continue;
1688 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001689 if (btrfs_file_extent_type(leaf, fi) ==
1690 BTRFS_FILE_EXTENT_INLINE)
Chris Mason236454df2007-04-19 13:37:44 -04001691 continue;
Chris Mason6407bf62007-03-27 06:33:00 -04001692 /*
1693 * FIXME make sure to insert a trans record that
1694 * repeats the snapshot del on crash
1695 */
Chris Masondb945352007-10-15 16:15:53 -04001696 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
1697 if (disk_bytenr == 0)
Chris Mason3a686372007-05-24 13:35:57 -04001698 continue;
Chris Masondb945352007-10-15 16:15:53 -04001699 ret = btrfs_free_extent(trans, root, disk_bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -05001700 btrfs_file_extent_disk_num_bytes(leaf, fi),
1701 leaf_owner, leaf_generation,
1702 key.objectid, key.offset, 0);
Chris Mason6407bf62007-03-27 06:33:00 -04001703 BUG_ON(ret);
1704 }
1705 return 0;
1706}
1707
Chris Masone0115992007-06-19 16:23:05 -04001708static void reada_walk_down(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -04001709 struct extent_buffer *node)
Chris Masone0115992007-06-19 16:23:05 -04001710{
1711 int i;
1712 u32 nritems;
Chris Masondb945352007-10-15 16:15:53 -04001713 u64 bytenr;
Chris Masone0115992007-06-19 16:23:05 -04001714 int ret;
1715 u32 refs;
Chris Masondb945352007-10-15 16:15:53 -04001716 int level;
1717 u32 blocksize;
Chris Masone0115992007-06-19 16:23:05 -04001718
Chris Mason5f39d392007-10-15 16:14:19 -04001719 nritems = btrfs_header_nritems(node);
Chris Masondb945352007-10-15 16:15:53 -04001720 level = btrfs_header_level(node);
Chris Masone0115992007-06-19 16:23:05 -04001721 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04001722 bytenr = btrfs_node_blockptr(node, i);
1723 blocksize = btrfs_level_size(root, level - 1);
1724 ret = lookup_extent_ref(NULL, root, bytenr, blocksize, &refs);
Chris Masone0115992007-06-19 16:23:05 -04001725 BUG_ON(ret);
1726 if (refs != 1)
1727 continue;
Chris Mason409eb952007-08-08 20:17:12 -04001728 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masondb945352007-10-15 16:15:53 -04001729 ret = readahead_tree_block(root, bytenr, blocksize);
Chris Mason409eb952007-08-08 20:17:12 -04001730 cond_resched();
1731 mutex_lock(&root->fs_info->fs_mutex);
Chris Masone0115992007-06-19 16:23:05 -04001732 if (ret)
1733 break;
1734 }
1735}
1736
Chris Mason9aca1d52007-03-13 11:09:37 -04001737/*
1738 * helper function for drop_snapshot, this walks down the tree dropping ref
1739 * counts as it goes.
1740 */
Chris Masone089f052007-03-16 16:20:31 -04001741static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
1742 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05001743{
Chris Mason7bb86312007-12-11 09:25:06 -05001744 u64 root_owner;
1745 u64 root_gen;
1746 u64 bytenr;
Chris Mason5f39d392007-10-15 16:14:19 -04001747 struct extent_buffer *next;
1748 struct extent_buffer *cur;
Chris Mason7bb86312007-12-11 09:25:06 -05001749 struct extent_buffer *parent;
Chris Masondb945352007-10-15 16:15:53 -04001750 u32 blocksize;
Chris Mason20524f02007-03-10 06:35:47 -05001751 int ret;
1752 u32 refs;
1753
Chris Mason5caf2a02007-04-02 11:20:42 -04001754 WARN_ON(*level < 0);
1755 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason5f39d392007-10-15 16:14:19 -04001756 ret = lookup_extent_ref(trans, root,
Chris Masondb945352007-10-15 16:15:53 -04001757 path->nodes[*level]->start,
1758 path->nodes[*level]->len, &refs);
Chris Mason20524f02007-03-10 06:35:47 -05001759 BUG_ON(ret);
1760 if (refs > 1)
1761 goto out;
Chris Masone0115992007-06-19 16:23:05 -04001762
Chris Mason9aca1d52007-03-13 11:09:37 -04001763 /*
1764 * walk down to the last node level and free all the leaves
1765 */
Chris Mason6407bf62007-03-27 06:33:00 -04001766 while(*level >= 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001767 WARN_ON(*level < 0);
1768 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason20524f02007-03-10 06:35:47 -05001769 cur = path->nodes[*level];
Chris Masone0115992007-06-19 16:23:05 -04001770
1771 if (*level > 0 && path->slots[*level] == 0)
Chris Mason5f39d392007-10-15 16:14:19 -04001772 reada_walk_down(root, cur);
Chris Masone0115992007-06-19 16:23:05 -04001773
Chris Mason5f39d392007-10-15 16:14:19 -04001774 if (btrfs_header_level(cur) != *level)
Chris Mason2c90e5d2007-04-02 10:50:19 -04001775 WARN_ON(1);
Chris Masone0115992007-06-19 16:23:05 -04001776
Chris Mason7518a232007-03-12 12:01:18 -04001777 if (path->slots[*level] >=
Chris Mason5f39d392007-10-15 16:14:19 -04001778 btrfs_header_nritems(cur))
Chris Mason20524f02007-03-10 06:35:47 -05001779 break;
Chris Mason6407bf62007-03-27 06:33:00 -04001780 if (*level == 0) {
1781 ret = drop_leaf_ref(trans, root, cur);
1782 BUG_ON(ret);
1783 break;
1784 }
Chris Masondb945352007-10-15 16:15:53 -04001785 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
1786 blocksize = btrfs_level_size(root, *level - 1);
1787 ret = lookup_extent_ref(trans, root, bytenr, blocksize, &refs);
Chris Mason6407bf62007-03-27 06:33:00 -04001788 BUG_ON(ret);
1789 if (refs != 1) {
Chris Mason7bb86312007-12-11 09:25:06 -05001790 parent = path->nodes[*level];
1791 root_owner = btrfs_header_owner(parent);
1792 root_gen = btrfs_header_generation(parent);
Chris Mason20524f02007-03-10 06:35:47 -05001793 path->slots[*level]++;
Chris Masondb945352007-10-15 16:15:53 -04001794 ret = btrfs_free_extent(trans, root, bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -05001795 blocksize, root_owner,
1796 root_gen, 0, 0, 1);
Chris Mason20524f02007-03-10 06:35:47 -05001797 BUG_ON(ret);
1798 continue;
1799 }
Chris Masondb945352007-10-15 16:15:53 -04001800 next = btrfs_find_tree_block(root, bytenr, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -04001801 if (!next || !btrfs_buffer_uptodate(next)) {
1802 free_extent_buffer(next);
Chris Masone9d0b132007-08-10 14:06:19 -04001803 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masondb945352007-10-15 16:15:53 -04001804 next = read_tree_block(root, bytenr, blocksize);
Chris Masone9d0b132007-08-10 14:06:19 -04001805 mutex_lock(&root->fs_info->fs_mutex);
1806
1807 /* we dropped the lock, check one more time */
Chris Masondb945352007-10-15 16:15:53 -04001808 ret = lookup_extent_ref(trans, root, bytenr,
1809 blocksize, &refs);
Chris Masone9d0b132007-08-10 14:06:19 -04001810 BUG_ON(ret);
1811 if (refs != 1) {
Chris Mason7bb86312007-12-11 09:25:06 -05001812 parent = path->nodes[*level];
1813 root_owner = btrfs_header_owner(parent);
1814 root_gen = btrfs_header_generation(parent);
1815
Chris Masone9d0b132007-08-10 14:06:19 -04001816 path->slots[*level]++;
Chris Mason5f39d392007-10-15 16:14:19 -04001817 free_extent_buffer(next);
Chris Mason7bb86312007-12-11 09:25:06 -05001818 ret = btrfs_free_extent(trans, root, bytenr,
1819 blocksize,
1820 root_owner,
1821 root_gen, 0, 0, 1);
Chris Masone9d0b132007-08-10 14:06:19 -04001822 BUG_ON(ret);
1823 continue;
1824 }
1825 }
Chris Mason5caf2a02007-04-02 11:20:42 -04001826 WARN_ON(*level <= 0);
Chris Mason83e15a22007-03-12 09:03:27 -04001827 if (path->nodes[*level-1])
Chris Mason5f39d392007-10-15 16:14:19 -04001828 free_extent_buffer(path->nodes[*level-1]);
Chris Mason20524f02007-03-10 06:35:47 -05001829 path->nodes[*level-1] = next;
Chris Mason5f39d392007-10-15 16:14:19 -04001830 *level = btrfs_header_level(next);
Chris Mason20524f02007-03-10 06:35:47 -05001831 path->slots[*level] = 0;
1832 }
1833out:
Chris Mason5caf2a02007-04-02 11:20:42 -04001834 WARN_ON(*level < 0);
1835 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason7bb86312007-12-11 09:25:06 -05001836
1837 if (path->nodes[*level] == root->node) {
1838 root_owner = root->root_key.objectid;
1839 parent = path->nodes[*level];
1840 } else {
1841 parent = path->nodes[*level + 1];
1842 root_owner = btrfs_header_owner(parent);
1843 }
1844
1845 root_gen = btrfs_header_generation(parent);
Chris Masondb945352007-10-15 16:15:53 -04001846 ret = btrfs_free_extent(trans, root, path->nodes[*level]->start,
Chris Mason7bb86312007-12-11 09:25:06 -05001847 path->nodes[*level]->len,
1848 root_owner, root_gen, 0, 0, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001849 free_extent_buffer(path->nodes[*level]);
Chris Mason20524f02007-03-10 06:35:47 -05001850 path->nodes[*level] = NULL;
1851 *level += 1;
1852 BUG_ON(ret);
1853 return 0;
1854}
1855
Chris Mason9aca1d52007-03-13 11:09:37 -04001856/*
1857 * helper for dropping snapshots. This walks back up the tree in the path
1858 * to find the first node higher up where we haven't yet gone through
1859 * all the slots
1860 */
Chris Masone089f052007-03-16 16:20:31 -04001861static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root
1862 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05001863{
Chris Mason7bb86312007-12-11 09:25:06 -05001864 u64 root_owner;
1865 u64 root_gen;
1866 struct btrfs_root_item *root_item = &root->root_item;
Chris Mason20524f02007-03-10 06:35:47 -05001867 int i;
1868 int slot;
1869 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04001870
Chris Mason234b63a2007-03-13 10:46:10 -04001871 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Mason20524f02007-03-10 06:35:47 -05001872 slot = path->slots[i];
Chris Mason5f39d392007-10-15 16:14:19 -04001873 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
1874 struct extent_buffer *node;
1875 struct btrfs_disk_key disk_key;
1876 node = path->nodes[i];
Chris Mason20524f02007-03-10 06:35:47 -05001877 path->slots[i]++;
1878 *level = i;
Chris Mason9f3a7422007-08-07 15:52:19 -04001879 WARN_ON(*level == 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001880 btrfs_node_key(node, &disk_key, path->slots[i]);
Chris Mason9f3a7422007-08-07 15:52:19 -04001881 memcpy(&root_item->drop_progress,
Chris Mason5f39d392007-10-15 16:14:19 -04001882 &disk_key, sizeof(disk_key));
Chris Mason9f3a7422007-08-07 15:52:19 -04001883 root_item->drop_level = i;
Chris Mason20524f02007-03-10 06:35:47 -05001884 return 0;
1885 } else {
Chris Mason7bb86312007-12-11 09:25:06 -05001886 if (path->nodes[*level] == root->node) {
1887 root_owner = root->root_key.objectid;
1888 root_gen =
1889 btrfs_header_generation(path->nodes[*level]);
1890 } else {
1891 struct extent_buffer *node;
1892 node = path->nodes[*level + 1];
1893 root_owner = btrfs_header_owner(node);
1894 root_gen = btrfs_header_generation(node);
1895 }
Chris Masone089f052007-03-16 16:20:31 -04001896 ret = btrfs_free_extent(trans, root,
Chris Masondb945352007-10-15 16:15:53 -04001897 path->nodes[*level]->start,
Chris Mason7bb86312007-12-11 09:25:06 -05001898 path->nodes[*level]->len,
1899 root_owner, root_gen, 0, 0, 1);
Chris Mason6407bf62007-03-27 06:33:00 -04001900 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04001901 free_extent_buffer(path->nodes[*level]);
Chris Mason83e15a22007-03-12 09:03:27 -04001902 path->nodes[*level] = NULL;
Chris Mason20524f02007-03-10 06:35:47 -05001903 *level = i + 1;
Chris Mason20524f02007-03-10 06:35:47 -05001904 }
1905 }
1906 return 1;
1907}
1908
Chris Mason9aca1d52007-03-13 11:09:37 -04001909/*
1910 * drop the reference count on the tree rooted at 'snap'. This traverses
1911 * the tree freeing any blocks that have a ref count of zero after being
1912 * decremented.
1913 */
Chris Masone089f052007-03-16 16:20:31 -04001914int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason9f3a7422007-08-07 15:52:19 -04001915 *root)
Chris Mason20524f02007-03-10 06:35:47 -05001916{
Chris Mason3768f362007-03-13 16:47:54 -04001917 int ret = 0;
Chris Mason9aca1d52007-03-13 11:09:37 -04001918 int wret;
Chris Mason20524f02007-03-10 06:35:47 -05001919 int level;
Chris Mason5caf2a02007-04-02 11:20:42 -04001920 struct btrfs_path *path;
Chris Mason20524f02007-03-10 06:35:47 -05001921 int i;
1922 int orig_level;
Chris Mason9f3a7422007-08-07 15:52:19 -04001923 struct btrfs_root_item *root_item = &root->root_item;
Chris Mason20524f02007-03-10 06:35:47 -05001924
Chris Mason5caf2a02007-04-02 11:20:42 -04001925 path = btrfs_alloc_path();
1926 BUG_ON(!path);
Chris Mason20524f02007-03-10 06:35:47 -05001927
Chris Mason5f39d392007-10-15 16:14:19 -04001928 level = btrfs_header_level(root->node);
Chris Mason20524f02007-03-10 06:35:47 -05001929 orig_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04001930 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
1931 path->nodes[level] = root->node;
Chris Masonf510cfe2007-10-15 16:14:48 -04001932 extent_buffer_get(root->node);
Chris Mason9f3a7422007-08-07 15:52:19 -04001933 path->slots[level] = 0;
1934 } else {
1935 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001936 struct btrfs_disk_key found_key;
1937 struct extent_buffer *node;
Chris Mason6702ed42007-08-07 16:15:09 -04001938
Chris Mason9f3a7422007-08-07 15:52:19 -04001939 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Chris Mason6702ed42007-08-07 16:15:09 -04001940 level = root_item->drop_level;
1941 path->lowest_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04001942 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason6702ed42007-08-07 16:15:09 -04001943 if (wret < 0) {
Chris Mason9f3a7422007-08-07 15:52:19 -04001944 ret = wret;
1945 goto out;
1946 }
Chris Mason5f39d392007-10-15 16:14:19 -04001947 node = path->nodes[level];
1948 btrfs_node_key(node, &found_key, path->slots[level]);
1949 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
1950 sizeof(found_key)));
Chris Mason9f3a7422007-08-07 15:52:19 -04001951 }
Chris Mason20524f02007-03-10 06:35:47 -05001952 while(1) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001953 wret = walk_down_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04001954 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05001955 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04001956 if (wret < 0)
1957 ret = wret;
1958
Chris Mason5caf2a02007-04-02 11:20:42 -04001959 wret = walk_up_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04001960 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05001961 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04001962 if (wret < 0)
1963 ret = wret;
Chris Mason409eb952007-08-08 20:17:12 -04001964 ret = -EAGAIN;
Chris Mason409eb952007-08-08 20:17:12 -04001965 break;
Chris Mason20524f02007-03-10 06:35:47 -05001966 }
Chris Mason83e15a22007-03-12 09:03:27 -04001967 for (i = 0; i <= orig_level; i++) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001968 if (path->nodes[i]) {
Chris Mason5f39d392007-10-15 16:14:19 -04001969 free_extent_buffer(path->nodes[i]);
Chris Mason0f827312007-10-15 16:18:56 -04001970 path->nodes[i] = NULL;
Chris Mason83e15a22007-03-12 09:03:27 -04001971 }
Chris Mason20524f02007-03-10 06:35:47 -05001972 }
Chris Mason9f3a7422007-08-07 15:52:19 -04001973out:
Chris Mason5caf2a02007-04-02 11:20:42 -04001974 btrfs_free_path(path);
Chris Mason9aca1d52007-03-13 11:09:37 -04001975 return ret;
Chris Mason20524f02007-03-10 06:35:47 -05001976}
Chris Mason9078a3e2007-04-26 16:46:15 -04001977
Chris Masonbe744172007-05-06 10:15:01 -04001978int btrfs_free_block_groups(struct btrfs_fs_info *info)
1979{
Chris Masonf510cfe2007-10-15 16:14:48 -04001980 u64 start;
1981 u64 end;
Yanb97f9202007-11-01 11:28:41 -04001982 u64 ptr;
Chris Mason96b51792007-10-15 16:15:19 -04001983 int ret;
Chris Mason96b51792007-10-15 16:15:19 -04001984 while(1) {
1985 ret = find_first_extent_bit(&info->block_group_cache, 0,
1986 &start, &end, (unsigned int)-1);
1987 if (ret)
1988 break;
Yanb97f9202007-11-01 11:28:41 -04001989 ret = get_state_private(&info->block_group_cache, start, &ptr);
1990 if (!ret)
1991 kfree((void *)(unsigned long)ptr);
Chris Mason96b51792007-10-15 16:15:19 -04001992 clear_extent_bits(&info->block_group_cache, start,
1993 end, (unsigned int)-1, GFP_NOFS);
1994 }
Chris Masone37c9e62007-05-09 20:13:14 -04001995 while(1) {
Chris Masonf510cfe2007-10-15 16:14:48 -04001996 ret = find_first_extent_bit(&info->free_space_cache, 0,
1997 &start, &end, EXTENT_DIRTY);
1998 if (ret)
Chris Masone37c9e62007-05-09 20:13:14 -04001999 break;
Chris Masonf510cfe2007-10-15 16:14:48 -04002000 clear_extent_dirty(&info->free_space_cache, start,
2001 end, GFP_NOFS);
Chris Masone37c9e62007-05-09 20:13:14 -04002002 }
Chris Masonbe744172007-05-06 10:15:01 -04002003 return 0;
2004}
2005
Chris Mason9078a3e2007-04-26 16:46:15 -04002006int btrfs_read_block_groups(struct btrfs_root *root)
2007{
2008 struct btrfs_path *path;
2009 int ret;
2010 int err = 0;
Chris Mason96b51792007-10-15 16:15:19 -04002011 int bit;
Chris Mason9078a3e2007-04-26 16:46:15 -04002012 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04002013 struct btrfs_fs_info *info = root->fs_info;
Chris Mason96b51792007-10-15 16:15:19 -04002014 struct extent_map_tree *block_group_cache;
Chris Mason9078a3e2007-04-26 16:46:15 -04002015 struct btrfs_key key;
2016 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -04002017 struct extent_buffer *leaf;
Chris Mason96b51792007-10-15 16:15:19 -04002018
2019 block_group_cache = &info->block_group_cache;
Chris Mason9078a3e2007-04-26 16:46:15 -04002020
Chris Masonbe744172007-05-06 10:15:01 -04002021 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04002022 key.objectid = 0;
Chris Masondb945352007-10-15 16:15:53 -04002023 key.offset = BTRFS_BLOCK_GROUP_SIZE;
Chris Mason9078a3e2007-04-26 16:46:15 -04002024 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
2025
2026 path = btrfs_alloc_path();
2027 if (!path)
2028 return -ENOMEM;
2029
2030 while(1) {
Chris Masonbe744172007-05-06 10:15:01 -04002031 ret = btrfs_search_slot(NULL, info->extent_root,
Chris Mason9078a3e2007-04-26 16:46:15 -04002032 &key, path, 0, 0);
2033 if (ret != 0) {
2034 err = ret;
2035 break;
2036 }
Chris Mason5f39d392007-10-15 16:14:19 -04002037 leaf = path->nodes[0];
2038 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason9078a3e2007-04-26 16:46:15 -04002039 cache = kmalloc(sizeof(*cache), GFP_NOFS);
2040 if (!cache) {
2041 err = -1;
2042 break;
2043 }
Chris Mason3e1ad542007-05-07 20:03:49 -04002044
Chris Mason5f39d392007-10-15 16:14:19 -04002045 read_extent_buffer(leaf, &cache->item,
2046 btrfs_item_ptr_offset(leaf, path->slots[0]),
2047 sizeof(cache->item));
Chris Mason9078a3e2007-04-26 16:46:15 -04002048 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Masone37c9e62007-05-09 20:13:14 -04002049 cache->cached = 0;
Yan324ae4d2007-11-16 14:57:08 -05002050 cache->pinned = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04002051 key.objectid = found_key.objectid + found_key.offset;
2052 btrfs_release_path(root, path);
Chris Mason5f39d392007-10-15 16:14:19 -04002053
Chris Masonf84a8b32007-11-06 10:26:29 -05002054 if (cache->item.flags & BTRFS_BLOCK_GROUP_MIXED) {
2055 bit = BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA;
2056 cache->data = BTRFS_BLOCK_GROUP_MIXED;
2057 } else if (cache->item.flags & BTRFS_BLOCK_GROUP_DATA) {
Chris Mason96b51792007-10-15 16:15:19 -04002058 bit = BLOCK_GROUP_DATA;
Chris Masonf84a8b32007-11-06 10:26:29 -05002059 cache->data = BTRFS_BLOCK_GROUP_DATA;
Chris Mason96b51792007-10-15 16:15:19 -04002060 } else {
2061 bit = BLOCK_GROUP_METADATA;
2062 cache->data = 0;
Chris Mason31f3c992007-04-30 15:25:45 -04002063 }
Chris Mason96b51792007-10-15 16:15:19 -04002064
2065 /* use EXTENT_LOCKED to prevent merging */
2066 set_extent_bits(block_group_cache, found_key.objectid,
2067 found_key.objectid + found_key.offset - 1,
2068 bit | EXTENT_LOCKED, GFP_NOFS);
2069 set_state_private(block_group_cache, found_key.objectid,
Jens Axboeae2f5412007-10-19 09:22:59 -04002070 (unsigned long)cache);
Chris Mason96b51792007-10-15 16:15:19 -04002071
Chris Mason9078a3e2007-04-26 16:46:15 -04002072 if (key.objectid >=
Chris Masondb945352007-10-15 16:15:53 -04002073 btrfs_super_total_bytes(&info->super_copy))
Chris Mason9078a3e2007-04-26 16:46:15 -04002074 break;
2075 }
2076
2077 btrfs_free_path(path);
2078 return 0;
2079}