blob: 3e2f969de42d9883635b0bdaea891ac48d3627a6 [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
Zheng Yan31840ae2008-09-23 13:14:14 -040032#define PENDING_EXTENT_INSERT 0
33#define PENDING_EXTENT_DELETE 1
34#define PENDING_BACKREF_UPDATE 2
35
36struct pending_extent_op {
37 int type;
38 u64 bytenr;
39 u64 num_bytes;
40 u64 parent;
41 u64 orig_parent;
42 u64 generation;
43 u64 orig_generation;
44 int level;
45};
46
Chris Masone089f052007-03-16 16:20:31 -040047static int finish_current_insert(struct btrfs_trans_handle *trans, struct
48 btrfs_root *extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -040049static int del_pending_extents(struct btrfs_trans_handle *trans, struct
50 btrfs_root *extent_root);
Chris Mason925baed2008-06-25 16:01:30 -040051static struct btrfs_block_group_cache *
52__btrfs_find_block_group(struct btrfs_root *root,
53 struct btrfs_block_group_cache *hint,
54 u64 search_start, int data, int owner);
Yand548ee52008-01-03 13:56:30 -050055
Chris Mason925baed2008-06-25 16:01:30 -040056void maybe_lock_mutex(struct btrfs_root *root)
57{
58 if (root != root->fs_info->extent_root &&
59 root != root->fs_info->chunk_root &&
60 root != root->fs_info->dev_root) {
61 mutex_lock(&root->fs_info->alloc_mutex);
62 }
63}
64
65void maybe_unlock_mutex(struct btrfs_root *root)
66{
67 if (root != root->fs_info->extent_root &&
68 root != root->fs_info->chunk_root &&
69 root != root->fs_info->dev_root) {
70 mutex_unlock(&root->fs_info->alloc_mutex);
71 }
72}
Chris Masonfec577f2007-02-26 10:40:21 -050073
Josef Bacik0f9dd462008-09-23 13:14:11 -040074static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
75{
76 return (cache->flags & bits) == bits;
77}
78
79/*
80 * this adds the block group to the fs_info rb tree for the block group
81 * cache
82 */
83int btrfs_add_block_group_cache(struct btrfs_fs_info *info,
84 struct btrfs_block_group_cache *block_group)
85{
86 struct rb_node **p;
87 struct rb_node *parent = NULL;
88 struct btrfs_block_group_cache *cache;
89
90 spin_lock(&info->block_group_cache_lock);
91 p = &info->block_group_cache_tree.rb_node;
92
93 while (*p) {
94 parent = *p;
95 cache = rb_entry(parent, struct btrfs_block_group_cache,
96 cache_node);
97 if (block_group->key.objectid < cache->key.objectid) {
98 p = &(*p)->rb_left;
99 } else if (block_group->key.objectid > cache->key.objectid) {
100 p = &(*p)->rb_right;
101 } else {
102 spin_unlock(&info->block_group_cache_lock);
103 return -EEXIST;
104 }
105 }
106
107 rb_link_node(&block_group->cache_node, parent, p);
108 rb_insert_color(&block_group->cache_node,
109 &info->block_group_cache_tree);
110 spin_unlock(&info->block_group_cache_lock);
111
112 return 0;
113}
114
115/*
116 * This will return the block group at or after bytenr if contains is 0, else
117 * it will return the block group that contains the bytenr
118 */
119static struct btrfs_block_group_cache *
120block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr,
121 int contains)
122{
123 struct btrfs_block_group_cache *cache, *ret = NULL;
124 struct rb_node *n;
125 u64 end, start;
126
127 spin_lock(&info->block_group_cache_lock);
128 n = info->block_group_cache_tree.rb_node;
129
130 while (n) {
131 cache = rb_entry(n, struct btrfs_block_group_cache,
132 cache_node);
133 end = cache->key.objectid + cache->key.offset - 1;
134 start = cache->key.objectid;
135
136 if (bytenr < start) {
137 if (!contains && (!ret || start < ret->key.objectid))
138 ret = cache;
139 n = n->rb_left;
140 } else if (bytenr > start) {
141 if (contains && bytenr <= end) {
142 ret = cache;
143 break;
144 }
145 n = n->rb_right;
146 } else {
147 ret = cache;
148 break;
149 }
150 }
151 spin_unlock(&info->block_group_cache_lock);
152
153 return ret;
154}
155
156/*
157 * this is only called by cache_block_group, since we could have freed extents
158 * we need to check the pinned_extents for any extents that can't be used yet
159 * since their free space will be released as soon as the transaction commits.
160 */
161static int add_new_free_space(struct btrfs_block_group_cache *block_group,
162 struct btrfs_fs_info *info, u64 start, u64 end)
163{
164 u64 extent_start, extent_end, size;
165 int ret;
166
167 while (start < end) {
168 ret = find_first_extent_bit(&info->pinned_extents, start,
169 &extent_start, &extent_end,
170 EXTENT_DIRTY);
171 if (ret)
172 break;
173
174 if (extent_start == start) {
175 start = extent_end + 1;
176 } else if (extent_start > start && extent_start < end) {
177 size = extent_start - start;
178 ret = btrfs_add_free_space(block_group, start, size);
179 BUG_ON(ret);
180 start = extent_end + 1;
181 } else {
182 break;
183 }
184 }
185
186 if (start < end) {
187 size = end - start;
188 ret = btrfs_add_free_space(block_group, start, size);
189 BUG_ON(ret);
190 }
191
192 return 0;
193}
194
Chris Masone37c9e62007-05-09 20:13:14 -0400195static int cache_block_group(struct btrfs_root *root,
196 struct btrfs_block_group_cache *block_group)
197{
198 struct btrfs_path *path;
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400199 int ret = 0;
Chris Masone37c9e62007-05-09 20:13:14 -0400200 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400201 struct extent_buffer *leaf;
Chris Masone37c9e62007-05-09 20:13:14 -0400202 int slot;
Chris Masone37c9e62007-05-09 20:13:14 -0400203 u64 last = 0;
Yan7d7d6062007-09-14 16:15:28 -0400204 u64 first_free;
Chris Masone37c9e62007-05-09 20:13:14 -0400205 int found = 0;
206
Chris Mason00f5c792007-11-30 10:09:33 -0500207 if (!block_group)
208 return 0;
209
Chris Masone37c9e62007-05-09 20:13:14 -0400210 root = root->fs_info->extent_root;
Chris Masone37c9e62007-05-09 20:13:14 -0400211
212 if (block_group->cached)
213 return 0;
Chris Masonf510cfe2007-10-15 16:14:48 -0400214
Chris Masone37c9e62007-05-09 20:13:14 -0400215 path = btrfs_alloc_path();
216 if (!path)
217 return -ENOMEM;
Yan7d7d6062007-09-14 16:15:28 -0400218
Chris Mason2cc58cf2007-08-27 16:49:44 -0400219 path->reada = 2;
Chris Mason5cd57b22008-06-25 16:01:30 -0400220 /*
221 * we get into deadlocks with paths held by callers of this function.
222 * since the alloc_mutex is protecting things right now, just
223 * skip the locking here
224 */
225 path->skip_locking = 1;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400226 first_free = max_t(u64, block_group->key.objectid,
227 BTRFS_SUPER_INFO_OFFSET + BTRFS_SUPER_INFO_SIZE);
Chris Masone37c9e62007-05-09 20:13:14 -0400228 key.objectid = block_group->key.objectid;
Chris Masone37c9e62007-05-09 20:13:14 -0400229 key.offset = 0;
230 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
231 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
232 if (ret < 0)
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400233 goto err;
Chris Mason0b86a832008-03-24 15:01:56 -0400234 ret = btrfs_previous_item(root, path, 0, BTRFS_EXTENT_ITEM_KEY);
Yand548ee52008-01-03 13:56:30 -0500235 if (ret < 0)
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400236 goto err;
Yand548ee52008-01-03 13:56:30 -0500237 if (ret == 0) {
238 leaf = path->nodes[0];
239 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
240 if (key.objectid + key.offset > first_free)
241 first_free = key.objectid + key.offset;
242 }
Chris Masone37c9e62007-05-09 20:13:14 -0400243 while(1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400244 leaf = path->nodes[0];
Chris Masone37c9e62007-05-09 20:13:14 -0400245 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -0400246 if (slot >= btrfs_header_nritems(leaf)) {
Chris Masone37c9e62007-05-09 20:13:14 -0400247 ret = btrfs_next_leaf(root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -0400248 if (ret < 0)
249 goto err;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400250 if (ret == 0)
Chris Masone37c9e62007-05-09 20:13:14 -0400251 continue;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400252 else
Chris Masone37c9e62007-05-09 20:13:14 -0400253 break;
Chris Masone37c9e62007-05-09 20:13:14 -0400254 }
Chris Mason5f39d392007-10-15 16:14:19 -0400255 btrfs_item_key_to_cpu(leaf, &key, slot);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400256 if (key.objectid < block_group->key.objectid)
Yan7d7d6062007-09-14 16:15:28 -0400257 goto next;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400258
Chris Masone37c9e62007-05-09 20:13:14 -0400259 if (key.objectid >= block_group->key.objectid +
Josef Bacik0f9dd462008-09-23 13:14:11 -0400260 block_group->key.offset)
Yan7d7d6062007-09-14 16:15:28 -0400261 break;
Yan7d7d6062007-09-14 16:15:28 -0400262
263 if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) {
264 if (!found) {
265 last = first_free;
266 found = 1;
Chris Masone37c9e62007-05-09 20:13:14 -0400267 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400268
269 add_new_free_space(block_group, root->fs_info, last,
270 key.objectid);
271
Yan7d7d6062007-09-14 16:15:28 -0400272 last = key.objectid + key.offset;
Chris Masone37c9e62007-05-09 20:13:14 -0400273 }
Yan7d7d6062007-09-14 16:15:28 -0400274next:
Chris Masone37c9e62007-05-09 20:13:14 -0400275 path->slots[0]++;
276 }
277
Yan7d7d6062007-09-14 16:15:28 -0400278 if (!found)
279 last = first_free;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400280
281 add_new_free_space(block_group, root->fs_info, last,
282 block_group->key.objectid +
283 block_group->key.offset);
284
Chris Masone37c9e62007-05-09 20:13:14 -0400285 block_group->cached = 1;
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400286 ret = 0;
Chris Mason54aa1f42007-06-22 14:16:25 -0400287err:
Chris Masone37c9e62007-05-09 20:13:14 -0400288 btrfs_free_path(path);
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400289 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -0400290}
291
Josef Bacik0f9dd462008-09-23 13:14:11 -0400292/*
293 * return the block group that starts at or after bytenr
294 */
Chris Mason0ef3e662008-05-24 14:04:53 -0400295struct btrfs_block_group_cache *btrfs_lookup_first_block_group(struct
296 btrfs_fs_info *info,
297 u64 bytenr)
298{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400299 struct btrfs_block_group_cache *cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400300
Josef Bacik0f9dd462008-09-23 13:14:11 -0400301 cache = block_group_cache_tree_search(info, bytenr, 0);
Chris Mason0ef3e662008-05-24 14:04:53 -0400302
Josef Bacik0f9dd462008-09-23 13:14:11 -0400303 return cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400304}
305
Josef Bacik0f9dd462008-09-23 13:14:11 -0400306/*
307 * return the block group that contains teh given bytenr
308 */
Chris Mason5276aed2007-06-11 21:33:38 -0400309struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
310 btrfs_fs_info *info,
Chris Masondb945352007-10-15 16:15:53 -0400311 u64 bytenr)
Chris Masonbe744172007-05-06 10:15:01 -0400312{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400313 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -0400314
Josef Bacik0f9dd462008-09-23 13:14:11 -0400315 cache = block_group_cache_tree_search(info, bytenr, 1);
Chris Mason96b51792007-10-15 16:15:19 -0400316
Josef Bacik0f9dd462008-09-23 13:14:11 -0400317 return cache;
Chris Masonbe744172007-05-06 10:15:01 -0400318}
Chris Mason0b86a832008-03-24 15:01:56 -0400319
Josef Bacik0f9dd462008-09-23 13:14:11 -0400320static int noinline find_free_space(struct btrfs_root *root,
321 struct btrfs_block_group_cache **cache_ret,
322 u64 *start_ret, u64 num, int data)
Chris Masone37c9e62007-05-09 20:13:14 -0400323{
Chris Masone37c9e62007-05-09 20:13:14 -0400324 int ret;
325 struct btrfs_block_group_cache *cache = *cache_ret;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400326 struct btrfs_free_space *info = NULL;
Chris Masone19caa52007-10-15 16:17:44 -0400327 u64 last;
Chris Mason0b86a832008-03-24 15:01:56 -0400328 u64 search_start = *start_ret;
Chris Masone37c9e62007-05-09 20:13:14 -0400329
Chris Mason7d9eb122008-07-08 14:19:17 -0400330 WARN_ON(!mutex_is_locked(&root->fs_info->alloc_mutex));
Chris Mason0ef3e662008-05-24 14:04:53 -0400331 if (!cache)
Chris Mason54aa1f42007-06-22 14:16:25 -0400332 goto out;
Chris Masonf84a8b32007-11-06 10:26:29 -0500333
Josef Bacik0f9dd462008-09-23 13:14:11 -0400334 last = max(search_start, cache->key.objectid);
335
Chris Mason0ef3e662008-05-24 14:04:53 -0400336again:
337 ret = cache_block_group(root, cache);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400338 if (ret)
Chris Mason0ef3e662008-05-24 14:04:53 -0400339 goto out;
Chris Masone19caa52007-10-15 16:17:44 -0400340
Josef Bacik0f9dd462008-09-23 13:14:11 -0400341 if (cache->ro || !block_group_bits(cache, data))
Chris Mason0ef3e662008-05-24 14:04:53 -0400342 goto new_group;
343
Josef Bacik0f9dd462008-09-23 13:14:11 -0400344 info = btrfs_find_free_space(cache, last, num);
345 if (info) {
346 *start_ret = info->offset;
Chris Mason0b86a832008-03-24 15:01:56 -0400347 return 0;
Chris Mason8790d502008-04-03 16:29:03 -0400348 }
Chris Masone37c9e62007-05-09 20:13:14 -0400349
350new_group:
Chris Masone19caa52007-10-15 16:17:44 -0400351 last = cache->key.objectid + cache->key.offset;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400352
Chris Mason0ef3e662008-05-24 14:04:53 -0400353 cache = btrfs_lookup_first_block_group(root->fs_info, last);
Zheng Yane8569812008-09-26 10:05:48 -0400354 if (!cache)
Chris Mason1a2b2ac2007-12-04 13:18:24 -0500355 goto out;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400356
Chris Masone37c9e62007-05-09 20:13:14 -0400357 *cache_ret = cache;
358 goto again;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400359
360out:
361 return -ENOSPC;
Chris Masone37c9e62007-05-09 20:13:14 -0400362}
363
Chris Mason84f54cf2007-06-12 07:43:08 -0400364static u64 div_factor(u64 num, int factor)
365{
Chris Mason257d0ce2007-11-07 21:08:16 -0500366 if (factor == 10)
367 return num;
Chris Mason84f54cf2007-06-12 07:43:08 -0400368 num *= factor;
369 do_div(num, 10);
370 return num;
371}
372
Josef Bacik0f9dd462008-09-23 13:14:11 -0400373static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
374 u64 flags)
Chris Mason6324fbf2008-03-24 15:01:59 -0400375{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400376 struct list_head *head = &info->space_info;
377 struct list_head *cur;
378 struct btrfs_space_info *found;
379 list_for_each(cur, head) {
380 found = list_entry(cur, struct btrfs_space_info, list);
381 if (found->flags == flags)
382 return found;
383 }
384 return NULL;
Chris Mason6324fbf2008-03-24 15:01:59 -0400385}
386
Chris Mason925baed2008-06-25 16:01:30 -0400387static struct btrfs_block_group_cache *
388__btrfs_find_block_group(struct btrfs_root *root,
389 struct btrfs_block_group_cache *hint,
390 u64 search_start, int data, int owner)
Chris Masoncd1bc462007-04-27 10:08:34 -0400391{
Chris Mason96b51792007-10-15 16:15:19 -0400392 struct btrfs_block_group_cache *cache;
Chris Mason31f3c992007-04-30 15:25:45 -0400393 struct btrfs_block_group_cache *found_group = NULL;
Chris Masoncd1bc462007-04-27 10:08:34 -0400394 struct btrfs_fs_info *info = root->fs_info;
395 u64 used;
Chris Mason31f3c992007-04-30 15:25:45 -0400396 u64 last = 0;
Chris Mason96b51792007-10-15 16:15:19 -0400397 u64 free_check;
Chris Mason31f3c992007-04-30 15:25:45 -0400398 int full_search = 0;
Chris Masonbce4eae2008-04-24 14:42:46 -0400399 int factor = 10;
Chris Mason0ef3e662008-05-24 14:04:53 -0400400 int wrapped = 0;
Chris Masonde428b62007-05-18 13:28:27 -0400401
Chris Masona236aed2008-04-29 09:38:00 -0400402 if (data & BTRFS_BLOCK_GROUP_METADATA)
403 factor = 9;
Chris Masonbe744172007-05-06 10:15:01 -0400404
Chris Mason0ef3e662008-05-24 14:04:53 -0400405 if (search_start) {
Chris Masonbe744172007-05-06 10:15:01 -0400406 struct btrfs_block_group_cache *shint;
Chris Mason0ef3e662008-05-24 14:04:53 -0400407 shint = btrfs_lookup_first_block_group(info, search_start);
Chris Mason8f18cf12008-04-25 16:53:30 -0400408 if (shint && block_group_bits(shint, data) && !shint->ro) {
Chris Masonc286ac42008-07-22 23:06:41 -0400409 spin_lock(&shint->lock);
Chris Masonbe744172007-05-06 10:15:01 -0400410 used = btrfs_block_group_used(&shint->item);
Zheng Yane8569812008-09-26 10:05:48 -0400411 if (used + shint->pinned + shint->reserved <
Yan324ae4d2007-11-16 14:57:08 -0500412 div_factor(shint->key.offset, factor)) {
Chris Masonc286ac42008-07-22 23:06:41 -0400413 spin_unlock(&shint->lock);
Chris Masonbe744172007-05-06 10:15:01 -0400414 return shint;
415 }
Chris Masonc286ac42008-07-22 23:06:41 -0400416 spin_unlock(&shint->lock);
Chris Masonbe744172007-05-06 10:15:01 -0400417 }
418 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400419 if (hint && !hint->ro && block_group_bits(hint, data)) {
Chris Masonc286ac42008-07-22 23:06:41 -0400420 spin_lock(&hint->lock);
Chris Mason31f3c992007-04-30 15:25:45 -0400421 used = btrfs_block_group_used(&hint->item);
Zheng Yane8569812008-09-26 10:05:48 -0400422 if (used + hint->pinned + hint->reserved <
Yan324ae4d2007-11-16 14:57:08 -0500423 div_factor(hint->key.offset, factor)) {
Chris Masonc286ac42008-07-22 23:06:41 -0400424 spin_unlock(&hint->lock);
Chris Mason31f3c992007-04-30 15:25:45 -0400425 return hint;
426 }
Chris Masonc286ac42008-07-22 23:06:41 -0400427 spin_unlock(&hint->lock);
Chris Masone19caa52007-10-15 16:17:44 -0400428 last = hint->key.objectid + hint->key.offset;
Chris Mason31f3c992007-04-30 15:25:45 -0400429 } else {
Chris Masone37c9e62007-05-09 20:13:14 -0400430 if (hint)
Chris Mason0ef3e662008-05-24 14:04:53 -0400431 last = max(hint->key.objectid, search_start);
Chris Masone37c9e62007-05-09 20:13:14 -0400432 else
Chris Mason0ef3e662008-05-24 14:04:53 -0400433 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -0400434 }
Chris Mason31f3c992007-04-30 15:25:45 -0400435again:
Zheng Yane8569812008-09-26 10:05:48 -0400436 while (1) {
437 cache = btrfs_lookup_first_block_group(root->fs_info, last);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400438 if (!cache)
Chris Masoncd1bc462007-04-27 10:08:34 -0400439 break;
Chris Mason96b51792007-10-15 16:15:19 -0400440
Chris Masonc286ac42008-07-22 23:06:41 -0400441 spin_lock(&cache->lock);
Chris Mason96b51792007-10-15 16:15:19 -0400442 last = cache->key.objectid + cache->key.offset;
443 used = btrfs_block_group_used(&cache->item);
444
Chris Mason8f18cf12008-04-25 16:53:30 -0400445 if (!cache->ro && block_group_bits(cache, data)) {
Chris Mason0ef3e662008-05-24 14:04:53 -0400446 free_check = div_factor(cache->key.offset, factor);
Zheng Yane8569812008-09-26 10:05:48 -0400447 if (used + cache->pinned + cache->reserved <
448 free_check) {
Chris Mason8790d502008-04-03 16:29:03 -0400449 found_group = cache;
Chris Masonc286ac42008-07-22 23:06:41 -0400450 spin_unlock(&cache->lock);
Chris Mason8790d502008-04-03 16:29:03 -0400451 goto found;
452 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400453 }
Chris Masonc286ac42008-07-22 23:06:41 -0400454 spin_unlock(&cache->lock);
Chris Masonde428b62007-05-18 13:28:27 -0400455 cond_resched();
Chris Masoncd1bc462007-04-27 10:08:34 -0400456 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400457 if (!wrapped) {
458 last = search_start;
459 wrapped = 1;
460 goto again;
461 }
462 if (!full_search && factor < 10) {
Chris Masonbe744172007-05-06 10:15:01 -0400463 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -0400464 full_search = 1;
Chris Mason0ef3e662008-05-24 14:04:53 -0400465 factor = 10;
Chris Mason31f3c992007-04-30 15:25:45 -0400466 goto again;
467 }
Chris Masonbe744172007-05-06 10:15:01 -0400468found:
Chris Mason31f3c992007-04-30 15:25:45 -0400469 return found_group;
Chris Masoncd1bc462007-04-27 10:08:34 -0400470}
471
Chris Mason925baed2008-06-25 16:01:30 -0400472struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
473 struct btrfs_block_group_cache
474 *hint, u64 search_start,
475 int data, int owner)
476{
477
478 struct btrfs_block_group_cache *ret;
Chris Mason925baed2008-06-25 16:01:30 -0400479 ret = __btrfs_find_block_group(root, hint, search_start, data, owner);
Chris Mason925baed2008-06-25 16:01:30 -0400480 return ret;
481}
Josef Bacik0f9dd462008-09-23 13:14:11 -0400482
Chris Masone02119d2008-09-05 16:13:11 -0400483/* simple helper to search for an existing extent at a given offset */
Zheng Yan31840ae2008-09-23 13:14:14 -0400484int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len)
Chris Masone02119d2008-09-05 16:13:11 -0400485{
486 int ret;
487 struct btrfs_key key;
Zheng Yan31840ae2008-09-23 13:14:14 -0400488 struct btrfs_path *path;
Chris Masone02119d2008-09-05 16:13:11 -0400489
Zheng Yan31840ae2008-09-23 13:14:14 -0400490 path = btrfs_alloc_path();
491 BUG_ON(!path);
Chris Masone02119d2008-09-05 16:13:11 -0400492 maybe_lock_mutex(root);
493 key.objectid = start;
494 key.offset = len;
495 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
496 ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
497 0, 0);
498 maybe_unlock_mutex(root);
Zheng Yan31840ae2008-09-23 13:14:14 -0400499 btrfs_free_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -0500500 return ret;
501}
502
Chris Masond8d5f3e2007-12-11 12:42:00 -0500503/*
504 * Back reference rules. Back refs have three main goals:
505 *
506 * 1) differentiate between all holders of references to an extent so that
507 * when a reference is dropped we can make sure it was a valid reference
508 * before freeing the extent.
509 *
510 * 2) Provide enough information to quickly find the holders of an extent
511 * if we notice a given block is corrupted or bad.
512 *
513 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
514 * maintenance. This is actually the same as #2, but with a slightly
515 * different use case.
516 *
517 * File extents can be referenced by:
518 *
519 * - multiple snapshots, subvolumes, or different generations in one subvol
Zheng Yan31840ae2008-09-23 13:14:14 -0400520 * - different files inside a single subvolume
Chris Masond8d5f3e2007-12-11 12:42:00 -0500521 * - different offsets inside a file (bookend extents in file.c)
522 *
523 * The extent ref structure has fields for:
524 *
525 * - Objectid of the subvolume root
526 * - Generation number of the tree holding the reference
527 * - objectid of the file holding the reference
528 * - offset in the file corresponding to the key holding the reference
Zheng Yan31840ae2008-09-23 13:14:14 -0400529 * - number of references holding by parent node (alway 1 for tree blocks)
530 *
531 * Btree leaf may hold multiple references to a file extent. In most cases,
532 * these references are from same file and the corresponding offsets inside
533 * the file are close together. So inode objectid and offset in file are
534 * just hints, they provide hints about where in the btree the references
535 * can be found and when we can stop searching.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500536 *
537 * When a file extent is allocated the fields are filled in:
Zheng Yan31840ae2008-09-23 13:14:14 -0400538 * (root_key.objectid, trans->transid, inode objectid, offset in file, 1)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500539 *
540 * When a leaf is cow'd new references are added for every file extent found
Zheng Yan31840ae2008-09-23 13:14:14 -0400541 * in the leaf. It looks similar to the create case, but trans->transid will
542 * be different when the block is cow'd.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500543 *
Zheng Yan31840ae2008-09-23 13:14:14 -0400544 * (root_key.objectid, trans->transid, inode objectid, offset in file,
545 * number of references in the leaf)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500546 *
Zheng Yan31840ae2008-09-23 13:14:14 -0400547 * Because inode objectid and offset in file are just hints, they are not
548 * used when backrefs are deleted. When a file extent is removed either
549 * during snapshot deletion or file truncation, we find the corresponding
550 * back back reference and check the following fields.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500551 *
Zheng Yan31840ae2008-09-23 13:14:14 -0400552 * (btrfs_header_owner(leaf), btrfs_header_generation(leaf))
Chris Masond8d5f3e2007-12-11 12:42:00 -0500553 *
554 * Btree extents can be referenced by:
555 *
556 * - Different subvolumes
557 * - Different generations of the same subvolume
558 *
Chris Masond8d5f3e2007-12-11 12:42:00 -0500559 * When a tree block is created, back references are inserted:
560 *
Zheng Yan31840ae2008-09-23 13:14:14 -0400561 * (root->root_key.objectid, trans->transid, level, 0, 1)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500562 *
Zheng Yan31840ae2008-09-23 13:14:14 -0400563 * When a tree block is cow'd, new back references are added for all the
564 * blocks it points to. If the tree block isn't in reference counted root,
565 * the old back references are removed. These new back references are of
566 * the form (trans->transid will have increased since creation):
Chris Masond8d5f3e2007-12-11 12:42:00 -0500567 *
Zheng Yan31840ae2008-09-23 13:14:14 -0400568 * (root->root_key.objectid, trans->transid, level, 0, 1)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500569 *
Zheng Yan31840ae2008-09-23 13:14:14 -0400570 * When a backref is in deleting, the following fields are checked:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500571 *
572 * if backref was for a tree root:
Zheng Yan31840ae2008-09-23 13:14:14 -0400573 * (btrfs_header_owner(itself), btrfs_header_generation(itself))
Chris Masond8d5f3e2007-12-11 12:42:00 -0500574 * else
Zheng Yan31840ae2008-09-23 13:14:14 -0400575 * (btrfs_header_owner(parent), btrfs_header_generation(parent))
Chris Masond8d5f3e2007-12-11 12:42:00 -0500576 *
Zheng Yan31840ae2008-09-23 13:14:14 -0400577 * Back Reference Key composing:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500578 *
Zheng Yan31840ae2008-09-23 13:14:14 -0400579 * The key objectid corresponds to the first byte in the extent, the key
580 * type is set to BTRFS_EXTENT_REF_KEY, and the key offset is the first
581 * byte of parent extent. If a extent is tree root, the key offset is set
582 * to the key objectid.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500583 */
Zheng Yan31840ae2008-09-23 13:14:14 -0400584
585static int noinline lookup_extent_backref(struct btrfs_trans_handle *trans,
586 struct btrfs_root *root,
587 struct btrfs_path *path, u64 bytenr,
588 u64 parent, u64 ref_root,
589 u64 ref_generation, int del)
Chris Mason74493f72007-12-11 09:25:06 -0500590{
Chris Mason74493f72007-12-11 09:25:06 -0500591 struct btrfs_key key;
Zheng Yan31840ae2008-09-23 13:14:14 -0400592 struct btrfs_extent_ref *ref;
593 struct extent_buffer *leaf;
Chris Mason74493f72007-12-11 09:25:06 -0500594 int ret;
595
Chris Mason74493f72007-12-11 09:25:06 -0500596 key.objectid = bytenr;
597 key.type = BTRFS_EXTENT_REF_KEY;
Zheng Yan31840ae2008-09-23 13:14:14 -0400598 key.offset = parent;
Chris Mason74493f72007-12-11 09:25:06 -0500599
Zheng Yan31840ae2008-09-23 13:14:14 -0400600 ret = btrfs_search_slot(trans, root, &key, path, del ? -1 : 0, 1);
601 if (ret < 0)
Chris Mason7bb86312007-12-11 09:25:06 -0500602 goto out;
Zheng Yan31840ae2008-09-23 13:14:14 -0400603 if (ret > 0) {
604 ret = -ENOENT;
605 goto out;
606 }
607
608 leaf = path->nodes[0];
609 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
610 if (btrfs_ref_root(leaf, ref) != ref_root ||
611 btrfs_ref_generation(leaf, ref) != ref_generation) {
612 ret = -EIO;
613 WARN_ON(1);
614 goto out;
615 }
616 ret = 0;
617out:
618 return ret;
619}
620
621static int noinline insert_extent_backref(struct btrfs_trans_handle *trans,
622 struct btrfs_root *root,
623 struct btrfs_path *path,
624 u64 bytenr, u64 parent,
625 u64 ref_root, u64 ref_generation,
626 u64 owner_objectid, u64 owner_offset)
627{
628 struct btrfs_key key;
629 struct extent_buffer *leaf;
630 struct btrfs_extent_ref *ref;
631 u32 num_refs;
632 int ret;
633
634 key.objectid = bytenr;
635 key.type = BTRFS_EXTENT_REF_KEY;
636 key.offset = parent;
637
638 ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(*ref));
639 if (ret == 0) {
640 leaf = path->nodes[0];
641 ref = btrfs_item_ptr(leaf, path->slots[0],
642 struct btrfs_extent_ref);
643 btrfs_set_ref_root(leaf, ref, ref_root);
644 btrfs_set_ref_generation(leaf, ref, ref_generation);
645 btrfs_set_ref_objectid(leaf, ref, owner_objectid);
646 btrfs_set_ref_offset(leaf, ref, owner_offset);
647 btrfs_set_ref_num_refs(leaf, ref, 1);
648 } else if (ret == -EEXIST) {
649 u64 existing_owner;
650 BUG_ON(owner_objectid < BTRFS_FIRST_FREE_OBJECTID);
651 leaf = path->nodes[0];
652 ref = btrfs_item_ptr(leaf, path->slots[0],
653 struct btrfs_extent_ref);
654 if (btrfs_ref_root(leaf, ref) != ref_root ||
655 btrfs_ref_generation(leaf, ref) != ref_generation) {
656 ret = -EIO;
657 WARN_ON(1);
658 goto out;
659 }
660
661 num_refs = btrfs_ref_num_refs(leaf, ref);
662 BUG_ON(num_refs == 0);
663 btrfs_set_ref_num_refs(leaf, ref, num_refs + 1);
664
665 existing_owner = btrfs_ref_objectid(leaf, ref);
666 if (existing_owner == owner_objectid &&
667 btrfs_ref_offset(leaf, ref) > owner_offset) {
668 btrfs_set_ref_offset(leaf, ref, owner_offset);
669 } else if (existing_owner != owner_objectid &&
670 existing_owner != BTRFS_MULTIPLE_OBJECTIDS) {
671 btrfs_set_ref_objectid(leaf, ref,
672 BTRFS_MULTIPLE_OBJECTIDS);
673 btrfs_set_ref_offset(leaf, ref, 0);
674 }
675 ret = 0;
676 } else {
677 goto out;
678 }
Chris Mason7bb86312007-12-11 09:25:06 -0500679 btrfs_mark_buffer_dirty(path->nodes[0]);
680out:
681 btrfs_release_path(root, path);
682 return ret;
Chris Mason74493f72007-12-11 09:25:06 -0500683}
684
Zheng Yan31840ae2008-09-23 13:14:14 -0400685static int noinline remove_extent_backref(struct btrfs_trans_handle *trans,
686 struct btrfs_root *root,
687 struct btrfs_path *path)
688{
689 struct extent_buffer *leaf;
690 struct btrfs_extent_ref *ref;
691 u32 num_refs;
692 int ret = 0;
693
694 leaf = path->nodes[0];
695 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
696 num_refs = btrfs_ref_num_refs(leaf, ref);
697 BUG_ON(num_refs == 0);
698 num_refs -= 1;
699 if (num_refs == 0) {
700 ret = btrfs_del_item(trans, root, path);
701 } else {
702 btrfs_set_ref_num_refs(leaf, ref, num_refs);
703 btrfs_mark_buffer_dirty(leaf);
704 }
705 btrfs_release_path(root, path);
706 return ret;
707}
708
709static int __btrfs_update_extent_ref(struct btrfs_trans_handle *trans,
710 struct btrfs_root *root, u64 bytenr,
711 u64 orig_parent, u64 parent,
712 u64 orig_root, u64 ref_root,
713 u64 orig_generation, u64 ref_generation,
714 u64 owner_objectid, u64 owner_offset)
715{
716 int ret;
717 struct btrfs_root *extent_root = root->fs_info->extent_root;
718 struct btrfs_path *path;
719
720 if (root == root->fs_info->extent_root) {
721 struct pending_extent_op *extent_op;
722 u64 num_bytes;
723
724 BUG_ON(owner_objectid >= BTRFS_MAX_LEVEL);
725 num_bytes = btrfs_level_size(root, (int)owner_objectid);
726 if (test_range_bit(&root->fs_info->extent_ins, bytenr,
727 bytenr + num_bytes - 1, EXTENT_LOCKED, 0)) {
728 u64 priv;
729 ret = get_state_private(&root->fs_info->extent_ins,
730 bytenr, &priv);
731 BUG_ON(ret);
732 extent_op = (struct pending_extent_op *)
733 (unsigned long)priv;
734 BUG_ON(extent_op->parent != orig_parent);
735 BUG_ON(extent_op->generation != orig_generation);
736 extent_op->parent = parent;
737 extent_op->generation = ref_generation;
738 } else {
739 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
740 BUG_ON(!extent_op);
741
742 extent_op->type = PENDING_BACKREF_UPDATE;
743 extent_op->bytenr = bytenr;
744 extent_op->num_bytes = num_bytes;
745 extent_op->parent = parent;
746 extent_op->orig_parent = orig_parent;
747 extent_op->generation = ref_generation;
748 extent_op->orig_generation = orig_generation;
749 extent_op->level = (int)owner_objectid;
750
751 set_extent_bits(&root->fs_info->extent_ins,
752 bytenr, bytenr + num_bytes - 1,
753 EXTENT_LOCKED, GFP_NOFS);
754 set_state_private(&root->fs_info->extent_ins,
755 bytenr, (unsigned long)extent_op);
756 }
757 return 0;
758 }
759
760 path = btrfs_alloc_path();
761 if (!path)
762 return -ENOMEM;
763 ret = lookup_extent_backref(trans, extent_root, path,
764 bytenr, orig_parent, orig_root,
765 orig_generation, 1);
766 if (ret)
767 goto out;
768 ret = remove_extent_backref(trans, extent_root, path);
769 if (ret)
770 goto out;
771 ret = insert_extent_backref(trans, extent_root, path, bytenr,
772 parent, ref_root, ref_generation,
773 owner_objectid, owner_offset);
774 BUG_ON(ret);
775 finish_current_insert(trans, extent_root);
776 del_pending_extents(trans, extent_root);
777out:
778 btrfs_free_path(path);
779 return ret;
780}
781
782int btrfs_update_extent_ref(struct btrfs_trans_handle *trans,
783 struct btrfs_root *root, u64 bytenr,
784 u64 orig_parent, u64 parent,
785 u64 ref_root, u64 ref_generation,
786 u64 owner_objectid, u64 owner_offset)
787{
788 int ret;
789 if (ref_root == BTRFS_TREE_LOG_OBJECTID &&
790 owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
791 return 0;
792 maybe_lock_mutex(root);
793 ret = __btrfs_update_extent_ref(trans, root, bytenr, orig_parent,
794 parent, ref_root, ref_root,
795 ref_generation, ref_generation,
796 owner_objectid, owner_offset);
797 maybe_unlock_mutex(root);
798 return ret;
799}
800
Chris Mason925baed2008-06-25 16:01:30 -0400801static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -0400802 struct btrfs_root *root, u64 bytenr,
803 u64 orig_parent, u64 parent,
804 u64 orig_root, u64 ref_root,
805 u64 orig_generation, u64 ref_generation,
806 u64 owner_objectid, u64 owner_offset)
Chris Mason02217ed2007-03-02 16:08:05 -0500807{
Chris Mason5caf2a02007-04-02 11:20:42 -0400808 struct btrfs_path *path;
Chris Mason02217ed2007-03-02 16:08:05 -0500809 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400810 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400811 struct extent_buffer *l;
Chris Mason234b63a2007-03-13 10:46:10 -0400812 struct btrfs_extent_item *item;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400813 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500814
Chris Mason5caf2a02007-04-02 11:20:42 -0400815 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -0400816 if (!path)
817 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -0400818
Chris Mason3c12ac72008-04-21 12:01:38 -0400819 path->reada = 1;
Chris Masondb945352007-10-15 16:15:53 -0400820 key.objectid = bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -0400821 key.type = BTRFS_EXTENT_ITEM_KEY;
822 key.offset = (u64)-1;
823
Chris Mason5caf2a02007-04-02 11:20:42 -0400824 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -0400825 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400826 if (ret < 0)
827 return ret;
Zheng Yan31840ae2008-09-23 13:14:14 -0400828 BUG_ON(ret == 0 || path->slots[0] == 0);
829
830 path->slots[0]--;
Chris Mason5f39d392007-10-15 16:14:19 -0400831 l = path->nodes[0];
Zheng Yan31840ae2008-09-23 13:14:14 -0400832
833 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
834 BUG_ON(key.objectid != bytenr);
835 BUG_ON(key.type != BTRFS_EXTENT_ITEM_KEY);
836
Chris Mason5caf2a02007-04-02 11:20:42 -0400837 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400838 refs = btrfs_extent_refs(l, item);
839 btrfs_set_extent_refs(l, item, refs + 1);
Chris Mason5caf2a02007-04-02 11:20:42 -0400840 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masona28ec192007-03-06 20:08:01 -0500841
Chris Mason5caf2a02007-04-02 11:20:42 -0400842 btrfs_release_path(root->fs_info->extent_root, path);
Chris Mason7bb86312007-12-11 09:25:06 -0500843
Chris Mason3c12ac72008-04-21 12:01:38 -0400844 path->reada = 1;
Zheng Yan31840ae2008-09-23 13:14:14 -0400845 ret = insert_extent_backref(trans, root->fs_info->extent_root,
846 path, bytenr, parent,
847 ref_root, ref_generation,
848 owner_objectid, owner_offset);
Chris Mason7bb86312007-12-11 09:25:06 -0500849 BUG_ON(ret);
Chris Mason9f5fae22007-03-20 14:38:32 -0400850 finish_current_insert(trans, root->fs_info->extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -0400851 del_pending_extents(trans, root->fs_info->extent_root);
Chris Mason74493f72007-12-11 09:25:06 -0500852
853 btrfs_free_path(path);
Chris Mason02217ed2007-03-02 16:08:05 -0500854 return 0;
855}
856
Chris Mason925baed2008-06-25 16:01:30 -0400857int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -0400858 struct btrfs_root *root,
859 u64 bytenr, u64 num_bytes, u64 parent,
860 u64 ref_root, u64 ref_generation,
861 u64 owner_objectid, u64 owner_offset)
Chris Mason925baed2008-06-25 16:01:30 -0400862{
863 int ret;
Zheng Yan31840ae2008-09-23 13:14:14 -0400864 if (ref_root == BTRFS_TREE_LOG_OBJECTID &&
865 owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
866 return 0;
867 maybe_lock_mutex(root);
868 ret = __btrfs_inc_extent_ref(trans, root, bytenr, 0, parent,
869 0, ref_root, 0, ref_generation,
870 owner_objectid, owner_offset);
871 maybe_unlock_mutex(root);
Chris Mason925baed2008-06-25 16:01:30 -0400872 return ret;
873}
874
Chris Masone9d0b132007-08-10 14:06:19 -0400875int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
876 struct btrfs_root *root)
877{
878 finish_current_insert(trans, root->fs_info->extent_root);
879 del_pending_extents(trans, root->fs_info->extent_root);
880 return 0;
881}
882
Zheng Yan31840ae2008-09-23 13:14:14 -0400883int btrfs_lookup_extent_ref(struct btrfs_trans_handle *trans,
884 struct btrfs_root *root, u64 bytenr,
885 u64 num_bytes, u32 *refs)
Chris Masona28ec192007-03-06 20:08:01 -0500886{
Chris Mason5caf2a02007-04-02 11:20:42 -0400887 struct btrfs_path *path;
Chris Masona28ec192007-03-06 20:08:01 -0500888 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400889 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400890 struct extent_buffer *l;
Chris Mason234b63a2007-03-13 10:46:10 -0400891 struct btrfs_extent_item *item;
Chris Mason5caf2a02007-04-02 11:20:42 -0400892
Chris Masondb945352007-10-15 16:15:53 -0400893 WARN_ON(num_bytes < root->sectorsize);
Chris Mason5caf2a02007-04-02 11:20:42 -0400894 path = btrfs_alloc_path();
Chris Mason3c12ac72008-04-21 12:01:38 -0400895 path->reada = 1;
Chris Masondb945352007-10-15 16:15:53 -0400896 key.objectid = bytenr;
897 key.offset = num_bytes;
Chris Mason62e27492007-03-15 12:56:47 -0400898 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Mason5caf2a02007-04-02 11:20:42 -0400899 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -0400900 0, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400901 if (ret < 0)
902 goto out;
Chris Mason5f39d392007-10-15 16:14:19 -0400903 if (ret != 0) {
904 btrfs_print_leaf(root, path->nodes[0]);
Chris Masondb945352007-10-15 16:15:53 -0400905 printk("failed to find block number %Lu\n", bytenr);
Chris Masona28ec192007-03-06 20:08:01 -0500906 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -0400907 }
908 l = path->nodes[0];
Chris Mason5caf2a02007-04-02 11:20:42 -0400909 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400910 *refs = btrfs_extent_refs(l, item);
Chris Mason54aa1f42007-06-22 14:16:25 -0400911out:
Chris Mason5caf2a02007-04-02 11:20:42 -0400912 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -0500913 return 0;
914}
915
Yan Zhengf321e492008-07-30 09:26:11 -0400916static int get_reference_status(struct btrfs_root *root, u64 bytenr,
917 u64 parent_gen, u64 ref_objectid,
918 u64 *min_generation, u32 *ref_count)
Chris Masonbe20aa92007-12-17 20:14:01 -0500919{
920 struct btrfs_root *extent_root = root->fs_info->extent_root;
921 struct btrfs_path *path;
Yan Zhengf321e492008-07-30 09:26:11 -0400922 struct extent_buffer *leaf;
923 struct btrfs_extent_ref *ref_item;
Chris Masonbe20aa92007-12-17 20:14:01 -0500924 struct btrfs_key key;
925 struct btrfs_key found_key;
Yan Zhengf321e492008-07-30 09:26:11 -0400926 u64 root_objectid = root->root_key.objectid;
927 u64 ref_generation;
928 u32 nritems;
929 int ret;
Chris Masonbe20aa92007-12-17 20:14:01 -0500930
Chris Masonbe20aa92007-12-17 20:14:01 -0500931 key.objectid = bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -0400932 key.offset = (u64)-1;
Yan Zhengf321e492008-07-30 09:26:11 -0400933 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masonbe20aa92007-12-17 20:14:01 -0500934
Yan Zhengf321e492008-07-30 09:26:11 -0400935 path = btrfs_alloc_path();
936 mutex_lock(&root->fs_info->alloc_mutex);
Chris Masonbe20aa92007-12-17 20:14:01 -0500937 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
938 if (ret < 0)
939 goto out;
940 BUG_ON(ret == 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400941 if (ret < 0 || path->slots[0] == 0)
942 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -0500943
Zheng Yan31840ae2008-09-23 13:14:14 -0400944 path->slots[0]--;
Yan Zhengf321e492008-07-30 09:26:11 -0400945 leaf = path->nodes[0];
946 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -0500947
948 if (found_key.objectid != bytenr ||
949 found_key.type != BTRFS_EXTENT_ITEM_KEY) {
Yan Zhengf321e492008-07-30 09:26:11 -0400950 ret = 1;
Chris Masonbe20aa92007-12-17 20:14:01 -0500951 goto out;
952 }
953
Yan Zhengf321e492008-07-30 09:26:11 -0400954 *ref_count = 0;
955 *min_generation = (u64)-1;
956
Chris Masonbe20aa92007-12-17 20:14:01 -0500957 while (1) {
Yan Zhengf321e492008-07-30 09:26:11 -0400958 leaf = path->nodes[0];
959 nritems = btrfs_header_nritems(leaf);
Chris Masonbe20aa92007-12-17 20:14:01 -0500960 if (path->slots[0] >= nritems) {
961 ret = btrfs_next_leaf(extent_root, path);
Yan Zhengf321e492008-07-30 09:26:11 -0400962 if (ret < 0)
963 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -0500964 if (ret == 0)
965 continue;
966 break;
967 }
Yan Zhengf321e492008-07-30 09:26:11 -0400968 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -0500969 if (found_key.objectid != bytenr)
970 break;
Chris Masonbd098352008-01-03 13:23:19 -0500971
Chris Masonbe20aa92007-12-17 20:14:01 -0500972 if (found_key.type != BTRFS_EXTENT_REF_KEY) {
973 path->slots[0]++;
974 continue;
975 }
976
Yan Zhengf321e492008-07-30 09:26:11 -0400977 ref_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masonbe20aa92007-12-17 20:14:01 -0500978 struct btrfs_extent_ref);
Yan Zhengf321e492008-07-30 09:26:11 -0400979 ref_generation = btrfs_ref_generation(leaf, ref_item);
980 /*
Zheng Yan31840ae2008-09-23 13:14:14 -0400981 * For (parent_gen > 0 && parent_gen > ref_generation):
Yan Zhengf321e492008-07-30 09:26:11 -0400982 *
Yanbcc63ab2008-07-30 16:29:20 -0400983 * we reach here through the oldest root, therefore
984 * all other reference from same snapshot should have
Yan Zhengf321e492008-07-30 09:26:11 -0400985 * a larger generation.
986 */
987 if ((root_objectid != btrfs_ref_root(leaf, ref_item)) ||
988 (parent_gen > 0 && parent_gen > ref_generation) ||
989 (ref_objectid >= BTRFS_FIRST_FREE_OBJECTID &&
990 ref_objectid != btrfs_ref_objectid(leaf, ref_item))) {
Zheng Yan31840ae2008-09-23 13:14:14 -0400991 *ref_count = 2;
Yan Zhengf321e492008-07-30 09:26:11 -0400992 break;
993 }
Chris Masonbe20aa92007-12-17 20:14:01 -0500994
Yan Zhengf321e492008-07-30 09:26:11 -0400995 *ref_count = 1;
996 if (*min_generation > ref_generation)
997 *min_generation = ref_generation;
998
Chris Masonbe20aa92007-12-17 20:14:01 -0500999 path->slots[0]++;
1000 }
Yan Zhengf321e492008-07-30 09:26:11 -04001001 ret = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001002out:
Chris Mason925baed2008-06-25 16:01:30 -04001003 mutex_unlock(&root->fs_info->alloc_mutex);
Yan Zhengf321e492008-07-30 09:26:11 -04001004 btrfs_free_path(path);
1005 return ret;
1006}
1007
Yan Zheng7ea394f2008-08-05 13:05:02 -04001008int btrfs_cross_ref_exists(struct btrfs_trans_handle *trans,
1009 struct btrfs_root *root,
Yan Zhengf321e492008-07-30 09:26:11 -04001010 struct btrfs_key *key, u64 bytenr)
1011{
Yan Zhengf321e492008-07-30 09:26:11 -04001012 struct btrfs_root *old_root;
1013 struct btrfs_path *path = NULL;
1014 struct extent_buffer *eb;
1015 struct btrfs_file_extent_item *item;
1016 u64 ref_generation;
1017 u64 min_generation;
1018 u64 extent_start;
1019 u32 ref_count;
1020 int level;
1021 int ret;
1022
Yan Zheng7ea394f2008-08-05 13:05:02 -04001023 BUG_ON(trans == NULL);
Yan Zhengf321e492008-07-30 09:26:11 -04001024 BUG_ON(key->type != BTRFS_EXTENT_DATA_KEY);
1025 ret = get_reference_status(root, bytenr, 0, key->objectid,
1026 &min_generation, &ref_count);
1027 if (ret)
1028 return ret;
1029
1030 if (ref_count != 1)
1031 return 1;
1032
Yan Zhengf321e492008-07-30 09:26:11 -04001033 old_root = root->dirty_root->root;
1034 ref_generation = old_root->root_key.offset;
1035
1036 /* all references are created in running transaction */
1037 if (min_generation > ref_generation) {
1038 ret = 0;
1039 goto out;
1040 }
1041
1042 path = btrfs_alloc_path();
1043 if (!path) {
1044 ret = -ENOMEM;
1045 goto out;
1046 }
1047
1048 path->skip_locking = 1;
1049 /* if no item found, the extent is referenced by other snapshot */
1050 ret = btrfs_search_slot(NULL, old_root, key, path, 0, 0);
1051 if (ret)
1052 goto out;
1053
1054 eb = path->nodes[0];
1055 item = btrfs_item_ptr(eb, path->slots[0],
1056 struct btrfs_file_extent_item);
1057 if (btrfs_file_extent_type(eb, item) != BTRFS_FILE_EXTENT_REG ||
1058 btrfs_file_extent_disk_bytenr(eb, item) != bytenr) {
1059 ret = 1;
1060 goto out;
1061 }
1062
1063 for (level = BTRFS_MAX_LEVEL - 1; level >= -1; level--) {
1064 if (level >= 0) {
1065 eb = path->nodes[level];
1066 if (!eb)
1067 continue;
1068 extent_start = eb->start;
Yanbcc63ab2008-07-30 16:29:20 -04001069 } else
Yan Zhengf321e492008-07-30 09:26:11 -04001070 extent_start = bytenr;
1071
1072 ret = get_reference_status(root, extent_start, ref_generation,
1073 0, &min_generation, &ref_count);
1074 if (ret)
1075 goto out;
1076
1077 if (ref_count != 1) {
1078 ret = 1;
1079 goto out;
1080 }
1081 if (level >= 0)
1082 ref_generation = btrfs_header_generation(eb);
1083 }
1084 ret = 0;
1085out:
1086 if (path)
1087 btrfs_free_path(path);
Yan Zhengf321e492008-07-30 09:26:11 -04001088 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001089}
Chris Masonc5739bb2007-04-10 09:27:04 -04001090
Zheng Yan31840ae2008-09-23 13:14:14 -04001091int btrfs_cache_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1092 struct extent_buffer *buf, u32 nr_extents)
Chris Mason02217ed2007-03-02 16:08:05 -05001093{
Chris Mason5f39d392007-10-15 16:14:19 -04001094 u32 nritems;
1095 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -04001096 struct btrfs_file_extent_item *fi;
Chris Mason02217ed2007-03-02 16:08:05 -05001097 int i;
Chris Masondb945352007-10-15 16:15:53 -04001098 int level;
Zheng Yan31840ae2008-09-23 13:14:14 -04001099 int ret = 0;
Chris Masona28ec192007-03-06 20:08:01 -05001100
Chris Mason3768f362007-03-13 16:47:54 -04001101 if (!root->ref_cows)
Chris Masona28ec192007-03-06 20:08:01 -05001102 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001103
Chris Masondb945352007-10-15 16:15:53 -04001104 level = btrfs_header_level(buf);
Chris Mason5f39d392007-10-15 16:14:19 -04001105 nritems = btrfs_header_nritems(buf);
Chris Mason4a096752008-07-21 10:29:44 -04001106
Zheng Yan31840ae2008-09-23 13:14:14 -04001107 if (level == 0) {
Yan Zheng31153d82008-07-28 15:32:19 -04001108 struct btrfs_leaf_ref *ref;
1109 struct btrfs_extent_info *info;
1110
Zheng Yan31840ae2008-09-23 13:14:14 -04001111 ref = btrfs_alloc_leaf_ref(root, nr_extents);
Yan Zheng31153d82008-07-28 15:32:19 -04001112 if (!ref) {
Zheng Yan31840ae2008-09-23 13:14:14 -04001113 ret = -ENOMEM;
Yan Zheng31153d82008-07-28 15:32:19 -04001114 goto out;
1115 }
1116
Chris Mason47ac14f2008-07-31 09:46:18 -04001117 ref->root_gen = root->root_key.offset;
Yan Zheng31153d82008-07-28 15:32:19 -04001118 ref->bytenr = buf->start;
1119 ref->owner = btrfs_header_owner(buf);
1120 ref->generation = btrfs_header_generation(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04001121 ref->nritems = nr_extents;
Yan Zheng31153d82008-07-28 15:32:19 -04001122 info = ref->extents;
Yanbcc63ab2008-07-30 16:29:20 -04001123
Zheng Yan31840ae2008-09-23 13:14:14 -04001124 for (i = 0; nr_extents > 0 && i < nritems; i++) {
Yan Zheng31153d82008-07-28 15:32:19 -04001125 u64 disk_bytenr;
1126 btrfs_item_key_to_cpu(buf, &key, i);
1127 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1128 continue;
1129 fi = btrfs_item_ptr(buf, i,
1130 struct btrfs_file_extent_item);
1131 if (btrfs_file_extent_type(buf, fi) ==
1132 BTRFS_FILE_EXTENT_INLINE)
1133 continue;
1134 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1135 if (disk_bytenr == 0)
1136 continue;
1137
1138 info->bytenr = disk_bytenr;
1139 info->num_bytes =
1140 btrfs_file_extent_disk_num_bytes(buf, fi);
1141 info->objectid = key.objectid;
1142 info->offset = key.offset;
1143 info++;
1144 }
1145
1146 BUG_ON(!root->ref_tree);
1147 ret = btrfs_add_leaf_ref(root, ref);
1148 WARN_ON(ret);
Yanbcc63ab2008-07-30 16:29:20 -04001149 btrfs_free_leaf_ref(root, ref);
Yan Zheng31153d82008-07-28 15:32:19 -04001150 }
1151out:
Zheng Yan31840ae2008-09-23 13:14:14 -04001152 return ret;
1153}
1154
1155int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1156 struct extent_buffer *orig_buf, struct extent_buffer *buf,
1157 u32 *nr_extents)
1158{
1159 u64 bytenr;
1160 u64 ref_root;
1161 u64 orig_root;
1162 u64 ref_generation;
1163 u64 orig_generation;
1164 u32 nritems;
1165 u32 nr_file_extents = 0;
1166 struct btrfs_key key;
1167 struct btrfs_file_extent_item *fi;
1168 int i;
1169 int level;
1170 int ret = 0;
1171 int faili = 0;
1172 int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
1173 u64, u64, u64, u64, u64, u64, u64, u64, u64);
1174
1175 ref_root = btrfs_header_owner(buf);
1176 ref_generation = btrfs_header_generation(buf);
1177 orig_root = btrfs_header_owner(orig_buf);
1178 orig_generation = btrfs_header_generation(orig_buf);
1179
1180 nritems = btrfs_header_nritems(buf);
1181 level = btrfs_header_level(buf);
1182
1183 if (root->ref_cows) {
1184 process_func = __btrfs_inc_extent_ref;
1185 } else {
1186 if (level == 0 &&
1187 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
1188 goto out;
1189 if (level != 0 &&
1190 root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID)
1191 goto out;
1192 process_func = __btrfs_update_extent_ref;
1193 }
1194
1195 for (i = 0; i < nritems; i++) {
1196 cond_resched();
Chris Masondb945352007-10-15 16:15:53 -04001197 if (level == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001198 btrfs_item_key_to_cpu(buf, &key, i);
1199 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -04001200 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04001201 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -04001202 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001203 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -04001204 BTRFS_FILE_EXTENT_INLINE)
1205 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04001206 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1207 if (bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -04001208 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04001209
1210 nr_file_extents++;
1211
1212 maybe_lock_mutex(root);
1213 ret = process_func(trans, root, bytenr,
1214 orig_buf->start, buf->start,
1215 orig_root, ref_root,
1216 orig_generation, ref_generation,
1217 key.objectid, key.offset);
1218 maybe_unlock_mutex(root);
1219
1220 if (ret) {
1221 faili = i;
1222 WARN_ON(1);
1223 goto fail;
1224 }
Chris Mason54aa1f42007-06-22 14:16:25 -04001225 } else {
Chris Masondb945352007-10-15 16:15:53 -04001226 bytenr = btrfs_node_blockptr(buf, i);
Zheng Yan31840ae2008-09-23 13:14:14 -04001227 maybe_lock_mutex(root);
1228 ret = process_func(trans, root, bytenr,
1229 orig_buf->start, buf->start,
1230 orig_root, ref_root,
1231 orig_generation, ref_generation,
1232 level - 1, 0);
1233 maybe_unlock_mutex(root);
1234 if (ret) {
1235 faili = i;
1236 WARN_ON(1);
1237 goto fail;
1238 }
Chris Mason54aa1f42007-06-22 14:16:25 -04001239 }
1240 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001241out:
1242 if (nr_extents) {
1243 if (level == 0)
1244 *nr_extents = nr_file_extents;
1245 else
1246 *nr_extents = nritems;
1247 }
1248 return 0;
1249fail:
1250 WARN_ON(1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001251 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05001252}
1253
Zheng Yan31840ae2008-09-23 13:14:14 -04001254int btrfs_update_ref(struct btrfs_trans_handle *trans,
1255 struct btrfs_root *root, struct extent_buffer *orig_buf,
1256 struct extent_buffer *buf, int start_slot, int nr)
1257
1258{
1259 u64 bytenr;
1260 u64 ref_root;
1261 u64 orig_root;
1262 u64 ref_generation;
1263 u64 orig_generation;
1264 struct btrfs_key key;
1265 struct btrfs_file_extent_item *fi;
1266 int i;
1267 int ret;
1268 int slot;
1269 int level;
1270
1271 BUG_ON(start_slot < 0);
1272 BUG_ON(start_slot + nr > btrfs_header_nritems(buf));
1273
1274 ref_root = btrfs_header_owner(buf);
1275 ref_generation = btrfs_header_generation(buf);
1276 orig_root = btrfs_header_owner(orig_buf);
1277 orig_generation = btrfs_header_generation(orig_buf);
1278 level = btrfs_header_level(buf);
1279
1280 if (!root->ref_cows) {
1281 if (level == 0 &&
1282 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
1283 return 0;
1284 if (level != 0 &&
1285 root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID)
1286 return 0;
1287 }
1288
1289 for (i = 0, slot = start_slot; i < nr; i++, slot++) {
1290 cond_resched();
1291 if (level == 0) {
1292 btrfs_item_key_to_cpu(buf, &key, slot);
1293 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1294 continue;
1295 fi = btrfs_item_ptr(buf, slot,
1296 struct btrfs_file_extent_item);
1297 if (btrfs_file_extent_type(buf, fi) ==
1298 BTRFS_FILE_EXTENT_INLINE)
1299 continue;
1300 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1301 if (bytenr == 0)
1302 continue;
1303 maybe_lock_mutex(root);
1304 ret = __btrfs_update_extent_ref(trans, root, bytenr,
1305 orig_buf->start, buf->start,
1306 orig_root, ref_root,
1307 orig_generation, ref_generation,
1308 key.objectid, key.offset);
1309 maybe_unlock_mutex(root);
1310 if (ret)
1311 goto fail;
1312 } else {
1313 bytenr = btrfs_node_blockptr(buf, slot);
1314 maybe_lock_mutex(root);
1315 ret = __btrfs_update_extent_ref(trans, root, bytenr,
1316 orig_buf->start, buf->start,
1317 orig_root, ref_root,
1318 orig_generation, ref_generation,
1319 level - 1, 0);
1320 maybe_unlock_mutex(root);
1321 if (ret)
1322 goto fail;
1323 }
1324 }
1325 return 0;
1326fail:
1327 WARN_ON(1);
1328 return -1;
1329}
1330
Chris Mason9078a3e2007-04-26 16:46:15 -04001331static int write_one_cache_group(struct btrfs_trans_handle *trans,
1332 struct btrfs_root *root,
1333 struct btrfs_path *path,
1334 struct btrfs_block_group_cache *cache)
1335{
1336 int ret;
1337 int pending_ret;
1338 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04001339 unsigned long bi;
1340 struct extent_buffer *leaf;
Chris Mason9078a3e2007-04-26 16:46:15 -04001341
Chris Mason9078a3e2007-04-26 16:46:15 -04001342 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001343 if (ret < 0)
1344 goto fail;
Chris Mason9078a3e2007-04-26 16:46:15 -04001345 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04001346
1347 leaf = path->nodes[0];
1348 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
1349 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
1350 btrfs_mark_buffer_dirty(leaf);
Chris Mason9078a3e2007-04-26 16:46:15 -04001351 btrfs_release_path(extent_root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -04001352fail:
Chris Mason9078a3e2007-04-26 16:46:15 -04001353 finish_current_insert(trans, extent_root);
1354 pending_ret = del_pending_extents(trans, extent_root);
1355 if (ret)
1356 return ret;
1357 if (pending_ret)
1358 return pending_ret;
1359 return 0;
1360
1361}
1362
Chris Mason96b51792007-10-15 16:15:19 -04001363int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
1364 struct btrfs_root *root)
Chris Mason9078a3e2007-04-26 16:46:15 -04001365{
Josef Bacik0f9dd462008-09-23 13:14:11 -04001366 struct btrfs_block_group_cache *cache, *entry;
1367 struct rb_node *n;
Chris Mason9078a3e2007-04-26 16:46:15 -04001368 int err = 0;
1369 int werr = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04001370 struct btrfs_path *path;
Chris Mason96b51792007-10-15 16:15:19 -04001371 u64 last = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04001372
1373 path = btrfs_alloc_path();
1374 if (!path)
1375 return -ENOMEM;
1376
Chris Mason925baed2008-06-25 16:01:30 -04001377 mutex_lock(&root->fs_info->alloc_mutex);
Chris Mason9078a3e2007-04-26 16:46:15 -04001378 while(1) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001379 cache = NULL;
1380 spin_lock(&root->fs_info->block_group_cache_lock);
1381 for (n = rb_first(&root->fs_info->block_group_cache_tree);
1382 n; n = rb_next(n)) {
1383 entry = rb_entry(n, struct btrfs_block_group_cache,
1384 cache_node);
1385 if (entry->dirty) {
1386 cache = entry;
1387 break;
1388 }
1389 }
1390 spin_unlock(&root->fs_info->block_group_cache_lock);
1391
1392 if (!cache)
Chris Mason9078a3e2007-04-26 16:46:15 -04001393 break;
Chris Mason54aa1f42007-06-22 14:16:25 -04001394
Zheng Yane8569812008-09-26 10:05:48 -04001395 cache->dirty = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001396 last += cache->key.offset;
1397
Chris Mason96b51792007-10-15 16:15:19 -04001398 err = write_one_cache_group(trans, root,
1399 path, cache);
1400 /*
1401 * if we fail to write the cache group, we want
1402 * to keep it marked dirty in hopes that a later
1403 * write will work
1404 */
1405 if (err) {
1406 werr = err;
1407 continue;
Chris Mason9078a3e2007-04-26 16:46:15 -04001408 }
1409 }
1410 btrfs_free_path(path);
Chris Mason925baed2008-06-25 16:01:30 -04001411 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Mason9078a3e2007-04-26 16:46:15 -04001412 return werr;
1413}
1414
Chris Mason593060d2008-03-25 16:50:33 -04001415static int update_space_info(struct btrfs_fs_info *info, u64 flags,
1416 u64 total_bytes, u64 bytes_used,
1417 struct btrfs_space_info **space_info)
1418{
1419 struct btrfs_space_info *found;
1420
1421 found = __find_space_info(info, flags);
1422 if (found) {
1423 found->total_bytes += total_bytes;
1424 found->bytes_used += bytes_used;
Chris Mason8f18cf12008-04-25 16:53:30 -04001425 found->full = 0;
Chris Mason593060d2008-03-25 16:50:33 -04001426 *space_info = found;
1427 return 0;
1428 }
1429 found = kmalloc(sizeof(*found), GFP_NOFS);
1430 if (!found)
1431 return -ENOMEM;
1432
1433 list_add(&found->list, &info->space_info);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001434 INIT_LIST_HEAD(&found->block_groups);
1435 spin_lock_init(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04001436 found->flags = flags;
1437 found->total_bytes = total_bytes;
1438 found->bytes_used = bytes_used;
1439 found->bytes_pinned = 0;
Zheng Yane8569812008-09-26 10:05:48 -04001440 found->bytes_reserved = 0;
Chris Mason593060d2008-03-25 16:50:33 -04001441 found->full = 0;
Chris Mason0ef3e662008-05-24 14:04:53 -04001442 found->force_alloc = 0;
Chris Mason593060d2008-03-25 16:50:33 -04001443 *space_info = found;
1444 return 0;
1445}
1446
Chris Mason8790d502008-04-03 16:29:03 -04001447static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
1448{
1449 u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
Chris Mason611f0e02008-04-03 16:29:03 -04001450 BTRFS_BLOCK_GROUP_RAID1 |
Chris Mason321aecc2008-04-16 10:49:51 -04001451 BTRFS_BLOCK_GROUP_RAID10 |
Chris Mason611f0e02008-04-03 16:29:03 -04001452 BTRFS_BLOCK_GROUP_DUP);
Chris Mason8790d502008-04-03 16:29:03 -04001453 if (extra_flags) {
1454 if (flags & BTRFS_BLOCK_GROUP_DATA)
1455 fs_info->avail_data_alloc_bits |= extra_flags;
1456 if (flags & BTRFS_BLOCK_GROUP_METADATA)
1457 fs_info->avail_metadata_alloc_bits |= extra_flags;
1458 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
1459 fs_info->avail_system_alloc_bits |= extra_flags;
1460 }
1461}
Chris Mason593060d2008-03-25 16:50:33 -04001462
Chris Masona061fc82008-05-07 11:43:44 -04001463static u64 reduce_alloc_profile(struct btrfs_root *root, u64 flags)
Chris Masonec44a352008-04-28 15:29:52 -04001464{
Chris Masona061fc82008-05-07 11:43:44 -04001465 u64 num_devices = root->fs_info->fs_devices->num_devices;
1466
1467 if (num_devices == 1)
1468 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
1469 if (num_devices < 4)
1470 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
1471
Chris Masonec44a352008-04-28 15:29:52 -04001472 if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
1473 (flags & (BTRFS_BLOCK_GROUP_RAID1 |
Chris Masona061fc82008-05-07 11:43:44 -04001474 BTRFS_BLOCK_GROUP_RAID10))) {
Chris Masonec44a352008-04-28 15:29:52 -04001475 flags &= ~BTRFS_BLOCK_GROUP_DUP;
Chris Masona061fc82008-05-07 11:43:44 -04001476 }
Chris Masonec44a352008-04-28 15:29:52 -04001477
1478 if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
Chris Masona061fc82008-05-07 11:43:44 -04001479 (flags & BTRFS_BLOCK_GROUP_RAID10)) {
Chris Masonec44a352008-04-28 15:29:52 -04001480 flags &= ~BTRFS_BLOCK_GROUP_RAID1;
Chris Masona061fc82008-05-07 11:43:44 -04001481 }
Chris Masonec44a352008-04-28 15:29:52 -04001482
1483 if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
1484 ((flags & BTRFS_BLOCK_GROUP_RAID1) |
1485 (flags & BTRFS_BLOCK_GROUP_RAID10) |
1486 (flags & BTRFS_BLOCK_GROUP_DUP)))
1487 flags &= ~BTRFS_BLOCK_GROUP_RAID0;
1488 return flags;
1489}
1490
Chris Mason6324fbf2008-03-24 15:01:59 -04001491static int do_chunk_alloc(struct btrfs_trans_handle *trans,
1492 struct btrfs_root *extent_root, u64 alloc_bytes,
Chris Mason0ef3e662008-05-24 14:04:53 -04001493 u64 flags, int force)
Chris Mason6324fbf2008-03-24 15:01:59 -04001494{
1495 struct btrfs_space_info *space_info;
1496 u64 thresh;
1497 u64 start;
1498 u64 num_bytes;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001499 int ret = 0;
Chris Mason6324fbf2008-03-24 15:01:59 -04001500
Chris Masona061fc82008-05-07 11:43:44 -04001501 flags = reduce_alloc_profile(extent_root, flags);
Chris Masonec44a352008-04-28 15:29:52 -04001502
Chris Mason6324fbf2008-03-24 15:01:59 -04001503 space_info = __find_space_info(extent_root->fs_info, flags);
Chris Mason593060d2008-03-25 16:50:33 -04001504 if (!space_info) {
1505 ret = update_space_info(extent_root->fs_info, flags,
1506 0, 0, &space_info);
1507 BUG_ON(ret);
1508 }
Chris Mason6324fbf2008-03-24 15:01:59 -04001509 BUG_ON(!space_info);
1510
Chris Mason0ef3e662008-05-24 14:04:53 -04001511 if (space_info->force_alloc) {
1512 force = 1;
1513 space_info->force_alloc = 0;
1514 }
Chris Mason6324fbf2008-03-24 15:01:59 -04001515 if (space_info->full)
Chris Mason925baed2008-06-25 16:01:30 -04001516 goto out;
Chris Mason6324fbf2008-03-24 15:01:59 -04001517
Chris Mason8790d502008-04-03 16:29:03 -04001518 thresh = div_factor(space_info->total_bytes, 6);
Chris Mason0ef3e662008-05-24 14:04:53 -04001519 if (!force &&
Zheng Yane8569812008-09-26 10:05:48 -04001520 (space_info->bytes_used + space_info->bytes_pinned +
1521 space_info->bytes_reserved + alloc_bytes) < thresh)
Chris Mason925baed2008-06-25 16:01:30 -04001522 goto out;
Chris Mason6324fbf2008-03-24 15:01:59 -04001523
Chris Mason925baed2008-06-25 16:01:30 -04001524 mutex_lock(&extent_root->fs_info->chunk_mutex);
Chris Mason6324fbf2008-03-24 15:01:59 -04001525 ret = btrfs_alloc_chunk(trans, extent_root, &start, &num_bytes, flags);
1526 if (ret == -ENOSPC) {
1527printk("space info full %Lu\n", flags);
1528 space_info->full = 1;
Chris Masona74a4b92008-06-25 16:01:31 -04001529 goto out_unlock;
Chris Mason6324fbf2008-03-24 15:01:59 -04001530 }
Chris Mason6324fbf2008-03-24 15:01:59 -04001531 BUG_ON(ret);
1532
1533 ret = btrfs_make_block_group(trans, extent_root, 0, flags,
Chris Masone17cade2008-04-15 15:41:47 -04001534 BTRFS_FIRST_CHUNK_TREE_OBJECTID, start, num_bytes);
Chris Mason6324fbf2008-03-24 15:01:59 -04001535 BUG_ON(ret);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001536
Chris Masona74a4b92008-06-25 16:01:31 -04001537out_unlock:
Chris Mason333db942008-06-25 16:01:30 -04001538 mutex_unlock(&extent_root->fs_info->chunk_mutex);
Chris Masona74a4b92008-06-25 16:01:31 -04001539out:
Josef Bacik0f9dd462008-09-23 13:14:11 -04001540 return ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04001541}
1542
Chris Mason9078a3e2007-04-26 16:46:15 -04001543static int update_block_group(struct btrfs_trans_handle *trans,
1544 struct btrfs_root *root,
Chris Masondb945352007-10-15 16:15:53 -04001545 u64 bytenr, u64 num_bytes, int alloc,
Chris Mason0b86a832008-03-24 15:01:56 -04001546 int mark_free)
Chris Mason9078a3e2007-04-26 16:46:15 -04001547{
1548 struct btrfs_block_group_cache *cache;
1549 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -04001550 u64 total = num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04001551 u64 old_val;
Chris Masondb945352007-10-15 16:15:53 -04001552 u64 byte_in_group;
Chris Mason3e1ad542007-05-07 20:03:49 -04001553
Chris Mason7d9eb122008-07-08 14:19:17 -04001554 WARN_ON(!mutex_is_locked(&root->fs_info->alloc_mutex));
Chris Mason9078a3e2007-04-26 16:46:15 -04001555 while(total) {
Chris Masondb945352007-10-15 16:15:53 -04001556 cache = btrfs_lookup_block_group(info, bytenr);
Chris Mason3e1ad542007-05-07 20:03:49 -04001557 if (!cache) {
Chris Mason9078a3e2007-04-26 16:46:15 -04001558 return -1;
Chris Masoncd1bc462007-04-27 10:08:34 -04001559 }
Chris Masondb945352007-10-15 16:15:53 -04001560 byte_in_group = bytenr - cache->key.objectid;
1561 WARN_ON(byte_in_group > cache->key.offset);
Chris Mason9078a3e2007-04-26 16:46:15 -04001562
Chris Masonc286ac42008-07-22 23:06:41 -04001563 spin_lock(&cache->lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001564 cache->dirty = 1;
Chris Mason9078a3e2007-04-26 16:46:15 -04001565 old_val = btrfs_block_group_used(&cache->item);
Chris Masondb945352007-10-15 16:15:53 -04001566 num_bytes = min(total, cache->key.offset - byte_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -04001567 if (alloc) {
Chris Masondb945352007-10-15 16:15:53 -04001568 old_val += num_bytes;
Chris Mason6324fbf2008-03-24 15:01:59 -04001569 cache->space_info->bytes_used += num_bytes;
Chris Masonc286ac42008-07-22 23:06:41 -04001570 btrfs_set_block_group_used(&cache->item, old_val);
1571 spin_unlock(&cache->lock);
Chris Masoncd1bc462007-04-27 10:08:34 -04001572 } else {
Chris Masondb945352007-10-15 16:15:53 -04001573 old_val -= num_bytes;
Chris Mason6324fbf2008-03-24 15:01:59 -04001574 cache->space_info->bytes_used -= num_bytes;
Chris Masonc286ac42008-07-22 23:06:41 -04001575 btrfs_set_block_group_used(&cache->item, old_val);
1576 spin_unlock(&cache->lock);
Chris Masonf510cfe2007-10-15 16:14:48 -04001577 if (mark_free) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001578 int ret;
1579 ret = btrfs_add_free_space(cache, bytenr,
1580 num_bytes);
1581 if (ret)
1582 return -1;
Chris Masone37c9e62007-05-09 20:13:14 -04001583 }
Chris Masoncd1bc462007-04-27 10:08:34 -04001584 }
Chris Masondb945352007-10-15 16:15:53 -04001585 total -= num_bytes;
1586 bytenr += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04001587 }
1588 return 0;
1589}
Chris Mason6324fbf2008-03-24 15:01:59 -04001590
Chris Masona061fc82008-05-07 11:43:44 -04001591static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
1592{
Josef Bacik0f9dd462008-09-23 13:14:11 -04001593 struct btrfs_block_group_cache *cache;
1594
1595 cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
1596 if (!cache)
Chris Masona061fc82008-05-07 11:43:44 -04001597 return 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001598
1599 return cache->key.objectid;
Chris Masona061fc82008-05-07 11:43:44 -04001600}
1601
Chris Masone02119d2008-09-05 16:13:11 -04001602int btrfs_update_pinned_extents(struct btrfs_root *root,
Yan324ae4d2007-11-16 14:57:08 -05001603 u64 bytenr, u64 num, int pin)
1604{
1605 u64 len;
1606 struct btrfs_block_group_cache *cache;
1607 struct btrfs_fs_info *fs_info = root->fs_info;
1608
Chris Mason7d9eb122008-07-08 14:19:17 -04001609 WARN_ON(!mutex_is_locked(&root->fs_info->alloc_mutex));
Yan324ae4d2007-11-16 14:57:08 -05001610 if (pin) {
1611 set_extent_dirty(&fs_info->pinned_extents,
1612 bytenr, bytenr + num - 1, GFP_NOFS);
1613 } else {
1614 clear_extent_dirty(&fs_info->pinned_extents,
1615 bytenr, bytenr + num - 1, GFP_NOFS);
1616 }
1617 while (num > 0) {
1618 cache = btrfs_lookup_block_group(fs_info, bytenr);
Zheng Yane8569812008-09-26 10:05:48 -04001619 BUG_ON(!cache);
1620 len = min(num, cache->key.offset -
1621 (bytenr - cache->key.objectid));
Yan324ae4d2007-11-16 14:57:08 -05001622 if (pin) {
Zheng Yane8569812008-09-26 10:05:48 -04001623 spin_lock(&cache->lock);
1624 cache->pinned += len;
1625 cache->space_info->bytes_pinned += len;
1626 spin_unlock(&cache->lock);
Yan324ae4d2007-11-16 14:57:08 -05001627 fs_info->total_pinned += len;
1628 } else {
Zheng Yane8569812008-09-26 10:05:48 -04001629 spin_lock(&cache->lock);
1630 cache->pinned -= len;
1631 cache->space_info->bytes_pinned -= len;
1632 spin_unlock(&cache->lock);
Yan324ae4d2007-11-16 14:57:08 -05001633 fs_info->total_pinned -= len;
1634 }
1635 bytenr += len;
1636 num -= len;
1637 }
1638 return 0;
1639}
Chris Mason9078a3e2007-04-26 16:46:15 -04001640
Zheng Yane8569812008-09-26 10:05:48 -04001641static int update_reserved_extents(struct btrfs_root *root,
1642 u64 bytenr, u64 num, int reserve)
1643{
1644 u64 len;
1645 struct btrfs_block_group_cache *cache;
1646 struct btrfs_fs_info *fs_info = root->fs_info;
1647
1648 WARN_ON(!mutex_is_locked(&root->fs_info->alloc_mutex));
1649 while (num > 0) {
1650 cache = btrfs_lookup_block_group(fs_info, bytenr);
1651 BUG_ON(!cache);
1652 len = min(num, cache->key.offset -
1653 (bytenr - cache->key.objectid));
1654 if (reserve) {
1655 spin_lock(&cache->lock);
1656 cache->reserved += len;
1657 cache->space_info->bytes_reserved += len;
1658 spin_unlock(&cache->lock);
1659 } else {
1660 spin_lock(&cache->lock);
1661 cache->reserved -= len;
1662 cache->space_info->bytes_reserved -= len;
1663 spin_unlock(&cache->lock);
1664 }
1665 bytenr += len;
1666 num -= len;
1667 }
1668 return 0;
1669}
1670
Chris Masond1310b22008-01-24 16:13:08 -05001671int btrfs_copy_pinned(struct btrfs_root *root, struct extent_io_tree *copy)
Chris Masonccd467d2007-06-28 15:57:36 -04001672{
Chris Masonccd467d2007-06-28 15:57:36 -04001673 u64 last = 0;
Chris Mason1a5bc162007-10-15 16:15:26 -04001674 u64 start;
1675 u64 end;
Chris Masond1310b22008-01-24 16:13:08 -05001676 struct extent_io_tree *pinned_extents = &root->fs_info->pinned_extents;
Chris Masonccd467d2007-06-28 15:57:36 -04001677 int ret;
Chris Masonccd467d2007-06-28 15:57:36 -04001678
1679 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04001680 ret = find_first_extent_bit(pinned_extents, last,
1681 &start, &end, EXTENT_DIRTY);
1682 if (ret)
Chris Masonccd467d2007-06-28 15:57:36 -04001683 break;
Chris Mason1a5bc162007-10-15 16:15:26 -04001684 set_extent_dirty(copy, start, end, GFP_NOFS);
1685 last = end + 1;
Chris Masonccd467d2007-06-28 15:57:36 -04001686 }
1687 return 0;
1688}
1689
1690int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
1691 struct btrfs_root *root,
Chris Masond1310b22008-01-24 16:13:08 -05001692 struct extent_io_tree *unpin)
Chris Masona28ec192007-03-06 20:08:01 -05001693{
Chris Mason1a5bc162007-10-15 16:15:26 -04001694 u64 start;
1695 u64 end;
Chris Masona28ec192007-03-06 20:08:01 -05001696 int ret;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001697 struct btrfs_block_group_cache *cache;
Chris Masona28ec192007-03-06 20:08:01 -05001698
Chris Mason925baed2008-06-25 16:01:30 -04001699 mutex_lock(&root->fs_info->alloc_mutex);
Chris Masona28ec192007-03-06 20:08:01 -05001700 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04001701 ret = find_first_extent_bit(unpin, 0, &start, &end,
1702 EXTENT_DIRTY);
1703 if (ret)
Chris Masona28ec192007-03-06 20:08:01 -05001704 break;
Chris Masone02119d2008-09-05 16:13:11 -04001705 btrfs_update_pinned_extents(root, start, end + 1 - start, 0);
Chris Mason1a5bc162007-10-15 16:15:26 -04001706 clear_extent_dirty(unpin, start, end, GFP_NOFS);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001707 cache = btrfs_lookup_block_group(root->fs_info, start);
1708 if (cache->cached)
1709 btrfs_add_free_space(cache, start, end - start + 1);
Chris Masonc286ac42008-07-22 23:06:41 -04001710 if (need_resched()) {
1711 mutex_unlock(&root->fs_info->alloc_mutex);
1712 cond_resched();
1713 mutex_lock(&root->fs_info->alloc_mutex);
1714 }
Chris Masona28ec192007-03-06 20:08:01 -05001715 }
Chris Mason925baed2008-06-25 16:01:30 -04001716 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Masona28ec192007-03-06 20:08:01 -05001717 return 0;
1718}
1719
Chris Mason98ed5172008-01-03 10:01:48 -05001720static int finish_current_insert(struct btrfs_trans_handle *trans,
1721 struct btrfs_root *extent_root)
Chris Mason037e6392007-03-07 11:50:24 -05001722{
Chris Mason7bb86312007-12-11 09:25:06 -05001723 u64 start;
1724 u64 end;
Zheng Yan31840ae2008-09-23 13:14:14 -04001725 u64 priv;
Chris Mason7bb86312007-12-11 09:25:06 -05001726 struct btrfs_fs_info *info = extent_root->fs_info;
1727 struct btrfs_path *path;
Zheng Yan31840ae2008-09-23 13:14:14 -04001728 struct btrfs_extent_ref *ref;
1729 struct pending_extent_op *extent_op;
1730 struct btrfs_key key;
Chris Mason234b63a2007-03-13 10:46:10 -04001731 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -05001732 int ret;
Chris Mason1a5bc162007-10-15 16:15:26 -04001733 int err = 0;
Chris Mason037e6392007-03-07 11:50:24 -05001734
Chris Mason7d9eb122008-07-08 14:19:17 -04001735 WARN_ON(!mutex_is_locked(&extent_root->fs_info->alloc_mutex));
Chris Mason5f39d392007-10-15 16:14:19 -04001736 btrfs_set_stack_extent_refs(&extent_item, 1);
Chris Mason7bb86312007-12-11 09:25:06 -05001737 path = btrfs_alloc_path();
Chris Mason037e6392007-03-07 11:50:24 -05001738
Chris Mason26b80032007-08-08 20:17:12 -04001739 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04001740 ret = find_first_extent_bit(&info->extent_ins, 0, &start,
1741 &end, EXTENT_LOCKED);
1742 if (ret)
Chris Mason26b80032007-08-08 20:17:12 -04001743 break;
1744
Zheng Yan31840ae2008-09-23 13:14:14 -04001745 ret = get_state_private(&info->extent_ins, start, &priv);
1746 BUG_ON(ret);
1747 extent_op = (struct pending_extent_op *)(unsigned long)priv;
1748
1749 if (extent_op->type == PENDING_EXTENT_INSERT) {
1750 key.objectid = start;
1751 key.offset = end + 1 - start;
1752 key.type = BTRFS_EXTENT_ITEM_KEY;
1753 err = btrfs_insert_item(trans, extent_root, &key,
Chris Mason1a5bc162007-10-15 16:15:26 -04001754 &extent_item, sizeof(extent_item));
Zheng Yan31840ae2008-09-23 13:14:14 -04001755 BUG_ON(err);
Chris Masonc286ac42008-07-22 23:06:41 -04001756
Zheng Yan31840ae2008-09-23 13:14:14 -04001757 clear_extent_bits(&info->extent_ins, start, end,
1758 EXTENT_LOCKED, GFP_NOFS);
Chris Masonc286ac42008-07-22 23:06:41 -04001759
Zheng Yan31840ae2008-09-23 13:14:14 -04001760 err = insert_extent_backref(trans, extent_root, path,
1761 start, extent_op->parent,
1762 extent_root->root_key.objectid,
1763 extent_op->generation,
1764 extent_op->level, 0);
1765 BUG_ON(err);
1766 } else if (extent_op->type == PENDING_BACKREF_UPDATE) {
1767 err = lookup_extent_backref(trans, extent_root, path,
1768 start, extent_op->orig_parent,
1769 extent_root->root_key.objectid,
1770 extent_op->orig_generation, 0);
1771 BUG_ON(err);
Chris Masonc286ac42008-07-22 23:06:41 -04001772
Zheng Yan31840ae2008-09-23 13:14:14 -04001773 clear_extent_bits(&info->extent_ins, start, end,
1774 EXTENT_LOCKED, GFP_NOFS);
1775
1776 key.objectid = start;
1777 key.offset = extent_op->parent;
1778 key.type = BTRFS_EXTENT_REF_KEY;
1779 err = btrfs_set_item_key_safe(trans, extent_root, path,
1780 &key);
1781 BUG_ON(err);
1782 ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1783 struct btrfs_extent_ref);
1784 btrfs_set_ref_generation(path->nodes[0], ref,
1785 extent_op->generation);
1786 btrfs_mark_buffer_dirty(path->nodes[0]);
1787 btrfs_release_path(extent_root, path);
Chris Masond8d5f3e2007-12-11 12:42:00 -05001788 } else {
Zheng Yan31840ae2008-09-23 13:14:14 -04001789 BUG_ON(1);
Chris Masond8d5f3e2007-12-11 12:42:00 -05001790 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001791 kfree(extent_op);
1792
Chris Masonc286ac42008-07-22 23:06:41 -04001793 if (need_resched()) {
1794 mutex_unlock(&extent_root->fs_info->alloc_mutex);
1795 cond_resched();
1796 mutex_lock(&extent_root->fs_info->alloc_mutex);
1797 }
Chris Mason037e6392007-03-07 11:50:24 -05001798 }
Chris Mason7bb86312007-12-11 09:25:06 -05001799 btrfs_free_path(path);
Chris Mason037e6392007-03-07 11:50:24 -05001800 return 0;
1801}
1802
Zheng Yan31840ae2008-09-23 13:14:14 -04001803static int pin_down_bytes(struct btrfs_trans_handle *trans,
1804 struct btrfs_root *root,
1805 u64 bytenr, u64 num_bytes, int is_data)
Chris Masone20d96d2007-03-22 12:13:20 -04001806{
Chris Mason1a5bc162007-10-15 16:15:26 -04001807 int err = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04001808 struct extent_buffer *buf;
Chris Mason78fae272007-03-25 11:35:08 -04001809
Chris Mason7d9eb122008-07-08 14:19:17 -04001810 WARN_ON(!mutex_is_locked(&root->fs_info->alloc_mutex));
Zheng Yan31840ae2008-09-23 13:14:14 -04001811 if (is_data)
1812 goto pinit;
Chris Mason4bef0842008-09-08 11:18:08 -04001813
Zheng Yan31840ae2008-09-23 13:14:14 -04001814 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
1815 if (!buf)
1816 goto pinit;
Chris Mason4bef0842008-09-08 11:18:08 -04001817
Zheng Yan31840ae2008-09-23 13:14:14 -04001818 /* we can reuse a block if it hasn't been written
1819 * and it is from this transaction. We can't
1820 * reuse anything from the tree log root because
1821 * it has tiny sub-transactions.
1822 */
1823 if (btrfs_buffer_uptodate(buf, 0) &&
1824 btrfs_try_tree_lock(buf)) {
1825 u64 header_owner = btrfs_header_owner(buf);
1826 u64 header_transid = btrfs_header_generation(buf);
1827 if (header_owner != BTRFS_TREE_LOG_OBJECTID &&
1828 header_transid == trans->transid &&
1829 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
1830 clean_tree_block(NULL, root, buf);
1831 btrfs_tree_unlock(buf);
Chris Mason5f39d392007-10-15 16:14:19 -04001832 free_extent_buffer(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04001833 return 1;
Chris Mason8ef97622007-03-26 10:15:30 -04001834 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001835 btrfs_tree_unlock(buf);
Chris Masonf4b9aa82007-03-27 11:05:53 -04001836 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001837 free_extent_buffer(buf);
1838pinit:
1839 btrfs_update_pinned_extents(root, bytenr, num_bytes, 1);
1840
Chris Masonbe744172007-05-06 10:15:01 -04001841 BUG_ON(err < 0);
Chris Masone20d96d2007-03-22 12:13:20 -04001842 return 0;
1843}
1844
Chris Masona28ec192007-03-06 20:08:01 -05001845/*
1846 * remove an extent from the root, returns 0 on success
1847 */
Zheng Yan31840ae2008-09-23 13:14:14 -04001848static int __free_extent(struct btrfs_trans_handle *trans,
1849 struct btrfs_root *root,
1850 u64 bytenr, u64 num_bytes, u64 parent,
Chris Mason7bb86312007-12-11 09:25:06 -05001851 u64 root_objectid, u64 ref_generation,
Zheng Yan31840ae2008-09-23 13:14:14 -04001852 u64 owner_objectid, u64 owner_offset,
1853 int pin, int mark_free)
Chris Masona28ec192007-03-06 20:08:01 -05001854{
Chris Mason5caf2a02007-04-02 11:20:42 -04001855 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -04001856 struct btrfs_key key;
Chris Mason1261ec42007-03-20 20:35:03 -04001857 struct btrfs_fs_info *info = root->fs_info;
1858 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04001859 struct extent_buffer *leaf;
Chris Masona28ec192007-03-06 20:08:01 -05001860 int ret;
Chris Mason952fcca2008-02-18 16:33:44 -05001861 int extent_slot = 0;
1862 int found_extent = 0;
1863 int num_to_del = 1;
Chris Mason234b63a2007-03-13 10:46:10 -04001864 struct btrfs_extent_item *ei;
Chris Masoncf27e1e2007-03-13 09:49:06 -04001865 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -05001866
Chris Mason7d9eb122008-07-08 14:19:17 -04001867 WARN_ON(!mutex_is_locked(&root->fs_info->alloc_mutex));
Chris Masondb945352007-10-15 16:15:53 -04001868 key.objectid = bytenr;
Chris Mason62e27492007-03-15 12:56:47 -04001869 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masondb945352007-10-15 16:15:53 -04001870 key.offset = num_bytes;
Chris Mason5caf2a02007-04-02 11:20:42 -04001871 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04001872 if (!path)
1873 return -ENOMEM;
1874
Chris Mason3c12ac72008-04-21 12:01:38 -04001875 path->reada = 1;
Zheng Yan31840ae2008-09-23 13:14:14 -04001876 ret = lookup_extent_backref(trans, extent_root, path, bytenr, parent,
1877 root_objectid, ref_generation, 1);
Chris Mason7bb86312007-12-11 09:25:06 -05001878 if (ret == 0) {
Chris Mason952fcca2008-02-18 16:33:44 -05001879 struct btrfs_key found_key;
1880 extent_slot = path->slots[0];
1881 while(extent_slot > 0) {
1882 extent_slot--;
1883 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1884 extent_slot);
1885 if (found_key.objectid != bytenr)
1886 break;
1887 if (found_key.type == BTRFS_EXTENT_ITEM_KEY &&
1888 found_key.offset == num_bytes) {
1889 found_extent = 1;
1890 break;
1891 }
1892 if (path->slots[0] - extent_slot > 5)
1893 break;
1894 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001895 if (!found_extent) {
1896 ret = remove_extent_backref(trans, extent_root, path);
1897 BUG_ON(ret);
1898 btrfs_release_path(extent_root, path);
1899 ret = btrfs_search_slot(trans, extent_root,
1900 &key, path, -1, 1);
1901 BUG_ON(ret);
1902 extent_slot = path->slots[0];
1903 }
Chris Mason7bb86312007-12-11 09:25:06 -05001904 } else {
1905 btrfs_print_leaf(extent_root, path->nodes[0]);
1906 WARN_ON(1);
1907 printk("Unable to find ref byte nr %Lu root %Lu "
1908 " gen %Lu owner %Lu offset %Lu\n", bytenr,
1909 root_objectid, ref_generation, owner_objectid,
1910 owner_offset);
1911 }
Chris Mason5f39d392007-10-15 16:14:19 -04001912
1913 leaf = path->nodes[0];
Chris Mason952fcca2008-02-18 16:33:44 -05001914 ei = btrfs_item_ptr(leaf, extent_slot,
Chris Mason123abc82007-03-14 14:14:43 -04001915 struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001916 refs = btrfs_extent_refs(leaf, ei);
1917 BUG_ON(refs == 0);
1918 refs -= 1;
1919 btrfs_set_extent_refs(leaf, ei, refs);
Chris Mason952fcca2008-02-18 16:33:44 -05001920
Chris Mason5f39d392007-10-15 16:14:19 -04001921 btrfs_mark_buffer_dirty(leaf);
1922
Chris Mason952fcca2008-02-18 16:33:44 -05001923 if (refs == 0 && found_extent && path->slots[0] == extent_slot + 1) {
Zheng Yan31840ae2008-09-23 13:14:14 -04001924 struct btrfs_extent_ref *ref;
1925 ref = btrfs_item_ptr(leaf, path->slots[0],
1926 struct btrfs_extent_ref);
1927 BUG_ON(btrfs_ref_num_refs(leaf, ref) != 1);
Chris Mason952fcca2008-02-18 16:33:44 -05001928 /* if the back ref and the extent are next to each other
1929 * they get deleted below in one shot
1930 */
1931 path->slots[0] = extent_slot;
1932 num_to_del = 2;
1933 } else if (found_extent) {
1934 /* otherwise delete the extent back ref */
Zheng Yan31840ae2008-09-23 13:14:14 -04001935 ret = remove_extent_backref(trans, extent_root, path);
Chris Mason952fcca2008-02-18 16:33:44 -05001936 BUG_ON(ret);
1937 /* if refs are 0, we need to setup the path for deletion */
1938 if (refs == 0) {
1939 btrfs_release_path(extent_root, path);
1940 ret = btrfs_search_slot(trans, extent_root, &key, path,
1941 -1, 1);
Chris Mason952fcca2008-02-18 16:33:44 -05001942 BUG_ON(ret);
1943 }
1944 }
1945
Chris Masoncf27e1e2007-03-13 09:49:06 -04001946 if (refs == 0) {
Chris Masondb945352007-10-15 16:15:53 -04001947 u64 super_used;
1948 u64 root_used;
David Woodhouse21af8042008-08-12 14:13:26 +01001949#ifdef BIO_RW_DISCARD
1950 u64 map_length = num_bytes;
1951 struct btrfs_multi_bio *multi = NULL;
1952#endif
Chris Mason78fae272007-03-25 11:35:08 -04001953
1954 if (pin) {
Zheng Yan31840ae2008-09-23 13:14:14 -04001955 ret = pin_down_bytes(trans, root, bytenr, num_bytes,
1956 owner_objectid >= BTRFS_FIRST_FREE_OBJECTID);
Yanc5492282007-11-06 10:25:25 -05001957 if (ret > 0)
1958 mark_free = 1;
1959 BUG_ON(ret < 0);
Chris Mason78fae272007-03-25 11:35:08 -04001960 }
1961
Josef Bacik58176a92007-08-29 15:47:34 -04001962 /* block accounting for super block */
Chris Masona2135012008-06-25 16:01:30 -04001963 spin_lock_irq(&info->delalloc_lock);
Chris Masondb945352007-10-15 16:15:53 -04001964 super_used = btrfs_super_bytes_used(&info->super_copy);
1965 btrfs_set_super_bytes_used(&info->super_copy,
1966 super_used - num_bytes);
Chris Masona2135012008-06-25 16:01:30 -04001967 spin_unlock_irq(&info->delalloc_lock);
Josef Bacik58176a92007-08-29 15:47:34 -04001968
1969 /* block accounting for root item */
Chris Masondb945352007-10-15 16:15:53 -04001970 root_used = btrfs_root_used(&root->root_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001971 btrfs_set_root_used(&root->root_item,
Chris Masondb945352007-10-15 16:15:53 -04001972 root_used - num_bytes);
Chris Mason952fcca2008-02-18 16:33:44 -05001973 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
1974 num_to_del);
Zheng Yan31840ae2008-09-23 13:14:14 -04001975 BUG_ON(ret);
Chris Masondb945352007-10-15 16:15:53 -04001976 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
Chris Mason0b86a832008-03-24 15:01:56 -04001977 mark_free);
Chris Mason9078a3e2007-04-26 16:46:15 -04001978 BUG_ON(ret);
David Woodhouse21af8042008-08-12 14:13:26 +01001979
1980#ifdef BIO_RW_DISCARD
1981 /* Tell the block device(s) that the sectors can be discarded */
1982 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
1983 bytenr, &map_length, &multi, 0);
1984 if (!ret) {
1985 struct btrfs_bio_stripe *stripe = multi->stripes;
1986 int i;
1987
1988 if (map_length > num_bytes)
1989 map_length = num_bytes;
1990
1991 for (i = 0; i < multi->num_stripes; i++, stripe++) {
1992 blkdev_issue_discard(stripe->dev->bdev,
1993 stripe->physical >> 9,
1994 map_length >> 9);
1995 }
1996 kfree(multi);
1997 }
1998#endif
Chris Masona28ec192007-03-06 20:08:01 -05001999 }
Chris Mason5caf2a02007-04-02 11:20:42 -04002000 btrfs_free_path(path);
Chris Masone089f052007-03-16 16:20:31 -04002001 finish_current_insert(trans, extent_root);
Chris Masona28ec192007-03-06 20:08:01 -05002002 return ret;
2003}
2004
2005/*
Chris Masonfec577f2007-02-26 10:40:21 -05002006 * find all the blocks marked as pending in the radix tree and remove
2007 * them from the extent map
2008 */
Chris Masone089f052007-03-16 16:20:31 -04002009static int del_pending_extents(struct btrfs_trans_handle *trans, struct
2010 btrfs_root *extent_root)
Chris Masonfec577f2007-02-26 10:40:21 -05002011{
2012 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -04002013 int err = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04002014 int mark_free = 0;
Chris Mason1a5bc162007-10-15 16:15:26 -04002015 u64 start;
2016 u64 end;
Zheng Yan31840ae2008-09-23 13:14:14 -04002017 u64 priv;
Chris Masond1310b22008-01-24 16:13:08 -05002018 struct extent_io_tree *pending_del;
Zheng Yan31840ae2008-09-23 13:14:14 -04002019 struct extent_io_tree *extent_ins;
2020 struct pending_extent_op *extent_op;
Chris Mason8ef97622007-03-26 10:15:30 -04002021
Chris Mason7d9eb122008-07-08 14:19:17 -04002022 WARN_ON(!mutex_is_locked(&extent_root->fs_info->alloc_mutex));
Zheng Yan31840ae2008-09-23 13:14:14 -04002023 extent_ins = &extent_root->fs_info->extent_ins;
Chris Mason1a5bc162007-10-15 16:15:26 -04002024 pending_del = &extent_root->fs_info->pending_del;
Chris Masonfec577f2007-02-26 10:40:21 -05002025
2026 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04002027 ret = find_first_extent_bit(pending_del, 0, &start, &end,
2028 EXTENT_LOCKED);
2029 if (ret)
Chris Masonfec577f2007-02-26 10:40:21 -05002030 break;
Zheng Yan31840ae2008-09-23 13:14:14 -04002031
2032 ret = get_state_private(pending_del, start, &priv);
2033 BUG_ON(ret);
2034 extent_op = (struct pending_extent_op *)(unsigned long)priv;
2035
Chris Mason1a5bc162007-10-15 16:15:26 -04002036 clear_extent_bits(pending_del, start, end, EXTENT_LOCKED,
2037 GFP_NOFS);
Zheng Yan31840ae2008-09-23 13:14:14 -04002038
2039 ret = pin_down_bytes(trans, extent_root, start,
2040 end + 1 - start, 0);
2041 mark_free = ret > 0;
2042 if (!test_range_bit(extent_ins, start, end,
2043 EXTENT_LOCKED, 0)) {
2044free_extent:
Chris Masonc286ac42008-07-22 23:06:41 -04002045 ret = __free_extent(trans, extent_root,
Zheng Yan31840ae2008-09-23 13:14:14 -04002046 start, end + 1 - start,
2047 extent_op->orig_parent,
2048 extent_root->root_key.objectid,
2049 extent_op->orig_generation,
2050 extent_op->level, 0, 0, mark_free);
2051 kfree(extent_op);
Chris Masonc286ac42008-07-22 23:06:41 -04002052 } else {
Zheng Yan31840ae2008-09-23 13:14:14 -04002053 kfree(extent_op);
2054 ret = get_state_private(extent_ins, start, &priv);
2055 BUG_ON(ret);
2056 extent_op = (struct pending_extent_op *)
2057 (unsigned long)priv;
2058
2059 clear_extent_bits(extent_ins, start, end,
2060 EXTENT_LOCKED, GFP_NOFS);
2061
2062 if (extent_op->type == PENDING_BACKREF_UPDATE)
2063 goto free_extent;
2064
2065 ret = update_block_group(trans, extent_root, start,
2066 end + 1 - start, 0, mark_free);
2067 BUG_ON(ret);
2068 kfree(extent_op);
Chris Masonc286ac42008-07-22 23:06:41 -04002069 }
Chris Mason1a5bc162007-10-15 16:15:26 -04002070 if (ret)
2071 err = ret;
Chris Masonc286ac42008-07-22 23:06:41 -04002072
2073 if (need_resched()) {
2074 mutex_unlock(&extent_root->fs_info->alloc_mutex);
2075 cond_resched();
2076 mutex_lock(&extent_root->fs_info->alloc_mutex);
2077 }
Chris Masonfec577f2007-02-26 10:40:21 -05002078 }
Chris Masone20d96d2007-03-22 12:13:20 -04002079 return err;
Chris Masonfec577f2007-02-26 10:40:21 -05002080}
2081
2082/*
2083 * remove an extent from the root, returns 0 on success
2084 */
Chris Mason925baed2008-06-25 16:01:30 -04002085static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04002086 struct btrfs_root *root,
2087 u64 bytenr, u64 num_bytes, u64 parent,
2088 u64 root_objectid, u64 ref_generation,
2089 u64 owner_objectid, u64 owner_offset, int pin)
Chris Masonfec577f2007-02-26 10:40:21 -05002090{
Chris Mason9f5fae22007-03-20 14:38:32 -04002091 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Masonfec577f2007-02-26 10:40:21 -05002092 int pending_ret;
2093 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05002094
Chris Masondb945352007-10-15 16:15:53 -04002095 WARN_ON(num_bytes < root->sectorsize);
Chris Masona28ec192007-03-06 20:08:01 -05002096 if (root == extent_root) {
Zheng Yan31840ae2008-09-23 13:14:14 -04002097 struct pending_extent_op *extent_op;
2098
2099 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
2100 BUG_ON(!extent_op);
2101
2102 extent_op->type = PENDING_EXTENT_DELETE;
2103 extent_op->bytenr = bytenr;
2104 extent_op->num_bytes = num_bytes;
2105 extent_op->parent = parent;
2106 extent_op->orig_parent = parent;
2107 extent_op->generation = ref_generation;
2108 extent_op->orig_generation = ref_generation;
2109 extent_op->level = (int)owner_objectid;
2110
2111 set_extent_bits(&root->fs_info->pending_del,
2112 bytenr, bytenr + num_bytes - 1,
2113 EXTENT_LOCKED, GFP_NOFS);
2114 set_state_private(&root->fs_info->pending_del,
2115 bytenr, (unsigned long)extent_op);
Chris Masona28ec192007-03-06 20:08:01 -05002116 return 0;
2117 }
Chris Mason4bef0842008-09-08 11:18:08 -04002118 /* if metadata always pin */
Chris Masond00aff02008-09-11 15:54:42 -04002119 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
2120 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04002121 struct btrfs_block_group_cache *cache;
2122
Chris Masond00aff02008-09-11 15:54:42 -04002123 /* btrfs_free_reserved_extent */
Josef Bacik0f9dd462008-09-23 13:14:11 -04002124 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
2125 BUG_ON(!cache);
2126 btrfs_add_free_space(cache, bytenr, num_bytes);
Zheng Yane8569812008-09-26 10:05:48 -04002127 update_reserved_extents(root, bytenr, num_bytes, 0);
Chris Masond00aff02008-09-11 15:54:42 -04002128 return 0;
2129 }
Chris Mason4bef0842008-09-08 11:18:08 -04002130 pin = 1;
Chris Masond00aff02008-09-11 15:54:42 -04002131 }
Chris Mason4bef0842008-09-08 11:18:08 -04002132
2133 /* if data pin when any transaction has committed this */
2134 if (ref_generation != trans->transid)
2135 pin = 1;
2136
Zheng Yan31840ae2008-09-23 13:14:14 -04002137 ret = __free_extent(trans, root, bytenr, num_bytes, parent,
2138 root_objectid, ref_generation, owner_objectid,
2139 owner_offset, pin, pin == 0);
Chris Masonee6e6502008-07-17 12:54:40 -04002140
2141 finish_current_insert(trans, root->fs_info->extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -04002142 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
Chris Masonfec577f2007-02-26 10:40:21 -05002143 return ret ? ret : pending_ret;
2144}
2145
Chris Mason925baed2008-06-25 16:01:30 -04002146int btrfs_free_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04002147 struct btrfs_root *root,
2148 u64 bytenr, u64 num_bytes, u64 parent,
2149 u64 root_objectid, u64 ref_generation,
2150 u64 owner_objectid, u64 owner_offset, int pin)
Chris Mason925baed2008-06-25 16:01:30 -04002151{
2152 int ret;
2153
2154 maybe_lock_mutex(root);
Zheng Yan31840ae2008-09-23 13:14:14 -04002155 ret = __btrfs_free_extent(trans, root, bytenr, num_bytes, parent,
Chris Mason925baed2008-06-25 16:01:30 -04002156 root_objectid, ref_generation,
2157 owner_objectid, owner_offset, pin);
2158 maybe_unlock_mutex(root);
2159 return ret;
2160}
2161
Chris Mason87ee04e2007-11-30 11:30:34 -05002162static u64 stripe_align(struct btrfs_root *root, u64 val)
2163{
2164 u64 mask = ((u64)root->stripesize - 1);
2165 u64 ret = (val + mask) & ~mask;
2166 return ret;
2167}
2168
Chris Masonfec577f2007-02-26 10:40:21 -05002169/*
2170 * walks the btree of allocated extents and find a hole of a given size.
2171 * The key ins is changed to record the hole:
2172 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -04002173 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -05002174 * ins->offset == number of blocks
2175 * Any available blocks before search_start are skipped.
2176 */
Chris Mason98ed5172008-01-03 10:01:48 -05002177static int noinline find_free_extent(struct btrfs_trans_handle *trans,
2178 struct btrfs_root *orig_root,
2179 u64 num_bytes, u64 empty_size,
2180 u64 search_start, u64 search_end,
2181 u64 hint_byte, struct btrfs_key *ins,
2182 u64 exclude_start, u64 exclude_nr,
2183 int data)
Chris Masonfec577f2007-02-26 10:40:21 -05002184{
Chris Mason87ee04e2007-11-30 11:30:34 -05002185 int ret;
Chris Masona061fc82008-05-07 11:43:44 -04002186 u64 orig_search_start;
Chris Mason9f5fae22007-03-20 14:38:32 -04002187 struct btrfs_root * root = orig_root->fs_info->extent_root;
Chris Masonf2458e12007-04-25 15:52:25 -04002188 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -04002189 u64 total_needed = num_bytes;
Chris Mason239b14b2008-03-24 15:02:07 -04002190 u64 *last_ptr = NULL;
Chris Masonbe08c1b2007-05-03 09:06:49 -04002191 struct btrfs_block_group_cache *block_group;
Chris Mason0ef3e662008-05-24 14:04:53 -04002192 int chunk_alloc_done = 0;
Chris Mason239b14b2008-03-24 15:02:07 -04002193 int empty_cluster = 2 * 1024 * 1024;
Chris Mason0ef3e662008-05-24 14:04:53 -04002194 int allowed_chunk_alloc = 0;
Chris Masonfec577f2007-02-26 10:40:21 -05002195
Chris Masondb945352007-10-15 16:15:53 -04002196 WARN_ON(num_bytes < root->sectorsize);
Chris Masonb1a4d962007-04-04 15:27:52 -04002197 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
2198
Chris Mason0ef3e662008-05-24 14:04:53 -04002199 if (orig_root->ref_cows || empty_size)
2200 allowed_chunk_alloc = 1;
2201
Chris Mason239b14b2008-03-24 15:02:07 -04002202 if (data & BTRFS_BLOCK_GROUP_METADATA) {
2203 last_ptr = &root->fs_info->last_alloc;
Chris Mason8790d502008-04-03 16:29:03 -04002204 empty_cluster = 256 * 1024;
Chris Mason239b14b2008-03-24 15:02:07 -04002205 }
2206
Josef Bacik0f9dd462008-09-23 13:14:11 -04002207 if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD))
Chris Mason239b14b2008-03-24 15:02:07 -04002208 last_ptr = &root->fs_info->last_data_alloc;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002209
Chris Masone02119d2008-09-05 16:13:11 -04002210 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
2211 last_ptr = &root->fs_info->last_log_alloc;
2212 if (!last_ptr == 0 && root->fs_info->last_alloc) {
2213 *last_ptr = root->fs_info->last_alloc + empty_cluster;
2214 }
2215 }
Chris Mason239b14b2008-03-24 15:02:07 -04002216
2217 if (last_ptr) {
2218 if (*last_ptr)
2219 hint_byte = *last_ptr;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002220 else
Chris Mason239b14b2008-03-24 15:02:07 -04002221 empty_size += empty_cluster;
Chris Mason239b14b2008-03-24 15:02:07 -04002222 }
2223
Chris Masona061fc82008-05-07 11:43:44 -04002224 search_start = max(search_start, first_logical_byte(root, 0));
2225 orig_search_start = search_start;
2226
Chris Mason239b14b2008-03-24 15:02:07 -04002227 search_start = max(search_start, hint_byte);
Chris Mason6702ed42007-08-07 16:15:09 -04002228 total_needed += empty_size;
Chris Mason0b86a832008-03-24 15:01:56 -04002229
Josef Bacik0f9dd462008-09-23 13:14:11 -04002230new_group:
Zheng Yane8569812008-09-26 10:05:48 -04002231 block_group = btrfs_lookup_first_block_group(info, search_start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002232
2233 /*
2234 * Ok this looks a little tricky, buts its really simple. First if we
2235 * didn't find a block group obviously we want to start over.
2236 * Secondly, if the block group we found does not match the type we
2237 * need, and we have a last_ptr and its not 0, chances are the last
2238 * allocation we made was at the end of the block group, so lets go
2239 * ahead and skip the looking through the rest of the block groups and
2240 * start at the beginning. This helps with metadata allocations,
2241 * since you are likely to have a bunch of data block groups to search
2242 * through first before you realize that you need to start over, so go
2243 * ahead and start over and save the time.
2244 */
2245 if (!block_group || (!block_group_bits(block_group, data) &&
2246 last_ptr && *last_ptr)) {
2247 if (search_start != orig_search_start) {
2248 if (last_ptr && *last_ptr)
2249 *last_ptr = 0;
2250 search_start = orig_search_start;
2251 goto new_group;
2252 } else if (!chunk_alloc_done && allowed_chunk_alloc) {
2253 ret = do_chunk_alloc(trans, root,
2254 num_bytes + 2 * 1024 * 1024,
2255 data, 1);
Zheng Yane8569812008-09-26 10:05:48 -04002256 if (ret < 0)
Josef Bacik0f9dd462008-09-23 13:14:11 -04002257 goto error;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002258 BUG_ON(ret);
2259 chunk_alloc_done = 1;
2260 search_start = orig_search_start;
2261 goto new_group;
2262 } else {
2263 ret = -ENOSPC;
2264 goto error;
Chris Mason0ef3e662008-05-24 14:04:53 -04002265 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002266 }
2267
2268 /*
2269 * this is going to seach through all of the existing block groups it
2270 * can find, so if we don't find something we need to see if we can
2271 * allocate what we need.
2272 */
2273 ret = find_free_space(root, &block_group, &search_start,
2274 total_needed, data);
2275 if (ret == -ENOSPC) {
2276 /*
2277 * instead of allocating, start at the original search start
2278 * and see if there is something to be found, if not then we
2279 * allocate
2280 */
2281 if (search_start != orig_search_start) {
2282 if (last_ptr && *last_ptr) {
2283 *last_ptr = 0;
2284 total_needed += empty_cluster;
2285 }
2286 search_start = orig_search_start;
2287 goto new_group;
2288 }
2289
2290 /*
2291 * we've already allocated, we're pretty screwed
2292 */
2293 if (chunk_alloc_done) {
2294 goto error;
2295 } else if (!allowed_chunk_alloc && block_group &&
2296 block_group_bits(block_group, data)) {
2297 block_group->space_info->force_alloc = 1;
2298 goto error;
2299 } else if (!allowed_chunk_alloc) {
2300 goto error;
2301 }
2302
2303 ret = do_chunk_alloc(trans, root, num_bytes + 2 * 1024 * 1024,
2304 data, 1);
2305 if (ret < 0)
2306 goto error;
2307
2308 BUG_ON(ret);
Chris Mason0ef3e662008-05-24 14:04:53 -04002309 chunk_alloc_done = 1;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002310 if (block_group)
2311 search_start = block_group->key.objectid +
2312 block_group->key.offset;
2313 else
2314 search_start = orig_search_start;
2315 goto new_group;
Chris Mason0ef3e662008-05-24 14:04:53 -04002316 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002317
Chris Mason0b86a832008-03-24 15:01:56 -04002318 if (ret)
2319 goto error;
2320
Chris Mason87ee04e2007-11-30 11:30:34 -05002321 search_start = stripe_align(root, search_start);
Chris Masonfec577f2007-02-26 10:40:21 -05002322 ins->objectid = search_start;
Chris Mason0b86a832008-03-24 15:01:56 -04002323 ins->offset = num_bytes;
Chris Masone37c9e62007-05-09 20:13:14 -04002324
Josef Bacik0f9dd462008-09-23 13:14:11 -04002325 if (ins->objectid + num_bytes >= search_end) {
2326 search_start = orig_search_start;
2327 if (chunk_alloc_done) {
2328 ret = -ENOSPC;
2329 goto error;
2330 }
2331 goto new_group;
2332 }
Chris Mason0b86a832008-03-24 15:01:56 -04002333
2334 if (ins->objectid + num_bytes >
2335 block_group->key.objectid + block_group->key.offset) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04002336 if (search_start == orig_search_start && chunk_alloc_done) {
2337 ret = -ENOSPC;
2338 goto error;
2339 }
Chris Masone19caa52007-10-15 16:17:44 -04002340 search_start = block_group->key.objectid +
2341 block_group->key.offset;
2342 goto new_group;
2343 }
Chris Mason0b86a832008-03-24 15:01:56 -04002344
Chris Masondb945352007-10-15 16:15:53 -04002345 if (exclude_nr > 0 && (ins->objectid + num_bytes > exclude_start &&
Chris Masonf2654de2007-06-26 12:20:46 -04002346 ins->objectid < exclude_start + exclude_nr)) {
2347 search_start = exclude_start + exclude_nr;
2348 goto new_group;
2349 }
Chris Mason0b86a832008-03-24 15:01:56 -04002350
Josef Bacik0f9dd462008-09-23 13:14:11 -04002351 if (!(data & BTRFS_BLOCK_GROUP_DATA))
2352 trans->block_group = block_group;
2353
Chris Masondb945352007-10-15 16:15:53 -04002354 ins->offset = num_bytes;
Chris Mason239b14b2008-03-24 15:02:07 -04002355 if (last_ptr) {
2356 *last_ptr = ins->objectid + ins->offset;
2357 if (*last_ptr ==
Josef Bacik0f9dd462008-09-23 13:14:11 -04002358 btrfs_super_total_bytes(&root->fs_info->super_copy))
Chris Mason239b14b2008-03-24 15:02:07 -04002359 *last_ptr = 0;
Chris Mason239b14b2008-03-24 15:02:07 -04002360 }
Chris Masonbe744172007-05-06 10:15:01 -04002361
Josef Bacik0f9dd462008-09-23 13:14:11 -04002362 ret = 0;
Chris Mason0f70abe2007-02-28 16:46:22 -05002363error:
Chris Mason0f70abe2007-02-28 16:46:22 -05002364 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05002365}
Chris Masonec44a352008-04-28 15:29:52 -04002366
Josef Bacik0f9dd462008-09-23 13:14:11 -04002367static void dump_space_info(struct btrfs_space_info *info, u64 bytes)
2368{
2369 struct btrfs_block_group_cache *cache;
2370 struct list_head *l;
2371
2372 printk(KERN_INFO "space_info has %Lu free, is %sfull\n",
Zheng Yane8569812008-09-26 10:05:48 -04002373 info->total_bytes - info->bytes_used - info->bytes_pinned -
2374 info->bytes_reserved, (info->full) ? "" : "not ");
Josef Bacik0f9dd462008-09-23 13:14:11 -04002375
2376 spin_lock(&info->lock);
2377 list_for_each(l, &info->block_groups) {
2378 cache = list_entry(l, struct btrfs_block_group_cache, list);
2379 spin_lock(&cache->lock);
2380 printk(KERN_INFO "block group %Lu has %Lu bytes, %Lu used "
Zheng Yane8569812008-09-26 10:05:48 -04002381 "%Lu pinned %Lu reserved\n",
Josef Bacik0f9dd462008-09-23 13:14:11 -04002382 cache->key.objectid, cache->key.offset,
Zheng Yane8569812008-09-26 10:05:48 -04002383 btrfs_block_group_used(&cache->item),
2384 cache->pinned, cache->reserved);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002385 btrfs_dump_free_space(cache, bytes);
2386 spin_unlock(&cache->lock);
2387 }
2388 spin_unlock(&info->lock);
2389}
Zheng Yane8569812008-09-26 10:05:48 -04002390
Chris Masone6dcd2d2008-07-17 12:53:50 -04002391static int __btrfs_reserve_extent(struct btrfs_trans_handle *trans,
2392 struct btrfs_root *root,
2393 u64 num_bytes, u64 min_alloc_size,
2394 u64 empty_size, u64 hint_byte,
2395 u64 search_end, struct btrfs_key *ins,
2396 u64 data)
Chris Masonfec577f2007-02-26 10:40:21 -05002397{
2398 int ret;
Chris Masonfbdc7622007-05-30 10:22:12 -04002399 u64 search_start = 0;
Chris Mason8790d502008-04-03 16:29:03 -04002400 u64 alloc_profile;
Chris Mason1261ec42007-03-20 20:35:03 -04002401 struct btrfs_fs_info *info = root->fs_info;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002402 struct btrfs_block_group_cache *cache;
Chris Mason925baed2008-06-25 16:01:30 -04002403
Chris Mason6324fbf2008-03-24 15:01:59 -04002404 if (data) {
Chris Mason8790d502008-04-03 16:29:03 -04002405 alloc_profile = info->avail_data_alloc_bits &
2406 info->data_alloc_profile;
2407 data = BTRFS_BLOCK_GROUP_DATA | alloc_profile;
Chris Mason6324fbf2008-03-24 15:01:59 -04002408 } else if (root == root->fs_info->chunk_root) {
Chris Mason8790d502008-04-03 16:29:03 -04002409 alloc_profile = info->avail_system_alloc_bits &
2410 info->system_alloc_profile;
2411 data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile;
Chris Mason6324fbf2008-03-24 15:01:59 -04002412 } else {
Chris Mason8790d502008-04-03 16:29:03 -04002413 alloc_profile = info->avail_metadata_alloc_bits &
2414 info->metadata_alloc_profile;
2415 data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile;
Chris Mason6324fbf2008-03-24 15:01:59 -04002416 }
Chris Mason98d20f62008-04-14 09:46:10 -04002417again:
Chris Masona061fc82008-05-07 11:43:44 -04002418 data = reduce_alloc_profile(root, data);
Chris Mason0ef3e662008-05-24 14:04:53 -04002419 /*
2420 * the only place that sets empty_size is btrfs_realloc_node, which
2421 * is not called recursively on allocations
2422 */
2423 if (empty_size || root->ref_cows) {
Chris Mason593060d2008-03-25 16:50:33 -04002424 if (!(data & BTRFS_BLOCK_GROUP_METADATA)) {
Chris Mason6324fbf2008-03-24 15:01:59 -04002425 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0ef3e662008-05-24 14:04:53 -04002426 2 * 1024 * 1024,
2427 BTRFS_BLOCK_GROUP_METADATA |
2428 (info->metadata_alloc_profile &
2429 info->avail_metadata_alloc_bits), 0);
Chris Mason6324fbf2008-03-24 15:01:59 -04002430 }
2431 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0ef3e662008-05-24 14:04:53 -04002432 num_bytes + 2 * 1024 * 1024, data, 0);
Chris Mason6324fbf2008-03-24 15:01:59 -04002433 }
Chris Mason0b86a832008-03-24 15:01:56 -04002434
Chris Masondb945352007-10-15 16:15:53 -04002435 WARN_ON(num_bytes < root->sectorsize);
2436 ret = find_free_extent(trans, root, num_bytes, empty_size,
2437 search_start, search_end, hint_byte, ins,
Chris Mason26b80032007-08-08 20:17:12 -04002438 trans->alloc_exclude_start,
2439 trans->alloc_exclude_nr, data);
Chris Mason3b951512008-04-17 11:29:12 -04002440
Chris Mason98d20f62008-04-14 09:46:10 -04002441 if (ret == -ENOSPC && num_bytes > min_alloc_size) {
2442 num_bytes = num_bytes >> 1;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002443 num_bytes = num_bytes & ~(root->sectorsize - 1);
Chris Mason98d20f62008-04-14 09:46:10 -04002444 num_bytes = max(num_bytes, min_alloc_size);
Chris Mason0ef3e662008-05-24 14:04:53 -04002445 do_chunk_alloc(trans, root->fs_info->extent_root,
2446 num_bytes, data, 1);
Chris Mason98d20f62008-04-14 09:46:10 -04002447 goto again;
2448 }
Chris Masonec44a352008-04-28 15:29:52 -04002449 if (ret) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04002450 struct btrfs_space_info *sinfo;
2451
2452 sinfo = __find_space_info(root->fs_info, data);
2453 printk("allocation failed flags %Lu, wanted %Lu\n",
2454 data, num_bytes);
2455 dump_space_info(sinfo, num_bytes);
Chris Mason925baed2008-06-25 16:01:30 -04002456 BUG();
Chris Mason925baed2008-06-25 16:01:30 -04002457 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002458 cache = btrfs_lookup_block_group(root->fs_info, ins->objectid);
2459 if (!cache) {
2460 printk(KERN_ERR "Unable to find block group for %Lu\n", ins->objectid);
2461 return -ENOSPC;
2462 }
2463
2464 ret = btrfs_remove_free_space(cache, ins->objectid, ins->offset);
2465
2466 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002467}
2468
Chris Mason65b51a02008-08-01 15:11:20 -04002469int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len)
2470{
Josef Bacik0f9dd462008-09-23 13:14:11 -04002471 struct btrfs_block_group_cache *cache;
2472
Chris Mason65b51a02008-08-01 15:11:20 -04002473 maybe_lock_mutex(root);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002474 cache = btrfs_lookup_block_group(root->fs_info, start);
2475 if (!cache) {
2476 printk(KERN_ERR "Unable to find block group for %Lu\n", start);
2477 maybe_unlock_mutex(root);
2478 return -ENOSPC;
2479 }
2480 btrfs_add_free_space(cache, start, len);
Chris Mason65b51a02008-08-01 15:11:20 -04002481 maybe_unlock_mutex(root);
2482 return 0;
2483}
2484
Chris Masone6dcd2d2008-07-17 12:53:50 -04002485int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
2486 struct btrfs_root *root,
2487 u64 num_bytes, u64 min_alloc_size,
2488 u64 empty_size, u64 hint_byte,
2489 u64 search_end, struct btrfs_key *ins,
2490 u64 data)
2491{
2492 int ret;
2493 maybe_lock_mutex(root);
2494 ret = __btrfs_reserve_extent(trans, root, num_bytes, min_alloc_size,
2495 empty_size, hint_byte, search_end, ins,
2496 data);
Zheng Yane8569812008-09-26 10:05:48 -04002497 update_reserved_extents(root, ins->objectid, ins->offset, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002498 maybe_unlock_mutex(root);
2499 return ret;
2500}
2501
2502static int __btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04002503 struct btrfs_root *root, u64 parent,
Chris Masone6dcd2d2008-07-17 12:53:50 -04002504 u64 root_objectid, u64 ref_generation,
2505 u64 owner, u64 owner_offset,
2506 struct btrfs_key *ins)
2507{
2508 int ret;
2509 int pending_ret;
2510 u64 super_used;
2511 u64 root_used;
2512 u64 num_bytes = ins->offset;
2513 u32 sizes[2];
2514 struct btrfs_fs_info *info = root->fs_info;
2515 struct btrfs_root *extent_root = info->extent_root;
2516 struct btrfs_extent_item *extent_item;
2517 struct btrfs_extent_ref *ref;
2518 struct btrfs_path *path;
2519 struct btrfs_key keys[2];
Chris Masonf2654de2007-06-26 12:20:46 -04002520
Zheng Yan31840ae2008-09-23 13:14:14 -04002521 if (parent == 0)
2522 parent = ins->objectid;
2523
Josef Bacik58176a92007-08-29 15:47:34 -04002524 /* block accounting for super block */
Chris Masona2135012008-06-25 16:01:30 -04002525 spin_lock_irq(&info->delalloc_lock);
Chris Masondb945352007-10-15 16:15:53 -04002526 super_used = btrfs_super_bytes_used(&info->super_copy);
2527 btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes);
Chris Masona2135012008-06-25 16:01:30 -04002528 spin_unlock_irq(&info->delalloc_lock);
Chris Mason26b80032007-08-08 20:17:12 -04002529
Josef Bacik58176a92007-08-29 15:47:34 -04002530 /* block accounting for root item */
Chris Masondb945352007-10-15 16:15:53 -04002531 root_used = btrfs_root_used(&root->root_item);
2532 btrfs_set_root_used(&root->root_item, root_used + num_bytes);
Josef Bacik58176a92007-08-29 15:47:34 -04002533
Chris Mason26b80032007-08-08 20:17:12 -04002534 if (root == extent_root) {
Zheng Yan31840ae2008-09-23 13:14:14 -04002535 struct pending_extent_op *extent_op;
2536
2537 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
2538 BUG_ON(!extent_op);
2539
2540 extent_op->type = PENDING_EXTENT_INSERT;
2541 extent_op->bytenr = ins->objectid;
2542 extent_op->num_bytes = ins->offset;
2543 extent_op->parent = parent;
2544 extent_op->orig_parent = 0;
2545 extent_op->generation = ref_generation;
2546 extent_op->orig_generation = 0;
2547 extent_op->level = (int)owner;
2548
Chris Mason1a5bc162007-10-15 16:15:26 -04002549 set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
2550 ins->objectid + ins->offset - 1,
2551 EXTENT_LOCKED, GFP_NOFS);
Zheng Yan31840ae2008-09-23 13:14:14 -04002552 set_state_private(&root->fs_info->extent_ins,
2553 ins->objectid, (unsigned long)extent_op);
Chris Mason26b80032007-08-08 20:17:12 -04002554 goto update_block;
2555 }
2556
Chris Mason47e4bb92008-02-01 14:51:59 -05002557 memcpy(&keys[0], ins, sizeof(*ins));
Chris Mason47e4bb92008-02-01 14:51:59 -05002558 keys[1].objectid = ins->objectid;
2559 keys[1].type = BTRFS_EXTENT_REF_KEY;
Zheng Yan31840ae2008-09-23 13:14:14 -04002560 keys[1].offset = parent;
Chris Mason47e4bb92008-02-01 14:51:59 -05002561 sizes[0] = sizeof(*extent_item);
2562 sizes[1] = sizeof(*ref);
Chris Mason7bb86312007-12-11 09:25:06 -05002563
2564 path = btrfs_alloc_path();
2565 BUG_ON(!path);
Chris Mason47e4bb92008-02-01 14:51:59 -05002566
2567 ret = btrfs_insert_empty_items(trans, extent_root, path, keys,
2568 sizes, 2);
Chris Masonccd467d2007-06-28 15:57:36 -04002569 BUG_ON(ret);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002570
Chris Mason47e4bb92008-02-01 14:51:59 -05002571 extent_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2572 struct btrfs_extent_item);
2573 btrfs_set_extent_refs(path->nodes[0], extent_item, 1);
2574 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
2575 struct btrfs_extent_ref);
2576
2577 btrfs_set_ref_root(path->nodes[0], ref, root_objectid);
2578 btrfs_set_ref_generation(path->nodes[0], ref, ref_generation);
2579 btrfs_set_ref_objectid(path->nodes[0], ref, owner);
2580 btrfs_set_ref_offset(path->nodes[0], ref, owner_offset);
Zheng Yan31840ae2008-09-23 13:14:14 -04002581 btrfs_set_ref_num_refs(path->nodes[0], ref, 1);
Chris Mason47e4bb92008-02-01 14:51:59 -05002582
2583 btrfs_mark_buffer_dirty(path->nodes[0]);
2584
2585 trans->alloc_exclude_start = 0;
2586 trans->alloc_exclude_nr = 0;
Chris Mason7bb86312007-12-11 09:25:06 -05002587 btrfs_free_path(path);
Chris Masone089f052007-03-16 16:20:31 -04002588 finish_current_insert(trans, extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -04002589 pending_ret = del_pending_extents(trans, extent_root);
Chris Masonf510cfe2007-10-15 16:14:48 -04002590
Chris Mason925baed2008-06-25 16:01:30 -04002591 if (ret)
2592 goto out;
Chris Masone37c9e62007-05-09 20:13:14 -04002593 if (pending_ret) {
Chris Mason925baed2008-06-25 16:01:30 -04002594 ret = pending_ret;
2595 goto out;
Chris Masone37c9e62007-05-09 20:13:14 -04002596 }
Chris Mason26b80032007-08-08 20:17:12 -04002597
2598update_block:
Chris Mason0b86a832008-03-24 15:01:56 -04002599 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0);
Chris Masonf5947062008-02-04 10:10:13 -05002600 if (ret) {
2601 printk("update block group failed for %Lu %Lu\n",
2602 ins->objectid, ins->offset);
2603 BUG();
2604 }
Chris Mason925baed2008-06-25 16:01:30 -04002605out:
Chris Masone6dcd2d2008-07-17 12:53:50 -04002606 return ret;
2607}
2608
2609int btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04002610 struct btrfs_root *root, u64 parent,
Chris Masone6dcd2d2008-07-17 12:53:50 -04002611 u64 root_objectid, u64 ref_generation,
2612 u64 owner, u64 owner_offset,
2613 struct btrfs_key *ins)
2614{
2615 int ret;
Chris Mason1c2308f2008-09-23 13:14:13 -04002616
2617 if (root_objectid == BTRFS_TREE_LOG_OBJECTID)
2618 return 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002619 maybe_lock_mutex(root);
Zheng Yan31840ae2008-09-23 13:14:14 -04002620 ret = __btrfs_alloc_reserved_extent(trans, root, parent,
2621 root_objectid, ref_generation,
2622 owner, owner_offset, ins);
Zheng Yane8569812008-09-26 10:05:48 -04002623 update_reserved_extents(root, ins->objectid, ins->offset, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002624 maybe_unlock_mutex(root);
2625 return ret;
2626}
Chris Masone02119d2008-09-05 16:13:11 -04002627
2628/*
2629 * this is used by the tree logging recovery code. It records that
2630 * an extent has been allocated and makes sure to clear the free
2631 * space cache bits as well
2632 */
2633int btrfs_alloc_logged_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04002634 struct btrfs_root *root, u64 parent,
Chris Masone02119d2008-09-05 16:13:11 -04002635 u64 root_objectid, u64 ref_generation,
2636 u64 owner, u64 owner_offset,
2637 struct btrfs_key *ins)
2638{
2639 int ret;
2640 struct btrfs_block_group_cache *block_group;
2641
2642 maybe_lock_mutex(root);
2643 block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
2644 cache_block_group(root, block_group);
2645
Josef Bacik0f9dd462008-09-23 13:14:11 -04002646 ret = btrfs_remove_free_space(block_group, ins->objectid, ins->offset);
2647 BUG_ON(ret);
Zheng Yan31840ae2008-09-23 13:14:14 -04002648 ret = __btrfs_alloc_reserved_extent(trans, root, parent,
2649 root_objectid, ref_generation,
2650 owner, owner_offset, ins);
Chris Masone02119d2008-09-05 16:13:11 -04002651 maybe_unlock_mutex(root);
2652 return ret;
2653}
2654
Chris Masone6dcd2d2008-07-17 12:53:50 -04002655/*
2656 * finds a free extent and does all the dirty work required for allocation
2657 * returns the key for the extent through ins, and a tree buffer for
2658 * the first block of the extent through buf.
2659 *
2660 * returns 0 if everything worked, non-zero otherwise.
2661 */
2662int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
2663 struct btrfs_root *root,
Zheng Yan31840ae2008-09-23 13:14:14 -04002664 u64 num_bytes, u64 parent, u64 min_alloc_size,
Chris Masone6dcd2d2008-07-17 12:53:50 -04002665 u64 root_objectid, u64 ref_generation,
Zheng Yan31840ae2008-09-23 13:14:14 -04002666 u64 owner_objectid, u64 owner_offset,
Chris Masone6dcd2d2008-07-17 12:53:50 -04002667 u64 empty_size, u64 hint_byte,
2668 u64 search_end, struct btrfs_key *ins, u64 data)
2669{
2670 int ret;
2671
2672 maybe_lock_mutex(root);
2673
2674 ret = __btrfs_reserve_extent(trans, root, num_bytes,
2675 min_alloc_size, empty_size, hint_byte,
2676 search_end, ins, data);
2677 BUG_ON(ret);
Chris Masond00aff02008-09-11 15:54:42 -04002678 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
Zheng Yan31840ae2008-09-23 13:14:14 -04002679 ret = __btrfs_alloc_reserved_extent(trans, root, parent,
2680 root_objectid, ref_generation,
2681 owner_objectid, owner_offset, ins);
Chris Masond00aff02008-09-11 15:54:42 -04002682 BUG_ON(ret);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002683
Zheng Yane8569812008-09-26 10:05:48 -04002684 } else {
2685 update_reserved_extents(root, ins->objectid, ins->offset, 1);
Chris Masond00aff02008-09-11 15:54:42 -04002686 }
Chris Mason925baed2008-06-25 16:01:30 -04002687 maybe_unlock_mutex(root);
2688 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05002689}
Chris Mason65b51a02008-08-01 15:11:20 -04002690
2691struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
2692 struct btrfs_root *root,
2693 u64 bytenr, u32 blocksize)
2694{
2695 struct extent_buffer *buf;
2696
2697 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
2698 if (!buf)
2699 return ERR_PTR(-ENOMEM);
2700 btrfs_set_header_generation(buf, trans->transid);
2701 btrfs_tree_lock(buf);
2702 clean_tree_block(trans, root, buf);
2703 btrfs_set_buffer_uptodate(buf);
Chris Masond0c803c2008-09-11 16:17:57 -04002704 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
2705 set_extent_dirty(&root->dirty_log_pages, buf->start,
Chris Mason65b51a02008-08-01 15:11:20 -04002706 buf->start + buf->len - 1, GFP_NOFS);
Chris Masond0c803c2008-09-11 16:17:57 -04002707 } else {
2708 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
2709 buf->start + buf->len - 1, GFP_NOFS);
2710 }
Chris Mason65b51a02008-08-01 15:11:20 -04002711 trans->blocks_used++;
2712 return buf;
2713}
2714
Chris Masonfec577f2007-02-26 10:40:21 -05002715/*
2716 * helper function to allocate a block for a given tree
2717 * returns the tree buffer or NULL.
2718 */
Chris Mason5f39d392007-10-15 16:14:19 -04002719struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Chris Masondb945352007-10-15 16:15:53 -04002720 struct btrfs_root *root,
Zheng Yan31840ae2008-09-23 13:14:14 -04002721 u32 blocksize, u64 parent,
Chris Mason7bb86312007-12-11 09:25:06 -05002722 u64 root_objectid,
2723 u64 ref_generation,
Chris Mason7bb86312007-12-11 09:25:06 -05002724 int level,
2725 u64 hint,
Chris Mason5f39d392007-10-15 16:14:19 -04002726 u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05002727{
Chris Masone2fa7222007-03-12 16:22:34 -04002728 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -05002729 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04002730 struct extent_buffer *buf;
Chris Masonfec577f2007-02-26 10:40:21 -05002731
Zheng Yan31840ae2008-09-23 13:14:14 -04002732 ret = btrfs_alloc_extent(trans, root, blocksize, parent, blocksize,
2733 root_objectid, ref_generation, level, 0,
2734 empty_size, hint, (u64)-1, &ins, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05002735 if (ret) {
Chris Mason54aa1f42007-06-22 14:16:25 -04002736 BUG_ON(ret > 0);
2737 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05002738 }
Chris Mason55c69072008-01-09 15:55:33 -05002739
Chris Mason65b51a02008-08-01 15:11:20 -04002740 buf = btrfs_init_new_buffer(trans, root, ins.objectid, blocksize);
Chris Masonfec577f2007-02-26 10:40:21 -05002741 return buf;
2742}
Chris Masona28ec192007-03-06 20:08:01 -05002743
Chris Masone02119d2008-09-05 16:13:11 -04002744int btrfs_drop_leaf_ref(struct btrfs_trans_handle *trans,
2745 struct btrfs_root *root, struct extent_buffer *leaf)
Chris Mason6407bf62007-03-27 06:33:00 -04002746{
Chris Mason7bb86312007-12-11 09:25:06 -05002747 u64 leaf_owner;
2748 u64 leaf_generation;
Chris Mason5f39d392007-10-15 16:14:19 -04002749 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -04002750 struct btrfs_file_extent_item *fi;
2751 int i;
2752 int nritems;
2753 int ret;
2754
Chris Mason5f39d392007-10-15 16:14:19 -04002755 BUG_ON(!btrfs_is_leaf(leaf));
2756 nritems = btrfs_header_nritems(leaf);
Chris Mason7bb86312007-12-11 09:25:06 -05002757 leaf_owner = btrfs_header_owner(leaf);
2758 leaf_generation = btrfs_header_generation(leaf);
2759
Chris Mason6407bf62007-03-27 06:33:00 -04002760 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04002761 u64 disk_bytenr;
Chris Masone34a5b42008-07-22 12:08:37 -04002762 cond_resched();
Chris Mason5f39d392007-10-15 16:14:19 -04002763
2764 btrfs_item_key_to_cpu(leaf, &key, i);
2765 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason6407bf62007-03-27 06:33:00 -04002766 continue;
2767 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002768 if (btrfs_file_extent_type(leaf, fi) ==
2769 BTRFS_FILE_EXTENT_INLINE)
Chris Mason236454df2007-04-19 13:37:44 -04002770 continue;
Chris Mason6407bf62007-03-27 06:33:00 -04002771 /*
2772 * FIXME make sure to insert a trans record that
2773 * repeats the snapshot del on crash
2774 */
Chris Masondb945352007-10-15 16:15:53 -04002775 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
2776 if (disk_bytenr == 0)
Chris Mason3a686372007-05-24 13:35:57 -04002777 continue;
Chris Mason4a096752008-07-21 10:29:44 -04002778
2779 mutex_lock(&root->fs_info->alloc_mutex);
Chris Mason925baed2008-06-25 16:01:30 -04002780 ret = __btrfs_free_extent(trans, root, disk_bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -05002781 btrfs_file_extent_disk_num_bytes(leaf, fi),
Zheng Yan31840ae2008-09-23 13:14:14 -04002782 leaf->start, leaf_owner, leaf_generation,
Chris Mason7bb86312007-12-11 09:25:06 -05002783 key.objectid, key.offset, 0);
Chris Mason4a096752008-07-21 10:29:44 -04002784 mutex_unlock(&root->fs_info->alloc_mutex);
Zheng Yan31840ae2008-09-23 13:14:14 -04002785 BUG_ON(ret);
Chris Mason2dd3e672008-08-04 08:20:15 -04002786
2787 atomic_inc(&root->fs_info->throttle_gen);
2788 wake_up(&root->fs_info->transaction_throttle);
2789 cond_resched();
Chris Mason6407bf62007-03-27 06:33:00 -04002790 }
2791 return 0;
2792}
2793
Chris Masone02119d2008-09-05 16:13:11 -04002794static int noinline cache_drop_leaf_ref(struct btrfs_trans_handle *trans,
2795 struct btrfs_root *root,
2796 struct btrfs_leaf_ref *ref)
Yan Zheng31153d82008-07-28 15:32:19 -04002797{
2798 int i;
2799 int ret;
2800 struct btrfs_extent_info *info = ref->extents;
2801
Yan Zheng31153d82008-07-28 15:32:19 -04002802 for (i = 0; i < ref->nritems; i++) {
2803 mutex_lock(&root->fs_info->alloc_mutex);
Zheng Yan31840ae2008-09-23 13:14:14 -04002804 ret = __btrfs_free_extent(trans, root, info->bytenr,
2805 info->num_bytes, ref->bytenr,
2806 ref->owner, ref->generation,
2807 info->objectid, info->offset, 0);
Yan Zheng31153d82008-07-28 15:32:19 -04002808 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Mason2dd3e672008-08-04 08:20:15 -04002809
2810 atomic_inc(&root->fs_info->throttle_gen);
2811 wake_up(&root->fs_info->transaction_throttle);
2812 cond_resched();
2813
Yan Zheng31153d82008-07-28 15:32:19 -04002814 BUG_ON(ret);
2815 info++;
2816 }
Yan Zheng31153d82008-07-28 15:32:19 -04002817
2818 return 0;
2819}
2820
Chris Mason333db942008-06-25 16:01:30 -04002821int drop_snap_lookup_refcount(struct btrfs_root *root, u64 start, u64 len,
2822 u32 *refs)
2823{
Chris Mason017e5362008-07-28 15:32:51 -04002824 int ret;
Chris Masonf87f0572008-08-01 11:27:23 -04002825
Zheng Yan31840ae2008-09-23 13:14:14 -04002826 ret = btrfs_lookup_extent_ref(NULL, root, start, len, refs);
Chris Masonf87f0572008-08-01 11:27:23 -04002827 BUG_ON(ret);
2828
2829#if 0 // some debugging code in case we see problems here
2830 /* if the refs count is one, it won't get increased again. But
2831 * if the ref count is > 1, someone may be decreasing it at
2832 * the same time we are.
2833 */
2834 if (*refs != 1) {
2835 struct extent_buffer *eb = NULL;
2836 eb = btrfs_find_create_tree_block(root, start, len);
2837 if (eb)
2838 btrfs_tree_lock(eb);
2839
2840 mutex_lock(&root->fs_info->alloc_mutex);
2841 ret = lookup_extent_ref(NULL, root, start, len, refs);
2842 BUG_ON(ret);
2843 mutex_unlock(&root->fs_info->alloc_mutex);
2844
2845 if (eb) {
2846 btrfs_tree_unlock(eb);
2847 free_extent_buffer(eb);
2848 }
2849 if (*refs == 1) {
2850 printk("block %llu went down to one during drop_snap\n",
2851 (unsigned long long)start);
2852 }
2853
2854 }
2855#endif
2856
Chris Masone7a84562008-06-25 16:01:31 -04002857 cond_resched();
Chris Mason017e5362008-07-28 15:32:51 -04002858 return ret;
Chris Mason333db942008-06-25 16:01:30 -04002859}
2860
2861/*
Chris Mason9aca1d52007-03-13 11:09:37 -04002862 * helper function for drop_snapshot, this walks down the tree dropping ref
2863 * counts as it goes.
2864 */
Chris Mason98ed5172008-01-03 10:01:48 -05002865static int noinline walk_down_tree(struct btrfs_trans_handle *trans,
2866 struct btrfs_root *root,
2867 struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05002868{
Chris Mason7bb86312007-12-11 09:25:06 -05002869 u64 root_owner;
2870 u64 root_gen;
2871 u64 bytenr;
Chris Masonca7a79a2008-05-12 12:59:19 -04002872 u64 ptr_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04002873 struct extent_buffer *next;
2874 struct extent_buffer *cur;
Chris Mason7bb86312007-12-11 09:25:06 -05002875 struct extent_buffer *parent;
Yan Zheng31153d82008-07-28 15:32:19 -04002876 struct btrfs_leaf_ref *ref;
Chris Masondb945352007-10-15 16:15:53 -04002877 u32 blocksize;
Chris Mason20524f02007-03-10 06:35:47 -05002878 int ret;
2879 u32 refs;
2880
Chris Mason5caf2a02007-04-02 11:20:42 -04002881 WARN_ON(*level < 0);
2882 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason333db942008-06-25 16:01:30 -04002883 ret = drop_snap_lookup_refcount(root, path->nodes[*level]->start,
Chris Masondb945352007-10-15 16:15:53 -04002884 path->nodes[*level]->len, &refs);
Chris Mason20524f02007-03-10 06:35:47 -05002885 BUG_ON(ret);
2886 if (refs > 1)
2887 goto out;
Chris Masone0115992007-06-19 16:23:05 -04002888
Chris Mason9aca1d52007-03-13 11:09:37 -04002889 /*
2890 * walk down to the last node level and free all the leaves
2891 */
Chris Mason6407bf62007-03-27 06:33:00 -04002892 while(*level >= 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -04002893 WARN_ON(*level < 0);
2894 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason20524f02007-03-10 06:35:47 -05002895 cur = path->nodes[*level];
Chris Masone0115992007-06-19 16:23:05 -04002896
Chris Mason5f39d392007-10-15 16:14:19 -04002897 if (btrfs_header_level(cur) != *level)
Chris Mason2c90e5d2007-04-02 10:50:19 -04002898 WARN_ON(1);
Chris Masone0115992007-06-19 16:23:05 -04002899
Chris Mason7518a232007-03-12 12:01:18 -04002900 if (path->slots[*level] >=
Chris Mason5f39d392007-10-15 16:14:19 -04002901 btrfs_header_nritems(cur))
Chris Mason20524f02007-03-10 06:35:47 -05002902 break;
Chris Mason6407bf62007-03-27 06:33:00 -04002903 if (*level == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002904 ret = btrfs_drop_leaf_ref(trans, root, cur);
Chris Mason6407bf62007-03-27 06:33:00 -04002905 BUG_ON(ret);
2906 break;
2907 }
Chris Masondb945352007-10-15 16:15:53 -04002908 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
Chris Masonca7a79a2008-05-12 12:59:19 -04002909 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
Chris Masondb945352007-10-15 16:15:53 -04002910 blocksize = btrfs_level_size(root, *level - 1);
Chris Mason925baed2008-06-25 16:01:30 -04002911
Chris Mason333db942008-06-25 16:01:30 -04002912 ret = drop_snap_lookup_refcount(root, bytenr, blocksize, &refs);
Chris Mason6407bf62007-03-27 06:33:00 -04002913 BUG_ON(ret);
2914 if (refs != 1) {
Chris Mason7bb86312007-12-11 09:25:06 -05002915 parent = path->nodes[*level];
2916 root_owner = btrfs_header_owner(parent);
2917 root_gen = btrfs_header_generation(parent);
Chris Mason20524f02007-03-10 06:35:47 -05002918 path->slots[*level]++;
Chris Masonf87f0572008-08-01 11:27:23 -04002919
2920 mutex_lock(&root->fs_info->alloc_mutex);
Chris Mason925baed2008-06-25 16:01:30 -04002921 ret = __btrfs_free_extent(trans, root, bytenr,
Zheng Yan31840ae2008-09-23 13:14:14 -04002922 blocksize, parent->start,
2923 root_owner, root_gen, 0, 0, 1);
Chris Mason20524f02007-03-10 06:35:47 -05002924 BUG_ON(ret);
Chris Masonf87f0572008-08-01 11:27:23 -04002925 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Mason18e35e0a2008-08-01 13:11:41 -04002926
2927 atomic_inc(&root->fs_info->throttle_gen);
2928 wake_up(&root->fs_info->transaction_throttle);
Chris Mason2dd3e672008-08-04 08:20:15 -04002929 cond_resched();
Chris Mason18e35e0a2008-08-01 13:11:41 -04002930
Chris Mason20524f02007-03-10 06:35:47 -05002931 continue;
2932 }
Chris Masonf87f0572008-08-01 11:27:23 -04002933 /*
2934 * at this point, we have a single ref, and since the
2935 * only place referencing this extent is a dead root
2936 * the reference count should never go higher.
2937 * So, we don't need to check it again
2938 */
Yan Zheng31153d82008-07-28 15:32:19 -04002939 if (*level == 1) {
Chris Mason017e5362008-07-28 15:32:51 -04002940 ref = btrfs_lookup_leaf_ref(root, bytenr);
Yan Zheng31153d82008-07-28 15:32:19 -04002941 if (ref) {
Chris Masone02119d2008-09-05 16:13:11 -04002942 ret = cache_drop_leaf_ref(trans, root, ref);
Yan Zheng31153d82008-07-28 15:32:19 -04002943 BUG_ON(ret);
2944 btrfs_remove_leaf_ref(root, ref);
Yanbcc63ab2008-07-30 16:29:20 -04002945 btrfs_free_leaf_ref(root, ref);
Yan Zheng31153d82008-07-28 15:32:19 -04002946 *level = 0;
2947 break;
2948 }
Chris Mason37d1aee2008-07-31 10:48:37 -04002949 if (printk_ratelimit())
2950 printk("leaf ref miss for bytenr %llu\n",
2951 (unsigned long long)bytenr);
Yan Zheng31153d82008-07-28 15:32:19 -04002952 }
Chris Masondb945352007-10-15 16:15:53 -04002953 next = btrfs_find_tree_block(root, bytenr, blocksize);
Chris Mason1259ab72008-05-12 13:39:03 -04002954 if (!next || !btrfs_buffer_uptodate(next, ptr_gen)) {
Chris Mason5f39d392007-10-15 16:14:19 -04002955 free_extent_buffer(next);
Chris Mason333db942008-06-25 16:01:30 -04002956
Chris Masonca7a79a2008-05-12 12:59:19 -04002957 next = read_tree_block(root, bytenr, blocksize,
2958 ptr_gen);
Chris Masone7a84562008-06-25 16:01:31 -04002959 cond_resched();
Chris Masonf87f0572008-08-01 11:27:23 -04002960#if 0
2961 /*
2962 * this is a debugging check and can go away
2963 * the ref should never go all the way down to 1
2964 * at this point
2965 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04002966 ret = lookup_extent_ref(NULL, root, bytenr, blocksize,
2967 &refs);
Chris Masone9d0b132007-08-10 14:06:19 -04002968 BUG_ON(ret);
Chris Masonf87f0572008-08-01 11:27:23 -04002969 WARN_ON(refs != 1);
2970#endif
Chris Masone9d0b132007-08-10 14:06:19 -04002971 }
Chris Mason5caf2a02007-04-02 11:20:42 -04002972 WARN_ON(*level <= 0);
Chris Mason83e15a22007-03-12 09:03:27 -04002973 if (path->nodes[*level-1])
Chris Mason5f39d392007-10-15 16:14:19 -04002974 free_extent_buffer(path->nodes[*level-1]);
Chris Mason20524f02007-03-10 06:35:47 -05002975 path->nodes[*level-1] = next;
Chris Mason5f39d392007-10-15 16:14:19 -04002976 *level = btrfs_header_level(next);
Chris Mason20524f02007-03-10 06:35:47 -05002977 path->slots[*level] = 0;
Chris Mason2dd3e672008-08-04 08:20:15 -04002978 cond_resched();
Chris Mason20524f02007-03-10 06:35:47 -05002979 }
2980out:
Chris Mason5caf2a02007-04-02 11:20:42 -04002981 WARN_ON(*level < 0);
2982 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason7bb86312007-12-11 09:25:06 -05002983
2984 if (path->nodes[*level] == root->node) {
Chris Mason7bb86312007-12-11 09:25:06 -05002985 parent = path->nodes[*level];
Yan Zheng31153d82008-07-28 15:32:19 -04002986 bytenr = path->nodes[*level]->start;
Chris Mason7bb86312007-12-11 09:25:06 -05002987 } else {
2988 parent = path->nodes[*level + 1];
Yan Zheng31153d82008-07-28 15:32:19 -04002989 bytenr = btrfs_node_blockptr(parent, path->slots[*level + 1]);
Chris Mason7bb86312007-12-11 09:25:06 -05002990 }
2991
Yan Zheng31153d82008-07-28 15:32:19 -04002992 blocksize = btrfs_level_size(root, *level);
2993 root_owner = btrfs_header_owner(parent);
Chris Mason7bb86312007-12-11 09:25:06 -05002994 root_gen = btrfs_header_generation(parent);
Yan Zheng31153d82008-07-28 15:32:19 -04002995
Chris Masonf87f0572008-08-01 11:27:23 -04002996 mutex_lock(&root->fs_info->alloc_mutex);
Yan Zheng31153d82008-07-28 15:32:19 -04002997 ret = __btrfs_free_extent(trans, root, bytenr, blocksize,
Zheng Yan31840ae2008-09-23 13:14:14 -04002998 parent->start, root_owner, root_gen,
2999 0, 0, 1);
3000 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Mason5f39d392007-10-15 16:14:19 -04003001 free_extent_buffer(path->nodes[*level]);
Chris Mason20524f02007-03-10 06:35:47 -05003002 path->nodes[*level] = NULL;
3003 *level += 1;
3004 BUG_ON(ret);
Chris Masonf87f0572008-08-01 11:27:23 -04003005
Chris Masone7a84562008-06-25 16:01:31 -04003006 cond_resched();
Chris Mason20524f02007-03-10 06:35:47 -05003007 return 0;
3008}
3009
Chris Mason9aca1d52007-03-13 11:09:37 -04003010/*
3011 * helper for dropping snapshots. This walks back up the tree in the path
3012 * to find the first node higher up where we haven't yet gone through
3013 * all the slots
3014 */
Chris Mason98ed5172008-01-03 10:01:48 -05003015static int noinline walk_up_tree(struct btrfs_trans_handle *trans,
3016 struct btrfs_root *root,
3017 struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05003018{
Chris Mason7bb86312007-12-11 09:25:06 -05003019 u64 root_owner;
3020 u64 root_gen;
3021 struct btrfs_root_item *root_item = &root->root_item;
Chris Mason20524f02007-03-10 06:35:47 -05003022 int i;
3023 int slot;
3024 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04003025
Chris Mason234b63a2007-03-13 10:46:10 -04003026 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Mason20524f02007-03-10 06:35:47 -05003027 slot = path->slots[i];
Chris Mason5f39d392007-10-15 16:14:19 -04003028 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
3029 struct extent_buffer *node;
3030 struct btrfs_disk_key disk_key;
3031 node = path->nodes[i];
Chris Mason20524f02007-03-10 06:35:47 -05003032 path->slots[i]++;
3033 *level = i;
Chris Mason9f3a7422007-08-07 15:52:19 -04003034 WARN_ON(*level == 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003035 btrfs_node_key(node, &disk_key, path->slots[i]);
Chris Mason9f3a7422007-08-07 15:52:19 -04003036 memcpy(&root_item->drop_progress,
Chris Mason5f39d392007-10-15 16:14:19 -04003037 &disk_key, sizeof(disk_key));
Chris Mason9f3a7422007-08-07 15:52:19 -04003038 root_item->drop_level = i;
Chris Mason20524f02007-03-10 06:35:47 -05003039 return 0;
3040 } else {
Zheng Yan31840ae2008-09-23 13:14:14 -04003041 struct extent_buffer *parent;
3042 if (path->nodes[*level] == root->node)
3043 parent = path->nodes[*level];
3044 else
3045 parent = path->nodes[*level + 1];
3046
3047 root_owner = btrfs_header_owner(parent);
3048 root_gen = btrfs_header_generation(parent);
Chris Masone089f052007-03-16 16:20:31 -04003049 ret = btrfs_free_extent(trans, root,
Chris Masondb945352007-10-15 16:15:53 -04003050 path->nodes[*level]->start,
Chris Mason7bb86312007-12-11 09:25:06 -05003051 path->nodes[*level]->len,
Zheng Yan31840ae2008-09-23 13:14:14 -04003052 parent->start,
Chris Mason7bb86312007-12-11 09:25:06 -05003053 root_owner, root_gen, 0, 0, 1);
Chris Mason6407bf62007-03-27 06:33:00 -04003054 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04003055 free_extent_buffer(path->nodes[*level]);
Chris Mason83e15a22007-03-12 09:03:27 -04003056 path->nodes[*level] = NULL;
Chris Mason20524f02007-03-10 06:35:47 -05003057 *level = i + 1;
Chris Mason20524f02007-03-10 06:35:47 -05003058 }
3059 }
3060 return 1;
3061}
3062
Chris Mason9aca1d52007-03-13 11:09:37 -04003063/*
3064 * drop the reference count on the tree rooted at 'snap'. This traverses
3065 * the tree freeing any blocks that have a ref count of zero after being
3066 * decremented.
3067 */
Chris Masone089f052007-03-16 16:20:31 -04003068int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason9f3a7422007-08-07 15:52:19 -04003069 *root)
Chris Mason20524f02007-03-10 06:35:47 -05003070{
Chris Mason3768f362007-03-13 16:47:54 -04003071 int ret = 0;
Chris Mason9aca1d52007-03-13 11:09:37 -04003072 int wret;
Chris Mason20524f02007-03-10 06:35:47 -05003073 int level;
Chris Mason5caf2a02007-04-02 11:20:42 -04003074 struct btrfs_path *path;
Chris Mason20524f02007-03-10 06:35:47 -05003075 int i;
3076 int orig_level;
Chris Mason9f3a7422007-08-07 15:52:19 -04003077 struct btrfs_root_item *root_item = &root->root_item;
Chris Mason20524f02007-03-10 06:35:47 -05003078
Chris Masona2135012008-06-25 16:01:30 -04003079 WARN_ON(!mutex_is_locked(&root->fs_info->drop_mutex));
Chris Mason5caf2a02007-04-02 11:20:42 -04003080 path = btrfs_alloc_path();
3081 BUG_ON(!path);
Chris Mason20524f02007-03-10 06:35:47 -05003082
Chris Mason5f39d392007-10-15 16:14:19 -04003083 level = btrfs_header_level(root->node);
Chris Mason20524f02007-03-10 06:35:47 -05003084 orig_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04003085 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
3086 path->nodes[level] = root->node;
Chris Masonf510cfe2007-10-15 16:14:48 -04003087 extent_buffer_get(root->node);
Chris Mason9f3a7422007-08-07 15:52:19 -04003088 path->slots[level] = 0;
3089 } else {
3090 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04003091 struct btrfs_disk_key found_key;
3092 struct extent_buffer *node;
Chris Mason6702ed42007-08-07 16:15:09 -04003093
Chris Mason9f3a7422007-08-07 15:52:19 -04003094 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Chris Mason6702ed42007-08-07 16:15:09 -04003095 level = root_item->drop_level;
3096 path->lowest_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04003097 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason6702ed42007-08-07 16:15:09 -04003098 if (wret < 0) {
Chris Mason9f3a7422007-08-07 15:52:19 -04003099 ret = wret;
3100 goto out;
3101 }
Chris Mason5f39d392007-10-15 16:14:19 -04003102 node = path->nodes[level];
3103 btrfs_node_key(node, &found_key, path->slots[level]);
3104 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
3105 sizeof(found_key)));
Chris Mason7d9eb122008-07-08 14:19:17 -04003106 /*
3107 * unlock our path, this is safe because only this
3108 * function is allowed to delete this snapshot
3109 */
Chris Mason925baed2008-06-25 16:01:30 -04003110 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
3111 if (path->nodes[i] && path->locks[i]) {
3112 path->locks[i] = 0;
3113 btrfs_tree_unlock(path->nodes[i]);
3114 }
3115 }
Chris Mason9f3a7422007-08-07 15:52:19 -04003116 }
Chris Mason20524f02007-03-10 06:35:47 -05003117 while(1) {
Chris Mason5caf2a02007-04-02 11:20:42 -04003118 wret = walk_down_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04003119 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05003120 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04003121 if (wret < 0)
3122 ret = wret;
3123
Chris Mason5caf2a02007-04-02 11:20:42 -04003124 wret = walk_up_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04003125 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05003126 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04003127 if (wret < 0)
3128 ret = wret;
Chris Masone7a84562008-06-25 16:01:31 -04003129 if (trans->transaction->in_commit) {
3130 ret = -EAGAIN;
3131 break;
3132 }
Chris Mason18e35e0a2008-08-01 13:11:41 -04003133 atomic_inc(&root->fs_info->throttle_gen);
Chris Mason017e5362008-07-28 15:32:51 -04003134 wake_up(&root->fs_info->transaction_throttle);
Chris Mason20524f02007-03-10 06:35:47 -05003135 }
Chris Mason83e15a22007-03-12 09:03:27 -04003136 for (i = 0; i <= orig_level; i++) {
Chris Mason5caf2a02007-04-02 11:20:42 -04003137 if (path->nodes[i]) {
Chris Mason5f39d392007-10-15 16:14:19 -04003138 free_extent_buffer(path->nodes[i]);
Chris Mason0f827312007-10-15 16:18:56 -04003139 path->nodes[i] = NULL;
Chris Mason83e15a22007-03-12 09:03:27 -04003140 }
Chris Mason20524f02007-03-10 06:35:47 -05003141 }
Chris Mason9f3a7422007-08-07 15:52:19 -04003142out:
Chris Mason5caf2a02007-04-02 11:20:42 -04003143 btrfs_free_path(path);
Chris Mason9aca1d52007-03-13 11:09:37 -04003144 return ret;
Chris Mason20524f02007-03-10 06:35:47 -05003145}
Chris Mason9078a3e2007-04-26 16:46:15 -04003146
Chris Masonbe744172007-05-06 10:15:01 -04003147int btrfs_free_block_groups(struct btrfs_fs_info *info)
3148{
Josef Bacik0f9dd462008-09-23 13:14:11 -04003149 struct btrfs_block_group_cache *block_group;
3150 struct rb_node *n;
Chris Mason925baed2008-06-25 16:01:30 -04003151
3152 mutex_lock(&info->alloc_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003153 spin_lock(&info->block_group_cache_lock);
3154 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
3155 block_group = rb_entry(n, struct btrfs_block_group_cache,
3156 cache_node);
3157
Chris Mason4434c332008-09-24 12:50:10 -04003158 spin_unlock(&info->block_group_cache_lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003159 btrfs_remove_free_space_cache(block_group);
Chris Mason4434c332008-09-24 12:50:10 -04003160 spin_lock(&info->block_group_cache_lock);
3161
Josef Bacik0f9dd462008-09-23 13:14:11 -04003162 rb_erase(&block_group->cache_node,
3163 &info->block_group_cache_tree);
Chris Mason4434c332008-09-24 12:50:10 -04003164
Josef Bacik0f9dd462008-09-23 13:14:11 -04003165 spin_lock(&block_group->space_info->lock);
3166 list_del(&block_group->list);
3167 spin_unlock(&block_group->space_info->lock);
3168 kfree(block_group);
Chris Mason96b51792007-10-15 16:15:19 -04003169 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04003170 spin_unlock(&info->block_group_cache_lock);
Chris Mason925baed2008-06-25 16:01:30 -04003171 mutex_unlock(&info->alloc_mutex);
Chris Masonbe744172007-05-06 10:15:01 -04003172 return 0;
3173}
3174
Chris Mason8e7bf942008-04-28 09:02:36 -04003175static unsigned long calc_ra(unsigned long start, unsigned long last,
3176 unsigned long nr)
3177{
3178 return min(last, start + nr - 1);
3179}
3180
Chris Mason98ed5172008-01-03 10:01:48 -05003181static int noinline relocate_inode_pages(struct inode *inode, u64 start,
3182 u64 len)
Chris Masonedbd8d42007-12-21 16:27:24 -05003183{
3184 u64 page_start;
3185 u64 page_end;
Chris Masonedbd8d42007-12-21 16:27:24 -05003186 unsigned long last_index;
Chris Masonedbd8d42007-12-21 16:27:24 -05003187 unsigned long i;
3188 struct page *page;
Chris Masond1310b22008-01-24 16:13:08 -05003189 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason4313b392008-01-03 09:08:48 -05003190 struct file_ra_state *ra;
Chris Mason8e7bf942008-04-28 09:02:36 -04003191 unsigned long total_read = 0;
3192 unsigned long ra_pages;
Chris Mason3eaa2882008-07-24 11:57:52 -04003193 struct btrfs_ordered_extent *ordered;
Chris Masona061fc82008-05-07 11:43:44 -04003194 struct btrfs_trans_handle *trans;
Chris Mason4313b392008-01-03 09:08:48 -05003195
3196 ra = kzalloc(sizeof(*ra), GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05003197
3198 mutex_lock(&inode->i_mutex);
Chris Mason4313b392008-01-03 09:08:48 -05003199 i = start >> PAGE_CACHE_SHIFT;
Chris Masonedbd8d42007-12-21 16:27:24 -05003200 last_index = (start + len - 1) >> PAGE_CACHE_SHIFT;
3201
Chris Mason8e7bf942008-04-28 09:02:36 -04003202 ra_pages = BTRFS_I(inode)->root->fs_info->bdi.ra_pages;
3203
Chris Mason4313b392008-01-03 09:08:48 -05003204 file_ra_state_init(ra, inode->i_mapping);
Chris Masonedbd8d42007-12-21 16:27:24 -05003205
Chris Mason4313b392008-01-03 09:08:48 -05003206 for (; i <= last_index; i++) {
Chris Mason8e7bf942008-04-28 09:02:36 -04003207 if (total_read % ra_pages == 0) {
3208 btrfs_force_ra(inode->i_mapping, ra, NULL, i,
3209 calc_ra(i, last_index, ra_pages));
3210 }
3211 total_read++;
Chris Mason3eaa2882008-07-24 11:57:52 -04003212again:
3213 if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode))
Chris Masona061fc82008-05-07 11:43:44 -04003214 goto truncate_racing;
Chris Masonedbd8d42007-12-21 16:27:24 -05003215 page = grab_cache_page(inode->i_mapping, i);
Chris Masona061fc82008-05-07 11:43:44 -04003216 if (!page) {
Chris Masonedbd8d42007-12-21 16:27:24 -05003217 goto out_unlock;
Chris Masona061fc82008-05-07 11:43:44 -04003218 }
Chris Masonedbd8d42007-12-21 16:27:24 -05003219 if (!PageUptodate(page)) {
3220 btrfs_readpage(NULL, page);
3221 lock_page(page);
3222 if (!PageUptodate(page)) {
3223 unlock_page(page);
3224 page_cache_release(page);
3225 goto out_unlock;
3226 }
3227 }
Chris Masonec44a352008-04-28 15:29:52 -04003228 wait_on_page_writeback(page);
Chris Mason3eaa2882008-07-24 11:57:52 -04003229
Chris Masonedbd8d42007-12-21 16:27:24 -05003230 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
3231 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05003232 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05003233
Chris Mason3eaa2882008-07-24 11:57:52 -04003234 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3235 if (ordered) {
3236 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
3237 unlock_page(page);
3238 page_cache_release(page);
3239 btrfs_start_ordered_extent(inode, ordered, 1);
3240 btrfs_put_ordered_extent(ordered);
3241 goto again;
3242 }
3243 set_page_extent_mapped(page);
3244
Chris Masonf87f0572008-08-01 11:27:23 -04003245 /*
3246 * make sure page_mkwrite is called for this page if userland
3247 * wants to change it from mmap
3248 */
3249 clear_page_dirty_for_io(page);
Chris Mason3eaa2882008-07-24 11:57:52 -04003250
Chris Masonea8c2812008-08-04 23:17:27 -04003251 btrfs_set_extent_delalloc(inode, page_start, page_end);
Chris Masona061fc82008-05-07 11:43:44 -04003252 set_page_dirty(page);
Chris Masonedbd8d42007-12-21 16:27:24 -05003253
Chris Masond1310b22008-01-24 16:13:08 -05003254 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05003255 unlock_page(page);
3256 page_cache_release(page);
3257 }
3258
3259out_unlock:
Chris Mason3eaa2882008-07-24 11:57:52 -04003260 /* we have to start the IO in order to get the ordered extents
3261 * instantiated. This allows the relocation to code to wait
3262 * for all the ordered extents to hit the disk.
3263 *
3264 * Otherwise, it would constantly loop over the same extents
3265 * because the old ones don't get deleted until the IO is
3266 * started
3267 */
3268 btrfs_fdatawrite_range(inode->i_mapping, start, start + len - 1,
3269 WB_SYNC_NONE);
Chris Masonec44a352008-04-28 15:29:52 -04003270 kfree(ra);
Chris Masona061fc82008-05-07 11:43:44 -04003271 trans = btrfs_start_transaction(BTRFS_I(inode)->root, 1);
3272 if (trans) {
Chris Masona061fc82008-05-07 11:43:44 -04003273 btrfs_end_transaction(trans, BTRFS_I(inode)->root);
3274 mark_inode_dirty(inode);
3275 }
Chris Masonedbd8d42007-12-21 16:27:24 -05003276 mutex_unlock(&inode->i_mutex);
3277 return 0;
Chris Masona061fc82008-05-07 11:43:44 -04003278
3279truncate_racing:
3280 vmtruncate(inode, inode->i_size);
3281 balance_dirty_pages_ratelimited_nr(inode->i_mapping,
3282 total_read);
3283 goto out_unlock;
Chris Masonedbd8d42007-12-21 16:27:24 -05003284}
3285
Chris Mason4313b392008-01-03 09:08:48 -05003286/*
Chris Masonbf4ef672008-05-08 13:26:18 -04003287 * The back references tell us which tree holds a ref on a block,
3288 * but it is possible for the tree root field in the reference to
3289 * reflect the original root before a snapshot was made. In this
3290 * case we should search through all the children of a given root
3291 * to find potential holders of references on a block.
3292 *
3293 * Instead, we do something a little less fancy and just search
3294 * all the roots for a given key/block combination.
3295 */
3296static int find_root_for_ref(struct btrfs_root *root,
3297 struct btrfs_path *path,
3298 struct btrfs_key *key0,
3299 int level,
3300 int file_key,
3301 struct btrfs_root **found_root,
3302 u64 bytenr)
3303{
3304 struct btrfs_key root_location;
3305 struct btrfs_root *cur_root = *found_root;
3306 struct btrfs_file_extent_item *file_extent;
3307 u64 root_search_start = BTRFS_FS_TREE_OBJECTID;
3308 u64 found_bytenr;
3309 int ret;
Chris Masonbf4ef672008-05-08 13:26:18 -04003310
3311 root_location.offset = (u64)-1;
3312 root_location.type = BTRFS_ROOT_ITEM_KEY;
3313 path->lowest_level = level;
3314 path->reada = 0;
3315 while(1) {
3316 ret = btrfs_search_slot(NULL, cur_root, key0, path, 0, 0);
3317 found_bytenr = 0;
3318 if (ret == 0 && file_key) {
3319 struct extent_buffer *leaf = path->nodes[0];
3320 file_extent = btrfs_item_ptr(leaf, path->slots[0],
3321 struct btrfs_file_extent_item);
3322 if (btrfs_file_extent_type(leaf, file_extent) ==
3323 BTRFS_FILE_EXTENT_REG) {
3324 found_bytenr =
3325 btrfs_file_extent_disk_bytenr(leaf,
3326 file_extent);
3327 }
Chris Mason323da792008-05-09 11:46:48 -04003328 } else if (!file_key) {
Chris Masonbf4ef672008-05-08 13:26:18 -04003329 if (path->nodes[level])
3330 found_bytenr = path->nodes[level]->start;
3331 }
3332
Chris Masonbf4ef672008-05-08 13:26:18 -04003333 btrfs_release_path(cur_root, path);
3334
3335 if (found_bytenr == bytenr) {
3336 *found_root = cur_root;
3337 ret = 0;
3338 goto out;
3339 }
3340 ret = btrfs_search_root(root->fs_info->tree_root,
3341 root_search_start, &root_search_start);
3342 if (ret)
3343 break;
3344
3345 root_location.objectid = root_search_start;
3346 cur_root = btrfs_read_fs_root_no_name(root->fs_info,
3347 &root_location);
3348 if (!cur_root) {
3349 ret = 1;
3350 break;
3351 }
3352 }
3353out:
3354 path->lowest_level = 0;
3355 return ret;
3356}
3357
3358/*
Chris Mason4313b392008-01-03 09:08:48 -05003359 * note, this releases the path
3360 */
Chris Mason98ed5172008-01-03 10:01:48 -05003361static int noinline relocate_one_reference(struct btrfs_root *extent_root,
Chris Masonedbd8d42007-12-21 16:27:24 -05003362 struct btrfs_path *path,
Chris Mason0ef3e662008-05-24 14:04:53 -04003363 struct btrfs_key *extent_key,
3364 u64 *last_file_objectid,
3365 u64 *last_file_offset,
3366 u64 *last_file_root,
3367 u64 last_extent)
Chris Masonedbd8d42007-12-21 16:27:24 -05003368{
3369 struct inode *inode;
3370 struct btrfs_root *found_root;
Chris Masonbf4ef672008-05-08 13:26:18 -04003371 struct btrfs_key root_location;
3372 struct btrfs_key found_key;
Chris Mason4313b392008-01-03 09:08:48 -05003373 struct btrfs_extent_ref *ref;
3374 u64 ref_root;
3375 u64 ref_gen;
3376 u64 ref_objectid;
3377 u64 ref_offset;
Chris Masonedbd8d42007-12-21 16:27:24 -05003378 int ret;
Chris Masonbf4ef672008-05-08 13:26:18 -04003379 int level;
Chris Masonedbd8d42007-12-21 16:27:24 -05003380
Chris Mason7d9eb122008-07-08 14:19:17 -04003381 WARN_ON(!mutex_is_locked(&extent_root->fs_info->alloc_mutex));
3382
Chris Mason4313b392008-01-03 09:08:48 -05003383 ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
3384 struct btrfs_extent_ref);
3385 ref_root = btrfs_ref_root(path->nodes[0], ref);
3386 ref_gen = btrfs_ref_generation(path->nodes[0], ref);
3387 ref_objectid = btrfs_ref_objectid(path->nodes[0], ref);
3388 ref_offset = btrfs_ref_offset(path->nodes[0], ref);
3389 btrfs_release_path(extent_root, path);
3390
Chris Masonbf4ef672008-05-08 13:26:18 -04003391 root_location.objectid = ref_root;
Chris Masonedbd8d42007-12-21 16:27:24 -05003392 if (ref_gen == 0)
Chris Masonbf4ef672008-05-08 13:26:18 -04003393 root_location.offset = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05003394 else
Chris Masonbf4ef672008-05-08 13:26:18 -04003395 root_location.offset = (u64)-1;
3396 root_location.type = BTRFS_ROOT_ITEM_KEY;
Chris Masonedbd8d42007-12-21 16:27:24 -05003397
3398 found_root = btrfs_read_fs_root_no_name(extent_root->fs_info,
Chris Masonbf4ef672008-05-08 13:26:18 -04003399 &root_location);
Chris Masonedbd8d42007-12-21 16:27:24 -05003400 BUG_ON(!found_root);
Chris Mason7d9eb122008-07-08 14:19:17 -04003401 mutex_unlock(&extent_root->fs_info->alloc_mutex);
Chris Masonedbd8d42007-12-21 16:27:24 -05003402
3403 if (ref_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
Chris Masonbf4ef672008-05-08 13:26:18 -04003404 found_key.objectid = ref_objectid;
3405 found_key.type = BTRFS_EXTENT_DATA_KEY;
3406 found_key.offset = ref_offset;
3407 level = 0;
3408
Chris Mason0ef3e662008-05-24 14:04:53 -04003409 if (last_extent == extent_key->objectid &&
3410 *last_file_objectid == ref_objectid &&
3411 *last_file_offset == ref_offset &&
3412 *last_file_root == ref_root)
3413 goto out;
3414
Chris Masonbf4ef672008-05-08 13:26:18 -04003415 ret = find_root_for_ref(extent_root, path, &found_key,
3416 level, 1, &found_root,
3417 extent_key->objectid);
3418
3419 if (ret)
3420 goto out;
3421
Chris Mason0ef3e662008-05-24 14:04:53 -04003422 if (last_extent == extent_key->objectid &&
3423 *last_file_objectid == ref_objectid &&
3424 *last_file_offset == ref_offset &&
3425 *last_file_root == ref_root)
3426 goto out;
3427
Chris Masonedbd8d42007-12-21 16:27:24 -05003428 inode = btrfs_iget_locked(extent_root->fs_info->sb,
3429 ref_objectid, found_root);
3430 if (inode->i_state & I_NEW) {
3431 /* the inode and parent dir are two different roots */
3432 BTRFS_I(inode)->root = found_root;
3433 BTRFS_I(inode)->location.objectid = ref_objectid;
3434 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
3435 BTRFS_I(inode)->location.offset = 0;
3436 btrfs_read_locked_inode(inode);
3437 unlock_new_inode(inode);
3438
3439 }
3440 /* this can happen if the reference is not against
3441 * the latest version of the tree root
3442 */
Chris Mason7d9eb122008-07-08 14:19:17 -04003443 if (is_bad_inode(inode))
Chris Masonedbd8d42007-12-21 16:27:24 -05003444 goto out;
Chris Mason7d9eb122008-07-08 14:19:17 -04003445
Chris Mason0ef3e662008-05-24 14:04:53 -04003446 *last_file_objectid = inode->i_ino;
3447 *last_file_root = found_root->root_key.objectid;
3448 *last_file_offset = ref_offset;
3449
Chris Masonedbd8d42007-12-21 16:27:24 -05003450 relocate_inode_pages(inode, ref_offset, extent_key->offset);
Chris Masonedbd8d42007-12-21 16:27:24 -05003451 iput(inode);
Chris Masonedbd8d42007-12-21 16:27:24 -05003452 } else {
3453 struct btrfs_trans_handle *trans;
Chris Masonedbd8d42007-12-21 16:27:24 -05003454 struct extent_buffer *eb;
Chris Mason7d9eb122008-07-08 14:19:17 -04003455 int needs_lock = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05003456
Chris Masonedbd8d42007-12-21 16:27:24 -05003457 eb = read_tree_block(found_root, extent_key->objectid,
Chris Masonca7a79a2008-05-12 12:59:19 -04003458 extent_key->offset, 0);
Chris Mason925baed2008-06-25 16:01:30 -04003459 btrfs_tree_lock(eb);
Chris Masonedbd8d42007-12-21 16:27:24 -05003460 level = btrfs_header_level(eb);
3461
3462 if (level == 0)
3463 btrfs_item_key_to_cpu(eb, &found_key, 0);
3464 else
3465 btrfs_node_key_to_cpu(eb, &found_key, 0);
3466
Chris Mason925baed2008-06-25 16:01:30 -04003467 btrfs_tree_unlock(eb);
Chris Masonedbd8d42007-12-21 16:27:24 -05003468 free_extent_buffer(eb);
3469
Chris Masonbf4ef672008-05-08 13:26:18 -04003470 ret = find_root_for_ref(extent_root, path, &found_key,
3471 level, 0, &found_root,
3472 extent_key->objectid);
3473
3474 if (ret)
3475 goto out;
3476
Chris Mason7d9eb122008-07-08 14:19:17 -04003477 /*
3478 * right here almost anything could happen to our key,
3479 * but that's ok. The cow below will either relocate it
3480 * or someone else will have relocated it. Either way,
3481 * it is in a different spot than it was before and
3482 * we're happy.
3483 */
3484
Chris Masonbf4ef672008-05-08 13:26:18 -04003485 trans = btrfs_start_transaction(found_root, 1);
3486
Chris Mason7d9eb122008-07-08 14:19:17 -04003487 if (found_root == extent_root->fs_info->extent_root ||
3488 found_root == extent_root->fs_info->chunk_root ||
3489 found_root == extent_root->fs_info->dev_root) {
3490 needs_lock = 1;
3491 mutex_lock(&extent_root->fs_info->alloc_mutex);
3492 }
3493
Chris Masonedbd8d42007-12-21 16:27:24 -05003494 path->lowest_level = level;
Chris Mason8f662a72008-01-02 10:01:11 -05003495 path->reada = 2;
Chris Masonedbd8d42007-12-21 16:27:24 -05003496 ret = btrfs_search_slot(trans, found_root, &found_key, path,
3497 0, 1);
3498 path->lowest_level = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05003499 btrfs_release_path(found_root, path);
Chris Mason7d9eb122008-07-08 14:19:17 -04003500
Chris Mason0ef3e662008-05-24 14:04:53 -04003501 if (found_root == found_root->fs_info->extent_root)
3502 btrfs_extent_post_op(trans, found_root);
Chris Mason7d9eb122008-07-08 14:19:17 -04003503 if (needs_lock)
3504 mutex_unlock(&extent_root->fs_info->alloc_mutex);
Chris Masonedbd8d42007-12-21 16:27:24 -05003505
Chris Mason7d9eb122008-07-08 14:19:17 -04003506 btrfs_end_transaction(trans, found_root);
3507
3508 }
Chris Masonedbd8d42007-12-21 16:27:24 -05003509out:
Chris Mason7d9eb122008-07-08 14:19:17 -04003510 mutex_lock(&extent_root->fs_info->alloc_mutex);
Chris Masonedbd8d42007-12-21 16:27:24 -05003511 return 0;
3512}
3513
Chris Masona061fc82008-05-07 11:43:44 -04003514static int noinline del_extent_zero(struct btrfs_root *extent_root,
3515 struct btrfs_path *path,
3516 struct btrfs_key *extent_key)
3517{
3518 int ret;
3519 struct btrfs_trans_handle *trans;
3520
3521 trans = btrfs_start_transaction(extent_root, 1);
3522 ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1);
3523 if (ret > 0) {
3524 ret = -EIO;
3525 goto out;
3526 }
3527 if (ret < 0)
3528 goto out;
3529 ret = btrfs_del_item(trans, extent_root, path);
3530out:
3531 btrfs_end_transaction(trans, extent_root);
3532 return ret;
3533}
3534
Chris Mason98ed5172008-01-03 10:01:48 -05003535static int noinline relocate_one_extent(struct btrfs_root *extent_root,
3536 struct btrfs_path *path,
3537 struct btrfs_key *extent_key)
Chris Masonedbd8d42007-12-21 16:27:24 -05003538{
3539 struct btrfs_key key;
3540 struct btrfs_key found_key;
Chris Masonedbd8d42007-12-21 16:27:24 -05003541 struct extent_buffer *leaf;
Chris Mason0ef3e662008-05-24 14:04:53 -04003542 u64 last_file_objectid = 0;
3543 u64 last_file_root = 0;
3544 u64 last_file_offset = (u64)-1;
3545 u64 last_extent = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05003546 u32 nritems;
3547 u32 item_size;
3548 int ret = 0;
3549
Chris Masona061fc82008-05-07 11:43:44 -04003550 if (extent_key->objectid == 0) {
3551 ret = del_extent_zero(extent_root, path, extent_key);
3552 goto out;
3553 }
Chris Masonedbd8d42007-12-21 16:27:24 -05003554 key.objectid = extent_key->objectid;
3555 key.type = BTRFS_EXTENT_REF_KEY;
3556 key.offset = 0;
3557
3558 while(1) {
3559 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
3560
Chris Masonedbd8d42007-12-21 16:27:24 -05003561 if (ret < 0)
3562 goto out;
3563
3564 ret = 0;
3565 leaf = path->nodes[0];
3566 nritems = btrfs_header_nritems(leaf);
Chris Masona061fc82008-05-07 11:43:44 -04003567 if (path->slots[0] == nritems) {
3568 ret = btrfs_next_leaf(extent_root, path);
3569 if (ret > 0) {
3570 ret = 0;
3571 goto out;
3572 }
3573 if (ret < 0)
3574 goto out;
Chris Masonbf4ef672008-05-08 13:26:18 -04003575 leaf = path->nodes[0];
Chris Masona061fc82008-05-07 11:43:44 -04003576 }
Chris Masonedbd8d42007-12-21 16:27:24 -05003577
3578 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masona061fc82008-05-07 11:43:44 -04003579 if (found_key.objectid != extent_key->objectid) {
Chris Masonedbd8d42007-12-21 16:27:24 -05003580 break;
Chris Masona061fc82008-05-07 11:43:44 -04003581 }
Chris Masonedbd8d42007-12-21 16:27:24 -05003582
Chris Masona061fc82008-05-07 11:43:44 -04003583 if (found_key.type != BTRFS_EXTENT_REF_KEY) {
Chris Masonedbd8d42007-12-21 16:27:24 -05003584 break;
Chris Masona061fc82008-05-07 11:43:44 -04003585 }
Chris Masonedbd8d42007-12-21 16:27:24 -05003586
3587 key.offset = found_key.offset + 1;
3588 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
3589
Chris Mason0ef3e662008-05-24 14:04:53 -04003590 ret = relocate_one_reference(extent_root, path, extent_key,
3591 &last_file_objectid,
3592 &last_file_offset,
3593 &last_file_root, last_extent);
Chris Masonedbd8d42007-12-21 16:27:24 -05003594 if (ret)
3595 goto out;
Chris Mason0ef3e662008-05-24 14:04:53 -04003596 last_extent = extent_key->objectid;
Chris Masonedbd8d42007-12-21 16:27:24 -05003597 }
3598 ret = 0;
3599out:
3600 btrfs_release_path(extent_root, path);
3601 return ret;
3602}
3603
Chris Masonec44a352008-04-28 15:29:52 -04003604static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
3605{
3606 u64 num_devices;
3607 u64 stripped = BTRFS_BLOCK_GROUP_RAID0 |
3608 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
3609
Chris Masona061fc82008-05-07 11:43:44 -04003610 num_devices = root->fs_info->fs_devices->num_devices;
Chris Masonec44a352008-04-28 15:29:52 -04003611 if (num_devices == 1) {
3612 stripped |= BTRFS_BLOCK_GROUP_DUP;
3613 stripped = flags & ~stripped;
3614
3615 /* turn raid0 into single device chunks */
3616 if (flags & BTRFS_BLOCK_GROUP_RAID0)
3617 return stripped;
3618
3619 /* turn mirroring into duplication */
3620 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
3621 BTRFS_BLOCK_GROUP_RAID10))
3622 return stripped | BTRFS_BLOCK_GROUP_DUP;
3623 return flags;
3624 } else {
3625 /* they already had raid on here, just return */
Chris Masonec44a352008-04-28 15:29:52 -04003626 if (flags & stripped)
3627 return flags;
3628
3629 stripped |= BTRFS_BLOCK_GROUP_DUP;
3630 stripped = flags & ~stripped;
3631
3632 /* switch duplicated blocks with raid1 */
3633 if (flags & BTRFS_BLOCK_GROUP_DUP)
3634 return stripped | BTRFS_BLOCK_GROUP_RAID1;
3635
3636 /* turn single device chunks into raid0 */
3637 return stripped | BTRFS_BLOCK_GROUP_RAID0;
3638 }
3639 return flags;
3640}
3641
Chris Mason0ef3e662008-05-24 14:04:53 -04003642int __alloc_chunk_for_shrink(struct btrfs_root *root,
3643 struct btrfs_block_group_cache *shrink_block_group,
3644 int force)
3645{
3646 struct btrfs_trans_handle *trans;
3647 u64 new_alloc_flags;
3648 u64 calc;
3649
Chris Masonc286ac42008-07-22 23:06:41 -04003650 spin_lock(&shrink_block_group->lock);
Chris Mason0ef3e662008-05-24 14:04:53 -04003651 if (btrfs_block_group_used(&shrink_block_group->item) > 0) {
Chris Masonc286ac42008-07-22 23:06:41 -04003652 spin_unlock(&shrink_block_group->lock);
Chris Mason7d9eb122008-07-08 14:19:17 -04003653 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Masonc286ac42008-07-22 23:06:41 -04003654
Chris Mason0ef3e662008-05-24 14:04:53 -04003655 trans = btrfs_start_transaction(root, 1);
Chris Mason7d9eb122008-07-08 14:19:17 -04003656 mutex_lock(&root->fs_info->alloc_mutex);
Chris Masonc286ac42008-07-22 23:06:41 -04003657 spin_lock(&shrink_block_group->lock);
Chris Mason7d9eb122008-07-08 14:19:17 -04003658
Chris Mason0ef3e662008-05-24 14:04:53 -04003659 new_alloc_flags = update_block_group_flags(root,
3660 shrink_block_group->flags);
3661 if (new_alloc_flags != shrink_block_group->flags) {
3662 calc =
3663 btrfs_block_group_used(&shrink_block_group->item);
3664 } else {
3665 calc = shrink_block_group->key.offset;
3666 }
Chris Masonc286ac42008-07-22 23:06:41 -04003667 spin_unlock(&shrink_block_group->lock);
3668
Chris Mason0ef3e662008-05-24 14:04:53 -04003669 do_chunk_alloc(trans, root->fs_info->extent_root,
3670 calc + 2 * 1024 * 1024, new_alloc_flags, force);
Chris Mason7d9eb122008-07-08 14:19:17 -04003671
3672 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Mason0ef3e662008-05-24 14:04:53 -04003673 btrfs_end_transaction(trans, root);
Chris Mason7d9eb122008-07-08 14:19:17 -04003674 mutex_lock(&root->fs_info->alloc_mutex);
Chris Masonc286ac42008-07-22 23:06:41 -04003675 } else
3676 spin_unlock(&shrink_block_group->lock);
Chris Mason0ef3e662008-05-24 14:04:53 -04003677 return 0;
3678}
3679
Chris Mason8f18cf12008-04-25 16:53:30 -04003680int btrfs_shrink_extent_tree(struct btrfs_root *root, u64 shrink_start)
Chris Masonedbd8d42007-12-21 16:27:24 -05003681{
3682 struct btrfs_trans_handle *trans;
3683 struct btrfs_root *tree_root = root->fs_info->tree_root;
3684 struct btrfs_path *path;
3685 u64 cur_byte;
3686 u64 total_found;
Chris Mason8f18cf12008-04-25 16:53:30 -04003687 u64 shrink_last_byte;
3688 struct btrfs_block_group_cache *shrink_block_group;
Chris Masonedbd8d42007-12-21 16:27:24 -05003689 struct btrfs_key key;
Yan73e48b22008-01-03 14:14:39 -05003690 struct btrfs_key found_key;
Chris Masonedbd8d42007-12-21 16:27:24 -05003691 struct extent_buffer *leaf;
3692 u32 nritems;
3693 int ret;
Chris Masona061fc82008-05-07 11:43:44 -04003694 int progress;
Chris Masonedbd8d42007-12-21 16:27:24 -05003695
Chris Mason925baed2008-06-25 16:01:30 -04003696 mutex_lock(&root->fs_info->alloc_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04003697 shrink_block_group = btrfs_lookup_block_group(root->fs_info,
3698 shrink_start);
3699 BUG_ON(!shrink_block_group);
3700
Chris Mason0ef3e662008-05-24 14:04:53 -04003701 shrink_last_byte = shrink_block_group->key.objectid +
3702 shrink_block_group->key.offset;
Chris Mason8f18cf12008-04-25 16:53:30 -04003703
3704 shrink_block_group->space_info->total_bytes -=
3705 shrink_block_group->key.offset;
Chris Masonedbd8d42007-12-21 16:27:24 -05003706 path = btrfs_alloc_path();
3707 root = root->fs_info->extent_root;
Chris Mason8f662a72008-01-02 10:01:11 -05003708 path->reada = 2;
Chris Masonedbd8d42007-12-21 16:27:24 -05003709
Chris Mason323da792008-05-09 11:46:48 -04003710 printk("btrfs relocating block group %llu flags %llu\n",
3711 (unsigned long long)shrink_start,
3712 (unsigned long long)shrink_block_group->flags);
3713
Chris Mason0ef3e662008-05-24 14:04:53 -04003714 __alloc_chunk_for_shrink(root, shrink_block_group, 1);
Chris Mason323da792008-05-09 11:46:48 -04003715
Chris Mason0ef3e662008-05-24 14:04:53 -04003716again:
3717
Chris Mason8f18cf12008-04-25 16:53:30 -04003718 shrink_block_group->ro = 1;
3719
Chris Masonedbd8d42007-12-21 16:27:24 -05003720 total_found = 0;
Chris Masona061fc82008-05-07 11:43:44 -04003721 progress = 0;
Chris Mason8f18cf12008-04-25 16:53:30 -04003722 key.objectid = shrink_start;
Chris Masonedbd8d42007-12-21 16:27:24 -05003723 key.offset = 0;
3724 key.type = 0;
Yan73e48b22008-01-03 14:14:39 -05003725 cur_byte = key.objectid;
Chris Mason4313b392008-01-03 09:08:48 -05003726
Chris Masonea8c2812008-08-04 23:17:27 -04003727 mutex_unlock(&root->fs_info->alloc_mutex);
3728
3729 btrfs_start_delalloc_inodes(root);
Yan Zheng7ea394f2008-08-05 13:05:02 -04003730 btrfs_wait_ordered_extents(tree_root, 0);
Chris Masonea8c2812008-08-04 23:17:27 -04003731
3732 mutex_lock(&root->fs_info->alloc_mutex);
3733
Yan73e48b22008-01-03 14:14:39 -05003734 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3735 if (ret < 0)
3736 goto out;
3737
Chris Mason0b86a832008-03-24 15:01:56 -04003738 ret = btrfs_previous_item(root, path, 0, BTRFS_EXTENT_ITEM_KEY);
Yan73e48b22008-01-03 14:14:39 -05003739 if (ret < 0)
3740 goto out;
Chris Mason8f18cf12008-04-25 16:53:30 -04003741
Yan73e48b22008-01-03 14:14:39 -05003742 if (ret == 0) {
3743 leaf = path->nodes[0];
3744 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason8f18cf12008-04-25 16:53:30 -04003745 if (found_key.objectid + found_key.offset > shrink_start &&
3746 found_key.objectid < shrink_last_byte) {
Yan73e48b22008-01-03 14:14:39 -05003747 cur_byte = found_key.objectid;
3748 key.objectid = cur_byte;
3749 }
3750 }
3751 btrfs_release_path(root, path);
3752
3753 while(1) {
Chris Masonedbd8d42007-12-21 16:27:24 -05003754 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3755 if (ret < 0)
3756 goto out;
Yan73e48b22008-01-03 14:14:39 -05003757
Chris Mason7d9eb122008-07-08 14:19:17 -04003758next:
Chris Masonedbd8d42007-12-21 16:27:24 -05003759 leaf = path->nodes[0];
Chris Masonedbd8d42007-12-21 16:27:24 -05003760 nritems = btrfs_header_nritems(leaf);
Yan73e48b22008-01-03 14:14:39 -05003761 if (path->slots[0] >= nritems) {
3762 ret = btrfs_next_leaf(root, path);
3763 if (ret < 0)
3764 goto out;
3765 if (ret == 1) {
3766 ret = 0;
3767 break;
Chris Masonedbd8d42007-12-21 16:27:24 -05003768 }
Yan73e48b22008-01-03 14:14:39 -05003769 leaf = path->nodes[0];
3770 nritems = btrfs_header_nritems(leaf);
3771 }
3772
3773 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason725c8462008-01-04 16:47:16 -05003774
Chris Mason8f18cf12008-04-25 16:53:30 -04003775 if (found_key.objectid >= shrink_last_byte)
3776 break;
3777
Chris Mason725c8462008-01-04 16:47:16 -05003778 if (progress && need_resched()) {
3779 memcpy(&key, &found_key, sizeof(key));
Chris Mason725c8462008-01-04 16:47:16 -05003780 cond_resched();
Chris Mason725c8462008-01-04 16:47:16 -05003781 btrfs_release_path(root, path);
3782 btrfs_search_slot(NULL, root, &key, path, 0, 0);
3783 progress = 0;
3784 goto next;
3785 }
3786 progress = 1;
3787
Yan73e48b22008-01-03 14:14:39 -05003788 if (btrfs_key_type(&found_key) != BTRFS_EXTENT_ITEM_KEY ||
3789 found_key.objectid + found_key.offset <= cur_byte) {
Chris Mason0ef3e662008-05-24 14:04:53 -04003790 memcpy(&key, &found_key, sizeof(key));
3791 key.offset++;
Yan73e48b22008-01-03 14:14:39 -05003792 path->slots[0]++;
Chris Masonedbd8d42007-12-21 16:27:24 -05003793 goto next;
3794 }
Yan73e48b22008-01-03 14:14:39 -05003795
Chris Masonedbd8d42007-12-21 16:27:24 -05003796 total_found++;
3797 cur_byte = found_key.objectid + found_key.offset;
3798 key.objectid = cur_byte;
3799 btrfs_release_path(root, path);
3800 ret = relocate_one_extent(root, path, &found_key);
Chris Mason0ef3e662008-05-24 14:04:53 -04003801 __alloc_chunk_for_shrink(root, shrink_block_group, 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05003802 }
3803
3804 btrfs_release_path(root, path);
3805
3806 if (total_found > 0) {
Chris Mason323da792008-05-09 11:46:48 -04003807 printk("btrfs relocate found %llu last extent was %llu\n",
3808 (unsigned long long)total_found,
3809 (unsigned long long)found_key.objectid);
Chris Mason7d9eb122008-07-08 14:19:17 -04003810 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Masonedbd8d42007-12-21 16:27:24 -05003811 trans = btrfs_start_transaction(tree_root, 1);
3812 btrfs_commit_transaction(trans, tree_root);
3813
Chris Masonedbd8d42007-12-21 16:27:24 -05003814 btrfs_clean_old_snapshots(tree_root);
Chris Masonedbd8d42007-12-21 16:27:24 -05003815
Chris Masonea8c2812008-08-04 23:17:27 -04003816 btrfs_start_delalloc_inodes(root);
Yan Zheng7ea394f2008-08-05 13:05:02 -04003817 btrfs_wait_ordered_extents(tree_root, 0);
Chris Mason3eaa2882008-07-24 11:57:52 -04003818
Chris Masonedbd8d42007-12-21 16:27:24 -05003819 trans = btrfs_start_transaction(tree_root, 1);
3820 btrfs_commit_transaction(trans, tree_root);
Chris Mason7d9eb122008-07-08 14:19:17 -04003821 mutex_lock(&root->fs_info->alloc_mutex);
Chris Masonedbd8d42007-12-21 16:27:24 -05003822 goto again;
3823 }
3824
Chris Mason8f18cf12008-04-25 16:53:30 -04003825 /*
3826 * we've freed all the extents, now remove the block
3827 * group item from the tree
3828 */
Chris Mason7d9eb122008-07-08 14:19:17 -04003829 mutex_unlock(&root->fs_info->alloc_mutex);
3830
Chris Masonedbd8d42007-12-21 16:27:24 -05003831 trans = btrfs_start_transaction(root, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04003832
Chris Mason7d9eb122008-07-08 14:19:17 -04003833 mutex_lock(&root->fs_info->alloc_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04003834 memcpy(&key, &shrink_block_group->key, sizeof(key));
Chris Mason4313b392008-01-03 09:08:48 -05003835
Chris Mason8f18cf12008-04-25 16:53:30 -04003836 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
3837 if (ret > 0)
3838 ret = -EIO;
Josef Bacik8e8a1e32008-07-24 12:17:14 -04003839 if (ret < 0) {
3840 btrfs_end_transaction(trans, root);
Chris Mason8f18cf12008-04-25 16:53:30 -04003841 goto out;
Josef Bacik8e8a1e32008-07-24 12:17:14 -04003842 }
Yan73e48b22008-01-03 14:14:39 -05003843
Josef Bacik0f9dd462008-09-23 13:14:11 -04003844 spin_lock(&root->fs_info->block_group_cache_lock);
3845 rb_erase(&shrink_block_group->cache_node,
3846 &root->fs_info->block_group_cache_tree);
3847 spin_unlock(&root->fs_info->block_group_cache_lock);
Yan73e48b22008-01-03 14:14:39 -05003848
Josef Bacik0f9dd462008-09-23 13:14:11 -04003849 ret = btrfs_remove_free_space(shrink_block_group, key.objectid,
3850 key.offset);
3851 if (ret) {
3852 btrfs_end_transaction(trans, root);
3853 goto out;
3854 }
Chris Masond7a029a2008-08-04 23:17:26 -04003855 /*
Chris Mason0ef3e662008-05-24 14:04:53 -04003856 memset(shrink_block_group, 0, sizeof(*shrink_block_group));
3857 kfree(shrink_block_group);
Chris Masond7a029a2008-08-04 23:17:26 -04003858 */
Chris Mason0ef3e662008-05-24 14:04:53 -04003859
Chris Mason8f18cf12008-04-25 16:53:30 -04003860 btrfs_del_item(trans, root, path);
Chris Mason7d9eb122008-07-08 14:19:17 -04003861 btrfs_release_path(root, path);
3862 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Masonedbd8d42007-12-21 16:27:24 -05003863 btrfs_commit_transaction(trans, root);
Chris Mason0ef3e662008-05-24 14:04:53 -04003864
Chris Mason7d9eb122008-07-08 14:19:17 -04003865 mutex_lock(&root->fs_info->alloc_mutex);
3866
Chris Mason0ef3e662008-05-24 14:04:53 -04003867 /* the code to unpin extents might set a few bits in the free
3868 * space cache for this range again
3869 */
Josef Bacik0f9dd462008-09-23 13:14:11 -04003870 /* XXX? */
3871 ret = btrfs_remove_free_space(shrink_block_group, key.objectid,
3872 key.offset);
Chris Masonedbd8d42007-12-21 16:27:24 -05003873out:
3874 btrfs_free_path(path);
Chris Mason925baed2008-06-25 16:01:30 -04003875 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Masonedbd8d42007-12-21 16:27:24 -05003876 return ret;
3877}
3878
Chris Mason0b86a832008-03-24 15:01:56 -04003879int find_first_block_group(struct btrfs_root *root, struct btrfs_path *path,
3880 struct btrfs_key *key)
3881{
Chris Mason925baed2008-06-25 16:01:30 -04003882 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04003883 struct btrfs_key found_key;
3884 struct extent_buffer *leaf;
3885 int slot;
3886
3887 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
3888 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04003889 goto out;
3890
Chris Mason0b86a832008-03-24 15:01:56 -04003891 while(1) {
3892 slot = path->slots[0];
3893 leaf = path->nodes[0];
3894 if (slot >= btrfs_header_nritems(leaf)) {
3895 ret = btrfs_next_leaf(root, path);
3896 if (ret == 0)
3897 continue;
3898 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04003899 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04003900 break;
3901 }
3902 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3903
3904 if (found_key.objectid >= key->objectid &&
Chris Mason925baed2008-06-25 16:01:30 -04003905 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
3906 ret = 0;
3907 goto out;
3908 }
Chris Mason0b86a832008-03-24 15:01:56 -04003909 path->slots[0]++;
3910 }
3911 ret = -ENOENT;
Chris Mason925baed2008-06-25 16:01:30 -04003912out:
Chris Mason0b86a832008-03-24 15:01:56 -04003913 return ret;
3914}
3915
Chris Mason9078a3e2007-04-26 16:46:15 -04003916int btrfs_read_block_groups(struct btrfs_root *root)
3917{
3918 struct btrfs_path *path;
3919 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04003920 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04003921 struct btrfs_fs_info *info = root->fs_info;
Chris Mason6324fbf2008-03-24 15:01:59 -04003922 struct btrfs_space_info *space_info;
Chris Mason9078a3e2007-04-26 16:46:15 -04003923 struct btrfs_key key;
3924 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -04003925 struct extent_buffer *leaf;
Chris Mason96b51792007-10-15 16:15:19 -04003926
Chris Masonbe744172007-05-06 10:15:01 -04003927 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04003928 key.objectid = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04003929 key.offset = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04003930 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
Chris Mason9078a3e2007-04-26 16:46:15 -04003931 path = btrfs_alloc_path();
3932 if (!path)
3933 return -ENOMEM;
3934
Chris Mason925baed2008-06-25 16:01:30 -04003935 mutex_lock(&root->fs_info->alloc_mutex);
Chris Mason9078a3e2007-04-26 16:46:15 -04003936 while(1) {
Chris Mason0b86a832008-03-24 15:01:56 -04003937 ret = find_first_block_group(root, path, &key);
3938 if (ret > 0) {
3939 ret = 0;
3940 goto error;
Chris Mason9078a3e2007-04-26 16:46:15 -04003941 }
Chris Mason0b86a832008-03-24 15:01:56 -04003942 if (ret != 0)
3943 goto error;
3944
Chris Mason5f39d392007-10-15 16:14:19 -04003945 leaf = path->nodes[0];
3946 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason8f18cf12008-04-25 16:53:30 -04003947 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -04003948 if (!cache) {
Chris Mason0b86a832008-03-24 15:01:56 -04003949 ret = -ENOMEM;
Chris Mason9078a3e2007-04-26 16:46:15 -04003950 break;
3951 }
Chris Mason3e1ad542007-05-07 20:03:49 -04003952
Chris Masonc286ac42008-07-22 23:06:41 -04003953 spin_lock_init(&cache->lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003954 INIT_LIST_HEAD(&cache->list);
Chris Mason5f39d392007-10-15 16:14:19 -04003955 read_extent_buffer(leaf, &cache->item,
3956 btrfs_item_ptr_offset(leaf, path->slots[0]),
3957 sizeof(cache->item));
Chris Mason9078a3e2007-04-26 16:46:15 -04003958 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Mason0b86a832008-03-24 15:01:56 -04003959
Chris Mason9078a3e2007-04-26 16:46:15 -04003960 key.objectid = found_key.objectid + found_key.offset;
3961 btrfs_release_path(root, path);
Chris Mason0b86a832008-03-24 15:01:56 -04003962 cache->flags = btrfs_block_group_flags(&cache->item);
Chris Mason96b51792007-10-15 16:15:19 -04003963
Chris Mason6324fbf2008-03-24 15:01:59 -04003964 ret = update_space_info(info, cache->flags, found_key.offset,
3965 btrfs_block_group_used(&cache->item),
3966 &space_info);
3967 BUG_ON(ret);
3968 cache->space_info = space_info;
Josef Bacik0f9dd462008-09-23 13:14:11 -04003969 spin_lock(&space_info->lock);
3970 list_add(&cache->list, &space_info->block_groups);
3971 spin_unlock(&space_info->lock);
Chris Mason6324fbf2008-03-24 15:01:59 -04003972
Josef Bacik0f9dd462008-09-23 13:14:11 -04003973 ret = btrfs_add_block_group_cache(root->fs_info, cache);
3974 BUG_ON(ret);
Chris Mason9078a3e2007-04-26 16:46:15 -04003975 }
Chris Mason0b86a832008-03-24 15:01:56 -04003976 ret = 0;
3977error:
Chris Mason9078a3e2007-04-26 16:46:15 -04003978 btrfs_free_path(path);
Chris Mason925baed2008-06-25 16:01:30 -04003979 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Mason0b86a832008-03-24 15:01:56 -04003980 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04003981}
Chris Mason6324fbf2008-03-24 15:01:59 -04003982
3983int btrfs_make_block_group(struct btrfs_trans_handle *trans,
3984 struct btrfs_root *root, u64 bytes_used,
Chris Masone17cade2008-04-15 15:41:47 -04003985 u64 type, u64 chunk_objectid, u64 chunk_offset,
Chris Mason6324fbf2008-03-24 15:01:59 -04003986 u64 size)
3987{
3988 int ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04003989 struct btrfs_root *extent_root;
3990 struct btrfs_block_group_cache *cache;
Chris Mason6324fbf2008-03-24 15:01:59 -04003991
Chris Mason7d9eb122008-07-08 14:19:17 -04003992 WARN_ON(!mutex_is_locked(&root->fs_info->alloc_mutex));
Chris Mason6324fbf2008-03-24 15:01:59 -04003993 extent_root = root->fs_info->extent_root;
Chris Mason6324fbf2008-03-24 15:01:59 -04003994
Chris Masone02119d2008-09-05 16:13:11 -04003995 root->fs_info->last_trans_new_blockgroup = trans->transid;
3996
Chris Mason8f18cf12008-04-25 16:53:30 -04003997 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003998 if (!cache)
3999 return -ENOMEM;
4000
Chris Masone17cade2008-04-15 15:41:47 -04004001 cache->key.objectid = chunk_offset;
Chris Mason6324fbf2008-03-24 15:01:59 -04004002 cache->key.offset = size;
Chris Masonc286ac42008-07-22 23:06:41 -04004003 spin_lock_init(&cache->lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04004004 INIT_LIST_HEAD(&cache->list);
Chris Mason6324fbf2008-03-24 15:01:59 -04004005 btrfs_set_key_type(&cache->key, BTRFS_BLOCK_GROUP_ITEM_KEY);
Chris Mason0ef3e662008-05-24 14:04:53 -04004006
Chris Mason6324fbf2008-03-24 15:01:59 -04004007 btrfs_set_block_group_used(&cache->item, bytes_used);
Chris Mason6324fbf2008-03-24 15:01:59 -04004008 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
4009 cache->flags = type;
4010 btrfs_set_block_group_flags(&cache->item, type);
4011
4012 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
4013 &cache->space_info);
4014 BUG_ON(ret);
Josef Bacik0f9dd462008-09-23 13:14:11 -04004015 spin_lock(&cache->space_info->lock);
4016 list_add(&cache->list, &cache->space_info->block_groups);
4017 spin_unlock(&cache->space_info->lock);
Chris Mason6324fbf2008-03-24 15:01:59 -04004018
Josef Bacik0f9dd462008-09-23 13:14:11 -04004019 ret = btrfs_add_block_group_cache(root->fs_info, cache);
4020 BUG_ON(ret);
Chris Masonc286ac42008-07-22 23:06:41 -04004021
Chris Mason6324fbf2008-03-24 15:01:59 -04004022 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
4023 sizeof(cache->item));
4024 BUG_ON(ret);
4025
4026 finish_current_insert(trans, extent_root);
4027 ret = del_pending_extents(trans, extent_root);
4028 BUG_ON(ret);
Chris Masond18a2c42008-04-04 15:40:00 -04004029 set_avail_alloc_bits(extent_root->fs_info, type);
Chris Mason925baed2008-06-25 16:01:30 -04004030
Chris Mason6324fbf2008-03-24 15:01:59 -04004031 return 0;
4032}