blob: 91c2b27d9689b39665487d52126fb81459a091e6 [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 Masonedbd8d42007-12-21 16:27:24 -050021#include <linux/pagemap.h>
Chris Mason74493f72007-12-11 09:25:06 -050022#include "hash.h"
Chris Masonfec577f2007-02-26 10:40:21 -050023#include "ctree.h"
24#include "disk-io.h"
25#include "print-tree.h"
Chris Masone089f052007-03-16 16:20:31 -040026#include "transaction.h"
Chris Masonfec577f2007-02-26 10:40:21 -050027
Chris Mason96b51792007-10-15 16:15:19 -040028#define BLOCK_GROUP_DATA EXTENT_WRITEBACK
29#define BLOCK_GROUP_METADATA EXTENT_UPTODATE
30#define BLOCK_GROUP_DIRTY EXTENT_DIRTY
31
Chris Masone089f052007-03-16 16:20:31 -040032static int finish_current_insert(struct btrfs_trans_handle *trans, struct
33 btrfs_root *extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -040034static int del_pending_extents(struct btrfs_trans_handle *trans, struct
35 btrfs_root *extent_root);
Chris Masonfec577f2007-02-26 10:40:21 -050036
Chris Masone37c9e62007-05-09 20:13:14 -040037static int cache_block_group(struct btrfs_root *root,
38 struct btrfs_block_group_cache *block_group)
39{
40 struct btrfs_path *path;
41 int ret;
42 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -040043 struct extent_buffer *leaf;
Chris Masonf510cfe2007-10-15 16:14:48 -040044 struct extent_map_tree *free_space_cache;
Chris Masone37c9e62007-05-09 20:13:14 -040045 int slot;
Chris Masone37c9e62007-05-09 20:13:14 -040046 u64 last = 0;
47 u64 hole_size;
Yan7d7d6062007-09-14 16:15:28 -040048 u64 first_free;
Chris Masone37c9e62007-05-09 20:13:14 -040049 int found = 0;
50
Chris Mason00f5c792007-11-30 10:09:33 -050051 if (!block_group)
52 return 0;
53
Chris Masone37c9e62007-05-09 20:13:14 -040054 root = root->fs_info->extent_root;
Chris Masonf510cfe2007-10-15 16:14:48 -040055 free_space_cache = &root->fs_info->free_space_cache;
Chris Masone37c9e62007-05-09 20:13:14 -040056
57 if (block_group->cached)
58 return 0;
Chris Masonf510cfe2007-10-15 16:14:48 -040059
Chris Masone37c9e62007-05-09 20:13:14 -040060 path = btrfs_alloc_path();
61 if (!path)
62 return -ENOMEM;
Yan7d7d6062007-09-14 16:15:28 -040063
Chris Mason2cc58cf2007-08-27 16:49:44 -040064 path->reada = 2;
Yan7d7d6062007-09-14 16:15:28 -040065 first_free = block_group->key.objectid;
Chris Masone37c9e62007-05-09 20:13:14 -040066 key.objectid = block_group->key.objectid;
Chris Masone37c9e62007-05-09 20:13:14 -040067 key.offset = 0;
Yan7d7d6062007-09-14 16:15:28 -040068
Chris Masone37c9e62007-05-09 20:13:14 -040069 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
70 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Yan7d7d6062007-09-14 16:15:28 -040071
Chris Masone37c9e62007-05-09 20:13:14 -040072 if (ret < 0)
73 return ret;
Yan7d7d6062007-09-14 16:15:28 -040074
Chris Masone37c9e62007-05-09 20:13:14 -040075 if (ret && path->slots[0] > 0)
76 path->slots[0]--;
Yan7d7d6062007-09-14 16:15:28 -040077
Chris Masone37c9e62007-05-09 20:13:14 -040078 while(1) {
Chris Mason5f39d392007-10-15 16:14:19 -040079 leaf = path->nodes[0];
Chris Masone37c9e62007-05-09 20:13:14 -040080 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -040081 if (slot >= btrfs_header_nritems(leaf)) {
Chris Masone37c9e62007-05-09 20:13:14 -040082 ret = btrfs_next_leaf(root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -040083 if (ret < 0)
84 goto err;
Chris Masonde428b62007-05-18 13:28:27 -040085 if (ret == 0) {
Chris Masone37c9e62007-05-09 20:13:14 -040086 continue;
Chris Masonde428b62007-05-18 13:28:27 -040087 } else {
Chris Masone37c9e62007-05-09 20:13:14 -040088 break;
89 }
90 }
Yan7d7d6062007-09-14 16:15:28 -040091
Chris Mason5f39d392007-10-15 16:14:19 -040092 btrfs_item_key_to_cpu(leaf, &key, slot);
Yan7d7d6062007-09-14 16:15:28 -040093 if (key.objectid < block_group->key.objectid) {
Chris Mason7bb86312007-12-11 09:25:06 -050094 if (btrfs_key_type(&key) != BTRFS_EXTENT_REF_KEY &&
95 key.objectid + key.offset > first_free)
Yan7d7d6062007-09-14 16:15:28 -040096 first_free = key.objectid + key.offset;
97 goto next;
98 }
99
Chris Masone37c9e62007-05-09 20:13:14 -0400100 if (key.objectid >= block_group->key.objectid +
101 block_group->key.offset) {
Yan7d7d6062007-09-14 16:15:28 -0400102 break;
103 }
104
105 if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) {
106 if (!found) {
107 last = first_free;
108 found = 1;
Chris Masone37c9e62007-05-09 20:13:14 -0400109 }
Chris Masonf510cfe2007-10-15 16:14:48 -0400110 if (key.objectid > last) {
111 hole_size = key.objectid - last;
112 set_extent_dirty(free_space_cache, last,
113 last + hole_size - 1,
114 GFP_NOFS);
Chris Masone37c9e62007-05-09 20:13:14 -0400115 }
Yan7d7d6062007-09-14 16:15:28 -0400116 last = key.objectid + key.offset;
Chris Masone37c9e62007-05-09 20:13:14 -0400117 }
Yan7d7d6062007-09-14 16:15:28 -0400118next:
Chris Masone37c9e62007-05-09 20:13:14 -0400119 path->slots[0]++;
120 }
121
Yan7d7d6062007-09-14 16:15:28 -0400122 if (!found)
123 last = first_free;
124 if (block_group->key.objectid +
125 block_group->key.offset > last) {
126 hole_size = block_group->key.objectid +
127 block_group->key.offset - last;
Chris Masonf510cfe2007-10-15 16:14:48 -0400128 set_extent_dirty(free_space_cache, last,
129 last + hole_size - 1, GFP_NOFS);
Yan7d7d6062007-09-14 16:15:28 -0400130 }
Chris Masone37c9e62007-05-09 20:13:14 -0400131 block_group->cached = 1;
Chris Mason54aa1f42007-06-22 14:16:25 -0400132err:
Chris Masone37c9e62007-05-09 20:13:14 -0400133 btrfs_free_path(path);
134 return 0;
135}
136
Chris Mason5276aed2007-06-11 21:33:38 -0400137struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
138 btrfs_fs_info *info,
Chris Masondb945352007-10-15 16:15:53 -0400139 u64 bytenr)
Chris Masonbe744172007-05-06 10:15:01 -0400140{
Chris Mason96b51792007-10-15 16:15:19 -0400141 struct extent_map_tree *block_group_cache;
142 struct btrfs_block_group_cache *block_group = NULL;
143 u64 ptr;
144 u64 start;
145 u64 end;
Chris Masonbe744172007-05-06 10:15:01 -0400146 int ret;
147
Chris Mason96b51792007-10-15 16:15:19 -0400148 block_group_cache = &info->block_group_cache;
149 ret = find_first_extent_bit(block_group_cache,
Chris Masondb945352007-10-15 16:15:53 -0400150 bytenr, &start, &end,
Chris Mason96b51792007-10-15 16:15:19 -0400151 BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA);
Chris Masonbe744172007-05-06 10:15:01 -0400152 if (ret) {
Chris Mason96b51792007-10-15 16:15:19 -0400153 return NULL;
Chris Masonbe744172007-05-06 10:15:01 -0400154 }
Chris Mason96b51792007-10-15 16:15:19 -0400155 ret = get_state_private(block_group_cache, start, &ptr);
156 if (ret)
157 return NULL;
158
Jens Axboeae2f5412007-10-19 09:22:59 -0400159 block_group = (struct btrfs_block_group_cache *)(unsigned long)ptr;
Yan5cf66422007-11-16 14:57:09 -0500160 if (block_group->key.objectid <= bytenr && bytenr <
Chris Mason96b51792007-10-15 16:15:19 -0400161 block_group->key.objectid + block_group->key.offset)
162 return block_group;
Chris Masonbe744172007-05-06 10:15:01 -0400163 return NULL;
164}
Chris Masone37c9e62007-05-09 20:13:14 -0400165static u64 find_search_start(struct btrfs_root *root,
166 struct btrfs_block_group_cache **cache_ret,
Yan5e5745d2007-11-16 14:57:09 -0500167 u64 search_start, int num,
168 int data, int full_scan)
Chris Masone37c9e62007-05-09 20:13:14 -0400169{
Chris Masone37c9e62007-05-09 20:13:14 -0400170 int ret;
171 struct btrfs_block_group_cache *cache = *cache_ret;
Chris Masone19caa52007-10-15 16:17:44 -0400172 u64 last;
Chris Masonf510cfe2007-10-15 16:14:48 -0400173 u64 start = 0;
174 u64 end = 0;
Chris Mason257d0ce2007-11-07 21:08:16 -0500175 u64 cache_miss = 0;
Chris Masonf84a8b32007-11-06 10:26:29 -0500176 int wrapped = 0;
Chris Masone37c9e62007-05-09 20:13:14 -0400177
Chris Mason00f5c792007-11-30 10:09:33 -0500178 if (!cache) {
Chris Mason1a2b2ac2007-12-04 13:18:24 -0500179 goto out;
Chris Mason00f5c792007-11-30 10:09:33 -0500180 }
Chris Masone37c9e62007-05-09 20:13:14 -0400181again:
Chris Mason54aa1f42007-06-22 14:16:25 -0400182 ret = cache_block_group(root, cache);
183 if (ret)
184 goto out;
Chris Masonf84a8b32007-11-06 10:26:29 -0500185
Chris Masone19caa52007-10-15 16:17:44 -0400186 last = max(search_start, cache->key.objectid);
187
Chris Masone37c9e62007-05-09 20:13:14 -0400188 while(1) {
Chris Masonf510cfe2007-10-15 16:14:48 -0400189 ret = find_first_extent_bit(&root->fs_info->free_space_cache,
190 last, &start, &end, EXTENT_DIRTY);
Chris Masone19caa52007-10-15 16:17:44 -0400191 if (ret) {
Chris Mason257d0ce2007-11-07 21:08:16 -0500192 if (!cache_miss)
193 cache_miss = last;
Chris Masone19caa52007-10-15 16:17:44 -0400194 goto new_group;
195 }
Chris Masonf510cfe2007-10-15 16:14:48 -0400196
197 start = max(last, start);
198 last = end + 1;
Chris Mason257d0ce2007-11-07 21:08:16 -0500199 if (last - start < num) {
200 if (last == cache->key.objectid + cache->key.offset)
201 cache_miss = start;
Chris Masonf510cfe2007-10-15 16:14:48 -0400202 continue;
Chris Mason257d0ce2007-11-07 21:08:16 -0500203 }
204 if (data != BTRFS_BLOCK_GROUP_MIXED &&
Yan5cf66422007-11-16 14:57:09 -0500205 start + num > cache->key.objectid + cache->key.offset)
Chris Masone37c9e62007-05-09 20:13:14 -0400206 goto new_group;
Chris Masonf510cfe2007-10-15 16:14:48 -0400207 return start;
Chris Masone37c9e62007-05-09 20:13:14 -0400208 }
209out:
Chris Mason1a2b2ac2007-12-04 13:18:24 -0500210 cache = btrfs_lookup_block_group(root->fs_info, search_start);
211 if (!cache) {
212 printk("Unable to find block group for %Lu\n",
213 search_start);
214 WARN_ON(1);
215 return search_start;
216 }
Chris Mason1a5bc162007-10-15 16:15:26 -0400217 return search_start;
Chris Masone37c9e62007-05-09 20:13:14 -0400218
219new_group:
Chris Masone19caa52007-10-15 16:17:44 -0400220 last = cache->key.objectid + cache->key.offset;
Chris Masonf84a8b32007-11-06 10:26:29 -0500221wrapped:
Chris Masone19caa52007-10-15 16:17:44 -0400222 cache = btrfs_lookup_block_group(root->fs_info, last);
Chris Masone37c9e62007-05-09 20:13:14 -0400223 if (!cache) {
Chris Mason0e4de582007-11-26 10:55:49 -0500224no_cache:
Chris Masonf84a8b32007-11-06 10:26:29 -0500225 if (!wrapped) {
226 wrapped = 1;
227 last = search_start;
228 data = BTRFS_BLOCK_GROUP_MIXED;
229 goto wrapped;
230 }
Chris Mason1a2b2ac2007-12-04 13:18:24 -0500231 goto out;
Chris Masone37c9e62007-05-09 20:13:14 -0400232 }
Chris Mason257d0ce2007-11-07 21:08:16 -0500233 if (cache_miss && !cache->cached) {
234 cache_block_group(root, cache);
235 last = cache_miss;
Chris Mason257d0ce2007-11-07 21:08:16 -0500236 cache = btrfs_lookup_block_group(root->fs_info, last);
237 }
Chris Mason1a2b2ac2007-12-04 13:18:24 -0500238 cache = btrfs_find_block_group(root, cache, last, data, 0);
Chris Mason0e4de582007-11-26 10:55:49 -0500239 if (!cache)
240 goto no_cache;
Chris Masone37c9e62007-05-09 20:13:14 -0400241 *cache_ret = cache;
Chris Mason257d0ce2007-11-07 21:08:16 -0500242 cache_miss = 0;
Chris Masone37c9e62007-05-09 20:13:14 -0400243 goto again;
244}
245
Chris Mason84f54cf2007-06-12 07:43:08 -0400246static u64 div_factor(u64 num, int factor)
247{
Chris Mason257d0ce2007-11-07 21:08:16 -0500248 if (factor == 10)
249 return num;
Chris Mason84f54cf2007-06-12 07:43:08 -0400250 num *= factor;
251 do_div(num, 10);
252 return num;
253}
254
Chris Mason31f3c992007-04-30 15:25:45 -0400255struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
256 struct btrfs_block_group_cache
Chris Masonbe744172007-05-06 10:15:01 -0400257 *hint, u64 search_start,
Chris Masonde428b62007-05-18 13:28:27 -0400258 int data, int owner)
Chris Masoncd1bc462007-04-27 10:08:34 -0400259{
Chris Mason96b51792007-10-15 16:15:19 -0400260 struct btrfs_block_group_cache *cache;
261 struct extent_map_tree *block_group_cache;
Chris Mason31f3c992007-04-30 15:25:45 -0400262 struct btrfs_block_group_cache *found_group = NULL;
Chris Masoncd1bc462007-04-27 10:08:34 -0400263 struct btrfs_fs_info *info = root->fs_info;
264 u64 used;
Chris Mason31f3c992007-04-30 15:25:45 -0400265 u64 last = 0;
266 u64 hint_last;
Chris Mason96b51792007-10-15 16:15:19 -0400267 u64 start;
268 u64 end;
269 u64 free_check;
270 u64 ptr;
271 int bit;
Chris Masoncd1bc462007-04-27 10:08:34 -0400272 int ret;
Chris Mason31f3c992007-04-30 15:25:45 -0400273 int full_search = 0;
Chris Masonde428b62007-05-18 13:28:27 -0400274 int factor = 8;
Chris Mason1e2677e2007-05-29 16:52:18 -0400275 int data_swap = 0;
Chris Masonde428b62007-05-18 13:28:27 -0400276
Chris Mason96b51792007-10-15 16:15:19 -0400277 block_group_cache = &info->block_group_cache;
278
Chris Masonde428b62007-05-18 13:28:27 -0400279 if (!owner)
Chris Masonf84a8b32007-11-06 10:26:29 -0500280 factor = 8;
Chris Masonbe744172007-05-06 10:15:01 -0400281
Chris Mason257d0ce2007-11-07 21:08:16 -0500282 if (data == BTRFS_BLOCK_GROUP_MIXED) {
Chris Masonf84a8b32007-11-06 10:26:29 -0500283 bit = BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA;
Chris Mason257d0ce2007-11-07 21:08:16 -0500284 factor = 10;
285 } else if (data)
Chris Mason96b51792007-10-15 16:15:19 -0400286 bit = BLOCK_GROUP_DATA;
287 else
288 bit = BLOCK_GROUP_METADATA;
Chris Masonbe744172007-05-06 10:15:01 -0400289
290 if (search_start) {
291 struct btrfs_block_group_cache *shint;
Chris Mason5276aed2007-06-11 21:33:38 -0400292 shint = btrfs_lookup_block_group(info, search_start);
Chris Masonf84a8b32007-11-06 10:26:29 -0500293 if (shint && (shint->data == data ||
294 shint->data == BTRFS_BLOCK_GROUP_MIXED)) {
Chris Masonbe744172007-05-06 10:15:01 -0400295 used = btrfs_block_group_used(&shint->item);
Yan324ae4d2007-11-16 14:57:08 -0500296 if (used + shint->pinned <
297 div_factor(shint->key.offset, factor)) {
Chris Masonbe744172007-05-06 10:15:01 -0400298 return shint;
299 }
300 }
301 }
Chris Masonf84a8b32007-11-06 10:26:29 -0500302 if (hint && (hint->data == data ||
303 hint->data == BTRFS_BLOCK_GROUP_MIXED)) {
Chris Mason31f3c992007-04-30 15:25:45 -0400304 used = btrfs_block_group_used(&hint->item);
Yan324ae4d2007-11-16 14:57:08 -0500305 if (used + hint->pinned <
306 div_factor(hint->key.offset, factor)) {
Chris Mason31f3c992007-04-30 15:25:45 -0400307 return hint;
308 }
Chris Masone19caa52007-10-15 16:17:44 -0400309 last = hint->key.objectid + hint->key.offset;
Chris Mason31f3c992007-04-30 15:25:45 -0400310 hint_last = last;
311 } else {
Chris Masone37c9e62007-05-09 20:13:14 -0400312 if (hint)
313 hint_last = max(hint->key.objectid, search_start);
314 else
315 hint_last = search_start;
316
317 last = hint_last;
Chris Mason31f3c992007-04-30 15:25:45 -0400318 }
Chris Mason31f3c992007-04-30 15:25:45 -0400319again:
Chris Masoncd1bc462007-04-27 10:08:34 -0400320 while(1) {
Chris Mason96b51792007-10-15 16:15:19 -0400321 ret = find_first_extent_bit(block_group_cache, last,
322 &start, &end, bit);
323 if (ret)
Chris Masoncd1bc462007-04-27 10:08:34 -0400324 break;
Chris Mason96b51792007-10-15 16:15:19 -0400325
326 ret = get_state_private(block_group_cache, start, &ptr);
327 if (ret)
328 break;
329
Jens Axboeae2f5412007-10-19 09:22:59 -0400330 cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
Chris Mason96b51792007-10-15 16:15:19 -0400331 last = cache->key.objectid + cache->key.offset;
332 used = btrfs_block_group_used(&cache->item);
333
334 if (full_search)
335 free_check = cache->key.offset;
336 else
337 free_check = div_factor(cache->key.offset, factor);
Yan324ae4d2007-11-16 14:57:08 -0500338 if (used + cache->pinned < free_check) {
Chris Mason96b51792007-10-15 16:15:19 -0400339 found_group = cache;
340 goto found;
Chris Masoncd1bc462007-04-27 10:08:34 -0400341 }
Chris Masonde428b62007-05-18 13:28:27 -0400342 cond_resched();
Chris Masoncd1bc462007-04-27 10:08:34 -0400343 }
Chris Mason31f3c992007-04-30 15:25:45 -0400344 if (!full_search) {
Chris Masonbe744172007-05-06 10:15:01 -0400345 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -0400346 full_search = 1;
347 goto again;
348 }
Chris Mason1e2677e2007-05-29 16:52:18 -0400349 if (!data_swap) {
Chris Mason1e2677e2007-05-29 16:52:18 -0400350 data_swap = 1;
Chris Mason96b51792007-10-15 16:15:19 -0400351 bit = BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA;
Chris Mason1e2677e2007-05-29 16:52:18 -0400352 last = search_start;
353 goto again;
354 }
Chris Masonbe744172007-05-06 10:15:01 -0400355found:
Chris Mason31f3c992007-04-30 15:25:45 -0400356 return found_group;
Chris Masoncd1bc462007-04-27 10:08:34 -0400357}
358
Chris Mason7bb86312007-12-11 09:25:06 -0500359static u64 hash_extent_ref(u64 root_objectid, u64 ref_generation,
Chris Mason74493f72007-12-11 09:25:06 -0500360 u64 owner, u64 owner_offset)
361{
362 u32 high_crc = ~(u32)0;
363 u32 low_crc = ~(u32)0;
364 __le64 lenum;
365
366 lenum = cpu_to_le64(root_objectid);
367 high_crc = crc32c(high_crc, &lenum, sizeof(lenum));
Chris Mason7bb86312007-12-11 09:25:06 -0500368 lenum = cpu_to_le64(ref_generation);
369 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
Chris Mason74493f72007-12-11 09:25:06 -0500370
Chris Mason7bb86312007-12-11 09:25:06 -0500371#if 0
Chris Mason74493f72007-12-11 09:25:06 -0500372 lenum = cpu_to_le64(owner);
373 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
Chris Mason74493f72007-12-11 09:25:06 -0500374 lenum = cpu_to_le64(owner_offset);
375 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
Chris Mason7bb86312007-12-11 09:25:06 -0500376#endif
Chris Mason74493f72007-12-11 09:25:06 -0500377 return ((u64)high_crc << 32) | (u64)low_crc;
378}
379
Chris Mason7bb86312007-12-11 09:25:06 -0500380static int match_extent_ref(struct extent_buffer *leaf,
381 struct btrfs_extent_ref *disk_ref,
382 struct btrfs_extent_ref *cpu_ref)
383{
384 int ret;
385 int len;
386
387 if (cpu_ref->objectid)
388 len = sizeof(*cpu_ref);
389 else
390 len = 2 * sizeof(u64);
391 ret = memcmp_extent_buffer(leaf, cpu_ref, (unsigned long)disk_ref,
392 len);
393 return ret == 0;
394}
395
396static int lookup_extent_backref(struct btrfs_trans_handle *trans,
397 struct btrfs_root *root,
398 struct btrfs_path *path, u64 bytenr,
399 u64 root_objectid, u64 ref_generation,
400 u64 owner, u64 owner_offset, int del)
401{
402 u64 hash;
403 struct btrfs_key key;
404 struct btrfs_key found_key;
405 struct btrfs_extent_ref ref;
406 struct extent_buffer *leaf;
407 struct btrfs_extent_ref *disk_ref;
408 int ret;
409 int ret2;
410
411 btrfs_set_stack_ref_root(&ref, root_objectid);
412 btrfs_set_stack_ref_generation(&ref, ref_generation);
413 btrfs_set_stack_ref_objectid(&ref, owner);
414 btrfs_set_stack_ref_offset(&ref, owner_offset);
415
416 hash = hash_extent_ref(root_objectid, ref_generation, owner,
417 owner_offset);
418 key.offset = hash;
419 key.objectid = bytenr;
420 key.type = BTRFS_EXTENT_REF_KEY;
421
422 while (1) {
423 ret = btrfs_search_slot(trans, root, &key, path,
424 del ? -1 : 0, del);
425 if (ret < 0)
426 goto out;
427 leaf = path->nodes[0];
428 if (ret != 0) {
429 u32 nritems = btrfs_header_nritems(leaf);
430 if (path->slots[0] >= nritems) {
431 ret2 = btrfs_next_leaf(root, path);
432 if (ret2)
433 goto out;
434 leaf = path->nodes[0];
435 }
436 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
437 if (found_key.objectid != bytenr ||
438 found_key.type != BTRFS_EXTENT_REF_KEY)
439 goto out;
440 key.offset = found_key.offset;
441 if (del) {
442 btrfs_release_path(root, path);
443 continue;
444 }
445 }
446 disk_ref = btrfs_item_ptr(path->nodes[0],
447 path->slots[0],
448 struct btrfs_extent_ref);
449 if (match_extent_ref(path->nodes[0], disk_ref, &ref)) {
450 ret = 0;
451 goto out;
452 }
453 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
454 key.offset = found_key.offset + 1;
455 btrfs_release_path(root, path);
456 }
457out:
458 return ret;
459}
460
Chris Masond8d5f3e2007-12-11 12:42:00 -0500461/*
462 * Back reference rules. Back refs have three main goals:
463 *
464 * 1) differentiate between all holders of references to an extent so that
465 * when a reference is dropped we can make sure it was a valid reference
466 * before freeing the extent.
467 *
468 * 2) Provide enough information to quickly find the holders of an extent
469 * if we notice a given block is corrupted or bad.
470 *
471 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
472 * maintenance. This is actually the same as #2, but with a slightly
473 * different use case.
474 *
475 * File extents can be referenced by:
476 *
477 * - multiple snapshots, subvolumes, or different generations in one subvol
478 * - different files inside a single subvolume (in theory, not implemented yet)
479 * - different offsets inside a file (bookend extents in file.c)
480 *
481 * The extent ref structure has fields for:
482 *
483 * - Objectid of the subvolume root
484 * - Generation number of the tree holding the reference
485 * - objectid of the file holding the reference
486 * - offset in the file corresponding to the key holding the reference
487 *
488 * When a file extent is allocated the fields are filled in:
489 * (root_key.objectid, trans->transid, inode objectid, offset in file)
490 *
491 * When a leaf is cow'd new references are added for every file extent found
492 * in the leaf. It looks the same as the create case, but trans->transid
493 * will be different when the block is cow'd.
494 *
495 * (root_key.objectid, trans->transid, inode objectid, offset in file)
496 *
497 * When a file extent is removed either during snapshot deletion or file
498 * truncation, the corresponding back reference is found
499 * by searching for:
500 *
501 * (btrfs_header_owner(leaf), btrfs_header_generation(leaf),
502 * inode objectid, offset in file)
503 *
504 * Btree extents can be referenced by:
505 *
506 * - Different subvolumes
507 * - Different generations of the same subvolume
508 *
509 * Storing sufficient information for a full reverse mapping of a btree
510 * block would require storing the lowest key of the block in the backref,
511 * and it would require updating that lowest key either before write out or
512 * every time it changed. Instead, the objectid of the lowest key is stored
513 * along with the level of the tree block. This provides a hint
514 * about where in the btree the block can be found. Searches through the
515 * btree only need to look for a pointer to that block, so they stop one
516 * level higher than the level recorded in the backref.
517 *
518 * Some btrees do not do reference counting on their extents. These
519 * include the extent tree and the tree of tree roots. Backrefs for these
520 * trees always have a generation of zero.
521 *
522 * When a tree block is created, back references are inserted:
523 *
Chris Masonf6dbff52007-12-13 11:13:32 -0500524 * (root->root_key.objectid, trans->transid or zero, level, lowest_key_objectid)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500525 *
526 * When a tree block is cow'd in a reference counted root,
527 * new back references are added for all the blocks it points to.
528 * These are of the form (trans->transid will have increased since creation):
529 *
Chris Masonf6dbff52007-12-13 11:13:32 -0500530 * (root->root_key.objectid, trans->transid, level, lowest_key_objectid)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500531 *
532 * Because the lowest_key_objectid and the level are just hints
533 * they are not used when backrefs are deleted. When a backref is deleted:
534 *
535 * if backref was for a tree root:
536 * root_objectid = root->root_key.objectid
537 * else
538 * root_objectid = btrfs_header_owner(parent)
539 *
540 * (root_objectid, btrfs_header_generation(parent) or zero, 0, 0)
541 *
542 * Back Reference Key hashing:
543 *
544 * Back references have four fields, each 64 bits long. Unfortunately,
545 * This is hashed into a single 64 bit number and placed into the key offset.
546 * The key objectid corresponds to the first byte in the extent, and the
547 * key type is set to BTRFS_EXTENT_REF_KEY
548 */
Chris Mason7bb86312007-12-11 09:25:06 -0500549int btrfs_insert_extent_backref(struct btrfs_trans_handle *trans,
550 struct btrfs_root *root,
551 struct btrfs_path *path, u64 bytenr,
552 u64 root_objectid, u64 ref_generation,
553 u64 owner, u64 owner_offset)
Chris Mason74493f72007-12-11 09:25:06 -0500554{
555 u64 hash;
556 struct btrfs_key key;
557 struct btrfs_extent_ref ref;
Chris Mason7bb86312007-12-11 09:25:06 -0500558 struct btrfs_extent_ref *disk_ref;
Chris Mason74493f72007-12-11 09:25:06 -0500559 int ret;
560
561 btrfs_set_stack_ref_root(&ref, root_objectid);
Chris Mason7bb86312007-12-11 09:25:06 -0500562 btrfs_set_stack_ref_generation(&ref, ref_generation);
Chris Mason74493f72007-12-11 09:25:06 -0500563 btrfs_set_stack_ref_objectid(&ref, owner);
564 btrfs_set_stack_ref_offset(&ref, owner_offset);
565
Chris Mason7bb86312007-12-11 09:25:06 -0500566 hash = hash_extent_ref(root_objectid, ref_generation, owner,
567 owner_offset);
Chris Mason74493f72007-12-11 09:25:06 -0500568 key.offset = hash;
569 key.objectid = bytenr;
570 key.type = BTRFS_EXTENT_REF_KEY;
571
572 ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(ref));
573 while (ret == -EEXIST) {
Chris Mason7bb86312007-12-11 09:25:06 -0500574 disk_ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
575 struct btrfs_extent_ref);
576 if (match_extent_ref(path->nodes[0], disk_ref, &ref))
577 goto out;
578 key.offset++;
579 btrfs_release_path(root, path);
580 ret = btrfs_insert_empty_item(trans, root, path, &key,
581 sizeof(ref));
Chris Mason74493f72007-12-11 09:25:06 -0500582 }
Chris Mason7bb86312007-12-11 09:25:06 -0500583 if (ret)
584 goto out;
585 disk_ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
586 struct btrfs_extent_ref);
587 write_extent_buffer(path->nodes[0], &ref, (unsigned long)disk_ref,
588 sizeof(ref));
589 btrfs_mark_buffer_dirty(path->nodes[0]);
590out:
591 btrfs_release_path(root, path);
592 return ret;
Chris Mason74493f72007-12-11 09:25:06 -0500593}
594
Chris Masonb18c6682007-04-17 13:26:50 -0400595int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
596 struct btrfs_root *root,
Chris Mason74493f72007-12-11 09:25:06 -0500597 u64 bytenr, u64 num_bytes,
Chris Mason7bb86312007-12-11 09:25:06 -0500598 u64 root_objectid, u64 ref_generation,
Chris Mason74493f72007-12-11 09:25:06 -0500599 u64 owner, u64 owner_offset)
Chris Mason02217ed2007-03-02 16:08:05 -0500600{
Chris Mason5caf2a02007-04-02 11:20:42 -0400601 struct btrfs_path *path;
Chris Mason02217ed2007-03-02 16:08:05 -0500602 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400603 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400604 struct extent_buffer *l;
Chris Mason234b63a2007-03-13 10:46:10 -0400605 struct btrfs_extent_item *item;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400606 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500607
Chris Masondb945352007-10-15 16:15:53 -0400608 WARN_ON(num_bytes < root->sectorsize);
Chris Mason5caf2a02007-04-02 11:20:42 -0400609 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -0400610 if (!path)
611 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -0400612
Chris Masondb945352007-10-15 16:15:53 -0400613 key.objectid = bytenr;
Chris Mason62e27492007-03-15 12:56:47 -0400614 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masondb945352007-10-15 16:15:53 -0400615 key.offset = num_bytes;
Chris Mason5caf2a02007-04-02 11:20:42 -0400616 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -0400617 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400618 if (ret < 0)
619 return ret;
Chris Masona429e512007-04-18 16:15:28 -0400620 if (ret != 0) {
Chris Masona28ec192007-03-06 20:08:01 -0500621 BUG();
Chris Masona429e512007-04-18 16:15:28 -0400622 }
Chris Mason02217ed2007-03-02 16:08:05 -0500623 BUG_ON(ret != 0);
Chris Mason5f39d392007-10-15 16:14:19 -0400624 l = path->nodes[0];
Chris Mason5caf2a02007-04-02 11:20:42 -0400625 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400626 refs = btrfs_extent_refs(l, item);
627 btrfs_set_extent_refs(l, item, refs + 1);
Chris Mason5caf2a02007-04-02 11:20:42 -0400628 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masona28ec192007-03-06 20:08:01 -0500629
Chris Mason5caf2a02007-04-02 11:20:42 -0400630 btrfs_release_path(root->fs_info->extent_root, path);
Chris Mason7bb86312007-12-11 09:25:06 -0500631
632 ret = btrfs_insert_extent_backref(trans, root->fs_info->extent_root,
633 path, bytenr, root_objectid,
634 ref_generation, owner, owner_offset);
635 BUG_ON(ret);
Chris Mason9f5fae22007-03-20 14:38:32 -0400636 finish_current_insert(trans, root->fs_info->extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -0400637 del_pending_extents(trans, root->fs_info->extent_root);
Chris Mason74493f72007-12-11 09:25:06 -0500638
639 btrfs_free_path(path);
Chris Mason02217ed2007-03-02 16:08:05 -0500640 return 0;
641}
642
Chris Masone9d0b132007-08-10 14:06:19 -0400643int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
644 struct btrfs_root *root)
645{
646 finish_current_insert(trans, root->fs_info->extent_root);
647 del_pending_extents(trans, root->fs_info->extent_root);
648 return 0;
649}
650
Chris Masonb18c6682007-04-17 13:26:50 -0400651static int lookup_extent_ref(struct btrfs_trans_handle *trans,
Chris Masondb945352007-10-15 16:15:53 -0400652 struct btrfs_root *root, u64 bytenr,
653 u64 num_bytes, u32 *refs)
Chris Masona28ec192007-03-06 20:08:01 -0500654{
Chris Mason5caf2a02007-04-02 11:20:42 -0400655 struct btrfs_path *path;
Chris Masona28ec192007-03-06 20:08:01 -0500656 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400657 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400658 struct extent_buffer *l;
Chris Mason234b63a2007-03-13 10:46:10 -0400659 struct btrfs_extent_item *item;
Chris Mason5caf2a02007-04-02 11:20:42 -0400660
Chris Masondb945352007-10-15 16:15:53 -0400661 WARN_ON(num_bytes < root->sectorsize);
Chris Mason5caf2a02007-04-02 11:20:42 -0400662 path = btrfs_alloc_path();
Chris Masondb945352007-10-15 16:15:53 -0400663 key.objectid = bytenr;
664 key.offset = num_bytes;
Chris Mason62e27492007-03-15 12:56:47 -0400665 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Mason5caf2a02007-04-02 11:20:42 -0400666 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -0400667 0, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400668 if (ret < 0)
669 goto out;
Chris Mason5f39d392007-10-15 16:14:19 -0400670 if (ret != 0) {
671 btrfs_print_leaf(root, path->nodes[0]);
Chris Masondb945352007-10-15 16:15:53 -0400672 printk("failed to find block number %Lu\n", bytenr);
Chris Masona28ec192007-03-06 20:08:01 -0500673 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -0400674 }
675 l = path->nodes[0];
Chris Mason5caf2a02007-04-02 11:20:42 -0400676 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400677 *refs = btrfs_extent_refs(l, item);
Chris Mason54aa1f42007-06-22 14:16:25 -0400678out:
Chris Mason5caf2a02007-04-02 11:20:42 -0400679 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -0500680 return 0;
681}
682
Chris Masonbe20aa92007-12-17 20:14:01 -0500683u32 btrfs_count_snapshots_in_path(struct btrfs_root *root,
684 struct btrfs_path *count_path,
685 u64 first_extent)
686{
687 struct btrfs_root *extent_root = root->fs_info->extent_root;
688 struct btrfs_path *path;
689 u64 bytenr;
690 u64 found_objectid;
Chris Mason56b453c2008-01-03 09:08:27 -0500691 u64 root_objectid = root->root_key.objectid;
Chris Masonbe20aa92007-12-17 20:14:01 -0500692 u32 total_count = 0;
693 u32 cur_count;
694 u32 refs;
695 u32 nritems;
696 int ret;
697 struct btrfs_key key;
698 struct btrfs_key found_key;
699 struct extent_buffer *l;
700 struct btrfs_extent_item *item;
701 struct btrfs_extent_ref *ref_item;
702 int level = -1;
703
704 path = btrfs_alloc_path();
705again:
706 if (level == -1)
707 bytenr = first_extent;
708 else
709 bytenr = count_path->nodes[level]->start;
710
711 cur_count = 0;
712 key.objectid = bytenr;
713 key.offset = 0;
714
715 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
716 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
717 if (ret < 0)
718 goto out;
719 BUG_ON(ret == 0);
720
721 l = path->nodes[0];
722 btrfs_item_key_to_cpu(l, &found_key, path->slots[0]);
723
724 if (found_key.objectid != bytenr ||
725 found_key.type != BTRFS_EXTENT_ITEM_KEY) {
726 goto out;
727 }
728
729 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
730 refs = btrfs_extent_refs(l, item);
731 while (1) {
732 nritems = btrfs_header_nritems(l);
733 if (path->slots[0] >= nritems) {
734 ret = btrfs_next_leaf(extent_root, path);
735 if (ret == 0)
736 continue;
737 break;
738 }
739 btrfs_item_key_to_cpu(l, &found_key, path->slots[0]);
740 if (found_key.objectid != bytenr)
741 break;
742 if (found_key.type != BTRFS_EXTENT_REF_KEY) {
743 path->slots[0]++;
744 continue;
745 }
746
747 cur_count++;
748 ref_item = btrfs_item_ptr(l, path->slots[0],
749 struct btrfs_extent_ref);
750 found_objectid = btrfs_ref_root(l, ref_item);
751
Chris Mason56b453c2008-01-03 09:08:27 -0500752 if (found_objectid != root_objectid) {
753 total_count = 2;
754 break;
755 }
756 total_count = 1;
Chris Masonbe20aa92007-12-17 20:14:01 -0500757 path->slots[0]++;
758 }
759 if (cur_count == 0) {
760 total_count = 0;
761 goto out;
762 }
763 if (total_count > 1)
764 goto out;
765 if (level >= 0 && root->node == count_path->nodes[level])
766 goto out;
767 level++;
768 btrfs_release_path(root, path);
769 goto again;
770
771out:
772 btrfs_free_path(path);
773 return total_count;
774
775}
776
Chris Masonc5739bb2007-04-10 09:27:04 -0400777int btrfs_inc_root_ref(struct btrfs_trans_handle *trans,
Chris Mason7bb86312007-12-11 09:25:06 -0500778 struct btrfs_root *root, u64 owner_objectid)
Chris Masonc5739bb2007-04-10 09:27:04 -0400779{
Chris Mason7bb86312007-12-11 09:25:06 -0500780 u64 generation;
781 u64 key_objectid;
782 u64 level;
783 u32 nritems;
784 struct btrfs_disk_key disk_key;
785
786 level = btrfs_header_level(root->node);
787 generation = trans->transid;
788 nritems = btrfs_header_nritems(root->node);
789 if (nritems > 0) {
790 if (level == 0)
791 btrfs_item_key(root->node, &disk_key, 0);
792 else
793 btrfs_node_key(root->node, &disk_key, 0);
794 key_objectid = btrfs_disk_key_objectid(&disk_key);
795 } else {
796 key_objectid = 0;
797 }
Chris Masondb945352007-10-15 16:15:53 -0400798 return btrfs_inc_extent_ref(trans, root, root->node->start,
Chris Mason7bb86312007-12-11 09:25:06 -0500799 root->node->len, owner_objectid,
Chris Masonf6dbff52007-12-13 11:13:32 -0500800 generation, level, key_objectid);
Chris Masonc5739bb2007-04-10 09:27:04 -0400801}
802
Chris Masone089f052007-03-16 16:20:31 -0400803int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400804 struct extent_buffer *buf)
Chris Mason02217ed2007-03-02 16:08:05 -0500805{
Chris Masondb945352007-10-15 16:15:53 -0400806 u64 bytenr;
Chris Mason5f39d392007-10-15 16:14:19 -0400807 u32 nritems;
808 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -0400809 struct btrfs_file_extent_item *fi;
Chris Mason02217ed2007-03-02 16:08:05 -0500810 int i;
Chris Masondb945352007-10-15 16:15:53 -0400811 int level;
Chris Mason6407bf62007-03-27 06:33:00 -0400812 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -0400813 int faili;
Chris Masona28ec192007-03-06 20:08:01 -0500814
Chris Mason3768f362007-03-13 16:47:54 -0400815 if (!root->ref_cows)
Chris Masona28ec192007-03-06 20:08:01 -0500816 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400817
Chris Masondb945352007-10-15 16:15:53 -0400818 level = btrfs_header_level(buf);
Chris Mason5f39d392007-10-15 16:14:19 -0400819 nritems = btrfs_header_nritems(buf);
820 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -0400821 if (level == 0) {
822 u64 disk_bytenr;
Chris Mason5f39d392007-10-15 16:14:19 -0400823 btrfs_item_key_to_cpu(buf, &key, i);
824 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason6407bf62007-03-27 06:33:00 -0400825 continue;
Chris Mason5f39d392007-10-15 16:14:19 -0400826 fi = btrfs_item_ptr(buf, i,
Chris Mason6407bf62007-03-27 06:33:00 -0400827 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400828 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason236454d2007-04-19 13:37:44 -0400829 BTRFS_FILE_EXTENT_INLINE)
830 continue;
Chris Masondb945352007-10-15 16:15:53 -0400831 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
832 if (disk_bytenr == 0)
Chris Mason3a686372007-05-24 13:35:57 -0400833 continue;
Chris Masondb945352007-10-15 16:15:53 -0400834 ret = btrfs_inc_extent_ref(trans, root, disk_bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -0500835 btrfs_file_extent_disk_num_bytes(buf, fi),
836 root->root_key.objectid, trans->transid,
837 key.objectid, key.offset);
Chris Mason54aa1f42007-06-22 14:16:25 -0400838 if (ret) {
839 faili = i;
840 goto fail;
841 }
Chris Mason6407bf62007-03-27 06:33:00 -0400842 } else {
Chris Masondb945352007-10-15 16:15:53 -0400843 bytenr = btrfs_node_blockptr(buf, i);
Chris Mason6caab482007-12-13 09:48:07 -0500844 btrfs_node_key_to_cpu(buf, &key, i);
Chris Masondb945352007-10-15 16:15:53 -0400845 ret = btrfs_inc_extent_ref(trans, root, bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -0500846 btrfs_level_size(root, level - 1),
847 root->root_key.objectid,
Chris Masonf6dbff52007-12-13 11:13:32 -0500848 trans->transid,
849 level - 1, key.objectid);
Chris Mason54aa1f42007-06-22 14:16:25 -0400850 if (ret) {
851 faili = i;
852 goto fail;
853 }
Chris Mason6407bf62007-03-27 06:33:00 -0400854 }
Chris Mason02217ed2007-03-02 16:08:05 -0500855 }
856 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -0400857fail:
Chris Masonccd467d2007-06-28 15:57:36 -0400858 WARN_ON(1);
Chris Mason7bb86312007-12-11 09:25:06 -0500859#if 0
Chris Mason54aa1f42007-06-22 14:16:25 -0400860 for (i =0; i < faili; i++) {
Chris Masondb945352007-10-15 16:15:53 -0400861 if (level == 0) {
862 u64 disk_bytenr;
Chris Mason5f39d392007-10-15 16:14:19 -0400863 btrfs_item_key_to_cpu(buf, &key, i);
864 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -0400865 continue;
Chris Mason5f39d392007-10-15 16:14:19 -0400866 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -0400867 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400868 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -0400869 BTRFS_FILE_EXTENT_INLINE)
870 continue;
Chris Masondb945352007-10-15 16:15:53 -0400871 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
872 if (disk_bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -0400873 continue;
Chris Masondb945352007-10-15 16:15:53 -0400874 err = btrfs_free_extent(trans, root, disk_bytenr,
875 btrfs_file_extent_disk_num_bytes(buf,
Chris Mason5f39d392007-10-15 16:14:19 -0400876 fi), 0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400877 BUG_ON(err);
878 } else {
Chris Masondb945352007-10-15 16:15:53 -0400879 bytenr = btrfs_node_blockptr(buf, i);
880 err = btrfs_free_extent(trans, root, bytenr,
881 btrfs_level_size(root, level - 1), 0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400882 BUG_ON(err);
883 }
884 }
Chris Mason7bb86312007-12-11 09:25:06 -0500885#endif
Chris Mason54aa1f42007-06-22 14:16:25 -0400886 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -0500887}
888
Chris Mason9078a3e2007-04-26 16:46:15 -0400889static int write_one_cache_group(struct btrfs_trans_handle *trans,
890 struct btrfs_root *root,
891 struct btrfs_path *path,
892 struct btrfs_block_group_cache *cache)
893{
894 int ret;
895 int pending_ret;
896 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -0400897 unsigned long bi;
898 struct extent_buffer *leaf;
Chris Mason9078a3e2007-04-26 16:46:15 -0400899
Chris Mason9078a3e2007-04-26 16:46:15 -0400900 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400901 if (ret < 0)
902 goto fail;
Chris Mason9078a3e2007-04-26 16:46:15 -0400903 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -0400904
905 leaf = path->nodes[0];
906 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
907 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
908 btrfs_mark_buffer_dirty(leaf);
Chris Mason9078a3e2007-04-26 16:46:15 -0400909 btrfs_release_path(extent_root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -0400910fail:
Chris Mason9078a3e2007-04-26 16:46:15 -0400911 finish_current_insert(trans, extent_root);
912 pending_ret = del_pending_extents(trans, extent_root);
913 if (ret)
914 return ret;
915 if (pending_ret)
916 return pending_ret;
917 return 0;
918
919}
920
Chris Mason96b51792007-10-15 16:15:19 -0400921int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
922 struct btrfs_root *root)
Chris Mason9078a3e2007-04-26 16:46:15 -0400923{
Chris Mason96b51792007-10-15 16:15:19 -0400924 struct extent_map_tree *block_group_cache;
925 struct btrfs_block_group_cache *cache;
Chris Mason9078a3e2007-04-26 16:46:15 -0400926 int ret;
927 int err = 0;
928 int werr = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -0400929 struct btrfs_path *path;
Chris Mason96b51792007-10-15 16:15:19 -0400930 u64 last = 0;
931 u64 start;
932 u64 end;
933 u64 ptr;
Chris Mason9078a3e2007-04-26 16:46:15 -0400934
Chris Mason96b51792007-10-15 16:15:19 -0400935 block_group_cache = &root->fs_info->block_group_cache;
Chris Mason9078a3e2007-04-26 16:46:15 -0400936 path = btrfs_alloc_path();
937 if (!path)
938 return -ENOMEM;
939
940 while(1) {
Chris Mason96b51792007-10-15 16:15:19 -0400941 ret = find_first_extent_bit(block_group_cache, last,
942 &start, &end, BLOCK_GROUP_DIRTY);
943 if (ret)
Chris Mason9078a3e2007-04-26 16:46:15 -0400944 break;
Chris Mason54aa1f42007-06-22 14:16:25 -0400945
Chris Mason96b51792007-10-15 16:15:19 -0400946 last = end + 1;
947 ret = get_state_private(block_group_cache, start, &ptr);
948 if (ret)
949 break;
950
Jens Axboeae2f5412007-10-19 09:22:59 -0400951 cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
Chris Mason96b51792007-10-15 16:15:19 -0400952 err = write_one_cache_group(trans, root,
953 path, cache);
954 /*
955 * if we fail to write the cache group, we want
956 * to keep it marked dirty in hopes that a later
957 * write will work
958 */
959 if (err) {
960 werr = err;
961 continue;
Chris Mason9078a3e2007-04-26 16:46:15 -0400962 }
Chris Mason96b51792007-10-15 16:15:19 -0400963 clear_extent_bits(block_group_cache, start, end,
964 BLOCK_GROUP_DIRTY, GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -0400965 }
966 btrfs_free_path(path);
967 return werr;
968}
969
970static int update_block_group(struct btrfs_trans_handle *trans,
971 struct btrfs_root *root,
Chris Masondb945352007-10-15 16:15:53 -0400972 u64 bytenr, u64 num_bytes, int alloc,
973 int mark_free, int data)
Chris Mason9078a3e2007-04-26 16:46:15 -0400974{
975 struct btrfs_block_group_cache *cache;
976 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -0400977 u64 total = num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -0400978 u64 old_val;
Chris Masondb945352007-10-15 16:15:53 -0400979 u64 byte_in_group;
Chris Mason96b51792007-10-15 16:15:19 -0400980 u64 start;
981 u64 end;
Chris Mason3e1ad542007-05-07 20:03:49 -0400982
Chris Mason9078a3e2007-04-26 16:46:15 -0400983 while(total) {
Chris Masondb945352007-10-15 16:15:53 -0400984 cache = btrfs_lookup_block_group(info, bytenr);
Chris Mason3e1ad542007-05-07 20:03:49 -0400985 if (!cache) {
Chris Mason9078a3e2007-04-26 16:46:15 -0400986 return -1;
Chris Masoncd1bc462007-04-27 10:08:34 -0400987 }
Chris Masondb945352007-10-15 16:15:53 -0400988 byte_in_group = bytenr - cache->key.objectid;
989 WARN_ON(byte_in_group > cache->key.offset);
Chris Mason96b51792007-10-15 16:15:19 -0400990 start = cache->key.objectid;
991 end = start + cache->key.offset - 1;
992 set_extent_bits(&info->block_group_cache, start, end,
993 BLOCK_GROUP_DIRTY, GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -0400994
995 old_val = btrfs_block_group_used(&cache->item);
Chris Masondb945352007-10-15 16:15:53 -0400996 num_bytes = min(total, cache->key.offset - byte_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -0400997 if (alloc) {
Chris Mason1e2677e2007-05-29 16:52:18 -0400998 if (cache->data != data &&
Chris Mason84f54cf2007-06-12 07:43:08 -0400999 old_val < (cache->key.offset >> 1)) {
Chris Mason96b51792007-10-15 16:15:19 -04001000 int bit_to_clear;
1001 int bit_to_set;
Chris Mason96b51792007-10-15 16:15:19 -04001002 cache->data = data;
Chris Mason1e2677e2007-05-29 16:52:18 -04001003 if (data) {
Yanb97f9202007-11-01 11:28:41 -04001004 bit_to_clear = BLOCK_GROUP_METADATA;
1005 bit_to_set = BLOCK_GROUP_DATA;
Chris Masonf84a8b32007-11-06 10:26:29 -05001006 cache->item.flags &=
1007 ~BTRFS_BLOCK_GROUP_MIXED;
Chris Mason1e2677e2007-05-29 16:52:18 -04001008 cache->item.flags |=
1009 BTRFS_BLOCK_GROUP_DATA;
1010 } else {
Yanb97f9202007-11-01 11:28:41 -04001011 bit_to_clear = BLOCK_GROUP_DATA;
1012 bit_to_set = BLOCK_GROUP_METADATA;
Chris Mason1e2677e2007-05-29 16:52:18 -04001013 cache->item.flags &=
Chris Masonf84a8b32007-11-06 10:26:29 -05001014 ~BTRFS_BLOCK_GROUP_MIXED;
1015 cache->item.flags &=
Chris Mason1e2677e2007-05-29 16:52:18 -04001016 ~BTRFS_BLOCK_GROUP_DATA;
1017 }
Chris Mason96b51792007-10-15 16:15:19 -04001018 clear_extent_bits(&info->block_group_cache,
1019 start, end, bit_to_clear,
1020 GFP_NOFS);
1021 set_extent_bits(&info->block_group_cache,
1022 start, end, bit_to_set,
1023 GFP_NOFS);
Chris Masonf84a8b32007-11-06 10:26:29 -05001024 } else if (cache->data != data &&
1025 cache->data != BTRFS_BLOCK_GROUP_MIXED) {
1026 cache->data = BTRFS_BLOCK_GROUP_MIXED;
1027 set_extent_bits(&info->block_group_cache,
1028 start, end,
1029 BLOCK_GROUP_DATA |
1030 BLOCK_GROUP_METADATA,
1031 GFP_NOFS);
Chris Mason1e2677e2007-05-29 16:52:18 -04001032 }
Chris Masondb945352007-10-15 16:15:53 -04001033 old_val += num_bytes;
Chris Masoncd1bc462007-04-27 10:08:34 -04001034 } else {
Chris Masondb945352007-10-15 16:15:53 -04001035 old_val -= num_bytes;
Chris Masonf510cfe2007-10-15 16:14:48 -04001036 if (mark_free) {
1037 set_extent_dirty(&info->free_space_cache,
Chris Masondb945352007-10-15 16:15:53 -04001038 bytenr, bytenr + num_bytes - 1,
Chris Masonf510cfe2007-10-15 16:14:48 -04001039 GFP_NOFS);
Chris Masone37c9e62007-05-09 20:13:14 -04001040 }
Chris Masoncd1bc462007-04-27 10:08:34 -04001041 }
Chris Mason9078a3e2007-04-26 16:46:15 -04001042 btrfs_set_block_group_used(&cache->item, old_val);
Chris Masondb945352007-10-15 16:15:53 -04001043 total -= num_bytes;
1044 bytenr += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04001045 }
1046 return 0;
1047}
Yan324ae4d2007-11-16 14:57:08 -05001048static int update_pinned_extents(struct btrfs_root *root,
1049 u64 bytenr, u64 num, int pin)
1050{
1051 u64 len;
1052 struct btrfs_block_group_cache *cache;
1053 struct btrfs_fs_info *fs_info = root->fs_info;
1054
1055 if (pin) {
1056 set_extent_dirty(&fs_info->pinned_extents,
1057 bytenr, bytenr + num - 1, GFP_NOFS);
1058 } else {
1059 clear_extent_dirty(&fs_info->pinned_extents,
1060 bytenr, bytenr + num - 1, GFP_NOFS);
1061 }
1062 while (num > 0) {
1063 cache = btrfs_lookup_block_group(fs_info, bytenr);
1064 WARN_ON(!cache);
1065 len = min(num, cache->key.offset -
1066 (bytenr - cache->key.objectid));
1067 if (pin) {
1068 cache->pinned += len;
1069 fs_info->total_pinned += len;
1070 } else {
1071 cache->pinned -= len;
1072 fs_info->total_pinned -= len;
1073 }
1074 bytenr += len;
1075 num -= len;
1076 }
1077 return 0;
1078}
Chris Mason9078a3e2007-04-26 16:46:15 -04001079
Chris Mason1a5bc162007-10-15 16:15:26 -04001080int btrfs_copy_pinned(struct btrfs_root *root, struct extent_map_tree *copy)
Chris Masonccd467d2007-06-28 15:57:36 -04001081{
Chris Masonccd467d2007-06-28 15:57:36 -04001082 u64 last = 0;
Chris Mason1a5bc162007-10-15 16:15:26 -04001083 u64 start;
1084 u64 end;
1085 struct extent_map_tree *pinned_extents = &root->fs_info->pinned_extents;
Chris Masonccd467d2007-06-28 15:57:36 -04001086 int ret;
Chris Masonccd467d2007-06-28 15:57:36 -04001087
1088 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04001089 ret = find_first_extent_bit(pinned_extents, last,
1090 &start, &end, EXTENT_DIRTY);
1091 if (ret)
Chris Masonccd467d2007-06-28 15:57:36 -04001092 break;
Chris Mason1a5bc162007-10-15 16:15:26 -04001093 set_extent_dirty(copy, start, end, GFP_NOFS);
1094 last = end + 1;
Chris Masonccd467d2007-06-28 15:57:36 -04001095 }
1096 return 0;
1097}
1098
1099int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
1100 struct btrfs_root *root,
Chris Mason1a5bc162007-10-15 16:15:26 -04001101 struct extent_map_tree *unpin)
Chris Masona28ec192007-03-06 20:08:01 -05001102{
Chris Mason1a5bc162007-10-15 16:15:26 -04001103 u64 start;
1104 u64 end;
Chris Masona28ec192007-03-06 20:08:01 -05001105 int ret;
Chris Masonf510cfe2007-10-15 16:14:48 -04001106 struct extent_map_tree *free_space_cache;
Chris Masonf510cfe2007-10-15 16:14:48 -04001107 free_space_cache = &root->fs_info->free_space_cache;
Chris Masona28ec192007-03-06 20:08:01 -05001108
1109 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04001110 ret = find_first_extent_bit(unpin, 0, &start, &end,
1111 EXTENT_DIRTY);
1112 if (ret)
Chris Masona28ec192007-03-06 20:08:01 -05001113 break;
Yan324ae4d2007-11-16 14:57:08 -05001114 update_pinned_extents(root, start, end + 1 - start, 0);
Chris Mason1a5bc162007-10-15 16:15:26 -04001115 clear_extent_dirty(unpin, start, end, GFP_NOFS);
1116 set_extent_dirty(free_space_cache, start, end, GFP_NOFS);
Chris Masona28ec192007-03-06 20:08:01 -05001117 }
1118 return 0;
1119}
1120
Chris Masone089f052007-03-16 16:20:31 -04001121static int finish_current_insert(struct btrfs_trans_handle *trans, struct
1122 btrfs_root *extent_root)
Chris Mason037e6392007-03-07 11:50:24 -05001123{
Chris Mason7bb86312007-12-11 09:25:06 -05001124 u64 start;
1125 u64 end;
1126 struct btrfs_fs_info *info = extent_root->fs_info;
Chris Masond8d5f3e2007-12-11 12:42:00 -05001127 struct extent_buffer *eb;
Chris Mason7bb86312007-12-11 09:25:06 -05001128 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -04001129 struct btrfs_key ins;
Chris Masond8d5f3e2007-12-11 12:42:00 -05001130 struct btrfs_disk_key first;
Chris Mason234b63a2007-03-13 10:46:10 -04001131 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -05001132 int ret;
Chris Masond8d5f3e2007-12-11 12:42:00 -05001133 int level;
Chris Mason1a5bc162007-10-15 16:15:26 -04001134 int err = 0;
Chris Mason037e6392007-03-07 11:50:24 -05001135
Chris Mason5f39d392007-10-15 16:14:19 -04001136 btrfs_set_stack_extent_refs(&extent_item, 1);
Chris Mason62e27492007-03-15 12:56:47 -04001137 btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
Chris Mason7bb86312007-12-11 09:25:06 -05001138 path = btrfs_alloc_path();
Chris Mason037e6392007-03-07 11:50:24 -05001139
Chris Mason26b80032007-08-08 20:17:12 -04001140 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04001141 ret = find_first_extent_bit(&info->extent_ins, 0, &start,
1142 &end, EXTENT_LOCKED);
1143 if (ret)
Chris Mason26b80032007-08-08 20:17:12 -04001144 break;
1145
Chris Mason1a5bc162007-10-15 16:15:26 -04001146 ins.objectid = start;
1147 ins.offset = end + 1 - start;
1148 err = btrfs_insert_item(trans, extent_root, &ins,
1149 &extent_item, sizeof(extent_item));
1150 clear_extent_bits(&info->extent_ins, start, end, EXTENT_LOCKED,
1151 GFP_NOFS);
Chris Masond8d5f3e2007-12-11 12:42:00 -05001152 eb = read_tree_block(extent_root, ins.objectid, ins.offset);
1153 level = btrfs_header_level(eb);
1154 if (level == 0) {
1155 btrfs_item_key(eb, &first, 0);
1156 } else {
1157 btrfs_node_key(eb, &first, 0);
1158 }
Chris Mason7bb86312007-12-11 09:25:06 -05001159 err = btrfs_insert_extent_backref(trans, extent_root, path,
1160 start, extent_root->root_key.objectid,
Chris Masonf6dbff52007-12-13 11:13:32 -05001161 0, level,
1162 btrfs_disk_key_objectid(&first));
Chris Mason7bb86312007-12-11 09:25:06 -05001163 BUG_ON(err);
Chris Masond8d5f3e2007-12-11 12:42:00 -05001164 free_extent_buffer(eb);
Chris Mason037e6392007-03-07 11:50:24 -05001165 }
Chris Mason7bb86312007-12-11 09:25:06 -05001166 btrfs_free_path(path);
Chris Mason037e6392007-03-07 11:50:24 -05001167 return 0;
1168}
1169
Chris Masondb945352007-10-15 16:15:53 -04001170static int pin_down_bytes(struct btrfs_root *root, u64 bytenr, u32 num_bytes,
1171 int pending)
Chris Masone20d96d2007-03-22 12:13:20 -04001172{
Chris Mason1a5bc162007-10-15 16:15:26 -04001173 int err = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001174 struct extent_buffer *buf;
Chris Mason78fae272007-03-25 11:35:08 -04001175
Chris Masonf4b9aa82007-03-27 11:05:53 -04001176 if (!pending) {
Chris Masondb945352007-10-15 16:15:53 -04001177 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
Chris Mason5f39d392007-10-15 16:14:19 -04001178 if (buf) {
1179 if (btrfs_buffer_uptodate(buf)) {
Chris Mason2c90e5d2007-04-02 10:50:19 -04001180 u64 transid =
1181 root->fs_info->running_transaction->transid;
Chris Mason5f39d392007-10-15 16:14:19 -04001182 if (btrfs_header_generation(buf) == transid) {
1183 free_extent_buffer(buf);
Yanc5492282007-11-06 10:25:25 -05001184 return 1;
Chris Mason2c90e5d2007-04-02 10:50:19 -04001185 }
Chris Masonf4b9aa82007-03-27 11:05:53 -04001186 }
Chris Mason5f39d392007-10-15 16:14:19 -04001187 free_extent_buffer(buf);
Chris Mason8ef97622007-03-26 10:15:30 -04001188 }
Yan324ae4d2007-11-16 14:57:08 -05001189 update_pinned_extents(root, bytenr, num_bytes, 1);
Chris Masonf4b9aa82007-03-27 11:05:53 -04001190 } else {
Chris Mason1a5bc162007-10-15 16:15:26 -04001191 set_extent_bits(&root->fs_info->pending_del,
Chris Masondb945352007-10-15 16:15:53 -04001192 bytenr, bytenr + num_bytes - 1,
1193 EXTENT_LOCKED, GFP_NOFS);
Chris Masonf4b9aa82007-03-27 11:05:53 -04001194 }
Chris Masonbe744172007-05-06 10:15:01 -04001195 BUG_ON(err < 0);
Chris Masone20d96d2007-03-22 12:13:20 -04001196 return 0;
1197}
1198
Chris Masona28ec192007-03-06 20:08:01 -05001199/*
1200 * remove an extent from the root, returns 0 on success
1201 */
Chris Masone089f052007-03-16 16:20:31 -04001202static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason7bb86312007-12-11 09:25:06 -05001203 *root, u64 bytenr, u64 num_bytes,
1204 u64 root_objectid, u64 ref_generation,
1205 u64 owner_objectid, u64 owner_offset, int pin,
Chris Masone37c9e62007-05-09 20:13:14 -04001206 int mark_free)
Chris Masona28ec192007-03-06 20:08:01 -05001207{
Chris Mason5caf2a02007-04-02 11:20:42 -04001208 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -04001209 struct btrfs_key key;
Chris Mason1261ec42007-03-20 20:35:03 -04001210 struct btrfs_fs_info *info = root->fs_info;
1211 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04001212 struct extent_buffer *leaf;
Chris Masona28ec192007-03-06 20:08:01 -05001213 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -04001214 struct btrfs_extent_item *ei;
Chris Masoncf27e1e2007-03-13 09:49:06 -04001215 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -05001216
Chris Masondb945352007-10-15 16:15:53 -04001217 key.objectid = bytenr;
Chris Mason62e27492007-03-15 12:56:47 -04001218 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masondb945352007-10-15 16:15:53 -04001219 key.offset = num_bytes;
Chris Masona28ec192007-03-06 20:08:01 -05001220
Chris Mason5caf2a02007-04-02 11:20:42 -04001221 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04001222 if (!path)
1223 return -ENOMEM;
1224
Chris Mason7bb86312007-12-11 09:25:06 -05001225 ret = lookup_extent_backref(trans, extent_root, path,
1226 bytenr, root_objectid,
1227 ref_generation,
1228 owner_objectid, owner_offset, 1);
1229 if (ret == 0) {
1230 ret = btrfs_del_item(trans, extent_root, path);
1231 } else {
1232 btrfs_print_leaf(extent_root, path->nodes[0]);
1233 WARN_ON(1);
1234 printk("Unable to find ref byte nr %Lu root %Lu "
1235 " gen %Lu owner %Lu offset %Lu\n", bytenr,
1236 root_objectid, ref_generation, owner_objectid,
1237 owner_offset);
1238 }
1239 btrfs_release_path(extent_root, path);
Chris Mason5caf2a02007-04-02 11:20:42 -04001240 ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001241 if (ret < 0)
1242 return ret;
1243 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04001244
1245 leaf = path->nodes[0];
1246 ei = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason123abc82007-03-14 14:14:43 -04001247 struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001248 refs = btrfs_extent_refs(leaf, ei);
1249 BUG_ON(refs == 0);
1250 refs -= 1;
1251 btrfs_set_extent_refs(leaf, ei, refs);
1252 btrfs_mark_buffer_dirty(leaf);
1253
Chris Masoncf27e1e2007-03-13 09:49:06 -04001254 if (refs == 0) {
Chris Masondb945352007-10-15 16:15:53 -04001255 u64 super_used;
1256 u64 root_used;
Chris Mason78fae272007-03-25 11:35:08 -04001257
1258 if (pin) {
Chris Masondb945352007-10-15 16:15:53 -04001259 ret = pin_down_bytes(root, bytenr, num_bytes, 0);
Yanc5492282007-11-06 10:25:25 -05001260 if (ret > 0)
1261 mark_free = 1;
1262 BUG_ON(ret < 0);
Chris Mason78fae272007-03-25 11:35:08 -04001263 }
1264
Josef Bacik58176a92007-08-29 15:47:34 -04001265 /* block accounting for super block */
Chris Masondb945352007-10-15 16:15:53 -04001266 super_used = btrfs_super_bytes_used(&info->super_copy);
1267 btrfs_set_super_bytes_used(&info->super_copy,
1268 super_used - num_bytes);
Josef Bacik58176a92007-08-29 15:47:34 -04001269
1270 /* block accounting for root item */
Chris Masondb945352007-10-15 16:15:53 -04001271 root_used = btrfs_root_used(&root->root_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001272 btrfs_set_root_used(&root->root_item,
Chris Masondb945352007-10-15 16:15:53 -04001273 root_used - num_bytes);
Josef Bacik58176a92007-08-29 15:47:34 -04001274
Chris Mason5caf2a02007-04-02 11:20:42 -04001275 ret = btrfs_del_item(trans, extent_root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -04001276 if (ret) {
1277 return ret;
1278 }
Chris Masondb945352007-10-15 16:15:53 -04001279 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
Chris Mason1e2677e2007-05-29 16:52:18 -04001280 mark_free, 0);
Chris Mason9078a3e2007-04-26 16:46:15 -04001281 BUG_ON(ret);
Chris Masona28ec192007-03-06 20:08:01 -05001282 }
Chris Mason5caf2a02007-04-02 11:20:42 -04001283 btrfs_free_path(path);
Chris Masone089f052007-03-16 16:20:31 -04001284 finish_current_insert(trans, extent_root);
Chris Masona28ec192007-03-06 20:08:01 -05001285 return ret;
1286}
1287
1288/*
Chris Masonfec577f2007-02-26 10:40:21 -05001289 * find all the blocks marked as pending in the radix tree and remove
1290 * them from the extent map
1291 */
Chris Masone089f052007-03-16 16:20:31 -04001292static int del_pending_extents(struct btrfs_trans_handle *trans, struct
1293 btrfs_root *extent_root)
Chris Masonfec577f2007-02-26 10:40:21 -05001294{
1295 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -04001296 int err = 0;
Chris Mason1a5bc162007-10-15 16:15:26 -04001297 u64 start;
1298 u64 end;
1299 struct extent_map_tree *pending_del;
1300 struct extent_map_tree *pinned_extents;
Chris Mason8ef97622007-03-26 10:15:30 -04001301
Chris Mason1a5bc162007-10-15 16:15:26 -04001302 pending_del = &extent_root->fs_info->pending_del;
1303 pinned_extents = &extent_root->fs_info->pinned_extents;
Chris Masonfec577f2007-02-26 10:40:21 -05001304
1305 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04001306 ret = find_first_extent_bit(pending_del, 0, &start, &end,
1307 EXTENT_LOCKED);
1308 if (ret)
Chris Masonfec577f2007-02-26 10:40:21 -05001309 break;
Yan324ae4d2007-11-16 14:57:08 -05001310 update_pinned_extents(extent_root, start, end + 1 - start, 1);
Chris Mason1a5bc162007-10-15 16:15:26 -04001311 clear_extent_bits(pending_del, start, end, EXTENT_LOCKED,
1312 GFP_NOFS);
1313 ret = __free_extent(trans, extent_root,
Chris Mason7bb86312007-12-11 09:25:06 -05001314 start, end + 1 - start,
1315 extent_root->root_key.objectid,
1316 0, 0, 0, 0, 0);
Chris Mason1a5bc162007-10-15 16:15:26 -04001317 if (ret)
1318 err = ret;
Chris Masonfec577f2007-02-26 10:40:21 -05001319 }
Chris Masone20d96d2007-03-22 12:13:20 -04001320 return err;
Chris Masonfec577f2007-02-26 10:40:21 -05001321}
1322
1323/*
1324 * remove an extent from the root, returns 0 on success
1325 */
Chris Masone089f052007-03-16 16:20:31 -04001326int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason7bb86312007-12-11 09:25:06 -05001327 *root, u64 bytenr, u64 num_bytes,
1328 u64 root_objectid, u64 ref_generation,
1329 u64 owner_objectid, u64 owner_offset, int pin)
Chris Masonfec577f2007-02-26 10:40:21 -05001330{
Chris Mason9f5fae22007-03-20 14:38:32 -04001331 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Masonfec577f2007-02-26 10:40:21 -05001332 int pending_ret;
1333 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05001334
Chris Masondb945352007-10-15 16:15:53 -04001335 WARN_ON(num_bytes < root->sectorsize);
Chris Mason7bb86312007-12-11 09:25:06 -05001336 if (!root->ref_cows)
1337 ref_generation = 0;
1338
Chris Masona28ec192007-03-06 20:08:01 -05001339 if (root == extent_root) {
Chris Masondb945352007-10-15 16:15:53 -04001340 pin_down_bytes(root, bytenr, num_bytes, 1);
Chris Masona28ec192007-03-06 20:08:01 -05001341 return 0;
1342 }
Chris Mason7bb86312007-12-11 09:25:06 -05001343 ret = __free_extent(trans, root, bytenr, num_bytes, root_objectid,
1344 ref_generation, owner_objectid, owner_offset,
1345 pin, pin == 0);
Chris Masone20d96d2007-03-22 12:13:20 -04001346 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
Chris Masonfec577f2007-02-26 10:40:21 -05001347 return ret ? ret : pending_ret;
1348}
1349
Chris Mason87ee04e2007-11-30 11:30:34 -05001350static u64 stripe_align(struct btrfs_root *root, u64 val)
1351{
1352 u64 mask = ((u64)root->stripesize - 1);
1353 u64 ret = (val + mask) & ~mask;
1354 return ret;
1355}
1356
Chris Masonfec577f2007-02-26 10:40:21 -05001357/*
1358 * walks the btree of allocated extents and find a hole of a given size.
1359 * The key ins is changed to record the hole:
1360 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -04001361 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -05001362 * ins->offset == number of blocks
1363 * Any available blocks before search_start are skipped.
1364 */
Chris Masone089f052007-03-16 16:20:31 -04001365static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masondb945352007-10-15 16:15:53 -04001366 *orig_root, u64 num_bytes, u64 empty_size,
1367 u64 search_start, u64 search_end, u64 hint_byte,
Chris Masonf2654de2007-06-26 12:20:46 -04001368 struct btrfs_key *ins, u64 exclude_start,
1369 u64 exclude_nr, int data)
Chris Masonfec577f2007-02-26 10:40:21 -05001370{
Chris Mason5caf2a02007-04-02 11:20:42 -04001371 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -04001372 struct btrfs_key key;
Chris Masonfec577f2007-02-26 10:40:21 -05001373 u64 hole_size = 0;
Chris Mason87ee04e2007-11-30 11:30:34 -05001374 u64 aligned;
1375 int ret;
Chris Masonfec577f2007-02-26 10:40:21 -05001376 int slot = 0;
Chris Masondb945352007-10-15 16:15:53 -04001377 u64 last_byte = 0;
Chris Masonbe744172007-05-06 10:15:01 -04001378 u64 orig_search_start = search_start;
Chris Masonfec577f2007-02-26 10:40:21 -05001379 int start_found;
Chris Mason5f39d392007-10-15 16:14:19 -04001380 struct extent_buffer *l;
Chris Mason9f5fae22007-03-20 14:38:32 -04001381 struct btrfs_root * root = orig_root->fs_info->extent_root;
Chris Masonf2458e12007-04-25 15:52:25 -04001382 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -04001383 u64 total_needed = num_bytes;
Chris Masone20d96d2007-03-22 12:13:20 -04001384 int level;
Chris Masonbe08c1b2007-05-03 09:06:49 -04001385 struct btrfs_block_group_cache *block_group;
Chris Masonbe744172007-05-06 10:15:01 -04001386 int full_scan = 0;
Chris Masonfbdc7622007-05-30 10:22:12 -04001387 int wrapped = 0;
Chris Masonf84a8b32007-11-06 10:26:29 -05001388 u64 cached_start;
Chris Masonfec577f2007-02-26 10:40:21 -05001389
Chris Masondb945352007-10-15 16:15:53 -04001390 WARN_ON(num_bytes < root->sectorsize);
Chris Masonb1a4d962007-04-04 15:27:52 -04001391 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
1392
Chris Mason5f39d392007-10-15 16:14:19 -04001393 level = btrfs_header_level(root->node);
1394
Chris Mason015a739c2007-11-26 16:15:16 -08001395 if (num_bytes >= 32 * 1024 * 1024 && hint_byte) {
Chris Mason257d0ce2007-11-07 21:08:16 -05001396 data = BTRFS_BLOCK_GROUP_MIXED;
1397 }
1398
Chris Mason3e1ad542007-05-07 20:03:49 -04001399 if (search_end == (u64)-1)
Chris Masondb945352007-10-15 16:15:53 -04001400 search_end = btrfs_super_total_bytes(&info->super_copy);
1401 if (hint_byte) {
1402 block_group = btrfs_lookup_block_group(info, hint_byte);
Chris Mason1a2b2ac2007-12-04 13:18:24 -05001403 if (!block_group)
1404 hint_byte = search_start;
Chris Masonbe744172007-05-06 10:15:01 -04001405 block_group = btrfs_find_block_group(root, block_group,
Chris Masondb945352007-10-15 16:15:53 -04001406 hint_byte, data, 1);
Chris Masonbe744172007-05-06 10:15:01 -04001407 } else {
1408 block_group = btrfs_find_block_group(root,
Chris Mason1a2b2ac2007-12-04 13:18:24 -05001409 trans->block_group,
1410 search_start, data, 1);
Chris Masonbe744172007-05-06 10:15:01 -04001411 }
1412
Chris Mason6702ed42007-08-07 16:15:09 -04001413 total_needed += empty_size;
Chris Masone0115992007-06-19 16:23:05 -04001414 path = btrfs_alloc_path();
Chris Masonbe744172007-05-06 10:15:01 -04001415check_failed:
Chris Mason70b043f2007-12-13 09:02:46 -05001416 if (!block_group) {
1417 block_group = btrfs_lookup_block_group(info, search_start);
1418 if (!block_group)
1419 block_group = btrfs_lookup_block_group(info,
1420 orig_search_start);
1421 }
Yan5e5745d2007-11-16 14:57:09 -05001422 search_start = find_search_start(root, &block_group, search_start,
1423 total_needed, data, full_scan);
Chris Mason87ee04e2007-11-30 11:30:34 -05001424 search_start = stripe_align(root, search_start);
Chris Masonf84a8b32007-11-06 10:26:29 -05001425 cached_start = search_start;
Chris Mason5caf2a02007-04-02 11:20:42 -04001426 btrfs_init_path(path);
Chris Masonfec577f2007-02-26 10:40:21 -05001427 ins->objectid = search_start;
1428 ins->offset = 0;
Chris Masonfec577f2007-02-26 10:40:21 -05001429 start_found = 0;
Chris Mason2cc58cf2007-08-27 16:49:44 -04001430 path->reada = 2;
Chris Masone37c9e62007-05-09 20:13:14 -04001431
Chris Mason5caf2a02007-04-02 11:20:42 -04001432 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
Chris Mason0f70abe2007-02-28 16:46:22 -05001433 if (ret < 0)
1434 goto error;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001435
Chris Masone37c9e62007-05-09 20:13:14 -04001436 if (path->slots[0] > 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001437 path->slots[0]--;
Chris Masone37c9e62007-05-09 20:13:14 -04001438 }
1439
Chris Mason5f39d392007-10-15 16:14:19 -04001440 l = path->nodes[0];
1441 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
1442
Chris Masone37c9e62007-05-09 20:13:14 -04001443 /*
Chris Mason7bb86312007-12-11 09:25:06 -05001444 * walk backwards to find the first extent item key
Chris Masone37c9e62007-05-09 20:13:14 -04001445 */
Chris Mason7bb86312007-12-11 09:25:06 -05001446 while(btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY) {
1447 if (path->slots[0] == 0) {
1448 ret = btrfs_prev_leaf(root, path);
1449 if (ret != 0) {
1450 ret = btrfs_search_slot(trans, root, ins,
1451 path, 0, 0);
1452 if (ret < 0)
1453 goto error;
1454 if (path->slots[0] > 0)
1455 path->slots[0]--;
1456 break;
1457 }
1458 } else {
Chris Masone37c9e62007-05-09 20:13:14 -04001459 path->slots[0]--;
1460 }
Chris Mason7bb86312007-12-11 09:25:06 -05001461 l = path->nodes[0];
1462 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
Chris Masone37c9e62007-05-09 20:13:14 -04001463 }
Chris Masonfec577f2007-02-26 10:40:21 -05001464 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001465 l = path->nodes[0];
Chris Mason5caf2a02007-04-02 11:20:42 -04001466 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04001467 if (slot >= btrfs_header_nritems(l)) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001468 ret = btrfs_next_leaf(root, path);
Chris Masonfec577f2007-02-26 10:40:21 -05001469 if (ret == 0)
1470 continue;
Chris Mason0f70abe2007-02-28 16:46:22 -05001471 if (ret < 0)
1472 goto error;
Chris Masone19caa52007-10-15 16:17:44 -04001473
1474 search_start = max(search_start,
1475 block_group->key.objectid);
Chris Masonfec577f2007-02-26 10:40:21 -05001476 if (!start_found) {
Chris Mason87ee04e2007-11-30 11:30:34 -05001477 aligned = stripe_align(root, search_start);
1478 ins->objectid = aligned;
1479 if (aligned >= search_end) {
1480 ret = -ENOSPC;
1481 goto error;
1482 }
1483 ins->offset = search_end - aligned;
Chris Masonfec577f2007-02-26 10:40:21 -05001484 start_found = 1;
1485 goto check_pending;
1486 }
Chris Mason87ee04e2007-11-30 11:30:34 -05001487 ins->objectid = stripe_align(root,
1488 last_byte > search_start ?
1489 last_byte : search_start);
1490 if (search_end <= ins->objectid) {
1491 ret = -ENOSPC;
1492 goto error;
1493 }
Chris Mason3e1ad542007-05-07 20:03:49 -04001494 ins->offset = search_end - ins->objectid;
Chris Masone19caa52007-10-15 16:17:44 -04001495 BUG_ON(ins->objectid >= search_end);
Chris Masonfec577f2007-02-26 10:40:21 -05001496 goto check_pending;
1497 }
Chris Mason5f39d392007-10-15 16:14:19 -04001498 btrfs_item_key_to_cpu(l, &key, slot);
Chris Mason96b51792007-10-15 16:15:19 -04001499
Chris Masondb945352007-10-15 16:15:53 -04001500 if (key.objectid >= search_start && key.objectid > last_byte &&
Chris Masone37c9e62007-05-09 20:13:14 -04001501 start_found) {
Chris Masondb945352007-10-15 16:15:53 -04001502 if (last_byte < search_start)
1503 last_byte = search_start;
Chris Mason87ee04e2007-11-30 11:30:34 -05001504 aligned = stripe_align(root, last_byte);
1505 hole_size = key.objectid - aligned;
1506 if (key.objectid > aligned && hole_size >= num_bytes) {
1507 ins->objectid = aligned;
Chris Masone37c9e62007-05-09 20:13:14 -04001508 ins->offset = hole_size;
1509 goto check_pending;
Chris Mason0579da42007-03-07 16:15:30 -05001510 }
Chris Masonfec577f2007-02-26 10:40:21 -05001511 }
Chris Mason96b51792007-10-15 16:15:19 -04001512 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY) {
Chris Mason7bb86312007-12-11 09:25:06 -05001513 if (!start_found && btrfs_key_type(&key) ==
1514 BTRFS_BLOCK_GROUP_ITEM_KEY) {
Chris Masondb945352007-10-15 16:15:53 -04001515 last_byte = key.objectid;
Chris Mason96b51792007-10-15 16:15:19 -04001516 start_found = 1;
1517 }
Chris Masone37c9e62007-05-09 20:13:14 -04001518 goto next;
Chris Mason96b51792007-10-15 16:15:19 -04001519 }
1520
Chris Masone37c9e62007-05-09 20:13:14 -04001521
Chris Mason0579da42007-03-07 16:15:30 -05001522 start_found = 1;
Chris Masondb945352007-10-15 16:15:53 -04001523 last_byte = key.objectid + key.offset;
Chris Masonf510cfe2007-10-15 16:14:48 -04001524
Chris Mason257d0ce2007-11-07 21:08:16 -05001525 if (!full_scan && data != BTRFS_BLOCK_GROUP_MIXED &&
1526 last_byte >= block_group->key.objectid +
Chris Masonbe744172007-05-06 10:15:01 -04001527 block_group->key.offset) {
1528 btrfs_release_path(root, path);
1529 search_start = block_group->key.objectid +
Chris Masone19caa52007-10-15 16:17:44 -04001530 block_group->key.offset;
Chris Masonbe744172007-05-06 10:15:01 -04001531 goto new_group;
1532 }
Chris Mason9078a3e2007-04-26 16:46:15 -04001533next:
Chris Mason5caf2a02007-04-02 11:20:42 -04001534 path->slots[0]++;
Chris Masonde428b62007-05-18 13:28:27 -04001535 cond_resched();
Chris Masonfec577f2007-02-26 10:40:21 -05001536 }
Chris Masonfec577f2007-02-26 10:40:21 -05001537check_pending:
1538 /* we have to make sure we didn't find an extent that has already
1539 * been allocated by the map tree or the original allocation
1540 */
Chris Mason5caf2a02007-04-02 11:20:42 -04001541 btrfs_release_path(root, path);
Chris Masonfec577f2007-02-26 10:40:21 -05001542 BUG_ON(ins->objectid < search_start);
Chris Masone37c9e62007-05-09 20:13:14 -04001543
Chris Masondb945352007-10-15 16:15:53 -04001544 if (ins->objectid + num_bytes >= search_end)
Chris Masoncf675822007-09-17 11:00:51 -04001545 goto enospc;
Chris Mason257d0ce2007-11-07 21:08:16 -05001546 if (!full_scan && data != BTRFS_BLOCK_GROUP_MIXED &&
Yan5cf66422007-11-16 14:57:09 -05001547 ins->objectid + num_bytes > block_group->
Chris Masone19caa52007-10-15 16:17:44 -04001548 key.objectid + block_group->key.offset) {
1549 search_start = block_group->key.objectid +
1550 block_group->key.offset;
1551 goto new_group;
1552 }
Chris Mason1a5bc162007-10-15 16:15:26 -04001553 if (test_range_bit(&info->extent_ins, ins->objectid,
Chris Masondb945352007-10-15 16:15:53 -04001554 ins->objectid + num_bytes -1, EXTENT_LOCKED, 0)) {
1555 search_start = ins->objectid + num_bytes;
Chris Mason1a5bc162007-10-15 16:15:26 -04001556 goto new_group;
1557 }
1558 if (test_range_bit(&info->pinned_extents, ins->objectid,
Chris Masondb945352007-10-15 16:15:53 -04001559 ins->objectid + num_bytes -1, EXTENT_DIRTY, 0)) {
1560 search_start = ins->objectid + num_bytes;
Chris Mason1a5bc162007-10-15 16:15:26 -04001561 goto new_group;
Chris Masonfec577f2007-02-26 10:40:21 -05001562 }
Chris Masondb945352007-10-15 16:15:53 -04001563 if (exclude_nr > 0 && (ins->objectid + num_bytes > exclude_start &&
Chris Masonf2654de2007-06-26 12:20:46 -04001564 ins->objectid < exclude_start + exclude_nr)) {
1565 search_start = exclude_start + exclude_nr;
1566 goto new_group;
1567 }
Chris Masone37c9e62007-05-09 20:13:14 -04001568 if (!data) {
Chris Mason5276aed2007-06-11 21:33:38 -04001569 block_group = btrfs_lookup_block_group(info, ins->objectid);
Chris Mason26b80032007-08-08 20:17:12 -04001570 if (block_group)
1571 trans->block_group = block_group;
Chris Masoncd1bc462007-04-27 10:08:34 -04001572 }
Chris Masondb945352007-10-15 16:15:53 -04001573 ins->offset = num_bytes;
Chris Mason5caf2a02007-04-02 11:20:42 -04001574 btrfs_free_path(path);
Chris Masonfec577f2007-02-26 10:40:21 -05001575 return 0;
Chris Masonbe744172007-05-06 10:15:01 -04001576
1577new_group:
Chris Masondb945352007-10-15 16:15:53 -04001578 if (search_start + num_bytes >= search_end) {
Chris Masoncf675822007-09-17 11:00:51 -04001579enospc:
Chris Masonbe744172007-05-06 10:15:01 -04001580 search_start = orig_search_start;
Chris Masonfbdc7622007-05-30 10:22:12 -04001581 if (full_scan) {
1582 ret = -ENOSPC;
1583 goto error;
1584 }
Chris Mason6702ed42007-08-07 16:15:09 -04001585 if (wrapped) {
1586 if (!full_scan)
1587 total_needed -= empty_size;
Chris Masonfbdc7622007-05-30 10:22:12 -04001588 full_scan = 1;
Chris Mason1a2b2ac2007-12-04 13:18:24 -05001589 data = BTRFS_BLOCK_GROUP_MIXED;
Chris Mason6702ed42007-08-07 16:15:09 -04001590 } else
Chris Masonfbdc7622007-05-30 10:22:12 -04001591 wrapped = 1;
Chris Masonbe744172007-05-06 10:15:01 -04001592 }
Chris Mason5276aed2007-06-11 21:33:38 -04001593 block_group = btrfs_lookup_block_group(info, search_start);
Chris Masonfbdc7622007-05-30 10:22:12 -04001594 cond_resched();
Chris Mason1a2b2ac2007-12-04 13:18:24 -05001595 block_group = btrfs_find_block_group(root, block_group,
1596 search_start, data, 0);
Chris Masonbe744172007-05-06 10:15:01 -04001597 goto check_failed;
1598
Chris Mason0f70abe2007-02-28 16:46:22 -05001599error:
Chris Mason5caf2a02007-04-02 11:20:42 -04001600 btrfs_release_path(root, path);
1601 btrfs_free_path(path);
Chris Mason0f70abe2007-02-28 16:46:22 -05001602 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05001603}
Chris Masonfec577f2007-02-26 10:40:21 -05001604/*
Chris Masonfec577f2007-02-26 10:40:21 -05001605 * finds a free extent and does all the dirty work required for allocation
1606 * returns the key for the extent through ins, and a tree buffer for
1607 * the first block of the extent through buf.
1608 *
1609 * returns 0 if everything worked, non-zero otherwise.
1610 */
Chris Mason4d775672007-04-20 20:23:12 -04001611int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
Chris Mason7bb86312007-12-11 09:25:06 -05001612 struct btrfs_root *root,
1613 u64 num_bytes, u64 root_objectid, u64 ref_generation,
1614 u64 owner, u64 owner_offset,
1615 u64 empty_size, u64 hint_byte,
Chris Masonbe08c1b2007-05-03 09:06:49 -04001616 u64 search_end, struct btrfs_key *ins, int data)
Chris Masonfec577f2007-02-26 10:40:21 -05001617{
1618 int ret;
1619 int pending_ret;
Chris Masondb945352007-10-15 16:15:53 -04001620 u64 super_used, root_used;
Chris Masonfbdc7622007-05-30 10:22:12 -04001621 u64 search_start = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05001622 u64 new_hint;
Chris Mason1261ec42007-03-20 20:35:03 -04001623 struct btrfs_fs_info *info = root->fs_info;
1624 struct btrfs_root *extent_root = info->extent_root;
Chris Mason234b63a2007-03-13 10:46:10 -04001625 struct btrfs_extent_item extent_item;
Chris Mason7bb86312007-12-11 09:25:06 -05001626 struct btrfs_path *path;
Chris Mason037e6392007-03-07 11:50:24 -05001627
Chris Mason5f39d392007-10-15 16:14:19 -04001628 btrfs_set_stack_extent_refs(&extent_item, 1);
Chris Mason8f662a72008-01-02 10:01:11 -05001629
1630 new_hint = max(hint_byte, root->fs_info->alloc_start);
Chris Masonedbd8d42007-12-21 16:27:24 -05001631 if (new_hint < btrfs_super_total_bytes(&info->super_copy))
1632 hint_byte = new_hint;
Chris Mason8f662a72008-01-02 10:01:11 -05001633
Chris Masondb945352007-10-15 16:15:53 -04001634 WARN_ON(num_bytes < root->sectorsize);
1635 ret = find_free_extent(trans, root, num_bytes, empty_size,
1636 search_start, search_end, hint_byte, ins,
Chris Mason26b80032007-08-08 20:17:12 -04001637 trans->alloc_exclude_start,
1638 trans->alloc_exclude_nr, data);
Chris Masonccd467d2007-06-28 15:57:36 -04001639 BUG_ON(ret);
Chris Masonf2654de2007-06-26 12:20:46 -04001640 if (ret)
1641 return ret;
1642
Josef Bacik58176a92007-08-29 15:47:34 -04001643 /* block accounting for super block */
Chris Masondb945352007-10-15 16:15:53 -04001644 super_used = btrfs_super_bytes_used(&info->super_copy);
1645 btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes);
Chris Mason26b80032007-08-08 20:17:12 -04001646
Josef Bacik58176a92007-08-29 15:47:34 -04001647 /* block accounting for root item */
Chris Masondb945352007-10-15 16:15:53 -04001648 root_used = btrfs_root_used(&root->root_item);
1649 btrfs_set_root_used(&root->root_item, root_used + num_bytes);
Josef Bacik58176a92007-08-29 15:47:34 -04001650
Chris Masonf510cfe2007-10-15 16:14:48 -04001651 clear_extent_dirty(&root->fs_info->free_space_cache,
1652 ins->objectid, ins->objectid + ins->offset - 1,
1653 GFP_NOFS);
1654
Chris Mason26b80032007-08-08 20:17:12 -04001655 if (root == extent_root) {
Chris Mason1a5bc162007-10-15 16:15:26 -04001656 set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
1657 ins->objectid + ins->offset - 1,
1658 EXTENT_LOCKED, GFP_NOFS);
Chris Masone19caa52007-10-15 16:17:44 -04001659 WARN_ON(data == 1);
Chris Mason26b80032007-08-08 20:17:12 -04001660 goto update_block;
1661 }
1662
1663 WARN_ON(trans->alloc_exclude_nr);
1664 trans->alloc_exclude_start = ins->objectid;
1665 trans->alloc_exclude_nr = ins->offset;
Chris Masone089f052007-03-16 16:20:31 -04001666 ret = btrfs_insert_item(trans, extent_root, ins, &extent_item,
1667 sizeof(extent_item));
Chris Mason037e6392007-03-07 11:50:24 -05001668
Chris Mason26b80032007-08-08 20:17:12 -04001669 trans->alloc_exclude_start = 0;
1670 trans->alloc_exclude_nr = 0;
Chris Mason7bb86312007-12-11 09:25:06 -05001671 BUG_ON(ret);
1672
1673 path = btrfs_alloc_path();
1674 BUG_ON(!path);
1675 ret = btrfs_insert_extent_backref(trans, extent_root, path,
1676 ins->objectid, root_objectid,
1677 ref_generation, owner, owner_offset);
Chris Mason26b80032007-08-08 20:17:12 -04001678
Chris Masonccd467d2007-06-28 15:57:36 -04001679 BUG_ON(ret);
Chris Mason7bb86312007-12-11 09:25:06 -05001680 btrfs_free_path(path);
Chris Masone089f052007-03-16 16:20:31 -04001681 finish_current_insert(trans, extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -04001682 pending_ret = del_pending_extents(trans, extent_root);
Chris Masonf510cfe2007-10-15 16:14:48 -04001683
Chris Masone37c9e62007-05-09 20:13:14 -04001684 if (ret) {
Chris Mason037e6392007-03-07 11:50:24 -05001685 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -04001686 }
1687 if (pending_ret) {
Chris Mason037e6392007-03-07 11:50:24 -05001688 return pending_ret;
Chris Masone37c9e62007-05-09 20:13:14 -04001689 }
Chris Mason26b80032007-08-08 20:17:12 -04001690
1691update_block:
Chris Mason1e2677e2007-05-29 16:52:18 -04001692 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0,
1693 data);
Chris Masonfabb5682007-06-07 22:13:21 -04001694 BUG_ON(ret);
Chris Mason037e6392007-03-07 11:50:24 -05001695 return 0;
Chris Masonfec577f2007-02-26 10:40:21 -05001696}
1697
1698/*
1699 * helper function to allocate a block for a given tree
1700 * returns the tree buffer or NULL.
1701 */
Chris Mason5f39d392007-10-15 16:14:19 -04001702struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Chris Masondb945352007-10-15 16:15:53 -04001703 struct btrfs_root *root,
Chris Mason7bb86312007-12-11 09:25:06 -05001704 u32 blocksize,
1705 u64 root_objectid, u64 hint,
1706 u64 empty_size)
1707{
1708 u64 ref_generation;
1709
1710 if (root->ref_cows)
1711 ref_generation = trans->transid;
1712 else
1713 ref_generation = 0;
1714
1715
1716 return __btrfs_alloc_free_block(trans, root, blocksize, root_objectid,
1717 ref_generation, 0, 0, hint, empty_size);
1718}
1719
1720/*
1721 * helper function to allocate a block for a given tree
1722 * returns the tree buffer or NULL.
1723 */
1724struct extent_buffer *__btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
1725 struct btrfs_root *root,
1726 u32 blocksize,
1727 u64 root_objectid,
1728 u64 ref_generation,
1729 u64 first_objectid,
1730 int level,
1731 u64 hint,
Chris Mason5f39d392007-10-15 16:14:19 -04001732 u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05001733{
Chris Masone2fa7222007-03-12 16:22:34 -04001734 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -05001735 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04001736 struct extent_buffer *buf;
Chris Masonfec577f2007-02-26 10:40:21 -05001737
Chris Mason7bb86312007-12-11 09:25:06 -05001738 ret = btrfs_alloc_extent(trans, root, blocksize,
1739 root_objectid, ref_generation,
Chris Masonf6dbff52007-12-13 11:13:32 -05001740 level, first_objectid, empty_size, hint,
Chris Masondb945352007-10-15 16:15:53 -04001741 (u64)-1, &ins, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05001742 if (ret) {
Chris Mason54aa1f42007-06-22 14:16:25 -04001743 BUG_ON(ret > 0);
1744 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05001745 }
Chris Masondb945352007-10-15 16:15:53 -04001746 buf = btrfs_find_create_tree_block(root, ins.objectid, blocksize);
Chris Mason54aa1f42007-06-22 14:16:25 -04001747 if (!buf) {
Chris Mason7bb86312007-12-11 09:25:06 -05001748 btrfs_free_extent(trans, root, ins.objectid, blocksize,
1749 root->root_key.objectid, ref_generation,
1750 0, 0, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001751 return ERR_PTR(-ENOMEM);
1752 }
Chris Mason5f39d392007-10-15 16:14:19 -04001753 btrfs_set_buffer_uptodate(buf);
1754 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
1755 buf->start + buf->len - 1, GFP_NOFS);
Chris Mason19c00dd2007-10-15 16:19:22 -04001756 set_extent_bits(&BTRFS_I(root->fs_info->btree_inode)->extent_tree,
1757 buf->start, buf->start + buf->len - 1,
1758 EXTENT_CSUM, GFP_NOFS);
1759 buf->flags |= EXTENT_CSUM;
Chris Mason6b800532007-10-15 16:17:34 -04001760 btrfs_set_buffer_defrag(buf);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001761 trans->blocks_used++;
Chris Masonfec577f2007-02-26 10:40:21 -05001762 return buf;
1763}
Chris Masona28ec192007-03-06 20:08:01 -05001764
Chris Mason6407bf62007-03-27 06:33:00 -04001765static int drop_leaf_ref(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001766 struct btrfs_root *root, struct extent_buffer *leaf)
Chris Mason6407bf62007-03-27 06:33:00 -04001767{
Chris Mason7bb86312007-12-11 09:25:06 -05001768 u64 leaf_owner;
1769 u64 leaf_generation;
Chris Mason5f39d392007-10-15 16:14:19 -04001770 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -04001771 struct btrfs_file_extent_item *fi;
1772 int i;
1773 int nritems;
1774 int ret;
1775
Chris Mason5f39d392007-10-15 16:14:19 -04001776 BUG_ON(!btrfs_is_leaf(leaf));
1777 nritems = btrfs_header_nritems(leaf);
Chris Mason7bb86312007-12-11 09:25:06 -05001778 leaf_owner = btrfs_header_owner(leaf);
1779 leaf_generation = btrfs_header_generation(leaf);
1780
Chris Mason6407bf62007-03-27 06:33:00 -04001781 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04001782 u64 disk_bytenr;
Chris Mason5f39d392007-10-15 16:14:19 -04001783
1784 btrfs_item_key_to_cpu(leaf, &key, i);
1785 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason6407bf62007-03-27 06:33:00 -04001786 continue;
1787 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001788 if (btrfs_file_extent_type(leaf, fi) ==
1789 BTRFS_FILE_EXTENT_INLINE)
Chris Mason236454d2007-04-19 13:37:44 -04001790 continue;
Chris Mason6407bf62007-03-27 06:33:00 -04001791 /*
1792 * FIXME make sure to insert a trans record that
1793 * repeats the snapshot del on crash
1794 */
Chris Masondb945352007-10-15 16:15:53 -04001795 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
1796 if (disk_bytenr == 0)
Chris Mason3a686372007-05-24 13:35:57 -04001797 continue;
Chris Masondb945352007-10-15 16:15:53 -04001798 ret = btrfs_free_extent(trans, root, disk_bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -05001799 btrfs_file_extent_disk_num_bytes(leaf, fi),
1800 leaf_owner, leaf_generation,
1801 key.objectid, key.offset, 0);
Chris Mason6407bf62007-03-27 06:33:00 -04001802 BUG_ON(ret);
1803 }
1804 return 0;
1805}
1806
Chris Masone0115992007-06-19 16:23:05 -04001807static void reada_walk_down(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -04001808 struct extent_buffer *node)
Chris Masone0115992007-06-19 16:23:05 -04001809{
1810 int i;
1811 u32 nritems;
Chris Masondb945352007-10-15 16:15:53 -04001812 u64 bytenr;
Chris Masone0115992007-06-19 16:23:05 -04001813 int ret;
1814 u32 refs;
Chris Masondb945352007-10-15 16:15:53 -04001815 int level;
1816 u32 blocksize;
Chris Masone0115992007-06-19 16:23:05 -04001817
Chris Mason5f39d392007-10-15 16:14:19 -04001818 nritems = btrfs_header_nritems(node);
Chris Masondb945352007-10-15 16:15:53 -04001819 level = btrfs_header_level(node);
Chris Masone0115992007-06-19 16:23:05 -04001820 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04001821 bytenr = btrfs_node_blockptr(node, i);
1822 blocksize = btrfs_level_size(root, level - 1);
1823 ret = lookup_extent_ref(NULL, root, bytenr, blocksize, &refs);
Chris Masone0115992007-06-19 16:23:05 -04001824 BUG_ON(ret);
1825 if (refs != 1)
1826 continue;
Chris Mason409eb952007-08-08 20:17:12 -04001827 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masondb945352007-10-15 16:15:53 -04001828 ret = readahead_tree_block(root, bytenr, blocksize);
Chris Mason409eb952007-08-08 20:17:12 -04001829 cond_resched();
1830 mutex_lock(&root->fs_info->fs_mutex);
Chris Masone0115992007-06-19 16:23:05 -04001831 if (ret)
1832 break;
1833 }
1834}
1835
Chris Mason9aca1d52007-03-13 11:09:37 -04001836/*
1837 * helper function for drop_snapshot, this walks down the tree dropping ref
1838 * counts as it goes.
1839 */
Chris Masone089f052007-03-16 16:20:31 -04001840static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
1841 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05001842{
Chris Mason7bb86312007-12-11 09:25:06 -05001843 u64 root_owner;
1844 u64 root_gen;
1845 u64 bytenr;
Chris Mason5f39d392007-10-15 16:14:19 -04001846 struct extent_buffer *next;
1847 struct extent_buffer *cur;
Chris Mason7bb86312007-12-11 09:25:06 -05001848 struct extent_buffer *parent;
Chris Masondb945352007-10-15 16:15:53 -04001849 u32 blocksize;
Chris Mason20524f02007-03-10 06:35:47 -05001850 int ret;
1851 u32 refs;
1852
Chris Mason5caf2a02007-04-02 11:20:42 -04001853 WARN_ON(*level < 0);
1854 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason5f39d392007-10-15 16:14:19 -04001855 ret = lookup_extent_ref(trans, root,
Chris Masondb945352007-10-15 16:15:53 -04001856 path->nodes[*level]->start,
1857 path->nodes[*level]->len, &refs);
Chris Mason20524f02007-03-10 06:35:47 -05001858 BUG_ON(ret);
1859 if (refs > 1)
1860 goto out;
Chris Masone0115992007-06-19 16:23:05 -04001861
Chris Mason9aca1d52007-03-13 11:09:37 -04001862 /*
1863 * walk down to the last node level and free all the leaves
1864 */
Chris Mason6407bf62007-03-27 06:33:00 -04001865 while(*level >= 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001866 WARN_ON(*level < 0);
1867 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason20524f02007-03-10 06:35:47 -05001868 cur = path->nodes[*level];
Chris Masone0115992007-06-19 16:23:05 -04001869
1870 if (*level > 0 && path->slots[*level] == 0)
Chris Mason5f39d392007-10-15 16:14:19 -04001871 reada_walk_down(root, cur);
Chris Masone0115992007-06-19 16:23:05 -04001872
Chris Mason5f39d392007-10-15 16:14:19 -04001873 if (btrfs_header_level(cur) != *level)
Chris Mason2c90e5d2007-04-02 10:50:19 -04001874 WARN_ON(1);
Chris Masone0115992007-06-19 16:23:05 -04001875
Chris Mason7518a232007-03-12 12:01:18 -04001876 if (path->slots[*level] >=
Chris Mason5f39d392007-10-15 16:14:19 -04001877 btrfs_header_nritems(cur))
Chris Mason20524f02007-03-10 06:35:47 -05001878 break;
Chris Mason6407bf62007-03-27 06:33:00 -04001879 if (*level == 0) {
1880 ret = drop_leaf_ref(trans, root, cur);
1881 BUG_ON(ret);
1882 break;
1883 }
Chris Masondb945352007-10-15 16:15:53 -04001884 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
1885 blocksize = btrfs_level_size(root, *level - 1);
1886 ret = lookup_extent_ref(trans, root, bytenr, blocksize, &refs);
Chris Mason6407bf62007-03-27 06:33:00 -04001887 BUG_ON(ret);
1888 if (refs != 1) {
Chris Mason7bb86312007-12-11 09:25:06 -05001889 parent = path->nodes[*level];
1890 root_owner = btrfs_header_owner(parent);
1891 root_gen = btrfs_header_generation(parent);
Chris Mason20524f02007-03-10 06:35:47 -05001892 path->slots[*level]++;
Chris Masondb945352007-10-15 16:15:53 -04001893 ret = btrfs_free_extent(trans, root, bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -05001894 blocksize, root_owner,
1895 root_gen, 0, 0, 1);
Chris Mason20524f02007-03-10 06:35:47 -05001896 BUG_ON(ret);
1897 continue;
1898 }
Chris Masondb945352007-10-15 16:15:53 -04001899 next = btrfs_find_tree_block(root, bytenr, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -04001900 if (!next || !btrfs_buffer_uptodate(next)) {
1901 free_extent_buffer(next);
Chris Masone9d0b132007-08-10 14:06:19 -04001902 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masondb945352007-10-15 16:15:53 -04001903 next = read_tree_block(root, bytenr, blocksize);
Chris Masone9d0b132007-08-10 14:06:19 -04001904 mutex_lock(&root->fs_info->fs_mutex);
1905
1906 /* we dropped the lock, check one more time */
Chris Masondb945352007-10-15 16:15:53 -04001907 ret = lookup_extent_ref(trans, root, bytenr,
1908 blocksize, &refs);
Chris Masone9d0b132007-08-10 14:06:19 -04001909 BUG_ON(ret);
1910 if (refs != 1) {
Chris Mason7bb86312007-12-11 09:25:06 -05001911 parent = path->nodes[*level];
1912 root_owner = btrfs_header_owner(parent);
1913 root_gen = btrfs_header_generation(parent);
1914
Chris Masone9d0b132007-08-10 14:06:19 -04001915 path->slots[*level]++;
Chris Mason5f39d392007-10-15 16:14:19 -04001916 free_extent_buffer(next);
Chris Mason7bb86312007-12-11 09:25:06 -05001917 ret = btrfs_free_extent(trans, root, bytenr,
1918 blocksize,
1919 root_owner,
1920 root_gen, 0, 0, 1);
Chris Masone9d0b132007-08-10 14:06:19 -04001921 BUG_ON(ret);
1922 continue;
1923 }
1924 }
Chris Mason5caf2a02007-04-02 11:20:42 -04001925 WARN_ON(*level <= 0);
Chris Mason83e15a22007-03-12 09:03:27 -04001926 if (path->nodes[*level-1])
Chris Mason5f39d392007-10-15 16:14:19 -04001927 free_extent_buffer(path->nodes[*level-1]);
Chris Mason20524f02007-03-10 06:35:47 -05001928 path->nodes[*level-1] = next;
Chris Mason5f39d392007-10-15 16:14:19 -04001929 *level = btrfs_header_level(next);
Chris Mason20524f02007-03-10 06:35:47 -05001930 path->slots[*level] = 0;
1931 }
1932out:
Chris Mason5caf2a02007-04-02 11:20:42 -04001933 WARN_ON(*level < 0);
1934 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason7bb86312007-12-11 09:25:06 -05001935
1936 if (path->nodes[*level] == root->node) {
1937 root_owner = root->root_key.objectid;
1938 parent = path->nodes[*level];
1939 } else {
1940 parent = path->nodes[*level + 1];
1941 root_owner = btrfs_header_owner(parent);
1942 }
1943
1944 root_gen = btrfs_header_generation(parent);
Chris Masondb945352007-10-15 16:15:53 -04001945 ret = btrfs_free_extent(trans, root, path->nodes[*level]->start,
Chris Mason7bb86312007-12-11 09:25:06 -05001946 path->nodes[*level]->len,
1947 root_owner, root_gen, 0, 0, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001948 free_extent_buffer(path->nodes[*level]);
Chris Mason20524f02007-03-10 06:35:47 -05001949 path->nodes[*level] = NULL;
1950 *level += 1;
1951 BUG_ON(ret);
1952 return 0;
1953}
1954
Chris Mason9aca1d52007-03-13 11:09:37 -04001955/*
1956 * helper for dropping snapshots. This walks back up the tree in the path
1957 * to find the first node higher up where we haven't yet gone through
1958 * all the slots
1959 */
Chris Masone089f052007-03-16 16:20:31 -04001960static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root
1961 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05001962{
Chris Mason7bb86312007-12-11 09:25:06 -05001963 u64 root_owner;
1964 u64 root_gen;
1965 struct btrfs_root_item *root_item = &root->root_item;
Chris Mason20524f02007-03-10 06:35:47 -05001966 int i;
1967 int slot;
1968 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04001969
Chris Mason234b63a2007-03-13 10:46:10 -04001970 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Mason20524f02007-03-10 06:35:47 -05001971 slot = path->slots[i];
Chris Mason5f39d392007-10-15 16:14:19 -04001972 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
1973 struct extent_buffer *node;
1974 struct btrfs_disk_key disk_key;
1975 node = path->nodes[i];
Chris Mason20524f02007-03-10 06:35:47 -05001976 path->slots[i]++;
1977 *level = i;
Chris Mason9f3a7422007-08-07 15:52:19 -04001978 WARN_ON(*level == 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001979 btrfs_node_key(node, &disk_key, path->slots[i]);
Chris Mason9f3a7422007-08-07 15:52:19 -04001980 memcpy(&root_item->drop_progress,
Chris Mason5f39d392007-10-15 16:14:19 -04001981 &disk_key, sizeof(disk_key));
Chris Mason9f3a7422007-08-07 15:52:19 -04001982 root_item->drop_level = i;
Chris Mason20524f02007-03-10 06:35:47 -05001983 return 0;
1984 } else {
Chris Mason7bb86312007-12-11 09:25:06 -05001985 if (path->nodes[*level] == root->node) {
1986 root_owner = root->root_key.objectid;
1987 root_gen =
1988 btrfs_header_generation(path->nodes[*level]);
1989 } else {
1990 struct extent_buffer *node;
1991 node = path->nodes[*level + 1];
1992 root_owner = btrfs_header_owner(node);
1993 root_gen = btrfs_header_generation(node);
1994 }
Chris Masone089f052007-03-16 16:20:31 -04001995 ret = btrfs_free_extent(trans, root,
Chris Masondb945352007-10-15 16:15:53 -04001996 path->nodes[*level]->start,
Chris Mason7bb86312007-12-11 09:25:06 -05001997 path->nodes[*level]->len,
1998 root_owner, root_gen, 0, 0, 1);
Chris Mason6407bf62007-03-27 06:33:00 -04001999 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04002000 free_extent_buffer(path->nodes[*level]);
Chris Mason83e15a22007-03-12 09:03:27 -04002001 path->nodes[*level] = NULL;
Chris Mason20524f02007-03-10 06:35:47 -05002002 *level = i + 1;
Chris Mason20524f02007-03-10 06:35:47 -05002003 }
2004 }
2005 return 1;
2006}
2007
Chris Mason9aca1d52007-03-13 11:09:37 -04002008/*
2009 * drop the reference count on the tree rooted at 'snap'. This traverses
2010 * the tree freeing any blocks that have a ref count of zero after being
2011 * decremented.
2012 */
Chris Masone089f052007-03-16 16:20:31 -04002013int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason9f3a7422007-08-07 15:52:19 -04002014 *root)
Chris Mason20524f02007-03-10 06:35:47 -05002015{
Chris Mason3768f362007-03-13 16:47:54 -04002016 int ret = 0;
Chris Mason9aca1d52007-03-13 11:09:37 -04002017 int wret;
Chris Mason20524f02007-03-10 06:35:47 -05002018 int level;
Chris Mason5caf2a02007-04-02 11:20:42 -04002019 struct btrfs_path *path;
Chris Mason20524f02007-03-10 06:35:47 -05002020 int i;
2021 int orig_level;
Chris Mason9f3a7422007-08-07 15:52:19 -04002022 struct btrfs_root_item *root_item = &root->root_item;
Chris Mason20524f02007-03-10 06:35:47 -05002023
Chris Mason5caf2a02007-04-02 11:20:42 -04002024 path = btrfs_alloc_path();
2025 BUG_ON(!path);
Chris Mason20524f02007-03-10 06:35:47 -05002026
Chris Mason5f39d392007-10-15 16:14:19 -04002027 level = btrfs_header_level(root->node);
Chris Mason20524f02007-03-10 06:35:47 -05002028 orig_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04002029 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
2030 path->nodes[level] = root->node;
Chris Masonf510cfe2007-10-15 16:14:48 -04002031 extent_buffer_get(root->node);
Chris Mason9f3a7422007-08-07 15:52:19 -04002032 path->slots[level] = 0;
2033 } else {
2034 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04002035 struct btrfs_disk_key found_key;
2036 struct extent_buffer *node;
Chris Mason6702ed42007-08-07 16:15:09 -04002037
Chris Mason9f3a7422007-08-07 15:52:19 -04002038 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Chris Mason6702ed42007-08-07 16:15:09 -04002039 level = root_item->drop_level;
2040 path->lowest_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04002041 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason6702ed42007-08-07 16:15:09 -04002042 if (wret < 0) {
Chris Mason9f3a7422007-08-07 15:52:19 -04002043 ret = wret;
2044 goto out;
2045 }
Chris Mason5f39d392007-10-15 16:14:19 -04002046 node = path->nodes[level];
2047 btrfs_node_key(node, &found_key, path->slots[level]);
2048 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
2049 sizeof(found_key)));
Chris Mason9f3a7422007-08-07 15:52:19 -04002050 }
Chris Mason20524f02007-03-10 06:35:47 -05002051 while(1) {
Chris Mason5caf2a02007-04-02 11:20:42 -04002052 wret = walk_down_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04002053 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05002054 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04002055 if (wret < 0)
2056 ret = wret;
2057
Chris Mason5caf2a02007-04-02 11:20:42 -04002058 wret = walk_up_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04002059 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05002060 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04002061 if (wret < 0)
2062 ret = wret;
Chris Mason409eb952007-08-08 20:17:12 -04002063 ret = -EAGAIN;
Chris Mason409eb952007-08-08 20:17:12 -04002064 break;
Chris Mason20524f02007-03-10 06:35:47 -05002065 }
Chris Mason83e15a22007-03-12 09:03:27 -04002066 for (i = 0; i <= orig_level; i++) {
Chris Mason5caf2a02007-04-02 11:20:42 -04002067 if (path->nodes[i]) {
Chris Mason5f39d392007-10-15 16:14:19 -04002068 free_extent_buffer(path->nodes[i]);
Chris Mason0f827312007-10-15 16:18:56 -04002069 path->nodes[i] = NULL;
Chris Mason83e15a22007-03-12 09:03:27 -04002070 }
Chris Mason20524f02007-03-10 06:35:47 -05002071 }
Chris Mason9f3a7422007-08-07 15:52:19 -04002072out:
Chris Mason5caf2a02007-04-02 11:20:42 -04002073 btrfs_free_path(path);
Chris Mason9aca1d52007-03-13 11:09:37 -04002074 return ret;
Chris Mason20524f02007-03-10 06:35:47 -05002075}
Chris Mason9078a3e2007-04-26 16:46:15 -04002076
Chris Masonbe744172007-05-06 10:15:01 -04002077int btrfs_free_block_groups(struct btrfs_fs_info *info)
2078{
Chris Masonf510cfe2007-10-15 16:14:48 -04002079 u64 start;
2080 u64 end;
Yanb97f9202007-11-01 11:28:41 -04002081 u64 ptr;
Chris Mason96b51792007-10-15 16:15:19 -04002082 int ret;
Chris Mason96b51792007-10-15 16:15:19 -04002083 while(1) {
2084 ret = find_first_extent_bit(&info->block_group_cache, 0,
2085 &start, &end, (unsigned int)-1);
2086 if (ret)
2087 break;
Yanb97f9202007-11-01 11:28:41 -04002088 ret = get_state_private(&info->block_group_cache, start, &ptr);
2089 if (!ret)
2090 kfree((void *)(unsigned long)ptr);
Chris Mason96b51792007-10-15 16:15:19 -04002091 clear_extent_bits(&info->block_group_cache, start,
2092 end, (unsigned int)-1, GFP_NOFS);
2093 }
Chris Masone37c9e62007-05-09 20:13:14 -04002094 while(1) {
Chris Masonf510cfe2007-10-15 16:14:48 -04002095 ret = find_first_extent_bit(&info->free_space_cache, 0,
2096 &start, &end, EXTENT_DIRTY);
2097 if (ret)
Chris Masone37c9e62007-05-09 20:13:14 -04002098 break;
Chris Masonf510cfe2007-10-15 16:14:48 -04002099 clear_extent_dirty(&info->free_space_cache, start,
2100 end, GFP_NOFS);
Chris Masone37c9e62007-05-09 20:13:14 -04002101 }
Chris Masonbe744172007-05-06 10:15:01 -04002102 return 0;
2103}
2104
Chris Masonedbd8d42007-12-21 16:27:24 -05002105static int relocate_inode_pages(struct inode *inode, u64 start, u64 len)
2106{
2107 u64 page_start;
2108 u64 page_end;
2109 u64 delalloc_start;
2110 u64 existing_delalloc;
2111 unsigned long last_index;
2112 unsigned long first_index;
2113 unsigned long i;
2114 struct page *page;
2115 struct btrfs_root *root = BTRFS_I(inode)->root;
2116 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
2117 struct file_ra_state ra;
2118
2119 mutex_lock(&inode->i_mutex);
2120 first_index = start >> PAGE_CACHE_SHIFT;
2121 last_index = (start + len - 1) >> PAGE_CACHE_SHIFT;
2122
2123 memset(&ra, 0, sizeof(ra));
2124 file_ra_state_init(&ra, inode->i_mapping);
2125 btrfs_force_ra(inode->i_mapping, &ra, NULL, first_index, last_index);
2126
2127 for (i = first_index; i <= last_index; i++) {
2128 page = grab_cache_page(inode->i_mapping, i);
2129 if (!page)
2130 goto out_unlock;
2131 if (!PageUptodate(page)) {
2132 btrfs_readpage(NULL, page);
2133 lock_page(page);
2134 if (!PageUptodate(page)) {
2135 unlock_page(page);
2136 page_cache_release(page);
2137 goto out_unlock;
2138 }
2139 }
2140 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
2141 page_end = page_start + PAGE_CACHE_SIZE - 1;
2142
2143 lock_extent(em_tree, page_start, page_end, GFP_NOFS);
2144
2145 delalloc_start = page_start;
2146 existing_delalloc =
2147 count_range_bits(&BTRFS_I(inode)->extent_tree,
2148 &delalloc_start, page_end,
2149 PAGE_CACHE_SIZE, EXTENT_DELALLOC);
2150
2151 set_extent_delalloc(em_tree, page_start,
2152 page_end, GFP_NOFS);
2153
2154 spin_lock(&root->fs_info->delalloc_lock);
2155 root->fs_info->delalloc_bytes += PAGE_CACHE_SIZE -
2156 existing_delalloc;
2157 spin_unlock(&root->fs_info->delalloc_lock);
2158
2159 unlock_extent(em_tree, page_start, page_end, GFP_NOFS);
2160 set_page_dirty(page);
2161 unlock_page(page);
2162 page_cache_release(page);
2163 }
2164
2165out_unlock:
2166 mutex_unlock(&inode->i_mutex);
2167 return 0;
2168}
2169
2170static int relocate_one_reference(struct btrfs_root *extent_root,
2171 struct btrfs_path *path,
2172 struct btrfs_key *extent_key,
2173 u64 ref_root, u64 ref_gen, u64 ref_objectid,
2174 u64 ref_offset)
2175{
2176 struct inode *inode;
2177 struct btrfs_root *found_root;
2178 struct btrfs_key root_location;
2179 int ret;
2180
2181 root_location.objectid = ref_root;
2182 if (ref_gen == 0)
2183 root_location.offset = 0;
2184 else
2185 root_location.offset = (u64)-1;
2186 root_location.type = BTRFS_ROOT_ITEM_KEY;
2187
2188 found_root = btrfs_read_fs_root_no_name(extent_root->fs_info,
2189 &root_location);
2190 BUG_ON(!found_root);
2191
2192 if (ref_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
2193 mutex_unlock(&extent_root->fs_info->fs_mutex);
2194 inode = btrfs_iget_locked(extent_root->fs_info->sb,
2195 ref_objectid, found_root);
2196 if (inode->i_state & I_NEW) {
2197 /* the inode and parent dir are two different roots */
2198 BTRFS_I(inode)->root = found_root;
2199 BTRFS_I(inode)->location.objectid = ref_objectid;
2200 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
2201 BTRFS_I(inode)->location.offset = 0;
2202 btrfs_read_locked_inode(inode);
2203 unlock_new_inode(inode);
2204
2205 }
2206 /* this can happen if the reference is not against
2207 * the latest version of the tree root
2208 */
2209 if (is_bad_inode(inode)) {
2210 mutex_lock(&extent_root->fs_info->fs_mutex);
2211 goto out;
2212 }
2213 relocate_inode_pages(inode, ref_offset, extent_key->offset);
2214 /* FIXME, data=ordered will help get rid of this */
2215 filemap_fdatawrite(inode->i_mapping);
2216 iput(inode);
2217 mutex_lock(&extent_root->fs_info->fs_mutex);
2218 } else {
2219 struct btrfs_trans_handle *trans;
2220 struct btrfs_key found_key;
2221 struct extent_buffer *eb;
2222 int level;
2223 int i;
2224
2225 trans = btrfs_start_transaction(found_root, 1);
2226 eb = read_tree_block(found_root, extent_key->objectid,
2227 extent_key->offset);
2228 level = btrfs_header_level(eb);
2229
2230 if (level == 0)
2231 btrfs_item_key_to_cpu(eb, &found_key, 0);
2232 else
2233 btrfs_node_key_to_cpu(eb, &found_key, 0);
2234
2235 free_extent_buffer(eb);
2236
2237 path->lowest_level = level;
Chris Mason8f662a72008-01-02 10:01:11 -05002238 path->reada = 2;
Chris Masonedbd8d42007-12-21 16:27:24 -05002239 ret = btrfs_search_slot(trans, found_root, &found_key, path,
2240 0, 1);
2241 path->lowest_level = 0;
2242 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2243 if (!path->nodes[i])
2244 break;
2245 free_extent_buffer(path->nodes[i]);
2246 path->nodes[i] = NULL;
2247 }
2248 btrfs_release_path(found_root, path);
2249 btrfs_end_transaction(trans, found_root);
2250 }
2251
2252out:
2253 return 0;
2254}
2255
2256static int relocate_one_extent(struct btrfs_root *extent_root,
2257 struct btrfs_path *path,
2258 struct btrfs_key *extent_key)
2259{
2260 struct btrfs_key key;
2261 struct btrfs_key found_key;
2262 struct btrfs_extent_ref *ref;
2263 struct extent_buffer *leaf;
2264 u64 ref_root;
2265 u64 ref_gen;
2266 u64 ref_objectid;
2267 u64 ref_offset;
2268 u32 nritems;
2269 u32 item_size;
2270 int ret = 0;
2271
2272 key.objectid = extent_key->objectid;
2273 key.type = BTRFS_EXTENT_REF_KEY;
2274 key.offset = 0;
2275
2276 while(1) {
2277 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
2278
2279 BUG_ON(ret == 0);
2280
2281 if (ret < 0)
2282 goto out;
2283
2284 ret = 0;
2285 leaf = path->nodes[0];
2286 nritems = btrfs_header_nritems(leaf);
2287 if (path->slots[0] == nritems)
2288 goto out;
2289
2290 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2291 if (found_key.objectid != extent_key->objectid)
2292 break;
2293
2294 if (found_key.type != BTRFS_EXTENT_REF_KEY)
2295 break;
2296
2297 key.offset = found_key.offset + 1;
2298 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2299
2300 ref = btrfs_item_ptr(leaf, path->slots[0],
2301 struct btrfs_extent_ref);
2302 ref_root = btrfs_ref_root(leaf, ref);
2303 ref_gen = btrfs_ref_generation(leaf, ref);
2304 ref_objectid = btrfs_ref_objectid(leaf, ref);
2305 ref_offset = btrfs_ref_offset(leaf, ref);
2306 btrfs_release_path(extent_root, path);
2307
2308 ret = relocate_one_reference(extent_root, path,
2309 extent_key, ref_root, ref_gen,
2310 ref_objectid, ref_offset);
2311 if (ret)
2312 goto out;
2313 }
2314 ret = 0;
2315out:
2316 btrfs_release_path(extent_root, path);
2317 return ret;
2318}
2319
2320static int find_overlapping_extent(struct btrfs_root *root,
2321 struct btrfs_path *path, u64 new_size)
2322{
2323 struct btrfs_key found_key;
2324 struct extent_buffer *leaf;
2325 int ret;
2326
2327 while(1) {
2328 if (path->slots[0] == 0) {
2329 ret = btrfs_prev_leaf(root, path);
2330 if (ret == 1) {
2331 return 1;
2332 }
2333 if (ret < 0)
2334 return ret;
2335 } else {
2336 path->slots[0]--;
2337 }
2338 leaf = path->nodes[0];
2339 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2340 if (found_key.type == BTRFS_EXTENT_ITEM_KEY) {
2341 if (found_key.objectid + found_key.offset > new_size)
2342 return 0;
2343 else
2344 return 1;
2345 }
2346 }
2347 return 1;
2348}
2349
2350int btrfs_shrink_extent_tree(struct btrfs_root *root, u64 new_size)
2351{
2352 struct btrfs_trans_handle *trans;
2353 struct btrfs_root *tree_root = root->fs_info->tree_root;
2354 struct btrfs_path *path;
2355 u64 cur_byte;
2356 u64 total_found;
2357 u64 ptr;
2358 struct btrfs_fs_info *info = root->fs_info;
2359 struct extent_map_tree *block_group_cache;
2360 struct btrfs_key key;
2361 struct btrfs_key found_key = { 0, 0, 0 };
2362 struct extent_buffer *leaf;
2363 u32 nritems;
2364 int ret;
2365 int slot;
2366
2367 btrfs_set_super_total_bytes(&info->super_copy, new_size);
2368 block_group_cache = &info->block_group_cache;
2369 path = btrfs_alloc_path();
2370 root = root->fs_info->extent_root;
Chris Mason8f662a72008-01-02 10:01:11 -05002371 path->reada = 2;
Chris Masonedbd8d42007-12-21 16:27:24 -05002372
2373again:
2374 total_found = 0;
2375 key.objectid = new_size;
2376 cur_byte = key.objectid;
2377 key.offset = 0;
2378 key.type = 0;
2379 while(1) {
2380 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2381 if (ret < 0)
2382 goto out;
2383next:
2384 leaf = path->nodes[0];
2385 if (key.objectid == new_size - 1) {
2386 ret = find_overlapping_extent(root, path, new_size);
2387 if (ret != 0) {
2388 btrfs_release_path(root, path);
2389 ret = btrfs_search_slot(NULL, root, &key,
2390 path, 0, 0);
2391 if (ret < 0)
2392 goto out;
2393 }
2394 }
2395 nritems = btrfs_header_nritems(leaf);
2396 ret = 0;
2397 slot = path->slots[0];
2398 if (slot < nritems)
2399 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2400 if (slot == nritems ||
2401 btrfs_key_type(&found_key) != BTRFS_EXTENT_ITEM_KEY) {
2402 path->slots[0]++;
2403 if (path->slots[0] >= nritems) {
2404 ret = btrfs_next_leaf(root, path);
2405 if (ret < 0)
2406 goto out;
2407 if (ret == 1) {
2408 ret = 0;
2409 break;
2410 }
2411 }
2412 goto next;
2413 }
2414 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2415 if (found_key.objectid + found_key.offset <= cur_byte)
2416 continue;
2417 total_found++;
2418 cur_byte = found_key.objectid + found_key.offset;
2419 key.objectid = cur_byte;
2420 btrfs_release_path(root, path);
2421 ret = relocate_one_extent(root, path, &found_key);
2422 }
2423
2424 btrfs_release_path(root, path);
2425
2426 if (total_found > 0) {
2427 trans = btrfs_start_transaction(tree_root, 1);
2428 btrfs_commit_transaction(trans, tree_root);
2429
2430 mutex_unlock(&root->fs_info->fs_mutex);
2431 btrfs_clean_old_snapshots(tree_root);
2432 mutex_lock(&root->fs_info->fs_mutex);
2433
2434 trans = btrfs_start_transaction(tree_root, 1);
2435 btrfs_commit_transaction(trans, tree_root);
2436 goto again;
2437 }
2438
2439 trans = btrfs_start_transaction(root, 1);
2440 key.objectid = new_size;
2441 key.offset = 0;
2442 key.type = 0;
2443 while(1) {
2444 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2445 if (ret < 0)
2446 goto out;
2447bg_next:
2448 leaf = path->nodes[0];
2449 nritems = btrfs_header_nritems(leaf);
2450 ret = 0;
2451 slot = path->slots[0];
2452 if (slot < nritems)
2453 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2454 if (slot == nritems ||
2455 btrfs_key_type(&found_key) != BTRFS_BLOCK_GROUP_ITEM_KEY) {
2456 if (slot < nritems) {
2457 printk("shrinker found key %Lu %u %Lu\n",
2458 found_key.objectid, found_key.type,
2459 found_key.offset);
2460 path->slots[0]++;
2461 }
2462 if (path->slots[0] >= nritems) {
2463 ret = btrfs_next_leaf(root, path);
2464 if (ret < 0)
2465 break;
2466 if (ret == 1) {
2467 ret = 0;
2468 break;
2469 }
2470 }
2471 goto bg_next;
2472 }
2473 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2474 ret = get_state_private(&info->block_group_cache,
2475 found_key.objectid, &ptr);
2476 if (!ret)
2477 kfree((void *)(unsigned long)ptr);
2478
2479 clear_extent_bits(&info->block_group_cache, found_key.objectid,
2480 found_key.objectid + found_key.offset - 1,
2481 (unsigned int)-1, GFP_NOFS);
2482
2483 key.objectid = found_key.objectid + 1;
2484 btrfs_del_item(trans, root, path);
2485 btrfs_release_path(root, path);
2486 }
2487 clear_extent_dirty(&info->free_space_cache, new_size, (u64)-1,
2488 GFP_NOFS);
2489 btrfs_commit_transaction(trans, root);
2490out:
2491 btrfs_free_path(path);
2492 return ret;
2493}
2494
2495int btrfs_grow_extent_tree(struct btrfs_trans_handle *trans,
2496 struct btrfs_root *root, u64 new_size)
2497{
2498 struct btrfs_path *path;
2499 u64 nr = 0;
2500 u64 cur_byte;
2501 u64 old_size;
2502 struct btrfs_block_group_cache *cache;
2503 struct btrfs_block_group_item *item;
2504 struct btrfs_fs_info *info = root->fs_info;
2505 struct extent_map_tree *block_group_cache;
2506 struct btrfs_key key;
2507 struct extent_buffer *leaf;
2508 int ret;
2509 int bit;
2510
2511 old_size = btrfs_super_total_bytes(&info->super_copy);
2512 block_group_cache = &info->block_group_cache;
2513
2514 root = info->extent_root;
2515
2516 cache = btrfs_lookup_block_group(root->fs_info, old_size - 1);
2517
2518 cur_byte = cache->key.objectid + cache->key.offset;
2519 if (cur_byte >= new_size)
2520 goto set_size;
2521
2522 key.offset = BTRFS_BLOCK_GROUP_SIZE;
2523 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
2524
2525 path = btrfs_alloc_path();
2526 if (!path)
2527 return -ENOMEM;
2528
2529 while(cur_byte < new_size) {
2530 key.objectid = cur_byte;
2531 ret = btrfs_insert_empty_item(trans, root, path, &key,
2532 sizeof(struct btrfs_block_group_item));
2533 BUG_ON(ret);
2534 leaf = path->nodes[0];
2535 item = btrfs_item_ptr(leaf, path->slots[0],
2536 struct btrfs_block_group_item);
2537
2538 btrfs_set_disk_block_group_used(leaf, item, 0);
2539 if (nr % 3) {
2540 btrfs_set_disk_block_group_flags(leaf, item,
2541 BTRFS_BLOCK_GROUP_DATA);
2542 } else {
2543 btrfs_set_disk_block_group_flags(leaf, item, 0);
2544 }
2545 nr++;
2546
2547 cache = kmalloc(sizeof(*cache), GFP_NOFS);
2548 BUG_ON(!cache);
2549
2550 read_extent_buffer(leaf, &cache->item, (unsigned long)item,
2551 sizeof(cache->item));
2552
2553 memcpy(&cache->key, &key, sizeof(key));
2554 cache->cached = 0;
2555 cache->pinned = 0;
2556 cur_byte = key.objectid + key.offset;
2557 btrfs_release_path(root, path);
2558
2559 if (cache->item.flags & BTRFS_BLOCK_GROUP_DATA) {
2560 bit = BLOCK_GROUP_DATA;
2561 cache->data = BTRFS_BLOCK_GROUP_DATA;
2562 } else {
2563 bit = BLOCK_GROUP_METADATA;
2564 cache->data = 0;
2565 }
2566
2567 /* use EXTENT_LOCKED to prevent merging */
2568 set_extent_bits(block_group_cache, key.objectid,
2569 key.objectid + key.offset - 1,
2570 bit | EXTENT_LOCKED, GFP_NOFS);
2571 set_state_private(block_group_cache, key.objectid,
2572 (unsigned long)cache);
2573 }
2574 btrfs_free_path(path);
2575set_size:
2576 btrfs_set_super_total_bytes(&info->super_copy, new_size);
2577 return 0;
2578}
2579
Chris Mason9078a3e2007-04-26 16:46:15 -04002580int btrfs_read_block_groups(struct btrfs_root *root)
2581{
2582 struct btrfs_path *path;
2583 int ret;
2584 int err = 0;
Chris Mason96b51792007-10-15 16:15:19 -04002585 int bit;
Chris Mason9078a3e2007-04-26 16:46:15 -04002586 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04002587 struct btrfs_fs_info *info = root->fs_info;
Chris Mason96b51792007-10-15 16:15:19 -04002588 struct extent_map_tree *block_group_cache;
Chris Mason9078a3e2007-04-26 16:46:15 -04002589 struct btrfs_key key;
2590 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -04002591 struct extent_buffer *leaf;
Chris Mason96b51792007-10-15 16:15:19 -04002592
2593 block_group_cache = &info->block_group_cache;
Chris Mason9078a3e2007-04-26 16:46:15 -04002594
Chris Masonbe744172007-05-06 10:15:01 -04002595 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04002596 key.objectid = 0;
Chris Masondb945352007-10-15 16:15:53 -04002597 key.offset = BTRFS_BLOCK_GROUP_SIZE;
Chris Mason9078a3e2007-04-26 16:46:15 -04002598 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
2599
2600 path = btrfs_alloc_path();
2601 if (!path)
2602 return -ENOMEM;
2603
2604 while(1) {
Chris Masonbe744172007-05-06 10:15:01 -04002605 ret = btrfs_search_slot(NULL, info->extent_root,
Chris Mason9078a3e2007-04-26 16:46:15 -04002606 &key, path, 0, 0);
2607 if (ret != 0) {
2608 err = ret;
2609 break;
2610 }
Chris Mason5f39d392007-10-15 16:14:19 -04002611 leaf = path->nodes[0];
2612 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason9078a3e2007-04-26 16:46:15 -04002613 cache = kmalloc(sizeof(*cache), GFP_NOFS);
2614 if (!cache) {
2615 err = -1;
2616 break;
2617 }
Chris Mason3e1ad542007-05-07 20:03:49 -04002618
Chris Mason5f39d392007-10-15 16:14:19 -04002619 read_extent_buffer(leaf, &cache->item,
2620 btrfs_item_ptr_offset(leaf, path->slots[0]),
2621 sizeof(cache->item));
Chris Mason9078a3e2007-04-26 16:46:15 -04002622 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Masone37c9e62007-05-09 20:13:14 -04002623 cache->cached = 0;
Yan324ae4d2007-11-16 14:57:08 -05002624 cache->pinned = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04002625 key.objectid = found_key.objectid + found_key.offset;
2626 btrfs_release_path(root, path);
Chris Mason5f39d392007-10-15 16:14:19 -04002627
Chris Masonf84a8b32007-11-06 10:26:29 -05002628 if (cache->item.flags & BTRFS_BLOCK_GROUP_MIXED) {
2629 bit = BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA;
2630 cache->data = BTRFS_BLOCK_GROUP_MIXED;
2631 } else if (cache->item.flags & BTRFS_BLOCK_GROUP_DATA) {
Chris Mason96b51792007-10-15 16:15:19 -04002632 bit = BLOCK_GROUP_DATA;
Chris Masonf84a8b32007-11-06 10:26:29 -05002633 cache->data = BTRFS_BLOCK_GROUP_DATA;
Chris Mason96b51792007-10-15 16:15:19 -04002634 } else {
2635 bit = BLOCK_GROUP_METADATA;
2636 cache->data = 0;
Chris Mason31f3c992007-04-30 15:25:45 -04002637 }
Chris Mason96b51792007-10-15 16:15:19 -04002638
2639 /* use EXTENT_LOCKED to prevent merging */
2640 set_extent_bits(block_group_cache, found_key.objectid,
2641 found_key.objectid + found_key.offset - 1,
2642 bit | EXTENT_LOCKED, GFP_NOFS);
2643 set_state_private(block_group_cache, found_key.objectid,
Jens Axboeae2f5412007-10-19 09:22:59 -04002644 (unsigned long)cache);
Chris Mason96b51792007-10-15 16:15:19 -04002645
Chris Mason9078a3e2007-04-26 16:46:15 -04002646 if (key.objectid >=
Chris Masondb945352007-10-15 16:15:53 -04002647 btrfs_super_total_bytes(&info->super_copy))
Chris Mason9078a3e2007-04-26 16:46:15 -04002648 break;
2649 }
2650
2651 btrfs_free_path(path);
2652 return 0;
2653}