blob: db37b867e4f168089ba59d1e801a3637dd579e0b [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 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -04001095 struct btrfs_file_extent_item *fi;
Zheng Yane4657682008-09-26 10:04:53 -04001096 u64 root_gen;
1097 u32 nritems;
Chris Mason02217ed2007-03-02 16:08:05 -05001098 int i;
Chris Masondb945352007-10-15 16:15:53 -04001099 int level;
Zheng Yan31840ae2008-09-23 13:14:14 -04001100 int ret = 0;
Zheng Yane4657682008-09-26 10:04:53 -04001101 int shared = 0;
Chris Masona28ec192007-03-06 20:08:01 -05001102
Chris Mason3768f362007-03-13 16:47:54 -04001103 if (!root->ref_cows)
Chris Masona28ec192007-03-06 20:08:01 -05001104 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001105
Zheng Yane4657682008-09-26 10:04:53 -04001106 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
1107 shared = 0;
1108 root_gen = root->root_key.offset;
1109 } else {
1110 shared = 1;
1111 root_gen = trans->transid - 1;
1112 }
1113
Chris Masondb945352007-10-15 16:15:53 -04001114 level = btrfs_header_level(buf);
Chris Mason5f39d392007-10-15 16:14:19 -04001115 nritems = btrfs_header_nritems(buf);
Chris Mason4a096752008-07-21 10:29:44 -04001116
Zheng Yan31840ae2008-09-23 13:14:14 -04001117 if (level == 0) {
Yan Zheng31153d82008-07-28 15:32:19 -04001118 struct btrfs_leaf_ref *ref;
1119 struct btrfs_extent_info *info;
1120
Zheng Yan31840ae2008-09-23 13:14:14 -04001121 ref = btrfs_alloc_leaf_ref(root, nr_extents);
Yan Zheng31153d82008-07-28 15:32:19 -04001122 if (!ref) {
Zheng Yan31840ae2008-09-23 13:14:14 -04001123 ret = -ENOMEM;
Yan Zheng31153d82008-07-28 15:32:19 -04001124 goto out;
1125 }
1126
Zheng Yane4657682008-09-26 10:04:53 -04001127 ref->root_gen = root_gen;
Yan Zheng31153d82008-07-28 15:32:19 -04001128 ref->bytenr = buf->start;
1129 ref->owner = btrfs_header_owner(buf);
1130 ref->generation = btrfs_header_generation(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04001131 ref->nritems = nr_extents;
Yan Zheng31153d82008-07-28 15:32:19 -04001132 info = ref->extents;
Yanbcc63ab2008-07-30 16:29:20 -04001133
Zheng Yan31840ae2008-09-23 13:14:14 -04001134 for (i = 0; nr_extents > 0 && i < nritems; i++) {
Yan Zheng31153d82008-07-28 15:32:19 -04001135 u64 disk_bytenr;
1136 btrfs_item_key_to_cpu(buf, &key, i);
1137 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1138 continue;
1139 fi = btrfs_item_ptr(buf, i,
1140 struct btrfs_file_extent_item);
1141 if (btrfs_file_extent_type(buf, fi) ==
1142 BTRFS_FILE_EXTENT_INLINE)
1143 continue;
1144 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1145 if (disk_bytenr == 0)
1146 continue;
1147
1148 info->bytenr = disk_bytenr;
1149 info->num_bytes =
1150 btrfs_file_extent_disk_num_bytes(buf, fi);
1151 info->objectid = key.objectid;
1152 info->offset = key.offset;
1153 info++;
1154 }
1155
Zheng Yane4657682008-09-26 10:04:53 -04001156 ret = btrfs_add_leaf_ref(root, ref, shared);
Yan Zheng31153d82008-07-28 15:32:19 -04001157 WARN_ON(ret);
Yanbcc63ab2008-07-30 16:29:20 -04001158 btrfs_free_leaf_ref(root, ref);
Yan Zheng31153d82008-07-28 15:32:19 -04001159 }
1160out:
Zheng Yan31840ae2008-09-23 13:14:14 -04001161 return ret;
1162}
1163
1164int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1165 struct extent_buffer *orig_buf, struct extent_buffer *buf,
1166 u32 *nr_extents)
1167{
1168 u64 bytenr;
1169 u64 ref_root;
1170 u64 orig_root;
1171 u64 ref_generation;
1172 u64 orig_generation;
1173 u32 nritems;
1174 u32 nr_file_extents = 0;
1175 struct btrfs_key key;
1176 struct btrfs_file_extent_item *fi;
1177 int i;
1178 int level;
1179 int ret = 0;
1180 int faili = 0;
1181 int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
1182 u64, u64, u64, u64, u64, u64, u64, u64, u64);
1183
1184 ref_root = btrfs_header_owner(buf);
1185 ref_generation = btrfs_header_generation(buf);
1186 orig_root = btrfs_header_owner(orig_buf);
1187 orig_generation = btrfs_header_generation(orig_buf);
1188
1189 nritems = btrfs_header_nritems(buf);
1190 level = btrfs_header_level(buf);
1191
1192 if (root->ref_cows) {
1193 process_func = __btrfs_inc_extent_ref;
1194 } else {
1195 if (level == 0 &&
1196 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
1197 goto out;
1198 if (level != 0 &&
1199 root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID)
1200 goto out;
1201 process_func = __btrfs_update_extent_ref;
1202 }
1203
1204 for (i = 0; i < nritems; i++) {
1205 cond_resched();
Chris Masondb945352007-10-15 16:15:53 -04001206 if (level == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001207 btrfs_item_key_to_cpu(buf, &key, i);
1208 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -04001209 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04001210 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -04001211 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001212 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -04001213 BTRFS_FILE_EXTENT_INLINE)
1214 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04001215 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1216 if (bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -04001217 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04001218
1219 nr_file_extents++;
1220
1221 maybe_lock_mutex(root);
1222 ret = process_func(trans, root, bytenr,
1223 orig_buf->start, buf->start,
1224 orig_root, ref_root,
1225 orig_generation, ref_generation,
1226 key.objectid, key.offset);
1227 maybe_unlock_mutex(root);
1228
1229 if (ret) {
1230 faili = i;
1231 WARN_ON(1);
1232 goto fail;
1233 }
Chris Mason54aa1f42007-06-22 14:16:25 -04001234 } else {
Chris Masondb945352007-10-15 16:15:53 -04001235 bytenr = btrfs_node_blockptr(buf, i);
Zheng Yan31840ae2008-09-23 13:14:14 -04001236 maybe_lock_mutex(root);
1237 ret = process_func(trans, root, bytenr,
1238 orig_buf->start, buf->start,
1239 orig_root, ref_root,
1240 orig_generation, ref_generation,
1241 level - 1, 0);
1242 maybe_unlock_mutex(root);
1243 if (ret) {
1244 faili = i;
1245 WARN_ON(1);
1246 goto fail;
1247 }
Chris Mason54aa1f42007-06-22 14:16:25 -04001248 }
1249 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001250out:
1251 if (nr_extents) {
1252 if (level == 0)
1253 *nr_extents = nr_file_extents;
1254 else
1255 *nr_extents = nritems;
1256 }
1257 return 0;
1258fail:
1259 WARN_ON(1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001260 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05001261}
1262
Zheng Yan31840ae2008-09-23 13:14:14 -04001263int btrfs_update_ref(struct btrfs_trans_handle *trans,
1264 struct btrfs_root *root, struct extent_buffer *orig_buf,
1265 struct extent_buffer *buf, int start_slot, int nr)
1266
1267{
1268 u64 bytenr;
1269 u64 ref_root;
1270 u64 orig_root;
1271 u64 ref_generation;
1272 u64 orig_generation;
1273 struct btrfs_key key;
1274 struct btrfs_file_extent_item *fi;
1275 int i;
1276 int ret;
1277 int slot;
1278 int level;
1279
1280 BUG_ON(start_slot < 0);
1281 BUG_ON(start_slot + nr > btrfs_header_nritems(buf));
1282
1283 ref_root = btrfs_header_owner(buf);
1284 ref_generation = btrfs_header_generation(buf);
1285 orig_root = btrfs_header_owner(orig_buf);
1286 orig_generation = btrfs_header_generation(orig_buf);
1287 level = btrfs_header_level(buf);
1288
1289 if (!root->ref_cows) {
1290 if (level == 0 &&
1291 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
1292 return 0;
1293 if (level != 0 &&
1294 root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID)
1295 return 0;
1296 }
1297
1298 for (i = 0, slot = start_slot; i < nr; i++, slot++) {
1299 cond_resched();
1300 if (level == 0) {
1301 btrfs_item_key_to_cpu(buf, &key, slot);
1302 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1303 continue;
1304 fi = btrfs_item_ptr(buf, slot,
1305 struct btrfs_file_extent_item);
1306 if (btrfs_file_extent_type(buf, fi) ==
1307 BTRFS_FILE_EXTENT_INLINE)
1308 continue;
1309 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1310 if (bytenr == 0)
1311 continue;
1312 maybe_lock_mutex(root);
1313 ret = __btrfs_update_extent_ref(trans, root, bytenr,
1314 orig_buf->start, buf->start,
1315 orig_root, ref_root,
1316 orig_generation, ref_generation,
1317 key.objectid, key.offset);
1318 maybe_unlock_mutex(root);
1319 if (ret)
1320 goto fail;
1321 } else {
1322 bytenr = btrfs_node_blockptr(buf, slot);
1323 maybe_lock_mutex(root);
1324 ret = __btrfs_update_extent_ref(trans, root, bytenr,
1325 orig_buf->start, buf->start,
1326 orig_root, ref_root,
1327 orig_generation, ref_generation,
1328 level - 1, 0);
1329 maybe_unlock_mutex(root);
1330 if (ret)
1331 goto fail;
1332 }
1333 }
1334 return 0;
1335fail:
1336 WARN_ON(1);
1337 return -1;
1338}
1339
Chris Mason9078a3e2007-04-26 16:46:15 -04001340static int write_one_cache_group(struct btrfs_trans_handle *trans,
1341 struct btrfs_root *root,
1342 struct btrfs_path *path,
1343 struct btrfs_block_group_cache *cache)
1344{
1345 int ret;
1346 int pending_ret;
1347 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04001348 unsigned long bi;
1349 struct extent_buffer *leaf;
Chris Mason9078a3e2007-04-26 16:46:15 -04001350
Chris Mason9078a3e2007-04-26 16:46:15 -04001351 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001352 if (ret < 0)
1353 goto fail;
Chris Mason9078a3e2007-04-26 16:46:15 -04001354 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04001355
1356 leaf = path->nodes[0];
1357 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
1358 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
1359 btrfs_mark_buffer_dirty(leaf);
Chris Mason9078a3e2007-04-26 16:46:15 -04001360 btrfs_release_path(extent_root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -04001361fail:
Chris Mason9078a3e2007-04-26 16:46:15 -04001362 finish_current_insert(trans, extent_root);
1363 pending_ret = del_pending_extents(trans, extent_root);
1364 if (ret)
1365 return ret;
1366 if (pending_ret)
1367 return pending_ret;
1368 return 0;
1369
1370}
1371
Chris Mason96b51792007-10-15 16:15:19 -04001372int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
1373 struct btrfs_root *root)
Chris Mason9078a3e2007-04-26 16:46:15 -04001374{
Josef Bacik0f9dd462008-09-23 13:14:11 -04001375 struct btrfs_block_group_cache *cache, *entry;
1376 struct rb_node *n;
Chris Mason9078a3e2007-04-26 16:46:15 -04001377 int err = 0;
1378 int werr = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04001379 struct btrfs_path *path;
Chris Mason96b51792007-10-15 16:15:19 -04001380 u64 last = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04001381
1382 path = btrfs_alloc_path();
1383 if (!path)
1384 return -ENOMEM;
1385
Chris Mason925baed2008-06-25 16:01:30 -04001386 mutex_lock(&root->fs_info->alloc_mutex);
Chris Mason9078a3e2007-04-26 16:46:15 -04001387 while(1) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001388 cache = NULL;
1389 spin_lock(&root->fs_info->block_group_cache_lock);
1390 for (n = rb_first(&root->fs_info->block_group_cache_tree);
1391 n; n = rb_next(n)) {
1392 entry = rb_entry(n, struct btrfs_block_group_cache,
1393 cache_node);
1394 if (entry->dirty) {
1395 cache = entry;
1396 break;
1397 }
1398 }
1399 spin_unlock(&root->fs_info->block_group_cache_lock);
1400
1401 if (!cache)
Chris Mason9078a3e2007-04-26 16:46:15 -04001402 break;
Chris Mason54aa1f42007-06-22 14:16:25 -04001403
Zheng Yane8569812008-09-26 10:05:48 -04001404 cache->dirty = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001405 last += cache->key.offset;
1406
Chris Mason96b51792007-10-15 16:15:19 -04001407 err = write_one_cache_group(trans, root,
1408 path, cache);
1409 /*
1410 * if we fail to write the cache group, we want
1411 * to keep it marked dirty in hopes that a later
1412 * write will work
1413 */
1414 if (err) {
1415 werr = err;
1416 continue;
Chris Mason9078a3e2007-04-26 16:46:15 -04001417 }
1418 }
1419 btrfs_free_path(path);
Chris Mason925baed2008-06-25 16:01:30 -04001420 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Mason9078a3e2007-04-26 16:46:15 -04001421 return werr;
1422}
1423
Chris Mason593060d2008-03-25 16:50:33 -04001424static int update_space_info(struct btrfs_fs_info *info, u64 flags,
1425 u64 total_bytes, u64 bytes_used,
1426 struct btrfs_space_info **space_info)
1427{
1428 struct btrfs_space_info *found;
1429
1430 found = __find_space_info(info, flags);
1431 if (found) {
1432 found->total_bytes += total_bytes;
1433 found->bytes_used += bytes_used;
Chris Mason8f18cf12008-04-25 16:53:30 -04001434 found->full = 0;
Chris Mason593060d2008-03-25 16:50:33 -04001435 *space_info = found;
1436 return 0;
1437 }
1438 found = kmalloc(sizeof(*found), GFP_NOFS);
1439 if (!found)
1440 return -ENOMEM;
1441
1442 list_add(&found->list, &info->space_info);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001443 INIT_LIST_HEAD(&found->block_groups);
1444 spin_lock_init(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04001445 found->flags = flags;
1446 found->total_bytes = total_bytes;
1447 found->bytes_used = bytes_used;
1448 found->bytes_pinned = 0;
Zheng Yane8569812008-09-26 10:05:48 -04001449 found->bytes_reserved = 0;
Chris Mason593060d2008-03-25 16:50:33 -04001450 found->full = 0;
Chris Mason0ef3e662008-05-24 14:04:53 -04001451 found->force_alloc = 0;
Chris Mason593060d2008-03-25 16:50:33 -04001452 *space_info = found;
1453 return 0;
1454}
1455
Chris Mason8790d502008-04-03 16:29:03 -04001456static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
1457{
1458 u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
Chris Mason611f0e02008-04-03 16:29:03 -04001459 BTRFS_BLOCK_GROUP_RAID1 |
Chris Mason321aecc2008-04-16 10:49:51 -04001460 BTRFS_BLOCK_GROUP_RAID10 |
Chris Mason611f0e02008-04-03 16:29:03 -04001461 BTRFS_BLOCK_GROUP_DUP);
Chris Mason8790d502008-04-03 16:29:03 -04001462 if (extra_flags) {
1463 if (flags & BTRFS_BLOCK_GROUP_DATA)
1464 fs_info->avail_data_alloc_bits |= extra_flags;
1465 if (flags & BTRFS_BLOCK_GROUP_METADATA)
1466 fs_info->avail_metadata_alloc_bits |= extra_flags;
1467 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
1468 fs_info->avail_system_alloc_bits |= extra_flags;
1469 }
1470}
Chris Mason593060d2008-03-25 16:50:33 -04001471
Chris Masona061fc82008-05-07 11:43:44 -04001472static u64 reduce_alloc_profile(struct btrfs_root *root, u64 flags)
Chris Masonec44a352008-04-28 15:29:52 -04001473{
Chris Masona061fc82008-05-07 11:43:44 -04001474 u64 num_devices = root->fs_info->fs_devices->num_devices;
1475
1476 if (num_devices == 1)
1477 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
1478 if (num_devices < 4)
1479 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
1480
Chris Masonec44a352008-04-28 15:29:52 -04001481 if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
1482 (flags & (BTRFS_BLOCK_GROUP_RAID1 |
Chris Masona061fc82008-05-07 11:43:44 -04001483 BTRFS_BLOCK_GROUP_RAID10))) {
Chris Masonec44a352008-04-28 15:29:52 -04001484 flags &= ~BTRFS_BLOCK_GROUP_DUP;
Chris Masona061fc82008-05-07 11:43:44 -04001485 }
Chris Masonec44a352008-04-28 15:29:52 -04001486
1487 if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
Chris Masona061fc82008-05-07 11:43:44 -04001488 (flags & BTRFS_BLOCK_GROUP_RAID10)) {
Chris Masonec44a352008-04-28 15:29:52 -04001489 flags &= ~BTRFS_BLOCK_GROUP_RAID1;
Chris Masona061fc82008-05-07 11:43:44 -04001490 }
Chris Masonec44a352008-04-28 15:29:52 -04001491
1492 if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
1493 ((flags & BTRFS_BLOCK_GROUP_RAID1) |
1494 (flags & BTRFS_BLOCK_GROUP_RAID10) |
1495 (flags & BTRFS_BLOCK_GROUP_DUP)))
1496 flags &= ~BTRFS_BLOCK_GROUP_RAID0;
1497 return flags;
1498}
1499
Chris Mason6324fbf2008-03-24 15:01:59 -04001500static int do_chunk_alloc(struct btrfs_trans_handle *trans,
1501 struct btrfs_root *extent_root, u64 alloc_bytes,
Chris Mason0ef3e662008-05-24 14:04:53 -04001502 u64 flags, int force)
Chris Mason6324fbf2008-03-24 15:01:59 -04001503{
1504 struct btrfs_space_info *space_info;
1505 u64 thresh;
1506 u64 start;
1507 u64 num_bytes;
Josef Bacikcf749822008-10-01 19:11:18 -04001508 int ret = 0, waited = 0;
Chris Mason6324fbf2008-03-24 15:01:59 -04001509
Chris Masona061fc82008-05-07 11:43:44 -04001510 flags = reduce_alloc_profile(extent_root, flags);
Chris Masonec44a352008-04-28 15:29:52 -04001511
Chris Mason6324fbf2008-03-24 15:01:59 -04001512 space_info = __find_space_info(extent_root->fs_info, flags);
Chris Mason593060d2008-03-25 16:50:33 -04001513 if (!space_info) {
1514 ret = update_space_info(extent_root->fs_info, flags,
1515 0, 0, &space_info);
1516 BUG_ON(ret);
1517 }
Chris Mason6324fbf2008-03-24 15:01:59 -04001518 BUG_ON(!space_info);
1519
Chris Mason0ef3e662008-05-24 14:04:53 -04001520 if (space_info->force_alloc) {
1521 force = 1;
1522 space_info->force_alloc = 0;
1523 }
Chris Mason6324fbf2008-03-24 15:01:59 -04001524 if (space_info->full)
Chris Mason925baed2008-06-25 16:01:30 -04001525 goto out;
Chris Mason6324fbf2008-03-24 15:01:59 -04001526
Chris Mason8790d502008-04-03 16:29:03 -04001527 thresh = div_factor(space_info->total_bytes, 6);
Chris Mason0ef3e662008-05-24 14:04:53 -04001528 if (!force &&
Zheng Yane8569812008-09-26 10:05:48 -04001529 (space_info->bytes_used + space_info->bytes_pinned +
1530 space_info->bytes_reserved + alloc_bytes) < thresh)
Chris Mason925baed2008-06-25 16:01:30 -04001531 goto out;
Chris Mason6324fbf2008-03-24 15:01:59 -04001532
Josef Bacikcf749822008-10-01 19:11:18 -04001533 while (!mutex_trylock(&extent_root->fs_info->chunk_mutex)) {
1534 if (!force)
1535 goto out;
1536 mutex_unlock(&extent_root->fs_info->alloc_mutex);
1537 cond_resched();
1538 mutex_lock(&extent_root->fs_info->alloc_mutex);
1539 waited = 1;
1540 }
1541
1542 if (waited && space_info->full)
1543 goto out_unlock;
1544
Chris Mason6324fbf2008-03-24 15:01:59 -04001545 ret = btrfs_alloc_chunk(trans, extent_root, &start, &num_bytes, flags);
1546 if (ret == -ENOSPC) {
1547printk("space info full %Lu\n", flags);
1548 space_info->full = 1;
Chris Masona74a4b92008-06-25 16:01:31 -04001549 goto out_unlock;
Chris Mason6324fbf2008-03-24 15:01:59 -04001550 }
Chris Mason6324fbf2008-03-24 15:01:59 -04001551 BUG_ON(ret);
1552
1553 ret = btrfs_make_block_group(trans, extent_root, 0, flags,
Chris Masone17cade2008-04-15 15:41:47 -04001554 BTRFS_FIRST_CHUNK_TREE_OBJECTID, start, num_bytes);
Chris Mason6324fbf2008-03-24 15:01:59 -04001555 BUG_ON(ret);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001556
Chris Masona74a4b92008-06-25 16:01:31 -04001557out_unlock:
Chris Mason333db942008-06-25 16:01:30 -04001558 mutex_unlock(&extent_root->fs_info->chunk_mutex);
Chris Masona74a4b92008-06-25 16:01:31 -04001559out:
Josef Bacik0f9dd462008-09-23 13:14:11 -04001560 return ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04001561}
1562
Chris Mason9078a3e2007-04-26 16:46:15 -04001563static int update_block_group(struct btrfs_trans_handle *trans,
1564 struct btrfs_root *root,
Chris Masondb945352007-10-15 16:15:53 -04001565 u64 bytenr, u64 num_bytes, int alloc,
Chris Mason0b86a832008-03-24 15:01:56 -04001566 int mark_free)
Chris Mason9078a3e2007-04-26 16:46:15 -04001567{
1568 struct btrfs_block_group_cache *cache;
1569 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -04001570 u64 total = num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04001571 u64 old_val;
Chris Masondb945352007-10-15 16:15:53 -04001572 u64 byte_in_group;
Chris Mason3e1ad542007-05-07 20:03:49 -04001573
Chris Mason7d9eb122008-07-08 14:19:17 -04001574 WARN_ON(!mutex_is_locked(&root->fs_info->alloc_mutex));
Chris Mason9078a3e2007-04-26 16:46:15 -04001575 while(total) {
Chris Masondb945352007-10-15 16:15:53 -04001576 cache = btrfs_lookup_block_group(info, bytenr);
Chris Mason3e1ad542007-05-07 20:03:49 -04001577 if (!cache) {
Chris Mason9078a3e2007-04-26 16:46:15 -04001578 return -1;
Chris Masoncd1bc462007-04-27 10:08:34 -04001579 }
Chris Masondb945352007-10-15 16:15:53 -04001580 byte_in_group = bytenr - cache->key.objectid;
1581 WARN_ON(byte_in_group > cache->key.offset);
Chris Mason9078a3e2007-04-26 16:46:15 -04001582
Chris Masonc286ac42008-07-22 23:06:41 -04001583 spin_lock(&cache->lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001584 cache->dirty = 1;
Chris Mason9078a3e2007-04-26 16:46:15 -04001585 old_val = btrfs_block_group_used(&cache->item);
Chris Masondb945352007-10-15 16:15:53 -04001586 num_bytes = min(total, cache->key.offset - byte_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -04001587 if (alloc) {
Chris Masondb945352007-10-15 16:15:53 -04001588 old_val += num_bytes;
Chris Mason6324fbf2008-03-24 15:01:59 -04001589 cache->space_info->bytes_used += num_bytes;
Chris Masonc286ac42008-07-22 23:06:41 -04001590 btrfs_set_block_group_used(&cache->item, old_val);
1591 spin_unlock(&cache->lock);
Chris Masoncd1bc462007-04-27 10:08:34 -04001592 } else {
Chris Masondb945352007-10-15 16:15:53 -04001593 old_val -= num_bytes;
Chris Mason6324fbf2008-03-24 15:01:59 -04001594 cache->space_info->bytes_used -= num_bytes;
Chris Masonc286ac42008-07-22 23:06:41 -04001595 btrfs_set_block_group_used(&cache->item, old_val);
1596 spin_unlock(&cache->lock);
Chris Masonf510cfe2007-10-15 16:14:48 -04001597 if (mark_free) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001598 int ret;
1599 ret = btrfs_add_free_space(cache, bytenr,
1600 num_bytes);
1601 if (ret)
1602 return -1;
Chris Masone37c9e62007-05-09 20:13:14 -04001603 }
Chris Masoncd1bc462007-04-27 10:08:34 -04001604 }
Chris Masondb945352007-10-15 16:15:53 -04001605 total -= num_bytes;
1606 bytenr += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04001607 }
1608 return 0;
1609}
Chris Mason6324fbf2008-03-24 15:01:59 -04001610
Chris Masona061fc82008-05-07 11:43:44 -04001611static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
1612{
Josef Bacik0f9dd462008-09-23 13:14:11 -04001613 struct btrfs_block_group_cache *cache;
1614
1615 cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
1616 if (!cache)
Chris Masona061fc82008-05-07 11:43:44 -04001617 return 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001618
1619 return cache->key.objectid;
Chris Masona061fc82008-05-07 11:43:44 -04001620}
1621
Chris Masone02119d2008-09-05 16:13:11 -04001622int btrfs_update_pinned_extents(struct btrfs_root *root,
Yan324ae4d2007-11-16 14:57:08 -05001623 u64 bytenr, u64 num, int pin)
1624{
1625 u64 len;
1626 struct btrfs_block_group_cache *cache;
1627 struct btrfs_fs_info *fs_info = root->fs_info;
1628
Chris Mason7d9eb122008-07-08 14:19:17 -04001629 WARN_ON(!mutex_is_locked(&root->fs_info->alloc_mutex));
Yan324ae4d2007-11-16 14:57:08 -05001630 if (pin) {
1631 set_extent_dirty(&fs_info->pinned_extents,
1632 bytenr, bytenr + num - 1, GFP_NOFS);
1633 } else {
1634 clear_extent_dirty(&fs_info->pinned_extents,
1635 bytenr, bytenr + num - 1, GFP_NOFS);
1636 }
1637 while (num > 0) {
1638 cache = btrfs_lookup_block_group(fs_info, bytenr);
Zheng Yane8569812008-09-26 10:05:48 -04001639 BUG_ON(!cache);
1640 len = min(num, cache->key.offset -
1641 (bytenr - cache->key.objectid));
Yan324ae4d2007-11-16 14:57:08 -05001642 if (pin) {
Zheng Yane8569812008-09-26 10:05:48 -04001643 spin_lock(&cache->lock);
1644 cache->pinned += len;
1645 cache->space_info->bytes_pinned += len;
1646 spin_unlock(&cache->lock);
Yan324ae4d2007-11-16 14:57:08 -05001647 fs_info->total_pinned += len;
1648 } else {
Zheng Yane8569812008-09-26 10:05:48 -04001649 spin_lock(&cache->lock);
1650 cache->pinned -= len;
1651 cache->space_info->bytes_pinned -= len;
1652 spin_unlock(&cache->lock);
Yan324ae4d2007-11-16 14:57:08 -05001653 fs_info->total_pinned -= len;
1654 }
1655 bytenr += len;
1656 num -= len;
1657 }
1658 return 0;
1659}
Chris Mason9078a3e2007-04-26 16:46:15 -04001660
Zheng Yane8569812008-09-26 10:05:48 -04001661static int update_reserved_extents(struct btrfs_root *root,
1662 u64 bytenr, u64 num, int reserve)
1663{
1664 u64 len;
1665 struct btrfs_block_group_cache *cache;
1666 struct btrfs_fs_info *fs_info = root->fs_info;
1667
1668 WARN_ON(!mutex_is_locked(&root->fs_info->alloc_mutex));
1669 while (num > 0) {
1670 cache = btrfs_lookup_block_group(fs_info, bytenr);
1671 BUG_ON(!cache);
1672 len = min(num, cache->key.offset -
1673 (bytenr - cache->key.objectid));
1674 if (reserve) {
1675 spin_lock(&cache->lock);
1676 cache->reserved += len;
1677 cache->space_info->bytes_reserved += len;
1678 spin_unlock(&cache->lock);
1679 } else {
1680 spin_lock(&cache->lock);
1681 cache->reserved -= len;
1682 cache->space_info->bytes_reserved -= len;
1683 spin_unlock(&cache->lock);
1684 }
1685 bytenr += len;
1686 num -= len;
1687 }
1688 return 0;
1689}
1690
Chris Masond1310b22008-01-24 16:13:08 -05001691int btrfs_copy_pinned(struct btrfs_root *root, struct extent_io_tree *copy)
Chris Masonccd467d2007-06-28 15:57:36 -04001692{
Chris Masonccd467d2007-06-28 15:57:36 -04001693 u64 last = 0;
Chris Mason1a5bc162007-10-15 16:15:26 -04001694 u64 start;
1695 u64 end;
Chris Masond1310b22008-01-24 16:13:08 -05001696 struct extent_io_tree *pinned_extents = &root->fs_info->pinned_extents;
Chris Masonccd467d2007-06-28 15:57:36 -04001697 int ret;
Chris Masonccd467d2007-06-28 15:57:36 -04001698
1699 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04001700 ret = find_first_extent_bit(pinned_extents, last,
1701 &start, &end, EXTENT_DIRTY);
1702 if (ret)
Chris Masonccd467d2007-06-28 15:57:36 -04001703 break;
Chris Mason1a5bc162007-10-15 16:15:26 -04001704 set_extent_dirty(copy, start, end, GFP_NOFS);
1705 last = end + 1;
Chris Masonccd467d2007-06-28 15:57:36 -04001706 }
1707 return 0;
1708}
1709
1710int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
1711 struct btrfs_root *root,
Chris Masond1310b22008-01-24 16:13:08 -05001712 struct extent_io_tree *unpin)
Chris Masona28ec192007-03-06 20:08:01 -05001713{
Chris Mason1a5bc162007-10-15 16:15:26 -04001714 u64 start;
1715 u64 end;
Chris Masona28ec192007-03-06 20:08:01 -05001716 int ret;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001717 struct btrfs_block_group_cache *cache;
Chris Masona28ec192007-03-06 20:08:01 -05001718
Chris Mason925baed2008-06-25 16:01:30 -04001719 mutex_lock(&root->fs_info->alloc_mutex);
Chris Masona28ec192007-03-06 20:08:01 -05001720 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04001721 ret = find_first_extent_bit(unpin, 0, &start, &end,
1722 EXTENT_DIRTY);
1723 if (ret)
Chris Masona28ec192007-03-06 20:08:01 -05001724 break;
Chris Masone02119d2008-09-05 16:13:11 -04001725 btrfs_update_pinned_extents(root, start, end + 1 - start, 0);
Chris Mason1a5bc162007-10-15 16:15:26 -04001726 clear_extent_dirty(unpin, start, end, GFP_NOFS);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001727 cache = btrfs_lookup_block_group(root->fs_info, start);
1728 if (cache->cached)
1729 btrfs_add_free_space(cache, start, end - start + 1);
Chris Masonc286ac42008-07-22 23:06:41 -04001730 if (need_resched()) {
1731 mutex_unlock(&root->fs_info->alloc_mutex);
1732 cond_resched();
1733 mutex_lock(&root->fs_info->alloc_mutex);
1734 }
Chris Masona28ec192007-03-06 20:08:01 -05001735 }
Chris Mason925baed2008-06-25 16:01:30 -04001736 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Masona28ec192007-03-06 20:08:01 -05001737 return 0;
1738}
1739
Chris Mason98ed5172008-01-03 10:01:48 -05001740static int finish_current_insert(struct btrfs_trans_handle *trans,
1741 struct btrfs_root *extent_root)
Chris Mason037e6392007-03-07 11:50:24 -05001742{
Chris Mason7bb86312007-12-11 09:25:06 -05001743 u64 start;
1744 u64 end;
Zheng Yan31840ae2008-09-23 13:14:14 -04001745 u64 priv;
Chris Mason7bb86312007-12-11 09:25:06 -05001746 struct btrfs_fs_info *info = extent_root->fs_info;
1747 struct btrfs_path *path;
Zheng Yan31840ae2008-09-23 13:14:14 -04001748 struct btrfs_extent_ref *ref;
1749 struct pending_extent_op *extent_op;
1750 struct btrfs_key key;
Chris Mason234b63a2007-03-13 10:46:10 -04001751 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -05001752 int ret;
Chris Mason1a5bc162007-10-15 16:15:26 -04001753 int err = 0;
Chris Mason037e6392007-03-07 11:50:24 -05001754
Chris Mason7d9eb122008-07-08 14:19:17 -04001755 WARN_ON(!mutex_is_locked(&extent_root->fs_info->alloc_mutex));
Chris Mason5f39d392007-10-15 16:14:19 -04001756 btrfs_set_stack_extent_refs(&extent_item, 1);
Chris Mason7bb86312007-12-11 09:25:06 -05001757 path = btrfs_alloc_path();
Chris Mason037e6392007-03-07 11:50:24 -05001758
Chris Mason26b80032007-08-08 20:17:12 -04001759 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04001760 ret = find_first_extent_bit(&info->extent_ins, 0, &start,
1761 &end, EXTENT_LOCKED);
1762 if (ret)
Chris Mason26b80032007-08-08 20:17:12 -04001763 break;
1764
Zheng Yan31840ae2008-09-23 13:14:14 -04001765 ret = get_state_private(&info->extent_ins, start, &priv);
1766 BUG_ON(ret);
1767 extent_op = (struct pending_extent_op *)(unsigned long)priv;
1768
1769 if (extent_op->type == PENDING_EXTENT_INSERT) {
1770 key.objectid = start;
1771 key.offset = end + 1 - start;
1772 key.type = BTRFS_EXTENT_ITEM_KEY;
1773 err = btrfs_insert_item(trans, extent_root, &key,
Chris Mason1a5bc162007-10-15 16:15:26 -04001774 &extent_item, sizeof(extent_item));
Zheng Yan31840ae2008-09-23 13:14:14 -04001775 BUG_ON(err);
Chris Masonc286ac42008-07-22 23:06:41 -04001776
Zheng Yan31840ae2008-09-23 13:14:14 -04001777 clear_extent_bits(&info->extent_ins, start, end,
1778 EXTENT_LOCKED, GFP_NOFS);
Chris Masonc286ac42008-07-22 23:06:41 -04001779
Zheng Yan31840ae2008-09-23 13:14:14 -04001780 err = insert_extent_backref(trans, extent_root, path,
1781 start, extent_op->parent,
1782 extent_root->root_key.objectid,
1783 extent_op->generation,
1784 extent_op->level, 0);
1785 BUG_ON(err);
1786 } else if (extent_op->type == PENDING_BACKREF_UPDATE) {
1787 err = lookup_extent_backref(trans, extent_root, path,
1788 start, extent_op->orig_parent,
1789 extent_root->root_key.objectid,
1790 extent_op->orig_generation, 0);
1791 BUG_ON(err);
Chris Masonc286ac42008-07-22 23:06:41 -04001792
Zheng Yan31840ae2008-09-23 13:14:14 -04001793 clear_extent_bits(&info->extent_ins, start, end,
1794 EXTENT_LOCKED, GFP_NOFS);
1795
1796 key.objectid = start;
1797 key.offset = extent_op->parent;
1798 key.type = BTRFS_EXTENT_REF_KEY;
1799 err = btrfs_set_item_key_safe(trans, extent_root, path,
1800 &key);
1801 BUG_ON(err);
1802 ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1803 struct btrfs_extent_ref);
1804 btrfs_set_ref_generation(path->nodes[0], ref,
1805 extent_op->generation);
1806 btrfs_mark_buffer_dirty(path->nodes[0]);
1807 btrfs_release_path(extent_root, path);
Chris Masond8d5f3e2007-12-11 12:42:00 -05001808 } else {
Zheng Yan31840ae2008-09-23 13:14:14 -04001809 BUG_ON(1);
Chris Masond8d5f3e2007-12-11 12:42:00 -05001810 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001811 kfree(extent_op);
1812
Chris Masonc286ac42008-07-22 23:06:41 -04001813 if (need_resched()) {
1814 mutex_unlock(&extent_root->fs_info->alloc_mutex);
1815 cond_resched();
1816 mutex_lock(&extent_root->fs_info->alloc_mutex);
1817 }
Chris Mason037e6392007-03-07 11:50:24 -05001818 }
Chris Mason7bb86312007-12-11 09:25:06 -05001819 btrfs_free_path(path);
Chris Mason037e6392007-03-07 11:50:24 -05001820 return 0;
1821}
1822
Zheng Yan31840ae2008-09-23 13:14:14 -04001823static int pin_down_bytes(struct btrfs_trans_handle *trans,
1824 struct btrfs_root *root,
1825 u64 bytenr, u64 num_bytes, int is_data)
Chris Masone20d96d2007-03-22 12:13:20 -04001826{
Chris Mason1a5bc162007-10-15 16:15:26 -04001827 int err = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04001828 struct extent_buffer *buf;
Chris Mason78fae272007-03-25 11:35:08 -04001829
Chris Mason7d9eb122008-07-08 14:19:17 -04001830 WARN_ON(!mutex_is_locked(&root->fs_info->alloc_mutex));
Zheng Yan31840ae2008-09-23 13:14:14 -04001831 if (is_data)
1832 goto pinit;
Chris Mason4bef0842008-09-08 11:18:08 -04001833
Zheng Yan31840ae2008-09-23 13:14:14 -04001834 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
1835 if (!buf)
1836 goto pinit;
Chris Mason4bef0842008-09-08 11:18:08 -04001837
Zheng Yan31840ae2008-09-23 13:14:14 -04001838 /* we can reuse a block if it hasn't been written
1839 * and it is from this transaction. We can't
1840 * reuse anything from the tree log root because
1841 * it has tiny sub-transactions.
1842 */
1843 if (btrfs_buffer_uptodate(buf, 0) &&
1844 btrfs_try_tree_lock(buf)) {
1845 u64 header_owner = btrfs_header_owner(buf);
1846 u64 header_transid = btrfs_header_generation(buf);
1847 if (header_owner != BTRFS_TREE_LOG_OBJECTID &&
Zheng Yan1a40e232008-09-26 10:09:34 -04001848 header_owner != BTRFS_TREE_RELOC_OBJECTID &&
Zheng Yan31840ae2008-09-23 13:14:14 -04001849 header_transid == trans->transid &&
1850 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
1851 clean_tree_block(NULL, root, buf);
1852 btrfs_tree_unlock(buf);
Chris Mason5f39d392007-10-15 16:14:19 -04001853 free_extent_buffer(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04001854 return 1;
Chris Mason8ef97622007-03-26 10:15:30 -04001855 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001856 btrfs_tree_unlock(buf);
Chris Masonf4b9aa82007-03-27 11:05:53 -04001857 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001858 free_extent_buffer(buf);
1859pinit:
1860 btrfs_update_pinned_extents(root, bytenr, num_bytes, 1);
1861
Chris Masonbe744172007-05-06 10:15:01 -04001862 BUG_ON(err < 0);
Chris Masone20d96d2007-03-22 12:13:20 -04001863 return 0;
1864}
1865
Chris Masona28ec192007-03-06 20:08:01 -05001866/*
1867 * remove an extent from the root, returns 0 on success
1868 */
Zheng Yan31840ae2008-09-23 13:14:14 -04001869static int __free_extent(struct btrfs_trans_handle *trans,
1870 struct btrfs_root *root,
1871 u64 bytenr, u64 num_bytes, u64 parent,
Chris Mason7bb86312007-12-11 09:25:06 -05001872 u64 root_objectid, u64 ref_generation,
Zheng Yan31840ae2008-09-23 13:14:14 -04001873 u64 owner_objectid, u64 owner_offset,
1874 int pin, int mark_free)
Chris Masona28ec192007-03-06 20:08:01 -05001875{
Chris Mason5caf2a02007-04-02 11:20:42 -04001876 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -04001877 struct btrfs_key key;
Chris Mason1261ec42007-03-20 20:35:03 -04001878 struct btrfs_fs_info *info = root->fs_info;
1879 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04001880 struct extent_buffer *leaf;
Chris Masona28ec192007-03-06 20:08:01 -05001881 int ret;
Chris Mason952fcca2008-02-18 16:33:44 -05001882 int extent_slot = 0;
1883 int found_extent = 0;
1884 int num_to_del = 1;
Chris Mason234b63a2007-03-13 10:46:10 -04001885 struct btrfs_extent_item *ei;
Chris Masoncf27e1e2007-03-13 09:49:06 -04001886 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -05001887
Chris Mason7d9eb122008-07-08 14:19:17 -04001888 WARN_ON(!mutex_is_locked(&root->fs_info->alloc_mutex));
Chris Masondb945352007-10-15 16:15:53 -04001889 key.objectid = bytenr;
Chris Mason62e27492007-03-15 12:56:47 -04001890 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masondb945352007-10-15 16:15:53 -04001891 key.offset = num_bytes;
Chris Mason5caf2a02007-04-02 11:20:42 -04001892 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04001893 if (!path)
1894 return -ENOMEM;
1895
Chris Mason3c12ac72008-04-21 12:01:38 -04001896 path->reada = 1;
Zheng Yan31840ae2008-09-23 13:14:14 -04001897 ret = lookup_extent_backref(trans, extent_root, path, bytenr, parent,
1898 root_objectid, ref_generation, 1);
Chris Mason7bb86312007-12-11 09:25:06 -05001899 if (ret == 0) {
Chris Mason952fcca2008-02-18 16:33:44 -05001900 struct btrfs_key found_key;
1901 extent_slot = path->slots[0];
1902 while(extent_slot > 0) {
1903 extent_slot--;
1904 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1905 extent_slot);
1906 if (found_key.objectid != bytenr)
1907 break;
1908 if (found_key.type == BTRFS_EXTENT_ITEM_KEY &&
1909 found_key.offset == num_bytes) {
1910 found_extent = 1;
1911 break;
1912 }
1913 if (path->slots[0] - extent_slot > 5)
1914 break;
1915 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001916 if (!found_extent) {
1917 ret = remove_extent_backref(trans, extent_root, path);
1918 BUG_ON(ret);
1919 btrfs_release_path(extent_root, path);
1920 ret = btrfs_search_slot(trans, extent_root,
1921 &key, path, -1, 1);
1922 BUG_ON(ret);
1923 extent_slot = path->slots[0];
1924 }
Chris Mason7bb86312007-12-11 09:25:06 -05001925 } else {
1926 btrfs_print_leaf(extent_root, path->nodes[0]);
1927 WARN_ON(1);
1928 printk("Unable to find ref byte nr %Lu root %Lu "
1929 " gen %Lu owner %Lu offset %Lu\n", bytenr,
1930 root_objectid, ref_generation, owner_objectid,
1931 owner_offset);
1932 }
Chris Mason5f39d392007-10-15 16:14:19 -04001933
1934 leaf = path->nodes[0];
Chris Mason952fcca2008-02-18 16:33:44 -05001935 ei = btrfs_item_ptr(leaf, extent_slot,
Chris Mason123abc82007-03-14 14:14:43 -04001936 struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001937 refs = btrfs_extent_refs(leaf, ei);
1938 BUG_ON(refs == 0);
1939 refs -= 1;
1940 btrfs_set_extent_refs(leaf, ei, refs);
Chris Mason952fcca2008-02-18 16:33:44 -05001941
Chris Mason5f39d392007-10-15 16:14:19 -04001942 btrfs_mark_buffer_dirty(leaf);
1943
Chris Mason952fcca2008-02-18 16:33:44 -05001944 if (refs == 0 && found_extent && path->slots[0] == extent_slot + 1) {
Zheng Yan31840ae2008-09-23 13:14:14 -04001945 struct btrfs_extent_ref *ref;
1946 ref = btrfs_item_ptr(leaf, path->slots[0],
1947 struct btrfs_extent_ref);
1948 BUG_ON(btrfs_ref_num_refs(leaf, ref) != 1);
Chris Mason952fcca2008-02-18 16:33:44 -05001949 /* if the back ref and the extent are next to each other
1950 * they get deleted below in one shot
1951 */
1952 path->slots[0] = extent_slot;
1953 num_to_del = 2;
1954 } else if (found_extent) {
1955 /* otherwise delete the extent back ref */
Zheng Yan31840ae2008-09-23 13:14:14 -04001956 ret = remove_extent_backref(trans, extent_root, path);
Chris Mason952fcca2008-02-18 16:33:44 -05001957 BUG_ON(ret);
1958 /* if refs are 0, we need to setup the path for deletion */
1959 if (refs == 0) {
1960 btrfs_release_path(extent_root, path);
1961 ret = btrfs_search_slot(trans, extent_root, &key, path,
1962 -1, 1);
Chris Mason952fcca2008-02-18 16:33:44 -05001963 BUG_ON(ret);
1964 }
1965 }
1966
Chris Masoncf27e1e2007-03-13 09:49:06 -04001967 if (refs == 0) {
Chris Masondb945352007-10-15 16:15:53 -04001968 u64 super_used;
1969 u64 root_used;
David Woodhouse21af8042008-08-12 14:13:26 +01001970#ifdef BIO_RW_DISCARD
1971 u64 map_length = num_bytes;
1972 struct btrfs_multi_bio *multi = NULL;
1973#endif
Chris Mason78fae272007-03-25 11:35:08 -04001974
1975 if (pin) {
Zheng Yan31840ae2008-09-23 13:14:14 -04001976 ret = pin_down_bytes(trans, root, bytenr, num_bytes,
1977 owner_objectid >= BTRFS_FIRST_FREE_OBJECTID);
Yanc5492282007-11-06 10:25:25 -05001978 if (ret > 0)
1979 mark_free = 1;
1980 BUG_ON(ret < 0);
Chris Mason78fae272007-03-25 11:35:08 -04001981 }
1982
Josef Bacik58176a92007-08-29 15:47:34 -04001983 /* block accounting for super block */
Chris Masona2135012008-06-25 16:01:30 -04001984 spin_lock_irq(&info->delalloc_lock);
Chris Masondb945352007-10-15 16:15:53 -04001985 super_used = btrfs_super_bytes_used(&info->super_copy);
1986 btrfs_set_super_bytes_used(&info->super_copy,
1987 super_used - num_bytes);
Chris Masona2135012008-06-25 16:01:30 -04001988 spin_unlock_irq(&info->delalloc_lock);
Josef Bacik58176a92007-08-29 15:47:34 -04001989
1990 /* block accounting for root item */
Chris Masondb945352007-10-15 16:15:53 -04001991 root_used = btrfs_root_used(&root->root_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001992 btrfs_set_root_used(&root->root_item,
Chris Masondb945352007-10-15 16:15:53 -04001993 root_used - num_bytes);
Chris Mason952fcca2008-02-18 16:33:44 -05001994 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
1995 num_to_del);
Zheng Yan31840ae2008-09-23 13:14:14 -04001996 BUG_ON(ret);
Chris Masondb945352007-10-15 16:15:53 -04001997 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
Chris Mason0b86a832008-03-24 15:01:56 -04001998 mark_free);
Chris Mason9078a3e2007-04-26 16:46:15 -04001999 BUG_ON(ret);
David Woodhouse21af8042008-08-12 14:13:26 +01002000
2001#ifdef BIO_RW_DISCARD
2002 /* Tell the block device(s) that the sectors can be discarded */
2003 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
2004 bytenr, &map_length, &multi, 0);
2005 if (!ret) {
2006 struct btrfs_bio_stripe *stripe = multi->stripes;
2007 int i;
2008
2009 if (map_length > num_bytes)
2010 map_length = num_bytes;
2011
2012 for (i = 0; i < multi->num_stripes; i++, stripe++) {
2013 blkdev_issue_discard(stripe->dev->bdev,
2014 stripe->physical >> 9,
2015 map_length >> 9);
2016 }
2017 kfree(multi);
2018 }
2019#endif
Chris Masona28ec192007-03-06 20:08:01 -05002020 }
Chris Mason5caf2a02007-04-02 11:20:42 -04002021 btrfs_free_path(path);
Chris Masone089f052007-03-16 16:20:31 -04002022 finish_current_insert(trans, extent_root);
Chris Masona28ec192007-03-06 20:08:01 -05002023 return ret;
2024}
2025
2026/*
Chris Masonfec577f2007-02-26 10:40:21 -05002027 * find all the blocks marked as pending in the radix tree and remove
2028 * them from the extent map
2029 */
Chris Masone089f052007-03-16 16:20:31 -04002030static int del_pending_extents(struct btrfs_trans_handle *trans, struct
2031 btrfs_root *extent_root)
Chris Masonfec577f2007-02-26 10:40:21 -05002032{
2033 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -04002034 int err = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04002035 int mark_free = 0;
Chris Mason1a5bc162007-10-15 16:15:26 -04002036 u64 start;
2037 u64 end;
Zheng Yan31840ae2008-09-23 13:14:14 -04002038 u64 priv;
Chris Masond1310b22008-01-24 16:13:08 -05002039 struct extent_io_tree *pending_del;
Zheng Yan31840ae2008-09-23 13:14:14 -04002040 struct extent_io_tree *extent_ins;
2041 struct pending_extent_op *extent_op;
Chris Mason8ef97622007-03-26 10:15:30 -04002042
Chris Mason7d9eb122008-07-08 14:19:17 -04002043 WARN_ON(!mutex_is_locked(&extent_root->fs_info->alloc_mutex));
Zheng Yan31840ae2008-09-23 13:14:14 -04002044 extent_ins = &extent_root->fs_info->extent_ins;
Chris Mason1a5bc162007-10-15 16:15:26 -04002045 pending_del = &extent_root->fs_info->pending_del;
Chris Masonfec577f2007-02-26 10:40:21 -05002046
2047 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04002048 ret = find_first_extent_bit(pending_del, 0, &start, &end,
2049 EXTENT_LOCKED);
2050 if (ret)
Chris Masonfec577f2007-02-26 10:40:21 -05002051 break;
Zheng Yan31840ae2008-09-23 13:14:14 -04002052
2053 ret = get_state_private(pending_del, start, &priv);
2054 BUG_ON(ret);
2055 extent_op = (struct pending_extent_op *)(unsigned long)priv;
2056
Chris Mason1a5bc162007-10-15 16:15:26 -04002057 clear_extent_bits(pending_del, start, end, EXTENT_LOCKED,
2058 GFP_NOFS);
Zheng Yan31840ae2008-09-23 13:14:14 -04002059
2060 ret = pin_down_bytes(trans, extent_root, start,
2061 end + 1 - start, 0);
2062 mark_free = ret > 0;
2063 if (!test_range_bit(extent_ins, start, end,
2064 EXTENT_LOCKED, 0)) {
2065free_extent:
Chris Masonc286ac42008-07-22 23:06:41 -04002066 ret = __free_extent(trans, extent_root,
Zheng Yan31840ae2008-09-23 13:14:14 -04002067 start, end + 1 - start,
2068 extent_op->orig_parent,
2069 extent_root->root_key.objectid,
2070 extent_op->orig_generation,
2071 extent_op->level, 0, 0, mark_free);
2072 kfree(extent_op);
Chris Masonc286ac42008-07-22 23:06:41 -04002073 } else {
Zheng Yan31840ae2008-09-23 13:14:14 -04002074 kfree(extent_op);
2075 ret = get_state_private(extent_ins, start, &priv);
2076 BUG_ON(ret);
2077 extent_op = (struct pending_extent_op *)
2078 (unsigned long)priv;
2079
2080 clear_extent_bits(extent_ins, start, end,
2081 EXTENT_LOCKED, GFP_NOFS);
2082
2083 if (extent_op->type == PENDING_BACKREF_UPDATE)
2084 goto free_extent;
2085
2086 ret = update_block_group(trans, extent_root, start,
2087 end + 1 - start, 0, mark_free);
2088 BUG_ON(ret);
2089 kfree(extent_op);
Chris Masonc286ac42008-07-22 23:06:41 -04002090 }
Chris Mason1a5bc162007-10-15 16:15:26 -04002091 if (ret)
2092 err = ret;
Chris Masonc286ac42008-07-22 23:06:41 -04002093
2094 if (need_resched()) {
2095 mutex_unlock(&extent_root->fs_info->alloc_mutex);
2096 cond_resched();
2097 mutex_lock(&extent_root->fs_info->alloc_mutex);
2098 }
Chris Masonfec577f2007-02-26 10:40:21 -05002099 }
Chris Masone20d96d2007-03-22 12:13:20 -04002100 return err;
Chris Masonfec577f2007-02-26 10:40:21 -05002101}
2102
2103/*
2104 * remove an extent from the root, returns 0 on success
2105 */
Chris Mason925baed2008-06-25 16:01:30 -04002106static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04002107 struct btrfs_root *root,
2108 u64 bytenr, u64 num_bytes, u64 parent,
2109 u64 root_objectid, u64 ref_generation,
2110 u64 owner_objectid, u64 owner_offset, int pin)
Chris Masonfec577f2007-02-26 10:40:21 -05002111{
Chris Mason9f5fae22007-03-20 14:38:32 -04002112 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Masonfec577f2007-02-26 10:40:21 -05002113 int pending_ret;
2114 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05002115
Chris Masondb945352007-10-15 16:15:53 -04002116 WARN_ON(num_bytes < root->sectorsize);
Chris Masona28ec192007-03-06 20:08:01 -05002117 if (root == extent_root) {
Zheng Yan31840ae2008-09-23 13:14:14 -04002118 struct pending_extent_op *extent_op;
2119
2120 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
2121 BUG_ON(!extent_op);
2122
2123 extent_op->type = PENDING_EXTENT_DELETE;
2124 extent_op->bytenr = bytenr;
2125 extent_op->num_bytes = num_bytes;
2126 extent_op->parent = parent;
2127 extent_op->orig_parent = parent;
2128 extent_op->generation = ref_generation;
2129 extent_op->orig_generation = ref_generation;
2130 extent_op->level = (int)owner_objectid;
2131
2132 set_extent_bits(&root->fs_info->pending_del,
2133 bytenr, bytenr + num_bytes - 1,
2134 EXTENT_LOCKED, GFP_NOFS);
2135 set_state_private(&root->fs_info->pending_del,
2136 bytenr, (unsigned long)extent_op);
Chris Masona28ec192007-03-06 20:08:01 -05002137 return 0;
2138 }
Chris Mason4bef0842008-09-08 11:18:08 -04002139 /* if metadata always pin */
Chris Masond00aff02008-09-11 15:54:42 -04002140 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
2141 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04002142 struct btrfs_block_group_cache *cache;
2143
Chris Masond00aff02008-09-11 15:54:42 -04002144 /* btrfs_free_reserved_extent */
Josef Bacik0f9dd462008-09-23 13:14:11 -04002145 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
2146 BUG_ON(!cache);
2147 btrfs_add_free_space(cache, bytenr, num_bytes);
Zheng Yane8569812008-09-26 10:05:48 -04002148 update_reserved_extents(root, bytenr, num_bytes, 0);
Chris Masond00aff02008-09-11 15:54:42 -04002149 return 0;
2150 }
Chris Mason4bef0842008-09-08 11:18:08 -04002151 pin = 1;
Chris Masond00aff02008-09-11 15:54:42 -04002152 }
Chris Mason4bef0842008-09-08 11:18:08 -04002153
2154 /* if data pin when any transaction has committed this */
2155 if (ref_generation != trans->transid)
2156 pin = 1;
2157
Zheng Yan31840ae2008-09-23 13:14:14 -04002158 ret = __free_extent(trans, root, bytenr, num_bytes, parent,
2159 root_objectid, ref_generation, owner_objectid,
2160 owner_offset, pin, pin == 0);
Chris Masonee6e6502008-07-17 12:54:40 -04002161
2162 finish_current_insert(trans, root->fs_info->extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -04002163 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
Chris Masonfec577f2007-02-26 10:40:21 -05002164 return ret ? ret : pending_ret;
2165}
2166
Chris Mason925baed2008-06-25 16:01:30 -04002167int btrfs_free_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04002168 struct btrfs_root *root,
2169 u64 bytenr, u64 num_bytes, u64 parent,
2170 u64 root_objectid, u64 ref_generation,
2171 u64 owner_objectid, u64 owner_offset, int pin)
Chris Mason925baed2008-06-25 16:01:30 -04002172{
2173 int ret;
2174
2175 maybe_lock_mutex(root);
Zheng Yan31840ae2008-09-23 13:14:14 -04002176 ret = __btrfs_free_extent(trans, root, bytenr, num_bytes, parent,
Chris Mason925baed2008-06-25 16:01:30 -04002177 root_objectid, ref_generation,
2178 owner_objectid, owner_offset, pin);
2179 maybe_unlock_mutex(root);
2180 return ret;
2181}
2182
Chris Mason87ee04e2007-11-30 11:30:34 -05002183static u64 stripe_align(struct btrfs_root *root, u64 val)
2184{
2185 u64 mask = ((u64)root->stripesize - 1);
2186 u64 ret = (val + mask) & ~mask;
2187 return ret;
2188}
2189
Chris Masonfec577f2007-02-26 10:40:21 -05002190/*
2191 * walks the btree of allocated extents and find a hole of a given size.
2192 * The key ins is changed to record the hole:
2193 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -04002194 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -05002195 * ins->offset == number of blocks
2196 * Any available blocks before search_start are skipped.
2197 */
Chris Mason98ed5172008-01-03 10:01:48 -05002198static int noinline find_free_extent(struct btrfs_trans_handle *trans,
2199 struct btrfs_root *orig_root,
2200 u64 num_bytes, u64 empty_size,
2201 u64 search_start, u64 search_end,
2202 u64 hint_byte, struct btrfs_key *ins,
2203 u64 exclude_start, u64 exclude_nr,
2204 int data)
Chris Masonfec577f2007-02-26 10:40:21 -05002205{
Chris Mason87ee04e2007-11-30 11:30:34 -05002206 int ret;
Chris Masona061fc82008-05-07 11:43:44 -04002207 u64 orig_search_start;
Chris Mason9f5fae22007-03-20 14:38:32 -04002208 struct btrfs_root * root = orig_root->fs_info->extent_root;
Chris Masonf2458e12007-04-25 15:52:25 -04002209 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -04002210 u64 total_needed = num_bytes;
Chris Mason239b14b2008-03-24 15:02:07 -04002211 u64 *last_ptr = NULL;
Chris Masonbe08c1b2007-05-03 09:06:49 -04002212 struct btrfs_block_group_cache *block_group;
Chris Mason0ef3e662008-05-24 14:04:53 -04002213 int chunk_alloc_done = 0;
Chris Mason239b14b2008-03-24 15:02:07 -04002214 int empty_cluster = 2 * 1024 * 1024;
Chris Mason0ef3e662008-05-24 14:04:53 -04002215 int allowed_chunk_alloc = 0;
Chris Masonfec577f2007-02-26 10:40:21 -05002216
Chris Masondb945352007-10-15 16:15:53 -04002217 WARN_ON(num_bytes < root->sectorsize);
Chris Masonb1a4d962007-04-04 15:27:52 -04002218 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
2219
Chris Mason0ef3e662008-05-24 14:04:53 -04002220 if (orig_root->ref_cows || empty_size)
2221 allowed_chunk_alloc = 1;
2222
Chris Mason239b14b2008-03-24 15:02:07 -04002223 if (data & BTRFS_BLOCK_GROUP_METADATA) {
2224 last_ptr = &root->fs_info->last_alloc;
Chris Mason8790d502008-04-03 16:29:03 -04002225 empty_cluster = 256 * 1024;
Chris Mason239b14b2008-03-24 15:02:07 -04002226 }
2227
Josef Bacik0f9dd462008-09-23 13:14:11 -04002228 if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD))
Chris Mason239b14b2008-03-24 15:02:07 -04002229 last_ptr = &root->fs_info->last_data_alloc;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002230
Chris Masone02119d2008-09-05 16:13:11 -04002231 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
2232 last_ptr = &root->fs_info->last_log_alloc;
2233 if (!last_ptr == 0 && root->fs_info->last_alloc) {
2234 *last_ptr = root->fs_info->last_alloc + empty_cluster;
2235 }
2236 }
Chris Mason239b14b2008-03-24 15:02:07 -04002237
2238 if (last_ptr) {
2239 if (*last_ptr)
2240 hint_byte = *last_ptr;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002241 else
Chris Mason239b14b2008-03-24 15:02:07 -04002242 empty_size += empty_cluster;
Chris Mason239b14b2008-03-24 15:02:07 -04002243 }
2244
Chris Masona061fc82008-05-07 11:43:44 -04002245 search_start = max(search_start, first_logical_byte(root, 0));
2246 orig_search_start = search_start;
2247
Chris Mason239b14b2008-03-24 15:02:07 -04002248 search_start = max(search_start, hint_byte);
Chris Mason6702ed42007-08-07 16:15:09 -04002249 total_needed += empty_size;
Chris Mason0b86a832008-03-24 15:01:56 -04002250
Josef Bacik0f9dd462008-09-23 13:14:11 -04002251new_group:
Josef Bacik45b8c9a2008-09-30 14:40:06 -04002252 block_group = btrfs_lookup_block_group(info, search_start);
2253 if (!block_group)
2254 block_group = btrfs_lookup_first_block_group(info,
2255 search_start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002256
2257 /*
2258 * Ok this looks a little tricky, buts its really simple. First if we
2259 * didn't find a block group obviously we want to start over.
2260 * Secondly, if the block group we found does not match the type we
2261 * need, and we have a last_ptr and its not 0, chances are the last
2262 * allocation we made was at the end of the block group, so lets go
2263 * ahead and skip the looking through the rest of the block groups and
2264 * start at the beginning. This helps with metadata allocations,
2265 * since you are likely to have a bunch of data block groups to search
2266 * through first before you realize that you need to start over, so go
2267 * ahead and start over and save the time.
2268 */
2269 if (!block_group || (!block_group_bits(block_group, data) &&
2270 last_ptr && *last_ptr)) {
2271 if (search_start != orig_search_start) {
Josef Bacik45b8c9a2008-09-30 14:40:06 -04002272 if (last_ptr && *last_ptr) {
2273 total_needed += empty_cluster;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002274 *last_ptr = 0;
Josef Bacik45b8c9a2008-09-30 14:40:06 -04002275 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002276 search_start = orig_search_start;
2277 goto new_group;
2278 } else if (!chunk_alloc_done && allowed_chunk_alloc) {
2279 ret = do_chunk_alloc(trans, root,
2280 num_bytes + 2 * 1024 * 1024,
2281 data, 1);
Zheng Yane8569812008-09-26 10:05:48 -04002282 if (ret < 0)
Josef Bacik0f9dd462008-09-23 13:14:11 -04002283 goto error;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002284 BUG_ON(ret);
2285 chunk_alloc_done = 1;
2286 search_start = orig_search_start;
2287 goto new_group;
2288 } else {
2289 ret = -ENOSPC;
2290 goto error;
Chris Mason0ef3e662008-05-24 14:04:53 -04002291 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002292 }
2293
2294 /*
2295 * this is going to seach through all of the existing block groups it
2296 * can find, so if we don't find something we need to see if we can
2297 * allocate what we need.
2298 */
2299 ret = find_free_space(root, &block_group, &search_start,
2300 total_needed, data);
2301 if (ret == -ENOSPC) {
2302 /*
2303 * instead of allocating, start at the original search start
2304 * and see if there is something to be found, if not then we
2305 * allocate
2306 */
2307 if (search_start != orig_search_start) {
2308 if (last_ptr && *last_ptr) {
2309 *last_ptr = 0;
2310 total_needed += empty_cluster;
2311 }
2312 search_start = orig_search_start;
2313 goto new_group;
2314 }
2315
2316 /*
2317 * we've already allocated, we're pretty screwed
2318 */
2319 if (chunk_alloc_done) {
2320 goto error;
2321 } else if (!allowed_chunk_alloc && block_group &&
2322 block_group_bits(block_group, data)) {
2323 block_group->space_info->force_alloc = 1;
2324 goto error;
2325 } else if (!allowed_chunk_alloc) {
2326 goto error;
2327 }
2328
2329 ret = do_chunk_alloc(trans, root, num_bytes + 2 * 1024 * 1024,
2330 data, 1);
2331 if (ret < 0)
2332 goto error;
2333
2334 BUG_ON(ret);
Chris Mason0ef3e662008-05-24 14:04:53 -04002335 chunk_alloc_done = 1;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002336 if (block_group)
2337 search_start = block_group->key.objectid +
2338 block_group->key.offset;
2339 else
2340 search_start = orig_search_start;
2341 goto new_group;
Chris Mason0ef3e662008-05-24 14:04:53 -04002342 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002343
Chris Mason0b86a832008-03-24 15:01:56 -04002344 if (ret)
2345 goto error;
2346
Chris Mason87ee04e2007-11-30 11:30:34 -05002347 search_start = stripe_align(root, search_start);
Chris Masonfec577f2007-02-26 10:40:21 -05002348 ins->objectid = search_start;
Chris Mason0b86a832008-03-24 15:01:56 -04002349 ins->offset = num_bytes;
Chris Masone37c9e62007-05-09 20:13:14 -04002350
Josef Bacik0f9dd462008-09-23 13:14:11 -04002351 if (ins->objectid + num_bytes >= search_end) {
2352 search_start = orig_search_start;
2353 if (chunk_alloc_done) {
2354 ret = -ENOSPC;
2355 goto error;
2356 }
2357 goto new_group;
2358 }
Chris Mason0b86a832008-03-24 15:01:56 -04002359
2360 if (ins->objectid + num_bytes >
2361 block_group->key.objectid + block_group->key.offset) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04002362 if (search_start == orig_search_start && chunk_alloc_done) {
2363 ret = -ENOSPC;
2364 goto error;
2365 }
Chris Masone19caa52007-10-15 16:17:44 -04002366 search_start = block_group->key.objectid +
2367 block_group->key.offset;
2368 goto new_group;
2369 }
Chris Mason0b86a832008-03-24 15:01:56 -04002370
Chris Masondb945352007-10-15 16:15:53 -04002371 if (exclude_nr > 0 && (ins->objectid + num_bytes > exclude_start &&
Chris Masonf2654de2007-06-26 12:20:46 -04002372 ins->objectid < exclude_start + exclude_nr)) {
2373 search_start = exclude_start + exclude_nr;
2374 goto new_group;
2375 }
Chris Mason0b86a832008-03-24 15:01:56 -04002376
Josef Bacik0f9dd462008-09-23 13:14:11 -04002377 if (!(data & BTRFS_BLOCK_GROUP_DATA))
2378 trans->block_group = block_group;
2379
Chris Masondb945352007-10-15 16:15:53 -04002380 ins->offset = num_bytes;
Chris Mason239b14b2008-03-24 15:02:07 -04002381 if (last_ptr) {
2382 *last_ptr = ins->objectid + ins->offset;
2383 if (*last_ptr ==
Josef Bacik0f9dd462008-09-23 13:14:11 -04002384 btrfs_super_total_bytes(&root->fs_info->super_copy))
Chris Mason239b14b2008-03-24 15:02:07 -04002385 *last_ptr = 0;
Chris Mason239b14b2008-03-24 15:02:07 -04002386 }
Chris Masonbe744172007-05-06 10:15:01 -04002387
Josef Bacik0f9dd462008-09-23 13:14:11 -04002388 ret = 0;
Chris Mason0f70abe2007-02-28 16:46:22 -05002389error:
Chris Mason0f70abe2007-02-28 16:46:22 -05002390 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05002391}
Chris Masonec44a352008-04-28 15:29:52 -04002392
Josef Bacik0f9dd462008-09-23 13:14:11 -04002393static void dump_space_info(struct btrfs_space_info *info, u64 bytes)
2394{
2395 struct btrfs_block_group_cache *cache;
2396 struct list_head *l;
2397
2398 printk(KERN_INFO "space_info has %Lu free, is %sfull\n",
Zheng Yane8569812008-09-26 10:05:48 -04002399 info->total_bytes - info->bytes_used - info->bytes_pinned -
2400 info->bytes_reserved, (info->full) ? "" : "not ");
Josef Bacik0f9dd462008-09-23 13:14:11 -04002401
2402 spin_lock(&info->lock);
2403 list_for_each(l, &info->block_groups) {
2404 cache = list_entry(l, struct btrfs_block_group_cache, list);
2405 spin_lock(&cache->lock);
2406 printk(KERN_INFO "block group %Lu has %Lu bytes, %Lu used "
Zheng Yane8569812008-09-26 10:05:48 -04002407 "%Lu pinned %Lu reserved\n",
Josef Bacik0f9dd462008-09-23 13:14:11 -04002408 cache->key.objectid, cache->key.offset,
Zheng Yane8569812008-09-26 10:05:48 -04002409 btrfs_block_group_used(&cache->item),
2410 cache->pinned, cache->reserved);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002411 btrfs_dump_free_space(cache, bytes);
2412 spin_unlock(&cache->lock);
2413 }
2414 spin_unlock(&info->lock);
2415}
Zheng Yane8569812008-09-26 10:05:48 -04002416
Chris Masone6dcd2d2008-07-17 12:53:50 -04002417static int __btrfs_reserve_extent(struct btrfs_trans_handle *trans,
2418 struct btrfs_root *root,
2419 u64 num_bytes, u64 min_alloc_size,
2420 u64 empty_size, u64 hint_byte,
2421 u64 search_end, struct btrfs_key *ins,
2422 u64 data)
Chris Masonfec577f2007-02-26 10:40:21 -05002423{
2424 int ret;
Chris Masonfbdc7622007-05-30 10:22:12 -04002425 u64 search_start = 0;
Chris Mason8790d502008-04-03 16:29:03 -04002426 u64 alloc_profile;
Chris Mason1261ec42007-03-20 20:35:03 -04002427 struct btrfs_fs_info *info = root->fs_info;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002428 struct btrfs_block_group_cache *cache;
Chris Mason925baed2008-06-25 16:01:30 -04002429
Chris Mason6324fbf2008-03-24 15:01:59 -04002430 if (data) {
Chris Mason8790d502008-04-03 16:29:03 -04002431 alloc_profile = info->avail_data_alloc_bits &
2432 info->data_alloc_profile;
2433 data = BTRFS_BLOCK_GROUP_DATA | alloc_profile;
Chris Mason6324fbf2008-03-24 15:01:59 -04002434 } else if (root == root->fs_info->chunk_root) {
Chris Mason8790d502008-04-03 16:29:03 -04002435 alloc_profile = info->avail_system_alloc_bits &
2436 info->system_alloc_profile;
2437 data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile;
Chris Mason6324fbf2008-03-24 15:01:59 -04002438 } else {
Chris Mason8790d502008-04-03 16:29:03 -04002439 alloc_profile = info->avail_metadata_alloc_bits &
2440 info->metadata_alloc_profile;
2441 data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile;
Chris Mason6324fbf2008-03-24 15:01:59 -04002442 }
Chris Mason98d20f62008-04-14 09:46:10 -04002443again:
Chris Masona061fc82008-05-07 11:43:44 -04002444 data = reduce_alloc_profile(root, data);
Chris Mason0ef3e662008-05-24 14:04:53 -04002445 /*
2446 * the only place that sets empty_size is btrfs_realloc_node, which
2447 * is not called recursively on allocations
2448 */
2449 if (empty_size || root->ref_cows) {
Chris Mason593060d2008-03-25 16:50:33 -04002450 if (!(data & BTRFS_BLOCK_GROUP_METADATA)) {
Chris Mason6324fbf2008-03-24 15:01:59 -04002451 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0ef3e662008-05-24 14:04:53 -04002452 2 * 1024 * 1024,
2453 BTRFS_BLOCK_GROUP_METADATA |
2454 (info->metadata_alloc_profile &
2455 info->avail_metadata_alloc_bits), 0);
Chris Mason6324fbf2008-03-24 15:01:59 -04002456 }
2457 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0ef3e662008-05-24 14:04:53 -04002458 num_bytes + 2 * 1024 * 1024, data, 0);
Chris Mason6324fbf2008-03-24 15:01:59 -04002459 }
Chris Mason0b86a832008-03-24 15:01:56 -04002460
Chris Masondb945352007-10-15 16:15:53 -04002461 WARN_ON(num_bytes < root->sectorsize);
2462 ret = find_free_extent(trans, root, num_bytes, empty_size,
2463 search_start, search_end, hint_byte, ins,
Chris Mason26b80032007-08-08 20:17:12 -04002464 trans->alloc_exclude_start,
2465 trans->alloc_exclude_nr, data);
Chris Mason3b951512008-04-17 11:29:12 -04002466
Chris Mason98d20f62008-04-14 09:46:10 -04002467 if (ret == -ENOSPC && num_bytes > min_alloc_size) {
2468 num_bytes = num_bytes >> 1;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002469 num_bytes = num_bytes & ~(root->sectorsize - 1);
Chris Mason98d20f62008-04-14 09:46:10 -04002470 num_bytes = max(num_bytes, min_alloc_size);
Chris Mason0ef3e662008-05-24 14:04:53 -04002471 do_chunk_alloc(trans, root->fs_info->extent_root,
2472 num_bytes, data, 1);
Chris Mason98d20f62008-04-14 09:46:10 -04002473 goto again;
2474 }
Chris Masonec44a352008-04-28 15:29:52 -04002475 if (ret) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04002476 struct btrfs_space_info *sinfo;
2477
2478 sinfo = __find_space_info(root->fs_info, data);
2479 printk("allocation failed flags %Lu, wanted %Lu\n",
2480 data, num_bytes);
2481 dump_space_info(sinfo, num_bytes);
Chris Mason925baed2008-06-25 16:01:30 -04002482 BUG();
Chris Mason925baed2008-06-25 16:01:30 -04002483 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002484 cache = btrfs_lookup_block_group(root->fs_info, ins->objectid);
2485 if (!cache) {
2486 printk(KERN_ERR "Unable to find block group for %Lu\n", ins->objectid);
2487 return -ENOSPC;
2488 }
2489
2490 ret = btrfs_remove_free_space(cache, ins->objectid, ins->offset);
2491
2492 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002493}
2494
Chris Mason65b51a02008-08-01 15:11:20 -04002495int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len)
2496{
Josef Bacik0f9dd462008-09-23 13:14:11 -04002497 struct btrfs_block_group_cache *cache;
2498
Chris Mason65b51a02008-08-01 15:11:20 -04002499 maybe_lock_mutex(root);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002500 cache = btrfs_lookup_block_group(root->fs_info, start);
2501 if (!cache) {
2502 printk(KERN_ERR "Unable to find block group for %Lu\n", start);
2503 maybe_unlock_mutex(root);
2504 return -ENOSPC;
2505 }
2506 btrfs_add_free_space(cache, start, len);
Zheng Yan1a40e232008-09-26 10:09:34 -04002507 update_reserved_extents(root, start, len, 0);
Chris Mason65b51a02008-08-01 15:11:20 -04002508 maybe_unlock_mutex(root);
2509 return 0;
2510}
2511
Chris Masone6dcd2d2008-07-17 12:53:50 -04002512int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
2513 struct btrfs_root *root,
2514 u64 num_bytes, u64 min_alloc_size,
2515 u64 empty_size, u64 hint_byte,
2516 u64 search_end, struct btrfs_key *ins,
2517 u64 data)
2518{
2519 int ret;
2520 maybe_lock_mutex(root);
2521 ret = __btrfs_reserve_extent(trans, root, num_bytes, min_alloc_size,
2522 empty_size, hint_byte, search_end, ins,
2523 data);
Zheng Yane8569812008-09-26 10:05:48 -04002524 update_reserved_extents(root, ins->objectid, ins->offset, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002525 maybe_unlock_mutex(root);
2526 return ret;
2527}
2528
2529static int __btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04002530 struct btrfs_root *root, u64 parent,
Chris Masone6dcd2d2008-07-17 12:53:50 -04002531 u64 root_objectid, u64 ref_generation,
2532 u64 owner, u64 owner_offset,
2533 struct btrfs_key *ins)
2534{
2535 int ret;
2536 int pending_ret;
2537 u64 super_used;
2538 u64 root_used;
2539 u64 num_bytes = ins->offset;
2540 u32 sizes[2];
2541 struct btrfs_fs_info *info = root->fs_info;
2542 struct btrfs_root *extent_root = info->extent_root;
2543 struct btrfs_extent_item *extent_item;
2544 struct btrfs_extent_ref *ref;
2545 struct btrfs_path *path;
2546 struct btrfs_key keys[2];
Chris Masonf2654de2007-06-26 12:20:46 -04002547
Zheng Yan31840ae2008-09-23 13:14:14 -04002548 if (parent == 0)
2549 parent = ins->objectid;
2550
Josef Bacik58176a92007-08-29 15:47:34 -04002551 /* block accounting for super block */
Chris Masona2135012008-06-25 16:01:30 -04002552 spin_lock_irq(&info->delalloc_lock);
Chris Masondb945352007-10-15 16:15:53 -04002553 super_used = btrfs_super_bytes_used(&info->super_copy);
2554 btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes);
Chris Masona2135012008-06-25 16:01:30 -04002555 spin_unlock_irq(&info->delalloc_lock);
Chris Mason26b80032007-08-08 20:17:12 -04002556
Josef Bacik58176a92007-08-29 15:47:34 -04002557 /* block accounting for root item */
Chris Masondb945352007-10-15 16:15:53 -04002558 root_used = btrfs_root_used(&root->root_item);
2559 btrfs_set_root_used(&root->root_item, root_used + num_bytes);
Josef Bacik58176a92007-08-29 15:47:34 -04002560
Chris Mason26b80032007-08-08 20:17:12 -04002561 if (root == extent_root) {
Zheng Yan31840ae2008-09-23 13:14:14 -04002562 struct pending_extent_op *extent_op;
2563
2564 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
2565 BUG_ON(!extent_op);
2566
2567 extent_op->type = PENDING_EXTENT_INSERT;
2568 extent_op->bytenr = ins->objectid;
2569 extent_op->num_bytes = ins->offset;
2570 extent_op->parent = parent;
2571 extent_op->orig_parent = 0;
2572 extent_op->generation = ref_generation;
2573 extent_op->orig_generation = 0;
2574 extent_op->level = (int)owner;
2575
Chris Mason1a5bc162007-10-15 16:15:26 -04002576 set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
2577 ins->objectid + ins->offset - 1,
2578 EXTENT_LOCKED, GFP_NOFS);
Zheng Yan31840ae2008-09-23 13:14:14 -04002579 set_state_private(&root->fs_info->extent_ins,
2580 ins->objectid, (unsigned long)extent_op);
Chris Mason26b80032007-08-08 20:17:12 -04002581 goto update_block;
2582 }
2583
Chris Mason47e4bb92008-02-01 14:51:59 -05002584 memcpy(&keys[0], ins, sizeof(*ins));
Chris Mason47e4bb92008-02-01 14:51:59 -05002585 keys[1].objectid = ins->objectid;
2586 keys[1].type = BTRFS_EXTENT_REF_KEY;
Zheng Yan31840ae2008-09-23 13:14:14 -04002587 keys[1].offset = parent;
Chris Mason47e4bb92008-02-01 14:51:59 -05002588 sizes[0] = sizeof(*extent_item);
2589 sizes[1] = sizeof(*ref);
Chris Mason7bb86312007-12-11 09:25:06 -05002590
2591 path = btrfs_alloc_path();
2592 BUG_ON(!path);
Chris Mason47e4bb92008-02-01 14:51:59 -05002593
2594 ret = btrfs_insert_empty_items(trans, extent_root, path, keys,
2595 sizes, 2);
Chris Masonccd467d2007-06-28 15:57:36 -04002596 BUG_ON(ret);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002597
Chris Mason47e4bb92008-02-01 14:51:59 -05002598 extent_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2599 struct btrfs_extent_item);
2600 btrfs_set_extent_refs(path->nodes[0], extent_item, 1);
2601 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
2602 struct btrfs_extent_ref);
2603
2604 btrfs_set_ref_root(path->nodes[0], ref, root_objectid);
2605 btrfs_set_ref_generation(path->nodes[0], ref, ref_generation);
2606 btrfs_set_ref_objectid(path->nodes[0], ref, owner);
2607 btrfs_set_ref_offset(path->nodes[0], ref, owner_offset);
Zheng Yan31840ae2008-09-23 13:14:14 -04002608 btrfs_set_ref_num_refs(path->nodes[0], ref, 1);
Chris Mason47e4bb92008-02-01 14:51:59 -05002609
2610 btrfs_mark_buffer_dirty(path->nodes[0]);
2611
2612 trans->alloc_exclude_start = 0;
2613 trans->alloc_exclude_nr = 0;
Chris Mason7bb86312007-12-11 09:25:06 -05002614 btrfs_free_path(path);
Chris Masone089f052007-03-16 16:20:31 -04002615 finish_current_insert(trans, extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -04002616 pending_ret = del_pending_extents(trans, extent_root);
Chris Masonf510cfe2007-10-15 16:14:48 -04002617
Chris Mason925baed2008-06-25 16:01:30 -04002618 if (ret)
2619 goto out;
Chris Masone37c9e62007-05-09 20:13:14 -04002620 if (pending_ret) {
Chris Mason925baed2008-06-25 16:01:30 -04002621 ret = pending_ret;
2622 goto out;
Chris Masone37c9e62007-05-09 20:13:14 -04002623 }
Chris Mason26b80032007-08-08 20:17:12 -04002624
2625update_block:
Chris Mason0b86a832008-03-24 15:01:56 -04002626 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0);
Chris Masonf5947062008-02-04 10:10:13 -05002627 if (ret) {
2628 printk("update block group failed for %Lu %Lu\n",
2629 ins->objectid, ins->offset);
2630 BUG();
2631 }
Chris Mason925baed2008-06-25 16:01:30 -04002632out:
Chris Masone6dcd2d2008-07-17 12:53:50 -04002633 return ret;
2634}
2635
2636int btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04002637 struct btrfs_root *root, u64 parent,
Chris Masone6dcd2d2008-07-17 12:53:50 -04002638 u64 root_objectid, u64 ref_generation,
2639 u64 owner, u64 owner_offset,
2640 struct btrfs_key *ins)
2641{
2642 int ret;
Chris Mason1c2308f2008-09-23 13:14:13 -04002643
2644 if (root_objectid == BTRFS_TREE_LOG_OBJECTID)
2645 return 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002646 maybe_lock_mutex(root);
Zheng Yan31840ae2008-09-23 13:14:14 -04002647 ret = __btrfs_alloc_reserved_extent(trans, root, parent,
2648 root_objectid, ref_generation,
2649 owner, owner_offset, ins);
Zheng Yane8569812008-09-26 10:05:48 -04002650 update_reserved_extents(root, ins->objectid, ins->offset, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002651 maybe_unlock_mutex(root);
2652 return ret;
2653}
Chris Masone02119d2008-09-05 16:13:11 -04002654
2655/*
2656 * this is used by the tree logging recovery code. It records that
2657 * an extent has been allocated and makes sure to clear the free
2658 * space cache bits as well
2659 */
2660int btrfs_alloc_logged_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04002661 struct btrfs_root *root, u64 parent,
Chris Masone02119d2008-09-05 16:13:11 -04002662 u64 root_objectid, u64 ref_generation,
2663 u64 owner, u64 owner_offset,
2664 struct btrfs_key *ins)
2665{
2666 int ret;
2667 struct btrfs_block_group_cache *block_group;
2668
2669 maybe_lock_mutex(root);
2670 block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
2671 cache_block_group(root, block_group);
2672
Josef Bacik0f9dd462008-09-23 13:14:11 -04002673 ret = btrfs_remove_free_space(block_group, ins->objectid, ins->offset);
2674 BUG_ON(ret);
Zheng Yan31840ae2008-09-23 13:14:14 -04002675 ret = __btrfs_alloc_reserved_extent(trans, root, parent,
2676 root_objectid, ref_generation,
2677 owner, owner_offset, ins);
Chris Masone02119d2008-09-05 16:13:11 -04002678 maybe_unlock_mutex(root);
2679 return ret;
2680}
2681
Chris Masone6dcd2d2008-07-17 12:53:50 -04002682/*
2683 * finds a free extent and does all the dirty work required for allocation
2684 * returns the key for the extent through ins, and a tree buffer for
2685 * the first block of the extent through buf.
2686 *
2687 * returns 0 if everything worked, non-zero otherwise.
2688 */
2689int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
2690 struct btrfs_root *root,
Zheng Yan31840ae2008-09-23 13:14:14 -04002691 u64 num_bytes, u64 parent, u64 min_alloc_size,
Chris Masone6dcd2d2008-07-17 12:53:50 -04002692 u64 root_objectid, u64 ref_generation,
Zheng Yan31840ae2008-09-23 13:14:14 -04002693 u64 owner_objectid, u64 owner_offset,
Chris Masone6dcd2d2008-07-17 12:53:50 -04002694 u64 empty_size, u64 hint_byte,
2695 u64 search_end, struct btrfs_key *ins, u64 data)
2696{
2697 int ret;
2698
2699 maybe_lock_mutex(root);
2700
2701 ret = __btrfs_reserve_extent(trans, root, num_bytes,
2702 min_alloc_size, empty_size, hint_byte,
2703 search_end, ins, data);
2704 BUG_ON(ret);
Chris Masond00aff02008-09-11 15:54:42 -04002705 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
Zheng Yan31840ae2008-09-23 13:14:14 -04002706 ret = __btrfs_alloc_reserved_extent(trans, root, parent,
2707 root_objectid, ref_generation,
2708 owner_objectid, owner_offset, ins);
Chris Masond00aff02008-09-11 15:54:42 -04002709 BUG_ON(ret);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002710
Zheng Yane8569812008-09-26 10:05:48 -04002711 } else {
2712 update_reserved_extents(root, ins->objectid, ins->offset, 1);
Chris Masond00aff02008-09-11 15:54:42 -04002713 }
Chris Mason925baed2008-06-25 16:01:30 -04002714 maybe_unlock_mutex(root);
2715 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05002716}
Chris Mason65b51a02008-08-01 15:11:20 -04002717
2718struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
2719 struct btrfs_root *root,
2720 u64 bytenr, u32 blocksize)
2721{
2722 struct extent_buffer *buf;
2723
2724 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
2725 if (!buf)
2726 return ERR_PTR(-ENOMEM);
2727 btrfs_set_header_generation(buf, trans->transid);
2728 btrfs_tree_lock(buf);
2729 clean_tree_block(trans, root, buf);
2730 btrfs_set_buffer_uptodate(buf);
Chris Masond0c803c2008-09-11 16:17:57 -04002731 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
2732 set_extent_dirty(&root->dirty_log_pages, buf->start,
Chris Mason65b51a02008-08-01 15:11:20 -04002733 buf->start + buf->len - 1, GFP_NOFS);
Chris Masond0c803c2008-09-11 16:17:57 -04002734 } else {
2735 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
2736 buf->start + buf->len - 1, GFP_NOFS);
2737 }
Chris Mason65b51a02008-08-01 15:11:20 -04002738 trans->blocks_used++;
2739 return buf;
2740}
2741
Chris Masonfec577f2007-02-26 10:40:21 -05002742/*
2743 * helper function to allocate a block for a given tree
2744 * returns the tree buffer or NULL.
2745 */
Chris Mason5f39d392007-10-15 16:14:19 -04002746struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Chris Masondb945352007-10-15 16:15:53 -04002747 struct btrfs_root *root,
Zheng Yan31840ae2008-09-23 13:14:14 -04002748 u32 blocksize, u64 parent,
Chris Mason7bb86312007-12-11 09:25:06 -05002749 u64 root_objectid,
2750 u64 ref_generation,
Chris Mason7bb86312007-12-11 09:25:06 -05002751 int level,
2752 u64 hint,
Chris Mason5f39d392007-10-15 16:14:19 -04002753 u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05002754{
Chris Masone2fa7222007-03-12 16:22:34 -04002755 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -05002756 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04002757 struct extent_buffer *buf;
Chris Masonfec577f2007-02-26 10:40:21 -05002758
Zheng Yan31840ae2008-09-23 13:14:14 -04002759 ret = btrfs_alloc_extent(trans, root, blocksize, parent, blocksize,
2760 root_objectid, ref_generation, level, 0,
2761 empty_size, hint, (u64)-1, &ins, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05002762 if (ret) {
Chris Mason54aa1f42007-06-22 14:16:25 -04002763 BUG_ON(ret > 0);
2764 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05002765 }
Chris Mason55c69072008-01-09 15:55:33 -05002766
Chris Mason65b51a02008-08-01 15:11:20 -04002767 buf = btrfs_init_new_buffer(trans, root, ins.objectid, blocksize);
Chris Masonfec577f2007-02-26 10:40:21 -05002768 return buf;
2769}
Chris Masona28ec192007-03-06 20:08:01 -05002770
Chris Masone02119d2008-09-05 16:13:11 -04002771int btrfs_drop_leaf_ref(struct btrfs_trans_handle *trans,
2772 struct btrfs_root *root, struct extent_buffer *leaf)
Chris Mason6407bf62007-03-27 06:33:00 -04002773{
Chris Mason7bb86312007-12-11 09:25:06 -05002774 u64 leaf_owner;
2775 u64 leaf_generation;
Chris Mason5f39d392007-10-15 16:14:19 -04002776 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -04002777 struct btrfs_file_extent_item *fi;
2778 int i;
2779 int nritems;
2780 int ret;
2781
Chris Mason5f39d392007-10-15 16:14:19 -04002782 BUG_ON(!btrfs_is_leaf(leaf));
2783 nritems = btrfs_header_nritems(leaf);
Chris Mason7bb86312007-12-11 09:25:06 -05002784 leaf_owner = btrfs_header_owner(leaf);
2785 leaf_generation = btrfs_header_generation(leaf);
2786
Chris Mason6407bf62007-03-27 06:33:00 -04002787 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04002788 u64 disk_bytenr;
Chris Masone34a5b42008-07-22 12:08:37 -04002789 cond_resched();
Chris Mason5f39d392007-10-15 16:14:19 -04002790
2791 btrfs_item_key_to_cpu(leaf, &key, i);
2792 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason6407bf62007-03-27 06:33:00 -04002793 continue;
2794 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002795 if (btrfs_file_extent_type(leaf, fi) ==
2796 BTRFS_FILE_EXTENT_INLINE)
Chris Mason236454df2007-04-19 13:37:44 -04002797 continue;
Chris Mason6407bf62007-03-27 06:33:00 -04002798 /*
2799 * FIXME make sure to insert a trans record that
2800 * repeats the snapshot del on crash
2801 */
Chris Masondb945352007-10-15 16:15:53 -04002802 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
2803 if (disk_bytenr == 0)
Chris Mason3a686372007-05-24 13:35:57 -04002804 continue;
Chris Mason4a096752008-07-21 10:29:44 -04002805
2806 mutex_lock(&root->fs_info->alloc_mutex);
Chris Mason925baed2008-06-25 16:01:30 -04002807 ret = __btrfs_free_extent(trans, root, disk_bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -05002808 btrfs_file_extent_disk_num_bytes(leaf, fi),
Zheng Yan31840ae2008-09-23 13:14:14 -04002809 leaf->start, leaf_owner, leaf_generation,
Chris Mason7bb86312007-12-11 09:25:06 -05002810 key.objectid, key.offset, 0);
Chris Mason4a096752008-07-21 10:29:44 -04002811 mutex_unlock(&root->fs_info->alloc_mutex);
Zheng Yan31840ae2008-09-23 13:14:14 -04002812 BUG_ON(ret);
Chris Mason2dd3e672008-08-04 08:20:15 -04002813
2814 atomic_inc(&root->fs_info->throttle_gen);
2815 wake_up(&root->fs_info->transaction_throttle);
2816 cond_resched();
Chris Mason6407bf62007-03-27 06:33:00 -04002817 }
2818 return 0;
2819}
2820
Chris Masone02119d2008-09-05 16:13:11 -04002821static int noinline cache_drop_leaf_ref(struct btrfs_trans_handle *trans,
2822 struct btrfs_root *root,
2823 struct btrfs_leaf_ref *ref)
Yan Zheng31153d82008-07-28 15:32:19 -04002824{
2825 int i;
2826 int ret;
2827 struct btrfs_extent_info *info = ref->extents;
2828
Yan Zheng31153d82008-07-28 15:32:19 -04002829 for (i = 0; i < ref->nritems; i++) {
2830 mutex_lock(&root->fs_info->alloc_mutex);
Zheng Yan31840ae2008-09-23 13:14:14 -04002831 ret = __btrfs_free_extent(trans, root, info->bytenr,
2832 info->num_bytes, ref->bytenr,
2833 ref->owner, ref->generation,
2834 info->objectid, info->offset, 0);
Yan Zheng31153d82008-07-28 15:32:19 -04002835 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Mason2dd3e672008-08-04 08:20:15 -04002836
2837 atomic_inc(&root->fs_info->throttle_gen);
2838 wake_up(&root->fs_info->transaction_throttle);
2839 cond_resched();
2840
Yan Zheng31153d82008-07-28 15:32:19 -04002841 BUG_ON(ret);
2842 info++;
2843 }
Yan Zheng31153d82008-07-28 15:32:19 -04002844
2845 return 0;
2846}
2847
Chris Mason333db942008-06-25 16:01:30 -04002848int drop_snap_lookup_refcount(struct btrfs_root *root, u64 start, u64 len,
2849 u32 *refs)
2850{
Chris Mason017e5362008-07-28 15:32:51 -04002851 int ret;
Chris Masonf87f0572008-08-01 11:27:23 -04002852
Zheng Yan31840ae2008-09-23 13:14:14 -04002853 ret = btrfs_lookup_extent_ref(NULL, root, start, len, refs);
Chris Masonf87f0572008-08-01 11:27:23 -04002854 BUG_ON(ret);
2855
2856#if 0 // some debugging code in case we see problems here
2857 /* if the refs count is one, it won't get increased again. But
2858 * if the ref count is > 1, someone may be decreasing it at
2859 * the same time we are.
2860 */
2861 if (*refs != 1) {
2862 struct extent_buffer *eb = NULL;
2863 eb = btrfs_find_create_tree_block(root, start, len);
2864 if (eb)
2865 btrfs_tree_lock(eb);
2866
2867 mutex_lock(&root->fs_info->alloc_mutex);
2868 ret = lookup_extent_ref(NULL, root, start, len, refs);
2869 BUG_ON(ret);
2870 mutex_unlock(&root->fs_info->alloc_mutex);
2871
2872 if (eb) {
2873 btrfs_tree_unlock(eb);
2874 free_extent_buffer(eb);
2875 }
2876 if (*refs == 1) {
2877 printk("block %llu went down to one during drop_snap\n",
2878 (unsigned long long)start);
2879 }
2880
2881 }
2882#endif
2883
Chris Masone7a84562008-06-25 16:01:31 -04002884 cond_resched();
Chris Mason017e5362008-07-28 15:32:51 -04002885 return ret;
Chris Mason333db942008-06-25 16:01:30 -04002886}
2887
2888/*
Chris Mason9aca1d52007-03-13 11:09:37 -04002889 * helper function for drop_snapshot, this walks down the tree dropping ref
2890 * counts as it goes.
2891 */
Chris Mason98ed5172008-01-03 10:01:48 -05002892static int noinline walk_down_tree(struct btrfs_trans_handle *trans,
2893 struct btrfs_root *root,
2894 struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05002895{
Chris Mason7bb86312007-12-11 09:25:06 -05002896 u64 root_owner;
2897 u64 root_gen;
2898 u64 bytenr;
Chris Masonca7a79a2008-05-12 12:59:19 -04002899 u64 ptr_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04002900 struct extent_buffer *next;
2901 struct extent_buffer *cur;
Chris Mason7bb86312007-12-11 09:25:06 -05002902 struct extent_buffer *parent;
Yan Zheng31153d82008-07-28 15:32:19 -04002903 struct btrfs_leaf_ref *ref;
Chris Masondb945352007-10-15 16:15:53 -04002904 u32 blocksize;
Chris Mason20524f02007-03-10 06:35:47 -05002905 int ret;
2906 u32 refs;
2907
Chris Mason5caf2a02007-04-02 11:20:42 -04002908 WARN_ON(*level < 0);
2909 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason333db942008-06-25 16:01:30 -04002910 ret = drop_snap_lookup_refcount(root, path->nodes[*level]->start,
Chris Masondb945352007-10-15 16:15:53 -04002911 path->nodes[*level]->len, &refs);
Chris Mason20524f02007-03-10 06:35:47 -05002912 BUG_ON(ret);
2913 if (refs > 1)
2914 goto out;
Chris Masone0115992007-06-19 16:23:05 -04002915
Chris Mason9aca1d52007-03-13 11:09:37 -04002916 /*
2917 * walk down to the last node level and free all the leaves
2918 */
Chris Mason6407bf62007-03-27 06:33:00 -04002919 while(*level >= 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -04002920 WARN_ON(*level < 0);
2921 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason20524f02007-03-10 06:35:47 -05002922 cur = path->nodes[*level];
Chris Masone0115992007-06-19 16:23:05 -04002923
Chris Mason5f39d392007-10-15 16:14:19 -04002924 if (btrfs_header_level(cur) != *level)
Chris Mason2c90e5d2007-04-02 10:50:19 -04002925 WARN_ON(1);
Chris Masone0115992007-06-19 16:23:05 -04002926
Chris Mason7518a232007-03-12 12:01:18 -04002927 if (path->slots[*level] >=
Chris Mason5f39d392007-10-15 16:14:19 -04002928 btrfs_header_nritems(cur))
Chris Mason20524f02007-03-10 06:35:47 -05002929 break;
Chris Mason6407bf62007-03-27 06:33:00 -04002930 if (*level == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002931 ret = btrfs_drop_leaf_ref(trans, root, cur);
Chris Mason6407bf62007-03-27 06:33:00 -04002932 BUG_ON(ret);
2933 break;
2934 }
Chris Masondb945352007-10-15 16:15:53 -04002935 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
Chris Masonca7a79a2008-05-12 12:59:19 -04002936 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
Chris Masondb945352007-10-15 16:15:53 -04002937 blocksize = btrfs_level_size(root, *level - 1);
Chris Mason925baed2008-06-25 16:01:30 -04002938
Chris Mason333db942008-06-25 16:01:30 -04002939 ret = drop_snap_lookup_refcount(root, bytenr, blocksize, &refs);
Chris Mason6407bf62007-03-27 06:33:00 -04002940 BUG_ON(ret);
2941 if (refs != 1) {
Chris Mason7bb86312007-12-11 09:25:06 -05002942 parent = path->nodes[*level];
2943 root_owner = btrfs_header_owner(parent);
2944 root_gen = btrfs_header_generation(parent);
Chris Mason20524f02007-03-10 06:35:47 -05002945 path->slots[*level]++;
Chris Masonf87f0572008-08-01 11:27:23 -04002946
2947 mutex_lock(&root->fs_info->alloc_mutex);
Chris Mason925baed2008-06-25 16:01:30 -04002948 ret = __btrfs_free_extent(trans, root, bytenr,
Zheng Yan31840ae2008-09-23 13:14:14 -04002949 blocksize, parent->start,
2950 root_owner, root_gen, 0, 0, 1);
Chris Mason20524f02007-03-10 06:35:47 -05002951 BUG_ON(ret);
Chris Masonf87f0572008-08-01 11:27:23 -04002952 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Mason18e35e0a2008-08-01 13:11:41 -04002953
2954 atomic_inc(&root->fs_info->throttle_gen);
2955 wake_up(&root->fs_info->transaction_throttle);
Chris Mason2dd3e672008-08-04 08:20:15 -04002956 cond_resched();
Chris Mason18e35e0a2008-08-01 13:11:41 -04002957
Chris Mason20524f02007-03-10 06:35:47 -05002958 continue;
2959 }
Chris Masonf87f0572008-08-01 11:27:23 -04002960 /*
2961 * at this point, we have a single ref, and since the
2962 * only place referencing this extent is a dead root
2963 * the reference count should never go higher.
2964 * So, we don't need to check it again
2965 */
Yan Zheng31153d82008-07-28 15:32:19 -04002966 if (*level == 1) {
Chris Mason017e5362008-07-28 15:32:51 -04002967 ref = btrfs_lookup_leaf_ref(root, bytenr);
Zheng Yan1a40e232008-09-26 10:09:34 -04002968 if (ref && ref->generation != ptr_gen) {
2969 btrfs_free_leaf_ref(root, ref);
2970 ref = NULL;
2971 }
Yan Zheng31153d82008-07-28 15:32:19 -04002972 if (ref) {
Chris Masone02119d2008-09-05 16:13:11 -04002973 ret = cache_drop_leaf_ref(trans, root, ref);
Yan Zheng31153d82008-07-28 15:32:19 -04002974 BUG_ON(ret);
2975 btrfs_remove_leaf_ref(root, ref);
Yanbcc63ab2008-07-30 16:29:20 -04002976 btrfs_free_leaf_ref(root, ref);
Yan Zheng31153d82008-07-28 15:32:19 -04002977 *level = 0;
2978 break;
2979 }
Chris Mason37d1aee2008-07-31 10:48:37 -04002980 if (printk_ratelimit())
2981 printk("leaf ref miss for bytenr %llu\n",
2982 (unsigned long long)bytenr);
Yan Zheng31153d82008-07-28 15:32:19 -04002983 }
Chris Masondb945352007-10-15 16:15:53 -04002984 next = btrfs_find_tree_block(root, bytenr, blocksize);
Chris Mason1259ab72008-05-12 13:39:03 -04002985 if (!next || !btrfs_buffer_uptodate(next, ptr_gen)) {
Chris Mason5f39d392007-10-15 16:14:19 -04002986 free_extent_buffer(next);
Chris Mason333db942008-06-25 16:01:30 -04002987
Chris Masonca7a79a2008-05-12 12:59:19 -04002988 next = read_tree_block(root, bytenr, blocksize,
2989 ptr_gen);
Chris Masone7a84562008-06-25 16:01:31 -04002990 cond_resched();
Chris Masonf87f0572008-08-01 11:27:23 -04002991#if 0
2992 /*
2993 * this is a debugging check and can go away
2994 * the ref should never go all the way down to 1
2995 * at this point
2996 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04002997 ret = lookup_extent_ref(NULL, root, bytenr, blocksize,
2998 &refs);
Chris Masone9d0b132007-08-10 14:06:19 -04002999 BUG_ON(ret);
Chris Masonf87f0572008-08-01 11:27:23 -04003000 WARN_ON(refs != 1);
3001#endif
Chris Masone9d0b132007-08-10 14:06:19 -04003002 }
Chris Mason5caf2a02007-04-02 11:20:42 -04003003 WARN_ON(*level <= 0);
Chris Mason83e15a22007-03-12 09:03:27 -04003004 if (path->nodes[*level-1])
Chris Mason5f39d392007-10-15 16:14:19 -04003005 free_extent_buffer(path->nodes[*level-1]);
Chris Mason20524f02007-03-10 06:35:47 -05003006 path->nodes[*level-1] = next;
Chris Mason5f39d392007-10-15 16:14:19 -04003007 *level = btrfs_header_level(next);
Chris Mason20524f02007-03-10 06:35:47 -05003008 path->slots[*level] = 0;
Chris Mason2dd3e672008-08-04 08:20:15 -04003009 cond_resched();
Chris Mason20524f02007-03-10 06:35:47 -05003010 }
3011out:
Chris Mason5caf2a02007-04-02 11:20:42 -04003012 WARN_ON(*level < 0);
3013 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason7bb86312007-12-11 09:25:06 -05003014
3015 if (path->nodes[*level] == root->node) {
Chris Mason7bb86312007-12-11 09:25:06 -05003016 parent = path->nodes[*level];
Yan Zheng31153d82008-07-28 15:32:19 -04003017 bytenr = path->nodes[*level]->start;
Chris Mason7bb86312007-12-11 09:25:06 -05003018 } else {
3019 parent = path->nodes[*level + 1];
Yan Zheng31153d82008-07-28 15:32:19 -04003020 bytenr = btrfs_node_blockptr(parent, path->slots[*level + 1]);
Chris Mason7bb86312007-12-11 09:25:06 -05003021 }
3022
Yan Zheng31153d82008-07-28 15:32:19 -04003023 blocksize = btrfs_level_size(root, *level);
3024 root_owner = btrfs_header_owner(parent);
Chris Mason7bb86312007-12-11 09:25:06 -05003025 root_gen = btrfs_header_generation(parent);
Yan Zheng31153d82008-07-28 15:32:19 -04003026
Chris Masonf87f0572008-08-01 11:27:23 -04003027 mutex_lock(&root->fs_info->alloc_mutex);
Yan Zheng31153d82008-07-28 15:32:19 -04003028 ret = __btrfs_free_extent(trans, root, bytenr, blocksize,
Zheng Yan31840ae2008-09-23 13:14:14 -04003029 parent->start, root_owner, root_gen,
3030 0, 0, 1);
3031 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Mason5f39d392007-10-15 16:14:19 -04003032 free_extent_buffer(path->nodes[*level]);
Chris Mason20524f02007-03-10 06:35:47 -05003033 path->nodes[*level] = NULL;
3034 *level += 1;
3035 BUG_ON(ret);
Chris Masonf87f0572008-08-01 11:27:23 -04003036
Chris Masone7a84562008-06-25 16:01:31 -04003037 cond_resched();
Chris Mason20524f02007-03-10 06:35:47 -05003038 return 0;
3039}
3040
Chris Mason9aca1d52007-03-13 11:09:37 -04003041/*
3042 * helper for dropping snapshots. This walks back up the tree in the path
3043 * to find the first node higher up where we haven't yet gone through
3044 * all the slots
3045 */
Chris Mason98ed5172008-01-03 10:01:48 -05003046static int noinline walk_up_tree(struct btrfs_trans_handle *trans,
3047 struct btrfs_root *root,
3048 struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05003049{
Chris Mason7bb86312007-12-11 09:25:06 -05003050 u64 root_owner;
3051 u64 root_gen;
3052 struct btrfs_root_item *root_item = &root->root_item;
Chris Mason20524f02007-03-10 06:35:47 -05003053 int i;
3054 int slot;
3055 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04003056
Chris Mason234b63a2007-03-13 10:46:10 -04003057 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Mason20524f02007-03-10 06:35:47 -05003058 slot = path->slots[i];
Chris Mason5f39d392007-10-15 16:14:19 -04003059 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
3060 struct extent_buffer *node;
3061 struct btrfs_disk_key disk_key;
3062 node = path->nodes[i];
Chris Mason20524f02007-03-10 06:35:47 -05003063 path->slots[i]++;
3064 *level = i;
Chris Mason9f3a7422007-08-07 15:52:19 -04003065 WARN_ON(*level == 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003066 btrfs_node_key(node, &disk_key, path->slots[i]);
Chris Mason9f3a7422007-08-07 15:52:19 -04003067 memcpy(&root_item->drop_progress,
Chris Mason5f39d392007-10-15 16:14:19 -04003068 &disk_key, sizeof(disk_key));
Chris Mason9f3a7422007-08-07 15:52:19 -04003069 root_item->drop_level = i;
Chris Mason20524f02007-03-10 06:35:47 -05003070 return 0;
3071 } else {
Zheng Yan31840ae2008-09-23 13:14:14 -04003072 struct extent_buffer *parent;
3073 if (path->nodes[*level] == root->node)
3074 parent = path->nodes[*level];
3075 else
3076 parent = path->nodes[*level + 1];
3077
3078 root_owner = btrfs_header_owner(parent);
3079 root_gen = btrfs_header_generation(parent);
Chris Masone089f052007-03-16 16:20:31 -04003080 ret = btrfs_free_extent(trans, root,
Chris Masondb945352007-10-15 16:15:53 -04003081 path->nodes[*level]->start,
Chris Mason7bb86312007-12-11 09:25:06 -05003082 path->nodes[*level]->len,
Zheng Yan31840ae2008-09-23 13:14:14 -04003083 parent->start,
Chris Mason7bb86312007-12-11 09:25:06 -05003084 root_owner, root_gen, 0, 0, 1);
Chris Mason6407bf62007-03-27 06:33:00 -04003085 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04003086 free_extent_buffer(path->nodes[*level]);
Chris Mason83e15a22007-03-12 09:03:27 -04003087 path->nodes[*level] = NULL;
Chris Mason20524f02007-03-10 06:35:47 -05003088 *level = i + 1;
Chris Mason20524f02007-03-10 06:35:47 -05003089 }
3090 }
3091 return 1;
3092}
3093
Chris Mason9aca1d52007-03-13 11:09:37 -04003094/*
3095 * drop the reference count on the tree rooted at 'snap'. This traverses
3096 * the tree freeing any blocks that have a ref count of zero after being
3097 * decremented.
3098 */
Chris Masone089f052007-03-16 16:20:31 -04003099int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason9f3a7422007-08-07 15:52:19 -04003100 *root)
Chris Mason20524f02007-03-10 06:35:47 -05003101{
Chris Mason3768f362007-03-13 16:47:54 -04003102 int ret = 0;
Chris Mason9aca1d52007-03-13 11:09:37 -04003103 int wret;
Chris Mason20524f02007-03-10 06:35:47 -05003104 int level;
Chris Mason5caf2a02007-04-02 11:20:42 -04003105 struct btrfs_path *path;
Chris Mason20524f02007-03-10 06:35:47 -05003106 int i;
3107 int orig_level;
Chris Mason9f3a7422007-08-07 15:52:19 -04003108 struct btrfs_root_item *root_item = &root->root_item;
Chris Mason20524f02007-03-10 06:35:47 -05003109
Chris Masona2135012008-06-25 16:01:30 -04003110 WARN_ON(!mutex_is_locked(&root->fs_info->drop_mutex));
Chris Mason5caf2a02007-04-02 11:20:42 -04003111 path = btrfs_alloc_path();
3112 BUG_ON(!path);
Chris Mason20524f02007-03-10 06:35:47 -05003113
Chris Mason5f39d392007-10-15 16:14:19 -04003114 level = btrfs_header_level(root->node);
Chris Mason20524f02007-03-10 06:35:47 -05003115 orig_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04003116 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
3117 path->nodes[level] = root->node;
Chris Masonf510cfe2007-10-15 16:14:48 -04003118 extent_buffer_get(root->node);
Chris Mason9f3a7422007-08-07 15:52:19 -04003119 path->slots[level] = 0;
3120 } else {
3121 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04003122 struct btrfs_disk_key found_key;
3123 struct extent_buffer *node;
Chris Mason6702ed42007-08-07 16:15:09 -04003124
Chris Mason9f3a7422007-08-07 15:52:19 -04003125 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Chris Mason6702ed42007-08-07 16:15:09 -04003126 level = root_item->drop_level;
3127 path->lowest_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04003128 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason6702ed42007-08-07 16:15:09 -04003129 if (wret < 0) {
Chris Mason9f3a7422007-08-07 15:52:19 -04003130 ret = wret;
3131 goto out;
3132 }
Chris Mason5f39d392007-10-15 16:14:19 -04003133 node = path->nodes[level];
3134 btrfs_node_key(node, &found_key, path->slots[level]);
3135 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
3136 sizeof(found_key)));
Chris Mason7d9eb122008-07-08 14:19:17 -04003137 /*
3138 * unlock our path, this is safe because only this
3139 * function is allowed to delete this snapshot
3140 */
Chris Mason925baed2008-06-25 16:01:30 -04003141 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
3142 if (path->nodes[i] && path->locks[i]) {
3143 path->locks[i] = 0;
3144 btrfs_tree_unlock(path->nodes[i]);
3145 }
3146 }
Chris Mason9f3a7422007-08-07 15:52:19 -04003147 }
Chris Mason20524f02007-03-10 06:35:47 -05003148 while(1) {
Chris Mason5caf2a02007-04-02 11:20:42 -04003149 wret = walk_down_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04003150 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05003151 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04003152 if (wret < 0)
3153 ret = wret;
3154
Chris Mason5caf2a02007-04-02 11:20:42 -04003155 wret = walk_up_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04003156 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05003157 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04003158 if (wret < 0)
3159 ret = wret;
Chris Masone7a84562008-06-25 16:01:31 -04003160 if (trans->transaction->in_commit) {
3161 ret = -EAGAIN;
3162 break;
3163 }
Chris Mason18e35e0a2008-08-01 13:11:41 -04003164 atomic_inc(&root->fs_info->throttle_gen);
Chris Mason017e5362008-07-28 15:32:51 -04003165 wake_up(&root->fs_info->transaction_throttle);
Chris Mason20524f02007-03-10 06:35:47 -05003166 }
Chris Mason83e15a22007-03-12 09:03:27 -04003167 for (i = 0; i <= orig_level; i++) {
Chris Mason5caf2a02007-04-02 11:20:42 -04003168 if (path->nodes[i]) {
Chris Mason5f39d392007-10-15 16:14:19 -04003169 free_extent_buffer(path->nodes[i]);
Chris Mason0f827312007-10-15 16:18:56 -04003170 path->nodes[i] = NULL;
Chris Mason83e15a22007-03-12 09:03:27 -04003171 }
Chris Mason20524f02007-03-10 06:35:47 -05003172 }
Chris Mason9f3a7422007-08-07 15:52:19 -04003173out:
Chris Mason5caf2a02007-04-02 11:20:42 -04003174 btrfs_free_path(path);
Chris Mason9aca1d52007-03-13 11:09:37 -04003175 return ret;
Chris Mason20524f02007-03-10 06:35:47 -05003176}
Chris Mason9078a3e2007-04-26 16:46:15 -04003177
Chris Mason8e7bf942008-04-28 09:02:36 -04003178static unsigned long calc_ra(unsigned long start, unsigned long last,
3179 unsigned long nr)
3180{
3181 return min(last, start + nr - 1);
3182}
3183
Chris Mason98ed5172008-01-03 10:01:48 -05003184static int noinline relocate_inode_pages(struct inode *inode, u64 start,
3185 u64 len)
Chris Masonedbd8d42007-12-21 16:27:24 -05003186{
3187 u64 page_start;
3188 u64 page_end;
Zheng Yan1a40e232008-09-26 10:09:34 -04003189 unsigned long first_index;
Chris Masonedbd8d42007-12-21 16:27:24 -05003190 unsigned long last_index;
Chris Masonedbd8d42007-12-21 16:27:24 -05003191 unsigned long i;
3192 struct page *page;
Chris Masond1310b22008-01-24 16:13:08 -05003193 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason4313b392008-01-03 09:08:48 -05003194 struct file_ra_state *ra;
Chris Mason3eaa2882008-07-24 11:57:52 -04003195 struct btrfs_ordered_extent *ordered;
Zheng Yan1a40e232008-09-26 10:09:34 -04003196 unsigned int total_read = 0;
3197 unsigned int total_dirty = 0;
3198 int ret = 0;
Chris Mason4313b392008-01-03 09:08:48 -05003199
3200 ra = kzalloc(sizeof(*ra), GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05003201
3202 mutex_lock(&inode->i_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04003203 first_index = start >> PAGE_CACHE_SHIFT;
Chris Masonedbd8d42007-12-21 16:27:24 -05003204 last_index = (start + len - 1) >> PAGE_CACHE_SHIFT;
3205
Zheng Yan1a40e232008-09-26 10:09:34 -04003206 /* make sure the dirty trick played by the caller work */
3207 ret = invalidate_inode_pages2_range(inode->i_mapping,
3208 first_index, last_index);
3209 if (ret)
3210 goto out_unlock;
Chris Mason8e7bf942008-04-28 09:02:36 -04003211
Chris Mason4313b392008-01-03 09:08:48 -05003212 file_ra_state_init(ra, inode->i_mapping);
Chris Masonedbd8d42007-12-21 16:27:24 -05003213
Zheng Yan1a40e232008-09-26 10:09:34 -04003214 for (i = first_index ; i <= last_index; i++) {
3215 if (total_read % ra->ra_pages == 0) {
Chris Mason8e7bf942008-04-28 09:02:36 -04003216 btrfs_force_ra(inode->i_mapping, ra, NULL, i,
Zheng Yan1a40e232008-09-26 10:09:34 -04003217 calc_ra(i, last_index, ra->ra_pages));
Chris Mason8e7bf942008-04-28 09:02:36 -04003218 }
3219 total_read++;
Chris Mason3eaa2882008-07-24 11:57:52 -04003220again:
3221 if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode))
Zheng Yan1a40e232008-09-26 10:09:34 -04003222 BUG_ON(1);
Chris Masonedbd8d42007-12-21 16:27:24 -05003223 page = grab_cache_page(inode->i_mapping, i);
Chris Masona061fc82008-05-07 11:43:44 -04003224 if (!page) {
Zheng Yan1a40e232008-09-26 10:09:34 -04003225 ret = -ENOMEM;
Chris Masonedbd8d42007-12-21 16:27:24 -05003226 goto out_unlock;
Chris Masona061fc82008-05-07 11:43:44 -04003227 }
Chris Masonedbd8d42007-12-21 16:27:24 -05003228 if (!PageUptodate(page)) {
3229 btrfs_readpage(NULL, page);
3230 lock_page(page);
3231 if (!PageUptodate(page)) {
3232 unlock_page(page);
3233 page_cache_release(page);
Zheng Yan1a40e232008-09-26 10:09:34 -04003234 ret = -EIO;
Chris Masonedbd8d42007-12-21 16:27:24 -05003235 goto out_unlock;
3236 }
3237 }
Chris Masonec44a352008-04-28 15:29:52 -04003238 wait_on_page_writeback(page);
Chris Mason3eaa2882008-07-24 11:57:52 -04003239
Chris Masonedbd8d42007-12-21 16:27:24 -05003240 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
3241 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05003242 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05003243
Chris Mason3eaa2882008-07-24 11:57:52 -04003244 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3245 if (ordered) {
3246 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
3247 unlock_page(page);
3248 page_cache_release(page);
3249 btrfs_start_ordered_extent(inode, ordered, 1);
3250 btrfs_put_ordered_extent(ordered);
3251 goto again;
3252 }
3253 set_page_extent_mapped(page);
3254
Chris Masonea8c2812008-08-04 23:17:27 -04003255 btrfs_set_extent_delalloc(inode, page_start, page_end);
Zheng Yan1a40e232008-09-26 10:09:34 -04003256 if (i == first_index)
3257 set_extent_bits(io_tree, page_start, page_end,
3258 EXTENT_BOUNDARY, GFP_NOFS);
3259
Chris Masona061fc82008-05-07 11:43:44 -04003260 set_page_dirty(page);
Zheng Yan1a40e232008-09-26 10:09:34 -04003261 total_dirty++;
Chris Masonedbd8d42007-12-21 16:27:24 -05003262
Chris Masond1310b22008-01-24 16:13:08 -05003263 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05003264 unlock_page(page);
3265 page_cache_release(page);
3266 }
3267
3268out_unlock:
Chris Masonec44a352008-04-28 15:29:52 -04003269 kfree(ra);
Chris Masonedbd8d42007-12-21 16:27:24 -05003270 mutex_unlock(&inode->i_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04003271 balance_dirty_pages_ratelimited_nr(inode->i_mapping, total_dirty);
Chris Masonbf4ef672008-05-08 13:26:18 -04003272 return ret;
3273}
3274
Zheng Yan1a40e232008-09-26 10:09:34 -04003275static int noinline relocate_data_extent(struct inode *reloc_inode,
3276 struct btrfs_key *extent_key,
3277 u64 offset)
Chris Masonedbd8d42007-12-21 16:27:24 -05003278{
Zheng Yan1a40e232008-09-26 10:09:34 -04003279 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
3280 struct extent_map_tree *em_tree = &BTRFS_I(reloc_inode)->extent_tree;
3281 struct extent_map *em;
3282
3283 em = alloc_extent_map(GFP_NOFS);
3284 BUG_ON(!em || IS_ERR(em));
3285
3286 em->start = extent_key->objectid - offset;
3287 em->len = extent_key->offset;
3288 em->block_start = extent_key->objectid;
3289 em->bdev = root->fs_info->fs_devices->latest_bdev;
3290 set_bit(EXTENT_FLAG_PINNED, &em->flags);
3291
3292 /* setup extent map to cheat btrfs_readpage */
3293 mutex_lock(&BTRFS_I(reloc_inode)->extent_mutex);
3294 while (1) {
3295 int ret;
3296 spin_lock(&em_tree->lock);
3297 ret = add_extent_mapping(em_tree, em);
3298 spin_unlock(&em_tree->lock);
3299 if (ret != -EEXIST) {
3300 free_extent_map(em);
3301 break;
3302 }
3303 btrfs_drop_extent_cache(reloc_inode, em->start,
3304 em->start + em->len - 1, 0);
3305 }
3306 mutex_unlock(&BTRFS_I(reloc_inode)->extent_mutex);
3307
3308 return relocate_inode_pages(reloc_inode, extent_key->objectid - offset,
3309 extent_key->offset);
3310}
3311
3312struct btrfs_ref_path {
3313 u64 extent_start;
3314 u64 nodes[BTRFS_MAX_LEVEL];
3315 u64 root_objectid;
3316 u64 root_generation;
3317 u64 owner_objectid;
3318 u64 owner_offset;
3319 u32 num_refs;
3320 int lowest_level;
3321 int current_level;
3322};
3323
3324struct disk_extent {
3325 u64 disk_bytenr;
3326 u64 disk_num_bytes;
3327 u64 offset;
3328 u64 num_bytes;
3329};
3330
3331static int is_cowonly_root(u64 root_objectid)
3332{
3333 if (root_objectid == BTRFS_ROOT_TREE_OBJECTID ||
3334 root_objectid == BTRFS_EXTENT_TREE_OBJECTID ||
3335 root_objectid == BTRFS_CHUNK_TREE_OBJECTID ||
3336 root_objectid == BTRFS_DEV_TREE_OBJECTID ||
3337 root_objectid == BTRFS_TREE_LOG_OBJECTID)
3338 return 1;
3339 return 0;
3340}
3341
3342static int noinline __next_ref_path(struct btrfs_trans_handle *trans,
3343 struct btrfs_root *extent_root,
3344 struct btrfs_ref_path *ref_path,
3345 int first_time)
3346{
3347 struct extent_buffer *leaf;
3348 struct btrfs_path *path;
Chris Mason4313b392008-01-03 09:08:48 -05003349 struct btrfs_extent_ref *ref;
Chris Masonedbd8d42007-12-21 16:27:24 -05003350 struct btrfs_key key;
3351 struct btrfs_key found_key;
Zheng Yan1a40e232008-09-26 10:09:34 -04003352 u64 bytenr;
Chris Masonedbd8d42007-12-21 16:27:24 -05003353 u32 nritems;
Zheng Yan1a40e232008-09-26 10:09:34 -04003354 int level;
3355 int ret = 1;
Chris Masonedbd8d42007-12-21 16:27:24 -05003356
Zheng Yan1a40e232008-09-26 10:09:34 -04003357 path = btrfs_alloc_path();
3358 if (!path)
3359 return -ENOMEM;
3360
3361 mutex_lock(&extent_root->fs_info->alloc_mutex);
3362
3363 if (first_time) {
3364 ref_path->lowest_level = -1;
3365 ref_path->current_level = -1;
3366 goto walk_up;
Chris Masona061fc82008-05-07 11:43:44 -04003367 }
Zheng Yan1a40e232008-09-26 10:09:34 -04003368walk_down:
3369 level = ref_path->current_level - 1;
3370 while (level >= -1) {
3371 u64 parent;
3372 if (level < ref_path->lowest_level)
3373 break;
Chris Masonedbd8d42007-12-21 16:27:24 -05003374
Zheng Yan1a40e232008-09-26 10:09:34 -04003375 if (level >= 0) {
3376 bytenr = ref_path->nodes[level];
3377 } else {
3378 bytenr = ref_path->extent_start;
3379 }
3380 BUG_ON(bytenr == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05003381
Zheng Yan1a40e232008-09-26 10:09:34 -04003382 parent = ref_path->nodes[level + 1];
3383 ref_path->nodes[level + 1] = 0;
3384 ref_path->current_level = level;
3385 BUG_ON(parent == 0);
3386
3387 key.objectid = bytenr;
3388 key.offset = parent + 1;
3389 key.type = BTRFS_EXTENT_REF_KEY;
3390
3391 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05003392 if (ret < 0)
3393 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04003394 BUG_ON(ret == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05003395
Chris Masonedbd8d42007-12-21 16:27:24 -05003396 leaf = path->nodes[0];
3397 nritems = btrfs_header_nritems(leaf);
Zheng Yan1a40e232008-09-26 10:09:34 -04003398 if (path->slots[0] >= nritems) {
Chris Masona061fc82008-05-07 11:43:44 -04003399 ret = btrfs_next_leaf(extent_root, path);
Chris Masona061fc82008-05-07 11:43:44 -04003400 if (ret < 0)
3401 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04003402 if (ret > 0)
3403 goto next;
Chris Masonbf4ef672008-05-08 13:26:18 -04003404 leaf = path->nodes[0];
Chris Masona061fc82008-05-07 11:43:44 -04003405 }
Chris Masonedbd8d42007-12-21 16:27:24 -05003406
3407 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Zheng Yan1a40e232008-09-26 10:09:34 -04003408 if (found_key.objectid == bytenr &&
3409 found_key.type == BTRFS_EXTENT_REF_KEY)
3410 goto found;
3411next:
3412 level--;
3413 btrfs_release_path(extent_root, path);
3414 if (need_resched()) {
3415 mutex_unlock(&extent_root->fs_info->alloc_mutex);
3416 cond_resched();
3417 mutex_lock(&extent_root->fs_info->alloc_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04003418 }
Zheng Yan1a40e232008-09-26 10:09:34 -04003419 }
3420 /* reached lowest level */
3421 ret = 1;
3422 goto out;
3423walk_up:
3424 level = ref_path->current_level;
3425 while (level < BTRFS_MAX_LEVEL - 1) {
3426 u64 ref_objectid;
3427 if (level >= 0) {
3428 bytenr = ref_path->nodes[level];
3429 } else {
3430 bytenr = ref_path->extent_start;
Chris Masona061fc82008-05-07 11:43:44 -04003431 }
Zheng Yan1a40e232008-09-26 10:09:34 -04003432 BUG_ON(bytenr == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05003433
Zheng Yan1a40e232008-09-26 10:09:34 -04003434 key.objectid = bytenr;
3435 key.offset = 0;
3436 key.type = BTRFS_EXTENT_REF_KEY;
Chris Masonedbd8d42007-12-21 16:27:24 -05003437
Zheng Yan1a40e232008-09-26 10:09:34 -04003438 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
3439 if (ret < 0)
Chris Masonedbd8d42007-12-21 16:27:24 -05003440 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04003441
3442 leaf = path->nodes[0];
3443 nritems = btrfs_header_nritems(leaf);
3444 if (path->slots[0] >= nritems) {
3445 ret = btrfs_next_leaf(extent_root, path);
3446 if (ret < 0)
3447 goto out;
3448 if (ret > 0) {
3449 /* the extent was freed by someone */
3450 if (ref_path->lowest_level == level)
3451 goto out;
3452 btrfs_release_path(extent_root, path);
3453 goto walk_down;
3454 }
3455 leaf = path->nodes[0];
3456 }
3457
3458 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3459 if (found_key.objectid != bytenr ||
3460 found_key.type != BTRFS_EXTENT_REF_KEY) {
3461 /* the extent was freed by someone */
3462 if (ref_path->lowest_level == level) {
3463 ret = 1;
3464 goto out;
3465 }
3466 btrfs_release_path(extent_root, path);
3467 goto walk_down;
3468 }
3469found:
3470 ref = btrfs_item_ptr(leaf, path->slots[0],
3471 struct btrfs_extent_ref);
3472 ref_objectid = btrfs_ref_objectid(leaf, ref);
3473 if (ref_objectid < BTRFS_FIRST_FREE_OBJECTID) {
3474 if (first_time) {
3475 level = (int)ref_objectid;
3476 BUG_ON(level >= BTRFS_MAX_LEVEL);
3477 ref_path->lowest_level = level;
3478 ref_path->current_level = level;
3479 ref_path->nodes[level] = bytenr;
3480 } else {
3481 WARN_ON(ref_objectid != level);
3482 }
3483 } else {
3484 WARN_ON(level != -1);
3485 }
3486 first_time = 0;
3487
3488 if (ref_path->lowest_level == level) {
3489 ref_path->owner_objectid = ref_objectid;
3490 ref_path->owner_offset = btrfs_ref_offset(leaf, ref);
3491 ref_path->num_refs = btrfs_ref_num_refs(leaf, ref);
3492 }
3493
3494 /*
3495 * the block is tree root or the block isn't in reference
3496 * counted tree.
3497 */
3498 if (found_key.objectid == found_key.offset ||
3499 is_cowonly_root(btrfs_ref_root(leaf, ref))) {
3500 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
3501 ref_path->root_generation =
3502 btrfs_ref_generation(leaf, ref);
3503 if (level < 0) {
3504 /* special reference from the tree log */
3505 ref_path->nodes[0] = found_key.offset;
3506 ref_path->current_level = 0;
3507 }
3508 ret = 0;
3509 goto out;
3510 }
3511
3512 level++;
3513 BUG_ON(ref_path->nodes[level] != 0);
3514 ref_path->nodes[level] = found_key.offset;
3515 ref_path->current_level = level;
3516
3517 /*
3518 * the reference was created in the running transaction,
3519 * no need to continue walking up.
3520 */
3521 if (btrfs_ref_generation(leaf, ref) == trans->transid) {
3522 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
3523 ref_path->root_generation =
3524 btrfs_ref_generation(leaf, ref);
3525 ret = 0;
3526 goto out;
3527 }
3528
3529 btrfs_release_path(extent_root, path);
3530 if (need_resched()) {
3531 mutex_unlock(&extent_root->fs_info->alloc_mutex);
3532 cond_resched();
3533 mutex_lock(&extent_root->fs_info->alloc_mutex);
3534 }
3535 }
3536 /* reached max tree level, but no tree root found. */
3537 BUG();
3538out:
3539 mutex_unlock(&extent_root->fs_info->alloc_mutex);
3540 btrfs_free_path(path);
3541 return ret;
3542}
3543
3544static int btrfs_first_ref_path(struct btrfs_trans_handle *trans,
3545 struct btrfs_root *extent_root,
3546 struct btrfs_ref_path *ref_path,
3547 u64 extent_start)
3548{
3549 memset(ref_path, 0, sizeof(*ref_path));
3550 ref_path->extent_start = extent_start;
3551
3552 return __next_ref_path(trans, extent_root, ref_path, 1);
3553}
3554
3555static int btrfs_next_ref_path(struct btrfs_trans_handle *trans,
3556 struct btrfs_root *extent_root,
3557 struct btrfs_ref_path *ref_path)
3558{
3559 return __next_ref_path(trans, extent_root, ref_path, 0);
3560}
3561
3562static int noinline get_new_locations(struct inode *reloc_inode,
3563 struct btrfs_key *extent_key,
3564 u64 offset, int no_fragment,
3565 struct disk_extent **extents,
3566 int *nr_extents)
3567{
3568 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
3569 struct btrfs_path *path;
3570 struct btrfs_file_extent_item *fi;
3571 struct extent_buffer *leaf;
3572 struct disk_extent *exts = *extents;
3573 struct btrfs_key found_key;
3574 u64 cur_pos;
3575 u64 last_byte;
3576 u32 nritems;
3577 int nr = 0;
3578 int max = *nr_extents;
3579 int ret;
3580
3581 WARN_ON(!no_fragment && *extents);
3582 if (!exts) {
3583 max = 1;
3584 exts = kmalloc(sizeof(*exts) * max, GFP_NOFS);
3585 if (!exts)
3586 return -ENOMEM;
3587 }
3588
3589 path = btrfs_alloc_path();
3590 BUG_ON(!path);
3591
3592 cur_pos = extent_key->objectid - offset;
3593 last_byte = extent_key->objectid + extent_key->offset;
3594 ret = btrfs_lookup_file_extent(NULL, root, path, reloc_inode->i_ino,
3595 cur_pos, 0);
3596 if (ret < 0)
3597 goto out;
3598 if (ret > 0) {
3599 ret = -ENOENT;
3600 goto out;
3601 }
3602
3603 while (1) {
3604 leaf = path->nodes[0];
3605 nritems = btrfs_header_nritems(leaf);
3606 if (path->slots[0] >= nritems) {
3607 ret = btrfs_next_leaf(root, path);
3608 if (ret < 0)
3609 goto out;
3610 if (ret > 0)
3611 break;
3612 leaf = path->nodes[0];
3613 }
3614
3615 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3616 if (found_key.offset != cur_pos ||
3617 found_key.type != BTRFS_EXTENT_DATA_KEY ||
3618 found_key.objectid != reloc_inode->i_ino)
3619 break;
3620
3621 fi = btrfs_item_ptr(leaf, path->slots[0],
3622 struct btrfs_file_extent_item);
3623 if (btrfs_file_extent_type(leaf, fi) !=
3624 BTRFS_FILE_EXTENT_REG ||
3625 btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
3626 break;
3627
3628 if (nr == max) {
3629 struct disk_extent *old = exts;
3630 max *= 2;
3631 exts = kzalloc(sizeof(*exts) * max, GFP_NOFS);
3632 memcpy(exts, old, sizeof(*exts) * nr);
3633 if (old != *extents)
3634 kfree(old);
3635 }
3636
3637 exts[nr].disk_bytenr =
3638 btrfs_file_extent_disk_bytenr(leaf, fi);
3639 exts[nr].disk_num_bytes =
3640 btrfs_file_extent_disk_num_bytes(leaf, fi);
3641 exts[nr].offset = btrfs_file_extent_offset(leaf, fi);
3642 exts[nr].num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
3643 WARN_ON(exts[nr].offset > 0);
3644 WARN_ON(exts[nr].num_bytes != exts[nr].disk_num_bytes);
3645
3646 cur_pos += exts[nr].num_bytes;
3647 nr++;
3648
3649 if (cur_pos + offset >= last_byte)
3650 break;
3651
3652 if (no_fragment) {
3653 ret = 1;
3654 goto out;
3655 }
3656 path->slots[0]++;
3657 }
3658
3659 WARN_ON(cur_pos + offset > last_byte);
3660 if (cur_pos + offset < last_byte) {
3661 ret = -ENOENT;
3662 goto out;
Chris Masonedbd8d42007-12-21 16:27:24 -05003663 }
3664 ret = 0;
3665out:
Zheng Yan1a40e232008-09-26 10:09:34 -04003666 btrfs_free_path(path);
3667 if (ret) {
3668 if (exts != *extents)
3669 kfree(exts);
3670 } else {
3671 *extents = exts;
3672 *nr_extents = nr;
3673 }
3674 return ret;
3675}
3676
3677static int noinline replace_one_extent(struct btrfs_trans_handle *trans,
3678 struct btrfs_root *root,
3679 struct btrfs_path *path,
3680 struct btrfs_key *extent_key,
3681 struct btrfs_key *leaf_key,
3682 struct btrfs_ref_path *ref_path,
3683 struct disk_extent *new_extents,
3684 int nr_extents)
3685{
3686 struct extent_buffer *leaf;
3687 struct btrfs_file_extent_item *fi;
3688 struct inode *inode = NULL;
3689 struct btrfs_key key;
3690 u64 lock_start = 0;
3691 u64 lock_end = 0;
3692 u64 num_bytes;
3693 u64 ext_offset;
3694 u64 first_pos;
3695 u32 nritems;
3696 int extent_locked = 0;
3697 int ret;
3698
3699 first_pos = ref_path->owner_offset;
3700 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
3701 key.objectid = ref_path->owner_objectid;
3702 key.offset = ref_path->owner_offset;
3703 key.type = BTRFS_EXTENT_DATA_KEY;
3704 } else {
3705 memcpy(&key, leaf_key, sizeof(key));
3706 }
3707
3708 while (1) {
3709 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
3710 if (ret < 0)
3711 goto out;
3712
3713 leaf = path->nodes[0];
3714 nritems = btrfs_header_nritems(leaf);
3715next:
3716 if (extent_locked && ret > 0) {
3717 /*
3718 * the file extent item was modified by someone
3719 * before the extent got locked.
3720 */
3721 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
3722 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
3723 lock_end, GFP_NOFS);
3724 extent_locked = 0;
3725 }
3726
3727 if (path->slots[0] >= nritems) {
3728 if (ref_path->owner_objectid ==
3729 BTRFS_MULTIPLE_OBJECTIDS)
3730 break;
3731
3732 BUG_ON(extent_locked);
3733 ret = btrfs_next_leaf(root, path);
3734 if (ret < 0)
3735 goto out;
3736 if (ret > 0)
3737 break;
3738 leaf = path->nodes[0];
3739 nritems = btrfs_header_nritems(leaf);
3740 }
3741
3742 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3743
3744 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
3745 if ((key.objectid > ref_path->owner_objectid) ||
3746 (key.objectid == ref_path->owner_objectid &&
3747 key.type > BTRFS_EXTENT_DATA_KEY) ||
3748 (key.offset >= first_pos + extent_key->offset))
3749 break;
3750 }
3751
3752 if (inode && key.objectid != inode->i_ino) {
3753 BUG_ON(extent_locked);
3754 btrfs_release_path(root, path);
3755 mutex_unlock(&inode->i_mutex);
3756 iput(inode);
3757 inode = NULL;
3758 continue;
3759 }
3760
3761 if (key.type != BTRFS_EXTENT_DATA_KEY) {
3762 path->slots[0]++;
3763 ret = 1;
3764 goto next;
3765 }
3766 fi = btrfs_item_ptr(leaf, path->slots[0],
3767 struct btrfs_file_extent_item);
3768 if ((btrfs_file_extent_type(leaf, fi) !=
3769 BTRFS_FILE_EXTENT_REG) ||
3770 (btrfs_file_extent_disk_bytenr(leaf, fi) !=
3771 extent_key->objectid)) {
3772 path->slots[0]++;
3773 ret = 1;
3774 goto next;
3775 }
3776
3777 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
3778 ext_offset = btrfs_file_extent_offset(leaf, fi);
3779
3780 if (first_pos > key.offset - ext_offset)
3781 first_pos = key.offset - ext_offset;
3782
3783 if (!extent_locked) {
3784 lock_start = key.offset;
3785 lock_end = lock_start + num_bytes - 1;
3786 } else {
3787 BUG_ON(lock_start != key.offset);
3788 BUG_ON(lock_end - lock_start + 1 < num_bytes);
3789 }
3790
3791 if (!inode) {
3792 btrfs_release_path(root, path);
3793
3794 inode = btrfs_iget_locked(root->fs_info->sb,
3795 key.objectid, root);
3796 if (inode->i_state & I_NEW) {
3797 BTRFS_I(inode)->root = root;
3798 BTRFS_I(inode)->location.objectid =
3799 key.objectid;
3800 BTRFS_I(inode)->location.type =
3801 BTRFS_INODE_ITEM_KEY;
3802 BTRFS_I(inode)->location.offset = 0;
3803 btrfs_read_locked_inode(inode);
3804 unlock_new_inode(inode);
3805 }
3806 /*
3807 * some code call btrfs_commit_transaction while
3808 * holding the i_mutex, so we can't use mutex_lock
3809 * here.
3810 */
3811 if (is_bad_inode(inode) ||
3812 !mutex_trylock(&inode->i_mutex)) {
3813 iput(inode);
3814 inode = NULL;
3815 key.offset = (u64)-1;
3816 goto skip;
3817 }
3818 }
3819
3820 if (!extent_locked) {
3821 struct btrfs_ordered_extent *ordered;
3822
3823 btrfs_release_path(root, path);
3824
3825 lock_extent(&BTRFS_I(inode)->io_tree, lock_start,
3826 lock_end, GFP_NOFS);
3827 ordered = btrfs_lookup_first_ordered_extent(inode,
3828 lock_end);
3829 if (ordered &&
3830 ordered->file_offset <= lock_end &&
3831 ordered->file_offset + ordered->len > lock_start) {
3832 unlock_extent(&BTRFS_I(inode)->io_tree,
3833 lock_start, lock_end, GFP_NOFS);
3834 btrfs_start_ordered_extent(inode, ordered, 1);
3835 btrfs_put_ordered_extent(ordered);
3836 key.offset += num_bytes;
3837 goto skip;
3838 }
3839 if (ordered)
3840 btrfs_put_ordered_extent(ordered);
3841
3842 mutex_lock(&BTRFS_I(inode)->extent_mutex);
3843 extent_locked = 1;
3844 continue;
3845 }
3846
3847 if (nr_extents == 1) {
3848 /* update extent pointer in place */
3849 btrfs_set_file_extent_generation(leaf, fi,
3850 trans->transid);
3851 btrfs_set_file_extent_disk_bytenr(leaf, fi,
3852 new_extents[0].disk_bytenr);
3853 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
3854 new_extents[0].disk_num_bytes);
3855 ext_offset += new_extents[0].offset;
3856 btrfs_set_file_extent_offset(leaf, fi, ext_offset);
3857 btrfs_mark_buffer_dirty(leaf);
3858
3859 btrfs_drop_extent_cache(inode, key.offset,
3860 key.offset + num_bytes - 1, 0);
3861
3862 ret = btrfs_inc_extent_ref(trans, root,
3863 new_extents[0].disk_bytenr,
3864 new_extents[0].disk_num_bytes,
3865 leaf->start,
3866 root->root_key.objectid,
3867 trans->transid,
3868 key.objectid, key.offset);
3869 BUG_ON(ret);
3870
3871 ret = btrfs_free_extent(trans, root,
3872 extent_key->objectid,
3873 extent_key->offset,
3874 leaf->start,
3875 btrfs_header_owner(leaf),
3876 btrfs_header_generation(leaf),
3877 key.objectid, key.offset, 0);
3878 BUG_ON(ret);
3879
3880 btrfs_release_path(root, path);
3881 key.offset += num_bytes;
3882 } else {
3883 u64 alloc_hint;
3884 u64 extent_len;
3885 int i;
3886 /*
3887 * drop old extent pointer at first, then insert the
3888 * new pointers one bye one
3889 */
3890 btrfs_release_path(root, path);
3891 ret = btrfs_drop_extents(trans, root, inode, key.offset,
3892 key.offset + num_bytes,
3893 key.offset, &alloc_hint);
3894 BUG_ON(ret);
3895
3896 for (i = 0; i < nr_extents; i++) {
3897 if (ext_offset >= new_extents[i].num_bytes) {
3898 ext_offset -= new_extents[i].num_bytes;
3899 continue;
3900 }
3901 extent_len = min(new_extents[i].num_bytes -
3902 ext_offset, num_bytes);
3903
3904 ret = btrfs_insert_empty_item(trans, root,
3905 path, &key,
3906 sizeof(*fi));
3907 BUG_ON(ret);
3908
3909 leaf = path->nodes[0];
3910 fi = btrfs_item_ptr(leaf, path->slots[0],
3911 struct btrfs_file_extent_item);
3912 btrfs_set_file_extent_generation(leaf, fi,
3913 trans->transid);
3914 btrfs_set_file_extent_type(leaf, fi,
3915 BTRFS_FILE_EXTENT_REG);
3916 btrfs_set_file_extent_disk_bytenr(leaf, fi,
3917 new_extents[i].disk_bytenr);
3918 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
3919 new_extents[i].disk_num_bytes);
3920 btrfs_set_file_extent_num_bytes(leaf, fi,
3921 extent_len);
3922 ext_offset += new_extents[i].offset;
3923 btrfs_set_file_extent_offset(leaf, fi,
3924 ext_offset);
3925 btrfs_mark_buffer_dirty(leaf);
3926
3927 btrfs_drop_extent_cache(inode, key.offset,
3928 key.offset + extent_len - 1, 0);
3929
3930 ret = btrfs_inc_extent_ref(trans, root,
3931 new_extents[i].disk_bytenr,
3932 new_extents[i].disk_num_bytes,
3933 leaf->start,
3934 root->root_key.objectid,
3935 trans->transid,
3936 key.objectid, key.offset);
3937 BUG_ON(ret);
3938 btrfs_release_path(root, path);
3939
3940 inode->i_blocks += extent_len >> 9;
3941
3942 ext_offset = 0;
3943 num_bytes -= extent_len;
3944 key.offset += extent_len;
3945
3946 if (num_bytes == 0)
3947 break;
3948 }
3949 BUG_ON(i >= nr_extents);
3950 }
3951
3952 if (extent_locked) {
3953 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
3954 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
3955 lock_end, GFP_NOFS);
3956 extent_locked = 0;
3957 }
3958skip:
3959 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS &&
3960 key.offset >= first_pos + extent_key->offset)
3961 break;
3962
3963 cond_resched();
3964 }
3965 ret = 0;
3966out:
3967 btrfs_release_path(root, path);
3968 if (inode) {
3969 mutex_unlock(&inode->i_mutex);
3970 if (extent_locked) {
3971 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
3972 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
3973 lock_end, GFP_NOFS);
3974 }
3975 iput(inode);
3976 }
3977 return ret;
3978}
3979
3980int btrfs_add_reloc_mapping(struct btrfs_root *root, u64 orig_bytenr,
3981 u64 num_bytes, u64 new_bytenr)
3982{
3983 set_extent_bits(&root->fs_info->reloc_mapping_tree,
3984 orig_bytenr, orig_bytenr + num_bytes - 1,
3985 EXTENT_LOCKED, GFP_NOFS);
3986 set_state_private(&root->fs_info->reloc_mapping_tree,
3987 orig_bytenr, new_bytenr);
3988 return 0;
3989}
3990
3991int btrfs_get_reloc_mapping(struct btrfs_root *root, u64 orig_bytenr,
3992 u64 num_bytes, u64 *new_bytenr)
3993{
3994 u64 bytenr;
3995 u64 cur_bytenr = orig_bytenr;
3996 u64 prev_bytenr = orig_bytenr;
3997 int ret;
3998
3999 while (1) {
4000 ret = get_state_private(&root->fs_info->reloc_mapping_tree,
4001 cur_bytenr, &bytenr);
4002 if (ret)
4003 break;
4004 prev_bytenr = cur_bytenr;
4005 cur_bytenr = bytenr;
4006 }
4007
4008 if (orig_bytenr == cur_bytenr)
4009 return -ENOENT;
4010
4011 if (prev_bytenr != orig_bytenr) {
4012 set_state_private(&root->fs_info->reloc_mapping_tree,
4013 orig_bytenr, cur_bytenr);
4014 }
4015 *new_bytenr = cur_bytenr;
4016 return 0;
4017}
4018
4019void btrfs_free_reloc_mappings(struct btrfs_root *root)
4020{
4021 clear_extent_bits(&root->fs_info->reloc_mapping_tree,
4022 0, (u64)-1, -1, GFP_NOFS);
4023}
4024
4025int btrfs_reloc_tree_cache_ref(struct btrfs_trans_handle *trans,
4026 struct btrfs_root *root,
4027 struct extent_buffer *buf, u64 orig_start)
4028{
4029 int level;
4030 int ret;
4031
4032 BUG_ON(btrfs_header_generation(buf) != trans->transid);
4033 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
4034
4035 level = btrfs_header_level(buf);
4036 if (level == 0) {
4037 struct btrfs_leaf_ref *ref;
4038 struct btrfs_leaf_ref *orig_ref;
4039
4040 orig_ref = btrfs_lookup_leaf_ref(root, orig_start);
4041 if (!orig_ref)
4042 return -ENOENT;
4043
4044 ref = btrfs_alloc_leaf_ref(root, orig_ref->nritems);
4045 if (!ref) {
4046 btrfs_free_leaf_ref(root, orig_ref);
4047 return -ENOMEM;
4048 }
4049
4050 ref->nritems = orig_ref->nritems;
4051 memcpy(ref->extents, orig_ref->extents,
4052 sizeof(ref->extents[0]) * ref->nritems);
4053
4054 btrfs_free_leaf_ref(root, orig_ref);
4055
4056 ref->root_gen = trans->transid;
4057 ref->bytenr = buf->start;
4058 ref->owner = btrfs_header_owner(buf);
4059 ref->generation = btrfs_header_generation(buf);
4060 ret = btrfs_add_leaf_ref(root, ref, 0);
4061 WARN_ON(ret);
4062 btrfs_free_leaf_ref(root, ref);
4063 }
4064 return 0;
4065}
4066
4067static int noinline invalidate_extent_cache(struct btrfs_root *root,
4068 struct extent_buffer *leaf,
4069 struct btrfs_block_group_cache *group,
4070 struct btrfs_root *target_root)
4071{
4072 struct btrfs_key key;
4073 struct inode *inode = NULL;
4074 struct btrfs_file_extent_item *fi;
4075 u64 num_bytes;
4076 u64 skip_objectid = 0;
4077 u32 nritems;
4078 u32 i;
4079
4080 nritems = btrfs_header_nritems(leaf);
4081 for (i = 0; i < nritems; i++) {
4082 btrfs_item_key_to_cpu(leaf, &key, i);
4083 if (key.objectid == skip_objectid ||
4084 key.type != BTRFS_EXTENT_DATA_KEY)
4085 continue;
4086 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
4087 if (btrfs_file_extent_type(leaf, fi) ==
4088 BTRFS_FILE_EXTENT_INLINE)
4089 continue;
4090 if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
4091 continue;
4092 if (!inode || inode->i_ino != key.objectid) {
4093 iput(inode);
4094 inode = btrfs_ilookup(target_root->fs_info->sb,
4095 key.objectid, target_root, 1);
4096 }
4097 if (!inode) {
4098 skip_objectid = key.objectid;
4099 continue;
4100 }
4101 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
4102
4103 lock_extent(&BTRFS_I(inode)->io_tree, key.offset,
4104 key.offset + num_bytes - 1, GFP_NOFS);
4105 mutex_lock(&BTRFS_I(inode)->extent_mutex);
4106 btrfs_drop_extent_cache(inode, key.offset,
4107 key.offset + num_bytes - 1, 1);
4108 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
4109 unlock_extent(&BTRFS_I(inode)->io_tree, key.offset,
4110 key.offset + num_bytes - 1, GFP_NOFS);
4111 cond_resched();
4112 }
4113 iput(inode);
4114 return 0;
4115}
4116
4117static int noinline replace_extents_in_leaf(struct btrfs_trans_handle *trans,
4118 struct btrfs_root *root,
4119 struct extent_buffer *leaf,
4120 struct btrfs_block_group_cache *group,
4121 struct inode *reloc_inode)
4122{
4123 struct btrfs_key key;
4124 struct btrfs_key extent_key;
4125 struct btrfs_file_extent_item *fi;
4126 struct btrfs_leaf_ref *ref;
4127 struct disk_extent *new_extent;
4128 u64 bytenr;
4129 u64 num_bytes;
4130 u32 nritems;
4131 u32 i;
4132 int ext_index;
4133 int nr_extent;
4134 int ret;
4135
4136 new_extent = kmalloc(sizeof(*new_extent), GFP_NOFS);
4137 BUG_ON(!new_extent);
4138
4139 ref = btrfs_lookup_leaf_ref(root, leaf->start);
4140 BUG_ON(!ref);
4141
4142 ext_index = -1;
4143 nritems = btrfs_header_nritems(leaf);
4144 for (i = 0; i < nritems; i++) {
4145 btrfs_item_key_to_cpu(leaf, &key, i);
4146 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
4147 continue;
4148 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
4149 if (btrfs_file_extent_type(leaf, fi) ==
4150 BTRFS_FILE_EXTENT_INLINE)
4151 continue;
4152 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
4153 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
4154 if (bytenr == 0)
4155 continue;
4156
4157 ext_index++;
4158 if (bytenr >= group->key.objectid + group->key.offset ||
4159 bytenr + num_bytes <= group->key.objectid)
4160 continue;
4161
4162 extent_key.objectid = bytenr;
4163 extent_key.offset = num_bytes;
4164 extent_key.type = BTRFS_EXTENT_ITEM_KEY;
4165 nr_extent = 1;
4166 ret = get_new_locations(reloc_inode, &extent_key,
4167 group->key.objectid, 1,
4168 &new_extent, &nr_extent);
4169 if (ret > 0)
4170 continue;
4171 BUG_ON(ret < 0);
4172
4173 BUG_ON(ref->extents[ext_index].bytenr != bytenr);
4174 BUG_ON(ref->extents[ext_index].num_bytes != num_bytes);
4175 ref->extents[ext_index].bytenr = new_extent->disk_bytenr;
4176 ref->extents[ext_index].num_bytes = new_extent->disk_num_bytes;
4177
4178 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
4179 btrfs_set_file_extent_disk_bytenr(leaf, fi,
4180 new_extent->disk_bytenr);
4181 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
4182 new_extent->disk_num_bytes);
4183 new_extent->offset += btrfs_file_extent_offset(leaf, fi);
4184 btrfs_set_file_extent_offset(leaf, fi, new_extent->offset);
4185 btrfs_mark_buffer_dirty(leaf);
4186
4187 ret = btrfs_inc_extent_ref(trans, root,
4188 new_extent->disk_bytenr,
4189 new_extent->disk_num_bytes,
4190 leaf->start,
4191 root->root_key.objectid,
4192 trans->transid,
4193 key.objectid, key.offset);
4194 BUG_ON(ret);
4195 ret = btrfs_free_extent(trans, root,
4196 bytenr, num_bytes, leaf->start,
4197 btrfs_header_owner(leaf),
4198 btrfs_header_generation(leaf),
4199 key.objectid, key.offset, 0);
4200 BUG_ON(ret);
4201 cond_resched();
4202 }
4203 kfree(new_extent);
4204 BUG_ON(ext_index + 1 != ref->nritems);
4205 btrfs_free_leaf_ref(root, ref);
4206 return 0;
4207}
4208
4209int btrfs_free_reloc_root(struct btrfs_root *root)
4210{
4211 struct btrfs_root *reloc_root;
4212
4213 if (root->reloc_root) {
4214 reloc_root = root->reloc_root;
4215 root->reloc_root = NULL;
4216 list_add(&reloc_root->dead_list,
4217 &root->fs_info->dead_reloc_roots);
4218 }
4219 return 0;
4220}
4221
4222int btrfs_drop_dead_reloc_roots(struct btrfs_root *root)
4223{
4224 struct btrfs_trans_handle *trans;
4225 struct btrfs_root *reloc_root;
4226 struct btrfs_root *prev_root = NULL;
4227 struct list_head dead_roots;
4228 int ret;
4229 unsigned long nr;
4230
4231 INIT_LIST_HEAD(&dead_roots);
4232 list_splice_init(&root->fs_info->dead_reloc_roots, &dead_roots);
4233
4234 while (!list_empty(&dead_roots)) {
4235 reloc_root = list_entry(dead_roots.prev,
4236 struct btrfs_root, dead_list);
4237 list_del_init(&reloc_root->dead_list);
4238
4239 BUG_ON(reloc_root->commit_root != NULL);
4240 while (1) {
4241 trans = btrfs_join_transaction(root, 1);
4242 BUG_ON(!trans);
4243
4244 mutex_lock(&root->fs_info->drop_mutex);
4245 ret = btrfs_drop_snapshot(trans, reloc_root);
4246 if (ret != -EAGAIN)
4247 break;
4248 mutex_unlock(&root->fs_info->drop_mutex);
4249
4250 nr = trans->blocks_used;
4251 ret = btrfs_end_transaction(trans, root);
4252 BUG_ON(ret);
4253 btrfs_btree_balance_dirty(root, nr);
4254 }
4255
4256 free_extent_buffer(reloc_root->node);
4257
4258 ret = btrfs_del_root(trans, root->fs_info->tree_root,
4259 &reloc_root->root_key);
4260 BUG_ON(ret);
4261 mutex_unlock(&root->fs_info->drop_mutex);
4262
4263 nr = trans->blocks_used;
4264 ret = btrfs_end_transaction(trans, root);
4265 BUG_ON(ret);
4266 btrfs_btree_balance_dirty(root, nr);
4267
4268 kfree(prev_root);
4269 prev_root = reloc_root;
4270 }
4271 if (prev_root) {
4272 btrfs_remove_leaf_refs(prev_root, (u64)-1, 0);
4273 kfree(prev_root);
4274 }
4275 return 0;
4276}
4277
4278int btrfs_add_dead_reloc_root(struct btrfs_root *root)
4279{
4280 list_add(&root->dead_list, &root->fs_info->dead_reloc_roots);
4281 return 0;
4282}
4283
4284int btrfs_cleanup_reloc_trees(struct btrfs_root *root)
4285{
4286 struct btrfs_root *reloc_root;
4287 struct btrfs_trans_handle *trans;
4288 struct btrfs_key location;
4289 int found;
4290 int ret;
4291
4292 mutex_lock(&root->fs_info->tree_reloc_mutex);
4293 ret = btrfs_find_dead_roots(root, BTRFS_TREE_RELOC_OBJECTID, NULL);
4294 BUG_ON(ret);
4295 found = !list_empty(&root->fs_info->dead_reloc_roots);
4296 mutex_unlock(&root->fs_info->tree_reloc_mutex);
4297
4298 if (found) {
4299 trans = btrfs_start_transaction(root, 1);
4300 BUG_ON(!trans);
4301 ret = btrfs_commit_transaction(trans, root);
4302 BUG_ON(ret);
4303 }
4304
4305 location.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
4306 location.offset = (u64)-1;
4307 location.type = BTRFS_ROOT_ITEM_KEY;
4308
4309 reloc_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
4310 BUG_ON(!reloc_root);
4311 btrfs_orphan_cleanup(reloc_root);
4312 return 0;
4313}
4314
4315static int noinline init_reloc_tree(struct btrfs_trans_handle *trans,
4316 struct btrfs_root *root)
4317{
4318 struct btrfs_root *reloc_root;
4319 struct extent_buffer *eb;
4320 struct btrfs_root_item *root_item;
4321 struct btrfs_key root_key;
4322 int ret;
4323
4324 BUG_ON(!root->ref_cows);
4325 if (root->reloc_root)
4326 return 0;
4327
4328 root_item = kmalloc(sizeof(*root_item), GFP_NOFS);
4329 BUG_ON(!root_item);
4330
4331 ret = btrfs_copy_root(trans, root, root->commit_root,
4332 &eb, BTRFS_TREE_RELOC_OBJECTID);
4333 BUG_ON(ret);
4334
4335 root_key.objectid = BTRFS_TREE_RELOC_OBJECTID;
4336 root_key.offset = root->root_key.objectid;
4337 root_key.type = BTRFS_ROOT_ITEM_KEY;
4338
4339 memcpy(root_item, &root->root_item, sizeof(root_item));
4340 btrfs_set_root_refs(root_item, 0);
4341 btrfs_set_root_bytenr(root_item, eb->start);
4342 btrfs_set_root_level(root_item, btrfs_header_level(eb));
4343 memset(&root_item->drop_progress, 0, sizeof(root_item->drop_progress));
4344 root_item->drop_level = 0;
4345
4346 btrfs_tree_unlock(eb);
4347 free_extent_buffer(eb);
4348
4349 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
4350 &root_key, root_item);
4351 BUG_ON(ret);
4352 kfree(root_item);
4353
4354 reloc_root = btrfs_read_fs_root_no_radix(root->fs_info->tree_root,
4355 &root_key);
4356 BUG_ON(!reloc_root);
4357 reloc_root->last_trans = trans->transid;
4358 reloc_root->commit_root = NULL;
4359 reloc_root->ref_tree = &root->fs_info->reloc_ref_tree;
4360
4361 root->reloc_root = reloc_root;
4362 return 0;
4363}
4364
4365/*
4366 * Core function of space balance.
4367 *
4368 * The idea is using reloc trees to relocate tree blocks in reference
4369 * counted roots. There is one reloc tree for each subvol, all reloc
4370 * trees share same key objectid. Reloc trees are snapshots of the
4371 * latest committed roots (subvol root->commit_root). To relocate a tree
4372 * block referenced by a subvol, the code COW the block through the reloc
4373 * tree, then update pointer in the subvol to point to the new block.
4374 * Since all reloc trees share same key objectid, we can easily do special
4375 * handing to share tree blocks between reloc trees. Once a tree block has
4376 * been COWed in one reloc tree, we can use the result when the same block
4377 * is COWed again through other reloc trees.
4378 */
4379static int noinline relocate_one_path(struct btrfs_trans_handle *trans,
4380 struct btrfs_root *root,
4381 struct btrfs_path *path,
4382 struct btrfs_key *first_key,
4383 struct btrfs_ref_path *ref_path,
4384 struct btrfs_block_group_cache *group,
4385 struct inode *reloc_inode)
4386{
4387 struct btrfs_root *reloc_root;
4388 struct extent_buffer *eb = NULL;
4389 struct btrfs_key *keys;
4390 u64 *nodes;
4391 int level;
4392 int lowest_merge;
4393 int lowest_level = 0;
4394 int update_refs;
4395 int ret;
4396
4397 if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
4398 lowest_level = ref_path->owner_objectid;
4399
4400 if (is_cowonly_root(ref_path->root_objectid)) {
4401 path->lowest_level = lowest_level;
4402 ret = btrfs_search_slot(trans, root, first_key, path, 0, 1);
4403 BUG_ON(ret < 0);
4404 path->lowest_level = 0;
4405 btrfs_release_path(root, path);
4406 return 0;
4407 }
4408
4409 keys = kzalloc(sizeof(*keys) * BTRFS_MAX_LEVEL, GFP_NOFS);
4410 BUG_ON(!keys);
4411 nodes = kzalloc(sizeof(*nodes) * BTRFS_MAX_LEVEL, GFP_NOFS);
4412 BUG_ON(!nodes);
4413
4414 mutex_lock(&root->fs_info->tree_reloc_mutex);
4415 ret = init_reloc_tree(trans, root);
4416 BUG_ON(ret);
4417 reloc_root = root->reloc_root;
4418
4419 path->lowest_level = lowest_level;
4420 ret = btrfs_search_slot(trans, reloc_root, first_key, path, 0, 0);
4421 BUG_ON(ret);
4422 /*
4423 * get relocation mapping for tree blocks in the path
4424 */
4425 lowest_merge = BTRFS_MAX_LEVEL;
4426 for (level = BTRFS_MAX_LEVEL - 1; level >= lowest_level; level--) {
4427 u64 new_bytenr;
4428 eb = path->nodes[level];
4429 if (!eb || eb == reloc_root->node)
4430 continue;
4431 ret = btrfs_get_reloc_mapping(reloc_root, eb->start, eb->len,
4432 &new_bytenr);
4433 if (ret)
4434 continue;
4435 if (level == 0)
4436 btrfs_item_key_to_cpu(eb, &keys[level], 0);
4437 else
4438 btrfs_node_key_to_cpu(eb, &keys[level], 0);
4439 nodes[level] = new_bytenr;
4440 lowest_merge = level;
4441 }
4442
4443 update_refs = 0;
4444 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
4445 eb = path->nodes[0];
4446 if (btrfs_header_generation(eb) < trans->transid)
4447 update_refs = 1;
4448 }
4449
4450 btrfs_release_path(reloc_root, path);
4451 /*
4452 * merge tree blocks that already relocated in other reloc trees
4453 */
4454 if (lowest_merge != BTRFS_MAX_LEVEL) {
4455 ret = btrfs_merge_path(trans, reloc_root, keys, nodes,
4456 lowest_merge);
4457 BUG_ON(ret < 0);
4458 }
4459 /*
4460 * cow any tree blocks that still haven't been relocated
4461 */
4462 ret = btrfs_search_slot(trans, reloc_root, first_key, path, 0, 1);
4463 BUG_ON(ret);
4464 /*
4465 * if we are relocating data block group, update extent pointers
4466 * in the newly created tree leaf.
4467 */
4468 eb = path->nodes[0];
4469 if (update_refs && nodes[0] != eb->start) {
4470 ret = replace_extents_in_leaf(trans, reloc_root, eb, group,
4471 reloc_inode);
4472 BUG_ON(ret);
4473 }
4474
4475 memset(keys, 0, sizeof(*keys) * BTRFS_MAX_LEVEL);
4476 memset(nodes, 0, sizeof(*nodes) * BTRFS_MAX_LEVEL);
4477 for (level = BTRFS_MAX_LEVEL - 1; level >= lowest_level; level--) {
4478 eb = path->nodes[level];
4479 if (!eb || eb == reloc_root->node)
4480 continue;
4481 BUG_ON(btrfs_header_owner(eb) != BTRFS_TREE_RELOC_OBJECTID);
4482 nodes[level] = eb->start;
4483 if (level == 0)
4484 btrfs_item_key_to_cpu(eb, &keys[level], 0);
4485 else
4486 btrfs_node_key_to_cpu(eb, &keys[level], 0);
4487 }
4488
4489 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
4490 eb = path->nodes[0];
4491 extent_buffer_get(eb);
4492 }
4493 btrfs_release_path(reloc_root, path);
4494 /*
4495 * replace tree blocks in the fs tree with tree blocks in
4496 * the reloc tree.
4497 */
4498 ret = btrfs_merge_path(trans, root, keys, nodes, lowest_level);
4499 BUG_ON(ret < 0);
4500
4501 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
4502 ret = invalidate_extent_cache(reloc_root, eb, group, root);
4503 BUG_ON(ret);
4504 free_extent_buffer(eb);
4505 }
4506 mutex_unlock(&root->fs_info->tree_reloc_mutex);
4507
4508 path->lowest_level = 0;
4509 kfree(nodes);
4510 kfree(keys);
4511 return 0;
4512}
4513
4514static int noinline relocate_tree_block(struct btrfs_trans_handle *trans,
4515 struct btrfs_root *root,
4516 struct btrfs_path *path,
4517 struct btrfs_key *first_key,
4518 struct btrfs_ref_path *ref_path)
4519{
4520 int ret;
4521 int needs_lock = 0;
4522
4523 if (root == root->fs_info->extent_root ||
4524 root == root->fs_info->chunk_root ||
4525 root == root->fs_info->dev_root) {
4526 needs_lock = 1;
4527 mutex_lock(&root->fs_info->alloc_mutex);
4528 }
4529
4530 ret = relocate_one_path(trans, root, path, first_key,
4531 ref_path, NULL, NULL);
4532 BUG_ON(ret);
4533
4534 if (root == root->fs_info->extent_root)
4535 btrfs_extent_post_op(trans, root);
4536 if (needs_lock)
4537 mutex_unlock(&root->fs_info->alloc_mutex);
4538
4539 return 0;
4540}
4541
4542static int noinline del_extent_zero(struct btrfs_trans_handle *trans,
4543 struct btrfs_root *extent_root,
4544 struct btrfs_path *path,
4545 struct btrfs_key *extent_key)
4546{
4547 int ret;
4548
4549 mutex_lock(&extent_root->fs_info->alloc_mutex);
4550 ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1);
4551 if (ret)
4552 goto out;
4553 ret = btrfs_del_item(trans, extent_root, path);
4554out:
Chris Masonedbd8d42007-12-21 16:27:24 -05004555 btrfs_release_path(extent_root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04004556 mutex_unlock(&extent_root->fs_info->alloc_mutex);
4557 return ret;
4558}
4559
4560static struct btrfs_root noinline *read_ref_root(struct btrfs_fs_info *fs_info,
4561 struct btrfs_ref_path *ref_path)
4562{
4563 struct btrfs_key root_key;
4564
4565 root_key.objectid = ref_path->root_objectid;
4566 root_key.type = BTRFS_ROOT_ITEM_KEY;
4567 if (is_cowonly_root(ref_path->root_objectid))
4568 root_key.offset = 0;
4569 else
4570 root_key.offset = (u64)-1;
4571
4572 return btrfs_read_fs_root_no_name(fs_info, &root_key);
4573}
4574
4575static int noinline relocate_one_extent(struct btrfs_root *extent_root,
4576 struct btrfs_path *path,
4577 struct btrfs_key *extent_key,
4578 struct btrfs_block_group_cache *group,
4579 struct inode *reloc_inode, int pass)
4580{
4581 struct btrfs_trans_handle *trans;
4582 struct btrfs_root *found_root;
4583 struct btrfs_ref_path *ref_path = NULL;
4584 struct disk_extent *new_extents = NULL;
4585 int nr_extents = 0;
4586 int loops;
4587 int ret;
4588 int level;
4589 struct btrfs_key first_key;
4590 u64 prev_block = 0;
4591
4592 mutex_unlock(&extent_root->fs_info->alloc_mutex);
4593
4594 trans = btrfs_start_transaction(extent_root, 1);
4595 BUG_ON(!trans);
4596
4597 if (extent_key->objectid == 0) {
4598 ret = del_extent_zero(trans, extent_root, path, extent_key);
4599 goto out;
4600 }
4601
4602 ref_path = kmalloc(sizeof(*ref_path), GFP_NOFS);
4603 if (!ref_path) {
4604 ret = -ENOMEM;
4605 goto out;
4606 }
4607
4608 for (loops = 0; ; loops++) {
4609 if (loops == 0) {
4610 ret = btrfs_first_ref_path(trans, extent_root, ref_path,
4611 extent_key->objectid);
4612 } else {
4613 ret = btrfs_next_ref_path(trans, extent_root, ref_path);
4614 }
4615 if (ret < 0)
4616 goto out;
4617 if (ret > 0)
4618 break;
4619
4620 if (ref_path->root_objectid == BTRFS_TREE_LOG_OBJECTID ||
4621 ref_path->root_objectid == BTRFS_TREE_RELOC_OBJECTID)
4622 continue;
4623
4624 found_root = read_ref_root(extent_root->fs_info, ref_path);
4625 BUG_ON(!found_root);
4626 /*
4627 * for reference counted tree, only process reference paths
4628 * rooted at the latest committed root.
4629 */
4630 if (found_root->ref_cows &&
4631 ref_path->root_generation != found_root->root_key.offset)
4632 continue;
4633
4634 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
4635 if (pass == 0) {
4636 /*
4637 * copy data extents to new locations
4638 */
4639 u64 group_start = group->key.objectid;
4640 ret = relocate_data_extent(reloc_inode,
4641 extent_key,
4642 group_start);
4643 if (ret < 0)
4644 goto out;
4645 break;
4646 }
4647 level = 0;
4648 } else {
4649 level = ref_path->owner_objectid;
4650 }
4651
4652 if (prev_block != ref_path->nodes[level]) {
4653 struct extent_buffer *eb;
4654 u64 block_start = ref_path->nodes[level];
4655 u64 block_size = btrfs_level_size(found_root, level);
4656
4657 eb = read_tree_block(found_root, block_start,
4658 block_size, 0);
4659 btrfs_tree_lock(eb);
4660 BUG_ON(level != btrfs_header_level(eb));
4661
4662 if (level == 0)
4663 btrfs_item_key_to_cpu(eb, &first_key, 0);
4664 else
4665 btrfs_node_key_to_cpu(eb, &first_key, 0);
4666
4667 btrfs_tree_unlock(eb);
4668 free_extent_buffer(eb);
4669 prev_block = block_start;
4670 }
4671
4672 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID &&
4673 pass >= 2) {
4674 /*
4675 * use fallback method to process the remaining
4676 * references.
4677 */
4678 if (!new_extents) {
4679 u64 group_start = group->key.objectid;
4680 ret = get_new_locations(reloc_inode,
4681 extent_key,
4682 group_start, 0,
4683 &new_extents,
4684 &nr_extents);
4685 if (ret < 0)
4686 goto out;
4687 }
4688 btrfs_record_root_in_trans(found_root);
4689 ret = replace_one_extent(trans, found_root,
4690 path, extent_key,
4691 &first_key, ref_path,
4692 new_extents, nr_extents);
4693 if (ret < 0)
4694 goto out;
4695 continue;
4696 }
4697
4698 btrfs_record_root_in_trans(found_root);
4699 if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
4700 ret = relocate_tree_block(trans, found_root, path,
4701 &first_key, ref_path);
4702 } else {
4703 /*
4704 * try to update data extent references while
4705 * keeping metadata shared between snapshots.
4706 */
4707 ret = relocate_one_path(trans, found_root, path,
4708 &first_key, ref_path,
4709 group, reloc_inode);
4710 }
4711 if (ret < 0)
4712 goto out;
4713 }
4714 ret = 0;
4715out:
4716 btrfs_end_transaction(trans, extent_root);
4717 kfree(new_extents);
4718 kfree(ref_path);
4719 mutex_lock(&extent_root->fs_info->alloc_mutex);
Chris Masonedbd8d42007-12-21 16:27:24 -05004720 return ret;
4721}
4722
Chris Masonec44a352008-04-28 15:29:52 -04004723static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
4724{
4725 u64 num_devices;
4726 u64 stripped = BTRFS_BLOCK_GROUP_RAID0 |
4727 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
4728
Chris Masona061fc82008-05-07 11:43:44 -04004729 num_devices = root->fs_info->fs_devices->num_devices;
Chris Masonec44a352008-04-28 15:29:52 -04004730 if (num_devices == 1) {
4731 stripped |= BTRFS_BLOCK_GROUP_DUP;
4732 stripped = flags & ~stripped;
4733
4734 /* turn raid0 into single device chunks */
4735 if (flags & BTRFS_BLOCK_GROUP_RAID0)
4736 return stripped;
4737
4738 /* turn mirroring into duplication */
4739 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
4740 BTRFS_BLOCK_GROUP_RAID10))
4741 return stripped | BTRFS_BLOCK_GROUP_DUP;
4742 return flags;
4743 } else {
4744 /* they already had raid on here, just return */
Chris Masonec44a352008-04-28 15:29:52 -04004745 if (flags & stripped)
4746 return flags;
4747
4748 stripped |= BTRFS_BLOCK_GROUP_DUP;
4749 stripped = flags & ~stripped;
4750
4751 /* switch duplicated blocks with raid1 */
4752 if (flags & BTRFS_BLOCK_GROUP_DUP)
4753 return stripped | BTRFS_BLOCK_GROUP_RAID1;
4754
4755 /* turn single device chunks into raid0 */
4756 return stripped | BTRFS_BLOCK_GROUP_RAID0;
4757 }
4758 return flags;
4759}
4760
Chris Mason0ef3e662008-05-24 14:04:53 -04004761int __alloc_chunk_for_shrink(struct btrfs_root *root,
4762 struct btrfs_block_group_cache *shrink_block_group,
4763 int force)
4764{
4765 struct btrfs_trans_handle *trans;
4766 u64 new_alloc_flags;
4767 u64 calc;
4768
Chris Masonc286ac42008-07-22 23:06:41 -04004769 spin_lock(&shrink_block_group->lock);
Chris Mason0ef3e662008-05-24 14:04:53 -04004770 if (btrfs_block_group_used(&shrink_block_group->item) > 0) {
Chris Masonc286ac42008-07-22 23:06:41 -04004771 spin_unlock(&shrink_block_group->lock);
Chris Mason7d9eb122008-07-08 14:19:17 -04004772 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Masonc286ac42008-07-22 23:06:41 -04004773
Chris Mason0ef3e662008-05-24 14:04:53 -04004774 trans = btrfs_start_transaction(root, 1);
Chris Mason7d9eb122008-07-08 14:19:17 -04004775 mutex_lock(&root->fs_info->alloc_mutex);
Chris Masonc286ac42008-07-22 23:06:41 -04004776 spin_lock(&shrink_block_group->lock);
Chris Mason7d9eb122008-07-08 14:19:17 -04004777
Chris Mason0ef3e662008-05-24 14:04:53 -04004778 new_alloc_flags = update_block_group_flags(root,
4779 shrink_block_group->flags);
4780 if (new_alloc_flags != shrink_block_group->flags) {
4781 calc =
4782 btrfs_block_group_used(&shrink_block_group->item);
4783 } else {
4784 calc = shrink_block_group->key.offset;
4785 }
Chris Masonc286ac42008-07-22 23:06:41 -04004786 spin_unlock(&shrink_block_group->lock);
4787
Chris Mason0ef3e662008-05-24 14:04:53 -04004788 do_chunk_alloc(trans, root->fs_info->extent_root,
4789 calc + 2 * 1024 * 1024, new_alloc_flags, force);
Chris Mason7d9eb122008-07-08 14:19:17 -04004790
4791 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Mason0ef3e662008-05-24 14:04:53 -04004792 btrfs_end_transaction(trans, root);
Chris Mason7d9eb122008-07-08 14:19:17 -04004793 mutex_lock(&root->fs_info->alloc_mutex);
Chris Masonc286ac42008-07-22 23:06:41 -04004794 } else
4795 spin_unlock(&shrink_block_group->lock);
Chris Mason0ef3e662008-05-24 14:04:53 -04004796 return 0;
4797}
4798
Zheng Yan1a40e232008-09-26 10:09:34 -04004799static int __insert_orphan_inode(struct btrfs_trans_handle *trans,
4800 struct btrfs_root *root,
4801 u64 objectid, u64 size)
4802{
4803 struct btrfs_path *path;
4804 struct btrfs_inode_item *item;
4805 struct extent_buffer *leaf;
4806 int ret;
4807
4808 path = btrfs_alloc_path();
4809 if (!path)
4810 return -ENOMEM;
4811
4812 ret = btrfs_insert_empty_inode(trans, root, path, objectid);
4813 if (ret)
4814 goto out;
4815
4816 leaf = path->nodes[0];
4817 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_inode_item);
4818 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
4819 btrfs_set_inode_generation(leaf, item, 1);
4820 btrfs_set_inode_size(leaf, item, size);
4821 btrfs_set_inode_mode(leaf, item, S_IFREG | 0600);
4822 btrfs_set_inode_flags(leaf, item, BTRFS_INODE_NODATASUM);
4823 btrfs_mark_buffer_dirty(leaf);
4824 btrfs_release_path(root, path);
4825out:
4826 btrfs_free_path(path);
4827 return ret;
4828}
4829
4830static struct inode noinline *create_reloc_inode(struct btrfs_fs_info *fs_info,
4831 struct btrfs_block_group_cache *group)
4832{
4833 struct inode *inode = NULL;
4834 struct btrfs_trans_handle *trans;
4835 struct btrfs_root *root;
4836 struct btrfs_key root_key;
4837 u64 objectid = BTRFS_FIRST_FREE_OBJECTID;
4838 int err = 0;
4839
4840 root_key.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
4841 root_key.type = BTRFS_ROOT_ITEM_KEY;
4842 root_key.offset = (u64)-1;
4843 root = btrfs_read_fs_root_no_name(fs_info, &root_key);
4844 if (IS_ERR(root))
4845 return ERR_CAST(root);
4846
4847 trans = btrfs_start_transaction(root, 1);
4848 BUG_ON(!trans);
4849
4850 err = btrfs_find_free_objectid(trans, root, objectid, &objectid);
4851 if (err)
4852 goto out;
4853
4854 err = __insert_orphan_inode(trans, root, objectid, group->key.offset);
4855 BUG_ON(err);
4856
4857 err = btrfs_insert_file_extent(trans, root, objectid, 0, 0, 0,
4858 group->key.offset, 0);
4859 BUG_ON(err);
4860
4861 inode = btrfs_iget_locked(root->fs_info->sb, objectid, root);
4862 if (inode->i_state & I_NEW) {
4863 BTRFS_I(inode)->root = root;
4864 BTRFS_I(inode)->location.objectid = objectid;
4865 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
4866 BTRFS_I(inode)->location.offset = 0;
4867 btrfs_read_locked_inode(inode);
4868 unlock_new_inode(inode);
4869 BUG_ON(is_bad_inode(inode));
4870 } else {
4871 BUG_ON(1);
4872 }
4873
4874 err = btrfs_orphan_add(trans, inode);
4875out:
4876 btrfs_end_transaction(trans, root);
4877 if (err) {
4878 if (inode)
4879 iput(inode);
4880 inode = ERR_PTR(err);
4881 }
4882 return inode;
4883}
4884
4885int btrfs_relocate_block_group(struct btrfs_root *root, u64 group_start)
Chris Masonedbd8d42007-12-21 16:27:24 -05004886{
4887 struct btrfs_trans_handle *trans;
Chris Masonedbd8d42007-12-21 16:27:24 -05004888 struct btrfs_path *path;
Zheng Yan1a40e232008-09-26 10:09:34 -04004889 struct btrfs_fs_info *info = root->fs_info;
4890 struct extent_buffer *leaf;
4891 struct inode *reloc_inode;
4892 struct btrfs_block_group_cache *block_group;
4893 struct btrfs_key key;
Chris Masonedbd8d42007-12-21 16:27:24 -05004894 u64 cur_byte;
4895 u64 total_found;
Chris Masonedbd8d42007-12-21 16:27:24 -05004896 u32 nritems;
4897 int ret;
Chris Masona061fc82008-05-07 11:43:44 -04004898 int progress;
Zheng Yan1a40e232008-09-26 10:09:34 -04004899 int pass = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05004900
Chris Masonedbd8d42007-12-21 16:27:24 -05004901 root = root->fs_info->extent_root;
Zheng Yan1a40e232008-09-26 10:09:34 -04004902
4903 block_group = btrfs_lookup_block_group(info, group_start);
4904 BUG_ON(!block_group);
Chris Masonedbd8d42007-12-21 16:27:24 -05004905
Chris Mason323da792008-05-09 11:46:48 -04004906 printk("btrfs relocating block group %llu flags %llu\n",
Zheng Yan1a40e232008-09-26 10:09:34 -04004907 (unsigned long long)block_group->key.objectid,
4908 (unsigned long long)block_group->flags);
Chris Mason323da792008-05-09 11:46:48 -04004909
Zheng Yan1a40e232008-09-26 10:09:34 -04004910 path = btrfs_alloc_path();
4911 BUG_ON(!path);
Chris Mason323da792008-05-09 11:46:48 -04004912
Zheng Yan1a40e232008-09-26 10:09:34 -04004913 reloc_inode = create_reloc_inode(info, block_group);
4914 BUG_ON(IS_ERR(reloc_inode));
4915
4916 mutex_lock(&root->fs_info->alloc_mutex);
4917
4918 __alloc_chunk_for_shrink(root, block_group, 1);
4919 block_group->ro = 1;
4920 block_group->space_info->total_bytes -= block_group->key.offset;
4921
4922 mutex_unlock(&root->fs_info->alloc_mutex);
4923
4924 btrfs_start_delalloc_inodes(info->tree_root);
4925 btrfs_wait_ordered_extents(info->tree_root, 0);
Chris Mason0ef3e662008-05-24 14:04:53 -04004926again:
Chris Masonedbd8d42007-12-21 16:27:24 -05004927 total_found = 0;
Chris Masona061fc82008-05-07 11:43:44 -04004928 progress = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04004929 key.objectid = block_group->key.objectid;
Chris Masonedbd8d42007-12-21 16:27:24 -05004930 key.offset = 0;
4931 key.type = 0;
Yan73e48b22008-01-03 14:14:39 -05004932 cur_byte = key.objectid;
Chris Mason4313b392008-01-03 09:08:48 -05004933
Zheng Yan1a40e232008-09-26 10:09:34 -04004934 trans = btrfs_start_transaction(info->tree_root, 1);
4935 btrfs_commit_transaction(trans, info->tree_root);
Chris Masonea8c2812008-08-04 23:17:27 -04004936
Zheng Yan1a40e232008-09-26 10:09:34 -04004937 mutex_lock(&root->fs_info->cleaner_mutex);
4938 btrfs_clean_old_snapshots(info->tree_root);
4939 btrfs_remove_leaf_refs(info->tree_root, (u64)-1, 1);
4940 mutex_unlock(&root->fs_info->cleaner_mutex);
Chris Masonea8c2812008-08-04 23:17:27 -04004941
4942 mutex_lock(&root->fs_info->alloc_mutex);
4943
Yan73e48b22008-01-03 14:14:39 -05004944 while(1) {
Chris Masonedbd8d42007-12-21 16:27:24 -05004945 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4946 if (ret < 0)
4947 goto out;
Chris Mason7d9eb122008-07-08 14:19:17 -04004948next:
Chris Masonedbd8d42007-12-21 16:27:24 -05004949 leaf = path->nodes[0];
Chris Masonedbd8d42007-12-21 16:27:24 -05004950 nritems = btrfs_header_nritems(leaf);
Yan73e48b22008-01-03 14:14:39 -05004951 if (path->slots[0] >= nritems) {
4952 ret = btrfs_next_leaf(root, path);
4953 if (ret < 0)
4954 goto out;
4955 if (ret == 1) {
4956 ret = 0;
4957 break;
Chris Masonedbd8d42007-12-21 16:27:24 -05004958 }
Yan73e48b22008-01-03 14:14:39 -05004959 leaf = path->nodes[0];
4960 nritems = btrfs_header_nritems(leaf);
4961 }
4962
Zheng Yan1a40e232008-09-26 10:09:34 -04004963 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Chris Mason725c8462008-01-04 16:47:16 -05004964
Zheng Yan1a40e232008-09-26 10:09:34 -04004965 if (key.objectid >= block_group->key.objectid +
4966 block_group->key.offset)
Chris Mason8f18cf12008-04-25 16:53:30 -04004967 break;
4968
Chris Mason725c8462008-01-04 16:47:16 -05004969 if (progress && need_resched()) {
Chris Mason725c8462008-01-04 16:47:16 -05004970 btrfs_release_path(root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04004971 mutex_unlock(&root->fs_info->alloc_mutex);
4972 cond_resched();
4973 mutex_lock(&root->fs_info->alloc_mutex);
Chris Mason725c8462008-01-04 16:47:16 -05004974 progress = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04004975 continue;
Chris Mason725c8462008-01-04 16:47:16 -05004976 }
4977 progress = 1;
4978
Zheng Yan1a40e232008-09-26 10:09:34 -04004979 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY ||
4980 key.objectid + key.offset <= cur_byte) {
Yan73e48b22008-01-03 14:14:39 -05004981 path->slots[0]++;
Chris Masonedbd8d42007-12-21 16:27:24 -05004982 goto next;
4983 }
Yan73e48b22008-01-03 14:14:39 -05004984
Chris Masonedbd8d42007-12-21 16:27:24 -05004985 total_found++;
Zheng Yan1a40e232008-09-26 10:09:34 -04004986 cur_byte = key.objectid + key.offset;
Chris Masonedbd8d42007-12-21 16:27:24 -05004987 btrfs_release_path(root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04004988
4989 __alloc_chunk_for_shrink(root, block_group, 0);
4990 ret = relocate_one_extent(root, path, &key, block_group,
4991 reloc_inode, pass);
4992 BUG_ON(ret < 0);
4993
4994 key.objectid = cur_byte;
4995 key.type = 0;
4996 key.offset = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05004997 }
4998
4999 btrfs_release_path(root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04005000 mutex_unlock(&root->fs_info->alloc_mutex);
5001
5002 if (pass == 0) {
5003 btrfs_wait_ordered_range(reloc_inode, 0, (u64)-1);
5004 invalidate_mapping_pages(reloc_inode->i_mapping, 0, -1);
5005 WARN_ON(reloc_inode->i_mapping->nrpages);
5006 }
Chris Masonedbd8d42007-12-21 16:27:24 -05005007
5008 if (total_found > 0) {
Zheng Yan1a40e232008-09-26 10:09:34 -04005009 printk("btrfs found %llu extents in pass %d\n",
5010 (unsigned long long)total_found, pass);
5011 pass++;
Chris Masonedbd8d42007-12-21 16:27:24 -05005012 goto again;
5013 }
5014
Zheng Yan1a40e232008-09-26 10:09:34 -04005015 /* delete reloc_inode */
5016 iput(reloc_inode);
Chris Mason7d9eb122008-07-08 14:19:17 -04005017
Zheng Yan1a40e232008-09-26 10:09:34 -04005018 /* unpin extents in this range */
5019 trans = btrfs_start_transaction(info->tree_root, 1);
5020 btrfs_commit_transaction(trans, info->tree_root);
Chris Mason0ef3e662008-05-24 14:04:53 -04005021
Chris Mason7d9eb122008-07-08 14:19:17 -04005022 mutex_lock(&root->fs_info->alloc_mutex);
5023
Zheng Yan1a40e232008-09-26 10:09:34 -04005024 spin_lock(&block_group->lock);
5025 WARN_ON(block_group->pinned > 0);
5026 WARN_ON(block_group->reserved > 0);
5027 WARN_ON(btrfs_block_group_used(&block_group->item) > 0);
5028 spin_unlock(&block_group->lock);
5029 ret = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05005030out:
Chris Mason925baed2008-06-25 16:01:30 -04005031 mutex_unlock(&root->fs_info->alloc_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04005032 btrfs_free_path(path);
Chris Masonedbd8d42007-12-21 16:27:24 -05005033 return ret;
5034}
5035
Chris Mason0b86a832008-03-24 15:01:56 -04005036int find_first_block_group(struct btrfs_root *root, struct btrfs_path *path,
5037 struct btrfs_key *key)
5038{
Chris Mason925baed2008-06-25 16:01:30 -04005039 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005040 struct btrfs_key found_key;
5041 struct extent_buffer *leaf;
5042 int slot;
5043
5044 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
5045 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04005046 goto out;
5047
Chris Mason0b86a832008-03-24 15:01:56 -04005048 while(1) {
5049 slot = path->slots[0];
5050 leaf = path->nodes[0];
5051 if (slot >= btrfs_header_nritems(leaf)) {
5052 ret = btrfs_next_leaf(root, path);
5053 if (ret == 0)
5054 continue;
5055 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04005056 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04005057 break;
5058 }
5059 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5060
5061 if (found_key.objectid >= key->objectid &&
Chris Mason925baed2008-06-25 16:01:30 -04005062 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
5063 ret = 0;
5064 goto out;
5065 }
Chris Mason0b86a832008-03-24 15:01:56 -04005066 path->slots[0]++;
5067 }
5068 ret = -ENOENT;
Chris Mason925baed2008-06-25 16:01:30 -04005069out:
Chris Mason0b86a832008-03-24 15:01:56 -04005070 return ret;
5071}
5072
Zheng Yan1a40e232008-09-26 10:09:34 -04005073int btrfs_free_block_groups(struct btrfs_fs_info *info)
5074{
5075 struct btrfs_block_group_cache *block_group;
5076 struct rb_node *n;
5077
5078 mutex_lock(&info->alloc_mutex);
5079 spin_lock(&info->block_group_cache_lock);
5080 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
5081 block_group = rb_entry(n, struct btrfs_block_group_cache,
5082 cache_node);
5083
5084 spin_unlock(&info->block_group_cache_lock);
5085 btrfs_remove_free_space_cache(block_group);
5086 spin_lock(&info->block_group_cache_lock);
5087
5088 rb_erase(&block_group->cache_node,
5089 &info->block_group_cache_tree);
5090 spin_lock(&block_group->space_info->lock);
5091 list_del(&block_group->list);
5092 spin_unlock(&block_group->space_info->lock);
5093 kfree(block_group);
5094 }
5095 spin_unlock(&info->block_group_cache_lock);
5096 mutex_unlock(&info->alloc_mutex);
5097 return 0;
5098}
5099
Chris Mason9078a3e2007-04-26 16:46:15 -04005100int btrfs_read_block_groups(struct btrfs_root *root)
5101{
5102 struct btrfs_path *path;
5103 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04005104 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04005105 struct btrfs_fs_info *info = root->fs_info;
Chris Mason6324fbf2008-03-24 15:01:59 -04005106 struct btrfs_space_info *space_info;
Chris Mason9078a3e2007-04-26 16:46:15 -04005107 struct btrfs_key key;
5108 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -04005109 struct extent_buffer *leaf;
Chris Mason96b51792007-10-15 16:15:19 -04005110
Chris Masonbe744172007-05-06 10:15:01 -04005111 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04005112 key.objectid = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005113 key.offset = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04005114 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
Chris Mason9078a3e2007-04-26 16:46:15 -04005115 path = btrfs_alloc_path();
5116 if (!path)
5117 return -ENOMEM;
5118
Chris Mason925baed2008-06-25 16:01:30 -04005119 mutex_lock(&root->fs_info->alloc_mutex);
Chris Mason9078a3e2007-04-26 16:46:15 -04005120 while(1) {
Chris Mason0b86a832008-03-24 15:01:56 -04005121 ret = find_first_block_group(root, path, &key);
5122 if (ret > 0) {
5123 ret = 0;
5124 goto error;
Chris Mason9078a3e2007-04-26 16:46:15 -04005125 }
Chris Mason0b86a832008-03-24 15:01:56 -04005126 if (ret != 0)
5127 goto error;
5128
Chris Mason5f39d392007-10-15 16:14:19 -04005129 leaf = path->nodes[0];
5130 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason8f18cf12008-04-25 16:53:30 -04005131 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -04005132 if (!cache) {
Chris Mason0b86a832008-03-24 15:01:56 -04005133 ret = -ENOMEM;
Chris Mason9078a3e2007-04-26 16:46:15 -04005134 break;
5135 }
Chris Mason3e1ad542007-05-07 20:03:49 -04005136
Chris Masonc286ac42008-07-22 23:06:41 -04005137 spin_lock_init(&cache->lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005138 INIT_LIST_HEAD(&cache->list);
Chris Mason5f39d392007-10-15 16:14:19 -04005139 read_extent_buffer(leaf, &cache->item,
5140 btrfs_item_ptr_offset(leaf, path->slots[0]),
5141 sizeof(cache->item));
Chris Mason9078a3e2007-04-26 16:46:15 -04005142 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Mason0b86a832008-03-24 15:01:56 -04005143
Chris Mason9078a3e2007-04-26 16:46:15 -04005144 key.objectid = found_key.objectid + found_key.offset;
5145 btrfs_release_path(root, path);
Chris Mason0b86a832008-03-24 15:01:56 -04005146 cache->flags = btrfs_block_group_flags(&cache->item);
Chris Mason96b51792007-10-15 16:15:19 -04005147
Chris Mason6324fbf2008-03-24 15:01:59 -04005148 ret = update_space_info(info, cache->flags, found_key.offset,
5149 btrfs_block_group_used(&cache->item),
5150 &space_info);
5151 BUG_ON(ret);
5152 cache->space_info = space_info;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005153 spin_lock(&space_info->lock);
5154 list_add(&cache->list, &space_info->block_groups);
5155 spin_unlock(&space_info->lock);
Chris Mason6324fbf2008-03-24 15:01:59 -04005156
Josef Bacik0f9dd462008-09-23 13:14:11 -04005157 ret = btrfs_add_block_group_cache(root->fs_info, cache);
5158 BUG_ON(ret);
Chris Mason75ccf472008-09-30 19:24:06 -04005159
5160 set_avail_alloc_bits(root->fs_info, cache->flags);
Chris Mason9078a3e2007-04-26 16:46:15 -04005161 }
Chris Mason0b86a832008-03-24 15:01:56 -04005162 ret = 0;
5163error:
Chris Mason9078a3e2007-04-26 16:46:15 -04005164 btrfs_free_path(path);
Chris Mason925baed2008-06-25 16:01:30 -04005165 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Mason0b86a832008-03-24 15:01:56 -04005166 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04005167}
Chris Mason6324fbf2008-03-24 15:01:59 -04005168
5169int btrfs_make_block_group(struct btrfs_trans_handle *trans,
5170 struct btrfs_root *root, u64 bytes_used,
Chris Masone17cade2008-04-15 15:41:47 -04005171 u64 type, u64 chunk_objectid, u64 chunk_offset,
Chris Mason6324fbf2008-03-24 15:01:59 -04005172 u64 size)
5173{
5174 int ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04005175 struct btrfs_root *extent_root;
5176 struct btrfs_block_group_cache *cache;
Chris Mason6324fbf2008-03-24 15:01:59 -04005177
Chris Mason7d9eb122008-07-08 14:19:17 -04005178 WARN_ON(!mutex_is_locked(&root->fs_info->alloc_mutex));
Chris Mason6324fbf2008-03-24 15:01:59 -04005179 extent_root = root->fs_info->extent_root;
Chris Mason6324fbf2008-03-24 15:01:59 -04005180
Chris Masone02119d2008-09-05 16:13:11 -04005181 root->fs_info->last_trans_new_blockgroup = trans->transid;
5182
Chris Mason8f18cf12008-04-25 16:53:30 -04005183 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005184 if (!cache)
5185 return -ENOMEM;
5186
Chris Masone17cade2008-04-15 15:41:47 -04005187 cache->key.objectid = chunk_offset;
Chris Mason6324fbf2008-03-24 15:01:59 -04005188 cache->key.offset = size;
Chris Masonc286ac42008-07-22 23:06:41 -04005189 spin_lock_init(&cache->lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005190 INIT_LIST_HEAD(&cache->list);
Chris Mason6324fbf2008-03-24 15:01:59 -04005191 btrfs_set_key_type(&cache->key, BTRFS_BLOCK_GROUP_ITEM_KEY);
Chris Mason0ef3e662008-05-24 14:04:53 -04005192
Chris Mason6324fbf2008-03-24 15:01:59 -04005193 btrfs_set_block_group_used(&cache->item, bytes_used);
Chris Mason6324fbf2008-03-24 15:01:59 -04005194 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
5195 cache->flags = type;
5196 btrfs_set_block_group_flags(&cache->item, type);
5197
5198 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
5199 &cache->space_info);
5200 BUG_ON(ret);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005201 spin_lock(&cache->space_info->lock);
5202 list_add(&cache->list, &cache->space_info->block_groups);
5203 spin_unlock(&cache->space_info->lock);
Chris Mason6324fbf2008-03-24 15:01:59 -04005204
Josef Bacik0f9dd462008-09-23 13:14:11 -04005205 ret = btrfs_add_block_group_cache(root->fs_info, cache);
5206 BUG_ON(ret);
Chris Masonc286ac42008-07-22 23:06:41 -04005207
Chris Mason6324fbf2008-03-24 15:01:59 -04005208 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
5209 sizeof(cache->item));
5210 BUG_ON(ret);
5211
5212 finish_current_insert(trans, extent_root);
5213 ret = del_pending_extents(trans, extent_root);
5214 BUG_ON(ret);
Chris Masond18a2c42008-04-04 15:40:00 -04005215 set_avail_alloc_bits(extent_root->fs_info, type);
Chris Mason925baed2008-06-25 16:01:30 -04005216
Chris Mason6324fbf2008-03-24 15:01:59 -04005217 return 0;
5218}
Zheng Yan1a40e232008-09-26 10:09:34 -04005219
5220int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
5221 struct btrfs_root *root, u64 group_start)
5222{
5223 struct btrfs_path *path;
5224 struct btrfs_block_group_cache *block_group;
5225 struct btrfs_key key;
5226 int ret;
5227
5228 BUG_ON(!mutex_is_locked(&root->fs_info->alloc_mutex));
5229 root = root->fs_info->extent_root;
5230
5231 block_group = btrfs_lookup_block_group(root->fs_info, group_start);
5232 BUG_ON(!block_group);
5233
5234 memcpy(&key, &block_group->key, sizeof(key));
5235
5236 path = btrfs_alloc_path();
5237 BUG_ON(!path);
5238
5239 btrfs_remove_free_space_cache(block_group);
5240 rb_erase(&block_group->cache_node,
5241 &root->fs_info->block_group_cache_tree);
5242 spin_lock(&block_group->space_info->lock);
5243 list_del(&block_group->list);
5244 spin_unlock(&block_group->space_info->lock);
5245
5246 /*
5247 memset(shrink_block_group, 0, sizeof(*shrink_block_group));
5248 kfree(shrink_block_group);
5249 */
5250
5251 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
5252 if (ret > 0)
5253 ret = -EIO;
5254 if (ret < 0)
5255 goto out;
5256
5257 ret = btrfs_del_item(trans, root, path);
5258out:
5259 btrfs_free_path(path);
5260 return ret;
5261}