blob: 62a332d34fdb84675e816c8b6047403e7b839152 [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
Zach Brownec6b9102007-07-11 10:00:37 -040018#include <linux/sched.h>
Chris Masonedbd8d42007-12-21 16:27:24 -050019#include <linux/pagemap.h>
Chris Masonec44a352008-04-28 15:29:52 -040020#include <linux/writeback.h>
David Woodhouse21af8042008-08-12 14:13:26 +010021#include <linux/blkdev.h>
Chris Masonb7a9f292009-02-04 09:23:45 -050022#include <linux/sort.h>
Chris Mason4184ea72009-03-10 12:39:20 -040023#include <linux/rcupdate.h>
Chris Mason4b4e25f2008-11-20 10:22:27 -050024#include "compat.h"
Chris Mason74493f72007-12-11 09:25:06 -050025#include "hash.h"
Chris Masonfec577f2007-02-26 10:40:21 -050026#include "ctree.h"
27#include "disk-io.h"
28#include "print-tree.h"
Chris Masone089f052007-03-16 16:20:31 -040029#include "transaction.h"
Chris Mason0b86a832008-03-24 15:01:56 -040030#include "volumes.h"
Chris Mason925baed2008-06-25 16:01:30 -040031#include "locking.h"
Chris Masonfa9c0d792009-04-03 09:47:43 -040032#include "free-space-cache.h"
Chris Masonfec577f2007-02-26 10:40:21 -050033
Chris Mason56bec292009-03-13 10:10:06 -040034static int update_reserved_extents(struct btrfs_root *root,
35 u64 bytenr, u64 num, int reserve);
Josef Bacikf3465ca2008-11-12 14:19:50 -050036static int update_block_group(struct btrfs_trans_handle *trans,
37 struct btrfs_root *root,
38 u64 bytenr, u64 num_bytes, int alloc,
39 int mark_free);
Yan Zheng5d4f98a2009-06-10 10:45:14 -040040static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
41 struct btrfs_root *root,
42 u64 bytenr, u64 num_bytes, u64 parent,
43 u64 root_objectid, u64 owner_objectid,
44 u64 owner_offset, int refs_to_drop,
45 struct btrfs_delayed_extent_op *extra_op);
46static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
47 struct extent_buffer *leaf,
48 struct btrfs_extent_item *ei);
49static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
50 struct btrfs_root *root,
51 u64 parent, u64 root_objectid,
52 u64 flags, u64 owner, u64 offset,
53 struct btrfs_key *ins, int ref_mod);
54static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
55 struct btrfs_root *root,
56 u64 parent, u64 root_objectid,
57 u64 flags, struct btrfs_disk_key *key,
58 int level, struct btrfs_key *ins);
Yand548ee52008-01-03 13:56:30 -050059
Josef Bacik6a632092009-02-20 11:00:09 -050060static int do_chunk_alloc(struct btrfs_trans_handle *trans,
61 struct btrfs_root *extent_root, u64 alloc_bytes,
62 u64 flags, int force);
63
Josef Bacik0f9dd462008-09-23 13:14:11 -040064static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
65{
66 return (cache->flags & bits) == bits;
67}
68
69/*
70 * this adds the block group to the fs_info rb tree for the block group
71 * cache
72 */
Christoph Hellwigb2950862008-12-02 09:54:17 -050073static int btrfs_add_block_group_cache(struct btrfs_fs_info *info,
Josef Bacik0f9dd462008-09-23 13:14:11 -040074 struct btrfs_block_group_cache *block_group)
75{
76 struct rb_node **p;
77 struct rb_node *parent = NULL;
78 struct btrfs_block_group_cache *cache;
79
80 spin_lock(&info->block_group_cache_lock);
81 p = &info->block_group_cache_tree.rb_node;
82
83 while (*p) {
84 parent = *p;
85 cache = rb_entry(parent, struct btrfs_block_group_cache,
86 cache_node);
87 if (block_group->key.objectid < cache->key.objectid) {
88 p = &(*p)->rb_left;
89 } else if (block_group->key.objectid > cache->key.objectid) {
90 p = &(*p)->rb_right;
91 } else {
92 spin_unlock(&info->block_group_cache_lock);
93 return -EEXIST;
94 }
95 }
96
97 rb_link_node(&block_group->cache_node, parent, p);
98 rb_insert_color(&block_group->cache_node,
99 &info->block_group_cache_tree);
100 spin_unlock(&info->block_group_cache_lock);
101
102 return 0;
103}
104
105/*
106 * This will return the block group at or after bytenr if contains is 0, else
107 * it will return the block group that contains the bytenr
108 */
109static struct btrfs_block_group_cache *
110block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr,
111 int contains)
112{
113 struct btrfs_block_group_cache *cache, *ret = NULL;
114 struct rb_node *n;
115 u64 end, start;
116
117 spin_lock(&info->block_group_cache_lock);
118 n = info->block_group_cache_tree.rb_node;
119
120 while (n) {
121 cache = rb_entry(n, struct btrfs_block_group_cache,
122 cache_node);
123 end = cache->key.objectid + cache->key.offset - 1;
124 start = cache->key.objectid;
125
126 if (bytenr < start) {
127 if (!contains && (!ret || start < ret->key.objectid))
128 ret = cache;
129 n = n->rb_left;
130 } else if (bytenr > start) {
131 if (contains && bytenr <= end) {
132 ret = cache;
133 break;
134 }
135 n = n->rb_right;
136 } else {
137 ret = cache;
138 break;
139 }
140 }
Yan Zhengd2fb3432008-12-11 16:30:39 -0500141 if (ret)
142 atomic_inc(&ret->count);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400143 spin_unlock(&info->block_group_cache_lock);
144
145 return ret;
146}
147
148/*
149 * this is only called by cache_block_group, since we could have freed extents
150 * we need to check the pinned_extents for any extents that can't be used yet
151 * since their free space will be released as soon as the transaction commits.
152 */
153static int add_new_free_space(struct btrfs_block_group_cache *block_group,
154 struct btrfs_fs_info *info, u64 start, u64 end)
155{
156 u64 extent_start, extent_end, size;
157 int ret;
158
159 while (start < end) {
160 ret = find_first_extent_bit(&info->pinned_extents, start,
161 &extent_start, &extent_end,
162 EXTENT_DIRTY);
163 if (ret)
164 break;
165
166 if (extent_start == start) {
167 start = extent_end + 1;
168 } else if (extent_start > start && extent_start < end) {
169 size = extent_start - start;
Josef Bacikea6a4782008-11-20 12:16:16 -0500170 ret = btrfs_add_free_space(block_group, start,
171 size);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400172 BUG_ON(ret);
173 start = extent_end + 1;
174 } else {
175 break;
176 }
177 }
178
179 if (start < end) {
180 size = end - start;
Josef Bacikea6a4782008-11-20 12:16:16 -0500181 ret = btrfs_add_free_space(block_group, start, size);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400182 BUG_ON(ret);
183 }
184
185 return 0;
186}
187
Yan Zhenga512bbf2008-12-08 16:46:26 -0500188static int remove_sb_from_cache(struct btrfs_root *root,
189 struct btrfs_block_group_cache *cache)
190{
191 u64 bytenr;
192 u64 *logical;
193 int stripe_len;
194 int i, nr, ret;
195
196 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
197 bytenr = btrfs_sb_offset(i);
198 ret = btrfs_rmap_block(&root->fs_info->mapping_tree,
199 cache->key.objectid, bytenr, 0,
200 &logical, &nr, &stripe_len);
201 BUG_ON(ret);
202 while (nr--) {
203 btrfs_remove_free_space(cache, logical[nr],
204 stripe_len);
205 }
206 kfree(logical);
207 }
208 return 0;
209}
210
Chris Masone37c9e62007-05-09 20:13:14 -0400211static int cache_block_group(struct btrfs_root *root,
212 struct btrfs_block_group_cache *block_group)
213{
214 struct btrfs_path *path;
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400215 int ret = 0;
Chris Masone37c9e62007-05-09 20:13:14 -0400216 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400217 struct extent_buffer *leaf;
Chris Masone37c9e62007-05-09 20:13:14 -0400218 int slot;
Yan Zhenge4404d62008-12-12 10:03:26 -0500219 u64 last;
Chris Masone37c9e62007-05-09 20:13:14 -0400220
Chris Mason00f5c792007-11-30 10:09:33 -0500221 if (!block_group)
222 return 0;
223
Chris Masone37c9e62007-05-09 20:13:14 -0400224 root = root->fs_info->extent_root;
Chris Masone37c9e62007-05-09 20:13:14 -0400225
226 if (block_group->cached)
227 return 0;
Chris Masonf510cfe2007-10-15 16:14:48 -0400228
Chris Masone37c9e62007-05-09 20:13:14 -0400229 path = btrfs_alloc_path();
230 if (!path)
231 return -ENOMEM;
Yan7d7d6062007-09-14 16:15:28 -0400232
Chris Mason2cc58cf2007-08-27 16:49:44 -0400233 path->reada = 2;
Chris Mason5cd57b22008-06-25 16:01:30 -0400234 /*
235 * we get into deadlocks with paths held by callers of this function.
236 * since the alloc_mutex is protecting things right now, just
237 * skip the locking here
238 */
239 path->skip_locking = 1;
Yan Zhenge4404d62008-12-12 10:03:26 -0500240 last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET);
241 key.objectid = last;
Chris Masone37c9e62007-05-09 20:13:14 -0400242 key.offset = 0;
243 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
244 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
245 if (ret < 0)
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400246 goto err;
Yan Zhenga512bbf2008-12-08 16:46:26 -0500247
Chris Masond3977122009-01-05 21:25:51 -0500248 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400249 leaf = path->nodes[0];
Chris Masone37c9e62007-05-09 20:13:14 -0400250 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -0400251 if (slot >= btrfs_header_nritems(leaf)) {
Chris Masone37c9e62007-05-09 20:13:14 -0400252 ret = btrfs_next_leaf(root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -0400253 if (ret < 0)
254 goto err;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400255 if (ret == 0)
Chris Masone37c9e62007-05-09 20:13:14 -0400256 continue;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400257 else
Chris Masone37c9e62007-05-09 20:13:14 -0400258 break;
Chris Masone37c9e62007-05-09 20:13:14 -0400259 }
Chris Mason5f39d392007-10-15 16:14:19 -0400260 btrfs_item_key_to_cpu(leaf, &key, slot);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400261 if (key.objectid < block_group->key.objectid)
Yan7d7d6062007-09-14 16:15:28 -0400262 goto next;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400263
Chris Masone37c9e62007-05-09 20:13:14 -0400264 if (key.objectid >= block_group->key.objectid +
Josef Bacik0f9dd462008-09-23 13:14:11 -0400265 block_group->key.offset)
Yan7d7d6062007-09-14 16:15:28 -0400266 break;
Yan7d7d6062007-09-14 16:15:28 -0400267
268 if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) {
Josef Bacik0f9dd462008-09-23 13:14:11 -0400269 add_new_free_space(block_group, root->fs_info, last,
270 key.objectid);
271
Yan7d7d6062007-09-14 16:15:28 -0400272 last = key.objectid + key.offset;
Chris Masone37c9e62007-05-09 20:13:14 -0400273 }
Yan7d7d6062007-09-14 16:15:28 -0400274next:
Chris Masone37c9e62007-05-09 20:13:14 -0400275 path->slots[0]++;
276 }
277
Josef Bacik0f9dd462008-09-23 13:14:11 -0400278 add_new_free_space(block_group, root->fs_info, last,
279 block_group->key.objectid +
280 block_group->key.offset);
281
Chris Masone37c9e62007-05-09 20:13:14 -0400282 block_group->cached = 1;
Josef Bacik70cb0742009-04-03 10:14:19 -0400283 remove_sb_from_cache(root, block_group);
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400284 ret = 0;
Chris Mason54aa1f42007-06-22 14:16:25 -0400285err:
Chris Masone37c9e62007-05-09 20:13:14 -0400286 btrfs_free_path(path);
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400287 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -0400288}
289
Josef Bacik0f9dd462008-09-23 13:14:11 -0400290/*
291 * return the block group that starts at or after bytenr
292 */
Chris Masond3977122009-01-05 21:25:51 -0500293static struct btrfs_block_group_cache *
294btrfs_lookup_first_block_group(struct btrfs_fs_info *info, u64 bytenr)
Chris Mason0ef3e662008-05-24 14:04:53 -0400295{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400296 struct btrfs_block_group_cache *cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400297
Josef Bacik0f9dd462008-09-23 13:14:11 -0400298 cache = block_group_cache_tree_search(info, bytenr, 0);
Chris Mason0ef3e662008-05-24 14:04:53 -0400299
Josef Bacik0f9dd462008-09-23 13:14:11 -0400300 return cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400301}
302
Josef Bacik0f9dd462008-09-23 13:14:11 -0400303/*
Sankar P9f556842009-05-14 13:52:22 -0400304 * return the block group that contains the given bytenr
Josef Bacik0f9dd462008-09-23 13:14:11 -0400305 */
Chris Masond3977122009-01-05 21:25:51 -0500306struct btrfs_block_group_cache *btrfs_lookup_block_group(
307 struct btrfs_fs_info *info,
308 u64 bytenr)
Chris Masonbe744172007-05-06 10:15:01 -0400309{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400310 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -0400311
Josef Bacik0f9dd462008-09-23 13:14:11 -0400312 cache = block_group_cache_tree_search(info, bytenr, 1);
Chris Mason96b51792007-10-15 16:15:19 -0400313
Josef Bacik0f9dd462008-09-23 13:14:11 -0400314 return cache;
Chris Masonbe744172007-05-06 10:15:01 -0400315}
Chris Mason0b86a832008-03-24 15:01:56 -0400316
Chris Masonfa9c0d792009-04-03 09:47:43 -0400317void btrfs_put_block_group(struct btrfs_block_group_cache *cache)
Yan Zhengd2fb3432008-12-11 16:30:39 -0500318{
319 if (atomic_dec_and_test(&cache->count))
320 kfree(cache);
321}
322
Josef Bacik0f9dd462008-09-23 13:14:11 -0400323static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
324 u64 flags)
Chris Mason6324fbf2008-03-24 15:01:59 -0400325{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400326 struct list_head *head = &info->space_info;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400327 struct btrfs_space_info *found;
Chris Mason4184ea72009-03-10 12:39:20 -0400328
329 rcu_read_lock();
330 list_for_each_entry_rcu(found, head, list) {
331 if (found->flags == flags) {
332 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -0400333 return found;
Chris Mason4184ea72009-03-10 12:39:20 -0400334 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400335 }
Chris Mason4184ea72009-03-10 12:39:20 -0400336 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -0400337 return NULL;
Chris Mason6324fbf2008-03-24 15:01:59 -0400338}
339
Chris Mason4184ea72009-03-10 12:39:20 -0400340/*
341 * after adding space to the filesystem, we need to clear the full flags
342 * on all the space infos.
343 */
344void btrfs_clear_space_info_full(struct btrfs_fs_info *info)
345{
346 struct list_head *head = &info->space_info;
347 struct btrfs_space_info *found;
348
349 rcu_read_lock();
350 list_for_each_entry_rcu(found, head, list)
351 found->full = 0;
352 rcu_read_unlock();
353}
354
Josef Bacik80eb2342008-10-29 14:49:05 -0400355static u64 div_factor(u64 num, int factor)
356{
357 if (factor == 10)
358 return num;
359 num *= factor;
360 do_div(num, 10);
361 return num;
362}
363
Yan Zhengd2fb3432008-12-11 16:30:39 -0500364u64 btrfs_find_block_group(struct btrfs_root *root,
365 u64 search_start, u64 search_hint, int owner)
Chris Masoncd1bc462007-04-27 10:08:34 -0400366{
Chris Mason96b51792007-10-15 16:15:19 -0400367 struct btrfs_block_group_cache *cache;
Chris Masoncd1bc462007-04-27 10:08:34 -0400368 u64 used;
Yan Zhengd2fb3432008-12-11 16:30:39 -0500369 u64 last = max(search_hint, search_start);
370 u64 group_start = 0;
Chris Mason31f3c992007-04-30 15:25:45 -0400371 int full_search = 0;
Yan Zhengd2fb3432008-12-11 16:30:39 -0500372 int factor = 9;
Chris Mason0ef3e662008-05-24 14:04:53 -0400373 int wrapped = 0;
Chris Mason31f3c992007-04-30 15:25:45 -0400374again:
Zheng Yane8569812008-09-26 10:05:48 -0400375 while (1) {
376 cache = btrfs_lookup_first_block_group(root->fs_info, last);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400377 if (!cache)
Chris Masoncd1bc462007-04-27 10:08:34 -0400378 break;
Chris Mason96b51792007-10-15 16:15:19 -0400379
Chris Masonc286ac42008-07-22 23:06:41 -0400380 spin_lock(&cache->lock);
Chris Mason96b51792007-10-15 16:15:19 -0400381 last = cache->key.objectid + cache->key.offset;
382 used = btrfs_block_group_used(&cache->item);
383
Yan Zhengd2fb3432008-12-11 16:30:39 -0500384 if ((full_search || !cache->ro) &&
385 block_group_bits(cache, BTRFS_BLOCK_GROUP_METADATA)) {
Zheng Yane8569812008-09-26 10:05:48 -0400386 if (used + cache->pinned + cache->reserved <
Yan Zhengd2fb3432008-12-11 16:30:39 -0500387 div_factor(cache->key.offset, factor)) {
388 group_start = cache->key.objectid;
Chris Masonc286ac42008-07-22 23:06:41 -0400389 spin_unlock(&cache->lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -0400390 btrfs_put_block_group(cache);
Chris Mason8790d502008-04-03 16:29:03 -0400391 goto found;
392 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400393 }
Chris Masonc286ac42008-07-22 23:06:41 -0400394 spin_unlock(&cache->lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -0400395 btrfs_put_block_group(cache);
Chris Masonde428b62007-05-18 13:28:27 -0400396 cond_resched();
Chris Masoncd1bc462007-04-27 10:08:34 -0400397 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400398 if (!wrapped) {
399 last = search_start;
400 wrapped = 1;
401 goto again;
402 }
403 if (!full_search && factor < 10) {
Chris Masonbe744172007-05-06 10:15:01 -0400404 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -0400405 full_search = 1;
Chris Mason0ef3e662008-05-24 14:04:53 -0400406 factor = 10;
Chris Mason31f3c992007-04-30 15:25:45 -0400407 goto again;
408 }
Chris Masonbe744172007-05-06 10:15:01 -0400409found:
Yan Zhengd2fb3432008-12-11 16:30:39 -0500410 return group_start;
Chris Mason925baed2008-06-25 16:01:30 -0400411}
Josef Bacik0f9dd462008-09-23 13:14:11 -0400412
Chris Masone02119d2008-09-05 16:13:11 -0400413/* simple helper to search for an existing extent at a given offset */
Zheng Yan31840ae2008-09-23 13:14:14 -0400414int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len)
Chris Masone02119d2008-09-05 16:13:11 -0400415{
416 int ret;
417 struct btrfs_key key;
Zheng Yan31840ae2008-09-23 13:14:14 -0400418 struct btrfs_path *path;
Chris Masone02119d2008-09-05 16:13:11 -0400419
Zheng Yan31840ae2008-09-23 13:14:14 -0400420 path = btrfs_alloc_path();
421 BUG_ON(!path);
Chris Masone02119d2008-09-05 16:13:11 -0400422 key.objectid = start;
423 key.offset = len;
424 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
425 ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
426 0, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400427 btrfs_free_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -0500428 return ret;
429}
430
Chris Masond8d5f3e2007-12-11 12:42:00 -0500431/*
432 * Back reference rules. Back refs have three main goals:
433 *
434 * 1) differentiate between all holders of references to an extent so that
435 * when a reference is dropped we can make sure it was a valid reference
436 * before freeing the extent.
437 *
438 * 2) Provide enough information to quickly find the holders of an extent
439 * if we notice a given block is corrupted or bad.
440 *
441 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
442 * maintenance. This is actually the same as #2, but with a slightly
443 * different use case.
444 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400445 * There are two kinds of back refs. The implicit back refs is optimized
446 * for pointers in non-shared tree blocks. For a given pointer in a block,
447 * back refs of this kind provide information about the block's owner tree
448 * and the pointer's key. These information allow us to find the block by
449 * b-tree searching. The full back refs is for pointers in tree blocks not
450 * referenced by their owner trees. The location of tree block is recorded
451 * in the back refs. Actually the full back refs is generic, and can be
452 * used in all cases the implicit back refs is used. The major shortcoming
453 * of the full back refs is its overhead. Every time a tree block gets
454 * COWed, we have to update back refs entry for all pointers in it.
455 *
456 * For a newly allocated tree block, we use implicit back refs for
457 * pointers in it. This means most tree related operations only involve
458 * implicit back refs. For a tree block created in old transaction, the
459 * only way to drop a reference to it is COW it. So we can detect the
460 * event that tree block loses its owner tree's reference and do the
461 * back refs conversion.
462 *
463 * When a tree block is COW'd through a tree, there are four cases:
464 *
465 * The reference count of the block is one and the tree is the block's
466 * owner tree. Nothing to do in this case.
467 *
468 * The reference count of the block is one and the tree is not the
469 * block's owner tree. In this case, full back refs is used for pointers
470 * in the block. Remove these full back refs, add implicit back refs for
471 * every pointers in the new block.
472 *
473 * The reference count of the block is greater than one and the tree is
474 * the block's owner tree. In this case, implicit back refs is used for
475 * pointers in the block. Add full back refs for every pointers in the
476 * block, increase lower level extents' reference counts. The original
477 * implicit back refs are entailed to the new block.
478 *
479 * The reference count of the block is greater than one and the tree is
480 * not the block's owner tree. Add implicit back refs for every pointer in
481 * the new block, increase lower level extents' reference count.
482 *
483 * Back Reference Key composing:
484 *
485 * The key objectid corresponds to the first byte in the extent,
486 * The key type is used to differentiate between types of back refs.
487 * There are different meanings of the key offset for different types
488 * of back refs.
489 *
Chris Masond8d5f3e2007-12-11 12:42:00 -0500490 * File extents can be referenced by:
491 *
492 * - multiple snapshots, subvolumes, or different generations in one subvol
Zheng Yan31840ae2008-09-23 13:14:14 -0400493 * - different files inside a single subvolume
Chris Masond8d5f3e2007-12-11 12:42:00 -0500494 * - different offsets inside a file (bookend extents in file.c)
495 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400496 * The extent ref structure for the implicit back refs has fields for:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500497 *
498 * - Objectid of the subvolume root
Chris Masond8d5f3e2007-12-11 12:42:00 -0500499 * - objectid of the file holding the reference
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400500 * - original offset in the file
501 * - how many bookend extents
Zheng Yan31840ae2008-09-23 13:14:14 -0400502 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400503 * The key offset for the implicit back refs is hash of the first
504 * three fields.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500505 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400506 * The extent ref structure for the full back refs has field for:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500507 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400508 * - number of pointers in the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -0500509 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400510 * The key offset for the implicit back refs is the first byte of
511 * the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -0500512 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400513 * When a file extent is allocated, The implicit back refs is used.
514 * the fields are filled in:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500515 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400516 * (root_key.objectid, inode objectid, offset in file, 1)
517 *
518 * When a file extent is removed file truncation, we find the
519 * corresponding implicit back refs and check the following fields:
520 *
521 * (btrfs_header_owner(leaf), inode objectid, offset in file)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500522 *
523 * Btree extents can be referenced by:
524 *
525 * - Different subvolumes
Chris Masond8d5f3e2007-12-11 12:42:00 -0500526 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400527 * Both the implicit back refs and the full back refs for tree blocks
528 * only consist of key. The key offset for the implicit back refs is
529 * objectid of block's owner tree. The key offset for the full back refs
530 * is the first byte of parent block.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500531 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400532 * When implicit back refs is used, information about the lowest key and
533 * level of the tree block are required. These information are stored in
534 * tree block info structure.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500535 */
Zheng Yan31840ae2008-09-23 13:14:14 -0400536
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400537#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
538static int convert_extent_item_v0(struct btrfs_trans_handle *trans,
539 struct btrfs_root *root,
540 struct btrfs_path *path,
541 u64 owner, u32 extra_size)
Chris Mason74493f72007-12-11 09:25:06 -0500542{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400543 struct btrfs_extent_item *item;
544 struct btrfs_extent_item_v0 *ei0;
545 struct btrfs_extent_ref_v0 *ref0;
546 struct btrfs_tree_block_info *bi;
Zheng Yan31840ae2008-09-23 13:14:14 -0400547 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400548 struct btrfs_key key;
549 struct btrfs_key found_key;
550 u32 new_size = sizeof(*item);
551 u64 refs;
Chris Mason74493f72007-12-11 09:25:06 -0500552 int ret;
553
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400554 leaf = path->nodes[0];
555 BUG_ON(btrfs_item_size_nr(leaf, path->slots[0]) != sizeof(*ei0));
Chris Mason74493f72007-12-11 09:25:06 -0500556
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400557 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
558 ei0 = btrfs_item_ptr(leaf, path->slots[0],
559 struct btrfs_extent_item_v0);
560 refs = btrfs_extent_refs_v0(leaf, ei0);
561
562 if (owner == (u64)-1) {
563 while (1) {
564 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
565 ret = btrfs_next_leaf(root, path);
566 if (ret < 0)
567 return ret;
568 BUG_ON(ret > 0);
569 leaf = path->nodes[0];
570 }
571 btrfs_item_key_to_cpu(leaf, &found_key,
572 path->slots[0]);
573 BUG_ON(key.objectid != found_key.objectid);
574 if (found_key.type != BTRFS_EXTENT_REF_V0_KEY) {
575 path->slots[0]++;
576 continue;
577 }
578 ref0 = btrfs_item_ptr(leaf, path->slots[0],
579 struct btrfs_extent_ref_v0);
580 owner = btrfs_ref_objectid_v0(leaf, ref0);
581 break;
582 }
583 }
584 btrfs_release_path(root, path);
585
586 if (owner < BTRFS_FIRST_FREE_OBJECTID)
587 new_size += sizeof(*bi);
588
589 new_size -= sizeof(*ei0);
590 ret = btrfs_search_slot(trans, root, &key, path,
591 new_size + extra_size, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -0400592 if (ret < 0)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400593 return ret;
594 BUG_ON(ret);
595
596 ret = btrfs_extend_item(trans, root, path, new_size);
597 BUG_ON(ret);
598
599 leaf = path->nodes[0];
600 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
601 btrfs_set_extent_refs(leaf, item, refs);
602 /* FIXME: get real generation */
603 btrfs_set_extent_generation(leaf, item, 0);
604 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
605 btrfs_set_extent_flags(leaf, item,
606 BTRFS_EXTENT_FLAG_TREE_BLOCK |
607 BTRFS_BLOCK_FLAG_FULL_BACKREF);
608 bi = (struct btrfs_tree_block_info *)(item + 1);
609 /* FIXME: get first key of the block */
610 memset_extent_buffer(leaf, 0, (unsigned long)bi, sizeof(*bi));
611 btrfs_set_tree_block_level(leaf, bi, (int)owner);
612 } else {
613 btrfs_set_extent_flags(leaf, item, BTRFS_EXTENT_FLAG_DATA);
614 }
615 btrfs_mark_buffer_dirty(leaf);
616 return 0;
617}
618#endif
619
620static u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset)
621{
622 u32 high_crc = ~(u32)0;
623 u32 low_crc = ~(u32)0;
624 __le64 lenum;
625
626 lenum = cpu_to_le64(root_objectid);
David Woodhouse163e7832009-04-19 13:02:41 +0100627 high_crc = crc32c(high_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400628 lenum = cpu_to_le64(owner);
David Woodhouse163e7832009-04-19 13:02:41 +0100629 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400630 lenum = cpu_to_le64(offset);
David Woodhouse163e7832009-04-19 13:02:41 +0100631 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400632
633 return ((u64)high_crc << 31) ^ (u64)low_crc;
634}
635
636static u64 hash_extent_data_ref_item(struct extent_buffer *leaf,
637 struct btrfs_extent_data_ref *ref)
638{
639 return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref),
640 btrfs_extent_data_ref_objectid(leaf, ref),
641 btrfs_extent_data_ref_offset(leaf, ref));
642}
643
644static int match_extent_data_ref(struct extent_buffer *leaf,
645 struct btrfs_extent_data_ref *ref,
646 u64 root_objectid, u64 owner, u64 offset)
647{
648 if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid ||
649 btrfs_extent_data_ref_objectid(leaf, ref) != owner ||
650 btrfs_extent_data_ref_offset(leaf, ref) != offset)
651 return 0;
652 return 1;
653}
654
655static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans,
656 struct btrfs_root *root,
657 struct btrfs_path *path,
658 u64 bytenr, u64 parent,
659 u64 root_objectid,
660 u64 owner, u64 offset)
661{
662 struct btrfs_key key;
663 struct btrfs_extent_data_ref *ref;
664 struct extent_buffer *leaf;
665 u32 nritems;
666 int ret;
667 int recow;
668 int err = -ENOENT;
669
670 key.objectid = bytenr;
671 if (parent) {
672 key.type = BTRFS_SHARED_DATA_REF_KEY;
673 key.offset = parent;
674 } else {
675 key.type = BTRFS_EXTENT_DATA_REF_KEY;
676 key.offset = hash_extent_data_ref(root_objectid,
677 owner, offset);
678 }
679again:
680 recow = 0;
681 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
682 if (ret < 0) {
683 err = ret;
684 goto fail;
685 }
686
687 if (parent) {
688 if (!ret)
689 return 0;
690#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
691 key.type = BTRFS_EXTENT_REF_V0_KEY;
692 btrfs_release_path(root, path);
693 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
694 if (ret < 0) {
695 err = ret;
696 goto fail;
697 }
698 if (!ret)
699 return 0;
700#endif
701 goto fail;
Zheng Yan31840ae2008-09-23 13:14:14 -0400702 }
703
704 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400705 nritems = btrfs_header_nritems(leaf);
706 while (1) {
707 if (path->slots[0] >= nritems) {
708 ret = btrfs_next_leaf(root, path);
709 if (ret < 0)
710 err = ret;
711 if (ret)
712 goto fail;
713
714 leaf = path->nodes[0];
715 nritems = btrfs_header_nritems(leaf);
716 recow = 1;
717 }
718
719 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
720 if (key.objectid != bytenr ||
721 key.type != BTRFS_EXTENT_DATA_REF_KEY)
722 goto fail;
723
724 ref = btrfs_item_ptr(leaf, path->slots[0],
725 struct btrfs_extent_data_ref);
726
727 if (match_extent_data_ref(leaf, ref, root_objectid,
728 owner, offset)) {
729 if (recow) {
730 btrfs_release_path(root, path);
731 goto again;
732 }
733 err = 0;
734 break;
735 }
736 path->slots[0]++;
Zheng Yan31840ae2008-09-23 13:14:14 -0400737 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400738fail:
739 return err;
Zheng Yan31840ae2008-09-23 13:14:14 -0400740}
741
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400742static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans,
743 struct btrfs_root *root,
744 struct btrfs_path *path,
745 u64 bytenr, u64 parent,
746 u64 root_objectid, u64 owner,
747 u64 offset, int refs_to_add)
Zheng Yan31840ae2008-09-23 13:14:14 -0400748{
749 struct btrfs_key key;
750 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400751 u32 size;
Zheng Yan31840ae2008-09-23 13:14:14 -0400752 u32 num_refs;
753 int ret;
754
755 key.objectid = bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400756 if (parent) {
757 key.type = BTRFS_SHARED_DATA_REF_KEY;
758 key.offset = parent;
759 size = sizeof(struct btrfs_shared_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -0400760 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400761 key.type = BTRFS_EXTENT_DATA_REF_KEY;
762 key.offset = hash_extent_data_ref(root_objectid,
763 owner, offset);
764 size = sizeof(struct btrfs_extent_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -0400765 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400766
767 ret = btrfs_insert_empty_item(trans, root, path, &key, size);
768 if (ret && ret != -EEXIST)
769 goto fail;
770
771 leaf = path->nodes[0];
772 if (parent) {
773 struct btrfs_shared_data_ref *ref;
774 ref = btrfs_item_ptr(leaf, path->slots[0],
775 struct btrfs_shared_data_ref);
776 if (ret == 0) {
777 btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add);
778 } else {
779 num_refs = btrfs_shared_data_ref_count(leaf, ref);
780 num_refs += refs_to_add;
781 btrfs_set_shared_data_ref_count(leaf, ref, num_refs);
782 }
783 } else {
784 struct btrfs_extent_data_ref *ref;
785 while (ret == -EEXIST) {
786 ref = btrfs_item_ptr(leaf, path->slots[0],
787 struct btrfs_extent_data_ref);
788 if (match_extent_data_ref(leaf, ref, root_objectid,
789 owner, offset))
790 break;
791 btrfs_release_path(root, path);
792 key.offset++;
793 ret = btrfs_insert_empty_item(trans, root, path, &key,
794 size);
795 if (ret && ret != -EEXIST)
796 goto fail;
797
798 leaf = path->nodes[0];
799 }
800 ref = btrfs_item_ptr(leaf, path->slots[0],
801 struct btrfs_extent_data_ref);
802 if (ret == 0) {
803 btrfs_set_extent_data_ref_root(leaf, ref,
804 root_objectid);
805 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
806 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
807 btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add);
808 } else {
809 num_refs = btrfs_extent_data_ref_count(leaf, ref);
810 num_refs += refs_to_add;
811 btrfs_set_extent_data_ref_count(leaf, ref, num_refs);
812 }
813 }
814 btrfs_mark_buffer_dirty(leaf);
815 ret = 0;
816fail:
Chris Mason7bb86312007-12-11 09:25:06 -0500817 btrfs_release_path(root, path);
818 return ret;
Chris Mason74493f72007-12-11 09:25:06 -0500819}
820
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400821static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans,
822 struct btrfs_root *root,
823 struct btrfs_path *path,
824 int refs_to_drop)
Zheng Yan31840ae2008-09-23 13:14:14 -0400825{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400826 struct btrfs_key key;
827 struct btrfs_extent_data_ref *ref1 = NULL;
828 struct btrfs_shared_data_ref *ref2 = NULL;
Zheng Yan31840ae2008-09-23 13:14:14 -0400829 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400830 u32 num_refs = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -0400831 int ret = 0;
832
833 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400834 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
835
836 if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
837 ref1 = btrfs_item_ptr(leaf, path->slots[0],
838 struct btrfs_extent_data_ref);
839 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
840 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
841 ref2 = btrfs_item_ptr(leaf, path->slots[0],
842 struct btrfs_shared_data_ref);
843 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
844#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
845 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
846 struct btrfs_extent_ref_v0 *ref0;
847 ref0 = btrfs_item_ptr(leaf, path->slots[0],
848 struct btrfs_extent_ref_v0);
849 num_refs = btrfs_ref_count_v0(leaf, ref0);
850#endif
851 } else {
852 BUG();
853 }
854
Chris Mason56bec292009-03-13 10:10:06 -0400855 BUG_ON(num_refs < refs_to_drop);
856 num_refs -= refs_to_drop;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400857
Zheng Yan31840ae2008-09-23 13:14:14 -0400858 if (num_refs == 0) {
859 ret = btrfs_del_item(trans, root, path);
860 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400861 if (key.type == BTRFS_EXTENT_DATA_REF_KEY)
862 btrfs_set_extent_data_ref_count(leaf, ref1, num_refs);
863 else if (key.type == BTRFS_SHARED_DATA_REF_KEY)
864 btrfs_set_shared_data_ref_count(leaf, ref2, num_refs);
865#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
866 else {
867 struct btrfs_extent_ref_v0 *ref0;
868 ref0 = btrfs_item_ptr(leaf, path->slots[0],
869 struct btrfs_extent_ref_v0);
870 btrfs_set_ref_count_v0(leaf, ref0, num_refs);
871 }
872#endif
Zheng Yan31840ae2008-09-23 13:14:14 -0400873 btrfs_mark_buffer_dirty(leaf);
874 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400875 return ret;
876}
877
878static noinline u32 extent_data_ref_count(struct btrfs_root *root,
879 struct btrfs_path *path,
880 struct btrfs_extent_inline_ref *iref)
881{
882 struct btrfs_key key;
883 struct extent_buffer *leaf;
884 struct btrfs_extent_data_ref *ref1;
885 struct btrfs_shared_data_ref *ref2;
886 u32 num_refs = 0;
887
888 leaf = path->nodes[0];
889 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
890 if (iref) {
891 if (btrfs_extent_inline_ref_type(leaf, iref) ==
892 BTRFS_EXTENT_DATA_REF_KEY) {
893 ref1 = (struct btrfs_extent_data_ref *)(&iref->offset);
894 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
895 } else {
896 ref2 = (struct btrfs_shared_data_ref *)(iref + 1);
897 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
898 }
899 } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
900 ref1 = btrfs_item_ptr(leaf, path->slots[0],
901 struct btrfs_extent_data_ref);
902 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
903 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
904 ref2 = btrfs_item_ptr(leaf, path->slots[0],
905 struct btrfs_shared_data_ref);
906 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
907#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
908 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
909 struct btrfs_extent_ref_v0 *ref0;
910 ref0 = btrfs_item_ptr(leaf, path->slots[0],
911 struct btrfs_extent_ref_v0);
912 num_refs = btrfs_ref_count_v0(leaf, ref0);
913#endif
914 } else {
915 WARN_ON(1);
916 }
917 return num_refs;
918}
919
920static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans,
921 struct btrfs_root *root,
922 struct btrfs_path *path,
923 u64 bytenr, u64 parent,
924 u64 root_objectid)
925{
926 struct btrfs_key key;
927 int ret;
928
929 key.objectid = bytenr;
930 if (parent) {
931 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
932 key.offset = parent;
933 } else {
934 key.type = BTRFS_TREE_BLOCK_REF_KEY;
935 key.offset = root_objectid;
936 }
937
938 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
939 if (ret > 0)
940 ret = -ENOENT;
941#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
942 if (ret == -ENOENT && parent) {
943 btrfs_release_path(root, path);
944 key.type = BTRFS_EXTENT_REF_V0_KEY;
945 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
946 if (ret > 0)
947 ret = -ENOENT;
948 }
949#endif
950 return ret;
951}
952
953static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans,
954 struct btrfs_root *root,
955 struct btrfs_path *path,
956 u64 bytenr, u64 parent,
957 u64 root_objectid)
958{
959 struct btrfs_key key;
960 int ret;
961
962 key.objectid = bytenr;
963 if (parent) {
964 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
965 key.offset = parent;
966 } else {
967 key.type = BTRFS_TREE_BLOCK_REF_KEY;
968 key.offset = root_objectid;
969 }
970
971 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400972 btrfs_release_path(root, path);
973 return ret;
974}
975
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400976static inline int extent_ref_type(u64 parent, u64 owner)
977{
978 int type;
979 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
980 if (parent > 0)
981 type = BTRFS_SHARED_BLOCK_REF_KEY;
982 else
983 type = BTRFS_TREE_BLOCK_REF_KEY;
984 } else {
985 if (parent > 0)
986 type = BTRFS_SHARED_DATA_REF_KEY;
987 else
988 type = BTRFS_EXTENT_DATA_REF_KEY;
989 }
990 return type;
991}
992
Yan Zheng2c47e6052009-06-27 21:07:35 -0400993static int find_next_key(struct btrfs_path *path, int level,
994 struct btrfs_key *key)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400995
996{
Yan Zheng2c47e6052009-06-27 21:07:35 -0400997 for (; level < BTRFS_MAX_LEVEL; level++) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400998 if (!path->nodes[level])
999 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001000 if (path->slots[level] + 1 >=
1001 btrfs_header_nritems(path->nodes[level]))
1002 continue;
1003 if (level == 0)
1004 btrfs_item_key_to_cpu(path->nodes[level], key,
1005 path->slots[level] + 1);
1006 else
1007 btrfs_node_key_to_cpu(path->nodes[level], key,
1008 path->slots[level] + 1);
1009 return 0;
1010 }
1011 return 1;
1012}
1013
1014/*
1015 * look for inline back ref. if back ref is found, *ref_ret is set
1016 * to the address of inline back ref, and 0 is returned.
1017 *
1018 * if back ref isn't found, *ref_ret is set to the address where it
1019 * should be inserted, and -ENOENT is returned.
1020 *
1021 * if insert is true and there are too many inline back refs, the path
1022 * points to the extent item, and -EAGAIN is returned.
1023 *
1024 * NOTE: inline back refs are ordered in the same way that back ref
1025 * items in the tree are ordered.
1026 */
1027static noinline_for_stack
1028int lookup_inline_extent_backref(struct btrfs_trans_handle *trans,
1029 struct btrfs_root *root,
1030 struct btrfs_path *path,
1031 struct btrfs_extent_inline_ref **ref_ret,
1032 u64 bytenr, u64 num_bytes,
1033 u64 parent, u64 root_objectid,
1034 u64 owner, u64 offset, int insert)
1035{
1036 struct btrfs_key key;
1037 struct extent_buffer *leaf;
1038 struct btrfs_extent_item *ei;
1039 struct btrfs_extent_inline_ref *iref;
1040 u64 flags;
1041 u64 item_size;
1042 unsigned long ptr;
1043 unsigned long end;
1044 int extra_size;
1045 int type;
1046 int want;
1047 int ret;
1048 int err = 0;
1049
1050 key.objectid = bytenr;
1051 key.type = BTRFS_EXTENT_ITEM_KEY;
1052 key.offset = num_bytes;
1053
1054 want = extent_ref_type(parent, owner);
1055 if (insert) {
1056 extra_size = btrfs_extent_inline_ref_size(want);
Yan Zheng85d41982009-06-11 08:51:10 -04001057 path->keep_locks = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001058 } else
1059 extra_size = -1;
1060 ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1);
1061 if (ret < 0) {
1062 err = ret;
1063 goto out;
1064 }
1065 BUG_ON(ret);
1066
1067 leaf = path->nodes[0];
1068 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1069#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1070 if (item_size < sizeof(*ei)) {
1071 if (!insert) {
1072 err = -ENOENT;
1073 goto out;
1074 }
1075 ret = convert_extent_item_v0(trans, root, path, owner,
1076 extra_size);
1077 if (ret < 0) {
1078 err = ret;
1079 goto out;
1080 }
1081 leaf = path->nodes[0];
1082 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1083 }
1084#endif
1085 BUG_ON(item_size < sizeof(*ei));
1086
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001087 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1088 flags = btrfs_extent_flags(leaf, ei);
1089
1090 ptr = (unsigned long)(ei + 1);
1091 end = (unsigned long)ei + item_size;
1092
1093 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
1094 ptr += sizeof(struct btrfs_tree_block_info);
1095 BUG_ON(ptr > end);
1096 } else {
1097 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_DATA));
1098 }
1099
1100 err = -ENOENT;
1101 while (1) {
1102 if (ptr >= end) {
1103 WARN_ON(ptr > end);
1104 break;
1105 }
1106 iref = (struct btrfs_extent_inline_ref *)ptr;
1107 type = btrfs_extent_inline_ref_type(leaf, iref);
1108 if (want < type)
1109 break;
1110 if (want > type) {
1111 ptr += btrfs_extent_inline_ref_size(type);
1112 continue;
1113 }
1114
1115 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1116 struct btrfs_extent_data_ref *dref;
1117 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1118 if (match_extent_data_ref(leaf, dref, root_objectid,
1119 owner, offset)) {
1120 err = 0;
1121 break;
1122 }
1123 if (hash_extent_data_ref_item(leaf, dref) <
1124 hash_extent_data_ref(root_objectid, owner, offset))
1125 break;
1126 } else {
1127 u64 ref_offset;
1128 ref_offset = btrfs_extent_inline_ref_offset(leaf, iref);
1129 if (parent > 0) {
1130 if (parent == ref_offset) {
1131 err = 0;
1132 break;
1133 }
1134 if (ref_offset < parent)
1135 break;
1136 } else {
1137 if (root_objectid == ref_offset) {
1138 err = 0;
1139 break;
1140 }
1141 if (ref_offset < root_objectid)
1142 break;
1143 }
1144 }
1145 ptr += btrfs_extent_inline_ref_size(type);
1146 }
1147 if (err == -ENOENT && insert) {
1148 if (item_size + extra_size >=
1149 BTRFS_MAX_EXTENT_ITEM_SIZE(root)) {
1150 err = -EAGAIN;
1151 goto out;
1152 }
1153 /*
1154 * To add new inline back ref, we have to make sure
1155 * there is no corresponding back ref item.
1156 * For simplicity, we just do not add new inline back
1157 * ref if there is any kind of item for this block
1158 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04001159 if (find_next_key(path, 0, &key) == 0 &&
1160 key.objectid == bytenr &&
Yan Zheng85d41982009-06-11 08:51:10 -04001161 key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001162 err = -EAGAIN;
1163 goto out;
1164 }
1165 }
1166 *ref_ret = (struct btrfs_extent_inline_ref *)ptr;
1167out:
Yan Zheng85d41982009-06-11 08:51:10 -04001168 if (insert) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001169 path->keep_locks = 0;
1170 btrfs_unlock_up_safe(path, 1);
1171 }
1172 return err;
1173}
1174
1175/*
1176 * helper to add new inline back ref
1177 */
1178static noinline_for_stack
1179int setup_inline_extent_backref(struct btrfs_trans_handle *trans,
1180 struct btrfs_root *root,
1181 struct btrfs_path *path,
1182 struct btrfs_extent_inline_ref *iref,
1183 u64 parent, u64 root_objectid,
1184 u64 owner, u64 offset, int refs_to_add,
1185 struct btrfs_delayed_extent_op *extent_op)
1186{
1187 struct extent_buffer *leaf;
1188 struct btrfs_extent_item *ei;
1189 unsigned long ptr;
1190 unsigned long end;
1191 unsigned long item_offset;
1192 u64 refs;
1193 int size;
1194 int type;
1195 int ret;
1196
1197 leaf = path->nodes[0];
1198 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1199 item_offset = (unsigned long)iref - (unsigned long)ei;
1200
1201 type = extent_ref_type(parent, owner);
1202 size = btrfs_extent_inline_ref_size(type);
1203
1204 ret = btrfs_extend_item(trans, root, path, size);
1205 BUG_ON(ret);
1206
1207 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1208 refs = btrfs_extent_refs(leaf, ei);
1209 refs += refs_to_add;
1210 btrfs_set_extent_refs(leaf, ei, refs);
1211 if (extent_op)
1212 __run_delayed_extent_op(extent_op, leaf, ei);
1213
1214 ptr = (unsigned long)ei + item_offset;
1215 end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]);
1216 if (ptr < end - size)
1217 memmove_extent_buffer(leaf, ptr + size, ptr,
1218 end - size - ptr);
1219
1220 iref = (struct btrfs_extent_inline_ref *)ptr;
1221 btrfs_set_extent_inline_ref_type(leaf, iref, type);
1222 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1223 struct btrfs_extent_data_ref *dref;
1224 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1225 btrfs_set_extent_data_ref_root(leaf, dref, root_objectid);
1226 btrfs_set_extent_data_ref_objectid(leaf, dref, owner);
1227 btrfs_set_extent_data_ref_offset(leaf, dref, offset);
1228 btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add);
1229 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1230 struct btrfs_shared_data_ref *sref;
1231 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1232 btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add);
1233 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1234 } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
1235 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1236 } else {
1237 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
1238 }
1239 btrfs_mark_buffer_dirty(leaf);
1240 return 0;
1241}
1242
1243static int lookup_extent_backref(struct btrfs_trans_handle *trans,
1244 struct btrfs_root *root,
1245 struct btrfs_path *path,
1246 struct btrfs_extent_inline_ref **ref_ret,
1247 u64 bytenr, u64 num_bytes, u64 parent,
1248 u64 root_objectid, u64 owner, u64 offset)
1249{
1250 int ret;
1251
1252 ret = lookup_inline_extent_backref(trans, root, path, ref_ret,
1253 bytenr, num_bytes, parent,
1254 root_objectid, owner, offset, 0);
1255 if (ret != -ENOENT)
1256 return ret;
1257
1258 btrfs_release_path(root, path);
1259 *ref_ret = NULL;
1260
1261 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1262 ret = lookup_tree_block_ref(trans, root, path, bytenr, parent,
1263 root_objectid);
1264 } else {
1265 ret = lookup_extent_data_ref(trans, root, path, bytenr, parent,
1266 root_objectid, owner, offset);
1267 }
1268 return ret;
1269}
1270
1271/*
1272 * helper to update/remove inline back ref
1273 */
1274static noinline_for_stack
1275int update_inline_extent_backref(struct btrfs_trans_handle *trans,
1276 struct btrfs_root *root,
1277 struct btrfs_path *path,
1278 struct btrfs_extent_inline_ref *iref,
1279 int refs_to_mod,
1280 struct btrfs_delayed_extent_op *extent_op)
1281{
1282 struct extent_buffer *leaf;
1283 struct btrfs_extent_item *ei;
1284 struct btrfs_extent_data_ref *dref = NULL;
1285 struct btrfs_shared_data_ref *sref = NULL;
1286 unsigned long ptr;
1287 unsigned long end;
1288 u32 item_size;
1289 int size;
1290 int type;
1291 int ret;
1292 u64 refs;
1293
1294 leaf = path->nodes[0];
1295 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1296 refs = btrfs_extent_refs(leaf, ei);
1297 WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0);
1298 refs += refs_to_mod;
1299 btrfs_set_extent_refs(leaf, ei, refs);
1300 if (extent_op)
1301 __run_delayed_extent_op(extent_op, leaf, ei);
1302
1303 type = btrfs_extent_inline_ref_type(leaf, iref);
1304
1305 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1306 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1307 refs = btrfs_extent_data_ref_count(leaf, dref);
1308 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1309 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1310 refs = btrfs_shared_data_ref_count(leaf, sref);
1311 } else {
1312 refs = 1;
1313 BUG_ON(refs_to_mod != -1);
1314 }
1315
1316 BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod);
1317 refs += refs_to_mod;
1318
1319 if (refs > 0) {
1320 if (type == BTRFS_EXTENT_DATA_REF_KEY)
1321 btrfs_set_extent_data_ref_count(leaf, dref, refs);
1322 else
1323 btrfs_set_shared_data_ref_count(leaf, sref, refs);
1324 } else {
1325 size = btrfs_extent_inline_ref_size(type);
1326 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1327 ptr = (unsigned long)iref;
1328 end = (unsigned long)ei + item_size;
1329 if (ptr + size < end)
1330 memmove_extent_buffer(leaf, ptr, ptr + size,
1331 end - ptr - size);
1332 item_size -= size;
1333 ret = btrfs_truncate_item(trans, root, path, item_size, 1);
1334 BUG_ON(ret);
1335 }
1336 btrfs_mark_buffer_dirty(leaf);
1337 return 0;
1338}
1339
1340static noinline_for_stack
1341int insert_inline_extent_backref(struct btrfs_trans_handle *trans,
1342 struct btrfs_root *root,
1343 struct btrfs_path *path,
1344 u64 bytenr, u64 num_bytes, u64 parent,
1345 u64 root_objectid, u64 owner,
1346 u64 offset, int refs_to_add,
1347 struct btrfs_delayed_extent_op *extent_op)
1348{
1349 struct btrfs_extent_inline_ref *iref;
1350 int ret;
1351
1352 ret = lookup_inline_extent_backref(trans, root, path, &iref,
1353 bytenr, num_bytes, parent,
1354 root_objectid, owner, offset, 1);
1355 if (ret == 0) {
1356 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID);
1357 ret = update_inline_extent_backref(trans, root, path, iref,
1358 refs_to_add, extent_op);
1359 } else if (ret == -ENOENT) {
1360 ret = setup_inline_extent_backref(trans, root, path, iref,
1361 parent, root_objectid,
1362 owner, offset, refs_to_add,
1363 extent_op);
1364 }
1365 return ret;
1366}
1367
1368static int insert_extent_backref(struct btrfs_trans_handle *trans,
1369 struct btrfs_root *root,
1370 struct btrfs_path *path,
1371 u64 bytenr, u64 parent, u64 root_objectid,
1372 u64 owner, u64 offset, int refs_to_add)
1373{
1374 int ret;
1375 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1376 BUG_ON(refs_to_add != 1);
1377 ret = insert_tree_block_ref(trans, root, path, bytenr,
1378 parent, root_objectid);
1379 } else {
1380 ret = insert_extent_data_ref(trans, root, path, bytenr,
1381 parent, root_objectid,
1382 owner, offset, refs_to_add);
1383 }
1384 return ret;
1385}
1386
1387static int remove_extent_backref(struct btrfs_trans_handle *trans,
1388 struct btrfs_root *root,
1389 struct btrfs_path *path,
1390 struct btrfs_extent_inline_ref *iref,
1391 int refs_to_drop, int is_data)
1392{
1393 int ret;
1394
1395 BUG_ON(!is_data && refs_to_drop != 1);
1396 if (iref) {
1397 ret = update_inline_extent_backref(trans, root, path, iref,
1398 -refs_to_drop, NULL);
1399 } else if (is_data) {
1400 ret = remove_extent_data_ref(trans, root, path, refs_to_drop);
1401 } else {
1402 ret = btrfs_del_item(trans, root, path);
1403 }
1404 return ret;
1405}
1406
Chris Mason4b4e25f2008-11-20 10:22:27 -05001407#ifdef BIO_RW_DISCARD
Chris Mason15916de2008-11-19 21:17:22 -05001408static void btrfs_issue_discard(struct block_device *bdev,
1409 u64 start, u64 len)
1410{
Chris Mason15916de2008-11-19 21:17:22 -05001411 blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_KERNEL);
Chris Mason15916de2008-11-19 21:17:22 -05001412}
Chris Mason4b4e25f2008-11-20 10:22:27 -05001413#endif
Chris Mason15916de2008-11-19 21:17:22 -05001414
Liu Hui1f3c79a2009-01-05 15:57:51 -05001415static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr,
1416 u64 num_bytes)
1417{
1418#ifdef BIO_RW_DISCARD
1419 int ret;
1420 u64 map_length = num_bytes;
1421 struct btrfs_multi_bio *multi = NULL;
1422
1423 /* Tell the block device(s) that the sectors can be discarded */
1424 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
1425 bytenr, &map_length, &multi, 0);
1426 if (!ret) {
1427 struct btrfs_bio_stripe *stripe = multi->stripes;
1428 int i;
1429
1430 if (map_length > num_bytes)
1431 map_length = num_bytes;
1432
1433 for (i = 0; i < multi->num_stripes; i++, stripe++) {
1434 btrfs_issue_discard(stripe->dev->bdev,
1435 stripe->physical,
1436 map_length);
1437 }
1438 kfree(multi);
1439 }
1440
1441 return ret;
1442#else
1443 return 0;
1444#endif
1445}
1446
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001447int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1448 struct btrfs_root *root,
1449 u64 bytenr, u64 num_bytes, u64 parent,
1450 u64 root_objectid, u64 owner, u64 offset)
Zheng Yan31840ae2008-09-23 13:14:14 -04001451{
1452 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001453 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID &&
1454 root_objectid == BTRFS_TREE_LOG_OBJECTID);
Zheng Yan31840ae2008-09-23 13:14:14 -04001455
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001456 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1457 ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes,
1458 parent, root_objectid, (int)owner,
1459 BTRFS_ADD_DELAYED_REF, NULL);
1460 } else {
1461 ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes,
1462 parent, root_objectid, owner, offset,
1463 BTRFS_ADD_DELAYED_REF, NULL);
1464 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001465 return ret;
1466}
1467
Chris Mason925baed2008-06-25 16:01:30 -04001468static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001469 struct btrfs_root *root,
1470 u64 bytenr, u64 num_bytes,
1471 u64 parent, u64 root_objectid,
1472 u64 owner, u64 offset, int refs_to_add,
1473 struct btrfs_delayed_extent_op *extent_op)
Chris Mason56bec292009-03-13 10:10:06 -04001474{
Chris Mason5caf2a02007-04-02 11:20:42 -04001475 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001476 struct extent_buffer *leaf;
Chris Mason234b63a2007-03-13 10:46:10 -04001477 struct btrfs_extent_item *item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001478 u64 refs;
1479 int ret;
1480 int err = 0;
Chris Mason037e6392007-03-07 11:50:24 -05001481
Chris Mason5caf2a02007-04-02 11:20:42 -04001482 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04001483 if (!path)
1484 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -04001485
Chris Mason3c12ac72008-04-21 12:01:38 -04001486 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04001487 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001488 /* this will setup the path even if it fails to insert the back ref */
1489 ret = insert_inline_extent_backref(trans, root->fs_info->extent_root,
1490 path, bytenr, num_bytes, parent,
1491 root_objectid, owner, offset,
1492 refs_to_add, extent_op);
1493 if (ret == 0)
1494 goto out;
Zheng Yan31840ae2008-09-23 13:14:14 -04001495
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001496 if (ret != -EAGAIN) {
1497 err = ret;
1498 goto out;
Chris Masonb9473432009-03-13 11:00:37 -04001499 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001500
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001501 leaf = path->nodes[0];
1502 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1503 refs = btrfs_extent_refs(leaf, item);
1504 btrfs_set_extent_refs(leaf, item, refs + refs_to_add);
1505 if (extent_op)
1506 __run_delayed_extent_op(extent_op, leaf, item);
Zheng Yan31840ae2008-09-23 13:14:14 -04001507
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001508 btrfs_mark_buffer_dirty(leaf);
Chris Mason5caf2a02007-04-02 11:20:42 -04001509 btrfs_release_path(root->fs_info->extent_root, path);
Chris Mason7bb86312007-12-11 09:25:06 -05001510
Chris Mason3c12ac72008-04-21 12:01:38 -04001511 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04001512 path->leave_spinning = 1;
1513
Chris Mason56bec292009-03-13 10:10:06 -04001514 /* now insert the actual backref */
Zheng Yan31840ae2008-09-23 13:14:14 -04001515 ret = insert_extent_backref(trans, root->fs_info->extent_root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001516 path, bytenr, parent, root_objectid,
1517 owner, offset, refs_to_add);
Chris Mason7bb86312007-12-11 09:25:06 -05001518 BUG_ON(ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001519out:
Chris Mason74493f72007-12-11 09:25:06 -05001520 btrfs_free_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001521 return err;
Chris Mason02217ed2007-03-02 16:08:05 -05001522}
1523
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001524static int run_delayed_data_ref(struct btrfs_trans_handle *trans,
1525 struct btrfs_root *root,
1526 struct btrfs_delayed_ref_node *node,
1527 struct btrfs_delayed_extent_op *extent_op,
1528 int insert_reserved)
Chris Masone9d0b132007-08-10 14:06:19 -04001529{
Chris Mason56bec292009-03-13 10:10:06 -04001530 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001531 struct btrfs_delayed_data_ref *ref;
1532 struct btrfs_key ins;
1533 u64 parent = 0;
1534 u64 ref_root = 0;
1535 u64 flags = 0;
Chris Mason56bec292009-03-13 10:10:06 -04001536
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001537 ins.objectid = node->bytenr;
1538 ins.offset = node->num_bytes;
1539 ins.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason56bec292009-03-13 10:10:06 -04001540
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001541 ref = btrfs_delayed_node_to_data_ref(node);
1542 if (node->type == BTRFS_SHARED_DATA_REF_KEY)
1543 parent = ref->parent;
1544 else
1545 ref_root = ref->root;
1546
1547 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
1548 if (extent_op) {
1549 BUG_ON(extent_op->update_key);
1550 flags |= extent_op->flags_to_set;
1551 }
1552 ret = alloc_reserved_file_extent(trans, root,
1553 parent, ref_root, flags,
1554 ref->objectid, ref->offset,
1555 &ins, node->ref_mod);
1556 update_reserved_extents(root, ins.objectid, ins.offset, 0);
1557 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
1558 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
1559 node->num_bytes, parent,
1560 ref_root, ref->objectid,
1561 ref->offset, node->ref_mod,
1562 extent_op);
1563 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
1564 ret = __btrfs_free_extent(trans, root, node->bytenr,
1565 node->num_bytes, parent,
1566 ref_root, ref->objectid,
1567 ref->offset, node->ref_mod,
1568 extent_op);
1569 } else {
1570 BUG();
1571 }
Chris Mason56bec292009-03-13 10:10:06 -04001572 return ret;
1573}
1574
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001575static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
1576 struct extent_buffer *leaf,
1577 struct btrfs_extent_item *ei)
1578{
1579 u64 flags = btrfs_extent_flags(leaf, ei);
1580 if (extent_op->update_flags) {
1581 flags |= extent_op->flags_to_set;
1582 btrfs_set_extent_flags(leaf, ei, flags);
1583 }
1584
1585 if (extent_op->update_key) {
1586 struct btrfs_tree_block_info *bi;
1587 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK));
1588 bi = (struct btrfs_tree_block_info *)(ei + 1);
1589 btrfs_set_tree_block_key(leaf, bi, &extent_op->key);
1590 }
1591}
1592
1593static int run_delayed_extent_op(struct btrfs_trans_handle *trans,
1594 struct btrfs_root *root,
1595 struct btrfs_delayed_ref_node *node,
1596 struct btrfs_delayed_extent_op *extent_op)
1597{
1598 struct btrfs_key key;
1599 struct btrfs_path *path;
1600 struct btrfs_extent_item *ei;
1601 struct extent_buffer *leaf;
1602 u32 item_size;
1603 int ret;
1604 int err = 0;
1605
1606 path = btrfs_alloc_path();
1607 if (!path)
1608 return -ENOMEM;
1609
1610 key.objectid = node->bytenr;
1611 key.type = BTRFS_EXTENT_ITEM_KEY;
1612 key.offset = node->num_bytes;
1613
1614 path->reada = 1;
1615 path->leave_spinning = 1;
1616 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key,
1617 path, 0, 1);
1618 if (ret < 0) {
1619 err = ret;
1620 goto out;
1621 }
1622 if (ret > 0) {
1623 err = -EIO;
1624 goto out;
1625 }
1626
1627 leaf = path->nodes[0];
1628 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1629#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1630 if (item_size < sizeof(*ei)) {
1631 ret = convert_extent_item_v0(trans, root->fs_info->extent_root,
1632 path, (u64)-1, 0);
1633 if (ret < 0) {
1634 err = ret;
1635 goto out;
1636 }
1637 leaf = path->nodes[0];
1638 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1639 }
1640#endif
1641 BUG_ON(item_size < sizeof(*ei));
1642 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1643 __run_delayed_extent_op(extent_op, leaf, ei);
1644
1645 btrfs_mark_buffer_dirty(leaf);
1646out:
1647 btrfs_free_path(path);
1648 return err;
1649}
1650
1651static int run_delayed_tree_ref(struct btrfs_trans_handle *trans,
1652 struct btrfs_root *root,
1653 struct btrfs_delayed_ref_node *node,
1654 struct btrfs_delayed_extent_op *extent_op,
1655 int insert_reserved)
1656{
1657 int ret = 0;
1658 struct btrfs_delayed_tree_ref *ref;
1659 struct btrfs_key ins;
1660 u64 parent = 0;
1661 u64 ref_root = 0;
1662
1663 ins.objectid = node->bytenr;
1664 ins.offset = node->num_bytes;
1665 ins.type = BTRFS_EXTENT_ITEM_KEY;
1666
1667 ref = btrfs_delayed_node_to_tree_ref(node);
1668 if (node->type == BTRFS_SHARED_BLOCK_REF_KEY)
1669 parent = ref->parent;
1670 else
1671 ref_root = ref->root;
1672
1673 BUG_ON(node->ref_mod != 1);
1674 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
1675 BUG_ON(!extent_op || !extent_op->update_flags ||
1676 !extent_op->update_key);
1677 ret = alloc_reserved_tree_block(trans, root,
1678 parent, ref_root,
1679 extent_op->flags_to_set,
1680 &extent_op->key,
1681 ref->level, &ins);
1682 update_reserved_extents(root, ins.objectid, ins.offset, 0);
1683 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
1684 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
1685 node->num_bytes, parent, ref_root,
1686 ref->level, 0, 1, extent_op);
1687 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
1688 ret = __btrfs_free_extent(trans, root, node->bytenr,
1689 node->num_bytes, parent, ref_root,
1690 ref->level, 0, 1, extent_op);
1691 } else {
1692 BUG();
1693 }
1694 return ret;
1695}
1696
1697
Chris Mason56bec292009-03-13 10:10:06 -04001698/* helper function to actually process a single delayed ref entry */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001699static int run_one_delayed_ref(struct btrfs_trans_handle *trans,
1700 struct btrfs_root *root,
1701 struct btrfs_delayed_ref_node *node,
1702 struct btrfs_delayed_extent_op *extent_op,
1703 int insert_reserved)
Chris Mason56bec292009-03-13 10:10:06 -04001704{
Josef Bacikeb099672009-02-12 09:27:38 -05001705 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001706 if (btrfs_delayed_ref_is_head(node)) {
Chris Mason56bec292009-03-13 10:10:06 -04001707 struct btrfs_delayed_ref_head *head;
1708 /*
1709 * we've hit the end of the chain and we were supposed
1710 * to insert this extent into the tree. But, it got
1711 * deleted before we ever needed to insert it, so all
1712 * we have to do is clean up the accounting
1713 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001714 BUG_ON(extent_op);
1715 head = btrfs_delayed_node_to_head(node);
Chris Mason56bec292009-03-13 10:10:06 -04001716 if (insert_reserved) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001717 if (head->is_data) {
1718 ret = btrfs_del_csums(trans, root,
1719 node->bytenr,
1720 node->num_bytes);
1721 BUG_ON(ret);
1722 }
1723 btrfs_update_pinned_extents(root, node->bytenr,
1724 node->num_bytes, 1);
Chris Mason56bec292009-03-13 10:10:06 -04001725 update_reserved_extents(root, node->bytenr,
1726 node->num_bytes, 0);
1727 }
Chris Mason56bec292009-03-13 10:10:06 -04001728 mutex_unlock(&head->mutex);
1729 return 0;
1730 }
Josef Bacikeb099672009-02-12 09:27:38 -05001731
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001732 if (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
1733 node->type == BTRFS_SHARED_BLOCK_REF_KEY)
1734 ret = run_delayed_tree_ref(trans, root, node, extent_op,
1735 insert_reserved);
1736 else if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
1737 node->type == BTRFS_SHARED_DATA_REF_KEY)
1738 ret = run_delayed_data_ref(trans, root, node, extent_op,
1739 insert_reserved);
1740 else
1741 BUG();
1742 return ret;
Chris Masone9d0b132007-08-10 14:06:19 -04001743}
1744
Chris Mason56bec292009-03-13 10:10:06 -04001745static noinline struct btrfs_delayed_ref_node *
1746select_delayed_ref(struct btrfs_delayed_ref_head *head)
Chris Masona28ec192007-03-06 20:08:01 -05001747{
Chris Mason56bec292009-03-13 10:10:06 -04001748 struct rb_node *node;
1749 struct btrfs_delayed_ref_node *ref;
1750 int action = BTRFS_ADD_DELAYED_REF;
1751again:
1752 /*
1753 * select delayed ref of type BTRFS_ADD_DELAYED_REF first.
1754 * this prevents ref count from going down to zero when
1755 * there still are pending delayed ref.
1756 */
1757 node = rb_prev(&head->node.rb_node);
1758 while (1) {
1759 if (!node)
1760 break;
1761 ref = rb_entry(node, struct btrfs_delayed_ref_node,
1762 rb_node);
1763 if (ref->bytenr != head->node.bytenr)
1764 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001765 if (ref->action == action)
Chris Mason56bec292009-03-13 10:10:06 -04001766 return ref;
1767 node = rb_prev(node);
Chris Mason5f39d392007-10-15 16:14:19 -04001768 }
Chris Mason56bec292009-03-13 10:10:06 -04001769 if (action == BTRFS_ADD_DELAYED_REF) {
1770 action = BTRFS_DROP_DELAYED_REF;
1771 goto again;
1772 }
1773 return NULL;
1774}
1775
Chris Masonc3e69d52009-03-13 10:17:05 -04001776static noinline int run_clustered_refs(struct btrfs_trans_handle *trans,
1777 struct btrfs_root *root,
1778 struct list_head *cluster)
Chris Mason56bec292009-03-13 10:10:06 -04001779{
Chris Mason56bec292009-03-13 10:10:06 -04001780 struct btrfs_delayed_ref_root *delayed_refs;
1781 struct btrfs_delayed_ref_node *ref;
1782 struct btrfs_delayed_ref_head *locked_ref = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001783 struct btrfs_delayed_extent_op *extent_op;
Chris Mason56bec292009-03-13 10:10:06 -04001784 int ret;
Chris Masonc3e69d52009-03-13 10:17:05 -04001785 int count = 0;
Chris Mason56bec292009-03-13 10:10:06 -04001786 int must_insert_reserved = 0;
Chris Mason56bec292009-03-13 10:10:06 -04001787
1788 delayed_refs = &trans->transaction->delayed_refs;
Chris Mason56bec292009-03-13 10:10:06 -04001789 while (1) {
1790 if (!locked_ref) {
Chris Masonc3e69d52009-03-13 10:17:05 -04001791 /* pick a new head ref from the cluster list */
1792 if (list_empty(cluster))
Chris Mason56bec292009-03-13 10:10:06 -04001793 break;
Chris Mason56bec292009-03-13 10:10:06 -04001794
Chris Masonc3e69d52009-03-13 10:17:05 -04001795 locked_ref = list_entry(cluster->next,
1796 struct btrfs_delayed_ref_head, cluster);
1797
1798 /* grab the lock that says we are going to process
1799 * all the refs for this head */
1800 ret = btrfs_delayed_ref_lock(trans, locked_ref);
1801
1802 /*
1803 * we may have dropped the spin lock to get the head
1804 * mutex lock, and that might have given someone else
1805 * time to free the head. If that's true, it has been
1806 * removed from our list and we can move on.
1807 */
1808 if (ret == -EAGAIN) {
1809 locked_ref = NULL;
1810 count++;
1811 continue;
Chris Mason56bec292009-03-13 10:10:06 -04001812 }
1813 }
1814
1815 /*
1816 * record the must insert reserved flag before we
1817 * drop the spin lock.
1818 */
1819 must_insert_reserved = locked_ref->must_insert_reserved;
1820 locked_ref->must_insert_reserved = 0;
1821
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001822 extent_op = locked_ref->extent_op;
1823 locked_ref->extent_op = NULL;
1824
Chris Mason56bec292009-03-13 10:10:06 -04001825 /*
1826 * locked_ref is the head node, so we have to go one
1827 * node back for any delayed ref updates
1828 */
Chris Mason56bec292009-03-13 10:10:06 -04001829 ref = select_delayed_ref(locked_ref);
1830 if (!ref) {
1831 /* All delayed refs have been processed, Go ahead
1832 * and send the head node to run_one_delayed_ref,
1833 * so that any accounting fixes can happen
1834 */
1835 ref = &locked_ref->node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001836
1837 if (extent_op && must_insert_reserved) {
1838 kfree(extent_op);
1839 extent_op = NULL;
1840 }
1841
1842 if (extent_op) {
1843 spin_unlock(&delayed_refs->lock);
1844
1845 ret = run_delayed_extent_op(trans, root,
1846 ref, extent_op);
1847 BUG_ON(ret);
1848 kfree(extent_op);
1849
1850 cond_resched();
1851 spin_lock(&delayed_refs->lock);
1852 continue;
1853 }
1854
Chris Masonc3e69d52009-03-13 10:17:05 -04001855 list_del_init(&locked_ref->cluster);
Chris Mason56bec292009-03-13 10:10:06 -04001856 locked_ref = NULL;
1857 }
1858
1859 ref->in_tree = 0;
1860 rb_erase(&ref->rb_node, &delayed_refs->root);
1861 delayed_refs->num_entries--;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001862
Chris Mason56bec292009-03-13 10:10:06 -04001863 spin_unlock(&delayed_refs->lock);
1864
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001865 ret = run_one_delayed_ref(trans, root, ref, extent_op,
Chris Mason56bec292009-03-13 10:10:06 -04001866 must_insert_reserved);
1867 BUG_ON(ret);
Chris Mason56bec292009-03-13 10:10:06 -04001868
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001869 btrfs_put_delayed_ref(ref);
1870 kfree(extent_op);
Chris Masonc3e69d52009-03-13 10:17:05 -04001871 count++;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001872
Chris Mason1887be62009-03-13 10:11:24 -04001873 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04001874 spin_lock(&delayed_refs->lock);
1875 }
Chris Masonc3e69d52009-03-13 10:17:05 -04001876 return count;
1877}
1878
1879/*
1880 * this starts processing the delayed reference count updates and
1881 * extent insertions we have queued up so far. count can be
1882 * 0, which means to process everything in the tree at the start
1883 * of the run (but not newly added entries), or it can be some target
1884 * number you'd like to process.
1885 */
1886int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
1887 struct btrfs_root *root, unsigned long count)
1888{
1889 struct rb_node *node;
1890 struct btrfs_delayed_ref_root *delayed_refs;
1891 struct btrfs_delayed_ref_node *ref;
1892 struct list_head cluster;
1893 int ret;
1894 int run_all = count == (unsigned long)-1;
1895 int run_most = 0;
1896
1897 if (root == root->fs_info->extent_root)
1898 root = root->fs_info->tree_root;
1899
1900 delayed_refs = &trans->transaction->delayed_refs;
1901 INIT_LIST_HEAD(&cluster);
1902again:
1903 spin_lock(&delayed_refs->lock);
1904 if (count == 0) {
1905 count = delayed_refs->num_entries * 2;
1906 run_most = 1;
1907 }
1908 while (1) {
1909 if (!(run_all || run_most) &&
1910 delayed_refs->num_heads_ready < 64)
1911 break;
1912
1913 /*
1914 * go find something we can process in the rbtree. We start at
1915 * the beginning of the tree, and then build a cluster
1916 * of refs to process starting at the first one we are able to
1917 * lock
1918 */
1919 ret = btrfs_find_ref_cluster(trans, &cluster,
1920 delayed_refs->run_delayed_start);
1921 if (ret)
1922 break;
1923
1924 ret = run_clustered_refs(trans, root, &cluster);
1925 BUG_ON(ret < 0);
1926
1927 count -= min_t(unsigned long, ret, count);
1928
1929 if (count == 0)
1930 break;
1931 }
1932
Chris Mason56bec292009-03-13 10:10:06 -04001933 if (run_all) {
Chris Mason56bec292009-03-13 10:10:06 -04001934 node = rb_first(&delayed_refs->root);
Chris Masonc3e69d52009-03-13 10:17:05 -04001935 if (!node)
Chris Mason56bec292009-03-13 10:10:06 -04001936 goto out;
Chris Masonc3e69d52009-03-13 10:17:05 -04001937 count = (unsigned long)-1;
Chris Mason56bec292009-03-13 10:10:06 -04001938
1939 while (node) {
1940 ref = rb_entry(node, struct btrfs_delayed_ref_node,
1941 rb_node);
1942 if (btrfs_delayed_ref_is_head(ref)) {
1943 struct btrfs_delayed_ref_head *head;
1944
1945 head = btrfs_delayed_node_to_head(ref);
1946 atomic_inc(&ref->refs);
1947
1948 spin_unlock(&delayed_refs->lock);
1949 mutex_lock(&head->mutex);
1950 mutex_unlock(&head->mutex);
1951
1952 btrfs_put_delayed_ref(ref);
Chris Mason1887be62009-03-13 10:11:24 -04001953 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04001954 goto again;
1955 }
1956 node = rb_next(node);
1957 }
1958 spin_unlock(&delayed_refs->lock);
Chris Mason56bec292009-03-13 10:10:06 -04001959 schedule_timeout(1);
1960 goto again;
1961 }
Chris Mason54aa1f42007-06-22 14:16:25 -04001962out:
Chris Masonc3e69d52009-03-13 10:17:05 -04001963 spin_unlock(&delayed_refs->lock);
Chris Masona28ec192007-03-06 20:08:01 -05001964 return 0;
1965}
1966
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001967int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
1968 struct btrfs_root *root,
1969 u64 bytenr, u64 num_bytes, u64 flags,
1970 int is_data)
1971{
1972 struct btrfs_delayed_extent_op *extent_op;
1973 int ret;
1974
1975 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
1976 if (!extent_op)
1977 return -ENOMEM;
1978
1979 extent_op->flags_to_set = flags;
1980 extent_op->update_flags = 1;
1981 extent_op->update_key = 0;
1982 extent_op->is_data = is_data ? 1 : 0;
1983
1984 ret = btrfs_add_delayed_extent_op(trans, bytenr, num_bytes, extent_op);
1985 if (ret)
1986 kfree(extent_op);
1987 return ret;
1988}
1989
1990static noinline int check_delayed_ref(struct btrfs_trans_handle *trans,
1991 struct btrfs_root *root,
1992 struct btrfs_path *path,
1993 u64 objectid, u64 offset, u64 bytenr)
1994{
1995 struct btrfs_delayed_ref_head *head;
1996 struct btrfs_delayed_ref_node *ref;
1997 struct btrfs_delayed_data_ref *data_ref;
1998 struct btrfs_delayed_ref_root *delayed_refs;
1999 struct rb_node *node;
2000 int ret = 0;
2001
2002 ret = -ENOENT;
2003 delayed_refs = &trans->transaction->delayed_refs;
2004 spin_lock(&delayed_refs->lock);
2005 head = btrfs_find_delayed_ref_head(trans, bytenr);
2006 if (!head)
2007 goto out;
2008
2009 if (!mutex_trylock(&head->mutex)) {
2010 atomic_inc(&head->node.refs);
2011 spin_unlock(&delayed_refs->lock);
2012
2013 btrfs_release_path(root->fs_info->extent_root, path);
2014
2015 mutex_lock(&head->mutex);
2016 mutex_unlock(&head->mutex);
2017 btrfs_put_delayed_ref(&head->node);
2018 return -EAGAIN;
2019 }
2020
2021 node = rb_prev(&head->node.rb_node);
2022 if (!node)
2023 goto out_unlock;
2024
2025 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2026
2027 if (ref->bytenr != bytenr)
2028 goto out_unlock;
2029
2030 ret = 1;
2031 if (ref->type != BTRFS_EXTENT_DATA_REF_KEY)
2032 goto out_unlock;
2033
2034 data_ref = btrfs_delayed_node_to_data_ref(ref);
2035
2036 node = rb_prev(node);
2037 if (node) {
2038 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2039 if (ref->bytenr == bytenr)
2040 goto out_unlock;
2041 }
2042
2043 if (data_ref->root != root->root_key.objectid ||
2044 data_ref->objectid != objectid || data_ref->offset != offset)
2045 goto out_unlock;
2046
2047 ret = 0;
2048out_unlock:
2049 mutex_unlock(&head->mutex);
2050out:
2051 spin_unlock(&delayed_refs->lock);
2052 return ret;
2053}
2054
2055static noinline int check_committed_ref(struct btrfs_trans_handle *trans,
2056 struct btrfs_root *root,
2057 struct btrfs_path *path,
2058 u64 objectid, u64 offset, u64 bytenr)
Chris Masonbe20aa92007-12-17 20:14:01 -05002059{
2060 struct btrfs_root *extent_root = root->fs_info->extent_root;
Yan Zhengf321e492008-07-30 09:26:11 -04002061 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002062 struct btrfs_extent_data_ref *ref;
2063 struct btrfs_extent_inline_ref *iref;
2064 struct btrfs_extent_item *ei;
Chris Masonbe20aa92007-12-17 20:14:01 -05002065 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002066 u32 item_size;
Yan Zhengf321e492008-07-30 09:26:11 -04002067 int ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05002068
Chris Masonbe20aa92007-12-17 20:14:01 -05002069 key.objectid = bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -04002070 key.offset = (u64)-1;
Yan Zhengf321e492008-07-30 09:26:11 -04002071 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masonbe20aa92007-12-17 20:14:01 -05002072
Chris Masonbe20aa92007-12-17 20:14:01 -05002073 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
2074 if (ret < 0)
2075 goto out;
2076 BUG_ON(ret == 0);
Yan Zheng80ff3852008-10-30 14:20:02 -04002077
2078 ret = -ENOENT;
2079 if (path->slots[0] == 0)
Zheng Yan31840ae2008-09-23 13:14:14 -04002080 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002081
Zheng Yan31840ae2008-09-23 13:14:14 -04002082 path->slots[0]--;
Yan Zhengf321e492008-07-30 09:26:11 -04002083 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002084 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05002085
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002086 if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY)
Chris Masonbe20aa92007-12-17 20:14:01 -05002087 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002088
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002089 ret = 1;
2090 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2091#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2092 if (item_size < sizeof(*ei)) {
2093 WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0));
2094 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002095 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002096#endif
2097 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2098
2099 if (item_size != sizeof(*ei) +
2100 btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY))
2101 goto out;
2102
2103 if (btrfs_extent_generation(leaf, ei) <=
2104 btrfs_root_last_snapshot(&root->root_item))
2105 goto out;
2106
2107 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
2108 if (btrfs_extent_inline_ref_type(leaf, iref) !=
2109 BTRFS_EXTENT_DATA_REF_KEY)
2110 goto out;
2111
2112 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
2113 if (btrfs_extent_refs(leaf, ei) !=
2114 btrfs_extent_data_ref_count(leaf, ref) ||
2115 btrfs_extent_data_ref_root(leaf, ref) !=
2116 root->root_key.objectid ||
2117 btrfs_extent_data_ref_objectid(leaf, ref) != objectid ||
2118 btrfs_extent_data_ref_offset(leaf, ref) != offset)
2119 goto out;
2120
Yan Zhengf321e492008-07-30 09:26:11 -04002121 ret = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05002122out:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002123 return ret;
2124}
2125
2126int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
2127 struct btrfs_root *root,
2128 u64 objectid, u64 offset, u64 bytenr)
2129{
2130 struct btrfs_path *path;
2131 int ret;
2132 int ret2;
2133
2134 path = btrfs_alloc_path();
2135 if (!path)
2136 return -ENOENT;
2137
2138 do {
2139 ret = check_committed_ref(trans, root, path, objectid,
2140 offset, bytenr);
2141 if (ret && ret != -ENOENT)
2142 goto out;
2143
2144 ret2 = check_delayed_ref(trans, root, path, objectid,
2145 offset, bytenr);
2146 } while (ret2 == -EAGAIN);
2147
2148 if (ret2 && ret2 != -ENOENT) {
2149 ret = ret2;
2150 goto out;
2151 }
2152
2153 if (ret != -ENOENT || ret2 != -ENOENT)
2154 ret = 0;
2155out:
Yan Zhengf321e492008-07-30 09:26:11 -04002156 btrfs_free_path(path);
2157 return ret;
2158}
2159
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002160#if 0
Zheng Yan31840ae2008-09-23 13:14:14 -04002161int btrfs_cache_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2162 struct extent_buffer *buf, u32 nr_extents)
Chris Mason02217ed2007-03-02 16:08:05 -05002163{
Chris Mason5f39d392007-10-15 16:14:19 -04002164 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -04002165 struct btrfs_file_extent_item *fi;
Zheng Yane4657682008-09-26 10:04:53 -04002166 u64 root_gen;
2167 u32 nritems;
Chris Mason02217ed2007-03-02 16:08:05 -05002168 int i;
Chris Masondb945352007-10-15 16:15:53 -04002169 int level;
Zheng Yan31840ae2008-09-23 13:14:14 -04002170 int ret = 0;
Zheng Yane4657682008-09-26 10:04:53 -04002171 int shared = 0;
Chris Masona28ec192007-03-06 20:08:01 -05002172
Chris Mason3768f362007-03-13 16:47:54 -04002173 if (!root->ref_cows)
Chris Masona28ec192007-03-06 20:08:01 -05002174 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002175
Zheng Yane4657682008-09-26 10:04:53 -04002176 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
2177 shared = 0;
2178 root_gen = root->root_key.offset;
2179 } else {
2180 shared = 1;
2181 root_gen = trans->transid - 1;
2182 }
2183
Chris Masondb945352007-10-15 16:15:53 -04002184 level = btrfs_header_level(buf);
Chris Mason5f39d392007-10-15 16:14:19 -04002185 nritems = btrfs_header_nritems(buf);
Chris Mason4a096752008-07-21 10:29:44 -04002186
Zheng Yan31840ae2008-09-23 13:14:14 -04002187 if (level == 0) {
Yan Zheng31153d82008-07-28 15:32:19 -04002188 struct btrfs_leaf_ref *ref;
2189 struct btrfs_extent_info *info;
2190
Zheng Yan31840ae2008-09-23 13:14:14 -04002191 ref = btrfs_alloc_leaf_ref(root, nr_extents);
Yan Zheng31153d82008-07-28 15:32:19 -04002192 if (!ref) {
Zheng Yan31840ae2008-09-23 13:14:14 -04002193 ret = -ENOMEM;
Yan Zheng31153d82008-07-28 15:32:19 -04002194 goto out;
2195 }
2196
Zheng Yane4657682008-09-26 10:04:53 -04002197 ref->root_gen = root_gen;
Yan Zheng31153d82008-07-28 15:32:19 -04002198 ref->bytenr = buf->start;
2199 ref->owner = btrfs_header_owner(buf);
2200 ref->generation = btrfs_header_generation(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04002201 ref->nritems = nr_extents;
Yan Zheng31153d82008-07-28 15:32:19 -04002202 info = ref->extents;
Yanbcc63ab2008-07-30 16:29:20 -04002203
Zheng Yan31840ae2008-09-23 13:14:14 -04002204 for (i = 0; nr_extents > 0 && i < nritems; i++) {
Yan Zheng31153d82008-07-28 15:32:19 -04002205 u64 disk_bytenr;
2206 btrfs_item_key_to_cpu(buf, &key, i);
2207 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
2208 continue;
2209 fi = btrfs_item_ptr(buf, i,
2210 struct btrfs_file_extent_item);
2211 if (btrfs_file_extent_type(buf, fi) ==
2212 BTRFS_FILE_EXTENT_INLINE)
2213 continue;
2214 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
2215 if (disk_bytenr == 0)
2216 continue;
2217
2218 info->bytenr = disk_bytenr;
2219 info->num_bytes =
2220 btrfs_file_extent_disk_num_bytes(buf, fi);
2221 info->objectid = key.objectid;
2222 info->offset = key.offset;
2223 info++;
2224 }
2225
Zheng Yane4657682008-09-26 10:04:53 -04002226 ret = btrfs_add_leaf_ref(root, ref, shared);
Yan Zheng5b84e8d2008-10-09 11:46:19 -04002227 if (ret == -EEXIST && shared) {
2228 struct btrfs_leaf_ref *old;
2229 old = btrfs_lookup_leaf_ref(root, ref->bytenr);
2230 BUG_ON(!old);
2231 btrfs_remove_leaf_ref(root, old);
2232 btrfs_free_leaf_ref(root, old);
2233 ret = btrfs_add_leaf_ref(root, ref, shared);
2234 }
Yan Zheng31153d82008-07-28 15:32:19 -04002235 WARN_ON(ret);
Yanbcc63ab2008-07-30 16:29:20 -04002236 btrfs_free_leaf_ref(root, ref);
Yan Zheng31153d82008-07-28 15:32:19 -04002237 }
2238out:
Zheng Yan31840ae2008-09-23 13:14:14 -04002239 return ret;
2240}
2241
Chris Masonb7a9f292009-02-04 09:23:45 -05002242/* when a block goes through cow, we update the reference counts of
2243 * everything that block points to. The internal pointers of the block
2244 * can be in just about any order, and it is likely to have clusters of
2245 * things that are close together and clusters of things that are not.
2246 *
2247 * To help reduce the seeks that come with updating all of these reference
2248 * counts, sort them by byte number before actual updates are done.
2249 *
2250 * struct refsort is used to match byte number to slot in the btree block.
2251 * we sort based on the byte number and then use the slot to actually
2252 * find the item.
Chris Masonbd56b302009-02-04 09:27:02 -05002253 *
2254 * struct refsort is smaller than strcut btrfs_item and smaller than
2255 * struct btrfs_key_ptr. Since we're currently limited to the page size
2256 * for a btree block, there's no way for a kmalloc of refsorts for a
2257 * single node to be bigger than a page.
Chris Masonb7a9f292009-02-04 09:23:45 -05002258 */
2259struct refsort {
2260 u64 bytenr;
2261 u32 slot;
2262};
2263
2264/*
2265 * for passing into sort()
2266 */
2267static int refsort_cmp(const void *a_void, const void *b_void)
2268{
2269 const struct refsort *a = a_void;
2270 const struct refsort *b = b_void;
2271
2272 if (a->bytenr < b->bytenr)
2273 return -1;
2274 if (a->bytenr > b->bytenr)
2275 return 1;
2276 return 0;
2277}
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002278#endif
Chris Masonb7a9f292009-02-04 09:23:45 -05002279
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002280static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
Chris Masonb7a9f292009-02-04 09:23:45 -05002281 struct btrfs_root *root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002282 struct extent_buffer *buf,
2283 int full_backref, int inc)
Zheng Yan31840ae2008-09-23 13:14:14 -04002284{
2285 u64 bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002286 u64 num_bytes;
2287 u64 parent;
Zheng Yan31840ae2008-09-23 13:14:14 -04002288 u64 ref_root;
Zheng Yan31840ae2008-09-23 13:14:14 -04002289 u32 nritems;
Zheng Yan31840ae2008-09-23 13:14:14 -04002290 struct btrfs_key key;
2291 struct btrfs_file_extent_item *fi;
2292 int i;
2293 int level;
2294 int ret = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04002295 int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002296 u64, u64, u64, u64, u64, u64);
Zheng Yan31840ae2008-09-23 13:14:14 -04002297
2298 ref_root = btrfs_header_owner(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04002299 nritems = btrfs_header_nritems(buf);
2300 level = btrfs_header_level(buf);
2301
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002302 if (!root->ref_cows && level == 0)
2303 return 0;
Chris Masonb7a9f292009-02-04 09:23:45 -05002304
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002305 if (inc)
2306 process_func = btrfs_inc_extent_ref;
2307 else
2308 process_func = btrfs_free_extent;
Zheng Yan31840ae2008-09-23 13:14:14 -04002309
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002310 if (full_backref)
2311 parent = buf->start;
2312 else
2313 parent = 0;
2314
Zheng Yan31840ae2008-09-23 13:14:14 -04002315 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04002316 if (level == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002317 btrfs_item_key_to_cpu(buf, &key, i);
2318 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -04002319 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04002320 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -04002321 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002322 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -04002323 BTRFS_FILE_EXTENT_INLINE)
2324 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04002325 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
2326 if (bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -04002327 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04002328
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002329 num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
2330 key.offset -= btrfs_file_extent_offset(buf, fi);
2331 ret = process_func(trans, root, bytenr, num_bytes,
2332 parent, ref_root, key.objectid,
2333 key.offset);
2334 if (ret)
2335 goto fail;
Chris Masonb7a9f292009-02-04 09:23:45 -05002336 } else {
2337 bytenr = btrfs_node_blockptr(buf, i);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002338 num_bytes = btrfs_level_size(root, level - 1);
2339 ret = process_func(trans, root, bytenr, num_bytes,
2340 parent, ref_root, level - 1, 0);
2341 if (ret)
Zheng Yan31840ae2008-09-23 13:14:14 -04002342 goto fail;
Chris Mason54aa1f42007-06-22 14:16:25 -04002343 }
2344 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002345 return 0;
2346fail:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002347 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -04002348 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05002349}
2350
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002351int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2352 struct extent_buffer *buf, int full_backref)
Zheng Yan31840ae2008-09-23 13:14:14 -04002353{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002354 return __btrfs_mod_ref(trans, root, buf, full_backref, 1);
2355}
Zheng Yan31840ae2008-09-23 13:14:14 -04002356
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002357int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2358 struct extent_buffer *buf, int full_backref)
2359{
2360 return __btrfs_mod_ref(trans, root, buf, full_backref, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04002361}
2362
Chris Mason9078a3e2007-04-26 16:46:15 -04002363static int write_one_cache_group(struct btrfs_trans_handle *trans,
2364 struct btrfs_root *root,
2365 struct btrfs_path *path,
2366 struct btrfs_block_group_cache *cache)
2367{
2368 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04002369 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04002370 unsigned long bi;
2371 struct extent_buffer *leaf;
Chris Mason9078a3e2007-04-26 16:46:15 -04002372
Chris Mason9078a3e2007-04-26 16:46:15 -04002373 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04002374 if (ret < 0)
2375 goto fail;
Chris Mason9078a3e2007-04-26 16:46:15 -04002376 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04002377
2378 leaf = path->nodes[0];
2379 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
2380 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
2381 btrfs_mark_buffer_dirty(leaf);
Chris Mason9078a3e2007-04-26 16:46:15 -04002382 btrfs_release_path(extent_root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -04002383fail:
Chris Mason9078a3e2007-04-26 16:46:15 -04002384 if (ret)
2385 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04002386 return 0;
2387
2388}
2389
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002390static struct btrfs_block_group_cache *
2391next_block_group(struct btrfs_root *root,
2392 struct btrfs_block_group_cache *cache)
2393{
2394 struct rb_node *node;
2395 spin_lock(&root->fs_info->block_group_cache_lock);
2396 node = rb_next(&cache->cache_node);
2397 btrfs_put_block_group(cache);
2398 if (node) {
2399 cache = rb_entry(node, struct btrfs_block_group_cache,
2400 cache_node);
2401 atomic_inc(&cache->count);
2402 } else
2403 cache = NULL;
2404 spin_unlock(&root->fs_info->block_group_cache_lock);
2405 return cache;
2406}
2407
Chris Mason96b51792007-10-15 16:15:19 -04002408int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
2409 struct btrfs_root *root)
Chris Mason9078a3e2007-04-26 16:46:15 -04002410{
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002411 struct btrfs_block_group_cache *cache;
Chris Mason9078a3e2007-04-26 16:46:15 -04002412 int err = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04002413 struct btrfs_path *path;
Chris Mason96b51792007-10-15 16:15:19 -04002414 u64 last = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04002415
2416 path = btrfs_alloc_path();
2417 if (!path)
2418 return -ENOMEM;
2419
Chris Masond3977122009-01-05 21:25:51 -05002420 while (1) {
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002421 if (last == 0) {
2422 err = btrfs_run_delayed_refs(trans, root,
2423 (unsigned long)-1);
2424 BUG_ON(err);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002425 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002426
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002427 cache = btrfs_lookup_first_block_group(root->fs_info, last);
2428 while (cache) {
2429 if (cache->dirty)
2430 break;
2431 cache = next_block_group(root, cache);
2432 }
2433 if (!cache) {
2434 if (last == 0)
2435 break;
2436 last = 0;
Chris Mason96b51792007-10-15 16:15:19 -04002437 continue;
Chris Mason9078a3e2007-04-26 16:46:15 -04002438 }
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002439
2440 cache->dirty = 0;
2441 last = cache->key.objectid + cache->key.offset;
2442
2443 err = write_one_cache_group(trans, root, path, cache);
2444 BUG_ON(err);
2445 btrfs_put_block_group(cache);
Chris Mason9078a3e2007-04-26 16:46:15 -04002446 }
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002447
Chris Mason9078a3e2007-04-26 16:46:15 -04002448 btrfs_free_path(path);
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002449 return 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04002450}
2451
Yan Zhengd2fb3432008-12-11 16:30:39 -05002452int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr)
2453{
2454 struct btrfs_block_group_cache *block_group;
2455 int readonly = 0;
2456
2457 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
2458 if (!block_group || block_group->ro)
2459 readonly = 1;
2460 if (block_group)
Chris Masonfa9c0d792009-04-03 09:47:43 -04002461 btrfs_put_block_group(block_group);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002462 return readonly;
2463}
2464
Chris Mason593060d2008-03-25 16:50:33 -04002465static int update_space_info(struct btrfs_fs_info *info, u64 flags,
2466 u64 total_bytes, u64 bytes_used,
2467 struct btrfs_space_info **space_info)
2468{
2469 struct btrfs_space_info *found;
2470
2471 found = __find_space_info(info, flags);
2472 if (found) {
Josef Bacik25179202008-10-29 14:49:05 -04002473 spin_lock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04002474 found->total_bytes += total_bytes;
2475 found->bytes_used += bytes_used;
Chris Mason8f18cf12008-04-25 16:53:30 -04002476 found->full = 0;
Josef Bacik25179202008-10-29 14:49:05 -04002477 spin_unlock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04002478 *space_info = found;
2479 return 0;
2480 }
Yan Zhengc146afa2008-11-12 14:34:12 -05002481 found = kzalloc(sizeof(*found), GFP_NOFS);
Chris Mason593060d2008-03-25 16:50:33 -04002482 if (!found)
2483 return -ENOMEM;
2484
Josef Bacik0f9dd462008-09-23 13:14:11 -04002485 INIT_LIST_HEAD(&found->block_groups);
Josef Bacik80eb2342008-10-29 14:49:05 -04002486 init_rwsem(&found->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002487 spin_lock_init(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04002488 found->flags = flags;
2489 found->total_bytes = total_bytes;
2490 found->bytes_used = bytes_used;
2491 found->bytes_pinned = 0;
Zheng Yane8569812008-09-26 10:05:48 -04002492 found->bytes_reserved = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05002493 found->bytes_readonly = 0;
Josef Bacik6a632092009-02-20 11:00:09 -05002494 found->bytes_delalloc = 0;
Chris Mason593060d2008-03-25 16:50:33 -04002495 found->full = 0;
Chris Mason0ef3e662008-05-24 14:04:53 -04002496 found->force_alloc = 0;
Chris Mason593060d2008-03-25 16:50:33 -04002497 *space_info = found;
Chris Mason4184ea72009-03-10 12:39:20 -04002498 list_add_rcu(&found->list, &info->space_info);
Chris Mason593060d2008-03-25 16:50:33 -04002499 return 0;
2500}
2501
Chris Mason8790d502008-04-03 16:29:03 -04002502static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
2503{
2504 u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
Chris Mason611f0e02008-04-03 16:29:03 -04002505 BTRFS_BLOCK_GROUP_RAID1 |
Chris Mason321aecc2008-04-16 10:49:51 -04002506 BTRFS_BLOCK_GROUP_RAID10 |
Chris Mason611f0e02008-04-03 16:29:03 -04002507 BTRFS_BLOCK_GROUP_DUP);
Chris Mason8790d502008-04-03 16:29:03 -04002508 if (extra_flags) {
2509 if (flags & BTRFS_BLOCK_GROUP_DATA)
2510 fs_info->avail_data_alloc_bits |= extra_flags;
2511 if (flags & BTRFS_BLOCK_GROUP_METADATA)
2512 fs_info->avail_metadata_alloc_bits |= extra_flags;
2513 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
2514 fs_info->avail_system_alloc_bits |= extra_flags;
2515 }
2516}
Chris Mason593060d2008-03-25 16:50:33 -04002517
Yan Zhengc146afa2008-11-12 14:34:12 -05002518static void set_block_group_readonly(struct btrfs_block_group_cache *cache)
2519{
2520 spin_lock(&cache->space_info->lock);
2521 spin_lock(&cache->lock);
2522 if (!cache->ro) {
2523 cache->space_info->bytes_readonly += cache->key.offset -
2524 btrfs_block_group_used(&cache->item);
2525 cache->ro = 1;
2526 }
2527 spin_unlock(&cache->lock);
2528 spin_unlock(&cache->space_info->lock);
2529}
2530
Yan Zheng2b820322008-11-17 21:11:30 -05002531u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
Chris Masonec44a352008-04-28 15:29:52 -04002532{
Yan Zheng2b820322008-11-17 21:11:30 -05002533 u64 num_devices = root->fs_info->fs_devices->rw_devices;
Chris Masona061fc82008-05-07 11:43:44 -04002534
2535 if (num_devices == 1)
2536 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
2537 if (num_devices < 4)
2538 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
2539
Chris Masonec44a352008-04-28 15:29:52 -04002540 if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
2541 (flags & (BTRFS_BLOCK_GROUP_RAID1 |
Chris Masona061fc82008-05-07 11:43:44 -04002542 BTRFS_BLOCK_GROUP_RAID10))) {
Chris Masonec44a352008-04-28 15:29:52 -04002543 flags &= ~BTRFS_BLOCK_GROUP_DUP;
Chris Masona061fc82008-05-07 11:43:44 -04002544 }
Chris Masonec44a352008-04-28 15:29:52 -04002545
2546 if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
Chris Masona061fc82008-05-07 11:43:44 -04002547 (flags & BTRFS_BLOCK_GROUP_RAID10)) {
Chris Masonec44a352008-04-28 15:29:52 -04002548 flags &= ~BTRFS_BLOCK_GROUP_RAID1;
Chris Masona061fc82008-05-07 11:43:44 -04002549 }
Chris Masonec44a352008-04-28 15:29:52 -04002550
2551 if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
2552 ((flags & BTRFS_BLOCK_GROUP_RAID1) |
2553 (flags & BTRFS_BLOCK_GROUP_RAID10) |
2554 (flags & BTRFS_BLOCK_GROUP_DUP)))
2555 flags &= ~BTRFS_BLOCK_GROUP_RAID0;
2556 return flags;
2557}
2558
Josef Bacik6a632092009-02-20 11:00:09 -05002559static u64 btrfs_get_alloc_profile(struct btrfs_root *root, u64 data)
2560{
2561 struct btrfs_fs_info *info = root->fs_info;
2562 u64 alloc_profile;
2563
2564 if (data) {
2565 alloc_profile = info->avail_data_alloc_bits &
2566 info->data_alloc_profile;
2567 data = BTRFS_BLOCK_GROUP_DATA | alloc_profile;
2568 } else if (root == root->fs_info->chunk_root) {
2569 alloc_profile = info->avail_system_alloc_bits &
2570 info->system_alloc_profile;
2571 data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile;
2572 } else {
2573 alloc_profile = info->avail_metadata_alloc_bits &
2574 info->metadata_alloc_profile;
2575 data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile;
2576 }
2577
2578 return btrfs_reduce_alloc_profile(root, data);
2579}
2580
2581void btrfs_set_inode_space_info(struct btrfs_root *root, struct inode *inode)
2582{
2583 u64 alloc_target;
2584
2585 alloc_target = btrfs_get_alloc_profile(root, 1);
2586 BTRFS_I(inode)->space_info = __find_space_info(root->fs_info,
2587 alloc_target);
2588}
2589
2590/*
2591 * for now this just makes sure we have at least 5% of our metadata space free
2592 * for use.
2593 */
2594int btrfs_check_metadata_free_space(struct btrfs_root *root)
2595{
2596 struct btrfs_fs_info *info = root->fs_info;
2597 struct btrfs_space_info *meta_sinfo;
2598 u64 alloc_target, thresh;
Josef Bacik4e06bdd2009-02-20 10:59:53 -05002599 int committed = 0, ret;
Josef Bacik6a632092009-02-20 11:00:09 -05002600
2601 /* get the space info for where the metadata will live */
2602 alloc_target = btrfs_get_alloc_profile(root, 0);
2603 meta_sinfo = __find_space_info(info, alloc_target);
2604
Josef Bacik4e06bdd2009-02-20 10:59:53 -05002605again:
Josef Bacik6a632092009-02-20 11:00:09 -05002606 spin_lock(&meta_sinfo->lock);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05002607 if (!meta_sinfo->full)
2608 thresh = meta_sinfo->total_bytes * 80;
2609 else
2610 thresh = meta_sinfo->total_bytes * 95;
Josef Bacik6a632092009-02-20 11:00:09 -05002611
2612 do_div(thresh, 100);
2613
2614 if (meta_sinfo->bytes_used + meta_sinfo->bytes_reserved +
2615 meta_sinfo->bytes_pinned + meta_sinfo->bytes_readonly > thresh) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05002616 struct btrfs_trans_handle *trans;
2617 if (!meta_sinfo->full) {
2618 meta_sinfo->force_alloc = 1;
2619 spin_unlock(&meta_sinfo->lock);
2620
2621 trans = btrfs_start_transaction(root, 1);
2622 if (!trans)
2623 return -ENOMEM;
2624
2625 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
2626 2 * 1024 * 1024, alloc_target, 0);
2627 btrfs_end_transaction(trans, root);
2628 goto again;
2629 }
Josef Bacik6a632092009-02-20 11:00:09 -05002630 spin_unlock(&meta_sinfo->lock);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05002631
2632 if (!committed) {
2633 committed = 1;
2634 trans = btrfs_join_transaction(root, 1);
2635 if (!trans)
2636 return -ENOMEM;
2637 ret = btrfs_commit_transaction(trans, root);
2638 if (ret)
2639 return ret;
2640 goto again;
2641 }
Josef Bacik6a632092009-02-20 11:00:09 -05002642 return -ENOSPC;
2643 }
2644 spin_unlock(&meta_sinfo->lock);
2645
2646 return 0;
2647}
2648
2649/*
2650 * This will check the space that the inode allocates from to make sure we have
2651 * enough space for bytes.
2652 */
2653int btrfs_check_data_free_space(struct btrfs_root *root, struct inode *inode,
2654 u64 bytes)
2655{
2656 struct btrfs_space_info *data_sinfo;
Josef Bacik4e06bdd2009-02-20 10:59:53 -05002657 int ret = 0, committed = 0;
Josef Bacik6a632092009-02-20 11:00:09 -05002658
2659 /* make sure bytes are sectorsize aligned */
2660 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
2661
2662 data_sinfo = BTRFS_I(inode)->space_info;
2663again:
2664 /* make sure we have enough space to handle the data first */
2665 spin_lock(&data_sinfo->lock);
2666 if (data_sinfo->total_bytes - data_sinfo->bytes_used -
2667 data_sinfo->bytes_delalloc - data_sinfo->bytes_reserved -
2668 data_sinfo->bytes_pinned - data_sinfo->bytes_readonly -
2669 data_sinfo->bytes_may_use < bytes) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05002670 struct btrfs_trans_handle *trans;
2671
Josef Bacik6a632092009-02-20 11:00:09 -05002672 /*
2673 * if we don't have enough free bytes in this space then we need
2674 * to alloc a new chunk.
2675 */
2676 if (!data_sinfo->full) {
2677 u64 alloc_target;
Josef Bacik6a632092009-02-20 11:00:09 -05002678
2679 data_sinfo->force_alloc = 1;
2680 spin_unlock(&data_sinfo->lock);
2681
2682 alloc_target = btrfs_get_alloc_profile(root, 1);
2683 trans = btrfs_start_transaction(root, 1);
2684 if (!trans)
2685 return -ENOMEM;
2686
2687 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
2688 bytes + 2 * 1024 * 1024,
2689 alloc_target, 0);
2690 btrfs_end_transaction(trans, root);
2691 if (ret)
2692 return ret;
2693 goto again;
2694 }
2695 spin_unlock(&data_sinfo->lock);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05002696
2697 /* commit the current transaction and try again */
2698 if (!committed) {
2699 committed = 1;
2700 trans = btrfs_join_transaction(root, 1);
2701 if (!trans)
2702 return -ENOMEM;
2703 ret = btrfs_commit_transaction(trans, root);
2704 if (ret)
2705 return ret;
2706 goto again;
2707 }
2708
Josef Bacik6a632092009-02-20 11:00:09 -05002709 printk(KERN_ERR "no space left, need %llu, %llu delalloc bytes"
2710 ", %llu bytes_used, %llu bytes_reserved, "
Hu Tao68f5a382009-07-02 13:55:45 -04002711 "%llu bytes_pinned, %llu bytes_readonly, %llu may use "
Joel Becker21380932009-04-21 12:38:29 -07002712 "%llu total\n", (unsigned long long)bytes,
2713 (unsigned long long)data_sinfo->bytes_delalloc,
2714 (unsigned long long)data_sinfo->bytes_used,
2715 (unsigned long long)data_sinfo->bytes_reserved,
2716 (unsigned long long)data_sinfo->bytes_pinned,
2717 (unsigned long long)data_sinfo->bytes_readonly,
2718 (unsigned long long)data_sinfo->bytes_may_use,
2719 (unsigned long long)data_sinfo->total_bytes);
Josef Bacik6a632092009-02-20 11:00:09 -05002720 return -ENOSPC;
2721 }
2722 data_sinfo->bytes_may_use += bytes;
2723 BTRFS_I(inode)->reserved_bytes += bytes;
2724 spin_unlock(&data_sinfo->lock);
2725
2726 return btrfs_check_metadata_free_space(root);
2727}
2728
2729/*
2730 * if there was an error for whatever reason after calling
2731 * btrfs_check_data_free_space, call this so we can cleanup the counters.
2732 */
2733void btrfs_free_reserved_data_space(struct btrfs_root *root,
2734 struct inode *inode, u64 bytes)
2735{
2736 struct btrfs_space_info *data_sinfo;
2737
2738 /* make sure bytes are sectorsize aligned */
2739 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
2740
2741 data_sinfo = BTRFS_I(inode)->space_info;
2742 spin_lock(&data_sinfo->lock);
2743 data_sinfo->bytes_may_use -= bytes;
2744 BTRFS_I(inode)->reserved_bytes -= bytes;
2745 spin_unlock(&data_sinfo->lock);
2746}
2747
2748/* called when we are adding a delalloc extent to the inode's io_tree */
2749void btrfs_delalloc_reserve_space(struct btrfs_root *root, struct inode *inode,
2750 u64 bytes)
2751{
2752 struct btrfs_space_info *data_sinfo;
2753
2754 /* get the space info for where this inode will be storing its data */
2755 data_sinfo = BTRFS_I(inode)->space_info;
2756
2757 /* make sure we have enough space to handle the data first */
2758 spin_lock(&data_sinfo->lock);
2759 data_sinfo->bytes_delalloc += bytes;
2760
2761 /*
2762 * we are adding a delalloc extent without calling
2763 * btrfs_check_data_free_space first. This happens on a weird
2764 * writepage condition, but shouldn't hurt our accounting
2765 */
2766 if (unlikely(bytes > BTRFS_I(inode)->reserved_bytes)) {
2767 data_sinfo->bytes_may_use -= BTRFS_I(inode)->reserved_bytes;
2768 BTRFS_I(inode)->reserved_bytes = 0;
2769 } else {
2770 data_sinfo->bytes_may_use -= bytes;
2771 BTRFS_I(inode)->reserved_bytes -= bytes;
2772 }
2773
2774 spin_unlock(&data_sinfo->lock);
2775}
2776
2777/* called when we are clearing an delalloc extent from the inode's io_tree */
2778void btrfs_delalloc_free_space(struct btrfs_root *root, struct inode *inode,
2779 u64 bytes)
2780{
2781 struct btrfs_space_info *info;
2782
2783 info = BTRFS_I(inode)->space_info;
2784
2785 spin_lock(&info->lock);
2786 info->bytes_delalloc -= bytes;
2787 spin_unlock(&info->lock);
2788}
2789
Josef Bacik97e728d2009-04-21 17:40:57 -04002790static void force_metadata_allocation(struct btrfs_fs_info *info)
2791{
2792 struct list_head *head = &info->space_info;
2793 struct btrfs_space_info *found;
2794
2795 rcu_read_lock();
2796 list_for_each_entry_rcu(found, head, list) {
2797 if (found->flags & BTRFS_BLOCK_GROUP_METADATA)
2798 found->force_alloc = 1;
2799 }
2800 rcu_read_unlock();
2801}
2802
Chris Mason6324fbf2008-03-24 15:01:59 -04002803static int do_chunk_alloc(struct btrfs_trans_handle *trans,
2804 struct btrfs_root *extent_root, u64 alloc_bytes,
Chris Mason0ef3e662008-05-24 14:04:53 -04002805 u64 flags, int force)
Chris Mason6324fbf2008-03-24 15:01:59 -04002806{
2807 struct btrfs_space_info *space_info;
Josef Bacik97e728d2009-04-21 17:40:57 -04002808 struct btrfs_fs_info *fs_info = extent_root->fs_info;
Chris Mason6324fbf2008-03-24 15:01:59 -04002809 u64 thresh;
Yan Zhengc146afa2008-11-12 14:34:12 -05002810 int ret = 0;
2811
Josef Bacik97e728d2009-04-21 17:40:57 -04002812 mutex_lock(&fs_info->chunk_mutex);
Chris Mason6324fbf2008-03-24 15:01:59 -04002813
Yan Zheng2b820322008-11-17 21:11:30 -05002814 flags = btrfs_reduce_alloc_profile(extent_root, flags);
Chris Masonec44a352008-04-28 15:29:52 -04002815
Chris Mason6324fbf2008-03-24 15:01:59 -04002816 space_info = __find_space_info(extent_root->fs_info, flags);
Chris Mason593060d2008-03-25 16:50:33 -04002817 if (!space_info) {
2818 ret = update_space_info(extent_root->fs_info, flags,
2819 0, 0, &space_info);
2820 BUG_ON(ret);
2821 }
Chris Mason6324fbf2008-03-24 15:01:59 -04002822 BUG_ON(!space_info);
2823
Josef Bacik25179202008-10-29 14:49:05 -04002824 spin_lock(&space_info->lock);
Chris Mason0ef3e662008-05-24 14:04:53 -04002825 if (space_info->force_alloc) {
2826 force = 1;
2827 space_info->force_alloc = 0;
2828 }
Josef Bacik25179202008-10-29 14:49:05 -04002829 if (space_info->full) {
2830 spin_unlock(&space_info->lock);
Chris Mason925baed2008-06-25 16:01:30 -04002831 goto out;
Josef Bacik25179202008-10-29 14:49:05 -04002832 }
Chris Mason6324fbf2008-03-24 15:01:59 -04002833
Yan Zhengc146afa2008-11-12 14:34:12 -05002834 thresh = space_info->total_bytes - space_info->bytes_readonly;
2835 thresh = div_factor(thresh, 6);
Chris Mason0ef3e662008-05-24 14:04:53 -04002836 if (!force &&
Zheng Yane8569812008-09-26 10:05:48 -04002837 (space_info->bytes_used + space_info->bytes_pinned +
Josef Bacik25179202008-10-29 14:49:05 -04002838 space_info->bytes_reserved + alloc_bytes) < thresh) {
2839 spin_unlock(&space_info->lock);
Chris Mason925baed2008-06-25 16:01:30 -04002840 goto out;
Josef Bacik25179202008-10-29 14:49:05 -04002841 }
Josef Bacik25179202008-10-29 14:49:05 -04002842 spin_unlock(&space_info->lock);
2843
Josef Bacik97e728d2009-04-21 17:40:57 -04002844 /*
2845 * if we're doing a data chunk, go ahead and make sure that
2846 * we keep a reasonable number of metadata chunks allocated in the
2847 * FS as well.
2848 */
2849 if (flags & BTRFS_BLOCK_GROUP_DATA) {
2850 fs_info->data_chunk_allocations++;
2851 if (!(fs_info->data_chunk_allocations %
2852 fs_info->metadata_ratio))
2853 force_metadata_allocation(fs_info);
2854 }
2855
Yan Zheng2b820322008-11-17 21:11:30 -05002856 ret = btrfs_alloc_chunk(trans, extent_root, flags);
Chris Masond3977122009-01-05 21:25:51 -05002857 if (ret)
Chris Mason6324fbf2008-03-24 15:01:59 -04002858 space_info->full = 1;
Chris Masona74a4b92008-06-25 16:01:31 -04002859out:
Yan Zhengc146afa2008-11-12 14:34:12 -05002860 mutex_unlock(&extent_root->fs_info->chunk_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002861 return ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04002862}
2863
Chris Mason9078a3e2007-04-26 16:46:15 -04002864static int update_block_group(struct btrfs_trans_handle *trans,
2865 struct btrfs_root *root,
Chris Masondb945352007-10-15 16:15:53 -04002866 u64 bytenr, u64 num_bytes, int alloc,
Chris Mason0b86a832008-03-24 15:01:56 -04002867 int mark_free)
Chris Mason9078a3e2007-04-26 16:46:15 -04002868{
2869 struct btrfs_block_group_cache *cache;
2870 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -04002871 u64 total = num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04002872 u64 old_val;
Chris Masondb945352007-10-15 16:15:53 -04002873 u64 byte_in_group;
Chris Mason3e1ad542007-05-07 20:03:49 -04002874
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002875 /* block accounting for super block */
2876 spin_lock(&info->delalloc_lock);
2877 old_val = btrfs_super_bytes_used(&info->super_copy);
2878 if (alloc)
2879 old_val += num_bytes;
2880 else
2881 old_val -= num_bytes;
2882 btrfs_set_super_bytes_used(&info->super_copy, old_val);
2883
2884 /* block accounting for root item */
2885 old_val = btrfs_root_used(&root->root_item);
2886 if (alloc)
2887 old_val += num_bytes;
2888 else
2889 old_val -= num_bytes;
2890 btrfs_set_root_used(&root->root_item, old_val);
2891 spin_unlock(&info->delalloc_lock);
2892
Chris Masond3977122009-01-05 21:25:51 -05002893 while (total) {
Chris Masondb945352007-10-15 16:15:53 -04002894 cache = btrfs_lookup_block_group(info, bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05002895 if (!cache)
Chris Mason9078a3e2007-04-26 16:46:15 -04002896 return -1;
Chris Masondb945352007-10-15 16:15:53 -04002897 byte_in_group = bytenr - cache->key.objectid;
2898 WARN_ON(byte_in_group > cache->key.offset);
Chris Mason9078a3e2007-04-26 16:46:15 -04002899
Josef Bacik25179202008-10-29 14:49:05 -04002900 spin_lock(&cache->space_info->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04002901 spin_lock(&cache->lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002902 cache->dirty = 1;
Chris Mason9078a3e2007-04-26 16:46:15 -04002903 old_val = btrfs_block_group_used(&cache->item);
Chris Masondb945352007-10-15 16:15:53 -04002904 num_bytes = min(total, cache->key.offset - byte_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -04002905 if (alloc) {
Chris Masondb945352007-10-15 16:15:53 -04002906 old_val += num_bytes;
Chris Mason6324fbf2008-03-24 15:01:59 -04002907 cache->space_info->bytes_used += num_bytes;
Yan Zhenga512bbf2008-12-08 16:46:26 -05002908 if (cache->ro)
Yan Zhengc146afa2008-11-12 14:34:12 -05002909 cache->space_info->bytes_readonly -= num_bytes;
Chris Masonc286ac42008-07-22 23:06:41 -04002910 btrfs_set_block_group_used(&cache->item, old_val);
2911 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04002912 spin_unlock(&cache->space_info->lock);
Chris Masoncd1bc462007-04-27 10:08:34 -04002913 } else {
Chris Masondb945352007-10-15 16:15:53 -04002914 old_val -= num_bytes;
Chris Mason6324fbf2008-03-24 15:01:59 -04002915 cache->space_info->bytes_used -= num_bytes;
Yan Zhengc146afa2008-11-12 14:34:12 -05002916 if (cache->ro)
2917 cache->space_info->bytes_readonly += num_bytes;
Chris Masonc286ac42008-07-22 23:06:41 -04002918 btrfs_set_block_group_used(&cache->item, old_val);
2919 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04002920 spin_unlock(&cache->space_info->lock);
Chris Masonf510cfe2007-10-15 16:14:48 -04002921 if (mark_free) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04002922 int ret;
Liu Hui1f3c79a2009-01-05 15:57:51 -05002923
2924 ret = btrfs_discard_extent(root, bytenr,
2925 num_bytes);
2926 WARN_ON(ret);
2927
Josef Bacik0f9dd462008-09-23 13:14:11 -04002928 ret = btrfs_add_free_space(cache, bytenr,
2929 num_bytes);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002930 WARN_ON(ret);
Chris Masone37c9e62007-05-09 20:13:14 -04002931 }
Chris Masoncd1bc462007-04-27 10:08:34 -04002932 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04002933 btrfs_put_block_group(cache);
Chris Masondb945352007-10-15 16:15:53 -04002934 total -= num_bytes;
2935 bytenr += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04002936 }
2937 return 0;
2938}
Chris Mason6324fbf2008-03-24 15:01:59 -04002939
Chris Masona061fc82008-05-07 11:43:44 -04002940static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
2941{
Josef Bacik0f9dd462008-09-23 13:14:11 -04002942 struct btrfs_block_group_cache *cache;
Yan Zhengd2fb3432008-12-11 16:30:39 -05002943 u64 bytenr;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002944
2945 cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
2946 if (!cache)
Chris Masona061fc82008-05-07 11:43:44 -04002947 return 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002948
Yan Zhengd2fb3432008-12-11 16:30:39 -05002949 bytenr = cache->key.objectid;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002950 btrfs_put_block_group(cache);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002951
2952 return bytenr;
Chris Masona061fc82008-05-07 11:43:44 -04002953}
2954
Chris Masone02119d2008-09-05 16:13:11 -04002955int btrfs_update_pinned_extents(struct btrfs_root *root,
Yan324ae4d2007-11-16 14:57:08 -05002956 u64 bytenr, u64 num, int pin)
2957{
2958 u64 len;
2959 struct btrfs_block_group_cache *cache;
2960 struct btrfs_fs_info *fs_info = root->fs_info;
2961
2962 if (pin) {
2963 set_extent_dirty(&fs_info->pinned_extents,
2964 bytenr, bytenr + num - 1, GFP_NOFS);
2965 } else {
2966 clear_extent_dirty(&fs_info->pinned_extents,
2967 bytenr, bytenr + num - 1, GFP_NOFS);
2968 }
Chris Masonb9473432009-03-13 11:00:37 -04002969
Yan324ae4d2007-11-16 14:57:08 -05002970 while (num > 0) {
2971 cache = btrfs_lookup_block_group(fs_info, bytenr);
Zheng Yane8569812008-09-26 10:05:48 -04002972 BUG_ON(!cache);
2973 len = min(num, cache->key.offset -
2974 (bytenr - cache->key.objectid));
Yan324ae4d2007-11-16 14:57:08 -05002975 if (pin) {
Josef Bacik25179202008-10-29 14:49:05 -04002976 spin_lock(&cache->space_info->lock);
Zheng Yane8569812008-09-26 10:05:48 -04002977 spin_lock(&cache->lock);
2978 cache->pinned += len;
2979 cache->space_info->bytes_pinned += len;
2980 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04002981 spin_unlock(&cache->space_info->lock);
Yan324ae4d2007-11-16 14:57:08 -05002982 fs_info->total_pinned += len;
2983 } else {
Josef Bacik25179202008-10-29 14:49:05 -04002984 spin_lock(&cache->space_info->lock);
Zheng Yane8569812008-09-26 10:05:48 -04002985 spin_lock(&cache->lock);
2986 cache->pinned -= len;
2987 cache->space_info->bytes_pinned -= len;
2988 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04002989 spin_unlock(&cache->space_info->lock);
Yan324ae4d2007-11-16 14:57:08 -05002990 fs_info->total_pinned -= len;
Josef Bacik07103a32008-11-19 15:17:55 -05002991 if (cache->cached)
2992 btrfs_add_free_space(cache, bytenr, len);
Yan324ae4d2007-11-16 14:57:08 -05002993 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04002994 btrfs_put_block_group(cache);
Yan324ae4d2007-11-16 14:57:08 -05002995 bytenr += len;
2996 num -= len;
2997 }
2998 return 0;
2999}
Chris Mason9078a3e2007-04-26 16:46:15 -04003000
Zheng Yane8569812008-09-26 10:05:48 -04003001static int update_reserved_extents(struct btrfs_root *root,
3002 u64 bytenr, u64 num, int reserve)
3003{
3004 u64 len;
3005 struct btrfs_block_group_cache *cache;
3006 struct btrfs_fs_info *fs_info = root->fs_info;
3007
Zheng Yane8569812008-09-26 10:05:48 -04003008 while (num > 0) {
3009 cache = btrfs_lookup_block_group(fs_info, bytenr);
3010 BUG_ON(!cache);
3011 len = min(num, cache->key.offset -
3012 (bytenr - cache->key.objectid));
Josef Bacik25179202008-10-29 14:49:05 -04003013
3014 spin_lock(&cache->space_info->lock);
3015 spin_lock(&cache->lock);
Zheng Yane8569812008-09-26 10:05:48 -04003016 if (reserve) {
Zheng Yane8569812008-09-26 10:05:48 -04003017 cache->reserved += len;
3018 cache->space_info->bytes_reserved += len;
Zheng Yane8569812008-09-26 10:05:48 -04003019 } else {
Zheng Yane8569812008-09-26 10:05:48 -04003020 cache->reserved -= len;
3021 cache->space_info->bytes_reserved -= len;
Zheng Yane8569812008-09-26 10:05:48 -04003022 }
Josef Bacik25179202008-10-29 14:49:05 -04003023 spin_unlock(&cache->lock);
3024 spin_unlock(&cache->space_info->lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04003025 btrfs_put_block_group(cache);
Zheng Yane8569812008-09-26 10:05:48 -04003026 bytenr += len;
3027 num -= len;
3028 }
3029 return 0;
3030}
3031
Chris Masond1310b22008-01-24 16:13:08 -05003032int btrfs_copy_pinned(struct btrfs_root *root, struct extent_io_tree *copy)
Chris Masonccd467d2007-06-28 15:57:36 -04003033{
Chris Masonccd467d2007-06-28 15:57:36 -04003034 u64 last = 0;
Chris Mason1a5bc162007-10-15 16:15:26 -04003035 u64 start;
3036 u64 end;
Chris Masond1310b22008-01-24 16:13:08 -05003037 struct extent_io_tree *pinned_extents = &root->fs_info->pinned_extents;
Chris Masonccd467d2007-06-28 15:57:36 -04003038 int ret;
Chris Masonccd467d2007-06-28 15:57:36 -04003039
Chris Masond3977122009-01-05 21:25:51 -05003040 while (1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04003041 ret = find_first_extent_bit(pinned_extents, last,
3042 &start, &end, EXTENT_DIRTY);
3043 if (ret)
Chris Masonccd467d2007-06-28 15:57:36 -04003044 break;
Chris Mason1a5bc162007-10-15 16:15:26 -04003045 set_extent_dirty(copy, start, end, GFP_NOFS);
3046 last = end + 1;
Chris Masonccd467d2007-06-28 15:57:36 -04003047 }
3048 return 0;
3049}
3050
3051int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
3052 struct btrfs_root *root,
Chris Masond1310b22008-01-24 16:13:08 -05003053 struct extent_io_tree *unpin)
Chris Masona28ec192007-03-06 20:08:01 -05003054{
Chris Mason1a5bc162007-10-15 16:15:26 -04003055 u64 start;
3056 u64 end;
Chris Masona28ec192007-03-06 20:08:01 -05003057 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05003058
Chris Masond3977122009-01-05 21:25:51 -05003059 while (1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04003060 ret = find_first_extent_bit(unpin, 0, &start, &end,
3061 EXTENT_DIRTY);
3062 if (ret)
Chris Masona28ec192007-03-06 20:08:01 -05003063 break;
Liu Hui1f3c79a2009-01-05 15:57:51 -05003064
3065 ret = btrfs_discard_extent(root, start, end + 1 - start);
3066
Chris Masonb9473432009-03-13 11:00:37 -04003067 /* unlocks the pinned mutex */
Chris Masone02119d2008-09-05 16:13:11 -04003068 btrfs_update_pinned_extents(root, start, end + 1 - start, 0);
Chris Mason1a5bc162007-10-15 16:15:26 -04003069 clear_extent_dirty(unpin, start, end, GFP_NOFS);
Liu Hui1f3c79a2009-01-05 15:57:51 -05003070
Chris Masonb9473432009-03-13 11:00:37 -04003071 cond_resched();
Chris Masona28ec192007-03-06 20:08:01 -05003072 }
Liu Hui1f3c79a2009-01-05 15:57:51 -05003073 return ret;
Chris Masona28ec192007-03-06 20:08:01 -05003074}
3075
Zheng Yan31840ae2008-09-23 13:14:14 -04003076static int pin_down_bytes(struct btrfs_trans_handle *trans,
3077 struct btrfs_root *root,
Chris Masonb9473432009-03-13 11:00:37 -04003078 struct btrfs_path *path,
3079 u64 bytenr, u64 num_bytes, int is_data,
3080 struct extent_buffer **must_clean)
Chris Masone20d96d2007-03-22 12:13:20 -04003081{
Chris Mason1a5bc162007-10-15 16:15:26 -04003082 int err = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04003083 struct extent_buffer *buf;
Chris Mason78fae272007-03-25 11:35:08 -04003084
Zheng Yan31840ae2008-09-23 13:14:14 -04003085 if (is_data)
3086 goto pinit;
Chris Mason4bef0842008-09-08 11:18:08 -04003087
Zheng Yan31840ae2008-09-23 13:14:14 -04003088 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
3089 if (!buf)
3090 goto pinit;
Chris Mason4bef0842008-09-08 11:18:08 -04003091
Zheng Yan31840ae2008-09-23 13:14:14 -04003092 /* we can reuse a block if it hasn't been written
3093 * and it is from this transaction. We can't
3094 * reuse anything from the tree log root because
3095 * it has tiny sub-transactions.
3096 */
3097 if (btrfs_buffer_uptodate(buf, 0) &&
3098 btrfs_try_tree_lock(buf)) {
3099 u64 header_owner = btrfs_header_owner(buf);
3100 u64 header_transid = btrfs_header_generation(buf);
3101 if (header_owner != BTRFS_TREE_LOG_OBJECTID &&
3102 header_transid == trans->transid &&
3103 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
Chris Masonb9473432009-03-13 11:00:37 -04003104 *must_clean = buf;
Zheng Yan31840ae2008-09-23 13:14:14 -04003105 return 1;
Chris Mason8ef97622007-03-26 10:15:30 -04003106 }
Zheng Yan31840ae2008-09-23 13:14:14 -04003107 btrfs_tree_unlock(buf);
Chris Masonf4b9aa82007-03-27 11:05:53 -04003108 }
Zheng Yan31840ae2008-09-23 13:14:14 -04003109 free_extent_buffer(buf);
3110pinit:
Chris Masonb9473432009-03-13 11:00:37 -04003111 btrfs_set_path_blocking(path);
Chris Masonb9473432009-03-13 11:00:37 -04003112 /* unlocks the pinned mutex */
Zheng Yan31840ae2008-09-23 13:14:14 -04003113 btrfs_update_pinned_extents(root, bytenr, num_bytes, 1);
3114
Chris Masonbe744172007-05-06 10:15:01 -04003115 BUG_ON(err < 0);
Chris Masone20d96d2007-03-22 12:13:20 -04003116 return 0;
3117}
3118
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003119
3120static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
3121 struct btrfs_root *root,
3122 u64 bytenr, u64 num_bytes, u64 parent,
3123 u64 root_objectid, u64 owner_objectid,
3124 u64 owner_offset, int refs_to_drop,
3125 struct btrfs_delayed_extent_op *extent_op)
Chris Masona28ec192007-03-06 20:08:01 -05003126{
Chris Masone2fa7222007-03-12 16:22:34 -04003127 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003128 struct btrfs_path *path;
Chris Mason1261ec42007-03-20 20:35:03 -04003129 struct btrfs_fs_info *info = root->fs_info;
3130 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04003131 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003132 struct btrfs_extent_item *ei;
3133 struct btrfs_extent_inline_ref *iref;
Chris Masona28ec192007-03-06 20:08:01 -05003134 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003135 int is_data;
Chris Mason952fcca2008-02-18 16:33:44 -05003136 int extent_slot = 0;
3137 int found_extent = 0;
3138 int num_to_del = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003139 u32 item_size;
3140 u64 refs;
Chris Mason037e6392007-03-07 11:50:24 -05003141
Chris Mason5caf2a02007-04-02 11:20:42 -04003142 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003143 if (!path)
3144 return -ENOMEM;
3145
Chris Mason3c12ac72008-04-21 12:01:38 -04003146 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04003147 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003148
3149 is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
3150 BUG_ON(!is_data && refs_to_drop != 1);
3151
3152 ret = lookup_extent_backref(trans, extent_root, path, &iref,
3153 bytenr, num_bytes, parent,
3154 root_objectid, owner_objectid,
3155 owner_offset);
Chris Mason7bb86312007-12-11 09:25:06 -05003156 if (ret == 0) {
Chris Mason952fcca2008-02-18 16:33:44 -05003157 extent_slot = path->slots[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003158 while (extent_slot >= 0) {
3159 btrfs_item_key_to_cpu(path->nodes[0], &key,
Chris Mason952fcca2008-02-18 16:33:44 -05003160 extent_slot);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003161 if (key.objectid != bytenr)
Chris Mason952fcca2008-02-18 16:33:44 -05003162 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003163 if (key.type == BTRFS_EXTENT_ITEM_KEY &&
3164 key.offset == num_bytes) {
Chris Mason952fcca2008-02-18 16:33:44 -05003165 found_extent = 1;
3166 break;
3167 }
3168 if (path->slots[0] - extent_slot > 5)
3169 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003170 extent_slot--;
Chris Mason952fcca2008-02-18 16:33:44 -05003171 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003172#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3173 item_size = btrfs_item_size_nr(path->nodes[0], extent_slot);
3174 if (found_extent && item_size < sizeof(*ei))
3175 found_extent = 0;
3176#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04003177 if (!found_extent) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003178 BUG_ON(iref);
Chris Mason56bec292009-03-13 10:10:06 -04003179 ret = remove_extent_backref(trans, extent_root, path,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003180 NULL, refs_to_drop,
3181 is_data);
Zheng Yan31840ae2008-09-23 13:14:14 -04003182 BUG_ON(ret);
3183 btrfs_release_path(extent_root, path);
Chris Masonb9473432009-03-13 11:00:37 -04003184 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003185
3186 key.objectid = bytenr;
3187 key.type = BTRFS_EXTENT_ITEM_KEY;
3188 key.offset = num_bytes;
3189
Zheng Yan31840ae2008-09-23 13:14:14 -04003190 ret = btrfs_search_slot(trans, extent_root,
3191 &key, path, -1, 1);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003192 if (ret) {
3193 printk(KERN_ERR "umm, got %d back from search"
Chris Masond3977122009-01-05 21:25:51 -05003194 ", was looking for %llu\n", ret,
3195 (unsigned long long)bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003196 btrfs_print_leaf(extent_root, path->nodes[0]);
3197 }
Zheng Yan31840ae2008-09-23 13:14:14 -04003198 BUG_ON(ret);
3199 extent_slot = path->slots[0];
3200 }
Chris Mason7bb86312007-12-11 09:25:06 -05003201 } else {
3202 btrfs_print_leaf(extent_root, path->nodes[0]);
3203 WARN_ON(1);
Chris Masond3977122009-01-05 21:25:51 -05003204 printk(KERN_ERR "btrfs unable to find ref byte nr %llu "
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003205 "parent %llu root %llu owner %llu offset %llu\n",
Chris Masond3977122009-01-05 21:25:51 -05003206 (unsigned long long)bytenr,
Chris Mason56bec292009-03-13 10:10:06 -04003207 (unsigned long long)parent,
Chris Masond3977122009-01-05 21:25:51 -05003208 (unsigned long long)root_objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003209 (unsigned long long)owner_objectid,
3210 (unsigned long long)owner_offset);
Chris Mason7bb86312007-12-11 09:25:06 -05003211 }
Chris Mason5f39d392007-10-15 16:14:19 -04003212
3213 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003214 item_size = btrfs_item_size_nr(leaf, extent_slot);
3215#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3216 if (item_size < sizeof(*ei)) {
3217 BUG_ON(found_extent || extent_slot != path->slots[0]);
3218 ret = convert_extent_item_v0(trans, extent_root, path,
3219 owner_objectid, 0);
3220 BUG_ON(ret < 0);
3221
3222 btrfs_release_path(extent_root, path);
3223 path->leave_spinning = 1;
3224
3225 key.objectid = bytenr;
3226 key.type = BTRFS_EXTENT_ITEM_KEY;
3227 key.offset = num_bytes;
3228
3229 ret = btrfs_search_slot(trans, extent_root, &key, path,
3230 -1, 1);
3231 if (ret) {
3232 printk(KERN_ERR "umm, got %d back from search"
3233 ", was looking for %llu\n", ret,
3234 (unsigned long long)bytenr);
3235 btrfs_print_leaf(extent_root, path->nodes[0]);
3236 }
3237 BUG_ON(ret);
3238 extent_slot = path->slots[0];
3239 leaf = path->nodes[0];
3240 item_size = btrfs_item_size_nr(leaf, extent_slot);
3241 }
3242#endif
3243 BUG_ON(item_size < sizeof(*ei));
Chris Mason952fcca2008-02-18 16:33:44 -05003244 ei = btrfs_item_ptr(leaf, extent_slot,
Chris Mason123abc82007-03-14 14:14:43 -04003245 struct btrfs_extent_item);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003246 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
3247 struct btrfs_tree_block_info *bi;
3248 BUG_ON(item_size < sizeof(*ei) + sizeof(*bi));
3249 bi = (struct btrfs_tree_block_info *)(ei + 1);
3250 WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
Chris Mason952fcca2008-02-18 16:33:44 -05003251 }
3252
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003253 refs = btrfs_extent_refs(leaf, ei);
3254 BUG_ON(refs < refs_to_drop);
3255 refs -= refs_to_drop;
3256
3257 if (refs > 0) {
3258 if (extent_op)
3259 __run_delayed_extent_op(extent_op, leaf, ei);
3260 /*
3261 * In the case of inline back ref, reference count will
3262 * be updated by remove_extent_backref
3263 */
3264 if (iref) {
3265 BUG_ON(!found_extent);
3266 } else {
3267 btrfs_set_extent_refs(leaf, ei, refs);
3268 btrfs_mark_buffer_dirty(leaf);
3269 }
3270 if (found_extent) {
3271 ret = remove_extent_backref(trans, extent_root, path,
3272 iref, refs_to_drop,
3273 is_data);
3274 BUG_ON(ret);
3275 }
3276 } else {
3277 int mark_free = 0;
Chris Masonb9473432009-03-13 11:00:37 -04003278 struct extent_buffer *must_clean = NULL;
Chris Mason78fae272007-03-25 11:35:08 -04003279
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003280 if (found_extent) {
3281 BUG_ON(is_data && refs_to_drop !=
3282 extent_data_ref_count(root, path, iref));
3283 if (iref) {
3284 BUG_ON(path->slots[0] != extent_slot);
3285 } else {
3286 BUG_ON(path->slots[0] != extent_slot + 1);
3287 path->slots[0] = extent_slot;
3288 num_to_del = 2;
3289 }
Chris Mason78fae272007-03-25 11:35:08 -04003290 }
Chris Masonb9473432009-03-13 11:00:37 -04003291
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003292 ret = pin_down_bytes(trans, root, path, bytenr,
3293 num_bytes, is_data, &must_clean);
3294 if (ret > 0)
3295 mark_free = 1;
3296 BUG_ON(ret < 0);
Chris Masonb9473432009-03-13 11:00:37 -04003297 /*
3298 * it is going to be very rare for someone to be waiting
3299 * on the block we're freeing. del_items might need to
3300 * schedule, so rather than get fancy, just force it
3301 * to blocking here
3302 */
3303 if (must_clean)
3304 btrfs_set_lock_blocking(must_clean);
3305
Chris Mason952fcca2008-02-18 16:33:44 -05003306 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
3307 num_to_del);
Zheng Yan31840ae2008-09-23 13:14:14 -04003308 BUG_ON(ret);
Josef Bacik25179202008-10-29 14:49:05 -04003309 btrfs_release_path(extent_root, path);
David Woodhouse21af8042008-08-12 14:13:26 +01003310
Chris Masonb9473432009-03-13 11:00:37 -04003311 if (must_clean) {
3312 clean_tree_block(NULL, root, must_clean);
3313 btrfs_tree_unlock(must_clean);
3314 free_extent_buffer(must_clean);
3315 }
3316
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003317 if (is_data) {
Chris Mason459931e2008-12-10 09:10:46 -05003318 ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
3319 BUG_ON(ret);
Chris Masond57e62b2009-03-31 13:47:50 -04003320 } else {
3321 invalidate_mapping_pages(info->btree_inode->i_mapping,
3322 bytenr >> PAGE_CACHE_SHIFT,
3323 (bytenr + num_bytes - 1) >> PAGE_CACHE_SHIFT);
Chris Mason459931e2008-12-10 09:10:46 -05003324 }
3325
Chris Masondcbdd4d2008-12-16 13:51:01 -05003326 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
3327 mark_free);
3328 BUG_ON(ret);
Chris Masona28ec192007-03-06 20:08:01 -05003329 }
Chris Mason5caf2a02007-04-02 11:20:42 -04003330 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -05003331 return ret;
3332}
3333
3334/*
Chris Mason1887be62009-03-13 10:11:24 -04003335 * when we free an extent, it is possible (and likely) that we free the last
3336 * delayed ref for that extent as well. This searches the delayed ref tree for
3337 * a given extent, and if there are no other delayed refs to be processed, it
3338 * removes it from the tree.
3339 */
3340static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
3341 struct btrfs_root *root, u64 bytenr)
3342{
3343 struct btrfs_delayed_ref_head *head;
3344 struct btrfs_delayed_ref_root *delayed_refs;
3345 struct btrfs_delayed_ref_node *ref;
3346 struct rb_node *node;
3347 int ret;
3348
3349 delayed_refs = &trans->transaction->delayed_refs;
3350 spin_lock(&delayed_refs->lock);
3351 head = btrfs_find_delayed_ref_head(trans, bytenr);
3352 if (!head)
3353 goto out;
3354
3355 node = rb_prev(&head->node.rb_node);
3356 if (!node)
3357 goto out;
3358
3359 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
3360
3361 /* there are still entries for this ref, we can't drop it */
3362 if (ref->bytenr == bytenr)
3363 goto out;
3364
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003365 if (head->extent_op) {
3366 if (!head->must_insert_reserved)
3367 goto out;
3368 kfree(head->extent_op);
3369 head->extent_op = NULL;
3370 }
3371
Chris Mason1887be62009-03-13 10:11:24 -04003372 /*
3373 * waiting for the lock here would deadlock. If someone else has it
3374 * locked they are already in the process of dropping it anyway
3375 */
3376 if (!mutex_trylock(&head->mutex))
3377 goto out;
3378
3379 /*
3380 * at this point we have a head with no other entries. Go
3381 * ahead and process it.
3382 */
3383 head->node.in_tree = 0;
3384 rb_erase(&head->node.rb_node, &delayed_refs->root);
Chris Masonc3e69d52009-03-13 10:17:05 -04003385
Chris Mason1887be62009-03-13 10:11:24 -04003386 delayed_refs->num_entries--;
3387
3388 /*
3389 * we don't take a ref on the node because we're removing it from the
3390 * tree, so we just steal the ref the tree was holding.
3391 */
Chris Masonc3e69d52009-03-13 10:17:05 -04003392 delayed_refs->num_heads--;
3393 if (list_empty(&head->cluster))
3394 delayed_refs->num_heads_ready--;
3395
3396 list_del_init(&head->cluster);
Chris Mason1887be62009-03-13 10:11:24 -04003397 spin_unlock(&delayed_refs->lock);
3398
3399 ret = run_one_delayed_ref(trans, root->fs_info->tree_root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003400 &head->node, head->extent_op,
3401 head->must_insert_reserved);
Chris Mason1887be62009-03-13 10:11:24 -04003402 BUG_ON(ret);
3403 btrfs_put_delayed_ref(&head->node);
3404 return 0;
3405out:
3406 spin_unlock(&delayed_refs->lock);
3407 return 0;
3408}
3409
Chris Mason925baed2008-06-25 16:01:30 -04003410int btrfs_free_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04003411 struct btrfs_root *root,
3412 u64 bytenr, u64 num_bytes, u64 parent,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003413 u64 root_objectid, u64 owner, u64 offset)
Chris Mason925baed2008-06-25 16:01:30 -04003414{
3415 int ret;
3416
Chris Mason56bec292009-03-13 10:10:06 -04003417 /*
3418 * tree log blocks never actually go into the extent allocation
3419 * tree, just update pinning info and exit early.
Chris Mason56bec292009-03-13 10:10:06 -04003420 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003421 if (root_objectid == BTRFS_TREE_LOG_OBJECTID) {
3422 WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID);
Chris Masonb9473432009-03-13 11:00:37 -04003423 /* unlocks the pinned mutex */
Chris Mason56bec292009-03-13 10:10:06 -04003424 btrfs_update_pinned_extents(root, bytenr, num_bytes, 1);
Chris Mason56bec292009-03-13 10:10:06 -04003425 update_reserved_extents(root, bytenr, num_bytes, 0);
3426 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003427 } else if (owner < BTRFS_FIRST_FREE_OBJECTID) {
3428 ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes,
3429 parent, root_objectid, (int)owner,
3430 BTRFS_DROP_DELAYED_REF, NULL);
Chris Mason1887be62009-03-13 10:11:24 -04003431 BUG_ON(ret);
3432 ret = check_ref_cleanup(trans, root, bytenr);
3433 BUG_ON(ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003434 } else {
3435 ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes,
3436 parent, root_objectid, owner,
3437 offset, BTRFS_DROP_DELAYED_REF, NULL);
3438 BUG_ON(ret);
Chris Mason56bec292009-03-13 10:10:06 -04003439 }
Chris Mason925baed2008-06-25 16:01:30 -04003440 return ret;
3441}
3442
Chris Mason87ee04e2007-11-30 11:30:34 -05003443static u64 stripe_align(struct btrfs_root *root, u64 val)
3444{
3445 u64 mask = ((u64)root->stripesize - 1);
3446 u64 ret = (val + mask) & ~mask;
3447 return ret;
3448}
3449
Chris Masonfec577f2007-02-26 10:40:21 -05003450/*
3451 * walks the btree of allocated extents and find a hole of a given size.
3452 * The key ins is changed to record the hole:
3453 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -04003454 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -05003455 * ins->offset == number of blocks
3456 * Any available blocks before search_start are skipped.
3457 */
Chris Masond3977122009-01-05 21:25:51 -05003458static noinline int find_free_extent(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05003459 struct btrfs_root *orig_root,
3460 u64 num_bytes, u64 empty_size,
3461 u64 search_start, u64 search_end,
3462 u64 hint_byte, struct btrfs_key *ins,
3463 u64 exclude_start, u64 exclude_nr,
3464 int data)
Chris Masonfec577f2007-02-26 10:40:21 -05003465{
Josef Bacik80eb2342008-10-29 14:49:05 -04003466 int ret = 0;
Chris Masond3977122009-01-05 21:25:51 -05003467 struct btrfs_root *root = orig_root->fs_info->extent_root;
Chris Masonfa9c0d792009-04-03 09:47:43 -04003468 struct btrfs_free_cluster *last_ptr = NULL;
Josef Bacik80eb2342008-10-29 14:49:05 -04003469 struct btrfs_block_group_cache *block_group = NULL;
Chris Mason239b14b2008-03-24 15:02:07 -04003470 int empty_cluster = 2 * 1024 * 1024;
Chris Mason0ef3e662008-05-24 14:04:53 -04003471 int allowed_chunk_alloc = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04003472 struct btrfs_space_info *space_info;
Chris Masonfa9c0d792009-04-03 09:47:43 -04003473 int last_ptr_loop = 0;
3474 int loop = 0;
Chris Masonfec577f2007-02-26 10:40:21 -05003475
Chris Masondb945352007-10-15 16:15:53 -04003476 WARN_ON(num_bytes < root->sectorsize);
Chris Masonb1a4d962007-04-04 15:27:52 -04003477 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
Josef Bacik80eb2342008-10-29 14:49:05 -04003478 ins->objectid = 0;
3479 ins->offset = 0;
Chris Masonb1a4d962007-04-04 15:27:52 -04003480
Josef Bacik2552d172009-04-03 10:14:19 -04003481 space_info = __find_space_info(root->fs_info, data);
3482
Chris Mason0ef3e662008-05-24 14:04:53 -04003483 if (orig_root->ref_cows || empty_size)
3484 allowed_chunk_alloc = 1;
3485
Chris Mason239b14b2008-03-24 15:02:07 -04003486 if (data & BTRFS_BLOCK_GROUP_METADATA) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04003487 last_ptr = &root->fs_info->meta_alloc_cluster;
Chris Mason536ac8a2009-02-12 09:41:38 -05003488 if (!btrfs_test_opt(root, SSD))
3489 empty_cluster = 64 * 1024;
Chris Mason239b14b2008-03-24 15:02:07 -04003490 }
3491
Chris Masonfa9c0d792009-04-03 09:47:43 -04003492 if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD)) {
3493 last_ptr = &root->fs_info->data_alloc_cluster;
3494 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04003495
Chris Mason239b14b2008-03-24 15:02:07 -04003496 if (last_ptr) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04003497 spin_lock(&last_ptr->lock);
3498 if (last_ptr->block_group)
3499 hint_byte = last_ptr->window_start;
3500 spin_unlock(&last_ptr->lock);
Chris Mason239b14b2008-03-24 15:02:07 -04003501 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04003502
Chris Masona061fc82008-05-07 11:43:44 -04003503 search_start = max(search_start, first_logical_byte(root, 0));
Chris Mason239b14b2008-03-24 15:02:07 -04003504 search_start = max(search_start, hint_byte);
Chris Mason0b86a832008-03-24 15:01:56 -04003505
Chris Masonfa9c0d792009-04-03 09:47:43 -04003506 if (!last_ptr) {
3507 empty_cluster = 0;
3508 loop = 1;
3509 }
3510
Josef Bacik2552d172009-04-03 10:14:19 -04003511 if (search_start == hint_byte) {
Josef Bacik2552d172009-04-03 10:14:19 -04003512 block_group = btrfs_lookup_block_group(root->fs_info,
3513 search_start);
3514 if (block_group && block_group_bits(block_group, data)) {
Josef Bacik2552d172009-04-03 10:14:19 -04003515 down_read(&space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04003516 if (list_empty(&block_group->list) ||
3517 block_group->ro) {
3518 /*
3519 * someone is removing this block group,
3520 * we can't jump into the have_block_group
3521 * target because our list pointers are not
3522 * valid
3523 */
3524 btrfs_put_block_group(block_group);
3525 up_read(&space_info->groups_sem);
3526 } else
3527 goto have_block_group;
Josef Bacik2552d172009-04-03 10:14:19 -04003528 } else if (block_group) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04003529 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04003530 }
Chris Mason42e70e72008-11-07 18:17:11 -05003531 }
Chris Mason43662112008-11-07 09:06:11 -05003532
Josef Bacik2552d172009-04-03 10:14:19 -04003533search:
Josef Bacik80eb2342008-10-29 14:49:05 -04003534 down_read(&space_info->groups_sem);
Josef Bacik2552d172009-04-03 10:14:19 -04003535 list_for_each_entry(block_group, &space_info->block_groups, list) {
Josef Bacik6226cb0a2009-04-03 10:14:18 -04003536 u64 offset;
Chris Mason8a1413a2008-11-10 16:13:54 -05003537
Josef Bacik2552d172009-04-03 10:14:19 -04003538 atomic_inc(&block_group->count);
3539 search_start = block_group->key.objectid;
Chris Mason42e70e72008-11-07 18:17:11 -05003540
Josef Bacik2552d172009-04-03 10:14:19 -04003541have_block_group:
Josef Bacikea6a4782008-11-20 12:16:16 -05003542 if (unlikely(!block_group->cached)) {
3543 mutex_lock(&block_group->cache_mutex);
3544 ret = cache_block_group(root, block_group);
3545 mutex_unlock(&block_group->cache_mutex);
Josef Bacik2552d172009-04-03 10:14:19 -04003546 if (ret) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04003547 btrfs_put_block_group(block_group);
Josef Bacikea6a4782008-11-20 12:16:16 -05003548 break;
Josef Bacik2552d172009-04-03 10:14:19 -04003549 }
Josef Bacikea6a4782008-11-20 12:16:16 -05003550 }
3551
Josef Bacikea6a4782008-11-20 12:16:16 -05003552 if (unlikely(block_group->ro))
Josef Bacik2552d172009-04-03 10:14:19 -04003553 goto loop;
Josef Bacik0f9dd462008-09-23 13:14:11 -04003554
Chris Masonfa9c0d792009-04-03 09:47:43 -04003555 if (last_ptr) {
3556 /*
3557 * the refill lock keeps out other
3558 * people trying to start a new cluster
3559 */
3560 spin_lock(&last_ptr->refill_lock);
Chris Mason44fb5512009-06-04 15:34:51 -04003561 if (last_ptr->block_group &&
3562 (last_ptr->block_group->ro ||
3563 !block_group_bits(last_ptr->block_group, data))) {
3564 offset = 0;
3565 goto refill_cluster;
3566 }
3567
Chris Masonfa9c0d792009-04-03 09:47:43 -04003568 offset = btrfs_alloc_from_cluster(block_group, last_ptr,
3569 num_bytes, search_start);
3570 if (offset) {
3571 /* we have a block, we're done */
3572 spin_unlock(&last_ptr->refill_lock);
3573 goto checks;
3574 }
3575
3576 spin_lock(&last_ptr->lock);
3577 /*
3578 * whoops, this cluster doesn't actually point to
3579 * this block group. Get a ref on the block
3580 * group is does point to and try again
3581 */
3582 if (!last_ptr_loop && last_ptr->block_group &&
3583 last_ptr->block_group != block_group) {
3584
3585 btrfs_put_block_group(block_group);
3586 block_group = last_ptr->block_group;
3587 atomic_inc(&block_group->count);
3588 spin_unlock(&last_ptr->lock);
3589 spin_unlock(&last_ptr->refill_lock);
3590
3591 last_ptr_loop = 1;
3592 search_start = block_group->key.objectid;
Chris Mason44fb5512009-06-04 15:34:51 -04003593 /*
3594 * we know this block group is properly
3595 * in the list because
3596 * btrfs_remove_block_group, drops the
3597 * cluster before it removes the block
3598 * group from the list
3599 */
Chris Masonfa9c0d792009-04-03 09:47:43 -04003600 goto have_block_group;
3601 }
3602 spin_unlock(&last_ptr->lock);
Chris Mason44fb5512009-06-04 15:34:51 -04003603refill_cluster:
Chris Masonfa9c0d792009-04-03 09:47:43 -04003604 /*
3605 * this cluster didn't work out, free it and
3606 * start over
3607 */
3608 btrfs_return_cluster_to_free_space(NULL, last_ptr);
3609
3610 last_ptr_loop = 0;
3611
3612 /* allocate a cluster in this block group */
Chris Mason451d7582009-06-09 20:28:34 -04003613 ret = btrfs_find_space_cluster(trans, root,
Chris Masonfa9c0d792009-04-03 09:47:43 -04003614 block_group, last_ptr,
3615 offset, num_bytes,
3616 empty_cluster + empty_size);
3617 if (ret == 0) {
3618 /*
3619 * now pull our allocation out of this
3620 * cluster
3621 */
3622 offset = btrfs_alloc_from_cluster(block_group,
3623 last_ptr, num_bytes,
3624 search_start);
3625 if (offset) {
3626 /* we found one, proceed */
3627 spin_unlock(&last_ptr->refill_lock);
3628 goto checks;
3629 }
3630 }
3631 /*
3632 * at this point we either didn't find a cluster
3633 * or we weren't able to allocate a block from our
3634 * cluster. Free the cluster we've been trying
3635 * to use, and go to the next block group
3636 */
3637 if (loop < 2) {
3638 btrfs_return_cluster_to_free_space(NULL,
3639 last_ptr);
3640 spin_unlock(&last_ptr->refill_lock);
3641 goto loop;
3642 }
3643 spin_unlock(&last_ptr->refill_lock);
3644 }
3645
Josef Bacik6226cb0a2009-04-03 10:14:18 -04003646 offset = btrfs_find_space_for_alloc(block_group, search_start,
3647 num_bytes, empty_size);
3648 if (!offset)
Josef Bacik2552d172009-04-03 10:14:19 -04003649 goto loop;
Chris Masonfa9c0d792009-04-03 09:47:43 -04003650checks:
Josef Bacik6226cb0a2009-04-03 10:14:18 -04003651 search_start = stripe_align(root, offset);
Chris Mason0b86a832008-03-24 15:01:56 -04003652
Josef Bacik2552d172009-04-03 10:14:19 -04003653 /* move on to the next group */
Josef Bacik6226cb0a2009-04-03 10:14:18 -04003654 if (search_start + num_bytes >= search_end) {
3655 btrfs_add_free_space(block_group, offset, num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04003656 goto loop;
Josef Bacik6226cb0a2009-04-03 10:14:18 -04003657 }
Chris Masone37c9e62007-05-09 20:13:14 -04003658
Josef Bacik2552d172009-04-03 10:14:19 -04003659 /* move on to the next group */
3660 if (search_start + num_bytes >
Josef Bacik6226cb0a2009-04-03 10:14:18 -04003661 block_group->key.objectid + block_group->key.offset) {
3662 btrfs_add_free_space(block_group, offset, num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04003663 goto loop;
Josef Bacik6226cb0a2009-04-03 10:14:18 -04003664 }
Josef Bacik80eb2342008-10-29 14:49:05 -04003665
Josef Bacik2552d172009-04-03 10:14:19 -04003666 if (exclude_nr > 0 &&
3667 (search_start + num_bytes > exclude_start &&
3668 search_start < exclude_start + exclude_nr)) {
3669 search_start = exclude_start + exclude_nr;
3670
Josef Bacik6226cb0a2009-04-03 10:14:18 -04003671 btrfs_add_free_space(block_group, offset, num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04003672 /*
3673 * if search_start is still in this block group
3674 * then we just re-search this block group
3675 */
3676 if (search_start >= block_group->key.objectid &&
3677 search_start < (block_group->key.objectid +
Josef Bacik6226cb0a2009-04-03 10:14:18 -04003678 block_group->key.offset))
Josef Bacik2552d172009-04-03 10:14:19 -04003679 goto have_block_group;
Josef Bacik2552d172009-04-03 10:14:19 -04003680 goto loop;
Josef Bacik0f9dd462008-09-23 13:14:11 -04003681 }
Josef Bacik2552d172009-04-03 10:14:19 -04003682
3683 ins->objectid = search_start;
3684 ins->offset = num_bytes;
3685
Josef Bacik6226cb0a2009-04-03 10:14:18 -04003686 if (offset < search_start)
3687 btrfs_add_free_space(block_group, offset,
3688 search_start - offset);
3689 BUG_ON(offset > search_start);
3690
Josef Bacik2552d172009-04-03 10:14:19 -04003691 /* we are all good, lets return */
Josef Bacik2552d172009-04-03 10:14:19 -04003692 break;
3693loop:
Chris Masonfa9c0d792009-04-03 09:47:43 -04003694 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04003695 }
3696 up_read(&space_info->groups_sem);
Chris Masonf5a31e12008-11-10 11:47:09 -05003697
Chris Masonfa9c0d792009-04-03 09:47:43 -04003698 /* loop == 0, try to find a clustered alloc in every block group
3699 * loop == 1, try again after forcing a chunk allocation
3700 * loop == 2, set empty_size and empty_cluster to 0 and try again
3701 */
3702 if (!ins->objectid && loop < 3 &&
3703 (empty_size || empty_cluster || allowed_chunk_alloc)) {
3704 if (loop >= 2) {
3705 empty_size = 0;
3706 empty_cluster = 0;
3707 }
Chris Mason42e70e72008-11-07 18:17:11 -05003708
Josef Bacik2552d172009-04-03 10:14:19 -04003709 if (allowed_chunk_alloc) {
3710 ret = do_chunk_alloc(trans, root, num_bytes +
3711 2 * 1024 * 1024, data, 1);
Josef Bacik2552d172009-04-03 10:14:19 -04003712 allowed_chunk_alloc = 0;
3713 } else {
3714 space_info->force_alloc = 1;
Josef Bacik0f9dd462008-09-23 13:14:11 -04003715 }
Josef Bacik80eb2342008-10-29 14:49:05 -04003716
Chris Masonfa9c0d792009-04-03 09:47:43 -04003717 if (loop < 3) {
3718 loop++;
Josef Bacik2552d172009-04-03 10:14:19 -04003719 goto search;
Chris Masonfa9c0d792009-04-03 09:47:43 -04003720 }
Josef Bacik2552d172009-04-03 10:14:19 -04003721 ret = -ENOSPC;
3722 } else if (!ins->objectid) {
3723 ret = -ENOSPC;
Chris Masone19caa52007-10-15 16:17:44 -04003724 }
Chris Mason0b86a832008-03-24 15:01:56 -04003725
Josef Bacik80eb2342008-10-29 14:49:05 -04003726 /* we found what we needed */
3727 if (ins->objectid) {
3728 if (!(data & BTRFS_BLOCK_GROUP_DATA))
Yan Zhengd2fb3432008-12-11 16:30:39 -05003729 trans->block_group = block_group->key.objectid;
Josef Bacik80eb2342008-10-29 14:49:05 -04003730
Chris Masonfa9c0d792009-04-03 09:47:43 -04003731 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04003732 ret = 0;
3733 }
Chris Mason0b86a832008-03-24 15:01:56 -04003734
Chris Mason0f70abe2007-02-28 16:46:22 -05003735 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05003736}
Chris Masonec44a352008-04-28 15:29:52 -04003737
Josef Bacik0f9dd462008-09-23 13:14:11 -04003738static void dump_space_info(struct btrfs_space_info *info, u64 bytes)
3739{
3740 struct btrfs_block_group_cache *cache;
Josef Bacik0f9dd462008-09-23 13:14:11 -04003741
Chris Masond3977122009-01-05 21:25:51 -05003742 printk(KERN_INFO "space_info has %llu free, is %sfull\n",
3743 (unsigned long long)(info->total_bytes - info->bytes_used -
3744 info->bytes_pinned - info->bytes_reserved),
3745 (info->full) ? "" : "not ");
Josef Bacik6a632092009-02-20 11:00:09 -05003746 printk(KERN_INFO "space_info total=%llu, pinned=%llu, delalloc=%llu,"
Joel Becker21380932009-04-21 12:38:29 -07003747 " may_use=%llu, used=%llu\n",
3748 (unsigned long long)info->total_bytes,
3749 (unsigned long long)info->bytes_pinned,
3750 (unsigned long long)info->bytes_delalloc,
3751 (unsigned long long)info->bytes_may_use,
3752 (unsigned long long)info->bytes_used);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003753
Josef Bacik80eb2342008-10-29 14:49:05 -04003754 down_read(&info->groups_sem);
Qinghuang Fengc6e30872009-01-21 10:59:08 -05003755 list_for_each_entry(cache, &info->block_groups, list) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04003756 spin_lock(&cache->lock);
Chris Masond3977122009-01-05 21:25:51 -05003757 printk(KERN_INFO "block group %llu has %llu bytes, %llu used "
3758 "%llu pinned %llu reserved\n",
3759 (unsigned long long)cache->key.objectid,
3760 (unsigned long long)cache->key.offset,
3761 (unsigned long long)btrfs_block_group_used(&cache->item),
3762 (unsigned long long)cache->pinned,
3763 (unsigned long long)cache->reserved);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003764 btrfs_dump_free_space(cache, bytes);
3765 spin_unlock(&cache->lock);
3766 }
Josef Bacik80eb2342008-10-29 14:49:05 -04003767 up_read(&info->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003768}
Zheng Yane8569812008-09-26 10:05:48 -04003769
Chris Masone6dcd2d2008-07-17 12:53:50 -04003770static int __btrfs_reserve_extent(struct btrfs_trans_handle *trans,
3771 struct btrfs_root *root,
3772 u64 num_bytes, u64 min_alloc_size,
3773 u64 empty_size, u64 hint_byte,
3774 u64 search_end, struct btrfs_key *ins,
3775 u64 data)
Chris Masonfec577f2007-02-26 10:40:21 -05003776{
3777 int ret;
Chris Masonfbdc7622007-05-30 10:22:12 -04003778 u64 search_start = 0;
Chris Mason1261ec42007-03-20 20:35:03 -04003779 struct btrfs_fs_info *info = root->fs_info;
Chris Mason925baed2008-06-25 16:01:30 -04003780
Josef Bacik6a632092009-02-20 11:00:09 -05003781 data = btrfs_get_alloc_profile(root, data);
Chris Mason98d20f62008-04-14 09:46:10 -04003782again:
Chris Mason0ef3e662008-05-24 14:04:53 -04003783 /*
3784 * the only place that sets empty_size is btrfs_realloc_node, which
3785 * is not called recursively on allocations
3786 */
3787 if (empty_size || root->ref_cows) {
Chris Mason593060d2008-03-25 16:50:33 -04003788 if (!(data & BTRFS_BLOCK_GROUP_METADATA)) {
Chris Mason6324fbf2008-03-24 15:01:59 -04003789 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0ef3e662008-05-24 14:04:53 -04003790 2 * 1024 * 1024,
3791 BTRFS_BLOCK_GROUP_METADATA |
3792 (info->metadata_alloc_profile &
3793 info->avail_metadata_alloc_bits), 0);
Chris Mason6324fbf2008-03-24 15:01:59 -04003794 }
3795 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0ef3e662008-05-24 14:04:53 -04003796 num_bytes + 2 * 1024 * 1024, data, 0);
Chris Mason6324fbf2008-03-24 15:01:59 -04003797 }
Chris Mason0b86a832008-03-24 15:01:56 -04003798
Chris Masondb945352007-10-15 16:15:53 -04003799 WARN_ON(num_bytes < root->sectorsize);
3800 ret = find_free_extent(trans, root, num_bytes, empty_size,
3801 search_start, search_end, hint_byte, ins,
Chris Mason26b80032007-08-08 20:17:12 -04003802 trans->alloc_exclude_start,
3803 trans->alloc_exclude_nr, data);
Chris Mason3b951512008-04-17 11:29:12 -04003804
Chris Mason98d20f62008-04-14 09:46:10 -04003805 if (ret == -ENOSPC && num_bytes > min_alloc_size) {
3806 num_bytes = num_bytes >> 1;
Josef Bacik0f9dd462008-09-23 13:14:11 -04003807 num_bytes = num_bytes & ~(root->sectorsize - 1);
Chris Mason98d20f62008-04-14 09:46:10 -04003808 num_bytes = max(num_bytes, min_alloc_size);
Chris Mason0ef3e662008-05-24 14:04:53 -04003809 do_chunk_alloc(trans, root->fs_info->extent_root,
3810 num_bytes, data, 1);
Chris Mason98d20f62008-04-14 09:46:10 -04003811 goto again;
3812 }
Chris Masonec44a352008-04-28 15:29:52 -04003813 if (ret) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04003814 struct btrfs_space_info *sinfo;
3815
3816 sinfo = __find_space_info(root->fs_info, data);
Chris Masond3977122009-01-05 21:25:51 -05003817 printk(KERN_ERR "btrfs allocation failed flags %llu, "
3818 "wanted %llu\n", (unsigned long long)data,
3819 (unsigned long long)num_bytes);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003820 dump_space_info(sinfo, num_bytes);
Chris Mason925baed2008-06-25 16:01:30 -04003821 BUG();
Chris Mason925baed2008-06-25 16:01:30 -04003822 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04003823
3824 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003825}
3826
Chris Mason65b51a02008-08-01 15:11:20 -04003827int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len)
3828{
Josef Bacik0f9dd462008-09-23 13:14:11 -04003829 struct btrfs_block_group_cache *cache;
Liu Hui1f3c79a2009-01-05 15:57:51 -05003830 int ret = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04003831
Josef Bacik0f9dd462008-09-23 13:14:11 -04003832 cache = btrfs_lookup_block_group(root->fs_info, start);
3833 if (!cache) {
Chris Masond3977122009-01-05 21:25:51 -05003834 printk(KERN_ERR "Unable to find block group for %llu\n",
3835 (unsigned long long)start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003836 return -ENOSPC;
3837 }
Liu Hui1f3c79a2009-01-05 15:57:51 -05003838
3839 ret = btrfs_discard_extent(root, start, len);
3840
Josef Bacik0f9dd462008-09-23 13:14:11 -04003841 btrfs_add_free_space(cache, start, len);
Chris Masonfa9c0d792009-04-03 09:47:43 -04003842 btrfs_put_block_group(cache);
Zheng Yan1a40e232008-09-26 10:09:34 -04003843 update_reserved_extents(root, start, len, 0);
Liu Hui1f3c79a2009-01-05 15:57:51 -05003844
3845 return ret;
Chris Mason65b51a02008-08-01 15:11:20 -04003846}
3847
Chris Masone6dcd2d2008-07-17 12:53:50 -04003848int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
3849 struct btrfs_root *root,
3850 u64 num_bytes, u64 min_alloc_size,
3851 u64 empty_size, u64 hint_byte,
3852 u64 search_end, struct btrfs_key *ins,
3853 u64 data)
3854{
3855 int ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003856 ret = __btrfs_reserve_extent(trans, root, num_bytes, min_alloc_size,
3857 empty_size, hint_byte, search_end, ins,
3858 data);
Zheng Yane8569812008-09-26 10:05:48 -04003859 update_reserved_extents(root, ins->objectid, ins->offset, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003860 return ret;
3861}
3862
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003863static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
3864 struct btrfs_root *root,
3865 u64 parent, u64 root_objectid,
3866 u64 flags, u64 owner, u64 offset,
3867 struct btrfs_key *ins, int ref_mod)
Chris Masone6dcd2d2008-07-17 12:53:50 -04003868{
3869 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003870 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003871 struct btrfs_extent_item *extent_item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003872 struct btrfs_extent_inline_ref *iref;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003873 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003874 struct extent_buffer *leaf;
3875 int type;
3876 u32 size;
Chris Masonf2654de2007-06-26 12:20:46 -04003877
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003878 if (parent > 0)
3879 type = BTRFS_SHARED_DATA_REF_KEY;
3880 else
3881 type = BTRFS_EXTENT_DATA_REF_KEY;
Zheng Yan31840ae2008-09-23 13:14:14 -04003882
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003883 size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type);
Chris Mason7bb86312007-12-11 09:25:06 -05003884
3885 path = btrfs_alloc_path();
3886 BUG_ON(!path);
Chris Mason47e4bb92008-02-01 14:51:59 -05003887
Chris Masonb9473432009-03-13 11:00:37 -04003888 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003889 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
3890 ins, size);
Chris Masonccd467d2007-06-28 15:57:36 -04003891 BUG_ON(ret);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003892
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003893 leaf = path->nodes[0];
3894 extent_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason47e4bb92008-02-01 14:51:59 -05003895 struct btrfs_extent_item);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003896 btrfs_set_extent_refs(leaf, extent_item, ref_mod);
3897 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
3898 btrfs_set_extent_flags(leaf, extent_item,
3899 flags | BTRFS_EXTENT_FLAG_DATA);
Chris Mason47e4bb92008-02-01 14:51:59 -05003900
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003901 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
3902 btrfs_set_extent_inline_ref_type(leaf, iref, type);
3903 if (parent > 0) {
3904 struct btrfs_shared_data_ref *ref;
3905 ref = (struct btrfs_shared_data_ref *)(iref + 1);
3906 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
3907 btrfs_set_shared_data_ref_count(leaf, ref, ref_mod);
3908 } else {
3909 struct btrfs_extent_data_ref *ref;
3910 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
3911 btrfs_set_extent_data_ref_root(leaf, ref, root_objectid);
3912 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
3913 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
3914 btrfs_set_extent_data_ref_count(leaf, ref, ref_mod);
3915 }
Chris Mason47e4bb92008-02-01 14:51:59 -05003916
3917 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason7bb86312007-12-11 09:25:06 -05003918 btrfs_free_path(path);
Chris Masonf510cfe2007-10-15 16:14:48 -04003919
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003920 ret = update_block_group(trans, root, ins->objectid, ins->offset,
3921 1, 0);
Chris Masonf5947062008-02-04 10:10:13 -05003922 if (ret) {
Chris Masond3977122009-01-05 21:25:51 -05003923 printk(KERN_ERR "btrfs update block group failed for %llu "
3924 "%llu\n", (unsigned long long)ins->objectid,
3925 (unsigned long long)ins->offset);
Chris Masonf5947062008-02-04 10:10:13 -05003926 BUG();
3927 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003928 return ret;
3929}
3930
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003931static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
3932 struct btrfs_root *root,
3933 u64 parent, u64 root_objectid,
3934 u64 flags, struct btrfs_disk_key *key,
3935 int level, struct btrfs_key *ins)
3936{
3937 int ret;
3938 struct btrfs_fs_info *fs_info = root->fs_info;
3939 struct btrfs_extent_item *extent_item;
3940 struct btrfs_tree_block_info *block_info;
3941 struct btrfs_extent_inline_ref *iref;
3942 struct btrfs_path *path;
3943 struct extent_buffer *leaf;
3944 u32 size = sizeof(*extent_item) + sizeof(*block_info) + sizeof(*iref);
3945
3946 path = btrfs_alloc_path();
3947 BUG_ON(!path);
3948
3949 path->leave_spinning = 1;
3950 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
3951 ins, size);
3952 BUG_ON(ret);
3953
3954 leaf = path->nodes[0];
3955 extent_item = btrfs_item_ptr(leaf, path->slots[0],
3956 struct btrfs_extent_item);
3957 btrfs_set_extent_refs(leaf, extent_item, 1);
3958 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
3959 btrfs_set_extent_flags(leaf, extent_item,
3960 flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
3961 block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
3962
3963 btrfs_set_tree_block_key(leaf, block_info, key);
3964 btrfs_set_tree_block_level(leaf, block_info, level);
3965
3966 iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
3967 if (parent > 0) {
3968 BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
3969 btrfs_set_extent_inline_ref_type(leaf, iref,
3970 BTRFS_SHARED_BLOCK_REF_KEY);
3971 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
3972 } else {
3973 btrfs_set_extent_inline_ref_type(leaf, iref,
3974 BTRFS_TREE_BLOCK_REF_KEY);
3975 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
3976 }
3977
3978 btrfs_mark_buffer_dirty(leaf);
3979 btrfs_free_path(path);
3980
3981 ret = update_block_group(trans, root, ins->objectid, ins->offset,
3982 1, 0);
3983 if (ret) {
3984 printk(KERN_ERR "btrfs update block group failed for %llu "
3985 "%llu\n", (unsigned long long)ins->objectid,
3986 (unsigned long long)ins->offset);
3987 BUG();
3988 }
3989 return ret;
3990}
3991
3992int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
3993 struct btrfs_root *root,
3994 u64 root_objectid, u64 owner,
3995 u64 offset, struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04003996{
3997 int ret;
Chris Mason1c2308f2008-09-23 13:14:13 -04003998
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003999 BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID);
Chris Mason56bec292009-03-13 10:10:06 -04004000
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004001 ret = btrfs_add_delayed_data_ref(trans, ins->objectid, ins->offset,
4002 0, root_objectid, owner, offset,
4003 BTRFS_ADD_DELAYED_EXTENT, NULL);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004004 return ret;
4005}
Chris Masone02119d2008-09-05 16:13:11 -04004006
4007/*
4008 * this is used by the tree logging recovery code. It records that
4009 * an extent has been allocated and makes sure to clear the free
4010 * space cache bits as well
4011 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004012int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
4013 struct btrfs_root *root,
4014 u64 root_objectid, u64 owner, u64 offset,
4015 struct btrfs_key *ins)
Chris Masone02119d2008-09-05 16:13:11 -04004016{
4017 int ret;
4018 struct btrfs_block_group_cache *block_group;
4019
Chris Masone02119d2008-09-05 16:13:11 -04004020 block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
Josef Bacikea6a4782008-11-20 12:16:16 -05004021 mutex_lock(&block_group->cache_mutex);
Chris Masone02119d2008-09-05 16:13:11 -04004022 cache_block_group(root, block_group);
Josef Bacikea6a4782008-11-20 12:16:16 -05004023 mutex_unlock(&block_group->cache_mutex);
Chris Masone02119d2008-09-05 16:13:11 -04004024
Josef Bacikea6a4782008-11-20 12:16:16 -05004025 ret = btrfs_remove_free_space(block_group, ins->objectid,
4026 ins->offset);
Josef Bacik0f9dd462008-09-23 13:14:11 -04004027 BUG_ON(ret);
Chris Masonfa9c0d792009-04-03 09:47:43 -04004028 btrfs_put_block_group(block_group);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004029 ret = alloc_reserved_file_extent(trans, root, 0, root_objectid,
4030 0, owner, offset, ins, 1);
Chris Masone02119d2008-09-05 16:13:11 -04004031 return ret;
4032}
4033
Chris Masone6dcd2d2008-07-17 12:53:50 -04004034/*
4035 * finds a free extent and does all the dirty work required for allocation
4036 * returns the key for the extent through ins, and a tree buffer for
4037 * the first block of the extent through buf.
4038 *
4039 * returns 0 if everything worked, non-zero otherwise.
4040 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004041static int alloc_tree_block(struct btrfs_trans_handle *trans,
4042 struct btrfs_root *root,
4043 u64 num_bytes, u64 parent, u64 root_objectid,
4044 struct btrfs_disk_key *key, int level,
4045 u64 empty_size, u64 hint_byte, u64 search_end,
4046 struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04004047{
4048 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004049 u64 flags = 0;
4050
4051 ret = __btrfs_reserve_extent(trans, root, num_bytes, num_bytes,
4052 empty_size, hint_byte, search_end,
4053 ins, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004054 BUG_ON(ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004055
4056 if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
4057 if (parent == 0)
4058 parent = ins->objectid;
4059 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
4060 } else
4061 BUG_ON(parent > 0);
4062
4063 update_reserved_extents(root, ins->objectid, ins->offset, 1);
Chris Masond00aff02008-09-11 15:54:42 -04004064 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004065 struct btrfs_delayed_extent_op *extent_op;
4066 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
4067 BUG_ON(!extent_op);
4068 if (key)
4069 memcpy(&extent_op->key, key, sizeof(extent_op->key));
4070 else
4071 memset(&extent_op->key, 0, sizeof(extent_op->key));
4072 extent_op->flags_to_set = flags;
4073 extent_op->update_key = 1;
4074 extent_op->update_flags = 1;
4075 extent_op->is_data = 0;
4076
4077 ret = btrfs_add_delayed_tree_ref(trans, ins->objectid,
4078 ins->offset, parent, root_objectid,
4079 level, BTRFS_ADD_DELAYED_EXTENT,
4080 extent_op);
Chris Masond00aff02008-09-11 15:54:42 -04004081 BUG_ON(ret);
Chris Masond00aff02008-09-11 15:54:42 -04004082 }
Chris Mason925baed2008-06-25 16:01:30 -04004083 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05004084}
Chris Mason65b51a02008-08-01 15:11:20 -04004085
4086struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
4087 struct btrfs_root *root,
Chris Mason4008c042009-02-12 14:09:45 -05004088 u64 bytenr, u32 blocksize,
4089 int level)
Chris Mason65b51a02008-08-01 15:11:20 -04004090{
4091 struct extent_buffer *buf;
4092
4093 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
4094 if (!buf)
4095 return ERR_PTR(-ENOMEM);
4096 btrfs_set_header_generation(buf, trans->transid);
Chris Mason4008c042009-02-12 14:09:45 -05004097 btrfs_set_buffer_lockdep_class(buf, level);
Chris Mason65b51a02008-08-01 15:11:20 -04004098 btrfs_tree_lock(buf);
4099 clean_tree_block(trans, root, buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004100
4101 btrfs_set_lock_blocking(buf);
Chris Mason65b51a02008-08-01 15:11:20 -04004102 btrfs_set_buffer_uptodate(buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004103
Chris Masond0c803c2008-09-11 16:17:57 -04004104 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
4105 set_extent_dirty(&root->dirty_log_pages, buf->start,
Chris Mason65b51a02008-08-01 15:11:20 -04004106 buf->start + buf->len - 1, GFP_NOFS);
Chris Masond0c803c2008-09-11 16:17:57 -04004107 } else {
4108 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
4109 buf->start + buf->len - 1, GFP_NOFS);
4110 }
Chris Mason65b51a02008-08-01 15:11:20 -04004111 trans->blocks_used++;
Chris Masonb4ce94d2009-02-04 09:25:08 -05004112 /* this returns a buffer locked for blocking */
Chris Mason65b51a02008-08-01 15:11:20 -04004113 return buf;
4114}
4115
Chris Masonfec577f2007-02-26 10:40:21 -05004116/*
4117 * helper function to allocate a block for a given tree
4118 * returns the tree buffer or NULL.
4119 */
Chris Mason5f39d392007-10-15 16:14:19 -04004120struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004121 struct btrfs_root *root, u32 blocksize,
4122 u64 parent, u64 root_objectid,
4123 struct btrfs_disk_key *key, int level,
4124 u64 hint, u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05004125{
Chris Masone2fa7222007-03-12 16:22:34 -04004126 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -05004127 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04004128 struct extent_buffer *buf;
Chris Masonfec577f2007-02-26 10:40:21 -05004129
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004130 ret = alloc_tree_block(trans, root, blocksize, parent, root_objectid,
4131 key, level, empty_size, hint, (u64)-1, &ins);
Chris Masonfec577f2007-02-26 10:40:21 -05004132 if (ret) {
Chris Mason54aa1f42007-06-22 14:16:25 -04004133 BUG_ON(ret > 0);
4134 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05004135 }
Chris Mason55c69072008-01-09 15:55:33 -05004136
Chris Mason4008c042009-02-12 14:09:45 -05004137 buf = btrfs_init_new_buffer(trans, root, ins.objectid,
4138 blocksize, level);
Chris Masonfec577f2007-02-26 10:40:21 -05004139 return buf;
4140}
Chris Masona28ec192007-03-06 20:08:01 -05004141
Yan Zheng2c47e6052009-06-27 21:07:35 -04004142#if 0
Chris Masone02119d2008-09-05 16:13:11 -04004143int btrfs_drop_leaf_ref(struct btrfs_trans_handle *trans,
4144 struct btrfs_root *root, struct extent_buffer *leaf)
Chris Mason6407bf62007-03-27 06:33:00 -04004145{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004146 u64 disk_bytenr;
4147 u64 num_bytes;
Chris Mason5f39d392007-10-15 16:14:19 -04004148 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -04004149 struct btrfs_file_extent_item *fi;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004150 u32 nritems;
Chris Mason6407bf62007-03-27 06:33:00 -04004151 int i;
Chris Mason6407bf62007-03-27 06:33:00 -04004152 int ret;
4153
Chris Mason5f39d392007-10-15 16:14:19 -04004154 BUG_ON(!btrfs_is_leaf(leaf));
4155 nritems = btrfs_header_nritems(leaf);
Chris Mason7bb86312007-12-11 09:25:06 -05004156
Chris Mason6407bf62007-03-27 06:33:00 -04004157 for (i = 0; i < nritems; i++) {
Chris Masone34a5b42008-07-22 12:08:37 -04004158 cond_resched();
Chris Mason5f39d392007-10-15 16:14:19 -04004159 btrfs_item_key_to_cpu(leaf, &key, i);
Chris Masonbd56b302009-02-04 09:27:02 -05004160
4161 /* only extents have references, skip everything else */
Chris Mason5f39d392007-10-15 16:14:19 -04004162 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason6407bf62007-03-27 06:33:00 -04004163 continue;
Chris Masonbd56b302009-02-04 09:27:02 -05004164
Chris Mason6407bf62007-03-27 06:33:00 -04004165 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
Chris Masonbd56b302009-02-04 09:27:02 -05004166
4167 /* inline extents live in the btree, they don't have refs */
Chris Mason5f39d392007-10-15 16:14:19 -04004168 if (btrfs_file_extent_type(leaf, fi) ==
4169 BTRFS_FILE_EXTENT_INLINE)
Chris Mason236454df2007-04-19 13:37:44 -04004170 continue;
Chris Masonbd56b302009-02-04 09:27:02 -05004171
Chris Masondb945352007-10-15 16:15:53 -04004172 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Chris Masonbd56b302009-02-04 09:27:02 -05004173
4174 /* holes don't have refs */
Chris Masondb945352007-10-15 16:15:53 -04004175 if (disk_bytenr == 0)
Chris Mason3a686372007-05-24 13:35:57 -04004176 continue;
Chris Mason4a096752008-07-21 10:29:44 -04004177
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004178 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
4179 ret = btrfs_free_extent(trans, root, disk_bytenr, num_bytes,
4180 leaf->start, 0, key.objectid, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04004181 BUG_ON(ret);
Chris Mason6407bf62007-03-27 06:33:00 -04004182 }
4183 return 0;
4184}
4185
Chris Masond3977122009-01-05 21:25:51 -05004186static noinline int cache_drop_leaf_ref(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04004187 struct btrfs_root *root,
4188 struct btrfs_leaf_ref *ref)
Yan Zheng31153d82008-07-28 15:32:19 -04004189{
4190 int i;
4191 int ret;
Chris Masonbd56b302009-02-04 09:27:02 -05004192 struct btrfs_extent_info *info;
4193 struct refsort *sorted;
Yan Zheng31153d82008-07-28 15:32:19 -04004194
Chris Masonbd56b302009-02-04 09:27:02 -05004195 if (ref->nritems == 0)
4196 return 0;
4197
4198 sorted = kmalloc(sizeof(*sorted) * ref->nritems, GFP_NOFS);
Yan Zheng31153d82008-07-28 15:32:19 -04004199 for (i = 0; i < ref->nritems; i++) {
Chris Masonbd56b302009-02-04 09:27:02 -05004200 sorted[i].bytenr = ref->extents[i].bytenr;
4201 sorted[i].slot = i;
4202 }
4203 sort(sorted, ref->nritems, sizeof(struct refsort), refsort_cmp, NULL);
4204
4205 /*
4206 * the items in the ref were sorted when the ref was inserted
4207 * into the ref cache, so this is already in order
4208 */
4209 for (i = 0; i < ref->nritems; i++) {
4210 info = ref->extents + sorted[i].slot;
Chris Mason56bec292009-03-13 10:10:06 -04004211 ret = btrfs_free_extent(trans, root, info->bytenr,
Zheng Yan31840ae2008-09-23 13:14:14 -04004212 info->num_bytes, ref->bytenr,
4213 ref->owner, ref->generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004214 info->objectid, 0);
Chris Mason2dd3e672008-08-04 08:20:15 -04004215
4216 atomic_inc(&root->fs_info->throttle_gen);
4217 wake_up(&root->fs_info->transaction_throttle);
4218 cond_resched();
4219
Yan Zheng31153d82008-07-28 15:32:19 -04004220 BUG_ON(ret);
4221 info++;
4222 }
Yan Zheng31153d82008-07-28 15:32:19 -04004223
Chris Mason806638b2009-02-05 09:08:14 -05004224 kfree(sorted);
Yan Zheng31153d82008-07-28 15:32:19 -04004225 return 0;
4226}
4227
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004228
Chris Mason56bec292009-03-13 10:10:06 -04004229static int drop_snap_lookup_refcount(struct btrfs_trans_handle *trans,
4230 struct btrfs_root *root, u64 start,
Chris Masond3977122009-01-05 21:25:51 -05004231 u64 len, u32 *refs)
Chris Mason333db942008-06-25 16:01:30 -04004232{
Chris Mason017e5362008-07-28 15:32:51 -04004233 int ret;
Chris Masonf87f0572008-08-01 11:27:23 -04004234
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004235 ret = btrfs_lookup_extent_refs(trans, root, start, len, refs);
Chris Masonf87f0572008-08-01 11:27:23 -04004236 BUG_ON(ret);
4237
Chris Masond3977122009-01-05 21:25:51 -05004238#if 0 /* some debugging code in case we see problems here */
Chris Masonf87f0572008-08-01 11:27:23 -04004239 /* if the refs count is one, it won't get increased again. But
4240 * if the ref count is > 1, someone may be decreasing it at
4241 * the same time we are.
4242 */
4243 if (*refs != 1) {
4244 struct extent_buffer *eb = NULL;
4245 eb = btrfs_find_create_tree_block(root, start, len);
4246 if (eb)
4247 btrfs_tree_lock(eb);
4248
4249 mutex_lock(&root->fs_info->alloc_mutex);
4250 ret = lookup_extent_ref(NULL, root, start, len, refs);
4251 BUG_ON(ret);
4252 mutex_unlock(&root->fs_info->alloc_mutex);
4253
4254 if (eb) {
4255 btrfs_tree_unlock(eb);
4256 free_extent_buffer(eb);
4257 }
4258 if (*refs == 1) {
Chris Masond3977122009-01-05 21:25:51 -05004259 printk(KERN_ERR "btrfs block %llu went down to one "
4260 "during drop_snap\n", (unsigned long long)start);
Chris Masonf87f0572008-08-01 11:27:23 -04004261 }
4262
4263 }
4264#endif
4265
Chris Masone7a84562008-06-25 16:01:31 -04004266 cond_resched();
Chris Mason017e5362008-07-28 15:32:51 -04004267 return ret;
Chris Mason333db942008-06-25 16:01:30 -04004268}
4269
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004270
Chris Mason333db942008-06-25 16:01:30 -04004271/*
Chris Masonbd56b302009-02-04 09:27:02 -05004272 * this is used while deleting old snapshots, and it drops the refs
4273 * on a whole subtree starting from a level 1 node.
4274 *
4275 * The idea is to sort all the leaf pointers, and then drop the
4276 * ref on all the leaves in order. Most of the time the leaves
4277 * will have ref cache entries, so no leaf IOs will be required to
4278 * find the extents they have references on.
4279 *
4280 * For each leaf, any references it has are also dropped in order
4281 *
4282 * This ends up dropping the references in something close to optimal
4283 * order for reading and modifying the extent allocation tree.
4284 */
4285static noinline int drop_level_one_refs(struct btrfs_trans_handle *trans,
4286 struct btrfs_root *root,
4287 struct btrfs_path *path)
4288{
4289 u64 bytenr;
4290 u64 root_owner;
4291 u64 root_gen;
4292 struct extent_buffer *eb = path->nodes[1];
4293 struct extent_buffer *leaf;
4294 struct btrfs_leaf_ref *ref;
4295 struct refsort *sorted = NULL;
4296 int nritems = btrfs_header_nritems(eb);
4297 int ret;
4298 int i;
4299 int refi = 0;
4300 int slot = path->slots[1];
4301 u32 blocksize = btrfs_level_size(root, 0);
4302 u32 refs;
4303
4304 if (nritems == 0)
4305 goto out;
4306
4307 root_owner = btrfs_header_owner(eb);
4308 root_gen = btrfs_header_generation(eb);
4309 sorted = kmalloc(sizeof(*sorted) * nritems, GFP_NOFS);
4310
4311 /*
4312 * step one, sort all the leaf pointers so we don't scribble
4313 * randomly into the extent allocation tree
4314 */
4315 for (i = slot; i < nritems; i++) {
4316 sorted[refi].bytenr = btrfs_node_blockptr(eb, i);
4317 sorted[refi].slot = i;
4318 refi++;
4319 }
4320
4321 /*
4322 * nritems won't be zero, but if we're picking up drop_snapshot
4323 * after a crash, slot might be > 0, so double check things
4324 * just in case.
4325 */
4326 if (refi == 0)
4327 goto out;
4328
4329 sort(sorted, refi, sizeof(struct refsort), refsort_cmp, NULL);
4330
4331 /*
4332 * the first loop frees everything the leaves point to
4333 */
4334 for (i = 0; i < refi; i++) {
4335 u64 ptr_gen;
4336
4337 bytenr = sorted[i].bytenr;
4338
4339 /*
4340 * check the reference count on this leaf. If it is > 1
4341 * we just decrement it below and don't update any
4342 * of the refs the leaf points to.
4343 */
Chris Mason56bec292009-03-13 10:10:06 -04004344 ret = drop_snap_lookup_refcount(trans, root, bytenr,
4345 blocksize, &refs);
Chris Masonbd56b302009-02-04 09:27:02 -05004346 BUG_ON(ret);
4347 if (refs != 1)
4348 continue;
4349
4350 ptr_gen = btrfs_node_ptr_generation(eb, sorted[i].slot);
4351
4352 /*
4353 * the leaf only had one reference, which means the
4354 * only thing pointing to this leaf is the snapshot
4355 * we're deleting. It isn't possible for the reference
4356 * count to increase again later
4357 *
4358 * The reference cache is checked for the leaf,
4359 * and if found we'll be able to drop any refs held by
4360 * the leaf without needing to read it in.
4361 */
4362 ref = btrfs_lookup_leaf_ref(root, bytenr);
4363 if (ref && ref->generation != ptr_gen) {
4364 btrfs_free_leaf_ref(root, ref);
4365 ref = NULL;
4366 }
4367 if (ref) {
4368 ret = cache_drop_leaf_ref(trans, root, ref);
4369 BUG_ON(ret);
4370 btrfs_remove_leaf_ref(root, ref);
4371 btrfs_free_leaf_ref(root, ref);
4372 } else {
4373 /*
4374 * the leaf wasn't in the reference cache, so
4375 * we have to read it.
4376 */
4377 leaf = read_tree_block(root, bytenr, blocksize,
4378 ptr_gen);
4379 ret = btrfs_drop_leaf_ref(trans, root, leaf);
4380 BUG_ON(ret);
4381 free_extent_buffer(leaf);
4382 }
4383 atomic_inc(&root->fs_info->throttle_gen);
4384 wake_up(&root->fs_info->transaction_throttle);
4385 cond_resched();
4386 }
4387
4388 /*
4389 * run through the loop again to free the refs on the leaves.
4390 * This is faster than doing it in the loop above because
4391 * the leaves are likely to be clustered together. We end up
4392 * working in nice chunks on the extent allocation tree.
4393 */
4394 for (i = 0; i < refi; i++) {
4395 bytenr = sorted[i].bytenr;
Chris Mason56bec292009-03-13 10:10:06 -04004396 ret = btrfs_free_extent(trans, root, bytenr,
Chris Masonbd56b302009-02-04 09:27:02 -05004397 blocksize, eb->start,
4398 root_owner, root_gen, 0, 1);
4399 BUG_ON(ret);
4400
4401 atomic_inc(&root->fs_info->throttle_gen);
4402 wake_up(&root->fs_info->transaction_throttle);
4403 cond_resched();
4404 }
4405out:
4406 kfree(sorted);
4407
4408 /*
4409 * update the path to show we've processed the entire level 1
4410 * node. This will get saved into the root's drop_snapshot_progress
4411 * field so these drops are not repeated again if this transaction
4412 * commits.
4413 */
4414 path->slots[1] = nritems;
4415 return 0;
4416}
4417
4418/*
Chris Mason9aca1d52007-03-13 11:09:37 -04004419 * helper function for drop_snapshot, this walks down the tree dropping ref
4420 * counts as it goes.
4421 */
Chris Masond3977122009-01-05 21:25:51 -05004422static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05004423 struct btrfs_root *root,
4424 struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05004425{
Chris Mason7bb86312007-12-11 09:25:06 -05004426 u64 root_owner;
4427 u64 root_gen;
4428 u64 bytenr;
Chris Masonca7a79a2008-05-12 12:59:19 -04004429 u64 ptr_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04004430 struct extent_buffer *next;
4431 struct extent_buffer *cur;
Chris Mason7bb86312007-12-11 09:25:06 -05004432 struct extent_buffer *parent;
Chris Masondb945352007-10-15 16:15:53 -04004433 u32 blocksize;
Chris Mason20524f02007-03-10 06:35:47 -05004434 int ret;
4435 u32 refs;
4436
Chris Mason5caf2a02007-04-02 11:20:42 -04004437 WARN_ON(*level < 0);
4438 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason56bec292009-03-13 10:10:06 -04004439 ret = drop_snap_lookup_refcount(trans, root, path->nodes[*level]->start,
Chris Masondb945352007-10-15 16:15:53 -04004440 path->nodes[*level]->len, &refs);
Chris Mason20524f02007-03-10 06:35:47 -05004441 BUG_ON(ret);
4442 if (refs > 1)
4443 goto out;
Chris Masone0115992007-06-19 16:23:05 -04004444
Chris Mason9aca1d52007-03-13 11:09:37 -04004445 /*
4446 * walk down to the last node level and free all the leaves
4447 */
Chris Masond3977122009-01-05 21:25:51 -05004448 while (*level >= 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -04004449 WARN_ON(*level < 0);
4450 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason20524f02007-03-10 06:35:47 -05004451 cur = path->nodes[*level];
Chris Masone0115992007-06-19 16:23:05 -04004452
Chris Mason5f39d392007-10-15 16:14:19 -04004453 if (btrfs_header_level(cur) != *level)
Chris Mason2c90e5d2007-04-02 10:50:19 -04004454 WARN_ON(1);
Chris Masone0115992007-06-19 16:23:05 -04004455
Chris Mason7518a232007-03-12 12:01:18 -04004456 if (path->slots[*level] >=
Chris Mason5f39d392007-10-15 16:14:19 -04004457 btrfs_header_nritems(cur))
Chris Mason20524f02007-03-10 06:35:47 -05004458 break;
Chris Masonbd56b302009-02-04 09:27:02 -05004459
4460 /* the new code goes down to level 1 and does all the
4461 * leaves pointed to that node in bulk. So, this check
4462 * for level 0 will always be false.
4463 *
4464 * But, the disk format allows the drop_snapshot_progress
4465 * field in the root to leave things in a state where
4466 * a leaf will need cleaning up here. If someone crashes
4467 * with the old code and then boots with the new code,
4468 * we might find a leaf here.
4469 */
Chris Mason6407bf62007-03-27 06:33:00 -04004470 if (*level == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004471 ret = btrfs_drop_leaf_ref(trans, root, cur);
Chris Mason6407bf62007-03-27 06:33:00 -04004472 BUG_ON(ret);
4473 break;
4474 }
Chris Masonbd56b302009-02-04 09:27:02 -05004475
4476 /*
4477 * once we get to level one, process the whole node
4478 * at once, including everything below it.
4479 */
4480 if (*level == 1) {
4481 ret = drop_level_one_refs(trans, root, path);
4482 BUG_ON(ret);
4483 break;
4484 }
4485
Chris Masondb945352007-10-15 16:15:53 -04004486 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
Chris Masonca7a79a2008-05-12 12:59:19 -04004487 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
Chris Masondb945352007-10-15 16:15:53 -04004488 blocksize = btrfs_level_size(root, *level - 1);
Chris Mason925baed2008-06-25 16:01:30 -04004489
Chris Mason56bec292009-03-13 10:10:06 -04004490 ret = drop_snap_lookup_refcount(trans, root, bytenr,
4491 blocksize, &refs);
Chris Mason6407bf62007-03-27 06:33:00 -04004492 BUG_ON(ret);
Chris Masonbd56b302009-02-04 09:27:02 -05004493
4494 /*
4495 * if there is more than one reference, we don't need
4496 * to read that node to drop any references it has. We
4497 * just drop the ref we hold on that node and move on to the
4498 * next slot in this level.
4499 */
Chris Mason6407bf62007-03-27 06:33:00 -04004500 if (refs != 1) {
Chris Mason7bb86312007-12-11 09:25:06 -05004501 parent = path->nodes[*level];
4502 root_owner = btrfs_header_owner(parent);
4503 root_gen = btrfs_header_generation(parent);
Chris Mason20524f02007-03-10 06:35:47 -05004504 path->slots[*level]++;
Chris Masonf87f0572008-08-01 11:27:23 -04004505
Chris Mason56bec292009-03-13 10:10:06 -04004506 ret = btrfs_free_extent(trans, root, bytenr,
Zheng Yan31840ae2008-09-23 13:14:14 -04004507 blocksize, parent->start,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004508 root_owner, root_gen,
4509 *level - 1, 1);
Chris Mason20524f02007-03-10 06:35:47 -05004510 BUG_ON(ret);
Chris Mason18e35e0a2008-08-01 13:11:41 -04004511
4512 atomic_inc(&root->fs_info->throttle_gen);
4513 wake_up(&root->fs_info->transaction_throttle);
Chris Mason2dd3e672008-08-04 08:20:15 -04004514 cond_resched();
Chris Mason18e35e0a2008-08-01 13:11:41 -04004515
Chris Mason20524f02007-03-10 06:35:47 -05004516 continue;
4517 }
Chris Mason333db942008-06-25 16:01:30 -04004518
Chris Masonbd56b302009-02-04 09:27:02 -05004519 /*
4520 * we need to keep freeing things in the next level down.
4521 * read the block and loop around to process it
4522 */
4523 next = read_tree_block(root, bytenr, blocksize, ptr_gen);
Chris Mason5caf2a02007-04-02 11:20:42 -04004524 WARN_ON(*level <= 0);
Chris Mason83e15a22007-03-12 09:03:27 -04004525 if (path->nodes[*level-1])
Chris Mason5f39d392007-10-15 16:14:19 -04004526 free_extent_buffer(path->nodes[*level-1]);
Chris Mason20524f02007-03-10 06:35:47 -05004527 path->nodes[*level-1] = next;
Chris Mason5f39d392007-10-15 16:14:19 -04004528 *level = btrfs_header_level(next);
Chris Mason20524f02007-03-10 06:35:47 -05004529 path->slots[*level] = 0;
Chris Mason2dd3e672008-08-04 08:20:15 -04004530 cond_resched();
Chris Mason20524f02007-03-10 06:35:47 -05004531 }
4532out:
Chris Mason5caf2a02007-04-02 11:20:42 -04004533 WARN_ON(*level < 0);
4534 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason7bb86312007-12-11 09:25:06 -05004535
4536 if (path->nodes[*level] == root->node) {
Chris Mason7bb86312007-12-11 09:25:06 -05004537 parent = path->nodes[*level];
Yan Zheng31153d82008-07-28 15:32:19 -04004538 bytenr = path->nodes[*level]->start;
Chris Mason7bb86312007-12-11 09:25:06 -05004539 } else {
4540 parent = path->nodes[*level + 1];
Yan Zheng31153d82008-07-28 15:32:19 -04004541 bytenr = btrfs_node_blockptr(parent, path->slots[*level + 1]);
Chris Mason7bb86312007-12-11 09:25:06 -05004542 }
4543
Yan Zheng31153d82008-07-28 15:32:19 -04004544 blocksize = btrfs_level_size(root, *level);
4545 root_owner = btrfs_header_owner(parent);
Chris Mason7bb86312007-12-11 09:25:06 -05004546 root_gen = btrfs_header_generation(parent);
Yan Zheng31153d82008-07-28 15:32:19 -04004547
Chris Masonbd56b302009-02-04 09:27:02 -05004548 /*
4549 * cleanup and free the reference on the last node
4550 * we processed
4551 */
Chris Mason56bec292009-03-13 10:10:06 -04004552 ret = btrfs_free_extent(trans, root, bytenr, blocksize,
Zheng Yan31840ae2008-09-23 13:14:14 -04004553 parent->start, root_owner, root_gen,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004554 *level, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04004555 free_extent_buffer(path->nodes[*level]);
Chris Mason20524f02007-03-10 06:35:47 -05004556 path->nodes[*level] = NULL;
Chris Masonbd56b302009-02-04 09:27:02 -05004557
Chris Mason20524f02007-03-10 06:35:47 -05004558 *level += 1;
4559 BUG_ON(ret);
Chris Masonf87f0572008-08-01 11:27:23 -04004560
Chris Masone7a84562008-06-25 16:01:31 -04004561 cond_resched();
Chris Mason20524f02007-03-10 06:35:47 -05004562 return 0;
4563}
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004564#endif
Chris Mason20524f02007-03-10 06:35:47 -05004565
Yan Zheng2c47e6052009-06-27 21:07:35 -04004566struct walk_control {
4567 u64 refs[BTRFS_MAX_LEVEL];
4568 u64 flags[BTRFS_MAX_LEVEL];
4569 struct btrfs_key update_progress;
4570 int stage;
4571 int level;
4572 int shared_level;
4573 int update_ref;
4574 int keep_locks;
4575};
4576
4577#define DROP_REFERENCE 1
4578#define UPDATE_BACKREF 2
4579
Chris Mason9aca1d52007-03-13 11:09:37 -04004580/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04004581 * hepler to process tree block while walking down the tree.
4582 *
4583 * when wc->stage == DROP_REFERENCE, this function checks
4584 * reference count of the block. if the block is shared and
4585 * we need update back refs for the subtree rooted at the
4586 * block, this function changes wc->stage to UPDATE_BACKREF
4587 *
4588 * when wc->stage == UPDATE_BACKREF, this function updates
4589 * back refs for pointers in the block.
4590 *
4591 * NOTE: return value 1 means we should stop walking down.
Yan Zhengf82d02d2008-10-29 14:49:05 -04004592 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04004593static noinline int walk_down_proc(struct btrfs_trans_handle *trans,
4594 struct btrfs_root *root,
4595 struct btrfs_path *path,
4596 struct walk_control *wc)
4597{
4598 int level = wc->level;
4599 struct extent_buffer *eb = path->nodes[level];
4600 struct btrfs_key key;
4601 u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF;
4602 int ret;
4603
4604 if (wc->stage == UPDATE_BACKREF &&
4605 btrfs_header_owner(eb) != root->root_key.objectid)
4606 return 1;
4607
4608 /*
4609 * when reference count of tree block is 1, it won't increase
4610 * again. once full backref flag is set, we never clear it.
4611 */
4612 if ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) ||
4613 (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag))) {
4614 BUG_ON(!path->locks[level]);
4615 ret = btrfs_lookup_extent_info(trans, root,
4616 eb->start, eb->len,
4617 &wc->refs[level],
4618 &wc->flags[level]);
4619 BUG_ON(ret);
4620 BUG_ON(wc->refs[level] == 0);
4621 }
4622
4623 if (wc->stage == DROP_REFERENCE &&
4624 wc->update_ref && wc->refs[level] > 1) {
4625 BUG_ON(eb == root->node);
4626 BUG_ON(path->slots[level] > 0);
4627 if (level == 0)
4628 btrfs_item_key_to_cpu(eb, &key, path->slots[level]);
4629 else
4630 btrfs_node_key_to_cpu(eb, &key, path->slots[level]);
4631 if (btrfs_header_owner(eb) == root->root_key.objectid &&
4632 btrfs_comp_cpu_keys(&key, &wc->update_progress) >= 0) {
4633 wc->stage = UPDATE_BACKREF;
4634 wc->shared_level = level;
4635 }
4636 }
4637
4638 if (wc->stage == DROP_REFERENCE) {
4639 if (wc->refs[level] > 1)
4640 return 1;
4641
4642 if (path->locks[level] && !wc->keep_locks) {
4643 btrfs_tree_unlock(eb);
4644 path->locks[level] = 0;
4645 }
4646 return 0;
4647 }
4648
4649 /* wc->stage == UPDATE_BACKREF */
4650 if (!(wc->flags[level] & flag)) {
4651 BUG_ON(!path->locks[level]);
4652 ret = btrfs_inc_ref(trans, root, eb, 1);
4653 BUG_ON(ret);
4654 ret = btrfs_dec_ref(trans, root, eb, 0);
4655 BUG_ON(ret);
4656 ret = btrfs_set_disk_extent_flags(trans, root, eb->start,
4657 eb->len, flag, 0);
4658 BUG_ON(ret);
4659 wc->flags[level] |= flag;
4660 }
4661
4662 /*
4663 * the block is shared by multiple trees, so it's not good to
4664 * keep the tree lock
4665 */
4666 if (path->locks[level] && level > 0) {
4667 btrfs_tree_unlock(eb);
4668 path->locks[level] = 0;
4669 }
4670 return 0;
4671}
4672
4673/*
4674 * hepler to process tree block while walking up the tree.
4675 *
4676 * when wc->stage == DROP_REFERENCE, this function drops
4677 * reference count on the block.
4678 *
4679 * when wc->stage == UPDATE_BACKREF, this function changes
4680 * wc->stage back to DROP_REFERENCE if we changed wc->stage
4681 * to UPDATE_BACKREF previously while processing the block.
4682 *
4683 * NOTE: return value 1 means we should stop walking up.
4684 */
4685static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
4686 struct btrfs_root *root,
4687 struct btrfs_path *path,
4688 struct walk_control *wc)
4689{
4690 int ret = 0;
4691 int level = wc->level;
4692 struct extent_buffer *eb = path->nodes[level];
4693 u64 parent = 0;
4694
4695 if (wc->stage == UPDATE_BACKREF) {
4696 BUG_ON(wc->shared_level < level);
4697 if (level < wc->shared_level)
4698 goto out;
4699
4700 BUG_ON(wc->refs[level] <= 1);
4701 ret = find_next_key(path, level + 1, &wc->update_progress);
4702 if (ret > 0)
4703 wc->update_ref = 0;
4704
4705 wc->stage = DROP_REFERENCE;
4706 wc->shared_level = -1;
4707 path->slots[level] = 0;
4708
4709 /*
4710 * check reference count again if the block isn't locked.
4711 * we should start walking down the tree again if reference
4712 * count is one.
4713 */
4714 if (!path->locks[level]) {
4715 BUG_ON(level == 0);
4716 btrfs_tree_lock(eb);
4717 btrfs_set_lock_blocking(eb);
4718 path->locks[level] = 1;
4719
4720 ret = btrfs_lookup_extent_info(trans, root,
4721 eb->start, eb->len,
4722 &wc->refs[level],
4723 &wc->flags[level]);
4724 BUG_ON(ret);
4725 BUG_ON(wc->refs[level] == 0);
4726 if (wc->refs[level] == 1) {
4727 btrfs_tree_unlock(eb);
4728 path->locks[level] = 0;
4729 return 1;
4730 }
4731 } else {
4732 BUG_ON(level != 0);
4733 }
4734 }
4735
4736 /* wc->stage == DROP_REFERENCE */
4737 BUG_ON(wc->refs[level] > 1 && !path->locks[level]);
4738
4739 if (wc->refs[level] == 1) {
4740 if (level == 0) {
4741 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
4742 ret = btrfs_dec_ref(trans, root, eb, 1);
4743 else
4744 ret = btrfs_dec_ref(trans, root, eb, 0);
4745 BUG_ON(ret);
4746 }
4747 /* make block locked assertion in clean_tree_block happy */
4748 if (!path->locks[level] &&
4749 btrfs_header_generation(eb) == trans->transid) {
4750 btrfs_tree_lock(eb);
4751 btrfs_set_lock_blocking(eb);
4752 path->locks[level] = 1;
4753 }
4754 clean_tree_block(trans, root, eb);
4755 }
4756
4757 if (eb == root->node) {
4758 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
4759 parent = eb->start;
4760 else
4761 BUG_ON(root->root_key.objectid !=
4762 btrfs_header_owner(eb));
4763 } else {
4764 if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
4765 parent = path->nodes[level + 1]->start;
4766 else
4767 BUG_ON(root->root_key.objectid !=
4768 btrfs_header_owner(path->nodes[level + 1]));
4769 }
4770
4771 ret = btrfs_free_extent(trans, root, eb->start, eb->len, parent,
4772 root->root_key.objectid, level, 0);
4773 BUG_ON(ret);
4774out:
4775 wc->refs[level] = 0;
4776 wc->flags[level] = 0;
4777 return ret;
4778}
4779
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004780static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
4781 struct btrfs_root *root,
Yan Zheng2c47e6052009-06-27 21:07:35 -04004782 struct btrfs_path *path,
4783 struct walk_control *wc)
Yan Zhengf82d02d2008-10-29 14:49:05 -04004784{
4785 struct extent_buffer *next;
4786 struct extent_buffer *cur;
Yan Zhengf82d02d2008-10-29 14:49:05 -04004787 u64 bytenr;
4788 u64 ptr_gen;
4789 u32 blocksize;
Yan Zheng2c47e6052009-06-27 21:07:35 -04004790 int level = wc->level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04004791 int ret;
4792
Yan Zheng2c47e6052009-06-27 21:07:35 -04004793 while (level >= 0) {
4794 cur = path->nodes[level];
4795 BUG_ON(path->slots[level] >= btrfs_header_nritems(cur));
Yan Zhengf82d02d2008-10-29 14:49:05 -04004796
Yan Zheng2c47e6052009-06-27 21:07:35 -04004797 ret = walk_down_proc(trans, root, path, wc);
4798 if (ret > 0)
Yan Zhengf82d02d2008-10-29 14:49:05 -04004799 break;
Yan Zhengf82d02d2008-10-29 14:49:05 -04004800
Yan Zheng2c47e6052009-06-27 21:07:35 -04004801 if (level == 0)
4802 break;
4803
4804 bytenr = btrfs_node_blockptr(cur, path->slots[level]);
4805 blocksize = btrfs_level_size(root, level - 1);
4806 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[level]);
Yan Zhengf82d02d2008-10-29 14:49:05 -04004807
4808 next = read_tree_block(root, bytenr, blocksize, ptr_gen);
4809 btrfs_tree_lock(next);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004810 btrfs_set_lock_blocking(next);
Yan Zhengf82d02d2008-10-29 14:49:05 -04004811
Yan Zheng2c47e6052009-06-27 21:07:35 -04004812 level--;
4813 BUG_ON(level != btrfs_header_level(next));
4814 path->nodes[level] = next;
4815 path->slots[level] = 0;
4816 path->locks[level] = 1;
4817 wc->level = level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04004818 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04004819 return 0;
4820}
4821
Chris Masond3977122009-01-05 21:25:51 -05004822static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05004823 struct btrfs_root *root,
Yan Zhengf82d02d2008-10-29 14:49:05 -04004824 struct btrfs_path *path,
Yan Zheng2c47e6052009-06-27 21:07:35 -04004825 struct walk_control *wc, int max_level)
Chris Mason20524f02007-03-10 06:35:47 -05004826{
Yan Zheng2c47e6052009-06-27 21:07:35 -04004827 int level = wc->level;
Chris Mason20524f02007-03-10 06:35:47 -05004828 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04004829
Yan Zheng2c47e6052009-06-27 21:07:35 -04004830 path->slots[level] = btrfs_header_nritems(path->nodes[level]);
4831 while (level < max_level && path->nodes[level]) {
4832 wc->level = level;
4833 if (path->slots[level] + 1 <
4834 btrfs_header_nritems(path->nodes[level])) {
4835 path->slots[level]++;
Chris Mason20524f02007-03-10 06:35:47 -05004836 return 0;
4837 } else {
Yan Zheng2c47e6052009-06-27 21:07:35 -04004838 ret = walk_up_proc(trans, root, path, wc);
4839 if (ret > 0)
4840 return 0;
Chris Masonbd56b302009-02-04 09:27:02 -05004841
Yan Zheng2c47e6052009-06-27 21:07:35 -04004842 if (path->locks[level]) {
4843 btrfs_tree_unlock(path->nodes[level]);
4844 path->locks[level] = 0;
Yan Zhengf82d02d2008-10-29 14:49:05 -04004845 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04004846 free_extent_buffer(path->nodes[level]);
4847 path->nodes[level] = NULL;
4848 level++;
Chris Mason20524f02007-03-10 06:35:47 -05004849 }
4850 }
4851 return 1;
4852}
4853
Chris Mason9aca1d52007-03-13 11:09:37 -04004854/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04004855 * drop a subvolume tree.
4856 *
4857 * this function traverses the tree freeing any blocks that only
4858 * referenced by the tree.
4859 *
4860 * when a shared tree block is found. this function decreases its
4861 * reference count by one. if update_ref is true, this function
4862 * also make sure backrefs for the shared block and all lower level
4863 * blocks are properly updated.
Chris Mason9aca1d52007-03-13 11:09:37 -04004864 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04004865int btrfs_drop_snapshot(struct btrfs_root *root, int update_ref)
Chris Mason20524f02007-03-10 06:35:47 -05004866{
Chris Mason5caf2a02007-04-02 11:20:42 -04004867 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04004868 struct btrfs_trans_handle *trans;
4869 struct btrfs_root *tree_root = root->fs_info->tree_root;
Chris Mason9f3a7422007-08-07 15:52:19 -04004870 struct btrfs_root_item *root_item = &root->root_item;
Yan Zheng2c47e6052009-06-27 21:07:35 -04004871 struct walk_control *wc;
4872 struct btrfs_key key;
4873 int err = 0;
4874 int ret;
4875 int level;
Chris Mason20524f02007-03-10 06:35:47 -05004876
Chris Mason5caf2a02007-04-02 11:20:42 -04004877 path = btrfs_alloc_path();
4878 BUG_ON(!path);
Chris Mason20524f02007-03-10 06:35:47 -05004879
Yan Zheng2c47e6052009-06-27 21:07:35 -04004880 wc = kzalloc(sizeof(*wc), GFP_NOFS);
4881 BUG_ON(!wc);
4882
4883 trans = btrfs_start_transaction(tree_root, 1);
4884
Chris Mason9f3a7422007-08-07 15:52:19 -04004885 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04004886 level = btrfs_header_level(root->node);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004887 path->nodes[level] = btrfs_lock_root_node(root);
4888 btrfs_set_lock_blocking(path->nodes[level]);
Chris Mason9f3a7422007-08-07 15:52:19 -04004889 path->slots[level] = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004890 path->locks[level] = 1;
Yan Zheng2c47e6052009-06-27 21:07:35 -04004891 memset(&wc->update_progress, 0,
4892 sizeof(wc->update_progress));
Chris Mason9f3a7422007-08-07 15:52:19 -04004893 } else {
Chris Mason9f3a7422007-08-07 15:52:19 -04004894 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Yan Zheng2c47e6052009-06-27 21:07:35 -04004895 memcpy(&wc->update_progress, &key,
4896 sizeof(wc->update_progress));
4897
Chris Mason6702ed42007-08-07 16:15:09 -04004898 level = root_item->drop_level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04004899 BUG_ON(level == 0);
Chris Mason6702ed42007-08-07 16:15:09 -04004900 path->lowest_level = level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04004901 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4902 path->lowest_level = 0;
4903 if (ret < 0) {
4904 err = ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04004905 goto out;
4906 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04004907 btrfs_node_key_to_cpu(path->nodes[level], &key,
4908 path->slots[level]);
4909 WARN_ON(memcmp(&key, &wc->update_progress, sizeof(key)));
4910
Chris Mason7d9eb122008-07-08 14:19:17 -04004911 /*
4912 * unlock our path, this is safe because only this
4913 * function is allowed to delete this snapshot
4914 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004915 btrfs_unlock_up_safe(path, 0);
Chris Mason9aca1d52007-03-13 11:09:37 -04004916
Yan Zheng2c47e6052009-06-27 21:07:35 -04004917 level = btrfs_header_level(root->node);
4918 while (1) {
4919 btrfs_tree_lock(path->nodes[level]);
4920 btrfs_set_lock_blocking(path->nodes[level]);
4921
4922 ret = btrfs_lookup_extent_info(trans, root,
4923 path->nodes[level]->start,
4924 path->nodes[level]->len,
4925 &wc->refs[level],
4926 &wc->flags[level]);
4927 BUG_ON(ret);
4928 BUG_ON(wc->refs[level] == 0);
4929
4930 if (level == root_item->drop_level)
4931 break;
4932
4933 btrfs_tree_unlock(path->nodes[level]);
4934 WARN_ON(wc->refs[level] != 1);
4935 level--;
4936 }
4937 }
4938
4939 wc->level = level;
4940 wc->shared_level = -1;
4941 wc->stage = DROP_REFERENCE;
4942 wc->update_ref = update_ref;
4943 wc->keep_locks = 0;
4944
4945 while (1) {
4946 ret = walk_down_tree(trans, root, path, wc);
4947 if (ret < 0) {
4948 err = ret;
Chris Masone7a84562008-06-25 16:01:31 -04004949 break;
4950 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04004951
4952 ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL);
4953 if (ret < 0) {
4954 err = ret;
4955 break;
4956 }
4957
4958 if (ret > 0) {
4959 BUG_ON(wc->stage != DROP_REFERENCE);
4960 break;
4961 }
4962
4963 if (wc->stage == DROP_REFERENCE) {
4964 level = wc->level;
4965 btrfs_node_key(path->nodes[level],
4966 &root_item->drop_progress,
4967 path->slots[level]);
4968 root_item->drop_level = level;
4969 }
4970
4971 BUG_ON(wc->level == 0);
4972 if (trans->transaction->in_commit ||
4973 trans->transaction->delayed_refs.flushing) {
4974 ret = btrfs_update_root(trans, tree_root,
4975 &root->root_key,
4976 root_item);
4977 BUG_ON(ret);
4978
4979 btrfs_end_transaction(trans, tree_root);
4980 trans = btrfs_start_transaction(tree_root, 1);
4981 } else {
4982 unsigned long update;
Chris Masonc3e69d52009-03-13 10:17:05 -04004983 update = trans->delayed_ref_updates;
4984 trans->delayed_ref_updates = 0;
4985 if (update)
Yan Zheng2c47e6052009-06-27 21:07:35 -04004986 btrfs_run_delayed_refs(trans, tree_root,
4987 update);
Chris Masonc3e69d52009-03-13 10:17:05 -04004988 }
Chris Mason20524f02007-03-10 06:35:47 -05004989 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04004990 btrfs_release_path(root, path);
4991 BUG_ON(err);
4992
4993 ret = btrfs_del_root(trans, tree_root, &root->root_key);
4994 BUG_ON(ret);
4995
4996 free_extent_buffer(root->node);
4997 free_extent_buffer(root->commit_root);
4998 kfree(root);
Chris Mason9f3a7422007-08-07 15:52:19 -04004999out:
Yan Zheng2c47e6052009-06-27 21:07:35 -04005000 btrfs_end_transaction(trans, tree_root);
5001 kfree(wc);
Chris Mason5caf2a02007-04-02 11:20:42 -04005002 btrfs_free_path(path);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005003 return err;
Chris Mason20524f02007-03-10 06:35:47 -05005004}
Chris Mason9078a3e2007-04-26 16:46:15 -04005005
Yan Zheng2c47e6052009-06-27 21:07:35 -04005006/*
5007 * drop subtree rooted at tree block 'node'.
5008 *
5009 * NOTE: this function will unlock and release tree block 'node'
5010 */
Yan Zhengf82d02d2008-10-29 14:49:05 -04005011int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
5012 struct btrfs_root *root,
5013 struct extent_buffer *node,
5014 struct extent_buffer *parent)
5015{
5016 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005017 struct walk_control *wc;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005018 int level;
5019 int parent_level;
5020 int ret = 0;
5021 int wret;
5022
Yan Zheng2c47e6052009-06-27 21:07:35 -04005023 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
5024
Yan Zhengf82d02d2008-10-29 14:49:05 -04005025 path = btrfs_alloc_path();
5026 BUG_ON(!path);
5027
Yan Zheng2c47e6052009-06-27 21:07:35 -04005028 wc = kzalloc(sizeof(*wc), GFP_NOFS);
5029 BUG_ON(!wc);
5030
Chris Masonb9447ef2009-03-09 11:45:38 -04005031 btrfs_assert_tree_locked(parent);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005032 parent_level = btrfs_header_level(parent);
5033 extent_buffer_get(parent);
5034 path->nodes[parent_level] = parent;
5035 path->slots[parent_level] = btrfs_header_nritems(parent);
5036
Chris Masonb9447ef2009-03-09 11:45:38 -04005037 btrfs_assert_tree_locked(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005038 level = btrfs_header_level(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005039 path->nodes[level] = node;
5040 path->slots[level] = 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005041 path->locks[level] = 1;
5042
5043 wc->refs[parent_level] = 1;
5044 wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF;
5045 wc->level = level;
5046 wc->shared_level = -1;
5047 wc->stage = DROP_REFERENCE;
5048 wc->update_ref = 0;
5049 wc->keep_locks = 1;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005050
5051 while (1) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04005052 wret = walk_down_tree(trans, root, path, wc);
5053 if (wret < 0) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04005054 ret = wret;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005055 break;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005056 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04005057
Yan Zheng2c47e6052009-06-27 21:07:35 -04005058 wret = walk_up_tree(trans, root, path, wc, parent_level);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005059 if (wret < 0)
5060 ret = wret;
5061 if (wret != 0)
5062 break;
5063 }
5064
Yan Zheng2c47e6052009-06-27 21:07:35 -04005065 kfree(wc);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005066 btrfs_free_path(path);
5067 return ret;
5068}
5069
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005070#if 0
Chris Mason8e7bf942008-04-28 09:02:36 -04005071static unsigned long calc_ra(unsigned long start, unsigned long last,
5072 unsigned long nr)
5073{
5074 return min(last, start + nr - 1);
5075}
5076
Chris Masond3977122009-01-05 21:25:51 -05005077static noinline int relocate_inode_pages(struct inode *inode, u64 start,
Chris Mason98ed5172008-01-03 10:01:48 -05005078 u64 len)
Chris Masonedbd8d42007-12-21 16:27:24 -05005079{
5080 u64 page_start;
5081 u64 page_end;
Zheng Yan1a40e232008-09-26 10:09:34 -04005082 unsigned long first_index;
Chris Masonedbd8d42007-12-21 16:27:24 -05005083 unsigned long last_index;
Chris Masonedbd8d42007-12-21 16:27:24 -05005084 unsigned long i;
5085 struct page *page;
Chris Masond1310b22008-01-24 16:13:08 -05005086 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason4313b392008-01-03 09:08:48 -05005087 struct file_ra_state *ra;
Chris Mason3eaa2882008-07-24 11:57:52 -04005088 struct btrfs_ordered_extent *ordered;
Zheng Yan1a40e232008-09-26 10:09:34 -04005089 unsigned int total_read = 0;
5090 unsigned int total_dirty = 0;
5091 int ret = 0;
Chris Mason4313b392008-01-03 09:08:48 -05005092
5093 ra = kzalloc(sizeof(*ra), GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05005094
5095 mutex_lock(&inode->i_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04005096 first_index = start >> PAGE_CACHE_SHIFT;
Chris Masonedbd8d42007-12-21 16:27:24 -05005097 last_index = (start + len - 1) >> PAGE_CACHE_SHIFT;
5098
Zheng Yan1a40e232008-09-26 10:09:34 -04005099 /* make sure the dirty trick played by the caller work */
5100 ret = invalidate_inode_pages2_range(inode->i_mapping,
5101 first_index, last_index);
5102 if (ret)
5103 goto out_unlock;
Chris Mason8e7bf942008-04-28 09:02:36 -04005104
Chris Mason4313b392008-01-03 09:08:48 -05005105 file_ra_state_init(ra, inode->i_mapping);
Chris Masonedbd8d42007-12-21 16:27:24 -05005106
Zheng Yan1a40e232008-09-26 10:09:34 -04005107 for (i = first_index ; i <= last_index; i++) {
5108 if (total_read % ra->ra_pages == 0) {
Chris Mason8e7bf942008-04-28 09:02:36 -04005109 btrfs_force_ra(inode->i_mapping, ra, NULL, i,
Zheng Yan1a40e232008-09-26 10:09:34 -04005110 calc_ra(i, last_index, ra->ra_pages));
Chris Mason8e7bf942008-04-28 09:02:36 -04005111 }
5112 total_read++;
Chris Mason3eaa2882008-07-24 11:57:52 -04005113again:
5114 if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode))
Zheng Yan1a40e232008-09-26 10:09:34 -04005115 BUG_ON(1);
Chris Masonedbd8d42007-12-21 16:27:24 -05005116 page = grab_cache_page(inode->i_mapping, i);
Chris Masona061fc82008-05-07 11:43:44 -04005117 if (!page) {
Zheng Yan1a40e232008-09-26 10:09:34 -04005118 ret = -ENOMEM;
Chris Masonedbd8d42007-12-21 16:27:24 -05005119 goto out_unlock;
Chris Masona061fc82008-05-07 11:43:44 -04005120 }
Chris Masonedbd8d42007-12-21 16:27:24 -05005121 if (!PageUptodate(page)) {
5122 btrfs_readpage(NULL, page);
5123 lock_page(page);
5124 if (!PageUptodate(page)) {
5125 unlock_page(page);
5126 page_cache_release(page);
Zheng Yan1a40e232008-09-26 10:09:34 -04005127 ret = -EIO;
Chris Masonedbd8d42007-12-21 16:27:24 -05005128 goto out_unlock;
5129 }
5130 }
Chris Masonec44a352008-04-28 15:29:52 -04005131 wait_on_page_writeback(page);
Chris Mason3eaa2882008-07-24 11:57:52 -04005132
Chris Masonedbd8d42007-12-21 16:27:24 -05005133 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
5134 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05005135 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05005136
Chris Mason3eaa2882008-07-24 11:57:52 -04005137 ordered = btrfs_lookup_ordered_extent(inode, page_start);
5138 if (ordered) {
5139 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
5140 unlock_page(page);
5141 page_cache_release(page);
5142 btrfs_start_ordered_extent(inode, ordered, 1);
5143 btrfs_put_ordered_extent(ordered);
5144 goto again;
5145 }
5146 set_page_extent_mapped(page);
5147
Zheng Yan1a40e232008-09-26 10:09:34 -04005148 if (i == first_index)
5149 set_extent_bits(io_tree, page_start, page_end,
5150 EXTENT_BOUNDARY, GFP_NOFS);
Yan Zheng1f80e4d2008-12-19 10:59:04 -05005151 btrfs_set_extent_delalloc(inode, page_start, page_end);
Zheng Yan1a40e232008-09-26 10:09:34 -04005152
Chris Masona061fc82008-05-07 11:43:44 -04005153 set_page_dirty(page);
Zheng Yan1a40e232008-09-26 10:09:34 -04005154 total_dirty++;
Chris Masonedbd8d42007-12-21 16:27:24 -05005155
Chris Masond1310b22008-01-24 16:13:08 -05005156 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05005157 unlock_page(page);
5158 page_cache_release(page);
5159 }
5160
5161out_unlock:
Chris Masonec44a352008-04-28 15:29:52 -04005162 kfree(ra);
Chris Masonedbd8d42007-12-21 16:27:24 -05005163 mutex_unlock(&inode->i_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04005164 balance_dirty_pages_ratelimited_nr(inode->i_mapping, total_dirty);
Chris Masonbf4ef672008-05-08 13:26:18 -04005165 return ret;
5166}
5167
Chris Masond3977122009-01-05 21:25:51 -05005168static noinline int relocate_data_extent(struct inode *reloc_inode,
Zheng Yan1a40e232008-09-26 10:09:34 -04005169 struct btrfs_key *extent_key,
5170 u64 offset)
Chris Masonedbd8d42007-12-21 16:27:24 -05005171{
Zheng Yan1a40e232008-09-26 10:09:34 -04005172 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
5173 struct extent_map_tree *em_tree = &BTRFS_I(reloc_inode)->extent_tree;
5174 struct extent_map *em;
Yan Zheng66435582008-10-30 14:19:50 -04005175 u64 start = extent_key->objectid - offset;
5176 u64 end = start + extent_key->offset - 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04005177
5178 em = alloc_extent_map(GFP_NOFS);
5179 BUG_ON(!em || IS_ERR(em));
5180
Yan Zheng66435582008-10-30 14:19:50 -04005181 em->start = start;
Zheng Yan1a40e232008-09-26 10:09:34 -04005182 em->len = extent_key->offset;
Chris Masonc8b97812008-10-29 14:49:59 -04005183 em->block_len = extent_key->offset;
Zheng Yan1a40e232008-09-26 10:09:34 -04005184 em->block_start = extent_key->objectid;
5185 em->bdev = root->fs_info->fs_devices->latest_bdev;
5186 set_bit(EXTENT_FLAG_PINNED, &em->flags);
5187
5188 /* setup extent map to cheat btrfs_readpage */
Yan Zheng66435582008-10-30 14:19:50 -04005189 lock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04005190 while (1) {
5191 int ret;
5192 spin_lock(&em_tree->lock);
5193 ret = add_extent_mapping(em_tree, em);
5194 spin_unlock(&em_tree->lock);
5195 if (ret != -EEXIST) {
5196 free_extent_map(em);
5197 break;
5198 }
Yan Zheng66435582008-10-30 14:19:50 -04005199 btrfs_drop_extent_cache(reloc_inode, start, end, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04005200 }
Yan Zheng66435582008-10-30 14:19:50 -04005201 unlock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04005202
Yan Zheng66435582008-10-30 14:19:50 -04005203 return relocate_inode_pages(reloc_inode, start, extent_key->offset);
Zheng Yan1a40e232008-09-26 10:09:34 -04005204}
5205
5206struct btrfs_ref_path {
5207 u64 extent_start;
5208 u64 nodes[BTRFS_MAX_LEVEL];
5209 u64 root_objectid;
5210 u64 root_generation;
5211 u64 owner_objectid;
Zheng Yan1a40e232008-09-26 10:09:34 -04005212 u32 num_refs;
5213 int lowest_level;
5214 int current_level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005215 int shared_level;
5216
5217 struct btrfs_key node_keys[BTRFS_MAX_LEVEL];
5218 u64 new_nodes[BTRFS_MAX_LEVEL];
Zheng Yan1a40e232008-09-26 10:09:34 -04005219};
5220
5221struct disk_extent {
Chris Masonc8b97812008-10-29 14:49:59 -04005222 u64 ram_bytes;
Zheng Yan1a40e232008-09-26 10:09:34 -04005223 u64 disk_bytenr;
5224 u64 disk_num_bytes;
5225 u64 offset;
5226 u64 num_bytes;
Chris Masonc8b97812008-10-29 14:49:59 -04005227 u8 compression;
5228 u8 encryption;
5229 u16 other_encoding;
Zheng Yan1a40e232008-09-26 10:09:34 -04005230};
5231
5232static int is_cowonly_root(u64 root_objectid)
5233{
5234 if (root_objectid == BTRFS_ROOT_TREE_OBJECTID ||
5235 root_objectid == BTRFS_EXTENT_TREE_OBJECTID ||
5236 root_objectid == BTRFS_CHUNK_TREE_OBJECTID ||
5237 root_objectid == BTRFS_DEV_TREE_OBJECTID ||
Yan Zheng0403e472008-12-10 20:32:51 -05005238 root_objectid == BTRFS_TREE_LOG_OBJECTID ||
5239 root_objectid == BTRFS_CSUM_TREE_OBJECTID)
Zheng Yan1a40e232008-09-26 10:09:34 -04005240 return 1;
5241 return 0;
5242}
5243
Chris Masond3977122009-01-05 21:25:51 -05005244static noinline int __next_ref_path(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04005245 struct btrfs_root *extent_root,
5246 struct btrfs_ref_path *ref_path,
5247 int first_time)
5248{
5249 struct extent_buffer *leaf;
5250 struct btrfs_path *path;
Chris Mason4313b392008-01-03 09:08:48 -05005251 struct btrfs_extent_ref *ref;
Chris Masonedbd8d42007-12-21 16:27:24 -05005252 struct btrfs_key key;
5253 struct btrfs_key found_key;
Zheng Yan1a40e232008-09-26 10:09:34 -04005254 u64 bytenr;
Chris Masonedbd8d42007-12-21 16:27:24 -05005255 u32 nritems;
Zheng Yan1a40e232008-09-26 10:09:34 -04005256 int level;
5257 int ret = 1;
Chris Masonedbd8d42007-12-21 16:27:24 -05005258
Zheng Yan1a40e232008-09-26 10:09:34 -04005259 path = btrfs_alloc_path();
5260 if (!path)
5261 return -ENOMEM;
5262
Zheng Yan1a40e232008-09-26 10:09:34 -04005263 if (first_time) {
5264 ref_path->lowest_level = -1;
5265 ref_path->current_level = -1;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005266 ref_path->shared_level = -1;
Zheng Yan1a40e232008-09-26 10:09:34 -04005267 goto walk_up;
Chris Masona061fc82008-05-07 11:43:44 -04005268 }
Zheng Yan1a40e232008-09-26 10:09:34 -04005269walk_down:
5270 level = ref_path->current_level - 1;
5271 while (level >= -1) {
5272 u64 parent;
5273 if (level < ref_path->lowest_level)
5274 break;
Chris Masonedbd8d42007-12-21 16:27:24 -05005275
Chris Masond3977122009-01-05 21:25:51 -05005276 if (level >= 0)
Zheng Yan1a40e232008-09-26 10:09:34 -04005277 bytenr = ref_path->nodes[level];
Chris Masond3977122009-01-05 21:25:51 -05005278 else
Zheng Yan1a40e232008-09-26 10:09:34 -04005279 bytenr = ref_path->extent_start;
Zheng Yan1a40e232008-09-26 10:09:34 -04005280 BUG_ON(bytenr == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05005281
Zheng Yan1a40e232008-09-26 10:09:34 -04005282 parent = ref_path->nodes[level + 1];
5283 ref_path->nodes[level + 1] = 0;
5284 ref_path->current_level = level;
5285 BUG_ON(parent == 0);
5286
5287 key.objectid = bytenr;
5288 key.offset = parent + 1;
5289 key.type = BTRFS_EXTENT_REF_KEY;
5290
5291 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05005292 if (ret < 0)
5293 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04005294 BUG_ON(ret == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05005295
Chris Masonedbd8d42007-12-21 16:27:24 -05005296 leaf = path->nodes[0];
5297 nritems = btrfs_header_nritems(leaf);
Zheng Yan1a40e232008-09-26 10:09:34 -04005298 if (path->slots[0] >= nritems) {
Chris Masona061fc82008-05-07 11:43:44 -04005299 ret = btrfs_next_leaf(extent_root, path);
Chris Masona061fc82008-05-07 11:43:44 -04005300 if (ret < 0)
5301 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04005302 if (ret > 0)
5303 goto next;
Chris Masonbf4ef672008-05-08 13:26:18 -04005304 leaf = path->nodes[0];
Chris Masona061fc82008-05-07 11:43:44 -04005305 }
Chris Masonedbd8d42007-12-21 16:27:24 -05005306
5307 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Zheng Yan1a40e232008-09-26 10:09:34 -04005308 if (found_key.objectid == bytenr &&
Yan Zhengf82d02d2008-10-29 14:49:05 -04005309 found_key.type == BTRFS_EXTENT_REF_KEY) {
5310 if (level < ref_path->shared_level)
5311 ref_path->shared_level = level;
Zheng Yan1a40e232008-09-26 10:09:34 -04005312 goto found;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005313 }
Zheng Yan1a40e232008-09-26 10:09:34 -04005314next:
5315 level--;
5316 btrfs_release_path(extent_root, path);
Yan Zhengd899e052008-10-30 14:25:28 -04005317 cond_resched();
Zheng Yan1a40e232008-09-26 10:09:34 -04005318 }
5319 /* reached lowest level */
5320 ret = 1;
5321 goto out;
5322walk_up:
5323 level = ref_path->current_level;
5324 while (level < BTRFS_MAX_LEVEL - 1) {
5325 u64 ref_objectid;
Chris Masond3977122009-01-05 21:25:51 -05005326
5327 if (level >= 0)
Zheng Yan1a40e232008-09-26 10:09:34 -04005328 bytenr = ref_path->nodes[level];
Chris Masond3977122009-01-05 21:25:51 -05005329 else
Zheng Yan1a40e232008-09-26 10:09:34 -04005330 bytenr = ref_path->extent_start;
Chris Masond3977122009-01-05 21:25:51 -05005331
Zheng Yan1a40e232008-09-26 10:09:34 -04005332 BUG_ON(bytenr == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05005333
Zheng Yan1a40e232008-09-26 10:09:34 -04005334 key.objectid = bytenr;
5335 key.offset = 0;
5336 key.type = BTRFS_EXTENT_REF_KEY;
Chris Masonedbd8d42007-12-21 16:27:24 -05005337
Zheng Yan1a40e232008-09-26 10:09:34 -04005338 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
5339 if (ret < 0)
Chris Masonedbd8d42007-12-21 16:27:24 -05005340 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04005341
5342 leaf = path->nodes[0];
5343 nritems = btrfs_header_nritems(leaf);
5344 if (path->slots[0] >= nritems) {
5345 ret = btrfs_next_leaf(extent_root, path);
5346 if (ret < 0)
5347 goto out;
5348 if (ret > 0) {
5349 /* the extent was freed by someone */
5350 if (ref_path->lowest_level == level)
5351 goto out;
5352 btrfs_release_path(extent_root, path);
5353 goto walk_down;
5354 }
5355 leaf = path->nodes[0];
5356 }
5357
5358 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5359 if (found_key.objectid != bytenr ||
5360 found_key.type != BTRFS_EXTENT_REF_KEY) {
5361 /* the extent was freed by someone */
5362 if (ref_path->lowest_level == level) {
5363 ret = 1;
5364 goto out;
5365 }
5366 btrfs_release_path(extent_root, path);
5367 goto walk_down;
5368 }
5369found:
5370 ref = btrfs_item_ptr(leaf, path->slots[0],
5371 struct btrfs_extent_ref);
5372 ref_objectid = btrfs_ref_objectid(leaf, ref);
5373 if (ref_objectid < BTRFS_FIRST_FREE_OBJECTID) {
5374 if (first_time) {
5375 level = (int)ref_objectid;
5376 BUG_ON(level >= BTRFS_MAX_LEVEL);
5377 ref_path->lowest_level = level;
5378 ref_path->current_level = level;
5379 ref_path->nodes[level] = bytenr;
5380 } else {
5381 WARN_ON(ref_objectid != level);
5382 }
5383 } else {
5384 WARN_ON(level != -1);
5385 }
5386 first_time = 0;
5387
5388 if (ref_path->lowest_level == level) {
5389 ref_path->owner_objectid = ref_objectid;
Zheng Yan1a40e232008-09-26 10:09:34 -04005390 ref_path->num_refs = btrfs_ref_num_refs(leaf, ref);
5391 }
5392
5393 /*
5394 * the block is tree root or the block isn't in reference
5395 * counted tree.
5396 */
5397 if (found_key.objectid == found_key.offset ||
5398 is_cowonly_root(btrfs_ref_root(leaf, ref))) {
5399 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
5400 ref_path->root_generation =
5401 btrfs_ref_generation(leaf, ref);
5402 if (level < 0) {
5403 /* special reference from the tree log */
5404 ref_path->nodes[0] = found_key.offset;
5405 ref_path->current_level = 0;
5406 }
5407 ret = 0;
5408 goto out;
5409 }
5410
5411 level++;
5412 BUG_ON(ref_path->nodes[level] != 0);
5413 ref_path->nodes[level] = found_key.offset;
5414 ref_path->current_level = level;
5415
5416 /*
5417 * the reference was created in the running transaction,
5418 * no need to continue walking up.
5419 */
5420 if (btrfs_ref_generation(leaf, ref) == trans->transid) {
5421 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
5422 ref_path->root_generation =
5423 btrfs_ref_generation(leaf, ref);
5424 ret = 0;
5425 goto out;
5426 }
5427
5428 btrfs_release_path(extent_root, path);
Yan Zhengd899e052008-10-30 14:25:28 -04005429 cond_resched();
Zheng Yan1a40e232008-09-26 10:09:34 -04005430 }
5431 /* reached max tree level, but no tree root found. */
5432 BUG();
5433out:
Zheng Yan1a40e232008-09-26 10:09:34 -04005434 btrfs_free_path(path);
5435 return ret;
5436}
5437
5438static int btrfs_first_ref_path(struct btrfs_trans_handle *trans,
5439 struct btrfs_root *extent_root,
5440 struct btrfs_ref_path *ref_path,
5441 u64 extent_start)
5442{
5443 memset(ref_path, 0, sizeof(*ref_path));
5444 ref_path->extent_start = extent_start;
5445
5446 return __next_ref_path(trans, extent_root, ref_path, 1);
5447}
5448
5449static int btrfs_next_ref_path(struct btrfs_trans_handle *trans,
5450 struct btrfs_root *extent_root,
5451 struct btrfs_ref_path *ref_path)
5452{
5453 return __next_ref_path(trans, extent_root, ref_path, 0);
5454}
5455
Chris Masond3977122009-01-05 21:25:51 -05005456static noinline int get_new_locations(struct inode *reloc_inode,
Zheng Yan1a40e232008-09-26 10:09:34 -04005457 struct btrfs_key *extent_key,
5458 u64 offset, int no_fragment,
5459 struct disk_extent **extents,
5460 int *nr_extents)
5461{
5462 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
5463 struct btrfs_path *path;
5464 struct btrfs_file_extent_item *fi;
5465 struct extent_buffer *leaf;
5466 struct disk_extent *exts = *extents;
5467 struct btrfs_key found_key;
5468 u64 cur_pos;
5469 u64 last_byte;
5470 u32 nritems;
5471 int nr = 0;
5472 int max = *nr_extents;
5473 int ret;
5474
5475 WARN_ON(!no_fragment && *extents);
5476 if (!exts) {
5477 max = 1;
5478 exts = kmalloc(sizeof(*exts) * max, GFP_NOFS);
5479 if (!exts)
5480 return -ENOMEM;
5481 }
5482
5483 path = btrfs_alloc_path();
5484 BUG_ON(!path);
5485
5486 cur_pos = extent_key->objectid - offset;
5487 last_byte = extent_key->objectid + extent_key->offset;
5488 ret = btrfs_lookup_file_extent(NULL, root, path, reloc_inode->i_ino,
5489 cur_pos, 0);
5490 if (ret < 0)
5491 goto out;
5492 if (ret > 0) {
5493 ret = -ENOENT;
5494 goto out;
5495 }
5496
5497 while (1) {
5498 leaf = path->nodes[0];
5499 nritems = btrfs_header_nritems(leaf);
5500 if (path->slots[0] >= nritems) {
5501 ret = btrfs_next_leaf(root, path);
5502 if (ret < 0)
5503 goto out;
5504 if (ret > 0)
5505 break;
5506 leaf = path->nodes[0];
5507 }
5508
5509 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5510 if (found_key.offset != cur_pos ||
5511 found_key.type != BTRFS_EXTENT_DATA_KEY ||
5512 found_key.objectid != reloc_inode->i_ino)
5513 break;
5514
5515 fi = btrfs_item_ptr(leaf, path->slots[0],
5516 struct btrfs_file_extent_item);
5517 if (btrfs_file_extent_type(leaf, fi) !=
5518 BTRFS_FILE_EXTENT_REG ||
5519 btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
5520 break;
5521
5522 if (nr == max) {
5523 struct disk_extent *old = exts;
5524 max *= 2;
5525 exts = kzalloc(sizeof(*exts) * max, GFP_NOFS);
5526 memcpy(exts, old, sizeof(*exts) * nr);
5527 if (old != *extents)
5528 kfree(old);
5529 }
5530
5531 exts[nr].disk_bytenr =
5532 btrfs_file_extent_disk_bytenr(leaf, fi);
5533 exts[nr].disk_num_bytes =
5534 btrfs_file_extent_disk_num_bytes(leaf, fi);
5535 exts[nr].offset = btrfs_file_extent_offset(leaf, fi);
5536 exts[nr].num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
Chris Masonc8b97812008-10-29 14:49:59 -04005537 exts[nr].ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
5538 exts[nr].compression = btrfs_file_extent_compression(leaf, fi);
5539 exts[nr].encryption = btrfs_file_extent_encryption(leaf, fi);
5540 exts[nr].other_encoding = btrfs_file_extent_other_encoding(leaf,
5541 fi);
Yan Zhengd899e052008-10-30 14:25:28 -04005542 BUG_ON(exts[nr].offset > 0);
5543 BUG_ON(exts[nr].compression || exts[nr].encryption);
5544 BUG_ON(exts[nr].num_bytes != exts[nr].disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04005545
5546 cur_pos += exts[nr].num_bytes;
5547 nr++;
5548
5549 if (cur_pos + offset >= last_byte)
5550 break;
5551
5552 if (no_fragment) {
5553 ret = 1;
5554 goto out;
5555 }
5556 path->slots[0]++;
5557 }
5558
Yan Zheng1f80e4d2008-12-19 10:59:04 -05005559 BUG_ON(cur_pos + offset > last_byte);
Zheng Yan1a40e232008-09-26 10:09:34 -04005560 if (cur_pos + offset < last_byte) {
5561 ret = -ENOENT;
5562 goto out;
Chris Masonedbd8d42007-12-21 16:27:24 -05005563 }
5564 ret = 0;
5565out:
Zheng Yan1a40e232008-09-26 10:09:34 -04005566 btrfs_free_path(path);
5567 if (ret) {
5568 if (exts != *extents)
5569 kfree(exts);
5570 } else {
5571 *extents = exts;
5572 *nr_extents = nr;
5573 }
5574 return ret;
5575}
5576
Chris Masond3977122009-01-05 21:25:51 -05005577static noinline int replace_one_extent(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04005578 struct btrfs_root *root,
5579 struct btrfs_path *path,
5580 struct btrfs_key *extent_key,
5581 struct btrfs_key *leaf_key,
5582 struct btrfs_ref_path *ref_path,
5583 struct disk_extent *new_extents,
5584 int nr_extents)
5585{
5586 struct extent_buffer *leaf;
5587 struct btrfs_file_extent_item *fi;
5588 struct inode *inode = NULL;
5589 struct btrfs_key key;
5590 u64 lock_start = 0;
5591 u64 lock_end = 0;
5592 u64 num_bytes;
5593 u64 ext_offset;
Yan Zheng86288a12009-01-21 10:49:16 -05005594 u64 search_end = (u64)-1;
Zheng Yan1a40e232008-09-26 10:09:34 -04005595 u32 nritems;
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04005596 int nr_scaned = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04005597 int extent_locked = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04005598 int extent_type;
Zheng Yan1a40e232008-09-26 10:09:34 -04005599 int ret;
5600
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04005601 memcpy(&key, leaf_key, sizeof(key));
Zheng Yan1a40e232008-09-26 10:09:34 -04005602 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04005603 if (key.objectid < ref_path->owner_objectid ||
5604 (key.objectid == ref_path->owner_objectid &&
5605 key.type < BTRFS_EXTENT_DATA_KEY)) {
5606 key.objectid = ref_path->owner_objectid;
5607 key.type = BTRFS_EXTENT_DATA_KEY;
5608 key.offset = 0;
5609 }
Zheng Yan1a40e232008-09-26 10:09:34 -04005610 }
5611
5612 while (1) {
5613 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
5614 if (ret < 0)
5615 goto out;
5616
5617 leaf = path->nodes[0];
5618 nritems = btrfs_header_nritems(leaf);
5619next:
5620 if (extent_locked && ret > 0) {
5621 /*
5622 * the file extent item was modified by someone
5623 * before the extent got locked.
5624 */
Zheng Yan1a40e232008-09-26 10:09:34 -04005625 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
5626 lock_end, GFP_NOFS);
5627 extent_locked = 0;
5628 }
5629
5630 if (path->slots[0] >= nritems) {
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04005631 if (++nr_scaned > 2)
Zheng Yan1a40e232008-09-26 10:09:34 -04005632 break;
5633
5634 BUG_ON(extent_locked);
5635 ret = btrfs_next_leaf(root, path);
5636 if (ret < 0)
5637 goto out;
5638 if (ret > 0)
5639 break;
5640 leaf = path->nodes[0];
5641 nritems = btrfs_header_nritems(leaf);
5642 }
5643
5644 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
5645
5646 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
5647 if ((key.objectid > ref_path->owner_objectid) ||
5648 (key.objectid == ref_path->owner_objectid &&
5649 key.type > BTRFS_EXTENT_DATA_KEY) ||
Yan Zheng86288a12009-01-21 10:49:16 -05005650 key.offset >= search_end)
Zheng Yan1a40e232008-09-26 10:09:34 -04005651 break;
5652 }
5653
5654 if (inode && key.objectid != inode->i_ino) {
5655 BUG_ON(extent_locked);
5656 btrfs_release_path(root, path);
5657 mutex_unlock(&inode->i_mutex);
5658 iput(inode);
5659 inode = NULL;
5660 continue;
5661 }
5662
5663 if (key.type != BTRFS_EXTENT_DATA_KEY) {
5664 path->slots[0]++;
5665 ret = 1;
5666 goto next;
5667 }
5668 fi = btrfs_item_ptr(leaf, path->slots[0],
5669 struct btrfs_file_extent_item);
Yan Zhengd899e052008-10-30 14:25:28 -04005670 extent_type = btrfs_file_extent_type(leaf, fi);
5671 if ((extent_type != BTRFS_FILE_EXTENT_REG &&
5672 extent_type != BTRFS_FILE_EXTENT_PREALLOC) ||
Zheng Yan1a40e232008-09-26 10:09:34 -04005673 (btrfs_file_extent_disk_bytenr(leaf, fi) !=
5674 extent_key->objectid)) {
5675 path->slots[0]++;
5676 ret = 1;
5677 goto next;
5678 }
5679
5680 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
5681 ext_offset = btrfs_file_extent_offset(leaf, fi);
5682
Yan Zheng86288a12009-01-21 10:49:16 -05005683 if (search_end == (u64)-1) {
5684 search_end = key.offset - ext_offset +
5685 btrfs_file_extent_ram_bytes(leaf, fi);
5686 }
Zheng Yan1a40e232008-09-26 10:09:34 -04005687
5688 if (!extent_locked) {
5689 lock_start = key.offset;
5690 lock_end = lock_start + num_bytes - 1;
5691 } else {
Yan Zheng66435582008-10-30 14:19:50 -04005692 if (lock_start > key.offset ||
5693 lock_end + 1 < key.offset + num_bytes) {
5694 unlock_extent(&BTRFS_I(inode)->io_tree,
5695 lock_start, lock_end, GFP_NOFS);
5696 extent_locked = 0;
5697 }
Zheng Yan1a40e232008-09-26 10:09:34 -04005698 }
5699
5700 if (!inode) {
5701 btrfs_release_path(root, path);
5702
5703 inode = btrfs_iget_locked(root->fs_info->sb,
5704 key.objectid, root);
5705 if (inode->i_state & I_NEW) {
5706 BTRFS_I(inode)->root = root;
5707 BTRFS_I(inode)->location.objectid =
5708 key.objectid;
5709 BTRFS_I(inode)->location.type =
5710 BTRFS_INODE_ITEM_KEY;
5711 BTRFS_I(inode)->location.offset = 0;
5712 btrfs_read_locked_inode(inode);
5713 unlock_new_inode(inode);
5714 }
5715 /*
5716 * some code call btrfs_commit_transaction while
5717 * holding the i_mutex, so we can't use mutex_lock
5718 * here.
5719 */
5720 if (is_bad_inode(inode) ||
5721 !mutex_trylock(&inode->i_mutex)) {
5722 iput(inode);
5723 inode = NULL;
5724 key.offset = (u64)-1;
5725 goto skip;
5726 }
5727 }
5728
5729 if (!extent_locked) {
5730 struct btrfs_ordered_extent *ordered;
5731
5732 btrfs_release_path(root, path);
5733
5734 lock_extent(&BTRFS_I(inode)->io_tree, lock_start,
5735 lock_end, GFP_NOFS);
5736 ordered = btrfs_lookup_first_ordered_extent(inode,
5737 lock_end);
5738 if (ordered &&
5739 ordered->file_offset <= lock_end &&
5740 ordered->file_offset + ordered->len > lock_start) {
5741 unlock_extent(&BTRFS_I(inode)->io_tree,
5742 lock_start, lock_end, GFP_NOFS);
5743 btrfs_start_ordered_extent(inode, ordered, 1);
5744 btrfs_put_ordered_extent(ordered);
5745 key.offset += num_bytes;
5746 goto skip;
5747 }
5748 if (ordered)
5749 btrfs_put_ordered_extent(ordered);
5750
Zheng Yan1a40e232008-09-26 10:09:34 -04005751 extent_locked = 1;
5752 continue;
5753 }
5754
5755 if (nr_extents == 1) {
5756 /* update extent pointer in place */
Zheng Yan1a40e232008-09-26 10:09:34 -04005757 btrfs_set_file_extent_disk_bytenr(leaf, fi,
5758 new_extents[0].disk_bytenr);
5759 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
5760 new_extents[0].disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04005761 btrfs_mark_buffer_dirty(leaf);
5762
5763 btrfs_drop_extent_cache(inode, key.offset,
5764 key.offset + num_bytes - 1, 0);
5765
5766 ret = btrfs_inc_extent_ref(trans, root,
5767 new_extents[0].disk_bytenr,
5768 new_extents[0].disk_num_bytes,
5769 leaf->start,
5770 root->root_key.objectid,
5771 trans->transid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04005772 key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04005773 BUG_ON(ret);
5774
5775 ret = btrfs_free_extent(trans, root,
5776 extent_key->objectid,
5777 extent_key->offset,
5778 leaf->start,
5779 btrfs_header_owner(leaf),
5780 btrfs_header_generation(leaf),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04005781 key.objectid, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04005782 BUG_ON(ret);
5783
5784 btrfs_release_path(root, path);
5785 key.offset += num_bytes;
5786 } else {
Yan Zhengd899e052008-10-30 14:25:28 -04005787 BUG_ON(1);
5788#if 0
Zheng Yan1a40e232008-09-26 10:09:34 -04005789 u64 alloc_hint;
5790 u64 extent_len;
5791 int i;
5792 /*
5793 * drop old extent pointer at first, then insert the
5794 * new pointers one bye one
5795 */
5796 btrfs_release_path(root, path);
5797 ret = btrfs_drop_extents(trans, root, inode, key.offset,
5798 key.offset + num_bytes,
5799 key.offset, &alloc_hint);
5800 BUG_ON(ret);
5801
5802 for (i = 0; i < nr_extents; i++) {
5803 if (ext_offset >= new_extents[i].num_bytes) {
5804 ext_offset -= new_extents[i].num_bytes;
5805 continue;
5806 }
5807 extent_len = min(new_extents[i].num_bytes -
5808 ext_offset, num_bytes);
5809
5810 ret = btrfs_insert_empty_item(trans, root,
5811 path, &key,
5812 sizeof(*fi));
5813 BUG_ON(ret);
5814
5815 leaf = path->nodes[0];
5816 fi = btrfs_item_ptr(leaf, path->slots[0],
5817 struct btrfs_file_extent_item);
5818 btrfs_set_file_extent_generation(leaf, fi,
5819 trans->transid);
5820 btrfs_set_file_extent_type(leaf, fi,
5821 BTRFS_FILE_EXTENT_REG);
5822 btrfs_set_file_extent_disk_bytenr(leaf, fi,
5823 new_extents[i].disk_bytenr);
5824 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
5825 new_extents[i].disk_num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -04005826 btrfs_set_file_extent_ram_bytes(leaf, fi,
5827 new_extents[i].ram_bytes);
5828
5829 btrfs_set_file_extent_compression(leaf, fi,
5830 new_extents[i].compression);
5831 btrfs_set_file_extent_encryption(leaf, fi,
5832 new_extents[i].encryption);
5833 btrfs_set_file_extent_other_encoding(leaf, fi,
5834 new_extents[i].other_encoding);
5835
Zheng Yan1a40e232008-09-26 10:09:34 -04005836 btrfs_set_file_extent_num_bytes(leaf, fi,
5837 extent_len);
5838 ext_offset += new_extents[i].offset;
5839 btrfs_set_file_extent_offset(leaf, fi,
5840 ext_offset);
5841 btrfs_mark_buffer_dirty(leaf);
5842
5843 btrfs_drop_extent_cache(inode, key.offset,
5844 key.offset + extent_len - 1, 0);
5845
5846 ret = btrfs_inc_extent_ref(trans, root,
5847 new_extents[i].disk_bytenr,
5848 new_extents[i].disk_num_bytes,
5849 leaf->start,
5850 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04005851 trans->transid, key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04005852 BUG_ON(ret);
5853 btrfs_release_path(root, path);
5854
Yan Zhenga76a3cd2008-10-09 11:46:29 -04005855 inode_add_bytes(inode, extent_len);
Zheng Yan1a40e232008-09-26 10:09:34 -04005856
5857 ext_offset = 0;
5858 num_bytes -= extent_len;
5859 key.offset += extent_len;
5860
5861 if (num_bytes == 0)
5862 break;
5863 }
5864 BUG_ON(i >= nr_extents);
Yan Zhengd899e052008-10-30 14:25:28 -04005865#endif
Zheng Yan1a40e232008-09-26 10:09:34 -04005866 }
5867
5868 if (extent_locked) {
Zheng Yan1a40e232008-09-26 10:09:34 -04005869 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
5870 lock_end, GFP_NOFS);
5871 extent_locked = 0;
5872 }
5873skip:
5874 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS &&
Yan Zheng86288a12009-01-21 10:49:16 -05005875 key.offset >= search_end)
Zheng Yan1a40e232008-09-26 10:09:34 -04005876 break;
5877
5878 cond_resched();
5879 }
5880 ret = 0;
5881out:
5882 btrfs_release_path(root, path);
5883 if (inode) {
5884 mutex_unlock(&inode->i_mutex);
5885 if (extent_locked) {
Zheng Yan1a40e232008-09-26 10:09:34 -04005886 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
5887 lock_end, GFP_NOFS);
5888 }
5889 iput(inode);
5890 }
5891 return ret;
5892}
5893
Zheng Yan1a40e232008-09-26 10:09:34 -04005894int btrfs_reloc_tree_cache_ref(struct btrfs_trans_handle *trans,
5895 struct btrfs_root *root,
5896 struct extent_buffer *buf, u64 orig_start)
5897{
5898 int level;
5899 int ret;
5900
5901 BUG_ON(btrfs_header_generation(buf) != trans->transid);
5902 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
5903
5904 level = btrfs_header_level(buf);
5905 if (level == 0) {
5906 struct btrfs_leaf_ref *ref;
5907 struct btrfs_leaf_ref *orig_ref;
5908
5909 orig_ref = btrfs_lookup_leaf_ref(root, orig_start);
5910 if (!orig_ref)
5911 return -ENOENT;
5912
5913 ref = btrfs_alloc_leaf_ref(root, orig_ref->nritems);
5914 if (!ref) {
5915 btrfs_free_leaf_ref(root, orig_ref);
5916 return -ENOMEM;
5917 }
5918
5919 ref->nritems = orig_ref->nritems;
5920 memcpy(ref->extents, orig_ref->extents,
5921 sizeof(ref->extents[0]) * ref->nritems);
5922
5923 btrfs_free_leaf_ref(root, orig_ref);
5924
5925 ref->root_gen = trans->transid;
5926 ref->bytenr = buf->start;
5927 ref->owner = btrfs_header_owner(buf);
5928 ref->generation = btrfs_header_generation(buf);
Chris Masonbd56b302009-02-04 09:27:02 -05005929
Zheng Yan1a40e232008-09-26 10:09:34 -04005930 ret = btrfs_add_leaf_ref(root, ref, 0);
5931 WARN_ON(ret);
5932 btrfs_free_leaf_ref(root, ref);
5933 }
5934 return 0;
5935}
5936
Chris Masond3977122009-01-05 21:25:51 -05005937static noinline int invalidate_extent_cache(struct btrfs_root *root,
Zheng Yan1a40e232008-09-26 10:09:34 -04005938 struct extent_buffer *leaf,
5939 struct btrfs_block_group_cache *group,
5940 struct btrfs_root *target_root)
5941{
5942 struct btrfs_key key;
5943 struct inode *inode = NULL;
5944 struct btrfs_file_extent_item *fi;
5945 u64 num_bytes;
5946 u64 skip_objectid = 0;
5947 u32 nritems;
5948 u32 i;
5949
5950 nritems = btrfs_header_nritems(leaf);
5951 for (i = 0; i < nritems; i++) {
5952 btrfs_item_key_to_cpu(leaf, &key, i);
5953 if (key.objectid == skip_objectid ||
5954 key.type != BTRFS_EXTENT_DATA_KEY)
5955 continue;
5956 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
5957 if (btrfs_file_extent_type(leaf, fi) ==
5958 BTRFS_FILE_EXTENT_INLINE)
5959 continue;
5960 if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
5961 continue;
5962 if (!inode || inode->i_ino != key.objectid) {
5963 iput(inode);
5964 inode = btrfs_ilookup(target_root->fs_info->sb,
5965 key.objectid, target_root, 1);
5966 }
5967 if (!inode) {
5968 skip_objectid = key.objectid;
5969 continue;
5970 }
5971 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
5972
5973 lock_extent(&BTRFS_I(inode)->io_tree, key.offset,
5974 key.offset + num_bytes - 1, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04005975 btrfs_drop_extent_cache(inode, key.offset,
5976 key.offset + num_bytes - 1, 1);
Zheng Yan1a40e232008-09-26 10:09:34 -04005977 unlock_extent(&BTRFS_I(inode)->io_tree, key.offset,
5978 key.offset + num_bytes - 1, GFP_NOFS);
5979 cond_resched();
5980 }
5981 iput(inode);
5982 return 0;
5983}
5984
Chris Masond3977122009-01-05 21:25:51 -05005985static noinline int replace_extents_in_leaf(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04005986 struct btrfs_root *root,
5987 struct extent_buffer *leaf,
5988 struct btrfs_block_group_cache *group,
5989 struct inode *reloc_inode)
5990{
5991 struct btrfs_key key;
5992 struct btrfs_key extent_key;
5993 struct btrfs_file_extent_item *fi;
5994 struct btrfs_leaf_ref *ref;
5995 struct disk_extent *new_extent;
5996 u64 bytenr;
5997 u64 num_bytes;
5998 u32 nritems;
5999 u32 i;
6000 int ext_index;
6001 int nr_extent;
6002 int ret;
6003
6004 new_extent = kmalloc(sizeof(*new_extent), GFP_NOFS);
6005 BUG_ON(!new_extent);
6006
6007 ref = btrfs_lookup_leaf_ref(root, leaf->start);
6008 BUG_ON(!ref);
6009
6010 ext_index = -1;
6011 nritems = btrfs_header_nritems(leaf);
6012 for (i = 0; i < nritems; i++) {
6013 btrfs_item_key_to_cpu(leaf, &key, i);
6014 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
6015 continue;
6016 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
6017 if (btrfs_file_extent_type(leaf, fi) ==
6018 BTRFS_FILE_EXTENT_INLINE)
6019 continue;
6020 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
6021 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
6022 if (bytenr == 0)
6023 continue;
6024
6025 ext_index++;
6026 if (bytenr >= group->key.objectid + group->key.offset ||
6027 bytenr + num_bytes <= group->key.objectid)
6028 continue;
6029
6030 extent_key.objectid = bytenr;
6031 extent_key.offset = num_bytes;
6032 extent_key.type = BTRFS_EXTENT_ITEM_KEY;
6033 nr_extent = 1;
6034 ret = get_new_locations(reloc_inode, &extent_key,
6035 group->key.objectid, 1,
6036 &new_extent, &nr_extent);
6037 if (ret > 0)
6038 continue;
6039 BUG_ON(ret < 0);
6040
6041 BUG_ON(ref->extents[ext_index].bytenr != bytenr);
6042 BUG_ON(ref->extents[ext_index].num_bytes != num_bytes);
6043 ref->extents[ext_index].bytenr = new_extent->disk_bytenr;
6044 ref->extents[ext_index].num_bytes = new_extent->disk_num_bytes;
6045
Zheng Yan1a40e232008-09-26 10:09:34 -04006046 btrfs_set_file_extent_disk_bytenr(leaf, fi,
6047 new_extent->disk_bytenr);
6048 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
6049 new_extent->disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04006050 btrfs_mark_buffer_dirty(leaf);
6051
6052 ret = btrfs_inc_extent_ref(trans, root,
6053 new_extent->disk_bytenr,
6054 new_extent->disk_num_bytes,
6055 leaf->start,
6056 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04006057 trans->transid, key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04006058 BUG_ON(ret);
Chris Mason56bec292009-03-13 10:10:06 -04006059
Zheng Yan1a40e232008-09-26 10:09:34 -04006060 ret = btrfs_free_extent(trans, root,
6061 bytenr, num_bytes, leaf->start,
6062 btrfs_header_owner(leaf),
6063 btrfs_header_generation(leaf),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04006064 key.objectid, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04006065 BUG_ON(ret);
6066 cond_resched();
6067 }
6068 kfree(new_extent);
6069 BUG_ON(ext_index + 1 != ref->nritems);
6070 btrfs_free_leaf_ref(root, ref);
6071 return 0;
6072}
6073
Yan Zhengf82d02d2008-10-29 14:49:05 -04006074int btrfs_free_reloc_root(struct btrfs_trans_handle *trans,
6075 struct btrfs_root *root)
Zheng Yan1a40e232008-09-26 10:09:34 -04006076{
6077 struct btrfs_root *reloc_root;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006078 int ret;
Zheng Yan1a40e232008-09-26 10:09:34 -04006079
6080 if (root->reloc_root) {
6081 reloc_root = root->reloc_root;
6082 root->reloc_root = NULL;
6083 list_add(&reloc_root->dead_list,
6084 &root->fs_info->dead_reloc_roots);
Yan Zhengf82d02d2008-10-29 14:49:05 -04006085
6086 btrfs_set_root_bytenr(&reloc_root->root_item,
6087 reloc_root->node->start);
6088 btrfs_set_root_level(&root->root_item,
6089 btrfs_header_level(reloc_root->node));
6090 memset(&reloc_root->root_item.drop_progress, 0,
6091 sizeof(struct btrfs_disk_key));
6092 reloc_root->root_item.drop_level = 0;
6093
6094 ret = btrfs_update_root(trans, root->fs_info->tree_root,
6095 &reloc_root->root_key,
6096 &reloc_root->root_item);
6097 BUG_ON(ret);
Zheng Yan1a40e232008-09-26 10:09:34 -04006098 }
6099 return 0;
6100}
6101
6102int btrfs_drop_dead_reloc_roots(struct btrfs_root *root)
6103{
6104 struct btrfs_trans_handle *trans;
6105 struct btrfs_root *reloc_root;
6106 struct btrfs_root *prev_root = NULL;
6107 struct list_head dead_roots;
6108 int ret;
6109 unsigned long nr;
6110
6111 INIT_LIST_HEAD(&dead_roots);
6112 list_splice_init(&root->fs_info->dead_reloc_roots, &dead_roots);
6113
6114 while (!list_empty(&dead_roots)) {
6115 reloc_root = list_entry(dead_roots.prev,
6116 struct btrfs_root, dead_list);
6117 list_del_init(&reloc_root->dead_list);
6118
6119 BUG_ON(reloc_root->commit_root != NULL);
6120 while (1) {
6121 trans = btrfs_join_transaction(root, 1);
6122 BUG_ON(!trans);
6123
6124 mutex_lock(&root->fs_info->drop_mutex);
6125 ret = btrfs_drop_snapshot(trans, reloc_root);
6126 if (ret != -EAGAIN)
6127 break;
6128 mutex_unlock(&root->fs_info->drop_mutex);
6129
6130 nr = trans->blocks_used;
6131 ret = btrfs_end_transaction(trans, root);
6132 BUG_ON(ret);
6133 btrfs_btree_balance_dirty(root, nr);
6134 }
6135
6136 free_extent_buffer(reloc_root->node);
6137
6138 ret = btrfs_del_root(trans, root->fs_info->tree_root,
6139 &reloc_root->root_key);
6140 BUG_ON(ret);
6141 mutex_unlock(&root->fs_info->drop_mutex);
6142
6143 nr = trans->blocks_used;
6144 ret = btrfs_end_transaction(trans, root);
6145 BUG_ON(ret);
6146 btrfs_btree_balance_dirty(root, nr);
6147
6148 kfree(prev_root);
6149 prev_root = reloc_root;
6150 }
6151 if (prev_root) {
6152 btrfs_remove_leaf_refs(prev_root, (u64)-1, 0);
6153 kfree(prev_root);
6154 }
6155 return 0;
6156}
6157
6158int btrfs_add_dead_reloc_root(struct btrfs_root *root)
6159{
6160 list_add(&root->dead_list, &root->fs_info->dead_reloc_roots);
6161 return 0;
6162}
6163
6164int btrfs_cleanup_reloc_trees(struct btrfs_root *root)
6165{
6166 struct btrfs_root *reloc_root;
6167 struct btrfs_trans_handle *trans;
6168 struct btrfs_key location;
6169 int found;
6170 int ret;
6171
6172 mutex_lock(&root->fs_info->tree_reloc_mutex);
6173 ret = btrfs_find_dead_roots(root, BTRFS_TREE_RELOC_OBJECTID, NULL);
6174 BUG_ON(ret);
6175 found = !list_empty(&root->fs_info->dead_reloc_roots);
6176 mutex_unlock(&root->fs_info->tree_reloc_mutex);
6177
6178 if (found) {
6179 trans = btrfs_start_transaction(root, 1);
6180 BUG_ON(!trans);
6181 ret = btrfs_commit_transaction(trans, root);
6182 BUG_ON(ret);
6183 }
6184
6185 location.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
6186 location.offset = (u64)-1;
6187 location.type = BTRFS_ROOT_ITEM_KEY;
6188
6189 reloc_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
6190 BUG_ON(!reloc_root);
6191 btrfs_orphan_cleanup(reloc_root);
6192 return 0;
6193}
6194
Chris Masond3977122009-01-05 21:25:51 -05006195static noinline int init_reloc_tree(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04006196 struct btrfs_root *root)
6197{
6198 struct btrfs_root *reloc_root;
6199 struct extent_buffer *eb;
6200 struct btrfs_root_item *root_item;
6201 struct btrfs_key root_key;
6202 int ret;
6203
6204 BUG_ON(!root->ref_cows);
6205 if (root->reloc_root)
6206 return 0;
6207
6208 root_item = kmalloc(sizeof(*root_item), GFP_NOFS);
6209 BUG_ON(!root_item);
6210
6211 ret = btrfs_copy_root(trans, root, root->commit_root,
6212 &eb, BTRFS_TREE_RELOC_OBJECTID);
6213 BUG_ON(ret);
6214
6215 root_key.objectid = BTRFS_TREE_RELOC_OBJECTID;
6216 root_key.offset = root->root_key.objectid;
6217 root_key.type = BTRFS_ROOT_ITEM_KEY;
6218
6219 memcpy(root_item, &root->root_item, sizeof(root_item));
6220 btrfs_set_root_refs(root_item, 0);
6221 btrfs_set_root_bytenr(root_item, eb->start);
6222 btrfs_set_root_level(root_item, btrfs_header_level(eb));
Yan Zheng84234f32008-10-29 14:49:05 -04006223 btrfs_set_root_generation(root_item, trans->transid);
Zheng Yan1a40e232008-09-26 10:09:34 -04006224
6225 btrfs_tree_unlock(eb);
6226 free_extent_buffer(eb);
6227
6228 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
6229 &root_key, root_item);
6230 BUG_ON(ret);
6231 kfree(root_item);
6232
6233 reloc_root = btrfs_read_fs_root_no_radix(root->fs_info->tree_root,
6234 &root_key);
6235 BUG_ON(!reloc_root);
6236 reloc_root->last_trans = trans->transid;
6237 reloc_root->commit_root = NULL;
6238 reloc_root->ref_tree = &root->fs_info->reloc_ref_tree;
6239
6240 root->reloc_root = reloc_root;
6241 return 0;
6242}
6243
6244/*
6245 * Core function of space balance.
6246 *
6247 * The idea is using reloc trees to relocate tree blocks in reference
Yan Zhengf82d02d2008-10-29 14:49:05 -04006248 * counted roots. There is one reloc tree for each subvol, and all
6249 * reloc trees share same root key objectid. Reloc trees are snapshots
6250 * of the latest committed roots of subvols (root->commit_root).
6251 *
6252 * To relocate a tree block referenced by a subvol, there are two steps.
6253 * COW the block through subvol's reloc tree, then update block pointer
6254 * in the subvol to point to the new block. Since all reloc trees share
6255 * same root key objectid, doing special handing for tree blocks owned
6256 * by them is easy. Once a tree block has been COWed in one reloc tree,
6257 * we can use the resulting new block directly when the same block is
6258 * required to COW again through other reloc trees. By this way, relocated
6259 * tree blocks are shared between reloc trees, so they are also shared
6260 * between subvols.
Zheng Yan1a40e232008-09-26 10:09:34 -04006261 */
Chris Masond3977122009-01-05 21:25:51 -05006262static noinline int relocate_one_path(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04006263 struct btrfs_root *root,
6264 struct btrfs_path *path,
6265 struct btrfs_key *first_key,
6266 struct btrfs_ref_path *ref_path,
6267 struct btrfs_block_group_cache *group,
6268 struct inode *reloc_inode)
6269{
6270 struct btrfs_root *reloc_root;
6271 struct extent_buffer *eb = NULL;
6272 struct btrfs_key *keys;
6273 u64 *nodes;
6274 int level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006275 int shared_level;
Zheng Yan1a40e232008-09-26 10:09:34 -04006276 int lowest_level = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04006277 int ret;
6278
6279 if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
6280 lowest_level = ref_path->owner_objectid;
6281
Yan Zhengf82d02d2008-10-29 14:49:05 -04006282 if (!root->ref_cows) {
Zheng Yan1a40e232008-09-26 10:09:34 -04006283 path->lowest_level = lowest_level;
6284 ret = btrfs_search_slot(trans, root, first_key, path, 0, 1);
6285 BUG_ON(ret < 0);
6286 path->lowest_level = 0;
6287 btrfs_release_path(root, path);
6288 return 0;
6289 }
6290
Zheng Yan1a40e232008-09-26 10:09:34 -04006291 mutex_lock(&root->fs_info->tree_reloc_mutex);
6292 ret = init_reloc_tree(trans, root);
6293 BUG_ON(ret);
6294 reloc_root = root->reloc_root;
6295
Yan Zhengf82d02d2008-10-29 14:49:05 -04006296 shared_level = ref_path->shared_level;
6297 ref_path->shared_level = BTRFS_MAX_LEVEL - 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04006298
Yan Zhengf82d02d2008-10-29 14:49:05 -04006299 keys = ref_path->node_keys;
6300 nodes = ref_path->new_nodes;
6301 memset(&keys[shared_level + 1], 0,
6302 sizeof(*keys) * (BTRFS_MAX_LEVEL - shared_level - 1));
6303 memset(&nodes[shared_level + 1], 0,
6304 sizeof(*nodes) * (BTRFS_MAX_LEVEL - shared_level - 1));
Zheng Yan1a40e232008-09-26 10:09:34 -04006305
Yan Zhengf82d02d2008-10-29 14:49:05 -04006306 if (nodes[lowest_level] == 0) {
6307 path->lowest_level = lowest_level;
6308 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
6309 0, 1);
6310 BUG_ON(ret);
6311 for (level = lowest_level; level < BTRFS_MAX_LEVEL; level++) {
6312 eb = path->nodes[level];
6313 if (!eb || eb == reloc_root->node)
6314 break;
6315 nodes[level] = eb->start;
6316 if (level == 0)
6317 btrfs_item_key_to_cpu(eb, &keys[level], 0);
6318 else
6319 btrfs_node_key_to_cpu(eb, &keys[level], 0);
6320 }
Yan Zheng2b820322008-11-17 21:11:30 -05006321 if (nodes[0] &&
6322 ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04006323 eb = path->nodes[0];
6324 ret = replace_extents_in_leaf(trans, reloc_root, eb,
6325 group, reloc_inode);
6326 BUG_ON(ret);
6327 }
6328 btrfs_release_path(reloc_root, path);
6329 } else {
Zheng Yan1a40e232008-09-26 10:09:34 -04006330 ret = btrfs_merge_path(trans, reloc_root, keys, nodes,
Yan Zhengf82d02d2008-10-29 14:49:05 -04006331 lowest_level);
Zheng Yan1a40e232008-09-26 10:09:34 -04006332 BUG_ON(ret);
6333 }
6334
Zheng Yan1a40e232008-09-26 10:09:34 -04006335 /*
6336 * replace tree blocks in the fs tree with tree blocks in
6337 * the reloc tree.
6338 */
6339 ret = btrfs_merge_path(trans, root, keys, nodes, lowest_level);
6340 BUG_ON(ret < 0);
6341
6342 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04006343 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
6344 0, 0);
6345 BUG_ON(ret);
6346 extent_buffer_get(path->nodes[0]);
6347 eb = path->nodes[0];
6348 btrfs_release_path(reloc_root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04006349 ret = invalidate_extent_cache(reloc_root, eb, group, root);
6350 BUG_ON(ret);
6351 free_extent_buffer(eb);
6352 }
Zheng Yan1a40e232008-09-26 10:09:34 -04006353
Yan Zhengf82d02d2008-10-29 14:49:05 -04006354 mutex_unlock(&root->fs_info->tree_reloc_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04006355 path->lowest_level = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04006356 return 0;
6357}
6358
Chris Masond3977122009-01-05 21:25:51 -05006359static noinline int relocate_tree_block(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04006360 struct btrfs_root *root,
6361 struct btrfs_path *path,
6362 struct btrfs_key *first_key,
6363 struct btrfs_ref_path *ref_path)
6364{
6365 int ret;
Zheng Yan1a40e232008-09-26 10:09:34 -04006366
6367 ret = relocate_one_path(trans, root, path, first_key,
6368 ref_path, NULL, NULL);
6369 BUG_ON(ret);
6370
Zheng Yan1a40e232008-09-26 10:09:34 -04006371 return 0;
6372}
6373
Chris Masond3977122009-01-05 21:25:51 -05006374static noinline int del_extent_zero(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04006375 struct btrfs_root *extent_root,
6376 struct btrfs_path *path,
6377 struct btrfs_key *extent_key)
6378{
6379 int ret;
6380
Zheng Yan1a40e232008-09-26 10:09:34 -04006381 ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1);
6382 if (ret)
6383 goto out;
6384 ret = btrfs_del_item(trans, extent_root, path);
6385out:
Chris Masonedbd8d42007-12-21 16:27:24 -05006386 btrfs_release_path(extent_root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04006387 return ret;
6388}
6389
Chris Masond3977122009-01-05 21:25:51 -05006390static noinline struct btrfs_root *read_ref_root(struct btrfs_fs_info *fs_info,
Zheng Yan1a40e232008-09-26 10:09:34 -04006391 struct btrfs_ref_path *ref_path)
6392{
6393 struct btrfs_key root_key;
6394
6395 root_key.objectid = ref_path->root_objectid;
6396 root_key.type = BTRFS_ROOT_ITEM_KEY;
6397 if (is_cowonly_root(ref_path->root_objectid))
6398 root_key.offset = 0;
6399 else
6400 root_key.offset = (u64)-1;
6401
6402 return btrfs_read_fs_root_no_name(fs_info, &root_key);
6403}
6404
Chris Masond3977122009-01-05 21:25:51 -05006405static noinline int relocate_one_extent(struct btrfs_root *extent_root,
Zheng Yan1a40e232008-09-26 10:09:34 -04006406 struct btrfs_path *path,
6407 struct btrfs_key *extent_key,
6408 struct btrfs_block_group_cache *group,
6409 struct inode *reloc_inode, int pass)
6410{
6411 struct btrfs_trans_handle *trans;
6412 struct btrfs_root *found_root;
6413 struct btrfs_ref_path *ref_path = NULL;
6414 struct disk_extent *new_extents = NULL;
6415 int nr_extents = 0;
6416 int loops;
6417 int ret;
6418 int level;
6419 struct btrfs_key first_key;
6420 u64 prev_block = 0;
6421
Zheng Yan1a40e232008-09-26 10:09:34 -04006422
6423 trans = btrfs_start_transaction(extent_root, 1);
6424 BUG_ON(!trans);
6425
6426 if (extent_key->objectid == 0) {
6427 ret = del_extent_zero(trans, extent_root, path, extent_key);
6428 goto out;
6429 }
6430
6431 ref_path = kmalloc(sizeof(*ref_path), GFP_NOFS);
6432 if (!ref_path) {
Chris Masond3977122009-01-05 21:25:51 -05006433 ret = -ENOMEM;
6434 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04006435 }
6436
6437 for (loops = 0; ; loops++) {
6438 if (loops == 0) {
6439 ret = btrfs_first_ref_path(trans, extent_root, ref_path,
6440 extent_key->objectid);
6441 } else {
6442 ret = btrfs_next_ref_path(trans, extent_root, ref_path);
6443 }
6444 if (ret < 0)
6445 goto out;
6446 if (ret > 0)
6447 break;
6448
6449 if (ref_path->root_objectid == BTRFS_TREE_LOG_OBJECTID ||
6450 ref_path->root_objectid == BTRFS_TREE_RELOC_OBJECTID)
6451 continue;
6452
6453 found_root = read_ref_root(extent_root->fs_info, ref_path);
6454 BUG_ON(!found_root);
6455 /*
6456 * for reference counted tree, only process reference paths
6457 * rooted at the latest committed root.
6458 */
6459 if (found_root->ref_cows &&
6460 ref_path->root_generation != found_root->root_key.offset)
6461 continue;
6462
6463 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
6464 if (pass == 0) {
6465 /*
6466 * copy data extents to new locations
6467 */
6468 u64 group_start = group->key.objectid;
6469 ret = relocate_data_extent(reloc_inode,
6470 extent_key,
6471 group_start);
6472 if (ret < 0)
6473 goto out;
6474 break;
6475 }
6476 level = 0;
6477 } else {
6478 level = ref_path->owner_objectid;
6479 }
6480
6481 if (prev_block != ref_path->nodes[level]) {
6482 struct extent_buffer *eb;
6483 u64 block_start = ref_path->nodes[level];
6484 u64 block_size = btrfs_level_size(found_root, level);
6485
6486 eb = read_tree_block(found_root, block_start,
6487 block_size, 0);
6488 btrfs_tree_lock(eb);
6489 BUG_ON(level != btrfs_header_level(eb));
6490
6491 if (level == 0)
6492 btrfs_item_key_to_cpu(eb, &first_key, 0);
6493 else
6494 btrfs_node_key_to_cpu(eb, &first_key, 0);
6495
6496 btrfs_tree_unlock(eb);
6497 free_extent_buffer(eb);
6498 prev_block = block_start;
6499 }
6500
Yan Zheng24562422009-02-12 14:14:53 -05006501 mutex_lock(&extent_root->fs_info->trans_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05006502 btrfs_record_root_in_trans(found_root);
Yan Zheng24562422009-02-12 14:14:53 -05006503 mutex_unlock(&extent_root->fs_info->trans_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05006504 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
6505 /*
6506 * try to update data extent references while
6507 * keeping metadata shared between snapshots.
6508 */
6509 if (pass == 1) {
6510 ret = relocate_one_path(trans, found_root,
6511 path, &first_key, ref_path,
6512 group, reloc_inode);
6513 if (ret < 0)
6514 goto out;
6515 continue;
6516 }
Zheng Yan1a40e232008-09-26 10:09:34 -04006517 /*
6518 * use fallback method to process the remaining
6519 * references.
6520 */
6521 if (!new_extents) {
6522 u64 group_start = group->key.objectid;
Yan Zhengd899e052008-10-30 14:25:28 -04006523 new_extents = kmalloc(sizeof(*new_extents),
6524 GFP_NOFS);
6525 nr_extents = 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04006526 ret = get_new_locations(reloc_inode,
6527 extent_key,
Yan Zhengd899e052008-10-30 14:25:28 -04006528 group_start, 1,
Zheng Yan1a40e232008-09-26 10:09:34 -04006529 &new_extents,
6530 &nr_extents);
Yan Zhengd899e052008-10-30 14:25:28 -04006531 if (ret)
Zheng Yan1a40e232008-09-26 10:09:34 -04006532 goto out;
6533 }
Zheng Yan1a40e232008-09-26 10:09:34 -04006534 ret = replace_one_extent(trans, found_root,
6535 path, extent_key,
6536 &first_key, ref_path,
6537 new_extents, nr_extents);
Yan Zhenge4404d62008-12-12 10:03:26 -05006538 } else {
Zheng Yan1a40e232008-09-26 10:09:34 -04006539 ret = relocate_tree_block(trans, found_root, path,
6540 &first_key, ref_path);
Zheng Yan1a40e232008-09-26 10:09:34 -04006541 }
6542 if (ret < 0)
6543 goto out;
6544 }
6545 ret = 0;
6546out:
6547 btrfs_end_transaction(trans, extent_root);
6548 kfree(new_extents);
6549 kfree(ref_path);
Chris Masonedbd8d42007-12-21 16:27:24 -05006550 return ret;
6551}
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006552#endif
Chris Masonedbd8d42007-12-21 16:27:24 -05006553
Chris Masonec44a352008-04-28 15:29:52 -04006554static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
6555{
6556 u64 num_devices;
6557 u64 stripped = BTRFS_BLOCK_GROUP_RAID0 |
6558 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
6559
Yan Zheng2b820322008-11-17 21:11:30 -05006560 num_devices = root->fs_info->fs_devices->rw_devices;
Chris Masonec44a352008-04-28 15:29:52 -04006561 if (num_devices == 1) {
6562 stripped |= BTRFS_BLOCK_GROUP_DUP;
6563 stripped = flags & ~stripped;
6564
6565 /* turn raid0 into single device chunks */
6566 if (flags & BTRFS_BLOCK_GROUP_RAID0)
6567 return stripped;
6568
6569 /* turn mirroring into duplication */
6570 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
6571 BTRFS_BLOCK_GROUP_RAID10))
6572 return stripped | BTRFS_BLOCK_GROUP_DUP;
6573 return flags;
6574 } else {
6575 /* they already had raid on here, just return */
Chris Masonec44a352008-04-28 15:29:52 -04006576 if (flags & stripped)
6577 return flags;
6578
6579 stripped |= BTRFS_BLOCK_GROUP_DUP;
6580 stripped = flags & ~stripped;
6581
6582 /* switch duplicated blocks with raid1 */
6583 if (flags & BTRFS_BLOCK_GROUP_DUP)
6584 return stripped | BTRFS_BLOCK_GROUP_RAID1;
6585
6586 /* turn single device chunks into raid0 */
6587 return stripped | BTRFS_BLOCK_GROUP_RAID0;
6588 }
6589 return flags;
6590}
6591
Christoph Hellwigb2950862008-12-02 09:54:17 -05006592static int __alloc_chunk_for_shrink(struct btrfs_root *root,
Chris Mason0ef3e662008-05-24 14:04:53 -04006593 struct btrfs_block_group_cache *shrink_block_group,
6594 int force)
6595{
6596 struct btrfs_trans_handle *trans;
6597 u64 new_alloc_flags;
6598 u64 calc;
6599
Chris Masonc286ac42008-07-22 23:06:41 -04006600 spin_lock(&shrink_block_group->lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006601 if (btrfs_block_group_used(&shrink_block_group->item) +
6602 shrink_block_group->reserved > 0) {
Chris Masonc286ac42008-07-22 23:06:41 -04006603 spin_unlock(&shrink_block_group->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04006604
Chris Mason0ef3e662008-05-24 14:04:53 -04006605 trans = btrfs_start_transaction(root, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04006606 spin_lock(&shrink_block_group->lock);
Chris Mason7d9eb122008-07-08 14:19:17 -04006607
Chris Mason0ef3e662008-05-24 14:04:53 -04006608 new_alloc_flags = update_block_group_flags(root,
6609 shrink_block_group->flags);
6610 if (new_alloc_flags != shrink_block_group->flags) {
6611 calc =
6612 btrfs_block_group_used(&shrink_block_group->item);
6613 } else {
6614 calc = shrink_block_group->key.offset;
6615 }
Chris Masonc286ac42008-07-22 23:06:41 -04006616 spin_unlock(&shrink_block_group->lock);
6617
Chris Mason0ef3e662008-05-24 14:04:53 -04006618 do_chunk_alloc(trans, root->fs_info->extent_root,
6619 calc + 2 * 1024 * 1024, new_alloc_flags, force);
Chris Mason7d9eb122008-07-08 14:19:17 -04006620
Chris Mason0ef3e662008-05-24 14:04:53 -04006621 btrfs_end_transaction(trans, root);
Chris Masonc286ac42008-07-22 23:06:41 -04006622 } else
6623 spin_unlock(&shrink_block_group->lock);
Chris Mason0ef3e662008-05-24 14:04:53 -04006624 return 0;
6625}
6626
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006627
6628int btrfs_prepare_block_group_relocation(struct btrfs_root *root,
6629 struct btrfs_block_group_cache *group)
6630
6631{
6632 __alloc_chunk_for_shrink(root, group, 1);
6633 set_block_group_readonly(group);
6634 return 0;
6635}
6636
6637#if 0
Zheng Yan1a40e232008-09-26 10:09:34 -04006638static int __insert_orphan_inode(struct btrfs_trans_handle *trans,
6639 struct btrfs_root *root,
6640 u64 objectid, u64 size)
6641{
6642 struct btrfs_path *path;
6643 struct btrfs_inode_item *item;
6644 struct extent_buffer *leaf;
6645 int ret;
6646
6647 path = btrfs_alloc_path();
6648 if (!path)
6649 return -ENOMEM;
6650
Chris Masonb9473432009-03-13 11:00:37 -04006651 path->leave_spinning = 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04006652 ret = btrfs_insert_empty_inode(trans, root, path, objectid);
6653 if (ret)
6654 goto out;
6655
6656 leaf = path->nodes[0];
6657 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_inode_item);
6658 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
6659 btrfs_set_inode_generation(leaf, item, 1);
6660 btrfs_set_inode_size(leaf, item, size);
6661 btrfs_set_inode_mode(leaf, item, S_IFREG | 0600);
Yan Zheng0403e472008-12-10 20:32:51 -05006662 btrfs_set_inode_flags(leaf, item, BTRFS_INODE_NOCOMPRESS);
Zheng Yan1a40e232008-09-26 10:09:34 -04006663 btrfs_mark_buffer_dirty(leaf);
6664 btrfs_release_path(root, path);
6665out:
6666 btrfs_free_path(path);
6667 return ret;
6668}
6669
Chris Masond3977122009-01-05 21:25:51 -05006670static noinline struct inode *create_reloc_inode(struct btrfs_fs_info *fs_info,
Zheng Yan1a40e232008-09-26 10:09:34 -04006671 struct btrfs_block_group_cache *group)
6672{
6673 struct inode *inode = NULL;
6674 struct btrfs_trans_handle *trans;
6675 struct btrfs_root *root;
6676 struct btrfs_key root_key;
6677 u64 objectid = BTRFS_FIRST_FREE_OBJECTID;
6678 int err = 0;
6679
6680 root_key.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
6681 root_key.type = BTRFS_ROOT_ITEM_KEY;
6682 root_key.offset = (u64)-1;
6683 root = btrfs_read_fs_root_no_name(fs_info, &root_key);
6684 if (IS_ERR(root))
6685 return ERR_CAST(root);
6686
6687 trans = btrfs_start_transaction(root, 1);
6688 BUG_ON(!trans);
6689
6690 err = btrfs_find_free_objectid(trans, root, objectid, &objectid);
6691 if (err)
6692 goto out;
6693
6694 err = __insert_orphan_inode(trans, root, objectid, group->key.offset);
6695 BUG_ON(err);
6696
6697 err = btrfs_insert_file_extent(trans, root, objectid, 0, 0, 0,
Chris Masonc8b97812008-10-29 14:49:59 -04006698 group->key.offset, 0, group->key.offset,
6699 0, 0, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04006700 BUG_ON(err);
6701
6702 inode = btrfs_iget_locked(root->fs_info->sb, objectid, root);
6703 if (inode->i_state & I_NEW) {
6704 BTRFS_I(inode)->root = root;
6705 BTRFS_I(inode)->location.objectid = objectid;
6706 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
6707 BTRFS_I(inode)->location.offset = 0;
6708 btrfs_read_locked_inode(inode);
6709 unlock_new_inode(inode);
6710 BUG_ON(is_bad_inode(inode));
6711 } else {
6712 BUG_ON(1);
6713 }
Yan Zheng17d217f2008-12-12 10:03:38 -05006714 BTRFS_I(inode)->index_cnt = group->key.objectid;
Zheng Yan1a40e232008-09-26 10:09:34 -04006715
6716 err = btrfs_orphan_add(trans, inode);
6717out:
6718 btrfs_end_transaction(trans, root);
6719 if (err) {
6720 if (inode)
6721 iput(inode);
6722 inode = ERR_PTR(err);
6723 }
6724 return inode;
6725}
6726
Yan Zheng17d217f2008-12-12 10:03:38 -05006727int btrfs_reloc_clone_csums(struct inode *inode, u64 file_pos, u64 len)
6728{
6729
6730 struct btrfs_ordered_sum *sums;
6731 struct btrfs_sector_sum *sector_sum;
6732 struct btrfs_ordered_extent *ordered;
6733 struct btrfs_root *root = BTRFS_I(inode)->root;
6734 struct list_head list;
6735 size_t offset;
6736 int ret;
6737 u64 disk_bytenr;
6738
6739 INIT_LIST_HEAD(&list);
6740
6741 ordered = btrfs_lookup_ordered_extent(inode, file_pos);
6742 BUG_ON(ordered->file_offset != file_pos || ordered->len != len);
6743
6744 disk_bytenr = file_pos + BTRFS_I(inode)->index_cnt;
Yan Zheng07d400a2009-01-06 11:42:00 -05006745 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, disk_bytenr,
Yan Zheng17d217f2008-12-12 10:03:38 -05006746 disk_bytenr + len - 1, &list);
6747
6748 while (!list_empty(&list)) {
6749 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
6750 list_del_init(&sums->list);
6751
6752 sector_sum = sums->sums;
6753 sums->bytenr = ordered->start;
6754
6755 offset = 0;
6756 while (offset < sums->len) {
6757 sector_sum->bytenr += ordered->start - disk_bytenr;
6758 sector_sum++;
6759 offset += root->sectorsize;
6760 }
6761
6762 btrfs_add_ordered_sum(inode, ordered, sums);
6763 }
6764 btrfs_put_ordered_extent(ordered);
6765 return 0;
6766}
6767
Zheng Yan1a40e232008-09-26 10:09:34 -04006768int btrfs_relocate_block_group(struct btrfs_root *root, u64 group_start)
Chris Masonedbd8d42007-12-21 16:27:24 -05006769{
6770 struct btrfs_trans_handle *trans;
Chris Masonedbd8d42007-12-21 16:27:24 -05006771 struct btrfs_path *path;
Zheng Yan1a40e232008-09-26 10:09:34 -04006772 struct btrfs_fs_info *info = root->fs_info;
6773 struct extent_buffer *leaf;
6774 struct inode *reloc_inode;
6775 struct btrfs_block_group_cache *block_group;
6776 struct btrfs_key key;
Yan Zhengd899e052008-10-30 14:25:28 -04006777 u64 skipped;
Chris Masonedbd8d42007-12-21 16:27:24 -05006778 u64 cur_byte;
6779 u64 total_found;
Chris Masonedbd8d42007-12-21 16:27:24 -05006780 u32 nritems;
6781 int ret;
Chris Masona061fc82008-05-07 11:43:44 -04006782 int progress;
Zheng Yan1a40e232008-09-26 10:09:34 -04006783 int pass = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05006784
Chris Masonedbd8d42007-12-21 16:27:24 -05006785 root = root->fs_info->extent_root;
Zheng Yan1a40e232008-09-26 10:09:34 -04006786
6787 block_group = btrfs_lookup_block_group(info, group_start);
6788 BUG_ON(!block_group);
Chris Masonedbd8d42007-12-21 16:27:24 -05006789
Chris Masond3977122009-01-05 21:25:51 -05006790 printk(KERN_INFO "btrfs relocating block group %llu flags %llu\n",
Zheng Yan1a40e232008-09-26 10:09:34 -04006791 (unsigned long long)block_group->key.objectid,
6792 (unsigned long long)block_group->flags);
Chris Mason323da792008-05-09 11:46:48 -04006793
Zheng Yan1a40e232008-09-26 10:09:34 -04006794 path = btrfs_alloc_path();
6795 BUG_ON(!path);
Chris Mason323da792008-05-09 11:46:48 -04006796
Zheng Yan1a40e232008-09-26 10:09:34 -04006797 reloc_inode = create_reloc_inode(info, block_group);
6798 BUG_ON(IS_ERR(reloc_inode));
6799
Zheng Yan1a40e232008-09-26 10:09:34 -04006800 __alloc_chunk_for_shrink(root, block_group, 1);
Yan Zhengc146afa2008-11-12 14:34:12 -05006801 set_block_group_readonly(block_group);
Zheng Yan1a40e232008-09-26 10:09:34 -04006802
Zheng Yan1a40e232008-09-26 10:09:34 -04006803 btrfs_start_delalloc_inodes(info->tree_root);
6804 btrfs_wait_ordered_extents(info->tree_root, 0);
Chris Mason0ef3e662008-05-24 14:04:53 -04006805again:
Yan Zhengd899e052008-10-30 14:25:28 -04006806 skipped = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05006807 total_found = 0;
Chris Masona061fc82008-05-07 11:43:44 -04006808 progress = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04006809 key.objectid = block_group->key.objectid;
Chris Masonedbd8d42007-12-21 16:27:24 -05006810 key.offset = 0;
6811 key.type = 0;
Yan73e48b22008-01-03 14:14:39 -05006812 cur_byte = key.objectid;
Chris Mason4313b392008-01-03 09:08:48 -05006813
Zheng Yan1a40e232008-09-26 10:09:34 -04006814 trans = btrfs_start_transaction(info->tree_root, 1);
6815 btrfs_commit_transaction(trans, info->tree_root);
Chris Masonea8c2812008-08-04 23:17:27 -04006816
Zheng Yan1a40e232008-09-26 10:09:34 -04006817 mutex_lock(&root->fs_info->cleaner_mutex);
6818 btrfs_clean_old_snapshots(info->tree_root);
6819 btrfs_remove_leaf_refs(info->tree_root, (u64)-1, 1);
6820 mutex_unlock(&root->fs_info->cleaner_mutex);
Chris Masonea8c2812008-08-04 23:17:27 -04006821
Chris Mason56bec292009-03-13 10:10:06 -04006822 trans = btrfs_start_transaction(info->tree_root, 1);
6823 btrfs_commit_transaction(trans, info->tree_root);
6824
Chris Masond3977122009-01-05 21:25:51 -05006825 while (1) {
Chris Masonedbd8d42007-12-21 16:27:24 -05006826 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6827 if (ret < 0)
6828 goto out;
Chris Mason7d9eb122008-07-08 14:19:17 -04006829next:
Chris Masonedbd8d42007-12-21 16:27:24 -05006830 leaf = path->nodes[0];
Chris Masonedbd8d42007-12-21 16:27:24 -05006831 nritems = btrfs_header_nritems(leaf);
Yan73e48b22008-01-03 14:14:39 -05006832 if (path->slots[0] >= nritems) {
6833 ret = btrfs_next_leaf(root, path);
6834 if (ret < 0)
6835 goto out;
6836 if (ret == 1) {
6837 ret = 0;
6838 break;
Chris Masonedbd8d42007-12-21 16:27:24 -05006839 }
Yan73e48b22008-01-03 14:14:39 -05006840 leaf = path->nodes[0];
6841 nritems = btrfs_header_nritems(leaf);
6842 }
6843
Zheng Yan1a40e232008-09-26 10:09:34 -04006844 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Chris Mason725c8462008-01-04 16:47:16 -05006845
Zheng Yan1a40e232008-09-26 10:09:34 -04006846 if (key.objectid >= block_group->key.objectid +
6847 block_group->key.offset)
Chris Mason8f18cf12008-04-25 16:53:30 -04006848 break;
6849
Chris Mason725c8462008-01-04 16:47:16 -05006850 if (progress && need_resched()) {
Chris Mason725c8462008-01-04 16:47:16 -05006851 btrfs_release_path(root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04006852 cond_resched();
Chris Mason725c8462008-01-04 16:47:16 -05006853 progress = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04006854 continue;
Chris Mason725c8462008-01-04 16:47:16 -05006855 }
6856 progress = 1;
6857
Zheng Yan1a40e232008-09-26 10:09:34 -04006858 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY ||
6859 key.objectid + key.offset <= cur_byte) {
Yan73e48b22008-01-03 14:14:39 -05006860 path->slots[0]++;
Chris Masonedbd8d42007-12-21 16:27:24 -05006861 goto next;
6862 }
Yan73e48b22008-01-03 14:14:39 -05006863
Chris Masonedbd8d42007-12-21 16:27:24 -05006864 total_found++;
Zheng Yan1a40e232008-09-26 10:09:34 -04006865 cur_byte = key.objectid + key.offset;
Chris Masonedbd8d42007-12-21 16:27:24 -05006866 btrfs_release_path(root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04006867
6868 __alloc_chunk_for_shrink(root, block_group, 0);
6869 ret = relocate_one_extent(root, path, &key, block_group,
6870 reloc_inode, pass);
6871 BUG_ON(ret < 0);
Yan Zhengd899e052008-10-30 14:25:28 -04006872 if (ret > 0)
6873 skipped++;
Zheng Yan1a40e232008-09-26 10:09:34 -04006874
6875 key.objectid = cur_byte;
6876 key.type = 0;
6877 key.offset = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05006878 }
6879
6880 btrfs_release_path(root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04006881
6882 if (pass == 0) {
6883 btrfs_wait_ordered_range(reloc_inode, 0, (u64)-1);
6884 invalidate_mapping_pages(reloc_inode->i_mapping, 0, -1);
Zheng Yan1a40e232008-09-26 10:09:34 -04006885 }
Chris Masonedbd8d42007-12-21 16:27:24 -05006886
6887 if (total_found > 0) {
Chris Masond3977122009-01-05 21:25:51 -05006888 printk(KERN_INFO "btrfs found %llu extents in pass %d\n",
Zheng Yan1a40e232008-09-26 10:09:34 -04006889 (unsigned long long)total_found, pass);
6890 pass++;
Yan Zhengd899e052008-10-30 14:25:28 -04006891 if (total_found == skipped && pass > 2) {
6892 iput(reloc_inode);
6893 reloc_inode = create_reloc_inode(info, block_group);
6894 pass = 0;
6895 }
Chris Masonedbd8d42007-12-21 16:27:24 -05006896 goto again;
6897 }
6898
Zheng Yan1a40e232008-09-26 10:09:34 -04006899 /* delete reloc_inode */
6900 iput(reloc_inode);
Chris Mason7d9eb122008-07-08 14:19:17 -04006901
Zheng Yan1a40e232008-09-26 10:09:34 -04006902 /* unpin extents in this range */
6903 trans = btrfs_start_transaction(info->tree_root, 1);
6904 btrfs_commit_transaction(trans, info->tree_root);
Chris Mason0ef3e662008-05-24 14:04:53 -04006905
Zheng Yan1a40e232008-09-26 10:09:34 -04006906 spin_lock(&block_group->lock);
6907 WARN_ON(block_group->pinned > 0);
6908 WARN_ON(block_group->reserved > 0);
6909 WARN_ON(btrfs_block_group_used(&block_group->item) > 0);
6910 spin_unlock(&block_group->lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04006911 btrfs_put_block_group(block_group);
Zheng Yan1a40e232008-09-26 10:09:34 -04006912 ret = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05006913out:
Zheng Yan1a40e232008-09-26 10:09:34 -04006914 btrfs_free_path(path);
Chris Masonedbd8d42007-12-21 16:27:24 -05006915 return ret;
6916}
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006917#endif
Chris Masonedbd8d42007-12-21 16:27:24 -05006918
Christoph Hellwigb2950862008-12-02 09:54:17 -05006919static int find_first_block_group(struct btrfs_root *root,
6920 struct btrfs_path *path, struct btrfs_key *key)
Chris Mason0b86a832008-03-24 15:01:56 -04006921{
Chris Mason925baed2008-06-25 16:01:30 -04006922 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006923 struct btrfs_key found_key;
6924 struct extent_buffer *leaf;
6925 int slot;
6926
6927 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
6928 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04006929 goto out;
6930
Chris Masond3977122009-01-05 21:25:51 -05006931 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04006932 slot = path->slots[0];
6933 leaf = path->nodes[0];
6934 if (slot >= btrfs_header_nritems(leaf)) {
6935 ret = btrfs_next_leaf(root, path);
6936 if (ret == 0)
6937 continue;
6938 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04006939 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04006940 break;
6941 }
6942 btrfs_item_key_to_cpu(leaf, &found_key, slot);
6943
6944 if (found_key.objectid >= key->objectid &&
Chris Mason925baed2008-06-25 16:01:30 -04006945 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
6946 ret = 0;
6947 goto out;
6948 }
Chris Mason0b86a832008-03-24 15:01:56 -04006949 path->slots[0]++;
6950 }
6951 ret = -ENOENT;
Chris Mason925baed2008-06-25 16:01:30 -04006952out:
Chris Mason0b86a832008-03-24 15:01:56 -04006953 return ret;
6954}
6955
Zheng Yan1a40e232008-09-26 10:09:34 -04006956int btrfs_free_block_groups(struct btrfs_fs_info *info)
6957{
6958 struct btrfs_block_group_cache *block_group;
Chris Mason4184ea72009-03-10 12:39:20 -04006959 struct btrfs_space_info *space_info;
Zheng Yan1a40e232008-09-26 10:09:34 -04006960 struct rb_node *n;
6961
Zheng Yan1a40e232008-09-26 10:09:34 -04006962 spin_lock(&info->block_group_cache_lock);
6963 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
6964 block_group = rb_entry(n, struct btrfs_block_group_cache,
6965 cache_node);
Zheng Yan1a40e232008-09-26 10:09:34 -04006966 rb_erase(&block_group->cache_node,
6967 &info->block_group_cache_tree);
Yan Zhengd899e052008-10-30 14:25:28 -04006968 spin_unlock(&info->block_group_cache_lock);
6969
6970 btrfs_remove_free_space_cache(block_group);
Josef Bacik80eb2342008-10-29 14:49:05 -04006971 down_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04006972 list_del(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04006973 up_write(&block_group->space_info->groups_sem);
Yan Zhengd2fb3432008-12-11 16:30:39 -05006974
6975 WARN_ON(atomic_read(&block_group->count) != 1);
Zheng Yan1a40e232008-09-26 10:09:34 -04006976 kfree(block_group);
Yan Zhengd899e052008-10-30 14:25:28 -04006977
6978 spin_lock(&info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04006979 }
6980 spin_unlock(&info->block_group_cache_lock);
Chris Mason4184ea72009-03-10 12:39:20 -04006981
6982 /* now that all the block groups are freed, go through and
6983 * free all the space_info structs. This is only called during
6984 * the final stages of unmount, and so we know nobody is
6985 * using them. We call synchronize_rcu() once before we start,
6986 * just to be on the safe side.
6987 */
6988 synchronize_rcu();
6989
6990 while(!list_empty(&info->space_info)) {
6991 space_info = list_entry(info->space_info.next,
6992 struct btrfs_space_info,
6993 list);
6994
6995 list_del(&space_info->list);
6996 kfree(space_info);
6997 }
Zheng Yan1a40e232008-09-26 10:09:34 -04006998 return 0;
6999}
7000
Chris Mason9078a3e2007-04-26 16:46:15 -04007001int btrfs_read_block_groups(struct btrfs_root *root)
7002{
7003 struct btrfs_path *path;
7004 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04007005 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04007006 struct btrfs_fs_info *info = root->fs_info;
Chris Mason6324fbf2008-03-24 15:01:59 -04007007 struct btrfs_space_info *space_info;
Chris Mason9078a3e2007-04-26 16:46:15 -04007008 struct btrfs_key key;
7009 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -04007010 struct extent_buffer *leaf;
Chris Mason96b51792007-10-15 16:15:19 -04007011
Chris Masonbe744172007-05-06 10:15:01 -04007012 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04007013 key.objectid = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04007014 key.offset = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04007015 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
Chris Mason9078a3e2007-04-26 16:46:15 -04007016 path = btrfs_alloc_path();
7017 if (!path)
7018 return -ENOMEM;
7019
Chris Masond3977122009-01-05 21:25:51 -05007020 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04007021 ret = find_first_block_group(root, path, &key);
7022 if (ret > 0) {
7023 ret = 0;
7024 goto error;
Chris Mason9078a3e2007-04-26 16:46:15 -04007025 }
Chris Mason0b86a832008-03-24 15:01:56 -04007026 if (ret != 0)
7027 goto error;
7028
Chris Mason5f39d392007-10-15 16:14:19 -04007029 leaf = path->nodes[0];
7030 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason8f18cf12008-04-25 16:53:30 -04007031 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -04007032 if (!cache) {
Chris Mason0b86a832008-03-24 15:01:56 -04007033 ret = -ENOMEM;
Chris Mason9078a3e2007-04-26 16:46:15 -04007034 break;
7035 }
Chris Mason3e1ad542007-05-07 20:03:49 -04007036
Yan Zhengd2fb3432008-12-11 16:30:39 -05007037 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04007038 spin_lock_init(&cache->lock);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04007039 spin_lock_init(&cache->tree_lock);
Josef Bacikea6a4782008-11-20 12:16:16 -05007040 mutex_init(&cache->cache_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04007041 INIT_LIST_HEAD(&cache->list);
Chris Masonfa9c0d792009-04-03 09:47:43 -04007042 INIT_LIST_HEAD(&cache->cluster_list);
Chris Mason5f39d392007-10-15 16:14:19 -04007043 read_extent_buffer(leaf, &cache->item,
7044 btrfs_item_ptr_offset(leaf, path->slots[0]),
7045 sizeof(cache->item));
Chris Mason9078a3e2007-04-26 16:46:15 -04007046 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Mason0b86a832008-03-24 15:01:56 -04007047
Chris Mason9078a3e2007-04-26 16:46:15 -04007048 key.objectid = found_key.objectid + found_key.offset;
7049 btrfs_release_path(root, path);
Chris Mason0b86a832008-03-24 15:01:56 -04007050 cache->flags = btrfs_block_group_flags(&cache->item);
Chris Mason96b51792007-10-15 16:15:19 -04007051
Chris Mason6324fbf2008-03-24 15:01:59 -04007052 ret = update_space_info(info, cache->flags, found_key.offset,
7053 btrfs_block_group_used(&cache->item),
7054 &space_info);
7055 BUG_ON(ret);
7056 cache->space_info = space_info;
Josef Bacik80eb2342008-10-29 14:49:05 -04007057 down_write(&space_info->groups_sem);
7058 list_add_tail(&cache->list, &space_info->block_groups);
7059 up_write(&space_info->groups_sem);
Chris Mason6324fbf2008-03-24 15:01:59 -04007060
Josef Bacik0f9dd462008-09-23 13:14:11 -04007061 ret = btrfs_add_block_group_cache(root->fs_info, cache);
7062 BUG_ON(ret);
Chris Mason75ccf472008-09-30 19:24:06 -04007063
7064 set_avail_alloc_bits(root->fs_info, cache->flags);
Yan Zheng2b820322008-11-17 21:11:30 -05007065 if (btrfs_chunk_readonly(root, cache->key.objectid))
7066 set_block_group_readonly(cache);
Chris Mason9078a3e2007-04-26 16:46:15 -04007067 }
Chris Mason0b86a832008-03-24 15:01:56 -04007068 ret = 0;
7069error:
Chris Mason9078a3e2007-04-26 16:46:15 -04007070 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04007071 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04007072}
Chris Mason6324fbf2008-03-24 15:01:59 -04007073
7074int btrfs_make_block_group(struct btrfs_trans_handle *trans,
7075 struct btrfs_root *root, u64 bytes_used,
Chris Masone17cade2008-04-15 15:41:47 -04007076 u64 type, u64 chunk_objectid, u64 chunk_offset,
Chris Mason6324fbf2008-03-24 15:01:59 -04007077 u64 size)
7078{
7079 int ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04007080 struct btrfs_root *extent_root;
7081 struct btrfs_block_group_cache *cache;
Chris Mason6324fbf2008-03-24 15:01:59 -04007082
7083 extent_root = root->fs_info->extent_root;
Chris Mason6324fbf2008-03-24 15:01:59 -04007084
Chris Mason12fcfd22009-03-24 10:24:20 -04007085 root->fs_info->last_trans_log_full_commit = trans->transid;
Chris Masone02119d2008-09-05 16:13:11 -04007086
Chris Mason8f18cf12008-04-25 16:53:30 -04007087 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Josef Bacik0f9dd462008-09-23 13:14:11 -04007088 if (!cache)
7089 return -ENOMEM;
7090
Chris Masone17cade2008-04-15 15:41:47 -04007091 cache->key.objectid = chunk_offset;
Chris Mason6324fbf2008-03-24 15:01:59 -04007092 cache->key.offset = size;
Yan Zhengd2fb3432008-12-11 16:30:39 -05007093 cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
7094 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04007095 spin_lock_init(&cache->lock);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04007096 spin_lock_init(&cache->tree_lock);
Josef Bacikea6a4782008-11-20 12:16:16 -05007097 mutex_init(&cache->cache_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04007098 INIT_LIST_HEAD(&cache->list);
Chris Masonfa9c0d792009-04-03 09:47:43 -04007099 INIT_LIST_HEAD(&cache->cluster_list);
Chris Mason0ef3e662008-05-24 14:04:53 -04007100
Chris Mason6324fbf2008-03-24 15:01:59 -04007101 btrfs_set_block_group_used(&cache->item, bytes_used);
Chris Mason6324fbf2008-03-24 15:01:59 -04007102 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
7103 cache->flags = type;
7104 btrfs_set_block_group_flags(&cache->item, type);
7105
7106 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
7107 &cache->space_info);
7108 BUG_ON(ret);
Josef Bacik80eb2342008-10-29 14:49:05 -04007109 down_write(&cache->space_info->groups_sem);
7110 list_add_tail(&cache->list, &cache->space_info->block_groups);
7111 up_write(&cache->space_info->groups_sem);
Chris Mason6324fbf2008-03-24 15:01:59 -04007112
Josef Bacik0f9dd462008-09-23 13:14:11 -04007113 ret = btrfs_add_block_group_cache(root->fs_info, cache);
7114 BUG_ON(ret);
Chris Masonc286ac42008-07-22 23:06:41 -04007115
Chris Mason6324fbf2008-03-24 15:01:59 -04007116 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
7117 sizeof(cache->item));
7118 BUG_ON(ret);
7119
Chris Masond18a2c42008-04-04 15:40:00 -04007120 set_avail_alloc_bits(extent_root->fs_info, type);
Chris Mason925baed2008-06-25 16:01:30 -04007121
Chris Mason6324fbf2008-03-24 15:01:59 -04007122 return 0;
7123}
Zheng Yan1a40e232008-09-26 10:09:34 -04007124
7125int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
7126 struct btrfs_root *root, u64 group_start)
7127{
7128 struct btrfs_path *path;
7129 struct btrfs_block_group_cache *block_group;
Chris Mason44fb5512009-06-04 15:34:51 -04007130 struct btrfs_free_cluster *cluster;
Zheng Yan1a40e232008-09-26 10:09:34 -04007131 struct btrfs_key key;
7132 int ret;
7133
Zheng Yan1a40e232008-09-26 10:09:34 -04007134 root = root->fs_info->extent_root;
7135
7136 block_group = btrfs_lookup_block_group(root->fs_info, group_start);
7137 BUG_ON(!block_group);
Yan Zhengc146afa2008-11-12 14:34:12 -05007138 BUG_ON(!block_group->ro);
Zheng Yan1a40e232008-09-26 10:09:34 -04007139
7140 memcpy(&key, &block_group->key, sizeof(key));
7141
Chris Mason44fb5512009-06-04 15:34:51 -04007142 /* make sure this block group isn't part of an allocation cluster */
7143 cluster = &root->fs_info->data_alloc_cluster;
7144 spin_lock(&cluster->refill_lock);
7145 btrfs_return_cluster_to_free_space(block_group, cluster);
7146 spin_unlock(&cluster->refill_lock);
7147
7148 /*
7149 * make sure this block group isn't part of a metadata
7150 * allocation cluster
7151 */
7152 cluster = &root->fs_info->meta_alloc_cluster;
7153 spin_lock(&cluster->refill_lock);
7154 btrfs_return_cluster_to_free_space(block_group, cluster);
7155 spin_unlock(&cluster->refill_lock);
7156
Zheng Yan1a40e232008-09-26 10:09:34 -04007157 path = btrfs_alloc_path();
7158 BUG_ON(!path);
7159
Yan Zheng3dfdb932009-01-21 10:49:16 -05007160 spin_lock(&root->fs_info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04007161 rb_erase(&block_group->cache_node,
7162 &root->fs_info->block_group_cache_tree);
Yan Zheng3dfdb932009-01-21 10:49:16 -05007163 spin_unlock(&root->fs_info->block_group_cache_lock);
7164 btrfs_remove_free_space_cache(block_group);
Josef Bacik80eb2342008-10-29 14:49:05 -04007165 down_write(&block_group->space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04007166 /*
7167 * we must use list_del_init so people can check to see if they
7168 * are still on the list after taking the semaphore
7169 */
7170 list_del_init(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04007171 up_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04007172
Yan Zhengc146afa2008-11-12 14:34:12 -05007173 spin_lock(&block_group->space_info->lock);
7174 block_group->space_info->total_bytes -= block_group->key.offset;
7175 block_group->space_info->bytes_readonly -= block_group->key.offset;
7176 spin_unlock(&block_group->space_info->lock);
Yan Zheng2b820322008-11-17 21:11:30 -05007177 block_group->space_info->full = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05007178
Chris Masonfa9c0d792009-04-03 09:47:43 -04007179 btrfs_put_block_group(block_group);
7180 btrfs_put_block_group(block_group);
Zheng Yan1a40e232008-09-26 10:09:34 -04007181
7182 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
7183 if (ret > 0)
7184 ret = -EIO;
7185 if (ret < 0)
7186 goto out;
7187
7188 ret = btrfs_del_item(trans, root, path);
7189out:
7190 btrfs_free_path(path);
7191 return ret;
7192}