blob: 4957cface9abf39e42ba25146d12643c0adba594 [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 Mason98ed5172008-01-03 10:01:48 -0500165
166static u64 noinline find_search_start(struct btrfs_root *root,
167 struct btrfs_block_group_cache **cache_ret,
168 u64 search_start, int num,
169 int data, int full_scan)
Chris Masone37c9e62007-05-09 20:13:14 -0400170{
Chris Masone37c9e62007-05-09 20:13:14 -0400171 int ret;
172 struct btrfs_block_group_cache *cache = *cache_ret;
Chris Masone19caa52007-10-15 16:17:44 -0400173 u64 last;
Chris Masonf510cfe2007-10-15 16:14:48 -0400174 u64 start = 0;
175 u64 end = 0;
Chris Mason257d0ce2007-11-07 21:08:16 -0500176 u64 cache_miss = 0;
Chris Masonf84a8b32007-11-06 10:26:29 -0500177 int wrapped = 0;
Chris Masone37c9e62007-05-09 20:13:14 -0400178
Chris Mason00f5c792007-11-30 10:09:33 -0500179 if (!cache) {
Chris Mason1a2b2ac2007-12-04 13:18:24 -0500180 goto out;
Chris Mason00f5c792007-11-30 10:09:33 -0500181 }
Chris Masone37c9e62007-05-09 20:13:14 -0400182again:
Chris Mason54aa1f42007-06-22 14:16:25 -0400183 ret = cache_block_group(root, cache);
184 if (ret)
185 goto out;
Chris Masonf84a8b32007-11-06 10:26:29 -0500186
Chris Masone19caa52007-10-15 16:17:44 -0400187 last = max(search_start, cache->key.objectid);
188
Chris Masone37c9e62007-05-09 20:13:14 -0400189 while(1) {
Chris Masonf510cfe2007-10-15 16:14:48 -0400190 ret = find_first_extent_bit(&root->fs_info->free_space_cache,
191 last, &start, &end, EXTENT_DIRTY);
Chris Masone19caa52007-10-15 16:17:44 -0400192 if (ret) {
Chris Mason257d0ce2007-11-07 21:08:16 -0500193 if (!cache_miss)
194 cache_miss = last;
Chris Masone19caa52007-10-15 16:17:44 -0400195 goto new_group;
196 }
Chris Masonf510cfe2007-10-15 16:14:48 -0400197
198 start = max(last, start);
199 last = end + 1;
Chris Mason257d0ce2007-11-07 21:08:16 -0500200 if (last - start < num) {
201 if (last == cache->key.objectid + cache->key.offset)
202 cache_miss = start;
Chris Masonf510cfe2007-10-15 16:14:48 -0400203 continue;
Chris Mason257d0ce2007-11-07 21:08:16 -0500204 }
205 if (data != BTRFS_BLOCK_GROUP_MIXED &&
Yan5cf66422007-11-16 14:57:09 -0500206 start + num > cache->key.objectid + cache->key.offset)
Chris Masone37c9e62007-05-09 20:13:14 -0400207 goto new_group;
Chris Masonf510cfe2007-10-15 16:14:48 -0400208 return start;
Chris Masone37c9e62007-05-09 20:13:14 -0400209 }
210out:
Chris Mason1a2b2ac2007-12-04 13:18:24 -0500211 cache = btrfs_lookup_block_group(root->fs_info, search_start);
212 if (!cache) {
213 printk("Unable to find block group for %Lu\n",
214 search_start);
215 WARN_ON(1);
216 return search_start;
217 }
Chris Mason1a5bc162007-10-15 16:15:26 -0400218 return search_start;
Chris Masone37c9e62007-05-09 20:13:14 -0400219
220new_group:
Chris Masone19caa52007-10-15 16:17:44 -0400221 last = cache->key.objectid + cache->key.offset;
Chris Masonf84a8b32007-11-06 10:26:29 -0500222wrapped:
Chris Masone19caa52007-10-15 16:17:44 -0400223 cache = btrfs_lookup_block_group(root->fs_info, last);
Chris Masone37c9e62007-05-09 20:13:14 -0400224 if (!cache) {
Chris Mason0e4de582007-11-26 10:55:49 -0500225no_cache:
Chris Masonf84a8b32007-11-06 10:26:29 -0500226 if (!wrapped) {
227 wrapped = 1;
228 last = search_start;
229 data = BTRFS_BLOCK_GROUP_MIXED;
230 goto wrapped;
231 }
Chris Mason1a2b2ac2007-12-04 13:18:24 -0500232 goto out;
Chris Masone37c9e62007-05-09 20:13:14 -0400233 }
Chris Mason257d0ce2007-11-07 21:08:16 -0500234 if (cache_miss && !cache->cached) {
235 cache_block_group(root, cache);
236 last = cache_miss;
Chris Mason257d0ce2007-11-07 21:08:16 -0500237 cache = btrfs_lookup_block_group(root->fs_info, last);
238 }
Chris Mason1a2b2ac2007-12-04 13:18:24 -0500239 cache = btrfs_find_block_group(root, cache, last, data, 0);
Chris Mason0e4de582007-11-26 10:55:49 -0500240 if (!cache)
241 goto no_cache;
Chris Masone37c9e62007-05-09 20:13:14 -0400242 *cache_ret = cache;
Chris Mason257d0ce2007-11-07 21:08:16 -0500243 cache_miss = 0;
Chris Masone37c9e62007-05-09 20:13:14 -0400244 goto again;
245}
246
Chris Mason84f54cf2007-06-12 07:43:08 -0400247static u64 div_factor(u64 num, int factor)
248{
Chris Mason257d0ce2007-11-07 21:08:16 -0500249 if (factor == 10)
250 return num;
Chris Mason84f54cf2007-06-12 07:43:08 -0400251 num *= factor;
252 do_div(num, 10);
253 return num;
254}
255
Chris Mason31f3c992007-04-30 15:25:45 -0400256struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
257 struct btrfs_block_group_cache
Chris Masonbe744172007-05-06 10:15:01 -0400258 *hint, u64 search_start,
Chris Masonde428b62007-05-18 13:28:27 -0400259 int data, int owner)
Chris Masoncd1bc462007-04-27 10:08:34 -0400260{
Chris Mason96b51792007-10-15 16:15:19 -0400261 struct btrfs_block_group_cache *cache;
262 struct extent_map_tree *block_group_cache;
Chris Mason31f3c992007-04-30 15:25:45 -0400263 struct btrfs_block_group_cache *found_group = NULL;
Chris Masoncd1bc462007-04-27 10:08:34 -0400264 struct btrfs_fs_info *info = root->fs_info;
265 u64 used;
Chris Mason31f3c992007-04-30 15:25:45 -0400266 u64 last = 0;
267 u64 hint_last;
Chris Mason96b51792007-10-15 16:15:19 -0400268 u64 start;
269 u64 end;
270 u64 free_check;
271 u64 ptr;
272 int bit;
Chris Masoncd1bc462007-04-27 10:08:34 -0400273 int ret;
Chris Mason31f3c992007-04-30 15:25:45 -0400274 int full_search = 0;
Chris Masonde428b62007-05-18 13:28:27 -0400275 int factor = 8;
Chris Mason1e2677e2007-05-29 16:52:18 -0400276 int data_swap = 0;
Chris Masonde428b62007-05-18 13:28:27 -0400277
Chris Mason96b51792007-10-15 16:15:19 -0400278 block_group_cache = &info->block_group_cache;
279
Chris Masonde428b62007-05-18 13:28:27 -0400280 if (!owner)
Chris Masonf84a8b32007-11-06 10:26:29 -0500281 factor = 8;
Chris Masonbe744172007-05-06 10:15:01 -0400282
Chris Mason257d0ce2007-11-07 21:08:16 -0500283 if (data == BTRFS_BLOCK_GROUP_MIXED) {
Chris Masonf84a8b32007-11-06 10:26:29 -0500284 bit = BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA;
Chris Mason257d0ce2007-11-07 21:08:16 -0500285 factor = 10;
286 } else if (data)
Chris Mason96b51792007-10-15 16:15:19 -0400287 bit = BLOCK_GROUP_DATA;
288 else
289 bit = BLOCK_GROUP_METADATA;
Chris Masonbe744172007-05-06 10:15:01 -0400290
291 if (search_start) {
292 struct btrfs_block_group_cache *shint;
Chris Mason5276aed2007-06-11 21:33:38 -0400293 shint = btrfs_lookup_block_group(info, search_start);
Chris Masonf84a8b32007-11-06 10:26:29 -0500294 if (shint && (shint->data == data ||
295 shint->data == BTRFS_BLOCK_GROUP_MIXED)) {
Chris Masonbe744172007-05-06 10:15:01 -0400296 used = btrfs_block_group_used(&shint->item);
Yan324ae4d2007-11-16 14:57:08 -0500297 if (used + shint->pinned <
298 div_factor(shint->key.offset, factor)) {
Chris Masonbe744172007-05-06 10:15:01 -0400299 return shint;
300 }
301 }
302 }
Chris Masonf84a8b32007-11-06 10:26:29 -0500303 if (hint && (hint->data == data ||
304 hint->data == BTRFS_BLOCK_GROUP_MIXED)) {
Chris Mason31f3c992007-04-30 15:25:45 -0400305 used = btrfs_block_group_used(&hint->item);
Yan324ae4d2007-11-16 14:57:08 -0500306 if (used + hint->pinned <
307 div_factor(hint->key.offset, factor)) {
Chris Mason31f3c992007-04-30 15:25:45 -0400308 return hint;
309 }
Chris Masone19caa52007-10-15 16:17:44 -0400310 last = hint->key.objectid + hint->key.offset;
Chris Mason31f3c992007-04-30 15:25:45 -0400311 hint_last = last;
312 } else {
Chris Masone37c9e62007-05-09 20:13:14 -0400313 if (hint)
314 hint_last = max(hint->key.objectid, search_start);
315 else
316 hint_last = search_start;
317
318 last = hint_last;
Chris Mason31f3c992007-04-30 15:25:45 -0400319 }
Chris Mason31f3c992007-04-30 15:25:45 -0400320again:
Chris Masoncd1bc462007-04-27 10:08:34 -0400321 while(1) {
Chris Mason96b51792007-10-15 16:15:19 -0400322 ret = find_first_extent_bit(block_group_cache, last,
323 &start, &end, bit);
324 if (ret)
Chris Masoncd1bc462007-04-27 10:08:34 -0400325 break;
Chris Mason96b51792007-10-15 16:15:19 -0400326
327 ret = get_state_private(block_group_cache, start, &ptr);
328 if (ret)
329 break;
330
Jens Axboeae2f5412007-10-19 09:22:59 -0400331 cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
Chris Mason96b51792007-10-15 16:15:19 -0400332 last = cache->key.objectid + cache->key.offset;
333 used = btrfs_block_group_used(&cache->item);
334
335 if (full_search)
336 free_check = cache->key.offset;
337 else
338 free_check = div_factor(cache->key.offset, factor);
Yan324ae4d2007-11-16 14:57:08 -0500339 if (used + cache->pinned < free_check) {
Chris Mason96b51792007-10-15 16:15:19 -0400340 found_group = cache;
341 goto found;
Chris Masoncd1bc462007-04-27 10:08:34 -0400342 }
Chris Masonde428b62007-05-18 13:28:27 -0400343 cond_resched();
Chris Masoncd1bc462007-04-27 10:08:34 -0400344 }
Chris Mason31f3c992007-04-30 15:25:45 -0400345 if (!full_search) {
Chris Masonbe744172007-05-06 10:15:01 -0400346 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -0400347 full_search = 1;
348 goto again;
349 }
Chris Mason1e2677e2007-05-29 16:52:18 -0400350 if (!data_swap) {
Chris Mason1e2677e2007-05-29 16:52:18 -0400351 data_swap = 1;
Chris Mason96b51792007-10-15 16:15:19 -0400352 bit = BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA;
Chris Mason1e2677e2007-05-29 16:52:18 -0400353 last = search_start;
354 goto again;
355 }
Chris Masonbe744172007-05-06 10:15:01 -0400356found:
Chris Mason31f3c992007-04-30 15:25:45 -0400357 return found_group;
Chris Masoncd1bc462007-04-27 10:08:34 -0400358}
359
Chris Mason7bb86312007-12-11 09:25:06 -0500360static u64 hash_extent_ref(u64 root_objectid, u64 ref_generation,
Chris Mason74493f72007-12-11 09:25:06 -0500361 u64 owner, u64 owner_offset)
362{
363 u32 high_crc = ~(u32)0;
364 u32 low_crc = ~(u32)0;
365 __le64 lenum;
366
367 lenum = cpu_to_le64(root_objectid);
368 high_crc = crc32c(high_crc, &lenum, sizeof(lenum));
Chris Mason7bb86312007-12-11 09:25:06 -0500369 lenum = cpu_to_le64(ref_generation);
370 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
Chris Mason74493f72007-12-11 09:25:06 -0500371
Chris Mason7bb86312007-12-11 09:25:06 -0500372#if 0
Chris Mason74493f72007-12-11 09:25:06 -0500373 lenum = cpu_to_le64(owner);
374 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
Chris Mason74493f72007-12-11 09:25:06 -0500375 lenum = cpu_to_le64(owner_offset);
376 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
Chris Mason7bb86312007-12-11 09:25:06 -0500377#endif
Chris Mason74493f72007-12-11 09:25:06 -0500378 return ((u64)high_crc << 32) | (u64)low_crc;
379}
380
Chris Mason7bb86312007-12-11 09:25:06 -0500381static int match_extent_ref(struct extent_buffer *leaf,
382 struct btrfs_extent_ref *disk_ref,
383 struct btrfs_extent_ref *cpu_ref)
384{
385 int ret;
386 int len;
387
388 if (cpu_ref->objectid)
389 len = sizeof(*cpu_ref);
390 else
391 len = 2 * sizeof(u64);
392 ret = memcmp_extent_buffer(leaf, cpu_ref, (unsigned long)disk_ref,
393 len);
394 return ret == 0;
395}
396
Chris Mason98ed5172008-01-03 10:01:48 -0500397static int noinline lookup_extent_backref(struct btrfs_trans_handle *trans,
398 struct btrfs_root *root,
399 struct btrfs_path *path, u64 bytenr,
400 u64 root_objectid,
401 u64 ref_generation, u64 owner,
402 u64 owner_offset, int del)
Chris Mason7bb86312007-12-11 09:25:06 -0500403{
404 u64 hash;
405 struct btrfs_key key;
406 struct btrfs_key found_key;
407 struct btrfs_extent_ref ref;
408 struct extent_buffer *leaf;
409 struct btrfs_extent_ref *disk_ref;
410 int ret;
411 int ret2;
412
413 btrfs_set_stack_ref_root(&ref, root_objectid);
414 btrfs_set_stack_ref_generation(&ref, ref_generation);
415 btrfs_set_stack_ref_objectid(&ref, owner);
416 btrfs_set_stack_ref_offset(&ref, owner_offset);
417
418 hash = hash_extent_ref(root_objectid, ref_generation, owner,
419 owner_offset);
420 key.offset = hash;
421 key.objectid = bytenr;
422 key.type = BTRFS_EXTENT_REF_KEY;
423
424 while (1) {
425 ret = btrfs_search_slot(trans, root, &key, path,
426 del ? -1 : 0, del);
427 if (ret < 0)
428 goto out;
429 leaf = path->nodes[0];
430 if (ret != 0) {
431 u32 nritems = btrfs_header_nritems(leaf);
432 if (path->slots[0] >= nritems) {
433 ret2 = btrfs_next_leaf(root, path);
434 if (ret2)
435 goto out;
436 leaf = path->nodes[0];
437 }
438 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
439 if (found_key.objectid != bytenr ||
440 found_key.type != BTRFS_EXTENT_REF_KEY)
441 goto out;
442 key.offset = found_key.offset;
443 if (del) {
444 btrfs_release_path(root, path);
445 continue;
446 }
447 }
448 disk_ref = btrfs_item_ptr(path->nodes[0],
449 path->slots[0],
450 struct btrfs_extent_ref);
451 if (match_extent_ref(path->nodes[0], disk_ref, &ref)) {
452 ret = 0;
453 goto out;
454 }
455 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
456 key.offset = found_key.offset + 1;
457 btrfs_release_path(root, path);
458 }
459out:
460 return ret;
461}
462
Chris Masond8d5f3e2007-12-11 12:42:00 -0500463/*
464 * Back reference rules. Back refs have three main goals:
465 *
466 * 1) differentiate between all holders of references to an extent so that
467 * when a reference is dropped we can make sure it was a valid reference
468 * before freeing the extent.
469 *
470 * 2) Provide enough information to quickly find the holders of an extent
471 * if we notice a given block is corrupted or bad.
472 *
473 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
474 * maintenance. This is actually the same as #2, but with a slightly
475 * different use case.
476 *
477 * File extents can be referenced by:
478 *
479 * - multiple snapshots, subvolumes, or different generations in one subvol
480 * - different files inside a single subvolume (in theory, not implemented yet)
481 * - different offsets inside a file (bookend extents in file.c)
482 *
483 * The extent ref structure has fields for:
484 *
485 * - Objectid of the subvolume root
486 * - Generation number of the tree holding the reference
487 * - objectid of the file holding the reference
488 * - offset in the file corresponding to the key holding the reference
489 *
490 * When a file extent is allocated the fields are filled in:
491 * (root_key.objectid, trans->transid, inode objectid, offset in file)
492 *
493 * When a leaf is cow'd new references are added for every file extent found
494 * in the leaf. It looks the same as the create case, but trans->transid
495 * will be different when the block is cow'd.
496 *
497 * (root_key.objectid, trans->transid, inode objectid, offset in file)
498 *
499 * When a file extent is removed either during snapshot deletion or file
500 * truncation, the corresponding back reference is found
501 * by searching for:
502 *
503 * (btrfs_header_owner(leaf), btrfs_header_generation(leaf),
504 * inode objectid, offset in file)
505 *
506 * Btree extents can be referenced by:
507 *
508 * - Different subvolumes
509 * - Different generations of the same subvolume
510 *
511 * Storing sufficient information for a full reverse mapping of a btree
512 * block would require storing the lowest key of the block in the backref,
513 * and it would require updating that lowest key either before write out or
514 * every time it changed. Instead, the objectid of the lowest key is stored
515 * along with the level of the tree block. This provides a hint
516 * about where in the btree the block can be found. Searches through the
517 * btree only need to look for a pointer to that block, so they stop one
518 * level higher than the level recorded in the backref.
519 *
520 * Some btrees do not do reference counting on their extents. These
521 * include the extent tree and the tree of tree roots. Backrefs for these
522 * trees always have a generation of zero.
523 *
524 * When a tree block is created, back references are inserted:
525 *
Chris Masonf6dbff52007-12-13 11:13:32 -0500526 * (root->root_key.objectid, trans->transid or zero, level, lowest_key_objectid)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500527 *
528 * When a tree block is cow'd in a reference counted root,
529 * new back references are added for all the blocks it points to.
530 * These are of the form (trans->transid will have increased since creation):
531 *
Chris Masonf6dbff52007-12-13 11:13:32 -0500532 * (root->root_key.objectid, trans->transid, level, lowest_key_objectid)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500533 *
534 * Because the lowest_key_objectid and the level are just hints
535 * they are not used when backrefs are deleted. When a backref is deleted:
536 *
537 * if backref was for a tree root:
538 * root_objectid = root->root_key.objectid
539 * else
540 * root_objectid = btrfs_header_owner(parent)
541 *
542 * (root_objectid, btrfs_header_generation(parent) or zero, 0, 0)
543 *
544 * Back Reference Key hashing:
545 *
546 * Back references have four fields, each 64 bits long. Unfortunately,
547 * This is hashed into a single 64 bit number and placed into the key offset.
548 * The key objectid corresponds to the first byte in the extent, and the
549 * key type is set to BTRFS_EXTENT_REF_KEY
550 */
Chris Mason7bb86312007-12-11 09:25:06 -0500551int btrfs_insert_extent_backref(struct btrfs_trans_handle *trans,
552 struct btrfs_root *root,
553 struct btrfs_path *path, u64 bytenr,
554 u64 root_objectid, u64 ref_generation,
555 u64 owner, u64 owner_offset)
Chris Mason74493f72007-12-11 09:25:06 -0500556{
557 u64 hash;
558 struct btrfs_key key;
559 struct btrfs_extent_ref ref;
Chris Mason7bb86312007-12-11 09:25:06 -0500560 struct btrfs_extent_ref *disk_ref;
Chris Mason74493f72007-12-11 09:25:06 -0500561 int ret;
562
563 btrfs_set_stack_ref_root(&ref, root_objectid);
Chris Mason7bb86312007-12-11 09:25:06 -0500564 btrfs_set_stack_ref_generation(&ref, ref_generation);
Chris Mason74493f72007-12-11 09:25:06 -0500565 btrfs_set_stack_ref_objectid(&ref, owner);
566 btrfs_set_stack_ref_offset(&ref, owner_offset);
567
Chris Mason7bb86312007-12-11 09:25:06 -0500568 hash = hash_extent_ref(root_objectid, ref_generation, owner,
569 owner_offset);
Chris Mason74493f72007-12-11 09:25:06 -0500570 key.offset = hash;
571 key.objectid = bytenr;
572 key.type = BTRFS_EXTENT_REF_KEY;
573
574 ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(ref));
575 while (ret == -EEXIST) {
Chris Mason7bb86312007-12-11 09:25:06 -0500576 disk_ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
577 struct btrfs_extent_ref);
578 if (match_extent_ref(path->nodes[0], disk_ref, &ref))
579 goto out;
580 key.offset++;
581 btrfs_release_path(root, path);
582 ret = btrfs_insert_empty_item(trans, root, path, &key,
583 sizeof(ref));
Chris Mason74493f72007-12-11 09:25:06 -0500584 }
Chris Mason7bb86312007-12-11 09:25:06 -0500585 if (ret)
586 goto out;
587 disk_ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
588 struct btrfs_extent_ref);
589 write_extent_buffer(path->nodes[0], &ref, (unsigned long)disk_ref,
590 sizeof(ref));
591 btrfs_mark_buffer_dirty(path->nodes[0]);
592out:
593 btrfs_release_path(root, path);
594 return ret;
Chris Mason74493f72007-12-11 09:25:06 -0500595}
596
Chris Masonb18c6682007-04-17 13:26:50 -0400597int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
598 struct btrfs_root *root,
Chris Mason74493f72007-12-11 09:25:06 -0500599 u64 bytenr, u64 num_bytes,
Chris Mason7bb86312007-12-11 09:25:06 -0500600 u64 root_objectid, u64 ref_generation,
Chris Mason74493f72007-12-11 09:25:06 -0500601 u64 owner, u64 owner_offset)
Chris Mason02217ed2007-03-02 16:08:05 -0500602{
Chris Mason5caf2a02007-04-02 11:20:42 -0400603 struct btrfs_path *path;
Chris Mason02217ed2007-03-02 16:08:05 -0500604 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400605 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400606 struct extent_buffer *l;
Chris Mason234b63a2007-03-13 10:46:10 -0400607 struct btrfs_extent_item *item;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400608 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500609
Chris Masondb945352007-10-15 16:15:53 -0400610 WARN_ON(num_bytes < root->sectorsize);
Chris Mason5caf2a02007-04-02 11:20:42 -0400611 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -0400612 if (!path)
613 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -0400614
Chris Masondb945352007-10-15 16:15:53 -0400615 key.objectid = bytenr;
Chris Mason62e27492007-03-15 12:56:47 -0400616 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masondb945352007-10-15 16:15:53 -0400617 key.offset = num_bytes;
Chris Mason5caf2a02007-04-02 11:20:42 -0400618 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -0400619 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400620 if (ret < 0)
621 return ret;
Chris Masona429e512007-04-18 16:15:28 -0400622 if (ret != 0) {
Chris Masona28ec192007-03-06 20:08:01 -0500623 BUG();
Chris Masona429e512007-04-18 16:15:28 -0400624 }
Chris Mason02217ed2007-03-02 16:08:05 -0500625 BUG_ON(ret != 0);
Chris Mason5f39d392007-10-15 16:14:19 -0400626 l = path->nodes[0];
Chris Mason5caf2a02007-04-02 11:20:42 -0400627 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400628 refs = btrfs_extent_refs(l, item);
629 btrfs_set_extent_refs(l, item, refs + 1);
Chris Mason5caf2a02007-04-02 11:20:42 -0400630 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masona28ec192007-03-06 20:08:01 -0500631
Chris Mason5caf2a02007-04-02 11:20:42 -0400632 btrfs_release_path(root->fs_info->extent_root, path);
Chris Mason7bb86312007-12-11 09:25:06 -0500633
634 ret = btrfs_insert_extent_backref(trans, root->fs_info->extent_root,
635 path, bytenr, root_objectid,
636 ref_generation, owner, owner_offset);
637 BUG_ON(ret);
Chris Mason9f5fae22007-03-20 14:38:32 -0400638 finish_current_insert(trans, root->fs_info->extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -0400639 del_pending_extents(trans, root->fs_info->extent_root);
Chris Mason74493f72007-12-11 09:25:06 -0500640
641 btrfs_free_path(path);
Chris Mason02217ed2007-03-02 16:08:05 -0500642 return 0;
643}
644
Chris Masone9d0b132007-08-10 14:06:19 -0400645int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
646 struct btrfs_root *root)
647{
648 finish_current_insert(trans, root->fs_info->extent_root);
649 del_pending_extents(trans, root->fs_info->extent_root);
650 return 0;
651}
652
Chris Masonb18c6682007-04-17 13:26:50 -0400653static int lookup_extent_ref(struct btrfs_trans_handle *trans,
Chris Masondb945352007-10-15 16:15:53 -0400654 struct btrfs_root *root, u64 bytenr,
655 u64 num_bytes, u32 *refs)
Chris Masona28ec192007-03-06 20:08:01 -0500656{
Chris Mason5caf2a02007-04-02 11:20:42 -0400657 struct btrfs_path *path;
Chris Masona28ec192007-03-06 20:08:01 -0500658 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400659 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400660 struct extent_buffer *l;
Chris Mason234b63a2007-03-13 10:46:10 -0400661 struct btrfs_extent_item *item;
Chris Mason5caf2a02007-04-02 11:20:42 -0400662
Chris Masondb945352007-10-15 16:15:53 -0400663 WARN_ON(num_bytes < root->sectorsize);
Chris Mason5caf2a02007-04-02 11:20:42 -0400664 path = btrfs_alloc_path();
Chris Masondb945352007-10-15 16:15:53 -0400665 key.objectid = bytenr;
666 key.offset = num_bytes;
Chris Mason62e27492007-03-15 12:56:47 -0400667 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Mason5caf2a02007-04-02 11:20:42 -0400668 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -0400669 0, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400670 if (ret < 0)
671 goto out;
Chris Mason5f39d392007-10-15 16:14:19 -0400672 if (ret != 0) {
673 btrfs_print_leaf(root, path->nodes[0]);
Chris Masondb945352007-10-15 16:15:53 -0400674 printk("failed to find block number %Lu\n", bytenr);
Chris Masona28ec192007-03-06 20:08:01 -0500675 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -0400676 }
677 l = path->nodes[0];
Chris Mason5caf2a02007-04-02 11:20:42 -0400678 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400679 *refs = btrfs_extent_refs(l, item);
Chris Mason54aa1f42007-06-22 14:16:25 -0400680out:
Chris Mason5caf2a02007-04-02 11:20:42 -0400681 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -0500682 return 0;
683}
684
Chris Masonbe20aa92007-12-17 20:14:01 -0500685u32 btrfs_count_snapshots_in_path(struct btrfs_root *root,
686 struct btrfs_path *count_path,
687 u64 first_extent)
688{
689 struct btrfs_root *extent_root = root->fs_info->extent_root;
690 struct btrfs_path *path;
691 u64 bytenr;
692 u64 found_objectid;
Chris Mason56b453c2008-01-03 09:08:27 -0500693 u64 root_objectid = root->root_key.objectid;
Chris Masonbe20aa92007-12-17 20:14:01 -0500694 u32 total_count = 0;
695 u32 cur_count;
696 u32 refs;
697 u32 nritems;
698 int ret;
699 struct btrfs_key key;
700 struct btrfs_key found_key;
701 struct extent_buffer *l;
702 struct btrfs_extent_item *item;
703 struct btrfs_extent_ref *ref_item;
704 int level = -1;
705
706 path = btrfs_alloc_path();
707again:
708 if (level == -1)
709 bytenr = first_extent;
710 else
711 bytenr = count_path->nodes[level]->start;
712
713 cur_count = 0;
714 key.objectid = bytenr;
715 key.offset = 0;
716
717 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
718 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
719 if (ret < 0)
720 goto out;
721 BUG_ON(ret == 0);
722
723 l = path->nodes[0];
724 btrfs_item_key_to_cpu(l, &found_key, path->slots[0]);
725
726 if (found_key.objectid != bytenr ||
727 found_key.type != BTRFS_EXTENT_ITEM_KEY) {
728 goto out;
729 }
730
731 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
732 refs = btrfs_extent_refs(l, item);
733 while (1) {
734 nritems = btrfs_header_nritems(l);
735 if (path->slots[0] >= nritems) {
736 ret = btrfs_next_leaf(extent_root, path);
737 if (ret == 0)
738 continue;
739 break;
740 }
741 btrfs_item_key_to_cpu(l, &found_key, path->slots[0]);
742 if (found_key.objectid != bytenr)
743 break;
744 if (found_key.type != BTRFS_EXTENT_REF_KEY) {
745 path->slots[0]++;
746 continue;
747 }
748
749 cur_count++;
750 ref_item = btrfs_item_ptr(l, path->slots[0],
751 struct btrfs_extent_ref);
752 found_objectid = btrfs_ref_root(l, ref_item);
753
Chris Mason56b453c2008-01-03 09:08:27 -0500754 if (found_objectid != root_objectid) {
755 total_count = 2;
Chris Mason4313b392008-01-03 09:08:48 -0500756 goto out;
Chris Mason56b453c2008-01-03 09:08:27 -0500757 }
758 total_count = 1;
Chris Masonbe20aa92007-12-17 20:14:01 -0500759 path->slots[0]++;
760 }
761 if (cur_count == 0) {
762 total_count = 0;
763 goto out;
764 }
Chris Masonbe20aa92007-12-17 20:14:01 -0500765 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 Mason98ed5172008-01-03 10:01:48 -05001121static int finish_current_insert(struct btrfs_trans_handle *trans,
1122 struct 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 Mason98ed5172008-01-03 10:01:48 -05001365static int noinline find_free_extent(struct btrfs_trans_handle *trans,
1366 struct btrfs_root *orig_root,
1367 u64 num_bytes, u64 empty_size,
1368 u64 search_start, u64 search_end,
1369 u64 hint_byte, struct btrfs_key *ins,
1370 u64 exclude_start, u64 exclude_nr,
1371 int data)
Chris Masonfec577f2007-02-26 10:40:21 -05001372{
Chris Mason5caf2a02007-04-02 11:20:42 -04001373 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -04001374 struct btrfs_key key;
Chris Masonfec577f2007-02-26 10:40:21 -05001375 u64 hole_size = 0;
Chris Mason87ee04e2007-11-30 11:30:34 -05001376 u64 aligned;
1377 int ret;
Chris Masonfec577f2007-02-26 10:40:21 -05001378 int slot = 0;
Chris Masondb945352007-10-15 16:15:53 -04001379 u64 last_byte = 0;
Chris Masonbe744172007-05-06 10:15:01 -04001380 u64 orig_search_start = search_start;
Chris Masonfec577f2007-02-26 10:40:21 -05001381 int start_found;
Chris Mason5f39d392007-10-15 16:14:19 -04001382 struct extent_buffer *l;
Chris Mason9f5fae22007-03-20 14:38:32 -04001383 struct btrfs_root * root = orig_root->fs_info->extent_root;
Chris Masonf2458e12007-04-25 15:52:25 -04001384 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -04001385 u64 total_needed = num_bytes;
Chris Masone20d96d2007-03-22 12:13:20 -04001386 int level;
Chris Masonbe08c1b2007-05-03 09:06:49 -04001387 struct btrfs_block_group_cache *block_group;
Chris Masonbe744172007-05-06 10:15:01 -04001388 int full_scan = 0;
Chris Masonfbdc7622007-05-30 10:22:12 -04001389 int wrapped = 0;
Chris Masonf84a8b32007-11-06 10:26:29 -05001390 u64 cached_start;
Chris Masonfec577f2007-02-26 10:40:21 -05001391
Chris Masondb945352007-10-15 16:15:53 -04001392 WARN_ON(num_bytes < root->sectorsize);
Chris Masonb1a4d962007-04-04 15:27:52 -04001393 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
1394
Chris Mason5f39d392007-10-15 16:14:19 -04001395 level = btrfs_header_level(root->node);
1396
Chris Mason015a739c2007-11-26 16:15:16 -08001397 if (num_bytes >= 32 * 1024 * 1024 && hint_byte) {
Chris Mason257d0ce2007-11-07 21:08:16 -05001398 data = BTRFS_BLOCK_GROUP_MIXED;
1399 }
1400
Chris Mason3e1ad542007-05-07 20:03:49 -04001401 if (search_end == (u64)-1)
Chris Masondb945352007-10-15 16:15:53 -04001402 search_end = btrfs_super_total_bytes(&info->super_copy);
1403 if (hint_byte) {
1404 block_group = btrfs_lookup_block_group(info, hint_byte);
Chris Mason1a2b2ac2007-12-04 13:18:24 -05001405 if (!block_group)
1406 hint_byte = search_start;
Chris Masonbe744172007-05-06 10:15:01 -04001407 block_group = btrfs_find_block_group(root, block_group,
Chris Masondb945352007-10-15 16:15:53 -04001408 hint_byte, data, 1);
Chris Masonbe744172007-05-06 10:15:01 -04001409 } else {
1410 block_group = btrfs_find_block_group(root,
Chris Mason1a2b2ac2007-12-04 13:18:24 -05001411 trans->block_group,
1412 search_start, data, 1);
Chris Masonbe744172007-05-06 10:15:01 -04001413 }
1414
Chris Mason6702ed42007-08-07 16:15:09 -04001415 total_needed += empty_size;
Chris Masone0115992007-06-19 16:23:05 -04001416 path = btrfs_alloc_path();
Chris Masonbe744172007-05-06 10:15:01 -04001417check_failed:
Chris Mason70b043f2007-12-13 09:02:46 -05001418 if (!block_group) {
1419 block_group = btrfs_lookup_block_group(info, search_start);
1420 if (!block_group)
1421 block_group = btrfs_lookup_block_group(info,
1422 orig_search_start);
1423 }
Yan5e5745d2007-11-16 14:57:09 -05001424 search_start = find_search_start(root, &block_group, search_start,
1425 total_needed, data, full_scan);
Chris Mason87ee04e2007-11-30 11:30:34 -05001426 search_start = stripe_align(root, search_start);
Chris Masonf84a8b32007-11-06 10:26:29 -05001427 cached_start = search_start;
Chris Mason5caf2a02007-04-02 11:20:42 -04001428 btrfs_init_path(path);
Chris Masonfec577f2007-02-26 10:40:21 -05001429 ins->objectid = search_start;
1430 ins->offset = 0;
Chris Masonfec577f2007-02-26 10:40:21 -05001431 start_found = 0;
Chris Mason2cc58cf2007-08-27 16:49:44 -04001432 path->reada = 2;
Chris Masone37c9e62007-05-09 20:13:14 -04001433
Chris Mason5caf2a02007-04-02 11:20:42 -04001434 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
Chris Mason0f70abe2007-02-28 16:46:22 -05001435 if (ret < 0)
1436 goto error;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001437
Chris Masone37c9e62007-05-09 20:13:14 -04001438 if (path->slots[0] > 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001439 path->slots[0]--;
Chris Masone37c9e62007-05-09 20:13:14 -04001440 }
1441
Chris Mason5f39d392007-10-15 16:14:19 -04001442 l = path->nodes[0];
1443 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
1444
Chris Masone37c9e62007-05-09 20:13:14 -04001445 /*
Chris Mason7bb86312007-12-11 09:25:06 -05001446 * walk backwards to find the first extent item key
Chris Masone37c9e62007-05-09 20:13:14 -04001447 */
Chris Mason7bb86312007-12-11 09:25:06 -05001448 while(btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY) {
1449 if (path->slots[0] == 0) {
1450 ret = btrfs_prev_leaf(root, path);
1451 if (ret != 0) {
1452 ret = btrfs_search_slot(trans, root, ins,
1453 path, 0, 0);
1454 if (ret < 0)
1455 goto error;
1456 if (path->slots[0] > 0)
1457 path->slots[0]--;
1458 break;
1459 }
1460 } else {
Chris Masone37c9e62007-05-09 20:13:14 -04001461 path->slots[0]--;
1462 }
Chris Mason7bb86312007-12-11 09:25:06 -05001463 l = path->nodes[0];
1464 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
Chris Masone37c9e62007-05-09 20:13:14 -04001465 }
Chris Masonfec577f2007-02-26 10:40:21 -05001466 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001467 l = path->nodes[0];
Chris Mason5caf2a02007-04-02 11:20:42 -04001468 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04001469 if (slot >= btrfs_header_nritems(l)) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001470 ret = btrfs_next_leaf(root, path);
Chris Masonfec577f2007-02-26 10:40:21 -05001471 if (ret == 0)
1472 continue;
Chris Mason0f70abe2007-02-28 16:46:22 -05001473 if (ret < 0)
1474 goto error;
Chris Masone19caa52007-10-15 16:17:44 -04001475
1476 search_start = max(search_start,
1477 block_group->key.objectid);
Chris Masonfec577f2007-02-26 10:40:21 -05001478 if (!start_found) {
Chris Mason87ee04e2007-11-30 11:30:34 -05001479 aligned = stripe_align(root, search_start);
1480 ins->objectid = aligned;
1481 if (aligned >= search_end) {
1482 ret = -ENOSPC;
1483 goto error;
1484 }
1485 ins->offset = search_end - aligned;
Chris Masonfec577f2007-02-26 10:40:21 -05001486 start_found = 1;
1487 goto check_pending;
1488 }
Chris Mason87ee04e2007-11-30 11:30:34 -05001489 ins->objectid = stripe_align(root,
1490 last_byte > search_start ?
1491 last_byte : search_start);
1492 if (search_end <= ins->objectid) {
1493 ret = -ENOSPC;
1494 goto error;
1495 }
Chris Mason3e1ad542007-05-07 20:03:49 -04001496 ins->offset = search_end - ins->objectid;
Chris Masone19caa52007-10-15 16:17:44 -04001497 BUG_ON(ins->objectid >= search_end);
Chris Masonfec577f2007-02-26 10:40:21 -05001498 goto check_pending;
1499 }
Chris Mason5f39d392007-10-15 16:14:19 -04001500 btrfs_item_key_to_cpu(l, &key, slot);
Chris Mason96b51792007-10-15 16:15:19 -04001501
Chris Masondb945352007-10-15 16:15:53 -04001502 if (key.objectid >= search_start && key.objectid > last_byte &&
Chris Masone37c9e62007-05-09 20:13:14 -04001503 start_found) {
Chris Masondb945352007-10-15 16:15:53 -04001504 if (last_byte < search_start)
1505 last_byte = search_start;
Chris Mason87ee04e2007-11-30 11:30:34 -05001506 aligned = stripe_align(root, last_byte);
1507 hole_size = key.objectid - aligned;
1508 if (key.objectid > aligned && hole_size >= num_bytes) {
1509 ins->objectid = aligned;
Chris Masone37c9e62007-05-09 20:13:14 -04001510 ins->offset = hole_size;
1511 goto check_pending;
Chris Mason0579da42007-03-07 16:15:30 -05001512 }
Chris Masonfec577f2007-02-26 10:40:21 -05001513 }
Chris Mason96b51792007-10-15 16:15:19 -04001514 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY) {
Chris Mason7bb86312007-12-11 09:25:06 -05001515 if (!start_found && btrfs_key_type(&key) ==
1516 BTRFS_BLOCK_GROUP_ITEM_KEY) {
Chris Masondb945352007-10-15 16:15:53 -04001517 last_byte = key.objectid;
Chris Mason96b51792007-10-15 16:15:19 -04001518 start_found = 1;
1519 }
Chris Masone37c9e62007-05-09 20:13:14 -04001520 goto next;
Chris Mason96b51792007-10-15 16:15:19 -04001521 }
1522
Chris Masone37c9e62007-05-09 20:13:14 -04001523
Chris Mason0579da42007-03-07 16:15:30 -05001524 start_found = 1;
Chris Masondb945352007-10-15 16:15:53 -04001525 last_byte = key.objectid + key.offset;
Chris Masonf510cfe2007-10-15 16:14:48 -04001526
Chris Mason257d0ce2007-11-07 21:08:16 -05001527 if (!full_scan && data != BTRFS_BLOCK_GROUP_MIXED &&
1528 last_byte >= block_group->key.objectid +
Chris Masonbe744172007-05-06 10:15:01 -04001529 block_group->key.offset) {
1530 btrfs_release_path(root, path);
1531 search_start = block_group->key.objectid +
Chris Masone19caa52007-10-15 16:17:44 -04001532 block_group->key.offset;
Chris Masonbe744172007-05-06 10:15:01 -04001533 goto new_group;
1534 }
Chris Mason9078a3e2007-04-26 16:46:15 -04001535next:
Chris Mason5caf2a02007-04-02 11:20:42 -04001536 path->slots[0]++;
Chris Masonde428b62007-05-18 13:28:27 -04001537 cond_resched();
Chris Masonfec577f2007-02-26 10:40:21 -05001538 }
Chris Masonfec577f2007-02-26 10:40:21 -05001539check_pending:
1540 /* we have to make sure we didn't find an extent that has already
1541 * been allocated by the map tree or the original allocation
1542 */
Chris Mason5caf2a02007-04-02 11:20:42 -04001543 btrfs_release_path(root, path);
Chris Masonfec577f2007-02-26 10:40:21 -05001544 BUG_ON(ins->objectid < search_start);
Chris Masone37c9e62007-05-09 20:13:14 -04001545
Chris Masondb945352007-10-15 16:15:53 -04001546 if (ins->objectid + num_bytes >= search_end)
Chris Masoncf675822007-09-17 11:00:51 -04001547 goto enospc;
Chris Mason257d0ce2007-11-07 21:08:16 -05001548 if (!full_scan && data != BTRFS_BLOCK_GROUP_MIXED &&
Yan5cf66422007-11-16 14:57:09 -05001549 ins->objectid + num_bytes > block_group->
Chris Masone19caa52007-10-15 16:17:44 -04001550 key.objectid + block_group->key.offset) {
1551 search_start = block_group->key.objectid +
1552 block_group->key.offset;
1553 goto new_group;
1554 }
Chris Mason1a5bc162007-10-15 16:15:26 -04001555 if (test_range_bit(&info->extent_ins, ins->objectid,
Chris Masondb945352007-10-15 16:15:53 -04001556 ins->objectid + num_bytes -1, EXTENT_LOCKED, 0)) {
1557 search_start = ins->objectid + num_bytes;
Chris Mason1a5bc162007-10-15 16:15:26 -04001558 goto new_group;
1559 }
1560 if (test_range_bit(&info->pinned_extents, ins->objectid,
Chris Masondb945352007-10-15 16:15:53 -04001561 ins->objectid + num_bytes -1, EXTENT_DIRTY, 0)) {
1562 search_start = ins->objectid + num_bytes;
Chris Mason1a5bc162007-10-15 16:15:26 -04001563 goto new_group;
Chris Masonfec577f2007-02-26 10:40:21 -05001564 }
Chris Masondb945352007-10-15 16:15:53 -04001565 if (exclude_nr > 0 && (ins->objectid + num_bytes > exclude_start &&
Chris Masonf2654de2007-06-26 12:20:46 -04001566 ins->objectid < exclude_start + exclude_nr)) {
1567 search_start = exclude_start + exclude_nr;
1568 goto new_group;
1569 }
Chris Masone37c9e62007-05-09 20:13:14 -04001570 if (!data) {
Chris Mason5276aed2007-06-11 21:33:38 -04001571 block_group = btrfs_lookup_block_group(info, ins->objectid);
Chris Mason26b80032007-08-08 20:17:12 -04001572 if (block_group)
1573 trans->block_group = block_group;
Chris Masoncd1bc462007-04-27 10:08:34 -04001574 }
Chris Masondb945352007-10-15 16:15:53 -04001575 ins->offset = num_bytes;
Chris Mason5caf2a02007-04-02 11:20:42 -04001576 btrfs_free_path(path);
Chris Masonfec577f2007-02-26 10:40:21 -05001577 return 0;
Chris Masonbe744172007-05-06 10:15:01 -04001578
1579new_group:
Chris Masondb945352007-10-15 16:15:53 -04001580 if (search_start + num_bytes >= search_end) {
Chris Masoncf675822007-09-17 11:00:51 -04001581enospc:
Chris Masonbe744172007-05-06 10:15:01 -04001582 search_start = orig_search_start;
Chris Masonfbdc7622007-05-30 10:22:12 -04001583 if (full_scan) {
1584 ret = -ENOSPC;
1585 goto error;
1586 }
Chris Mason6702ed42007-08-07 16:15:09 -04001587 if (wrapped) {
1588 if (!full_scan)
1589 total_needed -= empty_size;
Chris Masonfbdc7622007-05-30 10:22:12 -04001590 full_scan = 1;
Chris Mason1a2b2ac2007-12-04 13:18:24 -05001591 data = BTRFS_BLOCK_GROUP_MIXED;
Chris Mason6702ed42007-08-07 16:15:09 -04001592 } else
Chris Masonfbdc7622007-05-30 10:22:12 -04001593 wrapped = 1;
Chris Masonbe744172007-05-06 10:15:01 -04001594 }
Chris Mason5276aed2007-06-11 21:33:38 -04001595 block_group = btrfs_lookup_block_group(info, search_start);
Chris Masonfbdc7622007-05-30 10:22:12 -04001596 cond_resched();
Chris Mason1a2b2ac2007-12-04 13:18:24 -05001597 block_group = btrfs_find_block_group(root, block_group,
1598 search_start, data, 0);
Chris Masonbe744172007-05-06 10:15:01 -04001599 goto check_failed;
1600
Chris Mason0f70abe2007-02-28 16:46:22 -05001601error:
Chris Mason5caf2a02007-04-02 11:20:42 -04001602 btrfs_release_path(root, path);
1603 btrfs_free_path(path);
Chris Mason0f70abe2007-02-28 16:46:22 -05001604 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05001605}
Chris Masonfec577f2007-02-26 10:40:21 -05001606/*
Chris Masonfec577f2007-02-26 10:40:21 -05001607 * finds a free extent and does all the dirty work required for allocation
1608 * returns the key for the extent through ins, and a tree buffer for
1609 * the first block of the extent through buf.
1610 *
1611 * returns 0 if everything worked, non-zero otherwise.
1612 */
Chris Mason4d775672007-04-20 20:23:12 -04001613int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
Chris Mason7bb86312007-12-11 09:25:06 -05001614 struct btrfs_root *root,
1615 u64 num_bytes, u64 root_objectid, u64 ref_generation,
1616 u64 owner, u64 owner_offset,
1617 u64 empty_size, u64 hint_byte,
Chris Masonbe08c1b2007-05-03 09:06:49 -04001618 u64 search_end, struct btrfs_key *ins, int data)
Chris Masonfec577f2007-02-26 10:40:21 -05001619{
1620 int ret;
1621 int pending_ret;
Chris Masondb945352007-10-15 16:15:53 -04001622 u64 super_used, root_used;
Chris Masonfbdc7622007-05-30 10:22:12 -04001623 u64 search_start = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05001624 u64 new_hint;
Chris Mason1261ec42007-03-20 20:35:03 -04001625 struct btrfs_fs_info *info = root->fs_info;
1626 struct btrfs_root *extent_root = info->extent_root;
Chris Mason234b63a2007-03-13 10:46:10 -04001627 struct btrfs_extent_item extent_item;
Chris Mason7bb86312007-12-11 09:25:06 -05001628 struct btrfs_path *path;
Chris Mason037e6392007-03-07 11:50:24 -05001629
Chris Mason5f39d392007-10-15 16:14:19 -04001630 btrfs_set_stack_extent_refs(&extent_item, 1);
Chris Mason8f662a72008-01-02 10:01:11 -05001631
1632 new_hint = max(hint_byte, root->fs_info->alloc_start);
Chris Masonedbd8d42007-12-21 16:27:24 -05001633 if (new_hint < btrfs_super_total_bytes(&info->super_copy))
1634 hint_byte = new_hint;
Chris Mason8f662a72008-01-02 10:01:11 -05001635
Chris Masondb945352007-10-15 16:15:53 -04001636 WARN_ON(num_bytes < root->sectorsize);
1637 ret = find_free_extent(trans, root, num_bytes, empty_size,
1638 search_start, search_end, hint_byte, ins,
Chris Mason26b80032007-08-08 20:17:12 -04001639 trans->alloc_exclude_start,
1640 trans->alloc_exclude_nr, data);
Chris Masonccd467d2007-06-28 15:57:36 -04001641 BUG_ON(ret);
Chris Masonf2654de2007-06-26 12:20:46 -04001642 if (ret)
1643 return ret;
1644
Josef Bacik58176a92007-08-29 15:47:34 -04001645 /* block accounting for super block */
Chris Masondb945352007-10-15 16:15:53 -04001646 super_used = btrfs_super_bytes_used(&info->super_copy);
1647 btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes);
Chris Mason26b80032007-08-08 20:17:12 -04001648
Josef Bacik58176a92007-08-29 15:47:34 -04001649 /* block accounting for root item */
Chris Masondb945352007-10-15 16:15:53 -04001650 root_used = btrfs_root_used(&root->root_item);
1651 btrfs_set_root_used(&root->root_item, root_used + num_bytes);
Josef Bacik58176a92007-08-29 15:47:34 -04001652
Chris Masonf510cfe2007-10-15 16:14:48 -04001653 clear_extent_dirty(&root->fs_info->free_space_cache,
1654 ins->objectid, ins->objectid + ins->offset - 1,
1655 GFP_NOFS);
1656
Chris Mason26b80032007-08-08 20:17:12 -04001657 if (root == extent_root) {
Chris Mason1a5bc162007-10-15 16:15:26 -04001658 set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
1659 ins->objectid + ins->offset - 1,
1660 EXTENT_LOCKED, GFP_NOFS);
Chris Masone19caa52007-10-15 16:17:44 -04001661 WARN_ON(data == 1);
Chris Mason26b80032007-08-08 20:17:12 -04001662 goto update_block;
1663 }
1664
1665 WARN_ON(trans->alloc_exclude_nr);
1666 trans->alloc_exclude_start = ins->objectid;
1667 trans->alloc_exclude_nr = ins->offset;
Chris Masone089f052007-03-16 16:20:31 -04001668 ret = btrfs_insert_item(trans, extent_root, ins, &extent_item,
1669 sizeof(extent_item));
Chris Mason037e6392007-03-07 11:50:24 -05001670
Chris Mason26b80032007-08-08 20:17:12 -04001671 trans->alloc_exclude_start = 0;
1672 trans->alloc_exclude_nr = 0;
Chris Mason7bb86312007-12-11 09:25:06 -05001673 BUG_ON(ret);
1674
1675 path = btrfs_alloc_path();
1676 BUG_ON(!path);
1677 ret = btrfs_insert_extent_backref(trans, extent_root, path,
1678 ins->objectid, root_objectid,
1679 ref_generation, owner, owner_offset);
Chris Mason26b80032007-08-08 20:17:12 -04001680
Chris Masonccd467d2007-06-28 15:57:36 -04001681 BUG_ON(ret);
Chris Mason7bb86312007-12-11 09:25:06 -05001682 btrfs_free_path(path);
Chris Masone089f052007-03-16 16:20:31 -04001683 finish_current_insert(trans, extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -04001684 pending_ret = del_pending_extents(trans, extent_root);
Chris Masonf510cfe2007-10-15 16:14:48 -04001685
Chris Masone37c9e62007-05-09 20:13:14 -04001686 if (ret) {
Chris Mason037e6392007-03-07 11:50:24 -05001687 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -04001688 }
1689 if (pending_ret) {
Chris Mason037e6392007-03-07 11:50:24 -05001690 return pending_ret;
Chris Masone37c9e62007-05-09 20:13:14 -04001691 }
Chris Mason26b80032007-08-08 20:17:12 -04001692
1693update_block:
Chris Mason1e2677e2007-05-29 16:52:18 -04001694 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0,
1695 data);
Chris Masonfabb5682007-06-07 22:13:21 -04001696 BUG_ON(ret);
Chris Mason037e6392007-03-07 11:50:24 -05001697 return 0;
Chris Masonfec577f2007-02-26 10:40:21 -05001698}
1699
1700/*
1701 * helper function to allocate a block for a given tree
1702 * returns the tree buffer or NULL.
1703 */
Chris Mason5f39d392007-10-15 16:14:19 -04001704struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Chris Masondb945352007-10-15 16:15:53 -04001705 struct btrfs_root *root,
Chris Mason7bb86312007-12-11 09:25:06 -05001706 u32 blocksize,
1707 u64 root_objectid, u64 hint,
1708 u64 empty_size)
1709{
1710 u64 ref_generation;
1711
1712 if (root->ref_cows)
1713 ref_generation = trans->transid;
1714 else
1715 ref_generation = 0;
1716
1717
1718 return __btrfs_alloc_free_block(trans, root, blocksize, root_objectid,
1719 ref_generation, 0, 0, hint, empty_size);
1720}
1721
1722/*
1723 * helper function to allocate a block for a given tree
1724 * returns the tree buffer or NULL.
1725 */
1726struct extent_buffer *__btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
1727 struct btrfs_root *root,
1728 u32 blocksize,
1729 u64 root_objectid,
1730 u64 ref_generation,
1731 u64 first_objectid,
1732 int level,
1733 u64 hint,
Chris Mason5f39d392007-10-15 16:14:19 -04001734 u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05001735{
Chris Masone2fa7222007-03-12 16:22:34 -04001736 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -05001737 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04001738 struct extent_buffer *buf;
Chris Masonfec577f2007-02-26 10:40:21 -05001739
Chris Mason7bb86312007-12-11 09:25:06 -05001740 ret = btrfs_alloc_extent(trans, root, blocksize,
1741 root_objectid, ref_generation,
Chris Masonf6dbff52007-12-13 11:13:32 -05001742 level, first_objectid, empty_size, hint,
Chris Masondb945352007-10-15 16:15:53 -04001743 (u64)-1, &ins, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05001744 if (ret) {
Chris Mason54aa1f42007-06-22 14:16:25 -04001745 BUG_ON(ret > 0);
1746 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05001747 }
Chris Masondb945352007-10-15 16:15:53 -04001748 buf = btrfs_find_create_tree_block(root, ins.objectid, blocksize);
Chris Mason54aa1f42007-06-22 14:16:25 -04001749 if (!buf) {
Chris Mason7bb86312007-12-11 09:25:06 -05001750 btrfs_free_extent(trans, root, ins.objectid, blocksize,
1751 root->root_key.objectid, ref_generation,
1752 0, 0, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001753 return ERR_PTR(-ENOMEM);
1754 }
Chris Mason5f39d392007-10-15 16:14:19 -04001755 btrfs_set_buffer_uptodate(buf);
1756 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
1757 buf->start + buf->len - 1, GFP_NOFS);
Chris Mason19c00dd2007-10-15 16:19:22 -04001758 set_extent_bits(&BTRFS_I(root->fs_info->btree_inode)->extent_tree,
1759 buf->start, buf->start + buf->len - 1,
1760 EXTENT_CSUM, GFP_NOFS);
1761 buf->flags |= EXTENT_CSUM;
Chris Mason6b800532007-10-15 16:17:34 -04001762 btrfs_set_buffer_defrag(buf);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001763 trans->blocks_used++;
Chris Masonfec577f2007-02-26 10:40:21 -05001764 return buf;
1765}
Chris Masona28ec192007-03-06 20:08:01 -05001766
Chris Mason98ed5172008-01-03 10:01:48 -05001767static int noinline drop_leaf_ref(struct btrfs_trans_handle *trans,
1768 struct btrfs_root *root,
1769 struct extent_buffer *leaf)
Chris Mason6407bf62007-03-27 06:33:00 -04001770{
Chris Mason7bb86312007-12-11 09:25:06 -05001771 u64 leaf_owner;
1772 u64 leaf_generation;
Chris Mason5f39d392007-10-15 16:14:19 -04001773 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -04001774 struct btrfs_file_extent_item *fi;
1775 int i;
1776 int nritems;
1777 int ret;
1778
Chris Mason5f39d392007-10-15 16:14:19 -04001779 BUG_ON(!btrfs_is_leaf(leaf));
1780 nritems = btrfs_header_nritems(leaf);
Chris Mason7bb86312007-12-11 09:25:06 -05001781 leaf_owner = btrfs_header_owner(leaf);
1782 leaf_generation = btrfs_header_generation(leaf);
1783
Chris Mason6407bf62007-03-27 06:33:00 -04001784 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04001785 u64 disk_bytenr;
Chris Mason5f39d392007-10-15 16:14:19 -04001786
1787 btrfs_item_key_to_cpu(leaf, &key, i);
1788 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason6407bf62007-03-27 06:33:00 -04001789 continue;
1790 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001791 if (btrfs_file_extent_type(leaf, fi) ==
1792 BTRFS_FILE_EXTENT_INLINE)
Chris Mason236454d2007-04-19 13:37:44 -04001793 continue;
Chris Mason6407bf62007-03-27 06:33:00 -04001794 /*
1795 * FIXME make sure to insert a trans record that
1796 * repeats the snapshot del on crash
1797 */
Chris Masondb945352007-10-15 16:15:53 -04001798 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
1799 if (disk_bytenr == 0)
Chris Mason3a686372007-05-24 13:35:57 -04001800 continue;
Chris Masondb945352007-10-15 16:15:53 -04001801 ret = btrfs_free_extent(trans, root, disk_bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -05001802 btrfs_file_extent_disk_num_bytes(leaf, fi),
1803 leaf_owner, leaf_generation,
1804 key.objectid, key.offset, 0);
Chris Mason6407bf62007-03-27 06:33:00 -04001805 BUG_ON(ret);
1806 }
1807 return 0;
1808}
1809
Chris Mason98ed5172008-01-03 10:01:48 -05001810static void noinline reada_walk_down(struct btrfs_root *root,
1811 struct extent_buffer *node)
Chris Masone0115992007-06-19 16:23:05 -04001812{
1813 int i;
1814 u32 nritems;
Chris Masondb945352007-10-15 16:15:53 -04001815 u64 bytenr;
Chris Masone0115992007-06-19 16:23:05 -04001816 int ret;
1817 u32 refs;
Chris Masondb945352007-10-15 16:15:53 -04001818 int level;
1819 u32 blocksize;
Chris Masone0115992007-06-19 16:23:05 -04001820
Chris Mason5f39d392007-10-15 16:14:19 -04001821 nritems = btrfs_header_nritems(node);
Chris Masondb945352007-10-15 16:15:53 -04001822 level = btrfs_header_level(node);
Chris Masone0115992007-06-19 16:23:05 -04001823 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04001824 bytenr = btrfs_node_blockptr(node, i);
1825 blocksize = btrfs_level_size(root, level - 1);
1826 ret = lookup_extent_ref(NULL, root, bytenr, blocksize, &refs);
Chris Masone0115992007-06-19 16:23:05 -04001827 BUG_ON(ret);
1828 if (refs != 1)
1829 continue;
Chris Mason409eb952007-08-08 20:17:12 -04001830 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masondb945352007-10-15 16:15:53 -04001831 ret = readahead_tree_block(root, bytenr, blocksize);
Chris Mason409eb952007-08-08 20:17:12 -04001832 cond_resched();
1833 mutex_lock(&root->fs_info->fs_mutex);
Chris Masone0115992007-06-19 16:23:05 -04001834 if (ret)
1835 break;
1836 }
1837}
1838
Chris Mason9aca1d52007-03-13 11:09:37 -04001839/*
1840 * helper function for drop_snapshot, this walks down the tree dropping ref
1841 * counts as it goes.
1842 */
Chris Mason98ed5172008-01-03 10:01:48 -05001843static int noinline walk_down_tree(struct btrfs_trans_handle *trans,
1844 struct btrfs_root *root,
1845 struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05001846{
Chris Mason7bb86312007-12-11 09:25:06 -05001847 u64 root_owner;
1848 u64 root_gen;
1849 u64 bytenr;
Chris Mason5f39d392007-10-15 16:14:19 -04001850 struct extent_buffer *next;
1851 struct extent_buffer *cur;
Chris Mason7bb86312007-12-11 09:25:06 -05001852 struct extent_buffer *parent;
Chris Masondb945352007-10-15 16:15:53 -04001853 u32 blocksize;
Chris Mason20524f02007-03-10 06:35:47 -05001854 int ret;
1855 u32 refs;
1856
Chris Mason5caf2a02007-04-02 11:20:42 -04001857 WARN_ON(*level < 0);
1858 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason5f39d392007-10-15 16:14:19 -04001859 ret = lookup_extent_ref(trans, root,
Chris Masondb945352007-10-15 16:15:53 -04001860 path->nodes[*level]->start,
1861 path->nodes[*level]->len, &refs);
Chris Mason20524f02007-03-10 06:35:47 -05001862 BUG_ON(ret);
1863 if (refs > 1)
1864 goto out;
Chris Masone0115992007-06-19 16:23:05 -04001865
Chris Mason9aca1d52007-03-13 11:09:37 -04001866 /*
1867 * walk down to the last node level and free all the leaves
1868 */
Chris Mason6407bf62007-03-27 06:33:00 -04001869 while(*level >= 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001870 WARN_ON(*level < 0);
1871 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason20524f02007-03-10 06:35:47 -05001872 cur = path->nodes[*level];
Chris Masone0115992007-06-19 16:23:05 -04001873
1874 if (*level > 0 && path->slots[*level] == 0)
Chris Mason5f39d392007-10-15 16:14:19 -04001875 reada_walk_down(root, cur);
Chris Masone0115992007-06-19 16:23:05 -04001876
Chris Mason5f39d392007-10-15 16:14:19 -04001877 if (btrfs_header_level(cur) != *level)
Chris Mason2c90e5d2007-04-02 10:50:19 -04001878 WARN_ON(1);
Chris Masone0115992007-06-19 16:23:05 -04001879
Chris Mason7518a232007-03-12 12:01:18 -04001880 if (path->slots[*level] >=
Chris Mason5f39d392007-10-15 16:14:19 -04001881 btrfs_header_nritems(cur))
Chris Mason20524f02007-03-10 06:35:47 -05001882 break;
Chris Mason6407bf62007-03-27 06:33:00 -04001883 if (*level == 0) {
1884 ret = drop_leaf_ref(trans, root, cur);
1885 BUG_ON(ret);
1886 break;
1887 }
Chris Masondb945352007-10-15 16:15:53 -04001888 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
1889 blocksize = btrfs_level_size(root, *level - 1);
1890 ret = lookup_extent_ref(trans, root, bytenr, blocksize, &refs);
Chris Mason6407bf62007-03-27 06:33:00 -04001891 BUG_ON(ret);
1892 if (refs != 1) {
Chris Mason7bb86312007-12-11 09:25:06 -05001893 parent = path->nodes[*level];
1894 root_owner = btrfs_header_owner(parent);
1895 root_gen = btrfs_header_generation(parent);
Chris Mason20524f02007-03-10 06:35:47 -05001896 path->slots[*level]++;
Chris Masondb945352007-10-15 16:15:53 -04001897 ret = btrfs_free_extent(trans, root, bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -05001898 blocksize, root_owner,
1899 root_gen, 0, 0, 1);
Chris Mason20524f02007-03-10 06:35:47 -05001900 BUG_ON(ret);
1901 continue;
1902 }
Chris Masondb945352007-10-15 16:15:53 -04001903 next = btrfs_find_tree_block(root, bytenr, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -04001904 if (!next || !btrfs_buffer_uptodate(next)) {
1905 free_extent_buffer(next);
Chris Masone9d0b132007-08-10 14:06:19 -04001906 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masondb945352007-10-15 16:15:53 -04001907 next = read_tree_block(root, bytenr, blocksize);
Chris Masone9d0b132007-08-10 14:06:19 -04001908 mutex_lock(&root->fs_info->fs_mutex);
1909
1910 /* we dropped the lock, check one more time */
Chris Masondb945352007-10-15 16:15:53 -04001911 ret = lookup_extent_ref(trans, root, bytenr,
1912 blocksize, &refs);
Chris Masone9d0b132007-08-10 14:06:19 -04001913 BUG_ON(ret);
1914 if (refs != 1) {
Chris Mason7bb86312007-12-11 09:25:06 -05001915 parent = path->nodes[*level];
1916 root_owner = btrfs_header_owner(parent);
1917 root_gen = btrfs_header_generation(parent);
1918
Chris Masone9d0b132007-08-10 14:06:19 -04001919 path->slots[*level]++;
Chris Mason5f39d392007-10-15 16:14:19 -04001920 free_extent_buffer(next);
Chris Mason7bb86312007-12-11 09:25:06 -05001921 ret = btrfs_free_extent(trans, root, bytenr,
1922 blocksize,
1923 root_owner,
1924 root_gen, 0, 0, 1);
Chris Masone9d0b132007-08-10 14:06:19 -04001925 BUG_ON(ret);
1926 continue;
1927 }
1928 }
Chris Mason5caf2a02007-04-02 11:20:42 -04001929 WARN_ON(*level <= 0);
Chris Mason83e15a22007-03-12 09:03:27 -04001930 if (path->nodes[*level-1])
Chris Mason5f39d392007-10-15 16:14:19 -04001931 free_extent_buffer(path->nodes[*level-1]);
Chris Mason20524f02007-03-10 06:35:47 -05001932 path->nodes[*level-1] = next;
Chris Mason5f39d392007-10-15 16:14:19 -04001933 *level = btrfs_header_level(next);
Chris Mason20524f02007-03-10 06:35:47 -05001934 path->slots[*level] = 0;
1935 }
1936out:
Chris Mason5caf2a02007-04-02 11:20:42 -04001937 WARN_ON(*level < 0);
1938 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason7bb86312007-12-11 09:25:06 -05001939
1940 if (path->nodes[*level] == root->node) {
1941 root_owner = root->root_key.objectid;
1942 parent = path->nodes[*level];
1943 } else {
1944 parent = path->nodes[*level + 1];
1945 root_owner = btrfs_header_owner(parent);
1946 }
1947
1948 root_gen = btrfs_header_generation(parent);
Chris Masondb945352007-10-15 16:15:53 -04001949 ret = btrfs_free_extent(trans, root, path->nodes[*level]->start,
Chris Mason7bb86312007-12-11 09:25:06 -05001950 path->nodes[*level]->len,
1951 root_owner, root_gen, 0, 0, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001952 free_extent_buffer(path->nodes[*level]);
Chris Mason20524f02007-03-10 06:35:47 -05001953 path->nodes[*level] = NULL;
1954 *level += 1;
1955 BUG_ON(ret);
1956 return 0;
1957}
1958
Chris Mason9aca1d52007-03-13 11:09:37 -04001959/*
1960 * helper for dropping snapshots. This walks back up the tree in the path
1961 * to find the first node higher up where we haven't yet gone through
1962 * all the slots
1963 */
Chris Mason98ed5172008-01-03 10:01:48 -05001964static int noinline walk_up_tree(struct btrfs_trans_handle *trans,
1965 struct btrfs_root *root,
1966 struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05001967{
Chris Mason7bb86312007-12-11 09:25:06 -05001968 u64 root_owner;
1969 u64 root_gen;
1970 struct btrfs_root_item *root_item = &root->root_item;
Chris Mason20524f02007-03-10 06:35:47 -05001971 int i;
1972 int slot;
1973 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04001974
Chris Mason234b63a2007-03-13 10:46:10 -04001975 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Mason20524f02007-03-10 06:35:47 -05001976 slot = path->slots[i];
Chris Mason5f39d392007-10-15 16:14:19 -04001977 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
1978 struct extent_buffer *node;
1979 struct btrfs_disk_key disk_key;
1980 node = path->nodes[i];
Chris Mason20524f02007-03-10 06:35:47 -05001981 path->slots[i]++;
1982 *level = i;
Chris Mason9f3a7422007-08-07 15:52:19 -04001983 WARN_ON(*level == 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001984 btrfs_node_key(node, &disk_key, path->slots[i]);
Chris Mason9f3a7422007-08-07 15:52:19 -04001985 memcpy(&root_item->drop_progress,
Chris Mason5f39d392007-10-15 16:14:19 -04001986 &disk_key, sizeof(disk_key));
Chris Mason9f3a7422007-08-07 15:52:19 -04001987 root_item->drop_level = i;
Chris Mason20524f02007-03-10 06:35:47 -05001988 return 0;
1989 } else {
Chris Mason7bb86312007-12-11 09:25:06 -05001990 if (path->nodes[*level] == root->node) {
1991 root_owner = root->root_key.objectid;
1992 root_gen =
1993 btrfs_header_generation(path->nodes[*level]);
1994 } else {
1995 struct extent_buffer *node;
1996 node = path->nodes[*level + 1];
1997 root_owner = btrfs_header_owner(node);
1998 root_gen = btrfs_header_generation(node);
1999 }
Chris Masone089f052007-03-16 16:20:31 -04002000 ret = btrfs_free_extent(trans, root,
Chris Masondb945352007-10-15 16:15:53 -04002001 path->nodes[*level]->start,
Chris Mason7bb86312007-12-11 09:25:06 -05002002 path->nodes[*level]->len,
2003 root_owner, root_gen, 0, 0, 1);
Chris Mason6407bf62007-03-27 06:33:00 -04002004 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04002005 free_extent_buffer(path->nodes[*level]);
Chris Mason83e15a22007-03-12 09:03:27 -04002006 path->nodes[*level] = NULL;
Chris Mason20524f02007-03-10 06:35:47 -05002007 *level = i + 1;
Chris Mason20524f02007-03-10 06:35:47 -05002008 }
2009 }
2010 return 1;
2011}
2012
Chris Mason9aca1d52007-03-13 11:09:37 -04002013/*
2014 * drop the reference count on the tree rooted at 'snap'. This traverses
2015 * the tree freeing any blocks that have a ref count of zero after being
2016 * decremented.
2017 */
Chris Masone089f052007-03-16 16:20:31 -04002018int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason9f3a7422007-08-07 15:52:19 -04002019 *root)
Chris Mason20524f02007-03-10 06:35:47 -05002020{
Chris Mason3768f362007-03-13 16:47:54 -04002021 int ret = 0;
Chris Mason9aca1d52007-03-13 11:09:37 -04002022 int wret;
Chris Mason20524f02007-03-10 06:35:47 -05002023 int level;
Chris Mason5caf2a02007-04-02 11:20:42 -04002024 struct btrfs_path *path;
Chris Mason20524f02007-03-10 06:35:47 -05002025 int i;
2026 int orig_level;
Chris Mason9f3a7422007-08-07 15:52:19 -04002027 struct btrfs_root_item *root_item = &root->root_item;
Chris Mason20524f02007-03-10 06:35:47 -05002028
Chris Mason5caf2a02007-04-02 11:20:42 -04002029 path = btrfs_alloc_path();
2030 BUG_ON(!path);
Chris Mason20524f02007-03-10 06:35:47 -05002031
Chris Mason5f39d392007-10-15 16:14:19 -04002032 level = btrfs_header_level(root->node);
Chris Mason20524f02007-03-10 06:35:47 -05002033 orig_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04002034 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
2035 path->nodes[level] = root->node;
Chris Masonf510cfe2007-10-15 16:14:48 -04002036 extent_buffer_get(root->node);
Chris Mason9f3a7422007-08-07 15:52:19 -04002037 path->slots[level] = 0;
2038 } else {
2039 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04002040 struct btrfs_disk_key found_key;
2041 struct extent_buffer *node;
Chris Mason6702ed42007-08-07 16:15:09 -04002042
Chris Mason9f3a7422007-08-07 15:52:19 -04002043 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Chris Mason6702ed42007-08-07 16:15:09 -04002044 level = root_item->drop_level;
2045 path->lowest_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04002046 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason6702ed42007-08-07 16:15:09 -04002047 if (wret < 0) {
Chris Mason9f3a7422007-08-07 15:52:19 -04002048 ret = wret;
2049 goto out;
2050 }
Chris Mason5f39d392007-10-15 16:14:19 -04002051 node = path->nodes[level];
2052 btrfs_node_key(node, &found_key, path->slots[level]);
2053 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
2054 sizeof(found_key)));
Chris Mason9f3a7422007-08-07 15:52:19 -04002055 }
Chris Mason20524f02007-03-10 06:35:47 -05002056 while(1) {
Chris Mason5caf2a02007-04-02 11:20:42 -04002057 wret = walk_down_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04002058 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05002059 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04002060 if (wret < 0)
2061 ret = wret;
2062
Chris Mason5caf2a02007-04-02 11:20:42 -04002063 wret = walk_up_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04002064 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05002065 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04002066 if (wret < 0)
2067 ret = wret;
Chris Mason409eb952007-08-08 20:17:12 -04002068 ret = -EAGAIN;
Chris Mason409eb952007-08-08 20:17:12 -04002069 break;
Chris Mason20524f02007-03-10 06:35:47 -05002070 }
Chris Mason83e15a22007-03-12 09:03:27 -04002071 for (i = 0; i <= orig_level; i++) {
Chris Mason5caf2a02007-04-02 11:20:42 -04002072 if (path->nodes[i]) {
Chris Mason5f39d392007-10-15 16:14:19 -04002073 free_extent_buffer(path->nodes[i]);
Chris Mason0f827312007-10-15 16:18:56 -04002074 path->nodes[i] = NULL;
Chris Mason83e15a22007-03-12 09:03:27 -04002075 }
Chris Mason20524f02007-03-10 06:35:47 -05002076 }
Chris Mason9f3a7422007-08-07 15:52:19 -04002077out:
Chris Mason5caf2a02007-04-02 11:20:42 -04002078 btrfs_free_path(path);
Chris Mason9aca1d52007-03-13 11:09:37 -04002079 return ret;
Chris Mason20524f02007-03-10 06:35:47 -05002080}
Chris Mason9078a3e2007-04-26 16:46:15 -04002081
Chris Masonbe744172007-05-06 10:15:01 -04002082int btrfs_free_block_groups(struct btrfs_fs_info *info)
2083{
Chris Masonf510cfe2007-10-15 16:14:48 -04002084 u64 start;
2085 u64 end;
Yanb97f9202007-11-01 11:28:41 -04002086 u64 ptr;
Chris Mason96b51792007-10-15 16:15:19 -04002087 int ret;
Chris Mason96b51792007-10-15 16:15:19 -04002088 while(1) {
2089 ret = find_first_extent_bit(&info->block_group_cache, 0,
2090 &start, &end, (unsigned int)-1);
2091 if (ret)
2092 break;
Yanb97f9202007-11-01 11:28:41 -04002093 ret = get_state_private(&info->block_group_cache, start, &ptr);
2094 if (!ret)
2095 kfree((void *)(unsigned long)ptr);
Chris Mason96b51792007-10-15 16:15:19 -04002096 clear_extent_bits(&info->block_group_cache, start,
2097 end, (unsigned int)-1, GFP_NOFS);
2098 }
Chris Masone37c9e62007-05-09 20:13:14 -04002099 while(1) {
Chris Masonf510cfe2007-10-15 16:14:48 -04002100 ret = find_first_extent_bit(&info->free_space_cache, 0,
2101 &start, &end, EXTENT_DIRTY);
2102 if (ret)
Chris Masone37c9e62007-05-09 20:13:14 -04002103 break;
Chris Masonf510cfe2007-10-15 16:14:48 -04002104 clear_extent_dirty(&info->free_space_cache, start,
2105 end, GFP_NOFS);
Chris Masone37c9e62007-05-09 20:13:14 -04002106 }
Chris Masonbe744172007-05-06 10:15:01 -04002107 return 0;
2108}
2109
Chris Mason98ed5172008-01-03 10:01:48 -05002110static int noinline relocate_inode_pages(struct inode *inode, u64 start,
2111 u64 len)
Chris Masonedbd8d42007-12-21 16:27:24 -05002112{
2113 u64 page_start;
2114 u64 page_end;
2115 u64 delalloc_start;
2116 u64 existing_delalloc;
2117 unsigned long last_index;
Chris Masonedbd8d42007-12-21 16:27:24 -05002118 unsigned long i;
2119 struct page *page;
2120 struct btrfs_root *root = BTRFS_I(inode)->root;
2121 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason4313b392008-01-03 09:08:48 -05002122 struct file_ra_state *ra;
2123
2124 ra = kzalloc(sizeof(*ra), GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05002125
2126 mutex_lock(&inode->i_mutex);
Chris Mason4313b392008-01-03 09:08:48 -05002127 i = start >> PAGE_CACHE_SHIFT;
Chris Masonedbd8d42007-12-21 16:27:24 -05002128 last_index = (start + len - 1) >> PAGE_CACHE_SHIFT;
2129
Chris Mason4313b392008-01-03 09:08:48 -05002130 file_ra_state_init(ra, inode->i_mapping);
2131 btrfs_force_ra(inode->i_mapping, ra, NULL, i, last_index);
2132 kfree(ra);
Chris Masonedbd8d42007-12-21 16:27:24 -05002133
Chris Mason4313b392008-01-03 09:08:48 -05002134 for (; i <= last_index; i++) {
Chris Masonedbd8d42007-12-21 16:27:24 -05002135 page = grab_cache_page(inode->i_mapping, i);
2136 if (!page)
2137 goto out_unlock;
2138 if (!PageUptodate(page)) {
2139 btrfs_readpage(NULL, page);
2140 lock_page(page);
2141 if (!PageUptodate(page)) {
2142 unlock_page(page);
2143 page_cache_release(page);
2144 goto out_unlock;
2145 }
2146 }
2147 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
2148 page_end = page_start + PAGE_CACHE_SIZE - 1;
2149
2150 lock_extent(em_tree, page_start, page_end, GFP_NOFS);
2151
2152 delalloc_start = page_start;
2153 existing_delalloc =
2154 count_range_bits(&BTRFS_I(inode)->extent_tree,
2155 &delalloc_start, page_end,
2156 PAGE_CACHE_SIZE, EXTENT_DELALLOC);
2157
2158 set_extent_delalloc(em_tree, page_start,
2159 page_end, GFP_NOFS);
2160
2161 spin_lock(&root->fs_info->delalloc_lock);
2162 root->fs_info->delalloc_bytes += PAGE_CACHE_SIZE -
2163 existing_delalloc;
2164 spin_unlock(&root->fs_info->delalloc_lock);
2165
2166 unlock_extent(em_tree, page_start, page_end, GFP_NOFS);
2167 set_page_dirty(page);
2168 unlock_page(page);
2169 page_cache_release(page);
2170 }
2171
2172out_unlock:
2173 mutex_unlock(&inode->i_mutex);
2174 return 0;
2175}
2176
Chris Mason4313b392008-01-03 09:08:48 -05002177/*
2178 * note, this releases the path
2179 */
Chris Mason98ed5172008-01-03 10:01:48 -05002180static int noinline relocate_one_reference(struct btrfs_root *extent_root,
Chris Masonedbd8d42007-12-21 16:27:24 -05002181 struct btrfs_path *path,
Chris Mason4313b392008-01-03 09:08:48 -05002182 struct btrfs_key *extent_key)
Chris Masonedbd8d42007-12-21 16:27:24 -05002183{
2184 struct inode *inode;
2185 struct btrfs_root *found_root;
Chris Mason4313b392008-01-03 09:08:48 -05002186 struct btrfs_key *root_location;
2187 struct btrfs_extent_ref *ref;
2188 u64 ref_root;
2189 u64 ref_gen;
2190 u64 ref_objectid;
2191 u64 ref_offset;
Chris Masonedbd8d42007-12-21 16:27:24 -05002192 int ret;
2193
Chris Mason4313b392008-01-03 09:08:48 -05002194 ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
2195 struct btrfs_extent_ref);
2196 ref_root = btrfs_ref_root(path->nodes[0], ref);
2197 ref_gen = btrfs_ref_generation(path->nodes[0], ref);
2198 ref_objectid = btrfs_ref_objectid(path->nodes[0], ref);
2199 ref_offset = btrfs_ref_offset(path->nodes[0], ref);
2200 btrfs_release_path(extent_root, path);
2201
2202 root_location = kmalloc(sizeof(*root_location), GFP_NOFS);
2203 root_location->objectid = ref_root;
Chris Masonedbd8d42007-12-21 16:27:24 -05002204 if (ref_gen == 0)
Chris Mason4313b392008-01-03 09:08:48 -05002205 root_location->offset = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05002206 else
Chris Mason4313b392008-01-03 09:08:48 -05002207 root_location->offset = (u64)-1;
2208 root_location->type = BTRFS_ROOT_ITEM_KEY;
Chris Masonedbd8d42007-12-21 16:27:24 -05002209
2210 found_root = btrfs_read_fs_root_no_name(extent_root->fs_info,
Chris Mason4313b392008-01-03 09:08:48 -05002211 root_location);
Chris Masonedbd8d42007-12-21 16:27:24 -05002212 BUG_ON(!found_root);
Chris Mason4313b392008-01-03 09:08:48 -05002213 kfree(root_location);
Chris Masonedbd8d42007-12-21 16:27:24 -05002214
2215 if (ref_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
2216 mutex_unlock(&extent_root->fs_info->fs_mutex);
2217 inode = btrfs_iget_locked(extent_root->fs_info->sb,
2218 ref_objectid, found_root);
2219 if (inode->i_state & I_NEW) {
2220 /* the inode and parent dir are two different roots */
2221 BTRFS_I(inode)->root = found_root;
2222 BTRFS_I(inode)->location.objectid = ref_objectid;
2223 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
2224 BTRFS_I(inode)->location.offset = 0;
2225 btrfs_read_locked_inode(inode);
2226 unlock_new_inode(inode);
2227
2228 }
2229 /* this can happen if the reference is not against
2230 * the latest version of the tree root
2231 */
2232 if (is_bad_inode(inode)) {
2233 mutex_lock(&extent_root->fs_info->fs_mutex);
2234 goto out;
2235 }
2236 relocate_inode_pages(inode, ref_offset, extent_key->offset);
2237 /* FIXME, data=ordered will help get rid of this */
2238 filemap_fdatawrite(inode->i_mapping);
2239 iput(inode);
2240 mutex_lock(&extent_root->fs_info->fs_mutex);
2241 } else {
2242 struct btrfs_trans_handle *trans;
2243 struct btrfs_key found_key;
2244 struct extent_buffer *eb;
2245 int level;
2246 int i;
2247
2248 trans = btrfs_start_transaction(found_root, 1);
2249 eb = read_tree_block(found_root, extent_key->objectid,
2250 extent_key->offset);
2251 level = btrfs_header_level(eb);
2252
2253 if (level == 0)
2254 btrfs_item_key_to_cpu(eb, &found_key, 0);
2255 else
2256 btrfs_node_key_to_cpu(eb, &found_key, 0);
2257
2258 free_extent_buffer(eb);
2259
2260 path->lowest_level = level;
Chris Mason8f662a72008-01-02 10:01:11 -05002261 path->reada = 2;
Chris Masonedbd8d42007-12-21 16:27:24 -05002262 ret = btrfs_search_slot(trans, found_root, &found_key, path,
2263 0, 1);
2264 path->lowest_level = 0;
2265 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2266 if (!path->nodes[i])
2267 break;
2268 free_extent_buffer(path->nodes[i]);
2269 path->nodes[i] = NULL;
2270 }
2271 btrfs_release_path(found_root, path);
2272 btrfs_end_transaction(trans, found_root);
2273 }
2274
2275out:
2276 return 0;
2277}
2278
Chris Mason98ed5172008-01-03 10:01:48 -05002279static int noinline relocate_one_extent(struct btrfs_root *extent_root,
2280 struct btrfs_path *path,
2281 struct btrfs_key *extent_key)
Chris Masonedbd8d42007-12-21 16:27:24 -05002282{
2283 struct btrfs_key key;
2284 struct btrfs_key found_key;
Chris Masonedbd8d42007-12-21 16:27:24 -05002285 struct extent_buffer *leaf;
Chris Masonedbd8d42007-12-21 16:27:24 -05002286 u32 nritems;
2287 u32 item_size;
2288 int ret = 0;
2289
2290 key.objectid = extent_key->objectid;
2291 key.type = BTRFS_EXTENT_REF_KEY;
2292 key.offset = 0;
2293
2294 while(1) {
2295 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
2296
2297 BUG_ON(ret == 0);
2298
2299 if (ret < 0)
2300 goto out;
2301
2302 ret = 0;
2303 leaf = path->nodes[0];
2304 nritems = btrfs_header_nritems(leaf);
2305 if (path->slots[0] == nritems)
2306 goto out;
2307
2308 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2309 if (found_key.objectid != extent_key->objectid)
2310 break;
2311
2312 if (found_key.type != BTRFS_EXTENT_REF_KEY)
2313 break;
2314
2315 key.offset = found_key.offset + 1;
2316 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2317
Chris Mason4313b392008-01-03 09:08:48 -05002318 ret = relocate_one_reference(extent_root, path, extent_key);
Chris Masonedbd8d42007-12-21 16:27:24 -05002319 if (ret)
2320 goto out;
2321 }
2322 ret = 0;
2323out:
2324 btrfs_release_path(extent_root, path);
2325 return ret;
2326}
2327
2328static int find_overlapping_extent(struct btrfs_root *root,
2329 struct btrfs_path *path, u64 new_size)
2330{
2331 struct btrfs_key found_key;
2332 struct extent_buffer *leaf;
2333 int ret;
2334
2335 while(1) {
2336 if (path->slots[0] == 0) {
2337 ret = btrfs_prev_leaf(root, path);
2338 if (ret == 1) {
2339 return 1;
2340 }
2341 if (ret < 0)
2342 return ret;
2343 } else {
2344 path->slots[0]--;
2345 }
2346 leaf = path->nodes[0];
2347 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2348 if (found_key.type == BTRFS_EXTENT_ITEM_KEY) {
2349 if (found_key.objectid + found_key.offset > new_size)
2350 return 0;
2351 else
2352 return 1;
2353 }
2354 }
2355 return 1;
2356}
2357
2358int btrfs_shrink_extent_tree(struct btrfs_root *root, u64 new_size)
2359{
2360 struct btrfs_trans_handle *trans;
2361 struct btrfs_root *tree_root = root->fs_info->tree_root;
2362 struct btrfs_path *path;
2363 u64 cur_byte;
2364 u64 total_found;
Chris Masonedbd8d42007-12-21 16:27:24 -05002365 struct btrfs_fs_info *info = root->fs_info;
2366 struct extent_map_tree *block_group_cache;
2367 struct btrfs_key key;
2368 struct btrfs_key found_key = { 0, 0, 0 };
2369 struct extent_buffer *leaf;
2370 u32 nritems;
2371 int ret;
2372 int slot;
2373
2374 btrfs_set_super_total_bytes(&info->super_copy, new_size);
2375 block_group_cache = &info->block_group_cache;
2376 path = btrfs_alloc_path();
2377 root = root->fs_info->extent_root;
Chris Mason8f662a72008-01-02 10:01:11 -05002378 path->reada = 2;
Chris Masonedbd8d42007-12-21 16:27:24 -05002379
2380again:
2381 total_found = 0;
2382 key.objectid = new_size;
2383 cur_byte = key.objectid;
2384 key.offset = 0;
2385 key.type = 0;
2386 while(1) {
Chris Mason4313b392008-01-03 09:08:48 -05002387
Chris Masonedbd8d42007-12-21 16:27:24 -05002388 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2389 if (ret < 0)
2390 goto out;
2391next:
2392 leaf = path->nodes[0];
2393 if (key.objectid == new_size - 1) {
2394 ret = find_overlapping_extent(root, path, new_size);
2395 if (ret != 0) {
2396 btrfs_release_path(root, path);
2397 ret = btrfs_search_slot(NULL, root, &key,
2398 path, 0, 0);
2399 if (ret < 0)
2400 goto out;
2401 }
2402 }
2403 nritems = btrfs_header_nritems(leaf);
2404 ret = 0;
2405 slot = path->slots[0];
2406 if (slot < nritems)
2407 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2408 if (slot == nritems ||
2409 btrfs_key_type(&found_key) != BTRFS_EXTENT_ITEM_KEY) {
2410 path->slots[0]++;
2411 if (path->slots[0] >= nritems) {
2412 ret = btrfs_next_leaf(root, path);
2413 if (ret < 0)
2414 goto out;
2415 if (ret == 1) {
2416 ret = 0;
2417 break;
2418 }
2419 }
2420 goto next;
2421 }
2422 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2423 if (found_key.objectid + found_key.offset <= cur_byte)
2424 continue;
2425 total_found++;
2426 cur_byte = found_key.objectid + found_key.offset;
2427 key.objectid = cur_byte;
2428 btrfs_release_path(root, path);
2429 ret = relocate_one_extent(root, path, &found_key);
2430 }
2431
2432 btrfs_release_path(root, path);
2433
2434 if (total_found > 0) {
2435 trans = btrfs_start_transaction(tree_root, 1);
2436 btrfs_commit_transaction(trans, tree_root);
2437
2438 mutex_unlock(&root->fs_info->fs_mutex);
2439 btrfs_clean_old_snapshots(tree_root);
2440 mutex_lock(&root->fs_info->fs_mutex);
2441
2442 trans = btrfs_start_transaction(tree_root, 1);
2443 btrfs_commit_transaction(trans, tree_root);
2444 goto again;
2445 }
2446
2447 trans = btrfs_start_transaction(root, 1);
2448 key.objectid = new_size;
2449 key.offset = 0;
2450 key.type = 0;
2451 while(1) {
Chris Mason4313b392008-01-03 09:08:48 -05002452 u64 ptr;
2453
Chris Masonedbd8d42007-12-21 16:27:24 -05002454 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2455 if (ret < 0)
2456 goto out;
2457bg_next:
2458 leaf = path->nodes[0];
2459 nritems = btrfs_header_nritems(leaf);
2460 ret = 0;
2461 slot = path->slots[0];
2462 if (slot < nritems)
2463 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2464 if (slot == nritems ||
2465 btrfs_key_type(&found_key) != BTRFS_BLOCK_GROUP_ITEM_KEY) {
2466 if (slot < nritems) {
2467 printk("shrinker found key %Lu %u %Lu\n",
2468 found_key.objectid, found_key.type,
2469 found_key.offset);
2470 path->slots[0]++;
2471 }
2472 if (path->slots[0] >= nritems) {
2473 ret = btrfs_next_leaf(root, path);
2474 if (ret < 0)
2475 break;
2476 if (ret == 1) {
2477 ret = 0;
2478 break;
2479 }
2480 }
2481 goto bg_next;
2482 }
2483 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2484 ret = get_state_private(&info->block_group_cache,
2485 found_key.objectid, &ptr);
2486 if (!ret)
2487 kfree((void *)(unsigned long)ptr);
2488
2489 clear_extent_bits(&info->block_group_cache, found_key.objectid,
2490 found_key.objectid + found_key.offset - 1,
2491 (unsigned int)-1, GFP_NOFS);
2492
2493 key.objectid = found_key.objectid + 1;
2494 btrfs_del_item(trans, root, path);
2495 btrfs_release_path(root, path);
2496 }
2497 clear_extent_dirty(&info->free_space_cache, new_size, (u64)-1,
2498 GFP_NOFS);
2499 btrfs_commit_transaction(trans, root);
2500out:
2501 btrfs_free_path(path);
2502 return ret;
2503}
2504
2505int btrfs_grow_extent_tree(struct btrfs_trans_handle *trans,
2506 struct btrfs_root *root, u64 new_size)
2507{
2508 struct btrfs_path *path;
2509 u64 nr = 0;
2510 u64 cur_byte;
2511 u64 old_size;
Chris Masonf9ef6602008-01-03 09:22:38 -05002512 unsigned long rem;
Chris Masonedbd8d42007-12-21 16:27:24 -05002513 struct btrfs_block_group_cache *cache;
2514 struct btrfs_block_group_item *item;
2515 struct btrfs_fs_info *info = root->fs_info;
2516 struct extent_map_tree *block_group_cache;
2517 struct btrfs_key key;
2518 struct extent_buffer *leaf;
2519 int ret;
2520 int bit;
2521
2522 old_size = btrfs_super_total_bytes(&info->super_copy);
2523 block_group_cache = &info->block_group_cache;
2524
2525 root = info->extent_root;
2526
2527 cache = btrfs_lookup_block_group(root->fs_info, old_size - 1);
2528
2529 cur_byte = cache->key.objectid + cache->key.offset;
2530 if (cur_byte >= new_size)
2531 goto set_size;
2532
2533 key.offset = BTRFS_BLOCK_GROUP_SIZE;
2534 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
2535
2536 path = btrfs_alloc_path();
2537 if (!path)
2538 return -ENOMEM;
2539
2540 while(cur_byte < new_size) {
2541 key.objectid = cur_byte;
2542 ret = btrfs_insert_empty_item(trans, root, path, &key,
2543 sizeof(struct btrfs_block_group_item));
2544 BUG_ON(ret);
2545 leaf = path->nodes[0];
2546 item = btrfs_item_ptr(leaf, path->slots[0],
2547 struct btrfs_block_group_item);
2548
2549 btrfs_set_disk_block_group_used(leaf, item, 0);
Chris Masonf9ef6602008-01-03 09:22:38 -05002550 div_long_long_rem(nr, 3, &rem);
2551 if (rem) {
Chris Masonedbd8d42007-12-21 16:27:24 -05002552 btrfs_set_disk_block_group_flags(leaf, item,
2553 BTRFS_BLOCK_GROUP_DATA);
2554 } else {
2555 btrfs_set_disk_block_group_flags(leaf, item, 0);
2556 }
2557 nr++;
2558
2559 cache = kmalloc(sizeof(*cache), GFP_NOFS);
2560 BUG_ON(!cache);
2561
2562 read_extent_buffer(leaf, &cache->item, (unsigned long)item,
2563 sizeof(cache->item));
2564
2565 memcpy(&cache->key, &key, sizeof(key));
2566 cache->cached = 0;
2567 cache->pinned = 0;
2568 cur_byte = key.objectid + key.offset;
2569 btrfs_release_path(root, path);
2570
2571 if (cache->item.flags & BTRFS_BLOCK_GROUP_DATA) {
2572 bit = BLOCK_GROUP_DATA;
2573 cache->data = BTRFS_BLOCK_GROUP_DATA;
2574 } else {
2575 bit = BLOCK_GROUP_METADATA;
2576 cache->data = 0;
2577 }
2578
2579 /* use EXTENT_LOCKED to prevent merging */
2580 set_extent_bits(block_group_cache, key.objectid,
2581 key.objectid + key.offset - 1,
2582 bit | EXTENT_LOCKED, GFP_NOFS);
2583 set_state_private(block_group_cache, key.objectid,
2584 (unsigned long)cache);
2585 }
2586 btrfs_free_path(path);
2587set_size:
2588 btrfs_set_super_total_bytes(&info->super_copy, new_size);
2589 return 0;
2590}
2591
Chris Mason9078a3e2007-04-26 16:46:15 -04002592int btrfs_read_block_groups(struct btrfs_root *root)
2593{
2594 struct btrfs_path *path;
2595 int ret;
2596 int err = 0;
Chris Mason96b51792007-10-15 16:15:19 -04002597 int bit;
Chris Mason9078a3e2007-04-26 16:46:15 -04002598 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04002599 struct btrfs_fs_info *info = root->fs_info;
Chris Mason96b51792007-10-15 16:15:19 -04002600 struct extent_map_tree *block_group_cache;
Chris Mason9078a3e2007-04-26 16:46:15 -04002601 struct btrfs_key key;
2602 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -04002603 struct extent_buffer *leaf;
Chris Mason96b51792007-10-15 16:15:19 -04002604
2605 block_group_cache = &info->block_group_cache;
Chris Mason9078a3e2007-04-26 16:46:15 -04002606
Chris Masonbe744172007-05-06 10:15:01 -04002607 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04002608 key.objectid = 0;
Chris Masondb945352007-10-15 16:15:53 -04002609 key.offset = BTRFS_BLOCK_GROUP_SIZE;
Chris Mason9078a3e2007-04-26 16:46:15 -04002610 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
2611
2612 path = btrfs_alloc_path();
2613 if (!path)
2614 return -ENOMEM;
2615
2616 while(1) {
Chris Masonbe744172007-05-06 10:15:01 -04002617 ret = btrfs_search_slot(NULL, info->extent_root,
Chris Mason9078a3e2007-04-26 16:46:15 -04002618 &key, path, 0, 0);
2619 if (ret != 0) {
2620 err = ret;
2621 break;
2622 }
Chris Mason5f39d392007-10-15 16:14:19 -04002623 leaf = path->nodes[0];
2624 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason9078a3e2007-04-26 16:46:15 -04002625 cache = kmalloc(sizeof(*cache), GFP_NOFS);
2626 if (!cache) {
2627 err = -1;
2628 break;
2629 }
Chris Mason3e1ad542007-05-07 20:03:49 -04002630
Chris Mason5f39d392007-10-15 16:14:19 -04002631 read_extent_buffer(leaf, &cache->item,
2632 btrfs_item_ptr_offset(leaf, path->slots[0]),
2633 sizeof(cache->item));
Chris Mason9078a3e2007-04-26 16:46:15 -04002634 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Masone37c9e62007-05-09 20:13:14 -04002635 cache->cached = 0;
Yan324ae4d2007-11-16 14:57:08 -05002636 cache->pinned = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04002637 key.objectid = found_key.objectid + found_key.offset;
2638 btrfs_release_path(root, path);
Chris Mason5f39d392007-10-15 16:14:19 -04002639
Chris Masonf84a8b32007-11-06 10:26:29 -05002640 if (cache->item.flags & BTRFS_BLOCK_GROUP_MIXED) {
2641 bit = BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA;
2642 cache->data = BTRFS_BLOCK_GROUP_MIXED;
2643 } else if (cache->item.flags & BTRFS_BLOCK_GROUP_DATA) {
Chris Mason96b51792007-10-15 16:15:19 -04002644 bit = BLOCK_GROUP_DATA;
Chris Masonf84a8b32007-11-06 10:26:29 -05002645 cache->data = BTRFS_BLOCK_GROUP_DATA;
Chris Mason96b51792007-10-15 16:15:19 -04002646 } else {
2647 bit = BLOCK_GROUP_METADATA;
2648 cache->data = 0;
Chris Mason31f3c992007-04-30 15:25:45 -04002649 }
Chris Mason96b51792007-10-15 16:15:19 -04002650
2651 /* use EXTENT_LOCKED to prevent merging */
2652 set_extent_bits(block_group_cache, found_key.objectid,
2653 found_key.objectid + found_key.offset - 1,
2654 bit | EXTENT_LOCKED, GFP_NOFS);
2655 set_state_private(block_group_cache, found_key.objectid,
Jens Axboeae2f5412007-10-19 09:22:59 -04002656 (unsigned long)cache);
Chris Mason96b51792007-10-15 16:15:19 -04002657
Chris Mason9078a3e2007-04-26 16:46:15 -04002658 if (key.objectid >=
Chris Masondb945352007-10-15 16:15:53 -04002659 btrfs_super_total_bytes(&info->super_copy))
Chris Mason9078a3e2007-04-26 16:46:15 -04002660 break;
2661 }
2662
2663 btrfs_free_path(path);
2664 return 0;
2665}