blob: edc7d208c5ce6032fc7de520f5f4a00739755fec [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 Masonb7a9f292009-02-04 09:23:45 -050022#include <linux/sort.h>
Chris Mason4184ea72009-03-10 12:39:20 -040023#include <linux/rcupdate.h>
Chris Mason4b4e25f2008-11-20 10:22:27 -050024#include "compat.h"
Chris Mason74493f72007-12-11 09:25:06 -050025#include "hash.h"
Chris Masonfec577f2007-02-26 10:40:21 -050026#include "ctree.h"
27#include "disk-io.h"
28#include "print-tree.h"
Chris Masone089f052007-03-16 16:20:31 -040029#include "transaction.h"
Chris Mason0b86a832008-03-24 15:01:56 -040030#include "volumes.h"
Chris Mason925baed2008-06-25 16:01:30 -040031#include "locking.h"
Chris Masonfa9c0d792009-04-03 09:47:43 -040032#include "free-space-cache.h"
Chris Masonfec577f2007-02-26 10:40:21 -050033
Chris Mason56bec292009-03-13 10:10:06 -040034static int update_reserved_extents(struct btrfs_root *root,
35 u64 bytenr, u64 num, int reserve);
Josef Bacikf3465ca2008-11-12 14:19:50 -050036static int update_block_group(struct btrfs_trans_handle *trans,
37 struct btrfs_root *root,
38 u64 bytenr, u64 num_bytes, int alloc,
39 int mark_free);
Yan Zheng5d4f98a2009-06-10 10:45:14 -040040static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
41 struct btrfs_root *root,
42 u64 bytenr, u64 num_bytes, u64 parent,
43 u64 root_objectid, u64 owner_objectid,
44 u64 owner_offset, int refs_to_drop,
45 struct btrfs_delayed_extent_op *extra_op);
46static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
47 struct extent_buffer *leaf,
48 struct btrfs_extent_item *ei);
49static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
50 struct btrfs_root *root,
51 u64 parent, u64 root_objectid,
52 u64 flags, u64 owner, u64 offset,
53 struct btrfs_key *ins, int ref_mod);
54static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
55 struct btrfs_root *root,
56 u64 parent, u64 root_objectid,
57 u64 flags, struct btrfs_disk_key *key,
58 int level, struct btrfs_key *ins);
Yand548ee52008-01-03 13:56:30 -050059
Josef Bacik6a632092009-02-20 11:00:09 -050060static int do_chunk_alloc(struct btrfs_trans_handle *trans,
61 struct btrfs_root *extent_root, u64 alloc_bytes,
62 u64 flags, int force);
63
Josef Bacik0f9dd462008-09-23 13:14:11 -040064static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
65{
66 return (cache->flags & bits) == bits;
67}
68
69/*
70 * this adds the block group to the fs_info rb tree for the block group
71 * cache
72 */
Christoph Hellwigb2950862008-12-02 09:54:17 -050073static int btrfs_add_block_group_cache(struct btrfs_fs_info *info,
Josef Bacik0f9dd462008-09-23 13:14:11 -040074 struct btrfs_block_group_cache *block_group)
75{
76 struct rb_node **p;
77 struct rb_node *parent = NULL;
78 struct btrfs_block_group_cache *cache;
79
80 spin_lock(&info->block_group_cache_lock);
81 p = &info->block_group_cache_tree.rb_node;
82
83 while (*p) {
84 parent = *p;
85 cache = rb_entry(parent, struct btrfs_block_group_cache,
86 cache_node);
87 if (block_group->key.objectid < cache->key.objectid) {
88 p = &(*p)->rb_left;
89 } else if (block_group->key.objectid > cache->key.objectid) {
90 p = &(*p)->rb_right;
91 } else {
92 spin_unlock(&info->block_group_cache_lock);
93 return -EEXIST;
94 }
95 }
96
97 rb_link_node(&block_group->cache_node, parent, p);
98 rb_insert_color(&block_group->cache_node,
99 &info->block_group_cache_tree);
100 spin_unlock(&info->block_group_cache_lock);
101
102 return 0;
103}
104
105/*
106 * This will return the block group at or after bytenr if contains is 0, else
107 * it will return the block group that contains the bytenr
108 */
109static struct btrfs_block_group_cache *
110block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr,
111 int contains)
112{
113 struct btrfs_block_group_cache *cache, *ret = NULL;
114 struct rb_node *n;
115 u64 end, start;
116
117 spin_lock(&info->block_group_cache_lock);
118 n = info->block_group_cache_tree.rb_node;
119
120 while (n) {
121 cache = rb_entry(n, struct btrfs_block_group_cache,
122 cache_node);
123 end = cache->key.objectid + cache->key.offset - 1;
124 start = cache->key.objectid;
125
126 if (bytenr < start) {
127 if (!contains && (!ret || start < ret->key.objectid))
128 ret = cache;
129 n = n->rb_left;
130 } else if (bytenr > start) {
131 if (contains && bytenr <= end) {
132 ret = cache;
133 break;
134 }
135 n = n->rb_right;
136 } else {
137 ret = cache;
138 break;
139 }
140 }
Yan Zhengd2fb3432008-12-11 16:30:39 -0500141 if (ret)
142 atomic_inc(&ret->count);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400143 spin_unlock(&info->block_group_cache_lock);
144
145 return ret;
146}
147
148/*
149 * this is only called by cache_block_group, since we could have freed extents
150 * we need to check the pinned_extents for any extents that can't be used yet
151 * since their free space will be released as soon as the transaction commits.
152 */
153static int add_new_free_space(struct btrfs_block_group_cache *block_group,
154 struct btrfs_fs_info *info, u64 start, u64 end)
155{
156 u64 extent_start, extent_end, size;
157 int ret;
158
159 while (start < end) {
160 ret = find_first_extent_bit(&info->pinned_extents, start,
161 &extent_start, &extent_end,
162 EXTENT_DIRTY);
163 if (ret)
164 break;
165
166 if (extent_start == start) {
167 start = extent_end + 1;
168 } else if (extent_start > start && extent_start < end) {
169 size = extent_start - start;
Josef Bacikea6a4782008-11-20 12:16:16 -0500170 ret = btrfs_add_free_space(block_group, start,
171 size);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400172 BUG_ON(ret);
173 start = extent_end + 1;
174 } else {
175 break;
176 }
177 }
178
179 if (start < end) {
180 size = end - start;
Josef Bacikea6a4782008-11-20 12:16:16 -0500181 ret = btrfs_add_free_space(block_group, start, size);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400182 BUG_ON(ret);
183 }
184
185 return 0;
186}
187
Yan Zhenga512bbf2008-12-08 16:46:26 -0500188static int remove_sb_from_cache(struct btrfs_root *root,
189 struct btrfs_block_group_cache *cache)
190{
191 u64 bytenr;
192 u64 *logical;
193 int stripe_len;
194 int i, nr, ret;
195
196 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
197 bytenr = btrfs_sb_offset(i);
198 ret = btrfs_rmap_block(&root->fs_info->mapping_tree,
199 cache->key.objectid, bytenr, 0,
200 &logical, &nr, &stripe_len);
201 BUG_ON(ret);
202 while (nr--) {
203 btrfs_remove_free_space(cache, logical[nr],
204 stripe_len);
205 }
206 kfree(logical);
207 }
208 return 0;
209}
210
Chris Masone37c9e62007-05-09 20:13:14 -0400211static int cache_block_group(struct btrfs_root *root,
212 struct btrfs_block_group_cache *block_group)
213{
214 struct btrfs_path *path;
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400215 int ret = 0;
Chris Masone37c9e62007-05-09 20:13:14 -0400216 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400217 struct extent_buffer *leaf;
Chris Masone37c9e62007-05-09 20:13:14 -0400218 int slot;
Yan Zhenge4404d62008-12-12 10:03:26 -0500219 u64 last;
Chris Masone37c9e62007-05-09 20:13:14 -0400220
Chris Mason00f5c792007-11-30 10:09:33 -0500221 if (!block_group)
222 return 0;
223
Chris Masone37c9e62007-05-09 20:13:14 -0400224 root = root->fs_info->extent_root;
Chris Masone37c9e62007-05-09 20:13:14 -0400225
226 if (block_group->cached)
227 return 0;
Chris Masonf510cfe2007-10-15 16:14:48 -0400228
Chris Masone37c9e62007-05-09 20:13:14 -0400229 path = btrfs_alloc_path();
230 if (!path)
231 return -ENOMEM;
Yan7d7d6062007-09-14 16:15:28 -0400232
Chris Mason2cc58cf2007-08-27 16:49:44 -0400233 path->reada = 2;
Chris Mason5cd57b22008-06-25 16:01:30 -0400234 /*
235 * we get into deadlocks with paths held by callers of this function.
236 * since the alloc_mutex is protecting things right now, just
237 * skip the locking here
238 */
239 path->skip_locking = 1;
Yan Zhenge4404d62008-12-12 10:03:26 -0500240 last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET);
241 key.objectid = last;
Chris Masone37c9e62007-05-09 20:13:14 -0400242 key.offset = 0;
243 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
244 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
245 if (ret < 0)
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400246 goto err;
Yan Zhenga512bbf2008-12-08 16:46:26 -0500247
Chris Masond3977122009-01-05 21:25:51 -0500248 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400249 leaf = path->nodes[0];
Chris Masone37c9e62007-05-09 20:13:14 -0400250 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -0400251 if (slot >= btrfs_header_nritems(leaf)) {
Chris Masone37c9e62007-05-09 20:13:14 -0400252 ret = btrfs_next_leaf(root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -0400253 if (ret < 0)
254 goto err;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400255 if (ret == 0)
Chris Masone37c9e62007-05-09 20:13:14 -0400256 continue;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400257 else
Chris Masone37c9e62007-05-09 20:13:14 -0400258 break;
Chris Masone37c9e62007-05-09 20:13:14 -0400259 }
Chris Mason5f39d392007-10-15 16:14:19 -0400260 btrfs_item_key_to_cpu(leaf, &key, slot);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400261 if (key.objectid < block_group->key.objectid)
Yan7d7d6062007-09-14 16:15:28 -0400262 goto next;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400263
Chris Masone37c9e62007-05-09 20:13:14 -0400264 if (key.objectid >= block_group->key.objectid +
Josef Bacik0f9dd462008-09-23 13:14:11 -0400265 block_group->key.offset)
Yan7d7d6062007-09-14 16:15:28 -0400266 break;
Yan7d7d6062007-09-14 16:15:28 -0400267
268 if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) {
Josef Bacik0f9dd462008-09-23 13:14:11 -0400269 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
Josef Bacik0f9dd462008-09-23 13:14:11 -0400278 add_new_free_space(block_group, root->fs_info, last,
279 block_group->key.objectid +
280 block_group->key.offset);
281
Chris Masone37c9e62007-05-09 20:13:14 -0400282 block_group->cached = 1;
Josef Bacik70cb0742009-04-03 10:14:19 -0400283 remove_sb_from_cache(root, block_group);
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400284 ret = 0;
Chris Mason54aa1f42007-06-22 14:16:25 -0400285err:
Chris Masone37c9e62007-05-09 20:13:14 -0400286 btrfs_free_path(path);
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400287 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -0400288}
289
Josef Bacik0f9dd462008-09-23 13:14:11 -0400290/*
291 * return the block group that starts at or after bytenr
292 */
Chris Masond3977122009-01-05 21:25:51 -0500293static struct btrfs_block_group_cache *
294btrfs_lookup_first_block_group(struct btrfs_fs_info *info, u64 bytenr)
Chris Mason0ef3e662008-05-24 14:04:53 -0400295{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400296 struct btrfs_block_group_cache *cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400297
Josef Bacik0f9dd462008-09-23 13:14:11 -0400298 cache = block_group_cache_tree_search(info, bytenr, 0);
Chris Mason0ef3e662008-05-24 14:04:53 -0400299
Josef Bacik0f9dd462008-09-23 13:14:11 -0400300 return cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400301}
302
Josef Bacik0f9dd462008-09-23 13:14:11 -0400303/*
Sankar P9f556842009-05-14 13:52:22 -0400304 * return the block group that contains the given bytenr
Josef Bacik0f9dd462008-09-23 13:14:11 -0400305 */
Chris Masond3977122009-01-05 21:25:51 -0500306struct btrfs_block_group_cache *btrfs_lookup_block_group(
307 struct btrfs_fs_info *info,
308 u64 bytenr)
Chris Masonbe744172007-05-06 10:15:01 -0400309{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400310 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -0400311
Josef Bacik0f9dd462008-09-23 13:14:11 -0400312 cache = block_group_cache_tree_search(info, bytenr, 1);
Chris Mason96b51792007-10-15 16:15:19 -0400313
Josef Bacik0f9dd462008-09-23 13:14:11 -0400314 return cache;
Chris Masonbe744172007-05-06 10:15:01 -0400315}
Chris Mason0b86a832008-03-24 15:01:56 -0400316
Chris Masonfa9c0d792009-04-03 09:47:43 -0400317void btrfs_put_block_group(struct btrfs_block_group_cache *cache)
Yan Zhengd2fb3432008-12-11 16:30:39 -0500318{
319 if (atomic_dec_and_test(&cache->count))
320 kfree(cache);
321}
322
Josef Bacik0f9dd462008-09-23 13:14:11 -0400323static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
324 u64 flags)
Chris Mason6324fbf2008-03-24 15:01:59 -0400325{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400326 struct list_head *head = &info->space_info;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400327 struct btrfs_space_info *found;
Chris Mason4184ea72009-03-10 12:39:20 -0400328
329 rcu_read_lock();
330 list_for_each_entry_rcu(found, head, list) {
331 if (found->flags == flags) {
332 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -0400333 return found;
Chris Mason4184ea72009-03-10 12:39:20 -0400334 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400335 }
Chris Mason4184ea72009-03-10 12:39:20 -0400336 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -0400337 return NULL;
Chris Mason6324fbf2008-03-24 15:01:59 -0400338}
339
Chris Mason4184ea72009-03-10 12:39:20 -0400340/*
341 * after adding space to the filesystem, we need to clear the full flags
342 * on all the space infos.
343 */
344void btrfs_clear_space_info_full(struct btrfs_fs_info *info)
345{
346 struct list_head *head = &info->space_info;
347 struct btrfs_space_info *found;
348
349 rcu_read_lock();
350 list_for_each_entry_rcu(found, head, list)
351 found->full = 0;
352 rcu_read_unlock();
353}
354
Josef Bacik80eb2342008-10-29 14:49:05 -0400355static u64 div_factor(u64 num, int factor)
356{
357 if (factor == 10)
358 return num;
359 num *= factor;
360 do_div(num, 10);
361 return num;
362}
363
Yan Zhengd2fb3432008-12-11 16:30:39 -0500364u64 btrfs_find_block_group(struct btrfs_root *root,
365 u64 search_start, u64 search_hint, int owner)
Chris Masoncd1bc462007-04-27 10:08:34 -0400366{
Chris Mason96b51792007-10-15 16:15:19 -0400367 struct btrfs_block_group_cache *cache;
Chris Masoncd1bc462007-04-27 10:08:34 -0400368 u64 used;
Yan Zhengd2fb3432008-12-11 16:30:39 -0500369 u64 last = max(search_hint, search_start);
370 u64 group_start = 0;
Chris Mason31f3c992007-04-30 15:25:45 -0400371 int full_search = 0;
Yan Zhengd2fb3432008-12-11 16:30:39 -0500372 int factor = 9;
Chris Mason0ef3e662008-05-24 14:04:53 -0400373 int wrapped = 0;
Chris Mason31f3c992007-04-30 15:25:45 -0400374again:
Zheng Yane8569812008-09-26 10:05:48 -0400375 while (1) {
376 cache = btrfs_lookup_first_block_group(root->fs_info, last);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400377 if (!cache)
Chris Masoncd1bc462007-04-27 10:08:34 -0400378 break;
Chris Mason96b51792007-10-15 16:15:19 -0400379
Chris Masonc286ac42008-07-22 23:06:41 -0400380 spin_lock(&cache->lock);
Chris Mason96b51792007-10-15 16:15:19 -0400381 last = cache->key.objectid + cache->key.offset;
382 used = btrfs_block_group_used(&cache->item);
383
Yan Zhengd2fb3432008-12-11 16:30:39 -0500384 if ((full_search || !cache->ro) &&
385 block_group_bits(cache, BTRFS_BLOCK_GROUP_METADATA)) {
Zheng Yane8569812008-09-26 10:05:48 -0400386 if (used + cache->pinned + cache->reserved <
Yan Zhengd2fb3432008-12-11 16:30:39 -0500387 div_factor(cache->key.offset, factor)) {
388 group_start = cache->key.objectid;
Chris Masonc286ac42008-07-22 23:06:41 -0400389 spin_unlock(&cache->lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -0400390 btrfs_put_block_group(cache);
Chris Mason8790d502008-04-03 16:29:03 -0400391 goto found;
392 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400393 }
Chris Masonc286ac42008-07-22 23:06:41 -0400394 spin_unlock(&cache->lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -0400395 btrfs_put_block_group(cache);
Chris Masonde428b62007-05-18 13:28:27 -0400396 cond_resched();
Chris Masoncd1bc462007-04-27 10:08:34 -0400397 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400398 if (!wrapped) {
399 last = search_start;
400 wrapped = 1;
401 goto again;
402 }
403 if (!full_search && factor < 10) {
Chris Masonbe744172007-05-06 10:15:01 -0400404 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -0400405 full_search = 1;
Chris Mason0ef3e662008-05-24 14:04:53 -0400406 factor = 10;
Chris Mason31f3c992007-04-30 15:25:45 -0400407 goto again;
408 }
Chris Masonbe744172007-05-06 10:15:01 -0400409found:
Yan Zhengd2fb3432008-12-11 16:30:39 -0500410 return group_start;
Chris Mason925baed2008-06-25 16:01:30 -0400411}
Josef Bacik0f9dd462008-09-23 13:14:11 -0400412
Chris Masone02119d2008-09-05 16:13:11 -0400413/* simple helper to search for an existing extent at a given offset */
Zheng Yan31840ae2008-09-23 13:14:14 -0400414int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len)
Chris Masone02119d2008-09-05 16:13:11 -0400415{
416 int ret;
417 struct btrfs_key key;
Zheng Yan31840ae2008-09-23 13:14:14 -0400418 struct btrfs_path *path;
Chris Masone02119d2008-09-05 16:13:11 -0400419
Zheng Yan31840ae2008-09-23 13:14:14 -0400420 path = btrfs_alloc_path();
421 BUG_ON(!path);
Chris Masone02119d2008-09-05 16:13:11 -0400422 key.objectid = start;
423 key.offset = len;
424 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
425 ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
426 0, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400427 btrfs_free_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -0500428 return ret;
429}
430
Chris Masond8d5f3e2007-12-11 12:42:00 -0500431/*
432 * Back reference rules. Back refs have three main goals:
433 *
434 * 1) differentiate between all holders of references to an extent so that
435 * when a reference is dropped we can make sure it was a valid reference
436 * before freeing the extent.
437 *
438 * 2) Provide enough information to quickly find the holders of an extent
439 * if we notice a given block is corrupted or bad.
440 *
441 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
442 * maintenance. This is actually the same as #2, but with a slightly
443 * different use case.
444 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400445 * There are two kinds of back refs. The implicit back refs is optimized
446 * for pointers in non-shared tree blocks. For a given pointer in a block,
447 * back refs of this kind provide information about the block's owner tree
448 * and the pointer's key. These information allow us to find the block by
449 * b-tree searching. The full back refs is for pointers in tree blocks not
450 * referenced by their owner trees. The location of tree block is recorded
451 * in the back refs. Actually the full back refs is generic, and can be
452 * used in all cases the implicit back refs is used. The major shortcoming
453 * of the full back refs is its overhead. Every time a tree block gets
454 * COWed, we have to update back refs entry for all pointers in it.
455 *
456 * For a newly allocated tree block, we use implicit back refs for
457 * pointers in it. This means most tree related operations only involve
458 * implicit back refs. For a tree block created in old transaction, the
459 * only way to drop a reference to it is COW it. So we can detect the
460 * event that tree block loses its owner tree's reference and do the
461 * back refs conversion.
462 *
463 * When a tree block is COW'd through a tree, there are four cases:
464 *
465 * The reference count of the block is one and the tree is the block's
466 * owner tree. Nothing to do in this case.
467 *
468 * The reference count of the block is one and the tree is not the
469 * block's owner tree. In this case, full back refs is used for pointers
470 * in the block. Remove these full back refs, add implicit back refs for
471 * every pointers in the new block.
472 *
473 * The reference count of the block is greater than one and the tree is
474 * the block's owner tree. In this case, implicit back refs is used for
475 * pointers in the block. Add full back refs for every pointers in the
476 * block, increase lower level extents' reference counts. The original
477 * implicit back refs are entailed to the new block.
478 *
479 * The reference count of the block is greater than one and the tree is
480 * not the block's owner tree. Add implicit back refs for every pointer in
481 * the new block, increase lower level extents' reference count.
482 *
483 * Back Reference Key composing:
484 *
485 * The key objectid corresponds to the first byte in the extent,
486 * The key type is used to differentiate between types of back refs.
487 * There are different meanings of the key offset for different types
488 * of back refs.
489 *
Chris Masond8d5f3e2007-12-11 12:42:00 -0500490 * File extents can be referenced by:
491 *
492 * - multiple snapshots, subvolumes, or different generations in one subvol
Zheng Yan31840ae2008-09-23 13:14:14 -0400493 * - different files inside a single subvolume
Chris Masond8d5f3e2007-12-11 12:42:00 -0500494 * - different offsets inside a file (bookend extents in file.c)
495 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400496 * The extent ref structure for the implicit back refs has fields for:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500497 *
498 * - Objectid of the subvolume root
Chris Masond8d5f3e2007-12-11 12:42:00 -0500499 * - objectid of the file holding the reference
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400500 * - original offset in the file
501 * - how many bookend extents
Zheng Yan31840ae2008-09-23 13:14:14 -0400502 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400503 * The key offset for the implicit back refs is hash of the first
504 * three fields.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500505 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400506 * The extent ref structure for the full back refs has field for:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500507 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400508 * - number of pointers in the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -0500509 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400510 * The key offset for the implicit back refs is the first byte of
511 * the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -0500512 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400513 * When a file extent is allocated, The implicit back refs is used.
514 * the fields are filled in:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500515 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400516 * (root_key.objectid, inode objectid, offset in file, 1)
517 *
518 * When a file extent is removed file truncation, we find the
519 * corresponding implicit back refs and check the following fields:
520 *
521 * (btrfs_header_owner(leaf), inode objectid, offset in file)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500522 *
523 * Btree extents can be referenced by:
524 *
525 * - Different subvolumes
Chris Masond8d5f3e2007-12-11 12:42:00 -0500526 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400527 * Both the implicit back refs and the full back refs for tree blocks
528 * only consist of key. The key offset for the implicit back refs is
529 * objectid of block's owner tree. The key offset for the full back refs
530 * is the first byte of parent block.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500531 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400532 * When implicit back refs is used, information about the lowest key and
533 * level of the tree block are required. These information are stored in
534 * tree block info structure.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500535 */
Zheng Yan31840ae2008-09-23 13:14:14 -0400536
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400537#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
538static int convert_extent_item_v0(struct btrfs_trans_handle *trans,
539 struct btrfs_root *root,
540 struct btrfs_path *path,
541 u64 owner, u32 extra_size)
Chris Mason74493f72007-12-11 09:25:06 -0500542{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400543 struct btrfs_extent_item *item;
544 struct btrfs_extent_item_v0 *ei0;
545 struct btrfs_extent_ref_v0 *ref0;
546 struct btrfs_tree_block_info *bi;
Zheng Yan31840ae2008-09-23 13:14:14 -0400547 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400548 struct btrfs_key key;
549 struct btrfs_key found_key;
550 u32 new_size = sizeof(*item);
551 u64 refs;
Chris Mason74493f72007-12-11 09:25:06 -0500552 int ret;
553
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400554 leaf = path->nodes[0];
555 BUG_ON(btrfs_item_size_nr(leaf, path->slots[0]) != sizeof(*ei0));
Chris Mason74493f72007-12-11 09:25:06 -0500556
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400557 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
558 ei0 = btrfs_item_ptr(leaf, path->slots[0],
559 struct btrfs_extent_item_v0);
560 refs = btrfs_extent_refs_v0(leaf, ei0);
561
562 if (owner == (u64)-1) {
563 while (1) {
564 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
565 ret = btrfs_next_leaf(root, path);
566 if (ret < 0)
567 return ret;
568 BUG_ON(ret > 0);
569 leaf = path->nodes[0];
570 }
571 btrfs_item_key_to_cpu(leaf, &found_key,
572 path->slots[0]);
573 BUG_ON(key.objectid != found_key.objectid);
574 if (found_key.type != BTRFS_EXTENT_REF_V0_KEY) {
575 path->slots[0]++;
576 continue;
577 }
578 ref0 = btrfs_item_ptr(leaf, path->slots[0],
579 struct btrfs_extent_ref_v0);
580 owner = btrfs_ref_objectid_v0(leaf, ref0);
581 break;
582 }
583 }
584 btrfs_release_path(root, path);
585
586 if (owner < BTRFS_FIRST_FREE_OBJECTID)
587 new_size += sizeof(*bi);
588
589 new_size -= sizeof(*ei0);
590 ret = btrfs_search_slot(trans, root, &key, path,
591 new_size + extra_size, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -0400592 if (ret < 0)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400593 return ret;
594 BUG_ON(ret);
595
596 ret = btrfs_extend_item(trans, root, path, new_size);
597 BUG_ON(ret);
598
599 leaf = path->nodes[0];
600 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
601 btrfs_set_extent_refs(leaf, item, refs);
602 /* FIXME: get real generation */
603 btrfs_set_extent_generation(leaf, item, 0);
604 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
605 btrfs_set_extent_flags(leaf, item,
606 BTRFS_EXTENT_FLAG_TREE_BLOCK |
607 BTRFS_BLOCK_FLAG_FULL_BACKREF);
608 bi = (struct btrfs_tree_block_info *)(item + 1);
609 /* FIXME: get first key of the block */
610 memset_extent_buffer(leaf, 0, (unsigned long)bi, sizeof(*bi));
611 btrfs_set_tree_block_level(leaf, bi, (int)owner);
612 } else {
613 btrfs_set_extent_flags(leaf, item, BTRFS_EXTENT_FLAG_DATA);
614 }
615 btrfs_mark_buffer_dirty(leaf);
616 return 0;
617}
618#endif
619
620static u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset)
621{
622 u32 high_crc = ~(u32)0;
623 u32 low_crc = ~(u32)0;
624 __le64 lenum;
625
626 lenum = cpu_to_le64(root_objectid);
David Woodhouse163e7832009-04-19 13:02:41 +0100627 high_crc = crc32c(high_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400628 lenum = cpu_to_le64(owner);
David Woodhouse163e7832009-04-19 13:02:41 +0100629 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400630 lenum = cpu_to_le64(offset);
David Woodhouse163e7832009-04-19 13:02:41 +0100631 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400632
633 return ((u64)high_crc << 31) ^ (u64)low_crc;
634}
635
636static u64 hash_extent_data_ref_item(struct extent_buffer *leaf,
637 struct btrfs_extent_data_ref *ref)
638{
639 return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref),
640 btrfs_extent_data_ref_objectid(leaf, ref),
641 btrfs_extent_data_ref_offset(leaf, ref));
642}
643
644static int match_extent_data_ref(struct extent_buffer *leaf,
645 struct btrfs_extent_data_ref *ref,
646 u64 root_objectid, u64 owner, u64 offset)
647{
648 if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid ||
649 btrfs_extent_data_ref_objectid(leaf, ref) != owner ||
650 btrfs_extent_data_ref_offset(leaf, ref) != offset)
651 return 0;
652 return 1;
653}
654
655static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans,
656 struct btrfs_root *root,
657 struct btrfs_path *path,
658 u64 bytenr, u64 parent,
659 u64 root_objectid,
660 u64 owner, u64 offset)
661{
662 struct btrfs_key key;
663 struct btrfs_extent_data_ref *ref;
664 struct extent_buffer *leaf;
665 u32 nritems;
666 int ret;
667 int recow;
668 int err = -ENOENT;
669
670 key.objectid = bytenr;
671 if (parent) {
672 key.type = BTRFS_SHARED_DATA_REF_KEY;
673 key.offset = parent;
674 } else {
675 key.type = BTRFS_EXTENT_DATA_REF_KEY;
676 key.offset = hash_extent_data_ref(root_objectid,
677 owner, offset);
678 }
679again:
680 recow = 0;
681 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
682 if (ret < 0) {
683 err = ret;
684 goto fail;
685 }
686
687 if (parent) {
688 if (!ret)
689 return 0;
690#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
691 key.type = BTRFS_EXTENT_REF_V0_KEY;
692 btrfs_release_path(root, path);
693 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
694 if (ret < 0) {
695 err = ret;
696 goto fail;
697 }
698 if (!ret)
699 return 0;
700#endif
701 goto fail;
Zheng Yan31840ae2008-09-23 13:14:14 -0400702 }
703
704 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400705 nritems = btrfs_header_nritems(leaf);
706 while (1) {
707 if (path->slots[0] >= nritems) {
708 ret = btrfs_next_leaf(root, path);
709 if (ret < 0)
710 err = ret;
711 if (ret)
712 goto fail;
713
714 leaf = path->nodes[0];
715 nritems = btrfs_header_nritems(leaf);
716 recow = 1;
717 }
718
719 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
720 if (key.objectid != bytenr ||
721 key.type != BTRFS_EXTENT_DATA_REF_KEY)
722 goto fail;
723
724 ref = btrfs_item_ptr(leaf, path->slots[0],
725 struct btrfs_extent_data_ref);
726
727 if (match_extent_data_ref(leaf, ref, root_objectid,
728 owner, offset)) {
729 if (recow) {
730 btrfs_release_path(root, path);
731 goto again;
732 }
733 err = 0;
734 break;
735 }
736 path->slots[0]++;
Zheng Yan31840ae2008-09-23 13:14:14 -0400737 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400738fail:
739 return err;
Zheng Yan31840ae2008-09-23 13:14:14 -0400740}
741
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400742static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans,
743 struct btrfs_root *root,
744 struct btrfs_path *path,
745 u64 bytenr, u64 parent,
746 u64 root_objectid, u64 owner,
747 u64 offset, int refs_to_add)
Zheng Yan31840ae2008-09-23 13:14:14 -0400748{
749 struct btrfs_key key;
750 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400751 u32 size;
Zheng Yan31840ae2008-09-23 13:14:14 -0400752 u32 num_refs;
753 int ret;
754
755 key.objectid = bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400756 if (parent) {
757 key.type = BTRFS_SHARED_DATA_REF_KEY;
758 key.offset = parent;
759 size = sizeof(struct btrfs_shared_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -0400760 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400761 key.type = BTRFS_EXTENT_DATA_REF_KEY;
762 key.offset = hash_extent_data_ref(root_objectid,
763 owner, offset);
764 size = sizeof(struct btrfs_extent_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -0400765 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400766
767 ret = btrfs_insert_empty_item(trans, root, path, &key, size);
768 if (ret && ret != -EEXIST)
769 goto fail;
770
771 leaf = path->nodes[0];
772 if (parent) {
773 struct btrfs_shared_data_ref *ref;
774 ref = btrfs_item_ptr(leaf, path->slots[0],
775 struct btrfs_shared_data_ref);
776 if (ret == 0) {
777 btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add);
778 } else {
779 num_refs = btrfs_shared_data_ref_count(leaf, ref);
780 num_refs += refs_to_add;
781 btrfs_set_shared_data_ref_count(leaf, ref, num_refs);
782 }
783 } else {
784 struct btrfs_extent_data_ref *ref;
785 while (ret == -EEXIST) {
786 ref = btrfs_item_ptr(leaf, path->slots[0],
787 struct btrfs_extent_data_ref);
788 if (match_extent_data_ref(leaf, ref, root_objectid,
789 owner, offset))
790 break;
791 btrfs_release_path(root, path);
792 key.offset++;
793 ret = btrfs_insert_empty_item(trans, root, path, &key,
794 size);
795 if (ret && ret != -EEXIST)
796 goto fail;
797
798 leaf = path->nodes[0];
799 }
800 ref = btrfs_item_ptr(leaf, path->slots[0],
801 struct btrfs_extent_data_ref);
802 if (ret == 0) {
803 btrfs_set_extent_data_ref_root(leaf, ref,
804 root_objectid);
805 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
806 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
807 btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add);
808 } else {
809 num_refs = btrfs_extent_data_ref_count(leaf, ref);
810 num_refs += refs_to_add;
811 btrfs_set_extent_data_ref_count(leaf, ref, num_refs);
812 }
813 }
814 btrfs_mark_buffer_dirty(leaf);
815 ret = 0;
816fail:
Chris Mason7bb86312007-12-11 09:25:06 -0500817 btrfs_release_path(root, path);
818 return ret;
Chris Mason74493f72007-12-11 09:25:06 -0500819}
820
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400821static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans,
822 struct btrfs_root *root,
823 struct btrfs_path *path,
824 int refs_to_drop)
Zheng Yan31840ae2008-09-23 13:14:14 -0400825{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400826 struct btrfs_key key;
827 struct btrfs_extent_data_ref *ref1 = NULL;
828 struct btrfs_shared_data_ref *ref2 = NULL;
Zheng Yan31840ae2008-09-23 13:14:14 -0400829 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400830 u32 num_refs = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -0400831 int ret = 0;
832
833 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400834 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
835
836 if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
837 ref1 = btrfs_item_ptr(leaf, path->slots[0],
838 struct btrfs_extent_data_ref);
839 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
840 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
841 ref2 = btrfs_item_ptr(leaf, path->slots[0],
842 struct btrfs_shared_data_ref);
843 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
844#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
845 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
846 struct btrfs_extent_ref_v0 *ref0;
847 ref0 = btrfs_item_ptr(leaf, path->slots[0],
848 struct btrfs_extent_ref_v0);
849 num_refs = btrfs_ref_count_v0(leaf, ref0);
850#endif
851 } else {
852 BUG();
853 }
854
Chris Mason56bec292009-03-13 10:10:06 -0400855 BUG_ON(num_refs < refs_to_drop);
856 num_refs -= refs_to_drop;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400857
Zheng Yan31840ae2008-09-23 13:14:14 -0400858 if (num_refs == 0) {
859 ret = btrfs_del_item(trans, root, path);
860 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400861 if (key.type == BTRFS_EXTENT_DATA_REF_KEY)
862 btrfs_set_extent_data_ref_count(leaf, ref1, num_refs);
863 else if (key.type == BTRFS_SHARED_DATA_REF_KEY)
864 btrfs_set_shared_data_ref_count(leaf, ref2, num_refs);
865#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
866 else {
867 struct btrfs_extent_ref_v0 *ref0;
868 ref0 = btrfs_item_ptr(leaf, path->slots[0],
869 struct btrfs_extent_ref_v0);
870 btrfs_set_ref_count_v0(leaf, ref0, num_refs);
871 }
872#endif
Zheng Yan31840ae2008-09-23 13:14:14 -0400873 btrfs_mark_buffer_dirty(leaf);
874 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400875 return ret;
876}
877
878static noinline u32 extent_data_ref_count(struct btrfs_root *root,
879 struct btrfs_path *path,
880 struct btrfs_extent_inline_ref *iref)
881{
882 struct btrfs_key key;
883 struct extent_buffer *leaf;
884 struct btrfs_extent_data_ref *ref1;
885 struct btrfs_shared_data_ref *ref2;
886 u32 num_refs = 0;
887
888 leaf = path->nodes[0];
889 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
890 if (iref) {
891 if (btrfs_extent_inline_ref_type(leaf, iref) ==
892 BTRFS_EXTENT_DATA_REF_KEY) {
893 ref1 = (struct btrfs_extent_data_ref *)(&iref->offset);
894 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
895 } else {
896 ref2 = (struct btrfs_shared_data_ref *)(iref + 1);
897 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
898 }
899 } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
900 ref1 = btrfs_item_ptr(leaf, path->slots[0],
901 struct btrfs_extent_data_ref);
902 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
903 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
904 ref2 = btrfs_item_ptr(leaf, path->slots[0],
905 struct btrfs_shared_data_ref);
906 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
907#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
908 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
909 struct btrfs_extent_ref_v0 *ref0;
910 ref0 = btrfs_item_ptr(leaf, path->slots[0],
911 struct btrfs_extent_ref_v0);
912 num_refs = btrfs_ref_count_v0(leaf, ref0);
913#endif
914 } else {
915 WARN_ON(1);
916 }
917 return num_refs;
918}
919
920static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans,
921 struct btrfs_root *root,
922 struct btrfs_path *path,
923 u64 bytenr, u64 parent,
924 u64 root_objectid)
925{
926 struct btrfs_key key;
927 int ret;
928
929 key.objectid = bytenr;
930 if (parent) {
931 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
932 key.offset = parent;
933 } else {
934 key.type = BTRFS_TREE_BLOCK_REF_KEY;
935 key.offset = root_objectid;
936 }
937
938 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
939 if (ret > 0)
940 ret = -ENOENT;
941#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
942 if (ret == -ENOENT && parent) {
943 btrfs_release_path(root, path);
944 key.type = BTRFS_EXTENT_REF_V0_KEY;
945 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
946 if (ret > 0)
947 ret = -ENOENT;
948 }
949#endif
950 return ret;
951}
952
953static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans,
954 struct btrfs_root *root,
955 struct btrfs_path *path,
956 u64 bytenr, u64 parent,
957 u64 root_objectid)
958{
959 struct btrfs_key key;
960 int ret;
961
962 key.objectid = bytenr;
963 if (parent) {
964 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
965 key.offset = parent;
966 } else {
967 key.type = BTRFS_TREE_BLOCK_REF_KEY;
968 key.offset = root_objectid;
969 }
970
971 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400972 btrfs_release_path(root, path);
973 return ret;
974}
975
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400976static inline int extent_ref_type(u64 parent, u64 owner)
977{
978 int type;
979 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
980 if (parent > 0)
981 type = BTRFS_SHARED_BLOCK_REF_KEY;
982 else
983 type = BTRFS_TREE_BLOCK_REF_KEY;
984 } else {
985 if (parent > 0)
986 type = BTRFS_SHARED_DATA_REF_KEY;
987 else
988 type = BTRFS_EXTENT_DATA_REF_KEY;
989 }
990 return type;
991}
992
993static int find_next_key(struct btrfs_path *path, struct btrfs_key *key)
994
995{
996 int level;
997 BUG_ON(!path->keep_locks);
998 for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
999 if (!path->nodes[level])
1000 break;
1001 btrfs_assert_tree_locked(path->nodes[level]);
1002 if (path->slots[level] + 1 >=
1003 btrfs_header_nritems(path->nodes[level]))
1004 continue;
1005 if (level == 0)
1006 btrfs_item_key_to_cpu(path->nodes[level], key,
1007 path->slots[level] + 1);
1008 else
1009 btrfs_node_key_to_cpu(path->nodes[level], key,
1010 path->slots[level] + 1);
1011 return 0;
1012 }
1013 return 1;
1014}
1015
1016/*
1017 * look for inline back ref. if back ref is found, *ref_ret is set
1018 * to the address of inline back ref, and 0 is returned.
1019 *
1020 * if back ref isn't found, *ref_ret is set to the address where it
1021 * should be inserted, and -ENOENT is returned.
1022 *
1023 * if insert is true and there are too many inline back refs, the path
1024 * points to the extent item, and -EAGAIN is returned.
1025 *
1026 * NOTE: inline back refs are ordered in the same way that back ref
1027 * items in the tree are ordered.
1028 */
1029static noinline_for_stack
1030int lookup_inline_extent_backref(struct btrfs_trans_handle *trans,
1031 struct btrfs_root *root,
1032 struct btrfs_path *path,
1033 struct btrfs_extent_inline_ref **ref_ret,
1034 u64 bytenr, u64 num_bytes,
1035 u64 parent, u64 root_objectid,
1036 u64 owner, u64 offset, int insert)
1037{
1038 struct btrfs_key key;
1039 struct extent_buffer *leaf;
1040 struct btrfs_extent_item *ei;
1041 struct btrfs_extent_inline_ref *iref;
1042 u64 flags;
1043 u64 item_size;
1044 unsigned long ptr;
1045 unsigned long end;
1046 int extra_size;
1047 int type;
1048 int want;
1049 int ret;
1050 int err = 0;
1051
1052 key.objectid = bytenr;
1053 key.type = BTRFS_EXTENT_ITEM_KEY;
1054 key.offset = num_bytes;
1055
1056 want = extent_ref_type(parent, owner);
1057 if (insert) {
1058 extra_size = btrfs_extent_inline_ref_size(want);
Yan Zheng85d41982009-06-11 08:51:10 -04001059 path->keep_locks = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001060 } else
1061 extra_size = -1;
1062 ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1);
1063 if (ret < 0) {
1064 err = ret;
1065 goto out;
1066 }
1067 BUG_ON(ret);
1068
1069 leaf = path->nodes[0];
1070 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1071#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1072 if (item_size < sizeof(*ei)) {
1073 if (!insert) {
1074 err = -ENOENT;
1075 goto out;
1076 }
1077 ret = convert_extent_item_v0(trans, root, path, owner,
1078 extra_size);
1079 if (ret < 0) {
1080 err = ret;
1081 goto out;
1082 }
1083 leaf = path->nodes[0];
1084 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1085 }
1086#endif
1087 BUG_ON(item_size < sizeof(*ei));
1088
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001089 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1090 flags = btrfs_extent_flags(leaf, ei);
1091
1092 ptr = (unsigned long)(ei + 1);
1093 end = (unsigned long)ei + item_size;
1094
1095 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
1096 ptr += sizeof(struct btrfs_tree_block_info);
1097 BUG_ON(ptr > end);
1098 } else {
1099 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_DATA));
1100 }
1101
1102 err = -ENOENT;
1103 while (1) {
1104 if (ptr >= end) {
1105 WARN_ON(ptr > end);
1106 break;
1107 }
1108 iref = (struct btrfs_extent_inline_ref *)ptr;
1109 type = btrfs_extent_inline_ref_type(leaf, iref);
1110 if (want < type)
1111 break;
1112 if (want > type) {
1113 ptr += btrfs_extent_inline_ref_size(type);
1114 continue;
1115 }
1116
1117 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1118 struct btrfs_extent_data_ref *dref;
1119 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1120 if (match_extent_data_ref(leaf, dref, root_objectid,
1121 owner, offset)) {
1122 err = 0;
1123 break;
1124 }
1125 if (hash_extent_data_ref_item(leaf, dref) <
1126 hash_extent_data_ref(root_objectid, owner, offset))
1127 break;
1128 } else {
1129 u64 ref_offset;
1130 ref_offset = btrfs_extent_inline_ref_offset(leaf, iref);
1131 if (parent > 0) {
1132 if (parent == ref_offset) {
1133 err = 0;
1134 break;
1135 }
1136 if (ref_offset < parent)
1137 break;
1138 } else {
1139 if (root_objectid == ref_offset) {
1140 err = 0;
1141 break;
1142 }
1143 if (ref_offset < root_objectid)
1144 break;
1145 }
1146 }
1147 ptr += btrfs_extent_inline_ref_size(type);
1148 }
1149 if (err == -ENOENT && insert) {
1150 if (item_size + extra_size >=
1151 BTRFS_MAX_EXTENT_ITEM_SIZE(root)) {
1152 err = -EAGAIN;
1153 goto out;
1154 }
1155 /*
1156 * To add new inline back ref, we have to make sure
1157 * there is no corresponding back ref item.
1158 * For simplicity, we just do not add new inline back
1159 * ref if there is any kind of item for this block
1160 */
Yan Zheng85d41982009-06-11 08:51:10 -04001161 if (find_next_key(path, &key) == 0 && key.objectid == bytenr &&
1162 key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001163 err = -EAGAIN;
1164 goto out;
1165 }
1166 }
1167 *ref_ret = (struct btrfs_extent_inline_ref *)ptr;
1168out:
Yan Zheng85d41982009-06-11 08:51:10 -04001169 if (insert) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001170 path->keep_locks = 0;
1171 btrfs_unlock_up_safe(path, 1);
1172 }
1173 return err;
1174}
1175
1176/*
1177 * helper to add new inline back ref
1178 */
1179static noinline_for_stack
1180int setup_inline_extent_backref(struct btrfs_trans_handle *trans,
1181 struct btrfs_root *root,
1182 struct btrfs_path *path,
1183 struct btrfs_extent_inline_ref *iref,
1184 u64 parent, u64 root_objectid,
1185 u64 owner, u64 offset, int refs_to_add,
1186 struct btrfs_delayed_extent_op *extent_op)
1187{
1188 struct extent_buffer *leaf;
1189 struct btrfs_extent_item *ei;
1190 unsigned long ptr;
1191 unsigned long end;
1192 unsigned long item_offset;
1193 u64 refs;
1194 int size;
1195 int type;
1196 int ret;
1197
1198 leaf = path->nodes[0];
1199 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1200 item_offset = (unsigned long)iref - (unsigned long)ei;
1201
1202 type = extent_ref_type(parent, owner);
1203 size = btrfs_extent_inline_ref_size(type);
1204
1205 ret = btrfs_extend_item(trans, root, path, size);
1206 BUG_ON(ret);
1207
1208 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1209 refs = btrfs_extent_refs(leaf, ei);
1210 refs += refs_to_add;
1211 btrfs_set_extent_refs(leaf, ei, refs);
1212 if (extent_op)
1213 __run_delayed_extent_op(extent_op, leaf, ei);
1214
1215 ptr = (unsigned long)ei + item_offset;
1216 end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]);
1217 if (ptr < end - size)
1218 memmove_extent_buffer(leaf, ptr + size, ptr,
1219 end - size - ptr);
1220
1221 iref = (struct btrfs_extent_inline_ref *)ptr;
1222 btrfs_set_extent_inline_ref_type(leaf, iref, type);
1223 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1224 struct btrfs_extent_data_ref *dref;
1225 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1226 btrfs_set_extent_data_ref_root(leaf, dref, root_objectid);
1227 btrfs_set_extent_data_ref_objectid(leaf, dref, owner);
1228 btrfs_set_extent_data_ref_offset(leaf, dref, offset);
1229 btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add);
1230 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1231 struct btrfs_shared_data_ref *sref;
1232 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1233 btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add);
1234 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1235 } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
1236 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1237 } else {
1238 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
1239 }
1240 btrfs_mark_buffer_dirty(leaf);
1241 return 0;
1242}
1243
1244static int lookup_extent_backref(struct btrfs_trans_handle *trans,
1245 struct btrfs_root *root,
1246 struct btrfs_path *path,
1247 struct btrfs_extent_inline_ref **ref_ret,
1248 u64 bytenr, u64 num_bytes, u64 parent,
1249 u64 root_objectid, u64 owner, u64 offset)
1250{
1251 int ret;
1252
1253 ret = lookup_inline_extent_backref(trans, root, path, ref_ret,
1254 bytenr, num_bytes, parent,
1255 root_objectid, owner, offset, 0);
1256 if (ret != -ENOENT)
1257 return ret;
1258
1259 btrfs_release_path(root, path);
1260 *ref_ret = NULL;
1261
1262 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1263 ret = lookup_tree_block_ref(trans, root, path, bytenr, parent,
1264 root_objectid);
1265 } else {
1266 ret = lookup_extent_data_ref(trans, root, path, bytenr, parent,
1267 root_objectid, owner, offset);
1268 }
1269 return ret;
1270}
1271
1272/*
1273 * helper to update/remove inline back ref
1274 */
1275static noinline_for_stack
1276int update_inline_extent_backref(struct btrfs_trans_handle *trans,
1277 struct btrfs_root *root,
1278 struct btrfs_path *path,
1279 struct btrfs_extent_inline_ref *iref,
1280 int refs_to_mod,
1281 struct btrfs_delayed_extent_op *extent_op)
1282{
1283 struct extent_buffer *leaf;
1284 struct btrfs_extent_item *ei;
1285 struct btrfs_extent_data_ref *dref = NULL;
1286 struct btrfs_shared_data_ref *sref = NULL;
1287 unsigned long ptr;
1288 unsigned long end;
1289 u32 item_size;
1290 int size;
1291 int type;
1292 int ret;
1293 u64 refs;
1294
1295 leaf = path->nodes[0];
1296 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1297 refs = btrfs_extent_refs(leaf, ei);
1298 WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0);
1299 refs += refs_to_mod;
1300 btrfs_set_extent_refs(leaf, ei, refs);
1301 if (extent_op)
1302 __run_delayed_extent_op(extent_op, leaf, ei);
1303
1304 type = btrfs_extent_inline_ref_type(leaf, iref);
1305
1306 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1307 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1308 refs = btrfs_extent_data_ref_count(leaf, dref);
1309 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1310 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1311 refs = btrfs_shared_data_ref_count(leaf, sref);
1312 } else {
1313 refs = 1;
1314 BUG_ON(refs_to_mod != -1);
1315 }
1316
1317 BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod);
1318 refs += refs_to_mod;
1319
1320 if (refs > 0) {
1321 if (type == BTRFS_EXTENT_DATA_REF_KEY)
1322 btrfs_set_extent_data_ref_count(leaf, dref, refs);
1323 else
1324 btrfs_set_shared_data_ref_count(leaf, sref, refs);
1325 } else {
1326 size = btrfs_extent_inline_ref_size(type);
1327 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1328 ptr = (unsigned long)iref;
1329 end = (unsigned long)ei + item_size;
1330 if (ptr + size < end)
1331 memmove_extent_buffer(leaf, ptr, ptr + size,
1332 end - ptr - size);
1333 item_size -= size;
1334 ret = btrfs_truncate_item(trans, root, path, item_size, 1);
1335 BUG_ON(ret);
1336 }
1337 btrfs_mark_buffer_dirty(leaf);
1338 return 0;
1339}
1340
1341static noinline_for_stack
1342int insert_inline_extent_backref(struct btrfs_trans_handle *trans,
1343 struct btrfs_root *root,
1344 struct btrfs_path *path,
1345 u64 bytenr, u64 num_bytes, u64 parent,
1346 u64 root_objectid, u64 owner,
1347 u64 offset, int refs_to_add,
1348 struct btrfs_delayed_extent_op *extent_op)
1349{
1350 struct btrfs_extent_inline_ref *iref;
1351 int ret;
1352
1353 ret = lookup_inline_extent_backref(trans, root, path, &iref,
1354 bytenr, num_bytes, parent,
1355 root_objectid, owner, offset, 1);
1356 if (ret == 0) {
1357 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID);
1358 ret = update_inline_extent_backref(trans, root, path, iref,
1359 refs_to_add, extent_op);
1360 } else if (ret == -ENOENT) {
1361 ret = setup_inline_extent_backref(trans, root, path, iref,
1362 parent, root_objectid,
1363 owner, offset, refs_to_add,
1364 extent_op);
1365 }
1366 return ret;
1367}
1368
1369static int insert_extent_backref(struct btrfs_trans_handle *trans,
1370 struct btrfs_root *root,
1371 struct btrfs_path *path,
1372 u64 bytenr, u64 parent, u64 root_objectid,
1373 u64 owner, u64 offset, int refs_to_add)
1374{
1375 int ret;
1376 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1377 BUG_ON(refs_to_add != 1);
1378 ret = insert_tree_block_ref(trans, root, path, bytenr,
1379 parent, root_objectid);
1380 } else {
1381 ret = insert_extent_data_ref(trans, root, path, bytenr,
1382 parent, root_objectid,
1383 owner, offset, refs_to_add);
1384 }
1385 return ret;
1386}
1387
1388static int remove_extent_backref(struct btrfs_trans_handle *trans,
1389 struct btrfs_root *root,
1390 struct btrfs_path *path,
1391 struct btrfs_extent_inline_ref *iref,
1392 int refs_to_drop, int is_data)
1393{
1394 int ret;
1395
1396 BUG_ON(!is_data && refs_to_drop != 1);
1397 if (iref) {
1398 ret = update_inline_extent_backref(trans, root, path, iref,
1399 -refs_to_drop, NULL);
1400 } else if (is_data) {
1401 ret = remove_extent_data_ref(trans, root, path, refs_to_drop);
1402 } else {
1403 ret = btrfs_del_item(trans, root, path);
1404 }
1405 return ret;
1406}
1407
Chris Mason4b4e25f2008-11-20 10:22:27 -05001408#ifdef BIO_RW_DISCARD
Chris Mason15916de2008-11-19 21:17:22 -05001409static void btrfs_issue_discard(struct block_device *bdev,
1410 u64 start, u64 len)
1411{
Chris Mason15916de2008-11-19 21:17:22 -05001412 blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_KERNEL);
Chris Mason15916de2008-11-19 21:17:22 -05001413}
Chris Mason4b4e25f2008-11-20 10:22:27 -05001414#endif
Chris Mason15916de2008-11-19 21:17:22 -05001415
Liu Hui1f3c79a2009-01-05 15:57:51 -05001416static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr,
1417 u64 num_bytes)
1418{
1419#ifdef BIO_RW_DISCARD
1420 int ret;
1421 u64 map_length = num_bytes;
1422 struct btrfs_multi_bio *multi = NULL;
1423
1424 /* Tell the block device(s) that the sectors can be discarded */
1425 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
1426 bytenr, &map_length, &multi, 0);
1427 if (!ret) {
1428 struct btrfs_bio_stripe *stripe = multi->stripes;
1429 int i;
1430
1431 if (map_length > num_bytes)
1432 map_length = num_bytes;
1433
1434 for (i = 0; i < multi->num_stripes; i++, stripe++) {
1435 btrfs_issue_discard(stripe->dev->bdev,
1436 stripe->physical,
1437 map_length);
1438 }
1439 kfree(multi);
1440 }
1441
1442 return ret;
1443#else
1444 return 0;
1445#endif
1446}
1447
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001448int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1449 struct btrfs_root *root,
1450 u64 bytenr, u64 num_bytes, u64 parent,
1451 u64 root_objectid, u64 owner, u64 offset)
Zheng Yan31840ae2008-09-23 13:14:14 -04001452{
1453 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001454 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID &&
1455 root_objectid == BTRFS_TREE_LOG_OBJECTID);
Zheng Yan31840ae2008-09-23 13:14:14 -04001456
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001457 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1458 ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes,
1459 parent, root_objectid, (int)owner,
1460 BTRFS_ADD_DELAYED_REF, NULL);
1461 } else {
1462 ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes,
1463 parent, root_objectid, owner, offset,
1464 BTRFS_ADD_DELAYED_REF, NULL);
1465 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001466 return ret;
1467}
1468
Chris Mason925baed2008-06-25 16:01:30 -04001469static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001470 struct btrfs_root *root,
1471 u64 bytenr, u64 num_bytes,
1472 u64 parent, u64 root_objectid,
1473 u64 owner, u64 offset, int refs_to_add,
1474 struct btrfs_delayed_extent_op *extent_op)
Chris Mason56bec292009-03-13 10:10:06 -04001475{
Chris Mason5caf2a02007-04-02 11:20:42 -04001476 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001477 struct extent_buffer *leaf;
Chris Mason234b63a2007-03-13 10:46:10 -04001478 struct btrfs_extent_item *item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001479 u64 refs;
1480 int ret;
1481 int err = 0;
Chris Mason037e6392007-03-07 11:50:24 -05001482
Chris Mason5caf2a02007-04-02 11:20:42 -04001483 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04001484 if (!path)
1485 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -04001486
Chris Mason3c12ac72008-04-21 12:01:38 -04001487 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04001488 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001489 /* this will setup the path even if it fails to insert the back ref */
1490 ret = insert_inline_extent_backref(trans, root->fs_info->extent_root,
1491 path, bytenr, num_bytes, parent,
1492 root_objectid, owner, offset,
1493 refs_to_add, extent_op);
1494 if (ret == 0)
1495 goto out;
Zheng Yan31840ae2008-09-23 13:14:14 -04001496
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001497 if (ret != -EAGAIN) {
1498 err = ret;
1499 goto out;
Chris Masonb9473432009-03-13 11:00:37 -04001500 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001501
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001502 leaf = path->nodes[0];
1503 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1504 refs = btrfs_extent_refs(leaf, item);
1505 btrfs_set_extent_refs(leaf, item, refs + refs_to_add);
1506 if (extent_op)
1507 __run_delayed_extent_op(extent_op, leaf, item);
Zheng Yan31840ae2008-09-23 13:14:14 -04001508
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001509 btrfs_mark_buffer_dirty(leaf);
Chris Mason5caf2a02007-04-02 11:20:42 -04001510 btrfs_release_path(root->fs_info->extent_root, path);
Chris Mason7bb86312007-12-11 09:25:06 -05001511
Chris Mason3c12ac72008-04-21 12:01:38 -04001512 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04001513 path->leave_spinning = 1;
1514
Chris Mason56bec292009-03-13 10:10:06 -04001515 /* now insert the actual backref */
Zheng Yan31840ae2008-09-23 13:14:14 -04001516 ret = insert_extent_backref(trans, root->fs_info->extent_root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001517 path, bytenr, parent, root_objectid,
1518 owner, offset, refs_to_add);
Chris Mason7bb86312007-12-11 09:25:06 -05001519 BUG_ON(ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001520out:
Chris Mason74493f72007-12-11 09:25:06 -05001521 btrfs_free_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001522 return err;
Chris Mason02217ed2007-03-02 16:08:05 -05001523}
1524
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001525static int run_delayed_data_ref(struct btrfs_trans_handle *trans,
1526 struct btrfs_root *root,
1527 struct btrfs_delayed_ref_node *node,
1528 struct btrfs_delayed_extent_op *extent_op,
1529 int insert_reserved)
Chris Masone9d0b132007-08-10 14:06:19 -04001530{
Chris Mason56bec292009-03-13 10:10:06 -04001531 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001532 struct btrfs_delayed_data_ref *ref;
1533 struct btrfs_key ins;
1534 u64 parent = 0;
1535 u64 ref_root = 0;
1536 u64 flags = 0;
Chris Mason56bec292009-03-13 10:10:06 -04001537
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001538 ins.objectid = node->bytenr;
1539 ins.offset = node->num_bytes;
1540 ins.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason56bec292009-03-13 10:10:06 -04001541
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001542 ref = btrfs_delayed_node_to_data_ref(node);
1543 if (node->type == BTRFS_SHARED_DATA_REF_KEY)
1544 parent = ref->parent;
1545 else
1546 ref_root = ref->root;
1547
1548 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
1549 if (extent_op) {
1550 BUG_ON(extent_op->update_key);
1551 flags |= extent_op->flags_to_set;
1552 }
1553 ret = alloc_reserved_file_extent(trans, root,
1554 parent, ref_root, flags,
1555 ref->objectid, ref->offset,
1556 &ins, node->ref_mod);
1557 update_reserved_extents(root, ins.objectid, ins.offset, 0);
1558 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
1559 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
1560 node->num_bytes, parent,
1561 ref_root, ref->objectid,
1562 ref->offset, node->ref_mod,
1563 extent_op);
1564 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
1565 ret = __btrfs_free_extent(trans, root, node->bytenr,
1566 node->num_bytes, parent,
1567 ref_root, ref->objectid,
1568 ref->offset, node->ref_mod,
1569 extent_op);
1570 } else {
1571 BUG();
1572 }
Chris Mason56bec292009-03-13 10:10:06 -04001573 return ret;
1574}
1575
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001576static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
1577 struct extent_buffer *leaf,
1578 struct btrfs_extent_item *ei)
1579{
1580 u64 flags = btrfs_extent_flags(leaf, ei);
1581 if (extent_op->update_flags) {
1582 flags |= extent_op->flags_to_set;
1583 btrfs_set_extent_flags(leaf, ei, flags);
1584 }
1585
1586 if (extent_op->update_key) {
1587 struct btrfs_tree_block_info *bi;
1588 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK));
1589 bi = (struct btrfs_tree_block_info *)(ei + 1);
1590 btrfs_set_tree_block_key(leaf, bi, &extent_op->key);
1591 }
1592}
1593
1594static int run_delayed_extent_op(struct btrfs_trans_handle *trans,
1595 struct btrfs_root *root,
1596 struct btrfs_delayed_ref_node *node,
1597 struct btrfs_delayed_extent_op *extent_op)
1598{
1599 struct btrfs_key key;
1600 struct btrfs_path *path;
1601 struct btrfs_extent_item *ei;
1602 struct extent_buffer *leaf;
1603 u32 item_size;
1604 int ret;
1605 int err = 0;
1606
1607 path = btrfs_alloc_path();
1608 if (!path)
1609 return -ENOMEM;
1610
1611 key.objectid = node->bytenr;
1612 key.type = BTRFS_EXTENT_ITEM_KEY;
1613 key.offset = node->num_bytes;
1614
1615 path->reada = 1;
1616 path->leave_spinning = 1;
1617 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key,
1618 path, 0, 1);
1619 if (ret < 0) {
1620 err = ret;
1621 goto out;
1622 }
1623 if (ret > 0) {
1624 err = -EIO;
1625 goto out;
1626 }
1627
1628 leaf = path->nodes[0];
1629 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1630#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1631 if (item_size < sizeof(*ei)) {
1632 ret = convert_extent_item_v0(trans, root->fs_info->extent_root,
1633 path, (u64)-1, 0);
1634 if (ret < 0) {
1635 err = ret;
1636 goto out;
1637 }
1638 leaf = path->nodes[0];
1639 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1640 }
1641#endif
1642 BUG_ON(item_size < sizeof(*ei));
1643 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1644 __run_delayed_extent_op(extent_op, leaf, ei);
1645
1646 btrfs_mark_buffer_dirty(leaf);
1647out:
1648 btrfs_free_path(path);
1649 return err;
1650}
1651
1652static int run_delayed_tree_ref(struct btrfs_trans_handle *trans,
1653 struct btrfs_root *root,
1654 struct btrfs_delayed_ref_node *node,
1655 struct btrfs_delayed_extent_op *extent_op,
1656 int insert_reserved)
1657{
1658 int ret = 0;
1659 struct btrfs_delayed_tree_ref *ref;
1660 struct btrfs_key ins;
1661 u64 parent = 0;
1662 u64 ref_root = 0;
1663
1664 ins.objectid = node->bytenr;
1665 ins.offset = node->num_bytes;
1666 ins.type = BTRFS_EXTENT_ITEM_KEY;
1667
1668 ref = btrfs_delayed_node_to_tree_ref(node);
1669 if (node->type == BTRFS_SHARED_BLOCK_REF_KEY)
1670 parent = ref->parent;
1671 else
1672 ref_root = ref->root;
1673
1674 BUG_ON(node->ref_mod != 1);
1675 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
1676 BUG_ON(!extent_op || !extent_op->update_flags ||
1677 !extent_op->update_key);
1678 ret = alloc_reserved_tree_block(trans, root,
1679 parent, ref_root,
1680 extent_op->flags_to_set,
1681 &extent_op->key,
1682 ref->level, &ins);
1683 update_reserved_extents(root, ins.objectid, ins.offset, 0);
1684 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
1685 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
1686 node->num_bytes, parent, ref_root,
1687 ref->level, 0, 1, extent_op);
1688 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
1689 ret = __btrfs_free_extent(trans, root, node->bytenr,
1690 node->num_bytes, parent, ref_root,
1691 ref->level, 0, 1, extent_op);
1692 } else {
1693 BUG();
1694 }
1695 return ret;
1696}
1697
1698
Chris Mason56bec292009-03-13 10:10:06 -04001699/* helper function to actually process a single delayed ref entry */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001700static int run_one_delayed_ref(struct btrfs_trans_handle *trans,
1701 struct btrfs_root *root,
1702 struct btrfs_delayed_ref_node *node,
1703 struct btrfs_delayed_extent_op *extent_op,
1704 int insert_reserved)
Chris Mason56bec292009-03-13 10:10:06 -04001705{
Josef Bacikeb099672009-02-12 09:27:38 -05001706 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001707 if (btrfs_delayed_ref_is_head(node)) {
Chris Mason56bec292009-03-13 10:10:06 -04001708 struct btrfs_delayed_ref_head *head;
1709 /*
1710 * we've hit the end of the chain and we were supposed
1711 * to insert this extent into the tree. But, it got
1712 * deleted before we ever needed to insert it, so all
1713 * we have to do is clean up the accounting
1714 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001715 BUG_ON(extent_op);
1716 head = btrfs_delayed_node_to_head(node);
Chris Mason56bec292009-03-13 10:10:06 -04001717 if (insert_reserved) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001718 if (head->is_data) {
1719 ret = btrfs_del_csums(trans, root,
1720 node->bytenr,
1721 node->num_bytes);
1722 BUG_ON(ret);
1723 }
1724 btrfs_update_pinned_extents(root, node->bytenr,
1725 node->num_bytes, 1);
Chris Mason56bec292009-03-13 10:10:06 -04001726 update_reserved_extents(root, node->bytenr,
1727 node->num_bytes, 0);
1728 }
Chris Mason56bec292009-03-13 10:10:06 -04001729 mutex_unlock(&head->mutex);
1730 return 0;
1731 }
Josef Bacikeb099672009-02-12 09:27:38 -05001732
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001733 if (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
1734 node->type == BTRFS_SHARED_BLOCK_REF_KEY)
1735 ret = run_delayed_tree_ref(trans, root, node, extent_op,
1736 insert_reserved);
1737 else if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
1738 node->type == BTRFS_SHARED_DATA_REF_KEY)
1739 ret = run_delayed_data_ref(trans, root, node, extent_op,
1740 insert_reserved);
1741 else
1742 BUG();
1743 return ret;
Chris Masone9d0b132007-08-10 14:06:19 -04001744}
1745
Chris Mason56bec292009-03-13 10:10:06 -04001746static noinline struct btrfs_delayed_ref_node *
1747select_delayed_ref(struct btrfs_delayed_ref_head *head)
Chris Masona28ec192007-03-06 20:08:01 -05001748{
Chris Mason56bec292009-03-13 10:10:06 -04001749 struct rb_node *node;
1750 struct btrfs_delayed_ref_node *ref;
1751 int action = BTRFS_ADD_DELAYED_REF;
1752again:
1753 /*
1754 * select delayed ref of type BTRFS_ADD_DELAYED_REF first.
1755 * this prevents ref count from going down to zero when
1756 * there still are pending delayed ref.
1757 */
1758 node = rb_prev(&head->node.rb_node);
1759 while (1) {
1760 if (!node)
1761 break;
1762 ref = rb_entry(node, struct btrfs_delayed_ref_node,
1763 rb_node);
1764 if (ref->bytenr != head->node.bytenr)
1765 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001766 if (ref->action == action)
Chris Mason56bec292009-03-13 10:10:06 -04001767 return ref;
1768 node = rb_prev(node);
Chris Mason5f39d392007-10-15 16:14:19 -04001769 }
Chris Mason56bec292009-03-13 10:10:06 -04001770 if (action == BTRFS_ADD_DELAYED_REF) {
1771 action = BTRFS_DROP_DELAYED_REF;
1772 goto again;
1773 }
1774 return NULL;
1775}
1776
Chris Masonc3e69d52009-03-13 10:17:05 -04001777static noinline int run_clustered_refs(struct btrfs_trans_handle *trans,
1778 struct btrfs_root *root,
1779 struct list_head *cluster)
Chris Mason56bec292009-03-13 10:10:06 -04001780{
Chris Mason56bec292009-03-13 10:10:06 -04001781 struct btrfs_delayed_ref_root *delayed_refs;
1782 struct btrfs_delayed_ref_node *ref;
1783 struct btrfs_delayed_ref_head *locked_ref = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001784 struct btrfs_delayed_extent_op *extent_op;
Chris Mason56bec292009-03-13 10:10:06 -04001785 int ret;
Chris Masonc3e69d52009-03-13 10:17:05 -04001786 int count = 0;
Chris Mason56bec292009-03-13 10:10:06 -04001787 int must_insert_reserved = 0;
Chris Mason56bec292009-03-13 10:10:06 -04001788
1789 delayed_refs = &trans->transaction->delayed_refs;
Chris Mason56bec292009-03-13 10:10:06 -04001790 while (1) {
1791 if (!locked_ref) {
Chris Masonc3e69d52009-03-13 10:17:05 -04001792 /* pick a new head ref from the cluster list */
1793 if (list_empty(cluster))
Chris Mason56bec292009-03-13 10:10:06 -04001794 break;
Chris Mason56bec292009-03-13 10:10:06 -04001795
Chris Masonc3e69d52009-03-13 10:17:05 -04001796 locked_ref = list_entry(cluster->next,
1797 struct btrfs_delayed_ref_head, cluster);
1798
1799 /* grab the lock that says we are going to process
1800 * all the refs for this head */
1801 ret = btrfs_delayed_ref_lock(trans, locked_ref);
1802
1803 /*
1804 * we may have dropped the spin lock to get the head
1805 * mutex lock, and that might have given someone else
1806 * time to free the head. If that's true, it has been
1807 * removed from our list and we can move on.
1808 */
1809 if (ret == -EAGAIN) {
1810 locked_ref = NULL;
1811 count++;
1812 continue;
Chris Mason56bec292009-03-13 10:10:06 -04001813 }
1814 }
1815
1816 /*
1817 * record the must insert reserved flag before we
1818 * drop the spin lock.
1819 */
1820 must_insert_reserved = locked_ref->must_insert_reserved;
1821 locked_ref->must_insert_reserved = 0;
1822
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001823 extent_op = locked_ref->extent_op;
1824 locked_ref->extent_op = NULL;
1825
Chris Mason56bec292009-03-13 10:10:06 -04001826 /*
1827 * locked_ref is the head node, so we have to go one
1828 * node back for any delayed ref updates
1829 */
Chris Mason56bec292009-03-13 10:10:06 -04001830 ref = select_delayed_ref(locked_ref);
1831 if (!ref) {
1832 /* All delayed refs have been processed, Go ahead
1833 * and send the head node to run_one_delayed_ref,
1834 * so that any accounting fixes can happen
1835 */
1836 ref = &locked_ref->node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001837
1838 if (extent_op && must_insert_reserved) {
1839 kfree(extent_op);
1840 extent_op = NULL;
1841 }
1842
1843 if (extent_op) {
1844 spin_unlock(&delayed_refs->lock);
1845
1846 ret = run_delayed_extent_op(trans, root,
1847 ref, extent_op);
1848 BUG_ON(ret);
1849 kfree(extent_op);
1850
1851 cond_resched();
1852 spin_lock(&delayed_refs->lock);
1853 continue;
1854 }
1855
Chris Masonc3e69d52009-03-13 10:17:05 -04001856 list_del_init(&locked_ref->cluster);
Chris Mason56bec292009-03-13 10:10:06 -04001857 locked_ref = NULL;
1858 }
1859
1860 ref->in_tree = 0;
1861 rb_erase(&ref->rb_node, &delayed_refs->root);
1862 delayed_refs->num_entries--;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001863
Chris Mason56bec292009-03-13 10:10:06 -04001864 spin_unlock(&delayed_refs->lock);
1865
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001866 ret = run_one_delayed_ref(trans, root, ref, extent_op,
Chris Mason56bec292009-03-13 10:10:06 -04001867 must_insert_reserved);
1868 BUG_ON(ret);
Chris Mason56bec292009-03-13 10:10:06 -04001869
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001870 btrfs_put_delayed_ref(ref);
1871 kfree(extent_op);
Chris Masonc3e69d52009-03-13 10:17:05 -04001872 count++;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001873
Chris Mason1887be62009-03-13 10:11:24 -04001874 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04001875 spin_lock(&delayed_refs->lock);
1876 }
Chris Masonc3e69d52009-03-13 10:17:05 -04001877 return count;
1878}
1879
1880/*
1881 * this starts processing the delayed reference count updates and
1882 * extent insertions we have queued up so far. count can be
1883 * 0, which means to process everything in the tree at the start
1884 * of the run (but not newly added entries), or it can be some target
1885 * number you'd like to process.
1886 */
1887int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
1888 struct btrfs_root *root, unsigned long count)
1889{
1890 struct rb_node *node;
1891 struct btrfs_delayed_ref_root *delayed_refs;
1892 struct btrfs_delayed_ref_node *ref;
1893 struct list_head cluster;
1894 int ret;
1895 int run_all = count == (unsigned long)-1;
1896 int run_most = 0;
1897
1898 if (root == root->fs_info->extent_root)
1899 root = root->fs_info->tree_root;
1900
1901 delayed_refs = &trans->transaction->delayed_refs;
1902 INIT_LIST_HEAD(&cluster);
1903again:
1904 spin_lock(&delayed_refs->lock);
1905 if (count == 0) {
1906 count = delayed_refs->num_entries * 2;
1907 run_most = 1;
1908 }
1909 while (1) {
1910 if (!(run_all || run_most) &&
1911 delayed_refs->num_heads_ready < 64)
1912 break;
1913
1914 /*
1915 * go find something we can process in the rbtree. We start at
1916 * the beginning of the tree, and then build a cluster
1917 * of refs to process starting at the first one we are able to
1918 * lock
1919 */
1920 ret = btrfs_find_ref_cluster(trans, &cluster,
1921 delayed_refs->run_delayed_start);
1922 if (ret)
1923 break;
1924
1925 ret = run_clustered_refs(trans, root, &cluster);
1926 BUG_ON(ret < 0);
1927
1928 count -= min_t(unsigned long, ret, count);
1929
1930 if (count == 0)
1931 break;
1932 }
1933
Chris Mason56bec292009-03-13 10:10:06 -04001934 if (run_all) {
Chris Mason56bec292009-03-13 10:10:06 -04001935 node = rb_first(&delayed_refs->root);
Chris Masonc3e69d52009-03-13 10:17:05 -04001936 if (!node)
Chris Mason56bec292009-03-13 10:10:06 -04001937 goto out;
Chris Masonc3e69d52009-03-13 10:17:05 -04001938 count = (unsigned long)-1;
Chris Mason56bec292009-03-13 10:10:06 -04001939
1940 while (node) {
1941 ref = rb_entry(node, struct btrfs_delayed_ref_node,
1942 rb_node);
1943 if (btrfs_delayed_ref_is_head(ref)) {
1944 struct btrfs_delayed_ref_head *head;
1945
1946 head = btrfs_delayed_node_to_head(ref);
1947 atomic_inc(&ref->refs);
1948
1949 spin_unlock(&delayed_refs->lock);
1950 mutex_lock(&head->mutex);
1951 mutex_unlock(&head->mutex);
1952
1953 btrfs_put_delayed_ref(ref);
Chris Mason1887be62009-03-13 10:11:24 -04001954 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04001955 goto again;
1956 }
1957 node = rb_next(node);
1958 }
1959 spin_unlock(&delayed_refs->lock);
Chris Mason56bec292009-03-13 10:10:06 -04001960 schedule_timeout(1);
1961 goto again;
1962 }
Chris Mason54aa1f42007-06-22 14:16:25 -04001963out:
Chris Masonc3e69d52009-03-13 10:17:05 -04001964 spin_unlock(&delayed_refs->lock);
Chris Masona28ec192007-03-06 20:08:01 -05001965 return 0;
1966}
1967
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001968int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
1969 struct btrfs_root *root,
1970 u64 bytenr, u64 num_bytes, u64 flags,
1971 int is_data)
1972{
1973 struct btrfs_delayed_extent_op *extent_op;
1974 int ret;
1975
1976 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
1977 if (!extent_op)
1978 return -ENOMEM;
1979
1980 extent_op->flags_to_set = flags;
1981 extent_op->update_flags = 1;
1982 extent_op->update_key = 0;
1983 extent_op->is_data = is_data ? 1 : 0;
1984
1985 ret = btrfs_add_delayed_extent_op(trans, bytenr, num_bytes, extent_op);
1986 if (ret)
1987 kfree(extent_op);
1988 return ret;
1989}
1990
1991static noinline int check_delayed_ref(struct btrfs_trans_handle *trans,
1992 struct btrfs_root *root,
1993 struct btrfs_path *path,
1994 u64 objectid, u64 offset, u64 bytenr)
1995{
1996 struct btrfs_delayed_ref_head *head;
1997 struct btrfs_delayed_ref_node *ref;
1998 struct btrfs_delayed_data_ref *data_ref;
1999 struct btrfs_delayed_ref_root *delayed_refs;
2000 struct rb_node *node;
2001 int ret = 0;
2002
2003 ret = -ENOENT;
2004 delayed_refs = &trans->transaction->delayed_refs;
2005 spin_lock(&delayed_refs->lock);
2006 head = btrfs_find_delayed_ref_head(trans, bytenr);
2007 if (!head)
2008 goto out;
2009
2010 if (!mutex_trylock(&head->mutex)) {
2011 atomic_inc(&head->node.refs);
2012 spin_unlock(&delayed_refs->lock);
2013
2014 btrfs_release_path(root->fs_info->extent_root, path);
2015
2016 mutex_lock(&head->mutex);
2017 mutex_unlock(&head->mutex);
2018 btrfs_put_delayed_ref(&head->node);
2019 return -EAGAIN;
2020 }
2021
2022 node = rb_prev(&head->node.rb_node);
2023 if (!node)
2024 goto out_unlock;
2025
2026 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2027
2028 if (ref->bytenr != bytenr)
2029 goto out_unlock;
2030
2031 ret = 1;
2032 if (ref->type != BTRFS_EXTENT_DATA_REF_KEY)
2033 goto out_unlock;
2034
2035 data_ref = btrfs_delayed_node_to_data_ref(ref);
2036
2037 node = rb_prev(node);
2038 if (node) {
2039 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2040 if (ref->bytenr == bytenr)
2041 goto out_unlock;
2042 }
2043
2044 if (data_ref->root != root->root_key.objectid ||
2045 data_ref->objectid != objectid || data_ref->offset != offset)
2046 goto out_unlock;
2047
2048 ret = 0;
2049out_unlock:
2050 mutex_unlock(&head->mutex);
2051out:
2052 spin_unlock(&delayed_refs->lock);
2053 return ret;
2054}
2055
2056static noinline int check_committed_ref(struct btrfs_trans_handle *trans,
2057 struct btrfs_root *root,
2058 struct btrfs_path *path,
2059 u64 objectid, u64 offset, u64 bytenr)
Chris Masonbe20aa92007-12-17 20:14:01 -05002060{
2061 struct btrfs_root *extent_root = root->fs_info->extent_root;
Yan Zhengf321e492008-07-30 09:26:11 -04002062 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002063 struct btrfs_extent_data_ref *ref;
2064 struct btrfs_extent_inline_ref *iref;
2065 struct btrfs_extent_item *ei;
Chris Masonbe20aa92007-12-17 20:14:01 -05002066 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002067 u32 item_size;
Yan Zhengf321e492008-07-30 09:26:11 -04002068 int ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05002069
Chris Masonbe20aa92007-12-17 20:14:01 -05002070 key.objectid = bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -04002071 key.offset = (u64)-1;
Yan Zhengf321e492008-07-30 09:26:11 -04002072 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masonbe20aa92007-12-17 20:14:01 -05002073
Chris Masonbe20aa92007-12-17 20:14:01 -05002074 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
2075 if (ret < 0)
2076 goto out;
2077 BUG_ON(ret == 0);
Yan Zheng80ff3852008-10-30 14:20:02 -04002078
2079 ret = -ENOENT;
2080 if (path->slots[0] == 0)
Zheng Yan31840ae2008-09-23 13:14:14 -04002081 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002082
Zheng Yan31840ae2008-09-23 13:14:14 -04002083 path->slots[0]--;
Yan Zhengf321e492008-07-30 09:26:11 -04002084 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002085 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05002086
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002087 if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY)
Chris Masonbe20aa92007-12-17 20:14:01 -05002088 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002089
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002090 ret = 1;
2091 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2092#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2093 if (item_size < sizeof(*ei)) {
2094 WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0));
2095 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002096 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002097#endif
2098 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2099
2100 if (item_size != sizeof(*ei) +
2101 btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY))
2102 goto out;
2103
2104 if (btrfs_extent_generation(leaf, ei) <=
2105 btrfs_root_last_snapshot(&root->root_item))
2106 goto out;
2107
2108 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
2109 if (btrfs_extent_inline_ref_type(leaf, iref) !=
2110 BTRFS_EXTENT_DATA_REF_KEY)
2111 goto out;
2112
2113 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
2114 if (btrfs_extent_refs(leaf, ei) !=
2115 btrfs_extent_data_ref_count(leaf, ref) ||
2116 btrfs_extent_data_ref_root(leaf, ref) !=
2117 root->root_key.objectid ||
2118 btrfs_extent_data_ref_objectid(leaf, ref) != objectid ||
2119 btrfs_extent_data_ref_offset(leaf, ref) != offset)
2120 goto out;
2121
Yan Zhengf321e492008-07-30 09:26:11 -04002122 ret = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05002123out:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002124 return ret;
2125}
2126
2127int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
2128 struct btrfs_root *root,
2129 u64 objectid, u64 offset, u64 bytenr)
2130{
2131 struct btrfs_path *path;
2132 int ret;
2133 int ret2;
2134
2135 path = btrfs_alloc_path();
2136 if (!path)
2137 return -ENOENT;
2138
2139 do {
2140 ret = check_committed_ref(trans, root, path, objectid,
2141 offset, bytenr);
2142 if (ret && ret != -ENOENT)
2143 goto out;
2144
2145 ret2 = check_delayed_ref(trans, root, path, objectid,
2146 offset, bytenr);
2147 } while (ret2 == -EAGAIN);
2148
2149 if (ret2 && ret2 != -ENOENT) {
2150 ret = ret2;
2151 goto out;
2152 }
2153
2154 if (ret != -ENOENT || ret2 != -ENOENT)
2155 ret = 0;
2156out:
Yan Zhengf321e492008-07-30 09:26:11 -04002157 btrfs_free_path(path);
2158 return ret;
2159}
2160
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002161#if 0
Zheng Yan31840ae2008-09-23 13:14:14 -04002162int btrfs_cache_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2163 struct extent_buffer *buf, u32 nr_extents)
Chris Mason02217ed2007-03-02 16:08:05 -05002164{
Chris Mason5f39d392007-10-15 16:14:19 -04002165 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -04002166 struct btrfs_file_extent_item *fi;
Zheng Yane4657682008-09-26 10:04:53 -04002167 u64 root_gen;
2168 u32 nritems;
Chris Mason02217ed2007-03-02 16:08:05 -05002169 int i;
Chris Masondb945352007-10-15 16:15:53 -04002170 int level;
Zheng Yan31840ae2008-09-23 13:14:14 -04002171 int ret = 0;
Zheng Yane4657682008-09-26 10:04:53 -04002172 int shared = 0;
Chris Masona28ec192007-03-06 20:08:01 -05002173
Chris Mason3768f362007-03-13 16:47:54 -04002174 if (!root->ref_cows)
Chris Masona28ec192007-03-06 20:08:01 -05002175 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002176
Zheng Yane4657682008-09-26 10:04:53 -04002177 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
2178 shared = 0;
2179 root_gen = root->root_key.offset;
2180 } else {
2181 shared = 1;
2182 root_gen = trans->transid - 1;
2183 }
2184
Chris Masondb945352007-10-15 16:15:53 -04002185 level = btrfs_header_level(buf);
Chris Mason5f39d392007-10-15 16:14:19 -04002186 nritems = btrfs_header_nritems(buf);
Chris Mason4a096752008-07-21 10:29:44 -04002187
Zheng Yan31840ae2008-09-23 13:14:14 -04002188 if (level == 0) {
Yan Zheng31153d82008-07-28 15:32:19 -04002189 struct btrfs_leaf_ref *ref;
2190 struct btrfs_extent_info *info;
2191
Zheng Yan31840ae2008-09-23 13:14:14 -04002192 ref = btrfs_alloc_leaf_ref(root, nr_extents);
Yan Zheng31153d82008-07-28 15:32:19 -04002193 if (!ref) {
Zheng Yan31840ae2008-09-23 13:14:14 -04002194 ret = -ENOMEM;
Yan Zheng31153d82008-07-28 15:32:19 -04002195 goto out;
2196 }
2197
Zheng Yane4657682008-09-26 10:04:53 -04002198 ref->root_gen = root_gen;
Yan Zheng31153d82008-07-28 15:32:19 -04002199 ref->bytenr = buf->start;
2200 ref->owner = btrfs_header_owner(buf);
2201 ref->generation = btrfs_header_generation(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04002202 ref->nritems = nr_extents;
Yan Zheng31153d82008-07-28 15:32:19 -04002203 info = ref->extents;
Yanbcc63ab2008-07-30 16:29:20 -04002204
Zheng Yan31840ae2008-09-23 13:14:14 -04002205 for (i = 0; nr_extents > 0 && i < nritems; i++) {
Yan Zheng31153d82008-07-28 15:32:19 -04002206 u64 disk_bytenr;
2207 btrfs_item_key_to_cpu(buf, &key, i);
2208 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
2209 continue;
2210 fi = btrfs_item_ptr(buf, i,
2211 struct btrfs_file_extent_item);
2212 if (btrfs_file_extent_type(buf, fi) ==
2213 BTRFS_FILE_EXTENT_INLINE)
2214 continue;
2215 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
2216 if (disk_bytenr == 0)
2217 continue;
2218
2219 info->bytenr = disk_bytenr;
2220 info->num_bytes =
2221 btrfs_file_extent_disk_num_bytes(buf, fi);
2222 info->objectid = key.objectid;
2223 info->offset = key.offset;
2224 info++;
2225 }
2226
Zheng Yane4657682008-09-26 10:04:53 -04002227 ret = btrfs_add_leaf_ref(root, ref, shared);
Yan Zheng5b84e8d2008-10-09 11:46:19 -04002228 if (ret == -EEXIST && shared) {
2229 struct btrfs_leaf_ref *old;
2230 old = btrfs_lookup_leaf_ref(root, ref->bytenr);
2231 BUG_ON(!old);
2232 btrfs_remove_leaf_ref(root, old);
2233 btrfs_free_leaf_ref(root, old);
2234 ret = btrfs_add_leaf_ref(root, ref, shared);
2235 }
Yan Zheng31153d82008-07-28 15:32:19 -04002236 WARN_ON(ret);
Yanbcc63ab2008-07-30 16:29:20 -04002237 btrfs_free_leaf_ref(root, ref);
Yan Zheng31153d82008-07-28 15:32:19 -04002238 }
2239out:
Zheng Yan31840ae2008-09-23 13:14:14 -04002240 return ret;
2241}
2242
Chris Masonb7a9f292009-02-04 09:23:45 -05002243/* when a block goes through cow, we update the reference counts of
2244 * everything that block points to. The internal pointers of the block
2245 * can be in just about any order, and it is likely to have clusters of
2246 * things that are close together and clusters of things that are not.
2247 *
2248 * To help reduce the seeks that come with updating all of these reference
2249 * counts, sort them by byte number before actual updates are done.
2250 *
2251 * struct refsort is used to match byte number to slot in the btree block.
2252 * we sort based on the byte number and then use the slot to actually
2253 * find the item.
Chris Masonbd56b302009-02-04 09:27:02 -05002254 *
2255 * struct refsort is smaller than strcut btrfs_item and smaller than
2256 * struct btrfs_key_ptr. Since we're currently limited to the page size
2257 * for a btree block, there's no way for a kmalloc of refsorts for a
2258 * single node to be bigger than a page.
Chris Masonb7a9f292009-02-04 09:23:45 -05002259 */
2260struct refsort {
2261 u64 bytenr;
2262 u32 slot;
2263};
2264
2265/*
2266 * for passing into sort()
2267 */
2268static int refsort_cmp(const void *a_void, const void *b_void)
2269{
2270 const struct refsort *a = a_void;
2271 const struct refsort *b = b_void;
2272
2273 if (a->bytenr < b->bytenr)
2274 return -1;
2275 if (a->bytenr > b->bytenr)
2276 return 1;
2277 return 0;
2278}
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002279#endif
Chris Masonb7a9f292009-02-04 09:23:45 -05002280
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002281static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
Chris Masonb7a9f292009-02-04 09:23:45 -05002282 struct btrfs_root *root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002283 struct extent_buffer *buf,
2284 int full_backref, int inc)
Zheng Yan31840ae2008-09-23 13:14:14 -04002285{
2286 u64 bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002287 u64 num_bytes;
2288 u64 parent;
Zheng Yan31840ae2008-09-23 13:14:14 -04002289 u64 ref_root;
Zheng Yan31840ae2008-09-23 13:14:14 -04002290 u32 nritems;
Zheng Yan31840ae2008-09-23 13:14:14 -04002291 struct btrfs_key key;
2292 struct btrfs_file_extent_item *fi;
2293 int i;
2294 int level;
2295 int ret = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04002296 int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002297 u64, u64, u64, u64, u64, u64);
Zheng Yan31840ae2008-09-23 13:14:14 -04002298
2299 ref_root = btrfs_header_owner(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04002300 nritems = btrfs_header_nritems(buf);
2301 level = btrfs_header_level(buf);
2302
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002303 if (!root->ref_cows && level == 0)
2304 return 0;
Chris Masonb7a9f292009-02-04 09:23:45 -05002305
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002306 if (inc)
2307 process_func = btrfs_inc_extent_ref;
2308 else
2309 process_func = btrfs_free_extent;
Zheng Yan31840ae2008-09-23 13:14:14 -04002310
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002311 if (full_backref)
2312 parent = buf->start;
2313 else
2314 parent = 0;
2315
Zheng Yan31840ae2008-09-23 13:14:14 -04002316 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04002317 if (level == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002318 btrfs_item_key_to_cpu(buf, &key, i);
2319 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -04002320 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04002321 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -04002322 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002323 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -04002324 BTRFS_FILE_EXTENT_INLINE)
2325 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04002326 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
2327 if (bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -04002328 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04002329
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002330 num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
2331 key.offset -= btrfs_file_extent_offset(buf, fi);
2332 ret = process_func(trans, root, bytenr, num_bytes,
2333 parent, ref_root, key.objectid,
2334 key.offset);
2335 if (ret)
2336 goto fail;
Chris Masonb7a9f292009-02-04 09:23:45 -05002337 } else {
2338 bytenr = btrfs_node_blockptr(buf, i);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002339 num_bytes = btrfs_level_size(root, level - 1);
2340 ret = process_func(trans, root, bytenr, num_bytes,
2341 parent, ref_root, level - 1, 0);
2342 if (ret)
Zheng Yan31840ae2008-09-23 13:14:14 -04002343 goto fail;
Chris Mason54aa1f42007-06-22 14:16:25 -04002344 }
2345 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002346 return 0;
2347fail:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002348 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -04002349 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05002350}
2351
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002352int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2353 struct extent_buffer *buf, int full_backref)
Zheng Yan31840ae2008-09-23 13:14:14 -04002354{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002355 return __btrfs_mod_ref(trans, root, buf, full_backref, 1);
2356}
Zheng Yan31840ae2008-09-23 13:14:14 -04002357
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002358int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2359 struct extent_buffer *buf, int full_backref)
2360{
2361 return __btrfs_mod_ref(trans, root, buf, full_backref, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04002362}
2363
Chris Mason9078a3e2007-04-26 16:46:15 -04002364static int write_one_cache_group(struct btrfs_trans_handle *trans,
2365 struct btrfs_root *root,
2366 struct btrfs_path *path,
2367 struct btrfs_block_group_cache *cache)
2368{
2369 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04002370 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04002371 unsigned long bi;
2372 struct extent_buffer *leaf;
Chris Mason9078a3e2007-04-26 16:46:15 -04002373
Chris Mason9078a3e2007-04-26 16:46:15 -04002374 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04002375 if (ret < 0)
2376 goto fail;
Chris Mason9078a3e2007-04-26 16:46:15 -04002377 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04002378
2379 leaf = path->nodes[0];
2380 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
2381 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
2382 btrfs_mark_buffer_dirty(leaf);
Chris Mason9078a3e2007-04-26 16:46:15 -04002383 btrfs_release_path(extent_root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -04002384fail:
Chris Mason9078a3e2007-04-26 16:46:15 -04002385 if (ret)
2386 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04002387 return 0;
2388
2389}
2390
Chris Mason96b51792007-10-15 16:15:19 -04002391int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
2392 struct btrfs_root *root)
Chris Mason9078a3e2007-04-26 16:46:15 -04002393{
Josef Bacik0f9dd462008-09-23 13:14:11 -04002394 struct btrfs_block_group_cache *cache, *entry;
2395 struct rb_node *n;
Chris Mason9078a3e2007-04-26 16:46:15 -04002396 int err = 0;
2397 int werr = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04002398 struct btrfs_path *path;
Chris Mason96b51792007-10-15 16:15:19 -04002399 u64 last = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04002400
2401 path = btrfs_alloc_path();
2402 if (!path)
2403 return -ENOMEM;
2404
Chris Masond3977122009-01-05 21:25:51 -05002405 while (1) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04002406 cache = NULL;
2407 spin_lock(&root->fs_info->block_group_cache_lock);
2408 for (n = rb_first(&root->fs_info->block_group_cache_tree);
2409 n; n = rb_next(n)) {
2410 entry = rb_entry(n, struct btrfs_block_group_cache,
2411 cache_node);
2412 if (entry->dirty) {
2413 cache = entry;
2414 break;
2415 }
2416 }
2417 spin_unlock(&root->fs_info->block_group_cache_lock);
2418
2419 if (!cache)
Chris Mason9078a3e2007-04-26 16:46:15 -04002420 break;
Chris Mason54aa1f42007-06-22 14:16:25 -04002421
Zheng Yane8569812008-09-26 10:05:48 -04002422 cache->dirty = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002423 last += cache->key.offset;
2424
Chris Mason96b51792007-10-15 16:15:19 -04002425 err = write_one_cache_group(trans, root,
2426 path, cache);
2427 /*
2428 * if we fail to write the cache group, we want
2429 * to keep it marked dirty in hopes that a later
2430 * write will work
2431 */
2432 if (err) {
2433 werr = err;
2434 continue;
Chris Mason9078a3e2007-04-26 16:46:15 -04002435 }
2436 }
2437 btrfs_free_path(path);
2438 return werr;
2439}
2440
Yan Zhengd2fb3432008-12-11 16:30:39 -05002441int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr)
2442{
2443 struct btrfs_block_group_cache *block_group;
2444 int readonly = 0;
2445
2446 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
2447 if (!block_group || block_group->ro)
2448 readonly = 1;
2449 if (block_group)
Chris Masonfa9c0d792009-04-03 09:47:43 -04002450 btrfs_put_block_group(block_group);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002451 return readonly;
2452}
2453
Chris Mason593060d2008-03-25 16:50:33 -04002454static int update_space_info(struct btrfs_fs_info *info, u64 flags,
2455 u64 total_bytes, u64 bytes_used,
2456 struct btrfs_space_info **space_info)
2457{
2458 struct btrfs_space_info *found;
2459
2460 found = __find_space_info(info, flags);
2461 if (found) {
Josef Bacik25179202008-10-29 14:49:05 -04002462 spin_lock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04002463 found->total_bytes += total_bytes;
2464 found->bytes_used += bytes_used;
Chris Mason8f18cf12008-04-25 16:53:30 -04002465 found->full = 0;
Josef Bacik25179202008-10-29 14:49:05 -04002466 spin_unlock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04002467 *space_info = found;
2468 return 0;
2469 }
Yan Zhengc146afa2008-11-12 14:34:12 -05002470 found = kzalloc(sizeof(*found), GFP_NOFS);
Chris Mason593060d2008-03-25 16:50:33 -04002471 if (!found)
2472 return -ENOMEM;
2473
Josef Bacik0f9dd462008-09-23 13:14:11 -04002474 INIT_LIST_HEAD(&found->block_groups);
Josef Bacik80eb2342008-10-29 14:49:05 -04002475 init_rwsem(&found->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002476 spin_lock_init(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04002477 found->flags = flags;
2478 found->total_bytes = total_bytes;
2479 found->bytes_used = bytes_used;
2480 found->bytes_pinned = 0;
Zheng Yane8569812008-09-26 10:05:48 -04002481 found->bytes_reserved = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05002482 found->bytes_readonly = 0;
Josef Bacik6a632092009-02-20 11:00:09 -05002483 found->bytes_delalloc = 0;
Chris Mason593060d2008-03-25 16:50:33 -04002484 found->full = 0;
Chris Mason0ef3e662008-05-24 14:04:53 -04002485 found->force_alloc = 0;
Chris Mason593060d2008-03-25 16:50:33 -04002486 *space_info = found;
Chris Mason4184ea72009-03-10 12:39:20 -04002487 list_add_rcu(&found->list, &info->space_info);
Chris Mason593060d2008-03-25 16:50:33 -04002488 return 0;
2489}
2490
Chris Mason8790d502008-04-03 16:29:03 -04002491static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
2492{
2493 u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
Chris Mason611f0e02008-04-03 16:29:03 -04002494 BTRFS_BLOCK_GROUP_RAID1 |
Chris Mason321aecc2008-04-16 10:49:51 -04002495 BTRFS_BLOCK_GROUP_RAID10 |
Chris Mason611f0e02008-04-03 16:29:03 -04002496 BTRFS_BLOCK_GROUP_DUP);
Chris Mason8790d502008-04-03 16:29:03 -04002497 if (extra_flags) {
2498 if (flags & BTRFS_BLOCK_GROUP_DATA)
2499 fs_info->avail_data_alloc_bits |= extra_flags;
2500 if (flags & BTRFS_BLOCK_GROUP_METADATA)
2501 fs_info->avail_metadata_alloc_bits |= extra_flags;
2502 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
2503 fs_info->avail_system_alloc_bits |= extra_flags;
2504 }
2505}
Chris Mason593060d2008-03-25 16:50:33 -04002506
Yan Zhengc146afa2008-11-12 14:34:12 -05002507static void set_block_group_readonly(struct btrfs_block_group_cache *cache)
2508{
2509 spin_lock(&cache->space_info->lock);
2510 spin_lock(&cache->lock);
2511 if (!cache->ro) {
2512 cache->space_info->bytes_readonly += cache->key.offset -
2513 btrfs_block_group_used(&cache->item);
2514 cache->ro = 1;
2515 }
2516 spin_unlock(&cache->lock);
2517 spin_unlock(&cache->space_info->lock);
2518}
2519
Yan Zheng2b820322008-11-17 21:11:30 -05002520u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
Chris Masonec44a352008-04-28 15:29:52 -04002521{
Yan Zheng2b820322008-11-17 21:11:30 -05002522 u64 num_devices = root->fs_info->fs_devices->rw_devices;
Chris Masona061fc82008-05-07 11:43:44 -04002523
2524 if (num_devices == 1)
2525 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
2526 if (num_devices < 4)
2527 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
2528
Chris Masonec44a352008-04-28 15:29:52 -04002529 if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
2530 (flags & (BTRFS_BLOCK_GROUP_RAID1 |
Chris Masona061fc82008-05-07 11:43:44 -04002531 BTRFS_BLOCK_GROUP_RAID10))) {
Chris Masonec44a352008-04-28 15:29:52 -04002532 flags &= ~BTRFS_BLOCK_GROUP_DUP;
Chris Masona061fc82008-05-07 11:43:44 -04002533 }
Chris Masonec44a352008-04-28 15:29:52 -04002534
2535 if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
Chris Masona061fc82008-05-07 11:43:44 -04002536 (flags & BTRFS_BLOCK_GROUP_RAID10)) {
Chris Masonec44a352008-04-28 15:29:52 -04002537 flags &= ~BTRFS_BLOCK_GROUP_RAID1;
Chris Masona061fc82008-05-07 11:43:44 -04002538 }
Chris Masonec44a352008-04-28 15:29:52 -04002539
2540 if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
2541 ((flags & BTRFS_BLOCK_GROUP_RAID1) |
2542 (flags & BTRFS_BLOCK_GROUP_RAID10) |
2543 (flags & BTRFS_BLOCK_GROUP_DUP)))
2544 flags &= ~BTRFS_BLOCK_GROUP_RAID0;
2545 return flags;
2546}
2547
Josef Bacik6a632092009-02-20 11:00:09 -05002548static u64 btrfs_get_alloc_profile(struct btrfs_root *root, u64 data)
2549{
2550 struct btrfs_fs_info *info = root->fs_info;
2551 u64 alloc_profile;
2552
2553 if (data) {
2554 alloc_profile = info->avail_data_alloc_bits &
2555 info->data_alloc_profile;
2556 data = BTRFS_BLOCK_GROUP_DATA | alloc_profile;
2557 } else if (root == root->fs_info->chunk_root) {
2558 alloc_profile = info->avail_system_alloc_bits &
2559 info->system_alloc_profile;
2560 data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile;
2561 } else {
2562 alloc_profile = info->avail_metadata_alloc_bits &
2563 info->metadata_alloc_profile;
2564 data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile;
2565 }
2566
2567 return btrfs_reduce_alloc_profile(root, data);
2568}
2569
2570void btrfs_set_inode_space_info(struct btrfs_root *root, struct inode *inode)
2571{
2572 u64 alloc_target;
2573
2574 alloc_target = btrfs_get_alloc_profile(root, 1);
2575 BTRFS_I(inode)->space_info = __find_space_info(root->fs_info,
2576 alloc_target);
2577}
2578
2579/*
2580 * for now this just makes sure we have at least 5% of our metadata space free
2581 * for use.
2582 */
2583int btrfs_check_metadata_free_space(struct btrfs_root *root)
2584{
2585 struct btrfs_fs_info *info = root->fs_info;
2586 struct btrfs_space_info *meta_sinfo;
2587 u64 alloc_target, thresh;
Josef Bacik4e06bdd2009-02-20 10:59:53 -05002588 int committed = 0, ret;
Josef Bacik6a632092009-02-20 11:00:09 -05002589
2590 /* get the space info for where the metadata will live */
2591 alloc_target = btrfs_get_alloc_profile(root, 0);
2592 meta_sinfo = __find_space_info(info, alloc_target);
2593
Josef Bacik4e06bdd2009-02-20 10:59:53 -05002594again:
Josef Bacik6a632092009-02-20 11:00:09 -05002595 spin_lock(&meta_sinfo->lock);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05002596 if (!meta_sinfo->full)
2597 thresh = meta_sinfo->total_bytes * 80;
2598 else
2599 thresh = meta_sinfo->total_bytes * 95;
Josef Bacik6a632092009-02-20 11:00:09 -05002600
2601 do_div(thresh, 100);
2602
2603 if (meta_sinfo->bytes_used + meta_sinfo->bytes_reserved +
2604 meta_sinfo->bytes_pinned + meta_sinfo->bytes_readonly > thresh) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05002605 struct btrfs_trans_handle *trans;
2606 if (!meta_sinfo->full) {
2607 meta_sinfo->force_alloc = 1;
2608 spin_unlock(&meta_sinfo->lock);
2609
2610 trans = btrfs_start_transaction(root, 1);
2611 if (!trans)
2612 return -ENOMEM;
2613
2614 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
2615 2 * 1024 * 1024, alloc_target, 0);
2616 btrfs_end_transaction(trans, root);
2617 goto again;
2618 }
Josef Bacik6a632092009-02-20 11:00:09 -05002619 spin_unlock(&meta_sinfo->lock);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05002620
2621 if (!committed) {
2622 committed = 1;
2623 trans = btrfs_join_transaction(root, 1);
2624 if (!trans)
2625 return -ENOMEM;
2626 ret = btrfs_commit_transaction(trans, root);
2627 if (ret)
2628 return ret;
2629 goto again;
2630 }
Josef Bacik6a632092009-02-20 11:00:09 -05002631 return -ENOSPC;
2632 }
2633 spin_unlock(&meta_sinfo->lock);
2634
2635 return 0;
2636}
2637
2638/*
2639 * This will check the space that the inode allocates from to make sure we have
2640 * enough space for bytes.
2641 */
2642int btrfs_check_data_free_space(struct btrfs_root *root, struct inode *inode,
2643 u64 bytes)
2644{
2645 struct btrfs_space_info *data_sinfo;
Josef Bacik4e06bdd2009-02-20 10:59:53 -05002646 int ret = 0, committed = 0;
Josef Bacik6a632092009-02-20 11:00:09 -05002647
2648 /* make sure bytes are sectorsize aligned */
2649 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
2650
2651 data_sinfo = BTRFS_I(inode)->space_info;
2652again:
2653 /* make sure we have enough space to handle the data first */
2654 spin_lock(&data_sinfo->lock);
2655 if (data_sinfo->total_bytes - data_sinfo->bytes_used -
2656 data_sinfo->bytes_delalloc - data_sinfo->bytes_reserved -
2657 data_sinfo->bytes_pinned - data_sinfo->bytes_readonly -
2658 data_sinfo->bytes_may_use < bytes) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05002659 struct btrfs_trans_handle *trans;
2660
Josef Bacik6a632092009-02-20 11:00:09 -05002661 /*
2662 * if we don't have enough free bytes in this space then we need
2663 * to alloc a new chunk.
2664 */
2665 if (!data_sinfo->full) {
2666 u64 alloc_target;
Josef Bacik6a632092009-02-20 11:00:09 -05002667
2668 data_sinfo->force_alloc = 1;
2669 spin_unlock(&data_sinfo->lock);
2670
2671 alloc_target = btrfs_get_alloc_profile(root, 1);
2672 trans = btrfs_start_transaction(root, 1);
2673 if (!trans)
2674 return -ENOMEM;
2675
2676 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
2677 bytes + 2 * 1024 * 1024,
2678 alloc_target, 0);
2679 btrfs_end_transaction(trans, root);
2680 if (ret)
2681 return ret;
2682 goto again;
2683 }
2684 spin_unlock(&data_sinfo->lock);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05002685
2686 /* commit the current transaction and try again */
2687 if (!committed) {
2688 committed = 1;
2689 trans = btrfs_join_transaction(root, 1);
2690 if (!trans)
2691 return -ENOMEM;
2692 ret = btrfs_commit_transaction(trans, root);
2693 if (ret)
2694 return ret;
2695 goto again;
2696 }
2697
Josef Bacik6a632092009-02-20 11:00:09 -05002698 printk(KERN_ERR "no space left, need %llu, %llu delalloc bytes"
2699 ", %llu bytes_used, %llu bytes_reserved, "
2700 "%llu bytes_pinned, %llu bytes_readonly, %llu may use"
Joel Becker21380932009-04-21 12:38:29 -07002701 "%llu total\n", (unsigned long long)bytes,
2702 (unsigned long long)data_sinfo->bytes_delalloc,
2703 (unsigned long long)data_sinfo->bytes_used,
2704 (unsigned long long)data_sinfo->bytes_reserved,
2705 (unsigned long long)data_sinfo->bytes_pinned,
2706 (unsigned long long)data_sinfo->bytes_readonly,
2707 (unsigned long long)data_sinfo->bytes_may_use,
2708 (unsigned long long)data_sinfo->total_bytes);
Josef Bacik6a632092009-02-20 11:00:09 -05002709 return -ENOSPC;
2710 }
2711 data_sinfo->bytes_may_use += bytes;
2712 BTRFS_I(inode)->reserved_bytes += bytes;
2713 spin_unlock(&data_sinfo->lock);
2714
2715 return btrfs_check_metadata_free_space(root);
2716}
2717
2718/*
2719 * if there was an error for whatever reason after calling
2720 * btrfs_check_data_free_space, call this so we can cleanup the counters.
2721 */
2722void btrfs_free_reserved_data_space(struct btrfs_root *root,
2723 struct inode *inode, u64 bytes)
2724{
2725 struct btrfs_space_info *data_sinfo;
2726
2727 /* make sure bytes are sectorsize aligned */
2728 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
2729
2730 data_sinfo = BTRFS_I(inode)->space_info;
2731 spin_lock(&data_sinfo->lock);
2732 data_sinfo->bytes_may_use -= bytes;
2733 BTRFS_I(inode)->reserved_bytes -= bytes;
2734 spin_unlock(&data_sinfo->lock);
2735}
2736
2737/* called when we are adding a delalloc extent to the inode's io_tree */
2738void btrfs_delalloc_reserve_space(struct btrfs_root *root, struct inode *inode,
2739 u64 bytes)
2740{
2741 struct btrfs_space_info *data_sinfo;
2742
2743 /* get the space info for where this inode will be storing its data */
2744 data_sinfo = BTRFS_I(inode)->space_info;
2745
2746 /* make sure we have enough space to handle the data first */
2747 spin_lock(&data_sinfo->lock);
2748 data_sinfo->bytes_delalloc += bytes;
2749
2750 /*
2751 * we are adding a delalloc extent without calling
2752 * btrfs_check_data_free_space first. This happens on a weird
2753 * writepage condition, but shouldn't hurt our accounting
2754 */
2755 if (unlikely(bytes > BTRFS_I(inode)->reserved_bytes)) {
2756 data_sinfo->bytes_may_use -= BTRFS_I(inode)->reserved_bytes;
2757 BTRFS_I(inode)->reserved_bytes = 0;
2758 } else {
2759 data_sinfo->bytes_may_use -= bytes;
2760 BTRFS_I(inode)->reserved_bytes -= bytes;
2761 }
2762
2763 spin_unlock(&data_sinfo->lock);
2764}
2765
2766/* called when we are clearing an delalloc extent from the inode's io_tree */
2767void btrfs_delalloc_free_space(struct btrfs_root *root, struct inode *inode,
2768 u64 bytes)
2769{
2770 struct btrfs_space_info *info;
2771
2772 info = BTRFS_I(inode)->space_info;
2773
2774 spin_lock(&info->lock);
2775 info->bytes_delalloc -= bytes;
2776 spin_unlock(&info->lock);
2777}
2778
Josef Bacik97e728d2009-04-21 17:40:57 -04002779static void force_metadata_allocation(struct btrfs_fs_info *info)
2780{
2781 struct list_head *head = &info->space_info;
2782 struct btrfs_space_info *found;
2783
2784 rcu_read_lock();
2785 list_for_each_entry_rcu(found, head, list) {
2786 if (found->flags & BTRFS_BLOCK_GROUP_METADATA)
2787 found->force_alloc = 1;
2788 }
2789 rcu_read_unlock();
2790}
2791
Chris Mason6324fbf2008-03-24 15:01:59 -04002792static int do_chunk_alloc(struct btrfs_trans_handle *trans,
2793 struct btrfs_root *extent_root, u64 alloc_bytes,
Chris Mason0ef3e662008-05-24 14:04:53 -04002794 u64 flags, int force)
Chris Mason6324fbf2008-03-24 15:01:59 -04002795{
2796 struct btrfs_space_info *space_info;
Josef Bacik97e728d2009-04-21 17:40:57 -04002797 struct btrfs_fs_info *fs_info = extent_root->fs_info;
Chris Mason6324fbf2008-03-24 15:01:59 -04002798 u64 thresh;
Yan Zhengc146afa2008-11-12 14:34:12 -05002799 int ret = 0;
2800
Josef Bacik97e728d2009-04-21 17:40:57 -04002801 mutex_lock(&fs_info->chunk_mutex);
Chris Mason6324fbf2008-03-24 15:01:59 -04002802
Yan Zheng2b820322008-11-17 21:11:30 -05002803 flags = btrfs_reduce_alloc_profile(extent_root, flags);
Chris Masonec44a352008-04-28 15:29:52 -04002804
Chris Mason6324fbf2008-03-24 15:01:59 -04002805 space_info = __find_space_info(extent_root->fs_info, flags);
Chris Mason593060d2008-03-25 16:50:33 -04002806 if (!space_info) {
2807 ret = update_space_info(extent_root->fs_info, flags,
2808 0, 0, &space_info);
2809 BUG_ON(ret);
2810 }
Chris Mason6324fbf2008-03-24 15:01:59 -04002811 BUG_ON(!space_info);
2812
Josef Bacik25179202008-10-29 14:49:05 -04002813 spin_lock(&space_info->lock);
Chris Mason0ef3e662008-05-24 14:04:53 -04002814 if (space_info->force_alloc) {
2815 force = 1;
2816 space_info->force_alloc = 0;
2817 }
Josef Bacik25179202008-10-29 14:49:05 -04002818 if (space_info->full) {
2819 spin_unlock(&space_info->lock);
Chris Mason925baed2008-06-25 16:01:30 -04002820 goto out;
Josef Bacik25179202008-10-29 14:49:05 -04002821 }
Chris Mason6324fbf2008-03-24 15:01:59 -04002822
Yan Zhengc146afa2008-11-12 14:34:12 -05002823 thresh = space_info->total_bytes - space_info->bytes_readonly;
2824 thresh = div_factor(thresh, 6);
Chris Mason0ef3e662008-05-24 14:04:53 -04002825 if (!force &&
Zheng Yane8569812008-09-26 10:05:48 -04002826 (space_info->bytes_used + space_info->bytes_pinned +
Josef Bacik25179202008-10-29 14:49:05 -04002827 space_info->bytes_reserved + alloc_bytes) < thresh) {
2828 spin_unlock(&space_info->lock);
Chris Mason925baed2008-06-25 16:01:30 -04002829 goto out;
Josef Bacik25179202008-10-29 14:49:05 -04002830 }
Josef Bacik25179202008-10-29 14:49:05 -04002831 spin_unlock(&space_info->lock);
2832
Josef Bacik97e728d2009-04-21 17:40:57 -04002833 /*
2834 * if we're doing a data chunk, go ahead and make sure that
2835 * we keep a reasonable number of metadata chunks allocated in the
2836 * FS as well.
2837 */
2838 if (flags & BTRFS_BLOCK_GROUP_DATA) {
2839 fs_info->data_chunk_allocations++;
2840 if (!(fs_info->data_chunk_allocations %
2841 fs_info->metadata_ratio))
2842 force_metadata_allocation(fs_info);
2843 }
2844
Yan Zheng2b820322008-11-17 21:11:30 -05002845 ret = btrfs_alloc_chunk(trans, extent_root, flags);
Chris Masond3977122009-01-05 21:25:51 -05002846 if (ret)
Chris Mason6324fbf2008-03-24 15:01:59 -04002847 space_info->full = 1;
Chris Masona74a4b92008-06-25 16:01:31 -04002848out:
Yan Zhengc146afa2008-11-12 14:34:12 -05002849 mutex_unlock(&extent_root->fs_info->chunk_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002850 return ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04002851}
2852
Chris Mason9078a3e2007-04-26 16:46:15 -04002853static int update_block_group(struct btrfs_trans_handle *trans,
2854 struct btrfs_root *root,
Chris Masondb945352007-10-15 16:15:53 -04002855 u64 bytenr, u64 num_bytes, int alloc,
Chris Mason0b86a832008-03-24 15:01:56 -04002856 int mark_free)
Chris Mason9078a3e2007-04-26 16:46:15 -04002857{
2858 struct btrfs_block_group_cache *cache;
2859 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -04002860 u64 total = num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04002861 u64 old_val;
Chris Masondb945352007-10-15 16:15:53 -04002862 u64 byte_in_group;
Chris Mason3e1ad542007-05-07 20:03:49 -04002863
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002864 /* block accounting for super block */
2865 spin_lock(&info->delalloc_lock);
2866 old_val = btrfs_super_bytes_used(&info->super_copy);
2867 if (alloc)
2868 old_val += num_bytes;
2869 else
2870 old_val -= num_bytes;
2871 btrfs_set_super_bytes_used(&info->super_copy, old_val);
2872
2873 /* block accounting for root item */
2874 old_val = btrfs_root_used(&root->root_item);
2875 if (alloc)
2876 old_val += num_bytes;
2877 else
2878 old_val -= num_bytes;
2879 btrfs_set_root_used(&root->root_item, old_val);
2880 spin_unlock(&info->delalloc_lock);
2881
Chris Masond3977122009-01-05 21:25:51 -05002882 while (total) {
Chris Masondb945352007-10-15 16:15:53 -04002883 cache = btrfs_lookup_block_group(info, bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05002884 if (!cache)
Chris Mason9078a3e2007-04-26 16:46:15 -04002885 return -1;
Chris Masondb945352007-10-15 16:15:53 -04002886 byte_in_group = bytenr - cache->key.objectid;
2887 WARN_ON(byte_in_group > cache->key.offset);
Chris Mason9078a3e2007-04-26 16:46:15 -04002888
Josef Bacik25179202008-10-29 14:49:05 -04002889 spin_lock(&cache->space_info->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04002890 spin_lock(&cache->lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002891 cache->dirty = 1;
Chris Mason9078a3e2007-04-26 16:46:15 -04002892 old_val = btrfs_block_group_used(&cache->item);
Chris Masondb945352007-10-15 16:15:53 -04002893 num_bytes = min(total, cache->key.offset - byte_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -04002894 if (alloc) {
Chris Masondb945352007-10-15 16:15:53 -04002895 old_val += num_bytes;
Chris Mason6324fbf2008-03-24 15:01:59 -04002896 cache->space_info->bytes_used += num_bytes;
Yan Zhenga512bbf2008-12-08 16:46:26 -05002897 if (cache->ro)
Yan Zhengc146afa2008-11-12 14:34:12 -05002898 cache->space_info->bytes_readonly -= num_bytes;
Chris Masonc286ac42008-07-22 23:06:41 -04002899 btrfs_set_block_group_used(&cache->item, old_val);
2900 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04002901 spin_unlock(&cache->space_info->lock);
Chris Masoncd1bc462007-04-27 10:08:34 -04002902 } else {
Chris Masondb945352007-10-15 16:15:53 -04002903 old_val -= num_bytes;
Chris Mason6324fbf2008-03-24 15:01:59 -04002904 cache->space_info->bytes_used -= num_bytes;
Yan Zhengc146afa2008-11-12 14:34:12 -05002905 if (cache->ro)
2906 cache->space_info->bytes_readonly += num_bytes;
Chris Masonc286ac42008-07-22 23:06:41 -04002907 btrfs_set_block_group_used(&cache->item, old_val);
2908 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04002909 spin_unlock(&cache->space_info->lock);
Chris Masonf510cfe2007-10-15 16:14:48 -04002910 if (mark_free) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04002911 int ret;
Liu Hui1f3c79a2009-01-05 15:57:51 -05002912
2913 ret = btrfs_discard_extent(root, bytenr,
2914 num_bytes);
2915 WARN_ON(ret);
2916
Josef Bacik0f9dd462008-09-23 13:14:11 -04002917 ret = btrfs_add_free_space(cache, bytenr,
2918 num_bytes);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002919 WARN_ON(ret);
Chris Masone37c9e62007-05-09 20:13:14 -04002920 }
Chris Masoncd1bc462007-04-27 10:08:34 -04002921 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04002922 btrfs_put_block_group(cache);
Chris Masondb945352007-10-15 16:15:53 -04002923 total -= num_bytes;
2924 bytenr += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04002925 }
2926 return 0;
2927}
Chris Mason6324fbf2008-03-24 15:01:59 -04002928
Chris Masona061fc82008-05-07 11:43:44 -04002929static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
2930{
Josef Bacik0f9dd462008-09-23 13:14:11 -04002931 struct btrfs_block_group_cache *cache;
Yan Zhengd2fb3432008-12-11 16:30:39 -05002932 u64 bytenr;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002933
2934 cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
2935 if (!cache)
Chris Masona061fc82008-05-07 11:43:44 -04002936 return 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002937
Yan Zhengd2fb3432008-12-11 16:30:39 -05002938 bytenr = cache->key.objectid;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002939 btrfs_put_block_group(cache);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002940
2941 return bytenr;
Chris Masona061fc82008-05-07 11:43:44 -04002942}
2943
Chris Masone02119d2008-09-05 16:13:11 -04002944int btrfs_update_pinned_extents(struct btrfs_root *root,
Yan324ae4d2007-11-16 14:57:08 -05002945 u64 bytenr, u64 num, int pin)
2946{
2947 u64 len;
2948 struct btrfs_block_group_cache *cache;
2949 struct btrfs_fs_info *fs_info = root->fs_info;
2950
2951 if (pin) {
2952 set_extent_dirty(&fs_info->pinned_extents,
2953 bytenr, bytenr + num - 1, GFP_NOFS);
2954 } else {
2955 clear_extent_dirty(&fs_info->pinned_extents,
2956 bytenr, bytenr + num - 1, GFP_NOFS);
2957 }
Chris Masonb9473432009-03-13 11:00:37 -04002958
Yan324ae4d2007-11-16 14:57:08 -05002959 while (num > 0) {
2960 cache = btrfs_lookup_block_group(fs_info, bytenr);
Zheng Yane8569812008-09-26 10:05:48 -04002961 BUG_ON(!cache);
2962 len = min(num, cache->key.offset -
2963 (bytenr - cache->key.objectid));
Yan324ae4d2007-11-16 14:57:08 -05002964 if (pin) {
Josef Bacik25179202008-10-29 14:49:05 -04002965 spin_lock(&cache->space_info->lock);
Zheng Yane8569812008-09-26 10:05:48 -04002966 spin_lock(&cache->lock);
2967 cache->pinned += len;
2968 cache->space_info->bytes_pinned += len;
2969 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04002970 spin_unlock(&cache->space_info->lock);
Yan324ae4d2007-11-16 14:57:08 -05002971 fs_info->total_pinned += len;
2972 } else {
Josef Bacik25179202008-10-29 14:49:05 -04002973 spin_lock(&cache->space_info->lock);
Zheng Yane8569812008-09-26 10:05:48 -04002974 spin_lock(&cache->lock);
2975 cache->pinned -= len;
2976 cache->space_info->bytes_pinned -= len;
2977 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04002978 spin_unlock(&cache->space_info->lock);
Yan324ae4d2007-11-16 14:57:08 -05002979 fs_info->total_pinned -= len;
Josef Bacik07103a32008-11-19 15:17:55 -05002980 if (cache->cached)
2981 btrfs_add_free_space(cache, bytenr, len);
Yan324ae4d2007-11-16 14:57:08 -05002982 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04002983 btrfs_put_block_group(cache);
Yan324ae4d2007-11-16 14:57:08 -05002984 bytenr += len;
2985 num -= len;
2986 }
2987 return 0;
2988}
Chris Mason9078a3e2007-04-26 16:46:15 -04002989
Zheng Yane8569812008-09-26 10:05:48 -04002990static int update_reserved_extents(struct btrfs_root *root,
2991 u64 bytenr, u64 num, int reserve)
2992{
2993 u64 len;
2994 struct btrfs_block_group_cache *cache;
2995 struct btrfs_fs_info *fs_info = root->fs_info;
2996
Zheng Yane8569812008-09-26 10:05:48 -04002997 while (num > 0) {
2998 cache = btrfs_lookup_block_group(fs_info, bytenr);
2999 BUG_ON(!cache);
3000 len = min(num, cache->key.offset -
3001 (bytenr - cache->key.objectid));
Josef Bacik25179202008-10-29 14:49:05 -04003002
3003 spin_lock(&cache->space_info->lock);
3004 spin_lock(&cache->lock);
Zheng Yane8569812008-09-26 10:05:48 -04003005 if (reserve) {
Zheng Yane8569812008-09-26 10:05:48 -04003006 cache->reserved += len;
3007 cache->space_info->bytes_reserved += len;
Zheng Yane8569812008-09-26 10:05:48 -04003008 } else {
Zheng Yane8569812008-09-26 10:05:48 -04003009 cache->reserved -= len;
3010 cache->space_info->bytes_reserved -= len;
Zheng Yane8569812008-09-26 10:05:48 -04003011 }
Josef Bacik25179202008-10-29 14:49:05 -04003012 spin_unlock(&cache->lock);
3013 spin_unlock(&cache->space_info->lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04003014 btrfs_put_block_group(cache);
Zheng Yane8569812008-09-26 10:05:48 -04003015 bytenr += len;
3016 num -= len;
3017 }
3018 return 0;
3019}
3020
Chris Masond1310b22008-01-24 16:13:08 -05003021int btrfs_copy_pinned(struct btrfs_root *root, struct extent_io_tree *copy)
Chris Masonccd467d2007-06-28 15:57:36 -04003022{
Chris Masonccd467d2007-06-28 15:57:36 -04003023 u64 last = 0;
Chris Mason1a5bc162007-10-15 16:15:26 -04003024 u64 start;
3025 u64 end;
Chris Masond1310b22008-01-24 16:13:08 -05003026 struct extent_io_tree *pinned_extents = &root->fs_info->pinned_extents;
Chris Masonccd467d2007-06-28 15:57:36 -04003027 int ret;
Chris Masonccd467d2007-06-28 15:57:36 -04003028
Chris Masond3977122009-01-05 21:25:51 -05003029 while (1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04003030 ret = find_first_extent_bit(pinned_extents, last,
3031 &start, &end, EXTENT_DIRTY);
3032 if (ret)
Chris Masonccd467d2007-06-28 15:57:36 -04003033 break;
Chris Mason1a5bc162007-10-15 16:15:26 -04003034 set_extent_dirty(copy, start, end, GFP_NOFS);
3035 last = end + 1;
Chris Masonccd467d2007-06-28 15:57:36 -04003036 }
3037 return 0;
3038}
3039
3040int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
3041 struct btrfs_root *root,
Chris Masond1310b22008-01-24 16:13:08 -05003042 struct extent_io_tree *unpin)
Chris Masona28ec192007-03-06 20:08:01 -05003043{
Chris Mason1a5bc162007-10-15 16:15:26 -04003044 u64 start;
3045 u64 end;
Chris Masona28ec192007-03-06 20:08:01 -05003046 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05003047
Chris Masond3977122009-01-05 21:25:51 -05003048 while (1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04003049 ret = find_first_extent_bit(unpin, 0, &start, &end,
3050 EXTENT_DIRTY);
3051 if (ret)
Chris Masona28ec192007-03-06 20:08:01 -05003052 break;
Liu Hui1f3c79a2009-01-05 15:57:51 -05003053
3054 ret = btrfs_discard_extent(root, start, end + 1 - start);
3055
Chris Masonb9473432009-03-13 11:00:37 -04003056 /* unlocks the pinned mutex */
Chris Masone02119d2008-09-05 16:13:11 -04003057 btrfs_update_pinned_extents(root, start, end + 1 - start, 0);
Chris Mason1a5bc162007-10-15 16:15:26 -04003058 clear_extent_dirty(unpin, start, end, GFP_NOFS);
Liu Hui1f3c79a2009-01-05 15:57:51 -05003059
Chris Masonb9473432009-03-13 11:00:37 -04003060 cond_resched();
Chris Masona28ec192007-03-06 20:08:01 -05003061 }
Liu Hui1f3c79a2009-01-05 15:57:51 -05003062 return ret;
Chris Masona28ec192007-03-06 20:08:01 -05003063}
3064
Zheng Yan31840ae2008-09-23 13:14:14 -04003065static int pin_down_bytes(struct btrfs_trans_handle *trans,
3066 struct btrfs_root *root,
Chris Masonb9473432009-03-13 11:00:37 -04003067 struct btrfs_path *path,
3068 u64 bytenr, u64 num_bytes, int is_data,
3069 struct extent_buffer **must_clean)
Chris Masone20d96d2007-03-22 12:13:20 -04003070{
Chris Mason1a5bc162007-10-15 16:15:26 -04003071 int err = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04003072 struct extent_buffer *buf;
Chris Mason78fae272007-03-25 11:35:08 -04003073
Zheng Yan31840ae2008-09-23 13:14:14 -04003074 if (is_data)
3075 goto pinit;
Chris Mason4bef0842008-09-08 11:18:08 -04003076
Zheng Yan31840ae2008-09-23 13:14:14 -04003077 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
3078 if (!buf)
3079 goto pinit;
Chris Mason4bef0842008-09-08 11:18:08 -04003080
Zheng Yan31840ae2008-09-23 13:14:14 -04003081 /* we can reuse a block if it hasn't been written
3082 * and it is from this transaction. We can't
3083 * reuse anything from the tree log root because
3084 * it has tiny sub-transactions.
3085 */
3086 if (btrfs_buffer_uptodate(buf, 0) &&
3087 btrfs_try_tree_lock(buf)) {
3088 u64 header_owner = btrfs_header_owner(buf);
3089 u64 header_transid = btrfs_header_generation(buf);
3090 if (header_owner != BTRFS_TREE_LOG_OBJECTID &&
3091 header_transid == trans->transid &&
3092 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
Chris Masonb9473432009-03-13 11:00:37 -04003093 *must_clean = buf;
Zheng Yan31840ae2008-09-23 13:14:14 -04003094 return 1;
Chris Mason8ef97622007-03-26 10:15:30 -04003095 }
Zheng Yan31840ae2008-09-23 13:14:14 -04003096 btrfs_tree_unlock(buf);
Chris Masonf4b9aa82007-03-27 11:05:53 -04003097 }
Zheng Yan31840ae2008-09-23 13:14:14 -04003098 free_extent_buffer(buf);
3099pinit:
Chris Masonb9473432009-03-13 11:00:37 -04003100 btrfs_set_path_blocking(path);
Chris Masonb9473432009-03-13 11:00:37 -04003101 /* unlocks the pinned mutex */
Zheng Yan31840ae2008-09-23 13:14:14 -04003102 btrfs_update_pinned_extents(root, bytenr, num_bytes, 1);
3103
Chris Masonbe744172007-05-06 10:15:01 -04003104 BUG_ON(err < 0);
Chris Masone20d96d2007-03-22 12:13:20 -04003105 return 0;
3106}
3107
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003108
3109static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
3110 struct btrfs_root *root,
3111 u64 bytenr, u64 num_bytes, u64 parent,
3112 u64 root_objectid, u64 owner_objectid,
3113 u64 owner_offset, int refs_to_drop,
3114 struct btrfs_delayed_extent_op *extent_op)
Chris Masona28ec192007-03-06 20:08:01 -05003115{
Chris Masone2fa7222007-03-12 16:22:34 -04003116 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003117 struct btrfs_path *path;
Chris Mason1261ec42007-03-20 20:35:03 -04003118 struct btrfs_fs_info *info = root->fs_info;
3119 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04003120 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003121 struct btrfs_extent_item *ei;
3122 struct btrfs_extent_inline_ref *iref;
Chris Masona28ec192007-03-06 20:08:01 -05003123 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003124 int is_data;
Chris Mason952fcca2008-02-18 16:33:44 -05003125 int extent_slot = 0;
3126 int found_extent = 0;
3127 int num_to_del = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003128 u32 item_size;
3129 u64 refs;
Chris Mason037e6392007-03-07 11:50:24 -05003130
Chris Mason5caf2a02007-04-02 11:20:42 -04003131 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003132 if (!path)
3133 return -ENOMEM;
3134
Chris Mason3c12ac72008-04-21 12:01:38 -04003135 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04003136 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003137
3138 is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
3139 BUG_ON(!is_data && refs_to_drop != 1);
3140
3141 ret = lookup_extent_backref(trans, extent_root, path, &iref,
3142 bytenr, num_bytes, parent,
3143 root_objectid, owner_objectid,
3144 owner_offset);
Chris Mason7bb86312007-12-11 09:25:06 -05003145 if (ret == 0) {
Chris Mason952fcca2008-02-18 16:33:44 -05003146 extent_slot = path->slots[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003147 while (extent_slot >= 0) {
3148 btrfs_item_key_to_cpu(path->nodes[0], &key,
Chris Mason952fcca2008-02-18 16:33:44 -05003149 extent_slot);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003150 if (key.objectid != bytenr)
Chris Mason952fcca2008-02-18 16:33:44 -05003151 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003152 if (key.type == BTRFS_EXTENT_ITEM_KEY &&
3153 key.offset == num_bytes) {
Chris Mason952fcca2008-02-18 16:33:44 -05003154 found_extent = 1;
3155 break;
3156 }
3157 if (path->slots[0] - extent_slot > 5)
3158 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003159 extent_slot--;
Chris Mason952fcca2008-02-18 16:33:44 -05003160 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003161#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3162 item_size = btrfs_item_size_nr(path->nodes[0], extent_slot);
3163 if (found_extent && item_size < sizeof(*ei))
3164 found_extent = 0;
3165#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04003166 if (!found_extent) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003167 BUG_ON(iref);
Chris Mason56bec292009-03-13 10:10:06 -04003168 ret = remove_extent_backref(trans, extent_root, path,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003169 NULL, refs_to_drop,
3170 is_data);
Zheng Yan31840ae2008-09-23 13:14:14 -04003171 BUG_ON(ret);
3172 btrfs_release_path(extent_root, path);
Chris Masonb9473432009-03-13 11:00:37 -04003173 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003174
3175 key.objectid = bytenr;
3176 key.type = BTRFS_EXTENT_ITEM_KEY;
3177 key.offset = num_bytes;
3178
Zheng Yan31840ae2008-09-23 13:14:14 -04003179 ret = btrfs_search_slot(trans, extent_root,
3180 &key, path, -1, 1);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003181 if (ret) {
3182 printk(KERN_ERR "umm, got %d back from search"
Chris Masond3977122009-01-05 21:25:51 -05003183 ", was looking for %llu\n", ret,
3184 (unsigned long long)bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003185 btrfs_print_leaf(extent_root, path->nodes[0]);
3186 }
Zheng Yan31840ae2008-09-23 13:14:14 -04003187 BUG_ON(ret);
3188 extent_slot = path->slots[0];
3189 }
Chris Mason7bb86312007-12-11 09:25:06 -05003190 } else {
3191 btrfs_print_leaf(extent_root, path->nodes[0]);
3192 WARN_ON(1);
Chris Masond3977122009-01-05 21:25:51 -05003193 printk(KERN_ERR "btrfs unable to find ref byte nr %llu "
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003194 "parent %llu root %llu owner %llu offset %llu\n",
Chris Masond3977122009-01-05 21:25:51 -05003195 (unsigned long long)bytenr,
Chris Mason56bec292009-03-13 10:10:06 -04003196 (unsigned long long)parent,
Chris Masond3977122009-01-05 21:25:51 -05003197 (unsigned long long)root_objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003198 (unsigned long long)owner_objectid,
3199 (unsigned long long)owner_offset);
Chris Mason7bb86312007-12-11 09:25:06 -05003200 }
Chris Mason5f39d392007-10-15 16:14:19 -04003201
3202 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003203 item_size = btrfs_item_size_nr(leaf, extent_slot);
3204#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3205 if (item_size < sizeof(*ei)) {
3206 BUG_ON(found_extent || extent_slot != path->slots[0]);
3207 ret = convert_extent_item_v0(trans, extent_root, path,
3208 owner_objectid, 0);
3209 BUG_ON(ret < 0);
3210
3211 btrfs_release_path(extent_root, path);
3212 path->leave_spinning = 1;
3213
3214 key.objectid = bytenr;
3215 key.type = BTRFS_EXTENT_ITEM_KEY;
3216 key.offset = num_bytes;
3217
3218 ret = btrfs_search_slot(trans, extent_root, &key, path,
3219 -1, 1);
3220 if (ret) {
3221 printk(KERN_ERR "umm, got %d back from search"
3222 ", was looking for %llu\n", ret,
3223 (unsigned long long)bytenr);
3224 btrfs_print_leaf(extent_root, path->nodes[0]);
3225 }
3226 BUG_ON(ret);
3227 extent_slot = path->slots[0];
3228 leaf = path->nodes[0];
3229 item_size = btrfs_item_size_nr(leaf, extent_slot);
3230 }
3231#endif
3232 BUG_ON(item_size < sizeof(*ei));
Chris Mason952fcca2008-02-18 16:33:44 -05003233 ei = btrfs_item_ptr(leaf, extent_slot,
Chris Mason123abc82007-03-14 14:14:43 -04003234 struct btrfs_extent_item);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003235 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
3236 struct btrfs_tree_block_info *bi;
3237 BUG_ON(item_size < sizeof(*ei) + sizeof(*bi));
3238 bi = (struct btrfs_tree_block_info *)(ei + 1);
3239 WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
Chris Mason952fcca2008-02-18 16:33:44 -05003240 }
3241
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003242 refs = btrfs_extent_refs(leaf, ei);
3243 BUG_ON(refs < refs_to_drop);
3244 refs -= refs_to_drop;
3245
3246 if (refs > 0) {
3247 if (extent_op)
3248 __run_delayed_extent_op(extent_op, leaf, ei);
3249 /*
3250 * In the case of inline back ref, reference count will
3251 * be updated by remove_extent_backref
3252 */
3253 if (iref) {
3254 BUG_ON(!found_extent);
3255 } else {
3256 btrfs_set_extent_refs(leaf, ei, refs);
3257 btrfs_mark_buffer_dirty(leaf);
3258 }
3259 if (found_extent) {
3260 ret = remove_extent_backref(trans, extent_root, path,
3261 iref, refs_to_drop,
3262 is_data);
3263 BUG_ON(ret);
3264 }
3265 } else {
3266 int mark_free = 0;
Chris Masonb9473432009-03-13 11:00:37 -04003267 struct extent_buffer *must_clean = NULL;
Chris Mason78fae272007-03-25 11:35:08 -04003268
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003269 if (found_extent) {
3270 BUG_ON(is_data && refs_to_drop !=
3271 extent_data_ref_count(root, path, iref));
3272 if (iref) {
3273 BUG_ON(path->slots[0] != extent_slot);
3274 } else {
3275 BUG_ON(path->slots[0] != extent_slot + 1);
3276 path->slots[0] = extent_slot;
3277 num_to_del = 2;
3278 }
Chris Mason78fae272007-03-25 11:35:08 -04003279 }
Chris Masonb9473432009-03-13 11:00:37 -04003280
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003281 ret = pin_down_bytes(trans, root, path, bytenr,
3282 num_bytes, is_data, &must_clean);
3283 if (ret > 0)
3284 mark_free = 1;
3285 BUG_ON(ret < 0);
Chris Masonb9473432009-03-13 11:00:37 -04003286 /*
3287 * it is going to be very rare for someone to be waiting
3288 * on the block we're freeing. del_items might need to
3289 * schedule, so rather than get fancy, just force it
3290 * to blocking here
3291 */
3292 if (must_clean)
3293 btrfs_set_lock_blocking(must_clean);
3294
Chris Mason952fcca2008-02-18 16:33:44 -05003295 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
3296 num_to_del);
Zheng Yan31840ae2008-09-23 13:14:14 -04003297 BUG_ON(ret);
Josef Bacik25179202008-10-29 14:49:05 -04003298 btrfs_release_path(extent_root, path);
David Woodhouse21af8042008-08-12 14:13:26 +01003299
Chris Masonb9473432009-03-13 11:00:37 -04003300 if (must_clean) {
3301 clean_tree_block(NULL, root, must_clean);
3302 btrfs_tree_unlock(must_clean);
3303 free_extent_buffer(must_clean);
3304 }
3305
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003306 if (is_data) {
Chris Mason459931e2008-12-10 09:10:46 -05003307 ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
3308 BUG_ON(ret);
Chris Masond57e62b2009-03-31 13:47:50 -04003309 } else {
3310 invalidate_mapping_pages(info->btree_inode->i_mapping,
3311 bytenr >> PAGE_CACHE_SHIFT,
3312 (bytenr + num_bytes - 1) >> PAGE_CACHE_SHIFT);
Chris Mason459931e2008-12-10 09:10:46 -05003313 }
3314
Chris Masondcbdd4d2008-12-16 13:51:01 -05003315 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
3316 mark_free);
3317 BUG_ON(ret);
Chris Masona28ec192007-03-06 20:08:01 -05003318 }
Chris Mason5caf2a02007-04-02 11:20:42 -04003319 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -05003320 return ret;
3321}
3322
3323/*
Chris Mason1887be62009-03-13 10:11:24 -04003324 * when we free an extent, it is possible (and likely) that we free the last
3325 * delayed ref for that extent as well. This searches the delayed ref tree for
3326 * a given extent, and if there are no other delayed refs to be processed, it
3327 * removes it from the tree.
3328 */
3329static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
3330 struct btrfs_root *root, u64 bytenr)
3331{
3332 struct btrfs_delayed_ref_head *head;
3333 struct btrfs_delayed_ref_root *delayed_refs;
3334 struct btrfs_delayed_ref_node *ref;
3335 struct rb_node *node;
3336 int ret;
3337
3338 delayed_refs = &trans->transaction->delayed_refs;
3339 spin_lock(&delayed_refs->lock);
3340 head = btrfs_find_delayed_ref_head(trans, bytenr);
3341 if (!head)
3342 goto out;
3343
3344 node = rb_prev(&head->node.rb_node);
3345 if (!node)
3346 goto out;
3347
3348 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
3349
3350 /* there are still entries for this ref, we can't drop it */
3351 if (ref->bytenr == bytenr)
3352 goto out;
3353
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003354 if (head->extent_op) {
3355 if (!head->must_insert_reserved)
3356 goto out;
3357 kfree(head->extent_op);
3358 head->extent_op = NULL;
3359 }
3360
Chris Mason1887be62009-03-13 10:11:24 -04003361 /*
3362 * waiting for the lock here would deadlock. If someone else has it
3363 * locked they are already in the process of dropping it anyway
3364 */
3365 if (!mutex_trylock(&head->mutex))
3366 goto out;
3367
3368 /*
3369 * at this point we have a head with no other entries. Go
3370 * ahead and process it.
3371 */
3372 head->node.in_tree = 0;
3373 rb_erase(&head->node.rb_node, &delayed_refs->root);
Chris Masonc3e69d52009-03-13 10:17:05 -04003374
Chris Mason1887be62009-03-13 10:11:24 -04003375 delayed_refs->num_entries--;
3376
3377 /*
3378 * we don't take a ref on the node because we're removing it from the
3379 * tree, so we just steal the ref the tree was holding.
3380 */
Chris Masonc3e69d52009-03-13 10:17:05 -04003381 delayed_refs->num_heads--;
3382 if (list_empty(&head->cluster))
3383 delayed_refs->num_heads_ready--;
3384
3385 list_del_init(&head->cluster);
Chris Mason1887be62009-03-13 10:11:24 -04003386 spin_unlock(&delayed_refs->lock);
3387
3388 ret = run_one_delayed_ref(trans, root->fs_info->tree_root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003389 &head->node, head->extent_op,
3390 head->must_insert_reserved);
Chris Mason1887be62009-03-13 10:11:24 -04003391 BUG_ON(ret);
3392 btrfs_put_delayed_ref(&head->node);
3393 return 0;
3394out:
3395 spin_unlock(&delayed_refs->lock);
3396 return 0;
3397}
3398
Chris Mason925baed2008-06-25 16:01:30 -04003399int btrfs_free_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04003400 struct btrfs_root *root,
3401 u64 bytenr, u64 num_bytes, u64 parent,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003402 u64 root_objectid, u64 owner, u64 offset)
Chris Mason925baed2008-06-25 16:01:30 -04003403{
3404 int ret;
3405
Chris Mason56bec292009-03-13 10:10:06 -04003406 /*
3407 * tree log blocks never actually go into the extent allocation
3408 * tree, just update pinning info and exit early.
Chris Mason56bec292009-03-13 10:10:06 -04003409 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003410 if (root_objectid == BTRFS_TREE_LOG_OBJECTID) {
3411 WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID);
Chris Masonb9473432009-03-13 11:00:37 -04003412 /* unlocks the pinned mutex */
Chris Mason56bec292009-03-13 10:10:06 -04003413 btrfs_update_pinned_extents(root, bytenr, num_bytes, 1);
Chris Mason56bec292009-03-13 10:10:06 -04003414 update_reserved_extents(root, bytenr, num_bytes, 0);
3415 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003416 } else if (owner < BTRFS_FIRST_FREE_OBJECTID) {
3417 ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes,
3418 parent, root_objectid, (int)owner,
3419 BTRFS_DROP_DELAYED_REF, NULL);
Chris Mason1887be62009-03-13 10:11:24 -04003420 BUG_ON(ret);
3421 ret = check_ref_cleanup(trans, root, bytenr);
3422 BUG_ON(ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003423 } else {
3424 ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes,
3425 parent, root_objectid, owner,
3426 offset, BTRFS_DROP_DELAYED_REF, NULL);
3427 BUG_ON(ret);
Chris Mason56bec292009-03-13 10:10:06 -04003428 }
Chris Mason925baed2008-06-25 16:01:30 -04003429 return ret;
3430}
3431
Chris Mason87ee04e2007-11-30 11:30:34 -05003432static u64 stripe_align(struct btrfs_root *root, u64 val)
3433{
3434 u64 mask = ((u64)root->stripesize - 1);
3435 u64 ret = (val + mask) & ~mask;
3436 return ret;
3437}
3438
Chris Masonfec577f2007-02-26 10:40:21 -05003439/*
3440 * walks the btree of allocated extents and find a hole of a given size.
3441 * The key ins is changed to record the hole:
3442 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -04003443 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -05003444 * ins->offset == number of blocks
3445 * Any available blocks before search_start are skipped.
3446 */
Chris Masond3977122009-01-05 21:25:51 -05003447static noinline int find_free_extent(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05003448 struct btrfs_root *orig_root,
3449 u64 num_bytes, u64 empty_size,
3450 u64 search_start, u64 search_end,
3451 u64 hint_byte, struct btrfs_key *ins,
3452 u64 exclude_start, u64 exclude_nr,
3453 int data)
Chris Masonfec577f2007-02-26 10:40:21 -05003454{
Josef Bacik80eb2342008-10-29 14:49:05 -04003455 int ret = 0;
Chris Masond3977122009-01-05 21:25:51 -05003456 struct btrfs_root *root = orig_root->fs_info->extent_root;
Chris Masonfa9c0d792009-04-03 09:47:43 -04003457 struct btrfs_free_cluster *last_ptr = NULL;
Josef Bacik80eb2342008-10-29 14:49:05 -04003458 struct btrfs_block_group_cache *block_group = NULL;
Chris Mason239b14b2008-03-24 15:02:07 -04003459 int empty_cluster = 2 * 1024 * 1024;
Chris Mason0ef3e662008-05-24 14:04:53 -04003460 int allowed_chunk_alloc = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04003461 struct btrfs_space_info *space_info;
Chris Masonfa9c0d792009-04-03 09:47:43 -04003462 int last_ptr_loop = 0;
3463 int loop = 0;
Chris Masonfec577f2007-02-26 10:40:21 -05003464
Chris Masondb945352007-10-15 16:15:53 -04003465 WARN_ON(num_bytes < root->sectorsize);
Chris Masonb1a4d962007-04-04 15:27:52 -04003466 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
Josef Bacik80eb2342008-10-29 14:49:05 -04003467 ins->objectid = 0;
3468 ins->offset = 0;
Chris Masonb1a4d962007-04-04 15:27:52 -04003469
Josef Bacik2552d172009-04-03 10:14:19 -04003470 space_info = __find_space_info(root->fs_info, data);
3471
Chris Mason0ef3e662008-05-24 14:04:53 -04003472 if (orig_root->ref_cows || empty_size)
3473 allowed_chunk_alloc = 1;
3474
Chris Mason239b14b2008-03-24 15:02:07 -04003475 if (data & BTRFS_BLOCK_GROUP_METADATA) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04003476 last_ptr = &root->fs_info->meta_alloc_cluster;
Chris Mason536ac8a2009-02-12 09:41:38 -05003477 if (!btrfs_test_opt(root, SSD))
3478 empty_cluster = 64 * 1024;
Chris Mason239b14b2008-03-24 15:02:07 -04003479 }
3480
Chris Masonfa9c0d792009-04-03 09:47:43 -04003481 if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD)) {
3482 last_ptr = &root->fs_info->data_alloc_cluster;
3483 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04003484
Chris Mason239b14b2008-03-24 15:02:07 -04003485 if (last_ptr) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04003486 spin_lock(&last_ptr->lock);
3487 if (last_ptr->block_group)
3488 hint_byte = last_ptr->window_start;
3489 spin_unlock(&last_ptr->lock);
Chris Mason239b14b2008-03-24 15:02:07 -04003490 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04003491
Chris Masona061fc82008-05-07 11:43:44 -04003492 search_start = max(search_start, first_logical_byte(root, 0));
Chris Mason239b14b2008-03-24 15:02:07 -04003493 search_start = max(search_start, hint_byte);
Chris Mason0b86a832008-03-24 15:01:56 -04003494
Chris Masonfa9c0d792009-04-03 09:47:43 -04003495 if (!last_ptr) {
3496 empty_cluster = 0;
3497 loop = 1;
3498 }
3499
Josef Bacik2552d172009-04-03 10:14:19 -04003500 if (search_start == hint_byte) {
Josef Bacik2552d172009-04-03 10:14:19 -04003501 block_group = btrfs_lookup_block_group(root->fs_info,
3502 search_start);
3503 if (block_group && block_group_bits(block_group, data)) {
Josef Bacik2552d172009-04-03 10:14:19 -04003504 down_read(&space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04003505 if (list_empty(&block_group->list) ||
3506 block_group->ro) {
3507 /*
3508 * someone is removing this block group,
3509 * we can't jump into the have_block_group
3510 * target because our list pointers are not
3511 * valid
3512 */
3513 btrfs_put_block_group(block_group);
3514 up_read(&space_info->groups_sem);
3515 } else
3516 goto have_block_group;
Josef Bacik2552d172009-04-03 10:14:19 -04003517 } else if (block_group) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04003518 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04003519 }
Chris Mason42e70e72008-11-07 18:17:11 -05003520 }
Chris Mason43662112008-11-07 09:06:11 -05003521
Josef Bacik2552d172009-04-03 10:14:19 -04003522search:
Josef Bacik80eb2342008-10-29 14:49:05 -04003523 down_read(&space_info->groups_sem);
Josef Bacik2552d172009-04-03 10:14:19 -04003524 list_for_each_entry(block_group, &space_info->block_groups, list) {
Josef Bacik6226cb0a2009-04-03 10:14:18 -04003525 u64 offset;
Chris Mason8a1413a2008-11-10 16:13:54 -05003526
Josef Bacik2552d172009-04-03 10:14:19 -04003527 atomic_inc(&block_group->count);
3528 search_start = block_group->key.objectid;
Chris Mason42e70e72008-11-07 18:17:11 -05003529
Josef Bacik2552d172009-04-03 10:14:19 -04003530have_block_group:
Josef Bacikea6a4782008-11-20 12:16:16 -05003531 if (unlikely(!block_group->cached)) {
3532 mutex_lock(&block_group->cache_mutex);
3533 ret = cache_block_group(root, block_group);
3534 mutex_unlock(&block_group->cache_mutex);
Josef Bacik2552d172009-04-03 10:14:19 -04003535 if (ret) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04003536 btrfs_put_block_group(block_group);
Josef Bacikea6a4782008-11-20 12:16:16 -05003537 break;
Josef Bacik2552d172009-04-03 10:14:19 -04003538 }
Josef Bacikea6a4782008-11-20 12:16:16 -05003539 }
3540
Josef Bacikea6a4782008-11-20 12:16:16 -05003541 if (unlikely(block_group->ro))
Josef Bacik2552d172009-04-03 10:14:19 -04003542 goto loop;
Josef Bacik0f9dd462008-09-23 13:14:11 -04003543
Chris Masonfa9c0d792009-04-03 09:47:43 -04003544 if (last_ptr) {
3545 /*
3546 * the refill lock keeps out other
3547 * people trying to start a new cluster
3548 */
3549 spin_lock(&last_ptr->refill_lock);
Chris Mason44fb5512009-06-04 15:34:51 -04003550 if (last_ptr->block_group &&
3551 (last_ptr->block_group->ro ||
3552 !block_group_bits(last_ptr->block_group, data))) {
3553 offset = 0;
3554 goto refill_cluster;
3555 }
3556
Chris Masonfa9c0d792009-04-03 09:47:43 -04003557 offset = btrfs_alloc_from_cluster(block_group, last_ptr,
3558 num_bytes, search_start);
3559 if (offset) {
3560 /* we have a block, we're done */
3561 spin_unlock(&last_ptr->refill_lock);
3562 goto checks;
3563 }
3564
3565 spin_lock(&last_ptr->lock);
3566 /*
3567 * whoops, this cluster doesn't actually point to
3568 * this block group. Get a ref on the block
3569 * group is does point to and try again
3570 */
3571 if (!last_ptr_loop && last_ptr->block_group &&
3572 last_ptr->block_group != block_group) {
3573
3574 btrfs_put_block_group(block_group);
3575 block_group = last_ptr->block_group;
3576 atomic_inc(&block_group->count);
3577 spin_unlock(&last_ptr->lock);
3578 spin_unlock(&last_ptr->refill_lock);
3579
3580 last_ptr_loop = 1;
3581 search_start = block_group->key.objectid;
Chris Mason44fb5512009-06-04 15:34:51 -04003582 /*
3583 * we know this block group is properly
3584 * in the list because
3585 * btrfs_remove_block_group, drops the
3586 * cluster before it removes the block
3587 * group from the list
3588 */
Chris Masonfa9c0d792009-04-03 09:47:43 -04003589 goto have_block_group;
3590 }
3591 spin_unlock(&last_ptr->lock);
Chris Mason44fb5512009-06-04 15:34:51 -04003592refill_cluster:
Chris Masonfa9c0d792009-04-03 09:47:43 -04003593 /*
3594 * this cluster didn't work out, free it and
3595 * start over
3596 */
3597 btrfs_return_cluster_to_free_space(NULL, last_ptr);
3598
3599 last_ptr_loop = 0;
3600
3601 /* allocate a cluster in this block group */
Chris Mason451d7582009-06-09 20:28:34 -04003602 ret = btrfs_find_space_cluster(trans, root,
Chris Masonfa9c0d792009-04-03 09:47:43 -04003603 block_group, last_ptr,
3604 offset, num_bytes,
3605 empty_cluster + empty_size);
3606 if (ret == 0) {
3607 /*
3608 * now pull our allocation out of this
3609 * cluster
3610 */
3611 offset = btrfs_alloc_from_cluster(block_group,
3612 last_ptr, num_bytes,
3613 search_start);
3614 if (offset) {
3615 /* we found one, proceed */
3616 spin_unlock(&last_ptr->refill_lock);
3617 goto checks;
3618 }
3619 }
3620 /*
3621 * at this point we either didn't find a cluster
3622 * or we weren't able to allocate a block from our
3623 * cluster. Free the cluster we've been trying
3624 * to use, and go to the next block group
3625 */
3626 if (loop < 2) {
3627 btrfs_return_cluster_to_free_space(NULL,
3628 last_ptr);
3629 spin_unlock(&last_ptr->refill_lock);
3630 goto loop;
3631 }
3632 spin_unlock(&last_ptr->refill_lock);
3633 }
3634
Josef Bacik6226cb0a2009-04-03 10:14:18 -04003635 offset = btrfs_find_space_for_alloc(block_group, search_start,
3636 num_bytes, empty_size);
3637 if (!offset)
Josef Bacik2552d172009-04-03 10:14:19 -04003638 goto loop;
Chris Masonfa9c0d792009-04-03 09:47:43 -04003639checks:
Josef Bacik6226cb0a2009-04-03 10:14:18 -04003640 search_start = stripe_align(root, offset);
Chris Mason0b86a832008-03-24 15:01:56 -04003641
Josef Bacik2552d172009-04-03 10:14:19 -04003642 /* move on to the next group */
Josef Bacik6226cb0a2009-04-03 10:14:18 -04003643 if (search_start + num_bytes >= search_end) {
3644 btrfs_add_free_space(block_group, offset, num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04003645 goto loop;
Josef Bacik6226cb0a2009-04-03 10:14:18 -04003646 }
Chris Masone37c9e62007-05-09 20:13:14 -04003647
Josef Bacik2552d172009-04-03 10:14:19 -04003648 /* move on to the next group */
3649 if (search_start + num_bytes >
Josef Bacik6226cb0a2009-04-03 10:14:18 -04003650 block_group->key.objectid + block_group->key.offset) {
3651 btrfs_add_free_space(block_group, offset, num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04003652 goto loop;
Josef Bacik6226cb0a2009-04-03 10:14:18 -04003653 }
Josef Bacik80eb2342008-10-29 14:49:05 -04003654
Josef Bacik2552d172009-04-03 10:14:19 -04003655 if (exclude_nr > 0 &&
3656 (search_start + num_bytes > exclude_start &&
3657 search_start < exclude_start + exclude_nr)) {
3658 search_start = exclude_start + exclude_nr;
3659
Josef Bacik6226cb0a2009-04-03 10:14:18 -04003660 btrfs_add_free_space(block_group, offset, num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04003661 /*
3662 * if search_start is still in this block group
3663 * then we just re-search this block group
3664 */
3665 if (search_start >= block_group->key.objectid &&
3666 search_start < (block_group->key.objectid +
Josef Bacik6226cb0a2009-04-03 10:14:18 -04003667 block_group->key.offset))
Josef Bacik2552d172009-04-03 10:14:19 -04003668 goto have_block_group;
Josef Bacik2552d172009-04-03 10:14:19 -04003669 goto loop;
Josef Bacik0f9dd462008-09-23 13:14:11 -04003670 }
Josef Bacik2552d172009-04-03 10:14:19 -04003671
3672 ins->objectid = search_start;
3673 ins->offset = num_bytes;
3674
Josef Bacik6226cb0a2009-04-03 10:14:18 -04003675 if (offset < search_start)
3676 btrfs_add_free_space(block_group, offset,
3677 search_start - offset);
3678 BUG_ON(offset > search_start);
3679
Josef Bacik2552d172009-04-03 10:14:19 -04003680 /* we are all good, lets return */
Josef Bacik2552d172009-04-03 10:14:19 -04003681 break;
3682loop:
Chris Masonfa9c0d792009-04-03 09:47:43 -04003683 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04003684 }
3685 up_read(&space_info->groups_sem);
Chris Masonf5a31e12008-11-10 11:47:09 -05003686
Chris Masonfa9c0d792009-04-03 09:47:43 -04003687 /* loop == 0, try to find a clustered alloc in every block group
3688 * loop == 1, try again after forcing a chunk allocation
3689 * loop == 2, set empty_size and empty_cluster to 0 and try again
3690 */
3691 if (!ins->objectid && loop < 3 &&
3692 (empty_size || empty_cluster || allowed_chunk_alloc)) {
3693 if (loop >= 2) {
3694 empty_size = 0;
3695 empty_cluster = 0;
3696 }
Chris Mason42e70e72008-11-07 18:17:11 -05003697
Josef Bacik2552d172009-04-03 10:14:19 -04003698 if (allowed_chunk_alloc) {
3699 ret = do_chunk_alloc(trans, root, num_bytes +
3700 2 * 1024 * 1024, data, 1);
Josef Bacik2552d172009-04-03 10:14:19 -04003701 allowed_chunk_alloc = 0;
3702 } else {
3703 space_info->force_alloc = 1;
Josef Bacik0f9dd462008-09-23 13:14:11 -04003704 }
Josef Bacik80eb2342008-10-29 14:49:05 -04003705
Chris Masonfa9c0d792009-04-03 09:47:43 -04003706 if (loop < 3) {
3707 loop++;
Josef Bacik2552d172009-04-03 10:14:19 -04003708 goto search;
Chris Masonfa9c0d792009-04-03 09:47:43 -04003709 }
Josef Bacik2552d172009-04-03 10:14:19 -04003710 ret = -ENOSPC;
3711 } else if (!ins->objectid) {
3712 ret = -ENOSPC;
Chris Masone19caa52007-10-15 16:17:44 -04003713 }
Chris Mason0b86a832008-03-24 15:01:56 -04003714
Josef Bacik80eb2342008-10-29 14:49:05 -04003715 /* we found what we needed */
3716 if (ins->objectid) {
3717 if (!(data & BTRFS_BLOCK_GROUP_DATA))
Yan Zhengd2fb3432008-12-11 16:30:39 -05003718 trans->block_group = block_group->key.objectid;
Josef Bacik80eb2342008-10-29 14:49:05 -04003719
Chris Masonfa9c0d792009-04-03 09:47:43 -04003720 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04003721 ret = 0;
3722 }
Chris Mason0b86a832008-03-24 15:01:56 -04003723
Chris Mason0f70abe2007-02-28 16:46:22 -05003724 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05003725}
Chris Masonec44a352008-04-28 15:29:52 -04003726
Josef Bacik0f9dd462008-09-23 13:14:11 -04003727static void dump_space_info(struct btrfs_space_info *info, u64 bytes)
3728{
3729 struct btrfs_block_group_cache *cache;
Josef Bacik0f9dd462008-09-23 13:14:11 -04003730
Chris Masond3977122009-01-05 21:25:51 -05003731 printk(KERN_INFO "space_info has %llu free, is %sfull\n",
3732 (unsigned long long)(info->total_bytes - info->bytes_used -
3733 info->bytes_pinned - info->bytes_reserved),
3734 (info->full) ? "" : "not ");
Josef Bacik6a632092009-02-20 11:00:09 -05003735 printk(KERN_INFO "space_info total=%llu, pinned=%llu, delalloc=%llu,"
Joel Becker21380932009-04-21 12:38:29 -07003736 " may_use=%llu, used=%llu\n",
3737 (unsigned long long)info->total_bytes,
3738 (unsigned long long)info->bytes_pinned,
3739 (unsigned long long)info->bytes_delalloc,
3740 (unsigned long long)info->bytes_may_use,
3741 (unsigned long long)info->bytes_used);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003742
Josef Bacik80eb2342008-10-29 14:49:05 -04003743 down_read(&info->groups_sem);
Qinghuang Fengc6e30872009-01-21 10:59:08 -05003744 list_for_each_entry(cache, &info->block_groups, list) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04003745 spin_lock(&cache->lock);
Chris Masond3977122009-01-05 21:25:51 -05003746 printk(KERN_INFO "block group %llu has %llu bytes, %llu used "
3747 "%llu pinned %llu reserved\n",
3748 (unsigned long long)cache->key.objectid,
3749 (unsigned long long)cache->key.offset,
3750 (unsigned long long)btrfs_block_group_used(&cache->item),
3751 (unsigned long long)cache->pinned,
3752 (unsigned long long)cache->reserved);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003753 btrfs_dump_free_space(cache, bytes);
3754 spin_unlock(&cache->lock);
3755 }
Josef Bacik80eb2342008-10-29 14:49:05 -04003756 up_read(&info->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003757}
Zheng Yane8569812008-09-26 10:05:48 -04003758
Chris Masone6dcd2d2008-07-17 12:53:50 -04003759static int __btrfs_reserve_extent(struct btrfs_trans_handle *trans,
3760 struct btrfs_root *root,
3761 u64 num_bytes, u64 min_alloc_size,
3762 u64 empty_size, u64 hint_byte,
3763 u64 search_end, struct btrfs_key *ins,
3764 u64 data)
Chris Masonfec577f2007-02-26 10:40:21 -05003765{
3766 int ret;
Chris Masonfbdc7622007-05-30 10:22:12 -04003767 u64 search_start = 0;
Chris Mason1261ec42007-03-20 20:35:03 -04003768 struct btrfs_fs_info *info = root->fs_info;
Chris Mason925baed2008-06-25 16:01:30 -04003769
Josef Bacik6a632092009-02-20 11:00:09 -05003770 data = btrfs_get_alloc_profile(root, data);
Chris Mason98d20f62008-04-14 09:46:10 -04003771again:
Chris Mason0ef3e662008-05-24 14:04:53 -04003772 /*
3773 * the only place that sets empty_size is btrfs_realloc_node, which
3774 * is not called recursively on allocations
3775 */
3776 if (empty_size || root->ref_cows) {
Chris Mason593060d2008-03-25 16:50:33 -04003777 if (!(data & BTRFS_BLOCK_GROUP_METADATA)) {
Chris Mason6324fbf2008-03-24 15:01:59 -04003778 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0ef3e662008-05-24 14:04:53 -04003779 2 * 1024 * 1024,
3780 BTRFS_BLOCK_GROUP_METADATA |
3781 (info->metadata_alloc_profile &
3782 info->avail_metadata_alloc_bits), 0);
Chris Mason6324fbf2008-03-24 15:01:59 -04003783 }
3784 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0ef3e662008-05-24 14:04:53 -04003785 num_bytes + 2 * 1024 * 1024, data, 0);
Chris Mason6324fbf2008-03-24 15:01:59 -04003786 }
Chris Mason0b86a832008-03-24 15:01:56 -04003787
Chris Masondb945352007-10-15 16:15:53 -04003788 WARN_ON(num_bytes < root->sectorsize);
3789 ret = find_free_extent(trans, root, num_bytes, empty_size,
3790 search_start, search_end, hint_byte, ins,
Chris Mason26b80032007-08-08 20:17:12 -04003791 trans->alloc_exclude_start,
3792 trans->alloc_exclude_nr, data);
Chris Mason3b951512008-04-17 11:29:12 -04003793
Chris Mason98d20f62008-04-14 09:46:10 -04003794 if (ret == -ENOSPC && num_bytes > min_alloc_size) {
3795 num_bytes = num_bytes >> 1;
Josef Bacik0f9dd462008-09-23 13:14:11 -04003796 num_bytes = num_bytes & ~(root->sectorsize - 1);
Chris Mason98d20f62008-04-14 09:46:10 -04003797 num_bytes = max(num_bytes, min_alloc_size);
Chris Mason0ef3e662008-05-24 14:04:53 -04003798 do_chunk_alloc(trans, root->fs_info->extent_root,
3799 num_bytes, data, 1);
Chris Mason98d20f62008-04-14 09:46:10 -04003800 goto again;
3801 }
Chris Masonec44a352008-04-28 15:29:52 -04003802 if (ret) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04003803 struct btrfs_space_info *sinfo;
3804
3805 sinfo = __find_space_info(root->fs_info, data);
Chris Masond3977122009-01-05 21:25:51 -05003806 printk(KERN_ERR "btrfs allocation failed flags %llu, "
3807 "wanted %llu\n", (unsigned long long)data,
3808 (unsigned long long)num_bytes);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003809 dump_space_info(sinfo, num_bytes);
Chris Mason925baed2008-06-25 16:01:30 -04003810 BUG();
Chris Mason925baed2008-06-25 16:01:30 -04003811 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04003812
3813 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003814}
3815
Chris Mason65b51a02008-08-01 15:11:20 -04003816int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len)
3817{
Josef Bacik0f9dd462008-09-23 13:14:11 -04003818 struct btrfs_block_group_cache *cache;
Liu Hui1f3c79a2009-01-05 15:57:51 -05003819 int ret = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04003820
Josef Bacik0f9dd462008-09-23 13:14:11 -04003821 cache = btrfs_lookup_block_group(root->fs_info, start);
3822 if (!cache) {
Chris Masond3977122009-01-05 21:25:51 -05003823 printk(KERN_ERR "Unable to find block group for %llu\n",
3824 (unsigned long long)start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003825 return -ENOSPC;
3826 }
Liu Hui1f3c79a2009-01-05 15:57:51 -05003827
3828 ret = btrfs_discard_extent(root, start, len);
3829
Josef Bacik0f9dd462008-09-23 13:14:11 -04003830 btrfs_add_free_space(cache, start, len);
Chris Masonfa9c0d792009-04-03 09:47:43 -04003831 btrfs_put_block_group(cache);
Zheng Yan1a40e232008-09-26 10:09:34 -04003832 update_reserved_extents(root, start, len, 0);
Liu Hui1f3c79a2009-01-05 15:57:51 -05003833
3834 return ret;
Chris Mason65b51a02008-08-01 15:11:20 -04003835}
3836
Chris Masone6dcd2d2008-07-17 12:53:50 -04003837int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
3838 struct btrfs_root *root,
3839 u64 num_bytes, u64 min_alloc_size,
3840 u64 empty_size, u64 hint_byte,
3841 u64 search_end, struct btrfs_key *ins,
3842 u64 data)
3843{
3844 int ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003845 ret = __btrfs_reserve_extent(trans, root, num_bytes, min_alloc_size,
3846 empty_size, hint_byte, search_end, ins,
3847 data);
Zheng Yane8569812008-09-26 10:05:48 -04003848 update_reserved_extents(root, ins->objectid, ins->offset, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003849 return ret;
3850}
3851
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003852static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
3853 struct btrfs_root *root,
3854 u64 parent, u64 root_objectid,
3855 u64 flags, u64 owner, u64 offset,
3856 struct btrfs_key *ins, int ref_mod)
Chris Masone6dcd2d2008-07-17 12:53:50 -04003857{
3858 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003859 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003860 struct btrfs_extent_item *extent_item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003861 struct btrfs_extent_inline_ref *iref;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003862 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003863 struct extent_buffer *leaf;
3864 int type;
3865 u32 size;
Chris Masonf2654de2007-06-26 12:20:46 -04003866
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003867 if (parent > 0)
3868 type = BTRFS_SHARED_DATA_REF_KEY;
3869 else
3870 type = BTRFS_EXTENT_DATA_REF_KEY;
Zheng Yan31840ae2008-09-23 13:14:14 -04003871
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003872 size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type);
Chris Mason7bb86312007-12-11 09:25:06 -05003873
3874 path = btrfs_alloc_path();
3875 BUG_ON(!path);
Chris Mason47e4bb92008-02-01 14:51:59 -05003876
Chris Masonb9473432009-03-13 11:00:37 -04003877 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003878 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
3879 ins, size);
Chris Masonccd467d2007-06-28 15:57:36 -04003880 BUG_ON(ret);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003881
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003882 leaf = path->nodes[0];
3883 extent_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason47e4bb92008-02-01 14:51:59 -05003884 struct btrfs_extent_item);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003885 btrfs_set_extent_refs(leaf, extent_item, ref_mod);
3886 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
3887 btrfs_set_extent_flags(leaf, extent_item,
3888 flags | BTRFS_EXTENT_FLAG_DATA);
Chris Mason47e4bb92008-02-01 14:51:59 -05003889
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003890 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
3891 btrfs_set_extent_inline_ref_type(leaf, iref, type);
3892 if (parent > 0) {
3893 struct btrfs_shared_data_ref *ref;
3894 ref = (struct btrfs_shared_data_ref *)(iref + 1);
3895 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
3896 btrfs_set_shared_data_ref_count(leaf, ref, ref_mod);
3897 } else {
3898 struct btrfs_extent_data_ref *ref;
3899 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
3900 btrfs_set_extent_data_ref_root(leaf, ref, root_objectid);
3901 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
3902 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
3903 btrfs_set_extent_data_ref_count(leaf, ref, ref_mod);
3904 }
Chris Mason47e4bb92008-02-01 14:51:59 -05003905
3906 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason7bb86312007-12-11 09:25:06 -05003907 btrfs_free_path(path);
Chris Masonf510cfe2007-10-15 16:14:48 -04003908
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003909 ret = update_block_group(trans, root, ins->objectid, ins->offset,
3910 1, 0);
Chris Masonf5947062008-02-04 10:10:13 -05003911 if (ret) {
Chris Masond3977122009-01-05 21:25:51 -05003912 printk(KERN_ERR "btrfs update block group failed for %llu "
3913 "%llu\n", (unsigned long long)ins->objectid,
3914 (unsigned long long)ins->offset);
Chris Masonf5947062008-02-04 10:10:13 -05003915 BUG();
3916 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003917 return ret;
3918}
3919
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003920static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
3921 struct btrfs_root *root,
3922 u64 parent, u64 root_objectid,
3923 u64 flags, struct btrfs_disk_key *key,
3924 int level, struct btrfs_key *ins)
3925{
3926 int ret;
3927 struct btrfs_fs_info *fs_info = root->fs_info;
3928 struct btrfs_extent_item *extent_item;
3929 struct btrfs_tree_block_info *block_info;
3930 struct btrfs_extent_inline_ref *iref;
3931 struct btrfs_path *path;
3932 struct extent_buffer *leaf;
3933 u32 size = sizeof(*extent_item) + sizeof(*block_info) + sizeof(*iref);
3934
3935 path = btrfs_alloc_path();
3936 BUG_ON(!path);
3937
3938 path->leave_spinning = 1;
3939 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
3940 ins, size);
3941 BUG_ON(ret);
3942
3943 leaf = path->nodes[0];
3944 extent_item = btrfs_item_ptr(leaf, path->slots[0],
3945 struct btrfs_extent_item);
3946 btrfs_set_extent_refs(leaf, extent_item, 1);
3947 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
3948 btrfs_set_extent_flags(leaf, extent_item,
3949 flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
3950 block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
3951
3952 btrfs_set_tree_block_key(leaf, block_info, key);
3953 btrfs_set_tree_block_level(leaf, block_info, level);
3954
3955 iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
3956 if (parent > 0) {
3957 BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
3958 btrfs_set_extent_inline_ref_type(leaf, iref,
3959 BTRFS_SHARED_BLOCK_REF_KEY);
3960 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
3961 } else {
3962 btrfs_set_extent_inline_ref_type(leaf, iref,
3963 BTRFS_TREE_BLOCK_REF_KEY);
3964 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
3965 }
3966
3967 btrfs_mark_buffer_dirty(leaf);
3968 btrfs_free_path(path);
3969
3970 ret = update_block_group(trans, root, ins->objectid, ins->offset,
3971 1, 0);
3972 if (ret) {
3973 printk(KERN_ERR "btrfs update block group failed for %llu "
3974 "%llu\n", (unsigned long long)ins->objectid,
3975 (unsigned long long)ins->offset);
3976 BUG();
3977 }
3978 return ret;
3979}
3980
3981int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
3982 struct btrfs_root *root,
3983 u64 root_objectid, u64 owner,
3984 u64 offset, struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04003985{
3986 int ret;
Chris Mason1c2308f2008-09-23 13:14:13 -04003987
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003988 BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID);
Chris Mason56bec292009-03-13 10:10:06 -04003989
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003990 ret = btrfs_add_delayed_data_ref(trans, ins->objectid, ins->offset,
3991 0, root_objectid, owner, offset,
3992 BTRFS_ADD_DELAYED_EXTENT, NULL);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003993 return ret;
3994}
Chris Masone02119d2008-09-05 16:13:11 -04003995
3996/*
3997 * this is used by the tree logging recovery code. It records that
3998 * an extent has been allocated and makes sure to clear the free
3999 * space cache bits as well
4000 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004001int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
4002 struct btrfs_root *root,
4003 u64 root_objectid, u64 owner, u64 offset,
4004 struct btrfs_key *ins)
Chris Masone02119d2008-09-05 16:13:11 -04004005{
4006 int ret;
4007 struct btrfs_block_group_cache *block_group;
4008
Chris Masone02119d2008-09-05 16:13:11 -04004009 block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
Josef Bacikea6a4782008-11-20 12:16:16 -05004010 mutex_lock(&block_group->cache_mutex);
Chris Masone02119d2008-09-05 16:13:11 -04004011 cache_block_group(root, block_group);
Josef Bacikea6a4782008-11-20 12:16:16 -05004012 mutex_unlock(&block_group->cache_mutex);
Chris Masone02119d2008-09-05 16:13:11 -04004013
Josef Bacikea6a4782008-11-20 12:16:16 -05004014 ret = btrfs_remove_free_space(block_group, ins->objectid,
4015 ins->offset);
Josef Bacik0f9dd462008-09-23 13:14:11 -04004016 BUG_ON(ret);
Chris Masonfa9c0d792009-04-03 09:47:43 -04004017 btrfs_put_block_group(block_group);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004018 ret = alloc_reserved_file_extent(trans, root, 0, root_objectid,
4019 0, owner, offset, ins, 1);
Chris Masone02119d2008-09-05 16:13:11 -04004020 return ret;
4021}
4022
Chris Masone6dcd2d2008-07-17 12:53:50 -04004023/*
4024 * finds a free extent and does all the dirty work required for allocation
4025 * returns the key for the extent through ins, and a tree buffer for
4026 * the first block of the extent through buf.
4027 *
4028 * returns 0 if everything worked, non-zero otherwise.
4029 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004030static int alloc_tree_block(struct btrfs_trans_handle *trans,
4031 struct btrfs_root *root,
4032 u64 num_bytes, u64 parent, u64 root_objectid,
4033 struct btrfs_disk_key *key, int level,
4034 u64 empty_size, u64 hint_byte, u64 search_end,
4035 struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04004036{
4037 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004038 u64 flags = 0;
4039
4040 ret = __btrfs_reserve_extent(trans, root, num_bytes, num_bytes,
4041 empty_size, hint_byte, search_end,
4042 ins, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004043 BUG_ON(ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004044
4045 if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
4046 if (parent == 0)
4047 parent = ins->objectid;
4048 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
4049 } else
4050 BUG_ON(parent > 0);
4051
4052 update_reserved_extents(root, ins->objectid, ins->offset, 1);
Chris Masond00aff02008-09-11 15:54:42 -04004053 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004054 struct btrfs_delayed_extent_op *extent_op;
4055 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
4056 BUG_ON(!extent_op);
4057 if (key)
4058 memcpy(&extent_op->key, key, sizeof(extent_op->key));
4059 else
4060 memset(&extent_op->key, 0, sizeof(extent_op->key));
4061 extent_op->flags_to_set = flags;
4062 extent_op->update_key = 1;
4063 extent_op->update_flags = 1;
4064 extent_op->is_data = 0;
4065
4066 ret = btrfs_add_delayed_tree_ref(trans, ins->objectid,
4067 ins->offset, parent, root_objectid,
4068 level, BTRFS_ADD_DELAYED_EXTENT,
4069 extent_op);
Chris Masond00aff02008-09-11 15:54:42 -04004070 BUG_ON(ret);
Chris Masond00aff02008-09-11 15:54:42 -04004071 }
Chris Mason925baed2008-06-25 16:01:30 -04004072 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05004073}
Chris Mason65b51a02008-08-01 15:11:20 -04004074
4075struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
4076 struct btrfs_root *root,
Chris Mason4008c042009-02-12 14:09:45 -05004077 u64 bytenr, u32 blocksize,
4078 int level)
Chris Mason65b51a02008-08-01 15:11:20 -04004079{
4080 struct extent_buffer *buf;
4081
4082 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
4083 if (!buf)
4084 return ERR_PTR(-ENOMEM);
4085 btrfs_set_header_generation(buf, trans->transid);
Chris Mason4008c042009-02-12 14:09:45 -05004086 btrfs_set_buffer_lockdep_class(buf, level);
Chris Mason65b51a02008-08-01 15:11:20 -04004087 btrfs_tree_lock(buf);
4088 clean_tree_block(trans, root, buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004089
4090 btrfs_set_lock_blocking(buf);
Chris Mason65b51a02008-08-01 15:11:20 -04004091 btrfs_set_buffer_uptodate(buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004092
Chris Masond0c803c2008-09-11 16:17:57 -04004093 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
4094 set_extent_dirty(&root->dirty_log_pages, buf->start,
Chris Mason65b51a02008-08-01 15:11:20 -04004095 buf->start + buf->len - 1, GFP_NOFS);
Chris Masond0c803c2008-09-11 16:17:57 -04004096 } else {
4097 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
4098 buf->start + buf->len - 1, GFP_NOFS);
4099 }
Chris Mason65b51a02008-08-01 15:11:20 -04004100 trans->blocks_used++;
Chris Masonb4ce94d2009-02-04 09:25:08 -05004101 /* this returns a buffer locked for blocking */
Chris Mason65b51a02008-08-01 15:11:20 -04004102 return buf;
4103}
4104
Chris Masonfec577f2007-02-26 10:40:21 -05004105/*
4106 * helper function to allocate a block for a given tree
4107 * returns the tree buffer or NULL.
4108 */
Chris Mason5f39d392007-10-15 16:14:19 -04004109struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004110 struct btrfs_root *root, u32 blocksize,
4111 u64 parent, u64 root_objectid,
4112 struct btrfs_disk_key *key, int level,
4113 u64 hint, u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05004114{
Chris Masone2fa7222007-03-12 16:22:34 -04004115 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -05004116 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04004117 struct extent_buffer *buf;
Chris Masonfec577f2007-02-26 10:40:21 -05004118
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004119 ret = alloc_tree_block(trans, root, blocksize, parent, root_objectid,
4120 key, level, empty_size, hint, (u64)-1, &ins);
Chris Masonfec577f2007-02-26 10:40:21 -05004121 if (ret) {
Chris Mason54aa1f42007-06-22 14:16:25 -04004122 BUG_ON(ret > 0);
4123 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05004124 }
Chris Mason55c69072008-01-09 15:55:33 -05004125
Chris Mason4008c042009-02-12 14:09:45 -05004126 buf = btrfs_init_new_buffer(trans, root, ins.objectid,
4127 blocksize, level);
Chris Masonfec577f2007-02-26 10:40:21 -05004128 return buf;
4129}
Chris Masona28ec192007-03-06 20:08:01 -05004130
Chris Masone02119d2008-09-05 16:13:11 -04004131int btrfs_drop_leaf_ref(struct btrfs_trans_handle *trans,
4132 struct btrfs_root *root, struct extent_buffer *leaf)
Chris Mason6407bf62007-03-27 06:33:00 -04004133{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004134 u64 disk_bytenr;
4135 u64 num_bytes;
Chris Mason5f39d392007-10-15 16:14:19 -04004136 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -04004137 struct btrfs_file_extent_item *fi;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004138 u32 nritems;
Chris Mason6407bf62007-03-27 06:33:00 -04004139 int i;
Chris Mason6407bf62007-03-27 06:33:00 -04004140 int ret;
4141
Chris Mason5f39d392007-10-15 16:14:19 -04004142 BUG_ON(!btrfs_is_leaf(leaf));
4143 nritems = btrfs_header_nritems(leaf);
Chris Mason7bb86312007-12-11 09:25:06 -05004144
Chris Mason6407bf62007-03-27 06:33:00 -04004145 for (i = 0; i < nritems; i++) {
Chris Masone34a5b42008-07-22 12:08:37 -04004146 cond_resched();
Chris Mason5f39d392007-10-15 16:14:19 -04004147 btrfs_item_key_to_cpu(leaf, &key, i);
Chris Masonbd56b302009-02-04 09:27:02 -05004148
4149 /* only extents have references, skip everything else */
Chris Mason5f39d392007-10-15 16:14:19 -04004150 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason6407bf62007-03-27 06:33:00 -04004151 continue;
Chris Masonbd56b302009-02-04 09:27:02 -05004152
Chris Mason6407bf62007-03-27 06:33:00 -04004153 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
Chris Masonbd56b302009-02-04 09:27:02 -05004154
4155 /* inline extents live in the btree, they don't have refs */
Chris Mason5f39d392007-10-15 16:14:19 -04004156 if (btrfs_file_extent_type(leaf, fi) ==
4157 BTRFS_FILE_EXTENT_INLINE)
Chris Mason236454df2007-04-19 13:37:44 -04004158 continue;
Chris Masonbd56b302009-02-04 09:27:02 -05004159
Chris Masondb945352007-10-15 16:15:53 -04004160 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Chris Masonbd56b302009-02-04 09:27:02 -05004161
4162 /* holes don't have refs */
Chris Masondb945352007-10-15 16:15:53 -04004163 if (disk_bytenr == 0)
Chris Mason3a686372007-05-24 13:35:57 -04004164 continue;
Chris Mason4a096752008-07-21 10:29:44 -04004165
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004166 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
4167 ret = btrfs_free_extent(trans, root, disk_bytenr, num_bytes,
4168 leaf->start, 0, key.objectid, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04004169 BUG_ON(ret);
Chris Mason6407bf62007-03-27 06:33:00 -04004170 }
4171 return 0;
4172}
4173
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004174#if 0
4175
Chris Masond3977122009-01-05 21:25:51 -05004176static noinline int cache_drop_leaf_ref(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04004177 struct btrfs_root *root,
4178 struct btrfs_leaf_ref *ref)
Yan Zheng31153d82008-07-28 15:32:19 -04004179{
4180 int i;
4181 int ret;
Chris Masonbd56b302009-02-04 09:27:02 -05004182 struct btrfs_extent_info *info;
4183 struct refsort *sorted;
Yan Zheng31153d82008-07-28 15:32:19 -04004184
Chris Masonbd56b302009-02-04 09:27:02 -05004185 if (ref->nritems == 0)
4186 return 0;
4187
4188 sorted = kmalloc(sizeof(*sorted) * ref->nritems, GFP_NOFS);
Yan Zheng31153d82008-07-28 15:32:19 -04004189 for (i = 0; i < ref->nritems; i++) {
Chris Masonbd56b302009-02-04 09:27:02 -05004190 sorted[i].bytenr = ref->extents[i].bytenr;
4191 sorted[i].slot = i;
4192 }
4193 sort(sorted, ref->nritems, sizeof(struct refsort), refsort_cmp, NULL);
4194
4195 /*
4196 * the items in the ref were sorted when the ref was inserted
4197 * into the ref cache, so this is already in order
4198 */
4199 for (i = 0; i < ref->nritems; i++) {
4200 info = ref->extents + sorted[i].slot;
Chris Mason56bec292009-03-13 10:10:06 -04004201 ret = btrfs_free_extent(trans, root, info->bytenr,
Zheng Yan31840ae2008-09-23 13:14:14 -04004202 info->num_bytes, ref->bytenr,
4203 ref->owner, ref->generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004204 info->objectid, 0);
Chris Mason2dd3e672008-08-04 08:20:15 -04004205
4206 atomic_inc(&root->fs_info->throttle_gen);
4207 wake_up(&root->fs_info->transaction_throttle);
4208 cond_resched();
4209
Yan Zheng31153d82008-07-28 15:32:19 -04004210 BUG_ON(ret);
4211 info++;
4212 }
Yan Zheng31153d82008-07-28 15:32:19 -04004213
Chris Mason806638b2009-02-05 09:08:14 -05004214 kfree(sorted);
Yan Zheng31153d82008-07-28 15:32:19 -04004215 return 0;
4216}
4217
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004218
Chris Mason56bec292009-03-13 10:10:06 -04004219static int drop_snap_lookup_refcount(struct btrfs_trans_handle *trans,
4220 struct btrfs_root *root, u64 start,
Chris Masond3977122009-01-05 21:25:51 -05004221 u64 len, u32 *refs)
Chris Mason333db942008-06-25 16:01:30 -04004222{
Chris Mason017e5362008-07-28 15:32:51 -04004223 int ret;
Chris Masonf87f0572008-08-01 11:27:23 -04004224
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004225 ret = btrfs_lookup_extent_refs(trans, root, start, len, refs);
Chris Masonf87f0572008-08-01 11:27:23 -04004226 BUG_ON(ret);
4227
Chris Masond3977122009-01-05 21:25:51 -05004228#if 0 /* some debugging code in case we see problems here */
Chris Masonf87f0572008-08-01 11:27:23 -04004229 /* if the refs count is one, it won't get increased again. But
4230 * if the ref count is > 1, someone may be decreasing it at
4231 * the same time we are.
4232 */
4233 if (*refs != 1) {
4234 struct extent_buffer *eb = NULL;
4235 eb = btrfs_find_create_tree_block(root, start, len);
4236 if (eb)
4237 btrfs_tree_lock(eb);
4238
4239 mutex_lock(&root->fs_info->alloc_mutex);
4240 ret = lookup_extent_ref(NULL, root, start, len, refs);
4241 BUG_ON(ret);
4242 mutex_unlock(&root->fs_info->alloc_mutex);
4243
4244 if (eb) {
4245 btrfs_tree_unlock(eb);
4246 free_extent_buffer(eb);
4247 }
4248 if (*refs == 1) {
Chris Masond3977122009-01-05 21:25:51 -05004249 printk(KERN_ERR "btrfs block %llu went down to one "
4250 "during drop_snap\n", (unsigned long long)start);
Chris Masonf87f0572008-08-01 11:27:23 -04004251 }
4252
4253 }
4254#endif
4255
Chris Masone7a84562008-06-25 16:01:31 -04004256 cond_resched();
Chris Mason017e5362008-07-28 15:32:51 -04004257 return ret;
Chris Mason333db942008-06-25 16:01:30 -04004258}
4259
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004260
Chris Mason333db942008-06-25 16:01:30 -04004261/*
Chris Masonbd56b302009-02-04 09:27:02 -05004262 * this is used while deleting old snapshots, and it drops the refs
4263 * on a whole subtree starting from a level 1 node.
4264 *
4265 * The idea is to sort all the leaf pointers, and then drop the
4266 * ref on all the leaves in order. Most of the time the leaves
4267 * will have ref cache entries, so no leaf IOs will be required to
4268 * find the extents they have references on.
4269 *
4270 * For each leaf, any references it has are also dropped in order
4271 *
4272 * This ends up dropping the references in something close to optimal
4273 * order for reading and modifying the extent allocation tree.
4274 */
4275static noinline int drop_level_one_refs(struct btrfs_trans_handle *trans,
4276 struct btrfs_root *root,
4277 struct btrfs_path *path)
4278{
4279 u64 bytenr;
4280 u64 root_owner;
4281 u64 root_gen;
4282 struct extent_buffer *eb = path->nodes[1];
4283 struct extent_buffer *leaf;
4284 struct btrfs_leaf_ref *ref;
4285 struct refsort *sorted = NULL;
4286 int nritems = btrfs_header_nritems(eb);
4287 int ret;
4288 int i;
4289 int refi = 0;
4290 int slot = path->slots[1];
4291 u32 blocksize = btrfs_level_size(root, 0);
4292 u32 refs;
4293
4294 if (nritems == 0)
4295 goto out;
4296
4297 root_owner = btrfs_header_owner(eb);
4298 root_gen = btrfs_header_generation(eb);
4299 sorted = kmalloc(sizeof(*sorted) * nritems, GFP_NOFS);
4300
4301 /*
4302 * step one, sort all the leaf pointers so we don't scribble
4303 * randomly into the extent allocation tree
4304 */
4305 for (i = slot; i < nritems; i++) {
4306 sorted[refi].bytenr = btrfs_node_blockptr(eb, i);
4307 sorted[refi].slot = i;
4308 refi++;
4309 }
4310
4311 /*
4312 * nritems won't be zero, but if we're picking up drop_snapshot
4313 * after a crash, slot might be > 0, so double check things
4314 * just in case.
4315 */
4316 if (refi == 0)
4317 goto out;
4318
4319 sort(sorted, refi, sizeof(struct refsort), refsort_cmp, NULL);
4320
4321 /*
4322 * the first loop frees everything the leaves point to
4323 */
4324 for (i = 0; i < refi; i++) {
4325 u64 ptr_gen;
4326
4327 bytenr = sorted[i].bytenr;
4328
4329 /*
4330 * check the reference count on this leaf. If it is > 1
4331 * we just decrement it below and don't update any
4332 * of the refs the leaf points to.
4333 */
Chris Mason56bec292009-03-13 10:10:06 -04004334 ret = drop_snap_lookup_refcount(trans, root, bytenr,
4335 blocksize, &refs);
Chris Masonbd56b302009-02-04 09:27:02 -05004336 BUG_ON(ret);
4337 if (refs != 1)
4338 continue;
4339
4340 ptr_gen = btrfs_node_ptr_generation(eb, sorted[i].slot);
4341
4342 /*
4343 * the leaf only had one reference, which means the
4344 * only thing pointing to this leaf is the snapshot
4345 * we're deleting. It isn't possible for the reference
4346 * count to increase again later
4347 *
4348 * The reference cache is checked for the leaf,
4349 * and if found we'll be able to drop any refs held by
4350 * the leaf without needing to read it in.
4351 */
4352 ref = btrfs_lookup_leaf_ref(root, bytenr);
4353 if (ref && ref->generation != ptr_gen) {
4354 btrfs_free_leaf_ref(root, ref);
4355 ref = NULL;
4356 }
4357 if (ref) {
4358 ret = cache_drop_leaf_ref(trans, root, ref);
4359 BUG_ON(ret);
4360 btrfs_remove_leaf_ref(root, ref);
4361 btrfs_free_leaf_ref(root, ref);
4362 } else {
4363 /*
4364 * the leaf wasn't in the reference cache, so
4365 * we have to read it.
4366 */
4367 leaf = read_tree_block(root, bytenr, blocksize,
4368 ptr_gen);
4369 ret = btrfs_drop_leaf_ref(trans, root, leaf);
4370 BUG_ON(ret);
4371 free_extent_buffer(leaf);
4372 }
4373 atomic_inc(&root->fs_info->throttle_gen);
4374 wake_up(&root->fs_info->transaction_throttle);
4375 cond_resched();
4376 }
4377
4378 /*
4379 * run through the loop again to free the refs on the leaves.
4380 * This is faster than doing it in the loop above because
4381 * the leaves are likely to be clustered together. We end up
4382 * working in nice chunks on the extent allocation tree.
4383 */
4384 for (i = 0; i < refi; i++) {
4385 bytenr = sorted[i].bytenr;
Chris Mason56bec292009-03-13 10:10:06 -04004386 ret = btrfs_free_extent(trans, root, bytenr,
Chris Masonbd56b302009-02-04 09:27:02 -05004387 blocksize, eb->start,
4388 root_owner, root_gen, 0, 1);
4389 BUG_ON(ret);
4390
4391 atomic_inc(&root->fs_info->throttle_gen);
4392 wake_up(&root->fs_info->transaction_throttle);
4393 cond_resched();
4394 }
4395out:
4396 kfree(sorted);
4397
4398 /*
4399 * update the path to show we've processed the entire level 1
4400 * node. This will get saved into the root's drop_snapshot_progress
4401 * field so these drops are not repeated again if this transaction
4402 * commits.
4403 */
4404 path->slots[1] = nritems;
4405 return 0;
4406}
4407
4408/*
Chris Mason9aca1d52007-03-13 11:09:37 -04004409 * helper function for drop_snapshot, this walks down the tree dropping ref
4410 * counts as it goes.
4411 */
Chris Masond3977122009-01-05 21:25:51 -05004412static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05004413 struct btrfs_root *root,
4414 struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05004415{
Chris Mason7bb86312007-12-11 09:25:06 -05004416 u64 root_owner;
4417 u64 root_gen;
4418 u64 bytenr;
Chris Masonca7a79a2008-05-12 12:59:19 -04004419 u64 ptr_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04004420 struct extent_buffer *next;
4421 struct extent_buffer *cur;
Chris Mason7bb86312007-12-11 09:25:06 -05004422 struct extent_buffer *parent;
Chris Masondb945352007-10-15 16:15:53 -04004423 u32 blocksize;
Chris Mason20524f02007-03-10 06:35:47 -05004424 int ret;
4425 u32 refs;
4426
Chris Mason5caf2a02007-04-02 11:20:42 -04004427 WARN_ON(*level < 0);
4428 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason56bec292009-03-13 10:10:06 -04004429 ret = drop_snap_lookup_refcount(trans, root, path->nodes[*level]->start,
Chris Masondb945352007-10-15 16:15:53 -04004430 path->nodes[*level]->len, &refs);
Chris Mason20524f02007-03-10 06:35:47 -05004431 BUG_ON(ret);
4432 if (refs > 1)
4433 goto out;
Chris Masone0115992007-06-19 16:23:05 -04004434
Chris Mason9aca1d52007-03-13 11:09:37 -04004435 /*
4436 * walk down to the last node level and free all the leaves
4437 */
Chris Masond3977122009-01-05 21:25:51 -05004438 while (*level >= 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -04004439 WARN_ON(*level < 0);
4440 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason20524f02007-03-10 06:35:47 -05004441 cur = path->nodes[*level];
Chris Masone0115992007-06-19 16:23:05 -04004442
Chris Mason5f39d392007-10-15 16:14:19 -04004443 if (btrfs_header_level(cur) != *level)
Chris Mason2c90e5d2007-04-02 10:50:19 -04004444 WARN_ON(1);
Chris Masone0115992007-06-19 16:23:05 -04004445
Chris Mason7518a232007-03-12 12:01:18 -04004446 if (path->slots[*level] >=
Chris Mason5f39d392007-10-15 16:14:19 -04004447 btrfs_header_nritems(cur))
Chris Mason20524f02007-03-10 06:35:47 -05004448 break;
Chris Masonbd56b302009-02-04 09:27:02 -05004449
4450 /* the new code goes down to level 1 and does all the
4451 * leaves pointed to that node in bulk. So, this check
4452 * for level 0 will always be false.
4453 *
4454 * But, the disk format allows the drop_snapshot_progress
4455 * field in the root to leave things in a state where
4456 * a leaf will need cleaning up here. If someone crashes
4457 * with the old code and then boots with the new code,
4458 * we might find a leaf here.
4459 */
Chris Mason6407bf62007-03-27 06:33:00 -04004460 if (*level == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004461 ret = btrfs_drop_leaf_ref(trans, root, cur);
Chris Mason6407bf62007-03-27 06:33:00 -04004462 BUG_ON(ret);
4463 break;
4464 }
Chris Masonbd56b302009-02-04 09:27:02 -05004465
4466 /*
4467 * once we get to level one, process the whole node
4468 * at once, including everything below it.
4469 */
4470 if (*level == 1) {
4471 ret = drop_level_one_refs(trans, root, path);
4472 BUG_ON(ret);
4473 break;
4474 }
4475
Chris Masondb945352007-10-15 16:15:53 -04004476 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
Chris Masonca7a79a2008-05-12 12:59:19 -04004477 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
Chris Masondb945352007-10-15 16:15:53 -04004478 blocksize = btrfs_level_size(root, *level - 1);
Chris Mason925baed2008-06-25 16:01:30 -04004479
Chris Mason56bec292009-03-13 10:10:06 -04004480 ret = drop_snap_lookup_refcount(trans, root, bytenr,
4481 blocksize, &refs);
Chris Mason6407bf62007-03-27 06:33:00 -04004482 BUG_ON(ret);
Chris Masonbd56b302009-02-04 09:27:02 -05004483
4484 /*
4485 * if there is more than one reference, we don't need
4486 * to read that node to drop any references it has. We
4487 * just drop the ref we hold on that node and move on to the
4488 * next slot in this level.
4489 */
Chris Mason6407bf62007-03-27 06:33:00 -04004490 if (refs != 1) {
Chris Mason7bb86312007-12-11 09:25:06 -05004491 parent = path->nodes[*level];
4492 root_owner = btrfs_header_owner(parent);
4493 root_gen = btrfs_header_generation(parent);
Chris Mason20524f02007-03-10 06:35:47 -05004494 path->slots[*level]++;
Chris Masonf87f0572008-08-01 11:27:23 -04004495
Chris Mason56bec292009-03-13 10:10:06 -04004496 ret = btrfs_free_extent(trans, root, bytenr,
Zheng Yan31840ae2008-09-23 13:14:14 -04004497 blocksize, parent->start,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004498 root_owner, root_gen,
4499 *level - 1, 1);
Chris Mason20524f02007-03-10 06:35:47 -05004500 BUG_ON(ret);
Chris Mason18e35e0a2008-08-01 13:11:41 -04004501
4502 atomic_inc(&root->fs_info->throttle_gen);
4503 wake_up(&root->fs_info->transaction_throttle);
Chris Mason2dd3e672008-08-04 08:20:15 -04004504 cond_resched();
Chris Mason18e35e0a2008-08-01 13:11:41 -04004505
Chris Mason20524f02007-03-10 06:35:47 -05004506 continue;
4507 }
Chris Mason333db942008-06-25 16:01:30 -04004508
Chris Masonbd56b302009-02-04 09:27:02 -05004509 /*
4510 * we need to keep freeing things in the next level down.
4511 * read the block and loop around to process it
4512 */
4513 next = read_tree_block(root, bytenr, blocksize, ptr_gen);
Chris Mason5caf2a02007-04-02 11:20:42 -04004514 WARN_ON(*level <= 0);
Chris Mason83e15a22007-03-12 09:03:27 -04004515 if (path->nodes[*level-1])
Chris Mason5f39d392007-10-15 16:14:19 -04004516 free_extent_buffer(path->nodes[*level-1]);
Chris Mason20524f02007-03-10 06:35:47 -05004517 path->nodes[*level-1] = next;
Chris Mason5f39d392007-10-15 16:14:19 -04004518 *level = btrfs_header_level(next);
Chris Mason20524f02007-03-10 06:35:47 -05004519 path->slots[*level] = 0;
Chris Mason2dd3e672008-08-04 08:20:15 -04004520 cond_resched();
Chris Mason20524f02007-03-10 06:35:47 -05004521 }
4522out:
Chris Mason5caf2a02007-04-02 11:20:42 -04004523 WARN_ON(*level < 0);
4524 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason7bb86312007-12-11 09:25:06 -05004525
4526 if (path->nodes[*level] == root->node) {
Chris Mason7bb86312007-12-11 09:25:06 -05004527 parent = path->nodes[*level];
Yan Zheng31153d82008-07-28 15:32:19 -04004528 bytenr = path->nodes[*level]->start;
Chris Mason7bb86312007-12-11 09:25:06 -05004529 } else {
4530 parent = path->nodes[*level + 1];
Yan Zheng31153d82008-07-28 15:32:19 -04004531 bytenr = btrfs_node_blockptr(parent, path->slots[*level + 1]);
Chris Mason7bb86312007-12-11 09:25:06 -05004532 }
4533
Yan Zheng31153d82008-07-28 15:32:19 -04004534 blocksize = btrfs_level_size(root, *level);
4535 root_owner = btrfs_header_owner(parent);
Chris Mason7bb86312007-12-11 09:25:06 -05004536 root_gen = btrfs_header_generation(parent);
Yan Zheng31153d82008-07-28 15:32:19 -04004537
Chris Masonbd56b302009-02-04 09:27:02 -05004538 /*
4539 * cleanup and free the reference on the last node
4540 * we processed
4541 */
Chris Mason56bec292009-03-13 10:10:06 -04004542 ret = btrfs_free_extent(trans, root, bytenr, blocksize,
Zheng Yan31840ae2008-09-23 13:14:14 -04004543 parent->start, root_owner, root_gen,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004544 *level, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04004545 free_extent_buffer(path->nodes[*level]);
Chris Mason20524f02007-03-10 06:35:47 -05004546 path->nodes[*level] = NULL;
Chris Masonbd56b302009-02-04 09:27:02 -05004547
Chris Mason20524f02007-03-10 06:35:47 -05004548 *level += 1;
4549 BUG_ON(ret);
Chris Masonf87f0572008-08-01 11:27:23 -04004550
Chris Masone7a84562008-06-25 16:01:31 -04004551 cond_resched();
Chris Mason20524f02007-03-10 06:35:47 -05004552 return 0;
4553}
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004554#endif
Chris Mason20524f02007-03-10 06:35:47 -05004555
Chris Mason9aca1d52007-03-13 11:09:37 -04004556/*
Yan Zhengf82d02d2008-10-29 14:49:05 -04004557 * helper function for drop_subtree, this function is similar to
4558 * walk_down_tree. The main difference is that it checks reference
4559 * counts while tree blocks are locked.
4560 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004561static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
4562 struct btrfs_root *root,
4563 struct btrfs_path *path, int *level)
Yan Zhengf82d02d2008-10-29 14:49:05 -04004564{
4565 struct extent_buffer *next;
4566 struct extent_buffer *cur;
4567 struct extent_buffer *parent;
4568 u64 bytenr;
4569 u64 ptr_gen;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004570 u64 refs;
4571 u64 flags;
Yan Zhengf82d02d2008-10-29 14:49:05 -04004572 u32 blocksize;
Yan Zhengf82d02d2008-10-29 14:49:05 -04004573 int ret;
4574
4575 cur = path->nodes[*level];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004576 ret = btrfs_lookup_extent_info(trans, root, cur->start, cur->len,
4577 &refs, &flags);
Yan Zhengf82d02d2008-10-29 14:49:05 -04004578 BUG_ON(ret);
4579 if (refs > 1)
4580 goto out;
4581
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004582 BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
4583
Yan Zhengf82d02d2008-10-29 14:49:05 -04004584 while (*level >= 0) {
4585 cur = path->nodes[*level];
4586 if (*level == 0) {
4587 ret = btrfs_drop_leaf_ref(trans, root, cur);
4588 BUG_ON(ret);
4589 clean_tree_block(trans, root, cur);
4590 break;
4591 }
4592 if (path->slots[*level] >= btrfs_header_nritems(cur)) {
4593 clean_tree_block(trans, root, cur);
4594 break;
4595 }
4596
4597 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
4598 blocksize = btrfs_level_size(root, *level - 1);
4599 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
4600
4601 next = read_tree_block(root, bytenr, blocksize, ptr_gen);
4602 btrfs_tree_lock(next);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004603 btrfs_set_lock_blocking(next);
Yan Zhengf82d02d2008-10-29 14:49:05 -04004604
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004605 ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize,
4606 &refs, &flags);
Yan Zhengf82d02d2008-10-29 14:49:05 -04004607 BUG_ON(ret);
4608 if (refs > 1) {
4609 parent = path->nodes[*level];
4610 ret = btrfs_free_extent(trans, root, bytenr,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004611 blocksize, parent->start,
4612 btrfs_header_owner(parent),
4613 *level - 1, 0);
Yan Zhengf82d02d2008-10-29 14:49:05 -04004614 BUG_ON(ret);
4615 path->slots[*level]++;
4616 btrfs_tree_unlock(next);
4617 free_extent_buffer(next);
4618 continue;
4619 }
4620
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004621 BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
4622
Yan Zhengf82d02d2008-10-29 14:49:05 -04004623 *level = btrfs_header_level(next);
4624 path->nodes[*level] = next;
4625 path->slots[*level] = 0;
4626 path->locks[*level] = 1;
4627 cond_resched();
4628 }
4629out:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004630 if (path->nodes[*level] == root->node)
4631 parent = path->nodes[*level];
4632 else
4633 parent = path->nodes[*level + 1];
Yan Zhengf82d02d2008-10-29 14:49:05 -04004634 bytenr = path->nodes[*level]->start;
4635 blocksize = path->nodes[*level]->len;
4636
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004637 ret = btrfs_free_extent(trans, root, bytenr, blocksize, parent->start,
4638 btrfs_header_owner(parent), *level, 0);
Yan Zhengf82d02d2008-10-29 14:49:05 -04004639 BUG_ON(ret);
4640
4641 if (path->locks[*level]) {
4642 btrfs_tree_unlock(path->nodes[*level]);
4643 path->locks[*level] = 0;
4644 }
4645 free_extent_buffer(path->nodes[*level]);
4646 path->nodes[*level] = NULL;
4647 *level += 1;
4648 cond_resched();
4649 return 0;
4650}
4651
4652/*
Chris Mason9aca1d52007-03-13 11:09:37 -04004653 * helper for dropping snapshots. This walks back up the tree in the path
4654 * to find the first node higher up where we haven't yet gone through
4655 * all the slots
4656 */
Chris Masond3977122009-01-05 21:25:51 -05004657static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05004658 struct btrfs_root *root,
Yan Zhengf82d02d2008-10-29 14:49:05 -04004659 struct btrfs_path *path,
4660 int *level, int max_level)
Chris Mason20524f02007-03-10 06:35:47 -05004661{
Chris Mason7bb86312007-12-11 09:25:06 -05004662 struct btrfs_root_item *root_item = &root->root_item;
Chris Mason20524f02007-03-10 06:35:47 -05004663 int i;
4664 int slot;
4665 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04004666
Yan Zhengf82d02d2008-10-29 14:49:05 -04004667 for (i = *level; i < max_level && path->nodes[i]; i++) {
Chris Mason20524f02007-03-10 06:35:47 -05004668 slot = path->slots[i];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004669 if (slot + 1 < btrfs_header_nritems(path->nodes[i])) {
Chris Masonbd56b302009-02-04 09:27:02 -05004670 /*
4671 * there is more work to do in this level.
4672 * Update the drop_progress marker to reflect
4673 * the work we've done so far, and then bump
4674 * the slot number
4675 */
Chris Mason20524f02007-03-10 06:35:47 -05004676 path->slots[i]++;
Chris Mason9f3a7422007-08-07 15:52:19 -04004677 WARN_ON(*level == 0);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004678 if (max_level == BTRFS_MAX_LEVEL) {
4679 btrfs_node_key(path->nodes[i],
4680 &root_item->drop_progress,
4681 path->slots[i]);
4682 root_item->drop_level = i;
4683 }
4684 *level = i;
Chris Mason20524f02007-03-10 06:35:47 -05004685 return 0;
4686 } else {
Zheng Yan31840ae2008-09-23 13:14:14 -04004687 struct extent_buffer *parent;
Chris Masonbd56b302009-02-04 09:27:02 -05004688
4689 /*
4690 * this whole node is done, free our reference
4691 * on it and go up one level
4692 */
Zheng Yan31840ae2008-09-23 13:14:14 -04004693 if (path->nodes[*level] == root->node)
4694 parent = path->nodes[*level];
4695 else
4696 parent = path->nodes[*level + 1];
4697
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004698 clean_tree_block(trans, root, path->nodes[i]);
Chris Masone089f052007-03-16 16:20:31 -04004699 ret = btrfs_free_extent(trans, root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004700 path->nodes[i]->start,
4701 path->nodes[i]->len,
4702 parent->start,
4703 btrfs_header_owner(parent),
4704 *level, 0);
Chris Mason6407bf62007-03-27 06:33:00 -04004705 BUG_ON(ret);
Yan Zhengf82d02d2008-10-29 14:49:05 -04004706 if (path->locks[*level]) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004707 btrfs_tree_unlock(path->nodes[i]);
4708 path->locks[i] = 0;
Yan Zhengf82d02d2008-10-29 14:49:05 -04004709 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004710 free_extent_buffer(path->nodes[i]);
4711 path->nodes[i] = NULL;
Chris Mason20524f02007-03-10 06:35:47 -05004712 *level = i + 1;
Chris Mason20524f02007-03-10 06:35:47 -05004713 }
4714 }
4715 return 1;
4716}
4717
Chris Mason9aca1d52007-03-13 11:09:37 -04004718/*
4719 * drop the reference count on the tree rooted at 'snap'. This traverses
4720 * the tree freeing any blocks that have a ref count of zero after being
4721 * decremented.
4722 */
Chris Masone089f052007-03-16 16:20:31 -04004723int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason9f3a7422007-08-07 15:52:19 -04004724 *root)
Chris Mason20524f02007-03-10 06:35:47 -05004725{
Chris Mason3768f362007-03-13 16:47:54 -04004726 int ret = 0;
Chris Mason9aca1d52007-03-13 11:09:37 -04004727 int wret;
Chris Mason20524f02007-03-10 06:35:47 -05004728 int level;
Chris Mason5caf2a02007-04-02 11:20:42 -04004729 struct btrfs_path *path;
Chris Masonc3e69d52009-03-13 10:17:05 -04004730 int update_count;
Chris Mason9f3a7422007-08-07 15:52:19 -04004731 struct btrfs_root_item *root_item = &root->root_item;
Chris Mason20524f02007-03-10 06:35:47 -05004732
Chris Mason5caf2a02007-04-02 11:20:42 -04004733 path = btrfs_alloc_path();
4734 BUG_ON(!path);
Chris Mason20524f02007-03-10 06:35:47 -05004735
Chris Mason5f39d392007-10-15 16:14:19 -04004736 level = btrfs_header_level(root->node);
Chris Mason9f3a7422007-08-07 15:52:19 -04004737 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004738 path->nodes[level] = btrfs_lock_root_node(root);
4739 btrfs_set_lock_blocking(path->nodes[level]);
Chris Mason9f3a7422007-08-07 15:52:19 -04004740 path->slots[level] = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004741 path->locks[level] = 1;
Chris Mason9f3a7422007-08-07 15:52:19 -04004742 } else {
4743 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04004744 struct btrfs_disk_key found_key;
4745 struct extent_buffer *node;
Chris Mason6702ed42007-08-07 16:15:09 -04004746
Chris Mason9f3a7422007-08-07 15:52:19 -04004747 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Chris Mason6702ed42007-08-07 16:15:09 -04004748 level = root_item->drop_level;
4749 path->lowest_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04004750 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason6702ed42007-08-07 16:15:09 -04004751 if (wret < 0) {
Chris Mason9f3a7422007-08-07 15:52:19 -04004752 ret = wret;
4753 goto out;
4754 }
Chris Mason5f39d392007-10-15 16:14:19 -04004755 node = path->nodes[level];
4756 btrfs_node_key(node, &found_key, path->slots[level]);
4757 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
4758 sizeof(found_key)));
Chris Mason7d9eb122008-07-08 14:19:17 -04004759 /*
4760 * unlock our path, this is safe because only this
4761 * function is allowed to delete this snapshot
4762 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004763 btrfs_unlock_up_safe(path, 0);
Chris Mason9f3a7422007-08-07 15:52:19 -04004764 }
Chris Masond3977122009-01-05 21:25:51 -05004765 while (1) {
Chris Masonc3e69d52009-03-13 10:17:05 -04004766 unsigned long update;
Chris Mason5caf2a02007-04-02 11:20:42 -04004767 wret = walk_down_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04004768 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05004769 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04004770 if (wret < 0)
4771 ret = wret;
4772
Yan Zhengf82d02d2008-10-29 14:49:05 -04004773 wret = walk_up_tree(trans, root, path, &level,
4774 BTRFS_MAX_LEVEL);
Chris Mason9aca1d52007-03-13 11:09:37 -04004775 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05004776 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04004777 if (wret < 0)
4778 ret = wret;
Chris Masonb7ec40d2009-03-12 20:12:45 -04004779 if (trans->transaction->in_commit ||
4780 trans->transaction->delayed_refs.flushing) {
Chris Masone7a84562008-06-25 16:01:31 -04004781 ret = -EAGAIN;
4782 break;
4783 }
Chris Masonc3e69d52009-03-13 10:17:05 -04004784 for (update_count = 0; update_count < 16; update_count++) {
4785 update = trans->delayed_ref_updates;
4786 trans->delayed_ref_updates = 0;
4787 if (update)
4788 btrfs_run_delayed_refs(trans, root, update);
4789 else
4790 break;
4791 }
Chris Mason20524f02007-03-10 06:35:47 -05004792 }
Chris Mason9f3a7422007-08-07 15:52:19 -04004793out:
Chris Mason5caf2a02007-04-02 11:20:42 -04004794 btrfs_free_path(path);
Chris Mason9aca1d52007-03-13 11:09:37 -04004795 return ret;
Chris Mason20524f02007-03-10 06:35:47 -05004796}
Chris Mason9078a3e2007-04-26 16:46:15 -04004797
Yan Zhengf82d02d2008-10-29 14:49:05 -04004798int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
4799 struct btrfs_root *root,
4800 struct extent_buffer *node,
4801 struct extent_buffer *parent)
4802{
4803 struct btrfs_path *path;
4804 int level;
4805 int parent_level;
4806 int ret = 0;
4807 int wret;
4808
4809 path = btrfs_alloc_path();
4810 BUG_ON(!path);
4811
Chris Masonb9447ef2009-03-09 11:45:38 -04004812 btrfs_assert_tree_locked(parent);
Yan Zhengf82d02d2008-10-29 14:49:05 -04004813 parent_level = btrfs_header_level(parent);
4814 extent_buffer_get(parent);
4815 path->nodes[parent_level] = parent;
4816 path->slots[parent_level] = btrfs_header_nritems(parent);
4817
Chris Masonb9447ef2009-03-09 11:45:38 -04004818 btrfs_assert_tree_locked(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04004819 level = btrfs_header_level(node);
4820 extent_buffer_get(node);
4821 path->nodes[level] = node;
4822 path->slots[level] = 0;
4823
4824 while (1) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004825 wret = walk_down_tree(trans, root, path, &level);
Yan Zhengf82d02d2008-10-29 14:49:05 -04004826 if (wret < 0)
4827 ret = wret;
4828 if (wret != 0)
4829 break;
4830
4831 wret = walk_up_tree(trans, root, path, &level, parent_level);
4832 if (wret < 0)
4833 ret = wret;
4834 if (wret != 0)
4835 break;
4836 }
4837
4838 btrfs_free_path(path);
4839 return ret;
4840}
4841
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004842#if 0
Chris Mason8e7bf942008-04-28 09:02:36 -04004843static unsigned long calc_ra(unsigned long start, unsigned long last,
4844 unsigned long nr)
4845{
4846 return min(last, start + nr - 1);
4847}
4848
Chris Masond3977122009-01-05 21:25:51 -05004849static noinline int relocate_inode_pages(struct inode *inode, u64 start,
Chris Mason98ed5172008-01-03 10:01:48 -05004850 u64 len)
Chris Masonedbd8d42007-12-21 16:27:24 -05004851{
4852 u64 page_start;
4853 u64 page_end;
Zheng Yan1a40e232008-09-26 10:09:34 -04004854 unsigned long first_index;
Chris Masonedbd8d42007-12-21 16:27:24 -05004855 unsigned long last_index;
Chris Masonedbd8d42007-12-21 16:27:24 -05004856 unsigned long i;
4857 struct page *page;
Chris Masond1310b22008-01-24 16:13:08 -05004858 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason4313b392008-01-03 09:08:48 -05004859 struct file_ra_state *ra;
Chris Mason3eaa2882008-07-24 11:57:52 -04004860 struct btrfs_ordered_extent *ordered;
Zheng Yan1a40e232008-09-26 10:09:34 -04004861 unsigned int total_read = 0;
4862 unsigned int total_dirty = 0;
4863 int ret = 0;
Chris Mason4313b392008-01-03 09:08:48 -05004864
4865 ra = kzalloc(sizeof(*ra), GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05004866
4867 mutex_lock(&inode->i_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04004868 first_index = start >> PAGE_CACHE_SHIFT;
Chris Masonedbd8d42007-12-21 16:27:24 -05004869 last_index = (start + len - 1) >> PAGE_CACHE_SHIFT;
4870
Zheng Yan1a40e232008-09-26 10:09:34 -04004871 /* make sure the dirty trick played by the caller work */
4872 ret = invalidate_inode_pages2_range(inode->i_mapping,
4873 first_index, last_index);
4874 if (ret)
4875 goto out_unlock;
Chris Mason8e7bf942008-04-28 09:02:36 -04004876
Chris Mason4313b392008-01-03 09:08:48 -05004877 file_ra_state_init(ra, inode->i_mapping);
Chris Masonedbd8d42007-12-21 16:27:24 -05004878
Zheng Yan1a40e232008-09-26 10:09:34 -04004879 for (i = first_index ; i <= last_index; i++) {
4880 if (total_read % ra->ra_pages == 0) {
Chris Mason8e7bf942008-04-28 09:02:36 -04004881 btrfs_force_ra(inode->i_mapping, ra, NULL, i,
Zheng Yan1a40e232008-09-26 10:09:34 -04004882 calc_ra(i, last_index, ra->ra_pages));
Chris Mason8e7bf942008-04-28 09:02:36 -04004883 }
4884 total_read++;
Chris Mason3eaa2882008-07-24 11:57:52 -04004885again:
4886 if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode))
Zheng Yan1a40e232008-09-26 10:09:34 -04004887 BUG_ON(1);
Chris Masonedbd8d42007-12-21 16:27:24 -05004888 page = grab_cache_page(inode->i_mapping, i);
Chris Masona061fc82008-05-07 11:43:44 -04004889 if (!page) {
Zheng Yan1a40e232008-09-26 10:09:34 -04004890 ret = -ENOMEM;
Chris Masonedbd8d42007-12-21 16:27:24 -05004891 goto out_unlock;
Chris Masona061fc82008-05-07 11:43:44 -04004892 }
Chris Masonedbd8d42007-12-21 16:27:24 -05004893 if (!PageUptodate(page)) {
4894 btrfs_readpage(NULL, page);
4895 lock_page(page);
4896 if (!PageUptodate(page)) {
4897 unlock_page(page);
4898 page_cache_release(page);
Zheng Yan1a40e232008-09-26 10:09:34 -04004899 ret = -EIO;
Chris Masonedbd8d42007-12-21 16:27:24 -05004900 goto out_unlock;
4901 }
4902 }
Chris Masonec44a352008-04-28 15:29:52 -04004903 wait_on_page_writeback(page);
Chris Mason3eaa2882008-07-24 11:57:52 -04004904
Chris Masonedbd8d42007-12-21 16:27:24 -05004905 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
4906 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05004907 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05004908
Chris Mason3eaa2882008-07-24 11:57:52 -04004909 ordered = btrfs_lookup_ordered_extent(inode, page_start);
4910 if (ordered) {
4911 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
4912 unlock_page(page);
4913 page_cache_release(page);
4914 btrfs_start_ordered_extent(inode, ordered, 1);
4915 btrfs_put_ordered_extent(ordered);
4916 goto again;
4917 }
4918 set_page_extent_mapped(page);
4919
Zheng Yan1a40e232008-09-26 10:09:34 -04004920 if (i == first_index)
4921 set_extent_bits(io_tree, page_start, page_end,
4922 EXTENT_BOUNDARY, GFP_NOFS);
Yan Zheng1f80e4d2008-12-19 10:59:04 -05004923 btrfs_set_extent_delalloc(inode, page_start, page_end);
Zheng Yan1a40e232008-09-26 10:09:34 -04004924
Chris Masona061fc82008-05-07 11:43:44 -04004925 set_page_dirty(page);
Zheng Yan1a40e232008-09-26 10:09:34 -04004926 total_dirty++;
Chris Masonedbd8d42007-12-21 16:27:24 -05004927
Chris Masond1310b22008-01-24 16:13:08 -05004928 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05004929 unlock_page(page);
4930 page_cache_release(page);
4931 }
4932
4933out_unlock:
Chris Masonec44a352008-04-28 15:29:52 -04004934 kfree(ra);
Chris Masonedbd8d42007-12-21 16:27:24 -05004935 mutex_unlock(&inode->i_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04004936 balance_dirty_pages_ratelimited_nr(inode->i_mapping, total_dirty);
Chris Masonbf4ef672008-05-08 13:26:18 -04004937 return ret;
4938}
4939
Chris Masond3977122009-01-05 21:25:51 -05004940static noinline int relocate_data_extent(struct inode *reloc_inode,
Zheng Yan1a40e232008-09-26 10:09:34 -04004941 struct btrfs_key *extent_key,
4942 u64 offset)
Chris Masonedbd8d42007-12-21 16:27:24 -05004943{
Zheng Yan1a40e232008-09-26 10:09:34 -04004944 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
4945 struct extent_map_tree *em_tree = &BTRFS_I(reloc_inode)->extent_tree;
4946 struct extent_map *em;
Yan Zheng66435582008-10-30 14:19:50 -04004947 u64 start = extent_key->objectid - offset;
4948 u64 end = start + extent_key->offset - 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04004949
4950 em = alloc_extent_map(GFP_NOFS);
4951 BUG_ON(!em || IS_ERR(em));
4952
Yan Zheng66435582008-10-30 14:19:50 -04004953 em->start = start;
Zheng Yan1a40e232008-09-26 10:09:34 -04004954 em->len = extent_key->offset;
Chris Masonc8b97812008-10-29 14:49:59 -04004955 em->block_len = extent_key->offset;
Zheng Yan1a40e232008-09-26 10:09:34 -04004956 em->block_start = extent_key->objectid;
4957 em->bdev = root->fs_info->fs_devices->latest_bdev;
4958 set_bit(EXTENT_FLAG_PINNED, &em->flags);
4959
4960 /* setup extent map to cheat btrfs_readpage */
Yan Zheng66435582008-10-30 14:19:50 -04004961 lock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04004962 while (1) {
4963 int ret;
4964 spin_lock(&em_tree->lock);
4965 ret = add_extent_mapping(em_tree, em);
4966 spin_unlock(&em_tree->lock);
4967 if (ret != -EEXIST) {
4968 free_extent_map(em);
4969 break;
4970 }
Yan Zheng66435582008-10-30 14:19:50 -04004971 btrfs_drop_extent_cache(reloc_inode, start, end, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04004972 }
Yan Zheng66435582008-10-30 14:19:50 -04004973 unlock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04004974
Yan Zheng66435582008-10-30 14:19:50 -04004975 return relocate_inode_pages(reloc_inode, start, extent_key->offset);
Zheng Yan1a40e232008-09-26 10:09:34 -04004976}
4977
4978struct btrfs_ref_path {
4979 u64 extent_start;
4980 u64 nodes[BTRFS_MAX_LEVEL];
4981 u64 root_objectid;
4982 u64 root_generation;
4983 u64 owner_objectid;
Zheng Yan1a40e232008-09-26 10:09:34 -04004984 u32 num_refs;
4985 int lowest_level;
4986 int current_level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04004987 int shared_level;
4988
4989 struct btrfs_key node_keys[BTRFS_MAX_LEVEL];
4990 u64 new_nodes[BTRFS_MAX_LEVEL];
Zheng Yan1a40e232008-09-26 10:09:34 -04004991};
4992
4993struct disk_extent {
Chris Masonc8b97812008-10-29 14:49:59 -04004994 u64 ram_bytes;
Zheng Yan1a40e232008-09-26 10:09:34 -04004995 u64 disk_bytenr;
4996 u64 disk_num_bytes;
4997 u64 offset;
4998 u64 num_bytes;
Chris Masonc8b97812008-10-29 14:49:59 -04004999 u8 compression;
5000 u8 encryption;
5001 u16 other_encoding;
Zheng Yan1a40e232008-09-26 10:09:34 -04005002};
5003
5004static int is_cowonly_root(u64 root_objectid)
5005{
5006 if (root_objectid == BTRFS_ROOT_TREE_OBJECTID ||
5007 root_objectid == BTRFS_EXTENT_TREE_OBJECTID ||
5008 root_objectid == BTRFS_CHUNK_TREE_OBJECTID ||
5009 root_objectid == BTRFS_DEV_TREE_OBJECTID ||
Yan Zheng0403e472008-12-10 20:32:51 -05005010 root_objectid == BTRFS_TREE_LOG_OBJECTID ||
5011 root_objectid == BTRFS_CSUM_TREE_OBJECTID)
Zheng Yan1a40e232008-09-26 10:09:34 -04005012 return 1;
5013 return 0;
5014}
5015
Chris Masond3977122009-01-05 21:25:51 -05005016static noinline int __next_ref_path(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04005017 struct btrfs_root *extent_root,
5018 struct btrfs_ref_path *ref_path,
5019 int first_time)
5020{
5021 struct extent_buffer *leaf;
5022 struct btrfs_path *path;
Chris Mason4313b392008-01-03 09:08:48 -05005023 struct btrfs_extent_ref *ref;
Chris Masonedbd8d42007-12-21 16:27:24 -05005024 struct btrfs_key key;
5025 struct btrfs_key found_key;
Zheng Yan1a40e232008-09-26 10:09:34 -04005026 u64 bytenr;
Chris Masonedbd8d42007-12-21 16:27:24 -05005027 u32 nritems;
Zheng Yan1a40e232008-09-26 10:09:34 -04005028 int level;
5029 int ret = 1;
Chris Masonedbd8d42007-12-21 16:27:24 -05005030
Zheng Yan1a40e232008-09-26 10:09:34 -04005031 path = btrfs_alloc_path();
5032 if (!path)
5033 return -ENOMEM;
5034
Zheng Yan1a40e232008-09-26 10:09:34 -04005035 if (first_time) {
5036 ref_path->lowest_level = -1;
5037 ref_path->current_level = -1;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005038 ref_path->shared_level = -1;
Zheng Yan1a40e232008-09-26 10:09:34 -04005039 goto walk_up;
Chris Masona061fc82008-05-07 11:43:44 -04005040 }
Zheng Yan1a40e232008-09-26 10:09:34 -04005041walk_down:
5042 level = ref_path->current_level - 1;
5043 while (level >= -1) {
5044 u64 parent;
5045 if (level < ref_path->lowest_level)
5046 break;
Chris Masonedbd8d42007-12-21 16:27:24 -05005047
Chris Masond3977122009-01-05 21:25:51 -05005048 if (level >= 0)
Zheng Yan1a40e232008-09-26 10:09:34 -04005049 bytenr = ref_path->nodes[level];
Chris Masond3977122009-01-05 21:25:51 -05005050 else
Zheng Yan1a40e232008-09-26 10:09:34 -04005051 bytenr = ref_path->extent_start;
Zheng Yan1a40e232008-09-26 10:09:34 -04005052 BUG_ON(bytenr == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05005053
Zheng Yan1a40e232008-09-26 10:09:34 -04005054 parent = ref_path->nodes[level + 1];
5055 ref_path->nodes[level + 1] = 0;
5056 ref_path->current_level = level;
5057 BUG_ON(parent == 0);
5058
5059 key.objectid = bytenr;
5060 key.offset = parent + 1;
5061 key.type = BTRFS_EXTENT_REF_KEY;
5062
5063 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05005064 if (ret < 0)
5065 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04005066 BUG_ON(ret == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05005067
Chris Masonedbd8d42007-12-21 16:27:24 -05005068 leaf = path->nodes[0];
5069 nritems = btrfs_header_nritems(leaf);
Zheng Yan1a40e232008-09-26 10:09:34 -04005070 if (path->slots[0] >= nritems) {
Chris Masona061fc82008-05-07 11:43:44 -04005071 ret = btrfs_next_leaf(extent_root, path);
Chris Masona061fc82008-05-07 11:43:44 -04005072 if (ret < 0)
5073 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04005074 if (ret > 0)
5075 goto next;
Chris Masonbf4ef672008-05-08 13:26:18 -04005076 leaf = path->nodes[0];
Chris Masona061fc82008-05-07 11:43:44 -04005077 }
Chris Masonedbd8d42007-12-21 16:27:24 -05005078
5079 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Zheng Yan1a40e232008-09-26 10:09:34 -04005080 if (found_key.objectid == bytenr &&
Yan Zhengf82d02d2008-10-29 14:49:05 -04005081 found_key.type == BTRFS_EXTENT_REF_KEY) {
5082 if (level < ref_path->shared_level)
5083 ref_path->shared_level = level;
Zheng Yan1a40e232008-09-26 10:09:34 -04005084 goto found;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005085 }
Zheng Yan1a40e232008-09-26 10:09:34 -04005086next:
5087 level--;
5088 btrfs_release_path(extent_root, path);
Yan Zhengd899e052008-10-30 14:25:28 -04005089 cond_resched();
Zheng Yan1a40e232008-09-26 10:09:34 -04005090 }
5091 /* reached lowest level */
5092 ret = 1;
5093 goto out;
5094walk_up:
5095 level = ref_path->current_level;
5096 while (level < BTRFS_MAX_LEVEL - 1) {
5097 u64 ref_objectid;
Chris Masond3977122009-01-05 21:25:51 -05005098
5099 if (level >= 0)
Zheng Yan1a40e232008-09-26 10:09:34 -04005100 bytenr = ref_path->nodes[level];
Chris Masond3977122009-01-05 21:25:51 -05005101 else
Zheng Yan1a40e232008-09-26 10:09:34 -04005102 bytenr = ref_path->extent_start;
Chris Masond3977122009-01-05 21:25:51 -05005103
Zheng Yan1a40e232008-09-26 10:09:34 -04005104 BUG_ON(bytenr == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05005105
Zheng Yan1a40e232008-09-26 10:09:34 -04005106 key.objectid = bytenr;
5107 key.offset = 0;
5108 key.type = BTRFS_EXTENT_REF_KEY;
Chris Masonedbd8d42007-12-21 16:27:24 -05005109
Zheng Yan1a40e232008-09-26 10:09:34 -04005110 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
5111 if (ret < 0)
Chris Masonedbd8d42007-12-21 16:27:24 -05005112 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04005113
5114 leaf = path->nodes[0];
5115 nritems = btrfs_header_nritems(leaf);
5116 if (path->slots[0] >= nritems) {
5117 ret = btrfs_next_leaf(extent_root, path);
5118 if (ret < 0)
5119 goto out;
5120 if (ret > 0) {
5121 /* the extent was freed by someone */
5122 if (ref_path->lowest_level == level)
5123 goto out;
5124 btrfs_release_path(extent_root, path);
5125 goto walk_down;
5126 }
5127 leaf = path->nodes[0];
5128 }
5129
5130 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5131 if (found_key.objectid != bytenr ||
5132 found_key.type != BTRFS_EXTENT_REF_KEY) {
5133 /* the extent was freed by someone */
5134 if (ref_path->lowest_level == level) {
5135 ret = 1;
5136 goto out;
5137 }
5138 btrfs_release_path(extent_root, path);
5139 goto walk_down;
5140 }
5141found:
5142 ref = btrfs_item_ptr(leaf, path->slots[0],
5143 struct btrfs_extent_ref);
5144 ref_objectid = btrfs_ref_objectid(leaf, ref);
5145 if (ref_objectid < BTRFS_FIRST_FREE_OBJECTID) {
5146 if (first_time) {
5147 level = (int)ref_objectid;
5148 BUG_ON(level >= BTRFS_MAX_LEVEL);
5149 ref_path->lowest_level = level;
5150 ref_path->current_level = level;
5151 ref_path->nodes[level] = bytenr;
5152 } else {
5153 WARN_ON(ref_objectid != level);
5154 }
5155 } else {
5156 WARN_ON(level != -1);
5157 }
5158 first_time = 0;
5159
5160 if (ref_path->lowest_level == level) {
5161 ref_path->owner_objectid = ref_objectid;
Zheng Yan1a40e232008-09-26 10:09:34 -04005162 ref_path->num_refs = btrfs_ref_num_refs(leaf, ref);
5163 }
5164
5165 /*
5166 * the block is tree root or the block isn't in reference
5167 * counted tree.
5168 */
5169 if (found_key.objectid == found_key.offset ||
5170 is_cowonly_root(btrfs_ref_root(leaf, ref))) {
5171 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
5172 ref_path->root_generation =
5173 btrfs_ref_generation(leaf, ref);
5174 if (level < 0) {
5175 /* special reference from the tree log */
5176 ref_path->nodes[0] = found_key.offset;
5177 ref_path->current_level = 0;
5178 }
5179 ret = 0;
5180 goto out;
5181 }
5182
5183 level++;
5184 BUG_ON(ref_path->nodes[level] != 0);
5185 ref_path->nodes[level] = found_key.offset;
5186 ref_path->current_level = level;
5187
5188 /*
5189 * the reference was created in the running transaction,
5190 * no need to continue walking up.
5191 */
5192 if (btrfs_ref_generation(leaf, ref) == trans->transid) {
5193 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
5194 ref_path->root_generation =
5195 btrfs_ref_generation(leaf, ref);
5196 ret = 0;
5197 goto out;
5198 }
5199
5200 btrfs_release_path(extent_root, path);
Yan Zhengd899e052008-10-30 14:25:28 -04005201 cond_resched();
Zheng Yan1a40e232008-09-26 10:09:34 -04005202 }
5203 /* reached max tree level, but no tree root found. */
5204 BUG();
5205out:
Zheng Yan1a40e232008-09-26 10:09:34 -04005206 btrfs_free_path(path);
5207 return ret;
5208}
5209
5210static int btrfs_first_ref_path(struct btrfs_trans_handle *trans,
5211 struct btrfs_root *extent_root,
5212 struct btrfs_ref_path *ref_path,
5213 u64 extent_start)
5214{
5215 memset(ref_path, 0, sizeof(*ref_path));
5216 ref_path->extent_start = extent_start;
5217
5218 return __next_ref_path(trans, extent_root, ref_path, 1);
5219}
5220
5221static int btrfs_next_ref_path(struct btrfs_trans_handle *trans,
5222 struct btrfs_root *extent_root,
5223 struct btrfs_ref_path *ref_path)
5224{
5225 return __next_ref_path(trans, extent_root, ref_path, 0);
5226}
5227
Chris Masond3977122009-01-05 21:25:51 -05005228static noinline int get_new_locations(struct inode *reloc_inode,
Zheng Yan1a40e232008-09-26 10:09:34 -04005229 struct btrfs_key *extent_key,
5230 u64 offset, int no_fragment,
5231 struct disk_extent **extents,
5232 int *nr_extents)
5233{
5234 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
5235 struct btrfs_path *path;
5236 struct btrfs_file_extent_item *fi;
5237 struct extent_buffer *leaf;
5238 struct disk_extent *exts = *extents;
5239 struct btrfs_key found_key;
5240 u64 cur_pos;
5241 u64 last_byte;
5242 u32 nritems;
5243 int nr = 0;
5244 int max = *nr_extents;
5245 int ret;
5246
5247 WARN_ON(!no_fragment && *extents);
5248 if (!exts) {
5249 max = 1;
5250 exts = kmalloc(sizeof(*exts) * max, GFP_NOFS);
5251 if (!exts)
5252 return -ENOMEM;
5253 }
5254
5255 path = btrfs_alloc_path();
5256 BUG_ON(!path);
5257
5258 cur_pos = extent_key->objectid - offset;
5259 last_byte = extent_key->objectid + extent_key->offset;
5260 ret = btrfs_lookup_file_extent(NULL, root, path, reloc_inode->i_ino,
5261 cur_pos, 0);
5262 if (ret < 0)
5263 goto out;
5264 if (ret > 0) {
5265 ret = -ENOENT;
5266 goto out;
5267 }
5268
5269 while (1) {
5270 leaf = path->nodes[0];
5271 nritems = btrfs_header_nritems(leaf);
5272 if (path->slots[0] >= nritems) {
5273 ret = btrfs_next_leaf(root, path);
5274 if (ret < 0)
5275 goto out;
5276 if (ret > 0)
5277 break;
5278 leaf = path->nodes[0];
5279 }
5280
5281 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5282 if (found_key.offset != cur_pos ||
5283 found_key.type != BTRFS_EXTENT_DATA_KEY ||
5284 found_key.objectid != reloc_inode->i_ino)
5285 break;
5286
5287 fi = btrfs_item_ptr(leaf, path->slots[0],
5288 struct btrfs_file_extent_item);
5289 if (btrfs_file_extent_type(leaf, fi) !=
5290 BTRFS_FILE_EXTENT_REG ||
5291 btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
5292 break;
5293
5294 if (nr == max) {
5295 struct disk_extent *old = exts;
5296 max *= 2;
5297 exts = kzalloc(sizeof(*exts) * max, GFP_NOFS);
5298 memcpy(exts, old, sizeof(*exts) * nr);
5299 if (old != *extents)
5300 kfree(old);
5301 }
5302
5303 exts[nr].disk_bytenr =
5304 btrfs_file_extent_disk_bytenr(leaf, fi);
5305 exts[nr].disk_num_bytes =
5306 btrfs_file_extent_disk_num_bytes(leaf, fi);
5307 exts[nr].offset = btrfs_file_extent_offset(leaf, fi);
5308 exts[nr].num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
Chris Masonc8b97812008-10-29 14:49:59 -04005309 exts[nr].ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
5310 exts[nr].compression = btrfs_file_extent_compression(leaf, fi);
5311 exts[nr].encryption = btrfs_file_extent_encryption(leaf, fi);
5312 exts[nr].other_encoding = btrfs_file_extent_other_encoding(leaf,
5313 fi);
Yan Zhengd899e052008-10-30 14:25:28 -04005314 BUG_ON(exts[nr].offset > 0);
5315 BUG_ON(exts[nr].compression || exts[nr].encryption);
5316 BUG_ON(exts[nr].num_bytes != exts[nr].disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04005317
5318 cur_pos += exts[nr].num_bytes;
5319 nr++;
5320
5321 if (cur_pos + offset >= last_byte)
5322 break;
5323
5324 if (no_fragment) {
5325 ret = 1;
5326 goto out;
5327 }
5328 path->slots[0]++;
5329 }
5330
Yan Zheng1f80e4d2008-12-19 10:59:04 -05005331 BUG_ON(cur_pos + offset > last_byte);
Zheng Yan1a40e232008-09-26 10:09:34 -04005332 if (cur_pos + offset < last_byte) {
5333 ret = -ENOENT;
5334 goto out;
Chris Masonedbd8d42007-12-21 16:27:24 -05005335 }
5336 ret = 0;
5337out:
Zheng Yan1a40e232008-09-26 10:09:34 -04005338 btrfs_free_path(path);
5339 if (ret) {
5340 if (exts != *extents)
5341 kfree(exts);
5342 } else {
5343 *extents = exts;
5344 *nr_extents = nr;
5345 }
5346 return ret;
5347}
5348
Chris Masond3977122009-01-05 21:25:51 -05005349static noinline int replace_one_extent(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04005350 struct btrfs_root *root,
5351 struct btrfs_path *path,
5352 struct btrfs_key *extent_key,
5353 struct btrfs_key *leaf_key,
5354 struct btrfs_ref_path *ref_path,
5355 struct disk_extent *new_extents,
5356 int nr_extents)
5357{
5358 struct extent_buffer *leaf;
5359 struct btrfs_file_extent_item *fi;
5360 struct inode *inode = NULL;
5361 struct btrfs_key key;
5362 u64 lock_start = 0;
5363 u64 lock_end = 0;
5364 u64 num_bytes;
5365 u64 ext_offset;
Yan Zheng86288a12009-01-21 10:49:16 -05005366 u64 search_end = (u64)-1;
Zheng Yan1a40e232008-09-26 10:09:34 -04005367 u32 nritems;
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04005368 int nr_scaned = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04005369 int extent_locked = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04005370 int extent_type;
Zheng Yan1a40e232008-09-26 10:09:34 -04005371 int ret;
5372
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04005373 memcpy(&key, leaf_key, sizeof(key));
Zheng Yan1a40e232008-09-26 10:09:34 -04005374 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04005375 if (key.objectid < ref_path->owner_objectid ||
5376 (key.objectid == ref_path->owner_objectid &&
5377 key.type < BTRFS_EXTENT_DATA_KEY)) {
5378 key.objectid = ref_path->owner_objectid;
5379 key.type = BTRFS_EXTENT_DATA_KEY;
5380 key.offset = 0;
5381 }
Zheng Yan1a40e232008-09-26 10:09:34 -04005382 }
5383
5384 while (1) {
5385 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
5386 if (ret < 0)
5387 goto out;
5388
5389 leaf = path->nodes[0];
5390 nritems = btrfs_header_nritems(leaf);
5391next:
5392 if (extent_locked && ret > 0) {
5393 /*
5394 * the file extent item was modified by someone
5395 * before the extent got locked.
5396 */
Zheng Yan1a40e232008-09-26 10:09:34 -04005397 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
5398 lock_end, GFP_NOFS);
5399 extent_locked = 0;
5400 }
5401
5402 if (path->slots[0] >= nritems) {
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04005403 if (++nr_scaned > 2)
Zheng Yan1a40e232008-09-26 10:09:34 -04005404 break;
5405
5406 BUG_ON(extent_locked);
5407 ret = btrfs_next_leaf(root, path);
5408 if (ret < 0)
5409 goto out;
5410 if (ret > 0)
5411 break;
5412 leaf = path->nodes[0];
5413 nritems = btrfs_header_nritems(leaf);
5414 }
5415
5416 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
5417
5418 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
5419 if ((key.objectid > ref_path->owner_objectid) ||
5420 (key.objectid == ref_path->owner_objectid &&
5421 key.type > BTRFS_EXTENT_DATA_KEY) ||
Yan Zheng86288a12009-01-21 10:49:16 -05005422 key.offset >= search_end)
Zheng Yan1a40e232008-09-26 10:09:34 -04005423 break;
5424 }
5425
5426 if (inode && key.objectid != inode->i_ino) {
5427 BUG_ON(extent_locked);
5428 btrfs_release_path(root, path);
5429 mutex_unlock(&inode->i_mutex);
5430 iput(inode);
5431 inode = NULL;
5432 continue;
5433 }
5434
5435 if (key.type != BTRFS_EXTENT_DATA_KEY) {
5436 path->slots[0]++;
5437 ret = 1;
5438 goto next;
5439 }
5440 fi = btrfs_item_ptr(leaf, path->slots[0],
5441 struct btrfs_file_extent_item);
Yan Zhengd899e052008-10-30 14:25:28 -04005442 extent_type = btrfs_file_extent_type(leaf, fi);
5443 if ((extent_type != BTRFS_FILE_EXTENT_REG &&
5444 extent_type != BTRFS_FILE_EXTENT_PREALLOC) ||
Zheng Yan1a40e232008-09-26 10:09:34 -04005445 (btrfs_file_extent_disk_bytenr(leaf, fi) !=
5446 extent_key->objectid)) {
5447 path->slots[0]++;
5448 ret = 1;
5449 goto next;
5450 }
5451
5452 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
5453 ext_offset = btrfs_file_extent_offset(leaf, fi);
5454
Yan Zheng86288a12009-01-21 10:49:16 -05005455 if (search_end == (u64)-1) {
5456 search_end = key.offset - ext_offset +
5457 btrfs_file_extent_ram_bytes(leaf, fi);
5458 }
Zheng Yan1a40e232008-09-26 10:09:34 -04005459
5460 if (!extent_locked) {
5461 lock_start = key.offset;
5462 lock_end = lock_start + num_bytes - 1;
5463 } else {
Yan Zheng66435582008-10-30 14:19:50 -04005464 if (lock_start > key.offset ||
5465 lock_end + 1 < key.offset + num_bytes) {
5466 unlock_extent(&BTRFS_I(inode)->io_tree,
5467 lock_start, lock_end, GFP_NOFS);
5468 extent_locked = 0;
5469 }
Zheng Yan1a40e232008-09-26 10:09:34 -04005470 }
5471
5472 if (!inode) {
5473 btrfs_release_path(root, path);
5474
5475 inode = btrfs_iget_locked(root->fs_info->sb,
5476 key.objectid, root);
5477 if (inode->i_state & I_NEW) {
5478 BTRFS_I(inode)->root = root;
5479 BTRFS_I(inode)->location.objectid =
5480 key.objectid;
5481 BTRFS_I(inode)->location.type =
5482 BTRFS_INODE_ITEM_KEY;
5483 BTRFS_I(inode)->location.offset = 0;
5484 btrfs_read_locked_inode(inode);
5485 unlock_new_inode(inode);
5486 }
5487 /*
5488 * some code call btrfs_commit_transaction while
5489 * holding the i_mutex, so we can't use mutex_lock
5490 * here.
5491 */
5492 if (is_bad_inode(inode) ||
5493 !mutex_trylock(&inode->i_mutex)) {
5494 iput(inode);
5495 inode = NULL;
5496 key.offset = (u64)-1;
5497 goto skip;
5498 }
5499 }
5500
5501 if (!extent_locked) {
5502 struct btrfs_ordered_extent *ordered;
5503
5504 btrfs_release_path(root, path);
5505
5506 lock_extent(&BTRFS_I(inode)->io_tree, lock_start,
5507 lock_end, GFP_NOFS);
5508 ordered = btrfs_lookup_first_ordered_extent(inode,
5509 lock_end);
5510 if (ordered &&
5511 ordered->file_offset <= lock_end &&
5512 ordered->file_offset + ordered->len > lock_start) {
5513 unlock_extent(&BTRFS_I(inode)->io_tree,
5514 lock_start, lock_end, GFP_NOFS);
5515 btrfs_start_ordered_extent(inode, ordered, 1);
5516 btrfs_put_ordered_extent(ordered);
5517 key.offset += num_bytes;
5518 goto skip;
5519 }
5520 if (ordered)
5521 btrfs_put_ordered_extent(ordered);
5522
Zheng Yan1a40e232008-09-26 10:09:34 -04005523 extent_locked = 1;
5524 continue;
5525 }
5526
5527 if (nr_extents == 1) {
5528 /* update extent pointer in place */
Zheng Yan1a40e232008-09-26 10:09:34 -04005529 btrfs_set_file_extent_disk_bytenr(leaf, fi,
5530 new_extents[0].disk_bytenr);
5531 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
5532 new_extents[0].disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04005533 btrfs_mark_buffer_dirty(leaf);
5534
5535 btrfs_drop_extent_cache(inode, key.offset,
5536 key.offset + num_bytes - 1, 0);
5537
5538 ret = btrfs_inc_extent_ref(trans, root,
5539 new_extents[0].disk_bytenr,
5540 new_extents[0].disk_num_bytes,
5541 leaf->start,
5542 root->root_key.objectid,
5543 trans->transid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04005544 key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04005545 BUG_ON(ret);
5546
5547 ret = btrfs_free_extent(trans, root,
5548 extent_key->objectid,
5549 extent_key->offset,
5550 leaf->start,
5551 btrfs_header_owner(leaf),
5552 btrfs_header_generation(leaf),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04005553 key.objectid, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04005554 BUG_ON(ret);
5555
5556 btrfs_release_path(root, path);
5557 key.offset += num_bytes;
5558 } else {
Yan Zhengd899e052008-10-30 14:25:28 -04005559 BUG_ON(1);
5560#if 0
Zheng Yan1a40e232008-09-26 10:09:34 -04005561 u64 alloc_hint;
5562 u64 extent_len;
5563 int i;
5564 /*
5565 * drop old extent pointer at first, then insert the
5566 * new pointers one bye one
5567 */
5568 btrfs_release_path(root, path);
5569 ret = btrfs_drop_extents(trans, root, inode, key.offset,
5570 key.offset + num_bytes,
5571 key.offset, &alloc_hint);
5572 BUG_ON(ret);
5573
5574 for (i = 0; i < nr_extents; i++) {
5575 if (ext_offset >= new_extents[i].num_bytes) {
5576 ext_offset -= new_extents[i].num_bytes;
5577 continue;
5578 }
5579 extent_len = min(new_extents[i].num_bytes -
5580 ext_offset, num_bytes);
5581
5582 ret = btrfs_insert_empty_item(trans, root,
5583 path, &key,
5584 sizeof(*fi));
5585 BUG_ON(ret);
5586
5587 leaf = path->nodes[0];
5588 fi = btrfs_item_ptr(leaf, path->slots[0],
5589 struct btrfs_file_extent_item);
5590 btrfs_set_file_extent_generation(leaf, fi,
5591 trans->transid);
5592 btrfs_set_file_extent_type(leaf, fi,
5593 BTRFS_FILE_EXTENT_REG);
5594 btrfs_set_file_extent_disk_bytenr(leaf, fi,
5595 new_extents[i].disk_bytenr);
5596 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
5597 new_extents[i].disk_num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -04005598 btrfs_set_file_extent_ram_bytes(leaf, fi,
5599 new_extents[i].ram_bytes);
5600
5601 btrfs_set_file_extent_compression(leaf, fi,
5602 new_extents[i].compression);
5603 btrfs_set_file_extent_encryption(leaf, fi,
5604 new_extents[i].encryption);
5605 btrfs_set_file_extent_other_encoding(leaf, fi,
5606 new_extents[i].other_encoding);
5607
Zheng Yan1a40e232008-09-26 10:09:34 -04005608 btrfs_set_file_extent_num_bytes(leaf, fi,
5609 extent_len);
5610 ext_offset += new_extents[i].offset;
5611 btrfs_set_file_extent_offset(leaf, fi,
5612 ext_offset);
5613 btrfs_mark_buffer_dirty(leaf);
5614
5615 btrfs_drop_extent_cache(inode, key.offset,
5616 key.offset + extent_len - 1, 0);
5617
5618 ret = btrfs_inc_extent_ref(trans, root,
5619 new_extents[i].disk_bytenr,
5620 new_extents[i].disk_num_bytes,
5621 leaf->start,
5622 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04005623 trans->transid, key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04005624 BUG_ON(ret);
5625 btrfs_release_path(root, path);
5626
Yan Zhenga76a3cd2008-10-09 11:46:29 -04005627 inode_add_bytes(inode, extent_len);
Zheng Yan1a40e232008-09-26 10:09:34 -04005628
5629 ext_offset = 0;
5630 num_bytes -= extent_len;
5631 key.offset += extent_len;
5632
5633 if (num_bytes == 0)
5634 break;
5635 }
5636 BUG_ON(i >= nr_extents);
Yan Zhengd899e052008-10-30 14:25:28 -04005637#endif
Zheng Yan1a40e232008-09-26 10:09:34 -04005638 }
5639
5640 if (extent_locked) {
Zheng Yan1a40e232008-09-26 10:09:34 -04005641 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
5642 lock_end, GFP_NOFS);
5643 extent_locked = 0;
5644 }
5645skip:
5646 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS &&
Yan Zheng86288a12009-01-21 10:49:16 -05005647 key.offset >= search_end)
Zheng Yan1a40e232008-09-26 10:09:34 -04005648 break;
5649
5650 cond_resched();
5651 }
5652 ret = 0;
5653out:
5654 btrfs_release_path(root, path);
5655 if (inode) {
5656 mutex_unlock(&inode->i_mutex);
5657 if (extent_locked) {
Zheng Yan1a40e232008-09-26 10:09:34 -04005658 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
5659 lock_end, GFP_NOFS);
5660 }
5661 iput(inode);
5662 }
5663 return ret;
5664}
5665
Zheng Yan1a40e232008-09-26 10:09:34 -04005666int btrfs_reloc_tree_cache_ref(struct btrfs_trans_handle *trans,
5667 struct btrfs_root *root,
5668 struct extent_buffer *buf, u64 orig_start)
5669{
5670 int level;
5671 int ret;
5672
5673 BUG_ON(btrfs_header_generation(buf) != trans->transid);
5674 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
5675
5676 level = btrfs_header_level(buf);
5677 if (level == 0) {
5678 struct btrfs_leaf_ref *ref;
5679 struct btrfs_leaf_ref *orig_ref;
5680
5681 orig_ref = btrfs_lookup_leaf_ref(root, orig_start);
5682 if (!orig_ref)
5683 return -ENOENT;
5684
5685 ref = btrfs_alloc_leaf_ref(root, orig_ref->nritems);
5686 if (!ref) {
5687 btrfs_free_leaf_ref(root, orig_ref);
5688 return -ENOMEM;
5689 }
5690
5691 ref->nritems = orig_ref->nritems;
5692 memcpy(ref->extents, orig_ref->extents,
5693 sizeof(ref->extents[0]) * ref->nritems);
5694
5695 btrfs_free_leaf_ref(root, orig_ref);
5696
5697 ref->root_gen = trans->transid;
5698 ref->bytenr = buf->start;
5699 ref->owner = btrfs_header_owner(buf);
5700 ref->generation = btrfs_header_generation(buf);
Chris Masonbd56b302009-02-04 09:27:02 -05005701
Zheng Yan1a40e232008-09-26 10:09:34 -04005702 ret = btrfs_add_leaf_ref(root, ref, 0);
5703 WARN_ON(ret);
5704 btrfs_free_leaf_ref(root, ref);
5705 }
5706 return 0;
5707}
5708
Chris Masond3977122009-01-05 21:25:51 -05005709static noinline int invalidate_extent_cache(struct btrfs_root *root,
Zheng Yan1a40e232008-09-26 10:09:34 -04005710 struct extent_buffer *leaf,
5711 struct btrfs_block_group_cache *group,
5712 struct btrfs_root *target_root)
5713{
5714 struct btrfs_key key;
5715 struct inode *inode = NULL;
5716 struct btrfs_file_extent_item *fi;
5717 u64 num_bytes;
5718 u64 skip_objectid = 0;
5719 u32 nritems;
5720 u32 i;
5721
5722 nritems = btrfs_header_nritems(leaf);
5723 for (i = 0; i < nritems; i++) {
5724 btrfs_item_key_to_cpu(leaf, &key, i);
5725 if (key.objectid == skip_objectid ||
5726 key.type != BTRFS_EXTENT_DATA_KEY)
5727 continue;
5728 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
5729 if (btrfs_file_extent_type(leaf, fi) ==
5730 BTRFS_FILE_EXTENT_INLINE)
5731 continue;
5732 if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
5733 continue;
5734 if (!inode || inode->i_ino != key.objectid) {
5735 iput(inode);
5736 inode = btrfs_ilookup(target_root->fs_info->sb,
5737 key.objectid, target_root, 1);
5738 }
5739 if (!inode) {
5740 skip_objectid = key.objectid;
5741 continue;
5742 }
5743 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
5744
5745 lock_extent(&BTRFS_I(inode)->io_tree, key.offset,
5746 key.offset + num_bytes - 1, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04005747 btrfs_drop_extent_cache(inode, key.offset,
5748 key.offset + num_bytes - 1, 1);
Zheng Yan1a40e232008-09-26 10:09:34 -04005749 unlock_extent(&BTRFS_I(inode)->io_tree, key.offset,
5750 key.offset + num_bytes - 1, GFP_NOFS);
5751 cond_resched();
5752 }
5753 iput(inode);
5754 return 0;
5755}
5756
Chris Masond3977122009-01-05 21:25:51 -05005757static noinline int replace_extents_in_leaf(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04005758 struct btrfs_root *root,
5759 struct extent_buffer *leaf,
5760 struct btrfs_block_group_cache *group,
5761 struct inode *reloc_inode)
5762{
5763 struct btrfs_key key;
5764 struct btrfs_key extent_key;
5765 struct btrfs_file_extent_item *fi;
5766 struct btrfs_leaf_ref *ref;
5767 struct disk_extent *new_extent;
5768 u64 bytenr;
5769 u64 num_bytes;
5770 u32 nritems;
5771 u32 i;
5772 int ext_index;
5773 int nr_extent;
5774 int ret;
5775
5776 new_extent = kmalloc(sizeof(*new_extent), GFP_NOFS);
5777 BUG_ON(!new_extent);
5778
5779 ref = btrfs_lookup_leaf_ref(root, leaf->start);
5780 BUG_ON(!ref);
5781
5782 ext_index = -1;
5783 nritems = btrfs_header_nritems(leaf);
5784 for (i = 0; i < nritems; i++) {
5785 btrfs_item_key_to_cpu(leaf, &key, i);
5786 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
5787 continue;
5788 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
5789 if (btrfs_file_extent_type(leaf, fi) ==
5790 BTRFS_FILE_EXTENT_INLINE)
5791 continue;
5792 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
5793 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
5794 if (bytenr == 0)
5795 continue;
5796
5797 ext_index++;
5798 if (bytenr >= group->key.objectid + group->key.offset ||
5799 bytenr + num_bytes <= group->key.objectid)
5800 continue;
5801
5802 extent_key.objectid = bytenr;
5803 extent_key.offset = num_bytes;
5804 extent_key.type = BTRFS_EXTENT_ITEM_KEY;
5805 nr_extent = 1;
5806 ret = get_new_locations(reloc_inode, &extent_key,
5807 group->key.objectid, 1,
5808 &new_extent, &nr_extent);
5809 if (ret > 0)
5810 continue;
5811 BUG_ON(ret < 0);
5812
5813 BUG_ON(ref->extents[ext_index].bytenr != bytenr);
5814 BUG_ON(ref->extents[ext_index].num_bytes != num_bytes);
5815 ref->extents[ext_index].bytenr = new_extent->disk_bytenr;
5816 ref->extents[ext_index].num_bytes = new_extent->disk_num_bytes;
5817
Zheng Yan1a40e232008-09-26 10:09:34 -04005818 btrfs_set_file_extent_disk_bytenr(leaf, fi,
5819 new_extent->disk_bytenr);
5820 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
5821 new_extent->disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04005822 btrfs_mark_buffer_dirty(leaf);
5823
5824 ret = btrfs_inc_extent_ref(trans, root,
5825 new_extent->disk_bytenr,
5826 new_extent->disk_num_bytes,
5827 leaf->start,
5828 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04005829 trans->transid, key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04005830 BUG_ON(ret);
Chris Mason56bec292009-03-13 10:10:06 -04005831
Zheng Yan1a40e232008-09-26 10:09:34 -04005832 ret = btrfs_free_extent(trans, root,
5833 bytenr, num_bytes, leaf->start,
5834 btrfs_header_owner(leaf),
5835 btrfs_header_generation(leaf),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04005836 key.objectid, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04005837 BUG_ON(ret);
5838 cond_resched();
5839 }
5840 kfree(new_extent);
5841 BUG_ON(ext_index + 1 != ref->nritems);
5842 btrfs_free_leaf_ref(root, ref);
5843 return 0;
5844}
5845
Yan Zhengf82d02d2008-10-29 14:49:05 -04005846int btrfs_free_reloc_root(struct btrfs_trans_handle *trans,
5847 struct btrfs_root *root)
Zheng Yan1a40e232008-09-26 10:09:34 -04005848{
5849 struct btrfs_root *reloc_root;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005850 int ret;
Zheng Yan1a40e232008-09-26 10:09:34 -04005851
5852 if (root->reloc_root) {
5853 reloc_root = root->reloc_root;
5854 root->reloc_root = NULL;
5855 list_add(&reloc_root->dead_list,
5856 &root->fs_info->dead_reloc_roots);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005857
5858 btrfs_set_root_bytenr(&reloc_root->root_item,
5859 reloc_root->node->start);
5860 btrfs_set_root_level(&root->root_item,
5861 btrfs_header_level(reloc_root->node));
5862 memset(&reloc_root->root_item.drop_progress, 0,
5863 sizeof(struct btrfs_disk_key));
5864 reloc_root->root_item.drop_level = 0;
5865
5866 ret = btrfs_update_root(trans, root->fs_info->tree_root,
5867 &reloc_root->root_key,
5868 &reloc_root->root_item);
5869 BUG_ON(ret);
Zheng Yan1a40e232008-09-26 10:09:34 -04005870 }
5871 return 0;
5872}
5873
5874int btrfs_drop_dead_reloc_roots(struct btrfs_root *root)
5875{
5876 struct btrfs_trans_handle *trans;
5877 struct btrfs_root *reloc_root;
5878 struct btrfs_root *prev_root = NULL;
5879 struct list_head dead_roots;
5880 int ret;
5881 unsigned long nr;
5882
5883 INIT_LIST_HEAD(&dead_roots);
5884 list_splice_init(&root->fs_info->dead_reloc_roots, &dead_roots);
5885
5886 while (!list_empty(&dead_roots)) {
5887 reloc_root = list_entry(dead_roots.prev,
5888 struct btrfs_root, dead_list);
5889 list_del_init(&reloc_root->dead_list);
5890
5891 BUG_ON(reloc_root->commit_root != NULL);
5892 while (1) {
5893 trans = btrfs_join_transaction(root, 1);
5894 BUG_ON(!trans);
5895
5896 mutex_lock(&root->fs_info->drop_mutex);
5897 ret = btrfs_drop_snapshot(trans, reloc_root);
5898 if (ret != -EAGAIN)
5899 break;
5900 mutex_unlock(&root->fs_info->drop_mutex);
5901
5902 nr = trans->blocks_used;
5903 ret = btrfs_end_transaction(trans, root);
5904 BUG_ON(ret);
5905 btrfs_btree_balance_dirty(root, nr);
5906 }
5907
5908 free_extent_buffer(reloc_root->node);
5909
5910 ret = btrfs_del_root(trans, root->fs_info->tree_root,
5911 &reloc_root->root_key);
5912 BUG_ON(ret);
5913 mutex_unlock(&root->fs_info->drop_mutex);
5914
5915 nr = trans->blocks_used;
5916 ret = btrfs_end_transaction(trans, root);
5917 BUG_ON(ret);
5918 btrfs_btree_balance_dirty(root, nr);
5919
5920 kfree(prev_root);
5921 prev_root = reloc_root;
5922 }
5923 if (prev_root) {
5924 btrfs_remove_leaf_refs(prev_root, (u64)-1, 0);
5925 kfree(prev_root);
5926 }
5927 return 0;
5928}
5929
5930int btrfs_add_dead_reloc_root(struct btrfs_root *root)
5931{
5932 list_add(&root->dead_list, &root->fs_info->dead_reloc_roots);
5933 return 0;
5934}
5935
5936int btrfs_cleanup_reloc_trees(struct btrfs_root *root)
5937{
5938 struct btrfs_root *reloc_root;
5939 struct btrfs_trans_handle *trans;
5940 struct btrfs_key location;
5941 int found;
5942 int ret;
5943
5944 mutex_lock(&root->fs_info->tree_reloc_mutex);
5945 ret = btrfs_find_dead_roots(root, BTRFS_TREE_RELOC_OBJECTID, NULL);
5946 BUG_ON(ret);
5947 found = !list_empty(&root->fs_info->dead_reloc_roots);
5948 mutex_unlock(&root->fs_info->tree_reloc_mutex);
5949
5950 if (found) {
5951 trans = btrfs_start_transaction(root, 1);
5952 BUG_ON(!trans);
5953 ret = btrfs_commit_transaction(trans, root);
5954 BUG_ON(ret);
5955 }
5956
5957 location.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
5958 location.offset = (u64)-1;
5959 location.type = BTRFS_ROOT_ITEM_KEY;
5960
5961 reloc_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
5962 BUG_ON(!reloc_root);
5963 btrfs_orphan_cleanup(reloc_root);
5964 return 0;
5965}
5966
Chris Masond3977122009-01-05 21:25:51 -05005967static noinline int init_reloc_tree(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04005968 struct btrfs_root *root)
5969{
5970 struct btrfs_root *reloc_root;
5971 struct extent_buffer *eb;
5972 struct btrfs_root_item *root_item;
5973 struct btrfs_key root_key;
5974 int ret;
5975
5976 BUG_ON(!root->ref_cows);
5977 if (root->reloc_root)
5978 return 0;
5979
5980 root_item = kmalloc(sizeof(*root_item), GFP_NOFS);
5981 BUG_ON(!root_item);
5982
5983 ret = btrfs_copy_root(trans, root, root->commit_root,
5984 &eb, BTRFS_TREE_RELOC_OBJECTID);
5985 BUG_ON(ret);
5986
5987 root_key.objectid = BTRFS_TREE_RELOC_OBJECTID;
5988 root_key.offset = root->root_key.objectid;
5989 root_key.type = BTRFS_ROOT_ITEM_KEY;
5990
5991 memcpy(root_item, &root->root_item, sizeof(root_item));
5992 btrfs_set_root_refs(root_item, 0);
5993 btrfs_set_root_bytenr(root_item, eb->start);
5994 btrfs_set_root_level(root_item, btrfs_header_level(eb));
Yan Zheng84234f32008-10-29 14:49:05 -04005995 btrfs_set_root_generation(root_item, trans->transid);
Zheng Yan1a40e232008-09-26 10:09:34 -04005996
5997 btrfs_tree_unlock(eb);
5998 free_extent_buffer(eb);
5999
6000 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
6001 &root_key, root_item);
6002 BUG_ON(ret);
6003 kfree(root_item);
6004
6005 reloc_root = btrfs_read_fs_root_no_radix(root->fs_info->tree_root,
6006 &root_key);
6007 BUG_ON(!reloc_root);
6008 reloc_root->last_trans = trans->transid;
6009 reloc_root->commit_root = NULL;
6010 reloc_root->ref_tree = &root->fs_info->reloc_ref_tree;
6011
6012 root->reloc_root = reloc_root;
6013 return 0;
6014}
6015
6016/*
6017 * Core function of space balance.
6018 *
6019 * The idea is using reloc trees to relocate tree blocks in reference
Yan Zhengf82d02d2008-10-29 14:49:05 -04006020 * counted roots. There is one reloc tree for each subvol, and all
6021 * reloc trees share same root key objectid. Reloc trees are snapshots
6022 * of the latest committed roots of subvols (root->commit_root).
6023 *
6024 * To relocate a tree block referenced by a subvol, there are two steps.
6025 * COW the block through subvol's reloc tree, then update block pointer
6026 * in the subvol to point to the new block. Since all reloc trees share
6027 * same root key objectid, doing special handing for tree blocks owned
6028 * by them is easy. Once a tree block has been COWed in one reloc tree,
6029 * we can use the resulting new block directly when the same block is
6030 * required to COW again through other reloc trees. By this way, relocated
6031 * tree blocks are shared between reloc trees, so they are also shared
6032 * between subvols.
Zheng Yan1a40e232008-09-26 10:09:34 -04006033 */
Chris Masond3977122009-01-05 21:25:51 -05006034static noinline int relocate_one_path(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04006035 struct btrfs_root *root,
6036 struct btrfs_path *path,
6037 struct btrfs_key *first_key,
6038 struct btrfs_ref_path *ref_path,
6039 struct btrfs_block_group_cache *group,
6040 struct inode *reloc_inode)
6041{
6042 struct btrfs_root *reloc_root;
6043 struct extent_buffer *eb = NULL;
6044 struct btrfs_key *keys;
6045 u64 *nodes;
6046 int level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006047 int shared_level;
Zheng Yan1a40e232008-09-26 10:09:34 -04006048 int lowest_level = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04006049 int ret;
6050
6051 if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
6052 lowest_level = ref_path->owner_objectid;
6053
Yan Zhengf82d02d2008-10-29 14:49:05 -04006054 if (!root->ref_cows) {
Zheng Yan1a40e232008-09-26 10:09:34 -04006055 path->lowest_level = lowest_level;
6056 ret = btrfs_search_slot(trans, root, first_key, path, 0, 1);
6057 BUG_ON(ret < 0);
6058 path->lowest_level = 0;
6059 btrfs_release_path(root, path);
6060 return 0;
6061 }
6062
Zheng Yan1a40e232008-09-26 10:09:34 -04006063 mutex_lock(&root->fs_info->tree_reloc_mutex);
6064 ret = init_reloc_tree(trans, root);
6065 BUG_ON(ret);
6066 reloc_root = root->reloc_root;
6067
Yan Zhengf82d02d2008-10-29 14:49:05 -04006068 shared_level = ref_path->shared_level;
6069 ref_path->shared_level = BTRFS_MAX_LEVEL - 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04006070
Yan Zhengf82d02d2008-10-29 14:49:05 -04006071 keys = ref_path->node_keys;
6072 nodes = ref_path->new_nodes;
6073 memset(&keys[shared_level + 1], 0,
6074 sizeof(*keys) * (BTRFS_MAX_LEVEL - shared_level - 1));
6075 memset(&nodes[shared_level + 1], 0,
6076 sizeof(*nodes) * (BTRFS_MAX_LEVEL - shared_level - 1));
Zheng Yan1a40e232008-09-26 10:09:34 -04006077
Yan Zhengf82d02d2008-10-29 14:49:05 -04006078 if (nodes[lowest_level] == 0) {
6079 path->lowest_level = lowest_level;
6080 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
6081 0, 1);
6082 BUG_ON(ret);
6083 for (level = lowest_level; level < BTRFS_MAX_LEVEL; level++) {
6084 eb = path->nodes[level];
6085 if (!eb || eb == reloc_root->node)
6086 break;
6087 nodes[level] = eb->start;
6088 if (level == 0)
6089 btrfs_item_key_to_cpu(eb, &keys[level], 0);
6090 else
6091 btrfs_node_key_to_cpu(eb, &keys[level], 0);
6092 }
Yan Zheng2b820322008-11-17 21:11:30 -05006093 if (nodes[0] &&
6094 ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04006095 eb = path->nodes[0];
6096 ret = replace_extents_in_leaf(trans, reloc_root, eb,
6097 group, reloc_inode);
6098 BUG_ON(ret);
6099 }
6100 btrfs_release_path(reloc_root, path);
6101 } else {
Zheng Yan1a40e232008-09-26 10:09:34 -04006102 ret = btrfs_merge_path(trans, reloc_root, keys, nodes,
Yan Zhengf82d02d2008-10-29 14:49:05 -04006103 lowest_level);
Zheng Yan1a40e232008-09-26 10:09:34 -04006104 BUG_ON(ret);
6105 }
6106
Zheng Yan1a40e232008-09-26 10:09:34 -04006107 /*
6108 * replace tree blocks in the fs tree with tree blocks in
6109 * the reloc tree.
6110 */
6111 ret = btrfs_merge_path(trans, root, keys, nodes, lowest_level);
6112 BUG_ON(ret < 0);
6113
6114 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04006115 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
6116 0, 0);
6117 BUG_ON(ret);
6118 extent_buffer_get(path->nodes[0]);
6119 eb = path->nodes[0];
6120 btrfs_release_path(reloc_root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04006121 ret = invalidate_extent_cache(reloc_root, eb, group, root);
6122 BUG_ON(ret);
6123 free_extent_buffer(eb);
6124 }
Zheng Yan1a40e232008-09-26 10:09:34 -04006125
Yan Zhengf82d02d2008-10-29 14:49:05 -04006126 mutex_unlock(&root->fs_info->tree_reloc_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04006127 path->lowest_level = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04006128 return 0;
6129}
6130
Chris Masond3977122009-01-05 21:25:51 -05006131static noinline int relocate_tree_block(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04006132 struct btrfs_root *root,
6133 struct btrfs_path *path,
6134 struct btrfs_key *first_key,
6135 struct btrfs_ref_path *ref_path)
6136{
6137 int ret;
Zheng Yan1a40e232008-09-26 10:09:34 -04006138
6139 ret = relocate_one_path(trans, root, path, first_key,
6140 ref_path, NULL, NULL);
6141 BUG_ON(ret);
6142
Zheng Yan1a40e232008-09-26 10:09:34 -04006143 return 0;
6144}
6145
Chris Masond3977122009-01-05 21:25:51 -05006146static noinline int del_extent_zero(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04006147 struct btrfs_root *extent_root,
6148 struct btrfs_path *path,
6149 struct btrfs_key *extent_key)
6150{
6151 int ret;
6152
Zheng Yan1a40e232008-09-26 10:09:34 -04006153 ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1);
6154 if (ret)
6155 goto out;
6156 ret = btrfs_del_item(trans, extent_root, path);
6157out:
Chris Masonedbd8d42007-12-21 16:27:24 -05006158 btrfs_release_path(extent_root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04006159 return ret;
6160}
6161
Chris Masond3977122009-01-05 21:25:51 -05006162static noinline struct btrfs_root *read_ref_root(struct btrfs_fs_info *fs_info,
Zheng Yan1a40e232008-09-26 10:09:34 -04006163 struct btrfs_ref_path *ref_path)
6164{
6165 struct btrfs_key root_key;
6166
6167 root_key.objectid = ref_path->root_objectid;
6168 root_key.type = BTRFS_ROOT_ITEM_KEY;
6169 if (is_cowonly_root(ref_path->root_objectid))
6170 root_key.offset = 0;
6171 else
6172 root_key.offset = (u64)-1;
6173
6174 return btrfs_read_fs_root_no_name(fs_info, &root_key);
6175}
6176
Chris Masond3977122009-01-05 21:25:51 -05006177static noinline int relocate_one_extent(struct btrfs_root *extent_root,
Zheng Yan1a40e232008-09-26 10:09:34 -04006178 struct btrfs_path *path,
6179 struct btrfs_key *extent_key,
6180 struct btrfs_block_group_cache *group,
6181 struct inode *reloc_inode, int pass)
6182{
6183 struct btrfs_trans_handle *trans;
6184 struct btrfs_root *found_root;
6185 struct btrfs_ref_path *ref_path = NULL;
6186 struct disk_extent *new_extents = NULL;
6187 int nr_extents = 0;
6188 int loops;
6189 int ret;
6190 int level;
6191 struct btrfs_key first_key;
6192 u64 prev_block = 0;
6193
Zheng Yan1a40e232008-09-26 10:09:34 -04006194
6195 trans = btrfs_start_transaction(extent_root, 1);
6196 BUG_ON(!trans);
6197
6198 if (extent_key->objectid == 0) {
6199 ret = del_extent_zero(trans, extent_root, path, extent_key);
6200 goto out;
6201 }
6202
6203 ref_path = kmalloc(sizeof(*ref_path), GFP_NOFS);
6204 if (!ref_path) {
Chris Masond3977122009-01-05 21:25:51 -05006205 ret = -ENOMEM;
6206 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04006207 }
6208
6209 for (loops = 0; ; loops++) {
6210 if (loops == 0) {
6211 ret = btrfs_first_ref_path(trans, extent_root, ref_path,
6212 extent_key->objectid);
6213 } else {
6214 ret = btrfs_next_ref_path(trans, extent_root, ref_path);
6215 }
6216 if (ret < 0)
6217 goto out;
6218 if (ret > 0)
6219 break;
6220
6221 if (ref_path->root_objectid == BTRFS_TREE_LOG_OBJECTID ||
6222 ref_path->root_objectid == BTRFS_TREE_RELOC_OBJECTID)
6223 continue;
6224
6225 found_root = read_ref_root(extent_root->fs_info, ref_path);
6226 BUG_ON(!found_root);
6227 /*
6228 * for reference counted tree, only process reference paths
6229 * rooted at the latest committed root.
6230 */
6231 if (found_root->ref_cows &&
6232 ref_path->root_generation != found_root->root_key.offset)
6233 continue;
6234
6235 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
6236 if (pass == 0) {
6237 /*
6238 * copy data extents to new locations
6239 */
6240 u64 group_start = group->key.objectid;
6241 ret = relocate_data_extent(reloc_inode,
6242 extent_key,
6243 group_start);
6244 if (ret < 0)
6245 goto out;
6246 break;
6247 }
6248 level = 0;
6249 } else {
6250 level = ref_path->owner_objectid;
6251 }
6252
6253 if (prev_block != ref_path->nodes[level]) {
6254 struct extent_buffer *eb;
6255 u64 block_start = ref_path->nodes[level];
6256 u64 block_size = btrfs_level_size(found_root, level);
6257
6258 eb = read_tree_block(found_root, block_start,
6259 block_size, 0);
6260 btrfs_tree_lock(eb);
6261 BUG_ON(level != btrfs_header_level(eb));
6262
6263 if (level == 0)
6264 btrfs_item_key_to_cpu(eb, &first_key, 0);
6265 else
6266 btrfs_node_key_to_cpu(eb, &first_key, 0);
6267
6268 btrfs_tree_unlock(eb);
6269 free_extent_buffer(eb);
6270 prev_block = block_start;
6271 }
6272
Yan Zheng24562422009-02-12 14:14:53 -05006273 mutex_lock(&extent_root->fs_info->trans_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05006274 btrfs_record_root_in_trans(found_root);
Yan Zheng24562422009-02-12 14:14:53 -05006275 mutex_unlock(&extent_root->fs_info->trans_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05006276 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
6277 /*
6278 * try to update data extent references while
6279 * keeping metadata shared between snapshots.
6280 */
6281 if (pass == 1) {
6282 ret = relocate_one_path(trans, found_root,
6283 path, &first_key, ref_path,
6284 group, reloc_inode);
6285 if (ret < 0)
6286 goto out;
6287 continue;
6288 }
Zheng Yan1a40e232008-09-26 10:09:34 -04006289 /*
6290 * use fallback method to process the remaining
6291 * references.
6292 */
6293 if (!new_extents) {
6294 u64 group_start = group->key.objectid;
Yan Zhengd899e052008-10-30 14:25:28 -04006295 new_extents = kmalloc(sizeof(*new_extents),
6296 GFP_NOFS);
6297 nr_extents = 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04006298 ret = get_new_locations(reloc_inode,
6299 extent_key,
Yan Zhengd899e052008-10-30 14:25:28 -04006300 group_start, 1,
Zheng Yan1a40e232008-09-26 10:09:34 -04006301 &new_extents,
6302 &nr_extents);
Yan Zhengd899e052008-10-30 14:25:28 -04006303 if (ret)
Zheng Yan1a40e232008-09-26 10:09:34 -04006304 goto out;
6305 }
Zheng Yan1a40e232008-09-26 10:09:34 -04006306 ret = replace_one_extent(trans, found_root,
6307 path, extent_key,
6308 &first_key, ref_path,
6309 new_extents, nr_extents);
Yan Zhenge4404d62008-12-12 10:03:26 -05006310 } else {
Zheng Yan1a40e232008-09-26 10:09:34 -04006311 ret = relocate_tree_block(trans, found_root, path,
6312 &first_key, ref_path);
Zheng Yan1a40e232008-09-26 10:09:34 -04006313 }
6314 if (ret < 0)
6315 goto out;
6316 }
6317 ret = 0;
6318out:
6319 btrfs_end_transaction(trans, extent_root);
6320 kfree(new_extents);
6321 kfree(ref_path);
Chris Masonedbd8d42007-12-21 16:27:24 -05006322 return ret;
6323}
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006324#endif
Chris Masonedbd8d42007-12-21 16:27:24 -05006325
Chris Masonec44a352008-04-28 15:29:52 -04006326static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
6327{
6328 u64 num_devices;
6329 u64 stripped = BTRFS_BLOCK_GROUP_RAID0 |
6330 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
6331
Yan Zheng2b820322008-11-17 21:11:30 -05006332 num_devices = root->fs_info->fs_devices->rw_devices;
Chris Masonec44a352008-04-28 15:29:52 -04006333 if (num_devices == 1) {
6334 stripped |= BTRFS_BLOCK_GROUP_DUP;
6335 stripped = flags & ~stripped;
6336
6337 /* turn raid0 into single device chunks */
6338 if (flags & BTRFS_BLOCK_GROUP_RAID0)
6339 return stripped;
6340
6341 /* turn mirroring into duplication */
6342 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
6343 BTRFS_BLOCK_GROUP_RAID10))
6344 return stripped | BTRFS_BLOCK_GROUP_DUP;
6345 return flags;
6346 } else {
6347 /* they already had raid on here, just return */
Chris Masonec44a352008-04-28 15:29:52 -04006348 if (flags & stripped)
6349 return flags;
6350
6351 stripped |= BTRFS_BLOCK_GROUP_DUP;
6352 stripped = flags & ~stripped;
6353
6354 /* switch duplicated blocks with raid1 */
6355 if (flags & BTRFS_BLOCK_GROUP_DUP)
6356 return stripped | BTRFS_BLOCK_GROUP_RAID1;
6357
6358 /* turn single device chunks into raid0 */
6359 return stripped | BTRFS_BLOCK_GROUP_RAID0;
6360 }
6361 return flags;
6362}
6363
Christoph Hellwigb2950862008-12-02 09:54:17 -05006364static int __alloc_chunk_for_shrink(struct btrfs_root *root,
Chris Mason0ef3e662008-05-24 14:04:53 -04006365 struct btrfs_block_group_cache *shrink_block_group,
6366 int force)
6367{
6368 struct btrfs_trans_handle *trans;
6369 u64 new_alloc_flags;
6370 u64 calc;
6371
Chris Masonc286ac42008-07-22 23:06:41 -04006372 spin_lock(&shrink_block_group->lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006373 if (btrfs_block_group_used(&shrink_block_group->item) +
6374 shrink_block_group->reserved > 0) {
Chris Masonc286ac42008-07-22 23:06:41 -04006375 spin_unlock(&shrink_block_group->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04006376
Chris Mason0ef3e662008-05-24 14:04:53 -04006377 trans = btrfs_start_transaction(root, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04006378 spin_lock(&shrink_block_group->lock);
Chris Mason7d9eb122008-07-08 14:19:17 -04006379
Chris Mason0ef3e662008-05-24 14:04:53 -04006380 new_alloc_flags = update_block_group_flags(root,
6381 shrink_block_group->flags);
6382 if (new_alloc_flags != shrink_block_group->flags) {
6383 calc =
6384 btrfs_block_group_used(&shrink_block_group->item);
6385 } else {
6386 calc = shrink_block_group->key.offset;
6387 }
Chris Masonc286ac42008-07-22 23:06:41 -04006388 spin_unlock(&shrink_block_group->lock);
6389
Chris Mason0ef3e662008-05-24 14:04:53 -04006390 do_chunk_alloc(trans, root->fs_info->extent_root,
6391 calc + 2 * 1024 * 1024, new_alloc_flags, force);
Chris Mason7d9eb122008-07-08 14:19:17 -04006392
Chris Mason0ef3e662008-05-24 14:04:53 -04006393 btrfs_end_transaction(trans, root);
Chris Masonc286ac42008-07-22 23:06:41 -04006394 } else
6395 spin_unlock(&shrink_block_group->lock);
Chris Mason0ef3e662008-05-24 14:04:53 -04006396 return 0;
6397}
6398
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006399
6400int btrfs_prepare_block_group_relocation(struct btrfs_root *root,
6401 struct btrfs_block_group_cache *group)
6402
6403{
6404 __alloc_chunk_for_shrink(root, group, 1);
6405 set_block_group_readonly(group);
6406 return 0;
6407}
6408
6409#if 0
Zheng Yan1a40e232008-09-26 10:09:34 -04006410static int __insert_orphan_inode(struct btrfs_trans_handle *trans,
6411 struct btrfs_root *root,
6412 u64 objectid, u64 size)
6413{
6414 struct btrfs_path *path;
6415 struct btrfs_inode_item *item;
6416 struct extent_buffer *leaf;
6417 int ret;
6418
6419 path = btrfs_alloc_path();
6420 if (!path)
6421 return -ENOMEM;
6422
Chris Masonb9473432009-03-13 11:00:37 -04006423 path->leave_spinning = 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04006424 ret = btrfs_insert_empty_inode(trans, root, path, objectid);
6425 if (ret)
6426 goto out;
6427
6428 leaf = path->nodes[0];
6429 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_inode_item);
6430 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
6431 btrfs_set_inode_generation(leaf, item, 1);
6432 btrfs_set_inode_size(leaf, item, size);
6433 btrfs_set_inode_mode(leaf, item, S_IFREG | 0600);
Yan Zheng0403e472008-12-10 20:32:51 -05006434 btrfs_set_inode_flags(leaf, item, BTRFS_INODE_NOCOMPRESS);
Zheng Yan1a40e232008-09-26 10:09:34 -04006435 btrfs_mark_buffer_dirty(leaf);
6436 btrfs_release_path(root, path);
6437out:
6438 btrfs_free_path(path);
6439 return ret;
6440}
6441
Chris Masond3977122009-01-05 21:25:51 -05006442static noinline struct inode *create_reloc_inode(struct btrfs_fs_info *fs_info,
Zheng Yan1a40e232008-09-26 10:09:34 -04006443 struct btrfs_block_group_cache *group)
6444{
6445 struct inode *inode = NULL;
6446 struct btrfs_trans_handle *trans;
6447 struct btrfs_root *root;
6448 struct btrfs_key root_key;
6449 u64 objectid = BTRFS_FIRST_FREE_OBJECTID;
6450 int err = 0;
6451
6452 root_key.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
6453 root_key.type = BTRFS_ROOT_ITEM_KEY;
6454 root_key.offset = (u64)-1;
6455 root = btrfs_read_fs_root_no_name(fs_info, &root_key);
6456 if (IS_ERR(root))
6457 return ERR_CAST(root);
6458
6459 trans = btrfs_start_transaction(root, 1);
6460 BUG_ON(!trans);
6461
6462 err = btrfs_find_free_objectid(trans, root, objectid, &objectid);
6463 if (err)
6464 goto out;
6465
6466 err = __insert_orphan_inode(trans, root, objectid, group->key.offset);
6467 BUG_ON(err);
6468
6469 err = btrfs_insert_file_extent(trans, root, objectid, 0, 0, 0,
Chris Masonc8b97812008-10-29 14:49:59 -04006470 group->key.offset, 0, group->key.offset,
6471 0, 0, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04006472 BUG_ON(err);
6473
6474 inode = btrfs_iget_locked(root->fs_info->sb, objectid, root);
6475 if (inode->i_state & I_NEW) {
6476 BTRFS_I(inode)->root = root;
6477 BTRFS_I(inode)->location.objectid = objectid;
6478 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
6479 BTRFS_I(inode)->location.offset = 0;
6480 btrfs_read_locked_inode(inode);
6481 unlock_new_inode(inode);
6482 BUG_ON(is_bad_inode(inode));
6483 } else {
6484 BUG_ON(1);
6485 }
Yan Zheng17d217f2008-12-12 10:03:38 -05006486 BTRFS_I(inode)->index_cnt = group->key.objectid;
Zheng Yan1a40e232008-09-26 10:09:34 -04006487
6488 err = btrfs_orphan_add(trans, inode);
6489out:
6490 btrfs_end_transaction(trans, root);
6491 if (err) {
6492 if (inode)
6493 iput(inode);
6494 inode = ERR_PTR(err);
6495 }
6496 return inode;
6497}
6498
Yan Zheng17d217f2008-12-12 10:03:38 -05006499int btrfs_reloc_clone_csums(struct inode *inode, u64 file_pos, u64 len)
6500{
6501
6502 struct btrfs_ordered_sum *sums;
6503 struct btrfs_sector_sum *sector_sum;
6504 struct btrfs_ordered_extent *ordered;
6505 struct btrfs_root *root = BTRFS_I(inode)->root;
6506 struct list_head list;
6507 size_t offset;
6508 int ret;
6509 u64 disk_bytenr;
6510
6511 INIT_LIST_HEAD(&list);
6512
6513 ordered = btrfs_lookup_ordered_extent(inode, file_pos);
6514 BUG_ON(ordered->file_offset != file_pos || ordered->len != len);
6515
6516 disk_bytenr = file_pos + BTRFS_I(inode)->index_cnt;
Yan Zheng07d400a2009-01-06 11:42:00 -05006517 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, disk_bytenr,
Yan Zheng17d217f2008-12-12 10:03:38 -05006518 disk_bytenr + len - 1, &list);
6519
6520 while (!list_empty(&list)) {
6521 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
6522 list_del_init(&sums->list);
6523
6524 sector_sum = sums->sums;
6525 sums->bytenr = ordered->start;
6526
6527 offset = 0;
6528 while (offset < sums->len) {
6529 sector_sum->bytenr += ordered->start - disk_bytenr;
6530 sector_sum++;
6531 offset += root->sectorsize;
6532 }
6533
6534 btrfs_add_ordered_sum(inode, ordered, sums);
6535 }
6536 btrfs_put_ordered_extent(ordered);
6537 return 0;
6538}
6539
Zheng Yan1a40e232008-09-26 10:09:34 -04006540int btrfs_relocate_block_group(struct btrfs_root *root, u64 group_start)
Chris Masonedbd8d42007-12-21 16:27:24 -05006541{
6542 struct btrfs_trans_handle *trans;
Chris Masonedbd8d42007-12-21 16:27:24 -05006543 struct btrfs_path *path;
Zheng Yan1a40e232008-09-26 10:09:34 -04006544 struct btrfs_fs_info *info = root->fs_info;
6545 struct extent_buffer *leaf;
6546 struct inode *reloc_inode;
6547 struct btrfs_block_group_cache *block_group;
6548 struct btrfs_key key;
Yan Zhengd899e052008-10-30 14:25:28 -04006549 u64 skipped;
Chris Masonedbd8d42007-12-21 16:27:24 -05006550 u64 cur_byte;
6551 u64 total_found;
Chris Masonedbd8d42007-12-21 16:27:24 -05006552 u32 nritems;
6553 int ret;
Chris Masona061fc82008-05-07 11:43:44 -04006554 int progress;
Zheng Yan1a40e232008-09-26 10:09:34 -04006555 int pass = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05006556
Chris Masonedbd8d42007-12-21 16:27:24 -05006557 root = root->fs_info->extent_root;
Zheng Yan1a40e232008-09-26 10:09:34 -04006558
6559 block_group = btrfs_lookup_block_group(info, group_start);
6560 BUG_ON(!block_group);
Chris Masonedbd8d42007-12-21 16:27:24 -05006561
Chris Masond3977122009-01-05 21:25:51 -05006562 printk(KERN_INFO "btrfs relocating block group %llu flags %llu\n",
Zheng Yan1a40e232008-09-26 10:09:34 -04006563 (unsigned long long)block_group->key.objectid,
6564 (unsigned long long)block_group->flags);
Chris Mason323da792008-05-09 11:46:48 -04006565
Zheng Yan1a40e232008-09-26 10:09:34 -04006566 path = btrfs_alloc_path();
6567 BUG_ON(!path);
Chris Mason323da792008-05-09 11:46:48 -04006568
Zheng Yan1a40e232008-09-26 10:09:34 -04006569 reloc_inode = create_reloc_inode(info, block_group);
6570 BUG_ON(IS_ERR(reloc_inode));
6571
Zheng Yan1a40e232008-09-26 10:09:34 -04006572 __alloc_chunk_for_shrink(root, block_group, 1);
Yan Zhengc146afa2008-11-12 14:34:12 -05006573 set_block_group_readonly(block_group);
Zheng Yan1a40e232008-09-26 10:09:34 -04006574
Zheng Yan1a40e232008-09-26 10:09:34 -04006575 btrfs_start_delalloc_inodes(info->tree_root);
6576 btrfs_wait_ordered_extents(info->tree_root, 0);
Chris Mason0ef3e662008-05-24 14:04:53 -04006577again:
Yan Zhengd899e052008-10-30 14:25:28 -04006578 skipped = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05006579 total_found = 0;
Chris Masona061fc82008-05-07 11:43:44 -04006580 progress = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04006581 key.objectid = block_group->key.objectid;
Chris Masonedbd8d42007-12-21 16:27:24 -05006582 key.offset = 0;
6583 key.type = 0;
Yan73e48b22008-01-03 14:14:39 -05006584 cur_byte = key.objectid;
Chris Mason4313b392008-01-03 09:08:48 -05006585
Zheng Yan1a40e232008-09-26 10:09:34 -04006586 trans = btrfs_start_transaction(info->tree_root, 1);
6587 btrfs_commit_transaction(trans, info->tree_root);
Chris Masonea8c2812008-08-04 23:17:27 -04006588
Zheng Yan1a40e232008-09-26 10:09:34 -04006589 mutex_lock(&root->fs_info->cleaner_mutex);
6590 btrfs_clean_old_snapshots(info->tree_root);
6591 btrfs_remove_leaf_refs(info->tree_root, (u64)-1, 1);
6592 mutex_unlock(&root->fs_info->cleaner_mutex);
Chris Masonea8c2812008-08-04 23:17:27 -04006593
Chris Mason56bec292009-03-13 10:10:06 -04006594 trans = btrfs_start_transaction(info->tree_root, 1);
6595 btrfs_commit_transaction(trans, info->tree_root);
6596
Chris Masond3977122009-01-05 21:25:51 -05006597 while (1) {
Chris Masonedbd8d42007-12-21 16:27:24 -05006598 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6599 if (ret < 0)
6600 goto out;
Chris Mason7d9eb122008-07-08 14:19:17 -04006601next:
Chris Masonedbd8d42007-12-21 16:27:24 -05006602 leaf = path->nodes[0];
Chris Masonedbd8d42007-12-21 16:27:24 -05006603 nritems = btrfs_header_nritems(leaf);
Yan73e48b22008-01-03 14:14:39 -05006604 if (path->slots[0] >= nritems) {
6605 ret = btrfs_next_leaf(root, path);
6606 if (ret < 0)
6607 goto out;
6608 if (ret == 1) {
6609 ret = 0;
6610 break;
Chris Masonedbd8d42007-12-21 16:27:24 -05006611 }
Yan73e48b22008-01-03 14:14:39 -05006612 leaf = path->nodes[0];
6613 nritems = btrfs_header_nritems(leaf);
6614 }
6615
Zheng Yan1a40e232008-09-26 10:09:34 -04006616 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Chris Mason725c8462008-01-04 16:47:16 -05006617
Zheng Yan1a40e232008-09-26 10:09:34 -04006618 if (key.objectid >= block_group->key.objectid +
6619 block_group->key.offset)
Chris Mason8f18cf12008-04-25 16:53:30 -04006620 break;
6621
Chris Mason725c8462008-01-04 16:47:16 -05006622 if (progress && need_resched()) {
Chris Mason725c8462008-01-04 16:47:16 -05006623 btrfs_release_path(root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04006624 cond_resched();
Chris Mason725c8462008-01-04 16:47:16 -05006625 progress = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04006626 continue;
Chris Mason725c8462008-01-04 16:47:16 -05006627 }
6628 progress = 1;
6629
Zheng Yan1a40e232008-09-26 10:09:34 -04006630 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY ||
6631 key.objectid + key.offset <= cur_byte) {
Yan73e48b22008-01-03 14:14:39 -05006632 path->slots[0]++;
Chris Masonedbd8d42007-12-21 16:27:24 -05006633 goto next;
6634 }
Yan73e48b22008-01-03 14:14:39 -05006635
Chris Masonedbd8d42007-12-21 16:27:24 -05006636 total_found++;
Zheng Yan1a40e232008-09-26 10:09:34 -04006637 cur_byte = key.objectid + key.offset;
Chris Masonedbd8d42007-12-21 16:27:24 -05006638 btrfs_release_path(root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04006639
6640 __alloc_chunk_for_shrink(root, block_group, 0);
6641 ret = relocate_one_extent(root, path, &key, block_group,
6642 reloc_inode, pass);
6643 BUG_ON(ret < 0);
Yan Zhengd899e052008-10-30 14:25:28 -04006644 if (ret > 0)
6645 skipped++;
Zheng Yan1a40e232008-09-26 10:09:34 -04006646
6647 key.objectid = cur_byte;
6648 key.type = 0;
6649 key.offset = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05006650 }
6651
6652 btrfs_release_path(root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04006653
6654 if (pass == 0) {
6655 btrfs_wait_ordered_range(reloc_inode, 0, (u64)-1);
6656 invalidate_mapping_pages(reloc_inode->i_mapping, 0, -1);
Zheng Yan1a40e232008-09-26 10:09:34 -04006657 }
Chris Masonedbd8d42007-12-21 16:27:24 -05006658
6659 if (total_found > 0) {
Chris Masond3977122009-01-05 21:25:51 -05006660 printk(KERN_INFO "btrfs found %llu extents in pass %d\n",
Zheng Yan1a40e232008-09-26 10:09:34 -04006661 (unsigned long long)total_found, pass);
6662 pass++;
Yan Zhengd899e052008-10-30 14:25:28 -04006663 if (total_found == skipped && pass > 2) {
6664 iput(reloc_inode);
6665 reloc_inode = create_reloc_inode(info, block_group);
6666 pass = 0;
6667 }
Chris Masonedbd8d42007-12-21 16:27:24 -05006668 goto again;
6669 }
6670
Zheng Yan1a40e232008-09-26 10:09:34 -04006671 /* delete reloc_inode */
6672 iput(reloc_inode);
Chris Mason7d9eb122008-07-08 14:19:17 -04006673
Zheng Yan1a40e232008-09-26 10:09:34 -04006674 /* unpin extents in this range */
6675 trans = btrfs_start_transaction(info->tree_root, 1);
6676 btrfs_commit_transaction(trans, info->tree_root);
Chris Mason0ef3e662008-05-24 14:04:53 -04006677
Zheng Yan1a40e232008-09-26 10:09:34 -04006678 spin_lock(&block_group->lock);
6679 WARN_ON(block_group->pinned > 0);
6680 WARN_ON(block_group->reserved > 0);
6681 WARN_ON(btrfs_block_group_used(&block_group->item) > 0);
6682 spin_unlock(&block_group->lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04006683 btrfs_put_block_group(block_group);
Zheng Yan1a40e232008-09-26 10:09:34 -04006684 ret = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05006685out:
Zheng Yan1a40e232008-09-26 10:09:34 -04006686 btrfs_free_path(path);
Chris Masonedbd8d42007-12-21 16:27:24 -05006687 return ret;
6688}
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006689#endif
Chris Masonedbd8d42007-12-21 16:27:24 -05006690
Christoph Hellwigb2950862008-12-02 09:54:17 -05006691static int find_first_block_group(struct btrfs_root *root,
6692 struct btrfs_path *path, struct btrfs_key *key)
Chris Mason0b86a832008-03-24 15:01:56 -04006693{
Chris Mason925baed2008-06-25 16:01:30 -04006694 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006695 struct btrfs_key found_key;
6696 struct extent_buffer *leaf;
6697 int slot;
6698
6699 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
6700 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04006701 goto out;
6702
Chris Masond3977122009-01-05 21:25:51 -05006703 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04006704 slot = path->slots[0];
6705 leaf = path->nodes[0];
6706 if (slot >= btrfs_header_nritems(leaf)) {
6707 ret = btrfs_next_leaf(root, path);
6708 if (ret == 0)
6709 continue;
6710 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04006711 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04006712 break;
6713 }
6714 btrfs_item_key_to_cpu(leaf, &found_key, slot);
6715
6716 if (found_key.objectid >= key->objectid &&
Chris Mason925baed2008-06-25 16:01:30 -04006717 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
6718 ret = 0;
6719 goto out;
6720 }
Chris Mason0b86a832008-03-24 15:01:56 -04006721 path->slots[0]++;
6722 }
6723 ret = -ENOENT;
Chris Mason925baed2008-06-25 16:01:30 -04006724out:
Chris Mason0b86a832008-03-24 15:01:56 -04006725 return ret;
6726}
6727
Zheng Yan1a40e232008-09-26 10:09:34 -04006728int btrfs_free_block_groups(struct btrfs_fs_info *info)
6729{
6730 struct btrfs_block_group_cache *block_group;
Chris Mason4184ea72009-03-10 12:39:20 -04006731 struct btrfs_space_info *space_info;
Zheng Yan1a40e232008-09-26 10:09:34 -04006732 struct rb_node *n;
6733
Zheng Yan1a40e232008-09-26 10:09:34 -04006734 spin_lock(&info->block_group_cache_lock);
6735 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
6736 block_group = rb_entry(n, struct btrfs_block_group_cache,
6737 cache_node);
Zheng Yan1a40e232008-09-26 10:09:34 -04006738 rb_erase(&block_group->cache_node,
6739 &info->block_group_cache_tree);
Yan Zhengd899e052008-10-30 14:25:28 -04006740 spin_unlock(&info->block_group_cache_lock);
6741
6742 btrfs_remove_free_space_cache(block_group);
Josef Bacik80eb2342008-10-29 14:49:05 -04006743 down_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04006744 list_del(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04006745 up_write(&block_group->space_info->groups_sem);
Yan Zhengd2fb3432008-12-11 16:30:39 -05006746
6747 WARN_ON(atomic_read(&block_group->count) != 1);
Zheng Yan1a40e232008-09-26 10:09:34 -04006748 kfree(block_group);
Yan Zhengd899e052008-10-30 14:25:28 -04006749
6750 spin_lock(&info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04006751 }
6752 spin_unlock(&info->block_group_cache_lock);
Chris Mason4184ea72009-03-10 12:39:20 -04006753
6754 /* now that all the block groups are freed, go through and
6755 * free all the space_info structs. This is only called during
6756 * the final stages of unmount, and so we know nobody is
6757 * using them. We call synchronize_rcu() once before we start,
6758 * just to be on the safe side.
6759 */
6760 synchronize_rcu();
6761
6762 while(!list_empty(&info->space_info)) {
6763 space_info = list_entry(info->space_info.next,
6764 struct btrfs_space_info,
6765 list);
6766
6767 list_del(&space_info->list);
6768 kfree(space_info);
6769 }
Zheng Yan1a40e232008-09-26 10:09:34 -04006770 return 0;
6771}
6772
Chris Mason9078a3e2007-04-26 16:46:15 -04006773int btrfs_read_block_groups(struct btrfs_root *root)
6774{
6775 struct btrfs_path *path;
6776 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04006777 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04006778 struct btrfs_fs_info *info = root->fs_info;
Chris Mason6324fbf2008-03-24 15:01:59 -04006779 struct btrfs_space_info *space_info;
Chris Mason9078a3e2007-04-26 16:46:15 -04006780 struct btrfs_key key;
6781 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -04006782 struct extent_buffer *leaf;
Chris Mason96b51792007-10-15 16:15:19 -04006783
Chris Masonbe744172007-05-06 10:15:01 -04006784 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04006785 key.objectid = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006786 key.offset = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04006787 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
Chris Mason9078a3e2007-04-26 16:46:15 -04006788 path = btrfs_alloc_path();
6789 if (!path)
6790 return -ENOMEM;
6791
Chris Masond3977122009-01-05 21:25:51 -05006792 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04006793 ret = find_first_block_group(root, path, &key);
6794 if (ret > 0) {
6795 ret = 0;
6796 goto error;
Chris Mason9078a3e2007-04-26 16:46:15 -04006797 }
Chris Mason0b86a832008-03-24 15:01:56 -04006798 if (ret != 0)
6799 goto error;
6800
Chris Mason5f39d392007-10-15 16:14:19 -04006801 leaf = path->nodes[0];
6802 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason8f18cf12008-04-25 16:53:30 -04006803 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -04006804 if (!cache) {
Chris Mason0b86a832008-03-24 15:01:56 -04006805 ret = -ENOMEM;
Chris Mason9078a3e2007-04-26 16:46:15 -04006806 break;
6807 }
Chris Mason3e1ad542007-05-07 20:03:49 -04006808
Yan Zhengd2fb3432008-12-11 16:30:39 -05006809 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04006810 spin_lock_init(&cache->lock);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04006811 spin_lock_init(&cache->tree_lock);
Josef Bacikea6a4782008-11-20 12:16:16 -05006812 mutex_init(&cache->cache_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04006813 INIT_LIST_HEAD(&cache->list);
Chris Masonfa9c0d792009-04-03 09:47:43 -04006814 INIT_LIST_HEAD(&cache->cluster_list);
Chris Mason5f39d392007-10-15 16:14:19 -04006815 read_extent_buffer(leaf, &cache->item,
6816 btrfs_item_ptr_offset(leaf, path->slots[0]),
6817 sizeof(cache->item));
Chris Mason9078a3e2007-04-26 16:46:15 -04006818 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Mason0b86a832008-03-24 15:01:56 -04006819
Chris Mason9078a3e2007-04-26 16:46:15 -04006820 key.objectid = found_key.objectid + found_key.offset;
6821 btrfs_release_path(root, path);
Chris Mason0b86a832008-03-24 15:01:56 -04006822 cache->flags = btrfs_block_group_flags(&cache->item);
Chris Mason96b51792007-10-15 16:15:19 -04006823
Chris Mason6324fbf2008-03-24 15:01:59 -04006824 ret = update_space_info(info, cache->flags, found_key.offset,
6825 btrfs_block_group_used(&cache->item),
6826 &space_info);
6827 BUG_ON(ret);
6828 cache->space_info = space_info;
Josef Bacik80eb2342008-10-29 14:49:05 -04006829 down_write(&space_info->groups_sem);
6830 list_add_tail(&cache->list, &space_info->block_groups);
6831 up_write(&space_info->groups_sem);
Chris Mason6324fbf2008-03-24 15:01:59 -04006832
Josef Bacik0f9dd462008-09-23 13:14:11 -04006833 ret = btrfs_add_block_group_cache(root->fs_info, cache);
6834 BUG_ON(ret);
Chris Mason75ccf472008-09-30 19:24:06 -04006835
6836 set_avail_alloc_bits(root->fs_info, cache->flags);
Yan Zheng2b820322008-11-17 21:11:30 -05006837 if (btrfs_chunk_readonly(root, cache->key.objectid))
6838 set_block_group_readonly(cache);
Chris Mason9078a3e2007-04-26 16:46:15 -04006839 }
Chris Mason0b86a832008-03-24 15:01:56 -04006840 ret = 0;
6841error:
Chris Mason9078a3e2007-04-26 16:46:15 -04006842 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04006843 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04006844}
Chris Mason6324fbf2008-03-24 15:01:59 -04006845
6846int btrfs_make_block_group(struct btrfs_trans_handle *trans,
6847 struct btrfs_root *root, u64 bytes_used,
Chris Masone17cade2008-04-15 15:41:47 -04006848 u64 type, u64 chunk_objectid, u64 chunk_offset,
Chris Mason6324fbf2008-03-24 15:01:59 -04006849 u64 size)
6850{
6851 int ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04006852 struct btrfs_root *extent_root;
6853 struct btrfs_block_group_cache *cache;
Chris Mason6324fbf2008-03-24 15:01:59 -04006854
6855 extent_root = root->fs_info->extent_root;
Chris Mason6324fbf2008-03-24 15:01:59 -04006856
Chris Mason12fcfd22009-03-24 10:24:20 -04006857 root->fs_info->last_trans_log_full_commit = trans->transid;
Chris Masone02119d2008-09-05 16:13:11 -04006858
Chris Mason8f18cf12008-04-25 16:53:30 -04006859 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Josef Bacik0f9dd462008-09-23 13:14:11 -04006860 if (!cache)
6861 return -ENOMEM;
6862
Chris Masone17cade2008-04-15 15:41:47 -04006863 cache->key.objectid = chunk_offset;
Chris Mason6324fbf2008-03-24 15:01:59 -04006864 cache->key.offset = size;
Yan Zhengd2fb3432008-12-11 16:30:39 -05006865 cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
6866 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04006867 spin_lock_init(&cache->lock);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04006868 spin_lock_init(&cache->tree_lock);
Josef Bacikea6a4782008-11-20 12:16:16 -05006869 mutex_init(&cache->cache_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04006870 INIT_LIST_HEAD(&cache->list);
Chris Masonfa9c0d792009-04-03 09:47:43 -04006871 INIT_LIST_HEAD(&cache->cluster_list);
Chris Mason0ef3e662008-05-24 14:04:53 -04006872
Chris Mason6324fbf2008-03-24 15:01:59 -04006873 btrfs_set_block_group_used(&cache->item, bytes_used);
Chris Mason6324fbf2008-03-24 15:01:59 -04006874 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
6875 cache->flags = type;
6876 btrfs_set_block_group_flags(&cache->item, type);
6877
6878 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
6879 &cache->space_info);
6880 BUG_ON(ret);
Josef Bacik80eb2342008-10-29 14:49:05 -04006881 down_write(&cache->space_info->groups_sem);
6882 list_add_tail(&cache->list, &cache->space_info->block_groups);
6883 up_write(&cache->space_info->groups_sem);
Chris Mason6324fbf2008-03-24 15:01:59 -04006884
Josef Bacik0f9dd462008-09-23 13:14:11 -04006885 ret = btrfs_add_block_group_cache(root->fs_info, cache);
6886 BUG_ON(ret);
Chris Masonc286ac42008-07-22 23:06:41 -04006887
Chris Mason6324fbf2008-03-24 15:01:59 -04006888 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
6889 sizeof(cache->item));
6890 BUG_ON(ret);
6891
Chris Masond18a2c42008-04-04 15:40:00 -04006892 set_avail_alloc_bits(extent_root->fs_info, type);
Chris Mason925baed2008-06-25 16:01:30 -04006893
Chris Mason6324fbf2008-03-24 15:01:59 -04006894 return 0;
6895}
Zheng Yan1a40e232008-09-26 10:09:34 -04006896
6897int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
6898 struct btrfs_root *root, u64 group_start)
6899{
6900 struct btrfs_path *path;
6901 struct btrfs_block_group_cache *block_group;
Chris Mason44fb5512009-06-04 15:34:51 -04006902 struct btrfs_free_cluster *cluster;
Zheng Yan1a40e232008-09-26 10:09:34 -04006903 struct btrfs_key key;
6904 int ret;
6905
Zheng Yan1a40e232008-09-26 10:09:34 -04006906 root = root->fs_info->extent_root;
6907
6908 block_group = btrfs_lookup_block_group(root->fs_info, group_start);
6909 BUG_ON(!block_group);
Yan Zhengc146afa2008-11-12 14:34:12 -05006910 BUG_ON(!block_group->ro);
Zheng Yan1a40e232008-09-26 10:09:34 -04006911
6912 memcpy(&key, &block_group->key, sizeof(key));
6913
Chris Mason44fb5512009-06-04 15:34:51 -04006914 /* make sure this block group isn't part of an allocation cluster */
6915 cluster = &root->fs_info->data_alloc_cluster;
6916 spin_lock(&cluster->refill_lock);
6917 btrfs_return_cluster_to_free_space(block_group, cluster);
6918 spin_unlock(&cluster->refill_lock);
6919
6920 /*
6921 * make sure this block group isn't part of a metadata
6922 * allocation cluster
6923 */
6924 cluster = &root->fs_info->meta_alloc_cluster;
6925 spin_lock(&cluster->refill_lock);
6926 btrfs_return_cluster_to_free_space(block_group, cluster);
6927 spin_unlock(&cluster->refill_lock);
6928
Zheng Yan1a40e232008-09-26 10:09:34 -04006929 path = btrfs_alloc_path();
6930 BUG_ON(!path);
6931
Yan Zheng3dfdb932009-01-21 10:49:16 -05006932 spin_lock(&root->fs_info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04006933 rb_erase(&block_group->cache_node,
6934 &root->fs_info->block_group_cache_tree);
Yan Zheng3dfdb932009-01-21 10:49:16 -05006935 spin_unlock(&root->fs_info->block_group_cache_lock);
6936 btrfs_remove_free_space_cache(block_group);
Josef Bacik80eb2342008-10-29 14:49:05 -04006937 down_write(&block_group->space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04006938 /*
6939 * we must use list_del_init so people can check to see if they
6940 * are still on the list after taking the semaphore
6941 */
6942 list_del_init(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04006943 up_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04006944
Yan Zhengc146afa2008-11-12 14:34:12 -05006945 spin_lock(&block_group->space_info->lock);
6946 block_group->space_info->total_bytes -= block_group->key.offset;
6947 block_group->space_info->bytes_readonly -= block_group->key.offset;
6948 spin_unlock(&block_group->space_info->lock);
Yan Zheng2b820322008-11-17 21:11:30 -05006949 block_group->space_info->full = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05006950
Chris Masonfa9c0d792009-04-03 09:47:43 -04006951 btrfs_put_block_group(block_group);
6952 btrfs_put_block_group(block_group);
Zheng Yan1a40e232008-09-26 10:09:34 -04006953
6954 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
6955 if (ret > 0)
6956 ret = -EIO;
6957 if (ret < 0)
6958 goto out;
6959
6960 ret = btrfs_del_item(trans, root, path);
6961out:
6962 btrfs_free_path(path);
6963 return ret;
6964}