blob: 660b05a4baf5dd30ac0cc00c8171b918d349da5a [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);
Yand548ee52008-01-03 13:56:30 -050036static int find_previous_extent(struct btrfs_root *root,
37 struct btrfs_path *path)
38{
39 struct btrfs_key found_key;
40 struct extent_buffer *leaf;
41 int ret;
42
43 while(1) {
44 if (path->slots[0] == 0) {
45 ret = btrfs_prev_leaf(root, path);
46 if (ret != 0)
47 return ret;
48 } else {
49 path->slots[0]--;
50 }
51 leaf = path->nodes[0];
52 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
53 if (found_key.type == BTRFS_EXTENT_ITEM_KEY)
54 return 0;
55 }
56 return 1;
57}
Chris Masonfec577f2007-02-26 10:40:21 -050058
Chris Masone37c9e62007-05-09 20:13:14 -040059static int cache_block_group(struct btrfs_root *root,
60 struct btrfs_block_group_cache *block_group)
61{
62 struct btrfs_path *path;
63 int ret;
64 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -040065 struct extent_buffer *leaf;
Chris Masonf510cfe2007-10-15 16:14:48 -040066 struct extent_map_tree *free_space_cache;
Chris Masone37c9e62007-05-09 20:13:14 -040067 int slot;
Chris Masone37c9e62007-05-09 20:13:14 -040068 u64 last = 0;
69 u64 hole_size;
Yan7d7d6062007-09-14 16:15:28 -040070 u64 first_free;
Chris Masone37c9e62007-05-09 20:13:14 -040071 int found = 0;
72
Chris Mason00f5c792007-11-30 10:09:33 -050073 if (!block_group)
74 return 0;
75
Chris Masone37c9e62007-05-09 20:13:14 -040076 root = root->fs_info->extent_root;
Chris Masonf510cfe2007-10-15 16:14:48 -040077 free_space_cache = &root->fs_info->free_space_cache;
Chris Masone37c9e62007-05-09 20:13:14 -040078
79 if (block_group->cached)
80 return 0;
Chris Masonf510cfe2007-10-15 16:14:48 -040081
Chris Masone37c9e62007-05-09 20:13:14 -040082 path = btrfs_alloc_path();
83 if (!path)
84 return -ENOMEM;
Yan7d7d6062007-09-14 16:15:28 -040085
Chris Mason2cc58cf2007-08-27 16:49:44 -040086 path->reada = 2;
Yan7d7d6062007-09-14 16:15:28 -040087 first_free = block_group->key.objectid;
Chris Masone37c9e62007-05-09 20:13:14 -040088 key.objectid = block_group->key.objectid;
Chris Masone37c9e62007-05-09 20:13:14 -040089 key.offset = 0;
90 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
91 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
92 if (ret < 0)
93 return ret;
Yand548ee52008-01-03 13:56:30 -050094 ret = find_previous_extent(root, path);
95 if (ret < 0)
96 return ret;
97 if (ret == 0) {
98 leaf = path->nodes[0];
99 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
100 if (key.objectid + key.offset > first_free)
101 first_free = key.objectid + key.offset;
102 }
Chris Masone37c9e62007-05-09 20:13:14 -0400103 while(1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400104 leaf = path->nodes[0];
Chris Masone37c9e62007-05-09 20:13:14 -0400105 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -0400106 if (slot >= btrfs_header_nritems(leaf)) {
Chris Masone37c9e62007-05-09 20:13:14 -0400107 ret = btrfs_next_leaf(root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -0400108 if (ret < 0)
109 goto err;
Chris Masonde428b62007-05-18 13:28:27 -0400110 if (ret == 0) {
Chris Masone37c9e62007-05-09 20:13:14 -0400111 continue;
Chris Masonde428b62007-05-18 13:28:27 -0400112 } else {
Chris Masone37c9e62007-05-09 20:13:14 -0400113 break;
114 }
115 }
Chris Mason5f39d392007-10-15 16:14:19 -0400116 btrfs_item_key_to_cpu(leaf, &key, slot);
Yan7d7d6062007-09-14 16:15:28 -0400117 if (key.objectid < block_group->key.objectid) {
Yan7d7d6062007-09-14 16:15:28 -0400118 goto next;
119 }
Chris Masone37c9e62007-05-09 20:13:14 -0400120 if (key.objectid >= block_group->key.objectid +
121 block_group->key.offset) {
Yan7d7d6062007-09-14 16:15:28 -0400122 break;
123 }
124
125 if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) {
126 if (!found) {
127 last = first_free;
128 found = 1;
Chris Masone37c9e62007-05-09 20:13:14 -0400129 }
Chris Masonf510cfe2007-10-15 16:14:48 -0400130 if (key.objectid > last) {
131 hole_size = key.objectid - last;
132 set_extent_dirty(free_space_cache, last,
133 last + hole_size - 1,
134 GFP_NOFS);
Chris Masone37c9e62007-05-09 20:13:14 -0400135 }
Yan7d7d6062007-09-14 16:15:28 -0400136 last = key.objectid + key.offset;
Chris Masone37c9e62007-05-09 20:13:14 -0400137 }
Yan7d7d6062007-09-14 16:15:28 -0400138next:
Chris Masone37c9e62007-05-09 20:13:14 -0400139 path->slots[0]++;
140 }
141
Yan7d7d6062007-09-14 16:15:28 -0400142 if (!found)
143 last = first_free;
144 if (block_group->key.objectid +
145 block_group->key.offset > last) {
146 hole_size = block_group->key.objectid +
147 block_group->key.offset - last;
Chris Masonf510cfe2007-10-15 16:14:48 -0400148 set_extent_dirty(free_space_cache, last,
149 last + hole_size - 1, GFP_NOFS);
Yan7d7d6062007-09-14 16:15:28 -0400150 }
Chris Masone37c9e62007-05-09 20:13:14 -0400151 block_group->cached = 1;
Chris Mason54aa1f42007-06-22 14:16:25 -0400152err:
Chris Masone37c9e62007-05-09 20:13:14 -0400153 btrfs_free_path(path);
154 return 0;
155}
156
Chris Mason5276aed2007-06-11 21:33:38 -0400157struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
158 btrfs_fs_info *info,
Chris Masondb945352007-10-15 16:15:53 -0400159 u64 bytenr)
Chris Masonbe744172007-05-06 10:15:01 -0400160{
Chris Mason96b51792007-10-15 16:15:19 -0400161 struct extent_map_tree *block_group_cache;
162 struct btrfs_block_group_cache *block_group = NULL;
163 u64 ptr;
164 u64 start;
165 u64 end;
Chris Masonbe744172007-05-06 10:15:01 -0400166 int ret;
167
Chris Mason96b51792007-10-15 16:15:19 -0400168 block_group_cache = &info->block_group_cache;
169 ret = find_first_extent_bit(block_group_cache,
Chris Masondb945352007-10-15 16:15:53 -0400170 bytenr, &start, &end,
Chris Mason96b51792007-10-15 16:15:19 -0400171 BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA);
Chris Masonbe744172007-05-06 10:15:01 -0400172 if (ret) {
Chris Mason96b51792007-10-15 16:15:19 -0400173 return NULL;
Chris Masonbe744172007-05-06 10:15:01 -0400174 }
Chris Mason96b51792007-10-15 16:15:19 -0400175 ret = get_state_private(block_group_cache, start, &ptr);
176 if (ret)
177 return NULL;
178
Jens Axboeae2f5412007-10-19 09:22:59 -0400179 block_group = (struct btrfs_block_group_cache *)(unsigned long)ptr;
Yan5cf66422007-11-16 14:57:09 -0500180 if (block_group->key.objectid <= bytenr && bytenr <
Chris Mason96b51792007-10-15 16:15:19 -0400181 block_group->key.objectid + block_group->key.offset)
182 return block_group;
Chris Masonbe744172007-05-06 10:15:01 -0400183 return NULL;
184}
Chris Mason98ed5172008-01-03 10:01:48 -0500185static u64 noinline find_search_start(struct btrfs_root *root,
186 struct btrfs_block_group_cache **cache_ret,
Yand548ee52008-01-03 13:56:30 -0500187 u64 search_start, int num, int data)
Chris Masone37c9e62007-05-09 20:13:14 -0400188{
Chris Masone37c9e62007-05-09 20:13:14 -0400189 int ret;
190 struct btrfs_block_group_cache *cache = *cache_ret;
Chris Masone19caa52007-10-15 16:17:44 -0400191 u64 last;
Chris Masonf510cfe2007-10-15 16:14:48 -0400192 u64 start = 0;
193 u64 end = 0;
Chris Mason257d0ce2007-11-07 21:08:16 -0500194 u64 cache_miss = 0;
Chris Masonf84a8b32007-11-06 10:26:29 -0500195 int wrapped = 0;
Chris Masone37c9e62007-05-09 20:13:14 -0400196
Chris Mason00f5c792007-11-30 10:09:33 -0500197 if (!cache) {
Chris Mason1a2b2ac2007-12-04 13:18:24 -0500198 goto out;
Chris Mason00f5c792007-11-30 10:09:33 -0500199 }
Chris Masone37c9e62007-05-09 20:13:14 -0400200again:
Chris Mason54aa1f42007-06-22 14:16:25 -0400201 ret = cache_block_group(root, cache);
202 if (ret)
203 goto out;
Chris Masonf84a8b32007-11-06 10:26:29 -0500204
Chris Masone19caa52007-10-15 16:17:44 -0400205 last = max(search_start, cache->key.objectid);
206
Chris Masone37c9e62007-05-09 20:13:14 -0400207 while(1) {
Chris Masonf510cfe2007-10-15 16:14:48 -0400208 ret = find_first_extent_bit(&root->fs_info->free_space_cache,
209 last, &start, &end, EXTENT_DIRTY);
Chris Masone19caa52007-10-15 16:17:44 -0400210 if (ret) {
Chris Mason257d0ce2007-11-07 21:08:16 -0500211 if (!cache_miss)
212 cache_miss = last;
Chris Masone19caa52007-10-15 16:17:44 -0400213 goto new_group;
214 }
Chris Masonf510cfe2007-10-15 16:14:48 -0400215
216 start = max(last, start);
217 last = end + 1;
Chris Mason257d0ce2007-11-07 21:08:16 -0500218 if (last - start < num) {
219 if (last == cache->key.objectid + cache->key.offset)
220 cache_miss = start;
Chris Masonf510cfe2007-10-15 16:14:48 -0400221 continue;
Chris Mason257d0ce2007-11-07 21:08:16 -0500222 }
223 if (data != BTRFS_BLOCK_GROUP_MIXED &&
Yan5cf66422007-11-16 14:57:09 -0500224 start + num > cache->key.objectid + cache->key.offset)
Chris Masone37c9e62007-05-09 20:13:14 -0400225 goto new_group;
Chris Masonf510cfe2007-10-15 16:14:48 -0400226 return start;
Chris Masone37c9e62007-05-09 20:13:14 -0400227 }
228out:
Chris Mason1a2b2ac2007-12-04 13:18:24 -0500229 cache = btrfs_lookup_block_group(root->fs_info, search_start);
230 if (!cache) {
231 printk("Unable to find block group for %Lu\n",
232 search_start);
233 WARN_ON(1);
234 return search_start;
235 }
Chris Mason1a5bc162007-10-15 16:15:26 -0400236 return search_start;
Chris Masone37c9e62007-05-09 20:13:14 -0400237
238new_group:
Chris Masone19caa52007-10-15 16:17:44 -0400239 last = cache->key.objectid + cache->key.offset;
Chris Masonf84a8b32007-11-06 10:26:29 -0500240wrapped:
Chris Masone19caa52007-10-15 16:17:44 -0400241 cache = btrfs_lookup_block_group(root->fs_info, last);
Chris Masone37c9e62007-05-09 20:13:14 -0400242 if (!cache) {
Chris Mason0e4de582007-11-26 10:55:49 -0500243no_cache:
Chris Masonf84a8b32007-11-06 10:26:29 -0500244 if (!wrapped) {
245 wrapped = 1;
246 last = search_start;
247 data = BTRFS_BLOCK_GROUP_MIXED;
248 goto wrapped;
249 }
Chris Mason1a2b2ac2007-12-04 13:18:24 -0500250 goto out;
Chris Masone37c9e62007-05-09 20:13:14 -0400251 }
Chris Mason257d0ce2007-11-07 21:08:16 -0500252 if (cache_miss && !cache->cached) {
253 cache_block_group(root, cache);
254 last = cache_miss;
Chris Mason257d0ce2007-11-07 21:08:16 -0500255 cache = btrfs_lookup_block_group(root->fs_info, last);
256 }
Chris Mason1a2b2ac2007-12-04 13:18:24 -0500257 cache = btrfs_find_block_group(root, cache, last, data, 0);
Chris Mason0e4de582007-11-26 10:55:49 -0500258 if (!cache)
259 goto no_cache;
Chris Masone37c9e62007-05-09 20:13:14 -0400260 *cache_ret = cache;
Chris Mason257d0ce2007-11-07 21:08:16 -0500261 cache_miss = 0;
Chris Masone37c9e62007-05-09 20:13:14 -0400262 goto again;
263}
264
Chris Mason84f54cf2007-06-12 07:43:08 -0400265static u64 div_factor(u64 num, int factor)
266{
Chris Mason257d0ce2007-11-07 21:08:16 -0500267 if (factor == 10)
268 return num;
Chris Mason84f54cf2007-06-12 07:43:08 -0400269 num *= factor;
270 do_div(num, 10);
271 return num;
272}
273
Chris Mason31f3c992007-04-30 15:25:45 -0400274struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
275 struct btrfs_block_group_cache
Chris Masonbe744172007-05-06 10:15:01 -0400276 *hint, u64 search_start,
Chris Masonde428b62007-05-18 13:28:27 -0400277 int data, int owner)
Chris Masoncd1bc462007-04-27 10:08:34 -0400278{
Chris Mason96b51792007-10-15 16:15:19 -0400279 struct btrfs_block_group_cache *cache;
280 struct extent_map_tree *block_group_cache;
Chris Mason31f3c992007-04-30 15:25:45 -0400281 struct btrfs_block_group_cache *found_group = NULL;
Chris Masoncd1bc462007-04-27 10:08:34 -0400282 struct btrfs_fs_info *info = root->fs_info;
283 u64 used;
Chris Mason31f3c992007-04-30 15:25:45 -0400284 u64 last = 0;
285 u64 hint_last;
Chris Mason96b51792007-10-15 16:15:19 -0400286 u64 start;
287 u64 end;
288 u64 free_check;
289 u64 ptr;
290 int bit;
Chris Masoncd1bc462007-04-27 10:08:34 -0400291 int ret;
Chris Mason31f3c992007-04-30 15:25:45 -0400292 int full_search = 0;
Chris Masonde428b62007-05-18 13:28:27 -0400293 int factor = 8;
Chris Mason1e2677e2007-05-29 16:52:18 -0400294 int data_swap = 0;
Chris Masonde428b62007-05-18 13:28:27 -0400295
Chris Mason96b51792007-10-15 16:15:19 -0400296 block_group_cache = &info->block_group_cache;
297
Chris Masonde428b62007-05-18 13:28:27 -0400298 if (!owner)
Chris Masonf84a8b32007-11-06 10:26:29 -0500299 factor = 8;
Chris Masonbe744172007-05-06 10:15:01 -0400300
Chris Mason257d0ce2007-11-07 21:08:16 -0500301 if (data == BTRFS_BLOCK_GROUP_MIXED) {
Chris Masonf84a8b32007-11-06 10:26:29 -0500302 bit = BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA;
Chris Mason257d0ce2007-11-07 21:08:16 -0500303 factor = 10;
304 } else if (data)
Chris Mason96b51792007-10-15 16:15:19 -0400305 bit = BLOCK_GROUP_DATA;
306 else
307 bit = BLOCK_GROUP_METADATA;
Chris Masonbe744172007-05-06 10:15:01 -0400308
309 if (search_start) {
310 struct btrfs_block_group_cache *shint;
Chris Mason5276aed2007-06-11 21:33:38 -0400311 shint = btrfs_lookup_block_group(info, search_start);
Chris Masonf84a8b32007-11-06 10:26:29 -0500312 if (shint && (shint->data == data ||
313 shint->data == BTRFS_BLOCK_GROUP_MIXED)) {
Chris Masonbe744172007-05-06 10:15:01 -0400314 used = btrfs_block_group_used(&shint->item);
Yan324ae4d2007-11-16 14:57:08 -0500315 if (used + shint->pinned <
316 div_factor(shint->key.offset, factor)) {
Chris Masonbe744172007-05-06 10:15:01 -0400317 return shint;
318 }
319 }
320 }
Chris Masonf84a8b32007-11-06 10:26:29 -0500321 if (hint && (hint->data == data ||
322 hint->data == BTRFS_BLOCK_GROUP_MIXED)) {
Chris Mason31f3c992007-04-30 15:25:45 -0400323 used = btrfs_block_group_used(&hint->item);
Yan324ae4d2007-11-16 14:57:08 -0500324 if (used + hint->pinned <
325 div_factor(hint->key.offset, factor)) {
Chris Mason31f3c992007-04-30 15:25:45 -0400326 return hint;
327 }
Chris Masone19caa52007-10-15 16:17:44 -0400328 last = hint->key.objectid + hint->key.offset;
Chris Mason31f3c992007-04-30 15:25:45 -0400329 hint_last = last;
330 } else {
Chris Masone37c9e62007-05-09 20:13:14 -0400331 if (hint)
332 hint_last = max(hint->key.objectid, search_start);
333 else
334 hint_last = search_start;
335
336 last = hint_last;
Chris Mason31f3c992007-04-30 15:25:45 -0400337 }
Chris Mason31f3c992007-04-30 15:25:45 -0400338again:
Chris Masoncd1bc462007-04-27 10:08:34 -0400339 while(1) {
Chris Mason96b51792007-10-15 16:15:19 -0400340 ret = find_first_extent_bit(block_group_cache, last,
341 &start, &end, bit);
342 if (ret)
Chris Masoncd1bc462007-04-27 10:08:34 -0400343 break;
Chris Mason96b51792007-10-15 16:15:19 -0400344
345 ret = get_state_private(block_group_cache, start, &ptr);
346 if (ret)
347 break;
348
Jens Axboeae2f5412007-10-19 09:22:59 -0400349 cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
Chris Mason96b51792007-10-15 16:15:19 -0400350 last = cache->key.objectid + cache->key.offset;
351 used = btrfs_block_group_used(&cache->item);
352
353 if (full_search)
354 free_check = cache->key.offset;
355 else
356 free_check = div_factor(cache->key.offset, factor);
Yan324ae4d2007-11-16 14:57:08 -0500357 if (used + cache->pinned < free_check) {
Chris Mason96b51792007-10-15 16:15:19 -0400358 found_group = cache;
359 goto found;
Chris Masoncd1bc462007-04-27 10:08:34 -0400360 }
Chris Masonde428b62007-05-18 13:28:27 -0400361 cond_resched();
Chris Masoncd1bc462007-04-27 10:08:34 -0400362 }
Chris Mason31f3c992007-04-30 15:25:45 -0400363 if (!full_search) {
Chris Masonbe744172007-05-06 10:15:01 -0400364 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -0400365 full_search = 1;
366 goto again;
367 }
Chris Mason1e2677e2007-05-29 16:52:18 -0400368 if (!data_swap) {
Chris Mason1e2677e2007-05-29 16:52:18 -0400369 data_swap = 1;
Chris Mason96b51792007-10-15 16:15:19 -0400370 bit = BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA;
Chris Mason1e2677e2007-05-29 16:52:18 -0400371 last = search_start;
372 goto again;
373 }
Chris Masonbe744172007-05-06 10:15:01 -0400374found:
Chris Mason31f3c992007-04-30 15:25:45 -0400375 return found_group;
Chris Masoncd1bc462007-04-27 10:08:34 -0400376}
377
Chris Mason7bb86312007-12-11 09:25:06 -0500378static u64 hash_extent_ref(u64 root_objectid, u64 ref_generation,
Chris Mason74493f72007-12-11 09:25:06 -0500379 u64 owner, u64 owner_offset)
380{
381 u32 high_crc = ~(u32)0;
382 u32 low_crc = ~(u32)0;
383 __le64 lenum;
384
385 lenum = cpu_to_le64(root_objectid);
386 high_crc = crc32c(high_crc, &lenum, sizeof(lenum));
Chris Mason7bb86312007-12-11 09:25:06 -0500387 lenum = cpu_to_le64(ref_generation);
388 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
Chris Mason74493f72007-12-11 09:25:06 -0500389
Chris Mason7bb86312007-12-11 09:25:06 -0500390#if 0
Chris Mason74493f72007-12-11 09:25:06 -0500391 lenum = cpu_to_le64(owner);
392 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
Chris Mason74493f72007-12-11 09:25:06 -0500393 lenum = cpu_to_le64(owner_offset);
394 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
Chris Mason7bb86312007-12-11 09:25:06 -0500395#endif
Chris Mason74493f72007-12-11 09:25:06 -0500396 return ((u64)high_crc << 32) | (u64)low_crc;
397}
398
Chris Mason7bb86312007-12-11 09:25:06 -0500399static int match_extent_ref(struct extent_buffer *leaf,
400 struct btrfs_extent_ref *disk_ref,
401 struct btrfs_extent_ref *cpu_ref)
402{
403 int ret;
404 int len;
405
406 if (cpu_ref->objectid)
407 len = sizeof(*cpu_ref);
408 else
409 len = 2 * sizeof(u64);
410 ret = memcmp_extent_buffer(leaf, cpu_ref, (unsigned long)disk_ref,
411 len);
412 return ret == 0;
413}
414
Chris Mason98ed5172008-01-03 10:01:48 -0500415static int noinline lookup_extent_backref(struct btrfs_trans_handle *trans,
416 struct btrfs_root *root,
417 struct btrfs_path *path, u64 bytenr,
418 u64 root_objectid,
419 u64 ref_generation, u64 owner,
420 u64 owner_offset, int del)
Chris Mason7bb86312007-12-11 09:25:06 -0500421{
422 u64 hash;
423 struct btrfs_key key;
424 struct btrfs_key found_key;
425 struct btrfs_extent_ref ref;
426 struct extent_buffer *leaf;
427 struct btrfs_extent_ref *disk_ref;
428 int ret;
429 int ret2;
430
431 btrfs_set_stack_ref_root(&ref, root_objectid);
432 btrfs_set_stack_ref_generation(&ref, ref_generation);
433 btrfs_set_stack_ref_objectid(&ref, owner);
434 btrfs_set_stack_ref_offset(&ref, owner_offset);
435
436 hash = hash_extent_ref(root_objectid, ref_generation, owner,
437 owner_offset);
438 key.offset = hash;
439 key.objectid = bytenr;
440 key.type = BTRFS_EXTENT_REF_KEY;
441
442 while (1) {
443 ret = btrfs_search_slot(trans, root, &key, path,
444 del ? -1 : 0, del);
445 if (ret < 0)
446 goto out;
447 leaf = path->nodes[0];
448 if (ret != 0) {
449 u32 nritems = btrfs_header_nritems(leaf);
450 if (path->slots[0] >= nritems) {
451 ret2 = btrfs_next_leaf(root, path);
452 if (ret2)
453 goto out;
454 leaf = path->nodes[0];
455 }
456 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
457 if (found_key.objectid != bytenr ||
458 found_key.type != BTRFS_EXTENT_REF_KEY)
459 goto out;
460 key.offset = found_key.offset;
461 if (del) {
462 btrfs_release_path(root, path);
463 continue;
464 }
465 }
466 disk_ref = btrfs_item_ptr(path->nodes[0],
467 path->slots[0],
468 struct btrfs_extent_ref);
469 if (match_extent_ref(path->nodes[0], disk_ref, &ref)) {
470 ret = 0;
471 goto out;
472 }
473 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
474 key.offset = found_key.offset + 1;
475 btrfs_release_path(root, path);
476 }
477out:
478 return ret;
479}
480
Chris Masond8d5f3e2007-12-11 12:42:00 -0500481/*
482 * Back reference rules. Back refs have three main goals:
483 *
484 * 1) differentiate between all holders of references to an extent so that
485 * when a reference is dropped we can make sure it was a valid reference
486 * before freeing the extent.
487 *
488 * 2) Provide enough information to quickly find the holders of an extent
489 * if we notice a given block is corrupted or bad.
490 *
491 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
492 * maintenance. This is actually the same as #2, but with a slightly
493 * different use case.
494 *
495 * File extents can be referenced by:
496 *
497 * - multiple snapshots, subvolumes, or different generations in one subvol
498 * - different files inside a single subvolume (in theory, not implemented yet)
499 * - different offsets inside a file (bookend extents in file.c)
500 *
501 * The extent ref structure has fields for:
502 *
503 * - Objectid of the subvolume root
504 * - Generation number of the tree holding the reference
505 * - objectid of the file holding the reference
506 * - offset in the file corresponding to the key holding the reference
507 *
508 * When a file extent is allocated the fields are filled in:
509 * (root_key.objectid, trans->transid, inode objectid, offset in file)
510 *
511 * When a leaf is cow'd new references are added for every file extent found
512 * in the leaf. It looks the same as the create case, but trans->transid
513 * will be different when the block is cow'd.
514 *
515 * (root_key.objectid, trans->transid, inode objectid, offset in file)
516 *
517 * When a file extent is removed either during snapshot deletion or file
518 * truncation, the corresponding back reference is found
519 * by searching for:
520 *
521 * (btrfs_header_owner(leaf), btrfs_header_generation(leaf),
522 * inode objectid, offset in file)
523 *
524 * Btree extents can be referenced by:
525 *
526 * - Different subvolumes
527 * - Different generations of the same subvolume
528 *
529 * Storing sufficient information for a full reverse mapping of a btree
530 * block would require storing the lowest key of the block in the backref,
531 * and it would require updating that lowest key either before write out or
532 * every time it changed. Instead, the objectid of the lowest key is stored
533 * along with the level of the tree block. This provides a hint
534 * about where in the btree the block can be found. Searches through the
535 * btree only need to look for a pointer to that block, so they stop one
536 * level higher than the level recorded in the backref.
537 *
538 * Some btrees do not do reference counting on their extents. These
539 * include the extent tree and the tree of tree roots. Backrefs for these
540 * trees always have a generation of zero.
541 *
542 * When a tree block is created, back references are inserted:
543 *
Chris Masonf6dbff52007-12-13 11:13:32 -0500544 * (root->root_key.objectid, trans->transid or zero, level, lowest_key_objectid)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500545 *
546 * When a tree block is cow'd in a reference counted root,
547 * new back references are added for all the blocks it points to.
548 * These are of the form (trans->transid will have increased since creation):
549 *
Chris Masonf6dbff52007-12-13 11:13:32 -0500550 * (root->root_key.objectid, trans->transid, level, lowest_key_objectid)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500551 *
552 * Because the lowest_key_objectid and the level are just hints
553 * they are not used when backrefs are deleted. When a backref is deleted:
554 *
555 * if backref was for a tree root:
556 * root_objectid = root->root_key.objectid
557 * else
558 * root_objectid = btrfs_header_owner(parent)
559 *
560 * (root_objectid, btrfs_header_generation(parent) or zero, 0, 0)
561 *
562 * Back Reference Key hashing:
563 *
564 * Back references have four fields, each 64 bits long. Unfortunately,
565 * This is hashed into a single 64 bit number and placed into the key offset.
566 * The key objectid corresponds to the first byte in the extent, and the
567 * key type is set to BTRFS_EXTENT_REF_KEY
568 */
Chris Mason7bb86312007-12-11 09:25:06 -0500569int btrfs_insert_extent_backref(struct btrfs_trans_handle *trans,
570 struct btrfs_root *root,
571 struct btrfs_path *path, u64 bytenr,
572 u64 root_objectid, u64 ref_generation,
573 u64 owner, u64 owner_offset)
Chris Mason74493f72007-12-11 09:25:06 -0500574{
575 u64 hash;
576 struct btrfs_key key;
577 struct btrfs_extent_ref ref;
Chris Mason7bb86312007-12-11 09:25:06 -0500578 struct btrfs_extent_ref *disk_ref;
Chris Mason74493f72007-12-11 09:25:06 -0500579 int ret;
580
581 btrfs_set_stack_ref_root(&ref, root_objectid);
Chris Mason7bb86312007-12-11 09:25:06 -0500582 btrfs_set_stack_ref_generation(&ref, ref_generation);
Chris Mason74493f72007-12-11 09:25:06 -0500583 btrfs_set_stack_ref_objectid(&ref, owner);
584 btrfs_set_stack_ref_offset(&ref, owner_offset);
585
Chris Mason7bb86312007-12-11 09:25:06 -0500586 hash = hash_extent_ref(root_objectid, ref_generation, owner,
587 owner_offset);
Chris Mason74493f72007-12-11 09:25:06 -0500588 key.offset = hash;
589 key.objectid = bytenr;
590 key.type = BTRFS_EXTENT_REF_KEY;
591
592 ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(ref));
593 while (ret == -EEXIST) {
Chris Mason7bb86312007-12-11 09:25:06 -0500594 disk_ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
595 struct btrfs_extent_ref);
596 if (match_extent_ref(path->nodes[0], disk_ref, &ref))
597 goto out;
598 key.offset++;
599 btrfs_release_path(root, path);
600 ret = btrfs_insert_empty_item(trans, root, path, &key,
601 sizeof(ref));
Chris Mason74493f72007-12-11 09:25:06 -0500602 }
Chris Mason7bb86312007-12-11 09:25:06 -0500603 if (ret)
604 goto out;
605 disk_ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
606 struct btrfs_extent_ref);
607 write_extent_buffer(path->nodes[0], &ref, (unsigned long)disk_ref,
608 sizeof(ref));
609 btrfs_mark_buffer_dirty(path->nodes[0]);
610out:
611 btrfs_release_path(root, path);
612 return ret;
Chris Mason74493f72007-12-11 09:25:06 -0500613}
614
Chris Masonb18c6682007-04-17 13:26:50 -0400615int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
616 struct btrfs_root *root,
Chris Mason74493f72007-12-11 09:25:06 -0500617 u64 bytenr, u64 num_bytes,
Chris Mason7bb86312007-12-11 09:25:06 -0500618 u64 root_objectid, u64 ref_generation,
Chris Mason74493f72007-12-11 09:25:06 -0500619 u64 owner, u64 owner_offset)
Chris Mason02217ed2007-03-02 16:08:05 -0500620{
Chris Mason5caf2a02007-04-02 11:20:42 -0400621 struct btrfs_path *path;
Chris Mason02217ed2007-03-02 16:08:05 -0500622 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400623 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400624 struct extent_buffer *l;
Chris Mason234b63a2007-03-13 10:46:10 -0400625 struct btrfs_extent_item *item;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400626 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500627
Chris Masondb945352007-10-15 16:15:53 -0400628 WARN_ON(num_bytes < root->sectorsize);
Chris Mason5caf2a02007-04-02 11:20:42 -0400629 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -0400630 if (!path)
631 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -0400632
Chris Masonb0331a42008-01-08 15:46:31 -0500633 path->reada = 0;
Chris Masondb945352007-10-15 16:15:53 -0400634 key.objectid = bytenr;
Chris Mason62e27492007-03-15 12:56:47 -0400635 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masondb945352007-10-15 16:15:53 -0400636 key.offset = num_bytes;
Chris Mason5caf2a02007-04-02 11:20:42 -0400637 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -0400638 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400639 if (ret < 0)
640 return ret;
Chris Masona429e512007-04-18 16:15:28 -0400641 if (ret != 0) {
Chris Masona28ec192007-03-06 20:08:01 -0500642 BUG();
Chris Masona429e512007-04-18 16:15:28 -0400643 }
Chris Mason02217ed2007-03-02 16:08:05 -0500644 BUG_ON(ret != 0);
Chris Mason5f39d392007-10-15 16:14:19 -0400645 l = path->nodes[0];
Chris Mason5caf2a02007-04-02 11:20:42 -0400646 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400647 refs = btrfs_extent_refs(l, item);
648 btrfs_set_extent_refs(l, item, refs + 1);
Chris Mason5caf2a02007-04-02 11:20:42 -0400649 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masona28ec192007-03-06 20:08:01 -0500650
Chris Mason5caf2a02007-04-02 11:20:42 -0400651 btrfs_release_path(root->fs_info->extent_root, path);
Chris Mason7bb86312007-12-11 09:25:06 -0500652
Chris Masonb0331a42008-01-08 15:46:31 -0500653 path->reada = 0;
Chris Mason7bb86312007-12-11 09:25:06 -0500654 ret = btrfs_insert_extent_backref(trans, root->fs_info->extent_root,
655 path, bytenr, root_objectid,
656 ref_generation, owner, owner_offset);
657 BUG_ON(ret);
Chris Mason9f5fae22007-03-20 14:38:32 -0400658 finish_current_insert(trans, root->fs_info->extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -0400659 del_pending_extents(trans, root->fs_info->extent_root);
Chris Mason74493f72007-12-11 09:25:06 -0500660
661 btrfs_free_path(path);
Chris Mason02217ed2007-03-02 16:08:05 -0500662 return 0;
663}
664
Chris Masone9d0b132007-08-10 14:06:19 -0400665int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
666 struct btrfs_root *root)
667{
668 finish_current_insert(trans, root->fs_info->extent_root);
669 del_pending_extents(trans, root->fs_info->extent_root);
670 return 0;
671}
672
Chris Masonb18c6682007-04-17 13:26:50 -0400673static int lookup_extent_ref(struct btrfs_trans_handle *trans,
Chris Masondb945352007-10-15 16:15:53 -0400674 struct btrfs_root *root, u64 bytenr,
675 u64 num_bytes, u32 *refs)
Chris Masona28ec192007-03-06 20:08:01 -0500676{
Chris Mason5caf2a02007-04-02 11:20:42 -0400677 struct btrfs_path *path;
Chris Masona28ec192007-03-06 20:08:01 -0500678 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400679 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400680 struct extent_buffer *l;
Chris Mason234b63a2007-03-13 10:46:10 -0400681 struct btrfs_extent_item *item;
Chris Mason5caf2a02007-04-02 11:20:42 -0400682
Chris Masondb945352007-10-15 16:15:53 -0400683 WARN_ON(num_bytes < root->sectorsize);
Chris Mason5caf2a02007-04-02 11:20:42 -0400684 path = btrfs_alloc_path();
Chris Masonb0331a42008-01-08 15:46:31 -0500685 path->reada = 0;
Chris Masondb945352007-10-15 16:15:53 -0400686 key.objectid = bytenr;
687 key.offset = num_bytes;
Chris Mason62e27492007-03-15 12:56:47 -0400688 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Mason5caf2a02007-04-02 11:20:42 -0400689 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -0400690 0, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400691 if (ret < 0)
692 goto out;
Chris Mason5f39d392007-10-15 16:14:19 -0400693 if (ret != 0) {
694 btrfs_print_leaf(root, path->nodes[0]);
Chris Masondb945352007-10-15 16:15:53 -0400695 printk("failed to find block number %Lu\n", bytenr);
Chris Masona28ec192007-03-06 20:08:01 -0500696 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -0400697 }
698 l = path->nodes[0];
Chris Mason5caf2a02007-04-02 11:20:42 -0400699 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400700 *refs = btrfs_extent_refs(l, item);
Chris Mason54aa1f42007-06-22 14:16:25 -0400701out:
Chris Mason5caf2a02007-04-02 11:20:42 -0400702 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -0500703 return 0;
704}
705
Chris Masonbe20aa92007-12-17 20:14:01 -0500706u32 btrfs_count_snapshots_in_path(struct btrfs_root *root,
707 struct btrfs_path *count_path,
708 u64 first_extent)
709{
710 struct btrfs_root *extent_root = root->fs_info->extent_root;
711 struct btrfs_path *path;
712 u64 bytenr;
713 u64 found_objectid;
Chris Mason56b453c2008-01-03 09:08:27 -0500714 u64 root_objectid = root->root_key.objectid;
Chris Masonbe20aa92007-12-17 20:14:01 -0500715 u32 total_count = 0;
716 u32 cur_count;
Chris Masonbe20aa92007-12-17 20:14:01 -0500717 u32 nritems;
718 int ret;
719 struct btrfs_key key;
720 struct btrfs_key found_key;
721 struct extent_buffer *l;
722 struct btrfs_extent_item *item;
723 struct btrfs_extent_ref *ref_item;
724 int level = -1;
725
726 path = btrfs_alloc_path();
727again:
728 if (level == -1)
729 bytenr = first_extent;
730 else
731 bytenr = count_path->nodes[level]->start;
732
733 cur_count = 0;
734 key.objectid = bytenr;
735 key.offset = 0;
736
737 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
738 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
739 if (ret < 0)
740 goto out;
741 BUG_ON(ret == 0);
742
743 l = path->nodes[0];
744 btrfs_item_key_to_cpu(l, &found_key, path->slots[0]);
745
746 if (found_key.objectid != bytenr ||
747 found_key.type != BTRFS_EXTENT_ITEM_KEY) {
748 goto out;
749 }
750
751 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Masonbe20aa92007-12-17 20:14:01 -0500752 while (1) {
Chris Masonbd098352008-01-03 13:23:19 -0500753 l = path->nodes[0];
Chris Masonbe20aa92007-12-17 20:14:01 -0500754 nritems = btrfs_header_nritems(l);
755 if (path->slots[0] >= nritems) {
756 ret = btrfs_next_leaf(extent_root, path);
757 if (ret == 0)
758 continue;
759 break;
760 }
761 btrfs_item_key_to_cpu(l, &found_key, path->slots[0]);
762 if (found_key.objectid != bytenr)
763 break;
Chris Masonbd098352008-01-03 13:23:19 -0500764
Chris Masonbe20aa92007-12-17 20:14:01 -0500765 if (found_key.type != BTRFS_EXTENT_REF_KEY) {
766 path->slots[0]++;
767 continue;
768 }
769
770 cur_count++;
771 ref_item = btrfs_item_ptr(l, path->slots[0],
772 struct btrfs_extent_ref);
773 found_objectid = btrfs_ref_root(l, ref_item);
774
Chris Mason56b453c2008-01-03 09:08:27 -0500775 if (found_objectid != root_objectid) {
776 total_count = 2;
Chris Mason4313b392008-01-03 09:08:48 -0500777 goto out;
Chris Mason56b453c2008-01-03 09:08:27 -0500778 }
779 total_count = 1;
Chris Masonbe20aa92007-12-17 20:14:01 -0500780 path->slots[0]++;
781 }
782 if (cur_count == 0) {
783 total_count = 0;
784 goto out;
785 }
Chris Masonbe20aa92007-12-17 20:14:01 -0500786 if (level >= 0 && root->node == count_path->nodes[level])
787 goto out;
788 level++;
789 btrfs_release_path(root, path);
790 goto again;
791
792out:
793 btrfs_free_path(path);
794 return total_count;
Chris Masonbe20aa92007-12-17 20:14:01 -0500795}
Chris Masonc5739bb2007-04-10 09:27:04 -0400796int btrfs_inc_root_ref(struct btrfs_trans_handle *trans,
Chris Mason7bb86312007-12-11 09:25:06 -0500797 struct btrfs_root *root, u64 owner_objectid)
Chris Masonc5739bb2007-04-10 09:27:04 -0400798{
Chris Mason7bb86312007-12-11 09:25:06 -0500799 u64 generation;
800 u64 key_objectid;
801 u64 level;
802 u32 nritems;
803 struct btrfs_disk_key disk_key;
804
805 level = btrfs_header_level(root->node);
806 generation = trans->transid;
807 nritems = btrfs_header_nritems(root->node);
808 if (nritems > 0) {
809 if (level == 0)
810 btrfs_item_key(root->node, &disk_key, 0);
811 else
812 btrfs_node_key(root->node, &disk_key, 0);
813 key_objectid = btrfs_disk_key_objectid(&disk_key);
814 } else {
815 key_objectid = 0;
816 }
Chris Masondb945352007-10-15 16:15:53 -0400817 return btrfs_inc_extent_ref(trans, root, root->node->start,
Chris Mason7bb86312007-12-11 09:25:06 -0500818 root->node->len, owner_objectid,
Chris Masonf6dbff52007-12-13 11:13:32 -0500819 generation, level, key_objectid);
Chris Masonc5739bb2007-04-10 09:27:04 -0400820}
821
Chris Masone089f052007-03-16 16:20:31 -0400822int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400823 struct extent_buffer *buf)
Chris Mason02217ed2007-03-02 16:08:05 -0500824{
Chris Masondb945352007-10-15 16:15:53 -0400825 u64 bytenr;
Chris Mason5f39d392007-10-15 16:14:19 -0400826 u32 nritems;
827 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -0400828 struct btrfs_file_extent_item *fi;
Chris Mason02217ed2007-03-02 16:08:05 -0500829 int i;
Chris Masondb945352007-10-15 16:15:53 -0400830 int level;
Chris Mason6407bf62007-03-27 06:33:00 -0400831 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -0400832 int faili;
Chris Masona28ec192007-03-06 20:08:01 -0500833
Chris Mason3768f362007-03-13 16:47:54 -0400834 if (!root->ref_cows)
Chris Masona28ec192007-03-06 20:08:01 -0500835 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400836
Chris Masondb945352007-10-15 16:15:53 -0400837 level = btrfs_header_level(buf);
Chris Mason5f39d392007-10-15 16:14:19 -0400838 nritems = btrfs_header_nritems(buf);
839 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -0400840 if (level == 0) {
841 u64 disk_bytenr;
Chris Mason5f39d392007-10-15 16:14:19 -0400842 btrfs_item_key_to_cpu(buf, &key, i);
843 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason6407bf62007-03-27 06:33:00 -0400844 continue;
Chris Mason5f39d392007-10-15 16:14:19 -0400845 fi = btrfs_item_ptr(buf, i,
Chris Mason6407bf62007-03-27 06:33:00 -0400846 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400847 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason236454df2007-04-19 13:37:44 -0400848 BTRFS_FILE_EXTENT_INLINE)
849 continue;
Chris Masondb945352007-10-15 16:15:53 -0400850 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
851 if (disk_bytenr == 0)
Chris Mason3a686372007-05-24 13:35:57 -0400852 continue;
Chris Masondb945352007-10-15 16:15:53 -0400853 ret = btrfs_inc_extent_ref(trans, root, disk_bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -0500854 btrfs_file_extent_disk_num_bytes(buf, fi),
855 root->root_key.objectid, trans->transid,
856 key.objectid, key.offset);
Chris Mason54aa1f42007-06-22 14:16:25 -0400857 if (ret) {
858 faili = i;
859 goto fail;
860 }
Chris Mason6407bf62007-03-27 06:33:00 -0400861 } else {
Chris Masondb945352007-10-15 16:15:53 -0400862 bytenr = btrfs_node_blockptr(buf, i);
Chris Mason6caab482007-12-13 09:48:07 -0500863 btrfs_node_key_to_cpu(buf, &key, i);
Chris Masondb945352007-10-15 16:15:53 -0400864 ret = btrfs_inc_extent_ref(trans, root, bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -0500865 btrfs_level_size(root, level - 1),
866 root->root_key.objectid,
Chris Masonf6dbff52007-12-13 11:13:32 -0500867 trans->transid,
868 level - 1, key.objectid);
Chris Mason54aa1f42007-06-22 14:16:25 -0400869 if (ret) {
870 faili = i;
871 goto fail;
872 }
Chris Mason6407bf62007-03-27 06:33:00 -0400873 }
Chris Mason02217ed2007-03-02 16:08:05 -0500874 }
875 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -0400876fail:
Chris Masonccd467d2007-06-28 15:57:36 -0400877 WARN_ON(1);
Chris Mason7bb86312007-12-11 09:25:06 -0500878#if 0
Chris Mason54aa1f42007-06-22 14:16:25 -0400879 for (i =0; i < faili; i++) {
Chris Masondb945352007-10-15 16:15:53 -0400880 if (level == 0) {
881 u64 disk_bytenr;
Chris Mason5f39d392007-10-15 16:14:19 -0400882 btrfs_item_key_to_cpu(buf, &key, i);
883 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -0400884 continue;
Chris Mason5f39d392007-10-15 16:14:19 -0400885 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -0400886 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400887 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -0400888 BTRFS_FILE_EXTENT_INLINE)
889 continue;
Chris Masondb945352007-10-15 16:15:53 -0400890 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
891 if (disk_bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -0400892 continue;
Chris Masondb945352007-10-15 16:15:53 -0400893 err = btrfs_free_extent(trans, root, disk_bytenr,
894 btrfs_file_extent_disk_num_bytes(buf,
Chris Mason5f39d392007-10-15 16:14:19 -0400895 fi), 0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400896 BUG_ON(err);
897 } else {
Chris Masondb945352007-10-15 16:15:53 -0400898 bytenr = btrfs_node_blockptr(buf, i);
899 err = btrfs_free_extent(trans, root, bytenr,
900 btrfs_level_size(root, level - 1), 0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400901 BUG_ON(err);
902 }
903 }
Chris Mason7bb86312007-12-11 09:25:06 -0500904#endif
Chris Mason54aa1f42007-06-22 14:16:25 -0400905 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -0500906}
907
Chris Mason9078a3e2007-04-26 16:46:15 -0400908static int write_one_cache_group(struct btrfs_trans_handle *trans,
909 struct btrfs_root *root,
910 struct btrfs_path *path,
911 struct btrfs_block_group_cache *cache)
912{
913 int ret;
914 int pending_ret;
915 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -0400916 unsigned long bi;
917 struct extent_buffer *leaf;
Chris Mason9078a3e2007-04-26 16:46:15 -0400918
Chris Mason9078a3e2007-04-26 16:46:15 -0400919 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400920 if (ret < 0)
921 goto fail;
Chris Mason9078a3e2007-04-26 16:46:15 -0400922 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -0400923
924 leaf = path->nodes[0];
925 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
926 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
927 btrfs_mark_buffer_dirty(leaf);
Chris Mason9078a3e2007-04-26 16:46:15 -0400928 btrfs_release_path(extent_root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -0400929fail:
Chris Mason9078a3e2007-04-26 16:46:15 -0400930 finish_current_insert(trans, extent_root);
931 pending_ret = del_pending_extents(trans, extent_root);
932 if (ret)
933 return ret;
934 if (pending_ret)
935 return pending_ret;
936 return 0;
937
938}
939
Chris Mason96b51792007-10-15 16:15:19 -0400940int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
941 struct btrfs_root *root)
Chris Mason9078a3e2007-04-26 16:46:15 -0400942{
Chris Mason96b51792007-10-15 16:15:19 -0400943 struct extent_map_tree *block_group_cache;
944 struct btrfs_block_group_cache *cache;
Chris Mason9078a3e2007-04-26 16:46:15 -0400945 int ret;
946 int err = 0;
947 int werr = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -0400948 struct btrfs_path *path;
Chris Mason96b51792007-10-15 16:15:19 -0400949 u64 last = 0;
950 u64 start;
951 u64 end;
952 u64 ptr;
Chris Mason9078a3e2007-04-26 16:46:15 -0400953
Chris Mason96b51792007-10-15 16:15:19 -0400954 block_group_cache = &root->fs_info->block_group_cache;
Chris Mason9078a3e2007-04-26 16:46:15 -0400955 path = btrfs_alloc_path();
956 if (!path)
957 return -ENOMEM;
958
959 while(1) {
Chris Mason96b51792007-10-15 16:15:19 -0400960 ret = find_first_extent_bit(block_group_cache, last,
961 &start, &end, BLOCK_GROUP_DIRTY);
962 if (ret)
Chris Mason9078a3e2007-04-26 16:46:15 -0400963 break;
Chris Mason54aa1f42007-06-22 14:16:25 -0400964
Chris Mason96b51792007-10-15 16:15:19 -0400965 last = end + 1;
966 ret = get_state_private(block_group_cache, start, &ptr);
967 if (ret)
968 break;
969
Jens Axboeae2f5412007-10-19 09:22:59 -0400970 cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
Chris Mason96b51792007-10-15 16:15:19 -0400971 err = write_one_cache_group(trans, root,
972 path, cache);
973 /*
974 * if we fail to write the cache group, we want
975 * to keep it marked dirty in hopes that a later
976 * write will work
977 */
978 if (err) {
979 werr = err;
980 continue;
Chris Mason9078a3e2007-04-26 16:46:15 -0400981 }
Chris Mason96b51792007-10-15 16:15:19 -0400982 clear_extent_bits(block_group_cache, start, end,
983 BLOCK_GROUP_DIRTY, GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -0400984 }
985 btrfs_free_path(path);
986 return werr;
987}
988
989static int update_block_group(struct btrfs_trans_handle *trans,
990 struct btrfs_root *root,
Chris Masondb945352007-10-15 16:15:53 -0400991 u64 bytenr, u64 num_bytes, int alloc,
992 int mark_free, int data)
Chris Mason9078a3e2007-04-26 16:46:15 -0400993{
994 struct btrfs_block_group_cache *cache;
995 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -0400996 u64 total = num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -0400997 u64 old_val;
Chris Masondb945352007-10-15 16:15:53 -0400998 u64 byte_in_group;
Chris Mason96b51792007-10-15 16:15:19 -0400999 u64 start;
1000 u64 end;
Chris Mason3e1ad542007-05-07 20:03:49 -04001001
Chris Mason9078a3e2007-04-26 16:46:15 -04001002 while(total) {
Chris Masondb945352007-10-15 16:15:53 -04001003 cache = btrfs_lookup_block_group(info, bytenr);
Chris Mason3e1ad542007-05-07 20:03:49 -04001004 if (!cache) {
Chris Mason9078a3e2007-04-26 16:46:15 -04001005 return -1;
Chris Masoncd1bc462007-04-27 10:08:34 -04001006 }
Chris Masondb945352007-10-15 16:15:53 -04001007 byte_in_group = bytenr - cache->key.objectid;
1008 WARN_ON(byte_in_group > cache->key.offset);
Chris Mason96b51792007-10-15 16:15:19 -04001009 start = cache->key.objectid;
1010 end = start + cache->key.offset - 1;
1011 set_extent_bits(&info->block_group_cache, start, end,
1012 BLOCK_GROUP_DIRTY, GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -04001013
1014 old_val = btrfs_block_group_used(&cache->item);
Chris Masondb945352007-10-15 16:15:53 -04001015 num_bytes = min(total, cache->key.offset - byte_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -04001016 if (alloc) {
Chris Mason1e2677e2007-05-29 16:52:18 -04001017 if (cache->data != data &&
Chris Mason84f54cf2007-06-12 07:43:08 -04001018 old_val < (cache->key.offset >> 1)) {
Chris Mason96b51792007-10-15 16:15:19 -04001019 int bit_to_clear;
1020 int bit_to_set;
Chris Mason96b51792007-10-15 16:15:19 -04001021 cache->data = data;
Chris Mason1e2677e2007-05-29 16:52:18 -04001022 if (data) {
Yanb97f9202007-11-01 11:28:41 -04001023 bit_to_clear = BLOCK_GROUP_METADATA;
1024 bit_to_set = BLOCK_GROUP_DATA;
Chris Masonf84a8b32007-11-06 10:26:29 -05001025 cache->item.flags &=
1026 ~BTRFS_BLOCK_GROUP_MIXED;
Chris Mason1e2677e2007-05-29 16:52:18 -04001027 cache->item.flags |=
1028 BTRFS_BLOCK_GROUP_DATA;
1029 } else {
Yanb97f9202007-11-01 11:28:41 -04001030 bit_to_clear = BLOCK_GROUP_DATA;
1031 bit_to_set = BLOCK_GROUP_METADATA;
Chris Mason1e2677e2007-05-29 16:52:18 -04001032 cache->item.flags &=
Chris Masonf84a8b32007-11-06 10:26:29 -05001033 ~BTRFS_BLOCK_GROUP_MIXED;
1034 cache->item.flags &=
Chris Mason1e2677e2007-05-29 16:52:18 -04001035 ~BTRFS_BLOCK_GROUP_DATA;
1036 }
Chris Mason96b51792007-10-15 16:15:19 -04001037 clear_extent_bits(&info->block_group_cache,
1038 start, end, bit_to_clear,
1039 GFP_NOFS);
1040 set_extent_bits(&info->block_group_cache,
1041 start, end, bit_to_set,
1042 GFP_NOFS);
Chris Masonf84a8b32007-11-06 10:26:29 -05001043 } else if (cache->data != data &&
1044 cache->data != BTRFS_BLOCK_GROUP_MIXED) {
1045 cache->data = BTRFS_BLOCK_GROUP_MIXED;
1046 set_extent_bits(&info->block_group_cache,
1047 start, end,
1048 BLOCK_GROUP_DATA |
1049 BLOCK_GROUP_METADATA,
1050 GFP_NOFS);
Chris Mason1e2677e2007-05-29 16:52:18 -04001051 }
Chris Masondb945352007-10-15 16:15:53 -04001052 old_val += num_bytes;
Chris Masoncd1bc462007-04-27 10:08:34 -04001053 } else {
Chris Masondb945352007-10-15 16:15:53 -04001054 old_val -= num_bytes;
Chris Masonf510cfe2007-10-15 16:14:48 -04001055 if (mark_free) {
1056 set_extent_dirty(&info->free_space_cache,
Chris Masondb945352007-10-15 16:15:53 -04001057 bytenr, bytenr + num_bytes - 1,
Chris Masonf510cfe2007-10-15 16:14:48 -04001058 GFP_NOFS);
Chris Masone37c9e62007-05-09 20:13:14 -04001059 }
Chris Masoncd1bc462007-04-27 10:08:34 -04001060 }
Chris Mason9078a3e2007-04-26 16:46:15 -04001061 btrfs_set_block_group_used(&cache->item, old_val);
Chris Masondb945352007-10-15 16:15:53 -04001062 total -= num_bytes;
1063 bytenr += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04001064 }
1065 return 0;
1066}
Yan324ae4d2007-11-16 14:57:08 -05001067static int update_pinned_extents(struct btrfs_root *root,
1068 u64 bytenr, u64 num, int pin)
1069{
1070 u64 len;
1071 struct btrfs_block_group_cache *cache;
1072 struct btrfs_fs_info *fs_info = root->fs_info;
1073
1074 if (pin) {
1075 set_extent_dirty(&fs_info->pinned_extents,
1076 bytenr, bytenr + num - 1, GFP_NOFS);
1077 } else {
1078 clear_extent_dirty(&fs_info->pinned_extents,
1079 bytenr, bytenr + num - 1, GFP_NOFS);
1080 }
1081 while (num > 0) {
1082 cache = btrfs_lookup_block_group(fs_info, bytenr);
1083 WARN_ON(!cache);
1084 len = min(num, cache->key.offset -
1085 (bytenr - cache->key.objectid));
1086 if (pin) {
1087 cache->pinned += len;
1088 fs_info->total_pinned += len;
1089 } else {
1090 cache->pinned -= len;
1091 fs_info->total_pinned -= len;
1092 }
1093 bytenr += len;
1094 num -= len;
1095 }
1096 return 0;
1097}
Chris Mason9078a3e2007-04-26 16:46:15 -04001098
Chris Mason1a5bc162007-10-15 16:15:26 -04001099int btrfs_copy_pinned(struct btrfs_root *root, struct extent_map_tree *copy)
Chris Masonccd467d2007-06-28 15:57:36 -04001100{
Chris Masonccd467d2007-06-28 15:57:36 -04001101 u64 last = 0;
Chris Mason1a5bc162007-10-15 16:15:26 -04001102 u64 start;
1103 u64 end;
1104 struct extent_map_tree *pinned_extents = &root->fs_info->pinned_extents;
Chris Masonccd467d2007-06-28 15:57:36 -04001105 int ret;
Chris Masonccd467d2007-06-28 15:57:36 -04001106
1107 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04001108 ret = find_first_extent_bit(pinned_extents, last,
1109 &start, &end, EXTENT_DIRTY);
1110 if (ret)
Chris Masonccd467d2007-06-28 15:57:36 -04001111 break;
Chris Mason1a5bc162007-10-15 16:15:26 -04001112 set_extent_dirty(copy, start, end, GFP_NOFS);
1113 last = end + 1;
Chris Masonccd467d2007-06-28 15:57:36 -04001114 }
1115 return 0;
1116}
1117
1118int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
1119 struct btrfs_root *root,
Chris Mason1a5bc162007-10-15 16:15:26 -04001120 struct extent_map_tree *unpin)
Chris Masona28ec192007-03-06 20:08:01 -05001121{
Chris Mason1a5bc162007-10-15 16:15:26 -04001122 u64 start;
1123 u64 end;
Chris Masona28ec192007-03-06 20:08:01 -05001124 int ret;
Chris Masonf510cfe2007-10-15 16:14:48 -04001125 struct extent_map_tree *free_space_cache;
Chris Masonf510cfe2007-10-15 16:14:48 -04001126 free_space_cache = &root->fs_info->free_space_cache;
Chris Masona28ec192007-03-06 20:08:01 -05001127
1128 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04001129 ret = find_first_extent_bit(unpin, 0, &start, &end,
1130 EXTENT_DIRTY);
1131 if (ret)
Chris Masona28ec192007-03-06 20:08:01 -05001132 break;
Yan324ae4d2007-11-16 14:57:08 -05001133 update_pinned_extents(root, start, end + 1 - start, 0);
Chris Mason1a5bc162007-10-15 16:15:26 -04001134 clear_extent_dirty(unpin, start, end, GFP_NOFS);
1135 set_extent_dirty(free_space_cache, start, end, GFP_NOFS);
Chris Masona28ec192007-03-06 20:08:01 -05001136 }
1137 return 0;
1138}
1139
Chris Mason98ed5172008-01-03 10:01:48 -05001140static int finish_current_insert(struct btrfs_trans_handle *trans,
1141 struct btrfs_root *extent_root)
Chris Mason037e6392007-03-07 11:50:24 -05001142{
Chris Mason7bb86312007-12-11 09:25:06 -05001143 u64 start;
1144 u64 end;
1145 struct btrfs_fs_info *info = extent_root->fs_info;
Chris Masond8d5f3e2007-12-11 12:42:00 -05001146 struct extent_buffer *eb;
Chris Mason7bb86312007-12-11 09:25:06 -05001147 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -04001148 struct btrfs_key ins;
Chris Masond8d5f3e2007-12-11 12:42:00 -05001149 struct btrfs_disk_key first;
Chris Mason234b63a2007-03-13 10:46:10 -04001150 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -05001151 int ret;
Chris Masond8d5f3e2007-12-11 12:42:00 -05001152 int level;
Chris Mason1a5bc162007-10-15 16:15:26 -04001153 int err = 0;
Chris Mason037e6392007-03-07 11:50:24 -05001154
Chris Mason5f39d392007-10-15 16:14:19 -04001155 btrfs_set_stack_extent_refs(&extent_item, 1);
Chris Mason62e27492007-03-15 12:56:47 -04001156 btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
Chris Mason7bb86312007-12-11 09:25:06 -05001157 path = btrfs_alloc_path();
Chris Mason037e6392007-03-07 11:50:24 -05001158
Chris Mason26b80032007-08-08 20:17:12 -04001159 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04001160 ret = find_first_extent_bit(&info->extent_ins, 0, &start,
1161 &end, EXTENT_LOCKED);
1162 if (ret)
Chris Mason26b80032007-08-08 20:17:12 -04001163 break;
1164
Chris Mason1a5bc162007-10-15 16:15:26 -04001165 ins.objectid = start;
1166 ins.offset = end + 1 - start;
1167 err = btrfs_insert_item(trans, extent_root, &ins,
1168 &extent_item, sizeof(extent_item));
1169 clear_extent_bits(&info->extent_ins, start, end, EXTENT_LOCKED,
1170 GFP_NOFS);
Chris Masond8d5f3e2007-12-11 12:42:00 -05001171 eb = read_tree_block(extent_root, ins.objectid, ins.offset);
1172 level = btrfs_header_level(eb);
1173 if (level == 0) {
1174 btrfs_item_key(eb, &first, 0);
1175 } else {
1176 btrfs_node_key(eb, &first, 0);
1177 }
Chris Mason7bb86312007-12-11 09:25:06 -05001178 err = btrfs_insert_extent_backref(trans, extent_root, path,
1179 start, extent_root->root_key.objectid,
Chris Masonf6dbff52007-12-13 11:13:32 -05001180 0, level,
1181 btrfs_disk_key_objectid(&first));
Chris Mason7bb86312007-12-11 09:25:06 -05001182 BUG_ON(err);
Chris Masond8d5f3e2007-12-11 12:42:00 -05001183 free_extent_buffer(eb);
Chris Mason037e6392007-03-07 11:50:24 -05001184 }
Chris Mason7bb86312007-12-11 09:25:06 -05001185 btrfs_free_path(path);
Chris Mason037e6392007-03-07 11:50:24 -05001186 return 0;
1187}
1188
Chris Masondb945352007-10-15 16:15:53 -04001189static int pin_down_bytes(struct btrfs_root *root, u64 bytenr, u32 num_bytes,
1190 int pending)
Chris Masone20d96d2007-03-22 12:13:20 -04001191{
Chris Mason1a5bc162007-10-15 16:15:26 -04001192 int err = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001193 struct extent_buffer *buf;
Chris Mason78fae272007-03-25 11:35:08 -04001194
Chris Masonf4b9aa82007-03-27 11:05:53 -04001195 if (!pending) {
Chris Masondb945352007-10-15 16:15:53 -04001196 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
Chris Mason5f39d392007-10-15 16:14:19 -04001197 if (buf) {
1198 if (btrfs_buffer_uptodate(buf)) {
Chris Mason2c90e5d2007-04-02 10:50:19 -04001199 u64 transid =
1200 root->fs_info->running_transaction->transid;
Chris Masondc17ff82008-01-08 15:46:30 -05001201 u64 header_transid =
1202 btrfs_header_generation(buf);
1203 if (header_transid == transid) {
Chris Mason5f39d392007-10-15 16:14:19 -04001204 free_extent_buffer(buf);
Yanc5492282007-11-06 10:25:25 -05001205 return 1;
Chris Mason2c90e5d2007-04-02 10:50:19 -04001206 }
Chris Masonf4b9aa82007-03-27 11:05:53 -04001207 }
Chris Mason5f39d392007-10-15 16:14:19 -04001208 free_extent_buffer(buf);
Chris Mason8ef97622007-03-26 10:15:30 -04001209 }
Yan324ae4d2007-11-16 14:57:08 -05001210 update_pinned_extents(root, bytenr, num_bytes, 1);
Chris Masonf4b9aa82007-03-27 11:05:53 -04001211 } else {
Chris Mason1a5bc162007-10-15 16:15:26 -04001212 set_extent_bits(&root->fs_info->pending_del,
Chris Masondb945352007-10-15 16:15:53 -04001213 bytenr, bytenr + num_bytes - 1,
1214 EXTENT_LOCKED, GFP_NOFS);
Chris Masonf4b9aa82007-03-27 11:05:53 -04001215 }
Chris Masonbe744172007-05-06 10:15:01 -04001216 BUG_ON(err < 0);
Chris Masone20d96d2007-03-22 12:13:20 -04001217 return 0;
1218}
1219
Chris Masona28ec192007-03-06 20:08:01 -05001220/*
1221 * remove an extent from the root, returns 0 on success
1222 */
Chris Masone089f052007-03-16 16:20:31 -04001223static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason7bb86312007-12-11 09:25:06 -05001224 *root, u64 bytenr, u64 num_bytes,
1225 u64 root_objectid, u64 ref_generation,
1226 u64 owner_objectid, u64 owner_offset, int pin,
Chris Masone37c9e62007-05-09 20:13:14 -04001227 int mark_free)
Chris Masona28ec192007-03-06 20:08:01 -05001228{
Chris Mason5caf2a02007-04-02 11:20:42 -04001229 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -04001230 struct btrfs_key key;
Chris Mason1261ec42007-03-20 20:35:03 -04001231 struct btrfs_fs_info *info = root->fs_info;
1232 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04001233 struct extent_buffer *leaf;
Chris Masona28ec192007-03-06 20:08:01 -05001234 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -04001235 struct btrfs_extent_item *ei;
Chris Masoncf27e1e2007-03-13 09:49:06 -04001236 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -05001237
Chris Masondb945352007-10-15 16:15:53 -04001238 key.objectid = bytenr;
Chris Mason62e27492007-03-15 12:56:47 -04001239 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masondb945352007-10-15 16:15:53 -04001240 key.offset = num_bytes;
Chris Masona28ec192007-03-06 20:08:01 -05001241
Chris Mason5caf2a02007-04-02 11:20:42 -04001242 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04001243 if (!path)
1244 return -ENOMEM;
1245
Chris Masonb0331a42008-01-08 15:46:31 -05001246 path->reada = 0;
Chris Mason7bb86312007-12-11 09:25:06 -05001247 ret = lookup_extent_backref(trans, extent_root, path,
1248 bytenr, root_objectid,
1249 ref_generation,
1250 owner_objectid, owner_offset, 1);
1251 if (ret == 0) {
1252 ret = btrfs_del_item(trans, extent_root, path);
1253 } else {
1254 btrfs_print_leaf(extent_root, path->nodes[0]);
1255 WARN_ON(1);
1256 printk("Unable to find ref byte nr %Lu root %Lu "
1257 " gen %Lu owner %Lu offset %Lu\n", bytenr,
1258 root_objectid, ref_generation, owner_objectid,
1259 owner_offset);
1260 }
1261 btrfs_release_path(extent_root, path);
Chris Mason5caf2a02007-04-02 11:20:42 -04001262 ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001263 if (ret < 0)
1264 return ret;
1265 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04001266
1267 leaf = path->nodes[0];
1268 ei = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason123abc82007-03-14 14:14:43 -04001269 struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001270 refs = btrfs_extent_refs(leaf, ei);
1271 BUG_ON(refs == 0);
1272 refs -= 1;
1273 btrfs_set_extent_refs(leaf, ei, refs);
1274 btrfs_mark_buffer_dirty(leaf);
1275
Chris Masoncf27e1e2007-03-13 09:49:06 -04001276 if (refs == 0) {
Chris Masondb945352007-10-15 16:15:53 -04001277 u64 super_used;
1278 u64 root_used;
Chris Mason78fae272007-03-25 11:35:08 -04001279
1280 if (pin) {
Chris Masondb945352007-10-15 16:15:53 -04001281 ret = pin_down_bytes(root, bytenr, num_bytes, 0);
Yanc5492282007-11-06 10:25:25 -05001282 if (ret > 0)
1283 mark_free = 1;
1284 BUG_ON(ret < 0);
Chris Mason78fae272007-03-25 11:35:08 -04001285 }
1286
Josef Bacik58176a92007-08-29 15:47:34 -04001287 /* block accounting for super block */
Chris Masondb945352007-10-15 16:15:53 -04001288 super_used = btrfs_super_bytes_used(&info->super_copy);
1289 btrfs_set_super_bytes_used(&info->super_copy,
1290 super_used - num_bytes);
Josef Bacik58176a92007-08-29 15:47:34 -04001291
1292 /* block accounting for root item */
Chris Masondb945352007-10-15 16:15:53 -04001293 root_used = btrfs_root_used(&root->root_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001294 btrfs_set_root_used(&root->root_item,
Chris Masondb945352007-10-15 16:15:53 -04001295 root_used - num_bytes);
Josef Bacik58176a92007-08-29 15:47:34 -04001296
Chris Mason5caf2a02007-04-02 11:20:42 -04001297 ret = btrfs_del_item(trans, extent_root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -04001298 if (ret) {
1299 return ret;
1300 }
Chris Masondb945352007-10-15 16:15:53 -04001301 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
Chris Mason1e2677e2007-05-29 16:52:18 -04001302 mark_free, 0);
Chris Mason9078a3e2007-04-26 16:46:15 -04001303 BUG_ON(ret);
Chris Masona28ec192007-03-06 20:08:01 -05001304 }
Chris Mason5caf2a02007-04-02 11:20:42 -04001305 btrfs_free_path(path);
Chris Masone089f052007-03-16 16:20:31 -04001306 finish_current_insert(trans, extent_root);
Chris Masona28ec192007-03-06 20:08:01 -05001307 return ret;
1308}
1309
1310/*
Chris Masonfec577f2007-02-26 10:40:21 -05001311 * find all the blocks marked as pending in the radix tree and remove
1312 * them from the extent map
1313 */
Chris Masone089f052007-03-16 16:20:31 -04001314static int del_pending_extents(struct btrfs_trans_handle *trans, struct
1315 btrfs_root *extent_root)
Chris Masonfec577f2007-02-26 10:40:21 -05001316{
1317 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -04001318 int err = 0;
Chris Mason1a5bc162007-10-15 16:15:26 -04001319 u64 start;
1320 u64 end;
1321 struct extent_map_tree *pending_del;
1322 struct extent_map_tree *pinned_extents;
Chris Mason8ef97622007-03-26 10:15:30 -04001323
Chris Mason1a5bc162007-10-15 16:15:26 -04001324 pending_del = &extent_root->fs_info->pending_del;
1325 pinned_extents = &extent_root->fs_info->pinned_extents;
Chris Masonfec577f2007-02-26 10:40:21 -05001326
1327 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04001328 ret = find_first_extent_bit(pending_del, 0, &start, &end,
1329 EXTENT_LOCKED);
1330 if (ret)
Chris Masonfec577f2007-02-26 10:40:21 -05001331 break;
Yan324ae4d2007-11-16 14:57:08 -05001332 update_pinned_extents(extent_root, start, end + 1 - start, 1);
Chris Mason1a5bc162007-10-15 16:15:26 -04001333 clear_extent_bits(pending_del, start, end, EXTENT_LOCKED,
1334 GFP_NOFS);
1335 ret = __free_extent(trans, extent_root,
Chris Mason7bb86312007-12-11 09:25:06 -05001336 start, end + 1 - start,
1337 extent_root->root_key.objectid,
1338 0, 0, 0, 0, 0);
Chris Mason1a5bc162007-10-15 16:15:26 -04001339 if (ret)
1340 err = ret;
Chris Masonfec577f2007-02-26 10:40:21 -05001341 }
Chris Masone20d96d2007-03-22 12:13:20 -04001342 return err;
Chris Masonfec577f2007-02-26 10:40:21 -05001343}
1344
1345/*
1346 * remove an extent from the root, returns 0 on success
1347 */
Chris Masone089f052007-03-16 16:20:31 -04001348int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason7bb86312007-12-11 09:25:06 -05001349 *root, u64 bytenr, u64 num_bytes,
1350 u64 root_objectid, u64 ref_generation,
1351 u64 owner_objectid, u64 owner_offset, int pin)
Chris Masonfec577f2007-02-26 10:40:21 -05001352{
Chris Mason9f5fae22007-03-20 14:38:32 -04001353 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Masonfec577f2007-02-26 10:40:21 -05001354 int pending_ret;
1355 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05001356
Chris Masondb945352007-10-15 16:15:53 -04001357 WARN_ON(num_bytes < root->sectorsize);
Chris Mason7bb86312007-12-11 09:25:06 -05001358 if (!root->ref_cows)
1359 ref_generation = 0;
1360
Chris Masona28ec192007-03-06 20:08:01 -05001361 if (root == extent_root) {
Chris Masondb945352007-10-15 16:15:53 -04001362 pin_down_bytes(root, bytenr, num_bytes, 1);
Chris Masona28ec192007-03-06 20:08:01 -05001363 return 0;
1364 }
Chris Mason7bb86312007-12-11 09:25:06 -05001365 ret = __free_extent(trans, root, bytenr, num_bytes, root_objectid,
1366 ref_generation, owner_objectid, owner_offset,
1367 pin, pin == 0);
Chris Masone20d96d2007-03-22 12:13:20 -04001368 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
Chris Masonfec577f2007-02-26 10:40:21 -05001369 return ret ? ret : pending_ret;
1370}
1371
Chris Mason87ee04e2007-11-30 11:30:34 -05001372static u64 stripe_align(struct btrfs_root *root, u64 val)
1373{
1374 u64 mask = ((u64)root->stripesize - 1);
1375 u64 ret = (val + mask) & ~mask;
1376 return ret;
1377}
1378
Chris Masonfec577f2007-02-26 10:40:21 -05001379/*
1380 * walks the btree of allocated extents and find a hole of a given size.
1381 * The key ins is changed to record the hole:
1382 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -04001383 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -05001384 * ins->offset == number of blocks
1385 * Any available blocks before search_start are skipped.
1386 */
Chris Mason98ed5172008-01-03 10:01:48 -05001387static int noinline find_free_extent(struct btrfs_trans_handle *trans,
1388 struct btrfs_root *orig_root,
1389 u64 num_bytes, u64 empty_size,
1390 u64 search_start, u64 search_end,
1391 u64 hint_byte, struct btrfs_key *ins,
1392 u64 exclude_start, u64 exclude_nr,
1393 int data)
Chris Masonfec577f2007-02-26 10:40:21 -05001394{
Chris Mason5caf2a02007-04-02 11:20:42 -04001395 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -04001396 struct btrfs_key key;
Chris Masonfec577f2007-02-26 10:40:21 -05001397 u64 hole_size = 0;
Chris Mason87ee04e2007-11-30 11:30:34 -05001398 u64 aligned;
1399 int ret;
Chris Masonfec577f2007-02-26 10:40:21 -05001400 int slot = 0;
Chris Masondb945352007-10-15 16:15:53 -04001401 u64 last_byte = 0;
Chris Masonbe744172007-05-06 10:15:01 -04001402 u64 orig_search_start = search_start;
Chris Masonfec577f2007-02-26 10:40:21 -05001403 int start_found;
Chris Mason5f39d392007-10-15 16:14:19 -04001404 struct extent_buffer *l;
Chris Mason9f5fae22007-03-20 14:38:32 -04001405 struct btrfs_root * root = orig_root->fs_info->extent_root;
Chris Masonf2458e12007-04-25 15:52:25 -04001406 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -04001407 u64 total_needed = num_bytes;
Chris Masone20d96d2007-03-22 12:13:20 -04001408 int level;
Chris Masonbe08c1b2007-05-03 09:06:49 -04001409 struct btrfs_block_group_cache *block_group;
Chris Masonbe744172007-05-06 10:15:01 -04001410 int full_scan = 0;
Chris Masonfbdc7622007-05-30 10:22:12 -04001411 int wrapped = 0;
Chris Masonf84a8b32007-11-06 10:26:29 -05001412 u64 cached_start;
Chris Masonfec577f2007-02-26 10:40:21 -05001413
Chris Masondb945352007-10-15 16:15:53 -04001414 WARN_ON(num_bytes < root->sectorsize);
Chris Masonb1a4d962007-04-04 15:27:52 -04001415 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
1416
Chris Mason5f39d392007-10-15 16:14:19 -04001417 level = btrfs_header_level(root->node);
1418
Chris Mason015a739c2007-11-26 16:15:16 -08001419 if (num_bytes >= 32 * 1024 * 1024 && hint_byte) {
Chris Mason257d0ce2007-11-07 21:08:16 -05001420 data = BTRFS_BLOCK_GROUP_MIXED;
1421 }
1422
Chris Mason3e1ad542007-05-07 20:03:49 -04001423 if (search_end == (u64)-1)
Chris Masondb945352007-10-15 16:15:53 -04001424 search_end = btrfs_super_total_bytes(&info->super_copy);
1425 if (hint_byte) {
1426 block_group = btrfs_lookup_block_group(info, hint_byte);
Chris Mason1a2b2ac2007-12-04 13:18:24 -05001427 if (!block_group)
1428 hint_byte = search_start;
Chris Masonbe744172007-05-06 10:15:01 -04001429 block_group = btrfs_find_block_group(root, block_group,
Chris Masondb945352007-10-15 16:15:53 -04001430 hint_byte, data, 1);
Chris Masonbe744172007-05-06 10:15:01 -04001431 } else {
1432 block_group = btrfs_find_block_group(root,
Chris Mason1a2b2ac2007-12-04 13:18:24 -05001433 trans->block_group,
1434 search_start, data, 1);
Chris Masonbe744172007-05-06 10:15:01 -04001435 }
1436
Chris Mason6702ed42007-08-07 16:15:09 -04001437 total_needed += empty_size;
Chris Masone0115992007-06-19 16:23:05 -04001438 path = btrfs_alloc_path();
Chris Masonbe744172007-05-06 10:15:01 -04001439check_failed:
Chris Mason70b043f2007-12-13 09:02:46 -05001440 if (!block_group) {
1441 block_group = btrfs_lookup_block_group(info, search_start);
1442 if (!block_group)
1443 block_group = btrfs_lookup_block_group(info,
1444 orig_search_start);
1445 }
Yan5e5745d2007-11-16 14:57:09 -05001446 search_start = find_search_start(root, &block_group, search_start,
Yand548ee52008-01-03 13:56:30 -05001447 total_needed, data);
Chris Mason87ee04e2007-11-30 11:30:34 -05001448 search_start = stripe_align(root, search_start);
Chris Masonf84a8b32007-11-06 10:26:29 -05001449 cached_start = search_start;
Chris Mason5caf2a02007-04-02 11:20:42 -04001450 btrfs_init_path(path);
Chris Masonfec577f2007-02-26 10:40:21 -05001451 ins->objectid = search_start;
1452 ins->offset = 0;
Chris Masonfec577f2007-02-26 10:40:21 -05001453 start_found = 0;
Chris Mason2cc58cf2007-08-27 16:49:44 -04001454 path->reada = 2;
Chris Masone37c9e62007-05-09 20:13:14 -04001455
Chris Mason5caf2a02007-04-02 11:20:42 -04001456 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
Chris Mason0f70abe2007-02-28 16:46:22 -05001457 if (ret < 0)
1458 goto error;
Yand548ee52008-01-03 13:56:30 -05001459 ret = find_previous_extent(root, path);
1460 if (ret < 0)
1461 goto error;
Chris Mason5f39d392007-10-15 16:14:19 -04001462 l = path->nodes[0];
1463 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
Chris Masonfec577f2007-02-26 10:40:21 -05001464 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001465 l = path->nodes[0];
Chris Mason5caf2a02007-04-02 11:20:42 -04001466 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04001467 if (slot >= btrfs_header_nritems(l)) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001468 ret = btrfs_next_leaf(root, path);
Chris Masonfec577f2007-02-26 10:40:21 -05001469 if (ret == 0)
1470 continue;
Chris Mason0f70abe2007-02-28 16:46:22 -05001471 if (ret < 0)
1472 goto error;
Chris Masone19caa52007-10-15 16:17:44 -04001473
1474 search_start = max(search_start,
1475 block_group->key.objectid);
Chris Masonfec577f2007-02-26 10:40:21 -05001476 if (!start_found) {
Chris Mason87ee04e2007-11-30 11:30:34 -05001477 aligned = stripe_align(root, search_start);
1478 ins->objectid = aligned;
1479 if (aligned >= search_end) {
1480 ret = -ENOSPC;
1481 goto error;
1482 }
1483 ins->offset = search_end - aligned;
Chris Masonfec577f2007-02-26 10:40:21 -05001484 start_found = 1;
1485 goto check_pending;
1486 }
Chris Mason87ee04e2007-11-30 11:30:34 -05001487 ins->objectid = stripe_align(root,
1488 last_byte > search_start ?
1489 last_byte : search_start);
1490 if (search_end <= ins->objectid) {
1491 ret = -ENOSPC;
1492 goto error;
1493 }
Chris Mason3e1ad542007-05-07 20:03:49 -04001494 ins->offset = search_end - ins->objectid;
Chris Masone19caa52007-10-15 16:17:44 -04001495 BUG_ON(ins->objectid >= search_end);
Chris Masonfec577f2007-02-26 10:40:21 -05001496 goto check_pending;
1497 }
Chris Mason5f39d392007-10-15 16:14:19 -04001498 btrfs_item_key_to_cpu(l, &key, slot);
Chris Mason96b51792007-10-15 16:15:19 -04001499
Chris Masondb945352007-10-15 16:15:53 -04001500 if (key.objectid >= search_start && key.objectid > last_byte &&
Chris Masone37c9e62007-05-09 20:13:14 -04001501 start_found) {
Chris Masondb945352007-10-15 16:15:53 -04001502 if (last_byte < search_start)
1503 last_byte = search_start;
Chris Mason87ee04e2007-11-30 11:30:34 -05001504 aligned = stripe_align(root, last_byte);
1505 hole_size = key.objectid - aligned;
1506 if (key.objectid > aligned && hole_size >= num_bytes) {
1507 ins->objectid = aligned;
Chris Masone37c9e62007-05-09 20:13:14 -04001508 ins->offset = hole_size;
1509 goto check_pending;
Chris Mason0579da42007-03-07 16:15:30 -05001510 }
Chris Masonfec577f2007-02-26 10:40:21 -05001511 }
Chris Mason96b51792007-10-15 16:15:19 -04001512 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY) {
Chris Mason7bb86312007-12-11 09:25:06 -05001513 if (!start_found && btrfs_key_type(&key) ==
1514 BTRFS_BLOCK_GROUP_ITEM_KEY) {
Chris Masondb945352007-10-15 16:15:53 -04001515 last_byte = key.objectid;
Chris Mason96b51792007-10-15 16:15:19 -04001516 start_found = 1;
1517 }
Chris Masone37c9e62007-05-09 20:13:14 -04001518 goto next;
Chris Mason96b51792007-10-15 16:15:19 -04001519 }
1520
Chris Masone37c9e62007-05-09 20:13:14 -04001521
Chris Mason0579da42007-03-07 16:15:30 -05001522 start_found = 1;
Chris Masondb945352007-10-15 16:15:53 -04001523 last_byte = key.objectid + key.offset;
Chris Masonf510cfe2007-10-15 16:14:48 -04001524
Chris Mason257d0ce2007-11-07 21:08:16 -05001525 if (!full_scan && data != BTRFS_BLOCK_GROUP_MIXED &&
1526 last_byte >= block_group->key.objectid +
Chris Masonbe744172007-05-06 10:15:01 -04001527 block_group->key.offset) {
1528 btrfs_release_path(root, path);
1529 search_start = block_group->key.objectid +
Chris Masone19caa52007-10-15 16:17:44 -04001530 block_group->key.offset;
Chris Masonbe744172007-05-06 10:15:01 -04001531 goto new_group;
1532 }
Chris Mason9078a3e2007-04-26 16:46:15 -04001533next:
Chris Mason5caf2a02007-04-02 11:20:42 -04001534 path->slots[0]++;
Chris Masonde428b62007-05-18 13:28:27 -04001535 cond_resched();
Chris Masonfec577f2007-02-26 10:40:21 -05001536 }
Chris Masonfec577f2007-02-26 10:40:21 -05001537check_pending:
1538 /* we have to make sure we didn't find an extent that has already
1539 * been allocated by the map tree or the original allocation
1540 */
Chris Mason5caf2a02007-04-02 11:20:42 -04001541 btrfs_release_path(root, path);
Chris Masonfec577f2007-02-26 10:40:21 -05001542 BUG_ON(ins->objectid < search_start);
Chris Masone37c9e62007-05-09 20:13:14 -04001543
Chris Masondb945352007-10-15 16:15:53 -04001544 if (ins->objectid + num_bytes >= search_end)
Chris Masoncf675822007-09-17 11:00:51 -04001545 goto enospc;
Chris Mason257d0ce2007-11-07 21:08:16 -05001546 if (!full_scan && data != BTRFS_BLOCK_GROUP_MIXED &&
Yan5cf66422007-11-16 14:57:09 -05001547 ins->objectid + num_bytes > block_group->
Chris Masone19caa52007-10-15 16:17:44 -04001548 key.objectid + block_group->key.offset) {
1549 search_start = block_group->key.objectid +
1550 block_group->key.offset;
1551 goto new_group;
1552 }
Chris Mason1a5bc162007-10-15 16:15:26 -04001553 if (test_range_bit(&info->extent_ins, ins->objectid,
Chris Masondb945352007-10-15 16:15:53 -04001554 ins->objectid + num_bytes -1, EXTENT_LOCKED, 0)) {
1555 search_start = ins->objectid + num_bytes;
Chris Mason1a5bc162007-10-15 16:15:26 -04001556 goto new_group;
1557 }
1558 if (test_range_bit(&info->pinned_extents, ins->objectid,
Chris Masondb945352007-10-15 16:15:53 -04001559 ins->objectid + num_bytes -1, EXTENT_DIRTY, 0)) {
1560 search_start = ins->objectid + num_bytes;
Chris Mason1a5bc162007-10-15 16:15:26 -04001561 goto new_group;
Chris Masonfec577f2007-02-26 10:40:21 -05001562 }
Chris Masondb945352007-10-15 16:15:53 -04001563 if (exclude_nr > 0 && (ins->objectid + num_bytes > exclude_start &&
Chris Masonf2654de2007-06-26 12:20:46 -04001564 ins->objectid < exclude_start + exclude_nr)) {
1565 search_start = exclude_start + exclude_nr;
1566 goto new_group;
1567 }
Chris Masone37c9e62007-05-09 20:13:14 -04001568 if (!data) {
Chris Mason5276aed2007-06-11 21:33:38 -04001569 block_group = btrfs_lookup_block_group(info, ins->objectid);
Chris Mason26b80032007-08-08 20:17:12 -04001570 if (block_group)
1571 trans->block_group = block_group;
Chris Masoncd1bc462007-04-27 10:08:34 -04001572 }
Chris Masondb945352007-10-15 16:15:53 -04001573 ins->offset = num_bytes;
Chris Mason5caf2a02007-04-02 11:20:42 -04001574 btrfs_free_path(path);
Chris Masonfec577f2007-02-26 10:40:21 -05001575 return 0;
Chris Masonbe744172007-05-06 10:15:01 -04001576
1577new_group:
Chris Masondb945352007-10-15 16:15:53 -04001578 if (search_start + num_bytes >= search_end) {
Chris Masoncf675822007-09-17 11:00:51 -04001579enospc:
Chris Masonbe744172007-05-06 10:15:01 -04001580 search_start = orig_search_start;
Chris Masonfbdc7622007-05-30 10:22:12 -04001581 if (full_scan) {
1582 ret = -ENOSPC;
1583 goto error;
1584 }
Chris Mason6702ed42007-08-07 16:15:09 -04001585 if (wrapped) {
1586 if (!full_scan)
1587 total_needed -= empty_size;
Chris Masonfbdc7622007-05-30 10:22:12 -04001588 full_scan = 1;
Chris Mason1a2b2ac2007-12-04 13:18:24 -05001589 data = BTRFS_BLOCK_GROUP_MIXED;
Chris Mason6702ed42007-08-07 16:15:09 -04001590 } else
Chris Masonfbdc7622007-05-30 10:22:12 -04001591 wrapped = 1;
Chris Masonbe744172007-05-06 10:15:01 -04001592 }
Chris Mason5276aed2007-06-11 21:33:38 -04001593 block_group = btrfs_lookup_block_group(info, search_start);
Chris Masonfbdc7622007-05-30 10:22:12 -04001594 cond_resched();
Chris Mason1a2b2ac2007-12-04 13:18:24 -05001595 block_group = btrfs_find_block_group(root, block_group,
1596 search_start, data, 0);
Chris Masonbe744172007-05-06 10:15:01 -04001597 goto check_failed;
1598
Chris Mason0f70abe2007-02-28 16:46:22 -05001599error:
Chris Mason5caf2a02007-04-02 11:20:42 -04001600 btrfs_release_path(root, path);
1601 btrfs_free_path(path);
Chris Mason0f70abe2007-02-28 16:46:22 -05001602 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05001603}
Chris Masonfec577f2007-02-26 10:40:21 -05001604/*
Chris Masonfec577f2007-02-26 10:40:21 -05001605 * finds a free extent and does all the dirty work required for allocation
1606 * returns the key for the extent through ins, and a tree buffer for
1607 * the first block of the extent through buf.
1608 *
1609 * returns 0 if everything worked, non-zero otherwise.
1610 */
Chris Mason4d775672007-04-20 20:23:12 -04001611int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
Chris Mason7bb86312007-12-11 09:25:06 -05001612 struct btrfs_root *root,
1613 u64 num_bytes, u64 root_objectid, u64 ref_generation,
1614 u64 owner, u64 owner_offset,
1615 u64 empty_size, u64 hint_byte,
Chris Masonbe08c1b2007-05-03 09:06:49 -04001616 u64 search_end, struct btrfs_key *ins, int data)
Chris Masonfec577f2007-02-26 10:40:21 -05001617{
1618 int ret;
1619 int pending_ret;
Chris Masondb945352007-10-15 16:15:53 -04001620 u64 super_used, root_used;
Chris Masonfbdc7622007-05-30 10:22:12 -04001621 u64 search_start = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05001622 u64 new_hint;
Chris Mason1261ec42007-03-20 20:35:03 -04001623 struct btrfs_fs_info *info = root->fs_info;
1624 struct btrfs_root *extent_root = info->extent_root;
Chris Mason234b63a2007-03-13 10:46:10 -04001625 struct btrfs_extent_item extent_item;
Chris Mason7bb86312007-12-11 09:25:06 -05001626 struct btrfs_path *path;
Chris Mason037e6392007-03-07 11:50:24 -05001627
Chris Mason5f39d392007-10-15 16:14:19 -04001628 btrfs_set_stack_extent_refs(&extent_item, 1);
Chris Mason8f662a72008-01-02 10:01:11 -05001629
1630 new_hint = max(hint_byte, root->fs_info->alloc_start);
Chris Masonedbd8d42007-12-21 16:27:24 -05001631 if (new_hint < btrfs_super_total_bytes(&info->super_copy))
1632 hint_byte = new_hint;
Chris Mason8f662a72008-01-02 10:01:11 -05001633
Chris Masondb945352007-10-15 16:15:53 -04001634 WARN_ON(num_bytes < root->sectorsize);
1635 ret = find_free_extent(trans, root, num_bytes, empty_size,
1636 search_start, search_end, hint_byte, ins,
Chris Mason26b80032007-08-08 20:17:12 -04001637 trans->alloc_exclude_start,
1638 trans->alloc_exclude_nr, data);
Chris Masonccd467d2007-06-28 15:57:36 -04001639 BUG_ON(ret);
Chris Masonf2654de2007-06-26 12:20:46 -04001640 if (ret)
1641 return ret;
1642
Josef Bacik58176a92007-08-29 15:47:34 -04001643 /* block accounting for super block */
Chris Masondb945352007-10-15 16:15:53 -04001644 super_used = btrfs_super_bytes_used(&info->super_copy);
1645 btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes);
Chris Mason26b80032007-08-08 20:17:12 -04001646
Josef Bacik58176a92007-08-29 15:47:34 -04001647 /* block accounting for root item */
Chris Masondb945352007-10-15 16:15:53 -04001648 root_used = btrfs_root_used(&root->root_item);
1649 btrfs_set_root_used(&root->root_item, root_used + num_bytes);
Josef Bacik58176a92007-08-29 15:47:34 -04001650
Chris Masonf510cfe2007-10-15 16:14:48 -04001651 clear_extent_dirty(&root->fs_info->free_space_cache,
1652 ins->objectid, ins->objectid + ins->offset - 1,
1653 GFP_NOFS);
1654
Chris Mason26b80032007-08-08 20:17:12 -04001655 if (root == extent_root) {
Chris Mason1a5bc162007-10-15 16:15:26 -04001656 set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
1657 ins->objectid + ins->offset - 1,
1658 EXTENT_LOCKED, GFP_NOFS);
Chris Masone19caa52007-10-15 16:17:44 -04001659 WARN_ON(data == 1);
Chris Mason26b80032007-08-08 20:17:12 -04001660 goto update_block;
1661 }
1662
1663 WARN_ON(trans->alloc_exclude_nr);
1664 trans->alloc_exclude_start = ins->objectid;
1665 trans->alloc_exclude_nr = ins->offset;
Chris Masone089f052007-03-16 16:20:31 -04001666 ret = btrfs_insert_item(trans, extent_root, ins, &extent_item,
1667 sizeof(extent_item));
Chris Mason037e6392007-03-07 11:50:24 -05001668
Chris Mason26b80032007-08-08 20:17:12 -04001669 trans->alloc_exclude_start = 0;
1670 trans->alloc_exclude_nr = 0;
Chris Mason7bb86312007-12-11 09:25:06 -05001671 BUG_ON(ret);
1672
1673 path = btrfs_alloc_path();
1674 BUG_ON(!path);
1675 ret = btrfs_insert_extent_backref(trans, extent_root, path,
1676 ins->objectid, root_objectid,
1677 ref_generation, owner, owner_offset);
Chris Mason26b80032007-08-08 20:17:12 -04001678
Chris Masonccd467d2007-06-28 15:57:36 -04001679 BUG_ON(ret);
Chris Mason7bb86312007-12-11 09:25:06 -05001680 btrfs_free_path(path);
Chris Masone089f052007-03-16 16:20:31 -04001681 finish_current_insert(trans, extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -04001682 pending_ret = del_pending_extents(trans, extent_root);
Chris Masonf510cfe2007-10-15 16:14:48 -04001683
Chris Masone37c9e62007-05-09 20:13:14 -04001684 if (ret) {
Chris Mason037e6392007-03-07 11:50:24 -05001685 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -04001686 }
1687 if (pending_ret) {
Chris Mason037e6392007-03-07 11:50:24 -05001688 return pending_ret;
Chris Masone37c9e62007-05-09 20:13:14 -04001689 }
Chris Mason26b80032007-08-08 20:17:12 -04001690
1691update_block:
Chris Mason1e2677e2007-05-29 16:52:18 -04001692 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0,
1693 data);
Chris Masonfabb5682007-06-07 22:13:21 -04001694 BUG_ON(ret);
Chris Mason037e6392007-03-07 11:50:24 -05001695 return 0;
Chris Masonfec577f2007-02-26 10:40:21 -05001696}
1697
1698/*
1699 * helper function to allocate a block for a given tree
1700 * returns the tree buffer or NULL.
1701 */
Chris Mason5f39d392007-10-15 16:14:19 -04001702struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Chris Masondb945352007-10-15 16:15:53 -04001703 struct btrfs_root *root,
Chris Mason7bb86312007-12-11 09:25:06 -05001704 u32 blocksize,
1705 u64 root_objectid, u64 hint,
1706 u64 empty_size)
1707{
1708 u64 ref_generation;
1709
1710 if (root->ref_cows)
1711 ref_generation = trans->transid;
1712 else
1713 ref_generation = 0;
1714
1715
1716 return __btrfs_alloc_free_block(trans, root, blocksize, root_objectid,
1717 ref_generation, 0, 0, hint, empty_size);
1718}
1719
1720/*
1721 * helper function to allocate a block for a given tree
1722 * returns the tree buffer or NULL.
1723 */
1724struct extent_buffer *__btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
1725 struct btrfs_root *root,
1726 u32 blocksize,
1727 u64 root_objectid,
1728 u64 ref_generation,
1729 u64 first_objectid,
1730 int level,
1731 u64 hint,
Chris Mason5f39d392007-10-15 16:14:19 -04001732 u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05001733{
Chris Masone2fa7222007-03-12 16:22:34 -04001734 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -05001735 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04001736 struct extent_buffer *buf;
Chris Masonfec577f2007-02-26 10:40:21 -05001737
Chris Mason7bb86312007-12-11 09:25:06 -05001738 ret = btrfs_alloc_extent(trans, root, blocksize,
1739 root_objectid, ref_generation,
Chris Masonf6dbff52007-12-13 11:13:32 -05001740 level, first_objectid, empty_size, hint,
Chris Masondb945352007-10-15 16:15:53 -04001741 (u64)-1, &ins, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05001742 if (ret) {
Chris Mason54aa1f42007-06-22 14:16:25 -04001743 BUG_ON(ret > 0);
1744 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05001745 }
Chris Masondb945352007-10-15 16:15:53 -04001746 buf = btrfs_find_create_tree_block(root, ins.objectid, blocksize);
Chris Mason54aa1f42007-06-22 14:16:25 -04001747 if (!buf) {
Chris Mason7bb86312007-12-11 09:25:06 -05001748 btrfs_free_extent(trans, root, ins.objectid, blocksize,
1749 root->root_key.objectid, ref_generation,
1750 0, 0, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001751 return ERR_PTR(-ENOMEM);
1752 }
Chris Mason5f39d392007-10-15 16:14:19 -04001753 btrfs_set_buffer_uptodate(buf);
1754 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
1755 buf->start + buf->len - 1, GFP_NOFS);
Chris Mason19c00dd2007-10-15 16:19:22 -04001756 set_extent_bits(&BTRFS_I(root->fs_info->btree_inode)->extent_tree,
1757 buf->start, buf->start + buf->len - 1,
1758 EXTENT_CSUM, GFP_NOFS);
1759 buf->flags |= EXTENT_CSUM;
Chris Mason6b800532007-10-15 16:17:34 -04001760 btrfs_set_buffer_defrag(buf);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001761 trans->blocks_used++;
Chris Masonfec577f2007-02-26 10:40:21 -05001762 return buf;
1763}
Chris Masona28ec192007-03-06 20:08:01 -05001764
Chris Mason98ed5172008-01-03 10:01:48 -05001765static int noinline drop_leaf_ref(struct btrfs_trans_handle *trans,
1766 struct btrfs_root *root,
1767 struct extent_buffer *leaf)
Chris Mason6407bf62007-03-27 06:33:00 -04001768{
Chris Mason7bb86312007-12-11 09:25:06 -05001769 u64 leaf_owner;
1770 u64 leaf_generation;
Chris Mason5f39d392007-10-15 16:14:19 -04001771 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -04001772 struct btrfs_file_extent_item *fi;
1773 int i;
1774 int nritems;
1775 int ret;
1776
Chris Mason5f39d392007-10-15 16:14:19 -04001777 BUG_ON(!btrfs_is_leaf(leaf));
1778 nritems = btrfs_header_nritems(leaf);
Chris Mason7bb86312007-12-11 09:25:06 -05001779 leaf_owner = btrfs_header_owner(leaf);
1780 leaf_generation = btrfs_header_generation(leaf);
1781
Chris Mason6407bf62007-03-27 06:33:00 -04001782 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04001783 u64 disk_bytenr;
Chris Mason5f39d392007-10-15 16:14:19 -04001784
1785 btrfs_item_key_to_cpu(leaf, &key, i);
1786 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason6407bf62007-03-27 06:33:00 -04001787 continue;
1788 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001789 if (btrfs_file_extent_type(leaf, fi) ==
1790 BTRFS_FILE_EXTENT_INLINE)
Chris Mason236454df2007-04-19 13:37:44 -04001791 continue;
Chris Mason6407bf62007-03-27 06:33:00 -04001792 /*
1793 * FIXME make sure to insert a trans record that
1794 * repeats the snapshot del on crash
1795 */
Chris Masondb945352007-10-15 16:15:53 -04001796 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
1797 if (disk_bytenr == 0)
Chris Mason3a686372007-05-24 13:35:57 -04001798 continue;
Chris Masondb945352007-10-15 16:15:53 -04001799 ret = btrfs_free_extent(trans, root, disk_bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -05001800 btrfs_file_extent_disk_num_bytes(leaf, fi),
1801 leaf_owner, leaf_generation,
1802 key.objectid, key.offset, 0);
Chris Mason6407bf62007-03-27 06:33:00 -04001803 BUG_ON(ret);
1804 }
1805 return 0;
1806}
1807
Chris Mason98ed5172008-01-03 10:01:48 -05001808static void noinline reada_walk_down(struct btrfs_root *root,
1809 struct extent_buffer *node)
Chris Masone0115992007-06-19 16:23:05 -04001810{
1811 int i;
1812 u32 nritems;
Chris Masondb945352007-10-15 16:15:53 -04001813 u64 bytenr;
Chris Masone0115992007-06-19 16:23:05 -04001814 int ret;
1815 u32 refs;
Chris Masondb945352007-10-15 16:15:53 -04001816 int level;
1817 u32 blocksize;
Chris Masone0115992007-06-19 16:23:05 -04001818
Chris Mason5f39d392007-10-15 16:14:19 -04001819 nritems = btrfs_header_nritems(node);
Chris Masondb945352007-10-15 16:15:53 -04001820 level = btrfs_header_level(node);
Chris Masone0115992007-06-19 16:23:05 -04001821 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04001822 bytenr = btrfs_node_blockptr(node, i);
1823 blocksize = btrfs_level_size(root, level - 1);
1824 ret = lookup_extent_ref(NULL, root, bytenr, blocksize, &refs);
Chris Masone0115992007-06-19 16:23:05 -04001825 BUG_ON(ret);
1826 if (refs != 1)
1827 continue;
Chris Mason409eb952007-08-08 20:17:12 -04001828 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masondb945352007-10-15 16:15:53 -04001829 ret = readahead_tree_block(root, bytenr, blocksize);
Chris Mason409eb952007-08-08 20:17:12 -04001830 cond_resched();
1831 mutex_lock(&root->fs_info->fs_mutex);
Chris Masone0115992007-06-19 16:23:05 -04001832 if (ret)
1833 break;
1834 }
1835}
1836
Chris Mason9aca1d52007-03-13 11:09:37 -04001837/*
1838 * helper function for drop_snapshot, this walks down the tree dropping ref
1839 * counts as it goes.
1840 */
Chris Mason98ed5172008-01-03 10:01:48 -05001841static int noinline walk_down_tree(struct btrfs_trans_handle *trans,
1842 struct btrfs_root *root,
1843 struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05001844{
Chris Mason7bb86312007-12-11 09:25:06 -05001845 u64 root_owner;
1846 u64 root_gen;
1847 u64 bytenr;
Chris Mason5f39d392007-10-15 16:14:19 -04001848 struct extent_buffer *next;
1849 struct extent_buffer *cur;
Chris Mason7bb86312007-12-11 09:25:06 -05001850 struct extent_buffer *parent;
Chris Masondb945352007-10-15 16:15:53 -04001851 u32 blocksize;
Chris Mason20524f02007-03-10 06:35:47 -05001852 int ret;
1853 u32 refs;
1854
Chris Mason5caf2a02007-04-02 11:20:42 -04001855 WARN_ON(*level < 0);
1856 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason5f39d392007-10-15 16:14:19 -04001857 ret = lookup_extent_ref(trans, root,
Chris Masondb945352007-10-15 16:15:53 -04001858 path->nodes[*level]->start,
1859 path->nodes[*level]->len, &refs);
Chris Mason20524f02007-03-10 06:35:47 -05001860 BUG_ON(ret);
1861 if (refs > 1)
1862 goto out;
Chris Masone0115992007-06-19 16:23:05 -04001863
Chris Mason9aca1d52007-03-13 11:09:37 -04001864 /*
1865 * walk down to the last node level and free all the leaves
1866 */
Chris Mason6407bf62007-03-27 06:33:00 -04001867 while(*level >= 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001868 WARN_ON(*level < 0);
1869 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason20524f02007-03-10 06:35:47 -05001870 cur = path->nodes[*level];
Chris Masone0115992007-06-19 16:23:05 -04001871
1872 if (*level > 0 && path->slots[*level] == 0)
Chris Mason5f39d392007-10-15 16:14:19 -04001873 reada_walk_down(root, cur);
Chris Masone0115992007-06-19 16:23:05 -04001874
Chris Mason5f39d392007-10-15 16:14:19 -04001875 if (btrfs_header_level(cur) != *level)
Chris Mason2c90e5d2007-04-02 10:50:19 -04001876 WARN_ON(1);
Chris Masone0115992007-06-19 16:23:05 -04001877
Chris Mason7518a232007-03-12 12:01:18 -04001878 if (path->slots[*level] >=
Chris Mason5f39d392007-10-15 16:14:19 -04001879 btrfs_header_nritems(cur))
Chris Mason20524f02007-03-10 06:35:47 -05001880 break;
Chris Mason6407bf62007-03-27 06:33:00 -04001881 if (*level == 0) {
1882 ret = drop_leaf_ref(trans, root, cur);
1883 BUG_ON(ret);
1884 break;
1885 }
Chris Masondb945352007-10-15 16:15:53 -04001886 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
1887 blocksize = btrfs_level_size(root, *level - 1);
1888 ret = lookup_extent_ref(trans, root, bytenr, blocksize, &refs);
Chris Mason6407bf62007-03-27 06:33:00 -04001889 BUG_ON(ret);
1890 if (refs != 1) {
Chris Mason7bb86312007-12-11 09:25:06 -05001891 parent = path->nodes[*level];
1892 root_owner = btrfs_header_owner(parent);
1893 root_gen = btrfs_header_generation(parent);
Chris Mason20524f02007-03-10 06:35:47 -05001894 path->slots[*level]++;
Chris Masondb945352007-10-15 16:15:53 -04001895 ret = btrfs_free_extent(trans, root, bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -05001896 blocksize, root_owner,
1897 root_gen, 0, 0, 1);
Chris Mason20524f02007-03-10 06:35:47 -05001898 BUG_ON(ret);
1899 continue;
1900 }
Chris Masondb945352007-10-15 16:15:53 -04001901 next = btrfs_find_tree_block(root, bytenr, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -04001902 if (!next || !btrfs_buffer_uptodate(next)) {
1903 free_extent_buffer(next);
Chris Masone9d0b132007-08-10 14:06:19 -04001904 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masondb945352007-10-15 16:15:53 -04001905 next = read_tree_block(root, bytenr, blocksize);
Chris Masone9d0b132007-08-10 14:06:19 -04001906 mutex_lock(&root->fs_info->fs_mutex);
1907
1908 /* we dropped the lock, check one more time */
Chris Masondb945352007-10-15 16:15:53 -04001909 ret = lookup_extent_ref(trans, root, bytenr,
1910 blocksize, &refs);
Chris Masone9d0b132007-08-10 14:06:19 -04001911 BUG_ON(ret);
1912 if (refs != 1) {
Chris Mason7bb86312007-12-11 09:25:06 -05001913 parent = path->nodes[*level];
1914 root_owner = btrfs_header_owner(parent);
1915 root_gen = btrfs_header_generation(parent);
1916
Chris Masone9d0b132007-08-10 14:06:19 -04001917 path->slots[*level]++;
Chris Mason5f39d392007-10-15 16:14:19 -04001918 free_extent_buffer(next);
Chris Mason7bb86312007-12-11 09:25:06 -05001919 ret = btrfs_free_extent(trans, root, bytenr,
1920 blocksize,
1921 root_owner,
1922 root_gen, 0, 0, 1);
Chris Masone9d0b132007-08-10 14:06:19 -04001923 BUG_ON(ret);
1924 continue;
1925 }
1926 }
Chris Mason5caf2a02007-04-02 11:20:42 -04001927 WARN_ON(*level <= 0);
Chris Mason83e15a22007-03-12 09:03:27 -04001928 if (path->nodes[*level-1])
Chris Mason5f39d392007-10-15 16:14:19 -04001929 free_extent_buffer(path->nodes[*level-1]);
Chris Mason20524f02007-03-10 06:35:47 -05001930 path->nodes[*level-1] = next;
Chris Mason5f39d392007-10-15 16:14:19 -04001931 *level = btrfs_header_level(next);
Chris Mason20524f02007-03-10 06:35:47 -05001932 path->slots[*level] = 0;
1933 }
1934out:
Chris Mason5caf2a02007-04-02 11:20:42 -04001935 WARN_ON(*level < 0);
1936 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason7bb86312007-12-11 09:25:06 -05001937
1938 if (path->nodes[*level] == root->node) {
1939 root_owner = root->root_key.objectid;
1940 parent = path->nodes[*level];
1941 } else {
1942 parent = path->nodes[*level + 1];
1943 root_owner = btrfs_header_owner(parent);
1944 }
1945
1946 root_gen = btrfs_header_generation(parent);
Chris Masondb945352007-10-15 16:15:53 -04001947 ret = btrfs_free_extent(trans, root, path->nodes[*level]->start,
Chris Mason7bb86312007-12-11 09:25:06 -05001948 path->nodes[*level]->len,
1949 root_owner, root_gen, 0, 0, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001950 free_extent_buffer(path->nodes[*level]);
Chris Mason20524f02007-03-10 06:35:47 -05001951 path->nodes[*level] = NULL;
1952 *level += 1;
1953 BUG_ON(ret);
1954 return 0;
1955}
1956
Chris Mason9aca1d52007-03-13 11:09:37 -04001957/*
1958 * helper for dropping snapshots. This walks back up the tree in the path
1959 * to find the first node higher up where we haven't yet gone through
1960 * all the slots
1961 */
Chris Mason98ed5172008-01-03 10:01:48 -05001962static int noinline walk_up_tree(struct btrfs_trans_handle *trans,
1963 struct btrfs_root *root,
1964 struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05001965{
Chris Mason7bb86312007-12-11 09:25:06 -05001966 u64 root_owner;
1967 u64 root_gen;
1968 struct btrfs_root_item *root_item = &root->root_item;
Chris Mason20524f02007-03-10 06:35:47 -05001969 int i;
1970 int slot;
1971 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04001972
Chris Mason234b63a2007-03-13 10:46:10 -04001973 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Mason20524f02007-03-10 06:35:47 -05001974 slot = path->slots[i];
Chris Mason5f39d392007-10-15 16:14:19 -04001975 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
1976 struct extent_buffer *node;
1977 struct btrfs_disk_key disk_key;
1978 node = path->nodes[i];
Chris Mason20524f02007-03-10 06:35:47 -05001979 path->slots[i]++;
1980 *level = i;
Chris Mason9f3a7422007-08-07 15:52:19 -04001981 WARN_ON(*level == 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001982 btrfs_node_key(node, &disk_key, path->slots[i]);
Chris Mason9f3a7422007-08-07 15:52:19 -04001983 memcpy(&root_item->drop_progress,
Chris Mason5f39d392007-10-15 16:14:19 -04001984 &disk_key, sizeof(disk_key));
Chris Mason9f3a7422007-08-07 15:52:19 -04001985 root_item->drop_level = i;
Chris Mason20524f02007-03-10 06:35:47 -05001986 return 0;
1987 } else {
Chris Mason7bb86312007-12-11 09:25:06 -05001988 if (path->nodes[*level] == root->node) {
1989 root_owner = root->root_key.objectid;
1990 root_gen =
1991 btrfs_header_generation(path->nodes[*level]);
1992 } else {
1993 struct extent_buffer *node;
1994 node = path->nodes[*level + 1];
1995 root_owner = btrfs_header_owner(node);
1996 root_gen = btrfs_header_generation(node);
1997 }
Chris Masone089f052007-03-16 16:20:31 -04001998 ret = btrfs_free_extent(trans, root,
Chris Masondb945352007-10-15 16:15:53 -04001999 path->nodes[*level]->start,
Chris Mason7bb86312007-12-11 09:25:06 -05002000 path->nodes[*level]->len,
2001 root_owner, root_gen, 0, 0, 1);
Chris Mason6407bf62007-03-27 06:33:00 -04002002 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04002003 free_extent_buffer(path->nodes[*level]);
Chris Mason83e15a22007-03-12 09:03:27 -04002004 path->nodes[*level] = NULL;
Chris Mason20524f02007-03-10 06:35:47 -05002005 *level = i + 1;
Chris Mason20524f02007-03-10 06:35:47 -05002006 }
2007 }
2008 return 1;
2009}
2010
Chris Mason9aca1d52007-03-13 11:09:37 -04002011/*
2012 * drop the reference count on the tree rooted at 'snap'. This traverses
2013 * the tree freeing any blocks that have a ref count of zero after being
2014 * decremented.
2015 */
Chris Masone089f052007-03-16 16:20:31 -04002016int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason9f3a7422007-08-07 15:52:19 -04002017 *root)
Chris Mason20524f02007-03-10 06:35:47 -05002018{
Chris Mason3768f362007-03-13 16:47:54 -04002019 int ret = 0;
Chris Mason9aca1d52007-03-13 11:09:37 -04002020 int wret;
Chris Mason20524f02007-03-10 06:35:47 -05002021 int level;
Chris Mason5caf2a02007-04-02 11:20:42 -04002022 struct btrfs_path *path;
Chris Mason20524f02007-03-10 06:35:47 -05002023 int i;
2024 int orig_level;
Chris Mason9f3a7422007-08-07 15:52:19 -04002025 struct btrfs_root_item *root_item = &root->root_item;
Chris Mason20524f02007-03-10 06:35:47 -05002026
Chris Mason5caf2a02007-04-02 11:20:42 -04002027 path = btrfs_alloc_path();
2028 BUG_ON(!path);
Chris Mason20524f02007-03-10 06:35:47 -05002029
Chris Mason5f39d392007-10-15 16:14:19 -04002030 level = btrfs_header_level(root->node);
Chris Mason20524f02007-03-10 06:35:47 -05002031 orig_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04002032 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
2033 path->nodes[level] = root->node;
Chris Masonf510cfe2007-10-15 16:14:48 -04002034 extent_buffer_get(root->node);
Chris Mason9f3a7422007-08-07 15:52:19 -04002035 path->slots[level] = 0;
2036 } else {
2037 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04002038 struct btrfs_disk_key found_key;
2039 struct extent_buffer *node;
Chris Mason6702ed42007-08-07 16:15:09 -04002040
Chris Mason9f3a7422007-08-07 15:52:19 -04002041 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Chris Mason6702ed42007-08-07 16:15:09 -04002042 level = root_item->drop_level;
2043 path->lowest_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04002044 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason6702ed42007-08-07 16:15:09 -04002045 if (wret < 0) {
Chris Mason9f3a7422007-08-07 15:52:19 -04002046 ret = wret;
2047 goto out;
2048 }
Chris Mason5f39d392007-10-15 16:14:19 -04002049 node = path->nodes[level];
2050 btrfs_node_key(node, &found_key, path->slots[level]);
2051 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
2052 sizeof(found_key)));
Chris Mason9f3a7422007-08-07 15:52:19 -04002053 }
Chris Mason20524f02007-03-10 06:35:47 -05002054 while(1) {
Chris Mason5caf2a02007-04-02 11:20:42 -04002055 wret = walk_down_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04002056 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05002057 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04002058 if (wret < 0)
2059 ret = wret;
2060
Chris Mason5caf2a02007-04-02 11:20:42 -04002061 wret = walk_up_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04002062 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05002063 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04002064 if (wret < 0)
2065 ret = wret;
Chris Mason409eb952007-08-08 20:17:12 -04002066 ret = -EAGAIN;
Chris Mason409eb952007-08-08 20:17:12 -04002067 break;
Chris Mason20524f02007-03-10 06:35:47 -05002068 }
Chris Mason83e15a22007-03-12 09:03:27 -04002069 for (i = 0; i <= orig_level; i++) {
Chris Mason5caf2a02007-04-02 11:20:42 -04002070 if (path->nodes[i]) {
Chris Mason5f39d392007-10-15 16:14:19 -04002071 free_extent_buffer(path->nodes[i]);
Chris Mason0f827312007-10-15 16:18:56 -04002072 path->nodes[i] = NULL;
Chris Mason83e15a22007-03-12 09:03:27 -04002073 }
Chris Mason20524f02007-03-10 06:35:47 -05002074 }
Chris Mason9f3a7422007-08-07 15:52:19 -04002075out:
Chris Mason5caf2a02007-04-02 11:20:42 -04002076 btrfs_free_path(path);
Chris Mason9aca1d52007-03-13 11:09:37 -04002077 return ret;
Chris Mason20524f02007-03-10 06:35:47 -05002078}
Chris Mason9078a3e2007-04-26 16:46:15 -04002079
Chris Masonbe744172007-05-06 10:15:01 -04002080int btrfs_free_block_groups(struct btrfs_fs_info *info)
2081{
Chris Masonf510cfe2007-10-15 16:14:48 -04002082 u64 start;
2083 u64 end;
Yanb97f9202007-11-01 11:28:41 -04002084 u64 ptr;
Chris Mason96b51792007-10-15 16:15:19 -04002085 int ret;
Chris Mason96b51792007-10-15 16:15:19 -04002086 while(1) {
2087 ret = find_first_extent_bit(&info->block_group_cache, 0,
2088 &start, &end, (unsigned int)-1);
2089 if (ret)
2090 break;
Yanb97f9202007-11-01 11:28:41 -04002091 ret = get_state_private(&info->block_group_cache, start, &ptr);
2092 if (!ret)
2093 kfree((void *)(unsigned long)ptr);
Chris Mason96b51792007-10-15 16:15:19 -04002094 clear_extent_bits(&info->block_group_cache, start,
2095 end, (unsigned int)-1, GFP_NOFS);
2096 }
Chris Masone37c9e62007-05-09 20:13:14 -04002097 while(1) {
Chris Masonf510cfe2007-10-15 16:14:48 -04002098 ret = find_first_extent_bit(&info->free_space_cache, 0,
2099 &start, &end, EXTENT_DIRTY);
2100 if (ret)
Chris Masone37c9e62007-05-09 20:13:14 -04002101 break;
Chris Masonf510cfe2007-10-15 16:14:48 -04002102 clear_extent_dirty(&info->free_space_cache, start,
2103 end, GFP_NOFS);
Chris Masone37c9e62007-05-09 20:13:14 -04002104 }
Chris Masonbe744172007-05-06 10:15:01 -04002105 return 0;
2106}
2107
Chris Mason98ed5172008-01-03 10:01:48 -05002108static int noinline relocate_inode_pages(struct inode *inode, u64 start,
2109 u64 len)
Chris Masonedbd8d42007-12-21 16:27:24 -05002110{
2111 u64 page_start;
2112 u64 page_end;
2113 u64 delalloc_start;
2114 u64 existing_delalloc;
2115 unsigned long last_index;
Chris Masonedbd8d42007-12-21 16:27:24 -05002116 unsigned long i;
2117 struct page *page;
2118 struct btrfs_root *root = BTRFS_I(inode)->root;
2119 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason4313b392008-01-03 09:08:48 -05002120 struct file_ra_state *ra;
2121
2122 ra = kzalloc(sizeof(*ra), GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05002123
2124 mutex_lock(&inode->i_mutex);
Chris Mason4313b392008-01-03 09:08:48 -05002125 i = start >> PAGE_CACHE_SHIFT;
Chris Masonedbd8d42007-12-21 16:27:24 -05002126 last_index = (start + len - 1) >> PAGE_CACHE_SHIFT;
2127
Chris Mason4313b392008-01-03 09:08:48 -05002128 file_ra_state_init(ra, inode->i_mapping);
2129 btrfs_force_ra(inode->i_mapping, ra, NULL, i, last_index);
2130 kfree(ra);
Chris Masonedbd8d42007-12-21 16:27:24 -05002131
Chris Mason4313b392008-01-03 09:08:48 -05002132 for (; i <= last_index; i++) {
Chris Masonedbd8d42007-12-21 16:27:24 -05002133 page = grab_cache_page(inode->i_mapping, i);
2134 if (!page)
2135 goto out_unlock;
2136 if (!PageUptodate(page)) {
2137 btrfs_readpage(NULL, page);
2138 lock_page(page);
2139 if (!PageUptodate(page)) {
2140 unlock_page(page);
2141 page_cache_release(page);
2142 goto out_unlock;
2143 }
2144 }
2145 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
2146 page_end = page_start + PAGE_CACHE_SIZE - 1;
2147
2148 lock_extent(em_tree, page_start, page_end, GFP_NOFS);
2149
2150 delalloc_start = page_start;
2151 existing_delalloc =
2152 count_range_bits(&BTRFS_I(inode)->extent_tree,
2153 &delalloc_start, page_end,
2154 PAGE_CACHE_SIZE, EXTENT_DELALLOC);
2155
2156 set_extent_delalloc(em_tree, page_start,
2157 page_end, GFP_NOFS);
2158
2159 spin_lock(&root->fs_info->delalloc_lock);
2160 root->fs_info->delalloc_bytes += PAGE_CACHE_SIZE -
2161 existing_delalloc;
2162 spin_unlock(&root->fs_info->delalloc_lock);
2163
2164 unlock_extent(em_tree, page_start, page_end, GFP_NOFS);
2165 set_page_dirty(page);
2166 unlock_page(page);
2167 page_cache_release(page);
2168 }
2169
2170out_unlock:
2171 mutex_unlock(&inode->i_mutex);
2172 return 0;
2173}
2174
Chris Mason4313b392008-01-03 09:08:48 -05002175/*
2176 * note, this releases the path
2177 */
Chris Mason98ed5172008-01-03 10:01:48 -05002178static int noinline relocate_one_reference(struct btrfs_root *extent_root,
Chris Masonedbd8d42007-12-21 16:27:24 -05002179 struct btrfs_path *path,
Chris Mason4313b392008-01-03 09:08:48 -05002180 struct btrfs_key *extent_key)
Chris Masonedbd8d42007-12-21 16:27:24 -05002181{
2182 struct inode *inode;
2183 struct btrfs_root *found_root;
Chris Mason4313b392008-01-03 09:08:48 -05002184 struct btrfs_key *root_location;
2185 struct btrfs_extent_ref *ref;
2186 u64 ref_root;
2187 u64 ref_gen;
2188 u64 ref_objectid;
2189 u64 ref_offset;
Chris Masonedbd8d42007-12-21 16:27:24 -05002190 int ret;
2191
Chris Mason4313b392008-01-03 09:08:48 -05002192 ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
2193 struct btrfs_extent_ref);
2194 ref_root = btrfs_ref_root(path->nodes[0], ref);
2195 ref_gen = btrfs_ref_generation(path->nodes[0], ref);
2196 ref_objectid = btrfs_ref_objectid(path->nodes[0], ref);
2197 ref_offset = btrfs_ref_offset(path->nodes[0], ref);
2198 btrfs_release_path(extent_root, path);
2199
2200 root_location = kmalloc(sizeof(*root_location), GFP_NOFS);
2201 root_location->objectid = ref_root;
Chris Masonedbd8d42007-12-21 16:27:24 -05002202 if (ref_gen == 0)
Chris Mason4313b392008-01-03 09:08:48 -05002203 root_location->offset = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05002204 else
Chris Mason4313b392008-01-03 09:08:48 -05002205 root_location->offset = (u64)-1;
2206 root_location->type = BTRFS_ROOT_ITEM_KEY;
Chris Masonedbd8d42007-12-21 16:27:24 -05002207
2208 found_root = btrfs_read_fs_root_no_name(extent_root->fs_info,
Chris Mason4313b392008-01-03 09:08:48 -05002209 root_location);
Chris Masonedbd8d42007-12-21 16:27:24 -05002210 BUG_ON(!found_root);
Chris Mason4313b392008-01-03 09:08:48 -05002211 kfree(root_location);
Chris Masonedbd8d42007-12-21 16:27:24 -05002212
2213 if (ref_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
2214 mutex_unlock(&extent_root->fs_info->fs_mutex);
2215 inode = btrfs_iget_locked(extent_root->fs_info->sb,
2216 ref_objectid, found_root);
2217 if (inode->i_state & I_NEW) {
2218 /* the inode and parent dir are two different roots */
2219 BTRFS_I(inode)->root = found_root;
2220 BTRFS_I(inode)->location.objectid = ref_objectid;
2221 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
2222 BTRFS_I(inode)->location.offset = 0;
2223 btrfs_read_locked_inode(inode);
2224 unlock_new_inode(inode);
2225
2226 }
2227 /* this can happen if the reference is not against
2228 * the latest version of the tree root
2229 */
2230 if (is_bad_inode(inode)) {
2231 mutex_lock(&extent_root->fs_info->fs_mutex);
2232 goto out;
2233 }
2234 relocate_inode_pages(inode, ref_offset, extent_key->offset);
2235 /* FIXME, data=ordered will help get rid of this */
2236 filemap_fdatawrite(inode->i_mapping);
2237 iput(inode);
2238 mutex_lock(&extent_root->fs_info->fs_mutex);
2239 } else {
2240 struct btrfs_trans_handle *trans;
2241 struct btrfs_key found_key;
2242 struct extent_buffer *eb;
2243 int level;
2244 int i;
2245
2246 trans = btrfs_start_transaction(found_root, 1);
2247 eb = read_tree_block(found_root, extent_key->objectid,
2248 extent_key->offset);
2249 level = btrfs_header_level(eb);
2250
2251 if (level == 0)
2252 btrfs_item_key_to_cpu(eb, &found_key, 0);
2253 else
2254 btrfs_node_key_to_cpu(eb, &found_key, 0);
2255
2256 free_extent_buffer(eb);
2257
2258 path->lowest_level = level;
Chris Mason8f662a72008-01-02 10:01:11 -05002259 path->reada = 2;
Chris Masonedbd8d42007-12-21 16:27:24 -05002260 ret = btrfs_search_slot(trans, found_root, &found_key, path,
2261 0, 1);
2262 path->lowest_level = 0;
2263 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2264 if (!path->nodes[i])
2265 break;
2266 free_extent_buffer(path->nodes[i]);
2267 path->nodes[i] = NULL;
2268 }
2269 btrfs_release_path(found_root, path);
2270 btrfs_end_transaction(trans, found_root);
2271 }
2272
2273out:
2274 return 0;
2275}
2276
Chris Mason98ed5172008-01-03 10:01:48 -05002277static int noinline relocate_one_extent(struct btrfs_root *extent_root,
2278 struct btrfs_path *path,
2279 struct btrfs_key *extent_key)
Chris Masonedbd8d42007-12-21 16:27:24 -05002280{
2281 struct btrfs_key key;
2282 struct btrfs_key found_key;
Chris Masonedbd8d42007-12-21 16:27:24 -05002283 struct extent_buffer *leaf;
Chris Masonedbd8d42007-12-21 16:27:24 -05002284 u32 nritems;
2285 u32 item_size;
2286 int ret = 0;
2287
2288 key.objectid = extent_key->objectid;
2289 key.type = BTRFS_EXTENT_REF_KEY;
2290 key.offset = 0;
2291
2292 while(1) {
2293 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
2294
2295 BUG_ON(ret == 0);
2296
2297 if (ret < 0)
2298 goto out;
2299
2300 ret = 0;
2301 leaf = path->nodes[0];
2302 nritems = btrfs_header_nritems(leaf);
2303 if (path->slots[0] == nritems)
2304 goto out;
2305
2306 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2307 if (found_key.objectid != extent_key->objectid)
2308 break;
2309
2310 if (found_key.type != BTRFS_EXTENT_REF_KEY)
2311 break;
2312
2313 key.offset = found_key.offset + 1;
2314 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2315
Chris Mason4313b392008-01-03 09:08:48 -05002316 ret = relocate_one_reference(extent_root, path, extent_key);
Chris Masonedbd8d42007-12-21 16:27:24 -05002317 if (ret)
2318 goto out;
2319 }
2320 ret = 0;
2321out:
2322 btrfs_release_path(extent_root, path);
2323 return ret;
2324}
2325
Chris Masonedbd8d42007-12-21 16:27:24 -05002326int btrfs_shrink_extent_tree(struct btrfs_root *root, u64 new_size)
2327{
2328 struct btrfs_trans_handle *trans;
2329 struct btrfs_root *tree_root = root->fs_info->tree_root;
2330 struct btrfs_path *path;
2331 u64 cur_byte;
2332 u64 total_found;
Chris Masonedbd8d42007-12-21 16:27:24 -05002333 struct btrfs_fs_info *info = root->fs_info;
2334 struct extent_map_tree *block_group_cache;
2335 struct btrfs_key key;
Yan73e48b22008-01-03 14:14:39 -05002336 struct btrfs_key found_key;
Chris Masonedbd8d42007-12-21 16:27:24 -05002337 struct extent_buffer *leaf;
2338 u32 nritems;
2339 int ret;
Chris Mason725c8462008-01-04 16:47:16 -05002340 int progress = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05002341
2342 btrfs_set_super_total_bytes(&info->super_copy, new_size);
2343 block_group_cache = &info->block_group_cache;
2344 path = btrfs_alloc_path();
2345 root = root->fs_info->extent_root;
Chris Mason8f662a72008-01-02 10:01:11 -05002346 path->reada = 2;
Chris Masonedbd8d42007-12-21 16:27:24 -05002347
2348again:
2349 total_found = 0;
2350 key.objectid = new_size;
Chris Masonedbd8d42007-12-21 16:27:24 -05002351 key.offset = 0;
2352 key.type = 0;
Yan73e48b22008-01-03 14:14:39 -05002353 cur_byte = key.objectid;
Chris Mason4313b392008-01-03 09:08:48 -05002354
Yan73e48b22008-01-03 14:14:39 -05002355 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2356 if (ret < 0)
2357 goto out;
2358
2359 ret = find_previous_extent(root, path);
2360 if (ret < 0)
2361 goto out;
2362 if (ret == 0) {
2363 leaf = path->nodes[0];
2364 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2365 if (found_key.objectid + found_key.offset > new_size) {
2366 cur_byte = found_key.objectid;
2367 key.objectid = cur_byte;
2368 }
2369 }
2370 btrfs_release_path(root, path);
2371
2372 while(1) {
Chris Masonedbd8d42007-12-21 16:27:24 -05002373 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2374 if (ret < 0)
2375 goto out;
Yan73e48b22008-01-03 14:14:39 -05002376
Chris Masonedbd8d42007-12-21 16:27:24 -05002377 leaf = path->nodes[0];
Chris Masonedbd8d42007-12-21 16:27:24 -05002378 nritems = btrfs_header_nritems(leaf);
Yan73e48b22008-01-03 14:14:39 -05002379next:
2380 if (path->slots[0] >= nritems) {
2381 ret = btrfs_next_leaf(root, path);
2382 if (ret < 0)
2383 goto out;
2384 if (ret == 1) {
2385 ret = 0;
2386 break;
Chris Masonedbd8d42007-12-21 16:27:24 -05002387 }
Yan73e48b22008-01-03 14:14:39 -05002388 leaf = path->nodes[0];
2389 nritems = btrfs_header_nritems(leaf);
2390 }
2391
2392 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason725c8462008-01-04 16:47:16 -05002393
2394 if (progress && need_resched()) {
2395 memcpy(&key, &found_key, sizeof(key));
2396 mutex_unlock(&root->fs_info->fs_mutex);
2397 cond_resched();
2398 mutex_lock(&root->fs_info->fs_mutex);
2399 btrfs_release_path(root, path);
2400 btrfs_search_slot(NULL, root, &key, path, 0, 0);
2401 progress = 0;
2402 goto next;
2403 }
2404 progress = 1;
2405
Yan73e48b22008-01-03 14:14:39 -05002406 if (btrfs_key_type(&found_key) != BTRFS_EXTENT_ITEM_KEY ||
2407 found_key.objectid + found_key.offset <= cur_byte) {
2408 path->slots[0]++;
Chris Masonedbd8d42007-12-21 16:27:24 -05002409 goto next;
2410 }
Yan73e48b22008-01-03 14:14:39 -05002411
Chris Masonedbd8d42007-12-21 16:27:24 -05002412 total_found++;
2413 cur_byte = found_key.objectid + found_key.offset;
2414 key.objectid = cur_byte;
2415 btrfs_release_path(root, path);
2416 ret = relocate_one_extent(root, path, &found_key);
2417 }
2418
2419 btrfs_release_path(root, path);
2420
2421 if (total_found > 0) {
2422 trans = btrfs_start_transaction(tree_root, 1);
2423 btrfs_commit_transaction(trans, tree_root);
2424
2425 mutex_unlock(&root->fs_info->fs_mutex);
2426 btrfs_clean_old_snapshots(tree_root);
2427 mutex_lock(&root->fs_info->fs_mutex);
2428
2429 trans = btrfs_start_transaction(tree_root, 1);
2430 btrfs_commit_transaction(trans, tree_root);
2431 goto again;
2432 }
2433
2434 trans = btrfs_start_transaction(root, 1);
2435 key.objectid = new_size;
2436 key.offset = 0;
2437 key.type = 0;
2438 while(1) {
Chris Mason4313b392008-01-03 09:08:48 -05002439 u64 ptr;
2440
Chris Masonedbd8d42007-12-21 16:27:24 -05002441 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2442 if (ret < 0)
2443 goto out;
Yan73e48b22008-01-03 14:14:39 -05002444
Chris Masonedbd8d42007-12-21 16:27:24 -05002445 leaf = path->nodes[0];
2446 nritems = btrfs_header_nritems(leaf);
Yan73e48b22008-01-03 14:14:39 -05002447bg_next:
2448 if (path->slots[0] >= nritems) {
2449 ret = btrfs_next_leaf(root, path);
2450 if (ret < 0)
2451 break;
2452 if (ret == 1) {
2453 ret = 0;
2454 break;
Chris Masonedbd8d42007-12-21 16:27:24 -05002455 }
Yan73e48b22008-01-03 14:14:39 -05002456 leaf = path->nodes[0];
Chris Mason1372f8e2008-01-04 09:34:54 -05002457 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2458
2459 /*
2460 * btrfs_next_leaf doesn't cow buffers, we have to
2461 * do the search again
2462 */
2463 memcpy(&key, &found_key, sizeof(key));
2464 btrfs_release_path(root, path);
Chris Mason725c8462008-01-04 16:47:16 -05002465 goto resched_check;
Yan73e48b22008-01-03 14:14:39 -05002466 }
2467
2468 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2469 if (btrfs_key_type(&found_key) != BTRFS_BLOCK_GROUP_ITEM_KEY) {
2470 printk("shrinker found key %Lu %u %Lu\n",
2471 found_key.objectid, found_key.type,
2472 found_key.offset);
2473 path->slots[0]++;
Chris Masonedbd8d42007-12-21 16:27:24 -05002474 goto bg_next;
2475 }
Chris Masonedbd8d42007-12-21 16:27:24 -05002476 ret = get_state_private(&info->block_group_cache,
2477 found_key.objectid, &ptr);
2478 if (!ret)
2479 kfree((void *)(unsigned long)ptr);
2480
2481 clear_extent_bits(&info->block_group_cache, found_key.objectid,
2482 found_key.objectid + found_key.offset - 1,
2483 (unsigned int)-1, GFP_NOFS);
2484
2485 key.objectid = found_key.objectid + 1;
2486 btrfs_del_item(trans, root, path);
2487 btrfs_release_path(root, path);
Chris Mason725c8462008-01-04 16:47:16 -05002488resched_check:
2489 if (need_resched()) {
2490 mutex_unlock(&root->fs_info->fs_mutex);
2491 cond_resched();
2492 mutex_lock(&root->fs_info->fs_mutex);
2493 }
Chris Masonedbd8d42007-12-21 16:27:24 -05002494 }
2495 clear_extent_dirty(&info->free_space_cache, new_size, (u64)-1,
2496 GFP_NOFS);
2497 btrfs_commit_transaction(trans, root);
2498out:
2499 btrfs_free_path(path);
2500 return ret;
2501}
2502
2503int btrfs_grow_extent_tree(struct btrfs_trans_handle *trans,
2504 struct btrfs_root *root, u64 new_size)
2505{
2506 struct btrfs_path *path;
2507 u64 nr = 0;
2508 u64 cur_byte;
2509 u64 old_size;
Chris Masonf9ef6602008-01-03 09:22:38 -05002510 unsigned long rem;
Chris Masonedbd8d42007-12-21 16:27:24 -05002511 struct btrfs_block_group_cache *cache;
2512 struct btrfs_block_group_item *item;
2513 struct btrfs_fs_info *info = root->fs_info;
2514 struct extent_map_tree *block_group_cache;
2515 struct btrfs_key key;
2516 struct extent_buffer *leaf;
2517 int ret;
2518 int bit;
2519
2520 old_size = btrfs_super_total_bytes(&info->super_copy);
2521 block_group_cache = &info->block_group_cache;
2522
2523 root = info->extent_root;
2524
2525 cache = btrfs_lookup_block_group(root->fs_info, old_size - 1);
2526
2527 cur_byte = cache->key.objectid + cache->key.offset;
2528 if (cur_byte >= new_size)
2529 goto set_size;
2530
2531 key.offset = BTRFS_BLOCK_GROUP_SIZE;
2532 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
2533
2534 path = btrfs_alloc_path();
2535 if (!path)
2536 return -ENOMEM;
2537
2538 while(cur_byte < new_size) {
2539 key.objectid = cur_byte;
2540 ret = btrfs_insert_empty_item(trans, root, path, &key,
2541 sizeof(struct btrfs_block_group_item));
2542 BUG_ON(ret);
2543 leaf = path->nodes[0];
2544 item = btrfs_item_ptr(leaf, path->slots[0],
2545 struct btrfs_block_group_item);
2546
2547 btrfs_set_disk_block_group_used(leaf, item, 0);
Chris Masonf9ef6602008-01-03 09:22:38 -05002548 div_long_long_rem(nr, 3, &rem);
2549 if (rem) {
Chris Masonedbd8d42007-12-21 16:27:24 -05002550 btrfs_set_disk_block_group_flags(leaf, item,
2551 BTRFS_BLOCK_GROUP_DATA);
2552 } else {
2553 btrfs_set_disk_block_group_flags(leaf, item, 0);
2554 }
2555 nr++;
2556
2557 cache = kmalloc(sizeof(*cache), GFP_NOFS);
2558 BUG_ON(!cache);
2559
2560 read_extent_buffer(leaf, &cache->item, (unsigned long)item,
2561 sizeof(cache->item));
2562
2563 memcpy(&cache->key, &key, sizeof(key));
2564 cache->cached = 0;
2565 cache->pinned = 0;
2566 cur_byte = key.objectid + key.offset;
2567 btrfs_release_path(root, path);
2568
2569 if (cache->item.flags & BTRFS_BLOCK_GROUP_DATA) {
2570 bit = BLOCK_GROUP_DATA;
2571 cache->data = BTRFS_BLOCK_GROUP_DATA;
2572 } else {
2573 bit = BLOCK_GROUP_METADATA;
2574 cache->data = 0;
2575 }
2576
2577 /* use EXTENT_LOCKED to prevent merging */
2578 set_extent_bits(block_group_cache, key.objectid,
2579 key.objectid + key.offset - 1,
2580 bit | EXTENT_LOCKED, GFP_NOFS);
2581 set_state_private(block_group_cache, key.objectid,
2582 (unsigned long)cache);
2583 }
2584 btrfs_free_path(path);
2585set_size:
2586 btrfs_set_super_total_bytes(&info->super_copy, new_size);
2587 return 0;
2588}
2589
Chris Mason9078a3e2007-04-26 16:46:15 -04002590int btrfs_read_block_groups(struct btrfs_root *root)
2591{
2592 struct btrfs_path *path;
2593 int ret;
2594 int err = 0;
Chris Mason96b51792007-10-15 16:15:19 -04002595 int bit;
Chris Mason9078a3e2007-04-26 16:46:15 -04002596 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04002597 struct btrfs_fs_info *info = root->fs_info;
Chris Mason96b51792007-10-15 16:15:19 -04002598 struct extent_map_tree *block_group_cache;
Chris Mason9078a3e2007-04-26 16:46:15 -04002599 struct btrfs_key key;
2600 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -04002601 struct extent_buffer *leaf;
Chris Mason96b51792007-10-15 16:15:19 -04002602
2603 block_group_cache = &info->block_group_cache;
Chris Mason9078a3e2007-04-26 16:46:15 -04002604
Chris Masonbe744172007-05-06 10:15:01 -04002605 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04002606 key.objectid = 0;
Chris Masondb945352007-10-15 16:15:53 -04002607 key.offset = BTRFS_BLOCK_GROUP_SIZE;
Chris Mason9078a3e2007-04-26 16:46:15 -04002608 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
2609
2610 path = btrfs_alloc_path();
2611 if (!path)
2612 return -ENOMEM;
2613
2614 while(1) {
Chris Masonbe744172007-05-06 10:15:01 -04002615 ret = btrfs_search_slot(NULL, info->extent_root,
Chris Mason9078a3e2007-04-26 16:46:15 -04002616 &key, path, 0, 0);
2617 if (ret != 0) {
2618 err = ret;
2619 break;
2620 }
Chris Mason5f39d392007-10-15 16:14:19 -04002621 leaf = path->nodes[0];
2622 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason9078a3e2007-04-26 16:46:15 -04002623 cache = kmalloc(sizeof(*cache), GFP_NOFS);
2624 if (!cache) {
2625 err = -1;
2626 break;
2627 }
Chris Mason3e1ad542007-05-07 20:03:49 -04002628
Chris Mason5f39d392007-10-15 16:14:19 -04002629 read_extent_buffer(leaf, &cache->item,
2630 btrfs_item_ptr_offset(leaf, path->slots[0]),
2631 sizeof(cache->item));
Chris Mason9078a3e2007-04-26 16:46:15 -04002632 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Masone37c9e62007-05-09 20:13:14 -04002633 cache->cached = 0;
Yan324ae4d2007-11-16 14:57:08 -05002634 cache->pinned = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04002635 key.objectid = found_key.objectid + found_key.offset;
2636 btrfs_release_path(root, path);
Chris Mason5f39d392007-10-15 16:14:19 -04002637
Chris Masonf84a8b32007-11-06 10:26:29 -05002638 if (cache->item.flags & BTRFS_BLOCK_GROUP_MIXED) {
2639 bit = BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA;
2640 cache->data = BTRFS_BLOCK_GROUP_MIXED;
2641 } else if (cache->item.flags & BTRFS_BLOCK_GROUP_DATA) {
Chris Mason96b51792007-10-15 16:15:19 -04002642 bit = BLOCK_GROUP_DATA;
Chris Masonf84a8b32007-11-06 10:26:29 -05002643 cache->data = BTRFS_BLOCK_GROUP_DATA;
Chris Mason96b51792007-10-15 16:15:19 -04002644 } else {
2645 bit = BLOCK_GROUP_METADATA;
2646 cache->data = 0;
Chris Mason31f3c992007-04-30 15:25:45 -04002647 }
Chris Mason96b51792007-10-15 16:15:19 -04002648
2649 /* use EXTENT_LOCKED to prevent merging */
2650 set_extent_bits(block_group_cache, found_key.objectid,
2651 found_key.objectid + found_key.offset - 1,
2652 bit | EXTENT_LOCKED, GFP_NOFS);
2653 set_state_private(block_group_cache, found_key.objectid,
Jens Axboeae2f5412007-10-19 09:22:59 -04002654 (unsigned long)cache);
Chris Mason96b51792007-10-15 16:15:19 -04002655
Chris Mason9078a3e2007-04-26 16:46:15 -04002656 if (key.objectid >=
Chris Masondb945352007-10-15 16:15:53 -04002657 btrfs_super_total_bytes(&info->super_copy))
Chris Mason9078a3e2007-04-26 16:46:15 -04002658 break;
2659 }
2660
2661 btrfs_free_path(path);
2662 return 0;
2663}