blob: 33a65f2c8a3784db3bdc60d8614e9f9806beb1e8 [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 Masonfa9c0d72009-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 Masonfa9c0d72009-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 Masonfa9c0d72009-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 Masonfa9c0d72009-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);
1059 if (owner >= BTRFS_FIRST_FREE_OBJECTID)
1060 path->keep_locks = 1;
1061 } else
1062 extra_size = -1;
1063 ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1);
1064 if (ret < 0) {
1065 err = ret;
1066 goto out;
1067 }
1068 BUG_ON(ret);
1069
1070 leaf = path->nodes[0];
1071 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1072#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1073 if (item_size < sizeof(*ei)) {
1074 if (!insert) {
1075 err = -ENOENT;
1076 goto out;
1077 }
1078 ret = convert_extent_item_v0(trans, root, path, owner,
1079 extra_size);
1080 if (ret < 0) {
1081 err = ret;
1082 goto out;
1083 }
1084 leaf = path->nodes[0];
1085 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1086 }
1087#endif
1088 BUG_ON(item_size < sizeof(*ei));
1089
1090 if (owner < BTRFS_FIRST_FREE_OBJECTID && insert &&
1091 item_size + extra_size >= BTRFS_MAX_EXTENT_ITEM_SIZE(root)) {
1092 err = -EAGAIN;
1093 goto out;
1094 }
1095
1096 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1097 flags = btrfs_extent_flags(leaf, ei);
1098
1099 ptr = (unsigned long)(ei + 1);
1100 end = (unsigned long)ei + item_size;
1101
1102 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
1103 ptr += sizeof(struct btrfs_tree_block_info);
1104 BUG_ON(ptr > end);
1105 } else {
1106 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_DATA));
1107 }
1108
1109 err = -ENOENT;
1110 while (1) {
1111 if (ptr >= end) {
1112 WARN_ON(ptr > end);
1113 break;
1114 }
1115 iref = (struct btrfs_extent_inline_ref *)ptr;
1116 type = btrfs_extent_inline_ref_type(leaf, iref);
1117 if (want < type)
1118 break;
1119 if (want > type) {
1120 ptr += btrfs_extent_inline_ref_size(type);
1121 continue;
1122 }
1123
1124 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1125 struct btrfs_extent_data_ref *dref;
1126 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1127 if (match_extent_data_ref(leaf, dref, root_objectid,
1128 owner, offset)) {
1129 err = 0;
1130 break;
1131 }
1132 if (hash_extent_data_ref_item(leaf, dref) <
1133 hash_extent_data_ref(root_objectid, owner, offset))
1134 break;
1135 } else {
1136 u64 ref_offset;
1137 ref_offset = btrfs_extent_inline_ref_offset(leaf, iref);
1138 if (parent > 0) {
1139 if (parent == ref_offset) {
1140 err = 0;
1141 break;
1142 }
1143 if (ref_offset < parent)
1144 break;
1145 } else {
1146 if (root_objectid == ref_offset) {
1147 err = 0;
1148 break;
1149 }
1150 if (ref_offset < root_objectid)
1151 break;
1152 }
1153 }
1154 ptr += btrfs_extent_inline_ref_size(type);
1155 }
1156 if (err == -ENOENT && insert) {
1157 if (item_size + extra_size >=
1158 BTRFS_MAX_EXTENT_ITEM_SIZE(root)) {
1159 err = -EAGAIN;
1160 goto out;
1161 }
1162 /*
1163 * To add new inline back ref, we have to make sure
1164 * there is no corresponding back ref item.
1165 * For simplicity, we just do not add new inline back
1166 * ref if there is any kind of item for this block
1167 */
1168 if (owner >= BTRFS_FIRST_FREE_OBJECTID &&
1169 find_next_key(path, &key) == 0 && key.objectid == bytenr) {
1170 err = -EAGAIN;
1171 goto out;
1172 }
1173 }
1174 *ref_ret = (struct btrfs_extent_inline_ref *)ptr;
1175out:
1176 if (insert && owner >= BTRFS_FIRST_FREE_OBJECTID) {
1177 path->keep_locks = 0;
1178 btrfs_unlock_up_safe(path, 1);
1179 }
1180 return err;
1181}
1182
1183/*
1184 * helper to add new inline back ref
1185 */
1186static noinline_for_stack
1187int setup_inline_extent_backref(struct btrfs_trans_handle *trans,
1188 struct btrfs_root *root,
1189 struct btrfs_path *path,
1190 struct btrfs_extent_inline_ref *iref,
1191 u64 parent, u64 root_objectid,
1192 u64 owner, u64 offset, int refs_to_add,
1193 struct btrfs_delayed_extent_op *extent_op)
1194{
1195 struct extent_buffer *leaf;
1196 struct btrfs_extent_item *ei;
1197 unsigned long ptr;
1198 unsigned long end;
1199 unsigned long item_offset;
1200 u64 refs;
1201 int size;
1202 int type;
1203 int ret;
1204
1205 leaf = path->nodes[0];
1206 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1207 item_offset = (unsigned long)iref - (unsigned long)ei;
1208
1209 type = extent_ref_type(parent, owner);
1210 size = btrfs_extent_inline_ref_size(type);
1211
1212 ret = btrfs_extend_item(trans, root, path, size);
1213 BUG_ON(ret);
1214
1215 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1216 refs = btrfs_extent_refs(leaf, ei);
1217 refs += refs_to_add;
1218 btrfs_set_extent_refs(leaf, ei, refs);
1219 if (extent_op)
1220 __run_delayed_extent_op(extent_op, leaf, ei);
1221
1222 ptr = (unsigned long)ei + item_offset;
1223 end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]);
1224 if (ptr < end - size)
1225 memmove_extent_buffer(leaf, ptr + size, ptr,
1226 end - size - ptr);
1227
1228 iref = (struct btrfs_extent_inline_ref *)ptr;
1229 btrfs_set_extent_inline_ref_type(leaf, iref, type);
1230 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1231 struct btrfs_extent_data_ref *dref;
1232 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1233 btrfs_set_extent_data_ref_root(leaf, dref, root_objectid);
1234 btrfs_set_extent_data_ref_objectid(leaf, dref, owner);
1235 btrfs_set_extent_data_ref_offset(leaf, dref, offset);
1236 btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add);
1237 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1238 struct btrfs_shared_data_ref *sref;
1239 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1240 btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add);
1241 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1242 } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
1243 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1244 } else {
1245 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
1246 }
1247 btrfs_mark_buffer_dirty(leaf);
1248 return 0;
1249}
1250
1251static int lookup_extent_backref(struct btrfs_trans_handle *trans,
1252 struct btrfs_root *root,
1253 struct btrfs_path *path,
1254 struct btrfs_extent_inline_ref **ref_ret,
1255 u64 bytenr, u64 num_bytes, u64 parent,
1256 u64 root_objectid, u64 owner, u64 offset)
1257{
1258 int ret;
1259
1260 ret = lookup_inline_extent_backref(trans, root, path, ref_ret,
1261 bytenr, num_bytes, parent,
1262 root_objectid, owner, offset, 0);
1263 if (ret != -ENOENT)
1264 return ret;
1265
1266 btrfs_release_path(root, path);
1267 *ref_ret = NULL;
1268
1269 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1270 ret = lookup_tree_block_ref(trans, root, path, bytenr, parent,
1271 root_objectid);
1272 } else {
1273 ret = lookup_extent_data_ref(trans, root, path, bytenr, parent,
1274 root_objectid, owner, offset);
1275 }
1276 return ret;
1277}
1278
1279/*
1280 * helper to update/remove inline back ref
1281 */
1282static noinline_for_stack
1283int update_inline_extent_backref(struct btrfs_trans_handle *trans,
1284 struct btrfs_root *root,
1285 struct btrfs_path *path,
1286 struct btrfs_extent_inline_ref *iref,
1287 int refs_to_mod,
1288 struct btrfs_delayed_extent_op *extent_op)
1289{
1290 struct extent_buffer *leaf;
1291 struct btrfs_extent_item *ei;
1292 struct btrfs_extent_data_ref *dref = NULL;
1293 struct btrfs_shared_data_ref *sref = NULL;
1294 unsigned long ptr;
1295 unsigned long end;
1296 u32 item_size;
1297 int size;
1298 int type;
1299 int ret;
1300 u64 refs;
1301
1302 leaf = path->nodes[0];
1303 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1304 refs = btrfs_extent_refs(leaf, ei);
1305 WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0);
1306 refs += refs_to_mod;
1307 btrfs_set_extent_refs(leaf, ei, refs);
1308 if (extent_op)
1309 __run_delayed_extent_op(extent_op, leaf, ei);
1310
1311 type = btrfs_extent_inline_ref_type(leaf, iref);
1312
1313 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1314 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1315 refs = btrfs_extent_data_ref_count(leaf, dref);
1316 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1317 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1318 refs = btrfs_shared_data_ref_count(leaf, sref);
1319 } else {
1320 refs = 1;
1321 BUG_ON(refs_to_mod != -1);
1322 }
1323
1324 BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod);
1325 refs += refs_to_mod;
1326
1327 if (refs > 0) {
1328 if (type == BTRFS_EXTENT_DATA_REF_KEY)
1329 btrfs_set_extent_data_ref_count(leaf, dref, refs);
1330 else
1331 btrfs_set_shared_data_ref_count(leaf, sref, refs);
1332 } else {
1333 size = btrfs_extent_inline_ref_size(type);
1334 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1335 ptr = (unsigned long)iref;
1336 end = (unsigned long)ei + item_size;
1337 if (ptr + size < end)
1338 memmove_extent_buffer(leaf, ptr, ptr + size,
1339 end - ptr - size);
1340 item_size -= size;
1341 ret = btrfs_truncate_item(trans, root, path, item_size, 1);
1342 BUG_ON(ret);
1343 }
1344 btrfs_mark_buffer_dirty(leaf);
1345 return 0;
1346}
1347
1348static noinline_for_stack
1349int insert_inline_extent_backref(struct btrfs_trans_handle *trans,
1350 struct btrfs_root *root,
1351 struct btrfs_path *path,
1352 u64 bytenr, u64 num_bytes, u64 parent,
1353 u64 root_objectid, u64 owner,
1354 u64 offset, int refs_to_add,
1355 struct btrfs_delayed_extent_op *extent_op)
1356{
1357 struct btrfs_extent_inline_ref *iref;
1358 int ret;
1359
1360 ret = lookup_inline_extent_backref(trans, root, path, &iref,
1361 bytenr, num_bytes, parent,
1362 root_objectid, owner, offset, 1);
1363 if (ret == 0) {
1364 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID);
1365 ret = update_inline_extent_backref(trans, root, path, iref,
1366 refs_to_add, extent_op);
1367 } else if (ret == -ENOENT) {
1368 ret = setup_inline_extent_backref(trans, root, path, iref,
1369 parent, root_objectid,
1370 owner, offset, refs_to_add,
1371 extent_op);
1372 }
1373 return ret;
1374}
1375
1376static int insert_extent_backref(struct btrfs_trans_handle *trans,
1377 struct btrfs_root *root,
1378 struct btrfs_path *path,
1379 u64 bytenr, u64 parent, u64 root_objectid,
1380 u64 owner, u64 offset, int refs_to_add)
1381{
1382 int ret;
1383 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1384 BUG_ON(refs_to_add != 1);
1385 ret = insert_tree_block_ref(trans, root, path, bytenr,
1386 parent, root_objectid);
1387 } else {
1388 ret = insert_extent_data_ref(trans, root, path, bytenr,
1389 parent, root_objectid,
1390 owner, offset, refs_to_add);
1391 }
1392 return ret;
1393}
1394
1395static int remove_extent_backref(struct btrfs_trans_handle *trans,
1396 struct btrfs_root *root,
1397 struct btrfs_path *path,
1398 struct btrfs_extent_inline_ref *iref,
1399 int refs_to_drop, int is_data)
1400{
1401 int ret;
1402
1403 BUG_ON(!is_data && refs_to_drop != 1);
1404 if (iref) {
1405 ret = update_inline_extent_backref(trans, root, path, iref,
1406 -refs_to_drop, NULL);
1407 } else if (is_data) {
1408 ret = remove_extent_data_ref(trans, root, path, refs_to_drop);
1409 } else {
1410 ret = btrfs_del_item(trans, root, path);
1411 }
1412 return ret;
1413}
1414
Chris Mason4b4e25f2008-11-20 10:22:27 -05001415#ifdef BIO_RW_DISCARD
Chris Mason15916de2008-11-19 21:17:22 -05001416static void btrfs_issue_discard(struct block_device *bdev,
1417 u64 start, u64 len)
1418{
Chris Mason15916de2008-11-19 21:17:22 -05001419 blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_KERNEL);
Chris Mason15916de2008-11-19 21:17:22 -05001420}
Chris Mason4b4e25f2008-11-20 10:22:27 -05001421#endif
Chris Mason15916de2008-11-19 21:17:22 -05001422
Liu Hui1f3c79a2009-01-05 15:57:51 -05001423static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr,
1424 u64 num_bytes)
1425{
1426#ifdef BIO_RW_DISCARD
1427 int ret;
1428 u64 map_length = num_bytes;
1429 struct btrfs_multi_bio *multi = NULL;
1430
1431 /* Tell the block device(s) that the sectors can be discarded */
1432 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
1433 bytenr, &map_length, &multi, 0);
1434 if (!ret) {
1435 struct btrfs_bio_stripe *stripe = multi->stripes;
1436 int i;
1437
1438 if (map_length > num_bytes)
1439 map_length = num_bytes;
1440
1441 for (i = 0; i < multi->num_stripes; i++, stripe++) {
1442 btrfs_issue_discard(stripe->dev->bdev,
1443 stripe->physical,
1444 map_length);
1445 }
1446 kfree(multi);
1447 }
1448
1449 return ret;
1450#else
1451 return 0;
1452#endif
1453}
1454
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001455int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1456 struct btrfs_root *root,
1457 u64 bytenr, u64 num_bytes, u64 parent,
1458 u64 root_objectid, u64 owner, u64 offset)
Zheng Yan31840ae2008-09-23 13:14:14 -04001459{
1460 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001461 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID &&
1462 root_objectid == BTRFS_TREE_LOG_OBJECTID);
Zheng Yan31840ae2008-09-23 13:14:14 -04001463
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001464 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1465 ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes,
1466 parent, root_objectid, (int)owner,
1467 BTRFS_ADD_DELAYED_REF, NULL);
1468 } else {
1469 ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes,
1470 parent, root_objectid, owner, offset,
1471 BTRFS_ADD_DELAYED_REF, NULL);
1472 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001473 return ret;
1474}
1475
Chris Mason925baed2008-06-25 16:01:30 -04001476static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001477 struct btrfs_root *root,
1478 u64 bytenr, u64 num_bytes,
1479 u64 parent, u64 root_objectid,
1480 u64 owner, u64 offset, int refs_to_add,
1481 struct btrfs_delayed_extent_op *extent_op)
Chris Mason56bec292009-03-13 10:10:06 -04001482{
Chris Mason5caf2a02007-04-02 11:20:42 -04001483 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001484 struct extent_buffer *leaf;
Chris Mason234b63a2007-03-13 10:46:10 -04001485 struct btrfs_extent_item *item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001486 u64 refs;
1487 int ret;
1488 int err = 0;
Chris Mason037e6392007-03-07 11:50:24 -05001489
Chris Mason5caf2a02007-04-02 11:20:42 -04001490 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04001491 if (!path)
1492 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -04001493
Chris Mason3c12ac72008-04-21 12:01:38 -04001494 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04001495 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001496 /* this will setup the path even if it fails to insert the back ref */
1497 ret = insert_inline_extent_backref(trans, root->fs_info->extent_root,
1498 path, bytenr, num_bytes, parent,
1499 root_objectid, owner, offset,
1500 refs_to_add, extent_op);
1501 if (ret == 0)
1502 goto out;
Zheng Yan31840ae2008-09-23 13:14:14 -04001503
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001504 if (ret != -EAGAIN) {
1505 err = ret;
1506 goto out;
Chris Masonb9473432009-03-13 11:00:37 -04001507 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001508
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001509 leaf = path->nodes[0];
1510 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1511 refs = btrfs_extent_refs(leaf, item);
1512 btrfs_set_extent_refs(leaf, item, refs + refs_to_add);
1513 if (extent_op)
1514 __run_delayed_extent_op(extent_op, leaf, item);
Zheng Yan31840ae2008-09-23 13:14:14 -04001515
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001516 btrfs_mark_buffer_dirty(leaf);
Chris Mason5caf2a02007-04-02 11:20:42 -04001517 btrfs_release_path(root->fs_info->extent_root, path);
Chris Mason7bb86312007-12-11 09:25:06 -05001518
Chris Mason3c12ac72008-04-21 12:01:38 -04001519 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04001520 path->leave_spinning = 1;
1521
Chris Mason56bec292009-03-13 10:10:06 -04001522 /* now insert the actual backref */
Zheng Yan31840ae2008-09-23 13:14:14 -04001523 ret = insert_extent_backref(trans, root->fs_info->extent_root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001524 path, bytenr, parent, root_objectid,
1525 owner, offset, refs_to_add);
Chris Mason7bb86312007-12-11 09:25:06 -05001526 BUG_ON(ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001527out:
Chris Mason74493f72007-12-11 09:25:06 -05001528 btrfs_free_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001529 return err;
Chris Mason02217ed2007-03-02 16:08:05 -05001530}
1531
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001532static int run_delayed_data_ref(struct btrfs_trans_handle *trans,
1533 struct btrfs_root *root,
1534 struct btrfs_delayed_ref_node *node,
1535 struct btrfs_delayed_extent_op *extent_op,
1536 int insert_reserved)
Chris Masone9d0b132007-08-10 14:06:19 -04001537{
Chris Mason56bec292009-03-13 10:10:06 -04001538 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001539 struct btrfs_delayed_data_ref *ref;
1540 struct btrfs_key ins;
1541 u64 parent = 0;
1542 u64 ref_root = 0;
1543 u64 flags = 0;
Chris Mason56bec292009-03-13 10:10:06 -04001544
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001545 ins.objectid = node->bytenr;
1546 ins.offset = node->num_bytes;
1547 ins.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason56bec292009-03-13 10:10:06 -04001548
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001549 ref = btrfs_delayed_node_to_data_ref(node);
1550 if (node->type == BTRFS_SHARED_DATA_REF_KEY)
1551 parent = ref->parent;
1552 else
1553 ref_root = ref->root;
1554
1555 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
1556 if (extent_op) {
1557 BUG_ON(extent_op->update_key);
1558 flags |= extent_op->flags_to_set;
1559 }
1560 ret = alloc_reserved_file_extent(trans, root,
1561 parent, ref_root, flags,
1562 ref->objectid, ref->offset,
1563 &ins, node->ref_mod);
1564 update_reserved_extents(root, ins.objectid, ins.offset, 0);
1565 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
1566 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
1567 node->num_bytes, parent,
1568 ref_root, ref->objectid,
1569 ref->offset, node->ref_mod,
1570 extent_op);
1571 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
1572 ret = __btrfs_free_extent(trans, root, node->bytenr,
1573 node->num_bytes, parent,
1574 ref_root, ref->objectid,
1575 ref->offset, node->ref_mod,
1576 extent_op);
1577 } else {
1578 BUG();
1579 }
Chris Mason56bec292009-03-13 10:10:06 -04001580 return ret;
1581}
1582
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001583static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
1584 struct extent_buffer *leaf,
1585 struct btrfs_extent_item *ei)
1586{
1587 u64 flags = btrfs_extent_flags(leaf, ei);
1588 if (extent_op->update_flags) {
1589 flags |= extent_op->flags_to_set;
1590 btrfs_set_extent_flags(leaf, ei, flags);
1591 }
1592
1593 if (extent_op->update_key) {
1594 struct btrfs_tree_block_info *bi;
1595 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK));
1596 bi = (struct btrfs_tree_block_info *)(ei + 1);
1597 btrfs_set_tree_block_key(leaf, bi, &extent_op->key);
1598 }
1599}
1600
1601static int run_delayed_extent_op(struct btrfs_trans_handle *trans,
1602 struct btrfs_root *root,
1603 struct btrfs_delayed_ref_node *node,
1604 struct btrfs_delayed_extent_op *extent_op)
1605{
1606 struct btrfs_key key;
1607 struct btrfs_path *path;
1608 struct btrfs_extent_item *ei;
1609 struct extent_buffer *leaf;
1610 u32 item_size;
1611 int ret;
1612 int err = 0;
1613
1614 path = btrfs_alloc_path();
1615 if (!path)
1616 return -ENOMEM;
1617
1618 key.objectid = node->bytenr;
1619 key.type = BTRFS_EXTENT_ITEM_KEY;
1620 key.offset = node->num_bytes;
1621
1622 path->reada = 1;
1623 path->leave_spinning = 1;
1624 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key,
1625 path, 0, 1);
1626 if (ret < 0) {
1627 err = ret;
1628 goto out;
1629 }
1630 if (ret > 0) {
1631 err = -EIO;
1632 goto out;
1633 }
1634
1635 leaf = path->nodes[0];
1636 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1637#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1638 if (item_size < sizeof(*ei)) {
1639 ret = convert_extent_item_v0(trans, root->fs_info->extent_root,
1640 path, (u64)-1, 0);
1641 if (ret < 0) {
1642 err = ret;
1643 goto out;
1644 }
1645 leaf = path->nodes[0];
1646 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1647 }
1648#endif
1649 BUG_ON(item_size < sizeof(*ei));
1650 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1651 __run_delayed_extent_op(extent_op, leaf, ei);
1652
1653 btrfs_mark_buffer_dirty(leaf);
1654out:
1655 btrfs_free_path(path);
1656 return err;
1657}
1658
1659static int run_delayed_tree_ref(struct btrfs_trans_handle *trans,
1660 struct btrfs_root *root,
1661 struct btrfs_delayed_ref_node *node,
1662 struct btrfs_delayed_extent_op *extent_op,
1663 int insert_reserved)
1664{
1665 int ret = 0;
1666 struct btrfs_delayed_tree_ref *ref;
1667 struct btrfs_key ins;
1668 u64 parent = 0;
1669 u64 ref_root = 0;
1670
1671 ins.objectid = node->bytenr;
1672 ins.offset = node->num_bytes;
1673 ins.type = BTRFS_EXTENT_ITEM_KEY;
1674
1675 ref = btrfs_delayed_node_to_tree_ref(node);
1676 if (node->type == BTRFS_SHARED_BLOCK_REF_KEY)
1677 parent = ref->parent;
1678 else
1679 ref_root = ref->root;
1680
1681 BUG_ON(node->ref_mod != 1);
1682 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
1683 BUG_ON(!extent_op || !extent_op->update_flags ||
1684 !extent_op->update_key);
1685 ret = alloc_reserved_tree_block(trans, root,
1686 parent, ref_root,
1687 extent_op->flags_to_set,
1688 &extent_op->key,
1689 ref->level, &ins);
1690 update_reserved_extents(root, ins.objectid, ins.offset, 0);
1691 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
1692 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
1693 node->num_bytes, parent, ref_root,
1694 ref->level, 0, 1, extent_op);
1695 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
1696 ret = __btrfs_free_extent(trans, root, node->bytenr,
1697 node->num_bytes, parent, ref_root,
1698 ref->level, 0, 1, extent_op);
1699 } else {
1700 BUG();
1701 }
1702 return ret;
1703}
1704
1705
Chris Mason56bec292009-03-13 10:10:06 -04001706/* helper function to actually process a single delayed ref entry */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001707static int run_one_delayed_ref(struct btrfs_trans_handle *trans,
1708 struct btrfs_root *root,
1709 struct btrfs_delayed_ref_node *node,
1710 struct btrfs_delayed_extent_op *extent_op,
1711 int insert_reserved)
Chris Mason56bec292009-03-13 10:10:06 -04001712{
Josef Bacikeb099672009-02-12 09:27:38 -05001713 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001714 if (btrfs_delayed_ref_is_head(node)) {
Chris Mason56bec292009-03-13 10:10:06 -04001715 struct btrfs_delayed_ref_head *head;
1716 /*
1717 * we've hit the end of the chain and we were supposed
1718 * to insert this extent into the tree. But, it got
1719 * deleted before we ever needed to insert it, so all
1720 * we have to do is clean up the accounting
1721 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001722 BUG_ON(extent_op);
1723 head = btrfs_delayed_node_to_head(node);
Chris Mason56bec292009-03-13 10:10:06 -04001724 if (insert_reserved) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001725 if (head->is_data) {
1726 ret = btrfs_del_csums(trans, root,
1727 node->bytenr,
1728 node->num_bytes);
1729 BUG_ON(ret);
1730 }
1731 btrfs_update_pinned_extents(root, node->bytenr,
1732 node->num_bytes, 1);
Chris Mason56bec292009-03-13 10:10:06 -04001733 update_reserved_extents(root, node->bytenr,
1734 node->num_bytes, 0);
1735 }
Chris Mason56bec292009-03-13 10:10:06 -04001736 mutex_unlock(&head->mutex);
1737 return 0;
1738 }
Josef Bacikeb099672009-02-12 09:27:38 -05001739
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001740 if (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
1741 node->type == BTRFS_SHARED_BLOCK_REF_KEY)
1742 ret = run_delayed_tree_ref(trans, root, node, extent_op,
1743 insert_reserved);
1744 else if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
1745 node->type == BTRFS_SHARED_DATA_REF_KEY)
1746 ret = run_delayed_data_ref(trans, root, node, extent_op,
1747 insert_reserved);
1748 else
1749 BUG();
1750 return ret;
Chris Masone9d0b132007-08-10 14:06:19 -04001751}
1752
Chris Mason56bec292009-03-13 10:10:06 -04001753static noinline struct btrfs_delayed_ref_node *
1754select_delayed_ref(struct btrfs_delayed_ref_head *head)
Chris Masona28ec192007-03-06 20:08:01 -05001755{
Chris Mason56bec292009-03-13 10:10:06 -04001756 struct rb_node *node;
1757 struct btrfs_delayed_ref_node *ref;
1758 int action = BTRFS_ADD_DELAYED_REF;
1759again:
1760 /*
1761 * select delayed ref of type BTRFS_ADD_DELAYED_REF first.
1762 * this prevents ref count from going down to zero when
1763 * there still are pending delayed ref.
1764 */
1765 node = rb_prev(&head->node.rb_node);
1766 while (1) {
1767 if (!node)
1768 break;
1769 ref = rb_entry(node, struct btrfs_delayed_ref_node,
1770 rb_node);
1771 if (ref->bytenr != head->node.bytenr)
1772 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001773 if (ref->action == action)
Chris Mason56bec292009-03-13 10:10:06 -04001774 return ref;
1775 node = rb_prev(node);
Chris Mason5f39d392007-10-15 16:14:19 -04001776 }
Chris Mason56bec292009-03-13 10:10:06 -04001777 if (action == BTRFS_ADD_DELAYED_REF) {
1778 action = BTRFS_DROP_DELAYED_REF;
1779 goto again;
1780 }
1781 return NULL;
1782}
1783
Chris Masonc3e69d52009-03-13 10:17:05 -04001784static noinline int run_clustered_refs(struct btrfs_trans_handle *trans,
1785 struct btrfs_root *root,
1786 struct list_head *cluster)
Chris Mason56bec292009-03-13 10:10:06 -04001787{
Chris Mason56bec292009-03-13 10:10:06 -04001788 struct btrfs_delayed_ref_root *delayed_refs;
1789 struct btrfs_delayed_ref_node *ref;
1790 struct btrfs_delayed_ref_head *locked_ref = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001791 struct btrfs_delayed_extent_op *extent_op;
Chris Mason56bec292009-03-13 10:10:06 -04001792 int ret;
Chris Masonc3e69d52009-03-13 10:17:05 -04001793 int count = 0;
Chris Mason56bec292009-03-13 10:10:06 -04001794 int must_insert_reserved = 0;
Chris Mason56bec292009-03-13 10:10:06 -04001795
1796 delayed_refs = &trans->transaction->delayed_refs;
Chris Mason56bec292009-03-13 10:10:06 -04001797 while (1) {
1798 if (!locked_ref) {
Chris Masonc3e69d52009-03-13 10:17:05 -04001799 /* pick a new head ref from the cluster list */
1800 if (list_empty(cluster))
Chris Mason56bec292009-03-13 10:10:06 -04001801 break;
Chris Mason56bec292009-03-13 10:10:06 -04001802
Chris Masonc3e69d52009-03-13 10:17:05 -04001803 locked_ref = list_entry(cluster->next,
1804 struct btrfs_delayed_ref_head, cluster);
1805
1806 /* grab the lock that says we are going to process
1807 * all the refs for this head */
1808 ret = btrfs_delayed_ref_lock(trans, locked_ref);
1809
1810 /*
1811 * we may have dropped the spin lock to get the head
1812 * mutex lock, and that might have given someone else
1813 * time to free the head. If that's true, it has been
1814 * removed from our list and we can move on.
1815 */
1816 if (ret == -EAGAIN) {
1817 locked_ref = NULL;
1818 count++;
1819 continue;
Chris Mason56bec292009-03-13 10:10:06 -04001820 }
1821 }
1822
1823 /*
1824 * record the must insert reserved flag before we
1825 * drop the spin lock.
1826 */
1827 must_insert_reserved = locked_ref->must_insert_reserved;
1828 locked_ref->must_insert_reserved = 0;
1829
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001830 extent_op = locked_ref->extent_op;
1831 locked_ref->extent_op = NULL;
1832
Chris Mason56bec292009-03-13 10:10:06 -04001833 /*
1834 * locked_ref is the head node, so we have to go one
1835 * node back for any delayed ref updates
1836 */
Chris Mason56bec292009-03-13 10:10:06 -04001837 ref = select_delayed_ref(locked_ref);
1838 if (!ref) {
1839 /* All delayed refs have been processed, Go ahead
1840 * and send the head node to run_one_delayed_ref,
1841 * so that any accounting fixes can happen
1842 */
1843 ref = &locked_ref->node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001844
1845 if (extent_op && must_insert_reserved) {
1846 kfree(extent_op);
1847 extent_op = NULL;
1848 }
1849
1850 if (extent_op) {
1851 spin_unlock(&delayed_refs->lock);
1852
1853 ret = run_delayed_extent_op(trans, root,
1854 ref, extent_op);
1855 BUG_ON(ret);
1856 kfree(extent_op);
1857
1858 cond_resched();
1859 spin_lock(&delayed_refs->lock);
1860 continue;
1861 }
1862
Chris Masonc3e69d52009-03-13 10:17:05 -04001863 list_del_init(&locked_ref->cluster);
Chris Mason56bec292009-03-13 10:10:06 -04001864 locked_ref = NULL;
1865 }
1866
1867 ref->in_tree = 0;
1868 rb_erase(&ref->rb_node, &delayed_refs->root);
1869 delayed_refs->num_entries--;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001870
Chris Mason56bec292009-03-13 10:10:06 -04001871 spin_unlock(&delayed_refs->lock);
1872
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001873 ret = run_one_delayed_ref(trans, root, ref, extent_op,
Chris Mason56bec292009-03-13 10:10:06 -04001874 must_insert_reserved);
1875 BUG_ON(ret);
Chris Mason56bec292009-03-13 10:10:06 -04001876
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001877 btrfs_put_delayed_ref(ref);
1878 kfree(extent_op);
Chris Masonc3e69d52009-03-13 10:17:05 -04001879 count++;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001880
Chris Mason1887be62009-03-13 10:11:24 -04001881 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04001882 spin_lock(&delayed_refs->lock);
1883 }
Chris Masonc3e69d52009-03-13 10:17:05 -04001884 return count;
1885}
1886
1887/*
1888 * this starts processing the delayed reference count updates and
1889 * extent insertions we have queued up so far. count can be
1890 * 0, which means to process everything in the tree at the start
1891 * of the run (but not newly added entries), or it can be some target
1892 * number you'd like to process.
1893 */
1894int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
1895 struct btrfs_root *root, unsigned long count)
1896{
1897 struct rb_node *node;
1898 struct btrfs_delayed_ref_root *delayed_refs;
1899 struct btrfs_delayed_ref_node *ref;
1900 struct list_head cluster;
1901 int ret;
1902 int run_all = count == (unsigned long)-1;
1903 int run_most = 0;
1904
1905 if (root == root->fs_info->extent_root)
1906 root = root->fs_info->tree_root;
1907
1908 delayed_refs = &trans->transaction->delayed_refs;
1909 INIT_LIST_HEAD(&cluster);
1910again:
1911 spin_lock(&delayed_refs->lock);
1912 if (count == 0) {
1913 count = delayed_refs->num_entries * 2;
1914 run_most = 1;
1915 }
1916 while (1) {
1917 if (!(run_all || run_most) &&
1918 delayed_refs->num_heads_ready < 64)
1919 break;
1920
1921 /*
1922 * go find something we can process in the rbtree. We start at
1923 * the beginning of the tree, and then build a cluster
1924 * of refs to process starting at the first one we are able to
1925 * lock
1926 */
1927 ret = btrfs_find_ref_cluster(trans, &cluster,
1928 delayed_refs->run_delayed_start);
1929 if (ret)
1930 break;
1931
1932 ret = run_clustered_refs(trans, root, &cluster);
1933 BUG_ON(ret < 0);
1934
1935 count -= min_t(unsigned long, ret, count);
1936
1937 if (count == 0)
1938 break;
1939 }
1940
Chris Mason56bec292009-03-13 10:10:06 -04001941 if (run_all) {
Chris Mason56bec292009-03-13 10:10:06 -04001942 node = rb_first(&delayed_refs->root);
Chris Masonc3e69d52009-03-13 10:17:05 -04001943 if (!node)
Chris Mason56bec292009-03-13 10:10:06 -04001944 goto out;
Chris Masonc3e69d52009-03-13 10:17:05 -04001945 count = (unsigned long)-1;
Chris Mason56bec292009-03-13 10:10:06 -04001946
1947 while (node) {
1948 ref = rb_entry(node, struct btrfs_delayed_ref_node,
1949 rb_node);
1950 if (btrfs_delayed_ref_is_head(ref)) {
1951 struct btrfs_delayed_ref_head *head;
1952
1953 head = btrfs_delayed_node_to_head(ref);
1954 atomic_inc(&ref->refs);
1955
1956 spin_unlock(&delayed_refs->lock);
1957 mutex_lock(&head->mutex);
1958 mutex_unlock(&head->mutex);
1959
1960 btrfs_put_delayed_ref(ref);
Chris Mason1887be62009-03-13 10:11:24 -04001961 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04001962 goto again;
1963 }
1964 node = rb_next(node);
1965 }
1966 spin_unlock(&delayed_refs->lock);
Chris Mason56bec292009-03-13 10:10:06 -04001967 schedule_timeout(1);
1968 goto again;
1969 }
Chris Mason54aa1f42007-06-22 14:16:25 -04001970out:
Chris Masonc3e69d52009-03-13 10:17:05 -04001971 spin_unlock(&delayed_refs->lock);
Chris Masona28ec192007-03-06 20:08:01 -05001972 return 0;
1973}
1974
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001975int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
1976 struct btrfs_root *root,
1977 u64 bytenr, u64 num_bytes, u64 flags,
1978 int is_data)
1979{
1980 struct btrfs_delayed_extent_op *extent_op;
1981 int ret;
1982
1983 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
1984 if (!extent_op)
1985 return -ENOMEM;
1986
1987 extent_op->flags_to_set = flags;
1988 extent_op->update_flags = 1;
1989 extent_op->update_key = 0;
1990 extent_op->is_data = is_data ? 1 : 0;
1991
1992 ret = btrfs_add_delayed_extent_op(trans, bytenr, num_bytes, extent_op);
1993 if (ret)
1994 kfree(extent_op);
1995 return ret;
1996}
1997
1998static noinline int check_delayed_ref(struct btrfs_trans_handle *trans,
1999 struct btrfs_root *root,
2000 struct btrfs_path *path,
2001 u64 objectid, u64 offset, u64 bytenr)
2002{
2003 struct btrfs_delayed_ref_head *head;
2004 struct btrfs_delayed_ref_node *ref;
2005 struct btrfs_delayed_data_ref *data_ref;
2006 struct btrfs_delayed_ref_root *delayed_refs;
2007 struct rb_node *node;
2008 int ret = 0;
2009
2010 ret = -ENOENT;
2011 delayed_refs = &trans->transaction->delayed_refs;
2012 spin_lock(&delayed_refs->lock);
2013 head = btrfs_find_delayed_ref_head(trans, bytenr);
2014 if (!head)
2015 goto out;
2016
2017 if (!mutex_trylock(&head->mutex)) {
2018 atomic_inc(&head->node.refs);
2019 spin_unlock(&delayed_refs->lock);
2020
2021 btrfs_release_path(root->fs_info->extent_root, path);
2022
2023 mutex_lock(&head->mutex);
2024 mutex_unlock(&head->mutex);
2025 btrfs_put_delayed_ref(&head->node);
2026 return -EAGAIN;
2027 }
2028
2029 node = rb_prev(&head->node.rb_node);
2030 if (!node)
2031 goto out_unlock;
2032
2033 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2034
2035 if (ref->bytenr != bytenr)
2036 goto out_unlock;
2037
2038 ret = 1;
2039 if (ref->type != BTRFS_EXTENT_DATA_REF_KEY)
2040 goto out_unlock;
2041
2042 data_ref = btrfs_delayed_node_to_data_ref(ref);
2043
2044 node = rb_prev(node);
2045 if (node) {
2046 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2047 if (ref->bytenr == bytenr)
2048 goto out_unlock;
2049 }
2050
2051 if (data_ref->root != root->root_key.objectid ||
2052 data_ref->objectid != objectid || data_ref->offset != offset)
2053 goto out_unlock;
2054
2055 ret = 0;
2056out_unlock:
2057 mutex_unlock(&head->mutex);
2058out:
2059 spin_unlock(&delayed_refs->lock);
2060 return ret;
2061}
2062
2063static noinline int check_committed_ref(struct btrfs_trans_handle *trans,
2064 struct btrfs_root *root,
2065 struct btrfs_path *path,
2066 u64 objectid, u64 offset, u64 bytenr)
Chris Masonbe20aa92007-12-17 20:14:01 -05002067{
2068 struct btrfs_root *extent_root = root->fs_info->extent_root;
Yan Zhengf321e492008-07-30 09:26:11 -04002069 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002070 struct btrfs_extent_data_ref *ref;
2071 struct btrfs_extent_inline_ref *iref;
2072 struct btrfs_extent_item *ei;
Chris Masonbe20aa92007-12-17 20:14:01 -05002073 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002074 u32 item_size;
Yan Zhengf321e492008-07-30 09:26:11 -04002075 int ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05002076
Chris Masonbe20aa92007-12-17 20:14:01 -05002077 key.objectid = bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -04002078 key.offset = (u64)-1;
Yan Zhengf321e492008-07-30 09:26:11 -04002079 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masonbe20aa92007-12-17 20:14:01 -05002080
Chris Masonbe20aa92007-12-17 20:14:01 -05002081 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
2082 if (ret < 0)
2083 goto out;
2084 BUG_ON(ret == 0);
Yan Zheng80ff3852008-10-30 14:20:02 -04002085
2086 ret = -ENOENT;
2087 if (path->slots[0] == 0)
Zheng Yan31840ae2008-09-23 13:14:14 -04002088 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002089
Zheng Yan31840ae2008-09-23 13:14:14 -04002090 path->slots[0]--;
Yan Zhengf321e492008-07-30 09:26:11 -04002091 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002092 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05002093
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002094 if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY)
Chris Masonbe20aa92007-12-17 20:14:01 -05002095 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002096
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002097 ret = 1;
2098 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2099#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2100 if (item_size < sizeof(*ei)) {
2101 WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0));
2102 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002103 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002104#endif
2105 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2106
2107 if (item_size != sizeof(*ei) +
2108 btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY))
2109 goto out;
2110
2111 if (btrfs_extent_generation(leaf, ei) <=
2112 btrfs_root_last_snapshot(&root->root_item))
2113 goto out;
2114
2115 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
2116 if (btrfs_extent_inline_ref_type(leaf, iref) !=
2117 BTRFS_EXTENT_DATA_REF_KEY)
2118 goto out;
2119
2120 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
2121 if (btrfs_extent_refs(leaf, ei) !=
2122 btrfs_extent_data_ref_count(leaf, ref) ||
2123 btrfs_extent_data_ref_root(leaf, ref) !=
2124 root->root_key.objectid ||
2125 btrfs_extent_data_ref_objectid(leaf, ref) != objectid ||
2126 btrfs_extent_data_ref_offset(leaf, ref) != offset)
2127 goto out;
2128
Yan Zhengf321e492008-07-30 09:26:11 -04002129 ret = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05002130out:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002131 return ret;
2132}
2133
2134int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
2135 struct btrfs_root *root,
2136 u64 objectid, u64 offset, u64 bytenr)
2137{
2138 struct btrfs_path *path;
2139 int ret;
2140 int ret2;
2141
2142 path = btrfs_alloc_path();
2143 if (!path)
2144 return -ENOENT;
2145
2146 do {
2147 ret = check_committed_ref(trans, root, path, objectid,
2148 offset, bytenr);
2149 if (ret && ret != -ENOENT)
2150 goto out;
2151
2152 ret2 = check_delayed_ref(trans, root, path, objectid,
2153 offset, bytenr);
2154 } while (ret2 == -EAGAIN);
2155
2156 if (ret2 && ret2 != -ENOENT) {
2157 ret = ret2;
2158 goto out;
2159 }
2160
2161 if (ret != -ENOENT || ret2 != -ENOENT)
2162 ret = 0;
2163out:
Yan Zhengf321e492008-07-30 09:26:11 -04002164 btrfs_free_path(path);
2165 return ret;
2166}
2167
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002168#if 0
Zheng Yan31840ae2008-09-23 13:14:14 -04002169int btrfs_cache_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2170 struct extent_buffer *buf, u32 nr_extents)
Chris Mason02217ed2007-03-02 16:08:05 -05002171{
Chris Mason5f39d392007-10-15 16:14:19 -04002172 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -04002173 struct btrfs_file_extent_item *fi;
Zheng Yane4657682008-09-26 10:04:53 -04002174 u64 root_gen;
2175 u32 nritems;
Chris Mason02217ed2007-03-02 16:08:05 -05002176 int i;
Chris Masondb945352007-10-15 16:15:53 -04002177 int level;
Zheng Yan31840ae2008-09-23 13:14:14 -04002178 int ret = 0;
Zheng Yane4657682008-09-26 10:04:53 -04002179 int shared = 0;
Chris Masona28ec192007-03-06 20:08:01 -05002180
Chris Mason3768f362007-03-13 16:47:54 -04002181 if (!root->ref_cows)
Chris Masona28ec192007-03-06 20:08:01 -05002182 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002183
Zheng Yane4657682008-09-26 10:04:53 -04002184 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
2185 shared = 0;
2186 root_gen = root->root_key.offset;
2187 } else {
2188 shared = 1;
2189 root_gen = trans->transid - 1;
2190 }
2191
Chris Masondb945352007-10-15 16:15:53 -04002192 level = btrfs_header_level(buf);
Chris Mason5f39d392007-10-15 16:14:19 -04002193 nritems = btrfs_header_nritems(buf);
Chris Mason4a096752008-07-21 10:29:44 -04002194
Zheng Yan31840ae2008-09-23 13:14:14 -04002195 if (level == 0) {
Yan Zheng31153d82008-07-28 15:32:19 -04002196 struct btrfs_leaf_ref *ref;
2197 struct btrfs_extent_info *info;
2198
Zheng Yan31840ae2008-09-23 13:14:14 -04002199 ref = btrfs_alloc_leaf_ref(root, nr_extents);
Yan Zheng31153d82008-07-28 15:32:19 -04002200 if (!ref) {
Zheng Yan31840ae2008-09-23 13:14:14 -04002201 ret = -ENOMEM;
Yan Zheng31153d82008-07-28 15:32:19 -04002202 goto out;
2203 }
2204
Zheng Yane4657682008-09-26 10:04:53 -04002205 ref->root_gen = root_gen;
Yan Zheng31153d82008-07-28 15:32:19 -04002206 ref->bytenr = buf->start;
2207 ref->owner = btrfs_header_owner(buf);
2208 ref->generation = btrfs_header_generation(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04002209 ref->nritems = nr_extents;
Yan Zheng31153d82008-07-28 15:32:19 -04002210 info = ref->extents;
Yanbcc63ab2008-07-30 16:29:20 -04002211
Zheng Yan31840ae2008-09-23 13:14:14 -04002212 for (i = 0; nr_extents > 0 && i < nritems; i++) {
Yan Zheng31153d82008-07-28 15:32:19 -04002213 u64 disk_bytenr;
2214 btrfs_item_key_to_cpu(buf, &key, i);
2215 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
2216 continue;
2217 fi = btrfs_item_ptr(buf, i,
2218 struct btrfs_file_extent_item);
2219 if (btrfs_file_extent_type(buf, fi) ==
2220 BTRFS_FILE_EXTENT_INLINE)
2221 continue;
2222 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
2223 if (disk_bytenr == 0)
2224 continue;
2225
2226 info->bytenr = disk_bytenr;
2227 info->num_bytes =
2228 btrfs_file_extent_disk_num_bytes(buf, fi);
2229 info->objectid = key.objectid;
2230 info->offset = key.offset;
2231 info++;
2232 }
2233
Zheng Yane4657682008-09-26 10:04:53 -04002234 ret = btrfs_add_leaf_ref(root, ref, shared);
Yan Zheng5b84e8d2008-10-09 11:46:19 -04002235 if (ret == -EEXIST && shared) {
2236 struct btrfs_leaf_ref *old;
2237 old = btrfs_lookup_leaf_ref(root, ref->bytenr);
2238 BUG_ON(!old);
2239 btrfs_remove_leaf_ref(root, old);
2240 btrfs_free_leaf_ref(root, old);
2241 ret = btrfs_add_leaf_ref(root, ref, shared);
2242 }
Yan Zheng31153d82008-07-28 15:32:19 -04002243 WARN_ON(ret);
Yanbcc63ab2008-07-30 16:29:20 -04002244 btrfs_free_leaf_ref(root, ref);
Yan Zheng31153d82008-07-28 15:32:19 -04002245 }
2246out:
Zheng Yan31840ae2008-09-23 13:14:14 -04002247 return ret;
2248}
2249
Chris Masonb7a9f292009-02-04 09:23:45 -05002250/* when a block goes through cow, we update the reference counts of
2251 * everything that block points to. The internal pointers of the block
2252 * can be in just about any order, and it is likely to have clusters of
2253 * things that are close together and clusters of things that are not.
2254 *
2255 * To help reduce the seeks that come with updating all of these reference
2256 * counts, sort them by byte number before actual updates are done.
2257 *
2258 * struct refsort is used to match byte number to slot in the btree block.
2259 * we sort based on the byte number and then use the slot to actually
2260 * find the item.
Chris Masonbd56b302009-02-04 09:27:02 -05002261 *
2262 * struct refsort is smaller than strcut btrfs_item and smaller than
2263 * struct btrfs_key_ptr. Since we're currently limited to the page size
2264 * for a btree block, there's no way for a kmalloc of refsorts for a
2265 * single node to be bigger than a page.
Chris Masonb7a9f292009-02-04 09:23:45 -05002266 */
2267struct refsort {
2268 u64 bytenr;
2269 u32 slot;
2270};
2271
2272/*
2273 * for passing into sort()
2274 */
2275static int refsort_cmp(const void *a_void, const void *b_void)
2276{
2277 const struct refsort *a = a_void;
2278 const struct refsort *b = b_void;
2279
2280 if (a->bytenr < b->bytenr)
2281 return -1;
2282 if (a->bytenr > b->bytenr)
2283 return 1;
2284 return 0;
2285}
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002286#endif
Chris Masonb7a9f292009-02-04 09:23:45 -05002287
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002288static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
Chris Masonb7a9f292009-02-04 09:23:45 -05002289 struct btrfs_root *root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002290 struct extent_buffer *buf,
2291 int full_backref, int inc)
Zheng Yan31840ae2008-09-23 13:14:14 -04002292{
2293 u64 bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002294 u64 num_bytes;
2295 u64 parent;
Zheng Yan31840ae2008-09-23 13:14:14 -04002296 u64 ref_root;
Zheng Yan31840ae2008-09-23 13:14:14 -04002297 u32 nritems;
Zheng Yan31840ae2008-09-23 13:14:14 -04002298 struct btrfs_key key;
2299 struct btrfs_file_extent_item *fi;
2300 int i;
2301 int level;
2302 int ret = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04002303 int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002304 u64, u64, u64, u64, u64, u64);
Zheng Yan31840ae2008-09-23 13:14:14 -04002305
2306 ref_root = btrfs_header_owner(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04002307 nritems = btrfs_header_nritems(buf);
2308 level = btrfs_header_level(buf);
2309
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002310 if (!root->ref_cows && level == 0)
2311 return 0;
Chris Masonb7a9f292009-02-04 09:23:45 -05002312
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002313 if (inc)
2314 process_func = btrfs_inc_extent_ref;
2315 else
2316 process_func = btrfs_free_extent;
Zheng Yan31840ae2008-09-23 13:14:14 -04002317
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002318 if (full_backref)
2319 parent = buf->start;
2320 else
2321 parent = 0;
2322
Zheng Yan31840ae2008-09-23 13:14:14 -04002323 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04002324 if (level == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002325 btrfs_item_key_to_cpu(buf, &key, i);
2326 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -04002327 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04002328 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -04002329 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002330 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -04002331 BTRFS_FILE_EXTENT_INLINE)
2332 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04002333 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
2334 if (bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -04002335 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04002336
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002337 num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
2338 key.offset -= btrfs_file_extent_offset(buf, fi);
2339 ret = process_func(trans, root, bytenr, num_bytes,
2340 parent, ref_root, key.objectid,
2341 key.offset);
2342 if (ret)
2343 goto fail;
Chris Masonb7a9f292009-02-04 09:23:45 -05002344 } else {
2345 bytenr = btrfs_node_blockptr(buf, i);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002346 num_bytes = btrfs_level_size(root, level - 1);
2347 ret = process_func(trans, root, bytenr, num_bytes,
2348 parent, ref_root, level - 1, 0);
2349 if (ret)
Zheng Yan31840ae2008-09-23 13:14:14 -04002350 goto fail;
Chris Mason54aa1f42007-06-22 14:16:25 -04002351 }
2352 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002353 return 0;
2354fail:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002355 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -04002356 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05002357}
2358
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002359int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2360 struct extent_buffer *buf, int full_backref)
Zheng Yan31840ae2008-09-23 13:14:14 -04002361{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002362 return __btrfs_mod_ref(trans, root, buf, full_backref, 1);
2363}
Zheng Yan31840ae2008-09-23 13:14:14 -04002364
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002365int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2366 struct extent_buffer *buf, int full_backref)
2367{
2368 return __btrfs_mod_ref(trans, root, buf, full_backref, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04002369}
2370
Chris Mason9078a3e2007-04-26 16:46:15 -04002371static int write_one_cache_group(struct btrfs_trans_handle *trans,
2372 struct btrfs_root *root,
2373 struct btrfs_path *path,
2374 struct btrfs_block_group_cache *cache)
2375{
2376 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04002377 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04002378 unsigned long bi;
2379 struct extent_buffer *leaf;
Chris Mason9078a3e2007-04-26 16:46:15 -04002380
Chris Mason9078a3e2007-04-26 16:46:15 -04002381 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04002382 if (ret < 0)
2383 goto fail;
Chris Mason9078a3e2007-04-26 16:46:15 -04002384 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04002385
2386 leaf = path->nodes[0];
2387 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
2388 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
2389 btrfs_mark_buffer_dirty(leaf);
Chris Mason9078a3e2007-04-26 16:46:15 -04002390 btrfs_release_path(extent_root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -04002391fail:
Chris Mason9078a3e2007-04-26 16:46:15 -04002392 if (ret)
2393 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04002394 return 0;
2395
2396}
2397
Chris Mason96b51792007-10-15 16:15:19 -04002398int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
2399 struct btrfs_root *root)
Chris Mason9078a3e2007-04-26 16:46:15 -04002400{
Josef Bacik0f9dd462008-09-23 13:14:11 -04002401 struct btrfs_block_group_cache *cache, *entry;
2402 struct rb_node *n;
Chris Mason9078a3e2007-04-26 16:46:15 -04002403 int err = 0;
2404 int werr = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04002405 struct btrfs_path *path;
Chris Mason96b51792007-10-15 16:15:19 -04002406 u64 last = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04002407
2408 path = btrfs_alloc_path();
2409 if (!path)
2410 return -ENOMEM;
2411
Chris Masond3977122009-01-05 21:25:51 -05002412 while (1) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04002413 cache = NULL;
2414 spin_lock(&root->fs_info->block_group_cache_lock);
2415 for (n = rb_first(&root->fs_info->block_group_cache_tree);
2416 n; n = rb_next(n)) {
2417 entry = rb_entry(n, struct btrfs_block_group_cache,
2418 cache_node);
2419 if (entry->dirty) {
2420 cache = entry;
2421 break;
2422 }
2423 }
2424 spin_unlock(&root->fs_info->block_group_cache_lock);
2425
2426 if (!cache)
Chris Mason9078a3e2007-04-26 16:46:15 -04002427 break;
Chris Mason54aa1f42007-06-22 14:16:25 -04002428
Zheng Yane8569812008-09-26 10:05:48 -04002429 cache->dirty = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002430 last += cache->key.offset;
2431
Chris Mason96b51792007-10-15 16:15:19 -04002432 err = write_one_cache_group(trans, root,
2433 path, cache);
2434 /*
2435 * if we fail to write the cache group, we want
2436 * to keep it marked dirty in hopes that a later
2437 * write will work
2438 */
2439 if (err) {
2440 werr = err;
2441 continue;
Chris Mason9078a3e2007-04-26 16:46:15 -04002442 }
2443 }
2444 btrfs_free_path(path);
2445 return werr;
2446}
2447
Yan Zhengd2fb3432008-12-11 16:30:39 -05002448int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr)
2449{
2450 struct btrfs_block_group_cache *block_group;
2451 int readonly = 0;
2452
2453 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
2454 if (!block_group || block_group->ro)
2455 readonly = 1;
2456 if (block_group)
Chris Masonfa9c0d72009-04-03 09:47:43 -04002457 btrfs_put_block_group(block_group);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002458 return readonly;
2459}
2460
Chris Mason593060d2008-03-25 16:50:33 -04002461static int update_space_info(struct btrfs_fs_info *info, u64 flags,
2462 u64 total_bytes, u64 bytes_used,
2463 struct btrfs_space_info **space_info)
2464{
2465 struct btrfs_space_info *found;
2466
2467 found = __find_space_info(info, flags);
2468 if (found) {
Josef Bacik25179202008-10-29 14:49:05 -04002469 spin_lock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04002470 found->total_bytes += total_bytes;
2471 found->bytes_used += bytes_used;
Chris Mason8f18cf12008-04-25 16:53:30 -04002472 found->full = 0;
Josef Bacik25179202008-10-29 14:49:05 -04002473 spin_unlock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04002474 *space_info = found;
2475 return 0;
2476 }
Yan Zhengc146afa2008-11-12 14:34:12 -05002477 found = kzalloc(sizeof(*found), GFP_NOFS);
Chris Mason593060d2008-03-25 16:50:33 -04002478 if (!found)
2479 return -ENOMEM;
2480
Josef Bacik0f9dd462008-09-23 13:14:11 -04002481 INIT_LIST_HEAD(&found->block_groups);
Josef Bacik80eb2342008-10-29 14:49:05 -04002482 init_rwsem(&found->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002483 spin_lock_init(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04002484 found->flags = flags;
2485 found->total_bytes = total_bytes;
2486 found->bytes_used = bytes_used;
2487 found->bytes_pinned = 0;
Zheng Yane8569812008-09-26 10:05:48 -04002488 found->bytes_reserved = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05002489 found->bytes_readonly = 0;
Josef Bacik6a632092009-02-20 11:00:09 -05002490 found->bytes_delalloc = 0;
Chris Mason593060d2008-03-25 16:50:33 -04002491 found->full = 0;
Chris Mason0ef3e662008-05-24 14:04:53 -04002492 found->force_alloc = 0;
Chris Mason593060d2008-03-25 16:50:33 -04002493 *space_info = found;
Chris Mason4184ea72009-03-10 12:39:20 -04002494 list_add_rcu(&found->list, &info->space_info);
Chris Mason593060d2008-03-25 16:50:33 -04002495 return 0;
2496}
2497
Chris Mason8790d502008-04-03 16:29:03 -04002498static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
2499{
2500 u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
Chris Mason611f0e02008-04-03 16:29:03 -04002501 BTRFS_BLOCK_GROUP_RAID1 |
Chris Mason321aecc2008-04-16 10:49:51 -04002502 BTRFS_BLOCK_GROUP_RAID10 |
Chris Mason611f0e02008-04-03 16:29:03 -04002503 BTRFS_BLOCK_GROUP_DUP);
Chris Mason8790d502008-04-03 16:29:03 -04002504 if (extra_flags) {
2505 if (flags & BTRFS_BLOCK_GROUP_DATA)
2506 fs_info->avail_data_alloc_bits |= extra_flags;
2507 if (flags & BTRFS_BLOCK_GROUP_METADATA)
2508 fs_info->avail_metadata_alloc_bits |= extra_flags;
2509 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
2510 fs_info->avail_system_alloc_bits |= extra_flags;
2511 }
2512}
Chris Mason593060d2008-03-25 16:50:33 -04002513
Yan Zhengc146afa2008-11-12 14:34:12 -05002514static void set_block_group_readonly(struct btrfs_block_group_cache *cache)
2515{
2516 spin_lock(&cache->space_info->lock);
2517 spin_lock(&cache->lock);
2518 if (!cache->ro) {
2519 cache->space_info->bytes_readonly += cache->key.offset -
2520 btrfs_block_group_used(&cache->item);
2521 cache->ro = 1;
2522 }
2523 spin_unlock(&cache->lock);
2524 spin_unlock(&cache->space_info->lock);
2525}
2526
Yan Zheng2b820322008-11-17 21:11:30 -05002527u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
Chris Masonec44a352008-04-28 15:29:52 -04002528{
Yan Zheng2b820322008-11-17 21:11:30 -05002529 u64 num_devices = root->fs_info->fs_devices->rw_devices;
Chris Masona061fc82008-05-07 11:43:44 -04002530
2531 if (num_devices == 1)
2532 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
2533 if (num_devices < 4)
2534 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
2535
Chris Masonec44a352008-04-28 15:29:52 -04002536 if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
2537 (flags & (BTRFS_BLOCK_GROUP_RAID1 |
Chris Masona061fc82008-05-07 11:43:44 -04002538 BTRFS_BLOCK_GROUP_RAID10))) {
Chris Masonec44a352008-04-28 15:29:52 -04002539 flags &= ~BTRFS_BLOCK_GROUP_DUP;
Chris Masona061fc82008-05-07 11:43:44 -04002540 }
Chris Masonec44a352008-04-28 15:29:52 -04002541
2542 if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
Chris Masona061fc82008-05-07 11:43:44 -04002543 (flags & BTRFS_BLOCK_GROUP_RAID10)) {
Chris Masonec44a352008-04-28 15:29:52 -04002544 flags &= ~BTRFS_BLOCK_GROUP_RAID1;
Chris Masona061fc82008-05-07 11:43:44 -04002545 }
Chris Masonec44a352008-04-28 15:29:52 -04002546
2547 if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
2548 ((flags & BTRFS_BLOCK_GROUP_RAID1) |
2549 (flags & BTRFS_BLOCK_GROUP_RAID10) |
2550 (flags & BTRFS_BLOCK_GROUP_DUP)))
2551 flags &= ~BTRFS_BLOCK_GROUP_RAID0;
2552 return flags;
2553}
2554
Josef Bacik6a632092009-02-20 11:00:09 -05002555static u64 btrfs_get_alloc_profile(struct btrfs_root *root, u64 data)
2556{
2557 struct btrfs_fs_info *info = root->fs_info;
2558 u64 alloc_profile;
2559
2560 if (data) {
2561 alloc_profile = info->avail_data_alloc_bits &
2562 info->data_alloc_profile;
2563 data = BTRFS_BLOCK_GROUP_DATA | alloc_profile;
2564 } else if (root == root->fs_info->chunk_root) {
2565 alloc_profile = info->avail_system_alloc_bits &
2566 info->system_alloc_profile;
2567 data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile;
2568 } else {
2569 alloc_profile = info->avail_metadata_alloc_bits &
2570 info->metadata_alloc_profile;
2571 data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile;
2572 }
2573
2574 return btrfs_reduce_alloc_profile(root, data);
2575}
2576
2577void btrfs_set_inode_space_info(struct btrfs_root *root, struct inode *inode)
2578{
2579 u64 alloc_target;
2580
2581 alloc_target = btrfs_get_alloc_profile(root, 1);
2582 BTRFS_I(inode)->space_info = __find_space_info(root->fs_info,
2583 alloc_target);
2584}
2585
2586/*
2587 * for now this just makes sure we have at least 5% of our metadata space free
2588 * for use.
2589 */
2590int btrfs_check_metadata_free_space(struct btrfs_root *root)
2591{
2592 struct btrfs_fs_info *info = root->fs_info;
2593 struct btrfs_space_info *meta_sinfo;
2594 u64 alloc_target, thresh;
Josef Bacik4e06bdd2009-02-20 10:59:53 -05002595 int committed = 0, ret;
Josef Bacik6a632092009-02-20 11:00:09 -05002596
2597 /* get the space info for where the metadata will live */
2598 alloc_target = btrfs_get_alloc_profile(root, 0);
2599 meta_sinfo = __find_space_info(info, alloc_target);
2600
Josef Bacik4e06bdd2009-02-20 10:59:53 -05002601again:
Josef Bacik6a632092009-02-20 11:00:09 -05002602 spin_lock(&meta_sinfo->lock);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05002603 if (!meta_sinfo->full)
2604 thresh = meta_sinfo->total_bytes * 80;
2605 else
2606 thresh = meta_sinfo->total_bytes * 95;
Josef Bacik6a632092009-02-20 11:00:09 -05002607
2608 do_div(thresh, 100);
2609
2610 if (meta_sinfo->bytes_used + meta_sinfo->bytes_reserved +
2611 meta_sinfo->bytes_pinned + meta_sinfo->bytes_readonly > thresh) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05002612 struct btrfs_trans_handle *trans;
2613 if (!meta_sinfo->full) {
2614 meta_sinfo->force_alloc = 1;
2615 spin_unlock(&meta_sinfo->lock);
2616
2617 trans = btrfs_start_transaction(root, 1);
2618 if (!trans)
2619 return -ENOMEM;
2620
2621 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
2622 2 * 1024 * 1024, alloc_target, 0);
2623 btrfs_end_transaction(trans, root);
2624 goto again;
2625 }
Josef Bacik6a632092009-02-20 11:00:09 -05002626 spin_unlock(&meta_sinfo->lock);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05002627
2628 if (!committed) {
2629 committed = 1;
2630 trans = btrfs_join_transaction(root, 1);
2631 if (!trans)
2632 return -ENOMEM;
2633 ret = btrfs_commit_transaction(trans, root);
2634 if (ret)
2635 return ret;
2636 goto again;
2637 }
Josef Bacik6a632092009-02-20 11:00:09 -05002638 return -ENOSPC;
2639 }
2640 spin_unlock(&meta_sinfo->lock);
2641
2642 return 0;
2643}
2644
2645/*
2646 * This will check the space that the inode allocates from to make sure we have
2647 * enough space for bytes.
2648 */
2649int btrfs_check_data_free_space(struct btrfs_root *root, struct inode *inode,
2650 u64 bytes)
2651{
2652 struct btrfs_space_info *data_sinfo;
Josef Bacik4e06bdd2009-02-20 10:59:53 -05002653 int ret = 0, committed = 0;
Josef Bacik6a632092009-02-20 11:00:09 -05002654
2655 /* make sure bytes are sectorsize aligned */
2656 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
2657
2658 data_sinfo = BTRFS_I(inode)->space_info;
2659again:
2660 /* make sure we have enough space to handle the data first */
2661 spin_lock(&data_sinfo->lock);
2662 if (data_sinfo->total_bytes - data_sinfo->bytes_used -
2663 data_sinfo->bytes_delalloc - data_sinfo->bytes_reserved -
2664 data_sinfo->bytes_pinned - data_sinfo->bytes_readonly -
2665 data_sinfo->bytes_may_use < bytes) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05002666 struct btrfs_trans_handle *trans;
2667
Josef Bacik6a632092009-02-20 11:00:09 -05002668 /*
2669 * if we don't have enough free bytes in this space then we need
2670 * to alloc a new chunk.
2671 */
2672 if (!data_sinfo->full) {
2673 u64 alloc_target;
Josef Bacik6a632092009-02-20 11:00:09 -05002674
2675 data_sinfo->force_alloc = 1;
2676 spin_unlock(&data_sinfo->lock);
2677
2678 alloc_target = btrfs_get_alloc_profile(root, 1);
2679 trans = btrfs_start_transaction(root, 1);
2680 if (!trans)
2681 return -ENOMEM;
2682
2683 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
2684 bytes + 2 * 1024 * 1024,
2685 alloc_target, 0);
2686 btrfs_end_transaction(trans, root);
2687 if (ret)
2688 return ret;
2689 goto again;
2690 }
2691 spin_unlock(&data_sinfo->lock);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05002692
2693 /* commit the current transaction and try again */
2694 if (!committed) {
2695 committed = 1;
2696 trans = btrfs_join_transaction(root, 1);
2697 if (!trans)
2698 return -ENOMEM;
2699 ret = btrfs_commit_transaction(trans, root);
2700 if (ret)
2701 return ret;
2702 goto again;
2703 }
2704
Josef Bacik6a632092009-02-20 11:00:09 -05002705 printk(KERN_ERR "no space left, need %llu, %llu delalloc bytes"
2706 ", %llu bytes_used, %llu bytes_reserved, "
2707 "%llu bytes_pinned, %llu bytes_readonly, %llu may use"
Joel Becker21380932009-04-21 12:38:29 -07002708 "%llu total\n", (unsigned long long)bytes,
2709 (unsigned long long)data_sinfo->bytes_delalloc,
2710 (unsigned long long)data_sinfo->bytes_used,
2711 (unsigned long long)data_sinfo->bytes_reserved,
2712 (unsigned long long)data_sinfo->bytes_pinned,
2713 (unsigned long long)data_sinfo->bytes_readonly,
2714 (unsigned long long)data_sinfo->bytes_may_use,
2715 (unsigned long long)data_sinfo->total_bytes);
Josef Bacik6a632092009-02-20 11:00:09 -05002716 return -ENOSPC;
2717 }
2718 data_sinfo->bytes_may_use += bytes;
2719 BTRFS_I(inode)->reserved_bytes += bytes;
2720 spin_unlock(&data_sinfo->lock);
2721
2722 return btrfs_check_metadata_free_space(root);
2723}
2724
2725/*
2726 * if there was an error for whatever reason after calling
2727 * btrfs_check_data_free_space, call this so we can cleanup the counters.
2728 */
2729void btrfs_free_reserved_data_space(struct btrfs_root *root,
2730 struct inode *inode, u64 bytes)
2731{
2732 struct btrfs_space_info *data_sinfo;
2733
2734 /* make sure bytes are sectorsize aligned */
2735 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
2736
2737 data_sinfo = BTRFS_I(inode)->space_info;
2738 spin_lock(&data_sinfo->lock);
2739 data_sinfo->bytes_may_use -= bytes;
2740 BTRFS_I(inode)->reserved_bytes -= bytes;
2741 spin_unlock(&data_sinfo->lock);
2742}
2743
2744/* called when we are adding a delalloc extent to the inode's io_tree */
2745void btrfs_delalloc_reserve_space(struct btrfs_root *root, struct inode *inode,
2746 u64 bytes)
2747{
2748 struct btrfs_space_info *data_sinfo;
2749
2750 /* get the space info for where this inode will be storing its data */
2751 data_sinfo = BTRFS_I(inode)->space_info;
2752
2753 /* make sure we have enough space to handle the data first */
2754 spin_lock(&data_sinfo->lock);
2755 data_sinfo->bytes_delalloc += bytes;
2756
2757 /*
2758 * we are adding a delalloc extent without calling
2759 * btrfs_check_data_free_space first. This happens on a weird
2760 * writepage condition, but shouldn't hurt our accounting
2761 */
2762 if (unlikely(bytes > BTRFS_I(inode)->reserved_bytes)) {
2763 data_sinfo->bytes_may_use -= BTRFS_I(inode)->reserved_bytes;
2764 BTRFS_I(inode)->reserved_bytes = 0;
2765 } else {
2766 data_sinfo->bytes_may_use -= bytes;
2767 BTRFS_I(inode)->reserved_bytes -= bytes;
2768 }
2769
2770 spin_unlock(&data_sinfo->lock);
2771}
2772
2773/* called when we are clearing an delalloc extent from the inode's io_tree */
2774void btrfs_delalloc_free_space(struct btrfs_root *root, struct inode *inode,
2775 u64 bytes)
2776{
2777 struct btrfs_space_info *info;
2778
2779 info = BTRFS_I(inode)->space_info;
2780
2781 spin_lock(&info->lock);
2782 info->bytes_delalloc -= bytes;
2783 spin_unlock(&info->lock);
2784}
2785
Josef Bacik97e728d2009-04-21 17:40:57 -04002786static void force_metadata_allocation(struct btrfs_fs_info *info)
2787{
2788 struct list_head *head = &info->space_info;
2789 struct btrfs_space_info *found;
2790
2791 rcu_read_lock();
2792 list_for_each_entry_rcu(found, head, list) {
2793 if (found->flags & BTRFS_BLOCK_GROUP_METADATA)
2794 found->force_alloc = 1;
2795 }
2796 rcu_read_unlock();
2797}
2798
Chris Mason6324fbf2008-03-24 15:01:59 -04002799static int do_chunk_alloc(struct btrfs_trans_handle *trans,
2800 struct btrfs_root *extent_root, u64 alloc_bytes,
Chris Mason0ef3e662008-05-24 14:04:53 -04002801 u64 flags, int force)
Chris Mason6324fbf2008-03-24 15:01:59 -04002802{
2803 struct btrfs_space_info *space_info;
Josef Bacik97e728d2009-04-21 17:40:57 -04002804 struct btrfs_fs_info *fs_info = extent_root->fs_info;
Chris Mason6324fbf2008-03-24 15:01:59 -04002805 u64 thresh;
Yan Zhengc146afa2008-11-12 14:34:12 -05002806 int ret = 0;
2807
Josef Bacik97e728d2009-04-21 17:40:57 -04002808 mutex_lock(&fs_info->chunk_mutex);
Chris Mason6324fbf2008-03-24 15:01:59 -04002809
Yan Zheng2b820322008-11-17 21:11:30 -05002810 flags = btrfs_reduce_alloc_profile(extent_root, flags);
Chris Masonec44a352008-04-28 15:29:52 -04002811
Chris Mason6324fbf2008-03-24 15:01:59 -04002812 space_info = __find_space_info(extent_root->fs_info, flags);
Chris Mason593060d2008-03-25 16:50:33 -04002813 if (!space_info) {
2814 ret = update_space_info(extent_root->fs_info, flags,
2815 0, 0, &space_info);
2816 BUG_ON(ret);
2817 }
Chris Mason6324fbf2008-03-24 15:01:59 -04002818 BUG_ON(!space_info);
2819
Josef Bacik25179202008-10-29 14:49:05 -04002820 spin_lock(&space_info->lock);
Chris Mason0ef3e662008-05-24 14:04:53 -04002821 if (space_info->force_alloc) {
2822 force = 1;
2823 space_info->force_alloc = 0;
2824 }
Josef Bacik25179202008-10-29 14:49:05 -04002825 if (space_info->full) {
2826 spin_unlock(&space_info->lock);
Chris Mason925baed2008-06-25 16:01:30 -04002827 goto out;
Josef Bacik25179202008-10-29 14:49:05 -04002828 }
Chris Mason6324fbf2008-03-24 15:01:59 -04002829
Yan Zhengc146afa2008-11-12 14:34:12 -05002830 thresh = space_info->total_bytes - space_info->bytes_readonly;
2831 thresh = div_factor(thresh, 6);
Chris Mason0ef3e662008-05-24 14:04:53 -04002832 if (!force &&
Zheng Yane8569812008-09-26 10:05:48 -04002833 (space_info->bytes_used + space_info->bytes_pinned +
Josef Bacik25179202008-10-29 14:49:05 -04002834 space_info->bytes_reserved + alloc_bytes) < thresh) {
2835 spin_unlock(&space_info->lock);
Chris Mason925baed2008-06-25 16:01:30 -04002836 goto out;
Josef Bacik25179202008-10-29 14:49:05 -04002837 }
Josef Bacik25179202008-10-29 14:49:05 -04002838 spin_unlock(&space_info->lock);
2839
Josef Bacik97e728d2009-04-21 17:40:57 -04002840 /*
2841 * if we're doing a data chunk, go ahead and make sure that
2842 * we keep a reasonable number of metadata chunks allocated in the
2843 * FS as well.
2844 */
2845 if (flags & BTRFS_BLOCK_GROUP_DATA) {
2846 fs_info->data_chunk_allocations++;
2847 if (!(fs_info->data_chunk_allocations %
2848 fs_info->metadata_ratio))
2849 force_metadata_allocation(fs_info);
2850 }
2851
Yan Zheng2b820322008-11-17 21:11:30 -05002852 ret = btrfs_alloc_chunk(trans, extent_root, flags);
Chris Masond3977122009-01-05 21:25:51 -05002853 if (ret)
Chris Mason6324fbf2008-03-24 15:01:59 -04002854 space_info->full = 1;
Chris Masona74a4b92008-06-25 16:01:31 -04002855out:
Yan Zhengc146afa2008-11-12 14:34:12 -05002856 mutex_unlock(&extent_root->fs_info->chunk_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002857 return ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04002858}
2859
Chris Mason9078a3e2007-04-26 16:46:15 -04002860static int update_block_group(struct btrfs_trans_handle *trans,
2861 struct btrfs_root *root,
Chris Masondb945352007-10-15 16:15:53 -04002862 u64 bytenr, u64 num_bytes, int alloc,
Chris Mason0b86a832008-03-24 15:01:56 -04002863 int mark_free)
Chris Mason9078a3e2007-04-26 16:46:15 -04002864{
2865 struct btrfs_block_group_cache *cache;
2866 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -04002867 u64 total = num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04002868 u64 old_val;
Chris Masondb945352007-10-15 16:15:53 -04002869 u64 byte_in_group;
Chris Mason3e1ad542007-05-07 20:03:49 -04002870
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002871 /* block accounting for super block */
2872 spin_lock(&info->delalloc_lock);
2873 old_val = btrfs_super_bytes_used(&info->super_copy);
2874 if (alloc)
2875 old_val += num_bytes;
2876 else
2877 old_val -= num_bytes;
2878 btrfs_set_super_bytes_used(&info->super_copy, old_val);
2879
2880 /* block accounting for root item */
2881 old_val = btrfs_root_used(&root->root_item);
2882 if (alloc)
2883 old_val += num_bytes;
2884 else
2885 old_val -= num_bytes;
2886 btrfs_set_root_used(&root->root_item, old_val);
2887 spin_unlock(&info->delalloc_lock);
2888
Chris Masond3977122009-01-05 21:25:51 -05002889 while (total) {
Chris Masondb945352007-10-15 16:15:53 -04002890 cache = btrfs_lookup_block_group(info, bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05002891 if (!cache)
Chris Mason9078a3e2007-04-26 16:46:15 -04002892 return -1;
Chris Masondb945352007-10-15 16:15:53 -04002893 byte_in_group = bytenr - cache->key.objectid;
2894 WARN_ON(byte_in_group > cache->key.offset);
Chris Mason9078a3e2007-04-26 16:46:15 -04002895
Josef Bacik25179202008-10-29 14:49:05 -04002896 spin_lock(&cache->space_info->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04002897 spin_lock(&cache->lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002898 cache->dirty = 1;
Chris Mason9078a3e2007-04-26 16:46:15 -04002899 old_val = btrfs_block_group_used(&cache->item);
Chris Masondb945352007-10-15 16:15:53 -04002900 num_bytes = min(total, cache->key.offset - byte_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -04002901 if (alloc) {
Chris Masondb945352007-10-15 16:15:53 -04002902 old_val += num_bytes;
Chris Mason6324fbf2008-03-24 15:01:59 -04002903 cache->space_info->bytes_used += num_bytes;
Yan Zhenga512bbf2008-12-08 16:46:26 -05002904 if (cache->ro)
Yan Zhengc146afa2008-11-12 14:34:12 -05002905 cache->space_info->bytes_readonly -= num_bytes;
Chris Masonc286ac42008-07-22 23:06:41 -04002906 btrfs_set_block_group_used(&cache->item, old_val);
2907 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04002908 spin_unlock(&cache->space_info->lock);
Chris Masoncd1bc462007-04-27 10:08:34 -04002909 } else {
Chris Masondb945352007-10-15 16:15:53 -04002910 old_val -= num_bytes;
Chris Mason6324fbf2008-03-24 15:01:59 -04002911 cache->space_info->bytes_used -= num_bytes;
Yan Zhengc146afa2008-11-12 14:34:12 -05002912 if (cache->ro)
2913 cache->space_info->bytes_readonly += num_bytes;
Chris Masonc286ac42008-07-22 23:06:41 -04002914 btrfs_set_block_group_used(&cache->item, old_val);
2915 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04002916 spin_unlock(&cache->space_info->lock);
Chris Masonf510cfe2007-10-15 16:14:48 -04002917 if (mark_free) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04002918 int ret;
Liu Hui1f3c79a2009-01-05 15:57:51 -05002919
2920 ret = btrfs_discard_extent(root, bytenr,
2921 num_bytes);
2922 WARN_ON(ret);
2923
Josef Bacik0f9dd462008-09-23 13:14:11 -04002924 ret = btrfs_add_free_space(cache, bytenr,
2925 num_bytes);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002926 WARN_ON(ret);
Chris Masone37c9e62007-05-09 20:13:14 -04002927 }
Chris Masoncd1bc462007-04-27 10:08:34 -04002928 }
Chris Masonfa9c0d72009-04-03 09:47:43 -04002929 btrfs_put_block_group(cache);
Chris Masondb945352007-10-15 16:15:53 -04002930 total -= num_bytes;
2931 bytenr += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04002932 }
2933 return 0;
2934}
Chris Mason6324fbf2008-03-24 15:01:59 -04002935
Chris Masona061fc82008-05-07 11:43:44 -04002936static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
2937{
Josef Bacik0f9dd462008-09-23 13:14:11 -04002938 struct btrfs_block_group_cache *cache;
Yan Zhengd2fb3432008-12-11 16:30:39 -05002939 u64 bytenr;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002940
2941 cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
2942 if (!cache)
Chris Masona061fc82008-05-07 11:43:44 -04002943 return 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002944
Yan Zhengd2fb3432008-12-11 16:30:39 -05002945 bytenr = cache->key.objectid;
Chris Masonfa9c0d72009-04-03 09:47:43 -04002946 btrfs_put_block_group(cache);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002947
2948 return bytenr;
Chris Masona061fc82008-05-07 11:43:44 -04002949}
2950
Chris Masone02119d2008-09-05 16:13:11 -04002951int btrfs_update_pinned_extents(struct btrfs_root *root,
Yan324ae4d2007-11-16 14:57:08 -05002952 u64 bytenr, u64 num, int pin)
2953{
2954 u64 len;
2955 struct btrfs_block_group_cache *cache;
2956 struct btrfs_fs_info *fs_info = root->fs_info;
2957
2958 if (pin) {
2959 set_extent_dirty(&fs_info->pinned_extents,
2960 bytenr, bytenr + num - 1, GFP_NOFS);
2961 } else {
2962 clear_extent_dirty(&fs_info->pinned_extents,
2963 bytenr, bytenr + num - 1, GFP_NOFS);
2964 }
Chris Masonb9473432009-03-13 11:00:37 -04002965
Yan324ae4d2007-11-16 14:57:08 -05002966 while (num > 0) {
2967 cache = btrfs_lookup_block_group(fs_info, bytenr);
Zheng Yane8569812008-09-26 10:05:48 -04002968 BUG_ON(!cache);
2969 len = min(num, cache->key.offset -
2970 (bytenr - cache->key.objectid));
Yan324ae4d2007-11-16 14:57:08 -05002971 if (pin) {
Josef Bacik25179202008-10-29 14:49:05 -04002972 spin_lock(&cache->space_info->lock);
Zheng Yane8569812008-09-26 10:05:48 -04002973 spin_lock(&cache->lock);
2974 cache->pinned += len;
2975 cache->space_info->bytes_pinned += len;
2976 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04002977 spin_unlock(&cache->space_info->lock);
Yan324ae4d2007-11-16 14:57:08 -05002978 fs_info->total_pinned += len;
2979 } else {
Josef Bacik25179202008-10-29 14:49:05 -04002980 spin_lock(&cache->space_info->lock);
Zheng Yane8569812008-09-26 10:05:48 -04002981 spin_lock(&cache->lock);
2982 cache->pinned -= len;
2983 cache->space_info->bytes_pinned -= len;
2984 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04002985 spin_unlock(&cache->space_info->lock);
Yan324ae4d2007-11-16 14:57:08 -05002986 fs_info->total_pinned -= len;
Josef Bacik07103a32008-11-19 15:17:55 -05002987 if (cache->cached)
2988 btrfs_add_free_space(cache, bytenr, len);
Yan324ae4d2007-11-16 14:57:08 -05002989 }
Chris Masonfa9c0d72009-04-03 09:47:43 -04002990 btrfs_put_block_group(cache);
Yan324ae4d2007-11-16 14:57:08 -05002991 bytenr += len;
2992 num -= len;
2993 }
2994 return 0;
2995}
Chris Mason9078a3e2007-04-26 16:46:15 -04002996
Zheng Yane8569812008-09-26 10:05:48 -04002997static int update_reserved_extents(struct btrfs_root *root,
2998 u64 bytenr, u64 num, int reserve)
2999{
3000 u64 len;
3001 struct btrfs_block_group_cache *cache;
3002 struct btrfs_fs_info *fs_info = root->fs_info;
3003
Zheng Yane8569812008-09-26 10:05:48 -04003004 while (num > 0) {
3005 cache = btrfs_lookup_block_group(fs_info, bytenr);
3006 BUG_ON(!cache);
3007 len = min(num, cache->key.offset -
3008 (bytenr - cache->key.objectid));
Josef Bacik25179202008-10-29 14:49:05 -04003009
3010 spin_lock(&cache->space_info->lock);
3011 spin_lock(&cache->lock);
Zheng Yane8569812008-09-26 10:05:48 -04003012 if (reserve) {
Zheng Yane8569812008-09-26 10:05:48 -04003013 cache->reserved += len;
3014 cache->space_info->bytes_reserved += len;
Zheng Yane8569812008-09-26 10:05:48 -04003015 } else {
Zheng Yane8569812008-09-26 10:05:48 -04003016 cache->reserved -= len;
3017 cache->space_info->bytes_reserved -= len;
Zheng Yane8569812008-09-26 10:05:48 -04003018 }
Josef Bacik25179202008-10-29 14:49:05 -04003019 spin_unlock(&cache->lock);
3020 spin_unlock(&cache->space_info->lock);
Chris Masonfa9c0d72009-04-03 09:47:43 -04003021 btrfs_put_block_group(cache);
Zheng Yane8569812008-09-26 10:05:48 -04003022 bytenr += len;
3023 num -= len;
3024 }
3025 return 0;
3026}
3027
Chris Masond1310b22008-01-24 16:13:08 -05003028int btrfs_copy_pinned(struct btrfs_root *root, struct extent_io_tree *copy)
Chris Masonccd467d2007-06-28 15:57:36 -04003029{
Chris Masonccd467d2007-06-28 15:57:36 -04003030 u64 last = 0;
Chris Mason1a5bc162007-10-15 16:15:26 -04003031 u64 start;
3032 u64 end;
Chris Masond1310b22008-01-24 16:13:08 -05003033 struct extent_io_tree *pinned_extents = &root->fs_info->pinned_extents;
Chris Masonccd467d2007-06-28 15:57:36 -04003034 int ret;
Chris Masonccd467d2007-06-28 15:57:36 -04003035
Chris Masond3977122009-01-05 21:25:51 -05003036 while (1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04003037 ret = find_first_extent_bit(pinned_extents, last,
3038 &start, &end, EXTENT_DIRTY);
3039 if (ret)
Chris Masonccd467d2007-06-28 15:57:36 -04003040 break;
Chris Mason1a5bc162007-10-15 16:15:26 -04003041 set_extent_dirty(copy, start, end, GFP_NOFS);
3042 last = end + 1;
Chris Masonccd467d2007-06-28 15:57:36 -04003043 }
3044 return 0;
3045}
3046
3047int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
3048 struct btrfs_root *root,
Chris Masond1310b22008-01-24 16:13:08 -05003049 struct extent_io_tree *unpin)
Chris Masona28ec192007-03-06 20:08:01 -05003050{
Chris Mason1a5bc162007-10-15 16:15:26 -04003051 u64 start;
3052 u64 end;
Chris Masona28ec192007-03-06 20:08:01 -05003053 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05003054
Chris Masond3977122009-01-05 21:25:51 -05003055 while (1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04003056 ret = find_first_extent_bit(unpin, 0, &start, &end,
3057 EXTENT_DIRTY);
3058 if (ret)
Chris Masona28ec192007-03-06 20:08:01 -05003059 break;
Liu Hui1f3c79a2009-01-05 15:57:51 -05003060
3061 ret = btrfs_discard_extent(root, start, end + 1 - start);
3062
Chris Masonb9473432009-03-13 11:00:37 -04003063 /* unlocks the pinned mutex */
Chris Masone02119d2008-09-05 16:13:11 -04003064 btrfs_update_pinned_extents(root, start, end + 1 - start, 0);
Chris Mason1a5bc162007-10-15 16:15:26 -04003065 clear_extent_dirty(unpin, start, end, GFP_NOFS);
Liu Hui1f3c79a2009-01-05 15:57:51 -05003066
Chris Masonb9473432009-03-13 11:00:37 -04003067 cond_resched();
Chris Masona28ec192007-03-06 20:08:01 -05003068 }
Liu Hui1f3c79a2009-01-05 15:57:51 -05003069 return ret;
Chris Masona28ec192007-03-06 20:08:01 -05003070}
3071
Zheng Yan31840ae2008-09-23 13:14:14 -04003072static int pin_down_bytes(struct btrfs_trans_handle *trans,
3073 struct btrfs_root *root,
Chris Masonb9473432009-03-13 11:00:37 -04003074 struct btrfs_path *path,
3075 u64 bytenr, u64 num_bytes, int is_data,
3076 struct extent_buffer **must_clean)
Chris Masone20d96d2007-03-22 12:13:20 -04003077{
Chris Mason1a5bc162007-10-15 16:15:26 -04003078 int err = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04003079 struct extent_buffer *buf;
Chris Mason78fae272007-03-25 11:35:08 -04003080
Zheng Yan31840ae2008-09-23 13:14:14 -04003081 if (is_data)
3082 goto pinit;
Chris Mason4bef0842008-09-08 11:18:08 -04003083
Zheng Yan31840ae2008-09-23 13:14:14 -04003084 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
3085 if (!buf)
3086 goto pinit;
Chris Mason4bef0842008-09-08 11:18:08 -04003087
Zheng Yan31840ae2008-09-23 13:14:14 -04003088 /* we can reuse a block if it hasn't been written
3089 * and it is from this transaction. We can't
3090 * reuse anything from the tree log root because
3091 * it has tiny sub-transactions.
3092 */
3093 if (btrfs_buffer_uptodate(buf, 0) &&
3094 btrfs_try_tree_lock(buf)) {
3095 u64 header_owner = btrfs_header_owner(buf);
3096 u64 header_transid = btrfs_header_generation(buf);
3097 if (header_owner != BTRFS_TREE_LOG_OBJECTID &&
3098 header_transid == trans->transid &&
3099 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
Chris Masonb9473432009-03-13 11:00:37 -04003100 *must_clean = buf;
Zheng Yan31840ae2008-09-23 13:14:14 -04003101 return 1;
Chris Mason8ef97622007-03-26 10:15:30 -04003102 }
Zheng Yan31840ae2008-09-23 13:14:14 -04003103 btrfs_tree_unlock(buf);
Chris Masonf4b9aa82007-03-27 11:05:53 -04003104 }
Zheng Yan31840ae2008-09-23 13:14:14 -04003105 free_extent_buffer(buf);
3106pinit:
Chris Masonb9473432009-03-13 11:00:37 -04003107 btrfs_set_path_blocking(path);
Chris Masonb9473432009-03-13 11:00:37 -04003108 /* unlocks the pinned mutex */
Zheng Yan31840ae2008-09-23 13:14:14 -04003109 btrfs_update_pinned_extents(root, bytenr, num_bytes, 1);
3110
Chris Masonbe744172007-05-06 10:15:01 -04003111 BUG_ON(err < 0);
Chris Masone20d96d2007-03-22 12:13:20 -04003112 return 0;
3113}
3114
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003115
3116static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
3117 struct btrfs_root *root,
3118 u64 bytenr, u64 num_bytes, u64 parent,
3119 u64 root_objectid, u64 owner_objectid,
3120 u64 owner_offset, int refs_to_drop,
3121 struct btrfs_delayed_extent_op *extent_op)
Chris Masona28ec192007-03-06 20:08:01 -05003122{
Chris Masone2fa7222007-03-12 16:22:34 -04003123 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003124 struct btrfs_path *path;
Chris Mason1261ec42007-03-20 20:35:03 -04003125 struct btrfs_fs_info *info = root->fs_info;
3126 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04003127 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003128 struct btrfs_extent_item *ei;
3129 struct btrfs_extent_inline_ref *iref;
Chris Masona28ec192007-03-06 20:08:01 -05003130 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003131 int is_data;
Chris Mason952fcca2008-02-18 16:33:44 -05003132 int extent_slot = 0;
3133 int found_extent = 0;
3134 int num_to_del = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003135 u32 item_size;
3136 u64 refs;
Chris Mason037e6392007-03-07 11:50:24 -05003137
Chris Mason5caf2a02007-04-02 11:20:42 -04003138 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003139 if (!path)
3140 return -ENOMEM;
3141
Chris Mason3c12ac72008-04-21 12:01:38 -04003142 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04003143 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003144
3145 is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
3146 BUG_ON(!is_data && refs_to_drop != 1);
3147
3148 ret = lookup_extent_backref(trans, extent_root, path, &iref,
3149 bytenr, num_bytes, parent,
3150 root_objectid, owner_objectid,
3151 owner_offset);
Chris Mason7bb86312007-12-11 09:25:06 -05003152 if (ret == 0) {
Chris Mason952fcca2008-02-18 16:33:44 -05003153 extent_slot = path->slots[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003154 while (extent_slot >= 0) {
3155 btrfs_item_key_to_cpu(path->nodes[0], &key,
Chris Mason952fcca2008-02-18 16:33:44 -05003156 extent_slot);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003157 if (key.objectid != bytenr)
Chris Mason952fcca2008-02-18 16:33:44 -05003158 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003159 if (key.type == BTRFS_EXTENT_ITEM_KEY &&
3160 key.offset == num_bytes) {
Chris Mason952fcca2008-02-18 16:33:44 -05003161 found_extent = 1;
3162 break;
3163 }
3164 if (path->slots[0] - extent_slot > 5)
3165 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003166 extent_slot--;
Chris Mason952fcca2008-02-18 16:33:44 -05003167 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003168#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3169 item_size = btrfs_item_size_nr(path->nodes[0], extent_slot);
3170 if (found_extent && item_size < sizeof(*ei))
3171 found_extent = 0;
3172#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04003173 if (!found_extent) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003174 BUG_ON(iref);
Chris Mason56bec292009-03-13 10:10:06 -04003175 ret = remove_extent_backref(trans, extent_root, path,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003176 NULL, refs_to_drop,
3177 is_data);
Zheng Yan31840ae2008-09-23 13:14:14 -04003178 BUG_ON(ret);
3179 btrfs_release_path(extent_root, path);
Chris Masonb9473432009-03-13 11:00:37 -04003180 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003181
3182 key.objectid = bytenr;
3183 key.type = BTRFS_EXTENT_ITEM_KEY;
3184 key.offset = num_bytes;
3185
Zheng Yan31840ae2008-09-23 13:14:14 -04003186 ret = btrfs_search_slot(trans, extent_root,
3187 &key, path, -1, 1);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003188 if (ret) {
3189 printk(KERN_ERR "umm, got %d back from search"
Chris Masond3977122009-01-05 21:25:51 -05003190 ", was looking for %llu\n", ret,
3191 (unsigned long long)bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003192 btrfs_print_leaf(extent_root, path->nodes[0]);
3193 }
Zheng Yan31840ae2008-09-23 13:14:14 -04003194 BUG_ON(ret);
3195 extent_slot = path->slots[0];
3196 }
Chris Mason7bb86312007-12-11 09:25:06 -05003197 } else {
3198 btrfs_print_leaf(extent_root, path->nodes[0]);
3199 WARN_ON(1);
Chris Masond3977122009-01-05 21:25:51 -05003200 printk(KERN_ERR "btrfs unable to find ref byte nr %llu "
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003201 "parent %llu root %llu owner %llu offset %llu\n",
Chris Masond3977122009-01-05 21:25:51 -05003202 (unsigned long long)bytenr,
Chris Mason56bec292009-03-13 10:10:06 -04003203 (unsigned long long)parent,
Chris Masond3977122009-01-05 21:25:51 -05003204 (unsigned long long)root_objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003205 (unsigned long long)owner_objectid,
3206 (unsigned long long)owner_offset);
Chris Mason7bb86312007-12-11 09:25:06 -05003207 }
Chris Mason5f39d392007-10-15 16:14:19 -04003208
3209 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003210 item_size = btrfs_item_size_nr(leaf, extent_slot);
3211#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3212 if (item_size < sizeof(*ei)) {
3213 BUG_ON(found_extent || extent_slot != path->slots[0]);
3214 ret = convert_extent_item_v0(trans, extent_root, path,
3215 owner_objectid, 0);
3216 BUG_ON(ret < 0);
3217
3218 btrfs_release_path(extent_root, path);
3219 path->leave_spinning = 1;
3220
3221 key.objectid = bytenr;
3222 key.type = BTRFS_EXTENT_ITEM_KEY;
3223 key.offset = num_bytes;
3224
3225 ret = btrfs_search_slot(trans, extent_root, &key, path,
3226 -1, 1);
3227 if (ret) {
3228 printk(KERN_ERR "umm, got %d back from search"
3229 ", was looking for %llu\n", ret,
3230 (unsigned long long)bytenr);
3231 btrfs_print_leaf(extent_root, path->nodes[0]);
3232 }
3233 BUG_ON(ret);
3234 extent_slot = path->slots[0];
3235 leaf = path->nodes[0];
3236 item_size = btrfs_item_size_nr(leaf, extent_slot);
3237 }
3238#endif
3239 BUG_ON(item_size < sizeof(*ei));
Chris Mason952fcca2008-02-18 16:33:44 -05003240 ei = btrfs_item_ptr(leaf, extent_slot,
Chris Mason123abc82007-03-14 14:14:43 -04003241 struct btrfs_extent_item);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003242 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
3243 struct btrfs_tree_block_info *bi;
3244 BUG_ON(item_size < sizeof(*ei) + sizeof(*bi));
3245 bi = (struct btrfs_tree_block_info *)(ei + 1);
3246 WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
Chris Mason952fcca2008-02-18 16:33:44 -05003247 }
3248
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003249 refs = btrfs_extent_refs(leaf, ei);
3250 BUG_ON(refs < refs_to_drop);
3251 refs -= refs_to_drop;
3252
3253 if (refs > 0) {
3254 if (extent_op)
3255 __run_delayed_extent_op(extent_op, leaf, ei);
3256 /*
3257 * In the case of inline back ref, reference count will
3258 * be updated by remove_extent_backref
3259 */
3260 if (iref) {
3261 BUG_ON(!found_extent);
3262 } else {
3263 btrfs_set_extent_refs(leaf, ei, refs);
3264 btrfs_mark_buffer_dirty(leaf);
3265 }
3266 if (found_extent) {
3267 ret = remove_extent_backref(trans, extent_root, path,
3268 iref, refs_to_drop,
3269 is_data);
3270 BUG_ON(ret);
3271 }
3272 } else {
3273 int mark_free = 0;
Chris Masonb9473432009-03-13 11:00:37 -04003274 struct extent_buffer *must_clean = NULL;
Chris Mason78fae272007-03-25 11:35:08 -04003275
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003276 if (found_extent) {
3277 BUG_ON(is_data && refs_to_drop !=
3278 extent_data_ref_count(root, path, iref));
3279 if (iref) {
3280 BUG_ON(path->slots[0] != extent_slot);
3281 } else {
3282 BUG_ON(path->slots[0] != extent_slot + 1);
3283 path->slots[0] = extent_slot;
3284 num_to_del = 2;
3285 }
Chris Mason78fae272007-03-25 11:35:08 -04003286 }
Chris Masonb9473432009-03-13 11:00:37 -04003287
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003288 ret = pin_down_bytes(trans, root, path, bytenr,
3289 num_bytes, is_data, &must_clean);
3290 if (ret > 0)
3291 mark_free = 1;
3292 BUG_ON(ret < 0);
Chris Masonb9473432009-03-13 11:00:37 -04003293 /*
3294 * it is going to be very rare for someone to be waiting
3295 * on the block we're freeing. del_items might need to
3296 * schedule, so rather than get fancy, just force it
3297 * to blocking here
3298 */
3299 if (must_clean)
3300 btrfs_set_lock_blocking(must_clean);
3301
Chris Mason952fcca2008-02-18 16:33:44 -05003302 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
3303 num_to_del);
Zheng Yan31840ae2008-09-23 13:14:14 -04003304 BUG_ON(ret);
Josef Bacik25179202008-10-29 14:49:05 -04003305 btrfs_release_path(extent_root, path);
David Woodhouse21af8042008-08-12 14:13:26 +01003306
Chris Masonb9473432009-03-13 11:00:37 -04003307 if (must_clean) {
3308 clean_tree_block(NULL, root, must_clean);
3309 btrfs_tree_unlock(must_clean);
3310 free_extent_buffer(must_clean);
3311 }
3312
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003313 if (is_data) {
Chris Mason459931e2008-12-10 09:10:46 -05003314 ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
3315 BUG_ON(ret);
Chris Masond57e62b2009-03-31 13:47:50 -04003316 } else {
3317 invalidate_mapping_pages(info->btree_inode->i_mapping,
3318 bytenr >> PAGE_CACHE_SHIFT,
3319 (bytenr + num_bytes - 1) >> PAGE_CACHE_SHIFT);
Chris Mason459931e2008-12-10 09:10:46 -05003320 }
3321
Chris Masondcbdd4d2008-12-16 13:51:01 -05003322 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
3323 mark_free);
3324 BUG_ON(ret);
Chris Masona28ec192007-03-06 20:08:01 -05003325 }
Chris Mason5caf2a02007-04-02 11:20:42 -04003326 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -05003327 return ret;
3328}
3329
3330/*
Chris Mason1887be62009-03-13 10:11:24 -04003331 * when we free an extent, it is possible (and likely) that we free the last
3332 * delayed ref for that extent as well. This searches the delayed ref tree for
3333 * a given extent, and if there are no other delayed refs to be processed, it
3334 * removes it from the tree.
3335 */
3336static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
3337 struct btrfs_root *root, u64 bytenr)
3338{
3339 struct btrfs_delayed_ref_head *head;
3340 struct btrfs_delayed_ref_root *delayed_refs;
3341 struct btrfs_delayed_ref_node *ref;
3342 struct rb_node *node;
3343 int ret;
3344
3345 delayed_refs = &trans->transaction->delayed_refs;
3346 spin_lock(&delayed_refs->lock);
3347 head = btrfs_find_delayed_ref_head(trans, bytenr);
3348 if (!head)
3349 goto out;
3350
3351 node = rb_prev(&head->node.rb_node);
3352 if (!node)
3353 goto out;
3354
3355 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
3356
3357 /* there are still entries for this ref, we can't drop it */
3358 if (ref->bytenr == bytenr)
3359 goto out;
3360
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003361 if (head->extent_op) {
3362 if (!head->must_insert_reserved)
3363 goto out;
3364 kfree(head->extent_op);
3365 head->extent_op = NULL;
3366 }
3367
Chris Mason1887be62009-03-13 10:11:24 -04003368 /*
3369 * waiting for the lock here would deadlock. If someone else has it
3370 * locked they are already in the process of dropping it anyway
3371 */
3372 if (!mutex_trylock(&head->mutex))
3373 goto out;
3374
3375 /*
3376 * at this point we have a head with no other entries. Go
3377 * ahead and process it.
3378 */
3379 head->node.in_tree = 0;
3380 rb_erase(&head->node.rb_node, &delayed_refs->root);
Chris Masonc3e69d52009-03-13 10:17:05 -04003381
Chris Mason1887be62009-03-13 10:11:24 -04003382 delayed_refs->num_entries--;
3383
3384 /*
3385 * we don't take a ref on the node because we're removing it from the
3386 * tree, so we just steal the ref the tree was holding.
3387 */
Chris Masonc3e69d52009-03-13 10:17:05 -04003388 delayed_refs->num_heads--;
3389 if (list_empty(&head->cluster))
3390 delayed_refs->num_heads_ready--;
3391
3392 list_del_init(&head->cluster);
Chris Mason1887be62009-03-13 10:11:24 -04003393 spin_unlock(&delayed_refs->lock);
3394
3395 ret = run_one_delayed_ref(trans, root->fs_info->tree_root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003396 &head->node, head->extent_op,
3397 head->must_insert_reserved);
Chris Mason1887be62009-03-13 10:11:24 -04003398 BUG_ON(ret);
3399 btrfs_put_delayed_ref(&head->node);
3400 return 0;
3401out:
3402 spin_unlock(&delayed_refs->lock);
3403 return 0;
3404}
3405
Chris Mason925baed2008-06-25 16:01:30 -04003406int btrfs_free_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04003407 struct btrfs_root *root,
3408 u64 bytenr, u64 num_bytes, u64 parent,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003409 u64 root_objectid, u64 owner, u64 offset)
Chris Mason925baed2008-06-25 16:01:30 -04003410{
3411 int ret;
3412
Chris Mason56bec292009-03-13 10:10:06 -04003413 /*
3414 * tree log blocks never actually go into the extent allocation
3415 * tree, just update pinning info and exit early.
Chris Mason56bec292009-03-13 10:10:06 -04003416 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003417 if (root_objectid == BTRFS_TREE_LOG_OBJECTID) {
3418 WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID);
Chris Masonb9473432009-03-13 11:00:37 -04003419 /* unlocks the pinned mutex */
Chris Mason56bec292009-03-13 10:10:06 -04003420 btrfs_update_pinned_extents(root, bytenr, num_bytes, 1);
Chris Mason56bec292009-03-13 10:10:06 -04003421 update_reserved_extents(root, bytenr, num_bytes, 0);
3422 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003423 } else if (owner < BTRFS_FIRST_FREE_OBJECTID) {
3424 ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes,
3425 parent, root_objectid, (int)owner,
3426 BTRFS_DROP_DELAYED_REF, NULL);
Chris Mason1887be62009-03-13 10:11:24 -04003427 BUG_ON(ret);
3428 ret = check_ref_cleanup(trans, root, bytenr);
3429 BUG_ON(ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003430 } else {
3431 ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes,
3432 parent, root_objectid, owner,
3433 offset, BTRFS_DROP_DELAYED_REF, NULL);
3434 BUG_ON(ret);
Chris Mason56bec292009-03-13 10:10:06 -04003435 }
Chris Mason925baed2008-06-25 16:01:30 -04003436 return ret;
3437}
3438
Chris Mason87ee04e2007-11-30 11:30:34 -05003439static u64 stripe_align(struct btrfs_root *root, u64 val)
3440{
3441 u64 mask = ((u64)root->stripesize - 1);
3442 u64 ret = (val + mask) & ~mask;
3443 return ret;
3444}
3445
Chris Masonfec577f2007-02-26 10:40:21 -05003446/*
3447 * walks the btree of allocated extents and find a hole of a given size.
3448 * The key ins is changed to record the hole:
3449 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -04003450 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -05003451 * ins->offset == number of blocks
3452 * Any available blocks before search_start are skipped.
3453 */
Chris Masond3977122009-01-05 21:25:51 -05003454static noinline int find_free_extent(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05003455 struct btrfs_root *orig_root,
3456 u64 num_bytes, u64 empty_size,
3457 u64 search_start, u64 search_end,
3458 u64 hint_byte, struct btrfs_key *ins,
3459 u64 exclude_start, u64 exclude_nr,
3460 int data)
Chris Masonfec577f2007-02-26 10:40:21 -05003461{
Josef Bacik80eb2342008-10-29 14:49:05 -04003462 int ret = 0;
Chris Masond3977122009-01-05 21:25:51 -05003463 struct btrfs_root *root = orig_root->fs_info->extent_root;
Chris Masonfa9c0d72009-04-03 09:47:43 -04003464 struct btrfs_free_cluster *last_ptr = NULL;
Josef Bacik80eb2342008-10-29 14:49:05 -04003465 struct btrfs_block_group_cache *block_group = NULL;
Chris Mason239b14b2008-03-24 15:02:07 -04003466 int empty_cluster = 2 * 1024 * 1024;
Chris Mason0ef3e662008-05-24 14:04:53 -04003467 int allowed_chunk_alloc = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04003468 struct btrfs_space_info *space_info;
Chris Masonfa9c0d72009-04-03 09:47:43 -04003469 int last_ptr_loop = 0;
3470 int loop = 0;
Chris Masonfec577f2007-02-26 10:40:21 -05003471
Chris Masondb945352007-10-15 16:15:53 -04003472 WARN_ON(num_bytes < root->sectorsize);
Chris Masonb1a4d962007-04-04 15:27:52 -04003473 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
Josef Bacik80eb2342008-10-29 14:49:05 -04003474 ins->objectid = 0;
3475 ins->offset = 0;
Chris Masonb1a4d962007-04-04 15:27:52 -04003476
Josef Bacik2552d172009-04-03 10:14:19 -04003477 space_info = __find_space_info(root->fs_info, data);
3478
Chris Mason0ef3e662008-05-24 14:04:53 -04003479 if (orig_root->ref_cows || empty_size)
3480 allowed_chunk_alloc = 1;
3481
Chris Mason239b14b2008-03-24 15:02:07 -04003482 if (data & BTRFS_BLOCK_GROUP_METADATA) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04003483 last_ptr = &root->fs_info->meta_alloc_cluster;
Chris Mason536ac8a2009-02-12 09:41:38 -05003484 if (!btrfs_test_opt(root, SSD))
3485 empty_cluster = 64 * 1024;
Chris Mason239b14b2008-03-24 15:02:07 -04003486 }
3487
Chris Masonfa9c0d72009-04-03 09:47:43 -04003488 if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD)) {
3489 last_ptr = &root->fs_info->data_alloc_cluster;
3490 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04003491
Chris Mason239b14b2008-03-24 15:02:07 -04003492 if (last_ptr) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04003493 spin_lock(&last_ptr->lock);
3494 if (last_ptr->block_group)
3495 hint_byte = last_ptr->window_start;
3496 spin_unlock(&last_ptr->lock);
Chris Mason239b14b2008-03-24 15:02:07 -04003497 }
Chris Masonfa9c0d72009-04-03 09:47:43 -04003498
Chris Masona061fc82008-05-07 11:43:44 -04003499 search_start = max(search_start, first_logical_byte(root, 0));
Chris Mason239b14b2008-03-24 15:02:07 -04003500 search_start = max(search_start, hint_byte);
Chris Mason0b86a832008-03-24 15:01:56 -04003501
Chris Masonfa9c0d72009-04-03 09:47:43 -04003502 if (!last_ptr) {
3503 empty_cluster = 0;
3504 loop = 1;
3505 }
3506
Josef Bacik2552d172009-04-03 10:14:19 -04003507 if (search_start == hint_byte) {
Josef Bacik2552d172009-04-03 10:14:19 -04003508 block_group = btrfs_lookup_block_group(root->fs_info,
3509 search_start);
3510 if (block_group && block_group_bits(block_group, data)) {
Josef Bacik2552d172009-04-03 10:14:19 -04003511 down_read(&space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04003512 if (list_empty(&block_group->list) ||
3513 block_group->ro) {
3514 /*
3515 * someone is removing this block group,
3516 * we can't jump into the have_block_group
3517 * target because our list pointers are not
3518 * valid
3519 */
3520 btrfs_put_block_group(block_group);
3521 up_read(&space_info->groups_sem);
3522 } else
3523 goto have_block_group;
Josef Bacik2552d172009-04-03 10:14:19 -04003524 } else if (block_group) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04003525 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04003526 }
Chris Mason42e70e72008-11-07 18:17:11 -05003527 }
Chris Mason43662112008-11-07 09:06:11 -05003528
Josef Bacik2552d172009-04-03 10:14:19 -04003529search:
Josef Bacik80eb2342008-10-29 14:49:05 -04003530 down_read(&space_info->groups_sem);
Josef Bacik2552d172009-04-03 10:14:19 -04003531 list_for_each_entry(block_group, &space_info->block_groups, list) {
Josef Bacik6226cb02009-04-03 10:14:18 -04003532 u64 offset;
Chris Mason8a1413a2008-11-10 16:13:54 -05003533
Josef Bacik2552d172009-04-03 10:14:19 -04003534 atomic_inc(&block_group->count);
3535 search_start = block_group->key.objectid;
Chris Mason42e70e72008-11-07 18:17:11 -05003536
Josef Bacik2552d172009-04-03 10:14:19 -04003537have_block_group:
Josef Bacikea6a4782008-11-20 12:16:16 -05003538 if (unlikely(!block_group->cached)) {
3539 mutex_lock(&block_group->cache_mutex);
3540 ret = cache_block_group(root, block_group);
3541 mutex_unlock(&block_group->cache_mutex);
Josef Bacik2552d172009-04-03 10:14:19 -04003542 if (ret) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04003543 btrfs_put_block_group(block_group);
Josef Bacikea6a4782008-11-20 12:16:16 -05003544 break;
Josef Bacik2552d172009-04-03 10:14:19 -04003545 }
Josef Bacikea6a4782008-11-20 12:16:16 -05003546 }
3547
Josef Bacikea6a4782008-11-20 12:16:16 -05003548 if (unlikely(block_group->ro))
Josef Bacik2552d172009-04-03 10:14:19 -04003549 goto loop;
Josef Bacik0f9dd462008-09-23 13:14:11 -04003550
Chris Masonfa9c0d72009-04-03 09:47:43 -04003551 if (last_ptr) {
3552 /*
3553 * the refill lock keeps out other
3554 * people trying to start a new cluster
3555 */
3556 spin_lock(&last_ptr->refill_lock);
Chris Mason44fb5512009-06-04 15:34:51 -04003557 if (last_ptr->block_group &&
3558 (last_ptr->block_group->ro ||
3559 !block_group_bits(last_ptr->block_group, data))) {
3560 offset = 0;
3561 goto refill_cluster;
3562 }
3563
Chris Masonfa9c0d72009-04-03 09:47:43 -04003564 offset = btrfs_alloc_from_cluster(block_group, last_ptr,
3565 num_bytes, search_start);
3566 if (offset) {
3567 /* we have a block, we're done */
3568 spin_unlock(&last_ptr->refill_lock);
3569 goto checks;
3570 }
3571
3572 spin_lock(&last_ptr->lock);
3573 /*
3574 * whoops, this cluster doesn't actually point to
3575 * this block group. Get a ref on the block
3576 * group is does point to and try again
3577 */
3578 if (!last_ptr_loop && last_ptr->block_group &&
3579 last_ptr->block_group != block_group) {
3580
3581 btrfs_put_block_group(block_group);
3582 block_group = last_ptr->block_group;
3583 atomic_inc(&block_group->count);
3584 spin_unlock(&last_ptr->lock);
3585 spin_unlock(&last_ptr->refill_lock);
3586
3587 last_ptr_loop = 1;
3588 search_start = block_group->key.objectid;
Chris Mason44fb5512009-06-04 15:34:51 -04003589 /*
3590 * we know this block group is properly
3591 * in the list because
3592 * btrfs_remove_block_group, drops the
3593 * cluster before it removes the block
3594 * group from the list
3595 */
Chris Masonfa9c0d72009-04-03 09:47:43 -04003596 goto have_block_group;
3597 }
3598 spin_unlock(&last_ptr->lock);
Chris Mason44fb5512009-06-04 15:34:51 -04003599refill_cluster:
Chris Masonfa9c0d72009-04-03 09:47:43 -04003600 /*
3601 * this cluster didn't work out, free it and
3602 * start over
3603 */
3604 btrfs_return_cluster_to_free_space(NULL, last_ptr);
3605
3606 last_ptr_loop = 0;
3607
3608 /* allocate a cluster in this block group */
Chris Mason451d7582009-06-09 20:28:34 -04003609 ret = btrfs_find_space_cluster(trans, root,
Chris Masonfa9c0d72009-04-03 09:47:43 -04003610 block_group, last_ptr,
3611 offset, num_bytes,
3612 empty_cluster + empty_size);
3613 if (ret == 0) {
3614 /*
3615 * now pull our allocation out of this
3616 * cluster
3617 */
3618 offset = btrfs_alloc_from_cluster(block_group,
3619 last_ptr, num_bytes,
3620 search_start);
3621 if (offset) {
3622 /* we found one, proceed */
3623 spin_unlock(&last_ptr->refill_lock);
3624 goto checks;
3625 }
3626 }
3627 /*
3628 * at this point we either didn't find a cluster
3629 * or we weren't able to allocate a block from our
3630 * cluster. Free the cluster we've been trying
3631 * to use, and go to the next block group
3632 */
3633 if (loop < 2) {
3634 btrfs_return_cluster_to_free_space(NULL,
3635 last_ptr);
3636 spin_unlock(&last_ptr->refill_lock);
3637 goto loop;
3638 }
3639 spin_unlock(&last_ptr->refill_lock);
3640 }
3641
Josef Bacik6226cb02009-04-03 10:14:18 -04003642 offset = btrfs_find_space_for_alloc(block_group, search_start,
3643 num_bytes, empty_size);
3644 if (!offset)
Josef Bacik2552d172009-04-03 10:14:19 -04003645 goto loop;
Chris Masonfa9c0d72009-04-03 09:47:43 -04003646checks:
Josef Bacik6226cb02009-04-03 10:14:18 -04003647 search_start = stripe_align(root, offset);
Chris Mason0b86a832008-03-24 15:01:56 -04003648
Josef Bacik2552d172009-04-03 10:14:19 -04003649 /* move on to the next group */
Josef Bacik6226cb02009-04-03 10:14:18 -04003650 if (search_start + num_bytes >= search_end) {
3651 btrfs_add_free_space(block_group, offset, num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04003652 goto loop;
Josef Bacik6226cb02009-04-03 10:14:18 -04003653 }
Chris Masone37c9e62007-05-09 20:13:14 -04003654
Josef Bacik2552d172009-04-03 10:14:19 -04003655 /* move on to the next group */
3656 if (search_start + num_bytes >
Josef Bacik6226cb02009-04-03 10:14:18 -04003657 block_group->key.objectid + block_group->key.offset) {
3658 btrfs_add_free_space(block_group, offset, num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04003659 goto loop;
Josef Bacik6226cb02009-04-03 10:14:18 -04003660 }
Josef Bacik80eb2342008-10-29 14:49:05 -04003661
Josef Bacik2552d172009-04-03 10:14:19 -04003662 if (exclude_nr > 0 &&
3663 (search_start + num_bytes > exclude_start &&
3664 search_start < exclude_start + exclude_nr)) {
3665 search_start = exclude_start + exclude_nr;
3666
Josef Bacik6226cb02009-04-03 10:14:18 -04003667 btrfs_add_free_space(block_group, offset, num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04003668 /*
3669 * if search_start is still in this block group
3670 * then we just re-search this block group
3671 */
3672 if (search_start >= block_group->key.objectid &&
3673 search_start < (block_group->key.objectid +
Josef Bacik6226cb02009-04-03 10:14:18 -04003674 block_group->key.offset))
Josef Bacik2552d172009-04-03 10:14:19 -04003675 goto have_block_group;
Josef Bacik2552d172009-04-03 10:14:19 -04003676 goto loop;
Josef Bacik0f9dd462008-09-23 13:14:11 -04003677 }
Josef Bacik2552d172009-04-03 10:14:19 -04003678
3679 ins->objectid = search_start;
3680 ins->offset = num_bytes;
3681
Josef Bacik6226cb02009-04-03 10:14:18 -04003682 if (offset < search_start)
3683 btrfs_add_free_space(block_group, offset,
3684 search_start - offset);
3685 BUG_ON(offset > search_start);
3686
Josef Bacik2552d172009-04-03 10:14:19 -04003687 /* we are all good, lets return */
Josef Bacik2552d172009-04-03 10:14:19 -04003688 break;
3689loop:
Chris Masonfa9c0d72009-04-03 09:47:43 -04003690 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04003691 }
3692 up_read(&space_info->groups_sem);
Chris Masonf5a31e12008-11-10 11:47:09 -05003693
Chris Masonfa9c0d72009-04-03 09:47:43 -04003694 /* loop == 0, try to find a clustered alloc in every block group
3695 * loop == 1, try again after forcing a chunk allocation
3696 * loop == 2, set empty_size and empty_cluster to 0 and try again
3697 */
3698 if (!ins->objectid && loop < 3 &&
3699 (empty_size || empty_cluster || allowed_chunk_alloc)) {
3700 if (loop >= 2) {
3701 empty_size = 0;
3702 empty_cluster = 0;
3703 }
Chris Mason42e70e72008-11-07 18:17:11 -05003704
Josef Bacik2552d172009-04-03 10:14:19 -04003705 if (allowed_chunk_alloc) {
3706 ret = do_chunk_alloc(trans, root, num_bytes +
3707 2 * 1024 * 1024, data, 1);
Josef Bacik2552d172009-04-03 10:14:19 -04003708 allowed_chunk_alloc = 0;
3709 } else {
3710 space_info->force_alloc = 1;
Josef Bacik0f9dd462008-09-23 13:14:11 -04003711 }
Josef Bacik80eb2342008-10-29 14:49:05 -04003712
Chris Masonfa9c0d72009-04-03 09:47:43 -04003713 if (loop < 3) {
3714 loop++;
Josef Bacik2552d172009-04-03 10:14:19 -04003715 goto search;
Chris Masonfa9c0d72009-04-03 09:47:43 -04003716 }
Josef Bacik2552d172009-04-03 10:14:19 -04003717 ret = -ENOSPC;
3718 } else if (!ins->objectid) {
3719 ret = -ENOSPC;
Chris Masone19caa52007-10-15 16:17:44 -04003720 }
Chris Mason0b86a832008-03-24 15:01:56 -04003721
Josef Bacik80eb2342008-10-29 14:49:05 -04003722 /* we found what we needed */
3723 if (ins->objectid) {
3724 if (!(data & BTRFS_BLOCK_GROUP_DATA))
Yan Zhengd2fb3432008-12-11 16:30:39 -05003725 trans->block_group = block_group->key.objectid;
Josef Bacik80eb2342008-10-29 14:49:05 -04003726
Chris Masonfa9c0d72009-04-03 09:47:43 -04003727 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04003728 ret = 0;
3729 }
Chris Mason0b86a832008-03-24 15:01:56 -04003730
Chris Mason0f70abe2007-02-28 16:46:22 -05003731 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05003732}
Chris Masonec44a352008-04-28 15:29:52 -04003733
Josef Bacik0f9dd462008-09-23 13:14:11 -04003734static void dump_space_info(struct btrfs_space_info *info, u64 bytes)
3735{
3736 struct btrfs_block_group_cache *cache;
Josef Bacik0f9dd462008-09-23 13:14:11 -04003737
Chris Masond3977122009-01-05 21:25:51 -05003738 printk(KERN_INFO "space_info has %llu free, is %sfull\n",
3739 (unsigned long long)(info->total_bytes - info->bytes_used -
3740 info->bytes_pinned - info->bytes_reserved),
3741 (info->full) ? "" : "not ");
Josef Bacik6a632092009-02-20 11:00:09 -05003742 printk(KERN_INFO "space_info total=%llu, pinned=%llu, delalloc=%llu,"
Joel Becker21380932009-04-21 12:38:29 -07003743 " may_use=%llu, used=%llu\n",
3744 (unsigned long long)info->total_bytes,
3745 (unsigned long long)info->bytes_pinned,
3746 (unsigned long long)info->bytes_delalloc,
3747 (unsigned long long)info->bytes_may_use,
3748 (unsigned long long)info->bytes_used);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003749
Josef Bacik80eb2342008-10-29 14:49:05 -04003750 down_read(&info->groups_sem);
Qinghuang Fengc6e30872009-01-21 10:59:08 -05003751 list_for_each_entry(cache, &info->block_groups, list) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04003752 spin_lock(&cache->lock);
Chris Masond3977122009-01-05 21:25:51 -05003753 printk(KERN_INFO "block group %llu has %llu bytes, %llu used "
3754 "%llu pinned %llu reserved\n",
3755 (unsigned long long)cache->key.objectid,
3756 (unsigned long long)cache->key.offset,
3757 (unsigned long long)btrfs_block_group_used(&cache->item),
3758 (unsigned long long)cache->pinned,
3759 (unsigned long long)cache->reserved);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003760 btrfs_dump_free_space(cache, bytes);
3761 spin_unlock(&cache->lock);
3762 }
Josef Bacik80eb2342008-10-29 14:49:05 -04003763 up_read(&info->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003764}
Zheng Yane8569812008-09-26 10:05:48 -04003765
Chris Masone6dcd2d2008-07-17 12:53:50 -04003766static int __btrfs_reserve_extent(struct btrfs_trans_handle *trans,
3767 struct btrfs_root *root,
3768 u64 num_bytes, u64 min_alloc_size,
3769 u64 empty_size, u64 hint_byte,
3770 u64 search_end, struct btrfs_key *ins,
3771 u64 data)
Chris Masonfec577f2007-02-26 10:40:21 -05003772{
3773 int ret;
Chris Masonfbdc7622007-05-30 10:22:12 -04003774 u64 search_start = 0;
Chris Mason1261ec42007-03-20 20:35:03 -04003775 struct btrfs_fs_info *info = root->fs_info;
Chris Mason925baed2008-06-25 16:01:30 -04003776
Josef Bacik6a632092009-02-20 11:00:09 -05003777 data = btrfs_get_alloc_profile(root, data);
Chris Mason98d20f62008-04-14 09:46:10 -04003778again:
Chris Mason0ef3e662008-05-24 14:04:53 -04003779 /*
3780 * the only place that sets empty_size is btrfs_realloc_node, which
3781 * is not called recursively on allocations
3782 */
3783 if (empty_size || root->ref_cows) {
Chris Mason593060d2008-03-25 16:50:33 -04003784 if (!(data & BTRFS_BLOCK_GROUP_METADATA)) {
Chris Mason6324fbf2008-03-24 15:01:59 -04003785 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0ef3e662008-05-24 14:04:53 -04003786 2 * 1024 * 1024,
3787 BTRFS_BLOCK_GROUP_METADATA |
3788 (info->metadata_alloc_profile &
3789 info->avail_metadata_alloc_bits), 0);
Chris Mason6324fbf2008-03-24 15:01:59 -04003790 }
3791 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0ef3e662008-05-24 14:04:53 -04003792 num_bytes + 2 * 1024 * 1024, data, 0);
Chris Mason6324fbf2008-03-24 15:01:59 -04003793 }
Chris Mason0b86a832008-03-24 15:01:56 -04003794
Chris Masondb945352007-10-15 16:15:53 -04003795 WARN_ON(num_bytes < root->sectorsize);
3796 ret = find_free_extent(trans, root, num_bytes, empty_size,
3797 search_start, search_end, hint_byte, ins,
Chris Mason26b80032007-08-08 20:17:12 -04003798 trans->alloc_exclude_start,
3799 trans->alloc_exclude_nr, data);
Chris Mason3b951512008-04-17 11:29:12 -04003800
Chris Mason98d20f62008-04-14 09:46:10 -04003801 if (ret == -ENOSPC && num_bytes > min_alloc_size) {
3802 num_bytes = num_bytes >> 1;
Josef Bacik0f9dd462008-09-23 13:14:11 -04003803 num_bytes = num_bytes & ~(root->sectorsize - 1);
Chris Mason98d20f62008-04-14 09:46:10 -04003804 num_bytes = max(num_bytes, min_alloc_size);
Chris Mason0ef3e662008-05-24 14:04:53 -04003805 do_chunk_alloc(trans, root->fs_info->extent_root,
3806 num_bytes, data, 1);
Chris Mason98d20f62008-04-14 09:46:10 -04003807 goto again;
3808 }
Chris Masonec44a352008-04-28 15:29:52 -04003809 if (ret) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04003810 struct btrfs_space_info *sinfo;
3811
3812 sinfo = __find_space_info(root->fs_info, data);
Chris Masond3977122009-01-05 21:25:51 -05003813 printk(KERN_ERR "btrfs allocation failed flags %llu, "
3814 "wanted %llu\n", (unsigned long long)data,
3815 (unsigned long long)num_bytes);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003816 dump_space_info(sinfo, num_bytes);
Chris Mason925baed2008-06-25 16:01:30 -04003817 BUG();
Chris Mason925baed2008-06-25 16:01:30 -04003818 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04003819
3820 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003821}
3822
Chris Mason65b51a02008-08-01 15:11:20 -04003823int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len)
3824{
Josef Bacik0f9dd462008-09-23 13:14:11 -04003825 struct btrfs_block_group_cache *cache;
Liu Hui1f3c79a2009-01-05 15:57:51 -05003826 int ret = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04003827
Josef Bacik0f9dd462008-09-23 13:14:11 -04003828 cache = btrfs_lookup_block_group(root->fs_info, start);
3829 if (!cache) {
Chris Masond3977122009-01-05 21:25:51 -05003830 printk(KERN_ERR "Unable to find block group for %llu\n",
3831 (unsigned long long)start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003832 return -ENOSPC;
3833 }
Liu Hui1f3c79a2009-01-05 15:57:51 -05003834
3835 ret = btrfs_discard_extent(root, start, len);
3836
Josef Bacik0f9dd462008-09-23 13:14:11 -04003837 btrfs_add_free_space(cache, start, len);
Chris Masonfa9c0d72009-04-03 09:47:43 -04003838 btrfs_put_block_group(cache);
Zheng Yan1a40e232008-09-26 10:09:34 -04003839 update_reserved_extents(root, start, len, 0);
Liu Hui1f3c79a2009-01-05 15:57:51 -05003840
3841 return ret;
Chris Mason65b51a02008-08-01 15:11:20 -04003842}
3843
Chris Masone6dcd2d2008-07-17 12:53:50 -04003844int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
3845 struct btrfs_root *root,
3846 u64 num_bytes, u64 min_alloc_size,
3847 u64 empty_size, u64 hint_byte,
3848 u64 search_end, struct btrfs_key *ins,
3849 u64 data)
3850{
3851 int ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003852 ret = __btrfs_reserve_extent(trans, root, num_bytes, min_alloc_size,
3853 empty_size, hint_byte, search_end, ins,
3854 data);
Zheng Yane8569812008-09-26 10:05:48 -04003855 update_reserved_extents(root, ins->objectid, ins->offset, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003856 return ret;
3857}
3858
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003859static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
3860 struct btrfs_root *root,
3861 u64 parent, u64 root_objectid,
3862 u64 flags, u64 owner, u64 offset,
3863 struct btrfs_key *ins, int ref_mod)
Chris Masone6dcd2d2008-07-17 12:53:50 -04003864{
3865 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003866 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003867 struct btrfs_extent_item *extent_item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003868 struct btrfs_extent_inline_ref *iref;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003869 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003870 struct extent_buffer *leaf;
3871 int type;
3872 u32 size;
Chris Masonf2654de2007-06-26 12:20:46 -04003873
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003874 if (parent > 0)
3875 type = BTRFS_SHARED_DATA_REF_KEY;
3876 else
3877 type = BTRFS_EXTENT_DATA_REF_KEY;
Zheng Yan31840ae2008-09-23 13:14:14 -04003878
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003879 size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type);
Chris Mason7bb86312007-12-11 09:25:06 -05003880
3881 path = btrfs_alloc_path();
3882 BUG_ON(!path);
Chris Mason47e4bb92008-02-01 14:51:59 -05003883
Chris Masonb9473432009-03-13 11:00:37 -04003884 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003885 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
3886 ins, size);
Chris Masonccd467d2007-06-28 15:57:36 -04003887 BUG_ON(ret);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003888
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003889 leaf = path->nodes[0];
3890 extent_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason47e4bb92008-02-01 14:51:59 -05003891 struct btrfs_extent_item);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003892 btrfs_set_extent_refs(leaf, extent_item, ref_mod);
3893 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
3894 btrfs_set_extent_flags(leaf, extent_item,
3895 flags | BTRFS_EXTENT_FLAG_DATA);
Chris Mason47e4bb92008-02-01 14:51:59 -05003896
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003897 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
3898 btrfs_set_extent_inline_ref_type(leaf, iref, type);
3899 if (parent > 0) {
3900 struct btrfs_shared_data_ref *ref;
3901 ref = (struct btrfs_shared_data_ref *)(iref + 1);
3902 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
3903 btrfs_set_shared_data_ref_count(leaf, ref, ref_mod);
3904 } else {
3905 struct btrfs_extent_data_ref *ref;
3906 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
3907 btrfs_set_extent_data_ref_root(leaf, ref, root_objectid);
3908 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
3909 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
3910 btrfs_set_extent_data_ref_count(leaf, ref, ref_mod);
3911 }
Chris Mason47e4bb92008-02-01 14:51:59 -05003912
3913 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason7bb86312007-12-11 09:25:06 -05003914 btrfs_free_path(path);
Chris Masonf510cfe2007-10-15 16:14:48 -04003915
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003916 ret = update_block_group(trans, root, ins->objectid, ins->offset,
3917 1, 0);
Chris Masonf5947062008-02-04 10:10:13 -05003918 if (ret) {
Chris Masond3977122009-01-05 21:25:51 -05003919 printk(KERN_ERR "btrfs update block group failed for %llu "
3920 "%llu\n", (unsigned long long)ins->objectid,
3921 (unsigned long long)ins->offset);
Chris Masonf5947062008-02-04 10:10:13 -05003922 BUG();
3923 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003924 return ret;
3925}
3926
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003927static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
3928 struct btrfs_root *root,
3929 u64 parent, u64 root_objectid,
3930 u64 flags, struct btrfs_disk_key *key,
3931 int level, struct btrfs_key *ins)
3932{
3933 int ret;
3934 struct btrfs_fs_info *fs_info = root->fs_info;
3935 struct btrfs_extent_item *extent_item;
3936 struct btrfs_tree_block_info *block_info;
3937 struct btrfs_extent_inline_ref *iref;
3938 struct btrfs_path *path;
3939 struct extent_buffer *leaf;
3940 u32 size = sizeof(*extent_item) + sizeof(*block_info) + sizeof(*iref);
3941
3942 path = btrfs_alloc_path();
3943 BUG_ON(!path);
3944
3945 path->leave_spinning = 1;
3946 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
3947 ins, size);
3948 BUG_ON(ret);
3949
3950 leaf = path->nodes[0];
3951 extent_item = btrfs_item_ptr(leaf, path->slots[0],
3952 struct btrfs_extent_item);
3953 btrfs_set_extent_refs(leaf, extent_item, 1);
3954 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
3955 btrfs_set_extent_flags(leaf, extent_item,
3956 flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
3957 block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
3958
3959 btrfs_set_tree_block_key(leaf, block_info, key);
3960 btrfs_set_tree_block_level(leaf, block_info, level);
3961
3962 iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
3963 if (parent > 0) {
3964 BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
3965 btrfs_set_extent_inline_ref_type(leaf, iref,
3966 BTRFS_SHARED_BLOCK_REF_KEY);
3967 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
3968 } else {
3969 btrfs_set_extent_inline_ref_type(leaf, iref,
3970 BTRFS_TREE_BLOCK_REF_KEY);
3971 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
3972 }
3973
3974 btrfs_mark_buffer_dirty(leaf);
3975 btrfs_free_path(path);
3976
3977 ret = update_block_group(trans, root, ins->objectid, ins->offset,
3978 1, 0);
3979 if (ret) {
3980 printk(KERN_ERR "btrfs update block group failed for %llu "
3981 "%llu\n", (unsigned long long)ins->objectid,
3982 (unsigned long long)ins->offset);
3983 BUG();
3984 }
3985 return ret;
3986}
3987
3988int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
3989 struct btrfs_root *root,
3990 u64 root_objectid, u64 owner,
3991 u64 offset, struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04003992{
3993 int ret;
Chris Mason1c2308f2008-09-23 13:14:13 -04003994
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003995 BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID);
Chris Mason56bec292009-03-13 10:10:06 -04003996
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003997 ret = btrfs_add_delayed_data_ref(trans, ins->objectid, ins->offset,
3998 0, root_objectid, owner, offset,
3999 BTRFS_ADD_DELAYED_EXTENT, NULL);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004000 return ret;
4001}
Chris Masone02119d2008-09-05 16:13:11 -04004002
4003/*
4004 * this is used by the tree logging recovery code. It records that
4005 * an extent has been allocated and makes sure to clear the free
4006 * space cache bits as well
4007 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004008int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
4009 struct btrfs_root *root,
4010 u64 root_objectid, u64 owner, u64 offset,
4011 struct btrfs_key *ins)
Chris Masone02119d2008-09-05 16:13:11 -04004012{
4013 int ret;
4014 struct btrfs_block_group_cache *block_group;
4015
Chris Masone02119d2008-09-05 16:13:11 -04004016 block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
Josef Bacikea6a4782008-11-20 12:16:16 -05004017 mutex_lock(&block_group->cache_mutex);
Chris Masone02119d2008-09-05 16:13:11 -04004018 cache_block_group(root, block_group);
Josef Bacikea6a4782008-11-20 12:16:16 -05004019 mutex_unlock(&block_group->cache_mutex);
Chris Masone02119d2008-09-05 16:13:11 -04004020
Josef Bacikea6a4782008-11-20 12:16:16 -05004021 ret = btrfs_remove_free_space(block_group, ins->objectid,
4022 ins->offset);
Josef Bacik0f9dd462008-09-23 13:14:11 -04004023 BUG_ON(ret);
Chris Masonfa9c0d72009-04-03 09:47:43 -04004024 btrfs_put_block_group(block_group);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004025 ret = alloc_reserved_file_extent(trans, root, 0, root_objectid,
4026 0, owner, offset, ins, 1);
Chris Masone02119d2008-09-05 16:13:11 -04004027 return ret;
4028}
4029
Chris Masone6dcd2d2008-07-17 12:53:50 -04004030/*
4031 * finds a free extent and does all the dirty work required for allocation
4032 * returns the key for the extent through ins, and a tree buffer for
4033 * the first block of the extent through buf.
4034 *
4035 * returns 0 if everything worked, non-zero otherwise.
4036 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004037static int alloc_tree_block(struct btrfs_trans_handle *trans,
4038 struct btrfs_root *root,
4039 u64 num_bytes, u64 parent, u64 root_objectid,
4040 struct btrfs_disk_key *key, int level,
4041 u64 empty_size, u64 hint_byte, u64 search_end,
4042 struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04004043{
4044 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004045 u64 flags = 0;
4046
4047 ret = __btrfs_reserve_extent(trans, root, num_bytes, num_bytes,
4048 empty_size, hint_byte, search_end,
4049 ins, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004050 BUG_ON(ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004051
4052 if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
4053 if (parent == 0)
4054 parent = ins->objectid;
4055 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
4056 } else
4057 BUG_ON(parent > 0);
4058
4059 update_reserved_extents(root, ins->objectid, ins->offset, 1);
Chris Masond00aff02008-09-11 15:54:42 -04004060 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004061 struct btrfs_delayed_extent_op *extent_op;
4062 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
4063 BUG_ON(!extent_op);
4064 if (key)
4065 memcpy(&extent_op->key, key, sizeof(extent_op->key));
4066 else
4067 memset(&extent_op->key, 0, sizeof(extent_op->key));
4068 extent_op->flags_to_set = flags;
4069 extent_op->update_key = 1;
4070 extent_op->update_flags = 1;
4071 extent_op->is_data = 0;
4072
4073 ret = btrfs_add_delayed_tree_ref(trans, ins->objectid,
4074 ins->offset, parent, root_objectid,
4075 level, BTRFS_ADD_DELAYED_EXTENT,
4076 extent_op);
Chris Masond00aff02008-09-11 15:54:42 -04004077 BUG_ON(ret);
Chris Masond00aff02008-09-11 15:54:42 -04004078 }
Chris Mason925baed2008-06-25 16:01:30 -04004079 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05004080}
Chris Mason65b51a02008-08-01 15:11:20 -04004081
4082struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
4083 struct btrfs_root *root,
Chris Mason4008c042009-02-12 14:09:45 -05004084 u64 bytenr, u32 blocksize,
4085 int level)
Chris Mason65b51a02008-08-01 15:11:20 -04004086{
4087 struct extent_buffer *buf;
4088
4089 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
4090 if (!buf)
4091 return ERR_PTR(-ENOMEM);
4092 btrfs_set_header_generation(buf, trans->transid);
Chris Mason4008c042009-02-12 14:09:45 -05004093 btrfs_set_buffer_lockdep_class(buf, level);
Chris Mason65b51a02008-08-01 15:11:20 -04004094 btrfs_tree_lock(buf);
4095 clean_tree_block(trans, root, buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004096
4097 btrfs_set_lock_blocking(buf);
Chris Mason65b51a02008-08-01 15:11:20 -04004098 btrfs_set_buffer_uptodate(buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004099
Chris Masond0c803c2008-09-11 16:17:57 -04004100 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
4101 set_extent_dirty(&root->dirty_log_pages, buf->start,
Chris Mason65b51a02008-08-01 15:11:20 -04004102 buf->start + buf->len - 1, GFP_NOFS);
Chris Masond0c803c2008-09-11 16:17:57 -04004103 } else {
4104 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
4105 buf->start + buf->len - 1, GFP_NOFS);
4106 }
Chris Mason65b51a02008-08-01 15:11:20 -04004107 trans->blocks_used++;
Chris Masonb4ce94d2009-02-04 09:25:08 -05004108 /* this returns a buffer locked for blocking */
Chris Mason65b51a02008-08-01 15:11:20 -04004109 return buf;
4110}
4111
Chris Masonfec577f2007-02-26 10:40:21 -05004112/*
4113 * helper function to allocate a block for a given tree
4114 * returns the tree buffer or NULL.
4115 */
Chris Mason5f39d392007-10-15 16:14:19 -04004116struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004117 struct btrfs_root *root, u32 blocksize,
4118 u64 parent, u64 root_objectid,
4119 struct btrfs_disk_key *key, int level,
4120 u64 hint, u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05004121{
Chris Masone2fa7222007-03-12 16:22:34 -04004122 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -05004123 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04004124 struct extent_buffer *buf;
Chris Masonfec577f2007-02-26 10:40:21 -05004125
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004126 ret = alloc_tree_block(trans, root, blocksize, parent, root_objectid,
4127 key, level, empty_size, hint, (u64)-1, &ins);
Chris Masonfec577f2007-02-26 10:40:21 -05004128 if (ret) {
Chris Mason54aa1f42007-06-22 14:16:25 -04004129 BUG_ON(ret > 0);
4130 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05004131 }
Chris Mason55c69072008-01-09 15:55:33 -05004132
Chris Mason4008c042009-02-12 14:09:45 -05004133 buf = btrfs_init_new_buffer(trans, root, ins.objectid,
4134 blocksize, level);
Chris Masonfec577f2007-02-26 10:40:21 -05004135 return buf;
4136}
Chris Masona28ec192007-03-06 20:08:01 -05004137
Chris Masone02119d2008-09-05 16:13:11 -04004138int btrfs_drop_leaf_ref(struct btrfs_trans_handle *trans,
4139 struct btrfs_root *root, struct extent_buffer *leaf)
Chris Mason6407bf62007-03-27 06:33:00 -04004140{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004141 u64 disk_bytenr;
4142 u64 num_bytes;
Chris Mason5f39d392007-10-15 16:14:19 -04004143 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -04004144 struct btrfs_file_extent_item *fi;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004145 u32 nritems;
Chris Mason6407bf62007-03-27 06:33:00 -04004146 int i;
Chris Mason6407bf62007-03-27 06:33:00 -04004147 int ret;
4148
Chris Mason5f39d392007-10-15 16:14:19 -04004149 BUG_ON(!btrfs_is_leaf(leaf));
4150 nritems = btrfs_header_nritems(leaf);
Chris Mason7bb86312007-12-11 09:25:06 -05004151
Chris Mason6407bf62007-03-27 06:33:00 -04004152 for (i = 0; i < nritems; i++) {
Chris Masone34a5b42008-07-22 12:08:37 -04004153 cond_resched();
Chris Mason5f39d392007-10-15 16:14:19 -04004154 btrfs_item_key_to_cpu(leaf, &key, i);
Chris Masonbd56b302009-02-04 09:27:02 -05004155
4156 /* only extents have references, skip everything else */
Chris Mason5f39d392007-10-15 16:14:19 -04004157 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason6407bf62007-03-27 06:33:00 -04004158 continue;
Chris Masonbd56b302009-02-04 09:27:02 -05004159
Chris Mason6407bf62007-03-27 06:33:00 -04004160 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
Chris Masonbd56b302009-02-04 09:27:02 -05004161
4162 /* inline extents live in the btree, they don't have refs */
Chris Mason5f39d392007-10-15 16:14:19 -04004163 if (btrfs_file_extent_type(leaf, fi) ==
4164 BTRFS_FILE_EXTENT_INLINE)
Chris Mason236454d2007-04-19 13:37:44 -04004165 continue;
Chris Masonbd56b302009-02-04 09:27:02 -05004166
Chris Masondb945352007-10-15 16:15:53 -04004167 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Chris Masonbd56b302009-02-04 09:27:02 -05004168
4169 /* holes don't have refs */
Chris Masondb945352007-10-15 16:15:53 -04004170 if (disk_bytenr == 0)
Chris Mason3a686372007-05-24 13:35:57 -04004171 continue;
Chris Mason4a096752008-07-21 10:29:44 -04004172
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004173 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
4174 ret = btrfs_free_extent(trans, root, disk_bytenr, num_bytes,
4175 leaf->start, 0, key.objectid, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04004176 BUG_ON(ret);
Chris Mason6407bf62007-03-27 06:33:00 -04004177 }
4178 return 0;
4179}
4180
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004181#if 0
4182
Chris Masond3977122009-01-05 21:25:51 -05004183static noinline int cache_drop_leaf_ref(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04004184 struct btrfs_root *root,
4185 struct btrfs_leaf_ref *ref)
Yan Zheng31153d82008-07-28 15:32:19 -04004186{
4187 int i;
4188 int ret;
Chris Masonbd56b302009-02-04 09:27:02 -05004189 struct btrfs_extent_info *info;
4190 struct refsort *sorted;
Yan Zheng31153d82008-07-28 15:32:19 -04004191
Chris Masonbd56b302009-02-04 09:27:02 -05004192 if (ref->nritems == 0)
4193 return 0;
4194
4195 sorted = kmalloc(sizeof(*sorted) * ref->nritems, GFP_NOFS);
Yan Zheng31153d82008-07-28 15:32:19 -04004196 for (i = 0; i < ref->nritems; i++) {
Chris Masonbd56b302009-02-04 09:27:02 -05004197 sorted[i].bytenr = ref->extents[i].bytenr;
4198 sorted[i].slot = i;
4199 }
4200 sort(sorted, ref->nritems, sizeof(struct refsort), refsort_cmp, NULL);
4201
4202 /*
4203 * the items in the ref were sorted when the ref was inserted
4204 * into the ref cache, so this is already in order
4205 */
4206 for (i = 0; i < ref->nritems; i++) {
4207 info = ref->extents + sorted[i].slot;
Chris Mason56bec292009-03-13 10:10:06 -04004208 ret = btrfs_free_extent(trans, root, info->bytenr,
Zheng Yan31840ae2008-09-23 13:14:14 -04004209 info->num_bytes, ref->bytenr,
4210 ref->owner, ref->generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004211 info->objectid, 0);
Chris Mason2dd3e672008-08-04 08:20:15 -04004212
4213 atomic_inc(&root->fs_info->throttle_gen);
4214 wake_up(&root->fs_info->transaction_throttle);
4215 cond_resched();
4216
Yan Zheng31153d82008-07-28 15:32:19 -04004217 BUG_ON(ret);
4218 info++;
4219 }
Yan Zheng31153d82008-07-28 15:32:19 -04004220
Chris Mason806638b2009-02-05 09:08:14 -05004221 kfree(sorted);
Yan Zheng31153d82008-07-28 15:32:19 -04004222 return 0;
4223}
4224
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004225
Chris Mason56bec292009-03-13 10:10:06 -04004226static int drop_snap_lookup_refcount(struct btrfs_trans_handle *trans,
4227 struct btrfs_root *root, u64 start,
Chris Masond3977122009-01-05 21:25:51 -05004228 u64 len, u32 *refs)
Chris Mason333db942008-06-25 16:01:30 -04004229{
Chris Mason017e5362008-07-28 15:32:51 -04004230 int ret;
Chris Masonf87f0572008-08-01 11:27:23 -04004231
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004232 ret = btrfs_lookup_extent_refs(trans, root, start, len, refs);
Chris Masonf87f0572008-08-01 11:27:23 -04004233 BUG_ON(ret);
4234
Chris Masond3977122009-01-05 21:25:51 -05004235#if 0 /* some debugging code in case we see problems here */
Chris Masonf87f0572008-08-01 11:27:23 -04004236 /* if the refs count is one, it won't get increased again. But
4237 * if the ref count is > 1, someone may be decreasing it at
4238 * the same time we are.
4239 */
4240 if (*refs != 1) {
4241 struct extent_buffer *eb = NULL;
4242 eb = btrfs_find_create_tree_block(root, start, len);
4243 if (eb)
4244 btrfs_tree_lock(eb);
4245
4246 mutex_lock(&root->fs_info->alloc_mutex);
4247 ret = lookup_extent_ref(NULL, root, start, len, refs);
4248 BUG_ON(ret);
4249 mutex_unlock(&root->fs_info->alloc_mutex);
4250
4251 if (eb) {
4252 btrfs_tree_unlock(eb);
4253 free_extent_buffer(eb);
4254 }
4255 if (*refs == 1) {
Chris Masond3977122009-01-05 21:25:51 -05004256 printk(KERN_ERR "btrfs block %llu went down to one "
4257 "during drop_snap\n", (unsigned long long)start);
Chris Masonf87f0572008-08-01 11:27:23 -04004258 }
4259
4260 }
4261#endif
4262
Chris Masone7a84562008-06-25 16:01:31 -04004263 cond_resched();
Chris Mason017e5362008-07-28 15:32:51 -04004264 return ret;
Chris Mason333db942008-06-25 16:01:30 -04004265}
4266
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004267
Chris Mason333db942008-06-25 16:01:30 -04004268/*
Chris Masonbd56b302009-02-04 09:27:02 -05004269 * this is used while deleting old snapshots, and it drops the refs
4270 * on a whole subtree starting from a level 1 node.
4271 *
4272 * The idea is to sort all the leaf pointers, and then drop the
4273 * ref on all the leaves in order. Most of the time the leaves
4274 * will have ref cache entries, so no leaf IOs will be required to
4275 * find the extents they have references on.
4276 *
4277 * For each leaf, any references it has are also dropped in order
4278 *
4279 * This ends up dropping the references in something close to optimal
4280 * order for reading and modifying the extent allocation tree.
4281 */
4282static noinline int drop_level_one_refs(struct btrfs_trans_handle *trans,
4283 struct btrfs_root *root,
4284 struct btrfs_path *path)
4285{
4286 u64 bytenr;
4287 u64 root_owner;
4288 u64 root_gen;
4289 struct extent_buffer *eb = path->nodes[1];
4290 struct extent_buffer *leaf;
4291 struct btrfs_leaf_ref *ref;
4292 struct refsort *sorted = NULL;
4293 int nritems = btrfs_header_nritems(eb);
4294 int ret;
4295 int i;
4296 int refi = 0;
4297 int slot = path->slots[1];
4298 u32 blocksize = btrfs_level_size(root, 0);
4299 u32 refs;
4300
4301 if (nritems == 0)
4302 goto out;
4303
4304 root_owner = btrfs_header_owner(eb);
4305 root_gen = btrfs_header_generation(eb);
4306 sorted = kmalloc(sizeof(*sorted) * nritems, GFP_NOFS);
4307
4308 /*
4309 * step one, sort all the leaf pointers so we don't scribble
4310 * randomly into the extent allocation tree
4311 */
4312 for (i = slot; i < nritems; i++) {
4313 sorted[refi].bytenr = btrfs_node_blockptr(eb, i);
4314 sorted[refi].slot = i;
4315 refi++;
4316 }
4317
4318 /*
4319 * nritems won't be zero, but if we're picking up drop_snapshot
4320 * after a crash, slot might be > 0, so double check things
4321 * just in case.
4322 */
4323 if (refi == 0)
4324 goto out;
4325
4326 sort(sorted, refi, sizeof(struct refsort), refsort_cmp, NULL);
4327
4328 /*
4329 * the first loop frees everything the leaves point to
4330 */
4331 for (i = 0; i < refi; i++) {
4332 u64 ptr_gen;
4333
4334 bytenr = sorted[i].bytenr;
4335
4336 /*
4337 * check the reference count on this leaf. If it is > 1
4338 * we just decrement it below and don't update any
4339 * of the refs the leaf points to.
4340 */
Chris Mason56bec292009-03-13 10:10:06 -04004341 ret = drop_snap_lookup_refcount(trans, root, bytenr,
4342 blocksize, &refs);
Chris Masonbd56b302009-02-04 09:27:02 -05004343 BUG_ON(ret);
4344 if (refs != 1)
4345 continue;
4346
4347 ptr_gen = btrfs_node_ptr_generation(eb, sorted[i].slot);
4348
4349 /*
4350 * the leaf only had one reference, which means the
4351 * only thing pointing to this leaf is the snapshot
4352 * we're deleting. It isn't possible for the reference
4353 * count to increase again later
4354 *
4355 * The reference cache is checked for the leaf,
4356 * and if found we'll be able to drop any refs held by
4357 * the leaf without needing to read it in.
4358 */
4359 ref = btrfs_lookup_leaf_ref(root, bytenr);
4360 if (ref && ref->generation != ptr_gen) {
4361 btrfs_free_leaf_ref(root, ref);
4362 ref = NULL;
4363 }
4364 if (ref) {
4365 ret = cache_drop_leaf_ref(trans, root, ref);
4366 BUG_ON(ret);
4367 btrfs_remove_leaf_ref(root, ref);
4368 btrfs_free_leaf_ref(root, ref);
4369 } else {
4370 /*
4371 * the leaf wasn't in the reference cache, so
4372 * we have to read it.
4373 */
4374 leaf = read_tree_block(root, bytenr, blocksize,
4375 ptr_gen);
4376 ret = btrfs_drop_leaf_ref(trans, root, leaf);
4377 BUG_ON(ret);
4378 free_extent_buffer(leaf);
4379 }
4380 atomic_inc(&root->fs_info->throttle_gen);
4381 wake_up(&root->fs_info->transaction_throttle);
4382 cond_resched();
4383 }
4384
4385 /*
4386 * run through the loop again to free the refs on the leaves.
4387 * This is faster than doing it in the loop above because
4388 * the leaves are likely to be clustered together. We end up
4389 * working in nice chunks on the extent allocation tree.
4390 */
4391 for (i = 0; i < refi; i++) {
4392 bytenr = sorted[i].bytenr;
Chris Mason56bec292009-03-13 10:10:06 -04004393 ret = btrfs_free_extent(trans, root, bytenr,
Chris Masonbd56b302009-02-04 09:27:02 -05004394 blocksize, eb->start,
4395 root_owner, root_gen, 0, 1);
4396 BUG_ON(ret);
4397
4398 atomic_inc(&root->fs_info->throttle_gen);
4399 wake_up(&root->fs_info->transaction_throttle);
4400 cond_resched();
4401 }
4402out:
4403 kfree(sorted);
4404
4405 /*
4406 * update the path to show we've processed the entire level 1
4407 * node. This will get saved into the root's drop_snapshot_progress
4408 * field so these drops are not repeated again if this transaction
4409 * commits.
4410 */
4411 path->slots[1] = nritems;
4412 return 0;
4413}
4414
4415/*
Chris Mason9aca1d52007-03-13 11:09:37 -04004416 * helper function for drop_snapshot, this walks down the tree dropping ref
4417 * counts as it goes.
4418 */
Chris Masond3977122009-01-05 21:25:51 -05004419static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05004420 struct btrfs_root *root,
4421 struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05004422{
Chris Mason7bb86312007-12-11 09:25:06 -05004423 u64 root_owner;
4424 u64 root_gen;
4425 u64 bytenr;
Chris Masonca7a79a2008-05-12 12:59:19 -04004426 u64 ptr_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04004427 struct extent_buffer *next;
4428 struct extent_buffer *cur;
Chris Mason7bb86312007-12-11 09:25:06 -05004429 struct extent_buffer *parent;
Chris Masondb945352007-10-15 16:15:53 -04004430 u32 blocksize;
Chris Mason20524f02007-03-10 06:35:47 -05004431 int ret;
4432 u32 refs;
4433
Chris Mason5caf2a02007-04-02 11:20:42 -04004434 WARN_ON(*level < 0);
4435 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason56bec292009-03-13 10:10:06 -04004436 ret = drop_snap_lookup_refcount(trans, root, path->nodes[*level]->start,
Chris Masondb945352007-10-15 16:15:53 -04004437 path->nodes[*level]->len, &refs);
Chris Mason20524f02007-03-10 06:35:47 -05004438 BUG_ON(ret);
4439 if (refs > 1)
4440 goto out;
Chris Masone0115992007-06-19 16:23:05 -04004441
Chris Mason9aca1d52007-03-13 11:09:37 -04004442 /*
4443 * walk down to the last node level and free all the leaves
4444 */
Chris Masond3977122009-01-05 21:25:51 -05004445 while (*level >= 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -04004446 WARN_ON(*level < 0);
4447 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason20524f02007-03-10 06:35:47 -05004448 cur = path->nodes[*level];
Chris Masone0115992007-06-19 16:23:05 -04004449
Chris Mason5f39d392007-10-15 16:14:19 -04004450 if (btrfs_header_level(cur) != *level)
Chris Mason2c90e5d2007-04-02 10:50:19 -04004451 WARN_ON(1);
Chris Masone0115992007-06-19 16:23:05 -04004452
Chris Mason7518a232007-03-12 12:01:18 -04004453 if (path->slots[*level] >=
Chris Mason5f39d392007-10-15 16:14:19 -04004454 btrfs_header_nritems(cur))
Chris Mason20524f02007-03-10 06:35:47 -05004455 break;
Chris Masonbd56b302009-02-04 09:27:02 -05004456
4457 /* the new code goes down to level 1 and does all the
4458 * leaves pointed to that node in bulk. So, this check
4459 * for level 0 will always be false.
4460 *
4461 * But, the disk format allows the drop_snapshot_progress
4462 * field in the root to leave things in a state where
4463 * a leaf will need cleaning up here. If someone crashes
4464 * with the old code and then boots with the new code,
4465 * we might find a leaf here.
4466 */
Chris Mason6407bf62007-03-27 06:33:00 -04004467 if (*level == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004468 ret = btrfs_drop_leaf_ref(trans, root, cur);
Chris Mason6407bf62007-03-27 06:33:00 -04004469 BUG_ON(ret);
4470 break;
4471 }
Chris Masonbd56b302009-02-04 09:27:02 -05004472
4473 /*
4474 * once we get to level one, process the whole node
4475 * at once, including everything below it.
4476 */
4477 if (*level == 1) {
4478 ret = drop_level_one_refs(trans, root, path);
4479 BUG_ON(ret);
4480 break;
4481 }
4482
Chris Masondb945352007-10-15 16:15:53 -04004483 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
Chris Masonca7a79a2008-05-12 12:59:19 -04004484 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
Chris Masondb945352007-10-15 16:15:53 -04004485 blocksize = btrfs_level_size(root, *level - 1);
Chris Mason925baed2008-06-25 16:01:30 -04004486
Chris Mason56bec292009-03-13 10:10:06 -04004487 ret = drop_snap_lookup_refcount(trans, root, bytenr,
4488 blocksize, &refs);
Chris Mason6407bf62007-03-27 06:33:00 -04004489 BUG_ON(ret);
Chris Masonbd56b302009-02-04 09:27:02 -05004490
4491 /*
4492 * if there is more than one reference, we don't need
4493 * to read that node to drop any references it has. We
4494 * just drop the ref we hold on that node and move on to the
4495 * next slot in this level.
4496 */
Chris Mason6407bf62007-03-27 06:33:00 -04004497 if (refs != 1) {
Chris Mason7bb86312007-12-11 09:25:06 -05004498 parent = path->nodes[*level];
4499 root_owner = btrfs_header_owner(parent);
4500 root_gen = btrfs_header_generation(parent);
Chris Mason20524f02007-03-10 06:35:47 -05004501 path->slots[*level]++;
Chris Masonf87f0572008-08-01 11:27:23 -04004502
Chris Mason56bec292009-03-13 10:10:06 -04004503 ret = btrfs_free_extent(trans, root, bytenr,
Zheng Yan31840ae2008-09-23 13:14:14 -04004504 blocksize, parent->start,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004505 root_owner, root_gen,
4506 *level - 1, 1);
Chris Mason20524f02007-03-10 06:35:47 -05004507 BUG_ON(ret);
Chris Mason18e35e02008-08-01 13:11:41 -04004508
4509 atomic_inc(&root->fs_info->throttle_gen);
4510 wake_up(&root->fs_info->transaction_throttle);
Chris Mason2dd3e672008-08-04 08:20:15 -04004511 cond_resched();
Chris Mason18e35e02008-08-01 13:11:41 -04004512
Chris Mason20524f02007-03-10 06:35:47 -05004513 continue;
4514 }
Chris Mason333db942008-06-25 16:01:30 -04004515
Chris Masonbd56b302009-02-04 09:27:02 -05004516 /*
4517 * we need to keep freeing things in the next level down.
4518 * read the block and loop around to process it
4519 */
4520 next = read_tree_block(root, bytenr, blocksize, ptr_gen);
Chris Mason5caf2a02007-04-02 11:20:42 -04004521 WARN_ON(*level <= 0);
Chris Mason83e15a22007-03-12 09:03:27 -04004522 if (path->nodes[*level-1])
Chris Mason5f39d392007-10-15 16:14:19 -04004523 free_extent_buffer(path->nodes[*level-1]);
Chris Mason20524f02007-03-10 06:35:47 -05004524 path->nodes[*level-1] = next;
Chris Mason5f39d392007-10-15 16:14:19 -04004525 *level = btrfs_header_level(next);
Chris Mason20524f02007-03-10 06:35:47 -05004526 path->slots[*level] = 0;
Chris Mason2dd3e672008-08-04 08:20:15 -04004527 cond_resched();
Chris Mason20524f02007-03-10 06:35:47 -05004528 }
4529out:
Chris Mason5caf2a02007-04-02 11:20:42 -04004530 WARN_ON(*level < 0);
4531 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason7bb86312007-12-11 09:25:06 -05004532
4533 if (path->nodes[*level] == root->node) {
Chris Mason7bb86312007-12-11 09:25:06 -05004534 parent = path->nodes[*level];
Yan Zheng31153d82008-07-28 15:32:19 -04004535 bytenr = path->nodes[*level]->start;
Chris Mason7bb86312007-12-11 09:25:06 -05004536 } else {
4537 parent = path->nodes[*level + 1];
Yan Zheng31153d82008-07-28 15:32:19 -04004538 bytenr = btrfs_node_blockptr(parent, path->slots[*level + 1]);
Chris Mason7bb86312007-12-11 09:25:06 -05004539 }
4540
Yan Zheng31153d82008-07-28 15:32:19 -04004541 blocksize = btrfs_level_size(root, *level);
4542 root_owner = btrfs_header_owner(parent);
Chris Mason7bb86312007-12-11 09:25:06 -05004543 root_gen = btrfs_header_generation(parent);
Yan Zheng31153d82008-07-28 15:32:19 -04004544
Chris Masonbd56b302009-02-04 09:27:02 -05004545 /*
4546 * cleanup and free the reference on the last node
4547 * we processed
4548 */
Chris Mason56bec292009-03-13 10:10:06 -04004549 ret = btrfs_free_extent(trans, root, bytenr, blocksize,
Zheng Yan31840ae2008-09-23 13:14:14 -04004550 parent->start, root_owner, root_gen,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004551 *level, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04004552 free_extent_buffer(path->nodes[*level]);
Chris Mason20524f02007-03-10 06:35:47 -05004553 path->nodes[*level] = NULL;
Chris Masonbd56b302009-02-04 09:27:02 -05004554
Chris Mason20524f02007-03-10 06:35:47 -05004555 *level += 1;
4556 BUG_ON(ret);
Chris Masonf87f0572008-08-01 11:27:23 -04004557
Chris Masone7a84562008-06-25 16:01:31 -04004558 cond_resched();
Chris Mason20524f02007-03-10 06:35:47 -05004559 return 0;
4560}
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004561#endif
Chris Mason20524f02007-03-10 06:35:47 -05004562
Chris Mason9aca1d52007-03-13 11:09:37 -04004563/*
Yan Zhengf82d02d2008-10-29 14:49:05 -04004564 * helper function for drop_subtree, this function is similar to
4565 * walk_down_tree. The main difference is that it checks reference
4566 * counts while tree blocks are locked.
4567 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004568static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
4569 struct btrfs_root *root,
4570 struct btrfs_path *path, int *level)
Yan Zhengf82d02d2008-10-29 14:49:05 -04004571{
4572 struct extent_buffer *next;
4573 struct extent_buffer *cur;
4574 struct extent_buffer *parent;
4575 u64 bytenr;
4576 u64 ptr_gen;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004577 u64 refs;
4578 u64 flags;
Yan Zhengf82d02d2008-10-29 14:49:05 -04004579 u32 blocksize;
Yan Zhengf82d02d2008-10-29 14:49:05 -04004580 int ret;
4581
4582 cur = path->nodes[*level];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004583 ret = btrfs_lookup_extent_info(trans, root, cur->start, cur->len,
4584 &refs, &flags);
Yan Zhengf82d02d2008-10-29 14:49:05 -04004585 BUG_ON(ret);
4586 if (refs > 1)
4587 goto out;
4588
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004589 BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
4590
Yan Zhengf82d02d2008-10-29 14:49:05 -04004591 while (*level >= 0) {
4592 cur = path->nodes[*level];
4593 if (*level == 0) {
4594 ret = btrfs_drop_leaf_ref(trans, root, cur);
4595 BUG_ON(ret);
4596 clean_tree_block(trans, root, cur);
4597 break;
4598 }
4599 if (path->slots[*level] >= btrfs_header_nritems(cur)) {
4600 clean_tree_block(trans, root, cur);
4601 break;
4602 }
4603
4604 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
4605 blocksize = btrfs_level_size(root, *level - 1);
4606 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
4607
4608 next = read_tree_block(root, bytenr, blocksize, ptr_gen);
4609 btrfs_tree_lock(next);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004610 btrfs_set_lock_blocking(next);
Yan Zhengf82d02d2008-10-29 14:49:05 -04004611
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004612 ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize,
4613 &refs, &flags);
Yan Zhengf82d02d2008-10-29 14:49:05 -04004614 BUG_ON(ret);
4615 if (refs > 1) {
4616 parent = path->nodes[*level];
4617 ret = btrfs_free_extent(trans, root, bytenr,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004618 blocksize, parent->start,
4619 btrfs_header_owner(parent),
4620 *level - 1, 0);
Yan Zhengf82d02d2008-10-29 14:49:05 -04004621 BUG_ON(ret);
4622 path->slots[*level]++;
4623 btrfs_tree_unlock(next);
4624 free_extent_buffer(next);
4625 continue;
4626 }
4627
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004628 BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
4629
Yan Zhengf82d02d2008-10-29 14:49:05 -04004630 *level = btrfs_header_level(next);
4631 path->nodes[*level] = next;
4632 path->slots[*level] = 0;
4633 path->locks[*level] = 1;
4634 cond_resched();
4635 }
4636out:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004637 if (path->nodes[*level] == root->node)
4638 parent = path->nodes[*level];
4639 else
4640 parent = path->nodes[*level + 1];
Yan Zhengf82d02d2008-10-29 14:49:05 -04004641 bytenr = path->nodes[*level]->start;
4642 blocksize = path->nodes[*level]->len;
4643
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004644 ret = btrfs_free_extent(trans, root, bytenr, blocksize, parent->start,
4645 btrfs_header_owner(parent), *level, 0);
Yan Zhengf82d02d2008-10-29 14:49:05 -04004646 BUG_ON(ret);
4647
4648 if (path->locks[*level]) {
4649 btrfs_tree_unlock(path->nodes[*level]);
4650 path->locks[*level] = 0;
4651 }
4652 free_extent_buffer(path->nodes[*level]);
4653 path->nodes[*level] = NULL;
4654 *level += 1;
4655 cond_resched();
4656 return 0;
4657}
4658
4659/*
Chris Mason9aca1d52007-03-13 11:09:37 -04004660 * helper for dropping snapshots. This walks back up the tree in the path
4661 * to find the first node higher up where we haven't yet gone through
4662 * all the slots
4663 */
Chris Masond3977122009-01-05 21:25:51 -05004664static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05004665 struct btrfs_root *root,
Yan Zhengf82d02d2008-10-29 14:49:05 -04004666 struct btrfs_path *path,
4667 int *level, int max_level)
Chris Mason20524f02007-03-10 06:35:47 -05004668{
Chris Mason7bb86312007-12-11 09:25:06 -05004669 struct btrfs_root_item *root_item = &root->root_item;
Chris Mason20524f02007-03-10 06:35:47 -05004670 int i;
4671 int slot;
4672 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04004673
Yan Zhengf82d02d2008-10-29 14:49:05 -04004674 for (i = *level; i < max_level && path->nodes[i]; i++) {
Chris Mason20524f02007-03-10 06:35:47 -05004675 slot = path->slots[i];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004676 if (slot + 1 < btrfs_header_nritems(path->nodes[i])) {
Chris Masonbd56b302009-02-04 09:27:02 -05004677 /*
4678 * there is more work to do in this level.
4679 * Update the drop_progress marker to reflect
4680 * the work we've done so far, and then bump
4681 * the slot number
4682 */
Chris Mason20524f02007-03-10 06:35:47 -05004683 path->slots[i]++;
Chris Mason9f3a7422007-08-07 15:52:19 -04004684 WARN_ON(*level == 0);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004685 if (max_level == BTRFS_MAX_LEVEL) {
4686 btrfs_node_key(path->nodes[i],
4687 &root_item->drop_progress,
4688 path->slots[i]);
4689 root_item->drop_level = i;
4690 }
4691 *level = i;
Chris Mason20524f02007-03-10 06:35:47 -05004692 return 0;
4693 } else {
Zheng Yan31840ae2008-09-23 13:14:14 -04004694 struct extent_buffer *parent;
Chris Masonbd56b302009-02-04 09:27:02 -05004695
4696 /*
4697 * this whole node is done, free our reference
4698 * on it and go up one level
4699 */
Zheng Yan31840ae2008-09-23 13:14:14 -04004700 if (path->nodes[*level] == root->node)
4701 parent = path->nodes[*level];
4702 else
4703 parent = path->nodes[*level + 1];
4704
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004705 clean_tree_block(trans, root, path->nodes[i]);
Chris Masone089f052007-03-16 16:20:31 -04004706 ret = btrfs_free_extent(trans, root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004707 path->nodes[i]->start,
4708 path->nodes[i]->len,
4709 parent->start,
4710 btrfs_header_owner(parent),
4711 *level, 0);
Chris Mason6407bf62007-03-27 06:33:00 -04004712 BUG_ON(ret);
Yan Zhengf82d02d2008-10-29 14:49:05 -04004713 if (path->locks[*level]) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004714 btrfs_tree_unlock(path->nodes[i]);
4715 path->locks[i] = 0;
Yan Zhengf82d02d2008-10-29 14:49:05 -04004716 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004717 free_extent_buffer(path->nodes[i]);
4718 path->nodes[i] = NULL;
Chris Mason20524f02007-03-10 06:35:47 -05004719 *level = i + 1;
Chris Mason20524f02007-03-10 06:35:47 -05004720 }
4721 }
4722 return 1;
4723}
4724
Chris Mason9aca1d52007-03-13 11:09:37 -04004725/*
4726 * drop the reference count on the tree rooted at 'snap'. This traverses
4727 * the tree freeing any blocks that have a ref count of zero after being
4728 * decremented.
4729 */
Chris Masone089f052007-03-16 16:20:31 -04004730int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason9f3a7422007-08-07 15:52:19 -04004731 *root)
Chris Mason20524f02007-03-10 06:35:47 -05004732{
Chris Mason3768f362007-03-13 16:47:54 -04004733 int ret = 0;
Chris Mason9aca1d52007-03-13 11:09:37 -04004734 int wret;
Chris Mason20524f02007-03-10 06:35:47 -05004735 int level;
Chris Mason5caf2a02007-04-02 11:20:42 -04004736 struct btrfs_path *path;
Chris Masonc3e69d52009-03-13 10:17:05 -04004737 int update_count;
Chris Mason9f3a7422007-08-07 15:52:19 -04004738 struct btrfs_root_item *root_item = &root->root_item;
Chris Mason20524f02007-03-10 06:35:47 -05004739
Chris Mason5caf2a02007-04-02 11:20:42 -04004740 path = btrfs_alloc_path();
4741 BUG_ON(!path);
Chris Mason20524f02007-03-10 06:35:47 -05004742
Chris Mason5f39d392007-10-15 16:14:19 -04004743 level = btrfs_header_level(root->node);
Chris Mason9f3a7422007-08-07 15:52:19 -04004744 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004745 path->nodes[level] = btrfs_lock_root_node(root);
4746 btrfs_set_lock_blocking(path->nodes[level]);
Chris Mason9f3a7422007-08-07 15:52:19 -04004747 path->slots[level] = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004748 path->locks[level] = 1;
Chris Mason9f3a7422007-08-07 15:52:19 -04004749 } else {
4750 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04004751 struct btrfs_disk_key found_key;
4752 struct extent_buffer *node;
Chris Mason6702ed42007-08-07 16:15:09 -04004753
Chris Mason9f3a7422007-08-07 15:52:19 -04004754 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Chris Mason6702ed42007-08-07 16:15:09 -04004755 level = root_item->drop_level;
4756 path->lowest_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04004757 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason6702ed42007-08-07 16:15:09 -04004758 if (wret < 0) {
Chris Mason9f3a7422007-08-07 15:52:19 -04004759 ret = wret;
4760 goto out;
4761 }
Chris Mason5f39d392007-10-15 16:14:19 -04004762 node = path->nodes[level];
4763 btrfs_node_key(node, &found_key, path->slots[level]);
4764 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
4765 sizeof(found_key)));
Chris Mason7d9eb122008-07-08 14:19:17 -04004766 /*
4767 * unlock our path, this is safe because only this
4768 * function is allowed to delete this snapshot
4769 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004770 btrfs_unlock_up_safe(path, 0);
Chris Mason9f3a7422007-08-07 15:52:19 -04004771 }
Chris Masond3977122009-01-05 21:25:51 -05004772 while (1) {
Chris Masonc3e69d52009-03-13 10:17:05 -04004773 unsigned long update;
Chris Mason5caf2a02007-04-02 11:20:42 -04004774 wret = walk_down_tree(trans, root, path, &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;
4779
Yan Zhengf82d02d2008-10-29 14:49:05 -04004780 wret = walk_up_tree(trans, root, path, &level,
4781 BTRFS_MAX_LEVEL);
Chris Mason9aca1d52007-03-13 11:09:37 -04004782 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05004783 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04004784 if (wret < 0)
4785 ret = wret;
Chris Masonb7ec40d2009-03-12 20:12:45 -04004786 if (trans->transaction->in_commit ||
4787 trans->transaction->delayed_refs.flushing) {
Chris Masone7a84562008-06-25 16:01:31 -04004788 ret = -EAGAIN;
4789 break;
4790 }
Chris Masonc3e69d52009-03-13 10:17:05 -04004791 for (update_count = 0; update_count < 16; update_count++) {
4792 update = trans->delayed_ref_updates;
4793 trans->delayed_ref_updates = 0;
4794 if (update)
4795 btrfs_run_delayed_refs(trans, root, update);
4796 else
4797 break;
4798 }
Chris Mason20524f02007-03-10 06:35:47 -05004799 }
Chris Mason9f3a7422007-08-07 15:52:19 -04004800out:
Chris Mason5caf2a02007-04-02 11:20:42 -04004801 btrfs_free_path(path);
Chris Mason9aca1d52007-03-13 11:09:37 -04004802 return ret;
Chris Mason20524f02007-03-10 06:35:47 -05004803}
Chris Mason9078a3e2007-04-26 16:46:15 -04004804
Yan Zhengf82d02d2008-10-29 14:49:05 -04004805int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
4806 struct btrfs_root *root,
4807 struct extent_buffer *node,
4808 struct extent_buffer *parent)
4809{
4810 struct btrfs_path *path;
4811 int level;
4812 int parent_level;
4813 int ret = 0;
4814 int wret;
4815
4816 path = btrfs_alloc_path();
4817 BUG_ON(!path);
4818
Chris Masonb9447ef2009-03-09 11:45:38 -04004819 btrfs_assert_tree_locked(parent);
Yan Zhengf82d02d2008-10-29 14:49:05 -04004820 parent_level = btrfs_header_level(parent);
4821 extent_buffer_get(parent);
4822 path->nodes[parent_level] = parent;
4823 path->slots[parent_level] = btrfs_header_nritems(parent);
4824
Chris Masonb9447ef2009-03-09 11:45:38 -04004825 btrfs_assert_tree_locked(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04004826 level = btrfs_header_level(node);
4827 extent_buffer_get(node);
4828 path->nodes[level] = node;
4829 path->slots[level] = 0;
4830
4831 while (1) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004832 wret = walk_down_tree(trans, root, path, &level);
Yan Zhengf82d02d2008-10-29 14:49:05 -04004833 if (wret < 0)
4834 ret = wret;
4835 if (wret != 0)
4836 break;
4837
4838 wret = walk_up_tree(trans, root, path, &level, parent_level);
4839 if (wret < 0)
4840 ret = wret;
4841 if (wret != 0)
4842 break;
4843 }
4844
4845 btrfs_free_path(path);
4846 return ret;
4847}
4848
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004849#if 0
Chris Mason8e7bf942008-04-28 09:02:36 -04004850static unsigned long calc_ra(unsigned long start, unsigned long last,
4851 unsigned long nr)
4852{
4853 return min(last, start + nr - 1);
4854}
4855
Chris Masond3977122009-01-05 21:25:51 -05004856static noinline int relocate_inode_pages(struct inode *inode, u64 start,
Chris Mason98ed5172008-01-03 10:01:48 -05004857 u64 len)
Chris Masonedbd8d42007-12-21 16:27:24 -05004858{
4859 u64 page_start;
4860 u64 page_end;
Zheng Yan1a40e232008-09-26 10:09:34 -04004861 unsigned long first_index;
Chris Masonedbd8d42007-12-21 16:27:24 -05004862 unsigned long last_index;
Chris Masonedbd8d42007-12-21 16:27:24 -05004863 unsigned long i;
4864 struct page *page;
Chris Masond1310b22008-01-24 16:13:08 -05004865 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason4313b392008-01-03 09:08:48 -05004866 struct file_ra_state *ra;
Chris Mason3eaa2882008-07-24 11:57:52 -04004867 struct btrfs_ordered_extent *ordered;
Zheng Yan1a40e232008-09-26 10:09:34 -04004868 unsigned int total_read = 0;
4869 unsigned int total_dirty = 0;
4870 int ret = 0;
Chris Mason4313b392008-01-03 09:08:48 -05004871
4872 ra = kzalloc(sizeof(*ra), GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05004873
4874 mutex_lock(&inode->i_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04004875 first_index = start >> PAGE_CACHE_SHIFT;
Chris Masonedbd8d42007-12-21 16:27:24 -05004876 last_index = (start + len - 1) >> PAGE_CACHE_SHIFT;
4877
Zheng Yan1a40e232008-09-26 10:09:34 -04004878 /* make sure the dirty trick played by the caller work */
4879 ret = invalidate_inode_pages2_range(inode->i_mapping,
4880 first_index, last_index);
4881 if (ret)
4882 goto out_unlock;
Chris Mason8e7bf942008-04-28 09:02:36 -04004883
Chris Mason4313b392008-01-03 09:08:48 -05004884 file_ra_state_init(ra, inode->i_mapping);
Chris Masonedbd8d42007-12-21 16:27:24 -05004885
Zheng Yan1a40e232008-09-26 10:09:34 -04004886 for (i = first_index ; i <= last_index; i++) {
4887 if (total_read % ra->ra_pages == 0) {
Chris Mason8e7bf942008-04-28 09:02:36 -04004888 btrfs_force_ra(inode->i_mapping, ra, NULL, i,
Zheng Yan1a40e232008-09-26 10:09:34 -04004889 calc_ra(i, last_index, ra->ra_pages));
Chris Mason8e7bf942008-04-28 09:02:36 -04004890 }
4891 total_read++;
Chris Mason3eaa2882008-07-24 11:57:52 -04004892again:
4893 if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode))
Zheng Yan1a40e232008-09-26 10:09:34 -04004894 BUG_ON(1);
Chris Masonedbd8d42007-12-21 16:27:24 -05004895 page = grab_cache_page(inode->i_mapping, i);
Chris Masona061fc82008-05-07 11:43:44 -04004896 if (!page) {
Zheng Yan1a40e232008-09-26 10:09:34 -04004897 ret = -ENOMEM;
Chris Masonedbd8d42007-12-21 16:27:24 -05004898 goto out_unlock;
Chris Masona061fc82008-05-07 11:43:44 -04004899 }
Chris Masonedbd8d42007-12-21 16:27:24 -05004900 if (!PageUptodate(page)) {
4901 btrfs_readpage(NULL, page);
4902 lock_page(page);
4903 if (!PageUptodate(page)) {
4904 unlock_page(page);
4905 page_cache_release(page);
Zheng Yan1a40e232008-09-26 10:09:34 -04004906 ret = -EIO;
Chris Masonedbd8d42007-12-21 16:27:24 -05004907 goto out_unlock;
4908 }
4909 }
Chris Masonec44a352008-04-28 15:29:52 -04004910 wait_on_page_writeback(page);
Chris Mason3eaa2882008-07-24 11:57:52 -04004911
Chris Masonedbd8d42007-12-21 16:27:24 -05004912 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
4913 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05004914 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05004915
Chris Mason3eaa2882008-07-24 11:57:52 -04004916 ordered = btrfs_lookup_ordered_extent(inode, page_start);
4917 if (ordered) {
4918 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
4919 unlock_page(page);
4920 page_cache_release(page);
4921 btrfs_start_ordered_extent(inode, ordered, 1);
4922 btrfs_put_ordered_extent(ordered);
4923 goto again;
4924 }
4925 set_page_extent_mapped(page);
4926
Zheng Yan1a40e232008-09-26 10:09:34 -04004927 if (i == first_index)
4928 set_extent_bits(io_tree, page_start, page_end,
4929 EXTENT_BOUNDARY, GFP_NOFS);
Yan Zheng1f80e4d2008-12-19 10:59:04 -05004930 btrfs_set_extent_delalloc(inode, page_start, page_end);
Zheng Yan1a40e232008-09-26 10:09:34 -04004931
Chris Masona061fc82008-05-07 11:43:44 -04004932 set_page_dirty(page);
Zheng Yan1a40e232008-09-26 10:09:34 -04004933 total_dirty++;
Chris Masonedbd8d42007-12-21 16:27:24 -05004934
Chris Masond1310b22008-01-24 16:13:08 -05004935 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05004936 unlock_page(page);
4937 page_cache_release(page);
4938 }
4939
4940out_unlock:
Chris Masonec44a352008-04-28 15:29:52 -04004941 kfree(ra);
Chris Masonedbd8d42007-12-21 16:27:24 -05004942 mutex_unlock(&inode->i_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04004943 balance_dirty_pages_ratelimited_nr(inode->i_mapping, total_dirty);
Chris Masonbf4ef672008-05-08 13:26:18 -04004944 return ret;
4945}
4946
Chris Masond3977122009-01-05 21:25:51 -05004947static noinline int relocate_data_extent(struct inode *reloc_inode,
Zheng Yan1a40e232008-09-26 10:09:34 -04004948 struct btrfs_key *extent_key,
4949 u64 offset)
Chris Masonedbd8d42007-12-21 16:27:24 -05004950{
Zheng Yan1a40e232008-09-26 10:09:34 -04004951 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
4952 struct extent_map_tree *em_tree = &BTRFS_I(reloc_inode)->extent_tree;
4953 struct extent_map *em;
Yan Zheng66435582008-10-30 14:19:50 -04004954 u64 start = extent_key->objectid - offset;
4955 u64 end = start + extent_key->offset - 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04004956
4957 em = alloc_extent_map(GFP_NOFS);
4958 BUG_ON(!em || IS_ERR(em));
4959
Yan Zheng66435582008-10-30 14:19:50 -04004960 em->start = start;
Zheng Yan1a40e232008-09-26 10:09:34 -04004961 em->len = extent_key->offset;
Chris Masonc8b97812008-10-29 14:49:59 -04004962 em->block_len = extent_key->offset;
Zheng Yan1a40e232008-09-26 10:09:34 -04004963 em->block_start = extent_key->objectid;
4964 em->bdev = root->fs_info->fs_devices->latest_bdev;
4965 set_bit(EXTENT_FLAG_PINNED, &em->flags);
4966
4967 /* setup extent map to cheat btrfs_readpage */
Yan Zheng66435582008-10-30 14:19:50 -04004968 lock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04004969 while (1) {
4970 int ret;
4971 spin_lock(&em_tree->lock);
4972 ret = add_extent_mapping(em_tree, em);
4973 spin_unlock(&em_tree->lock);
4974 if (ret != -EEXIST) {
4975 free_extent_map(em);
4976 break;
4977 }
Yan Zheng66435582008-10-30 14:19:50 -04004978 btrfs_drop_extent_cache(reloc_inode, start, end, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04004979 }
Yan Zheng66435582008-10-30 14:19:50 -04004980 unlock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04004981
Yan Zheng66435582008-10-30 14:19:50 -04004982 return relocate_inode_pages(reloc_inode, start, extent_key->offset);
Zheng Yan1a40e232008-09-26 10:09:34 -04004983}
4984
4985struct btrfs_ref_path {
4986 u64 extent_start;
4987 u64 nodes[BTRFS_MAX_LEVEL];
4988 u64 root_objectid;
4989 u64 root_generation;
4990 u64 owner_objectid;
Zheng Yan1a40e232008-09-26 10:09:34 -04004991 u32 num_refs;
4992 int lowest_level;
4993 int current_level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04004994 int shared_level;
4995
4996 struct btrfs_key node_keys[BTRFS_MAX_LEVEL];
4997 u64 new_nodes[BTRFS_MAX_LEVEL];
Zheng Yan1a40e232008-09-26 10:09:34 -04004998};
4999
5000struct disk_extent {
Chris Masonc8b97812008-10-29 14:49:59 -04005001 u64 ram_bytes;
Zheng Yan1a40e232008-09-26 10:09:34 -04005002 u64 disk_bytenr;
5003 u64 disk_num_bytes;
5004 u64 offset;
5005 u64 num_bytes;
Chris Masonc8b97812008-10-29 14:49:59 -04005006 u8 compression;
5007 u8 encryption;
5008 u16 other_encoding;
Zheng Yan1a40e232008-09-26 10:09:34 -04005009};
5010
5011static int is_cowonly_root(u64 root_objectid)
5012{
5013 if (root_objectid == BTRFS_ROOT_TREE_OBJECTID ||
5014 root_objectid == BTRFS_EXTENT_TREE_OBJECTID ||
5015 root_objectid == BTRFS_CHUNK_TREE_OBJECTID ||
5016 root_objectid == BTRFS_DEV_TREE_OBJECTID ||
Yan Zheng0403e472008-12-10 20:32:51 -05005017 root_objectid == BTRFS_TREE_LOG_OBJECTID ||
5018 root_objectid == BTRFS_CSUM_TREE_OBJECTID)
Zheng Yan1a40e232008-09-26 10:09:34 -04005019 return 1;
5020 return 0;
5021}
5022
Chris Masond3977122009-01-05 21:25:51 -05005023static noinline int __next_ref_path(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04005024 struct btrfs_root *extent_root,
5025 struct btrfs_ref_path *ref_path,
5026 int first_time)
5027{
5028 struct extent_buffer *leaf;
5029 struct btrfs_path *path;
Chris Mason4313b392008-01-03 09:08:48 -05005030 struct btrfs_extent_ref *ref;
Chris Masonedbd8d42007-12-21 16:27:24 -05005031 struct btrfs_key key;
5032 struct btrfs_key found_key;
Zheng Yan1a40e232008-09-26 10:09:34 -04005033 u64 bytenr;
Chris Masonedbd8d42007-12-21 16:27:24 -05005034 u32 nritems;
Zheng Yan1a40e232008-09-26 10:09:34 -04005035 int level;
5036 int ret = 1;
Chris Masonedbd8d42007-12-21 16:27:24 -05005037
Zheng Yan1a40e232008-09-26 10:09:34 -04005038 path = btrfs_alloc_path();
5039 if (!path)
5040 return -ENOMEM;
5041
Zheng Yan1a40e232008-09-26 10:09:34 -04005042 if (first_time) {
5043 ref_path->lowest_level = -1;
5044 ref_path->current_level = -1;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005045 ref_path->shared_level = -1;
Zheng Yan1a40e232008-09-26 10:09:34 -04005046 goto walk_up;
Chris Masona061fc82008-05-07 11:43:44 -04005047 }
Zheng Yan1a40e232008-09-26 10:09:34 -04005048walk_down:
5049 level = ref_path->current_level - 1;
5050 while (level >= -1) {
5051 u64 parent;
5052 if (level < ref_path->lowest_level)
5053 break;
Chris Masonedbd8d42007-12-21 16:27:24 -05005054
Chris Masond3977122009-01-05 21:25:51 -05005055 if (level >= 0)
Zheng Yan1a40e232008-09-26 10:09:34 -04005056 bytenr = ref_path->nodes[level];
Chris Masond3977122009-01-05 21:25:51 -05005057 else
Zheng Yan1a40e232008-09-26 10:09:34 -04005058 bytenr = ref_path->extent_start;
Zheng Yan1a40e232008-09-26 10:09:34 -04005059 BUG_ON(bytenr == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05005060
Zheng Yan1a40e232008-09-26 10:09:34 -04005061 parent = ref_path->nodes[level + 1];
5062 ref_path->nodes[level + 1] = 0;
5063 ref_path->current_level = level;
5064 BUG_ON(parent == 0);
5065
5066 key.objectid = bytenr;
5067 key.offset = parent + 1;
5068 key.type = BTRFS_EXTENT_REF_KEY;
5069
5070 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05005071 if (ret < 0)
5072 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04005073 BUG_ON(ret == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05005074
Chris Masonedbd8d42007-12-21 16:27:24 -05005075 leaf = path->nodes[0];
5076 nritems = btrfs_header_nritems(leaf);
Zheng Yan1a40e232008-09-26 10:09:34 -04005077 if (path->slots[0] >= nritems) {
Chris Masona061fc82008-05-07 11:43:44 -04005078 ret = btrfs_next_leaf(extent_root, path);
Chris Masona061fc82008-05-07 11:43:44 -04005079 if (ret < 0)
5080 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04005081 if (ret > 0)
5082 goto next;
Chris Masonbf4ef672008-05-08 13:26:18 -04005083 leaf = path->nodes[0];
Chris Masona061fc82008-05-07 11:43:44 -04005084 }
Chris Masonedbd8d42007-12-21 16:27:24 -05005085
5086 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Zheng Yan1a40e232008-09-26 10:09:34 -04005087 if (found_key.objectid == bytenr &&
Yan Zhengf82d02d2008-10-29 14:49:05 -04005088 found_key.type == BTRFS_EXTENT_REF_KEY) {
5089 if (level < ref_path->shared_level)
5090 ref_path->shared_level = level;
Zheng Yan1a40e232008-09-26 10:09:34 -04005091 goto found;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005092 }
Zheng Yan1a40e232008-09-26 10:09:34 -04005093next:
5094 level--;
5095 btrfs_release_path(extent_root, path);
Yan Zhengd899e052008-10-30 14:25:28 -04005096 cond_resched();
Zheng Yan1a40e232008-09-26 10:09:34 -04005097 }
5098 /* reached lowest level */
5099 ret = 1;
5100 goto out;
5101walk_up:
5102 level = ref_path->current_level;
5103 while (level < BTRFS_MAX_LEVEL - 1) {
5104 u64 ref_objectid;
Chris Masond3977122009-01-05 21:25:51 -05005105
5106 if (level >= 0)
Zheng Yan1a40e232008-09-26 10:09:34 -04005107 bytenr = ref_path->nodes[level];
Chris Masond3977122009-01-05 21:25:51 -05005108 else
Zheng Yan1a40e232008-09-26 10:09:34 -04005109 bytenr = ref_path->extent_start;
Chris Masond3977122009-01-05 21:25:51 -05005110
Zheng Yan1a40e232008-09-26 10:09:34 -04005111 BUG_ON(bytenr == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05005112
Zheng Yan1a40e232008-09-26 10:09:34 -04005113 key.objectid = bytenr;
5114 key.offset = 0;
5115 key.type = BTRFS_EXTENT_REF_KEY;
Chris Masonedbd8d42007-12-21 16:27:24 -05005116
Zheng Yan1a40e232008-09-26 10:09:34 -04005117 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
5118 if (ret < 0)
Chris Masonedbd8d42007-12-21 16:27:24 -05005119 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04005120
5121 leaf = path->nodes[0];
5122 nritems = btrfs_header_nritems(leaf);
5123 if (path->slots[0] >= nritems) {
5124 ret = btrfs_next_leaf(extent_root, path);
5125 if (ret < 0)
5126 goto out;
5127 if (ret > 0) {
5128 /* the extent was freed by someone */
5129 if (ref_path->lowest_level == level)
5130 goto out;
5131 btrfs_release_path(extent_root, path);
5132 goto walk_down;
5133 }
5134 leaf = path->nodes[0];
5135 }
5136
5137 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5138 if (found_key.objectid != bytenr ||
5139 found_key.type != BTRFS_EXTENT_REF_KEY) {
5140 /* the extent was freed by someone */
5141 if (ref_path->lowest_level == level) {
5142 ret = 1;
5143 goto out;
5144 }
5145 btrfs_release_path(extent_root, path);
5146 goto walk_down;
5147 }
5148found:
5149 ref = btrfs_item_ptr(leaf, path->slots[0],
5150 struct btrfs_extent_ref);
5151 ref_objectid = btrfs_ref_objectid(leaf, ref);
5152 if (ref_objectid < BTRFS_FIRST_FREE_OBJECTID) {
5153 if (first_time) {
5154 level = (int)ref_objectid;
5155 BUG_ON(level >= BTRFS_MAX_LEVEL);
5156 ref_path->lowest_level = level;
5157 ref_path->current_level = level;
5158 ref_path->nodes[level] = bytenr;
5159 } else {
5160 WARN_ON(ref_objectid != level);
5161 }
5162 } else {
5163 WARN_ON(level != -1);
5164 }
5165 first_time = 0;
5166
5167 if (ref_path->lowest_level == level) {
5168 ref_path->owner_objectid = ref_objectid;
Zheng Yan1a40e232008-09-26 10:09:34 -04005169 ref_path->num_refs = btrfs_ref_num_refs(leaf, ref);
5170 }
5171
5172 /*
5173 * the block is tree root or the block isn't in reference
5174 * counted tree.
5175 */
5176 if (found_key.objectid == found_key.offset ||
5177 is_cowonly_root(btrfs_ref_root(leaf, ref))) {
5178 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
5179 ref_path->root_generation =
5180 btrfs_ref_generation(leaf, ref);
5181 if (level < 0) {
5182 /* special reference from the tree log */
5183 ref_path->nodes[0] = found_key.offset;
5184 ref_path->current_level = 0;
5185 }
5186 ret = 0;
5187 goto out;
5188 }
5189
5190 level++;
5191 BUG_ON(ref_path->nodes[level] != 0);
5192 ref_path->nodes[level] = found_key.offset;
5193 ref_path->current_level = level;
5194
5195 /*
5196 * the reference was created in the running transaction,
5197 * no need to continue walking up.
5198 */
5199 if (btrfs_ref_generation(leaf, ref) == trans->transid) {
5200 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
5201 ref_path->root_generation =
5202 btrfs_ref_generation(leaf, ref);
5203 ret = 0;
5204 goto out;
5205 }
5206
5207 btrfs_release_path(extent_root, path);
Yan Zhengd899e052008-10-30 14:25:28 -04005208 cond_resched();
Zheng Yan1a40e232008-09-26 10:09:34 -04005209 }
5210 /* reached max tree level, but no tree root found. */
5211 BUG();
5212out:
Zheng Yan1a40e232008-09-26 10:09:34 -04005213 btrfs_free_path(path);
5214 return ret;
5215}
5216
5217static int btrfs_first_ref_path(struct btrfs_trans_handle *trans,
5218 struct btrfs_root *extent_root,
5219 struct btrfs_ref_path *ref_path,
5220 u64 extent_start)
5221{
5222 memset(ref_path, 0, sizeof(*ref_path));
5223 ref_path->extent_start = extent_start;
5224
5225 return __next_ref_path(trans, extent_root, ref_path, 1);
5226}
5227
5228static int btrfs_next_ref_path(struct btrfs_trans_handle *trans,
5229 struct btrfs_root *extent_root,
5230 struct btrfs_ref_path *ref_path)
5231{
5232 return __next_ref_path(trans, extent_root, ref_path, 0);
5233}
5234
Chris Masond3977122009-01-05 21:25:51 -05005235static noinline int get_new_locations(struct inode *reloc_inode,
Zheng Yan1a40e232008-09-26 10:09:34 -04005236 struct btrfs_key *extent_key,
5237 u64 offset, int no_fragment,
5238 struct disk_extent **extents,
5239 int *nr_extents)
5240{
5241 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
5242 struct btrfs_path *path;
5243 struct btrfs_file_extent_item *fi;
5244 struct extent_buffer *leaf;
5245 struct disk_extent *exts = *extents;
5246 struct btrfs_key found_key;
5247 u64 cur_pos;
5248 u64 last_byte;
5249 u32 nritems;
5250 int nr = 0;
5251 int max = *nr_extents;
5252 int ret;
5253
5254 WARN_ON(!no_fragment && *extents);
5255 if (!exts) {
5256 max = 1;
5257 exts = kmalloc(sizeof(*exts) * max, GFP_NOFS);
5258 if (!exts)
5259 return -ENOMEM;
5260 }
5261
5262 path = btrfs_alloc_path();
5263 BUG_ON(!path);
5264
5265 cur_pos = extent_key->objectid - offset;
5266 last_byte = extent_key->objectid + extent_key->offset;
5267 ret = btrfs_lookup_file_extent(NULL, root, path, reloc_inode->i_ino,
5268 cur_pos, 0);
5269 if (ret < 0)
5270 goto out;
5271 if (ret > 0) {
5272 ret = -ENOENT;
5273 goto out;
5274 }
5275
5276 while (1) {
5277 leaf = path->nodes[0];
5278 nritems = btrfs_header_nritems(leaf);
5279 if (path->slots[0] >= nritems) {
5280 ret = btrfs_next_leaf(root, path);
5281 if (ret < 0)
5282 goto out;
5283 if (ret > 0)
5284 break;
5285 leaf = path->nodes[0];
5286 }
5287
5288 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5289 if (found_key.offset != cur_pos ||
5290 found_key.type != BTRFS_EXTENT_DATA_KEY ||
5291 found_key.objectid != reloc_inode->i_ino)
5292 break;
5293
5294 fi = btrfs_item_ptr(leaf, path->slots[0],
5295 struct btrfs_file_extent_item);
5296 if (btrfs_file_extent_type(leaf, fi) !=
5297 BTRFS_FILE_EXTENT_REG ||
5298 btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
5299 break;
5300
5301 if (nr == max) {
5302 struct disk_extent *old = exts;
5303 max *= 2;
5304 exts = kzalloc(sizeof(*exts) * max, GFP_NOFS);
5305 memcpy(exts, old, sizeof(*exts) * nr);
5306 if (old != *extents)
5307 kfree(old);
5308 }
5309
5310 exts[nr].disk_bytenr =
5311 btrfs_file_extent_disk_bytenr(leaf, fi);
5312 exts[nr].disk_num_bytes =
5313 btrfs_file_extent_disk_num_bytes(leaf, fi);
5314 exts[nr].offset = btrfs_file_extent_offset(leaf, fi);
5315 exts[nr].num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
Chris Masonc8b97812008-10-29 14:49:59 -04005316 exts[nr].ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
5317 exts[nr].compression = btrfs_file_extent_compression(leaf, fi);
5318 exts[nr].encryption = btrfs_file_extent_encryption(leaf, fi);
5319 exts[nr].other_encoding = btrfs_file_extent_other_encoding(leaf,
5320 fi);
Yan Zhengd899e052008-10-30 14:25:28 -04005321 BUG_ON(exts[nr].offset > 0);
5322 BUG_ON(exts[nr].compression || exts[nr].encryption);
5323 BUG_ON(exts[nr].num_bytes != exts[nr].disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04005324
5325 cur_pos += exts[nr].num_bytes;
5326 nr++;
5327
5328 if (cur_pos + offset >= last_byte)
5329 break;
5330
5331 if (no_fragment) {
5332 ret = 1;
5333 goto out;
5334 }
5335 path->slots[0]++;
5336 }
5337
Yan Zheng1f80e4d2008-12-19 10:59:04 -05005338 BUG_ON(cur_pos + offset > last_byte);
Zheng Yan1a40e232008-09-26 10:09:34 -04005339 if (cur_pos + offset < last_byte) {
5340 ret = -ENOENT;
5341 goto out;
Chris Masonedbd8d42007-12-21 16:27:24 -05005342 }
5343 ret = 0;
5344out:
Zheng Yan1a40e232008-09-26 10:09:34 -04005345 btrfs_free_path(path);
5346 if (ret) {
5347 if (exts != *extents)
5348 kfree(exts);
5349 } else {
5350 *extents = exts;
5351 *nr_extents = nr;
5352 }
5353 return ret;
5354}
5355
Chris Masond3977122009-01-05 21:25:51 -05005356static noinline int replace_one_extent(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04005357 struct btrfs_root *root,
5358 struct btrfs_path *path,
5359 struct btrfs_key *extent_key,
5360 struct btrfs_key *leaf_key,
5361 struct btrfs_ref_path *ref_path,
5362 struct disk_extent *new_extents,
5363 int nr_extents)
5364{
5365 struct extent_buffer *leaf;
5366 struct btrfs_file_extent_item *fi;
5367 struct inode *inode = NULL;
5368 struct btrfs_key key;
5369 u64 lock_start = 0;
5370 u64 lock_end = 0;
5371 u64 num_bytes;
5372 u64 ext_offset;
Yan Zheng86288a12009-01-21 10:49:16 -05005373 u64 search_end = (u64)-1;
Zheng Yan1a40e232008-09-26 10:09:34 -04005374 u32 nritems;
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04005375 int nr_scaned = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04005376 int extent_locked = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04005377 int extent_type;
Zheng Yan1a40e232008-09-26 10:09:34 -04005378 int ret;
5379
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04005380 memcpy(&key, leaf_key, sizeof(key));
Zheng Yan1a40e232008-09-26 10:09:34 -04005381 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04005382 if (key.objectid < ref_path->owner_objectid ||
5383 (key.objectid == ref_path->owner_objectid &&
5384 key.type < BTRFS_EXTENT_DATA_KEY)) {
5385 key.objectid = ref_path->owner_objectid;
5386 key.type = BTRFS_EXTENT_DATA_KEY;
5387 key.offset = 0;
5388 }
Zheng Yan1a40e232008-09-26 10:09:34 -04005389 }
5390
5391 while (1) {
5392 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
5393 if (ret < 0)
5394 goto out;
5395
5396 leaf = path->nodes[0];
5397 nritems = btrfs_header_nritems(leaf);
5398next:
5399 if (extent_locked && ret > 0) {
5400 /*
5401 * the file extent item was modified by someone
5402 * before the extent got locked.
5403 */
Zheng Yan1a40e232008-09-26 10:09:34 -04005404 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
5405 lock_end, GFP_NOFS);
5406 extent_locked = 0;
5407 }
5408
5409 if (path->slots[0] >= nritems) {
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04005410 if (++nr_scaned > 2)
Zheng Yan1a40e232008-09-26 10:09:34 -04005411 break;
5412
5413 BUG_ON(extent_locked);
5414 ret = btrfs_next_leaf(root, path);
5415 if (ret < 0)
5416 goto out;
5417 if (ret > 0)
5418 break;
5419 leaf = path->nodes[0];
5420 nritems = btrfs_header_nritems(leaf);
5421 }
5422
5423 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
5424
5425 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
5426 if ((key.objectid > ref_path->owner_objectid) ||
5427 (key.objectid == ref_path->owner_objectid &&
5428 key.type > BTRFS_EXTENT_DATA_KEY) ||
Yan Zheng86288a12009-01-21 10:49:16 -05005429 key.offset >= search_end)
Zheng Yan1a40e232008-09-26 10:09:34 -04005430 break;
5431 }
5432
5433 if (inode && key.objectid != inode->i_ino) {
5434 BUG_ON(extent_locked);
5435 btrfs_release_path(root, path);
5436 mutex_unlock(&inode->i_mutex);
5437 iput(inode);
5438 inode = NULL;
5439 continue;
5440 }
5441
5442 if (key.type != BTRFS_EXTENT_DATA_KEY) {
5443 path->slots[0]++;
5444 ret = 1;
5445 goto next;
5446 }
5447 fi = btrfs_item_ptr(leaf, path->slots[0],
5448 struct btrfs_file_extent_item);
Yan Zhengd899e052008-10-30 14:25:28 -04005449 extent_type = btrfs_file_extent_type(leaf, fi);
5450 if ((extent_type != BTRFS_FILE_EXTENT_REG &&
5451 extent_type != BTRFS_FILE_EXTENT_PREALLOC) ||
Zheng Yan1a40e232008-09-26 10:09:34 -04005452 (btrfs_file_extent_disk_bytenr(leaf, fi) !=
5453 extent_key->objectid)) {
5454 path->slots[0]++;
5455 ret = 1;
5456 goto next;
5457 }
5458
5459 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
5460 ext_offset = btrfs_file_extent_offset(leaf, fi);
5461
Yan Zheng86288a12009-01-21 10:49:16 -05005462 if (search_end == (u64)-1) {
5463 search_end = key.offset - ext_offset +
5464 btrfs_file_extent_ram_bytes(leaf, fi);
5465 }
Zheng Yan1a40e232008-09-26 10:09:34 -04005466
5467 if (!extent_locked) {
5468 lock_start = key.offset;
5469 lock_end = lock_start + num_bytes - 1;
5470 } else {
Yan Zheng66435582008-10-30 14:19:50 -04005471 if (lock_start > key.offset ||
5472 lock_end + 1 < key.offset + num_bytes) {
5473 unlock_extent(&BTRFS_I(inode)->io_tree,
5474 lock_start, lock_end, GFP_NOFS);
5475 extent_locked = 0;
5476 }
Zheng Yan1a40e232008-09-26 10:09:34 -04005477 }
5478
5479 if (!inode) {
5480 btrfs_release_path(root, path);
5481
5482 inode = btrfs_iget_locked(root->fs_info->sb,
5483 key.objectid, root);
5484 if (inode->i_state & I_NEW) {
5485 BTRFS_I(inode)->root = root;
5486 BTRFS_I(inode)->location.objectid =
5487 key.objectid;
5488 BTRFS_I(inode)->location.type =
5489 BTRFS_INODE_ITEM_KEY;
5490 BTRFS_I(inode)->location.offset = 0;
5491 btrfs_read_locked_inode(inode);
5492 unlock_new_inode(inode);
5493 }
5494 /*
5495 * some code call btrfs_commit_transaction while
5496 * holding the i_mutex, so we can't use mutex_lock
5497 * here.
5498 */
5499 if (is_bad_inode(inode) ||
5500 !mutex_trylock(&inode->i_mutex)) {
5501 iput(inode);
5502 inode = NULL;
5503 key.offset = (u64)-1;
5504 goto skip;
5505 }
5506 }
5507
5508 if (!extent_locked) {
5509 struct btrfs_ordered_extent *ordered;
5510
5511 btrfs_release_path(root, path);
5512
5513 lock_extent(&BTRFS_I(inode)->io_tree, lock_start,
5514 lock_end, GFP_NOFS);
5515 ordered = btrfs_lookup_first_ordered_extent(inode,
5516 lock_end);
5517 if (ordered &&
5518 ordered->file_offset <= lock_end &&
5519 ordered->file_offset + ordered->len > lock_start) {
5520 unlock_extent(&BTRFS_I(inode)->io_tree,
5521 lock_start, lock_end, GFP_NOFS);
5522 btrfs_start_ordered_extent(inode, ordered, 1);
5523 btrfs_put_ordered_extent(ordered);
5524 key.offset += num_bytes;
5525 goto skip;
5526 }
5527 if (ordered)
5528 btrfs_put_ordered_extent(ordered);
5529
Zheng Yan1a40e232008-09-26 10:09:34 -04005530 extent_locked = 1;
5531 continue;
5532 }
5533
5534 if (nr_extents == 1) {
5535 /* update extent pointer in place */
Zheng Yan1a40e232008-09-26 10:09:34 -04005536 btrfs_set_file_extent_disk_bytenr(leaf, fi,
5537 new_extents[0].disk_bytenr);
5538 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
5539 new_extents[0].disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04005540 btrfs_mark_buffer_dirty(leaf);
5541
5542 btrfs_drop_extent_cache(inode, key.offset,
5543 key.offset + num_bytes - 1, 0);
5544
5545 ret = btrfs_inc_extent_ref(trans, root,
5546 new_extents[0].disk_bytenr,
5547 new_extents[0].disk_num_bytes,
5548 leaf->start,
5549 root->root_key.objectid,
5550 trans->transid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04005551 key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04005552 BUG_ON(ret);
5553
5554 ret = btrfs_free_extent(trans, root,
5555 extent_key->objectid,
5556 extent_key->offset,
5557 leaf->start,
5558 btrfs_header_owner(leaf),
5559 btrfs_header_generation(leaf),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04005560 key.objectid, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04005561 BUG_ON(ret);
5562
5563 btrfs_release_path(root, path);
5564 key.offset += num_bytes;
5565 } else {
Yan Zhengd899e052008-10-30 14:25:28 -04005566 BUG_ON(1);
5567#if 0
Zheng Yan1a40e232008-09-26 10:09:34 -04005568 u64 alloc_hint;
5569 u64 extent_len;
5570 int i;
5571 /*
5572 * drop old extent pointer at first, then insert the
5573 * new pointers one bye one
5574 */
5575 btrfs_release_path(root, path);
5576 ret = btrfs_drop_extents(trans, root, inode, key.offset,
5577 key.offset + num_bytes,
5578 key.offset, &alloc_hint);
5579 BUG_ON(ret);
5580
5581 for (i = 0; i < nr_extents; i++) {
5582 if (ext_offset >= new_extents[i].num_bytes) {
5583 ext_offset -= new_extents[i].num_bytes;
5584 continue;
5585 }
5586 extent_len = min(new_extents[i].num_bytes -
5587 ext_offset, num_bytes);
5588
5589 ret = btrfs_insert_empty_item(trans, root,
5590 path, &key,
5591 sizeof(*fi));
5592 BUG_ON(ret);
5593
5594 leaf = path->nodes[0];
5595 fi = btrfs_item_ptr(leaf, path->slots[0],
5596 struct btrfs_file_extent_item);
5597 btrfs_set_file_extent_generation(leaf, fi,
5598 trans->transid);
5599 btrfs_set_file_extent_type(leaf, fi,
5600 BTRFS_FILE_EXTENT_REG);
5601 btrfs_set_file_extent_disk_bytenr(leaf, fi,
5602 new_extents[i].disk_bytenr);
5603 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
5604 new_extents[i].disk_num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -04005605 btrfs_set_file_extent_ram_bytes(leaf, fi,
5606 new_extents[i].ram_bytes);
5607
5608 btrfs_set_file_extent_compression(leaf, fi,
5609 new_extents[i].compression);
5610 btrfs_set_file_extent_encryption(leaf, fi,
5611 new_extents[i].encryption);
5612 btrfs_set_file_extent_other_encoding(leaf, fi,
5613 new_extents[i].other_encoding);
5614
Zheng Yan1a40e232008-09-26 10:09:34 -04005615 btrfs_set_file_extent_num_bytes(leaf, fi,
5616 extent_len);
5617 ext_offset += new_extents[i].offset;
5618 btrfs_set_file_extent_offset(leaf, fi,
5619 ext_offset);
5620 btrfs_mark_buffer_dirty(leaf);
5621
5622 btrfs_drop_extent_cache(inode, key.offset,
5623 key.offset + extent_len - 1, 0);
5624
5625 ret = btrfs_inc_extent_ref(trans, root,
5626 new_extents[i].disk_bytenr,
5627 new_extents[i].disk_num_bytes,
5628 leaf->start,
5629 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04005630 trans->transid, key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04005631 BUG_ON(ret);
5632 btrfs_release_path(root, path);
5633
Yan Zhenga76a3cd2008-10-09 11:46:29 -04005634 inode_add_bytes(inode, extent_len);
Zheng Yan1a40e232008-09-26 10:09:34 -04005635
5636 ext_offset = 0;
5637 num_bytes -= extent_len;
5638 key.offset += extent_len;
5639
5640 if (num_bytes == 0)
5641 break;
5642 }
5643 BUG_ON(i >= nr_extents);
Yan Zhengd899e052008-10-30 14:25:28 -04005644#endif
Zheng Yan1a40e232008-09-26 10:09:34 -04005645 }
5646
5647 if (extent_locked) {
Zheng Yan1a40e232008-09-26 10:09:34 -04005648 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
5649 lock_end, GFP_NOFS);
5650 extent_locked = 0;
5651 }
5652skip:
5653 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS &&
Yan Zheng86288a12009-01-21 10:49:16 -05005654 key.offset >= search_end)
Zheng Yan1a40e232008-09-26 10:09:34 -04005655 break;
5656
5657 cond_resched();
5658 }
5659 ret = 0;
5660out:
5661 btrfs_release_path(root, path);
5662 if (inode) {
5663 mutex_unlock(&inode->i_mutex);
5664 if (extent_locked) {
Zheng Yan1a40e232008-09-26 10:09:34 -04005665 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
5666 lock_end, GFP_NOFS);
5667 }
5668 iput(inode);
5669 }
5670 return ret;
5671}
5672
Zheng Yan1a40e232008-09-26 10:09:34 -04005673int btrfs_reloc_tree_cache_ref(struct btrfs_trans_handle *trans,
5674 struct btrfs_root *root,
5675 struct extent_buffer *buf, u64 orig_start)
5676{
5677 int level;
5678 int ret;
5679
5680 BUG_ON(btrfs_header_generation(buf) != trans->transid);
5681 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
5682
5683 level = btrfs_header_level(buf);
5684 if (level == 0) {
5685 struct btrfs_leaf_ref *ref;
5686 struct btrfs_leaf_ref *orig_ref;
5687
5688 orig_ref = btrfs_lookup_leaf_ref(root, orig_start);
5689 if (!orig_ref)
5690 return -ENOENT;
5691
5692 ref = btrfs_alloc_leaf_ref(root, orig_ref->nritems);
5693 if (!ref) {
5694 btrfs_free_leaf_ref(root, orig_ref);
5695 return -ENOMEM;
5696 }
5697
5698 ref->nritems = orig_ref->nritems;
5699 memcpy(ref->extents, orig_ref->extents,
5700 sizeof(ref->extents[0]) * ref->nritems);
5701
5702 btrfs_free_leaf_ref(root, orig_ref);
5703
5704 ref->root_gen = trans->transid;
5705 ref->bytenr = buf->start;
5706 ref->owner = btrfs_header_owner(buf);
5707 ref->generation = btrfs_header_generation(buf);
Chris Masonbd56b302009-02-04 09:27:02 -05005708
Zheng Yan1a40e232008-09-26 10:09:34 -04005709 ret = btrfs_add_leaf_ref(root, ref, 0);
5710 WARN_ON(ret);
5711 btrfs_free_leaf_ref(root, ref);
5712 }
5713 return 0;
5714}
5715
Chris Masond3977122009-01-05 21:25:51 -05005716static noinline int invalidate_extent_cache(struct btrfs_root *root,
Zheng Yan1a40e232008-09-26 10:09:34 -04005717 struct extent_buffer *leaf,
5718 struct btrfs_block_group_cache *group,
5719 struct btrfs_root *target_root)
5720{
5721 struct btrfs_key key;
5722 struct inode *inode = NULL;
5723 struct btrfs_file_extent_item *fi;
5724 u64 num_bytes;
5725 u64 skip_objectid = 0;
5726 u32 nritems;
5727 u32 i;
5728
5729 nritems = btrfs_header_nritems(leaf);
5730 for (i = 0; i < nritems; i++) {
5731 btrfs_item_key_to_cpu(leaf, &key, i);
5732 if (key.objectid == skip_objectid ||
5733 key.type != BTRFS_EXTENT_DATA_KEY)
5734 continue;
5735 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
5736 if (btrfs_file_extent_type(leaf, fi) ==
5737 BTRFS_FILE_EXTENT_INLINE)
5738 continue;
5739 if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
5740 continue;
5741 if (!inode || inode->i_ino != key.objectid) {
5742 iput(inode);
5743 inode = btrfs_ilookup(target_root->fs_info->sb,
5744 key.objectid, target_root, 1);
5745 }
5746 if (!inode) {
5747 skip_objectid = key.objectid;
5748 continue;
5749 }
5750 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
5751
5752 lock_extent(&BTRFS_I(inode)->io_tree, key.offset,
5753 key.offset + num_bytes - 1, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04005754 btrfs_drop_extent_cache(inode, key.offset,
5755 key.offset + num_bytes - 1, 1);
Zheng Yan1a40e232008-09-26 10:09:34 -04005756 unlock_extent(&BTRFS_I(inode)->io_tree, key.offset,
5757 key.offset + num_bytes - 1, GFP_NOFS);
5758 cond_resched();
5759 }
5760 iput(inode);
5761 return 0;
5762}
5763
Chris Masond3977122009-01-05 21:25:51 -05005764static noinline int replace_extents_in_leaf(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04005765 struct btrfs_root *root,
5766 struct extent_buffer *leaf,
5767 struct btrfs_block_group_cache *group,
5768 struct inode *reloc_inode)
5769{
5770 struct btrfs_key key;
5771 struct btrfs_key extent_key;
5772 struct btrfs_file_extent_item *fi;
5773 struct btrfs_leaf_ref *ref;
5774 struct disk_extent *new_extent;
5775 u64 bytenr;
5776 u64 num_bytes;
5777 u32 nritems;
5778 u32 i;
5779 int ext_index;
5780 int nr_extent;
5781 int ret;
5782
5783 new_extent = kmalloc(sizeof(*new_extent), GFP_NOFS);
5784 BUG_ON(!new_extent);
5785
5786 ref = btrfs_lookup_leaf_ref(root, leaf->start);
5787 BUG_ON(!ref);
5788
5789 ext_index = -1;
5790 nritems = btrfs_header_nritems(leaf);
5791 for (i = 0; i < nritems; i++) {
5792 btrfs_item_key_to_cpu(leaf, &key, i);
5793 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
5794 continue;
5795 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
5796 if (btrfs_file_extent_type(leaf, fi) ==
5797 BTRFS_FILE_EXTENT_INLINE)
5798 continue;
5799 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
5800 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
5801 if (bytenr == 0)
5802 continue;
5803
5804 ext_index++;
5805 if (bytenr >= group->key.objectid + group->key.offset ||
5806 bytenr + num_bytes <= group->key.objectid)
5807 continue;
5808
5809 extent_key.objectid = bytenr;
5810 extent_key.offset = num_bytes;
5811 extent_key.type = BTRFS_EXTENT_ITEM_KEY;
5812 nr_extent = 1;
5813 ret = get_new_locations(reloc_inode, &extent_key,
5814 group->key.objectid, 1,
5815 &new_extent, &nr_extent);
5816 if (ret > 0)
5817 continue;
5818 BUG_ON(ret < 0);
5819
5820 BUG_ON(ref->extents[ext_index].bytenr != bytenr);
5821 BUG_ON(ref->extents[ext_index].num_bytes != num_bytes);
5822 ref->extents[ext_index].bytenr = new_extent->disk_bytenr;
5823 ref->extents[ext_index].num_bytes = new_extent->disk_num_bytes;
5824
Zheng Yan1a40e232008-09-26 10:09:34 -04005825 btrfs_set_file_extent_disk_bytenr(leaf, fi,
5826 new_extent->disk_bytenr);
5827 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
5828 new_extent->disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04005829 btrfs_mark_buffer_dirty(leaf);
5830
5831 ret = btrfs_inc_extent_ref(trans, root,
5832 new_extent->disk_bytenr,
5833 new_extent->disk_num_bytes,
5834 leaf->start,
5835 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04005836 trans->transid, key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04005837 BUG_ON(ret);
Chris Mason56bec292009-03-13 10:10:06 -04005838
Zheng Yan1a40e232008-09-26 10:09:34 -04005839 ret = btrfs_free_extent(trans, root,
5840 bytenr, num_bytes, leaf->start,
5841 btrfs_header_owner(leaf),
5842 btrfs_header_generation(leaf),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04005843 key.objectid, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04005844 BUG_ON(ret);
5845 cond_resched();
5846 }
5847 kfree(new_extent);
5848 BUG_ON(ext_index + 1 != ref->nritems);
5849 btrfs_free_leaf_ref(root, ref);
5850 return 0;
5851}
5852
Yan Zhengf82d02d2008-10-29 14:49:05 -04005853int btrfs_free_reloc_root(struct btrfs_trans_handle *trans,
5854 struct btrfs_root *root)
Zheng Yan1a40e232008-09-26 10:09:34 -04005855{
5856 struct btrfs_root *reloc_root;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005857 int ret;
Zheng Yan1a40e232008-09-26 10:09:34 -04005858
5859 if (root->reloc_root) {
5860 reloc_root = root->reloc_root;
5861 root->reloc_root = NULL;
5862 list_add(&reloc_root->dead_list,
5863 &root->fs_info->dead_reloc_roots);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005864
5865 btrfs_set_root_bytenr(&reloc_root->root_item,
5866 reloc_root->node->start);
5867 btrfs_set_root_level(&root->root_item,
5868 btrfs_header_level(reloc_root->node));
5869 memset(&reloc_root->root_item.drop_progress, 0,
5870 sizeof(struct btrfs_disk_key));
5871 reloc_root->root_item.drop_level = 0;
5872
5873 ret = btrfs_update_root(trans, root->fs_info->tree_root,
5874 &reloc_root->root_key,
5875 &reloc_root->root_item);
5876 BUG_ON(ret);
Zheng Yan1a40e232008-09-26 10:09:34 -04005877 }
5878 return 0;
5879}
5880
5881int btrfs_drop_dead_reloc_roots(struct btrfs_root *root)
5882{
5883 struct btrfs_trans_handle *trans;
5884 struct btrfs_root *reloc_root;
5885 struct btrfs_root *prev_root = NULL;
5886 struct list_head dead_roots;
5887 int ret;
5888 unsigned long nr;
5889
5890 INIT_LIST_HEAD(&dead_roots);
5891 list_splice_init(&root->fs_info->dead_reloc_roots, &dead_roots);
5892
5893 while (!list_empty(&dead_roots)) {
5894 reloc_root = list_entry(dead_roots.prev,
5895 struct btrfs_root, dead_list);
5896 list_del_init(&reloc_root->dead_list);
5897
5898 BUG_ON(reloc_root->commit_root != NULL);
5899 while (1) {
5900 trans = btrfs_join_transaction(root, 1);
5901 BUG_ON(!trans);
5902
5903 mutex_lock(&root->fs_info->drop_mutex);
5904 ret = btrfs_drop_snapshot(trans, reloc_root);
5905 if (ret != -EAGAIN)
5906 break;
5907 mutex_unlock(&root->fs_info->drop_mutex);
5908
5909 nr = trans->blocks_used;
5910 ret = btrfs_end_transaction(trans, root);
5911 BUG_ON(ret);
5912 btrfs_btree_balance_dirty(root, nr);
5913 }
5914
5915 free_extent_buffer(reloc_root->node);
5916
5917 ret = btrfs_del_root(trans, root->fs_info->tree_root,
5918 &reloc_root->root_key);
5919 BUG_ON(ret);
5920 mutex_unlock(&root->fs_info->drop_mutex);
5921
5922 nr = trans->blocks_used;
5923 ret = btrfs_end_transaction(trans, root);
5924 BUG_ON(ret);
5925 btrfs_btree_balance_dirty(root, nr);
5926
5927 kfree(prev_root);
5928 prev_root = reloc_root;
5929 }
5930 if (prev_root) {
5931 btrfs_remove_leaf_refs(prev_root, (u64)-1, 0);
5932 kfree(prev_root);
5933 }
5934 return 0;
5935}
5936
5937int btrfs_add_dead_reloc_root(struct btrfs_root *root)
5938{
5939 list_add(&root->dead_list, &root->fs_info->dead_reloc_roots);
5940 return 0;
5941}
5942
5943int btrfs_cleanup_reloc_trees(struct btrfs_root *root)
5944{
5945 struct btrfs_root *reloc_root;
5946 struct btrfs_trans_handle *trans;
5947 struct btrfs_key location;
5948 int found;
5949 int ret;
5950
5951 mutex_lock(&root->fs_info->tree_reloc_mutex);
5952 ret = btrfs_find_dead_roots(root, BTRFS_TREE_RELOC_OBJECTID, NULL);
5953 BUG_ON(ret);
5954 found = !list_empty(&root->fs_info->dead_reloc_roots);
5955 mutex_unlock(&root->fs_info->tree_reloc_mutex);
5956
5957 if (found) {
5958 trans = btrfs_start_transaction(root, 1);
5959 BUG_ON(!trans);
5960 ret = btrfs_commit_transaction(trans, root);
5961 BUG_ON(ret);
5962 }
5963
5964 location.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
5965 location.offset = (u64)-1;
5966 location.type = BTRFS_ROOT_ITEM_KEY;
5967
5968 reloc_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
5969 BUG_ON(!reloc_root);
5970 btrfs_orphan_cleanup(reloc_root);
5971 return 0;
5972}
5973
Chris Masond3977122009-01-05 21:25:51 -05005974static noinline int init_reloc_tree(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04005975 struct btrfs_root *root)
5976{
5977 struct btrfs_root *reloc_root;
5978 struct extent_buffer *eb;
5979 struct btrfs_root_item *root_item;
5980 struct btrfs_key root_key;
5981 int ret;
5982
5983 BUG_ON(!root->ref_cows);
5984 if (root->reloc_root)
5985 return 0;
5986
5987 root_item = kmalloc(sizeof(*root_item), GFP_NOFS);
5988 BUG_ON(!root_item);
5989
5990 ret = btrfs_copy_root(trans, root, root->commit_root,
5991 &eb, BTRFS_TREE_RELOC_OBJECTID);
5992 BUG_ON(ret);
5993
5994 root_key.objectid = BTRFS_TREE_RELOC_OBJECTID;
5995 root_key.offset = root->root_key.objectid;
5996 root_key.type = BTRFS_ROOT_ITEM_KEY;
5997
5998 memcpy(root_item, &root->root_item, sizeof(root_item));
5999 btrfs_set_root_refs(root_item, 0);
6000 btrfs_set_root_bytenr(root_item, eb->start);
6001 btrfs_set_root_level(root_item, btrfs_header_level(eb));
Yan Zheng84234f32008-10-29 14:49:05 -04006002 btrfs_set_root_generation(root_item, trans->transid);
Zheng Yan1a40e232008-09-26 10:09:34 -04006003
6004 btrfs_tree_unlock(eb);
6005 free_extent_buffer(eb);
6006
6007 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
6008 &root_key, root_item);
6009 BUG_ON(ret);
6010 kfree(root_item);
6011
6012 reloc_root = btrfs_read_fs_root_no_radix(root->fs_info->tree_root,
6013 &root_key);
6014 BUG_ON(!reloc_root);
6015 reloc_root->last_trans = trans->transid;
6016 reloc_root->commit_root = NULL;
6017 reloc_root->ref_tree = &root->fs_info->reloc_ref_tree;
6018
6019 root->reloc_root = reloc_root;
6020 return 0;
6021}
6022
6023/*
6024 * Core function of space balance.
6025 *
6026 * The idea is using reloc trees to relocate tree blocks in reference
Yan Zhengf82d02d2008-10-29 14:49:05 -04006027 * counted roots. There is one reloc tree for each subvol, and all
6028 * reloc trees share same root key objectid. Reloc trees are snapshots
6029 * of the latest committed roots of subvols (root->commit_root).
6030 *
6031 * To relocate a tree block referenced by a subvol, there are two steps.
6032 * COW the block through subvol's reloc tree, then update block pointer
6033 * in the subvol to point to the new block. Since all reloc trees share
6034 * same root key objectid, doing special handing for tree blocks owned
6035 * by them is easy. Once a tree block has been COWed in one reloc tree,
6036 * we can use the resulting new block directly when the same block is
6037 * required to COW again through other reloc trees. By this way, relocated
6038 * tree blocks are shared between reloc trees, so they are also shared
6039 * between subvols.
Zheng Yan1a40e232008-09-26 10:09:34 -04006040 */
Chris Masond3977122009-01-05 21:25:51 -05006041static noinline int relocate_one_path(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04006042 struct btrfs_root *root,
6043 struct btrfs_path *path,
6044 struct btrfs_key *first_key,
6045 struct btrfs_ref_path *ref_path,
6046 struct btrfs_block_group_cache *group,
6047 struct inode *reloc_inode)
6048{
6049 struct btrfs_root *reloc_root;
6050 struct extent_buffer *eb = NULL;
6051 struct btrfs_key *keys;
6052 u64 *nodes;
6053 int level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006054 int shared_level;
Zheng Yan1a40e232008-09-26 10:09:34 -04006055 int lowest_level = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04006056 int ret;
6057
6058 if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
6059 lowest_level = ref_path->owner_objectid;
6060
Yan Zhengf82d02d2008-10-29 14:49:05 -04006061 if (!root->ref_cows) {
Zheng Yan1a40e232008-09-26 10:09:34 -04006062 path->lowest_level = lowest_level;
6063 ret = btrfs_search_slot(trans, root, first_key, path, 0, 1);
6064 BUG_ON(ret < 0);
6065 path->lowest_level = 0;
6066 btrfs_release_path(root, path);
6067 return 0;
6068 }
6069
Zheng Yan1a40e232008-09-26 10:09:34 -04006070 mutex_lock(&root->fs_info->tree_reloc_mutex);
6071 ret = init_reloc_tree(trans, root);
6072 BUG_ON(ret);
6073 reloc_root = root->reloc_root;
6074
Yan Zhengf82d02d2008-10-29 14:49:05 -04006075 shared_level = ref_path->shared_level;
6076 ref_path->shared_level = BTRFS_MAX_LEVEL - 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04006077
Yan Zhengf82d02d2008-10-29 14:49:05 -04006078 keys = ref_path->node_keys;
6079 nodes = ref_path->new_nodes;
6080 memset(&keys[shared_level + 1], 0,
6081 sizeof(*keys) * (BTRFS_MAX_LEVEL - shared_level - 1));
6082 memset(&nodes[shared_level + 1], 0,
6083 sizeof(*nodes) * (BTRFS_MAX_LEVEL - shared_level - 1));
Zheng Yan1a40e232008-09-26 10:09:34 -04006084
Yan Zhengf82d02d2008-10-29 14:49:05 -04006085 if (nodes[lowest_level] == 0) {
6086 path->lowest_level = lowest_level;
6087 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
6088 0, 1);
6089 BUG_ON(ret);
6090 for (level = lowest_level; level < BTRFS_MAX_LEVEL; level++) {
6091 eb = path->nodes[level];
6092 if (!eb || eb == reloc_root->node)
6093 break;
6094 nodes[level] = eb->start;
6095 if (level == 0)
6096 btrfs_item_key_to_cpu(eb, &keys[level], 0);
6097 else
6098 btrfs_node_key_to_cpu(eb, &keys[level], 0);
6099 }
Yan Zheng2b820322008-11-17 21:11:30 -05006100 if (nodes[0] &&
6101 ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04006102 eb = path->nodes[0];
6103 ret = replace_extents_in_leaf(trans, reloc_root, eb,
6104 group, reloc_inode);
6105 BUG_ON(ret);
6106 }
6107 btrfs_release_path(reloc_root, path);
6108 } else {
Zheng Yan1a40e232008-09-26 10:09:34 -04006109 ret = btrfs_merge_path(trans, reloc_root, keys, nodes,
Yan Zhengf82d02d2008-10-29 14:49:05 -04006110 lowest_level);
Zheng Yan1a40e232008-09-26 10:09:34 -04006111 BUG_ON(ret);
6112 }
6113
Zheng Yan1a40e232008-09-26 10:09:34 -04006114 /*
6115 * replace tree blocks in the fs tree with tree blocks in
6116 * the reloc tree.
6117 */
6118 ret = btrfs_merge_path(trans, root, keys, nodes, lowest_level);
6119 BUG_ON(ret < 0);
6120
6121 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04006122 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
6123 0, 0);
6124 BUG_ON(ret);
6125 extent_buffer_get(path->nodes[0]);
6126 eb = path->nodes[0];
6127 btrfs_release_path(reloc_root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04006128 ret = invalidate_extent_cache(reloc_root, eb, group, root);
6129 BUG_ON(ret);
6130 free_extent_buffer(eb);
6131 }
Zheng Yan1a40e232008-09-26 10:09:34 -04006132
Yan Zhengf82d02d2008-10-29 14:49:05 -04006133 mutex_unlock(&root->fs_info->tree_reloc_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04006134 path->lowest_level = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04006135 return 0;
6136}
6137
Chris Masond3977122009-01-05 21:25:51 -05006138static noinline int relocate_tree_block(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04006139 struct btrfs_root *root,
6140 struct btrfs_path *path,
6141 struct btrfs_key *first_key,
6142 struct btrfs_ref_path *ref_path)
6143{
6144 int ret;
Zheng Yan1a40e232008-09-26 10:09:34 -04006145
6146 ret = relocate_one_path(trans, root, path, first_key,
6147 ref_path, NULL, NULL);
6148 BUG_ON(ret);
6149
Zheng Yan1a40e232008-09-26 10:09:34 -04006150 return 0;
6151}
6152
Chris Masond3977122009-01-05 21:25:51 -05006153static noinline int del_extent_zero(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04006154 struct btrfs_root *extent_root,
6155 struct btrfs_path *path,
6156 struct btrfs_key *extent_key)
6157{
6158 int ret;
6159
Zheng Yan1a40e232008-09-26 10:09:34 -04006160 ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1);
6161 if (ret)
6162 goto out;
6163 ret = btrfs_del_item(trans, extent_root, path);
6164out:
Chris Masonedbd8d42007-12-21 16:27:24 -05006165 btrfs_release_path(extent_root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04006166 return ret;
6167}
6168
Chris Masond3977122009-01-05 21:25:51 -05006169static noinline struct btrfs_root *read_ref_root(struct btrfs_fs_info *fs_info,
Zheng Yan1a40e232008-09-26 10:09:34 -04006170 struct btrfs_ref_path *ref_path)
6171{
6172 struct btrfs_key root_key;
6173
6174 root_key.objectid = ref_path->root_objectid;
6175 root_key.type = BTRFS_ROOT_ITEM_KEY;
6176 if (is_cowonly_root(ref_path->root_objectid))
6177 root_key.offset = 0;
6178 else
6179 root_key.offset = (u64)-1;
6180
6181 return btrfs_read_fs_root_no_name(fs_info, &root_key);
6182}
6183
Chris Masond3977122009-01-05 21:25:51 -05006184static noinline int relocate_one_extent(struct btrfs_root *extent_root,
Zheng Yan1a40e232008-09-26 10:09:34 -04006185 struct btrfs_path *path,
6186 struct btrfs_key *extent_key,
6187 struct btrfs_block_group_cache *group,
6188 struct inode *reloc_inode, int pass)
6189{
6190 struct btrfs_trans_handle *trans;
6191 struct btrfs_root *found_root;
6192 struct btrfs_ref_path *ref_path = NULL;
6193 struct disk_extent *new_extents = NULL;
6194 int nr_extents = 0;
6195 int loops;
6196 int ret;
6197 int level;
6198 struct btrfs_key first_key;
6199 u64 prev_block = 0;
6200
Zheng Yan1a40e232008-09-26 10:09:34 -04006201
6202 trans = btrfs_start_transaction(extent_root, 1);
6203 BUG_ON(!trans);
6204
6205 if (extent_key->objectid == 0) {
6206 ret = del_extent_zero(trans, extent_root, path, extent_key);
6207 goto out;
6208 }
6209
6210 ref_path = kmalloc(sizeof(*ref_path), GFP_NOFS);
6211 if (!ref_path) {
Chris Masond3977122009-01-05 21:25:51 -05006212 ret = -ENOMEM;
6213 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04006214 }
6215
6216 for (loops = 0; ; loops++) {
6217 if (loops == 0) {
6218 ret = btrfs_first_ref_path(trans, extent_root, ref_path,
6219 extent_key->objectid);
6220 } else {
6221 ret = btrfs_next_ref_path(trans, extent_root, ref_path);
6222 }
6223 if (ret < 0)
6224 goto out;
6225 if (ret > 0)
6226 break;
6227
6228 if (ref_path->root_objectid == BTRFS_TREE_LOG_OBJECTID ||
6229 ref_path->root_objectid == BTRFS_TREE_RELOC_OBJECTID)
6230 continue;
6231
6232 found_root = read_ref_root(extent_root->fs_info, ref_path);
6233 BUG_ON(!found_root);
6234 /*
6235 * for reference counted tree, only process reference paths
6236 * rooted at the latest committed root.
6237 */
6238 if (found_root->ref_cows &&
6239 ref_path->root_generation != found_root->root_key.offset)
6240 continue;
6241
6242 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
6243 if (pass == 0) {
6244 /*
6245 * copy data extents to new locations
6246 */
6247 u64 group_start = group->key.objectid;
6248 ret = relocate_data_extent(reloc_inode,
6249 extent_key,
6250 group_start);
6251 if (ret < 0)
6252 goto out;
6253 break;
6254 }
6255 level = 0;
6256 } else {
6257 level = ref_path->owner_objectid;
6258 }
6259
6260 if (prev_block != ref_path->nodes[level]) {
6261 struct extent_buffer *eb;
6262 u64 block_start = ref_path->nodes[level];
6263 u64 block_size = btrfs_level_size(found_root, level);
6264
6265 eb = read_tree_block(found_root, block_start,
6266 block_size, 0);
6267 btrfs_tree_lock(eb);
6268 BUG_ON(level != btrfs_header_level(eb));
6269
6270 if (level == 0)
6271 btrfs_item_key_to_cpu(eb, &first_key, 0);
6272 else
6273 btrfs_node_key_to_cpu(eb, &first_key, 0);
6274
6275 btrfs_tree_unlock(eb);
6276 free_extent_buffer(eb);
6277 prev_block = block_start;
6278 }
6279
Yan Zheng24562422009-02-12 14:14:53 -05006280 mutex_lock(&extent_root->fs_info->trans_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05006281 btrfs_record_root_in_trans(found_root);
Yan Zheng24562422009-02-12 14:14:53 -05006282 mutex_unlock(&extent_root->fs_info->trans_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05006283 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
6284 /*
6285 * try to update data extent references while
6286 * keeping metadata shared between snapshots.
6287 */
6288 if (pass == 1) {
6289 ret = relocate_one_path(trans, found_root,
6290 path, &first_key, ref_path,
6291 group, reloc_inode);
6292 if (ret < 0)
6293 goto out;
6294 continue;
6295 }
Zheng Yan1a40e232008-09-26 10:09:34 -04006296 /*
6297 * use fallback method to process the remaining
6298 * references.
6299 */
6300 if (!new_extents) {
6301 u64 group_start = group->key.objectid;
Yan Zhengd899e052008-10-30 14:25:28 -04006302 new_extents = kmalloc(sizeof(*new_extents),
6303 GFP_NOFS);
6304 nr_extents = 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04006305 ret = get_new_locations(reloc_inode,
6306 extent_key,
Yan Zhengd899e052008-10-30 14:25:28 -04006307 group_start, 1,
Zheng Yan1a40e232008-09-26 10:09:34 -04006308 &new_extents,
6309 &nr_extents);
Yan Zhengd899e052008-10-30 14:25:28 -04006310 if (ret)
Zheng Yan1a40e232008-09-26 10:09:34 -04006311 goto out;
6312 }
Zheng Yan1a40e232008-09-26 10:09:34 -04006313 ret = replace_one_extent(trans, found_root,
6314 path, extent_key,
6315 &first_key, ref_path,
6316 new_extents, nr_extents);
Yan Zhenge4404d62008-12-12 10:03:26 -05006317 } else {
Zheng Yan1a40e232008-09-26 10:09:34 -04006318 ret = relocate_tree_block(trans, found_root, path,
6319 &first_key, ref_path);
Zheng Yan1a40e232008-09-26 10:09:34 -04006320 }
6321 if (ret < 0)
6322 goto out;
6323 }
6324 ret = 0;
6325out:
6326 btrfs_end_transaction(trans, extent_root);
6327 kfree(new_extents);
6328 kfree(ref_path);
Chris Masonedbd8d42007-12-21 16:27:24 -05006329 return ret;
6330}
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006331#endif
Chris Masonedbd8d42007-12-21 16:27:24 -05006332
Chris Masonec44a352008-04-28 15:29:52 -04006333static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
6334{
6335 u64 num_devices;
6336 u64 stripped = BTRFS_BLOCK_GROUP_RAID0 |
6337 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
6338
Yan Zheng2b820322008-11-17 21:11:30 -05006339 num_devices = root->fs_info->fs_devices->rw_devices;
Chris Masonec44a352008-04-28 15:29:52 -04006340 if (num_devices == 1) {
6341 stripped |= BTRFS_BLOCK_GROUP_DUP;
6342 stripped = flags & ~stripped;
6343
6344 /* turn raid0 into single device chunks */
6345 if (flags & BTRFS_BLOCK_GROUP_RAID0)
6346 return stripped;
6347
6348 /* turn mirroring into duplication */
6349 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
6350 BTRFS_BLOCK_GROUP_RAID10))
6351 return stripped | BTRFS_BLOCK_GROUP_DUP;
6352 return flags;
6353 } else {
6354 /* they already had raid on here, just return */
Chris Masonec44a352008-04-28 15:29:52 -04006355 if (flags & stripped)
6356 return flags;
6357
6358 stripped |= BTRFS_BLOCK_GROUP_DUP;
6359 stripped = flags & ~stripped;
6360
6361 /* switch duplicated blocks with raid1 */
6362 if (flags & BTRFS_BLOCK_GROUP_DUP)
6363 return stripped | BTRFS_BLOCK_GROUP_RAID1;
6364
6365 /* turn single device chunks into raid0 */
6366 return stripped | BTRFS_BLOCK_GROUP_RAID0;
6367 }
6368 return flags;
6369}
6370
Christoph Hellwigb2950862008-12-02 09:54:17 -05006371static int __alloc_chunk_for_shrink(struct btrfs_root *root,
Chris Mason0ef3e662008-05-24 14:04:53 -04006372 struct btrfs_block_group_cache *shrink_block_group,
6373 int force)
6374{
6375 struct btrfs_trans_handle *trans;
6376 u64 new_alloc_flags;
6377 u64 calc;
6378
Chris Masonc286ac42008-07-22 23:06:41 -04006379 spin_lock(&shrink_block_group->lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006380 if (btrfs_block_group_used(&shrink_block_group->item) +
6381 shrink_block_group->reserved > 0) {
Chris Masonc286ac42008-07-22 23:06:41 -04006382 spin_unlock(&shrink_block_group->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04006383
Chris Mason0ef3e662008-05-24 14:04:53 -04006384 trans = btrfs_start_transaction(root, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04006385 spin_lock(&shrink_block_group->lock);
Chris Mason7d9eb122008-07-08 14:19:17 -04006386
Chris Mason0ef3e662008-05-24 14:04:53 -04006387 new_alloc_flags = update_block_group_flags(root,
6388 shrink_block_group->flags);
6389 if (new_alloc_flags != shrink_block_group->flags) {
6390 calc =
6391 btrfs_block_group_used(&shrink_block_group->item);
6392 } else {
6393 calc = shrink_block_group->key.offset;
6394 }
Chris Masonc286ac42008-07-22 23:06:41 -04006395 spin_unlock(&shrink_block_group->lock);
6396
Chris Mason0ef3e662008-05-24 14:04:53 -04006397 do_chunk_alloc(trans, root->fs_info->extent_root,
6398 calc + 2 * 1024 * 1024, new_alloc_flags, force);
Chris Mason7d9eb122008-07-08 14:19:17 -04006399
Chris Mason0ef3e662008-05-24 14:04:53 -04006400 btrfs_end_transaction(trans, root);
Chris Masonc286ac42008-07-22 23:06:41 -04006401 } else
6402 spin_unlock(&shrink_block_group->lock);
Chris Mason0ef3e662008-05-24 14:04:53 -04006403 return 0;
6404}
6405
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006406
6407int btrfs_prepare_block_group_relocation(struct btrfs_root *root,
6408 struct btrfs_block_group_cache *group)
6409
6410{
6411 __alloc_chunk_for_shrink(root, group, 1);
6412 set_block_group_readonly(group);
6413 return 0;
6414}
6415
6416#if 0
Zheng Yan1a40e232008-09-26 10:09:34 -04006417static int __insert_orphan_inode(struct btrfs_trans_handle *trans,
6418 struct btrfs_root *root,
6419 u64 objectid, u64 size)
6420{
6421 struct btrfs_path *path;
6422 struct btrfs_inode_item *item;
6423 struct extent_buffer *leaf;
6424 int ret;
6425
6426 path = btrfs_alloc_path();
6427 if (!path)
6428 return -ENOMEM;
6429
Chris Masonb9473432009-03-13 11:00:37 -04006430 path->leave_spinning = 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04006431 ret = btrfs_insert_empty_inode(trans, root, path, objectid);
6432 if (ret)
6433 goto out;
6434
6435 leaf = path->nodes[0];
6436 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_inode_item);
6437 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
6438 btrfs_set_inode_generation(leaf, item, 1);
6439 btrfs_set_inode_size(leaf, item, size);
6440 btrfs_set_inode_mode(leaf, item, S_IFREG | 0600);
Yan Zheng0403e472008-12-10 20:32:51 -05006441 btrfs_set_inode_flags(leaf, item, BTRFS_INODE_NOCOMPRESS);
Zheng Yan1a40e232008-09-26 10:09:34 -04006442 btrfs_mark_buffer_dirty(leaf);
6443 btrfs_release_path(root, path);
6444out:
6445 btrfs_free_path(path);
6446 return ret;
6447}
6448
Chris Masond3977122009-01-05 21:25:51 -05006449static noinline struct inode *create_reloc_inode(struct btrfs_fs_info *fs_info,
Zheng Yan1a40e232008-09-26 10:09:34 -04006450 struct btrfs_block_group_cache *group)
6451{
6452 struct inode *inode = NULL;
6453 struct btrfs_trans_handle *trans;
6454 struct btrfs_root *root;
6455 struct btrfs_key root_key;
6456 u64 objectid = BTRFS_FIRST_FREE_OBJECTID;
6457 int err = 0;
6458
6459 root_key.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
6460 root_key.type = BTRFS_ROOT_ITEM_KEY;
6461 root_key.offset = (u64)-1;
6462 root = btrfs_read_fs_root_no_name(fs_info, &root_key);
6463 if (IS_ERR(root))
6464 return ERR_CAST(root);
6465
6466 trans = btrfs_start_transaction(root, 1);
6467 BUG_ON(!trans);
6468
6469 err = btrfs_find_free_objectid(trans, root, objectid, &objectid);
6470 if (err)
6471 goto out;
6472
6473 err = __insert_orphan_inode(trans, root, objectid, group->key.offset);
6474 BUG_ON(err);
6475
6476 err = btrfs_insert_file_extent(trans, root, objectid, 0, 0, 0,
Chris Masonc8b97812008-10-29 14:49:59 -04006477 group->key.offset, 0, group->key.offset,
6478 0, 0, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04006479 BUG_ON(err);
6480
6481 inode = btrfs_iget_locked(root->fs_info->sb, objectid, root);
6482 if (inode->i_state & I_NEW) {
6483 BTRFS_I(inode)->root = root;
6484 BTRFS_I(inode)->location.objectid = objectid;
6485 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
6486 BTRFS_I(inode)->location.offset = 0;
6487 btrfs_read_locked_inode(inode);
6488 unlock_new_inode(inode);
6489 BUG_ON(is_bad_inode(inode));
6490 } else {
6491 BUG_ON(1);
6492 }
Yan Zheng17d217f2008-12-12 10:03:38 -05006493 BTRFS_I(inode)->index_cnt = group->key.objectid;
Zheng Yan1a40e232008-09-26 10:09:34 -04006494
6495 err = btrfs_orphan_add(trans, inode);
6496out:
6497 btrfs_end_transaction(trans, root);
6498 if (err) {
6499 if (inode)
6500 iput(inode);
6501 inode = ERR_PTR(err);
6502 }
6503 return inode;
6504}
6505
Yan Zheng17d217f2008-12-12 10:03:38 -05006506int btrfs_reloc_clone_csums(struct inode *inode, u64 file_pos, u64 len)
6507{
6508
6509 struct btrfs_ordered_sum *sums;
6510 struct btrfs_sector_sum *sector_sum;
6511 struct btrfs_ordered_extent *ordered;
6512 struct btrfs_root *root = BTRFS_I(inode)->root;
6513 struct list_head list;
6514 size_t offset;
6515 int ret;
6516 u64 disk_bytenr;
6517
6518 INIT_LIST_HEAD(&list);
6519
6520 ordered = btrfs_lookup_ordered_extent(inode, file_pos);
6521 BUG_ON(ordered->file_offset != file_pos || ordered->len != len);
6522
6523 disk_bytenr = file_pos + BTRFS_I(inode)->index_cnt;
Yan Zheng07d400a2009-01-06 11:42:00 -05006524 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, disk_bytenr,
Yan Zheng17d217f2008-12-12 10:03:38 -05006525 disk_bytenr + len - 1, &list);
6526
6527 while (!list_empty(&list)) {
6528 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
6529 list_del_init(&sums->list);
6530
6531 sector_sum = sums->sums;
6532 sums->bytenr = ordered->start;
6533
6534 offset = 0;
6535 while (offset < sums->len) {
6536 sector_sum->bytenr += ordered->start - disk_bytenr;
6537 sector_sum++;
6538 offset += root->sectorsize;
6539 }
6540
6541 btrfs_add_ordered_sum(inode, ordered, sums);
6542 }
6543 btrfs_put_ordered_extent(ordered);
6544 return 0;
6545}
6546
Zheng Yan1a40e232008-09-26 10:09:34 -04006547int btrfs_relocate_block_group(struct btrfs_root *root, u64 group_start)
Chris Masonedbd8d42007-12-21 16:27:24 -05006548{
6549 struct btrfs_trans_handle *trans;
Chris Masonedbd8d42007-12-21 16:27:24 -05006550 struct btrfs_path *path;
Zheng Yan1a40e232008-09-26 10:09:34 -04006551 struct btrfs_fs_info *info = root->fs_info;
6552 struct extent_buffer *leaf;
6553 struct inode *reloc_inode;
6554 struct btrfs_block_group_cache *block_group;
6555 struct btrfs_key key;
Yan Zhengd899e052008-10-30 14:25:28 -04006556 u64 skipped;
Chris Masonedbd8d42007-12-21 16:27:24 -05006557 u64 cur_byte;
6558 u64 total_found;
Chris Masonedbd8d42007-12-21 16:27:24 -05006559 u32 nritems;
6560 int ret;
Chris Masona061fc82008-05-07 11:43:44 -04006561 int progress;
Zheng Yan1a40e232008-09-26 10:09:34 -04006562 int pass = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05006563
Chris Masonedbd8d42007-12-21 16:27:24 -05006564 root = root->fs_info->extent_root;
Zheng Yan1a40e232008-09-26 10:09:34 -04006565
6566 block_group = btrfs_lookup_block_group(info, group_start);
6567 BUG_ON(!block_group);
Chris Masonedbd8d42007-12-21 16:27:24 -05006568
Chris Masond3977122009-01-05 21:25:51 -05006569 printk(KERN_INFO "btrfs relocating block group %llu flags %llu\n",
Zheng Yan1a40e232008-09-26 10:09:34 -04006570 (unsigned long long)block_group->key.objectid,
6571 (unsigned long long)block_group->flags);
Chris Mason323da792008-05-09 11:46:48 -04006572
Zheng Yan1a40e232008-09-26 10:09:34 -04006573 path = btrfs_alloc_path();
6574 BUG_ON(!path);
Chris Mason323da792008-05-09 11:46:48 -04006575
Zheng Yan1a40e232008-09-26 10:09:34 -04006576 reloc_inode = create_reloc_inode(info, block_group);
6577 BUG_ON(IS_ERR(reloc_inode));
6578
Zheng Yan1a40e232008-09-26 10:09:34 -04006579 __alloc_chunk_for_shrink(root, block_group, 1);
Yan Zhengc146afa2008-11-12 14:34:12 -05006580 set_block_group_readonly(block_group);
Zheng Yan1a40e232008-09-26 10:09:34 -04006581
Zheng Yan1a40e232008-09-26 10:09:34 -04006582 btrfs_start_delalloc_inodes(info->tree_root);
6583 btrfs_wait_ordered_extents(info->tree_root, 0);
Chris Mason0ef3e662008-05-24 14:04:53 -04006584again:
Yan Zhengd899e052008-10-30 14:25:28 -04006585 skipped = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05006586 total_found = 0;
Chris Masona061fc82008-05-07 11:43:44 -04006587 progress = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04006588 key.objectid = block_group->key.objectid;
Chris Masonedbd8d42007-12-21 16:27:24 -05006589 key.offset = 0;
6590 key.type = 0;
Yan73e48b22008-01-03 14:14:39 -05006591 cur_byte = key.objectid;
Chris Mason4313b392008-01-03 09:08:48 -05006592
Zheng Yan1a40e232008-09-26 10:09:34 -04006593 trans = btrfs_start_transaction(info->tree_root, 1);
6594 btrfs_commit_transaction(trans, info->tree_root);
Chris Masonea8c2812008-08-04 23:17:27 -04006595
Zheng Yan1a40e232008-09-26 10:09:34 -04006596 mutex_lock(&root->fs_info->cleaner_mutex);
6597 btrfs_clean_old_snapshots(info->tree_root);
6598 btrfs_remove_leaf_refs(info->tree_root, (u64)-1, 1);
6599 mutex_unlock(&root->fs_info->cleaner_mutex);
Chris Masonea8c2812008-08-04 23:17:27 -04006600
Chris Mason56bec292009-03-13 10:10:06 -04006601 trans = btrfs_start_transaction(info->tree_root, 1);
6602 btrfs_commit_transaction(trans, info->tree_root);
6603
Chris Masond3977122009-01-05 21:25:51 -05006604 while (1) {
Chris Masonedbd8d42007-12-21 16:27:24 -05006605 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6606 if (ret < 0)
6607 goto out;
Chris Mason7d9eb122008-07-08 14:19:17 -04006608next:
Chris Masonedbd8d42007-12-21 16:27:24 -05006609 leaf = path->nodes[0];
Chris Masonedbd8d42007-12-21 16:27:24 -05006610 nritems = btrfs_header_nritems(leaf);
Yan73e48b22008-01-03 14:14:39 -05006611 if (path->slots[0] >= nritems) {
6612 ret = btrfs_next_leaf(root, path);
6613 if (ret < 0)
6614 goto out;
6615 if (ret == 1) {
6616 ret = 0;
6617 break;
Chris Masonedbd8d42007-12-21 16:27:24 -05006618 }
Yan73e48b22008-01-03 14:14:39 -05006619 leaf = path->nodes[0];
6620 nritems = btrfs_header_nritems(leaf);
6621 }
6622
Zheng Yan1a40e232008-09-26 10:09:34 -04006623 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Chris Mason725c8462008-01-04 16:47:16 -05006624
Zheng Yan1a40e232008-09-26 10:09:34 -04006625 if (key.objectid >= block_group->key.objectid +
6626 block_group->key.offset)
Chris Mason8f18cf12008-04-25 16:53:30 -04006627 break;
6628
Chris Mason725c8462008-01-04 16:47:16 -05006629 if (progress && need_resched()) {
Chris Mason725c8462008-01-04 16:47:16 -05006630 btrfs_release_path(root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04006631 cond_resched();
Chris Mason725c8462008-01-04 16:47:16 -05006632 progress = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04006633 continue;
Chris Mason725c8462008-01-04 16:47:16 -05006634 }
6635 progress = 1;
6636
Zheng Yan1a40e232008-09-26 10:09:34 -04006637 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY ||
6638 key.objectid + key.offset <= cur_byte) {
Yan73e48b22008-01-03 14:14:39 -05006639 path->slots[0]++;
Chris Masonedbd8d42007-12-21 16:27:24 -05006640 goto next;
6641 }
Yan73e48b22008-01-03 14:14:39 -05006642
Chris Masonedbd8d42007-12-21 16:27:24 -05006643 total_found++;
Zheng Yan1a40e232008-09-26 10:09:34 -04006644 cur_byte = key.objectid + key.offset;
Chris Masonedbd8d42007-12-21 16:27:24 -05006645 btrfs_release_path(root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04006646
6647 __alloc_chunk_for_shrink(root, block_group, 0);
6648 ret = relocate_one_extent(root, path, &key, block_group,
6649 reloc_inode, pass);
6650 BUG_ON(ret < 0);
Yan Zhengd899e052008-10-30 14:25:28 -04006651 if (ret > 0)
6652 skipped++;
Zheng Yan1a40e232008-09-26 10:09:34 -04006653
6654 key.objectid = cur_byte;
6655 key.type = 0;
6656 key.offset = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05006657 }
6658
6659 btrfs_release_path(root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04006660
6661 if (pass == 0) {
6662 btrfs_wait_ordered_range(reloc_inode, 0, (u64)-1);
6663 invalidate_mapping_pages(reloc_inode->i_mapping, 0, -1);
Zheng Yan1a40e232008-09-26 10:09:34 -04006664 }
Chris Masonedbd8d42007-12-21 16:27:24 -05006665
6666 if (total_found > 0) {
Chris Masond3977122009-01-05 21:25:51 -05006667 printk(KERN_INFO "btrfs found %llu extents in pass %d\n",
Zheng Yan1a40e232008-09-26 10:09:34 -04006668 (unsigned long long)total_found, pass);
6669 pass++;
Yan Zhengd899e052008-10-30 14:25:28 -04006670 if (total_found == skipped && pass > 2) {
6671 iput(reloc_inode);
6672 reloc_inode = create_reloc_inode(info, block_group);
6673 pass = 0;
6674 }
Chris Masonedbd8d42007-12-21 16:27:24 -05006675 goto again;
6676 }
6677
Zheng Yan1a40e232008-09-26 10:09:34 -04006678 /* delete reloc_inode */
6679 iput(reloc_inode);
Chris Mason7d9eb122008-07-08 14:19:17 -04006680
Zheng Yan1a40e232008-09-26 10:09:34 -04006681 /* unpin extents in this range */
6682 trans = btrfs_start_transaction(info->tree_root, 1);
6683 btrfs_commit_transaction(trans, info->tree_root);
Chris Mason0ef3e662008-05-24 14:04:53 -04006684
Zheng Yan1a40e232008-09-26 10:09:34 -04006685 spin_lock(&block_group->lock);
6686 WARN_ON(block_group->pinned > 0);
6687 WARN_ON(block_group->reserved > 0);
6688 WARN_ON(btrfs_block_group_used(&block_group->item) > 0);
6689 spin_unlock(&block_group->lock);
Chris Masonfa9c0d72009-04-03 09:47:43 -04006690 btrfs_put_block_group(block_group);
Zheng Yan1a40e232008-09-26 10:09:34 -04006691 ret = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05006692out:
Zheng Yan1a40e232008-09-26 10:09:34 -04006693 btrfs_free_path(path);
Chris Masonedbd8d42007-12-21 16:27:24 -05006694 return ret;
6695}
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006696#endif
Chris Masonedbd8d42007-12-21 16:27:24 -05006697
Christoph Hellwigb2950862008-12-02 09:54:17 -05006698static int find_first_block_group(struct btrfs_root *root,
6699 struct btrfs_path *path, struct btrfs_key *key)
Chris Mason0b86a832008-03-24 15:01:56 -04006700{
Chris Mason925baed2008-06-25 16:01:30 -04006701 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006702 struct btrfs_key found_key;
6703 struct extent_buffer *leaf;
6704 int slot;
6705
6706 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
6707 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04006708 goto out;
6709
Chris Masond3977122009-01-05 21:25:51 -05006710 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04006711 slot = path->slots[0];
6712 leaf = path->nodes[0];
6713 if (slot >= btrfs_header_nritems(leaf)) {
6714 ret = btrfs_next_leaf(root, path);
6715 if (ret == 0)
6716 continue;
6717 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04006718 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04006719 break;
6720 }
6721 btrfs_item_key_to_cpu(leaf, &found_key, slot);
6722
6723 if (found_key.objectid >= key->objectid &&
Chris Mason925baed2008-06-25 16:01:30 -04006724 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
6725 ret = 0;
6726 goto out;
6727 }
Chris Mason0b86a832008-03-24 15:01:56 -04006728 path->slots[0]++;
6729 }
6730 ret = -ENOENT;
Chris Mason925baed2008-06-25 16:01:30 -04006731out:
Chris Mason0b86a832008-03-24 15:01:56 -04006732 return ret;
6733}
6734
Zheng Yan1a40e232008-09-26 10:09:34 -04006735int btrfs_free_block_groups(struct btrfs_fs_info *info)
6736{
6737 struct btrfs_block_group_cache *block_group;
Chris Mason4184ea72009-03-10 12:39:20 -04006738 struct btrfs_space_info *space_info;
Zheng Yan1a40e232008-09-26 10:09:34 -04006739 struct rb_node *n;
6740
Zheng Yan1a40e232008-09-26 10:09:34 -04006741 spin_lock(&info->block_group_cache_lock);
6742 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
6743 block_group = rb_entry(n, struct btrfs_block_group_cache,
6744 cache_node);
Zheng Yan1a40e232008-09-26 10:09:34 -04006745 rb_erase(&block_group->cache_node,
6746 &info->block_group_cache_tree);
Yan Zhengd899e052008-10-30 14:25:28 -04006747 spin_unlock(&info->block_group_cache_lock);
6748
6749 btrfs_remove_free_space_cache(block_group);
Josef Bacik80eb2342008-10-29 14:49:05 -04006750 down_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04006751 list_del(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04006752 up_write(&block_group->space_info->groups_sem);
Yan Zhengd2fb3432008-12-11 16:30:39 -05006753
6754 WARN_ON(atomic_read(&block_group->count) != 1);
Zheng Yan1a40e232008-09-26 10:09:34 -04006755 kfree(block_group);
Yan Zhengd899e052008-10-30 14:25:28 -04006756
6757 spin_lock(&info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04006758 }
6759 spin_unlock(&info->block_group_cache_lock);
Chris Mason4184ea72009-03-10 12:39:20 -04006760
6761 /* now that all the block groups are freed, go through and
6762 * free all the space_info structs. This is only called during
6763 * the final stages of unmount, and so we know nobody is
6764 * using them. We call synchronize_rcu() once before we start,
6765 * just to be on the safe side.
6766 */
6767 synchronize_rcu();
6768
6769 while(!list_empty(&info->space_info)) {
6770 space_info = list_entry(info->space_info.next,
6771 struct btrfs_space_info,
6772 list);
6773
6774 list_del(&space_info->list);
6775 kfree(space_info);
6776 }
Zheng Yan1a40e232008-09-26 10:09:34 -04006777 return 0;
6778}
6779
Chris Mason9078a3e2007-04-26 16:46:15 -04006780int btrfs_read_block_groups(struct btrfs_root *root)
6781{
6782 struct btrfs_path *path;
6783 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04006784 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04006785 struct btrfs_fs_info *info = root->fs_info;
Chris Mason6324fbf2008-03-24 15:01:59 -04006786 struct btrfs_space_info *space_info;
Chris Mason9078a3e2007-04-26 16:46:15 -04006787 struct btrfs_key key;
6788 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -04006789 struct extent_buffer *leaf;
Chris Mason96b51792007-10-15 16:15:19 -04006790
Chris Masonbe744172007-05-06 10:15:01 -04006791 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04006792 key.objectid = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006793 key.offset = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04006794 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
Chris Mason9078a3e2007-04-26 16:46:15 -04006795 path = btrfs_alloc_path();
6796 if (!path)
6797 return -ENOMEM;
6798
Chris Masond3977122009-01-05 21:25:51 -05006799 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04006800 ret = find_first_block_group(root, path, &key);
6801 if (ret > 0) {
6802 ret = 0;
6803 goto error;
Chris Mason9078a3e2007-04-26 16:46:15 -04006804 }
Chris Mason0b86a832008-03-24 15:01:56 -04006805 if (ret != 0)
6806 goto error;
6807
Chris Mason5f39d392007-10-15 16:14:19 -04006808 leaf = path->nodes[0];
6809 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason8f18cf12008-04-25 16:53:30 -04006810 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -04006811 if (!cache) {
Chris Mason0b86a832008-03-24 15:01:56 -04006812 ret = -ENOMEM;
Chris Mason9078a3e2007-04-26 16:46:15 -04006813 break;
6814 }
Chris Mason3e1ad542007-05-07 20:03:49 -04006815
Yan Zhengd2fb3432008-12-11 16:30:39 -05006816 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04006817 spin_lock_init(&cache->lock);
Josef Bacik6226cb02009-04-03 10:14:18 -04006818 spin_lock_init(&cache->tree_lock);
Josef Bacikea6a4782008-11-20 12:16:16 -05006819 mutex_init(&cache->cache_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04006820 INIT_LIST_HEAD(&cache->list);
Chris Masonfa9c0d72009-04-03 09:47:43 -04006821 INIT_LIST_HEAD(&cache->cluster_list);
Chris Mason5f39d392007-10-15 16:14:19 -04006822 read_extent_buffer(leaf, &cache->item,
6823 btrfs_item_ptr_offset(leaf, path->slots[0]),
6824 sizeof(cache->item));
Chris Mason9078a3e2007-04-26 16:46:15 -04006825 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Mason0b86a832008-03-24 15:01:56 -04006826
Chris Mason9078a3e2007-04-26 16:46:15 -04006827 key.objectid = found_key.objectid + found_key.offset;
6828 btrfs_release_path(root, path);
Chris Mason0b86a832008-03-24 15:01:56 -04006829 cache->flags = btrfs_block_group_flags(&cache->item);
Chris Mason96b51792007-10-15 16:15:19 -04006830
Chris Mason6324fbf2008-03-24 15:01:59 -04006831 ret = update_space_info(info, cache->flags, found_key.offset,
6832 btrfs_block_group_used(&cache->item),
6833 &space_info);
6834 BUG_ON(ret);
6835 cache->space_info = space_info;
Josef Bacik80eb2342008-10-29 14:49:05 -04006836 down_write(&space_info->groups_sem);
6837 list_add_tail(&cache->list, &space_info->block_groups);
6838 up_write(&space_info->groups_sem);
Chris Mason6324fbf2008-03-24 15:01:59 -04006839
Josef Bacik0f9dd462008-09-23 13:14:11 -04006840 ret = btrfs_add_block_group_cache(root->fs_info, cache);
6841 BUG_ON(ret);
Chris Mason75ccf472008-09-30 19:24:06 -04006842
6843 set_avail_alloc_bits(root->fs_info, cache->flags);
Yan Zheng2b820322008-11-17 21:11:30 -05006844 if (btrfs_chunk_readonly(root, cache->key.objectid))
6845 set_block_group_readonly(cache);
Chris Mason9078a3e2007-04-26 16:46:15 -04006846 }
Chris Mason0b86a832008-03-24 15:01:56 -04006847 ret = 0;
6848error:
Chris Mason9078a3e2007-04-26 16:46:15 -04006849 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04006850 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04006851}
Chris Mason6324fbf2008-03-24 15:01:59 -04006852
6853int btrfs_make_block_group(struct btrfs_trans_handle *trans,
6854 struct btrfs_root *root, u64 bytes_used,
Chris Masone17cade2008-04-15 15:41:47 -04006855 u64 type, u64 chunk_objectid, u64 chunk_offset,
Chris Mason6324fbf2008-03-24 15:01:59 -04006856 u64 size)
6857{
6858 int ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04006859 struct btrfs_root *extent_root;
6860 struct btrfs_block_group_cache *cache;
Chris Mason6324fbf2008-03-24 15:01:59 -04006861
6862 extent_root = root->fs_info->extent_root;
Chris Mason6324fbf2008-03-24 15:01:59 -04006863
Chris Mason12fcfd22009-03-24 10:24:20 -04006864 root->fs_info->last_trans_log_full_commit = trans->transid;
Chris Masone02119d2008-09-05 16:13:11 -04006865
Chris Mason8f18cf12008-04-25 16:53:30 -04006866 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Josef Bacik0f9dd462008-09-23 13:14:11 -04006867 if (!cache)
6868 return -ENOMEM;
6869
Chris Masone17cade2008-04-15 15:41:47 -04006870 cache->key.objectid = chunk_offset;
Chris Mason6324fbf2008-03-24 15:01:59 -04006871 cache->key.offset = size;
Yan Zhengd2fb3432008-12-11 16:30:39 -05006872 cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
6873 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04006874 spin_lock_init(&cache->lock);
Josef Bacik6226cb02009-04-03 10:14:18 -04006875 spin_lock_init(&cache->tree_lock);
Josef Bacikea6a4782008-11-20 12:16:16 -05006876 mutex_init(&cache->cache_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04006877 INIT_LIST_HEAD(&cache->list);
Chris Masonfa9c0d72009-04-03 09:47:43 -04006878 INIT_LIST_HEAD(&cache->cluster_list);
Chris Mason0ef3e662008-05-24 14:04:53 -04006879
Chris Mason6324fbf2008-03-24 15:01:59 -04006880 btrfs_set_block_group_used(&cache->item, bytes_used);
Chris Mason6324fbf2008-03-24 15:01:59 -04006881 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
6882 cache->flags = type;
6883 btrfs_set_block_group_flags(&cache->item, type);
6884
6885 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
6886 &cache->space_info);
6887 BUG_ON(ret);
Josef Bacik80eb2342008-10-29 14:49:05 -04006888 down_write(&cache->space_info->groups_sem);
6889 list_add_tail(&cache->list, &cache->space_info->block_groups);
6890 up_write(&cache->space_info->groups_sem);
Chris Mason6324fbf2008-03-24 15:01:59 -04006891
Josef Bacik0f9dd462008-09-23 13:14:11 -04006892 ret = btrfs_add_block_group_cache(root->fs_info, cache);
6893 BUG_ON(ret);
Chris Masonc286ac42008-07-22 23:06:41 -04006894
Chris Mason6324fbf2008-03-24 15:01:59 -04006895 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
6896 sizeof(cache->item));
6897 BUG_ON(ret);
6898
Chris Masond18a2c42008-04-04 15:40:00 -04006899 set_avail_alloc_bits(extent_root->fs_info, type);
Chris Mason925baed2008-06-25 16:01:30 -04006900
Chris Mason6324fbf2008-03-24 15:01:59 -04006901 return 0;
6902}
Zheng Yan1a40e232008-09-26 10:09:34 -04006903
6904int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
6905 struct btrfs_root *root, u64 group_start)
6906{
6907 struct btrfs_path *path;
6908 struct btrfs_block_group_cache *block_group;
Chris Mason44fb5512009-06-04 15:34:51 -04006909 struct btrfs_free_cluster *cluster;
Zheng Yan1a40e232008-09-26 10:09:34 -04006910 struct btrfs_key key;
6911 int ret;
6912
Zheng Yan1a40e232008-09-26 10:09:34 -04006913 root = root->fs_info->extent_root;
6914
6915 block_group = btrfs_lookup_block_group(root->fs_info, group_start);
6916 BUG_ON(!block_group);
Yan Zhengc146afa2008-11-12 14:34:12 -05006917 BUG_ON(!block_group->ro);
Zheng Yan1a40e232008-09-26 10:09:34 -04006918
6919 memcpy(&key, &block_group->key, sizeof(key));
6920
Chris Mason44fb5512009-06-04 15:34:51 -04006921 /* make sure this block group isn't part of an allocation cluster */
6922 cluster = &root->fs_info->data_alloc_cluster;
6923 spin_lock(&cluster->refill_lock);
6924 btrfs_return_cluster_to_free_space(block_group, cluster);
6925 spin_unlock(&cluster->refill_lock);
6926
6927 /*
6928 * make sure this block group isn't part of a metadata
6929 * allocation cluster
6930 */
6931 cluster = &root->fs_info->meta_alloc_cluster;
6932 spin_lock(&cluster->refill_lock);
6933 btrfs_return_cluster_to_free_space(block_group, cluster);
6934 spin_unlock(&cluster->refill_lock);
6935
Zheng Yan1a40e232008-09-26 10:09:34 -04006936 path = btrfs_alloc_path();
6937 BUG_ON(!path);
6938
Yan Zheng3dfdb932009-01-21 10:49:16 -05006939 spin_lock(&root->fs_info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04006940 rb_erase(&block_group->cache_node,
6941 &root->fs_info->block_group_cache_tree);
Yan Zheng3dfdb932009-01-21 10:49:16 -05006942 spin_unlock(&root->fs_info->block_group_cache_lock);
6943 btrfs_remove_free_space_cache(block_group);
Josef Bacik80eb2342008-10-29 14:49:05 -04006944 down_write(&block_group->space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04006945 /*
6946 * we must use list_del_init so people can check to see if they
6947 * are still on the list after taking the semaphore
6948 */
6949 list_del_init(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04006950 up_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04006951
Yan Zhengc146afa2008-11-12 14:34:12 -05006952 spin_lock(&block_group->space_info->lock);
6953 block_group->space_info->total_bytes -= block_group->key.offset;
6954 block_group->space_info->bytes_readonly -= block_group->key.offset;
6955 spin_unlock(&block_group->space_info->lock);
Yan Zheng2b820322008-11-17 21:11:30 -05006956 block_group->space_info->full = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05006957
Chris Masonfa9c0d72009-04-03 09:47:43 -04006958 btrfs_put_block_group(block_group);
6959 btrfs_put_block_group(block_group);
Zheng Yan1a40e232008-09-26 10:09:34 -04006960
6961 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
6962 if (ret > 0)
6963 ret = -EIO;
6964 if (ret < 0)
6965 goto out;
6966
6967 ret = btrfs_del_item(trans, root, path);
6968out:
6969 btrfs_free_path(path);
6970 return ret;
6971}