blob: cc89df4493abf21445b4497a09ebc8638814c0d0 [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 Masondb945352007-10-15 16:15:53 -0400633 key.objectid = bytenr;
Chris Mason62e27492007-03-15 12:56:47 -0400634 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masondb945352007-10-15 16:15:53 -0400635 key.offset = num_bytes;
Chris Mason5caf2a02007-04-02 11:20:42 -0400636 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -0400637 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400638 if (ret < 0)
639 return ret;
Chris Masona429e512007-04-18 16:15:28 -0400640 if (ret != 0) {
Chris Masona28ec192007-03-06 20:08:01 -0500641 BUG();
Chris Masona429e512007-04-18 16:15:28 -0400642 }
Chris Mason02217ed2007-03-02 16:08:05 -0500643 BUG_ON(ret != 0);
Chris Mason5f39d392007-10-15 16:14:19 -0400644 l = path->nodes[0];
Chris Mason5caf2a02007-04-02 11:20:42 -0400645 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400646 refs = btrfs_extent_refs(l, item);
647 btrfs_set_extent_refs(l, item, refs + 1);
Chris Mason5caf2a02007-04-02 11:20:42 -0400648 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masona28ec192007-03-06 20:08:01 -0500649
Chris Mason5caf2a02007-04-02 11:20:42 -0400650 btrfs_release_path(root->fs_info->extent_root, path);
Chris Mason7bb86312007-12-11 09:25:06 -0500651
652 ret = btrfs_insert_extent_backref(trans, root->fs_info->extent_root,
653 path, bytenr, root_objectid,
654 ref_generation, owner, owner_offset);
655 BUG_ON(ret);
Chris Mason9f5fae22007-03-20 14:38:32 -0400656 finish_current_insert(trans, root->fs_info->extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -0400657 del_pending_extents(trans, root->fs_info->extent_root);
Chris Mason74493f72007-12-11 09:25:06 -0500658
659 btrfs_free_path(path);
Chris Mason02217ed2007-03-02 16:08:05 -0500660 return 0;
661}
662
Chris Masone9d0b132007-08-10 14:06:19 -0400663int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
664 struct btrfs_root *root)
665{
666 finish_current_insert(trans, root->fs_info->extent_root);
667 del_pending_extents(trans, root->fs_info->extent_root);
668 return 0;
669}
670
Chris Masonb18c6682007-04-17 13:26:50 -0400671static int lookup_extent_ref(struct btrfs_trans_handle *trans,
Chris Masondb945352007-10-15 16:15:53 -0400672 struct btrfs_root *root, u64 bytenr,
673 u64 num_bytes, u32 *refs)
Chris Masona28ec192007-03-06 20:08:01 -0500674{
Chris Mason5caf2a02007-04-02 11:20:42 -0400675 struct btrfs_path *path;
Chris Masona28ec192007-03-06 20:08:01 -0500676 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400677 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400678 struct extent_buffer *l;
Chris Mason234b63a2007-03-13 10:46:10 -0400679 struct btrfs_extent_item *item;
Chris Mason5caf2a02007-04-02 11:20:42 -0400680
Chris Masondb945352007-10-15 16:15:53 -0400681 WARN_ON(num_bytes < root->sectorsize);
Chris Mason5caf2a02007-04-02 11:20:42 -0400682 path = btrfs_alloc_path();
Chris Masondb945352007-10-15 16:15:53 -0400683 key.objectid = bytenr;
684 key.offset = num_bytes;
Chris Mason62e27492007-03-15 12:56:47 -0400685 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Mason5caf2a02007-04-02 11:20:42 -0400686 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -0400687 0, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400688 if (ret < 0)
689 goto out;
Chris Mason5f39d392007-10-15 16:14:19 -0400690 if (ret != 0) {
691 btrfs_print_leaf(root, path->nodes[0]);
Chris Masondb945352007-10-15 16:15:53 -0400692 printk("failed to find block number %Lu\n", bytenr);
Chris Masona28ec192007-03-06 20:08:01 -0500693 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -0400694 }
695 l = path->nodes[0];
Chris Mason5caf2a02007-04-02 11:20:42 -0400696 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400697 *refs = btrfs_extent_refs(l, item);
Chris Mason54aa1f42007-06-22 14:16:25 -0400698out:
Chris Mason5caf2a02007-04-02 11:20:42 -0400699 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -0500700 return 0;
701}
702
Chris Masonbe20aa92007-12-17 20:14:01 -0500703u32 btrfs_count_snapshots_in_path(struct btrfs_root *root,
704 struct btrfs_path *count_path,
705 u64 first_extent)
706{
707 struct btrfs_root *extent_root = root->fs_info->extent_root;
708 struct btrfs_path *path;
709 u64 bytenr;
710 u64 found_objectid;
Chris Mason56b453c2008-01-03 09:08:27 -0500711 u64 root_objectid = root->root_key.objectid;
Chris Masonbe20aa92007-12-17 20:14:01 -0500712 u32 total_count = 0;
713 u32 cur_count;
Chris Masonbe20aa92007-12-17 20:14:01 -0500714 u32 nritems;
715 int ret;
716 struct btrfs_key key;
717 struct btrfs_key found_key;
718 struct extent_buffer *l;
719 struct btrfs_extent_item *item;
720 struct btrfs_extent_ref *ref_item;
721 int level = -1;
722
723 path = btrfs_alloc_path();
724again:
725 if (level == -1)
726 bytenr = first_extent;
727 else
728 bytenr = count_path->nodes[level]->start;
729
730 cur_count = 0;
731 key.objectid = bytenr;
732 key.offset = 0;
733
734 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
735 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
736 if (ret < 0)
737 goto out;
738 BUG_ON(ret == 0);
739
740 l = path->nodes[0];
741 btrfs_item_key_to_cpu(l, &found_key, path->slots[0]);
742
743 if (found_key.objectid != bytenr ||
744 found_key.type != BTRFS_EXTENT_ITEM_KEY) {
745 goto out;
746 }
747
748 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Masonbe20aa92007-12-17 20:14:01 -0500749 while (1) {
Chris Masonbd098352008-01-03 13:23:19 -0500750 l = path->nodes[0];
Chris Masonbe20aa92007-12-17 20:14:01 -0500751 nritems = btrfs_header_nritems(l);
752 if (path->slots[0] >= nritems) {
753 ret = btrfs_next_leaf(extent_root, path);
754 if (ret == 0)
755 continue;
756 break;
757 }
758 btrfs_item_key_to_cpu(l, &found_key, path->slots[0]);
759 if (found_key.objectid != bytenr)
760 break;
Chris Masonbd098352008-01-03 13:23:19 -0500761
Chris Masonbe20aa92007-12-17 20:14:01 -0500762 if (found_key.type != BTRFS_EXTENT_REF_KEY) {
763 path->slots[0]++;
764 continue;
765 }
766
767 cur_count++;
768 ref_item = btrfs_item_ptr(l, path->slots[0],
769 struct btrfs_extent_ref);
770 found_objectid = btrfs_ref_root(l, ref_item);
771
Chris Mason56b453c2008-01-03 09:08:27 -0500772 if (found_objectid != root_objectid) {
773 total_count = 2;
Chris Mason4313b392008-01-03 09:08:48 -0500774 goto out;
Chris Mason56b453c2008-01-03 09:08:27 -0500775 }
776 total_count = 1;
Chris Masonbe20aa92007-12-17 20:14:01 -0500777 path->slots[0]++;
778 }
779 if (cur_count == 0) {
780 total_count = 0;
781 goto out;
782 }
Chris Masonbe20aa92007-12-17 20:14:01 -0500783 if (level >= 0 && root->node == count_path->nodes[level])
784 goto out;
785 level++;
786 btrfs_release_path(root, path);
787 goto again;
788
789out:
790 btrfs_free_path(path);
791 return total_count;
Chris Masonbe20aa92007-12-17 20:14:01 -0500792}
Chris Masonc5739bb2007-04-10 09:27:04 -0400793int btrfs_inc_root_ref(struct btrfs_trans_handle *trans,
Chris Mason7bb86312007-12-11 09:25:06 -0500794 struct btrfs_root *root, u64 owner_objectid)
Chris Masonc5739bb2007-04-10 09:27:04 -0400795{
Chris Mason7bb86312007-12-11 09:25:06 -0500796 u64 generation;
797 u64 key_objectid;
798 u64 level;
799 u32 nritems;
800 struct btrfs_disk_key disk_key;
801
802 level = btrfs_header_level(root->node);
803 generation = trans->transid;
804 nritems = btrfs_header_nritems(root->node);
805 if (nritems > 0) {
806 if (level == 0)
807 btrfs_item_key(root->node, &disk_key, 0);
808 else
809 btrfs_node_key(root->node, &disk_key, 0);
810 key_objectid = btrfs_disk_key_objectid(&disk_key);
811 } else {
812 key_objectid = 0;
813 }
Chris Masondb945352007-10-15 16:15:53 -0400814 return btrfs_inc_extent_ref(trans, root, root->node->start,
Chris Mason7bb86312007-12-11 09:25:06 -0500815 root->node->len, owner_objectid,
Chris Masonf6dbff52007-12-13 11:13:32 -0500816 generation, level, key_objectid);
Chris Masonc5739bb2007-04-10 09:27:04 -0400817}
818
Chris Masone089f052007-03-16 16:20:31 -0400819int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400820 struct extent_buffer *buf)
Chris Mason02217ed2007-03-02 16:08:05 -0500821{
Chris Masondb945352007-10-15 16:15:53 -0400822 u64 bytenr;
Chris Mason5f39d392007-10-15 16:14:19 -0400823 u32 nritems;
824 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -0400825 struct btrfs_file_extent_item *fi;
Chris Mason02217ed2007-03-02 16:08:05 -0500826 int i;
Chris Masondb945352007-10-15 16:15:53 -0400827 int level;
Chris Mason6407bf62007-03-27 06:33:00 -0400828 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -0400829 int faili;
Chris Masona28ec192007-03-06 20:08:01 -0500830
Chris Mason3768f362007-03-13 16:47:54 -0400831 if (!root->ref_cows)
Chris Masona28ec192007-03-06 20:08:01 -0500832 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400833
Chris Masondb945352007-10-15 16:15:53 -0400834 level = btrfs_header_level(buf);
Chris Mason5f39d392007-10-15 16:14:19 -0400835 nritems = btrfs_header_nritems(buf);
836 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -0400837 if (level == 0) {
838 u64 disk_bytenr;
Chris Mason5f39d392007-10-15 16:14:19 -0400839 btrfs_item_key_to_cpu(buf, &key, i);
840 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason6407bf62007-03-27 06:33:00 -0400841 continue;
Chris Mason5f39d392007-10-15 16:14:19 -0400842 fi = btrfs_item_ptr(buf, i,
Chris Mason6407bf62007-03-27 06:33:00 -0400843 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400844 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason236454df2007-04-19 13:37:44 -0400845 BTRFS_FILE_EXTENT_INLINE)
846 continue;
Chris Masondb945352007-10-15 16:15:53 -0400847 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
848 if (disk_bytenr == 0)
Chris Mason3a686372007-05-24 13:35:57 -0400849 continue;
Chris Masondb945352007-10-15 16:15:53 -0400850 ret = btrfs_inc_extent_ref(trans, root, disk_bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -0500851 btrfs_file_extent_disk_num_bytes(buf, fi),
852 root->root_key.objectid, trans->transid,
853 key.objectid, key.offset);
Chris Mason54aa1f42007-06-22 14:16:25 -0400854 if (ret) {
855 faili = i;
856 goto fail;
857 }
Chris Mason6407bf62007-03-27 06:33:00 -0400858 } else {
Chris Masondb945352007-10-15 16:15:53 -0400859 bytenr = btrfs_node_blockptr(buf, i);
Chris Mason6caab482007-12-13 09:48:07 -0500860 btrfs_node_key_to_cpu(buf, &key, i);
Chris Masondb945352007-10-15 16:15:53 -0400861 ret = btrfs_inc_extent_ref(trans, root, bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -0500862 btrfs_level_size(root, level - 1),
863 root->root_key.objectid,
Chris Masonf6dbff52007-12-13 11:13:32 -0500864 trans->transid,
865 level - 1, key.objectid);
Chris Mason54aa1f42007-06-22 14:16:25 -0400866 if (ret) {
867 faili = i;
868 goto fail;
869 }
Chris Mason6407bf62007-03-27 06:33:00 -0400870 }
Chris Mason02217ed2007-03-02 16:08:05 -0500871 }
872 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -0400873fail:
Chris Masonccd467d2007-06-28 15:57:36 -0400874 WARN_ON(1);
Chris Mason7bb86312007-12-11 09:25:06 -0500875#if 0
Chris Mason54aa1f42007-06-22 14:16:25 -0400876 for (i =0; i < faili; i++) {
Chris Masondb945352007-10-15 16:15:53 -0400877 if (level == 0) {
878 u64 disk_bytenr;
Chris Mason5f39d392007-10-15 16:14:19 -0400879 btrfs_item_key_to_cpu(buf, &key, i);
880 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -0400881 continue;
Chris Mason5f39d392007-10-15 16:14:19 -0400882 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -0400883 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400884 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -0400885 BTRFS_FILE_EXTENT_INLINE)
886 continue;
Chris Masondb945352007-10-15 16:15:53 -0400887 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
888 if (disk_bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -0400889 continue;
Chris Masondb945352007-10-15 16:15:53 -0400890 err = btrfs_free_extent(trans, root, disk_bytenr,
891 btrfs_file_extent_disk_num_bytes(buf,
Chris Mason5f39d392007-10-15 16:14:19 -0400892 fi), 0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400893 BUG_ON(err);
894 } else {
Chris Masondb945352007-10-15 16:15:53 -0400895 bytenr = btrfs_node_blockptr(buf, i);
896 err = btrfs_free_extent(trans, root, bytenr,
897 btrfs_level_size(root, level - 1), 0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400898 BUG_ON(err);
899 }
900 }
Chris Mason7bb86312007-12-11 09:25:06 -0500901#endif
Chris Mason54aa1f42007-06-22 14:16:25 -0400902 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -0500903}
904
Chris Mason9078a3e2007-04-26 16:46:15 -0400905static int write_one_cache_group(struct btrfs_trans_handle *trans,
906 struct btrfs_root *root,
907 struct btrfs_path *path,
908 struct btrfs_block_group_cache *cache)
909{
910 int ret;
911 int pending_ret;
912 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -0400913 unsigned long bi;
914 struct extent_buffer *leaf;
Chris Mason9078a3e2007-04-26 16:46:15 -0400915
Chris Mason9078a3e2007-04-26 16:46:15 -0400916 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400917 if (ret < 0)
918 goto fail;
Chris Mason9078a3e2007-04-26 16:46:15 -0400919 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -0400920
921 leaf = path->nodes[0];
922 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
923 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
924 btrfs_mark_buffer_dirty(leaf);
Chris Mason9078a3e2007-04-26 16:46:15 -0400925 btrfs_release_path(extent_root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -0400926fail:
Chris Mason9078a3e2007-04-26 16:46:15 -0400927 finish_current_insert(trans, extent_root);
928 pending_ret = del_pending_extents(trans, extent_root);
929 if (ret)
930 return ret;
931 if (pending_ret)
932 return pending_ret;
933 return 0;
934
935}
936
Chris Mason96b51792007-10-15 16:15:19 -0400937int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
938 struct btrfs_root *root)
Chris Mason9078a3e2007-04-26 16:46:15 -0400939{
Chris Mason96b51792007-10-15 16:15:19 -0400940 struct extent_map_tree *block_group_cache;
941 struct btrfs_block_group_cache *cache;
Chris Mason9078a3e2007-04-26 16:46:15 -0400942 int ret;
943 int err = 0;
944 int werr = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -0400945 struct btrfs_path *path;
Chris Mason96b51792007-10-15 16:15:19 -0400946 u64 last = 0;
947 u64 start;
948 u64 end;
949 u64 ptr;
Chris Mason9078a3e2007-04-26 16:46:15 -0400950
Chris Mason96b51792007-10-15 16:15:19 -0400951 block_group_cache = &root->fs_info->block_group_cache;
Chris Mason9078a3e2007-04-26 16:46:15 -0400952 path = btrfs_alloc_path();
953 if (!path)
954 return -ENOMEM;
955
956 while(1) {
Chris Mason96b51792007-10-15 16:15:19 -0400957 ret = find_first_extent_bit(block_group_cache, last,
958 &start, &end, BLOCK_GROUP_DIRTY);
959 if (ret)
Chris Mason9078a3e2007-04-26 16:46:15 -0400960 break;
Chris Mason54aa1f42007-06-22 14:16:25 -0400961
Chris Mason96b51792007-10-15 16:15:19 -0400962 last = end + 1;
963 ret = get_state_private(block_group_cache, start, &ptr);
964 if (ret)
965 break;
966
Jens Axboeae2f5412007-10-19 09:22:59 -0400967 cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
Chris Mason96b51792007-10-15 16:15:19 -0400968 err = write_one_cache_group(trans, root,
969 path, cache);
970 /*
971 * if we fail to write the cache group, we want
972 * to keep it marked dirty in hopes that a later
973 * write will work
974 */
975 if (err) {
976 werr = err;
977 continue;
Chris Mason9078a3e2007-04-26 16:46:15 -0400978 }
Chris Mason96b51792007-10-15 16:15:19 -0400979 clear_extent_bits(block_group_cache, start, end,
980 BLOCK_GROUP_DIRTY, GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -0400981 }
982 btrfs_free_path(path);
983 return werr;
984}
985
986static int update_block_group(struct btrfs_trans_handle *trans,
987 struct btrfs_root *root,
Chris Masondb945352007-10-15 16:15:53 -0400988 u64 bytenr, u64 num_bytes, int alloc,
989 int mark_free, int data)
Chris Mason9078a3e2007-04-26 16:46:15 -0400990{
991 struct btrfs_block_group_cache *cache;
992 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -0400993 u64 total = num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -0400994 u64 old_val;
Chris Masondb945352007-10-15 16:15:53 -0400995 u64 byte_in_group;
Chris Mason96b51792007-10-15 16:15:19 -0400996 u64 start;
997 u64 end;
Chris Mason3e1ad542007-05-07 20:03:49 -0400998
Chris Mason9078a3e2007-04-26 16:46:15 -0400999 while(total) {
Chris Masondb945352007-10-15 16:15:53 -04001000 cache = btrfs_lookup_block_group(info, bytenr);
Chris Mason3e1ad542007-05-07 20:03:49 -04001001 if (!cache) {
Chris Mason9078a3e2007-04-26 16:46:15 -04001002 return -1;
Chris Masoncd1bc462007-04-27 10:08:34 -04001003 }
Chris Masondb945352007-10-15 16:15:53 -04001004 byte_in_group = bytenr - cache->key.objectid;
1005 WARN_ON(byte_in_group > cache->key.offset);
Chris Mason96b51792007-10-15 16:15:19 -04001006 start = cache->key.objectid;
1007 end = start + cache->key.offset - 1;
1008 set_extent_bits(&info->block_group_cache, start, end,
1009 BLOCK_GROUP_DIRTY, GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -04001010
1011 old_val = btrfs_block_group_used(&cache->item);
Chris Masondb945352007-10-15 16:15:53 -04001012 num_bytes = min(total, cache->key.offset - byte_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -04001013 if (alloc) {
Chris Mason1e2677e2007-05-29 16:52:18 -04001014 if (cache->data != data &&
Chris Mason84f54cf2007-06-12 07:43:08 -04001015 old_val < (cache->key.offset >> 1)) {
Chris Mason96b51792007-10-15 16:15:19 -04001016 int bit_to_clear;
1017 int bit_to_set;
Chris Mason96b51792007-10-15 16:15:19 -04001018 cache->data = data;
Chris Mason1e2677e2007-05-29 16:52:18 -04001019 if (data) {
Yanb97f9202007-11-01 11:28:41 -04001020 bit_to_clear = BLOCK_GROUP_METADATA;
1021 bit_to_set = BLOCK_GROUP_DATA;
Chris Masonf84a8b32007-11-06 10:26:29 -05001022 cache->item.flags &=
1023 ~BTRFS_BLOCK_GROUP_MIXED;
Chris Mason1e2677e2007-05-29 16:52:18 -04001024 cache->item.flags |=
1025 BTRFS_BLOCK_GROUP_DATA;
1026 } else {
Yanb97f9202007-11-01 11:28:41 -04001027 bit_to_clear = BLOCK_GROUP_DATA;
1028 bit_to_set = BLOCK_GROUP_METADATA;
Chris Mason1e2677e2007-05-29 16:52:18 -04001029 cache->item.flags &=
Chris Masonf84a8b32007-11-06 10:26:29 -05001030 ~BTRFS_BLOCK_GROUP_MIXED;
1031 cache->item.flags &=
Chris Mason1e2677e2007-05-29 16:52:18 -04001032 ~BTRFS_BLOCK_GROUP_DATA;
1033 }
Chris Mason96b51792007-10-15 16:15:19 -04001034 clear_extent_bits(&info->block_group_cache,
1035 start, end, bit_to_clear,
1036 GFP_NOFS);
1037 set_extent_bits(&info->block_group_cache,
1038 start, end, bit_to_set,
1039 GFP_NOFS);
Chris Masonf84a8b32007-11-06 10:26:29 -05001040 } else if (cache->data != data &&
1041 cache->data != BTRFS_BLOCK_GROUP_MIXED) {
1042 cache->data = BTRFS_BLOCK_GROUP_MIXED;
1043 set_extent_bits(&info->block_group_cache,
1044 start, end,
1045 BLOCK_GROUP_DATA |
1046 BLOCK_GROUP_METADATA,
1047 GFP_NOFS);
Chris Mason1e2677e2007-05-29 16:52:18 -04001048 }
Chris Masondb945352007-10-15 16:15:53 -04001049 old_val += num_bytes;
Chris Masoncd1bc462007-04-27 10:08:34 -04001050 } else {
Chris Masondb945352007-10-15 16:15:53 -04001051 old_val -= num_bytes;
Chris Masonf510cfe2007-10-15 16:14:48 -04001052 if (mark_free) {
1053 set_extent_dirty(&info->free_space_cache,
Chris Masondb945352007-10-15 16:15:53 -04001054 bytenr, bytenr + num_bytes - 1,
Chris Masonf510cfe2007-10-15 16:14:48 -04001055 GFP_NOFS);
Chris Masone37c9e62007-05-09 20:13:14 -04001056 }
Chris Masoncd1bc462007-04-27 10:08:34 -04001057 }
Chris Mason9078a3e2007-04-26 16:46:15 -04001058 btrfs_set_block_group_used(&cache->item, old_val);
Chris Masondb945352007-10-15 16:15:53 -04001059 total -= num_bytes;
1060 bytenr += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04001061 }
1062 return 0;
1063}
Yan324ae4d2007-11-16 14:57:08 -05001064static int update_pinned_extents(struct btrfs_root *root,
1065 u64 bytenr, u64 num, int pin)
1066{
1067 u64 len;
1068 struct btrfs_block_group_cache *cache;
1069 struct btrfs_fs_info *fs_info = root->fs_info;
1070
1071 if (pin) {
1072 set_extent_dirty(&fs_info->pinned_extents,
1073 bytenr, bytenr + num - 1, GFP_NOFS);
1074 } else {
1075 clear_extent_dirty(&fs_info->pinned_extents,
1076 bytenr, bytenr + num - 1, GFP_NOFS);
1077 }
1078 while (num > 0) {
1079 cache = btrfs_lookup_block_group(fs_info, bytenr);
1080 WARN_ON(!cache);
1081 len = min(num, cache->key.offset -
1082 (bytenr - cache->key.objectid));
1083 if (pin) {
1084 cache->pinned += len;
1085 fs_info->total_pinned += len;
1086 } else {
1087 cache->pinned -= len;
1088 fs_info->total_pinned -= len;
1089 }
1090 bytenr += len;
1091 num -= len;
1092 }
1093 return 0;
1094}
Chris Mason9078a3e2007-04-26 16:46:15 -04001095
Chris Mason1a5bc162007-10-15 16:15:26 -04001096int btrfs_copy_pinned(struct btrfs_root *root, struct extent_map_tree *copy)
Chris Masonccd467d2007-06-28 15:57:36 -04001097{
Chris Masonccd467d2007-06-28 15:57:36 -04001098 u64 last = 0;
Chris Mason1a5bc162007-10-15 16:15:26 -04001099 u64 start;
1100 u64 end;
1101 struct extent_map_tree *pinned_extents = &root->fs_info->pinned_extents;
Chris Masonccd467d2007-06-28 15:57:36 -04001102 int ret;
Chris Masonccd467d2007-06-28 15:57:36 -04001103
1104 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04001105 ret = find_first_extent_bit(pinned_extents, last,
1106 &start, &end, EXTENT_DIRTY);
1107 if (ret)
Chris Masonccd467d2007-06-28 15:57:36 -04001108 break;
Chris Mason1a5bc162007-10-15 16:15:26 -04001109 set_extent_dirty(copy, start, end, GFP_NOFS);
1110 last = end + 1;
Chris Masonccd467d2007-06-28 15:57:36 -04001111 }
1112 return 0;
1113}
1114
1115int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
1116 struct btrfs_root *root,
Chris Mason1a5bc162007-10-15 16:15:26 -04001117 struct extent_map_tree *unpin)
Chris Masona28ec192007-03-06 20:08:01 -05001118{
Chris Mason1a5bc162007-10-15 16:15:26 -04001119 u64 start;
1120 u64 end;
Chris Masona28ec192007-03-06 20:08:01 -05001121 int ret;
Chris Masonf510cfe2007-10-15 16:14:48 -04001122 struct extent_map_tree *free_space_cache;
Chris Masonf510cfe2007-10-15 16:14:48 -04001123 free_space_cache = &root->fs_info->free_space_cache;
Chris Masona28ec192007-03-06 20:08:01 -05001124
1125 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04001126 ret = find_first_extent_bit(unpin, 0, &start, &end,
1127 EXTENT_DIRTY);
1128 if (ret)
Chris Masona28ec192007-03-06 20:08:01 -05001129 break;
Yan324ae4d2007-11-16 14:57:08 -05001130 update_pinned_extents(root, start, end + 1 - start, 0);
Chris Mason1a5bc162007-10-15 16:15:26 -04001131 clear_extent_dirty(unpin, start, end, GFP_NOFS);
1132 set_extent_dirty(free_space_cache, start, end, GFP_NOFS);
Chris Masona28ec192007-03-06 20:08:01 -05001133 }
1134 return 0;
1135}
1136
Chris Mason98ed5172008-01-03 10:01:48 -05001137static int finish_current_insert(struct btrfs_trans_handle *trans,
1138 struct btrfs_root *extent_root)
Chris Mason037e6392007-03-07 11:50:24 -05001139{
Chris Mason7bb86312007-12-11 09:25:06 -05001140 u64 start;
1141 u64 end;
1142 struct btrfs_fs_info *info = extent_root->fs_info;
Chris Masond8d5f3e2007-12-11 12:42:00 -05001143 struct extent_buffer *eb;
Chris Mason7bb86312007-12-11 09:25:06 -05001144 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -04001145 struct btrfs_key ins;
Chris Masond8d5f3e2007-12-11 12:42:00 -05001146 struct btrfs_disk_key first;
Chris Mason234b63a2007-03-13 10:46:10 -04001147 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -05001148 int ret;
Chris Masond8d5f3e2007-12-11 12:42:00 -05001149 int level;
Chris Mason1a5bc162007-10-15 16:15:26 -04001150 int err = 0;
Chris Mason037e6392007-03-07 11:50:24 -05001151
Chris Mason5f39d392007-10-15 16:14:19 -04001152 btrfs_set_stack_extent_refs(&extent_item, 1);
Chris Mason62e27492007-03-15 12:56:47 -04001153 btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
Chris Mason7bb86312007-12-11 09:25:06 -05001154 path = btrfs_alloc_path();
Chris Mason037e6392007-03-07 11:50:24 -05001155
Chris Mason26b80032007-08-08 20:17:12 -04001156 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04001157 ret = find_first_extent_bit(&info->extent_ins, 0, &start,
1158 &end, EXTENT_LOCKED);
1159 if (ret)
Chris Mason26b80032007-08-08 20:17:12 -04001160 break;
1161
Chris Mason1a5bc162007-10-15 16:15:26 -04001162 ins.objectid = start;
1163 ins.offset = end + 1 - start;
1164 err = btrfs_insert_item(trans, extent_root, &ins,
1165 &extent_item, sizeof(extent_item));
1166 clear_extent_bits(&info->extent_ins, start, end, EXTENT_LOCKED,
1167 GFP_NOFS);
Chris Masond8d5f3e2007-12-11 12:42:00 -05001168 eb = read_tree_block(extent_root, ins.objectid, ins.offset);
1169 level = btrfs_header_level(eb);
1170 if (level == 0) {
1171 btrfs_item_key(eb, &first, 0);
1172 } else {
1173 btrfs_node_key(eb, &first, 0);
1174 }
Chris Mason7bb86312007-12-11 09:25:06 -05001175 err = btrfs_insert_extent_backref(trans, extent_root, path,
1176 start, extent_root->root_key.objectid,
Chris Masonf6dbff52007-12-13 11:13:32 -05001177 0, level,
1178 btrfs_disk_key_objectid(&first));
Chris Mason7bb86312007-12-11 09:25:06 -05001179 BUG_ON(err);
Chris Masond8d5f3e2007-12-11 12:42:00 -05001180 free_extent_buffer(eb);
Chris Mason037e6392007-03-07 11:50:24 -05001181 }
Chris Mason7bb86312007-12-11 09:25:06 -05001182 btrfs_free_path(path);
Chris Mason037e6392007-03-07 11:50:24 -05001183 return 0;
1184}
1185
Chris Masondb945352007-10-15 16:15:53 -04001186static int pin_down_bytes(struct btrfs_root *root, u64 bytenr, u32 num_bytes,
1187 int pending)
Chris Masone20d96d2007-03-22 12:13:20 -04001188{
Chris Mason1a5bc162007-10-15 16:15:26 -04001189 int err = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001190 struct extent_buffer *buf;
Chris Mason78fae272007-03-25 11:35:08 -04001191
Chris Masonf4b9aa82007-03-27 11:05:53 -04001192 if (!pending) {
Chris Masondb945352007-10-15 16:15:53 -04001193 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
Chris Mason5f39d392007-10-15 16:14:19 -04001194 if (buf) {
1195 if (btrfs_buffer_uptodate(buf)) {
Chris Mason2c90e5d2007-04-02 10:50:19 -04001196 u64 transid =
1197 root->fs_info->running_transaction->transid;
Chris Mason5f39d392007-10-15 16:14:19 -04001198 if (btrfs_header_generation(buf) == transid) {
1199 free_extent_buffer(buf);
Yanc5492282007-11-06 10:25:25 -05001200 return 1;
Chris Mason2c90e5d2007-04-02 10:50:19 -04001201 }
Chris Masonf4b9aa82007-03-27 11:05:53 -04001202 }
Chris Mason5f39d392007-10-15 16:14:19 -04001203 free_extent_buffer(buf);
Chris Mason8ef97622007-03-26 10:15:30 -04001204 }
Yan324ae4d2007-11-16 14:57:08 -05001205 update_pinned_extents(root, bytenr, num_bytes, 1);
Chris Masonf4b9aa82007-03-27 11:05:53 -04001206 } else {
Chris Mason1a5bc162007-10-15 16:15:26 -04001207 set_extent_bits(&root->fs_info->pending_del,
Chris Masondb945352007-10-15 16:15:53 -04001208 bytenr, bytenr + num_bytes - 1,
1209 EXTENT_LOCKED, GFP_NOFS);
Chris Masonf4b9aa82007-03-27 11:05:53 -04001210 }
Chris Masonbe744172007-05-06 10:15:01 -04001211 BUG_ON(err < 0);
Chris Masone20d96d2007-03-22 12:13:20 -04001212 return 0;
1213}
1214
Chris Masona28ec192007-03-06 20:08:01 -05001215/*
1216 * remove an extent from the root, returns 0 on success
1217 */
Chris Masone089f052007-03-16 16:20:31 -04001218static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason7bb86312007-12-11 09:25:06 -05001219 *root, u64 bytenr, u64 num_bytes,
1220 u64 root_objectid, u64 ref_generation,
1221 u64 owner_objectid, u64 owner_offset, int pin,
Chris Masone37c9e62007-05-09 20:13:14 -04001222 int mark_free)
Chris Masona28ec192007-03-06 20:08:01 -05001223{
Chris Mason5caf2a02007-04-02 11:20:42 -04001224 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -04001225 struct btrfs_key key;
Chris Mason1261ec42007-03-20 20:35:03 -04001226 struct btrfs_fs_info *info = root->fs_info;
1227 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04001228 struct extent_buffer *leaf;
Chris Masona28ec192007-03-06 20:08:01 -05001229 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -04001230 struct btrfs_extent_item *ei;
Chris Masoncf27e1e2007-03-13 09:49:06 -04001231 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -05001232
Chris Masondb945352007-10-15 16:15:53 -04001233 key.objectid = bytenr;
Chris Mason62e27492007-03-15 12:56:47 -04001234 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masondb945352007-10-15 16:15:53 -04001235 key.offset = num_bytes;
Chris Masona28ec192007-03-06 20:08:01 -05001236
Chris Mason5caf2a02007-04-02 11:20:42 -04001237 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04001238 if (!path)
1239 return -ENOMEM;
1240
Chris Mason7bb86312007-12-11 09:25:06 -05001241 ret = lookup_extent_backref(trans, extent_root, path,
1242 bytenr, root_objectid,
1243 ref_generation,
1244 owner_objectid, owner_offset, 1);
1245 if (ret == 0) {
1246 ret = btrfs_del_item(trans, extent_root, path);
1247 } else {
1248 btrfs_print_leaf(extent_root, path->nodes[0]);
1249 WARN_ON(1);
1250 printk("Unable to find ref byte nr %Lu root %Lu "
1251 " gen %Lu owner %Lu offset %Lu\n", bytenr,
1252 root_objectid, ref_generation, owner_objectid,
1253 owner_offset);
1254 }
1255 btrfs_release_path(extent_root, path);
Chris Mason5caf2a02007-04-02 11:20:42 -04001256 ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001257 if (ret < 0)
1258 return ret;
1259 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04001260
1261 leaf = path->nodes[0];
1262 ei = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason123abc82007-03-14 14:14:43 -04001263 struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001264 refs = btrfs_extent_refs(leaf, ei);
1265 BUG_ON(refs == 0);
1266 refs -= 1;
1267 btrfs_set_extent_refs(leaf, ei, refs);
1268 btrfs_mark_buffer_dirty(leaf);
1269
Chris Masoncf27e1e2007-03-13 09:49:06 -04001270 if (refs == 0) {
Chris Masondb945352007-10-15 16:15:53 -04001271 u64 super_used;
1272 u64 root_used;
Chris Mason78fae272007-03-25 11:35:08 -04001273
1274 if (pin) {
Chris Masondb945352007-10-15 16:15:53 -04001275 ret = pin_down_bytes(root, bytenr, num_bytes, 0);
Yanc5492282007-11-06 10:25:25 -05001276 if (ret > 0)
1277 mark_free = 1;
1278 BUG_ON(ret < 0);
Chris Mason78fae272007-03-25 11:35:08 -04001279 }
1280
Josef Bacik58176a92007-08-29 15:47:34 -04001281 /* block accounting for super block */
Chris Masondb945352007-10-15 16:15:53 -04001282 super_used = btrfs_super_bytes_used(&info->super_copy);
1283 btrfs_set_super_bytes_used(&info->super_copy,
1284 super_used - num_bytes);
Josef Bacik58176a92007-08-29 15:47:34 -04001285
1286 /* block accounting for root item */
Chris Masondb945352007-10-15 16:15:53 -04001287 root_used = btrfs_root_used(&root->root_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001288 btrfs_set_root_used(&root->root_item,
Chris Masondb945352007-10-15 16:15:53 -04001289 root_used - num_bytes);
Josef Bacik58176a92007-08-29 15:47:34 -04001290
Chris Mason5caf2a02007-04-02 11:20:42 -04001291 ret = btrfs_del_item(trans, extent_root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -04001292 if (ret) {
1293 return ret;
1294 }
Chris Masondb945352007-10-15 16:15:53 -04001295 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
Chris Mason1e2677e2007-05-29 16:52:18 -04001296 mark_free, 0);
Chris Mason9078a3e2007-04-26 16:46:15 -04001297 BUG_ON(ret);
Chris Masona28ec192007-03-06 20:08:01 -05001298 }
Chris Mason5caf2a02007-04-02 11:20:42 -04001299 btrfs_free_path(path);
Chris Masone089f052007-03-16 16:20:31 -04001300 finish_current_insert(trans, extent_root);
Chris Masona28ec192007-03-06 20:08:01 -05001301 return ret;
1302}
1303
1304/*
Chris Masonfec577f2007-02-26 10:40:21 -05001305 * find all the blocks marked as pending in the radix tree and remove
1306 * them from the extent map
1307 */
Chris Masone089f052007-03-16 16:20:31 -04001308static int del_pending_extents(struct btrfs_trans_handle *trans, struct
1309 btrfs_root *extent_root)
Chris Masonfec577f2007-02-26 10:40:21 -05001310{
1311 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -04001312 int err = 0;
Chris Mason1a5bc162007-10-15 16:15:26 -04001313 u64 start;
1314 u64 end;
1315 struct extent_map_tree *pending_del;
1316 struct extent_map_tree *pinned_extents;
Chris Mason8ef97622007-03-26 10:15:30 -04001317
Chris Mason1a5bc162007-10-15 16:15:26 -04001318 pending_del = &extent_root->fs_info->pending_del;
1319 pinned_extents = &extent_root->fs_info->pinned_extents;
Chris Masonfec577f2007-02-26 10:40:21 -05001320
1321 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04001322 ret = find_first_extent_bit(pending_del, 0, &start, &end,
1323 EXTENT_LOCKED);
1324 if (ret)
Chris Masonfec577f2007-02-26 10:40:21 -05001325 break;
Yan324ae4d2007-11-16 14:57:08 -05001326 update_pinned_extents(extent_root, start, end + 1 - start, 1);
Chris Mason1a5bc162007-10-15 16:15:26 -04001327 clear_extent_bits(pending_del, start, end, EXTENT_LOCKED,
1328 GFP_NOFS);
1329 ret = __free_extent(trans, extent_root,
Chris Mason7bb86312007-12-11 09:25:06 -05001330 start, end + 1 - start,
1331 extent_root->root_key.objectid,
1332 0, 0, 0, 0, 0);
Chris Mason1a5bc162007-10-15 16:15:26 -04001333 if (ret)
1334 err = ret;
Chris Masonfec577f2007-02-26 10:40:21 -05001335 }
Chris Masone20d96d2007-03-22 12:13:20 -04001336 return err;
Chris Masonfec577f2007-02-26 10:40:21 -05001337}
1338
1339/*
1340 * remove an extent from the root, returns 0 on success
1341 */
Chris Masone089f052007-03-16 16:20:31 -04001342int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason7bb86312007-12-11 09:25:06 -05001343 *root, u64 bytenr, u64 num_bytes,
1344 u64 root_objectid, u64 ref_generation,
1345 u64 owner_objectid, u64 owner_offset, int pin)
Chris Masonfec577f2007-02-26 10:40:21 -05001346{
Chris Mason9f5fae22007-03-20 14:38:32 -04001347 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Masonfec577f2007-02-26 10:40:21 -05001348 int pending_ret;
1349 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05001350
Chris Masondb945352007-10-15 16:15:53 -04001351 WARN_ON(num_bytes < root->sectorsize);
Chris Mason7bb86312007-12-11 09:25:06 -05001352 if (!root->ref_cows)
1353 ref_generation = 0;
1354
Chris Masona28ec192007-03-06 20:08:01 -05001355 if (root == extent_root) {
Chris Masondb945352007-10-15 16:15:53 -04001356 pin_down_bytes(root, bytenr, num_bytes, 1);
Chris Masona28ec192007-03-06 20:08:01 -05001357 return 0;
1358 }
Chris Mason7bb86312007-12-11 09:25:06 -05001359 ret = __free_extent(trans, root, bytenr, num_bytes, root_objectid,
1360 ref_generation, owner_objectid, owner_offset,
1361 pin, pin == 0);
Chris Masone20d96d2007-03-22 12:13:20 -04001362 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
Chris Masonfec577f2007-02-26 10:40:21 -05001363 return ret ? ret : pending_ret;
1364}
1365
Chris Mason87ee04e2007-11-30 11:30:34 -05001366static u64 stripe_align(struct btrfs_root *root, u64 val)
1367{
1368 u64 mask = ((u64)root->stripesize - 1);
1369 u64 ret = (val + mask) & ~mask;
1370 return ret;
1371}
1372
Chris Masonfec577f2007-02-26 10:40:21 -05001373/*
1374 * walks the btree of allocated extents and find a hole of a given size.
1375 * The key ins is changed to record the hole:
1376 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -04001377 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -05001378 * ins->offset == number of blocks
1379 * Any available blocks before search_start are skipped.
1380 */
Chris Mason98ed5172008-01-03 10:01:48 -05001381static int noinline find_free_extent(struct btrfs_trans_handle *trans,
1382 struct btrfs_root *orig_root,
1383 u64 num_bytes, u64 empty_size,
1384 u64 search_start, u64 search_end,
1385 u64 hint_byte, struct btrfs_key *ins,
1386 u64 exclude_start, u64 exclude_nr,
1387 int data)
Chris Masonfec577f2007-02-26 10:40:21 -05001388{
Chris Mason5caf2a02007-04-02 11:20:42 -04001389 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -04001390 struct btrfs_key key;
Chris Masonfec577f2007-02-26 10:40:21 -05001391 u64 hole_size = 0;
Chris Mason87ee04e2007-11-30 11:30:34 -05001392 u64 aligned;
1393 int ret;
Chris Masonfec577f2007-02-26 10:40:21 -05001394 int slot = 0;
Chris Masondb945352007-10-15 16:15:53 -04001395 u64 last_byte = 0;
Chris Masonbe744172007-05-06 10:15:01 -04001396 u64 orig_search_start = search_start;
Chris Masonfec577f2007-02-26 10:40:21 -05001397 int start_found;
Chris Mason5f39d392007-10-15 16:14:19 -04001398 struct extent_buffer *l;
Chris Mason9f5fae22007-03-20 14:38:32 -04001399 struct btrfs_root * root = orig_root->fs_info->extent_root;
Chris Masonf2458e12007-04-25 15:52:25 -04001400 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -04001401 u64 total_needed = num_bytes;
Chris Masone20d96d2007-03-22 12:13:20 -04001402 int level;
Chris Masonbe08c1b2007-05-03 09:06:49 -04001403 struct btrfs_block_group_cache *block_group;
Chris Masonbe744172007-05-06 10:15:01 -04001404 int full_scan = 0;
Chris Masonfbdc7622007-05-30 10:22:12 -04001405 int wrapped = 0;
Chris Masonf84a8b32007-11-06 10:26:29 -05001406 u64 cached_start;
Chris Masonfec577f2007-02-26 10:40:21 -05001407
Chris Masondb945352007-10-15 16:15:53 -04001408 WARN_ON(num_bytes < root->sectorsize);
Chris Masonb1a4d962007-04-04 15:27:52 -04001409 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
1410
Chris Mason5f39d392007-10-15 16:14:19 -04001411 level = btrfs_header_level(root->node);
1412
Chris Mason015a739c2007-11-26 16:15:16 -08001413 if (num_bytes >= 32 * 1024 * 1024 && hint_byte) {
Chris Mason257d0ce2007-11-07 21:08:16 -05001414 data = BTRFS_BLOCK_GROUP_MIXED;
1415 }
1416
Chris Mason3e1ad542007-05-07 20:03:49 -04001417 if (search_end == (u64)-1)
Chris Masondb945352007-10-15 16:15:53 -04001418 search_end = btrfs_super_total_bytes(&info->super_copy);
1419 if (hint_byte) {
1420 block_group = btrfs_lookup_block_group(info, hint_byte);
Chris Mason1a2b2ac2007-12-04 13:18:24 -05001421 if (!block_group)
1422 hint_byte = search_start;
Chris Masonbe744172007-05-06 10:15:01 -04001423 block_group = btrfs_find_block_group(root, block_group,
Chris Masondb945352007-10-15 16:15:53 -04001424 hint_byte, data, 1);
Chris Masonbe744172007-05-06 10:15:01 -04001425 } else {
1426 block_group = btrfs_find_block_group(root,
Chris Mason1a2b2ac2007-12-04 13:18:24 -05001427 trans->block_group,
1428 search_start, data, 1);
Chris Masonbe744172007-05-06 10:15:01 -04001429 }
1430
Chris Mason6702ed42007-08-07 16:15:09 -04001431 total_needed += empty_size;
Chris Masone0115992007-06-19 16:23:05 -04001432 path = btrfs_alloc_path();
Chris Masonbe744172007-05-06 10:15:01 -04001433check_failed:
Chris Mason70b043f2007-12-13 09:02:46 -05001434 if (!block_group) {
1435 block_group = btrfs_lookup_block_group(info, search_start);
1436 if (!block_group)
1437 block_group = btrfs_lookup_block_group(info,
1438 orig_search_start);
1439 }
Yan5e5745d2007-11-16 14:57:09 -05001440 search_start = find_search_start(root, &block_group, search_start,
Yand548ee52008-01-03 13:56:30 -05001441 total_needed, data);
Chris Mason87ee04e2007-11-30 11:30:34 -05001442 search_start = stripe_align(root, search_start);
Chris Masonf84a8b32007-11-06 10:26:29 -05001443 cached_start = search_start;
Chris Mason5caf2a02007-04-02 11:20:42 -04001444 btrfs_init_path(path);
Chris Masonfec577f2007-02-26 10:40:21 -05001445 ins->objectid = search_start;
1446 ins->offset = 0;
Chris Masonfec577f2007-02-26 10:40:21 -05001447 start_found = 0;
Chris Mason2cc58cf2007-08-27 16:49:44 -04001448 path->reada = 2;
Chris Masone37c9e62007-05-09 20:13:14 -04001449
Chris Mason5caf2a02007-04-02 11:20:42 -04001450 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
Chris Mason0f70abe2007-02-28 16:46:22 -05001451 if (ret < 0)
1452 goto error;
Yand548ee52008-01-03 13:56:30 -05001453 ret = find_previous_extent(root, path);
1454 if (ret < 0)
1455 goto error;
Chris Mason5f39d392007-10-15 16:14:19 -04001456 l = path->nodes[0];
1457 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
Chris Masonfec577f2007-02-26 10:40:21 -05001458 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001459 l = path->nodes[0];
Chris Mason5caf2a02007-04-02 11:20:42 -04001460 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04001461 if (slot >= btrfs_header_nritems(l)) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001462 ret = btrfs_next_leaf(root, path);
Chris Masonfec577f2007-02-26 10:40:21 -05001463 if (ret == 0)
1464 continue;
Chris Mason0f70abe2007-02-28 16:46:22 -05001465 if (ret < 0)
1466 goto error;
Chris Masone19caa52007-10-15 16:17:44 -04001467
1468 search_start = max(search_start,
1469 block_group->key.objectid);
Chris Masonfec577f2007-02-26 10:40:21 -05001470 if (!start_found) {
Chris Mason87ee04e2007-11-30 11:30:34 -05001471 aligned = stripe_align(root, search_start);
1472 ins->objectid = aligned;
1473 if (aligned >= search_end) {
1474 ret = -ENOSPC;
1475 goto error;
1476 }
1477 ins->offset = search_end - aligned;
Chris Masonfec577f2007-02-26 10:40:21 -05001478 start_found = 1;
1479 goto check_pending;
1480 }
Chris Mason87ee04e2007-11-30 11:30:34 -05001481 ins->objectid = stripe_align(root,
1482 last_byte > search_start ?
1483 last_byte : search_start);
1484 if (search_end <= ins->objectid) {
1485 ret = -ENOSPC;
1486 goto error;
1487 }
Chris Mason3e1ad542007-05-07 20:03:49 -04001488 ins->offset = search_end - ins->objectid;
Chris Masone19caa52007-10-15 16:17:44 -04001489 BUG_ON(ins->objectid >= search_end);
Chris Masonfec577f2007-02-26 10:40:21 -05001490 goto check_pending;
1491 }
Chris Mason5f39d392007-10-15 16:14:19 -04001492 btrfs_item_key_to_cpu(l, &key, slot);
Chris Mason96b51792007-10-15 16:15:19 -04001493
Chris Masondb945352007-10-15 16:15:53 -04001494 if (key.objectid >= search_start && key.objectid > last_byte &&
Chris Masone37c9e62007-05-09 20:13:14 -04001495 start_found) {
Chris Masondb945352007-10-15 16:15:53 -04001496 if (last_byte < search_start)
1497 last_byte = search_start;
Chris Mason87ee04e2007-11-30 11:30:34 -05001498 aligned = stripe_align(root, last_byte);
1499 hole_size = key.objectid - aligned;
1500 if (key.objectid > aligned && hole_size >= num_bytes) {
1501 ins->objectid = aligned;
Chris Masone37c9e62007-05-09 20:13:14 -04001502 ins->offset = hole_size;
1503 goto check_pending;
Chris Mason0579da42007-03-07 16:15:30 -05001504 }
Chris Masonfec577f2007-02-26 10:40:21 -05001505 }
Chris Mason96b51792007-10-15 16:15:19 -04001506 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY) {
Chris Mason7bb86312007-12-11 09:25:06 -05001507 if (!start_found && btrfs_key_type(&key) ==
1508 BTRFS_BLOCK_GROUP_ITEM_KEY) {
Chris Masondb945352007-10-15 16:15:53 -04001509 last_byte = key.objectid;
Chris Mason96b51792007-10-15 16:15:19 -04001510 start_found = 1;
1511 }
Chris Masone37c9e62007-05-09 20:13:14 -04001512 goto next;
Chris Mason96b51792007-10-15 16:15:19 -04001513 }
1514
Chris Masone37c9e62007-05-09 20:13:14 -04001515
Chris Mason0579da42007-03-07 16:15:30 -05001516 start_found = 1;
Chris Masondb945352007-10-15 16:15:53 -04001517 last_byte = key.objectid + key.offset;
Chris Masonf510cfe2007-10-15 16:14:48 -04001518
Chris Mason257d0ce2007-11-07 21:08:16 -05001519 if (!full_scan && data != BTRFS_BLOCK_GROUP_MIXED &&
1520 last_byte >= block_group->key.objectid +
Chris Masonbe744172007-05-06 10:15:01 -04001521 block_group->key.offset) {
1522 btrfs_release_path(root, path);
1523 search_start = block_group->key.objectid +
Chris Masone19caa52007-10-15 16:17:44 -04001524 block_group->key.offset;
Chris Masonbe744172007-05-06 10:15:01 -04001525 goto new_group;
1526 }
Chris Mason9078a3e2007-04-26 16:46:15 -04001527next:
Chris Mason5caf2a02007-04-02 11:20:42 -04001528 path->slots[0]++;
Chris Masonde428b62007-05-18 13:28:27 -04001529 cond_resched();
Chris Masonfec577f2007-02-26 10:40:21 -05001530 }
Chris Masonfec577f2007-02-26 10:40:21 -05001531check_pending:
1532 /* we have to make sure we didn't find an extent that has already
1533 * been allocated by the map tree or the original allocation
1534 */
Chris Mason5caf2a02007-04-02 11:20:42 -04001535 btrfs_release_path(root, path);
Chris Masonfec577f2007-02-26 10:40:21 -05001536 BUG_ON(ins->objectid < search_start);
Chris Masone37c9e62007-05-09 20:13:14 -04001537
Chris Masondb945352007-10-15 16:15:53 -04001538 if (ins->objectid + num_bytes >= search_end)
Chris Masoncf675822007-09-17 11:00:51 -04001539 goto enospc;
Chris Mason257d0ce2007-11-07 21:08:16 -05001540 if (!full_scan && data != BTRFS_BLOCK_GROUP_MIXED &&
Yan5cf66422007-11-16 14:57:09 -05001541 ins->objectid + num_bytes > block_group->
Chris Masone19caa52007-10-15 16:17:44 -04001542 key.objectid + block_group->key.offset) {
1543 search_start = block_group->key.objectid +
1544 block_group->key.offset;
1545 goto new_group;
1546 }
Chris Mason1a5bc162007-10-15 16:15:26 -04001547 if (test_range_bit(&info->extent_ins, ins->objectid,
Chris Masondb945352007-10-15 16:15:53 -04001548 ins->objectid + num_bytes -1, EXTENT_LOCKED, 0)) {
1549 search_start = ins->objectid + num_bytes;
Chris Mason1a5bc162007-10-15 16:15:26 -04001550 goto new_group;
1551 }
1552 if (test_range_bit(&info->pinned_extents, ins->objectid,
Chris Masondb945352007-10-15 16:15:53 -04001553 ins->objectid + num_bytes -1, EXTENT_DIRTY, 0)) {
1554 search_start = ins->objectid + num_bytes;
Chris Mason1a5bc162007-10-15 16:15:26 -04001555 goto new_group;
Chris Masonfec577f2007-02-26 10:40:21 -05001556 }
Chris Masondb945352007-10-15 16:15:53 -04001557 if (exclude_nr > 0 && (ins->objectid + num_bytes > exclude_start &&
Chris Masonf2654de2007-06-26 12:20:46 -04001558 ins->objectid < exclude_start + exclude_nr)) {
1559 search_start = exclude_start + exclude_nr;
1560 goto new_group;
1561 }
Chris Masone37c9e62007-05-09 20:13:14 -04001562 if (!data) {
Chris Mason5276aed2007-06-11 21:33:38 -04001563 block_group = btrfs_lookup_block_group(info, ins->objectid);
Chris Mason26b80032007-08-08 20:17:12 -04001564 if (block_group)
1565 trans->block_group = block_group;
Chris Masoncd1bc462007-04-27 10:08:34 -04001566 }
Chris Masondb945352007-10-15 16:15:53 -04001567 ins->offset = num_bytes;
Chris Mason5caf2a02007-04-02 11:20:42 -04001568 btrfs_free_path(path);
Chris Masonfec577f2007-02-26 10:40:21 -05001569 return 0;
Chris Masonbe744172007-05-06 10:15:01 -04001570
1571new_group:
Chris Masondb945352007-10-15 16:15:53 -04001572 if (search_start + num_bytes >= search_end) {
Chris Masoncf675822007-09-17 11:00:51 -04001573enospc:
Chris Masonbe744172007-05-06 10:15:01 -04001574 search_start = orig_search_start;
Chris Masonfbdc7622007-05-30 10:22:12 -04001575 if (full_scan) {
1576 ret = -ENOSPC;
1577 goto error;
1578 }
Chris Mason6702ed42007-08-07 16:15:09 -04001579 if (wrapped) {
1580 if (!full_scan)
1581 total_needed -= empty_size;
Chris Masonfbdc7622007-05-30 10:22:12 -04001582 full_scan = 1;
Chris Mason1a2b2ac2007-12-04 13:18:24 -05001583 data = BTRFS_BLOCK_GROUP_MIXED;
Chris Mason6702ed42007-08-07 16:15:09 -04001584 } else
Chris Masonfbdc7622007-05-30 10:22:12 -04001585 wrapped = 1;
Chris Masonbe744172007-05-06 10:15:01 -04001586 }
Chris Mason5276aed2007-06-11 21:33:38 -04001587 block_group = btrfs_lookup_block_group(info, search_start);
Chris Masonfbdc7622007-05-30 10:22:12 -04001588 cond_resched();
Chris Mason1a2b2ac2007-12-04 13:18:24 -05001589 block_group = btrfs_find_block_group(root, block_group,
1590 search_start, data, 0);
Chris Masonbe744172007-05-06 10:15:01 -04001591 goto check_failed;
1592
Chris Mason0f70abe2007-02-28 16:46:22 -05001593error:
Chris Mason5caf2a02007-04-02 11:20:42 -04001594 btrfs_release_path(root, path);
1595 btrfs_free_path(path);
Chris Mason0f70abe2007-02-28 16:46:22 -05001596 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05001597}
Chris Masonfec577f2007-02-26 10:40:21 -05001598/*
Chris Masonfec577f2007-02-26 10:40:21 -05001599 * finds a free extent and does all the dirty work required for allocation
1600 * returns the key for the extent through ins, and a tree buffer for
1601 * the first block of the extent through buf.
1602 *
1603 * returns 0 if everything worked, non-zero otherwise.
1604 */
Chris Mason4d775672007-04-20 20:23:12 -04001605int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
Chris Mason7bb86312007-12-11 09:25:06 -05001606 struct btrfs_root *root,
1607 u64 num_bytes, u64 root_objectid, u64 ref_generation,
1608 u64 owner, u64 owner_offset,
1609 u64 empty_size, u64 hint_byte,
Chris Masonbe08c1b2007-05-03 09:06:49 -04001610 u64 search_end, struct btrfs_key *ins, int data)
Chris Masonfec577f2007-02-26 10:40:21 -05001611{
1612 int ret;
1613 int pending_ret;
Chris Masondb945352007-10-15 16:15:53 -04001614 u64 super_used, root_used;
Chris Masonfbdc7622007-05-30 10:22:12 -04001615 u64 search_start = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05001616 u64 new_hint;
Chris Mason1261ec42007-03-20 20:35:03 -04001617 struct btrfs_fs_info *info = root->fs_info;
1618 struct btrfs_root *extent_root = info->extent_root;
Chris Mason234b63a2007-03-13 10:46:10 -04001619 struct btrfs_extent_item extent_item;
Chris Mason7bb86312007-12-11 09:25:06 -05001620 struct btrfs_path *path;
Chris Mason037e6392007-03-07 11:50:24 -05001621
Chris Mason5f39d392007-10-15 16:14:19 -04001622 btrfs_set_stack_extent_refs(&extent_item, 1);
Chris Mason8f662a72008-01-02 10:01:11 -05001623
1624 new_hint = max(hint_byte, root->fs_info->alloc_start);
Chris Masonedbd8d42007-12-21 16:27:24 -05001625 if (new_hint < btrfs_super_total_bytes(&info->super_copy))
1626 hint_byte = new_hint;
Chris Mason8f662a72008-01-02 10:01:11 -05001627
Chris Masondb945352007-10-15 16:15:53 -04001628 WARN_ON(num_bytes < root->sectorsize);
1629 ret = find_free_extent(trans, root, num_bytes, empty_size,
1630 search_start, search_end, hint_byte, ins,
Chris Mason26b80032007-08-08 20:17:12 -04001631 trans->alloc_exclude_start,
1632 trans->alloc_exclude_nr, data);
Chris Masonccd467d2007-06-28 15:57:36 -04001633 BUG_ON(ret);
Chris Masonf2654de2007-06-26 12:20:46 -04001634 if (ret)
1635 return ret;
1636
Josef Bacik58176a92007-08-29 15:47:34 -04001637 /* block accounting for super block */
Chris Masondb945352007-10-15 16:15:53 -04001638 super_used = btrfs_super_bytes_used(&info->super_copy);
1639 btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes);
Chris Mason26b80032007-08-08 20:17:12 -04001640
Josef Bacik58176a92007-08-29 15:47:34 -04001641 /* block accounting for root item */
Chris Masondb945352007-10-15 16:15:53 -04001642 root_used = btrfs_root_used(&root->root_item);
1643 btrfs_set_root_used(&root->root_item, root_used + num_bytes);
Josef Bacik58176a92007-08-29 15:47:34 -04001644
Chris Masonf510cfe2007-10-15 16:14:48 -04001645 clear_extent_dirty(&root->fs_info->free_space_cache,
1646 ins->objectid, ins->objectid + ins->offset - 1,
1647 GFP_NOFS);
1648
Chris Mason26b80032007-08-08 20:17:12 -04001649 if (root == extent_root) {
Chris Mason1a5bc162007-10-15 16:15:26 -04001650 set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
1651 ins->objectid + ins->offset - 1,
1652 EXTENT_LOCKED, GFP_NOFS);
Chris Masone19caa52007-10-15 16:17:44 -04001653 WARN_ON(data == 1);
Chris Mason26b80032007-08-08 20:17:12 -04001654 goto update_block;
1655 }
1656
1657 WARN_ON(trans->alloc_exclude_nr);
1658 trans->alloc_exclude_start = ins->objectid;
1659 trans->alloc_exclude_nr = ins->offset;
Chris Masone089f052007-03-16 16:20:31 -04001660 ret = btrfs_insert_item(trans, extent_root, ins, &extent_item,
1661 sizeof(extent_item));
Chris Mason037e6392007-03-07 11:50:24 -05001662
Chris Mason26b80032007-08-08 20:17:12 -04001663 trans->alloc_exclude_start = 0;
1664 trans->alloc_exclude_nr = 0;
Chris Mason7bb86312007-12-11 09:25:06 -05001665 BUG_ON(ret);
1666
1667 path = btrfs_alloc_path();
1668 BUG_ON(!path);
1669 ret = btrfs_insert_extent_backref(trans, extent_root, path,
1670 ins->objectid, root_objectid,
1671 ref_generation, owner, owner_offset);
Chris Mason26b80032007-08-08 20:17:12 -04001672
Chris Masonccd467d2007-06-28 15:57:36 -04001673 BUG_ON(ret);
Chris Mason7bb86312007-12-11 09:25:06 -05001674 btrfs_free_path(path);
Chris Masone089f052007-03-16 16:20:31 -04001675 finish_current_insert(trans, extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -04001676 pending_ret = del_pending_extents(trans, extent_root);
Chris Masonf510cfe2007-10-15 16:14:48 -04001677
Chris Masone37c9e62007-05-09 20:13:14 -04001678 if (ret) {
Chris Mason037e6392007-03-07 11:50:24 -05001679 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -04001680 }
1681 if (pending_ret) {
Chris Mason037e6392007-03-07 11:50:24 -05001682 return pending_ret;
Chris Masone37c9e62007-05-09 20:13:14 -04001683 }
Chris Mason26b80032007-08-08 20:17:12 -04001684
1685update_block:
Chris Mason1e2677e2007-05-29 16:52:18 -04001686 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0,
1687 data);
Chris Masonfabb5682007-06-07 22:13:21 -04001688 BUG_ON(ret);
Chris Mason037e6392007-03-07 11:50:24 -05001689 return 0;
Chris Masonfec577f2007-02-26 10:40:21 -05001690}
1691
1692/*
1693 * helper function to allocate a block for a given tree
1694 * returns the tree buffer or NULL.
1695 */
Chris Mason5f39d392007-10-15 16:14:19 -04001696struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Chris Masondb945352007-10-15 16:15:53 -04001697 struct btrfs_root *root,
Chris Mason7bb86312007-12-11 09:25:06 -05001698 u32 blocksize,
1699 u64 root_objectid, u64 hint,
1700 u64 empty_size)
1701{
1702 u64 ref_generation;
1703
1704 if (root->ref_cows)
1705 ref_generation = trans->transid;
1706 else
1707 ref_generation = 0;
1708
1709
1710 return __btrfs_alloc_free_block(trans, root, blocksize, root_objectid,
1711 ref_generation, 0, 0, hint, empty_size);
1712}
1713
1714/*
1715 * helper function to allocate a block for a given tree
1716 * returns the tree buffer or NULL.
1717 */
1718struct extent_buffer *__btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
1719 struct btrfs_root *root,
1720 u32 blocksize,
1721 u64 root_objectid,
1722 u64 ref_generation,
1723 u64 first_objectid,
1724 int level,
1725 u64 hint,
Chris Mason5f39d392007-10-15 16:14:19 -04001726 u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05001727{
Chris Masone2fa7222007-03-12 16:22:34 -04001728 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -05001729 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04001730 struct extent_buffer *buf;
Chris Masonfec577f2007-02-26 10:40:21 -05001731
Chris Mason7bb86312007-12-11 09:25:06 -05001732 ret = btrfs_alloc_extent(trans, root, blocksize,
1733 root_objectid, ref_generation,
Chris Masonf6dbff52007-12-13 11:13:32 -05001734 level, first_objectid, empty_size, hint,
Chris Masondb945352007-10-15 16:15:53 -04001735 (u64)-1, &ins, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05001736 if (ret) {
Chris Mason54aa1f42007-06-22 14:16:25 -04001737 BUG_ON(ret > 0);
1738 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05001739 }
Chris Masondb945352007-10-15 16:15:53 -04001740 buf = btrfs_find_create_tree_block(root, ins.objectid, blocksize);
Chris Mason54aa1f42007-06-22 14:16:25 -04001741 if (!buf) {
Chris Mason7bb86312007-12-11 09:25:06 -05001742 btrfs_free_extent(trans, root, ins.objectid, blocksize,
1743 root->root_key.objectid, ref_generation,
1744 0, 0, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001745 return ERR_PTR(-ENOMEM);
1746 }
Chris Mason5f39d392007-10-15 16:14:19 -04001747 btrfs_set_buffer_uptodate(buf);
1748 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
1749 buf->start + buf->len - 1, GFP_NOFS);
Chris Mason19c00dd2007-10-15 16:19:22 -04001750 set_extent_bits(&BTRFS_I(root->fs_info->btree_inode)->extent_tree,
1751 buf->start, buf->start + buf->len - 1,
1752 EXTENT_CSUM, GFP_NOFS);
1753 buf->flags |= EXTENT_CSUM;
Chris Mason6b800532007-10-15 16:17:34 -04001754 btrfs_set_buffer_defrag(buf);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001755 trans->blocks_used++;
Chris Masonfec577f2007-02-26 10:40:21 -05001756 return buf;
1757}
Chris Masona28ec192007-03-06 20:08:01 -05001758
Chris Mason98ed5172008-01-03 10:01:48 -05001759static int noinline drop_leaf_ref(struct btrfs_trans_handle *trans,
1760 struct btrfs_root *root,
1761 struct extent_buffer *leaf)
Chris Mason6407bf62007-03-27 06:33:00 -04001762{
Chris Mason7bb86312007-12-11 09:25:06 -05001763 u64 leaf_owner;
1764 u64 leaf_generation;
Chris Mason5f39d392007-10-15 16:14:19 -04001765 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -04001766 struct btrfs_file_extent_item *fi;
1767 int i;
1768 int nritems;
1769 int ret;
1770
Chris Mason5f39d392007-10-15 16:14:19 -04001771 BUG_ON(!btrfs_is_leaf(leaf));
1772 nritems = btrfs_header_nritems(leaf);
Chris Mason7bb86312007-12-11 09:25:06 -05001773 leaf_owner = btrfs_header_owner(leaf);
1774 leaf_generation = btrfs_header_generation(leaf);
1775
Chris Mason6407bf62007-03-27 06:33:00 -04001776 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04001777 u64 disk_bytenr;
Chris Mason5f39d392007-10-15 16:14:19 -04001778
1779 btrfs_item_key_to_cpu(leaf, &key, i);
1780 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason6407bf62007-03-27 06:33:00 -04001781 continue;
1782 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001783 if (btrfs_file_extent_type(leaf, fi) ==
1784 BTRFS_FILE_EXTENT_INLINE)
Chris Mason236454df2007-04-19 13:37:44 -04001785 continue;
Chris Mason6407bf62007-03-27 06:33:00 -04001786 /*
1787 * FIXME make sure to insert a trans record that
1788 * repeats the snapshot del on crash
1789 */
Chris Masondb945352007-10-15 16:15:53 -04001790 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
1791 if (disk_bytenr == 0)
Chris Mason3a686372007-05-24 13:35:57 -04001792 continue;
Chris Masondb945352007-10-15 16:15:53 -04001793 ret = btrfs_free_extent(trans, root, disk_bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -05001794 btrfs_file_extent_disk_num_bytes(leaf, fi),
1795 leaf_owner, leaf_generation,
1796 key.objectid, key.offset, 0);
Chris Mason6407bf62007-03-27 06:33:00 -04001797 BUG_ON(ret);
1798 }
1799 return 0;
1800}
1801
Chris Mason98ed5172008-01-03 10:01:48 -05001802static void noinline reada_walk_down(struct btrfs_root *root,
1803 struct extent_buffer *node)
Chris Masone0115992007-06-19 16:23:05 -04001804{
1805 int i;
1806 u32 nritems;
Chris Masondb945352007-10-15 16:15:53 -04001807 u64 bytenr;
Chris Masone0115992007-06-19 16:23:05 -04001808 int ret;
1809 u32 refs;
Chris Masondb945352007-10-15 16:15:53 -04001810 int level;
1811 u32 blocksize;
Chris Masone0115992007-06-19 16:23:05 -04001812
Chris Mason5f39d392007-10-15 16:14:19 -04001813 nritems = btrfs_header_nritems(node);
Chris Masondb945352007-10-15 16:15:53 -04001814 level = btrfs_header_level(node);
Chris Masone0115992007-06-19 16:23:05 -04001815 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04001816 bytenr = btrfs_node_blockptr(node, i);
1817 blocksize = btrfs_level_size(root, level - 1);
1818 ret = lookup_extent_ref(NULL, root, bytenr, blocksize, &refs);
Chris Masone0115992007-06-19 16:23:05 -04001819 BUG_ON(ret);
1820 if (refs != 1)
1821 continue;
Chris Mason409eb952007-08-08 20:17:12 -04001822 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masondb945352007-10-15 16:15:53 -04001823 ret = readahead_tree_block(root, bytenr, blocksize);
Chris Mason409eb952007-08-08 20:17:12 -04001824 cond_resched();
1825 mutex_lock(&root->fs_info->fs_mutex);
Chris Masone0115992007-06-19 16:23:05 -04001826 if (ret)
1827 break;
1828 }
1829}
1830
Chris Mason9aca1d52007-03-13 11:09:37 -04001831/*
1832 * helper function for drop_snapshot, this walks down the tree dropping ref
1833 * counts as it goes.
1834 */
Chris Mason98ed5172008-01-03 10:01:48 -05001835static int noinline walk_down_tree(struct btrfs_trans_handle *trans,
1836 struct btrfs_root *root,
1837 struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05001838{
Chris Mason7bb86312007-12-11 09:25:06 -05001839 u64 root_owner;
1840 u64 root_gen;
1841 u64 bytenr;
Chris Mason5f39d392007-10-15 16:14:19 -04001842 struct extent_buffer *next;
1843 struct extent_buffer *cur;
Chris Mason7bb86312007-12-11 09:25:06 -05001844 struct extent_buffer *parent;
Chris Masondb945352007-10-15 16:15:53 -04001845 u32 blocksize;
Chris Mason20524f02007-03-10 06:35:47 -05001846 int ret;
1847 u32 refs;
1848
Chris Mason5caf2a02007-04-02 11:20:42 -04001849 WARN_ON(*level < 0);
1850 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason5f39d392007-10-15 16:14:19 -04001851 ret = lookup_extent_ref(trans, root,
Chris Masondb945352007-10-15 16:15:53 -04001852 path->nodes[*level]->start,
1853 path->nodes[*level]->len, &refs);
Chris Mason20524f02007-03-10 06:35:47 -05001854 BUG_ON(ret);
1855 if (refs > 1)
1856 goto out;
Chris Masone0115992007-06-19 16:23:05 -04001857
Chris Mason9aca1d52007-03-13 11:09:37 -04001858 /*
1859 * walk down to the last node level and free all the leaves
1860 */
Chris Mason6407bf62007-03-27 06:33:00 -04001861 while(*level >= 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001862 WARN_ON(*level < 0);
1863 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason20524f02007-03-10 06:35:47 -05001864 cur = path->nodes[*level];
Chris Masone0115992007-06-19 16:23:05 -04001865
1866 if (*level > 0 && path->slots[*level] == 0)
Chris Mason5f39d392007-10-15 16:14:19 -04001867 reada_walk_down(root, cur);
Chris Masone0115992007-06-19 16:23:05 -04001868
Chris Mason5f39d392007-10-15 16:14:19 -04001869 if (btrfs_header_level(cur) != *level)
Chris Mason2c90e5d2007-04-02 10:50:19 -04001870 WARN_ON(1);
Chris Masone0115992007-06-19 16:23:05 -04001871
Chris Mason7518a232007-03-12 12:01:18 -04001872 if (path->slots[*level] >=
Chris Mason5f39d392007-10-15 16:14:19 -04001873 btrfs_header_nritems(cur))
Chris Mason20524f02007-03-10 06:35:47 -05001874 break;
Chris Mason6407bf62007-03-27 06:33:00 -04001875 if (*level == 0) {
1876 ret = drop_leaf_ref(trans, root, cur);
1877 BUG_ON(ret);
1878 break;
1879 }
Chris Masondb945352007-10-15 16:15:53 -04001880 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
1881 blocksize = btrfs_level_size(root, *level - 1);
1882 ret = lookup_extent_ref(trans, root, bytenr, blocksize, &refs);
Chris Mason6407bf62007-03-27 06:33:00 -04001883 BUG_ON(ret);
1884 if (refs != 1) {
Chris Mason7bb86312007-12-11 09:25:06 -05001885 parent = path->nodes[*level];
1886 root_owner = btrfs_header_owner(parent);
1887 root_gen = btrfs_header_generation(parent);
Chris Mason20524f02007-03-10 06:35:47 -05001888 path->slots[*level]++;
Chris Masondb945352007-10-15 16:15:53 -04001889 ret = btrfs_free_extent(trans, root, bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -05001890 blocksize, root_owner,
1891 root_gen, 0, 0, 1);
Chris Mason20524f02007-03-10 06:35:47 -05001892 BUG_ON(ret);
1893 continue;
1894 }
Chris Masondb945352007-10-15 16:15:53 -04001895 next = btrfs_find_tree_block(root, bytenr, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -04001896 if (!next || !btrfs_buffer_uptodate(next)) {
1897 free_extent_buffer(next);
Chris Masone9d0b132007-08-10 14:06:19 -04001898 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masondb945352007-10-15 16:15:53 -04001899 next = read_tree_block(root, bytenr, blocksize);
Chris Masone9d0b132007-08-10 14:06:19 -04001900 mutex_lock(&root->fs_info->fs_mutex);
1901
1902 /* we dropped the lock, check one more time */
Chris Masondb945352007-10-15 16:15:53 -04001903 ret = lookup_extent_ref(trans, root, bytenr,
1904 blocksize, &refs);
Chris Masone9d0b132007-08-10 14:06:19 -04001905 BUG_ON(ret);
1906 if (refs != 1) {
Chris Mason7bb86312007-12-11 09:25:06 -05001907 parent = path->nodes[*level];
1908 root_owner = btrfs_header_owner(parent);
1909 root_gen = btrfs_header_generation(parent);
1910
Chris Masone9d0b132007-08-10 14:06:19 -04001911 path->slots[*level]++;
Chris Mason5f39d392007-10-15 16:14:19 -04001912 free_extent_buffer(next);
Chris Mason7bb86312007-12-11 09:25:06 -05001913 ret = btrfs_free_extent(trans, root, bytenr,
1914 blocksize,
1915 root_owner,
1916 root_gen, 0, 0, 1);
Chris Masone9d0b132007-08-10 14:06:19 -04001917 BUG_ON(ret);
1918 continue;
1919 }
1920 }
Chris Mason5caf2a02007-04-02 11:20:42 -04001921 WARN_ON(*level <= 0);
Chris Mason83e15a22007-03-12 09:03:27 -04001922 if (path->nodes[*level-1])
Chris Mason5f39d392007-10-15 16:14:19 -04001923 free_extent_buffer(path->nodes[*level-1]);
Chris Mason20524f02007-03-10 06:35:47 -05001924 path->nodes[*level-1] = next;
Chris Mason5f39d392007-10-15 16:14:19 -04001925 *level = btrfs_header_level(next);
Chris Mason20524f02007-03-10 06:35:47 -05001926 path->slots[*level] = 0;
1927 }
1928out:
Chris Mason5caf2a02007-04-02 11:20:42 -04001929 WARN_ON(*level < 0);
1930 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason7bb86312007-12-11 09:25:06 -05001931
1932 if (path->nodes[*level] == root->node) {
1933 root_owner = root->root_key.objectid;
1934 parent = path->nodes[*level];
1935 } else {
1936 parent = path->nodes[*level + 1];
1937 root_owner = btrfs_header_owner(parent);
1938 }
1939
1940 root_gen = btrfs_header_generation(parent);
Chris Masondb945352007-10-15 16:15:53 -04001941 ret = btrfs_free_extent(trans, root, path->nodes[*level]->start,
Chris Mason7bb86312007-12-11 09:25:06 -05001942 path->nodes[*level]->len,
1943 root_owner, root_gen, 0, 0, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001944 free_extent_buffer(path->nodes[*level]);
Chris Mason20524f02007-03-10 06:35:47 -05001945 path->nodes[*level] = NULL;
1946 *level += 1;
1947 BUG_ON(ret);
1948 return 0;
1949}
1950
Chris Mason9aca1d52007-03-13 11:09:37 -04001951/*
1952 * helper for dropping snapshots. This walks back up the tree in the path
1953 * to find the first node higher up where we haven't yet gone through
1954 * all the slots
1955 */
Chris Mason98ed5172008-01-03 10:01:48 -05001956static int noinline walk_up_tree(struct btrfs_trans_handle *trans,
1957 struct btrfs_root *root,
1958 struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05001959{
Chris Mason7bb86312007-12-11 09:25:06 -05001960 u64 root_owner;
1961 u64 root_gen;
1962 struct btrfs_root_item *root_item = &root->root_item;
Chris Mason20524f02007-03-10 06:35:47 -05001963 int i;
1964 int slot;
1965 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04001966
Chris Mason234b63a2007-03-13 10:46:10 -04001967 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Mason20524f02007-03-10 06:35:47 -05001968 slot = path->slots[i];
Chris Mason5f39d392007-10-15 16:14:19 -04001969 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
1970 struct extent_buffer *node;
1971 struct btrfs_disk_key disk_key;
1972 node = path->nodes[i];
Chris Mason20524f02007-03-10 06:35:47 -05001973 path->slots[i]++;
1974 *level = i;
Chris Mason9f3a7422007-08-07 15:52:19 -04001975 WARN_ON(*level == 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001976 btrfs_node_key(node, &disk_key, path->slots[i]);
Chris Mason9f3a7422007-08-07 15:52:19 -04001977 memcpy(&root_item->drop_progress,
Chris Mason5f39d392007-10-15 16:14:19 -04001978 &disk_key, sizeof(disk_key));
Chris Mason9f3a7422007-08-07 15:52:19 -04001979 root_item->drop_level = i;
Chris Mason20524f02007-03-10 06:35:47 -05001980 return 0;
1981 } else {
Chris Mason7bb86312007-12-11 09:25:06 -05001982 if (path->nodes[*level] == root->node) {
1983 root_owner = root->root_key.objectid;
1984 root_gen =
1985 btrfs_header_generation(path->nodes[*level]);
1986 } else {
1987 struct extent_buffer *node;
1988 node = path->nodes[*level + 1];
1989 root_owner = btrfs_header_owner(node);
1990 root_gen = btrfs_header_generation(node);
1991 }
Chris Masone089f052007-03-16 16:20:31 -04001992 ret = btrfs_free_extent(trans, root,
Chris Masondb945352007-10-15 16:15:53 -04001993 path->nodes[*level]->start,
Chris Mason7bb86312007-12-11 09:25:06 -05001994 path->nodes[*level]->len,
1995 root_owner, root_gen, 0, 0, 1);
Chris Mason6407bf62007-03-27 06:33:00 -04001996 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04001997 free_extent_buffer(path->nodes[*level]);
Chris Mason83e15a22007-03-12 09:03:27 -04001998 path->nodes[*level] = NULL;
Chris Mason20524f02007-03-10 06:35:47 -05001999 *level = i + 1;
Chris Mason20524f02007-03-10 06:35:47 -05002000 }
2001 }
2002 return 1;
2003}
2004
Chris Mason9aca1d52007-03-13 11:09:37 -04002005/*
2006 * drop the reference count on the tree rooted at 'snap'. This traverses
2007 * the tree freeing any blocks that have a ref count of zero after being
2008 * decremented.
2009 */
Chris Masone089f052007-03-16 16:20:31 -04002010int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason9f3a7422007-08-07 15:52:19 -04002011 *root)
Chris Mason20524f02007-03-10 06:35:47 -05002012{
Chris Mason3768f362007-03-13 16:47:54 -04002013 int ret = 0;
Chris Mason9aca1d52007-03-13 11:09:37 -04002014 int wret;
Chris Mason20524f02007-03-10 06:35:47 -05002015 int level;
Chris Mason5caf2a02007-04-02 11:20:42 -04002016 struct btrfs_path *path;
Chris Mason20524f02007-03-10 06:35:47 -05002017 int i;
2018 int orig_level;
Chris Mason9f3a7422007-08-07 15:52:19 -04002019 struct btrfs_root_item *root_item = &root->root_item;
Chris Mason20524f02007-03-10 06:35:47 -05002020
Chris Mason5caf2a02007-04-02 11:20:42 -04002021 path = btrfs_alloc_path();
2022 BUG_ON(!path);
Chris Mason20524f02007-03-10 06:35:47 -05002023
Chris Mason5f39d392007-10-15 16:14:19 -04002024 level = btrfs_header_level(root->node);
Chris Mason20524f02007-03-10 06:35:47 -05002025 orig_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04002026 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
2027 path->nodes[level] = root->node;
Chris Masonf510cfe2007-10-15 16:14:48 -04002028 extent_buffer_get(root->node);
Chris Mason9f3a7422007-08-07 15:52:19 -04002029 path->slots[level] = 0;
2030 } else {
2031 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04002032 struct btrfs_disk_key found_key;
2033 struct extent_buffer *node;
Chris Mason6702ed42007-08-07 16:15:09 -04002034
Chris Mason9f3a7422007-08-07 15:52:19 -04002035 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Chris Mason6702ed42007-08-07 16:15:09 -04002036 level = root_item->drop_level;
2037 path->lowest_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04002038 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason6702ed42007-08-07 16:15:09 -04002039 if (wret < 0) {
Chris Mason9f3a7422007-08-07 15:52:19 -04002040 ret = wret;
2041 goto out;
2042 }
Chris Mason5f39d392007-10-15 16:14:19 -04002043 node = path->nodes[level];
2044 btrfs_node_key(node, &found_key, path->slots[level]);
2045 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
2046 sizeof(found_key)));
Chris Mason9f3a7422007-08-07 15:52:19 -04002047 }
Chris Mason20524f02007-03-10 06:35:47 -05002048 while(1) {
Chris Mason5caf2a02007-04-02 11:20:42 -04002049 wret = walk_down_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04002050 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05002051 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04002052 if (wret < 0)
2053 ret = wret;
2054
Chris Mason5caf2a02007-04-02 11:20:42 -04002055 wret = walk_up_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;
Chris Mason409eb952007-08-08 20:17:12 -04002060 ret = -EAGAIN;
Chris Mason409eb952007-08-08 20:17:12 -04002061 break;
Chris Mason20524f02007-03-10 06:35:47 -05002062 }
Chris Mason83e15a22007-03-12 09:03:27 -04002063 for (i = 0; i <= orig_level; i++) {
Chris Mason5caf2a02007-04-02 11:20:42 -04002064 if (path->nodes[i]) {
Chris Mason5f39d392007-10-15 16:14:19 -04002065 free_extent_buffer(path->nodes[i]);
Chris Mason0f827312007-10-15 16:18:56 -04002066 path->nodes[i] = NULL;
Chris Mason83e15a22007-03-12 09:03:27 -04002067 }
Chris Mason20524f02007-03-10 06:35:47 -05002068 }
Chris Mason9f3a7422007-08-07 15:52:19 -04002069out:
Chris Mason5caf2a02007-04-02 11:20:42 -04002070 btrfs_free_path(path);
Chris Mason9aca1d52007-03-13 11:09:37 -04002071 return ret;
Chris Mason20524f02007-03-10 06:35:47 -05002072}
Chris Mason9078a3e2007-04-26 16:46:15 -04002073
Chris Masonbe744172007-05-06 10:15:01 -04002074int btrfs_free_block_groups(struct btrfs_fs_info *info)
2075{
Chris Masonf510cfe2007-10-15 16:14:48 -04002076 u64 start;
2077 u64 end;
Yanb97f9202007-11-01 11:28:41 -04002078 u64 ptr;
Chris Mason96b51792007-10-15 16:15:19 -04002079 int ret;
Chris Mason96b51792007-10-15 16:15:19 -04002080 while(1) {
2081 ret = find_first_extent_bit(&info->block_group_cache, 0,
2082 &start, &end, (unsigned int)-1);
2083 if (ret)
2084 break;
Yanb97f9202007-11-01 11:28:41 -04002085 ret = get_state_private(&info->block_group_cache, start, &ptr);
2086 if (!ret)
2087 kfree((void *)(unsigned long)ptr);
Chris Mason96b51792007-10-15 16:15:19 -04002088 clear_extent_bits(&info->block_group_cache, start,
2089 end, (unsigned int)-1, GFP_NOFS);
2090 }
Chris Masone37c9e62007-05-09 20:13:14 -04002091 while(1) {
Chris Masonf510cfe2007-10-15 16:14:48 -04002092 ret = find_first_extent_bit(&info->free_space_cache, 0,
2093 &start, &end, EXTENT_DIRTY);
2094 if (ret)
Chris Masone37c9e62007-05-09 20:13:14 -04002095 break;
Chris Masonf510cfe2007-10-15 16:14:48 -04002096 clear_extent_dirty(&info->free_space_cache, start,
2097 end, GFP_NOFS);
Chris Masone37c9e62007-05-09 20:13:14 -04002098 }
Chris Masonbe744172007-05-06 10:15:01 -04002099 return 0;
2100}
2101
Chris Mason98ed5172008-01-03 10:01:48 -05002102static int noinline relocate_inode_pages(struct inode *inode, u64 start,
2103 u64 len)
Chris Masonedbd8d42007-12-21 16:27:24 -05002104{
2105 u64 page_start;
2106 u64 page_end;
2107 u64 delalloc_start;
2108 u64 existing_delalloc;
2109 unsigned long last_index;
Chris Masonedbd8d42007-12-21 16:27:24 -05002110 unsigned long i;
2111 struct page *page;
2112 struct btrfs_root *root = BTRFS_I(inode)->root;
2113 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason4313b392008-01-03 09:08:48 -05002114 struct file_ra_state *ra;
2115
2116 ra = kzalloc(sizeof(*ra), GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05002117
2118 mutex_lock(&inode->i_mutex);
Chris Mason4313b392008-01-03 09:08:48 -05002119 i = start >> PAGE_CACHE_SHIFT;
Chris Masonedbd8d42007-12-21 16:27:24 -05002120 last_index = (start + len - 1) >> PAGE_CACHE_SHIFT;
2121
Chris Mason4313b392008-01-03 09:08:48 -05002122 file_ra_state_init(ra, inode->i_mapping);
2123 btrfs_force_ra(inode->i_mapping, ra, NULL, i, last_index);
2124 kfree(ra);
Chris Masonedbd8d42007-12-21 16:27:24 -05002125
Chris Mason4313b392008-01-03 09:08:48 -05002126 for (; i <= last_index; i++) {
Chris Masonedbd8d42007-12-21 16:27:24 -05002127 page = grab_cache_page(inode->i_mapping, i);
2128 if (!page)
2129 goto out_unlock;
2130 if (!PageUptodate(page)) {
2131 btrfs_readpage(NULL, page);
2132 lock_page(page);
2133 if (!PageUptodate(page)) {
2134 unlock_page(page);
2135 page_cache_release(page);
2136 goto out_unlock;
2137 }
2138 }
2139 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
2140 page_end = page_start + PAGE_CACHE_SIZE - 1;
2141
2142 lock_extent(em_tree, page_start, page_end, GFP_NOFS);
2143
2144 delalloc_start = page_start;
2145 existing_delalloc =
2146 count_range_bits(&BTRFS_I(inode)->extent_tree,
2147 &delalloc_start, page_end,
2148 PAGE_CACHE_SIZE, EXTENT_DELALLOC);
2149
2150 set_extent_delalloc(em_tree, page_start,
2151 page_end, GFP_NOFS);
2152
2153 spin_lock(&root->fs_info->delalloc_lock);
2154 root->fs_info->delalloc_bytes += PAGE_CACHE_SIZE -
2155 existing_delalloc;
2156 spin_unlock(&root->fs_info->delalloc_lock);
2157
2158 unlock_extent(em_tree, page_start, page_end, GFP_NOFS);
2159 set_page_dirty(page);
2160 unlock_page(page);
2161 page_cache_release(page);
2162 }
2163
2164out_unlock:
2165 mutex_unlock(&inode->i_mutex);
2166 return 0;
2167}
2168
Chris Mason4313b392008-01-03 09:08:48 -05002169/*
2170 * note, this releases the path
2171 */
Chris Mason98ed5172008-01-03 10:01:48 -05002172static int noinline relocate_one_reference(struct btrfs_root *extent_root,
Chris Masonedbd8d42007-12-21 16:27:24 -05002173 struct btrfs_path *path,
Chris Mason4313b392008-01-03 09:08:48 -05002174 struct btrfs_key *extent_key)
Chris Masonedbd8d42007-12-21 16:27:24 -05002175{
2176 struct inode *inode;
2177 struct btrfs_root *found_root;
Chris Mason4313b392008-01-03 09:08:48 -05002178 struct btrfs_key *root_location;
2179 struct btrfs_extent_ref *ref;
2180 u64 ref_root;
2181 u64 ref_gen;
2182 u64 ref_objectid;
2183 u64 ref_offset;
Chris Masonedbd8d42007-12-21 16:27:24 -05002184 int ret;
2185
Chris Mason4313b392008-01-03 09:08:48 -05002186 ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
2187 struct btrfs_extent_ref);
2188 ref_root = btrfs_ref_root(path->nodes[0], ref);
2189 ref_gen = btrfs_ref_generation(path->nodes[0], ref);
2190 ref_objectid = btrfs_ref_objectid(path->nodes[0], ref);
2191 ref_offset = btrfs_ref_offset(path->nodes[0], ref);
2192 btrfs_release_path(extent_root, path);
2193
2194 root_location = kmalloc(sizeof(*root_location), GFP_NOFS);
2195 root_location->objectid = ref_root;
Chris Masonedbd8d42007-12-21 16:27:24 -05002196 if (ref_gen == 0)
Chris Mason4313b392008-01-03 09:08:48 -05002197 root_location->offset = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05002198 else
Chris Mason4313b392008-01-03 09:08:48 -05002199 root_location->offset = (u64)-1;
2200 root_location->type = BTRFS_ROOT_ITEM_KEY;
Chris Masonedbd8d42007-12-21 16:27:24 -05002201
2202 found_root = btrfs_read_fs_root_no_name(extent_root->fs_info,
Chris Mason4313b392008-01-03 09:08:48 -05002203 root_location);
Chris Masonedbd8d42007-12-21 16:27:24 -05002204 BUG_ON(!found_root);
Chris Mason4313b392008-01-03 09:08:48 -05002205 kfree(root_location);
Chris Masonedbd8d42007-12-21 16:27:24 -05002206
2207 if (ref_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
2208 mutex_unlock(&extent_root->fs_info->fs_mutex);
2209 inode = btrfs_iget_locked(extent_root->fs_info->sb,
2210 ref_objectid, found_root);
2211 if (inode->i_state & I_NEW) {
2212 /* the inode and parent dir are two different roots */
2213 BTRFS_I(inode)->root = found_root;
2214 BTRFS_I(inode)->location.objectid = ref_objectid;
2215 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
2216 BTRFS_I(inode)->location.offset = 0;
2217 btrfs_read_locked_inode(inode);
2218 unlock_new_inode(inode);
2219
2220 }
2221 /* this can happen if the reference is not against
2222 * the latest version of the tree root
2223 */
2224 if (is_bad_inode(inode)) {
2225 mutex_lock(&extent_root->fs_info->fs_mutex);
2226 goto out;
2227 }
2228 relocate_inode_pages(inode, ref_offset, extent_key->offset);
2229 /* FIXME, data=ordered will help get rid of this */
2230 filemap_fdatawrite(inode->i_mapping);
2231 iput(inode);
2232 mutex_lock(&extent_root->fs_info->fs_mutex);
2233 } else {
2234 struct btrfs_trans_handle *trans;
2235 struct btrfs_key found_key;
2236 struct extent_buffer *eb;
2237 int level;
2238 int i;
2239
2240 trans = btrfs_start_transaction(found_root, 1);
2241 eb = read_tree_block(found_root, extent_key->objectid,
2242 extent_key->offset);
2243 level = btrfs_header_level(eb);
2244
2245 if (level == 0)
2246 btrfs_item_key_to_cpu(eb, &found_key, 0);
2247 else
2248 btrfs_node_key_to_cpu(eb, &found_key, 0);
2249
2250 free_extent_buffer(eb);
2251
2252 path->lowest_level = level;
Chris Mason8f662a72008-01-02 10:01:11 -05002253 path->reada = 2;
Chris Masonedbd8d42007-12-21 16:27:24 -05002254 ret = btrfs_search_slot(trans, found_root, &found_key, path,
2255 0, 1);
2256 path->lowest_level = 0;
2257 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2258 if (!path->nodes[i])
2259 break;
2260 free_extent_buffer(path->nodes[i]);
2261 path->nodes[i] = NULL;
2262 }
2263 btrfs_release_path(found_root, path);
2264 btrfs_end_transaction(trans, found_root);
2265 }
2266
2267out:
2268 return 0;
2269}
2270
Chris Mason98ed5172008-01-03 10:01:48 -05002271static int noinline relocate_one_extent(struct btrfs_root *extent_root,
2272 struct btrfs_path *path,
2273 struct btrfs_key *extent_key)
Chris Masonedbd8d42007-12-21 16:27:24 -05002274{
2275 struct btrfs_key key;
2276 struct btrfs_key found_key;
Chris Masonedbd8d42007-12-21 16:27:24 -05002277 struct extent_buffer *leaf;
Chris Masonedbd8d42007-12-21 16:27:24 -05002278 u32 nritems;
2279 u32 item_size;
2280 int ret = 0;
2281
2282 key.objectid = extent_key->objectid;
2283 key.type = BTRFS_EXTENT_REF_KEY;
2284 key.offset = 0;
2285
2286 while(1) {
2287 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
2288
2289 BUG_ON(ret == 0);
2290
2291 if (ret < 0)
2292 goto out;
2293
2294 ret = 0;
2295 leaf = path->nodes[0];
2296 nritems = btrfs_header_nritems(leaf);
2297 if (path->slots[0] == nritems)
2298 goto out;
2299
2300 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2301 if (found_key.objectid != extent_key->objectid)
2302 break;
2303
2304 if (found_key.type != BTRFS_EXTENT_REF_KEY)
2305 break;
2306
2307 key.offset = found_key.offset + 1;
2308 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2309
Chris Mason4313b392008-01-03 09:08:48 -05002310 ret = relocate_one_reference(extent_root, path, extent_key);
Chris Masonedbd8d42007-12-21 16:27:24 -05002311 if (ret)
2312 goto out;
2313 }
2314 ret = 0;
2315out:
2316 btrfs_release_path(extent_root, path);
2317 return ret;
2318}
2319
2320static int find_overlapping_extent(struct btrfs_root *root,
2321 struct btrfs_path *path, u64 new_size)
2322{
2323 struct btrfs_key found_key;
2324 struct extent_buffer *leaf;
2325 int ret;
2326
2327 while(1) {
2328 if (path->slots[0] == 0) {
2329 ret = btrfs_prev_leaf(root, path);
2330 if (ret == 1) {
2331 return 1;
2332 }
2333 if (ret < 0)
2334 return ret;
2335 } else {
2336 path->slots[0]--;
2337 }
2338 leaf = path->nodes[0];
2339 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2340 if (found_key.type == BTRFS_EXTENT_ITEM_KEY) {
2341 if (found_key.objectid + found_key.offset > new_size)
2342 return 0;
2343 else
2344 return 1;
2345 }
2346 }
2347 return 1;
2348}
2349
2350int btrfs_shrink_extent_tree(struct btrfs_root *root, u64 new_size)
2351{
2352 struct btrfs_trans_handle *trans;
2353 struct btrfs_root *tree_root = root->fs_info->tree_root;
2354 struct btrfs_path *path;
2355 u64 cur_byte;
2356 u64 total_found;
Chris Masonedbd8d42007-12-21 16:27:24 -05002357 struct btrfs_fs_info *info = root->fs_info;
2358 struct extent_map_tree *block_group_cache;
2359 struct btrfs_key key;
2360 struct btrfs_key found_key = { 0, 0, 0 };
2361 struct extent_buffer *leaf;
2362 u32 nritems;
2363 int ret;
2364 int slot;
2365
2366 btrfs_set_super_total_bytes(&info->super_copy, new_size);
2367 block_group_cache = &info->block_group_cache;
2368 path = btrfs_alloc_path();
2369 root = root->fs_info->extent_root;
Chris Mason8f662a72008-01-02 10:01:11 -05002370 path->reada = 2;
Chris Masonedbd8d42007-12-21 16:27:24 -05002371
2372again:
2373 total_found = 0;
2374 key.objectid = new_size;
2375 cur_byte = key.objectid;
2376 key.offset = 0;
2377 key.type = 0;
2378 while(1) {
Chris Mason4313b392008-01-03 09:08:48 -05002379
Chris Masonedbd8d42007-12-21 16:27:24 -05002380 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2381 if (ret < 0)
2382 goto out;
2383next:
2384 leaf = path->nodes[0];
2385 if (key.objectid == new_size - 1) {
2386 ret = find_overlapping_extent(root, path, new_size);
2387 if (ret != 0) {
2388 btrfs_release_path(root, path);
2389 ret = btrfs_search_slot(NULL, root, &key,
2390 path, 0, 0);
2391 if (ret < 0)
2392 goto out;
2393 }
2394 }
2395 nritems = btrfs_header_nritems(leaf);
2396 ret = 0;
2397 slot = path->slots[0];
2398 if (slot < nritems)
2399 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2400 if (slot == nritems ||
2401 btrfs_key_type(&found_key) != BTRFS_EXTENT_ITEM_KEY) {
2402 path->slots[0]++;
2403 if (path->slots[0] >= nritems) {
2404 ret = btrfs_next_leaf(root, path);
2405 if (ret < 0)
2406 goto out;
2407 if (ret == 1) {
2408 ret = 0;
2409 break;
2410 }
2411 }
2412 goto next;
2413 }
2414 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2415 if (found_key.objectid + found_key.offset <= cur_byte)
2416 continue;
2417 total_found++;
2418 cur_byte = found_key.objectid + found_key.offset;
2419 key.objectid = cur_byte;
2420 btrfs_release_path(root, path);
2421 ret = relocate_one_extent(root, path, &found_key);
2422 }
2423
2424 btrfs_release_path(root, path);
2425
2426 if (total_found > 0) {
2427 trans = btrfs_start_transaction(tree_root, 1);
2428 btrfs_commit_transaction(trans, tree_root);
2429
2430 mutex_unlock(&root->fs_info->fs_mutex);
2431 btrfs_clean_old_snapshots(tree_root);
2432 mutex_lock(&root->fs_info->fs_mutex);
2433
2434 trans = btrfs_start_transaction(tree_root, 1);
2435 btrfs_commit_transaction(trans, tree_root);
2436 goto again;
2437 }
2438
2439 trans = btrfs_start_transaction(root, 1);
2440 key.objectid = new_size;
2441 key.offset = 0;
2442 key.type = 0;
2443 while(1) {
Chris Mason4313b392008-01-03 09:08:48 -05002444 u64 ptr;
2445
Chris Masonedbd8d42007-12-21 16:27:24 -05002446 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2447 if (ret < 0)
2448 goto out;
2449bg_next:
2450 leaf = path->nodes[0];
2451 nritems = btrfs_header_nritems(leaf);
2452 ret = 0;
2453 slot = path->slots[0];
2454 if (slot < nritems)
2455 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2456 if (slot == nritems ||
2457 btrfs_key_type(&found_key) != BTRFS_BLOCK_GROUP_ITEM_KEY) {
2458 if (slot < nritems) {
2459 printk("shrinker found key %Lu %u %Lu\n",
2460 found_key.objectid, found_key.type,
2461 found_key.offset);
2462 path->slots[0]++;
2463 }
2464 if (path->slots[0] >= nritems) {
2465 ret = btrfs_next_leaf(root, path);
2466 if (ret < 0)
2467 break;
2468 if (ret == 1) {
2469 ret = 0;
2470 break;
2471 }
2472 }
2473 goto bg_next;
2474 }
2475 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2476 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);
2488 }
2489 clear_extent_dirty(&info->free_space_cache, new_size, (u64)-1,
2490 GFP_NOFS);
2491 btrfs_commit_transaction(trans, root);
2492out:
2493 btrfs_free_path(path);
2494 return ret;
2495}
2496
2497int btrfs_grow_extent_tree(struct btrfs_trans_handle *trans,
2498 struct btrfs_root *root, u64 new_size)
2499{
2500 struct btrfs_path *path;
2501 u64 nr = 0;
2502 u64 cur_byte;
2503 u64 old_size;
Chris Masonf9ef6602008-01-03 09:22:38 -05002504 unsigned long rem;
Chris Masonedbd8d42007-12-21 16:27:24 -05002505 struct btrfs_block_group_cache *cache;
2506 struct btrfs_block_group_item *item;
2507 struct btrfs_fs_info *info = root->fs_info;
2508 struct extent_map_tree *block_group_cache;
2509 struct btrfs_key key;
2510 struct extent_buffer *leaf;
2511 int ret;
2512 int bit;
2513
2514 old_size = btrfs_super_total_bytes(&info->super_copy);
2515 block_group_cache = &info->block_group_cache;
2516
2517 root = info->extent_root;
2518
2519 cache = btrfs_lookup_block_group(root->fs_info, old_size - 1);
2520
2521 cur_byte = cache->key.objectid + cache->key.offset;
2522 if (cur_byte >= new_size)
2523 goto set_size;
2524
2525 key.offset = BTRFS_BLOCK_GROUP_SIZE;
2526 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
2527
2528 path = btrfs_alloc_path();
2529 if (!path)
2530 return -ENOMEM;
2531
2532 while(cur_byte < new_size) {
2533 key.objectid = cur_byte;
2534 ret = btrfs_insert_empty_item(trans, root, path, &key,
2535 sizeof(struct btrfs_block_group_item));
2536 BUG_ON(ret);
2537 leaf = path->nodes[0];
2538 item = btrfs_item_ptr(leaf, path->slots[0],
2539 struct btrfs_block_group_item);
2540
2541 btrfs_set_disk_block_group_used(leaf, item, 0);
Chris Masonf9ef6602008-01-03 09:22:38 -05002542 div_long_long_rem(nr, 3, &rem);
2543 if (rem) {
Chris Masonedbd8d42007-12-21 16:27:24 -05002544 btrfs_set_disk_block_group_flags(leaf, item,
2545 BTRFS_BLOCK_GROUP_DATA);
2546 } else {
2547 btrfs_set_disk_block_group_flags(leaf, item, 0);
2548 }
2549 nr++;
2550
2551 cache = kmalloc(sizeof(*cache), GFP_NOFS);
2552 BUG_ON(!cache);
2553
2554 read_extent_buffer(leaf, &cache->item, (unsigned long)item,
2555 sizeof(cache->item));
2556
2557 memcpy(&cache->key, &key, sizeof(key));
2558 cache->cached = 0;
2559 cache->pinned = 0;
2560 cur_byte = key.objectid + key.offset;
2561 btrfs_release_path(root, path);
2562
2563 if (cache->item.flags & BTRFS_BLOCK_GROUP_DATA) {
2564 bit = BLOCK_GROUP_DATA;
2565 cache->data = BTRFS_BLOCK_GROUP_DATA;
2566 } else {
2567 bit = BLOCK_GROUP_METADATA;
2568 cache->data = 0;
2569 }
2570
2571 /* use EXTENT_LOCKED to prevent merging */
2572 set_extent_bits(block_group_cache, key.objectid,
2573 key.objectid + key.offset - 1,
2574 bit | EXTENT_LOCKED, GFP_NOFS);
2575 set_state_private(block_group_cache, key.objectid,
2576 (unsigned long)cache);
2577 }
2578 btrfs_free_path(path);
2579set_size:
2580 btrfs_set_super_total_bytes(&info->super_copy, new_size);
2581 return 0;
2582}
2583
Chris Mason9078a3e2007-04-26 16:46:15 -04002584int btrfs_read_block_groups(struct btrfs_root *root)
2585{
2586 struct btrfs_path *path;
2587 int ret;
2588 int err = 0;
Chris Mason96b51792007-10-15 16:15:19 -04002589 int bit;
Chris Mason9078a3e2007-04-26 16:46:15 -04002590 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04002591 struct btrfs_fs_info *info = root->fs_info;
Chris Mason96b51792007-10-15 16:15:19 -04002592 struct extent_map_tree *block_group_cache;
Chris Mason9078a3e2007-04-26 16:46:15 -04002593 struct btrfs_key key;
2594 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -04002595 struct extent_buffer *leaf;
Chris Mason96b51792007-10-15 16:15:19 -04002596
2597 block_group_cache = &info->block_group_cache;
Chris Mason9078a3e2007-04-26 16:46:15 -04002598
Chris Masonbe744172007-05-06 10:15:01 -04002599 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04002600 key.objectid = 0;
Chris Masondb945352007-10-15 16:15:53 -04002601 key.offset = BTRFS_BLOCK_GROUP_SIZE;
Chris Mason9078a3e2007-04-26 16:46:15 -04002602 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
2603
2604 path = btrfs_alloc_path();
2605 if (!path)
2606 return -ENOMEM;
2607
2608 while(1) {
Chris Masonbe744172007-05-06 10:15:01 -04002609 ret = btrfs_search_slot(NULL, info->extent_root,
Chris Mason9078a3e2007-04-26 16:46:15 -04002610 &key, path, 0, 0);
2611 if (ret != 0) {
2612 err = ret;
2613 break;
2614 }
Chris Mason5f39d392007-10-15 16:14:19 -04002615 leaf = path->nodes[0];
2616 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason9078a3e2007-04-26 16:46:15 -04002617 cache = kmalloc(sizeof(*cache), GFP_NOFS);
2618 if (!cache) {
2619 err = -1;
2620 break;
2621 }
Chris Mason3e1ad542007-05-07 20:03:49 -04002622
Chris Mason5f39d392007-10-15 16:14:19 -04002623 read_extent_buffer(leaf, &cache->item,
2624 btrfs_item_ptr_offset(leaf, path->slots[0]),
2625 sizeof(cache->item));
Chris Mason9078a3e2007-04-26 16:46:15 -04002626 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Masone37c9e62007-05-09 20:13:14 -04002627 cache->cached = 0;
Yan324ae4d2007-11-16 14:57:08 -05002628 cache->pinned = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04002629 key.objectid = found_key.objectid + found_key.offset;
2630 btrfs_release_path(root, path);
Chris Mason5f39d392007-10-15 16:14:19 -04002631
Chris Masonf84a8b32007-11-06 10:26:29 -05002632 if (cache->item.flags & BTRFS_BLOCK_GROUP_MIXED) {
2633 bit = BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA;
2634 cache->data = BTRFS_BLOCK_GROUP_MIXED;
2635 } else if (cache->item.flags & BTRFS_BLOCK_GROUP_DATA) {
Chris Mason96b51792007-10-15 16:15:19 -04002636 bit = BLOCK_GROUP_DATA;
Chris Masonf84a8b32007-11-06 10:26:29 -05002637 cache->data = BTRFS_BLOCK_GROUP_DATA;
Chris Mason96b51792007-10-15 16:15:19 -04002638 } else {
2639 bit = BLOCK_GROUP_METADATA;
2640 cache->data = 0;
Chris Mason31f3c992007-04-30 15:25:45 -04002641 }
Chris Mason96b51792007-10-15 16:15:19 -04002642
2643 /* use EXTENT_LOCKED to prevent merging */
2644 set_extent_bits(block_group_cache, found_key.objectid,
2645 found_key.objectid + found_key.offset - 1,
2646 bit | EXTENT_LOCKED, GFP_NOFS);
2647 set_state_private(block_group_cache, found_key.objectid,
Jens Axboeae2f5412007-10-19 09:22:59 -04002648 (unsigned long)cache);
Chris Mason96b51792007-10-15 16:15:19 -04002649
Chris Mason9078a3e2007-04-26 16:46:15 -04002650 if (key.objectid >=
Chris Masondb945352007-10-15 16:15:53 -04002651 btrfs_super_total_bytes(&info->super_copy))
Chris Mason9078a3e2007-04-26 16:46:15 -04002652 break;
2653 }
2654
2655 btrfs_free_path(path);
2656 return 0;
2657}