blob: 5154b91f6380a2c0766a31ad9f3d8230b9ebb76e [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>
David Sterbadff51cd2011-06-14 12:52:17 +020026#include <linux/ratelimit.h>
Josef Bacikb150a4f2013-06-19 15:00:04 -040027#include <linux/percpu_counter.h>
Chris Mason4b4e25f2008-11-20 10:22:27 -050028#include "compat.h"
Chris Mason74493f72007-12-11 09:25:06 -050029#include "hash.h"
Chris Masonfec577f2007-02-26 10:40:21 -050030#include "ctree.h"
31#include "disk-io.h"
32#include "print-tree.h"
Chris Masone089f052007-03-16 16:20:31 -040033#include "transaction.h"
Chris Mason0b86a832008-03-24 15:01:56 -040034#include "volumes.h"
David Woodhouse53b381b2013-01-29 18:40:14 -050035#include "raid56.h"
Chris Mason925baed2008-06-25 16:01:30 -040036#include "locking.h"
Chris Masonfa9c0d72009-04-03 09:47:43 -040037#include "free-space-cache.h"
Miao Xie3fed40c2012-09-13 04:51:36 -060038#include "math.h"
Chris Masonfec577f2007-02-26 10:40:21 -050039
Arne Jansen709c0482011-09-12 12:22:57 +020040#undef SCRAMBLE_DELAYED_REFS
41
Miao Xie9e622d62012-01-26 15:01:12 -050042/*
43 * control flags for do_chunk_alloc's force field
Chris Mason0e4f8f82011-04-15 16:05:44 -040044 * CHUNK_ALLOC_NO_FORCE means to only allocate a chunk
45 * if we really need one.
46 *
Chris Mason0e4f8f82011-04-15 16:05:44 -040047 * CHUNK_ALLOC_LIMITED means to only try and allocate one
48 * if we have very few chunks already allocated. This is
49 * used as part of the clustering code to help make sure
50 * we have a good pool of storage to cluster in, without
51 * filling the FS with empty chunks
52 *
Miao Xie9e622d62012-01-26 15:01:12 -050053 * CHUNK_ALLOC_FORCE means it must try to allocate one
54 *
Chris Mason0e4f8f82011-04-15 16:05:44 -040055 */
56enum {
57 CHUNK_ALLOC_NO_FORCE = 0,
Miao Xie9e622d62012-01-26 15:01:12 -050058 CHUNK_ALLOC_LIMITED = 1,
59 CHUNK_ALLOC_FORCE = 2,
Chris Mason0e4f8f82011-04-15 16:05:44 -040060};
61
Josef Bacikfb25e912011-07-26 17:00:46 -040062/*
63 * Control how reservations are dealt with.
64 *
65 * RESERVE_FREE - freeing a reservation.
66 * RESERVE_ALLOC - allocating space and we need to update bytes_may_use for
67 * ENOSPC accounting
68 * RESERVE_ALLOC_NO_ACCOUNT - allocating space and we should not update
69 * bytes_may_use as the ENOSPC accounting is done elsewhere
70 */
71enum {
72 RESERVE_FREE = 0,
73 RESERVE_ALLOC = 1,
74 RESERVE_ALLOC_NO_ACCOUNT = 2,
75};
76
Liu Boc53d6132012-12-27 09:01:19 +000077static int update_block_group(struct btrfs_root *root,
Yan, Zhengf0486c62010-05-16 10:46:25 -040078 u64 bytenr, u64 num_bytes, int alloc);
Yan Zheng5d4f98a2009-06-10 10:45:14 -040079static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
80 struct btrfs_root *root,
81 u64 bytenr, u64 num_bytes, u64 parent,
82 u64 root_objectid, u64 owner_objectid,
83 u64 owner_offset, int refs_to_drop,
84 struct btrfs_delayed_extent_op *extra_op);
85static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
86 struct extent_buffer *leaf,
87 struct btrfs_extent_item *ei);
88static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
89 struct btrfs_root *root,
90 u64 parent, u64 root_objectid,
91 u64 flags, u64 owner, u64 offset,
92 struct btrfs_key *ins, int ref_mod);
93static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
94 struct btrfs_root *root,
95 u64 parent, u64 root_objectid,
96 u64 flags, struct btrfs_disk_key *key,
97 int level, struct btrfs_key *ins);
Josef Bacik6a632092009-02-20 11:00:09 -050098static int do_chunk_alloc(struct btrfs_trans_handle *trans,
Josef Bacik698d0082012-09-12 14:08:47 -040099 struct btrfs_root *extent_root, u64 flags,
100 int force);
Yan Zheng11833d62009-09-11 16:11:19 -0400101static int find_next_key(struct btrfs_path *path, int level,
102 struct btrfs_key *key);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400103static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
104 int dump_block_groups);
Josef Bacikfb25e912011-07-26 17:00:46 -0400105static int btrfs_update_reserved_bytes(struct btrfs_block_group_cache *cache,
106 u64 num_bytes, int reserve);
Josef Bacik5d803662013-02-07 16:06:02 -0500107static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv,
108 u64 num_bytes);
Eric Sandeen48a3b632013-04-25 20:41:01 +0000109int btrfs_pin_extent(struct btrfs_root *root,
110 u64 bytenr, u64 num_bytes, int reserved);
Josef Bacik6a632092009-02-20 11:00:09 -0500111
Josef Bacik817d52f2009-07-13 21:29:25 -0400112static noinline int
113block_group_cache_done(struct btrfs_block_group_cache *cache)
114{
115 smp_mb();
116 return cache->cached == BTRFS_CACHE_FINISHED;
117}
118
Josef Bacik0f9dd462008-09-23 13:14:11 -0400119static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
120{
121 return (cache->flags & bits) == bits;
122}
123
David Sterba62a45b62011-04-20 15:52:26 +0200124static void btrfs_get_block_group(struct btrfs_block_group_cache *cache)
Josef Bacik11dfe352009-11-13 20:12:59 +0000125{
126 atomic_inc(&cache->count);
127}
128
129void btrfs_put_block_group(struct btrfs_block_group_cache *cache)
130{
Yan, Zhengf0486c62010-05-16 10:46:25 -0400131 if (atomic_dec_and_test(&cache->count)) {
132 WARN_ON(cache->pinned > 0);
133 WARN_ON(cache->reserved > 0);
Li Zefan34d52cb6c2011-03-29 13:46:06 +0800134 kfree(cache->free_space_ctl);
Josef Bacik11dfe352009-11-13 20:12:59 +0000135 kfree(cache);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400136 }
Josef Bacik11dfe352009-11-13 20:12:59 +0000137}
138
Josef Bacik0f9dd462008-09-23 13:14:11 -0400139/*
140 * this adds the block group to the fs_info rb tree for the block group
141 * cache
142 */
Christoph Hellwigb2950862008-12-02 09:54:17 -0500143static int btrfs_add_block_group_cache(struct btrfs_fs_info *info,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400144 struct btrfs_block_group_cache *block_group)
145{
146 struct rb_node **p;
147 struct rb_node *parent = NULL;
148 struct btrfs_block_group_cache *cache;
149
150 spin_lock(&info->block_group_cache_lock);
151 p = &info->block_group_cache_tree.rb_node;
152
153 while (*p) {
154 parent = *p;
155 cache = rb_entry(parent, struct btrfs_block_group_cache,
156 cache_node);
157 if (block_group->key.objectid < cache->key.objectid) {
158 p = &(*p)->rb_left;
159 } else if (block_group->key.objectid > cache->key.objectid) {
160 p = &(*p)->rb_right;
161 } else {
162 spin_unlock(&info->block_group_cache_lock);
163 return -EEXIST;
164 }
165 }
166
167 rb_link_node(&block_group->cache_node, parent, p);
168 rb_insert_color(&block_group->cache_node,
169 &info->block_group_cache_tree);
Liu Boa1897fd2012-12-27 09:01:23 +0000170
171 if (info->first_logical_byte > block_group->key.objectid)
172 info->first_logical_byte = block_group->key.objectid;
173
Josef Bacik0f9dd462008-09-23 13:14:11 -0400174 spin_unlock(&info->block_group_cache_lock);
175
176 return 0;
177}
178
179/*
180 * This will return the block group at or after bytenr if contains is 0, else
181 * it will return the block group that contains the bytenr
182 */
183static struct btrfs_block_group_cache *
184block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr,
185 int contains)
186{
187 struct btrfs_block_group_cache *cache, *ret = NULL;
188 struct rb_node *n;
189 u64 end, start;
190
191 spin_lock(&info->block_group_cache_lock);
192 n = info->block_group_cache_tree.rb_node;
193
194 while (n) {
195 cache = rb_entry(n, struct btrfs_block_group_cache,
196 cache_node);
197 end = cache->key.objectid + cache->key.offset - 1;
198 start = cache->key.objectid;
199
200 if (bytenr < start) {
201 if (!contains && (!ret || start < ret->key.objectid))
202 ret = cache;
203 n = n->rb_left;
204 } else if (bytenr > start) {
205 if (contains && bytenr <= end) {
206 ret = cache;
207 break;
208 }
209 n = n->rb_right;
210 } else {
211 ret = cache;
212 break;
213 }
214 }
Liu Boa1897fd2012-12-27 09:01:23 +0000215 if (ret) {
Josef Bacik11dfe352009-11-13 20:12:59 +0000216 btrfs_get_block_group(ret);
Liu Boa1897fd2012-12-27 09:01:23 +0000217 if (bytenr == 0 && info->first_logical_byte > ret->key.objectid)
218 info->first_logical_byte = ret->key.objectid;
219 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400220 spin_unlock(&info->block_group_cache_lock);
221
222 return ret;
223}
224
Yan Zheng11833d62009-09-11 16:11:19 -0400225static int add_excluded_extent(struct btrfs_root *root,
226 u64 start, u64 num_bytes)
Josef Bacik817d52f2009-07-13 21:29:25 -0400227{
Yan Zheng11833d62009-09-11 16:11:19 -0400228 u64 end = start + num_bytes - 1;
229 set_extent_bits(&root->fs_info->freed_extents[0],
230 start, end, EXTENT_UPTODATE, GFP_NOFS);
231 set_extent_bits(&root->fs_info->freed_extents[1],
232 start, end, EXTENT_UPTODATE, GFP_NOFS);
233 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400234}
235
Yan Zheng11833d62009-09-11 16:11:19 -0400236static void free_excluded_extents(struct btrfs_root *root,
237 struct btrfs_block_group_cache *cache)
Josef Bacik817d52f2009-07-13 21:29:25 -0400238{
Yan Zheng11833d62009-09-11 16:11:19 -0400239 u64 start, end;
240
241 start = cache->key.objectid;
242 end = start + cache->key.offset - 1;
243
244 clear_extent_bits(&root->fs_info->freed_extents[0],
245 start, end, EXTENT_UPTODATE, GFP_NOFS);
246 clear_extent_bits(&root->fs_info->freed_extents[1],
247 start, end, EXTENT_UPTODATE, GFP_NOFS);
248}
249
250static int exclude_super_stripes(struct btrfs_root *root,
251 struct btrfs_block_group_cache *cache)
252{
Josef Bacik817d52f2009-07-13 21:29:25 -0400253 u64 bytenr;
254 u64 *logical;
255 int stripe_len;
256 int i, nr, ret;
257
Yan, Zheng06b23312009-11-26 09:31:11 +0000258 if (cache->key.objectid < BTRFS_SUPER_INFO_OFFSET) {
259 stripe_len = BTRFS_SUPER_INFO_OFFSET - cache->key.objectid;
260 cache->bytes_super += stripe_len;
261 ret = add_excluded_extent(root, cache->key.objectid,
262 stripe_len);
Josef Bacik835d9742013-03-19 12:13:25 -0400263 if (ret)
264 return ret;
Yan, Zheng06b23312009-11-26 09:31:11 +0000265 }
266
Josef Bacik817d52f2009-07-13 21:29:25 -0400267 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
268 bytenr = btrfs_sb_offset(i);
269 ret = btrfs_rmap_block(&root->fs_info->mapping_tree,
270 cache->key.objectid, bytenr,
271 0, &logical, &nr, &stripe_len);
Josef Bacik835d9742013-03-19 12:13:25 -0400272 if (ret)
273 return ret;
Yan Zheng11833d62009-09-11 16:11:19 -0400274
Josef Bacik817d52f2009-07-13 21:29:25 -0400275 while (nr--) {
Josef Bacik51bf5f02013-04-23 12:55:21 -0400276 u64 start, len;
277
278 if (logical[nr] > cache->key.objectid +
279 cache->key.offset)
280 continue;
281
282 if (logical[nr] + stripe_len <= cache->key.objectid)
283 continue;
284
285 start = logical[nr];
286 if (start < cache->key.objectid) {
287 start = cache->key.objectid;
288 len = (logical[nr] + stripe_len) - start;
289 } else {
290 len = min_t(u64, stripe_len,
291 cache->key.objectid +
292 cache->key.offset - start);
293 }
294
295 cache->bytes_super += len;
296 ret = add_excluded_extent(root, start, len);
Josef Bacik835d9742013-03-19 12:13:25 -0400297 if (ret) {
298 kfree(logical);
299 return ret;
300 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400301 }
Yan Zheng11833d62009-09-11 16:11:19 -0400302
Josef Bacik817d52f2009-07-13 21:29:25 -0400303 kfree(logical);
304 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400305 return 0;
306}
307
Yan Zheng11833d62009-09-11 16:11:19 -0400308static struct btrfs_caching_control *
309get_caching_control(struct btrfs_block_group_cache *cache)
310{
311 struct btrfs_caching_control *ctl;
312
313 spin_lock(&cache->lock);
314 if (cache->cached != BTRFS_CACHE_STARTED) {
315 spin_unlock(&cache->lock);
316 return NULL;
317 }
318
Josef Bacikdde5abe2010-09-16 16:17:03 -0400319 /* We're loading it the fast way, so we don't have a caching_ctl. */
320 if (!cache->caching_ctl) {
321 spin_unlock(&cache->lock);
322 return NULL;
323 }
324
Yan Zheng11833d62009-09-11 16:11:19 -0400325 ctl = cache->caching_ctl;
326 atomic_inc(&ctl->count);
327 spin_unlock(&cache->lock);
328 return ctl;
329}
330
331static void put_caching_control(struct btrfs_caching_control *ctl)
332{
333 if (atomic_dec_and_test(&ctl->count))
334 kfree(ctl);
335}
336
Josef Bacik0f9dd462008-09-23 13:14:11 -0400337/*
338 * this is only called by cache_block_group, since we could have freed extents
339 * we need to check the pinned_extents for any extents that can't be used yet
340 * since their free space will be released as soon as the transaction commits.
341 */
Josef Bacik817d52f2009-07-13 21:29:25 -0400342static u64 add_new_free_space(struct btrfs_block_group_cache *block_group,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400343 struct btrfs_fs_info *info, u64 start, u64 end)
344{
Josef Bacik817d52f2009-07-13 21:29:25 -0400345 u64 extent_start, extent_end, size, total_added = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400346 int ret;
347
348 while (start < end) {
Yan Zheng11833d62009-09-11 16:11:19 -0400349 ret = find_first_extent_bit(info->pinned_extents, start,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400350 &extent_start, &extent_end,
Josef Bacike6138872012-09-27 17:07:30 -0400351 EXTENT_DIRTY | EXTENT_UPTODATE,
352 NULL);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400353 if (ret)
354 break;
355
Yan, Zheng06b23312009-11-26 09:31:11 +0000356 if (extent_start <= start) {
Josef Bacik0f9dd462008-09-23 13:14:11 -0400357 start = extent_end + 1;
358 } else if (extent_start > start && extent_start < end) {
359 size = extent_start - start;
Josef Bacik817d52f2009-07-13 21:29:25 -0400360 total_added += size;
Josef Bacikea6a4782008-11-20 12:16:16 -0500361 ret = btrfs_add_free_space(block_group, start,
362 size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100363 BUG_ON(ret); /* -ENOMEM or logic error */
Josef Bacik0f9dd462008-09-23 13:14:11 -0400364 start = extent_end + 1;
365 } else {
366 break;
367 }
368 }
369
370 if (start < end) {
371 size = end - start;
Josef Bacik817d52f2009-07-13 21:29:25 -0400372 total_added += size;
Josef Bacikea6a4782008-11-20 12:16:16 -0500373 ret = btrfs_add_free_space(block_group, start, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100374 BUG_ON(ret); /* -ENOMEM or logic error */
Josef Bacik0f9dd462008-09-23 13:14:11 -0400375 }
376
Josef Bacik817d52f2009-07-13 21:29:25 -0400377 return total_added;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400378}
379
Josef Bacikbab39bf2011-06-30 14:42:28 -0400380static noinline void caching_thread(struct btrfs_work *work)
Chris Masone37c9e62007-05-09 20:13:14 -0400381{
Josef Bacikbab39bf2011-06-30 14:42:28 -0400382 struct btrfs_block_group_cache *block_group;
383 struct btrfs_fs_info *fs_info;
384 struct btrfs_caching_control *caching_ctl;
385 struct btrfs_root *extent_root;
Chris Masone37c9e62007-05-09 20:13:14 -0400386 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400387 struct extent_buffer *leaf;
Yan Zheng11833d62009-09-11 16:11:19 -0400388 struct btrfs_key key;
Josef Bacik817d52f2009-07-13 21:29:25 -0400389 u64 total_found = 0;
Yan Zheng11833d62009-09-11 16:11:19 -0400390 u64 last = 0;
391 u32 nritems;
392 int ret = 0;
Chris Masonf510cfe2007-10-15 16:14:48 -0400393
Josef Bacikbab39bf2011-06-30 14:42:28 -0400394 caching_ctl = container_of(work, struct btrfs_caching_control, work);
395 block_group = caching_ctl->block_group;
396 fs_info = block_group->fs_info;
397 extent_root = fs_info->extent_root;
398
Chris Masone37c9e62007-05-09 20:13:14 -0400399 path = btrfs_alloc_path();
400 if (!path)
Josef Bacikbab39bf2011-06-30 14:42:28 -0400401 goto out;
Yan7d7d6062007-09-14 16:15:28 -0400402
Josef Bacik817d52f2009-07-13 21:29:25 -0400403 last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET);
Yan Zheng11833d62009-09-11 16:11:19 -0400404
Chris Mason5cd57b22008-06-25 16:01:30 -0400405 /*
Josef Bacik817d52f2009-07-13 21:29:25 -0400406 * We don't want to deadlock with somebody trying to allocate a new
407 * extent for the extent root while also trying to search the extent
408 * root to add free space. So we skip locking and search the commit
409 * root, since its read-only
Chris Mason5cd57b22008-06-25 16:01:30 -0400410 */
411 path->skip_locking = 1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400412 path->search_commit_root = 1;
Josef Bacik026fd312011-05-13 10:32:11 -0400413 path->reada = 1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400414
Yan Zhenge4404d62008-12-12 10:03:26 -0500415 key.objectid = last;
Chris Masone37c9e62007-05-09 20:13:14 -0400416 key.offset = 0;
Yan Zheng11833d62009-09-11 16:11:19 -0400417 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason013f1b12009-07-31 14:57:55 -0400418again:
Yan Zheng11833d62009-09-11 16:11:19 -0400419 mutex_lock(&caching_ctl->mutex);
Chris Mason013f1b12009-07-31 14:57:55 -0400420 /* need to make sure the commit_root doesn't disappear */
421 down_read(&fs_info->extent_commit_sem);
422
Yan Zheng11833d62009-09-11 16:11:19 -0400423 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
Chris Masone37c9e62007-05-09 20:13:14 -0400424 if (ret < 0)
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400425 goto err;
Yan Zhenga512bbf2008-12-08 16:46:26 -0500426
Yan Zheng11833d62009-09-11 16:11:19 -0400427 leaf = path->nodes[0];
428 nritems = btrfs_header_nritems(leaf);
429
Chris Masond3977122009-01-05 21:25:51 -0500430 while (1) {
David Sterba7841cb22011-05-31 18:07:27 +0200431 if (btrfs_fs_closing(fs_info) > 1) {
Yan Zhengf25784b2009-07-28 08:41:57 -0400432 last = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400433 break;
Yan Zhengf25784b2009-07-28 08:41:57 -0400434 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400435
Yan Zheng11833d62009-09-11 16:11:19 -0400436 if (path->slots[0] < nritems) {
437 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
438 } else {
439 ret = find_next_key(path, 0, &key);
440 if (ret)
Chris Masone37c9e62007-05-09 20:13:14 -0400441 break;
Josef Bacik817d52f2009-07-13 21:29:25 -0400442
Josef Bacik0a3896d2013-04-19 14:37:26 -0400443 if (need_resched()) {
Josef Bacik589d8ad2011-05-11 17:30:53 -0400444 caching_ctl->progress = last;
Chris Masonff5714c2011-05-28 07:00:39 -0400445 btrfs_release_path(path);
Josef Bacik589d8ad2011-05-11 17:30:53 -0400446 up_read(&fs_info->extent_commit_sem);
447 mutex_unlock(&caching_ctl->mutex);
Yan Zheng11833d62009-09-11 16:11:19 -0400448 cond_resched();
Josef Bacik589d8ad2011-05-11 17:30:53 -0400449 goto again;
450 }
Josef Bacik0a3896d2013-04-19 14:37:26 -0400451
452 ret = btrfs_next_leaf(extent_root, path);
453 if (ret < 0)
454 goto err;
455 if (ret)
456 break;
Josef Bacik589d8ad2011-05-11 17:30:53 -0400457 leaf = path->nodes[0];
458 nritems = btrfs_header_nritems(leaf);
459 continue;
Yan Zheng11833d62009-09-11 16:11:19 -0400460 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400461
Yan Zheng11833d62009-09-11 16:11:19 -0400462 if (key.objectid < block_group->key.objectid) {
463 path->slots[0]++;
Josef Bacik817d52f2009-07-13 21:29:25 -0400464 continue;
Chris Masone37c9e62007-05-09 20:13:14 -0400465 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400466
Chris Masone37c9e62007-05-09 20:13:14 -0400467 if (key.objectid >= block_group->key.objectid +
Josef Bacik0f9dd462008-09-23 13:14:11 -0400468 block_group->key.offset)
Yan7d7d6062007-09-14 16:15:28 -0400469 break;
Yan7d7d6062007-09-14 16:15:28 -0400470
Josef Bacik3173a182013-03-07 14:22:04 -0500471 if (key.type == BTRFS_EXTENT_ITEM_KEY ||
472 key.type == BTRFS_METADATA_ITEM_KEY) {
Josef Bacik817d52f2009-07-13 21:29:25 -0400473 total_found += add_new_free_space(block_group,
474 fs_info, last,
475 key.objectid);
Josef Bacik3173a182013-03-07 14:22:04 -0500476 if (key.type == BTRFS_METADATA_ITEM_KEY)
477 last = key.objectid +
478 fs_info->tree_root->leafsize;
479 else
480 last = key.objectid + key.offset;
Josef Bacik817d52f2009-07-13 21:29:25 -0400481
Yan Zheng11833d62009-09-11 16:11:19 -0400482 if (total_found > (1024 * 1024 * 2)) {
483 total_found = 0;
484 wake_up(&caching_ctl->wait);
485 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400486 }
Chris Masone37c9e62007-05-09 20:13:14 -0400487 path->slots[0]++;
488 }
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400489 ret = 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400490
491 total_found += add_new_free_space(block_group, fs_info, last,
492 block_group->key.objectid +
493 block_group->key.offset);
Yan Zheng11833d62009-09-11 16:11:19 -0400494 caching_ctl->progress = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400495
496 spin_lock(&block_group->lock);
Yan Zheng11833d62009-09-11 16:11:19 -0400497 block_group->caching_ctl = NULL;
Josef Bacik817d52f2009-07-13 21:29:25 -0400498 block_group->cached = BTRFS_CACHE_FINISHED;
499 spin_unlock(&block_group->lock);
500
Chris Mason54aa1f42007-06-22 14:16:25 -0400501err:
Chris Masone37c9e62007-05-09 20:13:14 -0400502 btrfs_free_path(path);
Yan Zheng276e6802009-07-30 09:40:40 -0400503 up_read(&fs_info->extent_commit_sem);
Josef Bacik817d52f2009-07-13 21:29:25 -0400504
Yan Zheng11833d62009-09-11 16:11:19 -0400505 free_excluded_extents(extent_root, block_group);
506
507 mutex_unlock(&caching_ctl->mutex);
Josef Bacikbab39bf2011-06-30 14:42:28 -0400508out:
Yan Zheng11833d62009-09-11 16:11:19 -0400509 wake_up(&caching_ctl->wait);
510
511 put_caching_control(caching_ctl);
Josef Bacik11dfe352009-11-13 20:12:59 +0000512 btrfs_put_block_group(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -0400513}
514
Josef Bacik9d66e232010-08-25 16:54:15 -0400515static int cache_block_group(struct btrfs_block_group_cache *cache,
Josef Bacik9d66e232010-08-25 16:54:15 -0400516 int load_cache_only)
Josef Bacik817d52f2009-07-13 21:29:25 -0400517{
Josef Bacik291c7d22011-11-14 13:52:14 -0500518 DEFINE_WAIT(wait);
Yan Zheng11833d62009-09-11 16:11:19 -0400519 struct btrfs_fs_info *fs_info = cache->fs_info;
520 struct btrfs_caching_control *caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -0400521 int ret = 0;
522
Josef Bacik291c7d22011-11-14 13:52:14 -0500523 caching_ctl = kzalloc(sizeof(*caching_ctl), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100524 if (!caching_ctl)
525 return -ENOMEM;
Josef Bacik291c7d22011-11-14 13:52:14 -0500526
527 INIT_LIST_HEAD(&caching_ctl->list);
528 mutex_init(&caching_ctl->mutex);
529 init_waitqueue_head(&caching_ctl->wait);
530 caching_ctl->block_group = cache;
531 caching_ctl->progress = cache->key.objectid;
532 atomic_set(&caching_ctl->count, 1);
533 caching_ctl->work.func = caching_thread;
534
535 spin_lock(&cache->lock);
536 /*
537 * This should be a rare occasion, but this could happen I think in the
538 * case where one thread starts to load the space cache info, and then
539 * some other thread starts a transaction commit which tries to do an
540 * allocation while the other thread is still loading the space cache
541 * info. The previous loop should have kept us from choosing this block
542 * group, but if we've moved to the state where we will wait on caching
543 * block groups we need to first check if we're doing a fast load here,
544 * so we can wait for it to finish, otherwise we could end up allocating
545 * from a block group who's cache gets evicted for one reason or
546 * another.
547 */
548 while (cache->cached == BTRFS_CACHE_FAST) {
549 struct btrfs_caching_control *ctl;
550
551 ctl = cache->caching_ctl;
552 atomic_inc(&ctl->count);
553 prepare_to_wait(&ctl->wait, &wait, TASK_UNINTERRUPTIBLE);
554 spin_unlock(&cache->lock);
555
556 schedule();
557
558 finish_wait(&ctl->wait, &wait);
559 put_caching_control(ctl);
560 spin_lock(&cache->lock);
561 }
562
563 if (cache->cached != BTRFS_CACHE_NO) {
564 spin_unlock(&cache->lock);
565 kfree(caching_ctl);
Yan Zheng11833d62009-09-11 16:11:19 -0400566 return 0;
Josef Bacik291c7d22011-11-14 13:52:14 -0500567 }
568 WARN_ON(cache->caching_ctl);
569 cache->caching_ctl = caching_ctl;
570 cache->cached = BTRFS_CACHE_FAST;
571 spin_unlock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -0400572
Josef Bacikd53ba472012-04-12 16:03:57 -0400573 if (fs_info->mount_opt & BTRFS_MOUNT_SPACE_CACHE) {
Josef Bacik9d66e232010-08-25 16:54:15 -0400574 ret = load_free_space_cache(fs_info, cache);
575
576 spin_lock(&cache->lock);
577 if (ret == 1) {
Josef Bacik291c7d22011-11-14 13:52:14 -0500578 cache->caching_ctl = NULL;
Josef Bacik9d66e232010-08-25 16:54:15 -0400579 cache->cached = BTRFS_CACHE_FINISHED;
580 cache->last_byte_to_unpin = (u64)-1;
581 } else {
Josef Bacik291c7d22011-11-14 13:52:14 -0500582 if (load_cache_only) {
583 cache->caching_ctl = NULL;
584 cache->cached = BTRFS_CACHE_NO;
585 } else {
586 cache->cached = BTRFS_CACHE_STARTED;
587 }
Josef Bacik9d66e232010-08-25 16:54:15 -0400588 }
589 spin_unlock(&cache->lock);
Josef Bacik291c7d22011-11-14 13:52:14 -0500590 wake_up(&caching_ctl->wait);
Josef Bacik3c148742011-02-02 15:53:47 +0000591 if (ret == 1) {
Josef Bacik291c7d22011-11-14 13:52:14 -0500592 put_caching_control(caching_ctl);
Josef Bacik3c148742011-02-02 15:53:47 +0000593 free_excluded_extents(fs_info->extent_root, cache);
Josef Bacik9d66e232010-08-25 16:54:15 -0400594 return 0;
Josef Bacik3c148742011-02-02 15:53:47 +0000595 }
Josef Bacik291c7d22011-11-14 13:52:14 -0500596 } else {
597 /*
598 * We are not going to do the fast caching, set cached to the
599 * appropriate value and wakeup any waiters.
600 */
601 spin_lock(&cache->lock);
602 if (load_cache_only) {
603 cache->caching_ctl = NULL;
604 cache->cached = BTRFS_CACHE_NO;
605 } else {
606 cache->cached = BTRFS_CACHE_STARTED;
607 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400608 spin_unlock(&cache->lock);
Josef Bacik291c7d22011-11-14 13:52:14 -0500609 wake_up(&caching_ctl->wait);
610 }
611
612 if (load_cache_only) {
613 put_caching_control(caching_ctl);
Yan Zheng11833d62009-09-11 16:11:19 -0400614 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400615 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400616
Yan Zheng11833d62009-09-11 16:11:19 -0400617 down_write(&fs_info->extent_commit_sem);
Josef Bacik291c7d22011-11-14 13:52:14 -0500618 atomic_inc(&caching_ctl->count);
Yan Zheng11833d62009-09-11 16:11:19 -0400619 list_add_tail(&caching_ctl->list, &fs_info->caching_block_groups);
620 up_write(&fs_info->extent_commit_sem);
621
Josef Bacik11dfe352009-11-13 20:12:59 +0000622 btrfs_get_block_group(cache);
Yan Zheng11833d62009-09-11 16:11:19 -0400623
Josef Bacikbab39bf2011-06-30 14:42:28 -0400624 btrfs_queue_worker(&fs_info->caching_workers, &caching_ctl->work);
Josef Bacik817d52f2009-07-13 21:29:25 -0400625
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400626 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -0400627}
628
Josef Bacik0f9dd462008-09-23 13:14:11 -0400629/*
630 * return the block group that starts at or after bytenr
631 */
Chris Masond3977122009-01-05 21:25:51 -0500632static struct btrfs_block_group_cache *
633btrfs_lookup_first_block_group(struct btrfs_fs_info *info, u64 bytenr)
Chris Mason0ef3e662008-05-24 14:04:53 -0400634{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400635 struct btrfs_block_group_cache *cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400636
Josef Bacik0f9dd462008-09-23 13:14:11 -0400637 cache = block_group_cache_tree_search(info, bytenr, 0);
Chris Mason0ef3e662008-05-24 14:04:53 -0400638
Josef Bacik0f9dd462008-09-23 13:14:11 -0400639 return cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400640}
641
Josef Bacik0f9dd462008-09-23 13:14:11 -0400642/*
Sankar P9f556842009-05-14 13:52:22 -0400643 * return the block group that contains the given bytenr
Josef Bacik0f9dd462008-09-23 13:14:11 -0400644 */
Chris Masond3977122009-01-05 21:25:51 -0500645struct btrfs_block_group_cache *btrfs_lookup_block_group(
646 struct btrfs_fs_info *info,
647 u64 bytenr)
Chris Masonbe744172007-05-06 10:15:01 -0400648{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400649 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -0400650
Josef Bacik0f9dd462008-09-23 13:14:11 -0400651 cache = block_group_cache_tree_search(info, bytenr, 1);
Chris Mason96b51792007-10-15 16:15:19 -0400652
Josef Bacik0f9dd462008-09-23 13:14:11 -0400653 return cache;
Chris Masonbe744172007-05-06 10:15:01 -0400654}
Chris Mason0b86a832008-03-24 15:01:56 -0400655
Josef Bacik0f9dd462008-09-23 13:14:11 -0400656static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
657 u64 flags)
Chris Mason6324fbf2008-03-24 15:01:59 -0400658{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400659 struct list_head *head = &info->space_info;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400660 struct btrfs_space_info *found;
Chris Mason4184ea72009-03-10 12:39:20 -0400661
Ilya Dryomov52ba6922012-01-16 22:04:47 +0200662 flags &= BTRFS_BLOCK_GROUP_TYPE_MASK;
Yan, Zhengb742bb82010-05-16 10:46:24 -0400663
Chris Mason4184ea72009-03-10 12:39:20 -0400664 rcu_read_lock();
665 list_for_each_entry_rcu(found, head, list) {
Josef Bacik67377732010-09-16 16:19:09 -0400666 if (found->flags & flags) {
Chris Mason4184ea72009-03-10 12:39:20 -0400667 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -0400668 return found;
Chris Mason4184ea72009-03-10 12:39:20 -0400669 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400670 }
Chris Mason4184ea72009-03-10 12:39:20 -0400671 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -0400672 return NULL;
Chris Mason6324fbf2008-03-24 15:01:59 -0400673}
674
Chris Mason4184ea72009-03-10 12:39:20 -0400675/*
676 * after adding space to the filesystem, we need to clear the full flags
677 * on all the space infos.
678 */
679void btrfs_clear_space_info_full(struct btrfs_fs_info *info)
680{
681 struct list_head *head = &info->space_info;
682 struct btrfs_space_info *found;
683
684 rcu_read_lock();
685 list_for_each_entry_rcu(found, head, list)
686 found->full = 0;
687 rcu_read_unlock();
688}
689
Chris Masone02119d2008-09-05 16:13:11 -0400690/* simple helper to search for an existing extent at a given offset */
Zheng Yan31840ae2008-09-23 13:14:14 -0400691int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len)
Chris Masone02119d2008-09-05 16:13:11 -0400692{
693 int ret;
694 struct btrfs_key key;
Zheng Yan31840ae2008-09-23 13:14:14 -0400695 struct btrfs_path *path;
Chris Masone02119d2008-09-05 16:13:11 -0400696
Zheng Yan31840ae2008-09-23 13:14:14 -0400697 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -0700698 if (!path)
699 return -ENOMEM;
700
Chris Masone02119d2008-09-05 16:13:11 -0400701 key.objectid = start;
702 key.offset = len;
Josef Bacik3173a182013-03-07 14:22:04 -0500703 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masone02119d2008-09-05 16:13:11 -0400704 ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
705 0, 0);
Josef Bacik3173a182013-03-07 14:22:04 -0500706 if (ret > 0) {
707 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
708 if (key.objectid == start &&
709 key.type == BTRFS_METADATA_ITEM_KEY)
710 ret = 0;
711 }
Zheng Yan31840ae2008-09-23 13:14:14 -0400712 btrfs_free_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -0500713 return ret;
714}
715
Chris Masond8d5f3e2007-12-11 12:42:00 -0500716/*
Josef Bacik3173a182013-03-07 14:22:04 -0500717 * helper function to lookup reference count and flags of a tree block.
Yan, Zhenga22285a2010-05-16 10:48:46 -0400718 *
719 * the head node for delayed ref is used to store the sum of all the
720 * reference count modifications queued up in the rbtree. the head
721 * node may also store the extent flags to set. This way you can check
722 * to see what the reference count and extent flags would be if all of
723 * the delayed refs are not processed.
724 */
725int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,
726 struct btrfs_root *root, u64 bytenr,
Josef Bacik3173a182013-03-07 14:22:04 -0500727 u64 offset, int metadata, u64 *refs, u64 *flags)
Yan, Zhenga22285a2010-05-16 10:48:46 -0400728{
729 struct btrfs_delayed_ref_head *head;
730 struct btrfs_delayed_ref_root *delayed_refs;
731 struct btrfs_path *path;
732 struct btrfs_extent_item *ei;
733 struct extent_buffer *leaf;
734 struct btrfs_key key;
735 u32 item_size;
736 u64 num_refs;
737 u64 extent_flags;
738 int ret;
739
Josef Bacik3173a182013-03-07 14:22:04 -0500740 /*
741 * If we don't have skinny metadata, don't bother doing anything
742 * different
743 */
744 if (metadata && !btrfs_fs_incompat(root->fs_info, SKINNY_METADATA)) {
745 offset = root->leafsize;
746 metadata = 0;
747 }
748
Yan, Zhenga22285a2010-05-16 10:48:46 -0400749 path = btrfs_alloc_path();
750 if (!path)
751 return -ENOMEM;
752
Josef Bacik3173a182013-03-07 14:22:04 -0500753 if (metadata) {
754 key.objectid = bytenr;
755 key.type = BTRFS_METADATA_ITEM_KEY;
756 key.offset = offset;
757 } else {
758 key.objectid = bytenr;
759 key.type = BTRFS_EXTENT_ITEM_KEY;
760 key.offset = offset;
761 }
762
Yan, Zhenga22285a2010-05-16 10:48:46 -0400763 if (!trans) {
764 path->skip_locking = 1;
765 path->search_commit_root = 1;
766 }
767again:
768 ret = btrfs_search_slot(trans, root->fs_info->extent_root,
769 &key, path, 0, 0);
770 if (ret < 0)
771 goto out_free;
772
Josef Bacik3173a182013-03-07 14:22:04 -0500773 if (ret > 0 && metadata && key.type == BTRFS_METADATA_ITEM_KEY) {
774 key.type = BTRFS_EXTENT_ITEM_KEY;
775 key.offset = root->leafsize;
776 btrfs_release_path(path);
777 goto again;
778 }
779
Yan, Zhenga22285a2010-05-16 10:48:46 -0400780 if (ret == 0) {
781 leaf = path->nodes[0];
782 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
783 if (item_size >= sizeof(*ei)) {
784 ei = btrfs_item_ptr(leaf, path->slots[0],
785 struct btrfs_extent_item);
786 num_refs = btrfs_extent_refs(leaf, ei);
787 extent_flags = btrfs_extent_flags(leaf, ei);
788 } else {
789#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
790 struct btrfs_extent_item_v0 *ei0;
791 BUG_ON(item_size != sizeof(*ei0));
792 ei0 = btrfs_item_ptr(leaf, path->slots[0],
793 struct btrfs_extent_item_v0);
794 num_refs = btrfs_extent_refs_v0(leaf, ei0);
795 /* FIXME: this isn't correct for data */
796 extent_flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
797#else
798 BUG();
799#endif
800 }
801 BUG_ON(num_refs == 0);
802 } else {
803 num_refs = 0;
804 extent_flags = 0;
805 ret = 0;
806 }
807
808 if (!trans)
809 goto out;
810
811 delayed_refs = &trans->transaction->delayed_refs;
812 spin_lock(&delayed_refs->lock);
813 head = btrfs_find_delayed_ref_head(trans, bytenr);
814 if (head) {
815 if (!mutex_trylock(&head->mutex)) {
816 atomic_inc(&head->node.refs);
817 spin_unlock(&delayed_refs->lock);
818
David Sterbab3b4aa72011-04-21 01:20:15 +0200819 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400820
David Sterba8cc33e52011-05-02 15:29:25 +0200821 /*
822 * Mutex was contended, block until it's released and try
823 * again
824 */
Yan, Zhenga22285a2010-05-16 10:48:46 -0400825 mutex_lock(&head->mutex);
826 mutex_unlock(&head->mutex);
827 btrfs_put_delayed_ref(&head->node);
828 goto again;
829 }
830 if (head->extent_op && head->extent_op->update_flags)
831 extent_flags |= head->extent_op->flags_to_set;
832 else
833 BUG_ON(num_refs == 0);
834
835 num_refs += head->node.ref_mod;
836 mutex_unlock(&head->mutex);
837 }
838 spin_unlock(&delayed_refs->lock);
839out:
840 WARN_ON(num_refs == 0);
841 if (refs)
842 *refs = num_refs;
843 if (flags)
844 *flags = extent_flags;
845out_free:
846 btrfs_free_path(path);
847 return ret;
848}
849
850/*
Chris Masond8d5f3e2007-12-11 12:42:00 -0500851 * Back reference rules. Back refs have three main goals:
852 *
853 * 1) differentiate between all holders of references to an extent so that
854 * when a reference is dropped we can make sure it was a valid reference
855 * before freeing the extent.
856 *
857 * 2) Provide enough information to quickly find the holders of an extent
858 * if we notice a given block is corrupted or bad.
859 *
860 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
861 * maintenance. This is actually the same as #2, but with a slightly
862 * different use case.
863 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400864 * There are two kinds of back refs. The implicit back refs is optimized
865 * for pointers in non-shared tree blocks. For a given pointer in a block,
866 * back refs of this kind provide information about the block's owner tree
867 * and the pointer's key. These information allow us to find the block by
868 * b-tree searching. The full back refs is for pointers in tree blocks not
869 * referenced by their owner trees. The location of tree block is recorded
870 * in the back refs. Actually the full back refs is generic, and can be
871 * used in all cases the implicit back refs is used. The major shortcoming
872 * of the full back refs is its overhead. Every time a tree block gets
873 * COWed, we have to update back refs entry for all pointers in it.
874 *
875 * For a newly allocated tree block, we use implicit back refs for
876 * pointers in it. This means most tree related operations only involve
877 * implicit back refs. For a tree block created in old transaction, the
878 * only way to drop a reference to it is COW it. So we can detect the
879 * event that tree block loses its owner tree's reference and do the
880 * back refs conversion.
881 *
882 * When a tree block is COW'd through a tree, there are four cases:
883 *
884 * The reference count of the block is one and the tree is the block's
885 * owner tree. Nothing to do in this case.
886 *
887 * The reference count of the block is one and the tree is not the
888 * block's owner tree. In this case, full back refs is used for pointers
889 * in the block. Remove these full back refs, add implicit back refs for
890 * every pointers in the new block.
891 *
892 * The reference count of the block is greater than one and the tree is
893 * the block's owner tree. In this case, implicit back refs is used for
894 * pointers in the block. Add full back refs for every pointers in the
895 * block, increase lower level extents' reference counts. The original
896 * implicit back refs are entailed to the new block.
897 *
898 * The reference count of the block is greater than one and the tree is
899 * not the block's owner tree. Add implicit back refs for every pointer in
900 * the new block, increase lower level extents' reference count.
901 *
902 * Back Reference Key composing:
903 *
904 * The key objectid corresponds to the first byte in the extent,
905 * The key type is used to differentiate between types of back refs.
906 * There are different meanings of the key offset for different types
907 * of back refs.
908 *
Chris Masond8d5f3e2007-12-11 12:42:00 -0500909 * File extents can be referenced by:
910 *
911 * - multiple snapshots, subvolumes, or different generations in one subvol
Zheng Yan31840ae2008-09-23 13:14:14 -0400912 * - different files inside a single subvolume
Chris Masond8d5f3e2007-12-11 12:42:00 -0500913 * - different offsets inside a file (bookend extents in file.c)
914 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400915 * The extent ref structure for the implicit back refs has fields for:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500916 *
917 * - Objectid of the subvolume root
Chris Masond8d5f3e2007-12-11 12:42:00 -0500918 * - objectid of the file holding the reference
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400919 * - original offset in the file
920 * - how many bookend extents
Zheng Yan31840ae2008-09-23 13:14:14 -0400921 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400922 * The key offset for the implicit back refs is hash of the first
923 * three fields.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500924 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400925 * The extent ref structure for the full back refs has field for:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500926 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400927 * - number of pointers in the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -0500928 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400929 * The key offset for the implicit back refs is the first byte of
930 * the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -0500931 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400932 * When a file extent is allocated, The implicit back refs is used.
933 * the fields are filled in:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500934 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400935 * (root_key.objectid, inode objectid, offset in file, 1)
936 *
937 * When a file extent is removed file truncation, we find the
938 * corresponding implicit back refs and check the following fields:
939 *
940 * (btrfs_header_owner(leaf), inode objectid, offset in file)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500941 *
942 * Btree extents can be referenced by:
943 *
944 * - Different subvolumes
Chris Masond8d5f3e2007-12-11 12:42:00 -0500945 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400946 * Both the implicit back refs and the full back refs for tree blocks
947 * only consist of key. The key offset for the implicit back refs is
948 * objectid of block's owner tree. The key offset for the full back refs
949 * is the first byte of parent block.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500950 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400951 * When implicit back refs is used, information about the lowest key and
952 * level of the tree block are required. These information are stored in
953 * tree block info structure.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500954 */
Zheng Yan31840ae2008-09-23 13:14:14 -0400955
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400956#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
957static int convert_extent_item_v0(struct btrfs_trans_handle *trans,
958 struct btrfs_root *root,
959 struct btrfs_path *path,
960 u64 owner, u32 extra_size)
Chris Mason74493f72007-12-11 09:25:06 -0500961{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400962 struct btrfs_extent_item *item;
963 struct btrfs_extent_item_v0 *ei0;
964 struct btrfs_extent_ref_v0 *ref0;
965 struct btrfs_tree_block_info *bi;
Zheng Yan31840ae2008-09-23 13:14:14 -0400966 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400967 struct btrfs_key key;
968 struct btrfs_key found_key;
969 u32 new_size = sizeof(*item);
970 u64 refs;
Chris Mason74493f72007-12-11 09:25:06 -0500971 int ret;
972
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400973 leaf = path->nodes[0];
974 BUG_ON(btrfs_item_size_nr(leaf, path->slots[0]) != sizeof(*ei0));
Chris Mason74493f72007-12-11 09:25:06 -0500975
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400976 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
977 ei0 = btrfs_item_ptr(leaf, path->slots[0],
978 struct btrfs_extent_item_v0);
979 refs = btrfs_extent_refs_v0(leaf, ei0);
980
981 if (owner == (u64)-1) {
982 while (1) {
983 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
984 ret = btrfs_next_leaf(root, path);
985 if (ret < 0)
986 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100987 BUG_ON(ret > 0); /* Corruption */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400988 leaf = path->nodes[0];
989 }
990 btrfs_item_key_to_cpu(leaf, &found_key,
991 path->slots[0]);
992 BUG_ON(key.objectid != found_key.objectid);
993 if (found_key.type != BTRFS_EXTENT_REF_V0_KEY) {
994 path->slots[0]++;
995 continue;
996 }
997 ref0 = btrfs_item_ptr(leaf, path->slots[0],
998 struct btrfs_extent_ref_v0);
999 owner = btrfs_ref_objectid_v0(leaf, ref0);
1000 break;
1001 }
1002 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001003 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001004
1005 if (owner < BTRFS_FIRST_FREE_OBJECTID)
1006 new_size += sizeof(*bi);
1007
1008 new_size -= sizeof(*ei0);
1009 ret = btrfs_search_slot(trans, root, &key, path,
1010 new_size + extra_size, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04001011 if (ret < 0)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001012 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001013 BUG_ON(ret); /* Corruption */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001014
Tsutomu Itoh4b90c682013-04-16 05:18:49 +00001015 btrfs_extend_item(root, path, new_size);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001016
1017 leaf = path->nodes[0];
1018 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1019 btrfs_set_extent_refs(leaf, item, refs);
1020 /* FIXME: get real generation */
1021 btrfs_set_extent_generation(leaf, item, 0);
1022 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1023 btrfs_set_extent_flags(leaf, item,
1024 BTRFS_EXTENT_FLAG_TREE_BLOCK |
1025 BTRFS_BLOCK_FLAG_FULL_BACKREF);
1026 bi = (struct btrfs_tree_block_info *)(item + 1);
1027 /* FIXME: get first key of the block */
1028 memset_extent_buffer(leaf, 0, (unsigned long)bi, sizeof(*bi));
1029 btrfs_set_tree_block_level(leaf, bi, (int)owner);
1030 } else {
1031 btrfs_set_extent_flags(leaf, item, BTRFS_EXTENT_FLAG_DATA);
1032 }
1033 btrfs_mark_buffer_dirty(leaf);
1034 return 0;
1035}
1036#endif
1037
1038static u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset)
1039{
1040 u32 high_crc = ~(u32)0;
1041 u32 low_crc = ~(u32)0;
1042 __le64 lenum;
1043
1044 lenum = cpu_to_le64(root_objectid);
David Woodhouse163e7832009-04-19 13:02:41 +01001045 high_crc = crc32c(high_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001046 lenum = cpu_to_le64(owner);
David Woodhouse163e7832009-04-19 13:02:41 +01001047 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001048 lenum = cpu_to_le64(offset);
David Woodhouse163e7832009-04-19 13:02:41 +01001049 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001050
1051 return ((u64)high_crc << 31) ^ (u64)low_crc;
1052}
1053
1054static u64 hash_extent_data_ref_item(struct extent_buffer *leaf,
1055 struct btrfs_extent_data_ref *ref)
1056{
1057 return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref),
1058 btrfs_extent_data_ref_objectid(leaf, ref),
1059 btrfs_extent_data_ref_offset(leaf, ref));
1060}
1061
1062static int match_extent_data_ref(struct extent_buffer *leaf,
1063 struct btrfs_extent_data_ref *ref,
1064 u64 root_objectid, u64 owner, u64 offset)
1065{
1066 if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid ||
1067 btrfs_extent_data_ref_objectid(leaf, ref) != owner ||
1068 btrfs_extent_data_ref_offset(leaf, ref) != offset)
1069 return 0;
1070 return 1;
1071}
1072
1073static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans,
1074 struct btrfs_root *root,
1075 struct btrfs_path *path,
1076 u64 bytenr, u64 parent,
1077 u64 root_objectid,
1078 u64 owner, u64 offset)
1079{
1080 struct btrfs_key key;
1081 struct btrfs_extent_data_ref *ref;
1082 struct extent_buffer *leaf;
1083 u32 nritems;
1084 int ret;
1085 int recow;
1086 int err = -ENOENT;
1087
1088 key.objectid = bytenr;
1089 if (parent) {
1090 key.type = BTRFS_SHARED_DATA_REF_KEY;
1091 key.offset = parent;
1092 } else {
1093 key.type = BTRFS_EXTENT_DATA_REF_KEY;
1094 key.offset = hash_extent_data_ref(root_objectid,
1095 owner, offset);
1096 }
1097again:
1098 recow = 0;
1099 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1100 if (ret < 0) {
1101 err = ret;
1102 goto fail;
1103 }
1104
1105 if (parent) {
1106 if (!ret)
1107 return 0;
1108#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1109 key.type = BTRFS_EXTENT_REF_V0_KEY;
David Sterbab3b4aa72011-04-21 01:20:15 +02001110 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001111 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1112 if (ret < 0) {
1113 err = ret;
1114 goto fail;
1115 }
1116 if (!ret)
1117 return 0;
1118#endif
1119 goto fail;
Zheng Yan31840ae2008-09-23 13:14:14 -04001120 }
1121
1122 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001123 nritems = btrfs_header_nritems(leaf);
1124 while (1) {
1125 if (path->slots[0] >= nritems) {
1126 ret = btrfs_next_leaf(root, path);
1127 if (ret < 0)
1128 err = ret;
1129 if (ret)
1130 goto fail;
1131
1132 leaf = path->nodes[0];
1133 nritems = btrfs_header_nritems(leaf);
1134 recow = 1;
1135 }
1136
1137 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1138 if (key.objectid != bytenr ||
1139 key.type != BTRFS_EXTENT_DATA_REF_KEY)
1140 goto fail;
1141
1142 ref = btrfs_item_ptr(leaf, path->slots[0],
1143 struct btrfs_extent_data_ref);
1144
1145 if (match_extent_data_ref(leaf, ref, root_objectid,
1146 owner, offset)) {
1147 if (recow) {
David Sterbab3b4aa72011-04-21 01:20:15 +02001148 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001149 goto again;
1150 }
1151 err = 0;
1152 break;
1153 }
1154 path->slots[0]++;
Zheng Yan31840ae2008-09-23 13:14:14 -04001155 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001156fail:
1157 return err;
Zheng Yan31840ae2008-09-23 13:14:14 -04001158}
1159
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001160static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans,
1161 struct btrfs_root *root,
1162 struct btrfs_path *path,
1163 u64 bytenr, u64 parent,
1164 u64 root_objectid, u64 owner,
1165 u64 offset, int refs_to_add)
Zheng Yan31840ae2008-09-23 13:14:14 -04001166{
1167 struct btrfs_key key;
1168 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001169 u32 size;
Zheng Yan31840ae2008-09-23 13:14:14 -04001170 u32 num_refs;
1171 int ret;
1172
1173 key.objectid = bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001174 if (parent) {
1175 key.type = BTRFS_SHARED_DATA_REF_KEY;
1176 key.offset = parent;
1177 size = sizeof(struct btrfs_shared_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -04001178 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001179 key.type = BTRFS_EXTENT_DATA_REF_KEY;
1180 key.offset = hash_extent_data_ref(root_objectid,
1181 owner, offset);
1182 size = sizeof(struct btrfs_extent_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -04001183 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001184
1185 ret = btrfs_insert_empty_item(trans, root, path, &key, size);
1186 if (ret && ret != -EEXIST)
1187 goto fail;
1188
1189 leaf = path->nodes[0];
1190 if (parent) {
1191 struct btrfs_shared_data_ref *ref;
1192 ref = btrfs_item_ptr(leaf, path->slots[0],
1193 struct btrfs_shared_data_ref);
1194 if (ret == 0) {
1195 btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add);
1196 } else {
1197 num_refs = btrfs_shared_data_ref_count(leaf, ref);
1198 num_refs += refs_to_add;
1199 btrfs_set_shared_data_ref_count(leaf, ref, num_refs);
1200 }
1201 } else {
1202 struct btrfs_extent_data_ref *ref;
1203 while (ret == -EEXIST) {
1204 ref = btrfs_item_ptr(leaf, path->slots[0],
1205 struct btrfs_extent_data_ref);
1206 if (match_extent_data_ref(leaf, ref, root_objectid,
1207 owner, offset))
1208 break;
David Sterbab3b4aa72011-04-21 01:20:15 +02001209 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001210 key.offset++;
1211 ret = btrfs_insert_empty_item(trans, root, path, &key,
1212 size);
1213 if (ret && ret != -EEXIST)
1214 goto fail;
1215
1216 leaf = path->nodes[0];
1217 }
1218 ref = btrfs_item_ptr(leaf, path->slots[0],
1219 struct btrfs_extent_data_ref);
1220 if (ret == 0) {
1221 btrfs_set_extent_data_ref_root(leaf, ref,
1222 root_objectid);
1223 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
1224 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
1225 btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add);
1226 } else {
1227 num_refs = btrfs_extent_data_ref_count(leaf, ref);
1228 num_refs += refs_to_add;
1229 btrfs_set_extent_data_ref_count(leaf, ref, num_refs);
1230 }
1231 }
1232 btrfs_mark_buffer_dirty(leaf);
1233 ret = 0;
1234fail:
David Sterbab3b4aa72011-04-21 01:20:15 +02001235 btrfs_release_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -05001236 return ret;
Chris Mason74493f72007-12-11 09:25:06 -05001237}
1238
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001239static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans,
1240 struct btrfs_root *root,
1241 struct btrfs_path *path,
1242 int refs_to_drop)
Zheng Yan31840ae2008-09-23 13:14:14 -04001243{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001244 struct btrfs_key key;
1245 struct btrfs_extent_data_ref *ref1 = NULL;
1246 struct btrfs_shared_data_ref *ref2 = NULL;
Zheng Yan31840ae2008-09-23 13:14:14 -04001247 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001248 u32 num_refs = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04001249 int ret = 0;
1250
1251 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001252 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1253
1254 if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1255 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1256 struct btrfs_extent_data_ref);
1257 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1258 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1259 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1260 struct btrfs_shared_data_ref);
1261 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1262#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1263 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1264 struct btrfs_extent_ref_v0 *ref0;
1265 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1266 struct btrfs_extent_ref_v0);
1267 num_refs = btrfs_ref_count_v0(leaf, ref0);
1268#endif
1269 } else {
1270 BUG();
1271 }
1272
Chris Mason56bec292009-03-13 10:10:06 -04001273 BUG_ON(num_refs < refs_to_drop);
1274 num_refs -= refs_to_drop;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001275
Zheng Yan31840ae2008-09-23 13:14:14 -04001276 if (num_refs == 0) {
1277 ret = btrfs_del_item(trans, root, path);
1278 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001279 if (key.type == BTRFS_EXTENT_DATA_REF_KEY)
1280 btrfs_set_extent_data_ref_count(leaf, ref1, num_refs);
1281 else if (key.type == BTRFS_SHARED_DATA_REF_KEY)
1282 btrfs_set_shared_data_ref_count(leaf, ref2, num_refs);
1283#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1284 else {
1285 struct btrfs_extent_ref_v0 *ref0;
1286 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1287 struct btrfs_extent_ref_v0);
1288 btrfs_set_ref_count_v0(leaf, ref0, num_refs);
1289 }
1290#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04001291 btrfs_mark_buffer_dirty(leaf);
1292 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001293 return ret;
1294}
1295
1296static noinline u32 extent_data_ref_count(struct btrfs_root *root,
1297 struct btrfs_path *path,
1298 struct btrfs_extent_inline_ref *iref)
1299{
1300 struct btrfs_key key;
1301 struct extent_buffer *leaf;
1302 struct btrfs_extent_data_ref *ref1;
1303 struct btrfs_shared_data_ref *ref2;
1304 u32 num_refs = 0;
1305
1306 leaf = path->nodes[0];
1307 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1308 if (iref) {
1309 if (btrfs_extent_inline_ref_type(leaf, iref) ==
1310 BTRFS_EXTENT_DATA_REF_KEY) {
1311 ref1 = (struct btrfs_extent_data_ref *)(&iref->offset);
1312 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1313 } else {
1314 ref2 = (struct btrfs_shared_data_ref *)(iref + 1);
1315 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1316 }
1317 } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1318 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1319 struct btrfs_extent_data_ref);
1320 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1321 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1322 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1323 struct btrfs_shared_data_ref);
1324 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1325#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1326 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1327 struct btrfs_extent_ref_v0 *ref0;
1328 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1329 struct btrfs_extent_ref_v0);
1330 num_refs = btrfs_ref_count_v0(leaf, ref0);
1331#endif
1332 } else {
1333 WARN_ON(1);
1334 }
1335 return num_refs;
1336}
1337
1338static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans,
1339 struct btrfs_root *root,
1340 struct btrfs_path *path,
1341 u64 bytenr, u64 parent,
1342 u64 root_objectid)
1343{
1344 struct btrfs_key key;
1345 int ret;
1346
1347 key.objectid = bytenr;
1348 if (parent) {
1349 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1350 key.offset = parent;
1351 } else {
1352 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1353 key.offset = root_objectid;
1354 }
1355
1356 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1357 if (ret > 0)
1358 ret = -ENOENT;
1359#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1360 if (ret == -ENOENT && parent) {
David Sterbab3b4aa72011-04-21 01:20:15 +02001361 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001362 key.type = BTRFS_EXTENT_REF_V0_KEY;
1363 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1364 if (ret > 0)
1365 ret = -ENOENT;
1366 }
1367#endif
1368 return ret;
1369}
1370
1371static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans,
1372 struct btrfs_root *root,
1373 struct btrfs_path *path,
1374 u64 bytenr, u64 parent,
1375 u64 root_objectid)
1376{
1377 struct btrfs_key key;
1378 int ret;
1379
1380 key.objectid = bytenr;
1381 if (parent) {
1382 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1383 key.offset = parent;
1384 } else {
1385 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1386 key.offset = root_objectid;
1387 }
1388
1389 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
David Sterbab3b4aa72011-04-21 01:20:15 +02001390 btrfs_release_path(path);
Zheng Yan31840ae2008-09-23 13:14:14 -04001391 return ret;
1392}
1393
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001394static inline int extent_ref_type(u64 parent, u64 owner)
1395{
1396 int type;
1397 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1398 if (parent > 0)
1399 type = BTRFS_SHARED_BLOCK_REF_KEY;
1400 else
1401 type = BTRFS_TREE_BLOCK_REF_KEY;
1402 } else {
1403 if (parent > 0)
1404 type = BTRFS_SHARED_DATA_REF_KEY;
1405 else
1406 type = BTRFS_EXTENT_DATA_REF_KEY;
1407 }
1408 return type;
1409}
1410
Yan Zheng2c47e6052009-06-27 21:07:35 -04001411static int find_next_key(struct btrfs_path *path, int level,
1412 struct btrfs_key *key)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001413
1414{
Yan Zheng2c47e6052009-06-27 21:07:35 -04001415 for (; level < BTRFS_MAX_LEVEL; level++) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001416 if (!path->nodes[level])
1417 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001418 if (path->slots[level] + 1 >=
1419 btrfs_header_nritems(path->nodes[level]))
1420 continue;
1421 if (level == 0)
1422 btrfs_item_key_to_cpu(path->nodes[level], key,
1423 path->slots[level] + 1);
1424 else
1425 btrfs_node_key_to_cpu(path->nodes[level], key,
1426 path->slots[level] + 1);
1427 return 0;
1428 }
1429 return 1;
1430}
1431
1432/*
1433 * look for inline back ref. if back ref is found, *ref_ret is set
1434 * to the address of inline back ref, and 0 is returned.
1435 *
1436 * if back ref isn't found, *ref_ret is set to the address where it
1437 * should be inserted, and -ENOENT is returned.
1438 *
1439 * if insert is true and there are too many inline back refs, the path
1440 * points to the extent item, and -EAGAIN is returned.
1441 *
1442 * NOTE: inline back refs are ordered in the same way that back ref
1443 * items in the tree are ordered.
1444 */
1445static noinline_for_stack
1446int lookup_inline_extent_backref(struct btrfs_trans_handle *trans,
1447 struct btrfs_root *root,
1448 struct btrfs_path *path,
1449 struct btrfs_extent_inline_ref **ref_ret,
1450 u64 bytenr, u64 num_bytes,
1451 u64 parent, u64 root_objectid,
1452 u64 owner, u64 offset, int insert)
1453{
1454 struct btrfs_key key;
1455 struct extent_buffer *leaf;
1456 struct btrfs_extent_item *ei;
1457 struct btrfs_extent_inline_ref *iref;
1458 u64 flags;
1459 u64 item_size;
1460 unsigned long ptr;
1461 unsigned long end;
1462 int extra_size;
1463 int type;
1464 int want;
1465 int ret;
1466 int err = 0;
Josef Bacik3173a182013-03-07 14:22:04 -05001467 bool skinny_metadata = btrfs_fs_incompat(root->fs_info,
1468 SKINNY_METADATA);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001469
1470 key.objectid = bytenr;
1471 key.type = BTRFS_EXTENT_ITEM_KEY;
1472 key.offset = num_bytes;
1473
1474 want = extent_ref_type(parent, owner);
1475 if (insert) {
1476 extra_size = btrfs_extent_inline_ref_size(want);
Yan Zheng85d41982009-06-11 08:51:10 -04001477 path->keep_locks = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001478 } else
1479 extra_size = -1;
Josef Bacik3173a182013-03-07 14:22:04 -05001480
1481 /*
1482 * Owner is our parent level, so we can just add one to get the level
1483 * for the block we are interested in.
1484 */
1485 if (skinny_metadata && owner < BTRFS_FIRST_FREE_OBJECTID) {
1486 key.type = BTRFS_METADATA_ITEM_KEY;
1487 key.offset = owner;
1488 }
1489
1490again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001491 ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1);
1492 if (ret < 0) {
1493 err = ret;
1494 goto out;
1495 }
Josef Bacik3173a182013-03-07 14:22:04 -05001496
1497 /*
1498 * We may be a newly converted file system which still has the old fat
1499 * extent entries for metadata, so try and see if we have one of those.
1500 */
1501 if (ret > 0 && skinny_metadata) {
1502 skinny_metadata = false;
1503 if (path->slots[0]) {
1504 path->slots[0]--;
1505 btrfs_item_key_to_cpu(path->nodes[0], &key,
1506 path->slots[0]);
1507 if (key.objectid == bytenr &&
1508 key.type == BTRFS_EXTENT_ITEM_KEY &&
1509 key.offset == num_bytes)
1510 ret = 0;
1511 }
1512 if (ret) {
1513 key.type = BTRFS_EXTENT_ITEM_KEY;
1514 key.offset = num_bytes;
1515 btrfs_release_path(path);
1516 goto again;
1517 }
1518 }
1519
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001520 if (ret && !insert) {
1521 err = -ENOENT;
1522 goto out;
Josef Bacik492104c2013-03-08 15:41:02 -05001523 } else if (ret) {
1524 err = -EIO;
1525 WARN_ON(1);
1526 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001527 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001528
1529 leaf = path->nodes[0];
1530 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1531#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1532 if (item_size < sizeof(*ei)) {
1533 if (!insert) {
1534 err = -ENOENT;
1535 goto out;
1536 }
1537 ret = convert_extent_item_v0(trans, root, path, owner,
1538 extra_size);
1539 if (ret < 0) {
1540 err = ret;
1541 goto out;
1542 }
1543 leaf = path->nodes[0];
1544 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1545 }
1546#endif
1547 BUG_ON(item_size < sizeof(*ei));
1548
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001549 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1550 flags = btrfs_extent_flags(leaf, ei);
1551
1552 ptr = (unsigned long)(ei + 1);
1553 end = (unsigned long)ei + item_size;
1554
Josef Bacik3173a182013-03-07 14:22:04 -05001555 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK && !skinny_metadata) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001556 ptr += sizeof(struct btrfs_tree_block_info);
1557 BUG_ON(ptr > end);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001558 }
1559
1560 err = -ENOENT;
1561 while (1) {
1562 if (ptr >= end) {
1563 WARN_ON(ptr > end);
1564 break;
1565 }
1566 iref = (struct btrfs_extent_inline_ref *)ptr;
1567 type = btrfs_extent_inline_ref_type(leaf, iref);
1568 if (want < type)
1569 break;
1570 if (want > type) {
1571 ptr += btrfs_extent_inline_ref_size(type);
1572 continue;
1573 }
1574
1575 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1576 struct btrfs_extent_data_ref *dref;
1577 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1578 if (match_extent_data_ref(leaf, dref, root_objectid,
1579 owner, offset)) {
1580 err = 0;
1581 break;
1582 }
1583 if (hash_extent_data_ref_item(leaf, dref) <
1584 hash_extent_data_ref(root_objectid, owner, offset))
1585 break;
1586 } else {
1587 u64 ref_offset;
1588 ref_offset = btrfs_extent_inline_ref_offset(leaf, iref);
1589 if (parent > 0) {
1590 if (parent == ref_offset) {
1591 err = 0;
1592 break;
1593 }
1594 if (ref_offset < parent)
1595 break;
1596 } else {
1597 if (root_objectid == ref_offset) {
1598 err = 0;
1599 break;
1600 }
1601 if (ref_offset < root_objectid)
1602 break;
1603 }
1604 }
1605 ptr += btrfs_extent_inline_ref_size(type);
1606 }
1607 if (err == -ENOENT && insert) {
1608 if (item_size + extra_size >=
1609 BTRFS_MAX_EXTENT_ITEM_SIZE(root)) {
1610 err = -EAGAIN;
1611 goto out;
1612 }
1613 /*
1614 * To add new inline back ref, we have to make sure
1615 * there is no corresponding back ref item.
1616 * For simplicity, we just do not add new inline back
1617 * ref if there is any kind of item for this block
1618 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04001619 if (find_next_key(path, 0, &key) == 0 &&
1620 key.objectid == bytenr &&
Yan Zheng85d41982009-06-11 08:51:10 -04001621 key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001622 err = -EAGAIN;
1623 goto out;
1624 }
1625 }
1626 *ref_ret = (struct btrfs_extent_inline_ref *)ptr;
1627out:
Yan Zheng85d41982009-06-11 08:51:10 -04001628 if (insert) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001629 path->keep_locks = 0;
1630 btrfs_unlock_up_safe(path, 1);
1631 }
1632 return err;
1633}
1634
1635/*
1636 * helper to add new inline back ref
1637 */
1638static noinline_for_stack
Tsutomu Itohfd279fa2013-04-16 05:19:11 +00001639void setup_inline_extent_backref(struct btrfs_root *root,
Jeff Mahoney143bede2012-03-01 14:56:26 +01001640 struct btrfs_path *path,
1641 struct btrfs_extent_inline_ref *iref,
1642 u64 parent, u64 root_objectid,
1643 u64 owner, u64 offset, int refs_to_add,
1644 struct btrfs_delayed_extent_op *extent_op)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001645{
1646 struct extent_buffer *leaf;
1647 struct btrfs_extent_item *ei;
1648 unsigned long ptr;
1649 unsigned long end;
1650 unsigned long item_offset;
1651 u64 refs;
1652 int size;
1653 int type;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001654
1655 leaf = path->nodes[0];
1656 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1657 item_offset = (unsigned long)iref - (unsigned long)ei;
1658
1659 type = extent_ref_type(parent, owner);
1660 size = btrfs_extent_inline_ref_size(type);
1661
Tsutomu Itoh4b90c682013-04-16 05:18:49 +00001662 btrfs_extend_item(root, path, size);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001663
1664 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1665 refs = btrfs_extent_refs(leaf, ei);
1666 refs += refs_to_add;
1667 btrfs_set_extent_refs(leaf, ei, refs);
1668 if (extent_op)
1669 __run_delayed_extent_op(extent_op, leaf, ei);
1670
1671 ptr = (unsigned long)ei + item_offset;
1672 end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]);
1673 if (ptr < end - size)
1674 memmove_extent_buffer(leaf, ptr + size, ptr,
1675 end - size - ptr);
1676
1677 iref = (struct btrfs_extent_inline_ref *)ptr;
1678 btrfs_set_extent_inline_ref_type(leaf, iref, type);
1679 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1680 struct btrfs_extent_data_ref *dref;
1681 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1682 btrfs_set_extent_data_ref_root(leaf, dref, root_objectid);
1683 btrfs_set_extent_data_ref_objectid(leaf, dref, owner);
1684 btrfs_set_extent_data_ref_offset(leaf, dref, offset);
1685 btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add);
1686 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1687 struct btrfs_shared_data_ref *sref;
1688 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1689 btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add);
1690 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1691 } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
1692 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1693 } else {
1694 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
1695 }
1696 btrfs_mark_buffer_dirty(leaf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001697}
1698
1699static int lookup_extent_backref(struct btrfs_trans_handle *trans,
1700 struct btrfs_root *root,
1701 struct btrfs_path *path,
1702 struct btrfs_extent_inline_ref **ref_ret,
1703 u64 bytenr, u64 num_bytes, u64 parent,
1704 u64 root_objectid, u64 owner, u64 offset)
1705{
1706 int ret;
1707
1708 ret = lookup_inline_extent_backref(trans, root, path, ref_ret,
1709 bytenr, num_bytes, parent,
1710 root_objectid, owner, offset, 0);
1711 if (ret != -ENOENT)
1712 return ret;
1713
David Sterbab3b4aa72011-04-21 01:20:15 +02001714 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001715 *ref_ret = NULL;
1716
1717 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1718 ret = lookup_tree_block_ref(trans, root, path, bytenr, parent,
1719 root_objectid);
1720 } else {
1721 ret = lookup_extent_data_ref(trans, root, path, bytenr, parent,
1722 root_objectid, owner, offset);
1723 }
1724 return ret;
1725}
1726
1727/*
1728 * helper to update/remove inline back ref
1729 */
1730static noinline_for_stack
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00001731void update_inline_extent_backref(struct btrfs_root *root,
Jeff Mahoney143bede2012-03-01 14:56:26 +01001732 struct btrfs_path *path,
1733 struct btrfs_extent_inline_ref *iref,
1734 int refs_to_mod,
1735 struct btrfs_delayed_extent_op *extent_op)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001736{
1737 struct extent_buffer *leaf;
1738 struct btrfs_extent_item *ei;
1739 struct btrfs_extent_data_ref *dref = NULL;
1740 struct btrfs_shared_data_ref *sref = NULL;
1741 unsigned long ptr;
1742 unsigned long end;
1743 u32 item_size;
1744 int size;
1745 int type;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001746 u64 refs;
1747
1748 leaf = path->nodes[0];
1749 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1750 refs = btrfs_extent_refs(leaf, ei);
1751 WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0);
1752 refs += refs_to_mod;
1753 btrfs_set_extent_refs(leaf, ei, refs);
1754 if (extent_op)
1755 __run_delayed_extent_op(extent_op, leaf, ei);
1756
1757 type = btrfs_extent_inline_ref_type(leaf, iref);
1758
1759 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1760 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1761 refs = btrfs_extent_data_ref_count(leaf, dref);
1762 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1763 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1764 refs = btrfs_shared_data_ref_count(leaf, sref);
1765 } else {
1766 refs = 1;
1767 BUG_ON(refs_to_mod != -1);
1768 }
1769
1770 BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod);
1771 refs += refs_to_mod;
1772
1773 if (refs > 0) {
1774 if (type == BTRFS_EXTENT_DATA_REF_KEY)
1775 btrfs_set_extent_data_ref_count(leaf, dref, refs);
1776 else
1777 btrfs_set_shared_data_ref_count(leaf, sref, refs);
1778 } else {
1779 size = btrfs_extent_inline_ref_size(type);
1780 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1781 ptr = (unsigned long)iref;
1782 end = (unsigned long)ei + item_size;
1783 if (ptr + size < end)
1784 memmove_extent_buffer(leaf, ptr, ptr + size,
1785 end - ptr - size);
1786 item_size -= size;
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00001787 btrfs_truncate_item(root, path, item_size, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001788 }
1789 btrfs_mark_buffer_dirty(leaf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001790}
1791
1792static noinline_for_stack
1793int insert_inline_extent_backref(struct btrfs_trans_handle *trans,
1794 struct btrfs_root *root,
1795 struct btrfs_path *path,
1796 u64 bytenr, u64 num_bytes, u64 parent,
1797 u64 root_objectid, u64 owner,
1798 u64 offset, int refs_to_add,
1799 struct btrfs_delayed_extent_op *extent_op)
1800{
1801 struct btrfs_extent_inline_ref *iref;
1802 int ret;
1803
1804 ret = lookup_inline_extent_backref(trans, root, path, &iref,
1805 bytenr, num_bytes, parent,
1806 root_objectid, owner, offset, 1);
1807 if (ret == 0) {
1808 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00001809 update_inline_extent_backref(root, path, iref,
Jeff Mahoney143bede2012-03-01 14:56:26 +01001810 refs_to_add, extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001811 } else if (ret == -ENOENT) {
Tsutomu Itohfd279fa2013-04-16 05:19:11 +00001812 setup_inline_extent_backref(root, path, iref, parent,
Jeff Mahoney143bede2012-03-01 14:56:26 +01001813 root_objectid, owner, offset,
1814 refs_to_add, extent_op);
1815 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001816 }
1817 return ret;
1818}
1819
1820static int insert_extent_backref(struct btrfs_trans_handle *trans,
1821 struct btrfs_root *root,
1822 struct btrfs_path *path,
1823 u64 bytenr, u64 parent, u64 root_objectid,
1824 u64 owner, u64 offset, int refs_to_add)
1825{
1826 int ret;
1827 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1828 BUG_ON(refs_to_add != 1);
1829 ret = insert_tree_block_ref(trans, root, path, bytenr,
1830 parent, root_objectid);
1831 } else {
1832 ret = insert_extent_data_ref(trans, root, path, bytenr,
1833 parent, root_objectid,
1834 owner, offset, refs_to_add);
1835 }
1836 return ret;
1837}
1838
1839static int remove_extent_backref(struct btrfs_trans_handle *trans,
1840 struct btrfs_root *root,
1841 struct btrfs_path *path,
1842 struct btrfs_extent_inline_ref *iref,
1843 int refs_to_drop, int is_data)
1844{
Jeff Mahoney143bede2012-03-01 14:56:26 +01001845 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001846
1847 BUG_ON(!is_data && refs_to_drop != 1);
1848 if (iref) {
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00001849 update_inline_extent_backref(root, path, iref,
Jeff Mahoney143bede2012-03-01 14:56:26 +01001850 -refs_to_drop, NULL);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001851 } else if (is_data) {
1852 ret = remove_extent_data_ref(trans, root, path, refs_to_drop);
1853 } else {
1854 ret = btrfs_del_item(trans, root, path);
1855 }
1856 return ret;
1857}
1858
Li Dongyang5378e602011-03-24 10:24:27 +00001859static int btrfs_issue_discard(struct block_device *bdev,
Chris Mason15916de2008-11-19 21:17:22 -05001860 u64 start, u64 len)
1861{
Li Dongyang5378e602011-03-24 10:24:27 +00001862 return blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_NOFS, 0);
Chris Mason15916de2008-11-19 21:17:22 -05001863}
Chris Mason15916de2008-11-19 21:17:22 -05001864
Liu Hui1f3c79a2009-01-05 15:57:51 -05001865static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr,
Li Dongyang5378e602011-03-24 10:24:27 +00001866 u64 num_bytes, u64 *actual_bytes)
Liu Hui1f3c79a2009-01-05 15:57:51 -05001867{
Liu Hui1f3c79a2009-01-05 15:57:51 -05001868 int ret;
Li Dongyang5378e602011-03-24 10:24:27 +00001869 u64 discarded_bytes = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02001870 struct btrfs_bio *bbio = NULL;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001871
Christoph Hellwige244a0a2009-10-14 09:24:59 -04001872
Liu Hui1f3c79a2009-01-05 15:57:51 -05001873 /* Tell the block device(s) that the sectors can be discarded */
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001874 ret = btrfs_map_block(root->fs_info, REQ_DISCARD,
Jan Schmidta1d3c472011-08-04 17:15:33 +02001875 bytenr, &num_bytes, &bbio, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001876 /* Error condition is -ENOMEM */
Liu Hui1f3c79a2009-01-05 15:57:51 -05001877 if (!ret) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02001878 struct btrfs_bio_stripe *stripe = bbio->stripes;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001879 int i;
1880
Liu Hui1f3c79a2009-01-05 15:57:51 -05001881
Jan Schmidta1d3c472011-08-04 17:15:33 +02001882 for (i = 0; i < bbio->num_stripes; i++, stripe++) {
Josef Bacikd5e20032011-08-04 14:52:27 +00001883 if (!stripe->dev->can_discard)
1884 continue;
1885
Li Dongyang5378e602011-03-24 10:24:27 +00001886 ret = btrfs_issue_discard(stripe->dev->bdev,
1887 stripe->physical,
1888 stripe->length);
1889 if (!ret)
1890 discarded_bytes += stripe->length;
1891 else if (ret != -EOPNOTSUPP)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001892 break; /* Logic errors or -ENOMEM, or -EIO but I don't know how that could happen JDM */
Josef Bacikd5e20032011-08-04 14:52:27 +00001893
1894 /*
1895 * Just in case we get back EOPNOTSUPP for some reason,
1896 * just ignore the return value so we don't screw up
1897 * people calling discard_extent.
1898 */
1899 ret = 0;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001900 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02001901 kfree(bbio);
Liu Hui1f3c79a2009-01-05 15:57:51 -05001902 }
Li Dongyang5378e602011-03-24 10:24:27 +00001903
1904 if (actual_bytes)
1905 *actual_bytes = discarded_bytes;
1906
Liu Hui1f3c79a2009-01-05 15:57:51 -05001907
David Woodhouse53b381b2013-01-29 18:40:14 -05001908 if (ret == -EOPNOTSUPP)
1909 ret = 0;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001910 return ret;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001911}
1912
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001913/* Can return -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001914int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1915 struct btrfs_root *root,
1916 u64 bytenr, u64 num_bytes, u64 parent,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001917 u64 root_objectid, u64 owner, u64 offset, int for_cow)
Zheng Yan31840ae2008-09-23 13:14:14 -04001918{
1919 int ret;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001920 struct btrfs_fs_info *fs_info = root->fs_info;
1921
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001922 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID &&
1923 root_objectid == BTRFS_TREE_LOG_OBJECTID);
Zheng Yan31840ae2008-09-23 13:14:14 -04001924
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001925 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001926 ret = btrfs_add_delayed_tree_ref(fs_info, trans, bytenr,
1927 num_bytes,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001928 parent, root_objectid, (int)owner,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001929 BTRFS_ADD_DELAYED_REF, NULL, for_cow);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001930 } else {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001931 ret = btrfs_add_delayed_data_ref(fs_info, trans, bytenr,
1932 num_bytes,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001933 parent, root_objectid, owner, offset,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001934 BTRFS_ADD_DELAYED_REF, NULL, for_cow);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001935 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001936 return ret;
1937}
1938
Chris Mason925baed2008-06-25 16:01:30 -04001939static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001940 struct btrfs_root *root,
1941 u64 bytenr, u64 num_bytes,
1942 u64 parent, u64 root_objectid,
1943 u64 owner, u64 offset, int refs_to_add,
1944 struct btrfs_delayed_extent_op *extent_op)
Chris Mason56bec292009-03-13 10:10:06 -04001945{
Chris Mason5caf2a02007-04-02 11:20:42 -04001946 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001947 struct extent_buffer *leaf;
Chris Mason234b63a2007-03-13 10:46:10 -04001948 struct btrfs_extent_item *item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001949 u64 refs;
1950 int ret;
1951 int err = 0;
Chris Mason037e6392007-03-07 11:50:24 -05001952
Chris Mason5caf2a02007-04-02 11:20:42 -04001953 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04001954 if (!path)
1955 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -04001956
Chris Mason3c12ac72008-04-21 12:01:38 -04001957 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04001958 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001959 /* this will setup the path even if it fails to insert the back ref */
1960 ret = insert_inline_extent_backref(trans, root->fs_info->extent_root,
1961 path, bytenr, num_bytes, parent,
1962 root_objectid, owner, offset,
1963 refs_to_add, extent_op);
1964 if (ret == 0)
1965 goto out;
Zheng Yan31840ae2008-09-23 13:14:14 -04001966
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001967 if (ret != -EAGAIN) {
1968 err = ret;
1969 goto out;
Chris Masonb9473432009-03-13 11:00:37 -04001970 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001971
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001972 leaf = path->nodes[0];
1973 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1974 refs = btrfs_extent_refs(leaf, item);
1975 btrfs_set_extent_refs(leaf, item, refs + refs_to_add);
1976 if (extent_op)
1977 __run_delayed_extent_op(extent_op, leaf, item);
Zheng Yan31840ae2008-09-23 13:14:14 -04001978
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001979 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02001980 btrfs_release_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -05001981
Chris Mason3c12ac72008-04-21 12:01:38 -04001982 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04001983 path->leave_spinning = 1;
1984
Chris Mason56bec292009-03-13 10:10:06 -04001985 /* now insert the actual backref */
Zheng Yan31840ae2008-09-23 13:14:14 -04001986 ret = insert_extent_backref(trans, root->fs_info->extent_root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001987 path, bytenr, parent, root_objectid,
1988 owner, offset, refs_to_add);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001989 if (ret)
1990 btrfs_abort_transaction(trans, root, ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001991out:
Chris Mason74493f72007-12-11 09:25:06 -05001992 btrfs_free_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001993 return err;
Chris Mason02217ed2007-03-02 16:08:05 -05001994}
1995
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001996static int run_delayed_data_ref(struct btrfs_trans_handle *trans,
1997 struct btrfs_root *root,
1998 struct btrfs_delayed_ref_node *node,
1999 struct btrfs_delayed_extent_op *extent_op,
2000 int insert_reserved)
Chris Masone9d0b132007-08-10 14:06:19 -04002001{
Chris Mason56bec292009-03-13 10:10:06 -04002002 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002003 struct btrfs_delayed_data_ref *ref;
2004 struct btrfs_key ins;
2005 u64 parent = 0;
2006 u64 ref_root = 0;
2007 u64 flags = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002008
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002009 ins.objectid = node->bytenr;
2010 ins.offset = node->num_bytes;
2011 ins.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason56bec292009-03-13 10:10:06 -04002012
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002013 ref = btrfs_delayed_node_to_data_ref(node);
2014 if (node->type == BTRFS_SHARED_DATA_REF_KEY)
2015 parent = ref->parent;
2016 else
2017 ref_root = ref->root;
2018
2019 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
Josef Bacik3173a182013-03-07 14:22:04 -05002020 if (extent_op)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002021 flags |= extent_op->flags_to_set;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002022 ret = alloc_reserved_file_extent(trans, root,
2023 parent, ref_root, flags,
2024 ref->objectid, ref->offset,
2025 &ins, node->ref_mod);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002026 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
2027 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
2028 node->num_bytes, parent,
2029 ref_root, ref->objectid,
2030 ref->offset, node->ref_mod,
2031 extent_op);
2032 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
2033 ret = __btrfs_free_extent(trans, root, node->bytenr,
2034 node->num_bytes, parent,
2035 ref_root, ref->objectid,
2036 ref->offset, node->ref_mod,
2037 extent_op);
2038 } else {
2039 BUG();
2040 }
Chris Mason56bec292009-03-13 10:10:06 -04002041 return ret;
2042}
2043
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002044static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
2045 struct extent_buffer *leaf,
2046 struct btrfs_extent_item *ei)
2047{
2048 u64 flags = btrfs_extent_flags(leaf, ei);
2049 if (extent_op->update_flags) {
2050 flags |= extent_op->flags_to_set;
2051 btrfs_set_extent_flags(leaf, ei, flags);
2052 }
2053
2054 if (extent_op->update_key) {
2055 struct btrfs_tree_block_info *bi;
2056 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK));
2057 bi = (struct btrfs_tree_block_info *)(ei + 1);
2058 btrfs_set_tree_block_key(leaf, bi, &extent_op->key);
2059 }
2060}
2061
2062static int run_delayed_extent_op(struct btrfs_trans_handle *trans,
2063 struct btrfs_root *root,
2064 struct btrfs_delayed_ref_node *node,
2065 struct btrfs_delayed_extent_op *extent_op)
2066{
2067 struct btrfs_key key;
2068 struct btrfs_path *path;
2069 struct btrfs_extent_item *ei;
2070 struct extent_buffer *leaf;
2071 u32 item_size;
2072 int ret;
2073 int err = 0;
Josef Bacikb1c79e02013-05-09 13:49:30 -04002074 int metadata = !extent_op->is_data;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002075
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002076 if (trans->aborted)
2077 return 0;
2078
Josef Bacik3173a182013-03-07 14:22:04 -05002079 if (metadata && !btrfs_fs_incompat(root->fs_info, SKINNY_METADATA))
2080 metadata = 0;
2081
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002082 path = btrfs_alloc_path();
2083 if (!path)
2084 return -ENOMEM;
2085
2086 key.objectid = node->bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002087
Josef Bacik3173a182013-03-07 14:22:04 -05002088 if (metadata) {
Josef Bacik3173a182013-03-07 14:22:04 -05002089 key.type = BTRFS_METADATA_ITEM_KEY;
Josef Bacikb1c79e02013-05-09 13:49:30 -04002090 key.offset = extent_op->level;
Josef Bacik3173a182013-03-07 14:22:04 -05002091 } else {
2092 key.type = BTRFS_EXTENT_ITEM_KEY;
2093 key.offset = node->num_bytes;
2094 }
2095
2096again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002097 path->reada = 1;
2098 path->leave_spinning = 1;
2099 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key,
2100 path, 0, 1);
2101 if (ret < 0) {
2102 err = ret;
2103 goto out;
2104 }
2105 if (ret > 0) {
Josef Bacik3173a182013-03-07 14:22:04 -05002106 if (metadata) {
2107 btrfs_release_path(path);
2108 metadata = 0;
2109
2110 key.offset = node->num_bytes;
2111 key.type = BTRFS_EXTENT_ITEM_KEY;
2112 goto again;
2113 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002114 err = -EIO;
2115 goto out;
2116 }
2117
2118 leaf = path->nodes[0];
2119 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2120#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2121 if (item_size < sizeof(*ei)) {
2122 ret = convert_extent_item_v0(trans, root->fs_info->extent_root,
2123 path, (u64)-1, 0);
2124 if (ret < 0) {
2125 err = ret;
2126 goto out;
2127 }
2128 leaf = path->nodes[0];
2129 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2130 }
2131#endif
2132 BUG_ON(item_size < sizeof(*ei));
2133 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2134 __run_delayed_extent_op(extent_op, leaf, ei);
2135
2136 btrfs_mark_buffer_dirty(leaf);
2137out:
2138 btrfs_free_path(path);
2139 return err;
2140}
2141
2142static int run_delayed_tree_ref(struct btrfs_trans_handle *trans,
2143 struct btrfs_root *root,
2144 struct btrfs_delayed_ref_node *node,
2145 struct btrfs_delayed_extent_op *extent_op,
2146 int insert_reserved)
2147{
2148 int ret = 0;
2149 struct btrfs_delayed_tree_ref *ref;
2150 struct btrfs_key ins;
2151 u64 parent = 0;
2152 u64 ref_root = 0;
Josef Bacik3173a182013-03-07 14:22:04 -05002153 bool skinny_metadata = btrfs_fs_incompat(root->fs_info,
2154 SKINNY_METADATA);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002155
2156 ref = btrfs_delayed_node_to_tree_ref(node);
2157 if (node->type == BTRFS_SHARED_BLOCK_REF_KEY)
2158 parent = ref->parent;
2159 else
2160 ref_root = ref->root;
2161
Josef Bacik3173a182013-03-07 14:22:04 -05002162 ins.objectid = node->bytenr;
2163 if (skinny_metadata) {
2164 ins.offset = ref->level;
2165 ins.type = BTRFS_METADATA_ITEM_KEY;
2166 } else {
2167 ins.offset = node->num_bytes;
2168 ins.type = BTRFS_EXTENT_ITEM_KEY;
2169 }
2170
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002171 BUG_ON(node->ref_mod != 1);
2172 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
Josef Bacik3173a182013-03-07 14:22:04 -05002173 BUG_ON(!extent_op || !extent_op->update_flags);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002174 ret = alloc_reserved_tree_block(trans, root,
2175 parent, ref_root,
2176 extent_op->flags_to_set,
2177 &extent_op->key,
2178 ref->level, &ins);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002179 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
2180 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
2181 node->num_bytes, parent, ref_root,
2182 ref->level, 0, 1, extent_op);
2183 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
2184 ret = __btrfs_free_extent(trans, root, node->bytenr,
2185 node->num_bytes, parent, ref_root,
2186 ref->level, 0, 1, extent_op);
2187 } else {
2188 BUG();
2189 }
2190 return ret;
2191}
2192
Chris Mason56bec292009-03-13 10:10:06 -04002193/* helper function to actually process a single delayed ref entry */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002194static int run_one_delayed_ref(struct btrfs_trans_handle *trans,
2195 struct btrfs_root *root,
2196 struct btrfs_delayed_ref_node *node,
2197 struct btrfs_delayed_extent_op *extent_op,
2198 int insert_reserved)
Chris Mason56bec292009-03-13 10:10:06 -04002199{
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002200 int ret = 0;
2201
2202 if (trans->aborted)
2203 return 0;
2204
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002205 if (btrfs_delayed_ref_is_head(node)) {
Chris Mason56bec292009-03-13 10:10:06 -04002206 struct btrfs_delayed_ref_head *head;
2207 /*
2208 * we've hit the end of the chain and we were supposed
2209 * to insert this extent into the tree. But, it got
2210 * deleted before we ever needed to insert it, so all
2211 * we have to do is clean up the accounting
2212 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002213 BUG_ON(extent_op);
2214 head = btrfs_delayed_node_to_head(node);
Chris Mason56bec292009-03-13 10:10:06 -04002215 if (insert_reserved) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04002216 btrfs_pin_extent(root, node->bytenr,
2217 node->num_bytes, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002218 if (head->is_data) {
2219 ret = btrfs_del_csums(trans, root,
2220 node->bytenr,
2221 node->num_bytes);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002222 }
Chris Mason56bec292009-03-13 10:10:06 -04002223 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002224 return ret;
Chris Mason56bec292009-03-13 10:10:06 -04002225 }
Josef Bacikeb099672009-02-12 09:27:38 -05002226
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002227 if (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
2228 node->type == BTRFS_SHARED_BLOCK_REF_KEY)
2229 ret = run_delayed_tree_ref(trans, root, node, extent_op,
2230 insert_reserved);
2231 else if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
2232 node->type == BTRFS_SHARED_DATA_REF_KEY)
2233 ret = run_delayed_data_ref(trans, root, node, extent_op,
2234 insert_reserved);
2235 else
2236 BUG();
2237 return ret;
Chris Masone9d0b132007-08-10 14:06:19 -04002238}
2239
Chris Mason56bec292009-03-13 10:10:06 -04002240static noinline struct btrfs_delayed_ref_node *
2241select_delayed_ref(struct btrfs_delayed_ref_head *head)
Chris Masona28ec192007-03-06 20:08:01 -05002242{
Chris Mason56bec292009-03-13 10:10:06 -04002243 struct rb_node *node;
2244 struct btrfs_delayed_ref_node *ref;
2245 int action = BTRFS_ADD_DELAYED_REF;
2246again:
2247 /*
2248 * select delayed ref of type BTRFS_ADD_DELAYED_REF first.
2249 * this prevents ref count from going down to zero when
2250 * there still are pending delayed ref.
2251 */
2252 node = rb_prev(&head->node.rb_node);
2253 while (1) {
2254 if (!node)
2255 break;
2256 ref = rb_entry(node, struct btrfs_delayed_ref_node,
2257 rb_node);
2258 if (ref->bytenr != head->node.bytenr)
2259 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002260 if (ref->action == action)
Chris Mason56bec292009-03-13 10:10:06 -04002261 return ref;
2262 node = rb_prev(node);
Chris Mason5f39d392007-10-15 16:14:19 -04002263 }
Chris Mason56bec292009-03-13 10:10:06 -04002264 if (action == BTRFS_ADD_DELAYED_REF) {
2265 action = BTRFS_DROP_DELAYED_REF;
2266 goto again;
2267 }
2268 return NULL;
2269}
2270
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002271/*
2272 * Returns 0 on success or if called with an already aborted transaction.
2273 * Returns -ENOMEM or -EIO on failure and will abort the transaction.
2274 */
Chris Masonc3e69d52009-03-13 10:17:05 -04002275static noinline int run_clustered_refs(struct btrfs_trans_handle *trans,
2276 struct btrfs_root *root,
2277 struct list_head *cluster)
Chris Mason56bec292009-03-13 10:10:06 -04002278{
Chris Mason56bec292009-03-13 10:10:06 -04002279 struct btrfs_delayed_ref_root *delayed_refs;
2280 struct btrfs_delayed_ref_node *ref;
2281 struct btrfs_delayed_ref_head *locked_ref = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002282 struct btrfs_delayed_extent_op *extent_op;
Jan Schmidt097b8a72012-06-21 11:08:04 +02002283 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason56bec292009-03-13 10:10:06 -04002284 int ret;
Chris Masonc3e69d52009-03-13 10:17:05 -04002285 int count = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002286 int must_insert_reserved = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002287
2288 delayed_refs = &trans->transaction->delayed_refs;
Chris Mason56bec292009-03-13 10:10:06 -04002289 while (1) {
2290 if (!locked_ref) {
Chris Masonc3e69d52009-03-13 10:17:05 -04002291 /* pick a new head ref from the cluster list */
2292 if (list_empty(cluster))
Chris Mason56bec292009-03-13 10:10:06 -04002293 break;
Chris Mason56bec292009-03-13 10:10:06 -04002294
Chris Masonc3e69d52009-03-13 10:17:05 -04002295 locked_ref = list_entry(cluster->next,
2296 struct btrfs_delayed_ref_head, cluster);
2297
2298 /* grab the lock that says we are going to process
2299 * all the refs for this head */
2300 ret = btrfs_delayed_ref_lock(trans, locked_ref);
2301
2302 /*
2303 * we may have dropped the spin lock to get the head
2304 * mutex lock, and that might have given someone else
2305 * time to free the head. If that's true, it has been
2306 * removed from our list and we can move on.
2307 */
2308 if (ret == -EAGAIN) {
2309 locked_ref = NULL;
2310 count++;
2311 continue;
Chris Mason56bec292009-03-13 10:10:06 -04002312 }
2313 }
2314
2315 /*
Josef Bacikae1e2062012-08-07 16:00:32 -04002316 * We need to try and merge add/drops of the same ref since we
2317 * can run into issues with relocate dropping the implicit ref
2318 * and then it being added back again before the drop can
2319 * finish. If we merged anything we need to re-loop so we can
2320 * get a good ref.
2321 */
2322 btrfs_merge_delayed_refs(trans, fs_info, delayed_refs,
2323 locked_ref);
2324
2325 /*
Arne Jansend1270cd2011-09-13 15:16:43 +02002326 * locked_ref is the head node, so we have to go one
2327 * node back for any delayed ref updates
2328 */
2329 ref = select_delayed_ref(locked_ref);
2330
2331 if (ref && ref->seq &&
Jan Schmidt097b8a72012-06-21 11:08:04 +02002332 btrfs_check_delayed_seq(fs_info, delayed_refs, ref->seq)) {
Arne Jansend1270cd2011-09-13 15:16:43 +02002333 /*
2334 * there are still refs with lower seq numbers in the
2335 * process of being added. Don't run this ref yet.
2336 */
2337 list_del_init(&locked_ref->cluster);
Miao Xie093486c2012-12-19 08:10:10 +00002338 btrfs_delayed_ref_unlock(locked_ref);
Arne Jansend1270cd2011-09-13 15:16:43 +02002339 locked_ref = NULL;
2340 delayed_refs->num_heads_ready++;
2341 spin_unlock(&delayed_refs->lock);
2342 cond_resched();
2343 spin_lock(&delayed_refs->lock);
2344 continue;
2345 }
2346
2347 /*
Chris Mason56bec292009-03-13 10:10:06 -04002348 * record the must insert reserved flag before we
2349 * drop the spin lock.
2350 */
2351 must_insert_reserved = locked_ref->must_insert_reserved;
2352 locked_ref->must_insert_reserved = 0;
2353
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002354 extent_op = locked_ref->extent_op;
2355 locked_ref->extent_op = NULL;
2356
Chris Mason56bec292009-03-13 10:10:06 -04002357 if (!ref) {
2358 /* All delayed refs have been processed, Go ahead
2359 * and send the head node to run_one_delayed_ref,
2360 * so that any accounting fixes can happen
2361 */
2362 ref = &locked_ref->node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002363
2364 if (extent_op && must_insert_reserved) {
Miao Xie78a61842012-11-21 02:21:28 +00002365 btrfs_free_delayed_extent_op(extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002366 extent_op = NULL;
2367 }
2368
2369 if (extent_op) {
2370 spin_unlock(&delayed_refs->lock);
2371
2372 ret = run_delayed_extent_op(trans, root,
2373 ref, extent_op);
Miao Xie78a61842012-11-21 02:21:28 +00002374 btrfs_free_delayed_extent_op(extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002375
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002376 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002377 btrfs_debug(fs_info, "run_delayed_extent_op returned %d", ret);
Dan Carpenter253beeb2012-04-18 09:59:03 +03002378 spin_lock(&delayed_refs->lock);
Miao Xie093486c2012-12-19 08:10:10 +00002379 btrfs_delayed_ref_unlock(locked_ref);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002380 return ret;
2381 }
2382
Chris Mason203bf282012-01-06 15:23:57 -05002383 goto next;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002384 }
Chris Mason56bec292009-03-13 10:10:06 -04002385 }
2386
2387 ref->in_tree = 0;
2388 rb_erase(&ref->rb_node, &delayed_refs->root);
2389 delayed_refs->num_entries--;
Miao Xie093486c2012-12-19 08:10:10 +00002390 if (!btrfs_delayed_ref_is_head(ref)) {
Arne Jansen22cd2e72012-08-09 00:16:53 -06002391 /*
2392 * when we play the delayed ref, also correct the
2393 * ref_mod on head
2394 */
2395 switch (ref->action) {
2396 case BTRFS_ADD_DELAYED_REF:
2397 case BTRFS_ADD_DELAYED_EXTENT:
2398 locked_ref->node.ref_mod -= ref->ref_mod;
2399 break;
2400 case BTRFS_DROP_DELAYED_REF:
2401 locked_ref->node.ref_mod += ref->ref_mod;
2402 break;
2403 default:
2404 WARN_ON(1);
2405 }
2406 }
Chris Mason56bec292009-03-13 10:10:06 -04002407 spin_unlock(&delayed_refs->lock);
2408
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002409 ret = run_one_delayed_ref(trans, root, ref, extent_op,
Chris Mason56bec292009-03-13 10:10:06 -04002410 must_insert_reserved);
Chris Mason56bec292009-03-13 10:10:06 -04002411
Miao Xie78a61842012-11-21 02:21:28 +00002412 btrfs_free_delayed_extent_op(extent_op);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002413 if (ret) {
Miao Xie093486c2012-12-19 08:10:10 +00002414 btrfs_delayed_ref_unlock(locked_ref);
2415 btrfs_put_delayed_ref(ref);
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002416 btrfs_debug(fs_info, "run_one_delayed_ref returned %d", ret);
Dan Carpenter253beeb2012-04-18 09:59:03 +03002417 spin_lock(&delayed_refs->lock);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002418 return ret;
2419 }
2420
Miao Xie093486c2012-12-19 08:10:10 +00002421 /*
2422 * If this node is a head, that means all the refs in this head
2423 * have been dealt with, and we will pick the next head to deal
2424 * with, so we must unlock the head and drop it from the cluster
2425 * list before we release it.
2426 */
2427 if (btrfs_delayed_ref_is_head(ref)) {
2428 list_del_init(&locked_ref->cluster);
2429 btrfs_delayed_ref_unlock(locked_ref);
2430 locked_ref = NULL;
2431 }
2432 btrfs_put_delayed_ref(ref);
2433 count++;
Chris Mason203bf282012-01-06 15:23:57 -05002434next:
Chris Mason1887be62009-03-13 10:11:24 -04002435 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002436 spin_lock(&delayed_refs->lock);
2437 }
Chris Masonc3e69d52009-03-13 10:17:05 -04002438 return count;
2439}
2440
Arne Jansen709c0482011-09-12 12:22:57 +02002441#ifdef SCRAMBLE_DELAYED_REFS
2442/*
2443 * Normally delayed refs get processed in ascending bytenr order. This
2444 * correlates in most cases to the order added. To expose dependencies on this
2445 * order, we start to process the tree in the middle instead of the beginning
2446 */
2447static u64 find_middle(struct rb_root *root)
2448{
2449 struct rb_node *n = root->rb_node;
2450 struct btrfs_delayed_ref_node *entry;
2451 int alt = 1;
2452 u64 middle;
2453 u64 first = 0, last = 0;
2454
2455 n = rb_first(root);
2456 if (n) {
2457 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2458 first = entry->bytenr;
2459 }
2460 n = rb_last(root);
2461 if (n) {
2462 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2463 last = entry->bytenr;
2464 }
2465 n = root->rb_node;
2466
2467 while (n) {
2468 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2469 WARN_ON(!entry->in_tree);
2470
2471 middle = entry->bytenr;
2472
2473 if (alt)
2474 n = n->rb_left;
2475 else
2476 n = n->rb_right;
2477
2478 alt = 1 - alt;
2479 }
2480 return middle;
2481}
2482#endif
2483
Arne Jansenbed92ea2012-06-28 18:03:02 +02002484int btrfs_delayed_refs_qgroup_accounting(struct btrfs_trans_handle *trans,
2485 struct btrfs_fs_info *fs_info)
2486{
2487 struct qgroup_update *qgroup_update;
2488 int ret = 0;
2489
2490 if (list_empty(&trans->qgroup_ref_list) !=
2491 !trans->delayed_ref_elem.seq) {
2492 /* list without seq or seq without list */
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002493 btrfs_err(fs_info,
Jan Schmidtfc36ed72013-04-24 16:57:33 +00002494 "qgroup accounting update error, list is%s empty, seq is %#x.%x",
Arne Jansenbed92ea2012-06-28 18:03:02 +02002495 list_empty(&trans->qgroup_ref_list) ? "" : " not",
Jan Schmidtfc36ed72013-04-24 16:57:33 +00002496 (u32)(trans->delayed_ref_elem.seq >> 32),
2497 (u32)trans->delayed_ref_elem.seq);
Arne Jansenbed92ea2012-06-28 18:03:02 +02002498 BUG();
2499 }
2500
2501 if (!trans->delayed_ref_elem.seq)
2502 return 0;
2503
2504 while (!list_empty(&trans->qgroup_ref_list)) {
2505 qgroup_update = list_first_entry(&trans->qgroup_ref_list,
2506 struct qgroup_update, list);
2507 list_del(&qgroup_update->list);
2508 if (!ret)
2509 ret = btrfs_qgroup_account_ref(
2510 trans, fs_info, qgroup_update->node,
2511 qgroup_update->extent_op);
2512 kfree(qgroup_update);
2513 }
2514
2515 btrfs_put_tree_mod_seq(fs_info, &trans->delayed_ref_elem);
2516
2517 return ret;
2518}
2519
Chris Masonbb721702013-01-29 18:44:12 -05002520static int refs_newer(struct btrfs_delayed_ref_root *delayed_refs, int seq,
2521 int count)
2522{
2523 int val = atomic_read(&delayed_refs->ref_seq);
2524
2525 if (val < seq || val >= seq + count)
2526 return 1;
2527 return 0;
2528}
2529
Josef Bacik1be41b72013-06-12 13:56:06 -04002530static inline u64 heads_to_leaves(struct btrfs_root *root, u64 heads)
2531{
2532 u64 num_bytes;
2533
2534 num_bytes = heads * (sizeof(struct btrfs_extent_item) +
2535 sizeof(struct btrfs_extent_inline_ref));
2536 if (!btrfs_fs_incompat(root->fs_info, SKINNY_METADATA))
2537 num_bytes += heads * sizeof(struct btrfs_tree_block_info);
2538
2539 /*
2540 * We don't ever fill up leaves all the way so multiply by 2 just to be
2541 * closer to what we're really going to want to ouse.
2542 */
2543 return div64_u64(num_bytes, BTRFS_LEAF_DATA_SIZE(root));
2544}
2545
2546int btrfs_should_throttle_delayed_refs(struct btrfs_trans_handle *trans,
2547 struct btrfs_root *root)
2548{
2549 struct btrfs_block_rsv *global_rsv;
2550 u64 num_heads = trans->transaction->delayed_refs.num_heads_ready;
2551 u64 num_bytes;
2552 int ret = 0;
2553
2554 num_bytes = btrfs_calc_trans_metadata_size(root, 1);
2555 num_heads = heads_to_leaves(root, num_heads);
2556 if (num_heads > 1)
2557 num_bytes += (num_heads - 1) * root->leafsize;
2558 num_bytes <<= 1;
2559 global_rsv = &root->fs_info->global_block_rsv;
2560
2561 /*
2562 * If we can't allocate any more chunks lets make sure we have _lots_ of
2563 * wiggle room since running delayed refs can create more delayed refs.
2564 */
2565 if (global_rsv->space_info->full)
2566 num_bytes <<= 1;
2567
2568 spin_lock(&global_rsv->lock);
2569 if (global_rsv->reserved <= num_bytes)
2570 ret = 1;
2571 spin_unlock(&global_rsv->lock);
2572 return ret;
2573}
2574
Chris Masonc3e69d52009-03-13 10:17:05 -04002575/*
2576 * this starts processing the delayed reference count updates and
2577 * extent insertions we have queued up so far. count can be
2578 * 0, which means to process everything in the tree at the start
2579 * of the run (but not newly added entries), or it can be some target
2580 * number you'd like to process.
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002581 *
2582 * Returns 0 on success or if called with an aborted transaction
2583 * Returns <0 on error and aborts the transaction
Chris Masonc3e69d52009-03-13 10:17:05 -04002584 */
2585int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
2586 struct btrfs_root *root, unsigned long count)
2587{
2588 struct rb_node *node;
2589 struct btrfs_delayed_ref_root *delayed_refs;
2590 struct btrfs_delayed_ref_node *ref;
2591 struct list_head cluster;
2592 int ret;
Jan Schmidta1686502011-12-12 16:10:07 +01002593 u64 delayed_start;
Chris Masonc3e69d52009-03-13 10:17:05 -04002594 int run_all = count == (unsigned long)-1;
2595 int run_most = 0;
Arne Jansen1fa11e22012-08-06 14:18:51 -06002596 int loops;
Chris Masonc3e69d52009-03-13 10:17:05 -04002597
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002598 /* We'll clean this up in btrfs_cleanup_transaction */
2599 if (trans->aborted)
2600 return 0;
2601
Chris Masonc3e69d52009-03-13 10:17:05 -04002602 if (root == root->fs_info->extent_root)
2603 root = root->fs_info->tree_root;
2604
Jan Schmidtedf39272012-06-28 18:04:55 +02002605 btrfs_delayed_refs_qgroup_accounting(trans, root->fs_info);
2606
Chris Masonc3e69d52009-03-13 10:17:05 -04002607 delayed_refs = &trans->transaction->delayed_refs;
2608 INIT_LIST_HEAD(&cluster);
Chris Masonbb721702013-01-29 18:44:12 -05002609 if (count == 0) {
2610 count = delayed_refs->num_entries * 2;
2611 run_most = 1;
2612 }
2613
2614 if (!run_all && !run_most) {
2615 int old;
2616 int seq = atomic_read(&delayed_refs->ref_seq);
2617
2618progress:
2619 old = atomic_cmpxchg(&delayed_refs->procs_running_refs, 0, 1);
2620 if (old) {
2621 DEFINE_WAIT(__wait);
Josef Bacik1be41b72013-06-12 13:56:06 -04002622 if (delayed_refs->flushing ||
2623 !btrfs_should_throttle_delayed_refs(trans, root))
Chris Masonbb721702013-01-29 18:44:12 -05002624 return 0;
2625
2626 prepare_to_wait(&delayed_refs->wait, &__wait,
2627 TASK_UNINTERRUPTIBLE);
2628
2629 old = atomic_cmpxchg(&delayed_refs->procs_running_refs, 0, 1);
2630 if (old) {
2631 schedule();
2632 finish_wait(&delayed_refs->wait, &__wait);
2633
2634 if (!refs_newer(delayed_refs, seq, 256))
2635 goto progress;
2636 else
2637 return 0;
2638 } else {
2639 finish_wait(&delayed_refs->wait, &__wait);
2640 goto again;
2641 }
2642 }
2643
2644 } else {
2645 atomic_inc(&delayed_refs->procs_running_refs);
2646 }
2647
Chris Masonc3e69d52009-03-13 10:17:05 -04002648again:
Arne Jansen1fa11e22012-08-06 14:18:51 -06002649 loops = 0;
Chris Masonc3e69d52009-03-13 10:17:05 -04002650 spin_lock(&delayed_refs->lock);
Jan Schmidt097b8a72012-06-21 11:08:04 +02002651
Arne Jansen709c0482011-09-12 12:22:57 +02002652#ifdef SCRAMBLE_DELAYED_REFS
2653 delayed_refs->run_delayed_start = find_middle(&delayed_refs->root);
2654#endif
2655
Chris Masonc3e69d52009-03-13 10:17:05 -04002656 while (1) {
2657 if (!(run_all || run_most) &&
Josef Bacik1be41b72013-06-12 13:56:06 -04002658 !btrfs_should_throttle_delayed_refs(trans, root))
Chris Masonc3e69d52009-03-13 10:17:05 -04002659 break;
2660
2661 /*
2662 * go find something we can process in the rbtree. We start at
2663 * the beginning of the tree, and then build a cluster
2664 * of refs to process starting at the first one we are able to
2665 * lock
2666 */
Jan Schmidta1686502011-12-12 16:10:07 +01002667 delayed_start = delayed_refs->run_delayed_start;
Chris Masonc3e69d52009-03-13 10:17:05 -04002668 ret = btrfs_find_ref_cluster(trans, &cluster,
2669 delayed_refs->run_delayed_start);
2670 if (ret)
2671 break;
2672
2673 ret = run_clustered_refs(trans, root, &cluster);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002674 if (ret < 0) {
Miao Xie093486c2012-12-19 08:10:10 +00002675 btrfs_release_ref_cluster(&cluster);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002676 spin_unlock(&delayed_refs->lock);
2677 btrfs_abort_transaction(trans, root, ret);
Chris Masonbb721702013-01-29 18:44:12 -05002678 atomic_dec(&delayed_refs->procs_running_refs);
Josef Bacikf971fe22013-06-10 11:52:32 -04002679 wake_up(&delayed_refs->wait);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002680 return ret;
2681 }
Chris Masonc3e69d52009-03-13 10:17:05 -04002682
Chris Masonbb721702013-01-29 18:44:12 -05002683 atomic_add(ret, &delayed_refs->ref_seq);
2684
Chris Masonc3e69d52009-03-13 10:17:05 -04002685 count -= min_t(unsigned long, ret, count);
2686
2687 if (count == 0)
2688 break;
Jan Schmidta1686502011-12-12 16:10:07 +01002689
Arne Jansen1fa11e22012-08-06 14:18:51 -06002690 if (delayed_start >= delayed_refs->run_delayed_start) {
2691 if (loops == 0) {
2692 /*
2693 * btrfs_find_ref_cluster looped. let's do one
2694 * more cycle. if we don't run any delayed ref
2695 * during that cycle (because we can't because
2696 * all of them are blocked), bail out.
2697 */
2698 loops = 1;
2699 } else {
2700 /*
2701 * no runnable refs left, stop trying
2702 */
2703 BUG_ON(run_all);
2704 break;
2705 }
2706 }
2707 if (ret) {
Jan Schmidta1686502011-12-12 16:10:07 +01002708 /* refs were run, let's reset staleness detection */
Arne Jansen1fa11e22012-08-06 14:18:51 -06002709 loops = 0;
Jan Schmidta1686502011-12-12 16:10:07 +01002710 }
Chris Masonc3e69d52009-03-13 10:17:05 -04002711 }
2712
Chris Mason56bec292009-03-13 10:10:06 -04002713 if (run_all) {
Josef Bacikea658ba2012-09-11 16:57:25 -04002714 if (!list_empty(&trans->new_bgs)) {
2715 spin_unlock(&delayed_refs->lock);
2716 btrfs_create_pending_block_groups(trans, root);
2717 spin_lock(&delayed_refs->lock);
2718 }
2719
Chris Mason56bec292009-03-13 10:10:06 -04002720 node = rb_first(&delayed_refs->root);
Chris Masonc3e69d52009-03-13 10:17:05 -04002721 if (!node)
Chris Mason56bec292009-03-13 10:10:06 -04002722 goto out;
Chris Masonc3e69d52009-03-13 10:17:05 -04002723 count = (unsigned long)-1;
Chris Mason56bec292009-03-13 10:10:06 -04002724
2725 while (node) {
2726 ref = rb_entry(node, struct btrfs_delayed_ref_node,
2727 rb_node);
2728 if (btrfs_delayed_ref_is_head(ref)) {
2729 struct btrfs_delayed_ref_head *head;
2730
2731 head = btrfs_delayed_node_to_head(ref);
2732 atomic_inc(&ref->refs);
2733
2734 spin_unlock(&delayed_refs->lock);
David Sterba8cc33e52011-05-02 15:29:25 +02002735 /*
2736 * Mutex was contended, block until it's
2737 * released and try again
2738 */
Chris Mason56bec292009-03-13 10:10:06 -04002739 mutex_lock(&head->mutex);
2740 mutex_unlock(&head->mutex);
2741
2742 btrfs_put_delayed_ref(ref);
Chris Mason1887be62009-03-13 10:11:24 -04002743 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002744 goto again;
2745 }
2746 node = rb_next(node);
2747 }
2748 spin_unlock(&delayed_refs->lock);
Chris Mason56bec292009-03-13 10:10:06 -04002749 schedule_timeout(1);
2750 goto again;
2751 }
Chris Mason54aa1f42007-06-22 14:16:25 -04002752out:
Chris Masonbb721702013-01-29 18:44:12 -05002753 atomic_dec(&delayed_refs->procs_running_refs);
2754 smp_mb();
2755 if (waitqueue_active(&delayed_refs->wait))
2756 wake_up(&delayed_refs->wait);
2757
Chris Masonc3e69d52009-03-13 10:17:05 -04002758 spin_unlock(&delayed_refs->lock);
Jan Schmidtedf39272012-06-28 18:04:55 +02002759 assert_qgroups_uptodate(trans);
Chris Masona28ec192007-03-06 20:08:01 -05002760 return 0;
2761}
2762
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002763int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
2764 struct btrfs_root *root,
2765 u64 bytenr, u64 num_bytes, u64 flags,
Josef Bacikb1c79e02013-05-09 13:49:30 -04002766 int level, int is_data)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002767{
2768 struct btrfs_delayed_extent_op *extent_op;
2769 int ret;
2770
Miao Xie78a61842012-11-21 02:21:28 +00002771 extent_op = btrfs_alloc_delayed_extent_op();
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002772 if (!extent_op)
2773 return -ENOMEM;
2774
2775 extent_op->flags_to_set = flags;
2776 extent_op->update_flags = 1;
2777 extent_op->update_key = 0;
2778 extent_op->is_data = is_data ? 1 : 0;
Josef Bacikb1c79e02013-05-09 13:49:30 -04002779 extent_op->level = level;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002780
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002781 ret = btrfs_add_delayed_extent_op(root->fs_info, trans, bytenr,
2782 num_bytes, extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002783 if (ret)
Miao Xie78a61842012-11-21 02:21:28 +00002784 btrfs_free_delayed_extent_op(extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002785 return ret;
2786}
2787
2788static noinline int check_delayed_ref(struct btrfs_trans_handle *trans,
2789 struct btrfs_root *root,
2790 struct btrfs_path *path,
2791 u64 objectid, u64 offset, u64 bytenr)
2792{
2793 struct btrfs_delayed_ref_head *head;
2794 struct btrfs_delayed_ref_node *ref;
2795 struct btrfs_delayed_data_ref *data_ref;
2796 struct btrfs_delayed_ref_root *delayed_refs;
2797 struct rb_node *node;
2798 int ret = 0;
2799
2800 ret = -ENOENT;
2801 delayed_refs = &trans->transaction->delayed_refs;
2802 spin_lock(&delayed_refs->lock);
2803 head = btrfs_find_delayed_ref_head(trans, bytenr);
2804 if (!head)
2805 goto out;
2806
2807 if (!mutex_trylock(&head->mutex)) {
2808 atomic_inc(&head->node.refs);
2809 spin_unlock(&delayed_refs->lock);
2810
David Sterbab3b4aa72011-04-21 01:20:15 +02002811 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002812
David Sterba8cc33e52011-05-02 15:29:25 +02002813 /*
2814 * Mutex was contended, block until it's released and let
2815 * caller try again
2816 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002817 mutex_lock(&head->mutex);
2818 mutex_unlock(&head->mutex);
2819 btrfs_put_delayed_ref(&head->node);
2820 return -EAGAIN;
2821 }
2822
2823 node = rb_prev(&head->node.rb_node);
2824 if (!node)
2825 goto out_unlock;
2826
2827 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2828
2829 if (ref->bytenr != bytenr)
2830 goto out_unlock;
2831
2832 ret = 1;
2833 if (ref->type != BTRFS_EXTENT_DATA_REF_KEY)
2834 goto out_unlock;
2835
2836 data_ref = btrfs_delayed_node_to_data_ref(ref);
2837
2838 node = rb_prev(node);
2839 if (node) {
Liu Bodf57dbe2012-07-23 05:50:03 -06002840 int seq = ref->seq;
2841
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002842 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
Liu Bodf57dbe2012-07-23 05:50:03 -06002843 if (ref->bytenr == bytenr && ref->seq == seq)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002844 goto out_unlock;
2845 }
2846
2847 if (data_ref->root != root->root_key.objectid ||
2848 data_ref->objectid != objectid || data_ref->offset != offset)
2849 goto out_unlock;
2850
2851 ret = 0;
2852out_unlock:
2853 mutex_unlock(&head->mutex);
2854out:
2855 spin_unlock(&delayed_refs->lock);
2856 return ret;
2857}
2858
2859static noinline int check_committed_ref(struct btrfs_trans_handle *trans,
2860 struct btrfs_root *root,
2861 struct btrfs_path *path,
2862 u64 objectid, u64 offset, u64 bytenr)
Chris Masonbe20aa92007-12-17 20:14:01 -05002863{
2864 struct btrfs_root *extent_root = root->fs_info->extent_root;
Yan Zhengf321e492008-07-30 09:26:11 -04002865 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002866 struct btrfs_extent_data_ref *ref;
2867 struct btrfs_extent_inline_ref *iref;
2868 struct btrfs_extent_item *ei;
Chris Masonbe20aa92007-12-17 20:14:01 -05002869 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002870 u32 item_size;
Yan Zhengf321e492008-07-30 09:26:11 -04002871 int ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05002872
Chris Masonbe20aa92007-12-17 20:14:01 -05002873 key.objectid = bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -04002874 key.offset = (u64)-1;
Yan Zhengf321e492008-07-30 09:26:11 -04002875 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masonbe20aa92007-12-17 20:14:01 -05002876
Chris Masonbe20aa92007-12-17 20:14:01 -05002877 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
2878 if (ret < 0)
2879 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002880 BUG_ON(ret == 0); /* Corruption */
Yan Zheng80ff3852008-10-30 14:20:02 -04002881
2882 ret = -ENOENT;
2883 if (path->slots[0] == 0)
Zheng Yan31840ae2008-09-23 13:14:14 -04002884 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002885
Zheng Yan31840ae2008-09-23 13:14:14 -04002886 path->slots[0]--;
Yan Zhengf321e492008-07-30 09:26:11 -04002887 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002888 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05002889
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002890 if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY)
Chris Masonbe20aa92007-12-17 20:14:01 -05002891 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002892
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002893 ret = 1;
2894 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2895#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2896 if (item_size < sizeof(*ei)) {
2897 WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0));
2898 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002899 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002900#endif
2901 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2902
2903 if (item_size != sizeof(*ei) +
2904 btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY))
2905 goto out;
2906
2907 if (btrfs_extent_generation(leaf, ei) <=
2908 btrfs_root_last_snapshot(&root->root_item))
2909 goto out;
2910
2911 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
2912 if (btrfs_extent_inline_ref_type(leaf, iref) !=
2913 BTRFS_EXTENT_DATA_REF_KEY)
2914 goto out;
2915
2916 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
2917 if (btrfs_extent_refs(leaf, ei) !=
2918 btrfs_extent_data_ref_count(leaf, ref) ||
2919 btrfs_extent_data_ref_root(leaf, ref) !=
2920 root->root_key.objectid ||
2921 btrfs_extent_data_ref_objectid(leaf, ref) != objectid ||
2922 btrfs_extent_data_ref_offset(leaf, ref) != offset)
2923 goto out;
2924
Yan Zhengf321e492008-07-30 09:26:11 -04002925 ret = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05002926out:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002927 return ret;
2928}
2929
2930int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
2931 struct btrfs_root *root,
2932 u64 objectid, u64 offset, u64 bytenr)
2933{
2934 struct btrfs_path *path;
2935 int ret;
2936 int ret2;
2937
2938 path = btrfs_alloc_path();
2939 if (!path)
2940 return -ENOENT;
2941
2942 do {
2943 ret = check_committed_ref(trans, root, path, objectid,
2944 offset, bytenr);
2945 if (ret && ret != -ENOENT)
2946 goto out;
2947
2948 ret2 = check_delayed_ref(trans, root, path, objectid,
2949 offset, bytenr);
2950 } while (ret2 == -EAGAIN);
2951
2952 if (ret2 && ret2 != -ENOENT) {
2953 ret = ret2;
2954 goto out;
2955 }
2956
2957 if (ret != -ENOENT || ret2 != -ENOENT)
2958 ret = 0;
2959out:
Yan Zhengf321e492008-07-30 09:26:11 -04002960 btrfs_free_path(path);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002961 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2962 WARN_ON(ret > 0);
Yan Zhengf321e492008-07-30 09:26:11 -04002963 return ret;
2964}
2965
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002966static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
Chris Masonb7a9f292009-02-04 09:23:45 -05002967 struct btrfs_root *root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002968 struct extent_buffer *buf,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002969 int full_backref, int inc, int for_cow)
Zheng Yan31840ae2008-09-23 13:14:14 -04002970{
2971 u64 bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002972 u64 num_bytes;
2973 u64 parent;
Zheng Yan31840ae2008-09-23 13:14:14 -04002974 u64 ref_root;
Zheng Yan31840ae2008-09-23 13:14:14 -04002975 u32 nritems;
Zheng Yan31840ae2008-09-23 13:14:14 -04002976 struct btrfs_key key;
2977 struct btrfs_file_extent_item *fi;
2978 int i;
2979 int level;
2980 int ret = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04002981 int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002982 u64, u64, u64, u64, u64, u64, int);
Zheng Yan31840ae2008-09-23 13:14:14 -04002983
2984 ref_root = btrfs_header_owner(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04002985 nritems = btrfs_header_nritems(buf);
2986 level = btrfs_header_level(buf);
2987
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002988 if (!root->ref_cows && level == 0)
2989 return 0;
Chris Masonb7a9f292009-02-04 09:23:45 -05002990
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002991 if (inc)
2992 process_func = btrfs_inc_extent_ref;
2993 else
2994 process_func = btrfs_free_extent;
Zheng Yan31840ae2008-09-23 13:14:14 -04002995
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002996 if (full_backref)
2997 parent = buf->start;
2998 else
2999 parent = 0;
3000
Zheng Yan31840ae2008-09-23 13:14:14 -04003001 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04003002 if (level == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003003 btrfs_item_key_to_cpu(buf, &key, i);
3004 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -04003005 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04003006 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -04003007 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003008 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -04003009 BTRFS_FILE_EXTENT_INLINE)
3010 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04003011 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
3012 if (bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -04003013 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04003014
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003015 num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
3016 key.offset -= btrfs_file_extent_offset(buf, fi);
3017 ret = process_func(trans, root, bytenr, num_bytes,
3018 parent, ref_root, key.objectid,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02003019 key.offset, for_cow);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003020 if (ret)
3021 goto fail;
Chris Masonb7a9f292009-02-04 09:23:45 -05003022 } else {
3023 bytenr = btrfs_node_blockptr(buf, i);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003024 num_bytes = btrfs_level_size(root, level - 1);
3025 ret = process_func(trans, root, bytenr, num_bytes,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02003026 parent, ref_root, level - 1, 0,
3027 for_cow);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003028 if (ret)
Zheng Yan31840ae2008-09-23 13:14:14 -04003029 goto fail;
Chris Mason54aa1f42007-06-22 14:16:25 -04003030 }
3031 }
Zheng Yan31840ae2008-09-23 13:14:14 -04003032 return 0;
3033fail:
Chris Mason54aa1f42007-06-22 14:16:25 -04003034 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05003035}
3036
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003037int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02003038 struct extent_buffer *buf, int full_backref, int for_cow)
Zheng Yan31840ae2008-09-23 13:14:14 -04003039{
Arne Jansen66d7e7f2011-09-12 15:26:38 +02003040 return __btrfs_mod_ref(trans, root, buf, full_backref, 1, for_cow);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003041}
Zheng Yan31840ae2008-09-23 13:14:14 -04003042
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003043int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02003044 struct extent_buffer *buf, int full_backref, int for_cow)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003045{
Arne Jansen66d7e7f2011-09-12 15:26:38 +02003046 return __btrfs_mod_ref(trans, root, buf, full_backref, 0, for_cow);
Zheng Yan31840ae2008-09-23 13:14:14 -04003047}
3048
Chris Mason9078a3e2007-04-26 16:46:15 -04003049static int write_one_cache_group(struct btrfs_trans_handle *trans,
3050 struct btrfs_root *root,
3051 struct btrfs_path *path,
3052 struct btrfs_block_group_cache *cache)
3053{
3054 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04003055 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04003056 unsigned long bi;
3057 struct extent_buffer *leaf;
Chris Mason9078a3e2007-04-26 16:46:15 -04003058
Chris Mason9078a3e2007-04-26 16:46:15 -04003059 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04003060 if (ret < 0)
3061 goto fail;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003062 BUG_ON(ret); /* Corruption */
Chris Mason5f39d392007-10-15 16:14:19 -04003063
3064 leaf = path->nodes[0];
3065 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
3066 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
3067 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02003068 btrfs_release_path(path);
Chris Mason54aa1f42007-06-22 14:16:25 -04003069fail:
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003070 if (ret) {
3071 btrfs_abort_transaction(trans, root, ret);
Chris Mason9078a3e2007-04-26 16:46:15 -04003072 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003073 }
Chris Mason9078a3e2007-04-26 16:46:15 -04003074 return 0;
3075
3076}
3077
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003078static struct btrfs_block_group_cache *
3079next_block_group(struct btrfs_root *root,
3080 struct btrfs_block_group_cache *cache)
3081{
3082 struct rb_node *node;
3083 spin_lock(&root->fs_info->block_group_cache_lock);
3084 node = rb_next(&cache->cache_node);
3085 btrfs_put_block_group(cache);
3086 if (node) {
3087 cache = rb_entry(node, struct btrfs_block_group_cache,
3088 cache_node);
Josef Bacik11dfe352009-11-13 20:12:59 +00003089 btrfs_get_block_group(cache);
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003090 } else
3091 cache = NULL;
3092 spin_unlock(&root->fs_info->block_group_cache_lock);
3093 return cache;
3094}
3095
Josef Bacik0af3d002010-06-21 14:48:16 -04003096static int cache_save_setup(struct btrfs_block_group_cache *block_group,
3097 struct btrfs_trans_handle *trans,
3098 struct btrfs_path *path)
3099{
3100 struct btrfs_root *root = block_group->fs_info->tree_root;
3101 struct inode *inode = NULL;
3102 u64 alloc_hint = 0;
Josef Bacik2b209822010-12-03 13:17:53 -05003103 int dcs = BTRFS_DC_ERROR;
Josef Bacik0af3d002010-06-21 14:48:16 -04003104 int num_pages = 0;
3105 int retries = 0;
3106 int ret = 0;
3107
3108 /*
3109 * If this block group is smaller than 100 megs don't bother caching the
3110 * block group.
3111 */
3112 if (block_group->key.offset < (100 * 1024 * 1024)) {
3113 spin_lock(&block_group->lock);
3114 block_group->disk_cache_state = BTRFS_DC_WRITTEN;
3115 spin_unlock(&block_group->lock);
3116 return 0;
3117 }
3118
3119again:
3120 inode = lookup_free_space_inode(root, block_group, path);
3121 if (IS_ERR(inode) && PTR_ERR(inode) != -ENOENT) {
3122 ret = PTR_ERR(inode);
David Sterbab3b4aa72011-04-21 01:20:15 +02003123 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04003124 goto out;
3125 }
3126
3127 if (IS_ERR(inode)) {
3128 BUG_ON(retries);
3129 retries++;
3130
3131 if (block_group->ro)
3132 goto out_free;
3133
3134 ret = create_free_space_inode(root, trans, block_group, path);
3135 if (ret)
3136 goto out_free;
3137 goto again;
3138 }
3139
Josef Bacik5b0e95b2011-10-06 08:58:24 -04003140 /* We've already setup this transaction, go ahead and exit */
3141 if (block_group->cache_generation == trans->transid &&
3142 i_size_read(inode)) {
3143 dcs = BTRFS_DC_SETUP;
3144 goto out_put;
3145 }
3146
Josef Bacik0af3d002010-06-21 14:48:16 -04003147 /*
3148 * We want to set the generation to 0, that way if anything goes wrong
3149 * from here on out we know not to trust this cache when we load up next
3150 * time.
3151 */
3152 BTRFS_I(inode)->generation = 0;
3153 ret = btrfs_update_inode(trans, root, inode);
3154 WARN_ON(ret);
3155
3156 if (i_size_read(inode) > 0) {
Miao Xie7b61cd92013-05-13 13:55:09 +00003157 ret = btrfs_check_trunc_cache_free_space(root,
3158 &root->fs_info->global_block_rsv);
3159 if (ret)
3160 goto out_put;
3161
Josef Bacik0af3d002010-06-21 14:48:16 -04003162 ret = btrfs_truncate_free_space_cache(root, trans, path,
3163 inode);
3164 if (ret)
3165 goto out_put;
3166 }
3167
3168 spin_lock(&block_group->lock);
Liu Bocf7c1ef2012-07-06 03:31:34 -06003169 if (block_group->cached != BTRFS_CACHE_FINISHED ||
3170 !btrfs_test_opt(root, SPACE_CACHE)) {
3171 /*
3172 * don't bother trying to write stuff out _if_
3173 * a) we're not cached,
3174 * b) we're with nospace_cache mount option.
3175 */
Josef Bacik2b209822010-12-03 13:17:53 -05003176 dcs = BTRFS_DC_WRITTEN;
Josef Bacik0af3d002010-06-21 14:48:16 -04003177 spin_unlock(&block_group->lock);
3178 goto out_put;
3179 }
3180 spin_unlock(&block_group->lock);
3181
Josef Bacik6fc823b2012-08-06 13:46:38 -06003182 /*
3183 * Try to preallocate enough space based on how big the block group is.
3184 * Keep in mind this has to include any pinned space which could end up
3185 * taking up quite a bit since it's not folded into the other space
3186 * cache.
3187 */
3188 num_pages = (int)div64_u64(block_group->key.offset, 256 * 1024 * 1024);
Josef Bacik0af3d002010-06-21 14:48:16 -04003189 if (!num_pages)
3190 num_pages = 1;
3191
Josef Bacik0af3d002010-06-21 14:48:16 -04003192 num_pages *= 16;
3193 num_pages *= PAGE_CACHE_SIZE;
3194
3195 ret = btrfs_check_data_free_space(inode, num_pages);
3196 if (ret)
3197 goto out_put;
3198
3199 ret = btrfs_prealloc_file_range_trans(inode, trans, 0, 0, num_pages,
3200 num_pages, num_pages,
3201 &alloc_hint);
Josef Bacik2b209822010-12-03 13:17:53 -05003202 if (!ret)
3203 dcs = BTRFS_DC_SETUP;
Josef Bacik0af3d002010-06-21 14:48:16 -04003204 btrfs_free_reserved_data_space(inode, num_pages);
Josef Bacikc09544e2011-08-30 10:19:10 -04003205
Josef Bacik0af3d002010-06-21 14:48:16 -04003206out_put:
3207 iput(inode);
3208out_free:
David Sterbab3b4aa72011-04-21 01:20:15 +02003209 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04003210out:
3211 spin_lock(&block_group->lock);
Josef Bacike65cbb92011-12-13 16:04:54 -05003212 if (!ret && dcs == BTRFS_DC_SETUP)
Josef Bacik5b0e95b2011-10-06 08:58:24 -04003213 block_group->cache_generation = trans->transid;
Josef Bacik2b209822010-12-03 13:17:53 -05003214 block_group->disk_cache_state = dcs;
Josef Bacik0af3d002010-06-21 14:48:16 -04003215 spin_unlock(&block_group->lock);
3216
3217 return ret;
3218}
3219
Chris Mason96b51792007-10-15 16:15:19 -04003220int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
3221 struct btrfs_root *root)
Chris Mason9078a3e2007-04-26 16:46:15 -04003222{
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003223 struct btrfs_block_group_cache *cache;
Chris Mason9078a3e2007-04-26 16:46:15 -04003224 int err = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04003225 struct btrfs_path *path;
Chris Mason96b51792007-10-15 16:15:19 -04003226 u64 last = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04003227
3228 path = btrfs_alloc_path();
3229 if (!path)
3230 return -ENOMEM;
3231
Josef Bacik0af3d002010-06-21 14:48:16 -04003232again:
3233 while (1) {
3234 cache = btrfs_lookup_first_block_group(root->fs_info, last);
3235 while (cache) {
3236 if (cache->disk_cache_state == BTRFS_DC_CLEAR)
3237 break;
3238 cache = next_block_group(root, cache);
3239 }
3240 if (!cache) {
3241 if (last == 0)
3242 break;
3243 last = 0;
3244 continue;
3245 }
3246 err = cache_save_setup(cache, trans, path);
3247 last = cache->key.objectid + cache->key.offset;
3248 btrfs_put_block_group(cache);
3249 }
3250
Chris Masond3977122009-01-05 21:25:51 -05003251 while (1) {
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003252 if (last == 0) {
3253 err = btrfs_run_delayed_refs(trans, root,
3254 (unsigned long)-1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003255 if (err) /* File system offline */
3256 goto out;
Josef Bacik0f9dd462008-09-23 13:14:11 -04003257 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04003258
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003259 cache = btrfs_lookup_first_block_group(root->fs_info, last);
3260 while (cache) {
Josef Bacik0af3d002010-06-21 14:48:16 -04003261 if (cache->disk_cache_state == BTRFS_DC_CLEAR) {
3262 btrfs_put_block_group(cache);
3263 goto again;
3264 }
3265
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003266 if (cache->dirty)
3267 break;
3268 cache = next_block_group(root, cache);
3269 }
3270 if (!cache) {
3271 if (last == 0)
3272 break;
3273 last = 0;
Chris Mason96b51792007-10-15 16:15:19 -04003274 continue;
Chris Mason9078a3e2007-04-26 16:46:15 -04003275 }
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003276
Josef Bacik0cb59c92010-07-02 12:14:14 -04003277 if (cache->disk_cache_state == BTRFS_DC_SETUP)
3278 cache->disk_cache_state = BTRFS_DC_NEED_WRITE;
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003279 cache->dirty = 0;
3280 last = cache->key.objectid + cache->key.offset;
3281
3282 err = write_one_cache_group(trans, root, path, cache);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003283 if (err) /* File system offline */
3284 goto out;
3285
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003286 btrfs_put_block_group(cache);
Chris Mason9078a3e2007-04-26 16:46:15 -04003287 }
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003288
Josef Bacik0cb59c92010-07-02 12:14:14 -04003289 while (1) {
3290 /*
3291 * I don't think this is needed since we're just marking our
3292 * preallocated extent as written, but just in case it can't
3293 * hurt.
3294 */
3295 if (last == 0) {
3296 err = btrfs_run_delayed_refs(trans, root,
3297 (unsigned long)-1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003298 if (err) /* File system offline */
3299 goto out;
Josef Bacik0cb59c92010-07-02 12:14:14 -04003300 }
3301
3302 cache = btrfs_lookup_first_block_group(root->fs_info, last);
3303 while (cache) {
3304 /*
3305 * Really this shouldn't happen, but it could if we
3306 * couldn't write the entire preallocated extent and
3307 * splitting the extent resulted in a new block.
3308 */
3309 if (cache->dirty) {
3310 btrfs_put_block_group(cache);
3311 goto again;
3312 }
3313 if (cache->disk_cache_state == BTRFS_DC_NEED_WRITE)
3314 break;
3315 cache = next_block_group(root, cache);
3316 }
3317 if (!cache) {
3318 if (last == 0)
3319 break;
3320 last = 0;
3321 continue;
3322 }
3323
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003324 err = btrfs_write_out_cache(root, trans, cache, path);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003325
3326 /*
3327 * If we didn't have an error then the cache state is still
3328 * NEED_WRITE, so we can set it to WRITTEN.
3329 */
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003330 if (!err && cache->disk_cache_state == BTRFS_DC_NEED_WRITE)
Josef Bacik0cb59c92010-07-02 12:14:14 -04003331 cache->disk_cache_state = BTRFS_DC_WRITTEN;
3332 last = cache->key.objectid + cache->key.offset;
3333 btrfs_put_block_group(cache);
3334 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003335out:
Josef Bacik0cb59c92010-07-02 12:14:14 -04003336
Chris Mason9078a3e2007-04-26 16:46:15 -04003337 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003338 return err;
Chris Mason9078a3e2007-04-26 16:46:15 -04003339}
3340
Yan Zhengd2fb3432008-12-11 16:30:39 -05003341int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr)
3342{
3343 struct btrfs_block_group_cache *block_group;
3344 int readonly = 0;
3345
3346 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
3347 if (!block_group || block_group->ro)
3348 readonly = 1;
3349 if (block_group)
Chris Masonfa9c0d72009-04-03 09:47:43 -04003350 btrfs_put_block_group(block_group);
Yan Zhengd2fb3432008-12-11 16:30:39 -05003351 return readonly;
3352}
3353
Chris Mason593060d2008-03-25 16:50:33 -04003354static int update_space_info(struct btrfs_fs_info *info, u64 flags,
3355 u64 total_bytes, u64 bytes_used,
3356 struct btrfs_space_info **space_info)
3357{
3358 struct btrfs_space_info *found;
Yan, Zhengb742bb82010-05-16 10:46:24 -04003359 int i;
3360 int factor;
Josef Bacikb150a4f2013-06-19 15:00:04 -04003361 int ret;
Yan, Zhengb742bb82010-05-16 10:46:24 -04003362
3363 if (flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
3364 BTRFS_BLOCK_GROUP_RAID10))
3365 factor = 2;
3366 else
3367 factor = 1;
Chris Mason593060d2008-03-25 16:50:33 -04003368
3369 found = __find_space_info(info, flags);
3370 if (found) {
Josef Bacik25179202008-10-29 14:49:05 -04003371 spin_lock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04003372 found->total_bytes += total_bytes;
Josef Bacik89a55892010-10-14 14:52:27 -04003373 found->disk_total += total_bytes * factor;
Chris Mason593060d2008-03-25 16:50:33 -04003374 found->bytes_used += bytes_used;
Yan, Zhengb742bb82010-05-16 10:46:24 -04003375 found->disk_used += bytes_used * factor;
Chris Mason8f18cf12008-04-25 16:53:30 -04003376 found->full = 0;
Josef Bacik25179202008-10-29 14:49:05 -04003377 spin_unlock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04003378 *space_info = found;
3379 return 0;
3380 }
Yan Zhengc146afa2008-11-12 14:34:12 -05003381 found = kzalloc(sizeof(*found), GFP_NOFS);
Chris Mason593060d2008-03-25 16:50:33 -04003382 if (!found)
3383 return -ENOMEM;
3384
Josef Bacikb150a4f2013-06-19 15:00:04 -04003385 ret = percpu_counter_init(&found->total_bytes_pinned, 0);
3386 if (ret) {
3387 kfree(found);
3388 return ret;
3389 }
3390
Yan, Zhengb742bb82010-05-16 10:46:24 -04003391 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++)
3392 INIT_LIST_HEAD(&found->block_groups[i]);
Josef Bacik80eb2342008-10-29 14:49:05 -04003393 init_rwsem(&found->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003394 spin_lock_init(&found->lock);
Ilya Dryomov52ba6922012-01-16 22:04:47 +02003395 found->flags = flags & BTRFS_BLOCK_GROUP_TYPE_MASK;
Chris Mason593060d2008-03-25 16:50:33 -04003396 found->total_bytes = total_bytes;
Josef Bacik89a55892010-10-14 14:52:27 -04003397 found->disk_total = total_bytes * factor;
Chris Mason593060d2008-03-25 16:50:33 -04003398 found->bytes_used = bytes_used;
Yan, Zhengb742bb82010-05-16 10:46:24 -04003399 found->disk_used = bytes_used * factor;
Chris Mason593060d2008-03-25 16:50:33 -04003400 found->bytes_pinned = 0;
Zheng Yane8569812008-09-26 10:05:48 -04003401 found->bytes_reserved = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05003402 found->bytes_readonly = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003403 found->bytes_may_use = 0;
Chris Mason593060d2008-03-25 16:50:33 -04003404 found->full = 0;
Chris Mason0e4f8f82011-04-15 16:05:44 -04003405 found->force_alloc = CHUNK_ALLOC_NO_FORCE;
Josef Bacik6d741192011-04-11 20:20:11 -04003406 found->chunk_alloc = 0;
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003407 found->flush = 0;
3408 init_waitqueue_head(&found->wait);
Chris Mason593060d2008-03-25 16:50:33 -04003409 *space_info = found;
Chris Mason4184ea72009-03-10 12:39:20 -04003410 list_add_rcu(&found->list, &info->space_info);
Li Zefanb4d7c3c2012-07-09 20:21:07 -06003411 if (flags & BTRFS_BLOCK_GROUP_DATA)
3412 info->data_sinfo = found;
Chris Mason593060d2008-03-25 16:50:33 -04003413 return 0;
3414}
3415
Chris Mason8790d502008-04-03 16:29:03 -04003416static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
3417{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003418 u64 extra_flags = chunk_to_extended(flags) &
3419 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003420
Miao Xiede98ced2013-01-29 10:13:12 +00003421 write_seqlock(&fs_info->profiles_lock);
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003422 if (flags & BTRFS_BLOCK_GROUP_DATA)
3423 fs_info->avail_data_alloc_bits |= extra_flags;
3424 if (flags & BTRFS_BLOCK_GROUP_METADATA)
3425 fs_info->avail_metadata_alloc_bits |= extra_flags;
3426 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
3427 fs_info->avail_system_alloc_bits |= extra_flags;
Miao Xiede98ced2013-01-29 10:13:12 +00003428 write_sequnlock(&fs_info->profiles_lock);
Chris Mason8790d502008-04-03 16:29:03 -04003429}
Chris Mason593060d2008-03-25 16:50:33 -04003430
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003431/*
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003432 * returns target flags in extended format or 0 if restripe for this
3433 * chunk_type is not in progress
Ilya Dryomovc6664b42012-04-12 16:03:56 -04003434 *
3435 * should be called with either volume_mutex or balance_lock held
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003436 */
3437static u64 get_restripe_target(struct btrfs_fs_info *fs_info, u64 flags)
3438{
3439 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3440 u64 target = 0;
3441
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003442 if (!bctl)
3443 return 0;
3444
3445 if (flags & BTRFS_BLOCK_GROUP_DATA &&
3446 bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3447 target = BTRFS_BLOCK_GROUP_DATA | bctl->data.target;
3448 } else if (flags & BTRFS_BLOCK_GROUP_SYSTEM &&
3449 bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3450 target = BTRFS_BLOCK_GROUP_SYSTEM | bctl->sys.target;
3451 } else if (flags & BTRFS_BLOCK_GROUP_METADATA &&
3452 bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3453 target = BTRFS_BLOCK_GROUP_METADATA | bctl->meta.target;
3454 }
3455
3456 return target;
3457}
3458
3459/*
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003460 * @flags: available profiles in extended format (see ctree.h)
3461 *
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003462 * Returns reduced profile in chunk format. If profile changing is in
3463 * progress (either running or paused) picks the target profile (if it's
3464 * already available), otherwise falls back to plain reducing.
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003465 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003466static u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
Chris Masonec44a352008-04-28 15:29:52 -04003467{
Chris Masoncd02dca2010-12-13 14:56:23 -05003468 /*
3469 * we add in the count of missing devices because we want
3470 * to make sure that any RAID levels on a degraded FS
3471 * continue to be honored.
3472 */
3473 u64 num_devices = root->fs_info->fs_devices->rw_devices +
3474 root->fs_info->fs_devices->missing_devices;
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003475 u64 target;
David Woodhouse53b381b2013-01-29 18:40:14 -05003476 u64 tmp;
Chris Masona061fc82008-05-07 11:43:44 -04003477
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003478 /*
3479 * see if restripe for this chunk_type is in progress, if so
3480 * try to reduce to the target profile
3481 */
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003482 spin_lock(&root->fs_info->balance_lock);
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003483 target = get_restripe_target(root->fs_info, flags);
3484 if (target) {
3485 /* pick target profile only if it's already available */
3486 if ((flags & target) & BTRFS_EXTENDED_PROFILE_MASK) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003487 spin_unlock(&root->fs_info->balance_lock);
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003488 return extended_to_chunk(target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003489 }
3490 }
3491 spin_unlock(&root->fs_info->balance_lock);
3492
David Woodhouse53b381b2013-01-29 18:40:14 -05003493 /* First, mask out the RAID levels which aren't possible */
Chris Masona061fc82008-05-07 11:43:44 -04003494 if (num_devices == 1)
David Woodhouse53b381b2013-01-29 18:40:14 -05003495 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0 |
3496 BTRFS_BLOCK_GROUP_RAID5);
3497 if (num_devices < 3)
3498 flags &= ~BTRFS_BLOCK_GROUP_RAID6;
Chris Masona061fc82008-05-07 11:43:44 -04003499 if (num_devices < 4)
3500 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
3501
David Woodhouse53b381b2013-01-29 18:40:14 -05003502 tmp = flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID0 |
3503 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID5 |
3504 BTRFS_BLOCK_GROUP_RAID6 | BTRFS_BLOCK_GROUP_RAID10);
3505 flags &= ~tmp;
Chris Masonec44a352008-04-28 15:29:52 -04003506
David Woodhouse53b381b2013-01-29 18:40:14 -05003507 if (tmp & BTRFS_BLOCK_GROUP_RAID6)
3508 tmp = BTRFS_BLOCK_GROUP_RAID6;
3509 else if (tmp & BTRFS_BLOCK_GROUP_RAID5)
3510 tmp = BTRFS_BLOCK_GROUP_RAID5;
3511 else if (tmp & BTRFS_BLOCK_GROUP_RAID10)
3512 tmp = BTRFS_BLOCK_GROUP_RAID10;
3513 else if (tmp & BTRFS_BLOCK_GROUP_RAID1)
3514 tmp = BTRFS_BLOCK_GROUP_RAID1;
3515 else if (tmp & BTRFS_BLOCK_GROUP_RAID0)
3516 tmp = BTRFS_BLOCK_GROUP_RAID0;
Chris Masonec44a352008-04-28 15:29:52 -04003517
David Woodhouse53b381b2013-01-29 18:40:14 -05003518 return extended_to_chunk(flags | tmp);
Chris Masonec44a352008-04-28 15:29:52 -04003519}
3520
Yan, Zhengb742bb82010-05-16 10:46:24 -04003521static u64 get_alloc_profile(struct btrfs_root *root, u64 flags)
Josef Bacik6a632092009-02-20 11:00:09 -05003522{
Miao Xiede98ced2013-01-29 10:13:12 +00003523 unsigned seq;
3524
3525 do {
3526 seq = read_seqbegin(&root->fs_info->profiles_lock);
3527
3528 if (flags & BTRFS_BLOCK_GROUP_DATA)
3529 flags |= root->fs_info->avail_data_alloc_bits;
3530 else if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
3531 flags |= root->fs_info->avail_system_alloc_bits;
3532 else if (flags & BTRFS_BLOCK_GROUP_METADATA)
3533 flags |= root->fs_info->avail_metadata_alloc_bits;
3534 } while (read_seqretry(&root->fs_info->profiles_lock, seq));
Ilya Dryomov6fef8df2012-01-16 22:04:47 +02003535
Yan, Zhengb742bb82010-05-16 10:46:24 -04003536 return btrfs_reduce_alloc_profile(root, flags);
3537}
Josef Bacik6a632092009-02-20 11:00:09 -05003538
Miao Xie6d07bce2011-01-05 10:07:31 +00003539u64 btrfs_get_alloc_profile(struct btrfs_root *root, int data)
Yan, Zhengb742bb82010-05-16 10:46:24 -04003540{
3541 u64 flags;
David Woodhouse53b381b2013-01-29 18:40:14 -05003542 u64 ret;
Josef Bacik6a632092009-02-20 11:00:09 -05003543
Yan, Zhengb742bb82010-05-16 10:46:24 -04003544 if (data)
3545 flags = BTRFS_BLOCK_GROUP_DATA;
3546 else if (root == root->fs_info->chunk_root)
3547 flags = BTRFS_BLOCK_GROUP_SYSTEM;
3548 else
3549 flags = BTRFS_BLOCK_GROUP_METADATA;
3550
David Woodhouse53b381b2013-01-29 18:40:14 -05003551 ret = get_alloc_profile(root, flags);
3552 return ret;
Josef Bacik6a632092009-02-20 11:00:09 -05003553}
3554
Josef Bacik6a632092009-02-20 11:00:09 -05003555/*
3556 * This will check the space that the inode allocates from to make sure we have
3557 * enough space for bytes.
3558 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003559int btrfs_check_data_free_space(struct inode *inode, u64 bytes)
Josef Bacik6a632092009-02-20 11:00:09 -05003560{
3561 struct btrfs_space_info *data_sinfo;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003562 struct btrfs_root *root = BTRFS_I(inode)->root;
Li Zefanb4d7c3c2012-07-09 20:21:07 -06003563 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacikab6e24102010-03-19 14:38:13 +00003564 u64 used;
Josef Bacik0af3d002010-06-21 14:48:16 -04003565 int ret = 0, committed = 0, alloc_chunk = 1;
Josef Bacik6a632092009-02-20 11:00:09 -05003566
3567 /* make sure bytes are sectorsize aligned */
Qu Wenruofda28322013-02-26 08:10:22 +00003568 bytes = ALIGN(bytes, root->sectorsize);
Josef Bacik6a632092009-02-20 11:00:09 -05003569
Li Zefan82d59022011-04-20 10:33:24 +08003570 if (root == root->fs_info->tree_root ||
3571 BTRFS_I(inode)->location.objectid == BTRFS_FREE_INO_OBJECTID) {
Josef Bacik0af3d002010-06-21 14:48:16 -04003572 alloc_chunk = 0;
3573 committed = 1;
3574 }
3575
Li Zefanb4d7c3c2012-07-09 20:21:07 -06003576 data_sinfo = fs_info->data_sinfo;
Chris Mason33b4d472009-09-22 14:45:50 -04003577 if (!data_sinfo)
3578 goto alloc;
3579
Josef Bacik6a632092009-02-20 11:00:09 -05003580again:
3581 /* make sure we have enough space to handle the data first */
3582 spin_lock(&data_sinfo->lock);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003583 used = data_sinfo->bytes_used + data_sinfo->bytes_reserved +
3584 data_sinfo->bytes_pinned + data_sinfo->bytes_readonly +
3585 data_sinfo->bytes_may_use;
Josef Bacikab6e24102010-03-19 14:38:13 +00003586
3587 if (used + bytes > data_sinfo->total_bytes) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003588 struct btrfs_trans_handle *trans;
3589
Josef Bacik6a632092009-02-20 11:00:09 -05003590 /*
3591 * if we don't have enough free bytes in this space then we need
3592 * to alloc a new chunk.
3593 */
Josef Bacik0af3d002010-06-21 14:48:16 -04003594 if (!data_sinfo->full && alloc_chunk) {
Josef Bacik6a632092009-02-20 11:00:09 -05003595 u64 alloc_target;
Josef Bacik6a632092009-02-20 11:00:09 -05003596
Chris Mason0e4f8f82011-04-15 16:05:44 -04003597 data_sinfo->force_alloc = CHUNK_ALLOC_FORCE;
Josef Bacik6a632092009-02-20 11:00:09 -05003598 spin_unlock(&data_sinfo->lock);
Chris Mason33b4d472009-09-22 14:45:50 -04003599alloc:
Josef Bacik6a632092009-02-20 11:00:09 -05003600 alloc_target = btrfs_get_alloc_profile(root, 1);
Josef Bacik7a7eaa402011-04-13 12:54:33 -04003601 trans = btrfs_join_transaction(root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003602 if (IS_ERR(trans))
3603 return PTR_ERR(trans);
Josef Bacik6a632092009-02-20 11:00:09 -05003604
3605 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0e4f8f82011-04-15 16:05:44 -04003606 alloc_target,
3607 CHUNK_ALLOC_NO_FORCE);
Josef Bacik6a632092009-02-20 11:00:09 -05003608 btrfs_end_transaction(trans, root);
Miao Xied52a5b52011-01-05 10:07:18 +00003609 if (ret < 0) {
3610 if (ret != -ENOSPC)
3611 return ret;
3612 else
3613 goto commit_trans;
3614 }
Chris Mason33b4d472009-09-22 14:45:50 -04003615
Li Zefanb4d7c3c2012-07-09 20:21:07 -06003616 if (!data_sinfo)
3617 data_sinfo = fs_info->data_sinfo;
3618
Josef Bacik6a632092009-02-20 11:00:09 -05003619 goto again;
3620 }
Josef Bacikf2bb8f52011-05-25 13:10:16 -04003621
3622 /*
Josef Bacikb150a4f2013-06-19 15:00:04 -04003623 * If we don't have enough pinned space to deal with this
3624 * allocation don't bother committing the transaction.
Josef Bacikf2bb8f52011-05-25 13:10:16 -04003625 */
Josef Bacikb150a4f2013-06-19 15:00:04 -04003626 if (percpu_counter_compare(&data_sinfo->total_bytes_pinned,
3627 bytes) < 0)
Josef Bacikf2bb8f52011-05-25 13:10:16 -04003628 committed = 1;
Josef Bacik6a632092009-02-20 11:00:09 -05003629 spin_unlock(&data_sinfo->lock);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003630
3631 /* commit the current transaction and try again */
Miao Xied52a5b52011-01-05 10:07:18 +00003632commit_trans:
Josef Bacika4abeea2011-04-11 17:25:13 -04003633 if (!committed &&
3634 !atomic_read(&root->fs_info->open_ioctl_trans)) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003635 committed = 1;
Josef Bacikb150a4f2013-06-19 15:00:04 -04003636
Josef Bacik7a7eaa402011-04-13 12:54:33 -04003637 trans = btrfs_join_transaction(root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003638 if (IS_ERR(trans))
3639 return PTR_ERR(trans);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003640 ret = btrfs_commit_transaction(trans, root);
3641 if (ret)
3642 return ret;
3643 goto again;
3644 }
3645
Josef Bacik6a632092009-02-20 11:00:09 -05003646 return -ENOSPC;
3647 }
3648 data_sinfo->bytes_may_use += bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05003649 trace_btrfs_space_reservation(root->fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04003650 data_sinfo->flags, bytes, 1);
Josef Bacik6a632092009-02-20 11:00:09 -05003651 spin_unlock(&data_sinfo->lock);
3652
Josef Bacik9ed74f22009-09-11 16:12:44 -04003653 return 0;
Josef Bacik6a632092009-02-20 11:00:09 -05003654}
3655
3656/*
Josef Bacikfb25e912011-07-26 17:00:46 -04003657 * Called if we need to clear a data reservation for this inode.
Josef Bacik6a632092009-02-20 11:00:09 -05003658 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003659void btrfs_free_reserved_data_space(struct inode *inode, u64 bytes)
Josef Bacik6a632092009-02-20 11:00:09 -05003660{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003661 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik6a632092009-02-20 11:00:09 -05003662 struct btrfs_space_info *data_sinfo;
3663
3664 /* make sure bytes are sectorsize aligned */
Qu Wenruofda28322013-02-26 08:10:22 +00003665 bytes = ALIGN(bytes, root->sectorsize);
Josef Bacik6a632092009-02-20 11:00:09 -05003666
Li Zefanb4d7c3c2012-07-09 20:21:07 -06003667 data_sinfo = root->fs_info->data_sinfo;
Josef Bacik6a632092009-02-20 11:00:09 -05003668 spin_lock(&data_sinfo->lock);
3669 data_sinfo->bytes_may_use -= bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05003670 trace_btrfs_space_reservation(root->fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04003671 data_sinfo->flags, bytes, 0);
Josef Bacik6a632092009-02-20 11:00:09 -05003672 spin_unlock(&data_sinfo->lock);
3673}
3674
Josef Bacik97e728d2009-04-21 17:40:57 -04003675static void force_metadata_allocation(struct btrfs_fs_info *info)
3676{
3677 struct list_head *head = &info->space_info;
3678 struct btrfs_space_info *found;
3679
3680 rcu_read_lock();
3681 list_for_each_entry_rcu(found, head, list) {
3682 if (found->flags & BTRFS_BLOCK_GROUP_METADATA)
Chris Mason0e4f8f82011-04-15 16:05:44 -04003683 found->force_alloc = CHUNK_ALLOC_FORCE;
Josef Bacik97e728d2009-04-21 17:40:57 -04003684 }
3685 rcu_read_unlock();
3686}
3687
Miao Xie3c76cd82013-04-25 10:12:38 +00003688static inline u64 calc_global_rsv_need_space(struct btrfs_block_rsv *global)
3689{
3690 return (global->size << 1);
3691}
3692
Chris Masone5bc2452010-10-26 13:37:56 -04003693static int should_alloc_chunk(struct btrfs_root *root,
Josef Bacik698d0082012-09-12 14:08:47 -04003694 struct btrfs_space_info *sinfo, int force)
Yan, Zheng424499d2010-05-16 10:46:25 -04003695{
Josef Bacikfb25e912011-07-26 17:00:46 -04003696 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
Yan, Zheng424499d2010-05-16 10:46:25 -04003697 u64 num_bytes = sinfo->total_bytes - sinfo->bytes_readonly;
Chris Mason0e4f8f82011-04-15 16:05:44 -04003698 u64 num_allocated = sinfo->bytes_used + sinfo->bytes_reserved;
Chris Masone5bc2452010-10-26 13:37:56 -04003699 u64 thresh;
Yan, Zheng424499d2010-05-16 10:46:25 -04003700
Chris Mason0e4f8f82011-04-15 16:05:44 -04003701 if (force == CHUNK_ALLOC_FORCE)
3702 return 1;
3703
3704 /*
Josef Bacikfb25e912011-07-26 17:00:46 -04003705 * We need to take into account the global rsv because for all intents
3706 * and purposes it's used space. Don't worry about locking the
3707 * global_rsv, it doesn't change except when the transaction commits.
3708 */
Josef Bacik54338b52012-08-14 16:20:52 -04003709 if (sinfo->flags & BTRFS_BLOCK_GROUP_METADATA)
Miao Xie3c76cd82013-04-25 10:12:38 +00003710 num_allocated += calc_global_rsv_need_space(global_rsv);
Josef Bacikfb25e912011-07-26 17:00:46 -04003711
3712 /*
Chris Mason0e4f8f82011-04-15 16:05:44 -04003713 * in limited mode, we want to have some free space up to
3714 * about 1% of the FS size.
3715 */
3716 if (force == CHUNK_ALLOC_LIMITED) {
David Sterba6c417612011-04-13 15:41:04 +02003717 thresh = btrfs_super_total_bytes(root->fs_info->super_copy);
Chris Mason0e4f8f82011-04-15 16:05:44 -04003718 thresh = max_t(u64, 64 * 1024 * 1024,
3719 div_factor_fine(thresh, 1));
3720
3721 if (num_bytes - num_allocated < thresh)
3722 return 1;
3723 }
Chris Mason0e4f8f82011-04-15 16:05:44 -04003724
Josef Bacik698d0082012-09-12 14:08:47 -04003725 if (num_allocated + 2 * 1024 * 1024 < div_factor(num_bytes, 8))
Josef Bacik14ed0ca2010-10-15 15:23:48 -04003726 return 0;
Yan, Zheng424499d2010-05-16 10:46:25 -04003727 return 1;
3728}
3729
Liu Bo15d1ff82012-03-29 09:57:44 -04003730static u64 get_system_chunk_thresh(struct btrfs_root *root, u64 type)
3731{
3732 u64 num_dev;
3733
David Woodhouse53b381b2013-01-29 18:40:14 -05003734 if (type & (BTRFS_BLOCK_GROUP_RAID10 |
3735 BTRFS_BLOCK_GROUP_RAID0 |
3736 BTRFS_BLOCK_GROUP_RAID5 |
3737 BTRFS_BLOCK_GROUP_RAID6))
Liu Bo15d1ff82012-03-29 09:57:44 -04003738 num_dev = root->fs_info->fs_devices->rw_devices;
3739 else if (type & BTRFS_BLOCK_GROUP_RAID1)
3740 num_dev = 2;
3741 else
3742 num_dev = 1; /* DUP or single */
3743
3744 /* metadata for updaing devices and chunk tree */
3745 return btrfs_calc_trans_metadata_size(root, num_dev + 1);
3746}
3747
3748static void check_system_chunk(struct btrfs_trans_handle *trans,
3749 struct btrfs_root *root, u64 type)
3750{
3751 struct btrfs_space_info *info;
3752 u64 left;
3753 u64 thresh;
3754
3755 info = __find_space_info(root->fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
3756 spin_lock(&info->lock);
3757 left = info->total_bytes - info->bytes_used - info->bytes_pinned -
3758 info->bytes_reserved - info->bytes_readonly;
3759 spin_unlock(&info->lock);
3760
3761 thresh = get_system_chunk_thresh(root, type);
3762 if (left < thresh && btrfs_test_opt(root, ENOSPC_DEBUG)) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003763 btrfs_info(root->fs_info, "left=%llu, need=%llu, flags=%llu",
3764 left, thresh, type);
Liu Bo15d1ff82012-03-29 09:57:44 -04003765 dump_space_info(info, 0, 0);
3766 }
3767
3768 if (left < thresh) {
3769 u64 flags;
3770
3771 flags = btrfs_get_alloc_profile(root->fs_info->chunk_root, 0);
3772 btrfs_alloc_chunk(trans, root, flags);
3773 }
3774}
3775
Chris Mason6324fbf2008-03-24 15:01:59 -04003776static int do_chunk_alloc(struct btrfs_trans_handle *trans,
Josef Bacik698d0082012-09-12 14:08:47 -04003777 struct btrfs_root *extent_root, u64 flags, int force)
Chris Mason6324fbf2008-03-24 15:01:59 -04003778{
3779 struct btrfs_space_info *space_info;
Josef Bacik97e728d2009-04-21 17:40:57 -04003780 struct btrfs_fs_info *fs_info = extent_root->fs_info;
Josef Bacik6d741192011-04-11 20:20:11 -04003781 int wait_for_alloc = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05003782 int ret = 0;
3783
Josef Bacikc6b305a2012-12-18 09:16:16 -05003784 /* Don't re-enter if we're already allocating a chunk */
3785 if (trans->allocating_chunk)
3786 return -ENOSPC;
3787
Chris Mason6324fbf2008-03-24 15:01:59 -04003788 space_info = __find_space_info(extent_root->fs_info, flags);
Chris Mason593060d2008-03-25 16:50:33 -04003789 if (!space_info) {
3790 ret = update_space_info(extent_root->fs_info, flags,
3791 0, 0, &space_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003792 BUG_ON(ret); /* -ENOMEM */
Chris Mason593060d2008-03-25 16:50:33 -04003793 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003794 BUG_ON(!space_info); /* Logic error */
Chris Mason6324fbf2008-03-24 15:01:59 -04003795
Josef Bacik6d741192011-04-11 20:20:11 -04003796again:
Josef Bacik25179202008-10-29 14:49:05 -04003797 spin_lock(&space_info->lock);
Miao Xie9e622d62012-01-26 15:01:12 -05003798 if (force < space_info->force_alloc)
Chris Mason0e4f8f82011-04-15 16:05:44 -04003799 force = space_info->force_alloc;
Josef Bacik25179202008-10-29 14:49:05 -04003800 if (space_info->full) {
3801 spin_unlock(&space_info->lock);
Josef Bacik6d741192011-04-11 20:20:11 -04003802 return 0;
Josef Bacik25179202008-10-29 14:49:05 -04003803 }
Chris Mason6324fbf2008-03-24 15:01:59 -04003804
Josef Bacik698d0082012-09-12 14:08:47 -04003805 if (!should_alloc_chunk(extent_root, space_info, force)) {
Josef Bacik25179202008-10-29 14:49:05 -04003806 spin_unlock(&space_info->lock);
Josef Bacik6d741192011-04-11 20:20:11 -04003807 return 0;
3808 } else if (space_info->chunk_alloc) {
3809 wait_for_alloc = 1;
3810 } else {
3811 space_info->chunk_alloc = 1;
Josef Bacik25179202008-10-29 14:49:05 -04003812 }
Chris Mason0e4f8f82011-04-15 16:05:44 -04003813
Josef Bacik25179202008-10-29 14:49:05 -04003814 spin_unlock(&space_info->lock);
3815
Josef Bacik6d741192011-04-11 20:20:11 -04003816 mutex_lock(&fs_info->chunk_mutex);
3817
3818 /*
3819 * The chunk_mutex is held throughout the entirety of a chunk
3820 * allocation, so once we've acquired the chunk_mutex we know that the
3821 * other guy is done and we need to recheck and see if we should
3822 * allocate.
3823 */
3824 if (wait_for_alloc) {
3825 mutex_unlock(&fs_info->chunk_mutex);
3826 wait_for_alloc = 0;
3827 goto again;
3828 }
3829
Josef Bacikc6b305a2012-12-18 09:16:16 -05003830 trans->allocating_chunk = true;
3831
Josef Bacik97e728d2009-04-21 17:40:57 -04003832 /*
Josef Bacik67377732010-09-16 16:19:09 -04003833 * If we have mixed data/metadata chunks we want to make sure we keep
3834 * allocating mixed chunks instead of individual chunks.
3835 */
3836 if (btrfs_mixed_space_info(space_info))
3837 flags |= (BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA);
3838
3839 /*
Josef Bacik97e728d2009-04-21 17:40:57 -04003840 * if we're doing a data chunk, go ahead and make sure that
3841 * we keep a reasonable number of metadata chunks allocated in the
3842 * FS as well.
3843 */
Josef Bacik9ed74f22009-09-11 16:12:44 -04003844 if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) {
Josef Bacik97e728d2009-04-21 17:40:57 -04003845 fs_info->data_chunk_allocations++;
3846 if (!(fs_info->data_chunk_allocations %
3847 fs_info->metadata_ratio))
3848 force_metadata_allocation(fs_info);
3849 }
3850
Liu Bo15d1ff82012-03-29 09:57:44 -04003851 /*
3852 * Check if we have enough space in SYSTEM chunk because we may need
3853 * to update devices.
3854 */
3855 check_system_chunk(trans, extent_root, flags);
3856
Yan Zheng2b820322008-11-17 21:11:30 -05003857 ret = btrfs_alloc_chunk(trans, extent_root, flags);
Josef Bacikc6b305a2012-12-18 09:16:16 -05003858 trans->allocating_chunk = false;
Mark Fasheh92b8e892011-07-12 10:57:59 -07003859
Josef Bacik9ed74f22009-09-11 16:12:44 -04003860 spin_lock(&space_info->lock);
Alexandre Olivaa81cb9a2013-02-21 21:15:14 +00003861 if (ret < 0 && ret != -ENOSPC)
3862 goto out;
Chris Masond3977122009-01-05 21:25:51 -05003863 if (ret)
Chris Mason6324fbf2008-03-24 15:01:59 -04003864 space_info->full = 1;
Yan, Zheng424499d2010-05-16 10:46:25 -04003865 else
3866 ret = 1;
Josef Bacik6d741192011-04-11 20:20:11 -04003867
Chris Mason0e4f8f82011-04-15 16:05:44 -04003868 space_info->force_alloc = CHUNK_ALLOC_NO_FORCE;
Alexandre Olivaa81cb9a2013-02-21 21:15:14 +00003869out:
Josef Bacik6d741192011-04-11 20:20:11 -04003870 space_info->chunk_alloc = 0;
Josef Bacik9ed74f22009-09-11 16:12:44 -04003871 spin_unlock(&space_info->lock);
Dan Carpentera25c75d2012-04-18 09:59:29 +03003872 mutex_unlock(&fs_info->chunk_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003873 return ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04003874}
3875
Josef Bacika80c8dc2012-09-06 16:59:33 -04003876static int can_overcommit(struct btrfs_root *root,
3877 struct btrfs_space_info *space_info, u64 bytes,
Miao Xie08e007d2012-10-16 11:33:38 +00003878 enum btrfs_reserve_flush_enum flush)
Josef Bacika80c8dc2012-09-06 16:59:33 -04003879{
Josef Bacik96f1bb52013-01-30 17:02:51 -05003880 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
Josef Bacika80c8dc2012-09-06 16:59:33 -04003881 u64 profile = btrfs_get_alloc_profile(root, 0);
Miao Xie3c76cd82013-04-25 10:12:38 +00003882 u64 space_size;
Josef Bacika80c8dc2012-09-06 16:59:33 -04003883 u64 avail;
3884 u64 used;
Josef Bacik70afa392013-02-06 13:53:19 -05003885 u64 to_add;
Josef Bacika80c8dc2012-09-06 16:59:33 -04003886
3887 used = space_info->bytes_used + space_info->bytes_reserved +
Josef Bacik96f1bb52013-01-30 17:02:51 -05003888 space_info->bytes_pinned + space_info->bytes_readonly;
3889
Josef Bacik96f1bb52013-01-30 17:02:51 -05003890 /*
3891 * We only want to allow over committing if we have lots of actual space
3892 * free, but if we don't have enough space to handle the global reserve
3893 * space then we could end up having a real enospc problem when trying
3894 * to allocate a chunk or some other such important allocation.
3895 */
Miao Xie3c76cd82013-04-25 10:12:38 +00003896 spin_lock(&global_rsv->lock);
3897 space_size = calc_global_rsv_need_space(global_rsv);
3898 spin_unlock(&global_rsv->lock);
3899 if (used + space_size >= space_info->total_bytes)
Josef Bacik96f1bb52013-01-30 17:02:51 -05003900 return 0;
3901
3902 used += space_info->bytes_may_use;
Josef Bacika80c8dc2012-09-06 16:59:33 -04003903
3904 spin_lock(&root->fs_info->free_chunk_lock);
3905 avail = root->fs_info->free_chunk_space;
3906 spin_unlock(&root->fs_info->free_chunk_lock);
3907
3908 /*
3909 * If we have dup, raid1 or raid10 then only half of the free
David Woodhouse53b381b2013-01-29 18:40:14 -05003910 * space is actually useable. For raid56, the space info used
3911 * doesn't include the parity drive, so we don't have to
3912 * change the math
Josef Bacika80c8dc2012-09-06 16:59:33 -04003913 */
3914 if (profile & (BTRFS_BLOCK_GROUP_DUP |
3915 BTRFS_BLOCK_GROUP_RAID1 |
3916 BTRFS_BLOCK_GROUP_RAID10))
3917 avail >>= 1;
3918
Josef Bacik70afa392013-02-06 13:53:19 -05003919 to_add = space_info->total_bytes;
3920
Josef Bacika80c8dc2012-09-06 16:59:33 -04003921 /*
Miao Xie561c2942012-10-16 11:32:18 +00003922 * If we aren't flushing all things, let us overcommit up to
3923 * 1/2th of the space. If we can flush, don't let us overcommit
3924 * too much, let it overcommit up to 1/8 of the space.
Josef Bacika80c8dc2012-09-06 16:59:33 -04003925 */
Miao Xie08e007d2012-10-16 11:33:38 +00003926 if (flush == BTRFS_RESERVE_FLUSH_ALL)
Josef Bacik70afa392013-02-06 13:53:19 -05003927 to_add >>= 3;
Josef Bacika80c8dc2012-09-06 16:59:33 -04003928 else
Josef Bacik70afa392013-02-06 13:53:19 -05003929 to_add >>= 1;
Josef Bacika80c8dc2012-09-06 16:59:33 -04003930
Josef Bacik70afa392013-02-06 13:53:19 -05003931 /*
3932 * Limit the overcommit to the amount of free space we could possibly
3933 * allocate for chunks.
3934 */
3935 to_add = min(avail, to_add);
3936
3937 if (used + bytes < space_info->total_bytes + to_add)
Josef Bacika80c8dc2012-09-06 16:59:33 -04003938 return 1;
3939 return 0;
3940}
3941
Eric Sandeen48a3b632013-04-25 20:41:01 +00003942static void btrfs_writeback_inodes_sb_nr(struct btrfs_root *root,
3943 unsigned long nr_pages)
Miao Xieda633a42012-12-20 11:19:09 +00003944{
3945 struct super_block *sb = root->fs_info->sb;
Miao Xieda633a42012-12-20 11:19:09 +00003946
Josef Bacik925a6ef2013-06-20 12:31:27 -04003947 if (down_read_trylock(&sb->s_umount)) {
3948 writeback_inodes_sb_nr(sb, nr_pages, WB_REASON_FS_FREE_SPACE);
3949 up_read(&sb->s_umount);
3950 } else {
Miao Xieda633a42012-12-20 11:19:09 +00003951 /*
3952 * We needn't worry the filesystem going from r/w to r/o though
3953 * we don't acquire ->s_umount mutex, because the filesystem
3954 * should guarantee the delalloc inodes list be empty after
3955 * the filesystem is readonly(all dirty pages are written to
3956 * the disk).
3957 */
Miao Xieeb73c1b2013-05-15 07:48:22 +00003958 btrfs_start_all_delalloc_inodes(root->fs_info, 0);
Josef Bacik98ad69c2013-04-04 11:55:49 -04003959 if (!current->journal_info)
Miao Xie199c2a92013-05-15 07:48:23 +00003960 btrfs_wait_all_ordered_extents(root->fs_info, 0);
Miao Xieda633a42012-12-20 11:19:09 +00003961 }
3962}
3963
Yan, Zheng5da9d012010-05-16 10:46:25 -04003964/*
3965 * shrink metadata reservation for delalloc
3966 */
Josef Bacikf4c738c2012-07-02 17:10:51 -04003967static void shrink_delalloc(struct btrfs_root *root, u64 to_reclaim, u64 orig,
3968 bool wait_ordered)
Yan, Zheng5da9d012010-05-16 10:46:25 -04003969{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003970 struct btrfs_block_rsv *block_rsv;
Josef Bacik0019f102010-10-15 15:18:40 -04003971 struct btrfs_space_info *space_info;
Josef Bacik663350a2011-11-03 22:54:25 -04003972 struct btrfs_trans_handle *trans;
Josef Bacikf4c738c2012-07-02 17:10:51 -04003973 u64 delalloc_bytes;
Yan, Zheng5da9d012010-05-16 10:46:25 -04003974 u64 max_reclaim;
Josef Bacikb1953bc2011-01-21 21:10:01 +00003975 long time_left;
Josef Bacik877da172011-10-14 14:02:10 -04003976 unsigned long nr_pages = (2 * 1024 * 1024) >> PAGE_CACHE_SHIFT;
Josef Bacikb1953bc2011-01-21 21:10:01 +00003977 int loops = 0;
Miao Xie08e007d2012-10-16 11:33:38 +00003978 enum btrfs_reserve_flush_enum flush;
Yan, Zheng5da9d012010-05-16 10:46:25 -04003979
Josef Bacik663350a2011-11-03 22:54:25 -04003980 trans = (struct btrfs_trans_handle *)current->journal_info;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003981 block_rsv = &root->fs_info->delalloc_block_rsv;
Josef Bacik0019f102010-10-15 15:18:40 -04003982 space_info = block_rsv->space_info;
Chris Masonbf9022e2010-10-26 13:40:45 -04003983
3984 smp_mb();
Miao Xie963d6782013-01-29 10:10:51 +00003985 delalloc_bytes = percpu_counter_sum_positive(
3986 &root->fs_info->delalloc_bytes);
Josef Bacikf4c738c2012-07-02 17:10:51 -04003987 if (delalloc_bytes == 0) {
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003988 if (trans)
Josef Bacikf4c738c2012-07-02 17:10:51 -04003989 return;
Miao Xie199c2a92013-05-15 07:48:23 +00003990 btrfs_wait_all_ordered_extents(root->fs_info, 0);
Josef Bacikf4c738c2012-07-02 17:10:51 -04003991 return;
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003992 }
3993
Josef Bacikf4c738c2012-07-02 17:10:51 -04003994 while (delalloc_bytes && loops < 3) {
3995 max_reclaim = min(delalloc_bytes, to_reclaim);
3996 nr_pages = max_reclaim >> PAGE_CACHE_SHIFT;
Miao Xieda633a42012-12-20 11:19:09 +00003997 btrfs_writeback_inodes_sb_nr(root, nr_pages);
Josef Bacikdea31f52012-09-06 16:47:00 -04003998 /*
3999 * We need to wait for the async pages to actually start before
4000 * we do anything.
4001 */
4002 wait_event(root->fs_info->async_submit_wait,
4003 !atomic_read(&root->fs_info->async_delalloc_pages));
4004
Miao Xie08e007d2012-10-16 11:33:38 +00004005 if (!trans)
4006 flush = BTRFS_RESERVE_FLUSH_ALL;
4007 else
4008 flush = BTRFS_RESERVE_NO_FLUSH;
Josef Bacik0019f102010-10-15 15:18:40 -04004009 spin_lock(&space_info->lock);
Miao Xie08e007d2012-10-16 11:33:38 +00004010 if (can_overcommit(root, space_info, orig, flush)) {
Josef Bacikf4c738c2012-07-02 17:10:51 -04004011 spin_unlock(&space_info->lock);
4012 break;
4013 }
Josef Bacik0019f102010-10-15 15:18:40 -04004014 spin_unlock(&space_info->lock);
Yan, Zheng5da9d012010-05-16 10:46:25 -04004015
Chris Mason36e39c42011-03-12 07:08:42 -05004016 loops++;
Josef Bacikf104d042011-10-14 13:56:58 -04004017 if (wait_ordered && !trans) {
Miao Xie199c2a92013-05-15 07:48:23 +00004018 btrfs_wait_all_ordered_extents(root->fs_info, 0);
Josef Bacikf104d042011-10-14 13:56:58 -04004019 } else {
Josef Bacikf4c738c2012-07-02 17:10:51 -04004020 time_left = schedule_timeout_killable(1);
Josef Bacikf104d042011-10-14 13:56:58 -04004021 if (time_left)
4022 break;
4023 }
Josef Bacikf4c738c2012-07-02 17:10:51 -04004024 smp_mb();
Miao Xie963d6782013-01-29 10:10:51 +00004025 delalloc_bytes = percpu_counter_sum_positive(
4026 &root->fs_info->delalloc_bytes);
Yan, Zheng5da9d012010-05-16 10:46:25 -04004027 }
Yan, Zheng5da9d012010-05-16 10:46:25 -04004028}
4029
Josef Bacik4a92b1b2011-08-30 12:34:28 -04004030/**
Josef Bacik663350a2011-11-03 22:54:25 -04004031 * maybe_commit_transaction - possibly commit the transaction if its ok to
4032 * @root - the root we're allocating for
4033 * @bytes - the number of bytes we want to reserve
4034 * @force - force the commit
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004035 *
Josef Bacik663350a2011-11-03 22:54:25 -04004036 * This will check to make sure that committing the transaction will actually
4037 * get us somewhere and then commit the transaction if it does. Otherwise it
4038 * will return -ENOSPC.
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004039 */
Josef Bacik663350a2011-11-03 22:54:25 -04004040static int may_commit_transaction(struct btrfs_root *root,
4041 struct btrfs_space_info *space_info,
4042 u64 bytes, int force)
4043{
4044 struct btrfs_block_rsv *delayed_rsv = &root->fs_info->delayed_block_rsv;
4045 struct btrfs_trans_handle *trans;
4046
4047 trans = (struct btrfs_trans_handle *)current->journal_info;
4048 if (trans)
4049 return -EAGAIN;
4050
4051 if (force)
4052 goto commit;
4053
4054 /* See if there is enough pinned space to make this reservation */
4055 spin_lock(&space_info->lock);
Josef Bacikb150a4f2013-06-19 15:00:04 -04004056 if (percpu_counter_compare(&space_info->total_bytes_pinned,
4057 bytes) >= 0) {
Josef Bacik663350a2011-11-03 22:54:25 -04004058 spin_unlock(&space_info->lock);
4059 goto commit;
4060 }
4061 spin_unlock(&space_info->lock);
4062
4063 /*
4064 * See if there is some space in the delayed insertion reservation for
4065 * this reservation.
4066 */
4067 if (space_info != delayed_rsv->space_info)
4068 return -ENOSPC;
4069
Liu Bod9b02182012-02-16 18:34:39 +08004070 spin_lock(&space_info->lock);
Josef Bacik663350a2011-11-03 22:54:25 -04004071 spin_lock(&delayed_rsv->lock);
Josef Bacikb150a4f2013-06-19 15:00:04 -04004072 if (percpu_counter_compare(&space_info->total_bytes_pinned,
4073 bytes - delayed_rsv->size) >= 0) {
Josef Bacik663350a2011-11-03 22:54:25 -04004074 spin_unlock(&delayed_rsv->lock);
Liu Bod9b02182012-02-16 18:34:39 +08004075 spin_unlock(&space_info->lock);
Josef Bacik663350a2011-11-03 22:54:25 -04004076 return -ENOSPC;
4077 }
4078 spin_unlock(&delayed_rsv->lock);
Liu Bod9b02182012-02-16 18:34:39 +08004079 spin_unlock(&space_info->lock);
Josef Bacik663350a2011-11-03 22:54:25 -04004080
4081commit:
4082 trans = btrfs_join_transaction(root);
4083 if (IS_ERR(trans))
4084 return -ENOSPC;
4085
4086 return btrfs_commit_transaction(trans, root);
4087}
4088
Josef Bacik96c3f432012-06-21 14:05:49 -04004089enum flush_state {
Josef Bacik67b0fd62012-09-24 13:42:00 -04004090 FLUSH_DELAYED_ITEMS_NR = 1,
4091 FLUSH_DELAYED_ITEMS = 2,
4092 FLUSH_DELALLOC = 3,
4093 FLUSH_DELALLOC_WAIT = 4,
Josef Bacikea658ba2012-09-11 16:57:25 -04004094 ALLOC_CHUNK = 5,
4095 COMMIT_TRANS = 6,
Josef Bacik96c3f432012-06-21 14:05:49 -04004096};
4097
4098static int flush_space(struct btrfs_root *root,
4099 struct btrfs_space_info *space_info, u64 num_bytes,
4100 u64 orig_bytes, int state)
4101{
4102 struct btrfs_trans_handle *trans;
4103 int nr;
Josef Bacikf4c738c2012-07-02 17:10:51 -04004104 int ret = 0;
Josef Bacik96c3f432012-06-21 14:05:49 -04004105
4106 switch (state) {
Josef Bacik96c3f432012-06-21 14:05:49 -04004107 case FLUSH_DELAYED_ITEMS_NR:
4108 case FLUSH_DELAYED_ITEMS:
4109 if (state == FLUSH_DELAYED_ITEMS_NR) {
4110 u64 bytes = btrfs_calc_trans_metadata_size(root, 1);
4111
4112 nr = (int)div64_u64(num_bytes, bytes);
4113 if (!nr)
4114 nr = 1;
4115 nr *= 2;
4116 } else {
4117 nr = -1;
4118 }
4119 trans = btrfs_join_transaction(root);
4120 if (IS_ERR(trans)) {
4121 ret = PTR_ERR(trans);
4122 break;
4123 }
4124 ret = btrfs_run_delayed_items_nr(trans, root, nr);
4125 btrfs_end_transaction(trans, root);
4126 break;
Josef Bacik67b0fd62012-09-24 13:42:00 -04004127 case FLUSH_DELALLOC:
4128 case FLUSH_DELALLOC_WAIT:
4129 shrink_delalloc(root, num_bytes, orig_bytes,
4130 state == FLUSH_DELALLOC_WAIT);
4131 break;
Josef Bacikea658ba2012-09-11 16:57:25 -04004132 case ALLOC_CHUNK:
4133 trans = btrfs_join_transaction(root);
4134 if (IS_ERR(trans)) {
4135 ret = PTR_ERR(trans);
4136 break;
4137 }
4138 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Josef Bacikea658ba2012-09-11 16:57:25 -04004139 btrfs_get_alloc_profile(root, 0),
4140 CHUNK_ALLOC_NO_FORCE);
4141 btrfs_end_transaction(trans, root);
4142 if (ret == -ENOSPC)
4143 ret = 0;
4144 break;
Josef Bacik96c3f432012-06-21 14:05:49 -04004145 case COMMIT_TRANS:
4146 ret = may_commit_transaction(root, space_info, orig_bytes, 0);
4147 break;
4148 default:
4149 ret = -ENOSPC;
4150 break;
4151 }
4152
4153 return ret;
4154}
Josef Bacik663350a2011-11-03 22:54:25 -04004155/**
Josef Bacik4a92b1b2011-08-30 12:34:28 -04004156 * reserve_metadata_bytes - try to reserve bytes from the block_rsv's space
4157 * @root - the root we're allocating for
4158 * @block_rsv - the block_rsv we're allocating for
4159 * @orig_bytes - the number of bytes we want
Adam Buchbinder48fc7f72012-09-19 21:48:00 -04004160 * @flush - whether or not we can flush to make our reservation
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004161 *
Josef Bacik4a92b1b2011-08-30 12:34:28 -04004162 * This will reserve orgi_bytes number of bytes from the space info associated
4163 * with the block_rsv. If there is not enough space it will make an attempt to
4164 * flush out space to make room. It will do this by flushing delalloc if
4165 * possible or committing the transaction. If flush is 0 then no attempts to
4166 * regain reservations will be made and this will fail if there is not enough
4167 * space already.
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004168 */
Josef Bacik4a92b1b2011-08-30 12:34:28 -04004169static int reserve_metadata_bytes(struct btrfs_root *root,
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004170 struct btrfs_block_rsv *block_rsv,
Miao Xie08e007d2012-10-16 11:33:38 +00004171 u64 orig_bytes,
4172 enum btrfs_reserve_flush_enum flush)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004173{
4174 struct btrfs_space_info *space_info = block_rsv->space_info;
Josef Bacik2bf64752011-09-26 17:12:22 -04004175 u64 used;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004176 u64 num_bytes = orig_bytes;
Josef Bacik67b0fd62012-09-24 13:42:00 -04004177 int flush_state = FLUSH_DELAYED_ITEMS_NR;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004178 int ret = 0;
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004179 bool flushing = false;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004180
4181again:
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004182 ret = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004183 spin_lock(&space_info->lock);
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004184 /*
Miao Xie08e007d2012-10-16 11:33:38 +00004185 * We only want to wait if somebody other than us is flushing and we
4186 * are actually allowed to flush all things.
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004187 */
Miao Xie08e007d2012-10-16 11:33:38 +00004188 while (flush == BTRFS_RESERVE_FLUSH_ALL && !flushing &&
4189 space_info->flush) {
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004190 spin_unlock(&space_info->lock);
4191 /*
4192 * If we have a trans handle we can't wait because the flusher
4193 * may have to commit the transaction, which would mean we would
4194 * deadlock since we are waiting for the flusher to finish, but
4195 * hold the current transaction open.
4196 */
Josef Bacik663350a2011-11-03 22:54:25 -04004197 if (current->journal_info)
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004198 return -EAGAIN;
Arne Jansenb9688bb2012-04-18 10:27:16 +02004199 ret = wait_event_killable(space_info->wait, !space_info->flush);
4200 /* Must have been killed, return */
4201 if (ret)
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004202 return -EINTR;
4203
4204 spin_lock(&space_info->lock);
4205 }
4206
4207 ret = -ENOSPC;
Josef Bacik2bf64752011-09-26 17:12:22 -04004208 used = space_info->bytes_used + space_info->bytes_reserved +
4209 space_info->bytes_pinned + space_info->bytes_readonly +
4210 space_info->bytes_may_use;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004211
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004212 /*
4213 * The idea here is that we've not already over-reserved the block group
4214 * then we can go ahead and save our reservation first and then start
4215 * flushing if we need to. Otherwise if we've already overcommitted
4216 * lets start flushing stuff first and then come back and try to make
4217 * our reservation.
4218 */
Josef Bacik2bf64752011-09-26 17:12:22 -04004219 if (used <= space_info->total_bytes) {
4220 if (used + orig_bytes <= space_info->total_bytes) {
Josef Bacikfb25e912011-07-26 17:00:46 -04004221 space_info->bytes_may_use += orig_bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004222 trace_btrfs_space_reservation(root->fs_info,
Liu Bo2bcc0322012-03-29 09:57:44 -04004223 "space_info", space_info->flags, orig_bytes, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004224 ret = 0;
4225 } else {
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004226 /*
4227 * Ok set num_bytes to orig_bytes since we aren't
4228 * overocmmitted, this way we only try and reclaim what
4229 * we need.
4230 */
4231 num_bytes = orig_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004232 }
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004233 } else {
4234 /*
4235 * Ok we're over committed, set num_bytes to the overcommitted
4236 * amount plus the amount of bytes that we need for this
4237 * reservation.
4238 */
Josef Bacik2bf64752011-09-26 17:12:22 -04004239 num_bytes = used - space_info->total_bytes +
Josef Bacik96c3f432012-06-21 14:05:49 -04004240 (orig_bytes * 2);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004241 }
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004242
Josef Bacik44734ed2012-09-28 16:04:19 -04004243 if (ret && can_overcommit(root, space_info, orig_bytes, flush)) {
4244 space_info->bytes_may_use += orig_bytes;
4245 trace_btrfs_space_reservation(root->fs_info, "space_info",
4246 space_info->flags, orig_bytes,
4247 1);
4248 ret = 0;
Josef Bacik2bf64752011-09-26 17:12:22 -04004249 }
4250
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004251 /*
4252 * Couldn't make our reservation, save our place so while we're trying
4253 * to reclaim space we can actually use it instead of somebody else
4254 * stealing it from us.
Miao Xie08e007d2012-10-16 11:33:38 +00004255 *
4256 * We make the other tasks wait for the flush only when we can flush
4257 * all things.
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004258 */
Josef Bacik72bcd992012-12-18 15:16:34 -05004259 if (ret && flush != BTRFS_RESERVE_NO_FLUSH) {
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004260 flushing = true;
4261 space_info->flush = 1;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004262 }
4263
Yan, Zhengf0486c62010-05-16 10:46:25 -04004264 spin_unlock(&space_info->lock);
4265
Miao Xie08e007d2012-10-16 11:33:38 +00004266 if (!ret || flush == BTRFS_RESERVE_NO_FLUSH)
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004267 goto out;
4268
Josef Bacik96c3f432012-06-21 14:05:49 -04004269 ret = flush_space(root, space_info, num_bytes, orig_bytes,
4270 flush_state);
4271 flush_state++;
Miao Xie08e007d2012-10-16 11:33:38 +00004272
4273 /*
4274 * If we are FLUSH_LIMIT, we can not flush delalloc, or the deadlock
4275 * would happen. So skip delalloc flush.
4276 */
4277 if (flush == BTRFS_RESERVE_FLUSH_LIMIT &&
4278 (flush_state == FLUSH_DELALLOC ||
4279 flush_state == FLUSH_DELALLOC_WAIT))
4280 flush_state = ALLOC_CHUNK;
4281
Josef Bacik96c3f432012-06-21 14:05:49 -04004282 if (!ret)
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004283 goto again;
Miao Xie08e007d2012-10-16 11:33:38 +00004284 else if (flush == BTRFS_RESERVE_FLUSH_LIMIT &&
4285 flush_state < COMMIT_TRANS)
4286 goto again;
4287 else if (flush == BTRFS_RESERVE_FLUSH_ALL &&
4288 flush_state <= COMMIT_TRANS)
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004289 goto again;
4290
4291out:
Josef Bacik5d803662013-02-07 16:06:02 -05004292 if (ret == -ENOSPC &&
4293 unlikely(root->orphan_cleanup_state == ORPHAN_CLEANUP_STARTED)) {
4294 struct btrfs_block_rsv *global_rsv =
4295 &root->fs_info->global_block_rsv;
4296
4297 if (block_rsv != global_rsv &&
4298 !block_rsv_use_bytes(global_rsv, orig_bytes))
4299 ret = 0;
4300 }
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004301 if (flushing) {
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004302 spin_lock(&space_info->lock);
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004303 space_info->flush = 0;
4304 wake_up_all(&space_info->wait);
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004305 spin_unlock(&space_info->lock);
4306 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04004307 return ret;
4308}
4309
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004310static struct btrfs_block_rsv *get_block_rsv(
4311 const struct btrfs_trans_handle *trans,
4312 const struct btrfs_root *root)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004313{
Josef Bacik4c13d752011-08-30 11:31:29 -04004314 struct btrfs_block_rsv *block_rsv = NULL;
4315
Josef Bacik0e721102012-06-26 16:13:18 -04004316 if (root->ref_cows)
4317 block_rsv = trans->block_rsv;
4318
4319 if (root == root->fs_info->csum_root && trans->adding_csums)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004320 block_rsv = trans->block_rsv;
Josef Bacik4c13d752011-08-30 11:31:29 -04004321
4322 if (!block_rsv)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004323 block_rsv = root->block_rsv;
4324
4325 if (!block_rsv)
4326 block_rsv = &root->fs_info->empty_block_rsv;
4327
4328 return block_rsv;
4329}
4330
4331static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv,
4332 u64 num_bytes)
4333{
4334 int ret = -ENOSPC;
4335 spin_lock(&block_rsv->lock);
4336 if (block_rsv->reserved >= num_bytes) {
4337 block_rsv->reserved -= num_bytes;
4338 if (block_rsv->reserved < block_rsv->size)
4339 block_rsv->full = 0;
4340 ret = 0;
4341 }
4342 spin_unlock(&block_rsv->lock);
4343 return ret;
4344}
4345
4346static void block_rsv_add_bytes(struct btrfs_block_rsv *block_rsv,
4347 u64 num_bytes, int update_size)
4348{
4349 spin_lock(&block_rsv->lock);
4350 block_rsv->reserved += num_bytes;
4351 if (update_size)
4352 block_rsv->size += num_bytes;
4353 else if (block_rsv->reserved >= block_rsv->size)
4354 block_rsv->full = 1;
4355 spin_unlock(&block_rsv->lock);
4356}
4357
Josef Bacikd52be812013-05-29 14:54:47 -04004358int btrfs_cond_migrate_bytes(struct btrfs_fs_info *fs_info,
4359 struct btrfs_block_rsv *dest, u64 num_bytes,
4360 int min_factor)
4361{
4362 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
4363 u64 min_bytes;
4364
4365 if (global_rsv->space_info != dest->space_info)
4366 return -ENOSPC;
4367
4368 spin_lock(&global_rsv->lock);
4369 min_bytes = div_factor(global_rsv->size, min_factor);
4370 if (global_rsv->reserved < min_bytes + num_bytes) {
4371 spin_unlock(&global_rsv->lock);
4372 return -ENOSPC;
4373 }
4374 global_rsv->reserved -= num_bytes;
4375 if (global_rsv->reserved < global_rsv->size)
4376 global_rsv->full = 0;
4377 spin_unlock(&global_rsv->lock);
4378
4379 block_rsv_add_bytes(dest, num_bytes, 1);
4380 return 0;
4381}
4382
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004383static void block_rsv_release_bytes(struct btrfs_fs_info *fs_info,
4384 struct btrfs_block_rsv *block_rsv,
David Sterba62a45b62011-04-20 15:52:26 +02004385 struct btrfs_block_rsv *dest, u64 num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004386{
4387 struct btrfs_space_info *space_info = block_rsv->space_info;
4388
4389 spin_lock(&block_rsv->lock);
4390 if (num_bytes == (u64)-1)
4391 num_bytes = block_rsv->size;
4392 block_rsv->size -= num_bytes;
4393 if (block_rsv->reserved >= block_rsv->size) {
4394 num_bytes = block_rsv->reserved - block_rsv->size;
4395 block_rsv->reserved = block_rsv->size;
4396 block_rsv->full = 1;
4397 } else {
4398 num_bytes = 0;
4399 }
4400 spin_unlock(&block_rsv->lock);
4401
4402 if (num_bytes > 0) {
4403 if (dest) {
Josef Bacike9e22892011-01-24 21:43:19 +00004404 spin_lock(&dest->lock);
4405 if (!dest->full) {
4406 u64 bytes_to_add;
4407
4408 bytes_to_add = dest->size - dest->reserved;
4409 bytes_to_add = min(num_bytes, bytes_to_add);
4410 dest->reserved += bytes_to_add;
4411 if (dest->reserved >= dest->size)
4412 dest->full = 1;
4413 num_bytes -= bytes_to_add;
4414 }
4415 spin_unlock(&dest->lock);
4416 }
4417 if (num_bytes) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04004418 spin_lock(&space_info->lock);
Josef Bacikfb25e912011-07-26 17:00:46 -04004419 space_info->bytes_may_use -= num_bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004420 trace_btrfs_space_reservation(fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04004421 space_info->flags, num_bytes, 0);
Chris Mason36e39c42011-03-12 07:08:42 -05004422 space_info->reservation_progress++;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004423 spin_unlock(&space_info->lock);
4424 }
4425 }
4426}
4427
4428static int block_rsv_migrate_bytes(struct btrfs_block_rsv *src,
4429 struct btrfs_block_rsv *dst, u64 num_bytes)
4430{
4431 int ret;
4432
4433 ret = block_rsv_use_bytes(src, num_bytes);
4434 if (ret)
4435 return ret;
4436
4437 block_rsv_add_bytes(dst, num_bytes, 1);
4438 return 0;
4439}
4440
Miao Xie66d8f3d2012-09-06 04:02:28 -06004441void btrfs_init_block_rsv(struct btrfs_block_rsv *rsv, unsigned short type)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004442{
4443 memset(rsv, 0, sizeof(*rsv));
4444 spin_lock_init(&rsv->lock);
Miao Xie66d8f3d2012-09-06 04:02:28 -06004445 rsv->type = type;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004446}
4447
Miao Xie66d8f3d2012-09-06 04:02:28 -06004448struct btrfs_block_rsv *btrfs_alloc_block_rsv(struct btrfs_root *root,
4449 unsigned short type)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004450{
4451 struct btrfs_block_rsv *block_rsv;
4452 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004453
4454 block_rsv = kmalloc(sizeof(*block_rsv), GFP_NOFS);
4455 if (!block_rsv)
4456 return NULL;
4457
Miao Xie66d8f3d2012-09-06 04:02:28 -06004458 btrfs_init_block_rsv(block_rsv, type);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004459 block_rsv->space_info = __find_space_info(fs_info,
4460 BTRFS_BLOCK_GROUP_METADATA);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004461 return block_rsv;
4462}
4463
4464void btrfs_free_block_rsv(struct btrfs_root *root,
4465 struct btrfs_block_rsv *rsv)
4466{
Josef Bacik2aaa6652012-08-29 14:27:18 -04004467 if (!rsv)
4468 return;
Josef Bacikdabdb642011-08-08 12:50:18 -04004469 btrfs_block_rsv_release(root, rsv, (u64)-1);
4470 kfree(rsv);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004471}
4472
Miao Xie08e007d2012-10-16 11:33:38 +00004473int btrfs_block_rsv_add(struct btrfs_root *root,
4474 struct btrfs_block_rsv *block_rsv, u64 num_bytes,
4475 enum btrfs_reserve_flush_enum flush)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004476{
4477 int ret;
4478
4479 if (num_bytes == 0)
4480 return 0;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004481
Miao Xie61b520a2011-11-10 20:45:05 -05004482 ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004483 if (!ret) {
4484 block_rsv_add_bytes(block_rsv, num_bytes, 1);
4485 return 0;
4486 }
4487
Yan, Zhengf0486c62010-05-16 10:46:25 -04004488 return ret;
4489}
4490
Josef Bacik4a92b1b2011-08-30 12:34:28 -04004491int btrfs_block_rsv_check(struct btrfs_root *root,
Josef Bacik36ba0222011-10-18 12:15:48 -04004492 struct btrfs_block_rsv *block_rsv, int min_factor)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004493{
4494 u64 num_bytes = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004495 int ret = -ENOSPC;
4496
4497 if (!block_rsv)
4498 return 0;
4499
4500 spin_lock(&block_rsv->lock);
Josef Bacik36ba0222011-10-18 12:15:48 -04004501 num_bytes = div_factor(block_rsv->size, min_factor);
4502 if (block_rsv->reserved >= num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004503 ret = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004504 spin_unlock(&block_rsv->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004505
Josef Bacik36ba0222011-10-18 12:15:48 -04004506 return ret;
4507}
4508
Miao Xie08e007d2012-10-16 11:33:38 +00004509int btrfs_block_rsv_refill(struct btrfs_root *root,
4510 struct btrfs_block_rsv *block_rsv, u64 min_reserved,
4511 enum btrfs_reserve_flush_enum flush)
Josef Bacik36ba0222011-10-18 12:15:48 -04004512{
4513 u64 num_bytes = 0;
4514 int ret = -ENOSPC;
4515
4516 if (!block_rsv)
4517 return 0;
4518
4519 spin_lock(&block_rsv->lock);
4520 num_bytes = min_reserved;
Josef Bacik13553e52011-08-08 13:33:21 -04004521 if (block_rsv->reserved >= num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004522 ret = 0;
Josef Bacik13553e52011-08-08 13:33:21 -04004523 else
Yan, Zhengf0486c62010-05-16 10:46:25 -04004524 num_bytes -= block_rsv->reserved;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004525 spin_unlock(&block_rsv->lock);
Josef Bacik13553e52011-08-08 13:33:21 -04004526
Yan, Zhengf0486c62010-05-16 10:46:25 -04004527 if (!ret)
4528 return 0;
4529
Miao Xieaa38a712011-11-18 17:43:00 +08004530 ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush);
Josef Bacikdabdb642011-08-08 12:50:18 -04004531 if (!ret) {
4532 block_rsv_add_bytes(block_rsv, num_bytes, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004533 return 0;
4534 }
4535
Josef Bacik13553e52011-08-08 13:33:21 -04004536 return ret;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004537}
4538
4539int btrfs_block_rsv_migrate(struct btrfs_block_rsv *src_rsv,
4540 struct btrfs_block_rsv *dst_rsv,
4541 u64 num_bytes)
4542{
4543 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
4544}
4545
4546void btrfs_block_rsv_release(struct btrfs_root *root,
4547 struct btrfs_block_rsv *block_rsv,
4548 u64 num_bytes)
4549{
4550 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
4551 if (global_rsv->full || global_rsv == block_rsv ||
4552 block_rsv->space_info != global_rsv->space_info)
4553 global_rsv = NULL;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004554 block_rsv_release_bytes(root->fs_info, block_rsv, global_rsv,
4555 num_bytes);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004556}
4557
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004558/*
4559 * helper to calculate size of global block reservation.
4560 * the desired value is sum of space used by extent tree,
4561 * checksum tree and root tree
4562 */
4563static u64 calc_global_metadata_size(struct btrfs_fs_info *fs_info)
4564{
4565 struct btrfs_space_info *sinfo;
4566 u64 num_bytes;
4567 u64 meta_used;
4568 u64 data_used;
David Sterba6c417612011-04-13 15:41:04 +02004569 int csum_size = btrfs_super_csum_size(fs_info->super_copy);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004570
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004571 sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_DATA);
4572 spin_lock(&sinfo->lock);
4573 data_used = sinfo->bytes_used;
4574 spin_unlock(&sinfo->lock);
4575
4576 sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
4577 spin_lock(&sinfo->lock);
Josef Bacik6d487552010-10-15 15:13:32 -04004578 if (sinfo->flags & BTRFS_BLOCK_GROUP_DATA)
4579 data_used = 0;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004580 meta_used = sinfo->bytes_used;
4581 spin_unlock(&sinfo->lock);
4582
4583 num_bytes = (data_used >> fs_info->sb->s_blocksize_bits) *
4584 csum_size * 2;
4585 num_bytes += div64_u64(data_used + meta_used, 50);
4586
4587 if (num_bytes * 3 > meta_used)
Chris Mason8e62c2d2012-04-12 13:46:48 -04004588 num_bytes = div64_u64(meta_used, 3);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004589
4590 return ALIGN(num_bytes, fs_info->extent_root->leafsize << 10);
4591}
4592
4593static void update_global_block_rsv(struct btrfs_fs_info *fs_info)
4594{
4595 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
4596 struct btrfs_space_info *sinfo = block_rsv->space_info;
4597 u64 num_bytes;
4598
4599 num_bytes = calc_global_metadata_size(fs_info);
4600
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004601 spin_lock(&sinfo->lock);
Stefan Behrens1f699d32012-04-27 12:41:46 -04004602 spin_lock(&block_rsv->lock);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004603
Josef Bacikfdf30d12013-03-26 15:31:45 -04004604 block_rsv->size = min_t(u64, num_bytes, 512 * 1024 * 1024);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004605
4606 num_bytes = sinfo->bytes_used + sinfo->bytes_pinned +
Josef Bacik6d487552010-10-15 15:13:32 -04004607 sinfo->bytes_reserved + sinfo->bytes_readonly +
4608 sinfo->bytes_may_use;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004609
4610 if (sinfo->total_bytes > num_bytes) {
4611 num_bytes = sinfo->total_bytes - num_bytes;
4612 block_rsv->reserved += num_bytes;
Josef Bacikfb25e912011-07-26 17:00:46 -04004613 sinfo->bytes_may_use += num_bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004614 trace_btrfs_space_reservation(fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04004615 sinfo->flags, num_bytes, 1);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004616 }
4617
4618 if (block_rsv->reserved >= block_rsv->size) {
4619 num_bytes = block_rsv->reserved - block_rsv->size;
Josef Bacikfb25e912011-07-26 17:00:46 -04004620 sinfo->bytes_may_use -= num_bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004621 trace_btrfs_space_reservation(fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04004622 sinfo->flags, num_bytes, 0);
Chris Mason36e39c42011-03-12 07:08:42 -05004623 sinfo->reservation_progress++;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004624 block_rsv->reserved = block_rsv->size;
4625 block_rsv->full = 1;
4626 }
David Sterba182608c2011-05-05 13:13:16 +02004627
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004628 spin_unlock(&block_rsv->lock);
Stefan Behrens1f699d32012-04-27 12:41:46 -04004629 spin_unlock(&sinfo->lock);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004630}
4631
Yan, Zhengf0486c62010-05-16 10:46:25 -04004632static void init_global_block_rsv(struct btrfs_fs_info *fs_info)
4633{
4634 struct btrfs_space_info *space_info;
4635
4636 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
4637 fs_info->chunk_block_rsv.space_info = space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004638
4639 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004640 fs_info->global_block_rsv.space_info = space_info;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004641 fs_info->delalloc_block_rsv.space_info = space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004642 fs_info->trans_block_rsv.space_info = space_info;
4643 fs_info->empty_block_rsv.space_info = space_info;
Josef Bacik6d668dd2011-11-03 22:54:25 -04004644 fs_info->delayed_block_rsv.space_info = space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004645
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004646 fs_info->extent_root->block_rsv = &fs_info->global_block_rsv;
4647 fs_info->csum_root->block_rsv = &fs_info->global_block_rsv;
4648 fs_info->dev_root->block_rsv = &fs_info->global_block_rsv;
4649 fs_info->tree_root->block_rsv = &fs_info->global_block_rsv;
Stefan Behrens3a6cad92013-05-16 14:48:19 +00004650 if (fs_info->quota_root)
4651 fs_info->quota_root->block_rsv = &fs_info->global_block_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004652 fs_info->chunk_root->block_rsv = &fs_info->chunk_block_rsv;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004653
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004654 update_global_block_rsv(fs_info);
4655}
4656
4657static void release_global_block_rsv(struct btrfs_fs_info *fs_info)
4658{
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004659 block_rsv_release_bytes(fs_info, &fs_info->global_block_rsv, NULL,
4660 (u64)-1);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004661 WARN_ON(fs_info->delalloc_block_rsv.size > 0);
4662 WARN_ON(fs_info->delalloc_block_rsv.reserved > 0);
4663 WARN_ON(fs_info->trans_block_rsv.size > 0);
4664 WARN_ON(fs_info->trans_block_rsv.reserved > 0);
4665 WARN_ON(fs_info->chunk_block_rsv.size > 0);
4666 WARN_ON(fs_info->chunk_block_rsv.reserved > 0);
Josef Bacik6d668dd2011-11-03 22:54:25 -04004667 WARN_ON(fs_info->delayed_block_rsv.size > 0);
4668 WARN_ON(fs_info->delayed_block_rsv.reserved > 0);
Josef Bacikfcb80c22011-05-03 10:40:22 -04004669}
4670
Yan, Zhenga22285a2010-05-16 10:48:46 -04004671void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans,
4672 struct btrfs_root *root)
4673{
Josef Bacik0e721102012-06-26 16:13:18 -04004674 if (!trans->block_rsv)
4675 return;
4676
Yan, Zhenga22285a2010-05-16 10:48:46 -04004677 if (!trans->bytes_reserved)
4678 return;
4679
Chris Masone77266e2012-02-24 10:39:05 -05004680 trace_btrfs_space_reservation(root->fs_info, "transaction",
Liu Bo2bcc0322012-03-29 09:57:44 -04004681 trans->transid, trans->bytes_reserved, 0);
Josef Bacikb24e03d2011-10-14 14:40:17 -04004682 btrfs_block_rsv_release(root, trans->block_rsv, trans->bytes_reserved);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004683 trans->bytes_reserved = 0;
4684}
4685
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004686/* Can only return 0 or -ENOSPC */
Yan, Zhengd68fc572010-05-16 10:49:58 -04004687int btrfs_orphan_reserve_metadata(struct btrfs_trans_handle *trans,
4688 struct inode *inode)
4689{
4690 struct btrfs_root *root = BTRFS_I(inode)->root;
4691 struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root);
4692 struct btrfs_block_rsv *dst_rsv = root->orphan_block_rsv;
4693
4694 /*
Josef Bacikfcb80c22011-05-03 10:40:22 -04004695 * We need to hold space in order to delete our orphan item once we've
4696 * added it, so this takes the reservation so we can release it later
4697 * when we are truly done with the orphan item.
Yan, Zhengd68fc572010-05-16 10:49:58 -04004698 */
Chris Masonff5714c2011-05-28 07:00:39 -04004699 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 1);
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004700 trace_btrfs_space_reservation(root->fs_info, "orphan",
4701 btrfs_ino(inode), num_bytes, 1);
Yan, Zhengd68fc572010-05-16 10:49:58 -04004702 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
4703}
4704
4705void btrfs_orphan_release_metadata(struct inode *inode)
4706{
4707 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonff5714c2011-05-28 07:00:39 -04004708 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 1);
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004709 trace_btrfs_space_reservation(root->fs_info, "orphan",
4710 btrfs_ino(inode), num_bytes, 0);
Yan, Zhengd68fc572010-05-16 10:49:58 -04004711 btrfs_block_rsv_release(root, root->orphan_block_rsv, num_bytes);
4712}
4713
Miao Xied5c12072013-02-28 10:04:33 +00004714/*
4715 * btrfs_subvolume_reserve_metadata() - reserve space for subvolume operation
4716 * root: the root of the parent directory
4717 * rsv: block reservation
4718 * items: the number of items that we need do reservation
4719 * qgroup_reserved: used to return the reserved size in qgroup
4720 *
4721 * This function is used to reserve the space for snapshot/subvolume
4722 * creation and deletion. Those operations are different with the
4723 * common file/directory operations, they change two fs/file trees
4724 * and root tree, the number of items that the qgroup reserves is
4725 * different with the free space reservation. So we can not use
4726 * the space reseravtion mechanism in start_transaction().
4727 */
4728int btrfs_subvolume_reserve_metadata(struct btrfs_root *root,
4729 struct btrfs_block_rsv *rsv,
4730 int items,
4731 u64 *qgroup_reserved)
Yan, Zhenga22285a2010-05-16 10:48:46 -04004732{
Miao Xied5c12072013-02-28 10:04:33 +00004733 u64 num_bytes;
4734 int ret;
4735
4736 if (root->fs_info->quota_enabled) {
4737 /* One for parent inode, two for dir entries */
4738 num_bytes = 3 * root->leafsize;
4739 ret = btrfs_qgroup_reserve(root, num_bytes);
4740 if (ret)
4741 return ret;
4742 } else {
4743 num_bytes = 0;
4744 }
4745
4746 *qgroup_reserved = num_bytes;
4747
4748 num_bytes = btrfs_calc_trans_metadata_size(root, items);
4749 rsv->space_info = __find_space_info(root->fs_info,
4750 BTRFS_BLOCK_GROUP_METADATA);
4751 ret = btrfs_block_rsv_add(root, rsv, num_bytes,
4752 BTRFS_RESERVE_FLUSH_ALL);
4753 if (ret) {
4754 if (*qgroup_reserved)
4755 btrfs_qgroup_free(root, *qgroup_reserved);
4756 }
4757
4758 return ret;
4759}
4760
4761void btrfs_subvolume_release_metadata(struct btrfs_root *root,
4762 struct btrfs_block_rsv *rsv,
4763 u64 qgroup_reserved)
4764{
4765 btrfs_block_rsv_release(root, rsv, (u64)-1);
4766 if (qgroup_reserved)
4767 btrfs_qgroup_free(root, qgroup_reserved);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004768}
4769
Josef Bacik7709cde2011-08-04 10:25:02 -04004770/**
4771 * drop_outstanding_extent - drop an outstanding extent
4772 * @inode: the inode we're dropping the extent for
4773 *
4774 * This is called when we are freeing up an outstanding extent, either called
4775 * after an error or after an extent is written. This will return the number of
4776 * reserved extents that need to be freed. This must be called with
4777 * BTRFS_I(inode)->lock held.
4778 */
Josef Bacik9e0baf62011-07-15 15:16:44 +00004779static unsigned drop_outstanding_extent(struct inode *inode)
4780{
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004781 unsigned drop_inode_space = 0;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004782 unsigned dropped_extents = 0;
4783
Josef Bacik9e0baf62011-07-15 15:16:44 +00004784 BUG_ON(!BTRFS_I(inode)->outstanding_extents);
4785 BTRFS_I(inode)->outstanding_extents--;
4786
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004787 if (BTRFS_I(inode)->outstanding_extents == 0 &&
Josef Bacik72ac3c02012-05-23 14:13:11 -04004788 test_and_clear_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
4789 &BTRFS_I(inode)->runtime_flags))
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004790 drop_inode_space = 1;
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004791
Josef Bacik9e0baf62011-07-15 15:16:44 +00004792 /*
4793 * If we have more or the same amount of outsanding extents than we have
4794 * reserved then we need to leave the reserved extents count alone.
4795 */
4796 if (BTRFS_I(inode)->outstanding_extents >=
4797 BTRFS_I(inode)->reserved_extents)
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004798 return drop_inode_space;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004799
4800 dropped_extents = BTRFS_I(inode)->reserved_extents -
4801 BTRFS_I(inode)->outstanding_extents;
4802 BTRFS_I(inode)->reserved_extents -= dropped_extents;
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004803 return dropped_extents + drop_inode_space;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004804}
4805
Josef Bacik7709cde2011-08-04 10:25:02 -04004806/**
4807 * calc_csum_metadata_size - return the amount of metada space that must be
4808 * reserved/free'd for the given bytes.
4809 * @inode: the inode we're manipulating
4810 * @num_bytes: the number of bytes in question
4811 * @reserve: 1 if we are reserving space, 0 if we are freeing space
4812 *
4813 * This adjusts the number of csum_bytes in the inode and then returns the
4814 * correct amount of metadata that must either be reserved or freed. We
4815 * calculate how many checksums we can fit into one leaf and then divide the
4816 * number of bytes that will need to be checksumed by this value to figure out
4817 * how many checksums will be required. If we are adding bytes then the number
4818 * may go up and we will return the number of additional bytes that must be
4819 * reserved. If it is going down we will return the number of bytes that must
4820 * be freed.
4821 *
4822 * This must be called with BTRFS_I(inode)->lock held.
4823 */
4824static u64 calc_csum_metadata_size(struct inode *inode, u64 num_bytes,
4825 int reserve)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004826{
Josef Bacik7709cde2011-08-04 10:25:02 -04004827 struct btrfs_root *root = BTRFS_I(inode)->root;
4828 u64 csum_size;
4829 int num_csums_per_leaf;
4830 int num_csums;
4831 int old_csums;
4832
4833 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM &&
4834 BTRFS_I(inode)->csum_bytes == 0)
4835 return 0;
4836
4837 old_csums = (int)div64_u64(BTRFS_I(inode)->csum_bytes, root->sectorsize);
4838 if (reserve)
4839 BTRFS_I(inode)->csum_bytes += num_bytes;
4840 else
4841 BTRFS_I(inode)->csum_bytes -= num_bytes;
4842 csum_size = BTRFS_LEAF_DATA_SIZE(root) - sizeof(struct btrfs_item);
4843 num_csums_per_leaf = (int)div64_u64(csum_size,
4844 sizeof(struct btrfs_csum_item) +
4845 sizeof(struct btrfs_disk_key));
4846 num_csums = (int)div64_u64(BTRFS_I(inode)->csum_bytes, root->sectorsize);
4847 num_csums = num_csums + num_csums_per_leaf - 1;
4848 num_csums = num_csums / num_csums_per_leaf;
4849
4850 old_csums = old_csums + num_csums_per_leaf - 1;
4851 old_csums = old_csums / num_csums_per_leaf;
4852
4853 /* No change, no need to reserve more */
4854 if (old_csums == num_csums)
4855 return 0;
4856
4857 if (reserve)
4858 return btrfs_calc_trans_metadata_size(root,
4859 num_csums - old_csums);
4860
4861 return btrfs_calc_trans_metadata_size(root, old_csums - num_csums);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004862}
4863
4864int btrfs_delalloc_reserve_metadata(struct inode *inode, u64 num_bytes)
4865{
4866 struct btrfs_root *root = BTRFS_I(inode)->root;
4867 struct btrfs_block_rsv *block_rsv = &root->fs_info->delalloc_block_rsv;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004868 u64 to_reserve = 0;
Josef Bacik660d3f62011-12-09 11:18:51 -05004869 u64 csum_bytes;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004870 unsigned nr_extents = 0;
Josef Bacik660d3f62011-12-09 11:18:51 -05004871 int extra_reserve = 0;
Miao Xie08e007d2012-10-16 11:33:38 +00004872 enum btrfs_reserve_flush_enum flush = BTRFS_RESERVE_FLUSH_ALL;
Jan Schmidteb6b88d2013-01-27 23:26:00 -07004873 int ret = 0;
Josef Bacikc64c2bd2012-12-14 13:48:14 -05004874 bool delalloc_lock = true;
Wang Shilong88e081bf2013-03-01 11:36:01 +00004875 u64 to_free = 0;
4876 unsigned dropped;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004877
Josef Bacikc64c2bd2012-12-14 13:48:14 -05004878 /* If we are a free space inode we need to not flush since we will be in
4879 * the middle of a transaction commit. We also don't need the delalloc
4880 * mutex since we won't race with anybody. We need this mostly to make
4881 * lockdep shut its filthy mouth.
4882 */
4883 if (btrfs_is_free_space_inode(inode)) {
Miao Xie08e007d2012-10-16 11:33:38 +00004884 flush = BTRFS_RESERVE_NO_FLUSH;
Josef Bacikc64c2bd2012-12-14 13:48:14 -05004885 delalloc_lock = false;
4886 }
Josef Bacikc09544e2011-08-30 10:19:10 -04004887
Miao Xie08e007d2012-10-16 11:33:38 +00004888 if (flush != BTRFS_RESERVE_NO_FLUSH &&
4889 btrfs_transaction_in_commit(root->fs_info))
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004890 schedule_timeout(1);
4891
Josef Bacikc64c2bd2012-12-14 13:48:14 -05004892 if (delalloc_lock)
4893 mutex_lock(&BTRFS_I(inode)->delalloc_mutex);
4894
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004895 num_bytes = ALIGN(num_bytes, root->sectorsize);
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004896
Josef Bacik9e0baf62011-07-15 15:16:44 +00004897 spin_lock(&BTRFS_I(inode)->lock);
4898 BTRFS_I(inode)->outstanding_extents++;
Josef Bacik57a45ced2011-01-25 16:30:38 -05004899
Josef Bacik9e0baf62011-07-15 15:16:44 +00004900 if (BTRFS_I(inode)->outstanding_extents >
Josef Bacik660d3f62011-12-09 11:18:51 -05004901 BTRFS_I(inode)->reserved_extents)
Josef Bacik9e0baf62011-07-15 15:16:44 +00004902 nr_extents = BTRFS_I(inode)->outstanding_extents -
4903 BTRFS_I(inode)->reserved_extents;
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004904
4905 /*
4906 * Add an item to reserve for updating the inode when we complete the
4907 * delalloc io.
4908 */
Josef Bacik72ac3c02012-05-23 14:13:11 -04004909 if (!test_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
4910 &BTRFS_I(inode)->runtime_flags)) {
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004911 nr_extents++;
Josef Bacik660d3f62011-12-09 11:18:51 -05004912 extra_reserve = 1;
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004913 }
4914
4915 to_reserve = btrfs_calc_trans_metadata_size(root, nr_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04004916 to_reserve += calc_csum_metadata_size(inode, num_bytes, 1);
Josef Bacik660d3f62011-12-09 11:18:51 -05004917 csum_bytes = BTRFS_I(inode)->csum_bytes;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004918 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik57a45ced2011-01-25 16:30:38 -05004919
Wang Shilong88e081bf2013-03-01 11:36:01 +00004920 if (root->fs_info->quota_enabled) {
Arne Jansenc5567232011-09-14 15:44:05 +02004921 ret = btrfs_qgroup_reserve(root, num_bytes +
4922 nr_extents * root->leafsize);
Wang Shilong88e081bf2013-03-01 11:36:01 +00004923 if (ret)
4924 goto out_fail;
Wang Shilonga9870c02013-03-01 11:33:01 +00004925 }
Arne Jansenc5567232011-09-14 15:44:05 +02004926
Wang Shilong88e081bf2013-03-01 11:36:01 +00004927 ret = reserve_metadata_bytes(root, block_rsv, to_reserve, flush);
4928 if (unlikely(ret)) {
4929 if (root->fs_info->quota_enabled)
Miao Xie4b5829a2012-12-05 10:53:25 +00004930 btrfs_qgroup_free(root, num_bytes +
4931 nr_extents * root->leafsize);
Wang Shilong88e081bf2013-03-01 11:36:01 +00004932 goto out_fail;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004933 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004934
Josef Bacik660d3f62011-12-09 11:18:51 -05004935 spin_lock(&BTRFS_I(inode)->lock);
4936 if (extra_reserve) {
Josef Bacik72ac3c02012-05-23 14:13:11 -04004937 set_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
4938 &BTRFS_I(inode)->runtime_flags);
Josef Bacik660d3f62011-12-09 11:18:51 -05004939 nr_extents--;
4940 }
4941 BTRFS_I(inode)->reserved_extents += nr_extents;
4942 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacikc64c2bd2012-12-14 13:48:14 -05004943
4944 if (delalloc_lock)
4945 mutex_unlock(&BTRFS_I(inode)->delalloc_mutex);
Josef Bacik660d3f62011-12-09 11:18:51 -05004946
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004947 if (to_reserve)
4948 trace_btrfs_space_reservation(root->fs_info,"delalloc",
4949 btrfs_ino(inode), to_reserve, 1);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004950 block_rsv_add_bytes(block_rsv, to_reserve, 1);
4951
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004952 return 0;
Wang Shilong88e081bf2013-03-01 11:36:01 +00004953
4954out_fail:
4955 spin_lock(&BTRFS_I(inode)->lock);
4956 dropped = drop_outstanding_extent(inode);
4957 /*
4958 * If the inodes csum_bytes is the same as the original
4959 * csum_bytes then we know we haven't raced with any free()ers
4960 * so we can just reduce our inodes csum bytes and carry on.
Wang Shilong88e081bf2013-03-01 11:36:01 +00004961 */
Josef Bacikf4881bc2013-03-25 16:03:35 -04004962 if (BTRFS_I(inode)->csum_bytes == csum_bytes) {
Wang Shilong88e081bf2013-03-01 11:36:01 +00004963 calc_csum_metadata_size(inode, num_bytes, 0);
Josef Bacikf4881bc2013-03-25 16:03:35 -04004964 } else {
4965 u64 orig_csum_bytes = BTRFS_I(inode)->csum_bytes;
4966 u64 bytes;
4967
4968 /*
4969 * This is tricky, but first we need to figure out how much we
4970 * free'd from any free-ers that occured during this
4971 * reservation, so we reset ->csum_bytes to the csum_bytes
4972 * before we dropped our lock, and then call the free for the
4973 * number of bytes that were freed while we were trying our
4974 * reservation.
4975 */
4976 bytes = csum_bytes - BTRFS_I(inode)->csum_bytes;
4977 BTRFS_I(inode)->csum_bytes = csum_bytes;
4978 to_free = calc_csum_metadata_size(inode, bytes, 0);
4979
4980
4981 /*
4982 * Now we need to see how much we would have freed had we not
4983 * been making this reservation and our ->csum_bytes were not
4984 * artificially inflated.
4985 */
4986 BTRFS_I(inode)->csum_bytes = csum_bytes - num_bytes;
4987 bytes = csum_bytes - orig_csum_bytes;
4988 bytes = calc_csum_metadata_size(inode, bytes, 0);
4989
4990 /*
4991 * Now reset ->csum_bytes to what it should be. If bytes is
4992 * more than to_free then we would have free'd more space had we
4993 * not had an artificially high ->csum_bytes, so we need to free
4994 * the remainder. If bytes is the same or less then we don't
4995 * need to do anything, the other free-ers did the correct
4996 * thing.
4997 */
4998 BTRFS_I(inode)->csum_bytes = orig_csum_bytes - num_bytes;
4999 if (bytes > to_free)
5000 to_free = bytes - to_free;
5001 else
5002 to_free = 0;
5003 }
Wang Shilong88e081bf2013-03-01 11:36:01 +00005004 spin_unlock(&BTRFS_I(inode)->lock);
5005 if (dropped)
5006 to_free += btrfs_calc_trans_metadata_size(root, dropped);
5007
5008 if (to_free) {
5009 btrfs_block_rsv_release(root, block_rsv, to_free);
5010 trace_btrfs_space_reservation(root->fs_info, "delalloc",
5011 btrfs_ino(inode), to_free, 0);
5012 }
5013 if (delalloc_lock)
5014 mutex_unlock(&BTRFS_I(inode)->delalloc_mutex);
5015 return ret;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005016}
5017
Josef Bacik7709cde2011-08-04 10:25:02 -04005018/**
5019 * btrfs_delalloc_release_metadata - release a metadata reservation for an inode
5020 * @inode: the inode to release the reservation for
5021 * @num_bytes: the number of bytes we're releasing
5022 *
5023 * This will release the metadata reservation for an inode. This can be called
5024 * once we complete IO for a given set of bytes to release their metadata
5025 * reservations.
5026 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005027void btrfs_delalloc_release_metadata(struct inode *inode, u64 num_bytes)
5028{
5029 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik9e0baf62011-07-15 15:16:44 +00005030 u64 to_free = 0;
5031 unsigned dropped;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005032
5033 num_bytes = ALIGN(num_bytes, root->sectorsize);
Josef Bacik7709cde2011-08-04 10:25:02 -04005034 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik9e0baf62011-07-15 15:16:44 +00005035 dropped = drop_outstanding_extent(inode);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005036
Miao Xie09348562013-02-07 10:12:07 +00005037 if (num_bytes)
5038 to_free = calc_csum_metadata_size(inode, num_bytes, 0);
Josef Bacik7709cde2011-08-04 10:25:02 -04005039 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9e0baf62011-07-15 15:16:44 +00005040 if (dropped > 0)
5041 to_free += btrfs_calc_trans_metadata_size(root, dropped);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005042
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05005043 trace_btrfs_space_reservation(root->fs_info, "delalloc",
5044 btrfs_ino(inode), to_free, 0);
Arne Jansenc5567232011-09-14 15:44:05 +02005045 if (root->fs_info->quota_enabled) {
5046 btrfs_qgroup_free(root, num_bytes +
5047 dropped * root->leafsize);
5048 }
5049
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005050 btrfs_block_rsv_release(root, &root->fs_info->delalloc_block_rsv,
5051 to_free);
5052}
5053
Josef Bacik7709cde2011-08-04 10:25:02 -04005054/**
5055 * btrfs_delalloc_reserve_space - reserve data and metadata space for delalloc
5056 * @inode: inode we're writing to
5057 * @num_bytes: the number of bytes we want to allocate
5058 *
5059 * This will do the following things
5060 *
5061 * o reserve space in the data space info for num_bytes
5062 * o reserve space in the metadata space info based on number of outstanding
5063 * extents and how much csums will be needed
5064 * o add to the inodes ->delalloc_bytes
5065 * o add it to the fs_info's delalloc inodes list.
5066 *
5067 * This will return 0 for success and -ENOSPC if there is no space left.
5068 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005069int btrfs_delalloc_reserve_space(struct inode *inode, u64 num_bytes)
5070{
5071 int ret;
5072
5073 ret = btrfs_check_data_free_space(inode, num_bytes);
5074 if (ret)
5075 return ret;
5076
5077 ret = btrfs_delalloc_reserve_metadata(inode, num_bytes);
5078 if (ret) {
5079 btrfs_free_reserved_data_space(inode, num_bytes);
5080 return ret;
5081 }
5082
5083 return 0;
5084}
5085
Josef Bacik7709cde2011-08-04 10:25:02 -04005086/**
5087 * btrfs_delalloc_release_space - release data and metadata space for delalloc
5088 * @inode: inode we're releasing space for
5089 * @num_bytes: the number of bytes we want to free up
5090 *
5091 * This must be matched with a call to btrfs_delalloc_reserve_space. This is
5092 * called in the case that we don't need the metadata AND data reservations
5093 * anymore. So if there is an error or we insert an inline extent.
5094 *
5095 * This function will release the metadata space that was not used and will
5096 * decrement ->delalloc_bytes and remove it from the fs_info delalloc_inodes
5097 * list if there are no delalloc bytes left.
5098 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005099void btrfs_delalloc_release_space(struct inode *inode, u64 num_bytes)
5100{
5101 btrfs_delalloc_release_metadata(inode, num_bytes);
5102 btrfs_free_reserved_data_space(inode, num_bytes);
5103}
5104
Liu Boc53d6132012-12-27 09:01:19 +00005105static int update_block_group(struct btrfs_root *root,
Yan, Zhengf0486c62010-05-16 10:46:25 -04005106 u64 bytenr, u64 num_bytes, int alloc)
Chris Mason9078a3e2007-04-26 16:46:15 -04005107{
Josef Bacik0af3d002010-06-21 14:48:16 -04005108 struct btrfs_block_group_cache *cache = NULL;
Chris Mason9078a3e2007-04-26 16:46:15 -04005109 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -04005110 u64 total = num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04005111 u64 old_val;
Chris Masondb945352007-10-15 16:15:53 -04005112 u64 byte_in_group;
Josef Bacik0af3d002010-06-21 14:48:16 -04005113 int factor;
Chris Mason3e1ad542007-05-07 20:03:49 -04005114
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005115 /* block accounting for super block */
Miao Xieeb73c1b2013-05-15 07:48:22 +00005116 spin_lock(&info->delalloc_root_lock);
David Sterba6c417612011-04-13 15:41:04 +02005117 old_val = btrfs_super_bytes_used(info->super_copy);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005118 if (alloc)
5119 old_val += num_bytes;
5120 else
5121 old_val -= num_bytes;
David Sterba6c417612011-04-13 15:41:04 +02005122 btrfs_set_super_bytes_used(info->super_copy, old_val);
Miao Xieeb73c1b2013-05-15 07:48:22 +00005123 spin_unlock(&info->delalloc_root_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005124
Chris Masond3977122009-01-05 21:25:51 -05005125 while (total) {
Chris Masondb945352007-10-15 16:15:53 -04005126 cache = btrfs_lookup_block_group(info, bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05005127 if (!cache)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005128 return -ENOENT;
Yan, Zhengb742bb82010-05-16 10:46:24 -04005129 if (cache->flags & (BTRFS_BLOCK_GROUP_DUP |
5130 BTRFS_BLOCK_GROUP_RAID1 |
5131 BTRFS_BLOCK_GROUP_RAID10))
5132 factor = 2;
5133 else
5134 factor = 1;
Josef Bacik9d66e232010-08-25 16:54:15 -04005135 /*
5136 * If this block group has free space cache written out, we
5137 * need to make sure to load it if we are removing space. This
5138 * is because we need the unpinning stage to actually add the
5139 * space back to the block group, otherwise we will leak space.
5140 */
5141 if (!alloc && cache->cached == BTRFS_CACHE_NO)
Liu Bof6373bf2012-12-27 09:01:18 +00005142 cache_block_group(cache, 1);
Josef Bacik0af3d002010-06-21 14:48:16 -04005143
Chris Masondb945352007-10-15 16:15:53 -04005144 byte_in_group = bytenr - cache->key.objectid;
5145 WARN_ON(byte_in_group > cache->key.offset);
Chris Mason9078a3e2007-04-26 16:46:15 -04005146
Josef Bacik25179202008-10-29 14:49:05 -04005147 spin_lock(&cache->space_info->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04005148 spin_lock(&cache->lock);
Josef Bacik0af3d002010-06-21 14:48:16 -04005149
Josef Bacik73bc1872011-10-03 14:07:49 -04005150 if (btrfs_test_opt(root, SPACE_CACHE) &&
Josef Bacik0af3d002010-06-21 14:48:16 -04005151 cache->disk_cache_state < BTRFS_DC_CLEAR)
5152 cache->disk_cache_state = BTRFS_DC_CLEAR;
5153
Josef Bacik0f9dd462008-09-23 13:14:11 -04005154 cache->dirty = 1;
Chris Mason9078a3e2007-04-26 16:46:15 -04005155 old_val = btrfs_block_group_used(&cache->item);
Chris Masondb945352007-10-15 16:15:53 -04005156 num_bytes = min(total, cache->key.offset - byte_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -04005157 if (alloc) {
Chris Masondb945352007-10-15 16:15:53 -04005158 old_val += num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04005159 btrfs_set_block_group_used(&cache->item, old_val);
5160 cache->reserved -= num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04005161 cache->space_info->bytes_reserved -= num_bytes;
Yan, Zhengb742bb82010-05-16 10:46:24 -04005162 cache->space_info->bytes_used += num_bytes;
5163 cache->space_info->disk_used += num_bytes * factor;
Chris Masonc286ac42008-07-22 23:06:41 -04005164 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04005165 spin_unlock(&cache->space_info->lock);
Chris Masoncd1bc462007-04-27 10:08:34 -04005166 } else {
Chris Masondb945352007-10-15 16:15:53 -04005167 old_val -= num_bytes;
Chris Masonc286ac42008-07-22 23:06:41 -04005168 btrfs_set_block_group_used(&cache->item, old_val);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005169 cache->pinned += num_bytes;
5170 cache->space_info->bytes_pinned += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04005171 cache->space_info->bytes_used -= num_bytes;
Yan, Zhengb742bb82010-05-16 10:46:24 -04005172 cache->space_info->disk_used -= num_bytes * factor;
Chris Masonc286ac42008-07-22 23:06:41 -04005173 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04005174 spin_unlock(&cache->space_info->lock);
Liu Hui1f3c79a2009-01-05 15:57:51 -05005175
Yan, Zhengf0486c62010-05-16 10:46:25 -04005176 set_extent_dirty(info->pinned_extents,
5177 bytenr, bytenr + num_bytes - 1,
5178 GFP_NOFS | __GFP_NOFAIL);
Chris Masoncd1bc462007-04-27 10:08:34 -04005179 }
Chris Masonfa9c0d72009-04-03 09:47:43 -04005180 btrfs_put_block_group(cache);
Chris Masondb945352007-10-15 16:15:53 -04005181 total -= num_bytes;
5182 bytenr += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04005183 }
5184 return 0;
5185}
Chris Mason6324fbf2008-03-24 15:01:59 -04005186
Chris Masona061fc82008-05-07 11:43:44 -04005187static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
5188{
Josef Bacik0f9dd462008-09-23 13:14:11 -04005189 struct btrfs_block_group_cache *cache;
Yan Zhengd2fb3432008-12-11 16:30:39 -05005190 u64 bytenr;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005191
Liu Boa1897fd2012-12-27 09:01:23 +00005192 spin_lock(&root->fs_info->block_group_cache_lock);
5193 bytenr = root->fs_info->first_logical_byte;
5194 spin_unlock(&root->fs_info->block_group_cache_lock);
5195
5196 if (bytenr < (u64)-1)
5197 return bytenr;
5198
Josef Bacik0f9dd462008-09-23 13:14:11 -04005199 cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
5200 if (!cache)
Chris Masona061fc82008-05-07 11:43:44 -04005201 return 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005202
Yan Zhengd2fb3432008-12-11 16:30:39 -05005203 bytenr = cache->key.objectid;
Chris Masonfa9c0d72009-04-03 09:47:43 -04005204 btrfs_put_block_group(cache);
Yan Zhengd2fb3432008-12-11 16:30:39 -05005205
5206 return bytenr;
Chris Masona061fc82008-05-07 11:43:44 -04005207}
5208
Yan, Zhengf0486c62010-05-16 10:46:25 -04005209static int pin_down_extent(struct btrfs_root *root,
5210 struct btrfs_block_group_cache *cache,
5211 u64 bytenr, u64 num_bytes, int reserved)
Yan324ae4d2007-11-16 14:57:08 -05005212{
Yan Zheng11833d62009-09-11 16:11:19 -04005213 spin_lock(&cache->space_info->lock);
5214 spin_lock(&cache->lock);
5215 cache->pinned += num_bytes;
5216 cache->space_info->bytes_pinned += num_bytes;
5217 if (reserved) {
5218 cache->reserved -= num_bytes;
5219 cache->space_info->bytes_reserved -= num_bytes;
Yan324ae4d2007-11-16 14:57:08 -05005220 }
Yan Zheng11833d62009-09-11 16:11:19 -04005221 spin_unlock(&cache->lock);
5222 spin_unlock(&cache->space_info->lock);
5223
Yan, Zhengf0486c62010-05-16 10:46:25 -04005224 set_extent_dirty(root->fs_info->pinned_extents, bytenr,
5225 bytenr + num_bytes - 1, GFP_NOFS | __GFP_NOFAIL);
Yan324ae4d2007-11-16 14:57:08 -05005226 return 0;
5227}
Chris Mason9078a3e2007-04-26 16:46:15 -04005228
Yan, Zhengf0486c62010-05-16 10:46:25 -04005229/*
5230 * this function must be called within transaction
5231 */
5232int btrfs_pin_extent(struct btrfs_root *root,
5233 u64 bytenr, u64 num_bytes, int reserved)
Zheng Yane8569812008-09-26 10:05:48 -04005234{
Yan, Zhengf0486c62010-05-16 10:46:25 -04005235 struct btrfs_block_group_cache *cache;
5236
5237 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005238 BUG_ON(!cache); /* Logic error */
Yan, Zhengf0486c62010-05-16 10:46:25 -04005239
5240 pin_down_extent(root, cache, bytenr, num_bytes, reserved);
5241
5242 btrfs_put_block_group(cache);
Yan Zheng11833d62009-09-11 16:11:19 -04005243 return 0;
5244}
Zheng Yane8569812008-09-26 10:05:48 -04005245
Yan, Zhengf0486c62010-05-16 10:46:25 -04005246/*
Chris Masone688b722011-10-31 20:52:39 -04005247 * this function must be called within transaction
Yan, Zhengf0486c62010-05-16 10:46:25 -04005248 */
Liu Bodcfac412012-12-27 09:01:20 +00005249int btrfs_pin_extent_for_log_replay(struct btrfs_root *root,
Chris Masone688b722011-10-31 20:52:39 -04005250 u64 bytenr, u64 num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005251{
Chris Masone688b722011-10-31 20:52:39 -04005252 struct btrfs_block_group_cache *cache;
Josef Bacikb50c6e22013-04-25 15:55:30 -04005253 int ret;
Chris Masone688b722011-10-31 20:52:39 -04005254
5255 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
Josef Bacikb50c6e22013-04-25 15:55:30 -04005256 if (!cache)
5257 return -EINVAL;
Chris Masone688b722011-10-31 20:52:39 -04005258
5259 /*
5260 * pull in the free space cache (if any) so that our pin
5261 * removes the free space from the cache. We have load_only set
5262 * to one because the slow code to read in the free extents does check
5263 * the pinned extents.
5264 */
Liu Bof6373bf2012-12-27 09:01:18 +00005265 cache_block_group(cache, 1);
Chris Masone688b722011-10-31 20:52:39 -04005266
5267 pin_down_extent(root, cache, bytenr, num_bytes, 0);
5268
5269 /* remove us from the free space cache (if we're there at all) */
Josef Bacikb50c6e22013-04-25 15:55:30 -04005270 ret = btrfs_remove_free_space(cache, bytenr, num_bytes);
Chris Masone688b722011-10-31 20:52:39 -04005271 btrfs_put_block_group(cache);
Josef Bacikb50c6e22013-04-25 15:55:30 -04005272 return ret;
Chris Masone688b722011-10-31 20:52:39 -04005273}
5274
Josef Bacik8c2a1a32013-06-06 13:19:32 -04005275static int __exclude_logged_extent(struct btrfs_root *root, u64 start, u64 num_bytes)
5276{
5277 int ret;
5278 struct btrfs_block_group_cache *block_group;
5279 struct btrfs_caching_control *caching_ctl;
5280
5281 block_group = btrfs_lookup_block_group(root->fs_info, start);
5282 if (!block_group)
5283 return -EINVAL;
5284
5285 cache_block_group(block_group, 0);
5286 caching_ctl = get_caching_control(block_group);
5287
5288 if (!caching_ctl) {
5289 /* Logic error */
5290 BUG_ON(!block_group_cache_done(block_group));
5291 ret = btrfs_remove_free_space(block_group, start, num_bytes);
5292 } else {
5293 mutex_lock(&caching_ctl->mutex);
5294
5295 if (start >= caching_ctl->progress) {
5296 ret = add_excluded_extent(root, start, num_bytes);
5297 } else if (start + num_bytes <= caching_ctl->progress) {
5298 ret = btrfs_remove_free_space(block_group,
5299 start, num_bytes);
5300 } else {
5301 num_bytes = caching_ctl->progress - start;
5302 ret = btrfs_remove_free_space(block_group,
5303 start, num_bytes);
5304 if (ret)
5305 goto out_lock;
5306
5307 num_bytes = (start + num_bytes) -
5308 caching_ctl->progress;
5309 start = caching_ctl->progress;
5310 ret = add_excluded_extent(root, start, num_bytes);
5311 }
5312out_lock:
5313 mutex_unlock(&caching_ctl->mutex);
5314 put_caching_control(caching_ctl);
5315 }
5316 btrfs_put_block_group(block_group);
5317 return ret;
5318}
5319
5320int btrfs_exclude_logged_extents(struct btrfs_root *log,
5321 struct extent_buffer *eb)
5322{
5323 struct btrfs_file_extent_item *item;
5324 struct btrfs_key key;
5325 int found_type;
5326 int i;
5327
5328 if (!btrfs_fs_incompat(log->fs_info, MIXED_GROUPS))
5329 return 0;
5330
5331 for (i = 0; i < btrfs_header_nritems(eb); i++) {
5332 btrfs_item_key_to_cpu(eb, &key, i);
5333 if (key.type != BTRFS_EXTENT_DATA_KEY)
5334 continue;
5335 item = btrfs_item_ptr(eb, i, struct btrfs_file_extent_item);
5336 found_type = btrfs_file_extent_type(eb, item);
5337 if (found_type == BTRFS_FILE_EXTENT_INLINE)
5338 continue;
5339 if (btrfs_file_extent_disk_bytenr(eb, item) == 0)
5340 continue;
5341 key.objectid = btrfs_file_extent_disk_bytenr(eb, item);
5342 key.offset = btrfs_file_extent_disk_num_bytes(eb, item);
5343 __exclude_logged_extent(log, key.objectid, key.offset);
5344 }
5345
5346 return 0;
5347}
5348
Josef Bacikfb25e912011-07-26 17:00:46 -04005349/**
5350 * btrfs_update_reserved_bytes - update the block_group and space info counters
5351 * @cache: The cache we are manipulating
5352 * @num_bytes: The number of bytes in question
5353 * @reserve: One of the reservation enums
5354 *
5355 * This is called by the allocator when it reserves space, or by somebody who is
5356 * freeing space that was never actually used on disk. For example if you
5357 * reserve some space for a new leaf in transaction A and before transaction A
5358 * commits you free that leaf, you call this with reserve set to 0 in order to
5359 * clear the reservation.
5360 *
5361 * Metadata reservations should be called with RESERVE_ALLOC so we do the proper
5362 * ENOSPC accounting. For data we handle the reservation through clearing the
5363 * delalloc bits in the io_tree. We have to do this since we could end up
5364 * allocating less disk space for the amount of data we have reserved in the
5365 * case of compression.
5366 *
5367 * If this is a reservation and the block group has become read only we cannot
5368 * make the reservation and return -EAGAIN, otherwise this function always
5369 * succeeds.
Yan, Zhengf0486c62010-05-16 10:46:25 -04005370 */
Josef Bacikfb25e912011-07-26 17:00:46 -04005371static int btrfs_update_reserved_bytes(struct btrfs_block_group_cache *cache,
5372 u64 num_bytes, int reserve)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005373{
Josef Bacikfb25e912011-07-26 17:00:46 -04005374 struct btrfs_space_info *space_info = cache->space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005375 int ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005376
Josef Bacikfb25e912011-07-26 17:00:46 -04005377 spin_lock(&space_info->lock);
5378 spin_lock(&cache->lock);
5379 if (reserve != RESERVE_FREE) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04005380 if (cache->ro) {
5381 ret = -EAGAIN;
5382 } else {
Josef Bacikfb25e912011-07-26 17:00:46 -04005383 cache->reserved += num_bytes;
5384 space_info->bytes_reserved += num_bytes;
5385 if (reserve == RESERVE_ALLOC) {
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05005386 trace_btrfs_space_reservation(cache->fs_info,
Liu Bo2bcc0322012-03-29 09:57:44 -04005387 "space_info", space_info->flags,
5388 num_bytes, 0);
Josef Bacikfb25e912011-07-26 17:00:46 -04005389 space_info->bytes_may_use -= num_bytes;
5390 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04005391 }
Josef Bacikfb25e912011-07-26 17:00:46 -04005392 } else {
5393 if (cache->ro)
5394 space_info->bytes_readonly += num_bytes;
5395 cache->reserved -= num_bytes;
5396 space_info->bytes_reserved -= num_bytes;
5397 space_info->reservation_progress++;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005398 }
Josef Bacikfb25e912011-07-26 17:00:46 -04005399 spin_unlock(&cache->lock);
5400 spin_unlock(&space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005401 return ret;
5402}
5403
Jeff Mahoney143bede2012-03-01 14:56:26 +01005404void btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans,
Yan Zheng11833d62009-09-11 16:11:19 -04005405 struct btrfs_root *root)
5406{
5407 struct btrfs_fs_info *fs_info = root->fs_info;
5408 struct btrfs_caching_control *next;
5409 struct btrfs_caching_control *caching_ctl;
5410 struct btrfs_block_group_cache *cache;
Josef Bacikb150a4f2013-06-19 15:00:04 -04005411 struct btrfs_space_info *space_info;
Yan Zheng11833d62009-09-11 16:11:19 -04005412
5413 down_write(&fs_info->extent_commit_sem);
5414
5415 list_for_each_entry_safe(caching_ctl, next,
5416 &fs_info->caching_block_groups, list) {
5417 cache = caching_ctl->block_group;
5418 if (block_group_cache_done(cache)) {
5419 cache->last_byte_to_unpin = (u64)-1;
5420 list_del_init(&caching_ctl->list);
5421 put_caching_control(caching_ctl);
5422 } else {
5423 cache->last_byte_to_unpin = caching_ctl->progress;
5424 }
5425 }
5426
5427 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
5428 fs_info->pinned_extents = &fs_info->freed_extents[1];
5429 else
5430 fs_info->pinned_extents = &fs_info->freed_extents[0];
5431
5432 up_write(&fs_info->extent_commit_sem);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005433
Josef Bacikb150a4f2013-06-19 15:00:04 -04005434 list_for_each_entry_rcu(space_info, &fs_info->space_info, list)
5435 percpu_counter_set(&space_info->total_bytes_pinned, 0);
5436
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005437 update_global_block_rsv(fs_info);
Yan Zheng11833d62009-09-11 16:11:19 -04005438}
5439
5440static int unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
5441{
5442 struct btrfs_fs_info *fs_info = root->fs_info;
5443 struct btrfs_block_group_cache *cache = NULL;
Josef Bacik7b398f82012-10-22 15:52:28 -04005444 struct btrfs_space_info *space_info;
5445 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Yan Zheng11833d62009-09-11 16:11:19 -04005446 u64 len;
Josef Bacik7b398f82012-10-22 15:52:28 -04005447 bool readonly;
Yan Zheng11833d62009-09-11 16:11:19 -04005448
5449 while (start <= end) {
Josef Bacik7b398f82012-10-22 15:52:28 -04005450 readonly = false;
Yan Zheng11833d62009-09-11 16:11:19 -04005451 if (!cache ||
5452 start >= cache->key.objectid + cache->key.offset) {
5453 if (cache)
5454 btrfs_put_block_group(cache);
5455 cache = btrfs_lookup_block_group(fs_info, start);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005456 BUG_ON(!cache); /* Logic error */
Yan Zheng11833d62009-09-11 16:11:19 -04005457 }
5458
5459 len = cache->key.objectid + cache->key.offset - start;
5460 len = min(len, end + 1 - start);
5461
5462 if (start < cache->last_byte_to_unpin) {
5463 len = min(len, cache->last_byte_to_unpin - start);
5464 btrfs_add_free_space(cache, start, len);
5465 }
Josef Bacik25179202008-10-29 14:49:05 -04005466
Yan, Zhengf0486c62010-05-16 10:46:25 -04005467 start += len;
Josef Bacik7b398f82012-10-22 15:52:28 -04005468 space_info = cache->space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005469
Josef Bacik7b398f82012-10-22 15:52:28 -04005470 spin_lock(&space_info->lock);
Josef Bacik25179202008-10-29 14:49:05 -04005471 spin_lock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04005472 cache->pinned -= len;
Josef Bacik7b398f82012-10-22 15:52:28 -04005473 space_info->bytes_pinned -= len;
5474 if (cache->ro) {
5475 space_info->bytes_readonly += len;
5476 readonly = true;
5477 }
Josef Bacik25179202008-10-29 14:49:05 -04005478 spin_unlock(&cache->lock);
Josef Bacik7b398f82012-10-22 15:52:28 -04005479 if (!readonly && global_rsv->space_info == space_info) {
5480 spin_lock(&global_rsv->lock);
5481 if (!global_rsv->full) {
5482 len = min(len, global_rsv->size -
5483 global_rsv->reserved);
5484 global_rsv->reserved += len;
5485 space_info->bytes_may_use += len;
5486 if (global_rsv->reserved >= global_rsv->size)
5487 global_rsv->full = 1;
5488 }
5489 spin_unlock(&global_rsv->lock);
5490 }
5491 spin_unlock(&space_info->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04005492 }
5493
5494 if (cache)
Chris Masonfa9c0d72009-04-03 09:47:43 -04005495 btrfs_put_block_group(cache);
Chris Masonccd467d2007-06-28 15:57:36 -04005496 return 0;
5497}
5498
5499int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
Yan Zheng11833d62009-09-11 16:11:19 -04005500 struct btrfs_root *root)
Chris Masona28ec192007-03-06 20:08:01 -05005501{
Yan Zheng11833d62009-09-11 16:11:19 -04005502 struct btrfs_fs_info *fs_info = root->fs_info;
5503 struct extent_io_tree *unpin;
Chris Mason1a5bc162007-10-15 16:15:26 -04005504 u64 start;
5505 u64 end;
Chris Masona28ec192007-03-06 20:08:01 -05005506 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05005507
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005508 if (trans->aborted)
5509 return 0;
5510
Yan Zheng11833d62009-09-11 16:11:19 -04005511 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
5512 unpin = &fs_info->freed_extents[1];
5513 else
5514 unpin = &fs_info->freed_extents[0];
5515
Chris Masond3977122009-01-05 21:25:51 -05005516 while (1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04005517 ret = find_first_extent_bit(unpin, 0, &start, &end,
Josef Bacike6138872012-09-27 17:07:30 -04005518 EXTENT_DIRTY, NULL);
Chris Mason1a5bc162007-10-15 16:15:26 -04005519 if (ret)
Chris Masona28ec192007-03-06 20:08:01 -05005520 break;
Liu Hui1f3c79a2009-01-05 15:57:51 -05005521
Li Dongyang5378e602011-03-24 10:24:27 +00005522 if (btrfs_test_opt(root, DISCARD))
5523 ret = btrfs_discard_extent(root, start,
5524 end + 1 - start, NULL);
Liu Hui1f3c79a2009-01-05 15:57:51 -05005525
Chris Mason1a5bc162007-10-15 16:15:26 -04005526 clear_extent_dirty(unpin, start, end, GFP_NOFS);
Yan Zheng11833d62009-09-11 16:11:19 -04005527 unpin_extent_range(root, start, end);
Chris Masonb9473432009-03-13 11:00:37 -04005528 cond_resched();
Chris Masona28ec192007-03-06 20:08:01 -05005529 }
Josef Bacik817d52f2009-07-13 21:29:25 -04005530
Chris Masone20d96d2007-03-22 12:13:20 -04005531 return 0;
5532}
5533
Josef Bacikb150a4f2013-06-19 15:00:04 -04005534static void add_pinned_bytes(struct btrfs_fs_info *fs_info, u64 num_bytes,
5535 u64 owner, u64 root_objectid)
5536{
5537 struct btrfs_space_info *space_info;
5538 u64 flags;
5539
5540 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
5541 if (root_objectid == BTRFS_CHUNK_TREE_OBJECTID)
5542 flags = BTRFS_BLOCK_GROUP_SYSTEM;
5543 else
5544 flags = BTRFS_BLOCK_GROUP_METADATA;
5545 } else {
5546 flags = BTRFS_BLOCK_GROUP_DATA;
5547 }
5548
5549 space_info = __find_space_info(fs_info, flags);
5550 BUG_ON(!space_info); /* Logic bug */
5551 percpu_counter_add(&space_info->total_bytes_pinned, num_bytes);
5552}
5553
5554
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005555static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
5556 struct btrfs_root *root,
5557 u64 bytenr, u64 num_bytes, u64 parent,
5558 u64 root_objectid, u64 owner_objectid,
5559 u64 owner_offset, int refs_to_drop,
5560 struct btrfs_delayed_extent_op *extent_op)
Chris Masona28ec192007-03-06 20:08:01 -05005561{
Chris Masone2fa7222007-03-12 16:22:34 -04005562 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005563 struct btrfs_path *path;
Chris Mason1261ec42007-03-20 20:35:03 -04005564 struct btrfs_fs_info *info = root->fs_info;
5565 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04005566 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005567 struct btrfs_extent_item *ei;
5568 struct btrfs_extent_inline_ref *iref;
Chris Masona28ec192007-03-06 20:08:01 -05005569 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005570 int is_data;
Chris Mason952fcca2008-02-18 16:33:44 -05005571 int extent_slot = 0;
5572 int found_extent = 0;
5573 int num_to_del = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005574 u32 item_size;
5575 u64 refs;
Josef Bacik3173a182013-03-07 14:22:04 -05005576 bool skinny_metadata = btrfs_fs_incompat(root->fs_info,
5577 SKINNY_METADATA);
Chris Mason037e6392007-03-07 11:50:24 -05005578
Chris Mason5caf2a02007-04-02 11:20:42 -04005579 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04005580 if (!path)
5581 return -ENOMEM;
5582
Chris Mason3c12ac72008-04-21 12:01:38 -04005583 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04005584 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005585
5586 is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
5587 BUG_ON(!is_data && refs_to_drop != 1);
5588
Josef Bacik3173a182013-03-07 14:22:04 -05005589 if (is_data)
5590 skinny_metadata = 0;
5591
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005592 ret = lookup_extent_backref(trans, extent_root, path, &iref,
5593 bytenr, num_bytes, parent,
5594 root_objectid, owner_objectid,
5595 owner_offset);
Chris Mason7bb86312007-12-11 09:25:06 -05005596 if (ret == 0) {
Chris Mason952fcca2008-02-18 16:33:44 -05005597 extent_slot = path->slots[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005598 while (extent_slot >= 0) {
5599 btrfs_item_key_to_cpu(path->nodes[0], &key,
Chris Mason952fcca2008-02-18 16:33:44 -05005600 extent_slot);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005601 if (key.objectid != bytenr)
Chris Mason952fcca2008-02-18 16:33:44 -05005602 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005603 if (key.type == BTRFS_EXTENT_ITEM_KEY &&
5604 key.offset == num_bytes) {
Chris Mason952fcca2008-02-18 16:33:44 -05005605 found_extent = 1;
5606 break;
5607 }
Josef Bacik3173a182013-03-07 14:22:04 -05005608 if (key.type == BTRFS_METADATA_ITEM_KEY &&
5609 key.offset == owner_objectid) {
5610 found_extent = 1;
5611 break;
5612 }
Chris Mason952fcca2008-02-18 16:33:44 -05005613 if (path->slots[0] - extent_slot > 5)
5614 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005615 extent_slot--;
Chris Mason952fcca2008-02-18 16:33:44 -05005616 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005617#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
5618 item_size = btrfs_item_size_nr(path->nodes[0], extent_slot);
5619 if (found_extent && item_size < sizeof(*ei))
5620 found_extent = 0;
5621#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04005622 if (!found_extent) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005623 BUG_ON(iref);
Chris Mason56bec292009-03-13 10:10:06 -04005624 ret = remove_extent_backref(trans, extent_root, path,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005625 NULL, refs_to_drop,
5626 is_data);
David Sterba005d6422012-09-18 07:52:32 -06005627 if (ret) {
5628 btrfs_abort_transaction(trans, extent_root, ret);
5629 goto out;
5630 }
David Sterbab3b4aa72011-04-21 01:20:15 +02005631 btrfs_release_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04005632 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005633
5634 key.objectid = bytenr;
5635 key.type = BTRFS_EXTENT_ITEM_KEY;
5636 key.offset = num_bytes;
5637
Josef Bacik3173a182013-03-07 14:22:04 -05005638 if (!is_data && skinny_metadata) {
5639 key.type = BTRFS_METADATA_ITEM_KEY;
5640 key.offset = owner_objectid;
5641 }
5642
Zheng Yan31840ae2008-09-23 13:14:14 -04005643 ret = btrfs_search_slot(trans, extent_root,
5644 &key, path, -1, 1);
Josef Bacik3173a182013-03-07 14:22:04 -05005645 if (ret > 0 && skinny_metadata && path->slots[0]) {
5646 /*
5647 * Couldn't find our skinny metadata item,
5648 * see if we have ye olde extent item.
5649 */
5650 path->slots[0]--;
5651 btrfs_item_key_to_cpu(path->nodes[0], &key,
5652 path->slots[0]);
5653 if (key.objectid == bytenr &&
5654 key.type == BTRFS_EXTENT_ITEM_KEY &&
5655 key.offset == num_bytes)
5656 ret = 0;
5657 }
5658
5659 if (ret > 0 && skinny_metadata) {
5660 skinny_metadata = false;
5661 key.type = BTRFS_EXTENT_ITEM_KEY;
5662 key.offset = num_bytes;
5663 btrfs_release_path(path);
5664 ret = btrfs_search_slot(trans, extent_root,
5665 &key, path, -1, 1);
5666 }
5667
Josef Bacikf3465ca2008-11-12 14:19:50 -05005668 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00005669 btrfs_err(info, "umm, got %d back from search, was looking for %llu",
5670 ret, (unsigned long long)bytenr);
Josef Bacikb783e622011-07-13 15:03:50 +00005671 if (ret > 0)
5672 btrfs_print_leaf(extent_root,
5673 path->nodes[0]);
Josef Bacikf3465ca2008-11-12 14:19:50 -05005674 }
David Sterba005d6422012-09-18 07:52:32 -06005675 if (ret < 0) {
5676 btrfs_abort_transaction(trans, extent_root, ret);
5677 goto out;
5678 }
Zheng Yan31840ae2008-09-23 13:14:14 -04005679 extent_slot = path->slots[0];
5680 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005681 } else if (ret == -ENOENT) {
Chris Mason7bb86312007-12-11 09:25:06 -05005682 btrfs_print_leaf(extent_root, path->nodes[0]);
5683 WARN_ON(1);
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00005684 btrfs_err(info,
5685 "unable to find ref byte nr %llu parent %llu root %llu owner %llu offset %llu",
5686 (unsigned long long)bytenr,
5687 (unsigned long long)parent,
5688 (unsigned long long)root_objectid,
5689 (unsigned long long)owner_objectid,
5690 (unsigned long long)owner_offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005691 } else {
David Sterba005d6422012-09-18 07:52:32 -06005692 btrfs_abort_transaction(trans, extent_root, ret);
5693 goto out;
Chris Mason7bb86312007-12-11 09:25:06 -05005694 }
Chris Mason5f39d392007-10-15 16:14:19 -04005695
5696 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005697 item_size = btrfs_item_size_nr(leaf, extent_slot);
5698#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
5699 if (item_size < sizeof(*ei)) {
5700 BUG_ON(found_extent || extent_slot != path->slots[0]);
5701 ret = convert_extent_item_v0(trans, extent_root, path,
5702 owner_objectid, 0);
David Sterba005d6422012-09-18 07:52:32 -06005703 if (ret < 0) {
5704 btrfs_abort_transaction(trans, extent_root, ret);
5705 goto out;
5706 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005707
David Sterbab3b4aa72011-04-21 01:20:15 +02005708 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005709 path->leave_spinning = 1;
5710
5711 key.objectid = bytenr;
5712 key.type = BTRFS_EXTENT_ITEM_KEY;
5713 key.offset = num_bytes;
5714
5715 ret = btrfs_search_slot(trans, extent_root, &key, path,
5716 -1, 1);
5717 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00005718 btrfs_err(info, "umm, got %d back from search, was looking for %llu",
5719 ret, (unsigned long long)bytenr);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005720 btrfs_print_leaf(extent_root, path->nodes[0]);
5721 }
David Sterba005d6422012-09-18 07:52:32 -06005722 if (ret < 0) {
5723 btrfs_abort_transaction(trans, extent_root, ret);
5724 goto out;
5725 }
5726
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005727 extent_slot = path->slots[0];
5728 leaf = path->nodes[0];
5729 item_size = btrfs_item_size_nr(leaf, extent_slot);
5730 }
5731#endif
5732 BUG_ON(item_size < sizeof(*ei));
Chris Mason952fcca2008-02-18 16:33:44 -05005733 ei = btrfs_item_ptr(leaf, extent_slot,
Chris Mason123abc82007-03-14 14:14:43 -04005734 struct btrfs_extent_item);
Josef Bacik3173a182013-03-07 14:22:04 -05005735 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID &&
5736 key.type == BTRFS_EXTENT_ITEM_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005737 struct btrfs_tree_block_info *bi;
5738 BUG_ON(item_size < sizeof(*ei) + sizeof(*bi));
5739 bi = (struct btrfs_tree_block_info *)(ei + 1);
5740 WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
Chris Mason952fcca2008-02-18 16:33:44 -05005741 }
5742
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005743 refs = btrfs_extent_refs(leaf, ei);
Josef Bacik32b02532013-04-24 16:38:50 -04005744 if (refs < refs_to_drop) {
5745 btrfs_err(info, "trying to drop %d refs but we only have %Lu "
5746 "for bytenr %Lu\n", refs_to_drop, refs, bytenr);
5747 ret = -EINVAL;
5748 btrfs_abort_transaction(trans, extent_root, ret);
5749 goto out;
5750 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005751 refs -= refs_to_drop;
5752
5753 if (refs > 0) {
5754 if (extent_op)
5755 __run_delayed_extent_op(extent_op, leaf, ei);
5756 /*
5757 * In the case of inline back ref, reference count will
5758 * be updated by remove_extent_backref
5759 */
5760 if (iref) {
5761 BUG_ON(!found_extent);
5762 } else {
5763 btrfs_set_extent_refs(leaf, ei, refs);
5764 btrfs_mark_buffer_dirty(leaf);
5765 }
5766 if (found_extent) {
5767 ret = remove_extent_backref(trans, extent_root, path,
5768 iref, refs_to_drop,
5769 is_data);
David Sterba005d6422012-09-18 07:52:32 -06005770 if (ret) {
5771 btrfs_abort_transaction(trans, extent_root, ret);
5772 goto out;
5773 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005774 }
Josef Bacikb150a4f2013-06-19 15:00:04 -04005775 add_pinned_bytes(root->fs_info, -num_bytes, owner_objectid,
5776 root_objectid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005777 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005778 if (found_extent) {
5779 BUG_ON(is_data && refs_to_drop !=
5780 extent_data_ref_count(root, path, iref));
5781 if (iref) {
5782 BUG_ON(path->slots[0] != extent_slot);
5783 } else {
5784 BUG_ON(path->slots[0] != extent_slot + 1);
5785 path->slots[0] = extent_slot;
5786 num_to_del = 2;
5787 }
Chris Mason78fae272007-03-25 11:35:08 -04005788 }
Chris Masonb9473432009-03-13 11:00:37 -04005789
Chris Mason952fcca2008-02-18 16:33:44 -05005790 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
5791 num_to_del);
David Sterba005d6422012-09-18 07:52:32 -06005792 if (ret) {
5793 btrfs_abort_transaction(trans, extent_root, ret);
5794 goto out;
5795 }
David Sterbab3b4aa72011-04-21 01:20:15 +02005796 btrfs_release_path(path);
David Woodhouse21af8042008-08-12 14:13:26 +01005797
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005798 if (is_data) {
Chris Mason459931e2008-12-10 09:10:46 -05005799 ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
David Sterba005d6422012-09-18 07:52:32 -06005800 if (ret) {
5801 btrfs_abort_transaction(trans, extent_root, ret);
5802 goto out;
5803 }
Chris Mason459931e2008-12-10 09:10:46 -05005804 }
5805
Liu Boc53d6132012-12-27 09:01:19 +00005806 ret = update_block_group(root, bytenr, num_bytes, 0);
David Sterba005d6422012-09-18 07:52:32 -06005807 if (ret) {
5808 btrfs_abort_transaction(trans, extent_root, ret);
5809 goto out;
5810 }
Chris Masona28ec192007-03-06 20:08:01 -05005811 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005812out:
Chris Mason5caf2a02007-04-02 11:20:42 -04005813 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -05005814 return ret;
5815}
5816
5817/*
Yan, Zhengf0486c62010-05-16 10:46:25 -04005818 * when we free an block, it is possible (and likely) that we free the last
Chris Mason1887be62009-03-13 10:11:24 -04005819 * delayed ref for that extent as well. This searches the delayed ref tree for
5820 * a given extent, and if there are no other delayed refs to be processed, it
5821 * removes it from the tree.
5822 */
5823static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
5824 struct btrfs_root *root, u64 bytenr)
5825{
5826 struct btrfs_delayed_ref_head *head;
5827 struct btrfs_delayed_ref_root *delayed_refs;
5828 struct btrfs_delayed_ref_node *ref;
5829 struct rb_node *node;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005830 int ret = 0;
Chris Mason1887be62009-03-13 10:11:24 -04005831
5832 delayed_refs = &trans->transaction->delayed_refs;
5833 spin_lock(&delayed_refs->lock);
5834 head = btrfs_find_delayed_ref_head(trans, bytenr);
5835 if (!head)
5836 goto out;
5837
5838 node = rb_prev(&head->node.rb_node);
5839 if (!node)
5840 goto out;
5841
5842 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
5843
5844 /* there are still entries for this ref, we can't drop it */
5845 if (ref->bytenr == bytenr)
5846 goto out;
5847
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005848 if (head->extent_op) {
5849 if (!head->must_insert_reserved)
5850 goto out;
Miao Xie78a61842012-11-21 02:21:28 +00005851 btrfs_free_delayed_extent_op(head->extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005852 head->extent_op = NULL;
5853 }
5854
Chris Mason1887be62009-03-13 10:11:24 -04005855 /*
5856 * waiting for the lock here would deadlock. If someone else has it
5857 * locked they are already in the process of dropping it anyway
5858 */
5859 if (!mutex_trylock(&head->mutex))
5860 goto out;
5861
5862 /*
5863 * at this point we have a head with no other entries. Go
5864 * ahead and process it.
5865 */
5866 head->node.in_tree = 0;
5867 rb_erase(&head->node.rb_node, &delayed_refs->root);
Chris Masonc3e69d52009-03-13 10:17:05 -04005868
Chris Mason1887be62009-03-13 10:11:24 -04005869 delayed_refs->num_entries--;
5870
5871 /*
5872 * we don't take a ref on the node because we're removing it from the
5873 * tree, so we just steal the ref the tree was holding.
5874 */
Chris Masonc3e69d52009-03-13 10:17:05 -04005875 delayed_refs->num_heads--;
5876 if (list_empty(&head->cluster))
5877 delayed_refs->num_heads_ready--;
5878
5879 list_del_init(&head->cluster);
Chris Mason1887be62009-03-13 10:11:24 -04005880 spin_unlock(&delayed_refs->lock);
5881
Yan, Zhengf0486c62010-05-16 10:46:25 -04005882 BUG_ON(head->extent_op);
5883 if (head->must_insert_reserved)
5884 ret = 1;
5885
5886 mutex_unlock(&head->mutex);
Chris Mason1887be62009-03-13 10:11:24 -04005887 btrfs_put_delayed_ref(&head->node);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005888 return ret;
Chris Mason1887be62009-03-13 10:11:24 -04005889out:
5890 spin_unlock(&delayed_refs->lock);
5891 return 0;
5892}
5893
Yan, Zhengf0486c62010-05-16 10:46:25 -04005894void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
5895 struct btrfs_root *root,
5896 struct extent_buffer *buf,
Jan Schmidt5581a512012-05-16 17:04:52 +02005897 u64 parent, int last_ref)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005898{
Yan, Zhengf0486c62010-05-16 10:46:25 -04005899 struct btrfs_block_group_cache *cache = NULL;
Josef Bacikb150a4f2013-06-19 15:00:04 -04005900 int pin = 1;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005901 int ret;
5902
5903 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02005904 ret = btrfs_add_delayed_tree_ref(root->fs_info, trans,
5905 buf->start, buf->len,
5906 parent, root->root_key.objectid,
5907 btrfs_header_level(buf),
Jan Schmidt5581a512012-05-16 17:04:52 +02005908 BTRFS_DROP_DELAYED_REF, NULL, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005909 BUG_ON(ret); /* -ENOMEM */
Yan, Zhengf0486c62010-05-16 10:46:25 -04005910 }
5911
5912 if (!last_ref)
5913 return;
5914
Yan, Zhengf0486c62010-05-16 10:46:25 -04005915 cache = btrfs_lookup_block_group(root->fs_info, buf->start);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005916
5917 if (btrfs_header_generation(buf) == trans->transid) {
5918 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
5919 ret = check_ref_cleanup(trans, root, buf->start);
5920 if (!ret)
Josef Bacik37be25b2011-08-05 10:25:38 -04005921 goto out;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005922 }
5923
5924 if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
5925 pin_down_extent(root, cache, buf->start, buf->len, 1);
Josef Bacik37be25b2011-08-05 10:25:38 -04005926 goto out;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005927 }
5928
5929 WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags));
5930
5931 btrfs_add_free_space(cache, buf->start, buf->len);
Josef Bacikfb25e912011-07-26 17:00:46 -04005932 btrfs_update_reserved_bytes(cache, buf->len, RESERVE_FREE);
Josef Bacikb150a4f2013-06-19 15:00:04 -04005933 pin = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005934 }
5935out:
Josef Bacikb150a4f2013-06-19 15:00:04 -04005936 if (pin)
5937 add_pinned_bytes(root->fs_info, buf->len,
5938 btrfs_header_level(buf),
5939 root->root_key.objectid);
5940
Josef Bacika826d6d2011-03-16 13:42:43 -04005941 /*
5942 * Deleting the buffer, clear the corrupt flag since it doesn't matter
5943 * anymore.
5944 */
5945 clear_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005946 btrfs_put_block_group(cache);
5947}
5948
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005949/* Can return -ENOMEM */
Arne Jansen66d7e7f2011-09-12 15:26:38 +02005950int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root *root,
5951 u64 bytenr, u64 num_bytes, u64 parent, u64 root_objectid,
5952 u64 owner, u64 offset, int for_cow)
Chris Mason925baed2008-06-25 16:01:30 -04005953{
5954 int ret;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02005955 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason925baed2008-06-25 16:01:30 -04005956
Josef Bacikb150a4f2013-06-19 15:00:04 -04005957 add_pinned_bytes(root->fs_info, num_bytes, owner, root_objectid);
5958
Chris Mason56bec292009-03-13 10:10:06 -04005959 /*
5960 * tree log blocks never actually go into the extent allocation
5961 * tree, just update pinning info and exit early.
Chris Mason56bec292009-03-13 10:10:06 -04005962 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005963 if (root_objectid == BTRFS_TREE_LOG_OBJECTID) {
5964 WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID);
Chris Masonb9473432009-03-13 11:00:37 -04005965 /* unlocks the pinned mutex */
Yan Zheng11833d62009-09-11 16:11:19 -04005966 btrfs_pin_extent(root, bytenr, num_bytes, 1);
Chris Mason56bec292009-03-13 10:10:06 -04005967 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005968 } else if (owner < BTRFS_FIRST_FREE_OBJECTID) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02005969 ret = btrfs_add_delayed_tree_ref(fs_info, trans, bytenr,
5970 num_bytes,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005971 parent, root_objectid, (int)owner,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02005972 BTRFS_DROP_DELAYED_REF, NULL, for_cow);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005973 } else {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02005974 ret = btrfs_add_delayed_data_ref(fs_info, trans, bytenr,
5975 num_bytes,
5976 parent, root_objectid, owner,
5977 offset, BTRFS_DROP_DELAYED_REF,
5978 NULL, for_cow);
Chris Mason56bec292009-03-13 10:10:06 -04005979 }
Chris Mason925baed2008-06-25 16:01:30 -04005980 return ret;
5981}
5982
David Woodhouse53b381b2013-01-29 18:40:14 -05005983static u64 stripe_align(struct btrfs_root *root,
5984 struct btrfs_block_group_cache *cache,
5985 u64 val, u64 num_bytes)
Chris Mason87ee04e2007-11-30 11:30:34 -05005986{
Qu Wenruofda28322013-02-26 08:10:22 +00005987 u64 ret = ALIGN(val, root->stripesize);
Chris Mason87ee04e2007-11-30 11:30:34 -05005988 return ret;
5989}
5990
Chris Masonfec577f2007-02-26 10:40:21 -05005991/*
Josef Bacik817d52f2009-07-13 21:29:25 -04005992 * when we wait for progress in the block group caching, its because
5993 * our allocation attempt failed at least once. So, we must sleep
5994 * and let some progress happen before we try again.
5995 *
5996 * This function will sleep at least once waiting for new free space to
5997 * show up, and then it will check the block group free space numbers
5998 * for our min num_bytes. Another option is to have it go ahead
5999 * and look in the rbtree for a free extent of a given size, but this
6000 * is a good start.
6001 */
6002static noinline int
6003wait_block_group_cache_progress(struct btrfs_block_group_cache *cache,
6004 u64 num_bytes)
6005{
Yan Zheng11833d62009-09-11 16:11:19 -04006006 struct btrfs_caching_control *caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -04006007
Yan Zheng11833d62009-09-11 16:11:19 -04006008 caching_ctl = get_caching_control(cache);
6009 if (!caching_ctl)
Josef Bacik817d52f2009-07-13 21:29:25 -04006010 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -04006011
Yan Zheng11833d62009-09-11 16:11:19 -04006012 wait_event(caching_ctl->wait, block_group_cache_done(cache) ||
Li Zefan34d52cb6c2011-03-29 13:46:06 +08006013 (cache->free_space_ctl->free_space >= num_bytes));
Yan Zheng11833d62009-09-11 16:11:19 -04006014
6015 put_caching_control(caching_ctl);
6016 return 0;
6017}
6018
6019static noinline int
6020wait_block_group_cache_done(struct btrfs_block_group_cache *cache)
6021{
6022 struct btrfs_caching_control *caching_ctl;
Yan Zheng11833d62009-09-11 16:11:19 -04006023
6024 caching_ctl = get_caching_control(cache);
6025 if (!caching_ctl)
6026 return 0;
6027
6028 wait_event(caching_ctl->wait, block_group_cache_done(cache));
6029
6030 put_caching_control(caching_ctl);
Josef Bacik817d52f2009-07-13 21:29:25 -04006031 return 0;
6032}
6033
Liu Bo31e50222012-11-21 14:18:10 +00006034int __get_raid_index(u64 flags)
Yan, Zhengb742bb82010-05-16 10:46:24 -04006035{
Ilya Dryomov7738a532012-03-27 17:09:17 +03006036 if (flags & BTRFS_BLOCK_GROUP_RAID10)
Miao Xiee6ec7162013-01-17 05:38:51 +00006037 return BTRFS_RAID_RAID10;
Ilya Dryomov7738a532012-03-27 17:09:17 +03006038 else if (flags & BTRFS_BLOCK_GROUP_RAID1)
Miao Xiee6ec7162013-01-17 05:38:51 +00006039 return BTRFS_RAID_RAID1;
Ilya Dryomov7738a532012-03-27 17:09:17 +03006040 else if (flags & BTRFS_BLOCK_GROUP_DUP)
Miao Xiee6ec7162013-01-17 05:38:51 +00006041 return BTRFS_RAID_DUP;
Ilya Dryomov7738a532012-03-27 17:09:17 +03006042 else if (flags & BTRFS_BLOCK_GROUP_RAID0)
Miao Xiee6ec7162013-01-17 05:38:51 +00006043 return BTRFS_RAID_RAID0;
David Woodhouse53b381b2013-01-29 18:40:14 -05006044 else if (flags & BTRFS_BLOCK_GROUP_RAID5)
Chris Masone942f882013-02-20 14:06:05 -05006045 return BTRFS_RAID_RAID5;
David Woodhouse53b381b2013-01-29 18:40:14 -05006046 else if (flags & BTRFS_BLOCK_GROUP_RAID6)
Chris Masone942f882013-02-20 14:06:05 -05006047 return BTRFS_RAID_RAID6;
Ilya Dryomov7738a532012-03-27 17:09:17 +03006048
Chris Masone942f882013-02-20 14:06:05 -05006049 return BTRFS_RAID_SINGLE; /* BTRFS_BLOCK_GROUP_SINGLE */
Yan, Zhengb742bb82010-05-16 10:46:24 -04006050}
6051
Ilya Dryomov7738a532012-03-27 17:09:17 +03006052static int get_block_group_index(struct btrfs_block_group_cache *cache)
6053{
Liu Bo31e50222012-11-21 14:18:10 +00006054 return __get_raid_index(cache->flags);
Ilya Dryomov7738a532012-03-27 17:09:17 +03006055}
6056
Josef Bacik817d52f2009-07-13 21:29:25 -04006057enum btrfs_loop_type {
Josef Bacik285ff5a2012-01-13 15:27:45 -05006058 LOOP_CACHING_NOWAIT = 0,
6059 LOOP_CACHING_WAIT = 1,
6060 LOOP_ALLOC_CHUNK = 2,
6061 LOOP_NO_EMPTY_SIZE = 3,
Josef Bacik817d52f2009-07-13 21:29:25 -04006062};
6063
6064/*
Chris Masonfec577f2007-02-26 10:40:21 -05006065 * walks the btree of allocated extents and find a hole of a given size.
6066 * The key ins is changed to record the hole:
6067 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -04006068 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -05006069 * ins->offset == number of blocks
6070 * Any available blocks before search_start are skipped.
6071 */
Chris Masond3977122009-01-05 21:25:51 -05006072static noinline int find_free_extent(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05006073 struct btrfs_root *orig_root,
6074 u64 num_bytes, u64 empty_size,
Chris Mason98ed5172008-01-03 10:01:48 -05006075 u64 hint_byte, struct btrfs_key *ins,
David Sterbab6919a52013-04-29 13:39:40 +00006076 u64 flags)
Chris Masonfec577f2007-02-26 10:40:21 -05006077{
Josef Bacik80eb2342008-10-29 14:49:05 -04006078 int ret = 0;
Chris Masond3977122009-01-05 21:25:51 -05006079 struct btrfs_root *root = orig_root->fs_info->extent_root;
Chris Masonfa9c0d72009-04-03 09:47:43 -04006080 struct btrfs_free_cluster *last_ptr = NULL;
Josef Bacik80eb2342008-10-29 14:49:05 -04006081 struct btrfs_block_group_cache *block_group = NULL;
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006082 struct btrfs_block_group_cache *used_block_group;
Josef Bacik81c9ad22012-01-18 10:56:06 -05006083 u64 search_start = 0;
Chris Mason239b14b2008-03-24 15:02:07 -04006084 int empty_cluster = 2 * 1024 * 1024;
Josef Bacik80eb2342008-10-29 14:49:05 -04006085 struct btrfs_space_info *space_info;
Chris Masonfa9c0d72009-04-03 09:47:43 -04006086 int loop = 0;
David Sterbab6919a52013-04-29 13:39:40 +00006087 int index = __get_raid_index(flags);
6088 int alloc_type = (flags & BTRFS_BLOCK_GROUP_DATA) ?
Josef Bacikfb25e912011-07-26 17:00:46 -04006089 RESERVE_ALLOC_NO_ACCOUNT : RESERVE_ALLOC;
Josef Bacik817d52f2009-07-13 21:29:25 -04006090 bool found_uncached_bg = false;
Josef Bacik0a243252009-09-11 16:11:20 -04006091 bool failed_cluster_refill = false;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04006092 bool failed_alloc = false;
Josef Bacik67377732010-09-16 16:19:09 -04006093 bool use_cluster = true;
Miao Xie60d2adb2011-09-09 17:34:35 +08006094 bool have_caching_bg = false;
Chris Masonfec577f2007-02-26 10:40:21 -05006095
Chris Masondb945352007-10-15 16:15:53 -04006096 WARN_ON(num_bytes < root->sectorsize);
Chris Masonb1a4d962007-04-04 15:27:52 -04006097 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
Josef Bacik80eb2342008-10-29 14:49:05 -04006098 ins->objectid = 0;
6099 ins->offset = 0;
Chris Masonb1a4d962007-04-04 15:27:52 -04006100
David Sterbab6919a52013-04-29 13:39:40 +00006101 trace_find_free_extent(orig_root, num_bytes, empty_size, flags);
Josef Bacik3f7de032011-11-10 08:29:20 -05006102
David Sterbab6919a52013-04-29 13:39:40 +00006103 space_info = __find_space_info(root->fs_info, flags);
Josef Bacik1b1d1f62010-03-19 20:49:55 +00006104 if (!space_info) {
David Sterbab6919a52013-04-29 13:39:40 +00006105 btrfs_err(root->fs_info, "No space info for %llu", flags);
Josef Bacik1b1d1f62010-03-19 20:49:55 +00006106 return -ENOSPC;
6107 }
Josef Bacik2552d172009-04-03 10:14:19 -04006108
Josef Bacik67377732010-09-16 16:19:09 -04006109 /*
6110 * If the space info is for both data and metadata it means we have a
6111 * small filesystem and we can't use the clustering stuff.
6112 */
6113 if (btrfs_mixed_space_info(space_info))
6114 use_cluster = false;
6115
David Sterbab6919a52013-04-29 13:39:40 +00006116 if (flags & BTRFS_BLOCK_GROUP_METADATA && use_cluster) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04006117 last_ptr = &root->fs_info->meta_alloc_cluster;
Chris Mason536ac8a2009-02-12 09:41:38 -05006118 if (!btrfs_test_opt(root, SSD))
6119 empty_cluster = 64 * 1024;
Chris Mason239b14b2008-03-24 15:02:07 -04006120 }
6121
David Sterbab6919a52013-04-29 13:39:40 +00006122 if ((flags & BTRFS_BLOCK_GROUP_DATA) && use_cluster &&
Josef Bacik67377732010-09-16 16:19:09 -04006123 btrfs_test_opt(root, SSD)) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04006124 last_ptr = &root->fs_info->data_alloc_cluster;
6125 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04006126
Chris Mason239b14b2008-03-24 15:02:07 -04006127 if (last_ptr) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04006128 spin_lock(&last_ptr->lock);
6129 if (last_ptr->block_group)
6130 hint_byte = last_ptr->window_start;
6131 spin_unlock(&last_ptr->lock);
Chris Mason239b14b2008-03-24 15:02:07 -04006132 }
Chris Masonfa9c0d72009-04-03 09:47:43 -04006133
Chris Masona061fc82008-05-07 11:43:44 -04006134 search_start = max(search_start, first_logical_byte(root, 0));
Chris Mason239b14b2008-03-24 15:02:07 -04006135 search_start = max(search_start, hint_byte);
Chris Mason0b86a832008-03-24 15:01:56 -04006136
Josef Bacik817d52f2009-07-13 21:29:25 -04006137 if (!last_ptr)
Chris Masonfa9c0d72009-04-03 09:47:43 -04006138 empty_cluster = 0;
Chris Masonfa9c0d72009-04-03 09:47:43 -04006139
Josef Bacik2552d172009-04-03 10:14:19 -04006140 if (search_start == hint_byte) {
Josef Bacik2552d172009-04-03 10:14:19 -04006141 block_group = btrfs_lookup_block_group(root->fs_info,
6142 search_start);
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006143 used_block_group = block_group;
Josef Bacik817d52f2009-07-13 21:29:25 -04006144 /*
6145 * we don't want to use the block group if it doesn't match our
6146 * allocation bits, or if its not cached.
Josef Bacikccf0e722009-11-10 21:23:48 -05006147 *
6148 * However if we are re-searching with an ideal block group
6149 * picked out then we don't care that the block group is cached.
Josef Bacik817d52f2009-07-13 21:29:25 -04006150 */
David Sterbab6919a52013-04-29 13:39:40 +00006151 if (block_group && block_group_bits(block_group, flags) &&
Josef Bacik285ff5a2012-01-13 15:27:45 -05006152 block_group->cached != BTRFS_CACHE_NO) {
Josef Bacik2552d172009-04-03 10:14:19 -04006153 down_read(&space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04006154 if (list_empty(&block_group->list) ||
6155 block_group->ro) {
6156 /*
6157 * someone is removing this block group,
6158 * we can't jump into the have_block_group
6159 * target because our list pointers are not
6160 * valid
6161 */
6162 btrfs_put_block_group(block_group);
6163 up_read(&space_info->groups_sem);
Josef Bacikccf0e722009-11-10 21:23:48 -05006164 } else {
Yan, Zhengb742bb82010-05-16 10:46:24 -04006165 index = get_block_group_index(block_group);
Chris Mason44fb5512009-06-04 15:34:51 -04006166 goto have_block_group;
Josef Bacikccf0e722009-11-10 21:23:48 -05006167 }
Josef Bacik2552d172009-04-03 10:14:19 -04006168 } else if (block_group) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04006169 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04006170 }
Chris Mason42e70e72008-11-07 18:17:11 -05006171 }
Josef Bacik2552d172009-04-03 10:14:19 -04006172search:
Miao Xie60d2adb2011-09-09 17:34:35 +08006173 have_caching_bg = false;
Josef Bacik80eb2342008-10-29 14:49:05 -04006174 down_read(&space_info->groups_sem);
Yan, Zhengb742bb82010-05-16 10:46:24 -04006175 list_for_each_entry(block_group, &space_info->block_groups[index],
6176 list) {
Josef Bacik6226cb02009-04-03 10:14:18 -04006177 u64 offset;
Josef Bacik817d52f2009-07-13 21:29:25 -04006178 int cached;
Chris Mason8a1413a2008-11-10 16:13:54 -05006179
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006180 used_block_group = block_group;
Josef Bacik11dfe352009-11-13 20:12:59 +00006181 btrfs_get_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04006182 search_start = block_group->key.objectid;
Chris Mason42e70e72008-11-07 18:17:11 -05006183
Chris Mason83a50de2010-12-13 15:06:46 -05006184 /*
6185 * this can happen if we end up cycling through all the
6186 * raid types, but we want to make sure we only allocate
6187 * for the proper type.
6188 */
David Sterbab6919a52013-04-29 13:39:40 +00006189 if (!block_group_bits(block_group, flags)) {
Chris Mason83a50de2010-12-13 15:06:46 -05006190 u64 extra = BTRFS_BLOCK_GROUP_DUP |
6191 BTRFS_BLOCK_GROUP_RAID1 |
David Woodhouse53b381b2013-01-29 18:40:14 -05006192 BTRFS_BLOCK_GROUP_RAID5 |
6193 BTRFS_BLOCK_GROUP_RAID6 |
Chris Mason83a50de2010-12-13 15:06:46 -05006194 BTRFS_BLOCK_GROUP_RAID10;
6195
6196 /*
6197 * if they asked for extra copies and this block group
6198 * doesn't provide them, bail. This does allow us to
6199 * fill raid0 from raid1.
6200 */
David Sterbab6919a52013-04-29 13:39:40 +00006201 if ((flags & extra) && !(block_group->flags & extra))
Chris Mason83a50de2010-12-13 15:06:46 -05006202 goto loop;
6203 }
6204
Josef Bacik2552d172009-04-03 10:14:19 -04006205have_block_group:
Josef Bacik291c7d22011-11-14 13:52:14 -05006206 cached = block_group_cache_done(block_group);
6207 if (unlikely(!cached)) {
Josef Bacik291c7d22011-11-14 13:52:14 -05006208 found_uncached_bg = true;
Liu Bof6373bf2012-12-27 09:01:18 +00006209 ret = cache_block_group(block_group, 0);
Chris Mason1d4284b2012-03-28 20:31:37 -04006210 BUG_ON(ret < 0);
6211 ret = 0;
Josef Bacikea6a4782008-11-20 12:16:16 -05006212 }
6213
Josef Bacikea6a4782008-11-20 12:16:16 -05006214 if (unlikely(block_group->ro))
Josef Bacik2552d172009-04-03 10:14:19 -04006215 goto loop;
Josef Bacik0f9dd462008-09-23 13:14:11 -04006216
Josef Bacik0a243252009-09-11 16:11:20 -04006217 /*
Alexandre Oliva062c05c2011-12-07 19:50:42 -05006218 * Ok we want to try and use the cluster allocator, so
6219 * lets look there
Josef Bacik0a243252009-09-11 16:11:20 -04006220 */
Alexandre Oliva062c05c2011-12-07 19:50:42 -05006221 if (last_ptr) {
Chris Mason8de972b2013-01-04 15:39:43 -05006222 unsigned long aligned_cluster;
Chris Masonfa9c0d72009-04-03 09:47:43 -04006223 /*
6224 * the refill lock keeps out other
6225 * people trying to start a new cluster
6226 */
6227 spin_lock(&last_ptr->refill_lock);
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006228 used_block_group = last_ptr->block_group;
6229 if (used_block_group != block_group &&
6230 (!used_block_group ||
6231 used_block_group->ro ||
David Sterbab6919a52013-04-29 13:39:40 +00006232 !block_group_bits(used_block_group, flags))) {
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006233 used_block_group = block_group;
Chris Mason44fb5512009-06-04 15:34:51 -04006234 goto refill_cluster;
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006235 }
Chris Mason44fb5512009-06-04 15:34:51 -04006236
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006237 if (used_block_group != block_group)
6238 btrfs_get_block_group(used_block_group);
6239
6240 offset = btrfs_alloc_from_cluster(used_block_group,
6241 last_ptr, num_bytes, used_block_group->key.objectid);
Chris Masonfa9c0d72009-04-03 09:47:43 -04006242 if (offset) {
6243 /* we have a block, we're done */
6244 spin_unlock(&last_ptr->refill_lock);
Josef Bacik3f7de032011-11-10 08:29:20 -05006245 trace_btrfs_reserve_extent_cluster(root,
6246 block_group, search_start, num_bytes);
Chris Masonfa9c0d72009-04-03 09:47:43 -04006247 goto checks;
6248 }
6249
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006250 WARN_ON(last_ptr->block_group != used_block_group);
6251 if (used_block_group != block_group) {
6252 btrfs_put_block_group(used_block_group);
6253 used_block_group = block_group;
Chris Masonfa9c0d72009-04-03 09:47:43 -04006254 }
Chris Mason44fb5512009-06-04 15:34:51 -04006255refill_cluster:
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006256 BUG_ON(used_block_group != block_group);
Alexandre Oliva062c05c2011-12-07 19:50:42 -05006257 /* If we are on LOOP_NO_EMPTY_SIZE, we can't
6258 * set up a new clusters, so lets just skip it
6259 * and let the allocator find whatever block
6260 * it can find. If we reach this point, we
6261 * will have tried the cluster allocator
6262 * plenty of times and not have found
6263 * anything, so we are likely way too
6264 * fragmented for the clustering stuff to find
Alexandre Olivaa5f6f712011-12-12 04:48:19 -02006265 * anything.
6266 *
6267 * However, if the cluster is taken from the
6268 * current block group, release the cluster
6269 * first, so that we stand a better chance of
6270 * succeeding in the unclustered
6271 * allocation. */
6272 if (loop >= LOOP_NO_EMPTY_SIZE &&
6273 last_ptr->block_group != block_group) {
Alexandre Oliva062c05c2011-12-07 19:50:42 -05006274 spin_unlock(&last_ptr->refill_lock);
6275 goto unclustered_alloc;
6276 }
6277
Chris Masonfa9c0d72009-04-03 09:47:43 -04006278 /*
6279 * this cluster didn't work out, free it and
6280 * start over
6281 */
6282 btrfs_return_cluster_to_free_space(NULL, last_ptr);
6283
Alexandre Olivaa5f6f712011-12-12 04:48:19 -02006284 if (loop >= LOOP_NO_EMPTY_SIZE) {
6285 spin_unlock(&last_ptr->refill_lock);
6286 goto unclustered_alloc;
6287 }
6288
Chris Mason8de972b2013-01-04 15:39:43 -05006289 aligned_cluster = max_t(unsigned long,
6290 empty_cluster + empty_size,
6291 block_group->full_stripe_len);
6292
Chris Masonfa9c0d72009-04-03 09:47:43 -04006293 /* allocate a cluster in this block group */
Chris Mason451d7582009-06-09 20:28:34 -04006294 ret = btrfs_find_space_cluster(trans, root,
Chris Masonfa9c0d72009-04-03 09:47:43 -04006295 block_group, last_ptr,
Alexandre Oliva1b22bad2011-11-30 13:43:00 -05006296 search_start, num_bytes,
Chris Mason8de972b2013-01-04 15:39:43 -05006297 aligned_cluster);
Chris Masonfa9c0d72009-04-03 09:47:43 -04006298 if (ret == 0) {
6299 /*
6300 * now pull our allocation out of this
6301 * cluster
6302 */
6303 offset = btrfs_alloc_from_cluster(block_group,
6304 last_ptr, num_bytes,
6305 search_start);
6306 if (offset) {
6307 /* we found one, proceed */
6308 spin_unlock(&last_ptr->refill_lock);
Josef Bacik3f7de032011-11-10 08:29:20 -05006309 trace_btrfs_reserve_extent_cluster(root,
6310 block_group, search_start,
6311 num_bytes);
Chris Masonfa9c0d72009-04-03 09:47:43 -04006312 goto checks;
6313 }
Josef Bacik0a243252009-09-11 16:11:20 -04006314 } else if (!cached && loop > LOOP_CACHING_NOWAIT
6315 && !failed_cluster_refill) {
Josef Bacik817d52f2009-07-13 21:29:25 -04006316 spin_unlock(&last_ptr->refill_lock);
6317
Josef Bacik0a243252009-09-11 16:11:20 -04006318 failed_cluster_refill = true;
Josef Bacik817d52f2009-07-13 21:29:25 -04006319 wait_block_group_cache_progress(block_group,
6320 num_bytes + empty_cluster + empty_size);
6321 goto have_block_group;
Chris Masonfa9c0d72009-04-03 09:47:43 -04006322 }
Josef Bacik817d52f2009-07-13 21:29:25 -04006323
Chris Masonfa9c0d72009-04-03 09:47:43 -04006324 /*
6325 * at this point we either didn't find a cluster
6326 * or we weren't able to allocate a block from our
6327 * cluster. Free the cluster we've been trying
6328 * to use, and go to the next block group
6329 */
Josef Bacik0a243252009-09-11 16:11:20 -04006330 btrfs_return_cluster_to_free_space(NULL, last_ptr);
Chris Masonfa9c0d72009-04-03 09:47:43 -04006331 spin_unlock(&last_ptr->refill_lock);
Josef Bacik0a243252009-09-11 16:11:20 -04006332 goto loop;
Chris Masonfa9c0d72009-04-03 09:47:43 -04006333 }
6334
Alexandre Oliva062c05c2011-12-07 19:50:42 -05006335unclustered_alloc:
Alexandre Olivaa5f6f712011-12-12 04:48:19 -02006336 spin_lock(&block_group->free_space_ctl->tree_lock);
6337 if (cached &&
6338 block_group->free_space_ctl->free_space <
6339 num_bytes + empty_cluster + empty_size) {
6340 spin_unlock(&block_group->free_space_ctl->tree_lock);
6341 goto loop;
6342 }
6343 spin_unlock(&block_group->free_space_ctl->tree_lock);
6344
Josef Bacik6226cb02009-04-03 10:14:18 -04006345 offset = btrfs_find_space_for_alloc(block_group, search_start,
6346 num_bytes, empty_size);
Josef Bacik1cdda9b2009-10-06 10:04:28 -04006347 /*
6348 * If we didn't find a chunk, and we haven't failed on this
6349 * block group before, and this block group is in the middle of
6350 * caching and we are ok with waiting, then go ahead and wait
6351 * for progress to be made, and set failed_alloc to true.
6352 *
6353 * If failed_alloc is true then we've already waited on this
6354 * block group once and should move on to the next block group.
6355 */
6356 if (!offset && !failed_alloc && !cached &&
6357 loop > LOOP_CACHING_NOWAIT) {
Josef Bacik817d52f2009-07-13 21:29:25 -04006358 wait_block_group_cache_progress(block_group,
Josef Bacik1cdda9b2009-10-06 10:04:28 -04006359 num_bytes + empty_size);
6360 failed_alloc = true;
Josef Bacik817d52f2009-07-13 21:29:25 -04006361 goto have_block_group;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04006362 } else if (!offset) {
Miao Xie60d2adb2011-09-09 17:34:35 +08006363 if (!cached)
6364 have_caching_bg = true;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04006365 goto loop;
Josef Bacik817d52f2009-07-13 21:29:25 -04006366 }
Chris Masonfa9c0d72009-04-03 09:47:43 -04006367checks:
David Woodhouse53b381b2013-01-29 18:40:14 -05006368 search_start = stripe_align(root, used_block_group,
6369 offset, num_bytes);
Chris Masone37c9e62007-05-09 20:13:14 -04006370
Josef Bacik2552d172009-04-03 10:14:19 -04006371 /* move on to the next group */
6372 if (search_start + num_bytes >
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006373 used_block_group->key.objectid + used_block_group->key.offset) {
6374 btrfs_add_free_space(used_block_group, offset, num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04006375 goto loop;
Josef Bacik6226cb02009-04-03 10:14:18 -04006376 }
Josef Bacik80eb2342008-10-29 14:49:05 -04006377
Josef Bacik6226cb02009-04-03 10:14:18 -04006378 if (offset < search_start)
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006379 btrfs_add_free_space(used_block_group, offset,
Josef Bacik6226cb02009-04-03 10:14:18 -04006380 search_start - offset);
6381 BUG_ON(offset > search_start);
6382
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006383 ret = btrfs_update_reserved_bytes(used_block_group, num_bytes,
Josef Bacikfb25e912011-07-26 17:00:46 -04006384 alloc_type);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006385 if (ret == -EAGAIN) {
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006386 btrfs_add_free_space(used_block_group, offset, num_bytes);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006387 goto loop;
6388 }
Yan Zheng11833d62009-09-11 16:11:19 -04006389
Josef Bacik2552d172009-04-03 10:14:19 -04006390 /* we are all good, lets return */
Yan, Zhengf0486c62010-05-16 10:46:25 -04006391 ins->objectid = search_start;
6392 ins->offset = num_bytes;
6393
Josef Bacik3f7de032011-11-10 08:29:20 -05006394 trace_btrfs_reserve_extent(orig_root, block_group,
6395 search_start, num_bytes);
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006396 if (used_block_group != block_group)
6397 btrfs_put_block_group(used_block_group);
Josef Bacikd82a6f12011-05-11 15:26:06 -04006398 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04006399 break;
6400loop:
Josef Bacik0a243252009-09-11 16:11:20 -04006401 failed_cluster_refill = false;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04006402 failed_alloc = false;
Yan, Zhengb742bb82010-05-16 10:46:24 -04006403 BUG_ON(index != get_block_group_index(block_group));
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006404 if (used_block_group != block_group)
6405 btrfs_put_block_group(used_block_group);
Chris Masonfa9c0d72009-04-03 09:47:43 -04006406 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04006407 }
6408 up_read(&space_info->groups_sem);
Chris Masonf5a31e12008-11-10 11:47:09 -05006409
Miao Xie60d2adb2011-09-09 17:34:35 +08006410 if (!ins->objectid && loop >= LOOP_CACHING_WAIT && have_caching_bg)
6411 goto search;
6412
Yan, Zhengb742bb82010-05-16 10:46:24 -04006413 if (!ins->objectid && ++index < BTRFS_NR_RAID_TYPES)
6414 goto search;
6415
Josef Bacik285ff5a2012-01-13 15:27:45 -05006416 /*
Josef Bacikccf0e722009-11-10 21:23:48 -05006417 * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking
6418 * caching kthreads as we move along
Josef Bacik817d52f2009-07-13 21:29:25 -04006419 * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching
6420 * LOOP_ALLOC_CHUNK, force a chunk allocation and try again
6421 * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try
6422 * again
Chris Masonfa9c0d72009-04-03 09:47:43 -04006423 */
Josef Bacik723bda22011-05-27 16:11:38 -04006424 if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE) {
Yan, Zhengb742bb82010-05-16 10:46:24 -04006425 index = 0;
Josef Bacik723bda22011-05-27 16:11:38 -04006426 loop++;
Josef Bacik817d52f2009-07-13 21:29:25 -04006427 if (loop == LOOP_ALLOC_CHUNK) {
David Sterbab6919a52013-04-29 13:39:40 +00006428 ret = do_chunk_alloc(trans, root, flags,
Josef Bacikea658ba2012-09-11 16:57:25 -04006429 CHUNK_ALLOC_FORCE);
6430 /*
6431 * Do not bail out on ENOSPC since we
6432 * can do more things.
6433 */
6434 if (ret < 0 && ret != -ENOSPC) {
6435 btrfs_abort_transaction(trans,
6436 root, ret);
6437 goto out;
Josef Bacik723bda22011-05-27 16:11:38 -04006438 }
Josef Bacik723bda22011-05-27 16:11:38 -04006439 }
6440
6441 if (loop == LOOP_NO_EMPTY_SIZE) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04006442 empty_size = 0;
6443 empty_cluster = 0;
6444 }
Chris Mason42e70e72008-11-07 18:17:11 -05006445
Josef Bacik723bda22011-05-27 16:11:38 -04006446 goto search;
Josef Bacik2552d172009-04-03 10:14:19 -04006447 } else if (!ins->objectid) {
6448 ret = -ENOSPC;
Josef Bacikd82a6f12011-05-11 15:26:06 -04006449 } else if (ins->objectid) {
Josef Bacik2552d172009-04-03 10:14:19 -04006450 ret = 0;
6451 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006452out:
Chris Mason0b86a832008-03-24 15:01:56 -04006453
Chris Mason0f70abe2007-02-28 16:46:22 -05006454 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05006455}
Chris Masonec44a352008-04-28 15:29:52 -04006456
Josef Bacik9ed74f22009-09-11 16:12:44 -04006457static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
6458 int dump_block_groups)
Josef Bacik0f9dd462008-09-23 13:14:11 -04006459{
6460 struct btrfs_block_group_cache *cache;
Yan, Zhengb742bb82010-05-16 10:46:24 -04006461 int index = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04006462
Josef Bacik9ed74f22009-09-11 16:12:44 -04006463 spin_lock(&info->lock);
Josef Bacikfb25e912011-07-26 17:00:46 -04006464 printk(KERN_INFO "space_info %llu has %llu free, is %sfull\n",
6465 (unsigned long long)info->flags,
Chris Masond3977122009-01-05 21:25:51 -05006466 (unsigned long long)(info->total_bytes - info->bytes_used -
Josef Bacik9ed74f22009-09-11 16:12:44 -04006467 info->bytes_pinned - info->bytes_reserved -
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006468 info->bytes_readonly),
Chris Masond3977122009-01-05 21:25:51 -05006469 (info->full) ? "" : "not ");
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006470 printk(KERN_INFO "space_info total=%llu, used=%llu, pinned=%llu, "
6471 "reserved=%llu, may_use=%llu, readonly=%llu\n",
Joel Becker21380932009-04-21 12:38:29 -07006472 (unsigned long long)info->total_bytes,
Josef Bacik9ed74f22009-09-11 16:12:44 -04006473 (unsigned long long)info->bytes_used,
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006474 (unsigned long long)info->bytes_pinned,
6475 (unsigned long long)info->bytes_reserved,
6476 (unsigned long long)info->bytes_may_use,
6477 (unsigned long long)info->bytes_readonly);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006478 spin_unlock(&info->lock);
6479
6480 if (!dump_block_groups)
6481 return;
Josef Bacik0f9dd462008-09-23 13:14:11 -04006482
Josef Bacik80eb2342008-10-29 14:49:05 -04006483 down_read(&info->groups_sem);
Yan, Zhengb742bb82010-05-16 10:46:24 -04006484again:
6485 list_for_each_entry(cache, &info->block_groups[index], list) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04006486 spin_lock(&cache->lock);
Liu Bo799ffc32012-07-06 03:31:35 -06006487 printk(KERN_INFO "block group %llu has %llu bytes, %llu used %llu pinned %llu reserved %s\n",
Chris Masond3977122009-01-05 21:25:51 -05006488 (unsigned long long)cache->key.objectid,
6489 (unsigned long long)cache->key.offset,
6490 (unsigned long long)btrfs_block_group_used(&cache->item),
6491 (unsigned long long)cache->pinned,
Liu Bo799ffc32012-07-06 03:31:35 -06006492 (unsigned long long)cache->reserved,
6493 cache->ro ? "[readonly]" : "");
Josef Bacik0f9dd462008-09-23 13:14:11 -04006494 btrfs_dump_free_space(cache, bytes);
6495 spin_unlock(&cache->lock);
6496 }
Yan, Zhengb742bb82010-05-16 10:46:24 -04006497 if (++index < BTRFS_NR_RAID_TYPES)
6498 goto again;
Josef Bacik80eb2342008-10-29 14:49:05 -04006499 up_read(&info->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04006500}
Zheng Yane8569812008-09-26 10:05:48 -04006501
Yan Zheng11833d62009-09-11 16:11:19 -04006502int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
6503 struct btrfs_root *root,
6504 u64 num_bytes, u64 min_alloc_size,
6505 u64 empty_size, u64 hint_byte,
David Sterbab6919a52013-04-29 13:39:40 +00006506 struct btrfs_key *ins, int is_data)
Chris Masonfec577f2007-02-26 10:40:21 -05006507{
Miao Xie9e622d62012-01-26 15:01:12 -05006508 bool final_tried = false;
David Sterbab6919a52013-04-29 13:39:40 +00006509 u64 flags;
Chris Masonfec577f2007-02-26 10:40:21 -05006510 int ret;
Chris Mason925baed2008-06-25 16:01:30 -04006511
David Sterbab6919a52013-04-29 13:39:40 +00006512 flags = btrfs_get_alloc_profile(root, is_data);
Chris Mason98d20f62008-04-14 09:46:10 -04006513again:
Chris Masondb945352007-10-15 16:15:53 -04006514 WARN_ON(num_bytes < root->sectorsize);
6515 ret = find_free_extent(trans, root, num_bytes, empty_size,
David Sterbab6919a52013-04-29 13:39:40 +00006516 hint_byte, ins, flags);
Chris Mason3b951512008-04-17 11:29:12 -04006517
Miao Xie9e622d62012-01-26 15:01:12 -05006518 if (ret == -ENOSPC) {
6519 if (!final_tried) {
6520 num_bytes = num_bytes >> 1;
Zach Brown24542bf2012-11-16 00:04:43 +00006521 num_bytes = round_down(num_bytes, root->sectorsize);
Miao Xie9e622d62012-01-26 15:01:12 -05006522 num_bytes = max(num_bytes, min_alloc_size);
Miao Xie9e622d62012-01-26 15:01:12 -05006523 if (num_bytes == min_alloc_size)
6524 final_tried = true;
6525 goto again;
6526 } else if (btrfs_test_opt(root, ENOSPC_DEBUG)) {
6527 struct btrfs_space_info *sinfo;
Josef Bacik0f9dd462008-09-23 13:14:11 -04006528
David Sterbab6919a52013-04-29 13:39:40 +00006529 sinfo = __find_space_info(root->fs_info, flags);
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006530 btrfs_err(root->fs_info, "allocation failed flags %llu, wanted %llu",
David Sterbab6919a52013-04-29 13:39:40 +00006531 (unsigned long long)flags,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006532 (unsigned long long)num_bytes);
Jeff Mahoney53804282012-03-01 14:56:28 +01006533 if (sinfo)
6534 dump_space_info(sinfo, num_bytes, 1);
Miao Xie9e622d62012-01-26 15:01:12 -05006535 }
Chris Mason925baed2008-06-25 16:01:30 -04006536 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04006537
liubo1abe9b82011-03-24 11:18:59 +00006538 trace_btrfs_reserved_extent_alloc(root, ins->objectid, ins->offset);
6539
Josef Bacik0f9dd462008-09-23 13:14:11 -04006540 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006541}
6542
Chris Masone688b722011-10-31 20:52:39 -04006543static int __btrfs_free_reserved_extent(struct btrfs_root *root,
6544 u64 start, u64 len, int pin)
Chris Mason65b51a02008-08-01 15:11:20 -04006545{
Josef Bacik0f9dd462008-09-23 13:14:11 -04006546 struct btrfs_block_group_cache *cache;
Liu Hui1f3c79a2009-01-05 15:57:51 -05006547 int ret = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04006548
Josef Bacik0f9dd462008-09-23 13:14:11 -04006549 cache = btrfs_lookup_block_group(root->fs_info, start);
6550 if (!cache) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006551 btrfs_err(root->fs_info, "Unable to find block group for %llu",
6552 (unsigned long long)start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04006553 return -ENOSPC;
6554 }
Liu Hui1f3c79a2009-01-05 15:57:51 -05006555
Li Dongyang5378e602011-03-24 10:24:27 +00006556 if (btrfs_test_opt(root, DISCARD))
6557 ret = btrfs_discard_extent(root, start, len, NULL);
Liu Hui1f3c79a2009-01-05 15:57:51 -05006558
Chris Masone688b722011-10-31 20:52:39 -04006559 if (pin)
6560 pin_down_extent(root, cache, start, len, 1);
6561 else {
6562 btrfs_add_free_space(cache, start, len);
6563 btrfs_update_reserved_bytes(cache, len, RESERVE_FREE);
6564 }
Chris Masonfa9c0d72009-04-03 09:47:43 -04006565 btrfs_put_block_group(cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04006566
liubo1abe9b82011-03-24 11:18:59 +00006567 trace_btrfs_reserved_extent_free(root, start, len);
6568
Chris Masone6dcd2d2008-07-17 12:53:50 -04006569 return ret;
6570}
6571
Chris Masone688b722011-10-31 20:52:39 -04006572int btrfs_free_reserved_extent(struct btrfs_root *root,
6573 u64 start, u64 len)
6574{
6575 return __btrfs_free_reserved_extent(root, start, len, 0);
6576}
6577
6578int btrfs_free_and_pin_reserved_extent(struct btrfs_root *root,
6579 u64 start, u64 len)
6580{
6581 return __btrfs_free_reserved_extent(root, start, len, 1);
6582}
6583
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006584static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
6585 struct btrfs_root *root,
6586 u64 parent, u64 root_objectid,
6587 u64 flags, u64 owner, u64 offset,
6588 struct btrfs_key *ins, int ref_mod)
Chris Masone6dcd2d2008-07-17 12:53:50 -04006589{
6590 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006591 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006592 struct btrfs_extent_item *extent_item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006593 struct btrfs_extent_inline_ref *iref;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006594 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006595 struct extent_buffer *leaf;
6596 int type;
6597 u32 size;
Chris Masonf2654de2007-06-26 12:20:46 -04006598
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006599 if (parent > 0)
6600 type = BTRFS_SHARED_DATA_REF_KEY;
6601 else
6602 type = BTRFS_EXTENT_DATA_REF_KEY;
Zheng Yan31840ae2008-09-23 13:14:14 -04006603
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006604 size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type);
Chris Mason7bb86312007-12-11 09:25:06 -05006605
6606 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00006607 if (!path)
6608 return -ENOMEM;
Chris Mason47e4bb92008-02-01 14:51:59 -05006609
Chris Masonb9473432009-03-13 11:00:37 -04006610 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006611 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
6612 ins, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006613 if (ret) {
6614 btrfs_free_path(path);
6615 return ret;
6616 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04006617
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006618 leaf = path->nodes[0];
6619 extent_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason47e4bb92008-02-01 14:51:59 -05006620 struct btrfs_extent_item);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006621 btrfs_set_extent_refs(leaf, extent_item, ref_mod);
6622 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
6623 btrfs_set_extent_flags(leaf, extent_item,
6624 flags | BTRFS_EXTENT_FLAG_DATA);
Chris Mason47e4bb92008-02-01 14:51:59 -05006625
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006626 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
6627 btrfs_set_extent_inline_ref_type(leaf, iref, type);
6628 if (parent > 0) {
6629 struct btrfs_shared_data_ref *ref;
6630 ref = (struct btrfs_shared_data_ref *)(iref + 1);
6631 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
6632 btrfs_set_shared_data_ref_count(leaf, ref, ref_mod);
6633 } else {
6634 struct btrfs_extent_data_ref *ref;
6635 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
6636 btrfs_set_extent_data_ref_root(leaf, ref, root_objectid);
6637 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
6638 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
6639 btrfs_set_extent_data_ref_count(leaf, ref, ref_mod);
6640 }
Chris Mason47e4bb92008-02-01 14:51:59 -05006641
6642 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason7bb86312007-12-11 09:25:06 -05006643 btrfs_free_path(path);
Chris Masonf510cfe2007-10-15 16:14:48 -04006644
Liu Boc53d6132012-12-27 09:01:19 +00006645 ret = update_block_group(root, ins->objectid, ins->offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006646 if (ret) { /* -ENOENT, logic error */
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006647 btrfs_err(fs_info, "update block group failed for %llu %llu",
6648 (unsigned long long)ins->objectid,
6649 (unsigned long long)ins->offset);
Chris Masonf5947062008-02-04 10:10:13 -05006650 BUG();
6651 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006652 return ret;
6653}
6654
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006655static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
6656 struct btrfs_root *root,
6657 u64 parent, u64 root_objectid,
6658 u64 flags, struct btrfs_disk_key *key,
6659 int level, struct btrfs_key *ins)
6660{
6661 int ret;
6662 struct btrfs_fs_info *fs_info = root->fs_info;
6663 struct btrfs_extent_item *extent_item;
6664 struct btrfs_tree_block_info *block_info;
6665 struct btrfs_extent_inline_ref *iref;
6666 struct btrfs_path *path;
6667 struct extent_buffer *leaf;
Josef Bacik3173a182013-03-07 14:22:04 -05006668 u32 size = sizeof(*extent_item) + sizeof(*iref);
6669 bool skinny_metadata = btrfs_fs_incompat(root->fs_info,
6670 SKINNY_METADATA);
6671
6672 if (!skinny_metadata)
6673 size += sizeof(*block_info);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006674
6675 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006676 if (!path)
6677 return -ENOMEM;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006678
6679 path->leave_spinning = 1;
6680 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
6681 ins, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006682 if (ret) {
6683 btrfs_free_path(path);
6684 return ret;
6685 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006686
6687 leaf = path->nodes[0];
6688 extent_item = btrfs_item_ptr(leaf, path->slots[0],
6689 struct btrfs_extent_item);
6690 btrfs_set_extent_refs(leaf, extent_item, 1);
6691 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
6692 btrfs_set_extent_flags(leaf, extent_item,
6693 flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006694
Josef Bacik3173a182013-03-07 14:22:04 -05006695 if (skinny_metadata) {
6696 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
6697 } else {
6698 block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
6699 btrfs_set_tree_block_key(leaf, block_info, key);
6700 btrfs_set_tree_block_level(leaf, block_info, level);
6701 iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
6702 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006703
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006704 if (parent > 0) {
6705 BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
6706 btrfs_set_extent_inline_ref_type(leaf, iref,
6707 BTRFS_SHARED_BLOCK_REF_KEY);
6708 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
6709 } else {
6710 btrfs_set_extent_inline_ref_type(leaf, iref,
6711 BTRFS_TREE_BLOCK_REF_KEY);
6712 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
6713 }
6714
6715 btrfs_mark_buffer_dirty(leaf);
6716 btrfs_free_path(path);
6717
Josef Bacik3173a182013-03-07 14:22:04 -05006718 ret = update_block_group(root, ins->objectid, root->leafsize, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006719 if (ret) { /* -ENOENT, logic error */
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006720 btrfs_err(fs_info, "update block group failed for %llu %llu",
6721 (unsigned long long)ins->objectid,
6722 (unsigned long long)ins->offset);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006723 BUG();
6724 }
6725 return ret;
6726}
6727
6728int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
6729 struct btrfs_root *root,
6730 u64 root_objectid, u64 owner,
6731 u64 offset, struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04006732{
6733 int ret;
Chris Mason1c2308f2008-09-23 13:14:13 -04006734
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006735 BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID);
Chris Mason56bec292009-03-13 10:10:06 -04006736
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006737 ret = btrfs_add_delayed_data_ref(root->fs_info, trans, ins->objectid,
6738 ins->offset, 0,
6739 root_objectid, owner, offset,
6740 BTRFS_ADD_DELAYED_EXTENT, NULL, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006741 return ret;
6742}
Chris Masone02119d2008-09-05 16:13:11 -04006743
6744/*
6745 * this is used by the tree logging recovery code. It records that
6746 * an extent has been allocated and makes sure to clear the free
6747 * space cache bits as well
6748 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006749int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
6750 struct btrfs_root *root,
6751 u64 root_objectid, u64 owner, u64 offset,
6752 struct btrfs_key *ins)
Chris Masone02119d2008-09-05 16:13:11 -04006753{
6754 int ret;
6755 struct btrfs_block_group_cache *block_group;
Josef Bacik8c2a1a32013-06-06 13:19:32 -04006756
6757 /*
6758 * Mixed block groups will exclude before processing the log so we only
6759 * need to do the exlude dance if this fs isn't mixed.
6760 */
6761 if (!btrfs_fs_incompat(root->fs_info, MIXED_GROUPS)) {
6762 ret = __exclude_logged_extent(root, ins->objectid, ins->offset);
6763 if (ret)
6764 return ret;
6765 }
Chris Masone02119d2008-09-05 16:13:11 -04006766
Chris Masone02119d2008-09-05 16:13:11 -04006767 block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04006768 if (!block_group)
6769 return -EINVAL;
Yan Zheng11833d62009-09-11 16:11:19 -04006770
Josef Bacikfb25e912011-07-26 17:00:46 -04006771 ret = btrfs_update_reserved_bytes(block_group, ins->offset,
6772 RESERVE_ALLOC_NO_ACCOUNT);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006773 BUG_ON(ret); /* logic error */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006774 ret = alloc_reserved_file_extent(trans, root, 0, root_objectid,
6775 0, owner, offset, ins, 1);
Josef Bacikb50c6e22013-04-25 15:55:30 -04006776 btrfs_put_block_group(block_group);
Chris Masone02119d2008-09-05 16:13:11 -04006777 return ret;
6778}
6779
Eric Sandeen48a3b632013-04-25 20:41:01 +00006780static struct extent_buffer *
6781btrfs_init_new_buffer(struct btrfs_trans_handle *trans, struct btrfs_root *root,
6782 u64 bytenr, u32 blocksize, int level)
Chris Mason65b51a02008-08-01 15:11:20 -04006783{
6784 struct extent_buffer *buf;
6785
6786 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
6787 if (!buf)
6788 return ERR_PTR(-ENOMEM);
6789 btrfs_set_header_generation(buf, trans->transid);
Chris Mason85d4e462011-07-26 16:11:19 -04006790 btrfs_set_buffer_lockdep_class(root->root_key.objectid, buf, level);
Chris Mason65b51a02008-08-01 15:11:20 -04006791 btrfs_tree_lock(buf);
6792 clean_tree_block(trans, root, buf);
Josef Bacik3083ee22012-03-09 16:01:49 -05006793 clear_bit(EXTENT_BUFFER_STALE, &buf->bflags);
Chris Masonb4ce94d2009-02-04 09:25:08 -05006794
6795 btrfs_set_lock_blocking(buf);
Chris Mason65b51a02008-08-01 15:11:20 -04006796 btrfs_set_buffer_uptodate(buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05006797
Chris Masond0c803c2008-09-11 16:17:57 -04006798 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
Yan, Zheng8cef4e12009-11-12 09:33:26 +00006799 /*
6800 * we allow two log transactions at a time, use different
6801 * EXENT bit to differentiate dirty pages.
6802 */
6803 if (root->log_transid % 2 == 0)
6804 set_extent_dirty(&root->dirty_log_pages, buf->start,
6805 buf->start + buf->len - 1, GFP_NOFS);
6806 else
6807 set_extent_new(&root->dirty_log_pages, buf->start,
6808 buf->start + buf->len - 1, GFP_NOFS);
Chris Masond0c803c2008-09-11 16:17:57 -04006809 } else {
6810 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
6811 buf->start + buf->len - 1, GFP_NOFS);
6812 }
Chris Mason65b51a02008-08-01 15:11:20 -04006813 trans->blocks_used++;
Chris Masonb4ce94d2009-02-04 09:25:08 -05006814 /* this returns a buffer locked for blocking */
Chris Mason65b51a02008-08-01 15:11:20 -04006815 return buf;
6816}
6817
Yan, Zhengf0486c62010-05-16 10:46:25 -04006818static struct btrfs_block_rsv *
6819use_block_rsv(struct btrfs_trans_handle *trans,
6820 struct btrfs_root *root, u32 blocksize)
6821{
6822 struct btrfs_block_rsv *block_rsv;
Josef Bacik68a82272011-01-24 21:43:20 +00006823 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006824 int ret;
Miao Xied88033d2013-05-13 13:55:12 +00006825 bool global_updated = false;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006826
6827 block_rsv = get_block_rsv(trans, root);
6828
Miao Xieb586b322013-05-13 13:55:10 +00006829 if (unlikely(block_rsv->size == 0))
6830 goto try_reserve;
Miao Xied88033d2013-05-13 13:55:12 +00006831again:
Yan, Zhengf0486c62010-05-16 10:46:25 -04006832 ret = block_rsv_use_bytes(block_rsv, blocksize);
6833 if (!ret)
6834 return block_rsv;
6835
Miao Xieb586b322013-05-13 13:55:10 +00006836 if (block_rsv->failfast)
6837 return ERR_PTR(ret);
6838
Miao Xied88033d2013-05-13 13:55:12 +00006839 if (block_rsv->type == BTRFS_BLOCK_RSV_GLOBAL && !global_updated) {
6840 global_updated = true;
6841 update_global_block_rsv(root->fs_info);
6842 goto again;
6843 }
6844
Miao Xieb586b322013-05-13 13:55:10 +00006845 if (btrfs_test_opt(root, ENOSPC_DEBUG)) {
6846 static DEFINE_RATELIMIT_STATE(_rs,
6847 DEFAULT_RATELIMIT_INTERVAL * 10,
6848 /*DEFAULT_RATELIMIT_BURST*/ 1);
6849 if (__ratelimit(&_rs))
6850 WARN(1, KERN_DEBUG
6851 "btrfs: block rsv returned %d\n", ret);
6852 }
6853try_reserve:
6854 ret = reserve_metadata_bytes(root, block_rsv, blocksize,
6855 BTRFS_RESERVE_NO_FLUSH);
6856 if (!ret)
6857 return block_rsv;
6858 /*
6859 * If we couldn't reserve metadata bytes try and use some from
Miao Xie5881cfc2013-05-13 13:55:11 +00006860 * the global reserve if its space type is the same as the global
6861 * reservation.
Miao Xieb586b322013-05-13 13:55:10 +00006862 */
Miao Xie5881cfc2013-05-13 13:55:11 +00006863 if (block_rsv->type != BTRFS_BLOCK_RSV_GLOBAL &&
6864 block_rsv->space_info == global_rsv->space_info) {
Miao Xieb586b322013-05-13 13:55:10 +00006865 ret = block_rsv_use_bytes(global_rsv, blocksize);
6866 if (!ret)
6867 return global_rsv;
6868 }
6869 return ERR_PTR(ret);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006870}
6871
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05006872static void unuse_block_rsv(struct btrfs_fs_info *fs_info,
6873 struct btrfs_block_rsv *block_rsv, u32 blocksize)
Yan, Zhengf0486c62010-05-16 10:46:25 -04006874{
6875 block_rsv_add_bytes(block_rsv, blocksize, 0);
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05006876 block_rsv_release_bytes(fs_info, block_rsv, NULL, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006877}
6878
Chris Masonfec577f2007-02-26 10:40:21 -05006879/*
Yan, Zhengf0486c62010-05-16 10:46:25 -04006880 * finds a free extent and does all the dirty work required for allocation
6881 * returns the key for the extent through ins, and a tree buffer for
6882 * the first block of the extent through buf.
6883 *
Chris Masonfec577f2007-02-26 10:40:21 -05006884 * returns the tree buffer or NULL.
6885 */
Chris Mason5f39d392007-10-15 16:14:19 -04006886struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006887 struct btrfs_root *root, u32 blocksize,
6888 u64 parent, u64 root_objectid,
6889 struct btrfs_disk_key *key, int level,
Jan Schmidt5581a512012-05-16 17:04:52 +02006890 u64 hint, u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05006891{
Chris Masone2fa7222007-03-12 16:22:34 -04006892 struct btrfs_key ins;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006893 struct btrfs_block_rsv *block_rsv;
Chris Mason5f39d392007-10-15 16:14:19 -04006894 struct extent_buffer *buf;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006895 u64 flags = 0;
6896 int ret;
Josef Bacik3173a182013-03-07 14:22:04 -05006897 bool skinny_metadata = btrfs_fs_incompat(root->fs_info,
6898 SKINNY_METADATA);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006899
6900 block_rsv = use_block_rsv(trans, root, blocksize);
6901 if (IS_ERR(block_rsv))
6902 return ERR_CAST(block_rsv);
6903
6904 ret = btrfs_reserve_extent(trans, root, blocksize, blocksize,
Josef Bacik81c9ad22012-01-18 10:56:06 -05006905 empty_size, hint, &ins, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05006906 if (ret) {
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05006907 unuse_block_rsv(root->fs_info, block_rsv, blocksize);
Chris Mason54aa1f42007-06-22 14:16:25 -04006908 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05006909 }
Chris Mason55c69072008-01-09 15:55:33 -05006910
Chris Mason4008c042009-02-12 14:09:45 -05006911 buf = btrfs_init_new_buffer(trans, root, ins.objectid,
6912 blocksize, level);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006913 BUG_ON(IS_ERR(buf)); /* -ENOMEM */
Yan, Zhengf0486c62010-05-16 10:46:25 -04006914
6915 if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
6916 if (parent == 0)
6917 parent = ins.objectid;
6918 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
6919 } else
6920 BUG_ON(parent > 0);
6921
6922 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
6923 struct btrfs_delayed_extent_op *extent_op;
Miao Xie78a61842012-11-21 02:21:28 +00006924 extent_op = btrfs_alloc_delayed_extent_op();
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006925 BUG_ON(!extent_op); /* -ENOMEM */
Yan, Zhengf0486c62010-05-16 10:46:25 -04006926 if (key)
6927 memcpy(&extent_op->key, key, sizeof(extent_op->key));
6928 else
6929 memset(&extent_op->key, 0, sizeof(extent_op->key));
6930 extent_op->flags_to_set = flags;
Josef Bacik3173a182013-03-07 14:22:04 -05006931 if (skinny_metadata)
6932 extent_op->update_key = 0;
6933 else
6934 extent_op->update_key = 1;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006935 extent_op->update_flags = 1;
6936 extent_op->is_data = 0;
Josef Bacikb1c79e02013-05-09 13:49:30 -04006937 extent_op->level = level;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006938
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006939 ret = btrfs_add_delayed_tree_ref(root->fs_info, trans,
6940 ins.objectid,
Yan, Zhengf0486c62010-05-16 10:46:25 -04006941 ins.offset, parent, root_objectid,
6942 level, BTRFS_ADD_DELAYED_EXTENT,
Jan Schmidt5581a512012-05-16 17:04:52 +02006943 extent_op, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006944 BUG_ON(ret); /* -ENOMEM */
Yan, Zhengf0486c62010-05-16 10:46:25 -04006945 }
Chris Masonfec577f2007-02-26 10:40:21 -05006946 return buf;
6947}
Chris Masona28ec192007-03-06 20:08:01 -05006948
Yan Zheng2c47e6052009-06-27 21:07:35 -04006949struct walk_control {
6950 u64 refs[BTRFS_MAX_LEVEL];
6951 u64 flags[BTRFS_MAX_LEVEL];
6952 struct btrfs_key update_progress;
6953 int stage;
6954 int level;
6955 int shared_level;
6956 int update_ref;
6957 int keep_locks;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006958 int reada_slot;
6959 int reada_count;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006960 int for_reloc;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006961};
6962
6963#define DROP_REFERENCE 1
6964#define UPDATE_BACKREF 2
6965
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006966static noinline void reada_walk_down(struct btrfs_trans_handle *trans,
6967 struct btrfs_root *root,
6968 struct walk_control *wc,
6969 struct btrfs_path *path)
6970{
6971 u64 bytenr;
6972 u64 generation;
6973 u64 refs;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006974 u64 flags;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006975 u32 nritems;
6976 u32 blocksize;
6977 struct btrfs_key key;
6978 struct extent_buffer *eb;
6979 int ret;
6980 int slot;
6981 int nread = 0;
6982
6983 if (path->slots[wc->level] < wc->reada_slot) {
6984 wc->reada_count = wc->reada_count * 2 / 3;
6985 wc->reada_count = max(wc->reada_count, 2);
6986 } else {
6987 wc->reada_count = wc->reada_count * 3 / 2;
6988 wc->reada_count = min_t(int, wc->reada_count,
6989 BTRFS_NODEPTRS_PER_BLOCK(root));
6990 }
6991
6992 eb = path->nodes[wc->level];
6993 nritems = btrfs_header_nritems(eb);
6994 blocksize = btrfs_level_size(root, wc->level - 1);
6995
6996 for (slot = path->slots[wc->level]; slot < nritems; slot++) {
6997 if (nread >= wc->reada_count)
6998 break;
6999
7000 cond_resched();
7001 bytenr = btrfs_node_blockptr(eb, slot);
7002 generation = btrfs_node_ptr_generation(eb, slot);
7003
7004 if (slot == path->slots[wc->level])
7005 goto reada;
7006
7007 if (wc->stage == UPDATE_BACKREF &&
7008 generation <= root->root_key.offset)
7009 continue;
7010
Yan, Zheng94fcca92009-10-09 09:25:16 -04007011 /* We don't lock the tree block, it's OK to be racy here */
Josef Bacik3173a182013-03-07 14:22:04 -05007012 ret = btrfs_lookup_extent_info(trans, root, bytenr,
7013 wc->level - 1, 1, &refs,
7014 &flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007015 /* We don't care about errors in readahead. */
7016 if (ret < 0)
7017 continue;
Yan, Zheng94fcca92009-10-09 09:25:16 -04007018 BUG_ON(refs == 0);
7019
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007020 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007021 if (refs == 1)
7022 goto reada;
7023
Yan, Zheng94fcca92009-10-09 09:25:16 -04007024 if (wc->level == 1 &&
7025 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
7026 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007027 if (!wc->update_ref ||
7028 generation <= root->root_key.offset)
7029 continue;
7030 btrfs_node_key_to_cpu(eb, &key, slot);
7031 ret = btrfs_comp_cpu_keys(&key,
7032 &wc->update_progress);
7033 if (ret < 0)
7034 continue;
Yan, Zheng94fcca92009-10-09 09:25:16 -04007035 } else {
7036 if (wc->level == 1 &&
7037 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
7038 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007039 }
7040reada:
7041 ret = readahead_tree_block(root, bytenr, blocksize,
7042 generation);
7043 if (ret)
7044 break;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007045 nread++;
7046 }
7047 wc->reada_slot = slot;
7048}
7049
Chris Mason9aca1d52007-03-13 11:09:37 -04007050/*
Liu Bo2c016dc2012-12-26 15:32:17 +08007051 * helper to process tree block while walking down the tree.
Yan Zheng2c47e6052009-06-27 21:07:35 -04007052 *
Yan Zheng2c47e6052009-06-27 21:07:35 -04007053 * when wc->stage == UPDATE_BACKREF, this function updates
7054 * back refs for pointers in the block.
7055 *
7056 * NOTE: return value 1 means we should stop walking down.
Yan Zhengf82d02d2008-10-29 14:49:05 -04007057 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04007058static noinline int walk_down_proc(struct btrfs_trans_handle *trans,
7059 struct btrfs_root *root,
7060 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04007061 struct walk_control *wc, int lookup_info)
Yan Zheng2c47e6052009-06-27 21:07:35 -04007062{
7063 int level = wc->level;
7064 struct extent_buffer *eb = path->nodes[level];
Yan Zheng2c47e6052009-06-27 21:07:35 -04007065 u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF;
7066 int ret;
7067
7068 if (wc->stage == UPDATE_BACKREF &&
7069 btrfs_header_owner(eb) != root->root_key.objectid)
7070 return 1;
7071
7072 /*
7073 * when reference count of tree block is 1, it won't increase
7074 * again. once full backref flag is set, we never clear it.
7075 */
Yan, Zheng94fcca92009-10-09 09:25:16 -04007076 if (lookup_info &&
7077 ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) ||
7078 (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04007079 BUG_ON(!path->locks[level]);
7080 ret = btrfs_lookup_extent_info(trans, root,
Josef Bacik3173a182013-03-07 14:22:04 -05007081 eb->start, level, 1,
Yan Zheng2c47e6052009-06-27 21:07:35 -04007082 &wc->refs[level],
7083 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007084 BUG_ON(ret == -ENOMEM);
7085 if (ret)
7086 return ret;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007087 BUG_ON(wc->refs[level] == 0);
7088 }
7089
Yan Zheng2c47e6052009-06-27 21:07:35 -04007090 if (wc->stage == DROP_REFERENCE) {
7091 if (wc->refs[level] > 1)
7092 return 1;
7093
7094 if (path->locks[level] && !wc->keep_locks) {
Chris Masonbd681512011-07-16 15:23:14 -04007095 btrfs_tree_unlock_rw(eb, path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007096 path->locks[level] = 0;
7097 }
7098 return 0;
7099 }
7100
7101 /* wc->stage == UPDATE_BACKREF */
7102 if (!(wc->flags[level] & flag)) {
7103 BUG_ON(!path->locks[level]);
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007104 ret = btrfs_inc_ref(trans, root, eb, 1, wc->for_reloc);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007105 BUG_ON(ret); /* -ENOMEM */
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007106 ret = btrfs_dec_ref(trans, root, eb, 0, wc->for_reloc);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007107 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2c47e6052009-06-27 21:07:35 -04007108 ret = btrfs_set_disk_extent_flags(trans, root, eb->start,
Josef Bacikb1c79e02013-05-09 13:49:30 -04007109 eb->len, flag,
7110 btrfs_header_level(eb), 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007111 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2c47e6052009-06-27 21:07:35 -04007112 wc->flags[level] |= flag;
7113 }
7114
7115 /*
7116 * the block is shared by multiple trees, so it's not good to
7117 * keep the tree lock
7118 */
7119 if (path->locks[level] && level > 0) {
Chris Masonbd681512011-07-16 15:23:14 -04007120 btrfs_tree_unlock_rw(eb, path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007121 path->locks[level] = 0;
7122 }
7123 return 0;
7124}
7125
7126/*
Liu Bo2c016dc2012-12-26 15:32:17 +08007127 * helper to process tree block pointer.
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007128 *
7129 * when wc->stage == DROP_REFERENCE, this function checks
7130 * reference count of the block pointed to. if the block
7131 * is shared and we need update back refs for the subtree
7132 * rooted at the block, this function changes wc->stage to
7133 * UPDATE_BACKREF. if the block is shared and there is no
7134 * need to update back, this function drops the reference
7135 * to the block.
7136 *
7137 * NOTE: return value 1 means we should stop walking down.
7138 */
7139static noinline int do_walk_down(struct btrfs_trans_handle *trans,
7140 struct btrfs_root *root,
7141 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04007142 struct walk_control *wc, int *lookup_info)
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007143{
7144 u64 bytenr;
7145 u64 generation;
7146 u64 parent;
7147 u32 blocksize;
7148 struct btrfs_key key;
7149 struct extent_buffer *next;
7150 int level = wc->level;
7151 int reada = 0;
7152 int ret = 0;
7153
7154 generation = btrfs_node_ptr_generation(path->nodes[level],
7155 path->slots[level]);
7156 /*
7157 * if the lower level block was created before the snapshot
7158 * was created, we know there is no need to update back refs
7159 * for the subtree
7160 */
7161 if (wc->stage == UPDATE_BACKREF &&
Yan, Zheng94fcca92009-10-09 09:25:16 -04007162 generation <= root->root_key.offset) {
7163 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007164 return 1;
Yan, Zheng94fcca92009-10-09 09:25:16 -04007165 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007166
7167 bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]);
7168 blocksize = btrfs_level_size(root, level - 1);
7169
7170 next = btrfs_find_tree_block(root, bytenr, blocksize);
7171 if (!next) {
7172 next = btrfs_find_create_tree_block(root, bytenr, blocksize);
Miao Xie90d2c512010-03-25 12:37:12 +00007173 if (!next)
7174 return -ENOMEM;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007175 reada = 1;
7176 }
7177 btrfs_tree_lock(next);
7178 btrfs_set_lock_blocking(next);
7179
Josef Bacik3173a182013-03-07 14:22:04 -05007180 ret = btrfs_lookup_extent_info(trans, root, bytenr, level - 1, 1,
Yan, Zheng94fcca92009-10-09 09:25:16 -04007181 &wc->refs[level - 1],
7182 &wc->flags[level - 1]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007183 if (ret < 0) {
7184 btrfs_tree_unlock(next);
7185 return ret;
7186 }
7187
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00007188 if (unlikely(wc->refs[level - 1] == 0)) {
7189 btrfs_err(root->fs_info, "Missing references.");
7190 BUG();
7191 }
Yan, Zheng94fcca92009-10-09 09:25:16 -04007192 *lookup_info = 0;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007193
Yan, Zheng94fcca92009-10-09 09:25:16 -04007194 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007195 if (wc->refs[level - 1] > 1) {
Yan, Zheng94fcca92009-10-09 09:25:16 -04007196 if (level == 1 &&
7197 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
7198 goto skip;
7199
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007200 if (!wc->update_ref ||
7201 generation <= root->root_key.offset)
7202 goto skip;
7203
7204 btrfs_node_key_to_cpu(path->nodes[level], &key,
7205 path->slots[level]);
7206 ret = btrfs_comp_cpu_keys(&key, &wc->update_progress);
7207 if (ret < 0)
7208 goto skip;
7209
7210 wc->stage = UPDATE_BACKREF;
7211 wc->shared_level = level - 1;
7212 }
Yan, Zheng94fcca92009-10-09 09:25:16 -04007213 } else {
7214 if (level == 1 &&
7215 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
7216 goto skip;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007217 }
7218
Chris Masonb9fab912012-05-06 07:23:47 -04007219 if (!btrfs_buffer_uptodate(next, generation, 0)) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007220 btrfs_tree_unlock(next);
7221 free_extent_buffer(next);
7222 next = NULL;
Yan, Zheng94fcca92009-10-09 09:25:16 -04007223 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007224 }
7225
7226 if (!next) {
7227 if (reada && level == 1)
7228 reada_walk_down(trans, root, wc, path);
7229 next = read_tree_block(root, bytenr, blocksize, generation);
Josef Bacik416bc652013-04-23 14:17:42 -04007230 if (!next || !extent_buffer_uptodate(next)) {
7231 free_extent_buffer(next);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00007232 return -EIO;
Josef Bacik416bc652013-04-23 14:17:42 -04007233 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007234 btrfs_tree_lock(next);
7235 btrfs_set_lock_blocking(next);
7236 }
7237
7238 level--;
7239 BUG_ON(level != btrfs_header_level(next));
7240 path->nodes[level] = next;
7241 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04007242 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007243 wc->level = level;
7244 if (wc->level == 1)
7245 wc->reada_slot = 0;
7246 return 0;
7247skip:
7248 wc->refs[level - 1] = 0;
7249 wc->flags[level - 1] = 0;
Yan, Zheng94fcca92009-10-09 09:25:16 -04007250 if (wc->stage == DROP_REFERENCE) {
7251 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
7252 parent = path->nodes[level]->start;
7253 } else {
7254 BUG_ON(root->root_key.objectid !=
7255 btrfs_header_owner(path->nodes[level]));
7256 parent = 0;
7257 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007258
Yan, Zheng94fcca92009-10-09 09:25:16 -04007259 ret = btrfs_free_extent(trans, root, bytenr, blocksize, parent,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007260 root->root_key.objectid, level - 1, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007261 BUG_ON(ret); /* -ENOMEM */
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007262 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007263 btrfs_tree_unlock(next);
7264 free_extent_buffer(next);
Yan, Zheng94fcca92009-10-09 09:25:16 -04007265 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007266 return 1;
7267}
7268
7269/*
Liu Bo2c016dc2012-12-26 15:32:17 +08007270 * helper to process tree block while walking up the tree.
Yan Zheng2c47e6052009-06-27 21:07:35 -04007271 *
7272 * when wc->stage == DROP_REFERENCE, this function drops
7273 * reference count on the block.
7274 *
7275 * when wc->stage == UPDATE_BACKREF, this function changes
7276 * wc->stage back to DROP_REFERENCE if we changed wc->stage
7277 * to UPDATE_BACKREF previously while processing the block.
7278 *
7279 * NOTE: return value 1 means we should stop walking up.
7280 */
7281static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
7282 struct btrfs_root *root,
7283 struct btrfs_path *path,
7284 struct walk_control *wc)
7285{
Yan, Zhengf0486c62010-05-16 10:46:25 -04007286 int ret;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007287 int level = wc->level;
7288 struct extent_buffer *eb = path->nodes[level];
7289 u64 parent = 0;
7290
7291 if (wc->stage == UPDATE_BACKREF) {
7292 BUG_ON(wc->shared_level < level);
7293 if (level < wc->shared_level)
7294 goto out;
7295
Yan Zheng2c47e6052009-06-27 21:07:35 -04007296 ret = find_next_key(path, level + 1, &wc->update_progress);
7297 if (ret > 0)
7298 wc->update_ref = 0;
7299
7300 wc->stage = DROP_REFERENCE;
7301 wc->shared_level = -1;
7302 path->slots[level] = 0;
7303
7304 /*
7305 * check reference count again if the block isn't locked.
7306 * we should start walking down the tree again if reference
7307 * count is one.
7308 */
7309 if (!path->locks[level]) {
7310 BUG_ON(level == 0);
7311 btrfs_tree_lock(eb);
7312 btrfs_set_lock_blocking(eb);
Chris Masonbd681512011-07-16 15:23:14 -04007313 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007314
7315 ret = btrfs_lookup_extent_info(trans, root,
Josef Bacik3173a182013-03-07 14:22:04 -05007316 eb->start, level, 1,
Yan Zheng2c47e6052009-06-27 21:07:35 -04007317 &wc->refs[level],
7318 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007319 if (ret < 0) {
7320 btrfs_tree_unlock_rw(eb, path->locks[level]);
Liu Bo3268a242012-12-28 09:33:19 +00007321 path->locks[level] = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007322 return ret;
7323 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007324 BUG_ON(wc->refs[level] == 0);
7325 if (wc->refs[level] == 1) {
Chris Masonbd681512011-07-16 15:23:14 -04007326 btrfs_tree_unlock_rw(eb, path->locks[level]);
Liu Bo3268a242012-12-28 09:33:19 +00007327 path->locks[level] = 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007328 return 1;
7329 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007330 }
7331 }
7332
7333 /* wc->stage == DROP_REFERENCE */
7334 BUG_ON(wc->refs[level] > 1 && !path->locks[level]);
7335
7336 if (wc->refs[level] == 1) {
7337 if (level == 0) {
7338 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007339 ret = btrfs_dec_ref(trans, root, eb, 1,
7340 wc->for_reloc);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007341 else
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007342 ret = btrfs_dec_ref(trans, root, eb, 0,
7343 wc->for_reloc);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007344 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2c47e6052009-06-27 21:07:35 -04007345 }
7346 /* make block locked assertion in clean_tree_block happy */
7347 if (!path->locks[level] &&
7348 btrfs_header_generation(eb) == trans->transid) {
7349 btrfs_tree_lock(eb);
7350 btrfs_set_lock_blocking(eb);
Chris Masonbd681512011-07-16 15:23:14 -04007351 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007352 }
7353 clean_tree_block(trans, root, eb);
7354 }
7355
7356 if (eb == root->node) {
7357 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
7358 parent = eb->start;
7359 else
7360 BUG_ON(root->root_key.objectid !=
7361 btrfs_header_owner(eb));
7362 } else {
7363 if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
7364 parent = path->nodes[level + 1]->start;
7365 else
7366 BUG_ON(root->root_key.objectid !=
7367 btrfs_header_owner(path->nodes[level + 1]));
7368 }
7369
Jan Schmidt5581a512012-05-16 17:04:52 +02007370 btrfs_free_tree_block(trans, root, eb, parent, wc->refs[level] == 1);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007371out:
7372 wc->refs[level] = 0;
7373 wc->flags[level] = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007374 return 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007375}
7376
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007377static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
7378 struct btrfs_root *root,
Yan Zheng2c47e6052009-06-27 21:07:35 -04007379 struct btrfs_path *path,
7380 struct walk_control *wc)
Yan Zhengf82d02d2008-10-29 14:49:05 -04007381{
Yan Zheng2c47e6052009-06-27 21:07:35 -04007382 int level = wc->level;
Yan, Zheng94fcca92009-10-09 09:25:16 -04007383 int lookup_info = 1;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007384 int ret;
7385
Yan Zheng2c47e6052009-06-27 21:07:35 -04007386 while (level >= 0) {
Yan, Zheng94fcca92009-10-09 09:25:16 -04007387 ret = walk_down_proc(trans, root, path, wc, lookup_info);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007388 if (ret > 0)
Yan Zhengf82d02d2008-10-29 14:49:05 -04007389 break;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007390
Yan Zheng2c47e6052009-06-27 21:07:35 -04007391 if (level == 0)
7392 break;
7393
Yan, Zheng7a7965f2010-02-01 02:41:17 +00007394 if (path->slots[level] >=
7395 btrfs_header_nritems(path->nodes[level]))
7396 break;
7397
Yan, Zheng94fcca92009-10-09 09:25:16 -04007398 ret = do_walk_down(trans, root, path, wc, &lookup_info);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007399 if (ret > 0) {
7400 path->slots[level]++;
7401 continue;
Miao Xie90d2c512010-03-25 12:37:12 +00007402 } else if (ret < 0)
7403 return ret;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007404 level = wc->level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007405 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04007406 return 0;
7407}
7408
Chris Masond3977122009-01-05 21:25:51 -05007409static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05007410 struct btrfs_root *root,
Yan Zhengf82d02d2008-10-29 14:49:05 -04007411 struct btrfs_path *path,
Yan Zheng2c47e6052009-06-27 21:07:35 -04007412 struct walk_control *wc, int max_level)
Chris Mason20524f02007-03-10 06:35:47 -05007413{
Yan Zheng2c47e6052009-06-27 21:07:35 -04007414 int level = wc->level;
Chris Mason20524f02007-03-10 06:35:47 -05007415 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04007416
Yan Zheng2c47e6052009-06-27 21:07:35 -04007417 path->slots[level] = btrfs_header_nritems(path->nodes[level]);
7418 while (level < max_level && path->nodes[level]) {
7419 wc->level = level;
7420 if (path->slots[level] + 1 <
7421 btrfs_header_nritems(path->nodes[level])) {
7422 path->slots[level]++;
Chris Mason20524f02007-03-10 06:35:47 -05007423 return 0;
7424 } else {
Yan Zheng2c47e6052009-06-27 21:07:35 -04007425 ret = walk_up_proc(trans, root, path, wc);
7426 if (ret > 0)
7427 return 0;
Chris Masonbd56b302009-02-04 09:27:02 -05007428
Yan Zheng2c47e6052009-06-27 21:07:35 -04007429 if (path->locks[level]) {
Chris Masonbd681512011-07-16 15:23:14 -04007430 btrfs_tree_unlock_rw(path->nodes[level],
7431 path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007432 path->locks[level] = 0;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007433 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007434 free_extent_buffer(path->nodes[level]);
7435 path->nodes[level] = NULL;
7436 level++;
Chris Mason20524f02007-03-10 06:35:47 -05007437 }
7438 }
7439 return 1;
7440}
7441
Chris Mason9aca1d52007-03-13 11:09:37 -04007442/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04007443 * drop a subvolume tree.
7444 *
7445 * this function traverses the tree freeing any blocks that only
7446 * referenced by the tree.
7447 *
7448 * when a shared tree block is found. this function decreases its
7449 * reference count by one. if update_ref is true, this function
7450 * also make sure backrefs for the shared block and all lower level
7451 * blocks are properly updated.
David Sterba9d1a2a32013-03-12 15:13:28 +00007452 *
7453 * If called with for_reloc == 0, may exit early with -EAGAIN
Chris Mason9aca1d52007-03-13 11:09:37 -04007454 */
Jeff Mahoney2c536792011-10-03 23:22:41 -04007455int btrfs_drop_snapshot(struct btrfs_root *root,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007456 struct btrfs_block_rsv *block_rsv, int update_ref,
7457 int for_reloc)
Chris Mason20524f02007-03-10 06:35:47 -05007458{
Chris Mason5caf2a02007-04-02 11:20:42 -04007459 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007460 struct btrfs_trans_handle *trans;
7461 struct btrfs_root *tree_root = root->fs_info->tree_root;
Chris Mason9f3a7422007-08-07 15:52:19 -04007462 struct btrfs_root_item *root_item = &root->root_item;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007463 struct walk_control *wc;
7464 struct btrfs_key key;
7465 int err = 0;
7466 int ret;
7467 int level;
Chris Mason20524f02007-03-10 06:35:47 -05007468
Chris Mason5caf2a02007-04-02 11:20:42 -04007469 path = btrfs_alloc_path();
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00007470 if (!path) {
7471 err = -ENOMEM;
7472 goto out;
7473 }
Chris Mason20524f02007-03-10 06:35:47 -05007474
Yan Zheng2c47e6052009-06-27 21:07:35 -04007475 wc = kzalloc(sizeof(*wc), GFP_NOFS);
Mark Fasheh38a1a912011-07-13 10:59:59 -07007476 if (!wc) {
7477 btrfs_free_path(path);
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00007478 err = -ENOMEM;
7479 goto out;
Mark Fasheh38a1a912011-07-13 10:59:59 -07007480 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007481
Yan, Zhenga22285a2010-05-16 10:48:46 -04007482 trans = btrfs_start_transaction(tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007483 if (IS_ERR(trans)) {
7484 err = PTR_ERR(trans);
7485 goto out_free;
7486 }
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00007487
Yan, Zheng3fd0a552010-05-16 10:49:59 -04007488 if (block_rsv)
7489 trans->block_rsv = block_rsv;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007490
Chris Mason9f3a7422007-08-07 15:52:19 -04007491 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04007492 level = btrfs_header_level(root->node);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007493 path->nodes[level] = btrfs_lock_root_node(root);
7494 btrfs_set_lock_blocking(path->nodes[level]);
Chris Mason9f3a7422007-08-07 15:52:19 -04007495 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04007496 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007497 memset(&wc->update_progress, 0,
7498 sizeof(wc->update_progress));
Chris Mason9f3a7422007-08-07 15:52:19 -04007499 } else {
Chris Mason9f3a7422007-08-07 15:52:19 -04007500 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007501 memcpy(&wc->update_progress, &key,
7502 sizeof(wc->update_progress));
7503
Chris Mason6702ed42007-08-07 16:15:09 -04007504 level = root_item->drop_level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007505 BUG_ON(level == 0);
Chris Mason6702ed42007-08-07 16:15:09 -04007506 path->lowest_level = level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007507 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
7508 path->lowest_level = 0;
7509 if (ret < 0) {
7510 err = ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007511 goto out_end_trans;
Chris Mason9f3a7422007-08-07 15:52:19 -04007512 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007513 WARN_ON(ret > 0);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007514
Chris Mason7d9eb122008-07-08 14:19:17 -04007515 /*
7516 * unlock our path, this is safe because only this
7517 * function is allowed to delete this snapshot
7518 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007519 btrfs_unlock_up_safe(path, 0);
Chris Mason9aca1d52007-03-13 11:09:37 -04007520
Yan Zheng2c47e6052009-06-27 21:07:35 -04007521 level = btrfs_header_level(root->node);
7522 while (1) {
7523 btrfs_tree_lock(path->nodes[level]);
7524 btrfs_set_lock_blocking(path->nodes[level]);
7525
7526 ret = btrfs_lookup_extent_info(trans, root,
7527 path->nodes[level]->start,
Josef Bacik3173a182013-03-07 14:22:04 -05007528 level, 1, &wc->refs[level],
Yan Zheng2c47e6052009-06-27 21:07:35 -04007529 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007530 if (ret < 0) {
7531 err = ret;
7532 goto out_end_trans;
7533 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007534 BUG_ON(wc->refs[level] == 0);
7535
7536 if (level == root_item->drop_level)
7537 break;
7538
7539 btrfs_tree_unlock(path->nodes[level]);
7540 WARN_ON(wc->refs[level] != 1);
7541 level--;
7542 }
7543 }
7544
7545 wc->level = level;
7546 wc->shared_level = -1;
7547 wc->stage = DROP_REFERENCE;
7548 wc->update_ref = update_ref;
7549 wc->keep_locks = 0;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007550 wc->for_reloc = for_reloc;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007551 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007552
7553 while (1) {
Miao Xiebabbf172013-05-14 10:20:43 +00007554 if (!for_reloc && btrfs_need_cleaner_sleep(root)) {
David Sterba9d1a2a32013-03-12 15:13:28 +00007555 pr_debug("btrfs: drop snapshot early exit\n");
7556 err = -EAGAIN;
7557 goto out_end_trans;
7558 }
7559
Yan Zheng2c47e6052009-06-27 21:07:35 -04007560 ret = walk_down_tree(trans, root, path, wc);
7561 if (ret < 0) {
7562 err = ret;
Chris Masone7a84562008-06-25 16:01:31 -04007563 break;
7564 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007565
7566 ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL);
7567 if (ret < 0) {
7568 err = ret;
7569 break;
7570 }
7571
7572 if (ret > 0) {
7573 BUG_ON(wc->stage != DROP_REFERENCE);
7574 break;
7575 }
7576
7577 if (wc->stage == DROP_REFERENCE) {
7578 level = wc->level;
7579 btrfs_node_key(path->nodes[level],
7580 &root_item->drop_progress,
7581 path->slots[level]);
7582 root_item->drop_level = level;
7583 }
7584
7585 BUG_ON(wc->level == 0);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04007586 if (btrfs_should_end_transaction(trans, tree_root)) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04007587 ret = btrfs_update_root(trans, tree_root,
7588 &root->root_key,
7589 root_item);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007590 if (ret) {
7591 btrfs_abort_transaction(trans, tree_root, ret);
7592 err = ret;
7593 goto out_end_trans;
7594 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007595
Yan, Zheng3fd0a552010-05-16 10:49:59 -04007596 btrfs_end_transaction_throttle(trans, tree_root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04007597 trans = btrfs_start_transaction(tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007598 if (IS_ERR(trans)) {
7599 err = PTR_ERR(trans);
7600 goto out_free;
7601 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04007602 if (block_rsv)
7603 trans->block_rsv = block_rsv;
Chris Masonc3e69d52009-03-13 10:17:05 -04007604 }
Chris Mason20524f02007-03-10 06:35:47 -05007605 }
David Sterbab3b4aa72011-04-21 01:20:15 +02007606 btrfs_release_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007607 if (err)
7608 goto out_end_trans;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007609
7610 ret = btrfs_del_root(trans, tree_root, &root->root_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007611 if (ret) {
7612 btrfs_abort_transaction(trans, tree_root, ret);
7613 goto out_end_trans;
7614 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007615
Yan, Zheng76dda932009-09-21 16:00:26 -04007616 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
Miao Xiecb517ea2013-05-15 07:48:19 +00007617 ret = btrfs_find_root(tree_root, &root->root_key, path,
7618 NULL, NULL);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007619 if (ret < 0) {
7620 btrfs_abort_transaction(trans, tree_root, ret);
7621 err = ret;
7622 goto out_end_trans;
7623 } else if (ret > 0) {
Josef Bacik84cd9482010-12-08 12:24:01 -05007624 /* if we fail to delete the orphan item this time
7625 * around, it'll get picked up the next time.
7626 *
7627 * The most common failure here is just -ENOENT.
7628 */
7629 btrfs_del_orphan_item(trans, tree_root,
7630 root->root_key.objectid);
Yan, Zheng76dda932009-09-21 16:00:26 -04007631 }
7632 }
7633
7634 if (root->in_radix) {
Miao Xiecb517ea2013-05-15 07:48:19 +00007635 btrfs_drop_and_free_fs_root(tree_root->fs_info, root);
Yan, Zheng76dda932009-09-21 16:00:26 -04007636 } else {
7637 free_extent_buffer(root->node);
7638 free_extent_buffer(root->commit_root);
Miao Xieb0feb9d2013-05-15 07:48:20 +00007639 btrfs_put_fs_root(root);
Yan, Zheng76dda932009-09-21 16:00:26 -04007640 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007641out_end_trans:
Yan, Zheng3fd0a552010-05-16 10:49:59 -04007642 btrfs_end_transaction_throttle(trans, tree_root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007643out_free:
Yan Zheng2c47e6052009-06-27 21:07:35 -04007644 kfree(wc);
Chris Mason5caf2a02007-04-02 11:20:42 -04007645 btrfs_free_path(path);
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00007646out:
7647 if (err)
7648 btrfs_std_error(root->fs_info, err);
Jeff Mahoney2c536792011-10-03 23:22:41 -04007649 return err;
Chris Mason20524f02007-03-10 06:35:47 -05007650}
Chris Mason9078a3e2007-04-26 16:46:15 -04007651
Yan Zheng2c47e6052009-06-27 21:07:35 -04007652/*
7653 * drop subtree rooted at tree block 'node'.
7654 *
7655 * NOTE: this function will unlock and release tree block 'node'
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007656 * only used by relocation code
Yan Zheng2c47e6052009-06-27 21:07:35 -04007657 */
Yan Zhengf82d02d2008-10-29 14:49:05 -04007658int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
7659 struct btrfs_root *root,
7660 struct extent_buffer *node,
7661 struct extent_buffer *parent)
7662{
7663 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007664 struct walk_control *wc;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007665 int level;
7666 int parent_level;
7667 int ret = 0;
7668 int wret;
7669
Yan Zheng2c47e6052009-06-27 21:07:35 -04007670 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
7671
Yan Zhengf82d02d2008-10-29 14:49:05 -04007672 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00007673 if (!path)
7674 return -ENOMEM;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007675
Yan Zheng2c47e6052009-06-27 21:07:35 -04007676 wc = kzalloc(sizeof(*wc), GFP_NOFS);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00007677 if (!wc) {
7678 btrfs_free_path(path);
7679 return -ENOMEM;
7680 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007681
Chris Masonb9447ef2009-03-09 11:45:38 -04007682 btrfs_assert_tree_locked(parent);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007683 parent_level = btrfs_header_level(parent);
7684 extent_buffer_get(parent);
7685 path->nodes[parent_level] = parent;
7686 path->slots[parent_level] = btrfs_header_nritems(parent);
7687
Chris Masonb9447ef2009-03-09 11:45:38 -04007688 btrfs_assert_tree_locked(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007689 level = btrfs_header_level(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007690 path->nodes[level] = node;
7691 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04007692 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007693
7694 wc->refs[parent_level] = 1;
7695 wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF;
7696 wc->level = level;
7697 wc->shared_level = -1;
7698 wc->stage = DROP_REFERENCE;
7699 wc->update_ref = 0;
7700 wc->keep_locks = 1;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007701 wc->for_reloc = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007702 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007703
7704 while (1) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04007705 wret = walk_down_tree(trans, root, path, wc);
7706 if (wret < 0) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04007707 ret = wret;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007708 break;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007709 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04007710
Yan Zheng2c47e6052009-06-27 21:07:35 -04007711 wret = walk_up_tree(trans, root, path, wc, parent_level);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007712 if (wret < 0)
7713 ret = wret;
7714 if (wret != 0)
7715 break;
7716 }
7717
Yan Zheng2c47e6052009-06-27 21:07:35 -04007718 kfree(wc);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007719 btrfs_free_path(path);
7720 return ret;
7721}
7722
Chris Masonec44a352008-04-28 15:29:52 -04007723static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
7724{
7725 u64 num_devices;
Ilya Dryomovfc67c452012-03-27 17:09:17 +03007726 u64 stripped;
Chris Masonec44a352008-04-28 15:29:52 -04007727
Ilya Dryomovfc67c452012-03-27 17:09:17 +03007728 /*
7729 * if restripe for this chunk_type is on pick target profile and
7730 * return, otherwise do the usual balance
7731 */
7732 stripped = get_restripe_target(root->fs_info, flags);
7733 if (stripped)
7734 return extended_to_chunk(stripped);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02007735
Chris Masoncd02dca2010-12-13 14:56:23 -05007736 /*
7737 * we add in the count of missing devices because we want
7738 * to make sure that any RAID levels on a degraded FS
7739 * continue to be honored.
7740 */
7741 num_devices = root->fs_info->fs_devices->rw_devices +
7742 root->fs_info->fs_devices->missing_devices;
7743
Ilya Dryomovfc67c452012-03-27 17:09:17 +03007744 stripped = BTRFS_BLOCK_GROUP_RAID0 |
David Woodhouse53b381b2013-01-29 18:40:14 -05007745 BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6 |
Ilya Dryomovfc67c452012-03-27 17:09:17 +03007746 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
7747
Chris Masonec44a352008-04-28 15:29:52 -04007748 if (num_devices == 1) {
7749 stripped |= BTRFS_BLOCK_GROUP_DUP;
7750 stripped = flags & ~stripped;
7751
7752 /* turn raid0 into single device chunks */
7753 if (flags & BTRFS_BLOCK_GROUP_RAID0)
7754 return stripped;
7755
7756 /* turn mirroring into duplication */
7757 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
7758 BTRFS_BLOCK_GROUP_RAID10))
7759 return stripped | BTRFS_BLOCK_GROUP_DUP;
Chris Masonec44a352008-04-28 15:29:52 -04007760 } else {
7761 /* they already had raid on here, just return */
Chris Masonec44a352008-04-28 15:29:52 -04007762 if (flags & stripped)
7763 return flags;
7764
7765 stripped |= BTRFS_BLOCK_GROUP_DUP;
7766 stripped = flags & ~stripped;
7767
7768 /* switch duplicated blocks with raid1 */
7769 if (flags & BTRFS_BLOCK_GROUP_DUP)
7770 return stripped | BTRFS_BLOCK_GROUP_RAID1;
7771
Ilya Dryomove3176ca2012-03-27 17:09:16 +03007772 /* this is drive concat, leave it alone */
Chris Masonec44a352008-04-28 15:29:52 -04007773 }
Ilya Dryomove3176ca2012-03-27 17:09:16 +03007774
Chris Masonec44a352008-04-28 15:29:52 -04007775 return flags;
7776}
7777
Miao Xie199c36e2011-07-15 10:34:36 +00007778static int set_block_group_ro(struct btrfs_block_group_cache *cache, int force)
Chris Mason0ef3e662008-05-24 14:04:53 -04007779{
Yan, Zhengf0486c62010-05-16 10:46:25 -04007780 struct btrfs_space_info *sinfo = cache->space_info;
7781 u64 num_bytes;
Miao Xie199c36e2011-07-15 10:34:36 +00007782 u64 min_allocable_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007783 int ret = -ENOSPC;
Chris Mason0ef3e662008-05-24 14:04:53 -04007784
Chris Masonc286ac42008-07-22 23:06:41 -04007785
Miao Xie199c36e2011-07-15 10:34:36 +00007786 /*
7787 * We need some metadata space and system metadata space for
7788 * allocating chunks in some corner cases until we force to set
7789 * it to be readonly.
7790 */
7791 if ((sinfo->flags &
7792 (BTRFS_BLOCK_GROUP_SYSTEM | BTRFS_BLOCK_GROUP_METADATA)) &&
7793 !force)
7794 min_allocable_bytes = 1 * 1024 * 1024;
7795 else
7796 min_allocable_bytes = 0;
7797
Yan, Zhengf0486c62010-05-16 10:46:25 -04007798 spin_lock(&sinfo->lock);
7799 spin_lock(&cache->lock);
WuBo61cfea92011-07-26 03:30:11 +00007800
7801 if (cache->ro) {
7802 ret = 0;
7803 goto out;
7804 }
7805
Yan, Zhengf0486c62010-05-16 10:46:25 -04007806 num_bytes = cache->key.offset - cache->reserved - cache->pinned -
7807 cache->bytes_super - btrfs_block_group_used(&cache->item);
Chris Mason7d9eb122008-07-08 14:19:17 -04007808
Yan, Zhengf0486c62010-05-16 10:46:25 -04007809 if (sinfo->bytes_used + sinfo->bytes_reserved + sinfo->bytes_pinned +
Josef Bacik37be25b2011-08-05 10:25:38 -04007810 sinfo->bytes_may_use + sinfo->bytes_readonly + num_bytes +
7811 min_allocable_bytes <= sinfo->total_bytes) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04007812 sinfo->bytes_readonly += num_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007813 cache->ro = 1;
7814 ret = 0;
7815 }
WuBo61cfea92011-07-26 03:30:11 +00007816out:
Yan, Zhengf0486c62010-05-16 10:46:25 -04007817 spin_unlock(&cache->lock);
7818 spin_unlock(&sinfo->lock);
7819 return ret;
Chris Mason0ef3e662008-05-24 14:04:53 -04007820}
7821
Yan, Zhengf0486c62010-05-16 10:46:25 -04007822int btrfs_set_block_group_ro(struct btrfs_root *root,
7823 struct btrfs_block_group_cache *cache)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007824
7825{
Yan, Zhengf0486c62010-05-16 10:46:25 -04007826 struct btrfs_trans_handle *trans;
7827 u64 alloc_flags;
7828 int ret;
7829
7830 BUG_ON(cache->ro);
7831
Josef Bacik7a7eaa402011-04-13 12:54:33 -04007832 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007833 if (IS_ERR(trans))
7834 return PTR_ERR(trans);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007835
7836 alloc_flags = update_block_group_flags(root, cache->flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007837 if (alloc_flags != cache->flags) {
Josef Bacik698d0082012-09-12 14:08:47 -04007838 ret = do_chunk_alloc(trans, root, alloc_flags,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007839 CHUNK_ALLOC_FORCE);
7840 if (ret < 0)
7841 goto out;
7842 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04007843
Miao Xie199c36e2011-07-15 10:34:36 +00007844 ret = set_block_group_ro(cache, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007845 if (!ret)
7846 goto out;
7847 alloc_flags = get_alloc_profile(root, cache->space_info->flags);
Josef Bacik698d0082012-09-12 14:08:47 -04007848 ret = do_chunk_alloc(trans, root, alloc_flags,
Chris Mason0e4f8f82011-04-15 16:05:44 -04007849 CHUNK_ALLOC_FORCE);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007850 if (ret < 0)
7851 goto out;
Miao Xie199c36e2011-07-15 10:34:36 +00007852 ret = set_block_group_ro(cache, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007853out:
7854 btrfs_end_transaction(trans, root);
7855 return ret;
7856}
7857
Chris Masonc87f08c2011-02-16 13:57:04 -05007858int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans,
7859 struct btrfs_root *root, u64 type)
7860{
7861 u64 alloc_flags = get_alloc_profile(root, type);
Josef Bacik698d0082012-09-12 14:08:47 -04007862 return do_chunk_alloc(trans, root, alloc_flags,
Chris Mason0e4f8f82011-04-15 16:05:44 -04007863 CHUNK_ALLOC_FORCE);
Chris Masonc87f08c2011-02-16 13:57:04 -05007864}
7865
Miao Xie6d07bce2011-01-05 10:07:31 +00007866/*
7867 * helper to account the unused space of all the readonly block group in the
7868 * list. takes mirrors into account.
7869 */
7870static u64 __btrfs_get_ro_block_group_free_space(struct list_head *groups_list)
7871{
7872 struct btrfs_block_group_cache *block_group;
7873 u64 free_bytes = 0;
7874 int factor;
7875
7876 list_for_each_entry(block_group, groups_list, list) {
7877 spin_lock(&block_group->lock);
7878
7879 if (!block_group->ro) {
7880 spin_unlock(&block_group->lock);
7881 continue;
7882 }
7883
7884 if (block_group->flags & (BTRFS_BLOCK_GROUP_RAID1 |
7885 BTRFS_BLOCK_GROUP_RAID10 |
7886 BTRFS_BLOCK_GROUP_DUP))
7887 factor = 2;
7888 else
7889 factor = 1;
7890
7891 free_bytes += (block_group->key.offset -
7892 btrfs_block_group_used(&block_group->item)) *
7893 factor;
7894
7895 spin_unlock(&block_group->lock);
7896 }
7897
7898 return free_bytes;
7899}
7900
7901/*
7902 * helper to account the unused space of all the readonly block group in the
7903 * space_info. takes mirrors into account.
7904 */
7905u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo)
7906{
7907 int i;
7908 u64 free_bytes = 0;
7909
7910 spin_lock(&sinfo->lock);
7911
7912 for(i = 0; i < BTRFS_NR_RAID_TYPES; i++)
7913 if (!list_empty(&sinfo->block_groups[i]))
7914 free_bytes += __btrfs_get_ro_block_group_free_space(
7915 &sinfo->block_groups[i]);
7916
7917 spin_unlock(&sinfo->lock);
7918
7919 return free_bytes;
7920}
7921
Jeff Mahoney143bede2012-03-01 14:56:26 +01007922void btrfs_set_block_group_rw(struct btrfs_root *root,
Yan, Zhengf0486c62010-05-16 10:46:25 -04007923 struct btrfs_block_group_cache *cache)
7924{
7925 struct btrfs_space_info *sinfo = cache->space_info;
7926 u64 num_bytes;
7927
7928 BUG_ON(!cache->ro);
7929
7930 spin_lock(&sinfo->lock);
7931 spin_lock(&cache->lock);
7932 num_bytes = cache->key.offset - cache->reserved - cache->pinned -
7933 cache->bytes_super - btrfs_block_group_used(&cache->item);
7934 sinfo->bytes_readonly -= num_bytes;
7935 cache->ro = 0;
7936 spin_unlock(&cache->lock);
7937 spin_unlock(&sinfo->lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007938}
7939
Josef Bacikba1bf482009-09-11 16:11:19 -04007940/*
7941 * checks to see if its even possible to relocate this block group.
7942 *
7943 * @return - -1 if it's not a good idea to relocate this block group, 0 if its
7944 * ok to go ahead and try.
7945 */
7946int btrfs_can_relocate(struct btrfs_root *root, u64 bytenr)
Zheng Yan1a40e232008-09-26 10:09:34 -04007947{
Zheng Yan1a40e232008-09-26 10:09:34 -04007948 struct btrfs_block_group_cache *block_group;
Josef Bacikba1bf482009-09-11 16:11:19 -04007949 struct btrfs_space_info *space_info;
7950 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
7951 struct btrfs_device *device;
liubocdcb7252011-08-03 10:15:25 +00007952 u64 min_free;
Josef Bacik6719db62011-08-20 08:29:51 -04007953 u64 dev_min = 1;
7954 u64 dev_nr = 0;
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03007955 u64 target;
liubocdcb7252011-08-03 10:15:25 +00007956 int index;
Josef Bacikba1bf482009-09-11 16:11:19 -04007957 int full = 0;
7958 int ret = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05007959
Josef Bacikba1bf482009-09-11 16:11:19 -04007960 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
Zheng Yan1a40e232008-09-26 10:09:34 -04007961
Josef Bacikba1bf482009-09-11 16:11:19 -04007962 /* odd, couldn't find the block group, leave it alone */
7963 if (!block_group)
7964 return -1;
Chris Masonedbd8d42007-12-21 16:27:24 -05007965
liubocdcb7252011-08-03 10:15:25 +00007966 min_free = btrfs_block_group_used(&block_group->item);
7967
Josef Bacikba1bf482009-09-11 16:11:19 -04007968 /* no bytes used, we're good */
liubocdcb7252011-08-03 10:15:25 +00007969 if (!min_free)
Josef Bacikba1bf482009-09-11 16:11:19 -04007970 goto out;
Chris Mason323da792008-05-09 11:46:48 -04007971
Josef Bacikba1bf482009-09-11 16:11:19 -04007972 space_info = block_group->space_info;
7973 spin_lock(&space_info->lock);
Chris Mason323da792008-05-09 11:46:48 -04007974
Josef Bacikba1bf482009-09-11 16:11:19 -04007975 full = space_info->full;
Zheng Yan1a40e232008-09-26 10:09:34 -04007976
Josef Bacikba1bf482009-09-11 16:11:19 -04007977 /*
7978 * if this is the last block group we have in this space, we can't
Chris Mason7ce618d2009-09-22 14:48:44 -04007979 * relocate it unless we're able to allocate a new chunk below.
7980 *
7981 * Otherwise, we need to make sure we have room in the space to handle
7982 * all of the extents from this block group. If we can, we're good
Josef Bacikba1bf482009-09-11 16:11:19 -04007983 */
Chris Mason7ce618d2009-09-22 14:48:44 -04007984 if ((space_info->total_bytes != block_group->key.offset) &&
liubocdcb7252011-08-03 10:15:25 +00007985 (space_info->bytes_used + space_info->bytes_reserved +
7986 space_info->bytes_pinned + space_info->bytes_readonly +
7987 min_free < space_info->total_bytes)) {
Josef Bacikba1bf482009-09-11 16:11:19 -04007988 spin_unlock(&space_info->lock);
7989 goto out;
7990 }
7991 spin_unlock(&space_info->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04007992
Josef Bacikba1bf482009-09-11 16:11:19 -04007993 /*
7994 * ok we don't have enough space, but maybe we have free space on our
7995 * devices to allocate new chunks for relocation, so loop through our
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03007996 * alloc devices and guess if we have enough space. if this block
7997 * group is going to be restriped, run checks against the target
7998 * profile instead of the current one.
Josef Bacikba1bf482009-09-11 16:11:19 -04007999 */
8000 ret = -1;
Chris Mason4313b392008-01-03 09:08:48 -05008001
liubocdcb7252011-08-03 10:15:25 +00008002 /*
8003 * index:
8004 * 0: raid10
8005 * 1: raid1
8006 * 2: dup
8007 * 3: raid0
8008 * 4: single
8009 */
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03008010 target = get_restripe_target(root->fs_info, block_group->flags);
8011 if (target) {
Liu Bo31e50222012-11-21 14:18:10 +00008012 index = __get_raid_index(extended_to_chunk(target));
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03008013 } else {
8014 /*
8015 * this is just a balance, so if we were marked as full
8016 * we know there is no space for a new chunk
8017 */
8018 if (full)
8019 goto out;
8020
8021 index = get_block_group_index(block_group);
8022 }
8023
Miao Xiee6ec7162013-01-17 05:38:51 +00008024 if (index == BTRFS_RAID_RAID10) {
liubocdcb7252011-08-03 10:15:25 +00008025 dev_min = 4;
Josef Bacik6719db62011-08-20 08:29:51 -04008026 /* Divide by 2 */
8027 min_free >>= 1;
Miao Xiee6ec7162013-01-17 05:38:51 +00008028 } else if (index == BTRFS_RAID_RAID1) {
liubocdcb7252011-08-03 10:15:25 +00008029 dev_min = 2;
Miao Xiee6ec7162013-01-17 05:38:51 +00008030 } else if (index == BTRFS_RAID_DUP) {
Josef Bacik6719db62011-08-20 08:29:51 -04008031 /* Multiply by 2 */
8032 min_free <<= 1;
Miao Xiee6ec7162013-01-17 05:38:51 +00008033 } else if (index == BTRFS_RAID_RAID0) {
liubocdcb7252011-08-03 10:15:25 +00008034 dev_min = fs_devices->rw_devices;
Josef Bacik6719db62011-08-20 08:29:51 -04008035 do_div(min_free, dev_min);
liubocdcb7252011-08-03 10:15:25 +00008036 }
8037
Josef Bacikba1bf482009-09-11 16:11:19 -04008038 mutex_lock(&root->fs_info->chunk_mutex);
8039 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
Miao Xie7bfc8372011-01-05 10:07:26 +00008040 u64 dev_offset;
Chris Masonea8c2812008-08-04 23:17:27 -04008041
Josef Bacikba1bf482009-09-11 16:11:19 -04008042 /*
8043 * check to make sure we can actually find a chunk with enough
8044 * space to fit our block group in.
8045 */
Stefan Behrens63a212a2012-11-05 18:29:28 +01008046 if (device->total_bytes > device->bytes_used + min_free &&
8047 !device->is_tgtdev_for_dev_replace) {
Li Zefan125ccb02011-12-08 15:07:24 +08008048 ret = find_free_dev_extent(device, min_free,
Miao Xie7bfc8372011-01-05 10:07:26 +00008049 &dev_offset, NULL);
Josef Bacikba1bf482009-09-11 16:11:19 -04008050 if (!ret)
liubocdcb7252011-08-03 10:15:25 +00008051 dev_nr++;
8052
8053 if (dev_nr >= dev_min)
Yan73e48b22008-01-03 14:14:39 -05008054 break;
liubocdcb7252011-08-03 10:15:25 +00008055
Josef Bacikba1bf482009-09-11 16:11:19 -04008056 ret = -1;
Yan73e48b22008-01-03 14:14:39 -05008057 }
Chris Masonedbd8d42007-12-21 16:27:24 -05008058 }
Josef Bacikba1bf482009-09-11 16:11:19 -04008059 mutex_unlock(&root->fs_info->chunk_mutex);
Chris Masonedbd8d42007-12-21 16:27:24 -05008060out:
Josef Bacikba1bf482009-09-11 16:11:19 -04008061 btrfs_put_block_group(block_group);
Chris Masonedbd8d42007-12-21 16:27:24 -05008062 return ret;
8063}
8064
Christoph Hellwigb2950862008-12-02 09:54:17 -05008065static int find_first_block_group(struct btrfs_root *root,
8066 struct btrfs_path *path, struct btrfs_key *key)
Chris Mason0b86a832008-03-24 15:01:56 -04008067{
Chris Mason925baed2008-06-25 16:01:30 -04008068 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04008069 struct btrfs_key found_key;
8070 struct extent_buffer *leaf;
8071 int slot;
8072
8073 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
8074 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04008075 goto out;
8076
Chris Masond3977122009-01-05 21:25:51 -05008077 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04008078 slot = path->slots[0];
8079 leaf = path->nodes[0];
8080 if (slot >= btrfs_header_nritems(leaf)) {
8081 ret = btrfs_next_leaf(root, path);
8082 if (ret == 0)
8083 continue;
8084 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04008085 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04008086 break;
8087 }
8088 btrfs_item_key_to_cpu(leaf, &found_key, slot);
8089
8090 if (found_key.objectid >= key->objectid &&
Chris Mason925baed2008-06-25 16:01:30 -04008091 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
8092 ret = 0;
8093 goto out;
8094 }
Chris Mason0b86a832008-03-24 15:01:56 -04008095 path->slots[0]++;
8096 }
Chris Mason925baed2008-06-25 16:01:30 -04008097out:
Chris Mason0b86a832008-03-24 15:01:56 -04008098 return ret;
8099}
8100
Josef Bacik0af3d002010-06-21 14:48:16 -04008101void btrfs_put_block_group_cache(struct btrfs_fs_info *info)
8102{
8103 struct btrfs_block_group_cache *block_group;
8104 u64 last = 0;
8105
8106 while (1) {
8107 struct inode *inode;
8108
8109 block_group = btrfs_lookup_first_block_group(info, last);
8110 while (block_group) {
8111 spin_lock(&block_group->lock);
8112 if (block_group->iref)
8113 break;
8114 spin_unlock(&block_group->lock);
8115 block_group = next_block_group(info->tree_root,
8116 block_group);
8117 }
8118 if (!block_group) {
8119 if (last == 0)
8120 break;
8121 last = 0;
8122 continue;
8123 }
8124
8125 inode = block_group->inode;
8126 block_group->iref = 0;
8127 block_group->inode = NULL;
8128 spin_unlock(&block_group->lock);
8129 iput(inode);
8130 last = block_group->key.objectid + block_group->key.offset;
8131 btrfs_put_block_group(block_group);
8132 }
8133}
8134
Zheng Yan1a40e232008-09-26 10:09:34 -04008135int btrfs_free_block_groups(struct btrfs_fs_info *info)
8136{
8137 struct btrfs_block_group_cache *block_group;
Chris Mason4184ea72009-03-10 12:39:20 -04008138 struct btrfs_space_info *space_info;
Yan Zheng11833d62009-09-11 16:11:19 -04008139 struct btrfs_caching_control *caching_ctl;
Zheng Yan1a40e232008-09-26 10:09:34 -04008140 struct rb_node *n;
8141
Yan Zheng11833d62009-09-11 16:11:19 -04008142 down_write(&info->extent_commit_sem);
8143 while (!list_empty(&info->caching_block_groups)) {
8144 caching_ctl = list_entry(info->caching_block_groups.next,
8145 struct btrfs_caching_control, list);
8146 list_del(&caching_ctl->list);
8147 put_caching_control(caching_ctl);
8148 }
8149 up_write(&info->extent_commit_sem);
8150
Zheng Yan1a40e232008-09-26 10:09:34 -04008151 spin_lock(&info->block_group_cache_lock);
8152 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
8153 block_group = rb_entry(n, struct btrfs_block_group_cache,
8154 cache_node);
Zheng Yan1a40e232008-09-26 10:09:34 -04008155 rb_erase(&block_group->cache_node,
8156 &info->block_group_cache_tree);
Yan Zhengd899e052008-10-30 14:25:28 -04008157 spin_unlock(&info->block_group_cache_lock);
8158
Josef Bacik80eb2342008-10-29 14:49:05 -04008159 down_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04008160 list_del(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04008161 up_write(&block_group->space_info->groups_sem);
Yan Zhengd2fb3432008-12-11 16:30:39 -05008162
Josef Bacik817d52f2009-07-13 21:29:25 -04008163 if (block_group->cached == BTRFS_CACHE_STARTED)
Yan Zheng11833d62009-09-11 16:11:19 -04008164 wait_block_group_cache_done(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -04008165
Josef Bacik3c148742011-02-02 15:53:47 +00008166 /*
8167 * We haven't cached this block group, which means we could
8168 * possibly have excluded extents on this block group.
8169 */
8170 if (block_group->cached == BTRFS_CACHE_NO)
8171 free_excluded_extents(info->extent_root, block_group);
8172
Josef Bacik817d52f2009-07-13 21:29:25 -04008173 btrfs_remove_free_space_cache(block_group);
Josef Bacik11dfe352009-11-13 20:12:59 +00008174 btrfs_put_block_group(block_group);
Yan Zhengd899e052008-10-30 14:25:28 -04008175
8176 spin_lock(&info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04008177 }
8178 spin_unlock(&info->block_group_cache_lock);
Chris Mason4184ea72009-03-10 12:39:20 -04008179
8180 /* now that all the block groups are freed, go through and
8181 * free all the space_info structs. This is only called during
8182 * the final stages of unmount, and so we know nobody is
8183 * using them. We call synchronize_rcu() once before we start,
8184 * just to be on the safe side.
8185 */
8186 synchronize_rcu();
8187
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04008188 release_global_block_rsv(info);
8189
Chris Mason4184ea72009-03-10 12:39:20 -04008190 while(!list_empty(&info->space_info)) {
8191 space_info = list_entry(info->space_info.next,
8192 struct btrfs_space_info,
8193 list);
David Sterbab069e0c2013-02-08 21:28:17 +00008194 if (btrfs_test_opt(info->tree_root, ENOSPC_DEBUG)) {
8195 if (space_info->bytes_pinned > 0 ||
8196 space_info->bytes_reserved > 0 ||
8197 space_info->bytes_may_use > 0) {
8198 WARN_ON(1);
8199 dump_space_info(space_info, 0, 0);
8200 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04008201 }
Josef Bacikb150a4f2013-06-19 15:00:04 -04008202 percpu_counter_destroy(&space_info->total_bytes_pinned);
Chris Mason4184ea72009-03-10 12:39:20 -04008203 list_del(&space_info->list);
8204 kfree(space_info);
8205 }
Zheng Yan1a40e232008-09-26 10:09:34 -04008206 return 0;
8207}
8208
Yan, Zhengb742bb82010-05-16 10:46:24 -04008209static void __link_block_group(struct btrfs_space_info *space_info,
8210 struct btrfs_block_group_cache *cache)
8211{
8212 int index = get_block_group_index(cache);
8213
8214 down_write(&space_info->groups_sem);
8215 list_add_tail(&cache->list, &space_info->block_groups[index]);
8216 up_write(&space_info->groups_sem);
8217}
8218
Chris Mason9078a3e2007-04-26 16:46:15 -04008219int btrfs_read_block_groups(struct btrfs_root *root)
8220{
8221 struct btrfs_path *path;
8222 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04008223 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04008224 struct btrfs_fs_info *info = root->fs_info;
Chris Mason6324fbf2008-03-24 15:01:59 -04008225 struct btrfs_space_info *space_info;
Chris Mason9078a3e2007-04-26 16:46:15 -04008226 struct btrfs_key key;
8227 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -04008228 struct extent_buffer *leaf;
Josef Bacik0af3d002010-06-21 14:48:16 -04008229 int need_clear = 0;
8230 u64 cache_gen;
Chris Mason96b51792007-10-15 16:15:19 -04008231
Chris Masonbe744172007-05-06 10:15:01 -04008232 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04008233 key.objectid = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04008234 key.offset = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04008235 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
Chris Mason9078a3e2007-04-26 16:46:15 -04008236 path = btrfs_alloc_path();
8237 if (!path)
8238 return -ENOMEM;
Josef Bacik026fd312011-05-13 10:32:11 -04008239 path->reada = 1;
Chris Mason9078a3e2007-04-26 16:46:15 -04008240
David Sterba6c417612011-04-13 15:41:04 +02008241 cache_gen = btrfs_super_cache_generation(root->fs_info->super_copy);
Josef Bacik73bc1872011-10-03 14:07:49 -04008242 if (btrfs_test_opt(root, SPACE_CACHE) &&
David Sterba6c417612011-04-13 15:41:04 +02008243 btrfs_super_generation(root->fs_info->super_copy) != cache_gen)
Josef Bacik0af3d002010-06-21 14:48:16 -04008244 need_clear = 1;
Josef Bacik88c2ba32010-09-21 14:21:34 -04008245 if (btrfs_test_opt(root, CLEAR_CACHE))
8246 need_clear = 1;
Josef Bacik0af3d002010-06-21 14:48:16 -04008247
Chris Masond3977122009-01-05 21:25:51 -05008248 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04008249 ret = find_first_block_group(root, path, &key);
Yan, Zhengb742bb82010-05-16 10:46:24 -04008250 if (ret > 0)
8251 break;
Chris Mason0b86a832008-03-24 15:01:56 -04008252 if (ret != 0)
8253 goto error;
Chris Mason5f39d392007-10-15 16:14:19 -04008254 leaf = path->nodes[0];
8255 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason8f18cf12008-04-25 16:53:30 -04008256 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -04008257 if (!cache) {
Chris Mason0b86a832008-03-24 15:01:56 -04008258 ret = -ENOMEM;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008259 goto error;
Chris Mason9078a3e2007-04-26 16:46:15 -04008260 }
Li Zefan34d52cb6c2011-03-29 13:46:06 +08008261 cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
8262 GFP_NOFS);
8263 if (!cache->free_space_ctl) {
8264 kfree(cache);
8265 ret = -ENOMEM;
8266 goto error;
8267 }
Chris Mason3e1ad542007-05-07 20:03:49 -04008268
Yan Zhengd2fb3432008-12-11 16:30:39 -05008269 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04008270 spin_lock_init(&cache->lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04008271 cache->fs_info = info;
Josef Bacik0f9dd462008-09-23 13:14:11 -04008272 INIT_LIST_HEAD(&cache->list);
Chris Masonfa9c0d72009-04-03 09:47:43 -04008273 INIT_LIST_HEAD(&cache->cluster_list);
Josef Bacik96303082009-07-13 21:29:25 -04008274
Liu Bocf7c1ef2012-07-06 03:31:34 -06008275 if (need_clear) {
8276 /*
8277 * When we mount with old space cache, we need to
8278 * set BTRFS_DC_CLEAR and set dirty flag.
8279 *
8280 * a) Setting 'BTRFS_DC_CLEAR' makes sure that we
8281 * truncate the old free space cache inode and
8282 * setup a new one.
8283 * b) Setting 'dirty flag' makes sure that we flush
8284 * the new space cache info onto disk.
8285 */
Josef Bacik0af3d002010-06-21 14:48:16 -04008286 cache->disk_cache_state = BTRFS_DC_CLEAR;
Liu Bocf7c1ef2012-07-06 03:31:34 -06008287 if (btrfs_test_opt(root, SPACE_CACHE))
8288 cache->dirty = 1;
8289 }
Josef Bacik0af3d002010-06-21 14:48:16 -04008290
Chris Mason5f39d392007-10-15 16:14:19 -04008291 read_extent_buffer(leaf, &cache->item,
8292 btrfs_item_ptr_offset(leaf, path->slots[0]),
8293 sizeof(cache->item));
Chris Mason9078a3e2007-04-26 16:46:15 -04008294 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Mason0b86a832008-03-24 15:01:56 -04008295
Chris Mason9078a3e2007-04-26 16:46:15 -04008296 key.objectid = found_key.objectid + found_key.offset;
David Sterbab3b4aa72011-04-21 01:20:15 +02008297 btrfs_release_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04008298 cache->flags = btrfs_block_group_flags(&cache->item);
Josef Bacik817d52f2009-07-13 21:29:25 -04008299 cache->sectorsize = root->sectorsize;
David Woodhouse53b381b2013-01-29 18:40:14 -05008300 cache->full_stripe_len = btrfs_full_stripe_len(root,
8301 &root->fs_info->mapping_tree,
8302 found_key.objectid);
Li Zefan34d52cb6c2011-03-29 13:46:06 +08008303 btrfs_init_free_space_ctl(cache);
8304
Josef Bacik817d52f2009-07-13 21:29:25 -04008305 /*
Josef Bacik3c148742011-02-02 15:53:47 +00008306 * We need to exclude the super stripes now so that the space
8307 * info has super bytes accounted for, otherwise we'll think
8308 * we have more space than we actually do.
8309 */
Josef Bacik835d9742013-03-19 12:13:25 -04008310 ret = exclude_super_stripes(root, cache);
8311 if (ret) {
8312 /*
8313 * We may have excluded something, so call this just in
8314 * case.
8315 */
8316 free_excluded_extents(root, cache);
8317 kfree(cache->free_space_ctl);
8318 kfree(cache);
8319 goto error;
8320 }
Josef Bacik3c148742011-02-02 15:53:47 +00008321
8322 /*
Josef Bacik817d52f2009-07-13 21:29:25 -04008323 * check for two cases, either we are full, and therefore
8324 * don't need to bother with the caching work since we won't
8325 * find any space, or we are empty, and we can just add all
8326 * the space in and be done with it. This saves us _alot_ of
8327 * time, particularly in the full case.
8328 */
8329 if (found_key.offset == btrfs_block_group_used(&cache->item)) {
Yan Zheng11833d62009-09-11 16:11:19 -04008330 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04008331 cache->cached = BTRFS_CACHE_FINISHED;
Josef Bacik1b2da372009-09-11 16:11:20 -04008332 free_excluded_extents(root, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04008333 } else if (btrfs_block_group_used(&cache->item) == 0) {
Yan Zheng11833d62009-09-11 16:11:19 -04008334 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04008335 cache->cached = BTRFS_CACHE_FINISHED;
8336 add_new_free_space(cache, root->fs_info,
8337 found_key.objectid,
8338 found_key.objectid +
8339 found_key.offset);
Yan Zheng11833d62009-09-11 16:11:19 -04008340 free_excluded_extents(root, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04008341 }
Chris Mason96b51792007-10-15 16:15:19 -04008342
Josef Bacik8c579fe2013-04-02 12:40:42 -04008343 ret = btrfs_add_block_group_cache(root->fs_info, cache);
8344 if (ret) {
8345 btrfs_remove_free_space_cache(cache);
8346 btrfs_put_block_group(cache);
8347 goto error;
8348 }
8349
Chris Mason6324fbf2008-03-24 15:01:59 -04008350 ret = update_space_info(info, cache->flags, found_key.offset,
8351 btrfs_block_group_used(&cache->item),
8352 &space_info);
Josef Bacik8c579fe2013-04-02 12:40:42 -04008353 if (ret) {
8354 btrfs_remove_free_space_cache(cache);
8355 spin_lock(&info->block_group_cache_lock);
8356 rb_erase(&cache->cache_node,
8357 &info->block_group_cache_tree);
8358 spin_unlock(&info->block_group_cache_lock);
8359 btrfs_put_block_group(cache);
8360 goto error;
8361 }
8362
Chris Mason6324fbf2008-03-24 15:01:59 -04008363 cache->space_info = space_info;
Josef Bacik1b2da372009-09-11 16:11:20 -04008364 spin_lock(&cache->space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04008365 cache->space_info->bytes_readonly += cache->bytes_super;
Josef Bacik1b2da372009-09-11 16:11:20 -04008366 spin_unlock(&cache->space_info->lock);
8367
Yan, Zhengb742bb82010-05-16 10:46:24 -04008368 __link_block_group(space_info, cache);
Chris Mason6324fbf2008-03-24 15:01:59 -04008369
Chris Mason75ccf472008-09-30 19:24:06 -04008370 set_avail_alloc_bits(root->fs_info, cache->flags);
Yan Zheng2b820322008-11-17 21:11:30 -05008371 if (btrfs_chunk_readonly(root, cache->key.objectid))
Miao Xie199c36e2011-07-15 10:34:36 +00008372 set_block_group_ro(cache, 1);
Chris Mason9078a3e2007-04-26 16:46:15 -04008373 }
Yan, Zhengb742bb82010-05-16 10:46:24 -04008374
8375 list_for_each_entry_rcu(space_info, &root->fs_info->space_info, list) {
8376 if (!(get_alloc_profile(root, space_info->flags) &
8377 (BTRFS_BLOCK_GROUP_RAID10 |
8378 BTRFS_BLOCK_GROUP_RAID1 |
David Woodhouse53b381b2013-01-29 18:40:14 -05008379 BTRFS_BLOCK_GROUP_RAID5 |
8380 BTRFS_BLOCK_GROUP_RAID6 |
Yan, Zhengb742bb82010-05-16 10:46:24 -04008381 BTRFS_BLOCK_GROUP_DUP)))
8382 continue;
8383 /*
8384 * avoid allocating from un-mirrored block group if there are
8385 * mirrored block groups.
8386 */
8387 list_for_each_entry(cache, &space_info->block_groups[3], list)
Miao Xie199c36e2011-07-15 10:34:36 +00008388 set_block_group_ro(cache, 1);
Yan, Zhengb742bb82010-05-16 10:46:24 -04008389 list_for_each_entry(cache, &space_info->block_groups[4], list)
Miao Xie199c36e2011-07-15 10:34:36 +00008390 set_block_group_ro(cache, 1);
Yan, Zhengb742bb82010-05-16 10:46:24 -04008391 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04008392
8393 init_global_block_rsv(info);
Chris Mason0b86a832008-03-24 15:01:56 -04008394 ret = 0;
8395error:
Chris Mason9078a3e2007-04-26 16:46:15 -04008396 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04008397 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04008398}
Chris Mason6324fbf2008-03-24 15:01:59 -04008399
Josef Bacikea658ba2012-09-11 16:57:25 -04008400void btrfs_create_pending_block_groups(struct btrfs_trans_handle *trans,
8401 struct btrfs_root *root)
8402{
8403 struct btrfs_block_group_cache *block_group, *tmp;
8404 struct btrfs_root *extent_root = root->fs_info->extent_root;
8405 struct btrfs_block_group_item item;
8406 struct btrfs_key key;
8407 int ret = 0;
8408
8409 list_for_each_entry_safe(block_group, tmp, &trans->new_bgs,
8410 new_bg_list) {
8411 list_del_init(&block_group->new_bg_list);
8412
8413 if (ret)
8414 continue;
8415
8416 spin_lock(&block_group->lock);
8417 memcpy(&item, &block_group->item, sizeof(item));
8418 memcpy(&key, &block_group->key, sizeof(key));
8419 spin_unlock(&block_group->lock);
8420
8421 ret = btrfs_insert_item(trans, extent_root, &key, &item,
8422 sizeof(item));
8423 if (ret)
8424 btrfs_abort_transaction(trans, extent_root, ret);
8425 }
8426}
8427
Chris Mason6324fbf2008-03-24 15:01:59 -04008428int btrfs_make_block_group(struct btrfs_trans_handle *trans,
8429 struct btrfs_root *root, u64 bytes_used,
Chris Masone17cade2008-04-15 15:41:47 -04008430 u64 type, u64 chunk_objectid, u64 chunk_offset,
Chris Mason6324fbf2008-03-24 15:01:59 -04008431 u64 size)
8432{
8433 int ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04008434 struct btrfs_root *extent_root;
8435 struct btrfs_block_group_cache *cache;
Chris Mason6324fbf2008-03-24 15:01:59 -04008436
8437 extent_root = root->fs_info->extent_root;
Chris Mason6324fbf2008-03-24 15:01:59 -04008438
Chris Mason12fcfd22009-03-24 10:24:20 -04008439 root->fs_info->last_trans_log_full_commit = trans->transid;
Chris Masone02119d2008-09-05 16:13:11 -04008440
Chris Mason8f18cf12008-04-25 16:53:30 -04008441 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Josef Bacik0f9dd462008-09-23 13:14:11 -04008442 if (!cache)
8443 return -ENOMEM;
Li Zefan34d52cb6c2011-03-29 13:46:06 +08008444 cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
8445 GFP_NOFS);
8446 if (!cache->free_space_ctl) {
8447 kfree(cache);
8448 return -ENOMEM;
8449 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04008450
Chris Masone17cade2008-04-15 15:41:47 -04008451 cache->key.objectid = chunk_offset;
Chris Mason6324fbf2008-03-24 15:01:59 -04008452 cache->key.offset = size;
Yan Zhengd2fb3432008-12-11 16:30:39 -05008453 cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
Josef Bacik96303082009-07-13 21:29:25 -04008454 cache->sectorsize = root->sectorsize;
Josef Bacik0af3d002010-06-21 14:48:16 -04008455 cache->fs_info = root->fs_info;
David Woodhouse53b381b2013-01-29 18:40:14 -05008456 cache->full_stripe_len = btrfs_full_stripe_len(root,
8457 &root->fs_info->mapping_tree,
8458 chunk_offset);
Josef Bacik96303082009-07-13 21:29:25 -04008459
Yan Zhengd2fb3432008-12-11 16:30:39 -05008460 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04008461 spin_lock_init(&cache->lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04008462 INIT_LIST_HEAD(&cache->list);
Chris Masonfa9c0d72009-04-03 09:47:43 -04008463 INIT_LIST_HEAD(&cache->cluster_list);
Josef Bacikea658ba2012-09-11 16:57:25 -04008464 INIT_LIST_HEAD(&cache->new_bg_list);
Chris Mason0ef3e662008-05-24 14:04:53 -04008465
Li Zefan34d52cb6c2011-03-29 13:46:06 +08008466 btrfs_init_free_space_ctl(cache);
8467
Chris Mason6324fbf2008-03-24 15:01:59 -04008468 btrfs_set_block_group_used(&cache->item, bytes_used);
Chris Mason6324fbf2008-03-24 15:01:59 -04008469 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
8470 cache->flags = type;
8471 btrfs_set_block_group_flags(&cache->item, type);
8472
Yan Zheng11833d62009-09-11 16:11:19 -04008473 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04008474 cache->cached = BTRFS_CACHE_FINISHED;
Josef Bacik835d9742013-03-19 12:13:25 -04008475 ret = exclude_super_stripes(root, cache);
8476 if (ret) {
8477 /*
8478 * We may have excluded something, so call this just in
8479 * case.
8480 */
8481 free_excluded_extents(root, cache);
8482 kfree(cache->free_space_ctl);
8483 kfree(cache);
8484 return ret;
8485 }
Josef Bacik96303082009-07-13 21:29:25 -04008486
Josef Bacik817d52f2009-07-13 21:29:25 -04008487 add_new_free_space(cache, root->fs_info, chunk_offset,
8488 chunk_offset + size);
8489
Yan Zheng11833d62009-09-11 16:11:19 -04008490 free_excluded_extents(root, cache);
8491
Josef Bacik8c579fe2013-04-02 12:40:42 -04008492 ret = btrfs_add_block_group_cache(root->fs_info, cache);
8493 if (ret) {
8494 btrfs_remove_free_space_cache(cache);
8495 btrfs_put_block_group(cache);
8496 return ret;
8497 }
8498
Chris Mason6324fbf2008-03-24 15:01:59 -04008499 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
8500 &cache->space_info);
Josef Bacik8c579fe2013-04-02 12:40:42 -04008501 if (ret) {
8502 btrfs_remove_free_space_cache(cache);
8503 spin_lock(&root->fs_info->block_group_cache_lock);
8504 rb_erase(&cache->cache_node,
8505 &root->fs_info->block_group_cache_tree);
8506 spin_unlock(&root->fs_info->block_group_cache_lock);
8507 btrfs_put_block_group(cache);
8508 return ret;
8509 }
Li Zefanc7c144d2011-12-07 10:39:22 +08008510 update_global_block_rsv(root->fs_info);
Josef Bacik1b2da372009-09-11 16:11:20 -04008511
8512 spin_lock(&cache->space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04008513 cache->space_info->bytes_readonly += cache->bytes_super;
Josef Bacik1b2da372009-09-11 16:11:20 -04008514 spin_unlock(&cache->space_info->lock);
8515
Yan, Zhengb742bb82010-05-16 10:46:24 -04008516 __link_block_group(cache->space_info, cache);
Chris Mason6324fbf2008-03-24 15:01:59 -04008517
Josef Bacikea658ba2012-09-11 16:57:25 -04008518 list_add_tail(&cache->new_bg_list, &trans->new_bgs);
Chris Mason6324fbf2008-03-24 15:01:59 -04008519
Chris Masond18a2c42008-04-04 15:40:00 -04008520 set_avail_alloc_bits(extent_root->fs_info, type);
Chris Mason925baed2008-06-25 16:01:30 -04008521
Chris Mason6324fbf2008-03-24 15:01:59 -04008522 return 0;
8523}
Zheng Yan1a40e232008-09-26 10:09:34 -04008524
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02008525static void clear_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
8526{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03008527 u64 extra_flags = chunk_to_extended(flags) &
8528 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02008529
Miao Xiede98ced2013-01-29 10:13:12 +00008530 write_seqlock(&fs_info->profiles_lock);
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02008531 if (flags & BTRFS_BLOCK_GROUP_DATA)
8532 fs_info->avail_data_alloc_bits &= ~extra_flags;
8533 if (flags & BTRFS_BLOCK_GROUP_METADATA)
8534 fs_info->avail_metadata_alloc_bits &= ~extra_flags;
8535 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
8536 fs_info->avail_system_alloc_bits &= ~extra_flags;
Miao Xiede98ced2013-01-29 10:13:12 +00008537 write_sequnlock(&fs_info->profiles_lock);
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02008538}
8539
Zheng Yan1a40e232008-09-26 10:09:34 -04008540int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
8541 struct btrfs_root *root, u64 group_start)
8542{
8543 struct btrfs_path *path;
8544 struct btrfs_block_group_cache *block_group;
Chris Mason44fb5512009-06-04 15:34:51 -04008545 struct btrfs_free_cluster *cluster;
Josef Bacik0af3d002010-06-21 14:48:16 -04008546 struct btrfs_root *tree_root = root->fs_info->tree_root;
Zheng Yan1a40e232008-09-26 10:09:34 -04008547 struct btrfs_key key;
Josef Bacik0af3d002010-06-21 14:48:16 -04008548 struct inode *inode;
Zheng Yan1a40e232008-09-26 10:09:34 -04008549 int ret;
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02008550 int index;
Josef Bacik89a55892010-10-14 14:52:27 -04008551 int factor;
Zheng Yan1a40e232008-09-26 10:09:34 -04008552
Zheng Yan1a40e232008-09-26 10:09:34 -04008553 root = root->fs_info->extent_root;
8554
8555 block_group = btrfs_lookup_block_group(root->fs_info, group_start);
8556 BUG_ON(!block_group);
Yan Zhengc146afa2008-11-12 14:34:12 -05008557 BUG_ON(!block_group->ro);
Zheng Yan1a40e232008-09-26 10:09:34 -04008558
liubo9f7c43c2011-03-07 02:13:33 +00008559 /*
8560 * Free the reserved super bytes from this block group before
8561 * remove it.
8562 */
8563 free_excluded_extents(root, block_group);
8564
Zheng Yan1a40e232008-09-26 10:09:34 -04008565 memcpy(&key, &block_group->key, sizeof(key));
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02008566 index = get_block_group_index(block_group);
Josef Bacik89a55892010-10-14 14:52:27 -04008567 if (block_group->flags & (BTRFS_BLOCK_GROUP_DUP |
8568 BTRFS_BLOCK_GROUP_RAID1 |
8569 BTRFS_BLOCK_GROUP_RAID10))
8570 factor = 2;
8571 else
8572 factor = 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04008573
Chris Mason44fb5512009-06-04 15:34:51 -04008574 /* make sure this block group isn't part of an allocation cluster */
8575 cluster = &root->fs_info->data_alloc_cluster;
8576 spin_lock(&cluster->refill_lock);
8577 btrfs_return_cluster_to_free_space(block_group, cluster);
8578 spin_unlock(&cluster->refill_lock);
8579
8580 /*
8581 * make sure this block group isn't part of a metadata
8582 * allocation cluster
8583 */
8584 cluster = &root->fs_info->meta_alloc_cluster;
8585 spin_lock(&cluster->refill_lock);
8586 btrfs_return_cluster_to_free_space(block_group, cluster);
8587 spin_unlock(&cluster->refill_lock);
8588
Zheng Yan1a40e232008-09-26 10:09:34 -04008589 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07008590 if (!path) {
8591 ret = -ENOMEM;
8592 goto out;
8593 }
Zheng Yan1a40e232008-09-26 10:09:34 -04008594
Ilya Dryomov10b2f342011-10-02 13:56:53 +03008595 inode = lookup_free_space_inode(tree_root, block_group, path);
Josef Bacik0af3d002010-06-21 14:48:16 -04008596 if (!IS_ERR(inode)) {
Tsutomu Itohb5324022011-07-19 07:27:20 +00008597 ret = btrfs_orphan_add(trans, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008598 if (ret) {
8599 btrfs_add_delayed_iput(inode);
8600 goto out;
8601 }
Josef Bacik0af3d002010-06-21 14:48:16 -04008602 clear_nlink(inode);
8603 /* One for the block groups ref */
8604 spin_lock(&block_group->lock);
8605 if (block_group->iref) {
8606 block_group->iref = 0;
8607 block_group->inode = NULL;
8608 spin_unlock(&block_group->lock);
8609 iput(inode);
8610 } else {
8611 spin_unlock(&block_group->lock);
8612 }
8613 /* One for our lookup ref */
Josef Bacik455757c2011-09-19 12:26:24 -04008614 btrfs_add_delayed_iput(inode);
Josef Bacik0af3d002010-06-21 14:48:16 -04008615 }
8616
8617 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
8618 key.offset = block_group->key.objectid;
8619 key.type = 0;
8620
8621 ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1);
8622 if (ret < 0)
8623 goto out;
8624 if (ret > 0)
David Sterbab3b4aa72011-04-21 01:20:15 +02008625 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04008626 if (ret == 0) {
8627 ret = btrfs_del_item(trans, tree_root, path);
8628 if (ret)
8629 goto out;
David Sterbab3b4aa72011-04-21 01:20:15 +02008630 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04008631 }
8632
Yan Zheng3dfdb932009-01-21 10:49:16 -05008633 spin_lock(&root->fs_info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04008634 rb_erase(&block_group->cache_node,
8635 &root->fs_info->block_group_cache_tree);
Liu Boa1897fd2012-12-27 09:01:23 +00008636
8637 if (root->fs_info->first_logical_byte == block_group->key.objectid)
8638 root->fs_info->first_logical_byte = (u64)-1;
Yan Zheng3dfdb932009-01-21 10:49:16 -05008639 spin_unlock(&root->fs_info->block_group_cache_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04008640
Josef Bacik80eb2342008-10-29 14:49:05 -04008641 down_write(&block_group->space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04008642 /*
8643 * we must use list_del_init so people can check to see if they
8644 * are still on the list after taking the semaphore
8645 */
8646 list_del_init(&block_group->list);
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02008647 if (list_empty(&block_group->space_info->block_groups[index]))
8648 clear_avail_alloc_bits(root->fs_info, block_group->flags);
Josef Bacik80eb2342008-10-29 14:49:05 -04008649 up_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04008650
Josef Bacik817d52f2009-07-13 21:29:25 -04008651 if (block_group->cached == BTRFS_CACHE_STARTED)
Yan Zheng11833d62009-09-11 16:11:19 -04008652 wait_block_group_cache_done(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -04008653
8654 btrfs_remove_free_space_cache(block_group);
8655
Yan Zhengc146afa2008-11-12 14:34:12 -05008656 spin_lock(&block_group->space_info->lock);
8657 block_group->space_info->total_bytes -= block_group->key.offset;
8658 block_group->space_info->bytes_readonly -= block_group->key.offset;
Josef Bacik89a55892010-10-14 14:52:27 -04008659 block_group->space_info->disk_total -= block_group->key.offset * factor;
Yan Zhengc146afa2008-11-12 14:34:12 -05008660 spin_unlock(&block_group->space_info->lock);
Chris Mason283bb192009-07-24 16:30:55 -04008661
Josef Bacik0af3d002010-06-21 14:48:16 -04008662 memcpy(&key, &block_group->key, sizeof(key));
8663
Chris Mason283bb192009-07-24 16:30:55 -04008664 btrfs_clear_space_info_full(root->fs_info);
Yan Zhengc146afa2008-11-12 14:34:12 -05008665
Chris Masonfa9c0d72009-04-03 09:47:43 -04008666 btrfs_put_block_group(block_group);
8667 btrfs_put_block_group(block_group);
Zheng Yan1a40e232008-09-26 10:09:34 -04008668
8669 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
8670 if (ret > 0)
8671 ret = -EIO;
8672 if (ret < 0)
8673 goto out;
8674
8675 ret = btrfs_del_item(trans, root, path);
8676out:
8677 btrfs_free_path(path);
8678 return ret;
8679}
liuboacce9522011-01-06 19:30:25 +08008680
liuboc59021f2011-03-07 02:13:14 +00008681int btrfs_init_space_info(struct btrfs_fs_info *fs_info)
8682{
8683 struct btrfs_space_info *space_info;
liubo1aba86d2011-04-08 08:44:37 +00008684 struct btrfs_super_block *disk_super;
8685 u64 features;
8686 u64 flags;
8687 int mixed = 0;
liuboc59021f2011-03-07 02:13:14 +00008688 int ret;
8689
David Sterba6c417612011-04-13 15:41:04 +02008690 disk_super = fs_info->super_copy;
liubo1aba86d2011-04-08 08:44:37 +00008691 if (!btrfs_super_root(disk_super))
8692 return 1;
liuboc59021f2011-03-07 02:13:14 +00008693
liubo1aba86d2011-04-08 08:44:37 +00008694 features = btrfs_super_incompat_flags(disk_super);
8695 if (features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
8696 mixed = 1;
liuboc59021f2011-03-07 02:13:14 +00008697
liubo1aba86d2011-04-08 08:44:37 +00008698 flags = BTRFS_BLOCK_GROUP_SYSTEM;
8699 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
liuboc59021f2011-03-07 02:13:14 +00008700 if (ret)
liubo1aba86d2011-04-08 08:44:37 +00008701 goto out;
liuboc59021f2011-03-07 02:13:14 +00008702
liubo1aba86d2011-04-08 08:44:37 +00008703 if (mixed) {
8704 flags = BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA;
8705 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
8706 } else {
8707 flags = BTRFS_BLOCK_GROUP_METADATA;
8708 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
8709 if (ret)
8710 goto out;
8711
8712 flags = BTRFS_BLOCK_GROUP_DATA;
8713 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
8714 }
8715out:
liuboc59021f2011-03-07 02:13:14 +00008716 return ret;
8717}
8718
liuboacce9522011-01-06 19:30:25 +08008719int btrfs_error_unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
8720{
8721 return unpin_extent_range(root, start, end);
8722}
8723
8724int btrfs_error_discard_extent(struct btrfs_root *root, u64 bytenr,
Li Dongyang5378e602011-03-24 10:24:27 +00008725 u64 num_bytes, u64 *actual_bytes)
liuboacce9522011-01-06 19:30:25 +08008726{
Li Dongyang5378e602011-03-24 10:24:27 +00008727 return btrfs_discard_extent(root, bytenr, num_bytes, actual_bytes);
liuboacce9522011-01-06 19:30:25 +08008728}
Li Dongyangf7039b12011-03-24 10:24:28 +00008729
8730int btrfs_trim_fs(struct btrfs_root *root, struct fstrim_range *range)
8731{
8732 struct btrfs_fs_info *fs_info = root->fs_info;
8733 struct btrfs_block_group_cache *cache = NULL;
8734 u64 group_trimmed;
8735 u64 start;
8736 u64 end;
8737 u64 trimmed = 0;
Liu Bo2cac13e2012-02-09 18:17:41 +08008738 u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
Li Dongyangf7039b12011-03-24 10:24:28 +00008739 int ret = 0;
8740
Liu Bo2cac13e2012-02-09 18:17:41 +08008741 /*
8742 * try to trim all FS space, our block group may start from non-zero.
8743 */
8744 if (range->len == total_bytes)
8745 cache = btrfs_lookup_first_block_group(fs_info, range->start);
8746 else
8747 cache = btrfs_lookup_block_group(fs_info, range->start);
Li Dongyangf7039b12011-03-24 10:24:28 +00008748
8749 while (cache) {
8750 if (cache->key.objectid >= (range->start + range->len)) {
8751 btrfs_put_block_group(cache);
8752 break;
8753 }
8754
8755 start = max(range->start, cache->key.objectid);
8756 end = min(range->start + range->len,
8757 cache->key.objectid + cache->key.offset);
8758
8759 if (end - start >= range->minlen) {
8760 if (!block_group_cache_done(cache)) {
Liu Bof6373bf2012-12-27 09:01:18 +00008761 ret = cache_block_group(cache, 0);
Josef Bacik1be41b72013-06-12 13:56:06 -04008762 if (ret) {
8763 btrfs_put_block_group(cache);
8764 break;
8765 }
8766 ret = wait_block_group_cache_done(cache);
8767 if (ret) {
8768 btrfs_put_block_group(cache);
8769 break;
8770 }
Li Dongyangf7039b12011-03-24 10:24:28 +00008771 }
8772 ret = btrfs_trim_block_group(cache,
8773 &group_trimmed,
8774 start,
8775 end,
8776 range->minlen);
8777
8778 trimmed += group_trimmed;
8779 if (ret) {
8780 btrfs_put_block_group(cache);
8781 break;
8782 }
8783 }
8784
8785 cache = next_block_group(fs_info->tree_root, cache);
8786 }
8787
8788 range->len = trimmed;
8789 return ret;
8790}