blob: a421c32c6cfefe36ce0a1892e5462e2b8228f5b8 [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"
Miguela5eb62e2008-04-11 15:45:51 -040026#include "crc32c.h"
Chris Masonfec577f2007-02-26 10:40:21 -050027#include "ctree.h"
28#include "disk-io.h"
29#include "print-tree.h"
Chris Masone089f052007-03-16 16:20:31 -040030#include "transaction.h"
Chris Mason0b86a832008-03-24 15:01:56 -040031#include "volumes.h"
Chris Mason925baed2008-06-25 16:01:30 -040032#include "locking.h"
Yan Zheng31153d82008-07-28 15:32:19 -040033#include "ref-cache.h"
Chris Masonfec577f2007-02-26 10:40:21 -050034
Zheng Yan31840ae2008-09-23 13:14:14 -040035#define PENDING_EXTENT_INSERT 0
36#define PENDING_EXTENT_DELETE 1
37#define PENDING_BACKREF_UPDATE 2
38
39struct pending_extent_op {
40 int type;
41 u64 bytenr;
42 u64 num_bytes;
43 u64 parent;
44 u64 orig_parent;
45 u64 generation;
46 u64 orig_generation;
47 int level;
Josef Bacikf3465ca2008-11-12 14:19:50 -050048 struct list_head list;
49 int del;
Zheng Yan31840ae2008-09-23 13:14:14 -040050};
51
Chris Mason56bec292009-03-13 10:10:06 -040052static int __btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
53 struct btrfs_root *root, u64 parent,
54 u64 root_objectid, u64 ref_generation,
55 u64 owner, struct btrfs_key *ins,
56 int ref_mod);
57static int update_reserved_extents(struct btrfs_root *root,
58 u64 bytenr, u64 num, int reserve);
Josef Bacikf3465ca2008-11-12 14:19:50 -050059static int pin_down_bytes(struct btrfs_trans_handle *trans,
60 struct btrfs_root *root,
61 u64 bytenr, u64 num_bytes, int is_data);
62static int update_block_group(struct btrfs_trans_handle *trans,
63 struct btrfs_root *root,
64 u64 bytenr, u64 num_bytes, int alloc,
65 int mark_free);
Chris Mason56bec292009-03-13 10:10:06 -040066static noinline int __btrfs_free_extent(struct btrfs_trans_handle *trans,
67 struct btrfs_root *root,
68 u64 bytenr, u64 num_bytes, u64 parent,
69 u64 root_objectid, u64 ref_generation,
70 u64 owner_objectid, int pin,
71 int ref_to_drop);
Yand548ee52008-01-03 13:56:30 -050072
Josef Bacik6a632092009-02-20 11:00:09 -050073static int do_chunk_alloc(struct btrfs_trans_handle *trans,
74 struct btrfs_root *extent_root, u64 alloc_bytes,
75 u64 flags, int force);
76
Josef Bacik0f9dd462008-09-23 13:14:11 -040077static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
78{
79 return (cache->flags & bits) == bits;
80}
81
82/*
83 * this adds the block group to the fs_info rb tree for the block group
84 * cache
85 */
Christoph Hellwigb2950862008-12-02 09:54:17 -050086static int btrfs_add_block_group_cache(struct btrfs_fs_info *info,
Josef Bacik0f9dd462008-09-23 13:14:11 -040087 struct btrfs_block_group_cache *block_group)
88{
89 struct rb_node **p;
90 struct rb_node *parent = NULL;
91 struct btrfs_block_group_cache *cache;
92
93 spin_lock(&info->block_group_cache_lock);
94 p = &info->block_group_cache_tree.rb_node;
95
96 while (*p) {
97 parent = *p;
98 cache = rb_entry(parent, struct btrfs_block_group_cache,
99 cache_node);
100 if (block_group->key.objectid < cache->key.objectid) {
101 p = &(*p)->rb_left;
102 } else if (block_group->key.objectid > cache->key.objectid) {
103 p = &(*p)->rb_right;
104 } else {
105 spin_unlock(&info->block_group_cache_lock);
106 return -EEXIST;
107 }
108 }
109
110 rb_link_node(&block_group->cache_node, parent, p);
111 rb_insert_color(&block_group->cache_node,
112 &info->block_group_cache_tree);
113 spin_unlock(&info->block_group_cache_lock);
114
115 return 0;
116}
117
118/*
119 * This will return the block group at or after bytenr if contains is 0, else
120 * it will return the block group that contains the bytenr
121 */
122static struct btrfs_block_group_cache *
123block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr,
124 int contains)
125{
126 struct btrfs_block_group_cache *cache, *ret = NULL;
127 struct rb_node *n;
128 u64 end, start;
129
130 spin_lock(&info->block_group_cache_lock);
131 n = info->block_group_cache_tree.rb_node;
132
133 while (n) {
134 cache = rb_entry(n, struct btrfs_block_group_cache,
135 cache_node);
136 end = cache->key.objectid + cache->key.offset - 1;
137 start = cache->key.objectid;
138
139 if (bytenr < start) {
140 if (!contains && (!ret || start < ret->key.objectid))
141 ret = cache;
142 n = n->rb_left;
143 } else if (bytenr > start) {
144 if (contains && bytenr <= end) {
145 ret = cache;
146 break;
147 }
148 n = n->rb_right;
149 } else {
150 ret = cache;
151 break;
152 }
153 }
Yan Zhengd2fb3432008-12-11 16:30:39 -0500154 if (ret)
155 atomic_inc(&ret->count);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400156 spin_unlock(&info->block_group_cache_lock);
157
158 return ret;
159}
160
161/*
162 * this is only called by cache_block_group, since we could have freed extents
163 * we need to check the pinned_extents for any extents that can't be used yet
164 * since their free space will be released as soon as the transaction commits.
165 */
166static int add_new_free_space(struct btrfs_block_group_cache *block_group,
167 struct btrfs_fs_info *info, u64 start, u64 end)
168{
169 u64 extent_start, extent_end, size;
170 int ret;
171
Josef Bacik25179202008-10-29 14:49:05 -0400172 mutex_lock(&info->pinned_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400173 while (start < end) {
174 ret = find_first_extent_bit(&info->pinned_extents, start,
175 &extent_start, &extent_end,
176 EXTENT_DIRTY);
177 if (ret)
178 break;
179
180 if (extent_start == start) {
181 start = extent_end + 1;
182 } else if (extent_start > start && extent_start < end) {
183 size = extent_start - start;
Josef Bacikea6a4782008-11-20 12:16:16 -0500184 ret = btrfs_add_free_space(block_group, start,
185 size);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400186 BUG_ON(ret);
187 start = extent_end + 1;
188 } else {
189 break;
190 }
191 }
192
193 if (start < end) {
194 size = end - start;
Josef Bacikea6a4782008-11-20 12:16:16 -0500195 ret = btrfs_add_free_space(block_group, start, size);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400196 BUG_ON(ret);
197 }
Josef Bacik25179202008-10-29 14:49:05 -0400198 mutex_unlock(&info->pinned_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400199
200 return 0;
201}
202
Yan Zhenga512bbf2008-12-08 16:46:26 -0500203static int remove_sb_from_cache(struct btrfs_root *root,
204 struct btrfs_block_group_cache *cache)
205{
206 u64 bytenr;
207 u64 *logical;
208 int stripe_len;
209 int i, nr, ret;
210
211 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
212 bytenr = btrfs_sb_offset(i);
213 ret = btrfs_rmap_block(&root->fs_info->mapping_tree,
214 cache->key.objectid, bytenr, 0,
215 &logical, &nr, &stripe_len);
216 BUG_ON(ret);
217 while (nr--) {
218 btrfs_remove_free_space(cache, logical[nr],
219 stripe_len);
220 }
221 kfree(logical);
222 }
223 return 0;
224}
225
Chris Masone37c9e62007-05-09 20:13:14 -0400226static int cache_block_group(struct btrfs_root *root,
227 struct btrfs_block_group_cache *block_group)
228{
229 struct btrfs_path *path;
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400230 int ret = 0;
Chris Masone37c9e62007-05-09 20:13:14 -0400231 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400232 struct extent_buffer *leaf;
Chris Masone37c9e62007-05-09 20:13:14 -0400233 int slot;
Yan Zhenge4404d62008-12-12 10:03:26 -0500234 u64 last;
Chris Masone37c9e62007-05-09 20:13:14 -0400235
Chris Mason00f5c792007-11-30 10:09:33 -0500236 if (!block_group)
237 return 0;
238
Chris Masone37c9e62007-05-09 20:13:14 -0400239 root = root->fs_info->extent_root;
Chris Masone37c9e62007-05-09 20:13:14 -0400240
241 if (block_group->cached)
242 return 0;
Chris Masonf510cfe2007-10-15 16:14:48 -0400243
Chris Masone37c9e62007-05-09 20:13:14 -0400244 path = btrfs_alloc_path();
245 if (!path)
246 return -ENOMEM;
Yan7d7d6062007-09-14 16:15:28 -0400247
Chris Mason2cc58cf2007-08-27 16:49:44 -0400248 path->reada = 2;
Chris Mason5cd57b22008-06-25 16:01:30 -0400249 /*
250 * we get into deadlocks with paths held by callers of this function.
251 * since the alloc_mutex is protecting things right now, just
252 * skip the locking here
253 */
254 path->skip_locking = 1;
Yan Zhenge4404d62008-12-12 10:03:26 -0500255 last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET);
256 key.objectid = last;
Chris Masone37c9e62007-05-09 20:13:14 -0400257 key.offset = 0;
258 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
259 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
260 if (ret < 0)
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400261 goto err;
Yan Zhenga512bbf2008-12-08 16:46:26 -0500262
Chris Masond3977122009-01-05 21:25:51 -0500263 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400264 leaf = path->nodes[0];
Chris Masone37c9e62007-05-09 20:13:14 -0400265 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -0400266 if (slot >= btrfs_header_nritems(leaf)) {
Chris Masone37c9e62007-05-09 20:13:14 -0400267 ret = btrfs_next_leaf(root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -0400268 if (ret < 0)
269 goto err;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400270 if (ret == 0)
Chris Masone37c9e62007-05-09 20:13:14 -0400271 continue;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400272 else
Chris Masone37c9e62007-05-09 20:13:14 -0400273 break;
Chris Masone37c9e62007-05-09 20:13:14 -0400274 }
Chris Mason5f39d392007-10-15 16:14:19 -0400275 btrfs_item_key_to_cpu(leaf, &key, slot);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400276 if (key.objectid < block_group->key.objectid)
Yan7d7d6062007-09-14 16:15:28 -0400277 goto next;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400278
Chris Masone37c9e62007-05-09 20:13:14 -0400279 if (key.objectid >= block_group->key.objectid +
Josef Bacik0f9dd462008-09-23 13:14:11 -0400280 block_group->key.offset)
Yan7d7d6062007-09-14 16:15:28 -0400281 break;
Yan7d7d6062007-09-14 16:15:28 -0400282
283 if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) {
Josef Bacik0f9dd462008-09-23 13:14:11 -0400284 add_new_free_space(block_group, root->fs_info, last,
285 key.objectid);
286
Yan7d7d6062007-09-14 16:15:28 -0400287 last = key.objectid + key.offset;
Chris Masone37c9e62007-05-09 20:13:14 -0400288 }
Yan7d7d6062007-09-14 16:15:28 -0400289next:
Chris Masone37c9e62007-05-09 20:13:14 -0400290 path->slots[0]++;
291 }
292
Josef Bacik0f9dd462008-09-23 13:14:11 -0400293 add_new_free_space(block_group, root->fs_info, last,
294 block_group->key.objectid +
295 block_group->key.offset);
296
Yan Zhenga512bbf2008-12-08 16:46:26 -0500297 remove_sb_from_cache(root, block_group);
Chris Masone37c9e62007-05-09 20:13:14 -0400298 block_group->cached = 1;
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400299 ret = 0;
Chris Mason54aa1f42007-06-22 14:16:25 -0400300err:
Chris Masone37c9e62007-05-09 20:13:14 -0400301 btrfs_free_path(path);
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400302 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -0400303}
304
Josef Bacik0f9dd462008-09-23 13:14:11 -0400305/*
306 * return the block group that starts at or after bytenr
307 */
Chris Masond3977122009-01-05 21:25:51 -0500308static struct btrfs_block_group_cache *
309btrfs_lookup_first_block_group(struct btrfs_fs_info *info, u64 bytenr)
Chris Mason0ef3e662008-05-24 14:04:53 -0400310{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400311 struct btrfs_block_group_cache *cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400312
Josef Bacik0f9dd462008-09-23 13:14:11 -0400313 cache = block_group_cache_tree_search(info, bytenr, 0);
Chris Mason0ef3e662008-05-24 14:04:53 -0400314
Josef Bacik0f9dd462008-09-23 13:14:11 -0400315 return cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400316}
317
Josef Bacik0f9dd462008-09-23 13:14:11 -0400318/*
319 * return the block group that contains teh given bytenr
320 */
Chris Masond3977122009-01-05 21:25:51 -0500321struct btrfs_block_group_cache *btrfs_lookup_block_group(
322 struct btrfs_fs_info *info,
323 u64 bytenr)
Chris Masonbe744172007-05-06 10:15:01 -0400324{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400325 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -0400326
Josef Bacik0f9dd462008-09-23 13:14:11 -0400327 cache = block_group_cache_tree_search(info, bytenr, 1);
Chris Mason96b51792007-10-15 16:15:19 -0400328
Josef Bacik0f9dd462008-09-23 13:14:11 -0400329 return cache;
Chris Masonbe744172007-05-06 10:15:01 -0400330}
Chris Mason0b86a832008-03-24 15:01:56 -0400331
Yan Zhengd2fb3432008-12-11 16:30:39 -0500332static inline void put_block_group(struct btrfs_block_group_cache *cache)
333{
334 if (atomic_dec_and_test(&cache->count))
335 kfree(cache);
336}
337
Josef Bacik0f9dd462008-09-23 13:14:11 -0400338static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
339 u64 flags)
Chris Mason6324fbf2008-03-24 15:01:59 -0400340{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400341 struct list_head *head = &info->space_info;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400342 struct btrfs_space_info *found;
Chris Mason4184ea72009-03-10 12:39:20 -0400343
344 rcu_read_lock();
345 list_for_each_entry_rcu(found, head, list) {
346 if (found->flags == flags) {
347 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -0400348 return found;
Chris Mason4184ea72009-03-10 12:39:20 -0400349 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400350 }
Chris Mason4184ea72009-03-10 12:39:20 -0400351 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -0400352 return NULL;
Chris Mason6324fbf2008-03-24 15:01:59 -0400353}
354
Chris Mason4184ea72009-03-10 12:39:20 -0400355/*
356 * after adding space to the filesystem, we need to clear the full flags
357 * on all the space infos.
358 */
359void btrfs_clear_space_info_full(struct btrfs_fs_info *info)
360{
361 struct list_head *head = &info->space_info;
362 struct btrfs_space_info *found;
363
364 rcu_read_lock();
365 list_for_each_entry_rcu(found, head, list)
366 found->full = 0;
367 rcu_read_unlock();
368}
369
Josef Bacik80eb2342008-10-29 14:49:05 -0400370static u64 div_factor(u64 num, int factor)
371{
372 if (factor == 10)
373 return num;
374 num *= factor;
375 do_div(num, 10);
376 return num;
377}
378
Yan Zhengd2fb3432008-12-11 16:30:39 -0500379u64 btrfs_find_block_group(struct btrfs_root *root,
380 u64 search_start, u64 search_hint, int owner)
Chris Masoncd1bc462007-04-27 10:08:34 -0400381{
Chris Mason96b51792007-10-15 16:15:19 -0400382 struct btrfs_block_group_cache *cache;
Chris Masoncd1bc462007-04-27 10:08:34 -0400383 u64 used;
Yan Zhengd2fb3432008-12-11 16:30:39 -0500384 u64 last = max(search_hint, search_start);
385 u64 group_start = 0;
Chris Mason31f3c992007-04-30 15:25:45 -0400386 int full_search = 0;
Yan Zhengd2fb3432008-12-11 16:30:39 -0500387 int factor = 9;
Chris Mason0ef3e662008-05-24 14:04:53 -0400388 int wrapped = 0;
Chris Mason31f3c992007-04-30 15:25:45 -0400389again:
Zheng Yane8569812008-09-26 10:05:48 -0400390 while (1) {
391 cache = btrfs_lookup_first_block_group(root->fs_info, last);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400392 if (!cache)
Chris Masoncd1bc462007-04-27 10:08:34 -0400393 break;
Chris Mason96b51792007-10-15 16:15:19 -0400394
Chris Masonc286ac42008-07-22 23:06:41 -0400395 spin_lock(&cache->lock);
Chris Mason96b51792007-10-15 16:15:19 -0400396 last = cache->key.objectid + cache->key.offset;
397 used = btrfs_block_group_used(&cache->item);
398
Yan Zhengd2fb3432008-12-11 16:30:39 -0500399 if ((full_search || !cache->ro) &&
400 block_group_bits(cache, BTRFS_BLOCK_GROUP_METADATA)) {
Zheng Yane8569812008-09-26 10:05:48 -0400401 if (used + cache->pinned + cache->reserved <
Yan Zhengd2fb3432008-12-11 16:30:39 -0500402 div_factor(cache->key.offset, factor)) {
403 group_start = cache->key.objectid;
Chris Masonc286ac42008-07-22 23:06:41 -0400404 spin_unlock(&cache->lock);
Yan Zhengd2fb3432008-12-11 16:30:39 -0500405 put_block_group(cache);
Chris Mason8790d502008-04-03 16:29:03 -0400406 goto found;
407 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400408 }
Chris Masonc286ac42008-07-22 23:06:41 -0400409 spin_unlock(&cache->lock);
Yan Zhengd2fb3432008-12-11 16:30:39 -0500410 put_block_group(cache);
Chris Masonde428b62007-05-18 13:28:27 -0400411 cond_resched();
Chris Masoncd1bc462007-04-27 10:08:34 -0400412 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400413 if (!wrapped) {
414 last = search_start;
415 wrapped = 1;
416 goto again;
417 }
418 if (!full_search && factor < 10) {
Chris Masonbe744172007-05-06 10:15:01 -0400419 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -0400420 full_search = 1;
Chris Mason0ef3e662008-05-24 14:04:53 -0400421 factor = 10;
Chris Mason31f3c992007-04-30 15:25:45 -0400422 goto again;
423 }
Chris Masonbe744172007-05-06 10:15:01 -0400424found:
Yan Zhengd2fb3432008-12-11 16:30:39 -0500425 return group_start;
Chris Mason925baed2008-06-25 16:01:30 -0400426}
Josef Bacik0f9dd462008-09-23 13:14:11 -0400427
Chris Masone02119d2008-09-05 16:13:11 -0400428/* simple helper to search for an existing extent at a given offset */
Zheng Yan31840ae2008-09-23 13:14:14 -0400429int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len)
Chris Masone02119d2008-09-05 16:13:11 -0400430{
431 int ret;
432 struct btrfs_key key;
Zheng Yan31840ae2008-09-23 13:14:14 -0400433 struct btrfs_path *path;
Chris Masone02119d2008-09-05 16:13:11 -0400434
Zheng Yan31840ae2008-09-23 13:14:14 -0400435 path = btrfs_alloc_path();
436 BUG_ON(!path);
Chris Masone02119d2008-09-05 16:13:11 -0400437 key.objectid = start;
438 key.offset = len;
439 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
440 ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
441 0, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400442 btrfs_free_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -0500443 return ret;
444}
445
Chris Masond8d5f3e2007-12-11 12:42:00 -0500446/*
447 * Back reference rules. Back refs have three main goals:
448 *
449 * 1) differentiate between all holders of references to an extent so that
450 * when a reference is dropped we can make sure it was a valid reference
451 * before freeing the extent.
452 *
453 * 2) Provide enough information to quickly find the holders of an extent
454 * if we notice a given block is corrupted or bad.
455 *
456 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
457 * maintenance. This is actually the same as #2, but with a slightly
458 * different use case.
459 *
460 * File extents can be referenced by:
461 *
462 * - multiple snapshots, subvolumes, or different generations in one subvol
Zheng Yan31840ae2008-09-23 13:14:14 -0400463 * - different files inside a single subvolume
Chris Masond8d5f3e2007-12-11 12:42:00 -0500464 * - different offsets inside a file (bookend extents in file.c)
465 *
466 * The extent ref structure has fields for:
467 *
468 * - Objectid of the subvolume root
469 * - Generation number of the tree holding the reference
470 * - objectid of the file holding the reference
Zheng Yan31840ae2008-09-23 13:14:14 -0400471 * - number of references holding by parent node (alway 1 for tree blocks)
472 *
473 * Btree leaf may hold multiple references to a file extent. In most cases,
474 * these references are from same file and the corresponding offsets inside
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400475 * the file are close together.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500476 *
477 * When a file extent is allocated the fields are filled in:
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400478 * (root_key.objectid, trans->transid, inode objectid, 1)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500479 *
480 * When a leaf is cow'd new references are added for every file extent found
Zheng Yan31840ae2008-09-23 13:14:14 -0400481 * in the leaf. It looks similar to the create case, but trans->transid will
482 * be different when the block is cow'd.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500483 *
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400484 * (root_key.objectid, trans->transid, inode objectid,
Zheng Yan31840ae2008-09-23 13:14:14 -0400485 * number of references in the leaf)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500486 *
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400487 * When a file extent is removed either during snapshot deletion or
488 * file truncation, we find the corresponding back reference and check
489 * the following fields:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500490 *
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400491 * (btrfs_header_owner(leaf), btrfs_header_generation(leaf),
492 * inode objectid)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500493 *
494 * Btree extents can be referenced by:
495 *
496 * - Different subvolumes
497 * - Different generations of the same subvolume
498 *
Chris Masond8d5f3e2007-12-11 12:42:00 -0500499 * When a tree block is created, back references are inserted:
500 *
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400501 * (root->root_key.objectid, trans->transid, level, 1)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500502 *
Zheng Yan31840ae2008-09-23 13:14:14 -0400503 * When a tree block is cow'd, new back references are added for all the
504 * blocks it points to. If the tree block isn't in reference counted root,
505 * the old back references are removed. These new back references are of
506 * the form (trans->transid will have increased since creation):
Chris Masond8d5f3e2007-12-11 12:42:00 -0500507 *
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400508 * (root->root_key.objectid, trans->transid, level, 1)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500509 *
Zheng Yan31840ae2008-09-23 13:14:14 -0400510 * When a backref is in deleting, the following fields are checked:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500511 *
512 * if backref was for a tree root:
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400513 * (btrfs_header_owner(itself), btrfs_header_generation(itself), level)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500514 * else
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400515 * (btrfs_header_owner(parent), btrfs_header_generation(parent), level)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500516 *
Zheng Yan31840ae2008-09-23 13:14:14 -0400517 * Back Reference Key composing:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500518 *
Zheng Yan31840ae2008-09-23 13:14:14 -0400519 * The key objectid corresponds to the first byte in the extent, the key
520 * type is set to BTRFS_EXTENT_REF_KEY, and the key offset is the first
521 * byte of parent extent. If a extent is tree root, the key offset is set
522 * to the key objectid.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500523 */
Zheng Yan31840ae2008-09-23 13:14:14 -0400524
Chris Masond3977122009-01-05 21:25:51 -0500525static noinline int lookup_extent_backref(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -0400526 struct btrfs_root *root,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400527 struct btrfs_path *path,
528 u64 bytenr, u64 parent,
529 u64 ref_root, u64 ref_generation,
530 u64 owner_objectid, int del)
Chris Mason74493f72007-12-11 09:25:06 -0500531{
Chris Mason74493f72007-12-11 09:25:06 -0500532 struct btrfs_key key;
Zheng Yan31840ae2008-09-23 13:14:14 -0400533 struct btrfs_extent_ref *ref;
534 struct extent_buffer *leaf;
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400535 u64 ref_objectid;
Chris Mason74493f72007-12-11 09:25:06 -0500536 int ret;
537
Chris Mason74493f72007-12-11 09:25:06 -0500538 key.objectid = bytenr;
539 key.type = BTRFS_EXTENT_REF_KEY;
Zheng Yan31840ae2008-09-23 13:14:14 -0400540 key.offset = parent;
Chris Mason74493f72007-12-11 09:25:06 -0500541
Zheng Yan31840ae2008-09-23 13:14:14 -0400542 ret = btrfs_search_slot(trans, root, &key, path, del ? -1 : 0, 1);
543 if (ret < 0)
Chris Mason7bb86312007-12-11 09:25:06 -0500544 goto out;
Zheng Yan31840ae2008-09-23 13:14:14 -0400545 if (ret > 0) {
546 ret = -ENOENT;
547 goto out;
548 }
549
550 leaf = path->nodes[0];
551 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400552 ref_objectid = btrfs_ref_objectid(leaf, ref);
Zheng Yan31840ae2008-09-23 13:14:14 -0400553 if (btrfs_ref_root(leaf, ref) != ref_root ||
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400554 btrfs_ref_generation(leaf, ref) != ref_generation ||
555 (ref_objectid != owner_objectid &&
556 ref_objectid != BTRFS_MULTIPLE_OBJECTIDS)) {
Zheng Yan31840ae2008-09-23 13:14:14 -0400557 ret = -EIO;
558 WARN_ON(1);
559 goto out;
560 }
561 ret = 0;
562out:
563 return ret;
564}
565
Chris Masond3977122009-01-05 21:25:51 -0500566static noinline int insert_extent_backref(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -0400567 struct btrfs_root *root,
568 struct btrfs_path *path,
569 u64 bytenr, u64 parent,
570 u64 ref_root, u64 ref_generation,
Chris Mason56bec292009-03-13 10:10:06 -0400571 u64 owner_objectid,
572 int refs_to_add)
Zheng Yan31840ae2008-09-23 13:14:14 -0400573{
574 struct btrfs_key key;
575 struct extent_buffer *leaf;
576 struct btrfs_extent_ref *ref;
577 u32 num_refs;
578 int ret;
579
580 key.objectid = bytenr;
581 key.type = BTRFS_EXTENT_REF_KEY;
582 key.offset = parent;
583
584 ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(*ref));
585 if (ret == 0) {
586 leaf = path->nodes[0];
587 ref = btrfs_item_ptr(leaf, path->slots[0],
588 struct btrfs_extent_ref);
589 btrfs_set_ref_root(leaf, ref, ref_root);
590 btrfs_set_ref_generation(leaf, ref, ref_generation);
591 btrfs_set_ref_objectid(leaf, ref, owner_objectid);
Chris Mason56bec292009-03-13 10:10:06 -0400592 btrfs_set_ref_num_refs(leaf, ref, refs_to_add);
Zheng Yan31840ae2008-09-23 13:14:14 -0400593 } else if (ret == -EEXIST) {
594 u64 existing_owner;
Chris Mason56bec292009-03-13 10:10:06 -0400595
Zheng Yan31840ae2008-09-23 13:14:14 -0400596 BUG_ON(owner_objectid < BTRFS_FIRST_FREE_OBJECTID);
597 leaf = path->nodes[0];
598 ref = btrfs_item_ptr(leaf, path->slots[0],
599 struct btrfs_extent_ref);
600 if (btrfs_ref_root(leaf, ref) != ref_root ||
601 btrfs_ref_generation(leaf, ref) != ref_generation) {
602 ret = -EIO;
603 WARN_ON(1);
604 goto out;
605 }
606
607 num_refs = btrfs_ref_num_refs(leaf, ref);
608 BUG_ON(num_refs == 0);
Chris Mason56bec292009-03-13 10:10:06 -0400609 btrfs_set_ref_num_refs(leaf, ref, num_refs + refs_to_add);
Zheng Yan31840ae2008-09-23 13:14:14 -0400610
611 existing_owner = btrfs_ref_objectid(leaf, ref);
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400612 if (existing_owner != owner_objectid &&
613 existing_owner != BTRFS_MULTIPLE_OBJECTIDS) {
Zheng Yan31840ae2008-09-23 13:14:14 -0400614 btrfs_set_ref_objectid(leaf, ref,
615 BTRFS_MULTIPLE_OBJECTIDS);
Zheng Yan31840ae2008-09-23 13:14:14 -0400616 }
617 ret = 0;
618 } else {
619 goto out;
620 }
Chris Mason7bb86312007-12-11 09:25:06 -0500621 btrfs_mark_buffer_dirty(path->nodes[0]);
622out:
623 btrfs_release_path(root, path);
624 return ret;
Chris Mason74493f72007-12-11 09:25:06 -0500625}
626
Chris Masond3977122009-01-05 21:25:51 -0500627static noinline int remove_extent_backref(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -0400628 struct btrfs_root *root,
Chris Mason56bec292009-03-13 10:10:06 -0400629 struct btrfs_path *path,
630 int refs_to_drop)
Zheng Yan31840ae2008-09-23 13:14:14 -0400631{
632 struct extent_buffer *leaf;
633 struct btrfs_extent_ref *ref;
634 u32 num_refs;
635 int ret = 0;
636
637 leaf = path->nodes[0];
638 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
639 num_refs = btrfs_ref_num_refs(leaf, ref);
Chris Mason56bec292009-03-13 10:10:06 -0400640 BUG_ON(num_refs < refs_to_drop);
641 num_refs -= refs_to_drop;
Zheng Yan31840ae2008-09-23 13:14:14 -0400642 if (num_refs == 0) {
643 ret = btrfs_del_item(trans, root, path);
644 } else {
645 btrfs_set_ref_num_refs(leaf, ref, num_refs);
646 btrfs_mark_buffer_dirty(leaf);
647 }
648 btrfs_release_path(root, path);
649 return ret;
650}
651
Chris Mason4b4e25f2008-11-20 10:22:27 -0500652#ifdef BIO_RW_DISCARD
Chris Mason15916de2008-11-19 21:17:22 -0500653static void btrfs_issue_discard(struct block_device *bdev,
654 u64 start, u64 len)
655{
Chris Mason15916de2008-11-19 21:17:22 -0500656 blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_KERNEL);
Chris Mason15916de2008-11-19 21:17:22 -0500657}
Chris Mason4b4e25f2008-11-20 10:22:27 -0500658#endif
Chris Mason15916de2008-11-19 21:17:22 -0500659
Liu Hui1f3c79a2009-01-05 15:57:51 -0500660static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr,
661 u64 num_bytes)
662{
663#ifdef BIO_RW_DISCARD
664 int ret;
665 u64 map_length = num_bytes;
666 struct btrfs_multi_bio *multi = NULL;
667
668 /* Tell the block device(s) that the sectors can be discarded */
669 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
670 bytenr, &map_length, &multi, 0);
671 if (!ret) {
672 struct btrfs_bio_stripe *stripe = multi->stripes;
673 int i;
674
675 if (map_length > num_bytes)
676 map_length = num_bytes;
677
678 for (i = 0; i < multi->num_stripes; i++, stripe++) {
679 btrfs_issue_discard(stripe->dev->bdev,
680 stripe->physical,
681 map_length);
682 }
683 kfree(multi);
684 }
685
686 return ret;
687#else
688 return 0;
689#endif
690}
691
Zheng Yan31840ae2008-09-23 13:14:14 -0400692static int __btrfs_update_extent_ref(struct btrfs_trans_handle *trans,
693 struct btrfs_root *root, u64 bytenr,
Chris Mason56bec292009-03-13 10:10:06 -0400694 u64 num_bytes,
Zheng Yan31840ae2008-09-23 13:14:14 -0400695 u64 orig_parent, u64 parent,
696 u64 orig_root, u64 ref_root,
697 u64 orig_generation, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400698 u64 owner_objectid)
Zheng Yan31840ae2008-09-23 13:14:14 -0400699{
700 int ret;
Chris Mason56bec292009-03-13 10:10:06 -0400701 int pin = owner_objectid < BTRFS_FIRST_FREE_OBJECTID;
Zheng Yan31840ae2008-09-23 13:14:14 -0400702
Chris Mason56bec292009-03-13 10:10:06 -0400703 ret = btrfs_update_delayed_ref(trans, bytenr, num_bytes,
704 orig_parent, parent, orig_root,
705 ref_root, orig_generation,
706 ref_generation, owner_objectid, pin);
Zheng Yan31840ae2008-09-23 13:14:14 -0400707 BUG_ON(ret);
Zheng Yan31840ae2008-09-23 13:14:14 -0400708 return ret;
709}
710
711int btrfs_update_extent_ref(struct btrfs_trans_handle *trans,
712 struct btrfs_root *root, u64 bytenr,
Chris Mason56bec292009-03-13 10:10:06 -0400713 u64 num_bytes, u64 orig_parent, u64 parent,
Zheng Yan31840ae2008-09-23 13:14:14 -0400714 u64 ref_root, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400715 u64 owner_objectid)
Zheng Yan31840ae2008-09-23 13:14:14 -0400716{
717 int ret;
718 if (ref_root == BTRFS_TREE_LOG_OBJECTID &&
719 owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
720 return 0;
Chris Mason56bec292009-03-13 10:10:06 -0400721
722 ret = __btrfs_update_extent_ref(trans, root, bytenr, num_bytes,
723 orig_parent, parent, ref_root,
724 ref_root, ref_generation,
725 ref_generation, owner_objectid);
Zheng Yan31840ae2008-09-23 13:14:14 -0400726 return ret;
727}
Chris Mason925baed2008-06-25 16:01:30 -0400728static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -0400729 struct btrfs_root *root, u64 bytenr,
Chris Mason56bec292009-03-13 10:10:06 -0400730 u64 num_bytes,
Zheng Yan31840ae2008-09-23 13:14:14 -0400731 u64 orig_parent, u64 parent,
732 u64 orig_root, u64 ref_root,
733 u64 orig_generation, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400734 u64 owner_objectid)
Chris Mason02217ed2007-03-02 16:08:05 -0500735{
Chris Mason56bec292009-03-13 10:10:06 -0400736 int ret;
737
738 ret = btrfs_add_delayed_ref(trans, bytenr, num_bytes, parent, ref_root,
739 ref_generation, owner_objectid,
740 BTRFS_ADD_DELAYED_REF, 0);
741 BUG_ON(ret);
742 return ret;
743}
744
745static noinline_for_stack int add_extent_ref(struct btrfs_trans_handle *trans,
746 struct btrfs_root *root, u64 bytenr,
747 u64 num_bytes, u64 parent, u64 ref_root,
748 u64 ref_generation, u64 owner_objectid,
749 int refs_to_add)
750{
Chris Mason5caf2a02007-04-02 11:20:42 -0400751 struct btrfs_path *path;
Chris Mason02217ed2007-03-02 16:08:05 -0500752 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400753 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400754 struct extent_buffer *l;
Chris Mason234b63a2007-03-13 10:46:10 -0400755 struct btrfs_extent_item *item;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400756 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500757
Chris Mason5caf2a02007-04-02 11:20:42 -0400758 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -0400759 if (!path)
760 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -0400761
Chris Mason3c12ac72008-04-21 12:01:38 -0400762 path->reada = 1;
Chris Masondb945352007-10-15 16:15:53 -0400763 key.objectid = bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -0400764 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason56bec292009-03-13 10:10:06 -0400765 key.offset = num_bytes;
Zheng Yan31840ae2008-09-23 13:14:14 -0400766
Chris Mason56bec292009-03-13 10:10:06 -0400767 /* first find the extent item and update its reference count */
768 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key,
769 path, 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400770 if (ret < 0)
771 return ret;
Zheng Yan31840ae2008-09-23 13:14:14 -0400772
Chris Mason56bec292009-03-13 10:10:06 -0400773 if (ret > 0) {
774 WARN_ON(1);
775 btrfs_free_path(path);
776 return -EIO;
777 }
Chris Mason5f39d392007-10-15 16:14:19 -0400778 l = path->nodes[0];
Zheng Yan31840ae2008-09-23 13:14:14 -0400779
780 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
Chris Mason771ed682008-11-06 22:02:51 -0500781 if (key.objectid != bytenr) {
782 btrfs_print_leaf(root->fs_info->extent_root, path->nodes[0]);
Chris Masond3977122009-01-05 21:25:51 -0500783 printk(KERN_ERR "btrfs wanted %llu found %llu\n",
784 (unsigned long long)bytenr,
785 (unsigned long long)key.objectid);
Chris Mason771ed682008-11-06 22:02:51 -0500786 BUG();
787 }
Zheng Yan31840ae2008-09-23 13:14:14 -0400788 BUG_ON(key.type != BTRFS_EXTENT_ITEM_KEY);
789
Chris Mason5caf2a02007-04-02 11:20:42 -0400790 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Mason56bec292009-03-13 10:10:06 -0400791
Chris Mason5f39d392007-10-15 16:14:19 -0400792 refs = btrfs_extent_refs(l, item);
Chris Mason56bec292009-03-13 10:10:06 -0400793 btrfs_set_extent_refs(l, item, refs + refs_to_add);
Chris Mason5caf2a02007-04-02 11:20:42 -0400794 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masona28ec192007-03-06 20:08:01 -0500795
Chris Mason5caf2a02007-04-02 11:20:42 -0400796 btrfs_release_path(root->fs_info->extent_root, path);
Chris Mason7bb86312007-12-11 09:25:06 -0500797
Chris Mason3c12ac72008-04-21 12:01:38 -0400798 path->reada = 1;
Chris Mason56bec292009-03-13 10:10:06 -0400799 /* now insert the actual backref */
Zheng Yan31840ae2008-09-23 13:14:14 -0400800 ret = insert_extent_backref(trans, root->fs_info->extent_root,
801 path, bytenr, parent,
802 ref_root, ref_generation,
Chris Mason56bec292009-03-13 10:10:06 -0400803 owner_objectid, refs_to_add);
Chris Mason7bb86312007-12-11 09:25:06 -0500804 BUG_ON(ret);
Chris Mason74493f72007-12-11 09:25:06 -0500805 btrfs_free_path(path);
Chris Mason02217ed2007-03-02 16:08:05 -0500806 return 0;
807}
808
Chris Mason925baed2008-06-25 16:01:30 -0400809int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -0400810 struct btrfs_root *root,
811 u64 bytenr, u64 num_bytes, u64 parent,
812 u64 ref_root, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400813 u64 owner_objectid)
Chris Mason925baed2008-06-25 16:01:30 -0400814{
815 int ret;
Zheng Yan31840ae2008-09-23 13:14:14 -0400816 if (ref_root == BTRFS_TREE_LOG_OBJECTID &&
817 owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
818 return 0;
Chris Mason56bec292009-03-13 10:10:06 -0400819
820 ret = __btrfs_inc_extent_ref(trans, root, bytenr, num_bytes, 0, parent,
Zheng Yan31840ae2008-09-23 13:14:14 -0400821 0, ref_root, 0, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400822 owner_objectid);
Chris Mason925baed2008-06-25 16:01:30 -0400823 return ret;
824}
825
Chris Mason56bec292009-03-13 10:10:06 -0400826static int drop_delayed_ref(struct btrfs_trans_handle *trans,
827 struct btrfs_root *root,
828 struct btrfs_delayed_ref_node *node)
Chris Masone9d0b132007-08-10 14:06:19 -0400829{
Chris Mason56bec292009-03-13 10:10:06 -0400830 int ret = 0;
831 struct btrfs_delayed_ref *ref = btrfs_delayed_node_to_ref(node);
832
833 BUG_ON(node->ref_mod == 0);
834 ret = __btrfs_free_extent(trans, root, node->bytenr, node->num_bytes,
835 node->parent, ref->root, ref->generation,
836 ref->owner_objectid, ref->pin, node->ref_mod);
837
838 return ret;
839}
840
841/* helper function to actually process a single delayed ref entry */
842static noinline int run_one_delayed_ref(struct btrfs_trans_handle *trans,
843 struct btrfs_root *root,
844 struct btrfs_delayed_ref_node *node,
845 int insert_reserved)
846{
Josef Bacikeb099672009-02-12 09:27:38 -0500847 int ret;
Chris Mason56bec292009-03-13 10:10:06 -0400848 struct btrfs_delayed_ref *ref;
Josef Bacikeb099672009-02-12 09:27:38 -0500849
Chris Mason56bec292009-03-13 10:10:06 -0400850 if (node->parent == (u64)-1) {
851 struct btrfs_delayed_ref_head *head;
852 /*
853 * we've hit the end of the chain and we were supposed
854 * to insert this extent into the tree. But, it got
855 * deleted before we ever needed to insert it, so all
856 * we have to do is clean up the accounting
857 */
858 if (insert_reserved) {
859 update_reserved_extents(root, node->bytenr,
860 node->num_bytes, 0);
861 }
862 head = btrfs_delayed_node_to_head(node);
863 mutex_unlock(&head->mutex);
864 return 0;
865 }
Josef Bacikeb099672009-02-12 09:27:38 -0500866
Chris Mason56bec292009-03-13 10:10:06 -0400867 ref = btrfs_delayed_node_to_ref(node);
868 if (ref->action == BTRFS_ADD_DELAYED_REF) {
869 if (insert_reserved) {
870 struct btrfs_key ins;
871
872 ins.objectid = node->bytenr;
873 ins.offset = node->num_bytes;
874 ins.type = BTRFS_EXTENT_ITEM_KEY;
875
876 /* record the full extent allocation */
877 ret = __btrfs_alloc_reserved_extent(trans, root,
878 node->parent, ref->root,
879 ref->generation, ref->owner_objectid,
880 &ins, node->ref_mod);
881 update_reserved_extents(root, node->bytenr,
882 node->num_bytes, 0);
883 } else {
884 /* just add one backref */
885 ret = add_extent_ref(trans, root, node->bytenr,
886 node->num_bytes,
887 node->parent, ref->root, ref->generation,
888 ref->owner_objectid, node->ref_mod);
889 }
890 BUG_ON(ret);
891 } else if (ref->action == BTRFS_DROP_DELAYED_REF) {
892 WARN_ON(insert_reserved);
893 ret = drop_delayed_ref(trans, root, node);
Josef Bacikeb099672009-02-12 09:27:38 -0500894 }
Chris Masone9d0b132007-08-10 14:06:19 -0400895 return 0;
896}
897
Chris Mason56bec292009-03-13 10:10:06 -0400898static noinline struct btrfs_delayed_ref_node *
899select_delayed_ref(struct btrfs_delayed_ref_head *head)
Chris Masona28ec192007-03-06 20:08:01 -0500900{
Chris Mason56bec292009-03-13 10:10:06 -0400901 struct rb_node *node;
902 struct btrfs_delayed_ref_node *ref;
903 int action = BTRFS_ADD_DELAYED_REF;
904again:
905 /*
906 * select delayed ref of type BTRFS_ADD_DELAYED_REF first.
907 * this prevents ref count from going down to zero when
908 * there still are pending delayed ref.
909 */
910 node = rb_prev(&head->node.rb_node);
911 while (1) {
912 if (!node)
913 break;
914 ref = rb_entry(node, struct btrfs_delayed_ref_node,
915 rb_node);
916 if (ref->bytenr != head->node.bytenr)
917 break;
918 if (btrfs_delayed_node_to_ref(ref)->action == action)
919 return ref;
920 node = rb_prev(node);
Chris Mason5f39d392007-10-15 16:14:19 -0400921 }
Chris Mason56bec292009-03-13 10:10:06 -0400922 if (action == BTRFS_ADD_DELAYED_REF) {
923 action = BTRFS_DROP_DELAYED_REF;
924 goto again;
925 }
926 return NULL;
927}
928
Chris Masonc3e69d52009-03-13 10:17:05 -0400929static noinline int run_clustered_refs(struct btrfs_trans_handle *trans,
930 struct btrfs_root *root,
931 struct list_head *cluster)
Chris Mason56bec292009-03-13 10:10:06 -0400932{
Chris Mason56bec292009-03-13 10:10:06 -0400933 struct btrfs_delayed_ref_root *delayed_refs;
934 struct btrfs_delayed_ref_node *ref;
935 struct btrfs_delayed_ref_head *locked_ref = NULL;
936 int ret;
Chris Masonc3e69d52009-03-13 10:17:05 -0400937 int count = 0;
Chris Mason56bec292009-03-13 10:10:06 -0400938 int must_insert_reserved = 0;
Chris Mason56bec292009-03-13 10:10:06 -0400939
940 delayed_refs = &trans->transaction->delayed_refs;
Chris Mason56bec292009-03-13 10:10:06 -0400941 while (1) {
942 if (!locked_ref) {
Chris Masonc3e69d52009-03-13 10:17:05 -0400943 /* pick a new head ref from the cluster list */
944 if (list_empty(cluster))
Chris Mason56bec292009-03-13 10:10:06 -0400945 break;
Chris Mason56bec292009-03-13 10:10:06 -0400946
Chris Masonc3e69d52009-03-13 10:17:05 -0400947 locked_ref = list_entry(cluster->next,
948 struct btrfs_delayed_ref_head, cluster);
949
950 /* grab the lock that says we are going to process
951 * all the refs for this head */
952 ret = btrfs_delayed_ref_lock(trans, locked_ref);
953
954 /*
955 * we may have dropped the spin lock to get the head
956 * mutex lock, and that might have given someone else
957 * time to free the head. If that's true, it has been
958 * removed from our list and we can move on.
959 */
960 if (ret == -EAGAIN) {
961 locked_ref = NULL;
962 count++;
963 continue;
Chris Mason56bec292009-03-13 10:10:06 -0400964 }
965 }
966
967 /*
968 * record the must insert reserved flag before we
969 * drop the spin lock.
970 */
971 must_insert_reserved = locked_ref->must_insert_reserved;
972 locked_ref->must_insert_reserved = 0;
973
974 /*
975 * locked_ref is the head node, so we have to go one
976 * node back for any delayed ref updates
977 */
Chris Mason56bec292009-03-13 10:10:06 -0400978 ref = select_delayed_ref(locked_ref);
979 if (!ref) {
980 /* All delayed refs have been processed, Go ahead
981 * and send the head node to run_one_delayed_ref,
982 * so that any accounting fixes can happen
983 */
984 ref = &locked_ref->node;
Chris Masonc3e69d52009-03-13 10:17:05 -0400985 list_del_init(&locked_ref->cluster);
Chris Mason56bec292009-03-13 10:10:06 -0400986 locked_ref = NULL;
987 }
988
989 ref->in_tree = 0;
990 rb_erase(&ref->rb_node, &delayed_refs->root);
991 delayed_refs->num_entries--;
992 spin_unlock(&delayed_refs->lock);
993
994 ret = run_one_delayed_ref(trans, root, ref,
995 must_insert_reserved);
996 BUG_ON(ret);
997 btrfs_put_delayed_ref(ref);
998
Chris Masonc3e69d52009-03-13 10:17:05 -0400999 count++;
Chris Mason1887be62009-03-13 10:11:24 -04001000 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04001001 spin_lock(&delayed_refs->lock);
1002 }
Chris Masonc3e69d52009-03-13 10:17:05 -04001003 return count;
1004}
1005
1006/*
1007 * this starts processing the delayed reference count updates and
1008 * extent insertions we have queued up so far. count can be
1009 * 0, which means to process everything in the tree at the start
1010 * of the run (but not newly added entries), or it can be some target
1011 * number you'd like to process.
1012 */
1013int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
1014 struct btrfs_root *root, unsigned long count)
1015{
1016 struct rb_node *node;
1017 struct btrfs_delayed_ref_root *delayed_refs;
1018 struct btrfs_delayed_ref_node *ref;
1019 struct list_head cluster;
1020 int ret;
1021 int run_all = count == (unsigned long)-1;
1022 int run_most = 0;
1023
1024 if (root == root->fs_info->extent_root)
1025 root = root->fs_info->tree_root;
1026
1027 delayed_refs = &trans->transaction->delayed_refs;
1028 INIT_LIST_HEAD(&cluster);
1029again:
1030 spin_lock(&delayed_refs->lock);
1031 if (count == 0) {
1032 count = delayed_refs->num_entries * 2;
1033 run_most = 1;
1034 }
1035 while (1) {
1036 if (!(run_all || run_most) &&
1037 delayed_refs->num_heads_ready < 64)
1038 break;
1039
1040 /*
1041 * go find something we can process in the rbtree. We start at
1042 * the beginning of the tree, and then build a cluster
1043 * of refs to process starting at the first one we are able to
1044 * lock
1045 */
1046 ret = btrfs_find_ref_cluster(trans, &cluster,
1047 delayed_refs->run_delayed_start);
1048 if (ret)
1049 break;
1050
1051 ret = run_clustered_refs(trans, root, &cluster);
1052 BUG_ON(ret < 0);
1053
1054 count -= min_t(unsigned long, ret, count);
1055
1056 if (count == 0)
1057 break;
1058 }
1059
Chris Mason56bec292009-03-13 10:10:06 -04001060 if (run_all) {
Chris Mason56bec292009-03-13 10:10:06 -04001061 node = rb_first(&delayed_refs->root);
Chris Masonc3e69d52009-03-13 10:17:05 -04001062 if (!node)
Chris Mason56bec292009-03-13 10:10:06 -04001063 goto out;
Chris Masonc3e69d52009-03-13 10:17:05 -04001064 count = (unsigned long)-1;
Chris Mason56bec292009-03-13 10:10:06 -04001065
1066 while (node) {
1067 ref = rb_entry(node, struct btrfs_delayed_ref_node,
1068 rb_node);
1069 if (btrfs_delayed_ref_is_head(ref)) {
1070 struct btrfs_delayed_ref_head *head;
1071
1072 head = btrfs_delayed_node_to_head(ref);
1073 atomic_inc(&ref->refs);
1074
1075 spin_unlock(&delayed_refs->lock);
1076 mutex_lock(&head->mutex);
1077 mutex_unlock(&head->mutex);
1078
1079 btrfs_put_delayed_ref(ref);
Chris Mason1887be62009-03-13 10:11:24 -04001080 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04001081 goto again;
1082 }
1083 node = rb_next(node);
1084 }
1085 spin_unlock(&delayed_refs->lock);
Chris Mason56bec292009-03-13 10:10:06 -04001086 schedule_timeout(1);
1087 goto again;
1088 }
Chris Mason54aa1f42007-06-22 14:16:25 -04001089out:
Chris Masonc3e69d52009-03-13 10:17:05 -04001090 spin_unlock(&delayed_refs->lock);
Chris Masona28ec192007-03-06 20:08:01 -05001091 return 0;
1092}
1093
Yan Zheng80ff3852008-10-30 14:20:02 -04001094int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
Yan Zheng17d217f2008-12-12 10:03:38 -05001095 struct btrfs_root *root, u64 objectid, u64 bytenr)
Chris Masonbe20aa92007-12-17 20:14:01 -05001096{
1097 struct btrfs_root *extent_root = root->fs_info->extent_root;
1098 struct btrfs_path *path;
Yan Zhengf321e492008-07-30 09:26:11 -04001099 struct extent_buffer *leaf;
1100 struct btrfs_extent_ref *ref_item;
Chris Masonbe20aa92007-12-17 20:14:01 -05001101 struct btrfs_key key;
1102 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001103 u64 ref_root;
1104 u64 last_snapshot;
Yan Zhengf321e492008-07-30 09:26:11 -04001105 u32 nritems;
1106 int ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001107
Chris Masonbe20aa92007-12-17 20:14:01 -05001108 key.objectid = bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -04001109 key.offset = (u64)-1;
Yan Zhengf321e492008-07-30 09:26:11 -04001110 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masonbe20aa92007-12-17 20:14:01 -05001111
Yan Zhengf321e492008-07-30 09:26:11 -04001112 path = btrfs_alloc_path();
Chris Masonbe20aa92007-12-17 20:14:01 -05001113 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
1114 if (ret < 0)
1115 goto out;
1116 BUG_ON(ret == 0);
Yan Zheng80ff3852008-10-30 14:20:02 -04001117
1118 ret = -ENOENT;
1119 if (path->slots[0] == 0)
Zheng Yan31840ae2008-09-23 13:14:14 -04001120 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05001121
Zheng Yan31840ae2008-09-23 13:14:14 -04001122 path->slots[0]--;
Yan Zhengf321e492008-07-30 09:26:11 -04001123 leaf = path->nodes[0];
1124 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001125
1126 if (found_key.objectid != bytenr ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001127 found_key.type != BTRFS_EXTENT_ITEM_KEY)
Chris Masonbe20aa92007-12-17 20:14:01 -05001128 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05001129
Yan Zheng80ff3852008-10-30 14:20:02 -04001130 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Chris Masonbe20aa92007-12-17 20:14:01 -05001131 while (1) {
Yan Zhengf321e492008-07-30 09:26:11 -04001132 leaf = path->nodes[0];
1133 nritems = btrfs_header_nritems(leaf);
Chris Masonbe20aa92007-12-17 20:14:01 -05001134 if (path->slots[0] >= nritems) {
1135 ret = btrfs_next_leaf(extent_root, path);
Yan Zhengf321e492008-07-30 09:26:11 -04001136 if (ret < 0)
1137 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05001138 if (ret == 0)
1139 continue;
1140 break;
1141 }
Yan Zhengf321e492008-07-30 09:26:11 -04001142 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001143 if (found_key.objectid != bytenr)
1144 break;
Chris Masonbd098352008-01-03 13:23:19 -05001145
Chris Masonbe20aa92007-12-17 20:14:01 -05001146 if (found_key.type != BTRFS_EXTENT_REF_KEY) {
1147 path->slots[0]++;
1148 continue;
1149 }
1150
Yan Zhengf321e492008-07-30 09:26:11 -04001151 ref_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masonbe20aa92007-12-17 20:14:01 -05001152 struct btrfs_extent_ref);
Yan Zheng80ff3852008-10-30 14:20:02 -04001153 ref_root = btrfs_ref_root(leaf, ref_item);
Yan Zheng17d217f2008-12-12 10:03:38 -05001154 if ((ref_root != root->root_key.objectid &&
1155 ref_root != BTRFS_TREE_LOG_OBJECTID) ||
1156 objectid != btrfs_ref_objectid(leaf, ref_item)) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001157 ret = 1;
1158 goto out;
Yan Zhengf321e492008-07-30 09:26:11 -04001159 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001160 if (btrfs_ref_generation(leaf, ref_item) <= last_snapshot) {
1161 ret = 1;
1162 goto out;
1163 }
Yan Zhengf321e492008-07-30 09:26:11 -04001164
Chris Masonbe20aa92007-12-17 20:14:01 -05001165 path->slots[0]++;
1166 }
Yan Zhengf321e492008-07-30 09:26:11 -04001167 ret = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001168out:
Yan Zhengf321e492008-07-30 09:26:11 -04001169 btrfs_free_path(path);
1170 return ret;
1171}
1172
Zheng Yan31840ae2008-09-23 13:14:14 -04001173int btrfs_cache_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1174 struct extent_buffer *buf, u32 nr_extents)
Chris Mason02217ed2007-03-02 16:08:05 -05001175{
Chris Mason5f39d392007-10-15 16:14:19 -04001176 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -04001177 struct btrfs_file_extent_item *fi;
Zheng Yane4657682008-09-26 10:04:53 -04001178 u64 root_gen;
1179 u32 nritems;
Chris Mason02217ed2007-03-02 16:08:05 -05001180 int i;
Chris Masondb945352007-10-15 16:15:53 -04001181 int level;
Zheng Yan31840ae2008-09-23 13:14:14 -04001182 int ret = 0;
Zheng Yane4657682008-09-26 10:04:53 -04001183 int shared = 0;
Chris Masona28ec192007-03-06 20:08:01 -05001184
Chris Mason3768f362007-03-13 16:47:54 -04001185 if (!root->ref_cows)
Chris Masona28ec192007-03-06 20:08:01 -05001186 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001187
Zheng Yane4657682008-09-26 10:04:53 -04001188 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
1189 shared = 0;
1190 root_gen = root->root_key.offset;
1191 } else {
1192 shared = 1;
1193 root_gen = trans->transid - 1;
1194 }
1195
Chris Masondb945352007-10-15 16:15:53 -04001196 level = btrfs_header_level(buf);
Chris Mason5f39d392007-10-15 16:14:19 -04001197 nritems = btrfs_header_nritems(buf);
Chris Mason4a096752008-07-21 10:29:44 -04001198
Zheng Yan31840ae2008-09-23 13:14:14 -04001199 if (level == 0) {
Yan Zheng31153d82008-07-28 15:32:19 -04001200 struct btrfs_leaf_ref *ref;
1201 struct btrfs_extent_info *info;
1202
Zheng Yan31840ae2008-09-23 13:14:14 -04001203 ref = btrfs_alloc_leaf_ref(root, nr_extents);
Yan Zheng31153d82008-07-28 15:32:19 -04001204 if (!ref) {
Zheng Yan31840ae2008-09-23 13:14:14 -04001205 ret = -ENOMEM;
Yan Zheng31153d82008-07-28 15:32:19 -04001206 goto out;
1207 }
1208
Zheng Yane4657682008-09-26 10:04:53 -04001209 ref->root_gen = root_gen;
Yan Zheng31153d82008-07-28 15:32:19 -04001210 ref->bytenr = buf->start;
1211 ref->owner = btrfs_header_owner(buf);
1212 ref->generation = btrfs_header_generation(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04001213 ref->nritems = nr_extents;
Yan Zheng31153d82008-07-28 15:32:19 -04001214 info = ref->extents;
Yanbcc63ab2008-07-30 16:29:20 -04001215
Zheng Yan31840ae2008-09-23 13:14:14 -04001216 for (i = 0; nr_extents > 0 && i < nritems; i++) {
Yan Zheng31153d82008-07-28 15:32:19 -04001217 u64 disk_bytenr;
1218 btrfs_item_key_to_cpu(buf, &key, i);
1219 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1220 continue;
1221 fi = btrfs_item_ptr(buf, i,
1222 struct btrfs_file_extent_item);
1223 if (btrfs_file_extent_type(buf, fi) ==
1224 BTRFS_FILE_EXTENT_INLINE)
1225 continue;
1226 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1227 if (disk_bytenr == 0)
1228 continue;
1229
1230 info->bytenr = disk_bytenr;
1231 info->num_bytes =
1232 btrfs_file_extent_disk_num_bytes(buf, fi);
1233 info->objectid = key.objectid;
1234 info->offset = key.offset;
1235 info++;
1236 }
1237
Zheng Yane4657682008-09-26 10:04:53 -04001238 ret = btrfs_add_leaf_ref(root, ref, shared);
Yan Zheng5b84e8d2008-10-09 11:46:19 -04001239 if (ret == -EEXIST && shared) {
1240 struct btrfs_leaf_ref *old;
1241 old = btrfs_lookup_leaf_ref(root, ref->bytenr);
1242 BUG_ON(!old);
1243 btrfs_remove_leaf_ref(root, old);
1244 btrfs_free_leaf_ref(root, old);
1245 ret = btrfs_add_leaf_ref(root, ref, shared);
1246 }
Yan Zheng31153d82008-07-28 15:32:19 -04001247 WARN_ON(ret);
Yanbcc63ab2008-07-30 16:29:20 -04001248 btrfs_free_leaf_ref(root, ref);
Yan Zheng31153d82008-07-28 15:32:19 -04001249 }
1250out:
Zheng Yan31840ae2008-09-23 13:14:14 -04001251 return ret;
1252}
1253
Chris Masonb7a9f292009-02-04 09:23:45 -05001254/* when a block goes through cow, we update the reference counts of
1255 * everything that block points to. The internal pointers of the block
1256 * can be in just about any order, and it is likely to have clusters of
1257 * things that are close together and clusters of things that are not.
1258 *
1259 * To help reduce the seeks that come with updating all of these reference
1260 * counts, sort them by byte number before actual updates are done.
1261 *
1262 * struct refsort is used to match byte number to slot in the btree block.
1263 * we sort based on the byte number and then use the slot to actually
1264 * find the item.
Chris Masonbd56b302009-02-04 09:27:02 -05001265 *
1266 * struct refsort is smaller than strcut btrfs_item and smaller than
1267 * struct btrfs_key_ptr. Since we're currently limited to the page size
1268 * for a btree block, there's no way for a kmalloc of refsorts for a
1269 * single node to be bigger than a page.
Chris Masonb7a9f292009-02-04 09:23:45 -05001270 */
1271struct refsort {
1272 u64 bytenr;
1273 u32 slot;
1274};
1275
1276/*
1277 * for passing into sort()
1278 */
1279static int refsort_cmp(const void *a_void, const void *b_void)
1280{
1281 const struct refsort *a = a_void;
1282 const struct refsort *b = b_void;
1283
1284 if (a->bytenr < b->bytenr)
1285 return -1;
1286 if (a->bytenr > b->bytenr)
1287 return 1;
1288 return 0;
1289}
1290
1291
1292noinline int btrfs_inc_ref(struct btrfs_trans_handle *trans,
1293 struct btrfs_root *root,
1294 struct extent_buffer *orig_buf,
1295 struct extent_buffer *buf, u32 *nr_extents)
Zheng Yan31840ae2008-09-23 13:14:14 -04001296{
1297 u64 bytenr;
1298 u64 ref_root;
1299 u64 orig_root;
1300 u64 ref_generation;
1301 u64 orig_generation;
Chris Masonb7a9f292009-02-04 09:23:45 -05001302 struct refsort *sorted;
Zheng Yan31840ae2008-09-23 13:14:14 -04001303 u32 nritems;
1304 u32 nr_file_extents = 0;
1305 struct btrfs_key key;
1306 struct btrfs_file_extent_item *fi;
1307 int i;
1308 int level;
1309 int ret = 0;
1310 int faili = 0;
Chris Masonb7a9f292009-02-04 09:23:45 -05001311 int refi = 0;
1312 int slot;
Zheng Yan31840ae2008-09-23 13:14:14 -04001313 int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
Chris Mason56bec292009-03-13 10:10:06 -04001314 u64, u64, u64, u64, u64, u64, u64, u64, u64);
Zheng Yan31840ae2008-09-23 13:14:14 -04001315
1316 ref_root = btrfs_header_owner(buf);
1317 ref_generation = btrfs_header_generation(buf);
1318 orig_root = btrfs_header_owner(orig_buf);
1319 orig_generation = btrfs_header_generation(orig_buf);
1320
1321 nritems = btrfs_header_nritems(buf);
1322 level = btrfs_header_level(buf);
1323
Chris Masonb7a9f292009-02-04 09:23:45 -05001324 sorted = kmalloc(sizeof(struct refsort) * nritems, GFP_NOFS);
1325 BUG_ON(!sorted);
1326
Zheng Yan31840ae2008-09-23 13:14:14 -04001327 if (root->ref_cows) {
1328 process_func = __btrfs_inc_extent_ref;
1329 } else {
1330 if (level == 0 &&
1331 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
1332 goto out;
1333 if (level != 0 &&
1334 root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID)
1335 goto out;
1336 process_func = __btrfs_update_extent_ref;
1337 }
1338
Chris Masonb7a9f292009-02-04 09:23:45 -05001339 /*
1340 * we make two passes through the items. In the first pass we
1341 * only record the byte number and slot. Then we sort based on
1342 * byte number and do the actual work based on the sorted results
1343 */
Zheng Yan31840ae2008-09-23 13:14:14 -04001344 for (i = 0; i < nritems; i++) {
1345 cond_resched();
Chris Masondb945352007-10-15 16:15:53 -04001346 if (level == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001347 btrfs_item_key_to_cpu(buf, &key, i);
1348 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -04001349 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04001350 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -04001351 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001352 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -04001353 BTRFS_FILE_EXTENT_INLINE)
1354 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04001355 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1356 if (bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -04001357 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04001358
1359 nr_file_extents++;
Chris Masonb7a9f292009-02-04 09:23:45 -05001360 sorted[refi].bytenr = bytenr;
1361 sorted[refi].slot = i;
1362 refi++;
1363 } else {
1364 bytenr = btrfs_node_blockptr(buf, i);
1365 sorted[refi].bytenr = bytenr;
1366 sorted[refi].slot = i;
1367 refi++;
1368 }
1369 }
1370 /*
1371 * if refi == 0, we didn't actually put anything into the sorted
1372 * array and we're done
1373 */
1374 if (refi == 0)
1375 goto out;
1376
1377 sort(sorted, refi, sizeof(struct refsort), refsort_cmp, NULL);
1378
1379 for (i = 0; i < refi; i++) {
1380 cond_resched();
1381 slot = sorted[i].slot;
1382 bytenr = sorted[i].bytenr;
1383
1384 if (level == 0) {
1385 btrfs_item_key_to_cpu(buf, &key, slot);
Chris Mason56bec292009-03-13 10:10:06 -04001386 fi = btrfs_item_ptr(buf, slot,
1387 struct btrfs_file_extent_item);
1388
1389 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1390 if (bytenr == 0)
1391 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04001392
Zheng Yan31840ae2008-09-23 13:14:14 -04001393 ret = process_func(trans, root, bytenr,
Chris Mason56bec292009-03-13 10:10:06 -04001394 btrfs_file_extent_disk_num_bytes(buf, fi),
1395 orig_buf->start, buf->start,
1396 orig_root, ref_root,
1397 orig_generation, ref_generation,
1398 key.objectid);
Zheng Yan31840ae2008-09-23 13:14:14 -04001399
1400 if (ret) {
Chris Masonb7a9f292009-02-04 09:23:45 -05001401 faili = slot;
Zheng Yan31840ae2008-09-23 13:14:14 -04001402 WARN_ON(1);
1403 goto fail;
1404 }
Chris Mason54aa1f42007-06-22 14:16:25 -04001405 } else {
Chris Mason56bec292009-03-13 10:10:06 -04001406 ret = process_func(trans, root, bytenr, buf->len,
Zheng Yan31840ae2008-09-23 13:14:14 -04001407 orig_buf->start, buf->start,
1408 orig_root, ref_root,
1409 orig_generation, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001410 level - 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04001411 if (ret) {
Chris Masonb7a9f292009-02-04 09:23:45 -05001412 faili = slot;
Zheng Yan31840ae2008-09-23 13:14:14 -04001413 WARN_ON(1);
1414 goto fail;
1415 }
Chris Mason54aa1f42007-06-22 14:16:25 -04001416 }
1417 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001418out:
Chris Masonb7a9f292009-02-04 09:23:45 -05001419 kfree(sorted);
Zheng Yan31840ae2008-09-23 13:14:14 -04001420 if (nr_extents) {
1421 if (level == 0)
1422 *nr_extents = nr_file_extents;
1423 else
1424 *nr_extents = nritems;
1425 }
1426 return 0;
1427fail:
Chris Masonb7a9f292009-02-04 09:23:45 -05001428 kfree(sorted);
Zheng Yan31840ae2008-09-23 13:14:14 -04001429 WARN_ON(1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001430 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05001431}
1432
Zheng Yan31840ae2008-09-23 13:14:14 -04001433int btrfs_update_ref(struct btrfs_trans_handle *trans,
1434 struct btrfs_root *root, struct extent_buffer *orig_buf,
1435 struct extent_buffer *buf, int start_slot, int nr)
1436
1437{
1438 u64 bytenr;
1439 u64 ref_root;
1440 u64 orig_root;
1441 u64 ref_generation;
1442 u64 orig_generation;
1443 struct btrfs_key key;
1444 struct btrfs_file_extent_item *fi;
1445 int i;
1446 int ret;
1447 int slot;
1448 int level;
1449
1450 BUG_ON(start_slot < 0);
1451 BUG_ON(start_slot + nr > btrfs_header_nritems(buf));
1452
1453 ref_root = btrfs_header_owner(buf);
1454 ref_generation = btrfs_header_generation(buf);
1455 orig_root = btrfs_header_owner(orig_buf);
1456 orig_generation = btrfs_header_generation(orig_buf);
1457 level = btrfs_header_level(buf);
1458
1459 if (!root->ref_cows) {
1460 if (level == 0 &&
1461 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
1462 return 0;
1463 if (level != 0 &&
1464 root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID)
1465 return 0;
1466 }
1467
1468 for (i = 0, slot = start_slot; i < nr; i++, slot++) {
1469 cond_resched();
1470 if (level == 0) {
1471 btrfs_item_key_to_cpu(buf, &key, slot);
1472 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1473 continue;
1474 fi = btrfs_item_ptr(buf, slot,
1475 struct btrfs_file_extent_item);
1476 if (btrfs_file_extent_type(buf, fi) ==
1477 BTRFS_FILE_EXTENT_INLINE)
1478 continue;
1479 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1480 if (bytenr == 0)
1481 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04001482 ret = __btrfs_update_extent_ref(trans, root, bytenr,
Chris Mason56bec292009-03-13 10:10:06 -04001483 btrfs_file_extent_disk_num_bytes(buf, fi),
1484 orig_buf->start, buf->start,
1485 orig_root, ref_root, orig_generation,
1486 ref_generation, key.objectid);
Zheng Yan31840ae2008-09-23 13:14:14 -04001487 if (ret)
1488 goto fail;
1489 } else {
1490 bytenr = btrfs_node_blockptr(buf, slot);
Zheng Yan31840ae2008-09-23 13:14:14 -04001491 ret = __btrfs_update_extent_ref(trans, root, bytenr,
Chris Mason56bec292009-03-13 10:10:06 -04001492 buf->len, orig_buf->start,
1493 buf->start, orig_root, ref_root,
Zheng Yan31840ae2008-09-23 13:14:14 -04001494 orig_generation, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001495 level - 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04001496 if (ret)
1497 goto fail;
1498 }
1499 }
1500 return 0;
1501fail:
1502 WARN_ON(1);
1503 return -1;
1504}
1505
Chris Mason9078a3e2007-04-26 16:46:15 -04001506static int write_one_cache_group(struct btrfs_trans_handle *trans,
1507 struct btrfs_root *root,
1508 struct btrfs_path *path,
1509 struct btrfs_block_group_cache *cache)
1510{
1511 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04001512 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04001513 unsigned long bi;
1514 struct extent_buffer *leaf;
Chris Mason9078a3e2007-04-26 16:46:15 -04001515
Chris Mason9078a3e2007-04-26 16:46:15 -04001516 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001517 if (ret < 0)
1518 goto fail;
Chris Mason9078a3e2007-04-26 16:46:15 -04001519 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04001520
1521 leaf = path->nodes[0];
1522 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
1523 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
1524 btrfs_mark_buffer_dirty(leaf);
Chris Mason9078a3e2007-04-26 16:46:15 -04001525 btrfs_release_path(extent_root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -04001526fail:
Chris Mason9078a3e2007-04-26 16:46:15 -04001527 if (ret)
1528 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04001529 return 0;
1530
1531}
1532
Chris Mason96b51792007-10-15 16:15:19 -04001533int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
1534 struct btrfs_root *root)
Chris Mason9078a3e2007-04-26 16:46:15 -04001535{
Josef Bacik0f9dd462008-09-23 13:14:11 -04001536 struct btrfs_block_group_cache *cache, *entry;
1537 struct rb_node *n;
Chris Mason9078a3e2007-04-26 16:46:15 -04001538 int err = 0;
1539 int werr = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04001540 struct btrfs_path *path;
Chris Mason96b51792007-10-15 16:15:19 -04001541 u64 last = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04001542
1543 path = btrfs_alloc_path();
1544 if (!path)
1545 return -ENOMEM;
1546
Chris Masond3977122009-01-05 21:25:51 -05001547 while (1) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001548 cache = NULL;
1549 spin_lock(&root->fs_info->block_group_cache_lock);
1550 for (n = rb_first(&root->fs_info->block_group_cache_tree);
1551 n; n = rb_next(n)) {
1552 entry = rb_entry(n, struct btrfs_block_group_cache,
1553 cache_node);
1554 if (entry->dirty) {
1555 cache = entry;
1556 break;
1557 }
1558 }
1559 spin_unlock(&root->fs_info->block_group_cache_lock);
1560
1561 if (!cache)
Chris Mason9078a3e2007-04-26 16:46:15 -04001562 break;
Chris Mason54aa1f42007-06-22 14:16:25 -04001563
Zheng Yane8569812008-09-26 10:05:48 -04001564 cache->dirty = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001565 last += cache->key.offset;
1566
Chris Mason96b51792007-10-15 16:15:19 -04001567 err = write_one_cache_group(trans, root,
1568 path, cache);
1569 /*
1570 * if we fail to write the cache group, we want
1571 * to keep it marked dirty in hopes that a later
1572 * write will work
1573 */
1574 if (err) {
1575 werr = err;
1576 continue;
Chris Mason9078a3e2007-04-26 16:46:15 -04001577 }
1578 }
1579 btrfs_free_path(path);
1580 return werr;
1581}
1582
Yan Zhengd2fb3432008-12-11 16:30:39 -05001583int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr)
1584{
1585 struct btrfs_block_group_cache *block_group;
1586 int readonly = 0;
1587
1588 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
1589 if (!block_group || block_group->ro)
1590 readonly = 1;
1591 if (block_group)
1592 put_block_group(block_group);
1593 return readonly;
1594}
1595
Chris Mason593060d2008-03-25 16:50:33 -04001596static int update_space_info(struct btrfs_fs_info *info, u64 flags,
1597 u64 total_bytes, u64 bytes_used,
1598 struct btrfs_space_info **space_info)
1599{
1600 struct btrfs_space_info *found;
1601
1602 found = __find_space_info(info, flags);
1603 if (found) {
Josef Bacik25179202008-10-29 14:49:05 -04001604 spin_lock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04001605 found->total_bytes += total_bytes;
1606 found->bytes_used += bytes_used;
Chris Mason8f18cf12008-04-25 16:53:30 -04001607 found->full = 0;
Josef Bacik25179202008-10-29 14:49:05 -04001608 spin_unlock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04001609 *space_info = found;
1610 return 0;
1611 }
Yan Zhengc146afa2008-11-12 14:34:12 -05001612 found = kzalloc(sizeof(*found), GFP_NOFS);
Chris Mason593060d2008-03-25 16:50:33 -04001613 if (!found)
1614 return -ENOMEM;
1615
Josef Bacik0f9dd462008-09-23 13:14:11 -04001616 INIT_LIST_HEAD(&found->block_groups);
Josef Bacik80eb2342008-10-29 14:49:05 -04001617 init_rwsem(&found->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001618 spin_lock_init(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04001619 found->flags = flags;
1620 found->total_bytes = total_bytes;
1621 found->bytes_used = bytes_used;
1622 found->bytes_pinned = 0;
Zheng Yane8569812008-09-26 10:05:48 -04001623 found->bytes_reserved = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05001624 found->bytes_readonly = 0;
Josef Bacik6a632092009-02-20 11:00:09 -05001625 found->bytes_delalloc = 0;
Chris Mason593060d2008-03-25 16:50:33 -04001626 found->full = 0;
Chris Mason0ef3e662008-05-24 14:04:53 -04001627 found->force_alloc = 0;
Chris Mason593060d2008-03-25 16:50:33 -04001628 *space_info = found;
Chris Mason4184ea72009-03-10 12:39:20 -04001629 list_add_rcu(&found->list, &info->space_info);
Chris Mason593060d2008-03-25 16:50:33 -04001630 return 0;
1631}
1632
Chris Mason8790d502008-04-03 16:29:03 -04001633static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
1634{
1635 u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
Chris Mason611f0e02008-04-03 16:29:03 -04001636 BTRFS_BLOCK_GROUP_RAID1 |
Chris Mason321aecc2008-04-16 10:49:51 -04001637 BTRFS_BLOCK_GROUP_RAID10 |
Chris Mason611f0e02008-04-03 16:29:03 -04001638 BTRFS_BLOCK_GROUP_DUP);
Chris Mason8790d502008-04-03 16:29:03 -04001639 if (extra_flags) {
1640 if (flags & BTRFS_BLOCK_GROUP_DATA)
1641 fs_info->avail_data_alloc_bits |= extra_flags;
1642 if (flags & BTRFS_BLOCK_GROUP_METADATA)
1643 fs_info->avail_metadata_alloc_bits |= extra_flags;
1644 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
1645 fs_info->avail_system_alloc_bits |= extra_flags;
1646 }
1647}
Chris Mason593060d2008-03-25 16:50:33 -04001648
Yan Zhengc146afa2008-11-12 14:34:12 -05001649static void set_block_group_readonly(struct btrfs_block_group_cache *cache)
1650{
1651 spin_lock(&cache->space_info->lock);
1652 spin_lock(&cache->lock);
1653 if (!cache->ro) {
1654 cache->space_info->bytes_readonly += cache->key.offset -
1655 btrfs_block_group_used(&cache->item);
1656 cache->ro = 1;
1657 }
1658 spin_unlock(&cache->lock);
1659 spin_unlock(&cache->space_info->lock);
1660}
1661
Yan Zheng2b820322008-11-17 21:11:30 -05001662u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
Chris Masonec44a352008-04-28 15:29:52 -04001663{
Yan Zheng2b820322008-11-17 21:11:30 -05001664 u64 num_devices = root->fs_info->fs_devices->rw_devices;
Chris Masona061fc82008-05-07 11:43:44 -04001665
1666 if (num_devices == 1)
1667 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
1668 if (num_devices < 4)
1669 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
1670
Chris Masonec44a352008-04-28 15:29:52 -04001671 if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
1672 (flags & (BTRFS_BLOCK_GROUP_RAID1 |
Chris Masona061fc82008-05-07 11:43:44 -04001673 BTRFS_BLOCK_GROUP_RAID10))) {
Chris Masonec44a352008-04-28 15:29:52 -04001674 flags &= ~BTRFS_BLOCK_GROUP_DUP;
Chris Masona061fc82008-05-07 11:43:44 -04001675 }
Chris Masonec44a352008-04-28 15:29:52 -04001676
1677 if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
Chris Masona061fc82008-05-07 11:43:44 -04001678 (flags & BTRFS_BLOCK_GROUP_RAID10)) {
Chris Masonec44a352008-04-28 15:29:52 -04001679 flags &= ~BTRFS_BLOCK_GROUP_RAID1;
Chris Masona061fc82008-05-07 11:43:44 -04001680 }
Chris Masonec44a352008-04-28 15:29:52 -04001681
1682 if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
1683 ((flags & BTRFS_BLOCK_GROUP_RAID1) |
1684 (flags & BTRFS_BLOCK_GROUP_RAID10) |
1685 (flags & BTRFS_BLOCK_GROUP_DUP)))
1686 flags &= ~BTRFS_BLOCK_GROUP_RAID0;
1687 return flags;
1688}
1689
Josef Bacik6a632092009-02-20 11:00:09 -05001690static u64 btrfs_get_alloc_profile(struct btrfs_root *root, u64 data)
1691{
1692 struct btrfs_fs_info *info = root->fs_info;
1693 u64 alloc_profile;
1694
1695 if (data) {
1696 alloc_profile = info->avail_data_alloc_bits &
1697 info->data_alloc_profile;
1698 data = BTRFS_BLOCK_GROUP_DATA | alloc_profile;
1699 } else if (root == root->fs_info->chunk_root) {
1700 alloc_profile = info->avail_system_alloc_bits &
1701 info->system_alloc_profile;
1702 data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile;
1703 } else {
1704 alloc_profile = info->avail_metadata_alloc_bits &
1705 info->metadata_alloc_profile;
1706 data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile;
1707 }
1708
1709 return btrfs_reduce_alloc_profile(root, data);
1710}
1711
1712void btrfs_set_inode_space_info(struct btrfs_root *root, struct inode *inode)
1713{
1714 u64 alloc_target;
1715
1716 alloc_target = btrfs_get_alloc_profile(root, 1);
1717 BTRFS_I(inode)->space_info = __find_space_info(root->fs_info,
1718 alloc_target);
1719}
1720
1721/*
1722 * for now this just makes sure we have at least 5% of our metadata space free
1723 * for use.
1724 */
1725int btrfs_check_metadata_free_space(struct btrfs_root *root)
1726{
1727 struct btrfs_fs_info *info = root->fs_info;
1728 struct btrfs_space_info *meta_sinfo;
1729 u64 alloc_target, thresh;
Josef Bacik4e06bdd2009-02-20 10:59:53 -05001730 int committed = 0, ret;
Josef Bacik6a632092009-02-20 11:00:09 -05001731
1732 /* get the space info for where the metadata will live */
1733 alloc_target = btrfs_get_alloc_profile(root, 0);
1734 meta_sinfo = __find_space_info(info, alloc_target);
1735
Josef Bacik4e06bdd2009-02-20 10:59:53 -05001736again:
Josef Bacik6a632092009-02-20 11:00:09 -05001737 spin_lock(&meta_sinfo->lock);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05001738 if (!meta_sinfo->full)
1739 thresh = meta_sinfo->total_bytes * 80;
1740 else
1741 thresh = meta_sinfo->total_bytes * 95;
Josef Bacik6a632092009-02-20 11:00:09 -05001742
1743 do_div(thresh, 100);
1744
1745 if (meta_sinfo->bytes_used + meta_sinfo->bytes_reserved +
1746 meta_sinfo->bytes_pinned + meta_sinfo->bytes_readonly > thresh) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05001747 struct btrfs_trans_handle *trans;
1748 if (!meta_sinfo->full) {
1749 meta_sinfo->force_alloc = 1;
1750 spin_unlock(&meta_sinfo->lock);
1751
1752 trans = btrfs_start_transaction(root, 1);
1753 if (!trans)
1754 return -ENOMEM;
1755
1756 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
1757 2 * 1024 * 1024, alloc_target, 0);
1758 btrfs_end_transaction(trans, root);
1759 goto again;
1760 }
Josef Bacik6a632092009-02-20 11:00:09 -05001761 spin_unlock(&meta_sinfo->lock);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05001762
1763 if (!committed) {
1764 committed = 1;
1765 trans = btrfs_join_transaction(root, 1);
1766 if (!trans)
1767 return -ENOMEM;
1768 ret = btrfs_commit_transaction(trans, root);
1769 if (ret)
1770 return ret;
1771 goto again;
1772 }
Josef Bacik6a632092009-02-20 11:00:09 -05001773 return -ENOSPC;
1774 }
1775 spin_unlock(&meta_sinfo->lock);
1776
1777 return 0;
1778}
1779
1780/*
1781 * This will check the space that the inode allocates from to make sure we have
1782 * enough space for bytes.
1783 */
1784int btrfs_check_data_free_space(struct btrfs_root *root, struct inode *inode,
1785 u64 bytes)
1786{
1787 struct btrfs_space_info *data_sinfo;
Josef Bacik4e06bdd2009-02-20 10:59:53 -05001788 int ret = 0, committed = 0;
Josef Bacik6a632092009-02-20 11:00:09 -05001789
1790 /* make sure bytes are sectorsize aligned */
1791 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
1792
1793 data_sinfo = BTRFS_I(inode)->space_info;
1794again:
1795 /* make sure we have enough space to handle the data first */
1796 spin_lock(&data_sinfo->lock);
1797 if (data_sinfo->total_bytes - data_sinfo->bytes_used -
1798 data_sinfo->bytes_delalloc - data_sinfo->bytes_reserved -
1799 data_sinfo->bytes_pinned - data_sinfo->bytes_readonly -
1800 data_sinfo->bytes_may_use < bytes) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05001801 struct btrfs_trans_handle *trans;
1802
Josef Bacik6a632092009-02-20 11:00:09 -05001803 /*
1804 * if we don't have enough free bytes in this space then we need
1805 * to alloc a new chunk.
1806 */
1807 if (!data_sinfo->full) {
1808 u64 alloc_target;
Josef Bacik6a632092009-02-20 11:00:09 -05001809
1810 data_sinfo->force_alloc = 1;
1811 spin_unlock(&data_sinfo->lock);
1812
1813 alloc_target = btrfs_get_alloc_profile(root, 1);
1814 trans = btrfs_start_transaction(root, 1);
1815 if (!trans)
1816 return -ENOMEM;
1817
1818 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
1819 bytes + 2 * 1024 * 1024,
1820 alloc_target, 0);
1821 btrfs_end_transaction(trans, root);
1822 if (ret)
1823 return ret;
1824 goto again;
1825 }
1826 spin_unlock(&data_sinfo->lock);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05001827
1828 /* commit the current transaction and try again */
1829 if (!committed) {
1830 committed = 1;
1831 trans = btrfs_join_transaction(root, 1);
1832 if (!trans)
1833 return -ENOMEM;
1834 ret = btrfs_commit_transaction(trans, root);
1835 if (ret)
1836 return ret;
1837 goto again;
1838 }
1839
Josef Bacik6a632092009-02-20 11:00:09 -05001840 printk(KERN_ERR "no space left, need %llu, %llu delalloc bytes"
1841 ", %llu bytes_used, %llu bytes_reserved, "
1842 "%llu bytes_pinned, %llu bytes_readonly, %llu may use"
1843 "%llu total\n", bytes, data_sinfo->bytes_delalloc,
1844 data_sinfo->bytes_used, data_sinfo->bytes_reserved,
1845 data_sinfo->bytes_pinned, data_sinfo->bytes_readonly,
1846 data_sinfo->bytes_may_use, data_sinfo->total_bytes);
1847 return -ENOSPC;
1848 }
1849 data_sinfo->bytes_may_use += bytes;
1850 BTRFS_I(inode)->reserved_bytes += bytes;
1851 spin_unlock(&data_sinfo->lock);
1852
1853 return btrfs_check_metadata_free_space(root);
1854}
1855
1856/*
1857 * if there was an error for whatever reason after calling
1858 * btrfs_check_data_free_space, call this so we can cleanup the counters.
1859 */
1860void btrfs_free_reserved_data_space(struct btrfs_root *root,
1861 struct inode *inode, u64 bytes)
1862{
1863 struct btrfs_space_info *data_sinfo;
1864
1865 /* make sure bytes are sectorsize aligned */
1866 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
1867
1868 data_sinfo = BTRFS_I(inode)->space_info;
1869 spin_lock(&data_sinfo->lock);
1870 data_sinfo->bytes_may_use -= bytes;
1871 BTRFS_I(inode)->reserved_bytes -= bytes;
1872 spin_unlock(&data_sinfo->lock);
1873}
1874
1875/* called when we are adding a delalloc extent to the inode's io_tree */
1876void btrfs_delalloc_reserve_space(struct btrfs_root *root, struct inode *inode,
1877 u64 bytes)
1878{
1879 struct btrfs_space_info *data_sinfo;
1880
1881 /* get the space info for where this inode will be storing its data */
1882 data_sinfo = BTRFS_I(inode)->space_info;
1883
1884 /* make sure we have enough space to handle the data first */
1885 spin_lock(&data_sinfo->lock);
1886 data_sinfo->bytes_delalloc += bytes;
1887
1888 /*
1889 * we are adding a delalloc extent without calling
1890 * btrfs_check_data_free_space first. This happens on a weird
1891 * writepage condition, but shouldn't hurt our accounting
1892 */
1893 if (unlikely(bytes > BTRFS_I(inode)->reserved_bytes)) {
1894 data_sinfo->bytes_may_use -= BTRFS_I(inode)->reserved_bytes;
1895 BTRFS_I(inode)->reserved_bytes = 0;
1896 } else {
1897 data_sinfo->bytes_may_use -= bytes;
1898 BTRFS_I(inode)->reserved_bytes -= bytes;
1899 }
1900
1901 spin_unlock(&data_sinfo->lock);
1902}
1903
1904/* called when we are clearing an delalloc extent from the inode's io_tree */
1905void btrfs_delalloc_free_space(struct btrfs_root *root, struct inode *inode,
1906 u64 bytes)
1907{
1908 struct btrfs_space_info *info;
1909
1910 info = BTRFS_I(inode)->space_info;
1911
1912 spin_lock(&info->lock);
1913 info->bytes_delalloc -= bytes;
1914 spin_unlock(&info->lock);
1915}
1916
Chris Mason6324fbf2008-03-24 15:01:59 -04001917static int do_chunk_alloc(struct btrfs_trans_handle *trans,
1918 struct btrfs_root *extent_root, u64 alloc_bytes,
Chris Mason0ef3e662008-05-24 14:04:53 -04001919 u64 flags, int force)
Chris Mason6324fbf2008-03-24 15:01:59 -04001920{
1921 struct btrfs_space_info *space_info;
1922 u64 thresh;
Yan Zhengc146afa2008-11-12 14:34:12 -05001923 int ret = 0;
1924
1925 mutex_lock(&extent_root->fs_info->chunk_mutex);
Chris Mason6324fbf2008-03-24 15:01:59 -04001926
Yan Zheng2b820322008-11-17 21:11:30 -05001927 flags = btrfs_reduce_alloc_profile(extent_root, flags);
Chris Masonec44a352008-04-28 15:29:52 -04001928
Chris Mason6324fbf2008-03-24 15:01:59 -04001929 space_info = __find_space_info(extent_root->fs_info, flags);
Chris Mason593060d2008-03-25 16:50:33 -04001930 if (!space_info) {
1931 ret = update_space_info(extent_root->fs_info, flags,
1932 0, 0, &space_info);
1933 BUG_ON(ret);
1934 }
Chris Mason6324fbf2008-03-24 15:01:59 -04001935 BUG_ON(!space_info);
1936
Josef Bacik25179202008-10-29 14:49:05 -04001937 spin_lock(&space_info->lock);
Chris Mason0ef3e662008-05-24 14:04:53 -04001938 if (space_info->force_alloc) {
1939 force = 1;
1940 space_info->force_alloc = 0;
1941 }
Josef Bacik25179202008-10-29 14:49:05 -04001942 if (space_info->full) {
1943 spin_unlock(&space_info->lock);
Chris Mason925baed2008-06-25 16:01:30 -04001944 goto out;
Josef Bacik25179202008-10-29 14:49:05 -04001945 }
Chris Mason6324fbf2008-03-24 15:01:59 -04001946
Yan Zhengc146afa2008-11-12 14:34:12 -05001947 thresh = space_info->total_bytes - space_info->bytes_readonly;
1948 thresh = div_factor(thresh, 6);
Chris Mason0ef3e662008-05-24 14:04:53 -04001949 if (!force &&
Zheng Yane8569812008-09-26 10:05:48 -04001950 (space_info->bytes_used + space_info->bytes_pinned +
Josef Bacik25179202008-10-29 14:49:05 -04001951 space_info->bytes_reserved + alloc_bytes) < thresh) {
1952 spin_unlock(&space_info->lock);
Chris Mason925baed2008-06-25 16:01:30 -04001953 goto out;
Josef Bacik25179202008-10-29 14:49:05 -04001954 }
Josef Bacik25179202008-10-29 14:49:05 -04001955 spin_unlock(&space_info->lock);
1956
Yan Zheng2b820322008-11-17 21:11:30 -05001957 ret = btrfs_alloc_chunk(trans, extent_root, flags);
Chris Masond3977122009-01-05 21:25:51 -05001958 if (ret)
Chris Mason6324fbf2008-03-24 15:01:59 -04001959 space_info->full = 1;
Chris Masona74a4b92008-06-25 16:01:31 -04001960out:
Yan Zhengc146afa2008-11-12 14:34:12 -05001961 mutex_unlock(&extent_root->fs_info->chunk_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001962 return ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04001963}
1964
Chris Mason9078a3e2007-04-26 16:46:15 -04001965static int update_block_group(struct btrfs_trans_handle *trans,
1966 struct btrfs_root *root,
Chris Masondb945352007-10-15 16:15:53 -04001967 u64 bytenr, u64 num_bytes, int alloc,
Chris Mason0b86a832008-03-24 15:01:56 -04001968 int mark_free)
Chris Mason9078a3e2007-04-26 16:46:15 -04001969{
1970 struct btrfs_block_group_cache *cache;
1971 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -04001972 u64 total = num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04001973 u64 old_val;
Chris Masondb945352007-10-15 16:15:53 -04001974 u64 byte_in_group;
Chris Mason3e1ad542007-05-07 20:03:49 -04001975
Chris Masond3977122009-01-05 21:25:51 -05001976 while (total) {
Chris Masondb945352007-10-15 16:15:53 -04001977 cache = btrfs_lookup_block_group(info, bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05001978 if (!cache)
Chris Mason9078a3e2007-04-26 16:46:15 -04001979 return -1;
Chris Masondb945352007-10-15 16:15:53 -04001980 byte_in_group = bytenr - cache->key.objectid;
1981 WARN_ON(byte_in_group > cache->key.offset);
Chris Mason9078a3e2007-04-26 16:46:15 -04001982
Josef Bacik25179202008-10-29 14:49:05 -04001983 spin_lock(&cache->space_info->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04001984 spin_lock(&cache->lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001985 cache->dirty = 1;
Chris Mason9078a3e2007-04-26 16:46:15 -04001986 old_val = btrfs_block_group_used(&cache->item);
Chris Masondb945352007-10-15 16:15:53 -04001987 num_bytes = min(total, cache->key.offset - byte_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -04001988 if (alloc) {
Chris Masondb945352007-10-15 16:15:53 -04001989 old_val += num_bytes;
Chris Mason6324fbf2008-03-24 15:01:59 -04001990 cache->space_info->bytes_used += num_bytes;
Yan Zhenga512bbf2008-12-08 16:46:26 -05001991 if (cache->ro)
Yan Zhengc146afa2008-11-12 14:34:12 -05001992 cache->space_info->bytes_readonly -= num_bytes;
Chris Masonc286ac42008-07-22 23:06:41 -04001993 btrfs_set_block_group_used(&cache->item, old_val);
1994 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04001995 spin_unlock(&cache->space_info->lock);
Chris Masoncd1bc462007-04-27 10:08:34 -04001996 } else {
Chris Masondb945352007-10-15 16:15:53 -04001997 old_val -= num_bytes;
Chris Mason6324fbf2008-03-24 15:01:59 -04001998 cache->space_info->bytes_used -= num_bytes;
Yan Zhengc146afa2008-11-12 14:34:12 -05001999 if (cache->ro)
2000 cache->space_info->bytes_readonly += num_bytes;
Chris Masonc286ac42008-07-22 23:06:41 -04002001 btrfs_set_block_group_used(&cache->item, old_val);
2002 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04002003 spin_unlock(&cache->space_info->lock);
Chris Masonf510cfe2007-10-15 16:14:48 -04002004 if (mark_free) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04002005 int ret;
Liu Hui1f3c79a2009-01-05 15:57:51 -05002006
2007 ret = btrfs_discard_extent(root, bytenr,
2008 num_bytes);
2009 WARN_ON(ret);
2010
Josef Bacik0f9dd462008-09-23 13:14:11 -04002011 ret = btrfs_add_free_space(cache, bytenr,
2012 num_bytes);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002013 WARN_ON(ret);
Chris Masone37c9e62007-05-09 20:13:14 -04002014 }
Chris Masoncd1bc462007-04-27 10:08:34 -04002015 }
Yan Zhengd2fb3432008-12-11 16:30:39 -05002016 put_block_group(cache);
Chris Masondb945352007-10-15 16:15:53 -04002017 total -= num_bytes;
2018 bytenr += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04002019 }
2020 return 0;
2021}
Chris Mason6324fbf2008-03-24 15:01:59 -04002022
Chris Masona061fc82008-05-07 11:43:44 -04002023static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
2024{
Josef Bacik0f9dd462008-09-23 13:14:11 -04002025 struct btrfs_block_group_cache *cache;
Yan Zhengd2fb3432008-12-11 16:30:39 -05002026 u64 bytenr;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002027
2028 cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
2029 if (!cache)
Chris Masona061fc82008-05-07 11:43:44 -04002030 return 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002031
Yan Zhengd2fb3432008-12-11 16:30:39 -05002032 bytenr = cache->key.objectid;
2033 put_block_group(cache);
2034
2035 return bytenr;
Chris Masona061fc82008-05-07 11:43:44 -04002036}
2037
Chris Masone02119d2008-09-05 16:13:11 -04002038int btrfs_update_pinned_extents(struct btrfs_root *root,
Yan324ae4d2007-11-16 14:57:08 -05002039 u64 bytenr, u64 num, int pin)
2040{
2041 u64 len;
2042 struct btrfs_block_group_cache *cache;
2043 struct btrfs_fs_info *fs_info = root->fs_info;
2044
Josef Bacik25179202008-10-29 14:49:05 -04002045 WARN_ON(!mutex_is_locked(&root->fs_info->pinned_mutex));
Yan324ae4d2007-11-16 14:57:08 -05002046 if (pin) {
2047 set_extent_dirty(&fs_info->pinned_extents,
2048 bytenr, bytenr + num - 1, GFP_NOFS);
2049 } else {
2050 clear_extent_dirty(&fs_info->pinned_extents,
2051 bytenr, bytenr + num - 1, GFP_NOFS);
2052 }
2053 while (num > 0) {
2054 cache = btrfs_lookup_block_group(fs_info, bytenr);
Zheng Yane8569812008-09-26 10:05:48 -04002055 BUG_ON(!cache);
2056 len = min(num, cache->key.offset -
2057 (bytenr - cache->key.objectid));
Yan324ae4d2007-11-16 14:57:08 -05002058 if (pin) {
Josef Bacik25179202008-10-29 14:49:05 -04002059 spin_lock(&cache->space_info->lock);
Zheng Yane8569812008-09-26 10:05:48 -04002060 spin_lock(&cache->lock);
2061 cache->pinned += len;
2062 cache->space_info->bytes_pinned += len;
2063 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04002064 spin_unlock(&cache->space_info->lock);
Yan324ae4d2007-11-16 14:57:08 -05002065 fs_info->total_pinned += len;
2066 } else {
Josef Bacik25179202008-10-29 14:49:05 -04002067 spin_lock(&cache->space_info->lock);
Zheng Yane8569812008-09-26 10:05:48 -04002068 spin_lock(&cache->lock);
2069 cache->pinned -= len;
2070 cache->space_info->bytes_pinned -= len;
2071 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04002072 spin_unlock(&cache->space_info->lock);
Yan324ae4d2007-11-16 14:57:08 -05002073 fs_info->total_pinned -= len;
Josef Bacik07103a32008-11-19 15:17:55 -05002074 if (cache->cached)
2075 btrfs_add_free_space(cache, bytenr, len);
Yan324ae4d2007-11-16 14:57:08 -05002076 }
Yan Zhengd2fb3432008-12-11 16:30:39 -05002077 put_block_group(cache);
Yan324ae4d2007-11-16 14:57:08 -05002078 bytenr += len;
2079 num -= len;
2080 }
2081 return 0;
2082}
Chris Mason9078a3e2007-04-26 16:46:15 -04002083
Zheng Yane8569812008-09-26 10:05:48 -04002084static int update_reserved_extents(struct btrfs_root *root,
2085 u64 bytenr, u64 num, int reserve)
2086{
2087 u64 len;
2088 struct btrfs_block_group_cache *cache;
2089 struct btrfs_fs_info *fs_info = root->fs_info;
2090
Zheng Yane8569812008-09-26 10:05:48 -04002091 while (num > 0) {
2092 cache = btrfs_lookup_block_group(fs_info, bytenr);
2093 BUG_ON(!cache);
2094 len = min(num, cache->key.offset -
2095 (bytenr - cache->key.objectid));
Josef Bacik25179202008-10-29 14:49:05 -04002096
2097 spin_lock(&cache->space_info->lock);
2098 spin_lock(&cache->lock);
Zheng Yane8569812008-09-26 10:05:48 -04002099 if (reserve) {
Zheng Yane8569812008-09-26 10:05:48 -04002100 cache->reserved += len;
2101 cache->space_info->bytes_reserved += len;
Zheng Yane8569812008-09-26 10:05:48 -04002102 } else {
Zheng Yane8569812008-09-26 10:05:48 -04002103 cache->reserved -= len;
2104 cache->space_info->bytes_reserved -= len;
Zheng Yane8569812008-09-26 10:05:48 -04002105 }
Josef Bacik25179202008-10-29 14:49:05 -04002106 spin_unlock(&cache->lock);
2107 spin_unlock(&cache->space_info->lock);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002108 put_block_group(cache);
Zheng Yane8569812008-09-26 10:05:48 -04002109 bytenr += len;
2110 num -= len;
2111 }
2112 return 0;
2113}
2114
Chris Masond1310b22008-01-24 16:13:08 -05002115int btrfs_copy_pinned(struct btrfs_root *root, struct extent_io_tree *copy)
Chris Masonccd467d2007-06-28 15:57:36 -04002116{
Chris Masonccd467d2007-06-28 15:57:36 -04002117 u64 last = 0;
Chris Mason1a5bc162007-10-15 16:15:26 -04002118 u64 start;
2119 u64 end;
Chris Masond1310b22008-01-24 16:13:08 -05002120 struct extent_io_tree *pinned_extents = &root->fs_info->pinned_extents;
Chris Masonccd467d2007-06-28 15:57:36 -04002121 int ret;
Chris Masonccd467d2007-06-28 15:57:36 -04002122
Josef Bacik25179202008-10-29 14:49:05 -04002123 mutex_lock(&root->fs_info->pinned_mutex);
Chris Masond3977122009-01-05 21:25:51 -05002124 while (1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04002125 ret = find_first_extent_bit(pinned_extents, last,
2126 &start, &end, EXTENT_DIRTY);
2127 if (ret)
Chris Masonccd467d2007-06-28 15:57:36 -04002128 break;
Chris Mason1a5bc162007-10-15 16:15:26 -04002129 set_extent_dirty(copy, start, end, GFP_NOFS);
2130 last = end + 1;
Chris Masonccd467d2007-06-28 15:57:36 -04002131 }
Josef Bacik25179202008-10-29 14:49:05 -04002132 mutex_unlock(&root->fs_info->pinned_mutex);
Chris Masonccd467d2007-06-28 15:57:36 -04002133 return 0;
2134}
2135
2136int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
2137 struct btrfs_root *root,
Chris Masond1310b22008-01-24 16:13:08 -05002138 struct extent_io_tree *unpin)
Chris Masona28ec192007-03-06 20:08:01 -05002139{
Chris Mason1a5bc162007-10-15 16:15:26 -04002140 u64 start;
2141 u64 end;
Chris Masona28ec192007-03-06 20:08:01 -05002142 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05002143
Josef Bacik25179202008-10-29 14:49:05 -04002144 mutex_lock(&root->fs_info->pinned_mutex);
Chris Masond3977122009-01-05 21:25:51 -05002145 while (1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04002146 ret = find_first_extent_bit(unpin, 0, &start, &end,
2147 EXTENT_DIRTY);
2148 if (ret)
Chris Masona28ec192007-03-06 20:08:01 -05002149 break;
Liu Hui1f3c79a2009-01-05 15:57:51 -05002150
2151 ret = btrfs_discard_extent(root, start, end + 1 - start);
2152
Chris Masone02119d2008-09-05 16:13:11 -04002153 btrfs_update_pinned_extents(root, start, end + 1 - start, 0);
Chris Mason1a5bc162007-10-15 16:15:26 -04002154 clear_extent_dirty(unpin, start, end, GFP_NOFS);
Liu Hui1f3c79a2009-01-05 15:57:51 -05002155
Chris Masonc286ac42008-07-22 23:06:41 -04002156 if (need_resched()) {
Josef Bacik25179202008-10-29 14:49:05 -04002157 mutex_unlock(&root->fs_info->pinned_mutex);
Chris Masonc286ac42008-07-22 23:06:41 -04002158 cond_resched();
Josef Bacik25179202008-10-29 14:49:05 -04002159 mutex_lock(&root->fs_info->pinned_mutex);
Chris Masonc286ac42008-07-22 23:06:41 -04002160 }
Chris Masona28ec192007-03-06 20:08:01 -05002161 }
Josef Bacik25179202008-10-29 14:49:05 -04002162 mutex_unlock(&root->fs_info->pinned_mutex);
Liu Hui1f3c79a2009-01-05 15:57:51 -05002163 return ret;
Chris Masona28ec192007-03-06 20:08:01 -05002164}
2165
Zheng Yan31840ae2008-09-23 13:14:14 -04002166static int pin_down_bytes(struct btrfs_trans_handle *trans,
2167 struct btrfs_root *root,
2168 u64 bytenr, u64 num_bytes, int is_data)
Chris Masone20d96d2007-03-22 12:13:20 -04002169{
Chris Mason1a5bc162007-10-15 16:15:26 -04002170 int err = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04002171 struct extent_buffer *buf;
Chris Mason78fae272007-03-25 11:35:08 -04002172
Zheng Yan31840ae2008-09-23 13:14:14 -04002173 if (is_data)
2174 goto pinit;
Chris Mason4bef0842008-09-08 11:18:08 -04002175
Zheng Yan31840ae2008-09-23 13:14:14 -04002176 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
2177 if (!buf)
2178 goto pinit;
Chris Mason4bef0842008-09-08 11:18:08 -04002179
Zheng Yan31840ae2008-09-23 13:14:14 -04002180 /* we can reuse a block if it hasn't been written
2181 * and it is from this transaction. We can't
2182 * reuse anything from the tree log root because
2183 * it has tiny sub-transactions.
2184 */
2185 if (btrfs_buffer_uptodate(buf, 0) &&
2186 btrfs_try_tree_lock(buf)) {
2187 u64 header_owner = btrfs_header_owner(buf);
2188 u64 header_transid = btrfs_header_generation(buf);
2189 if (header_owner != BTRFS_TREE_LOG_OBJECTID &&
Zheng Yan1a40e232008-09-26 10:09:34 -04002190 header_owner != BTRFS_TREE_RELOC_OBJECTID &&
Chris Mason56bec292009-03-13 10:10:06 -04002191 header_owner != BTRFS_DATA_RELOC_TREE_OBJECTID &&
Zheng Yan31840ae2008-09-23 13:14:14 -04002192 header_transid == trans->transid &&
2193 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
2194 clean_tree_block(NULL, root, buf);
2195 btrfs_tree_unlock(buf);
Chris Mason5f39d392007-10-15 16:14:19 -04002196 free_extent_buffer(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04002197 return 1;
Chris Mason8ef97622007-03-26 10:15:30 -04002198 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002199 btrfs_tree_unlock(buf);
Chris Masonf4b9aa82007-03-27 11:05:53 -04002200 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002201 free_extent_buffer(buf);
2202pinit:
2203 btrfs_update_pinned_extents(root, bytenr, num_bytes, 1);
2204
Chris Masonbe744172007-05-06 10:15:01 -04002205 BUG_ON(err < 0);
Chris Masone20d96d2007-03-22 12:13:20 -04002206 return 0;
2207}
2208
Chris Masona28ec192007-03-06 20:08:01 -05002209/*
2210 * remove an extent from the root, returns 0 on success
2211 */
Zheng Yan31840ae2008-09-23 13:14:14 -04002212static int __free_extent(struct btrfs_trans_handle *trans,
2213 struct btrfs_root *root,
2214 u64 bytenr, u64 num_bytes, u64 parent,
Chris Mason7bb86312007-12-11 09:25:06 -05002215 u64 root_objectid, u64 ref_generation,
Chris Mason56bec292009-03-13 10:10:06 -04002216 u64 owner_objectid, int pin, int mark_free,
2217 int refs_to_drop)
Chris Masona28ec192007-03-06 20:08:01 -05002218{
Chris Mason5caf2a02007-04-02 11:20:42 -04002219 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -04002220 struct btrfs_key key;
Chris Mason1261ec42007-03-20 20:35:03 -04002221 struct btrfs_fs_info *info = root->fs_info;
2222 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04002223 struct extent_buffer *leaf;
Chris Masona28ec192007-03-06 20:08:01 -05002224 int ret;
Chris Mason952fcca2008-02-18 16:33:44 -05002225 int extent_slot = 0;
2226 int found_extent = 0;
2227 int num_to_del = 1;
Chris Mason234b63a2007-03-13 10:46:10 -04002228 struct btrfs_extent_item *ei;
Chris Masoncf27e1e2007-03-13 09:49:06 -04002229 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -05002230
Chris Masondb945352007-10-15 16:15:53 -04002231 key.objectid = bytenr;
Chris Mason62e27492007-03-15 12:56:47 -04002232 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masondb945352007-10-15 16:15:53 -04002233 key.offset = num_bytes;
Chris Mason5caf2a02007-04-02 11:20:42 -04002234 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04002235 if (!path)
2236 return -ENOMEM;
2237
Chris Mason3c12ac72008-04-21 12:01:38 -04002238 path->reada = 1;
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002239 ret = lookup_extent_backref(trans, extent_root, path,
2240 bytenr, parent, root_objectid,
2241 ref_generation, owner_objectid, 1);
Chris Mason7bb86312007-12-11 09:25:06 -05002242 if (ret == 0) {
Chris Mason952fcca2008-02-18 16:33:44 -05002243 struct btrfs_key found_key;
2244 extent_slot = path->slots[0];
Chris Masond3977122009-01-05 21:25:51 -05002245 while (extent_slot > 0) {
Chris Mason952fcca2008-02-18 16:33:44 -05002246 extent_slot--;
2247 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2248 extent_slot);
2249 if (found_key.objectid != bytenr)
2250 break;
2251 if (found_key.type == BTRFS_EXTENT_ITEM_KEY &&
2252 found_key.offset == num_bytes) {
2253 found_extent = 1;
2254 break;
2255 }
2256 if (path->slots[0] - extent_slot > 5)
2257 break;
2258 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002259 if (!found_extent) {
Chris Mason56bec292009-03-13 10:10:06 -04002260 ret = remove_extent_backref(trans, extent_root, path,
2261 refs_to_drop);
Zheng Yan31840ae2008-09-23 13:14:14 -04002262 BUG_ON(ret);
2263 btrfs_release_path(extent_root, path);
2264 ret = btrfs_search_slot(trans, extent_root,
2265 &key, path, -1, 1);
Josef Bacikf3465ca2008-11-12 14:19:50 -05002266 if (ret) {
2267 printk(KERN_ERR "umm, got %d back from search"
Chris Masond3977122009-01-05 21:25:51 -05002268 ", was looking for %llu\n", ret,
2269 (unsigned long long)bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05002270 btrfs_print_leaf(extent_root, path->nodes[0]);
2271 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002272 BUG_ON(ret);
2273 extent_slot = path->slots[0];
2274 }
Chris Mason7bb86312007-12-11 09:25:06 -05002275 } else {
2276 btrfs_print_leaf(extent_root, path->nodes[0]);
2277 WARN_ON(1);
Chris Masond3977122009-01-05 21:25:51 -05002278 printk(KERN_ERR "btrfs unable to find ref byte nr %llu "
Chris Mason56bec292009-03-13 10:10:06 -04002279 "parent %llu root %llu gen %llu owner %llu\n",
Chris Masond3977122009-01-05 21:25:51 -05002280 (unsigned long long)bytenr,
Chris Mason56bec292009-03-13 10:10:06 -04002281 (unsigned long long)parent,
Chris Masond3977122009-01-05 21:25:51 -05002282 (unsigned long long)root_objectid,
2283 (unsigned long long)ref_generation,
2284 (unsigned long long)owner_objectid);
Chris Mason7bb86312007-12-11 09:25:06 -05002285 }
Chris Mason5f39d392007-10-15 16:14:19 -04002286
2287 leaf = path->nodes[0];
Chris Mason952fcca2008-02-18 16:33:44 -05002288 ei = btrfs_item_ptr(leaf, extent_slot,
Chris Mason123abc82007-03-14 14:14:43 -04002289 struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002290 refs = btrfs_extent_refs(leaf, ei);
Chris Mason952fcca2008-02-18 16:33:44 -05002291
Chris Mason56bec292009-03-13 10:10:06 -04002292 /*
2293 * we're not allowed to delete the extent item if there
2294 * are other delayed ref updates pending
2295 */
2296
2297 BUG_ON(refs < refs_to_drop);
2298 refs -= refs_to_drop;
2299 btrfs_set_extent_refs(leaf, ei, refs);
Chris Mason5f39d392007-10-15 16:14:19 -04002300 btrfs_mark_buffer_dirty(leaf);
2301
Chris Mason56bec292009-03-13 10:10:06 -04002302 if (refs == 0 && found_extent &&
2303 path->slots[0] == extent_slot + 1) {
Zheng Yan31840ae2008-09-23 13:14:14 -04002304 struct btrfs_extent_ref *ref;
2305 ref = btrfs_item_ptr(leaf, path->slots[0],
2306 struct btrfs_extent_ref);
Chris Mason56bec292009-03-13 10:10:06 -04002307 BUG_ON(btrfs_ref_num_refs(leaf, ref) != refs_to_drop);
Chris Mason952fcca2008-02-18 16:33:44 -05002308 /* if the back ref and the extent are next to each other
2309 * they get deleted below in one shot
2310 */
2311 path->slots[0] = extent_slot;
2312 num_to_del = 2;
2313 } else if (found_extent) {
2314 /* otherwise delete the extent back ref */
Chris Mason56bec292009-03-13 10:10:06 -04002315 ret = remove_extent_backref(trans, extent_root, path,
2316 refs_to_drop);
Chris Mason952fcca2008-02-18 16:33:44 -05002317 BUG_ON(ret);
2318 /* if refs are 0, we need to setup the path for deletion */
2319 if (refs == 0) {
2320 btrfs_release_path(extent_root, path);
2321 ret = btrfs_search_slot(trans, extent_root, &key, path,
2322 -1, 1);
Chris Mason952fcca2008-02-18 16:33:44 -05002323 BUG_ON(ret);
2324 }
2325 }
2326
Chris Masoncf27e1e2007-03-13 09:49:06 -04002327 if (refs == 0) {
Chris Masondb945352007-10-15 16:15:53 -04002328 u64 super_used;
2329 u64 root_used;
Chris Mason78fae272007-03-25 11:35:08 -04002330
2331 if (pin) {
Josef Bacik25179202008-10-29 14:49:05 -04002332 mutex_lock(&root->fs_info->pinned_mutex);
Zheng Yan31840ae2008-09-23 13:14:14 -04002333 ret = pin_down_bytes(trans, root, bytenr, num_bytes,
2334 owner_objectid >= BTRFS_FIRST_FREE_OBJECTID);
Josef Bacik25179202008-10-29 14:49:05 -04002335 mutex_unlock(&root->fs_info->pinned_mutex);
Yanc5492282007-11-06 10:25:25 -05002336 if (ret > 0)
2337 mark_free = 1;
2338 BUG_ON(ret < 0);
Chris Mason78fae272007-03-25 11:35:08 -04002339 }
Josef Bacik58176a92007-08-29 15:47:34 -04002340 /* block accounting for super block */
Chris Mason75eff682008-12-15 15:54:40 -05002341 spin_lock(&info->delalloc_lock);
Chris Masondb945352007-10-15 16:15:53 -04002342 super_used = btrfs_super_bytes_used(&info->super_copy);
2343 btrfs_set_super_bytes_used(&info->super_copy,
2344 super_used - num_bytes);
Josef Bacik58176a92007-08-29 15:47:34 -04002345
2346 /* block accounting for root item */
Chris Masondb945352007-10-15 16:15:53 -04002347 root_used = btrfs_root_used(&root->root_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002348 btrfs_set_root_used(&root->root_item,
Chris Masondb945352007-10-15 16:15:53 -04002349 root_used - num_bytes);
Yan Zheng34bf63c2008-12-19 10:58:46 -05002350 spin_unlock(&info->delalloc_lock);
Chris Mason952fcca2008-02-18 16:33:44 -05002351 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
2352 num_to_del);
Zheng Yan31840ae2008-09-23 13:14:14 -04002353 BUG_ON(ret);
Josef Bacik25179202008-10-29 14:49:05 -04002354 btrfs_release_path(extent_root, path);
David Woodhouse21af8042008-08-12 14:13:26 +01002355
Chris Mason459931e2008-12-10 09:10:46 -05002356 if (owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
2357 ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
2358 BUG_ON(ret);
2359 }
2360
Chris Masondcbdd4d2008-12-16 13:51:01 -05002361 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
2362 mark_free);
2363 BUG_ON(ret);
Chris Masona28ec192007-03-06 20:08:01 -05002364 }
Chris Mason5caf2a02007-04-02 11:20:42 -04002365 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -05002366 return ret;
2367}
2368
2369/*
Chris Masonfec577f2007-02-26 10:40:21 -05002370 * remove an extent from the root, returns 0 on success
2371 */
Chris Mason925baed2008-06-25 16:01:30 -04002372static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
Chris Mason56bec292009-03-13 10:10:06 -04002373 struct btrfs_root *root,
2374 u64 bytenr, u64 num_bytes, u64 parent,
2375 u64 root_objectid, u64 ref_generation,
2376 u64 owner_objectid, int pin,
2377 int refs_to_drop)
Chris Masonfec577f2007-02-26 10:40:21 -05002378{
Chris Masondb945352007-10-15 16:15:53 -04002379 WARN_ON(num_bytes < root->sectorsize);
Josef Bacikf3465ca2008-11-12 14:19:50 -05002380
Chris Mason56bec292009-03-13 10:10:06 -04002381 /*
2382 * if metadata always pin
2383 * if data pin when any transaction has committed this
2384 */
2385 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID ||
2386 ref_generation != trans->transid)
Chris Mason4bef0842008-09-08 11:18:08 -04002387 pin = 1;
2388
Chris Mason4bef0842008-09-08 11:18:08 -04002389 if (ref_generation != trans->transid)
2390 pin = 1;
2391
Chris Mason56bec292009-03-13 10:10:06 -04002392 return __free_extent(trans, root, bytenr, num_bytes, parent,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002393 root_objectid, ref_generation,
Chris Mason56bec292009-03-13 10:10:06 -04002394 owner_objectid, pin, pin == 0, refs_to_drop);
Chris Masonfec577f2007-02-26 10:40:21 -05002395}
2396
Chris Mason1887be62009-03-13 10:11:24 -04002397/*
2398 * when we free an extent, it is possible (and likely) that we free the last
2399 * delayed ref for that extent as well. This searches the delayed ref tree for
2400 * a given extent, and if there are no other delayed refs to be processed, it
2401 * removes it from the tree.
2402 */
2403static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
2404 struct btrfs_root *root, u64 bytenr)
2405{
2406 struct btrfs_delayed_ref_head *head;
2407 struct btrfs_delayed_ref_root *delayed_refs;
2408 struct btrfs_delayed_ref_node *ref;
2409 struct rb_node *node;
2410 int ret;
2411
2412 delayed_refs = &trans->transaction->delayed_refs;
2413 spin_lock(&delayed_refs->lock);
2414 head = btrfs_find_delayed_ref_head(trans, bytenr);
2415 if (!head)
2416 goto out;
2417
2418 node = rb_prev(&head->node.rb_node);
2419 if (!node)
2420 goto out;
2421
2422 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2423
2424 /* there are still entries for this ref, we can't drop it */
2425 if (ref->bytenr == bytenr)
2426 goto out;
2427
2428 /*
2429 * waiting for the lock here would deadlock. If someone else has it
2430 * locked they are already in the process of dropping it anyway
2431 */
2432 if (!mutex_trylock(&head->mutex))
2433 goto out;
2434
2435 /*
2436 * at this point we have a head with no other entries. Go
2437 * ahead and process it.
2438 */
2439 head->node.in_tree = 0;
2440 rb_erase(&head->node.rb_node, &delayed_refs->root);
Chris Masonc3e69d52009-03-13 10:17:05 -04002441
Chris Mason1887be62009-03-13 10:11:24 -04002442 delayed_refs->num_entries--;
2443
2444 /*
2445 * we don't take a ref on the node because we're removing it from the
2446 * tree, so we just steal the ref the tree was holding.
2447 */
Chris Masonc3e69d52009-03-13 10:17:05 -04002448 delayed_refs->num_heads--;
2449 if (list_empty(&head->cluster))
2450 delayed_refs->num_heads_ready--;
2451
2452 list_del_init(&head->cluster);
Chris Mason1887be62009-03-13 10:11:24 -04002453 spin_unlock(&delayed_refs->lock);
2454
2455 ret = run_one_delayed_ref(trans, root->fs_info->tree_root,
2456 &head->node, head->must_insert_reserved);
2457 BUG_ON(ret);
2458 btrfs_put_delayed_ref(&head->node);
2459 return 0;
2460out:
2461 spin_unlock(&delayed_refs->lock);
2462 return 0;
2463}
2464
Chris Mason925baed2008-06-25 16:01:30 -04002465int btrfs_free_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04002466 struct btrfs_root *root,
2467 u64 bytenr, u64 num_bytes, u64 parent,
2468 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002469 u64 owner_objectid, int pin)
Chris Mason925baed2008-06-25 16:01:30 -04002470{
2471 int ret;
2472
Chris Mason56bec292009-03-13 10:10:06 -04002473 /*
2474 * tree log blocks never actually go into the extent allocation
2475 * tree, just update pinning info and exit early.
2476 *
2477 * data extents referenced by the tree log do need to have
2478 * their reference counts bumped.
2479 */
2480 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID &&
2481 owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
2482 mutex_lock(&root->fs_info->pinned_mutex);
2483 btrfs_update_pinned_extents(root, bytenr, num_bytes, 1);
2484 mutex_unlock(&root->fs_info->pinned_mutex);
2485 update_reserved_extents(root, bytenr, num_bytes, 0);
2486 ret = 0;
2487 } else {
2488 ret = btrfs_add_delayed_ref(trans, bytenr, num_bytes, parent,
2489 root_objectid, ref_generation,
2490 owner_objectid,
2491 BTRFS_DROP_DELAYED_REF, 1);
Chris Mason1887be62009-03-13 10:11:24 -04002492 BUG_ON(ret);
2493 ret = check_ref_cleanup(trans, root, bytenr);
2494 BUG_ON(ret);
Chris Mason56bec292009-03-13 10:10:06 -04002495 }
Chris Mason925baed2008-06-25 16:01:30 -04002496 return ret;
2497}
2498
Chris Mason87ee04e2007-11-30 11:30:34 -05002499static u64 stripe_align(struct btrfs_root *root, u64 val)
2500{
2501 u64 mask = ((u64)root->stripesize - 1);
2502 u64 ret = (val + mask) & ~mask;
2503 return ret;
2504}
2505
Chris Masonfec577f2007-02-26 10:40:21 -05002506/*
2507 * walks the btree of allocated extents and find a hole of a given size.
2508 * The key ins is changed to record the hole:
2509 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -04002510 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -05002511 * ins->offset == number of blocks
2512 * Any available blocks before search_start are skipped.
2513 */
Chris Masond3977122009-01-05 21:25:51 -05002514static noinline int find_free_extent(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05002515 struct btrfs_root *orig_root,
2516 u64 num_bytes, u64 empty_size,
2517 u64 search_start, u64 search_end,
2518 u64 hint_byte, struct btrfs_key *ins,
2519 u64 exclude_start, u64 exclude_nr,
2520 int data)
Chris Masonfec577f2007-02-26 10:40:21 -05002521{
Josef Bacik80eb2342008-10-29 14:49:05 -04002522 int ret = 0;
Chris Masond3977122009-01-05 21:25:51 -05002523 struct btrfs_root *root = orig_root->fs_info->extent_root;
Chris Masondb945352007-10-15 16:15:53 -04002524 u64 total_needed = num_bytes;
Chris Mason239b14b2008-03-24 15:02:07 -04002525 u64 *last_ptr = NULL;
Chris Mason43662112008-11-07 09:06:11 -05002526 u64 last_wanted = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04002527 struct btrfs_block_group_cache *block_group = NULL;
Chris Mason0ef3e662008-05-24 14:04:53 -04002528 int chunk_alloc_done = 0;
Chris Mason239b14b2008-03-24 15:02:07 -04002529 int empty_cluster = 2 * 1024 * 1024;
Chris Mason0ef3e662008-05-24 14:04:53 -04002530 int allowed_chunk_alloc = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04002531 struct list_head *head = NULL, *cur = NULL;
2532 int loop = 0;
Chris Masonf5a31e12008-11-10 11:47:09 -05002533 int extra_loop = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04002534 struct btrfs_space_info *space_info;
Chris Masonfec577f2007-02-26 10:40:21 -05002535
Chris Masondb945352007-10-15 16:15:53 -04002536 WARN_ON(num_bytes < root->sectorsize);
Chris Masonb1a4d962007-04-04 15:27:52 -04002537 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
Josef Bacik80eb2342008-10-29 14:49:05 -04002538 ins->objectid = 0;
2539 ins->offset = 0;
Chris Masonb1a4d962007-04-04 15:27:52 -04002540
Chris Mason0ef3e662008-05-24 14:04:53 -04002541 if (orig_root->ref_cows || empty_size)
2542 allowed_chunk_alloc = 1;
2543
Chris Mason239b14b2008-03-24 15:02:07 -04002544 if (data & BTRFS_BLOCK_GROUP_METADATA) {
2545 last_ptr = &root->fs_info->last_alloc;
Chris Mason536ac8a2009-02-12 09:41:38 -05002546 if (!btrfs_test_opt(root, SSD))
2547 empty_cluster = 64 * 1024;
Chris Mason239b14b2008-03-24 15:02:07 -04002548 }
2549
Josef Bacik0f9dd462008-09-23 13:14:11 -04002550 if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD))
Chris Mason239b14b2008-03-24 15:02:07 -04002551 last_ptr = &root->fs_info->last_data_alloc;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002552
Chris Mason239b14b2008-03-24 15:02:07 -04002553 if (last_ptr) {
Chris Mason43662112008-11-07 09:06:11 -05002554 if (*last_ptr) {
Chris Mason239b14b2008-03-24 15:02:07 -04002555 hint_byte = *last_ptr;
Chris Mason43662112008-11-07 09:06:11 -05002556 last_wanted = *last_ptr;
2557 } else
Chris Mason239b14b2008-03-24 15:02:07 -04002558 empty_size += empty_cluster;
Chris Mason43662112008-11-07 09:06:11 -05002559 } else {
2560 empty_cluster = 0;
Chris Mason239b14b2008-03-24 15:02:07 -04002561 }
Chris Masona061fc82008-05-07 11:43:44 -04002562 search_start = max(search_start, first_logical_byte(root, 0));
Chris Mason239b14b2008-03-24 15:02:07 -04002563 search_start = max(search_start, hint_byte);
Chris Mason0b86a832008-03-24 15:01:56 -04002564
Chris Mason42e70e72008-11-07 18:17:11 -05002565 if (last_wanted && search_start != last_wanted) {
Chris Mason43662112008-11-07 09:06:11 -05002566 last_wanted = 0;
Chris Mason42e70e72008-11-07 18:17:11 -05002567 empty_size += empty_cluster;
2568 }
Chris Mason43662112008-11-07 09:06:11 -05002569
Chris Mason42e70e72008-11-07 18:17:11 -05002570 total_needed += empty_size;
Josef Bacik80eb2342008-10-29 14:49:05 -04002571 block_group = btrfs_lookup_block_group(root->fs_info, search_start);
Yan Zhengd899e052008-10-30 14:25:28 -04002572 if (!block_group)
2573 block_group = btrfs_lookup_first_block_group(root->fs_info,
2574 search_start);
Josef Bacik80eb2342008-10-29 14:49:05 -04002575 space_info = __find_space_info(root->fs_info, data);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002576
Josef Bacik80eb2342008-10-29 14:49:05 -04002577 down_read(&space_info->groups_sem);
2578 while (1) {
2579 struct btrfs_free_space *free_space;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002580 /*
Josef Bacik80eb2342008-10-29 14:49:05 -04002581 * the only way this happens if our hint points to a block
2582 * group thats not of the proper type, while looping this
2583 * should never happen
Josef Bacik0f9dd462008-09-23 13:14:11 -04002584 */
Chris Mason8a1413a22008-11-10 16:13:54 -05002585 if (empty_size)
2586 extra_loop = 1;
2587
Chris Mason42e70e72008-11-07 18:17:11 -05002588 if (!block_group)
2589 goto new_group_no_lock;
2590
Josef Bacikea6a4782008-11-20 12:16:16 -05002591 if (unlikely(!block_group->cached)) {
2592 mutex_lock(&block_group->cache_mutex);
2593 ret = cache_block_group(root, block_group);
2594 mutex_unlock(&block_group->cache_mutex);
2595 if (ret)
2596 break;
2597 }
2598
Josef Bacik25179202008-10-29 14:49:05 -04002599 mutex_lock(&block_group->alloc_mutex);
Josef Bacik80eb2342008-10-29 14:49:05 -04002600 if (unlikely(!block_group_bits(block_group, data)))
Josef Bacik0f9dd462008-09-23 13:14:11 -04002601 goto new_group;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002602
Josef Bacikea6a4782008-11-20 12:16:16 -05002603 if (unlikely(block_group->ro))
Josef Bacik80eb2342008-10-29 14:49:05 -04002604 goto new_group;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002605
Josef Bacik80eb2342008-10-29 14:49:05 -04002606 free_space = btrfs_find_free_space(block_group, search_start,
2607 total_needed);
2608 if (free_space) {
2609 u64 start = block_group->key.objectid;
2610 u64 end = block_group->key.objectid +
Josef Bacik0f9dd462008-09-23 13:14:11 -04002611 block_group->key.offset;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002612
Josef Bacik80eb2342008-10-29 14:49:05 -04002613 search_start = stripe_align(root, free_space->offset);
Chris Mason0b86a832008-03-24 15:01:56 -04002614
Josef Bacik80eb2342008-10-29 14:49:05 -04002615 /* move on to the next group */
2616 if (search_start + num_bytes >= search_end)
2617 goto new_group;
Chris Masone37c9e62007-05-09 20:13:14 -04002618
Josef Bacik80eb2342008-10-29 14:49:05 -04002619 /* move on to the next group */
2620 if (search_start + num_bytes > end)
2621 goto new_group;
2622
Chris Mason43662112008-11-07 09:06:11 -05002623 if (last_wanted && search_start != last_wanted) {
Chris Mason3b7885b2008-11-06 21:48:27 -05002624 total_needed += empty_cluster;
Chris Mason8a1413a22008-11-10 16:13:54 -05002625 empty_size += empty_cluster;
Chris Mason43662112008-11-07 09:06:11 -05002626 last_wanted = 0;
Chris Mason3b7885b2008-11-06 21:48:27 -05002627 /*
2628 * if search_start is still in this block group
2629 * then we just re-search this block group
2630 */
2631 if (search_start >= start &&
2632 search_start < end) {
2633 mutex_unlock(&block_group->alloc_mutex);
2634 continue;
2635 }
2636
2637 /* else we go to the next block group */
2638 goto new_group;
2639 }
2640
Josef Bacik80eb2342008-10-29 14:49:05 -04002641 if (exclude_nr > 0 &&
2642 (search_start + num_bytes > exclude_start &&
2643 search_start < exclude_start + exclude_nr)) {
2644 search_start = exclude_start + exclude_nr;
2645 /*
2646 * if search_start is still in this block group
2647 * then we just re-search this block group
2648 */
2649 if (search_start >= start &&
Josef Bacik25179202008-10-29 14:49:05 -04002650 search_start < end) {
2651 mutex_unlock(&block_group->alloc_mutex);
Chris Mason43662112008-11-07 09:06:11 -05002652 last_wanted = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04002653 continue;
Josef Bacik25179202008-10-29 14:49:05 -04002654 }
Josef Bacik80eb2342008-10-29 14:49:05 -04002655
2656 /* else we go to the next block group */
2657 goto new_group;
2658 }
2659
2660 ins->objectid = search_start;
2661 ins->offset = num_bytes;
Josef Bacik25179202008-10-29 14:49:05 -04002662
2663 btrfs_remove_free_space_lock(block_group, search_start,
2664 num_bytes);
Josef Bacik80eb2342008-10-29 14:49:05 -04002665 /* we are all good, lets return */
Josef Bacik25179202008-10-29 14:49:05 -04002666 mutex_unlock(&block_group->alloc_mutex);
Josef Bacik80eb2342008-10-29 14:49:05 -04002667 break;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002668 }
Josef Bacik80eb2342008-10-29 14:49:05 -04002669new_group:
Chris Mason42e70e72008-11-07 18:17:11 -05002670 mutex_unlock(&block_group->alloc_mutex);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002671 put_block_group(block_group);
2672 block_group = NULL;
Chris Mason42e70e72008-11-07 18:17:11 -05002673new_group_no_lock:
Chris Masonf5a31e12008-11-10 11:47:09 -05002674 /* don't try to compare new allocations against the
2675 * last allocation any more
2676 */
Chris Mason43662112008-11-07 09:06:11 -05002677 last_wanted = 0;
Chris Masonf5a31e12008-11-10 11:47:09 -05002678
Josef Bacik80eb2342008-10-29 14:49:05 -04002679 /*
2680 * Here's how this works.
2681 * loop == 0: we were searching a block group via a hint
2682 * and didn't find anything, so we start at
2683 * the head of the block groups and keep searching
2684 * loop == 1: we're searching through all of the block groups
2685 * if we hit the head again we have searched
2686 * all of the block groups for this space and we
2687 * need to try and allocate, if we cant error out.
2688 * loop == 2: we allocated more space and are looping through
2689 * all of the block groups again.
2690 */
2691 if (loop == 0) {
2692 head = &space_info->block_groups;
2693 cur = head->next;
Josef Bacik80eb2342008-10-29 14:49:05 -04002694 loop++;
2695 } else if (loop == 1 && cur == head) {
Chris Masonf5a31e12008-11-10 11:47:09 -05002696 int keep_going;
Chris Mason42e70e72008-11-07 18:17:11 -05002697
Chris Masonf5a31e12008-11-10 11:47:09 -05002698 /* at this point we give up on the empty_size
2699 * allocations and just try to allocate the min
2700 * space.
2701 *
2702 * The extra_loop field was set if an empty_size
2703 * allocation was attempted above, and if this
2704 * is try we need to try the loop again without
2705 * the additional empty_size.
2706 */
Chris Mason5b7c3fc2008-11-10 07:26:33 -05002707 total_needed -= empty_size;
2708 empty_size = 0;
Chris Masonf5a31e12008-11-10 11:47:09 -05002709 keep_going = extra_loop;
2710 loop++;
Chris Mason42e70e72008-11-07 18:17:11 -05002711
Josef Bacik80eb2342008-10-29 14:49:05 -04002712 if (allowed_chunk_alloc && !chunk_alloc_done) {
2713 up_read(&space_info->groups_sem);
2714 ret = do_chunk_alloc(trans, root, num_bytes +
2715 2 * 1024 * 1024, data, 1);
Josef Bacik80eb2342008-10-29 14:49:05 -04002716 down_read(&space_info->groups_sem);
Chris Mason2ed6d662008-11-13 09:59:33 -05002717 if (ret < 0)
2718 goto loop_check;
Josef Bacik80eb2342008-10-29 14:49:05 -04002719 head = &space_info->block_groups;
Chris Masonf5a31e12008-11-10 11:47:09 -05002720 /*
2721 * we've allocated a new chunk, keep
2722 * trying
2723 */
2724 keep_going = 1;
Josef Bacik80eb2342008-10-29 14:49:05 -04002725 chunk_alloc_done = 1;
2726 } else if (!allowed_chunk_alloc) {
2727 space_info->force_alloc = 1;
Chris Masonf5a31e12008-11-10 11:47:09 -05002728 }
Chris Mason2ed6d662008-11-13 09:59:33 -05002729loop_check:
Chris Masonf5a31e12008-11-10 11:47:09 -05002730 if (keep_going) {
2731 cur = head->next;
2732 extra_loop = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04002733 } else {
2734 break;
2735 }
2736 } else if (cur == head) {
2737 break;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002738 }
Josef Bacik80eb2342008-10-29 14:49:05 -04002739
2740 block_group = list_entry(cur, struct btrfs_block_group_cache,
2741 list);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002742 atomic_inc(&block_group->count);
2743
Josef Bacik80eb2342008-10-29 14:49:05 -04002744 search_start = block_group->key.objectid;
2745 cur = cur->next;
Chris Masone19caa52007-10-15 16:17:44 -04002746 }
Chris Mason0b86a832008-03-24 15:01:56 -04002747
Josef Bacik80eb2342008-10-29 14:49:05 -04002748 /* we found what we needed */
2749 if (ins->objectid) {
2750 if (!(data & BTRFS_BLOCK_GROUP_DATA))
Yan Zhengd2fb3432008-12-11 16:30:39 -05002751 trans->block_group = block_group->key.objectid;
Josef Bacik80eb2342008-10-29 14:49:05 -04002752
2753 if (last_ptr)
2754 *last_ptr = ins->objectid + ins->offset;
2755 ret = 0;
2756 } else if (!ret) {
Chris Masond3977122009-01-05 21:25:51 -05002757 printk(KERN_ERR "btrfs searching for %llu bytes, "
2758 "num_bytes %llu, loop %d, allowed_alloc %d\n",
2759 (unsigned long long)total_needed,
2760 (unsigned long long)num_bytes,
Josef Bacik4ce4cb52008-11-17 21:12:00 -05002761 loop, allowed_chunk_alloc);
Josef Bacik80eb2342008-10-29 14:49:05 -04002762 ret = -ENOSPC;
Chris Masonf2654de2007-06-26 12:20:46 -04002763 }
Yan Zhengd2fb3432008-12-11 16:30:39 -05002764 if (block_group)
2765 put_block_group(block_group);
Chris Mason0b86a832008-03-24 15:01:56 -04002766
Josef Bacik80eb2342008-10-29 14:49:05 -04002767 up_read(&space_info->groups_sem);
Chris Mason0f70abe2007-02-28 16:46:22 -05002768 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05002769}
Chris Masonec44a352008-04-28 15:29:52 -04002770
Josef Bacik0f9dd462008-09-23 13:14:11 -04002771static void dump_space_info(struct btrfs_space_info *info, u64 bytes)
2772{
2773 struct btrfs_block_group_cache *cache;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002774
Chris Masond3977122009-01-05 21:25:51 -05002775 printk(KERN_INFO "space_info has %llu free, is %sfull\n",
2776 (unsigned long long)(info->total_bytes - info->bytes_used -
2777 info->bytes_pinned - info->bytes_reserved),
2778 (info->full) ? "" : "not ");
Josef Bacik6a632092009-02-20 11:00:09 -05002779 printk(KERN_INFO "space_info total=%llu, pinned=%llu, delalloc=%llu,"
2780 " may_use=%llu, used=%llu\n", info->total_bytes,
2781 info->bytes_pinned, info->bytes_delalloc, info->bytes_may_use,
2782 info->bytes_used);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002783
Josef Bacik80eb2342008-10-29 14:49:05 -04002784 down_read(&info->groups_sem);
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002785 list_for_each_entry(cache, &info->block_groups, list) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04002786 spin_lock(&cache->lock);
Chris Masond3977122009-01-05 21:25:51 -05002787 printk(KERN_INFO "block group %llu has %llu bytes, %llu used "
2788 "%llu pinned %llu reserved\n",
2789 (unsigned long long)cache->key.objectid,
2790 (unsigned long long)cache->key.offset,
2791 (unsigned long long)btrfs_block_group_used(&cache->item),
2792 (unsigned long long)cache->pinned,
2793 (unsigned long long)cache->reserved);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002794 btrfs_dump_free_space(cache, bytes);
2795 spin_unlock(&cache->lock);
2796 }
Josef Bacik80eb2342008-10-29 14:49:05 -04002797 up_read(&info->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002798}
Zheng Yane8569812008-09-26 10:05:48 -04002799
Chris Masone6dcd2d2008-07-17 12:53:50 -04002800static int __btrfs_reserve_extent(struct btrfs_trans_handle *trans,
2801 struct btrfs_root *root,
2802 u64 num_bytes, u64 min_alloc_size,
2803 u64 empty_size, u64 hint_byte,
2804 u64 search_end, struct btrfs_key *ins,
2805 u64 data)
Chris Masonfec577f2007-02-26 10:40:21 -05002806{
2807 int ret;
Chris Masonfbdc7622007-05-30 10:22:12 -04002808 u64 search_start = 0;
Chris Mason1261ec42007-03-20 20:35:03 -04002809 struct btrfs_fs_info *info = root->fs_info;
Chris Mason925baed2008-06-25 16:01:30 -04002810
Josef Bacik6a632092009-02-20 11:00:09 -05002811 data = btrfs_get_alloc_profile(root, data);
Chris Mason98d20f62008-04-14 09:46:10 -04002812again:
Chris Mason0ef3e662008-05-24 14:04:53 -04002813 /*
2814 * the only place that sets empty_size is btrfs_realloc_node, which
2815 * is not called recursively on allocations
2816 */
2817 if (empty_size || root->ref_cows) {
Chris Mason593060d2008-03-25 16:50:33 -04002818 if (!(data & BTRFS_BLOCK_GROUP_METADATA)) {
Chris Mason6324fbf2008-03-24 15:01:59 -04002819 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0ef3e662008-05-24 14:04:53 -04002820 2 * 1024 * 1024,
2821 BTRFS_BLOCK_GROUP_METADATA |
2822 (info->metadata_alloc_profile &
2823 info->avail_metadata_alloc_bits), 0);
Chris Mason6324fbf2008-03-24 15:01:59 -04002824 }
2825 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0ef3e662008-05-24 14:04:53 -04002826 num_bytes + 2 * 1024 * 1024, data, 0);
Chris Mason6324fbf2008-03-24 15:01:59 -04002827 }
Chris Mason0b86a832008-03-24 15:01:56 -04002828
Chris Masondb945352007-10-15 16:15:53 -04002829 WARN_ON(num_bytes < root->sectorsize);
2830 ret = find_free_extent(trans, root, num_bytes, empty_size,
2831 search_start, search_end, hint_byte, ins,
Chris Mason26b80032007-08-08 20:17:12 -04002832 trans->alloc_exclude_start,
2833 trans->alloc_exclude_nr, data);
Chris Mason3b951512008-04-17 11:29:12 -04002834
Chris Mason98d20f62008-04-14 09:46:10 -04002835 if (ret == -ENOSPC && num_bytes > min_alloc_size) {
2836 num_bytes = num_bytes >> 1;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002837 num_bytes = num_bytes & ~(root->sectorsize - 1);
Chris Mason98d20f62008-04-14 09:46:10 -04002838 num_bytes = max(num_bytes, min_alloc_size);
Chris Mason0ef3e662008-05-24 14:04:53 -04002839 do_chunk_alloc(trans, root->fs_info->extent_root,
2840 num_bytes, data, 1);
Chris Mason98d20f62008-04-14 09:46:10 -04002841 goto again;
2842 }
Chris Masonec44a352008-04-28 15:29:52 -04002843 if (ret) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04002844 struct btrfs_space_info *sinfo;
2845
2846 sinfo = __find_space_info(root->fs_info, data);
Chris Masond3977122009-01-05 21:25:51 -05002847 printk(KERN_ERR "btrfs allocation failed flags %llu, "
2848 "wanted %llu\n", (unsigned long long)data,
2849 (unsigned long long)num_bytes);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002850 dump_space_info(sinfo, num_bytes);
Chris Mason925baed2008-06-25 16:01:30 -04002851 BUG();
Chris Mason925baed2008-06-25 16:01:30 -04002852 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002853
2854 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002855}
2856
Chris Mason65b51a02008-08-01 15:11:20 -04002857int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len)
2858{
Josef Bacik0f9dd462008-09-23 13:14:11 -04002859 struct btrfs_block_group_cache *cache;
Liu Hui1f3c79a2009-01-05 15:57:51 -05002860 int ret = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002861
Josef Bacik0f9dd462008-09-23 13:14:11 -04002862 cache = btrfs_lookup_block_group(root->fs_info, start);
2863 if (!cache) {
Chris Masond3977122009-01-05 21:25:51 -05002864 printk(KERN_ERR "Unable to find block group for %llu\n",
2865 (unsigned long long)start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002866 return -ENOSPC;
2867 }
Liu Hui1f3c79a2009-01-05 15:57:51 -05002868
2869 ret = btrfs_discard_extent(root, start, len);
2870
Josef Bacik0f9dd462008-09-23 13:14:11 -04002871 btrfs_add_free_space(cache, start, len);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002872 put_block_group(cache);
Zheng Yan1a40e232008-09-26 10:09:34 -04002873 update_reserved_extents(root, start, len, 0);
Liu Hui1f3c79a2009-01-05 15:57:51 -05002874
2875 return ret;
Chris Mason65b51a02008-08-01 15:11:20 -04002876}
2877
Chris Masone6dcd2d2008-07-17 12:53:50 -04002878int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
2879 struct btrfs_root *root,
2880 u64 num_bytes, u64 min_alloc_size,
2881 u64 empty_size, u64 hint_byte,
2882 u64 search_end, struct btrfs_key *ins,
2883 u64 data)
2884{
2885 int ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002886 ret = __btrfs_reserve_extent(trans, root, num_bytes, min_alloc_size,
2887 empty_size, hint_byte, search_end, ins,
2888 data);
Zheng Yane8569812008-09-26 10:05:48 -04002889 update_reserved_extents(root, ins->objectid, ins->offset, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002890 return ret;
2891}
2892
2893static int __btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04002894 struct btrfs_root *root, u64 parent,
Chris Masone6dcd2d2008-07-17 12:53:50 -04002895 u64 root_objectid, u64 ref_generation,
Chris Mason56bec292009-03-13 10:10:06 -04002896 u64 owner, struct btrfs_key *ins,
2897 int ref_mod)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002898{
2899 int ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002900 u64 super_used;
2901 u64 root_used;
2902 u64 num_bytes = ins->offset;
2903 u32 sizes[2];
2904 struct btrfs_fs_info *info = root->fs_info;
2905 struct btrfs_root *extent_root = info->extent_root;
2906 struct btrfs_extent_item *extent_item;
2907 struct btrfs_extent_ref *ref;
2908 struct btrfs_path *path;
2909 struct btrfs_key keys[2];
Chris Masonf2654de2007-06-26 12:20:46 -04002910
Zheng Yan31840ae2008-09-23 13:14:14 -04002911 if (parent == 0)
2912 parent = ins->objectid;
2913
Josef Bacik58176a92007-08-29 15:47:34 -04002914 /* block accounting for super block */
Chris Mason75eff682008-12-15 15:54:40 -05002915 spin_lock(&info->delalloc_lock);
Chris Masondb945352007-10-15 16:15:53 -04002916 super_used = btrfs_super_bytes_used(&info->super_copy);
2917 btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes);
Chris Mason26b80032007-08-08 20:17:12 -04002918
Josef Bacik58176a92007-08-29 15:47:34 -04002919 /* block accounting for root item */
Chris Masondb945352007-10-15 16:15:53 -04002920 root_used = btrfs_root_used(&root->root_item);
2921 btrfs_set_root_used(&root->root_item, root_used + num_bytes);
Yan Zheng34bf63c2008-12-19 10:58:46 -05002922 spin_unlock(&info->delalloc_lock);
Josef Bacik58176a92007-08-29 15:47:34 -04002923
Chris Mason47e4bb92008-02-01 14:51:59 -05002924 memcpy(&keys[0], ins, sizeof(*ins));
Chris Mason47e4bb92008-02-01 14:51:59 -05002925 keys[1].objectid = ins->objectid;
2926 keys[1].type = BTRFS_EXTENT_REF_KEY;
Zheng Yan31840ae2008-09-23 13:14:14 -04002927 keys[1].offset = parent;
Chris Mason47e4bb92008-02-01 14:51:59 -05002928 sizes[0] = sizeof(*extent_item);
2929 sizes[1] = sizeof(*ref);
Chris Mason7bb86312007-12-11 09:25:06 -05002930
2931 path = btrfs_alloc_path();
2932 BUG_ON(!path);
Chris Mason47e4bb92008-02-01 14:51:59 -05002933
2934 ret = btrfs_insert_empty_items(trans, extent_root, path, keys,
2935 sizes, 2);
Chris Masonccd467d2007-06-28 15:57:36 -04002936 BUG_ON(ret);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002937
Chris Mason47e4bb92008-02-01 14:51:59 -05002938 extent_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2939 struct btrfs_extent_item);
Chris Mason56bec292009-03-13 10:10:06 -04002940 btrfs_set_extent_refs(path->nodes[0], extent_item, ref_mod);
Chris Mason47e4bb92008-02-01 14:51:59 -05002941 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
2942 struct btrfs_extent_ref);
2943
2944 btrfs_set_ref_root(path->nodes[0], ref, root_objectid);
2945 btrfs_set_ref_generation(path->nodes[0], ref, ref_generation);
2946 btrfs_set_ref_objectid(path->nodes[0], ref, owner);
Chris Mason56bec292009-03-13 10:10:06 -04002947 btrfs_set_ref_num_refs(path->nodes[0], ref, ref_mod);
Chris Mason47e4bb92008-02-01 14:51:59 -05002948
2949 btrfs_mark_buffer_dirty(path->nodes[0]);
2950
2951 trans->alloc_exclude_start = 0;
2952 trans->alloc_exclude_nr = 0;
Chris Mason7bb86312007-12-11 09:25:06 -05002953 btrfs_free_path(path);
Chris Masonf510cfe2007-10-15 16:14:48 -04002954
Chris Mason925baed2008-06-25 16:01:30 -04002955 if (ret)
2956 goto out;
Chris Mason26b80032007-08-08 20:17:12 -04002957
Chris Masond3977122009-01-05 21:25:51 -05002958 ret = update_block_group(trans, root, ins->objectid,
2959 ins->offset, 1, 0);
Chris Masonf5947062008-02-04 10:10:13 -05002960 if (ret) {
Chris Masond3977122009-01-05 21:25:51 -05002961 printk(KERN_ERR "btrfs update block group failed for %llu "
2962 "%llu\n", (unsigned long long)ins->objectid,
2963 (unsigned long long)ins->offset);
Chris Masonf5947062008-02-04 10:10:13 -05002964 BUG();
2965 }
Chris Mason925baed2008-06-25 16:01:30 -04002966out:
Chris Masone6dcd2d2008-07-17 12:53:50 -04002967 return ret;
2968}
2969
2970int btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04002971 struct btrfs_root *root, u64 parent,
Chris Masone6dcd2d2008-07-17 12:53:50 -04002972 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002973 u64 owner, struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002974{
2975 int ret;
Chris Mason1c2308f82008-09-23 13:14:13 -04002976
2977 if (root_objectid == BTRFS_TREE_LOG_OBJECTID)
2978 return 0;
Chris Mason56bec292009-03-13 10:10:06 -04002979
2980 ret = btrfs_add_delayed_ref(trans, ins->objectid,
2981 ins->offset, parent, root_objectid,
2982 ref_generation, owner,
2983 BTRFS_ADD_DELAYED_EXTENT, 0);
2984 BUG_ON(ret);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002985 return ret;
2986}
Chris Masone02119d2008-09-05 16:13:11 -04002987
2988/*
2989 * this is used by the tree logging recovery code. It records that
2990 * an extent has been allocated and makes sure to clear the free
2991 * space cache bits as well
2992 */
2993int btrfs_alloc_logged_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04002994 struct btrfs_root *root, u64 parent,
Chris Masone02119d2008-09-05 16:13:11 -04002995 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002996 u64 owner, struct btrfs_key *ins)
Chris Masone02119d2008-09-05 16:13:11 -04002997{
2998 int ret;
2999 struct btrfs_block_group_cache *block_group;
3000
Chris Masone02119d2008-09-05 16:13:11 -04003001 block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
Josef Bacikea6a4782008-11-20 12:16:16 -05003002 mutex_lock(&block_group->cache_mutex);
Chris Masone02119d2008-09-05 16:13:11 -04003003 cache_block_group(root, block_group);
Josef Bacikea6a4782008-11-20 12:16:16 -05003004 mutex_unlock(&block_group->cache_mutex);
Chris Masone02119d2008-09-05 16:13:11 -04003005
Josef Bacikea6a4782008-11-20 12:16:16 -05003006 ret = btrfs_remove_free_space(block_group, ins->objectid,
3007 ins->offset);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003008 BUG_ON(ret);
Yan Zhengd2fb3432008-12-11 16:30:39 -05003009 put_block_group(block_group);
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003010 ret = __btrfs_alloc_reserved_extent(trans, root, parent, root_objectid,
Chris Mason56bec292009-03-13 10:10:06 -04003011 ref_generation, owner, ins, 1);
Chris Masone02119d2008-09-05 16:13:11 -04003012 return ret;
3013}
3014
Chris Masone6dcd2d2008-07-17 12:53:50 -04003015/*
3016 * finds a free extent and does all the dirty work required for allocation
3017 * returns the key for the extent through ins, and a tree buffer for
3018 * the first block of the extent through buf.
3019 *
3020 * returns 0 if everything worked, non-zero otherwise.
3021 */
3022int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
3023 struct btrfs_root *root,
Zheng Yan31840ae2008-09-23 13:14:14 -04003024 u64 num_bytes, u64 parent, u64 min_alloc_size,
Chris Masone6dcd2d2008-07-17 12:53:50 -04003025 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003026 u64 owner_objectid, u64 empty_size, u64 hint_byte,
Chris Masone6dcd2d2008-07-17 12:53:50 -04003027 u64 search_end, struct btrfs_key *ins, u64 data)
3028{
3029 int ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003030 ret = __btrfs_reserve_extent(trans, root, num_bytes,
3031 min_alloc_size, empty_size, hint_byte,
3032 search_end, ins, data);
3033 BUG_ON(ret);
Chris Masond00aff02008-09-11 15:54:42 -04003034 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
Chris Mason56bec292009-03-13 10:10:06 -04003035 ret = btrfs_add_delayed_ref(trans, ins->objectid,
3036 ins->offset, parent, root_objectid,
3037 ref_generation, owner_objectid,
3038 BTRFS_ADD_DELAYED_EXTENT, 0);
Chris Masond00aff02008-09-11 15:54:42 -04003039 BUG_ON(ret);
Chris Masond00aff02008-09-11 15:54:42 -04003040 }
Chris Mason56bec292009-03-13 10:10:06 -04003041 update_reserved_extents(root, ins->objectid, ins->offset, 1);
Chris Mason925baed2008-06-25 16:01:30 -04003042 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05003043}
Chris Mason65b51a02008-08-01 15:11:20 -04003044
3045struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
3046 struct btrfs_root *root,
Chris Mason4008c042009-02-12 14:09:45 -05003047 u64 bytenr, u32 blocksize,
3048 int level)
Chris Mason65b51a02008-08-01 15:11:20 -04003049{
3050 struct extent_buffer *buf;
3051
3052 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
3053 if (!buf)
3054 return ERR_PTR(-ENOMEM);
3055 btrfs_set_header_generation(buf, trans->transid);
Chris Mason4008c042009-02-12 14:09:45 -05003056 btrfs_set_buffer_lockdep_class(buf, level);
Chris Mason65b51a02008-08-01 15:11:20 -04003057 btrfs_tree_lock(buf);
3058 clean_tree_block(trans, root, buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05003059
3060 btrfs_set_lock_blocking(buf);
Chris Mason65b51a02008-08-01 15:11:20 -04003061 btrfs_set_buffer_uptodate(buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05003062
Chris Masond0c803c2008-09-11 16:17:57 -04003063 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
3064 set_extent_dirty(&root->dirty_log_pages, buf->start,
Chris Mason65b51a02008-08-01 15:11:20 -04003065 buf->start + buf->len - 1, GFP_NOFS);
Chris Masond0c803c2008-09-11 16:17:57 -04003066 } else {
3067 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
3068 buf->start + buf->len - 1, GFP_NOFS);
3069 }
Chris Mason65b51a02008-08-01 15:11:20 -04003070 trans->blocks_used++;
Chris Masonb4ce94d2009-02-04 09:25:08 -05003071 /* this returns a buffer locked for blocking */
Chris Mason65b51a02008-08-01 15:11:20 -04003072 return buf;
3073}
3074
Chris Masonfec577f2007-02-26 10:40:21 -05003075/*
3076 * helper function to allocate a block for a given tree
3077 * returns the tree buffer or NULL.
3078 */
Chris Mason5f39d392007-10-15 16:14:19 -04003079struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Chris Masondb945352007-10-15 16:15:53 -04003080 struct btrfs_root *root,
Zheng Yan31840ae2008-09-23 13:14:14 -04003081 u32 blocksize, u64 parent,
Chris Mason7bb86312007-12-11 09:25:06 -05003082 u64 root_objectid,
3083 u64 ref_generation,
Chris Mason7bb86312007-12-11 09:25:06 -05003084 int level,
3085 u64 hint,
Chris Mason5f39d392007-10-15 16:14:19 -04003086 u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05003087{
Chris Masone2fa7222007-03-12 16:22:34 -04003088 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -05003089 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04003090 struct extent_buffer *buf;
Chris Masonfec577f2007-02-26 10:40:21 -05003091
Zheng Yan31840ae2008-09-23 13:14:14 -04003092 ret = btrfs_alloc_extent(trans, root, blocksize, parent, blocksize,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003093 root_objectid, ref_generation, level,
Zheng Yan31840ae2008-09-23 13:14:14 -04003094 empty_size, hint, (u64)-1, &ins, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05003095 if (ret) {
Chris Mason54aa1f42007-06-22 14:16:25 -04003096 BUG_ON(ret > 0);
3097 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05003098 }
Chris Mason55c69072008-01-09 15:55:33 -05003099
Chris Mason4008c042009-02-12 14:09:45 -05003100 buf = btrfs_init_new_buffer(trans, root, ins.objectid,
3101 blocksize, level);
Chris Masonfec577f2007-02-26 10:40:21 -05003102 return buf;
3103}
Chris Masona28ec192007-03-06 20:08:01 -05003104
Chris Masone02119d2008-09-05 16:13:11 -04003105int btrfs_drop_leaf_ref(struct btrfs_trans_handle *trans,
3106 struct btrfs_root *root, struct extent_buffer *leaf)
Chris Mason6407bf62007-03-27 06:33:00 -04003107{
Chris Mason7bb86312007-12-11 09:25:06 -05003108 u64 leaf_owner;
3109 u64 leaf_generation;
Chris Masonbd56b302009-02-04 09:27:02 -05003110 struct refsort *sorted;
Chris Mason5f39d392007-10-15 16:14:19 -04003111 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -04003112 struct btrfs_file_extent_item *fi;
3113 int i;
3114 int nritems;
3115 int ret;
Chris Masonbd56b302009-02-04 09:27:02 -05003116 int refi = 0;
3117 int slot;
Chris Mason6407bf62007-03-27 06:33:00 -04003118
Chris Mason5f39d392007-10-15 16:14:19 -04003119 BUG_ON(!btrfs_is_leaf(leaf));
3120 nritems = btrfs_header_nritems(leaf);
Chris Mason7bb86312007-12-11 09:25:06 -05003121 leaf_owner = btrfs_header_owner(leaf);
3122 leaf_generation = btrfs_header_generation(leaf);
3123
Chris Masonbd56b302009-02-04 09:27:02 -05003124 sorted = kmalloc(sizeof(*sorted) * nritems, GFP_NOFS);
3125 /* we do this loop twice. The first time we build a list
3126 * of the extents we have a reference on, then we sort the list
3127 * by bytenr. The second time around we actually do the
3128 * extent freeing.
3129 */
Chris Mason6407bf62007-03-27 06:33:00 -04003130 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04003131 u64 disk_bytenr;
Chris Masone34a5b42008-07-22 12:08:37 -04003132 cond_resched();
Chris Mason5f39d392007-10-15 16:14:19 -04003133
3134 btrfs_item_key_to_cpu(leaf, &key, i);
Chris Masonbd56b302009-02-04 09:27:02 -05003135
3136 /* only extents have references, skip everything else */
Chris Mason5f39d392007-10-15 16:14:19 -04003137 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason6407bf62007-03-27 06:33:00 -04003138 continue;
Chris Masonbd56b302009-02-04 09:27:02 -05003139
Chris Mason6407bf62007-03-27 06:33:00 -04003140 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
Chris Masonbd56b302009-02-04 09:27:02 -05003141
3142 /* inline extents live in the btree, they don't have refs */
Chris Mason5f39d392007-10-15 16:14:19 -04003143 if (btrfs_file_extent_type(leaf, fi) ==
3144 BTRFS_FILE_EXTENT_INLINE)
Chris Mason236454d2007-04-19 13:37:44 -04003145 continue;
Chris Masonbd56b302009-02-04 09:27:02 -05003146
Chris Masondb945352007-10-15 16:15:53 -04003147 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Chris Masonbd56b302009-02-04 09:27:02 -05003148
3149 /* holes don't have refs */
Chris Masondb945352007-10-15 16:15:53 -04003150 if (disk_bytenr == 0)
Chris Mason3a686372007-05-24 13:35:57 -04003151 continue;
Chris Mason4a096752008-07-21 10:29:44 -04003152
Chris Masonbd56b302009-02-04 09:27:02 -05003153 sorted[refi].bytenr = disk_bytenr;
3154 sorted[refi].slot = i;
3155 refi++;
3156 }
3157
3158 if (refi == 0)
3159 goto out;
3160
3161 sort(sorted, refi, sizeof(struct refsort), refsort_cmp, NULL);
3162
3163 for (i = 0; i < refi; i++) {
3164 u64 disk_bytenr;
3165
3166 disk_bytenr = sorted[i].bytenr;
3167 slot = sorted[i].slot;
3168
3169 cond_resched();
3170
3171 btrfs_item_key_to_cpu(leaf, &key, slot);
3172 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
3173 continue;
3174
3175 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
3176
Chris Mason56bec292009-03-13 10:10:06 -04003177 ret = btrfs_free_extent(trans, root, disk_bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -05003178 btrfs_file_extent_disk_num_bytes(leaf, fi),
Zheng Yan31840ae2008-09-23 13:14:14 -04003179 leaf->start, leaf_owner, leaf_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003180 key.objectid, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04003181 BUG_ON(ret);
Chris Mason2dd3e672008-08-04 08:20:15 -04003182
3183 atomic_inc(&root->fs_info->throttle_gen);
3184 wake_up(&root->fs_info->transaction_throttle);
3185 cond_resched();
Chris Mason6407bf62007-03-27 06:33:00 -04003186 }
Chris Masonbd56b302009-02-04 09:27:02 -05003187out:
3188 kfree(sorted);
Chris Mason6407bf62007-03-27 06:33:00 -04003189 return 0;
3190}
3191
Chris Masond3977122009-01-05 21:25:51 -05003192static noinline int cache_drop_leaf_ref(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04003193 struct btrfs_root *root,
3194 struct btrfs_leaf_ref *ref)
Yan Zheng31153d82008-07-28 15:32:19 -04003195{
3196 int i;
3197 int ret;
Chris Masonbd56b302009-02-04 09:27:02 -05003198 struct btrfs_extent_info *info;
3199 struct refsort *sorted;
Yan Zheng31153d82008-07-28 15:32:19 -04003200
Chris Masonbd56b302009-02-04 09:27:02 -05003201 if (ref->nritems == 0)
3202 return 0;
3203
3204 sorted = kmalloc(sizeof(*sorted) * ref->nritems, GFP_NOFS);
Yan Zheng31153d82008-07-28 15:32:19 -04003205 for (i = 0; i < ref->nritems; i++) {
Chris Masonbd56b302009-02-04 09:27:02 -05003206 sorted[i].bytenr = ref->extents[i].bytenr;
3207 sorted[i].slot = i;
3208 }
3209 sort(sorted, ref->nritems, sizeof(struct refsort), refsort_cmp, NULL);
3210
3211 /*
3212 * the items in the ref were sorted when the ref was inserted
3213 * into the ref cache, so this is already in order
3214 */
3215 for (i = 0; i < ref->nritems; i++) {
3216 info = ref->extents + sorted[i].slot;
Chris Mason56bec292009-03-13 10:10:06 -04003217 ret = btrfs_free_extent(trans, root, info->bytenr,
Zheng Yan31840ae2008-09-23 13:14:14 -04003218 info->num_bytes, ref->bytenr,
3219 ref->owner, ref->generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003220 info->objectid, 0);
Chris Mason2dd3e672008-08-04 08:20:15 -04003221
3222 atomic_inc(&root->fs_info->throttle_gen);
3223 wake_up(&root->fs_info->transaction_throttle);
3224 cond_resched();
3225
Yan Zheng31153d82008-07-28 15:32:19 -04003226 BUG_ON(ret);
3227 info++;
3228 }
Yan Zheng31153d82008-07-28 15:32:19 -04003229
Chris Mason806638b2009-02-05 09:08:14 -05003230 kfree(sorted);
Yan Zheng31153d82008-07-28 15:32:19 -04003231 return 0;
3232}
3233
Chris Mason56bec292009-03-13 10:10:06 -04003234static int drop_snap_lookup_refcount(struct btrfs_trans_handle *trans,
3235 struct btrfs_root *root, u64 start,
Chris Masond3977122009-01-05 21:25:51 -05003236 u64 len, u32 *refs)
Chris Mason333db942008-06-25 16:01:30 -04003237{
Chris Mason017e5362008-07-28 15:32:51 -04003238 int ret;
Chris Masonf87f0572008-08-01 11:27:23 -04003239
Chris Mason56bec292009-03-13 10:10:06 -04003240 ret = btrfs_lookup_extent_ref(trans, root, start, len, refs);
Chris Masonf87f0572008-08-01 11:27:23 -04003241 BUG_ON(ret);
3242
Chris Masond3977122009-01-05 21:25:51 -05003243#if 0 /* some debugging code in case we see problems here */
Chris Masonf87f0572008-08-01 11:27:23 -04003244 /* if the refs count is one, it won't get increased again. But
3245 * if the ref count is > 1, someone may be decreasing it at
3246 * the same time we are.
3247 */
3248 if (*refs != 1) {
3249 struct extent_buffer *eb = NULL;
3250 eb = btrfs_find_create_tree_block(root, start, len);
3251 if (eb)
3252 btrfs_tree_lock(eb);
3253
3254 mutex_lock(&root->fs_info->alloc_mutex);
3255 ret = lookup_extent_ref(NULL, root, start, len, refs);
3256 BUG_ON(ret);
3257 mutex_unlock(&root->fs_info->alloc_mutex);
3258
3259 if (eb) {
3260 btrfs_tree_unlock(eb);
3261 free_extent_buffer(eb);
3262 }
3263 if (*refs == 1) {
Chris Masond3977122009-01-05 21:25:51 -05003264 printk(KERN_ERR "btrfs block %llu went down to one "
3265 "during drop_snap\n", (unsigned long long)start);
Chris Masonf87f0572008-08-01 11:27:23 -04003266 }
3267
3268 }
3269#endif
3270
Chris Masone7a84562008-06-25 16:01:31 -04003271 cond_resched();
Chris Mason017e5362008-07-28 15:32:51 -04003272 return ret;
Chris Mason333db942008-06-25 16:01:30 -04003273}
3274
3275/*
Chris Masonbd56b302009-02-04 09:27:02 -05003276 * this is used while deleting old snapshots, and it drops the refs
3277 * on a whole subtree starting from a level 1 node.
3278 *
3279 * The idea is to sort all the leaf pointers, and then drop the
3280 * ref on all the leaves in order. Most of the time the leaves
3281 * will have ref cache entries, so no leaf IOs will be required to
3282 * find the extents they have references on.
3283 *
3284 * For each leaf, any references it has are also dropped in order
3285 *
3286 * This ends up dropping the references in something close to optimal
3287 * order for reading and modifying the extent allocation tree.
3288 */
3289static noinline int drop_level_one_refs(struct btrfs_trans_handle *trans,
3290 struct btrfs_root *root,
3291 struct btrfs_path *path)
3292{
3293 u64 bytenr;
3294 u64 root_owner;
3295 u64 root_gen;
3296 struct extent_buffer *eb = path->nodes[1];
3297 struct extent_buffer *leaf;
3298 struct btrfs_leaf_ref *ref;
3299 struct refsort *sorted = NULL;
3300 int nritems = btrfs_header_nritems(eb);
3301 int ret;
3302 int i;
3303 int refi = 0;
3304 int slot = path->slots[1];
3305 u32 blocksize = btrfs_level_size(root, 0);
3306 u32 refs;
3307
3308 if (nritems == 0)
3309 goto out;
3310
3311 root_owner = btrfs_header_owner(eb);
3312 root_gen = btrfs_header_generation(eb);
3313 sorted = kmalloc(sizeof(*sorted) * nritems, GFP_NOFS);
3314
3315 /*
3316 * step one, sort all the leaf pointers so we don't scribble
3317 * randomly into the extent allocation tree
3318 */
3319 for (i = slot; i < nritems; i++) {
3320 sorted[refi].bytenr = btrfs_node_blockptr(eb, i);
3321 sorted[refi].slot = i;
3322 refi++;
3323 }
3324
3325 /*
3326 * nritems won't be zero, but if we're picking up drop_snapshot
3327 * after a crash, slot might be > 0, so double check things
3328 * just in case.
3329 */
3330 if (refi == 0)
3331 goto out;
3332
3333 sort(sorted, refi, sizeof(struct refsort), refsort_cmp, NULL);
3334
3335 /*
3336 * the first loop frees everything the leaves point to
3337 */
3338 for (i = 0; i < refi; i++) {
3339 u64 ptr_gen;
3340
3341 bytenr = sorted[i].bytenr;
3342
3343 /*
3344 * check the reference count on this leaf. If it is > 1
3345 * we just decrement it below and don't update any
3346 * of the refs the leaf points to.
3347 */
Chris Mason56bec292009-03-13 10:10:06 -04003348 ret = drop_snap_lookup_refcount(trans, root, bytenr,
3349 blocksize, &refs);
Chris Masonbd56b302009-02-04 09:27:02 -05003350 BUG_ON(ret);
3351 if (refs != 1)
3352 continue;
3353
3354 ptr_gen = btrfs_node_ptr_generation(eb, sorted[i].slot);
3355
3356 /*
3357 * the leaf only had one reference, which means the
3358 * only thing pointing to this leaf is the snapshot
3359 * we're deleting. It isn't possible for the reference
3360 * count to increase again later
3361 *
3362 * The reference cache is checked for the leaf,
3363 * and if found we'll be able to drop any refs held by
3364 * the leaf without needing to read it in.
3365 */
3366 ref = btrfs_lookup_leaf_ref(root, bytenr);
3367 if (ref && ref->generation != ptr_gen) {
3368 btrfs_free_leaf_ref(root, ref);
3369 ref = NULL;
3370 }
3371 if (ref) {
3372 ret = cache_drop_leaf_ref(trans, root, ref);
3373 BUG_ON(ret);
3374 btrfs_remove_leaf_ref(root, ref);
3375 btrfs_free_leaf_ref(root, ref);
3376 } else {
3377 /*
3378 * the leaf wasn't in the reference cache, so
3379 * we have to read it.
3380 */
3381 leaf = read_tree_block(root, bytenr, blocksize,
3382 ptr_gen);
3383 ret = btrfs_drop_leaf_ref(trans, root, leaf);
3384 BUG_ON(ret);
3385 free_extent_buffer(leaf);
3386 }
3387 atomic_inc(&root->fs_info->throttle_gen);
3388 wake_up(&root->fs_info->transaction_throttle);
3389 cond_resched();
3390 }
3391
3392 /*
3393 * run through the loop again to free the refs on the leaves.
3394 * This is faster than doing it in the loop above because
3395 * the leaves are likely to be clustered together. We end up
3396 * working in nice chunks on the extent allocation tree.
3397 */
3398 for (i = 0; i < refi; i++) {
3399 bytenr = sorted[i].bytenr;
Chris Mason56bec292009-03-13 10:10:06 -04003400 ret = btrfs_free_extent(trans, root, bytenr,
Chris Masonbd56b302009-02-04 09:27:02 -05003401 blocksize, eb->start,
3402 root_owner, root_gen, 0, 1);
3403 BUG_ON(ret);
3404
3405 atomic_inc(&root->fs_info->throttle_gen);
3406 wake_up(&root->fs_info->transaction_throttle);
3407 cond_resched();
3408 }
3409out:
3410 kfree(sorted);
3411
3412 /*
3413 * update the path to show we've processed the entire level 1
3414 * node. This will get saved into the root's drop_snapshot_progress
3415 * field so these drops are not repeated again if this transaction
3416 * commits.
3417 */
3418 path->slots[1] = nritems;
3419 return 0;
3420}
3421
3422/*
Chris Mason9aca1d52007-03-13 11:09:37 -04003423 * helper function for drop_snapshot, this walks down the tree dropping ref
3424 * counts as it goes.
3425 */
Chris Masond3977122009-01-05 21:25:51 -05003426static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05003427 struct btrfs_root *root,
3428 struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05003429{
Chris Mason7bb86312007-12-11 09:25:06 -05003430 u64 root_owner;
3431 u64 root_gen;
3432 u64 bytenr;
Chris Masonca7a79a2008-05-12 12:59:19 -04003433 u64 ptr_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04003434 struct extent_buffer *next;
3435 struct extent_buffer *cur;
Chris Mason7bb86312007-12-11 09:25:06 -05003436 struct extent_buffer *parent;
Chris Masondb945352007-10-15 16:15:53 -04003437 u32 blocksize;
Chris Mason20524f02007-03-10 06:35:47 -05003438 int ret;
3439 u32 refs;
3440
Chris Mason5caf2a02007-04-02 11:20:42 -04003441 WARN_ON(*level < 0);
3442 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason56bec292009-03-13 10:10:06 -04003443 ret = drop_snap_lookup_refcount(trans, root, path->nodes[*level]->start,
Chris Masondb945352007-10-15 16:15:53 -04003444 path->nodes[*level]->len, &refs);
Chris Mason20524f02007-03-10 06:35:47 -05003445 BUG_ON(ret);
3446 if (refs > 1)
3447 goto out;
Chris Masone0115992007-06-19 16:23:05 -04003448
Chris Mason9aca1d52007-03-13 11:09:37 -04003449 /*
3450 * walk down to the last node level and free all the leaves
3451 */
Chris Masond3977122009-01-05 21:25:51 -05003452 while (*level >= 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -04003453 WARN_ON(*level < 0);
3454 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason20524f02007-03-10 06:35:47 -05003455 cur = path->nodes[*level];
Chris Masone0115992007-06-19 16:23:05 -04003456
Chris Mason5f39d392007-10-15 16:14:19 -04003457 if (btrfs_header_level(cur) != *level)
Chris Mason2c90e5d2007-04-02 10:50:19 -04003458 WARN_ON(1);
Chris Masone0115992007-06-19 16:23:05 -04003459
Chris Mason7518a232007-03-12 12:01:18 -04003460 if (path->slots[*level] >=
Chris Mason5f39d392007-10-15 16:14:19 -04003461 btrfs_header_nritems(cur))
Chris Mason20524f02007-03-10 06:35:47 -05003462 break;
Chris Masonbd56b302009-02-04 09:27:02 -05003463
3464 /* the new code goes down to level 1 and does all the
3465 * leaves pointed to that node in bulk. So, this check
3466 * for level 0 will always be false.
3467 *
3468 * But, the disk format allows the drop_snapshot_progress
3469 * field in the root to leave things in a state where
3470 * a leaf will need cleaning up here. If someone crashes
3471 * with the old code and then boots with the new code,
3472 * we might find a leaf here.
3473 */
Chris Mason6407bf62007-03-27 06:33:00 -04003474 if (*level == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003475 ret = btrfs_drop_leaf_ref(trans, root, cur);
Chris Mason6407bf62007-03-27 06:33:00 -04003476 BUG_ON(ret);
3477 break;
3478 }
Chris Masonbd56b302009-02-04 09:27:02 -05003479
3480 /*
3481 * once we get to level one, process the whole node
3482 * at once, including everything below it.
3483 */
3484 if (*level == 1) {
3485 ret = drop_level_one_refs(trans, root, path);
3486 BUG_ON(ret);
3487 break;
3488 }
3489
Chris Masondb945352007-10-15 16:15:53 -04003490 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
Chris Masonca7a79a2008-05-12 12:59:19 -04003491 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
Chris Masondb945352007-10-15 16:15:53 -04003492 blocksize = btrfs_level_size(root, *level - 1);
Chris Mason925baed2008-06-25 16:01:30 -04003493
Chris Mason56bec292009-03-13 10:10:06 -04003494 ret = drop_snap_lookup_refcount(trans, root, bytenr,
3495 blocksize, &refs);
Chris Mason6407bf62007-03-27 06:33:00 -04003496 BUG_ON(ret);
Chris Masonbd56b302009-02-04 09:27:02 -05003497
3498 /*
3499 * if there is more than one reference, we don't need
3500 * to read that node to drop any references it has. We
3501 * just drop the ref we hold on that node and move on to the
3502 * next slot in this level.
3503 */
Chris Mason6407bf62007-03-27 06:33:00 -04003504 if (refs != 1) {
Chris Mason7bb86312007-12-11 09:25:06 -05003505 parent = path->nodes[*level];
3506 root_owner = btrfs_header_owner(parent);
3507 root_gen = btrfs_header_generation(parent);
Chris Mason20524f02007-03-10 06:35:47 -05003508 path->slots[*level]++;
Chris Masonf87f0572008-08-01 11:27:23 -04003509
Chris Mason56bec292009-03-13 10:10:06 -04003510 ret = btrfs_free_extent(trans, root, bytenr,
Zheng Yan31840ae2008-09-23 13:14:14 -04003511 blocksize, parent->start,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003512 root_owner, root_gen,
3513 *level - 1, 1);
Chris Mason20524f02007-03-10 06:35:47 -05003514 BUG_ON(ret);
Chris Mason18e35e0a2008-08-01 13:11:41 -04003515
3516 atomic_inc(&root->fs_info->throttle_gen);
3517 wake_up(&root->fs_info->transaction_throttle);
Chris Mason2dd3e672008-08-04 08:20:15 -04003518 cond_resched();
Chris Mason18e35e0a2008-08-01 13:11:41 -04003519
Chris Mason20524f02007-03-10 06:35:47 -05003520 continue;
3521 }
Chris Mason333db942008-06-25 16:01:30 -04003522
Chris Masonbd56b302009-02-04 09:27:02 -05003523 /*
3524 * we need to keep freeing things in the next level down.
3525 * read the block and loop around to process it
3526 */
3527 next = read_tree_block(root, bytenr, blocksize, ptr_gen);
Chris Mason5caf2a02007-04-02 11:20:42 -04003528 WARN_ON(*level <= 0);
Chris Mason83e15a22007-03-12 09:03:27 -04003529 if (path->nodes[*level-1])
Chris Mason5f39d392007-10-15 16:14:19 -04003530 free_extent_buffer(path->nodes[*level-1]);
Chris Mason20524f02007-03-10 06:35:47 -05003531 path->nodes[*level-1] = next;
Chris Mason5f39d392007-10-15 16:14:19 -04003532 *level = btrfs_header_level(next);
Chris Mason20524f02007-03-10 06:35:47 -05003533 path->slots[*level] = 0;
Chris Mason2dd3e672008-08-04 08:20:15 -04003534 cond_resched();
Chris Mason20524f02007-03-10 06:35:47 -05003535 }
3536out:
Chris Mason5caf2a02007-04-02 11:20:42 -04003537 WARN_ON(*level < 0);
3538 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason7bb86312007-12-11 09:25:06 -05003539
3540 if (path->nodes[*level] == root->node) {
Chris Mason7bb86312007-12-11 09:25:06 -05003541 parent = path->nodes[*level];
Yan Zheng31153d82008-07-28 15:32:19 -04003542 bytenr = path->nodes[*level]->start;
Chris Mason7bb86312007-12-11 09:25:06 -05003543 } else {
3544 parent = path->nodes[*level + 1];
Yan Zheng31153d82008-07-28 15:32:19 -04003545 bytenr = btrfs_node_blockptr(parent, path->slots[*level + 1]);
Chris Mason7bb86312007-12-11 09:25:06 -05003546 }
3547
Yan Zheng31153d82008-07-28 15:32:19 -04003548 blocksize = btrfs_level_size(root, *level);
3549 root_owner = btrfs_header_owner(parent);
Chris Mason7bb86312007-12-11 09:25:06 -05003550 root_gen = btrfs_header_generation(parent);
Yan Zheng31153d82008-07-28 15:32:19 -04003551
Chris Masonbd56b302009-02-04 09:27:02 -05003552 /*
3553 * cleanup and free the reference on the last node
3554 * we processed
3555 */
Chris Mason56bec292009-03-13 10:10:06 -04003556 ret = btrfs_free_extent(trans, root, bytenr, blocksize,
Zheng Yan31840ae2008-09-23 13:14:14 -04003557 parent->start, root_owner, root_gen,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003558 *level, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003559 free_extent_buffer(path->nodes[*level]);
Chris Mason20524f02007-03-10 06:35:47 -05003560 path->nodes[*level] = NULL;
Chris Masonbd56b302009-02-04 09:27:02 -05003561
Chris Mason20524f02007-03-10 06:35:47 -05003562 *level += 1;
3563 BUG_ON(ret);
Chris Masonf87f0572008-08-01 11:27:23 -04003564
Chris Masone7a84562008-06-25 16:01:31 -04003565 cond_resched();
Chris Mason20524f02007-03-10 06:35:47 -05003566 return 0;
3567}
3568
Chris Mason9aca1d52007-03-13 11:09:37 -04003569/*
Yan Zhengf82d02d2008-10-29 14:49:05 -04003570 * helper function for drop_subtree, this function is similar to
3571 * walk_down_tree. The main difference is that it checks reference
3572 * counts while tree blocks are locked.
3573 */
Chris Masond3977122009-01-05 21:25:51 -05003574static noinline int walk_down_subtree(struct btrfs_trans_handle *trans,
Yan Zhengf82d02d2008-10-29 14:49:05 -04003575 struct btrfs_root *root,
3576 struct btrfs_path *path, int *level)
3577{
3578 struct extent_buffer *next;
3579 struct extent_buffer *cur;
3580 struct extent_buffer *parent;
3581 u64 bytenr;
3582 u64 ptr_gen;
3583 u32 blocksize;
3584 u32 refs;
3585 int ret;
3586
3587 cur = path->nodes[*level];
3588 ret = btrfs_lookup_extent_ref(trans, root, cur->start, cur->len,
3589 &refs);
3590 BUG_ON(ret);
3591 if (refs > 1)
3592 goto out;
3593
3594 while (*level >= 0) {
3595 cur = path->nodes[*level];
3596 if (*level == 0) {
3597 ret = btrfs_drop_leaf_ref(trans, root, cur);
3598 BUG_ON(ret);
3599 clean_tree_block(trans, root, cur);
3600 break;
3601 }
3602 if (path->slots[*level] >= btrfs_header_nritems(cur)) {
3603 clean_tree_block(trans, root, cur);
3604 break;
3605 }
3606
3607 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
3608 blocksize = btrfs_level_size(root, *level - 1);
3609 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
3610
3611 next = read_tree_block(root, bytenr, blocksize, ptr_gen);
3612 btrfs_tree_lock(next);
Chris Masonb4ce94d2009-02-04 09:25:08 -05003613 btrfs_set_lock_blocking(next);
Yan Zhengf82d02d2008-10-29 14:49:05 -04003614
3615 ret = btrfs_lookup_extent_ref(trans, root, bytenr, blocksize,
3616 &refs);
3617 BUG_ON(ret);
3618 if (refs > 1) {
3619 parent = path->nodes[*level];
3620 ret = btrfs_free_extent(trans, root, bytenr,
3621 blocksize, parent->start,
3622 btrfs_header_owner(parent),
3623 btrfs_header_generation(parent),
3624 *level - 1, 1);
3625 BUG_ON(ret);
3626 path->slots[*level]++;
3627 btrfs_tree_unlock(next);
3628 free_extent_buffer(next);
3629 continue;
3630 }
3631
3632 *level = btrfs_header_level(next);
3633 path->nodes[*level] = next;
3634 path->slots[*level] = 0;
3635 path->locks[*level] = 1;
3636 cond_resched();
3637 }
3638out:
3639 parent = path->nodes[*level + 1];
3640 bytenr = path->nodes[*level]->start;
3641 blocksize = path->nodes[*level]->len;
3642
3643 ret = btrfs_free_extent(trans, root, bytenr, blocksize,
3644 parent->start, btrfs_header_owner(parent),
3645 btrfs_header_generation(parent), *level, 1);
3646 BUG_ON(ret);
3647
3648 if (path->locks[*level]) {
3649 btrfs_tree_unlock(path->nodes[*level]);
3650 path->locks[*level] = 0;
3651 }
3652 free_extent_buffer(path->nodes[*level]);
3653 path->nodes[*level] = NULL;
3654 *level += 1;
3655 cond_resched();
3656 return 0;
3657}
3658
3659/*
Chris Mason9aca1d52007-03-13 11:09:37 -04003660 * helper for dropping snapshots. This walks back up the tree in the path
3661 * to find the first node higher up where we haven't yet gone through
3662 * all the slots
3663 */
Chris Masond3977122009-01-05 21:25:51 -05003664static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05003665 struct btrfs_root *root,
Yan Zhengf82d02d2008-10-29 14:49:05 -04003666 struct btrfs_path *path,
3667 int *level, int max_level)
Chris Mason20524f02007-03-10 06:35:47 -05003668{
Chris Mason7bb86312007-12-11 09:25:06 -05003669 u64 root_owner;
3670 u64 root_gen;
3671 struct btrfs_root_item *root_item = &root->root_item;
Chris Mason20524f02007-03-10 06:35:47 -05003672 int i;
3673 int slot;
3674 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04003675
Yan Zhengf82d02d2008-10-29 14:49:05 -04003676 for (i = *level; i < max_level && path->nodes[i]; i++) {
Chris Mason20524f02007-03-10 06:35:47 -05003677 slot = path->slots[i];
Chris Mason5f39d392007-10-15 16:14:19 -04003678 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
3679 struct extent_buffer *node;
3680 struct btrfs_disk_key disk_key;
Chris Masonbd56b302009-02-04 09:27:02 -05003681
3682 /*
3683 * there is more work to do in this level.
3684 * Update the drop_progress marker to reflect
3685 * the work we've done so far, and then bump
3686 * the slot number
3687 */
Chris Mason5f39d392007-10-15 16:14:19 -04003688 node = path->nodes[i];
Chris Mason20524f02007-03-10 06:35:47 -05003689 path->slots[i]++;
3690 *level = i;
Chris Mason9f3a7422007-08-07 15:52:19 -04003691 WARN_ON(*level == 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003692 btrfs_node_key(node, &disk_key, path->slots[i]);
Chris Mason9f3a7422007-08-07 15:52:19 -04003693 memcpy(&root_item->drop_progress,
Chris Mason5f39d392007-10-15 16:14:19 -04003694 &disk_key, sizeof(disk_key));
Chris Mason9f3a7422007-08-07 15:52:19 -04003695 root_item->drop_level = i;
Chris Mason20524f02007-03-10 06:35:47 -05003696 return 0;
3697 } else {
Zheng Yan31840ae2008-09-23 13:14:14 -04003698 struct extent_buffer *parent;
Chris Masonbd56b302009-02-04 09:27:02 -05003699
3700 /*
3701 * this whole node is done, free our reference
3702 * on it and go up one level
3703 */
Zheng Yan31840ae2008-09-23 13:14:14 -04003704 if (path->nodes[*level] == root->node)
3705 parent = path->nodes[*level];
3706 else
3707 parent = path->nodes[*level + 1];
3708
3709 root_owner = btrfs_header_owner(parent);
3710 root_gen = btrfs_header_generation(parent);
Yan Zhengf82d02d2008-10-29 14:49:05 -04003711
3712 clean_tree_block(trans, root, path->nodes[*level]);
Chris Masone089f052007-03-16 16:20:31 -04003713 ret = btrfs_free_extent(trans, root,
Chris Masondb945352007-10-15 16:15:53 -04003714 path->nodes[*level]->start,
Chris Mason7bb86312007-12-11 09:25:06 -05003715 path->nodes[*level]->len,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003716 parent->start, root_owner,
3717 root_gen, *level, 1);
Chris Mason6407bf62007-03-27 06:33:00 -04003718 BUG_ON(ret);
Yan Zhengf82d02d2008-10-29 14:49:05 -04003719 if (path->locks[*level]) {
3720 btrfs_tree_unlock(path->nodes[*level]);
3721 path->locks[*level] = 0;
3722 }
Chris Mason5f39d392007-10-15 16:14:19 -04003723 free_extent_buffer(path->nodes[*level]);
Chris Mason83e15a22007-03-12 09:03:27 -04003724 path->nodes[*level] = NULL;
Chris Mason20524f02007-03-10 06:35:47 -05003725 *level = i + 1;
Chris Mason20524f02007-03-10 06:35:47 -05003726 }
3727 }
3728 return 1;
3729}
3730
Chris Mason9aca1d52007-03-13 11:09:37 -04003731/*
3732 * drop the reference count on the tree rooted at 'snap'. This traverses
3733 * the tree freeing any blocks that have a ref count of zero after being
3734 * decremented.
3735 */
Chris Masone089f052007-03-16 16:20:31 -04003736int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason9f3a7422007-08-07 15:52:19 -04003737 *root)
Chris Mason20524f02007-03-10 06:35:47 -05003738{
Chris Mason3768f362007-03-13 16:47:54 -04003739 int ret = 0;
Chris Mason9aca1d52007-03-13 11:09:37 -04003740 int wret;
Chris Mason20524f02007-03-10 06:35:47 -05003741 int level;
Chris Mason5caf2a02007-04-02 11:20:42 -04003742 struct btrfs_path *path;
Chris Mason20524f02007-03-10 06:35:47 -05003743 int i;
3744 int orig_level;
Chris Masonc3e69d52009-03-13 10:17:05 -04003745 int update_count;
Chris Mason9f3a7422007-08-07 15:52:19 -04003746 struct btrfs_root_item *root_item = &root->root_item;
Chris Mason20524f02007-03-10 06:35:47 -05003747
Chris Masona2135012008-06-25 16:01:30 -04003748 WARN_ON(!mutex_is_locked(&root->fs_info->drop_mutex));
Chris Mason5caf2a02007-04-02 11:20:42 -04003749 path = btrfs_alloc_path();
3750 BUG_ON(!path);
Chris Mason20524f02007-03-10 06:35:47 -05003751
Chris Mason5f39d392007-10-15 16:14:19 -04003752 level = btrfs_header_level(root->node);
Chris Mason20524f02007-03-10 06:35:47 -05003753 orig_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04003754 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
3755 path->nodes[level] = root->node;
Chris Masonf510cfe2007-10-15 16:14:48 -04003756 extent_buffer_get(root->node);
Chris Mason9f3a7422007-08-07 15:52:19 -04003757 path->slots[level] = 0;
3758 } else {
3759 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04003760 struct btrfs_disk_key found_key;
3761 struct extent_buffer *node;
Chris Mason6702ed42007-08-07 16:15:09 -04003762
Chris Mason9f3a7422007-08-07 15:52:19 -04003763 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Chris Mason6702ed42007-08-07 16:15:09 -04003764 level = root_item->drop_level;
3765 path->lowest_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04003766 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason6702ed42007-08-07 16:15:09 -04003767 if (wret < 0) {
Chris Mason9f3a7422007-08-07 15:52:19 -04003768 ret = wret;
3769 goto out;
3770 }
Chris Mason5f39d392007-10-15 16:14:19 -04003771 node = path->nodes[level];
3772 btrfs_node_key(node, &found_key, path->slots[level]);
3773 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
3774 sizeof(found_key)));
Chris Mason7d9eb122008-07-08 14:19:17 -04003775 /*
3776 * unlock our path, this is safe because only this
3777 * function is allowed to delete this snapshot
3778 */
Chris Mason925baed2008-06-25 16:01:30 -04003779 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
3780 if (path->nodes[i] && path->locks[i]) {
3781 path->locks[i] = 0;
3782 btrfs_tree_unlock(path->nodes[i]);
3783 }
3784 }
Chris Mason9f3a7422007-08-07 15:52:19 -04003785 }
Chris Masond3977122009-01-05 21:25:51 -05003786 while (1) {
Chris Masonc3e69d52009-03-13 10:17:05 -04003787 unsigned long update;
Chris Mason5caf2a02007-04-02 11:20:42 -04003788 wret = walk_down_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04003789 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05003790 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04003791 if (wret < 0)
3792 ret = wret;
3793
Yan Zhengf82d02d2008-10-29 14:49:05 -04003794 wret = walk_up_tree(trans, root, path, &level,
3795 BTRFS_MAX_LEVEL);
Chris Mason9aca1d52007-03-13 11:09:37 -04003796 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05003797 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04003798 if (wret < 0)
3799 ret = wret;
Chris Masonb7ec40d2009-03-12 20:12:45 -04003800 if (trans->transaction->in_commit ||
3801 trans->transaction->delayed_refs.flushing) {
Chris Masone7a84562008-06-25 16:01:31 -04003802 ret = -EAGAIN;
3803 break;
3804 }
Chris Mason18e35e0a2008-08-01 13:11:41 -04003805 atomic_inc(&root->fs_info->throttle_gen);
Chris Mason017e5362008-07-28 15:32:51 -04003806 wake_up(&root->fs_info->transaction_throttle);
Chris Masonc3e69d52009-03-13 10:17:05 -04003807 for (update_count = 0; update_count < 16; update_count++) {
3808 update = trans->delayed_ref_updates;
3809 trans->delayed_ref_updates = 0;
3810 if (update)
3811 btrfs_run_delayed_refs(trans, root, update);
3812 else
3813 break;
3814 }
Chris Mason20524f02007-03-10 06:35:47 -05003815 }
Chris Mason83e15a22007-03-12 09:03:27 -04003816 for (i = 0; i <= orig_level; i++) {
Chris Mason5caf2a02007-04-02 11:20:42 -04003817 if (path->nodes[i]) {
Chris Mason5f39d392007-10-15 16:14:19 -04003818 free_extent_buffer(path->nodes[i]);
Chris Mason0f827312007-10-15 16:18:56 -04003819 path->nodes[i] = NULL;
Chris Mason83e15a22007-03-12 09:03:27 -04003820 }
Chris Mason20524f02007-03-10 06:35:47 -05003821 }
Chris Mason9f3a7422007-08-07 15:52:19 -04003822out:
Chris Mason5caf2a02007-04-02 11:20:42 -04003823 btrfs_free_path(path);
Chris Mason9aca1d52007-03-13 11:09:37 -04003824 return ret;
Chris Mason20524f02007-03-10 06:35:47 -05003825}
Chris Mason9078a3e2007-04-26 16:46:15 -04003826
Yan Zhengf82d02d2008-10-29 14:49:05 -04003827int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
3828 struct btrfs_root *root,
3829 struct extent_buffer *node,
3830 struct extent_buffer *parent)
3831{
3832 struct btrfs_path *path;
3833 int level;
3834 int parent_level;
3835 int ret = 0;
3836 int wret;
3837
3838 path = btrfs_alloc_path();
3839 BUG_ON(!path);
3840
Chris Masonb9447ef82009-03-09 11:45:38 -04003841 btrfs_assert_tree_locked(parent);
Yan Zhengf82d02d2008-10-29 14:49:05 -04003842 parent_level = btrfs_header_level(parent);
3843 extent_buffer_get(parent);
3844 path->nodes[parent_level] = parent;
3845 path->slots[parent_level] = btrfs_header_nritems(parent);
3846
Chris Masonb9447ef82009-03-09 11:45:38 -04003847 btrfs_assert_tree_locked(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04003848 level = btrfs_header_level(node);
3849 extent_buffer_get(node);
3850 path->nodes[level] = node;
3851 path->slots[level] = 0;
3852
3853 while (1) {
3854 wret = walk_down_subtree(trans, root, path, &level);
3855 if (wret < 0)
3856 ret = wret;
3857 if (wret != 0)
3858 break;
3859
3860 wret = walk_up_tree(trans, root, path, &level, parent_level);
3861 if (wret < 0)
3862 ret = wret;
3863 if (wret != 0)
3864 break;
3865 }
3866
3867 btrfs_free_path(path);
3868 return ret;
3869}
3870
Chris Mason8e7bf942008-04-28 09:02:36 -04003871static unsigned long calc_ra(unsigned long start, unsigned long last,
3872 unsigned long nr)
3873{
3874 return min(last, start + nr - 1);
3875}
3876
Chris Masond3977122009-01-05 21:25:51 -05003877static noinline int relocate_inode_pages(struct inode *inode, u64 start,
Chris Mason98ed5172008-01-03 10:01:48 -05003878 u64 len)
Chris Masonedbd8d42007-12-21 16:27:24 -05003879{
3880 u64 page_start;
3881 u64 page_end;
Zheng Yan1a40e232008-09-26 10:09:34 -04003882 unsigned long first_index;
Chris Masonedbd8d42007-12-21 16:27:24 -05003883 unsigned long last_index;
Chris Masonedbd8d42007-12-21 16:27:24 -05003884 unsigned long i;
3885 struct page *page;
Chris Masond1310b22008-01-24 16:13:08 -05003886 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason4313b392008-01-03 09:08:48 -05003887 struct file_ra_state *ra;
Chris Mason3eaa2882008-07-24 11:57:52 -04003888 struct btrfs_ordered_extent *ordered;
Zheng Yan1a40e232008-09-26 10:09:34 -04003889 unsigned int total_read = 0;
3890 unsigned int total_dirty = 0;
3891 int ret = 0;
Chris Mason4313b392008-01-03 09:08:48 -05003892
3893 ra = kzalloc(sizeof(*ra), GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05003894
3895 mutex_lock(&inode->i_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04003896 first_index = start >> PAGE_CACHE_SHIFT;
Chris Masonedbd8d42007-12-21 16:27:24 -05003897 last_index = (start + len - 1) >> PAGE_CACHE_SHIFT;
3898
Zheng Yan1a40e232008-09-26 10:09:34 -04003899 /* make sure the dirty trick played by the caller work */
3900 ret = invalidate_inode_pages2_range(inode->i_mapping,
3901 first_index, last_index);
3902 if (ret)
3903 goto out_unlock;
Chris Mason8e7bf942008-04-28 09:02:36 -04003904
Chris Mason4313b392008-01-03 09:08:48 -05003905 file_ra_state_init(ra, inode->i_mapping);
Chris Masonedbd8d42007-12-21 16:27:24 -05003906
Zheng Yan1a40e232008-09-26 10:09:34 -04003907 for (i = first_index ; i <= last_index; i++) {
3908 if (total_read % ra->ra_pages == 0) {
Chris Mason8e7bf942008-04-28 09:02:36 -04003909 btrfs_force_ra(inode->i_mapping, ra, NULL, i,
Zheng Yan1a40e232008-09-26 10:09:34 -04003910 calc_ra(i, last_index, ra->ra_pages));
Chris Mason8e7bf942008-04-28 09:02:36 -04003911 }
3912 total_read++;
Chris Mason3eaa2882008-07-24 11:57:52 -04003913again:
3914 if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode))
Zheng Yan1a40e232008-09-26 10:09:34 -04003915 BUG_ON(1);
Chris Masonedbd8d42007-12-21 16:27:24 -05003916 page = grab_cache_page(inode->i_mapping, i);
Chris Masona061fc82008-05-07 11:43:44 -04003917 if (!page) {
Zheng Yan1a40e232008-09-26 10:09:34 -04003918 ret = -ENOMEM;
Chris Masonedbd8d42007-12-21 16:27:24 -05003919 goto out_unlock;
Chris Masona061fc82008-05-07 11:43:44 -04003920 }
Chris Masonedbd8d42007-12-21 16:27:24 -05003921 if (!PageUptodate(page)) {
3922 btrfs_readpage(NULL, page);
3923 lock_page(page);
3924 if (!PageUptodate(page)) {
3925 unlock_page(page);
3926 page_cache_release(page);
Zheng Yan1a40e232008-09-26 10:09:34 -04003927 ret = -EIO;
Chris Masonedbd8d42007-12-21 16:27:24 -05003928 goto out_unlock;
3929 }
3930 }
Chris Masonec44a352008-04-28 15:29:52 -04003931 wait_on_page_writeback(page);
Chris Mason3eaa2882008-07-24 11:57:52 -04003932
Chris Masonedbd8d42007-12-21 16:27:24 -05003933 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
3934 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05003935 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05003936
Chris Mason3eaa2882008-07-24 11:57:52 -04003937 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3938 if (ordered) {
3939 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
3940 unlock_page(page);
3941 page_cache_release(page);
3942 btrfs_start_ordered_extent(inode, ordered, 1);
3943 btrfs_put_ordered_extent(ordered);
3944 goto again;
3945 }
3946 set_page_extent_mapped(page);
3947
Zheng Yan1a40e232008-09-26 10:09:34 -04003948 if (i == first_index)
3949 set_extent_bits(io_tree, page_start, page_end,
3950 EXTENT_BOUNDARY, GFP_NOFS);
Yan Zheng1f80e4d2008-12-19 10:59:04 -05003951 btrfs_set_extent_delalloc(inode, page_start, page_end);
Zheng Yan1a40e232008-09-26 10:09:34 -04003952
Chris Masona061fc82008-05-07 11:43:44 -04003953 set_page_dirty(page);
Zheng Yan1a40e232008-09-26 10:09:34 -04003954 total_dirty++;
Chris Masonedbd8d42007-12-21 16:27:24 -05003955
Chris Masond1310b22008-01-24 16:13:08 -05003956 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05003957 unlock_page(page);
3958 page_cache_release(page);
3959 }
3960
3961out_unlock:
Chris Masonec44a352008-04-28 15:29:52 -04003962 kfree(ra);
Chris Masonedbd8d42007-12-21 16:27:24 -05003963 mutex_unlock(&inode->i_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04003964 balance_dirty_pages_ratelimited_nr(inode->i_mapping, total_dirty);
Chris Masonbf4ef672008-05-08 13:26:18 -04003965 return ret;
3966}
3967
Chris Masond3977122009-01-05 21:25:51 -05003968static noinline int relocate_data_extent(struct inode *reloc_inode,
Zheng Yan1a40e232008-09-26 10:09:34 -04003969 struct btrfs_key *extent_key,
3970 u64 offset)
Chris Masonedbd8d42007-12-21 16:27:24 -05003971{
Zheng Yan1a40e232008-09-26 10:09:34 -04003972 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
3973 struct extent_map_tree *em_tree = &BTRFS_I(reloc_inode)->extent_tree;
3974 struct extent_map *em;
Yan Zheng66435582008-10-30 14:19:50 -04003975 u64 start = extent_key->objectid - offset;
3976 u64 end = start + extent_key->offset - 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04003977
3978 em = alloc_extent_map(GFP_NOFS);
3979 BUG_ON(!em || IS_ERR(em));
3980
Yan Zheng66435582008-10-30 14:19:50 -04003981 em->start = start;
Zheng Yan1a40e232008-09-26 10:09:34 -04003982 em->len = extent_key->offset;
Chris Masonc8b97812008-10-29 14:49:59 -04003983 em->block_len = extent_key->offset;
Zheng Yan1a40e232008-09-26 10:09:34 -04003984 em->block_start = extent_key->objectid;
3985 em->bdev = root->fs_info->fs_devices->latest_bdev;
3986 set_bit(EXTENT_FLAG_PINNED, &em->flags);
3987
3988 /* setup extent map to cheat btrfs_readpage */
Yan Zheng66435582008-10-30 14:19:50 -04003989 lock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04003990 while (1) {
3991 int ret;
3992 spin_lock(&em_tree->lock);
3993 ret = add_extent_mapping(em_tree, em);
3994 spin_unlock(&em_tree->lock);
3995 if (ret != -EEXIST) {
3996 free_extent_map(em);
3997 break;
3998 }
Yan Zheng66435582008-10-30 14:19:50 -04003999 btrfs_drop_extent_cache(reloc_inode, start, end, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04004000 }
Yan Zheng66435582008-10-30 14:19:50 -04004001 unlock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04004002
Yan Zheng66435582008-10-30 14:19:50 -04004003 return relocate_inode_pages(reloc_inode, start, extent_key->offset);
Zheng Yan1a40e232008-09-26 10:09:34 -04004004}
4005
4006struct btrfs_ref_path {
4007 u64 extent_start;
4008 u64 nodes[BTRFS_MAX_LEVEL];
4009 u64 root_objectid;
4010 u64 root_generation;
4011 u64 owner_objectid;
Zheng Yan1a40e232008-09-26 10:09:34 -04004012 u32 num_refs;
4013 int lowest_level;
4014 int current_level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04004015 int shared_level;
4016
4017 struct btrfs_key node_keys[BTRFS_MAX_LEVEL];
4018 u64 new_nodes[BTRFS_MAX_LEVEL];
Zheng Yan1a40e232008-09-26 10:09:34 -04004019};
4020
4021struct disk_extent {
Chris Masonc8b97812008-10-29 14:49:59 -04004022 u64 ram_bytes;
Zheng Yan1a40e232008-09-26 10:09:34 -04004023 u64 disk_bytenr;
4024 u64 disk_num_bytes;
4025 u64 offset;
4026 u64 num_bytes;
Chris Masonc8b97812008-10-29 14:49:59 -04004027 u8 compression;
4028 u8 encryption;
4029 u16 other_encoding;
Zheng Yan1a40e232008-09-26 10:09:34 -04004030};
4031
4032static int is_cowonly_root(u64 root_objectid)
4033{
4034 if (root_objectid == BTRFS_ROOT_TREE_OBJECTID ||
4035 root_objectid == BTRFS_EXTENT_TREE_OBJECTID ||
4036 root_objectid == BTRFS_CHUNK_TREE_OBJECTID ||
4037 root_objectid == BTRFS_DEV_TREE_OBJECTID ||
Yan Zheng0403e472008-12-10 20:32:51 -05004038 root_objectid == BTRFS_TREE_LOG_OBJECTID ||
4039 root_objectid == BTRFS_CSUM_TREE_OBJECTID)
Zheng Yan1a40e232008-09-26 10:09:34 -04004040 return 1;
4041 return 0;
4042}
4043
Chris Masond3977122009-01-05 21:25:51 -05004044static noinline int __next_ref_path(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04004045 struct btrfs_root *extent_root,
4046 struct btrfs_ref_path *ref_path,
4047 int first_time)
4048{
4049 struct extent_buffer *leaf;
4050 struct btrfs_path *path;
Chris Mason4313b392008-01-03 09:08:48 -05004051 struct btrfs_extent_ref *ref;
Chris Masonedbd8d42007-12-21 16:27:24 -05004052 struct btrfs_key key;
4053 struct btrfs_key found_key;
Zheng Yan1a40e232008-09-26 10:09:34 -04004054 u64 bytenr;
Chris Masonedbd8d42007-12-21 16:27:24 -05004055 u32 nritems;
Zheng Yan1a40e232008-09-26 10:09:34 -04004056 int level;
4057 int ret = 1;
Chris Masonedbd8d42007-12-21 16:27:24 -05004058
Zheng Yan1a40e232008-09-26 10:09:34 -04004059 path = btrfs_alloc_path();
4060 if (!path)
4061 return -ENOMEM;
4062
Zheng Yan1a40e232008-09-26 10:09:34 -04004063 if (first_time) {
4064 ref_path->lowest_level = -1;
4065 ref_path->current_level = -1;
Yan Zhengf82d02d2008-10-29 14:49:05 -04004066 ref_path->shared_level = -1;
Zheng Yan1a40e232008-09-26 10:09:34 -04004067 goto walk_up;
Chris Masona061fc82008-05-07 11:43:44 -04004068 }
Zheng Yan1a40e232008-09-26 10:09:34 -04004069walk_down:
4070 level = ref_path->current_level - 1;
4071 while (level >= -1) {
4072 u64 parent;
4073 if (level < ref_path->lowest_level)
4074 break;
Chris Masonedbd8d42007-12-21 16:27:24 -05004075
Chris Masond3977122009-01-05 21:25:51 -05004076 if (level >= 0)
Zheng Yan1a40e232008-09-26 10:09:34 -04004077 bytenr = ref_path->nodes[level];
Chris Masond3977122009-01-05 21:25:51 -05004078 else
Zheng Yan1a40e232008-09-26 10:09:34 -04004079 bytenr = ref_path->extent_start;
Zheng Yan1a40e232008-09-26 10:09:34 -04004080 BUG_ON(bytenr == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05004081
Zheng Yan1a40e232008-09-26 10:09:34 -04004082 parent = ref_path->nodes[level + 1];
4083 ref_path->nodes[level + 1] = 0;
4084 ref_path->current_level = level;
4085 BUG_ON(parent == 0);
4086
4087 key.objectid = bytenr;
4088 key.offset = parent + 1;
4089 key.type = BTRFS_EXTENT_REF_KEY;
4090
4091 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05004092 if (ret < 0)
4093 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04004094 BUG_ON(ret == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05004095
Chris Masonedbd8d42007-12-21 16:27:24 -05004096 leaf = path->nodes[0];
4097 nritems = btrfs_header_nritems(leaf);
Zheng Yan1a40e232008-09-26 10:09:34 -04004098 if (path->slots[0] >= nritems) {
Chris Masona061fc82008-05-07 11:43:44 -04004099 ret = btrfs_next_leaf(extent_root, path);
Chris Masona061fc82008-05-07 11:43:44 -04004100 if (ret < 0)
4101 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04004102 if (ret > 0)
4103 goto next;
Chris Masonbf4ef672008-05-08 13:26:18 -04004104 leaf = path->nodes[0];
Chris Masona061fc82008-05-07 11:43:44 -04004105 }
Chris Masonedbd8d42007-12-21 16:27:24 -05004106
4107 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Zheng Yan1a40e232008-09-26 10:09:34 -04004108 if (found_key.objectid == bytenr &&
Yan Zhengf82d02d2008-10-29 14:49:05 -04004109 found_key.type == BTRFS_EXTENT_REF_KEY) {
4110 if (level < ref_path->shared_level)
4111 ref_path->shared_level = level;
Zheng Yan1a40e232008-09-26 10:09:34 -04004112 goto found;
Yan Zhengf82d02d2008-10-29 14:49:05 -04004113 }
Zheng Yan1a40e232008-09-26 10:09:34 -04004114next:
4115 level--;
4116 btrfs_release_path(extent_root, path);
Yan Zhengd899e052008-10-30 14:25:28 -04004117 cond_resched();
Zheng Yan1a40e232008-09-26 10:09:34 -04004118 }
4119 /* reached lowest level */
4120 ret = 1;
4121 goto out;
4122walk_up:
4123 level = ref_path->current_level;
4124 while (level < BTRFS_MAX_LEVEL - 1) {
4125 u64 ref_objectid;
Chris Masond3977122009-01-05 21:25:51 -05004126
4127 if (level >= 0)
Zheng Yan1a40e232008-09-26 10:09:34 -04004128 bytenr = ref_path->nodes[level];
Chris Masond3977122009-01-05 21:25:51 -05004129 else
Zheng Yan1a40e232008-09-26 10:09:34 -04004130 bytenr = ref_path->extent_start;
Chris Masond3977122009-01-05 21:25:51 -05004131
Zheng Yan1a40e232008-09-26 10:09:34 -04004132 BUG_ON(bytenr == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05004133
Zheng Yan1a40e232008-09-26 10:09:34 -04004134 key.objectid = bytenr;
4135 key.offset = 0;
4136 key.type = BTRFS_EXTENT_REF_KEY;
Chris Masonedbd8d42007-12-21 16:27:24 -05004137
Zheng Yan1a40e232008-09-26 10:09:34 -04004138 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
4139 if (ret < 0)
Chris Masonedbd8d42007-12-21 16:27:24 -05004140 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04004141
4142 leaf = path->nodes[0];
4143 nritems = btrfs_header_nritems(leaf);
4144 if (path->slots[0] >= nritems) {
4145 ret = btrfs_next_leaf(extent_root, path);
4146 if (ret < 0)
4147 goto out;
4148 if (ret > 0) {
4149 /* the extent was freed by someone */
4150 if (ref_path->lowest_level == level)
4151 goto out;
4152 btrfs_release_path(extent_root, path);
4153 goto walk_down;
4154 }
4155 leaf = path->nodes[0];
4156 }
4157
4158 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4159 if (found_key.objectid != bytenr ||
4160 found_key.type != BTRFS_EXTENT_REF_KEY) {
4161 /* the extent was freed by someone */
4162 if (ref_path->lowest_level == level) {
4163 ret = 1;
4164 goto out;
4165 }
4166 btrfs_release_path(extent_root, path);
4167 goto walk_down;
4168 }
4169found:
4170 ref = btrfs_item_ptr(leaf, path->slots[0],
4171 struct btrfs_extent_ref);
4172 ref_objectid = btrfs_ref_objectid(leaf, ref);
4173 if (ref_objectid < BTRFS_FIRST_FREE_OBJECTID) {
4174 if (first_time) {
4175 level = (int)ref_objectid;
4176 BUG_ON(level >= BTRFS_MAX_LEVEL);
4177 ref_path->lowest_level = level;
4178 ref_path->current_level = level;
4179 ref_path->nodes[level] = bytenr;
4180 } else {
4181 WARN_ON(ref_objectid != level);
4182 }
4183 } else {
4184 WARN_ON(level != -1);
4185 }
4186 first_time = 0;
4187
4188 if (ref_path->lowest_level == level) {
4189 ref_path->owner_objectid = ref_objectid;
Zheng Yan1a40e232008-09-26 10:09:34 -04004190 ref_path->num_refs = btrfs_ref_num_refs(leaf, ref);
4191 }
4192
4193 /*
4194 * the block is tree root or the block isn't in reference
4195 * counted tree.
4196 */
4197 if (found_key.objectid == found_key.offset ||
4198 is_cowonly_root(btrfs_ref_root(leaf, ref))) {
4199 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
4200 ref_path->root_generation =
4201 btrfs_ref_generation(leaf, ref);
4202 if (level < 0) {
4203 /* special reference from the tree log */
4204 ref_path->nodes[0] = found_key.offset;
4205 ref_path->current_level = 0;
4206 }
4207 ret = 0;
4208 goto out;
4209 }
4210
4211 level++;
4212 BUG_ON(ref_path->nodes[level] != 0);
4213 ref_path->nodes[level] = found_key.offset;
4214 ref_path->current_level = level;
4215
4216 /*
4217 * the reference was created in the running transaction,
4218 * no need to continue walking up.
4219 */
4220 if (btrfs_ref_generation(leaf, ref) == trans->transid) {
4221 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
4222 ref_path->root_generation =
4223 btrfs_ref_generation(leaf, ref);
4224 ret = 0;
4225 goto out;
4226 }
4227
4228 btrfs_release_path(extent_root, path);
Yan Zhengd899e052008-10-30 14:25:28 -04004229 cond_resched();
Zheng Yan1a40e232008-09-26 10:09:34 -04004230 }
4231 /* reached max tree level, but no tree root found. */
4232 BUG();
4233out:
Zheng Yan1a40e232008-09-26 10:09:34 -04004234 btrfs_free_path(path);
4235 return ret;
4236}
4237
4238static int btrfs_first_ref_path(struct btrfs_trans_handle *trans,
4239 struct btrfs_root *extent_root,
4240 struct btrfs_ref_path *ref_path,
4241 u64 extent_start)
4242{
4243 memset(ref_path, 0, sizeof(*ref_path));
4244 ref_path->extent_start = extent_start;
4245
4246 return __next_ref_path(trans, extent_root, ref_path, 1);
4247}
4248
4249static int btrfs_next_ref_path(struct btrfs_trans_handle *trans,
4250 struct btrfs_root *extent_root,
4251 struct btrfs_ref_path *ref_path)
4252{
4253 return __next_ref_path(trans, extent_root, ref_path, 0);
4254}
4255
Chris Masond3977122009-01-05 21:25:51 -05004256static noinline int get_new_locations(struct inode *reloc_inode,
Zheng Yan1a40e232008-09-26 10:09:34 -04004257 struct btrfs_key *extent_key,
4258 u64 offset, int no_fragment,
4259 struct disk_extent **extents,
4260 int *nr_extents)
4261{
4262 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
4263 struct btrfs_path *path;
4264 struct btrfs_file_extent_item *fi;
4265 struct extent_buffer *leaf;
4266 struct disk_extent *exts = *extents;
4267 struct btrfs_key found_key;
4268 u64 cur_pos;
4269 u64 last_byte;
4270 u32 nritems;
4271 int nr = 0;
4272 int max = *nr_extents;
4273 int ret;
4274
4275 WARN_ON(!no_fragment && *extents);
4276 if (!exts) {
4277 max = 1;
4278 exts = kmalloc(sizeof(*exts) * max, GFP_NOFS);
4279 if (!exts)
4280 return -ENOMEM;
4281 }
4282
4283 path = btrfs_alloc_path();
4284 BUG_ON(!path);
4285
4286 cur_pos = extent_key->objectid - offset;
4287 last_byte = extent_key->objectid + extent_key->offset;
4288 ret = btrfs_lookup_file_extent(NULL, root, path, reloc_inode->i_ino,
4289 cur_pos, 0);
4290 if (ret < 0)
4291 goto out;
4292 if (ret > 0) {
4293 ret = -ENOENT;
4294 goto out;
4295 }
4296
4297 while (1) {
4298 leaf = path->nodes[0];
4299 nritems = btrfs_header_nritems(leaf);
4300 if (path->slots[0] >= nritems) {
4301 ret = btrfs_next_leaf(root, path);
4302 if (ret < 0)
4303 goto out;
4304 if (ret > 0)
4305 break;
4306 leaf = path->nodes[0];
4307 }
4308
4309 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4310 if (found_key.offset != cur_pos ||
4311 found_key.type != BTRFS_EXTENT_DATA_KEY ||
4312 found_key.objectid != reloc_inode->i_ino)
4313 break;
4314
4315 fi = btrfs_item_ptr(leaf, path->slots[0],
4316 struct btrfs_file_extent_item);
4317 if (btrfs_file_extent_type(leaf, fi) !=
4318 BTRFS_FILE_EXTENT_REG ||
4319 btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
4320 break;
4321
4322 if (nr == max) {
4323 struct disk_extent *old = exts;
4324 max *= 2;
4325 exts = kzalloc(sizeof(*exts) * max, GFP_NOFS);
4326 memcpy(exts, old, sizeof(*exts) * nr);
4327 if (old != *extents)
4328 kfree(old);
4329 }
4330
4331 exts[nr].disk_bytenr =
4332 btrfs_file_extent_disk_bytenr(leaf, fi);
4333 exts[nr].disk_num_bytes =
4334 btrfs_file_extent_disk_num_bytes(leaf, fi);
4335 exts[nr].offset = btrfs_file_extent_offset(leaf, fi);
4336 exts[nr].num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
Chris Masonc8b97812008-10-29 14:49:59 -04004337 exts[nr].ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
4338 exts[nr].compression = btrfs_file_extent_compression(leaf, fi);
4339 exts[nr].encryption = btrfs_file_extent_encryption(leaf, fi);
4340 exts[nr].other_encoding = btrfs_file_extent_other_encoding(leaf,
4341 fi);
Yan Zhengd899e052008-10-30 14:25:28 -04004342 BUG_ON(exts[nr].offset > 0);
4343 BUG_ON(exts[nr].compression || exts[nr].encryption);
4344 BUG_ON(exts[nr].num_bytes != exts[nr].disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04004345
4346 cur_pos += exts[nr].num_bytes;
4347 nr++;
4348
4349 if (cur_pos + offset >= last_byte)
4350 break;
4351
4352 if (no_fragment) {
4353 ret = 1;
4354 goto out;
4355 }
4356 path->slots[0]++;
4357 }
4358
Yan Zheng1f80e4d2008-12-19 10:59:04 -05004359 BUG_ON(cur_pos + offset > last_byte);
Zheng Yan1a40e232008-09-26 10:09:34 -04004360 if (cur_pos + offset < last_byte) {
4361 ret = -ENOENT;
4362 goto out;
Chris Masonedbd8d42007-12-21 16:27:24 -05004363 }
4364 ret = 0;
4365out:
Zheng Yan1a40e232008-09-26 10:09:34 -04004366 btrfs_free_path(path);
4367 if (ret) {
4368 if (exts != *extents)
4369 kfree(exts);
4370 } else {
4371 *extents = exts;
4372 *nr_extents = nr;
4373 }
4374 return ret;
4375}
4376
Chris Masond3977122009-01-05 21:25:51 -05004377static noinline int replace_one_extent(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04004378 struct btrfs_root *root,
4379 struct btrfs_path *path,
4380 struct btrfs_key *extent_key,
4381 struct btrfs_key *leaf_key,
4382 struct btrfs_ref_path *ref_path,
4383 struct disk_extent *new_extents,
4384 int nr_extents)
4385{
4386 struct extent_buffer *leaf;
4387 struct btrfs_file_extent_item *fi;
4388 struct inode *inode = NULL;
4389 struct btrfs_key key;
4390 u64 lock_start = 0;
4391 u64 lock_end = 0;
4392 u64 num_bytes;
4393 u64 ext_offset;
Yan Zheng86288a12009-01-21 10:49:16 -05004394 u64 search_end = (u64)-1;
Zheng Yan1a40e232008-09-26 10:09:34 -04004395 u32 nritems;
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004396 int nr_scaned = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04004397 int extent_locked = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04004398 int extent_type;
Zheng Yan1a40e232008-09-26 10:09:34 -04004399 int ret;
4400
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004401 memcpy(&key, leaf_key, sizeof(key));
Zheng Yan1a40e232008-09-26 10:09:34 -04004402 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004403 if (key.objectid < ref_path->owner_objectid ||
4404 (key.objectid == ref_path->owner_objectid &&
4405 key.type < BTRFS_EXTENT_DATA_KEY)) {
4406 key.objectid = ref_path->owner_objectid;
4407 key.type = BTRFS_EXTENT_DATA_KEY;
4408 key.offset = 0;
4409 }
Zheng Yan1a40e232008-09-26 10:09:34 -04004410 }
4411
4412 while (1) {
4413 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
4414 if (ret < 0)
4415 goto out;
4416
4417 leaf = path->nodes[0];
4418 nritems = btrfs_header_nritems(leaf);
4419next:
4420 if (extent_locked && ret > 0) {
4421 /*
4422 * the file extent item was modified by someone
4423 * before the extent got locked.
4424 */
Zheng Yan1a40e232008-09-26 10:09:34 -04004425 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
4426 lock_end, GFP_NOFS);
4427 extent_locked = 0;
4428 }
4429
4430 if (path->slots[0] >= nritems) {
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004431 if (++nr_scaned > 2)
Zheng Yan1a40e232008-09-26 10:09:34 -04004432 break;
4433
4434 BUG_ON(extent_locked);
4435 ret = btrfs_next_leaf(root, path);
4436 if (ret < 0)
4437 goto out;
4438 if (ret > 0)
4439 break;
4440 leaf = path->nodes[0];
4441 nritems = btrfs_header_nritems(leaf);
4442 }
4443
4444 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4445
4446 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
4447 if ((key.objectid > ref_path->owner_objectid) ||
4448 (key.objectid == ref_path->owner_objectid &&
4449 key.type > BTRFS_EXTENT_DATA_KEY) ||
Yan Zheng86288a12009-01-21 10:49:16 -05004450 key.offset >= search_end)
Zheng Yan1a40e232008-09-26 10:09:34 -04004451 break;
4452 }
4453
4454 if (inode && key.objectid != inode->i_ino) {
4455 BUG_ON(extent_locked);
4456 btrfs_release_path(root, path);
4457 mutex_unlock(&inode->i_mutex);
4458 iput(inode);
4459 inode = NULL;
4460 continue;
4461 }
4462
4463 if (key.type != BTRFS_EXTENT_DATA_KEY) {
4464 path->slots[0]++;
4465 ret = 1;
4466 goto next;
4467 }
4468 fi = btrfs_item_ptr(leaf, path->slots[0],
4469 struct btrfs_file_extent_item);
Yan Zhengd899e052008-10-30 14:25:28 -04004470 extent_type = btrfs_file_extent_type(leaf, fi);
4471 if ((extent_type != BTRFS_FILE_EXTENT_REG &&
4472 extent_type != BTRFS_FILE_EXTENT_PREALLOC) ||
Zheng Yan1a40e232008-09-26 10:09:34 -04004473 (btrfs_file_extent_disk_bytenr(leaf, fi) !=
4474 extent_key->objectid)) {
4475 path->slots[0]++;
4476 ret = 1;
4477 goto next;
4478 }
4479
4480 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
4481 ext_offset = btrfs_file_extent_offset(leaf, fi);
4482
Yan Zheng86288a12009-01-21 10:49:16 -05004483 if (search_end == (u64)-1) {
4484 search_end = key.offset - ext_offset +
4485 btrfs_file_extent_ram_bytes(leaf, fi);
4486 }
Zheng Yan1a40e232008-09-26 10:09:34 -04004487
4488 if (!extent_locked) {
4489 lock_start = key.offset;
4490 lock_end = lock_start + num_bytes - 1;
4491 } else {
Yan Zheng66435582008-10-30 14:19:50 -04004492 if (lock_start > key.offset ||
4493 lock_end + 1 < key.offset + num_bytes) {
4494 unlock_extent(&BTRFS_I(inode)->io_tree,
4495 lock_start, lock_end, GFP_NOFS);
4496 extent_locked = 0;
4497 }
Zheng Yan1a40e232008-09-26 10:09:34 -04004498 }
4499
4500 if (!inode) {
4501 btrfs_release_path(root, path);
4502
4503 inode = btrfs_iget_locked(root->fs_info->sb,
4504 key.objectid, root);
4505 if (inode->i_state & I_NEW) {
4506 BTRFS_I(inode)->root = root;
4507 BTRFS_I(inode)->location.objectid =
4508 key.objectid;
4509 BTRFS_I(inode)->location.type =
4510 BTRFS_INODE_ITEM_KEY;
4511 BTRFS_I(inode)->location.offset = 0;
4512 btrfs_read_locked_inode(inode);
4513 unlock_new_inode(inode);
4514 }
4515 /*
4516 * some code call btrfs_commit_transaction while
4517 * holding the i_mutex, so we can't use mutex_lock
4518 * here.
4519 */
4520 if (is_bad_inode(inode) ||
4521 !mutex_trylock(&inode->i_mutex)) {
4522 iput(inode);
4523 inode = NULL;
4524 key.offset = (u64)-1;
4525 goto skip;
4526 }
4527 }
4528
4529 if (!extent_locked) {
4530 struct btrfs_ordered_extent *ordered;
4531
4532 btrfs_release_path(root, path);
4533
4534 lock_extent(&BTRFS_I(inode)->io_tree, lock_start,
4535 lock_end, GFP_NOFS);
4536 ordered = btrfs_lookup_first_ordered_extent(inode,
4537 lock_end);
4538 if (ordered &&
4539 ordered->file_offset <= lock_end &&
4540 ordered->file_offset + ordered->len > lock_start) {
4541 unlock_extent(&BTRFS_I(inode)->io_tree,
4542 lock_start, lock_end, GFP_NOFS);
4543 btrfs_start_ordered_extent(inode, ordered, 1);
4544 btrfs_put_ordered_extent(ordered);
4545 key.offset += num_bytes;
4546 goto skip;
4547 }
4548 if (ordered)
4549 btrfs_put_ordered_extent(ordered);
4550
Zheng Yan1a40e232008-09-26 10:09:34 -04004551 extent_locked = 1;
4552 continue;
4553 }
4554
4555 if (nr_extents == 1) {
4556 /* update extent pointer in place */
Zheng Yan1a40e232008-09-26 10:09:34 -04004557 btrfs_set_file_extent_disk_bytenr(leaf, fi,
4558 new_extents[0].disk_bytenr);
4559 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
4560 new_extents[0].disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04004561 btrfs_mark_buffer_dirty(leaf);
4562
4563 btrfs_drop_extent_cache(inode, key.offset,
4564 key.offset + num_bytes - 1, 0);
4565
4566 ret = btrfs_inc_extent_ref(trans, root,
4567 new_extents[0].disk_bytenr,
4568 new_extents[0].disk_num_bytes,
4569 leaf->start,
4570 root->root_key.objectid,
4571 trans->transid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004572 key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04004573 BUG_ON(ret);
4574
4575 ret = btrfs_free_extent(trans, root,
4576 extent_key->objectid,
4577 extent_key->offset,
4578 leaf->start,
4579 btrfs_header_owner(leaf),
4580 btrfs_header_generation(leaf),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004581 key.objectid, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04004582 BUG_ON(ret);
4583
4584 btrfs_release_path(root, path);
4585 key.offset += num_bytes;
4586 } else {
Yan Zhengd899e052008-10-30 14:25:28 -04004587 BUG_ON(1);
4588#if 0
Zheng Yan1a40e232008-09-26 10:09:34 -04004589 u64 alloc_hint;
4590 u64 extent_len;
4591 int i;
4592 /*
4593 * drop old extent pointer at first, then insert the
4594 * new pointers one bye one
4595 */
4596 btrfs_release_path(root, path);
4597 ret = btrfs_drop_extents(trans, root, inode, key.offset,
4598 key.offset + num_bytes,
4599 key.offset, &alloc_hint);
4600 BUG_ON(ret);
4601
4602 for (i = 0; i < nr_extents; i++) {
4603 if (ext_offset >= new_extents[i].num_bytes) {
4604 ext_offset -= new_extents[i].num_bytes;
4605 continue;
4606 }
4607 extent_len = min(new_extents[i].num_bytes -
4608 ext_offset, num_bytes);
4609
4610 ret = btrfs_insert_empty_item(trans, root,
4611 path, &key,
4612 sizeof(*fi));
4613 BUG_ON(ret);
4614
4615 leaf = path->nodes[0];
4616 fi = btrfs_item_ptr(leaf, path->slots[0],
4617 struct btrfs_file_extent_item);
4618 btrfs_set_file_extent_generation(leaf, fi,
4619 trans->transid);
4620 btrfs_set_file_extent_type(leaf, fi,
4621 BTRFS_FILE_EXTENT_REG);
4622 btrfs_set_file_extent_disk_bytenr(leaf, fi,
4623 new_extents[i].disk_bytenr);
4624 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
4625 new_extents[i].disk_num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -04004626 btrfs_set_file_extent_ram_bytes(leaf, fi,
4627 new_extents[i].ram_bytes);
4628
4629 btrfs_set_file_extent_compression(leaf, fi,
4630 new_extents[i].compression);
4631 btrfs_set_file_extent_encryption(leaf, fi,
4632 new_extents[i].encryption);
4633 btrfs_set_file_extent_other_encoding(leaf, fi,
4634 new_extents[i].other_encoding);
4635
Zheng Yan1a40e232008-09-26 10:09:34 -04004636 btrfs_set_file_extent_num_bytes(leaf, fi,
4637 extent_len);
4638 ext_offset += new_extents[i].offset;
4639 btrfs_set_file_extent_offset(leaf, fi,
4640 ext_offset);
4641 btrfs_mark_buffer_dirty(leaf);
4642
4643 btrfs_drop_extent_cache(inode, key.offset,
4644 key.offset + extent_len - 1, 0);
4645
4646 ret = btrfs_inc_extent_ref(trans, root,
4647 new_extents[i].disk_bytenr,
4648 new_extents[i].disk_num_bytes,
4649 leaf->start,
4650 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004651 trans->transid, key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04004652 BUG_ON(ret);
4653 btrfs_release_path(root, path);
4654
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004655 inode_add_bytes(inode, extent_len);
Zheng Yan1a40e232008-09-26 10:09:34 -04004656
4657 ext_offset = 0;
4658 num_bytes -= extent_len;
4659 key.offset += extent_len;
4660
4661 if (num_bytes == 0)
4662 break;
4663 }
4664 BUG_ON(i >= nr_extents);
Yan Zhengd899e052008-10-30 14:25:28 -04004665#endif
Zheng Yan1a40e232008-09-26 10:09:34 -04004666 }
4667
4668 if (extent_locked) {
Zheng Yan1a40e232008-09-26 10:09:34 -04004669 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
4670 lock_end, GFP_NOFS);
4671 extent_locked = 0;
4672 }
4673skip:
4674 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS &&
Yan Zheng86288a12009-01-21 10:49:16 -05004675 key.offset >= search_end)
Zheng Yan1a40e232008-09-26 10:09:34 -04004676 break;
4677
4678 cond_resched();
4679 }
4680 ret = 0;
4681out:
4682 btrfs_release_path(root, path);
4683 if (inode) {
4684 mutex_unlock(&inode->i_mutex);
4685 if (extent_locked) {
Zheng Yan1a40e232008-09-26 10:09:34 -04004686 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
4687 lock_end, GFP_NOFS);
4688 }
4689 iput(inode);
4690 }
4691 return ret;
4692}
4693
Zheng Yan1a40e232008-09-26 10:09:34 -04004694int btrfs_reloc_tree_cache_ref(struct btrfs_trans_handle *trans,
4695 struct btrfs_root *root,
4696 struct extent_buffer *buf, u64 orig_start)
4697{
4698 int level;
4699 int ret;
4700
4701 BUG_ON(btrfs_header_generation(buf) != trans->transid);
4702 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
4703
4704 level = btrfs_header_level(buf);
4705 if (level == 0) {
4706 struct btrfs_leaf_ref *ref;
4707 struct btrfs_leaf_ref *orig_ref;
4708
4709 orig_ref = btrfs_lookup_leaf_ref(root, orig_start);
4710 if (!orig_ref)
4711 return -ENOENT;
4712
4713 ref = btrfs_alloc_leaf_ref(root, orig_ref->nritems);
4714 if (!ref) {
4715 btrfs_free_leaf_ref(root, orig_ref);
4716 return -ENOMEM;
4717 }
4718
4719 ref->nritems = orig_ref->nritems;
4720 memcpy(ref->extents, orig_ref->extents,
4721 sizeof(ref->extents[0]) * ref->nritems);
4722
4723 btrfs_free_leaf_ref(root, orig_ref);
4724
4725 ref->root_gen = trans->transid;
4726 ref->bytenr = buf->start;
4727 ref->owner = btrfs_header_owner(buf);
4728 ref->generation = btrfs_header_generation(buf);
Chris Masonbd56b302009-02-04 09:27:02 -05004729
Zheng Yan1a40e232008-09-26 10:09:34 -04004730 ret = btrfs_add_leaf_ref(root, ref, 0);
4731 WARN_ON(ret);
4732 btrfs_free_leaf_ref(root, ref);
4733 }
4734 return 0;
4735}
4736
Chris Masond3977122009-01-05 21:25:51 -05004737static noinline int invalidate_extent_cache(struct btrfs_root *root,
Zheng Yan1a40e232008-09-26 10:09:34 -04004738 struct extent_buffer *leaf,
4739 struct btrfs_block_group_cache *group,
4740 struct btrfs_root *target_root)
4741{
4742 struct btrfs_key key;
4743 struct inode *inode = NULL;
4744 struct btrfs_file_extent_item *fi;
4745 u64 num_bytes;
4746 u64 skip_objectid = 0;
4747 u32 nritems;
4748 u32 i;
4749
4750 nritems = btrfs_header_nritems(leaf);
4751 for (i = 0; i < nritems; i++) {
4752 btrfs_item_key_to_cpu(leaf, &key, i);
4753 if (key.objectid == skip_objectid ||
4754 key.type != BTRFS_EXTENT_DATA_KEY)
4755 continue;
4756 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
4757 if (btrfs_file_extent_type(leaf, fi) ==
4758 BTRFS_FILE_EXTENT_INLINE)
4759 continue;
4760 if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
4761 continue;
4762 if (!inode || inode->i_ino != key.objectid) {
4763 iput(inode);
4764 inode = btrfs_ilookup(target_root->fs_info->sb,
4765 key.objectid, target_root, 1);
4766 }
4767 if (!inode) {
4768 skip_objectid = key.objectid;
4769 continue;
4770 }
4771 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
4772
4773 lock_extent(&BTRFS_I(inode)->io_tree, key.offset,
4774 key.offset + num_bytes - 1, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04004775 btrfs_drop_extent_cache(inode, key.offset,
4776 key.offset + num_bytes - 1, 1);
Zheng Yan1a40e232008-09-26 10:09:34 -04004777 unlock_extent(&BTRFS_I(inode)->io_tree, key.offset,
4778 key.offset + num_bytes - 1, GFP_NOFS);
4779 cond_resched();
4780 }
4781 iput(inode);
4782 return 0;
4783}
4784
Chris Masond3977122009-01-05 21:25:51 -05004785static noinline int replace_extents_in_leaf(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04004786 struct btrfs_root *root,
4787 struct extent_buffer *leaf,
4788 struct btrfs_block_group_cache *group,
4789 struct inode *reloc_inode)
4790{
4791 struct btrfs_key key;
4792 struct btrfs_key extent_key;
4793 struct btrfs_file_extent_item *fi;
4794 struct btrfs_leaf_ref *ref;
4795 struct disk_extent *new_extent;
4796 u64 bytenr;
4797 u64 num_bytes;
4798 u32 nritems;
4799 u32 i;
4800 int ext_index;
4801 int nr_extent;
4802 int ret;
4803
4804 new_extent = kmalloc(sizeof(*new_extent), GFP_NOFS);
4805 BUG_ON(!new_extent);
4806
4807 ref = btrfs_lookup_leaf_ref(root, leaf->start);
4808 BUG_ON(!ref);
4809
4810 ext_index = -1;
4811 nritems = btrfs_header_nritems(leaf);
4812 for (i = 0; i < nritems; i++) {
4813 btrfs_item_key_to_cpu(leaf, &key, i);
4814 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
4815 continue;
4816 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
4817 if (btrfs_file_extent_type(leaf, fi) ==
4818 BTRFS_FILE_EXTENT_INLINE)
4819 continue;
4820 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
4821 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
4822 if (bytenr == 0)
4823 continue;
4824
4825 ext_index++;
4826 if (bytenr >= group->key.objectid + group->key.offset ||
4827 bytenr + num_bytes <= group->key.objectid)
4828 continue;
4829
4830 extent_key.objectid = bytenr;
4831 extent_key.offset = num_bytes;
4832 extent_key.type = BTRFS_EXTENT_ITEM_KEY;
4833 nr_extent = 1;
4834 ret = get_new_locations(reloc_inode, &extent_key,
4835 group->key.objectid, 1,
4836 &new_extent, &nr_extent);
4837 if (ret > 0)
4838 continue;
4839 BUG_ON(ret < 0);
4840
4841 BUG_ON(ref->extents[ext_index].bytenr != bytenr);
4842 BUG_ON(ref->extents[ext_index].num_bytes != num_bytes);
4843 ref->extents[ext_index].bytenr = new_extent->disk_bytenr;
4844 ref->extents[ext_index].num_bytes = new_extent->disk_num_bytes;
4845
Zheng Yan1a40e232008-09-26 10:09:34 -04004846 btrfs_set_file_extent_disk_bytenr(leaf, fi,
4847 new_extent->disk_bytenr);
4848 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
4849 new_extent->disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04004850 btrfs_mark_buffer_dirty(leaf);
4851
4852 ret = btrfs_inc_extent_ref(trans, root,
4853 new_extent->disk_bytenr,
4854 new_extent->disk_num_bytes,
4855 leaf->start,
4856 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004857 trans->transid, key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04004858 BUG_ON(ret);
Chris Mason56bec292009-03-13 10:10:06 -04004859
Zheng Yan1a40e232008-09-26 10:09:34 -04004860 ret = btrfs_free_extent(trans, root,
4861 bytenr, num_bytes, leaf->start,
4862 btrfs_header_owner(leaf),
4863 btrfs_header_generation(leaf),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004864 key.objectid, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04004865 BUG_ON(ret);
4866 cond_resched();
4867 }
4868 kfree(new_extent);
4869 BUG_ON(ext_index + 1 != ref->nritems);
4870 btrfs_free_leaf_ref(root, ref);
4871 return 0;
4872}
4873
Yan Zhengf82d02d2008-10-29 14:49:05 -04004874int btrfs_free_reloc_root(struct btrfs_trans_handle *trans,
4875 struct btrfs_root *root)
Zheng Yan1a40e232008-09-26 10:09:34 -04004876{
4877 struct btrfs_root *reloc_root;
Yan Zhengf82d02d2008-10-29 14:49:05 -04004878 int ret;
Zheng Yan1a40e232008-09-26 10:09:34 -04004879
4880 if (root->reloc_root) {
4881 reloc_root = root->reloc_root;
4882 root->reloc_root = NULL;
4883 list_add(&reloc_root->dead_list,
4884 &root->fs_info->dead_reloc_roots);
Yan Zhengf82d02d2008-10-29 14:49:05 -04004885
4886 btrfs_set_root_bytenr(&reloc_root->root_item,
4887 reloc_root->node->start);
4888 btrfs_set_root_level(&root->root_item,
4889 btrfs_header_level(reloc_root->node));
4890 memset(&reloc_root->root_item.drop_progress, 0,
4891 sizeof(struct btrfs_disk_key));
4892 reloc_root->root_item.drop_level = 0;
4893
4894 ret = btrfs_update_root(trans, root->fs_info->tree_root,
4895 &reloc_root->root_key,
4896 &reloc_root->root_item);
4897 BUG_ON(ret);
Zheng Yan1a40e232008-09-26 10:09:34 -04004898 }
4899 return 0;
4900}
4901
4902int btrfs_drop_dead_reloc_roots(struct btrfs_root *root)
4903{
4904 struct btrfs_trans_handle *trans;
4905 struct btrfs_root *reloc_root;
4906 struct btrfs_root *prev_root = NULL;
4907 struct list_head dead_roots;
4908 int ret;
4909 unsigned long nr;
4910
4911 INIT_LIST_HEAD(&dead_roots);
4912 list_splice_init(&root->fs_info->dead_reloc_roots, &dead_roots);
4913
4914 while (!list_empty(&dead_roots)) {
4915 reloc_root = list_entry(dead_roots.prev,
4916 struct btrfs_root, dead_list);
4917 list_del_init(&reloc_root->dead_list);
4918
4919 BUG_ON(reloc_root->commit_root != NULL);
4920 while (1) {
4921 trans = btrfs_join_transaction(root, 1);
4922 BUG_ON(!trans);
4923
4924 mutex_lock(&root->fs_info->drop_mutex);
4925 ret = btrfs_drop_snapshot(trans, reloc_root);
4926 if (ret != -EAGAIN)
4927 break;
4928 mutex_unlock(&root->fs_info->drop_mutex);
4929
4930 nr = trans->blocks_used;
4931 ret = btrfs_end_transaction(trans, root);
4932 BUG_ON(ret);
4933 btrfs_btree_balance_dirty(root, nr);
4934 }
4935
4936 free_extent_buffer(reloc_root->node);
4937
4938 ret = btrfs_del_root(trans, root->fs_info->tree_root,
4939 &reloc_root->root_key);
4940 BUG_ON(ret);
4941 mutex_unlock(&root->fs_info->drop_mutex);
4942
4943 nr = trans->blocks_used;
4944 ret = btrfs_end_transaction(trans, root);
4945 BUG_ON(ret);
4946 btrfs_btree_balance_dirty(root, nr);
4947
4948 kfree(prev_root);
4949 prev_root = reloc_root;
4950 }
4951 if (prev_root) {
4952 btrfs_remove_leaf_refs(prev_root, (u64)-1, 0);
4953 kfree(prev_root);
4954 }
4955 return 0;
4956}
4957
4958int btrfs_add_dead_reloc_root(struct btrfs_root *root)
4959{
4960 list_add(&root->dead_list, &root->fs_info->dead_reloc_roots);
4961 return 0;
4962}
4963
4964int btrfs_cleanup_reloc_trees(struct btrfs_root *root)
4965{
4966 struct btrfs_root *reloc_root;
4967 struct btrfs_trans_handle *trans;
4968 struct btrfs_key location;
4969 int found;
4970 int ret;
4971
4972 mutex_lock(&root->fs_info->tree_reloc_mutex);
4973 ret = btrfs_find_dead_roots(root, BTRFS_TREE_RELOC_OBJECTID, NULL);
4974 BUG_ON(ret);
4975 found = !list_empty(&root->fs_info->dead_reloc_roots);
4976 mutex_unlock(&root->fs_info->tree_reloc_mutex);
4977
4978 if (found) {
4979 trans = btrfs_start_transaction(root, 1);
4980 BUG_ON(!trans);
4981 ret = btrfs_commit_transaction(trans, root);
4982 BUG_ON(ret);
4983 }
4984
4985 location.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
4986 location.offset = (u64)-1;
4987 location.type = BTRFS_ROOT_ITEM_KEY;
4988
4989 reloc_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
4990 BUG_ON(!reloc_root);
4991 btrfs_orphan_cleanup(reloc_root);
4992 return 0;
4993}
4994
Chris Masond3977122009-01-05 21:25:51 -05004995static noinline int init_reloc_tree(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04004996 struct btrfs_root *root)
4997{
4998 struct btrfs_root *reloc_root;
4999 struct extent_buffer *eb;
5000 struct btrfs_root_item *root_item;
5001 struct btrfs_key root_key;
5002 int ret;
5003
5004 BUG_ON(!root->ref_cows);
5005 if (root->reloc_root)
5006 return 0;
5007
5008 root_item = kmalloc(sizeof(*root_item), GFP_NOFS);
5009 BUG_ON(!root_item);
5010
5011 ret = btrfs_copy_root(trans, root, root->commit_root,
5012 &eb, BTRFS_TREE_RELOC_OBJECTID);
5013 BUG_ON(ret);
5014
5015 root_key.objectid = BTRFS_TREE_RELOC_OBJECTID;
5016 root_key.offset = root->root_key.objectid;
5017 root_key.type = BTRFS_ROOT_ITEM_KEY;
5018
5019 memcpy(root_item, &root->root_item, sizeof(root_item));
5020 btrfs_set_root_refs(root_item, 0);
5021 btrfs_set_root_bytenr(root_item, eb->start);
5022 btrfs_set_root_level(root_item, btrfs_header_level(eb));
Yan Zheng84234f32008-10-29 14:49:05 -04005023 btrfs_set_root_generation(root_item, trans->transid);
Zheng Yan1a40e232008-09-26 10:09:34 -04005024
5025 btrfs_tree_unlock(eb);
5026 free_extent_buffer(eb);
5027
5028 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
5029 &root_key, root_item);
5030 BUG_ON(ret);
5031 kfree(root_item);
5032
5033 reloc_root = btrfs_read_fs_root_no_radix(root->fs_info->tree_root,
5034 &root_key);
5035 BUG_ON(!reloc_root);
5036 reloc_root->last_trans = trans->transid;
5037 reloc_root->commit_root = NULL;
5038 reloc_root->ref_tree = &root->fs_info->reloc_ref_tree;
5039
5040 root->reloc_root = reloc_root;
5041 return 0;
5042}
5043
5044/*
5045 * Core function of space balance.
5046 *
5047 * The idea is using reloc trees to relocate tree blocks in reference
Yan Zhengf82d02d2008-10-29 14:49:05 -04005048 * counted roots. There is one reloc tree for each subvol, and all
5049 * reloc trees share same root key objectid. Reloc trees are snapshots
5050 * of the latest committed roots of subvols (root->commit_root).
5051 *
5052 * To relocate a tree block referenced by a subvol, there are two steps.
5053 * COW the block through subvol's reloc tree, then update block pointer
5054 * in the subvol to point to the new block. Since all reloc trees share
5055 * same root key objectid, doing special handing for tree blocks owned
5056 * by them is easy. Once a tree block has been COWed in one reloc tree,
5057 * we can use the resulting new block directly when the same block is
5058 * required to COW again through other reloc trees. By this way, relocated
5059 * tree blocks are shared between reloc trees, so they are also shared
5060 * between subvols.
Zheng Yan1a40e232008-09-26 10:09:34 -04005061 */
Chris Masond3977122009-01-05 21:25:51 -05005062static noinline int relocate_one_path(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04005063 struct btrfs_root *root,
5064 struct btrfs_path *path,
5065 struct btrfs_key *first_key,
5066 struct btrfs_ref_path *ref_path,
5067 struct btrfs_block_group_cache *group,
5068 struct inode *reloc_inode)
5069{
5070 struct btrfs_root *reloc_root;
5071 struct extent_buffer *eb = NULL;
5072 struct btrfs_key *keys;
5073 u64 *nodes;
5074 int level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005075 int shared_level;
Zheng Yan1a40e232008-09-26 10:09:34 -04005076 int lowest_level = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04005077 int ret;
5078
5079 if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
5080 lowest_level = ref_path->owner_objectid;
5081
Yan Zhengf82d02d2008-10-29 14:49:05 -04005082 if (!root->ref_cows) {
Zheng Yan1a40e232008-09-26 10:09:34 -04005083 path->lowest_level = lowest_level;
5084 ret = btrfs_search_slot(trans, root, first_key, path, 0, 1);
5085 BUG_ON(ret < 0);
5086 path->lowest_level = 0;
5087 btrfs_release_path(root, path);
5088 return 0;
5089 }
5090
Zheng Yan1a40e232008-09-26 10:09:34 -04005091 mutex_lock(&root->fs_info->tree_reloc_mutex);
5092 ret = init_reloc_tree(trans, root);
5093 BUG_ON(ret);
5094 reloc_root = root->reloc_root;
5095
Yan Zhengf82d02d2008-10-29 14:49:05 -04005096 shared_level = ref_path->shared_level;
5097 ref_path->shared_level = BTRFS_MAX_LEVEL - 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04005098
Yan Zhengf82d02d2008-10-29 14:49:05 -04005099 keys = ref_path->node_keys;
5100 nodes = ref_path->new_nodes;
5101 memset(&keys[shared_level + 1], 0,
5102 sizeof(*keys) * (BTRFS_MAX_LEVEL - shared_level - 1));
5103 memset(&nodes[shared_level + 1], 0,
5104 sizeof(*nodes) * (BTRFS_MAX_LEVEL - shared_level - 1));
Zheng Yan1a40e232008-09-26 10:09:34 -04005105
Yan Zhengf82d02d2008-10-29 14:49:05 -04005106 if (nodes[lowest_level] == 0) {
5107 path->lowest_level = lowest_level;
5108 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
5109 0, 1);
5110 BUG_ON(ret);
5111 for (level = lowest_level; level < BTRFS_MAX_LEVEL; level++) {
5112 eb = path->nodes[level];
5113 if (!eb || eb == reloc_root->node)
5114 break;
5115 nodes[level] = eb->start;
5116 if (level == 0)
5117 btrfs_item_key_to_cpu(eb, &keys[level], 0);
5118 else
5119 btrfs_node_key_to_cpu(eb, &keys[level], 0);
5120 }
Yan Zheng2b820322008-11-17 21:11:30 -05005121 if (nodes[0] &&
5122 ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04005123 eb = path->nodes[0];
5124 ret = replace_extents_in_leaf(trans, reloc_root, eb,
5125 group, reloc_inode);
5126 BUG_ON(ret);
5127 }
5128 btrfs_release_path(reloc_root, path);
5129 } else {
Zheng Yan1a40e232008-09-26 10:09:34 -04005130 ret = btrfs_merge_path(trans, reloc_root, keys, nodes,
Yan Zhengf82d02d2008-10-29 14:49:05 -04005131 lowest_level);
Zheng Yan1a40e232008-09-26 10:09:34 -04005132 BUG_ON(ret);
5133 }
5134
Zheng Yan1a40e232008-09-26 10:09:34 -04005135 /*
5136 * replace tree blocks in the fs tree with tree blocks in
5137 * the reloc tree.
5138 */
5139 ret = btrfs_merge_path(trans, root, keys, nodes, lowest_level);
5140 BUG_ON(ret < 0);
5141
5142 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04005143 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
5144 0, 0);
5145 BUG_ON(ret);
5146 extent_buffer_get(path->nodes[0]);
5147 eb = path->nodes[0];
5148 btrfs_release_path(reloc_root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04005149 ret = invalidate_extent_cache(reloc_root, eb, group, root);
5150 BUG_ON(ret);
5151 free_extent_buffer(eb);
5152 }
Zheng Yan1a40e232008-09-26 10:09:34 -04005153
Yan Zhengf82d02d2008-10-29 14:49:05 -04005154 mutex_unlock(&root->fs_info->tree_reloc_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04005155 path->lowest_level = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04005156 return 0;
5157}
5158
Chris Masond3977122009-01-05 21:25:51 -05005159static noinline int relocate_tree_block(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04005160 struct btrfs_root *root,
5161 struct btrfs_path *path,
5162 struct btrfs_key *first_key,
5163 struct btrfs_ref_path *ref_path)
5164{
5165 int ret;
Zheng Yan1a40e232008-09-26 10:09:34 -04005166
5167 ret = relocate_one_path(trans, root, path, first_key,
5168 ref_path, NULL, NULL);
5169 BUG_ON(ret);
5170
Zheng Yan1a40e232008-09-26 10:09:34 -04005171 return 0;
5172}
5173
Chris Masond3977122009-01-05 21:25:51 -05005174static noinline int del_extent_zero(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04005175 struct btrfs_root *extent_root,
5176 struct btrfs_path *path,
5177 struct btrfs_key *extent_key)
5178{
5179 int ret;
5180
Zheng Yan1a40e232008-09-26 10:09:34 -04005181 ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1);
5182 if (ret)
5183 goto out;
5184 ret = btrfs_del_item(trans, extent_root, path);
5185out:
Chris Masonedbd8d42007-12-21 16:27:24 -05005186 btrfs_release_path(extent_root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04005187 return ret;
5188}
5189
Chris Masond3977122009-01-05 21:25:51 -05005190static noinline struct btrfs_root *read_ref_root(struct btrfs_fs_info *fs_info,
Zheng Yan1a40e232008-09-26 10:09:34 -04005191 struct btrfs_ref_path *ref_path)
5192{
5193 struct btrfs_key root_key;
5194
5195 root_key.objectid = ref_path->root_objectid;
5196 root_key.type = BTRFS_ROOT_ITEM_KEY;
5197 if (is_cowonly_root(ref_path->root_objectid))
5198 root_key.offset = 0;
5199 else
5200 root_key.offset = (u64)-1;
5201
5202 return btrfs_read_fs_root_no_name(fs_info, &root_key);
5203}
5204
Chris Masond3977122009-01-05 21:25:51 -05005205static noinline int relocate_one_extent(struct btrfs_root *extent_root,
Zheng Yan1a40e232008-09-26 10:09:34 -04005206 struct btrfs_path *path,
5207 struct btrfs_key *extent_key,
5208 struct btrfs_block_group_cache *group,
5209 struct inode *reloc_inode, int pass)
5210{
5211 struct btrfs_trans_handle *trans;
5212 struct btrfs_root *found_root;
5213 struct btrfs_ref_path *ref_path = NULL;
5214 struct disk_extent *new_extents = NULL;
5215 int nr_extents = 0;
5216 int loops;
5217 int ret;
5218 int level;
5219 struct btrfs_key first_key;
5220 u64 prev_block = 0;
5221
Zheng Yan1a40e232008-09-26 10:09:34 -04005222
5223 trans = btrfs_start_transaction(extent_root, 1);
5224 BUG_ON(!trans);
5225
5226 if (extent_key->objectid == 0) {
5227 ret = del_extent_zero(trans, extent_root, path, extent_key);
5228 goto out;
5229 }
5230
5231 ref_path = kmalloc(sizeof(*ref_path), GFP_NOFS);
5232 if (!ref_path) {
Chris Masond3977122009-01-05 21:25:51 -05005233 ret = -ENOMEM;
5234 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04005235 }
5236
5237 for (loops = 0; ; loops++) {
5238 if (loops == 0) {
5239 ret = btrfs_first_ref_path(trans, extent_root, ref_path,
5240 extent_key->objectid);
5241 } else {
5242 ret = btrfs_next_ref_path(trans, extent_root, ref_path);
5243 }
5244 if (ret < 0)
5245 goto out;
5246 if (ret > 0)
5247 break;
5248
5249 if (ref_path->root_objectid == BTRFS_TREE_LOG_OBJECTID ||
5250 ref_path->root_objectid == BTRFS_TREE_RELOC_OBJECTID)
5251 continue;
5252
5253 found_root = read_ref_root(extent_root->fs_info, ref_path);
5254 BUG_ON(!found_root);
5255 /*
5256 * for reference counted tree, only process reference paths
5257 * rooted at the latest committed root.
5258 */
5259 if (found_root->ref_cows &&
5260 ref_path->root_generation != found_root->root_key.offset)
5261 continue;
5262
5263 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
5264 if (pass == 0) {
5265 /*
5266 * copy data extents to new locations
5267 */
5268 u64 group_start = group->key.objectid;
5269 ret = relocate_data_extent(reloc_inode,
5270 extent_key,
5271 group_start);
5272 if (ret < 0)
5273 goto out;
5274 break;
5275 }
5276 level = 0;
5277 } else {
5278 level = ref_path->owner_objectid;
5279 }
5280
5281 if (prev_block != ref_path->nodes[level]) {
5282 struct extent_buffer *eb;
5283 u64 block_start = ref_path->nodes[level];
5284 u64 block_size = btrfs_level_size(found_root, level);
5285
5286 eb = read_tree_block(found_root, block_start,
5287 block_size, 0);
5288 btrfs_tree_lock(eb);
5289 BUG_ON(level != btrfs_header_level(eb));
5290
5291 if (level == 0)
5292 btrfs_item_key_to_cpu(eb, &first_key, 0);
5293 else
5294 btrfs_node_key_to_cpu(eb, &first_key, 0);
5295
5296 btrfs_tree_unlock(eb);
5297 free_extent_buffer(eb);
5298 prev_block = block_start;
5299 }
5300
Yan Zheng24562422009-02-12 14:14:53 -05005301 mutex_lock(&extent_root->fs_info->trans_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05005302 btrfs_record_root_in_trans(found_root);
Yan Zheng24562422009-02-12 14:14:53 -05005303 mutex_unlock(&extent_root->fs_info->trans_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05005304 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
5305 /*
5306 * try to update data extent references while
5307 * keeping metadata shared between snapshots.
5308 */
5309 if (pass == 1) {
5310 ret = relocate_one_path(trans, found_root,
5311 path, &first_key, ref_path,
5312 group, reloc_inode);
5313 if (ret < 0)
5314 goto out;
5315 continue;
5316 }
Zheng Yan1a40e232008-09-26 10:09:34 -04005317 /*
5318 * use fallback method to process the remaining
5319 * references.
5320 */
5321 if (!new_extents) {
5322 u64 group_start = group->key.objectid;
Yan Zhengd899e052008-10-30 14:25:28 -04005323 new_extents = kmalloc(sizeof(*new_extents),
5324 GFP_NOFS);
5325 nr_extents = 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04005326 ret = get_new_locations(reloc_inode,
5327 extent_key,
Yan Zhengd899e052008-10-30 14:25:28 -04005328 group_start, 1,
Zheng Yan1a40e232008-09-26 10:09:34 -04005329 &new_extents,
5330 &nr_extents);
Yan Zhengd899e052008-10-30 14:25:28 -04005331 if (ret)
Zheng Yan1a40e232008-09-26 10:09:34 -04005332 goto out;
5333 }
Zheng Yan1a40e232008-09-26 10:09:34 -04005334 ret = replace_one_extent(trans, found_root,
5335 path, extent_key,
5336 &first_key, ref_path,
5337 new_extents, nr_extents);
Yan Zhenge4404d62008-12-12 10:03:26 -05005338 } else {
Zheng Yan1a40e232008-09-26 10:09:34 -04005339 ret = relocate_tree_block(trans, found_root, path,
5340 &first_key, ref_path);
Zheng Yan1a40e232008-09-26 10:09:34 -04005341 }
5342 if (ret < 0)
5343 goto out;
5344 }
5345 ret = 0;
5346out:
5347 btrfs_end_transaction(trans, extent_root);
5348 kfree(new_extents);
5349 kfree(ref_path);
Chris Masonedbd8d42007-12-21 16:27:24 -05005350 return ret;
5351}
5352
Chris Masonec44a352008-04-28 15:29:52 -04005353static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
5354{
5355 u64 num_devices;
5356 u64 stripped = BTRFS_BLOCK_GROUP_RAID0 |
5357 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
5358
Yan Zheng2b820322008-11-17 21:11:30 -05005359 num_devices = root->fs_info->fs_devices->rw_devices;
Chris Masonec44a352008-04-28 15:29:52 -04005360 if (num_devices == 1) {
5361 stripped |= BTRFS_BLOCK_GROUP_DUP;
5362 stripped = flags & ~stripped;
5363
5364 /* turn raid0 into single device chunks */
5365 if (flags & BTRFS_BLOCK_GROUP_RAID0)
5366 return stripped;
5367
5368 /* turn mirroring into duplication */
5369 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
5370 BTRFS_BLOCK_GROUP_RAID10))
5371 return stripped | BTRFS_BLOCK_GROUP_DUP;
5372 return flags;
5373 } else {
5374 /* they already had raid on here, just return */
Chris Masonec44a352008-04-28 15:29:52 -04005375 if (flags & stripped)
5376 return flags;
5377
5378 stripped |= BTRFS_BLOCK_GROUP_DUP;
5379 stripped = flags & ~stripped;
5380
5381 /* switch duplicated blocks with raid1 */
5382 if (flags & BTRFS_BLOCK_GROUP_DUP)
5383 return stripped | BTRFS_BLOCK_GROUP_RAID1;
5384
5385 /* turn single device chunks into raid0 */
5386 return stripped | BTRFS_BLOCK_GROUP_RAID0;
5387 }
5388 return flags;
5389}
5390
Christoph Hellwigb2950862008-12-02 09:54:17 -05005391static int __alloc_chunk_for_shrink(struct btrfs_root *root,
Chris Mason0ef3e662008-05-24 14:04:53 -04005392 struct btrfs_block_group_cache *shrink_block_group,
5393 int force)
5394{
5395 struct btrfs_trans_handle *trans;
5396 u64 new_alloc_flags;
5397 u64 calc;
5398
Chris Masonc286ac42008-07-22 23:06:41 -04005399 spin_lock(&shrink_block_group->lock);
Chris Mason0ef3e662008-05-24 14:04:53 -04005400 if (btrfs_block_group_used(&shrink_block_group->item) > 0) {
Chris Masonc286ac42008-07-22 23:06:41 -04005401 spin_unlock(&shrink_block_group->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04005402
Chris Mason0ef3e662008-05-24 14:04:53 -04005403 trans = btrfs_start_transaction(root, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04005404 spin_lock(&shrink_block_group->lock);
Chris Mason7d9eb122008-07-08 14:19:17 -04005405
Chris Mason0ef3e662008-05-24 14:04:53 -04005406 new_alloc_flags = update_block_group_flags(root,
5407 shrink_block_group->flags);
5408 if (new_alloc_flags != shrink_block_group->flags) {
5409 calc =
5410 btrfs_block_group_used(&shrink_block_group->item);
5411 } else {
5412 calc = shrink_block_group->key.offset;
5413 }
Chris Masonc286ac42008-07-22 23:06:41 -04005414 spin_unlock(&shrink_block_group->lock);
5415
Chris Mason0ef3e662008-05-24 14:04:53 -04005416 do_chunk_alloc(trans, root->fs_info->extent_root,
5417 calc + 2 * 1024 * 1024, new_alloc_flags, force);
Chris Mason7d9eb122008-07-08 14:19:17 -04005418
Chris Mason0ef3e662008-05-24 14:04:53 -04005419 btrfs_end_transaction(trans, root);
Chris Masonc286ac42008-07-22 23:06:41 -04005420 } else
5421 spin_unlock(&shrink_block_group->lock);
Chris Mason0ef3e662008-05-24 14:04:53 -04005422 return 0;
5423}
5424
Zheng Yan1a40e232008-09-26 10:09:34 -04005425static int __insert_orphan_inode(struct btrfs_trans_handle *trans,
5426 struct btrfs_root *root,
5427 u64 objectid, u64 size)
5428{
5429 struct btrfs_path *path;
5430 struct btrfs_inode_item *item;
5431 struct extent_buffer *leaf;
5432 int ret;
5433
5434 path = btrfs_alloc_path();
5435 if (!path)
5436 return -ENOMEM;
5437
5438 ret = btrfs_insert_empty_inode(trans, root, path, objectid);
5439 if (ret)
5440 goto out;
5441
5442 leaf = path->nodes[0];
5443 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_inode_item);
5444 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
5445 btrfs_set_inode_generation(leaf, item, 1);
5446 btrfs_set_inode_size(leaf, item, size);
5447 btrfs_set_inode_mode(leaf, item, S_IFREG | 0600);
Yan Zheng0403e472008-12-10 20:32:51 -05005448 btrfs_set_inode_flags(leaf, item, BTRFS_INODE_NOCOMPRESS);
Zheng Yan1a40e232008-09-26 10:09:34 -04005449 btrfs_mark_buffer_dirty(leaf);
5450 btrfs_release_path(root, path);
5451out:
5452 btrfs_free_path(path);
5453 return ret;
5454}
5455
Chris Masond3977122009-01-05 21:25:51 -05005456static noinline struct inode *create_reloc_inode(struct btrfs_fs_info *fs_info,
Zheng Yan1a40e232008-09-26 10:09:34 -04005457 struct btrfs_block_group_cache *group)
5458{
5459 struct inode *inode = NULL;
5460 struct btrfs_trans_handle *trans;
5461 struct btrfs_root *root;
5462 struct btrfs_key root_key;
5463 u64 objectid = BTRFS_FIRST_FREE_OBJECTID;
5464 int err = 0;
5465
5466 root_key.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
5467 root_key.type = BTRFS_ROOT_ITEM_KEY;
5468 root_key.offset = (u64)-1;
5469 root = btrfs_read_fs_root_no_name(fs_info, &root_key);
5470 if (IS_ERR(root))
5471 return ERR_CAST(root);
5472
5473 trans = btrfs_start_transaction(root, 1);
5474 BUG_ON(!trans);
5475
5476 err = btrfs_find_free_objectid(trans, root, objectid, &objectid);
5477 if (err)
5478 goto out;
5479
5480 err = __insert_orphan_inode(trans, root, objectid, group->key.offset);
5481 BUG_ON(err);
5482
5483 err = btrfs_insert_file_extent(trans, root, objectid, 0, 0, 0,
Chris Masonc8b97812008-10-29 14:49:59 -04005484 group->key.offset, 0, group->key.offset,
5485 0, 0, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04005486 BUG_ON(err);
5487
5488 inode = btrfs_iget_locked(root->fs_info->sb, objectid, root);
5489 if (inode->i_state & I_NEW) {
5490 BTRFS_I(inode)->root = root;
5491 BTRFS_I(inode)->location.objectid = objectid;
5492 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
5493 BTRFS_I(inode)->location.offset = 0;
5494 btrfs_read_locked_inode(inode);
5495 unlock_new_inode(inode);
5496 BUG_ON(is_bad_inode(inode));
5497 } else {
5498 BUG_ON(1);
5499 }
Yan Zheng17d217f2008-12-12 10:03:38 -05005500 BTRFS_I(inode)->index_cnt = group->key.objectid;
Zheng Yan1a40e232008-09-26 10:09:34 -04005501
5502 err = btrfs_orphan_add(trans, inode);
5503out:
5504 btrfs_end_transaction(trans, root);
5505 if (err) {
5506 if (inode)
5507 iput(inode);
5508 inode = ERR_PTR(err);
5509 }
5510 return inode;
5511}
5512
Yan Zheng17d217f2008-12-12 10:03:38 -05005513int btrfs_reloc_clone_csums(struct inode *inode, u64 file_pos, u64 len)
5514{
5515
5516 struct btrfs_ordered_sum *sums;
5517 struct btrfs_sector_sum *sector_sum;
5518 struct btrfs_ordered_extent *ordered;
5519 struct btrfs_root *root = BTRFS_I(inode)->root;
5520 struct list_head list;
5521 size_t offset;
5522 int ret;
5523 u64 disk_bytenr;
5524
5525 INIT_LIST_HEAD(&list);
5526
5527 ordered = btrfs_lookup_ordered_extent(inode, file_pos);
5528 BUG_ON(ordered->file_offset != file_pos || ordered->len != len);
5529
5530 disk_bytenr = file_pos + BTRFS_I(inode)->index_cnt;
Yan Zheng07d400a2009-01-06 11:42:00 -05005531 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, disk_bytenr,
Yan Zheng17d217f2008-12-12 10:03:38 -05005532 disk_bytenr + len - 1, &list);
5533
5534 while (!list_empty(&list)) {
5535 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
5536 list_del_init(&sums->list);
5537
5538 sector_sum = sums->sums;
5539 sums->bytenr = ordered->start;
5540
5541 offset = 0;
5542 while (offset < sums->len) {
5543 sector_sum->bytenr += ordered->start - disk_bytenr;
5544 sector_sum++;
5545 offset += root->sectorsize;
5546 }
5547
5548 btrfs_add_ordered_sum(inode, ordered, sums);
5549 }
5550 btrfs_put_ordered_extent(ordered);
5551 return 0;
5552}
5553
Zheng Yan1a40e232008-09-26 10:09:34 -04005554int btrfs_relocate_block_group(struct btrfs_root *root, u64 group_start)
Chris Masonedbd8d42007-12-21 16:27:24 -05005555{
5556 struct btrfs_trans_handle *trans;
Chris Masonedbd8d42007-12-21 16:27:24 -05005557 struct btrfs_path *path;
Zheng Yan1a40e232008-09-26 10:09:34 -04005558 struct btrfs_fs_info *info = root->fs_info;
5559 struct extent_buffer *leaf;
5560 struct inode *reloc_inode;
5561 struct btrfs_block_group_cache *block_group;
5562 struct btrfs_key key;
Yan Zhengd899e052008-10-30 14:25:28 -04005563 u64 skipped;
Chris Masonedbd8d42007-12-21 16:27:24 -05005564 u64 cur_byte;
5565 u64 total_found;
Chris Masonedbd8d42007-12-21 16:27:24 -05005566 u32 nritems;
5567 int ret;
Chris Masona061fc82008-05-07 11:43:44 -04005568 int progress;
Zheng Yan1a40e232008-09-26 10:09:34 -04005569 int pass = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05005570
Chris Masonedbd8d42007-12-21 16:27:24 -05005571 root = root->fs_info->extent_root;
Zheng Yan1a40e232008-09-26 10:09:34 -04005572
5573 block_group = btrfs_lookup_block_group(info, group_start);
5574 BUG_ON(!block_group);
Chris Masonedbd8d42007-12-21 16:27:24 -05005575
Chris Masond3977122009-01-05 21:25:51 -05005576 printk(KERN_INFO "btrfs relocating block group %llu flags %llu\n",
Zheng Yan1a40e232008-09-26 10:09:34 -04005577 (unsigned long long)block_group->key.objectid,
5578 (unsigned long long)block_group->flags);
Chris Mason323da792008-05-09 11:46:48 -04005579
Zheng Yan1a40e232008-09-26 10:09:34 -04005580 path = btrfs_alloc_path();
5581 BUG_ON(!path);
Chris Mason323da792008-05-09 11:46:48 -04005582
Zheng Yan1a40e232008-09-26 10:09:34 -04005583 reloc_inode = create_reloc_inode(info, block_group);
5584 BUG_ON(IS_ERR(reloc_inode));
5585
Zheng Yan1a40e232008-09-26 10:09:34 -04005586 __alloc_chunk_for_shrink(root, block_group, 1);
Yan Zhengc146afa2008-11-12 14:34:12 -05005587 set_block_group_readonly(block_group);
Zheng Yan1a40e232008-09-26 10:09:34 -04005588
Zheng Yan1a40e232008-09-26 10:09:34 -04005589 btrfs_start_delalloc_inodes(info->tree_root);
5590 btrfs_wait_ordered_extents(info->tree_root, 0);
Chris Mason0ef3e662008-05-24 14:04:53 -04005591again:
Yan Zhengd899e052008-10-30 14:25:28 -04005592 skipped = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05005593 total_found = 0;
Chris Masona061fc82008-05-07 11:43:44 -04005594 progress = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04005595 key.objectid = block_group->key.objectid;
Chris Masonedbd8d42007-12-21 16:27:24 -05005596 key.offset = 0;
5597 key.type = 0;
Yan73e48b22008-01-03 14:14:39 -05005598 cur_byte = key.objectid;
Chris Mason4313b392008-01-03 09:08:48 -05005599
Zheng Yan1a40e232008-09-26 10:09:34 -04005600 trans = btrfs_start_transaction(info->tree_root, 1);
5601 btrfs_commit_transaction(trans, info->tree_root);
Chris Masonea8c2812008-08-04 23:17:27 -04005602
Zheng Yan1a40e232008-09-26 10:09:34 -04005603 mutex_lock(&root->fs_info->cleaner_mutex);
5604 btrfs_clean_old_snapshots(info->tree_root);
5605 btrfs_remove_leaf_refs(info->tree_root, (u64)-1, 1);
5606 mutex_unlock(&root->fs_info->cleaner_mutex);
Chris Masonea8c2812008-08-04 23:17:27 -04005607
Chris Mason56bec292009-03-13 10:10:06 -04005608 trans = btrfs_start_transaction(info->tree_root, 1);
5609 btrfs_commit_transaction(trans, info->tree_root);
5610
Chris Masond3977122009-01-05 21:25:51 -05005611 while (1) {
Chris Masonedbd8d42007-12-21 16:27:24 -05005612 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5613 if (ret < 0)
5614 goto out;
Chris Mason7d9eb122008-07-08 14:19:17 -04005615next:
Chris Masonedbd8d42007-12-21 16:27:24 -05005616 leaf = path->nodes[0];
Chris Masonedbd8d42007-12-21 16:27:24 -05005617 nritems = btrfs_header_nritems(leaf);
Yan73e48b22008-01-03 14:14:39 -05005618 if (path->slots[0] >= nritems) {
5619 ret = btrfs_next_leaf(root, path);
5620 if (ret < 0)
5621 goto out;
5622 if (ret == 1) {
5623 ret = 0;
5624 break;
Chris Masonedbd8d42007-12-21 16:27:24 -05005625 }
Yan73e48b22008-01-03 14:14:39 -05005626 leaf = path->nodes[0];
5627 nritems = btrfs_header_nritems(leaf);
5628 }
5629
Zheng Yan1a40e232008-09-26 10:09:34 -04005630 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Chris Mason725c8462008-01-04 16:47:16 -05005631
Zheng Yan1a40e232008-09-26 10:09:34 -04005632 if (key.objectid >= block_group->key.objectid +
5633 block_group->key.offset)
Chris Mason8f18cf12008-04-25 16:53:30 -04005634 break;
5635
Chris Mason725c8462008-01-04 16:47:16 -05005636 if (progress && need_resched()) {
Chris Mason725c8462008-01-04 16:47:16 -05005637 btrfs_release_path(root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04005638 cond_resched();
Chris Mason725c8462008-01-04 16:47:16 -05005639 progress = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04005640 continue;
Chris Mason725c8462008-01-04 16:47:16 -05005641 }
5642 progress = 1;
5643
Zheng Yan1a40e232008-09-26 10:09:34 -04005644 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY ||
5645 key.objectid + key.offset <= cur_byte) {
Yan73e48b22008-01-03 14:14:39 -05005646 path->slots[0]++;
Chris Masonedbd8d42007-12-21 16:27:24 -05005647 goto next;
5648 }
Yan73e48b22008-01-03 14:14:39 -05005649
Chris Masonedbd8d42007-12-21 16:27:24 -05005650 total_found++;
Zheng Yan1a40e232008-09-26 10:09:34 -04005651 cur_byte = key.objectid + key.offset;
Chris Masonedbd8d42007-12-21 16:27:24 -05005652 btrfs_release_path(root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04005653
5654 __alloc_chunk_for_shrink(root, block_group, 0);
5655 ret = relocate_one_extent(root, path, &key, block_group,
5656 reloc_inode, pass);
5657 BUG_ON(ret < 0);
Yan Zhengd899e052008-10-30 14:25:28 -04005658 if (ret > 0)
5659 skipped++;
Zheng Yan1a40e232008-09-26 10:09:34 -04005660
5661 key.objectid = cur_byte;
5662 key.type = 0;
5663 key.offset = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05005664 }
5665
5666 btrfs_release_path(root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04005667
5668 if (pass == 0) {
5669 btrfs_wait_ordered_range(reloc_inode, 0, (u64)-1);
5670 invalidate_mapping_pages(reloc_inode->i_mapping, 0, -1);
Zheng Yan1a40e232008-09-26 10:09:34 -04005671 }
Chris Masonedbd8d42007-12-21 16:27:24 -05005672
5673 if (total_found > 0) {
Chris Masond3977122009-01-05 21:25:51 -05005674 printk(KERN_INFO "btrfs found %llu extents in pass %d\n",
Zheng Yan1a40e232008-09-26 10:09:34 -04005675 (unsigned long long)total_found, pass);
5676 pass++;
Yan Zhengd899e052008-10-30 14:25:28 -04005677 if (total_found == skipped && pass > 2) {
5678 iput(reloc_inode);
5679 reloc_inode = create_reloc_inode(info, block_group);
5680 pass = 0;
5681 }
Chris Masonedbd8d42007-12-21 16:27:24 -05005682 goto again;
5683 }
5684
Zheng Yan1a40e232008-09-26 10:09:34 -04005685 /* delete reloc_inode */
5686 iput(reloc_inode);
Chris Mason7d9eb122008-07-08 14:19:17 -04005687
Zheng Yan1a40e232008-09-26 10:09:34 -04005688 /* unpin extents in this range */
5689 trans = btrfs_start_transaction(info->tree_root, 1);
5690 btrfs_commit_transaction(trans, info->tree_root);
Chris Mason0ef3e662008-05-24 14:04:53 -04005691
Zheng Yan1a40e232008-09-26 10:09:34 -04005692 spin_lock(&block_group->lock);
5693 WARN_ON(block_group->pinned > 0);
5694 WARN_ON(block_group->reserved > 0);
5695 WARN_ON(btrfs_block_group_used(&block_group->item) > 0);
5696 spin_unlock(&block_group->lock);
Yan Zhengd2fb3432008-12-11 16:30:39 -05005697 put_block_group(block_group);
Zheng Yan1a40e232008-09-26 10:09:34 -04005698 ret = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05005699out:
Zheng Yan1a40e232008-09-26 10:09:34 -04005700 btrfs_free_path(path);
Chris Masonedbd8d42007-12-21 16:27:24 -05005701 return ret;
5702}
5703
Christoph Hellwigb2950862008-12-02 09:54:17 -05005704static int find_first_block_group(struct btrfs_root *root,
5705 struct btrfs_path *path, struct btrfs_key *key)
Chris Mason0b86a832008-03-24 15:01:56 -04005706{
Chris Mason925baed2008-06-25 16:01:30 -04005707 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005708 struct btrfs_key found_key;
5709 struct extent_buffer *leaf;
5710 int slot;
5711
5712 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
5713 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04005714 goto out;
5715
Chris Masond3977122009-01-05 21:25:51 -05005716 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04005717 slot = path->slots[0];
5718 leaf = path->nodes[0];
5719 if (slot >= btrfs_header_nritems(leaf)) {
5720 ret = btrfs_next_leaf(root, path);
5721 if (ret == 0)
5722 continue;
5723 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04005724 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04005725 break;
5726 }
5727 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5728
5729 if (found_key.objectid >= key->objectid &&
Chris Mason925baed2008-06-25 16:01:30 -04005730 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
5731 ret = 0;
5732 goto out;
5733 }
Chris Mason0b86a832008-03-24 15:01:56 -04005734 path->slots[0]++;
5735 }
5736 ret = -ENOENT;
Chris Mason925baed2008-06-25 16:01:30 -04005737out:
Chris Mason0b86a832008-03-24 15:01:56 -04005738 return ret;
5739}
5740
Zheng Yan1a40e232008-09-26 10:09:34 -04005741int btrfs_free_block_groups(struct btrfs_fs_info *info)
5742{
5743 struct btrfs_block_group_cache *block_group;
Chris Mason4184ea72009-03-10 12:39:20 -04005744 struct btrfs_space_info *space_info;
Zheng Yan1a40e232008-09-26 10:09:34 -04005745 struct rb_node *n;
5746
Zheng Yan1a40e232008-09-26 10:09:34 -04005747 spin_lock(&info->block_group_cache_lock);
5748 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
5749 block_group = rb_entry(n, struct btrfs_block_group_cache,
5750 cache_node);
Zheng Yan1a40e232008-09-26 10:09:34 -04005751 rb_erase(&block_group->cache_node,
5752 &info->block_group_cache_tree);
Yan Zhengd899e052008-10-30 14:25:28 -04005753 spin_unlock(&info->block_group_cache_lock);
5754
5755 btrfs_remove_free_space_cache(block_group);
Josef Bacik80eb2342008-10-29 14:49:05 -04005756 down_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04005757 list_del(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04005758 up_write(&block_group->space_info->groups_sem);
Yan Zhengd2fb3432008-12-11 16:30:39 -05005759
5760 WARN_ON(atomic_read(&block_group->count) != 1);
Zheng Yan1a40e232008-09-26 10:09:34 -04005761 kfree(block_group);
Yan Zhengd899e052008-10-30 14:25:28 -04005762
5763 spin_lock(&info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04005764 }
5765 spin_unlock(&info->block_group_cache_lock);
Chris Mason4184ea72009-03-10 12:39:20 -04005766
5767 /* now that all the block groups are freed, go through and
5768 * free all the space_info structs. This is only called during
5769 * the final stages of unmount, and so we know nobody is
5770 * using them. We call synchronize_rcu() once before we start,
5771 * just to be on the safe side.
5772 */
5773 synchronize_rcu();
5774
5775 while(!list_empty(&info->space_info)) {
5776 space_info = list_entry(info->space_info.next,
5777 struct btrfs_space_info,
5778 list);
5779
5780 list_del(&space_info->list);
5781 kfree(space_info);
5782 }
Zheng Yan1a40e232008-09-26 10:09:34 -04005783 return 0;
5784}
5785
Chris Mason9078a3e2007-04-26 16:46:15 -04005786int btrfs_read_block_groups(struct btrfs_root *root)
5787{
5788 struct btrfs_path *path;
5789 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04005790 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04005791 struct btrfs_fs_info *info = root->fs_info;
Chris Mason6324fbf2008-03-24 15:01:59 -04005792 struct btrfs_space_info *space_info;
Chris Mason9078a3e2007-04-26 16:46:15 -04005793 struct btrfs_key key;
5794 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -04005795 struct extent_buffer *leaf;
Chris Mason96b51792007-10-15 16:15:19 -04005796
Chris Masonbe744172007-05-06 10:15:01 -04005797 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04005798 key.objectid = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005799 key.offset = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04005800 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
Chris Mason9078a3e2007-04-26 16:46:15 -04005801 path = btrfs_alloc_path();
5802 if (!path)
5803 return -ENOMEM;
5804
Chris Masond3977122009-01-05 21:25:51 -05005805 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04005806 ret = find_first_block_group(root, path, &key);
5807 if (ret > 0) {
5808 ret = 0;
5809 goto error;
Chris Mason9078a3e2007-04-26 16:46:15 -04005810 }
Chris Mason0b86a832008-03-24 15:01:56 -04005811 if (ret != 0)
5812 goto error;
5813
Chris Mason5f39d392007-10-15 16:14:19 -04005814 leaf = path->nodes[0];
5815 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason8f18cf12008-04-25 16:53:30 -04005816 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -04005817 if (!cache) {
Chris Mason0b86a832008-03-24 15:01:56 -04005818 ret = -ENOMEM;
Chris Mason9078a3e2007-04-26 16:46:15 -04005819 break;
5820 }
Chris Mason3e1ad542007-05-07 20:03:49 -04005821
Yan Zhengd2fb3432008-12-11 16:30:39 -05005822 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04005823 spin_lock_init(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04005824 mutex_init(&cache->alloc_mutex);
Josef Bacikea6a4782008-11-20 12:16:16 -05005825 mutex_init(&cache->cache_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005826 INIT_LIST_HEAD(&cache->list);
Chris Mason5f39d392007-10-15 16:14:19 -04005827 read_extent_buffer(leaf, &cache->item,
5828 btrfs_item_ptr_offset(leaf, path->slots[0]),
5829 sizeof(cache->item));
Chris Mason9078a3e2007-04-26 16:46:15 -04005830 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Mason0b86a832008-03-24 15:01:56 -04005831
Chris Mason9078a3e2007-04-26 16:46:15 -04005832 key.objectid = found_key.objectid + found_key.offset;
5833 btrfs_release_path(root, path);
Chris Mason0b86a832008-03-24 15:01:56 -04005834 cache->flags = btrfs_block_group_flags(&cache->item);
Chris Mason96b51792007-10-15 16:15:19 -04005835
Chris Mason6324fbf2008-03-24 15:01:59 -04005836 ret = update_space_info(info, cache->flags, found_key.offset,
5837 btrfs_block_group_used(&cache->item),
5838 &space_info);
5839 BUG_ON(ret);
5840 cache->space_info = space_info;
Josef Bacik80eb2342008-10-29 14:49:05 -04005841 down_write(&space_info->groups_sem);
5842 list_add_tail(&cache->list, &space_info->block_groups);
5843 up_write(&space_info->groups_sem);
Chris Mason6324fbf2008-03-24 15:01:59 -04005844
Josef Bacik0f9dd462008-09-23 13:14:11 -04005845 ret = btrfs_add_block_group_cache(root->fs_info, cache);
5846 BUG_ON(ret);
Chris Mason75ccf472008-09-30 19:24:06 -04005847
5848 set_avail_alloc_bits(root->fs_info, cache->flags);
Yan Zheng2b820322008-11-17 21:11:30 -05005849 if (btrfs_chunk_readonly(root, cache->key.objectid))
5850 set_block_group_readonly(cache);
Chris Mason9078a3e2007-04-26 16:46:15 -04005851 }
Chris Mason0b86a832008-03-24 15:01:56 -04005852 ret = 0;
5853error:
Chris Mason9078a3e2007-04-26 16:46:15 -04005854 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04005855 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04005856}
Chris Mason6324fbf2008-03-24 15:01:59 -04005857
5858int btrfs_make_block_group(struct btrfs_trans_handle *trans,
5859 struct btrfs_root *root, u64 bytes_used,
Chris Masone17cade2008-04-15 15:41:47 -04005860 u64 type, u64 chunk_objectid, u64 chunk_offset,
Chris Mason6324fbf2008-03-24 15:01:59 -04005861 u64 size)
5862{
5863 int ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04005864 struct btrfs_root *extent_root;
5865 struct btrfs_block_group_cache *cache;
Chris Mason6324fbf2008-03-24 15:01:59 -04005866
5867 extent_root = root->fs_info->extent_root;
Chris Mason6324fbf2008-03-24 15:01:59 -04005868
Chris Masone02119d2008-09-05 16:13:11 -04005869 root->fs_info->last_trans_new_blockgroup = trans->transid;
5870
Chris Mason8f18cf12008-04-25 16:53:30 -04005871 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005872 if (!cache)
5873 return -ENOMEM;
5874
Chris Masone17cade2008-04-15 15:41:47 -04005875 cache->key.objectid = chunk_offset;
Chris Mason6324fbf2008-03-24 15:01:59 -04005876 cache->key.offset = size;
Yan Zhengd2fb3432008-12-11 16:30:39 -05005877 cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
5878 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04005879 spin_lock_init(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04005880 mutex_init(&cache->alloc_mutex);
Josef Bacikea6a4782008-11-20 12:16:16 -05005881 mutex_init(&cache->cache_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005882 INIT_LIST_HEAD(&cache->list);
Chris Mason0ef3e662008-05-24 14:04:53 -04005883
Chris Mason6324fbf2008-03-24 15:01:59 -04005884 btrfs_set_block_group_used(&cache->item, bytes_used);
Chris Mason6324fbf2008-03-24 15:01:59 -04005885 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
5886 cache->flags = type;
5887 btrfs_set_block_group_flags(&cache->item, type);
5888
5889 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
5890 &cache->space_info);
5891 BUG_ON(ret);
Josef Bacik80eb2342008-10-29 14:49:05 -04005892 down_write(&cache->space_info->groups_sem);
5893 list_add_tail(&cache->list, &cache->space_info->block_groups);
5894 up_write(&cache->space_info->groups_sem);
Chris Mason6324fbf2008-03-24 15:01:59 -04005895
Josef Bacik0f9dd462008-09-23 13:14:11 -04005896 ret = btrfs_add_block_group_cache(root->fs_info, cache);
5897 BUG_ON(ret);
Chris Masonc286ac42008-07-22 23:06:41 -04005898
Chris Mason6324fbf2008-03-24 15:01:59 -04005899 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
5900 sizeof(cache->item));
5901 BUG_ON(ret);
5902
Chris Masond18a2c42008-04-04 15:40:00 -04005903 set_avail_alloc_bits(extent_root->fs_info, type);
Chris Mason925baed2008-06-25 16:01:30 -04005904
Chris Mason6324fbf2008-03-24 15:01:59 -04005905 return 0;
5906}
Zheng Yan1a40e232008-09-26 10:09:34 -04005907
5908int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
5909 struct btrfs_root *root, u64 group_start)
5910{
5911 struct btrfs_path *path;
5912 struct btrfs_block_group_cache *block_group;
5913 struct btrfs_key key;
5914 int ret;
5915
Zheng Yan1a40e232008-09-26 10:09:34 -04005916 root = root->fs_info->extent_root;
5917
5918 block_group = btrfs_lookup_block_group(root->fs_info, group_start);
5919 BUG_ON(!block_group);
Yan Zhengc146afa2008-11-12 14:34:12 -05005920 BUG_ON(!block_group->ro);
Zheng Yan1a40e232008-09-26 10:09:34 -04005921
5922 memcpy(&key, &block_group->key, sizeof(key));
5923
5924 path = btrfs_alloc_path();
5925 BUG_ON(!path);
5926
Yan Zheng3dfdb932009-01-21 10:49:16 -05005927 spin_lock(&root->fs_info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04005928 rb_erase(&block_group->cache_node,
5929 &root->fs_info->block_group_cache_tree);
Yan Zheng3dfdb932009-01-21 10:49:16 -05005930 spin_unlock(&root->fs_info->block_group_cache_lock);
5931 btrfs_remove_free_space_cache(block_group);
Josef Bacik80eb2342008-10-29 14:49:05 -04005932 down_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04005933 list_del(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04005934 up_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04005935
Yan Zhengc146afa2008-11-12 14:34:12 -05005936 spin_lock(&block_group->space_info->lock);
5937 block_group->space_info->total_bytes -= block_group->key.offset;
5938 block_group->space_info->bytes_readonly -= block_group->key.offset;
5939 spin_unlock(&block_group->space_info->lock);
Yan Zheng2b820322008-11-17 21:11:30 -05005940 block_group->space_info->full = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05005941
Yan Zhengd2fb3432008-12-11 16:30:39 -05005942 put_block_group(block_group);
5943 put_block_group(block_group);
Zheng Yan1a40e232008-09-26 10:09:34 -04005944
5945 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
5946 if (ret > 0)
5947 ret = -EIO;
5948 if (ret < 0)
5949 goto out;
5950
5951 ret = btrfs_del_item(trans, root, path);
5952out:
5953 btrfs_free_path(path);
5954 return ret;
5955}