blob: 813566acc5d3aef1e6aa671d6c4ec21310b50dfe [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 */
Zach Brownec6b9102007-07-11 10:00:37 -040018#include <linux/sched.h>
Chris Masonedbd8d42007-12-21 16:27:24 -050019#include <linux/pagemap.h>
Chris Masonec44a352008-04-28 15:29:52 -040020#include <linux/writeback.h>
David Woodhouse21af8042008-08-12 14:13:26 +010021#include <linux/blkdev.h>
Chris Mason74493f72007-12-11 09:25:06 -050022#include "hash.h"
Miguela5eb62e2008-04-11 15:45:51 -040023#include "crc32c.h"
Chris Masonfec577f2007-02-26 10:40:21 -050024#include "ctree.h"
25#include "disk-io.h"
26#include "print-tree.h"
Chris Masone089f052007-03-16 16:20:31 -040027#include "transaction.h"
Chris Mason0b86a832008-03-24 15:01:56 -040028#include "volumes.h"
Chris Mason925baed2008-06-25 16:01:30 -040029#include "locking.h"
Yan Zheng31153d82008-07-28 15:32:19 -040030#include "ref-cache.h"
Chris Masonfec577f2007-02-26 10:40:21 -050031
Chris Masone089f052007-03-16 16:20:31 -040032static int finish_current_insert(struct btrfs_trans_handle *trans, struct
33 btrfs_root *extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -040034static int del_pending_extents(struct btrfs_trans_handle *trans, struct
35 btrfs_root *extent_root);
Chris Mason925baed2008-06-25 16:01:30 -040036static struct btrfs_block_group_cache *
37__btrfs_find_block_group(struct btrfs_root *root,
38 struct btrfs_block_group_cache *hint,
39 u64 search_start, int data, int owner);
Yand548ee52008-01-03 13:56:30 -050040
Chris Mason925baed2008-06-25 16:01:30 -040041void maybe_lock_mutex(struct btrfs_root *root)
42{
43 if (root != root->fs_info->extent_root &&
44 root != root->fs_info->chunk_root &&
45 root != root->fs_info->dev_root) {
46 mutex_lock(&root->fs_info->alloc_mutex);
47 }
48}
49
50void maybe_unlock_mutex(struct btrfs_root *root)
51{
52 if (root != root->fs_info->extent_root &&
53 root != root->fs_info->chunk_root &&
54 root != root->fs_info->dev_root) {
55 mutex_unlock(&root->fs_info->alloc_mutex);
56 }
57}
Chris Masonfec577f2007-02-26 10:40:21 -050058
Josef Bacik0f9dd462008-09-23 13:14:11 -040059static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
60{
61 return (cache->flags & bits) == bits;
62}
63
64/*
65 * this adds the block group to the fs_info rb tree for the block group
66 * cache
67 */
68int btrfs_add_block_group_cache(struct btrfs_fs_info *info,
69 struct btrfs_block_group_cache *block_group)
70{
71 struct rb_node **p;
72 struct rb_node *parent = NULL;
73 struct btrfs_block_group_cache *cache;
74
75 spin_lock(&info->block_group_cache_lock);
76 p = &info->block_group_cache_tree.rb_node;
77
78 while (*p) {
79 parent = *p;
80 cache = rb_entry(parent, struct btrfs_block_group_cache,
81 cache_node);
82 if (block_group->key.objectid < cache->key.objectid) {
83 p = &(*p)->rb_left;
84 } else if (block_group->key.objectid > cache->key.objectid) {
85 p = &(*p)->rb_right;
86 } else {
87 spin_unlock(&info->block_group_cache_lock);
88 return -EEXIST;
89 }
90 }
91
92 rb_link_node(&block_group->cache_node, parent, p);
93 rb_insert_color(&block_group->cache_node,
94 &info->block_group_cache_tree);
95 spin_unlock(&info->block_group_cache_lock);
96
97 return 0;
98}
99
100/*
101 * This will return the block group at or after bytenr if contains is 0, else
102 * it will return the block group that contains the bytenr
103 */
104static struct btrfs_block_group_cache *
105block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr,
106 int contains)
107{
108 struct btrfs_block_group_cache *cache, *ret = NULL;
109 struct rb_node *n;
110 u64 end, start;
111
112 spin_lock(&info->block_group_cache_lock);
113 n = info->block_group_cache_tree.rb_node;
114
115 while (n) {
116 cache = rb_entry(n, struct btrfs_block_group_cache,
117 cache_node);
118 end = cache->key.objectid + cache->key.offset - 1;
119 start = cache->key.objectid;
120
121 if (bytenr < start) {
122 if (!contains && (!ret || start < ret->key.objectid))
123 ret = cache;
124 n = n->rb_left;
125 } else if (bytenr > start) {
126 if (contains && bytenr <= end) {
127 ret = cache;
128 break;
129 }
130 n = n->rb_right;
131 } else {
132 ret = cache;
133 break;
134 }
135 }
136 spin_unlock(&info->block_group_cache_lock);
137
138 return ret;
139}
140
141/*
142 * this is only called by cache_block_group, since we could have freed extents
143 * we need to check the pinned_extents for any extents that can't be used yet
144 * since their free space will be released as soon as the transaction commits.
145 */
146static int add_new_free_space(struct btrfs_block_group_cache *block_group,
147 struct btrfs_fs_info *info, u64 start, u64 end)
148{
149 u64 extent_start, extent_end, size;
150 int ret;
151
152 while (start < end) {
153 ret = find_first_extent_bit(&info->pinned_extents, start,
154 &extent_start, &extent_end,
155 EXTENT_DIRTY);
156 if (ret)
157 break;
158
159 if (extent_start == start) {
160 start = extent_end + 1;
161 } else if (extent_start > start && extent_start < end) {
162 size = extent_start - start;
163 ret = btrfs_add_free_space(block_group, start, size);
164 BUG_ON(ret);
165 start = extent_end + 1;
166 } else {
167 break;
168 }
169 }
170
171 if (start < end) {
172 size = end - start;
173 ret = btrfs_add_free_space(block_group, start, size);
174 BUG_ON(ret);
175 }
176
177 return 0;
178}
179
Chris Masone37c9e62007-05-09 20:13:14 -0400180static int cache_block_group(struct btrfs_root *root,
181 struct btrfs_block_group_cache *block_group)
182{
183 struct btrfs_path *path;
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400184 int ret = 0;
Chris Masone37c9e62007-05-09 20:13:14 -0400185 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400186 struct extent_buffer *leaf;
Chris Masone37c9e62007-05-09 20:13:14 -0400187 int slot;
Chris Masone37c9e62007-05-09 20:13:14 -0400188 u64 last = 0;
Yan7d7d6062007-09-14 16:15:28 -0400189 u64 first_free;
Chris Masone37c9e62007-05-09 20:13:14 -0400190 int found = 0;
191
Chris Mason00f5c792007-11-30 10:09:33 -0500192 if (!block_group)
193 return 0;
194
Chris Masone37c9e62007-05-09 20:13:14 -0400195 root = root->fs_info->extent_root;
Chris Masone37c9e62007-05-09 20:13:14 -0400196
197 if (block_group->cached)
198 return 0;
Chris Masonf510cfe2007-10-15 16:14:48 -0400199
Chris Masone37c9e62007-05-09 20:13:14 -0400200 path = btrfs_alloc_path();
201 if (!path)
202 return -ENOMEM;
Yan7d7d6062007-09-14 16:15:28 -0400203
Chris Mason2cc58cf2007-08-27 16:49:44 -0400204 path->reada = 2;
Chris Mason5cd57b22008-06-25 16:01:30 -0400205 /*
206 * we get into deadlocks with paths held by callers of this function.
207 * since the alloc_mutex is protecting things right now, just
208 * skip the locking here
209 */
210 path->skip_locking = 1;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400211 first_free = max_t(u64, block_group->key.objectid,
212 BTRFS_SUPER_INFO_OFFSET + BTRFS_SUPER_INFO_SIZE);
Chris Masone37c9e62007-05-09 20:13:14 -0400213 key.objectid = block_group->key.objectid;
Chris Masone37c9e62007-05-09 20:13:14 -0400214 key.offset = 0;
215 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
216 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
217 if (ret < 0)
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400218 goto err;
Chris Mason0b86a832008-03-24 15:01:56 -0400219 ret = btrfs_previous_item(root, path, 0, BTRFS_EXTENT_ITEM_KEY);
Yand548ee52008-01-03 13:56:30 -0500220 if (ret < 0)
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400221 goto err;
Yand548ee52008-01-03 13:56:30 -0500222 if (ret == 0) {
223 leaf = path->nodes[0];
224 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
225 if (key.objectid + key.offset > first_free)
226 first_free = key.objectid + key.offset;
227 }
Chris Masone37c9e62007-05-09 20:13:14 -0400228 while(1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400229 leaf = path->nodes[0];
Chris Masone37c9e62007-05-09 20:13:14 -0400230 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -0400231 if (slot >= btrfs_header_nritems(leaf)) {
Chris Masone37c9e62007-05-09 20:13:14 -0400232 ret = btrfs_next_leaf(root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -0400233 if (ret < 0)
234 goto err;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400235 if (ret == 0)
Chris Masone37c9e62007-05-09 20:13:14 -0400236 continue;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400237 else
Chris Masone37c9e62007-05-09 20:13:14 -0400238 break;
Chris Masone37c9e62007-05-09 20:13:14 -0400239 }
Chris Mason5f39d392007-10-15 16:14:19 -0400240 btrfs_item_key_to_cpu(leaf, &key, slot);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400241 if (key.objectid < block_group->key.objectid)
Yan7d7d6062007-09-14 16:15:28 -0400242 goto next;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400243
Chris Masone37c9e62007-05-09 20:13:14 -0400244 if (key.objectid >= block_group->key.objectid +
Josef Bacik0f9dd462008-09-23 13:14:11 -0400245 block_group->key.offset)
Yan7d7d6062007-09-14 16:15:28 -0400246 break;
Yan7d7d6062007-09-14 16:15:28 -0400247
248 if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) {
249 if (!found) {
250 last = first_free;
251 found = 1;
Chris Masone37c9e62007-05-09 20:13:14 -0400252 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400253
254 add_new_free_space(block_group, root->fs_info, last,
255 key.objectid);
256
Yan7d7d6062007-09-14 16:15:28 -0400257 last = key.objectid + key.offset;
Chris Masone37c9e62007-05-09 20:13:14 -0400258 }
Yan7d7d6062007-09-14 16:15:28 -0400259next:
Chris Masone37c9e62007-05-09 20:13:14 -0400260 path->slots[0]++;
261 }
262
Yan7d7d6062007-09-14 16:15:28 -0400263 if (!found)
264 last = first_free;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400265
266 add_new_free_space(block_group, root->fs_info, last,
267 block_group->key.objectid +
268 block_group->key.offset);
269
Chris Masone37c9e62007-05-09 20:13:14 -0400270 block_group->cached = 1;
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400271 ret = 0;
Chris Mason54aa1f42007-06-22 14:16:25 -0400272err:
Chris Masone37c9e62007-05-09 20:13:14 -0400273 btrfs_free_path(path);
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400274 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -0400275}
276
Josef Bacik0f9dd462008-09-23 13:14:11 -0400277/*
278 * return the block group that starts at or after bytenr
279 */
Chris Mason0ef3e662008-05-24 14:04:53 -0400280struct btrfs_block_group_cache *btrfs_lookup_first_block_group(struct
281 btrfs_fs_info *info,
282 u64 bytenr)
283{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400284 struct btrfs_block_group_cache *cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400285
Josef Bacik0f9dd462008-09-23 13:14:11 -0400286 cache = block_group_cache_tree_search(info, bytenr, 0);
Chris Mason0ef3e662008-05-24 14:04:53 -0400287
Josef Bacik0f9dd462008-09-23 13:14:11 -0400288 return cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400289}
290
Josef Bacik0f9dd462008-09-23 13:14:11 -0400291/*
292 * return the block group that contains teh given bytenr
293 */
Chris Mason5276aed2007-06-11 21:33:38 -0400294struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
295 btrfs_fs_info *info,
Chris Masondb945352007-10-15 16:15:53 -0400296 u64 bytenr)
Chris Masonbe744172007-05-06 10:15:01 -0400297{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400298 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -0400299
Josef Bacik0f9dd462008-09-23 13:14:11 -0400300 cache = block_group_cache_tree_search(info, bytenr, 1);
Chris Mason96b51792007-10-15 16:15:19 -0400301
Josef Bacik0f9dd462008-09-23 13:14:11 -0400302 return cache;
Chris Masonbe744172007-05-06 10:15:01 -0400303}
Chris Mason0b86a832008-03-24 15:01:56 -0400304
Josef Bacik0f9dd462008-09-23 13:14:11 -0400305static int noinline find_free_space(struct btrfs_root *root,
306 struct btrfs_block_group_cache **cache_ret,
307 u64 *start_ret, u64 num, int data)
Chris Masone37c9e62007-05-09 20:13:14 -0400308{
Chris Masone37c9e62007-05-09 20:13:14 -0400309 int ret;
310 struct btrfs_block_group_cache *cache = *cache_ret;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400311 struct btrfs_free_space *info = NULL;
Chris Masone19caa52007-10-15 16:17:44 -0400312 u64 last;
Chris Masonc31f8832008-01-08 15:46:31 -0500313 u64 total_fs_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -0400314 u64 search_start = *start_ret;
Chris Masone37c9e62007-05-09 20:13:14 -0400315
Chris Mason7d9eb122008-07-08 14:19:17 -0400316 WARN_ON(!mutex_is_locked(&root->fs_info->alloc_mutex));
Chris Masonc31f8832008-01-08 15:46:31 -0500317 total_fs_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
Chris Masond7fc6402008-02-18 12:12:38 -0500318
Chris Mason0ef3e662008-05-24 14:04:53 -0400319 if (!cache)
Chris Mason54aa1f42007-06-22 14:16:25 -0400320 goto out;
Chris Masonf84a8b32007-11-06 10:26:29 -0500321
Josef Bacik0f9dd462008-09-23 13:14:11 -0400322 last = max(search_start, cache->key.objectid);
323
Chris Mason0ef3e662008-05-24 14:04:53 -0400324again:
325 ret = cache_block_group(root, cache);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400326 if (ret)
Chris Mason0ef3e662008-05-24 14:04:53 -0400327 goto out;
Chris Masone19caa52007-10-15 16:17:44 -0400328
Josef Bacik0f9dd462008-09-23 13:14:11 -0400329 if (cache->ro || !block_group_bits(cache, data))
Chris Mason0ef3e662008-05-24 14:04:53 -0400330 goto new_group;
331
Josef Bacik0f9dd462008-09-23 13:14:11 -0400332 info = btrfs_find_free_space(cache, last, num);
333 if (info) {
334 *start_ret = info->offset;
Chris Mason0b86a832008-03-24 15:01:56 -0400335 return 0;
Chris Mason8790d502008-04-03 16:29:03 -0400336 }
Chris Masone37c9e62007-05-09 20:13:14 -0400337
338new_group:
Chris Masone19caa52007-10-15 16:17:44 -0400339 last = cache->key.objectid + cache->key.offset;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400340
Chris Mason0ef3e662008-05-24 14:04:53 -0400341 cache = btrfs_lookup_first_block_group(root->fs_info, last);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400342 if (!cache || cache->key.objectid >= total_fs_bytes)
Chris Mason1a2b2ac2007-12-04 13:18:24 -0500343 goto out;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400344
Chris Masone37c9e62007-05-09 20:13:14 -0400345 *cache_ret = cache;
346 goto again;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400347
348out:
349 return -ENOSPC;
Chris Masone37c9e62007-05-09 20:13:14 -0400350}
351
Chris Mason84f54cf2007-06-12 07:43:08 -0400352static u64 div_factor(u64 num, int factor)
353{
Chris Mason257d0ce2007-11-07 21:08:16 -0500354 if (factor == 10)
355 return num;
Chris Mason84f54cf2007-06-12 07:43:08 -0400356 num *= factor;
357 do_div(num, 10);
358 return num;
359}
360
Josef Bacik0f9dd462008-09-23 13:14:11 -0400361static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
362 u64 flags)
Chris Mason6324fbf2008-03-24 15:01:59 -0400363{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400364 struct list_head *head = &info->space_info;
365 struct list_head *cur;
366 struct btrfs_space_info *found;
367 list_for_each(cur, head) {
368 found = list_entry(cur, struct btrfs_space_info, list);
369 if (found->flags == flags)
370 return found;
371 }
372 return NULL;
373
Chris Mason6324fbf2008-03-24 15:01:59 -0400374}
375
Chris Mason925baed2008-06-25 16:01:30 -0400376static struct btrfs_block_group_cache *
377__btrfs_find_block_group(struct btrfs_root *root,
378 struct btrfs_block_group_cache *hint,
379 u64 search_start, int data, int owner)
Chris Masoncd1bc462007-04-27 10:08:34 -0400380{
Chris Mason96b51792007-10-15 16:15:19 -0400381 struct btrfs_block_group_cache *cache;
Chris Mason31f3c992007-04-30 15:25:45 -0400382 struct btrfs_block_group_cache *found_group = NULL;
Chris Masoncd1bc462007-04-27 10:08:34 -0400383 struct btrfs_fs_info *info = root->fs_info;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400384 struct btrfs_space_info *sinfo;
Chris Masoncd1bc462007-04-27 10:08:34 -0400385 u64 used;
Chris Mason31f3c992007-04-30 15:25:45 -0400386 u64 last = 0;
Chris Mason96b51792007-10-15 16:15:19 -0400387 u64 free_check;
Chris Mason31f3c992007-04-30 15:25:45 -0400388 int full_search = 0;
Chris Masonbce4eae2008-04-24 14:42:46 -0400389 int factor = 10;
Chris Mason0ef3e662008-05-24 14:04:53 -0400390 int wrapped = 0;
Chris Masonde428b62007-05-18 13:28:27 -0400391
Chris Masona236aed2008-04-29 09:38:00 -0400392 if (data & BTRFS_BLOCK_GROUP_METADATA)
393 factor = 9;
Chris Masonbe744172007-05-06 10:15:01 -0400394
Chris Mason0ef3e662008-05-24 14:04:53 -0400395 if (search_start) {
Chris Masonbe744172007-05-06 10:15:01 -0400396 struct btrfs_block_group_cache *shint;
Chris Mason0ef3e662008-05-24 14:04:53 -0400397 shint = btrfs_lookup_first_block_group(info, search_start);
Chris Mason8f18cf12008-04-25 16:53:30 -0400398 if (shint && block_group_bits(shint, data) && !shint->ro) {
Chris Masonc286ac42008-07-22 23:06:41 -0400399 spin_lock(&shint->lock);
Chris Masonbe744172007-05-06 10:15:01 -0400400 used = btrfs_block_group_used(&shint->item);
Yan324ae4d2007-11-16 14:57:08 -0500401 if (used + shint->pinned <
402 div_factor(shint->key.offset, factor)) {
Chris Masonc286ac42008-07-22 23:06:41 -0400403 spin_unlock(&shint->lock);
Chris Masonbe744172007-05-06 10:15:01 -0400404 return shint;
405 }
Chris Masonc286ac42008-07-22 23:06:41 -0400406 spin_unlock(&shint->lock);
Chris Masonbe744172007-05-06 10:15:01 -0400407 }
408 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400409 if (hint && !hint->ro && block_group_bits(hint, data)) {
Chris Masonc286ac42008-07-22 23:06:41 -0400410 spin_lock(&hint->lock);
Chris Mason31f3c992007-04-30 15:25:45 -0400411 used = btrfs_block_group_used(&hint->item);
Yan324ae4d2007-11-16 14:57:08 -0500412 if (used + hint->pinned <
413 div_factor(hint->key.offset, factor)) {
Chris Masonc286ac42008-07-22 23:06:41 -0400414 spin_unlock(&hint->lock);
Chris Mason31f3c992007-04-30 15:25:45 -0400415 return hint;
416 }
Chris Masonc286ac42008-07-22 23:06:41 -0400417 spin_unlock(&hint->lock);
Chris Masone19caa52007-10-15 16:17:44 -0400418 last = hint->key.objectid + hint->key.offset;
Chris Mason31f3c992007-04-30 15:25:45 -0400419 } else {
Chris Masone37c9e62007-05-09 20:13:14 -0400420 if (hint)
Chris Mason0ef3e662008-05-24 14:04:53 -0400421 last = max(hint->key.objectid, search_start);
Chris Masone37c9e62007-05-09 20:13:14 -0400422 else
Chris Mason0ef3e662008-05-24 14:04:53 -0400423 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -0400424 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400425 sinfo = __find_space_info(root->fs_info, data);
426 if (!sinfo)
427 goto found;
Chris Mason31f3c992007-04-30 15:25:45 -0400428again:
Chris Masoncd1bc462007-04-27 10:08:34 -0400429 while(1) {
Josef Bacik0f9dd462008-09-23 13:14:11 -0400430 struct list_head *l;
431
432 cache = NULL;
433
434 spin_lock(&sinfo->lock);
435 list_for_each(l, &sinfo->block_groups) {
436 struct btrfs_block_group_cache *entry;
437 entry = list_entry(l, struct btrfs_block_group_cache,
438 list);
439 if ((entry->key.objectid >= last) &&
440 (!cache || (entry->key.objectid <
441 cache->key.objectid)))
442 cache = entry;
443 }
444 spin_unlock(&sinfo->lock);
445
446 if (!cache)
Chris Masoncd1bc462007-04-27 10:08:34 -0400447 break;
Chris Mason96b51792007-10-15 16:15:19 -0400448
Chris Masonc286ac42008-07-22 23:06:41 -0400449 spin_lock(&cache->lock);
Chris Mason96b51792007-10-15 16:15:19 -0400450 last = cache->key.objectid + cache->key.offset;
451 used = btrfs_block_group_used(&cache->item);
452
Chris Mason8f18cf12008-04-25 16:53:30 -0400453 if (!cache->ro && block_group_bits(cache, data)) {
Chris Mason0ef3e662008-05-24 14:04:53 -0400454 free_check = div_factor(cache->key.offset, factor);
Chris Mason8790d502008-04-03 16:29:03 -0400455 if (used + cache->pinned < free_check) {
456 found_group = cache;
Chris Masonc286ac42008-07-22 23:06:41 -0400457 spin_unlock(&cache->lock);
Chris Mason8790d502008-04-03 16:29:03 -0400458 goto found;
459 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400460 }
Chris Masonc286ac42008-07-22 23:06:41 -0400461 spin_unlock(&cache->lock);
Chris Masonde428b62007-05-18 13:28:27 -0400462 cond_resched();
Chris Masoncd1bc462007-04-27 10:08:34 -0400463 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400464 if (!wrapped) {
465 last = search_start;
466 wrapped = 1;
467 goto again;
468 }
469 if (!full_search && factor < 10) {
Chris Masonbe744172007-05-06 10:15:01 -0400470 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -0400471 full_search = 1;
Chris Mason0ef3e662008-05-24 14:04:53 -0400472 factor = 10;
Chris Mason31f3c992007-04-30 15:25:45 -0400473 goto again;
474 }
Chris Masonbe744172007-05-06 10:15:01 -0400475found:
Chris Mason31f3c992007-04-30 15:25:45 -0400476 return found_group;
Chris Masoncd1bc462007-04-27 10:08:34 -0400477}
478
Chris Mason925baed2008-06-25 16:01:30 -0400479struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
480 struct btrfs_block_group_cache
481 *hint, u64 search_start,
482 int data, int owner)
483{
484
485 struct btrfs_block_group_cache *ret;
Chris Mason925baed2008-06-25 16:01:30 -0400486 ret = __btrfs_find_block_group(root, hint, search_start, data, owner);
Chris Mason925baed2008-06-25 16:01:30 -0400487 return ret;
488}
Josef Bacik0f9dd462008-09-23 13:14:11 -0400489
Chris Mason7bb86312007-12-11 09:25:06 -0500490static u64 hash_extent_ref(u64 root_objectid, u64 ref_generation,
Chris Mason74493f72007-12-11 09:25:06 -0500491 u64 owner, u64 owner_offset)
492{
493 u32 high_crc = ~(u32)0;
494 u32 low_crc = ~(u32)0;
495 __le64 lenum;
Chris Mason74493f72007-12-11 09:25:06 -0500496 lenum = cpu_to_le64(root_objectid);
Miguela5eb62e2008-04-11 15:45:51 -0400497 high_crc = btrfs_crc32c(high_crc, &lenum, sizeof(lenum));
Chris Mason7bb86312007-12-11 09:25:06 -0500498 lenum = cpu_to_le64(ref_generation);
Miguela5eb62e2008-04-11 15:45:51 -0400499 low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
Chris Mason21a49892008-02-01 14:51:59 -0500500 if (owner >= BTRFS_FIRST_FREE_OBJECTID) {
501 lenum = cpu_to_le64(owner);
Miguela5eb62e2008-04-11 15:45:51 -0400502 low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
Chris Mason21a49892008-02-01 14:51:59 -0500503 lenum = cpu_to_le64(owner_offset);
Miguela5eb62e2008-04-11 15:45:51 -0400504 low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
Chris Mason21a49892008-02-01 14:51:59 -0500505 }
Chris Mason74493f72007-12-11 09:25:06 -0500506 return ((u64)high_crc << 32) | (u64)low_crc;
507}
508
Chris Mason7bb86312007-12-11 09:25:06 -0500509static int match_extent_ref(struct extent_buffer *leaf,
510 struct btrfs_extent_ref *disk_ref,
511 struct btrfs_extent_ref *cpu_ref)
512{
513 int ret;
514 int len;
515
516 if (cpu_ref->objectid)
517 len = sizeof(*cpu_ref);
518 else
519 len = 2 * sizeof(u64);
520 ret = memcmp_extent_buffer(leaf, cpu_ref, (unsigned long)disk_ref,
521 len);
522 return ret == 0;
523}
524
Chris Masone02119d2008-09-05 16:13:11 -0400525/* simple helper to search for an existing extent at a given offset */
526int btrfs_lookup_extent(struct btrfs_root *root, struct btrfs_path *path,
527 u64 start, u64 len)
528{
529 int ret;
530 struct btrfs_key key;
531
532 maybe_lock_mutex(root);
533 key.objectid = start;
534 key.offset = len;
535 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
536 ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
537 0, 0);
538 maybe_unlock_mutex(root);
539 return ret;
540}
541
Chris Mason98ed5172008-01-03 10:01:48 -0500542static int noinline lookup_extent_backref(struct btrfs_trans_handle *trans,
543 struct btrfs_root *root,
544 struct btrfs_path *path, u64 bytenr,
545 u64 root_objectid,
546 u64 ref_generation, u64 owner,
547 u64 owner_offset, int del)
Chris Mason7bb86312007-12-11 09:25:06 -0500548{
549 u64 hash;
550 struct btrfs_key key;
551 struct btrfs_key found_key;
552 struct btrfs_extent_ref ref;
553 struct extent_buffer *leaf;
554 struct btrfs_extent_ref *disk_ref;
555 int ret;
556 int ret2;
557
558 btrfs_set_stack_ref_root(&ref, root_objectid);
559 btrfs_set_stack_ref_generation(&ref, ref_generation);
560 btrfs_set_stack_ref_objectid(&ref, owner);
561 btrfs_set_stack_ref_offset(&ref, owner_offset);
562
563 hash = hash_extent_ref(root_objectid, ref_generation, owner,
564 owner_offset);
565 key.offset = hash;
566 key.objectid = bytenr;
567 key.type = BTRFS_EXTENT_REF_KEY;
568
569 while (1) {
570 ret = btrfs_search_slot(trans, root, &key, path,
571 del ? -1 : 0, del);
572 if (ret < 0)
573 goto out;
574 leaf = path->nodes[0];
575 if (ret != 0) {
576 u32 nritems = btrfs_header_nritems(leaf);
577 if (path->slots[0] >= nritems) {
578 ret2 = btrfs_next_leaf(root, path);
579 if (ret2)
580 goto out;
581 leaf = path->nodes[0];
582 }
583 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
584 if (found_key.objectid != bytenr ||
585 found_key.type != BTRFS_EXTENT_REF_KEY)
586 goto out;
587 key.offset = found_key.offset;
588 if (del) {
589 btrfs_release_path(root, path);
590 continue;
591 }
592 }
593 disk_ref = btrfs_item_ptr(path->nodes[0],
594 path->slots[0],
595 struct btrfs_extent_ref);
596 if (match_extent_ref(path->nodes[0], disk_ref, &ref)) {
597 ret = 0;
598 goto out;
599 }
600 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
601 key.offset = found_key.offset + 1;
602 btrfs_release_path(root, path);
603 }
604out:
605 return ret;
606}
607
Chris Masond8d5f3e2007-12-11 12:42:00 -0500608/*
609 * Back reference rules. Back refs have three main goals:
610 *
611 * 1) differentiate between all holders of references to an extent so that
612 * when a reference is dropped we can make sure it was a valid reference
613 * before freeing the extent.
614 *
615 * 2) Provide enough information to quickly find the holders of an extent
616 * if we notice a given block is corrupted or bad.
617 *
618 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
619 * maintenance. This is actually the same as #2, but with a slightly
620 * different use case.
621 *
622 * File extents can be referenced by:
623 *
624 * - multiple snapshots, subvolumes, or different generations in one subvol
625 * - different files inside a single subvolume (in theory, not implemented yet)
626 * - different offsets inside a file (bookend extents in file.c)
627 *
628 * The extent ref structure has fields for:
629 *
630 * - Objectid of the subvolume root
631 * - Generation number of the tree holding the reference
632 * - objectid of the file holding the reference
633 * - offset in the file corresponding to the key holding the reference
634 *
635 * When a file extent is allocated the fields are filled in:
636 * (root_key.objectid, trans->transid, inode objectid, offset in file)
637 *
638 * When a leaf is cow'd new references are added for every file extent found
639 * in the leaf. It looks the same as the create case, but trans->transid
640 * will be different when the block is cow'd.
641 *
642 * (root_key.objectid, trans->transid, inode objectid, offset in file)
643 *
644 * When a file extent is removed either during snapshot deletion or file
645 * truncation, the corresponding back reference is found
646 * by searching for:
647 *
648 * (btrfs_header_owner(leaf), btrfs_header_generation(leaf),
649 * inode objectid, offset in file)
650 *
651 * Btree extents can be referenced by:
652 *
653 * - Different subvolumes
654 * - Different generations of the same subvolume
655 *
656 * Storing sufficient information for a full reverse mapping of a btree
657 * block would require storing the lowest key of the block in the backref,
658 * and it would require updating that lowest key either before write out or
659 * every time it changed. Instead, the objectid of the lowest key is stored
660 * along with the level of the tree block. This provides a hint
661 * about where in the btree the block can be found. Searches through the
662 * btree only need to look for a pointer to that block, so they stop one
663 * level higher than the level recorded in the backref.
664 *
665 * Some btrees do not do reference counting on their extents. These
666 * include the extent tree and the tree of tree roots. Backrefs for these
667 * trees always have a generation of zero.
668 *
669 * When a tree block is created, back references are inserted:
670 *
Chris Masonf6dbff52007-12-13 11:13:32 -0500671 * (root->root_key.objectid, trans->transid or zero, level, lowest_key_objectid)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500672 *
673 * When a tree block is cow'd in a reference counted root,
674 * new back references are added for all the blocks it points to.
675 * These are of the form (trans->transid will have increased since creation):
676 *
Chris Masonf6dbff52007-12-13 11:13:32 -0500677 * (root->root_key.objectid, trans->transid, level, lowest_key_objectid)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500678 *
679 * Because the lowest_key_objectid and the level are just hints
680 * they are not used when backrefs are deleted. When a backref is deleted:
681 *
682 * if backref was for a tree root:
683 * root_objectid = root->root_key.objectid
684 * else
685 * root_objectid = btrfs_header_owner(parent)
686 *
687 * (root_objectid, btrfs_header_generation(parent) or zero, 0, 0)
688 *
689 * Back Reference Key hashing:
690 *
691 * Back references have four fields, each 64 bits long. Unfortunately,
692 * This is hashed into a single 64 bit number and placed into the key offset.
693 * The key objectid corresponds to the first byte in the extent, and the
694 * key type is set to BTRFS_EXTENT_REF_KEY
695 */
Chris Mason7bb86312007-12-11 09:25:06 -0500696int btrfs_insert_extent_backref(struct btrfs_trans_handle *trans,
697 struct btrfs_root *root,
698 struct btrfs_path *path, u64 bytenr,
699 u64 root_objectid, u64 ref_generation,
700 u64 owner, u64 owner_offset)
Chris Mason74493f72007-12-11 09:25:06 -0500701{
702 u64 hash;
703 struct btrfs_key key;
704 struct btrfs_extent_ref ref;
Chris Mason7bb86312007-12-11 09:25:06 -0500705 struct btrfs_extent_ref *disk_ref;
Chris Mason74493f72007-12-11 09:25:06 -0500706 int ret;
707
708 btrfs_set_stack_ref_root(&ref, root_objectid);
Chris Mason7bb86312007-12-11 09:25:06 -0500709 btrfs_set_stack_ref_generation(&ref, ref_generation);
Chris Mason74493f72007-12-11 09:25:06 -0500710 btrfs_set_stack_ref_objectid(&ref, owner);
711 btrfs_set_stack_ref_offset(&ref, owner_offset);
712
Chris Mason7bb86312007-12-11 09:25:06 -0500713 hash = hash_extent_ref(root_objectid, ref_generation, owner,
714 owner_offset);
Chris Mason74493f72007-12-11 09:25:06 -0500715 key.offset = hash;
716 key.objectid = bytenr;
717 key.type = BTRFS_EXTENT_REF_KEY;
718
719 ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(ref));
720 while (ret == -EEXIST) {
Chris Mason7bb86312007-12-11 09:25:06 -0500721 disk_ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
722 struct btrfs_extent_ref);
723 if (match_extent_ref(path->nodes[0], disk_ref, &ref))
724 goto out;
725 key.offset++;
726 btrfs_release_path(root, path);
727 ret = btrfs_insert_empty_item(trans, root, path, &key,
728 sizeof(ref));
Chris Mason74493f72007-12-11 09:25:06 -0500729 }
Chris Mason7bb86312007-12-11 09:25:06 -0500730 if (ret)
731 goto out;
732 disk_ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
733 struct btrfs_extent_ref);
734 write_extent_buffer(path->nodes[0], &ref, (unsigned long)disk_ref,
735 sizeof(ref));
736 btrfs_mark_buffer_dirty(path->nodes[0]);
737out:
738 btrfs_release_path(root, path);
739 return ret;
Chris Mason74493f72007-12-11 09:25:06 -0500740}
741
Chris Mason925baed2008-06-25 16:01:30 -0400742static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Chris Masonb18c6682007-04-17 13:26:50 -0400743 struct btrfs_root *root,
Chris Mason74493f72007-12-11 09:25:06 -0500744 u64 bytenr, u64 num_bytes,
Chris Mason7bb86312007-12-11 09:25:06 -0500745 u64 root_objectid, u64 ref_generation,
Chris Mason74493f72007-12-11 09:25:06 -0500746 u64 owner, u64 owner_offset)
Chris Mason02217ed2007-03-02 16:08:05 -0500747{
Chris Mason5caf2a02007-04-02 11:20:42 -0400748 struct btrfs_path *path;
Chris Mason02217ed2007-03-02 16:08:05 -0500749 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400750 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400751 struct extent_buffer *l;
Chris Mason234b63a2007-03-13 10:46:10 -0400752 struct btrfs_extent_item *item;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400753 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500754
Chris Masondb945352007-10-15 16:15:53 -0400755 WARN_ON(num_bytes < root->sectorsize);
Chris Mason5caf2a02007-04-02 11:20:42 -0400756 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -0400757 if (!path)
758 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -0400759
Chris Mason3c12ac72008-04-21 12:01:38 -0400760 path->reada = 1;
Chris Masondb945352007-10-15 16:15:53 -0400761 key.objectid = bytenr;
Chris Mason62e27492007-03-15 12:56:47 -0400762 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masondb945352007-10-15 16:15:53 -0400763 key.offset = num_bytes;
Chris Mason5caf2a02007-04-02 11:20:42 -0400764 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -0400765 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400766 if (ret < 0)
767 return ret;
Chris Masona429e512007-04-18 16:15:28 -0400768 if (ret != 0) {
Chris Masona28ec192007-03-06 20:08:01 -0500769 BUG();
Chris Masona429e512007-04-18 16:15:28 -0400770 }
Chris Mason02217ed2007-03-02 16:08:05 -0500771 BUG_ON(ret != 0);
Chris Mason5f39d392007-10-15 16:14:19 -0400772 l = path->nodes[0];
Chris Mason5caf2a02007-04-02 11:20:42 -0400773 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400774 refs = btrfs_extent_refs(l, item);
775 btrfs_set_extent_refs(l, item, refs + 1);
Chris Mason5caf2a02007-04-02 11:20:42 -0400776 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masona28ec192007-03-06 20:08:01 -0500777
Chris Mason5caf2a02007-04-02 11:20:42 -0400778 btrfs_release_path(root->fs_info->extent_root, path);
Chris Mason7bb86312007-12-11 09:25:06 -0500779
Chris Mason3c12ac72008-04-21 12:01:38 -0400780 path->reada = 1;
Chris Mason7bb86312007-12-11 09:25:06 -0500781 ret = btrfs_insert_extent_backref(trans, root->fs_info->extent_root,
782 path, bytenr, root_objectid,
783 ref_generation, owner, owner_offset);
784 BUG_ON(ret);
Chris Mason9f5fae22007-03-20 14:38:32 -0400785 finish_current_insert(trans, root->fs_info->extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -0400786 del_pending_extents(trans, root->fs_info->extent_root);
Chris Mason74493f72007-12-11 09:25:06 -0500787
788 btrfs_free_path(path);
Chris Mason02217ed2007-03-02 16:08:05 -0500789 return 0;
790}
791
Chris Mason925baed2008-06-25 16:01:30 -0400792int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
793 struct btrfs_root *root,
794 u64 bytenr, u64 num_bytes,
795 u64 root_objectid, u64 ref_generation,
796 u64 owner, u64 owner_offset)
797{
798 int ret;
799
800 mutex_lock(&root->fs_info->alloc_mutex);
801 ret = __btrfs_inc_extent_ref(trans, root, bytenr, num_bytes,
802 root_objectid, ref_generation,
803 owner, owner_offset);
804 mutex_unlock(&root->fs_info->alloc_mutex);
805 return ret;
806}
807
Chris Masone9d0b132007-08-10 14:06:19 -0400808int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
809 struct btrfs_root *root)
810{
811 finish_current_insert(trans, root->fs_info->extent_root);
812 del_pending_extents(trans, root->fs_info->extent_root);
813 return 0;
814}
815
Chris Masonb18c6682007-04-17 13:26:50 -0400816static int lookup_extent_ref(struct btrfs_trans_handle *trans,
Chris Masondb945352007-10-15 16:15:53 -0400817 struct btrfs_root *root, u64 bytenr,
818 u64 num_bytes, u32 *refs)
Chris Masona28ec192007-03-06 20:08:01 -0500819{
Chris Mason5caf2a02007-04-02 11:20:42 -0400820 struct btrfs_path *path;
Chris Masona28ec192007-03-06 20:08:01 -0500821 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400822 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400823 struct extent_buffer *l;
Chris Mason234b63a2007-03-13 10:46:10 -0400824 struct btrfs_extent_item *item;
Chris Mason5caf2a02007-04-02 11:20:42 -0400825
Chris Masondb945352007-10-15 16:15:53 -0400826 WARN_ON(num_bytes < root->sectorsize);
Chris Mason5caf2a02007-04-02 11:20:42 -0400827 path = btrfs_alloc_path();
Chris Mason3c12ac72008-04-21 12:01:38 -0400828 path->reada = 1;
Chris Masondb945352007-10-15 16:15:53 -0400829 key.objectid = bytenr;
830 key.offset = num_bytes;
Chris Mason62e27492007-03-15 12:56:47 -0400831 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Mason5caf2a02007-04-02 11:20:42 -0400832 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -0400833 0, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400834 if (ret < 0)
835 goto out;
Chris Mason5f39d392007-10-15 16:14:19 -0400836 if (ret != 0) {
837 btrfs_print_leaf(root, path->nodes[0]);
Chris Masondb945352007-10-15 16:15:53 -0400838 printk("failed to find block number %Lu\n", bytenr);
Chris Masona28ec192007-03-06 20:08:01 -0500839 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -0400840 }
841 l = path->nodes[0];
Chris Mason5caf2a02007-04-02 11:20:42 -0400842 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400843 *refs = btrfs_extent_refs(l, item);
Chris Mason54aa1f42007-06-22 14:16:25 -0400844out:
Chris Mason5caf2a02007-04-02 11:20:42 -0400845 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -0500846 return 0;
847}
848
Yan Zhengf321e492008-07-30 09:26:11 -0400849
850static int get_reference_status(struct btrfs_root *root, u64 bytenr,
851 u64 parent_gen, u64 ref_objectid,
852 u64 *min_generation, u32 *ref_count)
Chris Masonbe20aa92007-12-17 20:14:01 -0500853{
854 struct btrfs_root *extent_root = root->fs_info->extent_root;
855 struct btrfs_path *path;
Yan Zhengf321e492008-07-30 09:26:11 -0400856 struct extent_buffer *leaf;
857 struct btrfs_extent_ref *ref_item;
Chris Masonbe20aa92007-12-17 20:14:01 -0500858 struct btrfs_key key;
859 struct btrfs_key found_key;
Yan Zhengf321e492008-07-30 09:26:11 -0400860 u64 root_objectid = root->root_key.objectid;
861 u64 ref_generation;
862 u32 nritems;
863 int ret;
Chris Masonbe20aa92007-12-17 20:14:01 -0500864
Chris Masonbe20aa92007-12-17 20:14:01 -0500865 key.objectid = bytenr;
866 key.offset = 0;
Yan Zhengf321e492008-07-30 09:26:11 -0400867 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masonbe20aa92007-12-17 20:14:01 -0500868
Yan Zhengf321e492008-07-30 09:26:11 -0400869 path = btrfs_alloc_path();
870 mutex_lock(&root->fs_info->alloc_mutex);
Chris Masonbe20aa92007-12-17 20:14:01 -0500871 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
872 if (ret < 0)
873 goto out;
874 BUG_ON(ret == 0);
875
Yan Zhengf321e492008-07-30 09:26:11 -0400876 leaf = path->nodes[0];
877 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -0500878
879 if (found_key.objectid != bytenr ||
880 found_key.type != BTRFS_EXTENT_ITEM_KEY) {
Yan Zhengf321e492008-07-30 09:26:11 -0400881 ret = 1;
Chris Masonbe20aa92007-12-17 20:14:01 -0500882 goto out;
883 }
884
Yan Zhengf321e492008-07-30 09:26:11 -0400885 *ref_count = 0;
886 *min_generation = (u64)-1;
887
Chris Masonbe20aa92007-12-17 20:14:01 -0500888 while (1) {
Yan Zhengf321e492008-07-30 09:26:11 -0400889 leaf = path->nodes[0];
890 nritems = btrfs_header_nritems(leaf);
Chris Masonbe20aa92007-12-17 20:14:01 -0500891 if (path->slots[0] >= nritems) {
892 ret = btrfs_next_leaf(extent_root, path);
Yan Zhengf321e492008-07-30 09:26:11 -0400893 if (ret < 0)
894 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -0500895 if (ret == 0)
896 continue;
897 break;
898 }
Yan Zhengf321e492008-07-30 09:26:11 -0400899 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -0500900 if (found_key.objectid != bytenr)
901 break;
Chris Masonbd098352008-01-03 13:23:19 -0500902
Chris Masonbe20aa92007-12-17 20:14:01 -0500903 if (found_key.type != BTRFS_EXTENT_REF_KEY) {
904 path->slots[0]++;
905 continue;
906 }
907
Yan Zhengf321e492008-07-30 09:26:11 -0400908 ref_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masonbe20aa92007-12-17 20:14:01 -0500909 struct btrfs_extent_ref);
Yan Zhengf321e492008-07-30 09:26:11 -0400910 ref_generation = btrfs_ref_generation(leaf, ref_item);
911 /*
912 * For (parent_gen > 0 && parent_gen > ref_gen):
913 *
Yanbcc63ab2008-07-30 16:29:20 -0400914 * we reach here through the oldest root, therefore
915 * all other reference from same snapshot should have
Yan Zhengf321e492008-07-30 09:26:11 -0400916 * a larger generation.
917 */
918 if ((root_objectid != btrfs_ref_root(leaf, ref_item)) ||
919 (parent_gen > 0 && parent_gen > ref_generation) ||
920 (ref_objectid >= BTRFS_FIRST_FREE_OBJECTID &&
921 ref_objectid != btrfs_ref_objectid(leaf, ref_item))) {
922 if (ref_count)
923 *ref_count = 2;
924 break;
925 }
Chris Masonbe20aa92007-12-17 20:14:01 -0500926
Yan Zhengf321e492008-07-30 09:26:11 -0400927 *ref_count = 1;
928 if (*min_generation > ref_generation)
929 *min_generation = ref_generation;
930
Chris Masonbe20aa92007-12-17 20:14:01 -0500931 path->slots[0]++;
932 }
Yan Zhengf321e492008-07-30 09:26:11 -0400933 ret = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -0500934out:
Chris Mason925baed2008-06-25 16:01:30 -0400935 mutex_unlock(&root->fs_info->alloc_mutex);
Yan Zhengf321e492008-07-30 09:26:11 -0400936 btrfs_free_path(path);
937 return ret;
938}
939
Yan Zheng7ea394f2008-08-05 13:05:02 -0400940int btrfs_cross_ref_exists(struct btrfs_trans_handle *trans,
941 struct btrfs_root *root,
Yan Zhengf321e492008-07-30 09:26:11 -0400942 struct btrfs_key *key, u64 bytenr)
943{
Yan Zhengf321e492008-07-30 09:26:11 -0400944 struct btrfs_root *old_root;
945 struct btrfs_path *path = NULL;
946 struct extent_buffer *eb;
947 struct btrfs_file_extent_item *item;
948 u64 ref_generation;
949 u64 min_generation;
950 u64 extent_start;
951 u32 ref_count;
952 int level;
953 int ret;
954
Yan Zheng7ea394f2008-08-05 13:05:02 -0400955 BUG_ON(trans == NULL);
Yan Zhengf321e492008-07-30 09:26:11 -0400956 BUG_ON(key->type != BTRFS_EXTENT_DATA_KEY);
957 ret = get_reference_status(root, bytenr, 0, key->objectid,
958 &min_generation, &ref_count);
959 if (ret)
960 return ret;
961
962 if (ref_count != 1)
963 return 1;
964
Yan Zhengf321e492008-07-30 09:26:11 -0400965 old_root = root->dirty_root->root;
966 ref_generation = old_root->root_key.offset;
967
968 /* all references are created in running transaction */
969 if (min_generation > ref_generation) {
970 ret = 0;
971 goto out;
972 }
973
974 path = btrfs_alloc_path();
975 if (!path) {
976 ret = -ENOMEM;
977 goto out;
978 }
979
980 path->skip_locking = 1;
981 /* if no item found, the extent is referenced by other snapshot */
982 ret = btrfs_search_slot(NULL, old_root, key, path, 0, 0);
983 if (ret)
984 goto out;
985
986 eb = path->nodes[0];
987 item = btrfs_item_ptr(eb, path->slots[0],
988 struct btrfs_file_extent_item);
989 if (btrfs_file_extent_type(eb, item) != BTRFS_FILE_EXTENT_REG ||
990 btrfs_file_extent_disk_bytenr(eb, item) != bytenr) {
991 ret = 1;
992 goto out;
993 }
994
995 for (level = BTRFS_MAX_LEVEL - 1; level >= -1; level--) {
996 if (level >= 0) {
997 eb = path->nodes[level];
998 if (!eb)
999 continue;
1000 extent_start = eb->start;
Yanbcc63ab2008-07-30 16:29:20 -04001001 } else
Yan Zhengf321e492008-07-30 09:26:11 -04001002 extent_start = bytenr;
1003
1004 ret = get_reference_status(root, extent_start, ref_generation,
1005 0, &min_generation, &ref_count);
1006 if (ret)
1007 goto out;
1008
1009 if (ref_count != 1) {
1010 ret = 1;
1011 goto out;
1012 }
1013 if (level >= 0)
1014 ref_generation = btrfs_header_generation(eb);
1015 }
1016 ret = 0;
1017out:
1018 if (path)
1019 btrfs_free_path(path);
Yan Zhengf321e492008-07-30 09:26:11 -04001020 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001021}
Chris Masonc5739bb2007-04-10 09:27:04 -04001022
Chris Masone089f052007-03-16 16:20:31 -04001023int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Yan Zheng31153d82008-07-28 15:32:19 -04001024 struct extent_buffer *buf, int cache_ref)
Chris Mason02217ed2007-03-02 16:08:05 -05001025{
Chris Masondb945352007-10-15 16:15:53 -04001026 u64 bytenr;
Chris Mason5f39d392007-10-15 16:14:19 -04001027 u32 nritems;
1028 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -04001029 struct btrfs_file_extent_item *fi;
Chris Mason02217ed2007-03-02 16:08:05 -05001030 int i;
Chris Masondb945352007-10-15 16:15:53 -04001031 int level;
Chris Mason6407bf62007-03-27 06:33:00 -04001032 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -04001033 int faili;
Yan Zheng31153d82008-07-28 15:32:19 -04001034 int nr_file_extents = 0;
Chris Masona28ec192007-03-06 20:08:01 -05001035
Chris Mason3768f362007-03-13 16:47:54 -04001036 if (!root->ref_cows)
Chris Masona28ec192007-03-06 20:08:01 -05001037 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001038
Chris Masondb945352007-10-15 16:15:53 -04001039 level = btrfs_header_level(buf);
Chris Mason5f39d392007-10-15 16:14:19 -04001040 nritems = btrfs_header_nritems(buf);
1041 for (i = 0; i < nritems; i++) {
Chris Masone34a5b42008-07-22 12:08:37 -04001042 cond_resched();
Chris Masondb945352007-10-15 16:15:53 -04001043 if (level == 0) {
1044 u64 disk_bytenr;
Chris Mason5f39d392007-10-15 16:14:19 -04001045 btrfs_item_key_to_cpu(buf, &key, i);
1046 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason6407bf62007-03-27 06:33:00 -04001047 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04001048 fi = btrfs_item_ptr(buf, i,
Chris Mason6407bf62007-03-27 06:33:00 -04001049 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001050 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason236454d2007-04-19 13:37:44 -04001051 BTRFS_FILE_EXTENT_INLINE)
1052 continue;
Chris Masondb945352007-10-15 16:15:53 -04001053 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1054 if (disk_bytenr == 0)
Chris Mason3a686372007-05-24 13:35:57 -04001055 continue;
Chris Mason4a096752008-07-21 10:29:44 -04001056
Yan Zheng31153d82008-07-28 15:32:19 -04001057 if (buf != root->commit_root)
1058 nr_file_extents++;
1059
Chris Mason4a096752008-07-21 10:29:44 -04001060 mutex_lock(&root->fs_info->alloc_mutex);
Chris Mason925baed2008-06-25 16:01:30 -04001061 ret = __btrfs_inc_extent_ref(trans, root, disk_bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -05001062 btrfs_file_extent_disk_num_bytes(buf, fi),
1063 root->root_key.objectid, trans->transid,
1064 key.objectid, key.offset);
Chris Mason4a096752008-07-21 10:29:44 -04001065 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Mason54aa1f42007-06-22 14:16:25 -04001066 if (ret) {
1067 faili = i;
Chris Mason4a096752008-07-21 10:29:44 -04001068 WARN_ON(1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001069 goto fail;
1070 }
Chris Mason6407bf62007-03-27 06:33:00 -04001071 } else {
Chris Masondb945352007-10-15 16:15:53 -04001072 bytenr = btrfs_node_blockptr(buf, i);
Chris Mason6caab482007-12-13 09:48:07 -05001073 btrfs_node_key_to_cpu(buf, &key, i);
Chris Mason4a096752008-07-21 10:29:44 -04001074
1075 mutex_lock(&root->fs_info->alloc_mutex);
Chris Mason925baed2008-06-25 16:01:30 -04001076 ret = __btrfs_inc_extent_ref(trans, root, bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -05001077 btrfs_level_size(root, level - 1),
1078 root->root_key.objectid,
Chris Masonf6dbff52007-12-13 11:13:32 -05001079 trans->transid,
1080 level - 1, key.objectid);
Chris Mason4a096752008-07-21 10:29:44 -04001081 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Mason54aa1f42007-06-22 14:16:25 -04001082 if (ret) {
1083 faili = i;
Chris Mason4a096752008-07-21 10:29:44 -04001084 WARN_ON(1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001085 goto fail;
1086 }
Chris Mason6407bf62007-03-27 06:33:00 -04001087 }
Chris Mason02217ed2007-03-02 16:08:05 -05001088 }
Yan Zheng31153d82008-07-28 15:32:19 -04001089 /* cache orignal leaf block's references */
1090 if (level == 0 && cache_ref && buf != root->commit_root) {
1091 struct btrfs_leaf_ref *ref;
1092 struct btrfs_extent_info *info;
1093
Yanbcc63ab2008-07-30 16:29:20 -04001094 ref = btrfs_alloc_leaf_ref(root, nr_file_extents);
Yan Zheng31153d82008-07-28 15:32:19 -04001095 if (!ref) {
1096 WARN_ON(1);
1097 goto out;
1098 }
1099
Chris Mason47ac14f2008-07-31 09:46:18 -04001100 ref->root_gen = root->root_key.offset;
Yan Zheng31153d82008-07-28 15:32:19 -04001101 ref->bytenr = buf->start;
1102 ref->owner = btrfs_header_owner(buf);
1103 ref->generation = btrfs_header_generation(buf);
1104 ref->nritems = nr_file_extents;
1105 info = ref->extents;
Yanbcc63ab2008-07-30 16:29:20 -04001106
Yan Zheng31153d82008-07-28 15:32:19 -04001107 for (i = 0; nr_file_extents > 0 && i < nritems; i++) {
1108 u64 disk_bytenr;
1109 btrfs_item_key_to_cpu(buf, &key, i);
1110 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1111 continue;
1112 fi = btrfs_item_ptr(buf, i,
1113 struct btrfs_file_extent_item);
1114 if (btrfs_file_extent_type(buf, fi) ==
1115 BTRFS_FILE_EXTENT_INLINE)
1116 continue;
1117 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1118 if (disk_bytenr == 0)
1119 continue;
1120
1121 info->bytenr = disk_bytenr;
1122 info->num_bytes =
1123 btrfs_file_extent_disk_num_bytes(buf, fi);
1124 info->objectid = key.objectid;
1125 info->offset = key.offset;
1126 info++;
1127 }
1128
1129 BUG_ON(!root->ref_tree);
1130 ret = btrfs_add_leaf_ref(root, ref);
1131 WARN_ON(ret);
Yanbcc63ab2008-07-30 16:29:20 -04001132 btrfs_free_leaf_ref(root, ref);
Yan Zheng31153d82008-07-28 15:32:19 -04001133 }
1134out:
Chris Mason02217ed2007-03-02 16:08:05 -05001135 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04001136fail:
Chris Masonccd467d2007-06-28 15:57:36 -04001137 WARN_ON(1);
Chris Mason7bb86312007-12-11 09:25:06 -05001138#if 0
Chris Mason54aa1f42007-06-22 14:16:25 -04001139 for (i =0; i < faili; i++) {
Chris Masondb945352007-10-15 16:15:53 -04001140 if (level == 0) {
1141 u64 disk_bytenr;
Chris Mason5f39d392007-10-15 16:14:19 -04001142 btrfs_item_key_to_cpu(buf, &key, i);
1143 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -04001144 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04001145 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -04001146 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001147 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -04001148 BTRFS_FILE_EXTENT_INLINE)
1149 continue;
Chris Masondb945352007-10-15 16:15:53 -04001150 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1151 if (disk_bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -04001152 continue;
Chris Masondb945352007-10-15 16:15:53 -04001153 err = btrfs_free_extent(trans, root, disk_bytenr,
1154 btrfs_file_extent_disk_num_bytes(buf,
Chris Mason5f39d392007-10-15 16:14:19 -04001155 fi), 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001156 BUG_ON(err);
1157 } else {
Chris Masondb945352007-10-15 16:15:53 -04001158 bytenr = btrfs_node_blockptr(buf, i);
1159 err = btrfs_free_extent(trans, root, bytenr,
1160 btrfs_level_size(root, level - 1), 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001161 BUG_ON(err);
1162 }
1163 }
Chris Mason7bb86312007-12-11 09:25:06 -05001164#endif
Chris Mason54aa1f42007-06-22 14:16:25 -04001165 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05001166}
1167
Chris Mason9078a3e2007-04-26 16:46:15 -04001168static int write_one_cache_group(struct btrfs_trans_handle *trans,
1169 struct btrfs_root *root,
1170 struct btrfs_path *path,
1171 struct btrfs_block_group_cache *cache)
1172{
1173 int ret;
1174 int pending_ret;
1175 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04001176 unsigned long bi;
1177 struct extent_buffer *leaf;
Chris Mason9078a3e2007-04-26 16:46:15 -04001178
Chris Mason9078a3e2007-04-26 16:46:15 -04001179 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001180 if (ret < 0)
1181 goto fail;
Chris Mason9078a3e2007-04-26 16:46:15 -04001182 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04001183
1184 leaf = path->nodes[0];
1185 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
1186 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
1187 btrfs_mark_buffer_dirty(leaf);
Chris Mason9078a3e2007-04-26 16:46:15 -04001188 btrfs_release_path(extent_root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -04001189fail:
Chris Mason9078a3e2007-04-26 16:46:15 -04001190 finish_current_insert(trans, extent_root);
1191 pending_ret = del_pending_extents(trans, extent_root);
1192 if (ret)
1193 return ret;
1194 if (pending_ret)
1195 return pending_ret;
1196 return 0;
1197
1198}
1199
Chris Mason96b51792007-10-15 16:15:19 -04001200int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
1201 struct btrfs_root *root)
Chris Mason9078a3e2007-04-26 16:46:15 -04001202{
Josef Bacik0f9dd462008-09-23 13:14:11 -04001203 struct btrfs_block_group_cache *cache, *entry;
1204 struct rb_node *n;
Chris Mason9078a3e2007-04-26 16:46:15 -04001205 int err = 0;
1206 int werr = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04001207 struct btrfs_path *path;
Chris Mason96b51792007-10-15 16:15:19 -04001208 u64 last = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04001209
1210 path = btrfs_alloc_path();
1211 if (!path)
1212 return -ENOMEM;
1213
Chris Mason925baed2008-06-25 16:01:30 -04001214 mutex_lock(&root->fs_info->alloc_mutex);
Chris Mason9078a3e2007-04-26 16:46:15 -04001215 while(1) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001216 cache = NULL;
1217 spin_lock(&root->fs_info->block_group_cache_lock);
1218 for (n = rb_first(&root->fs_info->block_group_cache_tree);
1219 n; n = rb_next(n)) {
1220 entry = rb_entry(n, struct btrfs_block_group_cache,
1221 cache_node);
1222 if (entry->dirty) {
1223 cache = entry;
1224 break;
1225 }
1226 }
1227 spin_unlock(&root->fs_info->block_group_cache_lock);
1228
1229 if (!cache)
Chris Mason9078a3e2007-04-26 16:46:15 -04001230 break;
Chris Mason54aa1f42007-06-22 14:16:25 -04001231
Josef Bacik0f9dd462008-09-23 13:14:11 -04001232 last += cache->key.offset;
1233
Chris Mason96b51792007-10-15 16:15:19 -04001234 err = write_one_cache_group(trans, root,
1235 path, cache);
1236 /*
1237 * if we fail to write the cache group, we want
1238 * to keep it marked dirty in hopes that a later
1239 * write will work
1240 */
1241 if (err) {
1242 werr = err;
1243 continue;
Chris Mason9078a3e2007-04-26 16:46:15 -04001244 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04001245
1246 cache->dirty = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04001247 }
1248 btrfs_free_path(path);
Chris Mason925baed2008-06-25 16:01:30 -04001249 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Mason9078a3e2007-04-26 16:46:15 -04001250 return werr;
1251}
1252
Chris Mason593060d2008-03-25 16:50:33 -04001253static int update_space_info(struct btrfs_fs_info *info, u64 flags,
1254 u64 total_bytes, u64 bytes_used,
1255 struct btrfs_space_info **space_info)
1256{
1257 struct btrfs_space_info *found;
1258
1259 found = __find_space_info(info, flags);
1260 if (found) {
1261 found->total_bytes += total_bytes;
1262 found->bytes_used += bytes_used;
Chris Mason8f18cf12008-04-25 16:53:30 -04001263 found->full = 0;
Chris Mason593060d2008-03-25 16:50:33 -04001264 *space_info = found;
1265 return 0;
1266 }
1267 found = kmalloc(sizeof(*found), GFP_NOFS);
1268 if (!found)
1269 return -ENOMEM;
1270
1271 list_add(&found->list, &info->space_info);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001272 INIT_LIST_HEAD(&found->block_groups);
1273 spin_lock_init(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04001274 found->flags = flags;
1275 found->total_bytes = total_bytes;
1276 found->bytes_used = bytes_used;
1277 found->bytes_pinned = 0;
1278 found->full = 0;
Chris Mason0ef3e662008-05-24 14:04:53 -04001279 found->force_alloc = 0;
Chris Mason593060d2008-03-25 16:50:33 -04001280 *space_info = found;
1281 return 0;
1282}
1283
Chris Mason8790d502008-04-03 16:29:03 -04001284static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
1285{
1286 u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
Chris Mason611f0e02008-04-03 16:29:03 -04001287 BTRFS_BLOCK_GROUP_RAID1 |
Chris Mason321aecc2008-04-16 10:49:51 -04001288 BTRFS_BLOCK_GROUP_RAID10 |
Chris Mason611f0e02008-04-03 16:29:03 -04001289 BTRFS_BLOCK_GROUP_DUP);
Chris Mason8790d502008-04-03 16:29:03 -04001290 if (extra_flags) {
1291 if (flags & BTRFS_BLOCK_GROUP_DATA)
1292 fs_info->avail_data_alloc_bits |= extra_flags;
1293 if (flags & BTRFS_BLOCK_GROUP_METADATA)
1294 fs_info->avail_metadata_alloc_bits |= extra_flags;
1295 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
1296 fs_info->avail_system_alloc_bits |= extra_flags;
1297 }
1298}
Chris Mason593060d2008-03-25 16:50:33 -04001299
Chris Masona061fc82008-05-07 11:43:44 -04001300static u64 reduce_alloc_profile(struct btrfs_root *root, u64 flags)
Chris Masonec44a352008-04-28 15:29:52 -04001301{
Chris Masona061fc82008-05-07 11:43:44 -04001302 u64 num_devices = root->fs_info->fs_devices->num_devices;
1303
1304 if (num_devices == 1)
1305 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
1306 if (num_devices < 4)
1307 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
1308
Chris Masonec44a352008-04-28 15:29:52 -04001309 if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
1310 (flags & (BTRFS_BLOCK_GROUP_RAID1 |
Chris Masona061fc82008-05-07 11:43:44 -04001311 BTRFS_BLOCK_GROUP_RAID10))) {
Chris Masonec44a352008-04-28 15:29:52 -04001312 flags &= ~BTRFS_BLOCK_GROUP_DUP;
Chris Masona061fc82008-05-07 11:43:44 -04001313 }
Chris Masonec44a352008-04-28 15:29:52 -04001314
1315 if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
Chris Masona061fc82008-05-07 11:43:44 -04001316 (flags & BTRFS_BLOCK_GROUP_RAID10)) {
Chris Masonec44a352008-04-28 15:29:52 -04001317 flags &= ~BTRFS_BLOCK_GROUP_RAID1;
Chris Masona061fc82008-05-07 11:43:44 -04001318 }
Chris Masonec44a352008-04-28 15:29:52 -04001319
1320 if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
1321 ((flags & BTRFS_BLOCK_GROUP_RAID1) |
1322 (flags & BTRFS_BLOCK_GROUP_RAID10) |
1323 (flags & BTRFS_BLOCK_GROUP_DUP)))
1324 flags &= ~BTRFS_BLOCK_GROUP_RAID0;
1325 return flags;
1326}
1327
Chris Mason6324fbf2008-03-24 15:01:59 -04001328static int do_chunk_alloc(struct btrfs_trans_handle *trans,
1329 struct btrfs_root *extent_root, u64 alloc_bytes,
Chris Mason0ef3e662008-05-24 14:04:53 -04001330 u64 flags, int force)
Chris Mason6324fbf2008-03-24 15:01:59 -04001331{
1332 struct btrfs_space_info *space_info;
1333 u64 thresh;
1334 u64 start;
1335 u64 num_bytes;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001336 int ret = 0;
Chris Mason6324fbf2008-03-24 15:01:59 -04001337
Chris Masona061fc82008-05-07 11:43:44 -04001338 flags = reduce_alloc_profile(extent_root, flags);
Chris Masonec44a352008-04-28 15:29:52 -04001339
Chris Mason6324fbf2008-03-24 15:01:59 -04001340 space_info = __find_space_info(extent_root->fs_info, flags);
Chris Mason593060d2008-03-25 16:50:33 -04001341 if (!space_info) {
1342 ret = update_space_info(extent_root->fs_info, flags,
1343 0, 0, &space_info);
1344 BUG_ON(ret);
1345 }
Chris Mason6324fbf2008-03-24 15:01:59 -04001346 BUG_ON(!space_info);
1347
Chris Mason0ef3e662008-05-24 14:04:53 -04001348 if (space_info->force_alloc) {
1349 force = 1;
1350 space_info->force_alloc = 0;
1351 }
Chris Mason6324fbf2008-03-24 15:01:59 -04001352 if (space_info->full)
Chris Mason925baed2008-06-25 16:01:30 -04001353 goto out;
Chris Mason6324fbf2008-03-24 15:01:59 -04001354
Chris Mason8790d502008-04-03 16:29:03 -04001355 thresh = div_factor(space_info->total_bytes, 6);
Chris Mason0ef3e662008-05-24 14:04:53 -04001356 if (!force &&
1357 (space_info->bytes_used + space_info->bytes_pinned + alloc_bytes) <
Chris Mason6324fbf2008-03-24 15:01:59 -04001358 thresh)
Chris Mason925baed2008-06-25 16:01:30 -04001359 goto out;
Chris Mason6324fbf2008-03-24 15:01:59 -04001360
Chris Mason925baed2008-06-25 16:01:30 -04001361 mutex_lock(&extent_root->fs_info->chunk_mutex);
Chris Mason6324fbf2008-03-24 15:01:59 -04001362 ret = btrfs_alloc_chunk(trans, extent_root, &start, &num_bytes, flags);
1363 if (ret == -ENOSPC) {
1364printk("space info full %Lu\n", flags);
1365 space_info->full = 1;
Chris Masona74a4b92008-06-25 16:01:31 -04001366 goto out_unlock;
Chris Mason6324fbf2008-03-24 15:01:59 -04001367 }
Chris Mason6324fbf2008-03-24 15:01:59 -04001368 BUG_ON(ret);
1369
1370 ret = btrfs_make_block_group(trans, extent_root, 0, flags,
Chris Masone17cade2008-04-15 15:41:47 -04001371 BTRFS_FIRST_CHUNK_TREE_OBJECTID, start, num_bytes);
Chris Mason6324fbf2008-03-24 15:01:59 -04001372 BUG_ON(ret);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001373
Chris Masona74a4b92008-06-25 16:01:31 -04001374out_unlock:
Chris Mason333db942008-06-25 16:01:30 -04001375 mutex_unlock(&extent_root->fs_info->chunk_mutex);
Chris Masona74a4b92008-06-25 16:01:31 -04001376out:
Josef Bacik0f9dd462008-09-23 13:14:11 -04001377 return ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04001378}
1379
Chris Mason9078a3e2007-04-26 16:46:15 -04001380static int update_block_group(struct btrfs_trans_handle *trans,
1381 struct btrfs_root *root,
Chris Masondb945352007-10-15 16:15:53 -04001382 u64 bytenr, u64 num_bytes, int alloc,
Chris Mason0b86a832008-03-24 15:01:56 -04001383 int mark_free)
Chris Mason9078a3e2007-04-26 16:46:15 -04001384{
1385 struct btrfs_block_group_cache *cache;
1386 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -04001387 u64 total = num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04001388 u64 old_val;
Chris Masondb945352007-10-15 16:15:53 -04001389 u64 byte_in_group;
Chris Mason3e1ad542007-05-07 20:03:49 -04001390
Chris Mason7d9eb122008-07-08 14:19:17 -04001391 WARN_ON(!mutex_is_locked(&root->fs_info->alloc_mutex));
Chris Mason9078a3e2007-04-26 16:46:15 -04001392 while(total) {
Chris Masondb945352007-10-15 16:15:53 -04001393 cache = btrfs_lookup_block_group(info, bytenr);
Chris Mason3e1ad542007-05-07 20:03:49 -04001394 if (!cache) {
Chris Mason9078a3e2007-04-26 16:46:15 -04001395 return -1;
Chris Masoncd1bc462007-04-27 10:08:34 -04001396 }
Chris Masondb945352007-10-15 16:15:53 -04001397 byte_in_group = bytenr - cache->key.objectid;
1398 WARN_ON(byte_in_group > cache->key.offset);
Chris Mason9078a3e2007-04-26 16:46:15 -04001399
Chris Masonc286ac42008-07-22 23:06:41 -04001400 spin_lock(&cache->lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001401 cache->dirty = 1;
Chris Mason9078a3e2007-04-26 16:46:15 -04001402 old_val = btrfs_block_group_used(&cache->item);
Chris Masondb945352007-10-15 16:15:53 -04001403 num_bytes = min(total, cache->key.offset - byte_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -04001404 if (alloc) {
Chris Masondb945352007-10-15 16:15:53 -04001405 old_val += num_bytes;
Chris Mason6324fbf2008-03-24 15:01:59 -04001406 cache->space_info->bytes_used += num_bytes;
Chris Masonc286ac42008-07-22 23:06:41 -04001407 btrfs_set_block_group_used(&cache->item, old_val);
1408 spin_unlock(&cache->lock);
Chris Masoncd1bc462007-04-27 10:08:34 -04001409 } else {
Chris Masondb945352007-10-15 16:15:53 -04001410 old_val -= num_bytes;
Chris Mason6324fbf2008-03-24 15:01:59 -04001411 cache->space_info->bytes_used -= num_bytes;
Chris Masonc286ac42008-07-22 23:06:41 -04001412 btrfs_set_block_group_used(&cache->item, old_val);
1413 spin_unlock(&cache->lock);
Chris Masonf510cfe2007-10-15 16:14:48 -04001414 if (mark_free) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001415 int ret;
1416 ret = btrfs_add_free_space(cache, bytenr,
1417 num_bytes);
1418 if (ret)
1419 return -1;
Chris Masone37c9e62007-05-09 20:13:14 -04001420 }
Chris Masoncd1bc462007-04-27 10:08:34 -04001421 }
Chris Masondb945352007-10-15 16:15:53 -04001422 total -= num_bytes;
1423 bytenr += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04001424 }
1425 return 0;
1426}
Chris Mason6324fbf2008-03-24 15:01:59 -04001427
Chris Masona061fc82008-05-07 11:43:44 -04001428static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
1429{
Josef Bacik0f9dd462008-09-23 13:14:11 -04001430 struct btrfs_block_group_cache *cache;
1431
1432 cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
1433 if (!cache)
Chris Masona061fc82008-05-07 11:43:44 -04001434 return 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001435
1436 return cache->key.objectid;
Chris Masona061fc82008-05-07 11:43:44 -04001437}
1438
1439
Chris Masone02119d2008-09-05 16:13:11 -04001440int btrfs_update_pinned_extents(struct btrfs_root *root,
Yan324ae4d2007-11-16 14:57:08 -05001441 u64 bytenr, u64 num, int pin)
1442{
1443 u64 len;
1444 struct btrfs_block_group_cache *cache;
1445 struct btrfs_fs_info *fs_info = root->fs_info;
1446
Chris Mason7d9eb122008-07-08 14:19:17 -04001447 WARN_ON(!mutex_is_locked(&root->fs_info->alloc_mutex));
Yan324ae4d2007-11-16 14:57:08 -05001448 if (pin) {
1449 set_extent_dirty(&fs_info->pinned_extents,
1450 bytenr, bytenr + num - 1, GFP_NOFS);
1451 } else {
1452 clear_extent_dirty(&fs_info->pinned_extents,
1453 bytenr, bytenr + num - 1, GFP_NOFS);
1454 }
1455 while (num > 0) {
1456 cache = btrfs_lookup_block_group(fs_info, bytenr);
Chris Masona061fc82008-05-07 11:43:44 -04001457 if (!cache) {
1458 u64 first = first_logical_byte(root, bytenr);
1459 WARN_ON(first < bytenr);
1460 len = min(first - bytenr, num);
1461 } else {
1462 len = min(num, cache->key.offset -
1463 (bytenr - cache->key.objectid));
1464 }
Yan324ae4d2007-11-16 14:57:08 -05001465 if (pin) {
Chris Masona061fc82008-05-07 11:43:44 -04001466 if (cache) {
Chris Masonc286ac42008-07-22 23:06:41 -04001467 spin_lock(&cache->lock);
Chris Masona061fc82008-05-07 11:43:44 -04001468 cache->pinned += len;
1469 cache->space_info->bytes_pinned += len;
Chris Masonc286ac42008-07-22 23:06:41 -04001470 spin_unlock(&cache->lock);
Chris Masona061fc82008-05-07 11:43:44 -04001471 }
Yan324ae4d2007-11-16 14:57:08 -05001472 fs_info->total_pinned += len;
1473 } else {
Chris Masona061fc82008-05-07 11:43:44 -04001474 if (cache) {
Chris Masonc286ac42008-07-22 23:06:41 -04001475 spin_lock(&cache->lock);
Chris Masona061fc82008-05-07 11:43:44 -04001476 cache->pinned -= len;
1477 cache->space_info->bytes_pinned -= len;
Chris Masonc286ac42008-07-22 23:06:41 -04001478 spin_unlock(&cache->lock);
Chris Masona061fc82008-05-07 11:43:44 -04001479 }
Yan324ae4d2007-11-16 14:57:08 -05001480 fs_info->total_pinned -= len;
1481 }
1482 bytenr += len;
1483 num -= len;
1484 }
1485 return 0;
1486}
Chris Mason9078a3e2007-04-26 16:46:15 -04001487
Chris Masond1310b22008-01-24 16:13:08 -05001488int btrfs_copy_pinned(struct btrfs_root *root, struct extent_io_tree *copy)
Chris Masonccd467d2007-06-28 15:57:36 -04001489{
Chris Masonccd467d2007-06-28 15:57:36 -04001490 u64 last = 0;
Chris Mason1a5bc162007-10-15 16:15:26 -04001491 u64 start;
1492 u64 end;
Chris Masond1310b22008-01-24 16:13:08 -05001493 struct extent_io_tree *pinned_extents = &root->fs_info->pinned_extents;
Chris Masonccd467d2007-06-28 15:57:36 -04001494 int ret;
Chris Masonccd467d2007-06-28 15:57:36 -04001495
1496 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04001497 ret = find_first_extent_bit(pinned_extents, last,
1498 &start, &end, EXTENT_DIRTY);
1499 if (ret)
Chris Masonccd467d2007-06-28 15:57:36 -04001500 break;
Chris Mason1a5bc162007-10-15 16:15:26 -04001501 set_extent_dirty(copy, start, end, GFP_NOFS);
1502 last = end + 1;
Chris Masonccd467d2007-06-28 15:57:36 -04001503 }
1504 return 0;
1505}
1506
1507int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
1508 struct btrfs_root *root,
Chris Masond1310b22008-01-24 16:13:08 -05001509 struct extent_io_tree *unpin)
Chris Masona28ec192007-03-06 20:08:01 -05001510{
Chris Mason1a5bc162007-10-15 16:15:26 -04001511 u64 start;
1512 u64 end;
Chris Masona28ec192007-03-06 20:08:01 -05001513 int ret;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001514 struct btrfs_block_group_cache *cache;
Chris Masona28ec192007-03-06 20:08:01 -05001515
Chris Mason925baed2008-06-25 16:01:30 -04001516 mutex_lock(&root->fs_info->alloc_mutex);
Chris Masona28ec192007-03-06 20:08:01 -05001517 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04001518 ret = find_first_extent_bit(unpin, 0, &start, &end,
1519 EXTENT_DIRTY);
1520 if (ret)
Chris Masona28ec192007-03-06 20:08:01 -05001521 break;
Chris Masone02119d2008-09-05 16:13:11 -04001522 btrfs_update_pinned_extents(root, start, end + 1 - start, 0);
Chris Mason1a5bc162007-10-15 16:15:26 -04001523 clear_extent_dirty(unpin, start, end, GFP_NOFS);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001524 cache = btrfs_lookup_block_group(root->fs_info, start);
1525 if (cache->cached)
1526 btrfs_add_free_space(cache, start, end - start + 1);
Chris Masonc286ac42008-07-22 23:06:41 -04001527 if (need_resched()) {
1528 mutex_unlock(&root->fs_info->alloc_mutex);
1529 cond_resched();
1530 mutex_lock(&root->fs_info->alloc_mutex);
1531 }
Chris Masona28ec192007-03-06 20:08:01 -05001532 }
Chris Mason925baed2008-06-25 16:01:30 -04001533 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Masona28ec192007-03-06 20:08:01 -05001534 return 0;
1535}
1536
Chris Mason98ed5172008-01-03 10:01:48 -05001537static int finish_current_insert(struct btrfs_trans_handle *trans,
1538 struct btrfs_root *extent_root)
Chris Mason037e6392007-03-07 11:50:24 -05001539{
Chris Mason7bb86312007-12-11 09:25:06 -05001540 u64 start;
1541 u64 end;
1542 struct btrfs_fs_info *info = extent_root->fs_info;
Chris Masond8d5f3e2007-12-11 12:42:00 -05001543 struct extent_buffer *eb;
Chris Mason7bb86312007-12-11 09:25:06 -05001544 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -04001545 struct btrfs_key ins;
Chris Masond8d5f3e2007-12-11 12:42:00 -05001546 struct btrfs_disk_key first;
Chris Mason234b63a2007-03-13 10:46:10 -04001547 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -05001548 int ret;
Chris Masond8d5f3e2007-12-11 12:42:00 -05001549 int level;
Chris Mason1a5bc162007-10-15 16:15:26 -04001550 int err = 0;
Chris Mason037e6392007-03-07 11:50:24 -05001551
Chris Mason7d9eb122008-07-08 14:19:17 -04001552 WARN_ON(!mutex_is_locked(&extent_root->fs_info->alloc_mutex));
Chris Mason5f39d392007-10-15 16:14:19 -04001553 btrfs_set_stack_extent_refs(&extent_item, 1);
Chris Mason62e27492007-03-15 12:56:47 -04001554 btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
Chris Mason7bb86312007-12-11 09:25:06 -05001555 path = btrfs_alloc_path();
Chris Mason037e6392007-03-07 11:50:24 -05001556
Chris Mason26b80032007-08-08 20:17:12 -04001557 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04001558 ret = find_first_extent_bit(&info->extent_ins, 0, &start,
1559 &end, EXTENT_LOCKED);
1560 if (ret)
Chris Mason26b80032007-08-08 20:17:12 -04001561 break;
1562
Chris Mason1a5bc162007-10-15 16:15:26 -04001563 ins.objectid = start;
1564 ins.offset = end + 1 - start;
1565 err = btrfs_insert_item(trans, extent_root, &ins,
1566 &extent_item, sizeof(extent_item));
1567 clear_extent_bits(&info->extent_ins, start, end, EXTENT_LOCKED,
1568 GFP_NOFS);
Chris Masonc286ac42008-07-22 23:06:41 -04001569
Chris Masone02119d2008-09-05 16:13:11 -04001570 eb = btrfs_find_create_tree_block(extent_root, ins.objectid,
Chris Masonc286ac42008-07-22 23:06:41 -04001571 ins.offset);
1572
Chris Masone02119d2008-09-05 16:13:11 -04001573 if (!btrfs_buffer_uptodate(eb, trans->transid))
Chris Masonc286ac42008-07-22 23:06:41 -04001574 btrfs_read_buffer(eb, trans->transid);
Chris Masonc286ac42008-07-22 23:06:41 -04001575
Chris Mason925baed2008-06-25 16:01:30 -04001576 btrfs_tree_lock(eb);
Chris Masond8d5f3e2007-12-11 12:42:00 -05001577 level = btrfs_header_level(eb);
1578 if (level == 0) {
1579 btrfs_item_key(eb, &first, 0);
1580 } else {
1581 btrfs_node_key(eb, &first, 0);
1582 }
Chris Mason925baed2008-06-25 16:01:30 -04001583 btrfs_tree_unlock(eb);
1584 free_extent_buffer(eb);
1585 /*
1586 * the first key is just a hint, so the race we've created
1587 * against reading it is fine
1588 */
Chris Mason7bb86312007-12-11 09:25:06 -05001589 err = btrfs_insert_extent_backref(trans, extent_root, path,
1590 start, extent_root->root_key.objectid,
Chris Masonf6dbff52007-12-13 11:13:32 -05001591 0, level,
1592 btrfs_disk_key_objectid(&first));
Chris Mason7bb86312007-12-11 09:25:06 -05001593 BUG_ON(err);
Chris Masonc286ac42008-07-22 23:06:41 -04001594 if (need_resched()) {
1595 mutex_unlock(&extent_root->fs_info->alloc_mutex);
1596 cond_resched();
1597 mutex_lock(&extent_root->fs_info->alloc_mutex);
1598 }
Chris Mason037e6392007-03-07 11:50:24 -05001599 }
Chris Mason7bb86312007-12-11 09:25:06 -05001600 btrfs_free_path(path);
Chris Mason037e6392007-03-07 11:50:24 -05001601 return 0;
1602}
1603
Chris Masondb945352007-10-15 16:15:53 -04001604static int pin_down_bytes(struct btrfs_root *root, u64 bytenr, u32 num_bytes,
Chris Mason4bef0842008-09-08 11:18:08 -04001605 int is_data, int pending)
Chris Masone20d96d2007-03-22 12:13:20 -04001606{
Chris Mason1a5bc162007-10-15 16:15:26 -04001607 int err = 0;
Chris Mason78fae272007-03-25 11:35:08 -04001608
Chris Mason7d9eb122008-07-08 14:19:17 -04001609 WARN_ON(!mutex_is_locked(&root->fs_info->alloc_mutex));
Chris Masonf4b9aa82007-03-27 11:05:53 -04001610 if (!pending) {
Chris Mason925baed2008-06-25 16:01:30 -04001611 struct extent_buffer *buf;
Chris Mason4bef0842008-09-08 11:18:08 -04001612
1613 if (is_data)
1614 goto pinit;
1615
Chris Masondb945352007-10-15 16:15:53 -04001616 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
Chris Mason5f39d392007-10-15 16:14:19 -04001617 if (buf) {
Chris Masone02119d2008-09-05 16:13:11 -04001618 /* we can reuse a block if it hasn't been written
1619 * and it is from this transaction. We can't
1620 * reuse anything from the tree log root because
1621 * it has tiny sub-transactions.
1622 */
Yan974e35a2008-07-24 12:18:16 -04001623 if (btrfs_buffer_uptodate(buf, 0) &&
1624 btrfs_try_tree_lock(buf)) {
Chris Mason2c90e5d2007-04-02 10:50:19 -04001625 u64 transid =
1626 root->fs_info->running_transaction->transid;
Chris Masondc17ff82008-01-08 15:46:30 -05001627 u64 header_transid =
1628 btrfs_header_generation(buf);
Chris Masone02119d2008-09-05 16:13:11 -04001629 if (btrfs_header_owner(buf) !=
1630 BTRFS_TREE_LOG_OBJECTID &&
1631 header_transid == transid &&
Chris Mason6bc34672008-04-04 15:40:00 -04001632 !btrfs_header_flag(buf,
1633 BTRFS_HEADER_FLAG_WRITTEN)) {
Chris Mason55c69072008-01-09 15:55:33 -05001634 clean_tree_block(NULL, root, buf);
Chris Mason925baed2008-06-25 16:01:30 -04001635 btrfs_tree_unlock(buf);
Chris Mason5f39d392007-10-15 16:14:19 -04001636 free_extent_buffer(buf);
Yanc5492282007-11-06 10:25:25 -05001637 return 1;
Chris Mason2c90e5d2007-04-02 10:50:19 -04001638 }
Chris Mason925baed2008-06-25 16:01:30 -04001639 btrfs_tree_unlock(buf);
Chris Masonf4b9aa82007-03-27 11:05:53 -04001640 }
Chris Mason5f39d392007-10-15 16:14:19 -04001641 free_extent_buffer(buf);
Chris Mason8ef97622007-03-26 10:15:30 -04001642 }
Chris Mason4bef0842008-09-08 11:18:08 -04001643pinit:
Chris Masone02119d2008-09-05 16:13:11 -04001644 btrfs_update_pinned_extents(root, bytenr, num_bytes, 1);
Chris Masonf4b9aa82007-03-27 11:05:53 -04001645 } else {
Chris Mason1a5bc162007-10-15 16:15:26 -04001646 set_extent_bits(&root->fs_info->pending_del,
Chris Masondb945352007-10-15 16:15:53 -04001647 bytenr, bytenr + num_bytes - 1,
1648 EXTENT_LOCKED, GFP_NOFS);
Chris Masonf4b9aa82007-03-27 11:05:53 -04001649 }
Chris Masonbe744172007-05-06 10:15:01 -04001650 BUG_ON(err < 0);
Chris Masone20d96d2007-03-22 12:13:20 -04001651 return 0;
1652}
1653
Chris Masona28ec192007-03-06 20:08:01 -05001654/*
1655 * remove an extent from the root, returns 0 on success
1656 */
Chris Masone089f052007-03-16 16:20:31 -04001657static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason7bb86312007-12-11 09:25:06 -05001658 *root, u64 bytenr, u64 num_bytes,
1659 u64 root_objectid, u64 ref_generation,
1660 u64 owner_objectid, u64 owner_offset, int pin,
Chris Masone37c9e62007-05-09 20:13:14 -04001661 int mark_free)
Chris Masona28ec192007-03-06 20:08:01 -05001662{
Chris Mason5caf2a02007-04-02 11:20:42 -04001663 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -04001664 struct btrfs_key key;
Chris Mason1261ec42007-03-20 20:35:03 -04001665 struct btrfs_fs_info *info = root->fs_info;
1666 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04001667 struct extent_buffer *leaf;
Chris Masona28ec192007-03-06 20:08:01 -05001668 int ret;
Chris Mason952fcca2008-02-18 16:33:44 -05001669 int extent_slot = 0;
1670 int found_extent = 0;
1671 int num_to_del = 1;
Chris Mason234b63a2007-03-13 10:46:10 -04001672 struct btrfs_extent_item *ei;
Chris Masoncf27e1e2007-03-13 09:49:06 -04001673 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -05001674
Chris Mason7d9eb122008-07-08 14:19:17 -04001675 WARN_ON(!mutex_is_locked(&root->fs_info->alloc_mutex));
Chris Masondb945352007-10-15 16:15:53 -04001676 key.objectid = bytenr;
Chris Mason62e27492007-03-15 12:56:47 -04001677 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masondb945352007-10-15 16:15:53 -04001678 key.offset = num_bytes;
Chris Mason5caf2a02007-04-02 11:20:42 -04001679 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04001680 if (!path)
1681 return -ENOMEM;
1682
Chris Mason3c12ac72008-04-21 12:01:38 -04001683 path->reada = 1;
Chris Mason7bb86312007-12-11 09:25:06 -05001684 ret = lookup_extent_backref(trans, extent_root, path,
1685 bytenr, root_objectid,
1686 ref_generation,
1687 owner_objectid, owner_offset, 1);
1688 if (ret == 0) {
Chris Mason952fcca2008-02-18 16:33:44 -05001689 struct btrfs_key found_key;
1690 extent_slot = path->slots[0];
1691 while(extent_slot > 0) {
1692 extent_slot--;
1693 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1694 extent_slot);
1695 if (found_key.objectid != bytenr)
1696 break;
1697 if (found_key.type == BTRFS_EXTENT_ITEM_KEY &&
1698 found_key.offset == num_bytes) {
1699 found_extent = 1;
1700 break;
1701 }
1702 if (path->slots[0] - extent_slot > 5)
1703 break;
1704 }
1705 if (!found_extent)
1706 ret = btrfs_del_item(trans, extent_root, path);
Chris Mason7bb86312007-12-11 09:25:06 -05001707 } else {
1708 btrfs_print_leaf(extent_root, path->nodes[0]);
1709 WARN_ON(1);
1710 printk("Unable to find ref byte nr %Lu root %Lu "
1711 " gen %Lu owner %Lu offset %Lu\n", bytenr,
1712 root_objectid, ref_generation, owner_objectid,
1713 owner_offset);
1714 }
Chris Mason952fcca2008-02-18 16:33:44 -05001715 if (!found_extent) {
1716 btrfs_release_path(extent_root, path);
1717 ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
1718 if (ret < 0)
1719 return ret;
1720 BUG_ON(ret);
1721 extent_slot = path->slots[0];
1722 }
Chris Mason5f39d392007-10-15 16:14:19 -04001723
1724 leaf = path->nodes[0];
Chris Mason952fcca2008-02-18 16:33:44 -05001725 ei = btrfs_item_ptr(leaf, extent_slot,
Chris Mason123abc82007-03-14 14:14:43 -04001726 struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001727 refs = btrfs_extent_refs(leaf, ei);
1728 BUG_ON(refs == 0);
1729 refs -= 1;
1730 btrfs_set_extent_refs(leaf, ei, refs);
Chris Mason952fcca2008-02-18 16:33:44 -05001731
Chris Mason5f39d392007-10-15 16:14:19 -04001732 btrfs_mark_buffer_dirty(leaf);
1733
Chris Mason952fcca2008-02-18 16:33:44 -05001734 if (refs == 0 && found_extent && path->slots[0] == extent_slot + 1) {
1735 /* if the back ref and the extent are next to each other
1736 * they get deleted below in one shot
1737 */
1738 path->slots[0] = extent_slot;
1739 num_to_del = 2;
1740 } else if (found_extent) {
1741 /* otherwise delete the extent back ref */
1742 ret = btrfs_del_item(trans, extent_root, path);
1743 BUG_ON(ret);
1744 /* if refs are 0, we need to setup the path for deletion */
1745 if (refs == 0) {
1746 btrfs_release_path(extent_root, path);
1747 ret = btrfs_search_slot(trans, extent_root, &key, path,
1748 -1, 1);
1749 if (ret < 0)
1750 return ret;
1751 BUG_ON(ret);
1752 }
1753 }
1754
Chris Masoncf27e1e2007-03-13 09:49:06 -04001755 if (refs == 0) {
Chris Masondb945352007-10-15 16:15:53 -04001756 u64 super_used;
1757 u64 root_used;
David Woodhouse21af8042008-08-12 14:13:26 +01001758#ifdef BIO_RW_DISCARD
1759 u64 map_length = num_bytes;
1760 struct btrfs_multi_bio *multi = NULL;
1761#endif
Chris Mason78fae272007-03-25 11:35:08 -04001762
1763 if (pin) {
Chris Mason4bef0842008-09-08 11:18:08 -04001764 ret = pin_down_bytes(root, bytenr, num_bytes,
1765 owner_objectid >= BTRFS_FIRST_FREE_OBJECTID, 0);
Yanc5492282007-11-06 10:25:25 -05001766 if (ret > 0)
1767 mark_free = 1;
1768 BUG_ON(ret < 0);
Chris Mason78fae272007-03-25 11:35:08 -04001769 }
1770
Josef Bacik58176a92007-08-29 15:47:34 -04001771 /* block accounting for super block */
Chris Masona2135012008-06-25 16:01:30 -04001772 spin_lock_irq(&info->delalloc_lock);
Chris Masondb945352007-10-15 16:15:53 -04001773 super_used = btrfs_super_bytes_used(&info->super_copy);
1774 btrfs_set_super_bytes_used(&info->super_copy,
1775 super_used - num_bytes);
Chris Masona2135012008-06-25 16:01:30 -04001776 spin_unlock_irq(&info->delalloc_lock);
Josef Bacik58176a92007-08-29 15:47:34 -04001777
1778 /* block accounting for root item */
Chris Masondb945352007-10-15 16:15:53 -04001779 root_used = btrfs_root_used(&root->root_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001780 btrfs_set_root_used(&root->root_item,
Chris Masondb945352007-10-15 16:15:53 -04001781 root_used - num_bytes);
Chris Mason952fcca2008-02-18 16:33:44 -05001782 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
1783 num_to_del);
Chris Mason54aa1f42007-06-22 14:16:25 -04001784 if (ret) {
1785 return ret;
1786 }
Chris Masondb945352007-10-15 16:15:53 -04001787 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
Chris Mason0b86a832008-03-24 15:01:56 -04001788 mark_free);
Chris Mason9078a3e2007-04-26 16:46:15 -04001789 BUG_ON(ret);
David Woodhouse21af8042008-08-12 14:13:26 +01001790
1791#ifdef BIO_RW_DISCARD
1792 /* Tell the block device(s) that the sectors can be discarded */
1793 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
1794 bytenr, &map_length, &multi, 0);
1795 if (!ret) {
1796 struct btrfs_bio_stripe *stripe = multi->stripes;
1797 int i;
1798
1799 if (map_length > num_bytes)
1800 map_length = num_bytes;
1801
1802 for (i = 0; i < multi->num_stripes; i++, stripe++) {
1803 blkdev_issue_discard(stripe->dev->bdev,
1804 stripe->physical >> 9,
1805 map_length >> 9);
1806 }
1807 kfree(multi);
1808 }
1809#endif
Chris Masona28ec192007-03-06 20:08:01 -05001810 }
Chris Mason5caf2a02007-04-02 11:20:42 -04001811 btrfs_free_path(path);
Chris Masone089f052007-03-16 16:20:31 -04001812 finish_current_insert(trans, extent_root);
Chris Masona28ec192007-03-06 20:08:01 -05001813 return ret;
1814}
1815
1816/*
Chris Masonfec577f2007-02-26 10:40:21 -05001817 * find all the blocks marked as pending in the radix tree and remove
1818 * them from the extent map
1819 */
Chris Masone089f052007-03-16 16:20:31 -04001820static int del_pending_extents(struct btrfs_trans_handle *trans, struct
1821 btrfs_root *extent_root)
Chris Masonfec577f2007-02-26 10:40:21 -05001822{
1823 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -04001824 int err = 0;
Chris Mason1a5bc162007-10-15 16:15:26 -04001825 u64 start;
1826 u64 end;
Chris Masond1310b22008-01-24 16:13:08 -05001827 struct extent_io_tree *pending_del;
1828 struct extent_io_tree *pinned_extents;
Chris Mason8ef97622007-03-26 10:15:30 -04001829
Chris Mason7d9eb122008-07-08 14:19:17 -04001830 WARN_ON(!mutex_is_locked(&extent_root->fs_info->alloc_mutex));
Chris Mason1a5bc162007-10-15 16:15:26 -04001831 pending_del = &extent_root->fs_info->pending_del;
1832 pinned_extents = &extent_root->fs_info->pinned_extents;
Chris Masonfec577f2007-02-26 10:40:21 -05001833
1834 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04001835 ret = find_first_extent_bit(pending_del, 0, &start, &end,
1836 EXTENT_LOCKED);
1837 if (ret)
Chris Masonfec577f2007-02-26 10:40:21 -05001838 break;
Chris Mason1a5bc162007-10-15 16:15:26 -04001839 clear_extent_bits(pending_del, start, end, EXTENT_LOCKED,
1840 GFP_NOFS);
Chris Masonc286ac42008-07-22 23:06:41 -04001841 if (!test_range_bit(&extent_root->fs_info->extent_ins,
1842 start, end, EXTENT_LOCKED, 0)) {
Chris Masone02119d2008-09-05 16:13:11 -04001843 btrfs_update_pinned_extents(extent_root, start,
Chris Masonc286ac42008-07-22 23:06:41 -04001844 end + 1 - start, 1);
1845 ret = __free_extent(trans, extent_root,
1846 start, end + 1 - start,
1847 extent_root->root_key.objectid,
1848 0, 0, 0, 0, 0);
1849 } else {
1850 clear_extent_bits(&extent_root->fs_info->extent_ins,
1851 start, end, EXTENT_LOCKED, GFP_NOFS);
1852 }
Chris Mason1a5bc162007-10-15 16:15:26 -04001853 if (ret)
1854 err = ret;
Chris Masonc286ac42008-07-22 23:06:41 -04001855
1856 if (need_resched()) {
1857 mutex_unlock(&extent_root->fs_info->alloc_mutex);
1858 cond_resched();
1859 mutex_lock(&extent_root->fs_info->alloc_mutex);
1860 }
Chris Masonfec577f2007-02-26 10:40:21 -05001861 }
Chris Masone20d96d2007-03-22 12:13:20 -04001862 return err;
Chris Masonfec577f2007-02-26 10:40:21 -05001863}
1864
1865/*
1866 * remove an extent from the root, returns 0 on success
1867 */
Chris Mason925baed2008-06-25 16:01:30 -04001868static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
1869 struct btrfs_root *root, u64 bytenr,
1870 u64 num_bytes, u64 root_objectid,
1871 u64 ref_generation, u64 owner_objectid,
1872 u64 owner_offset, int pin)
Chris Masonfec577f2007-02-26 10:40:21 -05001873{
Chris Mason9f5fae22007-03-20 14:38:32 -04001874 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Masonfec577f2007-02-26 10:40:21 -05001875 int pending_ret;
1876 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05001877
Chris Masondb945352007-10-15 16:15:53 -04001878 WARN_ON(num_bytes < root->sectorsize);
Chris Mason7bb86312007-12-11 09:25:06 -05001879 if (!root->ref_cows)
1880 ref_generation = 0;
1881
Chris Masona28ec192007-03-06 20:08:01 -05001882 if (root == extent_root) {
Chris Mason4bef0842008-09-08 11:18:08 -04001883 pin_down_bytes(root, bytenr, num_bytes, 0, 1);
Chris Masona28ec192007-03-06 20:08:01 -05001884 return 0;
1885 }
Chris Mason4bef0842008-09-08 11:18:08 -04001886 /* if metadata always pin */
Chris Masond00aff02008-09-11 15:54:42 -04001887 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
1888 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001889 struct btrfs_block_group_cache *cache;
1890
Chris Masond00aff02008-09-11 15:54:42 -04001891 /* btrfs_free_reserved_extent */
Josef Bacik0f9dd462008-09-23 13:14:11 -04001892 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
1893 BUG_ON(!cache);
1894 btrfs_add_free_space(cache, bytenr, num_bytes);
Chris Masond00aff02008-09-11 15:54:42 -04001895 return 0;
1896 }
Chris Mason4bef0842008-09-08 11:18:08 -04001897 pin = 1;
Chris Masond00aff02008-09-11 15:54:42 -04001898 }
Chris Mason4bef0842008-09-08 11:18:08 -04001899
1900 /* if data pin when any transaction has committed this */
1901 if (ref_generation != trans->transid)
1902 pin = 1;
1903
Chris Mason7bb86312007-12-11 09:25:06 -05001904 ret = __free_extent(trans, root, bytenr, num_bytes, root_objectid,
1905 ref_generation, owner_objectid, owner_offset,
1906 pin, pin == 0);
Chris Masonee6e6502008-07-17 12:54:40 -04001907
1908 finish_current_insert(trans, root->fs_info->extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -04001909 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
Chris Masonfec577f2007-02-26 10:40:21 -05001910 return ret ? ret : pending_ret;
1911}
1912
Chris Mason925baed2008-06-25 16:01:30 -04001913int btrfs_free_extent(struct btrfs_trans_handle *trans,
1914 struct btrfs_root *root, u64 bytenr,
1915 u64 num_bytes, u64 root_objectid,
1916 u64 ref_generation, u64 owner_objectid,
1917 u64 owner_offset, int pin)
1918{
1919 int ret;
1920
1921 maybe_lock_mutex(root);
1922 ret = __btrfs_free_extent(trans, root, bytenr, num_bytes,
1923 root_objectid, ref_generation,
1924 owner_objectid, owner_offset, pin);
1925 maybe_unlock_mutex(root);
1926 return ret;
1927}
1928
Chris Mason87ee04e2007-11-30 11:30:34 -05001929static u64 stripe_align(struct btrfs_root *root, u64 val)
1930{
1931 u64 mask = ((u64)root->stripesize - 1);
1932 u64 ret = (val + mask) & ~mask;
1933 return ret;
1934}
1935
Chris Masonfec577f2007-02-26 10:40:21 -05001936/*
1937 * walks the btree of allocated extents and find a hole of a given size.
1938 * The key ins is changed to record the hole:
1939 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -04001940 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -05001941 * ins->offset == number of blocks
1942 * Any available blocks before search_start are skipped.
1943 */
Chris Mason98ed5172008-01-03 10:01:48 -05001944static int noinline find_free_extent(struct btrfs_trans_handle *trans,
1945 struct btrfs_root *orig_root,
1946 u64 num_bytes, u64 empty_size,
1947 u64 search_start, u64 search_end,
1948 u64 hint_byte, struct btrfs_key *ins,
1949 u64 exclude_start, u64 exclude_nr,
1950 int data)
Chris Masonfec577f2007-02-26 10:40:21 -05001951{
Chris Mason87ee04e2007-11-30 11:30:34 -05001952 int ret;
Chris Masona061fc82008-05-07 11:43:44 -04001953 u64 orig_search_start;
Chris Mason9f5fae22007-03-20 14:38:32 -04001954 struct btrfs_root * root = orig_root->fs_info->extent_root;
Chris Masonf2458e12007-04-25 15:52:25 -04001955 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -04001956 u64 total_needed = num_bytes;
Chris Mason239b14b2008-03-24 15:02:07 -04001957 u64 *last_ptr = NULL;
Chris Masonbe08c1b2007-05-03 09:06:49 -04001958 struct btrfs_block_group_cache *block_group;
Chris Mason0ef3e662008-05-24 14:04:53 -04001959 int chunk_alloc_done = 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001960 int empty_cluster = 2 * 1024 * 1024;
Chris Mason0ef3e662008-05-24 14:04:53 -04001961 int allowed_chunk_alloc = 0;
Chris Masonfec577f2007-02-26 10:40:21 -05001962
Chris Masondb945352007-10-15 16:15:53 -04001963 WARN_ON(num_bytes < root->sectorsize);
Chris Masonb1a4d962007-04-04 15:27:52 -04001964 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
1965
Chris Mason0ef3e662008-05-24 14:04:53 -04001966 if (orig_root->ref_cows || empty_size)
1967 allowed_chunk_alloc = 1;
1968
Chris Mason239b14b2008-03-24 15:02:07 -04001969 if (data & BTRFS_BLOCK_GROUP_METADATA) {
1970 last_ptr = &root->fs_info->last_alloc;
Chris Mason8790d502008-04-03 16:29:03 -04001971 empty_cluster = 256 * 1024;
Chris Mason239b14b2008-03-24 15:02:07 -04001972 }
1973
Josef Bacik0f9dd462008-09-23 13:14:11 -04001974 if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD))
Chris Mason239b14b2008-03-24 15:02:07 -04001975 last_ptr = &root->fs_info->last_data_alloc;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001976
Chris Masone02119d2008-09-05 16:13:11 -04001977 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
1978 last_ptr = &root->fs_info->last_log_alloc;
1979 if (!last_ptr == 0 && root->fs_info->last_alloc) {
1980 *last_ptr = root->fs_info->last_alloc + empty_cluster;
1981 }
1982 }
Chris Mason239b14b2008-03-24 15:02:07 -04001983
1984 if (last_ptr) {
1985 if (*last_ptr)
1986 hint_byte = *last_ptr;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001987 else
Chris Mason239b14b2008-03-24 15:02:07 -04001988 empty_size += empty_cluster;
Chris Mason239b14b2008-03-24 15:02:07 -04001989 }
1990
Chris Masona061fc82008-05-07 11:43:44 -04001991 search_start = max(search_start, first_logical_byte(root, 0));
1992 orig_search_start = search_start;
1993
Chris Mason7f93bf82008-03-24 15:01:28 -04001994 if (search_end == (u64)-1)
1995 search_end = btrfs_super_total_bytes(&info->super_copy);
Chris Mason0b86a832008-03-24 15:01:56 -04001996
Chris Mason239b14b2008-03-24 15:02:07 -04001997 search_start = max(search_start, hint_byte);
Chris Mason6702ed42007-08-07 16:15:09 -04001998 total_needed += empty_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001999
Josef Bacik0f9dd462008-09-23 13:14:11 -04002000new_group:
2001 block_group = btrfs_lookup_block_group(info, search_start);
2002
2003 /*
2004 * Ok this looks a little tricky, buts its really simple. First if we
2005 * didn't find a block group obviously we want to start over.
2006 * Secondly, if the block group we found does not match the type we
2007 * need, and we have a last_ptr and its not 0, chances are the last
2008 * allocation we made was at the end of the block group, so lets go
2009 * ahead and skip the looking through the rest of the block groups and
2010 * start at the beginning. This helps with metadata allocations,
2011 * since you are likely to have a bunch of data block groups to search
2012 * through first before you realize that you need to start over, so go
2013 * ahead and start over and save the time.
2014 */
2015 if (!block_group || (!block_group_bits(block_group, data) &&
2016 last_ptr && *last_ptr)) {
2017 if (search_start != orig_search_start) {
2018 if (last_ptr && *last_ptr)
2019 *last_ptr = 0;
2020 search_start = orig_search_start;
2021 goto new_group;
2022 } else if (!chunk_alloc_done && allowed_chunk_alloc) {
2023 ret = do_chunk_alloc(trans, root,
2024 num_bytes + 2 * 1024 * 1024,
2025 data, 1);
2026 if (ret < 0) {
2027 struct btrfs_space_info *info;
2028
2029 info = __find_space_info(root->fs_info, data);
2030 goto error;
2031 }
2032 BUG_ON(ret);
2033 chunk_alloc_done = 1;
2034 search_start = orig_search_start;
2035 goto new_group;
2036 } else {
2037 ret = -ENOSPC;
2038 goto error;
Chris Mason0ef3e662008-05-24 14:04:53 -04002039 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002040 }
2041
2042 /*
2043 * this is going to seach through all of the existing block groups it
2044 * can find, so if we don't find something we need to see if we can
2045 * allocate what we need.
2046 */
2047 ret = find_free_space(root, &block_group, &search_start,
2048 total_needed, data);
2049 if (ret == -ENOSPC) {
2050 /*
2051 * instead of allocating, start at the original search start
2052 * and see if there is something to be found, if not then we
2053 * allocate
2054 */
2055 if (search_start != orig_search_start) {
2056 if (last_ptr && *last_ptr) {
2057 *last_ptr = 0;
2058 total_needed += empty_cluster;
2059 }
2060 search_start = orig_search_start;
2061 goto new_group;
2062 }
2063
2064 /*
2065 * we've already allocated, we're pretty screwed
2066 */
2067 if (chunk_alloc_done) {
2068 goto error;
2069 } else if (!allowed_chunk_alloc && block_group &&
2070 block_group_bits(block_group, data)) {
2071 block_group->space_info->force_alloc = 1;
2072 goto error;
2073 } else if (!allowed_chunk_alloc) {
2074 goto error;
2075 }
2076
2077 ret = do_chunk_alloc(trans, root, num_bytes + 2 * 1024 * 1024,
2078 data, 1);
2079 if (ret < 0)
2080 goto error;
2081
2082 BUG_ON(ret);
Chris Mason0ef3e662008-05-24 14:04:53 -04002083 chunk_alloc_done = 1;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002084 if (block_group)
2085 search_start = block_group->key.objectid +
2086 block_group->key.offset;
2087 else
2088 search_start = orig_search_start;
2089 goto new_group;
Chris Mason0ef3e662008-05-24 14:04:53 -04002090 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002091
Chris Mason0b86a832008-03-24 15:01:56 -04002092 if (ret)
2093 goto error;
2094
Chris Mason87ee04e2007-11-30 11:30:34 -05002095 search_start = stripe_align(root, search_start);
Chris Masonfec577f2007-02-26 10:40:21 -05002096 ins->objectid = search_start;
Chris Mason0b86a832008-03-24 15:01:56 -04002097 ins->offset = num_bytes;
Chris Masone37c9e62007-05-09 20:13:14 -04002098
Josef Bacik0f9dd462008-09-23 13:14:11 -04002099 if (ins->objectid + num_bytes >= search_end) {
2100 search_start = orig_search_start;
2101 if (chunk_alloc_done) {
2102 ret = -ENOSPC;
2103 goto error;
2104 }
2105 goto new_group;
2106 }
Chris Mason0b86a832008-03-24 15:01:56 -04002107
2108 if (ins->objectid + num_bytes >
2109 block_group->key.objectid + block_group->key.offset) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04002110 if (search_start == orig_search_start && chunk_alloc_done) {
2111 ret = -ENOSPC;
2112 goto error;
2113 }
Chris Masone19caa52007-10-15 16:17:44 -04002114 search_start = block_group->key.objectid +
2115 block_group->key.offset;
2116 goto new_group;
2117 }
Chris Mason0b86a832008-03-24 15:01:56 -04002118
Chris Masondb945352007-10-15 16:15:53 -04002119 if (exclude_nr > 0 && (ins->objectid + num_bytes > exclude_start &&
Chris Masonf2654de2007-06-26 12:20:46 -04002120 ins->objectid < exclude_start + exclude_nr)) {
2121 search_start = exclude_start + exclude_nr;
2122 goto new_group;
2123 }
Chris Mason0b86a832008-03-24 15:01:56 -04002124
Josef Bacik0f9dd462008-09-23 13:14:11 -04002125 if (!(data & BTRFS_BLOCK_GROUP_DATA))
2126 trans->block_group = block_group;
2127
Chris Masondb945352007-10-15 16:15:53 -04002128 ins->offset = num_bytes;
Chris Mason239b14b2008-03-24 15:02:07 -04002129 if (last_ptr) {
2130 *last_ptr = ins->objectid + ins->offset;
2131 if (*last_ptr ==
Josef Bacik0f9dd462008-09-23 13:14:11 -04002132 btrfs_super_total_bytes(&root->fs_info->super_copy))
Chris Mason239b14b2008-03-24 15:02:07 -04002133 *last_ptr = 0;
Chris Mason239b14b2008-03-24 15:02:07 -04002134 }
Chris Masonbe744172007-05-06 10:15:01 -04002135
Josef Bacik0f9dd462008-09-23 13:14:11 -04002136 ret = 0;
Chris Mason0f70abe2007-02-28 16:46:22 -05002137error:
Chris Mason0f70abe2007-02-28 16:46:22 -05002138 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05002139}
Chris Masonec44a352008-04-28 15:29:52 -04002140
Josef Bacik0f9dd462008-09-23 13:14:11 -04002141static void dump_space_info(struct btrfs_space_info *info, u64 bytes)
2142{
2143 struct btrfs_block_group_cache *cache;
2144 struct list_head *l;
2145
2146 printk(KERN_INFO "space_info has %Lu free, is %sfull\n",
2147 info->total_bytes - info->bytes_used - info->bytes_pinned,
2148 (info->full) ? "" : "not ");
2149
2150 spin_lock(&info->lock);
2151 list_for_each(l, &info->block_groups) {
2152 cache = list_entry(l, struct btrfs_block_group_cache, list);
2153 spin_lock(&cache->lock);
2154 printk(KERN_INFO "block group %Lu has %Lu bytes, %Lu used "
2155 "%Lu pinned\n",
2156 cache->key.objectid, cache->key.offset,
2157 btrfs_block_group_used(&cache->item), cache->pinned);
2158 btrfs_dump_free_space(cache, bytes);
2159 spin_unlock(&cache->lock);
2160 }
2161 spin_unlock(&info->lock);
2162}
Chris Masone6dcd2d2008-07-17 12:53:50 -04002163static int __btrfs_reserve_extent(struct btrfs_trans_handle *trans,
2164 struct btrfs_root *root,
2165 u64 num_bytes, u64 min_alloc_size,
2166 u64 empty_size, u64 hint_byte,
2167 u64 search_end, struct btrfs_key *ins,
2168 u64 data)
Chris Masonfec577f2007-02-26 10:40:21 -05002169{
2170 int ret;
Chris Masonfbdc7622007-05-30 10:22:12 -04002171 u64 search_start = 0;
Chris Mason8790d502008-04-03 16:29:03 -04002172 u64 alloc_profile;
Chris Mason1261ec42007-03-20 20:35:03 -04002173 struct btrfs_fs_info *info = root->fs_info;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002174 struct btrfs_block_group_cache *cache;
Chris Mason925baed2008-06-25 16:01:30 -04002175
Chris Mason6324fbf2008-03-24 15:01:59 -04002176 if (data) {
Chris Mason8790d502008-04-03 16:29:03 -04002177 alloc_profile = info->avail_data_alloc_bits &
2178 info->data_alloc_profile;
2179 data = BTRFS_BLOCK_GROUP_DATA | alloc_profile;
Chris Mason6324fbf2008-03-24 15:01:59 -04002180 } else if (root == root->fs_info->chunk_root) {
Chris Mason8790d502008-04-03 16:29:03 -04002181 alloc_profile = info->avail_system_alloc_bits &
2182 info->system_alloc_profile;
2183 data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile;
Chris Mason6324fbf2008-03-24 15:01:59 -04002184 } else {
Chris Mason8790d502008-04-03 16:29:03 -04002185 alloc_profile = info->avail_metadata_alloc_bits &
2186 info->metadata_alloc_profile;
2187 data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile;
Chris Mason6324fbf2008-03-24 15:01:59 -04002188 }
Chris Mason98d20f62008-04-14 09:46:10 -04002189again:
Chris Masona061fc82008-05-07 11:43:44 -04002190 data = reduce_alloc_profile(root, data);
Chris Mason0ef3e662008-05-24 14:04:53 -04002191 /*
2192 * the only place that sets empty_size is btrfs_realloc_node, which
2193 * is not called recursively on allocations
2194 */
2195 if (empty_size || root->ref_cows) {
Chris Mason593060d2008-03-25 16:50:33 -04002196 if (!(data & BTRFS_BLOCK_GROUP_METADATA)) {
Chris Mason6324fbf2008-03-24 15:01:59 -04002197 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0ef3e662008-05-24 14:04:53 -04002198 2 * 1024 * 1024,
2199 BTRFS_BLOCK_GROUP_METADATA |
2200 (info->metadata_alloc_profile &
2201 info->avail_metadata_alloc_bits), 0);
Chris Mason6324fbf2008-03-24 15:01:59 -04002202 }
2203 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0ef3e662008-05-24 14:04:53 -04002204 num_bytes + 2 * 1024 * 1024, data, 0);
Chris Mason6324fbf2008-03-24 15:01:59 -04002205 }
Chris Mason0b86a832008-03-24 15:01:56 -04002206
Chris Masondb945352007-10-15 16:15:53 -04002207 WARN_ON(num_bytes < root->sectorsize);
2208 ret = find_free_extent(trans, root, num_bytes, empty_size,
2209 search_start, search_end, hint_byte, ins,
Chris Mason26b80032007-08-08 20:17:12 -04002210 trans->alloc_exclude_start,
2211 trans->alloc_exclude_nr, data);
Chris Mason3b951512008-04-17 11:29:12 -04002212
Chris Mason98d20f62008-04-14 09:46:10 -04002213 if (ret == -ENOSPC && num_bytes > min_alloc_size) {
2214 num_bytes = num_bytes >> 1;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002215 num_bytes = num_bytes & ~(root->sectorsize - 1);
Chris Mason98d20f62008-04-14 09:46:10 -04002216 num_bytes = max(num_bytes, min_alloc_size);
Chris Mason0ef3e662008-05-24 14:04:53 -04002217 do_chunk_alloc(trans, root->fs_info->extent_root,
2218 num_bytes, data, 1);
Chris Mason98d20f62008-04-14 09:46:10 -04002219 goto again;
2220 }
Chris Masonec44a352008-04-28 15:29:52 -04002221 if (ret) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04002222 struct btrfs_space_info *sinfo;
2223
2224 sinfo = __find_space_info(root->fs_info, data);
2225 printk("allocation failed flags %Lu, wanted %Lu\n",
2226 data, num_bytes);
2227 dump_space_info(sinfo, num_bytes);
Chris Mason925baed2008-06-25 16:01:30 -04002228 BUG();
Chris Mason925baed2008-06-25 16:01:30 -04002229 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002230 cache = btrfs_lookup_block_group(root->fs_info, ins->objectid);
2231 if (!cache) {
2232 printk(KERN_ERR "Unable to find block group for %Lu\n", ins->objectid);
2233 return -ENOSPC;
2234 }
2235
2236 ret = btrfs_remove_free_space(cache, ins->objectid, ins->offset);
2237
2238 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002239}
2240
Chris Mason65b51a02008-08-01 15:11:20 -04002241int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len)
2242{
Josef Bacik0f9dd462008-09-23 13:14:11 -04002243 struct btrfs_block_group_cache *cache;
2244
Chris Mason65b51a02008-08-01 15:11:20 -04002245 maybe_lock_mutex(root);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002246 cache = btrfs_lookup_block_group(root->fs_info, start);
2247 if (!cache) {
2248 printk(KERN_ERR "Unable to find block group for %Lu\n", start);
2249 maybe_unlock_mutex(root);
2250 return -ENOSPC;
2251 }
2252 btrfs_add_free_space(cache, start, len);
Chris Mason65b51a02008-08-01 15:11:20 -04002253 maybe_unlock_mutex(root);
2254 return 0;
2255}
2256
Chris Masone6dcd2d2008-07-17 12:53:50 -04002257int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
2258 struct btrfs_root *root,
2259 u64 num_bytes, u64 min_alloc_size,
2260 u64 empty_size, u64 hint_byte,
2261 u64 search_end, struct btrfs_key *ins,
2262 u64 data)
2263{
2264 int ret;
2265 maybe_lock_mutex(root);
2266 ret = __btrfs_reserve_extent(trans, root, num_bytes, min_alloc_size,
2267 empty_size, hint_byte, search_end, ins,
2268 data);
2269 maybe_unlock_mutex(root);
2270 return ret;
2271}
2272
2273static int __btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
2274 struct btrfs_root *root,
2275 u64 root_objectid, u64 ref_generation,
2276 u64 owner, u64 owner_offset,
2277 struct btrfs_key *ins)
2278{
2279 int ret;
2280 int pending_ret;
2281 u64 super_used;
2282 u64 root_used;
2283 u64 num_bytes = ins->offset;
2284 u32 sizes[2];
2285 struct btrfs_fs_info *info = root->fs_info;
2286 struct btrfs_root *extent_root = info->extent_root;
2287 struct btrfs_extent_item *extent_item;
2288 struct btrfs_extent_ref *ref;
2289 struct btrfs_path *path;
2290 struct btrfs_key keys[2];
Chris Masonf2654de2007-06-26 12:20:46 -04002291
Josef Bacik58176a92007-08-29 15:47:34 -04002292 /* block accounting for super block */
Chris Masona2135012008-06-25 16:01:30 -04002293 spin_lock_irq(&info->delalloc_lock);
Chris Masondb945352007-10-15 16:15:53 -04002294 super_used = btrfs_super_bytes_used(&info->super_copy);
2295 btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes);
Chris Masona2135012008-06-25 16:01:30 -04002296 spin_unlock_irq(&info->delalloc_lock);
Chris Mason26b80032007-08-08 20:17:12 -04002297
Josef Bacik58176a92007-08-29 15:47:34 -04002298 /* block accounting for root item */
Chris Masondb945352007-10-15 16:15:53 -04002299 root_used = btrfs_root_used(&root->root_item);
2300 btrfs_set_root_used(&root->root_item, root_used + num_bytes);
Josef Bacik58176a92007-08-29 15:47:34 -04002301
Chris Mason26b80032007-08-08 20:17:12 -04002302 if (root == extent_root) {
Chris Mason1a5bc162007-10-15 16:15:26 -04002303 set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
2304 ins->objectid + ins->offset - 1,
2305 EXTENT_LOCKED, GFP_NOFS);
Chris Mason26b80032007-08-08 20:17:12 -04002306 goto update_block;
2307 }
2308
Chris Mason47e4bb92008-02-01 14:51:59 -05002309 memcpy(&keys[0], ins, sizeof(*ins));
2310 keys[1].offset = hash_extent_ref(root_objectid, ref_generation,
2311 owner, owner_offset);
2312 keys[1].objectid = ins->objectid;
2313 keys[1].type = BTRFS_EXTENT_REF_KEY;
2314 sizes[0] = sizeof(*extent_item);
2315 sizes[1] = sizeof(*ref);
Chris Mason7bb86312007-12-11 09:25:06 -05002316
2317 path = btrfs_alloc_path();
2318 BUG_ON(!path);
Chris Mason47e4bb92008-02-01 14:51:59 -05002319
2320 ret = btrfs_insert_empty_items(trans, extent_root, path, keys,
2321 sizes, 2);
Chris Masonccd467d2007-06-28 15:57:36 -04002322 BUG_ON(ret);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002323
Chris Mason47e4bb92008-02-01 14:51:59 -05002324 extent_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2325 struct btrfs_extent_item);
2326 btrfs_set_extent_refs(path->nodes[0], extent_item, 1);
2327 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
2328 struct btrfs_extent_ref);
2329
2330 btrfs_set_ref_root(path->nodes[0], ref, root_objectid);
2331 btrfs_set_ref_generation(path->nodes[0], ref, ref_generation);
2332 btrfs_set_ref_objectid(path->nodes[0], ref, owner);
2333 btrfs_set_ref_offset(path->nodes[0], ref, owner_offset);
2334
2335 btrfs_mark_buffer_dirty(path->nodes[0]);
2336
2337 trans->alloc_exclude_start = 0;
2338 trans->alloc_exclude_nr = 0;
Chris Mason7bb86312007-12-11 09:25:06 -05002339 btrfs_free_path(path);
Chris Masone089f052007-03-16 16:20:31 -04002340 finish_current_insert(trans, extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -04002341 pending_ret = del_pending_extents(trans, extent_root);
Chris Masonf510cfe2007-10-15 16:14:48 -04002342
Chris Mason925baed2008-06-25 16:01:30 -04002343 if (ret)
2344 goto out;
Chris Masone37c9e62007-05-09 20:13:14 -04002345 if (pending_ret) {
Chris Mason925baed2008-06-25 16:01:30 -04002346 ret = pending_ret;
2347 goto out;
Chris Masone37c9e62007-05-09 20:13:14 -04002348 }
Chris Mason26b80032007-08-08 20:17:12 -04002349
2350update_block:
Chris Mason0b86a832008-03-24 15:01:56 -04002351 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0);
Chris Masonf5947062008-02-04 10:10:13 -05002352 if (ret) {
2353 printk("update block group failed for %Lu %Lu\n",
2354 ins->objectid, ins->offset);
2355 BUG();
2356 }
Chris Mason925baed2008-06-25 16:01:30 -04002357out:
Chris Masone6dcd2d2008-07-17 12:53:50 -04002358 return ret;
2359}
2360
2361int btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
2362 struct btrfs_root *root,
2363 u64 root_objectid, u64 ref_generation,
2364 u64 owner, u64 owner_offset,
2365 struct btrfs_key *ins)
2366{
2367 int ret;
2368 maybe_lock_mutex(root);
2369 ret = __btrfs_alloc_reserved_extent(trans, root, root_objectid,
2370 ref_generation, owner,
2371 owner_offset, ins);
2372 maybe_unlock_mutex(root);
2373 return ret;
2374}
Chris Masone02119d2008-09-05 16:13:11 -04002375
2376/*
2377 * this is used by the tree logging recovery code. It records that
2378 * an extent has been allocated and makes sure to clear the free
2379 * space cache bits as well
2380 */
2381int btrfs_alloc_logged_extent(struct btrfs_trans_handle *trans,
2382 struct btrfs_root *root,
2383 u64 root_objectid, u64 ref_generation,
2384 u64 owner, u64 owner_offset,
2385 struct btrfs_key *ins)
2386{
2387 int ret;
2388 struct btrfs_block_group_cache *block_group;
2389
2390 maybe_lock_mutex(root);
2391 block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
2392 cache_block_group(root, block_group);
2393
Josef Bacik0f9dd462008-09-23 13:14:11 -04002394 ret = btrfs_remove_free_space(block_group, ins->objectid, ins->offset);
2395 BUG_ON(ret);
2396
Chris Masone02119d2008-09-05 16:13:11 -04002397 ret = __btrfs_alloc_reserved_extent(trans, root, root_objectid,
2398 ref_generation, owner,
2399 owner_offset, ins);
2400 maybe_unlock_mutex(root);
2401 return ret;
2402}
2403
Chris Masone6dcd2d2008-07-17 12:53:50 -04002404/*
2405 * finds a free extent and does all the dirty work required for allocation
2406 * returns the key for the extent through ins, and a tree buffer for
2407 * the first block of the extent through buf.
2408 *
2409 * returns 0 if everything worked, non-zero otherwise.
2410 */
2411int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
2412 struct btrfs_root *root,
2413 u64 num_bytes, u64 min_alloc_size,
2414 u64 root_objectid, u64 ref_generation,
2415 u64 owner, u64 owner_offset,
2416 u64 empty_size, u64 hint_byte,
2417 u64 search_end, struct btrfs_key *ins, u64 data)
2418{
2419 int ret;
2420
2421 maybe_lock_mutex(root);
2422
2423 ret = __btrfs_reserve_extent(trans, root, num_bytes,
2424 min_alloc_size, empty_size, hint_byte,
2425 search_end, ins, data);
2426 BUG_ON(ret);
Chris Masond00aff02008-09-11 15:54:42 -04002427 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
2428 ret = __btrfs_alloc_reserved_extent(trans, root, root_objectid,
2429 ref_generation, owner,
2430 owner_offset, ins);
2431 BUG_ON(ret);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002432
Chris Masond00aff02008-09-11 15:54:42 -04002433 }
Chris Mason925baed2008-06-25 16:01:30 -04002434 maybe_unlock_mutex(root);
2435 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05002436}
Chris Mason65b51a02008-08-01 15:11:20 -04002437
2438struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
2439 struct btrfs_root *root,
2440 u64 bytenr, u32 blocksize)
2441{
2442 struct extent_buffer *buf;
2443
2444 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
2445 if (!buf)
2446 return ERR_PTR(-ENOMEM);
2447 btrfs_set_header_generation(buf, trans->transid);
2448 btrfs_tree_lock(buf);
2449 clean_tree_block(trans, root, buf);
2450 btrfs_set_buffer_uptodate(buf);
Chris Masond0c803c2008-09-11 16:17:57 -04002451 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
2452 set_extent_dirty(&root->dirty_log_pages, buf->start,
Chris Mason65b51a02008-08-01 15:11:20 -04002453 buf->start + buf->len - 1, GFP_NOFS);
Chris Masond0c803c2008-09-11 16:17:57 -04002454 } else {
2455 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
2456 buf->start + buf->len - 1, GFP_NOFS);
2457 }
Chris Mason65b51a02008-08-01 15:11:20 -04002458 trans->blocks_used++;
2459 return buf;
2460}
2461
Chris Masonfec577f2007-02-26 10:40:21 -05002462/*
2463 * helper function to allocate a block for a given tree
2464 * returns the tree buffer or NULL.
2465 */
Chris Mason5f39d392007-10-15 16:14:19 -04002466struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Chris Masondb945352007-10-15 16:15:53 -04002467 struct btrfs_root *root,
Chris Mason7bb86312007-12-11 09:25:06 -05002468 u32 blocksize,
Chris Mason7bb86312007-12-11 09:25:06 -05002469 u64 root_objectid,
2470 u64 ref_generation,
2471 u64 first_objectid,
2472 int level,
2473 u64 hint,
Chris Mason5f39d392007-10-15 16:14:19 -04002474 u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05002475{
Chris Masone2fa7222007-03-12 16:22:34 -04002476 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -05002477 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04002478 struct extent_buffer *buf;
Chris Masonfec577f2007-02-26 10:40:21 -05002479
Chris Mason98d20f62008-04-14 09:46:10 -04002480 ret = btrfs_alloc_extent(trans, root, blocksize, blocksize,
Chris Mason7bb86312007-12-11 09:25:06 -05002481 root_objectid, ref_generation,
Chris Masonf6dbff52007-12-13 11:13:32 -05002482 level, first_objectid, empty_size, hint,
Chris Masondb945352007-10-15 16:15:53 -04002483 (u64)-1, &ins, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05002484 if (ret) {
Chris Mason54aa1f42007-06-22 14:16:25 -04002485 BUG_ON(ret > 0);
2486 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05002487 }
Chris Mason55c69072008-01-09 15:55:33 -05002488
Chris Mason65b51a02008-08-01 15:11:20 -04002489 buf = btrfs_init_new_buffer(trans, root, ins.objectid, blocksize);
Chris Masonfec577f2007-02-26 10:40:21 -05002490 return buf;
2491}
Chris Masona28ec192007-03-06 20:08:01 -05002492
Chris Masone02119d2008-09-05 16:13:11 -04002493int btrfs_drop_leaf_ref(struct btrfs_trans_handle *trans,
2494 struct btrfs_root *root, struct extent_buffer *leaf)
Chris Mason6407bf62007-03-27 06:33:00 -04002495{
Chris Mason7bb86312007-12-11 09:25:06 -05002496 u64 leaf_owner;
2497 u64 leaf_generation;
Chris Mason5f39d392007-10-15 16:14:19 -04002498 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -04002499 struct btrfs_file_extent_item *fi;
2500 int i;
2501 int nritems;
2502 int ret;
2503
Chris Mason5f39d392007-10-15 16:14:19 -04002504 BUG_ON(!btrfs_is_leaf(leaf));
2505 nritems = btrfs_header_nritems(leaf);
Chris Mason7bb86312007-12-11 09:25:06 -05002506 leaf_owner = btrfs_header_owner(leaf);
2507 leaf_generation = btrfs_header_generation(leaf);
2508
Chris Mason6407bf62007-03-27 06:33:00 -04002509 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04002510 u64 disk_bytenr;
Chris Masone34a5b42008-07-22 12:08:37 -04002511 cond_resched();
Chris Mason5f39d392007-10-15 16:14:19 -04002512
2513 btrfs_item_key_to_cpu(leaf, &key, i);
2514 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason6407bf62007-03-27 06:33:00 -04002515 continue;
2516 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002517 if (btrfs_file_extent_type(leaf, fi) ==
2518 BTRFS_FILE_EXTENT_INLINE)
Chris Mason236454d2007-04-19 13:37:44 -04002519 continue;
Chris Mason6407bf62007-03-27 06:33:00 -04002520 /*
2521 * FIXME make sure to insert a trans record that
2522 * repeats the snapshot del on crash
2523 */
Chris Masondb945352007-10-15 16:15:53 -04002524 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
2525 if (disk_bytenr == 0)
Chris Mason3a686372007-05-24 13:35:57 -04002526 continue;
Chris Mason4a096752008-07-21 10:29:44 -04002527
2528 mutex_lock(&root->fs_info->alloc_mutex);
Chris Mason925baed2008-06-25 16:01:30 -04002529 ret = __btrfs_free_extent(trans, root, disk_bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -05002530 btrfs_file_extent_disk_num_bytes(leaf, fi),
2531 leaf_owner, leaf_generation,
2532 key.objectid, key.offset, 0);
Chris Mason4a096752008-07-21 10:29:44 -04002533 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Mason2dd3e672008-08-04 08:20:15 -04002534
2535 atomic_inc(&root->fs_info->throttle_gen);
2536 wake_up(&root->fs_info->transaction_throttle);
2537 cond_resched();
2538
Chris Mason6407bf62007-03-27 06:33:00 -04002539 BUG_ON(ret);
2540 }
2541 return 0;
2542}
2543
Chris Masone02119d2008-09-05 16:13:11 -04002544static int noinline cache_drop_leaf_ref(struct btrfs_trans_handle *trans,
2545 struct btrfs_root *root,
2546 struct btrfs_leaf_ref *ref)
Yan Zheng31153d82008-07-28 15:32:19 -04002547{
2548 int i;
2549 int ret;
2550 struct btrfs_extent_info *info = ref->extents;
2551
Yan Zheng31153d82008-07-28 15:32:19 -04002552 for (i = 0; i < ref->nritems; i++) {
2553 mutex_lock(&root->fs_info->alloc_mutex);
2554 ret = __btrfs_free_extent(trans, root,
2555 info->bytenr, info->num_bytes,
2556 ref->owner, ref->generation,
2557 info->objectid, info->offset, 0);
2558 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Mason2dd3e672008-08-04 08:20:15 -04002559
2560 atomic_inc(&root->fs_info->throttle_gen);
2561 wake_up(&root->fs_info->transaction_throttle);
2562 cond_resched();
2563
Yan Zheng31153d82008-07-28 15:32:19 -04002564 BUG_ON(ret);
2565 info++;
2566 }
Yan Zheng31153d82008-07-28 15:32:19 -04002567
2568 return 0;
2569}
2570
Chris Mason333db942008-06-25 16:01:30 -04002571int drop_snap_lookup_refcount(struct btrfs_root *root, u64 start, u64 len,
2572 u32 *refs)
2573{
Chris Mason017e5362008-07-28 15:32:51 -04002574 int ret;
Chris Masonf87f0572008-08-01 11:27:23 -04002575
Chris Mason017e5362008-07-28 15:32:51 -04002576 ret = lookup_extent_ref(NULL, root, start, len, refs);
Chris Masonf87f0572008-08-01 11:27:23 -04002577 BUG_ON(ret);
2578
2579#if 0 // some debugging code in case we see problems here
2580 /* if the refs count is one, it won't get increased again. But
2581 * if the ref count is > 1, someone may be decreasing it at
2582 * the same time we are.
2583 */
2584 if (*refs != 1) {
2585 struct extent_buffer *eb = NULL;
2586 eb = btrfs_find_create_tree_block(root, start, len);
2587 if (eb)
2588 btrfs_tree_lock(eb);
2589
2590 mutex_lock(&root->fs_info->alloc_mutex);
2591 ret = lookup_extent_ref(NULL, root, start, len, refs);
2592 BUG_ON(ret);
2593 mutex_unlock(&root->fs_info->alloc_mutex);
2594
2595 if (eb) {
2596 btrfs_tree_unlock(eb);
2597 free_extent_buffer(eb);
2598 }
2599 if (*refs == 1) {
2600 printk("block %llu went down to one during drop_snap\n",
2601 (unsigned long long)start);
2602 }
2603
2604 }
2605#endif
2606
Chris Masone7a84562008-06-25 16:01:31 -04002607 cond_resched();
Chris Mason017e5362008-07-28 15:32:51 -04002608 return ret;
Chris Mason333db942008-06-25 16:01:30 -04002609}
2610
2611/*
Chris Mason9aca1d52007-03-13 11:09:37 -04002612 * helper function for drop_snapshot, this walks down the tree dropping ref
2613 * counts as it goes.
2614 */
Chris Mason98ed5172008-01-03 10:01:48 -05002615static int noinline walk_down_tree(struct btrfs_trans_handle *trans,
2616 struct btrfs_root *root,
2617 struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05002618{
Chris Mason7bb86312007-12-11 09:25:06 -05002619 u64 root_owner;
2620 u64 root_gen;
2621 u64 bytenr;
Chris Masonca7a79a2008-05-12 12:59:19 -04002622 u64 ptr_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04002623 struct extent_buffer *next;
2624 struct extent_buffer *cur;
Chris Mason7bb86312007-12-11 09:25:06 -05002625 struct extent_buffer *parent;
Yan Zheng31153d82008-07-28 15:32:19 -04002626 struct btrfs_leaf_ref *ref;
Chris Masondb945352007-10-15 16:15:53 -04002627 u32 blocksize;
Chris Mason20524f02007-03-10 06:35:47 -05002628 int ret;
2629 u32 refs;
2630
Chris Mason5caf2a02007-04-02 11:20:42 -04002631 WARN_ON(*level < 0);
2632 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason333db942008-06-25 16:01:30 -04002633 ret = drop_snap_lookup_refcount(root, path->nodes[*level]->start,
Chris Masondb945352007-10-15 16:15:53 -04002634 path->nodes[*level]->len, &refs);
Chris Mason20524f02007-03-10 06:35:47 -05002635 BUG_ON(ret);
2636 if (refs > 1)
2637 goto out;
Chris Masone0115992007-06-19 16:23:05 -04002638
Chris Mason9aca1d52007-03-13 11:09:37 -04002639 /*
2640 * walk down to the last node level and free all the leaves
2641 */
Chris Mason6407bf62007-03-27 06:33:00 -04002642 while(*level >= 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -04002643 WARN_ON(*level < 0);
2644 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason20524f02007-03-10 06:35:47 -05002645 cur = path->nodes[*level];
Chris Masone0115992007-06-19 16:23:05 -04002646
Chris Mason5f39d392007-10-15 16:14:19 -04002647 if (btrfs_header_level(cur) != *level)
Chris Mason2c90e5d2007-04-02 10:50:19 -04002648 WARN_ON(1);
Chris Masone0115992007-06-19 16:23:05 -04002649
Chris Mason7518a232007-03-12 12:01:18 -04002650 if (path->slots[*level] >=
Chris Mason5f39d392007-10-15 16:14:19 -04002651 btrfs_header_nritems(cur))
Chris Mason20524f02007-03-10 06:35:47 -05002652 break;
Chris Mason6407bf62007-03-27 06:33:00 -04002653 if (*level == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002654 ret = btrfs_drop_leaf_ref(trans, root, cur);
Chris Mason6407bf62007-03-27 06:33:00 -04002655 BUG_ON(ret);
2656 break;
2657 }
Chris Masondb945352007-10-15 16:15:53 -04002658 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
Chris Masonca7a79a2008-05-12 12:59:19 -04002659 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
Chris Masondb945352007-10-15 16:15:53 -04002660 blocksize = btrfs_level_size(root, *level - 1);
Chris Mason925baed2008-06-25 16:01:30 -04002661
Chris Mason333db942008-06-25 16:01:30 -04002662 ret = drop_snap_lookup_refcount(root, bytenr, blocksize, &refs);
Chris Mason6407bf62007-03-27 06:33:00 -04002663 BUG_ON(ret);
2664 if (refs != 1) {
Chris Mason7bb86312007-12-11 09:25:06 -05002665 parent = path->nodes[*level];
2666 root_owner = btrfs_header_owner(parent);
2667 root_gen = btrfs_header_generation(parent);
Chris Mason20524f02007-03-10 06:35:47 -05002668 path->slots[*level]++;
Chris Masonf87f0572008-08-01 11:27:23 -04002669
2670 mutex_lock(&root->fs_info->alloc_mutex);
Chris Mason925baed2008-06-25 16:01:30 -04002671 ret = __btrfs_free_extent(trans, root, bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -05002672 blocksize, root_owner,
2673 root_gen, 0, 0, 1);
Chris Mason20524f02007-03-10 06:35:47 -05002674 BUG_ON(ret);
Chris Masonf87f0572008-08-01 11:27:23 -04002675 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Mason18e35e0a2008-08-01 13:11:41 -04002676
2677 atomic_inc(&root->fs_info->throttle_gen);
2678 wake_up(&root->fs_info->transaction_throttle);
Chris Mason2dd3e672008-08-04 08:20:15 -04002679 cond_resched();
Chris Mason18e35e0a2008-08-01 13:11:41 -04002680
Chris Mason20524f02007-03-10 06:35:47 -05002681 continue;
2682 }
Chris Masonf87f0572008-08-01 11:27:23 -04002683 /*
2684 * at this point, we have a single ref, and since the
2685 * only place referencing this extent is a dead root
2686 * the reference count should never go higher.
2687 * So, we don't need to check it again
2688 */
Yan Zheng31153d82008-07-28 15:32:19 -04002689 if (*level == 1) {
2690 struct btrfs_key key;
2691 btrfs_node_key_to_cpu(cur, &key, path->slots[*level]);
Chris Mason017e5362008-07-28 15:32:51 -04002692 ref = btrfs_lookup_leaf_ref(root, bytenr);
Yan Zheng31153d82008-07-28 15:32:19 -04002693 if (ref) {
Chris Masone02119d2008-09-05 16:13:11 -04002694 ret = cache_drop_leaf_ref(trans, root, ref);
Yan Zheng31153d82008-07-28 15:32:19 -04002695 BUG_ON(ret);
2696 btrfs_remove_leaf_ref(root, ref);
Yanbcc63ab2008-07-30 16:29:20 -04002697 btrfs_free_leaf_ref(root, ref);
Yan Zheng31153d82008-07-28 15:32:19 -04002698 *level = 0;
2699 break;
2700 }
Chris Mason37d1aee2008-07-31 10:48:37 -04002701 if (printk_ratelimit())
2702 printk("leaf ref miss for bytenr %llu\n",
2703 (unsigned long long)bytenr);
Yan Zheng31153d82008-07-28 15:32:19 -04002704 }
Chris Masondb945352007-10-15 16:15:53 -04002705 next = btrfs_find_tree_block(root, bytenr, blocksize);
Chris Mason1259ab72008-05-12 13:39:03 -04002706 if (!next || !btrfs_buffer_uptodate(next, ptr_gen)) {
Chris Mason5f39d392007-10-15 16:14:19 -04002707 free_extent_buffer(next);
Chris Mason333db942008-06-25 16:01:30 -04002708
Chris Masonca7a79a2008-05-12 12:59:19 -04002709 next = read_tree_block(root, bytenr, blocksize,
2710 ptr_gen);
Chris Masone7a84562008-06-25 16:01:31 -04002711 cond_resched();
Chris Masonf87f0572008-08-01 11:27:23 -04002712#if 0
2713 /*
2714 * this is a debugging check and can go away
2715 * the ref should never go all the way down to 1
2716 * at this point
2717 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04002718 ret = lookup_extent_ref(NULL, root, bytenr, blocksize,
2719 &refs);
Chris Masone9d0b132007-08-10 14:06:19 -04002720 BUG_ON(ret);
Chris Masonf87f0572008-08-01 11:27:23 -04002721 WARN_ON(refs != 1);
2722#endif
Chris Masone9d0b132007-08-10 14:06:19 -04002723 }
Chris Mason5caf2a02007-04-02 11:20:42 -04002724 WARN_ON(*level <= 0);
Chris Mason83e15a22007-03-12 09:03:27 -04002725 if (path->nodes[*level-1])
Chris Mason5f39d392007-10-15 16:14:19 -04002726 free_extent_buffer(path->nodes[*level-1]);
Chris Mason20524f02007-03-10 06:35:47 -05002727 path->nodes[*level-1] = next;
Chris Mason5f39d392007-10-15 16:14:19 -04002728 *level = btrfs_header_level(next);
Chris Mason20524f02007-03-10 06:35:47 -05002729 path->slots[*level] = 0;
Chris Mason2dd3e672008-08-04 08:20:15 -04002730 cond_resched();
Chris Mason20524f02007-03-10 06:35:47 -05002731 }
2732out:
Chris Mason5caf2a02007-04-02 11:20:42 -04002733 WARN_ON(*level < 0);
2734 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason7bb86312007-12-11 09:25:06 -05002735
2736 if (path->nodes[*level] == root->node) {
Chris Mason7bb86312007-12-11 09:25:06 -05002737 parent = path->nodes[*level];
Yan Zheng31153d82008-07-28 15:32:19 -04002738 bytenr = path->nodes[*level]->start;
Chris Mason7bb86312007-12-11 09:25:06 -05002739 } else {
2740 parent = path->nodes[*level + 1];
Yan Zheng31153d82008-07-28 15:32:19 -04002741 bytenr = btrfs_node_blockptr(parent, path->slots[*level + 1]);
Chris Mason7bb86312007-12-11 09:25:06 -05002742 }
2743
Yan Zheng31153d82008-07-28 15:32:19 -04002744 blocksize = btrfs_level_size(root, *level);
2745 root_owner = btrfs_header_owner(parent);
Chris Mason7bb86312007-12-11 09:25:06 -05002746 root_gen = btrfs_header_generation(parent);
Yan Zheng31153d82008-07-28 15:32:19 -04002747
Chris Masonf87f0572008-08-01 11:27:23 -04002748 mutex_lock(&root->fs_info->alloc_mutex);
Yan Zheng31153d82008-07-28 15:32:19 -04002749 ret = __btrfs_free_extent(trans, root, bytenr, blocksize,
2750 root_owner, root_gen, 0, 0, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002751 free_extent_buffer(path->nodes[*level]);
Chris Mason20524f02007-03-10 06:35:47 -05002752 path->nodes[*level] = NULL;
2753 *level += 1;
2754 BUG_ON(ret);
Chris Mason925baed2008-06-25 16:01:30 -04002755 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Masonf87f0572008-08-01 11:27:23 -04002756
Chris Masone7a84562008-06-25 16:01:31 -04002757 cond_resched();
Chris Mason20524f02007-03-10 06:35:47 -05002758 return 0;
2759}
2760
Chris Mason9aca1d52007-03-13 11:09:37 -04002761/*
2762 * helper for dropping snapshots. This walks back up the tree in the path
2763 * to find the first node higher up where we haven't yet gone through
2764 * all the slots
2765 */
Chris Mason98ed5172008-01-03 10:01:48 -05002766static int noinline walk_up_tree(struct btrfs_trans_handle *trans,
2767 struct btrfs_root *root,
2768 struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05002769{
Chris Mason7bb86312007-12-11 09:25:06 -05002770 u64 root_owner;
2771 u64 root_gen;
2772 struct btrfs_root_item *root_item = &root->root_item;
Chris Mason20524f02007-03-10 06:35:47 -05002773 int i;
2774 int slot;
2775 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04002776
Chris Mason234b63a2007-03-13 10:46:10 -04002777 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Mason20524f02007-03-10 06:35:47 -05002778 slot = path->slots[i];
Chris Mason5f39d392007-10-15 16:14:19 -04002779 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
2780 struct extent_buffer *node;
2781 struct btrfs_disk_key disk_key;
2782 node = path->nodes[i];
Chris Mason20524f02007-03-10 06:35:47 -05002783 path->slots[i]++;
2784 *level = i;
Chris Mason9f3a7422007-08-07 15:52:19 -04002785 WARN_ON(*level == 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002786 btrfs_node_key(node, &disk_key, path->slots[i]);
Chris Mason9f3a7422007-08-07 15:52:19 -04002787 memcpy(&root_item->drop_progress,
Chris Mason5f39d392007-10-15 16:14:19 -04002788 &disk_key, sizeof(disk_key));
Chris Mason9f3a7422007-08-07 15:52:19 -04002789 root_item->drop_level = i;
Chris Mason20524f02007-03-10 06:35:47 -05002790 return 0;
2791 } else {
Chris Mason7bb86312007-12-11 09:25:06 -05002792 if (path->nodes[*level] == root->node) {
2793 root_owner = root->root_key.objectid;
2794 root_gen =
2795 btrfs_header_generation(path->nodes[*level]);
2796 } else {
2797 struct extent_buffer *node;
2798 node = path->nodes[*level + 1];
2799 root_owner = btrfs_header_owner(node);
2800 root_gen = btrfs_header_generation(node);
2801 }
Chris Masone089f052007-03-16 16:20:31 -04002802 ret = btrfs_free_extent(trans, root,
Chris Masondb945352007-10-15 16:15:53 -04002803 path->nodes[*level]->start,
Chris Mason7bb86312007-12-11 09:25:06 -05002804 path->nodes[*level]->len,
2805 root_owner, root_gen, 0, 0, 1);
Chris Mason6407bf62007-03-27 06:33:00 -04002806 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04002807 free_extent_buffer(path->nodes[*level]);
Chris Mason83e15a22007-03-12 09:03:27 -04002808 path->nodes[*level] = NULL;
Chris Mason20524f02007-03-10 06:35:47 -05002809 *level = i + 1;
Chris Mason20524f02007-03-10 06:35:47 -05002810 }
2811 }
2812 return 1;
2813}
2814
Chris Mason9aca1d52007-03-13 11:09:37 -04002815/*
2816 * drop the reference count on the tree rooted at 'snap'. This traverses
2817 * the tree freeing any blocks that have a ref count of zero after being
2818 * decremented.
2819 */
Chris Masone089f052007-03-16 16:20:31 -04002820int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason9f3a7422007-08-07 15:52:19 -04002821 *root)
Chris Mason20524f02007-03-10 06:35:47 -05002822{
Chris Mason3768f362007-03-13 16:47:54 -04002823 int ret = 0;
Chris Mason9aca1d52007-03-13 11:09:37 -04002824 int wret;
Chris Mason20524f02007-03-10 06:35:47 -05002825 int level;
Chris Mason5caf2a02007-04-02 11:20:42 -04002826 struct btrfs_path *path;
Chris Mason20524f02007-03-10 06:35:47 -05002827 int i;
2828 int orig_level;
Chris Mason9f3a7422007-08-07 15:52:19 -04002829 struct btrfs_root_item *root_item = &root->root_item;
Chris Mason20524f02007-03-10 06:35:47 -05002830
Chris Masona2135012008-06-25 16:01:30 -04002831 WARN_ON(!mutex_is_locked(&root->fs_info->drop_mutex));
Chris Mason5caf2a02007-04-02 11:20:42 -04002832 path = btrfs_alloc_path();
2833 BUG_ON(!path);
Chris Mason20524f02007-03-10 06:35:47 -05002834
Chris Mason5f39d392007-10-15 16:14:19 -04002835 level = btrfs_header_level(root->node);
Chris Mason20524f02007-03-10 06:35:47 -05002836 orig_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04002837 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
2838 path->nodes[level] = root->node;
Chris Masonf510cfe2007-10-15 16:14:48 -04002839 extent_buffer_get(root->node);
Chris Mason9f3a7422007-08-07 15:52:19 -04002840 path->slots[level] = 0;
2841 } else {
2842 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04002843 struct btrfs_disk_key found_key;
2844 struct extent_buffer *node;
Chris Mason6702ed42007-08-07 16:15:09 -04002845
Chris Mason9f3a7422007-08-07 15:52:19 -04002846 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Chris Mason6702ed42007-08-07 16:15:09 -04002847 level = root_item->drop_level;
2848 path->lowest_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04002849 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason6702ed42007-08-07 16:15:09 -04002850 if (wret < 0) {
Chris Mason9f3a7422007-08-07 15:52:19 -04002851 ret = wret;
2852 goto out;
2853 }
Chris Mason5f39d392007-10-15 16:14:19 -04002854 node = path->nodes[level];
2855 btrfs_node_key(node, &found_key, path->slots[level]);
2856 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
2857 sizeof(found_key)));
Chris Mason7d9eb122008-07-08 14:19:17 -04002858 /*
2859 * unlock our path, this is safe because only this
2860 * function is allowed to delete this snapshot
2861 */
Chris Mason925baed2008-06-25 16:01:30 -04002862 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
2863 if (path->nodes[i] && path->locks[i]) {
2864 path->locks[i] = 0;
2865 btrfs_tree_unlock(path->nodes[i]);
2866 }
2867 }
Chris Mason9f3a7422007-08-07 15:52:19 -04002868 }
Chris Mason20524f02007-03-10 06:35:47 -05002869 while(1) {
Chris Mason5caf2a02007-04-02 11:20:42 -04002870 wret = walk_down_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04002871 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05002872 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04002873 if (wret < 0)
2874 ret = wret;
2875
Chris Mason5caf2a02007-04-02 11:20:42 -04002876 wret = walk_up_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04002877 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05002878 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04002879 if (wret < 0)
2880 ret = wret;
Chris Masone7a84562008-06-25 16:01:31 -04002881 if (trans->transaction->in_commit) {
2882 ret = -EAGAIN;
2883 break;
2884 }
Chris Mason18e35e0a2008-08-01 13:11:41 -04002885 atomic_inc(&root->fs_info->throttle_gen);
Chris Mason017e5362008-07-28 15:32:51 -04002886 wake_up(&root->fs_info->transaction_throttle);
Chris Mason20524f02007-03-10 06:35:47 -05002887 }
Chris Mason83e15a22007-03-12 09:03:27 -04002888 for (i = 0; i <= orig_level; i++) {
Chris Mason5caf2a02007-04-02 11:20:42 -04002889 if (path->nodes[i]) {
Chris Mason5f39d392007-10-15 16:14:19 -04002890 free_extent_buffer(path->nodes[i]);
Chris Mason0f827312007-10-15 16:18:56 -04002891 path->nodes[i] = NULL;
Chris Mason83e15a22007-03-12 09:03:27 -04002892 }
Chris Mason20524f02007-03-10 06:35:47 -05002893 }
Chris Mason9f3a7422007-08-07 15:52:19 -04002894out:
Chris Mason5caf2a02007-04-02 11:20:42 -04002895 btrfs_free_path(path);
Chris Mason9aca1d52007-03-13 11:09:37 -04002896 return ret;
Chris Mason20524f02007-03-10 06:35:47 -05002897}
Chris Mason9078a3e2007-04-26 16:46:15 -04002898
Chris Masonbe744172007-05-06 10:15:01 -04002899int btrfs_free_block_groups(struct btrfs_fs_info *info)
2900{
Josef Bacik0f9dd462008-09-23 13:14:11 -04002901 struct btrfs_block_group_cache *block_group;
2902 struct rb_node *n;
Chris Mason925baed2008-06-25 16:01:30 -04002903
2904 mutex_lock(&info->alloc_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002905 spin_lock(&info->block_group_cache_lock);
2906 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
2907 block_group = rb_entry(n, struct btrfs_block_group_cache,
2908 cache_node);
2909
2910 btrfs_remove_free_space_cache(block_group);
2911 rb_erase(&block_group->cache_node,
2912 &info->block_group_cache_tree);
2913 spin_lock(&block_group->space_info->lock);
2914 list_del(&block_group->list);
2915 spin_unlock(&block_group->space_info->lock);
2916 kfree(block_group);
Chris Mason96b51792007-10-15 16:15:19 -04002917 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002918 spin_unlock(&info->block_group_cache_lock);
Chris Mason925baed2008-06-25 16:01:30 -04002919 mutex_unlock(&info->alloc_mutex);
Chris Masonbe744172007-05-06 10:15:01 -04002920 return 0;
2921}
2922
Chris Mason8e7bf942008-04-28 09:02:36 -04002923static unsigned long calc_ra(unsigned long start, unsigned long last,
2924 unsigned long nr)
2925{
2926 return min(last, start + nr - 1);
2927}
2928
Chris Mason98ed5172008-01-03 10:01:48 -05002929static int noinline relocate_inode_pages(struct inode *inode, u64 start,
2930 u64 len)
Chris Masonedbd8d42007-12-21 16:27:24 -05002931{
2932 u64 page_start;
2933 u64 page_end;
Chris Masonedbd8d42007-12-21 16:27:24 -05002934 unsigned long last_index;
Chris Masonedbd8d42007-12-21 16:27:24 -05002935 unsigned long i;
2936 struct page *page;
Chris Masond1310b22008-01-24 16:13:08 -05002937 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason4313b392008-01-03 09:08:48 -05002938 struct file_ra_state *ra;
Chris Mason8e7bf942008-04-28 09:02:36 -04002939 unsigned long total_read = 0;
2940 unsigned long ra_pages;
Chris Mason3eaa2882008-07-24 11:57:52 -04002941 struct btrfs_ordered_extent *ordered;
Chris Masona061fc82008-05-07 11:43:44 -04002942 struct btrfs_trans_handle *trans;
Chris Mason4313b392008-01-03 09:08:48 -05002943
2944 ra = kzalloc(sizeof(*ra), GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05002945
2946 mutex_lock(&inode->i_mutex);
Chris Mason4313b392008-01-03 09:08:48 -05002947 i = start >> PAGE_CACHE_SHIFT;
Chris Masonedbd8d42007-12-21 16:27:24 -05002948 last_index = (start + len - 1) >> PAGE_CACHE_SHIFT;
2949
Chris Mason8e7bf942008-04-28 09:02:36 -04002950 ra_pages = BTRFS_I(inode)->root->fs_info->bdi.ra_pages;
2951
Chris Mason4313b392008-01-03 09:08:48 -05002952 file_ra_state_init(ra, inode->i_mapping);
Chris Masonedbd8d42007-12-21 16:27:24 -05002953
Chris Mason4313b392008-01-03 09:08:48 -05002954 for (; i <= last_index; i++) {
Chris Mason8e7bf942008-04-28 09:02:36 -04002955 if (total_read % ra_pages == 0) {
2956 btrfs_force_ra(inode->i_mapping, ra, NULL, i,
2957 calc_ra(i, last_index, ra_pages));
2958 }
2959 total_read++;
Chris Mason3eaa2882008-07-24 11:57:52 -04002960again:
2961 if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode))
Chris Masona061fc82008-05-07 11:43:44 -04002962 goto truncate_racing;
Chris Masonedbd8d42007-12-21 16:27:24 -05002963 page = grab_cache_page(inode->i_mapping, i);
Chris Masona061fc82008-05-07 11:43:44 -04002964 if (!page) {
Chris Masonedbd8d42007-12-21 16:27:24 -05002965 goto out_unlock;
Chris Masona061fc82008-05-07 11:43:44 -04002966 }
Chris Masonedbd8d42007-12-21 16:27:24 -05002967 if (!PageUptodate(page)) {
2968 btrfs_readpage(NULL, page);
2969 lock_page(page);
2970 if (!PageUptodate(page)) {
2971 unlock_page(page);
2972 page_cache_release(page);
2973 goto out_unlock;
2974 }
2975 }
Chris Masonec44a352008-04-28 15:29:52 -04002976 wait_on_page_writeback(page);
Chris Mason3eaa2882008-07-24 11:57:52 -04002977
Chris Masonedbd8d42007-12-21 16:27:24 -05002978 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
2979 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05002980 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05002981
Chris Mason3eaa2882008-07-24 11:57:52 -04002982 ordered = btrfs_lookup_ordered_extent(inode, page_start);
2983 if (ordered) {
2984 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
2985 unlock_page(page);
2986 page_cache_release(page);
2987 btrfs_start_ordered_extent(inode, ordered, 1);
2988 btrfs_put_ordered_extent(ordered);
2989 goto again;
2990 }
2991 set_page_extent_mapped(page);
2992
Chris Masonf87f0572008-08-01 11:27:23 -04002993 /*
2994 * make sure page_mkwrite is called for this page if userland
2995 * wants to change it from mmap
2996 */
2997 clear_page_dirty_for_io(page);
Chris Mason3eaa2882008-07-24 11:57:52 -04002998
Chris Masonea8c2812008-08-04 23:17:27 -04002999 btrfs_set_extent_delalloc(inode, page_start, page_end);
Chris Masona061fc82008-05-07 11:43:44 -04003000 set_page_dirty(page);
Chris Masonedbd8d42007-12-21 16:27:24 -05003001
Chris Masond1310b22008-01-24 16:13:08 -05003002 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05003003 unlock_page(page);
3004 page_cache_release(page);
3005 }
3006
3007out_unlock:
Chris Mason3eaa2882008-07-24 11:57:52 -04003008 /* we have to start the IO in order to get the ordered extents
3009 * instantiated. This allows the relocation to code to wait
3010 * for all the ordered extents to hit the disk.
3011 *
3012 * Otherwise, it would constantly loop over the same extents
3013 * because the old ones don't get deleted until the IO is
3014 * started
3015 */
3016 btrfs_fdatawrite_range(inode->i_mapping, start, start + len - 1,
3017 WB_SYNC_NONE);
Chris Masonec44a352008-04-28 15:29:52 -04003018 kfree(ra);
Chris Masona061fc82008-05-07 11:43:44 -04003019 trans = btrfs_start_transaction(BTRFS_I(inode)->root, 1);
3020 if (trans) {
Chris Masona061fc82008-05-07 11:43:44 -04003021 btrfs_end_transaction(trans, BTRFS_I(inode)->root);
3022 mark_inode_dirty(inode);
3023 }
Chris Masonedbd8d42007-12-21 16:27:24 -05003024 mutex_unlock(&inode->i_mutex);
3025 return 0;
Chris Masona061fc82008-05-07 11:43:44 -04003026
3027truncate_racing:
3028 vmtruncate(inode, inode->i_size);
3029 balance_dirty_pages_ratelimited_nr(inode->i_mapping,
3030 total_read);
3031 goto out_unlock;
Chris Masonedbd8d42007-12-21 16:27:24 -05003032}
3033
Chris Mason4313b392008-01-03 09:08:48 -05003034/*
Chris Masonbf4ef672008-05-08 13:26:18 -04003035 * The back references tell us which tree holds a ref on a block,
3036 * but it is possible for the tree root field in the reference to
3037 * reflect the original root before a snapshot was made. In this
3038 * case we should search through all the children of a given root
3039 * to find potential holders of references on a block.
3040 *
3041 * Instead, we do something a little less fancy and just search
3042 * all the roots for a given key/block combination.
3043 */
3044static int find_root_for_ref(struct btrfs_root *root,
3045 struct btrfs_path *path,
3046 struct btrfs_key *key0,
3047 int level,
3048 int file_key,
3049 struct btrfs_root **found_root,
3050 u64 bytenr)
3051{
3052 struct btrfs_key root_location;
3053 struct btrfs_root *cur_root = *found_root;
3054 struct btrfs_file_extent_item *file_extent;
3055 u64 root_search_start = BTRFS_FS_TREE_OBJECTID;
3056 u64 found_bytenr;
3057 int ret;
Chris Masonbf4ef672008-05-08 13:26:18 -04003058
3059 root_location.offset = (u64)-1;
3060 root_location.type = BTRFS_ROOT_ITEM_KEY;
3061 path->lowest_level = level;
3062 path->reada = 0;
3063 while(1) {
3064 ret = btrfs_search_slot(NULL, cur_root, key0, path, 0, 0);
3065 found_bytenr = 0;
3066 if (ret == 0 && file_key) {
3067 struct extent_buffer *leaf = path->nodes[0];
3068 file_extent = btrfs_item_ptr(leaf, path->slots[0],
3069 struct btrfs_file_extent_item);
3070 if (btrfs_file_extent_type(leaf, file_extent) ==
3071 BTRFS_FILE_EXTENT_REG) {
3072 found_bytenr =
3073 btrfs_file_extent_disk_bytenr(leaf,
3074 file_extent);
3075 }
Chris Mason323da792008-05-09 11:46:48 -04003076 } else if (!file_key) {
Chris Masonbf4ef672008-05-08 13:26:18 -04003077 if (path->nodes[level])
3078 found_bytenr = path->nodes[level]->start;
3079 }
3080
Chris Masonbf4ef672008-05-08 13:26:18 -04003081 btrfs_release_path(cur_root, path);
3082
3083 if (found_bytenr == bytenr) {
3084 *found_root = cur_root;
3085 ret = 0;
3086 goto out;
3087 }
3088 ret = btrfs_search_root(root->fs_info->tree_root,
3089 root_search_start, &root_search_start);
3090 if (ret)
3091 break;
3092
3093 root_location.objectid = root_search_start;
3094 cur_root = btrfs_read_fs_root_no_name(root->fs_info,
3095 &root_location);
3096 if (!cur_root) {
3097 ret = 1;
3098 break;
3099 }
3100 }
3101out:
3102 path->lowest_level = 0;
3103 return ret;
3104}
3105
3106/*
Chris Mason4313b392008-01-03 09:08:48 -05003107 * note, this releases the path
3108 */
Chris Mason98ed5172008-01-03 10:01:48 -05003109static int noinline relocate_one_reference(struct btrfs_root *extent_root,
Chris Masonedbd8d42007-12-21 16:27:24 -05003110 struct btrfs_path *path,
Chris Mason0ef3e662008-05-24 14:04:53 -04003111 struct btrfs_key *extent_key,
3112 u64 *last_file_objectid,
3113 u64 *last_file_offset,
3114 u64 *last_file_root,
3115 u64 last_extent)
Chris Masonedbd8d42007-12-21 16:27:24 -05003116{
3117 struct inode *inode;
3118 struct btrfs_root *found_root;
Chris Masonbf4ef672008-05-08 13:26:18 -04003119 struct btrfs_key root_location;
3120 struct btrfs_key found_key;
Chris Mason4313b392008-01-03 09:08:48 -05003121 struct btrfs_extent_ref *ref;
3122 u64 ref_root;
3123 u64 ref_gen;
3124 u64 ref_objectid;
3125 u64 ref_offset;
Chris Masonedbd8d42007-12-21 16:27:24 -05003126 int ret;
Chris Masonbf4ef672008-05-08 13:26:18 -04003127 int level;
Chris Masonedbd8d42007-12-21 16:27:24 -05003128
Chris Mason7d9eb122008-07-08 14:19:17 -04003129 WARN_ON(!mutex_is_locked(&extent_root->fs_info->alloc_mutex));
3130
Chris Mason4313b392008-01-03 09:08:48 -05003131 ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
3132 struct btrfs_extent_ref);
3133 ref_root = btrfs_ref_root(path->nodes[0], ref);
3134 ref_gen = btrfs_ref_generation(path->nodes[0], ref);
3135 ref_objectid = btrfs_ref_objectid(path->nodes[0], ref);
3136 ref_offset = btrfs_ref_offset(path->nodes[0], ref);
3137 btrfs_release_path(extent_root, path);
3138
Chris Masonbf4ef672008-05-08 13:26:18 -04003139 root_location.objectid = ref_root;
Chris Masonedbd8d42007-12-21 16:27:24 -05003140 if (ref_gen == 0)
Chris Masonbf4ef672008-05-08 13:26:18 -04003141 root_location.offset = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05003142 else
Chris Masonbf4ef672008-05-08 13:26:18 -04003143 root_location.offset = (u64)-1;
3144 root_location.type = BTRFS_ROOT_ITEM_KEY;
Chris Masonedbd8d42007-12-21 16:27:24 -05003145
3146 found_root = btrfs_read_fs_root_no_name(extent_root->fs_info,
Chris Masonbf4ef672008-05-08 13:26:18 -04003147 &root_location);
Chris Masonedbd8d42007-12-21 16:27:24 -05003148 BUG_ON(!found_root);
Chris Mason7d9eb122008-07-08 14:19:17 -04003149 mutex_unlock(&extent_root->fs_info->alloc_mutex);
Chris Masonedbd8d42007-12-21 16:27:24 -05003150
3151 if (ref_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
Chris Masonbf4ef672008-05-08 13:26:18 -04003152 found_key.objectid = ref_objectid;
3153 found_key.type = BTRFS_EXTENT_DATA_KEY;
3154 found_key.offset = ref_offset;
3155 level = 0;
3156
Chris Mason0ef3e662008-05-24 14:04:53 -04003157 if (last_extent == extent_key->objectid &&
3158 *last_file_objectid == ref_objectid &&
3159 *last_file_offset == ref_offset &&
3160 *last_file_root == ref_root)
3161 goto out;
3162
Chris Masonbf4ef672008-05-08 13:26:18 -04003163 ret = find_root_for_ref(extent_root, path, &found_key,
3164 level, 1, &found_root,
3165 extent_key->objectid);
3166
3167 if (ret)
3168 goto out;
3169
Chris Mason0ef3e662008-05-24 14:04:53 -04003170 if (last_extent == extent_key->objectid &&
3171 *last_file_objectid == ref_objectid &&
3172 *last_file_offset == ref_offset &&
3173 *last_file_root == ref_root)
3174 goto out;
3175
Chris Masonedbd8d42007-12-21 16:27:24 -05003176 inode = btrfs_iget_locked(extent_root->fs_info->sb,
3177 ref_objectid, found_root);
3178 if (inode->i_state & I_NEW) {
3179 /* the inode and parent dir are two different roots */
3180 BTRFS_I(inode)->root = found_root;
3181 BTRFS_I(inode)->location.objectid = ref_objectid;
3182 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
3183 BTRFS_I(inode)->location.offset = 0;
3184 btrfs_read_locked_inode(inode);
3185 unlock_new_inode(inode);
3186
3187 }
3188 /* this can happen if the reference is not against
3189 * the latest version of the tree root
3190 */
Chris Mason7d9eb122008-07-08 14:19:17 -04003191 if (is_bad_inode(inode))
Chris Masonedbd8d42007-12-21 16:27:24 -05003192 goto out;
Chris Mason7d9eb122008-07-08 14:19:17 -04003193
Chris Mason0ef3e662008-05-24 14:04:53 -04003194 *last_file_objectid = inode->i_ino;
3195 *last_file_root = found_root->root_key.objectid;
3196 *last_file_offset = ref_offset;
3197
Chris Masonedbd8d42007-12-21 16:27:24 -05003198 relocate_inode_pages(inode, ref_offset, extent_key->offset);
Chris Masonedbd8d42007-12-21 16:27:24 -05003199 iput(inode);
Chris Masonedbd8d42007-12-21 16:27:24 -05003200 } else {
3201 struct btrfs_trans_handle *trans;
Chris Masonedbd8d42007-12-21 16:27:24 -05003202 struct extent_buffer *eb;
Chris Mason7d9eb122008-07-08 14:19:17 -04003203 int needs_lock = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05003204
Chris Masonedbd8d42007-12-21 16:27:24 -05003205 eb = read_tree_block(found_root, extent_key->objectid,
Chris Masonca7a79a2008-05-12 12:59:19 -04003206 extent_key->offset, 0);
Chris Mason925baed2008-06-25 16:01:30 -04003207 btrfs_tree_lock(eb);
Chris Masonedbd8d42007-12-21 16:27:24 -05003208 level = btrfs_header_level(eb);
3209
3210 if (level == 0)
3211 btrfs_item_key_to_cpu(eb, &found_key, 0);
3212 else
3213 btrfs_node_key_to_cpu(eb, &found_key, 0);
3214
Chris Mason925baed2008-06-25 16:01:30 -04003215 btrfs_tree_unlock(eb);
Chris Masonedbd8d42007-12-21 16:27:24 -05003216 free_extent_buffer(eb);
3217
Chris Masonbf4ef672008-05-08 13:26:18 -04003218 ret = find_root_for_ref(extent_root, path, &found_key,
3219 level, 0, &found_root,
3220 extent_key->objectid);
3221
3222 if (ret)
3223 goto out;
3224
Chris Mason7d9eb122008-07-08 14:19:17 -04003225 /*
3226 * right here almost anything could happen to our key,
3227 * but that's ok. The cow below will either relocate it
3228 * or someone else will have relocated it. Either way,
3229 * it is in a different spot than it was before and
3230 * we're happy.
3231 */
3232
Chris Masonbf4ef672008-05-08 13:26:18 -04003233 trans = btrfs_start_transaction(found_root, 1);
3234
Chris Mason7d9eb122008-07-08 14:19:17 -04003235 if (found_root == extent_root->fs_info->extent_root ||
3236 found_root == extent_root->fs_info->chunk_root ||
3237 found_root == extent_root->fs_info->dev_root) {
3238 needs_lock = 1;
3239 mutex_lock(&extent_root->fs_info->alloc_mutex);
3240 }
3241
Chris Masonedbd8d42007-12-21 16:27:24 -05003242 path->lowest_level = level;
Chris Mason8f662a72008-01-02 10:01:11 -05003243 path->reada = 2;
Chris Masonedbd8d42007-12-21 16:27:24 -05003244 ret = btrfs_search_slot(trans, found_root, &found_key, path,
3245 0, 1);
3246 path->lowest_level = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05003247 btrfs_release_path(found_root, path);
Chris Mason7d9eb122008-07-08 14:19:17 -04003248
Chris Mason0ef3e662008-05-24 14:04:53 -04003249 if (found_root == found_root->fs_info->extent_root)
3250 btrfs_extent_post_op(trans, found_root);
Chris Mason7d9eb122008-07-08 14:19:17 -04003251 if (needs_lock)
3252 mutex_unlock(&extent_root->fs_info->alloc_mutex);
Chris Masonedbd8d42007-12-21 16:27:24 -05003253
Chris Mason7d9eb122008-07-08 14:19:17 -04003254 btrfs_end_transaction(trans, found_root);
3255
3256 }
Chris Masonedbd8d42007-12-21 16:27:24 -05003257out:
Chris Mason7d9eb122008-07-08 14:19:17 -04003258 mutex_lock(&extent_root->fs_info->alloc_mutex);
Chris Masonedbd8d42007-12-21 16:27:24 -05003259 return 0;
3260}
3261
Chris Masona061fc82008-05-07 11:43:44 -04003262static int noinline del_extent_zero(struct btrfs_root *extent_root,
3263 struct btrfs_path *path,
3264 struct btrfs_key *extent_key)
3265{
3266 int ret;
3267 struct btrfs_trans_handle *trans;
3268
3269 trans = btrfs_start_transaction(extent_root, 1);
3270 ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1);
3271 if (ret > 0) {
3272 ret = -EIO;
3273 goto out;
3274 }
3275 if (ret < 0)
3276 goto out;
3277 ret = btrfs_del_item(trans, extent_root, path);
3278out:
3279 btrfs_end_transaction(trans, extent_root);
3280 return ret;
3281}
3282
Chris Mason98ed5172008-01-03 10:01:48 -05003283static int noinline relocate_one_extent(struct btrfs_root *extent_root,
3284 struct btrfs_path *path,
3285 struct btrfs_key *extent_key)
Chris Masonedbd8d42007-12-21 16:27:24 -05003286{
3287 struct btrfs_key key;
3288 struct btrfs_key found_key;
Chris Masonedbd8d42007-12-21 16:27:24 -05003289 struct extent_buffer *leaf;
Chris Mason0ef3e662008-05-24 14:04:53 -04003290 u64 last_file_objectid = 0;
3291 u64 last_file_root = 0;
3292 u64 last_file_offset = (u64)-1;
3293 u64 last_extent = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05003294 u32 nritems;
3295 u32 item_size;
3296 int ret = 0;
3297
Chris Masona061fc82008-05-07 11:43:44 -04003298 if (extent_key->objectid == 0) {
3299 ret = del_extent_zero(extent_root, path, extent_key);
3300 goto out;
3301 }
Chris Masonedbd8d42007-12-21 16:27:24 -05003302 key.objectid = extent_key->objectid;
3303 key.type = BTRFS_EXTENT_REF_KEY;
3304 key.offset = 0;
3305
3306 while(1) {
3307 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
3308
Chris Masonedbd8d42007-12-21 16:27:24 -05003309 if (ret < 0)
3310 goto out;
3311
3312 ret = 0;
3313 leaf = path->nodes[0];
3314 nritems = btrfs_header_nritems(leaf);
Chris Masona061fc82008-05-07 11:43:44 -04003315 if (path->slots[0] == nritems) {
3316 ret = btrfs_next_leaf(extent_root, path);
3317 if (ret > 0) {
3318 ret = 0;
3319 goto out;
3320 }
3321 if (ret < 0)
3322 goto out;
Chris Masonbf4ef672008-05-08 13:26:18 -04003323 leaf = path->nodes[0];
Chris Masona061fc82008-05-07 11:43:44 -04003324 }
Chris Masonedbd8d42007-12-21 16:27:24 -05003325
3326 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masona061fc82008-05-07 11:43:44 -04003327 if (found_key.objectid != extent_key->objectid) {
Chris Masonedbd8d42007-12-21 16:27:24 -05003328 break;
Chris Masona061fc82008-05-07 11:43:44 -04003329 }
Chris Masonedbd8d42007-12-21 16:27:24 -05003330
Chris Masona061fc82008-05-07 11:43:44 -04003331 if (found_key.type != BTRFS_EXTENT_REF_KEY) {
Chris Masonedbd8d42007-12-21 16:27:24 -05003332 break;
Chris Masona061fc82008-05-07 11:43:44 -04003333 }
Chris Masonedbd8d42007-12-21 16:27:24 -05003334
3335 key.offset = found_key.offset + 1;
3336 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
3337
Chris Mason0ef3e662008-05-24 14:04:53 -04003338 ret = relocate_one_reference(extent_root, path, extent_key,
3339 &last_file_objectid,
3340 &last_file_offset,
3341 &last_file_root, last_extent);
Chris Masonedbd8d42007-12-21 16:27:24 -05003342 if (ret)
3343 goto out;
Chris Mason0ef3e662008-05-24 14:04:53 -04003344 last_extent = extent_key->objectid;
Chris Masonedbd8d42007-12-21 16:27:24 -05003345 }
3346 ret = 0;
3347out:
3348 btrfs_release_path(extent_root, path);
3349 return ret;
3350}
3351
Chris Masonec44a352008-04-28 15:29:52 -04003352static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
3353{
3354 u64 num_devices;
3355 u64 stripped = BTRFS_BLOCK_GROUP_RAID0 |
3356 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
3357
Chris Masona061fc82008-05-07 11:43:44 -04003358 num_devices = root->fs_info->fs_devices->num_devices;
Chris Masonec44a352008-04-28 15:29:52 -04003359 if (num_devices == 1) {
3360 stripped |= BTRFS_BLOCK_GROUP_DUP;
3361 stripped = flags & ~stripped;
3362
3363 /* turn raid0 into single device chunks */
3364 if (flags & BTRFS_BLOCK_GROUP_RAID0)
3365 return stripped;
3366
3367 /* turn mirroring into duplication */
3368 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
3369 BTRFS_BLOCK_GROUP_RAID10))
3370 return stripped | BTRFS_BLOCK_GROUP_DUP;
3371 return flags;
3372 } else {
3373 /* they already had raid on here, just return */
Chris Masonec44a352008-04-28 15:29:52 -04003374 if (flags & stripped)
3375 return flags;
3376
3377 stripped |= BTRFS_BLOCK_GROUP_DUP;
3378 stripped = flags & ~stripped;
3379
3380 /* switch duplicated blocks with raid1 */
3381 if (flags & BTRFS_BLOCK_GROUP_DUP)
3382 return stripped | BTRFS_BLOCK_GROUP_RAID1;
3383
3384 /* turn single device chunks into raid0 */
3385 return stripped | BTRFS_BLOCK_GROUP_RAID0;
3386 }
3387 return flags;
3388}
3389
Chris Mason0ef3e662008-05-24 14:04:53 -04003390int __alloc_chunk_for_shrink(struct btrfs_root *root,
3391 struct btrfs_block_group_cache *shrink_block_group,
3392 int force)
3393{
3394 struct btrfs_trans_handle *trans;
3395 u64 new_alloc_flags;
3396 u64 calc;
3397
Chris Masonc286ac42008-07-22 23:06:41 -04003398 spin_lock(&shrink_block_group->lock);
Chris Mason0ef3e662008-05-24 14:04:53 -04003399 if (btrfs_block_group_used(&shrink_block_group->item) > 0) {
Chris Masonc286ac42008-07-22 23:06:41 -04003400 spin_unlock(&shrink_block_group->lock);
Chris Mason7d9eb122008-07-08 14:19:17 -04003401 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Masonc286ac42008-07-22 23:06:41 -04003402
Chris Mason0ef3e662008-05-24 14:04:53 -04003403 trans = btrfs_start_transaction(root, 1);
Chris Mason7d9eb122008-07-08 14:19:17 -04003404 mutex_lock(&root->fs_info->alloc_mutex);
Chris Masonc286ac42008-07-22 23:06:41 -04003405 spin_lock(&shrink_block_group->lock);
Chris Mason7d9eb122008-07-08 14:19:17 -04003406
Chris Mason0ef3e662008-05-24 14:04:53 -04003407 new_alloc_flags = update_block_group_flags(root,
3408 shrink_block_group->flags);
3409 if (new_alloc_flags != shrink_block_group->flags) {
3410 calc =
3411 btrfs_block_group_used(&shrink_block_group->item);
3412 } else {
3413 calc = shrink_block_group->key.offset;
3414 }
Chris Masonc286ac42008-07-22 23:06:41 -04003415 spin_unlock(&shrink_block_group->lock);
3416
Chris Mason0ef3e662008-05-24 14:04:53 -04003417 do_chunk_alloc(trans, root->fs_info->extent_root,
3418 calc + 2 * 1024 * 1024, new_alloc_flags, force);
Chris Mason7d9eb122008-07-08 14:19:17 -04003419
3420 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Mason0ef3e662008-05-24 14:04:53 -04003421 btrfs_end_transaction(trans, root);
Chris Mason7d9eb122008-07-08 14:19:17 -04003422 mutex_lock(&root->fs_info->alloc_mutex);
Chris Masonc286ac42008-07-22 23:06:41 -04003423 } else
3424 spin_unlock(&shrink_block_group->lock);
Chris Mason0ef3e662008-05-24 14:04:53 -04003425 return 0;
3426}
3427
Chris Mason8f18cf12008-04-25 16:53:30 -04003428int btrfs_shrink_extent_tree(struct btrfs_root *root, u64 shrink_start)
Chris Masonedbd8d42007-12-21 16:27:24 -05003429{
3430 struct btrfs_trans_handle *trans;
3431 struct btrfs_root *tree_root = root->fs_info->tree_root;
3432 struct btrfs_path *path;
3433 u64 cur_byte;
3434 u64 total_found;
Chris Mason8f18cf12008-04-25 16:53:30 -04003435 u64 shrink_last_byte;
3436 struct btrfs_block_group_cache *shrink_block_group;
Chris Masonedbd8d42007-12-21 16:27:24 -05003437 struct btrfs_key key;
Yan73e48b22008-01-03 14:14:39 -05003438 struct btrfs_key found_key;
Chris Masonedbd8d42007-12-21 16:27:24 -05003439 struct extent_buffer *leaf;
3440 u32 nritems;
3441 int ret;
Chris Masona061fc82008-05-07 11:43:44 -04003442 int progress;
Chris Masonedbd8d42007-12-21 16:27:24 -05003443
Chris Mason925baed2008-06-25 16:01:30 -04003444 mutex_lock(&root->fs_info->alloc_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04003445 shrink_block_group = btrfs_lookup_block_group(root->fs_info,
3446 shrink_start);
3447 BUG_ON(!shrink_block_group);
3448
Chris Mason0ef3e662008-05-24 14:04:53 -04003449 shrink_last_byte = shrink_block_group->key.objectid +
3450 shrink_block_group->key.offset;
Chris Mason8f18cf12008-04-25 16:53:30 -04003451
3452 shrink_block_group->space_info->total_bytes -=
3453 shrink_block_group->key.offset;
Chris Masonedbd8d42007-12-21 16:27:24 -05003454 path = btrfs_alloc_path();
3455 root = root->fs_info->extent_root;
Chris Mason8f662a72008-01-02 10:01:11 -05003456 path->reada = 2;
Chris Masonedbd8d42007-12-21 16:27:24 -05003457
Chris Mason323da792008-05-09 11:46:48 -04003458 printk("btrfs relocating block group %llu flags %llu\n",
3459 (unsigned long long)shrink_start,
3460 (unsigned long long)shrink_block_group->flags);
3461
Chris Mason0ef3e662008-05-24 14:04:53 -04003462 __alloc_chunk_for_shrink(root, shrink_block_group, 1);
Chris Mason323da792008-05-09 11:46:48 -04003463
Chris Mason0ef3e662008-05-24 14:04:53 -04003464again:
3465
Chris Mason8f18cf12008-04-25 16:53:30 -04003466 shrink_block_group->ro = 1;
3467
Chris Masonedbd8d42007-12-21 16:27:24 -05003468 total_found = 0;
Chris Masona061fc82008-05-07 11:43:44 -04003469 progress = 0;
Chris Mason8f18cf12008-04-25 16:53:30 -04003470 key.objectid = shrink_start;
Chris Masonedbd8d42007-12-21 16:27:24 -05003471 key.offset = 0;
3472 key.type = 0;
Yan73e48b22008-01-03 14:14:39 -05003473 cur_byte = key.objectid;
Chris Mason4313b392008-01-03 09:08:48 -05003474
Chris Masonea8c2812008-08-04 23:17:27 -04003475 mutex_unlock(&root->fs_info->alloc_mutex);
3476
3477 btrfs_start_delalloc_inodes(root);
Yan Zheng7ea394f2008-08-05 13:05:02 -04003478 btrfs_wait_ordered_extents(tree_root, 0);
Chris Masonea8c2812008-08-04 23:17:27 -04003479
3480 mutex_lock(&root->fs_info->alloc_mutex);
3481
Yan73e48b22008-01-03 14:14:39 -05003482 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3483 if (ret < 0)
3484 goto out;
3485
Chris Mason0b86a832008-03-24 15:01:56 -04003486 ret = btrfs_previous_item(root, path, 0, BTRFS_EXTENT_ITEM_KEY);
Yan73e48b22008-01-03 14:14:39 -05003487 if (ret < 0)
3488 goto out;
Chris Mason8f18cf12008-04-25 16:53:30 -04003489
Yan73e48b22008-01-03 14:14:39 -05003490 if (ret == 0) {
3491 leaf = path->nodes[0];
3492 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason8f18cf12008-04-25 16:53:30 -04003493 if (found_key.objectid + found_key.offset > shrink_start &&
3494 found_key.objectid < shrink_last_byte) {
Yan73e48b22008-01-03 14:14:39 -05003495 cur_byte = found_key.objectid;
3496 key.objectid = cur_byte;
3497 }
3498 }
3499 btrfs_release_path(root, path);
3500
3501 while(1) {
Chris Masonedbd8d42007-12-21 16:27:24 -05003502 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3503 if (ret < 0)
3504 goto out;
Yan73e48b22008-01-03 14:14:39 -05003505
Chris Mason7d9eb122008-07-08 14:19:17 -04003506next:
Chris Masonedbd8d42007-12-21 16:27:24 -05003507 leaf = path->nodes[0];
Chris Masonedbd8d42007-12-21 16:27:24 -05003508 nritems = btrfs_header_nritems(leaf);
Yan73e48b22008-01-03 14:14:39 -05003509 if (path->slots[0] >= nritems) {
3510 ret = btrfs_next_leaf(root, path);
3511 if (ret < 0)
3512 goto out;
3513 if (ret == 1) {
3514 ret = 0;
3515 break;
Chris Masonedbd8d42007-12-21 16:27:24 -05003516 }
Yan73e48b22008-01-03 14:14:39 -05003517 leaf = path->nodes[0];
3518 nritems = btrfs_header_nritems(leaf);
3519 }
3520
3521 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason725c8462008-01-04 16:47:16 -05003522
Chris Mason8f18cf12008-04-25 16:53:30 -04003523 if (found_key.objectid >= shrink_last_byte)
3524 break;
3525
Chris Mason725c8462008-01-04 16:47:16 -05003526 if (progress && need_resched()) {
3527 memcpy(&key, &found_key, sizeof(key));
Chris Mason725c8462008-01-04 16:47:16 -05003528 cond_resched();
Chris Mason725c8462008-01-04 16:47:16 -05003529 btrfs_release_path(root, path);
3530 btrfs_search_slot(NULL, root, &key, path, 0, 0);
3531 progress = 0;
3532 goto next;
3533 }
3534 progress = 1;
3535
Yan73e48b22008-01-03 14:14:39 -05003536 if (btrfs_key_type(&found_key) != BTRFS_EXTENT_ITEM_KEY ||
3537 found_key.objectid + found_key.offset <= cur_byte) {
Chris Mason0ef3e662008-05-24 14:04:53 -04003538 memcpy(&key, &found_key, sizeof(key));
3539 key.offset++;
Yan73e48b22008-01-03 14:14:39 -05003540 path->slots[0]++;
Chris Masonedbd8d42007-12-21 16:27:24 -05003541 goto next;
3542 }
Yan73e48b22008-01-03 14:14:39 -05003543
Chris Masonedbd8d42007-12-21 16:27:24 -05003544 total_found++;
3545 cur_byte = found_key.objectid + found_key.offset;
3546 key.objectid = cur_byte;
3547 btrfs_release_path(root, path);
3548 ret = relocate_one_extent(root, path, &found_key);
Chris Mason0ef3e662008-05-24 14:04:53 -04003549 __alloc_chunk_for_shrink(root, shrink_block_group, 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05003550 }
3551
3552 btrfs_release_path(root, path);
3553
3554 if (total_found > 0) {
Chris Mason323da792008-05-09 11:46:48 -04003555 printk("btrfs relocate found %llu last extent was %llu\n",
3556 (unsigned long long)total_found,
3557 (unsigned long long)found_key.objectid);
Chris Mason7d9eb122008-07-08 14:19:17 -04003558 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Masonedbd8d42007-12-21 16:27:24 -05003559 trans = btrfs_start_transaction(tree_root, 1);
3560 btrfs_commit_transaction(trans, tree_root);
3561
Chris Masonedbd8d42007-12-21 16:27:24 -05003562 btrfs_clean_old_snapshots(tree_root);
Chris Masonedbd8d42007-12-21 16:27:24 -05003563
Chris Masonea8c2812008-08-04 23:17:27 -04003564 btrfs_start_delalloc_inodes(root);
Yan Zheng7ea394f2008-08-05 13:05:02 -04003565 btrfs_wait_ordered_extents(tree_root, 0);
Chris Mason3eaa2882008-07-24 11:57:52 -04003566
Chris Masonedbd8d42007-12-21 16:27:24 -05003567 trans = btrfs_start_transaction(tree_root, 1);
3568 btrfs_commit_transaction(trans, tree_root);
Chris Mason7d9eb122008-07-08 14:19:17 -04003569 mutex_lock(&root->fs_info->alloc_mutex);
Chris Masonedbd8d42007-12-21 16:27:24 -05003570 goto again;
3571 }
3572
Chris Mason8f18cf12008-04-25 16:53:30 -04003573 /*
3574 * we've freed all the extents, now remove the block
3575 * group item from the tree
3576 */
Chris Mason7d9eb122008-07-08 14:19:17 -04003577 mutex_unlock(&root->fs_info->alloc_mutex);
3578
Chris Masonedbd8d42007-12-21 16:27:24 -05003579 trans = btrfs_start_transaction(root, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04003580
Chris Mason7d9eb122008-07-08 14:19:17 -04003581 mutex_lock(&root->fs_info->alloc_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04003582 memcpy(&key, &shrink_block_group->key, sizeof(key));
Chris Mason4313b392008-01-03 09:08:48 -05003583
Chris Mason8f18cf12008-04-25 16:53:30 -04003584 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
3585 if (ret > 0)
3586 ret = -EIO;
Josef Bacik8e8a1e32008-07-24 12:17:14 -04003587 if (ret < 0) {
3588 btrfs_end_transaction(trans, root);
Chris Mason8f18cf12008-04-25 16:53:30 -04003589 goto out;
Josef Bacik8e8a1e32008-07-24 12:17:14 -04003590 }
Yan73e48b22008-01-03 14:14:39 -05003591
Josef Bacik0f9dd462008-09-23 13:14:11 -04003592 spin_lock(&root->fs_info->block_group_cache_lock);
3593 rb_erase(&shrink_block_group->cache_node,
3594 &root->fs_info->block_group_cache_tree);
3595 spin_unlock(&root->fs_info->block_group_cache_lock);
Yan73e48b22008-01-03 14:14:39 -05003596
Josef Bacik0f9dd462008-09-23 13:14:11 -04003597 ret = btrfs_remove_free_space(shrink_block_group, key.objectid,
3598 key.offset);
3599 if (ret) {
3600 btrfs_end_transaction(trans, root);
3601 goto out;
3602 }
Chris Masond7a029a2008-08-04 23:17:26 -04003603 /*
Chris Mason0ef3e662008-05-24 14:04:53 -04003604 memset(shrink_block_group, 0, sizeof(*shrink_block_group));
3605 kfree(shrink_block_group);
Chris Masond7a029a2008-08-04 23:17:26 -04003606 */
Chris Mason0ef3e662008-05-24 14:04:53 -04003607
Chris Mason8f18cf12008-04-25 16:53:30 -04003608 btrfs_del_item(trans, root, path);
Chris Mason7d9eb122008-07-08 14:19:17 -04003609 btrfs_release_path(root, path);
3610 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Masonedbd8d42007-12-21 16:27:24 -05003611 btrfs_commit_transaction(trans, root);
Chris Mason0ef3e662008-05-24 14:04:53 -04003612
Chris Mason7d9eb122008-07-08 14:19:17 -04003613 mutex_lock(&root->fs_info->alloc_mutex);
3614
Chris Mason0ef3e662008-05-24 14:04:53 -04003615 /* the code to unpin extents might set a few bits in the free
3616 * space cache for this range again
3617 */
Josef Bacik0f9dd462008-09-23 13:14:11 -04003618 /* XXX? */
3619 ret = btrfs_remove_free_space(shrink_block_group, key.objectid,
3620 key.offset);
Chris Masonedbd8d42007-12-21 16:27:24 -05003621out:
3622 btrfs_free_path(path);
Chris Mason925baed2008-06-25 16:01:30 -04003623 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Masonedbd8d42007-12-21 16:27:24 -05003624 return ret;
3625}
3626
Chris Mason0b86a832008-03-24 15:01:56 -04003627int find_first_block_group(struct btrfs_root *root, struct btrfs_path *path,
3628 struct btrfs_key *key)
3629{
Chris Mason925baed2008-06-25 16:01:30 -04003630 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04003631 struct btrfs_key found_key;
3632 struct extent_buffer *leaf;
3633 int slot;
3634
3635 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
3636 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04003637 goto out;
3638
Chris Mason0b86a832008-03-24 15:01:56 -04003639 while(1) {
3640 slot = path->slots[0];
3641 leaf = path->nodes[0];
3642 if (slot >= btrfs_header_nritems(leaf)) {
3643 ret = btrfs_next_leaf(root, path);
3644 if (ret == 0)
3645 continue;
3646 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04003647 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04003648 break;
3649 }
3650 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3651
3652 if (found_key.objectid >= key->objectid &&
Chris Mason925baed2008-06-25 16:01:30 -04003653 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
3654 ret = 0;
3655 goto out;
3656 }
Chris Mason0b86a832008-03-24 15:01:56 -04003657 path->slots[0]++;
3658 }
3659 ret = -ENOENT;
Chris Mason925baed2008-06-25 16:01:30 -04003660out:
Chris Mason0b86a832008-03-24 15:01:56 -04003661 return ret;
3662}
3663
Chris Mason9078a3e2007-04-26 16:46:15 -04003664int btrfs_read_block_groups(struct btrfs_root *root)
3665{
3666 struct btrfs_path *path;
3667 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04003668 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04003669 struct btrfs_fs_info *info = root->fs_info;
Chris Mason6324fbf2008-03-24 15:01:59 -04003670 struct btrfs_space_info *space_info;
Chris Mason9078a3e2007-04-26 16:46:15 -04003671 struct btrfs_key key;
3672 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -04003673 struct extent_buffer *leaf;
Chris Mason96b51792007-10-15 16:15:19 -04003674
Chris Masonbe744172007-05-06 10:15:01 -04003675 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04003676 key.objectid = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04003677 key.offset = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04003678 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
Chris Mason9078a3e2007-04-26 16:46:15 -04003679 path = btrfs_alloc_path();
3680 if (!path)
3681 return -ENOMEM;
3682
Chris Mason925baed2008-06-25 16:01:30 -04003683 mutex_lock(&root->fs_info->alloc_mutex);
Chris Mason9078a3e2007-04-26 16:46:15 -04003684 while(1) {
Chris Mason0b86a832008-03-24 15:01:56 -04003685 ret = find_first_block_group(root, path, &key);
3686 if (ret > 0) {
3687 ret = 0;
3688 goto error;
Chris Mason9078a3e2007-04-26 16:46:15 -04003689 }
Chris Mason0b86a832008-03-24 15:01:56 -04003690 if (ret != 0)
3691 goto error;
3692
Chris Mason5f39d392007-10-15 16:14:19 -04003693 leaf = path->nodes[0];
3694 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason8f18cf12008-04-25 16:53:30 -04003695 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -04003696 if (!cache) {
Chris Mason0b86a832008-03-24 15:01:56 -04003697 ret = -ENOMEM;
Chris Mason9078a3e2007-04-26 16:46:15 -04003698 break;
3699 }
Chris Mason3e1ad542007-05-07 20:03:49 -04003700
Chris Masonc286ac42008-07-22 23:06:41 -04003701 spin_lock_init(&cache->lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003702 INIT_LIST_HEAD(&cache->list);
Chris Mason5f39d392007-10-15 16:14:19 -04003703 read_extent_buffer(leaf, &cache->item,
3704 btrfs_item_ptr_offset(leaf, path->slots[0]),
3705 sizeof(cache->item));
Chris Mason9078a3e2007-04-26 16:46:15 -04003706 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Mason0b86a832008-03-24 15:01:56 -04003707
Chris Mason9078a3e2007-04-26 16:46:15 -04003708 key.objectid = found_key.objectid + found_key.offset;
3709 btrfs_release_path(root, path);
Chris Mason0b86a832008-03-24 15:01:56 -04003710 cache->flags = btrfs_block_group_flags(&cache->item);
Chris Mason96b51792007-10-15 16:15:19 -04003711
Chris Mason6324fbf2008-03-24 15:01:59 -04003712 ret = update_space_info(info, cache->flags, found_key.offset,
3713 btrfs_block_group_used(&cache->item),
3714 &space_info);
3715 BUG_ON(ret);
3716 cache->space_info = space_info;
Josef Bacik0f9dd462008-09-23 13:14:11 -04003717 spin_lock(&space_info->lock);
3718 list_add(&cache->list, &space_info->block_groups);
3719 spin_unlock(&space_info->lock);
Chris Mason6324fbf2008-03-24 15:01:59 -04003720
Josef Bacik0f9dd462008-09-23 13:14:11 -04003721 ret = btrfs_add_block_group_cache(root->fs_info, cache);
3722 BUG_ON(ret);
3723
Chris Mason9078a3e2007-04-26 16:46:15 -04003724 if (key.objectid >=
Chris Masondb945352007-10-15 16:15:53 -04003725 btrfs_super_total_bytes(&info->super_copy))
Chris Mason9078a3e2007-04-26 16:46:15 -04003726 break;
3727 }
Chris Mason0b86a832008-03-24 15:01:56 -04003728 ret = 0;
3729error:
Chris Mason9078a3e2007-04-26 16:46:15 -04003730 btrfs_free_path(path);
Chris Mason925baed2008-06-25 16:01:30 -04003731 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Mason0b86a832008-03-24 15:01:56 -04003732 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04003733}
Chris Mason6324fbf2008-03-24 15:01:59 -04003734
3735int btrfs_make_block_group(struct btrfs_trans_handle *trans,
3736 struct btrfs_root *root, u64 bytes_used,
Chris Masone17cade2008-04-15 15:41:47 -04003737 u64 type, u64 chunk_objectid, u64 chunk_offset,
Chris Mason6324fbf2008-03-24 15:01:59 -04003738 u64 size)
3739{
3740 int ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04003741 struct btrfs_root *extent_root;
3742 struct btrfs_block_group_cache *cache;
Chris Mason6324fbf2008-03-24 15:01:59 -04003743
Chris Mason7d9eb122008-07-08 14:19:17 -04003744 WARN_ON(!mutex_is_locked(&root->fs_info->alloc_mutex));
Chris Mason6324fbf2008-03-24 15:01:59 -04003745 extent_root = root->fs_info->extent_root;
Chris Mason6324fbf2008-03-24 15:01:59 -04003746
Chris Masone02119d2008-09-05 16:13:11 -04003747 root->fs_info->last_trans_new_blockgroup = trans->transid;
3748
Chris Mason8f18cf12008-04-25 16:53:30 -04003749 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003750 if (!cache)
3751 return -ENOMEM;
3752
Chris Masone17cade2008-04-15 15:41:47 -04003753 cache->key.objectid = chunk_offset;
Chris Mason6324fbf2008-03-24 15:01:59 -04003754 cache->key.offset = size;
Chris Masonc286ac42008-07-22 23:06:41 -04003755 spin_lock_init(&cache->lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003756 INIT_LIST_HEAD(&cache->list);
Chris Mason6324fbf2008-03-24 15:01:59 -04003757 btrfs_set_key_type(&cache->key, BTRFS_BLOCK_GROUP_ITEM_KEY);
Chris Mason0ef3e662008-05-24 14:04:53 -04003758
Chris Mason6324fbf2008-03-24 15:01:59 -04003759 btrfs_set_block_group_used(&cache->item, bytes_used);
Chris Mason6324fbf2008-03-24 15:01:59 -04003760 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
3761 cache->flags = type;
3762 btrfs_set_block_group_flags(&cache->item, type);
3763
3764 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
3765 &cache->space_info);
3766 BUG_ON(ret);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003767 spin_lock(&cache->space_info->lock);
3768 list_add(&cache->list, &cache->space_info->block_groups);
3769 spin_unlock(&cache->space_info->lock);
Chris Mason6324fbf2008-03-24 15:01:59 -04003770
Josef Bacik0f9dd462008-09-23 13:14:11 -04003771 ret = btrfs_add_block_group_cache(root->fs_info, cache);
3772 BUG_ON(ret);
Chris Masonc286ac42008-07-22 23:06:41 -04003773
Chris Mason6324fbf2008-03-24 15:01:59 -04003774 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
3775 sizeof(cache->item));
3776 BUG_ON(ret);
3777
3778 finish_current_insert(trans, extent_root);
3779 ret = del_pending_extents(trans, extent_root);
3780 BUG_ON(ret);
Chris Masond18a2c42008-04-04 15:40:00 -04003781 set_avail_alloc_bits(extent_root->fs_info, type);
Chris Mason925baed2008-06-25 16:01:30 -04003782
Chris Mason6324fbf2008-03-24 15:01:59 -04003783 return 0;
3784}