blob: 8471c79b0877316d17deae2c6366aa76cc775809 [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
929/*
930 * this starts processing the delayed reference count updates and
931 * extent insertions we have queued up so far. count can be
932 * 0, which means to process everything in the tree at the start
933 * of the run (but not newly added entries), or it can be some target
934 * number you'd like to process.
935 */
936int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
937 struct btrfs_root *root, unsigned long count)
938{
939 struct rb_node *node;
940 struct btrfs_delayed_ref_root *delayed_refs;
941 struct btrfs_delayed_ref_node *ref;
942 struct btrfs_delayed_ref_head *locked_ref = NULL;
943 int ret;
944 int must_insert_reserved = 0;
945 int run_all = count == (unsigned long)-1;
946
947 if (root == root->fs_info->extent_root)
948 root = root->fs_info->tree_root;
949
950 delayed_refs = &trans->transaction->delayed_refs;
951again:
952 spin_lock(&delayed_refs->lock);
953 if (count == 0)
954 count = delayed_refs->num_entries;
955 while (1) {
956 if (!locked_ref) {
957 /*
958 * no locked ref, go find something we can
959 * process in the rbtree. We start at
960 * the beginning of the tree, there may be less
961 * lock contention if we do something smarter here.
962 */
963 node = rb_first(&delayed_refs->root);
964 if (!node) {
965 spin_unlock(&delayed_refs->lock);
966 break;
967 }
968
969 ref = rb_entry(node, struct btrfs_delayed_ref_node,
970 rb_node);
971 ret = btrfs_lock_delayed_ref(trans, ref, &locked_ref);
972 if (ret) {
973 spin_unlock(&delayed_refs->lock);
974 break;
975 }
976 }
977
978 /*
979 * record the must insert reserved flag before we
980 * drop the spin lock.
981 */
982 must_insert_reserved = locked_ref->must_insert_reserved;
983 locked_ref->must_insert_reserved = 0;
984
985 /*
986 * locked_ref is the head node, so we have to go one
987 * node back for any delayed ref updates
988 */
989
990 ref = select_delayed_ref(locked_ref);
991 if (!ref) {
992 /* All delayed refs have been processed, Go ahead
993 * and send the head node to run_one_delayed_ref,
994 * so that any accounting fixes can happen
995 */
996 ref = &locked_ref->node;
997 locked_ref = NULL;
998 }
999
1000 ref->in_tree = 0;
1001 rb_erase(&ref->rb_node, &delayed_refs->root);
1002 delayed_refs->num_entries--;
1003 spin_unlock(&delayed_refs->lock);
1004
1005 ret = run_one_delayed_ref(trans, root, ref,
1006 must_insert_reserved);
1007 BUG_ON(ret);
1008 btrfs_put_delayed_ref(ref);
1009
1010 /* once we lock the head ref, we have to process all the
1011 * entries for it. So, we might end up doing more entries
1012 * that count was asking us to do.
1013 */
1014 if (count > 0)
1015 count--;
1016
1017 /*
1018 * we set locked_ref to null above if we're all done
1019 * with this bytenr
1020 */
1021 if (!locked_ref && count == 0)
1022 break;
1023
Chris Mason1887be62009-03-13 10:11:24 -04001024 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04001025 spin_lock(&delayed_refs->lock);
1026 }
1027 if (run_all) {
1028 spin_lock(&delayed_refs->lock);
1029 node = rb_first(&delayed_refs->root);
1030 if (!node) {
1031 spin_unlock(&delayed_refs->lock);
1032 goto out;
1033 }
1034
1035 while (node) {
1036 ref = rb_entry(node, struct btrfs_delayed_ref_node,
1037 rb_node);
1038 if (btrfs_delayed_ref_is_head(ref)) {
1039 struct btrfs_delayed_ref_head *head;
1040
1041 head = btrfs_delayed_node_to_head(ref);
1042 atomic_inc(&ref->refs);
1043
1044 spin_unlock(&delayed_refs->lock);
1045 mutex_lock(&head->mutex);
1046 mutex_unlock(&head->mutex);
1047
1048 btrfs_put_delayed_ref(ref);
Chris Mason1887be62009-03-13 10:11:24 -04001049 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04001050 goto again;
1051 }
1052 node = rb_next(node);
1053 }
1054 spin_unlock(&delayed_refs->lock);
1055 count = (unsigned long)-1;
1056 schedule_timeout(1);
1057 goto again;
1058 }
Chris Mason54aa1f42007-06-22 14:16:25 -04001059out:
Chris Masona28ec192007-03-06 20:08:01 -05001060 return 0;
1061}
1062
Yan Zheng80ff3852008-10-30 14:20:02 -04001063int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
Yan Zheng17d217f2008-12-12 10:03:38 -05001064 struct btrfs_root *root, u64 objectid, u64 bytenr)
Chris Masonbe20aa92007-12-17 20:14:01 -05001065{
1066 struct btrfs_root *extent_root = root->fs_info->extent_root;
1067 struct btrfs_path *path;
Yan Zhengf321e492008-07-30 09:26:11 -04001068 struct extent_buffer *leaf;
1069 struct btrfs_extent_ref *ref_item;
Chris Masonbe20aa92007-12-17 20:14:01 -05001070 struct btrfs_key key;
1071 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001072 u64 ref_root;
1073 u64 last_snapshot;
Yan Zhengf321e492008-07-30 09:26:11 -04001074 u32 nritems;
1075 int ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001076
Chris Masonbe20aa92007-12-17 20:14:01 -05001077 key.objectid = bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -04001078 key.offset = (u64)-1;
Yan Zhengf321e492008-07-30 09:26:11 -04001079 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masonbe20aa92007-12-17 20:14:01 -05001080
Yan Zhengf321e492008-07-30 09:26:11 -04001081 path = btrfs_alloc_path();
Chris Masonbe20aa92007-12-17 20:14:01 -05001082 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
1083 if (ret < 0)
1084 goto out;
1085 BUG_ON(ret == 0);
Yan Zheng80ff3852008-10-30 14:20:02 -04001086
1087 ret = -ENOENT;
1088 if (path->slots[0] == 0)
Zheng Yan31840ae2008-09-23 13:14:14 -04001089 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05001090
Zheng Yan31840ae2008-09-23 13:14:14 -04001091 path->slots[0]--;
Yan Zhengf321e492008-07-30 09:26:11 -04001092 leaf = path->nodes[0];
1093 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001094
1095 if (found_key.objectid != bytenr ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001096 found_key.type != BTRFS_EXTENT_ITEM_KEY)
Chris Masonbe20aa92007-12-17 20:14:01 -05001097 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05001098
Yan Zheng80ff3852008-10-30 14:20:02 -04001099 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Chris Masonbe20aa92007-12-17 20:14:01 -05001100 while (1) {
Yan Zhengf321e492008-07-30 09:26:11 -04001101 leaf = path->nodes[0];
1102 nritems = btrfs_header_nritems(leaf);
Chris Masonbe20aa92007-12-17 20:14:01 -05001103 if (path->slots[0] >= nritems) {
1104 ret = btrfs_next_leaf(extent_root, path);
Yan Zhengf321e492008-07-30 09:26:11 -04001105 if (ret < 0)
1106 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05001107 if (ret == 0)
1108 continue;
1109 break;
1110 }
Yan Zhengf321e492008-07-30 09:26:11 -04001111 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001112 if (found_key.objectid != bytenr)
1113 break;
Chris Masonbd098352008-01-03 13:23:19 -05001114
Chris Masonbe20aa92007-12-17 20:14:01 -05001115 if (found_key.type != BTRFS_EXTENT_REF_KEY) {
1116 path->slots[0]++;
1117 continue;
1118 }
1119
Yan Zhengf321e492008-07-30 09:26:11 -04001120 ref_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masonbe20aa92007-12-17 20:14:01 -05001121 struct btrfs_extent_ref);
Yan Zheng80ff3852008-10-30 14:20:02 -04001122 ref_root = btrfs_ref_root(leaf, ref_item);
Yan Zheng17d217f2008-12-12 10:03:38 -05001123 if ((ref_root != root->root_key.objectid &&
1124 ref_root != BTRFS_TREE_LOG_OBJECTID) ||
1125 objectid != btrfs_ref_objectid(leaf, ref_item)) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001126 ret = 1;
1127 goto out;
Yan Zhengf321e492008-07-30 09:26:11 -04001128 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001129 if (btrfs_ref_generation(leaf, ref_item) <= last_snapshot) {
1130 ret = 1;
1131 goto out;
1132 }
Yan Zhengf321e492008-07-30 09:26:11 -04001133
Chris Masonbe20aa92007-12-17 20:14:01 -05001134 path->slots[0]++;
1135 }
Yan Zhengf321e492008-07-30 09:26:11 -04001136 ret = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001137out:
Yan Zhengf321e492008-07-30 09:26:11 -04001138 btrfs_free_path(path);
1139 return ret;
1140}
1141
Zheng Yan31840ae2008-09-23 13:14:14 -04001142int btrfs_cache_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1143 struct extent_buffer *buf, u32 nr_extents)
Chris Mason02217ed2007-03-02 16:08:05 -05001144{
Chris Mason5f39d392007-10-15 16:14:19 -04001145 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -04001146 struct btrfs_file_extent_item *fi;
Zheng Yane4657682008-09-26 10:04:53 -04001147 u64 root_gen;
1148 u32 nritems;
Chris Mason02217ed2007-03-02 16:08:05 -05001149 int i;
Chris Masondb945352007-10-15 16:15:53 -04001150 int level;
Zheng Yan31840ae2008-09-23 13:14:14 -04001151 int ret = 0;
Zheng Yane4657682008-09-26 10:04:53 -04001152 int shared = 0;
Chris Masona28ec192007-03-06 20:08:01 -05001153
Chris Mason3768f362007-03-13 16:47:54 -04001154 if (!root->ref_cows)
Chris Masona28ec192007-03-06 20:08:01 -05001155 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001156
Zheng Yane4657682008-09-26 10:04:53 -04001157 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
1158 shared = 0;
1159 root_gen = root->root_key.offset;
1160 } else {
1161 shared = 1;
1162 root_gen = trans->transid - 1;
1163 }
1164
Chris Masondb945352007-10-15 16:15:53 -04001165 level = btrfs_header_level(buf);
Chris Mason5f39d392007-10-15 16:14:19 -04001166 nritems = btrfs_header_nritems(buf);
Chris Mason4a096752008-07-21 10:29:44 -04001167
Zheng Yan31840ae2008-09-23 13:14:14 -04001168 if (level == 0) {
Yan Zheng31153d82008-07-28 15:32:19 -04001169 struct btrfs_leaf_ref *ref;
1170 struct btrfs_extent_info *info;
1171
Zheng Yan31840ae2008-09-23 13:14:14 -04001172 ref = btrfs_alloc_leaf_ref(root, nr_extents);
Yan Zheng31153d82008-07-28 15:32:19 -04001173 if (!ref) {
Zheng Yan31840ae2008-09-23 13:14:14 -04001174 ret = -ENOMEM;
Yan Zheng31153d82008-07-28 15:32:19 -04001175 goto out;
1176 }
1177
Zheng Yane4657682008-09-26 10:04:53 -04001178 ref->root_gen = root_gen;
Yan Zheng31153d82008-07-28 15:32:19 -04001179 ref->bytenr = buf->start;
1180 ref->owner = btrfs_header_owner(buf);
1181 ref->generation = btrfs_header_generation(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04001182 ref->nritems = nr_extents;
Yan Zheng31153d82008-07-28 15:32:19 -04001183 info = ref->extents;
Yanbcc63ab2008-07-30 16:29:20 -04001184
Zheng Yan31840ae2008-09-23 13:14:14 -04001185 for (i = 0; nr_extents > 0 && i < nritems; i++) {
Yan Zheng31153d82008-07-28 15:32:19 -04001186 u64 disk_bytenr;
1187 btrfs_item_key_to_cpu(buf, &key, i);
1188 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1189 continue;
1190 fi = btrfs_item_ptr(buf, i,
1191 struct btrfs_file_extent_item);
1192 if (btrfs_file_extent_type(buf, fi) ==
1193 BTRFS_FILE_EXTENT_INLINE)
1194 continue;
1195 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1196 if (disk_bytenr == 0)
1197 continue;
1198
1199 info->bytenr = disk_bytenr;
1200 info->num_bytes =
1201 btrfs_file_extent_disk_num_bytes(buf, fi);
1202 info->objectid = key.objectid;
1203 info->offset = key.offset;
1204 info++;
1205 }
1206
Zheng Yane4657682008-09-26 10:04:53 -04001207 ret = btrfs_add_leaf_ref(root, ref, shared);
Yan Zheng5b84e8d2008-10-09 11:46:19 -04001208 if (ret == -EEXIST && shared) {
1209 struct btrfs_leaf_ref *old;
1210 old = btrfs_lookup_leaf_ref(root, ref->bytenr);
1211 BUG_ON(!old);
1212 btrfs_remove_leaf_ref(root, old);
1213 btrfs_free_leaf_ref(root, old);
1214 ret = btrfs_add_leaf_ref(root, ref, shared);
1215 }
Yan Zheng31153d82008-07-28 15:32:19 -04001216 WARN_ON(ret);
Yanbcc63ab2008-07-30 16:29:20 -04001217 btrfs_free_leaf_ref(root, ref);
Yan Zheng31153d82008-07-28 15:32:19 -04001218 }
1219out:
Zheng Yan31840ae2008-09-23 13:14:14 -04001220 return ret;
1221}
1222
Chris Masonb7a9f292009-02-04 09:23:45 -05001223/* when a block goes through cow, we update the reference counts of
1224 * everything that block points to. The internal pointers of the block
1225 * can be in just about any order, and it is likely to have clusters of
1226 * things that are close together and clusters of things that are not.
1227 *
1228 * To help reduce the seeks that come with updating all of these reference
1229 * counts, sort them by byte number before actual updates are done.
1230 *
1231 * struct refsort is used to match byte number to slot in the btree block.
1232 * we sort based on the byte number and then use the slot to actually
1233 * find the item.
Chris Masonbd56b302009-02-04 09:27:02 -05001234 *
1235 * struct refsort is smaller than strcut btrfs_item and smaller than
1236 * struct btrfs_key_ptr. Since we're currently limited to the page size
1237 * for a btree block, there's no way for a kmalloc of refsorts for a
1238 * single node to be bigger than a page.
Chris Masonb7a9f292009-02-04 09:23:45 -05001239 */
1240struct refsort {
1241 u64 bytenr;
1242 u32 slot;
1243};
1244
1245/*
1246 * for passing into sort()
1247 */
1248static int refsort_cmp(const void *a_void, const void *b_void)
1249{
1250 const struct refsort *a = a_void;
1251 const struct refsort *b = b_void;
1252
1253 if (a->bytenr < b->bytenr)
1254 return -1;
1255 if (a->bytenr > b->bytenr)
1256 return 1;
1257 return 0;
1258}
1259
1260
1261noinline int btrfs_inc_ref(struct btrfs_trans_handle *trans,
1262 struct btrfs_root *root,
1263 struct extent_buffer *orig_buf,
1264 struct extent_buffer *buf, u32 *nr_extents)
Zheng Yan31840ae2008-09-23 13:14:14 -04001265{
1266 u64 bytenr;
1267 u64 ref_root;
1268 u64 orig_root;
1269 u64 ref_generation;
1270 u64 orig_generation;
Chris Masonb7a9f292009-02-04 09:23:45 -05001271 struct refsort *sorted;
Zheng Yan31840ae2008-09-23 13:14:14 -04001272 u32 nritems;
1273 u32 nr_file_extents = 0;
1274 struct btrfs_key key;
1275 struct btrfs_file_extent_item *fi;
1276 int i;
1277 int level;
1278 int ret = 0;
1279 int faili = 0;
Chris Masonb7a9f292009-02-04 09:23:45 -05001280 int refi = 0;
1281 int slot;
Zheng Yan31840ae2008-09-23 13:14:14 -04001282 int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
Chris Mason56bec292009-03-13 10:10:06 -04001283 u64, u64, u64, u64, u64, u64, u64, u64, u64);
Zheng Yan31840ae2008-09-23 13:14:14 -04001284
1285 ref_root = btrfs_header_owner(buf);
1286 ref_generation = btrfs_header_generation(buf);
1287 orig_root = btrfs_header_owner(orig_buf);
1288 orig_generation = btrfs_header_generation(orig_buf);
1289
1290 nritems = btrfs_header_nritems(buf);
1291 level = btrfs_header_level(buf);
1292
Chris Masonb7a9f292009-02-04 09:23:45 -05001293 sorted = kmalloc(sizeof(struct refsort) * nritems, GFP_NOFS);
1294 BUG_ON(!sorted);
1295
Zheng Yan31840ae2008-09-23 13:14:14 -04001296 if (root->ref_cows) {
1297 process_func = __btrfs_inc_extent_ref;
1298 } else {
1299 if (level == 0 &&
1300 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
1301 goto out;
1302 if (level != 0 &&
1303 root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID)
1304 goto out;
1305 process_func = __btrfs_update_extent_ref;
1306 }
1307
Chris Masonb7a9f292009-02-04 09:23:45 -05001308 /*
1309 * we make two passes through the items. In the first pass we
1310 * only record the byte number and slot. Then we sort based on
1311 * byte number and do the actual work based on the sorted results
1312 */
Zheng Yan31840ae2008-09-23 13:14:14 -04001313 for (i = 0; i < nritems; i++) {
1314 cond_resched();
Chris Masondb945352007-10-15 16:15:53 -04001315 if (level == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001316 btrfs_item_key_to_cpu(buf, &key, i);
1317 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -04001318 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04001319 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -04001320 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001321 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -04001322 BTRFS_FILE_EXTENT_INLINE)
1323 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04001324 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1325 if (bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -04001326 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04001327
1328 nr_file_extents++;
Chris Masonb7a9f292009-02-04 09:23:45 -05001329 sorted[refi].bytenr = bytenr;
1330 sorted[refi].slot = i;
1331 refi++;
1332 } else {
1333 bytenr = btrfs_node_blockptr(buf, i);
1334 sorted[refi].bytenr = bytenr;
1335 sorted[refi].slot = i;
1336 refi++;
1337 }
1338 }
1339 /*
1340 * if refi == 0, we didn't actually put anything into the sorted
1341 * array and we're done
1342 */
1343 if (refi == 0)
1344 goto out;
1345
1346 sort(sorted, refi, sizeof(struct refsort), refsort_cmp, NULL);
1347
1348 for (i = 0; i < refi; i++) {
1349 cond_resched();
1350 slot = sorted[i].slot;
1351 bytenr = sorted[i].bytenr;
1352
1353 if (level == 0) {
1354 btrfs_item_key_to_cpu(buf, &key, slot);
Chris Mason56bec292009-03-13 10:10:06 -04001355 fi = btrfs_item_ptr(buf, slot,
1356 struct btrfs_file_extent_item);
1357
1358 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1359 if (bytenr == 0)
1360 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04001361
Zheng Yan31840ae2008-09-23 13:14:14 -04001362 ret = process_func(trans, root, bytenr,
Chris Mason56bec292009-03-13 10:10:06 -04001363 btrfs_file_extent_disk_num_bytes(buf, fi),
1364 orig_buf->start, buf->start,
1365 orig_root, ref_root,
1366 orig_generation, ref_generation,
1367 key.objectid);
Zheng Yan31840ae2008-09-23 13:14:14 -04001368
1369 if (ret) {
Chris Masonb7a9f292009-02-04 09:23:45 -05001370 faili = slot;
Zheng Yan31840ae2008-09-23 13:14:14 -04001371 WARN_ON(1);
1372 goto fail;
1373 }
Chris Mason54aa1f42007-06-22 14:16:25 -04001374 } else {
Chris Mason56bec292009-03-13 10:10:06 -04001375 ret = process_func(trans, root, bytenr, buf->len,
Zheng Yan31840ae2008-09-23 13:14:14 -04001376 orig_buf->start, buf->start,
1377 orig_root, ref_root,
1378 orig_generation, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001379 level - 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04001380 if (ret) {
Chris Masonb7a9f292009-02-04 09:23:45 -05001381 faili = slot;
Zheng Yan31840ae2008-09-23 13:14:14 -04001382 WARN_ON(1);
1383 goto fail;
1384 }
Chris Mason54aa1f42007-06-22 14:16:25 -04001385 }
1386 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001387out:
Chris Masonb7a9f292009-02-04 09:23:45 -05001388 kfree(sorted);
Zheng Yan31840ae2008-09-23 13:14:14 -04001389 if (nr_extents) {
1390 if (level == 0)
1391 *nr_extents = nr_file_extents;
1392 else
1393 *nr_extents = nritems;
1394 }
1395 return 0;
1396fail:
Chris Masonb7a9f292009-02-04 09:23:45 -05001397 kfree(sorted);
Zheng Yan31840ae2008-09-23 13:14:14 -04001398 WARN_ON(1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001399 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05001400}
1401
Zheng Yan31840ae2008-09-23 13:14:14 -04001402int btrfs_update_ref(struct btrfs_trans_handle *trans,
1403 struct btrfs_root *root, struct extent_buffer *orig_buf,
1404 struct extent_buffer *buf, int start_slot, int nr)
1405
1406{
1407 u64 bytenr;
1408 u64 ref_root;
1409 u64 orig_root;
1410 u64 ref_generation;
1411 u64 orig_generation;
1412 struct btrfs_key key;
1413 struct btrfs_file_extent_item *fi;
1414 int i;
1415 int ret;
1416 int slot;
1417 int level;
1418
1419 BUG_ON(start_slot < 0);
1420 BUG_ON(start_slot + nr > btrfs_header_nritems(buf));
1421
1422 ref_root = btrfs_header_owner(buf);
1423 ref_generation = btrfs_header_generation(buf);
1424 orig_root = btrfs_header_owner(orig_buf);
1425 orig_generation = btrfs_header_generation(orig_buf);
1426 level = btrfs_header_level(buf);
1427
1428 if (!root->ref_cows) {
1429 if (level == 0 &&
1430 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
1431 return 0;
1432 if (level != 0 &&
1433 root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID)
1434 return 0;
1435 }
1436
1437 for (i = 0, slot = start_slot; i < nr; i++, slot++) {
1438 cond_resched();
1439 if (level == 0) {
1440 btrfs_item_key_to_cpu(buf, &key, slot);
1441 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1442 continue;
1443 fi = btrfs_item_ptr(buf, slot,
1444 struct btrfs_file_extent_item);
1445 if (btrfs_file_extent_type(buf, fi) ==
1446 BTRFS_FILE_EXTENT_INLINE)
1447 continue;
1448 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1449 if (bytenr == 0)
1450 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04001451 ret = __btrfs_update_extent_ref(trans, root, bytenr,
Chris Mason56bec292009-03-13 10:10:06 -04001452 btrfs_file_extent_disk_num_bytes(buf, fi),
1453 orig_buf->start, buf->start,
1454 orig_root, ref_root, orig_generation,
1455 ref_generation, key.objectid);
Zheng Yan31840ae2008-09-23 13:14:14 -04001456 if (ret)
1457 goto fail;
1458 } else {
1459 bytenr = btrfs_node_blockptr(buf, slot);
Zheng Yan31840ae2008-09-23 13:14:14 -04001460 ret = __btrfs_update_extent_ref(trans, root, bytenr,
Chris Mason56bec292009-03-13 10:10:06 -04001461 buf->len, orig_buf->start,
1462 buf->start, orig_root, ref_root,
Zheng Yan31840ae2008-09-23 13:14:14 -04001463 orig_generation, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001464 level - 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04001465 if (ret)
1466 goto fail;
1467 }
1468 }
1469 return 0;
1470fail:
1471 WARN_ON(1);
1472 return -1;
1473}
1474
Chris Mason9078a3e2007-04-26 16:46:15 -04001475static int write_one_cache_group(struct btrfs_trans_handle *trans,
1476 struct btrfs_root *root,
1477 struct btrfs_path *path,
1478 struct btrfs_block_group_cache *cache)
1479{
1480 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04001481 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04001482 unsigned long bi;
1483 struct extent_buffer *leaf;
Chris Mason9078a3e2007-04-26 16:46:15 -04001484
Chris Mason9078a3e2007-04-26 16:46:15 -04001485 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001486 if (ret < 0)
1487 goto fail;
Chris Mason9078a3e2007-04-26 16:46:15 -04001488 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04001489
1490 leaf = path->nodes[0];
1491 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
1492 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
1493 btrfs_mark_buffer_dirty(leaf);
Chris Mason9078a3e2007-04-26 16:46:15 -04001494 btrfs_release_path(extent_root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -04001495fail:
Chris Mason9078a3e2007-04-26 16:46:15 -04001496 if (ret)
1497 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04001498 return 0;
1499
1500}
1501
Chris Mason96b51792007-10-15 16:15:19 -04001502int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
1503 struct btrfs_root *root)
Chris Mason9078a3e2007-04-26 16:46:15 -04001504{
Josef Bacik0f9dd462008-09-23 13:14:11 -04001505 struct btrfs_block_group_cache *cache, *entry;
1506 struct rb_node *n;
Chris Mason9078a3e2007-04-26 16:46:15 -04001507 int err = 0;
1508 int werr = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04001509 struct btrfs_path *path;
Chris Mason96b51792007-10-15 16:15:19 -04001510 u64 last = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04001511
1512 path = btrfs_alloc_path();
1513 if (!path)
1514 return -ENOMEM;
1515
Chris Masond3977122009-01-05 21:25:51 -05001516 while (1) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001517 cache = NULL;
1518 spin_lock(&root->fs_info->block_group_cache_lock);
1519 for (n = rb_first(&root->fs_info->block_group_cache_tree);
1520 n; n = rb_next(n)) {
1521 entry = rb_entry(n, struct btrfs_block_group_cache,
1522 cache_node);
1523 if (entry->dirty) {
1524 cache = entry;
1525 break;
1526 }
1527 }
1528 spin_unlock(&root->fs_info->block_group_cache_lock);
1529
1530 if (!cache)
Chris Mason9078a3e2007-04-26 16:46:15 -04001531 break;
Chris Mason54aa1f42007-06-22 14:16:25 -04001532
Zheng Yane8569812008-09-26 10:05:48 -04001533 cache->dirty = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001534 last += cache->key.offset;
1535
Chris Mason96b51792007-10-15 16:15:19 -04001536 err = write_one_cache_group(trans, root,
1537 path, cache);
1538 /*
1539 * if we fail to write the cache group, we want
1540 * to keep it marked dirty in hopes that a later
1541 * write will work
1542 */
1543 if (err) {
1544 werr = err;
1545 continue;
Chris Mason9078a3e2007-04-26 16:46:15 -04001546 }
1547 }
1548 btrfs_free_path(path);
1549 return werr;
1550}
1551
Yan Zhengd2fb3432008-12-11 16:30:39 -05001552int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr)
1553{
1554 struct btrfs_block_group_cache *block_group;
1555 int readonly = 0;
1556
1557 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
1558 if (!block_group || block_group->ro)
1559 readonly = 1;
1560 if (block_group)
1561 put_block_group(block_group);
1562 return readonly;
1563}
1564
Chris Mason593060d2008-03-25 16:50:33 -04001565static int update_space_info(struct btrfs_fs_info *info, u64 flags,
1566 u64 total_bytes, u64 bytes_used,
1567 struct btrfs_space_info **space_info)
1568{
1569 struct btrfs_space_info *found;
1570
1571 found = __find_space_info(info, flags);
1572 if (found) {
Josef Bacik25179202008-10-29 14:49:05 -04001573 spin_lock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04001574 found->total_bytes += total_bytes;
1575 found->bytes_used += bytes_used;
Chris Mason8f18cf12008-04-25 16:53:30 -04001576 found->full = 0;
Josef Bacik25179202008-10-29 14:49:05 -04001577 spin_unlock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04001578 *space_info = found;
1579 return 0;
1580 }
Yan Zhengc146afa2008-11-12 14:34:12 -05001581 found = kzalloc(sizeof(*found), GFP_NOFS);
Chris Mason593060d2008-03-25 16:50:33 -04001582 if (!found)
1583 return -ENOMEM;
1584
Josef Bacik0f9dd462008-09-23 13:14:11 -04001585 INIT_LIST_HEAD(&found->block_groups);
Josef Bacik80eb2342008-10-29 14:49:05 -04001586 init_rwsem(&found->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001587 spin_lock_init(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04001588 found->flags = flags;
1589 found->total_bytes = total_bytes;
1590 found->bytes_used = bytes_used;
1591 found->bytes_pinned = 0;
Zheng Yane8569812008-09-26 10:05:48 -04001592 found->bytes_reserved = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05001593 found->bytes_readonly = 0;
Josef Bacik6a632092009-02-20 11:00:09 -05001594 found->bytes_delalloc = 0;
Chris Mason593060d2008-03-25 16:50:33 -04001595 found->full = 0;
Chris Mason0ef3e662008-05-24 14:04:53 -04001596 found->force_alloc = 0;
Chris Mason593060d2008-03-25 16:50:33 -04001597 *space_info = found;
Chris Mason4184ea72009-03-10 12:39:20 -04001598 list_add_rcu(&found->list, &info->space_info);
Chris Mason593060d2008-03-25 16:50:33 -04001599 return 0;
1600}
1601
Chris Mason8790d502008-04-03 16:29:03 -04001602static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
1603{
1604 u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
Chris Mason611f0e02008-04-03 16:29:03 -04001605 BTRFS_BLOCK_GROUP_RAID1 |
Chris Mason321aecc2008-04-16 10:49:51 -04001606 BTRFS_BLOCK_GROUP_RAID10 |
Chris Mason611f0e02008-04-03 16:29:03 -04001607 BTRFS_BLOCK_GROUP_DUP);
Chris Mason8790d502008-04-03 16:29:03 -04001608 if (extra_flags) {
1609 if (flags & BTRFS_BLOCK_GROUP_DATA)
1610 fs_info->avail_data_alloc_bits |= extra_flags;
1611 if (flags & BTRFS_BLOCK_GROUP_METADATA)
1612 fs_info->avail_metadata_alloc_bits |= extra_flags;
1613 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
1614 fs_info->avail_system_alloc_bits |= extra_flags;
1615 }
1616}
Chris Mason593060d2008-03-25 16:50:33 -04001617
Yan Zhengc146afa2008-11-12 14:34:12 -05001618static void set_block_group_readonly(struct btrfs_block_group_cache *cache)
1619{
1620 spin_lock(&cache->space_info->lock);
1621 spin_lock(&cache->lock);
1622 if (!cache->ro) {
1623 cache->space_info->bytes_readonly += cache->key.offset -
1624 btrfs_block_group_used(&cache->item);
1625 cache->ro = 1;
1626 }
1627 spin_unlock(&cache->lock);
1628 spin_unlock(&cache->space_info->lock);
1629}
1630
Yan Zheng2b820322008-11-17 21:11:30 -05001631u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
Chris Masonec44a352008-04-28 15:29:52 -04001632{
Yan Zheng2b820322008-11-17 21:11:30 -05001633 u64 num_devices = root->fs_info->fs_devices->rw_devices;
Chris Masona061fc82008-05-07 11:43:44 -04001634
1635 if (num_devices == 1)
1636 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
1637 if (num_devices < 4)
1638 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
1639
Chris Masonec44a352008-04-28 15:29:52 -04001640 if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
1641 (flags & (BTRFS_BLOCK_GROUP_RAID1 |
Chris Masona061fc82008-05-07 11:43:44 -04001642 BTRFS_BLOCK_GROUP_RAID10))) {
Chris Masonec44a352008-04-28 15:29:52 -04001643 flags &= ~BTRFS_BLOCK_GROUP_DUP;
Chris Masona061fc82008-05-07 11:43:44 -04001644 }
Chris Masonec44a352008-04-28 15:29:52 -04001645
1646 if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
Chris Masona061fc82008-05-07 11:43:44 -04001647 (flags & BTRFS_BLOCK_GROUP_RAID10)) {
Chris Masonec44a352008-04-28 15:29:52 -04001648 flags &= ~BTRFS_BLOCK_GROUP_RAID1;
Chris Masona061fc82008-05-07 11:43:44 -04001649 }
Chris Masonec44a352008-04-28 15:29:52 -04001650
1651 if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
1652 ((flags & BTRFS_BLOCK_GROUP_RAID1) |
1653 (flags & BTRFS_BLOCK_GROUP_RAID10) |
1654 (flags & BTRFS_BLOCK_GROUP_DUP)))
1655 flags &= ~BTRFS_BLOCK_GROUP_RAID0;
1656 return flags;
1657}
1658
Josef Bacik6a632092009-02-20 11:00:09 -05001659static u64 btrfs_get_alloc_profile(struct btrfs_root *root, u64 data)
1660{
1661 struct btrfs_fs_info *info = root->fs_info;
1662 u64 alloc_profile;
1663
1664 if (data) {
1665 alloc_profile = info->avail_data_alloc_bits &
1666 info->data_alloc_profile;
1667 data = BTRFS_BLOCK_GROUP_DATA | alloc_profile;
1668 } else if (root == root->fs_info->chunk_root) {
1669 alloc_profile = info->avail_system_alloc_bits &
1670 info->system_alloc_profile;
1671 data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile;
1672 } else {
1673 alloc_profile = info->avail_metadata_alloc_bits &
1674 info->metadata_alloc_profile;
1675 data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile;
1676 }
1677
1678 return btrfs_reduce_alloc_profile(root, data);
1679}
1680
1681void btrfs_set_inode_space_info(struct btrfs_root *root, struct inode *inode)
1682{
1683 u64 alloc_target;
1684
1685 alloc_target = btrfs_get_alloc_profile(root, 1);
1686 BTRFS_I(inode)->space_info = __find_space_info(root->fs_info,
1687 alloc_target);
1688}
1689
1690/*
1691 * for now this just makes sure we have at least 5% of our metadata space free
1692 * for use.
1693 */
1694int btrfs_check_metadata_free_space(struct btrfs_root *root)
1695{
1696 struct btrfs_fs_info *info = root->fs_info;
1697 struct btrfs_space_info *meta_sinfo;
1698 u64 alloc_target, thresh;
Josef Bacik4e06bdd2009-02-20 10:59:53 -05001699 int committed = 0, ret;
Josef Bacik6a632092009-02-20 11:00:09 -05001700
1701 /* get the space info for where the metadata will live */
1702 alloc_target = btrfs_get_alloc_profile(root, 0);
1703 meta_sinfo = __find_space_info(info, alloc_target);
1704
Josef Bacik4e06bdd2009-02-20 10:59:53 -05001705again:
Josef Bacik6a632092009-02-20 11:00:09 -05001706 spin_lock(&meta_sinfo->lock);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05001707 if (!meta_sinfo->full)
1708 thresh = meta_sinfo->total_bytes * 80;
1709 else
1710 thresh = meta_sinfo->total_bytes * 95;
Josef Bacik6a632092009-02-20 11:00:09 -05001711
1712 do_div(thresh, 100);
1713
1714 if (meta_sinfo->bytes_used + meta_sinfo->bytes_reserved +
1715 meta_sinfo->bytes_pinned + meta_sinfo->bytes_readonly > thresh) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05001716 struct btrfs_trans_handle *trans;
1717 if (!meta_sinfo->full) {
1718 meta_sinfo->force_alloc = 1;
1719 spin_unlock(&meta_sinfo->lock);
1720
1721 trans = btrfs_start_transaction(root, 1);
1722 if (!trans)
1723 return -ENOMEM;
1724
1725 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
1726 2 * 1024 * 1024, alloc_target, 0);
1727 btrfs_end_transaction(trans, root);
1728 goto again;
1729 }
Josef Bacik6a632092009-02-20 11:00:09 -05001730 spin_unlock(&meta_sinfo->lock);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05001731
1732 if (!committed) {
1733 committed = 1;
1734 trans = btrfs_join_transaction(root, 1);
1735 if (!trans)
1736 return -ENOMEM;
1737 ret = btrfs_commit_transaction(trans, root);
1738 if (ret)
1739 return ret;
1740 goto again;
1741 }
Josef Bacik6a632092009-02-20 11:00:09 -05001742 return -ENOSPC;
1743 }
1744 spin_unlock(&meta_sinfo->lock);
1745
1746 return 0;
1747}
1748
1749/*
1750 * This will check the space that the inode allocates from to make sure we have
1751 * enough space for bytes.
1752 */
1753int btrfs_check_data_free_space(struct btrfs_root *root, struct inode *inode,
1754 u64 bytes)
1755{
1756 struct btrfs_space_info *data_sinfo;
Josef Bacik4e06bdd2009-02-20 10:59:53 -05001757 int ret = 0, committed = 0;
Josef Bacik6a632092009-02-20 11:00:09 -05001758
1759 /* make sure bytes are sectorsize aligned */
1760 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
1761
1762 data_sinfo = BTRFS_I(inode)->space_info;
1763again:
1764 /* make sure we have enough space to handle the data first */
1765 spin_lock(&data_sinfo->lock);
1766 if (data_sinfo->total_bytes - data_sinfo->bytes_used -
1767 data_sinfo->bytes_delalloc - data_sinfo->bytes_reserved -
1768 data_sinfo->bytes_pinned - data_sinfo->bytes_readonly -
1769 data_sinfo->bytes_may_use < bytes) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05001770 struct btrfs_trans_handle *trans;
1771
Josef Bacik6a632092009-02-20 11:00:09 -05001772 /*
1773 * if we don't have enough free bytes in this space then we need
1774 * to alloc a new chunk.
1775 */
1776 if (!data_sinfo->full) {
1777 u64 alloc_target;
Josef Bacik6a632092009-02-20 11:00:09 -05001778
1779 data_sinfo->force_alloc = 1;
1780 spin_unlock(&data_sinfo->lock);
1781
1782 alloc_target = btrfs_get_alloc_profile(root, 1);
1783 trans = btrfs_start_transaction(root, 1);
1784 if (!trans)
1785 return -ENOMEM;
1786
1787 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
1788 bytes + 2 * 1024 * 1024,
1789 alloc_target, 0);
1790 btrfs_end_transaction(trans, root);
1791 if (ret)
1792 return ret;
1793 goto again;
1794 }
1795 spin_unlock(&data_sinfo->lock);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05001796
1797 /* commit the current transaction and try again */
1798 if (!committed) {
1799 committed = 1;
1800 trans = btrfs_join_transaction(root, 1);
1801 if (!trans)
1802 return -ENOMEM;
1803 ret = btrfs_commit_transaction(trans, root);
1804 if (ret)
1805 return ret;
1806 goto again;
1807 }
1808
Josef Bacik6a632092009-02-20 11:00:09 -05001809 printk(KERN_ERR "no space left, need %llu, %llu delalloc bytes"
1810 ", %llu bytes_used, %llu bytes_reserved, "
1811 "%llu bytes_pinned, %llu bytes_readonly, %llu may use"
1812 "%llu total\n", bytes, data_sinfo->bytes_delalloc,
1813 data_sinfo->bytes_used, data_sinfo->bytes_reserved,
1814 data_sinfo->bytes_pinned, data_sinfo->bytes_readonly,
1815 data_sinfo->bytes_may_use, data_sinfo->total_bytes);
1816 return -ENOSPC;
1817 }
1818 data_sinfo->bytes_may_use += bytes;
1819 BTRFS_I(inode)->reserved_bytes += bytes;
1820 spin_unlock(&data_sinfo->lock);
1821
1822 return btrfs_check_metadata_free_space(root);
1823}
1824
1825/*
1826 * if there was an error for whatever reason after calling
1827 * btrfs_check_data_free_space, call this so we can cleanup the counters.
1828 */
1829void btrfs_free_reserved_data_space(struct btrfs_root *root,
1830 struct inode *inode, u64 bytes)
1831{
1832 struct btrfs_space_info *data_sinfo;
1833
1834 /* make sure bytes are sectorsize aligned */
1835 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
1836
1837 data_sinfo = BTRFS_I(inode)->space_info;
1838 spin_lock(&data_sinfo->lock);
1839 data_sinfo->bytes_may_use -= bytes;
1840 BTRFS_I(inode)->reserved_bytes -= bytes;
1841 spin_unlock(&data_sinfo->lock);
1842}
1843
1844/* called when we are adding a delalloc extent to the inode's io_tree */
1845void btrfs_delalloc_reserve_space(struct btrfs_root *root, struct inode *inode,
1846 u64 bytes)
1847{
1848 struct btrfs_space_info *data_sinfo;
1849
1850 /* get the space info for where this inode will be storing its data */
1851 data_sinfo = BTRFS_I(inode)->space_info;
1852
1853 /* make sure we have enough space to handle the data first */
1854 spin_lock(&data_sinfo->lock);
1855 data_sinfo->bytes_delalloc += bytes;
1856
1857 /*
1858 * we are adding a delalloc extent without calling
1859 * btrfs_check_data_free_space first. This happens on a weird
1860 * writepage condition, but shouldn't hurt our accounting
1861 */
1862 if (unlikely(bytes > BTRFS_I(inode)->reserved_bytes)) {
1863 data_sinfo->bytes_may_use -= BTRFS_I(inode)->reserved_bytes;
1864 BTRFS_I(inode)->reserved_bytes = 0;
1865 } else {
1866 data_sinfo->bytes_may_use -= bytes;
1867 BTRFS_I(inode)->reserved_bytes -= bytes;
1868 }
1869
1870 spin_unlock(&data_sinfo->lock);
1871}
1872
1873/* called when we are clearing an delalloc extent from the inode's io_tree */
1874void btrfs_delalloc_free_space(struct btrfs_root *root, struct inode *inode,
1875 u64 bytes)
1876{
1877 struct btrfs_space_info *info;
1878
1879 info = BTRFS_I(inode)->space_info;
1880
1881 spin_lock(&info->lock);
1882 info->bytes_delalloc -= bytes;
1883 spin_unlock(&info->lock);
1884}
1885
Chris Mason6324fbf2008-03-24 15:01:59 -04001886static int do_chunk_alloc(struct btrfs_trans_handle *trans,
1887 struct btrfs_root *extent_root, u64 alloc_bytes,
Chris Mason0ef3e662008-05-24 14:04:53 -04001888 u64 flags, int force)
Chris Mason6324fbf2008-03-24 15:01:59 -04001889{
1890 struct btrfs_space_info *space_info;
1891 u64 thresh;
Yan Zhengc146afa2008-11-12 14:34:12 -05001892 int ret = 0;
1893
1894 mutex_lock(&extent_root->fs_info->chunk_mutex);
Chris Mason6324fbf2008-03-24 15:01:59 -04001895
Yan Zheng2b820322008-11-17 21:11:30 -05001896 flags = btrfs_reduce_alloc_profile(extent_root, flags);
Chris Masonec44a352008-04-28 15:29:52 -04001897
Chris Mason6324fbf2008-03-24 15:01:59 -04001898 space_info = __find_space_info(extent_root->fs_info, flags);
Chris Mason593060d2008-03-25 16:50:33 -04001899 if (!space_info) {
1900 ret = update_space_info(extent_root->fs_info, flags,
1901 0, 0, &space_info);
1902 BUG_ON(ret);
1903 }
Chris Mason6324fbf2008-03-24 15:01:59 -04001904 BUG_ON(!space_info);
1905
Josef Bacik25179202008-10-29 14:49:05 -04001906 spin_lock(&space_info->lock);
Chris Mason0ef3e662008-05-24 14:04:53 -04001907 if (space_info->force_alloc) {
1908 force = 1;
1909 space_info->force_alloc = 0;
1910 }
Josef Bacik25179202008-10-29 14:49:05 -04001911 if (space_info->full) {
1912 spin_unlock(&space_info->lock);
Chris Mason925baed2008-06-25 16:01:30 -04001913 goto out;
Josef Bacik25179202008-10-29 14:49:05 -04001914 }
Chris Mason6324fbf2008-03-24 15:01:59 -04001915
Yan Zhengc146afa2008-11-12 14:34:12 -05001916 thresh = space_info->total_bytes - space_info->bytes_readonly;
1917 thresh = div_factor(thresh, 6);
Chris Mason0ef3e662008-05-24 14:04:53 -04001918 if (!force &&
Zheng Yane8569812008-09-26 10:05:48 -04001919 (space_info->bytes_used + space_info->bytes_pinned +
Josef Bacik25179202008-10-29 14:49:05 -04001920 space_info->bytes_reserved + alloc_bytes) < thresh) {
1921 spin_unlock(&space_info->lock);
Chris Mason925baed2008-06-25 16:01:30 -04001922 goto out;
Josef Bacik25179202008-10-29 14:49:05 -04001923 }
Josef Bacik25179202008-10-29 14:49:05 -04001924 spin_unlock(&space_info->lock);
1925
Yan Zheng2b820322008-11-17 21:11:30 -05001926 ret = btrfs_alloc_chunk(trans, extent_root, flags);
Chris Masond3977122009-01-05 21:25:51 -05001927 if (ret)
Chris Mason6324fbf2008-03-24 15:01:59 -04001928 space_info->full = 1;
Chris Masona74a4b92008-06-25 16:01:31 -04001929out:
Yan Zhengc146afa2008-11-12 14:34:12 -05001930 mutex_unlock(&extent_root->fs_info->chunk_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001931 return ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04001932}
1933
Chris Mason9078a3e2007-04-26 16:46:15 -04001934static int update_block_group(struct btrfs_trans_handle *trans,
1935 struct btrfs_root *root,
Chris Masondb945352007-10-15 16:15:53 -04001936 u64 bytenr, u64 num_bytes, int alloc,
Chris Mason0b86a832008-03-24 15:01:56 -04001937 int mark_free)
Chris Mason9078a3e2007-04-26 16:46:15 -04001938{
1939 struct btrfs_block_group_cache *cache;
1940 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -04001941 u64 total = num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04001942 u64 old_val;
Chris Masondb945352007-10-15 16:15:53 -04001943 u64 byte_in_group;
Chris Mason3e1ad542007-05-07 20:03:49 -04001944
Chris Masond3977122009-01-05 21:25:51 -05001945 while (total) {
Chris Masondb945352007-10-15 16:15:53 -04001946 cache = btrfs_lookup_block_group(info, bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05001947 if (!cache)
Chris Mason9078a3e2007-04-26 16:46:15 -04001948 return -1;
Chris Masondb945352007-10-15 16:15:53 -04001949 byte_in_group = bytenr - cache->key.objectid;
1950 WARN_ON(byte_in_group > cache->key.offset);
Chris Mason9078a3e2007-04-26 16:46:15 -04001951
Josef Bacik25179202008-10-29 14:49:05 -04001952 spin_lock(&cache->space_info->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04001953 spin_lock(&cache->lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001954 cache->dirty = 1;
Chris Mason9078a3e2007-04-26 16:46:15 -04001955 old_val = btrfs_block_group_used(&cache->item);
Chris Masondb945352007-10-15 16:15:53 -04001956 num_bytes = min(total, cache->key.offset - byte_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -04001957 if (alloc) {
Chris Masondb945352007-10-15 16:15:53 -04001958 old_val += num_bytes;
Chris Mason6324fbf2008-03-24 15:01:59 -04001959 cache->space_info->bytes_used += num_bytes;
Yan Zhenga512bbf2008-12-08 16:46:26 -05001960 if (cache->ro)
Yan Zhengc146afa2008-11-12 14:34:12 -05001961 cache->space_info->bytes_readonly -= num_bytes;
Chris Masonc286ac42008-07-22 23:06:41 -04001962 btrfs_set_block_group_used(&cache->item, old_val);
1963 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04001964 spin_unlock(&cache->space_info->lock);
Chris Masoncd1bc462007-04-27 10:08:34 -04001965 } else {
Chris Masondb945352007-10-15 16:15:53 -04001966 old_val -= num_bytes;
Chris Mason6324fbf2008-03-24 15:01:59 -04001967 cache->space_info->bytes_used -= num_bytes;
Yan Zhengc146afa2008-11-12 14:34:12 -05001968 if (cache->ro)
1969 cache->space_info->bytes_readonly += num_bytes;
Chris Masonc286ac42008-07-22 23:06:41 -04001970 btrfs_set_block_group_used(&cache->item, old_val);
1971 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04001972 spin_unlock(&cache->space_info->lock);
Chris Masonf510cfe2007-10-15 16:14:48 -04001973 if (mark_free) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001974 int ret;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001975
1976 ret = btrfs_discard_extent(root, bytenr,
1977 num_bytes);
1978 WARN_ON(ret);
1979
Josef Bacik0f9dd462008-09-23 13:14:11 -04001980 ret = btrfs_add_free_space(cache, bytenr,
1981 num_bytes);
Yan Zhengd2fb3432008-12-11 16:30:39 -05001982 WARN_ON(ret);
Chris Masone37c9e62007-05-09 20:13:14 -04001983 }
Chris Masoncd1bc462007-04-27 10:08:34 -04001984 }
Yan Zhengd2fb3432008-12-11 16:30:39 -05001985 put_block_group(cache);
Chris Masondb945352007-10-15 16:15:53 -04001986 total -= num_bytes;
1987 bytenr += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04001988 }
1989 return 0;
1990}
Chris Mason6324fbf2008-03-24 15:01:59 -04001991
Chris Masona061fc82008-05-07 11:43:44 -04001992static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
1993{
Josef Bacik0f9dd462008-09-23 13:14:11 -04001994 struct btrfs_block_group_cache *cache;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001995 u64 bytenr;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001996
1997 cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
1998 if (!cache)
Chris Masona061fc82008-05-07 11:43:44 -04001999 return 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002000
Yan Zhengd2fb3432008-12-11 16:30:39 -05002001 bytenr = cache->key.objectid;
2002 put_block_group(cache);
2003
2004 return bytenr;
Chris Masona061fc82008-05-07 11:43:44 -04002005}
2006
Chris Masone02119d2008-09-05 16:13:11 -04002007int btrfs_update_pinned_extents(struct btrfs_root *root,
Yan324ae4d2007-11-16 14:57:08 -05002008 u64 bytenr, u64 num, int pin)
2009{
2010 u64 len;
2011 struct btrfs_block_group_cache *cache;
2012 struct btrfs_fs_info *fs_info = root->fs_info;
2013
Josef Bacik25179202008-10-29 14:49:05 -04002014 WARN_ON(!mutex_is_locked(&root->fs_info->pinned_mutex));
Yan324ae4d2007-11-16 14:57:08 -05002015 if (pin) {
2016 set_extent_dirty(&fs_info->pinned_extents,
2017 bytenr, bytenr + num - 1, GFP_NOFS);
2018 } else {
2019 clear_extent_dirty(&fs_info->pinned_extents,
2020 bytenr, bytenr + num - 1, GFP_NOFS);
2021 }
2022 while (num > 0) {
2023 cache = btrfs_lookup_block_group(fs_info, bytenr);
Zheng Yane8569812008-09-26 10:05:48 -04002024 BUG_ON(!cache);
2025 len = min(num, cache->key.offset -
2026 (bytenr - cache->key.objectid));
Yan324ae4d2007-11-16 14:57:08 -05002027 if (pin) {
Josef Bacik25179202008-10-29 14:49:05 -04002028 spin_lock(&cache->space_info->lock);
Zheng Yane8569812008-09-26 10:05:48 -04002029 spin_lock(&cache->lock);
2030 cache->pinned += len;
2031 cache->space_info->bytes_pinned += len;
2032 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04002033 spin_unlock(&cache->space_info->lock);
Yan324ae4d2007-11-16 14:57:08 -05002034 fs_info->total_pinned += len;
2035 } else {
Josef Bacik25179202008-10-29 14:49:05 -04002036 spin_lock(&cache->space_info->lock);
Zheng Yane8569812008-09-26 10:05:48 -04002037 spin_lock(&cache->lock);
2038 cache->pinned -= len;
2039 cache->space_info->bytes_pinned -= len;
2040 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04002041 spin_unlock(&cache->space_info->lock);
Yan324ae4d2007-11-16 14:57:08 -05002042 fs_info->total_pinned -= len;
Josef Bacik07103a32008-11-19 15:17:55 -05002043 if (cache->cached)
2044 btrfs_add_free_space(cache, bytenr, len);
Yan324ae4d2007-11-16 14:57:08 -05002045 }
Yan Zhengd2fb3432008-12-11 16:30:39 -05002046 put_block_group(cache);
Yan324ae4d2007-11-16 14:57:08 -05002047 bytenr += len;
2048 num -= len;
2049 }
2050 return 0;
2051}
Chris Mason9078a3e2007-04-26 16:46:15 -04002052
Zheng Yane8569812008-09-26 10:05:48 -04002053static int update_reserved_extents(struct btrfs_root *root,
2054 u64 bytenr, u64 num, int reserve)
2055{
2056 u64 len;
2057 struct btrfs_block_group_cache *cache;
2058 struct btrfs_fs_info *fs_info = root->fs_info;
2059
Zheng Yane8569812008-09-26 10:05:48 -04002060 while (num > 0) {
2061 cache = btrfs_lookup_block_group(fs_info, bytenr);
2062 BUG_ON(!cache);
2063 len = min(num, cache->key.offset -
2064 (bytenr - cache->key.objectid));
Josef Bacik25179202008-10-29 14:49:05 -04002065
2066 spin_lock(&cache->space_info->lock);
2067 spin_lock(&cache->lock);
Zheng Yane8569812008-09-26 10:05:48 -04002068 if (reserve) {
Zheng Yane8569812008-09-26 10:05:48 -04002069 cache->reserved += len;
2070 cache->space_info->bytes_reserved += len;
Zheng Yane8569812008-09-26 10:05:48 -04002071 } else {
Zheng Yane8569812008-09-26 10:05:48 -04002072 cache->reserved -= len;
2073 cache->space_info->bytes_reserved -= len;
Zheng Yane8569812008-09-26 10:05:48 -04002074 }
Josef Bacik25179202008-10-29 14:49:05 -04002075 spin_unlock(&cache->lock);
2076 spin_unlock(&cache->space_info->lock);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002077 put_block_group(cache);
Zheng Yane8569812008-09-26 10:05:48 -04002078 bytenr += len;
2079 num -= len;
2080 }
2081 return 0;
2082}
2083
Chris Masond1310b22008-01-24 16:13:08 -05002084int btrfs_copy_pinned(struct btrfs_root *root, struct extent_io_tree *copy)
Chris Masonccd467d2007-06-28 15:57:36 -04002085{
Chris Masonccd467d2007-06-28 15:57:36 -04002086 u64 last = 0;
Chris Mason1a5bc162007-10-15 16:15:26 -04002087 u64 start;
2088 u64 end;
Chris Masond1310b22008-01-24 16:13:08 -05002089 struct extent_io_tree *pinned_extents = &root->fs_info->pinned_extents;
Chris Masonccd467d2007-06-28 15:57:36 -04002090 int ret;
Chris Masonccd467d2007-06-28 15:57:36 -04002091
Josef Bacik25179202008-10-29 14:49:05 -04002092 mutex_lock(&root->fs_info->pinned_mutex);
Chris Masond3977122009-01-05 21:25:51 -05002093 while (1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04002094 ret = find_first_extent_bit(pinned_extents, last,
2095 &start, &end, EXTENT_DIRTY);
2096 if (ret)
Chris Masonccd467d2007-06-28 15:57:36 -04002097 break;
Chris Mason1a5bc162007-10-15 16:15:26 -04002098 set_extent_dirty(copy, start, end, GFP_NOFS);
2099 last = end + 1;
Chris Masonccd467d2007-06-28 15:57:36 -04002100 }
Josef Bacik25179202008-10-29 14:49:05 -04002101 mutex_unlock(&root->fs_info->pinned_mutex);
Chris Masonccd467d2007-06-28 15:57:36 -04002102 return 0;
2103}
2104
2105int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
2106 struct btrfs_root *root,
Chris Masond1310b22008-01-24 16:13:08 -05002107 struct extent_io_tree *unpin)
Chris Masona28ec192007-03-06 20:08:01 -05002108{
Chris Mason1a5bc162007-10-15 16:15:26 -04002109 u64 start;
2110 u64 end;
Chris Masona28ec192007-03-06 20:08:01 -05002111 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05002112
Josef Bacik25179202008-10-29 14:49:05 -04002113 mutex_lock(&root->fs_info->pinned_mutex);
Chris Masond3977122009-01-05 21:25:51 -05002114 while (1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04002115 ret = find_first_extent_bit(unpin, 0, &start, &end,
2116 EXTENT_DIRTY);
2117 if (ret)
Chris Masona28ec192007-03-06 20:08:01 -05002118 break;
Liu Hui1f3c79a2009-01-05 15:57:51 -05002119
2120 ret = btrfs_discard_extent(root, start, end + 1 - start);
2121
Chris Masone02119d2008-09-05 16:13:11 -04002122 btrfs_update_pinned_extents(root, start, end + 1 - start, 0);
Chris Mason1a5bc162007-10-15 16:15:26 -04002123 clear_extent_dirty(unpin, start, end, GFP_NOFS);
Liu Hui1f3c79a2009-01-05 15:57:51 -05002124
Chris Masonc286ac42008-07-22 23:06:41 -04002125 if (need_resched()) {
Josef Bacik25179202008-10-29 14:49:05 -04002126 mutex_unlock(&root->fs_info->pinned_mutex);
Chris Masonc286ac42008-07-22 23:06:41 -04002127 cond_resched();
Josef Bacik25179202008-10-29 14:49:05 -04002128 mutex_lock(&root->fs_info->pinned_mutex);
Chris Masonc286ac42008-07-22 23:06:41 -04002129 }
Chris Masona28ec192007-03-06 20:08:01 -05002130 }
Josef Bacik25179202008-10-29 14:49:05 -04002131 mutex_unlock(&root->fs_info->pinned_mutex);
Liu Hui1f3c79a2009-01-05 15:57:51 -05002132 return ret;
Chris Masona28ec192007-03-06 20:08:01 -05002133}
2134
Zheng Yan31840ae2008-09-23 13:14:14 -04002135static int pin_down_bytes(struct btrfs_trans_handle *trans,
2136 struct btrfs_root *root,
2137 u64 bytenr, u64 num_bytes, int is_data)
Chris Masone20d96d2007-03-22 12:13:20 -04002138{
Chris Mason1a5bc162007-10-15 16:15:26 -04002139 int err = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04002140 struct extent_buffer *buf;
Chris Mason78fae272007-03-25 11:35:08 -04002141
Zheng Yan31840ae2008-09-23 13:14:14 -04002142 if (is_data)
2143 goto pinit;
Chris Mason4bef0842008-09-08 11:18:08 -04002144
Zheng Yan31840ae2008-09-23 13:14:14 -04002145 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
2146 if (!buf)
2147 goto pinit;
Chris Mason4bef0842008-09-08 11:18:08 -04002148
Zheng Yan31840ae2008-09-23 13:14:14 -04002149 /* we can reuse a block if it hasn't been written
2150 * and it is from this transaction. We can't
2151 * reuse anything from the tree log root because
2152 * it has tiny sub-transactions.
2153 */
2154 if (btrfs_buffer_uptodate(buf, 0) &&
2155 btrfs_try_tree_lock(buf)) {
2156 u64 header_owner = btrfs_header_owner(buf);
2157 u64 header_transid = btrfs_header_generation(buf);
2158 if (header_owner != BTRFS_TREE_LOG_OBJECTID &&
Zheng Yan1a40e232008-09-26 10:09:34 -04002159 header_owner != BTRFS_TREE_RELOC_OBJECTID &&
Chris Mason56bec292009-03-13 10:10:06 -04002160 header_owner != BTRFS_DATA_RELOC_TREE_OBJECTID &&
Zheng Yan31840ae2008-09-23 13:14:14 -04002161 header_transid == trans->transid &&
2162 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
2163 clean_tree_block(NULL, root, buf);
2164 btrfs_tree_unlock(buf);
Chris Mason5f39d392007-10-15 16:14:19 -04002165 free_extent_buffer(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04002166 return 1;
Chris Mason8ef97622007-03-26 10:15:30 -04002167 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002168 btrfs_tree_unlock(buf);
Chris Masonf4b9aa82007-03-27 11:05:53 -04002169 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002170 free_extent_buffer(buf);
2171pinit:
2172 btrfs_update_pinned_extents(root, bytenr, num_bytes, 1);
2173
Chris Masonbe744172007-05-06 10:15:01 -04002174 BUG_ON(err < 0);
Chris Masone20d96d2007-03-22 12:13:20 -04002175 return 0;
2176}
2177
Chris Masona28ec192007-03-06 20:08:01 -05002178/*
2179 * remove an extent from the root, returns 0 on success
2180 */
Zheng Yan31840ae2008-09-23 13:14:14 -04002181static int __free_extent(struct btrfs_trans_handle *trans,
2182 struct btrfs_root *root,
2183 u64 bytenr, u64 num_bytes, u64 parent,
Chris Mason7bb86312007-12-11 09:25:06 -05002184 u64 root_objectid, u64 ref_generation,
Chris Mason56bec292009-03-13 10:10:06 -04002185 u64 owner_objectid, int pin, int mark_free,
2186 int refs_to_drop)
Chris Masona28ec192007-03-06 20:08:01 -05002187{
Chris Mason5caf2a02007-04-02 11:20:42 -04002188 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -04002189 struct btrfs_key key;
Chris Mason1261ec42007-03-20 20:35:03 -04002190 struct btrfs_fs_info *info = root->fs_info;
2191 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04002192 struct extent_buffer *leaf;
Chris Masona28ec192007-03-06 20:08:01 -05002193 int ret;
Chris Mason952fcca2008-02-18 16:33:44 -05002194 int extent_slot = 0;
2195 int found_extent = 0;
2196 int num_to_del = 1;
Chris Mason234b63a2007-03-13 10:46:10 -04002197 struct btrfs_extent_item *ei;
Chris Masoncf27e1e2007-03-13 09:49:06 -04002198 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -05002199
Chris Masondb945352007-10-15 16:15:53 -04002200 key.objectid = bytenr;
Chris Mason62e27492007-03-15 12:56:47 -04002201 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masondb945352007-10-15 16:15:53 -04002202 key.offset = num_bytes;
Chris Mason5caf2a02007-04-02 11:20:42 -04002203 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04002204 if (!path)
2205 return -ENOMEM;
2206
Chris Mason3c12ac72008-04-21 12:01:38 -04002207 path->reada = 1;
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002208 ret = lookup_extent_backref(trans, extent_root, path,
2209 bytenr, parent, root_objectid,
2210 ref_generation, owner_objectid, 1);
Chris Mason7bb86312007-12-11 09:25:06 -05002211 if (ret == 0) {
Chris Mason952fcca2008-02-18 16:33:44 -05002212 struct btrfs_key found_key;
2213 extent_slot = path->slots[0];
Chris Masond3977122009-01-05 21:25:51 -05002214 while (extent_slot > 0) {
Chris Mason952fcca2008-02-18 16:33:44 -05002215 extent_slot--;
2216 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2217 extent_slot);
2218 if (found_key.objectid != bytenr)
2219 break;
2220 if (found_key.type == BTRFS_EXTENT_ITEM_KEY &&
2221 found_key.offset == num_bytes) {
2222 found_extent = 1;
2223 break;
2224 }
2225 if (path->slots[0] - extent_slot > 5)
2226 break;
2227 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002228 if (!found_extent) {
Chris Mason56bec292009-03-13 10:10:06 -04002229 ret = remove_extent_backref(trans, extent_root, path,
2230 refs_to_drop);
Zheng Yan31840ae2008-09-23 13:14:14 -04002231 BUG_ON(ret);
2232 btrfs_release_path(extent_root, path);
2233 ret = btrfs_search_slot(trans, extent_root,
2234 &key, path, -1, 1);
Josef Bacikf3465ca2008-11-12 14:19:50 -05002235 if (ret) {
2236 printk(KERN_ERR "umm, got %d back from search"
Chris Masond3977122009-01-05 21:25:51 -05002237 ", was looking for %llu\n", ret,
2238 (unsigned long long)bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05002239 btrfs_print_leaf(extent_root, path->nodes[0]);
2240 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002241 BUG_ON(ret);
2242 extent_slot = path->slots[0];
2243 }
Chris Mason7bb86312007-12-11 09:25:06 -05002244 } else {
2245 btrfs_print_leaf(extent_root, path->nodes[0]);
2246 WARN_ON(1);
Chris Masond3977122009-01-05 21:25:51 -05002247 printk(KERN_ERR "btrfs unable to find ref byte nr %llu "
Chris Mason56bec292009-03-13 10:10:06 -04002248 "parent %llu root %llu gen %llu owner %llu\n",
Chris Masond3977122009-01-05 21:25:51 -05002249 (unsigned long long)bytenr,
Chris Mason56bec292009-03-13 10:10:06 -04002250 (unsigned long long)parent,
Chris Masond3977122009-01-05 21:25:51 -05002251 (unsigned long long)root_objectid,
2252 (unsigned long long)ref_generation,
2253 (unsigned long long)owner_objectid);
Chris Mason7bb86312007-12-11 09:25:06 -05002254 }
Chris Mason5f39d392007-10-15 16:14:19 -04002255
2256 leaf = path->nodes[0];
Chris Mason952fcca2008-02-18 16:33:44 -05002257 ei = btrfs_item_ptr(leaf, extent_slot,
Chris Mason123abc82007-03-14 14:14:43 -04002258 struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002259 refs = btrfs_extent_refs(leaf, ei);
Chris Mason952fcca2008-02-18 16:33:44 -05002260
Chris Mason56bec292009-03-13 10:10:06 -04002261 /*
2262 * we're not allowed to delete the extent item if there
2263 * are other delayed ref updates pending
2264 */
2265
2266 BUG_ON(refs < refs_to_drop);
2267 refs -= refs_to_drop;
2268 btrfs_set_extent_refs(leaf, ei, refs);
Chris Mason5f39d392007-10-15 16:14:19 -04002269 btrfs_mark_buffer_dirty(leaf);
2270
Chris Mason56bec292009-03-13 10:10:06 -04002271 if (refs == 0 && found_extent &&
2272 path->slots[0] == extent_slot + 1) {
Zheng Yan31840ae2008-09-23 13:14:14 -04002273 struct btrfs_extent_ref *ref;
2274 ref = btrfs_item_ptr(leaf, path->slots[0],
2275 struct btrfs_extent_ref);
Chris Mason56bec292009-03-13 10:10:06 -04002276 BUG_ON(btrfs_ref_num_refs(leaf, ref) != refs_to_drop);
Chris Mason952fcca2008-02-18 16:33:44 -05002277 /* if the back ref and the extent are next to each other
2278 * they get deleted below in one shot
2279 */
2280 path->slots[0] = extent_slot;
2281 num_to_del = 2;
2282 } else if (found_extent) {
2283 /* otherwise delete the extent back ref */
Chris Mason56bec292009-03-13 10:10:06 -04002284 ret = remove_extent_backref(trans, extent_root, path,
2285 refs_to_drop);
Chris Mason952fcca2008-02-18 16:33:44 -05002286 BUG_ON(ret);
2287 /* if refs are 0, we need to setup the path for deletion */
2288 if (refs == 0) {
2289 btrfs_release_path(extent_root, path);
2290 ret = btrfs_search_slot(trans, extent_root, &key, path,
2291 -1, 1);
Chris Mason952fcca2008-02-18 16:33:44 -05002292 BUG_ON(ret);
2293 }
2294 }
2295
Chris Masoncf27e1e2007-03-13 09:49:06 -04002296 if (refs == 0) {
Chris Masondb945352007-10-15 16:15:53 -04002297 u64 super_used;
2298 u64 root_used;
Chris Mason78fae272007-03-25 11:35:08 -04002299
2300 if (pin) {
Josef Bacik25179202008-10-29 14:49:05 -04002301 mutex_lock(&root->fs_info->pinned_mutex);
Zheng Yan31840ae2008-09-23 13:14:14 -04002302 ret = pin_down_bytes(trans, root, bytenr, num_bytes,
2303 owner_objectid >= BTRFS_FIRST_FREE_OBJECTID);
Josef Bacik25179202008-10-29 14:49:05 -04002304 mutex_unlock(&root->fs_info->pinned_mutex);
Yanc5492282007-11-06 10:25:25 -05002305 if (ret > 0)
2306 mark_free = 1;
2307 BUG_ON(ret < 0);
Chris Mason78fae272007-03-25 11:35:08 -04002308 }
Josef Bacik58176a92007-08-29 15:47:34 -04002309 /* block accounting for super block */
Chris Mason75eff682008-12-15 15:54:40 -05002310 spin_lock(&info->delalloc_lock);
Chris Masondb945352007-10-15 16:15:53 -04002311 super_used = btrfs_super_bytes_used(&info->super_copy);
2312 btrfs_set_super_bytes_used(&info->super_copy,
2313 super_used - num_bytes);
Josef Bacik58176a92007-08-29 15:47:34 -04002314
2315 /* block accounting for root item */
Chris Masondb945352007-10-15 16:15:53 -04002316 root_used = btrfs_root_used(&root->root_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002317 btrfs_set_root_used(&root->root_item,
Chris Masondb945352007-10-15 16:15:53 -04002318 root_used - num_bytes);
Yan Zheng34bf63c2008-12-19 10:58:46 -05002319 spin_unlock(&info->delalloc_lock);
Chris Mason952fcca2008-02-18 16:33:44 -05002320 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
2321 num_to_del);
Zheng Yan31840ae2008-09-23 13:14:14 -04002322 BUG_ON(ret);
Josef Bacik25179202008-10-29 14:49:05 -04002323 btrfs_release_path(extent_root, path);
David Woodhouse21af8042008-08-12 14:13:26 +01002324
Chris Mason459931e2008-12-10 09:10:46 -05002325 if (owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
2326 ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
2327 BUG_ON(ret);
2328 }
2329
Chris Masondcbdd4d2008-12-16 13:51:01 -05002330 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
2331 mark_free);
2332 BUG_ON(ret);
Chris Masona28ec192007-03-06 20:08:01 -05002333 }
Chris Mason5caf2a02007-04-02 11:20:42 -04002334 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -05002335 return ret;
2336}
2337
2338/*
Chris Masonfec577f2007-02-26 10:40:21 -05002339 * remove an extent from the root, returns 0 on success
2340 */
Chris Mason925baed2008-06-25 16:01:30 -04002341static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
Chris Mason56bec292009-03-13 10:10:06 -04002342 struct btrfs_root *root,
2343 u64 bytenr, u64 num_bytes, u64 parent,
2344 u64 root_objectid, u64 ref_generation,
2345 u64 owner_objectid, int pin,
2346 int refs_to_drop)
Chris Masonfec577f2007-02-26 10:40:21 -05002347{
Chris Masondb945352007-10-15 16:15:53 -04002348 WARN_ON(num_bytes < root->sectorsize);
Josef Bacikf3465ca2008-11-12 14:19:50 -05002349
Chris Mason56bec292009-03-13 10:10:06 -04002350 /*
2351 * if metadata always pin
2352 * if data pin when any transaction has committed this
2353 */
2354 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID ||
2355 ref_generation != trans->transid)
Chris Mason4bef0842008-09-08 11:18:08 -04002356 pin = 1;
2357
Chris Mason4bef0842008-09-08 11:18:08 -04002358 if (ref_generation != trans->transid)
2359 pin = 1;
2360
Chris Mason56bec292009-03-13 10:10:06 -04002361 return __free_extent(trans, root, bytenr, num_bytes, parent,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002362 root_objectid, ref_generation,
Chris Mason56bec292009-03-13 10:10:06 -04002363 owner_objectid, pin, pin == 0, refs_to_drop);
Chris Masonfec577f2007-02-26 10:40:21 -05002364}
2365
Chris Mason1887be62009-03-13 10:11:24 -04002366/*
2367 * when we free an extent, it is possible (and likely) that we free the last
2368 * delayed ref for that extent as well. This searches the delayed ref tree for
2369 * a given extent, and if there are no other delayed refs to be processed, it
2370 * removes it from the tree.
2371 */
2372static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
2373 struct btrfs_root *root, u64 bytenr)
2374{
2375 struct btrfs_delayed_ref_head *head;
2376 struct btrfs_delayed_ref_root *delayed_refs;
2377 struct btrfs_delayed_ref_node *ref;
2378 struct rb_node *node;
2379 int ret;
2380
2381 delayed_refs = &trans->transaction->delayed_refs;
2382 spin_lock(&delayed_refs->lock);
2383 head = btrfs_find_delayed_ref_head(trans, bytenr);
2384 if (!head)
2385 goto out;
2386
2387 node = rb_prev(&head->node.rb_node);
2388 if (!node)
2389 goto out;
2390
2391 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2392
2393 /* there are still entries for this ref, we can't drop it */
2394 if (ref->bytenr == bytenr)
2395 goto out;
2396
2397 /*
2398 * waiting for the lock here would deadlock. If someone else has it
2399 * locked they are already in the process of dropping it anyway
2400 */
2401 if (!mutex_trylock(&head->mutex))
2402 goto out;
2403
2404 /*
2405 * at this point we have a head with no other entries. Go
2406 * ahead and process it.
2407 */
2408 head->node.in_tree = 0;
2409 rb_erase(&head->node.rb_node, &delayed_refs->root);
2410 delayed_refs->num_entries--;
2411
2412 /*
2413 * we don't take a ref on the node because we're removing it from the
2414 * tree, so we just steal the ref the tree was holding.
2415 */
2416 spin_unlock(&delayed_refs->lock);
2417
2418 ret = run_one_delayed_ref(trans, root->fs_info->tree_root,
2419 &head->node, head->must_insert_reserved);
2420 BUG_ON(ret);
2421 btrfs_put_delayed_ref(&head->node);
2422 return 0;
2423out:
2424 spin_unlock(&delayed_refs->lock);
2425 return 0;
2426}
2427
Chris Mason925baed2008-06-25 16:01:30 -04002428int btrfs_free_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04002429 struct btrfs_root *root,
2430 u64 bytenr, u64 num_bytes, u64 parent,
2431 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002432 u64 owner_objectid, int pin)
Chris Mason925baed2008-06-25 16:01:30 -04002433{
2434 int ret;
2435
Chris Mason56bec292009-03-13 10:10:06 -04002436 /*
2437 * tree log blocks never actually go into the extent allocation
2438 * tree, just update pinning info and exit early.
2439 *
2440 * data extents referenced by the tree log do need to have
2441 * their reference counts bumped.
2442 */
2443 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID &&
2444 owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
2445 mutex_lock(&root->fs_info->pinned_mutex);
2446 btrfs_update_pinned_extents(root, bytenr, num_bytes, 1);
2447 mutex_unlock(&root->fs_info->pinned_mutex);
2448 update_reserved_extents(root, bytenr, num_bytes, 0);
2449 ret = 0;
2450 } else {
2451 ret = btrfs_add_delayed_ref(trans, bytenr, num_bytes, parent,
2452 root_objectid, ref_generation,
2453 owner_objectid,
2454 BTRFS_DROP_DELAYED_REF, 1);
Chris Mason1887be62009-03-13 10:11:24 -04002455 BUG_ON(ret);
2456 ret = check_ref_cleanup(trans, root, bytenr);
2457 BUG_ON(ret);
Chris Mason56bec292009-03-13 10:10:06 -04002458 }
Chris Mason925baed2008-06-25 16:01:30 -04002459 return ret;
2460}
2461
Chris Mason87ee04e2007-11-30 11:30:34 -05002462static u64 stripe_align(struct btrfs_root *root, u64 val)
2463{
2464 u64 mask = ((u64)root->stripesize - 1);
2465 u64 ret = (val + mask) & ~mask;
2466 return ret;
2467}
2468
Chris Masonfec577f2007-02-26 10:40:21 -05002469/*
2470 * walks the btree of allocated extents and find a hole of a given size.
2471 * The key ins is changed to record the hole:
2472 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -04002473 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -05002474 * ins->offset == number of blocks
2475 * Any available blocks before search_start are skipped.
2476 */
Chris Masond3977122009-01-05 21:25:51 -05002477static noinline int find_free_extent(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05002478 struct btrfs_root *orig_root,
2479 u64 num_bytes, u64 empty_size,
2480 u64 search_start, u64 search_end,
2481 u64 hint_byte, struct btrfs_key *ins,
2482 u64 exclude_start, u64 exclude_nr,
2483 int data)
Chris Masonfec577f2007-02-26 10:40:21 -05002484{
Josef Bacik80eb2342008-10-29 14:49:05 -04002485 int ret = 0;
Chris Masond3977122009-01-05 21:25:51 -05002486 struct btrfs_root *root = orig_root->fs_info->extent_root;
Chris Masondb945352007-10-15 16:15:53 -04002487 u64 total_needed = num_bytes;
Chris Mason239b14b2008-03-24 15:02:07 -04002488 u64 *last_ptr = NULL;
Chris Mason43662112008-11-07 09:06:11 -05002489 u64 last_wanted = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04002490 struct btrfs_block_group_cache *block_group = NULL;
Chris Mason0ef3e662008-05-24 14:04:53 -04002491 int chunk_alloc_done = 0;
Chris Mason239b14b2008-03-24 15:02:07 -04002492 int empty_cluster = 2 * 1024 * 1024;
Chris Mason0ef3e662008-05-24 14:04:53 -04002493 int allowed_chunk_alloc = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04002494 struct list_head *head = NULL, *cur = NULL;
2495 int loop = 0;
Chris Masonf5a31e12008-11-10 11:47:09 -05002496 int extra_loop = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04002497 struct btrfs_space_info *space_info;
Chris Masonfec577f2007-02-26 10:40:21 -05002498
Chris Masondb945352007-10-15 16:15:53 -04002499 WARN_ON(num_bytes < root->sectorsize);
Chris Masonb1a4d962007-04-04 15:27:52 -04002500 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
Josef Bacik80eb2342008-10-29 14:49:05 -04002501 ins->objectid = 0;
2502 ins->offset = 0;
Chris Masonb1a4d962007-04-04 15:27:52 -04002503
Chris Mason0ef3e662008-05-24 14:04:53 -04002504 if (orig_root->ref_cows || empty_size)
2505 allowed_chunk_alloc = 1;
2506
Chris Mason239b14b2008-03-24 15:02:07 -04002507 if (data & BTRFS_BLOCK_GROUP_METADATA) {
2508 last_ptr = &root->fs_info->last_alloc;
Chris Mason536ac8a2009-02-12 09:41:38 -05002509 if (!btrfs_test_opt(root, SSD))
2510 empty_cluster = 64 * 1024;
Chris Mason239b14b2008-03-24 15:02:07 -04002511 }
2512
Josef Bacik0f9dd462008-09-23 13:14:11 -04002513 if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD))
Chris Mason239b14b2008-03-24 15:02:07 -04002514 last_ptr = &root->fs_info->last_data_alloc;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002515
Chris Mason239b14b2008-03-24 15:02:07 -04002516 if (last_ptr) {
Chris Mason43662112008-11-07 09:06:11 -05002517 if (*last_ptr) {
Chris Mason239b14b2008-03-24 15:02:07 -04002518 hint_byte = *last_ptr;
Chris Mason43662112008-11-07 09:06:11 -05002519 last_wanted = *last_ptr;
2520 } else
Chris Mason239b14b2008-03-24 15:02:07 -04002521 empty_size += empty_cluster;
Chris Mason43662112008-11-07 09:06:11 -05002522 } else {
2523 empty_cluster = 0;
Chris Mason239b14b2008-03-24 15:02:07 -04002524 }
Chris Masona061fc82008-05-07 11:43:44 -04002525 search_start = max(search_start, first_logical_byte(root, 0));
Chris Mason239b14b2008-03-24 15:02:07 -04002526 search_start = max(search_start, hint_byte);
Chris Mason0b86a832008-03-24 15:01:56 -04002527
Chris Mason42e70e72008-11-07 18:17:11 -05002528 if (last_wanted && search_start != last_wanted) {
Chris Mason43662112008-11-07 09:06:11 -05002529 last_wanted = 0;
Chris Mason42e70e72008-11-07 18:17:11 -05002530 empty_size += empty_cluster;
2531 }
Chris Mason43662112008-11-07 09:06:11 -05002532
Chris Mason42e70e72008-11-07 18:17:11 -05002533 total_needed += empty_size;
Josef Bacik80eb2342008-10-29 14:49:05 -04002534 block_group = btrfs_lookup_block_group(root->fs_info, search_start);
Yan Zhengd899e052008-10-30 14:25:28 -04002535 if (!block_group)
2536 block_group = btrfs_lookup_first_block_group(root->fs_info,
2537 search_start);
Josef Bacik80eb2342008-10-29 14:49:05 -04002538 space_info = __find_space_info(root->fs_info, data);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002539
Josef Bacik80eb2342008-10-29 14:49:05 -04002540 down_read(&space_info->groups_sem);
2541 while (1) {
2542 struct btrfs_free_space *free_space;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002543 /*
Josef Bacik80eb2342008-10-29 14:49:05 -04002544 * the only way this happens if our hint points to a block
2545 * group thats not of the proper type, while looping this
2546 * should never happen
Josef Bacik0f9dd462008-09-23 13:14:11 -04002547 */
Chris Mason8a1413a2008-11-10 16:13:54 -05002548 if (empty_size)
2549 extra_loop = 1;
2550
Chris Mason42e70e72008-11-07 18:17:11 -05002551 if (!block_group)
2552 goto new_group_no_lock;
2553
Josef Bacikea6a4782008-11-20 12:16:16 -05002554 if (unlikely(!block_group->cached)) {
2555 mutex_lock(&block_group->cache_mutex);
2556 ret = cache_block_group(root, block_group);
2557 mutex_unlock(&block_group->cache_mutex);
2558 if (ret)
2559 break;
2560 }
2561
Josef Bacik25179202008-10-29 14:49:05 -04002562 mutex_lock(&block_group->alloc_mutex);
Josef Bacik80eb2342008-10-29 14:49:05 -04002563 if (unlikely(!block_group_bits(block_group, data)))
Josef Bacik0f9dd462008-09-23 13:14:11 -04002564 goto new_group;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002565
Josef Bacikea6a4782008-11-20 12:16:16 -05002566 if (unlikely(block_group->ro))
Josef Bacik80eb2342008-10-29 14:49:05 -04002567 goto new_group;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002568
Josef Bacik80eb2342008-10-29 14:49:05 -04002569 free_space = btrfs_find_free_space(block_group, search_start,
2570 total_needed);
2571 if (free_space) {
2572 u64 start = block_group->key.objectid;
2573 u64 end = block_group->key.objectid +
Josef Bacik0f9dd462008-09-23 13:14:11 -04002574 block_group->key.offset;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002575
Josef Bacik80eb2342008-10-29 14:49:05 -04002576 search_start = stripe_align(root, free_space->offset);
Chris Mason0b86a832008-03-24 15:01:56 -04002577
Josef Bacik80eb2342008-10-29 14:49:05 -04002578 /* move on to the next group */
2579 if (search_start + num_bytes >= search_end)
2580 goto new_group;
Chris Masone37c9e62007-05-09 20:13:14 -04002581
Josef Bacik80eb2342008-10-29 14:49:05 -04002582 /* move on to the next group */
2583 if (search_start + num_bytes > end)
2584 goto new_group;
2585
Chris Mason43662112008-11-07 09:06:11 -05002586 if (last_wanted && search_start != last_wanted) {
Chris Mason3b7885b2008-11-06 21:48:27 -05002587 total_needed += empty_cluster;
Chris Mason8a1413a2008-11-10 16:13:54 -05002588 empty_size += empty_cluster;
Chris Mason43662112008-11-07 09:06:11 -05002589 last_wanted = 0;
Chris Mason3b7885b2008-11-06 21:48:27 -05002590 /*
2591 * if search_start is still in this block group
2592 * then we just re-search this block group
2593 */
2594 if (search_start >= start &&
2595 search_start < end) {
2596 mutex_unlock(&block_group->alloc_mutex);
2597 continue;
2598 }
2599
2600 /* else we go to the next block group */
2601 goto new_group;
2602 }
2603
Josef Bacik80eb2342008-10-29 14:49:05 -04002604 if (exclude_nr > 0 &&
2605 (search_start + num_bytes > exclude_start &&
2606 search_start < exclude_start + exclude_nr)) {
2607 search_start = exclude_start + exclude_nr;
2608 /*
2609 * if search_start is still in this block group
2610 * then we just re-search this block group
2611 */
2612 if (search_start >= start &&
Josef Bacik25179202008-10-29 14:49:05 -04002613 search_start < end) {
2614 mutex_unlock(&block_group->alloc_mutex);
Chris Mason43662112008-11-07 09:06:11 -05002615 last_wanted = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04002616 continue;
Josef Bacik25179202008-10-29 14:49:05 -04002617 }
Josef Bacik80eb2342008-10-29 14:49:05 -04002618
2619 /* else we go to the next block group */
2620 goto new_group;
2621 }
2622
2623 ins->objectid = search_start;
2624 ins->offset = num_bytes;
Josef Bacik25179202008-10-29 14:49:05 -04002625
2626 btrfs_remove_free_space_lock(block_group, search_start,
2627 num_bytes);
Josef Bacik80eb2342008-10-29 14:49:05 -04002628 /* we are all good, lets return */
Josef Bacik25179202008-10-29 14:49:05 -04002629 mutex_unlock(&block_group->alloc_mutex);
Josef Bacik80eb2342008-10-29 14:49:05 -04002630 break;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002631 }
Josef Bacik80eb2342008-10-29 14:49:05 -04002632new_group:
Chris Mason42e70e72008-11-07 18:17:11 -05002633 mutex_unlock(&block_group->alloc_mutex);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002634 put_block_group(block_group);
2635 block_group = NULL;
Chris Mason42e70e72008-11-07 18:17:11 -05002636new_group_no_lock:
Chris Masonf5a31e12008-11-10 11:47:09 -05002637 /* don't try to compare new allocations against the
2638 * last allocation any more
2639 */
Chris Mason43662112008-11-07 09:06:11 -05002640 last_wanted = 0;
Chris Masonf5a31e12008-11-10 11:47:09 -05002641
Josef Bacik80eb2342008-10-29 14:49:05 -04002642 /*
2643 * Here's how this works.
2644 * loop == 0: we were searching a block group via a hint
2645 * and didn't find anything, so we start at
2646 * the head of the block groups and keep searching
2647 * loop == 1: we're searching through all of the block groups
2648 * if we hit the head again we have searched
2649 * all of the block groups for this space and we
2650 * need to try and allocate, if we cant error out.
2651 * loop == 2: we allocated more space and are looping through
2652 * all of the block groups again.
2653 */
2654 if (loop == 0) {
2655 head = &space_info->block_groups;
2656 cur = head->next;
Josef Bacik80eb2342008-10-29 14:49:05 -04002657 loop++;
2658 } else if (loop == 1 && cur == head) {
Chris Masonf5a31e12008-11-10 11:47:09 -05002659 int keep_going;
Chris Mason42e70e72008-11-07 18:17:11 -05002660
Chris Masonf5a31e12008-11-10 11:47:09 -05002661 /* at this point we give up on the empty_size
2662 * allocations and just try to allocate the min
2663 * space.
2664 *
2665 * The extra_loop field was set if an empty_size
2666 * allocation was attempted above, and if this
2667 * is try we need to try the loop again without
2668 * the additional empty_size.
2669 */
Chris Mason5b7c3fc2008-11-10 07:26:33 -05002670 total_needed -= empty_size;
2671 empty_size = 0;
Chris Masonf5a31e12008-11-10 11:47:09 -05002672 keep_going = extra_loop;
2673 loop++;
Chris Mason42e70e72008-11-07 18:17:11 -05002674
Josef Bacik80eb2342008-10-29 14:49:05 -04002675 if (allowed_chunk_alloc && !chunk_alloc_done) {
2676 up_read(&space_info->groups_sem);
2677 ret = do_chunk_alloc(trans, root, num_bytes +
2678 2 * 1024 * 1024, data, 1);
Josef Bacik80eb2342008-10-29 14:49:05 -04002679 down_read(&space_info->groups_sem);
Chris Mason2ed6d662008-11-13 09:59:33 -05002680 if (ret < 0)
2681 goto loop_check;
Josef Bacik80eb2342008-10-29 14:49:05 -04002682 head = &space_info->block_groups;
Chris Masonf5a31e12008-11-10 11:47:09 -05002683 /*
2684 * we've allocated a new chunk, keep
2685 * trying
2686 */
2687 keep_going = 1;
Josef Bacik80eb2342008-10-29 14:49:05 -04002688 chunk_alloc_done = 1;
2689 } else if (!allowed_chunk_alloc) {
2690 space_info->force_alloc = 1;
Chris Masonf5a31e12008-11-10 11:47:09 -05002691 }
Chris Mason2ed6d662008-11-13 09:59:33 -05002692loop_check:
Chris Masonf5a31e12008-11-10 11:47:09 -05002693 if (keep_going) {
2694 cur = head->next;
2695 extra_loop = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04002696 } else {
2697 break;
2698 }
2699 } else if (cur == head) {
2700 break;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002701 }
Josef Bacik80eb2342008-10-29 14:49:05 -04002702
2703 block_group = list_entry(cur, struct btrfs_block_group_cache,
2704 list);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002705 atomic_inc(&block_group->count);
2706
Josef Bacik80eb2342008-10-29 14:49:05 -04002707 search_start = block_group->key.objectid;
2708 cur = cur->next;
Chris Masone19caa52007-10-15 16:17:44 -04002709 }
Chris Mason0b86a832008-03-24 15:01:56 -04002710
Josef Bacik80eb2342008-10-29 14:49:05 -04002711 /* we found what we needed */
2712 if (ins->objectid) {
2713 if (!(data & BTRFS_BLOCK_GROUP_DATA))
Yan Zhengd2fb3432008-12-11 16:30:39 -05002714 trans->block_group = block_group->key.objectid;
Josef Bacik80eb2342008-10-29 14:49:05 -04002715
2716 if (last_ptr)
2717 *last_ptr = ins->objectid + ins->offset;
2718 ret = 0;
2719 } else if (!ret) {
Chris Masond3977122009-01-05 21:25:51 -05002720 printk(KERN_ERR "btrfs searching for %llu bytes, "
2721 "num_bytes %llu, loop %d, allowed_alloc %d\n",
2722 (unsigned long long)total_needed,
2723 (unsigned long long)num_bytes,
Josef Bacik4ce4cb52008-11-17 21:12:00 -05002724 loop, allowed_chunk_alloc);
Josef Bacik80eb2342008-10-29 14:49:05 -04002725 ret = -ENOSPC;
Chris Masonf2654de2007-06-26 12:20:46 -04002726 }
Yan Zhengd2fb3432008-12-11 16:30:39 -05002727 if (block_group)
2728 put_block_group(block_group);
Chris Mason0b86a832008-03-24 15:01:56 -04002729
Josef Bacik80eb2342008-10-29 14:49:05 -04002730 up_read(&space_info->groups_sem);
Chris Mason0f70abe2007-02-28 16:46:22 -05002731 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05002732}
Chris Masonec44a352008-04-28 15:29:52 -04002733
Josef Bacik0f9dd462008-09-23 13:14:11 -04002734static void dump_space_info(struct btrfs_space_info *info, u64 bytes)
2735{
2736 struct btrfs_block_group_cache *cache;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002737
Chris Masond3977122009-01-05 21:25:51 -05002738 printk(KERN_INFO "space_info has %llu free, is %sfull\n",
2739 (unsigned long long)(info->total_bytes - info->bytes_used -
2740 info->bytes_pinned - info->bytes_reserved),
2741 (info->full) ? "" : "not ");
Josef Bacik6a632092009-02-20 11:00:09 -05002742 printk(KERN_INFO "space_info total=%llu, pinned=%llu, delalloc=%llu,"
2743 " may_use=%llu, used=%llu\n", info->total_bytes,
2744 info->bytes_pinned, info->bytes_delalloc, info->bytes_may_use,
2745 info->bytes_used);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002746
Josef Bacik80eb2342008-10-29 14:49:05 -04002747 down_read(&info->groups_sem);
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002748 list_for_each_entry(cache, &info->block_groups, list) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04002749 spin_lock(&cache->lock);
Chris Masond3977122009-01-05 21:25:51 -05002750 printk(KERN_INFO "block group %llu has %llu bytes, %llu used "
2751 "%llu pinned %llu reserved\n",
2752 (unsigned long long)cache->key.objectid,
2753 (unsigned long long)cache->key.offset,
2754 (unsigned long long)btrfs_block_group_used(&cache->item),
2755 (unsigned long long)cache->pinned,
2756 (unsigned long long)cache->reserved);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002757 btrfs_dump_free_space(cache, bytes);
2758 spin_unlock(&cache->lock);
2759 }
Josef Bacik80eb2342008-10-29 14:49:05 -04002760 up_read(&info->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002761}
Zheng Yane8569812008-09-26 10:05:48 -04002762
Chris Masone6dcd2d2008-07-17 12:53:50 -04002763static int __btrfs_reserve_extent(struct btrfs_trans_handle *trans,
2764 struct btrfs_root *root,
2765 u64 num_bytes, u64 min_alloc_size,
2766 u64 empty_size, u64 hint_byte,
2767 u64 search_end, struct btrfs_key *ins,
2768 u64 data)
Chris Masonfec577f2007-02-26 10:40:21 -05002769{
2770 int ret;
Chris Masonfbdc7622007-05-30 10:22:12 -04002771 u64 search_start = 0;
Chris Mason1261ec42007-03-20 20:35:03 -04002772 struct btrfs_fs_info *info = root->fs_info;
Chris Mason925baed2008-06-25 16:01:30 -04002773
Josef Bacik6a632092009-02-20 11:00:09 -05002774 data = btrfs_get_alloc_profile(root, data);
Chris Mason98d20f62008-04-14 09:46:10 -04002775again:
Chris Mason0ef3e662008-05-24 14:04:53 -04002776 /*
2777 * the only place that sets empty_size is btrfs_realloc_node, which
2778 * is not called recursively on allocations
2779 */
2780 if (empty_size || root->ref_cows) {
Chris Mason593060d2008-03-25 16:50:33 -04002781 if (!(data & BTRFS_BLOCK_GROUP_METADATA)) {
Chris Mason6324fbf2008-03-24 15:01:59 -04002782 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0ef3e662008-05-24 14:04:53 -04002783 2 * 1024 * 1024,
2784 BTRFS_BLOCK_GROUP_METADATA |
2785 (info->metadata_alloc_profile &
2786 info->avail_metadata_alloc_bits), 0);
Chris Mason6324fbf2008-03-24 15:01:59 -04002787 }
2788 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0ef3e662008-05-24 14:04:53 -04002789 num_bytes + 2 * 1024 * 1024, data, 0);
Chris Mason6324fbf2008-03-24 15:01:59 -04002790 }
Chris Mason0b86a832008-03-24 15:01:56 -04002791
Chris Masondb945352007-10-15 16:15:53 -04002792 WARN_ON(num_bytes < root->sectorsize);
2793 ret = find_free_extent(trans, root, num_bytes, empty_size,
2794 search_start, search_end, hint_byte, ins,
Chris Mason26b80032007-08-08 20:17:12 -04002795 trans->alloc_exclude_start,
2796 trans->alloc_exclude_nr, data);
Chris Mason3b951512008-04-17 11:29:12 -04002797
Chris Mason98d20f62008-04-14 09:46:10 -04002798 if (ret == -ENOSPC && num_bytes > min_alloc_size) {
2799 num_bytes = num_bytes >> 1;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002800 num_bytes = num_bytes & ~(root->sectorsize - 1);
Chris Mason98d20f62008-04-14 09:46:10 -04002801 num_bytes = max(num_bytes, min_alloc_size);
Chris Mason0ef3e662008-05-24 14:04:53 -04002802 do_chunk_alloc(trans, root->fs_info->extent_root,
2803 num_bytes, data, 1);
Chris Mason98d20f62008-04-14 09:46:10 -04002804 goto again;
2805 }
Chris Masonec44a352008-04-28 15:29:52 -04002806 if (ret) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04002807 struct btrfs_space_info *sinfo;
2808
2809 sinfo = __find_space_info(root->fs_info, data);
Chris Masond3977122009-01-05 21:25:51 -05002810 printk(KERN_ERR "btrfs allocation failed flags %llu, "
2811 "wanted %llu\n", (unsigned long long)data,
2812 (unsigned long long)num_bytes);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002813 dump_space_info(sinfo, num_bytes);
Chris Mason925baed2008-06-25 16:01:30 -04002814 BUG();
Chris Mason925baed2008-06-25 16:01:30 -04002815 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002816
2817 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002818}
2819
Chris Mason65b51a02008-08-01 15:11:20 -04002820int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len)
2821{
Josef Bacik0f9dd462008-09-23 13:14:11 -04002822 struct btrfs_block_group_cache *cache;
Liu Hui1f3c79a2009-01-05 15:57:51 -05002823 int ret = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002824
Josef Bacik0f9dd462008-09-23 13:14:11 -04002825 cache = btrfs_lookup_block_group(root->fs_info, start);
2826 if (!cache) {
Chris Masond3977122009-01-05 21:25:51 -05002827 printk(KERN_ERR "Unable to find block group for %llu\n",
2828 (unsigned long long)start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002829 return -ENOSPC;
2830 }
Liu Hui1f3c79a2009-01-05 15:57:51 -05002831
2832 ret = btrfs_discard_extent(root, start, len);
2833
Josef Bacik0f9dd462008-09-23 13:14:11 -04002834 btrfs_add_free_space(cache, start, len);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002835 put_block_group(cache);
Zheng Yan1a40e232008-09-26 10:09:34 -04002836 update_reserved_extents(root, start, len, 0);
Liu Hui1f3c79a2009-01-05 15:57:51 -05002837
2838 return ret;
Chris Mason65b51a02008-08-01 15:11:20 -04002839}
2840
Chris Masone6dcd2d2008-07-17 12:53:50 -04002841int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
2842 struct btrfs_root *root,
2843 u64 num_bytes, u64 min_alloc_size,
2844 u64 empty_size, u64 hint_byte,
2845 u64 search_end, struct btrfs_key *ins,
2846 u64 data)
2847{
2848 int ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002849 ret = __btrfs_reserve_extent(trans, root, num_bytes, min_alloc_size,
2850 empty_size, hint_byte, search_end, ins,
2851 data);
Zheng Yane8569812008-09-26 10:05:48 -04002852 update_reserved_extents(root, ins->objectid, ins->offset, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002853 return ret;
2854}
2855
2856static int __btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04002857 struct btrfs_root *root, u64 parent,
Chris Masone6dcd2d2008-07-17 12:53:50 -04002858 u64 root_objectid, u64 ref_generation,
Chris Mason56bec292009-03-13 10:10:06 -04002859 u64 owner, struct btrfs_key *ins,
2860 int ref_mod)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002861{
2862 int ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002863 u64 super_used;
2864 u64 root_used;
2865 u64 num_bytes = ins->offset;
2866 u32 sizes[2];
2867 struct btrfs_fs_info *info = root->fs_info;
2868 struct btrfs_root *extent_root = info->extent_root;
2869 struct btrfs_extent_item *extent_item;
2870 struct btrfs_extent_ref *ref;
2871 struct btrfs_path *path;
2872 struct btrfs_key keys[2];
Chris Masonf2654de2007-06-26 12:20:46 -04002873
Zheng Yan31840ae2008-09-23 13:14:14 -04002874 if (parent == 0)
2875 parent = ins->objectid;
2876
Josef Bacik58176a92007-08-29 15:47:34 -04002877 /* block accounting for super block */
Chris Mason75eff682008-12-15 15:54:40 -05002878 spin_lock(&info->delalloc_lock);
Chris Masondb945352007-10-15 16:15:53 -04002879 super_used = btrfs_super_bytes_used(&info->super_copy);
2880 btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes);
Chris Mason26b80032007-08-08 20:17:12 -04002881
Josef Bacik58176a92007-08-29 15:47:34 -04002882 /* block accounting for root item */
Chris Masondb945352007-10-15 16:15:53 -04002883 root_used = btrfs_root_used(&root->root_item);
2884 btrfs_set_root_used(&root->root_item, root_used + num_bytes);
Yan Zheng34bf63c2008-12-19 10:58:46 -05002885 spin_unlock(&info->delalloc_lock);
Josef Bacik58176a92007-08-29 15:47:34 -04002886
Chris Mason47e4bb92008-02-01 14:51:59 -05002887 memcpy(&keys[0], ins, sizeof(*ins));
Chris Mason47e4bb92008-02-01 14:51:59 -05002888 keys[1].objectid = ins->objectid;
2889 keys[1].type = BTRFS_EXTENT_REF_KEY;
Zheng Yan31840ae2008-09-23 13:14:14 -04002890 keys[1].offset = parent;
Chris Mason47e4bb92008-02-01 14:51:59 -05002891 sizes[0] = sizeof(*extent_item);
2892 sizes[1] = sizeof(*ref);
Chris Mason7bb86312007-12-11 09:25:06 -05002893
2894 path = btrfs_alloc_path();
2895 BUG_ON(!path);
Chris Mason47e4bb92008-02-01 14:51:59 -05002896
2897 ret = btrfs_insert_empty_items(trans, extent_root, path, keys,
2898 sizes, 2);
Chris Masonccd467d2007-06-28 15:57:36 -04002899 BUG_ON(ret);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002900
Chris Mason47e4bb92008-02-01 14:51:59 -05002901 extent_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2902 struct btrfs_extent_item);
Chris Mason56bec292009-03-13 10:10:06 -04002903 btrfs_set_extent_refs(path->nodes[0], extent_item, ref_mod);
Chris Mason47e4bb92008-02-01 14:51:59 -05002904 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
2905 struct btrfs_extent_ref);
2906
2907 btrfs_set_ref_root(path->nodes[0], ref, root_objectid);
2908 btrfs_set_ref_generation(path->nodes[0], ref, ref_generation);
2909 btrfs_set_ref_objectid(path->nodes[0], ref, owner);
Chris Mason56bec292009-03-13 10:10:06 -04002910 btrfs_set_ref_num_refs(path->nodes[0], ref, ref_mod);
Chris Mason47e4bb92008-02-01 14:51:59 -05002911
2912 btrfs_mark_buffer_dirty(path->nodes[0]);
2913
2914 trans->alloc_exclude_start = 0;
2915 trans->alloc_exclude_nr = 0;
Chris Mason7bb86312007-12-11 09:25:06 -05002916 btrfs_free_path(path);
Chris Masonf510cfe2007-10-15 16:14:48 -04002917
Chris Mason925baed2008-06-25 16:01:30 -04002918 if (ret)
2919 goto out;
Chris Mason26b80032007-08-08 20:17:12 -04002920
Chris Masond3977122009-01-05 21:25:51 -05002921 ret = update_block_group(trans, root, ins->objectid,
2922 ins->offset, 1, 0);
Chris Masonf5947062008-02-04 10:10:13 -05002923 if (ret) {
Chris Masond3977122009-01-05 21:25:51 -05002924 printk(KERN_ERR "btrfs update block group failed for %llu "
2925 "%llu\n", (unsigned long long)ins->objectid,
2926 (unsigned long long)ins->offset);
Chris Masonf5947062008-02-04 10:10:13 -05002927 BUG();
2928 }
Chris Mason925baed2008-06-25 16:01:30 -04002929out:
Chris Masone6dcd2d2008-07-17 12:53:50 -04002930 return ret;
2931}
2932
2933int btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04002934 struct btrfs_root *root, u64 parent,
Chris Masone6dcd2d2008-07-17 12:53:50 -04002935 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002936 u64 owner, struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002937{
2938 int ret;
Chris Mason1c2308f2008-09-23 13:14:13 -04002939
2940 if (root_objectid == BTRFS_TREE_LOG_OBJECTID)
2941 return 0;
Chris Mason56bec292009-03-13 10:10:06 -04002942
2943 ret = btrfs_add_delayed_ref(trans, ins->objectid,
2944 ins->offset, parent, root_objectid,
2945 ref_generation, owner,
2946 BTRFS_ADD_DELAYED_EXTENT, 0);
2947 BUG_ON(ret);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002948 return ret;
2949}
Chris Masone02119d2008-09-05 16:13:11 -04002950
2951/*
2952 * this is used by the tree logging recovery code. It records that
2953 * an extent has been allocated and makes sure to clear the free
2954 * space cache bits as well
2955 */
2956int btrfs_alloc_logged_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04002957 struct btrfs_root *root, u64 parent,
Chris Masone02119d2008-09-05 16:13:11 -04002958 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002959 u64 owner, struct btrfs_key *ins)
Chris Masone02119d2008-09-05 16:13:11 -04002960{
2961 int ret;
2962 struct btrfs_block_group_cache *block_group;
2963
Chris Masone02119d2008-09-05 16:13:11 -04002964 block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
Josef Bacikea6a4782008-11-20 12:16:16 -05002965 mutex_lock(&block_group->cache_mutex);
Chris Masone02119d2008-09-05 16:13:11 -04002966 cache_block_group(root, block_group);
Josef Bacikea6a4782008-11-20 12:16:16 -05002967 mutex_unlock(&block_group->cache_mutex);
Chris Masone02119d2008-09-05 16:13:11 -04002968
Josef Bacikea6a4782008-11-20 12:16:16 -05002969 ret = btrfs_remove_free_space(block_group, ins->objectid,
2970 ins->offset);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002971 BUG_ON(ret);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002972 put_block_group(block_group);
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002973 ret = __btrfs_alloc_reserved_extent(trans, root, parent, root_objectid,
Chris Mason56bec292009-03-13 10:10:06 -04002974 ref_generation, owner, ins, 1);
Chris Masone02119d2008-09-05 16:13:11 -04002975 return ret;
2976}
2977
Chris Masone6dcd2d2008-07-17 12:53:50 -04002978/*
2979 * finds a free extent and does all the dirty work required for allocation
2980 * returns the key for the extent through ins, and a tree buffer for
2981 * the first block of the extent through buf.
2982 *
2983 * returns 0 if everything worked, non-zero otherwise.
2984 */
2985int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
2986 struct btrfs_root *root,
Zheng Yan31840ae2008-09-23 13:14:14 -04002987 u64 num_bytes, u64 parent, u64 min_alloc_size,
Chris Masone6dcd2d2008-07-17 12:53:50 -04002988 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002989 u64 owner_objectid, u64 empty_size, u64 hint_byte,
Chris Masone6dcd2d2008-07-17 12:53:50 -04002990 u64 search_end, struct btrfs_key *ins, u64 data)
2991{
2992 int ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002993 ret = __btrfs_reserve_extent(trans, root, num_bytes,
2994 min_alloc_size, empty_size, hint_byte,
2995 search_end, ins, data);
2996 BUG_ON(ret);
Chris Masond00aff02008-09-11 15:54:42 -04002997 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
Chris Mason56bec292009-03-13 10:10:06 -04002998 ret = btrfs_add_delayed_ref(trans, ins->objectid,
2999 ins->offset, parent, root_objectid,
3000 ref_generation, owner_objectid,
3001 BTRFS_ADD_DELAYED_EXTENT, 0);
Chris Masond00aff02008-09-11 15:54:42 -04003002 BUG_ON(ret);
Chris Masond00aff02008-09-11 15:54:42 -04003003 }
Chris Mason56bec292009-03-13 10:10:06 -04003004 update_reserved_extents(root, ins->objectid, ins->offset, 1);
Chris Mason925baed2008-06-25 16:01:30 -04003005 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05003006}
Chris Mason65b51a02008-08-01 15:11:20 -04003007
3008struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
3009 struct btrfs_root *root,
Chris Mason4008c042009-02-12 14:09:45 -05003010 u64 bytenr, u32 blocksize,
3011 int level)
Chris Mason65b51a02008-08-01 15:11:20 -04003012{
3013 struct extent_buffer *buf;
3014
3015 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
3016 if (!buf)
3017 return ERR_PTR(-ENOMEM);
3018 btrfs_set_header_generation(buf, trans->transid);
Chris Mason4008c042009-02-12 14:09:45 -05003019 btrfs_set_buffer_lockdep_class(buf, level);
Chris Mason65b51a02008-08-01 15:11:20 -04003020 btrfs_tree_lock(buf);
3021 clean_tree_block(trans, root, buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05003022
3023 btrfs_set_lock_blocking(buf);
Chris Mason65b51a02008-08-01 15:11:20 -04003024 btrfs_set_buffer_uptodate(buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05003025
Chris Masond0c803c2008-09-11 16:17:57 -04003026 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
3027 set_extent_dirty(&root->dirty_log_pages, buf->start,
Chris Mason65b51a02008-08-01 15:11:20 -04003028 buf->start + buf->len - 1, GFP_NOFS);
Chris Masond0c803c2008-09-11 16:17:57 -04003029 } else {
3030 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
3031 buf->start + buf->len - 1, GFP_NOFS);
3032 }
Chris Mason65b51a02008-08-01 15:11:20 -04003033 trans->blocks_used++;
Chris Masonb4ce94d2009-02-04 09:25:08 -05003034 /* this returns a buffer locked for blocking */
Chris Mason65b51a02008-08-01 15:11:20 -04003035 return buf;
3036}
3037
Chris Masonfec577f2007-02-26 10:40:21 -05003038/*
3039 * helper function to allocate a block for a given tree
3040 * returns the tree buffer or NULL.
3041 */
Chris Mason5f39d392007-10-15 16:14:19 -04003042struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Chris Masondb945352007-10-15 16:15:53 -04003043 struct btrfs_root *root,
Zheng Yan31840ae2008-09-23 13:14:14 -04003044 u32 blocksize, u64 parent,
Chris Mason7bb86312007-12-11 09:25:06 -05003045 u64 root_objectid,
3046 u64 ref_generation,
Chris Mason7bb86312007-12-11 09:25:06 -05003047 int level,
3048 u64 hint,
Chris Mason5f39d392007-10-15 16:14:19 -04003049 u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05003050{
Chris Masone2fa7222007-03-12 16:22:34 -04003051 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -05003052 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04003053 struct extent_buffer *buf;
Chris Masonfec577f2007-02-26 10:40:21 -05003054
Zheng Yan31840ae2008-09-23 13:14:14 -04003055 ret = btrfs_alloc_extent(trans, root, blocksize, parent, blocksize,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003056 root_objectid, ref_generation, level,
Zheng Yan31840ae2008-09-23 13:14:14 -04003057 empty_size, hint, (u64)-1, &ins, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05003058 if (ret) {
Chris Mason54aa1f42007-06-22 14:16:25 -04003059 BUG_ON(ret > 0);
3060 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05003061 }
Chris Mason55c69072008-01-09 15:55:33 -05003062
Chris Mason4008c042009-02-12 14:09:45 -05003063 buf = btrfs_init_new_buffer(trans, root, ins.objectid,
3064 blocksize, level);
Chris Masonfec577f2007-02-26 10:40:21 -05003065 return buf;
3066}
Chris Masona28ec192007-03-06 20:08:01 -05003067
Chris Masone02119d2008-09-05 16:13:11 -04003068int btrfs_drop_leaf_ref(struct btrfs_trans_handle *trans,
3069 struct btrfs_root *root, struct extent_buffer *leaf)
Chris Mason6407bf62007-03-27 06:33:00 -04003070{
Chris Mason7bb86312007-12-11 09:25:06 -05003071 u64 leaf_owner;
3072 u64 leaf_generation;
Chris Masonbd56b302009-02-04 09:27:02 -05003073 struct refsort *sorted;
Chris Mason5f39d392007-10-15 16:14:19 -04003074 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -04003075 struct btrfs_file_extent_item *fi;
3076 int i;
3077 int nritems;
3078 int ret;
Chris Masonbd56b302009-02-04 09:27:02 -05003079 int refi = 0;
3080 int slot;
Chris Mason6407bf62007-03-27 06:33:00 -04003081
Chris Mason5f39d392007-10-15 16:14:19 -04003082 BUG_ON(!btrfs_is_leaf(leaf));
3083 nritems = btrfs_header_nritems(leaf);
Chris Mason7bb86312007-12-11 09:25:06 -05003084 leaf_owner = btrfs_header_owner(leaf);
3085 leaf_generation = btrfs_header_generation(leaf);
3086
Chris Masonbd56b302009-02-04 09:27:02 -05003087 sorted = kmalloc(sizeof(*sorted) * nritems, GFP_NOFS);
3088 /* we do this loop twice. The first time we build a list
3089 * of the extents we have a reference on, then we sort the list
3090 * by bytenr. The second time around we actually do the
3091 * extent freeing.
3092 */
Chris Mason6407bf62007-03-27 06:33:00 -04003093 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04003094 u64 disk_bytenr;
Chris Masone34a5b42008-07-22 12:08:37 -04003095 cond_resched();
Chris Mason5f39d392007-10-15 16:14:19 -04003096
3097 btrfs_item_key_to_cpu(leaf, &key, i);
Chris Masonbd56b302009-02-04 09:27:02 -05003098
3099 /* only extents have references, skip everything else */
Chris Mason5f39d392007-10-15 16:14:19 -04003100 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason6407bf62007-03-27 06:33:00 -04003101 continue;
Chris Masonbd56b302009-02-04 09:27:02 -05003102
Chris Mason6407bf62007-03-27 06:33:00 -04003103 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
Chris Masonbd56b302009-02-04 09:27:02 -05003104
3105 /* inline extents live in the btree, they don't have refs */
Chris Mason5f39d392007-10-15 16:14:19 -04003106 if (btrfs_file_extent_type(leaf, fi) ==
3107 BTRFS_FILE_EXTENT_INLINE)
Chris Mason236454d2007-04-19 13:37:44 -04003108 continue;
Chris Masonbd56b302009-02-04 09:27:02 -05003109
Chris Masondb945352007-10-15 16:15:53 -04003110 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Chris Masonbd56b302009-02-04 09:27:02 -05003111
3112 /* holes don't have refs */
Chris Masondb945352007-10-15 16:15:53 -04003113 if (disk_bytenr == 0)
Chris Mason3a686372007-05-24 13:35:57 -04003114 continue;
Chris Mason4a096752008-07-21 10:29:44 -04003115
Chris Masonbd56b302009-02-04 09:27:02 -05003116 sorted[refi].bytenr = disk_bytenr;
3117 sorted[refi].slot = i;
3118 refi++;
3119 }
3120
3121 if (refi == 0)
3122 goto out;
3123
3124 sort(sorted, refi, sizeof(struct refsort), refsort_cmp, NULL);
3125
3126 for (i = 0; i < refi; i++) {
3127 u64 disk_bytenr;
3128
3129 disk_bytenr = sorted[i].bytenr;
3130 slot = sorted[i].slot;
3131
3132 cond_resched();
3133
3134 btrfs_item_key_to_cpu(leaf, &key, slot);
3135 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
3136 continue;
3137
3138 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
3139
Chris Mason56bec292009-03-13 10:10:06 -04003140 ret = btrfs_free_extent(trans, root, disk_bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -05003141 btrfs_file_extent_disk_num_bytes(leaf, fi),
Zheng Yan31840ae2008-09-23 13:14:14 -04003142 leaf->start, leaf_owner, leaf_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003143 key.objectid, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04003144 BUG_ON(ret);
Chris Mason2dd3e672008-08-04 08:20:15 -04003145
3146 atomic_inc(&root->fs_info->throttle_gen);
3147 wake_up(&root->fs_info->transaction_throttle);
3148 cond_resched();
Chris Mason6407bf62007-03-27 06:33:00 -04003149 }
Chris Masonbd56b302009-02-04 09:27:02 -05003150out:
3151 kfree(sorted);
Chris Mason6407bf62007-03-27 06:33:00 -04003152 return 0;
3153}
3154
Chris Masond3977122009-01-05 21:25:51 -05003155static noinline int cache_drop_leaf_ref(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04003156 struct btrfs_root *root,
3157 struct btrfs_leaf_ref *ref)
Yan Zheng31153d82008-07-28 15:32:19 -04003158{
3159 int i;
3160 int ret;
Chris Masonbd56b302009-02-04 09:27:02 -05003161 struct btrfs_extent_info *info;
3162 struct refsort *sorted;
Yan Zheng31153d82008-07-28 15:32:19 -04003163
Chris Masonbd56b302009-02-04 09:27:02 -05003164 if (ref->nritems == 0)
3165 return 0;
3166
3167 sorted = kmalloc(sizeof(*sorted) * ref->nritems, GFP_NOFS);
Yan Zheng31153d82008-07-28 15:32:19 -04003168 for (i = 0; i < ref->nritems; i++) {
Chris Masonbd56b302009-02-04 09:27:02 -05003169 sorted[i].bytenr = ref->extents[i].bytenr;
3170 sorted[i].slot = i;
3171 }
3172 sort(sorted, ref->nritems, sizeof(struct refsort), refsort_cmp, NULL);
3173
3174 /*
3175 * the items in the ref were sorted when the ref was inserted
3176 * into the ref cache, so this is already in order
3177 */
3178 for (i = 0; i < ref->nritems; i++) {
3179 info = ref->extents + sorted[i].slot;
Chris Mason56bec292009-03-13 10:10:06 -04003180 ret = btrfs_free_extent(trans, root, info->bytenr,
Zheng Yan31840ae2008-09-23 13:14:14 -04003181 info->num_bytes, ref->bytenr,
3182 ref->owner, ref->generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003183 info->objectid, 0);
Chris Mason2dd3e672008-08-04 08:20:15 -04003184
3185 atomic_inc(&root->fs_info->throttle_gen);
3186 wake_up(&root->fs_info->transaction_throttle);
3187 cond_resched();
3188
Yan Zheng31153d82008-07-28 15:32:19 -04003189 BUG_ON(ret);
3190 info++;
3191 }
Yan Zheng31153d82008-07-28 15:32:19 -04003192
Chris Mason806638b2009-02-05 09:08:14 -05003193 kfree(sorted);
Yan Zheng31153d82008-07-28 15:32:19 -04003194 return 0;
3195}
3196
Chris Mason56bec292009-03-13 10:10:06 -04003197static int drop_snap_lookup_refcount(struct btrfs_trans_handle *trans,
3198 struct btrfs_root *root, u64 start,
Chris Masond3977122009-01-05 21:25:51 -05003199 u64 len, u32 *refs)
Chris Mason333db942008-06-25 16:01:30 -04003200{
Chris Mason017e5362008-07-28 15:32:51 -04003201 int ret;
Chris Masonf87f0572008-08-01 11:27:23 -04003202
Chris Mason56bec292009-03-13 10:10:06 -04003203 ret = btrfs_lookup_extent_ref(trans, root, start, len, refs);
Chris Masonf87f0572008-08-01 11:27:23 -04003204 BUG_ON(ret);
3205
Chris Masond3977122009-01-05 21:25:51 -05003206#if 0 /* some debugging code in case we see problems here */
Chris Masonf87f0572008-08-01 11:27:23 -04003207 /* if the refs count is one, it won't get increased again. But
3208 * if the ref count is > 1, someone may be decreasing it at
3209 * the same time we are.
3210 */
3211 if (*refs != 1) {
3212 struct extent_buffer *eb = NULL;
3213 eb = btrfs_find_create_tree_block(root, start, len);
3214 if (eb)
3215 btrfs_tree_lock(eb);
3216
3217 mutex_lock(&root->fs_info->alloc_mutex);
3218 ret = lookup_extent_ref(NULL, root, start, len, refs);
3219 BUG_ON(ret);
3220 mutex_unlock(&root->fs_info->alloc_mutex);
3221
3222 if (eb) {
3223 btrfs_tree_unlock(eb);
3224 free_extent_buffer(eb);
3225 }
3226 if (*refs == 1) {
Chris Masond3977122009-01-05 21:25:51 -05003227 printk(KERN_ERR "btrfs block %llu went down to one "
3228 "during drop_snap\n", (unsigned long long)start);
Chris Masonf87f0572008-08-01 11:27:23 -04003229 }
3230
3231 }
3232#endif
3233
Chris Masone7a84562008-06-25 16:01:31 -04003234 cond_resched();
Chris Mason017e5362008-07-28 15:32:51 -04003235 return ret;
Chris Mason333db942008-06-25 16:01:30 -04003236}
3237
3238/*
Chris Masonbd56b302009-02-04 09:27:02 -05003239 * this is used while deleting old snapshots, and it drops the refs
3240 * on a whole subtree starting from a level 1 node.
3241 *
3242 * The idea is to sort all the leaf pointers, and then drop the
3243 * ref on all the leaves in order. Most of the time the leaves
3244 * will have ref cache entries, so no leaf IOs will be required to
3245 * find the extents they have references on.
3246 *
3247 * For each leaf, any references it has are also dropped in order
3248 *
3249 * This ends up dropping the references in something close to optimal
3250 * order for reading and modifying the extent allocation tree.
3251 */
3252static noinline int drop_level_one_refs(struct btrfs_trans_handle *trans,
3253 struct btrfs_root *root,
3254 struct btrfs_path *path)
3255{
3256 u64 bytenr;
3257 u64 root_owner;
3258 u64 root_gen;
3259 struct extent_buffer *eb = path->nodes[1];
3260 struct extent_buffer *leaf;
3261 struct btrfs_leaf_ref *ref;
3262 struct refsort *sorted = NULL;
3263 int nritems = btrfs_header_nritems(eb);
3264 int ret;
3265 int i;
3266 int refi = 0;
3267 int slot = path->slots[1];
3268 u32 blocksize = btrfs_level_size(root, 0);
3269 u32 refs;
3270
3271 if (nritems == 0)
3272 goto out;
3273
3274 root_owner = btrfs_header_owner(eb);
3275 root_gen = btrfs_header_generation(eb);
3276 sorted = kmalloc(sizeof(*sorted) * nritems, GFP_NOFS);
3277
3278 /*
3279 * step one, sort all the leaf pointers so we don't scribble
3280 * randomly into the extent allocation tree
3281 */
3282 for (i = slot; i < nritems; i++) {
3283 sorted[refi].bytenr = btrfs_node_blockptr(eb, i);
3284 sorted[refi].slot = i;
3285 refi++;
3286 }
3287
3288 /*
3289 * nritems won't be zero, but if we're picking up drop_snapshot
3290 * after a crash, slot might be > 0, so double check things
3291 * just in case.
3292 */
3293 if (refi == 0)
3294 goto out;
3295
3296 sort(sorted, refi, sizeof(struct refsort), refsort_cmp, NULL);
3297
3298 /*
3299 * the first loop frees everything the leaves point to
3300 */
3301 for (i = 0; i < refi; i++) {
3302 u64 ptr_gen;
3303
3304 bytenr = sorted[i].bytenr;
3305
3306 /*
3307 * check the reference count on this leaf. If it is > 1
3308 * we just decrement it below and don't update any
3309 * of the refs the leaf points to.
3310 */
Chris Mason56bec292009-03-13 10:10:06 -04003311 ret = drop_snap_lookup_refcount(trans, root, bytenr,
3312 blocksize, &refs);
Chris Masonbd56b302009-02-04 09:27:02 -05003313 BUG_ON(ret);
3314 if (refs != 1)
3315 continue;
3316
3317 ptr_gen = btrfs_node_ptr_generation(eb, sorted[i].slot);
3318
3319 /*
3320 * the leaf only had one reference, which means the
3321 * only thing pointing to this leaf is the snapshot
3322 * we're deleting. It isn't possible for the reference
3323 * count to increase again later
3324 *
3325 * The reference cache is checked for the leaf,
3326 * and if found we'll be able to drop any refs held by
3327 * the leaf without needing to read it in.
3328 */
3329 ref = btrfs_lookup_leaf_ref(root, bytenr);
3330 if (ref && ref->generation != ptr_gen) {
3331 btrfs_free_leaf_ref(root, ref);
3332 ref = NULL;
3333 }
3334 if (ref) {
3335 ret = cache_drop_leaf_ref(trans, root, ref);
3336 BUG_ON(ret);
3337 btrfs_remove_leaf_ref(root, ref);
3338 btrfs_free_leaf_ref(root, ref);
3339 } else {
3340 /*
3341 * the leaf wasn't in the reference cache, so
3342 * we have to read it.
3343 */
3344 leaf = read_tree_block(root, bytenr, blocksize,
3345 ptr_gen);
3346 ret = btrfs_drop_leaf_ref(trans, root, leaf);
3347 BUG_ON(ret);
3348 free_extent_buffer(leaf);
3349 }
3350 atomic_inc(&root->fs_info->throttle_gen);
3351 wake_up(&root->fs_info->transaction_throttle);
3352 cond_resched();
3353 }
3354
3355 /*
3356 * run through the loop again to free the refs on the leaves.
3357 * This is faster than doing it in the loop above because
3358 * the leaves are likely to be clustered together. We end up
3359 * working in nice chunks on the extent allocation tree.
3360 */
3361 for (i = 0; i < refi; i++) {
3362 bytenr = sorted[i].bytenr;
Chris Mason56bec292009-03-13 10:10:06 -04003363 ret = btrfs_free_extent(trans, root, bytenr,
Chris Masonbd56b302009-02-04 09:27:02 -05003364 blocksize, eb->start,
3365 root_owner, root_gen, 0, 1);
3366 BUG_ON(ret);
3367
3368 atomic_inc(&root->fs_info->throttle_gen);
3369 wake_up(&root->fs_info->transaction_throttle);
3370 cond_resched();
3371 }
3372out:
3373 kfree(sorted);
3374
3375 /*
3376 * update the path to show we've processed the entire level 1
3377 * node. This will get saved into the root's drop_snapshot_progress
3378 * field so these drops are not repeated again if this transaction
3379 * commits.
3380 */
3381 path->slots[1] = nritems;
3382 return 0;
3383}
3384
3385/*
Chris Mason9aca1d52007-03-13 11:09:37 -04003386 * helper function for drop_snapshot, this walks down the tree dropping ref
3387 * counts as it goes.
3388 */
Chris Masond3977122009-01-05 21:25:51 -05003389static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05003390 struct btrfs_root *root,
3391 struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05003392{
Chris Mason7bb86312007-12-11 09:25:06 -05003393 u64 root_owner;
3394 u64 root_gen;
3395 u64 bytenr;
Chris Masonca7a79a2008-05-12 12:59:19 -04003396 u64 ptr_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04003397 struct extent_buffer *next;
3398 struct extent_buffer *cur;
Chris Mason7bb86312007-12-11 09:25:06 -05003399 struct extent_buffer *parent;
Chris Masondb945352007-10-15 16:15:53 -04003400 u32 blocksize;
Chris Mason20524f02007-03-10 06:35:47 -05003401 int ret;
3402 u32 refs;
3403
Chris Mason5caf2a02007-04-02 11:20:42 -04003404 WARN_ON(*level < 0);
3405 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason56bec292009-03-13 10:10:06 -04003406 ret = drop_snap_lookup_refcount(trans, root, path->nodes[*level]->start,
Chris Masondb945352007-10-15 16:15:53 -04003407 path->nodes[*level]->len, &refs);
Chris Mason20524f02007-03-10 06:35:47 -05003408 BUG_ON(ret);
3409 if (refs > 1)
3410 goto out;
Chris Masone0115992007-06-19 16:23:05 -04003411
Chris Mason9aca1d52007-03-13 11:09:37 -04003412 /*
3413 * walk down to the last node level and free all the leaves
3414 */
Chris Masond3977122009-01-05 21:25:51 -05003415 while (*level >= 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -04003416 WARN_ON(*level < 0);
3417 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason20524f02007-03-10 06:35:47 -05003418 cur = path->nodes[*level];
Chris Masone0115992007-06-19 16:23:05 -04003419
Chris Mason5f39d392007-10-15 16:14:19 -04003420 if (btrfs_header_level(cur) != *level)
Chris Mason2c90e5d2007-04-02 10:50:19 -04003421 WARN_ON(1);
Chris Masone0115992007-06-19 16:23:05 -04003422
Chris Mason7518a232007-03-12 12:01:18 -04003423 if (path->slots[*level] >=
Chris Mason5f39d392007-10-15 16:14:19 -04003424 btrfs_header_nritems(cur))
Chris Mason20524f02007-03-10 06:35:47 -05003425 break;
Chris Masonbd56b302009-02-04 09:27:02 -05003426
3427 /* the new code goes down to level 1 and does all the
3428 * leaves pointed to that node in bulk. So, this check
3429 * for level 0 will always be false.
3430 *
3431 * But, the disk format allows the drop_snapshot_progress
3432 * field in the root to leave things in a state where
3433 * a leaf will need cleaning up here. If someone crashes
3434 * with the old code and then boots with the new code,
3435 * we might find a leaf here.
3436 */
Chris Mason6407bf62007-03-27 06:33:00 -04003437 if (*level == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003438 ret = btrfs_drop_leaf_ref(trans, root, cur);
Chris Mason6407bf62007-03-27 06:33:00 -04003439 BUG_ON(ret);
3440 break;
3441 }
Chris Masonbd56b302009-02-04 09:27:02 -05003442
3443 /*
3444 * once we get to level one, process the whole node
3445 * at once, including everything below it.
3446 */
3447 if (*level == 1) {
3448 ret = drop_level_one_refs(trans, root, path);
3449 BUG_ON(ret);
3450 break;
3451 }
3452
Chris Masondb945352007-10-15 16:15:53 -04003453 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
Chris Masonca7a79a2008-05-12 12:59:19 -04003454 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
Chris Masondb945352007-10-15 16:15:53 -04003455 blocksize = btrfs_level_size(root, *level - 1);
Chris Mason925baed2008-06-25 16:01:30 -04003456
Chris Mason56bec292009-03-13 10:10:06 -04003457 ret = drop_snap_lookup_refcount(trans, root, bytenr,
3458 blocksize, &refs);
Chris Mason6407bf62007-03-27 06:33:00 -04003459 BUG_ON(ret);
Chris Masonbd56b302009-02-04 09:27:02 -05003460
3461 /*
3462 * if there is more than one reference, we don't need
3463 * to read that node to drop any references it has. We
3464 * just drop the ref we hold on that node and move on to the
3465 * next slot in this level.
3466 */
Chris Mason6407bf62007-03-27 06:33:00 -04003467 if (refs != 1) {
Chris Mason7bb86312007-12-11 09:25:06 -05003468 parent = path->nodes[*level];
3469 root_owner = btrfs_header_owner(parent);
3470 root_gen = btrfs_header_generation(parent);
Chris Mason20524f02007-03-10 06:35:47 -05003471 path->slots[*level]++;
Chris Masonf87f0572008-08-01 11:27:23 -04003472
Chris Mason56bec292009-03-13 10:10:06 -04003473 ret = btrfs_free_extent(trans, root, bytenr,
Zheng Yan31840ae2008-09-23 13:14:14 -04003474 blocksize, parent->start,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003475 root_owner, root_gen,
3476 *level - 1, 1);
Chris Mason20524f02007-03-10 06:35:47 -05003477 BUG_ON(ret);
Chris Mason18e35e02008-08-01 13:11:41 -04003478
3479 atomic_inc(&root->fs_info->throttle_gen);
3480 wake_up(&root->fs_info->transaction_throttle);
Chris Mason2dd3e672008-08-04 08:20:15 -04003481 cond_resched();
Chris Mason18e35e02008-08-01 13:11:41 -04003482
Chris Mason20524f02007-03-10 06:35:47 -05003483 continue;
3484 }
Chris Mason333db942008-06-25 16:01:30 -04003485
Chris Masonbd56b302009-02-04 09:27:02 -05003486 /*
3487 * we need to keep freeing things in the next level down.
3488 * read the block and loop around to process it
3489 */
3490 next = read_tree_block(root, bytenr, blocksize, ptr_gen);
Chris Mason5caf2a02007-04-02 11:20:42 -04003491 WARN_ON(*level <= 0);
Chris Mason83e15a22007-03-12 09:03:27 -04003492 if (path->nodes[*level-1])
Chris Mason5f39d392007-10-15 16:14:19 -04003493 free_extent_buffer(path->nodes[*level-1]);
Chris Mason20524f02007-03-10 06:35:47 -05003494 path->nodes[*level-1] = next;
Chris Mason5f39d392007-10-15 16:14:19 -04003495 *level = btrfs_header_level(next);
Chris Mason20524f02007-03-10 06:35:47 -05003496 path->slots[*level] = 0;
Chris Mason2dd3e672008-08-04 08:20:15 -04003497 cond_resched();
Chris Mason20524f02007-03-10 06:35:47 -05003498 }
3499out:
Chris Mason5caf2a02007-04-02 11:20:42 -04003500 WARN_ON(*level < 0);
3501 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason7bb86312007-12-11 09:25:06 -05003502
3503 if (path->nodes[*level] == root->node) {
Chris Mason7bb86312007-12-11 09:25:06 -05003504 parent = path->nodes[*level];
Yan Zheng31153d82008-07-28 15:32:19 -04003505 bytenr = path->nodes[*level]->start;
Chris Mason7bb86312007-12-11 09:25:06 -05003506 } else {
3507 parent = path->nodes[*level + 1];
Yan Zheng31153d82008-07-28 15:32:19 -04003508 bytenr = btrfs_node_blockptr(parent, path->slots[*level + 1]);
Chris Mason7bb86312007-12-11 09:25:06 -05003509 }
3510
Yan Zheng31153d82008-07-28 15:32:19 -04003511 blocksize = btrfs_level_size(root, *level);
3512 root_owner = btrfs_header_owner(parent);
Chris Mason7bb86312007-12-11 09:25:06 -05003513 root_gen = btrfs_header_generation(parent);
Yan Zheng31153d82008-07-28 15:32:19 -04003514
Chris Masonbd56b302009-02-04 09:27:02 -05003515 /*
3516 * cleanup and free the reference on the last node
3517 * we processed
3518 */
Chris Mason56bec292009-03-13 10:10:06 -04003519 ret = btrfs_free_extent(trans, root, bytenr, blocksize,
Zheng Yan31840ae2008-09-23 13:14:14 -04003520 parent->start, root_owner, root_gen,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003521 *level, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003522 free_extent_buffer(path->nodes[*level]);
Chris Mason20524f02007-03-10 06:35:47 -05003523 path->nodes[*level] = NULL;
Chris Masonbd56b302009-02-04 09:27:02 -05003524
Chris Mason20524f02007-03-10 06:35:47 -05003525 *level += 1;
3526 BUG_ON(ret);
Chris Masonf87f0572008-08-01 11:27:23 -04003527
Chris Masone7a84562008-06-25 16:01:31 -04003528 cond_resched();
Chris Mason20524f02007-03-10 06:35:47 -05003529 return 0;
3530}
3531
Chris Mason9aca1d52007-03-13 11:09:37 -04003532/*
Yan Zhengf82d02d2008-10-29 14:49:05 -04003533 * helper function for drop_subtree, this function is similar to
3534 * walk_down_tree. The main difference is that it checks reference
3535 * counts while tree blocks are locked.
3536 */
Chris Masond3977122009-01-05 21:25:51 -05003537static noinline int walk_down_subtree(struct btrfs_trans_handle *trans,
Yan Zhengf82d02d2008-10-29 14:49:05 -04003538 struct btrfs_root *root,
3539 struct btrfs_path *path, int *level)
3540{
3541 struct extent_buffer *next;
3542 struct extent_buffer *cur;
3543 struct extent_buffer *parent;
3544 u64 bytenr;
3545 u64 ptr_gen;
3546 u32 blocksize;
3547 u32 refs;
3548 int ret;
3549
3550 cur = path->nodes[*level];
3551 ret = btrfs_lookup_extent_ref(trans, root, cur->start, cur->len,
3552 &refs);
3553 BUG_ON(ret);
3554 if (refs > 1)
3555 goto out;
3556
3557 while (*level >= 0) {
3558 cur = path->nodes[*level];
3559 if (*level == 0) {
3560 ret = btrfs_drop_leaf_ref(trans, root, cur);
3561 BUG_ON(ret);
3562 clean_tree_block(trans, root, cur);
3563 break;
3564 }
3565 if (path->slots[*level] >= btrfs_header_nritems(cur)) {
3566 clean_tree_block(trans, root, cur);
3567 break;
3568 }
3569
3570 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
3571 blocksize = btrfs_level_size(root, *level - 1);
3572 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
3573
3574 next = read_tree_block(root, bytenr, blocksize, ptr_gen);
3575 btrfs_tree_lock(next);
Chris Masonb4ce94d2009-02-04 09:25:08 -05003576 btrfs_set_lock_blocking(next);
Yan Zhengf82d02d2008-10-29 14:49:05 -04003577
3578 ret = btrfs_lookup_extent_ref(trans, root, bytenr, blocksize,
3579 &refs);
3580 BUG_ON(ret);
3581 if (refs > 1) {
3582 parent = path->nodes[*level];
3583 ret = btrfs_free_extent(trans, root, bytenr,
3584 blocksize, parent->start,
3585 btrfs_header_owner(parent),
3586 btrfs_header_generation(parent),
3587 *level - 1, 1);
3588 BUG_ON(ret);
3589 path->slots[*level]++;
3590 btrfs_tree_unlock(next);
3591 free_extent_buffer(next);
3592 continue;
3593 }
3594
3595 *level = btrfs_header_level(next);
3596 path->nodes[*level] = next;
3597 path->slots[*level] = 0;
3598 path->locks[*level] = 1;
3599 cond_resched();
3600 }
3601out:
3602 parent = path->nodes[*level + 1];
3603 bytenr = path->nodes[*level]->start;
3604 blocksize = path->nodes[*level]->len;
3605
3606 ret = btrfs_free_extent(trans, root, bytenr, blocksize,
3607 parent->start, btrfs_header_owner(parent),
3608 btrfs_header_generation(parent), *level, 1);
3609 BUG_ON(ret);
3610
3611 if (path->locks[*level]) {
3612 btrfs_tree_unlock(path->nodes[*level]);
3613 path->locks[*level] = 0;
3614 }
3615 free_extent_buffer(path->nodes[*level]);
3616 path->nodes[*level] = NULL;
3617 *level += 1;
3618 cond_resched();
3619 return 0;
3620}
3621
3622/*
Chris Mason9aca1d52007-03-13 11:09:37 -04003623 * helper for dropping snapshots. This walks back up the tree in the path
3624 * to find the first node higher up where we haven't yet gone through
3625 * all the slots
3626 */
Chris Masond3977122009-01-05 21:25:51 -05003627static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05003628 struct btrfs_root *root,
Yan Zhengf82d02d2008-10-29 14:49:05 -04003629 struct btrfs_path *path,
3630 int *level, int max_level)
Chris Mason20524f02007-03-10 06:35:47 -05003631{
Chris Mason7bb86312007-12-11 09:25:06 -05003632 u64 root_owner;
3633 u64 root_gen;
3634 struct btrfs_root_item *root_item = &root->root_item;
Chris Mason20524f02007-03-10 06:35:47 -05003635 int i;
3636 int slot;
3637 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04003638
Yan Zhengf82d02d2008-10-29 14:49:05 -04003639 for (i = *level; i < max_level && path->nodes[i]; i++) {
Chris Mason20524f02007-03-10 06:35:47 -05003640 slot = path->slots[i];
Chris Mason5f39d392007-10-15 16:14:19 -04003641 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
3642 struct extent_buffer *node;
3643 struct btrfs_disk_key disk_key;
Chris Masonbd56b302009-02-04 09:27:02 -05003644
3645 /*
3646 * there is more work to do in this level.
3647 * Update the drop_progress marker to reflect
3648 * the work we've done so far, and then bump
3649 * the slot number
3650 */
Chris Mason5f39d392007-10-15 16:14:19 -04003651 node = path->nodes[i];
Chris Mason20524f02007-03-10 06:35:47 -05003652 path->slots[i]++;
3653 *level = i;
Chris Mason9f3a7422007-08-07 15:52:19 -04003654 WARN_ON(*level == 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003655 btrfs_node_key(node, &disk_key, path->slots[i]);
Chris Mason9f3a7422007-08-07 15:52:19 -04003656 memcpy(&root_item->drop_progress,
Chris Mason5f39d392007-10-15 16:14:19 -04003657 &disk_key, sizeof(disk_key));
Chris Mason9f3a7422007-08-07 15:52:19 -04003658 root_item->drop_level = i;
Chris Mason20524f02007-03-10 06:35:47 -05003659 return 0;
3660 } else {
Zheng Yan31840ae2008-09-23 13:14:14 -04003661 struct extent_buffer *parent;
Chris Masonbd56b302009-02-04 09:27:02 -05003662
3663 /*
3664 * this whole node is done, free our reference
3665 * on it and go up one level
3666 */
Zheng Yan31840ae2008-09-23 13:14:14 -04003667 if (path->nodes[*level] == root->node)
3668 parent = path->nodes[*level];
3669 else
3670 parent = path->nodes[*level + 1];
3671
3672 root_owner = btrfs_header_owner(parent);
3673 root_gen = btrfs_header_generation(parent);
Yan Zhengf82d02d2008-10-29 14:49:05 -04003674
3675 clean_tree_block(trans, root, path->nodes[*level]);
Chris Masone089f052007-03-16 16:20:31 -04003676 ret = btrfs_free_extent(trans, root,
Chris Masondb945352007-10-15 16:15:53 -04003677 path->nodes[*level]->start,
Chris Mason7bb86312007-12-11 09:25:06 -05003678 path->nodes[*level]->len,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003679 parent->start, root_owner,
3680 root_gen, *level, 1);
Chris Mason6407bf62007-03-27 06:33:00 -04003681 BUG_ON(ret);
Yan Zhengf82d02d2008-10-29 14:49:05 -04003682 if (path->locks[*level]) {
3683 btrfs_tree_unlock(path->nodes[*level]);
3684 path->locks[*level] = 0;
3685 }
Chris Mason5f39d392007-10-15 16:14:19 -04003686 free_extent_buffer(path->nodes[*level]);
Chris Mason83e15a22007-03-12 09:03:27 -04003687 path->nodes[*level] = NULL;
Chris Mason20524f02007-03-10 06:35:47 -05003688 *level = i + 1;
Chris Mason20524f02007-03-10 06:35:47 -05003689 }
3690 }
3691 return 1;
3692}
3693
Chris Mason9aca1d52007-03-13 11:09:37 -04003694/*
3695 * drop the reference count on the tree rooted at 'snap'. This traverses
3696 * the tree freeing any blocks that have a ref count of zero after being
3697 * decremented.
3698 */
Chris Masone089f052007-03-16 16:20:31 -04003699int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason9f3a7422007-08-07 15:52:19 -04003700 *root)
Chris Mason20524f02007-03-10 06:35:47 -05003701{
Chris Mason3768f362007-03-13 16:47:54 -04003702 int ret = 0;
Chris Mason9aca1d52007-03-13 11:09:37 -04003703 int wret;
Chris Mason20524f02007-03-10 06:35:47 -05003704 int level;
Chris Mason5caf2a02007-04-02 11:20:42 -04003705 struct btrfs_path *path;
Chris Mason20524f02007-03-10 06:35:47 -05003706 int i;
3707 int orig_level;
Chris Mason9f3a7422007-08-07 15:52:19 -04003708 struct btrfs_root_item *root_item = &root->root_item;
Chris Mason20524f02007-03-10 06:35:47 -05003709
Chris Masona2135012008-06-25 16:01:30 -04003710 WARN_ON(!mutex_is_locked(&root->fs_info->drop_mutex));
Chris Mason5caf2a02007-04-02 11:20:42 -04003711 path = btrfs_alloc_path();
3712 BUG_ON(!path);
Chris Mason20524f02007-03-10 06:35:47 -05003713
Chris Mason5f39d392007-10-15 16:14:19 -04003714 level = btrfs_header_level(root->node);
Chris Mason20524f02007-03-10 06:35:47 -05003715 orig_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04003716 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
3717 path->nodes[level] = root->node;
Chris Masonf510cfe2007-10-15 16:14:48 -04003718 extent_buffer_get(root->node);
Chris Mason9f3a7422007-08-07 15:52:19 -04003719 path->slots[level] = 0;
3720 } else {
3721 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04003722 struct btrfs_disk_key found_key;
3723 struct extent_buffer *node;
Chris Mason6702ed42007-08-07 16:15:09 -04003724
Chris Mason9f3a7422007-08-07 15:52:19 -04003725 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Chris Mason6702ed42007-08-07 16:15:09 -04003726 level = root_item->drop_level;
3727 path->lowest_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04003728 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason6702ed42007-08-07 16:15:09 -04003729 if (wret < 0) {
Chris Mason9f3a7422007-08-07 15:52:19 -04003730 ret = wret;
3731 goto out;
3732 }
Chris Mason5f39d392007-10-15 16:14:19 -04003733 node = path->nodes[level];
3734 btrfs_node_key(node, &found_key, path->slots[level]);
3735 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
3736 sizeof(found_key)));
Chris Mason7d9eb122008-07-08 14:19:17 -04003737 /*
3738 * unlock our path, this is safe because only this
3739 * function is allowed to delete this snapshot
3740 */
Chris Mason925baed2008-06-25 16:01:30 -04003741 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
3742 if (path->nodes[i] && path->locks[i]) {
3743 path->locks[i] = 0;
3744 btrfs_tree_unlock(path->nodes[i]);
3745 }
3746 }
Chris Mason9f3a7422007-08-07 15:52:19 -04003747 }
Chris Masond3977122009-01-05 21:25:51 -05003748 while (1) {
Chris Mason5caf2a02007-04-02 11:20:42 -04003749 wret = walk_down_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04003750 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05003751 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04003752 if (wret < 0)
3753 ret = wret;
3754
Yan Zhengf82d02d2008-10-29 14:49:05 -04003755 wret = walk_up_tree(trans, root, path, &level,
3756 BTRFS_MAX_LEVEL);
Chris Mason9aca1d52007-03-13 11:09:37 -04003757 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05003758 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04003759 if (wret < 0)
3760 ret = wret;
Chris Masone7a84562008-06-25 16:01:31 -04003761 if (trans->transaction->in_commit) {
3762 ret = -EAGAIN;
3763 break;
3764 }
Chris Mason18e35e02008-08-01 13:11:41 -04003765 atomic_inc(&root->fs_info->throttle_gen);
Chris Mason017e5362008-07-28 15:32:51 -04003766 wake_up(&root->fs_info->transaction_throttle);
Chris Mason20524f02007-03-10 06:35:47 -05003767 }
Chris Mason83e15a22007-03-12 09:03:27 -04003768 for (i = 0; i <= orig_level; i++) {
Chris Mason5caf2a02007-04-02 11:20:42 -04003769 if (path->nodes[i]) {
Chris Mason5f39d392007-10-15 16:14:19 -04003770 free_extent_buffer(path->nodes[i]);
Chris Mason0f827312007-10-15 16:18:56 -04003771 path->nodes[i] = NULL;
Chris Mason83e15a22007-03-12 09:03:27 -04003772 }
Chris Mason20524f02007-03-10 06:35:47 -05003773 }
Chris Mason9f3a7422007-08-07 15:52:19 -04003774out:
Chris Mason5caf2a02007-04-02 11:20:42 -04003775 btrfs_free_path(path);
Chris Mason9aca1d52007-03-13 11:09:37 -04003776 return ret;
Chris Mason20524f02007-03-10 06:35:47 -05003777}
Chris Mason9078a3e2007-04-26 16:46:15 -04003778
Yan Zhengf82d02d2008-10-29 14:49:05 -04003779int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
3780 struct btrfs_root *root,
3781 struct extent_buffer *node,
3782 struct extent_buffer *parent)
3783{
3784 struct btrfs_path *path;
3785 int level;
3786 int parent_level;
3787 int ret = 0;
3788 int wret;
3789
3790 path = btrfs_alloc_path();
3791 BUG_ON(!path);
3792
Chris Masonb9447ef2009-03-09 11:45:38 -04003793 btrfs_assert_tree_locked(parent);
Yan Zhengf82d02d2008-10-29 14:49:05 -04003794 parent_level = btrfs_header_level(parent);
3795 extent_buffer_get(parent);
3796 path->nodes[parent_level] = parent;
3797 path->slots[parent_level] = btrfs_header_nritems(parent);
3798
Chris Masonb9447ef2009-03-09 11:45:38 -04003799 btrfs_assert_tree_locked(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04003800 level = btrfs_header_level(node);
3801 extent_buffer_get(node);
3802 path->nodes[level] = node;
3803 path->slots[level] = 0;
3804
3805 while (1) {
3806 wret = walk_down_subtree(trans, root, path, &level);
3807 if (wret < 0)
3808 ret = wret;
3809 if (wret != 0)
3810 break;
3811
3812 wret = walk_up_tree(trans, root, path, &level, parent_level);
3813 if (wret < 0)
3814 ret = wret;
3815 if (wret != 0)
3816 break;
3817 }
3818
3819 btrfs_free_path(path);
3820 return ret;
3821}
3822
Chris Mason8e7bf942008-04-28 09:02:36 -04003823static unsigned long calc_ra(unsigned long start, unsigned long last,
3824 unsigned long nr)
3825{
3826 return min(last, start + nr - 1);
3827}
3828
Chris Masond3977122009-01-05 21:25:51 -05003829static noinline int relocate_inode_pages(struct inode *inode, u64 start,
Chris Mason98ed5172008-01-03 10:01:48 -05003830 u64 len)
Chris Masonedbd8d42007-12-21 16:27:24 -05003831{
3832 u64 page_start;
3833 u64 page_end;
Zheng Yan1a40e232008-09-26 10:09:34 -04003834 unsigned long first_index;
Chris Masonedbd8d42007-12-21 16:27:24 -05003835 unsigned long last_index;
Chris Masonedbd8d42007-12-21 16:27:24 -05003836 unsigned long i;
3837 struct page *page;
Chris Masond1310b22008-01-24 16:13:08 -05003838 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason4313b392008-01-03 09:08:48 -05003839 struct file_ra_state *ra;
Chris Mason3eaa2882008-07-24 11:57:52 -04003840 struct btrfs_ordered_extent *ordered;
Zheng Yan1a40e232008-09-26 10:09:34 -04003841 unsigned int total_read = 0;
3842 unsigned int total_dirty = 0;
3843 int ret = 0;
Chris Mason4313b392008-01-03 09:08:48 -05003844
3845 ra = kzalloc(sizeof(*ra), GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05003846
3847 mutex_lock(&inode->i_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04003848 first_index = start >> PAGE_CACHE_SHIFT;
Chris Masonedbd8d42007-12-21 16:27:24 -05003849 last_index = (start + len - 1) >> PAGE_CACHE_SHIFT;
3850
Zheng Yan1a40e232008-09-26 10:09:34 -04003851 /* make sure the dirty trick played by the caller work */
3852 ret = invalidate_inode_pages2_range(inode->i_mapping,
3853 first_index, last_index);
3854 if (ret)
3855 goto out_unlock;
Chris Mason8e7bf942008-04-28 09:02:36 -04003856
Chris Mason4313b392008-01-03 09:08:48 -05003857 file_ra_state_init(ra, inode->i_mapping);
Chris Masonedbd8d42007-12-21 16:27:24 -05003858
Zheng Yan1a40e232008-09-26 10:09:34 -04003859 for (i = first_index ; i <= last_index; i++) {
3860 if (total_read % ra->ra_pages == 0) {
Chris Mason8e7bf942008-04-28 09:02:36 -04003861 btrfs_force_ra(inode->i_mapping, ra, NULL, i,
Zheng Yan1a40e232008-09-26 10:09:34 -04003862 calc_ra(i, last_index, ra->ra_pages));
Chris Mason8e7bf942008-04-28 09:02:36 -04003863 }
3864 total_read++;
Chris Mason3eaa2882008-07-24 11:57:52 -04003865again:
3866 if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode))
Zheng Yan1a40e232008-09-26 10:09:34 -04003867 BUG_ON(1);
Chris Masonedbd8d42007-12-21 16:27:24 -05003868 page = grab_cache_page(inode->i_mapping, i);
Chris Masona061fc82008-05-07 11:43:44 -04003869 if (!page) {
Zheng Yan1a40e232008-09-26 10:09:34 -04003870 ret = -ENOMEM;
Chris Masonedbd8d42007-12-21 16:27:24 -05003871 goto out_unlock;
Chris Masona061fc82008-05-07 11:43:44 -04003872 }
Chris Masonedbd8d42007-12-21 16:27:24 -05003873 if (!PageUptodate(page)) {
3874 btrfs_readpage(NULL, page);
3875 lock_page(page);
3876 if (!PageUptodate(page)) {
3877 unlock_page(page);
3878 page_cache_release(page);
Zheng Yan1a40e232008-09-26 10:09:34 -04003879 ret = -EIO;
Chris Masonedbd8d42007-12-21 16:27:24 -05003880 goto out_unlock;
3881 }
3882 }
Chris Masonec44a352008-04-28 15:29:52 -04003883 wait_on_page_writeback(page);
Chris Mason3eaa2882008-07-24 11:57:52 -04003884
Chris Masonedbd8d42007-12-21 16:27:24 -05003885 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
3886 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05003887 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05003888
Chris Mason3eaa2882008-07-24 11:57:52 -04003889 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3890 if (ordered) {
3891 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
3892 unlock_page(page);
3893 page_cache_release(page);
3894 btrfs_start_ordered_extent(inode, ordered, 1);
3895 btrfs_put_ordered_extent(ordered);
3896 goto again;
3897 }
3898 set_page_extent_mapped(page);
3899
Zheng Yan1a40e232008-09-26 10:09:34 -04003900 if (i == first_index)
3901 set_extent_bits(io_tree, page_start, page_end,
3902 EXTENT_BOUNDARY, GFP_NOFS);
Yan Zheng1f80e4d2008-12-19 10:59:04 -05003903 btrfs_set_extent_delalloc(inode, page_start, page_end);
Zheng Yan1a40e232008-09-26 10:09:34 -04003904
Chris Masona061fc82008-05-07 11:43:44 -04003905 set_page_dirty(page);
Zheng Yan1a40e232008-09-26 10:09:34 -04003906 total_dirty++;
Chris Masonedbd8d42007-12-21 16:27:24 -05003907
Chris Masond1310b22008-01-24 16:13:08 -05003908 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05003909 unlock_page(page);
3910 page_cache_release(page);
3911 }
3912
3913out_unlock:
Chris Masonec44a352008-04-28 15:29:52 -04003914 kfree(ra);
Chris Masonedbd8d42007-12-21 16:27:24 -05003915 mutex_unlock(&inode->i_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04003916 balance_dirty_pages_ratelimited_nr(inode->i_mapping, total_dirty);
Chris Masonbf4ef672008-05-08 13:26:18 -04003917 return ret;
3918}
3919
Chris Masond3977122009-01-05 21:25:51 -05003920static noinline int relocate_data_extent(struct inode *reloc_inode,
Zheng Yan1a40e232008-09-26 10:09:34 -04003921 struct btrfs_key *extent_key,
3922 u64 offset)
Chris Masonedbd8d42007-12-21 16:27:24 -05003923{
Zheng Yan1a40e232008-09-26 10:09:34 -04003924 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
3925 struct extent_map_tree *em_tree = &BTRFS_I(reloc_inode)->extent_tree;
3926 struct extent_map *em;
Yan Zheng66435582008-10-30 14:19:50 -04003927 u64 start = extent_key->objectid - offset;
3928 u64 end = start + extent_key->offset - 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04003929
3930 em = alloc_extent_map(GFP_NOFS);
3931 BUG_ON(!em || IS_ERR(em));
3932
Yan Zheng66435582008-10-30 14:19:50 -04003933 em->start = start;
Zheng Yan1a40e232008-09-26 10:09:34 -04003934 em->len = extent_key->offset;
Chris Masonc8b97812008-10-29 14:49:59 -04003935 em->block_len = extent_key->offset;
Zheng Yan1a40e232008-09-26 10:09:34 -04003936 em->block_start = extent_key->objectid;
3937 em->bdev = root->fs_info->fs_devices->latest_bdev;
3938 set_bit(EXTENT_FLAG_PINNED, &em->flags);
3939
3940 /* setup extent map to cheat btrfs_readpage */
Yan Zheng66435582008-10-30 14:19:50 -04003941 lock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04003942 while (1) {
3943 int ret;
3944 spin_lock(&em_tree->lock);
3945 ret = add_extent_mapping(em_tree, em);
3946 spin_unlock(&em_tree->lock);
3947 if (ret != -EEXIST) {
3948 free_extent_map(em);
3949 break;
3950 }
Yan Zheng66435582008-10-30 14:19:50 -04003951 btrfs_drop_extent_cache(reloc_inode, start, end, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04003952 }
Yan Zheng66435582008-10-30 14:19:50 -04003953 unlock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04003954
Yan Zheng66435582008-10-30 14:19:50 -04003955 return relocate_inode_pages(reloc_inode, start, extent_key->offset);
Zheng Yan1a40e232008-09-26 10:09:34 -04003956}
3957
3958struct btrfs_ref_path {
3959 u64 extent_start;
3960 u64 nodes[BTRFS_MAX_LEVEL];
3961 u64 root_objectid;
3962 u64 root_generation;
3963 u64 owner_objectid;
Zheng Yan1a40e232008-09-26 10:09:34 -04003964 u32 num_refs;
3965 int lowest_level;
3966 int current_level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04003967 int shared_level;
3968
3969 struct btrfs_key node_keys[BTRFS_MAX_LEVEL];
3970 u64 new_nodes[BTRFS_MAX_LEVEL];
Zheng Yan1a40e232008-09-26 10:09:34 -04003971};
3972
3973struct disk_extent {
Chris Masonc8b97812008-10-29 14:49:59 -04003974 u64 ram_bytes;
Zheng Yan1a40e232008-09-26 10:09:34 -04003975 u64 disk_bytenr;
3976 u64 disk_num_bytes;
3977 u64 offset;
3978 u64 num_bytes;
Chris Masonc8b97812008-10-29 14:49:59 -04003979 u8 compression;
3980 u8 encryption;
3981 u16 other_encoding;
Zheng Yan1a40e232008-09-26 10:09:34 -04003982};
3983
3984static int is_cowonly_root(u64 root_objectid)
3985{
3986 if (root_objectid == BTRFS_ROOT_TREE_OBJECTID ||
3987 root_objectid == BTRFS_EXTENT_TREE_OBJECTID ||
3988 root_objectid == BTRFS_CHUNK_TREE_OBJECTID ||
3989 root_objectid == BTRFS_DEV_TREE_OBJECTID ||
Yan Zheng0403e472008-12-10 20:32:51 -05003990 root_objectid == BTRFS_TREE_LOG_OBJECTID ||
3991 root_objectid == BTRFS_CSUM_TREE_OBJECTID)
Zheng Yan1a40e232008-09-26 10:09:34 -04003992 return 1;
3993 return 0;
3994}
3995
Chris Masond3977122009-01-05 21:25:51 -05003996static noinline int __next_ref_path(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04003997 struct btrfs_root *extent_root,
3998 struct btrfs_ref_path *ref_path,
3999 int first_time)
4000{
4001 struct extent_buffer *leaf;
4002 struct btrfs_path *path;
Chris Mason4313b392008-01-03 09:08:48 -05004003 struct btrfs_extent_ref *ref;
Chris Masonedbd8d42007-12-21 16:27:24 -05004004 struct btrfs_key key;
4005 struct btrfs_key found_key;
Zheng Yan1a40e232008-09-26 10:09:34 -04004006 u64 bytenr;
Chris Masonedbd8d42007-12-21 16:27:24 -05004007 u32 nritems;
Zheng Yan1a40e232008-09-26 10:09:34 -04004008 int level;
4009 int ret = 1;
Chris Masonedbd8d42007-12-21 16:27:24 -05004010
Zheng Yan1a40e232008-09-26 10:09:34 -04004011 path = btrfs_alloc_path();
4012 if (!path)
4013 return -ENOMEM;
4014
Zheng Yan1a40e232008-09-26 10:09:34 -04004015 if (first_time) {
4016 ref_path->lowest_level = -1;
4017 ref_path->current_level = -1;
Yan Zhengf82d02d2008-10-29 14:49:05 -04004018 ref_path->shared_level = -1;
Zheng Yan1a40e232008-09-26 10:09:34 -04004019 goto walk_up;
Chris Masona061fc82008-05-07 11:43:44 -04004020 }
Zheng Yan1a40e232008-09-26 10:09:34 -04004021walk_down:
4022 level = ref_path->current_level - 1;
4023 while (level >= -1) {
4024 u64 parent;
4025 if (level < ref_path->lowest_level)
4026 break;
Chris Masonedbd8d42007-12-21 16:27:24 -05004027
Chris Masond3977122009-01-05 21:25:51 -05004028 if (level >= 0)
Zheng Yan1a40e232008-09-26 10:09:34 -04004029 bytenr = ref_path->nodes[level];
Chris Masond3977122009-01-05 21:25:51 -05004030 else
Zheng Yan1a40e232008-09-26 10:09:34 -04004031 bytenr = ref_path->extent_start;
Zheng Yan1a40e232008-09-26 10:09:34 -04004032 BUG_ON(bytenr == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05004033
Zheng Yan1a40e232008-09-26 10:09:34 -04004034 parent = ref_path->nodes[level + 1];
4035 ref_path->nodes[level + 1] = 0;
4036 ref_path->current_level = level;
4037 BUG_ON(parent == 0);
4038
4039 key.objectid = bytenr;
4040 key.offset = parent + 1;
4041 key.type = BTRFS_EXTENT_REF_KEY;
4042
4043 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05004044 if (ret < 0)
4045 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04004046 BUG_ON(ret == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05004047
Chris Masonedbd8d42007-12-21 16:27:24 -05004048 leaf = path->nodes[0];
4049 nritems = btrfs_header_nritems(leaf);
Zheng Yan1a40e232008-09-26 10:09:34 -04004050 if (path->slots[0] >= nritems) {
Chris Masona061fc82008-05-07 11:43:44 -04004051 ret = btrfs_next_leaf(extent_root, path);
Chris Masona061fc82008-05-07 11:43:44 -04004052 if (ret < 0)
4053 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04004054 if (ret > 0)
4055 goto next;
Chris Masonbf4ef672008-05-08 13:26:18 -04004056 leaf = path->nodes[0];
Chris Masona061fc82008-05-07 11:43:44 -04004057 }
Chris Masonedbd8d42007-12-21 16:27:24 -05004058
4059 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Zheng Yan1a40e232008-09-26 10:09:34 -04004060 if (found_key.objectid == bytenr &&
Yan Zhengf82d02d2008-10-29 14:49:05 -04004061 found_key.type == BTRFS_EXTENT_REF_KEY) {
4062 if (level < ref_path->shared_level)
4063 ref_path->shared_level = level;
Zheng Yan1a40e232008-09-26 10:09:34 -04004064 goto found;
Yan Zhengf82d02d2008-10-29 14:49:05 -04004065 }
Zheng Yan1a40e232008-09-26 10:09:34 -04004066next:
4067 level--;
4068 btrfs_release_path(extent_root, path);
Yan Zhengd899e052008-10-30 14:25:28 -04004069 cond_resched();
Zheng Yan1a40e232008-09-26 10:09:34 -04004070 }
4071 /* reached lowest level */
4072 ret = 1;
4073 goto out;
4074walk_up:
4075 level = ref_path->current_level;
4076 while (level < BTRFS_MAX_LEVEL - 1) {
4077 u64 ref_objectid;
Chris Masond3977122009-01-05 21:25:51 -05004078
4079 if (level >= 0)
Zheng Yan1a40e232008-09-26 10:09:34 -04004080 bytenr = ref_path->nodes[level];
Chris Masond3977122009-01-05 21:25:51 -05004081 else
Zheng Yan1a40e232008-09-26 10:09:34 -04004082 bytenr = ref_path->extent_start;
Chris Masond3977122009-01-05 21:25:51 -05004083
Zheng Yan1a40e232008-09-26 10:09:34 -04004084 BUG_ON(bytenr == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05004085
Zheng Yan1a40e232008-09-26 10:09:34 -04004086 key.objectid = bytenr;
4087 key.offset = 0;
4088 key.type = BTRFS_EXTENT_REF_KEY;
Chris Masonedbd8d42007-12-21 16:27:24 -05004089
Zheng Yan1a40e232008-09-26 10:09:34 -04004090 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
4091 if (ret < 0)
Chris Masonedbd8d42007-12-21 16:27:24 -05004092 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04004093
4094 leaf = path->nodes[0];
4095 nritems = btrfs_header_nritems(leaf);
4096 if (path->slots[0] >= nritems) {
4097 ret = btrfs_next_leaf(extent_root, path);
4098 if (ret < 0)
4099 goto out;
4100 if (ret > 0) {
4101 /* the extent was freed by someone */
4102 if (ref_path->lowest_level == level)
4103 goto out;
4104 btrfs_release_path(extent_root, path);
4105 goto walk_down;
4106 }
4107 leaf = path->nodes[0];
4108 }
4109
4110 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4111 if (found_key.objectid != bytenr ||
4112 found_key.type != BTRFS_EXTENT_REF_KEY) {
4113 /* the extent was freed by someone */
4114 if (ref_path->lowest_level == level) {
4115 ret = 1;
4116 goto out;
4117 }
4118 btrfs_release_path(extent_root, path);
4119 goto walk_down;
4120 }
4121found:
4122 ref = btrfs_item_ptr(leaf, path->slots[0],
4123 struct btrfs_extent_ref);
4124 ref_objectid = btrfs_ref_objectid(leaf, ref);
4125 if (ref_objectid < BTRFS_FIRST_FREE_OBJECTID) {
4126 if (first_time) {
4127 level = (int)ref_objectid;
4128 BUG_ON(level >= BTRFS_MAX_LEVEL);
4129 ref_path->lowest_level = level;
4130 ref_path->current_level = level;
4131 ref_path->nodes[level] = bytenr;
4132 } else {
4133 WARN_ON(ref_objectid != level);
4134 }
4135 } else {
4136 WARN_ON(level != -1);
4137 }
4138 first_time = 0;
4139
4140 if (ref_path->lowest_level == level) {
4141 ref_path->owner_objectid = ref_objectid;
Zheng Yan1a40e232008-09-26 10:09:34 -04004142 ref_path->num_refs = btrfs_ref_num_refs(leaf, ref);
4143 }
4144
4145 /*
4146 * the block is tree root or the block isn't in reference
4147 * counted tree.
4148 */
4149 if (found_key.objectid == found_key.offset ||
4150 is_cowonly_root(btrfs_ref_root(leaf, ref))) {
4151 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
4152 ref_path->root_generation =
4153 btrfs_ref_generation(leaf, ref);
4154 if (level < 0) {
4155 /* special reference from the tree log */
4156 ref_path->nodes[0] = found_key.offset;
4157 ref_path->current_level = 0;
4158 }
4159 ret = 0;
4160 goto out;
4161 }
4162
4163 level++;
4164 BUG_ON(ref_path->nodes[level] != 0);
4165 ref_path->nodes[level] = found_key.offset;
4166 ref_path->current_level = level;
4167
4168 /*
4169 * the reference was created in the running transaction,
4170 * no need to continue walking up.
4171 */
4172 if (btrfs_ref_generation(leaf, ref) == trans->transid) {
4173 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
4174 ref_path->root_generation =
4175 btrfs_ref_generation(leaf, ref);
4176 ret = 0;
4177 goto out;
4178 }
4179
4180 btrfs_release_path(extent_root, path);
Yan Zhengd899e052008-10-30 14:25:28 -04004181 cond_resched();
Zheng Yan1a40e232008-09-26 10:09:34 -04004182 }
4183 /* reached max tree level, but no tree root found. */
4184 BUG();
4185out:
Zheng Yan1a40e232008-09-26 10:09:34 -04004186 btrfs_free_path(path);
4187 return ret;
4188}
4189
4190static int btrfs_first_ref_path(struct btrfs_trans_handle *trans,
4191 struct btrfs_root *extent_root,
4192 struct btrfs_ref_path *ref_path,
4193 u64 extent_start)
4194{
4195 memset(ref_path, 0, sizeof(*ref_path));
4196 ref_path->extent_start = extent_start;
4197
4198 return __next_ref_path(trans, extent_root, ref_path, 1);
4199}
4200
4201static int btrfs_next_ref_path(struct btrfs_trans_handle *trans,
4202 struct btrfs_root *extent_root,
4203 struct btrfs_ref_path *ref_path)
4204{
4205 return __next_ref_path(trans, extent_root, ref_path, 0);
4206}
4207
Chris Masond3977122009-01-05 21:25:51 -05004208static noinline int get_new_locations(struct inode *reloc_inode,
Zheng Yan1a40e232008-09-26 10:09:34 -04004209 struct btrfs_key *extent_key,
4210 u64 offset, int no_fragment,
4211 struct disk_extent **extents,
4212 int *nr_extents)
4213{
4214 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
4215 struct btrfs_path *path;
4216 struct btrfs_file_extent_item *fi;
4217 struct extent_buffer *leaf;
4218 struct disk_extent *exts = *extents;
4219 struct btrfs_key found_key;
4220 u64 cur_pos;
4221 u64 last_byte;
4222 u32 nritems;
4223 int nr = 0;
4224 int max = *nr_extents;
4225 int ret;
4226
4227 WARN_ON(!no_fragment && *extents);
4228 if (!exts) {
4229 max = 1;
4230 exts = kmalloc(sizeof(*exts) * max, GFP_NOFS);
4231 if (!exts)
4232 return -ENOMEM;
4233 }
4234
4235 path = btrfs_alloc_path();
4236 BUG_ON(!path);
4237
4238 cur_pos = extent_key->objectid - offset;
4239 last_byte = extent_key->objectid + extent_key->offset;
4240 ret = btrfs_lookup_file_extent(NULL, root, path, reloc_inode->i_ino,
4241 cur_pos, 0);
4242 if (ret < 0)
4243 goto out;
4244 if (ret > 0) {
4245 ret = -ENOENT;
4246 goto out;
4247 }
4248
4249 while (1) {
4250 leaf = path->nodes[0];
4251 nritems = btrfs_header_nritems(leaf);
4252 if (path->slots[0] >= nritems) {
4253 ret = btrfs_next_leaf(root, path);
4254 if (ret < 0)
4255 goto out;
4256 if (ret > 0)
4257 break;
4258 leaf = path->nodes[0];
4259 }
4260
4261 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4262 if (found_key.offset != cur_pos ||
4263 found_key.type != BTRFS_EXTENT_DATA_KEY ||
4264 found_key.objectid != reloc_inode->i_ino)
4265 break;
4266
4267 fi = btrfs_item_ptr(leaf, path->slots[0],
4268 struct btrfs_file_extent_item);
4269 if (btrfs_file_extent_type(leaf, fi) !=
4270 BTRFS_FILE_EXTENT_REG ||
4271 btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
4272 break;
4273
4274 if (nr == max) {
4275 struct disk_extent *old = exts;
4276 max *= 2;
4277 exts = kzalloc(sizeof(*exts) * max, GFP_NOFS);
4278 memcpy(exts, old, sizeof(*exts) * nr);
4279 if (old != *extents)
4280 kfree(old);
4281 }
4282
4283 exts[nr].disk_bytenr =
4284 btrfs_file_extent_disk_bytenr(leaf, fi);
4285 exts[nr].disk_num_bytes =
4286 btrfs_file_extent_disk_num_bytes(leaf, fi);
4287 exts[nr].offset = btrfs_file_extent_offset(leaf, fi);
4288 exts[nr].num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
Chris Masonc8b97812008-10-29 14:49:59 -04004289 exts[nr].ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
4290 exts[nr].compression = btrfs_file_extent_compression(leaf, fi);
4291 exts[nr].encryption = btrfs_file_extent_encryption(leaf, fi);
4292 exts[nr].other_encoding = btrfs_file_extent_other_encoding(leaf,
4293 fi);
Yan Zhengd899e052008-10-30 14:25:28 -04004294 BUG_ON(exts[nr].offset > 0);
4295 BUG_ON(exts[nr].compression || exts[nr].encryption);
4296 BUG_ON(exts[nr].num_bytes != exts[nr].disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04004297
4298 cur_pos += exts[nr].num_bytes;
4299 nr++;
4300
4301 if (cur_pos + offset >= last_byte)
4302 break;
4303
4304 if (no_fragment) {
4305 ret = 1;
4306 goto out;
4307 }
4308 path->slots[0]++;
4309 }
4310
Yan Zheng1f80e4d2008-12-19 10:59:04 -05004311 BUG_ON(cur_pos + offset > last_byte);
Zheng Yan1a40e232008-09-26 10:09:34 -04004312 if (cur_pos + offset < last_byte) {
4313 ret = -ENOENT;
4314 goto out;
Chris Masonedbd8d42007-12-21 16:27:24 -05004315 }
4316 ret = 0;
4317out:
Zheng Yan1a40e232008-09-26 10:09:34 -04004318 btrfs_free_path(path);
4319 if (ret) {
4320 if (exts != *extents)
4321 kfree(exts);
4322 } else {
4323 *extents = exts;
4324 *nr_extents = nr;
4325 }
4326 return ret;
4327}
4328
Chris Masond3977122009-01-05 21:25:51 -05004329static noinline int replace_one_extent(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04004330 struct btrfs_root *root,
4331 struct btrfs_path *path,
4332 struct btrfs_key *extent_key,
4333 struct btrfs_key *leaf_key,
4334 struct btrfs_ref_path *ref_path,
4335 struct disk_extent *new_extents,
4336 int nr_extents)
4337{
4338 struct extent_buffer *leaf;
4339 struct btrfs_file_extent_item *fi;
4340 struct inode *inode = NULL;
4341 struct btrfs_key key;
4342 u64 lock_start = 0;
4343 u64 lock_end = 0;
4344 u64 num_bytes;
4345 u64 ext_offset;
Yan Zheng86288a12009-01-21 10:49:16 -05004346 u64 search_end = (u64)-1;
Zheng Yan1a40e232008-09-26 10:09:34 -04004347 u32 nritems;
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004348 int nr_scaned = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04004349 int extent_locked = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04004350 int extent_type;
Zheng Yan1a40e232008-09-26 10:09:34 -04004351 int ret;
4352
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004353 memcpy(&key, leaf_key, sizeof(key));
Zheng Yan1a40e232008-09-26 10:09:34 -04004354 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004355 if (key.objectid < ref_path->owner_objectid ||
4356 (key.objectid == ref_path->owner_objectid &&
4357 key.type < BTRFS_EXTENT_DATA_KEY)) {
4358 key.objectid = ref_path->owner_objectid;
4359 key.type = BTRFS_EXTENT_DATA_KEY;
4360 key.offset = 0;
4361 }
Zheng Yan1a40e232008-09-26 10:09:34 -04004362 }
4363
4364 while (1) {
4365 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
4366 if (ret < 0)
4367 goto out;
4368
4369 leaf = path->nodes[0];
4370 nritems = btrfs_header_nritems(leaf);
4371next:
4372 if (extent_locked && ret > 0) {
4373 /*
4374 * the file extent item was modified by someone
4375 * before the extent got locked.
4376 */
Zheng Yan1a40e232008-09-26 10:09:34 -04004377 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
4378 lock_end, GFP_NOFS);
4379 extent_locked = 0;
4380 }
4381
4382 if (path->slots[0] >= nritems) {
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004383 if (++nr_scaned > 2)
Zheng Yan1a40e232008-09-26 10:09:34 -04004384 break;
4385
4386 BUG_ON(extent_locked);
4387 ret = btrfs_next_leaf(root, path);
4388 if (ret < 0)
4389 goto out;
4390 if (ret > 0)
4391 break;
4392 leaf = path->nodes[0];
4393 nritems = btrfs_header_nritems(leaf);
4394 }
4395
4396 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4397
4398 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
4399 if ((key.objectid > ref_path->owner_objectid) ||
4400 (key.objectid == ref_path->owner_objectid &&
4401 key.type > BTRFS_EXTENT_DATA_KEY) ||
Yan Zheng86288a12009-01-21 10:49:16 -05004402 key.offset >= search_end)
Zheng Yan1a40e232008-09-26 10:09:34 -04004403 break;
4404 }
4405
4406 if (inode && key.objectid != inode->i_ino) {
4407 BUG_ON(extent_locked);
4408 btrfs_release_path(root, path);
4409 mutex_unlock(&inode->i_mutex);
4410 iput(inode);
4411 inode = NULL;
4412 continue;
4413 }
4414
4415 if (key.type != BTRFS_EXTENT_DATA_KEY) {
4416 path->slots[0]++;
4417 ret = 1;
4418 goto next;
4419 }
4420 fi = btrfs_item_ptr(leaf, path->slots[0],
4421 struct btrfs_file_extent_item);
Yan Zhengd899e052008-10-30 14:25:28 -04004422 extent_type = btrfs_file_extent_type(leaf, fi);
4423 if ((extent_type != BTRFS_FILE_EXTENT_REG &&
4424 extent_type != BTRFS_FILE_EXTENT_PREALLOC) ||
Zheng Yan1a40e232008-09-26 10:09:34 -04004425 (btrfs_file_extent_disk_bytenr(leaf, fi) !=
4426 extent_key->objectid)) {
4427 path->slots[0]++;
4428 ret = 1;
4429 goto next;
4430 }
4431
4432 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
4433 ext_offset = btrfs_file_extent_offset(leaf, fi);
4434
Yan Zheng86288a12009-01-21 10:49:16 -05004435 if (search_end == (u64)-1) {
4436 search_end = key.offset - ext_offset +
4437 btrfs_file_extent_ram_bytes(leaf, fi);
4438 }
Zheng Yan1a40e232008-09-26 10:09:34 -04004439
4440 if (!extent_locked) {
4441 lock_start = key.offset;
4442 lock_end = lock_start + num_bytes - 1;
4443 } else {
Yan Zheng66435582008-10-30 14:19:50 -04004444 if (lock_start > key.offset ||
4445 lock_end + 1 < key.offset + num_bytes) {
4446 unlock_extent(&BTRFS_I(inode)->io_tree,
4447 lock_start, lock_end, GFP_NOFS);
4448 extent_locked = 0;
4449 }
Zheng Yan1a40e232008-09-26 10:09:34 -04004450 }
4451
4452 if (!inode) {
4453 btrfs_release_path(root, path);
4454
4455 inode = btrfs_iget_locked(root->fs_info->sb,
4456 key.objectid, root);
4457 if (inode->i_state & I_NEW) {
4458 BTRFS_I(inode)->root = root;
4459 BTRFS_I(inode)->location.objectid =
4460 key.objectid;
4461 BTRFS_I(inode)->location.type =
4462 BTRFS_INODE_ITEM_KEY;
4463 BTRFS_I(inode)->location.offset = 0;
4464 btrfs_read_locked_inode(inode);
4465 unlock_new_inode(inode);
4466 }
4467 /*
4468 * some code call btrfs_commit_transaction while
4469 * holding the i_mutex, so we can't use mutex_lock
4470 * here.
4471 */
4472 if (is_bad_inode(inode) ||
4473 !mutex_trylock(&inode->i_mutex)) {
4474 iput(inode);
4475 inode = NULL;
4476 key.offset = (u64)-1;
4477 goto skip;
4478 }
4479 }
4480
4481 if (!extent_locked) {
4482 struct btrfs_ordered_extent *ordered;
4483
4484 btrfs_release_path(root, path);
4485
4486 lock_extent(&BTRFS_I(inode)->io_tree, lock_start,
4487 lock_end, GFP_NOFS);
4488 ordered = btrfs_lookup_first_ordered_extent(inode,
4489 lock_end);
4490 if (ordered &&
4491 ordered->file_offset <= lock_end &&
4492 ordered->file_offset + ordered->len > lock_start) {
4493 unlock_extent(&BTRFS_I(inode)->io_tree,
4494 lock_start, lock_end, GFP_NOFS);
4495 btrfs_start_ordered_extent(inode, ordered, 1);
4496 btrfs_put_ordered_extent(ordered);
4497 key.offset += num_bytes;
4498 goto skip;
4499 }
4500 if (ordered)
4501 btrfs_put_ordered_extent(ordered);
4502
Zheng Yan1a40e232008-09-26 10:09:34 -04004503 extent_locked = 1;
4504 continue;
4505 }
4506
4507 if (nr_extents == 1) {
4508 /* update extent pointer in place */
Zheng Yan1a40e232008-09-26 10:09:34 -04004509 btrfs_set_file_extent_disk_bytenr(leaf, fi,
4510 new_extents[0].disk_bytenr);
4511 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
4512 new_extents[0].disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04004513 btrfs_mark_buffer_dirty(leaf);
4514
4515 btrfs_drop_extent_cache(inode, key.offset,
4516 key.offset + num_bytes - 1, 0);
4517
4518 ret = btrfs_inc_extent_ref(trans, root,
4519 new_extents[0].disk_bytenr,
4520 new_extents[0].disk_num_bytes,
4521 leaf->start,
4522 root->root_key.objectid,
4523 trans->transid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004524 key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04004525 BUG_ON(ret);
4526
4527 ret = btrfs_free_extent(trans, root,
4528 extent_key->objectid,
4529 extent_key->offset,
4530 leaf->start,
4531 btrfs_header_owner(leaf),
4532 btrfs_header_generation(leaf),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004533 key.objectid, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04004534 BUG_ON(ret);
4535
4536 btrfs_release_path(root, path);
4537 key.offset += num_bytes;
4538 } else {
Yan Zhengd899e052008-10-30 14:25:28 -04004539 BUG_ON(1);
4540#if 0
Zheng Yan1a40e232008-09-26 10:09:34 -04004541 u64 alloc_hint;
4542 u64 extent_len;
4543 int i;
4544 /*
4545 * drop old extent pointer at first, then insert the
4546 * new pointers one bye one
4547 */
4548 btrfs_release_path(root, path);
4549 ret = btrfs_drop_extents(trans, root, inode, key.offset,
4550 key.offset + num_bytes,
4551 key.offset, &alloc_hint);
4552 BUG_ON(ret);
4553
4554 for (i = 0; i < nr_extents; i++) {
4555 if (ext_offset >= new_extents[i].num_bytes) {
4556 ext_offset -= new_extents[i].num_bytes;
4557 continue;
4558 }
4559 extent_len = min(new_extents[i].num_bytes -
4560 ext_offset, num_bytes);
4561
4562 ret = btrfs_insert_empty_item(trans, root,
4563 path, &key,
4564 sizeof(*fi));
4565 BUG_ON(ret);
4566
4567 leaf = path->nodes[0];
4568 fi = btrfs_item_ptr(leaf, path->slots[0],
4569 struct btrfs_file_extent_item);
4570 btrfs_set_file_extent_generation(leaf, fi,
4571 trans->transid);
4572 btrfs_set_file_extent_type(leaf, fi,
4573 BTRFS_FILE_EXTENT_REG);
4574 btrfs_set_file_extent_disk_bytenr(leaf, fi,
4575 new_extents[i].disk_bytenr);
4576 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
4577 new_extents[i].disk_num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -04004578 btrfs_set_file_extent_ram_bytes(leaf, fi,
4579 new_extents[i].ram_bytes);
4580
4581 btrfs_set_file_extent_compression(leaf, fi,
4582 new_extents[i].compression);
4583 btrfs_set_file_extent_encryption(leaf, fi,
4584 new_extents[i].encryption);
4585 btrfs_set_file_extent_other_encoding(leaf, fi,
4586 new_extents[i].other_encoding);
4587
Zheng Yan1a40e232008-09-26 10:09:34 -04004588 btrfs_set_file_extent_num_bytes(leaf, fi,
4589 extent_len);
4590 ext_offset += new_extents[i].offset;
4591 btrfs_set_file_extent_offset(leaf, fi,
4592 ext_offset);
4593 btrfs_mark_buffer_dirty(leaf);
4594
4595 btrfs_drop_extent_cache(inode, key.offset,
4596 key.offset + extent_len - 1, 0);
4597
4598 ret = btrfs_inc_extent_ref(trans, root,
4599 new_extents[i].disk_bytenr,
4600 new_extents[i].disk_num_bytes,
4601 leaf->start,
4602 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004603 trans->transid, key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04004604 BUG_ON(ret);
4605 btrfs_release_path(root, path);
4606
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004607 inode_add_bytes(inode, extent_len);
Zheng Yan1a40e232008-09-26 10:09:34 -04004608
4609 ext_offset = 0;
4610 num_bytes -= extent_len;
4611 key.offset += extent_len;
4612
4613 if (num_bytes == 0)
4614 break;
4615 }
4616 BUG_ON(i >= nr_extents);
Yan Zhengd899e052008-10-30 14:25:28 -04004617#endif
Zheng Yan1a40e232008-09-26 10:09:34 -04004618 }
4619
4620 if (extent_locked) {
Zheng Yan1a40e232008-09-26 10:09:34 -04004621 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
4622 lock_end, GFP_NOFS);
4623 extent_locked = 0;
4624 }
4625skip:
4626 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS &&
Yan Zheng86288a12009-01-21 10:49:16 -05004627 key.offset >= search_end)
Zheng Yan1a40e232008-09-26 10:09:34 -04004628 break;
4629
4630 cond_resched();
4631 }
4632 ret = 0;
4633out:
4634 btrfs_release_path(root, path);
4635 if (inode) {
4636 mutex_unlock(&inode->i_mutex);
4637 if (extent_locked) {
Zheng Yan1a40e232008-09-26 10:09:34 -04004638 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
4639 lock_end, GFP_NOFS);
4640 }
4641 iput(inode);
4642 }
4643 return ret;
4644}
4645
Zheng Yan1a40e232008-09-26 10:09:34 -04004646int btrfs_reloc_tree_cache_ref(struct btrfs_trans_handle *trans,
4647 struct btrfs_root *root,
4648 struct extent_buffer *buf, u64 orig_start)
4649{
4650 int level;
4651 int ret;
4652
4653 BUG_ON(btrfs_header_generation(buf) != trans->transid);
4654 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
4655
4656 level = btrfs_header_level(buf);
4657 if (level == 0) {
4658 struct btrfs_leaf_ref *ref;
4659 struct btrfs_leaf_ref *orig_ref;
4660
4661 orig_ref = btrfs_lookup_leaf_ref(root, orig_start);
4662 if (!orig_ref)
4663 return -ENOENT;
4664
4665 ref = btrfs_alloc_leaf_ref(root, orig_ref->nritems);
4666 if (!ref) {
4667 btrfs_free_leaf_ref(root, orig_ref);
4668 return -ENOMEM;
4669 }
4670
4671 ref->nritems = orig_ref->nritems;
4672 memcpy(ref->extents, orig_ref->extents,
4673 sizeof(ref->extents[0]) * ref->nritems);
4674
4675 btrfs_free_leaf_ref(root, orig_ref);
4676
4677 ref->root_gen = trans->transid;
4678 ref->bytenr = buf->start;
4679 ref->owner = btrfs_header_owner(buf);
4680 ref->generation = btrfs_header_generation(buf);
Chris Masonbd56b302009-02-04 09:27:02 -05004681
Zheng Yan1a40e232008-09-26 10:09:34 -04004682 ret = btrfs_add_leaf_ref(root, ref, 0);
4683 WARN_ON(ret);
4684 btrfs_free_leaf_ref(root, ref);
4685 }
4686 return 0;
4687}
4688
Chris Masond3977122009-01-05 21:25:51 -05004689static noinline int invalidate_extent_cache(struct btrfs_root *root,
Zheng Yan1a40e232008-09-26 10:09:34 -04004690 struct extent_buffer *leaf,
4691 struct btrfs_block_group_cache *group,
4692 struct btrfs_root *target_root)
4693{
4694 struct btrfs_key key;
4695 struct inode *inode = NULL;
4696 struct btrfs_file_extent_item *fi;
4697 u64 num_bytes;
4698 u64 skip_objectid = 0;
4699 u32 nritems;
4700 u32 i;
4701
4702 nritems = btrfs_header_nritems(leaf);
4703 for (i = 0; i < nritems; i++) {
4704 btrfs_item_key_to_cpu(leaf, &key, i);
4705 if (key.objectid == skip_objectid ||
4706 key.type != BTRFS_EXTENT_DATA_KEY)
4707 continue;
4708 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
4709 if (btrfs_file_extent_type(leaf, fi) ==
4710 BTRFS_FILE_EXTENT_INLINE)
4711 continue;
4712 if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
4713 continue;
4714 if (!inode || inode->i_ino != key.objectid) {
4715 iput(inode);
4716 inode = btrfs_ilookup(target_root->fs_info->sb,
4717 key.objectid, target_root, 1);
4718 }
4719 if (!inode) {
4720 skip_objectid = key.objectid;
4721 continue;
4722 }
4723 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
4724
4725 lock_extent(&BTRFS_I(inode)->io_tree, key.offset,
4726 key.offset + num_bytes - 1, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04004727 btrfs_drop_extent_cache(inode, key.offset,
4728 key.offset + num_bytes - 1, 1);
Zheng Yan1a40e232008-09-26 10:09:34 -04004729 unlock_extent(&BTRFS_I(inode)->io_tree, key.offset,
4730 key.offset + num_bytes - 1, GFP_NOFS);
4731 cond_resched();
4732 }
4733 iput(inode);
4734 return 0;
4735}
4736
Chris Masond3977122009-01-05 21:25:51 -05004737static noinline int replace_extents_in_leaf(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04004738 struct btrfs_root *root,
4739 struct extent_buffer *leaf,
4740 struct btrfs_block_group_cache *group,
4741 struct inode *reloc_inode)
4742{
4743 struct btrfs_key key;
4744 struct btrfs_key extent_key;
4745 struct btrfs_file_extent_item *fi;
4746 struct btrfs_leaf_ref *ref;
4747 struct disk_extent *new_extent;
4748 u64 bytenr;
4749 u64 num_bytes;
4750 u32 nritems;
4751 u32 i;
4752 int ext_index;
4753 int nr_extent;
4754 int ret;
4755
4756 new_extent = kmalloc(sizeof(*new_extent), GFP_NOFS);
4757 BUG_ON(!new_extent);
4758
4759 ref = btrfs_lookup_leaf_ref(root, leaf->start);
4760 BUG_ON(!ref);
4761
4762 ext_index = -1;
4763 nritems = btrfs_header_nritems(leaf);
4764 for (i = 0; i < nritems; i++) {
4765 btrfs_item_key_to_cpu(leaf, &key, i);
4766 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
4767 continue;
4768 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
4769 if (btrfs_file_extent_type(leaf, fi) ==
4770 BTRFS_FILE_EXTENT_INLINE)
4771 continue;
4772 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
4773 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
4774 if (bytenr == 0)
4775 continue;
4776
4777 ext_index++;
4778 if (bytenr >= group->key.objectid + group->key.offset ||
4779 bytenr + num_bytes <= group->key.objectid)
4780 continue;
4781
4782 extent_key.objectid = bytenr;
4783 extent_key.offset = num_bytes;
4784 extent_key.type = BTRFS_EXTENT_ITEM_KEY;
4785 nr_extent = 1;
4786 ret = get_new_locations(reloc_inode, &extent_key,
4787 group->key.objectid, 1,
4788 &new_extent, &nr_extent);
4789 if (ret > 0)
4790 continue;
4791 BUG_ON(ret < 0);
4792
4793 BUG_ON(ref->extents[ext_index].bytenr != bytenr);
4794 BUG_ON(ref->extents[ext_index].num_bytes != num_bytes);
4795 ref->extents[ext_index].bytenr = new_extent->disk_bytenr;
4796 ref->extents[ext_index].num_bytes = new_extent->disk_num_bytes;
4797
Zheng Yan1a40e232008-09-26 10:09:34 -04004798 btrfs_set_file_extent_disk_bytenr(leaf, fi,
4799 new_extent->disk_bytenr);
4800 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
4801 new_extent->disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04004802 btrfs_mark_buffer_dirty(leaf);
4803
4804 ret = btrfs_inc_extent_ref(trans, root,
4805 new_extent->disk_bytenr,
4806 new_extent->disk_num_bytes,
4807 leaf->start,
4808 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004809 trans->transid, key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04004810 BUG_ON(ret);
Chris Mason56bec292009-03-13 10:10:06 -04004811
Zheng Yan1a40e232008-09-26 10:09:34 -04004812 ret = btrfs_free_extent(trans, root,
4813 bytenr, num_bytes, leaf->start,
4814 btrfs_header_owner(leaf),
4815 btrfs_header_generation(leaf),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004816 key.objectid, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04004817 BUG_ON(ret);
4818 cond_resched();
4819 }
4820 kfree(new_extent);
4821 BUG_ON(ext_index + 1 != ref->nritems);
4822 btrfs_free_leaf_ref(root, ref);
4823 return 0;
4824}
4825
Yan Zhengf82d02d2008-10-29 14:49:05 -04004826int btrfs_free_reloc_root(struct btrfs_trans_handle *trans,
4827 struct btrfs_root *root)
Zheng Yan1a40e232008-09-26 10:09:34 -04004828{
4829 struct btrfs_root *reloc_root;
Yan Zhengf82d02d2008-10-29 14:49:05 -04004830 int ret;
Zheng Yan1a40e232008-09-26 10:09:34 -04004831
4832 if (root->reloc_root) {
4833 reloc_root = root->reloc_root;
4834 root->reloc_root = NULL;
4835 list_add(&reloc_root->dead_list,
4836 &root->fs_info->dead_reloc_roots);
Yan Zhengf82d02d2008-10-29 14:49:05 -04004837
4838 btrfs_set_root_bytenr(&reloc_root->root_item,
4839 reloc_root->node->start);
4840 btrfs_set_root_level(&root->root_item,
4841 btrfs_header_level(reloc_root->node));
4842 memset(&reloc_root->root_item.drop_progress, 0,
4843 sizeof(struct btrfs_disk_key));
4844 reloc_root->root_item.drop_level = 0;
4845
4846 ret = btrfs_update_root(trans, root->fs_info->tree_root,
4847 &reloc_root->root_key,
4848 &reloc_root->root_item);
4849 BUG_ON(ret);
Zheng Yan1a40e232008-09-26 10:09:34 -04004850 }
4851 return 0;
4852}
4853
4854int btrfs_drop_dead_reloc_roots(struct btrfs_root *root)
4855{
4856 struct btrfs_trans_handle *trans;
4857 struct btrfs_root *reloc_root;
4858 struct btrfs_root *prev_root = NULL;
4859 struct list_head dead_roots;
4860 int ret;
4861 unsigned long nr;
4862
4863 INIT_LIST_HEAD(&dead_roots);
4864 list_splice_init(&root->fs_info->dead_reloc_roots, &dead_roots);
4865
4866 while (!list_empty(&dead_roots)) {
4867 reloc_root = list_entry(dead_roots.prev,
4868 struct btrfs_root, dead_list);
4869 list_del_init(&reloc_root->dead_list);
4870
4871 BUG_ON(reloc_root->commit_root != NULL);
4872 while (1) {
4873 trans = btrfs_join_transaction(root, 1);
4874 BUG_ON(!trans);
4875
4876 mutex_lock(&root->fs_info->drop_mutex);
4877 ret = btrfs_drop_snapshot(trans, reloc_root);
4878 if (ret != -EAGAIN)
4879 break;
4880 mutex_unlock(&root->fs_info->drop_mutex);
4881
4882 nr = trans->blocks_used;
4883 ret = btrfs_end_transaction(trans, root);
4884 BUG_ON(ret);
4885 btrfs_btree_balance_dirty(root, nr);
4886 }
4887
4888 free_extent_buffer(reloc_root->node);
4889
4890 ret = btrfs_del_root(trans, root->fs_info->tree_root,
4891 &reloc_root->root_key);
4892 BUG_ON(ret);
4893 mutex_unlock(&root->fs_info->drop_mutex);
4894
4895 nr = trans->blocks_used;
4896 ret = btrfs_end_transaction(trans, root);
4897 BUG_ON(ret);
4898 btrfs_btree_balance_dirty(root, nr);
4899
4900 kfree(prev_root);
4901 prev_root = reloc_root;
4902 }
4903 if (prev_root) {
4904 btrfs_remove_leaf_refs(prev_root, (u64)-1, 0);
4905 kfree(prev_root);
4906 }
4907 return 0;
4908}
4909
4910int btrfs_add_dead_reloc_root(struct btrfs_root *root)
4911{
4912 list_add(&root->dead_list, &root->fs_info->dead_reloc_roots);
4913 return 0;
4914}
4915
4916int btrfs_cleanup_reloc_trees(struct btrfs_root *root)
4917{
4918 struct btrfs_root *reloc_root;
4919 struct btrfs_trans_handle *trans;
4920 struct btrfs_key location;
4921 int found;
4922 int ret;
4923
4924 mutex_lock(&root->fs_info->tree_reloc_mutex);
4925 ret = btrfs_find_dead_roots(root, BTRFS_TREE_RELOC_OBJECTID, NULL);
4926 BUG_ON(ret);
4927 found = !list_empty(&root->fs_info->dead_reloc_roots);
4928 mutex_unlock(&root->fs_info->tree_reloc_mutex);
4929
4930 if (found) {
4931 trans = btrfs_start_transaction(root, 1);
4932 BUG_ON(!trans);
4933 ret = btrfs_commit_transaction(trans, root);
4934 BUG_ON(ret);
4935 }
4936
4937 location.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
4938 location.offset = (u64)-1;
4939 location.type = BTRFS_ROOT_ITEM_KEY;
4940
4941 reloc_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
4942 BUG_ON(!reloc_root);
4943 btrfs_orphan_cleanup(reloc_root);
4944 return 0;
4945}
4946
Chris Masond3977122009-01-05 21:25:51 -05004947static noinline int init_reloc_tree(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04004948 struct btrfs_root *root)
4949{
4950 struct btrfs_root *reloc_root;
4951 struct extent_buffer *eb;
4952 struct btrfs_root_item *root_item;
4953 struct btrfs_key root_key;
4954 int ret;
4955
4956 BUG_ON(!root->ref_cows);
4957 if (root->reloc_root)
4958 return 0;
4959
4960 root_item = kmalloc(sizeof(*root_item), GFP_NOFS);
4961 BUG_ON(!root_item);
4962
4963 ret = btrfs_copy_root(trans, root, root->commit_root,
4964 &eb, BTRFS_TREE_RELOC_OBJECTID);
4965 BUG_ON(ret);
4966
4967 root_key.objectid = BTRFS_TREE_RELOC_OBJECTID;
4968 root_key.offset = root->root_key.objectid;
4969 root_key.type = BTRFS_ROOT_ITEM_KEY;
4970
4971 memcpy(root_item, &root->root_item, sizeof(root_item));
4972 btrfs_set_root_refs(root_item, 0);
4973 btrfs_set_root_bytenr(root_item, eb->start);
4974 btrfs_set_root_level(root_item, btrfs_header_level(eb));
Yan Zheng84234f32008-10-29 14:49:05 -04004975 btrfs_set_root_generation(root_item, trans->transid);
Zheng Yan1a40e232008-09-26 10:09:34 -04004976
4977 btrfs_tree_unlock(eb);
4978 free_extent_buffer(eb);
4979
4980 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
4981 &root_key, root_item);
4982 BUG_ON(ret);
4983 kfree(root_item);
4984
4985 reloc_root = btrfs_read_fs_root_no_radix(root->fs_info->tree_root,
4986 &root_key);
4987 BUG_ON(!reloc_root);
4988 reloc_root->last_trans = trans->transid;
4989 reloc_root->commit_root = NULL;
4990 reloc_root->ref_tree = &root->fs_info->reloc_ref_tree;
4991
4992 root->reloc_root = reloc_root;
4993 return 0;
4994}
4995
4996/*
4997 * Core function of space balance.
4998 *
4999 * The idea is using reloc trees to relocate tree blocks in reference
Yan Zhengf82d02d2008-10-29 14:49:05 -04005000 * counted roots. There is one reloc tree for each subvol, and all
5001 * reloc trees share same root key objectid. Reloc trees are snapshots
5002 * of the latest committed roots of subvols (root->commit_root).
5003 *
5004 * To relocate a tree block referenced by a subvol, there are two steps.
5005 * COW the block through subvol's reloc tree, then update block pointer
5006 * in the subvol to point to the new block. Since all reloc trees share
5007 * same root key objectid, doing special handing for tree blocks owned
5008 * by them is easy. Once a tree block has been COWed in one reloc tree,
5009 * we can use the resulting new block directly when the same block is
5010 * required to COW again through other reloc trees. By this way, relocated
5011 * tree blocks are shared between reloc trees, so they are also shared
5012 * between subvols.
Zheng Yan1a40e232008-09-26 10:09:34 -04005013 */
Chris Masond3977122009-01-05 21:25:51 -05005014static noinline int relocate_one_path(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04005015 struct btrfs_root *root,
5016 struct btrfs_path *path,
5017 struct btrfs_key *first_key,
5018 struct btrfs_ref_path *ref_path,
5019 struct btrfs_block_group_cache *group,
5020 struct inode *reloc_inode)
5021{
5022 struct btrfs_root *reloc_root;
5023 struct extent_buffer *eb = NULL;
5024 struct btrfs_key *keys;
5025 u64 *nodes;
5026 int level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005027 int shared_level;
Zheng Yan1a40e232008-09-26 10:09:34 -04005028 int lowest_level = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04005029 int ret;
5030
5031 if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
5032 lowest_level = ref_path->owner_objectid;
5033
Yan Zhengf82d02d2008-10-29 14:49:05 -04005034 if (!root->ref_cows) {
Zheng Yan1a40e232008-09-26 10:09:34 -04005035 path->lowest_level = lowest_level;
5036 ret = btrfs_search_slot(trans, root, first_key, path, 0, 1);
5037 BUG_ON(ret < 0);
5038 path->lowest_level = 0;
5039 btrfs_release_path(root, path);
5040 return 0;
5041 }
5042
Zheng Yan1a40e232008-09-26 10:09:34 -04005043 mutex_lock(&root->fs_info->tree_reloc_mutex);
5044 ret = init_reloc_tree(trans, root);
5045 BUG_ON(ret);
5046 reloc_root = root->reloc_root;
5047
Yan Zhengf82d02d2008-10-29 14:49:05 -04005048 shared_level = ref_path->shared_level;
5049 ref_path->shared_level = BTRFS_MAX_LEVEL - 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04005050
Yan Zhengf82d02d2008-10-29 14:49:05 -04005051 keys = ref_path->node_keys;
5052 nodes = ref_path->new_nodes;
5053 memset(&keys[shared_level + 1], 0,
5054 sizeof(*keys) * (BTRFS_MAX_LEVEL - shared_level - 1));
5055 memset(&nodes[shared_level + 1], 0,
5056 sizeof(*nodes) * (BTRFS_MAX_LEVEL - shared_level - 1));
Zheng Yan1a40e232008-09-26 10:09:34 -04005057
Yan Zhengf82d02d2008-10-29 14:49:05 -04005058 if (nodes[lowest_level] == 0) {
5059 path->lowest_level = lowest_level;
5060 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
5061 0, 1);
5062 BUG_ON(ret);
5063 for (level = lowest_level; level < BTRFS_MAX_LEVEL; level++) {
5064 eb = path->nodes[level];
5065 if (!eb || eb == reloc_root->node)
5066 break;
5067 nodes[level] = eb->start;
5068 if (level == 0)
5069 btrfs_item_key_to_cpu(eb, &keys[level], 0);
5070 else
5071 btrfs_node_key_to_cpu(eb, &keys[level], 0);
5072 }
Yan Zheng2b820322008-11-17 21:11:30 -05005073 if (nodes[0] &&
5074 ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04005075 eb = path->nodes[0];
5076 ret = replace_extents_in_leaf(trans, reloc_root, eb,
5077 group, reloc_inode);
5078 BUG_ON(ret);
5079 }
5080 btrfs_release_path(reloc_root, path);
5081 } else {
Zheng Yan1a40e232008-09-26 10:09:34 -04005082 ret = btrfs_merge_path(trans, reloc_root, keys, nodes,
Yan Zhengf82d02d2008-10-29 14:49:05 -04005083 lowest_level);
Zheng Yan1a40e232008-09-26 10:09:34 -04005084 BUG_ON(ret);
5085 }
5086
Zheng Yan1a40e232008-09-26 10:09:34 -04005087 /*
5088 * replace tree blocks in the fs tree with tree blocks in
5089 * the reloc tree.
5090 */
5091 ret = btrfs_merge_path(trans, root, keys, nodes, lowest_level);
5092 BUG_ON(ret < 0);
5093
5094 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04005095 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
5096 0, 0);
5097 BUG_ON(ret);
5098 extent_buffer_get(path->nodes[0]);
5099 eb = path->nodes[0];
5100 btrfs_release_path(reloc_root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04005101 ret = invalidate_extent_cache(reloc_root, eb, group, root);
5102 BUG_ON(ret);
5103 free_extent_buffer(eb);
5104 }
Zheng Yan1a40e232008-09-26 10:09:34 -04005105
Yan Zhengf82d02d2008-10-29 14:49:05 -04005106 mutex_unlock(&root->fs_info->tree_reloc_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04005107 path->lowest_level = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04005108 return 0;
5109}
5110
Chris Masond3977122009-01-05 21:25:51 -05005111static noinline int relocate_tree_block(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04005112 struct btrfs_root *root,
5113 struct btrfs_path *path,
5114 struct btrfs_key *first_key,
5115 struct btrfs_ref_path *ref_path)
5116{
5117 int ret;
Zheng Yan1a40e232008-09-26 10:09:34 -04005118
5119 ret = relocate_one_path(trans, root, path, first_key,
5120 ref_path, NULL, NULL);
5121 BUG_ON(ret);
5122
Zheng Yan1a40e232008-09-26 10:09:34 -04005123 return 0;
5124}
5125
Chris Masond3977122009-01-05 21:25:51 -05005126static noinline int del_extent_zero(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04005127 struct btrfs_root *extent_root,
5128 struct btrfs_path *path,
5129 struct btrfs_key *extent_key)
5130{
5131 int ret;
5132
Zheng Yan1a40e232008-09-26 10:09:34 -04005133 ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1);
5134 if (ret)
5135 goto out;
5136 ret = btrfs_del_item(trans, extent_root, path);
5137out:
Chris Masonedbd8d42007-12-21 16:27:24 -05005138 btrfs_release_path(extent_root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04005139 return ret;
5140}
5141
Chris Masond3977122009-01-05 21:25:51 -05005142static noinline struct btrfs_root *read_ref_root(struct btrfs_fs_info *fs_info,
Zheng Yan1a40e232008-09-26 10:09:34 -04005143 struct btrfs_ref_path *ref_path)
5144{
5145 struct btrfs_key root_key;
5146
5147 root_key.objectid = ref_path->root_objectid;
5148 root_key.type = BTRFS_ROOT_ITEM_KEY;
5149 if (is_cowonly_root(ref_path->root_objectid))
5150 root_key.offset = 0;
5151 else
5152 root_key.offset = (u64)-1;
5153
5154 return btrfs_read_fs_root_no_name(fs_info, &root_key);
5155}
5156
Chris Masond3977122009-01-05 21:25:51 -05005157static noinline int relocate_one_extent(struct btrfs_root *extent_root,
Zheng Yan1a40e232008-09-26 10:09:34 -04005158 struct btrfs_path *path,
5159 struct btrfs_key *extent_key,
5160 struct btrfs_block_group_cache *group,
5161 struct inode *reloc_inode, int pass)
5162{
5163 struct btrfs_trans_handle *trans;
5164 struct btrfs_root *found_root;
5165 struct btrfs_ref_path *ref_path = NULL;
5166 struct disk_extent *new_extents = NULL;
5167 int nr_extents = 0;
5168 int loops;
5169 int ret;
5170 int level;
5171 struct btrfs_key first_key;
5172 u64 prev_block = 0;
5173
Zheng Yan1a40e232008-09-26 10:09:34 -04005174
5175 trans = btrfs_start_transaction(extent_root, 1);
5176 BUG_ON(!trans);
5177
5178 if (extent_key->objectid == 0) {
5179 ret = del_extent_zero(trans, extent_root, path, extent_key);
5180 goto out;
5181 }
5182
5183 ref_path = kmalloc(sizeof(*ref_path), GFP_NOFS);
5184 if (!ref_path) {
Chris Masond3977122009-01-05 21:25:51 -05005185 ret = -ENOMEM;
5186 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04005187 }
5188
5189 for (loops = 0; ; loops++) {
5190 if (loops == 0) {
5191 ret = btrfs_first_ref_path(trans, extent_root, ref_path,
5192 extent_key->objectid);
5193 } else {
5194 ret = btrfs_next_ref_path(trans, extent_root, ref_path);
5195 }
5196 if (ret < 0)
5197 goto out;
5198 if (ret > 0)
5199 break;
5200
5201 if (ref_path->root_objectid == BTRFS_TREE_LOG_OBJECTID ||
5202 ref_path->root_objectid == BTRFS_TREE_RELOC_OBJECTID)
5203 continue;
5204
5205 found_root = read_ref_root(extent_root->fs_info, ref_path);
5206 BUG_ON(!found_root);
5207 /*
5208 * for reference counted tree, only process reference paths
5209 * rooted at the latest committed root.
5210 */
5211 if (found_root->ref_cows &&
5212 ref_path->root_generation != found_root->root_key.offset)
5213 continue;
5214
5215 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
5216 if (pass == 0) {
5217 /*
5218 * copy data extents to new locations
5219 */
5220 u64 group_start = group->key.objectid;
5221 ret = relocate_data_extent(reloc_inode,
5222 extent_key,
5223 group_start);
5224 if (ret < 0)
5225 goto out;
5226 break;
5227 }
5228 level = 0;
5229 } else {
5230 level = ref_path->owner_objectid;
5231 }
5232
5233 if (prev_block != ref_path->nodes[level]) {
5234 struct extent_buffer *eb;
5235 u64 block_start = ref_path->nodes[level];
5236 u64 block_size = btrfs_level_size(found_root, level);
5237
5238 eb = read_tree_block(found_root, block_start,
5239 block_size, 0);
5240 btrfs_tree_lock(eb);
5241 BUG_ON(level != btrfs_header_level(eb));
5242
5243 if (level == 0)
5244 btrfs_item_key_to_cpu(eb, &first_key, 0);
5245 else
5246 btrfs_node_key_to_cpu(eb, &first_key, 0);
5247
5248 btrfs_tree_unlock(eb);
5249 free_extent_buffer(eb);
5250 prev_block = block_start;
5251 }
5252
Yan Zheng24562422009-02-12 14:14:53 -05005253 mutex_lock(&extent_root->fs_info->trans_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05005254 btrfs_record_root_in_trans(found_root);
Yan Zheng24562422009-02-12 14:14:53 -05005255 mutex_unlock(&extent_root->fs_info->trans_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05005256 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
5257 /*
5258 * try to update data extent references while
5259 * keeping metadata shared between snapshots.
5260 */
5261 if (pass == 1) {
5262 ret = relocate_one_path(trans, found_root,
5263 path, &first_key, ref_path,
5264 group, reloc_inode);
5265 if (ret < 0)
5266 goto out;
5267 continue;
5268 }
Zheng Yan1a40e232008-09-26 10:09:34 -04005269 /*
5270 * use fallback method to process the remaining
5271 * references.
5272 */
5273 if (!new_extents) {
5274 u64 group_start = group->key.objectid;
Yan Zhengd899e052008-10-30 14:25:28 -04005275 new_extents = kmalloc(sizeof(*new_extents),
5276 GFP_NOFS);
5277 nr_extents = 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04005278 ret = get_new_locations(reloc_inode,
5279 extent_key,
Yan Zhengd899e052008-10-30 14:25:28 -04005280 group_start, 1,
Zheng Yan1a40e232008-09-26 10:09:34 -04005281 &new_extents,
5282 &nr_extents);
Yan Zhengd899e052008-10-30 14:25:28 -04005283 if (ret)
Zheng Yan1a40e232008-09-26 10:09:34 -04005284 goto out;
5285 }
Zheng Yan1a40e232008-09-26 10:09:34 -04005286 ret = replace_one_extent(trans, found_root,
5287 path, extent_key,
5288 &first_key, ref_path,
5289 new_extents, nr_extents);
Yan Zhenge4404d62008-12-12 10:03:26 -05005290 } else {
Zheng Yan1a40e232008-09-26 10:09:34 -04005291 ret = relocate_tree_block(trans, found_root, path,
5292 &first_key, ref_path);
Zheng Yan1a40e232008-09-26 10:09:34 -04005293 }
5294 if (ret < 0)
5295 goto out;
5296 }
5297 ret = 0;
5298out:
5299 btrfs_end_transaction(trans, extent_root);
5300 kfree(new_extents);
5301 kfree(ref_path);
Chris Masonedbd8d42007-12-21 16:27:24 -05005302 return ret;
5303}
5304
Chris Masonec44a352008-04-28 15:29:52 -04005305static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
5306{
5307 u64 num_devices;
5308 u64 stripped = BTRFS_BLOCK_GROUP_RAID0 |
5309 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
5310
Yan Zheng2b820322008-11-17 21:11:30 -05005311 num_devices = root->fs_info->fs_devices->rw_devices;
Chris Masonec44a352008-04-28 15:29:52 -04005312 if (num_devices == 1) {
5313 stripped |= BTRFS_BLOCK_GROUP_DUP;
5314 stripped = flags & ~stripped;
5315
5316 /* turn raid0 into single device chunks */
5317 if (flags & BTRFS_BLOCK_GROUP_RAID0)
5318 return stripped;
5319
5320 /* turn mirroring into duplication */
5321 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
5322 BTRFS_BLOCK_GROUP_RAID10))
5323 return stripped | BTRFS_BLOCK_GROUP_DUP;
5324 return flags;
5325 } else {
5326 /* they already had raid on here, just return */
Chris Masonec44a352008-04-28 15:29:52 -04005327 if (flags & stripped)
5328 return flags;
5329
5330 stripped |= BTRFS_BLOCK_GROUP_DUP;
5331 stripped = flags & ~stripped;
5332
5333 /* switch duplicated blocks with raid1 */
5334 if (flags & BTRFS_BLOCK_GROUP_DUP)
5335 return stripped | BTRFS_BLOCK_GROUP_RAID1;
5336
5337 /* turn single device chunks into raid0 */
5338 return stripped | BTRFS_BLOCK_GROUP_RAID0;
5339 }
5340 return flags;
5341}
5342
Christoph Hellwigb2950862008-12-02 09:54:17 -05005343static int __alloc_chunk_for_shrink(struct btrfs_root *root,
Chris Mason0ef3e662008-05-24 14:04:53 -04005344 struct btrfs_block_group_cache *shrink_block_group,
5345 int force)
5346{
5347 struct btrfs_trans_handle *trans;
5348 u64 new_alloc_flags;
5349 u64 calc;
5350
Chris Masonc286ac42008-07-22 23:06:41 -04005351 spin_lock(&shrink_block_group->lock);
Chris Mason0ef3e662008-05-24 14:04:53 -04005352 if (btrfs_block_group_used(&shrink_block_group->item) > 0) {
Chris Masonc286ac42008-07-22 23:06:41 -04005353 spin_unlock(&shrink_block_group->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04005354
Chris Mason0ef3e662008-05-24 14:04:53 -04005355 trans = btrfs_start_transaction(root, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04005356 spin_lock(&shrink_block_group->lock);
Chris Mason7d9eb122008-07-08 14:19:17 -04005357
Chris Mason0ef3e662008-05-24 14:04:53 -04005358 new_alloc_flags = update_block_group_flags(root,
5359 shrink_block_group->flags);
5360 if (new_alloc_flags != shrink_block_group->flags) {
5361 calc =
5362 btrfs_block_group_used(&shrink_block_group->item);
5363 } else {
5364 calc = shrink_block_group->key.offset;
5365 }
Chris Masonc286ac42008-07-22 23:06:41 -04005366 spin_unlock(&shrink_block_group->lock);
5367
Chris Mason0ef3e662008-05-24 14:04:53 -04005368 do_chunk_alloc(trans, root->fs_info->extent_root,
5369 calc + 2 * 1024 * 1024, new_alloc_flags, force);
Chris Mason7d9eb122008-07-08 14:19:17 -04005370
Chris Mason0ef3e662008-05-24 14:04:53 -04005371 btrfs_end_transaction(trans, root);
Chris Masonc286ac42008-07-22 23:06:41 -04005372 } else
5373 spin_unlock(&shrink_block_group->lock);
Chris Mason0ef3e662008-05-24 14:04:53 -04005374 return 0;
5375}
5376
Zheng Yan1a40e232008-09-26 10:09:34 -04005377static int __insert_orphan_inode(struct btrfs_trans_handle *trans,
5378 struct btrfs_root *root,
5379 u64 objectid, u64 size)
5380{
5381 struct btrfs_path *path;
5382 struct btrfs_inode_item *item;
5383 struct extent_buffer *leaf;
5384 int ret;
5385
5386 path = btrfs_alloc_path();
5387 if (!path)
5388 return -ENOMEM;
5389
5390 ret = btrfs_insert_empty_inode(trans, root, path, objectid);
5391 if (ret)
5392 goto out;
5393
5394 leaf = path->nodes[0];
5395 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_inode_item);
5396 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
5397 btrfs_set_inode_generation(leaf, item, 1);
5398 btrfs_set_inode_size(leaf, item, size);
5399 btrfs_set_inode_mode(leaf, item, S_IFREG | 0600);
Yan Zheng0403e472008-12-10 20:32:51 -05005400 btrfs_set_inode_flags(leaf, item, BTRFS_INODE_NOCOMPRESS);
Zheng Yan1a40e232008-09-26 10:09:34 -04005401 btrfs_mark_buffer_dirty(leaf);
5402 btrfs_release_path(root, path);
5403out:
5404 btrfs_free_path(path);
5405 return ret;
5406}
5407
Chris Masond3977122009-01-05 21:25:51 -05005408static noinline struct inode *create_reloc_inode(struct btrfs_fs_info *fs_info,
Zheng Yan1a40e232008-09-26 10:09:34 -04005409 struct btrfs_block_group_cache *group)
5410{
5411 struct inode *inode = NULL;
5412 struct btrfs_trans_handle *trans;
5413 struct btrfs_root *root;
5414 struct btrfs_key root_key;
5415 u64 objectid = BTRFS_FIRST_FREE_OBJECTID;
5416 int err = 0;
5417
5418 root_key.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
5419 root_key.type = BTRFS_ROOT_ITEM_KEY;
5420 root_key.offset = (u64)-1;
5421 root = btrfs_read_fs_root_no_name(fs_info, &root_key);
5422 if (IS_ERR(root))
5423 return ERR_CAST(root);
5424
5425 trans = btrfs_start_transaction(root, 1);
5426 BUG_ON(!trans);
5427
5428 err = btrfs_find_free_objectid(trans, root, objectid, &objectid);
5429 if (err)
5430 goto out;
5431
5432 err = __insert_orphan_inode(trans, root, objectid, group->key.offset);
5433 BUG_ON(err);
5434
5435 err = btrfs_insert_file_extent(trans, root, objectid, 0, 0, 0,
Chris Masonc8b97812008-10-29 14:49:59 -04005436 group->key.offset, 0, group->key.offset,
5437 0, 0, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04005438 BUG_ON(err);
5439
5440 inode = btrfs_iget_locked(root->fs_info->sb, objectid, root);
5441 if (inode->i_state & I_NEW) {
5442 BTRFS_I(inode)->root = root;
5443 BTRFS_I(inode)->location.objectid = objectid;
5444 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
5445 BTRFS_I(inode)->location.offset = 0;
5446 btrfs_read_locked_inode(inode);
5447 unlock_new_inode(inode);
5448 BUG_ON(is_bad_inode(inode));
5449 } else {
5450 BUG_ON(1);
5451 }
Yan Zheng17d217f2008-12-12 10:03:38 -05005452 BTRFS_I(inode)->index_cnt = group->key.objectid;
Zheng Yan1a40e232008-09-26 10:09:34 -04005453
5454 err = btrfs_orphan_add(trans, inode);
5455out:
5456 btrfs_end_transaction(trans, root);
5457 if (err) {
5458 if (inode)
5459 iput(inode);
5460 inode = ERR_PTR(err);
5461 }
5462 return inode;
5463}
5464
Yan Zheng17d217f2008-12-12 10:03:38 -05005465int btrfs_reloc_clone_csums(struct inode *inode, u64 file_pos, u64 len)
5466{
5467
5468 struct btrfs_ordered_sum *sums;
5469 struct btrfs_sector_sum *sector_sum;
5470 struct btrfs_ordered_extent *ordered;
5471 struct btrfs_root *root = BTRFS_I(inode)->root;
5472 struct list_head list;
5473 size_t offset;
5474 int ret;
5475 u64 disk_bytenr;
5476
5477 INIT_LIST_HEAD(&list);
5478
5479 ordered = btrfs_lookup_ordered_extent(inode, file_pos);
5480 BUG_ON(ordered->file_offset != file_pos || ordered->len != len);
5481
5482 disk_bytenr = file_pos + BTRFS_I(inode)->index_cnt;
Yan Zheng07d400a2009-01-06 11:42:00 -05005483 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, disk_bytenr,
Yan Zheng17d217f2008-12-12 10:03:38 -05005484 disk_bytenr + len - 1, &list);
5485
5486 while (!list_empty(&list)) {
5487 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
5488 list_del_init(&sums->list);
5489
5490 sector_sum = sums->sums;
5491 sums->bytenr = ordered->start;
5492
5493 offset = 0;
5494 while (offset < sums->len) {
5495 sector_sum->bytenr += ordered->start - disk_bytenr;
5496 sector_sum++;
5497 offset += root->sectorsize;
5498 }
5499
5500 btrfs_add_ordered_sum(inode, ordered, sums);
5501 }
5502 btrfs_put_ordered_extent(ordered);
5503 return 0;
5504}
5505
Zheng Yan1a40e232008-09-26 10:09:34 -04005506int btrfs_relocate_block_group(struct btrfs_root *root, u64 group_start)
Chris Masonedbd8d42007-12-21 16:27:24 -05005507{
5508 struct btrfs_trans_handle *trans;
Chris Masonedbd8d42007-12-21 16:27:24 -05005509 struct btrfs_path *path;
Zheng Yan1a40e232008-09-26 10:09:34 -04005510 struct btrfs_fs_info *info = root->fs_info;
5511 struct extent_buffer *leaf;
5512 struct inode *reloc_inode;
5513 struct btrfs_block_group_cache *block_group;
5514 struct btrfs_key key;
Yan Zhengd899e052008-10-30 14:25:28 -04005515 u64 skipped;
Chris Masonedbd8d42007-12-21 16:27:24 -05005516 u64 cur_byte;
5517 u64 total_found;
Chris Masonedbd8d42007-12-21 16:27:24 -05005518 u32 nritems;
5519 int ret;
Chris Masona061fc82008-05-07 11:43:44 -04005520 int progress;
Zheng Yan1a40e232008-09-26 10:09:34 -04005521 int pass = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05005522
Chris Masonedbd8d42007-12-21 16:27:24 -05005523 root = root->fs_info->extent_root;
Zheng Yan1a40e232008-09-26 10:09:34 -04005524
5525 block_group = btrfs_lookup_block_group(info, group_start);
5526 BUG_ON(!block_group);
Chris Masonedbd8d42007-12-21 16:27:24 -05005527
Chris Masond3977122009-01-05 21:25:51 -05005528 printk(KERN_INFO "btrfs relocating block group %llu flags %llu\n",
Zheng Yan1a40e232008-09-26 10:09:34 -04005529 (unsigned long long)block_group->key.objectid,
5530 (unsigned long long)block_group->flags);
Chris Mason323da792008-05-09 11:46:48 -04005531
Zheng Yan1a40e232008-09-26 10:09:34 -04005532 path = btrfs_alloc_path();
5533 BUG_ON(!path);
Chris Mason323da792008-05-09 11:46:48 -04005534
Zheng Yan1a40e232008-09-26 10:09:34 -04005535 reloc_inode = create_reloc_inode(info, block_group);
5536 BUG_ON(IS_ERR(reloc_inode));
5537
Zheng Yan1a40e232008-09-26 10:09:34 -04005538 __alloc_chunk_for_shrink(root, block_group, 1);
Yan Zhengc146afa2008-11-12 14:34:12 -05005539 set_block_group_readonly(block_group);
Zheng Yan1a40e232008-09-26 10:09:34 -04005540
Zheng Yan1a40e232008-09-26 10:09:34 -04005541 btrfs_start_delalloc_inodes(info->tree_root);
5542 btrfs_wait_ordered_extents(info->tree_root, 0);
Chris Mason0ef3e662008-05-24 14:04:53 -04005543again:
Yan Zhengd899e052008-10-30 14:25:28 -04005544 skipped = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05005545 total_found = 0;
Chris Masona061fc82008-05-07 11:43:44 -04005546 progress = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04005547 key.objectid = block_group->key.objectid;
Chris Masonedbd8d42007-12-21 16:27:24 -05005548 key.offset = 0;
5549 key.type = 0;
Yan73e48b22008-01-03 14:14:39 -05005550 cur_byte = key.objectid;
Chris Mason4313b392008-01-03 09:08:48 -05005551
Zheng Yan1a40e232008-09-26 10:09:34 -04005552 trans = btrfs_start_transaction(info->tree_root, 1);
5553 btrfs_commit_transaction(trans, info->tree_root);
Chris Masonea8c2812008-08-04 23:17:27 -04005554
Zheng Yan1a40e232008-09-26 10:09:34 -04005555 mutex_lock(&root->fs_info->cleaner_mutex);
5556 btrfs_clean_old_snapshots(info->tree_root);
5557 btrfs_remove_leaf_refs(info->tree_root, (u64)-1, 1);
5558 mutex_unlock(&root->fs_info->cleaner_mutex);
Chris Masonea8c2812008-08-04 23:17:27 -04005559
Chris Mason56bec292009-03-13 10:10:06 -04005560 trans = btrfs_start_transaction(info->tree_root, 1);
5561 btrfs_commit_transaction(trans, info->tree_root);
5562
Chris Masond3977122009-01-05 21:25:51 -05005563 while (1) {
Chris Masonedbd8d42007-12-21 16:27:24 -05005564 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5565 if (ret < 0)
5566 goto out;
Chris Mason7d9eb122008-07-08 14:19:17 -04005567next:
Chris Masonedbd8d42007-12-21 16:27:24 -05005568 leaf = path->nodes[0];
Chris Masonedbd8d42007-12-21 16:27:24 -05005569 nritems = btrfs_header_nritems(leaf);
Yan73e48b22008-01-03 14:14:39 -05005570 if (path->slots[0] >= nritems) {
5571 ret = btrfs_next_leaf(root, path);
5572 if (ret < 0)
5573 goto out;
5574 if (ret == 1) {
5575 ret = 0;
5576 break;
Chris Masonedbd8d42007-12-21 16:27:24 -05005577 }
Yan73e48b22008-01-03 14:14:39 -05005578 leaf = path->nodes[0];
5579 nritems = btrfs_header_nritems(leaf);
5580 }
5581
Zheng Yan1a40e232008-09-26 10:09:34 -04005582 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Chris Mason725c8462008-01-04 16:47:16 -05005583
Zheng Yan1a40e232008-09-26 10:09:34 -04005584 if (key.objectid >= block_group->key.objectid +
5585 block_group->key.offset)
Chris Mason8f18cf12008-04-25 16:53:30 -04005586 break;
5587
Chris Mason725c8462008-01-04 16:47:16 -05005588 if (progress && need_resched()) {
Chris Mason725c8462008-01-04 16:47:16 -05005589 btrfs_release_path(root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04005590 cond_resched();
Chris Mason725c8462008-01-04 16:47:16 -05005591 progress = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04005592 continue;
Chris Mason725c8462008-01-04 16:47:16 -05005593 }
5594 progress = 1;
5595
Zheng Yan1a40e232008-09-26 10:09:34 -04005596 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY ||
5597 key.objectid + key.offset <= cur_byte) {
Yan73e48b22008-01-03 14:14:39 -05005598 path->slots[0]++;
Chris Masonedbd8d42007-12-21 16:27:24 -05005599 goto next;
5600 }
Yan73e48b22008-01-03 14:14:39 -05005601
Chris Masonedbd8d42007-12-21 16:27:24 -05005602 total_found++;
Zheng Yan1a40e232008-09-26 10:09:34 -04005603 cur_byte = key.objectid + key.offset;
Chris Masonedbd8d42007-12-21 16:27:24 -05005604 btrfs_release_path(root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04005605
5606 __alloc_chunk_for_shrink(root, block_group, 0);
5607 ret = relocate_one_extent(root, path, &key, block_group,
5608 reloc_inode, pass);
5609 BUG_ON(ret < 0);
Yan Zhengd899e052008-10-30 14:25:28 -04005610 if (ret > 0)
5611 skipped++;
Zheng Yan1a40e232008-09-26 10:09:34 -04005612
5613 key.objectid = cur_byte;
5614 key.type = 0;
5615 key.offset = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05005616 }
5617
5618 btrfs_release_path(root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04005619
5620 if (pass == 0) {
5621 btrfs_wait_ordered_range(reloc_inode, 0, (u64)-1);
5622 invalidate_mapping_pages(reloc_inode->i_mapping, 0, -1);
Zheng Yan1a40e232008-09-26 10:09:34 -04005623 }
Chris Masonedbd8d42007-12-21 16:27:24 -05005624
5625 if (total_found > 0) {
Chris Masond3977122009-01-05 21:25:51 -05005626 printk(KERN_INFO "btrfs found %llu extents in pass %d\n",
Zheng Yan1a40e232008-09-26 10:09:34 -04005627 (unsigned long long)total_found, pass);
5628 pass++;
Yan Zhengd899e052008-10-30 14:25:28 -04005629 if (total_found == skipped && pass > 2) {
5630 iput(reloc_inode);
5631 reloc_inode = create_reloc_inode(info, block_group);
5632 pass = 0;
5633 }
Chris Masonedbd8d42007-12-21 16:27:24 -05005634 goto again;
5635 }
5636
Zheng Yan1a40e232008-09-26 10:09:34 -04005637 /* delete reloc_inode */
5638 iput(reloc_inode);
Chris Mason7d9eb122008-07-08 14:19:17 -04005639
Zheng Yan1a40e232008-09-26 10:09:34 -04005640 /* unpin extents in this range */
5641 trans = btrfs_start_transaction(info->tree_root, 1);
5642 btrfs_commit_transaction(trans, info->tree_root);
Chris Mason0ef3e662008-05-24 14:04:53 -04005643
Zheng Yan1a40e232008-09-26 10:09:34 -04005644 spin_lock(&block_group->lock);
5645 WARN_ON(block_group->pinned > 0);
5646 WARN_ON(block_group->reserved > 0);
5647 WARN_ON(btrfs_block_group_used(&block_group->item) > 0);
5648 spin_unlock(&block_group->lock);
Yan Zhengd2fb3432008-12-11 16:30:39 -05005649 put_block_group(block_group);
Zheng Yan1a40e232008-09-26 10:09:34 -04005650 ret = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05005651out:
Zheng Yan1a40e232008-09-26 10:09:34 -04005652 btrfs_free_path(path);
Chris Masonedbd8d42007-12-21 16:27:24 -05005653 return ret;
5654}
5655
Christoph Hellwigb2950862008-12-02 09:54:17 -05005656static int find_first_block_group(struct btrfs_root *root,
5657 struct btrfs_path *path, struct btrfs_key *key)
Chris Mason0b86a832008-03-24 15:01:56 -04005658{
Chris Mason925baed2008-06-25 16:01:30 -04005659 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005660 struct btrfs_key found_key;
5661 struct extent_buffer *leaf;
5662 int slot;
5663
5664 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
5665 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04005666 goto out;
5667
Chris Masond3977122009-01-05 21:25:51 -05005668 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04005669 slot = path->slots[0];
5670 leaf = path->nodes[0];
5671 if (slot >= btrfs_header_nritems(leaf)) {
5672 ret = btrfs_next_leaf(root, path);
5673 if (ret == 0)
5674 continue;
5675 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04005676 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04005677 break;
5678 }
5679 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5680
5681 if (found_key.objectid >= key->objectid &&
Chris Mason925baed2008-06-25 16:01:30 -04005682 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
5683 ret = 0;
5684 goto out;
5685 }
Chris Mason0b86a832008-03-24 15:01:56 -04005686 path->slots[0]++;
5687 }
5688 ret = -ENOENT;
Chris Mason925baed2008-06-25 16:01:30 -04005689out:
Chris Mason0b86a832008-03-24 15:01:56 -04005690 return ret;
5691}
5692
Zheng Yan1a40e232008-09-26 10:09:34 -04005693int btrfs_free_block_groups(struct btrfs_fs_info *info)
5694{
5695 struct btrfs_block_group_cache *block_group;
Chris Mason4184ea72009-03-10 12:39:20 -04005696 struct btrfs_space_info *space_info;
Zheng Yan1a40e232008-09-26 10:09:34 -04005697 struct rb_node *n;
5698
Zheng Yan1a40e232008-09-26 10:09:34 -04005699 spin_lock(&info->block_group_cache_lock);
5700 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
5701 block_group = rb_entry(n, struct btrfs_block_group_cache,
5702 cache_node);
Zheng Yan1a40e232008-09-26 10:09:34 -04005703 rb_erase(&block_group->cache_node,
5704 &info->block_group_cache_tree);
Yan Zhengd899e052008-10-30 14:25:28 -04005705 spin_unlock(&info->block_group_cache_lock);
5706
5707 btrfs_remove_free_space_cache(block_group);
Josef Bacik80eb2342008-10-29 14:49:05 -04005708 down_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04005709 list_del(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04005710 up_write(&block_group->space_info->groups_sem);
Yan Zhengd2fb3432008-12-11 16:30:39 -05005711
5712 WARN_ON(atomic_read(&block_group->count) != 1);
Zheng Yan1a40e232008-09-26 10:09:34 -04005713 kfree(block_group);
Yan Zhengd899e052008-10-30 14:25:28 -04005714
5715 spin_lock(&info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04005716 }
5717 spin_unlock(&info->block_group_cache_lock);
Chris Mason4184ea72009-03-10 12:39:20 -04005718
5719 /* now that all the block groups are freed, go through and
5720 * free all the space_info structs. This is only called during
5721 * the final stages of unmount, and so we know nobody is
5722 * using them. We call synchronize_rcu() once before we start,
5723 * just to be on the safe side.
5724 */
5725 synchronize_rcu();
5726
5727 while(!list_empty(&info->space_info)) {
5728 space_info = list_entry(info->space_info.next,
5729 struct btrfs_space_info,
5730 list);
5731
5732 list_del(&space_info->list);
5733 kfree(space_info);
5734 }
Zheng Yan1a40e232008-09-26 10:09:34 -04005735 return 0;
5736}
5737
Chris Mason9078a3e2007-04-26 16:46:15 -04005738int btrfs_read_block_groups(struct btrfs_root *root)
5739{
5740 struct btrfs_path *path;
5741 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04005742 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04005743 struct btrfs_fs_info *info = root->fs_info;
Chris Mason6324fbf2008-03-24 15:01:59 -04005744 struct btrfs_space_info *space_info;
Chris Mason9078a3e2007-04-26 16:46:15 -04005745 struct btrfs_key key;
5746 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -04005747 struct extent_buffer *leaf;
Chris Mason96b51792007-10-15 16:15:19 -04005748
Chris Masonbe744172007-05-06 10:15:01 -04005749 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04005750 key.objectid = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005751 key.offset = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04005752 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
Chris Mason9078a3e2007-04-26 16:46:15 -04005753 path = btrfs_alloc_path();
5754 if (!path)
5755 return -ENOMEM;
5756
Chris Masond3977122009-01-05 21:25:51 -05005757 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04005758 ret = find_first_block_group(root, path, &key);
5759 if (ret > 0) {
5760 ret = 0;
5761 goto error;
Chris Mason9078a3e2007-04-26 16:46:15 -04005762 }
Chris Mason0b86a832008-03-24 15:01:56 -04005763 if (ret != 0)
5764 goto error;
5765
Chris Mason5f39d392007-10-15 16:14:19 -04005766 leaf = path->nodes[0];
5767 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason8f18cf12008-04-25 16:53:30 -04005768 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -04005769 if (!cache) {
Chris Mason0b86a832008-03-24 15:01:56 -04005770 ret = -ENOMEM;
Chris Mason9078a3e2007-04-26 16:46:15 -04005771 break;
5772 }
Chris Mason3e1ad542007-05-07 20:03:49 -04005773
Yan Zhengd2fb3432008-12-11 16:30:39 -05005774 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04005775 spin_lock_init(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04005776 mutex_init(&cache->alloc_mutex);
Josef Bacikea6a4782008-11-20 12:16:16 -05005777 mutex_init(&cache->cache_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005778 INIT_LIST_HEAD(&cache->list);
Chris Mason5f39d392007-10-15 16:14:19 -04005779 read_extent_buffer(leaf, &cache->item,
5780 btrfs_item_ptr_offset(leaf, path->slots[0]),
5781 sizeof(cache->item));
Chris Mason9078a3e2007-04-26 16:46:15 -04005782 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Mason0b86a832008-03-24 15:01:56 -04005783
Chris Mason9078a3e2007-04-26 16:46:15 -04005784 key.objectid = found_key.objectid + found_key.offset;
5785 btrfs_release_path(root, path);
Chris Mason0b86a832008-03-24 15:01:56 -04005786 cache->flags = btrfs_block_group_flags(&cache->item);
Chris Mason96b51792007-10-15 16:15:19 -04005787
Chris Mason6324fbf2008-03-24 15:01:59 -04005788 ret = update_space_info(info, cache->flags, found_key.offset,
5789 btrfs_block_group_used(&cache->item),
5790 &space_info);
5791 BUG_ON(ret);
5792 cache->space_info = space_info;
Josef Bacik80eb2342008-10-29 14:49:05 -04005793 down_write(&space_info->groups_sem);
5794 list_add_tail(&cache->list, &space_info->block_groups);
5795 up_write(&space_info->groups_sem);
Chris Mason6324fbf2008-03-24 15:01:59 -04005796
Josef Bacik0f9dd462008-09-23 13:14:11 -04005797 ret = btrfs_add_block_group_cache(root->fs_info, cache);
5798 BUG_ON(ret);
Chris Mason75ccf472008-09-30 19:24:06 -04005799
5800 set_avail_alloc_bits(root->fs_info, cache->flags);
Yan Zheng2b820322008-11-17 21:11:30 -05005801 if (btrfs_chunk_readonly(root, cache->key.objectid))
5802 set_block_group_readonly(cache);
Chris Mason9078a3e2007-04-26 16:46:15 -04005803 }
Chris Mason0b86a832008-03-24 15:01:56 -04005804 ret = 0;
5805error:
Chris Mason9078a3e2007-04-26 16:46:15 -04005806 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04005807 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04005808}
Chris Mason6324fbf2008-03-24 15:01:59 -04005809
5810int btrfs_make_block_group(struct btrfs_trans_handle *trans,
5811 struct btrfs_root *root, u64 bytes_used,
Chris Masone17cade2008-04-15 15:41:47 -04005812 u64 type, u64 chunk_objectid, u64 chunk_offset,
Chris Mason6324fbf2008-03-24 15:01:59 -04005813 u64 size)
5814{
5815 int ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04005816 struct btrfs_root *extent_root;
5817 struct btrfs_block_group_cache *cache;
Chris Mason6324fbf2008-03-24 15:01:59 -04005818
5819 extent_root = root->fs_info->extent_root;
Chris Mason6324fbf2008-03-24 15:01:59 -04005820
Chris Masone02119d2008-09-05 16:13:11 -04005821 root->fs_info->last_trans_new_blockgroup = trans->transid;
5822
Chris Mason8f18cf12008-04-25 16:53:30 -04005823 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005824 if (!cache)
5825 return -ENOMEM;
5826
Chris Masone17cade2008-04-15 15:41:47 -04005827 cache->key.objectid = chunk_offset;
Chris Mason6324fbf2008-03-24 15:01:59 -04005828 cache->key.offset = size;
Yan Zhengd2fb3432008-12-11 16:30:39 -05005829 cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
5830 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04005831 spin_lock_init(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04005832 mutex_init(&cache->alloc_mutex);
Josef Bacikea6a4782008-11-20 12:16:16 -05005833 mutex_init(&cache->cache_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005834 INIT_LIST_HEAD(&cache->list);
Chris Mason0ef3e662008-05-24 14:04:53 -04005835
Chris Mason6324fbf2008-03-24 15:01:59 -04005836 btrfs_set_block_group_used(&cache->item, bytes_used);
Chris Mason6324fbf2008-03-24 15:01:59 -04005837 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
5838 cache->flags = type;
5839 btrfs_set_block_group_flags(&cache->item, type);
5840
5841 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
5842 &cache->space_info);
5843 BUG_ON(ret);
Josef Bacik80eb2342008-10-29 14:49:05 -04005844 down_write(&cache->space_info->groups_sem);
5845 list_add_tail(&cache->list, &cache->space_info->block_groups);
5846 up_write(&cache->space_info->groups_sem);
Chris Mason6324fbf2008-03-24 15:01:59 -04005847
Josef Bacik0f9dd462008-09-23 13:14:11 -04005848 ret = btrfs_add_block_group_cache(root->fs_info, cache);
5849 BUG_ON(ret);
Chris Masonc286ac42008-07-22 23:06:41 -04005850
Chris Mason6324fbf2008-03-24 15:01:59 -04005851 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
5852 sizeof(cache->item));
5853 BUG_ON(ret);
5854
Chris Masond18a2c42008-04-04 15:40:00 -04005855 set_avail_alloc_bits(extent_root->fs_info, type);
Chris Mason925baed2008-06-25 16:01:30 -04005856
Chris Mason6324fbf2008-03-24 15:01:59 -04005857 return 0;
5858}
Zheng Yan1a40e232008-09-26 10:09:34 -04005859
5860int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
5861 struct btrfs_root *root, u64 group_start)
5862{
5863 struct btrfs_path *path;
5864 struct btrfs_block_group_cache *block_group;
5865 struct btrfs_key key;
5866 int ret;
5867
Zheng Yan1a40e232008-09-26 10:09:34 -04005868 root = root->fs_info->extent_root;
5869
5870 block_group = btrfs_lookup_block_group(root->fs_info, group_start);
5871 BUG_ON(!block_group);
Yan Zhengc146afa2008-11-12 14:34:12 -05005872 BUG_ON(!block_group->ro);
Zheng Yan1a40e232008-09-26 10:09:34 -04005873
5874 memcpy(&key, &block_group->key, sizeof(key));
5875
5876 path = btrfs_alloc_path();
5877 BUG_ON(!path);
5878
Yan Zheng3dfdb932009-01-21 10:49:16 -05005879 spin_lock(&root->fs_info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04005880 rb_erase(&block_group->cache_node,
5881 &root->fs_info->block_group_cache_tree);
Yan Zheng3dfdb932009-01-21 10:49:16 -05005882 spin_unlock(&root->fs_info->block_group_cache_lock);
5883 btrfs_remove_free_space_cache(block_group);
Josef Bacik80eb2342008-10-29 14:49:05 -04005884 down_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04005885 list_del(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04005886 up_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04005887
Yan Zhengc146afa2008-11-12 14:34:12 -05005888 spin_lock(&block_group->space_info->lock);
5889 block_group->space_info->total_bytes -= block_group->key.offset;
5890 block_group->space_info->bytes_readonly -= block_group->key.offset;
5891 spin_unlock(&block_group->space_info->lock);
Yan Zheng2b820322008-11-17 21:11:30 -05005892 block_group->space_info->full = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05005893
Yan Zhengd2fb3432008-12-11 16:30:39 -05005894 put_block_group(block_group);
5895 put_block_group(block_group);
Zheng Yan1a40e232008-09-26 10:09:34 -04005896
5897 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
5898 if (ret > 0)
5899 ret = -EIO;
5900 if (ret < 0)
5901 goto out;
5902
5903 ret = btrfs_del_item(trans, root, path);
5904out:
5905 btrfs_free_path(path);
5906 return ret;
5907}