blob: 0671f5b77eb86573cce906d28923b90aaad45f03 [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>
Josef Bacik817d52f2009-07-13 21:29:25 -040024#include <linux/kthread.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
Chris Mason4b4e25f2008-11-20 10:22:27 -050026#include "compat.h"
Chris Mason74493f72007-12-11 09:25:06 -050027#include "hash.h"
Chris Masonfec577f2007-02-26 10:40:21 -050028#include "ctree.h"
29#include "disk-io.h"
30#include "print-tree.h"
Chris Masone089f052007-03-16 16:20:31 -040031#include "transaction.h"
Chris Mason0b86a832008-03-24 15:01:56 -040032#include "volumes.h"
Chris Mason925baed2008-06-25 16:01:30 -040033#include "locking.h"
Chris Masonfa9c0d792009-04-03 09:47:43 -040034#include "free-space-cache.h"
Chris Masonfec577f2007-02-26 10:40:21 -050035
Josef Bacikf3465ca2008-11-12 14:19:50 -050036static int update_block_group(struct btrfs_trans_handle *trans,
37 struct btrfs_root *root,
Yan, Zhengf0486c62010-05-16 10:46:25 -040038 u64 bytenr, u64 num_bytes, int alloc);
Yan Zheng5d4f98a2009-06-10 10:45:14 -040039static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
40 struct btrfs_root *root,
41 u64 bytenr, u64 num_bytes, u64 parent,
42 u64 root_objectid, u64 owner_objectid,
43 u64 owner_offset, int refs_to_drop,
44 struct btrfs_delayed_extent_op *extra_op);
45static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
46 struct extent_buffer *leaf,
47 struct btrfs_extent_item *ei);
48static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
49 struct btrfs_root *root,
50 u64 parent, u64 root_objectid,
51 u64 flags, u64 owner, u64 offset,
52 struct btrfs_key *ins, int ref_mod);
53static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
54 struct btrfs_root *root,
55 u64 parent, u64 root_objectid,
56 u64 flags, struct btrfs_disk_key *key,
57 int level, struct btrfs_key *ins);
Josef Bacik6a632092009-02-20 11:00:09 -050058static int do_chunk_alloc(struct btrfs_trans_handle *trans,
59 struct btrfs_root *extent_root, u64 alloc_bytes,
60 u64 flags, int force);
Yan Zheng11833d62009-09-11 16:11:19 -040061static int find_next_key(struct btrfs_path *path, int level,
62 struct btrfs_key *key);
Josef Bacik9ed74f22009-09-11 16:12:44 -040063static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
64 int dump_block_groups);
Josef Bacik6a632092009-02-20 11:00:09 -050065
Josef Bacik817d52f2009-07-13 21:29:25 -040066static noinline int
67block_group_cache_done(struct btrfs_block_group_cache *cache)
68{
69 smp_mb();
70 return cache->cached == BTRFS_CACHE_FINISHED;
71}
72
Josef Bacik0f9dd462008-09-23 13:14:11 -040073static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
74{
75 return (cache->flags & bits) == bits;
76}
77
Josef Bacik11dfe352009-11-13 20:12:59 +000078void btrfs_get_block_group(struct btrfs_block_group_cache *cache)
79{
80 atomic_inc(&cache->count);
81}
82
83void btrfs_put_block_group(struct btrfs_block_group_cache *cache)
84{
Yan, Zhengf0486c62010-05-16 10:46:25 -040085 if (atomic_dec_and_test(&cache->count)) {
86 WARN_ON(cache->pinned > 0);
87 WARN_ON(cache->reserved > 0);
88 WARN_ON(cache->reserved_pinned > 0);
Josef Bacik11dfe352009-11-13 20:12:59 +000089 kfree(cache);
Yan, Zhengf0486c62010-05-16 10:46:25 -040090 }
Josef Bacik11dfe352009-11-13 20:12:59 +000091}
92
Josef Bacik0f9dd462008-09-23 13:14:11 -040093/*
94 * this adds the block group to the fs_info rb tree for the block group
95 * cache
96 */
Christoph Hellwigb2950862008-12-02 09:54:17 -050097static int btrfs_add_block_group_cache(struct btrfs_fs_info *info,
Josef Bacik0f9dd462008-09-23 13:14:11 -040098 struct btrfs_block_group_cache *block_group)
99{
100 struct rb_node **p;
101 struct rb_node *parent = NULL;
102 struct btrfs_block_group_cache *cache;
103
104 spin_lock(&info->block_group_cache_lock);
105 p = &info->block_group_cache_tree.rb_node;
106
107 while (*p) {
108 parent = *p;
109 cache = rb_entry(parent, struct btrfs_block_group_cache,
110 cache_node);
111 if (block_group->key.objectid < cache->key.objectid) {
112 p = &(*p)->rb_left;
113 } else if (block_group->key.objectid > cache->key.objectid) {
114 p = &(*p)->rb_right;
115 } else {
116 spin_unlock(&info->block_group_cache_lock);
117 return -EEXIST;
118 }
119 }
120
121 rb_link_node(&block_group->cache_node, parent, p);
122 rb_insert_color(&block_group->cache_node,
123 &info->block_group_cache_tree);
124 spin_unlock(&info->block_group_cache_lock);
125
126 return 0;
127}
128
129/*
130 * This will return the block group at or after bytenr if contains is 0, else
131 * it will return the block group that contains the bytenr
132 */
133static struct btrfs_block_group_cache *
134block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr,
135 int contains)
136{
137 struct btrfs_block_group_cache *cache, *ret = NULL;
138 struct rb_node *n;
139 u64 end, start;
140
141 spin_lock(&info->block_group_cache_lock);
142 n = info->block_group_cache_tree.rb_node;
143
144 while (n) {
145 cache = rb_entry(n, struct btrfs_block_group_cache,
146 cache_node);
147 end = cache->key.objectid + cache->key.offset - 1;
148 start = cache->key.objectid;
149
150 if (bytenr < start) {
151 if (!contains && (!ret || start < ret->key.objectid))
152 ret = cache;
153 n = n->rb_left;
154 } else if (bytenr > start) {
155 if (contains && bytenr <= end) {
156 ret = cache;
157 break;
158 }
159 n = n->rb_right;
160 } else {
161 ret = cache;
162 break;
163 }
164 }
Yan Zhengd2fb3432008-12-11 16:30:39 -0500165 if (ret)
Josef Bacik11dfe352009-11-13 20:12:59 +0000166 btrfs_get_block_group(ret);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400167 spin_unlock(&info->block_group_cache_lock);
168
169 return ret;
170}
171
Yan Zheng11833d62009-09-11 16:11:19 -0400172static int add_excluded_extent(struct btrfs_root *root,
173 u64 start, u64 num_bytes)
Josef Bacik817d52f2009-07-13 21:29:25 -0400174{
Yan Zheng11833d62009-09-11 16:11:19 -0400175 u64 end = start + num_bytes - 1;
176 set_extent_bits(&root->fs_info->freed_extents[0],
177 start, end, EXTENT_UPTODATE, GFP_NOFS);
178 set_extent_bits(&root->fs_info->freed_extents[1],
179 start, end, EXTENT_UPTODATE, GFP_NOFS);
180 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400181}
182
Yan Zheng11833d62009-09-11 16:11:19 -0400183static void free_excluded_extents(struct btrfs_root *root,
184 struct btrfs_block_group_cache *cache)
Josef Bacik817d52f2009-07-13 21:29:25 -0400185{
Yan Zheng11833d62009-09-11 16:11:19 -0400186 u64 start, end;
187
188 start = cache->key.objectid;
189 end = start + cache->key.offset - 1;
190
191 clear_extent_bits(&root->fs_info->freed_extents[0],
192 start, end, EXTENT_UPTODATE, GFP_NOFS);
193 clear_extent_bits(&root->fs_info->freed_extents[1],
194 start, end, EXTENT_UPTODATE, GFP_NOFS);
195}
196
197static int exclude_super_stripes(struct btrfs_root *root,
198 struct btrfs_block_group_cache *cache)
199{
Josef Bacik817d52f2009-07-13 21:29:25 -0400200 u64 bytenr;
201 u64 *logical;
202 int stripe_len;
203 int i, nr, ret;
204
Yan, Zheng06b23312009-11-26 09:31:11 +0000205 if (cache->key.objectid < BTRFS_SUPER_INFO_OFFSET) {
206 stripe_len = BTRFS_SUPER_INFO_OFFSET - cache->key.objectid;
207 cache->bytes_super += stripe_len;
208 ret = add_excluded_extent(root, cache->key.objectid,
209 stripe_len);
210 BUG_ON(ret);
211 }
212
Josef Bacik817d52f2009-07-13 21:29:25 -0400213 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
214 bytenr = btrfs_sb_offset(i);
215 ret = btrfs_rmap_block(&root->fs_info->mapping_tree,
216 cache->key.objectid, bytenr,
217 0, &logical, &nr, &stripe_len);
218 BUG_ON(ret);
Yan Zheng11833d62009-09-11 16:11:19 -0400219
Josef Bacik817d52f2009-07-13 21:29:25 -0400220 while (nr--) {
Josef Bacik1b2da372009-09-11 16:11:20 -0400221 cache->bytes_super += stripe_len;
Yan Zheng11833d62009-09-11 16:11:19 -0400222 ret = add_excluded_extent(root, logical[nr],
223 stripe_len);
224 BUG_ON(ret);
Josef Bacik817d52f2009-07-13 21:29:25 -0400225 }
Yan Zheng11833d62009-09-11 16:11:19 -0400226
Josef Bacik817d52f2009-07-13 21:29:25 -0400227 kfree(logical);
228 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400229 return 0;
230}
231
Yan Zheng11833d62009-09-11 16:11:19 -0400232static struct btrfs_caching_control *
233get_caching_control(struct btrfs_block_group_cache *cache)
234{
235 struct btrfs_caching_control *ctl;
236
237 spin_lock(&cache->lock);
238 if (cache->cached != BTRFS_CACHE_STARTED) {
239 spin_unlock(&cache->lock);
240 return NULL;
241 }
242
Josef Bacikdde5abe2010-09-16 16:17:03 -0400243 /* We're loading it the fast way, so we don't have a caching_ctl. */
244 if (!cache->caching_ctl) {
245 spin_unlock(&cache->lock);
246 return NULL;
247 }
248
Yan Zheng11833d62009-09-11 16:11:19 -0400249 ctl = cache->caching_ctl;
250 atomic_inc(&ctl->count);
251 spin_unlock(&cache->lock);
252 return ctl;
253}
254
255static void put_caching_control(struct btrfs_caching_control *ctl)
256{
257 if (atomic_dec_and_test(&ctl->count))
258 kfree(ctl);
259}
260
Josef Bacik0f9dd462008-09-23 13:14:11 -0400261/*
262 * this is only called by cache_block_group, since we could have freed extents
263 * we need to check the pinned_extents for any extents that can't be used yet
264 * since their free space will be released as soon as the transaction commits.
265 */
Josef Bacik817d52f2009-07-13 21:29:25 -0400266static u64 add_new_free_space(struct btrfs_block_group_cache *block_group,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400267 struct btrfs_fs_info *info, u64 start, u64 end)
268{
Josef Bacik817d52f2009-07-13 21:29:25 -0400269 u64 extent_start, extent_end, size, total_added = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400270 int ret;
271
272 while (start < end) {
Yan Zheng11833d62009-09-11 16:11:19 -0400273 ret = find_first_extent_bit(info->pinned_extents, start,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400274 &extent_start, &extent_end,
Yan Zheng11833d62009-09-11 16:11:19 -0400275 EXTENT_DIRTY | EXTENT_UPTODATE);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400276 if (ret)
277 break;
278
Yan, Zheng06b23312009-11-26 09:31:11 +0000279 if (extent_start <= start) {
Josef Bacik0f9dd462008-09-23 13:14:11 -0400280 start = extent_end + 1;
281 } else if (extent_start > start && extent_start < end) {
282 size = extent_start - start;
Josef Bacik817d52f2009-07-13 21:29:25 -0400283 total_added += size;
Josef Bacikea6a4782008-11-20 12:16:16 -0500284 ret = btrfs_add_free_space(block_group, start,
285 size);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400286 BUG_ON(ret);
287 start = extent_end + 1;
288 } else {
289 break;
290 }
291 }
292
293 if (start < end) {
294 size = end - start;
Josef Bacik817d52f2009-07-13 21:29:25 -0400295 total_added += size;
Josef Bacikea6a4782008-11-20 12:16:16 -0500296 ret = btrfs_add_free_space(block_group, start, size);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400297 BUG_ON(ret);
298 }
299
Josef Bacik817d52f2009-07-13 21:29:25 -0400300 return total_added;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400301}
302
Josef Bacik817d52f2009-07-13 21:29:25 -0400303static int caching_kthread(void *data)
Chris Masone37c9e62007-05-09 20:13:14 -0400304{
Josef Bacik817d52f2009-07-13 21:29:25 -0400305 struct btrfs_block_group_cache *block_group = data;
306 struct btrfs_fs_info *fs_info = block_group->fs_info;
Yan Zheng11833d62009-09-11 16:11:19 -0400307 struct btrfs_caching_control *caching_ctl = block_group->caching_ctl;
308 struct btrfs_root *extent_root = fs_info->extent_root;
Chris Masone37c9e62007-05-09 20:13:14 -0400309 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400310 struct extent_buffer *leaf;
Yan Zheng11833d62009-09-11 16:11:19 -0400311 struct btrfs_key key;
Josef Bacik817d52f2009-07-13 21:29:25 -0400312 u64 total_found = 0;
Yan Zheng11833d62009-09-11 16:11:19 -0400313 u64 last = 0;
314 u32 nritems;
315 int ret = 0;
Chris Masonf510cfe2007-10-15 16:14:48 -0400316
Chris Masone37c9e62007-05-09 20:13:14 -0400317 path = btrfs_alloc_path();
318 if (!path)
319 return -ENOMEM;
Yan7d7d6062007-09-14 16:15:28 -0400320
Josef Bacik817d52f2009-07-13 21:29:25 -0400321 last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET);
Yan Zheng11833d62009-09-11 16:11:19 -0400322
Chris Mason5cd57b22008-06-25 16:01:30 -0400323 /*
Josef Bacik817d52f2009-07-13 21:29:25 -0400324 * We don't want to deadlock with somebody trying to allocate a new
325 * extent for the extent root while also trying to search the extent
326 * root to add free space. So we skip locking and search the commit
327 * root, since its read-only
Chris Mason5cd57b22008-06-25 16:01:30 -0400328 */
329 path->skip_locking = 1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400330 path->search_commit_root = 1;
331 path->reada = 2;
332
Yan Zhenge4404d62008-12-12 10:03:26 -0500333 key.objectid = last;
Chris Masone37c9e62007-05-09 20:13:14 -0400334 key.offset = 0;
Yan Zheng11833d62009-09-11 16:11:19 -0400335 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason013f1b12009-07-31 14:57:55 -0400336again:
Yan Zheng11833d62009-09-11 16:11:19 -0400337 mutex_lock(&caching_ctl->mutex);
Chris Mason013f1b12009-07-31 14:57:55 -0400338 /* need to make sure the commit_root doesn't disappear */
339 down_read(&fs_info->extent_commit_sem);
340
Yan Zheng11833d62009-09-11 16:11:19 -0400341 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
Chris Masone37c9e62007-05-09 20:13:14 -0400342 if (ret < 0)
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400343 goto err;
Yan Zhenga512bbf2008-12-08 16:46:26 -0500344
Yan Zheng11833d62009-09-11 16:11:19 -0400345 leaf = path->nodes[0];
346 nritems = btrfs_header_nritems(leaf);
347
Chris Masond3977122009-01-05 21:25:51 -0500348 while (1) {
Josef Bacik817d52f2009-07-13 21:29:25 -0400349 smp_mb();
Yan Zheng11833d62009-09-11 16:11:19 -0400350 if (fs_info->closing > 1) {
Yan Zhengf25784b2009-07-28 08:41:57 -0400351 last = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400352 break;
Yan Zhengf25784b2009-07-28 08:41:57 -0400353 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400354
Yan Zheng11833d62009-09-11 16:11:19 -0400355 if (path->slots[0] < nritems) {
356 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
357 } else {
358 ret = find_next_key(path, 0, &key);
359 if (ret)
Chris Masone37c9e62007-05-09 20:13:14 -0400360 break;
Josef Bacik817d52f2009-07-13 21:29:25 -0400361
Yan Zheng11833d62009-09-11 16:11:19 -0400362 caching_ctl->progress = last;
363 btrfs_release_path(extent_root, path);
364 up_read(&fs_info->extent_commit_sem);
365 mutex_unlock(&caching_ctl->mutex);
366 if (btrfs_transaction_in_commit(fs_info))
Chris Masonf36f3042009-07-30 10:04:48 -0400367 schedule_timeout(1);
Yan Zheng11833d62009-09-11 16:11:19 -0400368 else
369 cond_resched();
370 goto again;
371 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400372
Yan Zheng11833d62009-09-11 16:11:19 -0400373 if (key.objectid < block_group->key.objectid) {
374 path->slots[0]++;
Josef Bacik817d52f2009-07-13 21:29:25 -0400375 continue;
Chris Masone37c9e62007-05-09 20:13:14 -0400376 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400377
Chris Masone37c9e62007-05-09 20:13:14 -0400378 if (key.objectid >= block_group->key.objectid +
Josef Bacik0f9dd462008-09-23 13:14:11 -0400379 block_group->key.offset)
Yan7d7d6062007-09-14 16:15:28 -0400380 break;
Yan7d7d6062007-09-14 16:15:28 -0400381
Yan Zheng11833d62009-09-11 16:11:19 -0400382 if (key.type == BTRFS_EXTENT_ITEM_KEY) {
Josef Bacik817d52f2009-07-13 21:29:25 -0400383 total_found += add_new_free_space(block_group,
384 fs_info, last,
385 key.objectid);
Yan7d7d6062007-09-14 16:15:28 -0400386 last = key.objectid + key.offset;
Josef Bacik817d52f2009-07-13 21:29:25 -0400387
Yan Zheng11833d62009-09-11 16:11:19 -0400388 if (total_found > (1024 * 1024 * 2)) {
389 total_found = 0;
390 wake_up(&caching_ctl->wait);
391 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400392 }
Chris Masone37c9e62007-05-09 20:13:14 -0400393 path->slots[0]++;
394 }
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400395 ret = 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400396
397 total_found += add_new_free_space(block_group, fs_info, last,
398 block_group->key.objectid +
399 block_group->key.offset);
Yan Zheng11833d62009-09-11 16:11:19 -0400400 caching_ctl->progress = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400401
402 spin_lock(&block_group->lock);
Yan Zheng11833d62009-09-11 16:11:19 -0400403 block_group->caching_ctl = NULL;
Josef Bacik817d52f2009-07-13 21:29:25 -0400404 block_group->cached = BTRFS_CACHE_FINISHED;
405 spin_unlock(&block_group->lock);
406
Chris Mason54aa1f42007-06-22 14:16:25 -0400407err:
Chris Masone37c9e62007-05-09 20:13:14 -0400408 btrfs_free_path(path);
Yan Zheng276e6802009-07-30 09:40:40 -0400409 up_read(&fs_info->extent_commit_sem);
Josef Bacik817d52f2009-07-13 21:29:25 -0400410
Yan Zheng11833d62009-09-11 16:11:19 -0400411 free_excluded_extents(extent_root, block_group);
412
413 mutex_unlock(&caching_ctl->mutex);
414 wake_up(&caching_ctl->wait);
415
416 put_caching_control(caching_ctl);
417 atomic_dec(&block_group->space_info->caching_threads);
Josef Bacik11dfe352009-11-13 20:12:59 +0000418 btrfs_put_block_group(block_group);
419
Josef Bacik817d52f2009-07-13 21:29:25 -0400420 return 0;
421}
422
Josef Bacik9d66e232010-08-25 16:54:15 -0400423static int cache_block_group(struct btrfs_block_group_cache *cache,
424 struct btrfs_trans_handle *trans,
Josef Bacikb8399de2010-12-08 09:15:11 -0500425 struct btrfs_root *root,
Josef Bacik9d66e232010-08-25 16:54:15 -0400426 int load_cache_only)
Josef Bacik817d52f2009-07-13 21:29:25 -0400427{
Yan Zheng11833d62009-09-11 16:11:19 -0400428 struct btrfs_fs_info *fs_info = cache->fs_info;
429 struct btrfs_caching_control *caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -0400430 struct task_struct *tsk;
431 int ret = 0;
432
Yan Zheng11833d62009-09-11 16:11:19 -0400433 smp_mb();
434 if (cache->cached != BTRFS_CACHE_NO)
435 return 0;
436
Josef Bacik9d66e232010-08-25 16:54:15 -0400437 /*
438 * We can't do the read from on-disk cache during a commit since we need
Josef Bacikb8399de2010-12-08 09:15:11 -0500439 * to have the normal tree locking. Also if we are currently trying to
440 * allocate blocks for the tree root we can't do the fast caching since
441 * we likely hold important locks.
Josef Bacik9d66e232010-08-25 16:54:15 -0400442 */
Josef Bacikb8399de2010-12-08 09:15:11 -0500443 if (!trans->transaction->in_commit &&
444 (root && root != root->fs_info->tree_root)) {
Josef Bacik9d66e232010-08-25 16:54:15 -0400445 spin_lock(&cache->lock);
446 if (cache->cached != BTRFS_CACHE_NO) {
447 spin_unlock(&cache->lock);
448 return 0;
449 }
450 cache->cached = BTRFS_CACHE_STARTED;
451 spin_unlock(&cache->lock);
452
453 ret = load_free_space_cache(fs_info, cache);
454
455 spin_lock(&cache->lock);
456 if (ret == 1) {
457 cache->cached = BTRFS_CACHE_FINISHED;
458 cache->last_byte_to_unpin = (u64)-1;
459 } else {
460 cache->cached = BTRFS_CACHE_NO;
461 }
462 spin_unlock(&cache->lock);
Josef Bacik3c148742011-02-02 15:53:47 +0000463 if (ret == 1) {
464 free_excluded_extents(fs_info->extent_root, cache);
Josef Bacik9d66e232010-08-25 16:54:15 -0400465 return 0;
Josef Bacik3c148742011-02-02 15:53:47 +0000466 }
Josef Bacik9d66e232010-08-25 16:54:15 -0400467 }
468
469 if (load_cache_only)
470 return 0;
471
Miao Xiefc0e4a32011-03-24 11:41:21 +0000472 caching_ctl = kzalloc(sizeof(*caching_ctl), GFP_NOFS);
Yan Zheng11833d62009-09-11 16:11:19 -0400473 BUG_ON(!caching_ctl);
474
475 INIT_LIST_HEAD(&caching_ctl->list);
476 mutex_init(&caching_ctl->mutex);
477 init_waitqueue_head(&caching_ctl->wait);
478 caching_ctl->block_group = cache;
479 caching_ctl->progress = cache->key.objectid;
480 /* one for caching kthread, one for caching block group list */
481 atomic_set(&caching_ctl->count, 2);
482
Josef Bacik817d52f2009-07-13 21:29:25 -0400483 spin_lock(&cache->lock);
484 if (cache->cached != BTRFS_CACHE_NO) {
485 spin_unlock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -0400486 kfree(caching_ctl);
487 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400488 }
Yan Zheng11833d62009-09-11 16:11:19 -0400489 cache->caching_ctl = caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -0400490 cache->cached = BTRFS_CACHE_STARTED;
491 spin_unlock(&cache->lock);
492
Yan Zheng11833d62009-09-11 16:11:19 -0400493 down_write(&fs_info->extent_commit_sem);
494 list_add_tail(&caching_ctl->list, &fs_info->caching_block_groups);
495 up_write(&fs_info->extent_commit_sem);
496
497 atomic_inc(&cache->space_info->caching_threads);
Josef Bacik11dfe352009-11-13 20:12:59 +0000498 btrfs_get_block_group(cache);
Yan Zheng11833d62009-09-11 16:11:19 -0400499
Josef Bacik817d52f2009-07-13 21:29:25 -0400500 tsk = kthread_run(caching_kthread, cache, "btrfs-cache-%llu\n",
501 cache->key.objectid);
502 if (IS_ERR(tsk)) {
503 ret = PTR_ERR(tsk);
504 printk(KERN_ERR "error running thread %d\n", ret);
505 BUG();
506 }
507
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400508 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -0400509}
510
Josef Bacik0f9dd462008-09-23 13:14:11 -0400511/*
512 * return the block group that starts at or after bytenr
513 */
Chris Masond3977122009-01-05 21:25:51 -0500514static struct btrfs_block_group_cache *
515btrfs_lookup_first_block_group(struct btrfs_fs_info *info, u64 bytenr)
Chris Mason0ef3e662008-05-24 14:04:53 -0400516{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400517 struct btrfs_block_group_cache *cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400518
Josef Bacik0f9dd462008-09-23 13:14:11 -0400519 cache = block_group_cache_tree_search(info, bytenr, 0);
Chris Mason0ef3e662008-05-24 14:04:53 -0400520
Josef Bacik0f9dd462008-09-23 13:14:11 -0400521 return cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400522}
523
Josef Bacik0f9dd462008-09-23 13:14:11 -0400524/*
Sankar P9f556842009-05-14 13:52:22 -0400525 * return the block group that contains the given bytenr
Josef Bacik0f9dd462008-09-23 13:14:11 -0400526 */
Chris Masond3977122009-01-05 21:25:51 -0500527struct btrfs_block_group_cache *btrfs_lookup_block_group(
528 struct btrfs_fs_info *info,
529 u64 bytenr)
Chris Masonbe744172007-05-06 10:15:01 -0400530{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400531 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -0400532
Josef Bacik0f9dd462008-09-23 13:14:11 -0400533 cache = block_group_cache_tree_search(info, bytenr, 1);
Chris Mason96b51792007-10-15 16:15:19 -0400534
Josef Bacik0f9dd462008-09-23 13:14:11 -0400535 return cache;
Chris Masonbe744172007-05-06 10:15:01 -0400536}
Chris Mason0b86a832008-03-24 15:01:56 -0400537
Josef Bacik0f9dd462008-09-23 13:14:11 -0400538static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
539 u64 flags)
Chris Mason6324fbf2008-03-24 15:01:59 -0400540{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400541 struct list_head *head = &info->space_info;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400542 struct btrfs_space_info *found;
Chris Mason4184ea72009-03-10 12:39:20 -0400543
Yan, Zhengb742bb822010-05-16 10:46:24 -0400544 flags &= BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_SYSTEM |
545 BTRFS_BLOCK_GROUP_METADATA;
546
Chris Mason4184ea72009-03-10 12:39:20 -0400547 rcu_read_lock();
548 list_for_each_entry_rcu(found, head, list) {
Josef Bacik67377732010-09-16 16:19:09 -0400549 if (found->flags & flags) {
Chris Mason4184ea72009-03-10 12:39:20 -0400550 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -0400551 return found;
Chris Mason4184ea72009-03-10 12:39:20 -0400552 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400553 }
Chris Mason4184ea72009-03-10 12:39:20 -0400554 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -0400555 return NULL;
Chris Mason6324fbf2008-03-24 15:01:59 -0400556}
557
Chris Mason4184ea72009-03-10 12:39:20 -0400558/*
559 * after adding space to the filesystem, we need to clear the full flags
560 * on all the space infos.
561 */
562void btrfs_clear_space_info_full(struct btrfs_fs_info *info)
563{
564 struct list_head *head = &info->space_info;
565 struct btrfs_space_info *found;
566
567 rcu_read_lock();
568 list_for_each_entry_rcu(found, head, list)
569 found->full = 0;
570 rcu_read_unlock();
571}
572
Josef Bacik80eb2342008-10-29 14:49:05 -0400573static u64 div_factor(u64 num, int factor)
574{
575 if (factor == 10)
576 return num;
577 num *= factor;
578 do_div(num, 10);
579 return num;
580}
581
Chris Masone5bc2452010-10-26 13:37:56 -0400582static u64 div_factor_fine(u64 num, int factor)
583{
584 if (factor == 100)
585 return num;
586 num *= factor;
587 do_div(num, 100);
588 return num;
589}
590
Yan Zhengd2fb3432008-12-11 16:30:39 -0500591u64 btrfs_find_block_group(struct btrfs_root *root,
592 u64 search_start, u64 search_hint, int owner)
Chris Masoncd1bc462007-04-27 10:08:34 -0400593{
Chris Mason96b51792007-10-15 16:15:19 -0400594 struct btrfs_block_group_cache *cache;
Chris Masoncd1bc462007-04-27 10:08:34 -0400595 u64 used;
Yan Zhengd2fb3432008-12-11 16:30:39 -0500596 u64 last = max(search_hint, search_start);
597 u64 group_start = 0;
Chris Mason31f3c992007-04-30 15:25:45 -0400598 int full_search = 0;
Yan Zhengd2fb3432008-12-11 16:30:39 -0500599 int factor = 9;
Chris Mason0ef3e662008-05-24 14:04:53 -0400600 int wrapped = 0;
Chris Mason31f3c992007-04-30 15:25:45 -0400601again:
Zheng Yane8569812008-09-26 10:05:48 -0400602 while (1) {
603 cache = btrfs_lookup_first_block_group(root->fs_info, last);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400604 if (!cache)
Chris Masoncd1bc462007-04-27 10:08:34 -0400605 break;
Chris Mason96b51792007-10-15 16:15:19 -0400606
Chris Masonc286ac42008-07-22 23:06:41 -0400607 spin_lock(&cache->lock);
Chris Mason96b51792007-10-15 16:15:19 -0400608 last = cache->key.objectid + cache->key.offset;
609 used = btrfs_block_group_used(&cache->item);
610
Yan Zhengd2fb3432008-12-11 16:30:39 -0500611 if ((full_search || !cache->ro) &&
612 block_group_bits(cache, BTRFS_BLOCK_GROUP_METADATA)) {
Zheng Yane8569812008-09-26 10:05:48 -0400613 if (used + cache->pinned + cache->reserved <
Yan Zhengd2fb3432008-12-11 16:30:39 -0500614 div_factor(cache->key.offset, factor)) {
615 group_start = cache->key.objectid;
Chris Masonc286ac42008-07-22 23:06:41 -0400616 spin_unlock(&cache->lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -0400617 btrfs_put_block_group(cache);
Chris Mason8790d502008-04-03 16:29:03 -0400618 goto found;
619 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400620 }
Chris Masonc286ac42008-07-22 23:06:41 -0400621 spin_unlock(&cache->lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -0400622 btrfs_put_block_group(cache);
Chris Masonde428b62007-05-18 13:28:27 -0400623 cond_resched();
Chris Masoncd1bc462007-04-27 10:08:34 -0400624 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400625 if (!wrapped) {
626 last = search_start;
627 wrapped = 1;
628 goto again;
629 }
630 if (!full_search && factor < 10) {
Chris Masonbe744172007-05-06 10:15:01 -0400631 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -0400632 full_search = 1;
Chris Mason0ef3e662008-05-24 14:04:53 -0400633 factor = 10;
Chris Mason31f3c992007-04-30 15:25:45 -0400634 goto again;
635 }
Chris Masonbe744172007-05-06 10:15:01 -0400636found:
Yan Zhengd2fb3432008-12-11 16:30:39 -0500637 return group_start;
Chris Mason925baed2008-06-25 16:01:30 -0400638}
Josef Bacik0f9dd462008-09-23 13:14:11 -0400639
Chris Masone02119d2008-09-05 16:13:11 -0400640/* simple helper to search for an existing extent at a given offset */
Zheng Yan31840ae2008-09-23 13:14:14 -0400641int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len)
Chris Masone02119d2008-09-05 16:13:11 -0400642{
643 int ret;
644 struct btrfs_key key;
Zheng Yan31840ae2008-09-23 13:14:14 -0400645 struct btrfs_path *path;
Chris Masone02119d2008-09-05 16:13:11 -0400646
Zheng Yan31840ae2008-09-23 13:14:14 -0400647 path = btrfs_alloc_path();
648 BUG_ON(!path);
Chris Masone02119d2008-09-05 16:13:11 -0400649 key.objectid = start;
650 key.offset = len;
651 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
652 ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
653 0, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400654 btrfs_free_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -0500655 return ret;
656}
657
Chris Masond8d5f3e2007-12-11 12:42:00 -0500658/*
Yan, Zhenga22285a2010-05-16 10:48:46 -0400659 * helper function to lookup reference count and flags of extent.
660 *
661 * the head node for delayed ref is used to store the sum of all the
662 * reference count modifications queued up in the rbtree. the head
663 * node may also store the extent flags to set. This way you can check
664 * to see what the reference count and extent flags would be if all of
665 * the delayed refs are not processed.
666 */
667int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,
668 struct btrfs_root *root, u64 bytenr,
669 u64 num_bytes, u64 *refs, u64 *flags)
670{
671 struct btrfs_delayed_ref_head *head;
672 struct btrfs_delayed_ref_root *delayed_refs;
673 struct btrfs_path *path;
674 struct btrfs_extent_item *ei;
675 struct extent_buffer *leaf;
676 struct btrfs_key key;
677 u32 item_size;
678 u64 num_refs;
679 u64 extent_flags;
680 int ret;
681
682 path = btrfs_alloc_path();
683 if (!path)
684 return -ENOMEM;
685
686 key.objectid = bytenr;
687 key.type = BTRFS_EXTENT_ITEM_KEY;
688 key.offset = num_bytes;
689 if (!trans) {
690 path->skip_locking = 1;
691 path->search_commit_root = 1;
692 }
693again:
694 ret = btrfs_search_slot(trans, root->fs_info->extent_root,
695 &key, path, 0, 0);
696 if (ret < 0)
697 goto out_free;
698
699 if (ret == 0) {
700 leaf = path->nodes[0];
701 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
702 if (item_size >= sizeof(*ei)) {
703 ei = btrfs_item_ptr(leaf, path->slots[0],
704 struct btrfs_extent_item);
705 num_refs = btrfs_extent_refs(leaf, ei);
706 extent_flags = btrfs_extent_flags(leaf, ei);
707 } else {
708#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
709 struct btrfs_extent_item_v0 *ei0;
710 BUG_ON(item_size != sizeof(*ei0));
711 ei0 = btrfs_item_ptr(leaf, path->slots[0],
712 struct btrfs_extent_item_v0);
713 num_refs = btrfs_extent_refs_v0(leaf, ei0);
714 /* FIXME: this isn't correct for data */
715 extent_flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
716#else
717 BUG();
718#endif
719 }
720 BUG_ON(num_refs == 0);
721 } else {
722 num_refs = 0;
723 extent_flags = 0;
724 ret = 0;
725 }
726
727 if (!trans)
728 goto out;
729
730 delayed_refs = &trans->transaction->delayed_refs;
731 spin_lock(&delayed_refs->lock);
732 head = btrfs_find_delayed_ref_head(trans, bytenr);
733 if (head) {
734 if (!mutex_trylock(&head->mutex)) {
735 atomic_inc(&head->node.refs);
736 spin_unlock(&delayed_refs->lock);
737
738 btrfs_release_path(root->fs_info->extent_root, path);
739
740 mutex_lock(&head->mutex);
741 mutex_unlock(&head->mutex);
742 btrfs_put_delayed_ref(&head->node);
743 goto again;
744 }
745 if (head->extent_op && head->extent_op->update_flags)
746 extent_flags |= head->extent_op->flags_to_set;
747 else
748 BUG_ON(num_refs == 0);
749
750 num_refs += head->node.ref_mod;
751 mutex_unlock(&head->mutex);
752 }
753 spin_unlock(&delayed_refs->lock);
754out:
755 WARN_ON(num_refs == 0);
756 if (refs)
757 *refs = num_refs;
758 if (flags)
759 *flags = extent_flags;
760out_free:
761 btrfs_free_path(path);
762 return ret;
763}
764
765/*
Chris Masond8d5f3e2007-12-11 12:42:00 -0500766 * Back reference rules. Back refs have three main goals:
767 *
768 * 1) differentiate between all holders of references to an extent so that
769 * when a reference is dropped we can make sure it was a valid reference
770 * before freeing the extent.
771 *
772 * 2) Provide enough information to quickly find the holders of an extent
773 * if we notice a given block is corrupted or bad.
774 *
775 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
776 * maintenance. This is actually the same as #2, but with a slightly
777 * different use case.
778 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400779 * There are two kinds of back refs. The implicit back refs is optimized
780 * for pointers in non-shared tree blocks. For a given pointer in a block,
781 * back refs of this kind provide information about the block's owner tree
782 * and the pointer's key. These information allow us to find the block by
783 * b-tree searching. The full back refs is for pointers in tree blocks not
784 * referenced by their owner trees. The location of tree block is recorded
785 * in the back refs. Actually the full back refs is generic, and can be
786 * used in all cases the implicit back refs is used. The major shortcoming
787 * of the full back refs is its overhead. Every time a tree block gets
788 * COWed, we have to update back refs entry for all pointers in it.
789 *
790 * For a newly allocated tree block, we use implicit back refs for
791 * pointers in it. This means most tree related operations only involve
792 * implicit back refs. For a tree block created in old transaction, the
793 * only way to drop a reference to it is COW it. So we can detect the
794 * event that tree block loses its owner tree's reference and do the
795 * back refs conversion.
796 *
797 * When a tree block is COW'd through a tree, there are four cases:
798 *
799 * The reference count of the block is one and the tree is the block's
800 * owner tree. Nothing to do in this case.
801 *
802 * The reference count of the block is one and the tree is not the
803 * block's owner tree. In this case, full back refs is used for pointers
804 * in the block. Remove these full back refs, add implicit back refs for
805 * every pointers in the new block.
806 *
807 * The reference count of the block is greater than one and the tree is
808 * the block's owner tree. In this case, implicit back refs is used for
809 * pointers in the block. Add full back refs for every pointers in the
810 * block, increase lower level extents' reference counts. The original
811 * implicit back refs are entailed to the new block.
812 *
813 * The reference count of the block is greater than one and the tree is
814 * not the block's owner tree. Add implicit back refs for every pointer in
815 * the new block, increase lower level extents' reference count.
816 *
817 * Back Reference Key composing:
818 *
819 * The key objectid corresponds to the first byte in the extent,
820 * The key type is used to differentiate between types of back refs.
821 * There are different meanings of the key offset for different types
822 * of back refs.
823 *
Chris Masond8d5f3e2007-12-11 12:42:00 -0500824 * File extents can be referenced by:
825 *
826 * - multiple snapshots, subvolumes, or different generations in one subvol
Zheng Yan31840ae2008-09-23 13:14:14 -0400827 * - different files inside a single subvolume
Chris Masond8d5f3e2007-12-11 12:42:00 -0500828 * - different offsets inside a file (bookend extents in file.c)
829 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400830 * The extent ref structure for the implicit back refs has fields for:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500831 *
832 * - Objectid of the subvolume root
Chris Masond8d5f3e2007-12-11 12:42:00 -0500833 * - objectid of the file holding the reference
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400834 * - original offset in the file
835 * - how many bookend extents
Zheng Yan31840ae2008-09-23 13:14:14 -0400836 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400837 * The key offset for the implicit back refs is hash of the first
838 * three fields.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500839 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400840 * The extent ref structure for the full back refs has field for:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500841 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400842 * - number of pointers in the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -0500843 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400844 * The key offset for the implicit back refs is the first byte of
845 * the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -0500846 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400847 * When a file extent is allocated, The implicit back refs is used.
848 * the fields are filled in:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500849 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400850 * (root_key.objectid, inode objectid, offset in file, 1)
851 *
852 * When a file extent is removed file truncation, we find the
853 * corresponding implicit back refs and check the following fields:
854 *
855 * (btrfs_header_owner(leaf), inode objectid, offset in file)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500856 *
857 * Btree extents can be referenced by:
858 *
859 * - Different subvolumes
Chris Masond8d5f3e2007-12-11 12:42:00 -0500860 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400861 * Both the implicit back refs and the full back refs for tree blocks
862 * only consist of key. The key offset for the implicit back refs is
863 * objectid of block's owner tree. The key offset for the full back refs
864 * is the first byte of parent block.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500865 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400866 * When implicit back refs is used, information about the lowest key and
867 * level of the tree block are required. These information are stored in
868 * tree block info structure.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500869 */
Zheng Yan31840ae2008-09-23 13:14:14 -0400870
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400871#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
872static int convert_extent_item_v0(struct btrfs_trans_handle *trans,
873 struct btrfs_root *root,
874 struct btrfs_path *path,
875 u64 owner, u32 extra_size)
Chris Mason74493f72007-12-11 09:25:06 -0500876{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400877 struct btrfs_extent_item *item;
878 struct btrfs_extent_item_v0 *ei0;
879 struct btrfs_extent_ref_v0 *ref0;
880 struct btrfs_tree_block_info *bi;
Zheng Yan31840ae2008-09-23 13:14:14 -0400881 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400882 struct btrfs_key key;
883 struct btrfs_key found_key;
884 u32 new_size = sizeof(*item);
885 u64 refs;
Chris Mason74493f72007-12-11 09:25:06 -0500886 int ret;
887
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400888 leaf = path->nodes[0];
889 BUG_ON(btrfs_item_size_nr(leaf, path->slots[0]) != sizeof(*ei0));
Chris Mason74493f72007-12-11 09:25:06 -0500890
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400891 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
892 ei0 = btrfs_item_ptr(leaf, path->slots[0],
893 struct btrfs_extent_item_v0);
894 refs = btrfs_extent_refs_v0(leaf, ei0);
895
896 if (owner == (u64)-1) {
897 while (1) {
898 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
899 ret = btrfs_next_leaf(root, path);
900 if (ret < 0)
901 return ret;
902 BUG_ON(ret > 0);
903 leaf = path->nodes[0];
904 }
905 btrfs_item_key_to_cpu(leaf, &found_key,
906 path->slots[0]);
907 BUG_ON(key.objectid != found_key.objectid);
908 if (found_key.type != BTRFS_EXTENT_REF_V0_KEY) {
909 path->slots[0]++;
910 continue;
911 }
912 ref0 = btrfs_item_ptr(leaf, path->slots[0],
913 struct btrfs_extent_ref_v0);
914 owner = btrfs_ref_objectid_v0(leaf, ref0);
915 break;
916 }
917 }
918 btrfs_release_path(root, path);
919
920 if (owner < BTRFS_FIRST_FREE_OBJECTID)
921 new_size += sizeof(*bi);
922
923 new_size -= sizeof(*ei0);
924 ret = btrfs_search_slot(trans, root, &key, path,
925 new_size + extra_size, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -0400926 if (ret < 0)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400927 return ret;
928 BUG_ON(ret);
929
930 ret = btrfs_extend_item(trans, root, path, new_size);
931 BUG_ON(ret);
932
933 leaf = path->nodes[0];
934 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
935 btrfs_set_extent_refs(leaf, item, refs);
936 /* FIXME: get real generation */
937 btrfs_set_extent_generation(leaf, item, 0);
938 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
939 btrfs_set_extent_flags(leaf, item,
940 BTRFS_EXTENT_FLAG_TREE_BLOCK |
941 BTRFS_BLOCK_FLAG_FULL_BACKREF);
942 bi = (struct btrfs_tree_block_info *)(item + 1);
943 /* FIXME: get first key of the block */
944 memset_extent_buffer(leaf, 0, (unsigned long)bi, sizeof(*bi));
945 btrfs_set_tree_block_level(leaf, bi, (int)owner);
946 } else {
947 btrfs_set_extent_flags(leaf, item, BTRFS_EXTENT_FLAG_DATA);
948 }
949 btrfs_mark_buffer_dirty(leaf);
950 return 0;
951}
952#endif
953
954static u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset)
955{
956 u32 high_crc = ~(u32)0;
957 u32 low_crc = ~(u32)0;
958 __le64 lenum;
959
960 lenum = cpu_to_le64(root_objectid);
David Woodhouse163e7832009-04-19 13:02:41 +0100961 high_crc = crc32c(high_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400962 lenum = cpu_to_le64(owner);
David Woodhouse163e7832009-04-19 13:02:41 +0100963 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400964 lenum = cpu_to_le64(offset);
David Woodhouse163e7832009-04-19 13:02:41 +0100965 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400966
967 return ((u64)high_crc << 31) ^ (u64)low_crc;
968}
969
970static u64 hash_extent_data_ref_item(struct extent_buffer *leaf,
971 struct btrfs_extent_data_ref *ref)
972{
973 return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref),
974 btrfs_extent_data_ref_objectid(leaf, ref),
975 btrfs_extent_data_ref_offset(leaf, ref));
976}
977
978static int match_extent_data_ref(struct extent_buffer *leaf,
979 struct btrfs_extent_data_ref *ref,
980 u64 root_objectid, u64 owner, u64 offset)
981{
982 if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid ||
983 btrfs_extent_data_ref_objectid(leaf, ref) != owner ||
984 btrfs_extent_data_ref_offset(leaf, ref) != offset)
985 return 0;
986 return 1;
987}
988
989static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans,
990 struct btrfs_root *root,
991 struct btrfs_path *path,
992 u64 bytenr, u64 parent,
993 u64 root_objectid,
994 u64 owner, u64 offset)
995{
996 struct btrfs_key key;
997 struct btrfs_extent_data_ref *ref;
998 struct extent_buffer *leaf;
999 u32 nritems;
1000 int ret;
1001 int recow;
1002 int err = -ENOENT;
1003
1004 key.objectid = bytenr;
1005 if (parent) {
1006 key.type = BTRFS_SHARED_DATA_REF_KEY;
1007 key.offset = parent;
1008 } else {
1009 key.type = BTRFS_EXTENT_DATA_REF_KEY;
1010 key.offset = hash_extent_data_ref(root_objectid,
1011 owner, offset);
1012 }
1013again:
1014 recow = 0;
1015 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1016 if (ret < 0) {
1017 err = ret;
1018 goto fail;
1019 }
1020
1021 if (parent) {
1022 if (!ret)
1023 return 0;
1024#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1025 key.type = BTRFS_EXTENT_REF_V0_KEY;
1026 btrfs_release_path(root, path);
1027 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1028 if (ret < 0) {
1029 err = ret;
1030 goto fail;
1031 }
1032 if (!ret)
1033 return 0;
1034#endif
1035 goto fail;
Zheng Yan31840ae2008-09-23 13:14:14 -04001036 }
1037
1038 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001039 nritems = btrfs_header_nritems(leaf);
1040 while (1) {
1041 if (path->slots[0] >= nritems) {
1042 ret = btrfs_next_leaf(root, path);
1043 if (ret < 0)
1044 err = ret;
1045 if (ret)
1046 goto fail;
1047
1048 leaf = path->nodes[0];
1049 nritems = btrfs_header_nritems(leaf);
1050 recow = 1;
1051 }
1052
1053 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1054 if (key.objectid != bytenr ||
1055 key.type != BTRFS_EXTENT_DATA_REF_KEY)
1056 goto fail;
1057
1058 ref = btrfs_item_ptr(leaf, path->slots[0],
1059 struct btrfs_extent_data_ref);
1060
1061 if (match_extent_data_ref(leaf, ref, root_objectid,
1062 owner, offset)) {
1063 if (recow) {
1064 btrfs_release_path(root, path);
1065 goto again;
1066 }
1067 err = 0;
1068 break;
1069 }
1070 path->slots[0]++;
Zheng Yan31840ae2008-09-23 13:14:14 -04001071 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001072fail:
1073 return err;
Zheng Yan31840ae2008-09-23 13:14:14 -04001074}
1075
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001076static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans,
1077 struct btrfs_root *root,
1078 struct btrfs_path *path,
1079 u64 bytenr, u64 parent,
1080 u64 root_objectid, u64 owner,
1081 u64 offset, int refs_to_add)
Zheng Yan31840ae2008-09-23 13:14:14 -04001082{
1083 struct btrfs_key key;
1084 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001085 u32 size;
Zheng Yan31840ae2008-09-23 13:14:14 -04001086 u32 num_refs;
1087 int ret;
1088
1089 key.objectid = bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001090 if (parent) {
1091 key.type = BTRFS_SHARED_DATA_REF_KEY;
1092 key.offset = parent;
1093 size = sizeof(struct btrfs_shared_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -04001094 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001095 key.type = BTRFS_EXTENT_DATA_REF_KEY;
1096 key.offset = hash_extent_data_ref(root_objectid,
1097 owner, offset);
1098 size = sizeof(struct btrfs_extent_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -04001099 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001100
1101 ret = btrfs_insert_empty_item(trans, root, path, &key, size);
1102 if (ret && ret != -EEXIST)
1103 goto fail;
1104
1105 leaf = path->nodes[0];
1106 if (parent) {
1107 struct btrfs_shared_data_ref *ref;
1108 ref = btrfs_item_ptr(leaf, path->slots[0],
1109 struct btrfs_shared_data_ref);
1110 if (ret == 0) {
1111 btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add);
1112 } else {
1113 num_refs = btrfs_shared_data_ref_count(leaf, ref);
1114 num_refs += refs_to_add;
1115 btrfs_set_shared_data_ref_count(leaf, ref, num_refs);
1116 }
1117 } else {
1118 struct btrfs_extent_data_ref *ref;
1119 while (ret == -EEXIST) {
1120 ref = btrfs_item_ptr(leaf, path->slots[0],
1121 struct btrfs_extent_data_ref);
1122 if (match_extent_data_ref(leaf, ref, root_objectid,
1123 owner, offset))
1124 break;
1125 btrfs_release_path(root, path);
1126 key.offset++;
1127 ret = btrfs_insert_empty_item(trans, root, path, &key,
1128 size);
1129 if (ret && ret != -EEXIST)
1130 goto fail;
1131
1132 leaf = path->nodes[0];
1133 }
1134 ref = btrfs_item_ptr(leaf, path->slots[0],
1135 struct btrfs_extent_data_ref);
1136 if (ret == 0) {
1137 btrfs_set_extent_data_ref_root(leaf, ref,
1138 root_objectid);
1139 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
1140 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
1141 btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add);
1142 } else {
1143 num_refs = btrfs_extent_data_ref_count(leaf, ref);
1144 num_refs += refs_to_add;
1145 btrfs_set_extent_data_ref_count(leaf, ref, num_refs);
1146 }
1147 }
1148 btrfs_mark_buffer_dirty(leaf);
1149 ret = 0;
1150fail:
Chris Mason7bb86312007-12-11 09:25:06 -05001151 btrfs_release_path(root, path);
1152 return ret;
Chris Mason74493f72007-12-11 09:25:06 -05001153}
1154
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001155static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans,
1156 struct btrfs_root *root,
1157 struct btrfs_path *path,
1158 int refs_to_drop)
Zheng Yan31840ae2008-09-23 13:14:14 -04001159{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001160 struct btrfs_key key;
1161 struct btrfs_extent_data_ref *ref1 = NULL;
1162 struct btrfs_shared_data_ref *ref2 = NULL;
Zheng Yan31840ae2008-09-23 13:14:14 -04001163 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001164 u32 num_refs = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04001165 int ret = 0;
1166
1167 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001168 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1169
1170 if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1171 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1172 struct btrfs_extent_data_ref);
1173 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1174 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1175 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1176 struct btrfs_shared_data_ref);
1177 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1178#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1179 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1180 struct btrfs_extent_ref_v0 *ref0;
1181 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1182 struct btrfs_extent_ref_v0);
1183 num_refs = btrfs_ref_count_v0(leaf, ref0);
1184#endif
1185 } else {
1186 BUG();
1187 }
1188
Chris Mason56bec292009-03-13 10:10:06 -04001189 BUG_ON(num_refs < refs_to_drop);
1190 num_refs -= refs_to_drop;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001191
Zheng Yan31840ae2008-09-23 13:14:14 -04001192 if (num_refs == 0) {
1193 ret = btrfs_del_item(trans, root, path);
1194 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001195 if (key.type == BTRFS_EXTENT_DATA_REF_KEY)
1196 btrfs_set_extent_data_ref_count(leaf, ref1, num_refs);
1197 else if (key.type == BTRFS_SHARED_DATA_REF_KEY)
1198 btrfs_set_shared_data_ref_count(leaf, ref2, num_refs);
1199#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1200 else {
1201 struct btrfs_extent_ref_v0 *ref0;
1202 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1203 struct btrfs_extent_ref_v0);
1204 btrfs_set_ref_count_v0(leaf, ref0, num_refs);
1205 }
1206#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04001207 btrfs_mark_buffer_dirty(leaf);
1208 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001209 return ret;
1210}
1211
1212static noinline u32 extent_data_ref_count(struct btrfs_root *root,
1213 struct btrfs_path *path,
1214 struct btrfs_extent_inline_ref *iref)
1215{
1216 struct btrfs_key key;
1217 struct extent_buffer *leaf;
1218 struct btrfs_extent_data_ref *ref1;
1219 struct btrfs_shared_data_ref *ref2;
1220 u32 num_refs = 0;
1221
1222 leaf = path->nodes[0];
1223 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1224 if (iref) {
1225 if (btrfs_extent_inline_ref_type(leaf, iref) ==
1226 BTRFS_EXTENT_DATA_REF_KEY) {
1227 ref1 = (struct btrfs_extent_data_ref *)(&iref->offset);
1228 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1229 } else {
1230 ref2 = (struct btrfs_shared_data_ref *)(iref + 1);
1231 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1232 }
1233 } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1234 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1235 struct btrfs_extent_data_ref);
1236 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1237 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1238 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1239 struct btrfs_shared_data_ref);
1240 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1241#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1242 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1243 struct btrfs_extent_ref_v0 *ref0;
1244 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1245 struct btrfs_extent_ref_v0);
1246 num_refs = btrfs_ref_count_v0(leaf, ref0);
1247#endif
1248 } else {
1249 WARN_ON(1);
1250 }
1251 return num_refs;
1252}
1253
1254static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans,
1255 struct btrfs_root *root,
1256 struct btrfs_path *path,
1257 u64 bytenr, u64 parent,
1258 u64 root_objectid)
1259{
1260 struct btrfs_key key;
1261 int ret;
1262
1263 key.objectid = bytenr;
1264 if (parent) {
1265 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1266 key.offset = parent;
1267 } else {
1268 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1269 key.offset = root_objectid;
1270 }
1271
1272 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1273 if (ret > 0)
1274 ret = -ENOENT;
1275#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1276 if (ret == -ENOENT && parent) {
1277 btrfs_release_path(root, path);
1278 key.type = BTRFS_EXTENT_REF_V0_KEY;
1279 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1280 if (ret > 0)
1281 ret = -ENOENT;
1282 }
1283#endif
1284 return ret;
1285}
1286
1287static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans,
1288 struct btrfs_root *root,
1289 struct btrfs_path *path,
1290 u64 bytenr, u64 parent,
1291 u64 root_objectid)
1292{
1293 struct btrfs_key key;
1294 int ret;
1295
1296 key.objectid = bytenr;
1297 if (parent) {
1298 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1299 key.offset = parent;
1300 } else {
1301 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1302 key.offset = root_objectid;
1303 }
1304
1305 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04001306 btrfs_release_path(root, path);
1307 return ret;
1308}
1309
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001310static inline int extent_ref_type(u64 parent, u64 owner)
1311{
1312 int type;
1313 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1314 if (parent > 0)
1315 type = BTRFS_SHARED_BLOCK_REF_KEY;
1316 else
1317 type = BTRFS_TREE_BLOCK_REF_KEY;
1318 } else {
1319 if (parent > 0)
1320 type = BTRFS_SHARED_DATA_REF_KEY;
1321 else
1322 type = BTRFS_EXTENT_DATA_REF_KEY;
1323 }
1324 return type;
1325}
1326
Yan Zheng2c47e6052009-06-27 21:07:35 -04001327static int find_next_key(struct btrfs_path *path, int level,
1328 struct btrfs_key *key)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001329
1330{
Yan Zheng2c47e6052009-06-27 21:07:35 -04001331 for (; level < BTRFS_MAX_LEVEL; level++) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001332 if (!path->nodes[level])
1333 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001334 if (path->slots[level] + 1 >=
1335 btrfs_header_nritems(path->nodes[level]))
1336 continue;
1337 if (level == 0)
1338 btrfs_item_key_to_cpu(path->nodes[level], key,
1339 path->slots[level] + 1);
1340 else
1341 btrfs_node_key_to_cpu(path->nodes[level], key,
1342 path->slots[level] + 1);
1343 return 0;
1344 }
1345 return 1;
1346}
1347
1348/*
1349 * look for inline back ref. if back ref is found, *ref_ret is set
1350 * to the address of inline back ref, and 0 is returned.
1351 *
1352 * if back ref isn't found, *ref_ret is set to the address where it
1353 * should be inserted, and -ENOENT is returned.
1354 *
1355 * if insert is true and there are too many inline back refs, the path
1356 * points to the extent item, and -EAGAIN is returned.
1357 *
1358 * NOTE: inline back refs are ordered in the same way that back ref
1359 * items in the tree are ordered.
1360 */
1361static noinline_for_stack
1362int lookup_inline_extent_backref(struct btrfs_trans_handle *trans,
1363 struct btrfs_root *root,
1364 struct btrfs_path *path,
1365 struct btrfs_extent_inline_ref **ref_ret,
1366 u64 bytenr, u64 num_bytes,
1367 u64 parent, u64 root_objectid,
1368 u64 owner, u64 offset, int insert)
1369{
1370 struct btrfs_key key;
1371 struct extent_buffer *leaf;
1372 struct btrfs_extent_item *ei;
1373 struct btrfs_extent_inline_ref *iref;
1374 u64 flags;
1375 u64 item_size;
1376 unsigned long ptr;
1377 unsigned long end;
1378 int extra_size;
1379 int type;
1380 int want;
1381 int ret;
1382 int err = 0;
1383
1384 key.objectid = bytenr;
1385 key.type = BTRFS_EXTENT_ITEM_KEY;
1386 key.offset = num_bytes;
1387
1388 want = extent_ref_type(parent, owner);
1389 if (insert) {
1390 extra_size = btrfs_extent_inline_ref_size(want);
Yan Zheng85d41982009-06-11 08:51:10 -04001391 path->keep_locks = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001392 } else
1393 extra_size = -1;
1394 ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1);
1395 if (ret < 0) {
1396 err = ret;
1397 goto out;
1398 }
1399 BUG_ON(ret);
1400
1401 leaf = path->nodes[0];
1402 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1403#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1404 if (item_size < sizeof(*ei)) {
1405 if (!insert) {
1406 err = -ENOENT;
1407 goto out;
1408 }
1409 ret = convert_extent_item_v0(trans, root, path, owner,
1410 extra_size);
1411 if (ret < 0) {
1412 err = ret;
1413 goto out;
1414 }
1415 leaf = path->nodes[0];
1416 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1417 }
1418#endif
1419 BUG_ON(item_size < sizeof(*ei));
1420
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001421 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1422 flags = btrfs_extent_flags(leaf, ei);
1423
1424 ptr = (unsigned long)(ei + 1);
1425 end = (unsigned long)ei + item_size;
1426
1427 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
1428 ptr += sizeof(struct btrfs_tree_block_info);
1429 BUG_ON(ptr > end);
1430 } else {
1431 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_DATA));
1432 }
1433
1434 err = -ENOENT;
1435 while (1) {
1436 if (ptr >= end) {
1437 WARN_ON(ptr > end);
1438 break;
1439 }
1440 iref = (struct btrfs_extent_inline_ref *)ptr;
1441 type = btrfs_extent_inline_ref_type(leaf, iref);
1442 if (want < type)
1443 break;
1444 if (want > type) {
1445 ptr += btrfs_extent_inline_ref_size(type);
1446 continue;
1447 }
1448
1449 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1450 struct btrfs_extent_data_ref *dref;
1451 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1452 if (match_extent_data_ref(leaf, dref, root_objectid,
1453 owner, offset)) {
1454 err = 0;
1455 break;
1456 }
1457 if (hash_extent_data_ref_item(leaf, dref) <
1458 hash_extent_data_ref(root_objectid, owner, offset))
1459 break;
1460 } else {
1461 u64 ref_offset;
1462 ref_offset = btrfs_extent_inline_ref_offset(leaf, iref);
1463 if (parent > 0) {
1464 if (parent == ref_offset) {
1465 err = 0;
1466 break;
1467 }
1468 if (ref_offset < parent)
1469 break;
1470 } else {
1471 if (root_objectid == ref_offset) {
1472 err = 0;
1473 break;
1474 }
1475 if (ref_offset < root_objectid)
1476 break;
1477 }
1478 }
1479 ptr += btrfs_extent_inline_ref_size(type);
1480 }
1481 if (err == -ENOENT && insert) {
1482 if (item_size + extra_size >=
1483 BTRFS_MAX_EXTENT_ITEM_SIZE(root)) {
1484 err = -EAGAIN;
1485 goto out;
1486 }
1487 /*
1488 * To add new inline back ref, we have to make sure
1489 * there is no corresponding back ref item.
1490 * For simplicity, we just do not add new inline back
1491 * ref if there is any kind of item for this block
1492 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04001493 if (find_next_key(path, 0, &key) == 0 &&
1494 key.objectid == bytenr &&
Yan Zheng85d41982009-06-11 08:51:10 -04001495 key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001496 err = -EAGAIN;
1497 goto out;
1498 }
1499 }
1500 *ref_ret = (struct btrfs_extent_inline_ref *)ptr;
1501out:
Yan Zheng85d41982009-06-11 08:51:10 -04001502 if (insert) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001503 path->keep_locks = 0;
1504 btrfs_unlock_up_safe(path, 1);
1505 }
1506 return err;
1507}
1508
1509/*
1510 * helper to add new inline back ref
1511 */
1512static noinline_for_stack
1513int setup_inline_extent_backref(struct btrfs_trans_handle *trans,
1514 struct btrfs_root *root,
1515 struct btrfs_path *path,
1516 struct btrfs_extent_inline_ref *iref,
1517 u64 parent, u64 root_objectid,
1518 u64 owner, u64 offset, int refs_to_add,
1519 struct btrfs_delayed_extent_op *extent_op)
1520{
1521 struct extent_buffer *leaf;
1522 struct btrfs_extent_item *ei;
1523 unsigned long ptr;
1524 unsigned long end;
1525 unsigned long item_offset;
1526 u64 refs;
1527 int size;
1528 int type;
1529 int ret;
1530
1531 leaf = path->nodes[0];
1532 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1533 item_offset = (unsigned long)iref - (unsigned long)ei;
1534
1535 type = extent_ref_type(parent, owner);
1536 size = btrfs_extent_inline_ref_size(type);
1537
1538 ret = btrfs_extend_item(trans, root, path, size);
1539 BUG_ON(ret);
1540
1541 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1542 refs = btrfs_extent_refs(leaf, ei);
1543 refs += refs_to_add;
1544 btrfs_set_extent_refs(leaf, ei, refs);
1545 if (extent_op)
1546 __run_delayed_extent_op(extent_op, leaf, ei);
1547
1548 ptr = (unsigned long)ei + item_offset;
1549 end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]);
1550 if (ptr < end - size)
1551 memmove_extent_buffer(leaf, ptr + size, ptr,
1552 end - size - ptr);
1553
1554 iref = (struct btrfs_extent_inline_ref *)ptr;
1555 btrfs_set_extent_inline_ref_type(leaf, iref, type);
1556 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1557 struct btrfs_extent_data_ref *dref;
1558 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1559 btrfs_set_extent_data_ref_root(leaf, dref, root_objectid);
1560 btrfs_set_extent_data_ref_objectid(leaf, dref, owner);
1561 btrfs_set_extent_data_ref_offset(leaf, dref, offset);
1562 btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add);
1563 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1564 struct btrfs_shared_data_ref *sref;
1565 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1566 btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add);
1567 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1568 } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
1569 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1570 } else {
1571 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
1572 }
1573 btrfs_mark_buffer_dirty(leaf);
1574 return 0;
1575}
1576
1577static int lookup_extent_backref(struct btrfs_trans_handle *trans,
1578 struct btrfs_root *root,
1579 struct btrfs_path *path,
1580 struct btrfs_extent_inline_ref **ref_ret,
1581 u64 bytenr, u64 num_bytes, u64 parent,
1582 u64 root_objectid, u64 owner, u64 offset)
1583{
1584 int ret;
1585
1586 ret = lookup_inline_extent_backref(trans, root, path, ref_ret,
1587 bytenr, num_bytes, parent,
1588 root_objectid, owner, offset, 0);
1589 if (ret != -ENOENT)
1590 return ret;
1591
1592 btrfs_release_path(root, path);
1593 *ref_ret = NULL;
1594
1595 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1596 ret = lookup_tree_block_ref(trans, root, path, bytenr, parent,
1597 root_objectid);
1598 } else {
1599 ret = lookup_extent_data_ref(trans, root, path, bytenr, parent,
1600 root_objectid, owner, offset);
1601 }
1602 return ret;
1603}
1604
1605/*
1606 * helper to update/remove inline back ref
1607 */
1608static noinline_for_stack
1609int update_inline_extent_backref(struct btrfs_trans_handle *trans,
1610 struct btrfs_root *root,
1611 struct btrfs_path *path,
1612 struct btrfs_extent_inline_ref *iref,
1613 int refs_to_mod,
1614 struct btrfs_delayed_extent_op *extent_op)
1615{
1616 struct extent_buffer *leaf;
1617 struct btrfs_extent_item *ei;
1618 struct btrfs_extent_data_ref *dref = NULL;
1619 struct btrfs_shared_data_ref *sref = NULL;
1620 unsigned long ptr;
1621 unsigned long end;
1622 u32 item_size;
1623 int size;
1624 int type;
1625 int ret;
1626 u64 refs;
1627
1628 leaf = path->nodes[0];
1629 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1630 refs = btrfs_extent_refs(leaf, ei);
1631 WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0);
1632 refs += refs_to_mod;
1633 btrfs_set_extent_refs(leaf, ei, refs);
1634 if (extent_op)
1635 __run_delayed_extent_op(extent_op, leaf, ei);
1636
1637 type = btrfs_extent_inline_ref_type(leaf, iref);
1638
1639 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1640 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1641 refs = btrfs_extent_data_ref_count(leaf, dref);
1642 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1643 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1644 refs = btrfs_shared_data_ref_count(leaf, sref);
1645 } else {
1646 refs = 1;
1647 BUG_ON(refs_to_mod != -1);
1648 }
1649
1650 BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod);
1651 refs += refs_to_mod;
1652
1653 if (refs > 0) {
1654 if (type == BTRFS_EXTENT_DATA_REF_KEY)
1655 btrfs_set_extent_data_ref_count(leaf, dref, refs);
1656 else
1657 btrfs_set_shared_data_ref_count(leaf, sref, refs);
1658 } else {
1659 size = btrfs_extent_inline_ref_size(type);
1660 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1661 ptr = (unsigned long)iref;
1662 end = (unsigned long)ei + item_size;
1663 if (ptr + size < end)
1664 memmove_extent_buffer(leaf, ptr, ptr + size,
1665 end - ptr - size);
1666 item_size -= size;
1667 ret = btrfs_truncate_item(trans, root, path, item_size, 1);
1668 BUG_ON(ret);
1669 }
1670 btrfs_mark_buffer_dirty(leaf);
1671 return 0;
1672}
1673
1674static noinline_for_stack
1675int insert_inline_extent_backref(struct btrfs_trans_handle *trans,
1676 struct btrfs_root *root,
1677 struct btrfs_path *path,
1678 u64 bytenr, u64 num_bytes, u64 parent,
1679 u64 root_objectid, u64 owner,
1680 u64 offset, int refs_to_add,
1681 struct btrfs_delayed_extent_op *extent_op)
1682{
1683 struct btrfs_extent_inline_ref *iref;
1684 int ret;
1685
1686 ret = lookup_inline_extent_backref(trans, root, path, &iref,
1687 bytenr, num_bytes, parent,
1688 root_objectid, owner, offset, 1);
1689 if (ret == 0) {
1690 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID);
1691 ret = update_inline_extent_backref(trans, root, path, iref,
1692 refs_to_add, extent_op);
1693 } else if (ret == -ENOENT) {
1694 ret = setup_inline_extent_backref(trans, root, path, iref,
1695 parent, root_objectid,
1696 owner, offset, refs_to_add,
1697 extent_op);
1698 }
1699 return ret;
1700}
1701
1702static int insert_extent_backref(struct btrfs_trans_handle *trans,
1703 struct btrfs_root *root,
1704 struct btrfs_path *path,
1705 u64 bytenr, u64 parent, u64 root_objectid,
1706 u64 owner, u64 offset, int refs_to_add)
1707{
1708 int ret;
1709 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1710 BUG_ON(refs_to_add != 1);
1711 ret = insert_tree_block_ref(trans, root, path, bytenr,
1712 parent, root_objectid);
1713 } else {
1714 ret = insert_extent_data_ref(trans, root, path, bytenr,
1715 parent, root_objectid,
1716 owner, offset, refs_to_add);
1717 }
1718 return ret;
1719}
1720
1721static int remove_extent_backref(struct btrfs_trans_handle *trans,
1722 struct btrfs_root *root,
1723 struct btrfs_path *path,
1724 struct btrfs_extent_inline_ref *iref,
1725 int refs_to_drop, int is_data)
1726{
1727 int ret;
1728
1729 BUG_ON(!is_data && refs_to_drop != 1);
1730 if (iref) {
1731 ret = update_inline_extent_backref(trans, root, path, iref,
1732 -refs_to_drop, NULL);
1733 } else if (is_data) {
1734 ret = remove_extent_data_ref(trans, root, path, refs_to_drop);
1735 } else {
1736 ret = btrfs_del_item(trans, root, path);
1737 }
1738 return ret;
1739}
1740
Chris Mason15916de2008-11-19 21:17:22 -05001741static void btrfs_issue_discard(struct block_device *bdev,
1742 u64 start, u64 len)
1743{
Miao Xiefc0e4a32011-03-24 11:41:21 +00001744 blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_NOFS, 0);
Chris Mason15916de2008-11-19 21:17:22 -05001745}
Chris Mason15916de2008-11-19 21:17:22 -05001746
Liu Hui1f3c79a2009-01-05 15:57:51 -05001747static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr,
1748 u64 num_bytes)
1749{
Liu Hui1f3c79a2009-01-05 15:57:51 -05001750 int ret;
1751 u64 map_length = num_bytes;
1752 struct btrfs_multi_bio *multi = NULL;
1753
Christoph Hellwige244a0a2009-10-14 09:24:59 -04001754 if (!btrfs_test_opt(root, DISCARD))
1755 return 0;
1756
Liu Hui1f3c79a2009-01-05 15:57:51 -05001757 /* Tell the block device(s) that the sectors can be discarded */
1758 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
1759 bytenr, &map_length, &multi, 0);
1760 if (!ret) {
1761 struct btrfs_bio_stripe *stripe = multi->stripes;
1762 int i;
1763
1764 if (map_length > num_bytes)
1765 map_length = num_bytes;
1766
1767 for (i = 0; i < multi->num_stripes; i++, stripe++) {
1768 btrfs_issue_discard(stripe->dev->bdev,
1769 stripe->physical,
1770 map_length);
1771 }
1772 kfree(multi);
1773 }
1774
1775 return ret;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001776}
1777
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001778int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1779 struct btrfs_root *root,
1780 u64 bytenr, u64 num_bytes, u64 parent,
1781 u64 root_objectid, u64 owner, u64 offset)
Zheng Yan31840ae2008-09-23 13:14:14 -04001782{
1783 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001784 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID &&
1785 root_objectid == BTRFS_TREE_LOG_OBJECTID);
Zheng Yan31840ae2008-09-23 13:14:14 -04001786
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001787 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1788 ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes,
1789 parent, root_objectid, (int)owner,
1790 BTRFS_ADD_DELAYED_REF, NULL);
1791 } else {
1792 ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes,
1793 parent, root_objectid, owner, offset,
1794 BTRFS_ADD_DELAYED_REF, NULL);
1795 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001796 return ret;
1797}
1798
Chris Mason925baed2008-06-25 16:01:30 -04001799static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001800 struct btrfs_root *root,
1801 u64 bytenr, u64 num_bytes,
1802 u64 parent, u64 root_objectid,
1803 u64 owner, u64 offset, int refs_to_add,
1804 struct btrfs_delayed_extent_op *extent_op)
Chris Mason56bec292009-03-13 10:10:06 -04001805{
Chris Mason5caf2a02007-04-02 11:20:42 -04001806 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001807 struct extent_buffer *leaf;
Chris Mason234b63a2007-03-13 10:46:10 -04001808 struct btrfs_extent_item *item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001809 u64 refs;
1810 int ret;
1811 int err = 0;
Chris Mason037e6392007-03-07 11:50:24 -05001812
Chris Mason5caf2a02007-04-02 11:20:42 -04001813 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04001814 if (!path)
1815 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -04001816
Chris Mason3c12ac72008-04-21 12:01:38 -04001817 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04001818 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001819 /* this will setup the path even if it fails to insert the back ref */
1820 ret = insert_inline_extent_backref(trans, root->fs_info->extent_root,
1821 path, bytenr, num_bytes, parent,
1822 root_objectid, owner, offset,
1823 refs_to_add, extent_op);
1824 if (ret == 0)
1825 goto out;
Zheng Yan31840ae2008-09-23 13:14:14 -04001826
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001827 if (ret != -EAGAIN) {
1828 err = ret;
1829 goto out;
Chris Masonb9473432009-03-13 11:00:37 -04001830 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001831
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001832 leaf = path->nodes[0];
1833 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1834 refs = btrfs_extent_refs(leaf, item);
1835 btrfs_set_extent_refs(leaf, item, refs + refs_to_add);
1836 if (extent_op)
1837 __run_delayed_extent_op(extent_op, leaf, item);
Zheng Yan31840ae2008-09-23 13:14:14 -04001838
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001839 btrfs_mark_buffer_dirty(leaf);
Chris Mason5caf2a02007-04-02 11:20:42 -04001840 btrfs_release_path(root->fs_info->extent_root, path);
Chris Mason7bb86312007-12-11 09:25:06 -05001841
Chris Mason3c12ac72008-04-21 12:01:38 -04001842 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04001843 path->leave_spinning = 1;
1844
Chris Mason56bec292009-03-13 10:10:06 -04001845 /* now insert the actual backref */
Zheng Yan31840ae2008-09-23 13:14:14 -04001846 ret = insert_extent_backref(trans, root->fs_info->extent_root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001847 path, bytenr, parent, root_objectid,
1848 owner, offset, refs_to_add);
Chris Mason7bb86312007-12-11 09:25:06 -05001849 BUG_ON(ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001850out:
Chris Mason74493f72007-12-11 09:25:06 -05001851 btrfs_free_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001852 return err;
Chris Mason02217ed2007-03-02 16:08:05 -05001853}
1854
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001855static int run_delayed_data_ref(struct btrfs_trans_handle *trans,
1856 struct btrfs_root *root,
1857 struct btrfs_delayed_ref_node *node,
1858 struct btrfs_delayed_extent_op *extent_op,
1859 int insert_reserved)
Chris Masone9d0b132007-08-10 14:06:19 -04001860{
Chris Mason56bec292009-03-13 10:10:06 -04001861 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001862 struct btrfs_delayed_data_ref *ref;
1863 struct btrfs_key ins;
1864 u64 parent = 0;
1865 u64 ref_root = 0;
1866 u64 flags = 0;
Chris Mason56bec292009-03-13 10:10:06 -04001867
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001868 ins.objectid = node->bytenr;
1869 ins.offset = node->num_bytes;
1870 ins.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason56bec292009-03-13 10:10:06 -04001871
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001872 ref = btrfs_delayed_node_to_data_ref(node);
1873 if (node->type == BTRFS_SHARED_DATA_REF_KEY)
1874 parent = ref->parent;
1875 else
1876 ref_root = ref->root;
1877
1878 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
1879 if (extent_op) {
1880 BUG_ON(extent_op->update_key);
1881 flags |= extent_op->flags_to_set;
1882 }
1883 ret = alloc_reserved_file_extent(trans, root,
1884 parent, ref_root, flags,
1885 ref->objectid, ref->offset,
1886 &ins, node->ref_mod);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001887 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
1888 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
1889 node->num_bytes, parent,
1890 ref_root, ref->objectid,
1891 ref->offset, node->ref_mod,
1892 extent_op);
1893 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
1894 ret = __btrfs_free_extent(trans, root, node->bytenr,
1895 node->num_bytes, parent,
1896 ref_root, ref->objectid,
1897 ref->offset, node->ref_mod,
1898 extent_op);
1899 } else {
1900 BUG();
1901 }
Chris Mason56bec292009-03-13 10:10:06 -04001902 return ret;
1903}
1904
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001905static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
1906 struct extent_buffer *leaf,
1907 struct btrfs_extent_item *ei)
1908{
1909 u64 flags = btrfs_extent_flags(leaf, ei);
1910 if (extent_op->update_flags) {
1911 flags |= extent_op->flags_to_set;
1912 btrfs_set_extent_flags(leaf, ei, flags);
1913 }
1914
1915 if (extent_op->update_key) {
1916 struct btrfs_tree_block_info *bi;
1917 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK));
1918 bi = (struct btrfs_tree_block_info *)(ei + 1);
1919 btrfs_set_tree_block_key(leaf, bi, &extent_op->key);
1920 }
1921}
1922
1923static int run_delayed_extent_op(struct btrfs_trans_handle *trans,
1924 struct btrfs_root *root,
1925 struct btrfs_delayed_ref_node *node,
1926 struct btrfs_delayed_extent_op *extent_op)
1927{
1928 struct btrfs_key key;
1929 struct btrfs_path *path;
1930 struct btrfs_extent_item *ei;
1931 struct extent_buffer *leaf;
1932 u32 item_size;
1933 int ret;
1934 int err = 0;
1935
1936 path = btrfs_alloc_path();
1937 if (!path)
1938 return -ENOMEM;
1939
1940 key.objectid = node->bytenr;
1941 key.type = BTRFS_EXTENT_ITEM_KEY;
1942 key.offset = node->num_bytes;
1943
1944 path->reada = 1;
1945 path->leave_spinning = 1;
1946 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key,
1947 path, 0, 1);
1948 if (ret < 0) {
1949 err = ret;
1950 goto out;
1951 }
1952 if (ret > 0) {
1953 err = -EIO;
1954 goto out;
1955 }
1956
1957 leaf = path->nodes[0];
1958 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1959#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1960 if (item_size < sizeof(*ei)) {
1961 ret = convert_extent_item_v0(trans, root->fs_info->extent_root,
1962 path, (u64)-1, 0);
1963 if (ret < 0) {
1964 err = ret;
1965 goto out;
1966 }
1967 leaf = path->nodes[0];
1968 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1969 }
1970#endif
1971 BUG_ON(item_size < sizeof(*ei));
1972 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1973 __run_delayed_extent_op(extent_op, leaf, ei);
1974
1975 btrfs_mark_buffer_dirty(leaf);
1976out:
1977 btrfs_free_path(path);
1978 return err;
1979}
1980
1981static int run_delayed_tree_ref(struct btrfs_trans_handle *trans,
1982 struct btrfs_root *root,
1983 struct btrfs_delayed_ref_node *node,
1984 struct btrfs_delayed_extent_op *extent_op,
1985 int insert_reserved)
1986{
1987 int ret = 0;
1988 struct btrfs_delayed_tree_ref *ref;
1989 struct btrfs_key ins;
1990 u64 parent = 0;
1991 u64 ref_root = 0;
1992
1993 ins.objectid = node->bytenr;
1994 ins.offset = node->num_bytes;
1995 ins.type = BTRFS_EXTENT_ITEM_KEY;
1996
1997 ref = btrfs_delayed_node_to_tree_ref(node);
1998 if (node->type == BTRFS_SHARED_BLOCK_REF_KEY)
1999 parent = ref->parent;
2000 else
2001 ref_root = ref->root;
2002
2003 BUG_ON(node->ref_mod != 1);
2004 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
2005 BUG_ON(!extent_op || !extent_op->update_flags ||
2006 !extent_op->update_key);
2007 ret = alloc_reserved_tree_block(trans, root,
2008 parent, ref_root,
2009 extent_op->flags_to_set,
2010 &extent_op->key,
2011 ref->level, &ins);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002012 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
2013 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
2014 node->num_bytes, parent, ref_root,
2015 ref->level, 0, 1, extent_op);
2016 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
2017 ret = __btrfs_free_extent(trans, root, node->bytenr,
2018 node->num_bytes, parent, ref_root,
2019 ref->level, 0, 1, extent_op);
2020 } else {
2021 BUG();
2022 }
2023 return ret;
2024}
2025
Chris Mason56bec292009-03-13 10:10:06 -04002026/* helper function to actually process a single delayed ref entry */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002027static int run_one_delayed_ref(struct btrfs_trans_handle *trans,
2028 struct btrfs_root *root,
2029 struct btrfs_delayed_ref_node *node,
2030 struct btrfs_delayed_extent_op *extent_op,
2031 int insert_reserved)
Chris Mason56bec292009-03-13 10:10:06 -04002032{
Josef Bacikeb099672009-02-12 09:27:38 -05002033 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002034 if (btrfs_delayed_ref_is_head(node)) {
Chris Mason56bec292009-03-13 10:10:06 -04002035 struct btrfs_delayed_ref_head *head;
2036 /*
2037 * we've hit the end of the chain and we were supposed
2038 * to insert this extent into the tree. But, it got
2039 * deleted before we ever needed to insert it, so all
2040 * we have to do is clean up the accounting
2041 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002042 BUG_ON(extent_op);
2043 head = btrfs_delayed_node_to_head(node);
Chris Mason56bec292009-03-13 10:10:06 -04002044 if (insert_reserved) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04002045 btrfs_pin_extent(root, node->bytenr,
2046 node->num_bytes, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002047 if (head->is_data) {
2048 ret = btrfs_del_csums(trans, root,
2049 node->bytenr,
2050 node->num_bytes);
2051 BUG_ON(ret);
2052 }
Chris Mason56bec292009-03-13 10:10:06 -04002053 }
Chris Mason56bec292009-03-13 10:10:06 -04002054 mutex_unlock(&head->mutex);
2055 return 0;
2056 }
Josef Bacikeb099672009-02-12 09:27:38 -05002057
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002058 if (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
2059 node->type == BTRFS_SHARED_BLOCK_REF_KEY)
2060 ret = run_delayed_tree_ref(trans, root, node, extent_op,
2061 insert_reserved);
2062 else if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
2063 node->type == BTRFS_SHARED_DATA_REF_KEY)
2064 ret = run_delayed_data_ref(trans, root, node, extent_op,
2065 insert_reserved);
2066 else
2067 BUG();
2068 return ret;
Chris Masone9d0b132007-08-10 14:06:19 -04002069}
2070
Chris Mason56bec292009-03-13 10:10:06 -04002071static noinline struct btrfs_delayed_ref_node *
2072select_delayed_ref(struct btrfs_delayed_ref_head *head)
Chris Masona28ec192007-03-06 20:08:01 -05002073{
Chris Mason56bec292009-03-13 10:10:06 -04002074 struct rb_node *node;
2075 struct btrfs_delayed_ref_node *ref;
2076 int action = BTRFS_ADD_DELAYED_REF;
2077again:
2078 /*
2079 * select delayed ref of type BTRFS_ADD_DELAYED_REF first.
2080 * this prevents ref count from going down to zero when
2081 * there still are pending delayed ref.
2082 */
2083 node = rb_prev(&head->node.rb_node);
2084 while (1) {
2085 if (!node)
2086 break;
2087 ref = rb_entry(node, struct btrfs_delayed_ref_node,
2088 rb_node);
2089 if (ref->bytenr != head->node.bytenr)
2090 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002091 if (ref->action == action)
Chris Mason56bec292009-03-13 10:10:06 -04002092 return ref;
2093 node = rb_prev(node);
Chris Mason5f39d392007-10-15 16:14:19 -04002094 }
Chris Mason56bec292009-03-13 10:10:06 -04002095 if (action == BTRFS_ADD_DELAYED_REF) {
2096 action = BTRFS_DROP_DELAYED_REF;
2097 goto again;
2098 }
2099 return NULL;
2100}
2101
Chris Masonc3e69d52009-03-13 10:17:05 -04002102static noinline int run_clustered_refs(struct btrfs_trans_handle *trans,
2103 struct btrfs_root *root,
2104 struct list_head *cluster)
Chris Mason56bec292009-03-13 10:10:06 -04002105{
Chris Mason56bec292009-03-13 10:10:06 -04002106 struct btrfs_delayed_ref_root *delayed_refs;
2107 struct btrfs_delayed_ref_node *ref;
2108 struct btrfs_delayed_ref_head *locked_ref = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002109 struct btrfs_delayed_extent_op *extent_op;
Chris Mason56bec292009-03-13 10:10:06 -04002110 int ret;
Chris Masonc3e69d52009-03-13 10:17:05 -04002111 int count = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002112 int must_insert_reserved = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002113
2114 delayed_refs = &trans->transaction->delayed_refs;
Chris Mason56bec292009-03-13 10:10:06 -04002115 while (1) {
2116 if (!locked_ref) {
Chris Masonc3e69d52009-03-13 10:17:05 -04002117 /* pick a new head ref from the cluster list */
2118 if (list_empty(cluster))
Chris Mason56bec292009-03-13 10:10:06 -04002119 break;
Chris Mason56bec292009-03-13 10:10:06 -04002120
Chris Masonc3e69d52009-03-13 10:17:05 -04002121 locked_ref = list_entry(cluster->next,
2122 struct btrfs_delayed_ref_head, cluster);
2123
2124 /* grab the lock that says we are going to process
2125 * all the refs for this head */
2126 ret = btrfs_delayed_ref_lock(trans, locked_ref);
2127
2128 /*
2129 * we may have dropped the spin lock to get the head
2130 * mutex lock, and that might have given someone else
2131 * time to free the head. If that's true, it has been
2132 * removed from our list and we can move on.
2133 */
2134 if (ret == -EAGAIN) {
2135 locked_ref = NULL;
2136 count++;
2137 continue;
Chris Mason56bec292009-03-13 10:10:06 -04002138 }
2139 }
2140
2141 /*
2142 * record the must insert reserved flag before we
2143 * drop the spin lock.
2144 */
2145 must_insert_reserved = locked_ref->must_insert_reserved;
2146 locked_ref->must_insert_reserved = 0;
2147
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002148 extent_op = locked_ref->extent_op;
2149 locked_ref->extent_op = NULL;
2150
Chris Mason56bec292009-03-13 10:10:06 -04002151 /*
2152 * locked_ref is the head node, so we have to go one
2153 * node back for any delayed ref updates
2154 */
Chris Mason56bec292009-03-13 10:10:06 -04002155 ref = select_delayed_ref(locked_ref);
2156 if (!ref) {
2157 /* All delayed refs have been processed, Go ahead
2158 * and send the head node to run_one_delayed_ref,
2159 * so that any accounting fixes can happen
2160 */
2161 ref = &locked_ref->node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002162
2163 if (extent_op && must_insert_reserved) {
2164 kfree(extent_op);
2165 extent_op = NULL;
2166 }
2167
2168 if (extent_op) {
2169 spin_unlock(&delayed_refs->lock);
2170
2171 ret = run_delayed_extent_op(trans, root,
2172 ref, extent_op);
2173 BUG_ON(ret);
2174 kfree(extent_op);
2175
2176 cond_resched();
2177 spin_lock(&delayed_refs->lock);
2178 continue;
2179 }
2180
Chris Masonc3e69d52009-03-13 10:17:05 -04002181 list_del_init(&locked_ref->cluster);
Chris Mason56bec292009-03-13 10:10:06 -04002182 locked_ref = NULL;
2183 }
2184
2185 ref->in_tree = 0;
2186 rb_erase(&ref->rb_node, &delayed_refs->root);
2187 delayed_refs->num_entries--;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002188
Chris Mason56bec292009-03-13 10:10:06 -04002189 spin_unlock(&delayed_refs->lock);
2190
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002191 ret = run_one_delayed_ref(trans, root, ref, extent_op,
Chris Mason56bec292009-03-13 10:10:06 -04002192 must_insert_reserved);
2193 BUG_ON(ret);
Chris Mason56bec292009-03-13 10:10:06 -04002194
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002195 btrfs_put_delayed_ref(ref);
2196 kfree(extent_op);
Chris Masonc3e69d52009-03-13 10:17:05 -04002197 count++;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002198
Chris Mason1887be62009-03-13 10:11:24 -04002199 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002200 spin_lock(&delayed_refs->lock);
2201 }
Chris Masonc3e69d52009-03-13 10:17:05 -04002202 return count;
2203}
2204
2205/*
2206 * this starts processing the delayed reference count updates and
2207 * extent insertions we have queued up so far. count can be
2208 * 0, which means to process everything in the tree at the start
2209 * of the run (but not newly added entries), or it can be some target
2210 * number you'd like to process.
2211 */
2212int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
2213 struct btrfs_root *root, unsigned long count)
2214{
2215 struct rb_node *node;
2216 struct btrfs_delayed_ref_root *delayed_refs;
2217 struct btrfs_delayed_ref_node *ref;
2218 struct list_head cluster;
2219 int ret;
2220 int run_all = count == (unsigned long)-1;
2221 int run_most = 0;
2222
2223 if (root == root->fs_info->extent_root)
2224 root = root->fs_info->tree_root;
2225
2226 delayed_refs = &trans->transaction->delayed_refs;
2227 INIT_LIST_HEAD(&cluster);
2228again:
2229 spin_lock(&delayed_refs->lock);
2230 if (count == 0) {
2231 count = delayed_refs->num_entries * 2;
2232 run_most = 1;
2233 }
2234 while (1) {
2235 if (!(run_all || run_most) &&
2236 delayed_refs->num_heads_ready < 64)
2237 break;
2238
2239 /*
2240 * go find something we can process in the rbtree. We start at
2241 * the beginning of the tree, and then build a cluster
2242 * of refs to process starting at the first one we are able to
2243 * lock
2244 */
2245 ret = btrfs_find_ref_cluster(trans, &cluster,
2246 delayed_refs->run_delayed_start);
2247 if (ret)
2248 break;
2249
2250 ret = run_clustered_refs(trans, root, &cluster);
2251 BUG_ON(ret < 0);
2252
2253 count -= min_t(unsigned long, ret, count);
2254
2255 if (count == 0)
2256 break;
2257 }
2258
Chris Mason56bec292009-03-13 10:10:06 -04002259 if (run_all) {
Chris Mason56bec292009-03-13 10:10:06 -04002260 node = rb_first(&delayed_refs->root);
Chris Masonc3e69d52009-03-13 10:17:05 -04002261 if (!node)
Chris Mason56bec292009-03-13 10:10:06 -04002262 goto out;
Chris Masonc3e69d52009-03-13 10:17:05 -04002263 count = (unsigned long)-1;
Chris Mason56bec292009-03-13 10:10:06 -04002264
2265 while (node) {
2266 ref = rb_entry(node, struct btrfs_delayed_ref_node,
2267 rb_node);
2268 if (btrfs_delayed_ref_is_head(ref)) {
2269 struct btrfs_delayed_ref_head *head;
2270
2271 head = btrfs_delayed_node_to_head(ref);
2272 atomic_inc(&ref->refs);
2273
2274 spin_unlock(&delayed_refs->lock);
2275 mutex_lock(&head->mutex);
2276 mutex_unlock(&head->mutex);
2277
2278 btrfs_put_delayed_ref(ref);
Chris Mason1887be62009-03-13 10:11:24 -04002279 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002280 goto again;
2281 }
2282 node = rb_next(node);
2283 }
2284 spin_unlock(&delayed_refs->lock);
Chris Mason56bec292009-03-13 10:10:06 -04002285 schedule_timeout(1);
2286 goto again;
2287 }
Chris Mason54aa1f42007-06-22 14:16:25 -04002288out:
Chris Masonc3e69d52009-03-13 10:17:05 -04002289 spin_unlock(&delayed_refs->lock);
Chris Masona28ec192007-03-06 20:08:01 -05002290 return 0;
2291}
2292
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002293int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
2294 struct btrfs_root *root,
2295 u64 bytenr, u64 num_bytes, u64 flags,
2296 int is_data)
2297{
2298 struct btrfs_delayed_extent_op *extent_op;
2299 int ret;
2300
2301 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
2302 if (!extent_op)
2303 return -ENOMEM;
2304
2305 extent_op->flags_to_set = flags;
2306 extent_op->update_flags = 1;
2307 extent_op->update_key = 0;
2308 extent_op->is_data = is_data ? 1 : 0;
2309
2310 ret = btrfs_add_delayed_extent_op(trans, bytenr, num_bytes, extent_op);
2311 if (ret)
2312 kfree(extent_op);
2313 return ret;
2314}
2315
2316static noinline int check_delayed_ref(struct btrfs_trans_handle *trans,
2317 struct btrfs_root *root,
2318 struct btrfs_path *path,
2319 u64 objectid, u64 offset, u64 bytenr)
2320{
2321 struct btrfs_delayed_ref_head *head;
2322 struct btrfs_delayed_ref_node *ref;
2323 struct btrfs_delayed_data_ref *data_ref;
2324 struct btrfs_delayed_ref_root *delayed_refs;
2325 struct rb_node *node;
2326 int ret = 0;
2327
2328 ret = -ENOENT;
2329 delayed_refs = &trans->transaction->delayed_refs;
2330 spin_lock(&delayed_refs->lock);
2331 head = btrfs_find_delayed_ref_head(trans, bytenr);
2332 if (!head)
2333 goto out;
2334
2335 if (!mutex_trylock(&head->mutex)) {
2336 atomic_inc(&head->node.refs);
2337 spin_unlock(&delayed_refs->lock);
2338
2339 btrfs_release_path(root->fs_info->extent_root, path);
2340
2341 mutex_lock(&head->mutex);
2342 mutex_unlock(&head->mutex);
2343 btrfs_put_delayed_ref(&head->node);
2344 return -EAGAIN;
2345 }
2346
2347 node = rb_prev(&head->node.rb_node);
2348 if (!node)
2349 goto out_unlock;
2350
2351 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2352
2353 if (ref->bytenr != bytenr)
2354 goto out_unlock;
2355
2356 ret = 1;
2357 if (ref->type != BTRFS_EXTENT_DATA_REF_KEY)
2358 goto out_unlock;
2359
2360 data_ref = btrfs_delayed_node_to_data_ref(ref);
2361
2362 node = rb_prev(node);
2363 if (node) {
2364 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2365 if (ref->bytenr == bytenr)
2366 goto out_unlock;
2367 }
2368
2369 if (data_ref->root != root->root_key.objectid ||
2370 data_ref->objectid != objectid || data_ref->offset != offset)
2371 goto out_unlock;
2372
2373 ret = 0;
2374out_unlock:
2375 mutex_unlock(&head->mutex);
2376out:
2377 spin_unlock(&delayed_refs->lock);
2378 return ret;
2379}
2380
2381static noinline int check_committed_ref(struct btrfs_trans_handle *trans,
2382 struct btrfs_root *root,
2383 struct btrfs_path *path,
2384 u64 objectid, u64 offset, u64 bytenr)
Chris Masonbe20aa92007-12-17 20:14:01 -05002385{
2386 struct btrfs_root *extent_root = root->fs_info->extent_root;
Yan Zhengf321e492008-07-30 09:26:11 -04002387 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002388 struct btrfs_extent_data_ref *ref;
2389 struct btrfs_extent_inline_ref *iref;
2390 struct btrfs_extent_item *ei;
Chris Masonbe20aa92007-12-17 20:14:01 -05002391 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002392 u32 item_size;
Yan Zhengf321e492008-07-30 09:26:11 -04002393 int ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05002394
Chris Masonbe20aa92007-12-17 20:14:01 -05002395 key.objectid = bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -04002396 key.offset = (u64)-1;
Yan Zhengf321e492008-07-30 09:26:11 -04002397 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masonbe20aa92007-12-17 20:14:01 -05002398
Chris Masonbe20aa92007-12-17 20:14:01 -05002399 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
2400 if (ret < 0)
2401 goto out;
2402 BUG_ON(ret == 0);
Yan Zheng80ff3852008-10-30 14:20:02 -04002403
2404 ret = -ENOENT;
2405 if (path->slots[0] == 0)
Zheng Yan31840ae2008-09-23 13:14:14 -04002406 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002407
Zheng Yan31840ae2008-09-23 13:14:14 -04002408 path->slots[0]--;
Yan Zhengf321e492008-07-30 09:26:11 -04002409 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002410 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05002411
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002412 if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY)
Chris Masonbe20aa92007-12-17 20:14:01 -05002413 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002414
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002415 ret = 1;
2416 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2417#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2418 if (item_size < sizeof(*ei)) {
2419 WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0));
2420 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002421 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002422#endif
2423 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2424
2425 if (item_size != sizeof(*ei) +
2426 btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY))
2427 goto out;
2428
2429 if (btrfs_extent_generation(leaf, ei) <=
2430 btrfs_root_last_snapshot(&root->root_item))
2431 goto out;
2432
2433 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
2434 if (btrfs_extent_inline_ref_type(leaf, iref) !=
2435 BTRFS_EXTENT_DATA_REF_KEY)
2436 goto out;
2437
2438 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
2439 if (btrfs_extent_refs(leaf, ei) !=
2440 btrfs_extent_data_ref_count(leaf, ref) ||
2441 btrfs_extent_data_ref_root(leaf, ref) !=
2442 root->root_key.objectid ||
2443 btrfs_extent_data_ref_objectid(leaf, ref) != objectid ||
2444 btrfs_extent_data_ref_offset(leaf, ref) != offset)
2445 goto out;
2446
Yan Zhengf321e492008-07-30 09:26:11 -04002447 ret = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05002448out:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002449 return ret;
2450}
2451
2452int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
2453 struct btrfs_root *root,
2454 u64 objectid, u64 offset, u64 bytenr)
2455{
2456 struct btrfs_path *path;
2457 int ret;
2458 int ret2;
2459
2460 path = btrfs_alloc_path();
2461 if (!path)
2462 return -ENOENT;
2463
2464 do {
2465 ret = check_committed_ref(trans, root, path, objectid,
2466 offset, bytenr);
2467 if (ret && ret != -ENOENT)
2468 goto out;
2469
2470 ret2 = check_delayed_ref(trans, root, path, objectid,
2471 offset, bytenr);
2472 } while (ret2 == -EAGAIN);
2473
2474 if (ret2 && ret2 != -ENOENT) {
2475 ret = ret2;
2476 goto out;
2477 }
2478
2479 if (ret != -ENOENT || ret2 != -ENOENT)
2480 ret = 0;
2481out:
Yan Zhengf321e492008-07-30 09:26:11 -04002482 btrfs_free_path(path);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002483 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2484 WARN_ON(ret > 0);
Yan Zhengf321e492008-07-30 09:26:11 -04002485 return ret;
2486}
2487
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002488#if 0
Zheng Yan31840ae2008-09-23 13:14:14 -04002489int btrfs_cache_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2490 struct extent_buffer *buf, u32 nr_extents)
Chris Mason02217ed2007-03-02 16:08:05 -05002491{
Chris Mason5f39d392007-10-15 16:14:19 -04002492 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -04002493 struct btrfs_file_extent_item *fi;
Zheng Yane4657682008-09-26 10:04:53 -04002494 u64 root_gen;
2495 u32 nritems;
Chris Mason02217ed2007-03-02 16:08:05 -05002496 int i;
Chris Masondb945352007-10-15 16:15:53 -04002497 int level;
Zheng Yan31840ae2008-09-23 13:14:14 -04002498 int ret = 0;
Zheng Yane4657682008-09-26 10:04:53 -04002499 int shared = 0;
Chris Masona28ec192007-03-06 20:08:01 -05002500
Chris Mason3768f362007-03-13 16:47:54 -04002501 if (!root->ref_cows)
Chris Masona28ec192007-03-06 20:08:01 -05002502 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002503
Zheng Yane4657682008-09-26 10:04:53 -04002504 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
2505 shared = 0;
2506 root_gen = root->root_key.offset;
2507 } else {
2508 shared = 1;
2509 root_gen = trans->transid - 1;
2510 }
2511
Chris Masondb945352007-10-15 16:15:53 -04002512 level = btrfs_header_level(buf);
Chris Mason5f39d392007-10-15 16:14:19 -04002513 nritems = btrfs_header_nritems(buf);
Chris Mason4a096752008-07-21 10:29:44 -04002514
Zheng Yan31840ae2008-09-23 13:14:14 -04002515 if (level == 0) {
Yan Zheng31153d82008-07-28 15:32:19 -04002516 struct btrfs_leaf_ref *ref;
2517 struct btrfs_extent_info *info;
2518
Zheng Yan31840ae2008-09-23 13:14:14 -04002519 ref = btrfs_alloc_leaf_ref(root, nr_extents);
Yan Zheng31153d82008-07-28 15:32:19 -04002520 if (!ref) {
Zheng Yan31840ae2008-09-23 13:14:14 -04002521 ret = -ENOMEM;
Yan Zheng31153d82008-07-28 15:32:19 -04002522 goto out;
2523 }
2524
Zheng Yane4657682008-09-26 10:04:53 -04002525 ref->root_gen = root_gen;
Yan Zheng31153d82008-07-28 15:32:19 -04002526 ref->bytenr = buf->start;
2527 ref->owner = btrfs_header_owner(buf);
2528 ref->generation = btrfs_header_generation(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04002529 ref->nritems = nr_extents;
Yan Zheng31153d82008-07-28 15:32:19 -04002530 info = ref->extents;
Yanbcc63ab2008-07-30 16:29:20 -04002531
Zheng Yan31840ae2008-09-23 13:14:14 -04002532 for (i = 0; nr_extents > 0 && i < nritems; i++) {
Yan Zheng31153d82008-07-28 15:32:19 -04002533 u64 disk_bytenr;
2534 btrfs_item_key_to_cpu(buf, &key, i);
2535 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
2536 continue;
2537 fi = btrfs_item_ptr(buf, i,
2538 struct btrfs_file_extent_item);
2539 if (btrfs_file_extent_type(buf, fi) ==
2540 BTRFS_FILE_EXTENT_INLINE)
2541 continue;
2542 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
2543 if (disk_bytenr == 0)
2544 continue;
2545
2546 info->bytenr = disk_bytenr;
2547 info->num_bytes =
2548 btrfs_file_extent_disk_num_bytes(buf, fi);
2549 info->objectid = key.objectid;
2550 info->offset = key.offset;
2551 info++;
2552 }
2553
Zheng Yane4657682008-09-26 10:04:53 -04002554 ret = btrfs_add_leaf_ref(root, ref, shared);
Yan Zheng5b84e8d2008-10-09 11:46:19 -04002555 if (ret == -EEXIST && shared) {
2556 struct btrfs_leaf_ref *old;
2557 old = btrfs_lookup_leaf_ref(root, ref->bytenr);
2558 BUG_ON(!old);
2559 btrfs_remove_leaf_ref(root, old);
2560 btrfs_free_leaf_ref(root, old);
2561 ret = btrfs_add_leaf_ref(root, ref, shared);
2562 }
Yan Zheng31153d82008-07-28 15:32:19 -04002563 WARN_ON(ret);
Yanbcc63ab2008-07-30 16:29:20 -04002564 btrfs_free_leaf_ref(root, ref);
Yan Zheng31153d82008-07-28 15:32:19 -04002565 }
2566out:
Zheng Yan31840ae2008-09-23 13:14:14 -04002567 return ret;
2568}
2569
Chris Masonb7a9f292009-02-04 09:23:45 -05002570/* when a block goes through cow, we update the reference counts of
2571 * everything that block points to. The internal pointers of the block
2572 * can be in just about any order, and it is likely to have clusters of
2573 * things that are close together and clusters of things that are not.
2574 *
2575 * To help reduce the seeks that come with updating all of these reference
2576 * counts, sort them by byte number before actual updates are done.
2577 *
2578 * struct refsort is used to match byte number to slot in the btree block.
2579 * we sort based on the byte number and then use the slot to actually
2580 * find the item.
Chris Masonbd56b302009-02-04 09:27:02 -05002581 *
2582 * struct refsort is smaller than strcut btrfs_item and smaller than
2583 * struct btrfs_key_ptr. Since we're currently limited to the page size
2584 * for a btree block, there's no way for a kmalloc of refsorts for a
2585 * single node to be bigger than a page.
Chris Masonb7a9f292009-02-04 09:23:45 -05002586 */
2587struct refsort {
2588 u64 bytenr;
2589 u32 slot;
2590};
2591
2592/*
2593 * for passing into sort()
2594 */
2595static int refsort_cmp(const void *a_void, const void *b_void)
2596{
2597 const struct refsort *a = a_void;
2598 const struct refsort *b = b_void;
2599
2600 if (a->bytenr < b->bytenr)
2601 return -1;
2602 if (a->bytenr > b->bytenr)
2603 return 1;
2604 return 0;
2605}
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002606#endif
Chris Masonb7a9f292009-02-04 09:23:45 -05002607
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002608static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
Chris Masonb7a9f292009-02-04 09:23:45 -05002609 struct btrfs_root *root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002610 struct extent_buffer *buf,
2611 int full_backref, int inc)
Zheng Yan31840ae2008-09-23 13:14:14 -04002612{
2613 u64 bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002614 u64 num_bytes;
2615 u64 parent;
Zheng Yan31840ae2008-09-23 13:14:14 -04002616 u64 ref_root;
Zheng Yan31840ae2008-09-23 13:14:14 -04002617 u32 nritems;
Zheng Yan31840ae2008-09-23 13:14:14 -04002618 struct btrfs_key key;
2619 struct btrfs_file_extent_item *fi;
2620 int i;
2621 int level;
2622 int ret = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04002623 int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002624 u64, u64, u64, u64, u64, u64);
Zheng Yan31840ae2008-09-23 13:14:14 -04002625
2626 ref_root = btrfs_header_owner(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04002627 nritems = btrfs_header_nritems(buf);
2628 level = btrfs_header_level(buf);
2629
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002630 if (!root->ref_cows && level == 0)
2631 return 0;
Chris Masonb7a9f292009-02-04 09:23:45 -05002632
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002633 if (inc)
2634 process_func = btrfs_inc_extent_ref;
2635 else
2636 process_func = btrfs_free_extent;
Zheng Yan31840ae2008-09-23 13:14:14 -04002637
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002638 if (full_backref)
2639 parent = buf->start;
2640 else
2641 parent = 0;
2642
Zheng Yan31840ae2008-09-23 13:14:14 -04002643 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04002644 if (level == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002645 btrfs_item_key_to_cpu(buf, &key, i);
2646 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -04002647 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04002648 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -04002649 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002650 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -04002651 BTRFS_FILE_EXTENT_INLINE)
2652 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04002653 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
2654 if (bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -04002655 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04002656
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002657 num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
2658 key.offset -= btrfs_file_extent_offset(buf, fi);
2659 ret = process_func(trans, root, bytenr, num_bytes,
2660 parent, ref_root, key.objectid,
2661 key.offset);
2662 if (ret)
2663 goto fail;
Chris Masonb7a9f292009-02-04 09:23:45 -05002664 } else {
2665 bytenr = btrfs_node_blockptr(buf, i);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002666 num_bytes = btrfs_level_size(root, level - 1);
2667 ret = process_func(trans, root, bytenr, num_bytes,
2668 parent, ref_root, level - 1, 0);
2669 if (ret)
Zheng Yan31840ae2008-09-23 13:14:14 -04002670 goto fail;
Chris Mason54aa1f42007-06-22 14:16:25 -04002671 }
2672 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002673 return 0;
2674fail:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002675 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -04002676 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05002677}
2678
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002679int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2680 struct extent_buffer *buf, int full_backref)
Zheng Yan31840ae2008-09-23 13:14:14 -04002681{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002682 return __btrfs_mod_ref(trans, root, buf, full_backref, 1);
2683}
Zheng Yan31840ae2008-09-23 13:14:14 -04002684
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002685int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2686 struct extent_buffer *buf, int full_backref)
2687{
2688 return __btrfs_mod_ref(trans, root, buf, full_backref, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04002689}
2690
Chris Mason9078a3e2007-04-26 16:46:15 -04002691static int write_one_cache_group(struct btrfs_trans_handle *trans,
2692 struct btrfs_root *root,
2693 struct btrfs_path *path,
2694 struct btrfs_block_group_cache *cache)
2695{
2696 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04002697 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04002698 unsigned long bi;
2699 struct extent_buffer *leaf;
Chris Mason9078a3e2007-04-26 16:46:15 -04002700
Chris Mason9078a3e2007-04-26 16:46:15 -04002701 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04002702 if (ret < 0)
2703 goto fail;
Chris Mason9078a3e2007-04-26 16:46:15 -04002704 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04002705
2706 leaf = path->nodes[0];
2707 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
2708 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
2709 btrfs_mark_buffer_dirty(leaf);
Chris Mason9078a3e2007-04-26 16:46:15 -04002710 btrfs_release_path(extent_root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -04002711fail:
Chris Mason9078a3e2007-04-26 16:46:15 -04002712 if (ret)
2713 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04002714 return 0;
2715
2716}
2717
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002718static struct btrfs_block_group_cache *
2719next_block_group(struct btrfs_root *root,
2720 struct btrfs_block_group_cache *cache)
2721{
2722 struct rb_node *node;
2723 spin_lock(&root->fs_info->block_group_cache_lock);
2724 node = rb_next(&cache->cache_node);
2725 btrfs_put_block_group(cache);
2726 if (node) {
2727 cache = rb_entry(node, struct btrfs_block_group_cache,
2728 cache_node);
Josef Bacik11dfe352009-11-13 20:12:59 +00002729 btrfs_get_block_group(cache);
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002730 } else
2731 cache = NULL;
2732 spin_unlock(&root->fs_info->block_group_cache_lock);
2733 return cache;
2734}
2735
Josef Bacik0af3d002010-06-21 14:48:16 -04002736static int cache_save_setup(struct btrfs_block_group_cache *block_group,
2737 struct btrfs_trans_handle *trans,
2738 struct btrfs_path *path)
2739{
2740 struct btrfs_root *root = block_group->fs_info->tree_root;
2741 struct inode *inode = NULL;
2742 u64 alloc_hint = 0;
Josef Bacik2b209822010-12-03 13:17:53 -05002743 int dcs = BTRFS_DC_ERROR;
Josef Bacik0af3d002010-06-21 14:48:16 -04002744 int num_pages = 0;
2745 int retries = 0;
2746 int ret = 0;
2747
2748 /*
2749 * If this block group is smaller than 100 megs don't bother caching the
2750 * block group.
2751 */
2752 if (block_group->key.offset < (100 * 1024 * 1024)) {
2753 spin_lock(&block_group->lock);
2754 block_group->disk_cache_state = BTRFS_DC_WRITTEN;
2755 spin_unlock(&block_group->lock);
2756 return 0;
2757 }
2758
2759again:
2760 inode = lookup_free_space_inode(root, block_group, path);
2761 if (IS_ERR(inode) && PTR_ERR(inode) != -ENOENT) {
2762 ret = PTR_ERR(inode);
2763 btrfs_release_path(root, path);
2764 goto out;
2765 }
2766
2767 if (IS_ERR(inode)) {
2768 BUG_ON(retries);
2769 retries++;
2770
2771 if (block_group->ro)
2772 goto out_free;
2773
2774 ret = create_free_space_inode(root, trans, block_group, path);
2775 if (ret)
2776 goto out_free;
2777 goto again;
2778 }
2779
2780 /*
2781 * We want to set the generation to 0, that way if anything goes wrong
2782 * from here on out we know not to trust this cache when we load up next
2783 * time.
2784 */
2785 BTRFS_I(inode)->generation = 0;
2786 ret = btrfs_update_inode(trans, root, inode);
2787 WARN_ON(ret);
2788
2789 if (i_size_read(inode) > 0) {
2790 ret = btrfs_truncate_free_space_cache(root, trans, path,
2791 inode);
2792 if (ret)
2793 goto out_put;
2794 }
2795
2796 spin_lock(&block_group->lock);
2797 if (block_group->cached != BTRFS_CACHE_FINISHED) {
Josef Bacik2b209822010-12-03 13:17:53 -05002798 /* We're not cached, don't bother trying to write stuff out */
2799 dcs = BTRFS_DC_WRITTEN;
Josef Bacik0af3d002010-06-21 14:48:16 -04002800 spin_unlock(&block_group->lock);
2801 goto out_put;
2802 }
2803 spin_unlock(&block_group->lock);
2804
2805 num_pages = (int)div64_u64(block_group->key.offset, 1024 * 1024 * 1024);
2806 if (!num_pages)
2807 num_pages = 1;
2808
2809 /*
2810 * Just to make absolutely sure we have enough space, we're going to
2811 * preallocate 12 pages worth of space for each block group. In
2812 * practice we ought to use at most 8, but we need extra space so we can
2813 * add our header and have a terminator between the extents and the
2814 * bitmaps.
2815 */
2816 num_pages *= 16;
2817 num_pages *= PAGE_CACHE_SIZE;
2818
2819 ret = btrfs_check_data_free_space(inode, num_pages);
2820 if (ret)
2821 goto out_put;
2822
2823 ret = btrfs_prealloc_file_range_trans(inode, trans, 0, 0, num_pages,
2824 num_pages, num_pages,
2825 &alloc_hint);
Josef Bacik2b209822010-12-03 13:17:53 -05002826 if (!ret)
2827 dcs = BTRFS_DC_SETUP;
Josef Bacik0af3d002010-06-21 14:48:16 -04002828 btrfs_free_reserved_data_space(inode, num_pages);
2829out_put:
2830 iput(inode);
2831out_free:
2832 btrfs_release_path(root, path);
2833out:
2834 spin_lock(&block_group->lock);
Josef Bacik2b209822010-12-03 13:17:53 -05002835 block_group->disk_cache_state = dcs;
Josef Bacik0af3d002010-06-21 14:48:16 -04002836 spin_unlock(&block_group->lock);
2837
2838 return ret;
2839}
2840
Chris Mason96b51792007-10-15 16:15:19 -04002841int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
2842 struct btrfs_root *root)
Chris Mason9078a3e2007-04-26 16:46:15 -04002843{
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002844 struct btrfs_block_group_cache *cache;
Chris Mason9078a3e2007-04-26 16:46:15 -04002845 int err = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04002846 struct btrfs_path *path;
Chris Mason96b51792007-10-15 16:15:19 -04002847 u64 last = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04002848
2849 path = btrfs_alloc_path();
2850 if (!path)
2851 return -ENOMEM;
2852
Josef Bacik0af3d002010-06-21 14:48:16 -04002853again:
2854 while (1) {
2855 cache = btrfs_lookup_first_block_group(root->fs_info, last);
2856 while (cache) {
2857 if (cache->disk_cache_state == BTRFS_DC_CLEAR)
2858 break;
2859 cache = next_block_group(root, cache);
2860 }
2861 if (!cache) {
2862 if (last == 0)
2863 break;
2864 last = 0;
2865 continue;
2866 }
2867 err = cache_save_setup(cache, trans, path);
2868 last = cache->key.objectid + cache->key.offset;
2869 btrfs_put_block_group(cache);
2870 }
2871
Chris Masond3977122009-01-05 21:25:51 -05002872 while (1) {
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002873 if (last == 0) {
2874 err = btrfs_run_delayed_refs(trans, root,
2875 (unsigned long)-1);
2876 BUG_ON(err);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002877 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002878
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002879 cache = btrfs_lookup_first_block_group(root->fs_info, last);
2880 while (cache) {
Josef Bacik0af3d002010-06-21 14:48:16 -04002881 if (cache->disk_cache_state == BTRFS_DC_CLEAR) {
2882 btrfs_put_block_group(cache);
2883 goto again;
2884 }
2885
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002886 if (cache->dirty)
2887 break;
2888 cache = next_block_group(root, cache);
2889 }
2890 if (!cache) {
2891 if (last == 0)
2892 break;
2893 last = 0;
Chris Mason96b51792007-10-15 16:15:19 -04002894 continue;
Chris Mason9078a3e2007-04-26 16:46:15 -04002895 }
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002896
Josef Bacik0cb59c92010-07-02 12:14:14 -04002897 if (cache->disk_cache_state == BTRFS_DC_SETUP)
2898 cache->disk_cache_state = BTRFS_DC_NEED_WRITE;
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002899 cache->dirty = 0;
2900 last = cache->key.objectid + cache->key.offset;
2901
2902 err = write_one_cache_group(trans, root, path, cache);
2903 BUG_ON(err);
2904 btrfs_put_block_group(cache);
Chris Mason9078a3e2007-04-26 16:46:15 -04002905 }
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002906
Josef Bacik0cb59c92010-07-02 12:14:14 -04002907 while (1) {
2908 /*
2909 * I don't think this is needed since we're just marking our
2910 * preallocated extent as written, but just in case it can't
2911 * hurt.
2912 */
2913 if (last == 0) {
2914 err = btrfs_run_delayed_refs(trans, root,
2915 (unsigned long)-1);
2916 BUG_ON(err);
2917 }
2918
2919 cache = btrfs_lookup_first_block_group(root->fs_info, last);
2920 while (cache) {
2921 /*
2922 * Really this shouldn't happen, but it could if we
2923 * couldn't write the entire preallocated extent and
2924 * splitting the extent resulted in a new block.
2925 */
2926 if (cache->dirty) {
2927 btrfs_put_block_group(cache);
2928 goto again;
2929 }
2930 if (cache->disk_cache_state == BTRFS_DC_NEED_WRITE)
2931 break;
2932 cache = next_block_group(root, cache);
2933 }
2934 if (!cache) {
2935 if (last == 0)
2936 break;
2937 last = 0;
2938 continue;
2939 }
2940
2941 btrfs_write_out_cache(root, trans, cache, path);
2942
2943 /*
2944 * If we didn't have an error then the cache state is still
2945 * NEED_WRITE, so we can set it to WRITTEN.
2946 */
2947 if (cache->disk_cache_state == BTRFS_DC_NEED_WRITE)
2948 cache->disk_cache_state = BTRFS_DC_WRITTEN;
2949 last = cache->key.objectid + cache->key.offset;
2950 btrfs_put_block_group(cache);
2951 }
2952
Chris Mason9078a3e2007-04-26 16:46:15 -04002953 btrfs_free_path(path);
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002954 return 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04002955}
2956
Yan Zhengd2fb3432008-12-11 16:30:39 -05002957int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr)
2958{
2959 struct btrfs_block_group_cache *block_group;
2960 int readonly = 0;
2961
2962 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
2963 if (!block_group || block_group->ro)
2964 readonly = 1;
2965 if (block_group)
Chris Masonfa9c0d792009-04-03 09:47:43 -04002966 btrfs_put_block_group(block_group);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002967 return readonly;
2968}
2969
Chris Mason593060d2008-03-25 16:50:33 -04002970static int update_space_info(struct btrfs_fs_info *info, u64 flags,
2971 u64 total_bytes, u64 bytes_used,
2972 struct btrfs_space_info **space_info)
2973{
2974 struct btrfs_space_info *found;
Yan, Zhengb742bb822010-05-16 10:46:24 -04002975 int i;
2976 int factor;
2977
2978 if (flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
2979 BTRFS_BLOCK_GROUP_RAID10))
2980 factor = 2;
2981 else
2982 factor = 1;
Chris Mason593060d2008-03-25 16:50:33 -04002983
2984 found = __find_space_info(info, flags);
2985 if (found) {
Josef Bacik25179202008-10-29 14:49:05 -04002986 spin_lock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04002987 found->total_bytes += total_bytes;
Josef Bacik89a55892010-10-14 14:52:27 -04002988 found->disk_total += total_bytes * factor;
Chris Mason593060d2008-03-25 16:50:33 -04002989 found->bytes_used += bytes_used;
Yan, Zhengb742bb822010-05-16 10:46:24 -04002990 found->disk_used += bytes_used * factor;
Chris Mason8f18cf12008-04-25 16:53:30 -04002991 found->full = 0;
Josef Bacik25179202008-10-29 14:49:05 -04002992 spin_unlock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04002993 *space_info = found;
2994 return 0;
2995 }
Yan Zhengc146afa2008-11-12 14:34:12 -05002996 found = kzalloc(sizeof(*found), GFP_NOFS);
Chris Mason593060d2008-03-25 16:50:33 -04002997 if (!found)
2998 return -ENOMEM;
2999
Yan, Zhengb742bb822010-05-16 10:46:24 -04003000 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++)
3001 INIT_LIST_HEAD(&found->block_groups[i]);
Josef Bacik80eb2342008-10-29 14:49:05 -04003002 init_rwsem(&found->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003003 spin_lock_init(&found->lock);
Yan, Zhengb742bb822010-05-16 10:46:24 -04003004 found->flags = flags & (BTRFS_BLOCK_GROUP_DATA |
3005 BTRFS_BLOCK_GROUP_SYSTEM |
3006 BTRFS_BLOCK_GROUP_METADATA);
Chris Mason593060d2008-03-25 16:50:33 -04003007 found->total_bytes = total_bytes;
Josef Bacik89a55892010-10-14 14:52:27 -04003008 found->disk_total = total_bytes * factor;
Chris Mason593060d2008-03-25 16:50:33 -04003009 found->bytes_used = bytes_used;
Yan, Zhengb742bb822010-05-16 10:46:24 -04003010 found->disk_used = bytes_used * factor;
Chris Mason593060d2008-03-25 16:50:33 -04003011 found->bytes_pinned = 0;
Zheng Yane8569812008-09-26 10:05:48 -04003012 found->bytes_reserved = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05003013 found->bytes_readonly = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003014 found->bytes_may_use = 0;
Chris Mason593060d2008-03-25 16:50:33 -04003015 found->full = 0;
Chris Mason0ef3e662008-05-24 14:04:53 -04003016 found->force_alloc = 0;
Chris Mason593060d2008-03-25 16:50:33 -04003017 *space_info = found;
Chris Mason4184ea72009-03-10 12:39:20 -04003018 list_add_rcu(&found->list, &info->space_info);
Josef Bacik817d52f2009-07-13 21:29:25 -04003019 atomic_set(&found->caching_threads, 0);
Chris Mason593060d2008-03-25 16:50:33 -04003020 return 0;
3021}
3022
Chris Mason8790d502008-04-03 16:29:03 -04003023static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
3024{
3025 u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
Chris Mason611f0e02008-04-03 16:29:03 -04003026 BTRFS_BLOCK_GROUP_RAID1 |
Chris Mason321aecc2008-04-16 10:49:51 -04003027 BTRFS_BLOCK_GROUP_RAID10 |
Chris Mason611f0e02008-04-03 16:29:03 -04003028 BTRFS_BLOCK_GROUP_DUP);
Chris Mason8790d502008-04-03 16:29:03 -04003029 if (extra_flags) {
3030 if (flags & BTRFS_BLOCK_GROUP_DATA)
3031 fs_info->avail_data_alloc_bits |= extra_flags;
3032 if (flags & BTRFS_BLOCK_GROUP_METADATA)
3033 fs_info->avail_metadata_alloc_bits |= extra_flags;
3034 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
3035 fs_info->avail_system_alloc_bits |= extra_flags;
3036 }
3037}
Chris Mason593060d2008-03-25 16:50:33 -04003038
Yan Zheng2b820322008-11-17 21:11:30 -05003039u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
Chris Masonec44a352008-04-28 15:29:52 -04003040{
Chris Masoncd02dca2010-12-13 14:56:23 -05003041 /*
3042 * we add in the count of missing devices because we want
3043 * to make sure that any RAID levels on a degraded FS
3044 * continue to be honored.
3045 */
3046 u64 num_devices = root->fs_info->fs_devices->rw_devices +
3047 root->fs_info->fs_devices->missing_devices;
Chris Masona061fc82008-05-07 11:43:44 -04003048
3049 if (num_devices == 1)
3050 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
3051 if (num_devices < 4)
3052 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
3053
Chris Masonec44a352008-04-28 15:29:52 -04003054 if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
3055 (flags & (BTRFS_BLOCK_GROUP_RAID1 |
Chris Masona061fc82008-05-07 11:43:44 -04003056 BTRFS_BLOCK_GROUP_RAID10))) {
Chris Masonec44a352008-04-28 15:29:52 -04003057 flags &= ~BTRFS_BLOCK_GROUP_DUP;
Chris Masona061fc82008-05-07 11:43:44 -04003058 }
Chris Masonec44a352008-04-28 15:29:52 -04003059
3060 if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
Chris Masona061fc82008-05-07 11:43:44 -04003061 (flags & BTRFS_BLOCK_GROUP_RAID10)) {
Chris Masonec44a352008-04-28 15:29:52 -04003062 flags &= ~BTRFS_BLOCK_GROUP_RAID1;
Chris Masona061fc82008-05-07 11:43:44 -04003063 }
Chris Masonec44a352008-04-28 15:29:52 -04003064
3065 if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
3066 ((flags & BTRFS_BLOCK_GROUP_RAID1) |
3067 (flags & BTRFS_BLOCK_GROUP_RAID10) |
3068 (flags & BTRFS_BLOCK_GROUP_DUP)))
3069 flags &= ~BTRFS_BLOCK_GROUP_RAID0;
3070 return flags;
3071}
3072
Yan, Zhengb742bb822010-05-16 10:46:24 -04003073static u64 get_alloc_profile(struct btrfs_root *root, u64 flags)
Josef Bacik6a632092009-02-20 11:00:09 -05003074{
Yan, Zhengb742bb822010-05-16 10:46:24 -04003075 if (flags & BTRFS_BLOCK_GROUP_DATA)
3076 flags |= root->fs_info->avail_data_alloc_bits &
3077 root->fs_info->data_alloc_profile;
3078 else if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
3079 flags |= root->fs_info->avail_system_alloc_bits &
3080 root->fs_info->system_alloc_profile;
3081 else if (flags & BTRFS_BLOCK_GROUP_METADATA)
3082 flags |= root->fs_info->avail_metadata_alloc_bits &
3083 root->fs_info->metadata_alloc_profile;
3084 return btrfs_reduce_alloc_profile(root, flags);
3085}
Josef Bacik6a632092009-02-20 11:00:09 -05003086
Miao Xie6d07bce2011-01-05 10:07:31 +00003087u64 btrfs_get_alloc_profile(struct btrfs_root *root, int data)
Yan, Zhengb742bb822010-05-16 10:46:24 -04003088{
3089 u64 flags;
Josef Bacik6a632092009-02-20 11:00:09 -05003090
Yan, Zhengb742bb822010-05-16 10:46:24 -04003091 if (data)
3092 flags = BTRFS_BLOCK_GROUP_DATA;
3093 else if (root == root->fs_info->chunk_root)
3094 flags = BTRFS_BLOCK_GROUP_SYSTEM;
3095 else
3096 flags = BTRFS_BLOCK_GROUP_METADATA;
3097
3098 return get_alloc_profile(root, flags);
Josef Bacik6a632092009-02-20 11:00:09 -05003099}
3100
3101void btrfs_set_inode_space_info(struct btrfs_root *root, struct inode *inode)
3102{
Josef Bacik6a632092009-02-20 11:00:09 -05003103 BTRFS_I(inode)->space_info = __find_space_info(root->fs_info,
Yan, Zhengf0486c62010-05-16 10:46:25 -04003104 BTRFS_BLOCK_GROUP_DATA);
Josef Bacik6a632092009-02-20 11:00:09 -05003105}
3106
3107/*
3108 * This will check the space that the inode allocates from to make sure we have
3109 * enough space for bytes.
3110 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003111int btrfs_check_data_free_space(struct inode *inode, u64 bytes)
Josef Bacik6a632092009-02-20 11:00:09 -05003112{
3113 struct btrfs_space_info *data_sinfo;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003114 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikab6e24102010-03-19 14:38:13 +00003115 u64 used;
Josef Bacik0af3d002010-06-21 14:48:16 -04003116 int ret = 0, committed = 0, alloc_chunk = 1;
Josef Bacik6a632092009-02-20 11:00:09 -05003117
3118 /* make sure bytes are sectorsize aligned */
3119 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
3120
Josef Bacik0af3d002010-06-21 14:48:16 -04003121 if (root == root->fs_info->tree_root) {
3122 alloc_chunk = 0;
3123 committed = 1;
3124 }
3125
Josef Bacik6a632092009-02-20 11:00:09 -05003126 data_sinfo = BTRFS_I(inode)->space_info;
Chris Mason33b4d472009-09-22 14:45:50 -04003127 if (!data_sinfo)
3128 goto alloc;
3129
Josef Bacik6a632092009-02-20 11:00:09 -05003130again:
3131 /* make sure we have enough space to handle the data first */
3132 spin_lock(&data_sinfo->lock);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003133 used = data_sinfo->bytes_used + data_sinfo->bytes_reserved +
3134 data_sinfo->bytes_pinned + data_sinfo->bytes_readonly +
3135 data_sinfo->bytes_may_use;
Josef Bacikab6e24102010-03-19 14:38:13 +00003136
3137 if (used + bytes > data_sinfo->total_bytes) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003138 struct btrfs_trans_handle *trans;
3139
Josef Bacik6a632092009-02-20 11:00:09 -05003140 /*
3141 * if we don't have enough free bytes in this space then we need
3142 * to alloc a new chunk.
3143 */
Josef Bacik0af3d002010-06-21 14:48:16 -04003144 if (!data_sinfo->full && alloc_chunk) {
Josef Bacik6a632092009-02-20 11:00:09 -05003145 u64 alloc_target;
Josef Bacik6a632092009-02-20 11:00:09 -05003146
3147 data_sinfo->force_alloc = 1;
3148 spin_unlock(&data_sinfo->lock);
Chris Mason33b4d472009-09-22 14:45:50 -04003149alloc:
Josef Bacik6a632092009-02-20 11:00:09 -05003150 alloc_target = btrfs_get_alloc_profile(root, 1);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003151 trans = btrfs_join_transaction(root, 1);
3152 if (IS_ERR(trans))
3153 return PTR_ERR(trans);
Josef Bacik6a632092009-02-20 11:00:09 -05003154
3155 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
3156 bytes + 2 * 1024 * 1024,
3157 alloc_target, 0);
3158 btrfs_end_transaction(trans, root);
Miao Xied52a5b52011-01-05 10:07:18 +00003159 if (ret < 0) {
3160 if (ret != -ENOSPC)
3161 return ret;
3162 else
3163 goto commit_trans;
3164 }
Chris Mason33b4d472009-09-22 14:45:50 -04003165
3166 if (!data_sinfo) {
3167 btrfs_set_inode_space_info(root, inode);
3168 data_sinfo = BTRFS_I(inode)->space_info;
3169 }
Josef Bacik6a632092009-02-20 11:00:09 -05003170 goto again;
3171 }
3172 spin_unlock(&data_sinfo->lock);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003173
3174 /* commit the current transaction and try again */
Miao Xied52a5b52011-01-05 10:07:18 +00003175commit_trans:
Sage Weildd7e0b72009-09-29 18:38:44 -04003176 if (!committed && !root->fs_info->open_ioctl_trans) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003177 committed = 1;
3178 trans = btrfs_join_transaction(root, 1);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003179 if (IS_ERR(trans))
3180 return PTR_ERR(trans);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003181 ret = btrfs_commit_transaction(trans, root);
3182 if (ret)
3183 return ret;
3184 goto again;
3185 }
3186
Chris Mason933b5852010-05-26 11:31:00 -04003187#if 0 /* I hope we never need this code again, just in case */
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003188 printk(KERN_ERR "no space left, need %llu, %llu bytes_used, "
3189 "%llu bytes_reserved, " "%llu bytes_pinned, "
3190 "%llu bytes_readonly, %llu may use %llu total\n",
3191 (unsigned long long)bytes,
Joel Becker21380932009-04-21 12:38:29 -07003192 (unsigned long long)data_sinfo->bytes_used,
3193 (unsigned long long)data_sinfo->bytes_reserved,
3194 (unsigned long long)data_sinfo->bytes_pinned,
3195 (unsigned long long)data_sinfo->bytes_readonly,
3196 (unsigned long long)data_sinfo->bytes_may_use,
3197 (unsigned long long)data_sinfo->total_bytes);
Chris Mason933b5852010-05-26 11:31:00 -04003198#endif
Josef Bacik6a632092009-02-20 11:00:09 -05003199 return -ENOSPC;
3200 }
3201 data_sinfo->bytes_may_use += bytes;
3202 BTRFS_I(inode)->reserved_bytes += bytes;
3203 spin_unlock(&data_sinfo->lock);
3204
Josef Bacik9ed74f22009-09-11 16:12:44 -04003205 return 0;
Josef Bacik6a632092009-02-20 11:00:09 -05003206}
3207
3208/*
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003209 * called when we are clearing an delalloc extent from the
3210 * inode's io_tree or there was an error for whatever reason
3211 * after calling btrfs_check_data_free_space
Josef Bacik6a632092009-02-20 11:00:09 -05003212 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003213void btrfs_free_reserved_data_space(struct inode *inode, u64 bytes)
Josef Bacik6a632092009-02-20 11:00:09 -05003214{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003215 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik6a632092009-02-20 11:00:09 -05003216 struct btrfs_space_info *data_sinfo;
3217
3218 /* make sure bytes are sectorsize aligned */
3219 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
3220
3221 data_sinfo = BTRFS_I(inode)->space_info;
3222 spin_lock(&data_sinfo->lock);
3223 data_sinfo->bytes_may_use -= bytes;
3224 BTRFS_I(inode)->reserved_bytes -= bytes;
3225 spin_unlock(&data_sinfo->lock);
3226}
3227
Josef Bacik97e728d2009-04-21 17:40:57 -04003228static void force_metadata_allocation(struct btrfs_fs_info *info)
3229{
3230 struct list_head *head = &info->space_info;
3231 struct btrfs_space_info *found;
3232
3233 rcu_read_lock();
3234 list_for_each_entry_rcu(found, head, list) {
3235 if (found->flags & BTRFS_BLOCK_GROUP_METADATA)
3236 found->force_alloc = 1;
3237 }
3238 rcu_read_unlock();
3239}
3240
Chris Masone5bc2452010-10-26 13:37:56 -04003241static int should_alloc_chunk(struct btrfs_root *root,
3242 struct btrfs_space_info *sinfo, u64 alloc_bytes)
Yan, Zheng424499d2010-05-16 10:46:25 -04003243{
3244 u64 num_bytes = sinfo->total_bytes - sinfo->bytes_readonly;
Chris Masone5bc2452010-10-26 13:37:56 -04003245 u64 thresh;
Yan, Zheng424499d2010-05-16 10:46:25 -04003246
3247 if (sinfo->bytes_used + sinfo->bytes_reserved +
3248 alloc_bytes + 256 * 1024 * 1024 < num_bytes)
3249 return 0;
3250
3251 if (sinfo->bytes_used + sinfo->bytes_reserved +
3252 alloc_bytes < div_factor(num_bytes, 8))
3253 return 0;
3254
Chris Masone5bc2452010-10-26 13:37:56 -04003255 thresh = btrfs_super_total_bytes(&root->fs_info->super_copy);
3256 thresh = max_t(u64, 256 * 1024 * 1024, div_factor_fine(thresh, 5));
3257
3258 if (num_bytes > thresh && sinfo->bytes_used < div_factor(num_bytes, 3))
Josef Bacik14ed0ca2010-10-15 15:23:48 -04003259 return 0;
3260
Yan, Zheng424499d2010-05-16 10:46:25 -04003261 return 1;
3262}
3263
Chris Mason6324fbf2008-03-24 15:01:59 -04003264static int do_chunk_alloc(struct btrfs_trans_handle *trans,
3265 struct btrfs_root *extent_root, u64 alloc_bytes,
Chris Mason0ef3e662008-05-24 14:04:53 -04003266 u64 flags, int force)
Chris Mason6324fbf2008-03-24 15:01:59 -04003267{
3268 struct btrfs_space_info *space_info;
Josef Bacik97e728d2009-04-21 17:40:57 -04003269 struct btrfs_fs_info *fs_info = extent_root->fs_info;
Yan Zhengc146afa2008-11-12 14:34:12 -05003270 int ret = 0;
3271
Josef Bacik97e728d2009-04-21 17:40:57 -04003272 mutex_lock(&fs_info->chunk_mutex);
Chris Mason6324fbf2008-03-24 15:01:59 -04003273
Yan Zheng2b820322008-11-17 21:11:30 -05003274 flags = btrfs_reduce_alloc_profile(extent_root, flags);
Chris Masonec44a352008-04-28 15:29:52 -04003275
Chris Mason6324fbf2008-03-24 15:01:59 -04003276 space_info = __find_space_info(extent_root->fs_info, flags);
Chris Mason593060d2008-03-25 16:50:33 -04003277 if (!space_info) {
3278 ret = update_space_info(extent_root->fs_info, flags,
3279 0, 0, &space_info);
3280 BUG_ON(ret);
3281 }
Chris Mason6324fbf2008-03-24 15:01:59 -04003282 BUG_ON(!space_info);
3283
Josef Bacik25179202008-10-29 14:49:05 -04003284 spin_lock(&space_info->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003285 if (space_info->force_alloc)
Chris Mason0ef3e662008-05-24 14:04:53 -04003286 force = 1;
Josef Bacik25179202008-10-29 14:49:05 -04003287 if (space_info->full) {
3288 spin_unlock(&space_info->lock);
Chris Mason925baed2008-06-25 16:01:30 -04003289 goto out;
Josef Bacik25179202008-10-29 14:49:05 -04003290 }
Chris Mason6324fbf2008-03-24 15:01:59 -04003291
Chris Masone5bc2452010-10-26 13:37:56 -04003292 if (!force && !should_alloc_chunk(extent_root, space_info,
3293 alloc_bytes)) {
Josef Bacik25179202008-10-29 14:49:05 -04003294 spin_unlock(&space_info->lock);
Chris Mason925baed2008-06-25 16:01:30 -04003295 goto out;
Josef Bacik25179202008-10-29 14:49:05 -04003296 }
Josef Bacik25179202008-10-29 14:49:05 -04003297 spin_unlock(&space_info->lock);
3298
Josef Bacik97e728d2009-04-21 17:40:57 -04003299 /*
Josef Bacik67377732010-09-16 16:19:09 -04003300 * If we have mixed data/metadata chunks we want to make sure we keep
3301 * allocating mixed chunks instead of individual chunks.
3302 */
3303 if (btrfs_mixed_space_info(space_info))
3304 flags |= (BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA);
3305
3306 /*
Josef Bacik97e728d2009-04-21 17:40:57 -04003307 * if we're doing a data chunk, go ahead and make sure that
3308 * we keep a reasonable number of metadata chunks allocated in the
3309 * FS as well.
3310 */
Josef Bacik9ed74f22009-09-11 16:12:44 -04003311 if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) {
Josef Bacik97e728d2009-04-21 17:40:57 -04003312 fs_info->data_chunk_allocations++;
3313 if (!(fs_info->data_chunk_allocations %
3314 fs_info->metadata_ratio))
3315 force_metadata_allocation(fs_info);
3316 }
3317
Yan Zheng2b820322008-11-17 21:11:30 -05003318 ret = btrfs_alloc_chunk(trans, extent_root, flags);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003319 spin_lock(&space_info->lock);
Chris Masond3977122009-01-05 21:25:51 -05003320 if (ret)
Chris Mason6324fbf2008-03-24 15:01:59 -04003321 space_info->full = 1;
Yan, Zheng424499d2010-05-16 10:46:25 -04003322 else
3323 ret = 1;
Josef Bacik9ed74f22009-09-11 16:12:44 -04003324 space_info->force_alloc = 0;
3325 spin_unlock(&space_info->lock);
Chris Masona74a4b92008-06-25 16:01:31 -04003326out:
Yan Zhengc146afa2008-11-12 14:34:12 -05003327 mutex_unlock(&extent_root->fs_info->chunk_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003328 return ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04003329}
3330
Yan, Zheng5da9d012010-05-16 10:46:25 -04003331/*
3332 * shrink metadata reservation for delalloc
3333 */
3334static int shrink_delalloc(struct btrfs_trans_handle *trans,
Josef Bacik0019f102010-10-15 15:18:40 -04003335 struct btrfs_root *root, u64 to_reclaim, int sync)
Yan, Zheng5da9d012010-05-16 10:46:25 -04003336{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003337 struct btrfs_block_rsv *block_rsv;
Josef Bacik0019f102010-10-15 15:18:40 -04003338 struct btrfs_space_info *space_info;
Yan, Zheng5da9d012010-05-16 10:46:25 -04003339 u64 reserved;
3340 u64 max_reclaim;
3341 u64 reclaimed = 0;
Josef Bacikb1953bc2011-01-21 21:10:01 +00003342 long time_left;
Chris Masonbf9022e2010-10-26 13:40:45 -04003343 int nr_pages = (2 * 1024 * 1024) >> PAGE_CACHE_SHIFT;
Josef Bacikb1953bc2011-01-21 21:10:01 +00003344 int loops = 0;
Chris Mason36e39c42011-03-12 07:08:42 -05003345 unsigned long progress;
Yan, Zheng5da9d012010-05-16 10:46:25 -04003346
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003347 block_rsv = &root->fs_info->delalloc_block_rsv;
Josef Bacik0019f102010-10-15 15:18:40 -04003348 space_info = block_rsv->space_info;
Chris Masonbf9022e2010-10-26 13:40:45 -04003349
3350 smp_mb();
Josef Bacik0019f102010-10-15 15:18:40 -04003351 reserved = space_info->bytes_reserved;
Chris Mason36e39c42011-03-12 07:08:42 -05003352 progress = space_info->reservation_progress;
Yan, Zheng5da9d012010-05-16 10:46:25 -04003353
3354 if (reserved == 0)
3355 return 0;
3356
3357 max_reclaim = min(reserved, to_reclaim);
3358
Josef Bacikb1953bc2011-01-21 21:10:01 +00003359 while (loops < 1024) {
Chris Masonbf9022e2010-10-26 13:40:45 -04003360 /* have the flusher threads jump in and do some IO */
3361 smp_mb();
3362 nr_pages = min_t(unsigned long, nr_pages,
3363 root->fs_info->delalloc_bytes >> PAGE_CACHE_SHIFT);
3364 writeback_inodes_sb_nr_if_idle(root->fs_info->sb, nr_pages);
Yan, Zheng5da9d012010-05-16 10:46:25 -04003365
Josef Bacik0019f102010-10-15 15:18:40 -04003366 spin_lock(&space_info->lock);
Chris Mason36e39c42011-03-12 07:08:42 -05003367 if (reserved > space_info->bytes_reserved)
Josef Bacik0019f102010-10-15 15:18:40 -04003368 reclaimed += reserved - space_info->bytes_reserved;
3369 reserved = space_info->bytes_reserved;
3370 spin_unlock(&space_info->lock);
Yan, Zheng5da9d012010-05-16 10:46:25 -04003371
Chris Mason36e39c42011-03-12 07:08:42 -05003372 loops++;
3373
Yan, Zheng5da9d012010-05-16 10:46:25 -04003374 if (reserved == 0 || reclaimed >= max_reclaim)
3375 break;
3376
3377 if (trans && trans->transaction->blocked)
3378 return -EAGAIN;
Chris Masonbf9022e2010-10-26 13:40:45 -04003379
Chris Mason36e39c42011-03-12 07:08:42 -05003380 time_left = schedule_timeout_interruptible(1);
Josef Bacikb1953bc2011-01-21 21:10:01 +00003381
3382 /* We were interrupted, exit */
3383 if (time_left)
3384 break;
3385
Chris Mason36e39c42011-03-12 07:08:42 -05003386 /* we've kicked the IO a few times, if anything has been freed,
3387 * exit. There is no sense in looping here for a long time
3388 * when we really need to commit the transaction, or there are
3389 * just too many writers without enough free space
3390 */
3391
3392 if (loops > 3) {
3393 smp_mb();
3394 if (progress != space_info->reservation_progress)
3395 break;
3396 }
Chris Masonbf9022e2010-10-26 13:40:45 -04003397
Yan, Zheng5da9d012010-05-16 10:46:25 -04003398 }
3399 return reclaimed >= to_reclaim;
3400}
3401
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003402/*
3403 * Retries tells us how many times we've called reserve_metadata_bytes. The
3404 * idea is if this is the first call (retries == 0) then we will add to our
3405 * reserved count if we can't make the allocation in order to hold our place
3406 * while we go and try and free up space. That way for retries > 1 we don't try
3407 * and add space, we just check to see if the amount of unused space is >= the
3408 * total space, meaning that our reservation is valid.
3409 *
3410 * However if we don't intend to retry this reservation, pass -1 as retries so
3411 * that it short circuits this logic.
3412 */
3413static int reserve_metadata_bytes(struct btrfs_trans_handle *trans,
3414 struct btrfs_root *root,
3415 struct btrfs_block_rsv *block_rsv,
3416 u64 orig_bytes, int flush)
Yan, Zhengf0486c62010-05-16 10:46:25 -04003417{
3418 struct btrfs_space_info *space_info = block_rsv->space_info;
3419 u64 unused;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003420 u64 num_bytes = orig_bytes;
3421 int retries = 0;
3422 int ret = 0;
3423 bool reserved = false;
Josef Bacik38227932010-10-26 12:52:53 -04003424 bool committed = false;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003425
3426again:
3427 ret = -ENOSPC;
3428 if (reserved)
3429 num_bytes = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003430
3431 spin_lock(&space_info->lock);
3432 unused = space_info->bytes_used + space_info->bytes_reserved +
Josef Bacik6d487552010-10-15 15:13:32 -04003433 space_info->bytes_pinned + space_info->bytes_readonly +
3434 space_info->bytes_may_use;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003435
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003436 /*
3437 * The idea here is that we've not already over-reserved the block group
3438 * then we can go ahead and save our reservation first and then start
3439 * flushing if we need to. Otherwise if we've already overcommitted
3440 * lets start flushing stuff first and then come back and try to make
3441 * our reservation.
3442 */
3443 if (unused <= space_info->total_bytes) {
Arne Jansen6f334342010-11-12 23:17:56 +00003444 unused = space_info->total_bytes - unused;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003445 if (unused >= num_bytes) {
3446 if (!reserved)
3447 space_info->bytes_reserved += orig_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003448 ret = 0;
3449 } else {
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003450 /*
3451 * Ok set num_bytes to orig_bytes since we aren't
3452 * overocmmitted, this way we only try and reclaim what
3453 * we need.
3454 */
3455 num_bytes = orig_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003456 }
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003457 } else {
3458 /*
3459 * Ok we're over committed, set num_bytes to the overcommitted
3460 * amount plus the amount of bytes that we need for this
3461 * reservation.
3462 */
3463 num_bytes = unused - space_info->total_bytes +
3464 (orig_bytes * (retries + 1));
Yan, Zhengf0486c62010-05-16 10:46:25 -04003465 }
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003466
3467 /*
3468 * Couldn't make our reservation, save our place so while we're trying
3469 * to reclaim space we can actually use it instead of somebody else
3470 * stealing it from us.
3471 */
3472 if (ret && !reserved) {
3473 space_info->bytes_reserved += orig_bytes;
3474 reserved = true;
3475 }
3476
Yan, Zhengf0486c62010-05-16 10:46:25 -04003477 spin_unlock(&space_info->lock);
3478
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003479 if (!ret)
3480 return 0;
3481
3482 if (!flush)
3483 goto out;
3484
3485 /*
3486 * We do synchronous shrinking since we don't actually unreserve
3487 * metadata until after the IO is completed.
3488 */
3489 ret = shrink_delalloc(trans, root, num_bytes, 1);
3490 if (ret > 0)
3491 return 0;
3492 else if (ret < 0)
3493 goto out;
3494
3495 /*
3496 * So if we were overcommitted it's possible that somebody else flushed
3497 * out enough space and we simply didn't have enough space to reclaim,
3498 * so go back around and try again.
3499 */
3500 if (retries < 2) {
3501 retries++;
3502 goto again;
3503 }
3504
3505 spin_lock(&space_info->lock);
3506 /*
3507 * Not enough space to be reclaimed, don't bother committing the
3508 * transaction.
3509 */
3510 if (space_info->bytes_pinned < orig_bytes)
3511 ret = -ENOSPC;
3512 spin_unlock(&space_info->lock);
3513 if (ret)
3514 goto out;
3515
3516 ret = -EAGAIN;
Josef Bacik38227932010-10-26 12:52:53 -04003517 if (trans || committed)
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003518 goto out;
3519
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003520 ret = -ENOSPC;
3521 trans = btrfs_join_transaction(root, 1);
3522 if (IS_ERR(trans))
3523 goto out;
3524 ret = btrfs_commit_transaction(trans, root);
Josef Bacik38227932010-10-26 12:52:53 -04003525 if (!ret) {
3526 trans = NULL;
3527 committed = true;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003528 goto again;
Josef Bacik38227932010-10-26 12:52:53 -04003529 }
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003530
3531out:
3532 if (reserved) {
3533 spin_lock(&space_info->lock);
3534 space_info->bytes_reserved -= orig_bytes;
3535 spin_unlock(&space_info->lock);
3536 }
3537
Yan, Zhengf0486c62010-05-16 10:46:25 -04003538 return ret;
3539}
3540
3541static struct btrfs_block_rsv *get_block_rsv(struct btrfs_trans_handle *trans,
3542 struct btrfs_root *root)
3543{
3544 struct btrfs_block_rsv *block_rsv;
3545 if (root->ref_cows)
3546 block_rsv = trans->block_rsv;
3547 else
3548 block_rsv = root->block_rsv;
3549
3550 if (!block_rsv)
3551 block_rsv = &root->fs_info->empty_block_rsv;
3552
3553 return block_rsv;
3554}
3555
3556static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv,
3557 u64 num_bytes)
3558{
3559 int ret = -ENOSPC;
3560 spin_lock(&block_rsv->lock);
3561 if (block_rsv->reserved >= num_bytes) {
3562 block_rsv->reserved -= num_bytes;
3563 if (block_rsv->reserved < block_rsv->size)
3564 block_rsv->full = 0;
3565 ret = 0;
3566 }
3567 spin_unlock(&block_rsv->lock);
3568 return ret;
3569}
3570
3571static void block_rsv_add_bytes(struct btrfs_block_rsv *block_rsv,
3572 u64 num_bytes, int update_size)
3573{
3574 spin_lock(&block_rsv->lock);
3575 block_rsv->reserved += num_bytes;
3576 if (update_size)
3577 block_rsv->size += num_bytes;
3578 else if (block_rsv->reserved >= block_rsv->size)
3579 block_rsv->full = 1;
3580 spin_unlock(&block_rsv->lock);
3581}
3582
3583void block_rsv_release_bytes(struct btrfs_block_rsv *block_rsv,
3584 struct btrfs_block_rsv *dest, u64 num_bytes)
3585{
3586 struct btrfs_space_info *space_info = block_rsv->space_info;
3587
3588 spin_lock(&block_rsv->lock);
3589 if (num_bytes == (u64)-1)
3590 num_bytes = block_rsv->size;
3591 block_rsv->size -= num_bytes;
3592 if (block_rsv->reserved >= block_rsv->size) {
3593 num_bytes = block_rsv->reserved - block_rsv->size;
3594 block_rsv->reserved = block_rsv->size;
3595 block_rsv->full = 1;
3596 } else {
3597 num_bytes = 0;
3598 }
3599 spin_unlock(&block_rsv->lock);
3600
3601 if (num_bytes > 0) {
3602 if (dest) {
Josef Bacike9e22892011-01-24 21:43:19 +00003603 spin_lock(&dest->lock);
3604 if (!dest->full) {
3605 u64 bytes_to_add;
3606
3607 bytes_to_add = dest->size - dest->reserved;
3608 bytes_to_add = min(num_bytes, bytes_to_add);
3609 dest->reserved += bytes_to_add;
3610 if (dest->reserved >= dest->size)
3611 dest->full = 1;
3612 num_bytes -= bytes_to_add;
3613 }
3614 spin_unlock(&dest->lock);
3615 }
3616 if (num_bytes) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04003617 spin_lock(&space_info->lock);
3618 space_info->bytes_reserved -= num_bytes;
Chris Mason36e39c42011-03-12 07:08:42 -05003619 space_info->reservation_progress++;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003620 spin_unlock(&space_info->lock);
3621 }
3622 }
3623}
3624
3625static int block_rsv_migrate_bytes(struct btrfs_block_rsv *src,
3626 struct btrfs_block_rsv *dst, u64 num_bytes)
3627{
3628 int ret;
3629
3630 ret = block_rsv_use_bytes(src, num_bytes);
3631 if (ret)
3632 return ret;
3633
3634 block_rsv_add_bytes(dst, num_bytes, 1);
3635 return 0;
3636}
3637
3638void btrfs_init_block_rsv(struct btrfs_block_rsv *rsv)
3639{
3640 memset(rsv, 0, sizeof(*rsv));
3641 spin_lock_init(&rsv->lock);
3642 atomic_set(&rsv->usage, 1);
3643 rsv->priority = 6;
3644 INIT_LIST_HEAD(&rsv->list);
3645}
3646
3647struct btrfs_block_rsv *btrfs_alloc_block_rsv(struct btrfs_root *root)
3648{
3649 struct btrfs_block_rsv *block_rsv;
3650 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003651
3652 block_rsv = kmalloc(sizeof(*block_rsv), GFP_NOFS);
3653 if (!block_rsv)
3654 return NULL;
3655
3656 btrfs_init_block_rsv(block_rsv);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003657 block_rsv->space_info = __find_space_info(fs_info,
3658 BTRFS_BLOCK_GROUP_METADATA);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003659 return block_rsv;
3660}
3661
3662void btrfs_free_block_rsv(struct btrfs_root *root,
3663 struct btrfs_block_rsv *rsv)
3664{
3665 if (rsv && atomic_dec_and_test(&rsv->usage)) {
3666 btrfs_block_rsv_release(root, rsv, (u64)-1);
3667 if (!rsv->durable)
3668 kfree(rsv);
3669 }
3670}
3671
3672/*
3673 * make the block_rsv struct be able to capture freed space.
3674 * the captured space will re-add to the the block_rsv struct
3675 * after transaction commit
3676 */
3677void btrfs_add_durable_block_rsv(struct btrfs_fs_info *fs_info,
3678 struct btrfs_block_rsv *block_rsv)
3679{
3680 block_rsv->durable = 1;
3681 mutex_lock(&fs_info->durable_block_rsv_mutex);
3682 list_add_tail(&block_rsv->list, &fs_info->durable_block_rsv_list);
3683 mutex_unlock(&fs_info->durable_block_rsv_mutex);
3684}
3685
3686int btrfs_block_rsv_add(struct btrfs_trans_handle *trans,
3687 struct btrfs_root *root,
3688 struct btrfs_block_rsv *block_rsv,
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003689 u64 num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04003690{
3691 int ret;
3692
3693 if (num_bytes == 0)
3694 return 0;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003695
3696 ret = reserve_metadata_bytes(trans, root, block_rsv, num_bytes, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003697 if (!ret) {
3698 block_rsv_add_bytes(block_rsv, num_bytes, 1);
3699 return 0;
3700 }
3701
Yan, Zhengf0486c62010-05-16 10:46:25 -04003702 return ret;
3703}
3704
3705int btrfs_block_rsv_check(struct btrfs_trans_handle *trans,
3706 struct btrfs_root *root,
3707 struct btrfs_block_rsv *block_rsv,
3708 u64 min_reserved, int min_factor)
3709{
3710 u64 num_bytes = 0;
3711 int commit_trans = 0;
3712 int ret = -ENOSPC;
3713
3714 if (!block_rsv)
3715 return 0;
3716
3717 spin_lock(&block_rsv->lock);
3718 if (min_factor > 0)
3719 num_bytes = div_factor(block_rsv->size, min_factor);
3720 if (min_reserved > num_bytes)
3721 num_bytes = min_reserved;
3722
3723 if (block_rsv->reserved >= num_bytes) {
3724 ret = 0;
3725 } else {
3726 num_bytes -= block_rsv->reserved;
3727 if (block_rsv->durable &&
3728 block_rsv->freed[0] + block_rsv->freed[1] >= num_bytes)
3729 commit_trans = 1;
3730 }
3731 spin_unlock(&block_rsv->lock);
3732 if (!ret)
3733 return 0;
3734
3735 if (block_rsv->refill_used) {
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003736 ret = reserve_metadata_bytes(trans, root, block_rsv,
3737 num_bytes, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003738 if (!ret) {
3739 block_rsv_add_bytes(block_rsv, num_bytes, 0);
3740 return 0;
3741 }
3742 }
3743
3744 if (commit_trans) {
3745 if (trans)
3746 return -EAGAIN;
3747
3748 trans = btrfs_join_transaction(root, 1);
3749 BUG_ON(IS_ERR(trans));
3750 ret = btrfs_commit_transaction(trans, root);
3751 return 0;
3752 }
3753
Yan, Zhengf0486c62010-05-16 10:46:25 -04003754 return -ENOSPC;
3755}
3756
3757int btrfs_block_rsv_migrate(struct btrfs_block_rsv *src_rsv,
3758 struct btrfs_block_rsv *dst_rsv,
3759 u64 num_bytes)
3760{
3761 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
3762}
3763
3764void btrfs_block_rsv_release(struct btrfs_root *root,
3765 struct btrfs_block_rsv *block_rsv,
3766 u64 num_bytes)
3767{
3768 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
3769 if (global_rsv->full || global_rsv == block_rsv ||
3770 block_rsv->space_info != global_rsv->space_info)
3771 global_rsv = NULL;
3772 block_rsv_release_bytes(block_rsv, global_rsv, num_bytes);
3773}
3774
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003775/*
3776 * helper to calculate size of global block reservation.
3777 * the desired value is sum of space used by extent tree,
3778 * checksum tree and root tree
3779 */
3780static u64 calc_global_metadata_size(struct btrfs_fs_info *fs_info)
3781{
3782 struct btrfs_space_info *sinfo;
3783 u64 num_bytes;
3784 u64 meta_used;
3785 u64 data_used;
3786 int csum_size = btrfs_super_csum_size(&fs_info->super_copy);
3787#if 0
3788 /*
3789 * per tree used space accounting can be inaccuracy, so we
3790 * can't rely on it.
3791 */
3792 spin_lock(&fs_info->extent_root->accounting_lock);
3793 num_bytes = btrfs_root_used(&fs_info->extent_root->root_item);
3794 spin_unlock(&fs_info->extent_root->accounting_lock);
3795
3796 spin_lock(&fs_info->csum_root->accounting_lock);
3797 num_bytes += btrfs_root_used(&fs_info->csum_root->root_item);
3798 spin_unlock(&fs_info->csum_root->accounting_lock);
3799
3800 spin_lock(&fs_info->tree_root->accounting_lock);
3801 num_bytes += btrfs_root_used(&fs_info->tree_root->root_item);
3802 spin_unlock(&fs_info->tree_root->accounting_lock);
3803#endif
3804 sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_DATA);
3805 spin_lock(&sinfo->lock);
3806 data_used = sinfo->bytes_used;
3807 spin_unlock(&sinfo->lock);
3808
3809 sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
3810 spin_lock(&sinfo->lock);
Josef Bacik6d487552010-10-15 15:13:32 -04003811 if (sinfo->flags & BTRFS_BLOCK_GROUP_DATA)
3812 data_used = 0;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003813 meta_used = sinfo->bytes_used;
3814 spin_unlock(&sinfo->lock);
3815
3816 num_bytes = (data_used >> fs_info->sb->s_blocksize_bits) *
3817 csum_size * 2;
3818 num_bytes += div64_u64(data_used + meta_used, 50);
3819
3820 if (num_bytes * 3 > meta_used)
3821 num_bytes = div64_u64(meta_used, 3);
3822
3823 return ALIGN(num_bytes, fs_info->extent_root->leafsize << 10);
3824}
3825
3826static void update_global_block_rsv(struct btrfs_fs_info *fs_info)
3827{
3828 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
3829 struct btrfs_space_info *sinfo = block_rsv->space_info;
3830 u64 num_bytes;
3831
3832 num_bytes = calc_global_metadata_size(fs_info);
3833
3834 spin_lock(&block_rsv->lock);
3835 spin_lock(&sinfo->lock);
3836
3837 block_rsv->size = num_bytes;
3838
3839 num_bytes = sinfo->bytes_used + sinfo->bytes_pinned +
Josef Bacik6d487552010-10-15 15:13:32 -04003840 sinfo->bytes_reserved + sinfo->bytes_readonly +
3841 sinfo->bytes_may_use;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003842
3843 if (sinfo->total_bytes > num_bytes) {
3844 num_bytes = sinfo->total_bytes - num_bytes;
3845 block_rsv->reserved += num_bytes;
3846 sinfo->bytes_reserved += num_bytes;
3847 }
3848
3849 if (block_rsv->reserved >= block_rsv->size) {
3850 num_bytes = block_rsv->reserved - block_rsv->size;
3851 sinfo->bytes_reserved -= num_bytes;
Chris Mason36e39c42011-03-12 07:08:42 -05003852 sinfo->reservation_progress++;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003853 block_rsv->reserved = block_rsv->size;
3854 block_rsv->full = 1;
3855 }
3856#if 0
3857 printk(KERN_INFO"global block rsv size %llu reserved %llu\n",
3858 block_rsv->size, block_rsv->reserved);
3859#endif
3860 spin_unlock(&sinfo->lock);
3861 spin_unlock(&block_rsv->lock);
3862}
3863
Yan, Zhengf0486c62010-05-16 10:46:25 -04003864static void init_global_block_rsv(struct btrfs_fs_info *fs_info)
3865{
3866 struct btrfs_space_info *space_info;
3867
3868 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
3869 fs_info->chunk_block_rsv.space_info = space_info;
3870 fs_info->chunk_block_rsv.priority = 10;
3871
3872 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003873 fs_info->global_block_rsv.space_info = space_info;
3874 fs_info->global_block_rsv.priority = 10;
3875 fs_info->global_block_rsv.refill_used = 1;
3876 fs_info->delalloc_block_rsv.space_info = space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003877 fs_info->trans_block_rsv.space_info = space_info;
3878 fs_info->empty_block_rsv.space_info = space_info;
3879 fs_info->empty_block_rsv.priority = 10;
3880
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003881 fs_info->extent_root->block_rsv = &fs_info->global_block_rsv;
3882 fs_info->csum_root->block_rsv = &fs_info->global_block_rsv;
3883 fs_info->dev_root->block_rsv = &fs_info->global_block_rsv;
3884 fs_info->tree_root->block_rsv = &fs_info->global_block_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003885 fs_info->chunk_root->block_rsv = &fs_info->chunk_block_rsv;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003886
3887 btrfs_add_durable_block_rsv(fs_info, &fs_info->global_block_rsv);
3888
3889 btrfs_add_durable_block_rsv(fs_info, &fs_info->delalloc_block_rsv);
3890
3891 update_global_block_rsv(fs_info);
3892}
3893
3894static void release_global_block_rsv(struct btrfs_fs_info *fs_info)
3895{
3896 block_rsv_release_bytes(&fs_info->global_block_rsv, NULL, (u64)-1);
3897 WARN_ON(fs_info->delalloc_block_rsv.size > 0);
3898 WARN_ON(fs_info->delalloc_block_rsv.reserved > 0);
3899 WARN_ON(fs_info->trans_block_rsv.size > 0);
3900 WARN_ON(fs_info->trans_block_rsv.reserved > 0);
3901 WARN_ON(fs_info->chunk_block_rsv.size > 0);
3902 WARN_ON(fs_info->chunk_block_rsv.reserved > 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003903}
3904
Yan, Zhenga22285a2010-05-16 10:48:46 -04003905static u64 calc_trans_metadata_size(struct btrfs_root *root, int num_items)
3906{
3907 return (root->leafsize + root->nodesize * (BTRFS_MAX_LEVEL - 1)) *
3908 3 * num_items;
3909}
3910
3911int btrfs_trans_reserve_metadata(struct btrfs_trans_handle *trans,
3912 struct btrfs_root *root,
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003913 int num_items)
Yan, Zhenga22285a2010-05-16 10:48:46 -04003914{
3915 u64 num_bytes;
3916 int ret;
3917
3918 if (num_items == 0 || root->fs_info->chunk_root == root)
3919 return 0;
3920
3921 num_bytes = calc_trans_metadata_size(root, num_items);
3922 ret = btrfs_block_rsv_add(trans, root, &root->fs_info->trans_block_rsv,
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003923 num_bytes);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003924 if (!ret) {
3925 trans->bytes_reserved += num_bytes;
3926 trans->block_rsv = &root->fs_info->trans_block_rsv;
3927 }
3928 return ret;
3929}
3930
3931void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans,
3932 struct btrfs_root *root)
3933{
3934 if (!trans->bytes_reserved)
3935 return;
3936
3937 BUG_ON(trans->block_rsv != &root->fs_info->trans_block_rsv);
3938 btrfs_block_rsv_release(root, trans->block_rsv,
3939 trans->bytes_reserved);
3940 trans->bytes_reserved = 0;
3941}
3942
Yan, Zhengd68fc572010-05-16 10:49:58 -04003943int btrfs_orphan_reserve_metadata(struct btrfs_trans_handle *trans,
3944 struct inode *inode)
3945{
3946 struct btrfs_root *root = BTRFS_I(inode)->root;
3947 struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root);
3948 struct btrfs_block_rsv *dst_rsv = root->orphan_block_rsv;
3949
3950 /*
3951 * one for deleting orphan item, one for updating inode and
3952 * two for calling btrfs_truncate_inode_items.
3953 *
3954 * btrfs_truncate_inode_items is a delete operation, it frees
3955 * more space than it uses in most cases. So two units of
3956 * metadata space should be enough for calling it many times.
3957 * If all of the metadata space is used, we can commit
3958 * transaction and use space it freed.
3959 */
3960 u64 num_bytes = calc_trans_metadata_size(root, 4);
3961 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
3962}
3963
3964void btrfs_orphan_release_metadata(struct inode *inode)
3965{
3966 struct btrfs_root *root = BTRFS_I(inode)->root;
3967 u64 num_bytes = calc_trans_metadata_size(root, 4);
3968 btrfs_block_rsv_release(root, root->orphan_block_rsv, num_bytes);
3969}
3970
Yan, Zhenga22285a2010-05-16 10:48:46 -04003971int btrfs_snap_reserve_metadata(struct btrfs_trans_handle *trans,
3972 struct btrfs_pending_snapshot *pending)
3973{
3974 struct btrfs_root *root = pending->root;
3975 struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root);
3976 struct btrfs_block_rsv *dst_rsv = &pending->block_rsv;
3977 /*
3978 * two for root back/forward refs, two for directory entries
3979 * and one for root of the snapshot.
3980 */
3981 u64 num_bytes = calc_trans_metadata_size(root, 5);
3982 dst_rsv->space_info = src_rsv->space_info;
3983 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
3984}
3985
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003986static u64 calc_csum_metadata_size(struct inode *inode, u64 num_bytes)
3987{
3988 return num_bytes >>= 3;
3989}
3990
3991int btrfs_delalloc_reserve_metadata(struct inode *inode, u64 num_bytes)
3992{
3993 struct btrfs_root *root = BTRFS_I(inode)->root;
3994 struct btrfs_block_rsv *block_rsv = &root->fs_info->delalloc_block_rsv;
3995 u64 to_reserve;
3996 int nr_extents;
Josef Bacik57a45ced2011-01-25 16:30:38 -05003997 int reserved_extents;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003998 int ret;
3999
4000 if (btrfs_transaction_in_commit(root->fs_info))
4001 schedule_timeout(1);
4002
4003 num_bytes = ALIGN(num_bytes, root->sectorsize);
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004004
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004005 nr_extents = atomic_read(&BTRFS_I(inode)->outstanding_extents) + 1;
Josef Bacik57a45ced2011-01-25 16:30:38 -05004006 reserved_extents = atomic_read(&BTRFS_I(inode)->reserved_extents);
4007
4008 if (nr_extents > reserved_extents) {
4009 nr_extents -= reserved_extents;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004010 to_reserve = calc_trans_metadata_size(root, nr_extents);
4011 } else {
4012 nr_extents = 0;
4013 to_reserve = 0;
4014 }
Josef Bacik57a45ced2011-01-25 16:30:38 -05004015
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004016 to_reserve += calc_csum_metadata_size(inode, num_bytes);
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004017 ret = reserve_metadata_bytes(NULL, root, block_rsv, to_reserve, 1);
4018 if (ret)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004019 return ret;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004020
Josef Bacik57a45ced2011-01-25 16:30:38 -05004021 atomic_add(nr_extents, &BTRFS_I(inode)->reserved_extents);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004022 atomic_inc(&BTRFS_I(inode)->outstanding_extents);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004023
4024 block_rsv_add_bytes(block_rsv, to_reserve, 1);
4025
4026 if (block_rsv->size > 512 * 1024 * 1024)
Josef Bacik0019f102010-10-15 15:18:40 -04004027 shrink_delalloc(NULL, root, to_reserve, 0);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004028
4029 return 0;
4030}
4031
4032void btrfs_delalloc_release_metadata(struct inode *inode, u64 num_bytes)
4033{
4034 struct btrfs_root *root = BTRFS_I(inode)->root;
4035 u64 to_free;
4036 int nr_extents;
Josef Bacik57a45ced2011-01-25 16:30:38 -05004037 int reserved_extents;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004038
4039 num_bytes = ALIGN(num_bytes, root->sectorsize);
4040 atomic_dec(&BTRFS_I(inode)->outstanding_extents);
Josef Bacik3c148742011-02-02 15:53:47 +00004041 WARN_ON(atomic_read(&BTRFS_I(inode)->outstanding_extents) < 0);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004042
Josef Bacik57a45ced2011-01-25 16:30:38 -05004043 reserved_extents = atomic_read(&BTRFS_I(inode)->reserved_extents);
4044 do {
4045 int old, new;
4046
4047 nr_extents = atomic_read(&BTRFS_I(inode)->outstanding_extents);
4048 if (nr_extents >= reserved_extents) {
4049 nr_extents = 0;
4050 break;
4051 }
4052 old = reserved_extents;
4053 nr_extents = reserved_extents - nr_extents;
4054 new = reserved_extents - nr_extents;
4055 old = atomic_cmpxchg(&BTRFS_I(inode)->reserved_extents,
4056 reserved_extents, new);
4057 if (likely(old == reserved_extents))
4058 break;
4059 reserved_extents = old;
4060 } while (1);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004061
4062 to_free = calc_csum_metadata_size(inode, num_bytes);
4063 if (nr_extents > 0)
4064 to_free += calc_trans_metadata_size(root, nr_extents);
4065
4066 btrfs_block_rsv_release(root, &root->fs_info->delalloc_block_rsv,
4067 to_free);
4068}
4069
4070int btrfs_delalloc_reserve_space(struct inode *inode, u64 num_bytes)
4071{
4072 int ret;
4073
4074 ret = btrfs_check_data_free_space(inode, num_bytes);
4075 if (ret)
4076 return ret;
4077
4078 ret = btrfs_delalloc_reserve_metadata(inode, num_bytes);
4079 if (ret) {
4080 btrfs_free_reserved_data_space(inode, num_bytes);
4081 return ret;
4082 }
4083
4084 return 0;
4085}
4086
4087void btrfs_delalloc_release_space(struct inode *inode, u64 num_bytes)
4088{
4089 btrfs_delalloc_release_metadata(inode, num_bytes);
4090 btrfs_free_reserved_data_space(inode, num_bytes);
4091}
4092
Chris Mason9078a3e2007-04-26 16:46:15 -04004093static int update_block_group(struct btrfs_trans_handle *trans,
4094 struct btrfs_root *root,
Yan, Zhengf0486c62010-05-16 10:46:25 -04004095 u64 bytenr, u64 num_bytes, int alloc)
Chris Mason9078a3e2007-04-26 16:46:15 -04004096{
Josef Bacik0af3d002010-06-21 14:48:16 -04004097 struct btrfs_block_group_cache *cache = NULL;
Chris Mason9078a3e2007-04-26 16:46:15 -04004098 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -04004099 u64 total = num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04004100 u64 old_val;
Chris Masondb945352007-10-15 16:15:53 -04004101 u64 byte_in_group;
Josef Bacik0af3d002010-06-21 14:48:16 -04004102 int factor;
Chris Mason3e1ad542007-05-07 20:03:49 -04004103
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004104 /* block accounting for super block */
4105 spin_lock(&info->delalloc_lock);
4106 old_val = btrfs_super_bytes_used(&info->super_copy);
4107 if (alloc)
4108 old_val += num_bytes;
4109 else
4110 old_val -= num_bytes;
4111 btrfs_set_super_bytes_used(&info->super_copy, old_val);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004112 spin_unlock(&info->delalloc_lock);
4113
Chris Masond3977122009-01-05 21:25:51 -05004114 while (total) {
Chris Masondb945352007-10-15 16:15:53 -04004115 cache = btrfs_lookup_block_group(info, bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004116 if (!cache)
Chris Mason9078a3e2007-04-26 16:46:15 -04004117 return -1;
Yan, Zhengb742bb822010-05-16 10:46:24 -04004118 if (cache->flags & (BTRFS_BLOCK_GROUP_DUP |
4119 BTRFS_BLOCK_GROUP_RAID1 |
4120 BTRFS_BLOCK_GROUP_RAID10))
4121 factor = 2;
4122 else
4123 factor = 1;
Josef Bacik9d66e232010-08-25 16:54:15 -04004124 /*
4125 * If this block group has free space cache written out, we
4126 * need to make sure to load it if we are removing space. This
4127 * is because we need the unpinning stage to actually add the
4128 * space back to the block group, otherwise we will leak space.
4129 */
4130 if (!alloc && cache->cached == BTRFS_CACHE_NO)
Josef Bacikb8399de2010-12-08 09:15:11 -05004131 cache_block_group(cache, trans, NULL, 1);
Josef Bacik0af3d002010-06-21 14:48:16 -04004132
Chris Masondb945352007-10-15 16:15:53 -04004133 byte_in_group = bytenr - cache->key.objectid;
4134 WARN_ON(byte_in_group > cache->key.offset);
Chris Mason9078a3e2007-04-26 16:46:15 -04004135
Josef Bacik25179202008-10-29 14:49:05 -04004136 spin_lock(&cache->space_info->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04004137 spin_lock(&cache->lock);
Josef Bacik0af3d002010-06-21 14:48:16 -04004138
4139 if (btrfs_super_cache_generation(&info->super_copy) != 0 &&
4140 cache->disk_cache_state < BTRFS_DC_CLEAR)
4141 cache->disk_cache_state = BTRFS_DC_CLEAR;
4142
Josef Bacik0f9dd462008-09-23 13:14:11 -04004143 cache->dirty = 1;
Chris Mason9078a3e2007-04-26 16:46:15 -04004144 old_val = btrfs_block_group_used(&cache->item);
Chris Masondb945352007-10-15 16:15:53 -04004145 num_bytes = min(total, cache->key.offset - byte_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -04004146 if (alloc) {
Chris Masondb945352007-10-15 16:15:53 -04004147 old_val += num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04004148 btrfs_set_block_group_used(&cache->item, old_val);
4149 cache->reserved -= num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04004150 cache->space_info->bytes_reserved -= num_bytes;
Chris Mason36e39c42011-03-12 07:08:42 -05004151 cache->space_info->reservation_progress++;
Yan, Zhengb742bb822010-05-16 10:46:24 -04004152 cache->space_info->bytes_used += num_bytes;
4153 cache->space_info->disk_used += num_bytes * factor;
Chris Masonc286ac42008-07-22 23:06:41 -04004154 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04004155 spin_unlock(&cache->space_info->lock);
Chris Masoncd1bc462007-04-27 10:08:34 -04004156 } else {
Chris Masondb945352007-10-15 16:15:53 -04004157 old_val -= num_bytes;
Chris Masonc286ac42008-07-22 23:06:41 -04004158 btrfs_set_block_group_used(&cache->item, old_val);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004159 cache->pinned += num_bytes;
4160 cache->space_info->bytes_pinned += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04004161 cache->space_info->bytes_used -= num_bytes;
Yan, Zhengb742bb822010-05-16 10:46:24 -04004162 cache->space_info->disk_used -= num_bytes * factor;
Chris Masonc286ac42008-07-22 23:06:41 -04004163 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04004164 spin_unlock(&cache->space_info->lock);
Liu Hui1f3c79a2009-01-05 15:57:51 -05004165
Yan, Zhengf0486c62010-05-16 10:46:25 -04004166 set_extent_dirty(info->pinned_extents,
4167 bytenr, bytenr + num_bytes - 1,
4168 GFP_NOFS | __GFP_NOFAIL);
Chris Masoncd1bc462007-04-27 10:08:34 -04004169 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04004170 btrfs_put_block_group(cache);
Chris Masondb945352007-10-15 16:15:53 -04004171 total -= num_bytes;
4172 bytenr += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04004173 }
4174 return 0;
4175}
Chris Mason6324fbf2008-03-24 15:01:59 -04004176
Chris Masona061fc82008-05-07 11:43:44 -04004177static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
4178{
Josef Bacik0f9dd462008-09-23 13:14:11 -04004179 struct btrfs_block_group_cache *cache;
Yan Zhengd2fb3432008-12-11 16:30:39 -05004180 u64 bytenr;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004181
4182 cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
4183 if (!cache)
Chris Masona061fc82008-05-07 11:43:44 -04004184 return 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004185
Yan Zhengd2fb3432008-12-11 16:30:39 -05004186 bytenr = cache->key.objectid;
Chris Masonfa9c0d792009-04-03 09:47:43 -04004187 btrfs_put_block_group(cache);
Yan Zhengd2fb3432008-12-11 16:30:39 -05004188
4189 return bytenr;
Chris Masona061fc82008-05-07 11:43:44 -04004190}
4191
Yan, Zhengf0486c62010-05-16 10:46:25 -04004192static int pin_down_extent(struct btrfs_root *root,
4193 struct btrfs_block_group_cache *cache,
4194 u64 bytenr, u64 num_bytes, int reserved)
Yan324ae4d2007-11-16 14:57:08 -05004195{
Yan Zheng11833d62009-09-11 16:11:19 -04004196 spin_lock(&cache->space_info->lock);
4197 spin_lock(&cache->lock);
4198 cache->pinned += num_bytes;
4199 cache->space_info->bytes_pinned += num_bytes;
4200 if (reserved) {
4201 cache->reserved -= num_bytes;
4202 cache->space_info->bytes_reserved -= num_bytes;
Chris Mason36e39c42011-03-12 07:08:42 -05004203 cache->space_info->reservation_progress++;
Yan324ae4d2007-11-16 14:57:08 -05004204 }
Yan Zheng11833d62009-09-11 16:11:19 -04004205 spin_unlock(&cache->lock);
4206 spin_unlock(&cache->space_info->lock);
4207
Yan, Zhengf0486c62010-05-16 10:46:25 -04004208 set_extent_dirty(root->fs_info->pinned_extents, bytenr,
4209 bytenr + num_bytes - 1, GFP_NOFS | __GFP_NOFAIL);
Yan324ae4d2007-11-16 14:57:08 -05004210 return 0;
4211}
Chris Mason9078a3e2007-04-26 16:46:15 -04004212
Yan, Zhengf0486c62010-05-16 10:46:25 -04004213/*
4214 * this function must be called within transaction
4215 */
4216int btrfs_pin_extent(struct btrfs_root *root,
4217 u64 bytenr, u64 num_bytes, int reserved)
Zheng Yane8569812008-09-26 10:05:48 -04004218{
Yan, Zhengf0486c62010-05-16 10:46:25 -04004219 struct btrfs_block_group_cache *cache;
4220
4221 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
4222 BUG_ON(!cache);
4223
4224 pin_down_extent(root, cache, bytenr, num_bytes, reserved);
4225
4226 btrfs_put_block_group(cache);
Yan Zheng11833d62009-09-11 16:11:19 -04004227 return 0;
4228}
Zheng Yane8569812008-09-26 10:05:48 -04004229
Yan, Zhengf0486c62010-05-16 10:46:25 -04004230/*
4231 * update size of reserved extents. this function may return -EAGAIN
4232 * if 'reserve' is true or 'sinfo' is false.
4233 */
Li Dongyangb4d00d52011-03-24 10:24:25 +00004234int btrfs_update_reserved_bytes(struct btrfs_block_group_cache *cache,
4235 u64 num_bytes, int reserve, int sinfo)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004236{
4237 int ret = 0;
4238 if (sinfo) {
4239 struct btrfs_space_info *space_info = cache->space_info;
4240 spin_lock(&space_info->lock);
4241 spin_lock(&cache->lock);
4242 if (reserve) {
4243 if (cache->ro) {
4244 ret = -EAGAIN;
4245 } else {
4246 cache->reserved += num_bytes;
4247 space_info->bytes_reserved += num_bytes;
4248 }
4249 } else {
4250 if (cache->ro)
4251 space_info->bytes_readonly += num_bytes;
4252 cache->reserved -= num_bytes;
4253 space_info->bytes_reserved -= num_bytes;
Chris Mason36e39c42011-03-12 07:08:42 -05004254 space_info->reservation_progress++;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004255 }
4256 spin_unlock(&cache->lock);
4257 spin_unlock(&space_info->lock);
4258 } else {
4259 spin_lock(&cache->lock);
4260 if (cache->ro) {
4261 ret = -EAGAIN;
4262 } else {
4263 if (reserve)
4264 cache->reserved += num_bytes;
4265 else
4266 cache->reserved -= num_bytes;
4267 }
4268 spin_unlock(&cache->lock);
4269 }
4270 return ret;
4271}
4272
Yan Zheng11833d62009-09-11 16:11:19 -04004273int btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans,
4274 struct btrfs_root *root)
4275{
4276 struct btrfs_fs_info *fs_info = root->fs_info;
4277 struct btrfs_caching_control *next;
4278 struct btrfs_caching_control *caching_ctl;
4279 struct btrfs_block_group_cache *cache;
4280
4281 down_write(&fs_info->extent_commit_sem);
4282
4283 list_for_each_entry_safe(caching_ctl, next,
4284 &fs_info->caching_block_groups, list) {
4285 cache = caching_ctl->block_group;
4286 if (block_group_cache_done(cache)) {
4287 cache->last_byte_to_unpin = (u64)-1;
4288 list_del_init(&caching_ctl->list);
4289 put_caching_control(caching_ctl);
4290 } else {
4291 cache->last_byte_to_unpin = caching_ctl->progress;
4292 }
4293 }
4294
4295 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
4296 fs_info->pinned_extents = &fs_info->freed_extents[1];
4297 else
4298 fs_info->pinned_extents = &fs_info->freed_extents[0];
4299
4300 up_write(&fs_info->extent_commit_sem);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004301
4302 update_global_block_rsv(fs_info);
Yan Zheng11833d62009-09-11 16:11:19 -04004303 return 0;
4304}
4305
4306static int unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
4307{
4308 struct btrfs_fs_info *fs_info = root->fs_info;
4309 struct btrfs_block_group_cache *cache = NULL;
4310 u64 len;
4311
4312 while (start <= end) {
4313 if (!cache ||
4314 start >= cache->key.objectid + cache->key.offset) {
4315 if (cache)
4316 btrfs_put_block_group(cache);
4317 cache = btrfs_lookup_block_group(fs_info, start);
4318 BUG_ON(!cache);
4319 }
4320
4321 len = cache->key.objectid + cache->key.offset - start;
4322 len = min(len, end + 1 - start);
4323
4324 if (start < cache->last_byte_to_unpin) {
4325 len = min(len, cache->last_byte_to_unpin - start);
4326 btrfs_add_free_space(cache, start, len);
4327 }
Josef Bacik25179202008-10-29 14:49:05 -04004328
Yan, Zhengf0486c62010-05-16 10:46:25 -04004329 start += len;
4330
Josef Bacik25179202008-10-29 14:49:05 -04004331 spin_lock(&cache->space_info->lock);
4332 spin_lock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04004333 cache->pinned -= len;
4334 cache->space_info->bytes_pinned -= len;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004335 if (cache->ro) {
4336 cache->space_info->bytes_readonly += len;
4337 } else if (cache->reserved_pinned > 0) {
4338 len = min(len, cache->reserved_pinned);
4339 cache->reserved_pinned -= len;
4340 cache->space_info->bytes_reserved += len;
4341 }
Josef Bacik25179202008-10-29 14:49:05 -04004342 spin_unlock(&cache->lock);
4343 spin_unlock(&cache->space_info->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04004344 }
4345
4346 if (cache)
Chris Masonfa9c0d792009-04-03 09:47:43 -04004347 btrfs_put_block_group(cache);
Chris Masonccd467d2007-06-28 15:57:36 -04004348 return 0;
4349}
4350
4351int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
Yan Zheng11833d62009-09-11 16:11:19 -04004352 struct btrfs_root *root)
Chris Masona28ec192007-03-06 20:08:01 -05004353{
Yan Zheng11833d62009-09-11 16:11:19 -04004354 struct btrfs_fs_info *fs_info = root->fs_info;
4355 struct extent_io_tree *unpin;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004356 struct btrfs_block_rsv *block_rsv;
4357 struct btrfs_block_rsv *next_rsv;
Chris Mason1a5bc162007-10-15 16:15:26 -04004358 u64 start;
4359 u64 end;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004360 int idx;
Chris Masona28ec192007-03-06 20:08:01 -05004361 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05004362
Yan Zheng11833d62009-09-11 16:11:19 -04004363 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
4364 unpin = &fs_info->freed_extents[1];
4365 else
4366 unpin = &fs_info->freed_extents[0];
4367
Chris Masond3977122009-01-05 21:25:51 -05004368 while (1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04004369 ret = find_first_extent_bit(unpin, 0, &start, &end,
4370 EXTENT_DIRTY);
4371 if (ret)
Chris Masona28ec192007-03-06 20:08:01 -05004372 break;
Liu Hui1f3c79a2009-01-05 15:57:51 -05004373
4374 ret = btrfs_discard_extent(root, start, end + 1 - start);
4375
Chris Mason1a5bc162007-10-15 16:15:26 -04004376 clear_extent_dirty(unpin, start, end, GFP_NOFS);
Yan Zheng11833d62009-09-11 16:11:19 -04004377 unpin_extent_range(root, start, end);
Chris Masonb9473432009-03-13 11:00:37 -04004378 cond_resched();
Chris Masona28ec192007-03-06 20:08:01 -05004379 }
Josef Bacik817d52f2009-07-13 21:29:25 -04004380
Yan, Zhengf0486c62010-05-16 10:46:25 -04004381 mutex_lock(&fs_info->durable_block_rsv_mutex);
4382 list_for_each_entry_safe(block_rsv, next_rsv,
4383 &fs_info->durable_block_rsv_list, list) {
Chris Masona28ec192007-03-06 20:08:01 -05004384
Yan, Zhengf0486c62010-05-16 10:46:25 -04004385 idx = trans->transid & 0x1;
4386 if (block_rsv->freed[idx] > 0) {
4387 block_rsv_add_bytes(block_rsv,
4388 block_rsv->freed[idx], 0);
4389 block_rsv->freed[idx] = 0;
Chris Mason8ef97622007-03-26 10:15:30 -04004390 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04004391 if (atomic_read(&block_rsv->usage) == 0) {
4392 btrfs_block_rsv_release(root, block_rsv, (u64)-1);
Zheng Yan31840ae2008-09-23 13:14:14 -04004393
Yan, Zhengf0486c62010-05-16 10:46:25 -04004394 if (block_rsv->freed[0] == 0 &&
4395 block_rsv->freed[1] == 0) {
4396 list_del_init(&block_rsv->list);
4397 kfree(block_rsv);
4398 }
4399 } else {
4400 btrfs_block_rsv_release(root, block_rsv, 0);
4401 }
4402 }
4403 mutex_unlock(&fs_info->durable_block_rsv_mutex);
4404
Chris Masone20d96d2007-03-22 12:13:20 -04004405 return 0;
4406}
4407
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004408static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
4409 struct btrfs_root *root,
4410 u64 bytenr, u64 num_bytes, u64 parent,
4411 u64 root_objectid, u64 owner_objectid,
4412 u64 owner_offset, int refs_to_drop,
4413 struct btrfs_delayed_extent_op *extent_op)
Chris Masona28ec192007-03-06 20:08:01 -05004414{
Chris Masone2fa7222007-03-12 16:22:34 -04004415 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004416 struct btrfs_path *path;
Chris Mason1261ec42007-03-20 20:35:03 -04004417 struct btrfs_fs_info *info = root->fs_info;
4418 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04004419 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004420 struct btrfs_extent_item *ei;
4421 struct btrfs_extent_inline_ref *iref;
Chris Masona28ec192007-03-06 20:08:01 -05004422 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004423 int is_data;
Chris Mason952fcca2008-02-18 16:33:44 -05004424 int extent_slot = 0;
4425 int found_extent = 0;
4426 int num_to_del = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004427 u32 item_size;
4428 u64 refs;
Chris Mason037e6392007-03-07 11:50:24 -05004429
Chris Mason5caf2a02007-04-02 11:20:42 -04004430 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04004431 if (!path)
4432 return -ENOMEM;
4433
Chris Mason3c12ac72008-04-21 12:01:38 -04004434 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04004435 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004436
4437 is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
4438 BUG_ON(!is_data && refs_to_drop != 1);
4439
4440 ret = lookup_extent_backref(trans, extent_root, path, &iref,
4441 bytenr, num_bytes, parent,
4442 root_objectid, owner_objectid,
4443 owner_offset);
Chris Mason7bb86312007-12-11 09:25:06 -05004444 if (ret == 0) {
Chris Mason952fcca2008-02-18 16:33:44 -05004445 extent_slot = path->slots[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004446 while (extent_slot >= 0) {
4447 btrfs_item_key_to_cpu(path->nodes[0], &key,
Chris Mason952fcca2008-02-18 16:33:44 -05004448 extent_slot);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004449 if (key.objectid != bytenr)
Chris Mason952fcca2008-02-18 16:33:44 -05004450 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004451 if (key.type == BTRFS_EXTENT_ITEM_KEY &&
4452 key.offset == num_bytes) {
Chris Mason952fcca2008-02-18 16:33:44 -05004453 found_extent = 1;
4454 break;
4455 }
4456 if (path->slots[0] - extent_slot > 5)
4457 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004458 extent_slot--;
Chris Mason952fcca2008-02-18 16:33:44 -05004459 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004460#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
4461 item_size = btrfs_item_size_nr(path->nodes[0], extent_slot);
4462 if (found_extent && item_size < sizeof(*ei))
4463 found_extent = 0;
4464#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04004465 if (!found_extent) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004466 BUG_ON(iref);
Chris Mason56bec292009-03-13 10:10:06 -04004467 ret = remove_extent_backref(trans, extent_root, path,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004468 NULL, refs_to_drop,
4469 is_data);
Zheng Yan31840ae2008-09-23 13:14:14 -04004470 BUG_ON(ret);
4471 btrfs_release_path(extent_root, path);
Chris Masonb9473432009-03-13 11:00:37 -04004472 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004473
4474 key.objectid = bytenr;
4475 key.type = BTRFS_EXTENT_ITEM_KEY;
4476 key.offset = num_bytes;
4477
Zheng Yan31840ae2008-09-23 13:14:14 -04004478 ret = btrfs_search_slot(trans, extent_root,
4479 &key, path, -1, 1);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004480 if (ret) {
4481 printk(KERN_ERR "umm, got %d back from search"
Chris Masond3977122009-01-05 21:25:51 -05004482 ", was looking for %llu\n", ret,
4483 (unsigned long long)bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004484 btrfs_print_leaf(extent_root, path->nodes[0]);
4485 }
Zheng Yan31840ae2008-09-23 13:14:14 -04004486 BUG_ON(ret);
4487 extent_slot = path->slots[0];
4488 }
Chris Mason7bb86312007-12-11 09:25:06 -05004489 } else {
4490 btrfs_print_leaf(extent_root, path->nodes[0]);
4491 WARN_ON(1);
Chris Masond3977122009-01-05 21:25:51 -05004492 printk(KERN_ERR "btrfs unable to find ref byte nr %llu "
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004493 "parent %llu root %llu owner %llu offset %llu\n",
Chris Masond3977122009-01-05 21:25:51 -05004494 (unsigned long long)bytenr,
Chris Mason56bec292009-03-13 10:10:06 -04004495 (unsigned long long)parent,
Chris Masond3977122009-01-05 21:25:51 -05004496 (unsigned long long)root_objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004497 (unsigned long long)owner_objectid,
4498 (unsigned long long)owner_offset);
Chris Mason7bb86312007-12-11 09:25:06 -05004499 }
Chris Mason5f39d392007-10-15 16:14:19 -04004500
4501 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004502 item_size = btrfs_item_size_nr(leaf, extent_slot);
4503#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
4504 if (item_size < sizeof(*ei)) {
4505 BUG_ON(found_extent || extent_slot != path->slots[0]);
4506 ret = convert_extent_item_v0(trans, extent_root, path,
4507 owner_objectid, 0);
4508 BUG_ON(ret < 0);
4509
4510 btrfs_release_path(extent_root, path);
4511 path->leave_spinning = 1;
4512
4513 key.objectid = bytenr;
4514 key.type = BTRFS_EXTENT_ITEM_KEY;
4515 key.offset = num_bytes;
4516
4517 ret = btrfs_search_slot(trans, extent_root, &key, path,
4518 -1, 1);
4519 if (ret) {
4520 printk(KERN_ERR "umm, got %d back from search"
4521 ", was looking for %llu\n", ret,
4522 (unsigned long long)bytenr);
4523 btrfs_print_leaf(extent_root, path->nodes[0]);
4524 }
4525 BUG_ON(ret);
4526 extent_slot = path->slots[0];
4527 leaf = path->nodes[0];
4528 item_size = btrfs_item_size_nr(leaf, extent_slot);
4529 }
4530#endif
4531 BUG_ON(item_size < sizeof(*ei));
Chris Mason952fcca2008-02-18 16:33:44 -05004532 ei = btrfs_item_ptr(leaf, extent_slot,
Chris Mason123abc82007-03-14 14:14:43 -04004533 struct btrfs_extent_item);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004534 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
4535 struct btrfs_tree_block_info *bi;
4536 BUG_ON(item_size < sizeof(*ei) + sizeof(*bi));
4537 bi = (struct btrfs_tree_block_info *)(ei + 1);
4538 WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
Chris Mason952fcca2008-02-18 16:33:44 -05004539 }
4540
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004541 refs = btrfs_extent_refs(leaf, ei);
4542 BUG_ON(refs < refs_to_drop);
4543 refs -= refs_to_drop;
4544
4545 if (refs > 0) {
4546 if (extent_op)
4547 __run_delayed_extent_op(extent_op, leaf, ei);
4548 /*
4549 * In the case of inline back ref, reference count will
4550 * be updated by remove_extent_backref
4551 */
4552 if (iref) {
4553 BUG_ON(!found_extent);
4554 } else {
4555 btrfs_set_extent_refs(leaf, ei, refs);
4556 btrfs_mark_buffer_dirty(leaf);
4557 }
4558 if (found_extent) {
4559 ret = remove_extent_backref(trans, extent_root, path,
4560 iref, refs_to_drop,
4561 is_data);
4562 BUG_ON(ret);
4563 }
4564 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004565 if (found_extent) {
4566 BUG_ON(is_data && refs_to_drop !=
4567 extent_data_ref_count(root, path, iref));
4568 if (iref) {
4569 BUG_ON(path->slots[0] != extent_slot);
4570 } else {
4571 BUG_ON(path->slots[0] != extent_slot + 1);
4572 path->slots[0] = extent_slot;
4573 num_to_del = 2;
4574 }
Chris Mason78fae272007-03-25 11:35:08 -04004575 }
Chris Masonb9473432009-03-13 11:00:37 -04004576
Chris Mason952fcca2008-02-18 16:33:44 -05004577 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
4578 num_to_del);
Zheng Yan31840ae2008-09-23 13:14:14 -04004579 BUG_ON(ret);
Josef Bacik25179202008-10-29 14:49:05 -04004580 btrfs_release_path(extent_root, path);
David Woodhouse21af8042008-08-12 14:13:26 +01004581
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004582 if (is_data) {
Chris Mason459931e2008-12-10 09:10:46 -05004583 ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
4584 BUG_ON(ret);
Chris Masond57e62b2009-03-31 13:47:50 -04004585 } else {
4586 invalidate_mapping_pages(info->btree_inode->i_mapping,
4587 bytenr >> PAGE_CACHE_SHIFT,
4588 (bytenr + num_bytes - 1) >> PAGE_CACHE_SHIFT);
Chris Mason459931e2008-12-10 09:10:46 -05004589 }
4590
Yan, Zhengf0486c62010-05-16 10:46:25 -04004591 ret = update_block_group(trans, root, bytenr, num_bytes, 0);
Chris Masondcbdd4d2008-12-16 13:51:01 -05004592 BUG_ON(ret);
Chris Masona28ec192007-03-06 20:08:01 -05004593 }
Chris Mason5caf2a02007-04-02 11:20:42 -04004594 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -05004595 return ret;
4596}
4597
4598/*
Yan, Zhengf0486c62010-05-16 10:46:25 -04004599 * when we free an block, it is possible (and likely) that we free the last
Chris Mason1887be62009-03-13 10:11:24 -04004600 * delayed ref for that extent as well. This searches the delayed ref tree for
4601 * a given extent, and if there are no other delayed refs to be processed, it
4602 * removes it from the tree.
4603 */
4604static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
4605 struct btrfs_root *root, u64 bytenr)
4606{
4607 struct btrfs_delayed_ref_head *head;
4608 struct btrfs_delayed_ref_root *delayed_refs;
4609 struct btrfs_delayed_ref_node *ref;
4610 struct rb_node *node;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004611 int ret = 0;
Chris Mason1887be62009-03-13 10:11:24 -04004612
4613 delayed_refs = &trans->transaction->delayed_refs;
4614 spin_lock(&delayed_refs->lock);
4615 head = btrfs_find_delayed_ref_head(trans, bytenr);
4616 if (!head)
4617 goto out;
4618
4619 node = rb_prev(&head->node.rb_node);
4620 if (!node)
4621 goto out;
4622
4623 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
4624
4625 /* there are still entries for this ref, we can't drop it */
4626 if (ref->bytenr == bytenr)
4627 goto out;
4628
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004629 if (head->extent_op) {
4630 if (!head->must_insert_reserved)
4631 goto out;
4632 kfree(head->extent_op);
4633 head->extent_op = NULL;
4634 }
4635
Chris Mason1887be62009-03-13 10:11:24 -04004636 /*
4637 * waiting for the lock here would deadlock. If someone else has it
4638 * locked they are already in the process of dropping it anyway
4639 */
4640 if (!mutex_trylock(&head->mutex))
4641 goto out;
4642
4643 /*
4644 * at this point we have a head with no other entries. Go
4645 * ahead and process it.
4646 */
4647 head->node.in_tree = 0;
4648 rb_erase(&head->node.rb_node, &delayed_refs->root);
Chris Masonc3e69d52009-03-13 10:17:05 -04004649
Chris Mason1887be62009-03-13 10:11:24 -04004650 delayed_refs->num_entries--;
4651
4652 /*
4653 * we don't take a ref on the node because we're removing it from the
4654 * tree, so we just steal the ref the tree was holding.
4655 */
Chris Masonc3e69d52009-03-13 10:17:05 -04004656 delayed_refs->num_heads--;
4657 if (list_empty(&head->cluster))
4658 delayed_refs->num_heads_ready--;
4659
4660 list_del_init(&head->cluster);
Chris Mason1887be62009-03-13 10:11:24 -04004661 spin_unlock(&delayed_refs->lock);
4662
Yan, Zhengf0486c62010-05-16 10:46:25 -04004663 BUG_ON(head->extent_op);
4664 if (head->must_insert_reserved)
4665 ret = 1;
4666
4667 mutex_unlock(&head->mutex);
Chris Mason1887be62009-03-13 10:11:24 -04004668 btrfs_put_delayed_ref(&head->node);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004669 return ret;
Chris Mason1887be62009-03-13 10:11:24 -04004670out:
4671 spin_unlock(&delayed_refs->lock);
4672 return 0;
4673}
4674
Yan, Zhengf0486c62010-05-16 10:46:25 -04004675void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
4676 struct btrfs_root *root,
4677 struct extent_buffer *buf,
4678 u64 parent, int last_ref)
4679{
4680 struct btrfs_block_rsv *block_rsv;
4681 struct btrfs_block_group_cache *cache = NULL;
4682 int ret;
4683
4684 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4685 ret = btrfs_add_delayed_tree_ref(trans, buf->start, buf->len,
4686 parent, root->root_key.objectid,
4687 btrfs_header_level(buf),
4688 BTRFS_DROP_DELAYED_REF, NULL);
4689 BUG_ON(ret);
4690 }
4691
4692 if (!last_ref)
4693 return;
4694
4695 block_rsv = get_block_rsv(trans, root);
4696 cache = btrfs_lookup_block_group(root->fs_info, buf->start);
Yan, Zheng3bf84a52010-05-31 09:04:46 +00004697 if (block_rsv->space_info != cache->space_info)
4698 goto out;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004699
4700 if (btrfs_header_generation(buf) == trans->transid) {
4701 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4702 ret = check_ref_cleanup(trans, root, buf->start);
4703 if (!ret)
4704 goto pin;
4705 }
4706
4707 if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
4708 pin_down_extent(root, cache, buf->start, buf->len, 1);
4709 goto pin;
4710 }
4711
4712 WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags));
4713
4714 btrfs_add_free_space(cache, buf->start, buf->len);
Li Dongyangb4d00d52011-03-24 10:24:25 +00004715 ret = btrfs_update_reserved_bytes(cache, buf->len, 0, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004716 if (ret == -EAGAIN) {
4717 /* block group became read-only */
Li Dongyangb4d00d52011-03-24 10:24:25 +00004718 btrfs_update_reserved_bytes(cache, buf->len, 0, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004719 goto out;
4720 }
4721
4722 ret = 1;
4723 spin_lock(&block_rsv->lock);
4724 if (block_rsv->reserved < block_rsv->size) {
4725 block_rsv->reserved += buf->len;
4726 ret = 0;
4727 }
4728 spin_unlock(&block_rsv->lock);
4729
4730 if (ret) {
4731 spin_lock(&cache->space_info->lock);
4732 cache->space_info->bytes_reserved -= buf->len;
Chris Mason36e39c42011-03-12 07:08:42 -05004733 cache->space_info->reservation_progress++;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004734 spin_unlock(&cache->space_info->lock);
4735 }
4736 goto out;
4737 }
4738pin:
4739 if (block_rsv->durable && !cache->ro) {
4740 ret = 0;
4741 spin_lock(&cache->lock);
4742 if (!cache->ro) {
4743 cache->reserved_pinned += buf->len;
4744 ret = 1;
4745 }
4746 spin_unlock(&cache->lock);
4747
4748 if (ret) {
4749 spin_lock(&block_rsv->lock);
4750 block_rsv->freed[trans->transid & 0x1] += buf->len;
4751 spin_unlock(&block_rsv->lock);
4752 }
4753 }
4754out:
Josef Bacika826d6d2011-03-16 13:42:43 -04004755 /*
4756 * Deleting the buffer, clear the corrupt flag since it doesn't matter
4757 * anymore.
4758 */
4759 clear_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004760 btrfs_put_block_group(cache);
4761}
4762
Chris Mason925baed2008-06-25 16:01:30 -04004763int btrfs_free_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04004764 struct btrfs_root *root,
4765 u64 bytenr, u64 num_bytes, u64 parent,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004766 u64 root_objectid, u64 owner, u64 offset)
Chris Mason925baed2008-06-25 16:01:30 -04004767{
4768 int ret;
4769
Chris Mason56bec292009-03-13 10:10:06 -04004770 /*
4771 * tree log blocks never actually go into the extent allocation
4772 * tree, just update pinning info and exit early.
Chris Mason56bec292009-03-13 10:10:06 -04004773 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004774 if (root_objectid == BTRFS_TREE_LOG_OBJECTID) {
4775 WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID);
Chris Masonb9473432009-03-13 11:00:37 -04004776 /* unlocks the pinned mutex */
Yan Zheng11833d62009-09-11 16:11:19 -04004777 btrfs_pin_extent(root, bytenr, num_bytes, 1);
Chris Mason56bec292009-03-13 10:10:06 -04004778 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004779 } else if (owner < BTRFS_FIRST_FREE_OBJECTID) {
4780 ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes,
4781 parent, root_objectid, (int)owner,
4782 BTRFS_DROP_DELAYED_REF, NULL);
Chris Mason1887be62009-03-13 10:11:24 -04004783 BUG_ON(ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004784 } else {
4785 ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes,
4786 parent, root_objectid, owner,
4787 offset, BTRFS_DROP_DELAYED_REF, NULL);
4788 BUG_ON(ret);
Chris Mason56bec292009-03-13 10:10:06 -04004789 }
Chris Mason925baed2008-06-25 16:01:30 -04004790 return ret;
4791}
4792
Chris Mason87ee04e2007-11-30 11:30:34 -05004793static u64 stripe_align(struct btrfs_root *root, u64 val)
4794{
4795 u64 mask = ((u64)root->stripesize - 1);
4796 u64 ret = (val + mask) & ~mask;
4797 return ret;
4798}
4799
Chris Masonfec577f2007-02-26 10:40:21 -05004800/*
Josef Bacik817d52f2009-07-13 21:29:25 -04004801 * when we wait for progress in the block group caching, its because
4802 * our allocation attempt failed at least once. So, we must sleep
4803 * and let some progress happen before we try again.
4804 *
4805 * This function will sleep at least once waiting for new free space to
4806 * show up, and then it will check the block group free space numbers
4807 * for our min num_bytes. Another option is to have it go ahead
4808 * and look in the rbtree for a free extent of a given size, but this
4809 * is a good start.
4810 */
4811static noinline int
4812wait_block_group_cache_progress(struct btrfs_block_group_cache *cache,
4813 u64 num_bytes)
4814{
Yan Zheng11833d62009-09-11 16:11:19 -04004815 struct btrfs_caching_control *caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -04004816 DEFINE_WAIT(wait);
4817
Yan Zheng11833d62009-09-11 16:11:19 -04004818 caching_ctl = get_caching_control(cache);
4819 if (!caching_ctl)
Josef Bacik817d52f2009-07-13 21:29:25 -04004820 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -04004821
Yan Zheng11833d62009-09-11 16:11:19 -04004822 wait_event(caching_ctl->wait, block_group_cache_done(cache) ||
Josef Bacik817d52f2009-07-13 21:29:25 -04004823 (cache->free_space >= num_bytes));
Yan Zheng11833d62009-09-11 16:11:19 -04004824
4825 put_caching_control(caching_ctl);
4826 return 0;
4827}
4828
4829static noinline int
4830wait_block_group_cache_done(struct btrfs_block_group_cache *cache)
4831{
4832 struct btrfs_caching_control *caching_ctl;
4833 DEFINE_WAIT(wait);
4834
4835 caching_ctl = get_caching_control(cache);
4836 if (!caching_ctl)
4837 return 0;
4838
4839 wait_event(caching_ctl->wait, block_group_cache_done(cache));
4840
4841 put_caching_control(caching_ctl);
Josef Bacik817d52f2009-07-13 21:29:25 -04004842 return 0;
4843}
4844
Yan, Zhengb742bb822010-05-16 10:46:24 -04004845static int get_block_group_index(struct btrfs_block_group_cache *cache)
4846{
4847 int index;
4848 if (cache->flags & BTRFS_BLOCK_GROUP_RAID10)
4849 index = 0;
4850 else if (cache->flags & BTRFS_BLOCK_GROUP_RAID1)
4851 index = 1;
4852 else if (cache->flags & BTRFS_BLOCK_GROUP_DUP)
4853 index = 2;
4854 else if (cache->flags & BTRFS_BLOCK_GROUP_RAID0)
4855 index = 3;
4856 else
4857 index = 4;
4858 return index;
4859}
4860
Josef Bacik817d52f2009-07-13 21:29:25 -04004861enum btrfs_loop_type {
Josef Bacikccf0e722009-11-10 21:23:48 -05004862 LOOP_FIND_IDEAL = 0,
Josef Bacik817d52f2009-07-13 21:29:25 -04004863 LOOP_CACHING_NOWAIT = 1,
4864 LOOP_CACHING_WAIT = 2,
4865 LOOP_ALLOC_CHUNK = 3,
4866 LOOP_NO_EMPTY_SIZE = 4,
4867};
4868
4869/*
Chris Masonfec577f2007-02-26 10:40:21 -05004870 * walks the btree of allocated extents and find a hole of a given size.
4871 * The key ins is changed to record the hole:
4872 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -04004873 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -05004874 * ins->offset == number of blocks
4875 * Any available blocks before search_start are skipped.
4876 */
Chris Masond3977122009-01-05 21:25:51 -05004877static noinline int find_free_extent(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05004878 struct btrfs_root *orig_root,
4879 u64 num_bytes, u64 empty_size,
4880 u64 search_start, u64 search_end,
4881 u64 hint_byte, struct btrfs_key *ins,
Chris Mason98ed5172008-01-03 10:01:48 -05004882 int data)
Chris Masonfec577f2007-02-26 10:40:21 -05004883{
Josef Bacik80eb2342008-10-29 14:49:05 -04004884 int ret = 0;
Chris Masond3977122009-01-05 21:25:51 -05004885 struct btrfs_root *root = orig_root->fs_info->extent_root;
Chris Masonfa9c0d792009-04-03 09:47:43 -04004886 struct btrfs_free_cluster *last_ptr = NULL;
Josef Bacik80eb2342008-10-29 14:49:05 -04004887 struct btrfs_block_group_cache *block_group = NULL;
Chris Mason239b14b2008-03-24 15:02:07 -04004888 int empty_cluster = 2 * 1024 * 1024;
Chris Mason0ef3e662008-05-24 14:04:53 -04004889 int allowed_chunk_alloc = 0;
Josef Bacikccf0e722009-11-10 21:23:48 -05004890 int done_chunk_alloc = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04004891 struct btrfs_space_info *space_info;
Chris Masonfa9c0d792009-04-03 09:47:43 -04004892 int last_ptr_loop = 0;
4893 int loop = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004894 int index = 0;
Josef Bacik817d52f2009-07-13 21:29:25 -04004895 bool found_uncached_bg = false;
Josef Bacik0a243252009-09-11 16:11:20 -04004896 bool failed_cluster_refill = false;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04004897 bool failed_alloc = false;
Josef Bacik67377732010-09-16 16:19:09 -04004898 bool use_cluster = true;
Josef Bacikccf0e722009-11-10 21:23:48 -05004899 u64 ideal_cache_percent = 0;
4900 u64 ideal_cache_offset = 0;
Chris Masonfec577f2007-02-26 10:40:21 -05004901
Chris Masondb945352007-10-15 16:15:53 -04004902 WARN_ON(num_bytes < root->sectorsize);
Chris Masonb1a4d962007-04-04 15:27:52 -04004903 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
Josef Bacik80eb2342008-10-29 14:49:05 -04004904 ins->objectid = 0;
4905 ins->offset = 0;
Chris Masonb1a4d962007-04-04 15:27:52 -04004906
Josef Bacik2552d172009-04-03 10:14:19 -04004907 space_info = __find_space_info(root->fs_info, data);
Josef Bacik1b1d1f62010-03-19 20:49:55 +00004908 if (!space_info) {
4909 printk(KERN_ERR "No space info for %d\n", data);
4910 return -ENOSPC;
4911 }
Josef Bacik2552d172009-04-03 10:14:19 -04004912
Josef Bacik67377732010-09-16 16:19:09 -04004913 /*
4914 * If the space info is for both data and metadata it means we have a
4915 * small filesystem and we can't use the clustering stuff.
4916 */
4917 if (btrfs_mixed_space_info(space_info))
4918 use_cluster = false;
4919
Chris Mason0ef3e662008-05-24 14:04:53 -04004920 if (orig_root->ref_cows || empty_size)
4921 allowed_chunk_alloc = 1;
4922
Josef Bacik67377732010-09-16 16:19:09 -04004923 if (data & BTRFS_BLOCK_GROUP_METADATA && use_cluster) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04004924 last_ptr = &root->fs_info->meta_alloc_cluster;
Chris Mason536ac8a2009-02-12 09:41:38 -05004925 if (!btrfs_test_opt(root, SSD))
4926 empty_cluster = 64 * 1024;
Chris Mason239b14b2008-03-24 15:02:07 -04004927 }
4928
Josef Bacik67377732010-09-16 16:19:09 -04004929 if ((data & BTRFS_BLOCK_GROUP_DATA) && use_cluster &&
4930 btrfs_test_opt(root, SSD)) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04004931 last_ptr = &root->fs_info->data_alloc_cluster;
4932 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04004933
Chris Mason239b14b2008-03-24 15:02:07 -04004934 if (last_ptr) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04004935 spin_lock(&last_ptr->lock);
4936 if (last_ptr->block_group)
4937 hint_byte = last_ptr->window_start;
4938 spin_unlock(&last_ptr->lock);
Chris Mason239b14b2008-03-24 15:02:07 -04004939 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04004940
Chris Masona061fc82008-05-07 11:43:44 -04004941 search_start = max(search_start, first_logical_byte(root, 0));
Chris Mason239b14b2008-03-24 15:02:07 -04004942 search_start = max(search_start, hint_byte);
Chris Mason0b86a832008-03-24 15:01:56 -04004943
Josef Bacik817d52f2009-07-13 21:29:25 -04004944 if (!last_ptr)
Chris Masonfa9c0d792009-04-03 09:47:43 -04004945 empty_cluster = 0;
Chris Masonfa9c0d792009-04-03 09:47:43 -04004946
Josef Bacik2552d172009-04-03 10:14:19 -04004947 if (search_start == hint_byte) {
Josef Bacikccf0e722009-11-10 21:23:48 -05004948ideal_cache:
Josef Bacik2552d172009-04-03 10:14:19 -04004949 block_group = btrfs_lookup_block_group(root->fs_info,
4950 search_start);
Josef Bacik817d52f2009-07-13 21:29:25 -04004951 /*
4952 * we don't want to use the block group if it doesn't match our
4953 * allocation bits, or if its not cached.
Josef Bacikccf0e722009-11-10 21:23:48 -05004954 *
4955 * However if we are re-searching with an ideal block group
4956 * picked out then we don't care that the block group is cached.
Josef Bacik817d52f2009-07-13 21:29:25 -04004957 */
4958 if (block_group && block_group_bits(block_group, data) &&
Josef Bacikccf0e722009-11-10 21:23:48 -05004959 (block_group->cached != BTRFS_CACHE_NO ||
4960 search_start == ideal_cache_offset)) {
Josef Bacik2552d172009-04-03 10:14:19 -04004961 down_read(&space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04004962 if (list_empty(&block_group->list) ||
4963 block_group->ro) {
4964 /*
4965 * someone is removing this block group,
4966 * we can't jump into the have_block_group
4967 * target because our list pointers are not
4968 * valid
4969 */
4970 btrfs_put_block_group(block_group);
4971 up_read(&space_info->groups_sem);
Josef Bacikccf0e722009-11-10 21:23:48 -05004972 } else {
Yan, Zhengb742bb822010-05-16 10:46:24 -04004973 index = get_block_group_index(block_group);
Chris Mason44fb5512009-06-04 15:34:51 -04004974 goto have_block_group;
Josef Bacikccf0e722009-11-10 21:23:48 -05004975 }
Josef Bacik2552d172009-04-03 10:14:19 -04004976 } else if (block_group) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04004977 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04004978 }
Chris Mason42e70e72008-11-07 18:17:11 -05004979 }
Josef Bacik2552d172009-04-03 10:14:19 -04004980search:
Josef Bacik80eb2342008-10-29 14:49:05 -04004981 down_read(&space_info->groups_sem);
Yan, Zhengb742bb822010-05-16 10:46:24 -04004982 list_for_each_entry(block_group, &space_info->block_groups[index],
4983 list) {
Josef Bacik6226cb02009-04-03 10:14:18 -04004984 u64 offset;
Josef Bacik817d52f2009-07-13 21:29:25 -04004985 int cached;
Chris Mason8a1413a22008-11-10 16:13:54 -05004986
Josef Bacik11dfe352009-11-13 20:12:59 +00004987 btrfs_get_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04004988 search_start = block_group->key.objectid;
Chris Mason42e70e72008-11-07 18:17:11 -05004989
Chris Mason83a50de2010-12-13 15:06:46 -05004990 /*
4991 * this can happen if we end up cycling through all the
4992 * raid types, but we want to make sure we only allocate
4993 * for the proper type.
4994 */
4995 if (!block_group_bits(block_group, data)) {
4996 u64 extra = BTRFS_BLOCK_GROUP_DUP |
4997 BTRFS_BLOCK_GROUP_RAID1 |
4998 BTRFS_BLOCK_GROUP_RAID10;
4999
5000 /*
5001 * if they asked for extra copies and this block group
5002 * doesn't provide them, bail. This does allow us to
5003 * fill raid0 from raid1.
5004 */
5005 if ((data & extra) && !(block_group->flags & extra))
5006 goto loop;
5007 }
5008
Josef Bacik2552d172009-04-03 10:14:19 -04005009have_block_group:
Josef Bacik817d52f2009-07-13 21:29:25 -04005010 if (unlikely(block_group->cached == BTRFS_CACHE_NO)) {
Josef Bacikccf0e722009-11-10 21:23:48 -05005011 u64 free_percent;
5012
Josef Bacikb8399de2010-12-08 09:15:11 -05005013 ret = cache_block_group(block_group, trans,
5014 orig_root, 1);
Josef Bacik9d66e232010-08-25 16:54:15 -04005015 if (block_group->cached == BTRFS_CACHE_FINISHED)
5016 goto have_block_group;
5017
Josef Bacikccf0e722009-11-10 21:23:48 -05005018 free_percent = btrfs_block_group_used(&block_group->item);
5019 free_percent *= 100;
5020 free_percent = div64_u64(free_percent,
5021 block_group->key.offset);
5022 free_percent = 100 - free_percent;
5023 if (free_percent > ideal_cache_percent &&
5024 likely(!block_group->ro)) {
5025 ideal_cache_offset = block_group->key.objectid;
5026 ideal_cache_percent = free_percent;
5027 }
5028
Josef Bacik817d52f2009-07-13 21:29:25 -04005029 /*
Josef Bacikccf0e722009-11-10 21:23:48 -05005030 * We only want to start kthread caching if we are at
5031 * the point where we will wait for caching to make
5032 * progress, or if our ideal search is over and we've
5033 * found somebody to start caching.
Josef Bacik817d52f2009-07-13 21:29:25 -04005034 */
5035 if (loop > LOOP_CACHING_NOWAIT ||
Josef Bacikccf0e722009-11-10 21:23:48 -05005036 (loop > LOOP_FIND_IDEAL &&
5037 atomic_read(&space_info->caching_threads) < 2)) {
Josef Bacikb8399de2010-12-08 09:15:11 -05005038 ret = cache_block_group(block_group, trans,
5039 orig_root, 0);
Josef Bacik817d52f2009-07-13 21:29:25 -04005040 BUG_ON(ret);
Josef Bacik2552d172009-04-03 10:14:19 -04005041 }
Josef Bacikccf0e722009-11-10 21:23:48 -05005042 found_uncached_bg = true;
5043
5044 /*
5045 * If loop is set for cached only, try the next block
5046 * group.
5047 */
5048 if (loop == LOOP_FIND_IDEAL)
5049 goto loop;
Josef Bacikea6a4782008-11-20 12:16:16 -05005050 }
5051
Josef Bacik817d52f2009-07-13 21:29:25 -04005052 cached = block_group_cache_done(block_group);
Josef Bacikccf0e722009-11-10 21:23:48 -05005053 if (unlikely(!cached))
Josef Bacik817d52f2009-07-13 21:29:25 -04005054 found_uncached_bg = true;
5055
Josef Bacikea6a4782008-11-20 12:16:16 -05005056 if (unlikely(block_group->ro))
Josef Bacik2552d172009-04-03 10:14:19 -04005057 goto loop;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005058
Josef Bacik0a243252009-09-11 16:11:20 -04005059 /*
5060 * Ok we want to try and use the cluster allocator, so lets look
5061 * there, unless we are on LOOP_NO_EMPTY_SIZE, since we will
5062 * have tried the cluster allocator plenty of times at this
5063 * point and not have found anything, so we are likely way too
5064 * fragmented for the clustering stuff to find anything, so lets
5065 * just skip it and let the allocator find whatever block it can
5066 * find
5067 */
5068 if (last_ptr && loop < LOOP_NO_EMPTY_SIZE) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04005069 /*
5070 * the refill lock keeps out other
5071 * people trying to start a new cluster
5072 */
5073 spin_lock(&last_ptr->refill_lock);
Chris Mason44fb5512009-06-04 15:34:51 -04005074 if (last_ptr->block_group &&
5075 (last_ptr->block_group->ro ||
5076 !block_group_bits(last_ptr->block_group, data))) {
5077 offset = 0;
5078 goto refill_cluster;
5079 }
5080
Chris Masonfa9c0d792009-04-03 09:47:43 -04005081 offset = btrfs_alloc_from_cluster(block_group, last_ptr,
5082 num_bytes, search_start);
5083 if (offset) {
5084 /* we have a block, we're done */
5085 spin_unlock(&last_ptr->refill_lock);
5086 goto checks;
5087 }
5088
5089 spin_lock(&last_ptr->lock);
5090 /*
5091 * whoops, this cluster doesn't actually point to
5092 * this block group. Get a ref on the block
5093 * group is does point to and try again
5094 */
5095 if (!last_ptr_loop && last_ptr->block_group &&
5096 last_ptr->block_group != block_group) {
5097
5098 btrfs_put_block_group(block_group);
5099 block_group = last_ptr->block_group;
Josef Bacik11dfe352009-11-13 20:12:59 +00005100 btrfs_get_block_group(block_group);
Chris Masonfa9c0d792009-04-03 09:47:43 -04005101 spin_unlock(&last_ptr->lock);
5102 spin_unlock(&last_ptr->refill_lock);
5103
5104 last_ptr_loop = 1;
5105 search_start = block_group->key.objectid;
Chris Mason44fb5512009-06-04 15:34:51 -04005106 /*
5107 * we know this block group is properly
5108 * in the list because
5109 * btrfs_remove_block_group, drops the
5110 * cluster before it removes the block
5111 * group from the list
5112 */
Chris Masonfa9c0d792009-04-03 09:47:43 -04005113 goto have_block_group;
5114 }
5115 spin_unlock(&last_ptr->lock);
Chris Mason44fb5512009-06-04 15:34:51 -04005116refill_cluster:
Chris Masonfa9c0d792009-04-03 09:47:43 -04005117 /*
5118 * this cluster didn't work out, free it and
5119 * start over
5120 */
5121 btrfs_return_cluster_to_free_space(NULL, last_ptr);
5122
5123 last_ptr_loop = 0;
5124
5125 /* allocate a cluster in this block group */
Chris Mason451d7582009-06-09 20:28:34 -04005126 ret = btrfs_find_space_cluster(trans, root,
Chris Masonfa9c0d792009-04-03 09:47:43 -04005127 block_group, last_ptr,
5128 offset, num_bytes,
5129 empty_cluster + empty_size);
5130 if (ret == 0) {
5131 /*
5132 * now pull our allocation out of this
5133 * cluster
5134 */
5135 offset = btrfs_alloc_from_cluster(block_group,
5136 last_ptr, num_bytes,
5137 search_start);
5138 if (offset) {
5139 /* we found one, proceed */
5140 spin_unlock(&last_ptr->refill_lock);
5141 goto checks;
5142 }
Josef Bacik0a243252009-09-11 16:11:20 -04005143 } else if (!cached && loop > LOOP_CACHING_NOWAIT
5144 && !failed_cluster_refill) {
Josef Bacik817d52f2009-07-13 21:29:25 -04005145 spin_unlock(&last_ptr->refill_lock);
5146
Josef Bacik0a243252009-09-11 16:11:20 -04005147 failed_cluster_refill = true;
Josef Bacik817d52f2009-07-13 21:29:25 -04005148 wait_block_group_cache_progress(block_group,
5149 num_bytes + empty_cluster + empty_size);
5150 goto have_block_group;
Chris Masonfa9c0d792009-04-03 09:47:43 -04005151 }
Josef Bacik817d52f2009-07-13 21:29:25 -04005152
Chris Masonfa9c0d792009-04-03 09:47:43 -04005153 /*
5154 * at this point we either didn't find a cluster
5155 * or we weren't able to allocate a block from our
5156 * cluster. Free the cluster we've been trying
5157 * to use, and go to the next block group
5158 */
Josef Bacik0a243252009-09-11 16:11:20 -04005159 btrfs_return_cluster_to_free_space(NULL, last_ptr);
Chris Masonfa9c0d792009-04-03 09:47:43 -04005160 spin_unlock(&last_ptr->refill_lock);
Josef Bacik0a243252009-09-11 16:11:20 -04005161 goto loop;
Chris Masonfa9c0d792009-04-03 09:47:43 -04005162 }
5163
Josef Bacik6226cb02009-04-03 10:14:18 -04005164 offset = btrfs_find_space_for_alloc(block_group, search_start,
5165 num_bytes, empty_size);
Josef Bacik1cdda9b2009-10-06 10:04:28 -04005166 /*
5167 * If we didn't find a chunk, and we haven't failed on this
5168 * block group before, and this block group is in the middle of
5169 * caching and we are ok with waiting, then go ahead and wait
5170 * for progress to be made, and set failed_alloc to true.
5171 *
5172 * If failed_alloc is true then we've already waited on this
5173 * block group once and should move on to the next block group.
5174 */
5175 if (!offset && !failed_alloc && !cached &&
5176 loop > LOOP_CACHING_NOWAIT) {
Josef Bacik817d52f2009-07-13 21:29:25 -04005177 wait_block_group_cache_progress(block_group,
Josef Bacik1cdda9b2009-10-06 10:04:28 -04005178 num_bytes + empty_size);
5179 failed_alloc = true;
Josef Bacik817d52f2009-07-13 21:29:25 -04005180 goto have_block_group;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04005181 } else if (!offset) {
5182 goto loop;
Josef Bacik817d52f2009-07-13 21:29:25 -04005183 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04005184checks:
Josef Bacik6226cb02009-04-03 10:14:18 -04005185 search_start = stripe_align(root, offset);
Josef Bacik2552d172009-04-03 10:14:19 -04005186 /* move on to the next group */
Josef Bacik6226cb02009-04-03 10:14:18 -04005187 if (search_start + num_bytes >= search_end) {
5188 btrfs_add_free_space(block_group, offset, num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04005189 goto loop;
Josef Bacik6226cb02009-04-03 10:14:18 -04005190 }
Chris Masone37c9e62007-05-09 20:13:14 -04005191
Josef Bacik2552d172009-04-03 10:14:19 -04005192 /* move on to the next group */
5193 if (search_start + num_bytes >
Josef Bacik6226cb02009-04-03 10:14:18 -04005194 block_group->key.objectid + block_group->key.offset) {
5195 btrfs_add_free_space(block_group, offset, num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04005196 goto loop;
Josef Bacik6226cb02009-04-03 10:14:18 -04005197 }
Josef Bacik80eb2342008-10-29 14:49:05 -04005198
Josef Bacik2552d172009-04-03 10:14:19 -04005199 ins->objectid = search_start;
5200 ins->offset = num_bytes;
5201
Josef Bacik6226cb02009-04-03 10:14:18 -04005202 if (offset < search_start)
5203 btrfs_add_free_space(block_group, offset,
5204 search_start - offset);
5205 BUG_ON(offset > search_start);
5206
Li Dongyangb4d00d52011-03-24 10:24:25 +00005207 ret = btrfs_update_reserved_bytes(block_group, num_bytes, 1,
Yan, Zhengf0486c62010-05-16 10:46:25 -04005208 (data & BTRFS_BLOCK_GROUP_DATA));
5209 if (ret == -EAGAIN) {
5210 btrfs_add_free_space(block_group, offset, num_bytes);
5211 goto loop;
5212 }
Yan Zheng11833d62009-09-11 16:11:19 -04005213
Josef Bacik2552d172009-04-03 10:14:19 -04005214 /* we are all good, lets return */
Yan, Zhengf0486c62010-05-16 10:46:25 -04005215 ins->objectid = search_start;
5216 ins->offset = num_bytes;
5217
5218 if (offset < search_start)
5219 btrfs_add_free_space(block_group, offset,
5220 search_start - offset);
5221 BUG_ON(offset > search_start);
Josef Bacik2552d172009-04-03 10:14:19 -04005222 break;
5223loop:
Josef Bacik0a243252009-09-11 16:11:20 -04005224 failed_cluster_refill = false;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04005225 failed_alloc = false;
Yan, Zhengb742bb822010-05-16 10:46:24 -04005226 BUG_ON(index != get_block_group_index(block_group));
Chris Masonfa9c0d792009-04-03 09:47:43 -04005227 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04005228 }
5229 up_read(&space_info->groups_sem);
Chris Masonf5a31e12008-11-10 11:47:09 -05005230
Yan, Zhengb742bb822010-05-16 10:46:24 -04005231 if (!ins->objectid && ++index < BTRFS_NR_RAID_TYPES)
5232 goto search;
5233
Josef Bacikccf0e722009-11-10 21:23:48 -05005234 /* LOOP_FIND_IDEAL, only search caching/cached bg's, and don't wait for
5235 * for them to make caching progress. Also
5236 * determine the best possible bg to cache
5237 * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking
5238 * caching kthreads as we move along
Josef Bacik817d52f2009-07-13 21:29:25 -04005239 * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching
5240 * LOOP_ALLOC_CHUNK, force a chunk allocation and try again
5241 * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try
5242 * again
Chris Masonfa9c0d792009-04-03 09:47:43 -04005243 */
Josef Bacik817d52f2009-07-13 21:29:25 -04005244 if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE &&
5245 (found_uncached_bg || empty_size || empty_cluster ||
5246 allowed_chunk_alloc)) {
Yan, Zhengb742bb822010-05-16 10:46:24 -04005247 index = 0;
Josef Bacikccf0e722009-11-10 21:23:48 -05005248 if (loop == LOOP_FIND_IDEAL && found_uncached_bg) {
Josef Bacik817d52f2009-07-13 21:29:25 -04005249 found_uncached_bg = false;
Josef Bacikccf0e722009-11-10 21:23:48 -05005250 loop++;
5251 if (!ideal_cache_percent &&
5252 atomic_read(&space_info->caching_threads))
Josef Bacik817d52f2009-07-13 21:29:25 -04005253 goto search;
Josef Bacikccf0e722009-11-10 21:23:48 -05005254
5255 /*
5256 * 1 of the following 2 things have happened so far
5257 *
5258 * 1) We found an ideal block group for caching that
5259 * is mostly full and will cache quickly, so we might
5260 * as well wait for it.
5261 *
5262 * 2) We searched for cached only and we didn't find
5263 * anything, and we didn't start any caching kthreads
5264 * either, so chances are we will loop through and
5265 * start a couple caching kthreads, and then come back
5266 * around and just wait for them. This will be slower
5267 * because we will have 2 caching kthreads reading at
5268 * the same time when we could have just started one
5269 * and waited for it to get far enough to give us an
5270 * allocation, so go ahead and go to the wait caching
5271 * loop.
5272 */
5273 loop = LOOP_CACHING_WAIT;
5274 search_start = ideal_cache_offset;
5275 ideal_cache_percent = 0;
5276 goto ideal_cache;
5277 } else if (loop == LOOP_FIND_IDEAL) {
5278 /*
5279 * Didn't find a uncached bg, wait on anything we find
5280 * next.
5281 */
5282 loop = LOOP_CACHING_WAIT;
5283 goto search;
5284 }
5285
5286 if (loop < LOOP_CACHING_WAIT) {
5287 loop++;
5288 goto search;
Josef Bacik817d52f2009-07-13 21:29:25 -04005289 }
5290
5291 if (loop == LOOP_ALLOC_CHUNK) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04005292 empty_size = 0;
5293 empty_cluster = 0;
5294 }
Chris Mason42e70e72008-11-07 18:17:11 -05005295
Josef Bacik2552d172009-04-03 10:14:19 -04005296 if (allowed_chunk_alloc) {
5297 ret = do_chunk_alloc(trans, root, num_bytes +
5298 2 * 1024 * 1024, data, 1);
Josef Bacik2552d172009-04-03 10:14:19 -04005299 allowed_chunk_alloc = 0;
Josef Bacikccf0e722009-11-10 21:23:48 -05005300 done_chunk_alloc = 1;
5301 } else if (!done_chunk_alloc) {
Josef Bacik2552d172009-04-03 10:14:19 -04005302 space_info->force_alloc = 1;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005303 }
Josef Bacik80eb2342008-10-29 14:49:05 -04005304
Josef Bacik817d52f2009-07-13 21:29:25 -04005305 if (loop < LOOP_NO_EMPTY_SIZE) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04005306 loop++;
Josef Bacik2552d172009-04-03 10:14:19 -04005307 goto search;
Chris Masonfa9c0d792009-04-03 09:47:43 -04005308 }
Josef Bacik2552d172009-04-03 10:14:19 -04005309 ret = -ENOSPC;
5310 } else if (!ins->objectid) {
5311 ret = -ENOSPC;
Chris Masone19caa52007-10-15 16:17:44 -04005312 }
Chris Mason0b86a832008-03-24 15:01:56 -04005313
Josef Bacik80eb2342008-10-29 14:49:05 -04005314 /* we found what we needed */
5315 if (ins->objectid) {
5316 if (!(data & BTRFS_BLOCK_GROUP_DATA))
Yan Zhengd2fb3432008-12-11 16:30:39 -05005317 trans->block_group = block_group->key.objectid;
Josef Bacik80eb2342008-10-29 14:49:05 -04005318
Chris Masonfa9c0d792009-04-03 09:47:43 -04005319 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04005320 ret = 0;
5321 }
Chris Mason0b86a832008-03-24 15:01:56 -04005322
Chris Mason0f70abe2007-02-28 16:46:22 -05005323 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05005324}
Chris Masonec44a352008-04-28 15:29:52 -04005325
Josef Bacik9ed74f22009-09-11 16:12:44 -04005326static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
5327 int dump_block_groups)
Josef Bacik0f9dd462008-09-23 13:14:11 -04005328{
5329 struct btrfs_block_group_cache *cache;
Yan, Zhengb742bb822010-05-16 10:46:24 -04005330 int index = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005331
Josef Bacik9ed74f22009-09-11 16:12:44 -04005332 spin_lock(&info->lock);
Chris Masond3977122009-01-05 21:25:51 -05005333 printk(KERN_INFO "space_info has %llu free, is %sfull\n",
5334 (unsigned long long)(info->total_bytes - info->bytes_used -
Josef Bacik9ed74f22009-09-11 16:12:44 -04005335 info->bytes_pinned - info->bytes_reserved -
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005336 info->bytes_readonly),
Chris Masond3977122009-01-05 21:25:51 -05005337 (info->full) ? "" : "not ");
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005338 printk(KERN_INFO "space_info total=%llu, used=%llu, pinned=%llu, "
5339 "reserved=%llu, may_use=%llu, readonly=%llu\n",
Joel Becker21380932009-04-21 12:38:29 -07005340 (unsigned long long)info->total_bytes,
Josef Bacik9ed74f22009-09-11 16:12:44 -04005341 (unsigned long long)info->bytes_used,
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005342 (unsigned long long)info->bytes_pinned,
5343 (unsigned long long)info->bytes_reserved,
5344 (unsigned long long)info->bytes_may_use,
5345 (unsigned long long)info->bytes_readonly);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005346 spin_unlock(&info->lock);
5347
5348 if (!dump_block_groups)
5349 return;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005350
Josef Bacik80eb2342008-10-29 14:49:05 -04005351 down_read(&info->groups_sem);
Yan, Zhengb742bb822010-05-16 10:46:24 -04005352again:
5353 list_for_each_entry(cache, &info->block_groups[index], list) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04005354 spin_lock(&cache->lock);
Chris Masond3977122009-01-05 21:25:51 -05005355 printk(KERN_INFO "block group %llu has %llu bytes, %llu used "
5356 "%llu pinned %llu reserved\n",
5357 (unsigned long long)cache->key.objectid,
5358 (unsigned long long)cache->key.offset,
5359 (unsigned long long)btrfs_block_group_used(&cache->item),
5360 (unsigned long long)cache->pinned,
5361 (unsigned long long)cache->reserved);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005362 btrfs_dump_free_space(cache, bytes);
5363 spin_unlock(&cache->lock);
5364 }
Yan, Zhengb742bb822010-05-16 10:46:24 -04005365 if (++index < BTRFS_NR_RAID_TYPES)
5366 goto again;
Josef Bacik80eb2342008-10-29 14:49:05 -04005367 up_read(&info->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005368}
Zheng Yane8569812008-09-26 10:05:48 -04005369
Yan Zheng11833d62009-09-11 16:11:19 -04005370int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
5371 struct btrfs_root *root,
5372 u64 num_bytes, u64 min_alloc_size,
5373 u64 empty_size, u64 hint_byte,
5374 u64 search_end, struct btrfs_key *ins,
5375 u64 data)
Chris Masonfec577f2007-02-26 10:40:21 -05005376{
5377 int ret;
Chris Masonfbdc7622007-05-30 10:22:12 -04005378 u64 search_start = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005379
Josef Bacik6a632092009-02-20 11:00:09 -05005380 data = btrfs_get_alloc_profile(root, data);
Chris Mason98d20f62008-04-14 09:46:10 -04005381again:
Chris Mason0ef3e662008-05-24 14:04:53 -04005382 /*
5383 * the only place that sets empty_size is btrfs_realloc_node, which
5384 * is not called recursively on allocations
5385 */
Josef Bacik83d3c962009-12-07 21:45:59 +00005386 if (empty_size || root->ref_cows)
Chris Mason6324fbf2008-03-24 15:01:59 -04005387 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0ef3e662008-05-24 14:04:53 -04005388 num_bytes + 2 * 1024 * 1024, data, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04005389
Chris Masondb945352007-10-15 16:15:53 -04005390 WARN_ON(num_bytes < root->sectorsize);
5391 ret = find_free_extent(trans, root, num_bytes, empty_size,
Yan, Zhengf0486c62010-05-16 10:46:25 -04005392 search_start, search_end, hint_byte,
5393 ins, data);
Chris Mason3b951512008-04-17 11:29:12 -04005394
Chris Mason98d20f62008-04-14 09:46:10 -04005395 if (ret == -ENOSPC && num_bytes > min_alloc_size) {
5396 num_bytes = num_bytes >> 1;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005397 num_bytes = num_bytes & ~(root->sectorsize - 1);
Chris Mason98d20f62008-04-14 09:46:10 -04005398 num_bytes = max(num_bytes, min_alloc_size);
Chris Mason0ef3e662008-05-24 14:04:53 -04005399 do_chunk_alloc(trans, root->fs_info->extent_root,
5400 num_bytes, data, 1);
Chris Mason98d20f62008-04-14 09:46:10 -04005401 goto again;
5402 }
Chris Mason91435652011-02-16 13:10:41 -05005403 if (ret == -ENOSPC && btrfs_test_opt(root, ENOSPC_DEBUG)) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04005404 struct btrfs_space_info *sinfo;
5405
5406 sinfo = __find_space_info(root->fs_info, data);
Chris Masond3977122009-01-05 21:25:51 -05005407 printk(KERN_ERR "btrfs allocation failed flags %llu, "
5408 "wanted %llu\n", (unsigned long long)data,
5409 (unsigned long long)num_bytes);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005410 dump_space_info(sinfo, num_bytes, 1);
Chris Mason925baed2008-06-25 16:01:30 -04005411 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04005412
liubo1abe9b82011-03-24 11:18:59 +00005413 trace_btrfs_reserved_extent_alloc(root, ins->objectid, ins->offset);
5414
Josef Bacik0f9dd462008-09-23 13:14:11 -04005415 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005416}
5417
Chris Mason65b51a02008-08-01 15:11:20 -04005418int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len)
5419{
Josef Bacik0f9dd462008-09-23 13:14:11 -04005420 struct btrfs_block_group_cache *cache;
Liu Hui1f3c79a2009-01-05 15:57:51 -05005421 int ret = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005422
Josef Bacik0f9dd462008-09-23 13:14:11 -04005423 cache = btrfs_lookup_block_group(root->fs_info, start);
5424 if (!cache) {
Chris Masond3977122009-01-05 21:25:51 -05005425 printk(KERN_ERR "Unable to find block group for %llu\n",
5426 (unsigned long long)start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005427 return -ENOSPC;
5428 }
Liu Hui1f3c79a2009-01-05 15:57:51 -05005429
5430 ret = btrfs_discard_extent(root, start, len);
5431
Josef Bacik0f9dd462008-09-23 13:14:11 -04005432 btrfs_add_free_space(cache, start, len);
Li Dongyangb4d00d52011-03-24 10:24:25 +00005433 btrfs_update_reserved_bytes(cache, len, 0, 1);
Chris Masonfa9c0d792009-04-03 09:47:43 -04005434 btrfs_put_block_group(cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04005435
liubo1abe9b82011-03-24 11:18:59 +00005436 trace_btrfs_reserved_extent_free(root, start, len);
5437
Chris Masone6dcd2d2008-07-17 12:53:50 -04005438 return ret;
5439}
5440
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005441static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
5442 struct btrfs_root *root,
5443 u64 parent, u64 root_objectid,
5444 u64 flags, u64 owner, u64 offset,
5445 struct btrfs_key *ins, int ref_mod)
Chris Masone6dcd2d2008-07-17 12:53:50 -04005446{
5447 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005448 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005449 struct btrfs_extent_item *extent_item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005450 struct btrfs_extent_inline_ref *iref;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005451 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005452 struct extent_buffer *leaf;
5453 int type;
5454 u32 size;
Chris Masonf2654de2007-06-26 12:20:46 -04005455
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005456 if (parent > 0)
5457 type = BTRFS_SHARED_DATA_REF_KEY;
5458 else
5459 type = BTRFS_EXTENT_DATA_REF_KEY;
Zheng Yan31840ae2008-09-23 13:14:14 -04005460
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005461 size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type);
Chris Mason7bb86312007-12-11 09:25:06 -05005462
5463 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00005464 if (!path)
5465 return -ENOMEM;
Chris Mason47e4bb92008-02-01 14:51:59 -05005466
Chris Masonb9473432009-03-13 11:00:37 -04005467 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005468 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
5469 ins, size);
Chris Masonccd467d2007-06-28 15:57:36 -04005470 BUG_ON(ret);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005471
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005472 leaf = path->nodes[0];
5473 extent_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason47e4bb92008-02-01 14:51:59 -05005474 struct btrfs_extent_item);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005475 btrfs_set_extent_refs(leaf, extent_item, ref_mod);
5476 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
5477 btrfs_set_extent_flags(leaf, extent_item,
5478 flags | BTRFS_EXTENT_FLAG_DATA);
Chris Mason47e4bb92008-02-01 14:51:59 -05005479
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005480 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
5481 btrfs_set_extent_inline_ref_type(leaf, iref, type);
5482 if (parent > 0) {
5483 struct btrfs_shared_data_ref *ref;
5484 ref = (struct btrfs_shared_data_ref *)(iref + 1);
5485 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
5486 btrfs_set_shared_data_ref_count(leaf, ref, ref_mod);
5487 } else {
5488 struct btrfs_extent_data_ref *ref;
5489 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
5490 btrfs_set_extent_data_ref_root(leaf, ref, root_objectid);
5491 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
5492 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
5493 btrfs_set_extent_data_ref_count(leaf, ref, ref_mod);
5494 }
Chris Mason47e4bb92008-02-01 14:51:59 -05005495
5496 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason7bb86312007-12-11 09:25:06 -05005497 btrfs_free_path(path);
Chris Masonf510cfe2007-10-15 16:14:48 -04005498
Yan, Zhengf0486c62010-05-16 10:46:25 -04005499 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1);
Chris Masonf5947062008-02-04 10:10:13 -05005500 if (ret) {
Chris Masond3977122009-01-05 21:25:51 -05005501 printk(KERN_ERR "btrfs update block group failed for %llu "
5502 "%llu\n", (unsigned long long)ins->objectid,
5503 (unsigned long long)ins->offset);
Chris Masonf5947062008-02-04 10:10:13 -05005504 BUG();
5505 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005506 return ret;
5507}
5508
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005509static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
5510 struct btrfs_root *root,
5511 u64 parent, u64 root_objectid,
5512 u64 flags, struct btrfs_disk_key *key,
5513 int level, struct btrfs_key *ins)
5514{
5515 int ret;
5516 struct btrfs_fs_info *fs_info = root->fs_info;
5517 struct btrfs_extent_item *extent_item;
5518 struct btrfs_tree_block_info *block_info;
5519 struct btrfs_extent_inline_ref *iref;
5520 struct btrfs_path *path;
5521 struct extent_buffer *leaf;
5522 u32 size = sizeof(*extent_item) + sizeof(*block_info) + sizeof(*iref);
5523
5524 path = btrfs_alloc_path();
5525 BUG_ON(!path);
5526
5527 path->leave_spinning = 1;
5528 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
5529 ins, size);
5530 BUG_ON(ret);
5531
5532 leaf = path->nodes[0];
5533 extent_item = btrfs_item_ptr(leaf, path->slots[0],
5534 struct btrfs_extent_item);
5535 btrfs_set_extent_refs(leaf, extent_item, 1);
5536 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
5537 btrfs_set_extent_flags(leaf, extent_item,
5538 flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
5539 block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
5540
5541 btrfs_set_tree_block_key(leaf, block_info, key);
5542 btrfs_set_tree_block_level(leaf, block_info, level);
5543
5544 iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
5545 if (parent > 0) {
5546 BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
5547 btrfs_set_extent_inline_ref_type(leaf, iref,
5548 BTRFS_SHARED_BLOCK_REF_KEY);
5549 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
5550 } else {
5551 btrfs_set_extent_inline_ref_type(leaf, iref,
5552 BTRFS_TREE_BLOCK_REF_KEY);
5553 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
5554 }
5555
5556 btrfs_mark_buffer_dirty(leaf);
5557 btrfs_free_path(path);
5558
Yan, Zhengf0486c62010-05-16 10:46:25 -04005559 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005560 if (ret) {
5561 printk(KERN_ERR "btrfs update block group failed for %llu "
5562 "%llu\n", (unsigned long long)ins->objectid,
5563 (unsigned long long)ins->offset);
5564 BUG();
5565 }
5566 return ret;
5567}
5568
5569int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
5570 struct btrfs_root *root,
5571 u64 root_objectid, u64 owner,
5572 u64 offset, struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04005573{
5574 int ret;
Chris Mason1c2308f82008-09-23 13:14:13 -04005575
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005576 BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID);
Chris Mason56bec292009-03-13 10:10:06 -04005577
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005578 ret = btrfs_add_delayed_data_ref(trans, ins->objectid, ins->offset,
5579 0, root_objectid, owner, offset,
5580 BTRFS_ADD_DELAYED_EXTENT, NULL);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005581 return ret;
5582}
Chris Masone02119d2008-09-05 16:13:11 -04005583
5584/*
5585 * this is used by the tree logging recovery code. It records that
5586 * an extent has been allocated and makes sure to clear the free
5587 * space cache bits as well
5588 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005589int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
5590 struct btrfs_root *root,
5591 u64 root_objectid, u64 owner, u64 offset,
5592 struct btrfs_key *ins)
Chris Masone02119d2008-09-05 16:13:11 -04005593{
5594 int ret;
5595 struct btrfs_block_group_cache *block_group;
Yan Zheng11833d62009-09-11 16:11:19 -04005596 struct btrfs_caching_control *caching_ctl;
5597 u64 start = ins->objectid;
5598 u64 num_bytes = ins->offset;
Chris Masone02119d2008-09-05 16:13:11 -04005599
Chris Masone02119d2008-09-05 16:13:11 -04005600 block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
Josef Bacikb8399de2010-12-08 09:15:11 -05005601 cache_block_group(block_group, trans, NULL, 0);
Yan Zheng11833d62009-09-11 16:11:19 -04005602 caching_ctl = get_caching_control(block_group);
Chris Masone02119d2008-09-05 16:13:11 -04005603
Yan Zheng11833d62009-09-11 16:11:19 -04005604 if (!caching_ctl) {
5605 BUG_ON(!block_group_cache_done(block_group));
5606 ret = btrfs_remove_free_space(block_group, start, num_bytes);
5607 BUG_ON(ret);
5608 } else {
5609 mutex_lock(&caching_ctl->mutex);
5610
5611 if (start >= caching_ctl->progress) {
5612 ret = add_excluded_extent(root, start, num_bytes);
5613 BUG_ON(ret);
5614 } else if (start + num_bytes <= caching_ctl->progress) {
5615 ret = btrfs_remove_free_space(block_group,
5616 start, num_bytes);
5617 BUG_ON(ret);
5618 } else {
5619 num_bytes = caching_ctl->progress - start;
5620 ret = btrfs_remove_free_space(block_group,
5621 start, num_bytes);
5622 BUG_ON(ret);
5623
5624 start = caching_ctl->progress;
5625 num_bytes = ins->objectid + ins->offset -
5626 caching_ctl->progress;
5627 ret = add_excluded_extent(root, start, num_bytes);
5628 BUG_ON(ret);
5629 }
5630
5631 mutex_unlock(&caching_ctl->mutex);
5632 put_caching_control(caching_ctl);
5633 }
5634
Li Dongyangb4d00d52011-03-24 10:24:25 +00005635 ret = btrfs_update_reserved_bytes(block_group, ins->offset, 1, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005636 BUG_ON(ret);
Chris Masonfa9c0d792009-04-03 09:47:43 -04005637 btrfs_put_block_group(block_group);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005638 ret = alloc_reserved_file_extent(trans, root, 0, root_objectid,
5639 0, owner, offset, ins, 1);
Chris Masone02119d2008-09-05 16:13:11 -04005640 return ret;
5641}
5642
Chris Mason65b51a02008-08-01 15:11:20 -04005643struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
5644 struct btrfs_root *root,
Chris Mason4008c042009-02-12 14:09:45 -05005645 u64 bytenr, u32 blocksize,
5646 int level)
Chris Mason65b51a02008-08-01 15:11:20 -04005647{
5648 struct extent_buffer *buf;
5649
5650 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
5651 if (!buf)
5652 return ERR_PTR(-ENOMEM);
5653 btrfs_set_header_generation(buf, trans->transid);
Chris Mason4008c042009-02-12 14:09:45 -05005654 btrfs_set_buffer_lockdep_class(buf, level);
Chris Mason65b51a02008-08-01 15:11:20 -04005655 btrfs_tree_lock(buf);
5656 clean_tree_block(trans, root, buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05005657
5658 btrfs_set_lock_blocking(buf);
Chris Mason65b51a02008-08-01 15:11:20 -04005659 btrfs_set_buffer_uptodate(buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05005660
Chris Masond0c803c2008-09-11 16:17:57 -04005661 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
Yan, Zheng8cef4e12009-11-12 09:33:26 +00005662 /*
5663 * we allow two log transactions at a time, use different
5664 * EXENT bit to differentiate dirty pages.
5665 */
5666 if (root->log_transid % 2 == 0)
5667 set_extent_dirty(&root->dirty_log_pages, buf->start,
5668 buf->start + buf->len - 1, GFP_NOFS);
5669 else
5670 set_extent_new(&root->dirty_log_pages, buf->start,
5671 buf->start + buf->len - 1, GFP_NOFS);
Chris Masond0c803c2008-09-11 16:17:57 -04005672 } else {
5673 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
5674 buf->start + buf->len - 1, GFP_NOFS);
5675 }
Chris Mason65b51a02008-08-01 15:11:20 -04005676 trans->blocks_used++;
Chris Masonb4ce94d2009-02-04 09:25:08 -05005677 /* this returns a buffer locked for blocking */
Chris Mason65b51a02008-08-01 15:11:20 -04005678 return buf;
5679}
5680
Yan, Zhengf0486c62010-05-16 10:46:25 -04005681static struct btrfs_block_rsv *
5682use_block_rsv(struct btrfs_trans_handle *trans,
5683 struct btrfs_root *root, u32 blocksize)
5684{
5685 struct btrfs_block_rsv *block_rsv;
Josef Bacik68a82272011-01-24 21:43:20 +00005686 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005687 int ret;
5688
5689 block_rsv = get_block_rsv(trans, root);
5690
5691 if (block_rsv->size == 0) {
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005692 ret = reserve_metadata_bytes(trans, root, block_rsv,
5693 blocksize, 0);
Josef Bacik68a82272011-01-24 21:43:20 +00005694 /*
5695 * If we couldn't reserve metadata bytes try and use some from
5696 * the global reserve.
5697 */
5698 if (ret && block_rsv != global_rsv) {
5699 ret = block_rsv_use_bytes(global_rsv, blocksize);
5700 if (!ret)
5701 return global_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005702 return ERR_PTR(ret);
Josef Bacik68a82272011-01-24 21:43:20 +00005703 } else if (ret) {
5704 return ERR_PTR(ret);
5705 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04005706 return block_rsv;
5707 }
5708
5709 ret = block_rsv_use_bytes(block_rsv, blocksize);
5710 if (!ret)
5711 return block_rsv;
Josef Bacik68a82272011-01-24 21:43:20 +00005712 if (ret) {
5713 WARN_ON(1);
5714 ret = reserve_metadata_bytes(trans, root, block_rsv, blocksize,
5715 0);
5716 if (!ret) {
5717 spin_lock(&block_rsv->lock);
5718 block_rsv->size += blocksize;
5719 spin_unlock(&block_rsv->lock);
5720 return block_rsv;
5721 } else if (ret && block_rsv != global_rsv) {
5722 ret = block_rsv_use_bytes(global_rsv, blocksize);
5723 if (!ret)
5724 return global_rsv;
5725 }
5726 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04005727
Yan, Zhengf0486c62010-05-16 10:46:25 -04005728 return ERR_PTR(-ENOSPC);
5729}
5730
5731static void unuse_block_rsv(struct btrfs_block_rsv *block_rsv, u32 blocksize)
5732{
5733 block_rsv_add_bytes(block_rsv, blocksize, 0);
5734 block_rsv_release_bytes(block_rsv, NULL, 0);
5735}
5736
Chris Masonfec577f2007-02-26 10:40:21 -05005737/*
Yan, Zhengf0486c62010-05-16 10:46:25 -04005738 * finds a free extent and does all the dirty work required for allocation
5739 * returns the key for the extent through ins, and a tree buffer for
5740 * the first block of the extent through buf.
5741 *
Chris Masonfec577f2007-02-26 10:40:21 -05005742 * returns the tree buffer or NULL.
5743 */
Chris Mason5f39d392007-10-15 16:14:19 -04005744struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005745 struct btrfs_root *root, u32 blocksize,
5746 u64 parent, u64 root_objectid,
5747 struct btrfs_disk_key *key, int level,
5748 u64 hint, u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05005749{
Chris Masone2fa7222007-03-12 16:22:34 -04005750 struct btrfs_key ins;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005751 struct btrfs_block_rsv *block_rsv;
Chris Mason5f39d392007-10-15 16:14:19 -04005752 struct extent_buffer *buf;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005753 u64 flags = 0;
5754 int ret;
Chris Masonfec577f2007-02-26 10:40:21 -05005755
Yan, Zhengf0486c62010-05-16 10:46:25 -04005756
5757 block_rsv = use_block_rsv(trans, root, blocksize);
5758 if (IS_ERR(block_rsv))
5759 return ERR_CAST(block_rsv);
5760
5761 ret = btrfs_reserve_extent(trans, root, blocksize, blocksize,
5762 empty_size, hint, (u64)-1, &ins, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05005763 if (ret) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04005764 unuse_block_rsv(block_rsv, blocksize);
Chris Mason54aa1f42007-06-22 14:16:25 -04005765 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05005766 }
Chris Mason55c69072008-01-09 15:55:33 -05005767
Chris Mason4008c042009-02-12 14:09:45 -05005768 buf = btrfs_init_new_buffer(trans, root, ins.objectid,
5769 blocksize, level);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005770 BUG_ON(IS_ERR(buf));
5771
5772 if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
5773 if (parent == 0)
5774 parent = ins.objectid;
5775 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
5776 } else
5777 BUG_ON(parent > 0);
5778
5779 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
5780 struct btrfs_delayed_extent_op *extent_op;
5781 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
5782 BUG_ON(!extent_op);
5783 if (key)
5784 memcpy(&extent_op->key, key, sizeof(extent_op->key));
5785 else
5786 memset(&extent_op->key, 0, sizeof(extent_op->key));
5787 extent_op->flags_to_set = flags;
5788 extent_op->update_key = 1;
5789 extent_op->update_flags = 1;
5790 extent_op->is_data = 0;
5791
5792 ret = btrfs_add_delayed_tree_ref(trans, ins.objectid,
5793 ins.offset, parent, root_objectid,
5794 level, BTRFS_ADD_DELAYED_EXTENT,
5795 extent_op);
5796 BUG_ON(ret);
5797 }
Chris Masonfec577f2007-02-26 10:40:21 -05005798 return buf;
5799}
Chris Masona28ec192007-03-06 20:08:01 -05005800
Yan Zheng2c47e6052009-06-27 21:07:35 -04005801struct walk_control {
5802 u64 refs[BTRFS_MAX_LEVEL];
5803 u64 flags[BTRFS_MAX_LEVEL];
5804 struct btrfs_key update_progress;
5805 int stage;
5806 int level;
5807 int shared_level;
5808 int update_ref;
5809 int keep_locks;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005810 int reada_slot;
5811 int reada_count;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005812};
5813
5814#define DROP_REFERENCE 1
5815#define UPDATE_BACKREF 2
5816
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005817static noinline void reada_walk_down(struct btrfs_trans_handle *trans,
5818 struct btrfs_root *root,
5819 struct walk_control *wc,
5820 struct btrfs_path *path)
5821{
5822 u64 bytenr;
5823 u64 generation;
5824 u64 refs;
Yan, Zheng94fcca92009-10-09 09:25:16 -04005825 u64 flags;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005826 u32 nritems;
5827 u32 blocksize;
5828 struct btrfs_key key;
5829 struct extent_buffer *eb;
5830 int ret;
5831 int slot;
5832 int nread = 0;
5833
5834 if (path->slots[wc->level] < wc->reada_slot) {
5835 wc->reada_count = wc->reada_count * 2 / 3;
5836 wc->reada_count = max(wc->reada_count, 2);
5837 } else {
5838 wc->reada_count = wc->reada_count * 3 / 2;
5839 wc->reada_count = min_t(int, wc->reada_count,
5840 BTRFS_NODEPTRS_PER_BLOCK(root));
5841 }
5842
5843 eb = path->nodes[wc->level];
5844 nritems = btrfs_header_nritems(eb);
5845 blocksize = btrfs_level_size(root, wc->level - 1);
5846
5847 for (slot = path->slots[wc->level]; slot < nritems; slot++) {
5848 if (nread >= wc->reada_count)
5849 break;
5850
5851 cond_resched();
5852 bytenr = btrfs_node_blockptr(eb, slot);
5853 generation = btrfs_node_ptr_generation(eb, slot);
5854
5855 if (slot == path->slots[wc->level])
5856 goto reada;
5857
5858 if (wc->stage == UPDATE_BACKREF &&
5859 generation <= root->root_key.offset)
5860 continue;
5861
Yan, Zheng94fcca92009-10-09 09:25:16 -04005862 /* We don't lock the tree block, it's OK to be racy here */
5863 ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize,
5864 &refs, &flags);
5865 BUG_ON(ret);
5866 BUG_ON(refs == 0);
5867
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005868 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005869 if (refs == 1)
5870 goto reada;
5871
Yan, Zheng94fcca92009-10-09 09:25:16 -04005872 if (wc->level == 1 &&
5873 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
5874 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005875 if (!wc->update_ref ||
5876 generation <= root->root_key.offset)
5877 continue;
5878 btrfs_node_key_to_cpu(eb, &key, slot);
5879 ret = btrfs_comp_cpu_keys(&key,
5880 &wc->update_progress);
5881 if (ret < 0)
5882 continue;
Yan, Zheng94fcca92009-10-09 09:25:16 -04005883 } else {
5884 if (wc->level == 1 &&
5885 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
5886 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005887 }
5888reada:
5889 ret = readahead_tree_block(root, bytenr, blocksize,
5890 generation);
5891 if (ret)
5892 break;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005893 nread++;
5894 }
5895 wc->reada_slot = slot;
5896}
5897
Chris Mason9aca1d52007-03-13 11:09:37 -04005898/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04005899 * hepler to process tree block while walking down the tree.
5900 *
Yan Zheng2c47e6052009-06-27 21:07:35 -04005901 * when wc->stage == UPDATE_BACKREF, this function updates
5902 * back refs for pointers in the block.
5903 *
5904 * NOTE: return value 1 means we should stop walking down.
Yan Zhengf82d02d2008-10-29 14:49:05 -04005905 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04005906static noinline int walk_down_proc(struct btrfs_trans_handle *trans,
5907 struct btrfs_root *root,
5908 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04005909 struct walk_control *wc, int lookup_info)
Yan Zheng2c47e6052009-06-27 21:07:35 -04005910{
5911 int level = wc->level;
5912 struct extent_buffer *eb = path->nodes[level];
Yan Zheng2c47e6052009-06-27 21:07:35 -04005913 u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF;
5914 int ret;
5915
5916 if (wc->stage == UPDATE_BACKREF &&
5917 btrfs_header_owner(eb) != root->root_key.objectid)
5918 return 1;
5919
5920 /*
5921 * when reference count of tree block is 1, it won't increase
5922 * again. once full backref flag is set, we never clear it.
5923 */
Yan, Zheng94fcca92009-10-09 09:25:16 -04005924 if (lookup_info &&
5925 ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) ||
5926 (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04005927 BUG_ON(!path->locks[level]);
5928 ret = btrfs_lookup_extent_info(trans, root,
5929 eb->start, eb->len,
5930 &wc->refs[level],
5931 &wc->flags[level]);
5932 BUG_ON(ret);
5933 BUG_ON(wc->refs[level] == 0);
5934 }
5935
Yan Zheng2c47e6052009-06-27 21:07:35 -04005936 if (wc->stage == DROP_REFERENCE) {
5937 if (wc->refs[level] > 1)
5938 return 1;
5939
5940 if (path->locks[level] && !wc->keep_locks) {
5941 btrfs_tree_unlock(eb);
5942 path->locks[level] = 0;
5943 }
5944 return 0;
5945 }
5946
5947 /* wc->stage == UPDATE_BACKREF */
5948 if (!(wc->flags[level] & flag)) {
5949 BUG_ON(!path->locks[level]);
5950 ret = btrfs_inc_ref(trans, root, eb, 1);
5951 BUG_ON(ret);
5952 ret = btrfs_dec_ref(trans, root, eb, 0);
5953 BUG_ON(ret);
5954 ret = btrfs_set_disk_extent_flags(trans, root, eb->start,
5955 eb->len, flag, 0);
5956 BUG_ON(ret);
5957 wc->flags[level] |= flag;
5958 }
5959
5960 /*
5961 * the block is shared by multiple trees, so it's not good to
5962 * keep the tree lock
5963 */
5964 if (path->locks[level] && level > 0) {
5965 btrfs_tree_unlock(eb);
5966 path->locks[level] = 0;
5967 }
5968 return 0;
5969}
5970
5971/*
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005972 * hepler to process tree block pointer.
5973 *
5974 * when wc->stage == DROP_REFERENCE, this function checks
5975 * reference count of the block pointed to. if the block
5976 * is shared and we need update back refs for the subtree
5977 * rooted at the block, this function changes wc->stage to
5978 * UPDATE_BACKREF. if the block is shared and there is no
5979 * need to update back, this function drops the reference
5980 * to the block.
5981 *
5982 * NOTE: return value 1 means we should stop walking down.
5983 */
5984static noinline int do_walk_down(struct btrfs_trans_handle *trans,
5985 struct btrfs_root *root,
5986 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04005987 struct walk_control *wc, int *lookup_info)
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005988{
5989 u64 bytenr;
5990 u64 generation;
5991 u64 parent;
5992 u32 blocksize;
5993 struct btrfs_key key;
5994 struct extent_buffer *next;
5995 int level = wc->level;
5996 int reada = 0;
5997 int ret = 0;
5998
5999 generation = btrfs_node_ptr_generation(path->nodes[level],
6000 path->slots[level]);
6001 /*
6002 * if the lower level block was created before the snapshot
6003 * was created, we know there is no need to update back refs
6004 * for the subtree
6005 */
6006 if (wc->stage == UPDATE_BACKREF &&
Yan, Zheng94fcca92009-10-09 09:25:16 -04006007 generation <= root->root_key.offset) {
6008 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006009 return 1;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006010 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006011
6012 bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]);
6013 blocksize = btrfs_level_size(root, level - 1);
6014
6015 next = btrfs_find_tree_block(root, bytenr, blocksize);
6016 if (!next) {
6017 next = btrfs_find_create_tree_block(root, bytenr, blocksize);
Miao Xie90d2c51d2010-03-25 12:37:12 +00006018 if (!next)
6019 return -ENOMEM;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006020 reada = 1;
6021 }
6022 btrfs_tree_lock(next);
6023 btrfs_set_lock_blocking(next);
6024
Yan, Zheng94fcca92009-10-09 09:25:16 -04006025 ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize,
6026 &wc->refs[level - 1],
6027 &wc->flags[level - 1]);
6028 BUG_ON(ret);
6029 BUG_ON(wc->refs[level - 1] == 0);
6030 *lookup_info = 0;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006031
Yan, Zheng94fcca92009-10-09 09:25:16 -04006032 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006033 if (wc->refs[level - 1] > 1) {
Yan, Zheng94fcca92009-10-09 09:25:16 -04006034 if (level == 1 &&
6035 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
6036 goto skip;
6037
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006038 if (!wc->update_ref ||
6039 generation <= root->root_key.offset)
6040 goto skip;
6041
6042 btrfs_node_key_to_cpu(path->nodes[level], &key,
6043 path->slots[level]);
6044 ret = btrfs_comp_cpu_keys(&key, &wc->update_progress);
6045 if (ret < 0)
6046 goto skip;
6047
6048 wc->stage = UPDATE_BACKREF;
6049 wc->shared_level = level - 1;
6050 }
Yan, Zheng94fcca92009-10-09 09:25:16 -04006051 } else {
6052 if (level == 1 &&
6053 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
6054 goto skip;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006055 }
6056
6057 if (!btrfs_buffer_uptodate(next, generation)) {
6058 btrfs_tree_unlock(next);
6059 free_extent_buffer(next);
6060 next = NULL;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006061 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006062 }
6063
6064 if (!next) {
6065 if (reada && level == 1)
6066 reada_walk_down(trans, root, wc, path);
6067 next = read_tree_block(root, bytenr, blocksize, generation);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00006068 if (!next)
6069 return -EIO;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006070 btrfs_tree_lock(next);
6071 btrfs_set_lock_blocking(next);
6072 }
6073
6074 level--;
6075 BUG_ON(level != btrfs_header_level(next));
6076 path->nodes[level] = next;
6077 path->slots[level] = 0;
6078 path->locks[level] = 1;
6079 wc->level = level;
6080 if (wc->level == 1)
6081 wc->reada_slot = 0;
6082 return 0;
6083skip:
6084 wc->refs[level - 1] = 0;
6085 wc->flags[level - 1] = 0;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006086 if (wc->stage == DROP_REFERENCE) {
6087 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
6088 parent = path->nodes[level]->start;
6089 } else {
6090 BUG_ON(root->root_key.objectid !=
6091 btrfs_header_owner(path->nodes[level]));
6092 parent = 0;
6093 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006094
Yan, Zheng94fcca92009-10-09 09:25:16 -04006095 ret = btrfs_free_extent(trans, root, bytenr, blocksize, parent,
6096 root->root_key.objectid, level - 1, 0);
6097 BUG_ON(ret);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006098 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006099 btrfs_tree_unlock(next);
6100 free_extent_buffer(next);
Yan, Zheng94fcca92009-10-09 09:25:16 -04006101 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006102 return 1;
6103}
6104
6105/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04006106 * hepler to process tree block while walking up the tree.
6107 *
6108 * when wc->stage == DROP_REFERENCE, this function drops
6109 * reference count on the block.
6110 *
6111 * when wc->stage == UPDATE_BACKREF, this function changes
6112 * wc->stage back to DROP_REFERENCE if we changed wc->stage
6113 * to UPDATE_BACKREF previously while processing the block.
6114 *
6115 * NOTE: return value 1 means we should stop walking up.
6116 */
6117static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
6118 struct btrfs_root *root,
6119 struct btrfs_path *path,
6120 struct walk_control *wc)
6121{
Yan, Zhengf0486c62010-05-16 10:46:25 -04006122 int ret;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006123 int level = wc->level;
6124 struct extent_buffer *eb = path->nodes[level];
6125 u64 parent = 0;
6126
6127 if (wc->stage == UPDATE_BACKREF) {
6128 BUG_ON(wc->shared_level < level);
6129 if (level < wc->shared_level)
6130 goto out;
6131
Yan Zheng2c47e6052009-06-27 21:07:35 -04006132 ret = find_next_key(path, level + 1, &wc->update_progress);
6133 if (ret > 0)
6134 wc->update_ref = 0;
6135
6136 wc->stage = DROP_REFERENCE;
6137 wc->shared_level = -1;
6138 path->slots[level] = 0;
6139
6140 /*
6141 * check reference count again if the block isn't locked.
6142 * we should start walking down the tree again if reference
6143 * count is one.
6144 */
6145 if (!path->locks[level]) {
6146 BUG_ON(level == 0);
6147 btrfs_tree_lock(eb);
6148 btrfs_set_lock_blocking(eb);
6149 path->locks[level] = 1;
6150
6151 ret = btrfs_lookup_extent_info(trans, root,
6152 eb->start, eb->len,
6153 &wc->refs[level],
6154 &wc->flags[level]);
6155 BUG_ON(ret);
6156 BUG_ON(wc->refs[level] == 0);
6157 if (wc->refs[level] == 1) {
6158 btrfs_tree_unlock(eb);
6159 path->locks[level] = 0;
6160 return 1;
6161 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04006162 }
6163 }
6164
6165 /* wc->stage == DROP_REFERENCE */
6166 BUG_ON(wc->refs[level] > 1 && !path->locks[level]);
6167
6168 if (wc->refs[level] == 1) {
6169 if (level == 0) {
6170 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
6171 ret = btrfs_dec_ref(trans, root, eb, 1);
6172 else
6173 ret = btrfs_dec_ref(trans, root, eb, 0);
6174 BUG_ON(ret);
6175 }
6176 /* make block locked assertion in clean_tree_block happy */
6177 if (!path->locks[level] &&
6178 btrfs_header_generation(eb) == trans->transid) {
6179 btrfs_tree_lock(eb);
6180 btrfs_set_lock_blocking(eb);
6181 path->locks[level] = 1;
6182 }
6183 clean_tree_block(trans, root, eb);
6184 }
6185
6186 if (eb == root->node) {
6187 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
6188 parent = eb->start;
6189 else
6190 BUG_ON(root->root_key.objectid !=
6191 btrfs_header_owner(eb));
6192 } else {
6193 if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
6194 parent = path->nodes[level + 1]->start;
6195 else
6196 BUG_ON(root->root_key.objectid !=
6197 btrfs_header_owner(path->nodes[level + 1]));
6198 }
6199
Yan, Zhengf0486c62010-05-16 10:46:25 -04006200 btrfs_free_tree_block(trans, root, eb, parent, wc->refs[level] == 1);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006201out:
6202 wc->refs[level] = 0;
6203 wc->flags[level] = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006204 return 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006205}
6206
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006207static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
6208 struct btrfs_root *root,
Yan Zheng2c47e6052009-06-27 21:07:35 -04006209 struct btrfs_path *path,
6210 struct walk_control *wc)
Yan Zhengf82d02d2008-10-29 14:49:05 -04006211{
Yan Zheng2c47e6052009-06-27 21:07:35 -04006212 int level = wc->level;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006213 int lookup_info = 1;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006214 int ret;
6215
Yan Zheng2c47e6052009-06-27 21:07:35 -04006216 while (level >= 0) {
Yan, Zheng94fcca92009-10-09 09:25:16 -04006217 ret = walk_down_proc(trans, root, path, wc, lookup_info);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006218 if (ret > 0)
Yan Zhengf82d02d2008-10-29 14:49:05 -04006219 break;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006220
Yan Zheng2c47e6052009-06-27 21:07:35 -04006221 if (level == 0)
6222 break;
6223
Yan, Zheng7a7965f2010-02-01 02:41:17 +00006224 if (path->slots[level] >=
6225 btrfs_header_nritems(path->nodes[level]))
6226 break;
6227
Yan, Zheng94fcca92009-10-09 09:25:16 -04006228 ret = do_walk_down(trans, root, path, wc, &lookup_info);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006229 if (ret > 0) {
6230 path->slots[level]++;
6231 continue;
Miao Xie90d2c51d2010-03-25 12:37:12 +00006232 } else if (ret < 0)
6233 return ret;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006234 level = wc->level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006235 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04006236 return 0;
6237}
6238
Chris Masond3977122009-01-05 21:25:51 -05006239static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05006240 struct btrfs_root *root,
Yan Zhengf82d02d2008-10-29 14:49:05 -04006241 struct btrfs_path *path,
Yan Zheng2c47e6052009-06-27 21:07:35 -04006242 struct walk_control *wc, int max_level)
Chris Mason20524f02007-03-10 06:35:47 -05006243{
Yan Zheng2c47e6052009-06-27 21:07:35 -04006244 int level = wc->level;
Chris Mason20524f02007-03-10 06:35:47 -05006245 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04006246
Yan Zheng2c47e6052009-06-27 21:07:35 -04006247 path->slots[level] = btrfs_header_nritems(path->nodes[level]);
6248 while (level < max_level && path->nodes[level]) {
6249 wc->level = level;
6250 if (path->slots[level] + 1 <
6251 btrfs_header_nritems(path->nodes[level])) {
6252 path->slots[level]++;
Chris Mason20524f02007-03-10 06:35:47 -05006253 return 0;
6254 } else {
Yan Zheng2c47e6052009-06-27 21:07:35 -04006255 ret = walk_up_proc(trans, root, path, wc);
6256 if (ret > 0)
6257 return 0;
Chris Masonbd56b302009-02-04 09:27:02 -05006258
Yan Zheng2c47e6052009-06-27 21:07:35 -04006259 if (path->locks[level]) {
6260 btrfs_tree_unlock(path->nodes[level]);
6261 path->locks[level] = 0;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006262 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04006263 free_extent_buffer(path->nodes[level]);
6264 path->nodes[level] = NULL;
6265 level++;
Chris Mason20524f02007-03-10 06:35:47 -05006266 }
6267 }
6268 return 1;
6269}
6270
Chris Mason9aca1d52007-03-13 11:09:37 -04006271/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04006272 * drop a subvolume tree.
6273 *
6274 * this function traverses the tree freeing any blocks that only
6275 * referenced by the tree.
6276 *
6277 * when a shared tree block is found. this function decreases its
6278 * reference count by one. if update_ref is true, this function
6279 * also make sure backrefs for the shared block and all lower level
6280 * blocks are properly updated.
Chris Mason9aca1d52007-03-13 11:09:37 -04006281 */
Yan, Zheng3fd0a552010-05-16 10:49:59 -04006282int btrfs_drop_snapshot(struct btrfs_root *root,
6283 struct btrfs_block_rsv *block_rsv, int update_ref)
Chris Mason20524f02007-03-10 06:35:47 -05006284{
Chris Mason5caf2a02007-04-02 11:20:42 -04006285 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006286 struct btrfs_trans_handle *trans;
6287 struct btrfs_root *tree_root = root->fs_info->tree_root;
Chris Mason9f3a7422007-08-07 15:52:19 -04006288 struct btrfs_root_item *root_item = &root->root_item;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006289 struct walk_control *wc;
6290 struct btrfs_key key;
6291 int err = 0;
6292 int ret;
6293 int level;
Chris Mason20524f02007-03-10 06:35:47 -05006294
Chris Mason5caf2a02007-04-02 11:20:42 -04006295 path = btrfs_alloc_path();
6296 BUG_ON(!path);
Chris Mason20524f02007-03-10 06:35:47 -05006297
Yan Zheng2c47e6052009-06-27 21:07:35 -04006298 wc = kzalloc(sizeof(*wc), GFP_NOFS);
6299 BUG_ON(!wc);
6300
Yan, Zhenga22285a2010-05-16 10:48:46 -04006301 trans = btrfs_start_transaction(tree_root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00006302 BUG_ON(IS_ERR(trans));
6303
Yan, Zheng3fd0a552010-05-16 10:49:59 -04006304 if (block_rsv)
6305 trans->block_rsv = block_rsv;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006306
Chris Mason9f3a7422007-08-07 15:52:19 -04006307 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04006308 level = btrfs_header_level(root->node);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006309 path->nodes[level] = btrfs_lock_root_node(root);
6310 btrfs_set_lock_blocking(path->nodes[level]);
Chris Mason9f3a7422007-08-07 15:52:19 -04006311 path->slots[level] = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006312 path->locks[level] = 1;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006313 memset(&wc->update_progress, 0,
6314 sizeof(wc->update_progress));
Chris Mason9f3a7422007-08-07 15:52:19 -04006315 } else {
Chris Mason9f3a7422007-08-07 15:52:19 -04006316 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006317 memcpy(&wc->update_progress, &key,
6318 sizeof(wc->update_progress));
6319
Chris Mason6702ed42007-08-07 16:15:09 -04006320 level = root_item->drop_level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006321 BUG_ON(level == 0);
Chris Mason6702ed42007-08-07 16:15:09 -04006322 path->lowest_level = level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006323 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6324 path->lowest_level = 0;
6325 if (ret < 0) {
6326 err = ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04006327 goto out;
6328 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006329 WARN_ON(ret > 0);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006330
Chris Mason7d9eb122008-07-08 14:19:17 -04006331 /*
6332 * unlock our path, this is safe because only this
6333 * function is allowed to delete this snapshot
6334 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006335 btrfs_unlock_up_safe(path, 0);
Chris Mason9aca1d52007-03-13 11:09:37 -04006336
Yan Zheng2c47e6052009-06-27 21:07:35 -04006337 level = btrfs_header_level(root->node);
6338 while (1) {
6339 btrfs_tree_lock(path->nodes[level]);
6340 btrfs_set_lock_blocking(path->nodes[level]);
6341
6342 ret = btrfs_lookup_extent_info(trans, root,
6343 path->nodes[level]->start,
6344 path->nodes[level]->len,
6345 &wc->refs[level],
6346 &wc->flags[level]);
6347 BUG_ON(ret);
6348 BUG_ON(wc->refs[level] == 0);
6349
6350 if (level == root_item->drop_level)
6351 break;
6352
6353 btrfs_tree_unlock(path->nodes[level]);
6354 WARN_ON(wc->refs[level] != 1);
6355 level--;
6356 }
6357 }
6358
6359 wc->level = level;
6360 wc->shared_level = -1;
6361 wc->stage = DROP_REFERENCE;
6362 wc->update_ref = update_ref;
6363 wc->keep_locks = 0;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006364 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006365
6366 while (1) {
6367 ret = walk_down_tree(trans, root, path, wc);
6368 if (ret < 0) {
6369 err = ret;
Chris Masone7a84562008-06-25 16:01:31 -04006370 break;
6371 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04006372
6373 ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL);
6374 if (ret < 0) {
6375 err = ret;
6376 break;
6377 }
6378
6379 if (ret > 0) {
6380 BUG_ON(wc->stage != DROP_REFERENCE);
6381 break;
6382 }
6383
6384 if (wc->stage == DROP_REFERENCE) {
6385 level = wc->level;
6386 btrfs_node_key(path->nodes[level],
6387 &root_item->drop_progress,
6388 path->slots[level]);
6389 root_item->drop_level = level;
6390 }
6391
6392 BUG_ON(wc->level == 0);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04006393 if (btrfs_should_end_transaction(trans, tree_root)) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04006394 ret = btrfs_update_root(trans, tree_root,
6395 &root->root_key,
6396 root_item);
6397 BUG_ON(ret);
6398
Yan, Zheng3fd0a552010-05-16 10:49:59 -04006399 btrfs_end_transaction_throttle(trans, tree_root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006400 trans = btrfs_start_transaction(tree_root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00006401 BUG_ON(IS_ERR(trans));
Yan, Zheng3fd0a552010-05-16 10:49:59 -04006402 if (block_rsv)
6403 trans->block_rsv = block_rsv;
Chris Masonc3e69d52009-03-13 10:17:05 -04006404 }
Chris Mason20524f02007-03-10 06:35:47 -05006405 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04006406 btrfs_release_path(root, path);
6407 BUG_ON(err);
6408
6409 ret = btrfs_del_root(trans, tree_root, &root->root_key);
6410 BUG_ON(ret);
6411
Yan, Zheng76dda932009-09-21 16:00:26 -04006412 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
6413 ret = btrfs_find_last_root(tree_root, root->root_key.objectid,
6414 NULL, NULL);
6415 BUG_ON(ret < 0);
6416 if (ret > 0) {
Josef Bacik84cd9482010-12-08 12:24:01 -05006417 /* if we fail to delete the orphan item this time
6418 * around, it'll get picked up the next time.
6419 *
6420 * The most common failure here is just -ENOENT.
6421 */
6422 btrfs_del_orphan_item(trans, tree_root,
6423 root->root_key.objectid);
Yan, Zheng76dda932009-09-21 16:00:26 -04006424 }
6425 }
6426
6427 if (root->in_radix) {
6428 btrfs_free_fs_root(tree_root->fs_info, root);
6429 } else {
6430 free_extent_buffer(root->node);
6431 free_extent_buffer(root->commit_root);
6432 kfree(root);
6433 }
Chris Mason9f3a7422007-08-07 15:52:19 -04006434out:
Yan, Zheng3fd0a552010-05-16 10:49:59 -04006435 btrfs_end_transaction_throttle(trans, tree_root);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006436 kfree(wc);
Chris Mason5caf2a02007-04-02 11:20:42 -04006437 btrfs_free_path(path);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006438 return err;
Chris Mason20524f02007-03-10 06:35:47 -05006439}
Chris Mason9078a3e2007-04-26 16:46:15 -04006440
Yan Zheng2c47e6052009-06-27 21:07:35 -04006441/*
6442 * drop subtree rooted at tree block 'node'.
6443 *
6444 * NOTE: this function will unlock and release tree block 'node'
6445 */
Yan Zhengf82d02d2008-10-29 14:49:05 -04006446int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
6447 struct btrfs_root *root,
6448 struct extent_buffer *node,
6449 struct extent_buffer *parent)
6450{
6451 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006452 struct walk_control *wc;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006453 int level;
6454 int parent_level;
6455 int ret = 0;
6456 int wret;
6457
Yan Zheng2c47e6052009-06-27 21:07:35 -04006458 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
6459
Yan Zhengf82d02d2008-10-29 14:49:05 -04006460 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00006461 if (!path)
6462 return -ENOMEM;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006463
Yan Zheng2c47e6052009-06-27 21:07:35 -04006464 wc = kzalloc(sizeof(*wc), GFP_NOFS);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00006465 if (!wc) {
6466 btrfs_free_path(path);
6467 return -ENOMEM;
6468 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04006469
Chris Masonb9447ef82009-03-09 11:45:38 -04006470 btrfs_assert_tree_locked(parent);
Yan Zhengf82d02d2008-10-29 14:49:05 -04006471 parent_level = btrfs_header_level(parent);
6472 extent_buffer_get(parent);
6473 path->nodes[parent_level] = parent;
6474 path->slots[parent_level] = btrfs_header_nritems(parent);
6475
Chris Masonb9447ef82009-03-09 11:45:38 -04006476 btrfs_assert_tree_locked(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04006477 level = btrfs_header_level(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04006478 path->nodes[level] = node;
6479 path->slots[level] = 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006480 path->locks[level] = 1;
6481
6482 wc->refs[parent_level] = 1;
6483 wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF;
6484 wc->level = level;
6485 wc->shared_level = -1;
6486 wc->stage = DROP_REFERENCE;
6487 wc->update_ref = 0;
6488 wc->keep_locks = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006489 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
Yan Zhengf82d02d2008-10-29 14:49:05 -04006490
6491 while (1) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04006492 wret = walk_down_tree(trans, root, path, wc);
6493 if (wret < 0) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04006494 ret = wret;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006495 break;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006496 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04006497
Yan Zheng2c47e6052009-06-27 21:07:35 -04006498 wret = walk_up_tree(trans, root, path, wc, parent_level);
Yan Zhengf82d02d2008-10-29 14:49:05 -04006499 if (wret < 0)
6500 ret = wret;
6501 if (wret != 0)
6502 break;
6503 }
6504
Yan Zheng2c47e6052009-06-27 21:07:35 -04006505 kfree(wc);
Yan Zhengf82d02d2008-10-29 14:49:05 -04006506 btrfs_free_path(path);
6507 return ret;
6508}
6509
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006510#if 0
Chris Mason8e7bf942008-04-28 09:02:36 -04006511static unsigned long calc_ra(unsigned long start, unsigned long last,
6512 unsigned long nr)
6513{
6514 return min(last, start + nr - 1);
6515}
6516
Chris Masond3977122009-01-05 21:25:51 -05006517static noinline int relocate_inode_pages(struct inode *inode, u64 start,
Chris Mason98ed5172008-01-03 10:01:48 -05006518 u64 len)
Chris Masonedbd8d42007-12-21 16:27:24 -05006519{
6520 u64 page_start;
6521 u64 page_end;
Zheng Yan1a40e232008-09-26 10:09:34 -04006522 unsigned long first_index;
Chris Masonedbd8d42007-12-21 16:27:24 -05006523 unsigned long last_index;
Chris Masonedbd8d42007-12-21 16:27:24 -05006524 unsigned long i;
6525 struct page *page;
Chris Masond1310b22008-01-24 16:13:08 -05006526 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason4313b392008-01-03 09:08:48 -05006527 struct file_ra_state *ra;
Chris Mason3eaa2882008-07-24 11:57:52 -04006528 struct btrfs_ordered_extent *ordered;
Zheng Yan1a40e232008-09-26 10:09:34 -04006529 unsigned int total_read = 0;
6530 unsigned int total_dirty = 0;
6531 int ret = 0;
Chris Mason4313b392008-01-03 09:08:48 -05006532
6533 ra = kzalloc(sizeof(*ra), GFP_NOFS);
Tsutomu Itoh5df67082011-02-01 09:17:35 +00006534 if (!ra)
6535 return -ENOMEM;
Chris Masonedbd8d42007-12-21 16:27:24 -05006536
6537 mutex_lock(&inode->i_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04006538 first_index = start >> PAGE_CACHE_SHIFT;
Chris Masonedbd8d42007-12-21 16:27:24 -05006539 last_index = (start + len - 1) >> PAGE_CACHE_SHIFT;
6540
Zheng Yan1a40e232008-09-26 10:09:34 -04006541 /* make sure the dirty trick played by the caller work */
6542 ret = invalidate_inode_pages2_range(inode->i_mapping,
6543 first_index, last_index);
6544 if (ret)
6545 goto out_unlock;
Chris Mason8e7bf942008-04-28 09:02:36 -04006546
Chris Mason4313b392008-01-03 09:08:48 -05006547 file_ra_state_init(ra, inode->i_mapping);
Chris Masonedbd8d42007-12-21 16:27:24 -05006548
Zheng Yan1a40e232008-09-26 10:09:34 -04006549 for (i = first_index ; i <= last_index; i++) {
6550 if (total_read % ra->ra_pages == 0) {
Chris Mason8e7bf942008-04-28 09:02:36 -04006551 btrfs_force_ra(inode->i_mapping, ra, NULL, i,
Zheng Yan1a40e232008-09-26 10:09:34 -04006552 calc_ra(i, last_index, ra->ra_pages));
Chris Mason8e7bf942008-04-28 09:02:36 -04006553 }
6554 total_read++;
Chris Mason3eaa2882008-07-24 11:57:52 -04006555again:
6556 if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode))
Zheng Yan1a40e232008-09-26 10:09:34 -04006557 BUG_ON(1);
Chris Masonedbd8d42007-12-21 16:27:24 -05006558 page = grab_cache_page(inode->i_mapping, i);
Chris Masona061fc82008-05-07 11:43:44 -04006559 if (!page) {
Zheng Yan1a40e232008-09-26 10:09:34 -04006560 ret = -ENOMEM;
Chris Masonedbd8d42007-12-21 16:27:24 -05006561 goto out_unlock;
Chris Masona061fc82008-05-07 11:43:44 -04006562 }
Chris Masonedbd8d42007-12-21 16:27:24 -05006563 if (!PageUptodate(page)) {
6564 btrfs_readpage(NULL, page);
6565 lock_page(page);
6566 if (!PageUptodate(page)) {
6567 unlock_page(page);
6568 page_cache_release(page);
Zheng Yan1a40e232008-09-26 10:09:34 -04006569 ret = -EIO;
Chris Masonedbd8d42007-12-21 16:27:24 -05006570 goto out_unlock;
6571 }
6572 }
Chris Masonec44a352008-04-28 15:29:52 -04006573 wait_on_page_writeback(page);
Chris Mason3eaa2882008-07-24 11:57:52 -04006574
Chris Masonedbd8d42007-12-21 16:27:24 -05006575 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
6576 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05006577 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05006578
Chris Mason3eaa2882008-07-24 11:57:52 -04006579 ordered = btrfs_lookup_ordered_extent(inode, page_start);
6580 if (ordered) {
6581 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
6582 unlock_page(page);
6583 page_cache_release(page);
6584 btrfs_start_ordered_extent(inode, ordered, 1);
6585 btrfs_put_ordered_extent(ordered);
6586 goto again;
6587 }
6588 set_page_extent_mapped(page);
6589
Zheng Yan1a40e232008-09-26 10:09:34 -04006590 if (i == first_index)
6591 set_extent_bits(io_tree, page_start, page_end,
6592 EXTENT_BOUNDARY, GFP_NOFS);
Yan Zheng1f80e4d2008-12-19 10:59:04 -05006593 btrfs_set_extent_delalloc(inode, page_start, page_end);
Zheng Yan1a40e232008-09-26 10:09:34 -04006594
Chris Masona061fc82008-05-07 11:43:44 -04006595 set_page_dirty(page);
Zheng Yan1a40e232008-09-26 10:09:34 -04006596 total_dirty++;
Chris Masonedbd8d42007-12-21 16:27:24 -05006597
Chris Masond1310b22008-01-24 16:13:08 -05006598 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05006599 unlock_page(page);
6600 page_cache_release(page);
6601 }
6602
6603out_unlock:
Chris Masonec44a352008-04-28 15:29:52 -04006604 kfree(ra);
Chris Masonedbd8d42007-12-21 16:27:24 -05006605 mutex_unlock(&inode->i_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04006606 balance_dirty_pages_ratelimited_nr(inode->i_mapping, total_dirty);
Chris Masonbf4ef672008-05-08 13:26:18 -04006607 return ret;
6608}
6609
Chris Masond3977122009-01-05 21:25:51 -05006610static noinline int relocate_data_extent(struct inode *reloc_inode,
Zheng Yan1a40e232008-09-26 10:09:34 -04006611 struct btrfs_key *extent_key,
6612 u64 offset)
Chris Masonedbd8d42007-12-21 16:27:24 -05006613{
Zheng Yan1a40e232008-09-26 10:09:34 -04006614 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
6615 struct extent_map_tree *em_tree = &BTRFS_I(reloc_inode)->extent_tree;
6616 struct extent_map *em;
Yan Zheng66435582008-10-30 14:19:50 -04006617 u64 start = extent_key->objectid - offset;
6618 u64 end = start + extent_key->offset - 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04006619
6620 em = alloc_extent_map(GFP_NOFS);
Tsutomu Itohc26a9202011-02-14 00:45:29 +00006621 BUG_ON(!em);
Zheng Yan1a40e232008-09-26 10:09:34 -04006622
Yan Zheng66435582008-10-30 14:19:50 -04006623 em->start = start;
Zheng Yan1a40e232008-09-26 10:09:34 -04006624 em->len = extent_key->offset;
Chris Masonc8b97812008-10-29 14:49:59 -04006625 em->block_len = extent_key->offset;
Zheng Yan1a40e232008-09-26 10:09:34 -04006626 em->block_start = extent_key->objectid;
6627 em->bdev = root->fs_info->fs_devices->latest_bdev;
6628 set_bit(EXTENT_FLAG_PINNED, &em->flags);
6629
6630 /* setup extent map to cheat btrfs_readpage */
Yan Zheng66435582008-10-30 14:19:50 -04006631 lock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04006632 while (1) {
6633 int ret;
Chris Mason890871b2009-09-02 16:24:52 -04006634 write_lock(&em_tree->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04006635 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04006636 write_unlock(&em_tree->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04006637 if (ret != -EEXIST) {
6638 free_extent_map(em);
6639 break;
6640 }
Yan Zheng66435582008-10-30 14:19:50 -04006641 btrfs_drop_extent_cache(reloc_inode, start, end, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04006642 }
Yan Zheng66435582008-10-30 14:19:50 -04006643 unlock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04006644
Yan Zheng66435582008-10-30 14:19:50 -04006645 return relocate_inode_pages(reloc_inode, start, extent_key->offset);
Zheng Yan1a40e232008-09-26 10:09:34 -04006646}
6647
6648struct btrfs_ref_path {
6649 u64 extent_start;
6650 u64 nodes[BTRFS_MAX_LEVEL];
6651 u64 root_objectid;
6652 u64 root_generation;
6653 u64 owner_objectid;
Zheng Yan1a40e232008-09-26 10:09:34 -04006654 u32 num_refs;
6655 int lowest_level;
6656 int current_level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006657 int shared_level;
6658
6659 struct btrfs_key node_keys[BTRFS_MAX_LEVEL];
6660 u64 new_nodes[BTRFS_MAX_LEVEL];
Zheng Yan1a40e232008-09-26 10:09:34 -04006661};
6662
6663struct disk_extent {
Chris Masonc8b97812008-10-29 14:49:59 -04006664 u64 ram_bytes;
Zheng Yan1a40e232008-09-26 10:09:34 -04006665 u64 disk_bytenr;
6666 u64 disk_num_bytes;
6667 u64 offset;
6668 u64 num_bytes;
Chris Masonc8b97812008-10-29 14:49:59 -04006669 u8 compression;
6670 u8 encryption;
6671 u16 other_encoding;
Zheng Yan1a40e232008-09-26 10:09:34 -04006672};
6673
6674static int is_cowonly_root(u64 root_objectid)
6675{
6676 if (root_objectid == BTRFS_ROOT_TREE_OBJECTID ||
6677 root_objectid == BTRFS_EXTENT_TREE_OBJECTID ||
6678 root_objectid == BTRFS_CHUNK_TREE_OBJECTID ||
6679 root_objectid == BTRFS_DEV_TREE_OBJECTID ||
Yan Zheng0403e472008-12-10 20:32:51 -05006680 root_objectid == BTRFS_TREE_LOG_OBJECTID ||
6681 root_objectid == BTRFS_CSUM_TREE_OBJECTID)
Zheng Yan1a40e232008-09-26 10:09:34 -04006682 return 1;
6683 return 0;
6684}
6685
Chris Masond3977122009-01-05 21:25:51 -05006686static noinline int __next_ref_path(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04006687 struct btrfs_root *extent_root,
6688 struct btrfs_ref_path *ref_path,
6689 int first_time)
6690{
6691 struct extent_buffer *leaf;
6692 struct btrfs_path *path;
Chris Mason4313b392008-01-03 09:08:48 -05006693 struct btrfs_extent_ref *ref;
Chris Masonedbd8d42007-12-21 16:27:24 -05006694 struct btrfs_key key;
6695 struct btrfs_key found_key;
Zheng Yan1a40e232008-09-26 10:09:34 -04006696 u64 bytenr;
Chris Masonedbd8d42007-12-21 16:27:24 -05006697 u32 nritems;
Zheng Yan1a40e232008-09-26 10:09:34 -04006698 int level;
6699 int ret = 1;
Chris Masonedbd8d42007-12-21 16:27:24 -05006700
Zheng Yan1a40e232008-09-26 10:09:34 -04006701 path = btrfs_alloc_path();
6702 if (!path)
6703 return -ENOMEM;
6704
Zheng Yan1a40e232008-09-26 10:09:34 -04006705 if (first_time) {
6706 ref_path->lowest_level = -1;
6707 ref_path->current_level = -1;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006708 ref_path->shared_level = -1;
Zheng Yan1a40e232008-09-26 10:09:34 -04006709 goto walk_up;
Chris Masona061fc82008-05-07 11:43:44 -04006710 }
Zheng Yan1a40e232008-09-26 10:09:34 -04006711walk_down:
6712 level = ref_path->current_level - 1;
6713 while (level >= -1) {
6714 u64 parent;
6715 if (level < ref_path->lowest_level)
6716 break;
Chris Masonedbd8d42007-12-21 16:27:24 -05006717
Chris Masond3977122009-01-05 21:25:51 -05006718 if (level >= 0)
Zheng Yan1a40e232008-09-26 10:09:34 -04006719 bytenr = ref_path->nodes[level];
Chris Masond3977122009-01-05 21:25:51 -05006720 else
Zheng Yan1a40e232008-09-26 10:09:34 -04006721 bytenr = ref_path->extent_start;
Zheng Yan1a40e232008-09-26 10:09:34 -04006722 BUG_ON(bytenr == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05006723
Zheng Yan1a40e232008-09-26 10:09:34 -04006724 parent = ref_path->nodes[level + 1];
6725 ref_path->nodes[level + 1] = 0;
6726 ref_path->current_level = level;
6727 BUG_ON(parent == 0);
6728
6729 key.objectid = bytenr;
6730 key.offset = parent + 1;
6731 key.type = BTRFS_EXTENT_REF_KEY;
6732
6733 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05006734 if (ret < 0)
6735 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04006736 BUG_ON(ret == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05006737
Chris Masonedbd8d42007-12-21 16:27:24 -05006738 leaf = path->nodes[0];
6739 nritems = btrfs_header_nritems(leaf);
Zheng Yan1a40e232008-09-26 10:09:34 -04006740 if (path->slots[0] >= nritems) {
Chris Masona061fc82008-05-07 11:43:44 -04006741 ret = btrfs_next_leaf(extent_root, path);
Chris Masona061fc82008-05-07 11:43:44 -04006742 if (ret < 0)
6743 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04006744 if (ret > 0)
6745 goto next;
Chris Masonbf4ef672008-05-08 13:26:18 -04006746 leaf = path->nodes[0];
Chris Masona061fc82008-05-07 11:43:44 -04006747 }
Chris Masonedbd8d42007-12-21 16:27:24 -05006748
6749 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Zheng Yan1a40e232008-09-26 10:09:34 -04006750 if (found_key.objectid == bytenr &&
Yan Zhengf82d02d2008-10-29 14:49:05 -04006751 found_key.type == BTRFS_EXTENT_REF_KEY) {
6752 if (level < ref_path->shared_level)
6753 ref_path->shared_level = level;
Zheng Yan1a40e232008-09-26 10:09:34 -04006754 goto found;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006755 }
Zheng Yan1a40e232008-09-26 10:09:34 -04006756next:
6757 level--;
6758 btrfs_release_path(extent_root, path);
Yan Zhengd899e052008-10-30 14:25:28 -04006759 cond_resched();
Zheng Yan1a40e232008-09-26 10:09:34 -04006760 }
6761 /* reached lowest level */
6762 ret = 1;
6763 goto out;
6764walk_up:
6765 level = ref_path->current_level;
6766 while (level < BTRFS_MAX_LEVEL - 1) {
6767 u64 ref_objectid;
Chris Masond3977122009-01-05 21:25:51 -05006768
6769 if (level >= 0)
Zheng Yan1a40e232008-09-26 10:09:34 -04006770 bytenr = ref_path->nodes[level];
Chris Masond3977122009-01-05 21:25:51 -05006771 else
Zheng Yan1a40e232008-09-26 10:09:34 -04006772 bytenr = ref_path->extent_start;
Chris Masond3977122009-01-05 21:25:51 -05006773
Zheng Yan1a40e232008-09-26 10:09:34 -04006774 BUG_ON(bytenr == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05006775
Zheng Yan1a40e232008-09-26 10:09:34 -04006776 key.objectid = bytenr;
6777 key.offset = 0;
6778 key.type = BTRFS_EXTENT_REF_KEY;
Chris Masonedbd8d42007-12-21 16:27:24 -05006779
Zheng Yan1a40e232008-09-26 10:09:34 -04006780 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
6781 if (ret < 0)
Chris Masonedbd8d42007-12-21 16:27:24 -05006782 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04006783
6784 leaf = path->nodes[0];
6785 nritems = btrfs_header_nritems(leaf);
6786 if (path->slots[0] >= nritems) {
6787 ret = btrfs_next_leaf(extent_root, path);
6788 if (ret < 0)
6789 goto out;
6790 if (ret > 0) {
6791 /* the extent was freed by someone */
6792 if (ref_path->lowest_level == level)
6793 goto out;
6794 btrfs_release_path(extent_root, path);
6795 goto walk_down;
6796 }
6797 leaf = path->nodes[0];
6798 }
6799
6800 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6801 if (found_key.objectid != bytenr ||
6802 found_key.type != BTRFS_EXTENT_REF_KEY) {
6803 /* the extent was freed by someone */
6804 if (ref_path->lowest_level == level) {
6805 ret = 1;
6806 goto out;
6807 }
6808 btrfs_release_path(extent_root, path);
6809 goto walk_down;
6810 }
6811found:
6812 ref = btrfs_item_ptr(leaf, path->slots[0],
6813 struct btrfs_extent_ref);
6814 ref_objectid = btrfs_ref_objectid(leaf, ref);
6815 if (ref_objectid < BTRFS_FIRST_FREE_OBJECTID) {
6816 if (first_time) {
6817 level = (int)ref_objectid;
6818 BUG_ON(level >= BTRFS_MAX_LEVEL);
6819 ref_path->lowest_level = level;
6820 ref_path->current_level = level;
6821 ref_path->nodes[level] = bytenr;
6822 } else {
6823 WARN_ON(ref_objectid != level);
6824 }
6825 } else {
6826 WARN_ON(level != -1);
6827 }
6828 first_time = 0;
6829
6830 if (ref_path->lowest_level == level) {
6831 ref_path->owner_objectid = ref_objectid;
Zheng Yan1a40e232008-09-26 10:09:34 -04006832 ref_path->num_refs = btrfs_ref_num_refs(leaf, ref);
6833 }
6834
6835 /*
6836 * the block is tree root or the block isn't in reference
6837 * counted tree.
6838 */
6839 if (found_key.objectid == found_key.offset ||
6840 is_cowonly_root(btrfs_ref_root(leaf, ref))) {
6841 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
6842 ref_path->root_generation =
6843 btrfs_ref_generation(leaf, ref);
6844 if (level < 0) {
6845 /* special reference from the tree log */
6846 ref_path->nodes[0] = found_key.offset;
6847 ref_path->current_level = 0;
6848 }
6849 ret = 0;
6850 goto out;
6851 }
6852
6853 level++;
6854 BUG_ON(ref_path->nodes[level] != 0);
6855 ref_path->nodes[level] = found_key.offset;
6856 ref_path->current_level = level;
6857
6858 /*
6859 * the reference was created in the running transaction,
6860 * no need to continue walking up.
6861 */
6862 if (btrfs_ref_generation(leaf, ref) == trans->transid) {
6863 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
6864 ref_path->root_generation =
6865 btrfs_ref_generation(leaf, ref);
6866 ret = 0;
6867 goto out;
6868 }
6869
6870 btrfs_release_path(extent_root, path);
Yan Zhengd899e052008-10-30 14:25:28 -04006871 cond_resched();
Zheng Yan1a40e232008-09-26 10:09:34 -04006872 }
6873 /* reached max tree level, but no tree root found. */
6874 BUG();
6875out:
Zheng Yan1a40e232008-09-26 10:09:34 -04006876 btrfs_free_path(path);
6877 return ret;
6878}
6879
6880static int btrfs_first_ref_path(struct btrfs_trans_handle *trans,
6881 struct btrfs_root *extent_root,
6882 struct btrfs_ref_path *ref_path,
6883 u64 extent_start)
6884{
6885 memset(ref_path, 0, sizeof(*ref_path));
6886 ref_path->extent_start = extent_start;
6887
6888 return __next_ref_path(trans, extent_root, ref_path, 1);
6889}
6890
6891static int btrfs_next_ref_path(struct btrfs_trans_handle *trans,
6892 struct btrfs_root *extent_root,
6893 struct btrfs_ref_path *ref_path)
6894{
6895 return __next_ref_path(trans, extent_root, ref_path, 0);
6896}
6897
Chris Masond3977122009-01-05 21:25:51 -05006898static noinline int get_new_locations(struct inode *reloc_inode,
Zheng Yan1a40e232008-09-26 10:09:34 -04006899 struct btrfs_key *extent_key,
6900 u64 offset, int no_fragment,
6901 struct disk_extent **extents,
6902 int *nr_extents)
6903{
6904 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
6905 struct btrfs_path *path;
6906 struct btrfs_file_extent_item *fi;
6907 struct extent_buffer *leaf;
6908 struct disk_extent *exts = *extents;
6909 struct btrfs_key found_key;
6910 u64 cur_pos;
6911 u64 last_byte;
6912 u32 nritems;
6913 int nr = 0;
6914 int max = *nr_extents;
6915 int ret;
6916
6917 WARN_ON(!no_fragment && *extents);
6918 if (!exts) {
6919 max = 1;
6920 exts = kmalloc(sizeof(*exts) * max, GFP_NOFS);
6921 if (!exts)
6922 return -ENOMEM;
6923 }
6924
6925 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00006926 if (!path) {
6927 if (exts != *extents)
6928 kfree(exts);
6929 return -ENOMEM;
6930 }
Zheng Yan1a40e232008-09-26 10:09:34 -04006931
6932 cur_pos = extent_key->objectid - offset;
6933 last_byte = extent_key->objectid + extent_key->offset;
6934 ret = btrfs_lookup_file_extent(NULL, root, path, reloc_inode->i_ino,
6935 cur_pos, 0);
6936 if (ret < 0)
6937 goto out;
6938 if (ret > 0) {
6939 ret = -ENOENT;
6940 goto out;
6941 }
6942
6943 while (1) {
6944 leaf = path->nodes[0];
6945 nritems = btrfs_header_nritems(leaf);
6946 if (path->slots[0] >= nritems) {
6947 ret = btrfs_next_leaf(root, path);
6948 if (ret < 0)
6949 goto out;
6950 if (ret > 0)
6951 break;
6952 leaf = path->nodes[0];
6953 }
6954
6955 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6956 if (found_key.offset != cur_pos ||
6957 found_key.type != BTRFS_EXTENT_DATA_KEY ||
6958 found_key.objectid != reloc_inode->i_ino)
6959 break;
6960
6961 fi = btrfs_item_ptr(leaf, path->slots[0],
6962 struct btrfs_file_extent_item);
6963 if (btrfs_file_extent_type(leaf, fi) !=
6964 BTRFS_FILE_EXTENT_REG ||
6965 btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
6966 break;
6967
6968 if (nr == max) {
6969 struct disk_extent *old = exts;
6970 max *= 2;
6971 exts = kzalloc(sizeof(*exts) * max, GFP_NOFS);
6972 memcpy(exts, old, sizeof(*exts) * nr);
6973 if (old != *extents)
6974 kfree(old);
6975 }
6976
6977 exts[nr].disk_bytenr =
6978 btrfs_file_extent_disk_bytenr(leaf, fi);
6979 exts[nr].disk_num_bytes =
6980 btrfs_file_extent_disk_num_bytes(leaf, fi);
6981 exts[nr].offset = btrfs_file_extent_offset(leaf, fi);
6982 exts[nr].num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
Chris Masonc8b97812008-10-29 14:49:59 -04006983 exts[nr].ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
6984 exts[nr].compression = btrfs_file_extent_compression(leaf, fi);
6985 exts[nr].encryption = btrfs_file_extent_encryption(leaf, fi);
6986 exts[nr].other_encoding = btrfs_file_extent_other_encoding(leaf,
6987 fi);
Yan Zhengd899e052008-10-30 14:25:28 -04006988 BUG_ON(exts[nr].offset > 0);
6989 BUG_ON(exts[nr].compression || exts[nr].encryption);
6990 BUG_ON(exts[nr].num_bytes != exts[nr].disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04006991
6992 cur_pos += exts[nr].num_bytes;
6993 nr++;
6994
6995 if (cur_pos + offset >= last_byte)
6996 break;
6997
6998 if (no_fragment) {
6999 ret = 1;
7000 goto out;
7001 }
7002 path->slots[0]++;
7003 }
7004
Yan Zheng1f80e4d2008-12-19 10:59:04 -05007005 BUG_ON(cur_pos + offset > last_byte);
Zheng Yan1a40e232008-09-26 10:09:34 -04007006 if (cur_pos + offset < last_byte) {
7007 ret = -ENOENT;
7008 goto out;
Chris Masonedbd8d42007-12-21 16:27:24 -05007009 }
7010 ret = 0;
7011out:
Zheng Yan1a40e232008-09-26 10:09:34 -04007012 btrfs_free_path(path);
7013 if (ret) {
7014 if (exts != *extents)
7015 kfree(exts);
7016 } else {
7017 *extents = exts;
7018 *nr_extents = nr;
7019 }
7020 return ret;
7021}
7022
Chris Masond3977122009-01-05 21:25:51 -05007023static noinline int replace_one_extent(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04007024 struct btrfs_root *root,
7025 struct btrfs_path *path,
7026 struct btrfs_key *extent_key,
7027 struct btrfs_key *leaf_key,
7028 struct btrfs_ref_path *ref_path,
7029 struct disk_extent *new_extents,
7030 int nr_extents)
7031{
7032 struct extent_buffer *leaf;
7033 struct btrfs_file_extent_item *fi;
7034 struct inode *inode = NULL;
7035 struct btrfs_key key;
7036 u64 lock_start = 0;
7037 u64 lock_end = 0;
7038 u64 num_bytes;
7039 u64 ext_offset;
Yan Zheng86288a12009-01-21 10:49:16 -05007040 u64 search_end = (u64)-1;
Zheng Yan1a40e232008-09-26 10:09:34 -04007041 u32 nritems;
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04007042 int nr_scaned = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04007043 int extent_locked = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04007044 int extent_type;
Zheng Yan1a40e232008-09-26 10:09:34 -04007045 int ret;
7046
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04007047 memcpy(&key, leaf_key, sizeof(key));
Zheng Yan1a40e232008-09-26 10:09:34 -04007048 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04007049 if (key.objectid < ref_path->owner_objectid ||
7050 (key.objectid == ref_path->owner_objectid &&
7051 key.type < BTRFS_EXTENT_DATA_KEY)) {
7052 key.objectid = ref_path->owner_objectid;
7053 key.type = BTRFS_EXTENT_DATA_KEY;
7054 key.offset = 0;
7055 }
Zheng Yan1a40e232008-09-26 10:09:34 -04007056 }
7057
7058 while (1) {
7059 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
7060 if (ret < 0)
7061 goto out;
7062
7063 leaf = path->nodes[0];
7064 nritems = btrfs_header_nritems(leaf);
7065next:
7066 if (extent_locked && ret > 0) {
7067 /*
7068 * the file extent item was modified by someone
7069 * before the extent got locked.
7070 */
Zheng Yan1a40e232008-09-26 10:09:34 -04007071 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
7072 lock_end, GFP_NOFS);
7073 extent_locked = 0;
7074 }
7075
7076 if (path->slots[0] >= nritems) {
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04007077 if (++nr_scaned > 2)
Zheng Yan1a40e232008-09-26 10:09:34 -04007078 break;
7079
7080 BUG_ON(extent_locked);
7081 ret = btrfs_next_leaf(root, path);
7082 if (ret < 0)
7083 goto out;
7084 if (ret > 0)
7085 break;
7086 leaf = path->nodes[0];
7087 nritems = btrfs_header_nritems(leaf);
7088 }
7089
7090 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
7091
7092 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
7093 if ((key.objectid > ref_path->owner_objectid) ||
7094 (key.objectid == ref_path->owner_objectid &&
7095 key.type > BTRFS_EXTENT_DATA_KEY) ||
Yan Zheng86288a12009-01-21 10:49:16 -05007096 key.offset >= search_end)
Zheng Yan1a40e232008-09-26 10:09:34 -04007097 break;
7098 }
7099
7100 if (inode && key.objectid != inode->i_ino) {
7101 BUG_ON(extent_locked);
7102 btrfs_release_path(root, path);
7103 mutex_unlock(&inode->i_mutex);
7104 iput(inode);
7105 inode = NULL;
7106 continue;
7107 }
7108
7109 if (key.type != BTRFS_EXTENT_DATA_KEY) {
7110 path->slots[0]++;
7111 ret = 1;
7112 goto next;
7113 }
7114 fi = btrfs_item_ptr(leaf, path->slots[0],
7115 struct btrfs_file_extent_item);
Yan Zhengd899e052008-10-30 14:25:28 -04007116 extent_type = btrfs_file_extent_type(leaf, fi);
7117 if ((extent_type != BTRFS_FILE_EXTENT_REG &&
7118 extent_type != BTRFS_FILE_EXTENT_PREALLOC) ||
Zheng Yan1a40e232008-09-26 10:09:34 -04007119 (btrfs_file_extent_disk_bytenr(leaf, fi) !=
7120 extent_key->objectid)) {
7121 path->slots[0]++;
7122 ret = 1;
7123 goto next;
7124 }
7125
7126 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
7127 ext_offset = btrfs_file_extent_offset(leaf, fi);
7128
Yan Zheng86288a12009-01-21 10:49:16 -05007129 if (search_end == (u64)-1) {
7130 search_end = key.offset - ext_offset +
7131 btrfs_file_extent_ram_bytes(leaf, fi);
7132 }
Zheng Yan1a40e232008-09-26 10:09:34 -04007133
7134 if (!extent_locked) {
7135 lock_start = key.offset;
7136 lock_end = lock_start + num_bytes - 1;
7137 } else {
Yan Zheng66435582008-10-30 14:19:50 -04007138 if (lock_start > key.offset ||
7139 lock_end + 1 < key.offset + num_bytes) {
7140 unlock_extent(&BTRFS_I(inode)->io_tree,
7141 lock_start, lock_end, GFP_NOFS);
7142 extent_locked = 0;
7143 }
Zheng Yan1a40e232008-09-26 10:09:34 -04007144 }
7145
7146 if (!inode) {
7147 btrfs_release_path(root, path);
7148
7149 inode = btrfs_iget_locked(root->fs_info->sb,
7150 key.objectid, root);
7151 if (inode->i_state & I_NEW) {
7152 BTRFS_I(inode)->root = root;
7153 BTRFS_I(inode)->location.objectid =
7154 key.objectid;
7155 BTRFS_I(inode)->location.type =
7156 BTRFS_INODE_ITEM_KEY;
7157 BTRFS_I(inode)->location.offset = 0;
7158 btrfs_read_locked_inode(inode);
7159 unlock_new_inode(inode);
7160 }
7161 /*
7162 * some code call btrfs_commit_transaction while
7163 * holding the i_mutex, so we can't use mutex_lock
7164 * here.
7165 */
7166 if (is_bad_inode(inode) ||
7167 !mutex_trylock(&inode->i_mutex)) {
7168 iput(inode);
7169 inode = NULL;
7170 key.offset = (u64)-1;
7171 goto skip;
7172 }
7173 }
7174
7175 if (!extent_locked) {
7176 struct btrfs_ordered_extent *ordered;
7177
7178 btrfs_release_path(root, path);
7179
7180 lock_extent(&BTRFS_I(inode)->io_tree, lock_start,
7181 lock_end, GFP_NOFS);
7182 ordered = btrfs_lookup_first_ordered_extent(inode,
7183 lock_end);
7184 if (ordered &&
7185 ordered->file_offset <= lock_end &&
7186 ordered->file_offset + ordered->len > lock_start) {
7187 unlock_extent(&BTRFS_I(inode)->io_tree,
7188 lock_start, lock_end, GFP_NOFS);
7189 btrfs_start_ordered_extent(inode, ordered, 1);
7190 btrfs_put_ordered_extent(ordered);
7191 key.offset += num_bytes;
7192 goto skip;
7193 }
7194 if (ordered)
7195 btrfs_put_ordered_extent(ordered);
7196
Zheng Yan1a40e232008-09-26 10:09:34 -04007197 extent_locked = 1;
7198 continue;
7199 }
7200
7201 if (nr_extents == 1) {
7202 /* update extent pointer in place */
Zheng Yan1a40e232008-09-26 10:09:34 -04007203 btrfs_set_file_extent_disk_bytenr(leaf, fi,
7204 new_extents[0].disk_bytenr);
7205 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
7206 new_extents[0].disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04007207 btrfs_mark_buffer_dirty(leaf);
7208
7209 btrfs_drop_extent_cache(inode, key.offset,
7210 key.offset + num_bytes - 1, 0);
7211
7212 ret = btrfs_inc_extent_ref(trans, root,
7213 new_extents[0].disk_bytenr,
7214 new_extents[0].disk_num_bytes,
7215 leaf->start,
7216 root->root_key.objectid,
7217 trans->transid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04007218 key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04007219 BUG_ON(ret);
7220
7221 ret = btrfs_free_extent(trans, root,
7222 extent_key->objectid,
7223 extent_key->offset,
7224 leaf->start,
7225 btrfs_header_owner(leaf),
7226 btrfs_header_generation(leaf),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04007227 key.objectid, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04007228 BUG_ON(ret);
7229
7230 btrfs_release_path(root, path);
7231 key.offset += num_bytes;
7232 } else {
Yan Zhengd899e052008-10-30 14:25:28 -04007233 BUG_ON(1);
7234#if 0
Zheng Yan1a40e232008-09-26 10:09:34 -04007235 u64 alloc_hint;
7236 u64 extent_len;
7237 int i;
7238 /*
7239 * drop old extent pointer at first, then insert the
7240 * new pointers one bye one
7241 */
7242 btrfs_release_path(root, path);
7243 ret = btrfs_drop_extents(trans, root, inode, key.offset,
7244 key.offset + num_bytes,
7245 key.offset, &alloc_hint);
7246 BUG_ON(ret);
7247
7248 for (i = 0; i < nr_extents; i++) {
7249 if (ext_offset >= new_extents[i].num_bytes) {
7250 ext_offset -= new_extents[i].num_bytes;
7251 continue;
7252 }
7253 extent_len = min(new_extents[i].num_bytes -
7254 ext_offset, num_bytes);
7255
7256 ret = btrfs_insert_empty_item(trans, root,
7257 path, &key,
7258 sizeof(*fi));
7259 BUG_ON(ret);
7260
7261 leaf = path->nodes[0];
7262 fi = btrfs_item_ptr(leaf, path->slots[0],
7263 struct btrfs_file_extent_item);
7264 btrfs_set_file_extent_generation(leaf, fi,
7265 trans->transid);
7266 btrfs_set_file_extent_type(leaf, fi,
7267 BTRFS_FILE_EXTENT_REG);
7268 btrfs_set_file_extent_disk_bytenr(leaf, fi,
7269 new_extents[i].disk_bytenr);
7270 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
7271 new_extents[i].disk_num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -04007272 btrfs_set_file_extent_ram_bytes(leaf, fi,
7273 new_extents[i].ram_bytes);
7274
7275 btrfs_set_file_extent_compression(leaf, fi,
7276 new_extents[i].compression);
7277 btrfs_set_file_extent_encryption(leaf, fi,
7278 new_extents[i].encryption);
7279 btrfs_set_file_extent_other_encoding(leaf, fi,
7280 new_extents[i].other_encoding);
7281
Zheng Yan1a40e232008-09-26 10:09:34 -04007282 btrfs_set_file_extent_num_bytes(leaf, fi,
7283 extent_len);
7284 ext_offset += new_extents[i].offset;
7285 btrfs_set_file_extent_offset(leaf, fi,
7286 ext_offset);
7287 btrfs_mark_buffer_dirty(leaf);
7288
7289 btrfs_drop_extent_cache(inode, key.offset,
7290 key.offset + extent_len - 1, 0);
7291
7292 ret = btrfs_inc_extent_ref(trans, root,
7293 new_extents[i].disk_bytenr,
7294 new_extents[i].disk_num_bytes,
7295 leaf->start,
7296 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04007297 trans->transid, key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04007298 BUG_ON(ret);
7299 btrfs_release_path(root, path);
7300
Yan Zhenga76a3cd2008-10-09 11:46:29 -04007301 inode_add_bytes(inode, extent_len);
Zheng Yan1a40e232008-09-26 10:09:34 -04007302
7303 ext_offset = 0;
7304 num_bytes -= extent_len;
7305 key.offset += extent_len;
7306
7307 if (num_bytes == 0)
7308 break;
7309 }
7310 BUG_ON(i >= nr_extents);
Yan Zhengd899e052008-10-30 14:25:28 -04007311#endif
Zheng Yan1a40e232008-09-26 10:09:34 -04007312 }
7313
7314 if (extent_locked) {
Zheng Yan1a40e232008-09-26 10:09:34 -04007315 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
7316 lock_end, GFP_NOFS);
7317 extent_locked = 0;
7318 }
7319skip:
7320 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS &&
Yan Zheng86288a12009-01-21 10:49:16 -05007321 key.offset >= search_end)
Zheng Yan1a40e232008-09-26 10:09:34 -04007322 break;
7323
7324 cond_resched();
7325 }
7326 ret = 0;
7327out:
7328 btrfs_release_path(root, path);
7329 if (inode) {
7330 mutex_unlock(&inode->i_mutex);
7331 if (extent_locked) {
Zheng Yan1a40e232008-09-26 10:09:34 -04007332 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
7333 lock_end, GFP_NOFS);
7334 }
7335 iput(inode);
7336 }
7337 return ret;
7338}
7339
Zheng Yan1a40e232008-09-26 10:09:34 -04007340int btrfs_reloc_tree_cache_ref(struct btrfs_trans_handle *trans,
7341 struct btrfs_root *root,
7342 struct extent_buffer *buf, u64 orig_start)
7343{
7344 int level;
7345 int ret;
7346
7347 BUG_ON(btrfs_header_generation(buf) != trans->transid);
7348 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
7349
7350 level = btrfs_header_level(buf);
7351 if (level == 0) {
7352 struct btrfs_leaf_ref *ref;
7353 struct btrfs_leaf_ref *orig_ref;
7354
7355 orig_ref = btrfs_lookup_leaf_ref(root, orig_start);
7356 if (!orig_ref)
7357 return -ENOENT;
7358
7359 ref = btrfs_alloc_leaf_ref(root, orig_ref->nritems);
7360 if (!ref) {
7361 btrfs_free_leaf_ref(root, orig_ref);
7362 return -ENOMEM;
7363 }
7364
7365 ref->nritems = orig_ref->nritems;
7366 memcpy(ref->extents, orig_ref->extents,
7367 sizeof(ref->extents[0]) * ref->nritems);
7368
7369 btrfs_free_leaf_ref(root, orig_ref);
7370
7371 ref->root_gen = trans->transid;
7372 ref->bytenr = buf->start;
7373 ref->owner = btrfs_header_owner(buf);
7374 ref->generation = btrfs_header_generation(buf);
Chris Masonbd56b302009-02-04 09:27:02 -05007375
Zheng Yan1a40e232008-09-26 10:09:34 -04007376 ret = btrfs_add_leaf_ref(root, ref, 0);
7377 WARN_ON(ret);
7378 btrfs_free_leaf_ref(root, ref);
7379 }
7380 return 0;
7381}
7382
Chris Masond3977122009-01-05 21:25:51 -05007383static noinline int invalidate_extent_cache(struct btrfs_root *root,
Zheng Yan1a40e232008-09-26 10:09:34 -04007384 struct extent_buffer *leaf,
7385 struct btrfs_block_group_cache *group,
7386 struct btrfs_root *target_root)
7387{
7388 struct btrfs_key key;
7389 struct inode *inode = NULL;
7390 struct btrfs_file_extent_item *fi;
Josef Bacik2ac55d42010-02-03 19:33:23 +00007391 struct extent_state *cached_state = NULL;
Zheng Yan1a40e232008-09-26 10:09:34 -04007392 u64 num_bytes;
7393 u64 skip_objectid = 0;
7394 u32 nritems;
7395 u32 i;
7396
7397 nritems = btrfs_header_nritems(leaf);
7398 for (i = 0; i < nritems; i++) {
7399 btrfs_item_key_to_cpu(leaf, &key, i);
7400 if (key.objectid == skip_objectid ||
7401 key.type != BTRFS_EXTENT_DATA_KEY)
7402 continue;
7403 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
7404 if (btrfs_file_extent_type(leaf, fi) ==
7405 BTRFS_FILE_EXTENT_INLINE)
7406 continue;
7407 if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
7408 continue;
7409 if (!inode || inode->i_ino != key.objectid) {
7410 iput(inode);
7411 inode = btrfs_ilookup(target_root->fs_info->sb,
7412 key.objectid, target_root, 1);
7413 }
7414 if (!inode) {
7415 skip_objectid = key.objectid;
7416 continue;
7417 }
7418 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
7419
Josef Bacik2ac55d42010-02-03 19:33:23 +00007420 lock_extent_bits(&BTRFS_I(inode)->io_tree, key.offset,
7421 key.offset + num_bytes - 1, 0, &cached_state,
7422 GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04007423 btrfs_drop_extent_cache(inode, key.offset,
7424 key.offset + num_bytes - 1, 1);
Josef Bacik2ac55d42010-02-03 19:33:23 +00007425 unlock_extent_cached(&BTRFS_I(inode)->io_tree, key.offset,
7426 key.offset + num_bytes - 1, &cached_state,
7427 GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04007428 cond_resched();
7429 }
7430 iput(inode);
7431 return 0;
7432}
7433
Chris Masond3977122009-01-05 21:25:51 -05007434static noinline int replace_extents_in_leaf(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04007435 struct btrfs_root *root,
7436 struct extent_buffer *leaf,
7437 struct btrfs_block_group_cache *group,
7438 struct inode *reloc_inode)
7439{
7440 struct btrfs_key key;
7441 struct btrfs_key extent_key;
7442 struct btrfs_file_extent_item *fi;
7443 struct btrfs_leaf_ref *ref;
7444 struct disk_extent *new_extent;
7445 u64 bytenr;
7446 u64 num_bytes;
7447 u32 nritems;
7448 u32 i;
7449 int ext_index;
7450 int nr_extent;
7451 int ret;
7452
7453 new_extent = kmalloc(sizeof(*new_extent), GFP_NOFS);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00007454 if (!new_extent)
7455 return -ENOMEM;
Zheng Yan1a40e232008-09-26 10:09:34 -04007456
7457 ref = btrfs_lookup_leaf_ref(root, leaf->start);
7458 BUG_ON(!ref);
7459
7460 ext_index = -1;
7461 nritems = btrfs_header_nritems(leaf);
7462 for (i = 0; i < nritems; i++) {
7463 btrfs_item_key_to_cpu(leaf, &key, i);
7464 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
7465 continue;
7466 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
7467 if (btrfs_file_extent_type(leaf, fi) ==
7468 BTRFS_FILE_EXTENT_INLINE)
7469 continue;
7470 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
7471 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
7472 if (bytenr == 0)
7473 continue;
7474
7475 ext_index++;
7476 if (bytenr >= group->key.objectid + group->key.offset ||
7477 bytenr + num_bytes <= group->key.objectid)
7478 continue;
7479
7480 extent_key.objectid = bytenr;
7481 extent_key.offset = num_bytes;
7482 extent_key.type = BTRFS_EXTENT_ITEM_KEY;
7483 nr_extent = 1;
7484 ret = get_new_locations(reloc_inode, &extent_key,
7485 group->key.objectid, 1,
7486 &new_extent, &nr_extent);
7487 if (ret > 0)
7488 continue;
7489 BUG_ON(ret < 0);
7490
7491 BUG_ON(ref->extents[ext_index].bytenr != bytenr);
7492 BUG_ON(ref->extents[ext_index].num_bytes != num_bytes);
7493 ref->extents[ext_index].bytenr = new_extent->disk_bytenr;
7494 ref->extents[ext_index].num_bytes = new_extent->disk_num_bytes;
7495
Zheng Yan1a40e232008-09-26 10:09:34 -04007496 btrfs_set_file_extent_disk_bytenr(leaf, fi,
7497 new_extent->disk_bytenr);
7498 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
7499 new_extent->disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04007500 btrfs_mark_buffer_dirty(leaf);
7501
7502 ret = btrfs_inc_extent_ref(trans, root,
7503 new_extent->disk_bytenr,
7504 new_extent->disk_num_bytes,
7505 leaf->start,
7506 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04007507 trans->transid, key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04007508 BUG_ON(ret);
Chris Mason56bec292009-03-13 10:10:06 -04007509
Zheng Yan1a40e232008-09-26 10:09:34 -04007510 ret = btrfs_free_extent(trans, root,
7511 bytenr, num_bytes, leaf->start,
7512 btrfs_header_owner(leaf),
7513 btrfs_header_generation(leaf),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04007514 key.objectid, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04007515 BUG_ON(ret);
7516 cond_resched();
7517 }
7518 kfree(new_extent);
7519 BUG_ON(ext_index + 1 != ref->nritems);
7520 btrfs_free_leaf_ref(root, ref);
7521 return 0;
7522}
7523
Yan Zhengf82d02d2008-10-29 14:49:05 -04007524int btrfs_free_reloc_root(struct btrfs_trans_handle *trans,
7525 struct btrfs_root *root)
Zheng Yan1a40e232008-09-26 10:09:34 -04007526{
7527 struct btrfs_root *reloc_root;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007528 int ret;
Zheng Yan1a40e232008-09-26 10:09:34 -04007529
7530 if (root->reloc_root) {
7531 reloc_root = root->reloc_root;
7532 root->reloc_root = NULL;
7533 list_add(&reloc_root->dead_list,
7534 &root->fs_info->dead_reloc_roots);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007535
7536 btrfs_set_root_bytenr(&reloc_root->root_item,
7537 reloc_root->node->start);
7538 btrfs_set_root_level(&root->root_item,
7539 btrfs_header_level(reloc_root->node));
7540 memset(&reloc_root->root_item.drop_progress, 0,
7541 sizeof(struct btrfs_disk_key));
7542 reloc_root->root_item.drop_level = 0;
7543
7544 ret = btrfs_update_root(trans, root->fs_info->tree_root,
7545 &reloc_root->root_key,
7546 &reloc_root->root_item);
7547 BUG_ON(ret);
Zheng Yan1a40e232008-09-26 10:09:34 -04007548 }
7549 return 0;
7550}
7551
7552int btrfs_drop_dead_reloc_roots(struct btrfs_root *root)
7553{
7554 struct btrfs_trans_handle *trans;
7555 struct btrfs_root *reloc_root;
7556 struct btrfs_root *prev_root = NULL;
7557 struct list_head dead_roots;
7558 int ret;
7559 unsigned long nr;
7560
7561 INIT_LIST_HEAD(&dead_roots);
7562 list_splice_init(&root->fs_info->dead_reloc_roots, &dead_roots);
7563
7564 while (!list_empty(&dead_roots)) {
7565 reloc_root = list_entry(dead_roots.prev,
7566 struct btrfs_root, dead_list);
7567 list_del_init(&reloc_root->dead_list);
7568
7569 BUG_ON(reloc_root->commit_root != NULL);
7570 while (1) {
7571 trans = btrfs_join_transaction(root, 1);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00007572 BUG_ON(IS_ERR(trans));
Zheng Yan1a40e232008-09-26 10:09:34 -04007573
7574 mutex_lock(&root->fs_info->drop_mutex);
7575 ret = btrfs_drop_snapshot(trans, reloc_root);
7576 if (ret != -EAGAIN)
7577 break;
7578 mutex_unlock(&root->fs_info->drop_mutex);
7579
7580 nr = trans->blocks_used;
7581 ret = btrfs_end_transaction(trans, root);
7582 BUG_ON(ret);
7583 btrfs_btree_balance_dirty(root, nr);
7584 }
7585
7586 free_extent_buffer(reloc_root->node);
7587
7588 ret = btrfs_del_root(trans, root->fs_info->tree_root,
7589 &reloc_root->root_key);
7590 BUG_ON(ret);
7591 mutex_unlock(&root->fs_info->drop_mutex);
7592
7593 nr = trans->blocks_used;
7594 ret = btrfs_end_transaction(trans, root);
7595 BUG_ON(ret);
7596 btrfs_btree_balance_dirty(root, nr);
7597
7598 kfree(prev_root);
7599 prev_root = reloc_root;
7600 }
7601 if (prev_root) {
7602 btrfs_remove_leaf_refs(prev_root, (u64)-1, 0);
7603 kfree(prev_root);
7604 }
7605 return 0;
7606}
7607
7608int btrfs_add_dead_reloc_root(struct btrfs_root *root)
7609{
7610 list_add(&root->dead_list, &root->fs_info->dead_reloc_roots);
7611 return 0;
7612}
7613
7614int btrfs_cleanup_reloc_trees(struct btrfs_root *root)
7615{
7616 struct btrfs_root *reloc_root;
7617 struct btrfs_trans_handle *trans;
7618 struct btrfs_key location;
7619 int found;
7620 int ret;
7621
7622 mutex_lock(&root->fs_info->tree_reloc_mutex);
7623 ret = btrfs_find_dead_roots(root, BTRFS_TREE_RELOC_OBJECTID, NULL);
7624 BUG_ON(ret);
7625 found = !list_empty(&root->fs_info->dead_reloc_roots);
7626 mutex_unlock(&root->fs_info->tree_reloc_mutex);
7627
7628 if (found) {
7629 trans = btrfs_start_transaction(root, 1);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00007630 BUG_ON(IS_ERR(trans));
Zheng Yan1a40e232008-09-26 10:09:34 -04007631 ret = btrfs_commit_transaction(trans, root);
7632 BUG_ON(ret);
7633 }
7634
7635 location.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
7636 location.offset = (u64)-1;
7637 location.type = BTRFS_ROOT_ITEM_KEY;
7638
7639 reloc_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
7640 BUG_ON(!reloc_root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05007641 ret = btrfs_orphan_cleanup(reloc_root);
7642 BUG_ON(ret);
Zheng Yan1a40e232008-09-26 10:09:34 -04007643 return 0;
7644}
7645
Chris Masond3977122009-01-05 21:25:51 -05007646static noinline int init_reloc_tree(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04007647 struct btrfs_root *root)
7648{
7649 struct btrfs_root *reloc_root;
7650 struct extent_buffer *eb;
7651 struct btrfs_root_item *root_item;
7652 struct btrfs_key root_key;
7653 int ret;
7654
7655 BUG_ON(!root->ref_cows);
7656 if (root->reloc_root)
7657 return 0;
7658
7659 root_item = kmalloc(sizeof(*root_item), GFP_NOFS);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00007660 if (!root_item)
7661 return -ENOMEM;
Zheng Yan1a40e232008-09-26 10:09:34 -04007662
7663 ret = btrfs_copy_root(trans, root, root->commit_root,
7664 &eb, BTRFS_TREE_RELOC_OBJECTID);
7665 BUG_ON(ret);
7666
7667 root_key.objectid = BTRFS_TREE_RELOC_OBJECTID;
7668 root_key.offset = root->root_key.objectid;
7669 root_key.type = BTRFS_ROOT_ITEM_KEY;
7670
7671 memcpy(root_item, &root->root_item, sizeof(root_item));
7672 btrfs_set_root_refs(root_item, 0);
7673 btrfs_set_root_bytenr(root_item, eb->start);
7674 btrfs_set_root_level(root_item, btrfs_header_level(eb));
Yan Zheng84234f32008-10-29 14:49:05 -04007675 btrfs_set_root_generation(root_item, trans->transid);
Zheng Yan1a40e232008-09-26 10:09:34 -04007676
7677 btrfs_tree_unlock(eb);
7678 free_extent_buffer(eb);
7679
7680 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
7681 &root_key, root_item);
7682 BUG_ON(ret);
7683 kfree(root_item);
7684
7685 reloc_root = btrfs_read_fs_root_no_radix(root->fs_info->tree_root,
7686 &root_key);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00007687 BUG_ON(IS_ERR(reloc_root));
Zheng Yan1a40e232008-09-26 10:09:34 -04007688 reloc_root->last_trans = trans->transid;
7689 reloc_root->commit_root = NULL;
7690 reloc_root->ref_tree = &root->fs_info->reloc_ref_tree;
7691
7692 root->reloc_root = reloc_root;
7693 return 0;
7694}
7695
7696/*
7697 * Core function of space balance.
7698 *
7699 * The idea is using reloc trees to relocate tree blocks in reference
Yan Zhengf82d02d2008-10-29 14:49:05 -04007700 * counted roots. There is one reloc tree for each subvol, and all
7701 * reloc trees share same root key objectid. Reloc trees are snapshots
7702 * of the latest committed roots of subvols (root->commit_root).
7703 *
7704 * To relocate a tree block referenced by a subvol, there are two steps.
7705 * COW the block through subvol's reloc tree, then update block pointer
7706 * in the subvol to point to the new block. Since all reloc trees share
7707 * same root key objectid, doing special handing for tree blocks owned
7708 * by them is easy. Once a tree block has been COWed in one reloc tree,
7709 * we can use the resulting new block directly when the same block is
7710 * required to COW again through other reloc trees. By this way, relocated
7711 * tree blocks are shared between reloc trees, so they are also shared
7712 * between subvols.
Zheng Yan1a40e232008-09-26 10:09:34 -04007713 */
Chris Masond3977122009-01-05 21:25:51 -05007714static noinline int relocate_one_path(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04007715 struct btrfs_root *root,
7716 struct btrfs_path *path,
7717 struct btrfs_key *first_key,
7718 struct btrfs_ref_path *ref_path,
7719 struct btrfs_block_group_cache *group,
7720 struct inode *reloc_inode)
7721{
7722 struct btrfs_root *reloc_root;
7723 struct extent_buffer *eb = NULL;
7724 struct btrfs_key *keys;
7725 u64 *nodes;
7726 int level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007727 int shared_level;
Zheng Yan1a40e232008-09-26 10:09:34 -04007728 int lowest_level = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04007729 int ret;
7730
7731 if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
7732 lowest_level = ref_path->owner_objectid;
7733
Yan Zhengf82d02d2008-10-29 14:49:05 -04007734 if (!root->ref_cows) {
Zheng Yan1a40e232008-09-26 10:09:34 -04007735 path->lowest_level = lowest_level;
7736 ret = btrfs_search_slot(trans, root, first_key, path, 0, 1);
7737 BUG_ON(ret < 0);
7738 path->lowest_level = 0;
7739 btrfs_release_path(root, path);
7740 return 0;
7741 }
7742
Zheng Yan1a40e232008-09-26 10:09:34 -04007743 mutex_lock(&root->fs_info->tree_reloc_mutex);
7744 ret = init_reloc_tree(trans, root);
7745 BUG_ON(ret);
7746 reloc_root = root->reloc_root;
7747
Yan Zhengf82d02d2008-10-29 14:49:05 -04007748 shared_level = ref_path->shared_level;
7749 ref_path->shared_level = BTRFS_MAX_LEVEL - 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04007750
Yan Zhengf82d02d2008-10-29 14:49:05 -04007751 keys = ref_path->node_keys;
7752 nodes = ref_path->new_nodes;
7753 memset(&keys[shared_level + 1], 0,
7754 sizeof(*keys) * (BTRFS_MAX_LEVEL - shared_level - 1));
7755 memset(&nodes[shared_level + 1], 0,
7756 sizeof(*nodes) * (BTRFS_MAX_LEVEL - shared_level - 1));
Zheng Yan1a40e232008-09-26 10:09:34 -04007757
Yan Zhengf82d02d2008-10-29 14:49:05 -04007758 if (nodes[lowest_level] == 0) {
7759 path->lowest_level = lowest_level;
7760 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
7761 0, 1);
7762 BUG_ON(ret);
7763 for (level = lowest_level; level < BTRFS_MAX_LEVEL; level++) {
7764 eb = path->nodes[level];
7765 if (!eb || eb == reloc_root->node)
7766 break;
7767 nodes[level] = eb->start;
7768 if (level == 0)
7769 btrfs_item_key_to_cpu(eb, &keys[level], 0);
7770 else
7771 btrfs_node_key_to_cpu(eb, &keys[level], 0);
7772 }
Yan Zheng2b820322008-11-17 21:11:30 -05007773 if (nodes[0] &&
7774 ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04007775 eb = path->nodes[0];
7776 ret = replace_extents_in_leaf(trans, reloc_root, eb,
7777 group, reloc_inode);
7778 BUG_ON(ret);
7779 }
7780 btrfs_release_path(reloc_root, path);
7781 } else {
Zheng Yan1a40e232008-09-26 10:09:34 -04007782 ret = btrfs_merge_path(trans, reloc_root, keys, nodes,
Yan Zhengf82d02d2008-10-29 14:49:05 -04007783 lowest_level);
Zheng Yan1a40e232008-09-26 10:09:34 -04007784 BUG_ON(ret);
7785 }
7786
Zheng Yan1a40e232008-09-26 10:09:34 -04007787 /*
7788 * replace tree blocks in the fs tree with tree blocks in
7789 * the reloc tree.
7790 */
7791 ret = btrfs_merge_path(trans, root, keys, nodes, lowest_level);
7792 BUG_ON(ret < 0);
7793
7794 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04007795 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
7796 0, 0);
7797 BUG_ON(ret);
7798 extent_buffer_get(path->nodes[0]);
7799 eb = path->nodes[0];
7800 btrfs_release_path(reloc_root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04007801 ret = invalidate_extent_cache(reloc_root, eb, group, root);
7802 BUG_ON(ret);
7803 free_extent_buffer(eb);
7804 }
Zheng Yan1a40e232008-09-26 10:09:34 -04007805
Yan Zhengf82d02d2008-10-29 14:49:05 -04007806 mutex_unlock(&root->fs_info->tree_reloc_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04007807 path->lowest_level = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04007808 return 0;
7809}
7810
Chris Masond3977122009-01-05 21:25:51 -05007811static noinline int relocate_tree_block(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04007812 struct btrfs_root *root,
7813 struct btrfs_path *path,
7814 struct btrfs_key *first_key,
7815 struct btrfs_ref_path *ref_path)
7816{
7817 int ret;
Zheng Yan1a40e232008-09-26 10:09:34 -04007818
7819 ret = relocate_one_path(trans, root, path, first_key,
7820 ref_path, NULL, NULL);
7821 BUG_ON(ret);
7822
Zheng Yan1a40e232008-09-26 10:09:34 -04007823 return 0;
7824}
7825
Chris Masond3977122009-01-05 21:25:51 -05007826static noinline int del_extent_zero(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04007827 struct btrfs_root *extent_root,
7828 struct btrfs_path *path,
7829 struct btrfs_key *extent_key)
7830{
7831 int ret;
7832
Zheng Yan1a40e232008-09-26 10:09:34 -04007833 ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1);
7834 if (ret)
7835 goto out;
7836 ret = btrfs_del_item(trans, extent_root, path);
7837out:
Chris Masonedbd8d42007-12-21 16:27:24 -05007838 btrfs_release_path(extent_root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04007839 return ret;
7840}
7841
Chris Masond3977122009-01-05 21:25:51 -05007842static noinline struct btrfs_root *read_ref_root(struct btrfs_fs_info *fs_info,
Zheng Yan1a40e232008-09-26 10:09:34 -04007843 struct btrfs_ref_path *ref_path)
7844{
7845 struct btrfs_key root_key;
7846
7847 root_key.objectid = ref_path->root_objectid;
7848 root_key.type = BTRFS_ROOT_ITEM_KEY;
7849 if (is_cowonly_root(ref_path->root_objectid))
7850 root_key.offset = 0;
7851 else
7852 root_key.offset = (u64)-1;
7853
7854 return btrfs_read_fs_root_no_name(fs_info, &root_key);
7855}
7856
Chris Masond3977122009-01-05 21:25:51 -05007857static noinline int relocate_one_extent(struct btrfs_root *extent_root,
Zheng Yan1a40e232008-09-26 10:09:34 -04007858 struct btrfs_path *path,
7859 struct btrfs_key *extent_key,
7860 struct btrfs_block_group_cache *group,
7861 struct inode *reloc_inode, int pass)
7862{
7863 struct btrfs_trans_handle *trans;
7864 struct btrfs_root *found_root;
7865 struct btrfs_ref_path *ref_path = NULL;
7866 struct disk_extent *new_extents = NULL;
7867 int nr_extents = 0;
7868 int loops;
7869 int ret;
7870 int level;
7871 struct btrfs_key first_key;
7872 u64 prev_block = 0;
7873
Zheng Yan1a40e232008-09-26 10:09:34 -04007874
7875 trans = btrfs_start_transaction(extent_root, 1);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00007876 BUG_ON(IS_ERR(trans));
Zheng Yan1a40e232008-09-26 10:09:34 -04007877
7878 if (extent_key->objectid == 0) {
7879 ret = del_extent_zero(trans, extent_root, path, extent_key);
7880 goto out;
7881 }
7882
7883 ref_path = kmalloc(sizeof(*ref_path), GFP_NOFS);
7884 if (!ref_path) {
Chris Masond3977122009-01-05 21:25:51 -05007885 ret = -ENOMEM;
7886 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04007887 }
7888
7889 for (loops = 0; ; loops++) {
7890 if (loops == 0) {
7891 ret = btrfs_first_ref_path(trans, extent_root, ref_path,
7892 extent_key->objectid);
7893 } else {
7894 ret = btrfs_next_ref_path(trans, extent_root, ref_path);
7895 }
7896 if (ret < 0)
7897 goto out;
7898 if (ret > 0)
7899 break;
7900
7901 if (ref_path->root_objectid == BTRFS_TREE_LOG_OBJECTID ||
7902 ref_path->root_objectid == BTRFS_TREE_RELOC_OBJECTID)
7903 continue;
7904
7905 found_root = read_ref_root(extent_root->fs_info, ref_path);
7906 BUG_ON(!found_root);
7907 /*
7908 * for reference counted tree, only process reference paths
7909 * rooted at the latest committed root.
7910 */
7911 if (found_root->ref_cows &&
7912 ref_path->root_generation != found_root->root_key.offset)
7913 continue;
7914
7915 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
7916 if (pass == 0) {
7917 /*
7918 * copy data extents to new locations
7919 */
7920 u64 group_start = group->key.objectid;
7921 ret = relocate_data_extent(reloc_inode,
7922 extent_key,
7923 group_start);
7924 if (ret < 0)
7925 goto out;
7926 break;
7927 }
7928 level = 0;
7929 } else {
7930 level = ref_path->owner_objectid;
7931 }
7932
7933 if (prev_block != ref_path->nodes[level]) {
7934 struct extent_buffer *eb;
7935 u64 block_start = ref_path->nodes[level];
7936 u64 block_size = btrfs_level_size(found_root, level);
7937
7938 eb = read_tree_block(found_root, block_start,
7939 block_size, 0);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00007940 if (!eb) {
7941 ret = -EIO;
7942 goto out;
7943 }
Zheng Yan1a40e232008-09-26 10:09:34 -04007944 btrfs_tree_lock(eb);
7945 BUG_ON(level != btrfs_header_level(eb));
7946
7947 if (level == 0)
7948 btrfs_item_key_to_cpu(eb, &first_key, 0);
7949 else
7950 btrfs_node_key_to_cpu(eb, &first_key, 0);
7951
7952 btrfs_tree_unlock(eb);
7953 free_extent_buffer(eb);
7954 prev_block = block_start;
7955 }
7956
Yan Zheng24562422009-02-12 14:14:53 -05007957 mutex_lock(&extent_root->fs_info->trans_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05007958 btrfs_record_root_in_trans(found_root);
Yan Zheng24562422009-02-12 14:14:53 -05007959 mutex_unlock(&extent_root->fs_info->trans_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05007960 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
7961 /*
7962 * try to update data extent references while
7963 * keeping metadata shared between snapshots.
7964 */
7965 if (pass == 1) {
7966 ret = relocate_one_path(trans, found_root,
7967 path, &first_key, ref_path,
7968 group, reloc_inode);
7969 if (ret < 0)
7970 goto out;
7971 continue;
7972 }
Zheng Yan1a40e232008-09-26 10:09:34 -04007973 /*
7974 * use fallback method to process the remaining
7975 * references.
7976 */
7977 if (!new_extents) {
7978 u64 group_start = group->key.objectid;
Yan Zhengd899e052008-10-30 14:25:28 -04007979 new_extents = kmalloc(sizeof(*new_extents),
7980 GFP_NOFS);
7981 nr_extents = 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04007982 ret = get_new_locations(reloc_inode,
7983 extent_key,
Yan Zhengd899e052008-10-30 14:25:28 -04007984 group_start, 1,
Zheng Yan1a40e232008-09-26 10:09:34 -04007985 &new_extents,
7986 &nr_extents);
Yan Zhengd899e052008-10-30 14:25:28 -04007987 if (ret)
Zheng Yan1a40e232008-09-26 10:09:34 -04007988 goto out;
7989 }
Zheng Yan1a40e232008-09-26 10:09:34 -04007990 ret = replace_one_extent(trans, found_root,
7991 path, extent_key,
7992 &first_key, ref_path,
7993 new_extents, nr_extents);
Yan Zhenge4404d62008-12-12 10:03:26 -05007994 } else {
Zheng Yan1a40e232008-09-26 10:09:34 -04007995 ret = relocate_tree_block(trans, found_root, path,
7996 &first_key, ref_path);
Zheng Yan1a40e232008-09-26 10:09:34 -04007997 }
7998 if (ret < 0)
7999 goto out;
8000 }
8001 ret = 0;
8002out:
8003 btrfs_end_transaction(trans, extent_root);
8004 kfree(new_extents);
8005 kfree(ref_path);
Chris Masonedbd8d42007-12-21 16:27:24 -05008006 return ret;
8007}
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008008#endif
Chris Masonedbd8d42007-12-21 16:27:24 -05008009
Chris Masonec44a352008-04-28 15:29:52 -04008010static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
8011{
8012 u64 num_devices;
8013 u64 stripped = BTRFS_BLOCK_GROUP_RAID0 |
8014 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
8015
Chris Masoncd02dca2010-12-13 14:56:23 -05008016 /*
8017 * we add in the count of missing devices because we want
8018 * to make sure that any RAID levels on a degraded FS
8019 * continue to be honored.
8020 */
8021 num_devices = root->fs_info->fs_devices->rw_devices +
8022 root->fs_info->fs_devices->missing_devices;
8023
Chris Masonec44a352008-04-28 15:29:52 -04008024 if (num_devices == 1) {
8025 stripped |= BTRFS_BLOCK_GROUP_DUP;
8026 stripped = flags & ~stripped;
8027
8028 /* turn raid0 into single device chunks */
8029 if (flags & BTRFS_BLOCK_GROUP_RAID0)
8030 return stripped;
8031
8032 /* turn mirroring into duplication */
8033 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
8034 BTRFS_BLOCK_GROUP_RAID10))
8035 return stripped | BTRFS_BLOCK_GROUP_DUP;
8036 return flags;
8037 } else {
8038 /* they already had raid on here, just return */
Chris Masonec44a352008-04-28 15:29:52 -04008039 if (flags & stripped)
8040 return flags;
8041
8042 stripped |= BTRFS_BLOCK_GROUP_DUP;
8043 stripped = flags & ~stripped;
8044
8045 /* switch duplicated blocks with raid1 */
8046 if (flags & BTRFS_BLOCK_GROUP_DUP)
8047 return stripped | BTRFS_BLOCK_GROUP_RAID1;
8048
8049 /* turn single device chunks into raid0 */
8050 return stripped | BTRFS_BLOCK_GROUP_RAID0;
8051 }
8052 return flags;
8053}
8054
Yan, Zhengf0486c62010-05-16 10:46:25 -04008055static int set_block_group_ro(struct btrfs_block_group_cache *cache)
Chris Mason0ef3e662008-05-24 14:04:53 -04008056{
Yan, Zhengf0486c62010-05-16 10:46:25 -04008057 struct btrfs_space_info *sinfo = cache->space_info;
8058 u64 num_bytes;
8059 int ret = -ENOSPC;
Chris Mason0ef3e662008-05-24 14:04:53 -04008060
Yan, Zhengf0486c62010-05-16 10:46:25 -04008061 if (cache->ro)
8062 return 0;
Chris Masonc286ac42008-07-22 23:06:41 -04008063
Yan, Zhengf0486c62010-05-16 10:46:25 -04008064 spin_lock(&sinfo->lock);
8065 spin_lock(&cache->lock);
8066 num_bytes = cache->key.offset - cache->reserved - cache->pinned -
8067 cache->bytes_super - btrfs_block_group_used(&cache->item);
Chris Mason7d9eb122008-07-08 14:19:17 -04008068
Yan, Zhengf0486c62010-05-16 10:46:25 -04008069 if (sinfo->bytes_used + sinfo->bytes_reserved + sinfo->bytes_pinned +
8070 sinfo->bytes_may_use + sinfo->bytes_readonly +
Chris Mason65e53412010-12-24 06:41:52 -05008071 cache->reserved_pinned + num_bytes <= sinfo->total_bytes) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04008072 sinfo->bytes_readonly += num_bytes;
8073 sinfo->bytes_reserved += cache->reserved_pinned;
8074 cache->reserved_pinned = 0;
8075 cache->ro = 1;
8076 ret = 0;
8077 }
Chris Mason65e53412010-12-24 06:41:52 -05008078
Yan, Zhengf0486c62010-05-16 10:46:25 -04008079 spin_unlock(&cache->lock);
8080 spin_unlock(&sinfo->lock);
8081 return ret;
Chris Mason0ef3e662008-05-24 14:04:53 -04008082}
8083
Yan, Zhengf0486c62010-05-16 10:46:25 -04008084int btrfs_set_block_group_ro(struct btrfs_root *root,
8085 struct btrfs_block_group_cache *cache)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008086
8087{
Yan, Zhengf0486c62010-05-16 10:46:25 -04008088 struct btrfs_trans_handle *trans;
8089 u64 alloc_flags;
8090 int ret;
8091
8092 BUG_ON(cache->ro);
8093
8094 trans = btrfs_join_transaction(root, 1);
8095 BUG_ON(IS_ERR(trans));
8096
8097 alloc_flags = update_block_group_flags(root, cache->flags);
8098 if (alloc_flags != cache->flags)
8099 do_chunk_alloc(trans, root, 2 * 1024 * 1024, alloc_flags, 1);
8100
8101 ret = set_block_group_ro(cache);
8102 if (!ret)
8103 goto out;
8104 alloc_flags = get_alloc_profile(root, cache->space_info->flags);
8105 ret = do_chunk_alloc(trans, root, 2 * 1024 * 1024, alloc_flags, 1);
8106 if (ret < 0)
8107 goto out;
8108 ret = set_block_group_ro(cache);
8109out:
8110 btrfs_end_transaction(trans, root);
8111 return ret;
8112}
8113
Chris Masonc87f08c2011-02-16 13:57:04 -05008114int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans,
8115 struct btrfs_root *root, u64 type)
8116{
8117 u64 alloc_flags = get_alloc_profile(root, type);
8118 return do_chunk_alloc(trans, root, 2 * 1024 * 1024, alloc_flags, 1);
8119}
8120
Miao Xie6d07bce2011-01-05 10:07:31 +00008121/*
8122 * helper to account the unused space of all the readonly block group in the
8123 * list. takes mirrors into account.
8124 */
8125static u64 __btrfs_get_ro_block_group_free_space(struct list_head *groups_list)
8126{
8127 struct btrfs_block_group_cache *block_group;
8128 u64 free_bytes = 0;
8129 int factor;
8130
8131 list_for_each_entry(block_group, groups_list, list) {
8132 spin_lock(&block_group->lock);
8133
8134 if (!block_group->ro) {
8135 spin_unlock(&block_group->lock);
8136 continue;
8137 }
8138
8139 if (block_group->flags & (BTRFS_BLOCK_GROUP_RAID1 |
8140 BTRFS_BLOCK_GROUP_RAID10 |
8141 BTRFS_BLOCK_GROUP_DUP))
8142 factor = 2;
8143 else
8144 factor = 1;
8145
8146 free_bytes += (block_group->key.offset -
8147 btrfs_block_group_used(&block_group->item)) *
8148 factor;
8149
8150 spin_unlock(&block_group->lock);
8151 }
8152
8153 return free_bytes;
8154}
8155
8156/*
8157 * helper to account the unused space of all the readonly block group in the
8158 * space_info. takes mirrors into account.
8159 */
8160u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo)
8161{
8162 int i;
8163 u64 free_bytes = 0;
8164
8165 spin_lock(&sinfo->lock);
8166
8167 for(i = 0; i < BTRFS_NR_RAID_TYPES; i++)
8168 if (!list_empty(&sinfo->block_groups[i]))
8169 free_bytes += __btrfs_get_ro_block_group_free_space(
8170 &sinfo->block_groups[i]);
8171
8172 spin_unlock(&sinfo->lock);
8173
8174 return free_bytes;
8175}
8176
Yan, Zhengf0486c62010-05-16 10:46:25 -04008177int btrfs_set_block_group_rw(struct btrfs_root *root,
8178 struct btrfs_block_group_cache *cache)
8179{
8180 struct btrfs_space_info *sinfo = cache->space_info;
8181 u64 num_bytes;
8182
8183 BUG_ON(!cache->ro);
8184
8185 spin_lock(&sinfo->lock);
8186 spin_lock(&cache->lock);
8187 num_bytes = cache->key.offset - cache->reserved - cache->pinned -
8188 cache->bytes_super - btrfs_block_group_used(&cache->item);
8189 sinfo->bytes_readonly -= num_bytes;
8190 cache->ro = 0;
8191 spin_unlock(&cache->lock);
8192 spin_unlock(&sinfo->lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008193 return 0;
8194}
8195
Josef Bacikba1bf482009-09-11 16:11:19 -04008196/*
8197 * checks to see if its even possible to relocate this block group.
8198 *
8199 * @return - -1 if it's not a good idea to relocate this block group, 0 if its
8200 * ok to go ahead and try.
8201 */
8202int btrfs_can_relocate(struct btrfs_root *root, u64 bytenr)
Zheng Yan1a40e232008-09-26 10:09:34 -04008203{
Zheng Yan1a40e232008-09-26 10:09:34 -04008204 struct btrfs_block_group_cache *block_group;
Josef Bacikba1bf482009-09-11 16:11:19 -04008205 struct btrfs_space_info *space_info;
8206 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
8207 struct btrfs_device *device;
8208 int full = 0;
8209 int ret = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05008210
Josef Bacikba1bf482009-09-11 16:11:19 -04008211 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
Zheng Yan1a40e232008-09-26 10:09:34 -04008212
Josef Bacikba1bf482009-09-11 16:11:19 -04008213 /* odd, couldn't find the block group, leave it alone */
8214 if (!block_group)
8215 return -1;
Chris Masonedbd8d42007-12-21 16:27:24 -05008216
Josef Bacikba1bf482009-09-11 16:11:19 -04008217 /* no bytes used, we're good */
8218 if (!btrfs_block_group_used(&block_group->item))
8219 goto out;
Chris Mason323da792008-05-09 11:46:48 -04008220
Josef Bacikba1bf482009-09-11 16:11:19 -04008221 space_info = block_group->space_info;
8222 spin_lock(&space_info->lock);
Chris Mason323da792008-05-09 11:46:48 -04008223
Josef Bacikba1bf482009-09-11 16:11:19 -04008224 full = space_info->full;
Zheng Yan1a40e232008-09-26 10:09:34 -04008225
Josef Bacikba1bf482009-09-11 16:11:19 -04008226 /*
8227 * if this is the last block group we have in this space, we can't
Chris Mason7ce618d2009-09-22 14:48:44 -04008228 * relocate it unless we're able to allocate a new chunk below.
8229 *
8230 * Otherwise, we need to make sure we have room in the space to handle
8231 * all of the extents from this block group. If we can, we're good
Josef Bacikba1bf482009-09-11 16:11:19 -04008232 */
Chris Mason7ce618d2009-09-22 14:48:44 -04008233 if ((space_info->total_bytes != block_group->key.offset) &&
8234 (space_info->bytes_used + space_info->bytes_reserved +
Josef Bacikba1bf482009-09-11 16:11:19 -04008235 space_info->bytes_pinned + space_info->bytes_readonly +
8236 btrfs_block_group_used(&block_group->item) <
Chris Mason7ce618d2009-09-22 14:48:44 -04008237 space_info->total_bytes)) {
Josef Bacikba1bf482009-09-11 16:11:19 -04008238 spin_unlock(&space_info->lock);
8239 goto out;
8240 }
8241 spin_unlock(&space_info->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04008242
Josef Bacikba1bf482009-09-11 16:11:19 -04008243 /*
8244 * ok we don't have enough space, but maybe we have free space on our
8245 * devices to allocate new chunks for relocation, so loop through our
8246 * alloc devices and guess if we have enough space. However, if we
8247 * were marked as full, then we know there aren't enough chunks, and we
8248 * can just return.
8249 */
8250 ret = -1;
8251 if (full)
8252 goto out;
Chris Mason4313b392008-01-03 09:08:48 -05008253
Josef Bacikba1bf482009-09-11 16:11:19 -04008254 mutex_lock(&root->fs_info->chunk_mutex);
8255 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
8256 u64 min_free = btrfs_block_group_used(&block_group->item);
Miao Xie7bfc8372011-01-05 10:07:26 +00008257 u64 dev_offset;
Chris Masonea8c2812008-08-04 23:17:27 -04008258
Josef Bacikba1bf482009-09-11 16:11:19 -04008259 /*
8260 * check to make sure we can actually find a chunk with enough
8261 * space to fit our block group in.
8262 */
8263 if (device->total_bytes > device->bytes_used + min_free) {
8264 ret = find_free_dev_extent(NULL, device, min_free,
Miao Xie7bfc8372011-01-05 10:07:26 +00008265 &dev_offset, NULL);
Josef Bacikba1bf482009-09-11 16:11:19 -04008266 if (!ret)
Yan73e48b22008-01-03 14:14:39 -05008267 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04008268 ret = -1;
Yan73e48b22008-01-03 14:14:39 -05008269 }
Chris Masonedbd8d42007-12-21 16:27:24 -05008270 }
Josef Bacikba1bf482009-09-11 16:11:19 -04008271 mutex_unlock(&root->fs_info->chunk_mutex);
Chris Masonedbd8d42007-12-21 16:27:24 -05008272out:
Josef Bacikba1bf482009-09-11 16:11:19 -04008273 btrfs_put_block_group(block_group);
Chris Masonedbd8d42007-12-21 16:27:24 -05008274 return ret;
8275}
8276
Christoph Hellwigb2950862008-12-02 09:54:17 -05008277static int find_first_block_group(struct btrfs_root *root,
8278 struct btrfs_path *path, struct btrfs_key *key)
Chris Mason0b86a832008-03-24 15:01:56 -04008279{
Chris Mason925baed2008-06-25 16:01:30 -04008280 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04008281 struct btrfs_key found_key;
8282 struct extent_buffer *leaf;
8283 int slot;
8284
8285 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
8286 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04008287 goto out;
8288
Chris Masond3977122009-01-05 21:25:51 -05008289 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04008290 slot = path->slots[0];
8291 leaf = path->nodes[0];
8292 if (slot >= btrfs_header_nritems(leaf)) {
8293 ret = btrfs_next_leaf(root, path);
8294 if (ret == 0)
8295 continue;
8296 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04008297 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04008298 break;
8299 }
8300 btrfs_item_key_to_cpu(leaf, &found_key, slot);
8301
8302 if (found_key.objectid >= key->objectid &&
Chris Mason925baed2008-06-25 16:01:30 -04008303 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
8304 ret = 0;
8305 goto out;
8306 }
Chris Mason0b86a832008-03-24 15:01:56 -04008307 path->slots[0]++;
8308 }
Chris Mason925baed2008-06-25 16:01:30 -04008309out:
Chris Mason0b86a832008-03-24 15:01:56 -04008310 return ret;
8311}
8312
Josef Bacik0af3d002010-06-21 14:48:16 -04008313void btrfs_put_block_group_cache(struct btrfs_fs_info *info)
8314{
8315 struct btrfs_block_group_cache *block_group;
8316 u64 last = 0;
8317
8318 while (1) {
8319 struct inode *inode;
8320
8321 block_group = btrfs_lookup_first_block_group(info, last);
8322 while (block_group) {
8323 spin_lock(&block_group->lock);
8324 if (block_group->iref)
8325 break;
8326 spin_unlock(&block_group->lock);
8327 block_group = next_block_group(info->tree_root,
8328 block_group);
8329 }
8330 if (!block_group) {
8331 if (last == 0)
8332 break;
8333 last = 0;
8334 continue;
8335 }
8336
8337 inode = block_group->inode;
8338 block_group->iref = 0;
8339 block_group->inode = NULL;
8340 spin_unlock(&block_group->lock);
8341 iput(inode);
8342 last = block_group->key.objectid + block_group->key.offset;
8343 btrfs_put_block_group(block_group);
8344 }
8345}
8346
Zheng Yan1a40e232008-09-26 10:09:34 -04008347int btrfs_free_block_groups(struct btrfs_fs_info *info)
8348{
8349 struct btrfs_block_group_cache *block_group;
Chris Mason4184ea72009-03-10 12:39:20 -04008350 struct btrfs_space_info *space_info;
Yan Zheng11833d62009-09-11 16:11:19 -04008351 struct btrfs_caching_control *caching_ctl;
Zheng Yan1a40e232008-09-26 10:09:34 -04008352 struct rb_node *n;
8353
Yan Zheng11833d62009-09-11 16:11:19 -04008354 down_write(&info->extent_commit_sem);
8355 while (!list_empty(&info->caching_block_groups)) {
8356 caching_ctl = list_entry(info->caching_block_groups.next,
8357 struct btrfs_caching_control, list);
8358 list_del(&caching_ctl->list);
8359 put_caching_control(caching_ctl);
8360 }
8361 up_write(&info->extent_commit_sem);
8362
Zheng Yan1a40e232008-09-26 10:09:34 -04008363 spin_lock(&info->block_group_cache_lock);
8364 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
8365 block_group = rb_entry(n, struct btrfs_block_group_cache,
8366 cache_node);
Zheng Yan1a40e232008-09-26 10:09:34 -04008367 rb_erase(&block_group->cache_node,
8368 &info->block_group_cache_tree);
Yan Zhengd899e052008-10-30 14:25:28 -04008369 spin_unlock(&info->block_group_cache_lock);
8370
Josef Bacik80eb2342008-10-29 14:49:05 -04008371 down_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04008372 list_del(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04008373 up_write(&block_group->space_info->groups_sem);
Yan Zhengd2fb3432008-12-11 16:30:39 -05008374
Josef Bacik817d52f2009-07-13 21:29:25 -04008375 if (block_group->cached == BTRFS_CACHE_STARTED)
Yan Zheng11833d62009-09-11 16:11:19 -04008376 wait_block_group_cache_done(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -04008377
Josef Bacik3c148742011-02-02 15:53:47 +00008378 /*
8379 * We haven't cached this block group, which means we could
8380 * possibly have excluded extents on this block group.
8381 */
8382 if (block_group->cached == BTRFS_CACHE_NO)
8383 free_excluded_extents(info->extent_root, block_group);
8384
Josef Bacik817d52f2009-07-13 21:29:25 -04008385 btrfs_remove_free_space_cache(block_group);
Josef Bacik11dfe352009-11-13 20:12:59 +00008386 btrfs_put_block_group(block_group);
Yan Zhengd899e052008-10-30 14:25:28 -04008387
8388 spin_lock(&info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04008389 }
8390 spin_unlock(&info->block_group_cache_lock);
Chris Mason4184ea72009-03-10 12:39:20 -04008391
8392 /* now that all the block groups are freed, go through and
8393 * free all the space_info structs. This is only called during
8394 * the final stages of unmount, and so we know nobody is
8395 * using them. We call synchronize_rcu() once before we start,
8396 * just to be on the safe side.
8397 */
8398 synchronize_rcu();
8399
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04008400 release_global_block_rsv(info);
8401
Chris Mason4184ea72009-03-10 12:39:20 -04008402 while(!list_empty(&info->space_info)) {
8403 space_info = list_entry(info->space_info.next,
8404 struct btrfs_space_info,
8405 list);
Yan, Zhengf0486c62010-05-16 10:46:25 -04008406 if (space_info->bytes_pinned > 0 ||
8407 space_info->bytes_reserved > 0) {
8408 WARN_ON(1);
8409 dump_space_info(space_info, 0, 0);
8410 }
Chris Mason4184ea72009-03-10 12:39:20 -04008411 list_del(&space_info->list);
8412 kfree(space_info);
8413 }
Zheng Yan1a40e232008-09-26 10:09:34 -04008414 return 0;
8415}
8416
Yan, Zhengb742bb822010-05-16 10:46:24 -04008417static void __link_block_group(struct btrfs_space_info *space_info,
8418 struct btrfs_block_group_cache *cache)
8419{
8420 int index = get_block_group_index(cache);
8421
8422 down_write(&space_info->groups_sem);
8423 list_add_tail(&cache->list, &space_info->block_groups[index]);
8424 up_write(&space_info->groups_sem);
8425}
8426
Chris Mason9078a3e2007-04-26 16:46:15 -04008427int btrfs_read_block_groups(struct btrfs_root *root)
8428{
8429 struct btrfs_path *path;
8430 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04008431 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04008432 struct btrfs_fs_info *info = root->fs_info;
Chris Mason6324fbf2008-03-24 15:01:59 -04008433 struct btrfs_space_info *space_info;
Chris Mason9078a3e2007-04-26 16:46:15 -04008434 struct btrfs_key key;
8435 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -04008436 struct extent_buffer *leaf;
Josef Bacik0af3d002010-06-21 14:48:16 -04008437 int need_clear = 0;
8438 u64 cache_gen;
Chris Mason96b51792007-10-15 16:15:19 -04008439
Chris Masonbe744172007-05-06 10:15:01 -04008440 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04008441 key.objectid = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04008442 key.offset = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04008443 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
Chris Mason9078a3e2007-04-26 16:46:15 -04008444 path = btrfs_alloc_path();
8445 if (!path)
8446 return -ENOMEM;
8447
Josef Bacik0af3d002010-06-21 14:48:16 -04008448 cache_gen = btrfs_super_cache_generation(&root->fs_info->super_copy);
8449 if (cache_gen != 0 &&
8450 btrfs_super_generation(&root->fs_info->super_copy) != cache_gen)
8451 need_clear = 1;
Josef Bacik88c2ba32010-09-21 14:21:34 -04008452 if (btrfs_test_opt(root, CLEAR_CACHE))
8453 need_clear = 1;
Josef Bacik8216ef82010-10-28 16:55:47 -04008454 if (!btrfs_test_opt(root, SPACE_CACHE) && cache_gen)
8455 printk(KERN_INFO "btrfs: disk space caching is enabled\n");
Josef Bacik0af3d002010-06-21 14:48:16 -04008456
Chris Masond3977122009-01-05 21:25:51 -05008457 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04008458 ret = find_first_block_group(root, path, &key);
Yan, Zhengb742bb822010-05-16 10:46:24 -04008459 if (ret > 0)
8460 break;
Chris Mason0b86a832008-03-24 15:01:56 -04008461 if (ret != 0)
8462 goto error;
Chris Mason5f39d392007-10-15 16:14:19 -04008463 leaf = path->nodes[0];
8464 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason8f18cf12008-04-25 16:53:30 -04008465 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -04008466 if (!cache) {
Chris Mason0b86a832008-03-24 15:01:56 -04008467 ret = -ENOMEM;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008468 goto error;
Chris Mason9078a3e2007-04-26 16:46:15 -04008469 }
Chris Mason3e1ad542007-05-07 20:03:49 -04008470
Yan Zhengd2fb3432008-12-11 16:30:39 -05008471 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04008472 spin_lock_init(&cache->lock);
Josef Bacik6226cb02009-04-03 10:14:18 -04008473 spin_lock_init(&cache->tree_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04008474 cache->fs_info = info;
Josef Bacik0f9dd462008-09-23 13:14:11 -04008475 INIT_LIST_HEAD(&cache->list);
Chris Masonfa9c0d792009-04-03 09:47:43 -04008476 INIT_LIST_HEAD(&cache->cluster_list);
Josef Bacik96303082009-07-13 21:29:25 -04008477
Josef Bacik0af3d002010-06-21 14:48:16 -04008478 if (need_clear)
8479 cache->disk_cache_state = BTRFS_DC_CLEAR;
8480
Josef Bacik96303082009-07-13 21:29:25 -04008481 /*
8482 * we only want to have 32k of ram per block group for keeping
8483 * track of free space, and if we pass 1/2 of that we want to
8484 * start converting things over to using bitmaps
8485 */
8486 cache->extents_thresh = ((1024 * 32) / 2) /
8487 sizeof(struct btrfs_free_space);
8488
Chris Mason5f39d392007-10-15 16:14:19 -04008489 read_extent_buffer(leaf, &cache->item,
8490 btrfs_item_ptr_offset(leaf, path->slots[0]),
8491 sizeof(cache->item));
Chris Mason9078a3e2007-04-26 16:46:15 -04008492 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Mason0b86a832008-03-24 15:01:56 -04008493
Chris Mason9078a3e2007-04-26 16:46:15 -04008494 key.objectid = found_key.objectid + found_key.offset;
8495 btrfs_release_path(root, path);
Chris Mason0b86a832008-03-24 15:01:56 -04008496 cache->flags = btrfs_block_group_flags(&cache->item);
Josef Bacik817d52f2009-07-13 21:29:25 -04008497 cache->sectorsize = root->sectorsize;
8498
Josef Bacik817d52f2009-07-13 21:29:25 -04008499 /*
Josef Bacik3c148742011-02-02 15:53:47 +00008500 * We need to exclude the super stripes now so that the space
8501 * info has super bytes accounted for, otherwise we'll think
8502 * we have more space than we actually do.
8503 */
8504 exclude_super_stripes(root, cache);
8505
8506 /*
Josef Bacik817d52f2009-07-13 21:29:25 -04008507 * check for two cases, either we are full, and therefore
8508 * don't need to bother with the caching work since we won't
8509 * find any space, or we are empty, and we can just add all
8510 * the space in and be done with it. This saves us _alot_ of
8511 * time, particularly in the full case.
8512 */
8513 if (found_key.offset == btrfs_block_group_used(&cache->item)) {
Yan Zheng11833d62009-09-11 16:11:19 -04008514 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04008515 cache->cached = BTRFS_CACHE_FINISHED;
Josef Bacik1b2da372009-09-11 16:11:20 -04008516 free_excluded_extents(root, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04008517 } else if (btrfs_block_group_used(&cache->item) == 0) {
Yan Zheng11833d62009-09-11 16:11:19 -04008518 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04008519 cache->cached = BTRFS_CACHE_FINISHED;
8520 add_new_free_space(cache, root->fs_info,
8521 found_key.objectid,
8522 found_key.objectid +
8523 found_key.offset);
Yan Zheng11833d62009-09-11 16:11:19 -04008524 free_excluded_extents(root, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04008525 }
Chris Mason96b51792007-10-15 16:15:19 -04008526
Chris Mason6324fbf2008-03-24 15:01:59 -04008527 ret = update_space_info(info, cache->flags, found_key.offset,
8528 btrfs_block_group_used(&cache->item),
8529 &space_info);
8530 BUG_ON(ret);
8531 cache->space_info = space_info;
Josef Bacik1b2da372009-09-11 16:11:20 -04008532 spin_lock(&cache->space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04008533 cache->space_info->bytes_readonly += cache->bytes_super;
Josef Bacik1b2da372009-09-11 16:11:20 -04008534 spin_unlock(&cache->space_info->lock);
8535
Yan, Zhengb742bb822010-05-16 10:46:24 -04008536 __link_block_group(space_info, cache);
Chris Mason6324fbf2008-03-24 15:01:59 -04008537
Josef Bacik0f9dd462008-09-23 13:14:11 -04008538 ret = btrfs_add_block_group_cache(root->fs_info, cache);
8539 BUG_ON(ret);
Chris Mason75ccf472008-09-30 19:24:06 -04008540
8541 set_avail_alloc_bits(root->fs_info, cache->flags);
Yan Zheng2b820322008-11-17 21:11:30 -05008542 if (btrfs_chunk_readonly(root, cache->key.objectid))
Yan, Zhengf0486c62010-05-16 10:46:25 -04008543 set_block_group_ro(cache);
Chris Mason9078a3e2007-04-26 16:46:15 -04008544 }
Yan, Zhengb742bb822010-05-16 10:46:24 -04008545
8546 list_for_each_entry_rcu(space_info, &root->fs_info->space_info, list) {
8547 if (!(get_alloc_profile(root, space_info->flags) &
8548 (BTRFS_BLOCK_GROUP_RAID10 |
8549 BTRFS_BLOCK_GROUP_RAID1 |
8550 BTRFS_BLOCK_GROUP_DUP)))
8551 continue;
8552 /*
8553 * avoid allocating from un-mirrored block group if there are
8554 * mirrored block groups.
8555 */
8556 list_for_each_entry(cache, &space_info->block_groups[3], list)
Yan, Zhengf0486c62010-05-16 10:46:25 -04008557 set_block_group_ro(cache);
Yan, Zhengb742bb822010-05-16 10:46:24 -04008558 list_for_each_entry(cache, &space_info->block_groups[4], list)
Yan, Zhengf0486c62010-05-16 10:46:25 -04008559 set_block_group_ro(cache);
Yan, Zhengb742bb822010-05-16 10:46:24 -04008560 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04008561
8562 init_global_block_rsv(info);
Chris Mason0b86a832008-03-24 15:01:56 -04008563 ret = 0;
8564error:
Chris Mason9078a3e2007-04-26 16:46:15 -04008565 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04008566 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04008567}
Chris Mason6324fbf2008-03-24 15:01:59 -04008568
8569int btrfs_make_block_group(struct btrfs_trans_handle *trans,
8570 struct btrfs_root *root, u64 bytes_used,
Chris Masone17cade2008-04-15 15:41:47 -04008571 u64 type, u64 chunk_objectid, u64 chunk_offset,
Chris Mason6324fbf2008-03-24 15:01:59 -04008572 u64 size)
8573{
8574 int ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04008575 struct btrfs_root *extent_root;
8576 struct btrfs_block_group_cache *cache;
Chris Mason6324fbf2008-03-24 15:01:59 -04008577
8578 extent_root = root->fs_info->extent_root;
Chris Mason6324fbf2008-03-24 15:01:59 -04008579
Chris Mason12fcfd22009-03-24 10:24:20 -04008580 root->fs_info->last_trans_log_full_commit = trans->transid;
Chris Masone02119d2008-09-05 16:13:11 -04008581
Chris Mason8f18cf12008-04-25 16:53:30 -04008582 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Josef Bacik0f9dd462008-09-23 13:14:11 -04008583 if (!cache)
8584 return -ENOMEM;
8585
Chris Masone17cade2008-04-15 15:41:47 -04008586 cache->key.objectid = chunk_offset;
Chris Mason6324fbf2008-03-24 15:01:59 -04008587 cache->key.offset = size;
Yan Zhengd2fb3432008-12-11 16:30:39 -05008588 cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
Josef Bacik96303082009-07-13 21:29:25 -04008589 cache->sectorsize = root->sectorsize;
Josef Bacik0af3d002010-06-21 14:48:16 -04008590 cache->fs_info = root->fs_info;
Josef Bacik96303082009-07-13 21:29:25 -04008591
8592 /*
8593 * we only want to have 32k of ram per block group for keeping track
8594 * of free space, and if we pass 1/2 of that we want to start
8595 * converting things over to using bitmaps
8596 */
8597 cache->extents_thresh = ((1024 * 32) / 2) /
8598 sizeof(struct btrfs_free_space);
Yan Zhengd2fb3432008-12-11 16:30:39 -05008599 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04008600 spin_lock_init(&cache->lock);
Josef Bacik6226cb02009-04-03 10:14:18 -04008601 spin_lock_init(&cache->tree_lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04008602 INIT_LIST_HEAD(&cache->list);
Chris Masonfa9c0d792009-04-03 09:47:43 -04008603 INIT_LIST_HEAD(&cache->cluster_list);
Chris Mason0ef3e662008-05-24 14:04:53 -04008604
Chris Mason6324fbf2008-03-24 15:01:59 -04008605 btrfs_set_block_group_used(&cache->item, bytes_used);
Chris Mason6324fbf2008-03-24 15:01:59 -04008606 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
8607 cache->flags = type;
8608 btrfs_set_block_group_flags(&cache->item, type);
8609
Yan Zheng11833d62009-09-11 16:11:19 -04008610 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04008611 cache->cached = BTRFS_CACHE_FINISHED;
Yan Zheng11833d62009-09-11 16:11:19 -04008612 exclude_super_stripes(root, cache);
Josef Bacik96303082009-07-13 21:29:25 -04008613
Josef Bacik817d52f2009-07-13 21:29:25 -04008614 add_new_free_space(cache, root->fs_info, chunk_offset,
8615 chunk_offset + size);
8616
Yan Zheng11833d62009-09-11 16:11:19 -04008617 free_excluded_extents(root, cache);
8618
Chris Mason6324fbf2008-03-24 15:01:59 -04008619 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
8620 &cache->space_info);
8621 BUG_ON(ret);
Josef Bacik1b2da372009-09-11 16:11:20 -04008622
8623 spin_lock(&cache->space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04008624 cache->space_info->bytes_readonly += cache->bytes_super;
Josef Bacik1b2da372009-09-11 16:11:20 -04008625 spin_unlock(&cache->space_info->lock);
8626
Yan, Zhengb742bb822010-05-16 10:46:24 -04008627 __link_block_group(cache->space_info, cache);
Chris Mason6324fbf2008-03-24 15:01:59 -04008628
Josef Bacik0f9dd462008-09-23 13:14:11 -04008629 ret = btrfs_add_block_group_cache(root->fs_info, cache);
8630 BUG_ON(ret);
Chris Masonc286ac42008-07-22 23:06:41 -04008631
Chris Mason6324fbf2008-03-24 15:01:59 -04008632 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
8633 sizeof(cache->item));
8634 BUG_ON(ret);
8635
Chris Masond18a2c42008-04-04 15:40:00 -04008636 set_avail_alloc_bits(extent_root->fs_info, type);
Chris Mason925baed2008-06-25 16:01:30 -04008637
Chris Mason6324fbf2008-03-24 15:01:59 -04008638 return 0;
8639}
Zheng Yan1a40e232008-09-26 10:09:34 -04008640
8641int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
8642 struct btrfs_root *root, u64 group_start)
8643{
8644 struct btrfs_path *path;
8645 struct btrfs_block_group_cache *block_group;
Chris Mason44fb5512009-06-04 15:34:51 -04008646 struct btrfs_free_cluster *cluster;
Josef Bacik0af3d002010-06-21 14:48:16 -04008647 struct btrfs_root *tree_root = root->fs_info->tree_root;
Zheng Yan1a40e232008-09-26 10:09:34 -04008648 struct btrfs_key key;
Josef Bacik0af3d002010-06-21 14:48:16 -04008649 struct inode *inode;
Zheng Yan1a40e232008-09-26 10:09:34 -04008650 int ret;
Josef Bacik89a55892010-10-14 14:52:27 -04008651 int factor;
Zheng Yan1a40e232008-09-26 10:09:34 -04008652
Zheng Yan1a40e232008-09-26 10:09:34 -04008653 root = root->fs_info->extent_root;
8654
8655 block_group = btrfs_lookup_block_group(root->fs_info, group_start);
8656 BUG_ON(!block_group);
Yan Zhengc146afa2008-11-12 14:34:12 -05008657 BUG_ON(!block_group->ro);
Zheng Yan1a40e232008-09-26 10:09:34 -04008658
8659 memcpy(&key, &block_group->key, sizeof(key));
Josef Bacik89a55892010-10-14 14:52:27 -04008660 if (block_group->flags & (BTRFS_BLOCK_GROUP_DUP |
8661 BTRFS_BLOCK_GROUP_RAID1 |
8662 BTRFS_BLOCK_GROUP_RAID10))
8663 factor = 2;
8664 else
8665 factor = 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04008666
Chris Mason44fb5512009-06-04 15:34:51 -04008667 /* make sure this block group isn't part of an allocation cluster */
8668 cluster = &root->fs_info->data_alloc_cluster;
8669 spin_lock(&cluster->refill_lock);
8670 btrfs_return_cluster_to_free_space(block_group, cluster);
8671 spin_unlock(&cluster->refill_lock);
8672
8673 /*
8674 * make sure this block group isn't part of a metadata
8675 * allocation cluster
8676 */
8677 cluster = &root->fs_info->meta_alloc_cluster;
8678 spin_lock(&cluster->refill_lock);
8679 btrfs_return_cluster_to_free_space(block_group, cluster);
8680 spin_unlock(&cluster->refill_lock);
8681
Zheng Yan1a40e232008-09-26 10:09:34 -04008682 path = btrfs_alloc_path();
8683 BUG_ON(!path);
8684
Josef Bacik0af3d002010-06-21 14:48:16 -04008685 inode = lookup_free_space_inode(root, block_group, path);
8686 if (!IS_ERR(inode)) {
8687 btrfs_orphan_add(trans, inode);
8688 clear_nlink(inode);
8689 /* One for the block groups ref */
8690 spin_lock(&block_group->lock);
8691 if (block_group->iref) {
8692 block_group->iref = 0;
8693 block_group->inode = NULL;
8694 spin_unlock(&block_group->lock);
8695 iput(inode);
8696 } else {
8697 spin_unlock(&block_group->lock);
8698 }
8699 /* One for our lookup ref */
8700 iput(inode);
8701 }
8702
8703 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
8704 key.offset = block_group->key.objectid;
8705 key.type = 0;
8706
8707 ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1);
8708 if (ret < 0)
8709 goto out;
8710 if (ret > 0)
8711 btrfs_release_path(tree_root, path);
8712 if (ret == 0) {
8713 ret = btrfs_del_item(trans, tree_root, path);
8714 if (ret)
8715 goto out;
8716 btrfs_release_path(tree_root, path);
8717 }
8718
Yan Zheng3dfdb932009-01-21 10:49:16 -05008719 spin_lock(&root->fs_info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04008720 rb_erase(&block_group->cache_node,
8721 &root->fs_info->block_group_cache_tree);
Yan Zheng3dfdb932009-01-21 10:49:16 -05008722 spin_unlock(&root->fs_info->block_group_cache_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04008723
Josef Bacik80eb2342008-10-29 14:49:05 -04008724 down_write(&block_group->space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04008725 /*
8726 * we must use list_del_init so people can check to see if they
8727 * are still on the list after taking the semaphore
8728 */
8729 list_del_init(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04008730 up_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04008731
Josef Bacik817d52f2009-07-13 21:29:25 -04008732 if (block_group->cached == BTRFS_CACHE_STARTED)
Yan Zheng11833d62009-09-11 16:11:19 -04008733 wait_block_group_cache_done(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -04008734
8735 btrfs_remove_free_space_cache(block_group);
8736
Yan Zhengc146afa2008-11-12 14:34:12 -05008737 spin_lock(&block_group->space_info->lock);
8738 block_group->space_info->total_bytes -= block_group->key.offset;
8739 block_group->space_info->bytes_readonly -= block_group->key.offset;
Josef Bacik89a55892010-10-14 14:52:27 -04008740 block_group->space_info->disk_total -= block_group->key.offset * factor;
Yan Zhengc146afa2008-11-12 14:34:12 -05008741 spin_unlock(&block_group->space_info->lock);
Chris Mason283bb192009-07-24 16:30:55 -04008742
Josef Bacik0af3d002010-06-21 14:48:16 -04008743 memcpy(&key, &block_group->key, sizeof(key));
8744
Chris Mason283bb192009-07-24 16:30:55 -04008745 btrfs_clear_space_info_full(root->fs_info);
Yan Zhengc146afa2008-11-12 14:34:12 -05008746
Chris Masonfa9c0d792009-04-03 09:47:43 -04008747 btrfs_put_block_group(block_group);
8748 btrfs_put_block_group(block_group);
Zheng Yan1a40e232008-09-26 10:09:34 -04008749
8750 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
8751 if (ret > 0)
8752 ret = -EIO;
8753 if (ret < 0)
8754 goto out;
8755
8756 ret = btrfs_del_item(trans, root, path);
8757out:
8758 btrfs_free_path(path);
8759 return ret;
8760}
liuboacce9522011-01-06 19:30:25 +08008761
8762int btrfs_error_unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
8763{
8764 return unpin_extent_range(root, start, end);
8765}
8766
8767int btrfs_error_discard_extent(struct btrfs_root *root, u64 bytenr,
8768 u64 num_bytes)
8769{
8770 return btrfs_discard_extent(root, bytenr, num_bytes);
8771}