blob: 9b5da2b013e41598f56f849701c78bde34255057 [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
1024 spin_lock(&delayed_refs->lock);
1025 }
1026 if (run_all) {
1027 spin_lock(&delayed_refs->lock);
1028 node = rb_first(&delayed_refs->root);
1029 if (!node) {
1030 spin_unlock(&delayed_refs->lock);
1031 goto out;
1032 }
1033
1034 while (node) {
1035 ref = rb_entry(node, struct btrfs_delayed_ref_node,
1036 rb_node);
1037 if (btrfs_delayed_ref_is_head(ref)) {
1038 struct btrfs_delayed_ref_head *head;
1039
1040 head = btrfs_delayed_node_to_head(ref);
1041 atomic_inc(&ref->refs);
1042
1043 spin_unlock(&delayed_refs->lock);
1044 mutex_lock(&head->mutex);
1045 mutex_unlock(&head->mutex);
1046
1047 btrfs_put_delayed_ref(ref);
1048 goto again;
1049 }
1050 node = rb_next(node);
1051 }
1052 spin_unlock(&delayed_refs->lock);
1053 count = (unsigned long)-1;
1054 schedule_timeout(1);
1055 goto again;
1056 }
Chris Mason54aa1f42007-06-22 14:16:25 -04001057out:
Chris Masona28ec192007-03-06 20:08:01 -05001058 return 0;
1059}
1060
Yan Zheng80ff3852008-10-30 14:20:02 -04001061int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
Yan Zheng17d217f2008-12-12 10:03:38 -05001062 struct btrfs_root *root, u64 objectid, u64 bytenr)
Chris Masonbe20aa92007-12-17 20:14:01 -05001063{
1064 struct btrfs_root *extent_root = root->fs_info->extent_root;
1065 struct btrfs_path *path;
Yan Zhengf321e492008-07-30 09:26:11 -04001066 struct extent_buffer *leaf;
1067 struct btrfs_extent_ref *ref_item;
Chris Masonbe20aa92007-12-17 20:14:01 -05001068 struct btrfs_key key;
1069 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001070 u64 ref_root;
1071 u64 last_snapshot;
Yan Zhengf321e492008-07-30 09:26:11 -04001072 u32 nritems;
1073 int ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001074
Chris Masonbe20aa92007-12-17 20:14:01 -05001075 key.objectid = bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -04001076 key.offset = (u64)-1;
Yan Zhengf321e492008-07-30 09:26:11 -04001077 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masonbe20aa92007-12-17 20:14:01 -05001078
Yan Zhengf321e492008-07-30 09:26:11 -04001079 path = btrfs_alloc_path();
Chris Masonbe20aa92007-12-17 20:14:01 -05001080 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
1081 if (ret < 0)
1082 goto out;
1083 BUG_ON(ret == 0);
Yan Zheng80ff3852008-10-30 14:20:02 -04001084
1085 ret = -ENOENT;
1086 if (path->slots[0] == 0)
Zheng Yan31840ae2008-09-23 13:14:14 -04001087 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05001088
Zheng Yan31840ae2008-09-23 13:14:14 -04001089 path->slots[0]--;
Yan Zhengf321e492008-07-30 09:26:11 -04001090 leaf = path->nodes[0];
1091 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001092
1093 if (found_key.objectid != bytenr ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001094 found_key.type != BTRFS_EXTENT_ITEM_KEY)
Chris Masonbe20aa92007-12-17 20:14:01 -05001095 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05001096
Yan Zheng80ff3852008-10-30 14:20:02 -04001097 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Chris Masonbe20aa92007-12-17 20:14:01 -05001098 while (1) {
Yan Zhengf321e492008-07-30 09:26:11 -04001099 leaf = path->nodes[0];
1100 nritems = btrfs_header_nritems(leaf);
Chris Masonbe20aa92007-12-17 20:14:01 -05001101 if (path->slots[0] >= nritems) {
1102 ret = btrfs_next_leaf(extent_root, path);
Yan Zhengf321e492008-07-30 09:26:11 -04001103 if (ret < 0)
1104 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05001105 if (ret == 0)
1106 continue;
1107 break;
1108 }
Yan Zhengf321e492008-07-30 09:26:11 -04001109 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001110 if (found_key.objectid != bytenr)
1111 break;
Chris Masonbd098352008-01-03 13:23:19 -05001112
Chris Masonbe20aa92007-12-17 20:14:01 -05001113 if (found_key.type != BTRFS_EXTENT_REF_KEY) {
1114 path->slots[0]++;
1115 continue;
1116 }
1117
Yan Zhengf321e492008-07-30 09:26:11 -04001118 ref_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masonbe20aa92007-12-17 20:14:01 -05001119 struct btrfs_extent_ref);
Yan Zheng80ff3852008-10-30 14:20:02 -04001120 ref_root = btrfs_ref_root(leaf, ref_item);
Yan Zheng17d217f2008-12-12 10:03:38 -05001121 if ((ref_root != root->root_key.objectid &&
1122 ref_root != BTRFS_TREE_LOG_OBJECTID) ||
1123 objectid != btrfs_ref_objectid(leaf, ref_item)) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001124 ret = 1;
1125 goto out;
Yan Zhengf321e492008-07-30 09:26:11 -04001126 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001127 if (btrfs_ref_generation(leaf, ref_item) <= last_snapshot) {
1128 ret = 1;
1129 goto out;
1130 }
Yan Zhengf321e492008-07-30 09:26:11 -04001131
Chris Masonbe20aa92007-12-17 20:14:01 -05001132 path->slots[0]++;
1133 }
Yan Zhengf321e492008-07-30 09:26:11 -04001134 ret = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001135out:
Yan Zhengf321e492008-07-30 09:26:11 -04001136 btrfs_free_path(path);
1137 return ret;
1138}
1139
Zheng Yan31840ae2008-09-23 13:14:14 -04001140int btrfs_cache_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1141 struct extent_buffer *buf, u32 nr_extents)
Chris Mason02217ed2007-03-02 16:08:05 -05001142{
Chris Mason5f39d392007-10-15 16:14:19 -04001143 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -04001144 struct btrfs_file_extent_item *fi;
Zheng Yane4657682008-09-26 10:04:53 -04001145 u64 root_gen;
1146 u32 nritems;
Chris Mason02217ed2007-03-02 16:08:05 -05001147 int i;
Chris Masondb945352007-10-15 16:15:53 -04001148 int level;
Zheng Yan31840ae2008-09-23 13:14:14 -04001149 int ret = 0;
Zheng Yane4657682008-09-26 10:04:53 -04001150 int shared = 0;
Chris Masona28ec192007-03-06 20:08:01 -05001151
Chris Mason3768f362007-03-13 16:47:54 -04001152 if (!root->ref_cows)
Chris Masona28ec192007-03-06 20:08:01 -05001153 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001154
Zheng Yane4657682008-09-26 10:04:53 -04001155 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
1156 shared = 0;
1157 root_gen = root->root_key.offset;
1158 } else {
1159 shared = 1;
1160 root_gen = trans->transid - 1;
1161 }
1162
Chris Masondb945352007-10-15 16:15:53 -04001163 level = btrfs_header_level(buf);
Chris Mason5f39d392007-10-15 16:14:19 -04001164 nritems = btrfs_header_nritems(buf);
Chris Mason4a096752008-07-21 10:29:44 -04001165
Zheng Yan31840ae2008-09-23 13:14:14 -04001166 if (level == 0) {
Yan Zheng31153d82008-07-28 15:32:19 -04001167 struct btrfs_leaf_ref *ref;
1168 struct btrfs_extent_info *info;
1169
Zheng Yan31840ae2008-09-23 13:14:14 -04001170 ref = btrfs_alloc_leaf_ref(root, nr_extents);
Yan Zheng31153d82008-07-28 15:32:19 -04001171 if (!ref) {
Zheng Yan31840ae2008-09-23 13:14:14 -04001172 ret = -ENOMEM;
Yan Zheng31153d82008-07-28 15:32:19 -04001173 goto out;
1174 }
1175
Zheng Yane4657682008-09-26 10:04:53 -04001176 ref->root_gen = root_gen;
Yan Zheng31153d82008-07-28 15:32:19 -04001177 ref->bytenr = buf->start;
1178 ref->owner = btrfs_header_owner(buf);
1179 ref->generation = btrfs_header_generation(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04001180 ref->nritems = nr_extents;
Yan Zheng31153d82008-07-28 15:32:19 -04001181 info = ref->extents;
Yanbcc63ab2008-07-30 16:29:20 -04001182
Zheng Yan31840ae2008-09-23 13:14:14 -04001183 for (i = 0; nr_extents > 0 && i < nritems; i++) {
Yan Zheng31153d82008-07-28 15:32:19 -04001184 u64 disk_bytenr;
1185 btrfs_item_key_to_cpu(buf, &key, i);
1186 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1187 continue;
1188 fi = btrfs_item_ptr(buf, i,
1189 struct btrfs_file_extent_item);
1190 if (btrfs_file_extent_type(buf, fi) ==
1191 BTRFS_FILE_EXTENT_INLINE)
1192 continue;
1193 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1194 if (disk_bytenr == 0)
1195 continue;
1196
1197 info->bytenr = disk_bytenr;
1198 info->num_bytes =
1199 btrfs_file_extent_disk_num_bytes(buf, fi);
1200 info->objectid = key.objectid;
1201 info->offset = key.offset;
1202 info++;
1203 }
1204
Zheng Yane4657682008-09-26 10:04:53 -04001205 ret = btrfs_add_leaf_ref(root, ref, shared);
Yan Zheng5b84e8d2008-10-09 11:46:19 -04001206 if (ret == -EEXIST && shared) {
1207 struct btrfs_leaf_ref *old;
1208 old = btrfs_lookup_leaf_ref(root, ref->bytenr);
1209 BUG_ON(!old);
1210 btrfs_remove_leaf_ref(root, old);
1211 btrfs_free_leaf_ref(root, old);
1212 ret = btrfs_add_leaf_ref(root, ref, shared);
1213 }
Yan Zheng31153d82008-07-28 15:32:19 -04001214 WARN_ON(ret);
Yanbcc63ab2008-07-30 16:29:20 -04001215 btrfs_free_leaf_ref(root, ref);
Yan Zheng31153d82008-07-28 15:32:19 -04001216 }
1217out:
Zheng Yan31840ae2008-09-23 13:14:14 -04001218 return ret;
1219}
1220
Chris Masonb7a9f292009-02-04 09:23:45 -05001221/* when a block goes through cow, we update the reference counts of
1222 * everything that block points to. The internal pointers of the block
1223 * can be in just about any order, and it is likely to have clusters of
1224 * things that are close together and clusters of things that are not.
1225 *
1226 * To help reduce the seeks that come with updating all of these reference
1227 * counts, sort them by byte number before actual updates are done.
1228 *
1229 * struct refsort is used to match byte number to slot in the btree block.
1230 * we sort based on the byte number and then use the slot to actually
1231 * find the item.
Chris Masonbd56b302009-02-04 09:27:02 -05001232 *
1233 * struct refsort is smaller than strcut btrfs_item and smaller than
1234 * struct btrfs_key_ptr. Since we're currently limited to the page size
1235 * for a btree block, there's no way for a kmalloc of refsorts for a
1236 * single node to be bigger than a page.
Chris Masonb7a9f292009-02-04 09:23:45 -05001237 */
1238struct refsort {
1239 u64 bytenr;
1240 u32 slot;
1241};
1242
1243/*
1244 * for passing into sort()
1245 */
1246static int refsort_cmp(const void *a_void, const void *b_void)
1247{
1248 const struct refsort *a = a_void;
1249 const struct refsort *b = b_void;
1250
1251 if (a->bytenr < b->bytenr)
1252 return -1;
1253 if (a->bytenr > b->bytenr)
1254 return 1;
1255 return 0;
1256}
1257
1258
1259noinline int btrfs_inc_ref(struct btrfs_trans_handle *trans,
1260 struct btrfs_root *root,
1261 struct extent_buffer *orig_buf,
1262 struct extent_buffer *buf, u32 *nr_extents)
Zheng Yan31840ae2008-09-23 13:14:14 -04001263{
1264 u64 bytenr;
1265 u64 ref_root;
1266 u64 orig_root;
1267 u64 ref_generation;
1268 u64 orig_generation;
Chris Masonb7a9f292009-02-04 09:23:45 -05001269 struct refsort *sorted;
Zheng Yan31840ae2008-09-23 13:14:14 -04001270 u32 nritems;
1271 u32 nr_file_extents = 0;
1272 struct btrfs_key key;
1273 struct btrfs_file_extent_item *fi;
1274 int i;
1275 int level;
1276 int ret = 0;
1277 int faili = 0;
Chris Masonb7a9f292009-02-04 09:23:45 -05001278 int refi = 0;
1279 int slot;
Zheng Yan31840ae2008-09-23 13:14:14 -04001280 int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
Chris Mason56bec292009-03-13 10:10:06 -04001281 u64, u64, u64, u64, u64, u64, u64, u64, u64);
Zheng Yan31840ae2008-09-23 13:14:14 -04001282
1283 ref_root = btrfs_header_owner(buf);
1284 ref_generation = btrfs_header_generation(buf);
1285 orig_root = btrfs_header_owner(orig_buf);
1286 orig_generation = btrfs_header_generation(orig_buf);
1287
1288 nritems = btrfs_header_nritems(buf);
1289 level = btrfs_header_level(buf);
1290
Chris Masonb7a9f292009-02-04 09:23:45 -05001291 sorted = kmalloc(sizeof(struct refsort) * nritems, GFP_NOFS);
1292 BUG_ON(!sorted);
1293
Zheng Yan31840ae2008-09-23 13:14:14 -04001294 if (root->ref_cows) {
1295 process_func = __btrfs_inc_extent_ref;
1296 } else {
1297 if (level == 0 &&
1298 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
1299 goto out;
1300 if (level != 0 &&
1301 root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID)
1302 goto out;
1303 process_func = __btrfs_update_extent_ref;
1304 }
1305
Chris Masonb7a9f292009-02-04 09:23:45 -05001306 /*
1307 * we make two passes through the items. In the first pass we
1308 * only record the byte number and slot. Then we sort based on
1309 * byte number and do the actual work based on the sorted results
1310 */
Zheng Yan31840ae2008-09-23 13:14:14 -04001311 for (i = 0; i < nritems; i++) {
1312 cond_resched();
Chris Masondb945352007-10-15 16:15:53 -04001313 if (level == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001314 btrfs_item_key_to_cpu(buf, &key, i);
1315 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -04001316 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04001317 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -04001318 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001319 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -04001320 BTRFS_FILE_EXTENT_INLINE)
1321 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04001322 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1323 if (bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -04001324 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04001325
1326 nr_file_extents++;
Chris Masonb7a9f292009-02-04 09:23:45 -05001327 sorted[refi].bytenr = bytenr;
1328 sorted[refi].slot = i;
1329 refi++;
1330 } else {
1331 bytenr = btrfs_node_blockptr(buf, i);
1332 sorted[refi].bytenr = bytenr;
1333 sorted[refi].slot = i;
1334 refi++;
1335 }
1336 }
1337 /*
1338 * if refi == 0, we didn't actually put anything into the sorted
1339 * array and we're done
1340 */
1341 if (refi == 0)
1342 goto out;
1343
1344 sort(sorted, refi, sizeof(struct refsort), refsort_cmp, NULL);
1345
1346 for (i = 0; i < refi; i++) {
1347 cond_resched();
1348 slot = sorted[i].slot;
1349 bytenr = sorted[i].bytenr;
1350
1351 if (level == 0) {
1352 btrfs_item_key_to_cpu(buf, &key, slot);
Chris Mason56bec292009-03-13 10:10:06 -04001353 fi = btrfs_item_ptr(buf, slot,
1354 struct btrfs_file_extent_item);
1355
1356 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1357 if (bytenr == 0)
1358 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04001359
Zheng Yan31840ae2008-09-23 13:14:14 -04001360 ret = process_func(trans, root, bytenr,
Chris Mason56bec292009-03-13 10:10:06 -04001361 btrfs_file_extent_disk_num_bytes(buf, fi),
1362 orig_buf->start, buf->start,
1363 orig_root, ref_root,
1364 orig_generation, ref_generation,
1365 key.objectid);
Zheng Yan31840ae2008-09-23 13:14:14 -04001366
1367 if (ret) {
Chris Masonb7a9f292009-02-04 09:23:45 -05001368 faili = slot;
Zheng Yan31840ae2008-09-23 13:14:14 -04001369 WARN_ON(1);
1370 goto fail;
1371 }
Chris Mason54aa1f42007-06-22 14:16:25 -04001372 } else {
Chris Mason56bec292009-03-13 10:10:06 -04001373 ret = process_func(trans, root, bytenr, buf->len,
Zheng Yan31840ae2008-09-23 13:14:14 -04001374 orig_buf->start, buf->start,
1375 orig_root, ref_root,
1376 orig_generation, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001377 level - 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04001378 if (ret) {
Chris Masonb7a9f292009-02-04 09:23:45 -05001379 faili = slot;
Zheng Yan31840ae2008-09-23 13:14:14 -04001380 WARN_ON(1);
1381 goto fail;
1382 }
Chris Mason54aa1f42007-06-22 14:16:25 -04001383 }
1384 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001385out:
Chris Masonb7a9f292009-02-04 09:23:45 -05001386 kfree(sorted);
Zheng Yan31840ae2008-09-23 13:14:14 -04001387 if (nr_extents) {
1388 if (level == 0)
1389 *nr_extents = nr_file_extents;
1390 else
1391 *nr_extents = nritems;
1392 }
1393 return 0;
1394fail:
Chris Masonb7a9f292009-02-04 09:23:45 -05001395 kfree(sorted);
Zheng Yan31840ae2008-09-23 13:14:14 -04001396 WARN_ON(1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001397 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05001398}
1399
Zheng Yan31840ae2008-09-23 13:14:14 -04001400int btrfs_update_ref(struct btrfs_trans_handle *trans,
1401 struct btrfs_root *root, struct extent_buffer *orig_buf,
1402 struct extent_buffer *buf, int start_slot, int nr)
1403
1404{
1405 u64 bytenr;
1406 u64 ref_root;
1407 u64 orig_root;
1408 u64 ref_generation;
1409 u64 orig_generation;
1410 struct btrfs_key key;
1411 struct btrfs_file_extent_item *fi;
1412 int i;
1413 int ret;
1414 int slot;
1415 int level;
1416
1417 BUG_ON(start_slot < 0);
1418 BUG_ON(start_slot + nr > btrfs_header_nritems(buf));
1419
1420 ref_root = btrfs_header_owner(buf);
1421 ref_generation = btrfs_header_generation(buf);
1422 orig_root = btrfs_header_owner(orig_buf);
1423 orig_generation = btrfs_header_generation(orig_buf);
1424 level = btrfs_header_level(buf);
1425
1426 if (!root->ref_cows) {
1427 if (level == 0 &&
1428 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
1429 return 0;
1430 if (level != 0 &&
1431 root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID)
1432 return 0;
1433 }
1434
1435 for (i = 0, slot = start_slot; i < nr; i++, slot++) {
1436 cond_resched();
1437 if (level == 0) {
1438 btrfs_item_key_to_cpu(buf, &key, slot);
1439 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1440 continue;
1441 fi = btrfs_item_ptr(buf, slot,
1442 struct btrfs_file_extent_item);
1443 if (btrfs_file_extent_type(buf, fi) ==
1444 BTRFS_FILE_EXTENT_INLINE)
1445 continue;
1446 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1447 if (bytenr == 0)
1448 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04001449 ret = __btrfs_update_extent_ref(trans, root, bytenr,
Chris Mason56bec292009-03-13 10:10:06 -04001450 btrfs_file_extent_disk_num_bytes(buf, fi),
1451 orig_buf->start, buf->start,
1452 orig_root, ref_root, orig_generation,
1453 ref_generation, key.objectid);
Zheng Yan31840ae2008-09-23 13:14:14 -04001454 if (ret)
1455 goto fail;
1456 } else {
1457 bytenr = btrfs_node_blockptr(buf, slot);
Zheng Yan31840ae2008-09-23 13:14:14 -04001458 ret = __btrfs_update_extent_ref(trans, root, bytenr,
Chris Mason56bec292009-03-13 10:10:06 -04001459 buf->len, orig_buf->start,
1460 buf->start, orig_root, ref_root,
Zheng Yan31840ae2008-09-23 13:14:14 -04001461 orig_generation, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001462 level - 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04001463 if (ret)
1464 goto fail;
1465 }
1466 }
1467 return 0;
1468fail:
1469 WARN_ON(1);
1470 return -1;
1471}
1472
Chris Mason9078a3e2007-04-26 16:46:15 -04001473static int write_one_cache_group(struct btrfs_trans_handle *trans,
1474 struct btrfs_root *root,
1475 struct btrfs_path *path,
1476 struct btrfs_block_group_cache *cache)
1477{
1478 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04001479 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04001480 unsigned long bi;
1481 struct extent_buffer *leaf;
Chris Mason9078a3e2007-04-26 16:46:15 -04001482
Chris Mason9078a3e2007-04-26 16:46:15 -04001483 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001484 if (ret < 0)
1485 goto fail;
Chris Mason9078a3e2007-04-26 16:46:15 -04001486 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04001487
1488 leaf = path->nodes[0];
1489 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
1490 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
1491 btrfs_mark_buffer_dirty(leaf);
Chris Mason9078a3e2007-04-26 16:46:15 -04001492 btrfs_release_path(extent_root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -04001493fail:
Chris Mason9078a3e2007-04-26 16:46:15 -04001494 if (ret)
1495 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04001496 return 0;
1497
1498}
1499
Chris Mason96b51792007-10-15 16:15:19 -04001500int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
1501 struct btrfs_root *root)
Chris Mason9078a3e2007-04-26 16:46:15 -04001502{
Josef Bacik0f9dd462008-09-23 13:14:11 -04001503 struct btrfs_block_group_cache *cache, *entry;
1504 struct rb_node *n;
Chris Mason9078a3e2007-04-26 16:46:15 -04001505 int err = 0;
1506 int werr = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04001507 struct btrfs_path *path;
Chris Mason96b51792007-10-15 16:15:19 -04001508 u64 last = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04001509
1510 path = btrfs_alloc_path();
1511 if (!path)
1512 return -ENOMEM;
1513
Chris Masond3977122009-01-05 21:25:51 -05001514 while (1) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001515 cache = NULL;
1516 spin_lock(&root->fs_info->block_group_cache_lock);
1517 for (n = rb_first(&root->fs_info->block_group_cache_tree);
1518 n; n = rb_next(n)) {
1519 entry = rb_entry(n, struct btrfs_block_group_cache,
1520 cache_node);
1521 if (entry->dirty) {
1522 cache = entry;
1523 break;
1524 }
1525 }
1526 spin_unlock(&root->fs_info->block_group_cache_lock);
1527
1528 if (!cache)
Chris Mason9078a3e2007-04-26 16:46:15 -04001529 break;
Chris Mason54aa1f42007-06-22 14:16:25 -04001530
Zheng Yane8569812008-09-26 10:05:48 -04001531 cache->dirty = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001532 last += cache->key.offset;
1533
Chris Mason96b51792007-10-15 16:15:19 -04001534 err = write_one_cache_group(trans, root,
1535 path, cache);
1536 /*
1537 * if we fail to write the cache group, we want
1538 * to keep it marked dirty in hopes that a later
1539 * write will work
1540 */
1541 if (err) {
1542 werr = err;
1543 continue;
Chris Mason9078a3e2007-04-26 16:46:15 -04001544 }
1545 }
1546 btrfs_free_path(path);
1547 return werr;
1548}
1549
Yan Zhengd2fb3432008-12-11 16:30:39 -05001550int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr)
1551{
1552 struct btrfs_block_group_cache *block_group;
1553 int readonly = 0;
1554
1555 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
1556 if (!block_group || block_group->ro)
1557 readonly = 1;
1558 if (block_group)
1559 put_block_group(block_group);
1560 return readonly;
1561}
1562
Chris Mason593060d2008-03-25 16:50:33 -04001563static int update_space_info(struct btrfs_fs_info *info, u64 flags,
1564 u64 total_bytes, u64 bytes_used,
1565 struct btrfs_space_info **space_info)
1566{
1567 struct btrfs_space_info *found;
1568
1569 found = __find_space_info(info, flags);
1570 if (found) {
Josef Bacik25179202008-10-29 14:49:05 -04001571 spin_lock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04001572 found->total_bytes += total_bytes;
1573 found->bytes_used += bytes_used;
Chris Mason8f18cf12008-04-25 16:53:30 -04001574 found->full = 0;
Josef Bacik25179202008-10-29 14:49:05 -04001575 spin_unlock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04001576 *space_info = found;
1577 return 0;
1578 }
Yan Zhengc146afa2008-11-12 14:34:12 -05001579 found = kzalloc(sizeof(*found), GFP_NOFS);
Chris Mason593060d2008-03-25 16:50:33 -04001580 if (!found)
1581 return -ENOMEM;
1582
Josef Bacik0f9dd462008-09-23 13:14:11 -04001583 INIT_LIST_HEAD(&found->block_groups);
Josef Bacik80eb2342008-10-29 14:49:05 -04001584 init_rwsem(&found->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001585 spin_lock_init(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04001586 found->flags = flags;
1587 found->total_bytes = total_bytes;
1588 found->bytes_used = bytes_used;
1589 found->bytes_pinned = 0;
Zheng Yane8569812008-09-26 10:05:48 -04001590 found->bytes_reserved = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05001591 found->bytes_readonly = 0;
Josef Bacik6a632092009-02-20 11:00:09 -05001592 found->bytes_delalloc = 0;
Chris Mason593060d2008-03-25 16:50:33 -04001593 found->full = 0;
Chris Mason0ef3e662008-05-24 14:04:53 -04001594 found->force_alloc = 0;
Chris Mason593060d2008-03-25 16:50:33 -04001595 *space_info = found;
Chris Mason4184ea72009-03-10 12:39:20 -04001596 list_add_rcu(&found->list, &info->space_info);
Chris Mason593060d2008-03-25 16:50:33 -04001597 return 0;
1598}
1599
Chris Mason8790d502008-04-03 16:29:03 -04001600static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
1601{
1602 u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
Chris Mason611f0e02008-04-03 16:29:03 -04001603 BTRFS_BLOCK_GROUP_RAID1 |
Chris Mason321aecc2008-04-16 10:49:51 -04001604 BTRFS_BLOCK_GROUP_RAID10 |
Chris Mason611f0e02008-04-03 16:29:03 -04001605 BTRFS_BLOCK_GROUP_DUP);
Chris Mason8790d502008-04-03 16:29:03 -04001606 if (extra_flags) {
1607 if (flags & BTRFS_BLOCK_GROUP_DATA)
1608 fs_info->avail_data_alloc_bits |= extra_flags;
1609 if (flags & BTRFS_BLOCK_GROUP_METADATA)
1610 fs_info->avail_metadata_alloc_bits |= extra_flags;
1611 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
1612 fs_info->avail_system_alloc_bits |= extra_flags;
1613 }
1614}
Chris Mason593060d2008-03-25 16:50:33 -04001615
Yan Zhengc146afa2008-11-12 14:34:12 -05001616static void set_block_group_readonly(struct btrfs_block_group_cache *cache)
1617{
1618 spin_lock(&cache->space_info->lock);
1619 spin_lock(&cache->lock);
1620 if (!cache->ro) {
1621 cache->space_info->bytes_readonly += cache->key.offset -
1622 btrfs_block_group_used(&cache->item);
1623 cache->ro = 1;
1624 }
1625 spin_unlock(&cache->lock);
1626 spin_unlock(&cache->space_info->lock);
1627}
1628
Yan Zheng2b820322008-11-17 21:11:30 -05001629u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
Chris Masonec44a352008-04-28 15:29:52 -04001630{
Yan Zheng2b820322008-11-17 21:11:30 -05001631 u64 num_devices = root->fs_info->fs_devices->rw_devices;
Chris Masona061fc82008-05-07 11:43:44 -04001632
1633 if (num_devices == 1)
1634 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
1635 if (num_devices < 4)
1636 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
1637
Chris Masonec44a352008-04-28 15:29:52 -04001638 if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
1639 (flags & (BTRFS_BLOCK_GROUP_RAID1 |
Chris Masona061fc82008-05-07 11:43:44 -04001640 BTRFS_BLOCK_GROUP_RAID10))) {
Chris Masonec44a352008-04-28 15:29:52 -04001641 flags &= ~BTRFS_BLOCK_GROUP_DUP;
Chris Masona061fc82008-05-07 11:43:44 -04001642 }
Chris Masonec44a352008-04-28 15:29:52 -04001643
1644 if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
Chris Masona061fc82008-05-07 11:43:44 -04001645 (flags & BTRFS_BLOCK_GROUP_RAID10)) {
Chris Masonec44a352008-04-28 15:29:52 -04001646 flags &= ~BTRFS_BLOCK_GROUP_RAID1;
Chris Masona061fc82008-05-07 11:43:44 -04001647 }
Chris Masonec44a352008-04-28 15:29:52 -04001648
1649 if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
1650 ((flags & BTRFS_BLOCK_GROUP_RAID1) |
1651 (flags & BTRFS_BLOCK_GROUP_RAID10) |
1652 (flags & BTRFS_BLOCK_GROUP_DUP)))
1653 flags &= ~BTRFS_BLOCK_GROUP_RAID0;
1654 return flags;
1655}
1656
Josef Bacik6a632092009-02-20 11:00:09 -05001657static u64 btrfs_get_alloc_profile(struct btrfs_root *root, u64 data)
1658{
1659 struct btrfs_fs_info *info = root->fs_info;
1660 u64 alloc_profile;
1661
1662 if (data) {
1663 alloc_profile = info->avail_data_alloc_bits &
1664 info->data_alloc_profile;
1665 data = BTRFS_BLOCK_GROUP_DATA | alloc_profile;
1666 } else if (root == root->fs_info->chunk_root) {
1667 alloc_profile = info->avail_system_alloc_bits &
1668 info->system_alloc_profile;
1669 data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile;
1670 } else {
1671 alloc_profile = info->avail_metadata_alloc_bits &
1672 info->metadata_alloc_profile;
1673 data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile;
1674 }
1675
1676 return btrfs_reduce_alloc_profile(root, data);
1677}
1678
1679void btrfs_set_inode_space_info(struct btrfs_root *root, struct inode *inode)
1680{
1681 u64 alloc_target;
1682
1683 alloc_target = btrfs_get_alloc_profile(root, 1);
1684 BTRFS_I(inode)->space_info = __find_space_info(root->fs_info,
1685 alloc_target);
1686}
1687
1688/*
1689 * for now this just makes sure we have at least 5% of our metadata space free
1690 * for use.
1691 */
1692int btrfs_check_metadata_free_space(struct btrfs_root *root)
1693{
1694 struct btrfs_fs_info *info = root->fs_info;
1695 struct btrfs_space_info *meta_sinfo;
1696 u64 alloc_target, thresh;
Josef Bacik4e06bdd2009-02-20 10:59:53 -05001697 int committed = 0, ret;
Josef Bacik6a632092009-02-20 11:00:09 -05001698
1699 /* get the space info for where the metadata will live */
1700 alloc_target = btrfs_get_alloc_profile(root, 0);
1701 meta_sinfo = __find_space_info(info, alloc_target);
1702
Josef Bacik4e06bdd2009-02-20 10:59:53 -05001703again:
Josef Bacik6a632092009-02-20 11:00:09 -05001704 spin_lock(&meta_sinfo->lock);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05001705 if (!meta_sinfo->full)
1706 thresh = meta_sinfo->total_bytes * 80;
1707 else
1708 thresh = meta_sinfo->total_bytes * 95;
Josef Bacik6a632092009-02-20 11:00:09 -05001709
1710 do_div(thresh, 100);
1711
1712 if (meta_sinfo->bytes_used + meta_sinfo->bytes_reserved +
1713 meta_sinfo->bytes_pinned + meta_sinfo->bytes_readonly > thresh) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05001714 struct btrfs_trans_handle *trans;
1715 if (!meta_sinfo->full) {
1716 meta_sinfo->force_alloc = 1;
1717 spin_unlock(&meta_sinfo->lock);
1718
1719 trans = btrfs_start_transaction(root, 1);
1720 if (!trans)
1721 return -ENOMEM;
1722
1723 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
1724 2 * 1024 * 1024, alloc_target, 0);
1725 btrfs_end_transaction(trans, root);
1726 goto again;
1727 }
Josef Bacik6a632092009-02-20 11:00:09 -05001728 spin_unlock(&meta_sinfo->lock);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05001729
1730 if (!committed) {
1731 committed = 1;
1732 trans = btrfs_join_transaction(root, 1);
1733 if (!trans)
1734 return -ENOMEM;
1735 ret = btrfs_commit_transaction(trans, root);
1736 if (ret)
1737 return ret;
1738 goto again;
1739 }
Josef Bacik6a632092009-02-20 11:00:09 -05001740 return -ENOSPC;
1741 }
1742 spin_unlock(&meta_sinfo->lock);
1743
1744 return 0;
1745}
1746
1747/*
1748 * This will check the space that the inode allocates from to make sure we have
1749 * enough space for bytes.
1750 */
1751int btrfs_check_data_free_space(struct btrfs_root *root, struct inode *inode,
1752 u64 bytes)
1753{
1754 struct btrfs_space_info *data_sinfo;
Josef Bacik4e06bdd2009-02-20 10:59:53 -05001755 int ret = 0, committed = 0;
Josef Bacik6a632092009-02-20 11:00:09 -05001756
1757 /* make sure bytes are sectorsize aligned */
1758 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
1759
1760 data_sinfo = BTRFS_I(inode)->space_info;
1761again:
1762 /* make sure we have enough space to handle the data first */
1763 spin_lock(&data_sinfo->lock);
1764 if (data_sinfo->total_bytes - data_sinfo->bytes_used -
1765 data_sinfo->bytes_delalloc - data_sinfo->bytes_reserved -
1766 data_sinfo->bytes_pinned - data_sinfo->bytes_readonly -
1767 data_sinfo->bytes_may_use < bytes) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05001768 struct btrfs_trans_handle *trans;
1769
Josef Bacik6a632092009-02-20 11:00:09 -05001770 /*
1771 * if we don't have enough free bytes in this space then we need
1772 * to alloc a new chunk.
1773 */
1774 if (!data_sinfo->full) {
1775 u64 alloc_target;
Josef Bacik6a632092009-02-20 11:00:09 -05001776
1777 data_sinfo->force_alloc = 1;
1778 spin_unlock(&data_sinfo->lock);
1779
1780 alloc_target = btrfs_get_alloc_profile(root, 1);
1781 trans = btrfs_start_transaction(root, 1);
1782 if (!trans)
1783 return -ENOMEM;
1784
1785 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
1786 bytes + 2 * 1024 * 1024,
1787 alloc_target, 0);
1788 btrfs_end_transaction(trans, root);
1789 if (ret)
1790 return ret;
1791 goto again;
1792 }
1793 spin_unlock(&data_sinfo->lock);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05001794
1795 /* commit the current transaction and try again */
1796 if (!committed) {
1797 committed = 1;
1798 trans = btrfs_join_transaction(root, 1);
1799 if (!trans)
1800 return -ENOMEM;
1801 ret = btrfs_commit_transaction(trans, root);
1802 if (ret)
1803 return ret;
1804 goto again;
1805 }
1806
Josef Bacik6a632092009-02-20 11:00:09 -05001807 printk(KERN_ERR "no space left, need %llu, %llu delalloc bytes"
1808 ", %llu bytes_used, %llu bytes_reserved, "
1809 "%llu bytes_pinned, %llu bytes_readonly, %llu may use"
1810 "%llu total\n", bytes, data_sinfo->bytes_delalloc,
1811 data_sinfo->bytes_used, data_sinfo->bytes_reserved,
1812 data_sinfo->bytes_pinned, data_sinfo->bytes_readonly,
1813 data_sinfo->bytes_may_use, data_sinfo->total_bytes);
1814 return -ENOSPC;
1815 }
1816 data_sinfo->bytes_may_use += bytes;
1817 BTRFS_I(inode)->reserved_bytes += bytes;
1818 spin_unlock(&data_sinfo->lock);
1819
1820 return btrfs_check_metadata_free_space(root);
1821}
1822
1823/*
1824 * if there was an error for whatever reason after calling
1825 * btrfs_check_data_free_space, call this so we can cleanup the counters.
1826 */
1827void btrfs_free_reserved_data_space(struct btrfs_root *root,
1828 struct inode *inode, u64 bytes)
1829{
1830 struct btrfs_space_info *data_sinfo;
1831
1832 /* make sure bytes are sectorsize aligned */
1833 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
1834
1835 data_sinfo = BTRFS_I(inode)->space_info;
1836 spin_lock(&data_sinfo->lock);
1837 data_sinfo->bytes_may_use -= bytes;
1838 BTRFS_I(inode)->reserved_bytes -= bytes;
1839 spin_unlock(&data_sinfo->lock);
1840}
1841
1842/* called when we are adding a delalloc extent to the inode's io_tree */
1843void btrfs_delalloc_reserve_space(struct btrfs_root *root, struct inode *inode,
1844 u64 bytes)
1845{
1846 struct btrfs_space_info *data_sinfo;
1847
1848 /* get the space info for where this inode will be storing its data */
1849 data_sinfo = BTRFS_I(inode)->space_info;
1850
1851 /* make sure we have enough space to handle the data first */
1852 spin_lock(&data_sinfo->lock);
1853 data_sinfo->bytes_delalloc += bytes;
1854
1855 /*
1856 * we are adding a delalloc extent without calling
1857 * btrfs_check_data_free_space first. This happens on a weird
1858 * writepage condition, but shouldn't hurt our accounting
1859 */
1860 if (unlikely(bytes > BTRFS_I(inode)->reserved_bytes)) {
1861 data_sinfo->bytes_may_use -= BTRFS_I(inode)->reserved_bytes;
1862 BTRFS_I(inode)->reserved_bytes = 0;
1863 } else {
1864 data_sinfo->bytes_may_use -= bytes;
1865 BTRFS_I(inode)->reserved_bytes -= bytes;
1866 }
1867
1868 spin_unlock(&data_sinfo->lock);
1869}
1870
1871/* called when we are clearing an delalloc extent from the inode's io_tree */
1872void btrfs_delalloc_free_space(struct btrfs_root *root, struct inode *inode,
1873 u64 bytes)
1874{
1875 struct btrfs_space_info *info;
1876
1877 info = BTRFS_I(inode)->space_info;
1878
1879 spin_lock(&info->lock);
1880 info->bytes_delalloc -= bytes;
1881 spin_unlock(&info->lock);
1882}
1883
Chris Mason6324fbf2008-03-24 15:01:59 -04001884static int do_chunk_alloc(struct btrfs_trans_handle *trans,
1885 struct btrfs_root *extent_root, u64 alloc_bytes,
Chris Mason0ef3e662008-05-24 14:04:53 -04001886 u64 flags, int force)
Chris Mason6324fbf2008-03-24 15:01:59 -04001887{
1888 struct btrfs_space_info *space_info;
1889 u64 thresh;
Yan Zhengc146afa2008-11-12 14:34:12 -05001890 int ret = 0;
1891
1892 mutex_lock(&extent_root->fs_info->chunk_mutex);
Chris Mason6324fbf2008-03-24 15:01:59 -04001893
Yan Zheng2b820322008-11-17 21:11:30 -05001894 flags = btrfs_reduce_alloc_profile(extent_root, flags);
Chris Masonec44a352008-04-28 15:29:52 -04001895
Chris Mason6324fbf2008-03-24 15:01:59 -04001896 space_info = __find_space_info(extent_root->fs_info, flags);
Chris Mason593060d2008-03-25 16:50:33 -04001897 if (!space_info) {
1898 ret = update_space_info(extent_root->fs_info, flags,
1899 0, 0, &space_info);
1900 BUG_ON(ret);
1901 }
Chris Mason6324fbf2008-03-24 15:01:59 -04001902 BUG_ON(!space_info);
1903
Josef Bacik25179202008-10-29 14:49:05 -04001904 spin_lock(&space_info->lock);
Chris Mason0ef3e662008-05-24 14:04:53 -04001905 if (space_info->force_alloc) {
1906 force = 1;
1907 space_info->force_alloc = 0;
1908 }
Josef Bacik25179202008-10-29 14:49:05 -04001909 if (space_info->full) {
1910 spin_unlock(&space_info->lock);
Chris Mason925baed2008-06-25 16:01:30 -04001911 goto out;
Josef Bacik25179202008-10-29 14:49:05 -04001912 }
Chris Mason6324fbf2008-03-24 15:01:59 -04001913
Yan Zhengc146afa2008-11-12 14:34:12 -05001914 thresh = space_info->total_bytes - space_info->bytes_readonly;
1915 thresh = div_factor(thresh, 6);
Chris Mason0ef3e662008-05-24 14:04:53 -04001916 if (!force &&
Zheng Yane8569812008-09-26 10:05:48 -04001917 (space_info->bytes_used + space_info->bytes_pinned +
Josef Bacik25179202008-10-29 14:49:05 -04001918 space_info->bytes_reserved + alloc_bytes) < thresh) {
1919 spin_unlock(&space_info->lock);
Chris Mason925baed2008-06-25 16:01:30 -04001920 goto out;
Josef Bacik25179202008-10-29 14:49:05 -04001921 }
Josef Bacik25179202008-10-29 14:49:05 -04001922 spin_unlock(&space_info->lock);
1923
Yan Zheng2b820322008-11-17 21:11:30 -05001924 ret = btrfs_alloc_chunk(trans, extent_root, flags);
Chris Masond3977122009-01-05 21:25:51 -05001925 if (ret)
Chris Mason6324fbf2008-03-24 15:01:59 -04001926 space_info->full = 1;
Chris Masona74a4b92008-06-25 16:01:31 -04001927out:
Yan Zhengc146afa2008-11-12 14:34:12 -05001928 mutex_unlock(&extent_root->fs_info->chunk_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001929 return ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04001930}
1931
Chris Mason9078a3e2007-04-26 16:46:15 -04001932static int update_block_group(struct btrfs_trans_handle *trans,
1933 struct btrfs_root *root,
Chris Masondb945352007-10-15 16:15:53 -04001934 u64 bytenr, u64 num_bytes, int alloc,
Chris Mason0b86a832008-03-24 15:01:56 -04001935 int mark_free)
Chris Mason9078a3e2007-04-26 16:46:15 -04001936{
1937 struct btrfs_block_group_cache *cache;
1938 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -04001939 u64 total = num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04001940 u64 old_val;
Chris Masondb945352007-10-15 16:15:53 -04001941 u64 byte_in_group;
Chris Mason3e1ad542007-05-07 20:03:49 -04001942
Chris Masond3977122009-01-05 21:25:51 -05001943 while (total) {
Chris Masondb945352007-10-15 16:15:53 -04001944 cache = btrfs_lookup_block_group(info, bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05001945 if (!cache)
Chris Mason9078a3e2007-04-26 16:46:15 -04001946 return -1;
Chris Masondb945352007-10-15 16:15:53 -04001947 byte_in_group = bytenr - cache->key.objectid;
1948 WARN_ON(byte_in_group > cache->key.offset);
Chris Mason9078a3e2007-04-26 16:46:15 -04001949
Josef Bacik25179202008-10-29 14:49:05 -04001950 spin_lock(&cache->space_info->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04001951 spin_lock(&cache->lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001952 cache->dirty = 1;
Chris Mason9078a3e2007-04-26 16:46:15 -04001953 old_val = btrfs_block_group_used(&cache->item);
Chris Masondb945352007-10-15 16:15:53 -04001954 num_bytes = min(total, cache->key.offset - byte_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -04001955 if (alloc) {
Chris Masondb945352007-10-15 16:15:53 -04001956 old_val += num_bytes;
Chris Mason6324fbf2008-03-24 15:01:59 -04001957 cache->space_info->bytes_used += num_bytes;
Yan Zhenga512bbf2008-12-08 16:46:26 -05001958 if (cache->ro)
Yan Zhengc146afa2008-11-12 14:34:12 -05001959 cache->space_info->bytes_readonly -= num_bytes;
Chris Masonc286ac42008-07-22 23:06:41 -04001960 btrfs_set_block_group_used(&cache->item, old_val);
1961 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04001962 spin_unlock(&cache->space_info->lock);
Chris Masoncd1bc462007-04-27 10:08:34 -04001963 } else {
Chris Masondb945352007-10-15 16:15:53 -04001964 old_val -= num_bytes;
Chris Mason6324fbf2008-03-24 15:01:59 -04001965 cache->space_info->bytes_used -= num_bytes;
Yan Zhengc146afa2008-11-12 14:34:12 -05001966 if (cache->ro)
1967 cache->space_info->bytes_readonly += num_bytes;
Chris Masonc286ac42008-07-22 23:06:41 -04001968 btrfs_set_block_group_used(&cache->item, old_val);
1969 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04001970 spin_unlock(&cache->space_info->lock);
Chris Masonf510cfe2007-10-15 16:14:48 -04001971 if (mark_free) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001972 int ret;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001973
1974 ret = btrfs_discard_extent(root, bytenr,
1975 num_bytes);
1976 WARN_ON(ret);
1977
Josef Bacik0f9dd462008-09-23 13:14:11 -04001978 ret = btrfs_add_free_space(cache, bytenr,
1979 num_bytes);
Yan Zhengd2fb3432008-12-11 16:30:39 -05001980 WARN_ON(ret);
Chris Masone37c9e62007-05-09 20:13:14 -04001981 }
Chris Masoncd1bc462007-04-27 10:08:34 -04001982 }
Yan Zhengd2fb3432008-12-11 16:30:39 -05001983 put_block_group(cache);
Chris Masondb945352007-10-15 16:15:53 -04001984 total -= num_bytes;
1985 bytenr += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04001986 }
1987 return 0;
1988}
Chris Mason6324fbf2008-03-24 15:01:59 -04001989
Chris Masona061fc82008-05-07 11:43:44 -04001990static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
1991{
Josef Bacik0f9dd462008-09-23 13:14:11 -04001992 struct btrfs_block_group_cache *cache;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001993 u64 bytenr;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001994
1995 cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
1996 if (!cache)
Chris Masona061fc82008-05-07 11:43:44 -04001997 return 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001998
Yan Zhengd2fb3432008-12-11 16:30:39 -05001999 bytenr = cache->key.objectid;
2000 put_block_group(cache);
2001
2002 return bytenr;
Chris Masona061fc82008-05-07 11:43:44 -04002003}
2004
Chris Masone02119d2008-09-05 16:13:11 -04002005int btrfs_update_pinned_extents(struct btrfs_root *root,
Yan324ae4d2007-11-16 14:57:08 -05002006 u64 bytenr, u64 num, int pin)
2007{
2008 u64 len;
2009 struct btrfs_block_group_cache *cache;
2010 struct btrfs_fs_info *fs_info = root->fs_info;
2011
Josef Bacik25179202008-10-29 14:49:05 -04002012 WARN_ON(!mutex_is_locked(&root->fs_info->pinned_mutex));
Yan324ae4d2007-11-16 14:57:08 -05002013 if (pin) {
2014 set_extent_dirty(&fs_info->pinned_extents,
2015 bytenr, bytenr + num - 1, GFP_NOFS);
2016 } else {
2017 clear_extent_dirty(&fs_info->pinned_extents,
2018 bytenr, bytenr + num - 1, GFP_NOFS);
2019 }
2020 while (num > 0) {
2021 cache = btrfs_lookup_block_group(fs_info, bytenr);
Zheng Yane8569812008-09-26 10:05:48 -04002022 BUG_ON(!cache);
2023 len = min(num, cache->key.offset -
2024 (bytenr - cache->key.objectid));
Yan324ae4d2007-11-16 14:57:08 -05002025 if (pin) {
Josef Bacik25179202008-10-29 14:49:05 -04002026 spin_lock(&cache->space_info->lock);
Zheng Yane8569812008-09-26 10:05:48 -04002027 spin_lock(&cache->lock);
2028 cache->pinned += len;
2029 cache->space_info->bytes_pinned += len;
2030 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04002031 spin_unlock(&cache->space_info->lock);
Yan324ae4d2007-11-16 14:57:08 -05002032 fs_info->total_pinned += len;
2033 } else {
Josef Bacik25179202008-10-29 14:49:05 -04002034 spin_lock(&cache->space_info->lock);
Zheng Yane8569812008-09-26 10:05:48 -04002035 spin_lock(&cache->lock);
2036 cache->pinned -= len;
2037 cache->space_info->bytes_pinned -= len;
2038 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04002039 spin_unlock(&cache->space_info->lock);
Yan324ae4d2007-11-16 14:57:08 -05002040 fs_info->total_pinned -= len;
Josef Bacik07103a32008-11-19 15:17:55 -05002041 if (cache->cached)
2042 btrfs_add_free_space(cache, bytenr, len);
Yan324ae4d2007-11-16 14:57:08 -05002043 }
Yan Zhengd2fb3432008-12-11 16:30:39 -05002044 put_block_group(cache);
Yan324ae4d2007-11-16 14:57:08 -05002045 bytenr += len;
2046 num -= len;
2047 }
2048 return 0;
2049}
Chris Mason9078a3e2007-04-26 16:46:15 -04002050
Zheng Yane8569812008-09-26 10:05:48 -04002051static int update_reserved_extents(struct btrfs_root *root,
2052 u64 bytenr, u64 num, int reserve)
2053{
2054 u64 len;
2055 struct btrfs_block_group_cache *cache;
2056 struct btrfs_fs_info *fs_info = root->fs_info;
2057
Zheng Yane8569812008-09-26 10:05:48 -04002058 while (num > 0) {
2059 cache = btrfs_lookup_block_group(fs_info, bytenr);
2060 BUG_ON(!cache);
2061 len = min(num, cache->key.offset -
2062 (bytenr - cache->key.objectid));
Josef Bacik25179202008-10-29 14:49:05 -04002063
2064 spin_lock(&cache->space_info->lock);
2065 spin_lock(&cache->lock);
Zheng Yane8569812008-09-26 10:05:48 -04002066 if (reserve) {
Zheng Yane8569812008-09-26 10:05:48 -04002067 cache->reserved += len;
2068 cache->space_info->bytes_reserved += len;
Zheng Yane8569812008-09-26 10:05:48 -04002069 } else {
Zheng Yane8569812008-09-26 10:05:48 -04002070 cache->reserved -= len;
2071 cache->space_info->bytes_reserved -= len;
Zheng Yane8569812008-09-26 10:05:48 -04002072 }
Josef Bacik25179202008-10-29 14:49:05 -04002073 spin_unlock(&cache->lock);
2074 spin_unlock(&cache->space_info->lock);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002075 put_block_group(cache);
Zheng Yane8569812008-09-26 10:05:48 -04002076 bytenr += len;
2077 num -= len;
2078 }
2079 return 0;
2080}
2081
Chris Masond1310b22008-01-24 16:13:08 -05002082int btrfs_copy_pinned(struct btrfs_root *root, struct extent_io_tree *copy)
Chris Masonccd467d2007-06-28 15:57:36 -04002083{
Chris Masonccd467d2007-06-28 15:57:36 -04002084 u64 last = 0;
Chris Mason1a5bc162007-10-15 16:15:26 -04002085 u64 start;
2086 u64 end;
Chris Masond1310b22008-01-24 16:13:08 -05002087 struct extent_io_tree *pinned_extents = &root->fs_info->pinned_extents;
Chris Masonccd467d2007-06-28 15:57:36 -04002088 int ret;
Chris Masonccd467d2007-06-28 15:57:36 -04002089
Josef Bacik25179202008-10-29 14:49:05 -04002090 mutex_lock(&root->fs_info->pinned_mutex);
Chris Masond3977122009-01-05 21:25:51 -05002091 while (1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04002092 ret = find_first_extent_bit(pinned_extents, last,
2093 &start, &end, EXTENT_DIRTY);
2094 if (ret)
Chris Masonccd467d2007-06-28 15:57:36 -04002095 break;
Chris Mason1a5bc162007-10-15 16:15:26 -04002096 set_extent_dirty(copy, start, end, GFP_NOFS);
2097 last = end + 1;
Chris Masonccd467d2007-06-28 15:57:36 -04002098 }
Josef Bacik25179202008-10-29 14:49:05 -04002099 mutex_unlock(&root->fs_info->pinned_mutex);
Chris Masonccd467d2007-06-28 15:57:36 -04002100 return 0;
2101}
2102
2103int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
2104 struct btrfs_root *root,
Chris Masond1310b22008-01-24 16:13:08 -05002105 struct extent_io_tree *unpin)
Chris Masona28ec192007-03-06 20:08:01 -05002106{
Chris Mason1a5bc162007-10-15 16:15:26 -04002107 u64 start;
2108 u64 end;
Chris Masona28ec192007-03-06 20:08:01 -05002109 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05002110
Josef Bacik25179202008-10-29 14:49:05 -04002111 mutex_lock(&root->fs_info->pinned_mutex);
Chris Masond3977122009-01-05 21:25:51 -05002112 while (1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04002113 ret = find_first_extent_bit(unpin, 0, &start, &end,
2114 EXTENT_DIRTY);
2115 if (ret)
Chris Masona28ec192007-03-06 20:08:01 -05002116 break;
Liu Hui1f3c79a2009-01-05 15:57:51 -05002117
2118 ret = btrfs_discard_extent(root, start, end + 1 - start);
2119
Chris Masone02119d2008-09-05 16:13:11 -04002120 btrfs_update_pinned_extents(root, start, end + 1 - start, 0);
Chris Mason1a5bc162007-10-15 16:15:26 -04002121 clear_extent_dirty(unpin, start, end, GFP_NOFS);
Liu Hui1f3c79a2009-01-05 15:57:51 -05002122
Chris Masonc286ac42008-07-22 23:06:41 -04002123 if (need_resched()) {
Josef Bacik25179202008-10-29 14:49:05 -04002124 mutex_unlock(&root->fs_info->pinned_mutex);
Chris Masonc286ac42008-07-22 23:06:41 -04002125 cond_resched();
Josef Bacik25179202008-10-29 14:49:05 -04002126 mutex_lock(&root->fs_info->pinned_mutex);
Chris Masonc286ac42008-07-22 23:06:41 -04002127 }
Chris Masona28ec192007-03-06 20:08:01 -05002128 }
Josef Bacik25179202008-10-29 14:49:05 -04002129 mutex_unlock(&root->fs_info->pinned_mutex);
Liu Hui1f3c79a2009-01-05 15:57:51 -05002130 return ret;
Chris Masona28ec192007-03-06 20:08:01 -05002131}
2132
Zheng Yan31840ae2008-09-23 13:14:14 -04002133static int pin_down_bytes(struct btrfs_trans_handle *trans,
2134 struct btrfs_root *root,
2135 u64 bytenr, u64 num_bytes, int is_data)
Chris Masone20d96d2007-03-22 12:13:20 -04002136{
Chris Mason1a5bc162007-10-15 16:15:26 -04002137 int err = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04002138 struct extent_buffer *buf;
Chris Mason78fae272007-03-25 11:35:08 -04002139
Zheng Yan31840ae2008-09-23 13:14:14 -04002140 if (is_data)
2141 goto pinit;
Chris Mason4bef0842008-09-08 11:18:08 -04002142
Zheng Yan31840ae2008-09-23 13:14:14 -04002143 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
2144 if (!buf)
2145 goto pinit;
Chris Mason4bef0842008-09-08 11:18:08 -04002146
Zheng Yan31840ae2008-09-23 13:14:14 -04002147 /* we can reuse a block if it hasn't been written
2148 * and it is from this transaction. We can't
2149 * reuse anything from the tree log root because
2150 * it has tiny sub-transactions.
2151 */
2152 if (btrfs_buffer_uptodate(buf, 0) &&
2153 btrfs_try_tree_lock(buf)) {
2154 u64 header_owner = btrfs_header_owner(buf);
2155 u64 header_transid = btrfs_header_generation(buf);
2156 if (header_owner != BTRFS_TREE_LOG_OBJECTID &&
Zheng Yan1a40e232008-09-26 10:09:34 -04002157 header_owner != BTRFS_TREE_RELOC_OBJECTID &&
Chris Mason56bec292009-03-13 10:10:06 -04002158 header_owner != BTRFS_DATA_RELOC_TREE_OBJECTID &&
Zheng Yan31840ae2008-09-23 13:14:14 -04002159 header_transid == trans->transid &&
2160 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
2161 clean_tree_block(NULL, root, buf);
2162 btrfs_tree_unlock(buf);
Chris Mason5f39d392007-10-15 16:14:19 -04002163 free_extent_buffer(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04002164 return 1;
Chris Mason8ef97622007-03-26 10:15:30 -04002165 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002166 btrfs_tree_unlock(buf);
Chris Masonf4b9aa82007-03-27 11:05:53 -04002167 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002168 free_extent_buffer(buf);
2169pinit:
2170 btrfs_update_pinned_extents(root, bytenr, num_bytes, 1);
2171
Chris Masonbe744172007-05-06 10:15:01 -04002172 BUG_ON(err < 0);
Chris Masone20d96d2007-03-22 12:13:20 -04002173 return 0;
2174}
2175
Chris Masona28ec192007-03-06 20:08:01 -05002176/*
2177 * remove an extent from the root, returns 0 on success
2178 */
Zheng Yan31840ae2008-09-23 13:14:14 -04002179static int __free_extent(struct btrfs_trans_handle *trans,
2180 struct btrfs_root *root,
2181 u64 bytenr, u64 num_bytes, u64 parent,
Chris Mason7bb86312007-12-11 09:25:06 -05002182 u64 root_objectid, u64 ref_generation,
Chris Mason56bec292009-03-13 10:10:06 -04002183 u64 owner_objectid, int pin, int mark_free,
2184 int refs_to_drop)
Chris Masona28ec192007-03-06 20:08:01 -05002185{
Chris Mason5caf2a02007-04-02 11:20:42 -04002186 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -04002187 struct btrfs_key key;
Chris Mason1261ec42007-03-20 20:35:03 -04002188 struct btrfs_fs_info *info = root->fs_info;
2189 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04002190 struct extent_buffer *leaf;
Chris Masona28ec192007-03-06 20:08:01 -05002191 int ret;
Chris Mason952fcca2008-02-18 16:33:44 -05002192 int extent_slot = 0;
2193 int found_extent = 0;
2194 int num_to_del = 1;
Chris Mason234b63a2007-03-13 10:46:10 -04002195 struct btrfs_extent_item *ei;
Chris Masoncf27e1e2007-03-13 09:49:06 -04002196 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -05002197
Chris Masondb945352007-10-15 16:15:53 -04002198 key.objectid = bytenr;
Chris Mason62e27492007-03-15 12:56:47 -04002199 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masondb945352007-10-15 16:15:53 -04002200 key.offset = num_bytes;
Chris Mason5caf2a02007-04-02 11:20:42 -04002201 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04002202 if (!path)
2203 return -ENOMEM;
2204
Chris Mason3c12ac72008-04-21 12:01:38 -04002205 path->reada = 1;
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002206 ret = lookup_extent_backref(trans, extent_root, path,
2207 bytenr, parent, root_objectid,
2208 ref_generation, owner_objectid, 1);
Chris Mason7bb86312007-12-11 09:25:06 -05002209 if (ret == 0) {
Chris Mason952fcca2008-02-18 16:33:44 -05002210 struct btrfs_key found_key;
2211 extent_slot = path->slots[0];
Chris Masond3977122009-01-05 21:25:51 -05002212 while (extent_slot > 0) {
Chris Mason952fcca2008-02-18 16:33:44 -05002213 extent_slot--;
2214 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2215 extent_slot);
2216 if (found_key.objectid != bytenr)
2217 break;
2218 if (found_key.type == BTRFS_EXTENT_ITEM_KEY &&
2219 found_key.offset == num_bytes) {
2220 found_extent = 1;
2221 break;
2222 }
2223 if (path->slots[0] - extent_slot > 5)
2224 break;
2225 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002226 if (!found_extent) {
Chris Mason56bec292009-03-13 10:10:06 -04002227 ret = remove_extent_backref(trans, extent_root, path,
2228 refs_to_drop);
Zheng Yan31840ae2008-09-23 13:14:14 -04002229 BUG_ON(ret);
2230 btrfs_release_path(extent_root, path);
2231 ret = btrfs_search_slot(trans, extent_root,
2232 &key, path, -1, 1);
Josef Bacikf3465ca2008-11-12 14:19:50 -05002233 if (ret) {
2234 printk(KERN_ERR "umm, got %d back from search"
Chris Masond3977122009-01-05 21:25:51 -05002235 ", was looking for %llu\n", ret,
2236 (unsigned long long)bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05002237 btrfs_print_leaf(extent_root, path->nodes[0]);
2238 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002239 BUG_ON(ret);
2240 extent_slot = path->slots[0];
2241 }
Chris Mason7bb86312007-12-11 09:25:06 -05002242 } else {
2243 btrfs_print_leaf(extent_root, path->nodes[0]);
2244 WARN_ON(1);
Chris Masond3977122009-01-05 21:25:51 -05002245 printk(KERN_ERR "btrfs unable to find ref byte nr %llu "
Chris Mason56bec292009-03-13 10:10:06 -04002246 "parent %llu root %llu gen %llu owner %llu\n",
Chris Masond3977122009-01-05 21:25:51 -05002247 (unsigned long long)bytenr,
Chris Mason56bec292009-03-13 10:10:06 -04002248 (unsigned long long)parent,
Chris Masond3977122009-01-05 21:25:51 -05002249 (unsigned long long)root_objectid,
2250 (unsigned long long)ref_generation,
2251 (unsigned long long)owner_objectid);
Chris Mason7bb86312007-12-11 09:25:06 -05002252 }
Chris Mason5f39d392007-10-15 16:14:19 -04002253
2254 leaf = path->nodes[0];
Chris Mason952fcca2008-02-18 16:33:44 -05002255 ei = btrfs_item_ptr(leaf, extent_slot,
Chris Mason123abc82007-03-14 14:14:43 -04002256 struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002257 refs = btrfs_extent_refs(leaf, ei);
Chris Mason952fcca2008-02-18 16:33:44 -05002258
Chris Mason56bec292009-03-13 10:10:06 -04002259 /*
2260 * we're not allowed to delete the extent item if there
2261 * are other delayed ref updates pending
2262 */
2263
2264 BUG_ON(refs < refs_to_drop);
2265 refs -= refs_to_drop;
2266 btrfs_set_extent_refs(leaf, ei, refs);
Chris Mason5f39d392007-10-15 16:14:19 -04002267 btrfs_mark_buffer_dirty(leaf);
2268
Chris Mason56bec292009-03-13 10:10:06 -04002269 if (refs == 0 && found_extent &&
2270 path->slots[0] == extent_slot + 1) {
Zheng Yan31840ae2008-09-23 13:14:14 -04002271 struct btrfs_extent_ref *ref;
2272 ref = btrfs_item_ptr(leaf, path->slots[0],
2273 struct btrfs_extent_ref);
Chris Mason56bec292009-03-13 10:10:06 -04002274 BUG_ON(btrfs_ref_num_refs(leaf, ref) != refs_to_drop);
Chris Mason952fcca2008-02-18 16:33:44 -05002275 /* if the back ref and the extent are next to each other
2276 * they get deleted below in one shot
2277 */
2278 path->slots[0] = extent_slot;
2279 num_to_del = 2;
2280 } else if (found_extent) {
2281 /* otherwise delete the extent back ref */
Chris Mason56bec292009-03-13 10:10:06 -04002282 ret = remove_extent_backref(trans, extent_root, path,
2283 refs_to_drop);
Chris Mason952fcca2008-02-18 16:33:44 -05002284 BUG_ON(ret);
2285 /* if refs are 0, we need to setup the path for deletion */
2286 if (refs == 0) {
2287 btrfs_release_path(extent_root, path);
2288 ret = btrfs_search_slot(trans, extent_root, &key, path,
2289 -1, 1);
Chris Mason952fcca2008-02-18 16:33:44 -05002290 BUG_ON(ret);
2291 }
2292 }
2293
Chris Masoncf27e1e2007-03-13 09:49:06 -04002294 if (refs == 0) {
Chris Masondb945352007-10-15 16:15:53 -04002295 u64 super_used;
2296 u64 root_used;
Chris Mason78fae272007-03-25 11:35:08 -04002297
2298 if (pin) {
Josef Bacik25179202008-10-29 14:49:05 -04002299 mutex_lock(&root->fs_info->pinned_mutex);
Zheng Yan31840ae2008-09-23 13:14:14 -04002300 ret = pin_down_bytes(trans, root, bytenr, num_bytes,
2301 owner_objectid >= BTRFS_FIRST_FREE_OBJECTID);
Josef Bacik25179202008-10-29 14:49:05 -04002302 mutex_unlock(&root->fs_info->pinned_mutex);
Yanc5492282007-11-06 10:25:25 -05002303 if (ret > 0)
2304 mark_free = 1;
2305 BUG_ON(ret < 0);
Chris Mason78fae272007-03-25 11:35:08 -04002306 }
Josef Bacik58176a92007-08-29 15:47:34 -04002307 /* block accounting for super block */
Chris Mason75eff682008-12-15 15:54:40 -05002308 spin_lock(&info->delalloc_lock);
Chris Masondb945352007-10-15 16:15:53 -04002309 super_used = btrfs_super_bytes_used(&info->super_copy);
2310 btrfs_set_super_bytes_used(&info->super_copy,
2311 super_used - num_bytes);
Josef Bacik58176a92007-08-29 15:47:34 -04002312
2313 /* block accounting for root item */
Chris Masondb945352007-10-15 16:15:53 -04002314 root_used = btrfs_root_used(&root->root_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002315 btrfs_set_root_used(&root->root_item,
Chris Masondb945352007-10-15 16:15:53 -04002316 root_used - num_bytes);
Yan Zheng34bf63c2008-12-19 10:58:46 -05002317 spin_unlock(&info->delalloc_lock);
Chris Mason952fcca2008-02-18 16:33:44 -05002318 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
2319 num_to_del);
Zheng Yan31840ae2008-09-23 13:14:14 -04002320 BUG_ON(ret);
Josef Bacik25179202008-10-29 14:49:05 -04002321 btrfs_release_path(extent_root, path);
David Woodhouse21af8042008-08-12 14:13:26 +01002322
Chris Mason459931e2008-12-10 09:10:46 -05002323 if (owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
2324 ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
2325 BUG_ON(ret);
2326 }
2327
Chris Masondcbdd4d2008-12-16 13:51:01 -05002328 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
2329 mark_free);
2330 BUG_ON(ret);
Chris Masona28ec192007-03-06 20:08:01 -05002331 }
Chris Mason5caf2a02007-04-02 11:20:42 -04002332 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -05002333 return ret;
2334}
2335
2336/*
Chris Masonfec577f2007-02-26 10:40:21 -05002337 * remove an extent from the root, returns 0 on success
2338 */
Chris Mason925baed2008-06-25 16:01:30 -04002339static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
Chris Mason56bec292009-03-13 10:10:06 -04002340 struct btrfs_root *root,
2341 u64 bytenr, u64 num_bytes, u64 parent,
2342 u64 root_objectid, u64 ref_generation,
2343 u64 owner_objectid, int pin,
2344 int refs_to_drop)
Chris Masonfec577f2007-02-26 10:40:21 -05002345{
Chris Masondb945352007-10-15 16:15:53 -04002346 WARN_ON(num_bytes < root->sectorsize);
Josef Bacikf3465ca2008-11-12 14:19:50 -05002347
Chris Mason56bec292009-03-13 10:10:06 -04002348 /*
2349 * if metadata always pin
2350 * if data pin when any transaction has committed this
2351 */
2352 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID ||
2353 ref_generation != trans->transid)
Chris Mason4bef0842008-09-08 11:18:08 -04002354 pin = 1;
2355
Chris Mason4bef0842008-09-08 11:18:08 -04002356 if (ref_generation != trans->transid)
2357 pin = 1;
2358
Chris Mason56bec292009-03-13 10:10:06 -04002359 return __free_extent(trans, root, bytenr, num_bytes, parent,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002360 root_objectid, ref_generation,
Chris Mason56bec292009-03-13 10:10:06 -04002361 owner_objectid, pin, pin == 0, refs_to_drop);
Chris Masonfec577f2007-02-26 10:40:21 -05002362}
2363
Chris Mason925baed2008-06-25 16:01:30 -04002364int btrfs_free_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04002365 struct btrfs_root *root,
2366 u64 bytenr, u64 num_bytes, u64 parent,
2367 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002368 u64 owner_objectid, int pin)
Chris Mason925baed2008-06-25 16:01:30 -04002369{
2370 int ret;
2371
Chris Mason56bec292009-03-13 10:10:06 -04002372 /*
2373 * tree log blocks never actually go into the extent allocation
2374 * tree, just update pinning info and exit early.
2375 *
2376 * data extents referenced by the tree log do need to have
2377 * their reference counts bumped.
2378 */
2379 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID &&
2380 owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
2381 mutex_lock(&root->fs_info->pinned_mutex);
2382 btrfs_update_pinned_extents(root, bytenr, num_bytes, 1);
2383 mutex_unlock(&root->fs_info->pinned_mutex);
2384 update_reserved_extents(root, bytenr, num_bytes, 0);
2385 ret = 0;
2386 } else {
2387 ret = btrfs_add_delayed_ref(trans, bytenr, num_bytes, parent,
2388 root_objectid, ref_generation,
2389 owner_objectid,
2390 BTRFS_DROP_DELAYED_REF, 1);
2391 }
Chris Mason925baed2008-06-25 16:01:30 -04002392 return ret;
2393}
2394
Chris Mason87ee04e2007-11-30 11:30:34 -05002395static u64 stripe_align(struct btrfs_root *root, u64 val)
2396{
2397 u64 mask = ((u64)root->stripesize - 1);
2398 u64 ret = (val + mask) & ~mask;
2399 return ret;
2400}
2401
Chris Masonfec577f2007-02-26 10:40:21 -05002402/*
2403 * walks the btree of allocated extents and find a hole of a given size.
2404 * The key ins is changed to record the hole:
2405 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -04002406 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -05002407 * ins->offset == number of blocks
2408 * Any available blocks before search_start are skipped.
2409 */
Chris Masond3977122009-01-05 21:25:51 -05002410static noinline int find_free_extent(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05002411 struct btrfs_root *orig_root,
2412 u64 num_bytes, u64 empty_size,
2413 u64 search_start, u64 search_end,
2414 u64 hint_byte, struct btrfs_key *ins,
2415 u64 exclude_start, u64 exclude_nr,
2416 int data)
Chris Masonfec577f2007-02-26 10:40:21 -05002417{
Josef Bacik80eb2342008-10-29 14:49:05 -04002418 int ret = 0;
Chris Masond3977122009-01-05 21:25:51 -05002419 struct btrfs_root *root = orig_root->fs_info->extent_root;
Chris Masondb945352007-10-15 16:15:53 -04002420 u64 total_needed = num_bytes;
Chris Mason239b14b2008-03-24 15:02:07 -04002421 u64 *last_ptr = NULL;
Chris Mason43662112008-11-07 09:06:11 -05002422 u64 last_wanted = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04002423 struct btrfs_block_group_cache *block_group = NULL;
Chris Mason0ef3e662008-05-24 14:04:53 -04002424 int chunk_alloc_done = 0;
Chris Mason239b14b2008-03-24 15:02:07 -04002425 int empty_cluster = 2 * 1024 * 1024;
Chris Mason0ef3e662008-05-24 14:04:53 -04002426 int allowed_chunk_alloc = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04002427 struct list_head *head = NULL, *cur = NULL;
2428 int loop = 0;
Chris Masonf5a31e12008-11-10 11:47:09 -05002429 int extra_loop = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04002430 struct btrfs_space_info *space_info;
Chris Masonfec577f2007-02-26 10:40:21 -05002431
Chris Masondb945352007-10-15 16:15:53 -04002432 WARN_ON(num_bytes < root->sectorsize);
Chris Masonb1a4d962007-04-04 15:27:52 -04002433 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
Josef Bacik80eb2342008-10-29 14:49:05 -04002434 ins->objectid = 0;
2435 ins->offset = 0;
Chris Masonb1a4d962007-04-04 15:27:52 -04002436
Chris Mason0ef3e662008-05-24 14:04:53 -04002437 if (orig_root->ref_cows || empty_size)
2438 allowed_chunk_alloc = 1;
2439
Chris Mason239b14b2008-03-24 15:02:07 -04002440 if (data & BTRFS_BLOCK_GROUP_METADATA) {
2441 last_ptr = &root->fs_info->last_alloc;
Chris Mason536ac8a2009-02-12 09:41:38 -05002442 if (!btrfs_test_opt(root, SSD))
2443 empty_cluster = 64 * 1024;
Chris Mason239b14b2008-03-24 15:02:07 -04002444 }
2445
Josef Bacik0f9dd462008-09-23 13:14:11 -04002446 if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD))
Chris Mason239b14b2008-03-24 15:02:07 -04002447 last_ptr = &root->fs_info->last_data_alloc;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002448
Chris Mason239b14b2008-03-24 15:02:07 -04002449 if (last_ptr) {
Chris Mason43662112008-11-07 09:06:11 -05002450 if (*last_ptr) {
Chris Mason239b14b2008-03-24 15:02:07 -04002451 hint_byte = *last_ptr;
Chris Mason43662112008-11-07 09:06:11 -05002452 last_wanted = *last_ptr;
2453 } else
Chris Mason239b14b2008-03-24 15:02:07 -04002454 empty_size += empty_cluster;
Chris Mason43662112008-11-07 09:06:11 -05002455 } else {
2456 empty_cluster = 0;
Chris Mason239b14b2008-03-24 15:02:07 -04002457 }
Chris Masona061fc82008-05-07 11:43:44 -04002458 search_start = max(search_start, first_logical_byte(root, 0));
Chris Mason239b14b2008-03-24 15:02:07 -04002459 search_start = max(search_start, hint_byte);
Chris Mason0b86a832008-03-24 15:01:56 -04002460
Chris Mason42e70e72008-11-07 18:17:11 -05002461 if (last_wanted && search_start != last_wanted) {
Chris Mason43662112008-11-07 09:06:11 -05002462 last_wanted = 0;
Chris Mason42e70e72008-11-07 18:17:11 -05002463 empty_size += empty_cluster;
2464 }
Chris Mason43662112008-11-07 09:06:11 -05002465
Chris Mason42e70e72008-11-07 18:17:11 -05002466 total_needed += empty_size;
Josef Bacik80eb2342008-10-29 14:49:05 -04002467 block_group = btrfs_lookup_block_group(root->fs_info, search_start);
Yan Zhengd899e052008-10-30 14:25:28 -04002468 if (!block_group)
2469 block_group = btrfs_lookup_first_block_group(root->fs_info,
2470 search_start);
Josef Bacik80eb2342008-10-29 14:49:05 -04002471 space_info = __find_space_info(root->fs_info, data);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002472
Josef Bacik80eb2342008-10-29 14:49:05 -04002473 down_read(&space_info->groups_sem);
2474 while (1) {
2475 struct btrfs_free_space *free_space;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002476 /*
Josef Bacik80eb2342008-10-29 14:49:05 -04002477 * the only way this happens if our hint points to a block
2478 * group thats not of the proper type, while looping this
2479 * should never happen
Josef Bacik0f9dd462008-09-23 13:14:11 -04002480 */
Chris Mason8a1413a2008-11-10 16:13:54 -05002481 if (empty_size)
2482 extra_loop = 1;
2483
Chris Mason42e70e72008-11-07 18:17:11 -05002484 if (!block_group)
2485 goto new_group_no_lock;
2486
Josef Bacikea6a4782008-11-20 12:16:16 -05002487 if (unlikely(!block_group->cached)) {
2488 mutex_lock(&block_group->cache_mutex);
2489 ret = cache_block_group(root, block_group);
2490 mutex_unlock(&block_group->cache_mutex);
2491 if (ret)
2492 break;
2493 }
2494
Josef Bacik25179202008-10-29 14:49:05 -04002495 mutex_lock(&block_group->alloc_mutex);
Josef Bacik80eb2342008-10-29 14:49:05 -04002496 if (unlikely(!block_group_bits(block_group, data)))
Josef Bacik0f9dd462008-09-23 13:14:11 -04002497 goto new_group;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002498
Josef Bacikea6a4782008-11-20 12:16:16 -05002499 if (unlikely(block_group->ro))
Josef Bacik80eb2342008-10-29 14:49:05 -04002500 goto new_group;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002501
Josef Bacik80eb2342008-10-29 14:49:05 -04002502 free_space = btrfs_find_free_space(block_group, search_start,
2503 total_needed);
2504 if (free_space) {
2505 u64 start = block_group->key.objectid;
2506 u64 end = block_group->key.objectid +
Josef Bacik0f9dd462008-09-23 13:14:11 -04002507 block_group->key.offset;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002508
Josef Bacik80eb2342008-10-29 14:49:05 -04002509 search_start = stripe_align(root, free_space->offset);
Chris Mason0b86a832008-03-24 15:01:56 -04002510
Josef Bacik80eb2342008-10-29 14:49:05 -04002511 /* move on to the next group */
2512 if (search_start + num_bytes >= search_end)
2513 goto new_group;
Chris Masone37c9e62007-05-09 20:13:14 -04002514
Josef Bacik80eb2342008-10-29 14:49:05 -04002515 /* move on to the next group */
2516 if (search_start + num_bytes > end)
2517 goto new_group;
2518
Chris Mason43662112008-11-07 09:06:11 -05002519 if (last_wanted && search_start != last_wanted) {
Chris Mason3b7885b2008-11-06 21:48:27 -05002520 total_needed += empty_cluster;
Chris Mason8a1413a2008-11-10 16:13:54 -05002521 empty_size += empty_cluster;
Chris Mason43662112008-11-07 09:06:11 -05002522 last_wanted = 0;
Chris Mason3b7885b2008-11-06 21:48:27 -05002523 /*
2524 * if search_start is still in this block group
2525 * then we just re-search this block group
2526 */
2527 if (search_start >= start &&
2528 search_start < end) {
2529 mutex_unlock(&block_group->alloc_mutex);
2530 continue;
2531 }
2532
2533 /* else we go to the next block group */
2534 goto new_group;
2535 }
2536
Josef Bacik80eb2342008-10-29 14:49:05 -04002537 if (exclude_nr > 0 &&
2538 (search_start + num_bytes > exclude_start &&
2539 search_start < exclude_start + exclude_nr)) {
2540 search_start = exclude_start + exclude_nr;
2541 /*
2542 * if search_start is still in this block group
2543 * then we just re-search this block group
2544 */
2545 if (search_start >= start &&
Josef Bacik25179202008-10-29 14:49:05 -04002546 search_start < end) {
2547 mutex_unlock(&block_group->alloc_mutex);
Chris Mason43662112008-11-07 09:06:11 -05002548 last_wanted = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04002549 continue;
Josef Bacik25179202008-10-29 14:49:05 -04002550 }
Josef Bacik80eb2342008-10-29 14:49:05 -04002551
2552 /* else we go to the next block group */
2553 goto new_group;
2554 }
2555
2556 ins->objectid = search_start;
2557 ins->offset = num_bytes;
Josef Bacik25179202008-10-29 14:49:05 -04002558
2559 btrfs_remove_free_space_lock(block_group, search_start,
2560 num_bytes);
Josef Bacik80eb2342008-10-29 14:49:05 -04002561 /* we are all good, lets return */
Josef Bacik25179202008-10-29 14:49:05 -04002562 mutex_unlock(&block_group->alloc_mutex);
Josef Bacik80eb2342008-10-29 14:49:05 -04002563 break;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002564 }
Josef Bacik80eb2342008-10-29 14:49:05 -04002565new_group:
Chris Mason42e70e72008-11-07 18:17:11 -05002566 mutex_unlock(&block_group->alloc_mutex);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002567 put_block_group(block_group);
2568 block_group = NULL;
Chris Mason42e70e72008-11-07 18:17:11 -05002569new_group_no_lock:
Chris Masonf5a31e12008-11-10 11:47:09 -05002570 /* don't try to compare new allocations against the
2571 * last allocation any more
2572 */
Chris Mason43662112008-11-07 09:06:11 -05002573 last_wanted = 0;
Chris Masonf5a31e12008-11-10 11:47:09 -05002574
Josef Bacik80eb2342008-10-29 14:49:05 -04002575 /*
2576 * Here's how this works.
2577 * loop == 0: we were searching a block group via a hint
2578 * and didn't find anything, so we start at
2579 * the head of the block groups and keep searching
2580 * loop == 1: we're searching through all of the block groups
2581 * if we hit the head again we have searched
2582 * all of the block groups for this space and we
2583 * need to try and allocate, if we cant error out.
2584 * loop == 2: we allocated more space and are looping through
2585 * all of the block groups again.
2586 */
2587 if (loop == 0) {
2588 head = &space_info->block_groups;
2589 cur = head->next;
Josef Bacik80eb2342008-10-29 14:49:05 -04002590 loop++;
2591 } else if (loop == 1 && cur == head) {
Chris Masonf5a31e12008-11-10 11:47:09 -05002592 int keep_going;
Chris Mason42e70e72008-11-07 18:17:11 -05002593
Chris Masonf5a31e12008-11-10 11:47:09 -05002594 /* at this point we give up on the empty_size
2595 * allocations and just try to allocate the min
2596 * space.
2597 *
2598 * The extra_loop field was set if an empty_size
2599 * allocation was attempted above, and if this
2600 * is try we need to try the loop again without
2601 * the additional empty_size.
2602 */
Chris Mason5b7c3fc2008-11-10 07:26:33 -05002603 total_needed -= empty_size;
2604 empty_size = 0;
Chris Masonf5a31e12008-11-10 11:47:09 -05002605 keep_going = extra_loop;
2606 loop++;
Chris Mason42e70e72008-11-07 18:17:11 -05002607
Josef Bacik80eb2342008-10-29 14:49:05 -04002608 if (allowed_chunk_alloc && !chunk_alloc_done) {
2609 up_read(&space_info->groups_sem);
2610 ret = do_chunk_alloc(trans, root, num_bytes +
2611 2 * 1024 * 1024, data, 1);
Josef Bacik80eb2342008-10-29 14:49:05 -04002612 down_read(&space_info->groups_sem);
Chris Mason2ed6d662008-11-13 09:59:33 -05002613 if (ret < 0)
2614 goto loop_check;
Josef Bacik80eb2342008-10-29 14:49:05 -04002615 head = &space_info->block_groups;
Chris Masonf5a31e12008-11-10 11:47:09 -05002616 /*
2617 * we've allocated a new chunk, keep
2618 * trying
2619 */
2620 keep_going = 1;
Josef Bacik80eb2342008-10-29 14:49:05 -04002621 chunk_alloc_done = 1;
2622 } else if (!allowed_chunk_alloc) {
2623 space_info->force_alloc = 1;
Chris Masonf5a31e12008-11-10 11:47:09 -05002624 }
Chris Mason2ed6d662008-11-13 09:59:33 -05002625loop_check:
Chris Masonf5a31e12008-11-10 11:47:09 -05002626 if (keep_going) {
2627 cur = head->next;
2628 extra_loop = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04002629 } else {
2630 break;
2631 }
2632 } else if (cur == head) {
2633 break;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002634 }
Josef Bacik80eb2342008-10-29 14:49:05 -04002635
2636 block_group = list_entry(cur, struct btrfs_block_group_cache,
2637 list);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002638 atomic_inc(&block_group->count);
2639
Josef Bacik80eb2342008-10-29 14:49:05 -04002640 search_start = block_group->key.objectid;
2641 cur = cur->next;
Chris Masone19caa52007-10-15 16:17:44 -04002642 }
Chris Mason0b86a832008-03-24 15:01:56 -04002643
Josef Bacik80eb2342008-10-29 14:49:05 -04002644 /* we found what we needed */
2645 if (ins->objectid) {
2646 if (!(data & BTRFS_BLOCK_GROUP_DATA))
Yan Zhengd2fb3432008-12-11 16:30:39 -05002647 trans->block_group = block_group->key.objectid;
Josef Bacik80eb2342008-10-29 14:49:05 -04002648
2649 if (last_ptr)
2650 *last_ptr = ins->objectid + ins->offset;
2651 ret = 0;
2652 } else if (!ret) {
Chris Masond3977122009-01-05 21:25:51 -05002653 printk(KERN_ERR "btrfs searching for %llu bytes, "
2654 "num_bytes %llu, loop %d, allowed_alloc %d\n",
2655 (unsigned long long)total_needed,
2656 (unsigned long long)num_bytes,
Josef Bacik4ce4cb52008-11-17 21:12:00 -05002657 loop, allowed_chunk_alloc);
Josef Bacik80eb2342008-10-29 14:49:05 -04002658 ret = -ENOSPC;
Chris Masonf2654de2007-06-26 12:20:46 -04002659 }
Yan Zhengd2fb3432008-12-11 16:30:39 -05002660 if (block_group)
2661 put_block_group(block_group);
Chris Mason0b86a832008-03-24 15:01:56 -04002662
Josef Bacik80eb2342008-10-29 14:49:05 -04002663 up_read(&space_info->groups_sem);
Chris Mason0f70abe2007-02-28 16:46:22 -05002664 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05002665}
Chris Masonec44a352008-04-28 15:29:52 -04002666
Josef Bacik0f9dd462008-09-23 13:14:11 -04002667static void dump_space_info(struct btrfs_space_info *info, u64 bytes)
2668{
2669 struct btrfs_block_group_cache *cache;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002670
Chris Masond3977122009-01-05 21:25:51 -05002671 printk(KERN_INFO "space_info has %llu free, is %sfull\n",
2672 (unsigned long long)(info->total_bytes - info->bytes_used -
2673 info->bytes_pinned - info->bytes_reserved),
2674 (info->full) ? "" : "not ");
Josef Bacik6a632092009-02-20 11:00:09 -05002675 printk(KERN_INFO "space_info total=%llu, pinned=%llu, delalloc=%llu,"
2676 " may_use=%llu, used=%llu\n", info->total_bytes,
2677 info->bytes_pinned, info->bytes_delalloc, info->bytes_may_use,
2678 info->bytes_used);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002679
Josef Bacik80eb2342008-10-29 14:49:05 -04002680 down_read(&info->groups_sem);
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002681 list_for_each_entry(cache, &info->block_groups, list) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04002682 spin_lock(&cache->lock);
Chris Masond3977122009-01-05 21:25:51 -05002683 printk(KERN_INFO "block group %llu has %llu bytes, %llu used "
2684 "%llu pinned %llu reserved\n",
2685 (unsigned long long)cache->key.objectid,
2686 (unsigned long long)cache->key.offset,
2687 (unsigned long long)btrfs_block_group_used(&cache->item),
2688 (unsigned long long)cache->pinned,
2689 (unsigned long long)cache->reserved);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002690 btrfs_dump_free_space(cache, bytes);
2691 spin_unlock(&cache->lock);
2692 }
Josef Bacik80eb2342008-10-29 14:49:05 -04002693 up_read(&info->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002694}
Zheng Yane8569812008-09-26 10:05:48 -04002695
Chris Masone6dcd2d2008-07-17 12:53:50 -04002696static int __btrfs_reserve_extent(struct btrfs_trans_handle *trans,
2697 struct btrfs_root *root,
2698 u64 num_bytes, u64 min_alloc_size,
2699 u64 empty_size, u64 hint_byte,
2700 u64 search_end, struct btrfs_key *ins,
2701 u64 data)
Chris Masonfec577f2007-02-26 10:40:21 -05002702{
2703 int ret;
Chris Masonfbdc7622007-05-30 10:22:12 -04002704 u64 search_start = 0;
Chris Mason1261ec42007-03-20 20:35:03 -04002705 struct btrfs_fs_info *info = root->fs_info;
Chris Mason925baed2008-06-25 16:01:30 -04002706
Josef Bacik6a632092009-02-20 11:00:09 -05002707 data = btrfs_get_alloc_profile(root, data);
Chris Mason98d20f62008-04-14 09:46:10 -04002708again:
Chris Mason0ef3e662008-05-24 14:04:53 -04002709 /*
2710 * the only place that sets empty_size is btrfs_realloc_node, which
2711 * is not called recursively on allocations
2712 */
2713 if (empty_size || root->ref_cows) {
Chris Mason593060d2008-03-25 16:50:33 -04002714 if (!(data & BTRFS_BLOCK_GROUP_METADATA)) {
Chris Mason6324fbf2008-03-24 15:01:59 -04002715 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0ef3e662008-05-24 14:04:53 -04002716 2 * 1024 * 1024,
2717 BTRFS_BLOCK_GROUP_METADATA |
2718 (info->metadata_alloc_profile &
2719 info->avail_metadata_alloc_bits), 0);
Chris Mason6324fbf2008-03-24 15:01:59 -04002720 }
2721 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0ef3e662008-05-24 14:04:53 -04002722 num_bytes + 2 * 1024 * 1024, data, 0);
Chris Mason6324fbf2008-03-24 15:01:59 -04002723 }
Chris Mason0b86a832008-03-24 15:01:56 -04002724
Chris Masondb945352007-10-15 16:15:53 -04002725 WARN_ON(num_bytes < root->sectorsize);
2726 ret = find_free_extent(trans, root, num_bytes, empty_size,
2727 search_start, search_end, hint_byte, ins,
Chris Mason26b80032007-08-08 20:17:12 -04002728 trans->alloc_exclude_start,
2729 trans->alloc_exclude_nr, data);
Chris Mason3b951512008-04-17 11:29:12 -04002730
Chris Mason98d20f62008-04-14 09:46:10 -04002731 if (ret == -ENOSPC && num_bytes > min_alloc_size) {
2732 num_bytes = num_bytes >> 1;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002733 num_bytes = num_bytes & ~(root->sectorsize - 1);
Chris Mason98d20f62008-04-14 09:46:10 -04002734 num_bytes = max(num_bytes, min_alloc_size);
Chris Mason0ef3e662008-05-24 14:04:53 -04002735 do_chunk_alloc(trans, root->fs_info->extent_root,
2736 num_bytes, data, 1);
Chris Mason98d20f62008-04-14 09:46:10 -04002737 goto again;
2738 }
Chris Masonec44a352008-04-28 15:29:52 -04002739 if (ret) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04002740 struct btrfs_space_info *sinfo;
2741
2742 sinfo = __find_space_info(root->fs_info, data);
Chris Masond3977122009-01-05 21:25:51 -05002743 printk(KERN_ERR "btrfs allocation failed flags %llu, "
2744 "wanted %llu\n", (unsigned long long)data,
2745 (unsigned long long)num_bytes);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002746 dump_space_info(sinfo, num_bytes);
Chris Mason925baed2008-06-25 16:01:30 -04002747 BUG();
Chris Mason925baed2008-06-25 16:01:30 -04002748 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002749
2750 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002751}
2752
Chris Mason65b51a02008-08-01 15:11:20 -04002753int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len)
2754{
Josef Bacik0f9dd462008-09-23 13:14:11 -04002755 struct btrfs_block_group_cache *cache;
Liu Hui1f3c79a2009-01-05 15:57:51 -05002756 int ret = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002757
Josef Bacik0f9dd462008-09-23 13:14:11 -04002758 cache = btrfs_lookup_block_group(root->fs_info, start);
2759 if (!cache) {
Chris Masond3977122009-01-05 21:25:51 -05002760 printk(KERN_ERR "Unable to find block group for %llu\n",
2761 (unsigned long long)start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002762 return -ENOSPC;
2763 }
Liu Hui1f3c79a2009-01-05 15:57:51 -05002764
2765 ret = btrfs_discard_extent(root, start, len);
2766
Josef Bacik0f9dd462008-09-23 13:14:11 -04002767 btrfs_add_free_space(cache, start, len);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002768 put_block_group(cache);
Zheng Yan1a40e232008-09-26 10:09:34 -04002769 update_reserved_extents(root, start, len, 0);
Liu Hui1f3c79a2009-01-05 15:57:51 -05002770
2771 return ret;
Chris Mason65b51a02008-08-01 15:11:20 -04002772}
2773
Chris Masone6dcd2d2008-07-17 12:53:50 -04002774int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
2775 struct btrfs_root *root,
2776 u64 num_bytes, u64 min_alloc_size,
2777 u64 empty_size, u64 hint_byte,
2778 u64 search_end, struct btrfs_key *ins,
2779 u64 data)
2780{
2781 int ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002782 ret = __btrfs_reserve_extent(trans, root, num_bytes, min_alloc_size,
2783 empty_size, hint_byte, search_end, ins,
2784 data);
Zheng Yane8569812008-09-26 10:05:48 -04002785 update_reserved_extents(root, ins->objectid, ins->offset, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002786 return ret;
2787}
2788
2789static int __btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04002790 struct btrfs_root *root, u64 parent,
Chris Masone6dcd2d2008-07-17 12:53:50 -04002791 u64 root_objectid, u64 ref_generation,
Chris Mason56bec292009-03-13 10:10:06 -04002792 u64 owner, struct btrfs_key *ins,
2793 int ref_mod)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002794{
2795 int ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002796 u64 super_used;
2797 u64 root_used;
2798 u64 num_bytes = ins->offset;
2799 u32 sizes[2];
2800 struct btrfs_fs_info *info = root->fs_info;
2801 struct btrfs_root *extent_root = info->extent_root;
2802 struct btrfs_extent_item *extent_item;
2803 struct btrfs_extent_ref *ref;
2804 struct btrfs_path *path;
2805 struct btrfs_key keys[2];
Chris Masonf2654de2007-06-26 12:20:46 -04002806
Zheng Yan31840ae2008-09-23 13:14:14 -04002807 if (parent == 0)
2808 parent = ins->objectid;
2809
Josef Bacik58176a92007-08-29 15:47:34 -04002810 /* block accounting for super block */
Chris Mason75eff682008-12-15 15:54:40 -05002811 spin_lock(&info->delalloc_lock);
Chris Masondb945352007-10-15 16:15:53 -04002812 super_used = btrfs_super_bytes_used(&info->super_copy);
2813 btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes);
Chris Mason26b80032007-08-08 20:17:12 -04002814
Josef Bacik58176a92007-08-29 15:47:34 -04002815 /* block accounting for root item */
Chris Masondb945352007-10-15 16:15:53 -04002816 root_used = btrfs_root_used(&root->root_item);
2817 btrfs_set_root_used(&root->root_item, root_used + num_bytes);
Yan Zheng34bf63c2008-12-19 10:58:46 -05002818 spin_unlock(&info->delalloc_lock);
Josef Bacik58176a92007-08-29 15:47:34 -04002819
Chris Mason47e4bb92008-02-01 14:51:59 -05002820 memcpy(&keys[0], ins, sizeof(*ins));
Chris Mason47e4bb92008-02-01 14:51:59 -05002821 keys[1].objectid = ins->objectid;
2822 keys[1].type = BTRFS_EXTENT_REF_KEY;
Zheng Yan31840ae2008-09-23 13:14:14 -04002823 keys[1].offset = parent;
Chris Mason47e4bb92008-02-01 14:51:59 -05002824 sizes[0] = sizeof(*extent_item);
2825 sizes[1] = sizeof(*ref);
Chris Mason7bb86312007-12-11 09:25:06 -05002826
2827 path = btrfs_alloc_path();
2828 BUG_ON(!path);
Chris Mason47e4bb92008-02-01 14:51:59 -05002829
2830 ret = btrfs_insert_empty_items(trans, extent_root, path, keys,
2831 sizes, 2);
Chris Masonccd467d2007-06-28 15:57:36 -04002832 BUG_ON(ret);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002833
Chris Mason47e4bb92008-02-01 14:51:59 -05002834 extent_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2835 struct btrfs_extent_item);
Chris Mason56bec292009-03-13 10:10:06 -04002836 btrfs_set_extent_refs(path->nodes[0], extent_item, ref_mod);
Chris Mason47e4bb92008-02-01 14:51:59 -05002837 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
2838 struct btrfs_extent_ref);
2839
2840 btrfs_set_ref_root(path->nodes[0], ref, root_objectid);
2841 btrfs_set_ref_generation(path->nodes[0], ref, ref_generation);
2842 btrfs_set_ref_objectid(path->nodes[0], ref, owner);
Chris Mason56bec292009-03-13 10:10:06 -04002843 btrfs_set_ref_num_refs(path->nodes[0], ref, ref_mod);
Chris Mason47e4bb92008-02-01 14:51:59 -05002844
2845 btrfs_mark_buffer_dirty(path->nodes[0]);
2846
2847 trans->alloc_exclude_start = 0;
2848 trans->alloc_exclude_nr = 0;
Chris Mason7bb86312007-12-11 09:25:06 -05002849 btrfs_free_path(path);
Chris Masonf510cfe2007-10-15 16:14:48 -04002850
Chris Mason925baed2008-06-25 16:01:30 -04002851 if (ret)
2852 goto out;
Chris Mason26b80032007-08-08 20:17:12 -04002853
Chris Masond3977122009-01-05 21:25:51 -05002854 ret = update_block_group(trans, root, ins->objectid,
2855 ins->offset, 1, 0);
Chris Masonf5947062008-02-04 10:10:13 -05002856 if (ret) {
Chris Masond3977122009-01-05 21:25:51 -05002857 printk(KERN_ERR "btrfs update block group failed for %llu "
2858 "%llu\n", (unsigned long long)ins->objectid,
2859 (unsigned long long)ins->offset);
Chris Masonf5947062008-02-04 10:10:13 -05002860 BUG();
2861 }
Chris Mason925baed2008-06-25 16:01:30 -04002862out:
Chris Masone6dcd2d2008-07-17 12:53:50 -04002863 return ret;
2864}
2865
2866int btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04002867 struct btrfs_root *root, u64 parent,
Chris Masone6dcd2d2008-07-17 12:53:50 -04002868 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002869 u64 owner, struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002870{
2871 int ret;
Chris Mason1c2308f2008-09-23 13:14:13 -04002872
2873 if (root_objectid == BTRFS_TREE_LOG_OBJECTID)
2874 return 0;
Chris Mason56bec292009-03-13 10:10:06 -04002875
2876 ret = btrfs_add_delayed_ref(trans, ins->objectid,
2877 ins->offset, parent, root_objectid,
2878 ref_generation, owner,
2879 BTRFS_ADD_DELAYED_EXTENT, 0);
2880 BUG_ON(ret);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002881 return ret;
2882}
Chris Masone02119d2008-09-05 16:13:11 -04002883
2884/*
2885 * this is used by the tree logging recovery code. It records that
2886 * an extent has been allocated and makes sure to clear the free
2887 * space cache bits as well
2888 */
2889int btrfs_alloc_logged_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04002890 struct btrfs_root *root, u64 parent,
Chris Masone02119d2008-09-05 16:13:11 -04002891 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002892 u64 owner, struct btrfs_key *ins)
Chris Masone02119d2008-09-05 16:13:11 -04002893{
2894 int ret;
2895 struct btrfs_block_group_cache *block_group;
2896
Chris Masone02119d2008-09-05 16:13:11 -04002897 block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
Josef Bacikea6a4782008-11-20 12:16:16 -05002898 mutex_lock(&block_group->cache_mutex);
Chris Masone02119d2008-09-05 16:13:11 -04002899 cache_block_group(root, block_group);
Josef Bacikea6a4782008-11-20 12:16:16 -05002900 mutex_unlock(&block_group->cache_mutex);
Chris Masone02119d2008-09-05 16:13:11 -04002901
Josef Bacikea6a4782008-11-20 12:16:16 -05002902 ret = btrfs_remove_free_space(block_group, ins->objectid,
2903 ins->offset);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002904 BUG_ON(ret);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002905 put_block_group(block_group);
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002906 ret = __btrfs_alloc_reserved_extent(trans, root, parent, root_objectid,
Chris Mason56bec292009-03-13 10:10:06 -04002907 ref_generation, owner, ins, 1);
Chris Masone02119d2008-09-05 16:13:11 -04002908 return ret;
2909}
2910
Chris Masone6dcd2d2008-07-17 12:53:50 -04002911/*
2912 * finds a free extent and does all the dirty work required for allocation
2913 * returns the key for the extent through ins, and a tree buffer for
2914 * the first block of the extent through buf.
2915 *
2916 * returns 0 if everything worked, non-zero otherwise.
2917 */
2918int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
2919 struct btrfs_root *root,
Zheng Yan31840ae2008-09-23 13:14:14 -04002920 u64 num_bytes, u64 parent, u64 min_alloc_size,
Chris Masone6dcd2d2008-07-17 12:53:50 -04002921 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002922 u64 owner_objectid, u64 empty_size, u64 hint_byte,
Chris Masone6dcd2d2008-07-17 12:53:50 -04002923 u64 search_end, struct btrfs_key *ins, u64 data)
2924{
2925 int ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002926 ret = __btrfs_reserve_extent(trans, root, num_bytes,
2927 min_alloc_size, empty_size, hint_byte,
2928 search_end, ins, data);
2929 BUG_ON(ret);
Chris Masond00aff02008-09-11 15:54:42 -04002930 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
Chris Mason56bec292009-03-13 10:10:06 -04002931 ret = btrfs_add_delayed_ref(trans, ins->objectid,
2932 ins->offset, parent, root_objectid,
2933 ref_generation, owner_objectid,
2934 BTRFS_ADD_DELAYED_EXTENT, 0);
Chris Masond00aff02008-09-11 15:54:42 -04002935 BUG_ON(ret);
Chris Masond00aff02008-09-11 15:54:42 -04002936 }
Chris Mason56bec292009-03-13 10:10:06 -04002937 update_reserved_extents(root, ins->objectid, ins->offset, 1);
Chris Mason925baed2008-06-25 16:01:30 -04002938 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05002939}
Chris Mason65b51a02008-08-01 15:11:20 -04002940
2941struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
2942 struct btrfs_root *root,
Chris Mason4008c042009-02-12 14:09:45 -05002943 u64 bytenr, u32 blocksize,
2944 int level)
Chris Mason65b51a02008-08-01 15:11:20 -04002945{
2946 struct extent_buffer *buf;
2947
2948 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
2949 if (!buf)
2950 return ERR_PTR(-ENOMEM);
2951 btrfs_set_header_generation(buf, trans->transid);
Chris Mason4008c042009-02-12 14:09:45 -05002952 btrfs_set_buffer_lockdep_class(buf, level);
Chris Mason65b51a02008-08-01 15:11:20 -04002953 btrfs_tree_lock(buf);
2954 clean_tree_block(trans, root, buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002955
2956 btrfs_set_lock_blocking(buf);
Chris Mason65b51a02008-08-01 15:11:20 -04002957 btrfs_set_buffer_uptodate(buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002958
Chris Masond0c803c2008-09-11 16:17:57 -04002959 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
2960 set_extent_dirty(&root->dirty_log_pages, buf->start,
Chris Mason65b51a02008-08-01 15:11:20 -04002961 buf->start + buf->len - 1, GFP_NOFS);
Chris Masond0c803c2008-09-11 16:17:57 -04002962 } else {
2963 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
2964 buf->start + buf->len - 1, GFP_NOFS);
2965 }
Chris Mason65b51a02008-08-01 15:11:20 -04002966 trans->blocks_used++;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002967 /* this returns a buffer locked for blocking */
Chris Mason65b51a02008-08-01 15:11:20 -04002968 return buf;
2969}
2970
Chris Masonfec577f2007-02-26 10:40:21 -05002971/*
2972 * helper function to allocate a block for a given tree
2973 * returns the tree buffer or NULL.
2974 */
Chris Mason5f39d392007-10-15 16:14:19 -04002975struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Chris Masondb945352007-10-15 16:15:53 -04002976 struct btrfs_root *root,
Zheng Yan31840ae2008-09-23 13:14:14 -04002977 u32 blocksize, u64 parent,
Chris Mason7bb86312007-12-11 09:25:06 -05002978 u64 root_objectid,
2979 u64 ref_generation,
Chris Mason7bb86312007-12-11 09:25:06 -05002980 int level,
2981 u64 hint,
Chris Mason5f39d392007-10-15 16:14:19 -04002982 u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05002983{
Chris Masone2fa7222007-03-12 16:22:34 -04002984 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -05002985 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04002986 struct extent_buffer *buf;
Chris Masonfec577f2007-02-26 10:40:21 -05002987
Zheng Yan31840ae2008-09-23 13:14:14 -04002988 ret = btrfs_alloc_extent(trans, root, blocksize, parent, blocksize,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002989 root_objectid, ref_generation, level,
Zheng Yan31840ae2008-09-23 13:14:14 -04002990 empty_size, hint, (u64)-1, &ins, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05002991 if (ret) {
Chris Mason54aa1f42007-06-22 14:16:25 -04002992 BUG_ON(ret > 0);
2993 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05002994 }
Chris Mason55c69072008-01-09 15:55:33 -05002995
Chris Mason4008c042009-02-12 14:09:45 -05002996 buf = btrfs_init_new_buffer(trans, root, ins.objectid,
2997 blocksize, level);
Chris Masonfec577f2007-02-26 10:40:21 -05002998 return buf;
2999}
Chris Masona28ec192007-03-06 20:08:01 -05003000
Chris Masone02119d2008-09-05 16:13:11 -04003001int btrfs_drop_leaf_ref(struct btrfs_trans_handle *trans,
3002 struct btrfs_root *root, struct extent_buffer *leaf)
Chris Mason6407bf62007-03-27 06:33:00 -04003003{
Chris Mason7bb86312007-12-11 09:25:06 -05003004 u64 leaf_owner;
3005 u64 leaf_generation;
Chris Masonbd56b302009-02-04 09:27:02 -05003006 struct refsort *sorted;
Chris Mason5f39d392007-10-15 16:14:19 -04003007 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -04003008 struct btrfs_file_extent_item *fi;
3009 int i;
3010 int nritems;
3011 int ret;
Chris Masonbd56b302009-02-04 09:27:02 -05003012 int refi = 0;
3013 int slot;
Chris Mason6407bf62007-03-27 06:33:00 -04003014
Chris Mason5f39d392007-10-15 16:14:19 -04003015 BUG_ON(!btrfs_is_leaf(leaf));
3016 nritems = btrfs_header_nritems(leaf);
Chris Mason7bb86312007-12-11 09:25:06 -05003017 leaf_owner = btrfs_header_owner(leaf);
3018 leaf_generation = btrfs_header_generation(leaf);
3019
Chris Masonbd56b302009-02-04 09:27:02 -05003020 sorted = kmalloc(sizeof(*sorted) * nritems, GFP_NOFS);
3021 /* we do this loop twice. The first time we build a list
3022 * of the extents we have a reference on, then we sort the list
3023 * by bytenr. The second time around we actually do the
3024 * extent freeing.
3025 */
Chris Mason6407bf62007-03-27 06:33:00 -04003026 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04003027 u64 disk_bytenr;
Chris Masone34a5b42008-07-22 12:08:37 -04003028 cond_resched();
Chris Mason5f39d392007-10-15 16:14:19 -04003029
3030 btrfs_item_key_to_cpu(leaf, &key, i);
Chris Masonbd56b302009-02-04 09:27:02 -05003031
3032 /* only extents have references, skip everything else */
Chris Mason5f39d392007-10-15 16:14:19 -04003033 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason6407bf62007-03-27 06:33:00 -04003034 continue;
Chris Masonbd56b302009-02-04 09:27:02 -05003035
Chris Mason6407bf62007-03-27 06:33:00 -04003036 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
Chris Masonbd56b302009-02-04 09:27:02 -05003037
3038 /* inline extents live in the btree, they don't have refs */
Chris Mason5f39d392007-10-15 16:14:19 -04003039 if (btrfs_file_extent_type(leaf, fi) ==
3040 BTRFS_FILE_EXTENT_INLINE)
Chris Mason236454d2007-04-19 13:37:44 -04003041 continue;
Chris Masonbd56b302009-02-04 09:27:02 -05003042
Chris Masondb945352007-10-15 16:15:53 -04003043 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Chris Masonbd56b302009-02-04 09:27:02 -05003044
3045 /* holes don't have refs */
Chris Masondb945352007-10-15 16:15:53 -04003046 if (disk_bytenr == 0)
Chris Mason3a686372007-05-24 13:35:57 -04003047 continue;
Chris Mason4a096752008-07-21 10:29:44 -04003048
Chris Masonbd56b302009-02-04 09:27:02 -05003049 sorted[refi].bytenr = disk_bytenr;
3050 sorted[refi].slot = i;
3051 refi++;
3052 }
3053
3054 if (refi == 0)
3055 goto out;
3056
3057 sort(sorted, refi, sizeof(struct refsort), refsort_cmp, NULL);
3058
3059 for (i = 0; i < refi; i++) {
3060 u64 disk_bytenr;
3061
3062 disk_bytenr = sorted[i].bytenr;
3063 slot = sorted[i].slot;
3064
3065 cond_resched();
3066
3067 btrfs_item_key_to_cpu(leaf, &key, slot);
3068 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
3069 continue;
3070
3071 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
3072
Chris Mason56bec292009-03-13 10:10:06 -04003073 ret = btrfs_free_extent(trans, root, disk_bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -05003074 btrfs_file_extent_disk_num_bytes(leaf, fi),
Zheng Yan31840ae2008-09-23 13:14:14 -04003075 leaf->start, leaf_owner, leaf_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003076 key.objectid, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04003077 BUG_ON(ret);
Chris Mason2dd3e672008-08-04 08:20:15 -04003078
3079 atomic_inc(&root->fs_info->throttle_gen);
3080 wake_up(&root->fs_info->transaction_throttle);
3081 cond_resched();
Chris Mason6407bf62007-03-27 06:33:00 -04003082 }
Chris Masonbd56b302009-02-04 09:27:02 -05003083out:
3084 kfree(sorted);
Chris Mason6407bf62007-03-27 06:33:00 -04003085 return 0;
3086}
3087
Chris Masond3977122009-01-05 21:25:51 -05003088static noinline int cache_drop_leaf_ref(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04003089 struct btrfs_root *root,
3090 struct btrfs_leaf_ref *ref)
Yan Zheng31153d82008-07-28 15:32:19 -04003091{
3092 int i;
3093 int ret;
Chris Masonbd56b302009-02-04 09:27:02 -05003094 struct btrfs_extent_info *info;
3095 struct refsort *sorted;
Yan Zheng31153d82008-07-28 15:32:19 -04003096
Chris Masonbd56b302009-02-04 09:27:02 -05003097 if (ref->nritems == 0)
3098 return 0;
3099
3100 sorted = kmalloc(sizeof(*sorted) * ref->nritems, GFP_NOFS);
Yan Zheng31153d82008-07-28 15:32:19 -04003101 for (i = 0; i < ref->nritems; i++) {
Chris Masonbd56b302009-02-04 09:27:02 -05003102 sorted[i].bytenr = ref->extents[i].bytenr;
3103 sorted[i].slot = i;
3104 }
3105 sort(sorted, ref->nritems, sizeof(struct refsort), refsort_cmp, NULL);
3106
3107 /*
3108 * the items in the ref were sorted when the ref was inserted
3109 * into the ref cache, so this is already in order
3110 */
3111 for (i = 0; i < ref->nritems; i++) {
3112 info = ref->extents + sorted[i].slot;
Chris Mason56bec292009-03-13 10:10:06 -04003113 ret = btrfs_free_extent(trans, root, info->bytenr,
Zheng Yan31840ae2008-09-23 13:14:14 -04003114 info->num_bytes, ref->bytenr,
3115 ref->owner, ref->generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003116 info->objectid, 0);
Chris Mason2dd3e672008-08-04 08:20:15 -04003117
3118 atomic_inc(&root->fs_info->throttle_gen);
3119 wake_up(&root->fs_info->transaction_throttle);
3120 cond_resched();
3121
Yan Zheng31153d82008-07-28 15:32:19 -04003122 BUG_ON(ret);
3123 info++;
3124 }
Yan Zheng31153d82008-07-28 15:32:19 -04003125
Chris Mason806638b2009-02-05 09:08:14 -05003126 kfree(sorted);
Yan Zheng31153d82008-07-28 15:32:19 -04003127 return 0;
3128}
3129
Chris Mason56bec292009-03-13 10:10:06 -04003130static int drop_snap_lookup_refcount(struct btrfs_trans_handle *trans,
3131 struct btrfs_root *root, u64 start,
Chris Masond3977122009-01-05 21:25:51 -05003132 u64 len, u32 *refs)
Chris Mason333db942008-06-25 16:01:30 -04003133{
Chris Mason017e5362008-07-28 15:32:51 -04003134 int ret;
Chris Masonf87f0572008-08-01 11:27:23 -04003135
Chris Mason56bec292009-03-13 10:10:06 -04003136 ret = btrfs_lookup_extent_ref(trans, root, start, len, refs);
Chris Masonf87f0572008-08-01 11:27:23 -04003137 BUG_ON(ret);
3138
Chris Masond3977122009-01-05 21:25:51 -05003139#if 0 /* some debugging code in case we see problems here */
Chris Masonf87f0572008-08-01 11:27:23 -04003140 /* if the refs count is one, it won't get increased again. But
3141 * if the ref count is > 1, someone may be decreasing it at
3142 * the same time we are.
3143 */
3144 if (*refs != 1) {
3145 struct extent_buffer *eb = NULL;
3146 eb = btrfs_find_create_tree_block(root, start, len);
3147 if (eb)
3148 btrfs_tree_lock(eb);
3149
3150 mutex_lock(&root->fs_info->alloc_mutex);
3151 ret = lookup_extent_ref(NULL, root, start, len, refs);
3152 BUG_ON(ret);
3153 mutex_unlock(&root->fs_info->alloc_mutex);
3154
3155 if (eb) {
3156 btrfs_tree_unlock(eb);
3157 free_extent_buffer(eb);
3158 }
3159 if (*refs == 1) {
Chris Masond3977122009-01-05 21:25:51 -05003160 printk(KERN_ERR "btrfs block %llu went down to one "
3161 "during drop_snap\n", (unsigned long long)start);
Chris Masonf87f0572008-08-01 11:27:23 -04003162 }
3163
3164 }
3165#endif
3166
Chris Masone7a84562008-06-25 16:01:31 -04003167 cond_resched();
Chris Mason017e5362008-07-28 15:32:51 -04003168 return ret;
Chris Mason333db942008-06-25 16:01:30 -04003169}
3170
3171/*
Chris Masonbd56b302009-02-04 09:27:02 -05003172 * this is used while deleting old snapshots, and it drops the refs
3173 * on a whole subtree starting from a level 1 node.
3174 *
3175 * The idea is to sort all the leaf pointers, and then drop the
3176 * ref on all the leaves in order. Most of the time the leaves
3177 * will have ref cache entries, so no leaf IOs will be required to
3178 * find the extents they have references on.
3179 *
3180 * For each leaf, any references it has are also dropped in order
3181 *
3182 * This ends up dropping the references in something close to optimal
3183 * order for reading and modifying the extent allocation tree.
3184 */
3185static noinline int drop_level_one_refs(struct btrfs_trans_handle *trans,
3186 struct btrfs_root *root,
3187 struct btrfs_path *path)
3188{
3189 u64 bytenr;
3190 u64 root_owner;
3191 u64 root_gen;
3192 struct extent_buffer *eb = path->nodes[1];
3193 struct extent_buffer *leaf;
3194 struct btrfs_leaf_ref *ref;
3195 struct refsort *sorted = NULL;
3196 int nritems = btrfs_header_nritems(eb);
3197 int ret;
3198 int i;
3199 int refi = 0;
3200 int slot = path->slots[1];
3201 u32 blocksize = btrfs_level_size(root, 0);
3202 u32 refs;
3203
3204 if (nritems == 0)
3205 goto out;
3206
3207 root_owner = btrfs_header_owner(eb);
3208 root_gen = btrfs_header_generation(eb);
3209 sorted = kmalloc(sizeof(*sorted) * nritems, GFP_NOFS);
3210
3211 /*
3212 * step one, sort all the leaf pointers so we don't scribble
3213 * randomly into the extent allocation tree
3214 */
3215 for (i = slot; i < nritems; i++) {
3216 sorted[refi].bytenr = btrfs_node_blockptr(eb, i);
3217 sorted[refi].slot = i;
3218 refi++;
3219 }
3220
3221 /*
3222 * nritems won't be zero, but if we're picking up drop_snapshot
3223 * after a crash, slot might be > 0, so double check things
3224 * just in case.
3225 */
3226 if (refi == 0)
3227 goto out;
3228
3229 sort(sorted, refi, sizeof(struct refsort), refsort_cmp, NULL);
3230
3231 /*
3232 * the first loop frees everything the leaves point to
3233 */
3234 for (i = 0; i < refi; i++) {
3235 u64 ptr_gen;
3236
3237 bytenr = sorted[i].bytenr;
3238
3239 /*
3240 * check the reference count on this leaf. If it is > 1
3241 * we just decrement it below and don't update any
3242 * of the refs the leaf points to.
3243 */
Chris Mason56bec292009-03-13 10:10:06 -04003244 ret = drop_snap_lookup_refcount(trans, root, bytenr,
3245 blocksize, &refs);
Chris Masonbd56b302009-02-04 09:27:02 -05003246 BUG_ON(ret);
3247 if (refs != 1)
3248 continue;
3249
3250 ptr_gen = btrfs_node_ptr_generation(eb, sorted[i].slot);
3251
3252 /*
3253 * the leaf only had one reference, which means the
3254 * only thing pointing to this leaf is the snapshot
3255 * we're deleting. It isn't possible for the reference
3256 * count to increase again later
3257 *
3258 * The reference cache is checked for the leaf,
3259 * and if found we'll be able to drop any refs held by
3260 * the leaf without needing to read it in.
3261 */
3262 ref = btrfs_lookup_leaf_ref(root, bytenr);
3263 if (ref && ref->generation != ptr_gen) {
3264 btrfs_free_leaf_ref(root, ref);
3265 ref = NULL;
3266 }
3267 if (ref) {
3268 ret = cache_drop_leaf_ref(trans, root, ref);
3269 BUG_ON(ret);
3270 btrfs_remove_leaf_ref(root, ref);
3271 btrfs_free_leaf_ref(root, ref);
3272 } else {
3273 /*
3274 * the leaf wasn't in the reference cache, so
3275 * we have to read it.
3276 */
3277 leaf = read_tree_block(root, bytenr, blocksize,
3278 ptr_gen);
3279 ret = btrfs_drop_leaf_ref(trans, root, leaf);
3280 BUG_ON(ret);
3281 free_extent_buffer(leaf);
3282 }
3283 atomic_inc(&root->fs_info->throttle_gen);
3284 wake_up(&root->fs_info->transaction_throttle);
3285 cond_resched();
3286 }
3287
3288 /*
3289 * run through the loop again to free the refs on the leaves.
3290 * This is faster than doing it in the loop above because
3291 * the leaves are likely to be clustered together. We end up
3292 * working in nice chunks on the extent allocation tree.
3293 */
3294 for (i = 0; i < refi; i++) {
3295 bytenr = sorted[i].bytenr;
Chris Mason56bec292009-03-13 10:10:06 -04003296 ret = btrfs_free_extent(trans, root, bytenr,
Chris Masonbd56b302009-02-04 09:27:02 -05003297 blocksize, eb->start,
3298 root_owner, root_gen, 0, 1);
3299 BUG_ON(ret);
3300
3301 atomic_inc(&root->fs_info->throttle_gen);
3302 wake_up(&root->fs_info->transaction_throttle);
3303 cond_resched();
3304 }
3305out:
3306 kfree(sorted);
3307
3308 /*
3309 * update the path to show we've processed the entire level 1
3310 * node. This will get saved into the root's drop_snapshot_progress
3311 * field so these drops are not repeated again if this transaction
3312 * commits.
3313 */
3314 path->slots[1] = nritems;
3315 return 0;
3316}
3317
3318/*
Chris Mason9aca1d52007-03-13 11:09:37 -04003319 * helper function for drop_snapshot, this walks down the tree dropping ref
3320 * counts as it goes.
3321 */
Chris Masond3977122009-01-05 21:25:51 -05003322static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05003323 struct btrfs_root *root,
3324 struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05003325{
Chris Mason7bb86312007-12-11 09:25:06 -05003326 u64 root_owner;
3327 u64 root_gen;
3328 u64 bytenr;
Chris Masonca7a79a2008-05-12 12:59:19 -04003329 u64 ptr_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04003330 struct extent_buffer *next;
3331 struct extent_buffer *cur;
Chris Mason7bb86312007-12-11 09:25:06 -05003332 struct extent_buffer *parent;
Chris Masondb945352007-10-15 16:15:53 -04003333 u32 blocksize;
Chris Mason20524f02007-03-10 06:35:47 -05003334 int ret;
3335 u32 refs;
3336
Chris Mason5caf2a02007-04-02 11:20:42 -04003337 WARN_ON(*level < 0);
3338 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason56bec292009-03-13 10:10:06 -04003339 ret = drop_snap_lookup_refcount(trans, root, path->nodes[*level]->start,
Chris Masondb945352007-10-15 16:15:53 -04003340 path->nodes[*level]->len, &refs);
Chris Mason20524f02007-03-10 06:35:47 -05003341 BUG_ON(ret);
3342 if (refs > 1)
3343 goto out;
Chris Masone0115992007-06-19 16:23:05 -04003344
Chris Mason9aca1d52007-03-13 11:09:37 -04003345 /*
3346 * walk down to the last node level and free all the leaves
3347 */
Chris Masond3977122009-01-05 21:25:51 -05003348 while (*level >= 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -04003349 WARN_ON(*level < 0);
3350 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason20524f02007-03-10 06:35:47 -05003351 cur = path->nodes[*level];
Chris Masone0115992007-06-19 16:23:05 -04003352
Chris Mason5f39d392007-10-15 16:14:19 -04003353 if (btrfs_header_level(cur) != *level)
Chris Mason2c90e5d2007-04-02 10:50:19 -04003354 WARN_ON(1);
Chris Masone0115992007-06-19 16:23:05 -04003355
Chris Mason7518a232007-03-12 12:01:18 -04003356 if (path->slots[*level] >=
Chris Mason5f39d392007-10-15 16:14:19 -04003357 btrfs_header_nritems(cur))
Chris Mason20524f02007-03-10 06:35:47 -05003358 break;
Chris Masonbd56b302009-02-04 09:27:02 -05003359
3360 /* the new code goes down to level 1 and does all the
3361 * leaves pointed to that node in bulk. So, this check
3362 * for level 0 will always be false.
3363 *
3364 * But, the disk format allows the drop_snapshot_progress
3365 * field in the root to leave things in a state where
3366 * a leaf will need cleaning up here. If someone crashes
3367 * with the old code and then boots with the new code,
3368 * we might find a leaf here.
3369 */
Chris Mason6407bf62007-03-27 06:33:00 -04003370 if (*level == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003371 ret = btrfs_drop_leaf_ref(trans, root, cur);
Chris Mason6407bf62007-03-27 06:33:00 -04003372 BUG_ON(ret);
3373 break;
3374 }
Chris Masonbd56b302009-02-04 09:27:02 -05003375
3376 /*
3377 * once we get to level one, process the whole node
3378 * at once, including everything below it.
3379 */
3380 if (*level == 1) {
3381 ret = drop_level_one_refs(trans, root, path);
3382 BUG_ON(ret);
3383 break;
3384 }
3385
Chris Masondb945352007-10-15 16:15:53 -04003386 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
Chris Masonca7a79a2008-05-12 12:59:19 -04003387 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
Chris Masondb945352007-10-15 16:15:53 -04003388 blocksize = btrfs_level_size(root, *level - 1);
Chris Mason925baed2008-06-25 16:01:30 -04003389
Chris Mason56bec292009-03-13 10:10:06 -04003390 ret = drop_snap_lookup_refcount(trans, root, bytenr,
3391 blocksize, &refs);
Chris Mason6407bf62007-03-27 06:33:00 -04003392 BUG_ON(ret);
Chris Masonbd56b302009-02-04 09:27:02 -05003393
3394 /*
3395 * if there is more than one reference, we don't need
3396 * to read that node to drop any references it has. We
3397 * just drop the ref we hold on that node and move on to the
3398 * next slot in this level.
3399 */
Chris Mason6407bf62007-03-27 06:33:00 -04003400 if (refs != 1) {
Chris Mason7bb86312007-12-11 09:25:06 -05003401 parent = path->nodes[*level];
3402 root_owner = btrfs_header_owner(parent);
3403 root_gen = btrfs_header_generation(parent);
Chris Mason20524f02007-03-10 06:35:47 -05003404 path->slots[*level]++;
Chris Masonf87f0572008-08-01 11:27:23 -04003405
Chris Mason56bec292009-03-13 10:10:06 -04003406 ret = btrfs_free_extent(trans, root, bytenr,
Zheng Yan31840ae2008-09-23 13:14:14 -04003407 blocksize, parent->start,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003408 root_owner, root_gen,
3409 *level - 1, 1);
Chris Mason20524f02007-03-10 06:35:47 -05003410 BUG_ON(ret);
Chris Mason18e35e02008-08-01 13:11:41 -04003411
3412 atomic_inc(&root->fs_info->throttle_gen);
3413 wake_up(&root->fs_info->transaction_throttle);
Chris Mason2dd3e672008-08-04 08:20:15 -04003414 cond_resched();
Chris Mason18e35e02008-08-01 13:11:41 -04003415
Chris Mason20524f02007-03-10 06:35:47 -05003416 continue;
3417 }
Chris Mason333db942008-06-25 16:01:30 -04003418
Chris Masonbd56b302009-02-04 09:27:02 -05003419 /*
3420 * we need to keep freeing things in the next level down.
3421 * read the block and loop around to process it
3422 */
3423 next = read_tree_block(root, bytenr, blocksize, ptr_gen);
Chris Mason5caf2a02007-04-02 11:20:42 -04003424 WARN_ON(*level <= 0);
Chris Mason83e15a22007-03-12 09:03:27 -04003425 if (path->nodes[*level-1])
Chris Mason5f39d392007-10-15 16:14:19 -04003426 free_extent_buffer(path->nodes[*level-1]);
Chris Mason20524f02007-03-10 06:35:47 -05003427 path->nodes[*level-1] = next;
Chris Mason5f39d392007-10-15 16:14:19 -04003428 *level = btrfs_header_level(next);
Chris Mason20524f02007-03-10 06:35:47 -05003429 path->slots[*level] = 0;
Chris Mason2dd3e672008-08-04 08:20:15 -04003430 cond_resched();
Chris Mason20524f02007-03-10 06:35:47 -05003431 }
3432out:
Chris Mason5caf2a02007-04-02 11:20:42 -04003433 WARN_ON(*level < 0);
3434 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason7bb86312007-12-11 09:25:06 -05003435
3436 if (path->nodes[*level] == root->node) {
Chris Mason7bb86312007-12-11 09:25:06 -05003437 parent = path->nodes[*level];
Yan Zheng31153d82008-07-28 15:32:19 -04003438 bytenr = path->nodes[*level]->start;
Chris Mason7bb86312007-12-11 09:25:06 -05003439 } else {
3440 parent = path->nodes[*level + 1];
Yan Zheng31153d82008-07-28 15:32:19 -04003441 bytenr = btrfs_node_blockptr(parent, path->slots[*level + 1]);
Chris Mason7bb86312007-12-11 09:25:06 -05003442 }
3443
Yan Zheng31153d82008-07-28 15:32:19 -04003444 blocksize = btrfs_level_size(root, *level);
3445 root_owner = btrfs_header_owner(parent);
Chris Mason7bb86312007-12-11 09:25:06 -05003446 root_gen = btrfs_header_generation(parent);
Yan Zheng31153d82008-07-28 15:32:19 -04003447
Chris Masonbd56b302009-02-04 09:27:02 -05003448 /*
3449 * cleanup and free the reference on the last node
3450 * we processed
3451 */
Chris Mason56bec292009-03-13 10:10:06 -04003452 ret = btrfs_free_extent(trans, root, bytenr, blocksize,
Zheng Yan31840ae2008-09-23 13:14:14 -04003453 parent->start, root_owner, root_gen,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003454 *level, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003455 free_extent_buffer(path->nodes[*level]);
Chris Mason20524f02007-03-10 06:35:47 -05003456 path->nodes[*level] = NULL;
Chris Masonbd56b302009-02-04 09:27:02 -05003457
Chris Mason20524f02007-03-10 06:35:47 -05003458 *level += 1;
3459 BUG_ON(ret);
Chris Masonf87f0572008-08-01 11:27:23 -04003460
Chris Masone7a84562008-06-25 16:01:31 -04003461 cond_resched();
Chris Mason20524f02007-03-10 06:35:47 -05003462 return 0;
3463}
3464
Chris Mason9aca1d52007-03-13 11:09:37 -04003465/*
Yan Zhengf82d02d2008-10-29 14:49:05 -04003466 * helper function for drop_subtree, this function is similar to
3467 * walk_down_tree. The main difference is that it checks reference
3468 * counts while tree blocks are locked.
3469 */
Chris Masond3977122009-01-05 21:25:51 -05003470static noinline int walk_down_subtree(struct btrfs_trans_handle *trans,
Yan Zhengf82d02d2008-10-29 14:49:05 -04003471 struct btrfs_root *root,
3472 struct btrfs_path *path, int *level)
3473{
3474 struct extent_buffer *next;
3475 struct extent_buffer *cur;
3476 struct extent_buffer *parent;
3477 u64 bytenr;
3478 u64 ptr_gen;
3479 u32 blocksize;
3480 u32 refs;
3481 int ret;
3482
3483 cur = path->nodes[*level];
3484 ret = btrfs_lookup_extent_ref(trans, root, cur->start, cur->len,
3485 &refs);
3486 BUG_ON(ret);
3487 if (refs > 1)
3488 goto out;
3489
3490 while (*level >= 0) {
3491 cur = path->nodes[*level];
3492 if (*level == 0) {
3493 ret = btrfs_drop_leaf_ref(trans, root, cur);
3494 BUG_ON(ret);
3495 clean_tree_block(trans, root, cur);
3496 break;
3497 }
3498 if (path->slots[*level] >= btrfs_header_nritems(cur)) {
3499 clean_tree_block(trans, root, cur);
3500 break;
3501 }
3502
3503 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
3504 blocksize = btrfs_level_size(root, *level - 1);
3505 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
3506
3507 next = read_tree_block(root, bytenr, blocksize, ptr_gen);
3508 btrfs_tree_lock(next);
Chris Masonb4ce94d2009-02-04 09:25:08 -05003509 btrfs_set_lock_blocking(next);
Yan Zhengf82d02d2008-10-29 14:49:05 -04003510
3511 ret = btrfs_lookup_extent_ref(trans, root, bytenr, blocksize,
3512 &refs);
3513 BUG_ON(ret);
3514 if (refs > 1) {
3515 parent = path->nodes[*level];
3516 ret = btrfs_free_extent(trans, root, bytenr,
3517 blocksize, parent->start,
3518 btrfs_header_owner(parent),
3519 btrfs_header_generation(parent),
3520 *level - 1, 1);
3521 BUG_ON(ret);
3522 path->slots[*level]++;
3523 btrfs_tree_unlock(next);
3524 free_extent_buffer(next);
3525 continue;
3526 }
3527
3528 *level = btrfs_header_level(next);
3529 path->nodes[*level] = next;
3530 path->slots[*level] = 0;
3531 path->locks[*level] = 1;
3532 cond_resched();
3533 }
3534out:
3535 parent = path->nodes[*level + 1];
3536 bytenr = path->nodes[*level]->start;
3537 blocksize = path->nodes[*level]->len;
3538
3539 ret = btrfs_free_extent(trans, root, bytenr, blocksize,
3540 parent->start, btrfs_header_owner(parent),
3541 btrfs_header_generation(parent), *level, 1);
3542 BUG_ON(ret);
3543
3544 if (path->locks[*level]) {
3545 btrfs_tree_unlock(path->nodes[*level]);
3546 path->locks[*level] = 0;
3547 }
3548 free_extent_buffer(path->nodes[*level]);
3549 path->nodes[*level] = NULL;
3550 *level += 1;
3551 cond_resched();
3552 return 0;
3553}
3554
3555/*
Chris Mason9aca1d52007-03-13 11:09:37 -04003556 * helper for dropping snapshots. This walks back up the tree in the path
3557 * to find the first node higher up where we haven't yet gone through
3558 * all the slots
3559 */
Chris Masond3977122009-01-05 21:25:51 -05003560static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05003561 struct btrfs_root *root,
Yan Zhengf82d02d2008-10-29 14:49:05 -04003562 struct btrfs_path *path,
3563 int *level, int max_level)
Chris Mason20524f02007-03-10 06:35:47 -05003564{
Chris Mason7bb86312007-12-11 09:25:06 -05003565 u64 root_owner;
3566 u64 root_gen;
3567 struct btrfs_root_item *root_item = &root->root_item;
Chris Mason20524f02007-03-10 06:35:47 -05003568 int i;
3569 int slot;
3570 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04003571
Yan Zhengf82d02d2008-10-29 14:49:05 -04003572 for (i = *level; i < max_level && path->nodes[i]; i++) {
Chris Mason20524f02007-03-10 06:35:47 -05003573 slot = path->slots[i];
Chris Mason5f39d392007-10-15 16:14:19 -04003574 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
3575 struct extent_buffer *node;
3576 struct btrfs_disk_key disk_key;
Chris Masonbd56b302009-02-04 09:27:02 -05003577
3578 /*
3579 * there is more work to do in this level.
3580 * Update the drop_progress marker to reflect
3581 * the work we've done so far, and then bump
3582 * the slot number
3583 */
Chris Mason5f39d392007-10-15 16:14:19 -04003584 node = path->nodes[i];
Chris Mason20524f02007-03-10 06:35:47 -05003585 path->slots[i]++;
3586 *level = i;
Chris Mason9f3a7422007-08-07 15:52:19 -04003587 WARN_ON(*level == 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003588 btrfs_node_key(node, &disk_key, path->slots[i]);
Chris Mason9f3a7422007-08-07 15:52:19 -04003589 memcpy(&root_item->drop_progress,
Chris Mason5f39d392007-10-15 16:14:19 -04003590 &disk_key, sizeof(disk_key));
Chris Mason9f3a7422007-08-07 15:52:19 -04003591 root_item->drop_level = i;
Chris Mason20524f02007-03-10 06:35:47 -05003592 return 0;
3593 } else {
Zheng Yan31840ae2008-09-23 13:14:14 -04003594 struct extent_buffer *parent;
Chris Masonbd56b302009-02-04 09:27:02 -05003595
3596 /*
3597 * this whole node is done, free our reference
3598 * on it and go up one level
3599 */
Zheng Yan31840ae2008-09-23 13:14:14 -04003600 if (path->nodes[*level] == root->node)
3601 parent = path->nodes[*level];
3602 else
3603 parent = path->nodes[*level + 1];
3604
3605 root_owner = btrfs_header_owner(parent);
3606 root_gen = btrfs_header_generation(parent);
Yan Zhengf82d02d2008-10-29 14:49:05 -04003607
3608 clean_tree_block(trans, root, path->nodes[*level]);
Chris Masone089f052007-03-16 16:20:31 -04003609 ret = btrfs_free_extent(trans, root,
Chris Masondb945352007-10-15 16:15:53 -04003610 path->nodes[*level]->start,
Chris Mason7bb86312007-12-11 09:25:06 -05003611 path->nodes[*level]->len,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003612 parent->start, root_owner,
3613 root_gen, *level, 1);
Chris Mason6407bf62007-03-27 06:33:00 -04003614 BUG_ON(ret);
Yan Zhengf82d02d2008-10-29 14:49:05 -04003615 if (path->locks[*level]) {
3616 btrfs_tree_unlock(path->nodes[*level]);
3617 path->locks[*level] = 0;
3618 }
Chris Mason5f39d392007-10-15 16:14:19 -04003619 free_extent_buffer(path->nodes[*level]);
Chris Mason83e15a22007-03-12 09:03:27 -04003620 path->nodes[*level] = NULL;
Chris Mason20524f02007-03-10 06:35:47 -05003621 *level = i + 1;
Chris Mason20524f02007-03-10 06:35:47 -05003622 }
3623 }
3624 return 1;
3625}
3626
Chris Mason9aca1d52007-03-13 11:09:37 -04003627/*
3628 * drop the reference count on the tree rooted at 'snap'. This traverses
3629 * the tree freeing any blocks that have a ref count of zero after being
3630 * decremented.
3631 */
Chris Masone089f052007-03-16 16:20:31 -04003632int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason9f3a7422007-08-07 15:52:19 -04003633 *root)
Chris Mason20524f02007-03-10 06:35:47 -05003634{
Chris Mason3768f362007-03-13 16:47:54 -04003635 int ret = 0;
Chris Mason9aca1d52007-03-13 11:09:37 -04003636 int wret;
Chris Mason20524f02007-03-10 06:35:47 -05003637 int level;
Chris Mason5caf2a02007-04-02 11:20:42 -04003638 struct btrfs_path *path;
Chris Mason20524f02007-03-10 06:35:47 -05003639 int i;
3640 int orig_level;
Chris Mason9f3a7422007-08-07 15:52:19 -04003641 struct btrfs_root_item *root_item = &root->root_item;
Chris Mason20524f02007-03-10 06:35:47 -05003642
Chris Masona2135012008-06-25 16:01:30 -04003643 WARN_ON(!mutex_is_locked(&root->fs_info->drop_mutex));
Chris Mason5caf2a02007-04-02 11:20:42 -04003644 path = btrfs_alloc_path();
3645 BUG_ON(!path);
Chris Mason20524f02007-03-10 06:35:47 -05003646
Chris Mason5f39d392007-10-15 16:14:19 -04003647 level = btrfs_header_level(root->node);
Chris Mason20524f02007-03-10 06:35:47 -05003648 orig_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04003649 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
3650 path->nodes[level] = root->node;
Chris Masonf510cfe2007-10-15 16:14:48 -04003651 extent_buffer_get(root->node);
Chris Mason9f3a7422007-08-07 15:52:19 -04003652 path->slots[level] = 0;
3653 } else {
3654 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04003655 struct btrfs_disk_key found_key;
3656 struct extent_buffer *node;
Chris Mason6702ed42007-08-07 16:15:09 -04003657
Chris Mason9f3a7422007-08-07 15:52:19 -04003658 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Chris Mason6702ed42007-08-07 16:15:09 -04003659 level = root_item->drop_level;
3660 path->lowest_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04003661 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason6702ed42007-08-07 16:15:09 -04003662 if (wret < 0) {
Chris Mason9f3a7422007-08-07 15:52:19 -04003663 ret = wret;
3664 goto out;
3665 }
Chris Mason5f39d392007-10-15 16:14:19 -04003666 node = path->nodes[level];
3667 btrfs_node_key(node, &found_key, path->slots[level]);
3668 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
3669 sizeof(found_key)));
Chris Mason7d9eb122008-07-08 14:19:17 -04003670 /*
3671 * unlock our path, this is safe because only this
3672 * function is allowed to delete this snapshot
3673 */
Chris Mason925baed2008-06-25 16:01:30 -04003674 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
3675 if (path->nodes[i] && path->locks[i]) {
3676 path->locks[i] = 0;
3677 btrfs_tree_unlock(path->nodes[i]);
3678 }
3679 }
Chris Mason9f3a7422007-08-07 15:52:19 -04003680 }
Chris Masond3977122009-01-05 21:25:51 -05003681 while (1) {
Chris Mason5caf2a02007-04-02 11:20:42 -04003682 wret = walk_down_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04003683 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05003684 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04003685 if (wret < 0)
3686 ret = wret;
3687
Yan Zhengf82d02d2008-10-29 14:49:05 -04003688 wret = walk_up_tree(trans, root, path, &level,
3689 BTRFS_MAX_LEVEL);
Chris Mason9aca1d52007-03-13 11:09:37 -04003690 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05003691 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04003692 if (wret < 0)
3693 ret = wret;
Chris Masone7a84562008-06-25 16:01:31 -04003694 if (trans->transaction->in_commit) {
3695 ret = -EAGAIN;
3696 break;
3697 }
Chris Mason18e35e02008-08-01 13:11:41 -04003698 atomic_inc(&root->fs_info->throttle_gen);
Chris Mason017e5362008-07-28 15:32:51 -04003699 wake_up(&root->fs_info->transaction_throttle);
Chris Mason20524f02007-03-10 06:35:47 -05003700 }
Chris Mason83e15a22007-03-12 09:03:27 -04003701 for (i = 0; i <= orig_level; i++) {
Chris Mason5caf2a02007-04-02 11:20:42 -04003702 if (path->nodes[i]) {
Chris Mason5f39d392007-10-15 16:14:19 -04003703 free_extent_buffer(path->nodes[i]);
Chris Mason0f827312007-10-15 16:18:56 -04003704 path->nodes[i] = NULL;
Chris Mason83e15a22007-03-12 09:03:27 -04003705 }
Chris Mason20524f02007-03-10 06:35:47 -05003706 }
Chris Mason9f3a7422007-08-07 15:52:19 -04003707out:
Chris Mason5caf2a02007-04-02 11:20:42 -04003708 btrfs_free_path(path);
Chris Mason9aca1d52007-03-13 11:09:37 -04003709 return ret;
Chris Mason20524f02007-03-10 06:35:47 -05003710}
Chris Mason9078a3e2007-04-26 16:46:15 -04003711
Yan Zhengf82d02d2008-10-29 14:49:05 -04003712int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
3713 struct btrfs_root *root,
3714 struct extent_buffer *node,
3715 struct extent_buffer *parent)
3716{
3717 struct btrfs_path *path;
3718 int level;
3719 int parent_level;
3720 int ret = 0;
3721 int wret;
3722
3723 path = btrfs_alloc_path();
3724 BUG_ON(!path);
3725
Chris Masonb9447ef2009-03-09 11:45:38 -04003726 btrfs_assert_tree_locked(parent);
Yan Zhengf82d02d2008-10-29 14:49:05 -04003727 parent_level = btrfs_header_level(parent);
3728 extent_buffer_get(parent);
3729 path->nodes[parent_level] = parent;
3730 path->slots[parent_level] = btrfs_header_nritems(parent);
3731
Chris Masonb9447ef2009-03-09 11:45:38 -04003732 btrfs_assert_tree_locked(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04003733 level = btrfs_header_level(node);
3734 extent_buffer_get(node);
3735 path->nodes[level] = node;
3736 path->slots[level] = 0;
3737
3738 while (1) {
3739 wret = walk_down_subtree(trans, root, path, &level);
3740 if (wret < 0)
3741 ret = wret;
3742 if (wret != 0)
3743 break;
3744
3745 wret = walk_up_tree(trans, root, path, &level, parent_level);
3746 if (wret < 0)
3747 ret = wret;
3748 if (wret != 0)
3749 break;
3750 }
3751
3752 btrfs_free_path(path);
3753 return ret;
3754}
3755
Chris Mason8e7bf942008-04-28 09:02:36 -04003756static unsigned long calc_ra(unsigned long start, unsigned long last,
3757 unsigned long nr)
3758{
3759 return min(last, start + nr - 1);
3760}
3761
Chris Masond3977122009-01-05 21:25:51 -05003762static noinline int relocate_inode_pages(struct inode *inode, u64 start,
Chris Mason98ed5172008-01-03 10:01:48 -05003763 u64 len)
Chris Masonedbd8d42007-12-21 16:27:24 -05003764{
3765 u64 page_start;
3766 u64 page_end;
Zheng Yan1a40e232008-09-26 10:09:34 -04003767 unsigned long first_index;
Chris Masonedbd8d42007-12-21 16:27:24 -05003768 unsigned long last_index;
Chris Masonedbd8d42007-12-21 16:27:24 -05003769 unsigned long i;
3770 struct page *page;
Chris Masond1310b22008-01-24 16:13:08 -05003771 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason4313b392008-01-03 09:08:48 -05003772 struct file_ra_state *ra;
Chris Mason3eaa2882008-07-24 11:57:52 -04003773 struct btrfs_ordered_extent *ordered;
Zheng Yan1a40e232008-09-26 10:09:34 -04003774 unsigned int total_read = 0;
3775 unsigned int total_dirty = 0;
3776 int ret = 0;
Chris Mason4313b392008-01-03 09:08:48 -05003777
3778 ra = kzalloc(sizeof(*ra), GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05003779
3780 mutex_lock(&inode->i_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04003781 first_index = start >> PAGE_CACHE_SHIFT;
Chris Masonedbd8d42007-12-21 16:27:24 -05003782 last_index = (start + len - 1) >> PAGE_CACHE_SHIFT;
3783
Zheng Yan1a40e232008-09-26 10:09:34 -04003784 /* make sure the dirty trick played by the caller work */
3785 ret = invalidate_inode_pages2_range(inode->i_mapping,
3786 first_index, last_index);
3787 if (ret)
3788 goto out_unlock;
Chris Mason8e7bf942008-04-28 09:02:36 -04003789
Chris Mason4313b392008-01-03 09:08:48 -05003790 file_ra_state_init(ra, inode->i_mapping);
Chris Masonedbd8d42007-12-21 16:27:24 -05003791
Zheng Yan1a40e232008-09-26 10:09:34 -04003792 for (i = first_index ; i <= last_index; i++) {
3793 if (total_read % ra->ra_pages == 0) {
Chris Mason8e7bf942008-04-28 09:02:36 -04003794 btrfs_force_ra(inode->i_mapping, ra, NULL, i,
Zheng Yan1a40e232008-09-26 10:09:34 -04003795 calc_ra(i, last_index, ra->ra_pages));
Chris Mason8e7bf942008-04-28 09:02:36 -04003796 }
3797 total_read++;
Chris Mason3eaa2882008-07-24 11:57:52 -04003798again:
3799 if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode))
Zheng Yan1a40e232008-09-26 10:09:34 -04003800 BUG_ON(1);
Chris Masonedbd8d42007-12-21 16:27:24 -05003801 page = grab_cache_page(inode->i_mapping, i);
Chris Masona061fc82008-05-07 11:43:44 -04003802 if (!page) {
Zheng Yan1a40e232008-09-26 10:09:34 -04003803 ret = -ENOMEM;
Chris Masonedbd8d42007-12-21 16:27:24 -05003804 goto out_unlock;
Chris Masona061fc82008-05-07 11:43:44 -04003805 }
Chris Masonedbd8d42007-12-21 16:27:24 -05003806 if (!PageUptodate(page)) {
3807 btrfs_readpage(NULL, page);
3808 lock_page(page);
3809 if (!PageUptodate(page)) {
3810 unlock_page(page);
3811 page_cache_release(page);
Zheng Yan1a40e232008-09-26 10:09:34 -04003812 ret = -EIO;
Chris Masonedbd8d42007-12-21 16:27:24 -05003813 goto out_unlock;
3814 }
3815 }
Chris Masonec44a352008-04-28 15:29:52 -04003816 wait_on_page_writeback(page);
Chris Mason3eaa2882008-07-24 11:57:52 -04003817
Chris Masonedbd8d42007-12-21 16:27:24 -05003818 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
3819 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05003820 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05003821
Chris Mason3eaa2882008-07-24 11:57:52 -04003822 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3823 if (ordered) {
3824 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
3825 unlock_page(page);
3826 page_cache_release(page);
3827 btrfs_start_ordered_extent(inode, ordered, 1);
3828 btrfs_put_ordered_extent(ordered);
3829 goto again;
3830 }
3831 set_page_extent_mapped(page);
3832
Zheng Yan1a40e232008-09-26 10:09:34 -04003833 if (i == first_index)
3834 set_extent_bits(io_tree, page_start, page_end,
3835 EXTENT_BOUNDARY, GFP_NOFS);
Yan Zheng1f80e4d2008-12-19 10:59:04 -05003836 btrfs_set_extent_delalloc(inode, page_start, page_end);
Zheng Yan1a40e232008-09-26 10:09:34 -04003837
Chris Masona061fc82008-05-07 11:43:44 -04003838 set_page_dirty(page);
Zheng Yan1a40e232008-09-26 10:09:34 -04003839 total_dirty++;
Chris Masonedbd8d42007-12-21 16:27:24 -05003840
Chris Masond1310b22008-01-24 16:13:08 -05003841 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05003842 unlock_page(page);
3843 page_cache_release(page);
3844 }
3845
3846out_unlock:
Chris Masonec44a352008-04-28 15:29:52 -04003847 kfree(ra);
Chris Masonedbd8d42007-12-21 16:27:24 -05003848 mutex_unlock(&inode->i_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04003849 balance_dirty_pages_ratelimited_nr(inode->i_mapping, total_dirty);
Chris Masonbf4ef672008-05-08 13:26:18 -04003850 return ret;
3851}
3852
Chris Masond3977122009-01-05 21:25:51 -05003853static noinline int relocate_data_extent(struct inode *reloc_inode,
Zheng Yan1a40e232008-09-26 10:09:34 -04003854 struct btrfs_key *extent_key,
3855 u64 offset)
Chris Masonedbd8d42007-12-21 16:27:24 -05003856{
Zheng Yan1a40e232008-09-26 10:09:34 -04003857 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
3858 struct extent_map_tree *em_tree = &BTRFS_I(reloc_inode)->extent_tree;
3859 struct extent_map *em;
Yan Zheng66435582008-10-30 14:19:50 -04003860 u64 start = extent_key->objectid - offset;
3861 u64 end = start + extent_key->offset - 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04003862
3863 em = alloc_extent_map(GFP_NOFS);
3864 BUG_ON(!em || IS_ERR(em));
3865
Yan Zheng66435582008-10-30 14:19:50 -04003866 em->start = start;
Zheng Yan1a40e232008-09-26 10:09:34 -04003867 em->len = extent_key->offset;
Chris Masonc8b97812008-10-29 14:49:59 -04003868 em->block_len = extent_key->offset;
Zheng Yan1a40e232008-09-26 10:09:34 -04003869 em->block_start = extent_key->objectid;
3870 em->bdev = root->fs_info->fs_devices->latest_bdev;
3871 set_bit(EXTENT_FLAG_PINNED, &em->flags);
3872
3873 /* setup extent map to cheat btrfs_readpage */
Yan Zheng66435582008-10-30 14:19:50 -04003874 lock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04003875 while (1) {
3876 int ret;
3877 spin_lock(&em_tree->lock);
3878 ret = add_extent_mapping(em_tree, em);
3879 spin_unlock(&em_tree->lock);
3880 if (ret != -EEXIST) {
3881 free_extent_map(em);
3882 break;
3883 }
Yan Zheng66435582008-10-30 14:19:50 -04003884 btrfs_drop_extent_cache(reloc_inode, start, end, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04003885 }
Yan Zheng66435582008-10-30 14:19:50 -04003886 unlock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04003887
Yan Zheng66435582008-10-30 14:19:50 -04003888 return relocate_inode_pages(reloc_inode, start, extent_key->offset);
Zheng Yan1a40e232008-09-26 10:09:34 -04003889}
3890
3891struct btrfs_ref_path {
3892 u64 extent_start;
3893 u64 nodes[BTRFS_MAX_LEVEL];
3894 u64 root_objectid;
3895 u64 root_generation;
3896 u64 owner_objectid;
Zheng Yan1a40e232008-09-26 10:09:34 -04003897 u32 num_refs;
3898 int lowest_level;
3899 int current_level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04003900 int shared_level;
3901
3902 struct btrfs_key node_keys[BTRFS_MAX_LEVEL];
3903 u64 new_nodes[BTRFS_MAX_LEVEL];
Zheng Yan1a40e232008-09-26 10:09:34 -04003904};
3905
3906struct disk_extent {
Chris Masonc8b97812008-10-29 14:49:59 -04003907 u64 ram_bytes;
Zheng Yan1a40e232008-09-26 10:09:34 -04003908 u64 disk_bytenr;
3909 u64 disk_num_bytes;
3910 u64 offset;
3911 u64 num_bytes;
Chris Masonc8b97812008-10-29 14:49:59 -04003912 u8 compression;
3913 u8 encryption;
3914 u16 other_encoding;
Zheng Yan1a40e232008-09-26 10:09:34 -04003915};
3916
3917static int is_cowonly_root(u64 root_objectid)
3918{
3919 if (root_objectid == BTRFS_ROOT_TREE_OBJECTID ||
3920 root_objectid == BTRFS_EXTENT_TREE_OBJECTID ||
3921 root_objectid == BTRFS_CHUNK_TREE_OBJECTID ||
3922 root_objectid == BTRFS_DEV_TREE_OBJECTID ||
Yan Zheng0403e472008-12-10 20:32:51 -05003923 root_objectid == BTRFS_TREE_LOG_OBJECTID ||
3924 root_objectid == BTRFS_CSUM_TREE_OBJECTID)
Zheng Yan1a40e232008-09-26 10:09:34 -04003925 return 1;
3926 return 0;
3927}
3928
Chris Masond3977122009-01-05 21:25:51 -05003929static noinline int __next_ref_path(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04003930 struct btrfs_root *extent_root,
3931 struct btrfs_ref_path *ref_path,
3932 int first_time)
3933{
3934 struct extent_buffer *leaf;
3935 struct btrfs_path *path;
Chris Mason4313b392008-01-03 09:08:48 -05003936 struct btrfs_extent_ref *ref;
Chris Masonedbd8d42007-12-21 16:27:24 -05003937 struct btrfs_key key;
3938 struct btrfs_key found_key;
Zheng Yan1a40e232008-09-26 10:09:34 -04003939 u64 bytenr;
Chris Masonedbd8d42007-12-21 16:27:24 -05003940 u32 nritems;
Zheng Yan1a40e232008-09-26 10:09:34 -04003941 int level;
3942 int ret = 1;
Chris Masonedbd8d42007-12-21 16:27:24 -05003943
Zheng Yan1a40e232008-09-26 10:09:34 -04003944 path = btrfs_alloc_path();
3945 if (!path)
3946 return -ENOMEM;
3947
Zheng Yan1a40e232008-09-26 10:09:34 -04003948 if (first_time) {
3949 ref_path->lowest_level = -1;
3950 ref_path->current_level = -1;
Yan Zhengf82d02d2008-10-29 14:49:05 -04003951 ref_path->shared_level = -1;
Zheng Yan1a40e232008-09-26 10:09:34 -04003952 goto walk_up;
Chris Masona061fc82008-05-07 11:43:44 -04003953 }
Zheng Yan1a40e232008-09-26 10:09:34 -04003954walk_down:
3955 level = ref_path->current_level - 1;
3956 while (level >= -1) {
3957 u64 parent;
3958 if (level < ref_path->lowest_level)
3959 break;
Chris Masonedbd8d42007-12-21 16:27:24 -05003960
Chris Masond3977122009-01-05 21:25:51 -05003961 if (level >= 0)
Zheng Yan1a40e232008-09-26 10:09:34 -04003962 bytenr = ref_path->nodes[level];
Chris Masond3977122009-01-05 21:25:51 -05003963 else
Zheng Yan1a40e232008-09-26 10:09:34 -04003964 bytenr = ref_path->extent_start;
Zheng Yan1a40e232008-09-26 10:09:34 -04003965 BUG_ON(bytenr == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05003966
Zheng Yan1a40e232008-09-26 10:09:34 -04003967 parent = ref_path->nodes[level + 1];
3968 ref_path->nodes[level + 1] = 0;
3969 ref_path->current_level = level;
3970 BUG_ON(parent == 0);
3971
3972 key.objectid = bytenr;
3973 key.offset = parent + 1;
3974 key.type = BTRFS_EXTENT_REF_KEY;
3975
3976 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05003977 if (ret < 0)
3978 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04003979 BUG_ON(ret == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05003980
Chris Masonedbd8d42007-12-21 16:27:24 -05003981 leaf = path->nodes[0];
3982 nritems = btrfs_header_nritems(leaf);
Zheng Yan1a40e232008-09-26 10:09:34 -04003983 if (path->slots[0] >= nritems) {
Chris Masona061fc82008-05-07 11:43:44 -04003984 ret = btrfs_next_leaf(extent_root, path);
Chris Masona061fc82008-05-07 11:43:44 -04003985 if (ret < 0)
3986 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04003987 if (ret > 0)
3988 goto next;
Chris Masonbf4ef672008-05-08 13:26:18 -04003989 leaf = path->nodes[0];
Chris Masona061fc82008-05-07 11:43:44 -04003990 }
Chris Masonedbd8d42007-12-21 16:27:24 -05003991
3992 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Zheng Yan1a40e232008-09-26 10:09:34 -04003993 if (found_key.objectid == bytenr &&
Yan Zhengf82d02d2008-10-29 14:49:05 -04003994 found_key.type == BTRFS_EXTENT_REF_KEY) {
3995 if (level < ref_path->shared_level)
3996 ref_path->shared_level = level;
Zheng Yan1a40e232008-09-26 10:09:34 -04003997 goto found;
Yan Zhengf82d02d2008-10-29 14:49:05 -04003998 }
Zheng Yan1a40e232008-09-26 10:09:34 -04003999next:
4000 level--;
4001 btrfs_release_path(extent_root, path);
Yan Zhengd899e052008-10-30 14:25:28 -04004002 cond_resched();
Zheng Yan1a40e232008-09-26 10:09:34 -04004003 }
4004 /* reached lowest level */
4005 ret = 1;
4006 goto out;
4007walk_up:
4008 level = ref_path->current_level;
4009 while (level < BTRFS_MAX_LEVEL - 1) {
4010 u64 ref_objectid;
Chris Masond3977122009-01-05 21:25:51 -05004011
4012 if (level >= 0)
Zheng Yan1a40e232008-09-26 10:09:34 -04004013 bytenr = ref_path->nodes[level];
Chris Masond3977122009-01-05 21:25:51 -05004014 else
Zheng Yan1a40e232008-09-26 10:09:34 -04004015 bytenr = ref_path->extent_start;
Chris Masond3977122009-01-05 21:25:51 -05004016
Zheng Yan1a40e232008-09-26 10:09:34 -04004017 BUG_ON(bytenr == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05004018
Zheng Yan1a40e232008-09-26 10:09:34 -04004019 key.objectid = bytenr;
4020 key.offset = 0;
4021 key.type = BTRFS_EXTENT_REF_KEY;
Chris Masonedbd8d42007-12-21 16:27:24 -05004022
Zheng Yan1a40e232008-09-26 10:09:34 -04004023 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
4024 if (ret < 0)
Chris Masonedbd8d42007-12-21 16:27:24 -05004025 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04004026
4027 leaf = path->nodes[0];
4028 nritems = btrfs_header_nritems(leaf);
4029 if (path->slots[0] >= nritems) {
4030 ret = btrfs_next_leaf(extent_root, path);
4031 if (ret < 0)
4032 goto out;
4033 if (ret > 0) {
4034 /* the extent was freed by someone */
4035 if (ref_path->lowest_level == level)
4036 goto out;
4037 btrfs_release_path(extent_root, path);
4038 goto walk_down;
4039 }
4040 leaf = path->nodes[0];
4041 }
4042
4043 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4044 if (found_key.objectid != bytenr ||
4045 found_key.type != BTRFS_EXTENT_REF_KEY) {
4046 /* the extent was freed by someone */
4047 if (ref_path->lowest_level == level) {
4048 ret = 1;
4049 goto out;
4050 }
4051 btrfs_release_path(extent_root, path);
4052 goto walk_down;
4053 }
4054found:
4055 ref = btrfs_item_ptr(leaf, path->slots[0],
4056 struct btrfs_extent_ref);
4057 ref_objectid = btrfs_ref_objectid(leaf, ref);
4058 if (ref_objectid < BTRFS_FIRST_FREE_OBJECTID) {
4059 if (first_time) {
4060 level = (int)ref_objectid;
4061 BUG_ON(level >= BTRFS_MAX_LEVEL);
4062 ref_path->lowest_level = level;
4063 ref_path->current_level = level;
4064 ref_path->nodes[level] = bytenr;
4065 } else {
4066 WARN_ON(ref_objectid != level);
4067 }
4068 } else {
4069 WARN_ON(level != -1);
4070 }
4071 first_time = 0;
4072
4073 if (ref_path->lowest_level == level) {
4074 ref_path->owner_objectid = ref_objectid;
Zheng Yan1a40e232008-09-26 10:09:34 -04004075 ref_path->num_refs = btrfs_ref_num_refs(leaf, ref);
4076 }
4077
4078 /*
4079 * the block is tree root or the block isn't in reference
4080 * counted tree.
4081 */
4082 if (found_key.objectid == found_key.offset ||
4083 is_cowonly_root(btrfs_ref_root(leaf, ref))) {
4084 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
4085 ref_path->root_generation =
4086 btrfs_ref_generation(leaf, ref);
4087 if (level < 0) {
4088 /* special reference from the tree log */
4089 ref_path->nodes[0] = found_key.offset;
4090 ref_path->current_level = 0;
4091 }
4092 ret = 0;
4093 goto out;
4094 }
4095
4096 level++;
4097 BUG_ON(ref_path->nodes[level] != 0);
4098 ref_path->nodes[level] = found_key.offset;
4099 ref_path->current_level = level;
4100
4101 /*
4102 * the reference was created in the running transaction,
4103 * no need to continue walking up.
4104 */
4105 if (btrfs_ref_generation(leaf, ref) == trans->transid) {
4106 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
4107 ref_path->root_generation =
4108 btrfs_ref_generation(leaf, ref);
4109 ret = 0;
4110 goto out;
4111 }
4112
4113 btrfs_release_path(extent_root, path);
Yan Zhengd899e052008-10-30 14:25:28 -04004114 cond_resched();
Zheng Yan1a40e232008-09-26 10:09:34 -04004115 }
4116 /* reached max tree level, but no tree root found. */
4117 BUG();
4118out:
Zheng Yan1a40e232008-09-26 10:09:34 -04004119 btrfs_free_path(path);
4120 return ret;
4121}
4122
4123static int btrfs_first_ref_path(struct btrfs_trans_handle *trans,
4124 struct btrfs_root *extent_root,
4125 struct btrfs_ref_path *ref_path,
4126 u64 extent_start)
4127{
4128 memset(ref_path, 0, sizeof(*ref_path));
4129 ref_path->extent_start = extent_start;
4130
4131 return __next_ref_path(trans, extent_root, ref_path, 1);
4132}
4133
4134static int btrfs_next_ref_path(struct btrfs_trans_handle *trans,
4135 struct btrfs_root *extent_root,
4136 struct btrfs_ref_path *ref_path)
4137{
4138 return __next_ref_path(trans, extent_root, ref_path, 0);
4139}
4140
Chris Masond3977122009-01-05 21:25:51 -05004141static noinline int get_new_locations(struct inode *reloc_inode,
Zheng Yan1a40e232008-09-26 10:09:34 -04004142 struct btrfs_key *extent_key,
4143 u64 offset, int no_fragment,
4144 struct disk_extent **extents,
4145 int *nr_extents)
4146{
4147 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
4148 struct btrfs_path *path;
4149 struct btrfs_file_extent_item *fi;
4150 struct extent_buffer *leaf;
4151 struct disk_extent *exts = *extents;
4152 struct btrfs_key found_key;
4153 u64 cur_pos;
4154 u64 last_byte;
4155 u32 nritems;
4156 int nr = 0;
4157 int max = *nr_extents;
4158 int ret;
4159
4160 WARN_ON(!no_fragment && *extents);
4161 if (!exts) {
4162 max = 1;
4163 exts = kmalloc(sizeof(*exts) * max, GFP_NOFS);
4164 if (!exts)
4165 return -ENOMEM;
4166 }
4167
4168 path = btrfs_alloc_path();
4169 BUG_ON(!path);
4170
4171 cur_pos = extent_key->objectid - offset;
4172 last_byte = extent_key->objectid + extent_key->offset;
4173 ret = btrfs_lookup_file_extent(NULL, root, path, reloc_inode->i_ino,
4174 cur_pos, 0);
4175 if (ret < 0)
4176 goto out;
4177 if (ret > 0) {
4178 ret = -ENOENT;
4179 goto out;
4180 }
4181
4182 while (1) {
4183 leaf = path->nodes[0];
4184 nritems = btrfs_header_nritems(leaf);
4185 if (path->slots[0] >= nritems) {
4186 ret = btrfs_next_leaf(root, path);
4187 if (ret < 0)
4188 goto out;
4189 if (ret > 0)
4190 break;
4191 leaf = path->nodes[0];
4192 }
4193
4194 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4195 if (found_key.offset != cur_pos ||
4196 found_key.type != BTRFS_EXTENT_DATA_KEY ||
4197 found_key.objectid != reloc_inode->i_ino)
4198 break;
4199
4200 fi = btrfs_item_ptr(leaf, path->slots[0],
4201 struct btrfs_file_extent_item);
4202 if (btrfs_file_extent_type(leaf, fi) !=
4203 BTRFS_FILE_EXTENT_REG ||
4204 btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
4205 break;
4206
4207 if (nr == max) {
4208 struct disk_extent *old = exts;
4209 max *= 2;
4210 exts = kzalloc(sizeof(*exts) * max, GFP_NOFS);
4211 memcpy(exts, old, sizeof(*exts) * nr);
4212 if (old != *extents)
4213 kfree(old);
4214 }
4215
4216 exts[nr].disk_bytenr =
4217 btrfs_file_extent_disk_bytenr(leaf, fi);
4218 exts[nr].disk_num_bytes =
4219 btrfs_file_extent_disk_num_bytes(leaf, fi);
4220 exts[nr].offset = btrfs_file_extent_offset(leaf, fi);
4221 exts[nr].num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
Chris Masonc8b97812008-10-29 14:49:59 -04004222 exts[nr].ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
4223 exts[nr].compression = btrfs_file_extent_compression(leaf, fi);
4224 exts[nr].encryption = btrfs_file_extent_encryption(leaf, fi);
4225 exts[nr].other_encoding = btrfs_file_extent_other_encoding(leaf,
4226 fi);
Yan Zhengd899e052008-10-30 14:25:28 -04004227 BUG_ON(exts[nr].offset > 0);
4228 BUG_ON(exts[nr].compression || exts[nr].encryption);
4229 BUG_ON(exts[nr].num_bytes != exts[nr].disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04004230
4231 cur_pos += exts[nr].num_bytes;
4232 nr++;
4233
4234 if (cur_pos + offset >= last_byte)
4235 break;
4236
4237 if (no_fragment) {
4238 ret = 1;
4239 goto out;
4240 }
4241 path->slots[0]++;
4242 }
4243
Yan Zheng1f80e4d2008-12-19 10:59:04 -05004244 BUG_ON(cur_pos + offset > last_byte);
Zheng Yan1a40e232008-09-26 10:09:34 -04004245 if (cur_pos + offset < last_byte) {
4246 ret = -ENOENT;
4247 goto out;
Chris Masonedbd8d42007-12-21 16:27:24 -05004248 }
4249 ret = 0;
4250out:
Zheng Yan1a40e232008-09-26 10:09:34 -04004251 btrfs_free_path(path);
4252 if (ret) {
4253 if (exts != *extents)
4254 kfree(exts);
4255 } else {
4256 *extents = exts;
4257 *nr_extents = nr;
4258 }
4259 return ret;
4260}
4261
Chris Masond3977122009-01-05 21:25:51 -05004262static noinline int replace_one_extent(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04004263 struct btrfs_root *root,
4264 struct btrfs_path *path,
4265 struct btrfs_key *extent_key,
4266 struct btrfs_key *leaf_key,
4267 struct btrfs_ref_path *ref_path,
4268 struct disk_extent *new_extents,
4269 int nr_extents)
4270{
4271 struct extent_buffer *leaf;
4272 struct btrfs_file_extent_item *fi;
4273 struct inode *inode = NULL;
4274 struct btrfs_key key;
4275 u64 lock_start = 0;
4276 u64 lock_end = 0;
4277 u64 num_bytes;
4278 u64 ext_offset;
Yan Zheng86288a12009-01-21 10:49:16 -05004279 u64 search_end = (u64)-1;
Zheng Yan1a40e232008-09-26 10:09:34 -04004280 u32 nritems;
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004281 int nr_scaned = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04004282 int extent_locked = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04004283 int extent_type;
Zheng Yan1a40e232008-09-26 10:09:34 -04004284 int ret;
4285
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004286 memcpy(&key, leaf_key, sizeof(key));
Zheng Yan1a40e232008-09-26 10:09:34 -04004287 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004288 if (key.objectid < ref_path->owner_objectid ||
4289 (key.objectid == ref_path->owner_objectid &&
4290 key.type < BTRFS_EXTENT_DATA_KEY)) {
4291 key.objectid = ref_path->owner_objectid;
4292 key.type = BTRFS_EXTENT_DATA_KEY;
4293 key.offset = 0;
4294 }
Zheng Yan1a40e232008-09-26 10:09:34 -04004295 }
4296
4297 while (1) {
4298 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
4299 if (ret < 0)
4300 goto out;
4301
4302 leaf = path->nodes[0];
4303 nritems = btrfs_header_nritems(leaf);
4304next:
4305 if (extent_locked && ret > 0) {
4306 /*
4307 * the file extent item was modified by someone
4308 * before the extent got locked.
4309 */
Zheng Yan1a40e232008-09-26 10:09:34 -04004310 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
4311 lock_end, GFP_NOFS);
4312 extent_locked = 0;
4313 }
4314
4315 if (path->slots[0] >= nritems) {
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004316 if (++nr_scaned > 2)
Zheng Yan1a40e232008-09-26 10:09:34 -04004317 break;
4318
4319 BUG_ON(extent_locked);
4320 ret = btrfs_next_leaf(root, path);
4321 if (ret < 0)
4322 goto out;
4323 if (ret > 0)
4324 break;
4325 leaf = path->nodes[0];
4326 nritems = btrfs_header_nritems(leaf);
4327 }
4328
4329 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4330
4331 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
4332 if ((key.objectid > ref_path->owner_objectid) ||
4333 (key.objectid == ref_path->owner_objectid &&
4334 key.type > BTRFS_EXTENT_DATA_KEY) ||
Yan Zheng86288a12009-01-21 10:49:16 -05004335 key.offset >= search_end)
Zheng Yan1a40e232008-09-26 10:09:34 -04004336 break;
4337 }
4338
4339 if (inode && key.objectid != inode->i_ino) {
4340 BUG_ON(extent_locked);
4341 btrfs_release_path(root, path);
4342 mutex_unlock(&inode->i_mutex);
4343 iput(inode);
4344 inode = NULL;
4345 continue;
4346 }
4347
4348 if (key.type != BTRFS_EXTENT_DATA_KEY) {
4349 path->slots[0]++;
4350 ret = 1;
4351 goto next;
4352 }
4353 fi = btrfs_item_ptr(leaf, path->slots[0],
4354 struct btrfs_file_extent_item);
Yan Zhengd899e052008-10-30 14:25:28 -04004355 extent_type = btrfs_file_extent_type(leaf, fi);
4356 if ((extent_type != BTRFS_FILE_EXTENT_REG &&
4357 extent_type != BTRFS_FILE_EXTENT_PREALLOC) ||
Zheng Yan1a40e232008-09-26 10:09:34 -04004358 (btrfs_file_extent_disk_bytenr(leaf, fi) !=
4359 extent_key->objectid)) {
4360 path->slots[0]++;
4361 ret = 1;
4362 goto next;
4363 }
4364
4365 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
4366 ext_offset = btrfs_file_extent_offset(leaf, fi);
4367
Yan Zheng86288a12009-01-21 10:49:16 -05004368 if (search_end == (u64)-1) {
4369 search_end = key.offset - ext_offset +
4370 btrfs_file_extent_ram_bytes(leaf, fi);
4371 }
Zheng Yan1a40e232008-09-26 10:09:34 -04004372
4373 if (!extent_locked) {
4374 lock_start = key.offset;
4375 lock_end = lock_start + num_bytes - 1;
4376 } else {
Yan Zheng66435582008-10-30 14:19:50 -04004377 if (lock_start > key.offset ||
4378 lock_end + 1 < key.offset + num_bytes) {
4379 unlock_extent(&BTRFS_I(inode)->io_tree,
4380 lock_start, lock_end, GFP_NOFS);
4381 extent_locked = 0;
4382 }
Zheng Yan1a40e232008-09-26 10:09:34 -04004383 }
4384
4385 if (!inode) {
4386 btrfs_release_path(root, path);
4387
4388 inode = btrfs_iget_locked(root->fs_info->sb,
4389 key.objectid, root);
4390 if (inode->i_state & I_NEW) {
4391 BTRFS_I(inode)->root = root;
4392 BTRFS_I(inode)->location.objectid =
4393 key.objectid;
4394 BTRFS_I(inode)->location.type =
4395 BTRFS_INODE_ITEM_KEY;
4396 BTRFS_I(inode)->location.offset = 0;
4397 btrfs_read_locked_inode(inode);
4398 unlock_new_inode(inode);
4399 }
4400 /*
4401 * some code call btrfs_commit_transaction while
4402 * holding the i_mutex, so we can't use mutex_lock
4403 * here.
4404 */
4405 if (is_bad_inode(inode) ||
4406 !mutex_trylock(&inode->i_mutex)) {
4407 iput(inode);
4408 inode = NULL;
4409 key.offset = (u64)-1;
4410 goto skip;
4411 }
4412 }
4413
4414 if (!extent_locked) {
4415 struct btrfs_ordered_extent *ordered;
4416
4417 btrfs_release_path(root, path);
4418
4419 lock_extent(&BTRFS_I(inode)->io_tree, lock_start,
4420 lock_end, GFP_NOFS);
4421 ordered = btrfs_lookup_first_ordered_extent(inode,
4422 lock_end);
4423 if (ordered &&
4424 ordered->file_offset <= lock_end &&
4425 ordered->file_offset + ordered->len > lock_start) {
4426 unlock_extent(&BTRFS_I(inode)->io_tree,
4427 lock_start, lock_end, GFP_NOFS);
4428 btrfs_start_ordered_extent(inode, ordered, 1);
4429 btrfs_put_ordered_extent(ordered);
4430 key.offset += num_bytes;
4431 goto skip;
4432 }
4433 if (ordered)
4434 btrfs_put_ordered_extent(ordered);
4435
Zheng Yan1a40e232008-09-26 10:09:34 -04004436 extent_locked = 1;
4437 continue;
4438 }
4439
4440 if (nr_extents == 1) {
4441 /* update extent pointer in place */
Zheng Yan1a40e232008-09-26 10:09:34 -04004442 btrfs_set_file_extent_disk_bytenr(leaf, fi,
4443 new_extents[0].disk_bytenr);
4444 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
4445 new_extents[0].disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04004446 btrfs_mark_buffer_dirty(leaf);
4447
4448 btrfs_drop_extent_cache(inode, key.offset,
4449 key.offset + num_bytes - 1, 0);
4450
4451 ret = btrfs_inc_extent_ref(trans, root,
4452 new_extents[0].disk_bytenr,
4453 new_extents[0].disk_num_bytes,
4454 leaf->start,
4455 root->root_key.objectid,
4456 trans->transid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004457 key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04004458 BUG_ON(ret);
4459
4460 ret = btrfs_free_extent(trans, root,
4461 extent_key->objectid,
4462 extent_key->offset,
4463 leaf->start,
4464 btrfs_header_owner(leaf),
4465 btrfs_header_generation(leaf),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004466 key.objectid, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04004467 BUG_ON(ret);
4468
4469 btrfs_release_path(root, path);
4470 key.offset += num_bytes;
4471 } else {
Yan Zhengd899e052008-10-30 14:25:28 -04004472 BUG_ON(1);
4473#if 0
Zheng Yan1a40e232008-09-26 10:09:34 -04004474 u64 alloc_hint;
4475 u64 extent_len;
4476 int i;
4477 /*
4478 * drop old extent pointer at first, then insert the
4479 * new pointers one bye one
4480 */
4481 btrfs_release_path(root, path);
4482 ret = btrfs_drop_extents(trans, root, inode, key.offset,
4483 key.offset + num_bytes,
4484 key.offset, &alloc_hint);
4485 BUG_ON(ret);
4486
4487 for (i = 0; i < nr_extents; i++) {
4488 if (ext_offset >= new_extents[i].num_bytes) {
4489 ext_offset -= new_extents[i].num_bytes;
4490 continue;
4491 }
4492 extent_len = min(new_extents[i].num_bytes -
4493 ext_offset, num_bytes);
4494
4495 ret = btrfs_insert_empty_item(trans, root,
4496 path, &key,
4497 sizeof(*fi));
4498 BUG_ON(ret);
4499
4500 leaf = path->nodes[0];
4501 fi = btrfs_item_ptr(leaf, path->slots[0],
4502 struct btrfs_file_extent_item);
4503 btrfs_set_file_extent_generation(leaf, fi,
4504 trans->transid);
4505 btrfs_set_file_extent_type(leaf, fi,
4506 BTRFS_FILE_EXTENT_REG);
4507 btrfs_set_file_extent_disk_bytenr(leaf, fi,
4508 new_extents[i].disk_bytenr);
4509 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
4510 new_extents[i].disk_num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -04004511 btrfs_set_file_extent_ram_bytes(leaf, fi,
4512 new_extents[i].ram_bytes);
4513
4514 btrfs_set_file_extent_compression(leaf, fi,
4515 new_extents[i].compression);
4516 btrfs_set_file_extent_encryption(leaf, fi,
4517 new_extents[i].encryption);
4518 btrfs_set_file_extent_other_encoding(leaf, fi,
4519 new_extents[i].other_encoding);
4520
Zheng Yan1a40e232008-09-26 10:09:34 -04004521 btrfs_set_file_extent_num_bytes(leaf, fi,
4522 extent_len);
4523 ext_offset += new_extents[i].offset;
4524 btrfs_set_file_extent_offset(leaf, fi,
4525 ext_offset);
4526 btrfs_mark_buffer_dirty(leaf);
4527
4528 btrfs_drop_extent_cache(inode, key.offset,
4529 key.offset + extent_len - 1, 0);
4530
4531 ret = btrfs_inc_extent_ref(trans, root,
4532 new_extents[i].disk_bytenr,
4533 new_extents[i].disk_num_bytes,
4534 leaf->start,
4535 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004536 trans->transid, key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04004537 BUG_ON(ret);
4538 btrfs_release_path(root, path);
4539
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004540 inode_add_bytes(inode, extent_len);
Zheng Yan1a40e232008-09-26 10:09:34 -04004541
4542 ext_offset = 0;
4543 num_bytes -= extent_len;
4544 key.offset += extent_len;
4545
4546 if (num_bytes == 0)
4547 break;
4548 }
4549 BUG_ON(i >= nr_extents);
Yan Zhengd899e052008-10-30 14:25:28 -04004550#endif
Zheng Yan1a40e232008-09-26 10:09:34 -04004551 }
4552
4553 if (extent_locked) {
Zheng Yan1a40e232008-09-26 10:09:34 -04004554 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
4555 lock_end, GFP_NOFS);
4556 extent_locked = 0;
4557 }
4558skip:
4559 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS &&
Yan Zheng86288a12009-01-21 10:49:16 -05004560 key.offset >= search_end)
Zheng Yan1a40e232008-09-26 10:09:34 -04004561 break;
4562
4563 cond_resched();
4564 }
4565 ret = 0;
4566out:
4567 btrfs_release_path(root, path);
4568 if (inode) {
4569 mutex_unlock(&inode->i_mutex);
4570 if (extent_locked) {
Zheng Yan1a40e232008-09-26 10:09:34 -04004571 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
4572 lock_end, GFP_NOFS);
4573 }
4574 iput(inode);
4575 }
4576 return ret;
4577}
4578
Zheng Yan1a40e232008-09-26 10:09:34 -04004579int btrfs_reloc_tree_cache_ref(struct btrfs_trans_handle *trans,
4580 struct btrfs_root *root,
4581 struct extent_buffer *buf, u64 orig_start)
4582{
4583 int level;
4584 int ret;
4585
4586 BUG_ON(btrfs_header_generation(buf) != trans->transid);
4587 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
4588
4589 level = btrfs_header_level(buf);
4590 if (level == 0) {
4591 struct btrfs_leaf_ref *ref;
4592 struct btrfs_leaf_ref *orig_ref;
4593
4594 orig_ref = btrfs_lookup_leaf_ref(root, orig_start);
4595 if (!orig_ref)
4596 return -ENOENT;
4597
4598 ref = btrfs_alloc_leaf_ref(root, orig_ref->nritems);
4599 if (!ref) {
4600 btrfs_free_leaf_ref(root, orig_ref);
4601 return -ENOMEM;
4602 }
4603
4604 ref->nritems = orig_ref->nritems;
4605 memcpy(ref->extents, orig_ref->extents,
4606 sizeof(ref->extents[0]) * ref->nritems);
4607
4608 btrfs_free_leaf_ref(root, orig_ref);
4609
4610 ref->root_gen = trans->transid;
4611 ref->bytenr = buf->start;
4612 ref->owner = btrfs_header_owner(buf);
4613 ref->generation = btrfs_header_generation(buf);
Chris Masonbd56b302009-02-04 09:27:02 -05004614
Zheng Yan1a40e232008-09-26 10:09:34 -04004615 ret = btrfs_add_leaf_ref(root, ref, 0);
4616 WARN_ON(ret);
4617 btrfs_free_leaf_ref(root, ref);
4618 }
4619 return 0;
4620}
4621
Chris Masond3977122009-01-05 21:25:51 -05004622static noinline int invalidate_extent_cache(struct btrfs_root *root,
Zheng Yan1a40e232008-09-26 10:09:34 -04004623 struct extent_buffer *leaf,
4624 struct btrfs_block_group_cache *group,
4625 struct btrfs_root *target_root)
4626{
4627 struct btrfs_key key;
4628 struct inode *inode = NULL;
4629 struct btrfs_file_extent_item *fi;
4630 u64 num_bytes;
4631 u64 skip_objectid = 0;
4632 u32 nritems;
4633 u32 i;
4634
4635 nritems = btrfs_header_nritems(leaf);
4636 for (i = 0; i < nritems; i++) {
4637 btrfs_item_key_to_cpu(leaf, &key, i);
4638 if (key.objectid == skip_objectid ||
4639 key.type != BTRFS_EXTENT_DATA_KEY)
4640 continue;
4641 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
4642 if (btrfs_file_extent_type(leaf, fi) ==
4643 BTRFS_FILE_EXTENT_INLINE)
4644 continue;
4645 if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
4646 continue;
4647 if (!inode || inode->i_ino != key.objectid) {
4648 iput(inode);
4649 inode = btrfs_ilookup(target_root->fs_info->sb,
4650 key.objectid, target_root, 1);
4651 }
4652 if (!inode) {
4653 skip_objectid = key.objectid;
4654 continue;
4655 }
4656 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
4657
4658 lock_extent(&BTRFS_I(inode)->io_tree, key.offset,
4659 key.offset + num_bytes - 1, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04004660 btrfs_drop_extent_cache(inode, key.offset,
4661 key.offset + num_bytes - 1, 1);
Zheng Yan1a40e232008-09-26 10:09:34 -04004662 unlock_extent(&BTRFS_I(inode)->io_tree, key.offset,
4663 key.offset + num_bytes - 1, GFP_NOFS);
4664 cond_resched();
4665 }
4666 iput(inode);
4667 return 0;
4668}
4669
Chris Masond3977122009-01-05 21:25:51 -05004670static noinline int replace_extents_in_leaf(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04004671 struct btrfs_root *root,
4672 struct extent_buffer *leaf,
4673 struct btrfs_block_group_cache *group,
4674 struct inode *reloc_inode)
4675{
4676 struct btrfs_key key;
4677 struct btrfs_key extent_key;
4678 struct btrfs_file_extent_item *fi;
4679 struct btrfs_leaf_ref *ref;
4680 struct disk_extent *new_extent;
4681 u64 bytenr;
4682 u64 num_bytes;
4683 u32 nritems;
4684 u32 i;
4685 int ext_index;
4686 int nr_extent;
4687 int ret;
4688
4689 new_extent = kmalloc(sizeof(*new_extent), GFP_NOFS);
4690 BUG_ON(!new_extent);
4691
4692 ref = btrfs_lookup_leaf_ref(root, leaf->start);
4693 BUG_ON(!ref);
4694
4695 ext_index = -1;
4696 nritems = btrfs_header_nritems(leaf);
4697 for (i = 0; i < nritems; i++) {
4698 btrfs_item_key_to_cpu(leaf, &key, i);
4699 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
4700 continue;
4701 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
4702 if (btrfs_file_extent_type(leaf, fi) ==
4703 BTRFS_FILE_EXTENT_INLINE)
4704 continue;
4705 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
4706 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
4707 if (bytenr == 0)
4708 continue;
4709
4710 ext_index++;
4711 if (bytenr >= group->key.objectid + group->key.offset ||
4712 bytenr + num_bytes <= group->key.objectid)
4713 continue;
4714
4715 extent_key.objectid = bytenr;
4716 extent_key.offset = num_bytes;
4717 extent_key.type = BTRFS_EXTENT_ITEM_KEY;
4718 nr_extent = 1;
4719 ret = get_new_locations(reloc_inode, &extent_key,
4720 group->key.objectid, 1,
4721 &new_extent, &nr_extent);
4722 if (ret > 0)
4723 continue;
4724 BUG_ON(ret < 0);
4725
4726 BUG_ON(ref->extents[ext_index].bytenr != bytenr);
4727 BUG_ON(ref->extents[ext_index].num_bytes != num_bytes);
4728 ref->extents[ext_index].bytenr = new_extent->disk_bytenr;
4729 ref->extents[ext_index].num_bytes = new_extent->disk_num_bytes;
4730
Zheng Yan1a40e232008-09-26 10:09:34 -04004731 btrfs_set_file_extent_disk_bytenr(leaf, fi,
4732 new_extent->disk_bytenr);
4733 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
4734 new_extent->disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04004735 btrfs_mark_buffer_dirty(leaf);
4736
4737 ret = btrfs_inc_extent_ref(trans, root,
4738 new_extent->disk_bytenr,
4739 new_extent->disk_num_bytes,
4740 leaf->start,
4741 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004742 trans->transid, key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04004743 BUG_ON(ret);
Chris Mason56bec292009-03-13 10:10:06 -04004744
Zheng Yan1a40e232008-09-26 10:09:34 -04004745 ret = btrfs_free_extent(trans, root,
4746 bytenr, num_bytes, leaf->start,
4747 btrfs_header_owner(leaf),
4748 btrfs_header_generation(leaf),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004749 key.objectid, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04004750 BUG_ON(ret);
4751 cond_resched();
4752 }
4753 kfree(new_extent);
4754 BUG_ON(ext_index + 1 != ref->nritems);
4755 btrfs_free_leaf_ref(root, ref);
4756 return 0;
4757}
4758
Yan Zhengf82d02d2008-10-29 14:49:05 -04004759int btrfs_free_reloc_root(struct btrfs_trans_handle *trans,
4760 struct btrfs_root *root)
Zheng Yan1a40e232008-09-26 10:09:34 -04004761{
4762 struct btrfs_root *reloc_root;
Yan Zhengf82d02d2008-10-29 14:49:05 -04004763 int ret;
Zheng Yan1a40e232008-09-26 10:09:34 -04004764
4765 if (root->reloc_root) {
4766 reloc_root = root->reloc_root;
4767 root->reloc_root = NULL;
4768 list_add(&reloc_root->dead_list,
4769 &root->fs_info->dead_reloc_roots);
Yan Zhengf82d02d2008-10-29 14:49:05 -04004770
4771 btrfs_set_root_bytenr(&reloc_root->root_item,
4772 reloc_root->node->start);
4773 btrfs_set_root_level(&root->root_item,
4774 btrfs_header_level(reloc_root->node));
4775 memset(&reloc_root->root_item.drop_progress, 0,
4776 sizeof(struct btrfs_disk_key));
4777 reloc_root->root_item.drop_level = 0;
4778
4779 ret = btrfs_update_root(trans, root->fs_info->tree_root,
4780 &reloc_root->root_key,
4781 &reloc_root->root_item);
4782 BUG_ON(ret);
Zheng Yan1a40e232008-09-26 10:09:34 -04004783 }
4784 return 0;
4785}
4786
4787int btrfs_drop_dead_reloc_roots(struct btrfs_root *root)
4788{
4789 struct btrfs_trans_handle *trans;
4790 struct btrfs_root *reloc_root;
4791 struct btrfs_root *prev_root = NULL;
4792 struct list_head dead_roots;
4793 int ret;
4794 unsigned long nr;
4795
4796 INIT_LIST_HEAD(&dead_roots);
4797 list_splice_init(&root->fs_info->dead_reloc_roots, &dead_roots);
4798
4799 while (!list_empty(&dead_roots)) {
4800 reloc_root = list_entry(dead_roots.prev,
4801 struct btrfs_root, dead_list);
4802 list_del_init(&reloc_root->dead_list);
4803
4804 BUG_ON(reloc_root->commit_root != NULL);
4805 while (1) {
4806 trans = btrfs_join_transaction(root, 1);
4807 BUG_ON(!trans);
4808
4809 mutex_lock(&root->fs_info->drop_mutex);
4810 ret = btrfs_drop_snapshot(trans, reloc_root);
4811 if (ret != -EAGAIN)
4812 break;
4813 mutex_unlock(&root->fs_info->drop_mutex);
4814
4815 nr = trans->blocks_used;
4816 ret = btrfs_end_transaction(trans, root);
4817 BUG_ON(ret);
4818 btrfs_btree_balance_dirty(root, nr);
4819 }
4820
4821 free_extent_buffer(reloc_root->node);
4822
4823 ret = btrfs_del_root(trans, root->fs_info->tree_root,
4824 &reloc_root->root_key);
4825 BUG_ON(ret);
4826 mutex_unlock(&root->fs_info->drop_mutex);
4827
4828 nr = trans->blocks_used;
4829 ret = btrfs_end_transaction(trans, root);
4830 BUG_ON(ret);
4831 btrfs_btree_balance_dirty(root, nr);
4832
4833 kfree(prev_root);
4834 prev_root = reloc_root;
4835 }
4836 if (prev_root) {
4837 btrfs_remove_leaf_refs(prev_root, (u64)-1, 0);
4838 kfree(prev_root);
4839 }
4840 return 0;
4841}
4842
4843int btrfs_add_dead_reloc_root(struct btrfs_root *root)
4844{
4845 list_add(&root->dead_list, &root->fs_info->dead_reloc_roots);
4846 return 0;
4847}
4848
4849int btrfs_cleanup_reloc_trees(struct btrfs_root *root)
4850{
4851 struct btrfs_root *reloc_root;
4852 struct btrfs_trans_handle *trans;
4853 struct btrfs_key location;
4854 int found;
4855 int ret;
4856
4857 mutex_lock(&root->fs_info->tree_reloc_mutex);
4858 ret = btrfs_find_dead_roots(root, BTRFS_TREE_RELOC_OBJECTID, NULL);
4859 BUG_ON(ret);
4860 found = !list_empty(&root->fs_info->dead_reloc_roots);
4861 mutex_unlock(&root->fs_info->tree_reloc_mutex);
4862
4863 if (found) {
4864 trans = btrfs_start_transaction(root, 1);
4865 BUG_ON(!trans);
4866 ret = btrfs_commit_transaction(trans, root);
4867 BUG_ON(ret);
4868 }
4869
4870 location.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
4871 location.offset = (u64)-1;
4872 location.type = BTRFS_ROOT_ITEM_KEY;
4873
4874 reloc_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
4875 BUG_ON(!reloc_root);
4876 btrfs_orphan_cleanup(reloc_root);
4877 return 0;
4878}
4879
Chris Masond3977122009-01-05 21:25:51 -05004880static noinline int init_reloc_tree(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04004881 struct btrfs_root *root)
4882{
4883 struct btrfs_root *reloc_root;
4884 struct extent_buffer *eb;
4885 struct btrfs_root_item *root_item;
4886 struct btrfs_key root_key;
4887 int ret;
4888
4889 BUG_ON(!root->ref_cows);
4890 if (root->reloc_root)
4891 return 0;
4892
4893 root_item = kmalloc(sizeof(*root_item), GFP_NOFS);
4894 BUG_ON(!root_item);
4895
4896 ret = btrfs_copy_root(trans, root, root->commit_root,
4897 &eb, BTRFS_TREE_RELOC_OBJECTID);
4898 BUG_ON(ret);
4899
4900 root_key.objectid = BTRFS_TREE_RELOC_OBJECTID;
4901 root_key.offset = root->root_key.objectid;
4902 root_key.type = BTRFS_ROOT_ITEM_KEY;
4903
4904 memcpy(root_item, &root->root_item, sizeof(root_item));
4905 btrfs_set_root_refs(root_item, 0);
4906 btrfs_set_root_bytenr(root_item, eb->start);
4907 btrfs_set_root_level(root_item, btrfs_header_level(eb));
Yan Zheng84234f32008-10-29 14:49:05 -04004908 btrfs_set_root_generation(root_item, trans->transid);
Zheng Yan1a40e232008-09-26 10:09:34 -04004909
4910 btrfs_tree_unlock(eb);
4911 free_extent_buffer(eb);
4912
4913 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
4914 &root_key, root_item);
4915 BUG_ON(ret);
4916 kfree(root_item);
4917
4918 reloc_root = btrfs_read_fs_root_no_radix(root->fs_info->tree_root,
4919 &root_key);
4920 BUG_ON(!reloc_root);
4921 reloc_root->last_trans = trans->transid;
4922 reloc_root->commit_root = NULL;
4923 reloc_root->ref_tree = &root->fs_info->reloc_ref_tree;
4924
4925 root->reloc_root = reloc_root;
4926 return 0;
4927}
4928
4929/*
4930 * Core function of space balance.
4931 *
4932 * The idea is using reloc trees to relocate tree blocks in reference
Yan Zhengf82d02d2008-10-29 14:49:05 -04004933 * counted roots. There is one reloc tree for each subvol, and all
4934 * reloc trees share same root key objectid. Reloc trees are snapshots
4935 * of the latest committed roots of subvols (root->commit_root).
4936 *
4937 * To relocate a tree block referenced by a subvol, there are two steps.
4938 * COW the block through subvol's reloc tree, then update block pointer
4939 * in the subvol to point to the new block. Since all reloc trees share
4940 * same root key objectid, doing special handing for tree blocks owned
4941 * by them is easy. Once a tree block has been COWed in one reloc tree,
4942 * we can use the resulting new block directly when the same block is
4943 * required to COW again through other reloc trees. By this way, relocated
4944 * tree blocks are shared between reloc trees, so they are also shared
4945 * between subvols.
Zheng Yan1a40e232008-09-26 10:09:34 -04004946 */
Chris Masond3977122009-01-05 21:25:51 -05004947static noinline int relocate_one_path(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04004948 struct btrfs_root *root,
4949 struct btrfs_path *path,
4950 struct btrfs_key *first_key,
4951 struct btrfs_ref_path *ref_path,
4952 struct btrfs_block_group_cache *group,
4953 struct inode *reloc_inode)
4954{
4955 struct btrfs_root *reloc_root;
4956 struct extent_buffer *eb = NULL;
4957 struct btrfs_key *keys;
4958 u64 *nodes;
4959 int level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04004960 int shared_level;
Zheng Yan1a40e232008-09-26 10:09:34 -04004961 int lowest_level = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04004962 int ret;
4963
4964 if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
4965 lowest_level = ref_path->owner_objectid;
4966
Yan Zhengf82d02d2008-10-29 14:49:05 -04004967 if (!root->ref_cows) {
Zheng Yan1a40e232008-09-26 10:09:34 -04004968 path->lowest_level = lowest_level;
4969 ret = btrfs_search_slot(trans, root, first_key, path, 0, 1);
4970 BUG_ON(ret < 0);
4971 path->lowest_level = 0;
4972 btrfs_release_path(root, path);
4973 return 0;
4974 }
4975
Zheng Yan1a40e232008-09-26 10:09:34 -04004976 mutex_lock(&root->fs_info->tree_reloc_mutex);
4977 ret = init_reloc_tree(trans, root);
4978 BUG_ON(ret);
4979 reloc_root = root->reloc_root;
4980
Yan Zhengf82d02d2008-10-29 14:49:05 -04004981 shared_level = ref_path->shared_level;
4982 ref_path->shared_level = BTRFS_MAX_LEVEL - 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04004983
Yan Zhengf82d02d2008-10-29 14:49:05 -04004984 keys = ref_path->node_keys;
4985 nodes = ref_path->new_nodes;
4986 memset(&keys[shared_level + 1], 0,
4987 sizeof(*keys) * (BTRFS_MAX_LEVEL - shared_level - 1));
4988 memset(&nodes[shared_level + 1], 0,
4989 sizeof(*nodes) * (BTRFS_MAX_LEVEL - shared_level - 1));
Zheng Yan1a40e232008-09-26 10:09:34 -04004990
Yan Zhengf82d02d2008-10-29 14:49:05 -04004991 if (nodes[lowest_level] == 0) {
4992 path->lowest_level = lowest_level;
4993 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
4994 0, 1);
4995 BUG_ON(ret);
4996 for (level = lowest_level; level < BTRFS_MAX_LEVEL; level++) {
4997 eb = path->nodes[level];
4998 if (!eb || eb == reloc_root->node)
4999 break;
5000 nodes[level] = eb->start;
5001 if (level == 0)
5002 btrfs_item_key_to_cpu(eb, &keys[level], 0);
5003 else
5004 btrfs_node_key_to_cpu(eb, &keys[level], 0);
5005 }
Yan Zheng2b820322008-11-17 21:11:30 -05005006 if (nodes[0] &&
5007 ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04005008 eb = path->nodes[0];
5009 ret = replace_extents_in_leaf(trans, reloc_root, eb,
5010 group, reloc_inode);
5011 BUG_ON(ret);
5012 }
5013 btrfs_release_path(reloc_root, path);
5014 } else {
Zheng Yan1a40e232008-09-26 10:09:34 -04005015 ret = btrfs_merge_path(trans, reloc_root, keys, nodes,
Yan Zhengf82d02d2008-10-29 14:49:05 -04005016 lowest_level);
Zheng Yan1a40e232008-09-26 10:09:34 -04005017 BUG_ON(ret);
5018 }
5019
Zheng Yan1a40e232008-09-26 10:09:34 -04005020 /*
5021 * replace tree blocks in the fs tree with tree blocks in
5022 * the reloc tree.
5023 */
5024 ret = btrfs_merge_path(trans, root, keys, nodes, lowest_level);
5025 BUG_ON(ret < 0);
5026
5027 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04005028 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
5029 0, 0);
5030 BUG_ON(ret);
5031 extent_buffer_get(path->nodes[0]);
5032 eb = path->nodes[0];
5033 btrfs_release_path(reloc_root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04005034 ret = invalidate_extent_cache(reloc_root, eb, group, root);
5035 BUG_ON(ret);
5036 free_extent_buffer(eb);
5037 }
Zheng Yan1a40e232008-09-26 10:09:34 -04005038
Yan Zhengf82d02d2008-10-29 14:49:05 -04005039 mutex_unlock(&root->fs_info->tree_reloc_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04005040 path->lowest_level = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04005041 return 0;
5042}
5043
Chris Masond3977122009-01-05 21:25:51 -05005044static noinline int relocate_tree_block(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04005045 struct btrfs_root *root,
5046 struct btrfs_path *path,
5047 struct btrfs_key *first_key,
5048 struct btrfs_ref_path *ref_path)
5049{
5050 int ret;
Zheng Yan1a40e232008-09-26 10:09:34 -04005051
5052 ret = relocate_one_path(trans, root, path, first_key,
5053 ref_path, NULL, NULL);
5054 BUG_ON(ret);
5055
Zheng Yan1a40e232008-09-26 10:09:34 -04005056 return 0;
5057}
5058
Chris Masond3977122009-01-05 21:25:51 -05005059static noinline int del_extent_zero(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04005060 struct btrfs_root *extent_root,
5061 struct btrfs_path *path,
5062 struct btrfs_key *extent_key)
5063{
5064 int ret;
5065
Zheng Yan1a40e232008-09-26 10:09:34 -04005066 ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1);
5067 if (ret)
5068 goto out;
5069 ret = btrfs_del_item(trans, extent_root, path);
5070out:
Chris Masonedbd8d42007-12-21 16:27:24 -05005071 btrfs_release_path(extent_root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04005072 return ret;
5073}
5074
Chris Masond3977122009-01-05 21:25:51 -05005075static noinline struct btrfs_root *read_ref_root(struct btrfs_fs_info *fs_info,
Zheng Yan1a40e232008-09-26 10:09:34 -04005076 struct btrfs_ref_path *ref_path)
5077{
5078 struct btrfs_key root_key;
5079
5080 root_key.objectid = ref_path->root_objectid;
5081 root_key.type = BTRFS_ROOT_ITEM_KEY;
5082 if (is_cowonly_root(ref_path->root_objectid))
5083 root_key.offset = 0;
5084 else
5085 root_key.offset = (u64)-1;
5086
5087 return btrfs_read_fs_root_no_name(fs_info, &root_key);
5088}
5089
Chris Masond3977122009-01-05 21:25:51 -05005090static noinline int relocate_one_extent(struct btrfs_root *extent_root,
Zheng Yan1a40e232008-09-26 10:09:34 -04005091 struct btrfs_path *path,
5092 struct btrfs_key *extent_key,
5093 struct btrfs_block_group_cache *group,
5094 struct inode *reloc_inode, int pass)
5095{
5096 struct btrfs_trans_handle *trans;
5097 struct btrfs_root *found_root;
5098 struct btrfs_ref_path *ref_path = NULL;
5099 struct disk_extent *new_extents = NULL;
5100 int nr_extents = 0;
5101 int loops;
5102 int ret;
5103 int level;
5104 struct btrfs_key first_key;
5105 u64 prev_block = 0;
5106
Zheng Yan1a40e232008-09-26 10:09:34 -04005107
5108 trans = btrfs_start_transaction(extent_root, 1);
5109 BUG_ON(!trans);
5110
5111 if (extent_key->objectid == 0) {
5112 ret = del_extent_zero(trans, extent_root, path, extent_key);
5113 goto out;
5114 }
5115
5116 ref_path = kmalloc(sizeof(*ref_path), GFP_NOFS);
5117 if (!ref_path) {
Chris Masond3977122009-01-05 21:25:51 -05005118 ret = -ENOMEM;
5119 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04005120 }
5121
5122 for (loops = 0; ; loops++) {
5123 if (loops == 0) {
5124 ret = btrfs_first_ref_path(trans, extent_root, ref_path,
5125 extent_key->objectid);
5126 } else {
5127 ret = btrfs_next_ref_path(trans, extent_root, ref_path);
5128 }
5129 if (ret < 0)
5130 goto out;
5131 if (ret > 0)
5132 break;
5133
5134 if (ref_path->root_objectid == BTRFS_TREE_LOG_OBJECTID ||
5135 ref_path->root_objectid == BTRFS_TREE_RELOC_OBJECTID)
5136 continue;
5137
5138 found_root = read_ref_root(extent_root->fs_info, ref_path);
5139 BUG_ON(!found_root);
5140 /*
5141 * for reference counted tree, only process reference paths
5142 * rooted at the latest committed root.
5143 */
5144 if (found_root->ref_cows &&
5145 ref_path->root_generation != found_root->root_key.offset)
5146 continue;
5147
5148 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
5149 if (pass == 0) {
5150 /*
5151 * copy data extents to new locations
5152 */
5153 u64 group_start = group->key.objectid;
5154 ret = relocate_data_extent(reloc_inode,
5155 extent_key,
5156 group_start);
5157 if (ret < 0)
5158 goto out;
5159 break;
5160 }
5161 level = 0;
5162 } else {
5163 level = ref_path->owner_objectid;
5164 }
5165
5166 if (prev_block != ref_path->nodes[level]) {
5167 struct extent_buffer *eb;
5168 u64 block_start = ref_path->nodes[level];
5169 u64 block_size = btrfs_level_size(found_root, level);
5170
5171 eb = read_tree_block(found_root, block_start,
5172 block_size, 0);
5173 btrfs_tree_lock(eb);
5174 BUG_ON(level != btrfs_header_level(eb));
5175
5176 if (level == 0)
5177 btrfs_item_key_to_cpu(eb, &first_key, 0);
5178 else
5179 btrfs_node_key_to_cpu(eb, &first_key, 0);
5180
5181 btrfs_tree_unlock(eb);
5182 free_extent_buffer(eb);
5183 prev_block = block_start;
5184 }
5185
Yan Zheng24562422009-02-12 14:14:53 -05005186 mutex_lock(&extent_root->fs_info->trans_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05005187 btrfs_record_root_in_trans(found_root);
Yan Zheng24562422009-02-12 14:14:53 -05005188 mutex_unlock(&extent_root->fs_info->trans_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05005189 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
5190 /*
5191 * try to update data extent references while
5192 * keeping metadata shared between snapshots.
5193 */
5194 if (pass == 1) {
5195 ret = relocate_one_path(trans, found_root,
5196 path, &first_key, ref_path,
5197 group, reloc_inode);
5198 if (ret < 0)
5199 goto out;
5200 continue;
5201 }
Zheng Yan1a40e232008-09-26 10:09:34 -04005202 /*
5203 * use fallback method to process the remaining
5204 * references.
5205 */
5206 if (!new_extents) {
5207 u64 group_start = group->key.objectid;
Yan Zhengd899e052008-10-30 14:25:28 -04005208 new_extents = kmalloc(sizeof(*new_extents),
5209 GFP_NOFS);
5210 nr_extents = 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04005211 ret = get_new_locations(reloc_inode,
5212 extent_key,
Yan Zhengd899e052008-10-30 14:25:28 -04005213 group_start, 1,
Zheng Yan1a40e232008-09-26 10:09:34 -04005214 &new_extents,
5215 &nr_extents);
Yan Zhengd899e052008-10-30 14:25:28 -04005216 if (ret)
Zheng Yan1a40e232008-09-26 10:09:34 -04005217 goto out;
5218 }
Zheng Yan1a40e232008-09-26 10:09:34 -04005219 ret = replace_one_extent(trans, found_root,
5220 path, extent_key,
5221 &first_key, ref_path,
5222 new_extents, nr_extents);
Yan Zhenge4404d62008-12-12 10:03:26 -05005223 } else {
Zheng Yan1a40e232008-09-26 10:09:34 -04005224 ret = relocate_tree_block(trans, found_root, path,
5225 &first_key, ref_path);
Zheng Yan1a40e232008-09-26 10:09:34 -04005226 }
5227 if (ret < 0)
5228 goto out;
5229 }
5230 ret = 0;
5231out:
5232 btrfs_end_transaction(trans, extent_root);
5233 kfree(new_extents);
5234 kfree(ref_path);
Chris Masonedbd8d42007-12-21 16:27:24 -05005235 return ret;
5236}
5237
Chris Masonec44a352008-04-28 15:29:52 -04005238static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
5239{
5240 u64 num_devices;
5241 u64 stripped = BTRFS_BLOCK_GROUP_RAID0 |
5242 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
5243
Yan Zheng2b820322008-11-17 21:11:30 -05005244 num_devices = root->fs_info->fs_devices->rw_devices;
Chris Masonec44a352008-04-28 15:29:52 -04005245 if (num_devices == 1) {
5246 stripped |= BTRFS_BLOCK_GROUP_DUP;
5247 stripped = flags & ~stripped;
5248
5249 /* turn raid0 into single device chunks */
5250 if (flags & BTRFS_BLOCK_GROUP_RAID0)
5251 return stripped;
5252
5253 /* turn mirroring into duplication */
5254 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
5255 BTRFS_BLOCK_GROUP_RAID10))
5256 return stripped | BTRFS_BLOCK_GROUP_DUP;
5257 return flags;
5258 } else {
5259 /* they already had raid on here, just return */
Chris Masonec44a352008-04-28 15:29:52 -04005260 if (flags & stripped)
5261 return flags;
5262
5263 stripped |= BTRFS_BLOCK_GROUP_DUP;
5264 stripped = flags & ~stripped;
5265
5266 /* switch duplicated blocks with raid1 */
5267 if (flags & BTRFS_BLOCK_GROUP_DUP)
5268 return stripped | BTRFS_BLOCK_GROUP_RAID1;
5269
5270 /* turn single device chunks into raid0 */
5271 return stripped | BTRFS_BLOCK_GROUP_RAID0;
5272 }
5273 return flags;
5274}
5275
Christoph Hellwigb2950862008-12-02 09:54:17 -05005276static int __alloc_chunk_for_shrink(struct btrfs_root *root,
Chris Mason0ef3e662008-05-24 14:04:53 -04005277 struct btrfs_block_group_cache *shrink_block_group,
5278 int force)
5279{
5280 struct btrfs_trans_handle *trans;
5281 u64 new_alloc_flags;
5282 u64 calc;
5283
Chris Masonc286ac42008-07-22 23:06:41 -04005284 spin_lock(&shrink_block_group->lock);
Chris Mason0ef3e662008-05-24 14:04:53 -04005285 if (btrfs_block_group_used(&shrink_block_group->item) > 0) {
Chris Masonc286ac42008-07-22 23:06:41 -04005286 spin_unlock(&shrink_block_group->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04005287
Chris Mason0ef3e662008-05-24 14:04:53 -04005288 trans = btrfs_start_transaction(root, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04005289 spin_lock(&shrink_block_group->lock);
Chris Mason7d9eb122008-07-08 14:19:17 -04005290
Chris Mason0ef3e662008-05-24 14:04:53 -04005291 new_alloc_flags = update_block_group_flags(root,
5292 shrink_block_group->flags);
5293 if (new_alloc_flags != shrink_block_group->flags) {
5294 calc =
5295 btrfs_block_group_used(&shrink_block_group->item);
5296 } else {
5297 calc = shrink_block_group->key.offset;
5298 }
Chris Masonc286ac42008-07-22 23:06:41 -04005299 spin_unlock(&shrink_block_group->lock);
5300
Chris Mason0ef3e662008-05-24 14:04:53 -04005301 do_chunk_alloc(trans, root->fs_info->extent_root,
5302 calc + 2 * 1024 * 1024, new_alloc_flags, force);
Chris Mason7d9eb122008-07-08 14:19:17 -04005303
Chris Mason0ef3e662008-05-24 14:04:53 -04005304 btrfs_end_transaction(trans, root);
Chris Masonc286ac42008-07-22 23:06:41 -04005305 } else
5306 spin_unlock(&shrink_block_group->lock);
Chris Mason0ef3e662008-05-24 14:04:53 -04005307 return 0;
5308}
5309
Zheng Yan1a40e232008-09-26 10:09:34 -04005310static int __insert_orphan_inode(struct btrfs_trans_handle *trans,
5311 struct btrfs_root *root,
5312 u64 objectid, u64 size)
5313{
5314 struct btrfs_path *path;
5315 struct btrfs_inode_item *item;
5316 struct extent_buffer *leaf;
5317 int ret;
5318
5319 path = btrfs_alloc_path();
5320 if (!path)
5321 return -ENOMEM;
5322
5323 ret = btrfs_insert_empty_inode(trans, root, path, objectid);
5324 if (ret)
5325 goto out;
5326
5327 leaf = path->nodes[0];
5328 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_inode_item);
5329 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
5330 btrfs_set_inode_generation(leaf, item, 1);
5331 btrfs_set_inode_size(leaf, item, size);
5332 btrfs_set_inode_mode(leaf, item, S_IFREG | 0600);
Yan Zheng0403e472008-12-10 20:32:51 -05005333 btrfs_set_inode_flags(leaf, item, BTRFS_INODE_NOCOMPRESS);
Zheng Yan1a40e232008-09-26 10:09:34 -04005334 btrfs_mark_buffer_dirty(leaf);
5335 btrfs_release_path(root, path);
5336out:
5337 btrfs_free_path(path);
5338 return ret;
5339}
5340
Chris Masond3977122009-01-05 21:25:51 -05005341static noinline struct inode *create_reloc_inode(struct btrfs_fs_info *fs_info,
Zheng Yan1a40e232008-09-26 10:09:34 -04005342 struct btrfs_block_group_cache *group)
5343{
5344 struct inode *inode = NULL;
5345 struct btrfs_trans_handle *trans;
5346 struct btrfs_root *root;
5347 struct btrfs_key root_key;
5348 u64 objectid = BTRFS_FIRST_FREE_OBJECTID;
5349 int err = 0;
5350
5351 root_key.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
5352 root_key.type = BTRFS_ROOT_ITEM_KEY;
5353 root_key.offset = (u64)-1;
5354 root = btrfs_read_fs_root_no_name(fs_info, &root_key);
5355 if (IS_ERR(root))
5356 return ERR_CAST(root);
5357
5358 trans = btrfs_start_transaction(root, 1);
5359 BUG_ON(!trans);
5360
5361 err = btrfs_find_free_objectid(trans, root, objectid, &objectid);
5362 if (err)
5363 goto out;
5364
5365 err = __insert_orphan_inode(trans, root, objectid, group->key.offset);
5366 BUG_ON(err);
5367
5368 err = btrfs_insert_file_extent(trans, root, objectid, 0, 0, 0,
Chris Masonc8b97812008-10-29 14:49:59 -04005369 group->key.offset, 0, group->key.offset,
5370 0, 0, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04005371 BUG_ON(err);
5372
5373 inode = btrfs_iget_locked(root->fs_info->sb, objectid, root);
5374 if (inode->i_state & I_NEW) {
5375 BTRFS_I(inode)->root = root;
5376 BTRFS_I(inode)->location.objectid = objectid;
5377 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
5378 BTRFS_I(inode)->location.offset = 0;
5379 btrfs_read_locked_inode(inode);
5380 unlock_new_inode(inode);
5381 BUG_ON(is_bad_inode(inode));
5382 } else {
5383 BUG_ON(1);
5384 }
Yan Zheng17d217f2008-12-12 10:03:38 -05005385 BTRFS_I(inode)->index_cnt = group->key.objectid;
Zheng Yan1a40e232008-09-26 10:09:34 -04005386
5387 err = btrfs_orphan_add(trans, inode);
5388out:
5389 btrfs_end_transaction(trans, root);
5390 if (err) {
5391 if (inode)
5392 iput(inode);
5393 inode = ERR_PTR(err);
5394 }
5395 return inode;
5396}
5397
Yan Zheng17d217f2008-12-12 10:03:38 -05005398int btrfs_reloc_clone_csums(struct inode *inode, u64 file_pos, u64 len)
5399{
5400
5401 struct btrfs_ordered_sum *sums;
5402 struct btrfs_sector_sum *sector_sum;
5403 struct btrfs_ordered_extent *ordered;
5404 struct btrfs_root *root = BTRFS_I(inode)->root;
5405 struct list_head list;
5406 size_t offset;
5407 int ret;
5408 u64 disk_bytenr;
5409
5410 INIT_LIST_HEAD(&list);
5411
5412 ordered = btrfs_lookup_ordered_extent(inode, file_pos);
5413 BUG_ON(ordered->file_offset != file_pos || ordered->len != len);
5414
5415 disk_bytenr = file_pos + BTRFS_I(inode)->index_cnt;
Yan Zheng07d400a2009-01-06 11:42:00 -05005416 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, disk_bytenr,
Yan Zheng17d217f2008-12-12 10:03:38 -05005417 disk_bytenr + len - 1, &list);
5418
5419 while (!list_empty(&list)) {
5420 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
5421 list_del_init(&sums->list);
5422
5423 sector_sum = sums->sums;
5424 sums->bytenr = ordered->start;
5425
5426 offset = 0;
5427 while (offset < sums->len) {
5428 sector_sum->bytenr += ordered->start - disk_bytenr;
5429 sector_sum++;
5430 offset += root->sectorsize;
5431 }
5432
5433 btrfs_add_ordered_sum(inode, ordered, sums);
5434 }
5435 btrfs_put_ordered_extent(ordered);
5436 return 0;
5437}
5438
Zheng Yan1a40e232008-09-26 10:09:34 -04005439int btrfs_relocate_block_group(struct btrfs_root *root, u64 group_start)
Chris Masonedbd8d42007-12-21 16:27:24 -05005440{
5441 struct btrfs_trans_handle *trans;
Chris Masonedbd8d42007-12-21 16:27:24 -05005442 struct btrfs_path *path;
Zheng Yan1a40e232008-09-26 10:09:34 -04005443 struct btrfs_fs_info *info = root->fs_info;
5444 struct extent_buffer *leaf;
5445 struct inode *reloc_inode;
5446 struct btrfs_block_group_cache *block_group;
5447 struct btrfs_key key;
Yan Zhengd899e052008-10-30 14:25:28 -04005448 u64 skipped;
Chris Masonedbd8d42007-12-21 16:27:24 -05005449 u64 cur_byte;
5450 u64 total_found;
Chris Masonedbd8d42007-12-21 16:27:24 -05005451 u32 nritems;
5452 int ret;
Chris Masona061fc82008-05-07 11:43:44 -04005453 int progress;
Zheng Yan1a40e232008-09-26 10:09:34 -04005454 int pass = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05005455
Chris Masonedbd8d42007-12-21 16:27:24 -05005456 root = root->fs_info->extent_root;
Zheng Yan1a40e232008-09-26 10:09:34 -04005457
5458 block_group = btrfs_lookup_block_group(info, group_start);
5459 BUG_ON(!block_group);
Chris Masonedbd8d42007-12-21 16:27:24 -05005460
Chris Masond3977122009-01-05 21:25:51 -05005461 printk(KERN_INFO "btrfs relocating block group %llu flags %llu\n",
Zheng Yan1a40e232008-09-26 10:09:34 -04005462 (unsigned long long)block_group->key.objectid,
5463 (unsigned long long)block_group->flags);
Chris Mason323da792008-05-09 11:46:48 -04005464
Zheng Yan1a40e232008-09-26 10:09:34 -04005465 path = btrfs_alloc_path();
5466 BUG_ON(!path);
Chris Mason323da792008-05-09 11:46:48 -04005467
Zheng Yan1a40e232008-09-26 10:09:34 -04005468 reloc_inode = create_reloc_inode(info, block_group);
5469 BUG_ON(IS_ERR(reloc_inode));
5470
Zheng Yan1a40e232008-09-26 10:09:34 -04005471 __alloc_chunk_for_shrink(root, block_group, 1);
Yan Zhengc146afa2008-11-12 14:34:12 -05005472 set_block_group_readonly(block_group);
Zheng Yan1a40e232008-09-26 10:09:34 -04005473
Zheng Yan1a40e232008-09-26 10:09:34 -04005474 btrfs_start_delalloc_inodes(info->tree_root);
5475 btrfs_wait_ordered_extents(info->tree_root, 0);
Chris Mason0ef3e662008-05-24 14:04:53 -04005476again:
Yan Zhengd899e052008-10-30 14:25:28 -04005477 skipped = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05005478 total_found = 0;
Chris Masona061fc82008-05-07 11:43:44 -04005479 progress = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04005480 key.objectid = block_group->key.objectid;
Chris Masonedbd8d42007-12-21 16:27:24 -05005481 key.offset = 0;
5482 key.type = 0;
Yan73e48b22008-01-03 14:14:39 -05005483 cur_byte = key.objectid;
Chris Mason4313b392008-01-03 09:08:48 -05005484
Zheng Yan1a40e232008-09-26 10:09:34 -04005485 trans = btrfs_start_transaction(info->tree_root, 1);
5486 btrfs_commit_transaction(trans, info->tree_root);
Chris Masonea8c2812008-08-04 23:17:27 -04005487
Zheng Yan1a40e232008-09-26 10:09:34 -04005488 mutex_lock(&root->fs_info->cleaner_mutex);
5489 btrfs_clean_old_snapshots(info->tree_root);
5490 btrfs_remove_leaf_refs(info->tree_root, (u64)-1, 1);
5491 mutex_unlock(&root->fs_info->cleaner_mutex);
Chris Masonea8c2812008-08-04 23:17:27 -04005492
Chris Mason56bec292009-03-13 10:10:06 -04005493 trans = btrfs_start_transaction(info->tree_root, 1);
5494 btrfs_commit_transaction(trans, info->tree_root);
5495
Chris Masond3977122009-01-05 21:25:51 -05005496 while (1) {
Chris Masonedbd8d42007-12-21 16:27:24 -05005497 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5498 if (ret < 0)
5499 goto out;
Chris Mason7d9eb122008-07-08 14:19:17 -04005500next:
Chris Masonedbd8d42007-12-21 16:27:24 -05005501 leaf = path->nodes[0];
Chris Masonedbd8d42007-12-21 16:27:24 -05005502 nritems = btrfs_header_nritems(leaf);
Yan73e48b22008-01-03 14:14:39 -05005503 if (path->slots[0] >= nritems) {
5504 ret = btrfs_next_leaf(root, path);
5505 if (ret < 0)
5506 goto out;
5507 if (ret == 1) {
5508 ret = 0;
5509 break;
Chris Masonedbd8d42007-12-21 16:27:24 -05005510 }
Yan73e48b22008-01-03 14:14:39 -05005511 leaf = path->nodes[0];
5512 nritems = btrfs_header_nritems(leaf);
5513 }
5514
Zheng Yan1a40e232008-09-26 10:09:34 -04005515 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Chris Mason725c8462008-01-04 16:47:16 -05005516
Zheng Yan1a40e232008-09-26 10:09:34 -04005517 if (key.objectid >= block_group->key.objectid +
5518 block_group->key.offset)
Chris Mason8f18cf12008-04-25 16:53:30 -04005519 break;
5520
Chris Mason725c8462008-01-04 16:47:16 -05005521 if (progress && need_resched()) {
Chris Mason725c8462008-01-04 16:47:16 -05005522 btrfs_release_path(root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04005523 cond_resched();
Chris Mason725c8462008-01-04 16:47:16 -05005524 progress = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04005525 continue;
Chris Mason725c8462008-01-04 16:47:16 -05005526 }
5527 progress = 1;
5528
Zheng Yan1a40e232008-09-26 10:09:34 -04005529 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY ||
5530 key.objectid + key.offset <= cur_byte) {
Yan73e48b22008-01-03 14:14:39 -05005531 path->slots[0]++;
Chris Masonedbd8d42007-12-21 16:27:24 -05005532 goto next;
5533 }
Yan73e48b22008-01-03 14:14:39 -05005534
Chris Masonedbd8d42007-12-21 16:27:24 -05005535 total_found++;
Zheng Yan1a40e232008-09-26 10:09:34 -04005536 cur_byte = key.objectid + key.offset;
Chris Masonedbd8d42007-12-21 16:27:24 -05005537 btrfs_release_path(root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04005538
5539 __alloc_chunk_for_shrink(root, block_group, 0);
5540 ret = relocate_one_extent(root, path, &key, block_group,
5541 reloc_inode, pass);
5542 BUG_ON(ret < 0);
Yan Zhengd899e052008-10-30 14:25:28 -04005543 if (ret > 0)
5544 skipped++;
Zheng Yan1a40e232008-09-26 10:09:34 -04005545
5546 key.objectid = cur_byte;
5547 key.type = 0;
5548 key.offset = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05005549 }
5550
5551 btrfs_release_path(root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04005552
5553 if (pass == 0) {
5554 btrfs_wait_ordered_range(reloc_inode, 0, (u64)-1);
5555 invalidate_mapping_pages(reloc_inode->i_mapping, 0, -1);
Zheng Yan1a40e232008-09-26 10:09:34 -04005556 }
Chris Masonedbd8d42007-12-21 16:27:24 -05005557
5558 if (total_found > 0) {
Chris Masond3977122009-01-05 21:25:51 -05005559 printk(KERN_INFO "btrfs found %llu extents in pass %d\n",
Zheng Yan1a40e232008-09-26 10:09:34 -04005560 (unsigned long long)total_found, pass);
5561 pass++;
Yan Zhengd899e052008-10-30 14:25:28 -04005562 if (total_found == skipped && pass > 2) {
5563 iput(reloc_inode);
5564 reloc_inode = create_reloc_inode(info, block_group);
5565 pass = 0;
5566 }
Chris Masonedbd8d42007-12-21 16:27:24 -05005567 goto again;
5568 }
5569
Zheng Yan1a40e232008-09-26 10:09:34 -04005570 /* delete reloc_inode */
5571 iput(reloc_inode);
Chris Mason7d9eb122008-07-08 14:19:17 -04005572
Zheng Yan1a40e232008-09-26 10:09:34 -04005573 /* unpin extents in this range */
5574 trans = btrfs_start_transaction(info->tree_root, 1);
5575 btrfs_commit_transaction(trans, info->tree_root);
Chris Mason0ef3e662008-05-24 14:04:53 -04005576
Zheng Yan1a40e232008-09-26 10:09:34 -04005577 spin_lock(&block_group->lock);
5578 WARN_ON(block_group->pinned > 0);
5579 WARN_ON(block_group->reserved > 0);
5580 WARN_ON(btrfs_block_group_used(&block_group->item) > 0);
5581 spin_unlock(&block_group->lock);
Yan Zhengd2fb3432008-12-11 16:30:39 -05005582 put_block_group(block_group);
Zheng Yan1a40e232008-09-26 10:09:34 -04005583 ret = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05005584out:
Zheng Yan1a40e232008-09-26 10:09:34 -04005585 btrfs_free_path(path);
Chris Masonedbd8d42007-12-21 16:27:24 -05005586 return ret;
5587}
5588
Christoph Hellwigb2950862008-12-02 09:54:17 -05005589static int find_first_block_group(struct btrfs_root *root,
5590 struct btrfs_path *path, struct btrfs_key *key)
Chris Mason0b86a832008-03-24 15:01:56 -04005591{
Chris Mason925baed2008-06-25 16:01:30 -04005592 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005593 struct btrfs_key found_key;
5594 struct extent_buffer *leaf;
5595 int slot;
5596
5597 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
5598 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04005599 goto out;
5600
Chris Masond3977122009-01-05 21:25:51 -05005601 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04005602 slot = path->slots[0];
5603 leaf = path->nodes[0];
5604 if (slot >= btrfs_header_nritems(leaf)) {
5605 ret = btrfs_next_leaf(root, path);
5606 if (ret == 0)
5607 continue;
5608 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04005609 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04005610 break;
5611 }
5612 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5613
5614 if (found_key.objectid >= key->objectid &&
Chris Mason925baed2008-06-25 16:01:30 -04005615 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
5616 ret = 0;
5617 goto out;
5618 }
Chris Mason0b86a832008-03-24 15:01:56 -04005619 path->slots[0]++;
5620 }
5621 ret = -ENOENT;
Chris Mason925baed2008-06-25 16:01:30 -04005622out:
Chris Mason0b86a832008-03-24 15:01:56 -04005623 return ret;
5624}
5625
Zheng Yan1a40e232008-09-26 10:09:34 -04005626int btrfs_free_block_groups(struct btrfs_fs_info *info)
5627{
5628 struct btrfs_block_group_cache *block_group;
Chris Mason4184ea72009-03-10 12:39:20 -04005629 struct btrfs_space_info *space_info;
Zheng Yan1a40e232008-09-26 10:09:34 -04005630 struct rb_node *n;
5631
Zheng Yan1a40e232008-09-26 10:09:34 -04005632 spin_lock(&info->block_group_cache_lock);
5633 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
5634 block_group = rb_entry(n, struct btrfs_block_group_cache,
5635 cache_node);
Zheng Yan1a40e232008-09-26 10:09:34 -04005636 rb_erase(&block_group->cache_node,
5637 &info->block_group_cache_tree);
Yan Zhengd899e052008-10-30 14:25:28 -04005638 spin_unlock(&info->block_group_cache_lock);
5639
5640 btrfs_remove_free_space_cache(block_group);
Josef Bacik80eb2342008-10-29 14:49:05 -04005641 down_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04005642 list_del(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04005643 up_write(&block_group->space_info->groups_sem);
Yan Zhengd2fb3432008-12-11 16:30:39 -05005644
5645 WARN_ON(atomic_read(&block_group->count) != 1);
Zheng Yan1a40e232008-09-26 10:09:34 -04005646 kfree(block_group);
Yan Zhengd899e052008-10-30 14:25:28 -04005647
5648 spin_lock(&info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04005649 }
5650 spin_unlock(&info->block_group_cache_lock);
Chris Mason4184ea72009-03-10 12:39:20 -04005651
5652 /* now that all the block groups are freed, go through and
5653 * free all the space_info structs. This is only called during
5654 * the final stages of unmount, and so we know nobody is
5655 * using them. We call synchronize_rcu() once before we start,
5656 * just to be on the safe side.
5657 */
5658 synchronize_rcu();
5659
5660 while(!list_empty(&info->space_info)) {
5661 space_info = list_entry(info->space_info.next,
5662 struct btrfs_space_info,
5663 list);
5664
5665 list_del(&space_info->list);
5666 kfree(space_info);
5667 }
Zheng Yan1a40e232008-09-26 10:09:34 -04005668 return 0;
5669}
5670
Chris Mason9078a3e2007-04-26 16:46:15 -04005671int btrfs_read_block_groups(struct btrfs_root *root)
5672{
5673 struct btrfs_path *path;
5674 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04005675 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04005676 struct btrfs_fs_info *info = root->fs_info;
Chris Mason6324fbf2008-03-24 15:01:59 -04005677 struct btrfs_space_info *space_info;
Chris Mason9078a3e2007-04-26 16:46:15 -04005678 struct btrfs_key key;
5679 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -04005680 struct extent_buffer *leaf;
Chris Mason96b51792007-10-15 16:15:19 -04005681
Chris Masonbe744172007-05-06 10:15:01 -04005682 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04005683 key.objectid = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005684 key.offset = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04005685 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
Chris Mason9078a3e2007-04-26 16:46:15 -04005686 path = btrfs_alloc_path();
5687 if (!path)
5688 return -ENOMEM;
5689
Chris Masond3977122009-01-05 21:25:51 -05005690 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04005691 ret = find_first_block_group(root, path, &key);
5692 if (ret > 0) {
5693 ret = 0;
5694 goto error;
Chris Mason9078a3e2007-04-26 16:46:15 -04005695 }
Chris Mason0b86a832008-03-24 15:01:56 -04005696 if (ret != 0)
5697 goto error;
5698
Chris Mason5f39d392007-10-15 16:14:19 -04005699 leaf = path->nodes[0];
5700 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason8f18cf12008-04-25 16:53:30 -04005701 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -04005702 if (!cache) {
Chris Mason0b86a832008-03-24 15:01:56 -04005703 ret = -ENOMEM;
Chris Mason9078a3e2007-04-26 16:46:15 -04005704 break;
5705 }
Chris Mason3e1ad542007-05-07 20:03:49 -04005706
Yan Zhengd2fb3432008-12-11 16:30:39 -05005707 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04005708 spin_lock_init(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04005709 mutex_init(&cache->alloc_mutex);
Josef Bacikea6a4782008-11-20 12:16:16 -05005710 mutex_init(&cache->cache_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005711 INIT_LIST_HEAD(&cache->list);
Chris Mason5f39d392007-10-15 16:14:19 -04005712 read_extent_buffer(leaf, &cache->item,
5713 btrfs_item_ptr_offset(leaf, path->slots[0]),
5714 sizeof(cache->item));
Chris Mason9078a3e2007-04-26 16:46:15 -04005715 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Mason0b86a832008-03-24 15:01:56 -04005716
Chris Mason9078a3e2007-04-26 16:46:15 -04005717 key.objectid = found_key.objectid + found_key.offset;
5718 btrfs_release_path(root, path);
Chris Mason0b86a832008-03-24 15:01:56 -04005719 cache->flags = btrfs_block_group_flags(&cache->item);
Chris Mason96b51792007-10-15 16:15:19 -04005720
Chris Mason6324fbf2008-03-24 15:01:59 -04005721 ret = update_space_info(info, cache->flags, found_key.offset,
5722 btrfs_block_group_used(&cache->item),
5723 &space_info);
5724 BUG_ON(ret);
5725 cache->space_info = space_info;
Josef Bacik80eb2342008-10-29 14:49:05 -04005726 down_write(&space_info->groups_sem);
5727 list_add_tail(&cache->list, &space_info->block_groups);
5728 up_write(&space_info->groups_sem);
Chris Mason6324fbf2008-03-24 15:01:59 -04005729
Josef Bacik0f9dd462008-09-23 13:14:11 -04005730 ret = btrfs_add_block_group_cache(root->fs_info, cache);
5731 BUG_ON(ret);
Chris Mason75ccf472008-09-30 19:24:06 -04005732
5733 set_avail_alloc_bits(root->fs_info, cache->flags);
Yan Zheng2b820322008-11-17 21:11:30 -05005734 if (btrfs_chunk_readonly(root, cache->key.objectid))
5735 set_block_group_readonly(cache);
Chris Mason9078a3e2007-04-26 16:46:15 -04005736 }
Chris Mason0b86a832008-03-24 15:01:56 -04005737 ret = 0;
5738error:
Chris Mason9078a3e2007-04-26 16:46:15 -04005739 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04005740 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04005741}
Chris Mason6324fbf2008-03-24 15:01:59 -04005742
5743int btrfs_make_block_group(struct btrfs_trans_handle *trans,
5744 struct btrfs_root *root, u64 bytes_used,
Chris Masone17cade2008-04-15 15:41:47 -04005745 u64 type, u64 chunk_objectid, u64 chunk_offset,
Chris Mason6324fbf2008-03-24 15:01:59 -04005746 u64 size)
5747{
5748 int ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04005749 struct btrfs_root *extent_root;
5750 struct btrfs_block_group_cache *cache;
Chris Mason6324fbf2008-03-24 15:01:59 -04005751
5752 extent_root = root->fs_info->extent_root;
Chris Mason6324fbf2008-03-24 15:01:59 -04005753
Chris Masone02119d2008-09-05 16:13:11 -04005754 root->fs_info->last_trans_new_blockgroup = trans->transid;
5755
Chris Mason8f18cf12008-04-25 16:53:30 -04005756 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005757 if (!cache)
5758 return -ENOMEM;
5759
Chris Masone17cade2008-04-15 15:41:47 -04005760 cache->key.objectid = chunk_offset;
Chris Mason6324fbf2008-03-24 15:01:59 -04005761 cache->key.offset = size;
Yan Zhengd2fb3432008-12-11 16:30:39 -05005762 cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
5763 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04005764 spin_lock_init(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04005765 mutex_init(&cache->alloc_mutex);
Josef Bacikea6a4782008-11-20 12:16:16 -05005766 mutex_init(&cache->cache_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005767 INIT_LIST_HEAD(&cache->list);
Chris Mason0ef3e662008-05-24 14:04:53 -04005768
Chris Mason6324fbf2008-03-24 15:01:59 -04005769 btrfs_set_block_group_used(&cache->item, bytes_used);
Chris Mason6324fbf2008-03-24 15:01:59 -04005770 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
5771 cache->flags = type;
5772 btrfs_set_block_group_flags(&cache->item, type);
5773
5774 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
5775 &cache->space_info);
5776 BUG_ON(ret);
Josef Bacik80eb2342008-10-29 14:49:05 -04005777 down_write(&cache->space_info->groups_sem);
5778 list_add_tail(&cache->list, &cache->space_info->block_groups);
5779 up_write(&cache->space_info->groups_sem);
Chris Mason6324fbf2008-03-24 15:01:59 -04005780
Josef Bacik0f9dd462008-09-23 13:14:11 -04005781 ret = btrfs_add_block_group_cache(root->fs_info, cache);
5782 BUG_ON(ret);
Chris Masonc286ac42008-07-22 23:06:41 -04005783
Chris Mason6324fbf2008-03-24 15:01:59 -04005784 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
5785 sizeof(cache->item));
5786 BUG_ON(ret);
5787
Chris Masond18a2c42008-04-04 15:40:00 -04005788 set_avail_alloc_bits(extent_root->fs_info, type);
Chris Mason925baed2008-06-25 16:01:30 -04005789
Chris Mason6324fbf2008-03-24 15:01:59 -04005790 return 0;
5791}
Zheng Yan1a40e232008-09-26 10:09:34 -04005792
5793int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
5794 struct btrfs_root *root, u64 group_start)
5795{
5796 struct btrfs_path *path;
5797 struct btrfs_block_group_cache *block_group;
5798 struct btrfs_key key;
5799 int ret;
5800
Zheng Yan1a40e232008-09-26 10:09:34 -04005801 root = root->fs_info->extent_root;
5802
5803 block_group = btrfs_lookup_block_group(root->fs_info, group_start);
5804 BUG_ON(!block_group);
Yan Zhengc146afa2008-11-12 14:34:12 -05005805 BUG_ON(!block_group->ro);
Zheng Yan1a40e232008-09-26 10:09:34 -04005806
5807 memcpy(&key, &block_group->key, sizeof(key));
5808
5809 path = btrfs_alloc_path();
5810 BUG_ON(!path);
5811
Yan Zheng3dfdb932009-01-21 10:49:16 -05005812 spin_lock(&root->fs_info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04005813 rb_erase(&block_group->cache_node,
5814 &root->fs_info->block_group_cache_tree);
Yan Zheng3dfdb932009-01-21 10:49:16 -05005815 spin_unlock(&root->fs_info->block_group_cache_lock);
5816 btrfs_remove_free_space_cache(block_group);
Josef Bacik80eb2342008-10-29 14:49:05 -04005817 down_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04005818 list_del(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04005819 up_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04005820
Yan Zhengc146afa2008-11-12 14:34:12 -05005821 spin_lock(&block_group->space_info->lock);
5822 block_group->space_info->total_bytes -= block_group->key.offset;
5823 block_group->space_info->bytes_readonly -= block_group->key.offset;
5824 spin_unlock(&block_group->space_info->lock);
Yan Zheng2b820322008-11-17 21:11:30 -05005825 block_group->space_info->full = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05005826
Yan Zhengd2fb3432008-12-11 16:30:39 -05005827 put_block_group(block_group);
5828 put_block_group(block_group);
Zheng Yan1a40e232008-09-26 10:09:34 -04005829
5830 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
5831 if (ret > 0)
5832 ret = -EIO;
5833 if (ret < 0)
5834 goto out;
5835
5836 ret = btrfs_del_item(trans, root, path);
5837out:
5838 btrfs_free_path(path);
5839 return ret;
5840}